wrench_menu.h revision 116680a4aac90f2aa7413d9095a592090648e557
1010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// found in the LICENSE file.
4010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
5010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#ifndef CHROME_BROWSER_UI_VIEWS_TOOLBAR_WRENCH_MENU_H_
6010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#define CHROME_BROWSER_UI_VIEWS_TOOLBAR_WRENCH_MENU_H_
7010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
8010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include <map>
9010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include <utility>
10010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
11010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
12010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "base/observer_list.h"
13010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "components/bookmarks/browser/base_bookmark_model_observer.h"
14010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "content/public/browser/notification_observer.h"
15010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "content/public/browser/notification_registrar.h"
16010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "ui/base/models/menu_model.h"
17010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "ui/views/controls/menu/menu_delegate.h"
18010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
19010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)class BookmarkMenuDelegate;
20010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)class Browser;
21010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)class WrenchMenuObserver;
22010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
23010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)namespace ui {
24010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)class NativeTheme;
25010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
26010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
27010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)namespace views {
28010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)class MenuButton;
29010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)struct MenuConfig;
30010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)class MenuItemView;
31010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)class MenuRunner;
32010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)class View;
33010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}  // namespace views
34010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
35010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// WrenchMenu adapts the WrenchMenuModel to view's menu related classes.
36010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)class WrenchMenu : public views::MenuDelegate,
37                   public BaseBookmarkModelObserver,
38                   public content::NotificationObserver {
39 public:
40  // TODO: remove |use_new_menu| and |supports_new_separators|.
41  WrenchMenu(Browser* browser,
42             bool use_new_menu,
43             bool supports_new_separators);
44  virtual ~WrenchMenu();
45
46  void Init(ui::MenuModel* model);
47
48  // Shows the menu relative to the specified view.
49  void RunMenu(views::MenuButton* host);
50
51  // Whether the menu is currently visible to the user.
52  bool IsShowing();
53
54  bool use_new_menu() const { return use_new_menu_; }
55
56  void AddObserver(WrenchMenuObserver* observer);
57  void RemoveObserver(WrenchMenuObserver* observer);
58
59  // MenuDelegate overrides:
60  virtual const gfx::FontList* GetLabelFontList(int command_id) const OVERRIDE;
61  virtual bool GetShouldUseDisabledEmphasizedForegroundColor(
62      int command_id) const OVERRIDE;
63  virtual base::string16 GetTooltipText(int command_id,
64                                        const gfx::Point& p) const OVERRIDE;
65  virtual bool IsTriggerableEvent(views::MenuItemView* menu,
66                                  const ui::Event& e) OVERRIDE;
67  virtual bool GetDropFormats(
68      views::MenuItemView* menu,
69      int* formats,
70      std::set<ui::OSExchangeData::CustomFormat>* custom_formats) OVERRIDE;
71  virtual bool AreDropTypesRequired(views::MenuItemView* menu) OVERRIDE;
72  virtual bool CanDrop(views::MenuItemView* menu,
73                       const ui::OSExchangeData& data) OVERRIDE;
74  virtual int GetDropOperation(views::MenuItemView* item,
75                               const ui::DropTargetEvent& event,
76                               DropPosition* position) OVERRIDE;
77  virtual int OnPerformDrop(views::MenuItemView* menu,
78                            DropPosition position,
79                            const ui::DropTargetEvent& event) OVERRIDE;
80  virtual bool ShowContextMenu(views::MenuItemView* source,
81                               int command_id,
82                               const gfx::Point& p,
83                               ui::MenuSourceType source_type) OVERRIDE;
84  virtual bool CanDrag(views::MenuItemView* menu) OVERRIDE;
85  virtual void WriteDragData(views::MenuItemView* sender,
86                             ui::OSExchangeData* data) OVERRIDE;
87  virtual int GetDragOperations(views::MenuItemView* sender) OVERRIDE;
88  virtual int GetMaxWidthForMenu(views::MenuItemView* menu) OVERRIDE;
89  virtual bool IsItemChecked(int command_id) const OVERRIDE;
90  virtual bool IsCommandEnabled(int command_id) const OVERRIDE;
91  virtual void ExecuteCommand(int command_id, int mouse_event_flags) OVERRIDE;
92  virtual bool GetAccelerator(int command_id,
93                              ui::Accelerator* accelerator) const OVERRIDE;
94  virtual void WillShowMenu(views::MenuItemView* menu) OVERRIDE;
95  virtual void WillHideMenu(views::MenuItemView* menu) OVERRIDE;
96  virtual bool ShouldCloseOnDragComplete() OVERRIDE;
97
98  // BaseBookmarkModelObserver overrides:
99  virtual void BookmarkModelChanged() OVERRIDE;
100
101  // content::NotificationObserver overrides:
102  virtual void Observe(int type,
103                       const content::NotificationSource& source,
104                       const content::NotificationDetails& details) OVERRIDE;
105
106 private:
107  class CutCopyPasteView;
108  class RecentTabsMenuModelDelegate;
109  class ZoomView;
110
111  typedef std::pair<ui::MenuModel*,int> Entry;
112  typedef std::map<int,Entry> CommandIDToEntry;
113
114  // Populates |parent| with all the child menus in |model|. Recursively invokes
115  // |PopulateMenu| for any submenu.
116  void PopulateMenu(views::MenuItemView* parent,
117                    ui::MenuModel* model);
118
119  // Adds a new menu item to |parent| at |menu_index| to represent the item in
120  // |model| at |model_index|:
121  // - |menu_index|: position in |parent| to add the new item.
122  // - |model_index|: position in |model| to retrieve information about the
123  //   new menu item.
124  // - |height|: For button containing menu items, a |height| override can be
125  //   specified with a number bigger then 0.
126  // The returned item's MenuItemView::GetCommand() is the same as that of
127  // |model|->GetCommandIdAt(|model_index|).
128  views::MenuItemView* AddMenuItem(views::MenuItemView* parent,
129                                   int menu_index,
130                                   ui::MenuModel* model,
131                                   int model_index,
132                                   ui::MenuModel::ItemType menu_type,
133                                   int height);
134
135  // Invoked from the cut/copy/paste menus. Cancels the current active menu and
136  // activates the menu item in |model| at |index|.
137  void CancelAndEvaluate(ui::MenuModel* model, int index);
138
139  // Creates the bookmark menu if necessary. Does nothing if already created or
140  // the bookmark model isn't loaded.
141  void CreateBookmarkMenu();
142
143  // Returns the index of the MenuModel/index pair representing the |command_id|
144  // in |command_id_to_entry_|.
145  int ModelIndexFromCommandId(int command_id) const;
146
147  // The views menu. Owned by |menu_runner_|.
148  views::MenuItemView* root_;
149
150  scoped_ptr<views::MenuRunner> menu_runner_;
151
152  // Maps from the command ID in model to the model/index pair the item came
153  // from.
154  CommandIDToEntry command_id_to_entry_;
155
156  // Browser the menu is being shown for.
157  Browser* browser_;
158
159  // |CancelAndEvaluate| sets |selected_menu_model_| and |selected_index_|.
160  // If |selected_menu_model_| is non-null after the menu completes
161  // ActivatedAt is invoked. This is done so that ActivatedAt isn't invoked
162  // while the message loop is nested.
163  ui::MenuModel* selected_menu_model_;
164  int selected_index_;
165
166  // Used for managing the bookmark menu items.
167  scoped_ptr<BookmarkMenuDelegate> bookmark_menu_delegate_;
168
169  // Menu corresponding to IDC_BOOKMARKS_MENU.
170  views::MenuItemView* bookmark_menu_;
171
172  // Menu corresponding to IDC_FEEDBACK.
173  views::MenuItemView* feedback_menu_item_;
174
175  // Used for managing "Recent tabs" menu items.
176  scoped_ptr<RecentTabsMenuModelDelegate> recent_tabs_menu_model_delegate_;
177
178  content::NotificationRegistrar registrar_;
179
180  const bool use_new_menu_;
181
182  const bool supports_new_separators_;
183
184  ObserverList<WrenchMenuObserver> observer_list_;
185
186  DISALLOW_COPY_AND_ASSIGN(WrenchMenu);
187};
188
189#endif  // CHROME_BROWSER_UI_VIEWS_TOOLBAR_WRENCH_MENU_H_
190