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 "grit/generated_resources.h" 14 #include "ui/base/l10n/l10n_util.h" 15 #include "ui/views/controls/menu/menu_item_view.h" 16 #include "ui/views/controls/menu/menu_model_adapter.h" 17 #include "ui/views/controls/menu/menu_runner.h" 18 #include "ui/views/widget/widget.h" 19 20 using content::PageNavigator; 21 22 namespace { 23 24 // Returns true if |command_id| corresponds to a command that causes one or more 25 // bookmarks to be removed. 26 bool IsRemoveBookmarksCommand(int command_id) { 27 return command_id == IDC_CUT || command_id == IDC_BOOKMARK_BAR_REMOVE; 28 } 29 30 } // namespace 31 32 //////////////////////////////////////////////////////////////////////////////// 33 // BookmarkContextMenu, public: 34 35 BookmarkContextMenu::BookmarkContextMenu( 36 views::Widget* parent_widget, 37 Browser* browser, 38 Profile* profile, 39 PageNavigator* page_navigator, 40 const BookmarkNode* parent, 41 const std::vector<const BookmarkNode*>& selection, 42 bool close_on_remove) 43 : controller_(new BookmarkContextMenuController( 44 parent_widget ? parent_widget->GetNativeWindow() : NULL, this, 45 browser, profile, page_navigator, parent, selection)), 46 parent_widget_(parent_widget), 47 menu_(new views::MenuItemView(this)), 48 menu_runner_(new views::MenuRunner(menu_)), 49 observer_(NULL), 50 close_on_remove_(close_on_remove) { 51 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 59 BookmarkContextMenu::~BookmarkContextMenu() { 60 } 61 62 void 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( 70 parent_widget_, 71 NULL, 72 gfx::Rect(point.x(), point.y(), 0, 0), 73 views::MENU_ANCHOR_TOPLEFT, 74 source_type, 75 (views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::IS_NESTED | 76 views::MenuRunner::CONTEXT_MENU)) == 77 views::MenuRunner::MENU_DELETED) { 78 return; 79 } 80 } 81 82 void BookmarkContextMenu::SetPageNavigator(PageNavigator* navigator) { 83 controller_->set_navigator(navigator); 84 } 85 86 //////////////////////////////////////////////////////////////////////////////// 87 // BookmarkContextMenu, views::MenuDelegate implementation: 88 89 void BookmarkContextMenu::ExecuteCommand(int command_id, int event_flags) { 90 controller_->ExecuteCommand(command_id, event_flags); 91 } 92 93 bool BookmarkContextMenu::IsItemChecked(int command_id) const { 94 return controller_->IsCommandIdChecked(command_id); 95 } 96 97 bool BookmarkContextMenu::IsCommandEnabled(int command_id) const { 98 return controller_->IsCommandIdEnabled(command_id); 99 } 100 101 bool BookmarkContextMenu::IsCommandVisible(int command_id) const { 102 return controller_->IsCommandIdVisible(command_id); 103 } 104 105 bool BookmarkContextMenu::ShouldCloseAllMenusOnExecute(int id) { 106 return (id != IDC_BOOKMARK_BAR_REMOVE) || close_on_remove_; 107 } 108 109 //////////////////////////////////////////////////////////////////////////////// 110 // BookmarkContextMenuControllerDelegate 111 // implementation: 112 113 void BookmarkContextMenu::CloseMenu() { 114 menu_->Cancel(); 115 } 116 117 void BookmarkContextMenu::WillExecuteCommand( 118 int command_id, 119 const std::vector<const BookmarkNode*>& bookmarks) { 120 if (observer_ && IsRemoveBookmarksCommand(command_id)) 121 observer_->WillRemoveBookmarks(bookmarks); 122 } 123 124 void BookmarkContextMenu::DidExecuteCommand(int command_id) { 125 if (observer_ && IsRemoveBookmarksCommand(command_id)) 126 observer_->DidRemoveBookmarks(); 127 } 128