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) { |
sky
2011/03/18 17:54:11
Position doesn't match header.
| |
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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 |
302 void SimpleMenuModel::OnMenuClosed() { | 303 void SimpleMenuModel::OnMenuClosed() { |
303 if (delegate_) | 304 if (delegate_) |
304 delegate_->MenuClosed(); | 305 delegate_->MenuClosed(); |
305 } | 306 } |
306 | 307 |
308 void SimpleMenuModel::SetMenuModelDelegate( | |
309 ui::MenuModelDelegate* menu_model_delegate) { | |
310 menu_model_delegate_ = menu_model_delegate; | |
311 } | |
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 //////////////////////////////////////////////////////////////////////////////// |
312 // SimpleMenuModel, Private: | 318 // SimpleMenuModel, Private: |
313 | 319 |
314 void SimpleMenuModel::AppendItem(const Item& item) { | 320 void SimpleMenuModel::AppendItem(const Item& item) { |
315 ValidateItem(item); | 321 ValidateItem(item); |
316 items_.push_back(item); | 322 items_.push_back(item); |
317 } | 323 } |
318 | 324 |
319 void SimpleMenuModel::InsertItemAtIndex(const Item& item, int index) { | 325 void SimpleMenuModel::InsertItemAtIndex(const Item& item, int index) { |
320 ValidateItem(item); | 326 ValidateItem(item); |
321 items_.insert(items_.begin() + FlipIndex(index), item); | 327 items_.insert(items_.begin() + FlipIndex(index), item); |
322 } | 328 } |
323 | 329 |
324 void SimpleMenuModel::ValidateItem(const Item& item) { | 330 void SimpleMenuModel::ValidateItem(const Item& item) { |
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 |