OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/base/models/simple_menu_model.h" | 5 #include "ui/base/models/simple_menu_model.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "ui/base/l10n/l10n_util.h" | 9 #include "ui/base/l10n/l10n_util.h" |
10 #include "ui/gfx/image/image.h" | 10 #include "ui/gfx/image/image.h" |
11 | 11 |
12 namespace ui { | 12 namespace ui { |
13 | 13 |
14 const int kSeparatorId = -1; | 14 const int kSeparatorId = -1; |
15 | 15 |
16 struct SimpleMenuModel::Item { | 16 struct SimpleMenuModel::Item { |
17 int command_id; | 17 int command_id; |
18 string16 label; | 18 string16 label; |
| 19 string16 sublabel; |
19 gfx::Image icon; | 20 gfx::Image icon; |
20 ItemType type; | 21 ItemType type; |
21 int group_id; | 22 int group_id; |
22 MenuModel* submenu; | 23 MenuModel* submenu; |
23 ButtonMenuItemModel* button_model; | 24 ButtonMenuItemModel* button_model; |
24 MenuSeparatorType separator_type; | 25 MenuSeparatorType separator_type; |
25 }; | 26 }; |
26 | 27 |
27 //////////////////////////////////////////////////////////////////////////////// | 28 //////////////////////////////////////////////////////////////////////////////// |
28 // SimpleMenuModel::Delegate, public: | 29 // SimpleMenuModel::Delegate, public: |
29 | 30 |
30 bool SimpleMenuModel::Delegate::IsCommandIdVisible(int command_id) const { | 31 bool SimpleMenuModel::Delegate::IsCommandIdVisible(int command_id) const { |
31 return true; | 32 return true; |
32 } | 33 } |
33 | 34 |
34 bool SimpleMenuModel::Delegate::IsItemForCommandIdDynamic( | 35 bool SimpleMenuModel::Delegate::IsItemForCommandIdDynamic( |
35 int command_id) const { | 36 int command_id) const { |
36 return false; | 37 return false; |
37 } | 38 } |
38 | 39 |
39 string16 SimpleMenuModel::Delegate::GetLabelForCommandId(int command_id) const { | 40 string16 SimpleMenuModel::Delegate::GetLabelForCommandId(int command_id) const { |
40 return string16(); | 41 return string16(); |
41 } | 42 } |
42 | 43 |
| 44 string16 SimpleMenuModel::Delegate::GetSublabelForCommandId( |
| 45 int command_id) const { |
| 46 return string16(); |
| 47 } |
| 48 |
43 bool SimpleMenuModel::Delegate::GetIconForCommandId( | 49 bool SimpleMenuModel::Delegate::GetIconForCommandId( |
44 int command_id, gfx::Image* image_skia) const { | 50 int command_id, gfx::Image* image_skia) const { |
45 return false; | 51 return false; |
46 } | 52 } |
47 | 53 |
48 void SimpleMenuModel::Delegate::CommandIdHighlighted(int command_id) { | 54 void SimpleMenuModel::Delegate::CommandIdHighlighted(int command_id) { |
49 } | 55 } |
50 | 56 |
51 void SimpleMenuModel::Delegate::ExecuteCommand( | 57 void SimpleMenuModel::Delegate::ExecuteCommand( |
52 int command_id, int event_flags) { | 58 int command_id, int event_flags) { |
(...skipping 12 matching lines...) Expand all Loading... |
65 SimpleMenuModel::SimpleMenuModel(Delegate* delegate) | 71 SimpleMenuModel::SimpleMenuModel(Delegate* delegate) |
66 : delegate_(delegate), | 72 : delegate_(delegate), |
67 menu_model_delegate_(NULL), | 73 menu_model_delegate_(NULL), |
68 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | 74 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
69 } | 75 } |
70 | 76 |
71 SimpleMenuModel::~SimpleMenuModel() { | 77 SimpleMenuModel::~SimpleMenuModel() { |
72 } | 78 } |
73 | 79 |
74 void SimpleMenuModel::AddItem(int command_id, const string16& label) { | 80 void SimpleMenuModel::AddItem(int command_id, const string16& label) { |
75 Item item = { command_id, label, gfx::Image(), TYPE_COMMAND, -1, NULL, | 81 Item item = { command_id, label, string16(), gfx::Image(), |
76 NULL, NORMAL_SEPARATOR }; | 82 TYPE_COMMAND, -1, NULL, NULL, NORMAL_SEPARATOR }; |
77 AppendItem(item); | 83 AppendItem(item); |
78 } | 84 } |
79 | 85 |
80 void SimpleMenuModel::AddItemWithStringId(int command_id, int string_id) { | 86 void SimpleMenuModel::AddItemWithStringId(int command_id, int string_id) { |
81 AddItem(command_id, l10n_util::GetStringUTF16(string_id)); | 87 AddItem(command_id, l10n_util::GetStringUTF16(string_id)); |
82 } | 88 } |
83 | 89 |
84 void SimpleMenuModel::AddCheckItem(int command_id, const string16& label) { | 90 void SimpleMenuModel::AddCheckItem(int command_id, const string16& label) { |
85 Item item = { command_id, label, gfx::Image(), TYPE_CHECK, -1, NULL, | 91 Item item = { command_id, label, string16(), gfx::Image(), |
86 NULL, NORMAL_SEPARATOR }; | 92 TYPE_CHECK, -1, NULL, NULL, NORMAL_SEPARATOR }; |
87 AppendItem(item); | 93 AppendItem(item); |
88 } | 94 } |
89 | 95 |
90 void SimpleMenuModel::AddCheckItemWithStringId(int command_id, int string_id) { | 96 void SimpleMenuModel::AddCheckItemWithStringId(int command_id, int string_id) { |
91 AddCheckItem(command_id, l10n_util::GetStringUTF16(string_id)); | 97 AddCheckItem(command_id, l10n_util::GetStringUTF16(string_id)); |
92 } | 98 } |
93 | 99 |
94 void SimpleMenuModel::AddRadioItem(int command_id, const string16& label, | 100 void SimpleMenuModel::AddRadioItem(int command_id, const string16& label, |
95 int group_id) { | 101 int group_id) { |
96 Item item = { command_id, label, gfx::Image(), TYPE_RADIO, group_id, NULL, | 102 Item item = { command_id, label, string16(), gfx::Image(), |
97 NULL, NORMAL_SEPARATOR }; | 103 TYPE_RADIO, group_id, NULL, NULL, NORMAL_SEPARATOR }; |
98 AppendItem(item); | 104 AppendItem(item); |
99 } | 105 } |
100 | 106 |
101 void SimpleMenuModel::AddRadioItemWithStringId(int command_id, int string_id, | 107 void SimpleMenuModel::AddRadioItemWithStringId(int command_id, int string_id, |
102 int group_id) { | 108 int group_id) { |
103 AddRadioItem(command_id, l10n_util::GetStringUTF16(string_id), group_id); | 109 AddRadioItem(command_id, l10n_util::GetStringUTF16(string_id), group_id); |
104 } | 110 } |
105 | 111 |
106 void SimpleMenuModel::AddSeparator(MenuSeparatorType separator_type) { | 112 void SimpleMenuModel::AddSeparator(MenuSeparatorType separator_type) { |
107 if (items_.empty()) { | 113 if (items_.empty()) { |
108 if (separator_type == NORMAL_SEPARATOR) { | 114 if (separator_type == NORMAL_SEPARATOR) { |
109 return; | 115 return; |
110 } | 116 } |
111 DCHECK_EQ(SPACING_SEPARATOR, separator_type); | 117 DCHECK_EQ(SPACING_SEPARATOR, separator_type); |
112 } else if (items_.back().type == TYPE_SEPARATOR) { | 118 } else if (items_.back().type == TYPE_SEPARATOR) { |
113 DCHECK_EQ(NORMAL_SEPARATOR, separator_type); | 119 DCHECK_EQ(NORMAL_SEPARATOR, separator_type); |
114 DCHECK_EQ(NORMAL_SEPARATOR, items_.back().separator_type); | 120 DCHECK_EQ(NORMAL_SEPARATOR, items_.back().separator_type); |
115 return; | 121 return; |
116 } | 122 } |
117 #if !defined(USE_AURA) | 123 #if !defined(USE_AURA) |
118 if (separator_type != NORMAL_SEPARATOR) | 124 if (separator_type != NORMAL_SEPARATOR) |
119 NOTIMPLEMENTED(); | 125 NOTIMPLEMENTED(); |
120 #endif | 126 #endif |
121 Item item = { kSeparatorId, string16(), gfx::Image(), TYPE_SEPARATOR, | 127 Item item = { kSeparatorId, string16(), string16(), gfx::Image(), |
122 -1, NULL, NULL , separator_type }; | 128 TYPE_SEPARATOR, -1, NULL, NULL , separator_type }; |
123 AppendItem(item); | 129 AppendItem(item); |
124 } | 130 } |
125 | 131 |
126 void SimpleMenuModel::RemoveTrailingSeparators() { | 132 void SimpleMenuModel::RemoveTrailingSeparators() { |
127 while (!items_.empty() && items_.back().type == TYPE_SEPARATOR) | 133 while (!items_.empty() && items_.back().type == TYPE_SEPARATOR) |
128 items_.pop_back(); | 134 items_.pop_back(); |
129 } | 135 } |
130 | 136 |
131 void SimpleMenuModel::AddButtonItem(int command_id, | 137 void SimpleMenuModel::AddButtonItem(int command_id, |
132 ButtonMenuItemModel* model) { | 138 ButtonMenuItemModel* model) { |
133 Item item = { command_id, string16(), gfx::Image(), TYPE_BUTTON_ITEM, -1, | 139 Item item = { command_id, string16(), string16(), gfx::Image(), |
134 NULL, model, NORMAL_SEPARATOR }; | 140 TYPE_BUTTON_ITEM, -1, NULL, model, NORMAL_SEPARATOR }; |
135 AppendItem(item); | 141 AppendItem(item); |
136 } | 142 } |
137 | 143 |
138 void SimpleMenuModel::AddSubMenu(int command_id, const string16& label, | 144 void SimpleMenuModel::AddSubMenu(int command_id, const string16& label, |
139 MenuModel* model) { | 145 MenuModel* model) { |
140 Item item = { command_id, label, gfx::Image(), TYPE_SUBMENU, -1, model, | 146 Item item = { command_id, label, string16(), gfx::Image(), |
141 NULL, NORMAL_SEPARATOR }; | 147 TYPE_SUBMENU, -1, model, NULL, NORMAL_SEPARATOR }; |
142 AppendItem(item); | 148 AppendItem(item); |
143 } | 149 } |
144 | 150 |
145 void SimpleMenuModel::AddSubMenuWithStringId(int command_id, | 151 void SimpleMenuModel::AddSubMenuWithStringId(int command_id, |
146 int string_id, MenuModel* model) { | 152 int string_id, MenuModel* model) { |
147 AddSubMenu(command_id, l10n_util::GetStringUTF16(string_id), model); | 153 AddSubMenu(command_id, l10n_util::GetStringUTF16(string_id), model); |
148 } | 154 } |
149 | 155 |
150 void SimpleMenuModel::InsertItemAt( | 156 void SimpleMenuModel::InsertItemAt( |
151 int index, int command_id, const string16& label) { | 157 int index, int command_id, const string16& label) { |
152 Item item = { command_id, label, gfx::Image(), TYPE_COMMAND, -1, NULL, | 158 Item item = { command_id, label, string16(), gfx::Image(), |
153 NULL, NORMAL_SEPARATOR }; | 159 TYPE_COMMAND, -1, NULL, NULL, NORMAL_SEPARATOR }; |
154 InsertItemAtIndex(item, index); | 160 InsertItemAtIndex(item, index); |
155 } | 161 } |
156 | 162 |
157 void SimpleMenuModel::InsertItemWithStringIdAt( | 163 void SimpleMenuModel::InsertItemWithStringIdAt( |
158 int index, int command_id, int string_id) { | 164 int index, int command_id, int string_id) { |
159 InsertItemAt(index, command_id, l10n_util::GetStringUTF16(string_id)); | 165 InsertItemAt(index, command_id, l10n_util::GetStringUTF16(string_id)); |
160 } | 166 } |
161 | 167 |
162 void SimpleMenuModel::InsertSeparatorAt(int index, | 168 void SimpleMenuModel::InsertSeparatorAt(int index, |
163 MenuSeparatorType separator_type) { | 169 MenuSeparatorType separator_type) { |
164 #if !defined(USE_AURA) | 170 #if !defined(USE_AURA) |
165 if (separator_type != NORMAL_SEPARATOR) { | 171 if (separator_type != NORMAL_SEPARATOR) { |
166 NOTIMPLEMENTED(); | 172 NOTIMPLEMENTED(); |
167 } | 173 } |
168 #endif | 174 #endif |
169 Item item = { kSeparatorId, string16(), gfx::Image(), TYPE_SEPARATOR, | 175 Item item = { kSeparatorId, string16(), string16(), gfx::Image(), |
170 -1, NULL, NULL, separator_type }; | 176 TYPE_SEPARATOR, -1, NULL, NULL, separator_type }; |
171 InsertItemAtIndex(item, index); | 177 InsertItemAtIndex(item, index); |
172 } | 178 } |
173 | 179 |
174 void SimpleMenuModel::InsertCheckItemAt( | 180 void SimpleMenuModel::InsertCheckItemAt( |
175 int index, int command_id, const string16& label) { | 181 int index, int command_id, const string16& label) { |
176 Item item = { command_id, label, gfx::Image(), TYPE_CHECK, -1, NULL, | 182 Item item = { command_id, label, string16(), gfx::Image(), |
177 NULL, NORMAL_SEPARATOR }; | 183 TYPE_CHECK, -1, NULL, NULL, NORMAL_SEPARATOR }; |
178 InsertItemAtIndex(item, index); | 184 InsertItemAtIndex(item, index); |
179 } | 185 } |
180 | 186 |
181 void SimpleMenuModel::InsertCheckItemWithStringIdAt( | 187 void SimpleMenuModel::InsertCheckItemWithStringIdAt( |
182 int index, int command_id, int string_id) { | 188 int index, int command_id, int string_id) { |
183 InsertCheckItemAt(index, command_id, l10n_util::GetStringUTF16(string_id)); | 189 InsertCheckItemAt(index, command_id, l10n_util::GetStringUTF16(string_id)); |
184 } | 190 } |
185 | 191 |
186 void SimpleMenuModel::InsertRadioItemAt( | 192 void SimpleMenuModel::InsertRadioItemAt( |
187 int index, int command_id, const string16& label, int group_id) { | 193 int index, int command_id, const string16& label, int group_id) { |
188 Item item = { command_id, label, gfx::Image(), TYPE_RADIO, group_id, NULL, | 194 Item item = { command_id, label, string16(), gfx::Image(), |
189 NULL, NORMAL_SEPARATOR }; | 195 TYPE_RADIO, group_id, NULL, NULL, NORMAL_SEPARATOR }; |
190 InsertItemAtIndex(item, index); | 196 InsertItemAtIndex(item, index); |
191 } | 197 } |
192 | 198 |
193 void SimpleMenuModel::InsertRadioItemWithStringIdAt( | 199 void SimpleMenuModel::InsertRadioItemWithStringIdAt( |
194 int index, int command_id, int string_id, int group_id) { | 200 int index, int command_id, int string_id, int group_id) { |
195 InsertRadioItemAt( | 201 InsertRadioItemAt( |
196 index, command_id, l10n_util::GetStringUTF16(string_id), group_id); | 202 index, command_id, l10n_util::GetStringUTF16(string_id), group_id); |
197 } | 203 } |
198 | 204 |
199 void SimpleMenuModel::InsertSubMenuAt( | 205 void SimpleMenuModel::InsertSubMenuAt( |
200 int index, int command_id, const string16& label, MenuModel* model) { | 206 int index, int command_id, const string16& label, MenuModel* model) { |
201 Item item = { command_id, label, gfx::Image(), TYPE_SUBMENU, -1, model, | 207 Item item = { command_id, label, string16(), gfx::Image(), |
202 NULL, NORMAL_SEPARATOR }; | 208 TYPE_SUBMENU, -1, model, NULL, NORMAL_SEPARATOR }; |
203 InsertItemAtIndex(item, index); | 209 InsertItemAtIndex(item, index); |
204 } | 210 } |
205 | 211 |
206 void SimpleMenuModel::InsertSubMenuWithStringIdAt( | 212 void SimpleMenuModel::InsertSubMenuWithStringIdAt( |
207 int index, int command_id, int string_id, MenuModel* model) { | 213 int index, int command_id, int string_id, MenuModel* model) { |
208 InsertSubMenuAt(index, command_id, l10n_util::GetStringUTF16(string_id), | 214 InsertSubMenuAt(index, command_id, l10n_util::GetStringUTF16(string_id), |
209 model); | 215 model); |
210 } | 216 } |
211 | 217 |
212 void SimpleMenuModel::SetIcon(int index, const gfx::Image& icon) { | 218 void SimpleMenuModel::SetIcon(int index, const gfx::Image& icon) { |
213 items_[ValidateItemIndex(index)].icon = icon; | 219 items_[ValidateItemIndex(index)].icon = icon; |
214 } | 220 } |
215 | 221 |
| 222 void SimpleMenuModel::SetSublabel(int index, const string16& sublabel) { |
| 223 items_[ValidateItemIndex(index)].sublabel = sublabel; |
| 224 } |
| 225 |
216 void SimpleMenuModel::Clear() { | 226 void SimpleMenuModel::Clear() { |
217 items_.clear(); | 227 items_.clear(); |
218 } | 228 } |
219 | 229 |
220 int SimpleMenuModel::GetIndexOfCommandId(int command_id) { | 230 int SimpleMenuModel::GetIndexOfCommandId(int command_id) { |
221 for (ItemVector::iterator i = items_.begin(); i != items_.end(); ++i) { | 231 for (ItemVector::iterator i = items_.begin(); i != items_.end(); ++i) { |
222 if (i->command_id == command_id) | 232 if (i->command_id == command_id) |
223 return static_cast<int>(std::distance(items_.begin(), i)); | 233 return static_cast<int>(std::distance(items_.begin(), i)); |
224 } | 234 } |
225 return -1; | 235 return -1; |
(...skipping 26 matching lines...) Expand all Loading... |
252 int SimpleMenuModel::GetCommandIdAt(int index) const { | 262 int SimpleMenuModel::GetCommandIdAt(int index) const { |
253 return items_[ValidateItemIndex(index)].command_id; | 263 return items_[ValidateItemIndex(index)].command_id; |
254 } | 264 } |
255 | 265 |
256 string16 SimpleMenuModel::GetLabelAt(int index) const { | 266 string16 SimpleMenuModel::GetLabelAt(int index) const { |
257 if (IsItemDynamicAt(index)) | 267 if (IsItemDynamicAt(index)) |
258 return delegate_->GetLabelForCommandId(GetCommandIdAt(index)); | 268 return delegate_->GetLabelForCommandId(GetCommandIdAt(index)); |
259 return items_[ValidateItemIndex(index)].label; | 269 return items_[ValidateItemIndex(index)].label; |
260 } | 270 } |
261 | 271 |
| 272 string16 SimpleMenuModel::GetSublabelAt(int index) const { |
| 273 if (IsItemDynamicAt(index)) |
| 274 return delegate_->GetSublabelForCommandId(GetCommandIdAt(index)); |
| 275 return items_[ValidateItemIndex(index)].sublabel; |
| 276 } |
| 277 |
262 bool SimpleMenuModel::IsItemDynamicAt(int index) const { | 278 bool SimpleMenuModel::IsItemDynamicAt(int index) const { |
263 if (delegate_) | 279 if (delegate_) |
264 return delegate_->IsItemForCommandIdDynamic(GetCommandIdAt(index)); | 280 return delegate_->IsItemForCommandIdDynamic(GetCommandIdAt(index)); |
265 return false; | 281 return false; |
266 } | 282 } |
267 | 283 |
268 bool SimpleMenuModel::GetAcceleratorAt(int index, | 284 bool SimpleMenuModel::GetAcceleratorAt(int index, |
269 ui::Accelerator* accelerator) const { | 285 ui::Accelerator* accelerator) const { |
270 if (delegate_) { | 286 if (delegate_) { |
271 return delegate_->GetAcceleratorForCommandId(GetCommandIdAt(index), | 287 return delegate_->GetAcceleratorForCommandId(GetCommandIdAt(index), |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 #ifndef NDEBUG | 402 #ifndef NDEBUG |
387 if (item.type == TYPE_SEPARATOR) { | 403 if (item.type == TYPE_SEPARATOR) { |
388 DCHECK_EQ(item.command_id, kSeparatorId); | 404 DCHECK_EQ(item.command_id, kSeparatorId); |
389 } else { | 405 } else { |
390 DCHECK_GE(item.command_id, 0); | 406 DCHECK_GE(item.command_id, 0); |
391 } | 407 } |
392 #endif // NDEBUG | 408 #endif // NDEBUG |
393 } | 409 } |
394 | 410 |
395 } // namespace ui | 411 } // namespace ui |
OLD | NEW |