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