Audio plugin host https://kx.studio/carla
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.

466 lines
16KB

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