xref: /aosp_15_r20/external/lzma/CPP/Windows/Menu.h (revision f6dc9357d832569d4d1f5d24eacdb3935a1ae8e6)
1 // Windows/Menu.h
2 
3 #ifndef ZIP7_INC_WINDOWS_MENU_H
4 #define ZIP7_INC_WINDOWS_MENU_H
5 
6 #include "../Common/MyWindows.h"
7 #include "../Common/MyString.h"
8 
9 #include "Defs.h"
10 
11 namespace NWindows {
12 
13 #ifndef MIIM_STRING
14 #define MIIM_STRING      0x00000040
15 #endif
16 /*
17 #ifndef MIIM_BITMAP
18 #define MIIM_BITMAP      0x00000080
19 #endif
20 */
21 #ifndef MIIM_FTYPE
22 #define MIIM_FTYPE       0x00000100
23 #endif
24 
25 struct CMenuItem
26 {
27   UString StringValue;
28   UINT fMask;
29   UINT fType;
30   UINT fState;
31   UINT wID;
32   HMENU hSubMenu;
33   HBITMAP hbmpChecked;
34   HBITMAP hbmpUnchecked;
35   ULONG_PTR dwItemData;
36   // LPTSTR dwTypeData;
37   // UINT cch;
38   // HBITMAP hbmpItem;
IsStringCMenuItem39   bool IsString() const { return (fMask & (MIIM_TYPE | MIIM_STRING)) != 0; }
IsSeparatorCMenuItem40   bool IsSeparator() const { return (fType == MFT_SEPARATOR); }
CMenuItemCMenuItem41   CMenuItem(): fMask(0), fType(0), fState(0), wID(0),
42       hSubMenu(NULL), hbmpChecked(NULL), hbmpUnchecked(NULL), dwItemData(0) {}
43 };
44 
45 class CMenu
46 {
47   HMENU _menu;
48 public:
CMenu()49   CMenu(): _menu(NULL) {}
HMENU()50   operator HMENU() const { return _menu; }
Attach(HMENU menu)51   void Attach(HMENU menu) { _menu = menu; }
52 
Detach()53   HMENU Detach()
54   {
55     const HMENU menu = _menu;
56     _menu = NULL;
57     return menu;
58   }
59 
Create()60   bool Create()
61   {
62     _menu = ::CreateMenu();
63     return (_menu != NULL);
64   }
65 
CreatePopup()66   bool CreatePopup()
67   {
68     _menu = ::CreatePopupMenu();
69     return (_menu != NULL);
70   }
71 
Destroy()72   bool Destroy()
73   {
74     if (!_menu)
75       return false;
76     return BOOLToBool(::DestroyMenu(Detach()));
77   }
78 
GetItemCount()79   int GetItemCount() const
80   {
81     #ifdef UNDER_CE
82     for (unsigned i = 0;; i++)
83     {
84       CMenuItem item;
85       item.fMask = MIIM_STATE;
86       if (!GetItem(i, true, item))
87         return (int)i;
88     }
89     #else
90     return GetMenuItemCount(_menu);
91     #endif
92   }
93 
GetSubMenu(int pos)94   HMENU GetSubMenu(int pos) const { return ::GetSubMenu(_menu, pos); }
95   #ifndef UNDER_CE
96   /*
97   bool GetItemString(UINT idItem, UINT flag, CSysString &result)
98   {
99     result.Empty();
100     int len = ::GetMenuString(_menu, idItem, 0, 0, flag);
101     int len2 = ::GetMenuString(_menu, idItem, result.GetBuf(len + 2), len + 1, flag);
102     if (len > len2)
103       len = len2;
104     result.ReleaseBuf_CalcLen(len + 2);
105     return (len != 0);
106   }
107   */
GetItemID(int pos)108   UINT GetItemID(int pos) const { return ::GetMenuItemID(_menu, pos);   }
GetItemState(UINT id,UINT flags)109   UINT GetItemState(UINT id, UINT flags)  const { return ::GetMenuState(_menu, id, flags);   }
110   #endif
111 
GetItemInfo(UINT itemIndex,bool byPosition,LPMENUITEMINFO itemInfo)112   bool GetItemInfo(UINT itemIndex, bool byPosition, LPMENUITEMINFO itemInfo) const
113     { return BOOLToBool(::GetMenuItemInfo(_menu, itemIndex, BoolToBOOL(byPosition), itemInfo)); }
SetItemInfo(UINT itemIndex,bool byPosition,LPMENUITEMINFO itemInfo)114   bool SetItemInfo(UINT itemIndex, bool byPosition, LPMENUITEMINFO itemInfo)
115     { return BOOLToBool(::SetMenuItemInfo(_menu, itemIndex, BoolToBOOL(byPosition), itemInfo)); }
116 
AppendItem(UINT flags,UINT_PTR newItemID,LPCTSTR newItem)117   bool AppendItem(UINT flags, UINT_PTR newItemID, LPCTSTR newItem)
118     { return BOOLToBool(::AppendMenu(_menu, flags, newItemID, newItem)); }
119 
Insert(UINT position,UINT flags,UINT_PTR idNewItem,LPCTSTR newItem)120   bool Insert(UINT position, UINT flags, UINT_PTR idNewItem, LPCTSTR newItem)
121     { return BOOLToBool(::InsertMenu(_menu, position, flags, idNewItem, newItem)); }
122 
123   #ifndef UNDER_CE
InsertItem(UINT itemIndex,bool byPosition,LPCMENUITEMINFO itemInfo)124   bool InsertItem(UINT itemIndex, bool byPosition, LPCMENUITEMINFO itemInfo)
125     { return BOOLToBool(::InsertMenuItem(_menu, itemIndex, BoolToBOOL(byPosition), itemInfo)); }
126   #endif
127 
RemoveItem(UINT item,UINT flags)128   bool RemoveItem(UINT item, UINT flags) { return BOOLToBool(::RemoveMenu(_menu, item, flags)); }
RemoveAllItemsFrom(UINT index)129   void RemoveAllItemsFrom(UINT index) { while (RemoveItem(index, MF_BYPOSITION)); }
RemoveAllItems()130   void RemoveAllItems() { RemoveAllItemsFrom(0); }
131 
132   #ifndef _UNICODE
GetItemInfo(UINT itemIndex,bool byPosition,LPMENUITEMINFOW itemInfo)133   bool GetItemInfo(UINT itemIndex, bool byPosition, LPMENUITEMINFOW itemInfo) const
134     { return BOOLToBool(::GetMenuItemInfoW(_menu, itemIndex, BoolToBOOL(byPosition), itemInfo)); }
InsertItem(UINT itemIndex,bool byPosition,LPMENUITEMINFOW itemInfo)135   bool InsertItem(UINT itemIndex, bool byPosition, LPMENUITEMINFOW itemInfo)
136     { return BOOLToBool(::InsertMenuItemW(_menu, itemIndex, BoolToBOOL(byPosition), itemInfo)); }
SetItemInfo(UINT itemIndex,bool byPosition,LPMENUITEMINFOW itemInfo)137   bool SetItemInfo(UINT itemIndex, bool byPosition, LPMENUITEMINFOW itemInfo)
138     { return BOOLToBool(::SetMenuItemInfoW(_menu, itemIndex, BoolToBOOL(byPosition), itemInfo)); }
139   bool AppendItem(UINT flags, UINT_PTR newItemID, LPCWSTR newItem);
140   #endif
141 
142   bool GetItem(UINT itemIndex, bool byPosition, CMenuItem &item) const;
143   bool SetItem(UINT itemIndex, bool byPosition, const CMenuItem &item);
144   bool InsertItem(UINT itemIndex, bool byPosition, const CMenuItem &item);
145 
Track(UINT flags,int x,int y,HWND hWnd)146   int Track(UINT flags, int x, int y, HWND hWnd) { return ::TrackPopupMenuEx(_menu, flags, x, y, hWnd, NULL); }
147 
CheckRadioItem(UINT idFirst,UINT idLast,UINT idCheck,UINT flags)148   bool CheckRadioItem(UINT idFirst, UINT idLast, UINT idCheck, UINT flags)
149     { return BOOLToBool(::CheckMenuRadioItem(_menu, idFirst, idLast, idCheck, flags)); }
150 
CheckItem(UINT id,UINT uCheck)151   DWORD CheckItem(UINT id, UINT uCheck) { return ::CheckMenuItem(_menu, id, uCheck); }
CheckItemByID(UINT id,bool check)152   DWORD CheckItemByID(UINT id, bool check) { return CheckItem(id, MF_BYCOMMAND | (check ? MF_CHECKED : MF_UNCHECKED)); }
153 
EnableItem(UINT uIDEnableItem,UINT uEnable)154   BOOL EnableItem(UINT uIDEnableItem, UINT uEnable) { return EnableMenuItem(_menu, uIDEnableItem, uEnable); }
155 };
156 
157 class CMenuDestroyer
158 {
159   CMenu *_menu;
160 public:
CMenuDestroyer(CMenu & menu)161   CMenuDestroyer(CMenu &menu): _menu(&menu) {}
CMenuDestroyer()162   CMenuDestroyer(): _menu(NULL) {}
~CMenuDestroyer()163   ~CMenuDestroyer() { if (_menu) _menu->Destroy(); }
Attach(CMenu & menu)164   void Attach(CMenu &menu) { _menu = &menu; }
Disable()165   void Disable() { _menu = NULL; }
166 };
167 
168 }
169 
170 #endif
171