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.

474 lines
16KB

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