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 "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"
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 GetForegroundColor(int command_id
,
65 SkColor
* override_color
) const OVERRIDE
;
66 virtual base::string16
GetTooltipText(int command_id
,
67 const gfx::Point
& p
) const OVERRIDE
;
68 virtual bool IsTriggerableEvent(views::MenuItemView
* menu
,
69 const ui::Event
& e
) OVERRIDE
;
70 virtual bool GetDropFormats(
71 views::MenuItemView
* menu
,
73 std::set
<ui::OSExchangeData::CustomFormat
>* custom_formats
) OVERRIDE
;
74 virtual bool AreDropTypesRequired(views::MenuItemView
* menu
) OVERRIDE
;
75 virtual bool CanDrop(views::MenuItemView
* menu
,
76 const ui::OSExchangeData
& data
) OVERRIDE
;
77 virtual int GetDropOperation(views::MenuItemView
* item
,
78 const ui::DropTargetEvent
& event
,
79 DropPosition
* position
) OVERRIDE
;
80 virtual int OnPerformDrop(views::MenuItemView
* menu
,
81 DropPosition position
,
82 const ui::DropTargetEvent
& event
) OVERRIDE
;
83 virtual bool ShowContextMenu(views::MenuItemView
* source
,
86 ui::MenuSourceType source_type
) OVERRIDE
;
87 virtual bool CanDrag(views::MenuItemView
* menu
) OVERRIDE
;
88 virtual void WriteDragData(views::MenuItemView
* sender
,
89 ui::OSExchangeData
* data
) OVERRIDE
;
90 virtual int GetDragOperations(views::MenuItemView
* sender
) OVERRIDE
;
91 virtual int GetMaxWidthForMenu(views::MenuItemView
* menu
) OVERRIDE
;
92 virtual bool IsItemChecked(int command_id
) const OVERRIDE
;
93 virtual bool IsCommandEnabled(int command_id
) const OVERRIDE
;
94 virtual void ExecuteCommand(int command_id
, int mouse_event_flags
) OVERRIDE
;
95 virtual bool GetAccelerator(int command_id
,
96 ui::Accelerator
* accelerator
) OVERRIDE
;
97 virtual void WillShowMenu(views::MenuItemView
* menu
) OVERRIDE
;
98 virtual void WillHideMenu(views::MenuItemView
* menu
) OVERRIDE
;
100 // BaseBookmarkModelObserver overrides:
101 virtual void BookmarkModelChanged() OVERRIDE
;
103 // content::NotificationObserver overrides:
104 virtual void Observe(int type
,
105 const content::NotificationSource
& source
,
106 const content::NotificationDetails
& details
) OVERRIDE
;
109 class CutCopyPasteView
;
110 class RecentTabsMenuModelDelegate
;
113 typedef std::pair
<ui::MenuModel
*,int> Entry
;
114 typedef std::map
<int,Entry
> CommandIDToEntry
;
116 const ui::NativeTheme
* GetNativeTheme() const;
118 // Populates |parent| with all the child menus in |model|. Recursively invokes
119 // |PopulateMenu| for any submenu.
120 void PopulateMenu(views::MenuItemView
* parent
,
121 ui::MenuModel
* model
);
123 // Adds a new menu item to |parent| at |menu_index| to represent the item in
124 // |model| at |model_index|:
125 // - |menu_index|: position in |parent| to add the new item.
126 // - |model_index|: position in |model| to retrieve information about the
128 // - |height|: For button containing menu items, a |height| override can be
129 // specified with a number bigger then 0.
130 // The returned item's MenuItemView::GetCommand() is the same as that of
131 // |model|->GetCommandIdAt(|model_index|).
132 views::MenuItemView
* AddMenuItem(views::MenuItemView
* parent
,
134 ui::MenuModel
* model
,
136 ui::MenuModel::ItemType menu_type
,
139 // Invoked from the cut/copy/paste menus. Cancels the current active menu and
140 // activates the menu item in |model| at |index|.
141 void CancelAndEvaluate(ui::MenuModel
* model
, int index
);
143 // Creates the bookmark menu if necessary. Does nothing if already created or
144 // the bookmark model isn't loaded.
145 void CreateBookmarkMenu();
147 // Returns the index of the MenuModel/index pair representing the |command_id|
148 // in |command_id_to_entry_|.
149 int ModelIndexFromCommandId(int command_id
) const;
151 // The views menu. Owned by |menu_runner_|.
152 views::MenuItemView
* root_
;
154 scoped_ptr
<views::MenuRunner
> menu_runner_
;
156 // Maps from the command ID in model to the model/index pair the item came
158 CommandIDToEntry command_id_to_entry_
;
160 // Browser the menu is being shown for.
163 // |CancelAndEvaluate| sets |selected_menu_model_| and |selected_index_|.
164 // If |selected_menu_model_| is non-null after the menu completes
165 // ActivatedAt is invoked. This is done so that ActivatedAt isn't invoked
166 // while the message loop is nested.
167 ui::MenuModel
* selected_menu_model_
;
170 // Used for managing the bookmark menu items.
171 scoped_ptr
<BookmarkMenuDelegate
> bookmark_menu_delegate_
;
173 // Menu corresponding to IDC_BOOKMARKS_MENU.
174 views::MenuItemView
* bookmark_menu_
;
176 // Menu corresponding to IDC_FEEDBACK.
177 views::MenuItemView
* feedback_menu_item_
;
179 // Used for managing "Recent tabs" menu items.
180 scoped_ptr
<RecentTabsMenuModelDelegate
> recent_tabs_menu_model_delegate_
;
182 content::NotificationRegistrar registrar_
;
184 const bool use_new_menu_
;
186 const bool supports_new_separators_
;
188 ObserverList
<WrenchMenuObserver
> observer_list_
;
190 DISALLOW_COPY_AND_ASSIGN(WrenchMenu
);
193 #endif // CHROME_BROWSER_UI_VIEWS_TOOLBAR_WRENCH_MENU_H_