OLD | NEW |
| (Empty) |
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/bookmark_sub_menu_model_gtk.h" | |
6 | |
7 #include "base/stl_util.h" | |
8 #include "base/string16.h" | |
9 #include "base/utf_string_conversions.h" | |
10 #include "chrome/app/chrome_command_ids.h" | |
11 #include "chrome/browser/bookmarks/bookmark_model.h" | |
12 #include "chrome/browser/event_disposition.h" | |
13 #include "chrome/browser/profiles/profile.h" | |
14 #include "chrome/browser/ui/browser.h" | |
15 #include "chrome/browser/ui/gtk/bookmarks/bookmark_utils_gtk.h" | |
16 #include "chrome/browser/ui/gtk/menu_gtk.h" | |
17 #include "grit/generated_resources.h" | |
18 #include "ui/base/l10n/l10n_util.h" | |
19 | |
20 // Per chrome/app/chrome_command_ids.h, values < 4000 are for "dynamic menu | |
21 // items". We only use one command id for all the bookmarks, because we handle | |
22 // bookmark item activations directly. So we pick a suitably large random value | |
23 // and use that to avoid accidental conflicts with other dynamic items. | |
24 static const int kBookmarkItemCommandId = 1759; | |
25 | |
26 BookmarkNodeMenuModel::BookmarkNodeMenuModel( | |
27 ui::SimpleMenuModel::Delegate* delegate, | |
28 BookmarkModel* model, | |
29 const BookmarkNode* node, | |
30 PageNavigator* page_navigator) | |
31 : SimpleMenuModel(delegate), | |
32 model_(model), | |
33 node_(node), | |
34 page_navigator_(page_navigator) { | |
35 DCHECK(page_navigator_); | |
36 } | |
37 | |
38 BookmarkNodeMenuModel::~BookmarkNodeMenuModel() { | |
39 Clear(); | |
40 } | |
41 | |
42 void BookmarkNodeMenuModel::Clear() { | |
43 SimpleMenuModel::Clear(); | |
44 STLDeleteElements(&submenus_); | |
45 } | |
46 | |
47 void BookmarkNodeMenuModel::MenuWillShow() { | |
48 Clear(); | |
49 PopulateMenu(); | |
50 } | |
51 | |
52 void BookmarkNodeMenuModel::MenuClosed() { | |
53 Clear(); | |
54 } | |
55 | |
56 void BookmarkNodeMenuModel::ActivatedAt(int index) { | |
57 NavigateToMenuItem(index, CURRENT_TAB); | |
58 } | |
59 | |
60 void BookmarkNodeMenuModel::ActivatedAt(int index, int event_flags) { | |
61 NavigateToMenuItem(index, browser::DispositionFromEventFlags(event_flags)); | |
62 } | |
63 | |
64 void BookmarkNodeMenuModel::PopulateMenu() { | |
65 DCHECK(submenus_.empty()); | |
66 for (int i = 0; i < node_->child_count(); ++i) { | |
67 const BookmarkNode* child = node_->GetChild(i); | |
68 if (child->is_folder()) { | |
69 AddSubMenuForNode(child); | |
70 } else { | |
71 // Ironically the label will end up getting converted back to UTF8 later. | |
72 const string16 label = | |
73 UTF8ToUTF16(bookmark_utils::BuildMenuLabelFor(child)); | |
74 // No command id. We override ActivatedAt below to handle activations. | |
75 AddItem(kBookmarkItemCommandId, label); | |
76 const SkBitmap& node_icon = model_->GetFavicon(child); | |
77 if (node_icon.width() > 0) | |
78 SetIcon(GetItemCount() - 1, node_icon); | |
79 // TODO(mdm): set up an observer to watch for icon load events and set | |
80 // the icons in response. | |
81 } | |
82 } | |
83 } | |
84 | |
85 void BookmarkNodeMenuModel::AddSubMenuForNode(const BookmarkNode* node) { | |
86 DCHECK(node->is_folder()); | |
87 // Ironically the label will end up getting converted back to UTF8 later. | |
88 const string16 label = UTF8ToUTF16(bookmark_utils::BuildMenuLabelFor(node)); | |
89 // Don't pass in the delegate, if any. Bookmark submenus don't need one. | |
90 BookmarkNodeMenuModel* submenu = | |
91 new BookmarkNodeMenuModel(NULL, model_, node, page_navigator_); | |
92 // No command id. Nothing happens if you click on the submenu itself. | |
93 AddSubMenu(kBookmarkItemCommandId, label, submenu); | |
94 submenus_.push_back(submenu); | |
95 } | |
96 | |
97 void BookmarkNodeMenuModel::NavigateToMenuItem( | |
98 int index, | |
99 WindowOpenDisposition disposition) { | |
100 const BookmarkNode* node = node_->GetChild(index); | |
101 DCHECK(node); | |
102 page_navigator_->OpenURL(OpenURLParams( | |
103 node->url(), GURL(), disposition, | |
104 content::PAGE_TRANSITION_AUTO_BOOKMARK, | |
105 false)); // is_renderer_initiated | |
106 } | |
107 | |
108 BookmarkSubMenuModel::BookmarkSubMenuModel( | |
109 ui::SimpleMenuModel::Delegate* delegate, | |
110 Browser* browser) | |
111 : BookmarkNodeMenuModel(delegate, NULL, NULL, browser), | |
112 browser_(browser), | |
113 fixed_items_(0), | |
114 bookmark_end_(0), | |
115 menu_(NULL) { | |
116 } | |
117 | |
118 BookmarkSubMenuModel::~BookmarkSubMenuModel() { | |
119 if (model()) | |
120 model()->RemoveObserver(this); | |
121 } | |
122 | |
123 void BookmarkSubMenuModel::BookmarkModelChanged() { | |
124 if (menu_) | |
125 menu_->Cancel(); | |
126 } | |
127 | |
128 void BookmarkSubMenuModel::BookmarkModelBeingDeleted( | |
129 BookmarkModel* model) { | |
130 set_model(NULL); | |
131 // All our submenus will still have pointers to the model, but this call | |
132 // should force the menu to close, which will cause them to be deleted. | |
133 BookmarkModelChanged(); | |
134 } | |
135 | |
136 void BookmarkSubMenuModel::MenuWillShow() { | |
137 Clear(); | |
138 AddCheckItemWithStringId(IDC_SHOW_BOOKMARK_BAR, IDS_SHOW_BOOKMARK_BAR); | |
139 AddItemWithStringId(IDC_SHOW_BOOKMARK_MANAGER, IDS_BOOKMARK_MANAGER); | |
140 AddItemWithStringId(IDC_IMPORT_SETTINGS, IDS_IMPORT_SETTINGS_MENU_LABEL); | |
141 fixed_items_ = bookmark_end_ = GetItemCount(); | |
142 if (!model()) { | |
143 set_model(browser_->profile()->GetBookmarkModel()); | |
144 if (!model()) | |
145 return; | |
146 model()->AddObserver(this); | |
147 } | |
148 // The node count includes the node itself, so 1 means empty. | |
149 if (model()->bookmark_bar_node()->GetTotalNodeCount() > 1) { | |
150 AddSeparator(); | |
151 fixed_items_ = GetItemCount(); | |
152 if (!node()) | |
153 set_node(model()->bookmark_bar_node()); | |
154 // PopulateMenu() won't clear the items we added above. | |
155 PopulateMenu(); | |
156 } | |
157 bookmark_end_ = GetItemCount(); | |
158 if (model()->other_node()->GetTotalNodeCount() > 1) { | |
159 AddSeparator(); | |
160 AddSubMenuForNode(model()->other_node()); | |
161 } | |
162 } | |
163 | |
164 void BookmarkSubMenuModel::ActivatedAt(int index) { | |
165 // Because this is also overridden in BookmarkNodeMenuModel which doesn't know | |
166 // we might be prepending items, we have to adjust the index for it. | |
167 if (index >= fixed_items_ && index < bookmark_end_) | |
168 BookmarkNodeMenuModel::ActivatedAt(index - fixed_items_); | |
169 else | |
170 SimpleMenuModel::ActivatedAt(index); | |
171 } | |
172 | |
173 void BookmarkSubMenuModel::ActivatedAt(int index, int event_flags) { | |
174 // Because this is also overridden in BookmarkNodeMenuModel which doesn't know | |
175 // we might be prepending items, we have to adjust the index for it. | |
176 if (index >= fixed_items_ && index < bookmark_end_) | |
177 BookmarkNodeMenuModel::ActivatedAt(index - fixed_items_, event_flags); | |
178 else | |
179 SimpleMenuModel::ActivatedAt(index, event_flags); | |
180 } | |
181 | |
182 bool BookmarkSubMenuModel::IsEnabledAt(int index) const { | |
183 // We don't want the delegate interfering with bookmark items. | |
184 return index >= fixed_items_ || SimpleMenuModel::IsEnabledAt(index); | |
185 } | |
186 | |
187 bool BookmarkSubMenuModel::IsVisibleAt(int index) const { | |
188 // We don't want the delegate interfering with bookmark items. | |
189 return index >= fixed_items_ || SimpleMenuModel::IsVisibleAt(index); | |
190 } | |
191 | |
192 // static | |
193 bool BookmarkSubMenuModel::IsBookmarkItemCommandId(int command_id) { | |
194 return command_id == kBookmarkItemCommandId; | |
195 } | |
OLD | NEW |