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.
5 #ifndef CHROME_BROWSER_UI_VIEWS_TOOLBAR_WRENCH_MENU_H_
6 #define CHROME_BROWSER_UI_VIEWS_TOOLBAR_WRENCH_MENU_H_
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"
19 class BookmarkMenuDelegate
;
21 class WrenchMenuObserver
;
35 // WrenchMenu adapts the WrenchMenuModel to view's menu related classes.
36 class WrenchMenu
: public views::MenuDelegate
,
37 public BaseBookmarkModelObserver
,
38 public content::NotificationObserver
{
40 // TODO: remove |use_new_menu| and |supports_new_separators|.
41 WrenchMenu(Browser
* browser
,
43 bool supports_new_separators
);
44 virtual ~WrenchMenu();
46 void Init(ui::MenuModel
* model
);
48 // Shows the menu relative to the specified view.
49 void RunMenu(views::MenuButton
* host
);
51 // Whether the menu is currently visible to the user.
54 const views::MenuConfig
& GetMenuConfig() const;
56 bool use_new_menu() const { return use_new_menu_
; }
58 void AddObserver(WrenchMenuObserver
* observer
);
59 void RemoveObserver(WrenchMenuObserver
* observer
);
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
,
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
,
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
) const OVERRIDE
;
96 virtual void WillShowMenu(views::MenuItemView
* menu
) OVERRIDE
;
97 virtual void WillHideMenu(views::MenuItemView
* menu
) OVERRIDE
;
99 // BaseBookmarkModelObserver overrides:
100 virtual void BookmarkModelChanged() OVERRIDE
;
102 // content::NotificationObserver overrides:
103 virtual void Observe(int type
,
104 const content::NotificationSource
& source
,
105 const content::NotificationDetails
& details
) OVERRIDE
;
108 class CutCopyPasteView
;
109 class RecentTabsMenuModelDelegate
;
112 typedef std::pair
<ui::MenuModel
*,int> Entry
;
113 typedef std::map
<int,Entry
> CommandIDToEntry
;
115 const ui::NativeTheme
* GetNativeTheme() const;
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
);
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
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
,
133 ui::MenuModel
* model
,
135 ui::MenuModel::ItemType menu_type
,
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
);
142 // Creates the bookmark menu if necessary. Does nothing if already created or
143 // the bookmark model isn't loaded.
144 void CreateBookmarkMenu();
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;
150 // The views menu. Owned by |menu_runner_|.
151 views::MenuItemView
* root_
;
153 scoped_ptr
<views::MenuRunner
> menu_runner_
;
155 // Maps from the command ID in model to the model/index pair the item came
157 CommandIDToEntry command_id_to_entry_
;
159 // Browser the menu is being shown for.
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_
;
169 // Used for managing the bookmark menu items.
170 scoped_ptr
<BookmarkMenuDelegate
> bookmark_menu_delegate_
;
172 // Menu corresponding to IDC_BOOKMARKS_MENU.
173 views::MenuItemView
* bookmark_menu_
;
175 // Menu corresponding to IDC_FEEDBACK.
176 views::MenuItemView
* feedback_menu_item_
;
178 // Used for managing "Recent tabs" menu items.
179 scoped_ptr
<RecentTabsMenuModelDelegate
> recent_tabs_menu_model_delegate_
;
181 content::NotificationRegistrar registrar_
;
183 const bool use_new_menu_
;
185 const bool supports_new_separators_
;
187 ObserverList
<WrenchMenuObserver
> observer_list_
;
189 DISALLOW_COPY_AND_ASSIGN(WrenchMenu
);
192 #endif // CHROME_BROWSER_UI_VIEWS_TOOLBAR_WRENCH_MENU_H_