OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/gtk/global_menu_bar.h" | 5 #include "chrome/browser/ui/gtk/global_menu_bar.h" |
6 | 6 |
7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
8 | 8 |
9 #include "chrome/app/chrome_command_ids.h" | 9 #include "chrome/app/chrome_command_ids.h" |
10 #include "chrome/browser/prefs/pref_service.h" | 10 #include "chrome/browser/prefs/pref_service.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 { IDS_CLOSE_TAB_LINUX, IDC_CLOSE_TAB }, | 49 { IDS_CLOSE_TAB_LINUX, IDC_CLOSE_TAB }, |
50 { IDS_SAVE_PAGE, IDC_SAVE_PAGE }, | 50 { IDS_SAVE_PAGE, IDC_SAVE_PAGE }, |
51 | 51 |
52 { MENU_SEPARATOR, MENU_SEPARATOR }, | 52 { MENU_SEPARATOR, MENU_SEPARATOR }, |
53 | 53 |
54 { IDS_PRINT, IDC_PRINT }, | 54 { IDS_PRINT, IDC_PRINT }, |
55 | 55 |
56 { MENU_END, MENU_END } | 56 { MENU_END, MENU_END } |
57 }; | 57 }; |
58 | 58 |
59 // TODO(erg): Need to add support for undo/redo/other editing commands that | |
60 // don't go through the command id framework. | |
61 GlobalMenuBarCommand edit_menu[] = { | 59 GlobalMenuBarCommand edit_menu[] = { |
62 // TODO(erg): Undo | |
63 // TODO(erg): Redo | |
64 | |
65 // TODO(erg): Separator | |
66 | |
67 { IDS_CUT, IDC_CUT }, | 60 { IDS_CUT, IDC_CUT }, |
68 { IDS_COPY, IDC_COPY }, | 61 { IDS_COPY, IDC_COPY }, |
69 { IDS_PASTE, IDC_PASTE }, | 62 { IDS_PASTE, IDC_PASTE }, |
70 // TODO(erg): Delete | |
71 | 63 |
72 { MENU_SEPARATOR, MENU_SEPARATOR }, | 64 { MENU_SEPARATOR, MENU_SEPARATOR }, |
73 | 65 |
74 // TODO(erg): Select All | |
75 // TODO(erg): Another separator | |
76 | |
77 { IDS_FIND, IDC_FIND }, | 66 { IDS_FIND, IDC_FIND }, |
78 | 67 |
79 { MENU_SEPARATOR, MENU_SEPARATOR }, | 68 { MENU_SEPARATOR, MENU_SEPARATOR }, |
80 | 69 |
81 { IDS_PREFERENCES, IDC_OPTIONS }, | 70 { IDS_PREFERENCES, IDC_OPTIONS }, |
82 | 71 |
83 { MENU_END, MENU_END } | 72 { MENU_END, MENU_END } |
84 }; | 73 }; |
85 | 74 |
86 // TODO(erg): The View menu should be overhauled and based on the Firefox view | |
87 // menu. | |
88 GlobalMenuBarCommand view_menu[] = { | 75 GlobalMenuBarCommand view_menu[] = { |
89 { IDS_SHOW_BOOKMARK_BAR, IDC_SHOW_BOOKMARK_BAR }, | 76 { IDS_SHOW_BOOKMARK_BAR, IDC_SHOW_BOOKMARK_BAR }, |
90 | 77 |
91 { MENU_SEPARATOR, MENU_SEPARATOR }, | 78 { MENU_SEPARATOR, MENU_SEPARATOR }, |
92 | 79 |
93 { IDS_STOP_MENU_LINUX, IDC_STOP }, | 80 { IDS_STOP_MENU_LINUX, IDC_STOP }, |
94 { IDS_RELOAD_MENU_LINUX, IDC_RELOAD }, | 81 { IDS_RELOAD_MENU_LINUX, IDC_RELOAD }, |
95 | 82 |
96 { MENU_SEPARATOR, MENU_SEPARATOR }, | 83 { MENU_SEPARATOR, MENU_SEPARATOR }, |
97 | 84 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 dummy_accel_group_(gtk_accel_group_new()), | 156 dummy_accel_group_(gtk_accel_group_new()), |
170 block_activation_(false) { | 157 block_activation_(false) { |
171 // The global menu bar should never actually be shown in the app; it should | 158 // The global menu bar should never actually be shown in the app; it should |
172 // instead remain in our widget hierarchy simply to be noticed by third party | 159 // instead remain in our widget hierarchy simply to be noticed by third party |
173 // components. | 160 // components. |
174 gtk_widget_set_no_show_all(menu_bar_.get(), TRUE); | 161 gtk_widget_set_no_show_all(menu_bar_.get(), TRUE); |
175 | 162 |
176 // Set a nice name so it shows up in gtkparasite and others. | 163 // Set a nice name so it shows up in gtkparasite and others. |
177 gtk_widget_set_name(menu_bar_.get(), "chrome-hidden-global-menubar"); | 164 gtk_widget_set_name(menu_bar_.get(), "chrome-hidden-global-menubar"); |
178 | 165 |
179 BuildGtkMenuFrom(IDS_FILE_MENU_LINUX, &id_to_menu_item_, file_menu); | 166 BuildGtkMenuFrom(IDS_FILE_MENU_LINUX, &id_to_menu_item_, file_menu, NULL); |
180 BuildGtkMenuFrom(IDS_EDIT_MENU_LINUX, &id_to_menu_item_, edit_menu); | 167 BuildGtkMenuFrom(IDS_EDIT_MENU_LINUX, &id_to_menu_item_, edit_menu, NULL); |
181 BuildGtkMenuFrom(IDS_VIEW_MENU_LINUX, &id_to_menu_item_, view_menu); | 168 BuildGtkMenuFrom(IDS_VIEW_MENU_LINUX, &id_to_menu_item_, view_menu, NULL); |
182 history_menu_.Init(BuildGtkMenuFrom(IDS_HISTORY_MENU_LINUX, &id_to_menu_item_, | 169 BuildGtkMenuFrom(IDS_HISTORY_MENU_LINUX, &id_to_menu_item_, |
183 history_menu)); | 170 history_menu, &history_menu_); |
184 bookmark_menu_.Init(BuildGtkMenuFrom(IDS_BOOKMARKS_MENU_LINUX, | 171 BuildGtkMenuFrom(IDS_BOOKMARKS_MENU_LINUX, &id_to_menu_item_, bookmark_menu, |
185 &id_to_menu_item_, bookmark_menu)); | 172 &bookmark_menu_); |
186 BuildGtkMenuFrom(IDS_TOOLS_MENU_LINUX, &id_to_menu_item_, tools_menu); | 173 BuildGtkMenuFrom(IDS_TOOLS_MENU_LINUX, &id_to_menu_item_, tools_menu, NULL); |
187 BuildGtkMenuFrom(IDS_HELP_MENU_LINUX, &id_to_menu_item_, help_menu); | 174 BuildGtkMenuFrom(IDS_HELP_MENU_LINUX, &id_to_menu_item_, help_menu, NULL); |
188 | 175 |
189 for (CommandIDMenuItemMap::const_iterator it = id_to_menu_item_.begin(); | 176 for (CommandIDMenuItemMap::const_iterator it = id_to_menu_item_.begin(); |
190 it != id_to_menu_item_.end(); ++it) { | 177 it != id_to_menu_item_.end(); ++it) { |
191 // Get the starting enabled state. | 178 // Get the starting enabled state. |
192 gtk_widget_set_sensitive( | 179 gtk_widget_set_sensitive( |
193 it->second, | 180 it->second, |
194 browser_->command_updater()->IsCommandEnabled(it->first)); | 181 browser_->command_updater()->IsCommandEnabled(it->first)); |
195 | 182 |
196 // Set the accelerator for each menu item. | 183 // Set the accelerator for each menu item. |
197 const ui::AcceleratorGtk* accelerator_gtk = | 184 const ui::AcceleratorGtk* accelerator_gtk = |
(...skipping 21 matching lines...) Expand all Loading... |
219 | 206 |
220 GlobalMenuBar::~GlobalMenuBar() { | 207 GlobalMenuBar::~GlobalMenuBar() { |
221 for (CommandIDMenuItemMap::const_iterator it = id_to_menu_item_.begin(); | 208 for (CommandIDMenuItemMap::const_iterator it = id_to_menu_item_.begin(); |
222 it != id_to_menu_item_.end(); ++it) { | 209 it != id_to_menu_item_.end(); ++it) { |
223 browser_->command_updater()->RemoveCommandObserver(it->first, this); | 210 browser_->command_updater()->RemoveCommandObserver(it->first, this); |
224 } | 211 } |
225 | 212 |
226 g_object_unref(dummy_accel_group_); | 213 g_object_unref(dummy_accel_group_); |
227 } | 214 } |
228 | 215 |
229 GtkWidget* GlobalMenuBar::BuildGtkMenuFrom( | 216 void GlobalMenuBar::BuildGtkMenuFrom( |
230 int menu_str_id, | 217 int menu_str_id, |
231 std::map<int, GtkWidget*>* id_to_menu_item, | 218 std::map<int, GtkWidget*>* id_to_menu_item, |
232 GlobalMenuBarCommand* commands) { | 219 GlobalMenuBarCommand* commands, |
| 220 GlobalMenuOwner* owner) { |
233 GtkWidget* menu = gtk_menu_new(); | 221 GtkWidget* menu = gtk_menu_new(); |
234 for (int i = 0; commands[i].str_id != MENU_END; ++i) { | 222 for (int i = 0; commands[i].str_id != MENU_END; ++i) { |
235 GtkWidget* menu_item = BuildMenuItem( | 223 GtkWidget* menu_item = BuildMenuItem( |
236 commands[i].str_id, commands[i].command, commands[i].tag, | 224 commands[i].str_id, commands[i].command, commands[i].tag, |
237 id_to_menu_item, menu); | 225 id_to_menu_item, menu); |
238 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item); | 226 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item); |
239 } | 227 } |
240 | 228 |
241 gtk_widget_show(menu); | 229 gtk_widget_show(menu); |
242 | 230 |
243 GtkWidget* menu_item = gtk_menu_item_new_with_mnemonic( | 231 GtkWidget* menu_item = gtk_menu_item_new_with_mnemonic( |
244 gfx::ConvertAcceleratorsFromWindowsStyle( | 232 gfx::ConvertAcceleratorsFromWindowsStyle( |
245 l10n_util::GetStringUTF8(menu_str_id)).c_str()); | 233 l10n_util::GetStringUTF8(menu_str_id)).c_str()); |
| 234 |
| 235 // Give the owner a chance to sink the reference before we add it to the menu |
| 236 // bar. |
| 237 if (owner) |
| 238 owner->Init(menu, menu_item); |
| 239 |
246 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), menu); | 240 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), menu); |
247 gtk_widget_show(menu_item); | 241 gtk_widget_show(menu_item); |
248 gtk_menu_shell_append(GTK_MENU_SHELL(menu_bar_.get()), menu_item); | 242 gtk_menu_shell_append(GTK_MENU_SHELL(menu_bar_.get()), menu_item); |
249 | |
250 return menu; | |
251 } | 243 } |
252 | 244 |
253 GtkWidget* GlobalMenuBar::BuildMenuItem( | 245 GtkWidget* GlobalMenuBar::BuildMenuItem( |
254 int string_id, | 246 int string_id, |
255 int command_id, | 247 int command_id, |
256 int tag_id, | 248 int tag_id, |
257 std::map<int, GtkWidget*>* id_to_menu_item, | 249 std::map<int, GtkWidget*>* id_to_menu_item, |
258 GtkWidget* menu_to_add_to) { | 250 GtkWidget* menu_to_add_to) { |
259 GtkWidget* menu_item = NULL; | 251 GtkWidget* menu_item = NULL; |
260 if (string_id == MENU_SEPARATOR) { | 252 if (string_id == MENU_SEPARATOR) { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 } | 304 } |
313 } | 305 } |
314 | 306 |
315 void GlobalMenuBar::OnItemActivated(GtkWidget* sender) { | 307 void GlobalMenuBar::OnItemActivated(GtkWidget* sender) { |
316 if (block_activation_) | 308 if (block_activation_) |
317 return; | 309 return; |
318 | 310 |
319 int id = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(sender), "command-id")); | 311 int id = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(sender), "command-id")); |
320 browser_->ExecuteCommandIfEnabled(id); | 312 browser_->ExecuteCommandIfEnabled(id); |
321 } | 313 } |
OLD | NEW |