The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

475 lines
16KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class KeyMappingEditorComponent::ChangeKeyButton : public Button
  18. {
  19. public:
  20. ChangeKeyButton (KeyMappingEditorComponent& kec, const CommandID command,
  21. const String& keyName, const int keyIndex)
  22. : Button (keyName),
  23. owner (kec),
  24. commandID (command),
  25. keyNum (keyIndex)
  26. {
  27. setWantsKeyboardFocus (false);
  28. setTriggeredOnMouseDown (keyNum >= 0);
  29. setTooltip (keyIndex < 0 ? TRANS("adds a new key-mapping")
  30. : TRANS("click to change this key-mapping"));
  31. }
  32. void paintButton (Graphics& g, bool /*isOver*/, bool /*isDown*/) override
  33. {
  34. getLookAndFeel().drawKeymapChangeButton (g, getWidth(), getHeight(), *this,
  35. keyNum >= 0 ? getName() : String::empty);
  36. }
  37. static void menuCallback (int result, ChangeKeyButton* button)
  38. {
  39. if (button != nullptr)
  40. {
  41. switch (result)
  42. {
  43. case 1: button->assignNewKey(); break;
  44. case 2: button->owner.getMappings().removeKeyPress (button->commandID, button->keyNum); break;
  45. default: break;
  46. }
  47. }
  48. }
  49. void clicked() override
  50. {
  51. if (keyNum >= 0)
  52. {
  53. // existing key clicked..
  54. PopupMenu m;
  55. m.addItem (1, TRANS("change this key-mapping"));
  56. m.addSeparator();
  57. m.addItem (2, TRANS("remove this key-mapping"));
  58. m.showMenuAsync (PopupMenu::Options(),
  59. ModalCallbackFunction::forComponent (menuCallback, this));
  60. }
  61. else
  62. {
  63. assignNewKey(); // + button pressed..
  64. }
  65. }
  66. void fitToContent (const int h) noexcept
  67. {
  68. if (keyNum < 0)
  69. setSize (h, h);
  70. else
  71. setSize (jlimit (h * 4, h * 8, 6 + Font (h * 0.6f).getStringWidth (getName())), h);
  72. }
  73. //==============================================================================
  74. class KeyEntryWindow : public AlertWindow
  75. {
  76. public:
  77. KeyEntryWindow (KeyMappingEditorComponent& kec)
  78. : AlertWindow (TRANS("New key-mapping"),
  79. TRANS("Please press a key combination now..."),
  80. AlertWindow::NoIcon),
  81. owner (kec)
  82. {
  83. addButton (TRANS("OK"), 1);
  84. addButton (TRANS("Cancel"), 0);
  85. // (avoid return + escape keys getting processed by the buttons..)
  86. for (int i = getNumChildComponents(); --i >= 0;)
  87. getChildComponent (i)->setWantsKeyboardFocus (false);
  88. setWantsKeyboardFocus (true);
  89. grabKeyboardFocus();
  90. }
  91. bool keyPressed (const KeyPress& key) override
  92. {
  93. lastPress = key;
  94. String message (TRANS("Key") + ": " + owner.getDescriptionForKeyPress (key));
  95. const CommandID previousCommand = owner.getMappings().findCommandForKeyPress (key);
  96. if (previousCommand != 0)
  97. message << "\n\n("
  98. << TRANS("Currently assigned to \"CMDN\"")
  99. .replace ("CMDN", owner.getCommandManager().getNameOfCommand (previousCommand))
  100. << ')';
  101. setMessage (message);
  102. return true;
  103. }
  104. bool keyStateChanged (bool) override
  105. {
  106. return true;
  107. }
  108. KeyPress lastPress;
  109. private:
  110. KeyMappingEditorComponent& owner;
  111. JUCE_DECLARE_NON_COPYABLE (KeyEntryWindow)
  112. };
  113. static void assignNewKeyCallback (int result, ChangeKeyButton* button, KeyPress newKey)
  114. {
  115. if (result != 0 && button != nullptr)
  116. button->setNewKey (newKey, true);
  117. }
  118. void setNewKey (const KeyPress& newKey, bool dontAskUser)
  119. {
  120. if (newKey.isValid())
  121. {
  122. const CommandID previousCommand = owner.getMappings().findCommandForKeyPress (newKey);
  123. if (previousCommand == 0 || dontAskUser)
  124. {
  125. owner.getMappings().removeKeyPress (newKey);
  126. if (keyNum >= 0)
  127. owner.getMappings().removeKeyPress (commandID, keyNum);
  128. owner.getMappings().addKeyPress (commandID, newKey, keyNum);
  129. }
  130. else
  131. {
  132. AlertWindow::showOkCancelBox (AlertWindow::WarningIcon,
  133. TRANS("Change key-mapping"),
  134. TRANS("This key is already assigned to the command \"CMDN\"")
  135. .replace ("CMDN", owner.getCommandManager().getNameOfCommand (previousCommand))
  136. + "\n\n"
  137. + TRANS("Do you want to re-assign it to this new command instead?"),
  138. TRANS("Re-assign"),
  139. TRANS("Cancel"),
  140. this,
  141. ModalCallbackFunction::forComponent (assignNewKeyCallback,
  142. this, KeyPress (newKey)));
  143. }
  144. }
  145. }
  146. static void keyChosen (int result, ChangeKeyButton* button)
  147. {
  148. if (result != 0 && button != nullptr && button->currentKeyEntryWindow != nullptr)
  149. {
  150. button->currentKeyEntryWindow->setVisible (false);
  151. button->setNewKey (button->currentKeyEntryWindow->lastPress, false);
  152. }
  153. button->currentKeyEntryWindow = nullptr;
  154. }
  155. void assignNewKey()
  156. {
  157. currentKeyEntryWindow = new KeyEntryWindow (owner);
  158. currentKeyEntryWindow->enterModalState (true, ModalCallbackFunction::forComponent (keyChosen, this));
  159. }
  160. private:
  161. KeyMappingEditorComponent& owner;
  162. const CommandID commandID;
  163. const int keyNum;
  164. ScopedPointer<KeyEntryWindow> currentKeyEntryWindow;
  165. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChangeKeyButton)
  166. };
  167. //==============================================================================
  168. class KeyMappingEditorComponent::ItemComponent : public Component
  169. {
  170. public:
  171. ItemComponent (KeyMappingEditorComponent& kec, const CommandID command)
  172. : owner (kec), commandID (command)
  173. {
  174. setInterceptsMouseClicks (false, true);
  175. const bool isReadOnly = owner.isCommandReadOnly (commandID);
  176. const Array <KeyPress> keyPresses (owner.getMappings().getKeyPressesAssignedToCommand (commandID));
  177. for (int i = 0; i < jmin ((int) maxNumAssignments, keyPresses.size()); ++i)
  178. addKeyPressButton (owner.getDescriptionForKeyPress (keyPresses.getReference (i)), i, isReadOnly);
  179. addKeyPressButton (String::empty, -1, isReadOnly);
  180. }
  181. void addKeyPressButton (const String& desc, const int index, const bool isReadOnly)
  182. {
  183. ChangeKeyButton* const b = new ChangeKeyButton (owner, commandID, desc, index);
  184. keyChangeButtons.add (b);
  185. b->setEnabled (! isReadOnly);
  186. b->setVisible (keyChangeButtons.size() <= (int) maxNumAssignments);
  187. addChildComponent (b);
  188. }
  189. void paint (Graphics& g) override
  190. {
  191. g.setFont (getHeight() * 0.7f);
  192. g.setColour (findColour (KeyMappingEditorComponent::textColourId));
  193. g.drawFittedText (TRANS (owner.getCommandManager().getNameOfCommand (commandID)),
  194. 4, 0, jmax (40, getChildComponent (0)->getX() - 5), getHeight(),
  195. Justification::centredLeft, true);
  196. }
  197. void resized() override
  198. {
  199. int x = getWidth() - 4;
  200. for (int i = keyChangeButtons.size(); --i >= 0;)
  201. {
  202. ChangeKeyButton* const b = keyChangeButtons.getUnchecked(i);
  203. b->fitToContent (getHeight() - 2);
  204. b->setTopRightPosition (x, 1);
  205. x = b->getX() - 5;
  206. }
  207. }
  208. private:
  209. KeyMappingEditorComponent& owner;
  210. OwnedArray<ChangeKeyButton> keyChangeButtons;
  211. const CommandID commandID;
  212. enum { maxNumAssignments = 3 };
  213. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ItemComponent)
  214. };
  215. //==============================================================================
  216. class KeyMappingEditorComponent::MappingItem : public TreeViewItem
  217. {
  218. public:
  219. MappingItem (KeyMappingEditorComponent& kec, const CommandID command)
  220. : owner (kec), commandID (command)
  221. {}
  222. String getUniqueName() const { return String ((int) commandID) + "_id"; }
  223. bool mightContainSubItems() { return false; }
  224. int getItemHeight() const { return 20; }
  225. Component* createItemComponent() { return new ItemComponent (owner, commandID); }
  226. private:
  227. KeyMappingEditorComponent& owner;
  228. const CommandID commandID;
  229. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MappingItem)
  230. };
  231. //==============================================================================
  232. class KeyMappingEditorComponent::CategoryItem : public TreeViewItem
  233. {
  234. public:
  235. CategoryItem (KeyMappingEditorComponent& kec, const String& name)
  236. : owner (kec), categoryName (name)
  237. {}
  238. String getUniqueName() const { return categoryName + "_cat"; }
  239. bool mightContainSubItems() { return true; }
  240. int getItemHeight() const { return 28; }
  241. void paintItem (Graphics& g, int width, int height) override
  242. {
  243. g.setFont (Font (height * 0.6f, Font::bold));
  244. g.setColour (owner.findColour (KeyMappingEditorComponent::textColourId));
  245. g.drawText (categoryName,
  246. 2, 0, width - 2, height,
  247. Justification::centredLeft, true);
  248. }
  249. void itemOpennessChanged (bool isNowOpen) override
  250. {
  251. if (isNowOpen)
  252. {
  253. if (getNumSubItems() == 0)
  254. {
  255. const Array <CommandID> commands (owner.getCommandManager().getCommandsInCategory (categoryName));
  256. for (int i = 0; i < commands.size(); ++i)
  257. if (owner.shouldCommandBeIncluded (commands[i]))
  258. addSubItem (new MappingItem (owner, commands[i]));
  259. }
  260. }
  261. else
  262. {
  263. clearSubItems();
  264. }
  265. }
  266. private:
  267. KeyMappingEditorComponent& owner;
  268. String categoryName;
  269. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CategoryItem)
  270. };
  271. //==============================================================================
  272. class KeyMappingEditorComponent::TopLevelItem : public TreeViewItem,
  273. public ButtonListener,
  274. private ChangeListener
  275. {
  276. public:
  277. TopLevelItem (KeyMappingEditorComponent& kec)
  278. : owner (kec)
  279. {
  280. setLinesDrawnForSubItems (false);
  281. owner.getMappings().addChangeListener (this);
  282. }
  283. ~TopLevelItem()
  284. {
  285. owner.getMappings().removeChangeListener (this);
  286. }
  287. bool mightContainSubItems() { return true; }
  288. String getUniqueName() const { return "keys"; }
  289. void changeListenerCallback (ChangeBroadcaster*) override
  290. {
  291. const OpennessRestorer opennessRestorer (*this);
  292. clearSubItems();
  293. const StringArray categories (owner.getCommandManager().getCommandCategories());
  294. for (int i = 0; i < categories.size(); ++i)
  295. {
  296. const Array <CommandID> commands (owner.getCommandManager().getCommandsInCategory (categories[i]));
  297. int count = 0;
  298. for (int j = 0; j < commands.size(); ++j)
  299. if (owner.shouldCommandBeIncluded (commands[j]))
  300. ++count;
  301. if (count > 0)
  302. addSubItem (new CategoryItem (owner, categories[i]));
  303. }
  304. }
  305. static void resetToDefaultsCallback (int result, KeyMappingEditorComponent* owner)
  306. {
  307. if (result != 0 && owner != nullptr)
  308. owner->getMappings().resetToDefaultMappings();
  309. }
  310. void buttonClicked (Button*) override
  311. {
  312. AlertWindow::showOkCancelBox (AlertWindow::QuestionIcon,
  313. TRANS("Reset to defaults"),
  314. TRANS("Are you sure you want to reset all the key-mappings to their default state?"),
  315. TRANS("Reset"),
  316. String::empty,
  317. &owner,
  318. ModalCallbackFunction::forComponent (resetToDefaultsCallback, &owner));
  319. }
  320. private:
  321. KeyMappingEditorComponent& owner;
  322. };
  323. //==============================================================================
  324. KeyMappingEditorComponent::KeyMappingEditorComponent (KeyPressMappingSet& mappingManager,
  325. const bool showResetToDefaultButton)
  326. : mappings (mappingManager),
  327. resetButton (TRANS ("reset to defaults"))
  328. {
  329. treeItem = new TopLevelItem (*this);
  330. if (showResetToDefaultButton)
  331. {
  332. addAndMakeVisible (&resetButton);
  333. resetButton.addListener (treeItem);
  334. }
  335. addAndMakeVisible (&tree);
  336. tree.setColour (TreeView::backgroundColourId, findColour (backgroundColourId));
  337. tree.setRootItemVisible (false);
  338. tree.setDefaultOpenness (true);
  339. tree.setRootItem (treeItem);
  340. }
  341. KeyMappingEditorComponent::~KeyMappingEditorComponent()
  342. {
  343. tree.setRootItem (nullptr);
  344. }
  345. //==============================================================================
  346. void KeyMappingEditorComponent::setColours (Colour mainBackground,
  347. Colour textColour)
  348. {
  349. setColour (backgroundColourId, mainBackground);
  350. setColour (textColourId, textColour);
  351. tree.setColour (TreeView::backgroundColourId, mainBackground);
  352. }
  353. void KeyMappingEditorComponent::parentHierarchyChanged()
  354. {
  355. treeItem->changeListenerCallback (nullptr);
  356. }
  357. void KeyMappingEditorComponent::resized()
  358. {
  359. int h = getHeight();
  360. if (resetButton.isVisible())
  361. {
  362. const int buttonHeight = 20;
  363. h -= buttonHeight + 8;
  364. int x = getWidth() - 8;
  365. resetButton.changeWidthToFitText (buttonHeight);
  366. resetButton.setTopRightPosition (x, h + 6);
  367. }
  368. tree.setBounds (0, 0, getWidth(), h);
  369. }
  370. //==============================================================================
  371. bool KeyMappingEditorComponent::shouldCommandBeIncluded (const CommandID commandID)
  372. {
  373. const ApplicationCommandInfo* const ci = mappings.getCommandManager().getCommandForID (commandID);
  374. return ci != nullptr && (ci->flags & ApplicationCommandInfo::hiddenFromKeyEditor) == 0;
  375. }
  376. bool KeyMappingEditorComponent::isCommandReadOnly (const CommandID commandID)
  377. {
  378. const ApplicationCommandInfo* const ci = mappings.getCommandManager().getCommandForID (commandID);
  379. return ci != nullptr && (ci->flags & ApplicationCommandInfo::readOnlyInKeyEditor) != 0;
  380. }
  381. String KeyMappingEditorComponent::getDescriptionForKeyPress (const KeyPress& key)
  382. {
  383. return key.getTextDescription();
  384. }