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.

473 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", TRANS (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 (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. 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 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. {
  253. const Array<CommandID> commands (owner.getCommandManager().getCommandsInCategory (categoryName));
  254. for (int i = 0; i < commands.size(); ++i)
  255. if (owner.shouldCommandBeIncluded (commands.getUnchecked(i)))
  256. addSubItem (new MappingItem (owner, commands.getUnchecked(i)));
  257. }
  258. }
  259. else
  260. {
  261. clearSubItems();
  262. }
  263. }
  264. private:
  265. KeyMappingEditorComponent& owner;
  266. String categoryName;
  267. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CategoryItem)
  268. };
  269. //==============================================================================
  270. class KeyMappingEditorComponent::TopLevelItem : public TreeViewItem,
  271. public ButtonListener,
  272. private ChangeListener
  273. {
  274. public:
  275. TopLevelItem (KeyMappingEditorComponent& kec) : owner (kec)
  276. {
  277. setLinesDrawnForSubItems (false);
  278. owner.getMappings().addChangeListener (this);
  279. }
  280. ~TopLevelItem()
  281. {
  282. owner.getMappings().removeChangeListener (this);
  283. }
  284. bool mightContainSubItems() { return true; }
  285. String getUniqueName() const { return "keys"; }
  286. void changeListenerCallback (ChangeBroadcaster*) override
  287. {
  288. const OpennessRestorer opennessRestorer (*this);
  289. clearSubItems();
  290. const StringArray categories (owner.getCommandManager().getCommandCategories());
  291. for (int i = 0; i < categories.size(); ++i)
  292. {
  293. const Array<CommandID> commands (owner.getCommandManager().getCommandsInCategory (categories[i]));
  294. int count = 0;
  295. for (int j = 0; j < commands.size(); ++j)
  296. if (owner.shouldCommandBeIncluded (commands.getUnchecked(j)))
  297. ++count;
  298. if (count > 0)
  299. addSubItem (new CategoryItem (owner, categories[i]));
  300. }
  301. }
  302. static void resetToDefaultsCallback (int result, KeyMappingEditorComponent* owner)
  303. {
  304. if (result != 0 && owner != nullptr)
  305. owner->getMappings().resetToDefaultMappings();
  306. }
  307. void buttonClicked (Button*) override
  308. {
  309. AlertWindow::showOkCancelBox (AlertWindow::QuestionIcon,
  310. TRANS("Reset to defaults"),
  311. TRANS("Are you sure you want to reset all the key-mappings to their default state?"),
  312. TRANS("Reset"),
  313. String::empty,
  314. &owner,
  315. ModalCallbackFunction::forComponent (resetToDefaultsCallback, &owner));
  316. }
  317. private:
  318. KeyMappingEditorComponent& owner;
  319. };
  320. //==============================================================================
  321. KeyMappingEditorComponent::KeyMappingEditorComponent (KeyPressMappingSet& mappingManager,
  322. const bool showResetToDefaultButton)
  323. : mappings (mappingManager),
  324. resetButton (TRANS ("reset to defaults"))
  325. {
  326. treeItem = new TopLevelItem (*this);
  327. if (showResetToDefaultButton)
  328. {
  329. addAndMakeVisible (resetButton);
  330. resetButton.addListener (treeItem);
  331. }
  332. addAndMakeVisible (tree);
  333. tree.setColour (TreeView::backgroundColourId, findColour (backgroundColourId));
  334. tree.setRootItemVisible (false);
  335. tree.setDefaultOpenness (true);
  336. tree.setRootItem (treeItem);
  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. const ApplicationCommandInfo* const ci = mappings.getCommandManager().getCommandForID (commandID);
  372. return ci != nullptr && (ci->flags & ApplicationCommandInfo::hiddenFromKeyEditor) == 0;
  373. }
  374. bool KeyMappingEditorComponent::isCommandReadOnly (const CommandID commandID)
  375. {
  376. const ApplicationCommandInfo* const 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. }