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 BookmarkNodeMenuModel::BookmarkNodeMenuModel( |
| 21 ui::SimpleMenuModel::Delegate* delegate, |
| 22 BookmarkModel* model, |
| 23 const BookmarkNode* node, |
| 24 PageNavigator* page_navigator) |
| 25 : SimpleMenuModel(delegate), |
| 26 model_(model), |
| 27 node_(node), |
| 28 page_navigator_(page_navigator) { |
| 29 DCHECK(page_navigator_); |
| 30 } |
| 31 |
| 32 BookmarkNodeMenuModel::~BookmarkNodeMenuModel() { |
| 33 Clear(); |
| 34 } |
| 35 |
| 36 void BookmarkNodeMenuModel::Clear() { |
| 37 SimpleMenuModel::Clear(); |
| 38 STLDeleteElements(&submenus_); |
| 39 } |
| 40 |
| 41 void BookmarkNodeMenuModel::MenuWillShow() { |
| 42 Clear(); |
| 43 PopulateMenu(); |
| 44 } |
| 45 |
| 46 void BookmarkNodeMenuModel::MenuClosed() { |
| 47 Clear(); |
| 48 } |
| 49 |
| 50 void BookmarkNodeMenuModel::ActivatedAt(int index) { |
| 51 NavigateToMenuItem(index, CURRENT_TAB); |
| 52 } |
| 53 |
| 54 void BookmarkNodeMenuModel::ActivatedAt(int index, int event_flags) { |
| 55 NavigateToMenuItem(index, browser::DispositionFromEventFlags(event_flags)); |
| 56 } |
| 57 |
| 58 void BookmarkNodeMenuModel::PopulateMenu() { |
| 59 DCHECK(submenus_.empty()); |
| 60 for (int i = 0; i < node_->child_count(); ++i) { |
| 61 const BookmarkNode* child = node_->GetChild(i); |
| 62 // Ironically the label will end up getting converted back to UTF8 later... |
| 63 const string16 label = |
| 64 UTF8ToUTF16(bookmark_utils::BuildMenuLabelFor(child)); |
| 65 if (child->is_folder()) { |
| 66 // Don't pass in the delegate, if any. Bookmark submenus don't need one. |
| 67 BookmarkNodeMenuModel* submenu = |
| 68 new BookmarkNodeMenuModel(NULL, model_, child, page_navigator_); |
| 69 // No command id. Nothing happens if you click on the submenu itself. |
| 70 AddSubMenu(0, label, submenu); |
| 71 submenus_.push_back(submenu); |
| 72 } else { |
| 73 // No command id. We override ActivatedAt below to handle activations. |
| 74 AddItem(0, label); |
| 75 const SkBitmap& node_icon = model_->GetFavicon(child); |
| 76 if (node_icon.width() > 0) |
| 77 SetIcon(GetItemCount() - 1, node_icon); |
| 78 // TODO(mdm): set up an observer to watch for icon load events and set |
| 79 // the icons in response. |
| 80 } |
| 81 } |
| 82 } |
| 83 |
| 84 void BookmarkNodeMenuModel::NavigateToMenuItem( |
| 85 int index, |
| 86 WindowOpenDisposition disposition) { |
| 87 const BookmarkNode* node = node_->GetChild(index); |
| 88 DCHECK(node); |
| 89 page_navigator_->OpenURL(OpenURLParams( |
| 90 node->url(), GURL(), disposition, |
| 91 content::PAGE_TRANSITION_AUTO_BOOKMARK, |
| 92 false)); // is_renderer_initiated |
| 93 } |
| 94 |
| 95 BookmarkSubMenuModel::BookmarkSubMenuModel( |
| 96 ui::SimpleMenuModel::Delegate* delegate, |
| 97 Browser* browser) |
| 98 : BookmarkNodeMenuModel(delegate, NULL, NULL, browser), |
| 99 browser_(browser), |
| 100 fixed_items_(0), |
| 101 menu_(NULL) { |
| 102 } |
| 103 |
| 104 BookmarkSubMenuModel::~BookmarkSubMenuModel() { |
| 105 if (model()) |
| 106 model()->RemoveObserver(this); |
| 107 } |
| 108 |
| 109 void BookmarkSubMenuModel::BookmarkModelChanged() { |
| 110 if (menu_) |
| 111 menu_->Cancel(); |
| 112 } |
| 113 |
| 114 void BookmarkSubMenuModel::BookmarkModelBeingDeleted( |
| 115 BookmarkModel* model) { |
| 116 set_model(NULL); |
| 117 // All our submenus will still have pointers to the model, but this call |
| 118 // should force the menu to close, which will cause them to be deleted. |
| 119 BookmarkModelChanged(); |
| 120 } |
| 121 |
| 122 void BookmarkSubMenuModel::MenuWillShow() { |
| 123 Clear(); |
| 124 AddCheckItemWithStringId(IDC_SHOW_BOOKMARK_BAR, IDS_SHOW_BOOKMARK_BAR); |
| 125 AddItemWithStringId(IDC_SHOW_BOOKMARK_MANAGER, IDS_BOOKMARK_MANAGER); |
| 126 AddItemWithStringId(IDC_IMPORT_SETTINGS, IDS_IMPORT_SETTINGS_MENU_LABEL); |
| 127 if (!model()) { |
| 128 set_model(browser_->profile()->GetBookmarkModel()); |
| 129 if (model()) |
| 130 model()->AddObserver(this); |
| 131 } |
| 132 if (!model() || |
| 133 (model()->bookmark_bar_node()->GetTotalNodeCount() == 0 && |
| 134 model()->other_node()->GetTotalNodeCount() == 0)) { |
| 135 fixed_items_ = GetItemCount(); |
| 136 return; |
| 137 } |
| 138 AddSeparator(); |
| 139 fixed_items_ = GetItemCount(); |
| 140 if (!node()) |
| 141 set_node(model()->bookmark_bar_node()); |
| 142 // PopulateMenu() won't clear the items we added above. |
| 143 PopulateMenu(); |
| 144 // TODO(mdm): add a separator and the "other bookmarks" node here. |
| 145 } |
| 146 |
| 147 void BookmarkSubMenuModel::ActivatedAt(int index) { |
| 148 // Because this is also overridden in BookmarkNodeMenuModel which doesn't know |
| 149 // we might be prepending items, we have to adjust the index for it. |
| 150 if (index >= fixed_items_) |
| 151 BookmarkNodeMenuModel::ActivatedAt(index - fixed_items_); |
| 152 else |
| 153 SimpleMenuModel::ActivatedAt(index); |
| 154 } |
| 155 |
| 156 void BookmarkSubMenuModel::ActivatedAt(int index, int event_flags) { |
| 157 // Because this is also overridden in BookmarkNodeMenuModel which doesn't know |
| 158 // we might be prepending items, we have to adjust the index for it. |
| 159 if (index >= fixed_items_) |
| 160 BookmarkNodeMenuModel::ActivatedAt(index - fixed_items_, event_flags); |
| 161 else |
| 162 SimpleMenuModel::ActivatedAt(index, event_flags); |
| 163 } |
| 164 |
| 165 bool BookmarkSubMenuModel::IsEnabledAt(int index) const { |
| 166 // We don't want the delegate interfering with bookmark items. |
| 167 return index >= fixed_items_ || SimpleMenuModel::IsEnabledAt(index); |
| 168 } |
| 169 |
| 170 bool BookmarkSubMenuModel::IsVisibleAt(int index) const { |
| 171 // We don't want the delegate interfering with bookmark items. |
| 172 return index >= fixed_items_ || SimpleMenuModel::IsVisibleAt(index); |
| 173 } |
OLD | NEW |