OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this |
| 2 // source code is governed by a BSD-style license that can be found in the |
| 3 // LICENSE file. |
| 4 |
| 5 #ifndef CONTROLS_MENU_VIEWS_SIMPLE_MENU_MODEL_H_ |
| 6 #define CONTROLS_MENU_VIEWS_SIMPLE_MENU_MODEL_H_ |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "views/controls/menu/menu_2.h" |
| 11 |
| 12 namespace views { |
| 13 |
| 14 // A simple Menu2Model implementation with an imperative API for adding menu |
| 15 // items. This makes it easy to construct fixed menus. Menus populated by |
| 16 // dynamic data sources may be better off implementing Menu2Model directly. |
| 17 // The breadth of Menu2Model is not exposed through this API. |
| 18 class SimpleMenuModel : public Menu2Model { |
| 19 public: |
| 20 class Delegate { |
| 21 public: |
| 22 // Methods for determining the state of specific command ids. |
| 23 virtual bool IsCommandIdChecked(int command_id) const = 0; |
| 24 virtual bool IsCommandIdEnabled(int command_id) const = 0; |
| 25 |
| 26 // Gets the accelerator for the specified command id. Returns true if the |
| 27 // command id has a valid accelerator, false otherwise. |
| 28 virtual bool GetAcceleratorForCommandId( |
| 29 int command_id, |
| 30 views::Accelerator* accelerator) = 0; |
| 31 |
| 32 // Some command ids have labels that change over time. |
| 33 virtual bool IsLabelForCommandIdDynamic(int command_id) const = 0; |
| 34 virtual std::wstring GetLabelForCommandId(int command_id) const = 0; |
| 35 }; |
| 36 |
| 37 // The Delegate can be NULL, though if it is items can't be checked or |
| 38 // disabled. |
| 39 explicit SimpleMenuModel(Delegate* delegate); |
| 40 virtual ~SimpleMenuModel(); |
| 41 |
| 42 // Methods for adding items to the model. |
| 43 void AddItem(int command_id, const std::wstring& label); |
| 44 void AddItemWithStringId(int command_id, int string_id); |
| 45 void AddSeparator(); |
| 46 void AddCheckItem(int command_id, const std::wstring& label); |
| 47 void AddCheckItemWithStringId(int command_id, int string_id); |
| 48 void AddRadioItem(int command_id, const std::wstring& label, int group_id); |
| 49 void AddRadioItemWithStringId(int command_id, int string_id, int group_id); |
| 50 void AddSubMenu(const std::wstring& label, Menu2Model* model); |
| 51 void AddSubMenuWithStringId(int string_id, Menu2Model* model); |
| 52 |
| 53 // Overridden from Menu2Model: |
| 54 virtual bool HasIcons() const; |
| 55 virtual int GetItemCount() const; |
| 56 virtual ItemType GetTypeAt(int index) const; |
| 57 virtual int GetCommandIdAt(int index) const; |
| 58 virtual std::wstring GetLabelAt(int index) const; |
| 59 virtual bool IsLabelDynamicAt(int index) const; |
| 60 virtual bool GetAcceleratorAt(int index, |
| 61 views::Accelerator* accelerator) const; |
| 62 virtual bool IsItemCheckedAt(int index) const; |
| 63 virtual int GetGroupIdAt(int index) const; |
| 64 virtual bool GetIconAt(int index, SkBitmap* icon) const; |
| 65 virtual bool IsEnabledAt(int index) const; |
| 66 virtual Menu2Model* GetSubmenuModelAt(int index) const; |
| 67 |
| 68 protected: |
| 69 // Some variants of this model (SystemMenuModel) relies on items to be |
| 70 // inserted backwards. This is counter-intuitive for the API, so rather than |
| 71 // forcing customers to insert things backwards, we return the indices |
| 72 // backwards instead. That's what this method is for. By default, it just |
| 73 // returns what it's passed. |
| 74 virtual int FlipIndex(int index) const { return index; } |
| 75 |
| 76 private: |
| 77 struct Item { |
| 78 int command_id; |
| 79 std::wstring label; |
| 80 ItemType type; |
| 81 int group_id; |
| 82 Menu2Model* submenu; |
| 83 }; |
| 84 std::vector<Item> items_; |
| 85 |
| 86 Delegate* delegate_; |
| 87 |
| 88 DISALLOW_COPY_AND_ASSIGN(SimpleMenuModel); |
| 89 }; |
| 90 |
| 91 } // namespace views |
| 92 |
| 93 #endif // CONTROLS_MENU_VIEWS_SIMPLE_MENU_MODEL_H_ |
OLD | NEW |