Chromium Code Reviews| 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_skia.h" | 10 #include "ui/gfx/image/image_skia.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 gfx::ImageSkia icon; | 19 gfx::ImageSkia icon; |
| 20 ItemType type; | 20 ItemType type; |
| 21 int group_id; | 21 int group_id; |
| 22 MenuModel* submenu; | 22 MenuModel* submenu; |
| 23 ButtonMenuItemModel* button_model; | 23 ButtonMenuItemModel* button_model; |
| 24 MenuSeparatorType separator_type; | |
| 24 }; | 25 }; |
| 25 | 26 |
| 26 //////////////////////////////////////////////////////////////////////////////// | 27 //////////////////////////////////////////////////////////////////////////////// |
| 27 // SimpleMenuModel::Delegate, public: | 28 // SimpleMenuModel::Delegate, public: |
| 28 | 29 |
| 29 bool SimpleMenuModel::Delegate::IsCommandIdVisible(int command_id) const { | 30 bool SimpleMenuModel::Delegate::IsCommandIdVisible(int command_id) const { |
| 30 return true; | 31 return true; |
| 31 } | 32 } |
| 32 | 33 |
| 33 bool SimpleMenuModel::Delegate::IsItemForCommandIdDynamic( | 34 bool SimpleMenuModel::Delegate::IsItemForCommandIdDynamic( |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 65 : delegate_(delegate), | 66 : delegate_(delegate), |
| 66 menu_model_delegate_(NULL), | 67 menu_model_delegate_(NULL), |
| 67 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | 68 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
| 68 } | 69 } |
| 69 | 70 |
| 70 SimpleMenuModel::~SimpleMenuModel() { | 71 SimpleMenuModel::~SimpleMenuModel() { |
| 71 } | 72 } |
| 72 | 73 |
| 73 void SimpleMenuModel::AddItem(int command_id, const string16& label) { | 74 void SimpleMenuModel::AddItem(int command_id, const string16& label) { |
| 74 Item item = { command_id, label, gfx::ImageSkia(), TYPE_COMMAND, -1, NULL, | 75 Item item = { command_id, label, gfx::ImageSkia(), TYPE_COMMAND, -1, NULL, |
| 75 NULL }; | 76 NULL, NORMAL_SEPARATOR }; |
| 76 AppendItem(item); | 77 AppendItem(item); |
| 77 } | 78 } |
| 78 | 79 |
| 79 void SimpleMenuModel::AddItemWithStringId(int command_id, int string_id) { | 80 void SimpleMenuModel::AddItemWithStringId(int command_id, int string_id) { |
| 80 AddItem(command_id, l10n_util::GetStringUTF16(string_id)); | 81 AddItem(command_id, l10n_util::GetStringUTF16(string_id)); |
| 81 } | 82 } |
| 82 | 83 |
| 83 void SimpleMenuModel::AddSeparator() { | 84 void SimpleMenuModel::AddSeparator(MenuSeparatorType separator_type) { |
| 84 Item item = { kSeparatorId, string16(), gfx::ImageSkia(), TYPE_SEPARATOR, -1, | 85 #if defined(OS_WIN) |
|
sky
2012/08/21 19:36:29
See earlier comment, !USE_AURA
Mr4D (OOO till 08-26)
2012/08/21 21:34:54
Done, but - I am not sure that this is correct. To
sky
2012/08/21 22:35:23
This class is used every where and it seems as tho
| |
| 85 NULL, NULL }; | 86 if (separator_type != NORMAL_SEPARATOR) { |
| 87 NOTIMPLEMENTED(); | |
| 88 } | |
| 89 #endif | |
| 90 Item item = { kSeparatorId, string16(), gfx::ImageSkia(), TYPE_SEPARATOR, | |
| 91 -1, NULL, NULL , separator_type }; | |
| 86 AppendItem(item); | 92 AppendItem(item); |
| 87 } | 93 } |
| 88 | 94 |
| 89 void SimpleMenuModel::AddCheckItem(int command_id, const string16& label) { | 95 void SimpleMenuModel::AddCheckItem(int command_id, const string16& label) { |
| 90 Item item = { command_id, label, gfx::ImageSkia(), TYPE_CHECK, -1, NULL, | 96 Item item = { command_id, label, gfx::ImageSkia(), TYPE_CHECK, -1, NULL, |
| 91 NULL }; | 97 NULL, NORMAL_SEPARATOR }; |
| 92 AppendItem(item); | 98 AppendItem(item); |
| 93 } | 99 } |
| 94 | 100 |
| 95 void SimpleMenuModel::AddCheckItemWithStringId(int command_id, int string_id) { | 101 void SimpleMenuModel::AddCheckItemWithStringId(int command_id, int string_id) { |
| 96 AddCheckItem(command_id, l10n_util::GetStringUTF16(string_id)); | 102 AddCheckItem(command_id, l10n_util::GetStringUTF16(string_id)); |
| 97 } | 103 } |
| 98 | 104 |
| 99 void SimpleMenuModel::AddRadioItem(int command_id, const string16& label, | 105 void SimpleMenuModel::AddRadioItem(int command_id, const string16& label, |
| 100 int group_id) { | 106 int group_id) { |
| 101 Item item = { command_id, label, gfx::ImageSkia(), TYPE_RADIO, group_id, NULL, | 107 Item item = { command_id, label, gfx::ImageSkia(), TYPE_RADIO, group_id, NULL, |
| 102 NULL }; | 108 NULL, NORMAL_SEPARATOR }; |
| 103 AppendItem(item); | 109 AppendItem(item); |
| 104 } | 110 } |
| 105 | 111 |
| 106 void SimpleMenuModel::AddRadioItemWithStringId(int command_id, int string_id, | 112 void SimpleMenuModel::AddRadioItemWithStringId(int command_id, int string_id, |
| 107 int group_id) { | 113 int group_id) { |
| 108 AddRadioItem(command_id, l10n_util::GetStringUTF16(string_id), group_id); | 114 AddRadioItem(command_id, l10n_util::GetStringUTF16(string_id), group_id); |
| 109 } | 115 } |
| 110 | 116 |
| 111 void SimpleMenuModel::AddButtonItem(int command_id, | 117 void SimpleMenuModel::AddButtonItem(int command_id, |
| 112 ButtonMenuItemModel* model) { | 118 ButtonMenuItemModel* model) { |
| 113 Item item = { command_id, string16(), gfx::ImageSkia(), TYPE_BUTTON_ITEM, -1, | 119 Item item = { command_id, string16(), gfx::ImageSkia(), TYPE_BUTTON_ITEM, -1, |
| 114 NULL, model }; | 120 NULL, model, NORMAL_SEPARATOR }; |
| 115 AppendItem(item); | 121 AppendItem(item); |
| 116 } | 122 } |
| 117 | 123 |
| 118 void SimpleMenuModel::AddSubMenu(int command_id, const string16& label, | 124 void SimpleMenuModel::AddSubMenu(int command_id, const string16& label, |
| 119 MenuModel* model) { | 125 MenuModel* model) { |
| 120 Item item = { command_id, label, gfx::ImageSkia(), TYPE_SUBMENU, -1, model, | 126 Item item = { command_id, label, gfx::ImageSkia(), TYPE_SUBMENU, -1, model, |
| 121 NULL }; | 127 NULL, NORMAL_SEPARATOR }; |
| 122 AppendItem(item); | 128 AppendItem(item); |
| 123 } | 129 } |
| 124 | 130 |
| 125 void SimpleMenuModel::AddSubMenuWithStringId(int command_id, | 131 void SimpleMenuModel::AddSubMenuWithStringId(int command_id, |
| 126 int string_id, MenuModel* model) { | 132 int string_id, MenuModel* model) { |
| 127 AddSubMenu(command_id, l10n_util::GetStringUTF16(string_id), model); | 133 AddSubMenu(command_id, l10n_util::GetStringUTF16(string_id), model); |
| 128 } | 134 } |
| 129 | 135 |
| 130 void SimpleMenuModel::InsertItemAt( | 136 void SimpleMenuModel::InsertItemAt( |
| 131 int index, int command_id, const string16& label) { | 137 int index, int command_id, const string16& label) { |
| 132 Item item = { command_id, label, gfx::ImageSkia(), TYPE_COMMAND, -1, NULL, | 138 Item item = { command_id, label, gfx::ImageSkia(), TYPE_COMMAND, -1, NULL, |
| 133 NULL }; | 139 NULL, NORMAL_SEPARATOR }; |
| 134 InsertItemAtIndex(item, index); | 140 InsertItemAtIndex(item, index); |
| 135 } | 141 } |
| 136 | 142 |
| 137 void SimpleMenuModel::InsertItemWithStringIdAt( | 143 void SimpleMenuModel::InsertItemWithStringIdAt( |
| 138 int index, int command_id, int string_id) { | 144 int index, int command_id, int string_id) { |
| 139 InsertItemAt(index, command_id, l10n_util::GetStringUTF16(string_id)); | 145 InsertItemAt(index, command_id, l10n_util::GetStringUTF16(string_id)); |
| 140 } | 146 } |
| 141 | 147 |
| 142 void SimpleMenuModel::InsertSeparatorAt(int index) { | 148 void SimpleMenuModel::InsertSeparatorAt(int index, |
| 143 Item item = { kSeparatorId, string16(), gfx::ImageSkia(), TYPE_SEPARATOR, -1, | 149 MenuSeparatorType separator_type) { |
| 144 NULL, NULL }; | 150 #if defined(OS_WIN) |
|
sky
2012/08/21 19:36:29
Same thing here, !OS_AURA
Mr4D (OOO till 08-26)
2012/08/21 21:34:54
Done.
| |
| 151 if (separator_type != NORMAL_SEPARATOR) { | |
| 152 NOTIMPLEMENTED(); | |
| 153 } | |
| 154 #endif | |
| 155 Item item = { kSeparatorId, string16(), gfx::ImageSkia(), TYPE_SEPARATOR, | |
| 156 -1, NULL, NULL, separator_type }; | |
| 145 InsertItemAtIndex(item, index); | 157 InsertItemAtIndex(item, index); |
| 146 } | 158 } |
| 147 | 159 |
| 148 void SimpleMenuModel::InsertCheckItemAt( | 160 void SimpleMenuModel::InsertCheckItemAt( |
| 149 int index, int command_id, const string16& label) { | 161 int index, int command_id, const string16& label) { |
| 150 Item item = { command_id, label, gfx::ImageSkia(), TYPE_CHECK, -1, NULL, | 162 Item item = { command_id, label, gfx::ImageSkia(), TYPE_CHECK, -1, NULL, |
| 151 NULL }; | 163 NULL, NORMAL_SEPARATOR }; |
| 152 InsertItemAtIndex(item, index); | 164 InsertItemAtIndex(item, index); |
| 153 } | 165 } |
| 154 | 166 |
| 155 void SimpleMenuModel::InsertCheckItemWithStringIdAt( | 167 void SimpleMenuModel::InsertCheckItemWithStringIdAt( |
| 156 int index, int command_id, int string_id) { | 168 int index, int command_id, int string_id) { |
| 157 InsertCheckItemAt( | 169 InsertCheckItemAt( |
| 158 FlipIndex(index), command_id, l10n_util::GetStringUTF16(string_id)); | 170 FlipIndex(index), command_id, l10n_util::GetStringUTF16(string_id)); |
| 159 } | 171 } |
| 160 | 172 |
| 161 void SimpleMenuModel::InsertRadioItemAt( | 173 void SimpleMenuModel::InsertRadioItemAt( |
| 162 int index, int command_id, const string16& label, int group_id) { | 174 int index, int command_id, const string16& label, int group_id) { |
| 163 Item item = { command_id, label, gfx::ImageSkia(), TYPE_RADIO, group_id, NULL, | 175 Item item = { command_id, label, gfx::ImageSkia(), TYPE_RADIO, group_id, NULL, |
| 164 NULL }; | 176 NULL, NORMAL_SEPARATOR }; |
| 165 InsertItemAtIndex(item, index); | 177 InsertItemAtIndex(item, index); |
| 166 } | 178 } |
| 167 | 179 |
| 168 void SimpleMenuModel::InsertRadioItemWithStringIdAt( | 180 void SimpleMenuModel::InsertRadioItemWithStringIdAt( |
| 169 int index, int command_id, int string_id, int group_id) { | 181 int index, int command_id, int string_id, int group_id) { |
| 170 InsertRadioItemAt( | 182 InsertRadioItemAt( |
| 171 index, command_id, l10n_util::GetStringUTF16(string_id), group_id); | 183 index, command_id, l10n_util::GetStringUTF16(string_id), group_id); |
| 172 } | 184 } |
| 173 | 185 |
| 174 void SimpleMenuModel::InsertSubMenuAt( | 186 void SimpleMenuModel::InsertSubMenuAt( |
| 175 int index, int command_id, const string16& label, MenuModel* model) { | 187 int index, int command_id, const string16& label, MenuModel* model) { |
| 176 Item item = { command_id, label, gfx::ImageSkia(), TYPE_SUBMENU, -1, model, | 188 Item item = { command_id, label, gfx::ImageSkia(), TYPE_SUBMENU, -1, model, |
| 177 NULL }; | 189 NULL, NORMAL_SEPARATOR }; |
| 178 InsertItemAtIndex(item, index); | 190 InsertItemAtIndex(item, index); |
| 179 } | 191 } |
| 180 | 192 |
| 181 void SimpleMenuModel::InsertSubMenuWithStringIdAt( | 193 void SimpleMenuModel::InsertSubMenuWithStringIdAt( |
| 182 int index, int command_id, int string_id, MenuModel* model) { | 194 int index, int command_id, int string_id, MenuModel* model) { |
| 183 InsertSubMenuAt(index, command_id, l10n_util::GetStringUTF16(string_id), | 195 InsertSubMenuAt(index, command_id, l10n_util::GetStringUTF16(string_id), |
| 184 model); | 196 model); |
| 185 } | 197 } |
| 186 | 198 |
| 187 void SimpleMenuModel::SetIcon(int index, const gfx::ImageSkia& icon) { | 199 void SimpleMenuModel::SetIcon(int index, const gfx::ImageSkia& icon) { |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 214 } | 226 } |
| 215 | 227 |
| 216 int SimpleMenuModel::GetItemCount() const { | 228 int SimpleMenuModel::GetItemCount() const { |
| 217 return static_cast<int>(items_.size()); | 229 return static_cast<int>(items_.size()); |
| 218 } | 230 } |
| 219 | 231 |
| 220 MenuModel::ItemType SimpleMenuModel::GetTypeAt(int index) const { | 232 MenuModel::ItemType SimpleMenuModel::GetTypeAt(int index) const { |
| 221 return items_[ValidateItemIndex(FlipIndex(index))].type; | 233 return items_[ValidateItemIndex(FlipIndex(index))].type; |
| 222 } | 234 } |
| 223 | 235 |
| 236 ui::MenuSeparatorType SimpleMenuModel::GetSeparatorStyleAt(int index) const { | |
| 237 return items_[ValidateItemIndex(FlipIndex(index))].separator_type; | |
| 238 } | |
| 239 | |
| 224 int SimpleMenuModel::GetCommandIdAt(int index) const { | 240 int SimpleMenuModel::GetCommandIdAt(int index) const { |
| 225 return items_[ValidateItemIndex(FlipIndex(index))].command_id; | 241 return items_[ValidateItemIndex(FlipIndex(index))].command_id; |
| 226 } | 242 } |
| 227 | 243 |
| 228 string16 SimpleMenuModel::GetLabelAt(int index) const { | 244 string16 SimpleMenuModel::GetLabelAt(int index) const { |
| 229 if (IsItemDynamicAt(index)) | 245 if (IsItemDynamicAt(index)) |
| 230 return delegate_->GetLabelForCommandId(GetCommandIdAt(index)); | 246 return delegate_->GetLabelForCommandId(GetCommandIdAt(index)); |
| 231 return items_[ValidateItemIndex(FlipIndex(index))].label; | 247 return items_[ValidateItemIndex(FlipIndex(index))].label; |
| 232 } | 248 } |
| 233 | 249 |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 358 #ifndef NDEBUG | 374 #ifndef NDEBUG |
| 359 if (item.type == TYPE_SEPARATOR) { | 375 if (item.type == TYPE_SEPARATOR) { |
| 360 DCHECK_EQ(item.command_id, kSeparatorId); | 376 DCHECK_EQ(item.command_id, kSeparatorId); |
| 361 } else { | 377 } else { |
| 362 DCHECK_GE(item.command_id, 0); | 378 DCHECK_GE(item.command_id, 0); |
| 363 } | 379 } |
| 364 #endif // NDEBUG | 380 #endif // NDEBUG |
| 365 } | 381 } |
| 366 | 382 |
| 367 } // namespace ui | 383 } // namespace ui |
| OLD | NEW |