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

对话框与窗口

开发平台:

Visual C++

  1. // XTPCustomizeCommandsPage.cpp : implementation of the CXTPCustomizeCommandsPage 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/XTPDrawHelpers.h"
  24. #include "XTPControls.h"
  25. #include "XTPControl.h"
  26. #include "XTPControlPopup.h"
  27. #include "XTPPaintManager.h"
  28. #include "XTPCustomizeTools.h"
  29. #include "XTPCustomizeSheet.h"
  30. #include "XTPCommandBar.h"
  31. #include "XTPToolBar.h"
  32. #include "XTPCommandBars.h"
  33. #include "XTPCustomizeCommandsPage.h"
  34. #include "XTPMouseManager.h"
  35. #ifdef _DEBUG
  36. #define new DEBUG_NEW
  37. #undef THIS_FILE
  38. static char THIS_FILE[] = __FILE__;
  39. #endif
  40. XTP_COMMANDBARS_CATEGORYINFO::XTP_COMMANDBARS_CATEGORYINFO(LPCTSTR _strCategory, CXTPCommandBars* pCommandBars)
  41. {
  42. strCategory = _strCategory;
  43. pControls = new CXTPControls;
  44. pControls->SetCommandBars(pCommandBars);
  45. }
  46. XTP_COMMANDBARS_CATEGORYINFO::~XTP_COMMANDBARS_CATEGORYINFO()
  47. {
  48. pControls->InternalRelease();
  49. }
  50. void CXTPCustomizeCommandsListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  51. {
  52. CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
  53. CRect rc(lpDrawItemStruct->rcItem);
  54. CXTPControl* pControl = (CXTPControl*)lpDrawItemStruct->itemData;
  55. if (pControl)
  56. {
  57. BOOL bSelected = (lpDrawItemStruct->itemState & ODS_SELECTED);
  58. CXTPCommandBars* pCommandBars = m_pCommandBars;
  59. pCommandBars->GetPaintManager()->DrawListBoxControl(pDC, pControl, rc, bSelected, TRUE, pCommandBars);
  60. }
  61. }
  62. BEGIN_MESSAGE_MAP(CXTPCustomizeCommandsListBox, CListBox)
  63. //{{AFX_MSG_MAP(CXTPCustomizeCommandsPage)
  64. ON_WM_LBUTTONDOWN()
  65. //}}AFX_MSG_MAP
  66. END_MESSAGE_MAP()
  67. LRESULT CXTPCustomizeCommandsListBox::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  68. {
  69. switch (message)
  70. {
  71. case WM_PAINT:
  72. {
  73. CPaintDC dc(this);
  74. CXTPClientRect rc(this);
  75. CXTPBufferDC memDC(dc, rc);
  76. m_pCommandBars->GetPaintManager()->DrawListBoxControl(&memDC, NULL, CXTPClientRect(this), FALSE, TRUE, m_pCommandBars);
  77. return CListBox::DefWindowProc(WM_PAINT, (WPARAM)memDC.m_hDC, 0);
  78. }
  79. case WM_ERASEBKGND:
  80. CDC* pDC = CDC::FromHandle((HDC)wParam);
  81. m_pCommandBars->GetPaintManager()->DrawListBoxControl(pDC, NULL, CXTPClientRect(this), FALSE, TRUE, m_pCommandBars);
  82. return TRUE;
  83. }
  84. return CListBox::WindowProc(message, wParam, lParam);
  85. }
  86. void CXTPCustomizeCommandsListBox::OnLButtonDown(UINT nFlags, CPoint point)
  87. {
  88. CXTPCommandBars* pCommandBars = m_pCommandBars;
  89. ASSERT(pCommandBars);
  90. if (!pCommandBars)
  91. return;
  92. pCommandBars->SetDragControl(NULL);
  93. BOOL bOutside;
  94. int nIndex = (int)ItemFromPoint(point, bOutside);
  95. CXTPControl* pControl = NULL;
  96. if (!bOutside && m_bDragable)
  97. {
  98. if (SetCurSel(nIndex) != LB_ERR)
  99. {
  100. // Already selected, start drag operation
  101. pControl = (CXTPControl*)GetItemData(nIndex);
  102. // since we are eating the message, if the selection has changed, notify the owner.
  103. CWnd* pWndOwner = GetOwner();
  104. if (pWndOwner)
  105. {
  106. SetFocus();
  107. pWndOwner->PostMessage(WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(),
  108. LBN_SELCHANGE), (LPARAM)m_hWnd);
  109. }
  110. // Special case for New Menu
  111. if (pControl && pControl->GetCommandBar() &&
  112. pControl->GetCommandBar()->GetControlCount() == 0)
  113. {
  114. pControl = pControl->Clone(TRUE);
  115. CXTPCustomizeDropSource* pDropSource = pCommandBars->GetDropSource();
  116. pDropSource->DoDragDrop(pControl, TRUE);
  117. pControl->InternalRelease();
  118. return;
  119. }
  120. if (pControl)
  121. {
  122. CXTPCustomizeDropSource* pDropSource = pCommandBars->GetDropSource();
  123. pDropSource->DoDragDrop(pControl, TRUE);
  124. return;
  125. }
  126. }
  127. }
  128. // process as a click first
  129. CListBox::OnLButtonDown(nFlags, point);
  130. }
  131. /////////////////////////////////////////////////////////////////////////////
  132. // CXTPCustomizeCommandsPage property page
  133. IMPLEMENT_DYNCREATE(CXTPCustomizeCommandsPage, CPropertyPage)
  134. CXTPCustomizeCommandsPage::CXTPCustomizeCommandsPage(CXTPCustomizeSheet* pSheet)
  135. : CPropertyPage(XTP_IDD_PAGE_COMMANDS), m_pSheet(pSheet)
  136. {
  137. //{{AFX_DATA_INIT(CXTPCustomizeCommandsPage)
  138. // NOTE: the ClassWizard will add member initialization here
  139. //}}AFX_DATA_INIT
  140. }
  141. CXTPCustomizeCommandsPage::~CXTPCustomizeCommandsPage()
  142. {
  143. for (int i = 0; i < m_arrCategories.GetSize(); i++)
  144. {
  145. delete m_arrCategories[i];
  146. }
  147. }
  148. void CXTPCustomizeCommandsPage::DoDataExchange(CDataExchange* pDX)
  149. {
  150. CPropertyPage::DoDataExchange(pDX);
  151. //{{AFX_DATA_MAP(CXTPCustomizeCommandsPage)
  152. DDX_Control(pDX, XTP_IDC_LIST_COMMANDS, m_lstCommands);
  153. DDX_Control(pDX, XTP_IDC_LIST_CATEGORIES, m_lstCategories);
  154. //}}AFX_DATA_MAP
  155. }
  156. XTP_COMMANDBARS_CATEGORYINFO* CXTPCustomizeCommandsPage::FindCategory(LPCTSTR strCategory) const
  157. {
  158. for (int i = 0; i < m_arrCategories.GetSize(); i++)
  159. {
  160. if (m_arrCategories[i]->strCategory.Compare(strCategory) == 0)
  161. return m_arrCategories[i];
  162. }
  163. return NULL;
  164. }
  165. XTP_COMMANDBARS_CATEGORYINFO* CXTPCustomizeCommandsPage::GetCategoryInfo(int nIndex)
  166. {
  167. if (nIndex >= 0 && nIndex < m_arrCategories.GetSize())
  168. return m_arrCategories[nIndex];
  169. return NULL;
  170. }
  171. BOOL CXTPCustomizeCommandsPage::AddCategory(LPCTSTR strCategory, CMenu* pMenu, BOOL bListSubItems)
  172. {
  173. CXTPControls* pCategoryControls = InsertCategory(strCategory);
  174. int nCount = pMenu->GetMenuItemCount();
  175. for (int i = 0; i < nCount; i++)
  176. {
  177. if (pMenu->GetMenuItemID(i) > 0)
  178. {
  179. CXTPControlPopup* pControl = DYNAMIC_DOWNCAST(CXTPControlPopup,
  180. pCategoryControls->AddMenuItem(pMenu, i));
  181. if (pControl && bListSubItems)
  182. {
  183. CXTPControls* pControls = pControl->GetCommandBar()->GetControls();
  184. for (int j = 0; j < pControls->GetCount(); j++)
  185. {
  186. pCategoryControls->AddClone(pControls->GetAt(j));
  187. }
  188. }
  189. }
  190. }
  191. return TRUE;
  192. }
  193. BOOL CXTPCustomizeCommandsPage::_LoadToolBar(CXTPCommandBar* pCommandBar, UINT nIDResource)
  194. {
  195. LPCTSTR lpszResourceName = MAKEINTRESOURCE(nIDResource);
  196. struct CToolBarData
  197. {
  198. WORD wVersion;
  199. WORD wWidth;
  200. WORD wHeight;
  201. WORD wItemCount;
  202. WORD* items()
  203. { return (WORD*)(this + 1); }
  204. };
  205. ASSERT(lpszResourceName != NULL);
  206. if (!lpszResourceName)
  207. return FALSE;
  208. // determine location of the bitmap in resource fork
  209. HINSTANCE hInst = AfxFindResourceHandle(lpszResourceName, RT_TOOLBAR);
  210. if (!hInst)
  211. return FALSE;
  212. HRSRC hRsrc = ::FindResource(hInst, lpszResourceName, RT_TOOLBAR);
  213. if (!hRsrc)
  214. return FALSE;
  215. HGLOBAL hGlobal = LoadResource(hInst, hRsrc);
  216. if (!hGlobal)
  217. return FALSE;
  218. CToolBarData* pData = (CToolBarData*)LockResource(hGlobal);
  219. if (!pData)
  220. return FALSE;
  221. ASSERT(pData->wVersion == 1);
  222. ASSERT(pData->wWidth == 16);
  223. int i;
  224. UINT* pItems = new UINT[pData->wItemCount];
  225. for (i = 0; i < pData->wItemCount; i++)
  226. pItems[i] = pData->items()[i];
  227. BOOL bResult = pCommandBar->SetButtons(pItems, pData->wItemCount);
  228. delete[] pItems;
  229. UnlockResource(hGlobal);
  230. FreeResource(hGlobal);
  231. return bResult;
  232. }
  233. BOOL CXTPCustomizeCommandsPage::AddToolbarCategory(LPCTSTR strCategory, UINT nIDResource)
  234. {
  235. CXTPControls* pCategoryControls = InsertCategory(strCategory);
  236. ASSERT(pCategoryControls);
  237. if (!pCategoryControls)
  238. return FALSE;
  239. CXTPCommandBar* pCommandBar = (CXTPCommandBar*)CXTPCommandBar::CreateObject();
  240. pCommandBar->SetCommandBars(m_pSheet->GetCommandBars());
  241. if (!_LoadToolBar(pCommandBar, nIDResource))
  242. {
  243. pCommandBar->InternalRelease();
  244. return FALSE;
  245. }
  246. for (int i = 0; i < pCommandBar->GetControlCount(); i++)
  247. {
  248. pCategoryControls->AddClone(pCommandBar->GetControl(i));
  249. }
  250. pCommandBar->InternalRelease();
  251. return TRUE;
  252. }
  253. BOOL CXTPCustomizeCommandsPage::AddCategories(UINT nIDResource, BOOL bListSubItems)
  254. {
  255. CMenu menu;
  256. if (!XTPResourceManager()->LoadMenu(&menu, nIDResource))
  257. return FALSE;
  258. int nCount = menu.GetMenuItemCount();
  259. for (int i = 0; i < nCount; i++)
  260. {
  261. CString strCategory;
  262. if (XTPResourceManager()->GetMenuLocaleString(&menu, i, strCategory, MF_BYPOSITION) > 0)
  263. {
  264. CMenu* pMenu = menu.GetSubMenu(i);
  265. if (pMenu)
  266. {
  267. CXTPPaintManager::StripMnemonics(strCategory);
  268. int iPos = strCategory.Find(_T('t'));
  269. if (iPos > 0)
  270. {
  271. strCategory.ReleaseBuffer(iPos);
  272. }
  273. if (!AddCategory(strCategory, pMenu, bListSubItems))
  274. return FALSE;
  275. }
  276. }
  277. }
  278. return TRUE;
  279. }
  280. BOOL CXTPCustomizeCommandsPage::AddCategories(CXTPControls* pControls)
  281. {
  282. for (int i = 0; i < pControls->GetCount(); i++)
  283. {
  284. CXTPControl* pControl = pControls->GetAt(i);
  285. CString strCategory = pControl->GetCategory();
  286. if (!strCategory.IsEmpty())
  287. {
  288. CXTPControls* pCategoryControls = InsertCategory(strCategory);
  289. pCategoryControls->AddClone(pControl);
  290. }
  291. }
  292. return TRUE;
  293. }
  294. BOOL CXTPCustomizeCommandsPage::InsertBuiltInMenus(UINT nIDResourceMenu, UINT nIDResourceCategory, int nIndex)
  295. {
  296. CMenu menu;
  297. if (!XTPResourceManager()->LoadMenu(&menu, nIDResourceMenu))
  298. return FALSE;
  299. CString strCategory;
  300. XTPResourceManager()->LoadString(&strCategory, nIDResourceCategory);
  301. ASSERT(FindCategory(strCategory) == NULL);
  302. XTP_COMMANDBARS_CATEGORYINFO* pInfo = new XTP_COMMANDBARS_CATEGORYINFO(strCategory, m_pSheet->GetCommandBars());
  303. m_arrCategories.InsertAt(nIndex == -1 ? m_arrCategories.GetSize() : nIndex, pInfo);
  304. int nCount = menu.GetMenuItemCount();
  305. for (int i = 0; i < nCount; i++)
  306. {
  307. pInfo->pControls->AddMenuItem(&menu, i);
  308. }
  309. return TRUE;
  310. }
  311. void CXTPCustomizeCommandsPage::InsertNewMenuCategory(UINT nIDResource, int nIndex)
  312. {
  313. CString strCategory;
  314. XTPResourceManager()->LoadString(&strCategory, nIDResource);
  315. ASSERT(FindCategory(strCategory) == NULL);
  316. XTP_COMMANDBARS_CATEGORYINFO* pInfo = new XTP_COMMANDBARS_CATEGORYINFO(strCategory, m_pSheet->GetCommandBars());
  317. m_arrCategories.InsertAt(nIndex == -1 ? m_arrCategories.GetSize() : nIndex, pInfo);
  318. CXTPControl* pControl = pInfo->pControls->Add(xtpControlPopup, 0);
  319. pControl->SetCaption(strCategory);
  320. }
  321. CXTPControls* CXTPCustomizeCommandsPage::InsertCategory(LPCTSTR strCategory, int nIndex)
  322. {
  323. XTP_COMMANDBARS_CATEGORYINFO* pInfo = FindCategory(strCategory);
  324. if (!pInfo)
  325. {
  326. pInfo = new XTP_COMMANDBARS_CATEGORYINFO(strCategory, m_pSheet->GetCommandBars());
  327. m_arrCategories.InsertAt(nIndex == -1 ? m_arrCategories.GetSize() : nIndex, pInfo);
  328. }
  329. return pInfo->pControls;
  330. }
  331. CXTPControls* CXTPCustomizeCommandsPage::GetControls(LPCTSTR strCategory)
  332. {
  333. XTP_COMMANDBARS_CATEGORYINFO* pInfo = FindCategory(strCategory);
  334. return pInfo ? pInfo->pControls : NULL;
  335. }
  336. void CXTPCustomizeCommandsPage::InsertAllCommandsCategory(UINT nIDResource, int nIndex)
  337. {
  338. CString strCategory;
  339. XTPResourceManager()->LoadString(&strCategory, nIDResource);
  340. ASSERT(FindCategory(strCategory) == NULL);
  341. XTP_COMMANDBARS_CATEGORYINFO* pInfo = new XTP_COMMANDBARS_CATEGORYINFO(strCategory, m_pSheet->GetCommandBars());
  342. for (int i = 0; i < m_arrCategories.GetSize(); i++)
  343. {
  344. CXTPControls* pControls = m_arrCategories[i]->pControls;
  345. for (int j = 0; j < pControls->GetCount(); j++)
  346. {
  347. pInfo->pControls->AddClone(pControls->GetAt(j));
  348. }
  349. }
  350. m_arrCategories.InsertAt(nIndex == -1 ? m_arrCategories.GetSize() : nIndex, pInfo);
  351. }
  352. BEGIN_MESSAGE_MAP(CXTPCustomizeCommandsPage, CPropertyPage)
  353. //{{AFX_MSG_MAP(CXTPCustomizeCommandsPage)
  354. ON_LBN_SELCHANGE(XTP_IDC_LIST_CATEGORIES, OnCategoriesSelectionChanged)
  355. ON_WM_DESTROY()
  356. //}}AFX_MSG_MAP
  357. END_MESSAGE_MAP()
  358. /////////////////////////////////////////////////////////////////////////////
  359. // CXTPCustomizeCommandsPage message handlers
  360. BOOL CXTPCustomizeCommandsPage::OnInitDialog()
  361. {
  362. CPropertyPage::OnInitDialog();
  363. for (int i = 0; i < m_arrCategories.GetSize(); i++)
  364. {
  365. int nIndex = m_lstCategories.AddString(m_arrCategories[i]->strCategory);
  366. m_lstCategories.SetItemData(nIndex, i);
  367. if (i == 0) m_lstCategories.SetCurSel(0);
  368. }
  369. CXTPCommandBars* pCommandBars = m_pSheet->GetCommandBars();
  370. CSize sz = pCommandBars->GetPaintManager()->DrawListBoxControl(NULL, NULL, CRect(0, 0, 0, 0), FALSE, FALSE, pCommandBars);
  371. m_lstCommands.SetItemHeight(-1, sz.cy);
  372. m_lstCommands.m_pCommandBars = m_pSheet->GetCommandBars();
  373. m_lstCommands.m_bDragable = TRUE;
  374. if (pCommandBars->IsLayoutRTL())
  375. {
  376. m_lstCommands.ModifyStyleEx(0, WS_EX_LAYOUTRTL);
  377. m_lstCategories.ModifyStyleEx(0, WS_EX_RIGHT | WS_EX_LEFTSCROLLBAR | WS_EX_RTLREADING);
  378. }
  379. pCommandBars->GetMouseManager()->AddTrustedWindow(m_hWnd);
  380. OnCategoriesSelectionChanged();
  381. return FALSE;
  382. }
  383. void CXTPCustomizeCommandsPage::OnDestroy()
  384. {
  385. CXTPCommandBars* pCommandBars = m_pSheet->GetCommandBars();
  386. pCommandBars->GetMouseManager()->RemoveTrustedWindow(m_hWnd);
  387. CPropertyPage::OnDestroy();
  388. }
  389. void CXTPCustomizeCommandsPage::OnCategoriesSelectionChanged()
  390. {
  391. m_lstCommands.ResetContent();
  392. int nIndex = m_lstCategories.GetCurSel();
  393. if (nIndex == LB_ERR)
  394. return;
  395. XTP_COMMANDBARS_CATEGORYINFO* pInfo = GetCategoryInfo((int)m_lstCategories.GetItemData(nIndex));
  396. if (pInfo == NULL)
  397. return;
  398. for (int i = 0; i < pInfo->pControls->GetCount(); i++)
  399. {
  400. CXTPControl* pControl = pInfo->pControls->GetAt(i);
  401. m_lstCommands.SendMessage(LB_INSERTSTRING, m_lstCommands.GetCount(), (LPARAM)pControl);
  402. }
  403. }