bookmark_context_menu.cc revision 6e8cce623b6e4fe0c9e4af605d675dd9d0338c38
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_context_menu.h" 6 7#include "base/i18n/rtl.h" 8#include "base/strings/utf_string_conversions.h" 9#include "chrome/app/chrome_command_ids.h" 10#include "chrome/browser/chrome_notification_types.h" 11#include "components/bookmarks/browser/bookmark_model.h" 12#include "content/public/browser/notification_service.h" 13#include "ui/views/controls/menu/menu_item_view.h" 14#include "ui/views/controls/menu/menu_model_adapter.h" 15#include "ui/views/controls/menu/menu_runner.h" 16#include "ui/views/widget/widget.h" 17 18using content::PageNavigator; 19 20namespace { 21 22// Returns true if |command_id| corresponds to a command that causes one or more 23// bookmarks to be removed. 24bool IsRemoveBookmarksCommand(int command_id) { 25 return command_id == IDC_CUT || command_id == IDC_BOOKMARK_BAR_REMOVE; 26} 27 28} // namespace 29 30//////////////////////////////////////////////////////////////////////////////// 31// BookmarkContextMenu, public: 32 33BookmarkContextMenu::BookmarkContextMenu( 34 views::Widget* parent_widget, 35 Browser* browser, 36 Profile* profile, 37 PageNavigator* page_navigator, 38 const BookmarkNode* parent, 39 const std::vector<const BookmarkNode*>& selection, 40 bool close_on_remove) 41 : controller_(new BookmarkContextMenuController( 42 parent_widget ? parent_widget->GetNativeWindow() : NULL, this, 43 browser, profile, page_navigator, parent, selection)), 44 parent_widget_(parent_widget), 45 menu_(new views::MenuItemView(this)), 46 menu_runner_(new views::MenuRunner(menu_, 47 views::MenuRunner::HAS_MNEMONICS | 48 views::MenuRunner::IS_NESTED | 49 views::MenuRunner::CONTEXT_MENU)), 50 observer_(NULL), 51 close_on_remove_(close_on_remove) { 52 ui::SimpleMenuModel* menu_model = controller_->menu_model(); 53 for (int i = 0; i < menu_model->GetItemCount(); ++i) { 54 views::MenuModelAdapter::AppendMenuItemFromModel( 55 menu_model, i, menu_, menu_model->GetCommandIdAt(i)); 56 } 57} 58 59BookmarkContextMenu::~BookmarkContextMenu() { 60} 61 62void BookmarkContextMenu::RunMenuAt(const gfx::Point& point, 63 ui::MenuSourceType source_type) { 64 content::NotificationService::current()->Notify( 65 chrome::NOTIFICATION_BOOKMARK_CONTEXT_MENU_SHOWN, 66 content::Source<BookmarkContextMenu>(this), 67 content::NotificationService::NoDetails()); 68 // width/height don't matter here. 69 if (menu_runner_->RunMenuAt(parent_widget_, 70 NULL, 71 gfx::Rect(point.x(), point.y(), 0, 0), 72 views::MENU_ANCHOR_TOPLEFT, 73 source_type) == views::MenuRunner::MENU_DELETED) { 74 return; 75 } 76} 77 78void BookmarkContextMenu::SetPageNavigator(PageNavigator* navigator) { 79 controller_->set_navigator(navigator); 80} 81 82//////////////////////////////////////////////////////////////////////////////// 83// BookmarkContextMenu, views::MenuDelegate implementation: 84 85void BookmarkContextMenu::ExecuteCommand(int command_id, int event_flags) { 86 controller_->ExecuteCommand(command_id, event_flags); 87} 88 89bool BookmarkContextMenu::IsItemChecked(int command_id) const { 90 return controller_->IsCommandIdChecked(command_id); 91} 92 93bool BookmarkContextMenu::IsCommandEnabled(int command_id) const { 94 return controller_->IsCommandIdEnabled(command_id); 95} 96 97bool BookmarkContextMenu::IsCommandVisible(int command_id) const { 98 return controller_->IsCommandIdVisible(command_id); 99} 100 101bool BookmarkContextMenu::ShouldCloseAllMenusOnExecute(int id) { 102 return (id != IDC_BOOKMARK_BAR_REMOVE) || close_on_remove_; 103} 104 105//////////////////////////////////////////////////////////////////////////////// 106// BookmarkContextMenuControllerDelegate 107// implementation: 108 109void BookmarkContextMenu::CloseMenu() { 110 menu_->Cancel(); 111} 112 113void BookmarkContextMenu::WillExecuteCommand( 114 int command_id, 115 const std::vector<const BookmarkNode*>& bookmarks) { 116 if (observer_ && IsRemoveBookmarksCommand(command_id)) 117 observer_->WillRemoveBookmarks(bookmarks); 118} 119 120void BookmarkContextMenu::DidExecuteCommand(int command_id) { 121 if (observer_ && IsRemoveBookmarksCommand(command_id)) 122 observer_->DidRemoveBookmarks(); 123} 124