QuickPasteKeyboard.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. // QuickPasteKeyboard.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "CP_Main.h"
  5. #include "QuickPasteKeyboard.h"
  6. #include "afxdialogex.h"
  7. #include "ActionEnums.h"
  8. #include "HotKeys.h"
  9. // CQuickPasteKeyboard dialog
  10. IMPLEMENT_DYNAMIC(CQuickPasteKeyboard, CPropertyPage)
  11. CQuickPasteKeyboard::CQuickPasteKeyboard()
  12. : CPropertyPage(CQuickPasteKeyboard::IDD)
  13. {
  14. m_csTitle = theApp.m_Language.GetString("QuickPasteKeyboardTitle", "Quick Paste Keyboard");
  15. m_psp.pszTitle = m_csTitle;
  16. m_psp.dwFlags |= PSP_USETITLE;
  17. }
  18. CQuickPasteKeyboard::~CQuickPasteKeyboard()
  19. {
  20. }
  21. void CQuickPasteKeyboard::DoDataExchange(CDataExchange* pDX)
  22. {
  23. CPropertyPage::DoDataExchange(pDX);
  24. DDX_Control(pDX, IDC_LIST1, m_list);
  25. DDX_Control(pDX, IDC_HOTKEY1, m_hotKey1);
  26. DDX_Control(pDX, IDC_HOTKEY2, m_hotKey2);
  27. DDX_Control(pDX, IDC_COMBO_ALL_ASSIGNED, m_assignedCombo);
  28. }
  29. BEGIN_MESSAGE_MAP(CQuickPasteKeyboard, CPropertyPage)
  30. ON_NOTIFY(LVN_ITEMACTIVATE, IDC_LIST1, &CQuickPasteKeyboard::OnLvnItemActivateList1)
  31. ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST1, &CQuickPasteKeyboard::OnLvnItemchangedList1)
  32. ON_BN_CLICKED(IDC_ASSIGN, &CQuickPasteKeyboard::OnBnClickedAssign)
  33. ON_CBN_SELCHANGE(IDC_COMBO_ALL_ASSIGNED, &CQuickPasteKeyboard::OnCbnSelchangeComboAllAssigned)
  34. ON_BN_CLICKED(IDC_BUTTON_REMOVE, &CQuickPasteKeyboard::OnBnClickedButtonRemove)
  35. ON_BN_CLICKED(IDC_BUTTON_ADD, &CQuickPasteKeyboard::OnBnClickedButtonAdd)
  36. ON_CBN_KILLFOCUS(IDC_COMBO_ENTER_MODIFYER, &CQuickPasteKeyboard::OnCbnKillfocusComboEnterModifyer)
  37. ON_EN_KILLFOCUS(IDC_HOTKEY1, &CQuickPasteKeyboard::OnEnKillfocusEdit1)
  38. ON_WM_KILLFOCUS()
  39. END_MESSAGE_MAP()
  40. // CQuickPasteKeyboard message handlers
  41. BOOL CQuickPasteKeyboard::OnInitDialog()
  42. {
  43. CPropertyPage::OnInitDialog();
  44. InitListCtrlCols();
  45. LoadItems();
  46. SetWindowText(_T("Quick Paste Shortcuts"));
  47. return TRUE; // return TRUE unless you set the focus to a control
  48. // EXCEPTION: OCX Property Pages should return FALSE
  49. }
  50. void CQuickPasteKeyboard::InitListCtrlCols()
  51. {
  52. m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT);
  53. m_list.InsertColumn(0, theApp.m_Language.GetGlobalHotKeyString("QPHotKey", "Hot Key"), LVCFMT_LEFT, 150);
  54. m_list.InsertColumn(1, theApp.m_Language.GetGlobalHotKeyString("QPCommand", "Command"), LVCFMT_LEFT, 350);
  55. }
  56. void CQuickPasteKeyboard::LoadItems()
  57. {
  58. m_list.DeleteAllItems();
  59. // Use the LV_ITEM structure to insert the items
  60. LVITEM lvi;
  61. CString strItem;
  62. int row = 0;
  63. for (DWORD i = ActionEnums::FIRST_ACTION + 1; i < ActionEnums::LAST_ACTION; i++)
  64. {
  65. ActionEnums::ActionEnumValues action = (ActionEnums::ActionEnumValues) i;
  66. if (ActionEnums::UserConfigurable(action))
  67. {
  68. // Insert the first item
  69. lvi.mask = LVIF_TEXT;
  70. lvi.iItem = (int) i;
  71. KeyboardArray ar;
  72. for (int x = 0; x < 10; x++)
  73. {
  74. ar.Array[x].A = g_Opt.GetActionShortCutA(action, x);
  75. if (ar.Array[x].A < 0)
  76. break;
  77. ar.Array[x].B = g_Opt.GetActionShortCutB(action, x);
  78. }
  79. CString shortCutText = GetShortCutText(ar);
  80. lvi.iSubItem = 0;
  81. lvi.pszText = (LPTSTR) (LPCTSTR) (shortCutText);
  82. m_list.InsertItem(&lvi);
  83. CString col2 = ActionEnums::EnumDescription(action);
  84. m_list.SetItemText(row, 1, col2);
  85. m_list.SetItemData(row, i);
  86. m_map[i] = ar;
  87. row++;
  88. }
  89. }
  90. }
  91. CString CQuickPasteKeyboard::GetShortCutText(KeyboardAB ab)
  92. {
  93. CString shA;
  94. CString shB;
  95. if (ab.A > 0)
  96. {
  97. shA = CHotKey::GetHotKeyDisplayStatic(ab.A);
  98. if (ab.B > 0)
  99. {
  100. shB = CHotKey::GetHotKeyDisplayStatic(ab.B);
  101. }
  102. }
  103. CString combined;
  104. if (shA.IsEmpty() == FALSE &&
  105. shB.IsEmpty() == FALSE)
  106. {
  107. combined.Format(_T("%s - %s"), shA, shB);
  108. }
  109. else if (shA.IsEmpty() == FALSE)
  110. {
  111. combined.Format(_T("%s"), shA);
  112. }
  113. return combined;
  114. }
  115. CString CQuickPasteKeyboard::GetShortCutText(KeyboardArray ar)
  116. {
  117. CString all;
  118. for (int i = 0; i < 10; i++)
  119. {
  120. CString combined;
  121. if (ar.Array[i].A > 0)
  122. {
  123. combined = GetShortCutText(ar.Array[i]);
  124. }
  125. else
  126. {
  127. break;
  128. }
  129. if(all.GetLength() > 0)
  130. {
  131. all += _T(", ");
  132. }
  133. all += combined;
  134. }
  135. return all;
  136. }
  137. void CQuickPasteKeyboard::OnLvnItemActivateList1(NMHDR *pNMHDR, LRESULT *pResult)
  138. {
  139. LPNMITEMACTIVATE pNMIA = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
  140. // TODO: Add your control notification handler code here
  141. *pResult = 0;
  142. }
  143. void CQuickPasteKeyboard::OnLvnItemchangedList1(NMHDR *pNMHDR, LRESULT *pResult)
  144. {
  145. LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
  146. if ((pNMLV->uChanged & LVIF_STATE)
  147. && (pNMLV->uNewState & LVIS_SELECTED))
  148. {
  149. int id = (int) m_list.GetItemData(pNMLV->iItem);
  150. m_assignedCombo.ResetContent();
  151. for (int i = 0; i < 10; i++)
  152. {
  153. if(m_map[id].Array[i].A > 0)
  154. {
  155. CString shortcut = GetShortCutText(m_map[id].Array[i]);
  156. int pos = m_assignedCombo.AddString(shortcut);
  157. m_assignedCombo.SetItemData(pos, i);
  158. }
  159. else
  160. {
  161. if(i == 0)
  162. {
  163. CString shortcut;
  164. int pos = m_assignedCombo.AddString(shortcut);
  165. m_assignedCombo.SetItemData(pos, i);
  166. }
  167. break;
  168. }
  169. }
  170. m_assignedCombo.SetCurSel(0);
  171. LoadHotKey(m_map[id].Array[0]);
  172. }
  173. *pResult = 0;
  174. }
  175. void CQuickPasteKeyboard::OnBnClickedAssign()
  176. {
  177. int shortCutId = SelectedCommandShortCutId();
  178. int id = SelectedCommandId();
  179. int row = SelectedCommandRow();
  180. if (id > 0 &&
  181. id < m_map.size() &&
  182. shortCutId >= 0 &&
  183. shortCutId < 10)
  184. {
  185. m_map[id].Array[shortCutId].Dirty = true;
  186. m_map[id].Array[shortCutId].A = m_hotKey1.GetHotKey();
  187. m_map[id].Array[shortCutId].B = m_hotKey2.GetHotKey();
  188. CString sh = GetShortCutText(m_map[id]);
  189. LVITEM lvi;
  190. lvi.mask = LVIF_TEXT;
  191. lvi.iItem = (int) row;
  192. lvi.iSubItem = 0;
  193. lvi.pszText = (LPTSTR) (LPCTSTR) (sh);
  194. m_list.SetItem(&lvi);
  195. CString shItem = GetShortCutText(m_map[id].Array[shortCutId]);
  196. int index = m_assignedCombo.GetCurSel();
  197. m_assignedCombo.DeleteString(index);
  198. int pos = m_assignedCombo.InsertString(index, shItem);
  199. m_assignedCombo.SetItemData(pos, shortCutId);
  200. m_assignedCombo.SetCurSel(pos);
  201. }
  202. }
  203. BOOL CQuickPasteKeyboard::OnApply()
  204. {
  205. for (std::map<DWORD, KeyboardArray>::iterator it = m_map.begin(); it != m_map.end(); ++it)
  206. {
  207. for (int i = 0; i < 10; i++)
  208. {
  209. if (it->second.Array[i].Dirty)
  210. {
  211. g_Opt.SetActionShortCutA(it->first, it->second.Array[i].A, i);
  212. g_Opt.SetActionShortCutB(it->first, it->second.Array[i].B, i);
  213. it->second.Array[i].Dirty = false;
  214. }
  215. }
  216. }
  217. return CPropertyPage::OnApply();
  218. }
  219. void CQuickPasteKeyboard::OnCbnSelchangeComboAllAssigned()
  220. {
  221. int shortCutId = SelectedCommandShortCutId();
  222. int id = SelectedCommandId();
  223. if (id > 0 &&
  224. id < m_map.size() &&
  225. shortCutId >= 0 &&
  226. shortCutId < 10)
  227. {
  228. LoadHotKey(m_map[id].Array[shortCutId]);
  229. }
  230. }
  231. void CQuickPasteKeyboard::OnBnClickedButtonRemove()
  232. {
  233. int shortCutId = SelectedCommandShortCutId();
  234. int id = SelectedCommandId();
  235. if (id > 0 &&
  236. id < m_map.size() &&
  237. shortCutId >= 0 &&
  238. shortCutId < 10)
  239. {
  240. int row = SelectedCommandRow();
  241. m_map[id].Array[shortCutId].Dirty = true;
  242. m_map[id].Array[shortCutId].A = 0;
  243. m_map[id].Array[shortCutId].B = 0;
  244. m_assignedCombo.DeleteString(m_assignedCombo.GetCurSel());
  245. if (m_assignedCombo.GetCount() <= 0)
  246. {
  247. CString shortcut;
  248. int pos = m_assignedCombo.AddString(shortcut);
  249. m_assignedCombo.SetItemData(pos, 0);
  250. }
  251. CString sh = GetShortCutText(m_map[id]);
  252. LVITEM lvi;
  253. lvi.mask = LVIF_TEXT;
  254. lvi.iItem = (int) row;
  255. lvi.iSubItem = 0;
  256. lvi.pszText = (LPTSTR) (LPCTSTR) (sh);
  257. m_list.SetItem(&lvi);
  258. m_assignedCombo.SetCurSel(0);
  259. LoadHotKey(m_map[id].Array[0]);
  260. }
  261. }
  262. void CQuickPasteKeyboard::OnBnClickedButtonAdd()
  263. {
  264. int count = m_assignedCombo.GetCount();
  265. if (count < 10)
  266. {
  267. int id = SelectedCommandId();
  268. if (id > 0 && id < m_map.size())
  269. {
  270. CString shortcut;
  271. int pos = m_assignedCombo.AddString(shortcut);
  272. m_assignedCombo.SetItemData(pos, count + 1);
  273. m_assignedCombo.SetCurSel(pos);
  274. LoadHotKey(m_map[id].Array[count + 1]);
  275. m_hotKey1.SetFocus();
  276. }
  277. }
  278. }
  279. int CQuickPasteKeyboard::SelectedCommandId()
  280. {
  281. int id = -1;
  282. POSITION pos = m_list.GetFirstSelectedItemPosition();
  283. if (pos != NULL)
  284. {
  285. while (pos)
  286. {
  287. int row = m_list.GetNextSelectedItem(pos);
  288. id = (int) m_list.GetItemData(row);
  289. }
  290. }
  291. return id;
  292. }
  293. int CQuickPasteKeyboard::SelectedCommandRow()
  294. {
  295. int row = -1;
  296. POSITION pos = m_list.GetFirstSelectedItemPosition();
  297. if (pos != NULL)
  298. {
  299. while (pos)
  300. {
  301. row = m_list.GetNextSelectedItem(pos);
  302. break;
  303. }
  304. }
  305. return row;
  306. }
  307. int CQuickPasteKeyboard::SelectedCommandShortCutId()
  308. {
  309. int index = m_assignedCombo.GetCurSel();
  310. int shortCutId = m_assignedCombo.GetItemData(index);
  311. return shortCutId;
  312. }
  313. void CQuickPasteKeyboard::LoadHotKey(KeyboardAB ab)
  314. {
  315. int a = 0;
  316. if (ab.A > 0)
  317. {
  318. a = ab.A;
  319. }
  320. int b = 0;
  321. if (ab.B > 0)
  322. {
  323. b = ab.B;
  324. }
  325. m_hotKey1.SetHotKey(LOBYTE((DWORD)a), (HIBYTE((DWORD)a)));
  326. m_hotKey2.SetHotKey(LOBYTE((DWORD)b), (HIBYTE((DWORD)b)));
  327. }
  328. void CQuickPasteKeyboard::OnCbnKillfocusComboEnterModifyer()
  329. {
  330. // TODO: Add your control notification handler code here
  331. }
  332. void CQuickPasteKeyboard::OnEnKillfocusEdit1()
  333. {
  334. // TODO: Add your control notification handler code here
  335. }
  336. void CQuickPasteKeyboard::OnKillFocus(CWnd* pNewWnd)
  337. {
  338. CPropertyPage::OnKillFocus(pNewWnd);
  339. // TODO: Add your message handler code here
  340. }
  341. BOOL CQuickPasteKeyboard::PreTranslateMessage(MSG* pMsg)
  342. {
  343. switch(pMsg->message)
  344. {
  345. case WM_KILLFOCUS:
  346. int i = 9;
  347. break;
  348. }
  349. return CPropertyPage::PreTranslateMessage(pMsg);
  350. }
  351. BOOL CQuickPasteKeyboard::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
  352. {
  353. NMHDR* pNMHDR = (NMHDR *) lParam;
  354. if (pNMHDR &&pNMHDR->code == WM_KILLFOCUS)
  355. {
  356. CWnd *pFocus = CWnd::GetFocus(); // call to a static function
  357. if (pFocus && (pFocus->GetParent() == this))
  358. {
  359. if (pFocus->GetDlgCtrlID() != IDCANCEL)
  360. {
  361. // Ok, if the focus is not in the cancelbutton...
  362. if (pNMHDR->idFrom)
  363. {
  364. int k = 0;
  365. }
  366. }
  367. }
  368. }
  369. return CPropertyPage::OnNotify(wParam, lParam, pResult);
  370. }
  371. BOOL CQuickPasteKeyboard::OnCommand(WPARAM wParam, LPARAM lParam)
  372. {
  373. UINT notificationCode = (UINT) HIWORD(wParam);
  374. // For List control I handle it in another way....
  375. if ((notificationCode == EN_KILLFOCUS) ||
  376. (notificationCode == LBN_KILLFOCUS) ||
  377. (notificationCode == CBN_KILLFOCUS) ||
  378. (notificationCode == NM_KILLFOCUS) ||
  379. (notificationCode == WM_KILLFOCUS)) {
  380. CWnd *pFocus = CWnd::GetFocus();
  381. // call to a static function
  382. // If we are changing the focus to another
  383. // control of the same window...
  384. if (pFocus && (pFocus->GetParent() == this))
  385. {
  386. // Ok, if the focus is not in the cancel button...
  387. if (pFocus->GetDlgCtrlID() != IDCANCEL)
  388. {
  389. int k = 9;
  390. }
  391. }
  392. }
  393. return CPropertyPage::OnCommand(wParam, lParam);
  394. }