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 "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "chrome/app/chrome_command_ids.h" | 10 #include "chrome/app/chrome_command_ids.h" |
11 #include "chrome/browser/prefs/pref_service.h" | 11 #include "chrome/browser/prefs/pref_service.h" |
12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
14 #include "chrome/browser/ui/gtk/accelerators_gtk.h" | 14 #include "chrome/browser/ui/gtk/accelerators_gtk.h" |
| 15 #include "chrome/browser/ui/gtk/global_bookmark_menu.h" |
15 #include "chrome/browser/ui/gtk/gtk_theme_service.h" | 16 #include "chrome/browser/ui/gtk/gtk_theme_service.h" |
16 #include "chrome/browser/ui/gtk/gtk_util.h" | 17 #include "chrome/browser/ui/gtk/gtk_util.h" |
17 #include "chrome/common/chrome_notification_types.h" | 18 #include "chrome/common/chrome_notification_types.h" |
18 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
19 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
| 21 #include "content/common/notification_details.h" |
20 #include "content/common/notification_service.h" | 22 #include "content/common/notification_service.h" |
21 #include "content/common/notification_source.h" | 23 #include "content/common/notification_source.h" |
22 #include "grit/generated_resources.h" | 24 #include "grit/generated_resources.h" |
23 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
24 #include "ui/gfx/gtk_util.h" | 26 #include "ui/gfx/gtk_util.h" |
25 | 27 |
26 struct GlobalMenuBarCommand { | 28 struct GlobalMenuBarCommand { |
27 int str_id; | 29 int str_id; |
28 int command; | 30 int command; |
29 int tag; | 31 int tag; |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 "activate", | 208 "activate", |
207 dummy_accel_group_, | 209 dummy_accel_group_, |
208 accelerator_gtk->GetGdkKeyCode(), | 210 accelerator_gtk->GetGdkKeyCode(), |
209 accelerator_gtk->gdk_modifier_type(), | 211 accelerator_gtk->gdk_modifier_type(), |
210 GTK_ACCEL_VISIBLE); | 212 GTK_ACCEL_VISIBLE); |
211 } | 213 } |
212 | 214 |
213 browser_->command_updater()->AddCommandObserver(it->first, this); | 215 browser_->command_updater()->AddCommandObserver(it->first, this); |
214 } | 216 } |
215 | 217 |
216 // We listen to all notification sources because the bookmark bar | 218 pref_change_registrar_.Init(browser_->profile()->GetPrefs()); |
217 // state needs to stay in sync between the incognito and normal profiles. | 219 pref_change_registrar_.Add(prefs::kShowBookmarkBar, this); |
218 registrar_.Add(this, | 220 OnBookmarkBarVisibilityChanged(); |
219 chrome::NOTIFICATION_BOOKMARK_BAR_VISIBILITY_PREF_CHANGED, | |
220 NotificationService::AllBrowserContextsAndSources()); | |
221 Observe(chrome::NOTIFICATION_BOOKMARK_BAR_VISIBILITY_PREF_CHANGED, | |
222 Source<Profile>(browser->profile()), | |
223 NotificationService::NoDetails()); | |
224 } | 221 } |
225 | 222 |
226 GlobalMenuBar::~GlobalMenuBar() { | 223 GlobalMenuBar::~GlobalMenuBar() { |
227 Disable(); | 224 Disable(); |
228 g_object_unref(dummy_accel_group_); | 225 g_object_unref(dummy_accel_group_); |
229 } | 226 } |
230 | 227 |
231 void GlobalMenuBar::Disable() { | 228 void GlobalMenuBar::Disable() { |
232 for (CommandIDMenuItemMap::const_iterator it = id_to_menu_item_.begin(); | 229 for (CommandIDMenuItemMap::const_iterator it = id_to_menu_item_.begin(); |
233 it != id_to_menu_item_.end(); ++it) { | 230 it != id_to_menu_item_.end(); ++it) { |
234 browser_->command_updater()->RemoveCommandObserver(it->first, this); | 231 browser_->command_updater()->RemoveCommandObserver(it->first, this); |
235 } | 232 } |
236 id_to_menu_item_.clear(); | 233 id_to_menu_item_.clear(); |
237 | 234 |
238 if (registrar_.IsRegistered(this, | 235 pref_change_registrar_.RemoveAll(); |
239 chrome::NOTIFICATION_BOOKMARK_BAR_VISIBILITY_PREF_CHANGED, | |
240 NotificationService::AllBrowserContextsAndSources())) { | |
241 registrar_.Remove( | |
242 this, | |
243 chrome::NOTIFICATION_BOOKMARK_BAR_VISIBILITY_PREF_CHANGED, | |
244 NotificationService::AllBrowserContextsAndSources()); | |
245 } | |
246 } | 236 } |
247 | 237 |
248 void GlobalMenuBar::BuildGtkMenuFrom( | 238 void GlobalMenuBar::BuildGtkMenuFrom( |
249 int menu_str_id, | 239 int menu_str_id, |
250 std::map<int, GtkWidget*>* id_to_menu_item, | 240 std::map<int, GtkWidget*>* id_to_menu_item, |
251 GlobalMenuBarCommand* commands, | 241 GlobalMenuBarCommand* commands, |
252 GlobalMenuOwner* owner) { | 242 GlobalMenuOwner* owner) { |
253 GtkWidget* menu = gtk_menu_new(); | 243 GtkWidget* menu = gtk_menu_new(); |
254 for (int i = 0; commands[i].str_id != MENU_END; ++i) { | 244 for (int i = 0; commands[i].str_id != MENU_END; ++i) { |
255 GtkWidget* menu_item = BuildMenuItem( | 245 GtkWidget* menu_item = BuildMenuItem( |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 | 304 |
315 void GlobalMenuBar::EnabledStateChangedForCommand(int id, bool enabled) { | 305 void GlobalMenuBar::EnabledStateChangedForCommand(int id, bool enabled) { |
316 CommandIDMenuItemMap::iterator it = id_to_menu_item_.find(id); | 306 CommandIDMenuItemMap::iterator it = id_to_menu_item_.find(id); |
317 if (it != id_to_menu_item_.end()) | 307 if (it != id_to_menu_item_.end()) |
318 gtk_widget_set_sensitive(it->second, enabled); | 308 gtk_widget_set_sensitive(it->second, enabled); |
319 } | 309 } |
320 | 310 |
321 void GlobalMenuBar::Observe(int type, | 311 void GlobalMenuBar::Observe(int type, |
322 const NotificationSource& source, | 312 const NotificationSource& source, |
323 const NotificationDetails& details) { | 313 const NotificationDetails& details) { |
324 DCHECK_EQ(type, chrome::NOTIFICATION_BOOKMARK_BAR_VISIBILITY_PREF_CHANGED); | 314 DCHECK_EQ(chrome::NOTIFICATION_PREF_CHANGED, type); |
| 315 const std::string& pref_name = *Details<std::string>(details).ptr(); |
| 316 DCHECK_EQ(prefs::kShowBookmarkBar, pref_name); |
| 317 OnBookmarkBarVisibilityChanged(); |
| 318 } |
325 | 319 |
326 if (!browser_->profile()->IsSameProfile(Source<Profile>(source).ptr())) | 320 void GlobalMenuBar::OnBookmarkBarVisibilityChanged() { |
327 return; | |
328 | |
329 CommandIDMenuItemMap::iterator it = | 321 CommandIDMenuItemMap::iterator it = |
330 id_to_menu_item_.find(IDC_SHOW_BOOKMARK_BAR); | 322 id_to_menu_item_.find(IDC_SHOW_BOOKMARK_BAR); |
331 if (it != id_to_menu_item_.end()) { | 323 if (it != id_to_menu_item_.end()) { |
332 PrefService* prefs = browser_->profile()->GetPrefs(); | 324 PrefService* prefs = browser_->profile()->GetPrefs(); |
333 | |
334 block_activation_ = true; | 325 block_activation_ = true; |
335 gtk_check_menu_item_set_active( | 326 gtk_check_menu_item_set_active( |
336 GTK_CHECK_MENU_ITEM(it->second), | 327 GTK_CHECK_MENU_ITEM(it->second), |
337 prefs->GetBoolean(prefs::kShowBookmarkBar)); | 328 prefs->GetBoolean(prefs::kShowBookmarkBar)); |
338 block_activation_ = false; | 329 block_activation_ = false; |
339 } | 330 } |
340 } | 331 } |
341 | 332 |
342 void GlobalMenuBar::OnItemActivated(GtkWidget* sender) { | 333 void GlobalMenuBar::OnItemActivated(GtkWidget* sender) { |
343 if (block_activation_) | 334 if (block_activation_) |
344 return; | 335 return; |
345 | 336 |
346 int id = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(sender), "command-id")); | 337 int id = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(sender), "command-id")); |
347 browser_->ExecuteCommandIfEnabled(id); | 338 browser_->ExecuteCommandIfEnabled(id); |
348 } | 339 } |
OLD | NEW |