Update V8 to version 4.7.45.
[chromium-blink-merge.git] / ui / base / models / simple_menu_model.h
blob9691d3baab60cb86c6ae81cd2d5f36f43da80b33
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_
8 #include <vector>
10 #include "base/memory/weak_ptr.h"
11 #include "base/strings/string16.h"
12 #include "ui/base/models/menu_model.h"
14 namespace gfx {
15 class Image;
18 namespace ui {
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 {
27 public:
28 class UI_BASE_EXPORT Delegate {
29 public:
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(
40 int command_id,
41 ui::Accelerator* accelerator) = 0;
43 // Some command ids have labels, sublabels, minor text and icons that change
44 // over time.
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
71 // disabled.
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 // These three methods take pointers to various sub-models. These models
91 // should be owned by the same owner of this SimpleMenuModel.
92 void AddButtonItem(int command_id, ButtonMenuItemModel* model);
93 void AddSubMenu(int command_id,
94 const base::string16& label,
95 MenuModel* model);
96 void AddSubMenuWithStringId(int command_id, int string_id, MenuModel* model);
98 // Methods for inserting items into the model.
99 void InsertItemAt(int index, int command_id, const base::string16& label);
100 void InsertItemWithStringIdAt(int index, int command_id, int string_id);
101 void InsertSeparatorAt(int index, MenuSeparatorType separator_type);
102 void InsertCheckItemAt(int index,
103 int command_id,
104 const base::string16& label);
105 void InsertCheckItemWithStringIdAt(int index, int command_id, int string_id);
106 void InsertRadioItemAt(int index,
107 int command_id,
108 const base::string16& label,
109 int group_id);
110 void InsertRadioItemWithStringIdAt(
111 int index, int command_id, int string_id, int group_id);
112 void InsertSubMenuAt(int index,
113 int command_id,
114 const base::string16& label,
115 MenuModel* model);
116 void InsertSubMenuWithStringIdAt(
117 int index, int command_id, int string_id, MenuModel* model);
119 // Remove item at specified index from the model.
120 void RemoveItemAt(int index);
122 // Sets the icon for the item at |index|.
123 void SetIcon(int index, const gfx::Image& icon);
125 // Sets the sublabel for the item at |index|.
126 void SetSublabel(int index, const base::string16& sublabel);
128 // Sets the minor text for the item at |index|.
129 void SetMinorText(int index, const base::string16& minor_text);
131 // Clears all items. Note that it does not free MenuModel of submenu.
132 void Clear();
134 // Returns the index of the item that has the given |command_id|. Returns
135 // -1 if not found.
136 int GetIndexOfCommandId(int command_id);
138 // Overridden from MenuModel:
139 bool HasIcons() const override;
140 int GetItemCount() const override;
141 ItemType GetTypeAt(int index) const override;
142 ui::MenuSeparatorType GetSeparatorTypeAt(int index) const override;
143 int GetCommandIdAt(int index) const override;
144 base::string16 GetLabelAt(int index) const override;
145 base::string16 GetSublabelAt(int index) const override;
146 base::string16 GetMinorTextAt(int index) const override;
147 bool IsItemDynamicAt(int index) const override;
148 bool GetAcceleratorAt(int index, ui::Accelerator* accelerator) const override;
149 bool IsItemCheckedAt(int index) const override;
150 int GetGroupIdAt(int index) const override;
151 bool GetIconAt(int index, gfx::Image* icon) override;
152 ui::ButtonMenuItemModel* GetButtonMenuItemAt(int index) const override;
153 bool IsEnabledAt(int index) const override;
154 bool IsVisibleAt(int index) const override;
155 void HighlightChangedTo(int index) override;
156 void ActivatedAt(int index) override;
157 void ActivatedAt(int index, int event_flags) override;
158 MenuModel* GetSubmenuModelAt(int index) const override;
159 void MenuWillShow() override;
160 void MenuClosed() override;
161 void SetMenuModelDelegate(
162 ui::MenuModelDelegate* menu_model_delegate) override;
163 MenuModelDelegate* GetMenuModelDelegate() const override;
165 protected:
166 void set_delegate(Delegate* delegate) { delegate_ = delegate; }
167 Delegate* delegate() { return delegate_; }
169 // One or more of the menu menu items associated with the model has changed.
170 // Do any handling if necessary.
171 virtual void MenuItemsChanged();
173 private:
174 struct Item;
176 typedef std::vector<Item> ItemVector;
178 // Returns |index|.
179 int ValidateItemIndex(int index) const;
181 // Functions for inserting items into |items_|.
182 void AppendItem(const Item& item);
183 void InsertItemAtIndex(const Item& item, int index);
184 void ValidateItem(const Item& item);
186 // Notify the delegate that the menu is closed.
187 void OnMenuClosed();
189 ItemVector items_;
191 Delegate* delegate_;
193 MenuModelDelegate* menu_model_delegate_;
195 base::WeakPtrFactory<SimpleMenuModel> method_factory_;
197 DISALLOW_COPY_AND_ASSIGN(SimpleMenuModel);
200 } // namespace ui
202 #endif // UI_BASE_MODELS_SIMPLE_MENU_MODEL_H_