1 // Copyright (c) 2012 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/views/bookmarks/bookmark_menu_controller_views.h" 6 7 #include "base/prefs/pref_service.h" 8 #include "base/stl_util.h" 9 #include "base/strings/utf_string_conversions.h" 10 #include "chrome/browser/bookmarks/bookmark_model.h" 11 #include "chrome/browser/bookmarks/bookmark_model_factory.h" 12 #include "chrome/browser/bookmarks/bookmark_node_data.h" 13 #include "chrome/browser/bookmarks/bookmark_utils.h" 14 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h" 16 #include "chrome/browser/ui/views/bookmarks/bookmark_menu_delegate.h" 17 #include "chrome/common/pref_names.h" 18 #include "content/public/browser/page_navigator.h" 19 #include "content/public/browser/user_metrics.h" 20 #include "grit/generated_resources.h" 21 #include "grit/theme_resources.h" 22 #include "grit/ui_resources.h" 23 #include "ui/base/dragdrop/os_exchange_data.h" 24 #include "ui/base/resource/resource_bundle.h" 25 #include "ui/views/controls/button/menu_button.h" 26 #include "ui/views/controls/menu/menu_runner.h" 27 #include "ui/views/widget/widget.h" 28 29 using content::PageNavigator; 30 using content::UserMetricsAction; 31 using views::MenuItemView; 32 33 BookmarkMenuController::BookmarkMenuController(Browser* browser, 34 PageNavigator* page_navigator, 35 views::Widget* parent, 36 const BookmarkNode* node, 37 int start_child_index) 38 : menu_delegate_( 39 new BookmarkMenuDelegate(browser, page_navigator, parent, 1)), 40 node_(node), 41 observer_(NULL), 42 for_drop_(false), 43 bookmark_bar_(NULL) { 44 menu_delegate_->Init(this, NULL, node, start_child_index, 45 BookmarkMenuDelegate::HIDE_PERMANENT_FOLDERS, 46 bookmark_utils::LAUNCH_BAR_SUBFOLDER); 47 menu_runner_.reset(new views::MenuRunner(menu_delegate_->menu())); 48 } 49 50 void BookmarkMenuController::RunMenuAt(BookmarkBarView* bookmark_bar, 51 bool for_drop) { 52 bookmark_bar_ = bookmark_bar; 53 views::MenuButton* menu_button = bookmark_bar_->GetMenuButtonForNode(node_); 54 DCHECK(menu_button); 55 MenuItemView::AnchorPosition anchor; 56 bookmark_bar_->GetAnchorPositionForButton(menu_button, &anchor); 57 gfx::Point screen_loc; 58 views::View::ConvertPointToScreen(menu_button, &screen_loc); 59 // Subtract 1 from the height to make the popup flush with the button border. 60 gfx::Rect bounds(screen_loc.x(), screen_loc.y(), menu_button->width(), 61 menu_button->height() - 1); 62 for_drop_ = for_drop; 63 BookmarkModelFactory::GetForProfile( 64 menu_delegate_->profile())->AddObserver(this); 65 // We only delete ourself after the menu completes, so we can safely ignore 66 // the return value. 67 ignore_result(menu_runner_->RunMenuAt(menu_delegate_->parent(), menu_button, 68 bounds, anchor, ui::MENU_SOURCE_NONE, 69 for_drop ? views::MenuRunner::FOR_DROP : 0)); 70 if (!for_drop) 71 delete this; 72 } 73 74 void BookmarkMenuController::Cancel() { 75 menu_delegate_->menu()->Cancel(); 76 } 77 78 MenuItemView* BookmarkMenuController::menu() const { 79 return menu_delegate_->menu(); 80 } 81 82 MenuItemView* BookmarkMenuController::context_menu() const { 83 return menu_delegate_->context_menu(); 84 } 85 86 void BookmarkMenuController::SetPageNavigator(PageNavigator* navigator) { 87 menu_delegate_->SetPageNavigator(navigator); 88 } 89 90 string16 BookmarkMenuController::GetTooltipText(int id, 91 const gfx::Point& p) const { 92 return menu_delegate_->GetTooltipText(id, p); 93 } 94 95 bool BookmarkMenuController::IsTriggerableEvent(views::MenuItemView* menu, 96 const ui::Event& e) { 97 return menu_delegate_->IsTriggerableEvent(menu, e); 98 } 99 100 void BookmarkMenuController::ExecuteCommand(int id, int mouse_event_flags) { 101 menu_delegate_->ExecuteCommand(id, mouse_event_flags); 102 } 103 104 bool BookmarkMenuController::ShouldExecuteCommandWithoutClosingMenu( 105 int id, const ui::Event& e) { 106 return menu_delegate_->ShouldExecuteCommandWithoutClosingMenu(id, e); 107 } 108 109 bool BookmarkMenuController::GetDropFormats( 110 MenuItemView* menu, 111 int* formats, 112 std::set<ui::OSExchangeData::CustomFormat>* custom_formats) { 113 return menu_delegate_->GetDropFormats(menu, formats, custom_formats); 114 } 115 116 bool BookmarkMenuController::AreDropTypesRequired(MenuItemView* menu) { 117 return menu_delegate_->AreDropTypesRequired(menu); 118 } 119 120 bool BookmarkMenuController::CanDrop(MenuItemView* menu, 121 const ui::OSExchangeData& data) { 122 return menu_delegate_->CanDrop(menu, data); 123 } 124 125 int BookmarkMenuController::GetDropOperation( 126 MenuItemView* item, 127 const ui::DropTargetEvent& event, 128 DropPosition* position) { 129 return menu_delegate_->GetDropOperation(item, event, position); 130 } 131 132 int BookmarkMenuController::OnPerformDrop(MenuItemView* menu, 133 DropPosition position, 134 const ui::DropTargetEvent& event) { 135 int result = menu_delegate_->OnPerformDrop(menu, position, event); 136 if (for_drop_) 137 delete this; 138 return result; 139 } 140 141 bool BookmarkMenuController::ShowContextMenu(MenuItemView* source, 142 int id, 143 const gfx::Point& p, 144 ui::MenuSourceType source_type) { 145 return menu_delegate_->ShowContextMenu(source, id, p, source_type); 146 } 147 148 void BookmarkMenuController::DropMenuClosed(MenuItemView* menu) { 149 delete this; 150 } 151 152 bool BookmarkMenuController::CanDrag(MenuItemView* menu) { 153 return menu_delegate_->CanDrag(menu); 154 } 155 156 void BookmarkMenuController::WriteDragData(MenuItemView* sender, 157 ui::OSExchangeData* data) { 158 return menu_delegate_->WriteDragData(sender, data); 159 } 160 161 int BookmarkMenuController::GetDragOperations(MenuItemView* sender) { 162 return menu_delegate_->GetDragOperations(sender); 163 } 164 165 views::MenuItemView* BookmarkMenuController::GetSiblingMenu( 166 views::MenuItemView* menu, 167 const gfx::Point& screen_point, 168 views::MenuItemView::AnchorPosition* anchor, 169 bool* has_mnemonics, 170 views::MenuButton** button) { 171 if (!bookmark_bar_ || for_drop_) 172 return NULL; 173 gfx::Point bookmark_bar_loc(screen_point); 174 views::View::ConvertPointToTarget(NULL, bookmark_bar_, &bookmark_bar_loc); 175 int start_index; 176 const BookmarkNode* node = bookmark_bar_->GetNodeForButtonAtModelIndex( 177 bookmark_bar_loc, &start_index); 178 if (!node || !node->is_folder()) 179 return NULL; 180 181 menu_delegate_->SetActiveMenu(node, start_index); 182 *button = bookmark_bar_->GetMenuButtonForNode(node); 183 bookmark_bar_->GetAnchorPositionForButton(*button, anchor); 184 *has_mnemonics = false; 185 return this->menu(); 186 } 187 188 int BookmarkMenuController::GetMaxWidthForMenu(MenuItemView* view) { 189 return menu_delegate_->GetMaxWidthForMenu(view); 190 } 191 192 void BookmarkMenuController::BookmarkModelChanged() { 193 if (!menu_delegate_->is_mutating_model()) 194 menu()->Cancel(); 195 } 196 197 BookmarkMenuController::~BookmarkMenuController() { 198 BookmarkModelFactory::GetForProfile( 199 menu_delegate_->profile())->RemoveObserver(this); 200 if (observer_) 201 observer_->BookmarkMenuDeleted(this); 202 } 203