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" |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 | 166 |
167 void SimpleMenuModel::InsertCheckItemAt( | 167 void SimpleMenuModel::InsertCheckItemAt( |
168 int index, int command_id, const string16& label) { | 168 int index, int command_id, const string16& label) { |
169 Item item = { command_id, label, gfx::Image(), TYPE_CHECK, -1, NULL, | 169 Item item = { command_id, label, gfx::Image(), TYPE_CHECK, -1, NULL, |
170 NULL, NORMAL_SEPARATOR }; | 170 NULL, NORMAL_SEPARATOR }; |
171 InsertItemAtIndex(item, index); | 171 InsertItemAtIndex(item, index); |
172 } | 172 } |
173 | 173 |
174 void SimpleMenuModel::InsertCheckItemWithStringIdAt( | 174 void SimpleMenuModel::InsertCheckItemWithStringIdAt( |
175 int index, int command_id, int string_id) { | 175 int index, int command_id, int string_id) { |
176 InsertCheckItemAt( | 176 InsertCheckItemAt(index, command_id, l10n_util::GetStringUTF16(string_id)); |
177 FlipIndex(index), command_id, l10n_util::GetStringUTF16(string_id)); | |
178 } | 177 } |
179 | 178 |
180 void SimpleMenuModel::InsertRadioItemAt( | 179 void SimpleMenuModel::InsertRadioItemAt( |
181 int index, int command_id, const string16& label, int group_id) { | 180 int index, int command_id, const string16& label, int group_id) { |
182 Item item = { command_id, label, gfx::Image(), TYPE_RADIO, group_id, NULL, | 181 Item item = { command_id, label, gfx::Image(), TYPE_RADIO, group_id, NULL, |
183 NULL, NORMAL_SEPARATOR }; | 182 NULL, NORMAL_SEPARATOR }; |
184 InsertItemAtIndex(item, index); | 183 InsertItemAtIndex(item, index); |
185 } | 184 } |
186 | 185 |
187 void SimpleMenuModel::InsertRadioItemWithStringIdAt( | 186 void SimpleMenuModel::InsertRadioItemWithStringIdAt( |
(...skipping 18 matching lines...) Expand all Loading... |
206 void SimpleMenuModel::SetIcon(int index, const gfx::Image& icon) { | 205 void SimpleMenuModel::SetIcon(int index, const gfx::Image& icon) { |
207 items_[ValidateItemIndex(index)].icon = icon; | 206 items_[ValidateItemIndex(index)].icon = icon; |
208 } | 207 } |
209 | 208 |
210 void SimpleMenuModel::Clear() { | 209 void SimpleMenuModel::Clear() { |
211 items_.clear(); | 210 items_.clear(); |
212 } | 211 } |
213 | 212 |
214 int SimpleMenuModel::GetIndexOfCommandId(int command_id) { | 213 int SimpleMenuModel::GetIndexOfCommandId(int command_id) { |
215 for (ItemVector::iterator i = items_.begin(); i != items_.end(); ++i) { | 214 for (ItemVector::iterator i = items_.begin(); i != items_.end(); ++i) { |
216 if (i->command_id == command_id) { | 215 if (i->command_id == command_id) |
217 return FlipIndex(static_cast<int>(std::distance(items_.begin(), i))); | 216 return static_cast<int>(std::distance(items_.begin(), i)); |
218 } | |
219 } | 217 } |
220 return -1; | 218 return -1; |
221 } | 219 } |
222 | 220 |
223 //////////////////////////////////////////////////////////////////////////////// | 221 //////////////////////////////////////////////////////////////////////////////// |
224 // SimpleMenuModel, MenuModel implementation: | 222 // SimpleMenuModel, MenuModel implementation: |
225 | 223 |
226 bool SimpleMenuModel::HasIcons() const { | 224 bool SimpleMenuModel::HasIcons() const { |
227 for (ItemVector::const_iterator i = items_.begin(); i != items_.end(); ++i) { | 225 for (ItemVector::const_iterator i = items_.begin(); i != items_.end(); ++i) { |
228 if (!i->icon.IsEmpty()) | 226 if (!i->icon.IsEmpty()) |
229 return true; | 227 return true; |
230 } | 228 } |
231 | 229 |
232 return false; | 230 return false; |
233 } | 231 } |
234 | 232 |
235 int SimpleMenuModel::GetItemCount() const { | 233 int SimpleMenuModel::GetItemCount() const { |
236 return static_cast<int>(items_.size()); | 234 return static_cast<int>(items_.size()); |
237 } | 235 } |
238 | 236 |
239 MenuModel::ItemType SimpleMenuModel::GetTypeAt(int index) const { | 237 MenuModel::ItemType SimpleMenuModel::GetTypeAt(int index) const { |
240 return items_[ValidateItemIndex(FlipIndex(index))].type; | 238 return items_[ValidateItemIndex(index)].type; |
241 } | 239 } |
242 | 240 |
243 ui::MenuSeparatorType SimpleMenuModel::GetSeparatorTypeAt(int index) const { | 241 ui::MenuSeparatorType SimpleMenuModel::GetSeparatorTypeAt(int index) const { |
244 return items_[ValidateItemIndex(FlipIndex(index))].separator_type; | 242 return items_[ValidateItemIndex(index)].separator_type; |
245 } | 243 } |
246 | 244 |
247 int SimpleMenuModel::GetCommandIdAt(int index) const { | 245 int SimpleMenuModel::GetCommandIdAt(int index) const { |
248 return items_[ValidateItemIndex(FlipIndex(index))].command_id; | 246 return items_[ValidateItemIndex(index)].command_id; |
249 } | 247 } |
250 | 248 |
251 string16 SimpleMenuModel::GetLabelAt(int index) const { | 249 string16 SimpleMenuModel::GetLabelAt(int index) const { |
252 if (IsItemDynamicAt(index)) | 250 if (IsItemDynamicAt(index)) |
253 return delegate_->GetLabelForCommandId(GetCommandIdAt(index)); | 251 return delegate_->GetLabelForCommandId(GetCommandIdAt(index)); |
254 return items_[ValidateItemIndex(FlipIndex(index))].label; | 252 return items_[ValidateItemIndex(index)].label; |
255 } | 253 } |
256 | 254 |
257 bool SimpleMenuModel::IsItemDynamicAt(int index) const { | 255 bool SimpleMenuModel::IsItemDynamicAt(int index) const { |
258 if (delegate_) | 256 if (delegate_) |
259 return delegate_->IsItemForCommandIdDynamic(GetCommandIdAt(index)); | 257 return delegate_->IsItemForCommandIdDynamic(GetCommandIdAt(index)); |
260 return false; | 258 return false; |
261 } | 259 } |
262 | 260 |
263 bool SimpleMenuModel::GetAcceleratorAt(int index, | 261 bool SimpleMenuModel::GetAcceleratorAt(int index, |
264 ui::Accelerator* accelerator) const { | 262 ui::Accelerator* accelerator) const { |
265 if (delegate_) { | 263 if (delegate_) { |
266 return delegate_->GetAcceleratorForCommandId(GetCommandIdAt(index), | 264 return delegate_->GetAcceleratorForCommandId(GetCommandIdAt(index), |
267 accelerator); | 265 accelerator); |
268 } | 266 } |
269 return false; | 267 return false; |
270 } | 268 } |
271 | 269 |
272 bool SimpleMenuModel::IsItemCheckedAt(int index) const { | 270 bool SimpleMenuModel::IsItemCheckedAt(int index) const { |
273 if (!delegate_) | 271 if (!delegate_) |
274 return false; | 272 return false; |
275 MenuModel::ItemType item_type = GetTypeAt(index); | 273 MenuModel::ItemType item_type = GetTypeAt(index); |
276 return (item_type == TYPE_CHECK || item_type == TYPE_RADIO) ? | 274 return (item_type == TYPE_CHECK || item_type == TYPE_RADIO) ? |
277 delegate_->IsCommandIdChecked(GetCommandIdAt(index)) : false; | 275 delegate_->IsCommandIdChecked(GetCommandIdAt(index)) : false; |
278 } | 276 } |
279 | 277 |
280 int SimpleMenuModel::GetGroupIdAt(int index) const { | 278 int SimpleMenuModel::GetGroupIdAt(int index) const { |
281 return items_[ValidateItemIndex(FlipIndex(index))].group_id; | 279 return items_[ValidateItemIndex(index)].group_id; |
282 } | 280 } |
283 | 281 |
284 bool SimpleMenuModel::GetIconAt(int index, gfx::Image* icon) { | 282 bool SimpleMenuModel::GetIconAt(int index, gfx::Image* icon) { |
285 if (IsItemDynamicAt(index)) | 283 if (IsItemDynamicAt(index)) |
286 return delegate_->GetIconForCommandId(GetCommandIdAt(index), icon); | 284 return delegate_->GetIconForCommandId(GetCommandIdAt(index), icon); |
287 | 285 |
288 ValidateItemIndex(index); | 286 ValidateItemIndex(index); |
289 if (items_[index].icon.IsEmpty()) | 287 if (items_[index].icon.IsEmpty()) |
290 return false; | 288 return false; |
291 | 289 |
292 *icon = items_[index].icon; | 290 *icon = items_[index].icon; |
293 return true; | 291 return true; |
294 } | 292 } |
295 | 293 |
296 ButtonMenuItemModel* SimpleMenuModel::GetButtonMenuItemAt(int index) const { | 294 ButtonMenuItemModel* SimpleMenuModel::GetButtonMenuItemAt(int index) const { |
297 return items_[ValidateItemIndex(FlipIndex(index))].button_model; | 295 return items_[ValidateItemIndex(index)].button_model; |
298 } | 296 } |
299 | 297 |
300 bool SimpleMenuModel::IsEnabledAt(int index) const { | 298 bool SimpleMenuModel::IsEnabledAt(int index) const { |
301 int command_id = GetCommandIdAt(index); | 299 int command_id = GetCommandIdAt(index); |
302 if (!delegate_ || command_id == kSeparatorId || GetButtonMenuItemAt(index)) | 300 if (!delegate_ || command_id == kSeparatorId || GetButtonMenuItemAt(index)) |
303 return true; | 301 return true; |
304 return delegate_->IsCommandIdEnabled(command_id); | 302 return delegate_->IsCommandIdEnabled(command_id); |
305 } | 303 } |
306 | 304 |
307 bool SimpleMenuModel::IsVisibleAt(int index) const { | 305 bool SimpleMenuModel::IsVisibleAt(int index) const { |
(...skipping 12 matching lines...) Expand all Loading... |
320 if (delegate_) | 318 if (delegate_) |
321 delegate_->ExecuteCommand(GetCommandIdAt(index)); | 319 delegate_->ExecuteCommand(GetCommandIdAt(index)); |
322 } | 320 } |
323 | 321 |
324 void SimpleMenuModel::ActivatedAt(int index, int event_flags) { | 322 void SimpleMenuModel::ActivatedAt(int index, int event_flags) { |
325 if (delegate_) | 323 if (delegate_) |
326 delegate_->ExecuteCommand(GetCommandIdAt(index), event_flags); | 324 delegate_->ExecuteCommand(GetCommandIdAt(index), event_flags); |
327 } | 325 } |
328 | 326 |
329 MenuModel* SimpleMenuModel::GetSubmenuModelAt(int index) const { | 327 MenuModel* SimpleMenuModel::GetSubmenuModelAt(int index) const { |
330 return items_[ValidateItemIndex(FlipIndex(index))].submenu; | 328 return items_[ValidateItemIndex(index)].submenu; |
331 } | 329 } |
332 | 330 |
333 void SimpleMenuModel::MenuWillShow() { | 331 void SimpleMenuModel::MenuWillShow() { |
334 if (delegate_) | 332 if (delegate_) |
335 delegate_->MenuWillShow(this); | 333 delegate_->MenuWillShow(this); |
336 } | 334 } |
337 | 335 |
338 void SimpleMenuModel::MenuClosed() { | 336 void SimpleMenuModel::MenuClosed() { |
339 // Due to how menus work on the different platforms, ActivatedAt will be | 337 // Due to how menus work on the different platforms, ActivatedAt will be |
340 // called after this. It's more convenient for the delegate to be called | 338 // called after this. It's more convenient for the delegate to be called |
(...skipping 10 matching lines...) Expand all Loading... |
351 | 349 |
352 MenuModelDelegate* SimpleMenuModel::GetMenuModelDelegate() const { | 350 MenuModelDelegate* SimpleMenuModel::GetMenuModelDelegate() const { |
353 return menu_model_delegate_; | 351 return menu_model_delegate_; |
354 } | 352 } |
355 | 353 |
356 void SimpleMenuModel::OnMenuClosed() { | 354 void SimpleMenuModel::OnMenuClosed() { |
357 if (delegate_) | 355 if (delegate_) |
358 delegate_->MenuClosed(this); | 356 delegate_->MenuClosed(this); |
359 } | 357 } |
360 | 358 |
361 int SimpleMenuModel::FlipIndex(int index) const { | |
362 return index; | |
363 } | |
364 | |
365 //////////////////////////////////////////////////////////////////////////////// | 359 //////////////////////////////////////////////////////////////////////////////// |
366 // SimpleMenuModel, Private: | 360 // SimpleMenuModel, Private: |
367 | 361 |
368 int SimpleMenuModel::ValidateItemIndex(int index) const { | 362 int SimpleMenuModel::ValidateItemIndex(int index) const { |
369 CHECK_GE(index, 0); | 363 CHECK_GE(index, 0); |
370 CHECK_LT(static_cast<size_t>(index), items_.size()); | 364 CHECK_LT(static_cast<size_t>(index), items_.size()); |
371 return index; | 365 return index; |
372 } | 366 } |
373 | 367 |
374 void SimpleMenuModel::AppendItem(const Item& item) { | 368 void SimpleMenuModel::AppendItem(const Item& item) { |
375 ValidateItem(item); | 369 ValidateItem(item); |
376 items_.push_back(item); | 370 items_.push_back(item); |
377 } | 371 } |
378 | 372 |
379 void SimpleMenuModel::InsertItemAtIndex(const Item& item, int index) { | 373 void SimpleMenuModel::InsertItemAtIndex(const Item& item, int index) { |
380 ValidateItem(item); | 374 ValidateItem(item); |
381 items_.insert(items_.begin() + FlipIndex(index), item); | 375 items_.insert(items_.begin() + index, item); |
382 } | 376 } |
383 | 377 |
384 void SimpleMenuModel::ValidateItem(const Item& item) { | 378 void SimpleMenuModel::ValidateItem(const Item& item) { |
385 #ifndef NDEBUG | 379 #ifndef NDEBUG |
386 if (item.type == TYPE_SEPARATOR) { | 380 if (item.type == TYPE_SEPARATOR) { |
387 DCHECK_EQ(item.command_id, kSeparatorId); | 381 DCHECK_EQ(item.command_id, kSeparatorId); |
388 } else { | 382 } else { |
389 DCHECK_GE(item.command_id, 0); | 383 DCHECK_GE(item.command_id, 0); |
390 } | 384 } |
391 #endif // NDEBUG | 385 #endif // NDEBUG |
392 } | 386 } |
393 | 387 |
394 } // namespace ui | 388 } // namespace ui |
OLD | NEW |