Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(386)

Side by Side Diff: chrome/browser/ui/libgtk2ui/menu_util.cc

Issue 18334003: Linux status icon for Ubuntu Unity (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: file restored Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/libgtk2ui/menu_util.h"
6
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/app/chrome_command_ids.h"
9 #include "chrome/browser/ui/libgtk2ui/gtk2_util.h"
10 #include "chrome/browser/ui/libgtk2ui/skia_utils_gtk2.h"
11 #include "ui/base/accelerators/accelerator.h"
12 #include "ui/base/models/menu_model.h"
13
14 namespace libgtk2ui {
15
16 GtkWidget* BuildMenuItemWithImage(const std::string& label, GtkWidget* image) {
17 GtkWidget* menu_item = gtk_image_menu_item_new_with_mnemonic(label.c_str());
18 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menu_item), image);
19 return menu_item;
20 }
21
22 GtkWidget* BuildMenuItemWithImage(const std::string& label,
23 const gfx::Image& icon) {
24 GdkPixbuf* pixbuf = GdkPixbufFromSkBitmap(*icon.ToSkBitmap());
25
26 GtkWidget* menu_item =
27 BuildMenuItemWithImage(label, gtk_image_new_from_pixbuf(pixbuf));
28 g_object_unref(pixbuf);
29 return menu_item;
30 }
31
32 GtkWidget* BuildMenuItemWithLabel(const std::string& label) {
33 return gtk_menu_item_new_with_mnemonic(label.c_str());
34 }
35
36 ui::MenuModel* ModelForMenuItem(GtkMenuItem* menu_item) {
37 return reinterpret_cast<ui::MenuModel*>(
38 g_object_get_data(G_OBJECT(menu_item), "model"));
39 }
40
41 GtkWidget* AppendMenuItemToMenu(int index,
42 ui::MenuModel* model,
43 GtkWidget* menu_item,
44 GtkWidget* menu,
45 bool connect_to_activate,
46 GCallback item_activated_cb,
47 void* this_ptr) {
48 // Set the ID of a menu item.
49 // Add 1 to the menu_id to avoid setting zero (null) to "menu-id".
50 g_object_set_data(G_OBJECT(menu_item), "menu-id", GINT_TO_POINTER(index + 1));
51
52 // Native menu items do their own thing, so only selectively listen for the
53 // activate signal.
54 if (connect_to_activate) {
55 g_signal_connect(menu_item, "activate", item_activated_cb, this_ptr);
56 }
57
58 // AppendMenuItemToMenu is used both internally when we control menu creation
59 // from a model (where the model can choose to hide certain menu items), and
60 // with immediate commands which don't provide the option.
61 if (model) {
62 if (model->IsVisibleAt(index))
63 gtk_widget_show(menu_item);
64 } else {
65 gtk_widget_show(menu_item);
66 }
67 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);
68 return menu_item;
69 }
70
71 bool GetMenuItemID(GtkWidget* menu_item, int* menu_id) {
72 gpointer id_ptr = g_object_get_data(G_OBJECT(menu_item), "menu-id");
73 if (id_ptr != NULL) {
74 *menu_id = GPOINTER_TO_INT(id_ptr) - 1;
75 return true;
76 }
77
78 return false;
79 }
80
81 void ExecuteCommand(ui::MenuModel* model, int id) {
82 GdkEvent* event = gtk_get_current_event();
83 int event_flags = 0;
84
85 if (event && event->type == GDK_BUTTON_RELEASE)
86 event_flags = EventFlagsFromGdkState(event->button.state);
87 model->ActivatedAt(id, event_flags);
88
89 if (event)
90 gdk_event_free(event);
91 }
92
93 void BuildSubmenuFromModel(ui::MenuModel* model,
94 GtkWidget* menu,
95 GCallback item_activated_cb,
96 bool* block_activation,
97 void* this_ptr) {
98 std::map<int, GtkWidget*> radio_groups;
99 GtkWidget* menu_item = NULL;
100 for (int i = 0; i < model->GetItemCount(); ++i) {
101 gfx::Image icon;
102 std::string label =
103 ConvertAcceleratorsFromWindowsStyle(UTF16ToUTF8(model->GetLabelAt(i)));
104
105 bool connect_to_activate = true;
106
107 switch (model->GetTypeAt(i)) {
108 case ui::MenuModel::TYPE_SEPARATOR:
109 menu_item = gtk_separator_menu_item_new();
110 break;
111
112 case ui::MenuModel::TYPE_CHECK:
113 menu_item = gtk_check_menu_item_new_with_mnemonic(label.c_str());
114 break;
115
116 case ui::MenuModel::TYPE_RADIO: {
117 std::map<int, GtkWidget*>::iterator iter =
118 radio_groups.find(model->GetGroupIdAt(i));
119
120 if (iter == radio_groups.end()) {
121 menu_item =
122 gtk_radio_menu_item_new_with_mnemonic(NULL, label.c_str());
123 radio_groups[model->GetGroupIdAt(i)] = menu_item;
124 } else {
125 menu_item = gtk_radio_menu_item_new_with_mnemonic_from_widget(
126 GTK_RADIO_MENU_ITEM(iter->second), label.c_str());
127 }
128 break;
129 }
130 case ui::MenuModel::TYPE_BUTTON_ITEM: {
131 NOTIMPLEMENTED();
132 break;
133 }
134 case ui::MenuModel::TYPE_SUBMENU:
135 case ui::MenuModel::TYPE_COMMAND: {
136 if (model->GetIconAt(i, &icon))
137 menu_item = BuildMenuItemWithImage(label, icon);
138 else
139 menu_item = BuildMenuItemWithLabel(label);
140 if (GTK_IS_IMAGE_MENU_ITEM(menu_item)) {
141 SetAlwaysShowImage(menu_item);
142 }
143 break;
144 }
145
146 default:
147 NOTREACHED();
148 }
149
150 if (model->GetTypeAt(i) == ui::MenuModel::TYPE_SUBMENU) {
151 GtkWidget* submenu = gtk_menu_new();
152 ui::MenuModel* submenu_model = model->GetSubmenuModelAt(i);
153 BuildSubmenuFromModel(submenu_model,
154 submenu,
155 item_activated_cb,
156 block_activation,
157 this_ptr);
158 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), submenu);
159
160 // Update all the menu item info in the newly-generated menu.
161 gtk_container_foreach(
162 GTK_CONTAINER(submenu), SetMenuItemInfo, block_activation);
163 submenu_model->MenuWillShow();
164 connect_to_activate = false;
165 }
166
167 ui::Accelerator accelerator;
168 if (model->GetAcceleratorAt(i, &accelerator)) {
169 gtk_widget_add_accelerator(menu_item,
170 "activate",
171 NULL,
172 GetGdkKeyCodeForAccelerator(accelerator),
173 GetGdkModifierForAccelerator(accelerator),
174 GTK_ACCEL_VISIBLE);
175 }
176
177 g_object_set_data(G_OBJECT(menu_item), "model", model);
178 AppendMenuItemToMenu(i,
179 model,
180 menu_item,
181 menu,
182 connect_to_activate,
183 item_activated_cb,
184 this_ptr);
185
186 menu_item = NULL;
187 }
188 }
189
190 void SetMenuItemInfo(GtkWidget* widget, void* block_activation_ptr) {
191 if (GTK_IS_SEPARATOR_MENU_ITEM(widget)) {
192 // We need to explicitly handle this case because otherwise we'll ask the
193 // menu delegate about something with an invalid id.
194 return;
195 }
196
197 int id;
198 if (!GetMenuItemID(widget, &id))
199 return;
200
201 ui::MenuModel* model = ModelForMenuItem(GTK_MENU_ITEM(widget));
202 if (!model) {
203 // If we're not providing the sub menu, then there's no model. For
204 // example, the IME submenu doesn't have a model.
205 return;
206 }
207 bool* block_activation = static_cast<bool*>(block_activation_ptr);
208
209 if (GTK_IS_CHECK_MENU_ITEM(widget)) {
210 GtkCheckMenuItem* item = GTK_CHECK_MENU_ITEM(widget);
211
212 // gtk_check_menu_item_set_active() will send the activate signal. Touching
213 // the underlying "active" property will also call the "activate" handler
214 // for this menu item. So we prevent the "activate" handler from
215 // being called while we set the checkbox.
216 // Why not use one of the glib signal-blocking functions? Because when we
217 // toggle a radio button, it will deactivate one of the other radio buttons,
218 // which we don't have a pointer to.
219 *block_activation = true;
220 gtk_check_menu_item_set_active(item, model->IsItemCheckedAt(id));
221 *block_activation = false;
222 }
223
224 if (GTK_IS_MENU_ITEM(widget)) {
225 gtk_widget_set_sensitive(widget, model->IsEnabledAt(id));
226
227 if (model->IsVisibleAt(id)) {
228 // Update the menu item label if it is dynamic.
229 if (model->IsItemDynamicAt(id)) {
230 std::string label = ConvertAcceleratorsFromWindowsStyle(
231 UTF16ToUTF8(model->GetLabelAt(id)));
232
233 gtk_menu_item_set_label(GTK_MENU_ITEM(widget), label.c_str());
234 if (GTK_IS_IMAGE_MENU_ITEM(widget)) {
235 gfx::Image icon;
236 if (model->GetIconAt(id, &icon)) {
237 gtk_image_menu_item_set_image(
238 GTK_IMAGE_MENU_ITEM(widget),
239 gtk_image_new_from_pixbuf(
240 GdkPixbufFromSkBitmap(*icon.ToSkBitmap())));
241 } else {
242 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(widget), NULL);
243 }
244 }
245 }
246
247 gtk_widget_show(widget);
248 } else {
249 gtk_widget_hide(widget);
250 }
251
252 GtkWidget* submenu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(widget));
253 if (submenu) {
254 gtk_container_foreach(
255 GTK_CONTAINER(submenu), &SetMenuItemInfo, block_activation_ptr);
256 }
257 }
258 }
259
260 } // namespace libgtk2ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698