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

对话框与窗口

开发平台:

Visual C++

  1. // XTPCustomizeSheet.cpp : implementation of the CXTPCustomizeSheet 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 <math.h>
  22. #include "Resource.h"
  23. #include "Common/XTPResourceManager.h"
  24. #include "Common/XTPImageManager.h"
  25. #include "Common/XTPHookManager.h"
  26. #include "XTPCustomizeSheet.h"
  27. #include "XTPControls.h"
  28. #include "XTPToolBar.h"
  29. #include "XTPCustomizeTools.h"
  30. #include "XTPMouseManager.h"
  31. #include "XTPControlButton.h"
  32. #include "XTPControlEdit.h"
  33. #include "XTPCommandBars.h"
  34. #include "XTPCustomizeToolbarsPage.h"
  35. #include "XTPCustomizeCommandsPage.h"
  36. #include "XTPShortcutManager.h"
  37. #include "XTPPaintManager.h"
  38. #include "XTPImageEditor.h"
  39. #ifdef _DEBUG
  40. #define new DEBUG_NEW
  41. #undef THIS_FILE
  42. static char THIS_FILE[] = __FILE__;
  43. #endif
  44. #ifndef WS_EX_NOINHERITLAYOUT
  45. #define WS_EX_NOINHERITLAYOUT   0x00100000L
  46. #endif
  47. #define WM_GRABFOCUS (WM_APP + 0x130)
  48. class CXTPCustomizeSheet::CCustomizeButton : public CXTPControlButton
  49. {
  50. virtual void OnExecute()
  51. {
  52. CWnd* pOwner = m_pParent->GetOwnerSite();
  53. m_pParent->GetRootParent()->OnTrackLost();
  54. NMXTPCONTROL tagNMCONTROL;
  55. if (NotifySite(pOwner, CBN_XTP_EXECUTE, &tagNMCONTROL) == 0)
  56. {
  57. pOwner->SendMessage(WM_COMMAND, m_nId);
  58. }
  59. }
  60. };
  61. class CXTPCustomizeSheet::CCustomizeEdit : public CXTPControlEdit
  62. {
  63. BOOL OnSetSelected(int bSelected)
  64. {
  65. if (!bSelected && m_pEdit && ::GetFocus() == *m_pEdit)
  66. {
  67. SendCommand(FALSE);
  68. }
  69. return CXTPControlEdit::OnSetSelected(bSelected);
  70. }
  71. virtual void OnExecute()
  72. {
  73. SendCommand(TRUE);
  74. }
  75. void SendCommand(BOOL bTrackLost)
  76. {
  77. CWnd* pOwner = m_pParent->GetOwnerSite();
  78. if (bTrackLost) m_pParent->OnTrackLost();
  79. NMXTPCONTROL tagNMCONTROL;
  80. tagNMCONTROL.hdr.code = CBN_XTP_EXECUTE;
  81. tagNMCONTROL.hdr.idFrom = m_nId;
  82. tagNMCONTROL.hdr.hwndFrom = 0;
  83. tagNMCONTROL.pControl = this;
  84. pOwner->SendMessage(WM_XTP_COMMAND, m_nId, (LPARAM)&tagNMCONTROL);
  85. }
  86. };
  87. /////////////////////////////////////////////////////////////////////////////
  88. // CXTPCustomizeSheet
  89. IMPLEMENT_DYNAMIC(CXTPCustomizeSheet, CPropertySheet)
  90. CXTPCustomizeSheet::CXTPCustomizeSheet(CXTPCommandBars* pCommandBars, UINT iSelectPage)
  91. {
  92. CString strCaption;
  93. XTPResourceManager()->LoadString(&strCaption, XTP_IDS_CUSTOMIZE);
  94. Construct(strCaption, pCommandBars->GetSite(), iSelectPage);
  95. m_pPageToolBars = new CXTPCustomizeToolbarsPage(this);
  96. AddPage(m_pPageToolBars);
  97. m_pPageCommands = new CXTPCustomizeCommandsPage(this);
  98. AddPage(m_pPageCommands);
  99. m_pWndParent = pCommandBars->GetSite();
  100. m_pCommandBars = pCommandBars;
  101. CBitmap bmp;
  102. XTPResourceManager()->LoadBitmap(&bmp, XTP_IDB_CUSTOMIZE_ICONS);
  103. BITMAP bmpInfo;
  104. bmp.GetBitmap(&bmpInfo);
  105. int nCount = bmpInfo.bmWidth / 16;
  106. m_pCustomIcons = new CXTPImageManager;
  107. UINT* pIDs = new UINT[nCount];
  108. for (int i = 0; i < nCount; i++)
  109. {
  110. pIDs[i] = i + 1;
  111. }
  112. VERIFY(m_pCustomIcons->SetIcons(bmp, pIDs, nCount, CSize(16, 16), xtpImageNormal));
  113. delete[] pIDs;
  114. m_psh.dwFlags |= PSH_NOAPPLYNOW;
  115. m_pContextMenu = 0;
  116. CreateContextMenu();
  117. }
  118. CXTPCustomizeSheet::~CXTPCustomizeSheet()
  119. {
  120. delete m_pPageToolBars;
  121. delete m_pPageCommands;
  122. m_pContextMenu->InternalRelease();
  123. m_pCustomIcons->InternalRelease();
  124. }
  125. void CXTPCustomizeSheet::CreateContextMenu()
  126. {
  127. CMenu menu;
  128. if (!XTPResourceManager()->LoadMenu(&menu, XTP_IDR_CUSTOMIZEMENU))
  129. return;
  130. CMenu* pMenu = menu.GetSubMenu(0);
  131. ASSERT(pMenu);
  132. if (!pMenu)
  133. return;
  134. m_pContextMenu = CXTPPopupBar::CreatePopupBar(NULL);
  135. m_pContextMenu->m_pSite = m_pCommandBars->GetSite();
  136. m_pContextMenu->m_pOwner = this;
  137. CXTPPaintManager* pPaintManager = m_pCommandBars->GetPaintManager();
  138. pPaintManager->InternalAddRef();
  139. m_pContextMenu->SetPaintManager(pPaintManager);
  140. m_pCustomIcons->InternalAddRef();
  141. m_pCustomIcons->DrawReverted(m_pCommandBars->IsLayoutRTL());
  142. m_pContextMenu->SetImageManager(m_pCustomIcons);
  143. BOOL bSeparator = FALSE;
  144. for (int i = 0; i < ::GetMenuItemCount(pMenu->m_hMenu); i++)
  145. {
  146. MENUITEMINFO info = { sizeof(MENUITEMINFO), MIIM_TYPE | MIIM_STATE};
  147. ::GetMenuItemInfo(pMenu->m_hMenu, i, TRUE, &info);
  148. UINT nID = pMenu->GetMenuItemID(i);
  149. CString strCaption;
  150. XTPResourceManager()->GetMenuLocaleString(pMenu, i, strCaption, MF_BYPOSITION);
  151. if (((info.fType & MFT_SEPARATOR) == MFT_SEPARATOR) || (nID == 0))
  152. bSeparator = TRUE;
  153. else
  154. {
  155. CXTPControl* pControl = 0;
  156. if (nID == XTP_ID_CUSTOMIZE_CHANGEBUTTONIMAGE)
  157. {
  158. pControl = new CXTPControlPopup();
  159. CXTPPopupBar* pPopupBar = new CXTPPopupToolBar();
  160. pPopupBar->SetBarID(XTP_ID_CUSTOMIZE_CHANGEBUTTONIMAGE);
  161. ((CXTPControlPopup*)pControl)->SetCommandBar(pPopupBar);
  162. pPopupBar->InternalRelease();
  163. }
  164. else if (nID == XTP_ID_CUSTOMIZE_NAME)
  165. pControl = new CCustomizeEdit();
  166. else
  167. pControl = new CCustomizeButton();
  168. pControl->SetCaption(strCaption);
  169. ASSERT(pControl);
  170. m_pContextMenu->GetControls()->Add(pControl, nID);
  171. if (bSeparator)
  172. {
  173. pControl->SetBeginGroup(TRUE);
  174. bSeparator = FALSE;
  175. }
  176. }
  177. }
  178. }
  179. void CXTPCustomizeSheet::AddPage(CPropertyPage* pPage)
  180. {
  181. CPropertySheet::AddPage(pPage);
  182. LPCDLGTEMPLATE pResource = XTPResourceManager()->LoadDialogTemplate((UINT)(UINT_PTR)pPage->m_psp.pszTemplate);
  183. if (pResource)
  184. {
  185. pPage->m_psp.pResource = pResource;
  186. pPage->m_psp.dwFlags |= PSP_DLGINDIRECT;
  187. }
  188. }
  189. BEGIN_MESSAGE_MAP(CXTPCustomizeSheet, CPropertySheet)
  190. //{{AFX_MSG_MAP(CXTPCustomizeSheet)
  191. // NOTE - the ClassWizard will add and remove mapping macros here.
  192. //}}AFX_MSG_MAP
  193. ON_COMMAND(XTP_ID_CUSTOMIZE_DEFAULTSTYLE, OnCustomizeDefaultStyle)
  194. ON_COMMAND(XTP_ID_CUSTOMIZE_IMAGEANDTEXT, OnCustomizeImageAndText)
  195. ON_COMMAND(XTP_ID_CUSTOMIZE_TEXTONLY, OnCustomizeTextOnly)
  196. ON_COMMAND(XTP_ID_CUSTOMIZE_BEGINAGROUP, OnCustomizeBeginGroup)
  197. ON_COMMAND(XTP_ID_CUSTOMIZE_DELETE, OnCustomizeDelete)
  198. ON_COMMAND(XTP_ID_CUSTOMIZE_COPYBUTTONIMAGE, OnCustomizeCopyImage)
  199. ON_COMMAND(XTP_ID_CUSTOMIZE_PASTEBUTTONIMAGE, OnCustomizePasteImage)
  200. ON_COMMAND(XTP_ID_CUSTOMIZE_RESETBUTTONIMAGE, OnCustomizeResetImage)
  201. ON_COMMAND(XTP_ID_CUSTOMIZE_RESET, OnCustomizeReset)
  202. ON_COMMAND(XTP_ID_CUSTOMIZE_EDITBUTTONIMAGE, OnCustomizeEditImage)
  203. ON_UPDATE_COMMAND_UI(XTP_ID_CUSTOMIZE_DEFAULTSTYLE, OnUpdateCustomizeDefaultStyle)
  204. ON_UPDATE_COMMAND_UI(XTP_ID_CUSTOMIZE_IMAGEANDTEXT, OnUpdateCustomizeImageAndText)
  205. ON_UPDATE_COMMAND_UI(XTP_ID_CUSTOMIZE_TEXTONLY, OnUpdateCustomizeTextOnly)
  206. ON_UPDATE_COMMAND_UI(XTP_ID_CUSTOMIZE_BEGINAGROUP, OnUpdateCustomizeBeginGroup)
  207. ON_UPDATE_COMMAND_UI(XTP_ID_CUSTOMIZE_NAME, OnUpdateCustomizeName)
  208. ON_UPDATE_COMMAND_UI(XTP_ID_CUSTOMIZE_COPYBUTTONIMAGE, OnUpdateCustomizeCopyImage)
  209. ON_UPDATE_COMMAND_UI(XTP_ID_CUSTOMIZE_CHANGEBUTTONIMAGE, OnUpdateCustomizeCopyImage)
  210. ON_UPDATE_COMMAND_UI(XTP_ID_CUSTOMIZE_PASTEBUTTONIMAGE, OnUpdateCustomizePasteImage)
  211. ON_UPDATE_COMMAND_UI(XTP_ID_CUSTOMIZE_RESETBUTTONIMAGE, OnUpdateCustomizeResetImage)
  212. ON_UPDATE_COMMAND_UI(XTP_ID_CUSTOMIZE_RESET, OnUpdateCustomizeReset)
  213. ON_UPDATE_COMMAND_UI(XTP_ID_CUSTOMIZE_EDITBUTTONIMAGE, OnUpdateCustomizeEditImage)
  214. ON_XTP_INITCOMMANDSPOPUP()
  215. ON_MESSAGE(WM_XTP_COMMAND, OnCustomizeName)
  216. ON_WM_SYSKEYDOWN()
  217. ON_MESSAGE_VOID(WM_GRABFOCUS, OnGrabFocus)
  218. END_MESSAGE_MAP()
  219. /////////////////////////////////////////////////////////////////////////////
  220. // CXTPCustomizeSheet message handlers
  221. BOOL CXTPCustomizeSheet::OnInitDialog()
  222. {
  223. BOOL bResult = CPropertySheet::OnInitDialog();
  224. // Remove the OK button.
  225. CWnd* pWnd = GetDlgItem(IDOK);
  226. if (pWnd && ::IsWindow(pWnd->m_hWnd))
  227. {
  228. pWnd->ShowWindow(SW_HIDE);
  229. }
  230. // Rename the cancel button to 'Close"
  231. pWnd = GetDlgItem(IDCANCEL);
  232. if (pWnd && ::IsWindow(pWnd->m_hWnd))
  233. {
  234. CString strClose;
  235. XTPResourceManager()->LoadString(&strClose, XTP_IDS_CUSTOMIZE_CLOSE);
  236. pWnd->SetWindowText(strClose);
  237. }
  238. return bResult;
  239. }
  240. typedef  BOOL (CXTPCustomizeSheet::*FILTERPROC)(WPARAM wParam, LPARAM lParam);
  241. // A hook used in customization sheet to filter keyboard/mouse events
  242. class CXTPCustomizeHook
  243. {
  244. public:
  245. private:
  246. HHOOK m_hHook;
  247. FILTERPROC m_pFilter;
  248. CXTPCustomizeSheet* m_pSheet;
  249. public:
  250. CXTPCustomizeHook(CXTPCustomizeSheet* pSheet, FILTERPROC pFilter)
  251. : m_pFilter(pFilter), m_hHook(0), m_pSheet(pSheet)
  252. { }
  253. ~CXTPCustomizeHook()
  254. {
  255. Unhook();
  256. }
  257. void Hook(int idHook, HOOKPROC lpfn)
  258. {
  259. ASSERT(m_hHook == 0);
  260. m_hHook = ::SetWindowsHookEx(idHook, lpfn, NULL, ::GetCurrentThreadId());
  261. }
  262. void Unhook()
  263. {
  264. if (m_hHook)
  265. {
  266. ::UnhookWindowsHookEx(m_hHook);
  267. m_hHook = 0;
  268. }
  269. }
  270. LRESULT HandleEvent(int nCode, WPARAM wParam, LPARAM lParam)
  271. {
  272. if (nCode < 0)
  273. return TRUE;
  274. if ((m_pSheet->*m_pFilter)(wParam, lParam))
  275. {
  276. // process normally
  277. return  CallNextHookEx(m_hHook, nCode, wParam, lParam);
  278. }
  279. return TRUE;
  280. }
  281. };
  282. // A combination of keyboard/mouse hooks
  283. class CXTPCustomizeHooks
  284. {
  285. private:
  286. CXTPCustomizeHook m_mouseHook;
  287. CXTPCustomizeHook m_keyHook;
  288. CXTPCustomizeHook m_messageHook;
  289. static CXTPCustomizeHooks* m_pThis;
  290. public:
  291. CXTPCustomizeHooks(CXTPCustomizeSheet* pSheet, FILTERPROC FilterMouse, FILTERPROC FilterKey, FILTERPROC FilterMessage)
  292. : m_mouseHook(pSheet, FilterMouse)
  293. , m_keyHook(pSheet, FilterKey)
  294. , m_messageHook(pSheet, FilterMessage)
  295. {
  296. ASSERT(m_pThis == 0);  // one instance at a time
  297. m_pThis = this;
  298. }
  299. ~CXTPCustomizeHooks()
  300. {
  301. Unhook();
  302. m_pThis = 0;
  303. }
  304. void Hook()
  305. {
  306. m_mouseHook.Hook(WH_MOUSE, HOOKPROC(MouseProc));
  307. m_keyHook.Hook(WH_KEYBOARD, HOOKPROC(KeyProc));
  308. m_messageHook.Hook(WH_CALLWNDPROC, HOOKPROC(MessageProc));
  309. }
  310. void Unhook()
  311. {
  312. m_mouseHook.Unhook();
  313. m_keyHook.Unhook();
  314. m_messageHook.Unhook();
  315. }
  316. static LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
  317. {
  318. return m_pThis->m_mouseHook.HandleEvent(nCode, wParam, lParam);
  319. }
  320. static LRESULT CALLBACK KeyProc(int nCode, WPARAM wParam, LPARAM lParam)
  321. {
  322. return m_pThis->m_keyHook.HandleEvent(nCode, wParam, lParam);
  323. }
  324. static LRESULT CALLBACK MessageProc(int nCode, WPARAM wParam, LPARAM lParam)
  325. {
  326. return m_pThis->m_messageHook.HandleEvent(nCode, wParam, lParam);
  327. }
  328. friend class CXTPCustomizeSheet;
  329. };
  330. CXTPCustomizeHooks* CXTPCustomizeHooks::m_pThis = 0;
  331. void CXTPCustomizeSheet::ContextMenu(CPoint point)
  332. {
  333. if (m_pContextMenu)
  334. {
  335. BOOL bLayourRTL = m_pCommandBars->IsLayoutRTL();
  336. CXTPCommandBars::TrackPopupMenu(m_pContextMenu, TPM_RECURSE | (bLayourRTL ? TPM_RIGHTALIGN : 0), point.x, point.y, (CWnd*)m_pCommandBars->GetSite(), NULL, this);
  337. }
  338. }
  339. INT_PTR CXTPCustomizeSheet::DoModal()
  340. {
  341. if (CXTPCustomizeHooks::m_pThis != 0)
  342. return IDCANCEL;
  343. ASSERT_VALID(this);
  344. ASSERT(m_hWnd == NULL);
  345. m_pCommandBars->SetCustomizeMode(TRUE);
  346. m_pCommandBars->GetDropSource()->m_pSheet = this;
  347. // register common controls
  348. VERIFY(AfxDeferRegisterClass(AFX_WNDCOMMCTLS_REG));
  349. // finish building PROPSHEETHEADER structure
  350. BuildPropPageArray();
  351. // allow OLE servers to disable themselves
  352. CWinApp* pApp = AfxGetApp();
  353. if (pApp != NULL)
  354. pApp->EnableModeless(FALSE);
  355. // find parent HWND
  356. HWND hWndParent = m_pWndParent->GetSafeHwnd();
  357. #if _MSC_VER != 1200 // !MFC 6.0
  358. PROPSHEETHEADER* psh = &m_psh;
  359. #else
  360. AFX_OLDPROPSHEETHEADER* psh = GetPropSheetHeader();
  361. #endif //_MSC_VER != 1200
  362. psh->hwndParent = hWndParent;
  363. CXTPCustomizeHooks hooks(this, &CXTPCustomizeSheet::FilterMouse,
  364. &CXTPCustomizeSheet::FilterKey, &CXTPCustomizeSheet::FilterMessage);
  365. if (hWndParent)
  366. hooks.Hook();
  367. HWND hWndCapture = ::GetCapture();
  368. if (hWndCapture != NULL)
  369. ::SendMessage(hWndCapture, WM_CANCELMODE, 0, 0);
  370. // setup for modal loop and creation
  371. m_nModalResult = 0;
  372. m_nFlags |= WF_CONTINUEMODAL;
  373. // hook for creation of window
  374. AfxHookWindowCreate(this);
  375. psh->dwFlags |= PSH_MODELESS;
  376. m_nFlags |= WF_CONTINUEMODAL;
  377. HWND hWnd = (HWND)::PropertySheet((PROPSHEETHEADER*)psh);
  378. #ifdef _DEBUG
  379. DWORD dwError = ::GetLastError();
  380. #endif
  381. psh->dwFlags &= ~PSH_MODELESS;
  382. AfxUnhookWindowCreate();
  383. // handle error
  384. if (hWnd == NULL || hWnd == (HWND)-1)
  385. {
  386. TRACE1("PropertySheet() failed: GetLastError returned %dn", dwError);
  387. m_nFlags &= ~WF_CONTINUEMODAL;
  388. }
  389. if (GetCommandBars()->IsLayoutRTL())
  390. {
  391. ModifyStyleEx(0, WS_EX_LAYOUTRTL | WS_EX_NOINHERITLAYOUT, SWP_FRAMECHANGED);
  392. CWnd* pWndTabControl = GetDlgItem(12320);
  393. if (pWndTabControl)
  394. {
  395. pWndTabControl->ModifyStyleEx(0, WS_EX_LAYOUTRTL);
  396. pWndTabControl->Invalidate();
  397. }
  398. }
  399. int nResult = m_nModalResult;
  400. if (ContinueModal())
  401. {
  402. // enter modal loop
  403. DWORD dwFlags = MLF_SHOWONIDLE;
  404. if (GetStyle() & DS_NOIDLEMSG)
  405. dwFlags |= MLF_NOIDLEMSG;
  406. //CXTCustomizeSheetWndHook hook(m_bAppActive);  // disables window menu from taskbar
  407. nResult = RunModalLoop(dwFlags);
  408. }
  409. // hide the window before enabling parent window, etc.
  410. if (m_hWnd != NULL)
  411. {
  412. SetWindowPos(NULL, 0, 0, 0, 0, SWP_HIDEWINDOW |
  413. SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);
  414. }
  415. hooks.Unhook();
  416. bool bActivateParent = false;
  417. if (hWndParent != NULL && ::GetActiveWindow() == m_hWnd)
  418. {
  419. bActivateParent = TRUE;
  420. }
  421. // cleanup
  422. DestroyWindow();
  423. if (bActivateParent)
  424. {
  425. ::SetActiveWindow(hWndParent);
  426. }
  427. // allow OLE servers to enable themselves
  428. if (pApp != NULL)
  429. pApp->EnableModeless(TRUE);
  430. m_pCommandBars->SetCustomizeMode(FALSE);
  431. m_pCommandBars->GetDropSource()->m_pSheet = 0;
  432. m_pCommandBars->ClosePopups();
  433. return nResult;
  434. }
  435. static BOOL IsDescendant(HWND hwndParent, HWND hwnd)
  436. {
  437. while (hwnd)
  438. {
  439. if (hwndParent == hwnd)
  440. {
  441. return TRUE;
  442. }
  443. hwnd = ::GetParent(hwnd);
  444. }
  445. return FALSE;
  446. }
  447. BOOL CXTPCustomizeSheet::FilterMouse(WPARAM wParam, LPARAM lParam)
  448. {
  449. LPMOUSEHOOKSTRUCT pMsg = (LPMOUSEHOOKSTRUCT)(LPVOID)lParam;
  450. if (wParam == WM_MOUSEMOVE || wParam == WM_NCMOUSEMOVE)
  451. return TRUE;
  452. CXTPMouseManager* pMouseManager = m_pCommandBars->GetMouseManager();
  453. if (pMouseManager->IsTrackedLock(NULL))
  454. {
  455. CWnd* pWnd = CWnd::FromHandle(pMsg->hwnd);
  456. if (pWnd->IsKindOf(RUNTIME_CLASS(CXTPCommandBar)))
  457. {
  458. if (((CXTPCommandBar*)pWnd)->GetCommandBars() == NULL)
  459. return  TRUE;
  460. }
  461. CWnd* pWndParent = pWnd ? pWnd->GetParent() : 0;
  462. if (pWndParent && pWndParent->IsKindOf(RUNTIME_CLASS(CXTPCommandBar)))
  463. {
  464. if (((CXTPCommandBar*)pWndParent)->GetCommandBars() == NULL)
  465. return  TRUE;
  466. }
  467. pMouseManager->PreTranslateMouseEvents(wParam, pMsg->pt);
  468. }
  469. if (((m_pWndParent->GetStyle() & WS_CHILD) == 0) && (!IsDescendant(m_pWndParent->m_hWnd, ::GetForegroundWindow())))
  470. {
  471. SetForegroundWindow();
  472. SetFocus();
  473. }
  474. if (IsDescendant(m_pWndParent->m_hWnd, pMsg->hwnd))
  475. {
  476. if (IsDescendant(m_hWnd, pMsg->hwnd))
  477. {
  478. return TRUE;
  479. }
  480. if (::GetForegroundWindow() && IsDescendant(::GetForegroundWindow(), pMsg->hwnd))
  481. {
  482. return TRUE;
  483. }
  484. CWnd* pWnd = CWnd::FromHandle(pMsg->hwnd);
  485. CWnd* pWndParent = pWnd ? pWnd->GetParent() : 0;
  486. if (pWnd && pWnd->IsKindOf(RUNTIME_CLASS(CXTPCommandBar)))
  487. {
  488. CXTPCommandBars* pCommandBars = ((CXTPCommandBar*)pWnd)->GetCommandBars();
  489. return  pCommandBars == m_pCommandBars;
  490. }
  491. if (pWndParent && pWndParent->IsKindOf(RUNTIME_CLASS(CXTPCommandBar)))
  492. {
  493. CXTPCommandBars* pCommandBars = ((CXTPCommandBar*)pWndParent)->GetCommandBars();
  494. return  pCommandBars == m_pCommandBars;
  495. }
  496. // eat this message
  497. return FALSE;
  498. }
  499. return TRUE;
  500. }
  501. BOOL CXTPCustomizeSheet::FilterKey(WPARAM /*wParam*/, LPARAM /*lParam*/)
  502. {
  503. // allow keyboard events only if current focus window belongs to this one
  504. if (IsDescendant(m_hWnd, ::GetFocus()))
  505. {
  506. return TRUE;
  507. }
  508. CWnd* pWnd = GetFocus();
  509. CWnd* pWndParent = pWnd ? pWnd->GetParent() : 0;
  510. if (pWndParent && pWndParent->IsKindOf(RUNTIME_CLASS(CXTPCommandBar)))
  511. {
  512. return TRUE;
  513. }
  514. if (::GetForegroundWindow() && IsDescendant(::GetForegroundWindow(), ::GetFocus()))
  515. {
  516. return TRUE;
  517. }
  518. // pass thru if the capture is set (toolbar tracking does this)
  519. if (::GetCapture() != 0)
  520. {
  521. return TRUE;
  522. }
  523. // eat this message
  524. return FALSE;
  525. }
  526. BOOL CXTPCustomizeSheet::FilterMessage(WPARAM /*wParam*/, LPARAM lParam)
  527. {
  528. CWPSTRUCT* pMSG = (CWPSTRUCT*)lParam;
  529. if (pMSG && pMSG->message == WM_NCACTIVATE &&
  530. pMSG->hwnd == m_pWndParent->GetSafeHwnd() && pMSG->wParam)
  531. {
  532. PostMessage(WM_GRABFOCUS);
  533. return TRUE;
  534. }
  535. return TRUE;
  536. }
  537. void CXTPCustomizeSheet::OnCustomizeDefaultStyle()
  538. {
  539. CXTPControl* pControl = m_pCommandBars->GetDragControl();
  540. if (pControl)
  541. {
  542. pControl->m_buttonCustomStyle = xtpButtonAutomatic;
  543. pControl->GetParent()->OnRecalcLayout();
  544. }
  545. }
  546. void CXTPCustomizeSheet::OnCustomizeImageAndText()
  547. {
  548. CXTPControl* pControl = m_pCommandBars->GetDragControl();
  549. if (pControl)
  550. {
  551. pControl->m_buttonCustomStyle = xtpButtonIconAndCaption;
  552. pControl->GetParent()->OnRecalcLayout();
  553. }
  554. }
  555. void CXTPCustomizeSheet::OnCustomizeTextOnly()
  556. {
  557. CXTPControl* pControl = m_pCommandBars->GetDragControl();
  558. if (pControl)
  559. {
  560. pControl->m_buttonCustomStyle = xtpButtonCaption;
  561. pControl->GetParent()->OnRecalcLayout();
  562. }
  563. }
  564. void CXTPCustomizeSheet::OnCustomizeBeginGroup()
  565. {
  566. CXTPControl* pControl = m_pCommandBars->GetDragControl();
  567. if (pControl)
  568. {
  569. pControl->SetBeginGroup(!pControl->GetBeginGroup());
  570. pControl->GetParent()->OnRecalcLayout();
  571. }
  572. }
  573. void CXTPCustomizeSheet::OnCustomizeDelete()
  574. {
  575. CXTPControl* pControl = m_pCommandBars->GetDragControl();
  576. if (pControl)
  577. {
  578. GetCommandBars()->SetDragControl(NULL);
  579. CXTPCommandBar* pCommandBar = pControl->GetParent();
  580. pCommandBar->GetControls()->Remove(pControl);
  581. pCommandBar->OnRecalcLayout();
  582. }
  583. }
  584. void CXTPCustomizeSheet::OnCustomizeEditImage()
  585. {
  586. CXTPControl* pControl = m_pCommandBars->GetDragControl();
  587. if (pControl && pControl->GetIconId() != 0)
  588. {
  589. CSize szIcon = pControl->GetIconSize();
  590. CXTPImageManagerIcon* pImage = m_pCommandBars->GetImageManager()->GetImage(pControl->GetIconId(), szIcon.cx);
  591. CXTPImageEditorDlg ed(this, TRUE);
  592. if (pImage)
  593. {
  594. ed.SetIcon(pImage->GetIcon());
  595. }
  596. else
  597. {
  598. ed.SetIconSize(szIcon);
  599. }
  600. if (ed.DoModal() == IDOK)
  601. {
  602. CXTPImageManagerIconHandle hIcon;
  603. ed.GetIcon(&hIcon);
  604. if (hIcon.IsEmpty())
  605. return;
  606. if (pControl->m_nCustomIconId == 0)
  607. {
  608. pControl->m_nCustomIconId = m_pCommandBars->GetImageManager()->AddCustomIcon(hIcon);
  609. }
  610. else
  611. {
  612. pImage->SetIcon(hIcon);
  613. }
  614. pControl->GetParent()->OnRecalcLayout();
  615. }
  616. }
  617. }
  618. void CXTPCustomizeSheet::OnCustomizeCopyImage()
  619. {
  620. CXTPControl* pControl = m_pCommandBars->GetDragControl();
  621. if (pControl && pControl->GetIconId() != 0)
  622. {
  623. CXTPImageManagerIcon* pImage = m_pCommandBars->GetImageManager()->GetImage(pControl->GetIconId());
  624. if (pImage)
  625. {
  626. CXTPImageManagerIconHandle hIcon;
  627. hIcon.CopyHandle(pImage->GetIcon());
  628. CBitmap bitmapCopy;
  629. if (!hIcon.IsAlpha())
  630. {
  631. CSize sz = hIcon.GetExtent();
  632. CWindowDC dc(this);
  633. CDC memDC;
  634. memDC.CreateCompatibleDC (&dc);
  635. if (!bitmapCopy.CreateCompatibleBitmap(&dc, sz.cx, sz.cy))
  636. {
  637. return;
  638. }
  639. CBitmap* pOldBitmap = memDC.SelectObject (&bitmapCopy);
  640. memDC.FillSolidRect(0, 0, sz.cx, sz.cy, GetSysColor(COLOR_3DFACE));
  641. memDC.DrawState(CPoint(0, 0), sz, hIcon.GetIcon(), (UINT)DSS_NORMAL, HBRUSH(0));
  642. memDC.SelectObject (pOldBitmap);
  643. }
  644. else
  645. {
  646. bitmapCopy.Attach(CXTPImageManagerIcon::CopyAlphaBitmap(hIcon.GetBitmap()));
  647. }
  648. if (!OpenClipboard ())
  649. {
  650. return;
  651. }
  652. if (!::EmptyClipboard ())
  653. {
  654. ::CloseClipboard ();
  655. return;
  656. }
  657. HANDLE hclipData = ::SetClipboardData (CF_BITMAP, bitmapCopy.Detach ());
  658. if (hclipData == NULL)
  659. {
  660. TRACE (_T ("CXTPCustomizeSheet::OnCustomizeCopyImage() error. Error code = %xn"), GetLastError ());
  661. }
  662. if (hIcon.IsAlpha())
  663. {
  664. SetClipboardData(XTPImageManager()->m_nAlphaClipFormat, GlobalAlloc(GMEM_MOVEABLE, 1));
  665. }
  666. if (m_pCommandBars->GetImageManager()->GetIconSet(pControl->GetIconId()))
  667. {
  668. m_pCommandBars->GetImageManager()->CopyImage(pControl->GetIconId());
  669. }
  670. ::CloseClipboard ();
  671. }
  672. }
  673. }
  674. void CXTPCustomizeSheet::OnCustomizePasteImage()
  675. {
  676. CXTPControl* pControl = m_pCommandBars->GetDragControl();
  677. if (pControl)
  678. {
  679. COleDataObject data;
  680. if (!data.AttachClipboard ())
  681. {
  682. return;
  683. }
  684. if (data.IsDataAvailable(m_pCommandBars->GetImageManager()->m_nImageClipFormat))
  685. {
  686. int nCustom = m_pCommandBars->GetImageManager()->PasteCustomImage(data);
  687. if (nCustom != 0)
  688. {
  689. pControl->m_nCustomIconId = nCustom;
  690. pControl->GetParent()->OnRecalcLayout();
  691. }
  692. return;
  693. }
  694. if (!data.IsDataAvailable (CF_BITMAP))
  695. {
  696. return;
  697. }
  698. tagSTGMEDIUM dataMedium;
  699. if (!data.GetData (CF_BITMAP, &dataMedium))
  700. {
  701. return;
  702. }
  703. CBitmap* pBmpClip = CBitmap::FromHandle (dataMedium.hBitmap);
  704. if (pBmpClip == NULL)
  705. {
  706. return;
  707. }
  708. CXTPImageManagerIconHandle hIcon;
  709. if (data.IsDataAvailable(XTPImageManager()->m_nAlphaClipFormat))
  710. {
  711. hIcon.CopyHandle(dataMedium.hBitmap);
  712. }
  713. else
  714. {
  715. BITMAP bmp;
  716. pBmpClip->GetBitmap (&bmp);
  717. CImageList imgList;
  718. imgList.Create(bmp.bmWidth, bmp.bmHeight, ILC_COLOR24 | ILC_MASK, 0, 1);
  719. imgList.Add(pBmpClip, GetSysColor(COLOR_3DFACE));
  720. if (imgList.GetImageCount() != 1)
  721. return;
  722. hIcon = imgList.ExtractIcon(0);
  723. }
  724. if (hIcon.IsEmpty())
  725. return;
  726. pControl->m_nCustomIconId = m_pCommandBars->GetImageManager()->AddCustomIcon(hIcon);
  727. pControl->GetParent()->OnRecalcLayout();
  728. }
  729. }
  730. void CXTPCustomizeSheet::OnCustomizeResetImage()
  731. {
  732. CXTPControl* pControl = m_pCommandBars->GetDragControl();
  733. if (pControl)
  734. {
  735. pControl->m_nCustomIconId = 0;
  736. pControl->GetParent()->OnRecalcLayout();
  737. }
  738. }
  739. void CXTPCustomizeSheet::OnCustomizeReset()
  740. {
  741. CXTPControl* pControl = m_pCommandBars->GetDragControl();
  742. if (pControl)
  743. {
  744. pControl->Reset();
  745. }
  746. }
  747. void CXTPCustomizeSheet::OnInitCommandsPopup(CXTPPopupBar* pPopupBar)
  748. {
  749. if (pPopupBar->GetBarID() == XTP_ID_CUSTOMIZE_CHANGEBUTTONIMAGE)
  750. {
  751. if (pPopupBar->GetControlCount() > 0)
  752. return;
  753. int nCount = (int)m_pCustomIcons->GetImages()->GetCount();
  754. for (int i = 0; i < nCount; i++)
  755. {
  756. CXTPControl* pControl = pPopupBar->GetControls()->Add(new CCustomizeButton(), XTP_ID_CUSTOMIZE_CHANGEBUTTONIMAGE);
  757. pControl->SetIconId(i + 1);
  758. pControl->SetFlags(xtpFlagManualUpdate);
  759. }
  760. int nWidth = (int)pow((double)nCount, (double).5) * (16 + 7) + 6;
  761. pPopupBar->SetWidth(nWidth);
  762. }
  763. }
  764. BOOL CXTPCustomizeSheet::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
  765. {
  766. if (IsCustomizeCommand(nID))
  767. return CPropertySheet::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
  768. if ((nCode == CN_COMMAND) && (nID == IDCANCEL || nID == IDOK || nID == IDHELP))
  769. return CPropertySheet::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
  770. return m_pCommandBars->GetSite()->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
  771. }
  772. LRESULT CXTPCustomizeSheet::OnCustomizeName(WPARAM wParam, LPARAM lParam)
  773. {
  774. NMXTPCONTROL* tagNMCONTROL = (NMXTPCONTROL*)lParam;
  775. if (tagNMCONTROL->hdr.code != CBN_XTP_EXECUTE)
  776. return FALSE;
  777. if (wParam == XTP_ID_CUSTOMIZE_NAME)
  778. {
  779. CXTPControlEdit* pControlEdit = (CXTPControlEdit*)tagNMCONTROL->pControl;
  780. ASSERT(pControlEdit->GetType() == xtpControlEdit);
  781. CXTPControl* pControl = m_pCommandBars->GetDragControl();
  782. if (pControl)
  783. {
  784. pControl->m_strCustomCaption = pControlEdit->GetEditText();
  785. pControl->OnCaptionChanged();
  786. pControl->GetParent()->OnRecalcLayout();
  787. }
  788. return TRUE;
  789. }
  790. if (wParam == XTP_ID_CUSTOMIZE_CHANGEBUTTONIMAGE)
  791. {
  792. UINT nID = tagNMCONTROL->pControl->GetIconId();
  793. CXTPControl* pControl = m_pCommandBars->GetDragControl();
  794. if (pControl)
  795. {
  796. CXTPImageManagerIconSet* pIconSet = m_pCustomIcons->GetIconSet(nID);
  797. ASSERT(pIconSet);
  798. ASSERT(pIconSet->GetIcons()->GetCount() == 1);
  799. if (pIconSet && pIconSet->GetIcons()->GetCount() == 1)
  800. {
  801. POSITION pos = pIconSet->GetIcons()->GetStartPosition();
  802. CXTPImageManagerIcon* pIcon = NULL;
  803. pIconSet->GetIcons()->GetNextAssoc(pos, nID, pIcon);
  804. pControl->m_nCustomIconId = m_pCommandBars->GetImageManager()->AddCustomIcon(pIcon->GetIcon());
  805. pControl->GetParent()->OnRecalcLayout();
  806. }
  807. }
  808. return TRUE;
  809. }
  810. if (!IsCustomizeCommand((int)wParam))
  811. {
  812. tagNMCONTROL->pControl->GetParent()->GetRootParent()->OnTrackLost();
  813. if (m_pCommandBars->GetSite()->SendMessage(WM_XTP_COMMAND, wParam, lParam) == 0)
  814. {
  815. m_pCommandBars->GetSite()->SendMessage(WM_COMMAND, wParam);
  816. }
  817. return TRUE;
  818. }
  819. return FALSE;
  820. }
  821. BOOL CXTPCustomizeSheet::IsAllowChangeStyle(CXTPControl* pControl) const
  822. {
  823. if (!pControl)
  824. return FALSE;
  825. if (pControl->GetType() == xtpControlButton || IsPopupControlType(pControl->GetType()))
  826. {
  827. return pControl->GetIconId() > 0;
  828. }
  829. return FALSE;
  830. }
  831. void CXTPCustomizeSheet::OnUpdateCustomizeDefaultStyle(CCmdUI* pCmdUI)
  832. {
  833. CXTPControl* pControl = m_pCommandBars->GetDragControl();
  834. if (IsAllowChangeStyle(pControl))
  835. {
  836. pCmdUI->Enable();
  837. pCmdUI->SetCheck((pControl->GetStyle() == xtpButtonAutomatic || pControl->GetStyle() == xtpButtonIcon));
  838. }
  839. else
  840. {
  841. pCmdUI->SetCheck(TRUE);
  842. pCmdUI->Enable(FALSE);
  843. }
  844. }
  845. void CXTPCustomizeSheet::OnUpdateCustomizeImageAndText(CCmdUI* pCmdUI)
  846. {
  847. CXTPControl* pControl = m_pCommandBars->GetDragControl();
  848. if (IsAllowChangeStyle(pControl))
  849. {
  850. pCmdUI->Enable();
  851. pCmdUI->SetCheck(pControl->GetStyle() == xtpButtonIconAndCaption);
  852. }
  853. else pCmdUI->Enable(FALSE);
  854. }
  855. void CXTPCustomizeSheet::OnUpdateCustomizeTextOnly(CCmdUI* pCmdUI)
  856. {
  857. CXTPControl* pControl = m_pCommandBars->GetDragControl();
  858. if (IsAllowChangeStyle(pControl))
  859. {
  860. pCmdUI->Enable();
  861. pCmdUI->SetCheck(pControl->GetStyle() == xtpButtonCaption);
  862. }
  863. else pCmdUI->Enable(FALSE);
  864. }
  865. void CXTPCustomizeSheet::OnUpdateCustomizeCopyImage(CCmdUI* pCmdUI)
  866. {
  867. CXTPControl* pControl = m_pCommandBars->GetDragControl();
  868. if (pControl)
  869. {
  870. XTPControlType controlType = pControl->GetType();
  871. BOOL bDrawImage = IsPopupControlType(controlType) || (controlType == xtpControlButton);
  872. pCmdUI->Enable(pControl->GetIconId() > 0 && bDrawImage);
  873. }
  874. }
  875. void CXTPCustomizeSheet::OnUpdateCustomizePasteImage(CCmdUI* pCmdUI)
  876. {
  877. CXTPControl* pControl = m_pCommandBars->GetDragControl();
  878. if (pControl)
  879. {
  880. XTPControlType controlType = pControl->GetType();
  881. BOOL bDrawImage = IsPopupControlType(controlType) || (controlType == xtpControlButton);
  882. pCmdUI->Enable(::IsClipboardFormatAvailable(CF_BITMAP) && bDrawImage);
  883. }
  884. }
  885. void CXTPCustomizeSheet::OnUpdateCustomizeEditImage(CCmdUI* pCmdUI)
  886. {
  887. CXTPControl* pControl = m_pCommandBars->GetDragControl();
  888. if (pControl)
  889. {
  890. XTPControlType controlType = pControl->GetType();
  891. BOOL bDrawImage = IsPopupControlType(controlType) || (controlType == xtpControlButton);
  892. pCmdUI->Enable(pControl->GetIconId() > 0 && bDrawImage);
  893. }
  894. }
  895. void CXTPCustomizeSheet::OnUpdateCustomizeResetImage(CCmdUI* pCmdUI)
  896. {
  897. CXTPControl* pControl = m_pCommandBars->GetDragControl();
  898. if (pControl)
  899. {
  900. pCmdUI->Enable(pControl->m_nCustomIconId > 0);
  901. }
  902. }
  903. void CXTPCustomizeSheet::OnUpdateCustomizeReset(CCmdUI* pCmdUI)
  904. {
  905. CXTPControl* pControl = m_pCommandBars->GetDragControl();
  906. if (pControl)
  907. {
  908. pCmdUI->Enable((pControl->m_nCustomIconId > 0) || (!pControl->m_strCustomCaption.IsEmpty())
  909. || (pControl->m_buttonCustomStyle != xtpButtonUndefined));
  910. }
  911. }
  912. void CXTPCustomizeSheet::OnUpdateCustomizeBeginGroup(CCmdUI* pCmdUI)
  913. {
  914. pCmdUI->SetCheck(FALSE);
  915. CXTPControl* pControl = m_pCommandBars->GetDragControl();
  916. if (pControl)
  917. {
  918. pCmdUI->SetCheck(pControl->GetBeginGroup());
  919. pCmdUI->Enable(pControl->GetIndex() != 0);
  920. }
  921. }
  922. void CXTPCustomizeSheet::OnUpdateCustomizeName(CCmdUI* pCmdUI)
  923. {
  924. CXTPCommandBar* pCommandBar = (CXTPCommandBar*)pCmdUI->m_pOther;
  925. if (pCommandBar)
  926. {
  927. CXTPControlEdit* pEdit = (CXTPControlEdit*)pCommandBar->GetControls()->GetAt(pCmdUI->m_nIndex);
  928. CXTPControl* pControl = m_pCommandBars->GetDragControl();
  929. pEdit->SetEditText(pControl ? pControl->GetCaption() : _T(""));
  930. }
  931. pCmdUI->Enable(TRUE);
  932. }
  933. void CXTPCustomizeSheet::OnSysKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
  934. {
  935. CPropertySheet::OnSysKeyDown(nChar, nRepCnt, nFlags);
  936. }
  937. void CXTPCustomizeSheet::OnGrabFocus()
  938. {
  939. SetFocus();
  940. }
  941. BOOL CXTPCustomizeSheet::PreTranslateMessage(MSG* pMsg)
  942. {
  943. if (pMsg->message == WM_SYSKEYDOWN)
  944. {
  945. CTabCtrl* pTabCtrl = GetTabControl();
  946. if (pTabCtrl)
  947. {
  948. for (int i = 0; i < pTabCtrl->GetItemCount(); i++)
  949. {
  950. TCHAR lpCaption[256];
  951. lpCaption[0] = _T('');
  952. TCITEM tcItem;
  953. tcItem.mask = TCIF_TEXT;
  954. tcItem.pszText = lpCaption;
  955. tcItem.cchTextMax = 256;
  956. if (pTabCtrl->GetItem(i, &tcItem))
  957. {
  958. TCHAR* pAmp = _tcsrchr(lpCaption, _T('&'));
  959. if (pAmp)
  960. {
  961. if (CXTPShortcutManager::CompareAccelKey(*(pAmp + 1), (TCHAR)pMsg->wParam))
  962. {
  963. SetActivePage(i);
  964. return TRUE;
  965. }
  966. }
  967. }
  968. }
  969. }
  970. }
  971. return  CPropertySheet::PreTranslateMessage(pMsg);
  972. }
  973. int CXTPCustomizeSheet::MessageBox(LPCTSTR lpszText, UINT nType/* = MB_OK*/)
  974. {
  975. HWND hWndFocus = ::GetFocus();
  976. // disable windows for modal dialog
  977. AfxGetApp()->EnableModeless(FALSE);
  978. HWND hWndTop = m_hWnd;
  979. HWND hWndParent = GetCommandBars()->GetSite()->GetSafeHwnd();
  980. ::EnableWindow(hWndTop, FALSE);
  981. BOOL bEnableParent = FALSE;
  982. if (hWndParent != NULL && ::IsWindowEnabled(hWndParent))
  983. {
  984. ::EnableWindow(hWndParent, FALSE);
  985. bEnableParent = TRUE;
  986. }
  987. LPCTSTR pszAppName = AfxGetApp()->m_pszAppName;
  988. if (::GetWindowLong(hWndTop, GWL_EXSTYLE) & WS_EX_LAYOUTRTL)
  989. nType |= MB_RTLREADING;
  990. int nResult =
  991. ::MessageBox(hWndTop, lpszText, pszAppName, nType);
  992. // re-enable windows
  993. if (bEnableParent)
  994. ::EnableWindow(hWndParent, TRUE);
  995. if (::IsWindow(hWndTop))
  996. ::EnableWindow(hWndTop, TRUE);
  997. AfxGetApp()->EnableModeless(TRUE);
  998. ::SetFocus(hWndFocus);
  999. return nResult;
  1000. }