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

对话框与窗口

开发平台:

Visual C++

  1. // PaneAccelerators.cpp : implementation file
  2. //
  3. // This file is a part of the XTREME TOOLKIT PRO 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 "commandbarsdesigner.h"
  22. #include "PaneAccelerators.h"
  23. #include "DialogAccelProperties.h"
  24. #include "MainFrm.h"
  25. #ifdef _DEBUG
  26. #define new DEBUG_NEW
  27. #undef THIS_FILE
  28. static char THIS_FILE[] = __FILE__;
  29. #endif
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CPaneAccelerators
  32. LRESULT CPaneAccelerators::CAcceleratorsList::WindowProc(UINT message,WPARAM wParam,LPARAM lParam)
  33. {
  34. switch (message)
  35. {
  36. case WM_NCPAINT:
  37. {
  38. CListCtrl::WindowProc(message, wParam, lParam);
  39. CRect rc;
  40. GetWindowRect(&rc);
  41. CWindowDC dc(this);
  42. rc.OffsetRect(-rc.TopLeft());
  43. COLORREF clr = GetStaticFrameColor();
  44. dc.Draw3dRect(rc, clr, clr);
  45. return TRUE;
  46. }
  47. }
  48. return CListCtrl::WindowProc(message, wParam, lParam);
  49. }
  50. CPaneAccelerators::CPaneAccelerators()
  51. {
  52. m_pActiveFrame = 0;
  53. m_pAccelTable = NULL;
  54. }
  55. CPaneAccelerators::~CPaneAccelerators()
  56. {
  57. }
  58. BEGIN_MESSAGE_MAP(CPaneAccelerators, CWnd)
  59. //{{AFX_MSG_MAP(CPaneAccelerators)
  60. ON_WM_CREATE()
  61. ON_WM_SIZE()
  62. ON_COMMAND(ID_PANEACELLERATORS_NEW, OnPaneacelleratorsNew)
  63. ON_UPDATE_COMMAND_UI(ID_PANEACELLERATORS_NEW, OnUpdatePaneacelleratorsNew)
  64. ON_COMMAND(ID_PANEACELLERATORS_DELETE, OnPaneacelleratorsDelete)
  65. ON_UPDATE_COMMAND_UI(ID_PANEACELLERATORS_DELETE, OnUpdatePaneacelleratorsDelete)
  66. //}}AFX_MSG_MAP
  67. ON_NOTIFY(NM_DBLCLK, AFX_IDW_PANE_FIRST, OnDblClick)
  68. END_MESSAGE_MAP()
  69. /////////////////////////////////////////////////////////////////////////////
  70. // CPaneAccelerators message handlers
  71. int CPaneAccelerators::OnCreate(LPCREATESTRUCT lpCreateStruct)
  72. {
  73. if (CWnd::OnCreate(lpCreateStruct) == -1)
  74. return -1;
  75. VERIFY(m_wndToolBar.CreateToolBar(WS_VISIBLE|WS_CHILD|CBRS_TOOLTIPS, this));
  76. VERIFY(m_wndToolBar.LoadToolBar(IDR_PANE_ACCELERATORS));
  77. m_wndToolBar.GetControl(0)->SetStyle(xtpButtonIconAndCaption);
  78. m_wndToolBar.GetControl(1)->SetStyle(xtpButtonIconAndCaption);
  79. if (!m_wndListCtrl.CWnd::CreateEx(WS_EX_STATICEDGE, WC_LISTVIEW, NULL,
  80. WS_CHILD|WS_VISIBLE|LVS_REPORT|WS_VSCROLL|LVS_SINGLESEL|LVS_SHOWSELALWAYS,
  81. CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST))
  82. {
  83. TRACE0("Failed to create tree control.n");
  84. return -1;
  85. }
  86. m_wndListCtrl.ModifyStyleEx(0, WS_EX_STATICEDGE);
  87. m_wndListCtrl.InsertColumn(0, _T("ID"), LVCFMT_LEFT, 105);
  88. m_wndListCtrl.InsertColumn(1, _T("Key"), LVCFMT_LEFT, 84);
  89. ListView_SetExtendedListViewStyleEx(m_wndListCtrl.m_hWnd,
  90. LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);
  91. return 0;
  92. }
  93. void CPaneAccelerators::OnSize(UINT nType, int cx, int cy)
  94. {
  95. CWnd::OnSize(nType, cx, cy);
  96. int nTop = 0;
  97. if (m_wndToolBar.GetSafeHwnd())
  98. {
  99. CSize sz = m_wndToolBar.CalcDockingLayout(cx, /*LM_HIDEWRAP|*/ LM_HORZDOCK|LM_HORZ | LM_COMMIT);
  100. m_wndToolBar.MoveWindow(0, nTop, cx, sz.cy);
  101. m_wndToolBar.Invalidate(FALSE);
  102. nTop += sz.cy;
  103. }
  104. if (m_wndListCtrl.GetSafeHwnd())
  105. {
  106. m_wndListCtrl.MoveWindow(0, nTop, cx, cy - nTop);
  107. }
  108. }
  109. void CPaneAccelerators::RefreshAccels(CXTPShortcutManagerAccelTable* pAccelTable)
  110. {
  111. CResourceManager* pResourceManager = ((CEmbeddedFrame*)m_pActiveFrame)->ResourceManager();
  112. m_pAccelTable = pAccelTable;
  113. m_wndListCtrl.DeleteAllItems();
  114. if (!pAccelTable)
  115. return;
  116. int nAccelSize = m_pAccelTable->GetCount();
  117. CString strFirst = _T("");
  118. for (int i = 0; i < nAccelSize; i ++)
  119. {
  120. CXTPShortcutManagerAccel* accel = m_pAccelTable->GetAt(i);
  121. CString strID = pResourceManager->GetStringID(accel->cmd);
  122. ASSERT(!strID.IsEmpty());
  123. CString strKey = GetMainFrame()->GetCommandBars()->GetShortcutManager()->Format(accel, NULL);
  124. int nIndex = m_wndListCtrl.InsertItem(m_wndListCtrl.GetItemCount(), strID, 0);
  125. m_wndListCtrl.SetItemText(nIndex, 1, strKey);
  126. m_wndListCtrl.SetItemData(nIndex, i);
  127. }
  128. }
  129. void CPaneAccelerators::Refresh(BOOL bForceAll /*= FALSE*/)
  130. {
  131. if (bForceAll || (m_pActiveFrame != GetMainFrame()->GetActiveEmbeddedFrame()))
  132. {
  133. m_pActiveFrame = GetMainFrame()->GetActiveEmbeddedFrame();
  134. if (m_pActiveFrame)
  135. {
  136. CXTPShortcutManagerAccelTable* pAccel = GetActiveAccelTable();
  137. RefreshAccels(pAccel);
  138. }
  139. else
  140. {
  141. m_wndListCtrl.DeleteAllItems();
  142. m_pAccelTable = NULL;
  143. }
  144. }
  145. }
  146. BOOL CPaneAccelerators::PreTranslateMessage(MSG* pMsg)
  147. {
  148. if (pMsg->message == WM_KEYDOWN || pMsg->message == WM_CHAR || pMsg->message == WM_KEYUP)
  149. {
  150. if (pMsg->wParam == VK_INSERT) Insert();
  151. if (pMsg->wParam == VK_RETURN)
  152. {
  153. int nItem = m_wndListCtrl.GetNextItem(-1, LVNI_SELECTED);
  154. if (nItem != -1)
  155. {
  156. int nIndex = (int)m_wndListCtrl.GetItemData(nItem);
  157. Edit(nIndex);
  158. }
  159. }
  160. if (pMsg->wParam == VK_DELETE)
  161. {
  162. int nItem = m_wndListCtrl.GetNextItem(-1, LVNI_SELECTED);
  163. if (nItem != -1)
  164. {
  165. int nIndex = (int)m_wndListCtrl.GetItemData(nItem);
  166. Delete(nIndex);
  167. }
  168. }
  169. m_wndListCtrl.SendMessage(pMsg->message, pMsg->wParam, pMsg->lParam);
  170. return TRUE;
  171. }
  172. return CWnd::PreTranslateMessage(pMsg);
  173. }
  174. void CPaneAccelerators::Edit(int nIndex)
  175. {
  176. if (!m_pAccelTable)
  177. return;
  178. CResourceManager* pResourceManager = ((CEmbeddedFrame*)m_pActiveFrame)->ResourceManager();
  179. CXTPShortcutManagerAccel* accel = m_pAccelTable->GetAt(nIndex);
  180. CDialogAccelProperties ap;
  181. ap.m_strId = pResourceManager->GetStringID(accel->cmd);
  182. ap.m_lpAccel = accel;
  183. if (ap.DoModal() == IDOK)
  184. {
  185. *accel = *ap.m_wndAccel.GetAccel();
  186. accel->cmd = (WORD)pResourceManager->GetStringID(ap.m_strId);
  187. RefreshAccels(GetActiveAccelTable());
  188. }
  189. }
  190. CXTPShortcutManagerAccelTable* CPaneAccelerators::GetActiveAccelTable()
  191. {
  192. ASSERT(m_pActiveFrame);
  193. return m_pActiveFrame->GetCommandBars()->GetShortcutManager()->GetDefaultAccelerator();
  194. }
  195. void CPaneAccelerators::Delete(int nIndex)
  196. {
  197. if (!m_pAccelTable)
  198. return;
  199. if (nIndex < m_pAccelTable->GetCount())
  200. {
  201. m_pAccelTable->RemoveAt(nIndex);
  202. }
  203. RefreshAccels(GetActiveAccelTable());
  204. }
  205. void CPaneAccelerators::Insert()
  206. {
  207. if (!m_pAccelTable)
  208. return;
  209. CResourceManager* pResourceManager = ((CEmbeddedFrame*)m_pActiveFrame)->ResourceManager();
  210. CDialogAccelProperties ap;
  211. if (ap.DoModal() == IDOK)
  212. {
  213. if (!m_pAccelTable)
  214. return;
  215. CXTPShortcutManagerAccel accel = *ap.m_wndAccel.GetAccel();
  216. accel.cmd = pResourceManager->GetStringID(ap.m_strId);
  217. m_pAccelTable->Add(accel);
  218. RefreshAccels(GetActiveAccelTable());
  219. }
  220. }
  221. void CPaneAccelerators::OnDblClick(NMHDR* pNMHDR, LRESULT* pResult)
  222. {
  223. NMITEMACTIVATE * pNMItemActivate = (NMITEMACTIVATE  *)pNMHDR;
  224. if (!m_pActiveFrame)
  225. return;
  226. if (pNMItemActivate->iItem >= 0)
  227. {
  228. int nIndex = (int)m_wndListCtrl.GetItemData(pNMItemActivate->iItem);
  229. Edit(nIndex);
  230. }
  231. *pResult = 0;
  232. }
  233. void CPaneAccelerators::OnPaneacelleratorsNew()
  234. {
  235. Insert();
  236. }
  237. void CPaneAccelerators::OnUpdatePaneacelleratorsNew(CCmdUI* pCmdUI)
  238. {
  239. pCmdUI->Enable(m_pActiveFrame != NULL);
  240. }
  241. void CPaneAccelerators::OnPaneacelleratorsDelete()
  242. {
  243. int nItem = m_wndListCtrl.GetNextItem(-1, LVNI_SELECTED);
  244. if (nItem != -1)
  245. {
  246. int nIndex = (int)m_wndListCtrl.GetItemData(nItem);
  247. Delete(nIndex);
  248. }
  249. }
  250. void CPaneAccelerators::OnUpdatePaneacelleratorsDelete(CCmdUI* pCmdUI)
  251. {
  252. int nItem = m_wndListCtrl.GetNextItem(-1, LVNI_SELECTED);
  253. pCmdUI->Enable(nItem != -1);
  254. }