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.

2483 lines
69 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 __ATLCTRLW_H__
  9. #define __ATLCTRLW_H__
  10. #pragma once
  11. #ifndef __cplusplus
  12. #error ATL requires C++ compilation (use a .cpp suffix)
  13. #endif
  14. #ifndef __ATLAPP_H__
  15. #error atlctrlw.h requires atlapp.h to be included first
  16. #endif
  17. #ifndef __ATLCTRLS_H__
  18. #error atlctrlw.h requires atlctrls.h to be included first
  19. #endif
  20. #if (_WIN32_IE < 0x0400)
  21. #error atlctrlw.h requires _WIN32_IE >= 0x0400
  22. #endif
  23. namespace WTL
  24. {
  25. /////////////////////////////////////////////////////////////////////////////
  26. // Command Bars
  27. // Window Styles:
  28. #define CBRWS_TOP CCS_TOP
  29. #define CBRWS_BOTTOM CCS_BOTTOM
  30. #define CBRWS_NORESIZE CCS_NORESIZE
  31. #define CBRWS_NOPARENTALIGN CCS_NOPARENTALIGN
  32. #define CBRWS_NODIVIDER CCS_NODIVIDER
  33. // Extended styles
  34. #define CBR_EX_TRANSPARENT 0x00000001L
  35. #define CBR_EX_SHAREMENU 0x00000002L
  36. #define CBR_EX_ALTFOCUSMODE 0x00000004L
  37. // standard command bar styles
  38. #define ATL_SIMPLE_CMDBAR_PANE_STYLE \
  39. (WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | CBRWS_NODIVIDER | CBRWS_NORESIZE | CBRWS_NOPARENTALIGN)
  40. // Messages - support chevrons for frame windows
  41. #define CBRM_GETCMDBAR (WM_USER + 301) // return command bar HWND
  42. #define CBRM_GETMENU (WM_USER + 302) // returns loaded or attached menu
  43. #define CBRM_TRACKPOPUPMENU (WM_USER + 303) // displays a popup menu
  44. typedef struct tagCBRPOPUPMENU
  45. {
  46. int cbSize;
  47. HMENU hMenu; // popup menu do display
  48. UINT uFlags; // TPM_* flags for ::TrackPopupMenuEx
  49. int x;
  50. int y;
  51. LPTPMPARAMS lptpm; // ptr to TPMPARAMS for ::TrackPopupMenuEx
  52. } CBRPOPUPMENU, *LPCBRPOPUPMENU;
  53. // helper class
  54. template <class T>
  55. class CSimpleStack : public CSimpleArray< T >
  56. {
  57. public:
  58. BOOL Push(T t)
  59. {
  60. #ifdef _CMDBAR_EXTRA_TRACE
  61. ATLTRACE2(atlTraceUI, 0, "CmdBar - STACK-PUSH (%8.8X) size = %i\n", t, GetSize());
  62. #endif
  63. return Add(t);
  64. }
  65. T Pop()
  66. {
  67. int nLast = GetSize() - 1;
  68. if(nLast < 0)
  69. return NULL; // must be able to convert to NULL
  70. T t = m_aT[nLast];
  71. #ifdef _CMDBAR_EXTRA_TRACE
  72. ATLTRACE2(atlTraceUI, 0, "CmdBar - STACK-POP (%8.8X) size = %i\n", t, GetSize());
  73. #endif
  74. if(!RemoveAt(nLast))
  75. return NULL;
  76. return t;
  77. }
  78. T GetCurrent()
  79. {
  80. int nLast = GetSize() - 1;
  81. if(nLast < 0)
  82. return NULL; // must be able to convert to NULL
  83. return m_aT[nLast];
  84. }
  85. };
  86. /////////////////////////////////////////////////////////////////////////////
  87. // Forward declarations
  88. template <class T, class TBase = CCommandBarCtrlBase, class TWinTraits = CControlWinTraits> class CCommandBarCtrlImpl;
  89. class CCommandBarCtrl;
  90. /////////////////////////////////////////////////////////////////////////////
  91. // CCommandBarCtrlBase - base class for the Command Bar implementation
  92. class CCommandBarCtrlBase : public CToolBarCtrl
  93. {
  94. public:
  95. struct _MsgHookData
  96. {
  97. HHOOK hMsgHook;
  98. DWORD dwUsage;
  99. _MsgHookData() : hMsgHook(NULL), dwUsage(0) { }
  100. };
  101. typedef CSimpleMap<DWORD, _MsgHookData*> CMsgHookMap;
  102. static CMsgHookMap* s_pmapMsgHook;
  103. static HHOOK s_hCreateHook;
  104. static bool s_bW2K; // For animation flag
  105. static CCommandBarCtrlBase* s_pCurrentBar;
  106. static bool s_bStaticInit;
  107. CSimpleStack<HWND> m_stackMenuWnd;
  108. CSimpleStack<HMENU> m_stackMenuHandle;
  109. HWND m_hWndHook;
  110. DWORD m_dwMagic;
  111. CCommandBarCtrlBase() : m_hWndHook(NULL), m_dwMagic(1314)
  112. {
  113. // init static variables
  114. if(!s_bStaticInit)
  115. {
  116. ::EnterCriticalSection(&_Module.m_csStaticDataInit);
  117. if(!s_bStaticInit)
  118. {
  119. // Just in case...
  120. INITCOMMONCONTROLSEX iccx;
  121. iccx.dwSize = sizeof(iccx);
  122. iccx.dwICC = ICC_COOL_CLASSES | ICC_BAR_CLASSES;
  123. ::InitCommonControlsEx(&iccx);
  124. // Animation on Win2000 only
  125. s_bW2K = !AtlIsOldWindows();
  126. // done
  127. s_bStaticInit = true;
  128. }
  129. ::LeaveCriticalSection(&_Module.m_csStaticDataInit);
  130. }
  131. }
  132. bool IsCommandBarBase() const { return m_dwMagic == 1314; }
  133. };
  134. __declspec(selectany) CCommandBarCtrlBase::CMsgHookMap* CCommandBarCtrlBase::s_pmapMsgHook = NULL;
  135. __declspec(selectany) HHOOK CCommandBarCtrlBase::s_hCreateHook = NULL;
  136. __declspec(selectany) CCommandBarCtrlBase* CCommandBarCtrlBase::s_pCurrentBar = NULL;
  137. __declspec(selectany) bool CCommandBarCtrlBase::s_bW2K = false;
  138. __declspec(selectany) bool CCommandBarCtrlBase::s_bStaticInit = false;
  139. /////////////////////////////////////////////////////////////////////////////
  140. // CCommandBarCtrl - ATL implementation of Command Bars
  141. template <class T, class TBase /*= CCommandBarCtrlBase */, class TWinTraits /*= CControlWinTraits */ >
  142. class ATL_NO_VTABLE CCommandBarCtrlImpl : public CWindowImpl< T, TBase, TWinTraits >
  143. {
  144. public:
  145. DECLARE_WND_SUPERCLASS(NULL, TBase::GetWndClassName())
  146. // Declarations
  147. struct _MenuItemData // menu item data
  148. {
  149. DWORD dwMagic;
  150. LPTSTR lpstrText;
  151. UINT fType;
  152. UINT fState;
  153. int iButton;
  154. _MenuItemData() { dwMagic = 0x1313; }
  155. bool IsCmdBarMenuItem() { return (dwMagic == 0x1313); }
  156. };
  157. struct _ToolBarData // toolbar resource data
  158. {
  159. WORD wVersion;
  160. WORD wWidth;
  161. WORD wHeight;
  162. WORD wItemCount;
  163. //WORD aItems[wItemCount]
  164. WORD* items()
  165. { return (WORD*)(this+1); }
  166. };
  167. // Constants
  168. enum _CmdBarDrawConstants
  169. {
  170. s_kcxGap = 1,
  171. s_kcxTextMargin = 2,
  172. s_kcxButtonMargin = 3,
  173. s_kcyButtonMargin = 3
  174. };
  175. enum
  176. {
  177. _nMaxMenuItemTextLength = 100,
  178. _chChevronShortcut = _T('/')
  179. };
  180. // Data members
  181. HMENU m_hMenu;
  182. HIMAGELIST m_hImageList;
  183. CSimpleValArray<WORD> m_arrCommand;
  184. CContainedWindow m_wndParent;
  185. CContainedWindow m_wndMDIClient;
  186. bool m_bMenuActive;
  187. bool m_bAttachedMenu;
  188. bool m_bImagesVisible;
  189. bool m_bPopupItem;
  190. bool m_bContextMenu;
  191. bool m_bEscapePressed;
  192. int m_nPopBtn;
  193. int m_nNextPopBtn;
  194. SIZE m_szBitmap;
  195. SIZE m_szButton;
  196. COLORREF m_clrMask;
  197. CFont m_fontMenu;
  198. bool m_bSkipMsg;
  199. UINT m_uSysKey;
  200. HWND m_hWndFocus; // Alternate focus mode
  201. DWORD m_dwExtendedStyle; // Command Bar specific extended styles
  202. bool m_bParentActive;
  203. // Constructor/destructor
  204. CCommandBarCtrlImpl() :
  205. m_hMenu(NULL),
  206. m_hImageList(NULL),
  207. m_wndParent(this, 1),
  208. m_bMenuActive(false),
  209. m_bAttachedMenu(false),
  210. m_nPopBtn(-1),
  211. m_nNextPopBtn(-1),
  212. m_bPopupItem(false),
  213. m_bImagesVisible(true),
  214. m_wndMDIClient(this, 2),
  215. m_bSkipMsg(false),
  216. m_uSysKey(0),
  217. m_hWndFocus(NULL),
  218. m_bContextMenu(false),
  219. m_bEscapePressed(false),
  220. m_clrMask(RGB(192, 192, 192)),
  221. m_dwExtendedStyle(CBR_EX_TRANSPARENT | CBR_EX_SHAREMENU),
  222. m_bParentActive(true)
  223. {
  224. m_szBitmap.cx = 16;
  225. m_szBitmap.cy = 15;
  226. m_szButton.cx = m_szBitmap.cx + s_kcxButtonMargin;
  227. m_szButton.cy = m_szBitmap.cy + s_kcyButtonMargin;
  228. }
  229. ~CCommandBarCtrlImpl()
  230. {
  231. if(m_wndParent.IsWindow())
  232. /*scary!*/ m_wndParent.UnsubclassWindow();
  233. if(m_wndMDIClient.IsWindow())
  234. /*scary!*/ m_wndMDIClient.UnsubclassWindow();
  235. if(m_hMenu != NULL && (m_dwExtendedStyle & CBR_EX_SHAREMENU) == 0)
  236. ::DestroyMenu(m_hMenu);
  237. if(m_hImageList != NULL)
  238. ::ImageList_Destroy(m_hImageList);
  239. }
  240. // Attributes
  241. DWORD GetCommandBarExtendedStyle() const
  242. {
  243. return m_dwExtendedStyle;
  244. }
  245. DWORD SetCommandBarExtendedStyle(DWORD dwExtendedStyle, DWORD dwMask = 0)
  246. {
  247. DWORD dwPrevStyle = m_dwExtendedStyle;
  248. if(dwMask == 0)
  249. m_dwExtendedStyle = dwExtendedStyle;
  250. else
  251. m_dwExtendedStyle = (m_dwExtendedStyle & ~dwMask) | (dwExtendedStyle & dwMask);
  252. return dwPrevStyle;
  253. }
  254. CMenuHandle GetMenu() const
  255. {
  256. ATLASSERT(::IsWindow(m_hWnd));
  257. return m_hMenu;
  258. }
  259. COLORREF GetImageMaskColor() const
  260. {
  261. ATLASSERT(::IsWindow(m_hWnd));
  262. return m_clrMask;
  263. }
  264. COLORREF SetImageMaskColor(COLORREF clrMask)
  265. {
  266. ATLASSERT(::IsWindow(m_hWnd));
  267. COLORREF clrOld = m_clrMask;
  268. m_clrMask = clrMask;
  269. return clrOld;
  270. }
  271. bool GetImagesVisible() const
  272. {
  273. ATLASSERT(::IsWindow(m_hWnd));
  274. return m_bImagesVisible;
  275. }
  276. bool SetImagesVisible(bool bVisible)
  277. {
  278. ATLASSERT(::IsWindow(m_hWnd));
  279. bool bOld = m_bImagesVisible;
  280. m_bImagesVisible = bVisible;
  281. return bOld;
  282. }
  283. void GetImageSize(SIZE& size) const
  284. {
  285. ATLASSERT(::IsWindow(m_hWnd));
  286. size = m_szBitmap;
  287. }
  288. bool SetImageSize(SIZE& size)
  289. {
  290. ATLASSERT(::IsWindow(m_hWnd));
  291. if(m_hImageList != NULL)
  292. {
  293. if(::ImageList_GetImageCount(m_hImageList) == 0) // empty
  294. {
  295. ::ImageList_Destroy(m_hImageList);
  296. m_hImageList = NULL;
  297. }
  298. else
  299. {
  300. return false; // can't set, image list exists
  301. }
  302. }
  303. if(size.cx == 0 || size.cy == 0)
  304. return false;
  305. m_szBitmap = size;
  306. m_szButton.cx = m_szBitmap.cx + s_kcxButtonMargin;
  307. m_szButton.cy = m_szBitmap.cy + s_kcyButtonMargin;
  308. return true;
  309. }
  310. HWND GetCmdBar() const
  311. {
  312. ATLASSERT(::IsWindow(m_hWnd));
  313. return (HWND)::SendMessage(m_hWnd, CBRM_GETCMDBAR, 0, 0L);
  314. }
  315. // Methods
  316. HWND Create(HWND hWndParent, RECT& rcPos, LPCTSTR szWindowName = NULL,
  317. DWORD dwStyle = 0, DWORD dwExStyle = 0,
  318. UINT nID = 0, LPVOID lpCreateParam = NULL)
  319. {
  320. // These styles are required for command bars
  321. dwStyle |= TBSTYLE_LIST | TBSTYLE_FLAT;
  322. return CWindowImpl< T, TBase, TWinTraits >::Create(hWndParent, rcPos, szWindowName, dwStyle, dwExStyle, nID, lpCreateParam);
  323. }
  324. BOOL AttachToWindow(HWND hWnd)
  325. {
  326. ATLASSERT(m_hWnd == NULL);
  327. ATLASSERT(::IsWindow(hWnd));
  328. BOOL bRet = SubclassWindow(hWnd);
  329. if(bRet)
  330. {
  331. m_bAttachedMenu = true;
  332. GetSystemSettings();
  333. }
  334. return bRet;
  335. }
  336. BOOL LoadMenu(_U_STRINGorID menu)
  337. {
  338. ATLASSERT(::IsWindow(m_hWnd));
  339. if(m_bAttachedMenu) // doesn't work in this mode
  340. return FALSE;
  341. if(menu.m_lpstr == NULL)
  342. return FALSE;
  343. HMENU hMenu = ::LoadMenu(_Module.GetResourceInstance(), menu.m_lpstr);
  344. if(hMenu == NULL)
  345. return FALSE;
  346. return AttachMenu(hMenu);
  347. }
  348. BOOL AttachMenu(HMENU hMenu)
  349. {
  350. ATLASSERT(::IsWindow(m_hWnd));
  351. ATLASSERT(::IsMenu(hMenu));
  352. if(hMenu != NULL && !::IsMenu(hMenu))
  353. return FALSE;
  354. // destroy old menu, if needed, and set new one
  355. if(m_hMenu != NULL && (m_dwExtendedStyle & CBR_EX_SHAREMENU) == 0)
  356. ::DestroyMenu(m_hMenu);
  357. m_hMenu = hMenu;
  358. if(m_bAttachedMenu) // Nothing else in this mode
  359. return TRUE;
  360. // Build buttons according to menu
  361. SetRedraw(FALSE);
  362. // Clear all
  363. BOOL bRet;
  364. int nCount = GetButtonCount();
  365. for(int i = 0; i < nCount; i++)
  366. {
  367. bRet = DeleteButton(0);
  368. ATLASSERT(bRet);
  369. }
  370. // Add buttons for each menu item
  371. if(m_hMenu != NULL)
  372. {
  373. int nItems = ::GetMenuItemCount(m_hMenu);
  374. T* pT = static_cast<T*>(this);
  375. pT; // avoid level 4 warning
  376. TCHAR szString[pT->_nMaxMenuItemTextLength];
  377. for(int i = 0; i < nItems; i++)
  378. {
  379. CMenuItemInfo mii;
  380. mii.fMask = MIIM_TYPE | MIIM_STATE | MIIM_SUBMENU;
  381. mii.fType = MFT_STRING;
  382. mii.dwTypeData = szString;
  383. mii.cch = pT->_nMaxMenuItemTextLength;
  384. bRet = ::GetMenuItemInfo(m_hMenu, i, TRUE, &mii);
  385. ATLASSERT(bRet);
  386. // If we have more than the buffer, we assume we have bitmaps bits
  387. if(lstrlen(szString) > pT->_nMaxMenuItemTextLength - 1)
  388. {
  389. mii.fType = MFT_BITMAP;
  390. ::SetMenuItemInfo(m_hMenu, i, TRUE, &mii);
  391. szString[0] = 0;
  392. }
  393. // NOTE: Command Bar currently supports only drop-down menu items
  394. ATLASSERT(mii.hSubMenu != NULL);
  395. TBBUTTON btn;
  396. btn.iBitmap = 0;
  397. btn.idCommand = i;
  398. btn.fsState = (BYTE)(((mii.fState & MFS_DISABLED) == 0) ? TBSTATE_ENABLED : 0);
  399. btn.fsStyle = TBSTYLE_BUTTON | TBSTYLE_AUTOSIZE | TBSTYLE_DROPDOWN;
  400. btn.dwData = 0;
  401. btn.iString = 0;
  402. bRet = InsertButton(-1, &btn);
  403. ATLASSERT(bRet);
  404. TBBUTTONINFO bi;
  405. memset(&bi, 0, sizeof(bi));
  406. bi.cbSize = sizeof(TBBUTTONINFO);
  407. bi.dwMask = TBIF_TEXT;
  408. bi.pszText = szString;
  409. bRet = SetButtonInfo(i, &bi);
  410. ATLASSERT(bRet);
  411. }
  412. }
  413. SetRedraw(TRUE);
  414. Invalidate();
  415. UpdateWindow();
  416. return TRUE;
  417. }
  418. BOOL LoadImages(_U_STRINGorID image)
  419. {
  420. return _LoadImagesHelper(image, false);
  421. }
  422. BOOL LoadMappedImages(UINT nIDImage, UINT nFlags = 0, LPCOLORMAP lpColorMap = NULL, int nMapSize = 0)
  423. {
  424. return _LoadImagesHelper(nIDImage, true, nFlags , lpColorMap, nMapSize);
  425. }
  426. BOOL _LoadImagesHelper(_U_STRINGorID image, bool bMapped, UINT nFlags = 0, LPCOLORMAP lpColorMap = NULL, int nMapSize = 0)
  427. {
  428. ATLASSERT(::IsWindow(m_hWnd));
  429. HINSTANCE hInstance = _Module.GetResourceInstance();
  430. HRSRC hRsrc = ::FindResource(hInstance, image.m_lpstr, (LPTSTR)RT_TOOLBAR);
  431. if(hRsrc == NULL)
  432. return FALSE;
  433. HGLOBAL hGlobal = ::LoadResource(hInstance, hRsrc);
  434. if(hGlobal == NULL)
  435. return FALSE;
  436. _ToolBarData* pData = (_ToolBarData*)::LockResource(hGlobal);
  437. if(pData == NULL)
  438. return FALSE;
  439. ATLASSERT(pData->wVersion == 1);
  440. WORD* pItems = pData->items();
  441. int nItems = pData->wItemCount;
  442. // Add bitmap to our image list (create it if it doesn't exist)
  443. if(m_hImageList == NULL)
  444. {
  445. m_hImageList = ::ImageList_Create(pData->wWidth, pData->wHeight, ILC_COLOR | ILC_MASK, pData->wItemCount, 1);
  446. ATLASSERT(m_hImageList != NULL);
  447. if(m_hImageList == NULL)
  448. return FALSE;
  449. }
  450. CBitmap bmp;
  451. if(bMapped)
  452. {
  453. ATLASSERT(HIWORD(PtrToUlong(image.m_lpstr)) == 0); // if mapped, must be a numeric ID
  454. int nIDImage = (int)(short)LOWORD(PtrToUlong(image.m_lpstr));
  455. bmp.LoadMappedBitmap(nIDImage, (WORD)nFlags, lpColorMap, nMapSize);
  456. }
  457. else
  458. {
  459. bmp.LoadBitmap(image.m_lpstr);
  460. }
  461. ATLASSERT(bmp.m_hBitmap != NULL);
  462. if(bmp.m_hBitmap == NULL)
  463. return FALSE;
  464. if(::ImageList_AddMasked(m_hImageList, bmp, m_clrMask) == -1)
  465. return FALSE;
  466. // Fill the array with command IDs
  467. for(int i = 0; i < nItems; i++)
  468. {
  469. if(pItems[i] != 0)
  470. m_arrCommand.Add(pItems[i]);
  471. }
  472. ATLASSERT(::ImageList_GetImageCount(m_hImageList) == m_arrCommand.GetSize());
  473. if(::ImageList_GetImageCount(m_hImageList) != m_arrCommand.GetSize())
  474. return FALSE;
  475. // Set some internal stuff
  476. m_szBitmap.cx = pData->wWidth;
  477. m_szBitmap.cy = pData->wHeight;
  478. m_szButton.cx = m_szBitmap.cx + 2 * s_kcxButtonMargin;
  479. m_szButton.cy = m_szBitmap.cy + 2 * s_kcyButtonMargin;
  480. return TRUE;
  481. }
  482. BOOL AddBitmap(_U_STRINGorID bitmap, int nCommandID)
  483. {
  484. ATLASSERT(::IsWindow(m_hWnd));
  485. CBitmap bmp;
  486. bmp.LoadBitmap(bitmap.m_lpstr);
  487. if(bmp.m_hBitmap == NULL)
  488. return FALSE;
  489. return AddBitmap(bmp, nCommandID);
  490. }
  491. BOOL AddBitmap(HBITMAP hBitmap, UINT nCommandID)
  492. {
  493. // Create image list if it doesn't exist
  494. if(m_hImageList == NULL)
  495. {
  496. m_hImageList = ::ImageList_Create(m_szBitmap.cx, m_szBitmap.cy, ILC_COLOR | ILC_MASK, 1, 1);
  497. if(m_hImageList == NULL)
  498. return FALSE;
  499. }
  500. // check bitmap size
  501. CBitmapHandle bmp = hBitmap;
  502. SIZE size = { 0, 0 };
  503. bmp.GetSize(size);
  504. if(size.cx != m_szBitmap.cx || size.cy != m_szBitmap.cy)
  505. {
  506. ATLASSERT(FALSE); // must match size!
  507. return FALSE;
  508. }
  509. // add bitmap
  510. int nRet = ::ImageList_AddMasked(m_hImageList, hBitmap, m_clrMask);
  511. if(nRet == -1)
  512. return FALSE;
  513. BOOL bRet = m_arrCommand.Add((WORD)nCommandID);
  514. ATLASSERT(::ImageList_GetImageCount(m_hImageList) == m_arrCommand.GetSize());
  515. return bRet;
  516. }
  517. BOOL AddIcon(HICON hIcon, UINT nCommandID)
  518. {
  519. // create image list if it doesn't exist
  520. if(m_hImageList == NULL)
  521. {
  522. m_hImageList = ::ImageList_Create(m_szBitmap.cx, m_szBitmap.cy, ILC_COLOR | ILC_MASK, 1, 1);
  523. if(m_hImageList == NULL)
  524. return FALSE;
  525. }
  526. int nRet = ::ImageList_AddIcon(m_hImageList, hIcon);
  527. if(nRet == -1)
  528. return FALSE;
  529. BOOL bRet = m_arrCommand.Add((WORD)nCommandID);
  530. ATLASSERT(::ImageList_GetImageCount(m_hImageList) == m_arrCommand.GetSize());
  531. return bRet;
  532. }
  533. BOOL ReplaceBitmap(_U_STRINGorID bitmap, int nCommandID)
  534. {
  535. ATLASSERT(::IsWindow(m_hWnd));
  536. CBitmap bmp;
  537. bmp.LoadBitmap(bitmap.m_lpstr);
  538. if(bmp.m_hBitmap == NULL)
  539. return FALSE;
  540. return ReplaceBitmap(bmp, nCommandID);
  541. }
  542. BOOL ReplaceBitmap(HBITMAP hBitmap, UINT nCommandID)
  543. {
  544. BOOL bRet = FALSE;
  545. for(int i = 0; i < m_arrCommand.GetSize(); i++)
  546. {
  547. if(m_arrCommand[i] == nCommandID)
  548. {
  549. bRet = ::ImageList_Remove(m_hImageList, i);
  550. if(bRet)
  551. m_arrCommand.RemoveAt(i);
  552. break;
  553. }
  554. }
  555. if(bRet)
  556. bRet = AddBitmap(hBitmap, nCommandID);
  557. return bRet;
  558. }
  559. BOOL ReplaceIcon(HICON hIcon, UINT nCommandID)
  560. {
  561. BOOL bRet = FALSE;
  562. for(int i = 0; i < m_arrCommand.GetSize(); i++)
  563. {
  564. if(m_arrCommand[i] == nCommandID)
  565. {
  566. bRet = (::ImageList_ReplaceIcon(m_hImageList, i, hIcon) != -1);
  567. break;
  568. }
  569. }
  570. return bRet;
  571. }
  572. BOOL RemoveImage(int nCommandID)
  573. {
  574. ATLASSERT(::IsWindow(m_hWnd));
  575. BOOL bRet = FALSE;
  576. for(int i = 0; i < m_arrCommand.GetSize(); i++)
  577. {
  578. if(m_arrCommand[i] == nCommandID)
  579. {
  580. bRet = ::ImageList_Remove(m_hImageList, i);
  581. if(bRet)
  582. m_arrCommand.RemoveAt(i);
  583. break;
  584. }
  585. }
  586. return bRet;
  587. }
  588. BOOL RemoveAllImages()
  589. {
  590. ATLASSERT(::IsWindow(m_hWnd));
  591. ATLTRACE2(atlTraceUI, 0, "CmdBar - Removing all images\n");
  592. BOOL bRet = ::ImageList_RemoveAll(m_hImageList);
  593. if(bRet)
  594. m_arrCommand.RemoveAll();
  595. return bRet;
  596. }
  597. BOOL TrackPopupMenu(HMENU hMenu, UINT uFlags, int x, int y, LPTPMPARAMS lpParams = NULL)
  598. {
  599. ATLASSERT(::IsWindow(m_hWnd));
  600. ATLASSERT(::IsMenu(hMenu));
  601. if(!::IsMenu(hMenu))
  602. return FALSE;
  603. m_bContextMenu = true;
  604. return DoTrackPopupMenu(hMenu, uFlags, x, y, lpParams);
  605. }
  606. // NOTE: Limited support for MDI - no icon or min/max/close buttons
  607. BOOL SetMDIClient(HWND hWndMDIClient)
  608. {
  609. ATLASSERT(::IsWindow(m_hWnd));
  610. ATLASSERT(::IsWindow(hWndMDIClient));
  611. if(!::IsWindow(hWndMDIClient))
  612. return FALSE;
  613. #ifdef _DEBUG
  614. LPCTSTR lpszMDIClientClass = _T("MDICLIENT");
  615. int nNameLen = lstrlen(lpszMDIClientClass) + 1;
  616. LPTSTR lpstrClassName = (LPTSTR)_alloca(nNameLen * sizeof(TCHAR));
  617. ::GetClassName(hWndMDIClient, lpstrClassName, nNameLen);
  618. ATLASSERT(lstrcmpi(lpstrClassName, lpszMDIClientClass) == 0);
  619. if(lstrcmpi(lpstrClassName, lpszMDIClientClass) != 0)
  620. return FALSE; // not an "MDIClient" window
  621. #endif //_DEBUG
  622. if(m_wndMDIClient.IsWindow())
  623. m_wndMDIClient.UnsubclassWindow(); // scary!
  624. return m_wndMDIClient.SubclassWindow(hWndMDIClient);
  625. }
  626. // Message map and handlers
  627. BEGIN_MSG_MAP(CCommandBarCtrlImpl)
  628. MESSAGE_HANDLER(WM_CREATE, OnCreate)
  629. MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
  630. MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBackground)
  631. MESSAGE_HANDLER(WM_INITMENU, OnInitMenu)
  632. MESSAGE_HANDLER(WM_INITMENUPOPUP, OnInitMenuPopup)
  633. MESSAGE_HANDLER(WM_MENUSELECT, OnMenuSelect)
  634. MESSAGE_HANDLER(GetAutoPopupMessage(), OnInternalAutoPopup)
  635. MESSAGE_HANDLER(GetGetBarMessage(), OnInternalGetBar)
  636. MESSAGE_HANDLER(WM_SETTINGCHANGE, OnSettingChange)
  637. MESSAGE_HANDLER(WM_MENUCHAR, OnMenuChar)
  638. MESSAGE_HANDLER(WM_KEYDOWN, OnKeyDown)
  639. MESSAGE_HANDLER(WM_KEYUP, OnKeyUp)
  640. MESSAGE_HANDLER(WM_CHAR, OnChar)
  641. MESSAGE_HANDLER(WM_SYSKEYDOWN, OnSysKeyDown)
  642. MESSAGE_HANDLER(WM_SYSKEYUP, OnSysKeyUp)
  643. MESSAGE_HANDLER(WM_SYSCHAR, OnSysChar)
  644. // public API handlers - these stay to support chevrons in atlframe.h
  645. MESSAGE_HANDLER(CBRM_GETMENU, OnAPIGetMenu)
  646. MESSAGE_HANDLER(CBRM_TRACKPOPUPMENU, OnAPITrackPopupMenu)
  647. MESSAGE_HANDLER(CBRM_GETCMDBAR, OnAPIGetCmdBar)
  648. MESSAGE_HANDLER(WM_DRAWITEM, OnDrawItem)
  649. MESSAGE_HANDLER(WM_MEASUREITEM, OnMeasureItem)
  650. MESSAGE_HANDLER(WM_FORWARDMSG, OnForwardMsg)
  651. ALT_MSG_MAP(1) // Parent window messages
  652. NOTIFY_CODE_HANDLER(TBN_HOTITEMCHANGE, OnParentHotItemChange)
  653. NOTIFY_CODE_HANDLER(TBN_DROPDOWN, OnParentDropDown)
  654. MESSAGE_HANDLER(WM_INITMENUPOPUP, OnParentInitMenuPopup)
  655. MESSAGE_HANDLER(GetGetBarMessage(), OnParentInternalGetBar)
  656. MESSAGE_HANDLER(WM_SYSCOMMAND, OnParentSysCommand)
  657. MESSAGE_HANDLER(CBRM_GETMENU, OnParentAPIGetMenu)
  658. MESSAGE_HANDLER(WM_MENUCHAR, OnParentMenuChar)
  659. MESSAGE_HANDLER(CBRM_TRACKPOPUPMENU, OnParentAPITrackPopupMenu)
  660. MESSAGE_HANDLER(CBRM_GETCMDBAR, OnParentAPIGetCmdBar)
  661. MESSAGE_HANDLER(WM_DRAWITEM, OnParentDrawItem)
  662. MESSAGE_HANDLER(WM_MEASUREITEM, OnParentMeasureItem)
  663. MESSAGE_HANDLER(WM_ACTIVATE, OnParentActivate)
  664. NOTIFY_CODE_HANDLER(NM_CUSTOMDRAW, OnParentCustomDraw)
  665. ALT_MSG_MAP(2) // MDI client window messages
  666. MESSAGE_HANDLER(WM_MDISETMENU, OnMDISetMenu)
  667. ALT_MSG_MAP(3) // Message hook messages
  668. MESSAGE_HANDLER(WM_MOUSEMOVE, OnHookMouseMove)
  669. MESSAGE_HANDLER(WM_SYSKEYDOWN, OnHookSysKeyDown)
  670. MESSAGE_HANDLER(WM_SYSKEYUP, OnHookSysKeyUp)
  671. MESSAGE_HANDLER(WM_SYSCHAR, OnHookSysChar)
  672. MESSAGE_HANDLER(WM_KEYDOWN, OnHookKeyDown)
  673. MESSAGE_HANDLER(WM_NEXTMENU, OnHookNextMenu)
  674. MESSAGE_HANDLER(WM_CHAR, OnHookChar)
  675. END_MSG_MAP()
  676. LRESULT OnForwardMsg(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
  677. {
  678. LPMSG pMsg = (LPMSG)lParam;
  679. LRESULT lRet = 0;
  680. ProcessWindowMessage(pMsg->hwnd, pMsg->message, pMsg->wParam, pMsg->lParam, lRet, 3);
  681. return lRet;
  682. }
  683. LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
  684. {
  685. // Let the toolbar initialize itself
  686. LRESULT lRet = DefWindowProc(uMsg, wParam, lParam);
  687. // get and use system settings
  688. GetSystemSettings();
  689. // Parent init
  690. CWindow wndParent = GetParent();
  691. CWindow wndTopLevelParent = wndParent.GetTopLevelParent();
  692. m_wndParent.SubclassWindow(wndTopLevelParent);
  693. // Toolbar Init
  694. SetButtonStructSize();
  695. SetImageList(NULL);
  696. // Create message hook if needed
  697. ::EnterCriticalSection(&_Module.m_csWindowCreate);
  698. if(s_pmapMsgHook == NULL)
  699. {
  700. ATLTRY(s_pmapMsgHook = new CMsgHookMap);
  701. ATLASSERT(s_pmapMsgHook != NULL);
  702. }
  703. if(s_pmapMsgHook != NULL)
  704. {
  705. DWORD dwThreadID = ::GetCurrentThreadId();
  706. _MsgHookData* pData = s_pmapMsgHook->Lookup(dwThreadID);
  707. if(pData == NULL)
  708. {
  709. ATLTRY(pData = new _MsgHookData);
  710. ATLASSERT(pData != NULL);
  711. HHOOK hMsgHook = ::SetWindowsHookEx(WH_GETMESSAGE, MessageHookProc, _Module.GetModuleInstance(), dwThreadID);
  712. ATLASSERT(hMsgHook != NULL);
  713. if(pData != NULL && hMsgHook != NULL)
  714. {
  715. pData->hMsgHook = hMsgHook;
  716. pData->dwUsage = 1;
  717. BOOL bRet = s_pmapMsgHook->Add(dwThreadID, pData);
  718. bRet;
  719. ATLASSERT(bRet);
  720. }
  721. }
  722. else
  723. {
  724. (pData->dwUsage)++;
  725. }
  726. }
  727. ::LeaveCriticalSection(&_Module.m_csWindowCreate);
  728. return lRet;
  729. }
  730. LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
  731. {
  732. LRESULT lRet = DefWindowProc(uMsg, wParam, lParam);
  733. ::EnterCriticalSection(&_Module.m_csWindowCreate);
  734. ATLASSERT(s_pmapMsgHook != NULL);
  735. if(s_pmapMsgHook != NULL)
  736. {
  737. DWORD dwThreadID = ::GetCurrentThreadId();
  738. _MsgHookData* pData = s_pmapMsgHook->Lookup(dwThreadID);
  739. if(pData != NULL)
  740. {
  741. (pData->dwUsage)--;
  742. if(pData->dwUsage == 0)
  743. {
  744. BOOL bRet = ::UnhookWindowsHookEx(pData->hMsgHook);
  745. ATLASSERT(bRet);
  746. bRet = s_pmapMsgHook->Remove(dwThreadID);
  747. ATLASSERT(bRet);
  748. if(bRet)
  749. delete pData;
  750. }
  751. if(s_pmapMsgHook->GetSize() == 0)
  752. {
  753. delete s_pmapMsgHook;
  754. s_pmapMsgHook = NULL;
  755. }
  756. }
  757. }
  758. ::LeaveCriticalSection(&_Module.m_csWindowCreate);
  759. return lRet;
  760. }
  761. LRESULT OnKeyDown(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  762. {
  763. #ifdef _CMDBAR_EXTRA_TRACE
  764. ATLTRACE2(atlTraceUI, 0, "CmdBar - OnKeyDown\n");
  765. #endif
  766. // Simulate Alt+Space for the parent
  767. if(wParam == VK_SPACE)
  768. {
  769. m_wndParent.PostMessage(WM_SYSKEYDOWN, wParam, lParam | (1 << 29));
  770. bHandled = TRUE;
  771. return 0;
  772. }
  773. bHandled = FALSE;
  774. return 0;
  775. }
  776. LRESULT OnKeyUp(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled)
  777. {
  778. #ifdef _CMDBAR_EXTRA_TRACE
  779. ATLTRACE2(atlTraceUI, 0, "CmdBar - OnKeyUp\n");
  780. #endif
  781. if(wParam != VK_SPACE)
  782. bHandled = FALSE;
  783. return 0;
  784. }
  785. LRESULT OnChar(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled)
  786. {
  787. #ifdef _CMDBAR_EXTRA_TRACE
  788. ATLTRACE2(atlTraceUI, 0, "CmdBar - OnChar\n");
  789. #endif
  790. if(wParam != VK_SPACE)
  791. bHandled = FALSE;
  792. else
  793. return 0;
  794. // Security
  795. if(!m_wndParent.IsWindowEnabled() || ::GetFocus() != m_hWnd)
  796. return 0;
  797. // Handle mnemonic press when we have focus
  798. int nID = 0;
  799. if(wParam != VK_RETURN && !MapAccelerator((TCHAR)LOWORD(wParam), nID))
  800. {
  801. ::MessageBeep(0);
  802. }
  803. else
  804. {
  805. PostMessage(WM_KEYDOWN, VK_DOWN, 0L);
  806. if(wParam != VK_RETURN)
  807. SetHotItem(nID);
  808. }
  809. return 0;
  810. }
  811. LRESULT OnSysKeyDown(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
  812. {
  813. #ifdef _CMDBAR_EXTRA_TRACE
  814. ATLTRACE2(atlTraceUI, 0, "CmdBar - OnSysKeyDown\n");
  815. #endif
  816. bHandled = FALSE;
  817. return 0;
  818. }
  819. LRESULT OnSysKeyUp(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
  820. {
  821. #ifdef _CMDBAR_EXTRA_TRACE
  822. ATLTRACE2(atlTraceUI, 0, "CmdBar - OnSysKeyUp\n");
  823. #endif
  824. bHandled = FALSE;
  825. return 0;
  826. }
  827. LRESULT OnSysChar(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
  828. {
  829. #ifdef _CMDBAR_EXTRA_TRACE
  830. ATLTRACE2(atlTraceUI, 0, "CmdBar - OnSysChar\n");
  831. #endif
  832. bHandled = FALSE;
  833. return 0;
  834. }
  835. LRESULT OnEraseBackground(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled)
  836. {
  837. if(m_bAttachedMenu || (m_dwExtendedStyle & CBR_EX_TRANSPARENT))
  838. {
  839. bHandled = FALSE;
  840. return 0;
  841. }
  842. RECT rect;
  843. GetClientRect(&rect);
  844. ::FillRect((HDC)wParam, &rect, (HBRUSH)LongToPtr(COLOR_MENU + 1));
  845. return 1; // don't do the default erase
  846. }
  847. LRESULT OnInitMenu(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
  848. {
  849. int nIndex = GetHotItem();
  850. SendMessage(WM_MENUSELECT, MAKEWPARAM(nIndex, MF_POPUP|MF_HILITE), (LPARAM)m_hMenu);
  851. bHandled = FALSE;
  852. return 1;
  853. }
  854. LRESULT OnInitMenuPopup(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  855. {
  856. if((BOOL)HIWORD(lParam)) // System menu, do nothing
  857. {
  858. bHandled = FALSE;
  859. return 1;
  860. }
  861. if(!(m_bAttachedMenu || m_bMenuActive)) // Not attached or ours, do nothing
  862. {
  863. bHandled = FALSE;
  864. return 1;
  865. }
  866. ATLTRACE2(atlTraceUI, 0, "CmdBar - OnInitMenuPopup\n");
  867. // forward to the parent or subclassed window, so it can handle update UI
  868. LRESULT lRet = 0;
  869. if(m_bAttachedMenu)
  870. lRet = DefWindowProc(uMsg, wParam, (lParam || m_bContextMenu) ? lParam : GetHotItem());
  871. else
  872. lRet = m_wndParent.DefWindowProc(uMsg, wParam, (lParam || m_bContextMenu) ? lParam : GetHotItem());
  873. // Convert menu items to ownerdraw, add our data
  874. if(m_bImagesVisible)
  875. {
  876. CMenuHandle menuPopup = (HMENU)wParam;
  877. ATLASSERT(menuPopup.m_hMenu != NULL);
  878. T* pT = static_cast<T*>(this);
  879. pT; // avoid level 4 warning
  880. TCHAR szString[pT->_nMaxMenuItemTextLength];
  881. BOOL bRet;
  882. for(int i = 0; i < menuPopup.GetMenuItemCount(); i++)
  883. {
  884. CMenuItemInfo mii;
  885. mii.cch = pT->_nMaxMenuItemTextLength;
  886. mii.fMask = MIIM_CHECKMARKS | MIIM_DATA | MIIM_ID | MIIM_STATE | MIIM_SUBMENU | MIIM_TYPE;
  887. mii.dwTypeData = szString;
  888. bRet = menuPopup.GetMenuItemInfo(i, TRUE, &mii);
  889. ATLASSERT(bRet);
  890. if(!(mii.fType & MFT_OWNERDRAW)) // Not already an ownerdraw item
  891. {
  892. mii.fMask = MIIM_DATA | MIIM_TYPE | MIIM_STATE;
  893. _MenuItemData* pMI = NULL;
  894. ATLTRY(pMI = new _MenuItemData);
  895. ATLASSERT(pMI != NULL);
  896. if(pMI != NULL)
  897. {
  898. pMI->fType = mii.fType;
  899. pMI->fState = mii.fState;
  900. mii.fType |= MFT_OWNERDRAW;
  901. pMI->iButton = -1;
  902. for(int j = 0; j < m_arrCommand.GetSize(); j++)
  903. {
  904. if(m_arrCommand[j] == mii.wID)
  905. {
  906. pMI->iButton = j;
  907. break;
  908. }
  909. }
  910. pMI->lpstrText = NULL;
  911. ATLTRY(pMI->lpstrText = new TCHAR[lstrlen(szString) + 1]);
  912. ATLASSERT(pMI->lpstrText != NULL);
  913. if(pMI->lpstrText != NULL)
  914. lstrcpy(pMI->lpstrText, szString);
  915. mii.dwItemData = (ULONG_PTR)pMI;
  916. bRet = menuPopup.SetMenuItemInfo(i, TRUE, &mii);
  917. ATLASSERT(bRet);
  918. }
  919. }
  920. }
  921. // Add it to the list
  922. m_stackMenuHandle.Push(menuPopup.m_hMenu);
  923. }
  924. return lRet;
  925. }
  926. LRESULT OnMenuSelect(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  927. {
  928. if(!m_bAttachedMenu) // Not attached, do nothing, forward to parent
  929. {
  930. m_bPopupItem = (lParam != NULL) && ((HMENU)lParam != m_hMenu) && (HIWORD(wParam) & MF_POPUP);
  931. if(m_wndParent.IsWindow())
  932. m_wndParent.SendMessage(uMsg, wParam, lParam);
  933. bHandled = FALSE;
  934. return 1;
  935. }
  936. // Check if a menu is closing, do a cleanup
  937. if(HIWORD(wParam) == 0xFFFF && lParam == NULL) // Menu closing
  938. {
  939. #ifdef _CMDBAR_EXTRA_TRACE
  940. ATLTRACE2(atlTraceUI, 0, "CmdBar - OnMenuSelect - CLOSING!!!!\n");
  941. #endif
  942. ATLASSERT(m_stackMenuWnd.GetSize() == 0);
  943. // Restore the menu items to the previous state for all menus that were converted
  944. if(m_bImagesVisible)
  945. {
  946. HMENU hMenu;
  947. while((hMenu = m_stackMenuHandle.Pop()) != NULL)
  948. {
  949. CMenuHandle menuPopup = hMenu;
  950. ATLASSERT(menuPopup.m_hMenu != NULL);
  951. // Restore state and delete menu item data
  952. BOOL bRet;
  953. for(int i = 0; i < menuPopup.GetMenuItemCount(); i++)
  954. {
  955. CMenuItemInfo mii;
  956. mii.fMask = MIIM_DATA | MIIM_TYPE;
  957. bRet = menuPopup.GetMenuItemInfo(i, TRUE, &mii);
  958. ATLASSERT(bRet);
  959. _MenuItemData* pMI = (_MenuItemData*)mii.dwItemData;
  960. if(pMI != NULL && pMI->IsCmdBarMenuItem())
  961. {
  962. mii.fMask = MIIM_DATA | MIIM_TYPE | MIIM_STATE;
  963. mii.fType = pMI->fType;
  964. mii.dwTypeData = pMI->lpstrText;
  965. mii.cch = lstrlen(pMI->lpstrText);
  966. mii.dwItemData = NULL;
  967. bRet = menuPopup.SetMenuItemInfo(i, TRUE, &mii);
  968. ATLASSERT(bRet);
  969. delete [] pMI->lpstrText;
  970. pMI->dwMagic = 0x6666;
  971. delete pMI;
  972. }
  973. }
  974. }
  975. }
  976. }
  977. bHandled = FALSE;
  978. return 1;
  979. }
  980. LRESULT OnInternalAutoPopup(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
  981. {
  982. int nIndex = (int)wParam;
  983. DoPopupMenu(nIndex, false);
  984. return 0;
  985. }
  986. LRESULT OnInternalGetBar(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
  987. {
  988. // Let's make sure we're not embedded in another process
  989. if(wParam && !::IsBadWritePtr((LPVOID)wParam, sizeof DWORD))
  990. *((DWORD*)wParam) = GetCurrentProcessId();
  991. if(IsWindowVisible())
  992. return (LRESULT)static_cast<CCommandBarCtrlBase*>(this);
  993. else
  994. return NULL;
  995. }
  996. LRESULT OnSettingChange(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
  997. {
  998. GetSystemSettings();
  999. return 0;
  1000. }
  1001. LRESULT OnWindowPosChanging(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
  1002. {
  1003. LRESULT lRet = DefWindowProc(uMsg, wParam, lParam);
  1004. LPWINDOWPOS lpWP = (LPWINDOWPOS)lParam;
  1005. int cyMin = ::GetSystemMetrics(SM_CYMENU);
  1006. if(lpWP->cy < cyMin)
  1007. lpWP->cy = cyMin;
  1008. return lRet;
  1009. }
  1010. LRESULT OnMenuChar(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  1011. {
  1012. #ifdef _CMDBAR_EXTRA_TRACE
  1013. ATLTRACE2(atlTraceUI, 0, "CmdBar - OnMenuChar\n");
  1014. #endif
  1015. bHandled = TRUE;
  1016. LRESULT lRet;
  1017. if(m_bMenuActive && LOWORD(wParam) != 0x0D)
  1018. lRet = 0;
  1019. else
  1020. lRet = MAKELRESULT(1, 1);
  1021. if(m_bMenuActive && HIWORD(wParam) == MF_POPUP)
  1022. {
  1023. T* pT = static_cast<T*>(this);
  1024. pT; // avoid level 4 warning
  1025. // Convert character to lower/uppercase and possibly Unicode, using current keyboard layout
  1026. TCHAR ch = (TCHAR)LOWORD(wParam);
  1027. CMenuHandle menu = (HMENU)lParam;
  1028. int nCount = ::GetMenuItemCount(menu);
  1029. int nRetCode = MNC_EXECUTE;
  1030. BOOL bRet;
  1031. TCHAR szString[pT->_nMaxMenuItemTextLength];
  1032. WORD wMnem = 0;
  1033. bool bFound = false;
  1034. for(int i = 0; i < nCount; i++)
  1035. {
  1036. CMenuItemInfo mii;
  1037. mii.cch = pT->_nMaxMenuItemTextLength;
  1038. mii.fMask = MIIM_CHECKMARKS | MIIM_DATA | MIIM_ID | MIIM_STATE | MIIM_SUBMENU | MIIM_TYPE;
  1039. mii.dwTypeData = szString;
  1040. bRet = menu.GetMenuItemInfo(i, TRUE, &mii);
  1041. if(!bRet || (mii.fType & MFT_SEPARATOR))
  1042. continue;
  1043. _MenuItemData* pmd = (_MenuItemData*)mii.dwItemData;
  1044. if(pmd != NULL && pmd->IsCmdBarMenuItem())
  1045. {
  1046. LPTSTR p = pmd->lpstrText;
  1047. if(p != NULL)
  1048. {
  1049. while(*p && *p != _T('&'))
  1050. p = ::CharNext(p);
  1051. if(p != NULL && *p)
  1052. {
  1053. DWORD dwP = MAKELONG(*(++p), 0);
  1054. DWORD dwC = MAKELONG(ch, 0);
  1055. if(::CharLower((LPTSTR)ULongToPtr(dwP)) == ::CharLower((LPTSTR)ULongToPtr(dwC)))
  1056. {
  1057. if(!bFound)
  1058. {
  1059. wMnem = (WORD)i;
  1060. bFound = true;
  1061. PostMessage(TB_SETHOTITEM, (WPARAM)-1, 0L);
  1062. GiveFocusBack();
  1063. }
  1064. else
  1065. {
  1066. nRetCode = MNC_SELECT;
  1067. break;
  1068. }
  1069. }
  1070. }
  1071. }
  1072. }
  1073. }
  1074. if(bFound)
  1075. {
  1076. bHandled = TRUE;
  1077. lRet = MAKELRESULT(wMnem, nRetCode);
  1078. }
  1079. }
  1080. else if(!m_bMenuActive)
  1081. {
  1082. int nID = 0;
  1083. if(!MapAccelerator((TCHAR)LOWORD(wParam), nID))
  1084. {
  1085. bHandled = FALSE;
  1086. PostMessage(TB_SETHOTITEM, (WPARAM)-1, 0L);
  1087. GiveFocusBack();
  1088. #if (_WIN32_IE >= 0x0500)
  1089. // check if we should display chevron menu
  1090. T* pT = static_cast<T*>(this);
  1091. pT; // avoid level 4 warning
  1092. if((TCHAR)LOWORD(wParam) == pT->_chChevronShortcut)
  1093. {
  1094. // assume we are in a rebar
  1095. HWND hWndReBar = GetParent();
  1096. int nCount = (int)::SendMessage(hWndReBar, RB_GETBANDCOUNT, 0, 0L);
  1097. for(int i = 0; i < nCount; i++)
  1098. {
  1099. REBARBANDINFO rbbi = { sizeof(REBARBANDINFO), RBBIM_CHILD | RBBIM_STYLE };
  1100. BOOL bRet = (BOOL)::SendMessage(hWndReBar, RB_GETBANDINFO, i, (LPARAM)&rbbi);
  1101. if(bRet && rbbi.hwndChild == m_hWnd && (rbbi.fStyle & RBBS_USECHEVRON) != 0)
  1102. {
  1103. ::PostMessage(hWndReBar, RB_PUSHCHEVRON, i, 0L);
  1104. PostMessage(WM_KEYDOWN, VK_DOWN, 0L);
  1105. bHandled = TRUE;
  1106. break;
  1107. }
  1108. }
  1109. }
  1110. #endif //(_WIN32_IE >= 0x0500)
  1111. }
  1112. else if(m_wndParent.IsWindowEnabled())
  1113. {
  1114. TakeFocus();
  1115. PostMessage(WM_KEYDOWN, VK_DOWN, 0L);
  1116. SetHotItem(nID);
  1117. }
  1118. }
  1119. return lRet;
  1120. }
  1121. LRESULT OnDrawItem(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& bHandled)
  1122. {
  1123. LPDRAWITEMSTRUCT lpDrawItemStruct = (LPDRAWITEMSTRUCT)lParam;
  1124. _MenuItemData* pmd = (_MenuItemData*)lpDrawItemStruct->itemData;
  1125. if(lpDrawItemStruct->CtlType == ODT_MENU && pmd->IsCmdBarMenuItem())
  1126. {
  1127. T* pT = static_cast<T*>(this);
  1128. pT->DrawItem(lpDrawItemStruct);
  1129. }
  1130. else
  1131. {
  1132. bHandled = FALSE;
  1133. }
  1134. return (LRESULT)TRUE;
  1135. }
  1136. LRESULT OnMeasureItem(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& bHandled)
  1137. {
  1138. LPMEASUREITEMSTRUCT lpMeasureItemStruct = (LPMEASUREITEMSTRUCT)lParam;
  1139. _MenuItemData* pmd = (_MenuItemData*)lpMeasureItemStruct->itemData;
  1140. if(lpMeasureItemStruct->CtlType == ODT_MENU && pmd->IsCmdBarMenuItem())
  1141. {
  1142. T* pT = static_cast<T*>(this);
  1143. pT->MeasureItem(lpMeasureItemStruct);
  1144. }
  1145. else
  1146. {
  1147. bHandled = FALSE;
  1148. }
  1149. return (LRESULT)TRUE;
  1150. }
  1151. // API message handlers
  1152. LRESULT OnAPIGetMenu(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
  1153. {
  1154. return (LRESULT)m_hMenu;
  1155. }
  1156. LRESULT OnAPITrackPopupMenu(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
  1157. {
  1158. if(lParam == NULL)
  1159. return FALSE;
  1160. LPCBRPOPUPMENU lpCBRPopupMenu = (LPCBRPOPUPMENU)lParam;
  1161. if(lpCBRPopupMenu->cbSize != sizeof(CBRPOPUPMENU))
  1162. return FALSE;
  1163. if(!::IsMenu(lpCBRPopupMenu->hMenu))
  1164. return FALSE;
  1165. m_bContextMenu = true;
  1166. return DoTrackPopupMenu(lpCBRPopupMenu->hMenu, lpCBRPopupMenu->uFlags, lpCBRPopupMenu->x, lpCBRPopupMenu->y, lpCBRPopupMenu->lptpm);
  1167. }
  1168. LRESULT OnAPIGetCmdBar(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
  1169. {
  1170. return (LRESULT)m_hWnd;
  1171. }
  1172. // Parent window message handlers
  1173. // Do not hot track if application in background, OK for modeless dialogs
  1174. LRESULT OnParentHotItemChange(int /*idCtrl*/, LPNMHDR pnmh, BOOL& bHandled)
  1175. {
  1176. DWORD dwProcessID;
  1177. ::GetWindowThreadProcessId(::GetActiveWindow(), &dwProcessID);
  1178. LPNMTBHOTITEM lpNMHT = (LPNMTBHOTITEM)pnmh;
  1179. // Check if this comes from us
  1180. if(pnmh->hwndFrom != m_hWnd)
  1181. {
  1182. bHandled = FALSE;
  1183. return 0;
  1184. }
  1185. if((!m_wndParent.IsWindowEnabled() || ::GetCurrentProcessId() != dwProcessID) && lpNMHT->dwFlags & HICF_MOUSE)
  1186. {
  1187. return 1;
  1188. }
  1189. else
  1190. {
  1191. #ifndef HICF_LMOUSE
  1192. const DWORD HICF_LMOUSE = 0x00000080; // left mouse button selected
  1193. #endif
  1194. bHandled = FALSE;
  1195. // Send WM_MENUSELECT to the app if it needs to display a status text
  1196. if(!(lpNMHT->dwFlags & HICF_MOUSE)
  1197. && !(lpNMHT->dwFlags & HICF_ACCELERATOR)
  1198. && !(lpNMHT->dwFlags & HICF_LMOUSE))
  1199. {
  1200. if(lpNMHT->dwFlags & HICF_ENTERING)
  1201. m_wndParent.SendMessage(WM_MENUSELECT, 0, (LPARAM)m_hMenu);
  1202. if(lpNMHT->dwFlags & HICF_LEAVING)
  1203. m_wndParent.SendMessage(WM_MENUSELECT, MAKEWPARAM(0, 0xFFFF), NULL);
  1204. }
  1205. return 0;
  1206. }
  1207. }
  1208. LRESULT OnParentDropDown(int /*idCtrl*/, LPNMHDR pnmh, BOOL& bHandled)
  1209. {
  1210. // Check if this comes from us
  1211. if(pnmh->hwndFrom != m_hWnd)
  1212. {
  1213. bHandled = FALSE;
  1214. return 1;
  1215. }
  1216. if(::GetFocus() != m_hWnd)
  1217. TakeFocus();
  1218. LPNMTOOLBAR pNMToolBar = (LPNMTOOLBAR)pnmh;
  1219. int nIndex = CommandToIndex(pNMToolBar->iItem);
  1220. m_bContextMenu = false;
  1221. m_bEscapePressed = false;
  1222. DoPopupMenu(nIndex, true);
  1223. return TBDDRET_DEFAULT;
  1224. }
  1225. LRESULT OnParentInitMenuPopup(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  1226. {
  1227. return OnInitMenuPopup(uMsg, wParam, lParam, bHandled);
  1228. }
  1229. LRESULT OnParentInternalGetBar(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  1230. {
  1231. return OnInternalGetBar(uMsg, wParam, lParam, bHandled);
  1232. }
  1233. LRESULT OnParentSysCommand(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled)
  1234. {
  1235. bHandled = FALSE;
  1236. if((m_uSysKey == VK_MENU
  1237. || (m_uSysKey == VK_F10 && !(::GetKeyState(VK_SHIFT) & 0x80))
  1238. || m_uSysKey == VK_SPACE)
  1239. && wParam == SC_KEYMENU)
  1240. {
  1241. if(::GetFocus() == m_hWnd)
  1242. {
  1243. GiveFocusBack(); // exit menu "loop"
  1244. PostMessage(TB_SETHOTITEM, (WPARAM)-1, 0L);
  1245. }
  1246. else if(m_uSysKey != VK_SPACE && !m_bSkipMsg)
  1247. {
  1248. TakeFocus(); // enter menu "loop"
  1249. bHandled = TRUE;
  1250. }
  1251. else if(m_uSysKey != VK_SPACE)
  1252. {
  1253. bHandled = TRUE;
  1254. }
  1255. }
  1256. m_bSkipMsg = false;
  1257. return 0;
  1258. }
  1259. LRESULT OnParentAPIGetMenu(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  1260. {
  1261. return OnAPIGetMenu(uMsg, wParam, lParam, bHandled);
  1262. }
  1263. LRESULT OnParentMenuChar(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  1264. {
  1265. return OnMenuChar(uMsg, wParam, lParam, bHandled);
  1266. }
  1267. LRESULT OnParentAPITrackPopupMenu(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  1268. {
  1269. return OnAPITrackPopupMenu(uMsg, wParam, lParam, bHandled);
  1270. }
  1271. LRESULT OnParentAPIGetCmdBar(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  1272. {
  1273. return OnAPIGetCmdBar(uMsg, wParam, lParam, bHandled);
  1274. }
  1275. LRESULT OnParentDrawItem(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  1276. {
  1277. return OnDrawItem(uMsg, wParam, lParam, bHandled);
  1278. }
  1279. LRESULT OnParentMeasureItem(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  1280. {
  1281. return OnMeasureItem(uMsg, wParam, lParam, bHandled);
  1282. }
  1283. LRESULT OnParentActivate(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled)
  1284. {
  1285. m_bParentActive = (wParam != WA_INACTIVE);
  1286. Invalidate();
  1287. UpdateWindow();
  1288. bHandled = FALSE;
  1289. return 1;
  1290. }
  1291. LRESULT OnParentCustomDraw(int /*idCtrl*/, LPNMHDR pnmh, BOOL& bHandled)
  1292. {
  1293. LRESULT lRet = CDRF_DODEFAULT;
  1294. bHandled = FALSE;
  1295. if(pnmh->hwndFrom == m_hWnd)
  1296. {
  1297. LPNMTBCUSTOMDRAW lpTBCustomDraw = (LPNMTBCUSTOMDRAW)pnmh;
  1298. if(lpTBCustomDraw->nmcd.dwDrawStage == CDDS_PREPAINT)
  1299. {
  1300. lRet = CDRF_NOTIFYITEMDRAW;
  1301. bHandled = TRUE;
  1302. }
  1303. else if(lpTBCustomDraw->nmcd.dwDrawStage == CDDS_ITEMPREPAINT && !m_bParentActive)
  1304. {
  1305. lpTBCustomDraw->clrText = ::GetSysColor(COLOR_GRAYTEXT);
  1306. bHandled = TRUE;
  1307. }
  1308. }
  1309. return lRet;
  1310. }
  1311. // MDI client window message handlers
  1312. LRESULT OnMDISetMenu(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
  1313. {
  1314. m_wndMDIClient.DefWindowProc(uMsg, NULL, lParam);
  1315. HMENU hOldMenu = GetMenu();
  1316. BOOL bRet = AttachMenu((HMENU)wParam);
  1317. bRet;
  1318. ATLASSERT(bRet);
  1319. return (LRESULT)hOldMenu;
  1320. }
  1321. // Message hook handlers
  1322. LRESULT OnHookMouseMove(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
  1323. {
  1324. static POINT s_point = { -1, -1 };
  1325. DWORD dwPoint = ::GetMessagePos();
  1326. POINT point = { GET_X_LPARAM(dwPoint), GET_Y_LPARAM(dwPoint) };
  1327. bHandled = FALSE;
  1328. if(m_bMenuActive)
  1329. {
  1330. if(::WindowFromPoint(point) == m_hWnd)
  1331. {
  1332. ScreenToClient(&point);
  1333. int nHit = HitTest(&point);
  1334. if((point.x != s_point.x || point.y != s_point.y) && nHit >= 0 && nHit < ::GetMenuItemCount(m_hMenu) && nHit != m_nPopBtn && m_nPopBtn != -1)
  1335. {
  1336. TBBUTTON tbb;
  1337. GetButton(nHit, &tbb);
  1338. if((tbb.fsState & TBSTATE_ENABLED) != 0)
  1339. {
  1340. m_nNextPopBtn = nHit | 0xFFFF0000;
  1341. HWND hWndMenu = m_stackMenuWnd.GetCurrent();
  1342. ATLASSERT(hWndMenu != NULL);
  1343. // this one is needed to close a menu if mouse button was down
  1344. ::PostMessage(hWndMenu, WM_LBUTTONUP, 0, MAKELPARAM(point.x, point.y));
  1345. // this one closes a popup menu
  1346. ::PostMessage(hWndMenu, WM_KEYDOWN, VK_ESCAPE, 0L);
  1347. bHandled = TRUE;
  1348. }
  1349. }
  1350. }
  1351. }
  1352. else
  1353. {
  1354. ScreenToClient(&point);
  1355. }
  1356. s_point = point;
  1357. return 0;
  1358. }
  1359. LRESULT OnHookSysKeyDown(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled)
  1360. {
  1361. bHandled = FALSE;
  1362. #ifdef _CMDBAR_EXTRA_TRACE
  1363. ATLTRACE2(atlTraceUI, 0, "CmdBar - Hook WM_SYSKEYDOWN (0x%2.2X)\n", wParam);
  1364. #endif
  1365. if(wParam != VK_SPACE && (!m_bMenuActive && ::GetFocus() == m_hWnd) || m_bMenuActive)
  1366. {
  1367. PostMessage(TB_SETHOTITEM, (WPARAM)-1, 0L);
  1368. GiveFocusBack();
  1369. if(!m_bMenuActive)
  1370. m_bSkipMsg = true;
  1371. }
  1372. else
  1373. {
  1374. m_uSysKey = (UINT)wParam;
  1375. }
  1376. return 0;
  1377. }
  1378. LRESULT OnHookSysKeyUp(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled)
  1379. {
  1380. bHandled = FALSE;
  1381. wParam;
  1382. #ifdef _CMDBAR_EXTRA_TRACE
  1383. ATLTRACE2(atlTraceUI, 0, "CmdBar - Hook WM_SYSKEYUP (0x%2.2X)\n", wParam);
  1384. #endif
  1385. return 0;
  1386. }
  1387. LRESULT OnHookSysChar(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled)
  1388. {
  1389. bHandled = FALSE;
  1390. #ifdef _CMDBAR_EXTRA_TRACE
  1391. ATLTRACE2(atlTraceUI, 0, "CmdBar - Hook WM_SYSCHAR (0x%2.2X)\n", wParam);
  1392. #endif
  1393. if(!m_bMenuActive && m_hWndHook != m_hWnd && wParam != VK_SPACE)
  1394. bHandled = TRUE;
  1395. return 0;
  1396. }
  1397. LRESULT OnHookKeyDown(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled)
  1398. {
  1399. #ifdef _CMDBAR_EXTRA_TRACE
  1400. ATLTRACE2(atlTraceUI, 0, "CmdBar - Hook WM_KEYDOWN (0x%2.2X)\n", wParam);
  1401. #endif
  1402. bHandled = FALSE;
  1403. if(wParam == VK_ESCAPE)
  1404. {
  1405. if(m_bMenuActive && !m_bContextMenu)
  1406. {
  1407. int nHot = GetHotItem();
  1408. if(nHot == -1)
  1409. nHot = m_nPopBtn;
  1410. if(nHot == -1)
  1411. nHot = 0;
  1412. SetHotItem(nHot);
  1413. bHandled = TRUE;
  1414. TakeFocus();
  1415. m_bEscapePressed = true; // To keep focus
  1416. }
  1417. else if(::GetFocus() == m_hWnd && m_wndParent.IsWindow())
  1418. {
  1419. SetHotItem(-1);
  1420. GiveFocusBack();
  1421. bHandled = TRUE;
  1422. }
  1423. }
  1424. else if(wParam == VK_RETURN || wParam == VK_UP || wParam == VK_DOWN)
  1425. {
  1426. if(!m_bMenuActive && ::GetFocus() == m_hWnd && m_wndParent.IsWindow())
  1427. {
  1428. int nHot = GetHotItem();
  1429. if(nHot != -1)
  1430. {
  1431. if(wParam != VK_RETURN)
  1432. {
  1433. // IE4 only: WM_KEYDOWN doesn't generate TBN_DROPDOWN, we need to simulate a mouse click
  1434. #if (_WIN32_IE < 0x0500)
  1435. DWORD dwMajor = 0, dwMinor = 0;
  1436. AtlGetCommCtrlVersion(&dwMajor, &dwMinor);
  1437. if(dwMajor <= 4 || (dwMajor == 5 && dwMinor < 80))
  1438. {
  1439. RECT rect;
  1440. GetItemRect(nHot, &rect);
  1441. PostMessage(WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(rect.left, rect.top));
  1442. }
  1443. #endif //(_WIN32_IE < 0x0500)
  1444. PostMessage(WM_KEYDOWN, VK_DOWN, 0L);
  1445. }
  1446. }
  1447. else
  1448. {
  1449. ATLTRACE2(atlTraceUI, 0, "CmdBar - Can't find hot button\n");
  1450. }
  1451. }
  1452. if(wParam == VK_RETURN && m_bMenuActive)
  1453. {
  1454. PostMessage(TB_SETHOTITEM, (WPARAM)-1, 0L);
  1455. m_nNextPopBtn = -1;
  1456. GiveFocusBack();
  1457. }
  1458. }
  1459. else if(wParam == VK_LEFT || wParam == VK_RIGHT)
  1460. {
  1461. #if (WINVER >= 0x0500)
  1462. bool bRTL = ((GetExStyle() & WS_EX_LAYOUTRTL) != 0);
  1463. WPARAM wpNext = bRTL ? VK_LEFT : VK_RIGHT;
  1464. WPARAM wpPrev = bRTL ? VK_RIGHT : VK_LEFT;
  1465. #else // !(WINVER >= 0x0500)
  1466. WPARAM wpNext = VK_RIGHT;
  1467. WPARAM wpPrev = VK_LEFT;
  1468. #endif // !(WINVER >= 0x0500)
  1469. if(m_bMenuActive && !m_bContextMenu && !(wParam == wpNext && m_bPopupItem))
  1470. {
  1471. bool bAction = false;
  1472. T* pT = static_cast<T*>(this);
  1473. if(wParam == wpPrev && s_pCurrentBar->m_stackMenuWnd.GetSize() == 1)
  1474. {
  1475. m_nNextPopBtn = pT->GetPreviousMenuItem();
  1476. if(m_nNextPopBtn != -1)
  1477. bAction = true;
  1478. }
  1479. else if(wParam == wpNext)
  1480. {
  1481. m_nNextPopBtn = pT->GetNextMenuItem();;
  1482. if(m_nNextPopBtn != -1)
  1483. bAction = true;
  1484. }
  1485. HWND hWndMenu = m_stackMenuWnd.GetCurrent();
  1486. ATLASSERT(hWndMenu != NULL);
  1487. // Close the popup menu
  1488. if(bAction)
  1489. {
  1490. ::PostMessage(hWndMenu, WM_KEYDOWN, VK_ESCAPE, 0L);
  1491. if(wParam == wpNext)
  1492. {
  1493. int cItem = m_stackMenuWnd.GetSize() - 1;
  1494. while(cItem >= 0)
  1495. {
  1496. hWndMenu = m_stackMenuWnd[cItem];
  1497. if(hWndMenu != NULL)
  1498. ::PostMessage(hWndMenu, WM_KEYDOWN, VK_ESCAPE, 0L);
  1499. cItem--;
  1500. }
  1501. }
  1502. bHandled = TRUE;
  1503. }
  1504. }
  1505. }
  1506. return 0;
  1507. }
  1508. LRESULT OnHookNextMenu(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
  1509. {
  1510. #ifdef _CMDBAR_EXTRA_TRACE
  1511. ATLTRACE2(atlTraceUI, 0, "CmdBar - Hook WM_NEXTMENU\n");
  1512. #endif
  1513. bHandled = FALSE;
  1514. return 1;
  1515. }
  1516. LRESULT OnHookChar(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled)
  1517. {
  1518. #ifdef _CMDBAR_EXTRA_TRACE
  1519. ATLTRACE2(atlTraceUI, 0, "CmdBar - Hook WM_CHAR (0x%2.2X)\n", wParam);
  1520. #endif
  1521. bHandled = (wParam == VK_ESCAPE);
  1522. if(wParam != VK_ESCAPE && wParam != VK_RETURN && m_bMenuActive)
  1523. {
  1524. SetHotItem(-1);
  1525. GiveFocusBack();
  1526. }
  1527. return 0;
  1528. }
  1529. // Implementation - ownerdraw overrideables and helpers
  1530. void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  1531. {
  1532. _MenuItemData* pmd = (_MenuItemData*)lpDrawItemStruct->itemData;
  1533. CDCHandle dc = lpDrawItemStruct->hDC;
  1534. const RECT& rcItem = lpDrawItemStruct->rcItem;
  1535. if(pmd->fType & MFT_SEPARATOR)
  1536. {
  1537. // draw separator
  1538. RECT rc = rcItem;
  1539. rc.top += (rc.bottom - rc.top) / 2; // vertical center
  1540. dc.DrawEdge(&rc, EDGE_ETCHED, BF_TOP); // draw separator line
  1541. }
  1542. else // not a separator
  1543. {
  1544. BOOL bDisabled = lpDrawItemStruct->itemState & ODS_GRAYED;
  1545. BOOL bSelected = lpDrawItemStruct->itemState & ODS_SELECTED;
  1546. BOOL bChecked = lpDrawItemStruct->itemState & ODS_CHECKED;
  1547. BOOL bHasImage = FALSE;
  1548. if(LOWORD(lpDrawItemStruct->itemID) == (WORD)-1)
  1549. bSelected = FALSE;
  1550. RECT rcButn = { rcItem.left, rcItem.top, rcItem.left + m_szButton.cx, rcItem.top + m_szButton.cy }; // button rect
  1551. ::OffsetRect(&rcButn, 0, ((rcItem.bottom - rcItem.top) - (rcButn.bottom - rcButn.top)) / 2); // center vertically
  1552. int iButton = pmd->iButton;
  1553. if(iButton >= 0)
  1554. {
  1555. bHasImage = TRUE;
  1556. // calc drawing point
  1557. SIZE sz = { rcButn.right - rcButn.left - m_szBitmap.cx, rcButn.bottom - rcButn.top - m_szBitmap.cy };
  1558. sz.cx /= 2;
  1559. sz.cy /= 2;
  1560. POINT point = { rcButn.left + sz.cx, rcButn.top + sz.cy };
  1561. // draw disabled or normal
  1562. if(!bDisabled)
  1563. {
  1564. // normal - fill background depending on state
  1565. if(!bChecked || bSelected)
  1566. {
  1567. dc.FillRect(&rcButn, (HBRUSH)LongToPtr((bChecked && !bSelected) ? (COLOR_3DLIGHT + 1) : (COLOR_MENU + 1)));
  1568. }
  1569. else
  1570. {
  1571. COLORREF crTxt = dc.SetTextColor(::GetSysColor(COLOR_BTNFACE));
  1572. COLORREF crBk = dc.SetBkColor(::GetSysColor(COLOR_BTNHILIGHT));
  1573. CBrush hbr(CDCHandle::GetHalftoneBrush());
  1574. dc.SetBrushOrg(rcButn.left, rcButn.top);
  1575. dc.FillRect(&rcButn, hbr);
  1576. dc.SetTextColor(crTxt);
  1577. dc.SetBkColor(crBk);
  1578. }
  1579. // draw pushed-in or popped-out edge
  1580. if(bSelected || bChecked)
  1581. {
  1582. RECT rc2 = rcButn;
  1583. dc.DrawEdge(&rc2, bChecked ? BDR_SUNKENOUTER : BDR_RAISEDINNER, BF_RECT);
  1584. }
  1585. // draw the image
  1586. ::ImageList_Draw(m_hImageList, iButton, dc, point.x, point.y, ILD_TRANSPARENT);
  1587. }
  1588. else
  1589. {
  1590. DrawBitmapDisabled(dc, iButton, point);
  1591. }
  1592. }
  1593. else
  1594. {
  1595. // no image - look for custom checked/unchecked bitmaps
  1596. CMenuItemInfo info;
  1597. info.fMask = MIIM_CHECKMARKS | MIIM_TYPE;
  1598. ::GetMenuItemInfo((HMENU)lpDrawItemStruct->hwndItem, lpDrawItemStruct->itemID, MF_BYCOMMAND, &info);
  1599. if(bChecked || info.hbmpUnchecked != NULL)
  1600. {
  1601. BOOL bRadio = ((info.fType & MFT_RADIOCHECK) != 0);
  1602. bHasImage = Draw3DCheckmark(dc, rcButn, bSelected, bDisabled, bRadio, bChecked ? info.hbmpChecked : info.hbmpUnchecked);
  1603. }
  1604. }
  1605. // draw item text
  1606. int cxButn = m_szButton.cx;
  1607. COLORREF colorBG = ::GetSysColor(bSelected ? COLOR_HIGHLIGHT : COLOR_MENU);
  1608. if(bSelected || lpDrawItemStruct->itemAction == ODA_SELECT)
  1609. {
  1610. RECT rcBG = rcItem;
  1611. if(bHasImage)
  1612. rcBG.left += cxButn + s_kcxGap;
  1613. dc.FillRect(&rcBG, (HBRUSH)LongToPtr(bSelected ? (COLOR_HIGHLIGHT + 1) : (COLOR_MENU + 1)));
  1614. }
  1615. // calc text rectangle and colors
  1616. RECT rcText = rcItem;
  1617. rcText.left += cxButn + s_kcxGap + s_kcxTextMargin;
  1618. rcText.right -= cxButn;
  1619. dc.SetBkMode(TRANSPARENT);
  1620. COLORREF colorText = ::GetSysColor(bDisabled ? (bSelected ? COLOR_GRAYTEXT : COLOR_3DSHADOW) : (bSelected ? COLOR_HIGHLIGHTTEXT : COLOR_MENUTEXT));
  1621. // font already selected by Windows
  1622. if(bDisabled && (!bSelected || colorText == colorBG))
  1623. {
  1624. // disabled - draw shadow text shifted down and right 1 pixel (unles selected)
  1625. RECT rcDisabled = rcText;
  1626. ::OffsetRect(&rcDisabled, 1, 1);
  1627. DrawMenuText(dc, rcDisabled, pmd->lpstrText, ::GetSysColor(COLOR_3DHILIGHT));
  1628. }
  1629. DrawMenuText(dc, rcText, pmd->lpstrText, colorText); // finally!
  1630. }
  1631. }
  1632. void DrawMenuText(CDCHandle& dc, RECT& rc, LPCTSTR lpstrText, COLORREF color)
  1633. {
  1634. int nTab = -1;
  1635. for(int i = 0; i < lstrlen(lpstrText); i++)
  1636. {
  1637. if(lpstrText[i] == '\t')
  1638. {
  1639. nTab = i;
  1640. break;
  1641. }
  1642. }
  1643. dc.SetTextColor(color);
  1644. dc.DrawText(lpstrText, nTab, &rc, DT_SINGLELINE | DT_LEFT | DT_VCENTER);
  1645. if(nTab != -1)
  1646. dc.DrawText(&lpstrText[nTab + 1], -1, &rc, DT_SINGLELINE | DT_RIGHT | DT_VCENTER);
  1647. }
  1648. void DrawBitmapDisabled(CDCHandle& dc, int nImage, POINT point)
  1649. {
  1650. // create memory DC
  1651. CDC dcMem;
  1652. dcMem.CreateCompatibleDC(dc);
  1653. // create mono or color bitmap
  1654. CBitmap bmp;
  1655. bmp.CreateCompatibleBitmap(dc, m_szBitmap.cx, m_szBitmap.cy);
  1656. ATLASSERT(bmp.m_hBitmap != NULL);
  1657. // draw image into memory DC--fill BG white first
  1658. HBITMAP hBmpOld = dcMem.SelectBitmap(bmp);
  1659. dcMem.PatBlt(0, 0, m_szBitmap.cx, m_szBitmap.cy, WHITENESS);
  1660. // If white is the text color, we can't use the normal painting since
  1661. // it would blend with the WHITENESS, but the mask is OK
  1662. UINT uDrawStyle = (::GetSysColor(COLOR_BTNTEXT) == RGB(255, 255, 255)) ? ILD_MASK : ILD_NORMAL;
  1663. ::ImageList_Draw(m_hImageList, nImage, dcMem, 0, 0, uDrawStyle);
  1664. dc.DitherBlt(point.x, point.y, m_szBitmap.cx, m_szBitmap.cy, dcMem, NULL, 0, 0);
  1665. dcMem.SelectBitmap(hBmpOld); // restore
  1666. }
  1667. BOOL Draw3DCheckmark(CDCHandle& dc, const RECT& rc, BOOL bSelected, BOOL bDisabled, BOOL bRadio, HBITMAP hBmpCheck)
  1668. {
  1669. // get checkmark bitmap, if none, use Windows standard
  1670. SIZE size = { 0, 0 };
  1671. CBitmapHandle bmp = hBmpCheck;
  1672. if(hBmpCheck != NULL)
  1673. {
  1674. bmp.GetSize(size);
  1675. }
  1676. else
  1677. {
  1678. size.cx = ::GetSystemMetrics(SM_CXMENUCHECK);
  1679. size.cy = ::GetSystemMetrics(SM_CYMENUCHECK);
  1680. bmp.CreateCompatibleBitmap(dc, size.cx, size.cy);
  1681. ATLASSERT(bmp.m_hBitmap != NULL);
  1682. }
  1683. // center bitmap in caller's rectangle
  1684. RECT rcDest = rc;
  1685. if((rc.right - rc.left) > size.cx)
  1686. {
  1687. rcDest.left = rc.left + (rc.right - rc.left - size.cx) / 2;
  1688. rcDest.right = rcDest.left + size.cx;
  1689. }
  1690. if((rc.bottom - rc.top) > size.cy)
  1691. {
  1692. rcDest.top = rc.top + (rc.bottom - rc.top - size.cy) / 2;
  1693. rcDest.bottom = rcDest.top + size.cy;
  1694. }
  1695. // paint background
  1696. if(!bDisabled)
  1697. {
  1698. if(bSelected)
  1699. {
  1700. dc.FillRect(&rcDest, (HBRUSH)LongToPtr(COLOR_MENU + 1));
  1701. }
  1702. else
  1703. {
  1704. COLORREF clrTextOld = dc.SetTextColor(::GetSysColor(COLOR_BTNFACE));
  1705. COLORREF clrBkOld = dc.SetBkColor(::GetSysColor(COLOR_BTNHILIGHT));
  1706. CBrush hbr(CDCHandle::GetHalftoneBrush());
  1707. dc.SetBrushOrg(rcDest.left, rcDest.top);
  1708. dc.FillRect(&rcDest, hbr);
  1709. dc.SetTextColor(clrTextOld);
  1710. dc.SetBkColor(clrBkOld);
  1711. }
  1712. }
  1713. // create source image
  1714. CDC dcSource;
  1715. dcSource.CreateCompatibleDC(dc);
  1716. HBITMAP hBmpOld = dcSource.SelectBitmap(bmp);
  1717. // set colors
  1718. const COLORREF clrBlack = RGB(0, 0, 0);
  1719. const COLORREF clrWhite = RGB(255, 255, 255);
  1720. COLORREF clrTextOld = dc.SetTextColor(clrBlack);
  1721. COLORREF clrBkOld = dc.SetBkColor(clrWhite);
  1722. // create mask
  1723. CDC dcMask;
  1724. dcMask.CreateCompatibleDC(dc);
  1725. CBitmap bmpMask;
  1726. bmpMask.CreateBitmap(size.cx, size.cy, 1, 1, NULL);
  1727. HBITMAP hBmpOld1 = dcMask.SelectBitmap(bmpMask);
  1728. // draw the checkmark transparently
  1729. int cx = rcDest.right - rcDest.left;
  1730. int cy = rcDest.bottom - rcDest.top;
  1731. if(hBmpCheck != NULL)
  1732. {
  1733. // build mask based on transparent color
  1734. dcSource.SetBkColor(m_clrMask);
  1735. dcMask.SetBkColor(clrBlack);
  1736. dcMask.SetTextColor(clrWhite);
  1737. dcMask.BitBlt(0, 0, size.cx, size.cy, dcSource, 0, 0, SRCCOPY);
  1738. // draw bitmap using the mask
  1739. dc.BitBlt(rcDest.left, rcDest.top, cx, cy, dcSource, 0, 0, SRCINVERT);
  1740. dc.BitBlt(rcDest.left, rcDest.top, cx, cy, dcMask, 0, 0, SRCAND);
  1741. dc.BitBlt(rcDest.left, rcDest.top, cx, cy, dcSource, 0, 0, SRCINVERT);
  1742. }
  1743. else
  1744. {
  1745. const DWORD ROP_DSno = 0x00BB0226L;
  1746. const DWORD ROP_DSa = 0x008800C6L;
  1747. const DWORD ROP_DSo = 0x00EE0086L;
  1748. const DWORD ROP_DSna = 0x00220326L;
  1749. // draw mask
  1750. RECT rcSource = { 0, 0, size.cx, size.cy };
  1751. dcMask.DrawFrameControl(&rcSource, DFC_MENU, bRadio ? DFCS_MENUBULLET : DFCS_MENUCHECK);
  1752. // draw shadow if disabled
  1753. if(bDisabled)
  1754. {
  1755. // offset by one pixel
  1756. int x = rcDest.left + 1;
  1757. int y = rcDest.top + 1;
  1758. // paint source bitmap
  1759. const int nColor = COLOR_3DHILIGHT;
  1760. dcSource.FillRect(&rcSource, (HBRUSH)LongToPtr(nColor + 1));
  1761. // draw checkmark - special case black and white colors
  1762. COLORREF clrCheck = ::GetSysColor(nColor);
  1763. if(clrCheck == clrWhite)
  1764. {
  1765. dc.BitBlt(x, y, cx, cy, dcMask, 0, 0, ROP_DSno);
  1766. dc.BitBlt(x, y, cx, cy, dcSource, 0, 0, ROP_DSa);
  1767. }
  1768. else
  1769. {
  1770. if(clrCheck != clrBlack)
  1771. {
  1772. ATLASSERT(dcSource.GetTextColor() == clrBlack);
  1773. ATLASSERT(dcSource.GetBkColor() == clrWhite);
  1774. dcSource.BitBlt(0, 0, size.cx, size.cy, dcMask, 0, 0, ROP_DSna);
  1775. }
  1776. dc.BitBlt(x, y, cx, cy, dcMask, 0, 0, ROP_DSa);
  1777. dc.BitBlt(x, y, cx, cy, dcSource, 0, 0, ROP_DSo);
  1778. }
  1779. }
  1780. // paint source bitmap
  1781. const int nColor = bDisabled ? COLOR_BTNSHADOW : COLOR_MENUTEXT;
  1782. dcSource.FillRect(&rcSource, (HBRUSH)LongToPtr(nColor + 1));
  1783. // draw checkmark - special case black and white colors
  1784. COLORREF clrCheck = ::GetSysColor(nColor);
  1785. if(clrCheck == clrWhite)
  1786. {
  1787. dc.BitBlt(rcDest.left, rcDest.top, cx, cy, dcMask, 0, 0, ROP_DSno);
  1788. dc.BitBlt(rcDest.left, rcDest.top, cx, cy, dcSource, 0, 0, ROP_DSa);
  1789. }
  1790. else
  1791. {
  1792. if(clrCheck != clrBlack)
  1793. {
  1794. ATLASSERT(dcSource.GetTextColor() == clrBlack);
  1795. ATLASSERT(dcSource.GetBkColor() == clrWhite);
  1796. dcSource.BitBlt(0, 0, size.cx, size.cy, dcMask, 0, 0, ROP_DSna);
  1797. }
  1798. dc.BitBlt(rcDest.left, rcDest.top, cx, cy, dcMask, 0, 0, ROP_DSa);
  1799. dc.BitBlt(rcDest.left, rcDest.top, cx, cy, dcSource, 0, 0, ROP_DSo);
  1800. }
  1801. }
  1802. // restore all
  1803. dc.SetTextColor(clrTextOld);
  1804. dc.SetBkColor(clrBkOld);
  1805. dcSource.SelectBitmap(hBmpOld);
  1806. dcMask.SelectBitmap(hBmpOld1);
  1807. if(hBmpCheck == NULL)
  1808. bmp.DeleteObject();
  1809. // draw pushed-in hilight
  1810. if(!bDisabled)
  1811. {
  1812. if(rc.right - rc.left > size.cx)
  1813. ::InflateRect(&rcDest, 1,1); // inflate checkmark by one pixel all around
  1814. dc.DrawEdge(&rcDest, BDR_SUNKENOUTER, BF_RECT);
  1815. }
  1816. return TRUE;
  1817. }
  1818. void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
  1819. {
  1820. _MenuItemData* pmd = (_MenuItemData*)lpMeasureItemStruct->itemData;
  1821. if(pmd->fType & MFT_SEPARATOR) // separator - use half system height and zero width
  1822. {
  1823. lpMeasureItemStruct->itemHeight = ::GetSystemMetrics(SM_CYMENU) / 2;
  1824. lpMeasureItemStruct->itemWidth = 0;
  1825. }
  1826. else
  1827. {
  1828. // compute size of text - use DrawText with DT_CALCRECT
  1829. CWindowDC dc(NULL);
  1830. HFONT hOldFont;
  1831. if(pmd->fState & MFS_DEFAULT)
  1832. {
  1833. // need bold version of font
  1834. LOGFONT lf;
  1835. m_fontMenu.GetLogFont(lf);
  1836. lf.lfWeight += 200;
  1837. CFont fontBold;
  1838. fontBold.CreateFontIndirect(&lf);
  1839. ATLASSERT(fontBold.m_hFont != NULL);
  1840. hOldFont = dc.SelectFont(fontBold);
  1841. }
  1842. else
  1843. {
  1844. hOldFont = dc.SelectFont(m_fontMenu);
  1845. }
  1846. RECT rcText = { 0, 0, 0, 0 };
  1847. dc.DrawText(pmd->lpstrText, -1, &rcText, DT_SINGLELINE | DT_LEFT | DT_VCENTER | DT_CALCRECT);
  1848. int cx = rcText.right - rcText.left;
  1849. dc.SelectFont(hOldFont);
  1850. LOGFONT lf;
  1851. m_fontMenu.GetLogFont(lf);
  1852. int cy = lf.lfHeight;
  1853. if(cy < 0)
  1854. cy = -cy;
  1855. cy += 8;
  1856. // height of item is the bigger of these two
  1857. lpMeasureItemStruct->itemHeight = max(cy, (int)m_szButton.cy);
  1858. // width is width of text plus a bunch of stuff
  1859. cx += 2 * s_kcxTextMargin; // L/R margin for readability
  1860. cx += s_kcxGap; // space between button and menu text
  1861. cx += 2 * m_szButton.cx; // button width (L=button; R=empty margin)
  1862. // Windows adds 1 to returned value
  1863. cx -= ::GetSystemMetrics(SM_CXMENUCHECK) - 1;
  1864. lpMeasureItemStruct->itemWidth = cx; // done deal
  1865. }
  1866. }
  1867. // Implementation - Hook procs
  1868. static LRESULT CALLBACK CreateHookProc(int nCode, WPARAM wParam, LPARAM lParam)
  1869. {
  1870. LRESULT lRet = 0;
  1871. TCHAR szClassName[7];
  1872. if(nCode == HCBT_CREATEWND)
  1873. {
  1874. HWND hWndMenu = (HWND)wParam;
  1875. #ifdef _CMDBAR_EXTRA_TRACE
  1876. ATLTRACE2(atlTraceUI, 0, "CmdBar - HCBT_CREATEWND (HWND = %8.8X)\n", hWndMenu);
  1877. #endif
  1878. ::GetClassName(hWndMenu, szClassName, 7);
  1879. if(!lstrcmp(_T("#32768"), szClassName))
  1880. s_pCurrentBar->m_stackMenuWnd.Push(hWndMenu);
  1881. }
  1882. else if(nCode == HCBT_DESTROYWND)
  1883. {
  1884. HWND hWndMenu = (HWND)wParam;
  1885. #ifdef _CMDBAR_EXTRA_TRACE
  1886. ATLTRACE2(atlTraceUI, 0, "CmdBar - HCBT_DESTROYWND (HWND = %8.8X)\n", hWndMenu);
  1887. #endif
  1888. ::GetClassName(hWndMenu, szClassName, 7);
  1889. if(!lstrcmp(_T("#32768"), szClassName))
  1890. {
  1891. ATLASSERT(hWndMenu == s_pCurrentBar->m_stackMenuWnd.GetCurrent());
  1892. s_pCurrentBar->m_stackMenuWnd.Pop();
  1893. }
  1894. }
  1895. else if(nCode < 0)
  1896. {
  1897. lRet = ::CallNextHookEx(s_hCreateHook, nCode, wParam, lParam);
  1898. }
  1899. return lRet;
  1900. }
  1901. static LRESULT CALLBACK MessageHookProc(int nCode, WPARAM wParam, LPARAM lParam)
  1902. {
  1903. LPMSG pMsg = (LPMSG)lParam;
  1904. if(nCode == HC_ACTION && wParam == PM_REMOVE && pMsg->message != GetGetBarMessage() && pMsg->message != WM_FORWARDMSG)
  1905. {
  1906. CCommandBarCtrlBase* pCmdBar = NULL;
  1907. HWND hWnd = pMsg->hwnd;
  1908. DWORD dwPID = 0;
  1909. while(pCmdBar == NULL && hWnd != NULL)
  1910. {
  1911. pCmdBar = (CCommandBarCtrlBase*)::SendMessage(hWnd, GetGetBarMessage(), (WPARAM)&dwPID, 0L);
  1912. hWnd = ::GetParent(hWnd);
  1913. }
  1914. if(pCmdBar != NULL && dwPID == GetCurrentProcessId())
  1915. {
  1916. pCmdBar->m_hWndHook = pMsg->hwnd;
  1917. ATLASSERT(pCmdBar->IsCommandBarBase());
  1918. if(::IsWindow(pCmdBar->m_hWnd))
  1919. pCmdBar->SendMessage(WM_FORWARDMSG, 0, (LPARAM)pMsg);
  1920. else
  1921. ATLTRACE2(atlTraceUI, 0, "CmdBar - Hook skipping message, can't find command bar!\n");
  1922. }
  1923. }
  1924. LRESULT lRet = 0;
  1925. ATLASSERT(s_pmapMsgHook != NULL);
  1926. if(s_pmapMsgHook != NULL)
  1927. {
  1928. DWORD dwThreadID = ::GetCurrentThreadId();
  1929. _MsgHookData* pData = s_pmapMsgHook->Lookup(dwThreadID);
  1930. if(pData != NULL)
  1931. {
  1932. lRet = ::CallNextHookEx(pData->hMsgHook, nCode, wParam, lParam);
  1933. }
  1934. }
  1935. return lRet;
  1936. }
  1937. // Implementation
  1938. void DoPopupMenu(int nIndex, bool bAnimate)
  1939. {
  1940. #ifdef _CMDBAR_EXTRA_TRACE
  1941. ATLTRACE2(atlTraceUI, 0, "CmdBar - DoPopupMenu, bAnimate = %s\n", bAnimate ? "true" : "false");
  1942. #endif
  1943. // Menu animation flags
  1944. #ifndef TPM_VERPOSANIMATION
  1945. const UINT TPM_VERPOSANIMATION = 0x1000L;
  1946. #endif
  1947. #ifndef TPM_NOANIMATION
  1948. const UINT TPM_NOANIMATION = 0x4000L;
  1949. #endif
  1950. // get popup menu and it's position
  1951. RECT rect;
  1952. GetItemRect(nIndex, &rect);
  1953. ClientToScreen(&rect);
  1954. TPMPARAMS TPMParams;
  1955. TPMParams.cbSize = sizeof(TPMPARAMS);
  1956. TPMParams.rcExclude = rect;
  1957. HMENU hMenuPopup = ::GetSubMenu(m_hMenu, nIndex);
  1958. ATLASSERT(hMenuPopup != NULL);
  1959. // get button ID
  1960. TBBUTTON tbb;
  1961. GetButton(nIndex, &tbb);
  1962. int nCmdID = tbb.idCommand;
  1963. m_nPopBtn = nIndex; // remember current button's index
  1964. // press button and display popup menu
  1965. PressButton(nCmdID, TRUE);
  1966. SetHotItem(nCmdID);
  1967. DoTrackPopupMenu(hMenuPopup, TPM_LEFTBUTTON | TPM_VERTICAL | TPM_LEFTALIGN | TPM_TOPALIGN |
  1968. (s_bW2K ? (bAnimate ? TPM_VERPOSANIMATION : TPM_NOANIMATION) : 0), rect.left, rect.bottom, &TPMParams);
  1969. PressButton(nCmdID, FALSE);
  1970. if(::GetFocus() != m_hWnd)
  1971. SetHotItem(-1);
  1972. m_nPopBtn = -1; // restore
  1973. // eat next message if click is on the same button
  1974. MSG msg;
  1975. if(::PeekMessage(&msg, m_hWnd, NULL, NULL, PM_NOREMOVE))
  1976. {
  1977. if(msg.message == WM_LBUTTONDOWN && ::PtInRect(&rect, msg.pt))
  1978. ::PeekMessage(&msg, m_hWnd, WM_LBUTTONDOWN, WM_LBUTTONDOWN, PM_REMOVE);
  1979. }
  1980. // check if another popup menu should be displayed
  1981. if(m_nNextPopBtn != -1)
  1982. {
  1983. PostMessage(GetAutoPopupMessage(), m_nNextPopBtn & 0xFFFF);
  1984. if(!(m_nNextPopBtn & 0xFFFF0000) && !m_bPopupItem)
  1985. PostMessage(WM_KEYDOWN, VK_DOWN, 0);
  1986. m_nNextPopBtn = -1;
  1987. }
  1988. else
  1989. {
  1990. m_bContextMenu = false;
  1991. // If user didn't hit escape, give focus back
  1992. if(!m_bEscapePressed)
  1993. {
  1994. GiveFocusBack();
  1995. }
  1996. else
  1997. {
  1998. SetHotItem(nCmdID);
  1999. SetAnchorHighlight(TRUE);
  2000. }
  2001. }
  2002. }
  2003. BOOL DoTrackPopupMenu(HMENU hMenu, UINT uFlags, int x, int y, LPTPMPARAMS lpParams = NULL)
  2004. {
  2005. CMenuHandle menuPopup = hMenu;
  2006. ::EnterCriticalSection(&_Module.m_csWindowCreate);
  2007. ATLASSERT(s_hCreateHook == NULL);
  2008. s_pCurrentBar = static_cast<CCommandBarCtrlBase*>(this);
  2009. s_hCreateHook = ::SetWindowsHookEx(WH_CBT, CreateHookProc, _Module.GetModuleInstance(), GetCurrentThreadId());
  2010. ATLASSERT(s_hCreateHook != NULL);
  2011. m_bPopupItem = false;
  2012. m_bMenuActive = true;
  2013. BOOL bTrackRet = menuPopup.TrackPopupMenuEx(uFlags, x, y, m_hWnd, lpParams);
  2014. m_bMenuActive = false;
  2015. ::UnhookWindowsHookEx(s_hCreateHook);
  2016. s_hCreateHook = NULL;
  2017. s_pCurrentBar = NULL;
  2018. ::LeaveCriticalSection(&_Module.m_csWindowCreate);
  2019. // cleanup - convert menus back to original state
  2020. #ifdef _CMDBAR_EXTRA_TRACE
  2021. ATLTRACE2(atlTraceUI, 0, "CmdBar - TrackPopupMenu - cleanup\n");
  2022. #endif
  2023. ATLASSERT(m_stackMenuWnd.GetSize() == 0);
  2024. UpdateWindow();
  2025. CWindow wndTL = GetTopLevelParent();
  2026. wndTL.UpdateWindow();
  2027. // restore the menu items to the previous state for all menus that were converted
  2028. if(m_bImagesVisible)
  2029. {
  2030. HMENU hMenuSav;
  2031. while((hMenuSav = m_stackMenuHandle.Pop()) != NULL)
  2032. {
  2033. menuPopup = hMenuSav;
  2034. BOOL bRet;
  2035. // restore state and delete menu item data
  2036. for(int i = 0; i < menuPopup.GetMenuItemCount(); i++)
  2037. {
  2038. CMenuItemInfo mii;
  2039. mii.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID;
  2040. bRet = menuPopup.GetMenuItemInfo(i, TRUE, &mii);
  2041. ATLASSERT(bRet);
  2042. _MenuItemData* pMI = (_MenuItemData*)mii.dwItemData;
  2043. if(pMI != NULL && pMI->IsCmdBarMenuItem())
  2044. {
  2045. mii.fMask = MIIM_DATA | MIIM_TYPE | MIIM_STATE;
  2046. mii.fType = pMI->fType;
  2047. mii.fState = pMI->fState;
  2048. mii.dwTypeData = pMI->lpstrText;
  2049. mii.cch = lstrlen(pMI->lpstrText);
  2050. mii.dwItemData = NULL;
  2051. bRet = menuPopup.SetMenuItemInfo(i, TRUE, &mii);
  2052. // this one triggers WM_MEASUREITEM
  2053. menuPopup.ModifyMenu(i, MF_BYPOSITION | mii.fType | mii.fState, mii.wID, pMI->lpstrText);
  2054. ATLASSERT(bRet);
  2055. delete [] pMI->lpstrText;
  2056. delete pMI;
  2057. }
  2058. }
  2059. }
  2060. }
  2061. return bTrackRet;
  2062. }
  2063. int GetPreviousMenuItem() const
  2064. {
  2065. if(m_nPopBtn == -1)
  2066. return -1;
  2067. #if (_WIN32_IE >= 0x0500)
  2068. RECT rcClient;
  2069. GetClientRect(&rcClient);
  2070. #endif //(_WIN32_IE >= 0x0500)
  2071. int nNextPopBtn;
  2072. for(nNextPopBtn = m_nPopBtn - 1; nNextPopBtn != m_nPopBtn; nNextPopBtn--)
  2073. {
  2074. if(nNextPopBtn < 0)
  2075. nNextPopBtn = ::GetMenuItemCount(m_hMenu) - 1;
  2076. TBBUTTON tbb = { 0 };
  2077. GetButton(nNextPopBtn, &tbb);
  2078. #if (_WIN32_IE >= 0x0500)
  2079. RECT rcBtn;
  2080. GetItemRect(nNextPopBtn, &rcBtn);
  2081. if((tbb.fsState & TBSTATE_ENABLED) != 0 && (tbb.fsState & TBSTATE_HIDDEN) == 0 && rcBtn.right <= rcClient.right)
  2082. break;
  2083. #else //!(_WIN32_IE >= 0x0500)
  2084. if((tbb.fsState & TBSTATE_ENABLED) != 0 && (tbb.fsState & TBSTATE_HIDDEN) == 0)
  2085. break;
  2086. #endif //!(_WIN32_IE >= 0x0500)
  2087. }
  2088. return (nNextPopBtn != m_nPopBtn) ? nNextPopBtn : -1;
  2089. }
  2090. int GetNextMenuItem() const
  2091. {
  2092. if(m_nPopBtn == -1)
  2093. return -1;
  2094. #if (_WIN32_IE >= 0x0500)
  2095. RECT rcClient;
  2096. GetClientRect(&rcClient);
  2097. #endif //(_WIN32_IE >= 0x0500)
  2098. int nNextPopBtn;
  2099. int nCount = ::GetMenuItemCount(m_hMenu);
  2100. for(nNextPopBtn = m_nPopBtn + 1; nNextPopBtn != m_nPopBtn; nNextPopBtn++)
  2101. {
  2102. if(nNextPopBtn >= nCount)
  2103. nNextPopBtn = 0;
  2104. TBBUTTON tbb = { 0 };
  2105. GetButton(nNextPopBtn, &tbb);
  2106. #if (_WIN32_IE >= 0x0500)
  2107. RECT rcBtn;
  2108. GetItemRect(nNextPopBtn, &rcBtn);
  2109. if((tbb.fsState & TBSTATE_ENABLED) != 0 && (tbb.fsState & TBSTATE_HIDDEN) == 0 && rcBtn.right <= rcClient.right)
  2110. break;
  2111. #else //!(_WIN32_IE >= 0x0500)
  2112. if((tbb.fsState & TBSTATE_ENABLED) != 0 && (tbb.fsState & TBSTATE_HIDDEN) == 0)
  2113. break;
  2114. #endif //!(_WIN32_IE >= 0x0500)
  2115. }
  2116. return (nNextPopBtn != m_nPopBtn) ? nNextPopBtn : -1;
  2117. }
  2118. void GetSystemSettings()
  2119. {
  2120. // refresh our font
  2121. NONCLIENTMETRICS info;
  2122. info.cbSize = sizeof(info);
  2123. ::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(info), &info, 0);
  2124. LOGFONT logfont;
  2125. memset(&logfont, 0, sizeof(LOGFONT));
  2126. if(m_fontMenu.m_hFont != NULL)
  2127. m_fontMenu.GetLogFont(logfont);
  2128. if(logfont.lfHeight != info.lfMenuFont.lfHeight ||
  2129. logfont.lfWidth != info.lfMenuFont.lfWidth ||
  2130. logfont.lfEscapement != info.lfMenuFont.lfEscapement ||
  2131. logfont.lfOrientation != info.lfMenuFont.lfOrientation ||
  2132. logfont.lfWeight != info.lfMenuFont.lfWeight ||
  2133. logfont.lfItalic != info.lfMenuFont.lfItalic ||
  2134. logfont.lfUnderline != info.lfMenuFont.lfUnderline ||
  2135. logfont.lfStrikeOut != info.lfMenuFont.lfStrikeOut ||
  2136. logfont.lfCharSet != info.lfMenuFont.lfCharSet ||
  2137. logfont.lfOutPrecision != info.lfMenuFont.lfOutPrecision ||
  2138. logfont.lfClipPrecision != info.lfMenuFont.lfClipPrecision ||
  2139. logfont.lfQuality != info.lfMenuFont.lfQuality ||
  2140. logfont.lfPitchAndFamily != info.lfMenuFont.lfPitchAndFamily ||
  2141. lstrcmp(logfont.lfFaceName, info.lfMenuFont.lfFaceName) != 0)
  2142. {
  2143. HFONT hFontMenu = ::CreateFontIndirect(&info.lfMenuFont);
  2144. ATLASSERT(hFontMenu != NULL);
  2145. if(hFontMenu != NULL)
  2146. {
  2147. if(m_fontMenu.m_hFont != NULL)
  2148. m_fontMenu.DeleteObject();
  2149. m_fontMenu.Attach(hFontMenu);
  2150. SetFont(m_fontMenu);
  2151. AddStrings(_T("NS\0")); // for proper item height
  2152. AutoSize();
  2153. }
  2154. }
  2155. }
  2156. // Implementation - alternate focus mode support
  2157. void TakeFocus()
  2158. {
  2159. if((m_dwExtendedStyle & CBR_EX_ALTFOCUSMODE) && m_hWndFocus == NULL)
  2160. m_hWndFocus = ::GetFocus();
  2161. SetFocus();
  2162. }
  2163. void GiveFocusBack()
  2164. {
  2165. if((m_dwExtendedStyle & CBR_EX_ALTFOCUSMODE) && ::IsWindow(m_hWndFocus))
  2166. ::SetFocus(m_hWndFocus);
  2167. else if(!(m_dwExtendedStyle & CBR_EX_ALTFOCUSMODE) && m_wndParent.IsWindow())
  2168. m_wndParent.SetFocus();
  2169. m_hWndFocus = NULL;
  2170. SetAnchorHighlight(FALSE);
  2171. }
  2172. // Implementation - internal message helpers
  2173. static UINT GetAutoPopupMessage()
  2174. {
  2175. static UINT uAutoPopupMessage = 0;
  2176. if(uAutoPopupMessage == 0)
  2177. {
  2178. ::EnterCriticalSection(&_Module.m_csStaticDataInit);
  2179. if(uAutoPopupMessage == 0)
  2180. uAutoPopupMessage = ::RegisterWindowMessage(_T("WTL_CmdBar_InternalAutoPopupMsg"));
  2181. ::LeaveCriticalSection(&_Module.m_csStaticDataInit);
  2182. }
  2183. ATLASSERT(uAutoPopupMessage != 0);
  2184. return uAutoPopupMessage;
  2185. }
  2186. static UINT GetGetBarMessage()
  2187. {
  2188. static UINT uGetBarMessage = 0;
  2189. if(uGetBarMessage == 0)
  2190. {
  2191. ::EnterCriticalSection(&_Module.m_csStaticDataInit);
  2192. if(uGetBarMessage == 0)
  2193. uGetBarMessage = ::RegisterWindowMessage(_T("WTL_CmdBar_InternalGetBarMsg"));
  2194. ::LeaveCriticalSection(&_Module.m_csStaticDataInit);
  2195. }
  2196. ATLASSERT(uGetBarMessage != 0);
  2197. return uGetBarMessage;
  2198. }
  2199. };
  2200. class CCommandBarCtrl : public CCommandBarCtrlImpl<CCommandBarCtrl>
  2201. {
  2202. public:
  2203. DECLARE_WND_SUPERCLASS(_T("WTL_CommandBar"), GetWndClassName())
  2204. };
  2205. }; //namespace WTL
  2206. #endif // __ATLCTRLW_H__