Home | History | Annotate | Download | only in menu
      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.
      4 
      5 #ifndef UI_VIEWS_CONTROLS_MENU_MENU_WIN_H_
      6 #define UI_VIEWS_CONTROLS_MENU_MENU_WIN_H_
      7 
      8 #include <windows.h>
      9 
     10 #include <vector>
     11 
     12 #include "base/basictypes.h"
     13 #include "base/compiler_specific.h"
     14 #include "ui/views/controls/menu/menu.h"
     15 
     16 namespace views {
     17 
     18 namespace {
     19 class MenuHostWindow;
     20 }
     21 
     22 ///////////////////////////////////////////////////////////////////////////////
     23 //
     24 // Menu class
     25 //
     26 //   A wrapper around a Win32 HMENU handle that provides convenient APIs for
     27 //   menu construction, display and subsequent command execution.
     28 //
     29 ///////////////////////////////////////////////////////////////////////////////
     30 class MenuWin : public Menu {
     31  public:
     32   // Construct a Menu using the specified controller to determine command
     33   // state.
     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
     39   //              NULL.
     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);
     46   virtual ~MenuWin();
     47 
     48   // Overridden from Menu:
     49   virtual void AddMenuItemWithIcon(int index,
     50                                    int item_id,
     51                                    const string16& label,
     52                                    const gfx::ImageSkia& icon) OVERRIDE;
     53   virtual Menu* AddSubMenuWithIcon(int index,
     54                                    int item_id,
     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;
     65 
     66   virtual HMENU GetMenuHandle() const {
     67     return menu_;
     68   }
     69 
     70   // Gets the Win32 TPM alignment flags for the specified AnchorPoint.
     71   DWORD GetTPMAlignFlags() const;
     72 
     73  protected:
     74   virtual void AddMenuItemInternal(int index,
     75                                    int item_id,
     76                                    const string16& label,
     77                                    const gfx::ImageSkia& icon,
     78                                    MenuItemType type) OVERRIDE;
     79 
     80  private:
     81   friend class MenuHostWindow;
     82 
     83   // The data of menu items needed to display.
     84   struct ItemData;
     85 
     86   void AddMenuItemInternal(int index,
     87                            int item_id,
     88                            const string16& label,
     89                            const gfx::ImageSkia& icon,
     90                            HMENU submenu,
     91                            MenuItemType type);
     92 
     93   explicit MenuWin(MenuWin* parent);
     94 
     95   // Sets menu information before displaying, including sub-menus.
     96   void SetMenuInfo();
     97 
     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;
    102 
    103   // The Win32 Menu Handle we wrap
    104   HMENU menu_;
    105 
    106   // The window that would receive WM_COMMAND messages when the user selects
    107   // an item from the menu.
    108   HWND owner_;
    109 
    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_;
    114 
    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.
    118   bool owner_draw_;
    119 
    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_;
    127 
    128   // Our sub-menus, if any.
    129   std::vector<MenuWin*> submenus_;
    130 
    131   // Whether the menu is visible.
    132   bool is_menu_visible_;
    133 
    134   DISALLOW_COPY_AND_ASSIGN(MenuWin);
    135 };
    136 
    137 }  // namespace views
    138 
    139 #endif  // UI_VIEWS_CONTROLS_MENU_MENU_WIN_H_
    140