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 VIEWS_CONTROLS_MENU_NATIVE_MENU_WIN_H_ |
| 6 #define VIEWS_CONTROLS_MENU_NATIVE_MENU_WIN_H_ |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "base/scoped_ptr.h" |
| 11 #include "views/controls/menu/menu_wrapper.h" |
| 12 #include "views/controls/menu/simple_menu_model.h" |
| 13 |
| 14 namespace views { |
| 15 |
| 16 class Menu2Delegate; |
| 17 class Menu2Model; |
| 18 |
| 19 // A Windows implementation of MenuWrapper. |
| 20 // TODO(beng): rename to MenuWin once the old class is dead. |
| 21 class NativeMenuWin : public MenuWrapper { |
| 22 public: |
| 23 // Construct a NativeMenuWin, with a model and delegate. If |system_menu_for| |
| 24 // is non-NULL, the NativeMenuWin wraps the system menu for that window. |
| 25 // The caller owns the model and the delegate. |
| 26 NativeMenuWin(Menu2Model* model, |
| 27 Menu2Delegate* delegate, |
| 28 HWND system_menu_for); |
| 29 virtual ~NativeMenuWin(); |
| 30 |
| 31 // Overridden from MenuWrapper: |
| 32 virtual void RunMenuAt(const gfx::Point& point, int alignment); |
| 33 virtual void Rebuild(); |
| 34 virtual void UpdateStates(); |
| 35 virtual gfx::NativeMenu GetNativeMenu() const; |
| 36 |
| 37 private: |
| 38 // IMPORTANT: Note about indices. |
| 39 // Functions in this class deal in two index spaces: |
| 40 // 1. menu_index - the index of an item within the actual Windows |
| 41 // native menu. |
| 42 // 2. model_index - the index of the item within our model. |
| 43 // These two are most often but not always the same value! The |
| 44 // notable exception is when this object is used to wrap the |
| 45 // Windows System Menu. In this instance, the model indices start |
| 46 // at 0, but the insertion index into the existing menu is not. |
| 47 // It is important to take this into consideration when editing the |
| 48 // code in the functions in this class. |
| 49 |
| 50 // Returns true if the item at the specified index is a separator. |
| 51 bool IsSeparatorItemAt(int menu_index) const; |
| 52 |
| 53 // Add items. See note above about indices. |
| 54 void AddMenuItemAt(int menu_index, int model_index); |
| 55 void AddSeparatorItemAt(int menu_index, int model_index); |
| 56 |
| 57 // Sets the state of the item at the specified index. |
| 58 void SetMenuItemState(int menu_index, |
| 59 bool enabled, |
| 60 bool checked, |
| 61 bool is_default); |
| 62 |
| 63 // Sets the label of the item at the specified index. |
| 64 void SetMenuItemLabel(int menu_index, |
| 65 int model_index, |
| 66 const std::wstring& label); |
| 67 |
| 68 // Updates the local data structure with the correctly formatted version of |
| 69 // |label| at the specified model_index, and adds string data to |mii| if |
| 70 // the menu is not owner-draw. That's a mouthful. This function exists because |
| 71 // of the peculiarities of the Windows menu API. |
| 72 void UpdateMenuItemInfoForString(MENUITEMINFO* mii, |
| 73 int model_index, |
| 74 const std::wstring& label); |
| 75 |
| 76 // Returns the NativeMenuWin object that contains an item with the specified |
| 77 // command id. This function must only be called from RunMenuAt! |
| 78 NativeMenuWin* GetMenuForCommandId(UINT command_id) const; |
| 79 |
| 80 // Returns the alignment flags to be passed to TrackPopupMenuEx, based on the |
| 81 // supplied alignment and the UI text direction. |
| 82 UINT GetAlignmentFlags(int alignment) const; |
| 83 |
| 84 // Resets the native menu stored in |menu_| by destroying any old menu then |
| 85 // creating a new empty one. |
| 86 void ResetNativeMenu(); |
| 87 |
| 88 // Creates the host window that receives notifications from the menu. |
| 89 void CreateHostWindow(); |
| 90 |
| 91 // Our attached model and delegate. |
| 92 Menu2Model* model_; |
| 93 Menu2Delegate* delegate_; |
| 94 |
| 95 HMENU menu_; |
| 96 |
| 97 // True if the contents of menu items in this menu are drawn by the menu host |
| 98 // window, rather than Windows. |
| 99 bool owner_draw_; |
| 100 |
| 101 // An object that collects all of the data associated with an individual menu |
| 102 // item. |
| 103 struct ItemData; |
| 104 std::vector<ItemData*> items_; |
| 105 |
| 106 // The window that receives notifications from the menu. |
| 107 class MenuHostWindow; |
| 108 scoped_ptr<MenuHostWindow> host_window_; |
| 109 |
| 110 // The HWND this menu is the system menu for, or NULL if the menu is not a |
| 111 // system menu. |
| 112 HWND system_menu_for_; |
| 113 |
| 114 // The index of the first item in the model in the menu. |
| 115 int first_item_index_; |
| 116 |
| 117 DISALLOW_COPY_AND_ASSIGN(NativeMenuWin); |
| 118 }; |
| 119 |
| 120 // A SimpleMenuModel subclass that allows the system menu for a window to be |
| 121 // wrapped. |
| 122 class SystemMenuModel : public SimpleMenuModel { |
| 123 public: |
| 124 explicit SystemMenuModel(Delegate* delegate); |
| 125 virtual ~SystemMenuModel(); |
| 126 |
| 127 // Overridden from Menu2Model: |
| 128 virtual int GetFirstItemIndex(gfx::NativeMenu native_menu) const; |
| 129 |
| 130 protected: |
| 131 // Overridden from SimpleMenuModel: |
| 132 virtual int FlipIndex(int index) const { return GetItemCount() - index - 1; } |
| 133 |
| 134 private: |
| 135 DISALLOW_COPY_AND_ASSIGN(SystemMenuModel); |
| 136 }; |
| 137 |
| 138 } // namespace views |
| 139 |
| 140 #endif // VIEWS_CONTROLS_MENU_NATIVE_MENU_WIN_H_ |
OLD | NEW |