Roll src/third_party/WebKit 4526c29:30f3df0 (svn 190187:190189)
[chromium-blink-merge.git] / ui / base / models / button_menu_item_model.cc
blob83bd9737b62d52780903fb041bd10e13a2fb74bf
1 // Copyright (c) 2011 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 #include "ui/base/models/button_menu_item_model.h"
7 #include "ui/base/l10n/l10n_util.h"
9 namespace ui {
11 bool ButtonMenuItemModel::Delegate::IsItemForCommandIdDynamic(
12 int command_id) const {
13 return false;
16 base::string16 ButtonMenuItemModel::Delegate::GetLabelForCommandId(
17 int command_id) const {
18 return base::string16();
21 bool ButtonMenuItemModel::Delegate::IsCommandIdEnabled(int command_id) const {
22 return true;
25 bool ButtonMenuItemModel::Delegate::DoesCommandIdDismissMenu(
26 int command_id) const {
27 return true;
30 struct ButtonMenuItemModel::Item {
31 int command_id;
32 ButtonType type;
33 base::string16 label;
34 int icon_idr;
35 bool part_of_group;
38 ButtonMenuItemModel::ButtonMenuItemModel(
39 int string_id,
40 ButtonMenuItemModel::Delegate* delegate)
41 : item_label_(l10n_util::GetStringUTF16(string_id)),
42 delegate_(delegate) {
45 ButtonMenuItemModel::~ButtonMenuItemModel() {
48 void ButtonMenuItemModel::AddGroupItemWithStringId(
49 int command_id, int string_id) {
50 Item item = { command_id, TYPE_BUTTON, l10n_util::GetStringUTF16(string_id),
51 -1, true };
52 items_.push_back(item);
55 void ButtonMenuItemModel::AddItemWithImage(int command_id,
56 int icon_idr) {
57 Item item = { command_id, TYPE_BUTTON, base::string16(), icon_idr, false };
58 items_.push_back(item);
61 void ButtonMenuItemModel::AddButtonLabel(int command_id, int string_id) {
62 Item item = { command_id, TYPE_BUTTON_LABEL,
63 l10n_util::GetStringUTF16(string_id), -1, false };
64 items_.push_back(item);
67 void ButtonMenuItemModel::AddSpace() {
68 Item item = { 0, TYPE_SPACE, base::string16(), -1, false };
69 items_.push_back(item);
72 int ButtonMenuItemModel::GetItemCount() const {
73 return static_cast<int>(items_.size());
76 ButtonMenuItemModel::ButtonType ButtonMenuItemModel::GetTypeAt(
77 int index) const {
78 return items_[index].type;
81 int ButtonMenuItemModel::GetCommandIdAt(int index) const {
82 return items_[index].command_id;
85 bool ButtonMenuItemModel::IsItemDynamicAt(int index) const {
86 if (delegate_)
87 return delegate_->IsItemForCommandIdDynamic(GetCommandIdAt(index));
88 return false;
91 base::string16 ButtonMenuItemModel::GetLabelAt(int index) const {
92 if (IsItemDynamicAt(index))
93 return delegate_->GetLabelForCommandId(GetCommandIdAt(index));
94 return items_[index].label;
97 bool ButtonMenuItemModel::GetIconAt(int index, int* icon_idr) const {
98 if (items_[index].icon_idr == -1)
99 return false;
101 *icon_idr = items_[index].icon_idr;
102 return true;
105 bool ButtonMenuItemModel::PartOfGroup(int index) const {
106 return items_[index].part_of_group;
109 void ButtonMenuItemModel::ActivatedCommand(int command_id) {
110 if (delegate_)
111 delegate_->ExecuteCommand(command_id, 0);
114 bool ButtonMenuItemModel::IsEnabledAt(int index) const {
115 return IsCommandIdEnabled(items_[index].command_id);
118 bool ButtonMenuItemModel::DismissesMenuAt(int index) const {
119 return DoesCommandIdDismissMenu(items_[index].command_id);
122 bool ButtonMenuItemModel::IsCommandIdEnabled(int command_id) const {
123 if (delegate_)
124 return delegate_->IsCommandIdEnabled(command_id);
125 return true;
128 bool ButtonMenuItemModel::DoesCommandIdDismissMenu(int command_id) const {
129 if (delegate_)
130 return delegate_->DoesCommandIdDismissMenu(command_id);
131 return true;
135 } // namespace ui