KeyConfigDlg.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. /*
  2. * KeyConfigDlg.cpp
  3. * ----------------
  4. * Purpose: Implementation of OpenMPT's keyboard configuration dialog.
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #include "stdafx.h"
  10. #include "KeyConfigDlg.h"
  11. #include "FileDialog.h"
  12. #include "../soundlib/mod_specifications.h"
  13. #include "../soundlib/MIDIEvents.h"
  14. OPENMPT_NAMESPACE_BEGIN
  15. //***************************************************************************************//
  16. // CCustEdit: customised CEdit control to catch keypresses.
  17. // (does what CHotKeyCtrl does,but better)
  18. //***************************************************************************************//
  19. BEGIN_MESSAGE_MAP(CCustEdit, CEdit)
  20. ON_WM_SETFOCUS()
  21. ON_WM_KILLFOCUS()
  22. ON_MESSAGE(WM_MOD_MIDIMSG, &CCustEdit::OnMidiMsg)
  23. END_MESSAGE_MAP()
  24. LRESULT CCustEdit::OnMidiMsg(WPARAM dwMidiDataParam, LPARAM)
  25. {
  26. if(!m_isFocussed)
  27. return 1;
  28. uint32 midiData = static_cast<uint32>(dwMidiDataParam);
  29. const auto byte1 = MIDIEvents::GetDataByte1FromEvent(midiData), byte2 = MIDIEvents::GetDataByte2FromEvent(midiData);
  30. switch(MIDIEvents::GetTypeFromEvent(midiData))
  31. {
  32. case MIDIEvents::evControllerChange:
  33. if(byte2 != 0)
  34. {
  35. SetKey(ModMidi, byte1);
  36. if(!m_isDummy)
  37. m_pOptKeyDlg->OnSetKeyChoice();
  38. }
  39. break;
  40. case MIDIEvents::evNoteOn:
  41. case MIDIEvents::evNoteOff:
  42. SetKey(ModMidi, byte1 | 0x80);
  43. if(!m_isDummy)
  44. m_pOptKeyDlg->OnSetKeyChoice();
  45. break;
  46. }
  47. return 1;
  48. }
  49. BOOL CCustEdit::PreTranslateMessage(MSG *pMsg)
  50. {
  51. if(pMsg)
  52. {
  53. if(pMsg->message == WM_KEYDOWN || pMsg->message == WM_SYSKEYDOWN)
  54. {
  55. SetKey(CMainFrame::GetInputHandler()->GetModifierMask(), static_cast<UINT>(pMsg->wParam));
  56. return -1; // Keypress handled, don't pass on message.
  57. } else if(pMsg->message == WM_KEYUP || pMsg->message == WM_SYSKEYUP)
  58. {
  59. //if a key has been released but custom edit box is empty, we have probably just
  60. //navigated into the box with TAB or SHIFT-TAB. No need to set keychoice.
  61. if(code != 0 && !m_isDummy)
  62. m_pOptKeyDlg->OnSetKeyChoice();
  63. }
  64. }
  65. return CEdit::PreTranslateMessage(pMsg);
  66. }
  67. void CCustEdit::SetKey(FlagSet<Modifiers> inMod, UINT inCode)
  68. {
  69. mod = inMod;
  70. code = inCode;
  71. //Setup display
  72. SetWindowText(KeyCombination::GetKeyText(mod, code));
  73. }
  74. void CCustEdit::OnSetFocus(CWnd *pOldWnd)
  75. {
  76. CEdit::OnSetFocus(pOldWnd);
  77. // Lock the input handler
  78. CMainFrame::GetInputHandler()->Bypass(true);
  79. // Accept MIDI input
  80. CMainFrame::GetMainFrame()->SetMidiRecordWnd(m_hWnd);
  81. m_isFocussed = true;
  82. }
  83. void CCustEdit::OnKillFocus(CWnd *pNewWnd)
  84. {
  85. CEdit::OnKillFocus(pNewWnd);
  86. //unlock the input handler
  87. CMainFrame::GetInputHandler()->Bypass(false);
  88. m_isFocussed = false;
  89. }
  90. //***************************************************************************************//
  91. // COptionsKeyboard:
  92. //
  93. //***************************************************************************************//
  94. // Initialisation
  95. BEGIN_MESSAGE_MAP(COptionsKeyboard, CPropertyPage)
  96. ON_LBN_SELCHANGE(IDC_CHOICECOMBO, &COptionsKeyboard::OnKeyChoiceSelect)
  97. ON_LBN_SELCHANGE(IDC_COMMAND_LIST, &COptionsKeyboard::OnCommandKeySelChanged)
  98. ON_LBN_SELCHANGE(IDC_KEYCATEGORY, &COptionsKeyboard::OnCategorySelChanged)
  99. ON_EN_UPDATE(IDC_CHORDDETECTWAITTIME, &COptionsKeyboard::OnChordWaitTimeChanged) //rewbs.autochord
  100. ON_COMMAND(IDC_DELETE, &COptionsKeyboard::OnDeleteKeyChoice)
  101. ON_COMMAND(IDC_RESTORE, &COptionsKeyboard::OnRestoreKeyChoice)
  102. ON_COMMAND(IDC_LOAD, &COptionsKeyboard::OnLoad)
  103. ON_COMMAND(IDC_SAVE, &COptionsKeyboard::OnSave)
  104. ON_COMMAND(IDC_CHECKKEYDOWN, &COptionsKeyboard::OnCheck)
  105. ON_COMMAND(IDC_CHECKKEYHOLD, &COptionsKeyboard::OnCheck)
  106. ON_COMMAND(IDC_CHECKKEYUP, &COptionsKeyboard::OnCheck)
  107. ON_COMMAND(IDC_NOTESREPEAT, &COptionsKeyboard::OnNotesRepeat)
  108. ON_COMMAND(IDC_NONOTESREPEAT, &COptionsKeyboard::OnNoNotesRepeat)
  109. ON_COMMAND(IDC_CLEARLOG, &COptionsKeyboard::OnClearLog)
  110. ON_COMMAND(IDC_RESTORE_KEYMAP, &COptionsKeyboard::OnRestoreDefaultKeymap)
  111. ON_EN_CHANGE(IDC_FIND, &COptionsKeyboard::OnSearchTermChanged)
  112. ON_EN_CHANGE(IDC_FINDHOTKEY, &COptionsKeyboard::OnFindHotKey)
  113. ON_EN_SETFOCUS(IDC_FINDHOTKEY, &COptionsKeyboard::OnClearHotKey)
  114. ON_WM_DESTROY()
  115. END_MESSAGE_MAP()
  116. void COptionsKeyboard::DoDataExchange(CDataExchange *pDX)
  117. {
  118. CPropertyPage::DoDataExchange(pDX);
  119. DDX_Control(pDX, IDC_KEYCATEGORY, m_cmbCategory);
  120. DDX_Control(pDX, IDC_COMMAND_LIST, m_lbnCommandKeys);
  121. DDX_Control(pDX, IDC_CHOICECOMBO, m_cmbKeyChoice);
  122. DDX_Control(pDX, IDC_CHORDDETECTWAITTIME, m_eChordWaitTime);//rewbs.autochord
  123. DDX_Control(pDX, IDC_KEYREPORT, m_eReport);
  124. DDX_Control(pDX, IDC_CUSTHOTKEY, m_eCustHotKey);
  125. DDX_Control(pDX, IDC_FINDHOTKEY, m_eFindHotKey);
  126. DDX_Control(pDX, IDC_CHECKKEYDOWN, m_bKeyDown);
  127. DDX_Control(pDX, IDC_CHECKKEYHOLD, m_bKeyHold);
  128. DDX_Control(pDX, IDC_CHECKKEYUP, m_bKeyUp);
  129. DDX_Control(pDX, IDC_FIND, m_eFind);
  130. }
  131. BOOL COptionsKeyboard::OnSetActive()
  132. {
  133. CMainFrame::m_nLastOptionsPage = OPTIONS_PAGE_KEYBOARD;
  134. return CPropertyPage::OnSetActive();
  135. }
  136. BOOL COptionsKeyboard::OnInitDialog()
  137. {
  138. CPropertyPage::OnInitDialog();
  139. m_fullPathName = TrackerSettings::Instance().m_szKbdFile;
  140. m_localCmdSet = std::make_unique<CCommandSet>();
  141. m_localCmdSet->Copy(CMainFrame::GetInputHandler()->m_activeCommandSet.get());
  142. //Fill category combo and automatically selects first category
  143. DefineCommandCategories();
  144. for(size_t c = 0; c < commandCategories.size(); c++)
  145. {
  146. if(commandCategories[c].name && !commandCategories[c].commands.empty())
  147. m_cmbCategory.SetItemData(m_cmbCategory.AddString(commandCategories[c].name), c);
  148. }
  149. m_cmbCategory.SetCurSel(0);
  150. UpdateDialog();
  151. m_eCustHotKey.SetParent(m_hWnd, IDC_CUSTHOTKEY, this);
  152. m_eFindHotKey.SetParent(m_hWnd, IDC_FINDHOTKEY, this);
  153. m_eReport.FmtLines(TRUE);
  154. m_eReport.SetWindowText(_T(""));
  155. m_eChordWaitTime.SetWindowText(mpt::cfmt::val(TrackerSettings::Instance().gnAutoChordWaitTime));
  156. return TRUE;
  157. }
  158. void CommandCategory::AddCommands(CommandID first, CommandID last, bool addSeparatorAtEnd)
  159. {
  160. int count = last - first + 1, val = first;
  161. commands.insert(commands.end(), count, kcNull);
  162. std::generate(commands.end() - count, commands.end(), [&val] { return static_cast<CommandID>(val++); });
  163. if(addSeparatorAtEnd)
  164. separators.push_back(last);
  165. }
  166. // Filter commands: We only need user to see a select set off commands
  167. // for each category
  168. void COptionsKeyboard::DefineCommandCategories()
  169. {
  170. {
  171. CommandCategory newCat(_T("Global keys"), kCtxAllContexts);
  172. newCat.AddCommands(kcStartFile, kcEndFile, true);
  173. newCat.AddCommands(kcStartPlayCommands, kcEndPlayCommands, true);
  174. newCat.AddCommands(kcStartEditCommands, kcEndEditCommands, true);
  175. newCat.AddCommands(kcStartView, kcEndView, true);
  176. newCat.AddCommands(kcStartMisc, kcEndMisc, true);
  177. newCat.commands.push_back(kcDummyShortcut);
  178. commandCategories.push_back(newCat);
  179. }
  180. commandCategories.emplace_back(_T(" General [Top]"), kCtxCtrlGeneral);
  181. commandCategories.emplace_back(_T(" General [Bottom]"), kCtxViewGeneral);
  182. commandCategories.emplace_back(_T(" Pattern Editor [Top]"), kCtxCtrlPatterns);
  183. {
  184. CommandCategory newCat(_T(" Pattern Editor - Order List"), kCtxCtrlOrderlist);
  185. newCat.AddCommands(kcStartOrderlistCommands, kcEndOrderlistCommands);
  186. newCat.separators.push_back(kcEndOrderlistNavigation);
  187. newCat.separators.push_back(kcEndOrderlistEdit);
  188. newCat.separators.push_back(kcEndOrderlistNum);
  189. commandCategories.push_back(newCat);
  190. }
  191. {
  192. CommandCategory newCat(_T(" Pattern Editor - Quick Channel Settings"), kCtxChannelSettings);
  193. newCat.AddCommands(kcStartChnSettingsCommands, kcEndChnSettingsCommands);
  194. commandCategories.push_back(newCat);
  195. }
  196. {
  197. CommandCategory newCat(_T(" Pattern Editor - General"), kCtxViewPatterns);
  198. newCat.AddCommands(kcStartPlainNavigate, kcEndPlainNavigate, true);
  199. newCat.AddCommands(kcStartJumpSnap, kcEndJumpSnap, true);
  200. newCat.AddCommands(kcStartHomeEnd, kcEndHomeEnd, true);
  201. newCat.AddCommands(kcPrevPattern, kcNextSequence, true);
  202. newCat.AddCommands(kcStartSelect, kcEndSelect, true);
  203. newCat.AddCommands(kcStartPatternClipboard, kcEndPatternClipboard, true);
  204. newCat.AddCommands(kcClearRow, kcInsertWholeRowGlobal, true);
  205. newCat.AddCommands(kcStartChannelKeys, kcEndChannelKeys, true);
  206. newCat.AddCommands(kcBeginTranspose, kcEndTranspose, true);
  207. newCat.AddCommands(kcPatternAmplify, kcPatternShrinkSelection, true);
  208. newCat.AddCommands(kcStartPatternEditMisc, kcEndPatternEditMisc, true);
  209. commandCategories.push_back(newCat);
  210. }
  211. {
  212. CommandCategory newCat(_T(" Pattern Editor - Note Column"), kCtxViewPatternsNote);
  213. newCat.AddCommands(kcVPStartNotes, kcVPEndNotes, true);
  214. newCat.AddCommands(kcSetOctave0, kcSetOctave9, true);
  215. newCat.AddCommands(kcStartNoteMisc, kcEndNoteMisc);
  216. commandCategories.push_back(newCat);
  217. }
  218. {
  219. CommandCategory newCat(_T(" Pattern Editor - Instrument Column"), kCtxViewPatternsIns);
  220. newCat.AddCommands(kcSetIns0, kcSetIns9);
  221. commandCategories.push_back(newCat);
  222. }
  223. {
  224. CommandCategory newCat(_T(" Pattern Editor - Volume Column"), kCtxViewPatternsVol);
  225. newCat.AddCommands(kcSetVolumeStart, kcSetVolumeEnd);
  226. commandCategories.push_back(newCat);
  227. }
  228. {
  229. CommandCategory newCat(_T(" Pattern Editor - Effect Column"), kCtxViewPatternsFX);
  230. newCat.AddCommands(kcSetFXStart, kcSetFXEnd);
  231. commandCategories.push_back(newCat);
  232. }
  233. {
  234. CommandCategory newCat(_T(" Pattern Editor - Effect Parameter Column"), kCtxViewPatternsFXparam);
  235. newCat.AddCommands(kcSetFXParam0, kcSetFXParamF);
  236. commandCategories.push_back(newCat);
  237. }
  238. {
  239. CommandCategory newCat(_T(" Sample [Top]"), kCtxCtrlSamples);
  240. commandCategories.push_back(newCat);
  241. }
  242. {
  243. CommandCategory newCat(_T(" Sample Editor"), kCtxViewSamples);
  244. newCat.AddCommands(kcStartSampleEditing, kcEndSampleEditing, true);
  245. newCat.AddCommands(kcStartSampleMisc, kcEndSampleMisc, true);
  246. newCat.AddCommands(kcStartSampleCues, kcEndSampleCueGroup);
  247. commandCategories.push_back(newCat);
  248. }
  249. {
  250. CommandCategory newCat(_T(" Instrument Editor"), kCtxCtrlInstruments);
  251. newCat.AddCommands(kcStartInstrumentCtrlMisc, kcEndInstrumentCtrlMisc);
  252. commandCategories.push_back(newCat);
  253. }
  254. {
  255. CommandCategory newCat(_T(" Envelope Editor"), kCtxViewInstruments);
  256. newCat.AddCommands(kcStartInstrumentMisc, kcEndInstrumentMisc);
  257. commandCategories.push_back(newCat);
  258. }
  259. commandCategories.emplace_back(_T(" Comments [Top]"), kCtxCtrlComments);
  260. {
  261. CommandCategory newCat(_T(" Comments [Bottom]"), kCtxViewComments);
  262. newCat.AddCommands(kcStartCommentsCommands, kcEndCommentsCommands);
  263. commandCategories.push_back(newCat);
  264. }
  265. {
  266. CommandCategory newCat(_T(" Plugin Editor"), kCtxVSTGUI);
  267. newCat.AddCommands(kcStartVSTGUICommands, kcEndVSTGUICommands);
  268. commandCategories.push_back(newCat);
  269. }
  270. }
  271. // Pure GUI methods
  272. void COptionsKeyboard::UpdateDialog()
  273. {
  274. OnCategorySelChanged(); // Fills command list and automatically selects first command.
  275. OnCommandKeySelChanged(); // Fills command key choice list for that command and automatically selects first choice.
  276. }
  277. void COptionsKeyboard::OnKeyboardChanged()
  278. {
  279. OnSettingsChanged();
  280. UpdateDialog();
  281. }
  282. void COptionsKeyboard::OnCategorySelChanged()
  283. {
  284. int cat = static_cast<int>(m_cmbCategory.GetItemData(m_cmbCategory.GetCurSel()));
  285. if(cat >= 0 && cat != m_curCategory)
  286. {
  287. // Changed category
  288. UpdateShortcutList(cat);
  289. }
  290. }
  291. // Force last active category to be selected in dropdown menu.
  292. void COptionsKeyboard::UpdateCategory()
  293. {
  294. for(int i = 0; i < m_cmbCategory.GetCount(); i++)
  295. {
  296. if((int)m_cmbCategory.GetItemData(i) == m_curCategory)
  297. {
  298. m_cmbCategory.SetCurSel(i);
  299. break;
  300. }
  301. }
  302. }
  303. void COptionsKeyboard::OnSearchTermChanged()
  304. {
  305. CString findString;
  306. m_eFind.GetWindowText(findString);
  307. if(findString.IsEmpty())
  308. {
  309. UpdateCategory();
  310. }
  311. UpdateShortcutList(findString.IsEmpty() ? m_curCategory : -1);
  312. }
  313. void COptionsKeyboard::OnFindHotKey()
  314. {
  315. if(m_eFindHotKey.code == 0)
  316. {
  317. UpdateCategory();
  318. }
  319. UpdateShortcutList(m_eFindHotKey.code == 0 ? m_curCategory : -1);
  320. }
  321. void COptionsKeyboard::OnClearHotKey()
  322. {
  323. // Focus key search: Clear input
  324. m_eFindHotKey.SetKey(ModNone, 0);
  325. }
  326. // Fills command list and automatically selects first command.
  327. void COptionsKeyboard::UpdateShortcutList(int category)
  328. {
  329. CString findString;
  330. m_eFind.GetWindowText(findString);
  331. findString.MakeLower();
  332. const bool searchByName = !findString.IsEmpty(), searchByKey = (m_eFindHotKey.code != 0);
  333. const bool doSearch = (searchByName || searchByKey);
  334. int firstCat = category, lastCat = category;
  335. if(category == -1)
  336. {
  337. // We will search in all categories
  338. firstCat = 0;
  339. lastCat = static_cast<int>(commandCategories.size()) - 1;
  340. }
  341. CommandID curCommand = static_cast<CommandID>(m_lbnCommandKeys.GetItemData(m_lbnCommandKeys.GetCurSel()));
  342. m_lbnCommandKeys.ResetContent();
  343. for(int cat = firstCat; cat <= lastCat; cat++)
  344. {
  345. // When searching, we also add the category names to the list.
  346. bool addCategoryName = (firstCat != lastCat);
  347. for(size_t cmd = 0; cmd < commandCategories[cat].commands.size(); cmd++)
  348. {
  349. CommandID com = (CommandID)commandCategories[cat].commands[cmd];
  350. CString cmdText = m_localCmdSet->GetCommandText(com);
  351. bool addKey = true;
  352. if(searchByKey)
  353. {
  354. addKey = false;
  355. int numChoices = m_localCmdSet->GetKeyListSize(com);
  356. for(int choice = 0; choice < numChoices; choice++)
  357. {
  358. const KeyCombination &kc = m_localCmdSet->GetKey(com, choice);
  359. if(kc.KeyCode() == m_eFindHotKey.code && kc.Modifier() == m_eFindHotKey.mod)
  360. {
  361. addKey = true;
  362. break;
  363. }
  364. }
  365. }
  366. if(searchByName && addKey)
  367. {
  368. addKey = (cmdText.MakeLower().Find(findString) >= 0);
  369. }
  370. if(addKey)
  371. {
  372. m_curCategory = cat;
  373. if(!m_localCmdSet->isHidden(com))
  374. {
  375. if(doSearch && addCategoryName)
  376. {
  377. const CString catName = _T("------ ") + commandCategories[cat].name.Trim() + _T(" ------");
  378. m_lbnCommandKeys.SetItemData(m_lbnCommandKeys.AddString(catName), DWORD_PTR(-1));
  379. addCategoryName = false;
  380. }
  381. int item = m_lbnCommandKeys.AddString(m_localCmdSet->GetCommandText(com));
  382. m_lbnCommandKeys.SetItemData(item, com);
  383. if(curCommand == com)
  384. {
  385. // Keep selection on previously selected string
  386. m_lbnCommandKeys.SetCurSel(item);
  387. }
  388. }
  389. if(commandCategories[cat].SeparatorAt(com))
  390. m_lbnCommandKeys.SetItemData(m_lbnCommandKeys.AddString(_T("------------------------------------------------------")), DWORD_PTR(-1));
  391. }
  392. }
  393. }
  394. if(m_lbnCommandKeys.GetCurSel() == -1)
  395. {
  396. m_lbnCommandKeys.SetCurSel(0);
  397. }
  398. OnCommandKeySelChanged();
  399. }
  400. // Fills key choice list and automatically selects first key choice
  401. void COptionsKeyboard::OnCommandKeySelChanged()
  402. {
  403. const CommandID cmd = static_cast<CommandID>(m_lbnCommandKeys.GetItemData(m_lbnCommandKeys.GetCurSel()));
  404. CString str;
  405. //Separator
  406. if(cmd == kcNull)
  407. {
  408. m_cmbKeyChoice.SetWindowText(_T(""));
  409. m_cmbKeyChoice.EnableWindow(FALSE);
  410. m_eCustHotKey.SetWindowText(_T(""));
  411. m_eCustHotKey.EnableWindow(FALSE);
  412. m_bKeyDown.SetCheck(0);
  413. m_bKeyDown.EnableWindow(FALSE);
  414. m_bKeyHold.SetCheck(0);
  415. m_bKeyHold.EnableWindow(FALSE);
  416. m_bKeyUp.SetCheck(0);
  417. m_bKeyUp.EnableWindow(FALSE);
  418. m_curCommand = kcNull;
  419. }
  420. //Fill "choice" list
  421. else if((cmd >= 0) && (cmd != m_curCommand) || m_forceUpdate) // Have we changed command?
  422. {
  423. m_forceUpdate = false;
  424. m_cmbKeyChoice.EnableWindow(TRUE);
  425. m_eCustHotKey.EnableWindow(TRUE);
  426. m_bKeyDown.EnableWindow(TRUE);
  427. m_bKeyHold.EnableWindow(TRUE);
  428. m_bKeyUp.EnableWindow(TRUE);
  429. m_curCommand = cmd;
  430. m_curCategory = GetCategoryFromCommandID(cmd);
  431. m_cmbKeyChoice.ResetContent();
  432. int numChoices = m_localCmdSet->GetKeyListSize(cmd);
  433. if((cmd < kcNumCommands) && (numChoices > 0))
  434. {
  435. for(int i = 0; i < numChoices; i++)
  436. {
  437. CString s = MPT_CFORMAT("Choice {} (of {})")(i + 1, numChoices);
  438. m_cmbKeyChoice.SetItemData(m_cmbKeyChoice.AddString(s), i);
  439. }
  440. }
  441. m_cmbKeyChoice.SetItemData(m_cmbKeyChoice.AddString(_T("<new>")), numChoices);
  442. m_cmbKeyChoice.SetCurSel(0);
  443. m_curKeyChoice = -1;
  444. OnKeyChoiceSelect();
  445. }
  446. }
  447. //Fills or clears key choice info
  448. void COptionsKeyboard::OnKeyChoiceSelect()
  449. {
  450. int choice = static_cast<int>(m_cmbKeyChoice.GetItemData(m_cmbKeyChoice.GetCurSel()));
  451. CommandID cmd = m_curCommand;
  452. //If nothing there, clear
  453. if(cmd == kcNull || choice >= m_localCmdSet->GetKeyListSize(cmd) || choice < 0)
  454. {
  455. m_curKeyChoice = choice;
  456. m_forceUpdate = true;
  457. m_eCustHotKey.SetKey(ModNone, 0);
  458. m_bKeyDown.SetCheck(0);
  459. m_bKeyHold.SetCheck(0);
  460. m_bKeyUp.SetCheck(0);
  461. return;
  462. }
  463. //else, if changed, Fill
  464. if(choice != m_curKeyChoice || m_forceUpdate)
  465. {
  466. m_curKeyChoice = choice;
  467. m_forceUpdate = false;
  468. KeyCombination kc = m_localCmdSet->GetKey(cmd, choice);
  469. m_eCustHotKey.SetKey(kc.Modifier(), kc.KeyCode());
  470. m_bKeyDown.SetCheck((kc.EventType() & kKeyEventDown) ? BST_CHECKED : BST_UNCHECKED);
  471. m_bKeyHold.SetCheck((kc.EventType() & kKeyEventRepeat) ? BST_CHECKED : BST_UNCHECKED);
  472. m_bKeyUp.SetCheck((kc.EventType() & kKeyEventUp) ? BST_CHECKED : BST_UNCHECKED);
  473. }
  474. }
  475. void COptionsKeyboard::OnChordWaitTimeChanged()
  476. {
  477. CString s;
  478. UINT val;
  479. m_eChordWaitTime.GetWindowText(s);
  480. val = _tstoi(s);
  481. if(val > 5000)
  482. {
  483. val = 5000;
  484. m_eChordWaitTime.SetWindowText(_T("5000"));
  485. }
  486. OnSettingsChanged();
  487. }
  488. // Change handling
  489. void COptionsKeyboard::OnRestoreKeyChoice()
  490. {
  491. KeyCombination kc;
  492. CommandID cmd = m_curCommand;
  493. CInputHandler *ih = CMainFrame::GetInputHandler();
  494. // Do nothing if there's nothing to restore
  495. if(cmd == kcNull || m_curKeyChoice < 0 || m_curKeyChoice >= ih->GetKeyListSize(cmd))
  496. {
  497. ::MessageBeep(MB_ICONWARNING);
  498. return;
  499. }
  500. // Restore current key combination choice for currently selected command.
  501. kc = ih->m_activeCommandSet->GetKey(cmd, m_curKeyChoice);
  502. m_localCmdSet->Remove(m_curKeyChoice, cmd);
  503. m_localCmdSet->Add(kc, cmd, true, m_curKeyChoice);
  504. ForceUpdateGUI();
  505. return;
  506. }
  507. void COptionsKeyboard::OnDeleteKeyChoice()
  508. {
  509. CommandID cmd = m_curCommand;
  510. // Do nothing if there's no key defined for this slot.
  511. if(m_curCommand == kcNull || m_curKeyChoice < 0 || m_curKeyChoice >= m_localCmdSet->GetKeyListSize(cmd))
  512. {
  513. ::MessageBeep(MB_ICONWARNING);
  514. return;
  515. }
  516. // Delete current key combination choice for currently selected command.
  517. m_localCmdSet->Remove(m_curKeyChoice, cmd);
  518. ForceUpdateGUI();
  519. return;
  520. }
  521. void COptionsKeyboard::OnSetKeyChoice()
  522. {
  523. CommandID cmd = m_curCommand;
  524. if(cmd == kcNull)
  525. {
  526. Reporting::Warning("Invalid slot.", "Invalid key data", this);
  527. return;
  528. }
  529. FlagSet<KeyEventType> event = kKeyEventNone;
  530. if(m_bKeyDown.GetCheck() != BST_UNCHECKED)
  531. event |= kKeyEventDown;
  532. if(m_bKeyHold.GetCheck() != BST_UNCHECKED)
  533. event |= kKeyEventRepeat;
  534. if(m_bKeyUp.GetCheck() != BST_UNCHECKED)
  535. event |= kKeyEventUp;
  536. KeyCombination kc((commandCategories[m_curCategory]).id, m_eCustHotKey.mod, m_eCustHotKey.code, event);
  537. //detect invalid input
  538. if(!kc.KeyCode())
  539. {
  540. Reporting::Warning("You need to say to which key you'd like to map this command to.", "Invalid key data", this);
  541. return;
  542. }
  543. if(!kc.EventType())
  544. {
  545. ::MessageBeep(MB_ICONWARNING);
  546. kc.EventType(kKeyEventDown);
  547. }
  548. bool add = true;
  549. std::pair<CommandID, KeyCombination> conflictCmd;
  550. if((conflictCmd = m_localCmdSet->IsConflicting(kc, cmd)).first != kcNull
  551. && conflictCmd.first != cmd
  552. && !m_localCmdSet->IsCrossContextConflict(kc, conflictCmd.second))
  553. {
  554. ConfirmAnswer delOld = Reporting::Confirm(_T("New shortcut (") + kc.GetKeyText() + _T(") has the same key combination as ") + m_localCmdSet->GetCommandText(conflictCmd.first) + _T(" in ") + conflictCmd.second.GetContextText() + _T(".\nDo you want to delete the other shortcut, only keeping the new one?"), _T("Shortcut Conflict"), true, false, this);
  555. if(delOld == cnfYes)
  556. {
  557. m_localCmdSet->Remove(conflictCmd.second, conflictCmd.first);
  558. } else if(delOld == cnfCancel)
  559. {
  560. // Cancel altogther; restore original choice
  561. add = false;
  562. if(m_curKeyChoice >= 0 && m_curKeyChoice < m_localCmdSet->GetKeyListSize(cmd))
  563. {
  564. KeyCombination origKc = m_localCmdSet->GetKey(cmd, m_curKeyChoice);
  565. m_eCustHotKey.SetKey(origKc.Modifier(), origKc.KeyCode());
  566. } else
  567. {
  568. m_eCustHotKey.SetWindowText(_T(""));
  569. }
  570. }
  571. }
  572. if(add)
  573. {
  574. CString report, reportHistory;
  575. //process valid input
  576. m_localCmdSet->Remove(m_curKeyChoice, cmd);
  577. report = m_localCmdSet->Add(kc, cmd, true, m_curKeyChoice);
  578. //Update log
  579. m_eReport.GetWindowText(reportHistory);
  580. m_eReport.SetWindowText(report + reportHistory);
  581. ForceUpdateGUI();
  582. }
  583. }
  584. void COptionsKeyboard::OnOK()
  585. {
  586. CMainFrame::GetInputHandler()->SetNewCommandSet(m_localCmdSet.get());
  587. CString cs;
  588. m_eChordWaitTime.GetWindowText(cs);
  589. TrackerSettings::Instance().gnAutoChordWaitTime = _tstoi(cs);
  590. CPropertyPage::OnOK();
  591. }
  592. void COptionsKeyboard::OnDestroy()
  593. {
  594. CPropertyPage::OnDestroy();
  595. m_localCmdSet = nullptr;
  596. }
  597. void COptionsKeyboard::OnLoad()
  598. {
  599. auto dlg = OpenFileDialog()
  600. .DefaultExtension("mkb")
  601. .DefaultFilename(m_fullPathName)
  602. .ExtensionFilter("OpenMPT Key Bindings (*.mkb)|*.mkb||")
  603. .AddPlace(theApp.GetInstallPkgPath() + P_("extraKeymaps"))
  604. .WorkingDirectory(TrackerSettings::Instance().m_szKbdFile);
  605. if(!dlg.Show(this))
  606. return;
  607. m_fullPathName = dlg.GetFirstFile();
  608. m_localCmdSet->LoadFile(m_fullPathName);
  609. ForceUpdateGUI();
  610. }
  611. void COptionsKeyboard::OnSave()
  612. {
  613. auto dlg = SaveFileDialog()
  614. .DefaultExtension("mkb")
  615. .DefaultFilename(m_fullPathName)
  616. .ExtensionFilter("OpenMPT Key Bindings (*.mkb)|*.mkb||")
  617. .WorkingDirectory(TrackerSettings::Instance().m_szKbdFile);
  618. if(!dlg.Show(this))
  619. return;
  620. m_fullPathName = dlg.GetFirstFile();
  621. m_localCmdSet->SaveFile(m_fullPathName);
  622. }
  623. void COptionsKeyboard::OnNotesRepeat()
  624. {
  625. m_localCmdSet->QuickChange_NotesRepeat(true);
  626. ForceUpdateGUI();
  627. }
  628. void COptionsKeyboard::OnNoNotesRepeat()
  629. {
  630. m_localCmdSet->QuickChange_NotesRepeat(false);
  631. ForceUpdateGUI();
  632. }
  633. void COptionsKeyboard::ForceUpdateGUI()
  634. {
  635. m_forceUpdate = true; // m_nCurKeyChoice and m_nCurHotKey haven't changed, yet we still want to update.
  636. int ntmpChoice = m_curKeyChoice; // next call will overwrite m_nCurKeyChoice
  637. OnCommandKeySelChanged(); // update keychoice list
  638. m_cmbKeyChoice.SetCurSel(ntmpChoice); // select fresh keychoice (thus restoring m_nCurKeyChoice)
  639. OnKeyChoiceSelect(); // update key data
  640. OnSettingsChanged(); // Enable "apply" button
  641. }
  642. void COptionsKeyboard::OnClearLog()
  643. {
  644. m_eReport.SetWindowText(_T(""));
  645. ForceUpdateGUI();
  646. }
  647. void COptionsKeyboard::OnRestoreDefaultKeymap()
  648. {
  649. if(Reporting::Confirm("Discard all custom changes and restore default key configuration?", false, true, this) == cnfYes)
  650. {
  651. m_localCmdSet->LoadDefaultKeymap();
  652. ForceUpdateGUI();
  653. }
  654. }
  655. int COptionsKeyboard::GetCategoryFromCommandID(CommandID command) const
  656. {
  657. for(size_t cat = 0; cat < commandCategories.size(); cat++)
  658. {
  659. const auto &cmds = commandCategories[cat].commands;
  660. if(mpt::contains(cmds, command))
  661. return static_cast<int>(cat);
  662. }
  663. return -1;
  664. }
  665. OPENMPT_NAMESPACE_END