| 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_bookmark_menu.h" | 5 #include "chrome/browser/ui/gtk/global_bookmark_menu.h" |
| 6 | 6 |
| 7 #include <dlfcn.h> | 7 #include <dlfcn.h> |
| 8 #include <gtk/gtk.h> | 8 #include <gtk/gtk.h> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/bookmarks/bookmark_model.h" | 12 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/themes/theme_service_factory.h" | 14 #include "chrome/browser/themes/theme_service_factory.h" |
| 15 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
| 16 #include "chrome/browser/ui/gtk/bookmarks/bookmark_utils_gtk.h" | 16 #include "chrome/browser/ui/gtk/bookmarks/bookmark_utils_gtk.h" |
| 17 #include "chrome/browser/ui/gtk/global_bookmark_menu.h" | 17 #include "chrome/browser/ui/gtk/global_bookmark_menu.h" |
| 18 #include "chrome/browser/ui/gtk/global_menu_bar.h" | 18 #include "chrome/browser/ui/gtk/global_menu_bar.h" |
| 19 #include "chrome/browser/ui/gtk/gtk_theme_service.h" | 19 #include "chrome/browser/ui/gtk/gtk_theme_service.h" |
| 20 #include "chrome/browser/ui/gtk/gtk_util.h" | 20 #include "chrome/browser/ui/gtk/gtk_util.h" |
| 21 #include "chrome/common/chrome_notification_types.h" | 21 #include "chrome/common/chrome_notification_types.h" |
| 22 #include "content/common/notification_service.h" | 22 #include "content/common/notification_service.h" |
| 23 #include "grit/generated_resources.h" | 23 #include "grit/generated_resources.h" |
| 24 #include "ui/base/l10n/l10n_util.h" | 24 #include "ui/base/l10n/l10n_util.h" |
| 25 #include "ui/gfx/gtk_util.h" | 25 #include "ui/gfx/gtk_util.h" |
| 26 | 26 |
| 27 namespace { | |
| 28 | |
| 29 // We need to know whether we're using a newer GTK at run time because we need | |
| 30 // to prevent. | |
| 31 // | |
| 32 // TODO(erg): Once we've dropped Hardy support, remove this hack. | |
| 33 typedef void (*gtk_menu_item_set_label_func)(GtkMenuItem*, const gchar*); | |
| 34 gtk_menu_item_set_label_func gtk_menu_item_set_label_sym = | |
| 35 #if GTK_CHECK_VERSION(2, 16, 1) | |
| 36 gtk_menu_item_set_label; | |
| 37 #else | |
| 38 NULL; | |
| 39 #endif | |
| 40 | |
| 41 void EnsureMenuItemFunctions() { | |
| 42 #if !GTK_CHECK_VERSION(2, 16, 1) | |
| 43 static bool methods_looked_up = false; | |
| 44 if (!methods_looked_up) { | |
| 45 methods_looked_up = true; | |
| 46 gtk_menu_item_set_label_sym = | |
| 47 reinterpret_cast<gtk_menu_item_set_label_func>( | |
| 48 dlsym(NULL, "gtk_menu_item_set_label")); | |
| 49 } | |
| 50 #endif | |
| 51 } | |
| 52 | |
| 53 } // namespace | |
| 54 | |
| 55 GlobalBookmarkMenu::GlobalBookmarkMenu(Browser* browser) | 27 GlobalBookmarkMenu::GlobalBookmarkMenu(Browser* browser) |
| 56 : browser_(browser), | 28 : browser_(browser), |
| 57 profile_(browser->profile()), | 29 profile_(browser->profile()), |
| 58 default_favicon_(NULL), | 30 default_favicon_(NULL), |
| 59 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | 31 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
| 60 DCHECK(profile_); | 32 DCHECK(profile_); |
| 61 | 33 |
| 62 default_favicon_ = GtkThemeService::GetDefaultFavicon(true); | 34 default_favicon_ = GtkThemeService::GetDefaultFavicon(true); |
| 63 default_folder_ = GtkThemeService::GetFolderIcon(true); | 35 default_folder_ = GtkThemeService::GetFolderIcon(true); |
| 64 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, | 36 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, |
| 65 Source<ThemeService>( | 37 Source<ThemeService>( |
| 66 ThemeServiceFactory::GetForProfile(profile_))); | 38 ThemeServiceFactory::GetForProfile(profile_))); |
| 67 } | 39 } |
| 68 | 40 |
| 69 GlobalBookmarkMenu::~GlobalBookmarkMenu() { | 41 GlobalBookmarkMenu::~GlobalBookmarkMenu() { |
| 70 profile_->GetBookmarkModel()->RemoveObserver(this); | 42 profile_->GetBookmarkModel()->RemoveObserver(this); |
| 71 } | 43 } |
| 72 | 44 |
| 73 void GlobalBookmarkMenu::Init(GtkWidget* bookmark_menu, | 45 void GlobalBookmarkMenu::Init(GtkWidget* bookmark_menu, |
| 74 GtkWidget* bookmark_menu_item) { | 46 GtkWidget* bookmark_menu_item) { |
| 75 bookmark_menu_.Own(bookmark_menu); | 47 bookmark_menu_.Own(bookmark_menu); |
| 76 | 48 |
| 77 EnsureMenuItemFunctions(); | 49 BookmarkModel* model = profile_->GetBookmarkModel(); |
| 78 if (gtk_menu_item_set_label_sym) { | 50 model->AddObserver(this); |
| 79 BookmarkModel* model = profile_->GetBookmarkModel(); | 51 if (model->IsLoaded()) |
| 80 model->AddObserver(this); | 52 Loaded(model, false); |
| 81 if (model->IsLoaded()) | |
| 82 Loaded(model, false); | |
| 83 } | |
| 84 } | 53 } |
| 85 | 54 |
| 86 void GlobalBookmarkMenu::RebuildMenuInFuture() { | 55 void GlobalBookmarkMenu::RebuildMenuInFuture() { |
| 87 method_factory_.RevokeAll(); | 56 method_factory_.RevokeAll(); |
| 88 MessageLoop::current()->PostTask( | 57 MessageLoop::current()->PostTask( |
| 89 FROM_HERE, | 58 FROM_HERE, |
| 90 method_factory_.NewRunnableMethod(&GlobalBookmarkMenu::RebuildMenu)); | 59 method_factory_.NewRunnableMethod(&GlobalBookmarkMenu::RebuildMenu)); |
| 91 } | 60 } |
| 92 | 61 |
| 93 void GlobalBookmarkMenu::RebuildMenu() { | 62 void GlobalBookmarkMenu::RebuildMenu() { |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 AddBookmarkMenuItem(menu, item); | 129 AddBookmarkMenuItem(menu, item); |
| 161 } | 130 } |
| 162 } | 131 } |
| 163 } | 132 } |
| 164 | 133 |
| 165 void GlobalBookmarkMenu::ConfigureMenuItem(const BookmarkNode* node, | 134 void GlobalBookmarkMenu::ConfigureMenuItem(const BookmarkNode* node, |
| 166 GtkWidget* menu_item) { | 135 GtkWidget* menu_item) { |
| 167 CHECK(node); | 136 CHECK(node); |
| 168 CHECK(menu_item); | 137 CHECK(menu_item); |
| 169 | 138 |
| 170 // This check is only to make things compile on Hardy; this code won't | 139 gtk_menu_item_set_label(GTK_MENU_ITEM(menu_item), |
| 171 // display any visible widgets in older systems that don't have a global menu | 140 bookmark_utils::BuildMenuLabelFor(node).c_str()); |
| 172 // bar. | |
| 173 if (gtk_menu_item_set_label_sym) { | |
| 174 gtk_menu_item_set_label_sym( | |
| 175 GTK_MENU_ITEM(menu_item), | |
| 176 bookmark_utils::BuildMenuLabelFor(node).c_str()); | |
| 177 } | |
| 178 | 141 |
| 179 if (node->is_url()) { | 142 if (node->is_url()) { |
| 180 gtk_widget_set_tooltip_markup( | 143 gtk_widget_set_tooltip_markup( |
| 181 menu_item, | 144 menu_item, |
| 182 bookmark_utils::BuildTooltipFor(node).c_str()); | 145 bookmark_utils::BuildTooltipFor(node).c_str()); |
| 183 } | 146 } |
| 184 | 147 |
| 185 const SkBitmap& bitmap = profile_->GetBookmarkModel()->GetFavicon(node); | 148 const SkBitmap& bitmap = profile_->GetBookmarkModel()->GetFavicon(node); |
| 186 if (!bitmap.isNull()) { | 149 if (!bitmap.isNull()) { |
| 187 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(&bitmap); | 150 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(&bitmap); |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 void GlobalBookmarkMenu::OnBookmarkItemActivated(GtkWidget* menu_item) { | 258 void GlobalBookmarkMenu::OnBookmarkItemActivated(GtkWidget* menu_item) { |
| 296 // The actual mouse event that generated this activated event was in a | 259 // The actual mouse event that generated this activated event was in a |
| 297 // different process. Go with something default. | 260 // different process. Go with something default. |
| 298 const BookmarkNode* node = static_cast<const BookmarkNode*>( | 261 const BookmarkNode* node = static_cast<const BookmarkNode*>( |
| 299 g_object_get_data(G_OBJECT(menu_item), "bookmark-node")); | 262 g_object_get_data(G_OBJECT(menu_item), "bookmark-node")); |
| 300 | 263 |
| 301 browser_->OpenURL(node->url(), GURL(), NEW_FOREGROUND_TAB, | 264 browser_->OpenURL(node->url(), GURL(), NEW_FOREGROUND_TAB, |
| 302 PageTransition::AUTO_BOOKMARK); | 265 PageTransition::AUTO_BOOKMARK); |
| 303 } | 266 } |
| 304 | 267 |
| OLD | NEW |