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.

242 lines
10KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. #pragma once
  18. //==============================================================================
  19. /**
  20. Manages and edits a list of keypresses, which it uses to invoke the appropriate
  21. command in an ApplicationCommandManager.
  22. Normally, you won't actually create a KeyPressMappingSet directly, because
  23. each ApplicationCommandManager contains its own KeyPressMappingSet, so typically
  24. you'd create yourself an ApplicationCommandManager, and call its
  25. ApplicationCommandManager::getKeyMappings() method to get a pointer to its
  26. KeyPressMappingSet.
  27. For one of these to actually use keypresses, you'll need to add it as a KeyListener
  28. to the top-level component for which you want to handle keystrokes. So for example:
  29. @code
  30. class MyMainWindow : public Component
  31. {
  32. ApplicationCommandManager* myCommandManager;
  33. public:
  34. MyMainWindow()
  35. {
  36. myCommandManager = new ApplicationCommandManager();
  37. // first, make sure the command manager has registered all the commands that its
  38. // targets can perform..
  39. myCommandManager->registerAllCommandsForTarget (myCommandTarget1);
  40. myCommandManager->registerAllCommandsForTarget (myCommandTarget2);
  41. // this will use the command manager to initialise the KeyPressMappingSet with
  42. // the default keypresses that were specified when the targets added their commands
  43. // to the manager.
  44. myCommandManager->getKeyMappings()->resetToDefaultMappings();
  45. // having set up the default key-mappings, you might now want to load the last set
  46. // of mappings that the user configured.
  47. myCommandManager->getKeyMappings()->restoreFromXml (lastSavedKeyMappingsXML);
  48. // Now tell our top-level window to send any keypresses that arrive to the
  49. // KeyPressMappingSet, which will use them to invoke the appropriate commands.
  50. addKeyListener (myCommandManager->getKeyMappings());
  51. }
  52. ...
  53. }
  54. @endcode
  55. KeyPressMappingSet derives from ChangeBroadcaster so that interested parties can
  56. register to be told when a command or mapping is added, removed, etc.
  57. There's also a UI component called KeyMappingEditorComponent that can be used
  58. to easily edit the key mappings.
  59. @see Component::addKeyListener(), KeyMappingEditorComponent, ApplicationCommandManager
  60. */
  61. class JUCE_API KeyPressMappingSet : public KeyListener,
  62. public ChangeBroadcaster,
  63. private FocusChangeListener
  64. {
  65. public:
  66. //==============================================================================
  67. /** Creates a KeyPressMappingSet for a given command manager.
  68. Normally, you won't actually create a KeyPressMappingSet directly, because
  69. each ApplicationCommandManager contains its own KeyPressMappingSet, so the
  70. best thing to do is to create your ApplicationCommandManager, and use the
  71. ApplicationCommandManager::getKeyMappings() method to access its mappings.
  72. When a suitable keypress happens, the manager's invoke() method will be
  73. used to invoke the appropriate command.
  74. @see ApplicationCommandManager
  75. */
  76. explicit KeyPressMappingSet (ApplicationCommandManager&);
  77. /** Creates an copy of a KeyPressMappingSet. */
  78. KeyPressMappingSet (const KeyPressMappingSet&);
  79. /** Destructor. */
  80. ~KeyPressMappingSet();
  81. //==============================================================================
  82. ApplicationCommandManager& getCommandManager() const noexcept { return commandManager; }
  83. //==============================================================================
  84. /** Returns a list of keypresses that are assigned to a particular command.
  85. @param commandID the command's ID
  86. */
  87. Array<KeyPress> getKeyPressesAssignedToCommand (CommandID commandID) const;
  88. /** Assigns a keypress to a command.
  89. If the keypress is already assigned to a different command, it will first be
  90. removed from that command, to avoid it triggering multiple functions.
  91. @param commandID the ID of the command that you want to add a keypress to. If
  92. this is 0, the keypress will be removed from anything that it
  93. was previously assigned to, but not re-assigned
  94. @param newKeyPress the new key-press
  95. @param insertIndex if this is less than zero, the key will be appended to the
  96. end of the list of keypresses; otherwise the new keypress will
  97. be inserted into the existing list at this index
  98. */
  99. void addKeyPress (CommandID commandID,
  100. const KeyPress& newKeyPress,
  101. int insertIndex = -1);
  102. /** Reset all mappings to the defaults, as dictated by the ApplicationCommandManager.
  103. @see resetToDefaultMapping
  104. */
  105. void resetToDefaultMappings();
  106. /** Resets all key-mappings to the defaults for a particular command.
  107. @see resetToDefaultMappings
  108. */
  109. void resetToDefaultMapping (CommandID commandID);
  110. /** Removes all keypresses that are assigned to any commands. */
  111. void clearAllKeyPresses();
  112. /** Removes all keypresses that are assigned to a particular command. */
  113. void clearAllKeyPresses (CommandID commandID);
  114. /** Removes one of the keypresses that are assigned to a command.
  115. See the getKeyPressesAssignedToCommand() for the list of keypresses to
  116. which the keyPressIndex refers.
  117. */
  118. void removeKeyPress (CommandID commandID, int keyPressIndex);
  119. /** Removes a keypress from any command that it may be assigned to. */
  120. void removeKeyPress (const KeyPress& keypress);
  121. /** Returns true if the given command is linked to this key. */
  122. bool containsMapping (CommandID commandID, const KeyPress& keyPress) const noexcept;
  123. //==============================================================================
  124. /** Looks for a command that corresponds to a keypress.
  125. @returns the UID of the command or 0 if none was found
  126. */
  127. CommandID findCommandForKeyPress (const KeyPress& keyPress) const noexcept;
  128. //==============================================================================
  129. /** Tries to recreate the mappings from a previously stored state.
  130. The XML passed in must have been created by the createXml() method.
  131. If the stored state makes any reference to commands that aren't
  132. currently available, these will be ignored.
  133. If the set of mappings being loaded was a set of differences (using createXml (true)),
  134. then this will call resetToDefaultMappings() and then merge the saved mappings
  135. on top. If the saved set was created with createXml (false), then this method
  136. will first clear all existing mappings and load the saved ones as a complete set.
  137. @returns true if it manages to load the XML correctly
  138. @see createXml
  139. */
  140. bool restoreFromXml (const XmlElement& xmlVersion);
  141. /** Creates an XML representation of the current mappings.
  142. This will produce a lump of XML that can be later reloaded using
  143. restoreFromXml() to recreate the current mapping state.
  144. The object that is returned must be deleted by the caller.
  145. @param saveDifferencesFromDefaultSet if this is false, then all keypresses
  146. will be saved into the XML. If it's true, then the XML will
  147. only store the differences between the current mappings and
  148. the default mappings you'd get from calling resetToDefaultMappings().
  149. The advantage of saving a set of differences from the default is that
  150. if you change the default mappings (in a new version of your app, for
  151. example), then these will be merged into a user's saved preferences.
  152. @see restoreFromXml
  153. */
  154. XmlElement* createXml (bool saveDifferencesFromDefaultSet) const;
  155. //==============================================================================
  156. /** @internal */
  157. bool keyPressed (const KeyPress&, Component*) override;
  158. /** @internal */
  159. bool keyStateChanged (bool isKeyDown, Component*) override;
  160. /** @internal */
  161. void globalFocusChanged (Component*) override;
  162. private:
  163. //==============================================================================
  164. ApplicationCommandManager& commandManager;
  165. struct CommandMapping
  166. {
  167. CommandID commandID;
  168. Array<KeyPress> keypresses;
  169. bool wantsKeyUpDownCallbacks;
  170. };
  171. OwnedArray<CommandMapping> mappings;
  172. struct KeyPressTime
  173. {
  174. KeyPress key;
  175. uint32 timeWhenPressed;
  176. };
  177. OwnedArray<KeyPressTime> keysDown;
  178. void invokeCommand (const CommandID, const KeyPress&, const bool isKeyDown,
  179. const int millisecsSinceKeyPressed, Component* originator) const;
  180. KeyPressMappingSet& operator= (const KeyPressMappingSet&);
  181. JUCE_LEAK_DETECTOR (KeyPressMappingSet)
  182. };