| 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.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::Image 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 }; | 24 }; |
| 25 | 25 |
| 26 //////////////////////////////////////////////////////////////////////////////// | 26 //////////////////////////////////////////////////////////////////////////////// |
| 27 // SimpleMenuModel::Delegate, public: | 27 // SimpleMenuModel::Delegate, public: |
| 28 | 28 |
| 29 bool SimpleMenuModel::Delegate::IsCommandIdVisible(int command_id) const { | 29 bool SimpleMenuModel::Delegate::IsCommandIdVisible(int command_id) const { |
| 30 return true; | 30 return true; |
| 31 } | 31 } |
| 32 | 32 |
| 33 bool SimpleMenuModel::Delegate::IsItemForCommandIdDynamic( | 33 bool SimpleMenuModel::Delegate::IsItemForCommandIdDynamic( |
| 34 int command_id) const { | 34 int command_id) const { |
| 35 return false; | 35 return false; |
| 36 } | 36 } |
| 37 | 37 |
| 38 string16 SimpleMenuModel::Delegate::GetLabelForCommandId(int command_id) const { | 38 string16 SimpleMenuModel::Delegate::GetLabelForCommandId(int command_id) const { |
| 39 return string16(); | 39 return string16(); |
| 40 } | 40 } |
| 41 | 41 |
| 42 bool SimpleMenuModel::Delegate::GetIconForCommandId( | 42 bool SimpleMenuModel::Delegate::GetIconForCommandId( |
| 43 int command_id, gfx::ImageSkia* image_skia) const { | 43 int command_id, gfx::Image* image_skia) const { |
| 44 return false; | 44 return false; |
| 45 } | 45 } |
| 46 | 46 |
| 47 void SimpleMenuModel::Delegate::CommandIdHighlighted(int command_id) { | 47 void SimpleMenuModel::Delegate::CommandIdHighlighted(int command_id) { |
| 48 } | 48 } |
| 49 | 49 |
| 50 void SimpleMenuModel::Delegate::ExecuteCommand( | 50 void SimpleMenuModel::Delegate::ExecuteCommand( |
| 51 int command_id, int event_flags) { | 51 int command_id, int event_flags) { |
| 52 ExecuteCommand(command_id); | 52 ExecuteCommand(command_id); |
| 53 } | 53 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 64 SimpleMenuModel::SimpleMenuModel(Delegate* delegate) | 64 SimpleMenuModel::SimpleMenuModel(Delegate* delegate) |
| 65 : delegate_(delegate), | 65 : delegate_(delegate), |
| 66 menu_model_delegate_(NULL), | 66 menu_model_delegate_(NULL), |
| 67 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | 67 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
| 68 } | 68 } |
| 69 | 69 |
| 70 SimpleMenuModel::~SimpleMenuModel() { | 70 SimpleMenuModel::~SimpleMenuModel() { |
| 71 } | 71 } |
| 72 | 72 |
| 73 void SimpleMenuModel::AddItem(int command_id, const string16& label) { | 73 void SimpleMenuModel::AddItem(int command_id, const string16& label) { |
| 74 Item item = { command_id, label, gfx::ImageSkia(), TYPE_COMMAND, -1, NULL, | 74 Item item = { command_id, label, gfx::Image(), TYPE_COMMAND, -1, NULL, |
| 75 NULL }; | 75 NULL }; |
| 76 AppendItem(item); | 76 AppendItem(item); |
| 77 } | 77 } |
| 78 | 78 |
| 79 void SimpleMenuModel::AddItemWithStringId(int command_id, int string_id) { | 79 void SimpleMenuModel::AddItemWithStringId(int command_id, int string_id) { |
| 80 AddItem(command_id, l10n_util::GetStringUTF16(string_id)); | 80 AddItem(command_id, l10n_util::GetStringUTF16(string_id)); |
| 81 } | 81 } |
| 82 | 82 |
| 83 void SimpleMenuModel::AddSeparator() { | 83 void SimpleMenuModel::AddSeparator() { |
| 84 Item item = { kSeparatorId, string16(), gfx::ImageSkia(), TYPE_SEPARATOR, -1, | 84 Item item = { kSeparatorId, string16(), gfx::Image(), TYPE_SEPARATOR, -1, |
| 85 NULL, NULL }; | 85 NULL, NULL }; |
| 86 AppendItem(item); | 86 AppendItem(item); |
| 87 } | 87 } |
| 88 | 88 |
| 89 void SimpleMenuModel::AddCheckItem(int command_id, const string16& label) { | 89 void SimpleMenuModel::AddCheckItem(int command_id, const string16& label) { |
| 90 Item item = { command_id, label, gfx::ImageSkia(), TYPE_CHECK, -1, NULL, | 90 Item item = { command_id, label, gfx::Image(), TYPE_CHECK, -1, NULL, |
| 91 NULL }; | 91 NULL }; |
| 92 AppendItem(item); | 92 AppendItem(item); |
| 93 } | 93 } |
| 94 | 94 |
| 95 void SimpleMenuModel::AddCheckItemWithStringId(int command_id, int string_id) { | 95 void SimpleMenuModel::AddCheckItemWithStringId(int command_id, int string_id) { |
| 96 AddCheckItem(command_id, l10n_util::GetStringUTF16(string_id)); | 96 AddCheckItem(command_id, l10n_util::GetStringUTF16(string_id)); |
| 97 } | 97 } |
| 98 | 98 |
| 99 void SimpleMenuModel::AddRadioItem(int command_id, const string16& label, | 99 void SimpleMenuModel::AddRadioItem(int command_id, const string16& label, |
| 100 int group_id) { | 100 int group_id) { |
| 101 Item item = { command_id, label, gfx::ImageSkia(), TYPE_RADIO, group_id, NULL, | 101 Item item = { command_id, label, gfx::Image(), TYPE_RADIO, group_id, NULL, |
| 102 NULL }; | 102 NULL }; |
| 103 AppendItem(item); | 103 AppendItem(item); |
| 104 } | 104 } |
| 105 | 105 |
| 106 void SimpleMenuModel::AddRadioItemWithStringId(int command_id, int string_id, | 106 void SimpleMenuModel::AddRadioItemWithStringId(int command_id, int string_id, |
| 107 int group_id) { | 107 int group_id) { |
| 108 AddRadioItem(command_id, l10n_util::GetStringUTF16(string_id), group_id); | 108 AddRadioItem(command_id, l10n_util::GetStringUTF16(string_id), group_id); |
| 109 } | 109 } |
| 110 | 110 |
| 111 void SimpleMenuModel::AddButtonItem(int command_id, | 111 void SimpleMenuModel::AddButtonItem(int command_id, |
| 112 ButtonMenuItemModel* model) { | 112 ButtonMenuItemModel* model) { |
| 113 Item item = { command_id, string16(), gfx::ImageSkia(), TYPE_BUTTON_ITEM, -1, | 113 Item item = { command_id, string16(), gfx::Image(), TYPE_BUTTON_ITEM, -1, |
| 114 NULL, model }; | 114 NULL, model }; |
| 115 AppendItem(item); | 115 AppendItem(item); |
| 116 } | 116 } |
| 117 | 117 |
| 118 void SimpleMenuModel::AddSubMenu(int command_id, const string16& label, | 118 void SimpleMenuModel::AddSubMenu(int command_id, const string16& label, |
| 119 MenuModel* model) { | 119 MenuModel* model) { |
| 120 Item item = { command_id, label, gfx::ImageSkia(), TYPE_SUBMENU, -1, model, | 120 Item item = { command_id, label, gfx::Image(), TYPE_SUBMENU, -1, model, |
| 121 NULL }; | 121 NULL }; |
| 122 AppendItem(item); | 122 AppendItem(item); |
| 123 } | 123 } |
| 124 | 124 |
| 125 void SimpleMenuModel::AddSubMenuWithStringId(int command_id, | 125 void SimpleMenuModel::AddSubMenuWithStringId(int command_id, |
| 126 int string_id, MenuModel* model) { | 126 int string_id, MenuModel* model) { |
| 127 AddSubMenu(command_id, l10n_util::GetStringUTF16(string_id), model); | 127 AddSubMenu(command_id, l10n_util::GetStringUTF16(string_id), model); |
| 128 } | 128 } |
| 129 | 129 |
| 130 void SimpleMenuModel::InsertItemAt( | 130 void SimpleMenuModel::InsertItemAt( |
| 131 int index, int command_id, const string16& label) { | 131 int index, int command_id, const string16& label) { |
| 132 Item item = { command_id, label, gfx::ImageSkia(), TYPE_COMMAND, -1, NULL, | 132 Item item = { command_id, label, gfx::Image(), TYPE_COMMAND, -1, NULL, |
| 133 NULL }; | 133 NULL }; |
| 134 InsertItemAtIndex(item, index); | 134 InsertItemAtIndex(item, index); |
| 135 } | 135 } |
| 136 | 136 |
| 137 void SimpleMenuModel::InsertItemWithStringIdAt( | 137 void SimpleMenuModel::InsertItemWithStringIdAt( |
| 138 int index, int command_id, int string_id) { | 138 int index, int command_id, int string_id) { |
| 139 InsertItemAt(index, command_id, l10n_util::GetStringUTF16(string_id)); | 139 InsertItemAt(index, command_id, l10n_util::GetStringUTF16(string_id)); |
| 140 } | 140 } |
| 141 | 141 |
| 142 void SimpleMenuModel::InsertSeparatorAt(int index) { | 142 void SimpleMenuModel::InsertSeparatorAt(int index) { |
| 143 Item item = { kSeparatorId, string16(), gfx::ImageSkia(), TYPE_SEPARATOR, -1, | 143 Item item = { kSeparatorId, string16(), gfx::Image(), TYPE_SEPARATOR, -1, |
| 144 NULL, NULL }; | 144 NULL, NULL }; |
| 145 InsertItemAtIndex(item, index); | 145 InsertItemAtIndex(item, index); |
| 146 } | 146 } |
| 147 | 147 |
| 148 void SimpleMenuModel::InsertCheckItemAt( | 148 void SimpleMenuModel::InsertCheckItemAt( |
| 149 int index, int command_id, const string16& label) { | 149 int index, int command_id, const string16& label) { |
| 150 Item item = { command_id, label, gfx::ImageSkia(), TYPE_CHECK, -1, NULL, | 150 Item item = { command_id, label, gfx::Image(), TYPE_CHECK, -1, NULL, |
| 151 NULL }; | 151 NULL }; |
| 152 InsertItemAtIndex(item, index); | 152 InsertItemAtIndex(item, index); |
| 153 } | 153 } |
| 154 | 154 |
| 155 void SimpleMenuModel::InsertCheckItemWithStringIdAt( | 155 void SimpleMenuModel::InsertCheckItemWithStringIdAt( |
| 156 int index, int command_id, int string_id) { | 156 int index, int command_id, int string_id) { |
| 157 InsertCheckItemAt( | 157 InsertCheckItemAt( |
| 158 FlipIndex(index), command_id, l10n_util::GetStringUTF16(string_id)); | 158 FlipIndex(index), command_id, l10n_util::GetStringUTF16(string_id)); |
| 159 } | 159 } |
| 160 | 160 |
| 161 void SimpleMenuModel::InsertRadioItemAt( | 161 void SimpleMenuModel::InsertRadioItemAt( |
| 162 int index, int command_id, const string16& label, int group_id) { | 162 int index, int command_id, const string16& label, int group_id) { |
| 163 Item item = { command_id, label, gfx::ImageSkia(), TYPE_RADIO, group_id, NULL, | 163 Item item = { command_id, label, gfx::Image(), TYPE_RADIO, group_id, NULL, |
| 164 NULL }; | 164 NULL }; |
| 165 InsertItemAtIndex(item, index); | 165 InsertItemAtIndex(item, index); |
| 166 } | 166 } |
| 167 | 167 |
| 168 void SimpleMenuModel::InsertRadioItemWithStringIdAt( | 168 void SimpleMenuModel::InsertRadioItemWithStringIdAt( |
| 169 int index, int command_id, int string_id, int group_id) { | 169 int index, int command_id, int string_id, int group_id) { |
| 170 InsertRadioItemAt( | 170 InsertRadioItemAt( |
| 171 index, command_id, l10n_util::GetStringUTF16(string_id), group_id); | 171 index, command_id, l10n_util::GetStringUTF16(string_id), group_id); |
| 172 } | 172 } |
| 173 | 173 |
| 174 void SimpleMenuModel::InsertSubMenuAt( | 174 void SimpleMenuModel::InsertSubMenuAt( |
| 175 int index, int command_id, const string16& label, MenuModel* model) { | 175 int index, int command_id, const string16& label, MenuModel* model) { |
| 176 Item item = { command_id, label, gfx::ImageSkia(), TYPE_SUBMENU, -1, model, | 176 Item item = { command_id, label, gfx::Image(), TYPE_SUBMENU, -1, model, |
| 177 NULL }; | 177 NULL }; |
| 178 InsertItemAtIndex(item, index); | 178 InsertItemAtIndex(item, index); |
| 179 } | 179 } |
| 180 | 180 |
| 181 void SimpleMenuModel::InsertSubMenuWithStringIdAt( | 181 void SimpleMenuModel::InsertSubMenuWithStringIdAt( |
| 182 int index, int command_id, int string_id, MenuModel* model) { | 182 int index, int command_id, int string_id, MenuModel* model) { |
| 183 InsertSubMenuAt(index, command_id, l10n_util::GetStringUTF16(string_id), | 183 InsertSubMenuAt(index, command_id, l10n_util::GetStringUTF16(string_id), |
| 184 model); | 184 model); |
| 185 } | 185 } |
| 186 | 186 |
| 187 void SimpleMenuModel::SetIcon(int index, const gfx::ImageSkia& icon) { | 187 void SimpleMenuModel::SetIcon(int index, const gfx::Image& icon) { |
| 188 items_[ValidateItemIndex(index)].icon = icon; | 188 items_[ValidateItemIndex(index)].icon = icon; |
| 189 } | 189 } |
| 190 | 190 |
| 191 void SimpleMenuModel::Clear() { | 191 void SimpleMenuModel::Clear() { |
| 192 items_.clear(); | 192 items_.clear(); |
| 193 } | 193 } |
| 194 | 194 |
| 195 int SimpleMenuModel::GetIndexOfCommandId(int command_id) { | 195 int SimpleMenuModel::GetIndexOfCommandId(int command_id) { |
| 196 for (ItemVector::iterator i = items_.begin(); i != items_.end(); ++i) { | 196 for (ItemVector::iterator i = items_.begin(); i != items_.end(); ++i) { |
| 197 if (i->command_id == command_id) { | 197 if (i->command_id == command_id) { |
| 198 return FlipIndex(static_cast<int>(std::distance(items_.begin(), i))); | 198 return FlipIndex(static_cast<int>(std::distance(items_.begin(), i))); |
| 199 } | 199 } |
| 200 } | 200 } |
| 201 return -1; | 201 return -1; |
| 202 } | 202 } |
| 203 | 203 |
| 204 //////////////////////////////////////////////////////////////////////////////// | 204 //////////////////////////////////////////////////////////////////////////////// |
| 205 // SimpleMenuModel, MenuModel implementation: | 205 // SimpleMenuModel, MenuModel implementation: |
| 206 | 206 |
| 207 bool SimpleMenuModel::HasIcons() const { | 207 bool SimpleMenuModel::HasIcons() const { |
| 208 for (ItemVector::const_iterator i = items_.begin(); i != items_.end(); ++i) { | 208 for (ItemVector::const_iterator i = items_.begin(); i != items_.end(); ++i) { |
| 209 if (!i->icon.isNull()) | 209 if (!i->icon.IsEmpty()) |
| 210 return true; | 210 return true; |
| 211 } | 211 } |
| 212 | 212 |
| 213 return false; | 213 return false; |
| 214 } | 214 } |
| 215 | 215 |
| 216 int SimpleMenuModel::GetItemCount() const { | 216 int SimpleMenuModel::GetItemCount() const { |
| 217 return static_cast<int>(items_.size()); | 217 return static_cast<int>(items_.size()); |
| 218 } | 218 } |
| 219 | 219 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 return false; | 251 return false; |
| 252 MenuModel::ItemType item_type = GetTypeAt(index); | 252 MenuModel::ItemType item_type = GetTypeAt(index); |
| 253 return (item_type == TYPE_CHECK || item_type == TYPE_RADIO) ? | 253 return (item_type == TYPE_CHECK || item_type == TYPE_RADIO) ? |
| 254 delegate_->IsCommandIdChecked(GetCommandIdAt(index)) : false; | 254 delegate_->IsCommandIdChecked(GetCommandIdAt(index)) : false; |
| 255 } | 255 } |
| 256 | 256 |
| 257 int SimpleMenuModel::GetGroupIdAt(int index) const { | 257 int SimpleMenuModel::GetGroupIdAt(int index) const { |
| 258 return items_[ValidateItemIndex(FlipIndex(index))].group_id; | 258 return items_[ValidateItemIndex(FlipIndex(index))].group_id; |
| 259 } | 259 } |
| 260 | 260 |
| 261 bool SimpleMenuModel::GetIconAt(int index, gfx::ImageSkia* icon) { | 261 bool SimpleMenuModel::GetIconAt(int index, gfx::Image* icon) { |
| 262 if (IsItemDynamicAt(index)) | 262 if (IsItemDynamicAt(index)) |
| 263 return delegate_->GetIconForCommandId(GetCommandIdAt(index), icon); | 263 return delegate_->GetIconForCommandId(GetCommandIdAt(index), icon); |
| 264 | 264 |
| 265 ValidateItemIndex(index); | 265 ValidateItemIndex(index); |
| 266 if (items_[index].icon.isNull()) | 266 if (items_[index].icon.IsEmpty()) |
| 267 return false; | 267 return false; |
| 268 | 268 |
| 269 *icon = items_[index].icon; | 269 *icon = items_[index].icon; |
| 270 return true; | 270 return true; |
| 271 } | 271 } |
| 272 | 272 |
| 273 ButtonMenuItemModel* SimpleMenuModel::GetButtonMenuItemAt(int index) const { | 273 ButtonMenuItemModel* SimpleMenuModel::GetButtonMenuItemAt(int index) const { |
| 274 return items_[ValidateItemIndex(FlipIndex(index))].button_model; | 274 return items_[ValidateItemIndex(FlipIndex(index))].button_model; |
| 275 } | 275 } |
| 276 | 276 |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 #ifndef NDEBUG | 358 #ifndef NDEBUG |
| 359 if (item.type == TYPE_SEPARATOR) { | 359 if (item.type == TYPE_SEPARATOR) { |
| 360 DCHECK_EQ(item.command_id, kSeparatorId); | 360 DCHECK_EQ(item.command_id, kSeparatorId); |
| 361 } else { | 361 } else { |
| 362 DCHECK_GE(item.command_id, 0); | 362 DCHECK_GE(item.command_id, 0); |
| 363 } | 363 } |
| 364 #endif // NDEBUG | 364 #endif // NDEBUG |
| 365 } | 365 } |
| 366 | 366 |
| 367 } // namespace ui | 367 } // namespace ui |
| OLD | NEW |