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" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 default_favicon_ = GtkThemeService::GetDefaultFavicon(true); | 61 default_favicon_ = GtkThemeService::GetDefaultFavicon(true); |
62 default_folder_ = GtkThemeService::GetFolderIcon(true); | 62 default_folder_ = GtkThemeService::GetFolderIcon(true); |
63 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED, | 63 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED, |
64 Source<Profile>(profile_)); | 64 Source<Profile>(profile_)); |
65 } | 65 } |
66 | 66 |
67 GlobalBookmarkMenu::~GlobalBookmarkMenu() { | 67 GlobalBookmarkMenu::~GlobalBookmarkMenu() { |
68 profile_->GetBookmarkModel()->RemoveObserver(this); | 68 profile_->GetBookmarkModel()->RemoveObserver(this); |
69 } | 69 } |
70 | 70 |
71 void GlobalBookmarkMenu::Init(GtkWidget* bookmark_menu) { | 71 void GlobalBookmarkMenu::Init(GtkWidget* bookmark_menu, |
72 bookmark_menu_ = bookmark_menu; | 72 GtkWidget* bookmark_menu_item) { |
| 73 bookmark_menu_.Own(bookmark_menu); |
73 | 74 |
74 EnsureMenuItemFunctions(); | 75 EnsureMenuItemFunctions(); |
75 if (gtk_menu_item_set_label_sym) { | 76 if (gtk_menu_item_set_label_sym) { |
76 BookmarkModel* model = profile_->GetBookmarkModel(); | 77 BookmarkModel* model = profile_->GetBookmarkModel(); |
77 model->AddObserver(this); | 78 model->AddObserver(this); |
78 if (model->IsLoaded()) | 79 if (model->IsLoaded()) |
79 Loaded(model); | 80 Loaded(model); |
80 } | 81 } |
81 } | 82 } |
82 | 83 |
83 void GlobalBookmarkMenu::RebuildMenuInFuture() { | 84 void GlobalBookmarkMenu::RebuildMenuInFuture() { |
84 method_factory_.RevokeAll(); | 85 method_factory_.RevokeAll(); |
85 MessageLoop::current()->PostTask( | 86 MessageLoop::current()->PostTask( |
86 FROM_HERE, | 87 FROM_HERE, |
87 method_factory_.NewRunnableMethod(&GlobalBookmarkMenu::RebuildMenu)); | 88 method_factory_.NewRunnableMethod(&GlobalBookmarkMenu::RebuildMenu)); |
88 } | 89 } |
89 | 90 |
90 void GlobalBookmarkMenu::RebuildMenu() { | 91 void GlobalBookmarkMenu::RebuildMenu() { |
91 | |
92 | |
93 BookmarkModel* model = profile_->GetBookmarkModel(); | 92 BookmarkModel* model = profile_->GetBookmarkModel(); |
94 DCHECK(model); | 93 DCHECK(model); |
95 DCHECK(model->IsLoaded()); | 94 DCHECK(model->IsLoaded()); |
96 | 95 |
97 ClearBookmarkMenu(); | 96 ClearBookmarkMenu(); |
98 | 97 |
99 const BookmarkNode* bar_node = model->GetBookmarkBarNode(); | 98 const BookmarkNode* bar_node = model->GetBookmarkBarNode(); |
100 if (bar_node->child_count()) { | 99 if (bar_node->child_count()) { |
101 AddBookmarkMenuItem(bookmark_menu_, gtk_separator_menu_item_new()); | 100 AddBookmarkMenuItem(bookmark_menu_.get(), gtk_separator_menu_item_new()); |
102 AddNodeToMenu(bar_node, bookmark_menu_); | 101 AddNodeToMenu(bar_node, bookmark_menu_.get()); |
103 } | 102 } |
104 | 103 |
105 // Only display the other bookmarks folder in the menu if it has items in it. | 104 // Only display the other bookmarks folder in the menu if it has items in it. |
106 const BookmarkNode* other_node = model->other_node(); | 105 const BookmarkNode* other_node = model->other_node(); |
107 if (other_node->child_count()) { | 106 if (other_node->child_count()) { |
108 GtkWidget* submenu = gtk_menu_new(); | 107 GtkWidget* submenu = gtk_menu_new(); |
109 AddNodeToMenu(other_node, submenu); | 108 AddNodeToMenu(other_node, submenu); |
110 | 109 |
111 AddBookmarkMenuItem(bookmark_menu_, gtk_separator_menu_item_new()); | 110 AddBookmarkMenuItem(bookmark_menu_.get(), gtk_separator_menu_item_new()); |
112 | 111 |
113 GtkWidget* menu_item = gtk_image_menu_item_new_with_label( | 112 GtkWidget* menu_item = gtk_image_menu_item_new_with_label( |
114 l10n_util::GetStringUTF8(IDS_BOOMARK_BAR_OTHER_FOLDER_NAME).c_str()); | 113 l10n_util::GetStringUTF8(IDS_BOOMARK_BAR_OTHER_FOLDER_NAME).c_str()); |
115 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), submenu); | 114 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), submenu); |
116 gtk_image_menu_item_set_image( | 115 gtk_image_menu_item_set_image( |
117 GTK_IMAGE_MENU_ITEM(menu_item), | 116 GTK_IMAGE_MENU_ITEM(menu_item), |
118 gtk_image_new_from_pixbuf(default_folder_)); | 117 gtk_image_new_from_pixbuf(default_folder_)); |
119 | 118 |
120 AddBookmarkMenuItem(bookmark_menu_, menu_item); | 119 AddBookmarkMenuItem(bookmark_menu_.get(), menu_item); |
121 } | 120 } |
122 } | 121 } |
123 | 122 |
124 void GlobalBookmarkMenu::AddBookmarkMenuItem(GtkWidget* menu, | 123 void GlobalBookmarkMenu::AddBookmarkMenuItem(GtkWidget* menu, |
125 GtkWidget* menu_item) { | 124 GtkWidget* menu_item) { |
126 g_object_set_data(G_OBJECT(menu_item), "type-tag", | 125 g_object_set_data(G_OBJECT(menu_item), "type-tag", |
127 GINT_TO_POINTER(GlobalMenuBar::TAG_BOOKMARK_CLEARABLE)); | 126 GINT_TO_POINTER(GlobalMenuBar::TAG_BOOKMARK_CLEARABLE)); |
128 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item); | 127 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item); |
129 gtk_widget_show(menu_item); | 128 gtk_widget_show(menu_item); |
130 } | 129 } |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 std::map<const BookmarkNode*, GtkWidget*>::iterator it = | 197 std::map<const BookmarkNode*, GtkWidget*>::iterator it = |
199 bookmark_nodes_.find(node); | 198 bookmark_nodes_.find(node); |
200 if (it == bookmark_nodes_.end()) | 199 if (it == bookmark_nodes_.end()) |
201 return NULL; | 200 return NULL; |
202 return it->second; | 201 return it->second; |
203 } | 202 } |
204 | 203 |
205 void GlobalBookmarkMenu::ClearBookmarkMenu() { | 204 void GlobalBookmarkMenu::ClearBookmarkMenu() { |
206 bookmark_nodes_.clear(); | 205 bookmark_nodes_.clear(); |
207 | 206 |
208 gtk_container_foreach(GTK_CONTAINER(bookmark_menu_), | 207 gtk_container_foreach(GTK_CONTAINER(bookmark_menu_.get()), |
209 &ClearBookmarkItemCallback, | 208 &ClearBookmarkItemCallback, |
210 NULL); | 209 NULL); |
211 } | 210 } |
212 | 211 |
213 // static | 212 // static |
214 void GlobalBookmarkMenu::ClearBookmarkItemCallback(GtkWidget* menu_item, | 213 void GlobalBookmarkMenu::ClearBookmarkItemCallback(GtkWidget* menu_item, |
215 void* unused) { | 214 void* unused) { |
216 int tag = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(menu_item), "type-tag")); | 215 int tag = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(menu_item), "type-tag")); |
217 if (tag == GlobalMenuBar::TAG_BOOKMARK_CLEARABLE) | 216 if (tag == GlobalMenuBar::TAG_BOOKMARK_CLEARABLE) |
218 gtk_widget_destroy(menu_item); | 217 gtk_widget_destroy(menu_item); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 void GlobalBookmarkMenu::OnBookmarkItemActivated(GtkWidget* menu_item) { | 286 void GlobalBookmarkMenu::OnBookmarkItemActivated(GtkWidget* menu_item) { |
288 // The actual mouse event that generated this activated event was in a | 287 // The actual mouse event that generated this activated event was in a |
289 // different process. Go with something default. | 288 // different process. Go with something default. |
290 const BookmarkNode* node = static_cast<const BookmarkNode*>( | 289 const BookmarkNode* node = static_cast<const BookmarkNode*>( |
291 g_object_get_data(G_OBJECT(menu_item), "bookmark-node")); | 290 g_object_get_data(G_OBJECT(menu_item), "bookmark-node")); |
292 | 291 |
293 browser_->OpenURL(node->GetURL(), GURL(), NEW_FOREGROUND_TAB, | 292 browser_->OpenURL(node->GetURL(), GURL(), NEW_FOREGROUND_TAB, |
294 PageTransition::AUTO_BOOKMARK); | 293 PageTransition::AUTO_BOOKMARK); |
295 } | 294 } |
296 | 295 |
OLD | NEW |