1 // This is core/vgui/impl/win32/vgui_win32_utils.h
2 
3 #ifndef vgui_win32_utils_h
4 #define vgui_win32_utils_h
5 
6 #include <windows.h>
7 #include <vgui/vgui_menu.h>
8 
9 // The definitions of MENUEX_TEMPLATE_HEADER and MENUEX_TEMPLATE_ITEM
10 // are not present in any standard header file. They are copied here
11 // from MSDN documentation.
12 // We use MENUEX_TEMPLATE_HEADER and MENUEX_TEMPLATE_ITEM instead of
13 // MENUITEMTEMPLATEHEADER and MENUITEMTEMPLATE because a separator is
14 // not defined in the latter.
15 typedef struct
16 {
17   WORD  wVersion; // typedef unsigned short WORD;
18   WORD  wOffset;
19   DWORD dwHelpId; // typedef unsigned long DWORD;
20 } MENUEX_TEMPLATE_HEADER;
21 
22 typedef struct
23 {
24   DWORD dwType;
25   DWORD dwState;
26   DWORD menuId;
27   WORD  bResInfo;
28   WCHAR szText;   // variable length;
29   DWORD dwHelpId; // only included for submenu
30 } MENUEX_TEMPLATE_ITEM;
31 
32 #define MENU_ID_START      0x8400
33 #define POPUPMENU_ID_START 0x8600
34 
35 class vgui_win32_utils
36 {
37  public:
38   // (Create if necessary and) return singleton instance of this class.
39   static vgui_win32_utils *instance();
40 
41   // Convert a vgui_menu to a Win32 menu
42   HMENU vgui_menu_to_win32(vgui_menu const &vguimenu,
43                            std::vector<vgui_command_sptr> &callbacks, HACCEL *hAccel,
44                            bool isPopup = false);
45 
46   // Convert a vgui_menu to an extended Win32 menu
47   HMENU vgui_menu_to_win32ex(vgui_menu const &vguimenu,
48                              std::vector<vgui_command_sptr> &callbacks, HACCEL *hAccel,
49                              bool isPopup = false);
50 
51  protected:
vgui_win32_utils()52   vgui_win32_utils() {}
~vgui_win32_utils()53   ~vgui_win32_utils() {}
54 
55  private:
56   int addMenuItems(vgui_menu const &vguimenu, int offset, bool is_popup);
57   int addMenuItemsEx(vgui_menu const &vguimenu, int offset, bool is_popup);
58   void addAccelerator(std::string&, vgui_menu_item const&, int);
59   std::string vgui_key_to_string(vgui_key);
60   UINT vgui_key_to_virt_key(vgui_key);
61 
62   // Show explainary message for GetLastError().
63   void ShowErrorMessage(DWORD dwErrorNo);
64 
65   unsigned char *pMenu; // menu template buffer
66   int menu_capacity; // menu template length
67   std::vector<vgui_command_sptr> callbacks; // commands called by menu items
68   int item_count; // count of menu items
69 
70   ACCEL *pAccel; // pointer to accelerator table, varying length;
71   int accel_capacity;
72   int accel_count; // count of accelerators
73 };
74 
75 #endif // vgui_win32_utils_h
76