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 #ifndef CHROME_BROWSER_UI_VIEWS_WRENCH_MENU_H_ 6 #define CHROME_BROWSER_UI_VIEWS_WRENCH_MENU_H_ 7 8 #include <map> 9 #include <utility> 10 11 #include "base/memory/scoped_ptr.h" 12 #include "chrome/browser/bookmarks/base_bookmark_model_observer.h" 13 #include "content/public/browser/notification_observer.h" 14 #include "content/public/browser/notification_registrar.h" 15 #include "ui/base/models/menu_model.h" 16 #include "ui/views/controls/menu/menu_delegate.h" 17 18 class BookmarkMenuDelegate; 19 class Browser; 20 21 namespace ui { 22 class NativeTheme; 23 } 24 25 namespace views { 26 class MenuButton; 27 struct MenuConfig; 28 class MenuItemView; 29 class MenuRunner; 30 class View; 31 } // namespace views 32 33 // WrenchMenu adapts the WrenchMenuModel to view's menu related classes. 34 class WrenchMenu : public views::MenuDelegate, 35 public BaseBookmarkModelObserver, 36 public content::NotificationObserver { 37 public: 38 // TODO: remove |use_new_menu| and |supports_new_separators|. 39 WrenchMenu(Browser* browser, 40 bool use_new_menu, 41 bool supports_new_separators); 42 virtual ~WrenchMenu(); 43 44 void Init(ui::MenuModel* model); 45 46 // Shows the menu relative to the specified view. 47 void RunMenu(views::MenuButton* host); 48 49 // Whether the menu is currently visible to the user. 50 bool IsShowing(); 51 52 const views::MenuConfig& GetMenuConfig() const; 53 54 bool use_new_menu() const { return use_new_menu_; } 55 56 // MenuDelegate overrides: 57 virtual const gfx::Font* GetLabelFont(int index) const OVERRIDE; 58 virtual bool GetForegroundColor(int command_id, 59 bool is_hovered, 60 SkColor* override_color) const OVERRIDE; 61 virtual string16 GetTooltipText(int id, const gfx::Point& p) const OVERRIDE; 62 virtual bool IsTriggerableEvent(views::MenuItemView* menu, 63 const ui::Event& e) OVERRIDE; 64 virtual bool GetDropFormats( 65 views::MenuItemView* menu, 66 int* formats, 67 std::set<ui::OSExchangeData::CustomFormat>* custom_formats) OVERRIDE; 68 virtual bool AreDropTypesRequired(views::MenuItemView* menu) OVERRIDE; 69 virtual bool CanDrop(views::MenuItemView* menu, 70 const ui::OSExchangeData& data) OVERRIDE; 71 virtual int GetDropOperation(views::MenuItemView* item, 72 const ui::DropTargetEvent& event, 73 DropPosition* position) OVERRIDE; 74 virtual int OnPerformDrop(views::MenuItemView* menu, 75 DropPosition position, 76 const ui::DropTargetEvent& event) OVERRIDE; 77 virtual bool ShowContextMenu(views::MenuItemView* source, 78 int id, 79 const gfx::Point& p, 80 ui::MenuSourceType source_type) OVERRIDE; 81 virtual bool CanDrag(views::MenuItemView* menu) OVERRIDE; 82 virtual void WriteDragData(views::MenuItemView* sender, 83 ui::OSExchangeData* data) OVERRIDE; 84 virtual int GetDragOperations(views::MenuItemView* sender) OVERRIDE; 85 virtual int GetMaxWidthForMenu(views::MenuItemView* menu) OVERRIDE; 86 virtual bool IsItemChecked(int id) const OVERRIDE; 87 virtual bool IsCommandEnabled(int id) const OVERRIDE; 88 virtual void ExecuteCommand(int id, int mouse_event_flags) OVERRIDE; 89 virtual bool GetAccelerator(int id, ui::Accelerator* accelerator) OVERRIDE; 90 virtual void WillShowMenu(views::MenuItemView* menu) OVERRIDE; 91 virtual void WillHideMenu(views::MenuItemView* menu) OVERRIDE; 92 93 // BaseBookmarkModelObserver overrides: 94 virtual void BookmarkModelChanged() OVERRIDE; 95 96 // content::NotificationObserver overrides: 97 virtual void Observe(int type, 98 const content::NotificationSource& source, 99 const content::NotificationDetails& details) OVERRIDE; 100 101 private: 102 class CutCopyPasteView; 103 class RecentTabsMenuModelDelegate; 104 class ZoomView; 105 106 typedef std::pair<ui::MenuModel*,int> Entry; 107 typedef std::map<int,Entry> IDToEntry; 108 109 const ui::NativeTheme* GetNativeTheme() const; 110 111 // Populates |parent| with all the child menus in |model|. Recursively invokes 112 // |PopulateMenu| for any submenu. |next_id| is incremented for every menu 113 // that is created. 114 void PopulateMenu(views::MenuItemView* parent, 115 ui::MenuModel* model, 116 int* next_id); 117 118 // Adds a new menu to |parent| to represent the MenuModel/index pair passed 119 // in. 120 // Fur button containing menu items a |height| override can be specified with 121 // a number bigger then 0. 122 views::MenuItemView* AppendMenuItem(views::MenuItemView* parent, 123 ui::MenuModel* model, 124 int index, 125 ui::MenuModel::ItemType menu_type, 126 int* next_id, 127 int height); 128 129 // Invoked from the cut/copy/paste menus. Cancels the current active menu and 130 // activates the menu item in |model| at |index|. 131 void CancelAndEvaluate(ui::MenuModel* model, int index); 132 133 // Creates the bookmark menu if necessary. Does nothing if already created or 134 // the bookmark model isn't loaded. 135 void CreateBookmarkMenu(); 136 137 // Returns true if |id| identifies a bookmark menu item. 138 bool is_bookmark_command(int id) const { 139 return bookmark_menu_delegate_.get() && id >= first_bookmark_command_id_; 140 } 141 142 // Returns true if |id| identifies a recent tabs menu item. 143 bool is_recent_tabs_command(int id) const { 144 return (recent_tabs_menu_model_delegate_.get() && 145 id >= first_recent_tabs_command_id_ && 146 id <= last_recent_tabs_command_id_); 147 } 148 149 // The views menu. Owned by |menu_runner_|. 150 views::MenuItemView* root_; 151 152 scoped_ptr<views::MenuRunner> menu_runner_; 153 154 // Maps from the ID as understood by MenuItemView to the model/index pair the 155 // item came from. 156 IDToEntry id_to_entry_; 157 158 // Browser the menu is being shown for. 159 Browser* browser_; 160 161 // |CancelAndEvaluate| sets |selected_menu_model_| and |selected_index_|. 162 // If |selected_menu_model_| is non-null after the menu completes 163 // ActivatedAt is invoked. This is done so that ActivatedAt isn't invoked 164 // while the message loop is nested. 165 ui::MenuModel* selected_menu_model_; 166 int selected_index_; 167 168 // Used for managing the bookmark menu items. 169 scoped_ptr<BookmarkMenuDelegate> bookmark_menu_delegate_; 170 171 // Menu corresponding to IDC_BOOKMARKS_MENU. 172 views::MenuItemView* bookmark_menu_; 173 174 // Menu corresponding to IDC_FEEDBACK. 175 views::MenuItemView* feedback_menu_item_; 176 177 // Used for managing "Recent tabs" menu items. 178 scoped_ptr<RecentTabsMenuModelDelegate> recent_tabs_menu_model_delegate_; 179 180 // First ID to use for the items representing bookmarks in the bookmark menu. 181 int first_bookmark_command_id_; 182 183 // First/last IDs to use for the items of the recent tabs sub-menu. 184 int first_recent_tabs_command_id_; 185 int last_recent_tabs_command_id_; 186 187 content::NotificationRegistrar registrar_; 188 189 const bool use_new_menu_; 190 191 const bool supports_new_separators_; 192 193 DISALLOW_COPY_AND_ASSIGN(WrenchMenu); 194 }; 195 196 #endif // CHROME_BROWSER_UI_VIEWS_WRENCH_MENU_H_ 197