Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

339 lines
9.1 KiB

  1. // WTL Version 3.1
  2. // Copyright (C) 1997-2000 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This file is a part of Windows Template Library.
  6. // The code and information is provided "as-is" without
  7. // warranty of any kind, either expressed or implied.
  8. #ifndef __ATLUSER_H__
  9. #define __ATLUSER_H__
  10. #pragma once
  11. #ifndef __cplusplus
  12. #error ATL requires C++ compilation (use a .cpp suffix)
  13. #endif
  14. #ifndef __ATLBASE_H__
  15. #error atluser.h requires atlbase.h to be included first
  16. #endif
  17. namespace WTL
  18. {
  19. /////////////////////////////////////////////////////////////////////////////
  20. // Forward declarations
  21. class CMenuItemInfo;
  22. template <bool t_bManaged> class CMenuT;
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CMenu
  25. class CMenuItemInfo : public MENUITEMINFO
  26. {
  27. public:
  28. CMenuItemInfo()
  29. {
  30. memset(this, 0, sizeof(MENUITEMINFO));
  31. cbSize = sizeof(MENUITEMINFO);
  32. }
  33. };
  34. typedef CMenuT<false> CMenuHandle;
  35. typedef CMenuT<true> CMenu;
  36. template <bool t_bManaged>
  37. class CMenuT
  38. {
  39. public:
  40. // Data members
  41. HMENU m_hMenu;
  42. // Constructor/destructor/operators
  43. CMenuT(HMENU hMenu = NULL) : m_hMenu(hMenu)
  44. { }
  45. ~CMenuT()
  46. {
  47. if(t_bManaged && m_hMenu != NULL)
  48. DestroyMenu();
  49. }
  50. CMenuT<t_bManaged>& operator=(HMENU hMenu)
  51. {
  52. m_hMenu = hMenu;
  53. return *this;
  54. }
  55. void Attach(HMENU hMenuNew)
  56. {
  57. ATLASSERT(::IsMenu(hMenuNew));
  58. if(t_bManaged && m_hMenu != NULL)
  59. ::DestroyMenu(m_hMenu);
  60. m_hMenu = hMenuNew;
  61. }
  62. HMENU Detach()
  63. {
  64. HMENU hMenu = m_hMenu;
  65. m_hMenu = NULL;
  66. return hMenu;
  67. }
  68. operator HMENU() const { return m_hMenu; }
  69. BOOL IsMenu() const
  70. {
  71. return ::IsMenu(m_hMenu);
  72. }
  73. // Create and load methods
  74. BOOL CreateMenu()
  75. {
  76. ATLASSERT(m_hMenu == NULL);
  77. m_hMenu = ::CreateMenu();
  78. return (m_hMenu != NULL) ? TRUE : FALSE;
  79. }
  80. BOOL CreatePopupMenu()
  81. {
  82. ATLASSERT(m_hMenu == NULL);
  83. m_hMenu = ::CreatePopupMenu();
  84. return (m_hMenu != NULL) ? TRUE : FALSE;
  85. }
  86. BOOL LoadMenu(_U_STRINGorID menu)
  87. {
  88. ATLASSERT(m_hMenu == NULL);
  89. m_hMenu = ::LoadMenu(_Module.GetResourceInstance(), menu.m_lpstr);
  90. return (m_hMenu != NULL) ? TRUE : FALSE;
  91. }
  92. BOOL LoadMenuIndirect(const void* lpMenuTemplate)
  93. {
  94. ATLASSERT(m_hMenu == NULL);
  95. m_hMenu = ::LoadMenuIndirect(lpMenuTemplate);
  96. return (m_hMenu != NULL) ? TRUE : FALSE;
  97. }
  98. BOOL DestroyMenu()
  99. {
  100. if (m_hMenu == NULL)
  101. return FALSE;
  102. return ::DestroyMenu(Detach());
  103. }
  104. // Menu Operations
  105. BOOL DeleteMenu(UINT nPosition, UINT nFlags)
  106. {
  107. ATLASSERT(::IsMenu(m_hMenu));
  108. return ::DeleteMenu(m_hMenu, nPosition, nFlags);
  109. }
  110. BOOL TrackPopupMenu(UINT nFlags, int x, int y, HWND hWnd, LPCRECT lpRect = NULL)
  111. {
  112. ATLASSERT(::IsMenu(m_hMenu));
  113. return ::TrackPopupMenu(m_hMenu, nFlags, x, y, 0, hWnd, lpRect);
  114. }
  115. BOOL TrackPopupMenuEx(UINT uFlags, int x, int y, HWND hWnd, LPTPMPARAMS lptpm = NULL)
  116. {
  117. ATLASSERT(::IsMenu(m_hMenu));
  118. return ::TrackPopupMenuEx(m_hMenu, uFlags, x, y, hWnd, lptpm);
  119. }
  120. #if (WINVER >= 0x0500)
  121. BOOL GetMenuInfo(LPMENUINFO lpMenuInfo) const
  122. {
  123. ATLASSERT(::IsMenu(m_hMenu));
  124. return ::GetMenuInfo(m_hMenu, lpMenuInfo);
  125. }
  126. BOOL SetMenuInfo(LPCMENUINFO lpMenuInfo)
  127. {
  128. ATLASSERT(::IsMenu(m_hMenu));
  129. return ::SetMenuInfo(m_hMenu, lpMenuInfo);
  130. }
  131. #endif //(WINVER >= 0x0500)
  132. // Menu Item Operations
  133. BOOL AppendMenu(UINT nFlags, UINT_PTR nIDNewItem = 0, LPCTSTR lpszNewItem = NULL)
  134. {
  135. ATLASSERT(::IsMenu(m_hMenu));
  136. return ::AppendMenu(m_hMenu, nFlags, nIDNewItem, lpszNewItem);
  137. }
  138. BOOL AppendMenu(UINT nFlags, UINT_PTR nIDNewItem, HBITMAP hBmp)
  139. {
  140. ATLASSERT(::IsMenu(m_hMenu));
  141. return ::AppendMenu(m_hMenu, nFlags | MF_BITMAP, nIDNewItem, (LPCTSTR)hBmp);
  142. }
  143. UINT CheckMenuItem(UINT nIDCheckItem, UINT nCheck)
  144. {
  145. ATLASSERT(::IsMenu(m_hMenu));
  146. return (UINT)::CheckMenuItem(m_hMenu, nIDCheckItem, nCheck);
  147. }
  148. UINT EnableMenuItem(UINT nIDEnableItem, UINT nEnable)
  149. {
  150. ATLASSERT(::IsMenu(m_hMenu));
  151. return ::EnableMenuItem(m_hMenu, nIDEnableItem, nEnable);
  152. }
  153. BOOL HiliteMenuItem(HWND hWnd, UINT uIDHiliteItem, UINT uHilite)
  154. {
  155. ATLASSERT(::IsMenu(m_hMenu));
  156. return ::HiliteMenuItem(hWnd, m_hMenu, uIDHiliteItem, uHilite);
  157. }
  158. int GetMenuItemCount() const
  159. {
  160. ATLASSERT(::IsMenu(m_hMenu));
  161. return ::GetMenuItemCount(m_hMenu);
  162. }
  163. UINT GetMenuItemID(int nPos) const
  164. {
  165. ATLASSERT(::IsMenu(m_hMenu));
  166. return ::GetMenuItemID(m_hMenu, nPos);
  167. }
  168. UINT GetMenuState(UINT nID, UINT nFlags) const
  169. {
  170. ATLASSERT(::IsMenu(m_hMenu));
  171. return ::GetMenuState(m_hMenu, nID, nFlags);
  172. }
  173. int GetMenuString(UINT nIDItem, LPTSTR lpString, int nMaxCount, UINT nFlags) const
  174. {
  175. ATLASSERT(::IsMenu(m_hMenu));
  176. return ::GetMenuString(m_hMenu, nIDItem, lpString, nMaxCount, nFlags);
  177. }
  178. int GetMenuStringLen(UINT nIDItem, UINT nFlags) const
  179. {
  180. ATLASSERT(::IsMenu(m_hMenu));
  181. return ::GetMenuString(m_hMenu, nIDItem, NULL, 0, nFlags);
  182. }
  183. #ifndef _ATL_NO_COM
  184. BOOL GetMenuString(UINT nIDItem, BSTR& bstrText, UINT nFlags) const
  185. {
  186. USES_CONVERSION;
  187. ATLASSERT(::IsMenu(m_hMenu));
  188. ATLASSERT(bstrText == NULL);
  189. int nLen = GetMenuStringLen(nIDItem, nFlags);
  190. if(nLen == 0)
  191. {
  192. bstrText = ::SysAllocString(OLESTR(""));
  193. return (bstrText != NULL) ? TRUE : FALSE;
  194. }
  195. nLen++; // increment to include terminating NULL char
  196. LPTSTR lpszText = (LPTSTR)_alloca((nLen) * sizeof(TCHAR));
  197. if(!GetMenuString(nIDItem, lpszText, nLen, nFlags))
  198. return FALSE;
  199. bstrText = ::SysAllocString(T2OLE(lpszText));
  200. return (bstrText != NULL) ? TRUE : FALSE;
  201. }
  202. #endif //!_ATL_NO_COM
  203. #ifdef __ATLSTR_H__
  204. int GetMenuString(UINT nIDItem, CString& strText, UINT nFlags) const
  205. {
  206. ATLASSERT(::IsMenu(m_hMenu));
  207. int nLen = GetMenuStringLen(nIDItem, nFlags);
  208. if(nLen == 0)
  209. return 0;
  210. nLen++; // increment to include terminating NULL char
  211. int nRet = GetMenuString(nIDItem, strText.GetBufferSetLength(nLen), nLen, nFlags);
  212. strText.ReleaseBuffer();
  213. return nRet;
  214. }
  215. #endif //__ATLSTR_H__
  216. CMenuHandle GetSubMenu(int nPos) const
  217. {
  218. ATLASSERT(::IsMenu(m_hMenu));
  219. return CMenuHandle(::GetSubMenu(m_hMenu, nPos));
  220. }
  221. BOOL InsertMenu(UINT nPosition, UINT nFlags, UINT_PTR nIDNewItem = 0, LPCTSTR lpszNewItem = NULL)
  222. {
  223. ATLASSERT(::IsMenu(m_hMenu));
  224. return ::InsertMenu(m_hMenu, nPosition, nFlags, nIDNewItem, lpszNewItem);
  225. }
  226. BOOL InsertMenu(UINT nPosition, UINT nFlags, UINT_PTR nIDNewItem, HBITMAP hBmp)
  227. {
  228. ATLASSERT(::IsMenu(m_hMenu));
  229. return ::InsertMenu(m_hMenu, nPosition, nFlags | MF_BITMAP, nIDNewItem, (LPCTSTR)hBmp);
  230. }
  231. BOOL ModifyMenu(UINT nPosition, UINT nFlags, UINT_PTR nIDNewItem = 0, LPCTSTR lpszNewItem = NULL)
  232. {
  233. ATLASSERT(::IsMenu(m_hMenu));
  234. return ::ModifyMenu(m_hMenu, nPosition, nFlags, nIDNewItem, lpszNewItem);
  235. }
  236. BOOL ModifyMenu(UINT nPosition, UINT nFlags, UINT_PTR nIDNewItem, HBITMAP hBmp)
  237. {
  238. ATLASSERT(::IsMenu(m_hMenu));
  239. return ::ModifyMenu(m_hMenu, nPosition, nFlags | MF_BITMAP, nIDNewItem, (LPCTSTR)hBmp);
  240. }
  241. BOOL RemoveMenu(UINT nPosition, UINT nFlags)
  242. {
  243. ATLASSERT(::IsMenu(m_hMenu));
  244. return ::RemoveMenu(m_hMenu, nPosition, nFlags);
  245. }
  246. BOOL SetMenuItemBitmaps(UINT nPosition, UINT nFlags, HBITMAP hBmpUnchecked, HBITMAP hBmpChecked)
  247. {
  248. ATLASSERT(::IsMenu(m_hMenu));
  249. return ::SetMenuItemBitmaps(m_hMenu, nPosition, nFlags, hBmpUnchecked, hBmpChecked);
  250. }
  251. BOOL CheckMenuRadioItem(UINT nIDFirst, UINT nIDLast, UINT nIDItem, UINT nFlags)
  252. {
  253. ATLASSERT(::IsMenu(m_hMenu));
  254. return ::CheckMenuRadioItem(m_hMenu, nIDFirst, nIDLast, nIDItem, nFlags);
  255. }
  256. BOOL GetMenuItemInfo(UINT uItem, BOOL bByPosition, LPMENUITEMINFO lpmii) const
  257. {
  258. ATLASSERT(::IsMenu(m_hMenu));
  259. return (BOOL)::GetMenuItemInfo(m_hMenu, uItem, bByPosition, lpmii);
  260. }
  261. BOOL SetMenuItemInfo(UINT uItem, BOOL bByPosition, LPMENUITEMINFO lpmii)
  262. {
  263. ATLASSERT(::IsMenu(m_hMenu));
  264. return (BOOL)::SetMenuItemInfo(m_hMenu, uItem, bByPosition, lpmii);
  265. }
  266. BOOL InsertMenuItem(UINT uItem, BOOL bByPosition, LPMENUITEMINFO lpmii)
  267. {
  268. ATLASSERT(::IsMenu(m_hMenu));
  269. return (BOOL)::InsertMenuItem(m_hMenu, uItem, bByPosition, lpmii);
  270. }
  271. UINT GetMenuDefaultItem(BOOL bByPosition = FALSE, UINT uFlags = 0U) const
  272. {
  273. ATLASSERT(::IsMenu(m_hMenu));
  274. return ::GetMenuDefaultItem(m_hMenu, (UINT)bByPosition, uFlags);
  275. }
  276. BOOL SetMenuDefaultItem(UINT uItem = (UINT)-1, BOOL bByPosition = FALSE)
  277. {
  278. ATLASSERT(::IsMenu(m_hMenu));
  279. return ::SetMenuDefaultItem(m_hMenu, uItem, (UINT)bByPosition);
  280. }
  281. BOOL GetMenuItemRect(HWND hWnd, UINT uItem, LPRECT lprcItem) const
  282. {
  283. ATLASSERT(::IsMenu(m_hMenu));
  284. return ::GetMenuItemRect(hWnd, m_hMenu, uItem, lprcItem);
  285. }
  286. int MenuItemFromPoint(HWND hWnd, POINT point) const
  287. {
  288. ATLASSERT(::IsMenu(m_hMenu));
  289. return ::MenuItemFromPoint(hWnd, m_hMenu, point);
  290. }
  291. // Context Help Functions
  292. BOOL SetMenuContextHelpId(DWORD dwContextHelpId)
  293. {
  294. ATLASSERT(::IsMenu(m_hMenu));
  295. return ::SetMenuContextHelpId(m_hMenu, dwContextHelpId);
  296. }
  297. DWORD GetMenuContextHelpId() const
  298. {
  299. ATLASSERT(::IsMenu(m_hMenu));
  300. return ::GetMenuContextHelpId(m_hMenu);
  301. }
  302. };
  303. }; //namespace WTL
  304. #endif // __ATLUSER_H__