| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "third_party/skia/include/core/SkBitmap.h" | 8 #include "third_party/skia/include/core/SkBitmap.h" |
| 9 #include "ui/base/l10n/l10n_util.h" | 9 #include "ui/base/l10n/l10n_util.h" |
| 10 | 10 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 } | 47 } |
| 48 | 48 |
| 49 void SimpleMenuModel::Delegate::MenuClosed() { | 49 void SimpleMenuModel::Delegate::MenuClosed() { |
| 50 } | 50 } |
| 51 | 51 |
| 52 //////////////////////////////////////////////////////////////////////////////// | 52 //////////////////////////////////////////////////////////////////////////////// |
| 53 // SimpleMenuModel, public: | 53 // SimpleMenuModel, public: |
| 54 | 54 |
| 55 SimpleMenuModel::SimpleMenuModel(Delegate* delegate) | 55 SimpleMenuModel::SimpleMenuModel(Delegate* delegate) |
| 56 : delegate_(delegate), | 56 : delegate_(delegate), |
| 57 menu_model_delegate_(NULL), |
| 57 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | 58 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
| 58 } | 59 } |
| 59 | 60 |
| 60 SimpleMenuModel::~SimpleMenuModel() { | 61 SimpleMenuModel::~SimpleMenuModel() { |
| 61 } | 62 } |
| 62 | 63 |
| 63 void SimpleMenuModel::AddItem(int command_id, const string16& label) { | 64 void SimpleMenuModel::AddItem(int command_id, const string16& label) { |
| 64 Item item = { command_id, label, SkBitmap(), TYPE_COMMAND, -1, NULL, NULL }; | 65 Item item = { command_id, label, SkBitmap(), TYPE_COMMAND, -1, NULL, NULL }; |
| 65 AppendItem(item); | 66 AppendItem(item); |
| 66 } | 67 } |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 int item_index = FlipIndex(index); | 239 int item_index = FlipIndex(index); |
| 239 MenuModel::ItemType item_type = items_[item_index].type; | 240 MenuModel::ItemType item_type = items_[item_index].type; |
| 240 return (item_type == TYPE_CHECK || item_type == TYPE_RADIO) ? | 241 return (item_type == TYPE_CHECK || item_type == TYPE_RADIO) ? |
| 241 delegate_->IsCommandIdChecked(GetCommandIdAt(index)) : false; | 242 delegate_->IsCommandIdChecked(GetCommandIdAt(index)) : false; |
| 242 } | 243 } |
| 243 | 244 |
| 244 int SimpleMenuModel::GetGroupIdAt(int index) const { | 245 int SimpleMenuModel::GetGroupIdAt(int index) const { |
| 245 return items_.at(FlipIndex(index)).group_id; | 246 return items_.at(FlipIndex(index)).group_id; |
| 246 } | 247 } |
| 247 | 248 |
| 248 bool SimpleMenuModel::GetIconAt(int index, SkBitmap* icon) const { | 249 bool SimpleMenuModel::GetIconAt(int index, SkBitmap* icon) { |
| 249 if (IsItemDynamicAt(index)) | 250 if (IsItemDynamicAt(index)) |
| 250 return delegate_->GetIconForCommandId(GetCommandIdAt(index), icon); | 251 return delegate_->GetIconForCommandId(GetCommandIdAt(index), icon); |
| 251 | 252 |
| 252 if (items_[index].icon.isNull()) | 253 if (items_[index].icon.isNull()) |
| 253 return false; | 254 return false; |
| 254 | 255 |
| 255 *icon = items_[index].icon; | 256 *icon = items_[index].icon; |
| 256 return true; | 257 return true; |
| 257 } | 258 } |
| 258 | 259 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 | 293 |
| 293 void SimpleMenuModel::MenuClosed() { | 294 void SimpleMenuModel::MenuClosed() { |
| 294 // Due to how menus work on the different platforms, ActivatedAt will be | 295 // Due to how menus work on the different platforms, ActivatedAt will be |
| 295 // called after this. It's more convenient for the delegate to be called | 296 // called after this. It's more convenient for the delegate to be called |
| 296 // afterwards though, so post a task. | 297 // afterwards though, so post a task. |
| 297 MessageLoop::current()->PostTask( | 298 MessageLoop::current()->PostTask( |
| 298 FROM_HERE, | 299 FROM_HERE, |
| 299 method_factory_.NewRunnableMethod(&SimpleMenuModel::OnMenuClosed)); | 300 method_factory_.NewRunnableMethod(&SimpleMenuModel::OnMenuClosed)); |
| 300 } | 301 } |
| 301 | 302 |
| 303 void SimpleMenuModel::SetMenuModelDelegate( |
| 304 ui::MenuModelDelegate* menu_model_delegate) { |
| 305 menu_model_delegate_ = menu_model_delegate; |
| 306 } |
| 307 |
| 302 void SimpleMenuModel::OnMenuClosed() { | 308 void SimpleMenuModel::OnMenuClosed() { |
| 303 if (delegate_) | 309 if (delegate_) |
| 304 delegate_->MenuClosed(); | 310 delegate_->MenuClosed(); |
| 305 } | 311 } |
| 306 | 312 |
| 307 int SimpleMenuModel::FlipIndex(int index) const { | 313 int SimpleMenuModel::FlipIndex(int index) const { |
| 308 return index; | 314 return index; |
| 309 } | 315 } |
| 310 | 316 |
| 311 //////////////////////////////////////////////////////////////////////////////// | 317 //////////////////////////////////////////////////////////////////////////////// |
| (...skipping 13 matching lines...) Expand all Loading... |
| 325 #ifndef NDEBUG | 331 #ifndef NDEBUG |
| 326 if (item.type == TYPE_SEPARATOR) { | 332 if (item.type == TYPE_SEPARATOR) { |
| 327 DCHECK_EQ(item.command_id, kSeparatorId); | 333 DCHECK_EQ(item.command_id, kSeparatorId); |
| 328 } else { | 334 } else { |
| 329 DCHECK_GE(item.command_id, 0); | 335 DCHECK_GE(item.command_id, 0); |
| 330 } | 336 } |
| 331 #endif // NDEBUG | 337 #endif // NDEBUG |
| 332 } | 338 } |
| 333 | 339 |
| 334 } // namespace ui | 340 } // namespace ui |
| OLD | NEW |