XTPMenuBar.cpp
上传用户:szled88
上传日期:2015-04-09
资源大小:43957k
文件大小:21k
源码类别:

对话框与窗口

开发平台:

Visual C++

  1. // XTPMenuBar.cpp : implementation of the CXTPMenuBar class.
  2. //
  3. // This file is a part of the XTREME COMMANDBARS MFC class library.
  4. // (c)1998-2008 Codejock Software, All Rights Reserved.
  5. //
  6. // THIS SOURCE FILE IS THE PROPERTY OF CODEJOCK SOFTWARE AND IS NOT TO BE
  7. // RE-DISTRIBUTED BY ANY MEANS WHATSOEVER WITHOUT THE EXPRESSED WRITTEN
  8. // CONSENT OF CODEJOCK SOFTWARE.
  9. //
  10. // THIS SOURCE CODE CAN ONLY BE USED UNDER THE TERMS AND CONDITIONS OUTLINED
  11. // IN THE XTREME TOOLKIT PRO LICENSE AGREEMENT. CODEJOCK SOFTWARE GRANTS TO
  12. // YOU (ONE SOFTWARE DEVELOPER) THE LIMITED RIGHT TO USE THIS SOFTWARE ON A
  13. // SINGLE COMPUTER.
  14. //
  15. // CONTACT INFORMATION:
  16. // support@codejock.com
  17. // http://www.codejock.com
  18. //
  19. /////////////////////////////////////////////////////////////////////////////
  20. #include "stdafx.h"
  21. #include "Resource.h"
  22. #include "Common/XTPResourceManager.h"
  23. #include "Common/XTPImageManager.h"
  24. #include "Common/XTPVC50Helpers.h"
  25. #include "Common/XTPIntel80Helpers.h"
  26. #include "Common/XTPHookManager.h"
  27. #include "XTPMenuBar.h"
  28. #include "XTPControls.h"
  29. #include "XTPControl.h"
  30. #include "XTPControlButton.h"
  31. #include "XTPControlPopup.h"
  32. #include "XTPPaintManager.h"
  33. #include "XTPCommandBars.h"
  34. #include "XTPMouseManager.h"
  35. #include "XTPPopupBar.h"
  36. #ifdef _DEBUG
  37. #define new DEBUG_NEW
  38. #undef THIS_FILE
  39. static char THIS_FILE[] = __FILE__;
  40. #endif
  41. #define TID_REFRESH 12223
  42. #ifndef OIC_WINLOGO
  43. #define OIC_WINLOGO         32517
  44. #endif
  45. CXTPMenuBarMDIMenuInfo::CXTPMenuBarMDIMenuInfo(CXTPCommandBars* pCommandBars, UINT nIDResource)
  46. {
  47. ASSERT(pCommandBars);
  48. ASSERT(nIDResource != 0);
  49. m_pControls = new CXTPControls;
  50. m_pControls->SetCommandBars(pCommandBars);
  51. m_nIDResource = nIDResource;
  52. m_bChanged = FALSE;
  53. m_hIcon = AfxGetApp()->LoadIcon(nIDResource);
  54. #pragma warning(disable:4510)
  55. #pragma warning(disable:4610)
  56. POSITION pos = AfxGetApp()->GetFirstDocTemplatePosition();
  57. while (pos)
  58. {
  59. CDocTemplate* pDocTemplate = AfxGetApp()->GetNextDocTemplate(pos);
  60. USES_PROTECTED_ACCESS(CXTPMenuBarMDIMenuInfo, CDocTemplate, UINT, m_nIDResource);
  61. UINT nID = PROTECTED_ACCESS(CDocTemplate, pDocTemplate, m_nIDResource);
  62. if (nIDResource == nID)
  63. {
  64. pDocTemplate->GetDocString(m_strTitle, CDocTemplate::docName);
  65. pDocTemplate->GetDocString(m_strDescription, CDocTemplate::regFileTypeName);
  66. break;
  67. }
  68. }
  69. }
  70. CXTPMenuBarMDIMenuInfo::~CXTPMenuBarMDIMenuInfo()
  71. {
  72. m_pControls->InternalRelease();
  73. }
  74. //////////////////////////////////////////////////////////////////////////
  75. CXTPMenuBarMDIMenus::CXTPMenuBarMDIMenus(CXTPMenuBar* pMenuBar)
  76. : m_pMenuBar(pMenuBar)
  77. {
  78. }
  79. CXTPMenuBarMDIMenus::~CXTPMenuBarMDIMenus()
  80. {
  81. RemoveAll();
  82. }
  83. void CXTPMenuBarMDIMenus::RemoveAll()
  84. {
  85. POSITION pos = m_mapMenus.GetStartPosition();
  86. while (pos)
  87. {
  88. UINT nIDCommand;
  89. CXTPMenuBarMDIMenuInfo* pInfo;
  90. m_mapMenus.GetNextAssoc(pos, nIDCommand, pInfo);
  91. pInfo->InternalRelease();
  92. }
  93. m_mapMenus.RemoveAll();
  94. }
  95. void CXTPMenuBarMDIMenus::Remove(UINT nIDResource)
  96. {
  97. CXTPMenuBarMDIMenuInfo* pInfo = Lookup(nIDResource);
  98. if (pInfo)
  99. {
  100. m_mapMenus.RemoveKey(nIDResource);
  101. pInfo->InternalRelease();
  102. }
  103. }
  104. void CXTPMenuBarMDIMenus::SetAt(UINT nIDResource, CXTPMenuBarMDIMenuInfo* pInfo)
  105. {
  106. ASSERT(pInfo);
  107. ASSERT(!Lookup(nIDResource));
  108. m_mapMenus.SetAt(nIDResource, pInfo);
  109. }
  110. CXTPMenuBarMDIMenuInfo* CXTPMenuBarMDIMenus::Lookup(UINT nIDResource) const
  111. {
  112. CXTPMenuBarMDIMenuInfo* pInfo;
  113. if (!m_mapMenus.Lookup(nIDResource, pInfo))
  114. return NULL;
  115. return pInfo;
  116. }
  117. int CXTPMenuBarMDIMenus::GetCount() const
  118. {
  119. return (int)m_mapMenus.GetCount();
  120. }
  121. POSITION CXTPMenuBarMDIMenus::GetStartPosition() const
  122. {
  123. return m_mapMenus.GetStartPosition();
  124. }
  125. void CXTPMenuBarMDIMenus::GetNextMenu(POSITION& pos, CXTPMenuBarMDIMenuInfo*& pInfo) const
  126. {
  127. UINT nIDResource = 0;
  128. m_mapMenus.GetNextAssoc(pos, nIDResource, pInfo);
  129. }
  130. //////////////////////////////////////////////////////////////////////////
  131. class CXTPMenuBar::CControlMDIButton : public CXTPControlButton
  132. {
  133. public:
  134. CControlMDIButton()
  135. {
  136. m_dwFlags |= xtpFlagRightAlign | xtpFlagSkipFocus | xtpFlagManualUpdate | xtpFlagNoMovable;
  137. }
  138. CSize GetSize(CDC* /*pDC*/)
  139. {
  140. return CSize(16, 16);
  141. };
  142. void SetBeginGroup(BOOL /*bBeginGroup*/)
  143. {
  144. }
  145. void Draw(CDC* pDC);
  146. void OnExecute()
  147. {
  148. HWND hWndChild = ((CXTPMenuBar*)m_pParent)->GetActiveMdiChildWnd();
  149. ASSERT(hWndChild);
  150. UINT nId = m_nId == XTP_ID_MENUBAR_CLOSE ? SC_CLOSE :
  151. m_nId == XTP_ID_MENUBAR_RESTORE ? SC_RESTORE : SC_MINIMIZE;
  152. ::PostMessage(hWndChild, WM_SYSCOMMAND, nId, 0);
  153. }
  154. };
  155. class CXTPMenuBar::CControlMDISysMenuPopup : public CXTPControlPopup
  156. {
  157. class CXTPSysMenuPopupBar : public CXTPPopupBar
  158. {
  159. void UpdateShortcuts()
  160. {
  161. }
  162. };
  163. public:
  164. CControlMDISysMenuPopup(HWND hWndActiveChild, HMENU hDocMenu)
  165. {
  166. m_dwFlags |= xtpFlagManualUpdate | xtpFlagNoMovable;
  167. m_controlType = xtpControlPopup;
  168. m_strCaption = _T("-");
  169. m_hIcon = 0;
  170. m_pCommandBar = new CXTPSysMenuPopupBar();
  171. m_pCommandBar->EnableCustomization(FALSE);
  172. SetMDISysMenu(hWndActiveChild, hDocMenu);
  173. }
  174. void SetMDISysMenu(HWND hWndActiveChild, HMENU hDocMenu)
  175. {
  176. m_hDocMenu = hDocMenu;
  177. HICON hIcon = (HICON)::SendMessage(hWndActiveChild, WM_XTP_GETTABICON, 0, 0);
  178. if (hIcon == NULL) hIcon = (HICON)::SendMessage(hWndActiveChild, WM_GETICON, ICON_SMALL, 0);
  179. if (hIcon == NULL) hIcon = (HICON)::SendMessage(hWndActiveChild, WM_GETICON, ICON_BIG, 0);
  180. if (hIcon == NULL) hIcon = (HICON)(ULONG_PTR)::GetClassLongPtr(hWndActiveChild, GCLP_HICONSM);
  181. if (hIcon == NULL) hIcon = AfxGetApp()->LoadOEMIcon(OIC_WINLOGO);
  182. if (m_hIcon)
  183. {
  184. DestroyIcon(m_hIcon);
  185. m_hIcon = 0;
  186. }
  187. if (hIcon)
  188. m_hIcon = CXTPImageManagerIcon::ScaleToFit(hIcon, CXTPImageManagerIcon::GetExtent(hIcon), 16);
  189. m_pCommandBar->GetControls()->RemoveAll();
  190. CMenu* pMenu = CMenu::FromHandle(hDocMenu);
  191. BOOL bSeparator = FALSE;
  192. for (int i = 0; i < (int)pMenu->GetMenuItemCount(); i++)
  193. {
  194. UINT nItemID = pMenu->GetMenuItemID(i);
  195. if (nItemID == 0)
  196. bSeparator = TRUE;
  197. else
  198. {
  199. CXTPControl* pControl = NULL;
  200. if (nItemID >= 0xF000 && nItemID < 0xF1F0)
  201. {
  202. pControl = (CXTPControl*)CXTPControlButton::CreateObject();
  203. pControl->SetFlags(xtpFlagManualUpdate);
  204. m_pCommandBar->GetControls()->Add(pControl, nItemID);
  205. CString strCaption;
  206. pMenu->GetMenuString(i, strCaption, MF_BYPOSITION);
  207. pControl->SetCaption(strCaption);
  208. CString strDescription;
  209. if (!strDescription.LoadString(ID_COMMAND_FROM_SC(nItemID)))
  210. {
  211. }
  212. pControl->SetDescription(strDescription);
  213. pControl->SetItemDefault((int)::GetMenuDefaultItem(pMenu->m_hMenu, TRUE, GMDI_USEDISABLED) == i);
  214. pControl->SetEnabled(!(pMenu->GetMenuState (i, MF_BYPOSITION) & MF_DISABLED));
  215. }
  216. else
  217. {
  218. pControl = m_pCommandBar->GetControls()->AddMenuItem(pMenu, i);
  219. }
  220. if (pControl && bSeparator)
  221. {
  222. pControl->SetBeginGroup(TRUE);
  223. bSeparator = FALSE;
  224. }
  225. }
  226. }
  227. }
  228. ~CControlMDISysMenuPopup()
  229. {
  230. if (m_hIcon) DestroyIcon(m_hIcon);
  231. }
  232. CSize GetSize(CDC* /*pDC*/)
  233. {
  234. return CSize(16, 16);
  235. };
  236. void Draw(CDC* pDC)
  237. {
  238. if (m_hIcon)
  239. {
  240. pDC->DrawState (GetRect().TopLeft(), CSize(16, 16), m_hIcon, (UINT)DSS_NORMAL, HBRUSH(0));
  241. }
  242. }
  243. void UpdateShadow()
  244. {
  245. }
  246. void AdjustExcludeRect(CRect& rc, BOOL /*bVertical*/)
  247. {
  248. rc.InflateRect(1, 1);
  249. }
  250. public:
  251. HMENU m_hDocMenu;
  252. HICON m_hIcon;
  253. };
  254. void CXTPMenuBar::CControlMDIButton::Draw(CDC* pDC)
  255. {
  256. CFont font;
  257. font.CreatePointFont(MulDiv(80, 96, pDC->GetDeviceCaps(LOGPIXELSX)), _T("Marlett"));
  258. CFont* pFont = pDC->SelectObject(&font);
  259. GetPaintManager()->DrawControlEntry(pDC, this);
  260. pDC->SetTextColor(GetPaintManager()->GetControlTextColor(this));
  261. pDC->DrawText(m_strParameter, m_rcControl, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  262. pDC->SelectObject(pFont);
  263. }
  264. IMPLEMENT_XTP_COMMANDBAR(CXTPMenuBar, CXTPToolBar)
  265. CXTPMenuBar::CXTPMenuBar()
  266. {
  267. m_barType = xtpBarTypeMenuBar;
  268. m_bMdiApp = FALSE;
  269. m_hWndActiveChild = 0;
  270. m_nIDResource = 0;
  271. m_nIDResourceCurrent = 0;
  272. m_bDirtyMenu = FALSE;
  273. m_hMenuDefault = NULL;
  274. m_bShowExpandButton = FALSE;
  275. m_bCloseable = FALSE;
  276. m_nBarID = XTP_IDR_MENUBAR;
  277. m_dwFlags = xtpFlagAlignAny | xtpFlagFloating | xtpFlagStretched;
  278. m_pMDIMenus = new CXTPMenuBarMDIMenus(this);
  279. EnableAutomation();
  280. }
  281. CXTPMenuBar::~CXTPMenuBar()
  282. {
  283. FreeMDIMenus();
  284. CMDTARGET_RELEASE(m_pMDIMenus);
  285. if (m_hMenuDefault)
  286. {
  287. DestroyMenu(m_hMenuDefault);
  288. }
  289. }
  290. void CXTPMenuBar::SetupHook()
  291. {
  292. m_bMdiApp = GetSite()->IsKindOf(RUNTIME_CLASS(CMDIFrameWnd));
  293. CWnd* pSite = GetSite();
  294. XTPHookManager()->SetHook(pSite->GetSafeHwnd(), this);
  295. CMDIFrameWnd* pMDIFrame = DYNAMIC_DOWNCAST(CMDIFrameWnd, pSite);
  296. if (pMDIFrame)
  297. {
  298. XTPHookManager()->SetHook(pMDIFrame->m_hWndMDIClient, this);
  299. }
  300. }
  301. HWND CXTPMenuBar::GetActiveMdiChildWnd(BOOL* bMaximized)
  302. {
  303. BOOL bMax = FALSE;
  304. if (!m_bMdiApp) return NULL;
  305. CMDIFrameWnd * pFrame = DYNAMIC_DOWNCAST(CMDIFrameWnd, GetSite());
  306. ASSERT_VALID(pFrame);
  307. HWND hWndMdiArea = pFrame->m_hWndMDIClient;
  308. ASSERT(hWndMdiArea && ::IsWindow(hWndMdiArea));
  309. if (!hWndMdiArea)
  310. return 0;
  311. HWND hWnd = (HWND) ::SendMessage(hWndMdiArea, WM_MDIGETACTIVE, 0, (LPARAM)&bMax);
  312. if (!hWnd) bMax = FALSE;
  313. ASSERT(hWnd == NULL || ::IsWindow(hWnd));
  314. if (bMaximized) *bMaximized = bMax;
  315. return hWnd;
  316. }
  317. BOOL CXTPMenuBar::IsOleDocumentActive(HWND hWndActiveChild) const
  318. {
  319. #if _MSC_VER >= 1200 // MFC 6.0
  320. CFrameWnd* pFrame = DYNAMIC_DOWNCAST(CFrameWnd, CWnd::FromHandle(hWndActiveChild));
  321. COleDocument* pActiveOleDoc =  DYNAMIC_DOWNCAST(COleDocument,
  322. pFrame && pFrame->GetActiveView() ? pFrame->GetActiveView()->GetDocument() : NULL);
  323. if (!pActiveOleDoc || !pFrame)
  324. return FALSE;
  325. COleDocObjectItem* pItem = DYNAMIC_DOWNCAST(COleDocObjectItem,
  326. pActiveOleDoc->GetInPlaceActiveItem(pFrame->GetActiveView()));
  327. return  pItem != NULL;
  328. #else
  329. UNREFERENCED_PARAMETER(hWndActiveChild);
  330. return FALSE;
  331. #endif
  332. }
  333. void CXTPMenuBar::SyncActiveMdiChild()
  334. {
  335. HWND hWndActiveChild = GetActiveMdiChildWnd();
  336. if (m_hWndActiveChild == hWndActiveChild)
  337. return;
  338. if (IsOleDocumentActive(hWndActiveChild))
  339. return;
  340. if (m_hWndActiveChild != NULL)
  341. XTPHookManager()->RemoveHook(m_hWndActiveChild, this);
  342. m_hWndActiveChild = hWndActiveChild;
  343. if (m_hWndActiveChild != NULL)
  344. XTPHookManager()->SetHook(m_hWndActiveChild, this);
  345. }
  346. void CXTPMenuBar::RefreshMenu()
  347. {
  348. SyncActiveMdiChild();
  349. if (IsVisible())
  350. {
  351. RefreshSysButtons();
  352. }
  353. }
  354. void CXTPMenuBar::AddSysButton(CXTPControl* pControl, int nId, LPCTSTR strParameter, int nBefore /*= -1*/)
  355. {
  356. m_pControls->Add(pControl, nId, strParameter, nBefore, TRUE);
  357. }
  358. void CXTPMenuBar::RefreshSysButtons()
  359. {
  360. BOOL bMax = FALSE;
  361. HWND hWndActiveChild = GetActiveMdiChildWnd(&bMax);
  362. DWORD dwStyle = hWndActiveChild ? GetWindowLong(hWndActiveChild, GWL_STYLE) : 0;
  363. CXTPControl*  pButton;
  364. pButton = m_pControls->FindControl(XTP_ID_MENUBAR_SYSMENU);
  365. if (bMax && (m_dwFlags & xtpFlagAddMDISysPopup))
  366. {
  367. HMENU hDocMenuButton = pButton ? ((CControlMDISysMenuPopup*)pButton)->m_hDocMenu : 0;
  368. HMENU hDocMenu =
  369. ::GetSystemMenu(hWndActiveChild, FALSE);
  370. if (hDocMenu && ::IsMenu(hDocMenu))
  371. {
  372. if (hDocMenuButton != hDocMenu)
  373. {
  374. if (pButton)
  375. {
  376. ((CControlMDISysMenuPopup*)pButton)->SetMDISysMenu(hWndActiveChild, hDocMenu);
  377. DelayRedraw();
  378. }
  379. else
  380. {
  381. AddSysButton(new CControlMDISysMenuPopup(hWndActiveChild, hDocMenu), XTP_ID_MENUBAR_SYSMENU, _T(""), 0);
  382. }
  383. }
  384. }
  385. else
  386. {
  387. if (pButton) m_pControls->Remove(pButton);
  388. }
  389. }
  390. else if (pButton) m_pControls->Remove(pButton);
  391. pButton = m_pControls->FindControl(XTP_ID_MENUBAR_MINIMIZE);
  392. if (!pButton && bMax && (dwStyle & WS_MINIMIZEBOX) && (dwStyle & WS_SYSMENU) && (!(m_dwFlags & xtpFlagHideMinimizeBox))) AddSysButton(new CControlMDIButton(), XTP_ID_MENUBAR_MINIMIZE, _T("0"));
  393. else if (pButton && !bMax) m_pControls->Remove(pButton);
  394. pButton = m_pControls->FindControl(XTP_ID_MENUBAR_RESTORE);
  395. if (!pButton && bMax && (dwStyle & WS_MAXIMIZEBOX) && (dwStyle & WS_SYSMENU) && (!(m_dwFlags & xtpFlagHideMaximizeBox))) AddSysButton(new CControlMDIButton(), XTP_ID_MENUBAR_RESTORE, _T("2"));
  396. else if (pButton && !bMax) m_pControls->Remove(pButton);
  397. pButton = m_pControls->FindControl(XTP_ID_MENUBAR_CLOSE);
  398. if (!pButton && bMax && (dwStyle & WS_SYSMENU) && (!(m_dwFlags & xtpFlagHideClose))) AddSysButton(new CControlMDIButton(), XTP_ID_MENUBAR_CLOSE, _T("r"));
  399. else if (pButton && !bMax) m_pControls->Remove(pButton);
  400. }
  401. void CXTPMenuBar::FreeMDIMenus()
  402. {
  403. m_pMDIMenus->RemoveAll();
  404. }
  405. BOOL CXTPMenuBar::IsMDIMenusChanged()
  406. {
  407. m_bDirtyMenu = FALSE;
  408. if (m_pMDIMenus->GetCount() > 0)
  409. {
  410. SwitchMDIMenu(GetDefaultMenuResource());
  411. m_bDirtyMenu = TRUE;
  412. }
  413. BOOL bChanged = FALSE;
  414. POSITION pos = m_pMDIMenus->GetStartPosition();
  415. while (pos)
  416. {
  417. CXTPMenuBarMDIMenuInfo* pInfo;
  418. m_pMDIMenus->GetNextMenu(pos, pInfo);
  419. pInfo->m_bChanged = pInfo->m_pControls->IsChanged();
  420. bChanged = bChanged || pInfo->m_bChanged;
  421. }
  422. return bChanged;
  423. }
  424. CXTPMenuBarMDIMenuInfo* CXTPMenuBar::AddMDIMenu(UINT nIDResource)
  425. {
  426. CXTPMenuBarMDIMenuInfo* pInfo = m_pMDIMenus->Lookup(nIDResource);
  427. if (pInfo)
  428. return pInfo;
  429. pInfo = new CXTPMenuBarMDIMenuInfo(GetCommandBars(), nIDResource);
  430. m_pMDIMenus->SetAt(nIDResource, pInfo);
  431. CMenu menu;
  432. VERIFY(menu.LoadMenu(nIDResource));
  433. pInfo->m_pControls->LoadMenu(&menu);
  434. pInfo->m_pControls->CreateOriginalControls();
  435. return pInfo;
  436. }
  437. void CXTPMenuBar::SwitchActiveMenu()
  438. {
  439. CFrameWnd* pFrame = DYNAMIC_DOWNCAST(CFrameWnd, GetSite());
  440. if (pFrame && pFrame->GetSafeHwnd()) pFrame->OnUpdateFrameMenu(0);
  441. m_bDirtyMenu = FALSE;
  442. }
  443. void CXTPMenuBar::SwitchMDIMenu(UINT nIDResource, CMenu* pMenu)
  444. {
  445. if ((nIDResource == 0) || (m_nIDResourceCurrent == 0) || (m_nIDResourceCurrent == nIDResource))
  446. return;
  447. if (GetCommandBars()->IsCustomizeAvail() || m_pMDIMenus->Lookup(nIDResource))
  448. {
  449. if (pMenu == NULL && !m_pMDIMenus->Lookup(nIDResource))
  450. return;
  451. CXTPMenuBarMDIMenuInfo* pInfo = m_pMDIMenus->Lookup(m_nIDResourceCurrent);
  452. if (pInfo)
  453. {
  454. ASSERT(pInfo->m_pControls == m_pControls);
  455. }
  456. else
  457. {
  458. pInfo = new CXTPMenuBarMDIMenuInfo(GetCommandBars(), m_nIDResourceCurrent);
  459. pInfo->m_pControls->InternalRelease();
  460. pInfo->m_pControls = m_pControls;
  461. m_pControls->InternalAddRef();
  462. m_pMDIMenus->SetAt(m_nIDResourceCurrent, pInfo);
  463. }
  464. m_pControls->SetParent(0);
  465. m_pControls->InternalRelease();
  466. m_pControls = NULL;
  467. pInfo = m_pMDIMenus->Lookup(nIDResource);
  468. if (!pInfo)
  469. {
  470. pInfo = new CXTPMenuBarMDIMenuInfo(GetCommandBars(), nIDResource);
  471. m_pControls = pInfo->m_pControls;
  472. m_pControls->InternalAddRef();
  473. m_pControls->SetParent(this);
  474. ASSERT(pMenu);
  475. if (pMenu)
  476. {
  477. LoadMenu(pMenu);
  478. }
  479. m_pControls->CreateOriginalControls();
  480. ASSERT(nIDResource != m_nIDResource);
  481. m_pMDIMenus->SetAt(nIDResource, pInfo);
  482. }
  483. else
  484. {
  485. m_pControls = pInfo->m_pControls;
  486. m_pControls->SetParent(this);
  487. m_pControls->InternalAddRef();
  488. if (m_pControls->GetOriginalControls() == 0 && pMenu)
  489. {
  490. CXTPOriginalControls* pOriginalControls = new CXTPOriginalControls();
  491. pOriginalControls->LoadMenu(pMenu);
  492. m_pControls->SetOriginalControls(pOriginalControls);
  493. }
  494. if (nIDResource == m_nIDResource)
  495. {
  496. m_pMDIMenus->Remove(nIDResource);
  497. }
  498. }
  499. }
  500. else if (pMenu)
  501. {
  502. LoadMenu(pMenu);
  503. }
  504. OnRecalcLayout();
  505. m_nIDResourceCurrent = nIDResource;
  506. }
  507. void CXTPMenuBar::SwitchMDIMenu(HMENU hMenu)
  508. {
  509. UINT nIDResource = GetActiveDocTemplate();
  510. if (hMenu == m_hMenuDefault)
  511. {
  512. nIDResource = m_nIDResource;
  513. }
  514. if ((nIDResource == 0) || (m_nIDResourceCurrent == 0) || (m_nIDResourceCurrent == nIDResource))
  515. return;
  516. CMenu* pMenu = CMenu::FromHandle(hMenu);
  517. if (!pMenu || pMenu->GetMenuItemCount() == 0)
  518. return;
  519. SwitchMDIMenu(nIDResource, pMenu);
  520. }
  521. int CXTPMenuBar::OnHookMessage(HWND hWnd, UINT nMessage, WPARAM& wParam, LPARAM& lParam, LRESULT& lResult)
  522. {
  523. switch (nMessage)
  524. {
  525. case WM_SYSCOMMAND:
  526. if (IsWindowVisible() && !XTPMouseManager()->IsMouseLocked() && (GetKeyState(VK_SHIFT) >= 0) && !IsCustomizeMode())
  527. if (((wParam & 0xFFF0) == SC_KEYMENU) && (lParam == 0))
  528. {
  529. if (PreviewAccel((UINT)VK_MENU))
  530. return TRUE;
  531. CXTPCommandBars* pCommandBars = GetCommandBars();
  532. if (pCommandBars)
  533. {
  534. pCommandBars->SetToolBarFocus(this);
  535. }
  536. return TRUE;
  537. }
  538. break;
  539. case WM_MDISETMENU:
  540. if (IsVisible())
  541. {
  542. if (IsCustomizeMode())
  543. {
  544. GetSite()->SetMenu(0);
  545. wParam = 0;
  546. return TRUE;
  547. }
  548. BOOL bAccepMenus = ((m_dwFlags & xtpFlagIgnoreSetMenuMessage) == 0) && (wParam || m_hMenuDefault);
  549. if (bAccepMenus)
  550. {
  551. SwitchMDIMenu(wParam ? (HMENU)wParam : m_hMenuDefault);
  552. }
  553. GetSite()->SetMenu(0);
  554. RefreshMenu();
  555. wParam = 0;
  556. return TRUE;
  557. }
  558. else if (GetSite()->GetMenu() == 0)
  559. {
  560. wParam = 0;
  561. return TRUE;
  562. }
  563. break;
  564. case WM_MDICREATE:
  565. case WM_MDIDESTROY:
  566. case WM_MDIACTIVATE:
  567. PostMessage(WM_TIMER, TID_REFRESH, 0);
  568. break;
  569. case WM_SIZE:
  570. case WM_WINDOWPOSCHANGED:
  571. if (hWnd == m_hWndActiveChild)
  572. PostMessage(WM_TIMER, TID_REFRESH, 0);
  573. break;
  574. }
  575. return CXTPToolBar::OnHookMessage(hWnd, nMessage, wParam, lParam, lResult);
  576. }
  577. BOOL CXTPMenuBar::LoadMenuBar(UINT nIDResource)
  578. {
  579. ASSERT(m_pControls->GetCount() == 0);
  580. if (nIDResource != 0)
  581. {
  582. CMenu menu;
  583. if (!menu.LoadMenu(nIDResource))
  584. return FALSE;
  585. if (!LoadMenu(&menu))
  586. return FALSE;
  587. m_nIDResourceCurrent = m_nIDResource = nIDResource;
  588. }
  589. SetupHook();
  590. UINT nIDMenuBarIcons[] =
  591. {
  592. SC_RESTORE, SC_MINIMIZE, SC_MAXIMIZE, SC_CLOSE
  593. };
  594. CXTPCommandBars* pCommandBars = GetCommandBars();
  595. if (pCommandBars)
  596. pCommandBars->GetImageManager()->SetIcons(XTP_IDB_MENUBAR_ICONS, nIDMenuBarIcons, 4, CSize(16, 16));
  597. m_hMenuDefault = ::GetMenu(GetSite()->GetSafeHwnd());
  598. GetSite()->SetMenu(0);
  599. return TRUE;
  600. }
  601. BEGIN_MESSAGE_MAP(CXTPMenuBar, CXTPToolBar)
  602. ON_WM_TIMER()
  603. END_MESSAGE_MAP()
  604. void CXTPMenuBar::OnTimer(UINT_PTR nIDEvent)
  605. {
  606. if (nIDEvent == TID_REFRESH)
  607. RefreshMenu();
  608. CXTPToolBar::OnTimer(nIDEvent);
  609. }
  610. void CXTPMenuBar::Reset(BOOL bShowWarningMessage)
  611. {
  612. if (GetControls()->GetOriginalControls() != NULL)
  613. {
  614. CXTPToolBar::Reset(bShowWarningMessage);
  615. }
  616. else if (((m_pMDIMenus->GetCount() > 0) || (GetControls()->GetOriginalControls() == NULL)) && m_nIDResourceCurrent)
  617. {
  618. if (bShowWarningMessage)
  619. {
  620. CString strMessage, strTitle = GetTitle(), strReset;
  621. VERIFY(XTPResourceManager()->LoadString(&strReset, XTP_IDS_CONFIRM_RESET));
  622. strMessage.Format(strReset, (LPCTSTR)strTitle);
  623. CXTPPushRoutingFrame push(GetFrameSite());
  624. if (XTPResourceManager()->ShowMessageBox(strMessage, MB_ICONWARNING | MB_OKCANCEL) != IDOK)
  625. return;
  626. }
  627. CMenu menu;
  628. VERIFY(menu.LoadMenu(m_nIDResourceCurrent));
  629. LoadMenu(&menu);
  630. CWnd* pWnd = GetSite();
  631. if (pWnd && ::IsWindow(pWnd->m_hWnd)) pWnd->SendMessage(WM_XTP_CUSTOMIZATION_RESETTOOLBAR, 0, (LPARAM)this);
  632. }
  633. RefreshMenu();
  634. OnInitialUpdate();
  635. OnRecalcLayout();
  636. }
  637. void CXTPMenuBar::Copy(CXTPCommandBar* pCommandBar, BOOL bRecursive)
  638. {
  639. CXTPToolBar::Copy(pCommandBar, bRecursive);
  640. m_nIDResource = ((CXTPMenuBar*)pCommandBar)->m_nIDResource;
  641. POSITION pos = ((CXTPMenuBar*)pCommandBar)->m_pMDIMenus->GetStartPosition();
  642. while (pos)
  643. {
  644. CXTPMenuBarMDIMenuInfo* pInfo;
  645. ((CXTPMenuBar*)pCommandBar)->m_pMDIMenus->GetNextMenu(pos, pInfo);
  646. CXTPMenuBarMDIMenuInfo* pInfoOrig = m_pMDIMenus->Lookup(pInfo->m_nIDResource);
  647. if (pInfoOrig)
  648. {
  649. if (pInfoOrig->m_pControls && pInfoOrig->m_pControls->m_pOriginalControls != NULL &&
  650. pInfo->m_pControls && pInfo->m_pControls->m_pOriginalControls == NULL)
  651. {
  652. CXTPOriginalControls* pOriginalControls = pInfoOrig->m_pControls->m_pOriginalControls;
  653. CMDTARGET_ADDREF(pOriginalControls);
  654. pInfo->m_pControls->SetOriginalControls(pOriginalControls);
  655. }
  656. CMDTARGET_RELEASE(pInfoOrig->m_pControls);
  657. pInfoOrig->m_pControls = pInfo->m_pControls;
  658. CMDTARGET_ADDREF(pInfoOrig->m_pControls);
  659. }
  660. else
  661. {
  662. m_pMDIMenus->SetAt(pInfo->m_nIDResource, pInfo);
  663. pInfo->InternalAddRef();
  664. }
  665. }
  666. }
  667. BOOL CXTPMenuBar::ShouldSerializeBar()
  668. {
  669. if (!IsCustomizable())
  670. return FALSE;
  671. BOOL bChanged = IsMDIMenusChanged();
  672. if (bChanged)
  673. return TRUE;
  674. if ((GetControls()->GetOriginalControls() && !GetControls()->IsChanged()))
  675. return FALSE;
  676. return TRUE;
  677. }