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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/ui/gtk/global_menu_bar.h" 6 7 #include <gtk/gtk.h> 8 9 #include "chrome/app/chrome_command_ids.h" 10 #include "chrome/browser/prefs/pref_service.h" 11 #include "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/ui/browser.h" 13 #include "chrome/browser/ui/gtk/accelerators_gtk.h" 14 #include "chrome/common/pref_names.h" 15 #include "content/common/notification_service.h" 16 #include "grit/generated_resources.h" 17 #include "ui/base/l10n/l10n_util.h" 18 #include "ui/gfx/gtk_util.h" 19 20 struct GlobalMenuBarCommand { 21 int str_id; 22 int command; 23 }; 24 25 namespace { 26 27 const int MENU_SEPARATOR =-1; 28 const int MENU_END = -2; 29 30 GlobalMenuBarCommand file_menu[] = { 31 { IDS_NEW_TAB, IDC_NEW_TAB }, 32 { IDS_NEW_WINDOW, IDC_NEW_WINDOW }, 33 { IDS_NEW_INCOGNITO_WINDOW, IDC_NEW_INCOGNITO_WINDOW }, 34 { IDS_REOPEN_CLOSED_TABS_LINUX, IDC_RESTORE_TAB }, 35 { IDS_OPEN_FILE_LINUX, IDC_OPEN_FILE }, 36 { IDS_OPEN_LOCATION_LINUX, IDC_FOCUS_LOCATION }, 37 38 { MENU_SEPARATOR, MENU_SEPARATOR }, 39 40 { IDS_CREATE_SHORTCUTS, IDC_CREATE_SHORTCUTS }, 41 42 { MENU_SEPARATOR, MENU_SEPARATOR }, 43 44 { IDS_CLOSE_WINDOW_LINUX, IDC_CLOSE_WINDOW }, 45 { IDS_CLOSE_TAB_LINUX, IDC_CLOSE_TAB }, 46 { IDS_SAVE_PAGE, IDC_SAVE_PAGE }, 47 48 { MENU_SEPARATOR, MENU_SEPARATOR }, 49 50 { IDS_PRINT, IDC_PRINT }, 51 52 { MENU_END, MENU_END } 53 }; 54 55 // TODO(erg): Need to add support for undo/redo/other editing commands that 56 // don't go through the command id framework. 57 GlobalMenuBarCommand edit_menu[] = { 58 // TODO(erg): Undo 59 // TODO(erg): Redo 60 61 // TODO(erg): Separator 62 63 { IDS_CUT, IDC_CUT }, 64 { IDS_COPY, IDC_COPY }, 65 { IDS_PASTE, IDC_PASTE }, 66 // TODO(erg): Delete 67 68 { MENU_SEPARATOR, MENU_SEPARATOR }, 69 70 // TODO(erg): Select All 71 // TODO(erg): Another separator 72 73 { IDS_FIND, IDC_FIND }, 74 75 { MENU_SEPARATOR, MENU_SEPARATOR }, 76 77 { IDS_PREFERENCES, IDC_OPTIONS }, 78 79 { MENU_END, MENU_END } 80 }; 81 82 // TODO(erg): The View menu should be overhauled and based on the Firefox view 83 // menu. 84 GlobalMenuBarCommand view_menu[] = { 85 { IDS_SHOW_BOOKMARK_BAR, IDC_SHOW_BOOKMARK_BAR }, 86 87 { MENU_SEPARATOR, MENU_SEPARATOR }, 88 89 { IDS_STOP_MENU_LINUX, IDC_STOP }, 90 { IDS_RELOAD_MENU_LINUX, IDC_RELOAD }, 91 92 { MENU_SEPARATOR, MENU_SEPARATOR }, 93 94 { IDS_FULLSCREEN, IDC_FULLSCREEN }, 95 { IDS_TEXT_DEFAULT_LINUX, IDC_ZOOM_NORMAL }, 96 { IDS_TEXT_BIGGER_LINUX, IDC_ZOOM_PLUS }, 97 { IDS_TEXT_SMALLER_LINUX, IDC_ZOOM_MINUS }, 98 99 { MENU_END, MENU_END } 100 }; 101 102 GlobalMenuBarCommand tools_menu[] = { 103 { IDS_SHOW_DOWNLOADS, IDC_SHOW_DOWNLOADS }, 104 { IDS_SHOW_HISTORY, IDC_SHOW_HISTORY }, 105 { IDS_SHOW_EXTENSIONS, IDC_MANAGE_EXTENSIONS }, 106 107 { MENU_SEPARATOR, MENU_SEPARATOR }, 108 109 { IDS_TASK_MANAGER, IDC_TASK_MANAGER }, 110 { IDS_CLEAR_BROWSING_DATA, IDC_CLEAR_BROWSING_DATA }, 111 112 { MENU_SEPARATOR, MENU_SEPARATOR }, 113 114 { IDS_VIEW_SOURCE, IDC_VIEW_SOURCE }, 115 { IDS_DEV_TOOLS, IDC_DEV_TOOLS }, 116 { IDS_DEV_TOOLS_CONSOLE, IDC_DEV_TOOLS_CONSOLE }, 117 118 { MENU_END, MENU_END } 119 }; 120 121 GlobalMenuBarCommand help_menu[] = { 122 { IDS_FEEDBACK, IDC_FEEDBACK }, 123 { IDS_HELP_PAGE , IDC_HELP_PAGE }, 124 { MENU_END, MENU_END } 125 }; 126 127 } // namespace 128 129 GlobalMenuBar::GlobalMenuBar(Browser* browser, 130 BrowserWindowGtk* window) 131 : browser_(browser), 132 browser_window_(window), 133 menu_bar_(gtk_menu_bar_new()), 134 dummy_accel_group_(gtk_accel_group_new()), 135 block_activation_(false) { 136 // The global menu bar should never actually be shown in the app; it should 137 // instead remain in our widget hierarchy simply to be noticed by third party 138 // components. 139 gtk_widget_set_no_show_all(menu_bar_, TRUE); 140 141 // Set a nice name so it shows up in gtkparasite and others. 142 gtk_widget_set_name(menu_bar_, "chrome-hidden-global-menubar"); 143 144 BuildGtkMenuFrom(IDS_FILE_MENU_LINUX, &id_to_menu_item_, file_menu); 145 BuildGtkMenuFrom(IDS_EDIT_MENU_LINUX, &id_to_menu_item_, edit_menu); 146 BuildGtkMenuFrom(IDS_VIEW_MENU_LINUX, &id_to_menu_item_, view_menu); 147 BuildGtkMenuFrom(IDS_TOOLS_MENU_LINUX, &id_to_menu_item_, tools_menu); 148 BuildGtkMenuFrom(IDS_HELP_MENU_LINUX, &id_to_menu_item_, help_menu); 149 150 for (IDMenuItemMap::const_iterator it = id_to_menu_item_.begin(); 151 it != id_to_menu_item_.end(); ++it) { 152 // Get the starting enabled state. 153 gtk_widget_set_sensitive( 154 it->second, 155 browser_->command_updater()->IsCommandEnabled(it->first)); 156 157 // Set the accelerator for each menu item. 158 const ui::AcceleratorGtk* accelerator_gtk = 159 AcceleratorsGtk::GetInstance()->GetPrimaryAcceleratorForCommand( 160 it->first); 161 if (accelerator_gtk) { 162 gtk_widget_add_accelerator(it->second, 163 "activate", 164 dummy_accel_group_, 165 accelerator_gtk->GetGdkKeyCode(), 166 accelerator_gtk->gdk_modifier_type(), 167 GTK_ACCEL_VISIBLE); 168 } 169 170 browser_->command_updater()->AddCommandObserver(it->first, this); 171 } 172 173 // Listen for bookmark bar visibility changes and set the initial state. 174 registrar_.Add(this, NotificationType::BOOKMARK_BAR_VISIBILITY_PREF_CHANGED, 175 NotificationService::AllSources()); 176 Observe(NotificationType::BOOKMARK_BAR_VISIBILITY_PREF_CHANGED, 177 NotificationService::AllSources(), 178 NotificationService::NoDetails()); 179 } 180 181 GlobalMenuBar::~GlobalMenuBar() { 182 for (IDMenuItemMap::const_iterator it = id_to_menu_item_.begin(); 183 it != id_to_menu_item_.end(); ++it) { 184 browser_->command_updater()->RemoveCommandObserver(it->first, this); 185 } 186 187 g_object_unref(dummy_accel_group_); 188 } 189 190 void GlobalMenuBar::BuildGtkMenuFrom(int menu_str_id, 191 std::map<int, GtkWidget*>* id_to_menu_item, 192 GlobalMenuBarCommand* commands) { 193 GtkWidget* menu = gtk_menu_new(); 194 for (int i = 0; commands[i].str_id != MENU_END; ++i) { 195 GtkWidget* menu_item = NULL; 196 if (commands[i].str_id == MENU_SEPARATOR) { 197 menu_item = gtk_separator_menu_item_new(); 198 } else { 199 int command_id = commands[i].command; 200 std::string label = 201 gfx::ConvertAcceleratorsFromWindowsStyle( 202 l10n_util::GetStringUTF8(commands[i].str_id)); 203 204 if (command_id == IDC_SHOW_BOOKMARK_BAR) 205 menu_item = gtk_check_menu_item_new_with_mnemonic(label.c_str()); 206 else 207 menu_item = gtk_menu_item_new_with_mnemonic(label.c_str()); 208 209 id_to_menu_item->insert(std::make_pair(command_id, menu_item)); 210 g_object_set_data(G_OBJECT(menu_item), "command-id", 211 GINT_TO_POINTER(command_id)); 212 g_signal_connect(menu_item, "activate", 213 G_CALLBACK(OnItemActivatedThunk), this); 214 } 215 gtk_widget_show(menu_item); 216 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item); 217 } 218 219 gtk_widget_show(menu); 220 221 GtkWidget* menu_item = gtk_menu_item_new_with_mnemonic( 222 gfx::ConvertAcceleratorsFromWindowsStyle( 223 l10n_util::GetStringUTF8(menu_str_id)).c_str()); 224 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), menu); 225 gtk_widget_show(menu_item); 226 227 gtk_menu_shell_append(GTK_MENU_SHELL(menu_bar_), menu_item); 228 } 229 230 void GlobalMenuBar::EnabledStateChangedForCommand(int id, bool enabled) { 231 IDMenuItemMap::iterator it = id_to_menu_item_.find(id); 232 if (it != id_to_menu_item_.end()) 233 gtk_widget_set_sensitive(it->second, enabled); 234 } 235 236 void GlobalMenuBar::Observe(NotificationType type, 237 const NotificationSource& source, 238 const NotificationDetails& details) { 239 DCHECK(type.value == NotificationType::BOOKMARK_BAR_VISIBILITY_PREF_CHANGED); 240 241 IDMenuItemMap::iterator it = id_to_menu_item_.find(IDC_SHOW_BOOKMARK_BAR); 242 if (it != id_to_menu_item_.end()) { 243 PrefService* prefs = browser_->profile()->GetPrefs(); 244 245 block_activation_ = true; 246 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(it->second), 247 prefs->GetBoolean(prefs::kShowBookmarkBar)); 248 block_activation_ = false; 249 } 250 } 251 252 void GlobalMenuBar::OnItemActivated(GtkWidget* sender) { 253 if (block_activation_) 254 return; 255 256 int id = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(sender), "command-id")); 257 browser_->ExecuteCommandIfEnabled(id); 258 } 259