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 "components/bookmarks/browser/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  enum RunFlags {
41    // Indicates that the menu was opened for a drag-and-drop operation.
42    FOR_DROP = 1 << 0,
43  };
44
45  WrenchMenu(Browser* browser, int run_flags);
46  virtual ~WrenchMenu();
47
48  void Init(ui::MenuModel* model);
49
50  // Shows the menu relative to the specified view.
51  void RunMenu(views::MenuButton* host);
52
53  // Closes the menu if it is open, otherwise does nothing.
54  void CloseMenu();
55
56  // Whether the menu is currently visible to the user.
57  bool IsShowing();
58
59  bool for_drop() const { return (run_flags_ & FOR_DROP) != 0; }
60
61  void AddObserver(WrenchMenuObserver* observer);
62  void RemoveObserver(WrenchMenuObserver* observer);
63
64  // MenuDelegate overrides:
65  virtual const gfx::FontList* GetLabelFontList(int command_id) const OVERRIDE;
66  virtual bool GetShouldUseDisabledEmphasizedForegroundColor(
67      int command_id) const OVERRIDE;
68  virtual base::string16 GetTooltipText(int command_id,
69                                        const gfx::Point& p) const OVERRIDE;
70  virtual bool IsTriggerableEvent(views::MenuItemView* menu,
71                                  const ui::Event& e) OVERRIDE;
72  virtual bool GetDropFormats(
73      views::MenuItemView* menu,
74      int* formats,
75      std::set<ui::OSExchangeData::CustomFormat>* custom_formats) OVERRIDE;
76  virtual bool AreDropTypesRequired(views::MenuItemView* menu) OVERRIDE;
77  virtual bool CanDrop(views::MenuItemView* menu,
78                       const ui::OSExchangeData& data) OVERRIDE;
79  virtual int GetDropOperation(views::MenuItemView* item,
80                               const ui::DropTargetEvent& event,
81                               DropPosition* position) OVERRIDE;
82  virtual int OnPerformDrop(views::MenuItemView* menu,
83                            DropPosition position,
84                            const ui::DropTargetEvent& event) OVERRIDE;
85  virtual bool ShowContextMenu(views::MenuItemView* source,
86                               int command_id,
87                               const gfx::Point& p,
88                               ui::MenuSourceType source_type) OVERRIDE;
89  virtual bool CanDrag(views::MenuItemView* menu) OVERRIDE;
90  virtual void WriteDragData(views::MenuItemView* sender,
91                             ui::OSExchangeData* data) OVERRIDE;
92  virtual int GetDragOperations(views::MenuItemView* sender) OVERRIDE;
93  virtual int GetMaxWidthForMenu(views::MenuItemView* menu) OVERRIDE;
94  virtual bool IsItemChecked(int command_id) const OVERRIDE;
95  virtual bool IsCommandEnabled(int command_id) const OVERRIDE;
96  virtual void ExecuteCommand(int command_id, int mouse_event_flags) OVERRIDE;
97  virtual bool GetAccelerator(int command_id,
98                              ui::Accelerator* accelerator) const OVERRIDE;
99  virtual void WillShowMenu(views::MenuItemView* menu) OVERRIDE;
100  virtual void WillHideMenu(views::MenuItemView* menu) OVERRIDE;
101  virtual bool ShouldCloseOnDragComplete() OVERRIDE;
102
103  // BaseBookmarkModelObserver overrides:
104  virtual void BookmarkModelChanged() OVERRIDE;
105
106  // content::NotificationObserver overrides:
107  virtual void Observe(int type,
108                       const content::NotificationSource& source,
109                       const content::NotificationDetails& details) OVERRIDE;
110
111 private:
112  class CutCopyPasteView;
113  class RecentTabsMenuModelDelegate;
114  class ZoomView;
115
116  typedef std::pair<ui::MenuModel*,int> Entry;
117  typedef std::map<int,Entry> CommandIDToEntry;
118
119  // Populates |parent| with all the child menus in |model|. Recursively invokes
120  // |PopulateMenu| for any submenu.
121  void PopulateMenu(views::MenuItemView* parent,
122                    ui::MenuModel* model);
123
124  // Adds a new menu item to |parent| at |menu_index| to represent the item in
125  // |model| at |model_index|:
126  // - |menu_index|: position in |parent| to add the new item.
127  // - |model_index|: position in |model| to retrieve information about the
128  //   new menu item.
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
137  // Invoked from the cut/copy/paste menus. Cancels the current active menu and
138  // activates the menu item in |model| at |index|.
139  void CancelAndEvaluate(ui::MenuModel* model, int index);
140
141  // Creates the bookmark menu if necessary. Does nothing if already created or
142  // the bookmark model isn't loaded.
143  void CreateBookmarkMenu();
144
145  // Returns the index of the MenuModel/index pair representing the |command_id|
146  // in |command_id_to_entry_|.
147  int ModelIndexFromCommandId(int command_id) const;
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 command ID in model to the model/index pair the item came
155  // from.
156  CommandIDToEntry command_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  content::NotificationRegistrar registrar_;
181
182  // The bit mask of RunFlags.
183  const int run_flags_;
184
185  ObserverList<WrenchMenuObserver> observer_list_;
186
187  DISALLOW_COPY_AND_ASSIGN(WrenchMenu);
188};
189
190#endif  // CHROME_BROWSER_UI_VIEWS_TOOLBAR_WRENCH_MENU_H_
191