Refactor: Makes menus use gfx::FontList instead of gfx::Font.
[chromium-blink-merge.git] / ui / views / controls / menu / menu_delegate.h
blobe50ec8e5ce0d6c28865cf6954288470d9c4215f2
1 // Copyright (c) 2012 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 UI_VIEWS_CONTROLS_MENU_MENU_DELEGATE_H_
6 #define UI_VIEWS_CONTROLS_MENU_MENU_DELEGATE_H_
8 #include <set>
9 #include <string>
11 #include "base/logging.h"
12 #include "base/strings/string16.h"
13 #include "ui/base/dragdrop/drag_drop_types.h"
14 #include "ui/base/dragdrop/os_exchange_data.h"
15 #include "ui/views/controls/menu/menu_item_view.h"
17 using ui::OSExchangeData;
19 namespace gfx {
20 class FontList;
23 namespace ui {
24 class Accelerator;
27 namespace views {
29 class MenuButton;
31 // MenuDelegate --------------------------------------------------------------
33 // Delegate for a menu. This class is used as part of MenuItemView, see it
34 // for details.
35 // TODO(sky): merge this with ui::MenuModel.
36 class VIEWS_EXPORT MenuDelegate {
37 public:
38 // Used during drag and drop to indicate where the drop indicator should
39 // be rendered.
40 enum DropPosition {
41 DROP_UNKNOWN = -1,
43 // Indicates a drop is not allowed here.
44 DROP_NONE,
46 // Indicates the drop should occur before the item.
47 DROP_BEFORE,
49 // Indicates the drop should occur after the item.
50 DROP_AFTER,
52 // Indicates the drop should occur on the item.
53 DROP_ON
56 virtual ~MenuDelegate();
58 // Whether or not an item should be shown as checked. This is invoked for
59 // radio buttons and check buttons.
60 virtual bool IsItemChecked(int id) const;
62 // The string shown for the menu item. This is only invoked when an item is
63 // added with an empty label.
64 virtual base::string16 GetLabel(int id) const;
66 // The font for the menu item label.
67 virtual const gfx::FontList* GetLabelFontList(int id) const;
69 // Override the text color of a given menu item dependent on the
70 // |command_id| and its |is_hovered| state. Returns true if it chooses to
71 // override the color.
72 virtual bool GetForegroundColor(int command_id,
73 bool is_hovered,
74 SkColor* override_color) const;
76 // Override the background color of a given menu item dependent on the
77 // |command_id| and its |is_hovered| state. Returns true if it chooses to
78 // override the color.
79 virtual bool GetBackgroundColor(int command_id,
80 bool is_hovered,
81 SkColor* override_color) const;
83 // The tooltip shown for the menu item. This is invoked when the user
84 // hovers over the item, and no tooltip text has been set for that item.
85 virtual base::string16 GetTooltipText(int id,
86 const gfx::Point& screen_loc) const;
88 // If there is an accelerator for the menu item with id |id| it is set in
89 // |accelerator| and true is returned.
90 virtual bool GetAccelerator(int id, ui::Accelerator* accelerator);
92 // Shows the context menu with the specified id. This is invoked when the
93 // user does the appropriate gesture to show a context menu. The id
94 // identifies the id of the menu to show the context menu for.
95 // is_mouse_gesture is true if this is the result of a mouse gesture.
96 // If this is not the result of a mouse gesture |p| is the recommended
97 // location to display the content menu at. In either case, |p| is in
98 // screen coordinates.
99 // Returns true if a context menu was displayed, otherwise false
100 virtual bool ShowContextMenu(MenuItemView* source,
101 int id,
102 const gfx::Point& p,
103 ui::MenuSourceType source_type);
105 // Controller
106 virtual bool SupportsCommand(int id) const;
107 virtual bool IsCommandEnabled(int id) const;
108 virtual bool GetContextualLabel(int id, base::string16* out) const;
109 virtual void ExecuteCommand(int id) {
112 // If nested menus are showing (nested menus occur when a menu shows a context
113 // menu) this is invoked to determine if all the menus should be closed when
114 // the user selects the menu with the command |id|. This returns true to
115 // indicate that all menus should be closed. Return false if only the
116 // context menu should be closed.
117 virtual bool ShouldCloseAllMenusOnExecute(int id);
119 // Executes the specified command. mouse_event_flags give the flags of the
120 // mouse event that triggered this to be invoked (ui::MouseEvent
121 // flags). mouse_event_flags is 0 if this is triggered by a user gesture
122 // other than a mouse event.
123 virtual void ExecuteCommand(int id, int mouse_event_flags);
125 // Returns true if ExecuteCommand() should be invoked while leaving the
126 // menu open. Default implementation returns true.
127 virtual bool ShouldExecuteCommandWithoutClosingMenu(int id,
128 const ui::Event& e);
130 // Returns true if the specified event is one the user can use to trigger, or
131 // accept, the item. Defaults to left or right mouse buttons or tap.
132 virtual bool IsTriggerableEvent(MenuItemView* view, const ui::Event& e);
134 // Invoked to determine if drops can be accepted for a submenu. This is
135 // ONLY invoked for menus that have submenus and indicates whether or not
136 // a drop can occur on any of the child items of the item. For example,
137 // consider the following menu structure:
139 // A
140 // B
141 // C
143 // Where A has a submenu with children B and C. This is ONLY invoked for
144 // A, not B and C.
147 // To restrict which children can be dropped on override GetDropOperation.
148 virtual bool CanDrop(MenuItemView* menu, const OSExchangeData& data);
150 // See view for a description of this method.
151 virtual bool GetDropFormats(
152 MenuItemView* menu,
153 int* formats,
154 std::set<OSExchangeData::CustomFormat>* custom_formats);
156 // See view for a description of this method.
157 virtual bool AreDropTypesRequired(MenuItemView* menu);
159 // Returns the drop operation for the specified target menu item. This is
160 // only invoked if CanDrop returned true for the parent menu. position
161 // is set based on the location of the mouse, reset to specify a different
162 // position.
164 // If a drop should not be allowed, returned ui::DragDropTypes::DRAG_NONE.
165 virtual int GetDropOperation(MenuItemView* item,
166 const ui::DropTargetEvent& event,
167 DropPosition* position);
169 // Invoked to perform the drop operation. This is ONLY invoked if CanDrop()
170 // returned true for the parent menu item, and GetDropOperation() returned an
171 // operation other than ui::DragDropTypes::DRAG_NONE.
173 // |menu| is the menu the drop occurred on.
174 virtual int OnPerformDrop(MenuItemView* menu,
175 DropPosition position,
176 const ui::DropTargetEvent& event);
178 // Invoked to determine if it is possible for the user to drag the specified
179 // menu item.
180 virtual bool CanDrag(MenuItemView* menu);
182 // Invoked to write the data for a drag operation to data. sender is the
183 // MenuItemView being dragged.
184 virtual void WriteDragData(MenuItemView* sender, OSExchangeData* data);
186 // Invoked to determine the drag operations for a drag session of sender.
187 // See DragDropTypes for possible values.
188 virtual int GetDragOperations(MenuItemView* sender);
190 // Notification the menu has closed. This is only sent when running the
191 // menu for a drop.
192 virtual void DropMenuClosed(MenuItemView* menu) {
195 // Notification that the user has highlighted the specified item.
196 virtual void SelectionChanged(MenuItemView* menu) {
199 // If the user drags the mouse outside the bounds of the menu the delegate
200 // is queried for a sibling menu to show. If this returns non-null the
201 // current menu is hidden, and the menu returned from this method is shown.
203 // The delegate owns the returned menu, not the controller.
204 virtual MenuItemView* GetSiblingMenu(MenuItemView* menu,
205 const gfx::Point& screen_point,
206 MenuItemView::AnchorPosition* anchor,
207 bool* has_mnemonics,
208 MenuButton** button);
210 // Returns the max width menus can grow to be.
211 virtual int GetMaxWidthForMenu(MenuItemView* menu);
213 // Invoked prior to a menu being shown.
214 virtual void WillShowMenu(MenuItemView* menu);
216 // Invoked prior to a menu being hidden.
217 virtual void WillHideMenu(MenuItemView* menu);
219 // Returns additional horizontal spacing for the icon of the given item.
220 // The |command_id| specifies the item of interest, the |icon_size| tells the
221 // function the size of the icon and it will then return |left_margin|
222 // and |right_margin| accordingly. Note: Negative values can be returned.
223 virtual void GetHorizontalIconMargins(int command_id,
224 int icon_size,
225 int* left_margin,
226 int* right_margin) const;
227 // Returns true if the labels should reserve additional spacing for e.g.
228 // submenu indicators at the end of the line.
229 virtual bool ShouldReserveSpaceForSubmenuIndicator() const;
232 } // namespace views
234 #endif // UI_VIEWS_CONTROLS_MENU_MENU_DELEGATE_H_