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

对话框与窗口

开发平台:

Visual C++

  1. // XTPCustomizeKeyboardPage.cpp : implementation of the CXTPCustomizeKeyboardPage 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/XTPIntel80Helpers.h"
  24. #include "XTPShortcutManager.h"
  25. #include "XTPToolBar.h"
  26. #include "XTPPaintManager.h"
  27. #include "XTPControls.h"
  28. #include "XTPControl.h"
  29. #include "XTPControlPopup.h"
  30. #include "XTPCommandBars.h"
  31. #include "XTPCustomizeSheet.h"
  32. #include "XTPCustomizeCommandsPage.h"
  33. #include "XTPCustomizeKeyboardPage.h"
  34. #ifdef _DEBUG
  35. #define new DEBUG_NEW
  36. #undef THIS_FILE
  37. static char THIS_FILE[] = __FILE__;
  38. #endif
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CXTPCustomizeKeyboardPage property page
  41. CXTPCustomizeKeyboardPage::CXTPCustomizeKeyboardPage(CXTPCustomizeSheet* pSheet)
  42. : CPropertyPage(CXTPCustomizeKeyboardPage::IDD)
  43. , m_editShortcutKey(pSheet->GetCommandBars()->GetShortcutManager())
  44. {
  45. //{{AFX_DATA_INIT(CXTPCustomizeKeyboardPage)
  46. // NOTE: the ClassWizard will add member initialization here
  47. //}}AFX_DATA_INIT
  48. m_pSheet = pSheet;
  49. m_strDesc = _T("");
  50. m_iCategory = 0;
  51. ASSERT(pSheet->GetCommandBars()->GetShortcutManager()->GetOriginalAccelerator());
  52. }
  53. CXTPCustomizeKeyboardPage::~CXTPCustomizeKeyboardPage()
  54. {
  55. for (int i = 0; i < m_arrCategories.GetSize(); i++)
  56. {
  57. delete m_arrCategories[i];
  58. }
  59. }
  60. void CXTPCustomizeKeyboardPage::DoDataExchange(CDataExchange* pDX)
  61. {
  62. CPropertyPage::DoDataExchange(pDX);
  63. //{{AFX_DATA_MAP(CXTPCustomizeKeyboardPage)
  64. // NOTE: the ClassWizard will add DDX and DDV calls here
  65. //}}AFX_DATA_MAP
  66. DDX_Control(pDX, XTP_IDC_EDIT_SHORTCUTKEY, m_editShortcutKey);
  67. DDX_Control(pDX, XTP_IDC_LBOX_KEYS, m_lboxKeys);
  68. DDX_Control(pDX, XTP_IDC_LBOX_ACCEL_COMMANDS, m_lboxCommands);
  69. DDX_Control(pDX, XTP_IDC_COMBO_CATEGORY, m_comboCategory);
  70. DDX_Text(pDX, XTP_IDC_TXT_DESC_OVERVIEW, m_strDesc);
  71. DDX_CBIndex(pDX, XTP_IDC_COMBO_CATEGORY, m_iCategory);}
  72. BEGIN_MESSAGE_MAP(CXTPCustomizeKeyboardPage, CPropertyPage)
  73. //{{AFX_MSG_MAP(CXTPCustomizeKeyboardPage)
  74. // NOTE: the ClassWizard will add message map macros here
  75. //}}AFX_MSG_MAP
  76. ON_CBN_SELCHANGE(XTP_IDC_COMBO_CATEGORY, OnSelchangeCategory)
  77. ON_LBN_SELCHANGE(XTP_IDC_LBOX_ACCEL_COMMANDS, OnSelchangeCommands)
  78. ON_LBN_SELCHANGE(XTP_IDC_LBOX_KEYS, OnSelchangeCurKeys)
  79. ON_BN_CLICKED(XTP_IDC_BTN_ASSIGN, OnAssign)
  80. ON_BN_CLICKED(XTP_IDC_BTN_ACCEL_RESET, OnResetAll)
  81. ON_BN_CLICKED(XTP_IDC_BTN_REMOVE, OnRemove)
  82. ON_EN_CHANGE(XTP_IDC_EDIT_SHORTCUTKEY, OnChangeShortcutKey)
  83. END_MESSAGE_MAP()
  84. XTP_COMMANDBARS_CATEGORYINFO* CXTPCustomizeKeyboardPage::FindCategory(LPCTSTR strCategory) const
  85. {
  86. for (int i = 0; i < m_arrCategories.GetSize(); i++)
  87. {
  88. if (m_arrCategories[i]->strCategory.Compare(strCategory) == 0)
  89. return m_arrCategories[i];
  90. }
  91. return NULL;
  92. }
  93. CXTPControls* CXTPCustomizeKeyboardPage::InsertCategory(LPCTSTR strCategory, int nIndex)
  94. {
  95. XTP_COMMANDBARS_CATEGORYINFO* pInfo = FindCategory(strCategory);
  96. if (!pInfo)
  97. {
  98. pInfo = new XTP_COMMANDBARS_CATEGORYINFO(strCategory, m_pSheet->GetCommandBars());
  99. m_arrCategories.InsertAt(nIndex == -1 ? m_arrCategories.GetSize() : nIndex, pInfo);
  100. }
  101. return pInfo->pControls;
  102. }
  103. BOOL CXTPCustomizeKeyboardPage::AddCategory(LPCTSTR strCategory, CMenu* pMenu, BOOL bListSubItems)
  104. {
  105. CXTPControls* pCategoryControls = InsertCategory(strCategory);
  106. int nCount = pMenu->GetMenuItemCount();
  107. for (int i = 0; i < nCount; i++)
  108. {
  109. if (pMenu->GetMenuItemID(i) > 0)
  110. {
  111. CXTPControlPopup* pControl = DYNAMIC_DOWNCAST(CXTPControlPopup,
  112. pCategoryControls->AddMenuItem(pMenu, i));
  113. if (pControl && bListSubItems)
  114. {
  115. CString strSubCategory = pControl->GetCaption() + _T(" | ");
  116. CXTPControls* pControls = pControl->GetCommandBar()->GetControls();
  117. for (int j = 0; j < pControls->GetCount(); j++)
  118. {
  119. CXTPControl* pSubControl = pCategoryControls->AddClone(pControls->GetAt(j));
  120. pSubControl->SetCaption(strSubCategory + pSubControl->GetCaption());
  121. }
  122. }
  123. }
  124. }
  125. return TRUE;
  126. }
  127. BOOL CXTPCustomizeKeyboardPage::AddCategories(UINT nIDResource, BOOL bListSubItems)
  128. {
  129. CMenu menu;
  130. if (!XTPResourceManager()->LoadMenu(&menu, nIDResource))
  131. return FALSE;
  132. int nCount = menu.GetMenuItemCount();
  133. for (int i = 0; i < nCount; i++)
  134. {
  135. CString strCategory;
  136. if (XTPResourceManager()->GetMenuLocaleString(&menu, i, strCategory, MF_BYPOSITION) > 0)
  137. {
  138. CMenu* pMenu = menu.GetSubMenu(i);
  139. if (pMenu)
  140. {
  141. CXTPPaintManager::StripMnemonics(strCategory);
  142. int iPos = strCategory.Find(_T('t'));
  143. if (iPos > 0)
  144. {
  145. strCategory.ReleaseBuffer(iPos);
  146. }
  147. if (!AddCategory(strCategory, pMenu, bListSubItems))
  148. return FALSE;
  149. }
  150. }
  151. }
  152. return TRUE;
  153. }
  154. BOOL CXTPCustomizeKeyboardPage::AddCategories(CXTPControls* pControls)
  155. {
  156. for (int i = 0; i < pControls->GetCount(); i++)
  157. {
  158. CXTPControl* pControl = pControls->GetAt(i);
  159. CString strCategory = pControl->GetCategory();
  160. if (!strCategory.IsEmpty() && pControl->GetID() > 0 &&
  161. pControl->GetType() == xtpControlButton)
  162. {
  163. CXTPControls* pCategoryControls = InsertCategory(strCategory);
  164. pCategoryControls->AddClone(pControl);
  165. }
  166. }
  167. return TRUE;
  168. }
  169. /////////////////////////////////////////////////////////////////////////////
  170. // CXTPCustomizeKeyboardPage message handlers
  171. BOOL CXTPCustomizeKeyboardPage::OnInitDialog()
  172. {
  173. CPropertyPage::OnInitDialog();
  174. for (int i = 0; i < m_arrCategories.GetSize(); i++)
  175. {
  176. int nIndex = m_comboCategory.AddString(m_arrCategories[i]->strCategory);
  177. m_comboCategory.SetItemDataPtr(nIndex, m_arrCategories[i]->pControls);
  178. if (i == 0) m_comboCategory.SetCurSel(0);
  179. }
  180. CXTPCommandBars* pCommandBars = m_pSheet->GetCommandBars();
  181. if (pCommandBars->IsLayoutRTL())
  182. {
  183. m_lboxCommands.ModifyStyleEx(0, WS_EX_RIGHT | WS_EX_LEFTSCROLLBAR | WS_EX_RTLREADING);
  184. m_comboCategory.ModifyStyleEx(0, WS_EX_RIGHT | WS_EX_LEFTSCROLLBAR | WS_EX_RTLREADING);
  185. }
  186. OnSelchangeCategory();
  187. return FALSE;
  188. }
  189. void CXTPCustomizeKeyboardPage::ReloadList()
  190. {
  191. int iIndex = m_comboCategory.GetCurSel();
  192. if (iIndex == LB_ERR)
  193. return;
  194. CXTPControls* pControls = (CXTPControls*)m_comboCategory.GetItemDataPtr(iIndex);
  195. if (pControls == NULL)
  196. return;
  197. m_lboxCommands.ResetContent();
  198. int iItem;
  199. for (iItem = 0; iItem < pControls->GetCount(); ++iItem)
  200. {
  201. CXTPControl* pControl = pControls->GetAt(iItem);
  202. UINT nID = pControl->GetID();
  203. if (nID != 0 && pControl->GetType() != xtpControlPopup
  204. && pControl->GetType() != xtpControlButtonPopup)
  205. {
  206. if ((nID < ID_FILE_MRU_FIRST) || (nID > ID_FILE_MRU_LAST))
  207. {
  208. CString str = pControl->GetCaption();
  209. CXTPPaintManager::StripMnemonics(str);
  210. iIndex = m_lboxCommands.AddString(str);
  211. m_lboxCommands.SetItemDataPtr(iIndex, pControl);
  212. }
  213. }
  214. }
  215. if (m_lboxCommands.GetCount())
  216. m_lboxCommands.SetCurSel(0);
  217. }
  218. void CXTPCustomizeKeyboardPage::OnChangeShortcutKey()
  219. {
  220. EnableControls();
  221. }
  222. void CXTPCustomizeKeyboardPage::OnSelchangeCurKeys()
  223. {
  224. EnableControls();
  225. }
  226. void CXTPCustomizeKeyboardPage::OnSelchangeCategory()
  227. {
  228. UpdateData();
  229. ReloadList();
  230. OnSelchangeCommands();
  231. EnableControls();
  232. }
  233. CXTPShortcutManagerAccelTable* CXTPCustomizeKeyboardPage::GetFrameAccelerator() const
  234. {
  235. return m_pSheet->GetCommandBars()->GetShortcutManager()->GetDefaultAccelerator();
  236. }
  237. void CXTPCustomizeKeyboardPage::OnSelchangeCommands()
  238. {
  239. ASSERT(m_pSheet);
  240. if (!m_pSheet)
  241. return;
  242. m_lboxKeys.ResetContent();
  243. int iIndex = m_lboxCommands.GetCurSel();
  244. if (iIndex == -1)
  245. {
  246. return;
  247. }
  248. CXTPCommandBars* pCommandBars = m_pSheet->GetCommandBars();
  249. CXTPControl* pControl = (CXTPControl*)m_lboxCommands.GetItemDataPtr(iIndex);
  250. ASSERT(pControl);
  251. if (!pControl)
  252. return;
  253. CXTPShortcutManagerAccelTable* pAccelTable = GetFrameAccelerator();
  254. if (pAccelTable)
  255. {
  256. for (int i = 0; i < pAccelTable->GetCount(); i++)
  257. {
  258. CXTPShortcutManagerAccel* accel = pAccelTable->GetAt(i);
  259. if (accel->cmd == pControl->GetID())
  260. {
  261. CString str = pCommandBars->GetShortcutManager()->Format(accel, NULL);
  262. int nIndex = m_lboxKeys.AddString(str);
  263. m_lboxKeys.SetItemData(nIndex, i);
  264. }
  265. }
  266. }
  267. m_strDesc = pControl->GetDescription();
  268. UpdateData(FALSE);
  269. EnableControls();
  270. }
  271. void CXTPCustomizeKeyboardPage::UpdateAcellTable(LPACCEL lpAccel, int nSize)
  272. {
  273. m_pSheet->GetCommandBars()->GetShortcutManager()->UpdateAcellTable(lpAccel, nSize);
  274. }
  275. void CXTPCustomizeKeyboardPage::OnAssign()
  276. {
  277. int iIndex = m_lboxCommands.GetCurSel();
  278. if (iIndex == -1)
  279. return;
  280. CXTPControl* pControl = (CXTPControl*)m_lboxCommands.GetItemDataPtr(iIndex);
  281. if (!pControl)
  282. return;
  283. CXTPShortcutManagerAccel* pAccel = m_editShortcutKey.GetAccel();
  284. ASSERT(pAccel);
  285. if (!pAccel)
  286. return;
  287. pAccel->cmd = (WORD)pControl->GetID();
  288. if (m_pSheet->GetCommandBars()->GetShortcutManager()->OnBeforeAdd(pAccel))
  289. return;
  290. CXTPShortcutManagerAccelTable* pAccelTable = GetFrameAccelerator();
  291. for (int i = 0; i < pAccelTable->GetCount(); i++)
  292. {
  293. CXTPShortcutManagerAccel* accel = pAccelTable->GetAt(i);
  294. if (CXTPShortcutManager::CKeyHelper::EqualAccels(accel, pAccel))
  295. {
  296. if (XTPResourceManager()->ShowMessageBox(XTP_IDS_CONFIRM_REASSIGN, MB_ICONWARNING | MB_YESNO) != IDYES)
  297. {
  298. return;
  299. }
  300. pAccelTable->RemoveAt(i);
  301. break;
  302. }
  303. }
  304. pAccelTable->Add(*pAccel);
  305. OnSelchangeCommands();
  306. m_editShortcutKey.SetWindowText(_T(""));
  307. EnableControls();
  308. }
  309. void CXTPCustomizeKeyboardPage::EnableControls()
  310. {
  311. CString strText;
  312. m_editShortcutKey.GetWindowText(strText);
  313. CWnd* pWnd = GetDlgItem(XTP_IDC_BTN_ASSIGN);
  314. if (pWnd)
  315. {
  316. pWnd->EnableWindow(!strText.IsEmpty() && m_editShortcutKey.IsKeyDefined());
  317. }
  318. int iSel = m_lboxKeys.GetCurSel();
  319. pWnd = GetDlgItem(XTP_IDC_BTN_REMOVE);
  320. if (pWnd)
  321. {
  322. pWnd->EnableWindow(iSel != LB_ERR);
  323. }
  324. }
  325. void CXTPCustomizeKeyboardPage::OnResetAll()
  326. {
  327. CXTPShortcutManager* pShortcutManager = m_pSheet->GetCommandBars()->GetShortcutManager();
  328. CString strPrompt;
  329. VERIFY(XTPResourceManager()->LoadString(&strPrompt, XTP_IDS_CONFIRM_KEYBOARD));
  330. if (m_pSheet->MessageBox(strPrompt, MB_ICONWARNING | MB_YESNO | MB_DEFBUTTON2) == IDYES)
  331. {
  332. pShortcutManager->Reset();
  333. OnSelchangeCommands();
  334. EnableControls();
  335. }
  336. }
  337. void CXTPCustomizeKeyboardPage::OnRemove()
  338. {
  339. int iIndex = m_lboxKeys.GetCurSel();
  340. if (iIndex == LB_ERR)
  341. return;
  342. CXTPShortcutManagerAccelTable* pAccelTable = GetFrameAccelerator();
  343. if (!pAccelTable)
  344. return;
  345. int j = (int)m_lboxKeys.GetItemData(iIndex);
  346. ASSERT(j >= 0 && j < pAccelTable->GetCount());
  347. if (j >= 0 && j < pAccelTable->GetCount())
  348. {
  349. if (m_pSheet->GetCommandBars()->GetShortcutManager()->OnBeforeRemove(pAccelTable->GetAt(j)) == FALSE)
  350. {
  351. pAccelTable->RemoveAt(j);
  352. }
  353. }
  354. OnSelchangeCommands();
  355. EnableControls();
  356. }
  357. BOOL CXTPCustomizeKeyboardPage::OnSetActive()
  358. {
  359. UpdateData(FALSE);
  360. ReloadList();
  361. EnableControls();
  362. return CPropertyPage::OnSetActive();
  363. }