| 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 "third_party/skia/include/core/SkBitmap.h" | |
| 10 #include "ui/base/l10n/l10n_util.h" | 9 #include "ui/base/l10n/l10n_util.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 SkBitmap 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 }; | 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, SkBitmap* bitmap) const { | 43 int command_id, gfx::ImageSkia* 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, SkBitmap(), TYPE_COMMAND, -1, NULL, NULL }; | 74 Item item = { command_id, label, gfx::ImageSkia(), TYPE_COMMAND, -1, NULL, |
| 75 NULL }; |
| 75 AppendItem(item); | 76 AppendItem(item); |
| 76 } | 77 } |
| 77 | 78 |
| 78 void SimpleMenuModel::AddItemWithStringId(int command_id, int string_id) { | 79 void SimpleMenuModel::AddItemWithStringId(int command_id, int string_id) { |
| 79 AddItem(command_id, l10n_util::GetStringUTF16(string_id)); | 80 AddItem(command_id, l10n_util::GetStringUTF16(string_id)); |
| 80 } | 81 } |
| 81 | 82 |
| 82 void SimpleMenuModel::AddSeparator() { | 83 void SimpleMenuModel::AddSeparator() { |
| 83 Item item = { kSeparatorId, string16(), SkBitmap(), TYPE_SEPARATOR, -1, | 84 Item item = { kSeparatorId, string16(), gfx::ImageSkia(), TYPE_SEPARATOR, -1, |
| 84 NULL, NULL }; | 85 NULL, NULL }; |
| 85 AppendItem(item); | 86 AppendItem(item); |
| 86 } | 87 } |
| 87 | 88 |
| 88 void SimpleMenuModel::AddCheckItem(int command_id, const string16& label) { | 89 void SimpleMenuModel::AddCheckItem(int command_id, const string16& label) { |
| 89 Item item = { command_id, label, SkBitmap(), TYPE_CHECK, -1, NULL, NULL }; | 90 Item item = { command_id, label, gfx::ImageSkia(), TYPE_CHECK, -1, NULL, |
| 91 NULL }; |
| 90 AppendItem(item); | 92 AppendItem(item); |
| 91 } | 93 } |
| 92 | 94 |
| 93 void SimpleMenuModel::AddCheckItemWithStringId(int command_id, int string_id) { | 95 void SimpleMenuModel::AddCheckItemWithStringId(int command_id, int string_id) { |
| 94 AddCheckItem(command_id, l10n_util::GetStringUTF16(string_id)); | 96 AddCheckItem(command_id, l10n_util::GetStringUTF16(string_id)); |
| 95 } | 97 } |
| 96 | 98 |
| 97 void SimpleMenuModel::AddRadioItem(int command_id, const string16& label, | 99 void SimpleMenuModel::AddRadioItem(int command_id, const string16& label, |
| 98 int group_id) { | 100 int group_id) { |
| 99 Item item = { command_id, label, SkBitmap(), TYPE_RADIO, group_id, NULL, | 101 Item item = { command_id, label, gfx::ImageSkia(), TYPE_RADIO, group_id, NULL, |
| 100 NULL }; | 102 NULL }; |
| 101 AppendItem(item); | 103 AppendItem(item); |
| 102 } | 104 } |
| 103 | 105 |
| 104 void SimpleMenuModel::AddRadioItemWithStringId(int command_id, int string_id, | 106 void SimpleMenuModel::AddRadioItemWithStringId(int command_id, int string_id, |
| 105 int group_id) { | 107 int group_id) { |
| 106 AddRadioItem(command_id, l10n_util::GetStringUTF16(string_id), group_id); | 108 AddRadioItem(command_id, l10n_util::GetStringUTF16(string_id), group_id); |
| 107 } | 109 } |
| 108 | 110 |
| 109 void SimpleMenuModel::AddButtonItem(int command_id, | 111 void SimpleMenuModel::AddButtonItem(int command_id, |
| 110 ButtonMenuItemModel* model) { | 112 ButtonMenuItemModel* model) { |
| 111 Item item = { command_id, string16(), SkBitmap(), TYPE_BUTTON_ITEM, -1, NULL, | 113 Item item = { command_id, string16(), gfx::ImageSkia(), TYPE_BUTTON_ITEM, -1, |
| 112 model }; | 114 NULL, model }; |
| 113 AppendItem(item); | 115 AppendItem(item); |
| 114 } | 116 } |
| 115 | 117 |
| 116 void SimpleMenuModel::AddSubMenu(int command_id, const string16& label, | 118 void SimpleMenuModel::AddSubMenu(int command_id, const string16& label, |
| 117 MenuModel* model) { | 119 MenuModel* model) { |
| 118 Item item = { command_id, label, SkBitmap(), TYPE_SUBMENU, -1, model, NULL }; | 120 Item item = { command_id, label, gfx::ImageSkia(), TYPE_SUBMENU, -1, model, |
| 121 NULL }; |
| 119 AppendItem(item); | 122 AppendItem(item); |
| 120 } | 123 } |
| 121 | 124 |
| 122 void SimpleMenuModel::AddSubMenuWithStringId(int command_id, | 125 void SimpleMenuModel::AddSubMenuWithStringId(int command_id, |
| 123 int string_id, MenuModel* model) { | 126 int string_id, MenuModel* model) { |
| 124 AddSubMenu(command_id, l10n_util::GetStringUTF16(string_id), model); | 127 AddSubMenu(command_id, l10n_util::GetStringUTF16(string_id), model); |
| 125 } | 128 } |
| 126 | 129 |
| 127 void SimpleMenuModel::InsertItemAt( | 130 void SimpleMenuModel::InsertItemAt( |
| 128 int index, int command_id, const string16& label) { | 131 int index, int command_id, const string16& label) { |
| 129 Item item = { command_id, label, SkBitmap(), TYPE_COMMAND, -1, NULL, NULL }; | 132 Item item = { command_id, label, gfx::ImageSkia(), TYPE_COMMAND, -1, NULL, |
| 133 NULL }; |
| 130 InsertItemAtIndex(item, index); | 134 InsertItemAtIndex(item, index); |
| 131 } | 135 } |
| 132 | 136 |
| 133 void SimpleMenuModel::InsertItemWithStringIdAt( | 137 void SimpleMenuModel::InsertItemWithStringIdAt( |
| 134 int index, int command_id, int string_id) { | 138 int index, int command_id, int string_id) { |
| 135 InsertItemAt(index, command_id, l10n_util::GetStringUTF16(string_id)); | 139 InsertItemAt(index, command_id, l10n_util::GetStringUTF16(string_id)); |
| 136 } | 140 } |
| 137 | 141 |
| 138 void SimpleMenuModel::InsertSeparatorAt(int index) { | 142 void SimpleMenuModel::InsertSeparatorAt(int index) { |
| 139 Item item = { kSeparatorId, string16(), SkBitmap(), TYPE_SEPARATOR, -1, | 143 Item item = { kSeparatorId, string16(), gfx::ImageSkia(), TYPE_SEPARATOR, -1, |
| 140 NULL, NULL }; | 144 NULL, NULL }; |
| 141 InsertItemAtIndex(item, index); | 145 InsertItemAtIndex(item, index); |
| 142 } | 146 } |
| 143 | 147 |
| 144 void SimpleMenuModel::InsertCheckItemAt( | 148 void SimpleMenuModel::InsertCheckItemAt( |
| 145 int index, int command_id, const string16& label) { | 149 int index, int command_id, const string16& label) { |
| 146 Item item = { command_id, label, SkBitmap(), TYPE_CHECK, -1, NULL, NULL }; | 150 Item item = { command_id, label, gfx::ImageSkia(), TYPE_CHECK, -1, NULL, |
| 151 NULL }; |
| 147 InsertItemAtIndex(item, index); | 152 InsertItemAtIndex(item, index); |
| 148 } | 153 } |
| 149 | 154 |
| 150 void SimpleMenuModel::InsertCheckItemWithStringIdAt( | 155 void SimpleMenuModel::InsertCheckItemWithStringIdAt( |
| 151 int index, int command_id, int string_id) { | 156 int index, int command_id, int string_id) { |
| 152 InsertCheckItemAt( | 157 InsertCheckItemAt( |
| 153 FlipIndex(index), command_id, l10n_util::GetStringUTF16(string_id)); | 158 FlipIndex(index), command_id, l10n_util::GetStringUTF16(string_id)); |
| 154 } | 159 } |
| 155 | 160 |
| 156 void SimpleMenuModel::InsertRadioItemAt( | 161 void SimpleMenuModel::InsertRadioItemAt( |
| 157 int index, int command_id, const string16& label, int group_id) { | 162 int index, int command_id, const string16& label, int group_id) { |
| 158 Item item = { command_id, label, SkBitmap(), TYPE_RADIO, group_id, NULL, | 163 Item item = { command_id, label, gfx::ImageSkia(), TYPE_RADIO, group_id, NULL, |
| 159 NULL }; | 164 NULL }; |
| 160 InsertItemAtIndex(item, index); | 165 InsertItemAtIndex(item, index); |
| 161 } | 166 } |
| 162 | 167 |
| 163 void SimpleMenuModel::InsertRadioItemWithStringIdAt( | 168 void SimpleMenuModel::InsertRadioItemWithStringIdAt( |
| 164 int index, int command_id, int string_id, int group_id) { | 169 int index, int command_id, int string_id, int group_id) { |
| 165 InsertRadioItemAt( | 170 InsertRadioItemAt( |
| 166 index, command_id, l10n_util::GetStringUTF16(string_id), group_id); | 171 index, command_id, l10n_util::GetStringUTF16(string_id), group_id); |
| 167 } | 172 } |
| 168 | 173 |
| 169 void SimpleMenuModel::InsertSubMenuAt( | 174 void SimpleMenuModel::InsertSubMenuAt( |
| 170 int index, int command_id, const string16& label, MenuModel* model) { | 175 int index, int command_id, const string16& label, MenuModel* model) { |
| 171 Item item = { command_id, label, SkBitmap(), TYPE_SUBMENU, -1, model, NULL }; | 176 Item item = { command_id, label, gfx::ImageSkia(), TYPE_SUBMENU, -1, model, |
| 177 NULL }; |
| 172 InsertItemAtIndex(item, index); | 178 InsertItemAtIndex(item, index); |
| 173 } | 179 } |
| 174 | 180 |
| 175 void SimpleMenuModel::InsertSubMenuWithStringIdAt( | 181 void SimpleMenuModel::InsertSubMenuWithStringIdAt( |
| 176 int index, int command_id, int string_id, MenuModel* model) { | 182 int index, int command_id, int string_id, MenuModel* model) { |
| 177 InsertSubMenuAt(index, command_id, l10n_util::GetStringUTF16(string_id), | 183 InsertSubMenuAt(index, command_id, l10n_util::GetStringUTF16(string_id), |
| 178 model); | 184 model); |
| 179 } | 185 } |
| 180 | 186 |
| 181 void SimpleMenuModel::SetIcon(int index, const SkBitmap& icon) { | 187 void SimpleMenuModel::SetIcon(int index, const gfx::ImageSkia& icon) { |
| 182 items_[index].icon = icon; | 188 items_[index].icon = icon; |
| 183 } | 189 } |
| 184 | 190 |
| 185 void SimpleMenuModel::Clear() { | 191 void SimpleMenuModel::Clear() { |
| 186 items_.clear(); | 192 items_.clear(); |
| 187 } | 193 } |
| 188 | 194 |
| 189 int SimpleMenuModel::GetIndexOfCommandId(int command_id) { | 195 int SimpleMenuModel::GetIndexOfCommandId(int command_id) { |
| 190 for (ItemVector::iterator i = items_.begin(); i != items_.end(); ++i) { | 196 for (ItemVector::iterator i = items_.begin(); i != items_.end(); ++i) { |
| 191 if ((*i).command_id == command_id) { | 197 if ((*i).command_id == command_id) { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 int item_index = FlipIndex(index); | 252 int item_index = FlipIndex(index); |
| 247 MenuModel::ItemType item_type = items_[item_index].type; | 253 MenuModel::ItemType item_type = items_[item_index].type; |
| 248 return (item_type == TYPE_CHECK || item_type == TYPE_RADIO) ? | 254 return (item_type == TYPE_CHECK || item_type == TYPE_RADIO) ? |
| 249 delegate_->IsCommandIdChecked(GetCommandIdAt(index)) : false; | 255 delegate_->IsCommandIdChecked(GetCommandIdAt(index)) : false; |
| 250 } | 256 } |
| 251 | 257 |
| 252 int SimpleMenuModel::GetGroupIdAt(int index) const { | 258 int SimpleMenuModel::GetGroupIdAt(int index) const { |
| 253 return items_.at(FlipIndex(index)).group_id; | 259 return items_.at(FlipIndex(index)).group_id; |
| 254 } | 260 } |
| 255 | 261 |
| 256 bool SimpleMenuModel::GetIconAt(int index, SkBitmap* icon) { | 262 bool SimpleMenuModel::GetIconAt(int index, gfx::ImageSkia* icon) { |
| 257 if (IsItemDynamicAt(index)) | 263 if (IsItemDynamicAt(index)) |
| 258 return delegate_->GetIconForCommandId(GetCommandIdAt(index), icon); | 264 return delegate_->GetIconForCommandId(GetCommandIdAt(index), icon); |
| 259 | 265 |
| 260 if (items_[index].icon.isNull()) | 266 if (items_[index].icon.isNull()) |
| 261 return false; | 267 return false; |
| 262 | 268 |
| 263 *icon = items_[index].icon; | 269 *icon = items_[index].icon; |
| 264 return true; | 270 return true; |
| 265 } | 271 } |
| 266 | 272 |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 #ifndef NDEBUG | 354 #ifndef NDEBUG |
| 349 if (item.type == TYPE_SEPARATOR) { | 355 if (item.type == TYPE_SEPARATOR) { |
| 350 DCHECK_EQ(item.command_id, kSeparatorId); | 356 DCHECK_EQ(item.command_id, kSeparatorId); |
| 351 } else { | 357 } else { |
| 352 DCHECK_GE(item.command_id, 0); | 358 DCHECK_GE(item.command_id, 0); |
| 353 } | 359 } |
| 354 #endif // NDEBUG | 360 #endif // NDEBUG |
| 355 } | 361 } |
| 356 | 362 |
| 357 } // namespace ui | 363 } // namespace ui |
| OLD | NEW |