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.

276 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-7 by Raw Material Software ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the
  7. GNU General Public License, as published by the Free Software Foundation;
  8. either version 2 of the License, or (at your option) any later version.
  9. JUCE is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with JUCE; if not, visit www.gnu.org/licenses or write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. ------------------------------------------------------------------------------
  18. If you'd like to release a closed-source product which uses JUCE, commercial
  19. licenses are also available: visit www.rawmaterialsoftware.com/juce for
  20. more information.
  21. ==============================================================================
  22. */
  23. #ifndef __JUCE_KEYPRESSMAPPINGSET_JUCEHEADER__
  24. #define __JUCE_KEYPRESSMAPPINGSET_JUCEHEADER__
  25. #include "juce_KeyListener.h"
  26. #include "../../../application/juce_ApplicationCommandManager.h"
  27. #include "../../../events/juce_ChangeBroadcaster.h"
  28. #include "../../../../juce_core/text/juce_StringArray.h"
  29. #include "../../../../juce_core/text/juce_XmlElement.h"
  30. #include "../../../../juce_core/containers/juce_OwnedArray.h"
  31. //==============================================================================
  32. /**
  33. Manages and edits a list of keypresses, which it uses to invoke the appropriate
  34. command in a ApplicationCommandManager.
  35. Normally, you won't actually create a KeyPressMappingSet directly, because
  36. each ApplicationCommandManager contains its own KeyPressMappingSet, so typically
  37. you'd create yourself an ApplicationCommandManager, and call its
  38. ApplicationCommandManager::getKeyMappings() method to get a pointer to its
  39. KeyPressMappingSet.
  40. For one of these to actually use keypresses, you'll need to add it as a KeyListener
  41. to the top-level component for which you want to handle keystrokes. So for example:
  42. @code
  43. class MyMainWindow : public Component
  44. {
  45. ApplicationCommandManager* myCommandManager;
  46. public:
  47. MyMainWindow()
  48. {
  49. myCommandManager = new ApplicationCommandManager();
  50. // first, make sure the command manager has registered all the commands that its
  51. // targets can perform..
  52. myCommandManager->registerAllCommandsForTarget (myCommandTarget1);
  53. myCommandManager->registerAllCommandsForTarget (myCommandTarget2);
  54. // this will use the command manager to initialise the KeyPressMappingSet with
  55. // the default keypresses that were specified when the targets added their commands
  56. // to the manager.
  57. myCommandManager->getKeyMappings()->resetToDefaultMappings();
  58. // having set up the default key-mappings, you might now want to load the last set
  59. // of mappings that the user configured.
  60. myCommandManager->getKeyMappings()->restoreFromXml (lastSavedKeyMappingsXML);
  61. // Now tell our top-level window to send any keypresses that arrive to the
  62. // KeyPressMappingSet, which will use them to invoke the appropriate commands.
  63. addKeyListener (myCommandManager->getKeyMappings());
  64. }
  65. ...
  66. }
  67. @endcode
  68. KeyPressMappingSet derives from ChangeBroadcaster so that interested parties can
  69. register to be told when a command or mapping is added, removed, etc.
  70. There's also a UI component called KeyMappingEditorComponent that can be used
  71. to easily edit the key mappings.
  72. @see Component::addKeyListener(), KeyMappingEditorComponent, ApplicationCommandManager
  73. */
  74. class JUCE_API KeyPressMappingSet : public KeyListener,
  75. public ChangeBroadcaster,
  76. public FocusChangeListener
  77. {
  78. public:
  79. //==============================================================================
  80. /** Creates a KeyPressMappingSet for a given command manager.
  81. Normally, you won't actually create a KeyPressMappingSet directly, because
  82. each ApplicationCommandManager contains its own KeyPressMappingSet, so the
  83. best thing to do is to create your ApplicationCommandManager, and use the
  84. ApplicationCommandManager::getKeyMappings() method to access its mappings.
  85. When a suitable keypress happens, the manager's invoke() method will be
  86. used to invoke the appropriate command.
  87. @see ApplicationCommandManager
  88. */
  89. KeyPressMappingSet (ApplicationCommandManager* const commandManager) throw();
  90. /** Creates an copy of a KeyPressMappingSet. */
  91. KeyPressMappingSet (const KeyPressMappingSet& other) throw();
  92. /** Destructor. */
  93. ~KeyPressMappingSet();
  94. //==============================================================================
  95. ApplicationCommandManager* getCommandManager() const throw() { return commandManager; }
  96. //==============================================================================
  97. /** Returns a list of keypresses that are assigned to a particular command.
  98. @param commandID the command's ID
  99. */
  100. const Array <KeyPress> getKeyPressesAssignedToCommand (const CommandID commandID) const throw();
  101. /** Assigns a keypress to a command.
  102. If the keypress is already assigned to a different command, it will first be
  103. removed from that command, to avoid it triggering multiple functions.
  104. @param commandID the ID of the command that you want to add a keypress to. If
  105. this is 0, the keypress will be removed from anything that it
  106. was previously assigned to, but not re-assigned
  107. @param newKeyPress the new key-press
  108. @param insertIndex if this is less than zero, the key will be appended to the
  109. end of the list of keypresses; otherwise the new keypress will
  110. be inserted into the existing list at this index
  111. */
  112. void addKeyPress (const CommandID commandID,
  113. const KeyPress& newKeyPress,
  114. int insertIndex = -1) throw();
  115. /** Reset all mappings to the defaults, as dictated by the ApplicationCommandManager.
  116. @see resetToDefaultMapping
  117. */
  118. void resetToDefaultMappings() throw();
  119. /** Resets all key-mappings to the defaults for a particular command.
  120. @see resetToDefaultMappings
  121. */
  122. void resetToDefaultMapping (const CommandID commandID) throw();
  123. /** Removes all keypresses that are assigned to any commands. */
  124. void clearAllKeyPresses() throw();
  125. /** Removes all keypresses that are assigned to a particular command. */
  126. void clearAllKeyPresses (const CommandID commandID) throw();
  127. /** Removes one of the keypresses that are assigned to a command.
  128. See the getKeyPressesAssignedToCommand() for the list of keypresses to
  129. which the keyPressIndex refers.
  130. */
  131. void removeKeyPress (const CommandID commandID,
  132. const int keyPressIndex) throw();
  133. /** Removes a keypress from any command that it may be assigned to.
  134. */
  135. void removeKeyPress (const KeyPress& keypress) throw();
  136. /** Returns true if the given command is linked to this key. */
  137. bool containsMapping (const CommandID commandID,
  138. const KeyPress& keyPress) const throw();
  139. //==============================================================================
  140. /** Looks for a command that corresponds to a keypress.
  141. @returns the UID of the command or 0 if none was found
  142. */
  143. CommandID findCommandForKeyPress (const KeyPress& keyPress) const throw();
  144. //==============================================================================
  145. /** Tries to recreate the mappings from a previously stored state.
  146. The XML passed in must have been created by the createXml() method.
  147. If the stored state makes any reference to commands that aren't
  148. currently available, these will be ignored.
  149. If the set of mappings being loaded was a set of differences (using createXml (true)),
  150. then this will call resetToDefaultMappings() and then merge the saved mappings
  151. on top. If the saved set was created with createXml (false), then this method
  152. will first clear all existing mappings and load the saved ones as a complete set.
  153. @returns true if it manages to load the XML correctly
  154. @see createXml
  155. */
  156. bool restoreFromXml (const XmlElement& xmlVersion);
  157. /** Creates an XML representation of the current mappings.
  158. This will produce a lump of XML that can be later reloaded using
  159. restoreFromXml() to recreate the current mapping state.
  160. The object that is returned must be deleted by the caller.
  161. @param saveDifferencesFromDefaultSet if this is false, then all keypresses
  162. will be saved into the XML. If it's true, then the XML will
  163. only store the differences between the current mappings and
  164. the default mappings you'd get from calling resetToDefaultMappings().
  165. The advantage of saving a set of differences from the default is that
  166. if you change the default mappings (in a new version of your app, for
  167. example), then these will be merged into a user's saved preferences.
  168. @see restoreFromXml
  169. */
  170. XmlElement* createXml (const bool saveDifferencesFromDefaultSet) const;
  171. //==============================================================================
  172. /** @internal */
  173. bool keyPressed (const KeyPress& key, Component* originatingComponent);
  174. /** @internal */
  175. bool keyStateChanged (Component* originatingComponent);
  176. /** @internal */
  177. void globalFocusChanged (Component* focusedComponent);
  178. //==============================================================================
  179. juce_UseDebuggingNewOperator
  180. private:
  181. //==============================================================================
  182. ApplicationCommandManager* commandManager;
  183. struct CommandMapping
  184. {
  185. CommandID commandID;
  186. Array <KeyPress> keypresses;
  187. bool wantsKeyUpDownCallbacks;
  188. };
  189. OwnedArray <CommandMapping> mappings;
  190. struct KeyPressTime
  191. {
  192. KeyPress key;
  193. uint32 timeWhenPressed;
  194. };
  195. OwnedArray <KeyPressTime> keysDown;
  196. void handleMessage (const Message& message);
  197. void invokeCommand (const CommandID commandID,
  198. const KeyPress& keyPress,
  199. const bool isKeyDown,
  200. const int millisecsSinceKeyPressed,
  201. Component* const originatingComponent) const;
  202. const KeyPressMappingSet& operator= (const KeyPressMappingSet&);
  203. };
  204. #endif // __JUCE_KEYPRESSMAPPINGSET_JUCEHEADER__