1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef UI_VIEWS_CONTROLS_MENU_MENU_WIN_H_
6 #define UI_VIEWS_CONTROLS_MENU_MENU_WIN_H_
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "ui/views/controls/menu/menu.h"
22 ///////////////////////////////////////////////////////////////////////////////
26 // A wrapper around a Win32 HMENU handle that provides convenient APIs for
27 // menu construction, display and subsequent command execution.
29 ///////////////////////////////////////////////////////////////////////////////
30 class MenuWin : public Menu {
32 // Construct a Menu using the specified controller to determine command
34 // delegate A Menu::Delegate implementation that provides more
35 // information about the Menu presentation.
36 // anchor An alignment hint for the popup menu.
37 // owner The window that the menu is being brought up relative
38 // to. Not actually used for anything but must not be
40 MenuWin(Delegate* d, AnchorPoint anchor, HWND owner);
41 // Alternatively, a Menu object can be constructed wrapping an existing
42 // HMENU. This can be used to use the convenience methods to insert
43 // menu items and manage label string ownership. However this kind of
44 // Menu object cannot use the delegate.
45 explicit MenuWin(HMENU hmenu);
48 // Overridden from Menu:
49 virtual void AddMenuItemWithIcon(int index,
51 const string16& label,
52 const gfx::ImageSkia& icon) OVERRIDE;
53 virtual Menu* AddSubMenuWithIcon(int index,
55 const string16& label,
56 const gfx::ImageSkia& icon) OVERRIDE;
57 virtual void AddSeparator(int index) OVERRIDE;
58 virtual void EnableMenuItemByID(int item_id, bool enabled) OVERRIDE;
59 virtual void EnableMenuItemAt(int index, bool enabled) OVERRIDE;
60 virtual void SetMenuLabel(int item_id, const string16& label) OVERRIDE;
61 virtual bool SetIcon(const gfx::ImageSkia& icon, int item_id) OVERRIDE;
62 virtual void RunMenuAt(int x, int y) OVERRIDE;
63 virtual void Cancel() OVERRIDE;
64 virtual int ItemCount() OVERRIDE;
66 virtual HMENU GetMenuHandle() const {
70 // Gets the Win32 TPM alignment flags for the specified AnchorPoint.
71 DWORD GetTPMAlignFlags() const;
74 virtual void AddMenuItemInternal(int index,
76 const string16& label,
77 const gfx::ImageSkia& icon,
78 MenuItemType type) OVERRIDE;
81 friend class MenuHostWindow;
83 // The data of menu items needed to display.
86 void AddMenuItemInternal(int index,
88 const string16& label,
89 const gfx::ImageSkia& icon,
93 explicit MenuWin(MenuWin* parent);
95 // Sets menu information before displaying, including sub-menus.
98 // Get all the state flags for the |fState| field of MENUITEMINFO for the
99 // item with the specified id. |delegate| is consulted if non-NULL about
100 // the state of the item in preference to |controller_|.
101 UINT GetStateFlagsForItemID(int item_id) const;
103 // The Win32 Menu Handle we wrap
106 // The window that would receive WM_COMMAND messages when the user selects
107 // an item from the menu.
110 // This list is used to store the default labels for the menu items.
111 // We may use contextual labels when RunMenu is called, so we must save
112 // a copy of default ones here.
113 std::vector<string16> labels_;
115 // A flag to indicate whether this menu will be drawn by the Menu class.
116 // If it's true, all the menu items will be owner drawn. Otherwise,
117 // all the drawing will be done by Windows.
120 // This list is to store the string labels and icons to display. It's used
121 // when owner_draw_ is true. We give MENUITEMINFO pointers to these
122 // structures to specify what we'd like to draw. If owner_draw_ is false,
123 // we only give MENUITEMINFO pointers to the labels_.
124 // The label member of the ItemData structure comes from either labels_ or
125 // the GetContextualLabel.
126 std::vector<ItemData*> item_data_;
128 // Our sub-menus, if any.
129 std::vector<MenuWin*> submenus_;
131 // Whether the menu is visible.
132 bool is_menu_visible_;
134 DISALLOW_COPY_AND_ASSIGN(MenuWin);
139 #endif // UI_VIEWS_CONTROLS_MENU_MENU_WIN_H_