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.
4
5 #include "ui/base/models/button_menu_item_model.h"
6
7 #include "ui/base/l10n/l10n_util.h"
8
9 namespace ui {
10
IsItemForCommandIdDynamic(int command_id) const11 bool ButtonMenuItemModel::Delegate::IsItemForCommandIdDynamic(
12 int command_id) const {
13 return false;
14 }
15
GetLabelForCommandId(int command_id) const16 base::string16 ButtonMenuItemModel::Delegate::GetLabelForCommandId(
17 int command_id) const {
18 return base::string16();
19 }
20
IsCommandIdEnabled(int command_id) const21 bool ButtonMenuItemModel::Delegate::IsCommandIdEnabled(int command_id) const {
22 return true;
23 }
24
DoesCommandIdDismissMenu(int command_id) const25 bool ButtonMenuItemModel::Delegate::DoesCommandIdDismissMenu(
26 int command_id) const {
27 return true;
28 }
29
30 struct ButtonMenuItemModel::Item {
31 int command_id;
32 ButtonType type;
33 base::string16 label;
34 int icon_idr;
35 bool part_of_group;
36 };
37
ButtonMenuItemModel(int string_id,ButtonMenuItemModel::Delegate * delegate)38 ButtonMenuItemModel::ButtonMenuItemModel(
39 int string_id,
40 ButtonMenuItemModel::Delegate* delegate)
41 : item_label_(l10n_util::GetStringUTF16(string_id)),
42 delegate_(delegate) {
43 }
44
~ButtonMenuItemModel()45 ButtonMenuItemModel::~ButtonMenuItemModel() {
46 }
47
AddGroupItemWithStringId(int command_id,int string_id)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);
53 }
54
AddItemWithImage(int command_id,int icon_idr)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);
59 }
60
AddButtonLabel(int command_id,int string_id)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);
65 }
66
AddSpace()67 void ButtonMenuItemModel::AddSpace() {
68 Item item = { 0, TYPE_SPACE, base::string16(), -1, false };
69 items_.push_back(item);
70 }
71
GetItemCount() const72 int ButtonMenuItemModel::GetItemCount() const {
73 return static_cast<int>(items_.size());
74 }
75
GetTypeAt(int index) const76 ButtonMenuItemModel::ButtonType ButtonMenuItemModel::GetTypeAt(
77 int index) const {
78 return items_[index].type;
79 }
80
GetCommandIdAt(int index) const81 int ButtonMenuItemModel::GetCommandIdAt(int index) const {
82 return items_[index].command_id;
83 }
84
IsItemDynamicAt(int index) const85 bool ButtonMenuItemModel::IsItemDynamicAt(int index) const {
86 if (delegate_)
87 return delegate_->IsItemForCommandIdDynamic(GetCommandIdAt(index));
88 return false;
89 }
90
GetLabelAt(int index) const91 base::string16 ButtonMenuItemModel::GetLabelAt(int index) const {
92 if (IsItemDynamicAt(index))
93 return delegate_->GetLabelForCommandId(GetCommandIdAt(index));
94 return items_[index].label;
95 }
96
GetIconAt(int index,int * icon_idr) const97 bool ButtonMenuItemModel::GetIconAt(int index, int* icon_idr) const {
98 if (items_[index].icon_idr == -1)
99 return false;
100
101 *icon_idr = items_[index].icon_idr;
102 return true;
103 }
104
PartOfGroup(int index) const105 bool ButtonMenuItemModel::PartOfGroup(int index) const {
106 return items_[index].part_of_group;
107 }
108
ActivatedCommand(int command_id)109 void ButtonMenuItemModel::ActivatedCommand(int command_id) {
110 if (delegate_)
111 delegate_->ExecuteCommand(command_id, 0);
112 }
113
IsEnabledAt(int index) const114 bool ButtonMenuItemModel::IsEnabledAt(int index) const {
115 return IsCommandIdEnabled(items_[index].command_id);
116 }
117
DismissesMenuAt(int index) const118 bool ButtonMenuItemModel::DismissesMenuAt(int index) const {
119 return DoesCommandIdDismissMenu(items_[index].command_id);
120 }
121
IsCommandIdEnabled(int command_id) const122 bool ButtonMenuItemModel::IsCommandIdEnabled(int command_id) const {
123 if (delegate_)
124 return delegate_->IsCommandIdEnabled(command_id);
125 return true;
126 }
127
DoesCommandIdDismissMenu(int command_id) const128 bool ButtonMenuItemModel::DoesCommandIdDismissMenu(int command_id) const {
129 if (delegate_)
130 return delegate_->DoesCommandIdDismissMenu(command_id);
131 return true;
132 }
133
134
135 } // namespace ui
136