OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/libgtkui/menu_util.h" | 5 #include "chrome/browser/ui/libgtkui/menu_util.h" |
6 | 6 |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "chrome/app/chrome_command_ids.h" | 8 #include "chrome/app/chrome_command_ids.h" |
9 #include "chrome/browser/ui/libgtkui/gtk_util.h" | 9 #include "chrome/browser/ui/libgtkui/gtk_util.h" |
10 #include "chrome/browser/ui/libgtkui/skia_utils_gtk.h" | 10 #include "chrome/browser/ui/libgtkui/skia_utils_gtk.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 gtk_widget_show(menu_item); | 66 gtk_widget_show(menu_item); |
67 } else { | 67 } else { |
68 gtk_widget_show(menu_item); | 68 gtk_widget_show(menu_item); |
69 } | 69 } |
70 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item); | 70 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item); |
71 return menu_item; | 71 return menu_item; |
72 } | 72 } |
73 | 73 |
74 bool GetMenuItemID(GtkWidget* menu_item, int* menu_id) { | 74 bool GetMenuItemID(GtkWidget* menu_item, int* menu_id) { |
75 gpointer id_ptr = g_object_get_data(G_OBJECT(menu_item), "menu-id"); | 75 gpointer id_ptr = g_object_get_data(G_OBJECT(menu_item), "menu-id"); |
76 if (id_ptr != NULL) { | 76 if (id_ptr != nullptr) { |
77 *menu_id = GPOINTER_TO_INT(id_ptr) - 1; | 77 *menu_id = GPOINTER_TO_INT(id_ptr) - 1; |
78 return true; | 78 return true; |
79 } | 79 } |
80 | 80 |
81 return false; | 81 return false; |
82 } | 82 } |
83 | 83 |
84 void ExecuteCommand(ui::MenuModel* model, int id) { | 84 void ExecuteCommand(ui::MenuModel* model, int id) { |
85 GdkEvent* event = gtk_get_current_event(); | 85 GdkEvent* event = gtk_get_current_event(); |
86 int event_flags = 0; | 86 int event_flags = 0; |
87 | 87 |
88 if (event && event->type == GDK_BUTTON_RELEASE) | 88 if (event && event->type == GDK_BUTTON_RELEASE) |
89 event_flags = EventFlagsFromGdkState(event->button.state); | 89 event_flags = EventFlagsFromGdkState(event->button.state); |
90 model->ActivatedAt(id, event_flags); | 90 model->ActivatedAt(id, event_flags); |
91 | 91 |
92 if (event) | 92 if (event) |
93 gdk_event_free(event); | 93 gdk_event_free(event); |
94 } | 94 } |
95 | 95 |
96 void BuildSubmenuFromModel(ui::MenuModel* model, | 96 void BuildSubmenuFromModel(ui::MenuModel* model, |
97 GtkWidget* menu, | 97 GtkWidget* menu, |
98 GCallback item_activated_cb, | 98 GCallback item_activated_cb, |
99 bool* block_activation, | 99 bool* block_activation, |
100 void* this_ptr) { | 100 void* this_ptr) { |
101 std::map<int, GtkWidget*> radio_groups; | 101 std::map<int, GtkWidget*> radio_groups; |
102 GtkWidget* menu_item = NULL; | 102 GtkWidget* menu_item = nullptr; |
103 for (int i = 0; i < model->GetItemCount(); ++i) { | 103 for (int i = 0; i < model->GetItemCount(); ++i) { |
104 gfx::Image icon; | 104 gfx::Image icon; |
105 std::string label = ui::ConvertAcceleratorsFromWindowsStyle( | 105 std::string label = ui::ConvertAcceleratorsFromWindowsStyle( |
106 base::UTF16ToUTF8(model->GetLabelAt(i))); | 106 base::UTF16ToUTF8(model->GetLabelAt(i))); |
107 | 107 |
108 bool connect_to_activate = true; | 108 bool connect_to_activate = true; |
109 | 109 |
110 switch (model->GetTypeAt(i)) { | 110 switch (model->GetTypeAt(i)) { |
111 case ui::MenuModel::TYPE_SEPARATOR: | 111 case ui::MenuModel::TYPE_SEPARATOR: |
112 menu_item = gtk_separator_menu_item_new(); | 112 menu_item = gtk_separator_menu_item_new(); |
113 break; | 113 break; |
114 | 114 |
115 case ui::MenuModel::TYPE_CHECK: | 115 case ui::MenuModel::TYPE_CHECK: |
116 menu_item = gtk_check_menu_item_new_with_mnemonic(label.c_str()); | 116 menu_item = gtk_check_menu_item_new_with_mnemonic(label.c_str()); |
117 break; | 117 break; |
118 | 118 |
119 case ui::MenuModel::TYPE_RADIO: { | 119 case ui::MenuModel::TYPE_RADIO: { |
120 std::map<int, GtkWidget*>::iterator iter = | 120 std::map<int, GtkWidget*>::iterator iter = |
121 radio_groups.find(model->GetGroupIdAt(i)); | 121 radio_groups.find(model->GetGroupIdAt(i)); |
122 | 122 |
123 if (iter == radio_groups.end()) { | 123 if (iter == radio_groups.end()) { |
124 menu_item = | 124 menu_item = |
125 gtk_radio_menu_item_new_with_mnemonic(NULL, label.c_str()); | 125 gtk_radio_menu_item_new_with_mnemonic(nullptr, label.c_str()); |
126 radio_groups[model->GetGroupIdAt(i)] = menu_item; | 126 radio_groups[model->GetGroupIdAt(i)] = menu_item; |
127 } else { | 127 } else { |
128 menu_item = gtk_radio_menu_item_new_with_mnemonic_from_widget( | 128 menu_item = gtk_radio_menu_item_new_with_mnemonic_from_widget( |
129 GTK_RADIO_MENU_ITEM(iter->second), label.c_str()); | 129 GTK_RADIO_MENU_ITEM(iter->second), label.c_str()); |
130 } | 130 } |
131 break; | 131 break; |
132 } | 132 } |
133 case ui::MenuModel::TYPE_BUTTON_ITEM: { | 133 case ui::MenuModel::TYPE_BUTTON_ITEM: { |
134 NOTIMPLEMENTED(); | 134 NOTIMPLEMENTED(); |
135 break; | 135 break; |
(...skipping 27 matching lines...) Expand all Loading... |
163 | 163 |
164 // Update all the menu item info in the newly-generated menu. | 164 // Update all the menu item info in the newly-generated menu. |
165 gtk_container_foreach( | 165 gtk_container_foreach( |
166 GTK_CONTAINER(submenu), SetMenuItemInfo, block_activation); | 166 GTK_CONTAINER(submenu), SetMenuItemInfo, block_activation); |
167 submenu_model->MenuWillShow(); | 167 submenu_model->MenuWillShow(); |
168 connect_to_activate = false; | 168 connect_to_activate = false; |
169 } | 169 } |
170 | 170 |
171 ui::Accelerator accelerator; | 171 ui::Accelerator accelerator; |
172 if (model->GetAcceleratorAt(i, &accelerator)) { | 172 if (model->GetAcceleratorAt(i, &accelerator)) { |
173 gtk_widget_add_accelerator(menu_item, | 173 gtk_widget_add_accelerator(menu_item, "activate", nullptr, |
174 "activate", | |
175 NULL, | |
176 GetGdkKeyCodeForAccelerator(accelerator), | 174 GetGdkKeyCodeForAccelerator(accelerator), |
177 GetGdkModifierForAccelerator(accelerator), | 175 GetGdkModifierForAccelerator(accelerator), |
178 GTK_ACCEL_VISIBLE); | 176 GTK_ACCEL_VISIBLE); |
179 } | 177 } |
180 | 178 |
181 g_object_set_data(G_OBJECT(menu_item), "model", model); | 179 g_object_set_data(G_OBJECT(menu_item), "model", model); |
182 AppendMenuItemToMenu(i, | 180 AppendMenuItemToMenu(i, |
183 model, | 181 model, |
184 menu_item, | 182 menu_item, |
185 menu, | 183 menu, |
186 connect_to_activate, | 184 connect_to_activate, |
187 item_activated_cb, | 185 item_activated_cb, |
188 this_ptr); | 186 this_ptr); |
189 | 187 |
190 menu_item = NULL; | 188 menu_item = nullptr; |
191 } | 189 } |
192 } | 190 } |
193 | 191 |
194 void SetMenuItemInfo(GtkWidget* widget, void* block_activation_ptr) { | 192 void SetMenuItemInfo(GtkWidget* widget, void* block_activation_ptr) { |
195 if (GTK_IS_SEPARATOR_MENU_ITEM(widget)) { | 193 if (GTK_IS_SEPARATOR_MENU_ITEM(widget)) { |
196 // We need to explicitly handle this case because otherwise we'll ask the | 194 // We need to explicitly handle this case because otherwise we'll ask the |
197 // menu delegate about something with an invalid id. | 195 // menu delegate about something with an invalid id. |
198 return; | 196 return; |
199 } | 197 } |
200 | 198 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 | 234 |
237 gtk_menu_item_set_label(GTK_MENU_ITEM(widget), label.c_str()); | 235 gtk_menu_item_set_label(GTK_MENU_ITEM(widget), label.c_str()); |
238 if (GTK_IS_IMAGE_MENU_ITEM(widget)) { | 236 if (GTK_IS_IMAGE_MENU_ITEM(widget)) { |
239 gfx::Image icon; | 237 gfx::Image icon; |
240 if (model->GetIconAt(id, &icon)) { | 238 if (model->GetIconAt(id, &icon)) { |
241 GdkPixbuf* pixbuf = GdkPixbufFromSkBitmap(*icon.ToSkBitmap()); | 239 GdkPixbuf* pixbuf = GdkPixbufFromSkBitmap(*icon.ToSkBitmap()); |
242 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(widget), | 240 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(widget), |
243 gtk_image_new_from_pixbuf(pixbuf)); | 241 gtk_image_new_from_pixbuf(pixbuf)); |
244 g_object_unref(pixbuf); | 242 g_object_unref(pixbuf); |
245 } else { | 243 } else { |
246 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(widget), NULL); | 244 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(widget), nullptr); |
247 } | 245 } |
248 } | 246 } |
249 } | 247 } |
250 | 248 |
251 gtk_widget_show(widget); | 249 gtk_widget_show(widget); |
252 } else { | 250 } else { |
253 gtk_widget_hide(widget); | 251 gtk_widget_hide(widget); |
254 } | 252 } |
255 | 253 |
256 GtkWidget* submenu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(widget)); | 254 GtkWidget* submenu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(widget)); |
257 if (submenu) { | 255 if (submenu) { |
258 gtk_container_foreach( | 256 gtk_container_foreach( |
259 GTK_CONTAINER(submenu), &SetMenuItemInfo, block_activation_ptr); | 257 GTK_CONTAINER(submenu), &SetMenuItemInfo, block_activation_ptr); |
260 } | 258 } |
261 } | 259 } |
262 } | 260 } |
263 | 261 |
264 } // namespace libgtkui | 262 } // namespace libgtkui |
OLD | NEW |