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