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_BASE_MODELS_SIMPLE_MENU_MODEL_H_
6 #define UI_BASE_MODELS_SIMPLE_MENU_MODEL_H_
10 #include "base/memory/weak_ptr.h"
11 #include "base/strings/string16.h"
12 #include "ui/base/models/menu_model.h"
20 class ButtonMenuItemModel
;
22 // A simple MenuModel implementation with an imperative API for adding menu
23 // items. This makes it easy to construct fixed menus. Menus populated by
24 // dynamic data sources may be better off implementing MenuModel directly.
25 // The breadth of MenuModel is not exposed through this API.
26 class UI_BASE_EXPORT SimpleMenuModel
: public MenuModel
{
28 class UI_BASE_EXPORT Delegate
{
30 virtual ~Delegate() {}
32 // Methods for determining the state of specific command ids.
33 virtual bool IsCommandIdChecked(int command_id
) const = 0;
34 virtual bool IsCommandIdEnabled(int command_id
) const = 0;
35 virtual bool IsCommandIdVisible(int command_id
) const;
37 // Gets the accelerator for the specified command id. Returns true if the
38 // command id has a valid accelerator, false otherwise.
39 virtual bool GetAcceleratorForCommandId(
41 ui::Accelerator
* accelerator
) = 0;
43 // Some command ids have labels, sublabels, minor text and icons that change
45 virtual bool IsItemForCommandIdDynamic(int command_id
) const;
46 virtual base::string16
GetLabelForCommandId(int command_id
) const;
47 virtual base::string16
GetSublabelForCommandId(int command_id
) const;
48 virtual base::string16
GetMinorTextForCommandId(int command_id
) const;
49 // Gets the icon for the item with the specified id, returning true if there
50 // is an icon, false otherwise.
51 virtual bool GetIconForCommandId(int command_id
,
52 gfx::Image
* icon
) const;
54 // Notifies the delegate that the item with the specified command id was
55 // visually highlighted within the menu.
56 virtual void CommandIdHighlighted(int command_id
);
58 // Performs the action associates with the specified command id.
59 // The passed |event_flags| are the flags from the event which issued this
60 // command and they can be examined to find modifier keys.
61 virtual void ExecuteCommand(int command_id
, int event_flags
) = 0;
63 // Notifies the delegate that the menu is about to show.
64 virtual void MenuWillShow(SimpleMenuModel
* source
);
66 // Notifies the delegate that the menu has closed.
67 virtual void MenuClosed(SimpleMenuModel
* source
);
70 // The Delegate can be NULL, though if it is items can't be checked or
72 explicit SimpleMenuModel(Delegate
* delegate
);
73 ~SimpleMenuModel() override
;
75 // Methods for adding items to the model.
76 void AddItem(int command_id
, const base::string16
& label
);
77 void AddItemWithStringId(int command_id
, int string_id
);
78 void AddCheckItem(int command_id
, const base::string16
& label
);
79 void AddCheckItemWithStringId(int command_id
, int string_id
);
80 void AddRadioItem(int command_id
, const base::string16
& label
, int group_id
);
81 void AddRadioItemWithStringId(int command_id
, int string_id
, int group_id
);
83 // Adds a separator of the specified type to the model.
84 // - Adding a separator after another separator is always invalid if they
85 // differ in type, but silently ignored if they are both NORMAL.
86 // - Adding a separator to an empty model is invalid, unless they are NORMAL
87 // or SPACING. NORMAL separators are silently ignored if the model is empty.
88 void AddSeparator(MenuSeparatorType separator_type
);
90 // Removes separators until the model's last entry is not a separator, or the
92 void RemoveTrailingSeparators();
94 // These three methods take pointers to various sub-models. These models
95 // should be owned by the same owner of this SimpleMenuModel.
96 void AddButtonItem(int command_id
, ButtonMenuItemModel
* model
);
97 void AddSubMenu(int command_id
,
98 const base::string16
& label
,
100 void AddSubMenuWithStringId(int command_id
, int string_id
, MenuModel
* model
);
102 // Methods for inserting items into the model.
103 void InsertItemAt(int index
, int command_id
, const base::string16
& label
);
104 void InsertItemWithStringIdAt(int index
, int command_id
, int string_id
);
105 void InsertSeparatorAt(int index
, MenuSeparatorType separator_type
);
106 void InsertCheckItemAt(int index
,
108 const base::string16
& label
);
109 void InsertCheckItemWithStringIdAt(int index
, int command_id
, int string_id
);
110 void InsertRadioItemAt(int index
,
112 const base::string16
& label
,
114 void InsertRadioItemWithStringIdAt(
115 int index
, int command_id
, int string_id
, int group_id
);
116 void InsertSubMenuAt(int index
,
118 const base::string16
& label
,
120 void InsertSubMenuWithStringIdAt(
121 int index
, int command_id
, int string_id
, MenuModel
* model
);
123 // Remove item at specified index from the model.
124 void RemoveItemAt(int index
);
126 // Sets the icon for the item at |index|.
127 void SetIcon(int index
, const gfx::Image
& icon
);
129 // Sets the sublabel for the item at |index|.
130 void SetSublabel(int index
, const base::string16
& sublabel
);
132 // Sets the minor text for the item at |index|.
133 void SetMinorText(int index
, const base::string16
& minor_text
);
135 // Clears all items. Note that it does not free MenuModel of submenu.
138 // Returns the index of the item that has the given |command_id|. Returns
140 int GetIndexOfCommandId(int command_id
);
142 // Overridden from MenuModel:
143 bool HasIcons() const override
;
144 int GetItemCount() const override
;
145 ItemType
GetTypeAt(int index
) const override
;
146 ui::MenuSeparatorType
GetSeparatorTypeAt(int index
) const override
;
147 int GetCommandIdAt(int index
) const override
;
148 base::string16
GetLabelAt(int index
) const override
;
149 base::string16
GetSublabelAt(int index
) const override
;
150 base::string16
GetMinorTextAt(int index
) const override
;
151 bool IsItemDynamicAt(int index
) const override
;
152 bool GetAcceleratorAt(int index
, ui::Accelerator
* accelerator
) const override
;
153 bool IsItemCheckedAt(int index
) const override
;
154 int GetGroupIdAt(int index
) const override
;
155 bool GetIconAt(int index
, gfx::Image
* icon
) override
;
156 ui::ButtonMenuItemModel
* GetButtonMenuItemAt(int index
) const override
;
157 bool IsEnabledAt(int index
) const override
;
158 bool IsVisibleAt(int index
) const override
;
159 void HighlightChangedTo(int index
) override
;
160 void ActivatedAt(int index
) override
;
161 void ActivatedAt(int index
, int event_flags
) override
;
162 MenuModel
* GetSubmenuModelAt(int index
) const override
;
163 void MenuWillShow() override
;
164 void MenuClosed() override
;
165 void SetMenuModelDelegate(
166 ui::MenuModelDelegate
* menu_model_delegate
) override
;
167 MenuModelDelegate
* GetMenuModelDelegate() const override
;
170 void set_delegate(Delegate
* delegate
) { delegate_
= delegate
; }
171 Delegate
* delegate() { return delegate_
; }
173 // One or more of the menu menu items associated with the model has changed.
174 // Do any handling if necessary.
175 virtual void MenuItemsChanged();
180 typedef std::vector
<Item
> ItemVector
;
183 int ValidateItemIndex(int index
) const;
185 // Functions for inserting items into |items_|.
186 void AppendItem(const Item
& item
);
187 void InsertItemAtIndex(const Item
& item
, int index
);
188 void ValidateItem(const Item
& item
);
190 // Notify the delegate that the menu is closed.
197 MenuModelDelegate
* menu_model_delegate_
;
199 base::WeakPtrFactory
<SimpleMenuModel
> method_factory_
;
201 DISALLOW_COPY_AND_ASSIGN(SimpleMenuModel
);
206 #endif // UI_BASE_MODELS_SIMPLE_MENU_MODEL_H_