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.

351 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. #pragma once
  20. //==============================================================================
  21. /**
  22. One of these objects holds a list of all the commands your app can perform,
  23. and despatches these commands when needed.
  24. Application commands are a good way to trigger actions in your app, e.g. "Quit",
  25. "Copy", "Paste", etc. Menus, buttons and keypresses can all be given commands
  26. to invoke automatically, which means you don't have to handle the result of a menu
  27. or button click manually. Commands are despatched to ApplicationCommandTarget objects
  28. which can choose which events they want to handle.
  29. This architecture also allows for nested ApplicationCommandTargets, so that for example
  30. you could have two different objects, one inside the other, both of which can respond to
  31. a "delete" command. Depending on which one has focus, the command will be sent to the
  32. appropriate place, regardless of whether it was triggered by a menu, keypress or some other
  33. method.
  34. To set up your app to use commands, you'll need to do the following:
  35. - Create a global ApplicationCommandManager to hold the list of all possible
  36. commands. (This will also manage a set of key-mappings for them).
  37. - Make some of your UI components (or other objects) inherit from ApplicationCommandTarget.
  38. This allows the object to provide a list of commands that it can perform, and
  39. to handle them.
  40. - Register each type of command using ApplicationCommandManager::registerAllCommandsForTarget(),
  41. or ApplicationCommandManager::registerCommand().
  42. - If you want key-presses to trigger your commands, use the ApplicationCommandManager::getKeyMappings()
  43. method to access the key-mapper object, which you will need to register as a key-listener
  44. in whatever top-level component you're using. See the KeyPressMappingSet class for more help
  45. about setting this up.
  46. - Use methods such as PopupMenu::addCommandItem() or Button::setCommandToTrigger() to
  47. cause these commands to be invoked automatically.
  48. - Commands can be invoked directly by your code using ApplicationCommandManager::invokeDirectly().
  49. When a command is invoked, the ApplicationCommandManager will try to choose the best
  50. ApplicationCommandTarget to receive the specified command. To do this it will use the
  51. current keyboard focus to see which component might be interested, and will search the
  52. component hierarchy for those that also implement the ApplicationCommandTarget interface.
  53. If an ApplicationCommandTarget isn't interested in the command that is being invoked, then
  54. the next one in line will be tried (see the ApplicationCommandTarget::getNextCommandTarget()
  55. method), and so on until ApplicationCommandTarget::getNextCommandTarget() returns nullptr.
  56. At this point if the command still hasn't been performed, it will be passed to the current
  57. JUCEApplication object (which is itself an ApplicationCommandTarget).
  58. To exert some custom control over which ApplicationCommandTarget is chosen to invoke a command,
  59. you can override the ApplicationCommandManager::getFirstCommandTarget() method and choose
  60. the object yourself.
  61. @see ApplicationCommandTarget, ApplicationCommandInfo
  62. */
  63. class JUCE_API ApplicationCommandManager : private AsyncUpdater,
  64. private FocusChangeListener
  65. {
  66. public:
  67. //==============================================================================
  68. /** Creates an ApplicationCommandManager.
  69. Once created, you'll need to register all your app's commands with it, using
  70. ApplicationCommandManager::registerAllCommandsForTarget() or
  71. ApplicationCommandManager::registerCommand().
  72. */
  73. ApplicationCommandManager();
  74. /** Destructor.
  75. Make sure that you don't delete this if pointers to it are still being used by
  76. objects such as PopupMenus or Buttons.
  77. */
  78. virtual ~ApplicationCommandManager();
  79. //==============================================================================
  80. /** Clears the current list of all commands.
  81. Note that this will also clear the contents of the KeyPressMappingSet.
  82. */
  83. void clearCommands();
  84. /** Adds a command to the list of registered commands.
  85. @see registerAllCommandsForTarget
  86. */
  87. void registerCommand (const ApplicationCommandInfo& newCommand);
  88. /** Adds all the commands that this target publishes to the manager's list.
  89. This will use ApplicationCommandTarget::getAllCommands() and ApplicationCommandTarget::getCommandInfo()
  90. to get details about all the commands that this target can do, and will call
  91. registerCommand() to add each one to the manger's list.
  92. @see registerCommand
  93. */
  94. void registerAllCommandsForTarget (ApplicationCommandTarget* target);
  95. /** Removes the command with a specified ID.
  96. Note that this will also remove any key mappings that are mapped to the command.
  97. */
  98. void removeCommand (CommandID commandID);
  99. /** This should be called to tell the manager that one of its registered commands may have changed
  100. its active status.
  101. Because the command manager only finds out whether a command is active or inactive by querying
  102. the current ApplicationCommandTarget, this is used to tell it that things may have changed. It
  103. allows things like buttons to update their enablement, etc.
  104. This method will cause an asynchronous call to ApplicationCommandManagerListener::applicationCommandListChanged()
  105. for any registered listeners.
  106. */
  107. void commandStatusChanged();
  108. //==============================================================================
  109. /** Returns the number of commands that have been registered.
  110. @see registerCommand
  111. */
  112. int getNumCommands() const noexcept { return commands.size(); }
  113. /** Returns the details about one of the registered commands.
  114. The index is between 0 and (getNumCommands() - 1).
  115. */
  116. const ApplicationCommandInfo* getCommandForIndex (int index) const noexcept { return commands [index]; }
  117. /** Returns the details about a given command ID.
  118. This will search the list of registered commands for one with the given command
  119. ID number, and return its associated info. If no matching command is found, this
  120. will return nullptr.
  121. */
  122. const ApplicationCommandInfo* getCommandForID (CommandID commandID) const noexcept;
  123. /** Returns the name field for a command.
  124. An empty string is returned if no command with this ID has been registered.
  125. @see getDescriptionOfCommand
  126. */
  127. String getNameOfCommand (CommandID commandID) const noexcept;
  128. /** Returns the description field for a command.
  129. An empty string is returned if no command with this ID has been registered. If the
  130. command has no description, this will return its short name field instead.
  131. @see getNameOfCommand
  132. */
  133. String getDescriptionOfCommand (CommandID commandID) const noexcept;
  134. /** Returns the list of categories.
  135. This will go through all registered commands, and return a list of all the distinct
  136. categoryName values from their ApplicationCommandInfo structure.
  137. @see getCommandsInCategory()
  138. */
  139. StringArray getCommandCategories() const;
  140. /** Returns a list of all the command UIDs in a particular category.
  141. @see getCommandCategories()
  142. */
  143. Array<CommandID> getCommandsInCategory (const String& categoryName) const;
  144. //==============================================================================
  145. /** Returns the manager's internal set of key mappings.
  146. This object can be used to edit the keypresses. To actually link this object up
  147. to invoke commands when a key is pressed, see the comments for the KeyPressMappingSet
  148. class.
  149. @see KeyPressMappingSet
  150. */
  151. KeyPressMappingSet* getKeyMappings() const noexcept { return keyMappings; }
  152. //==============================================================================
  153. /** Invokes the given command directly, sending it to the default target.
  154. This is just an easy way to call invoke() without having to fill out the InvocationInfo
  155. structure.
  156. */
  157. bool invokeDirectly (CommandID commandID, bool asynchronously);
  158. /** Sends a command to the default target.
  159. This will choose a target using getFirstCommandTarget(), and send the specified command
  160. to it using the ApplicationCommandTarget::invoke() method. This means that if the
  161. first target can't handle the command, it will be passed on to targets further down the
  162. chain (see ApplicationCommandTarget::invoke() for more info).
  163. @param invocationInfo this must be correctly filled-in, describing the context for
  164. the invocation.
  165. @param asynchronously if false, the command will be performed before this method returns.
  166. If true, a message will be posted so that the command will be performed
  167. later on the message thread, and this method will return immediately.
  168. @see ApplicationCommandTarget::invoke
  169. */
  170. bool invoke (const ApplicationCommandTarget::InvocationInfo& invocationInfo,
  171. bool asynchronously);
  172. //==============================================================================
  173. /** Chooses the ApplicationCommandTarget to which a command should be sent.
  174. Whenever the manager needs to know which target a command should be sent to, it calls
  175. this method to determine the first one to try.
  176. By default, this method will return the target that was set by calling setFirstCommandTarget().
  177. If no target is set, it will return the result of findDefaultComponentTarget().
  178. If you need to make sure all commands go via your own custom target, then you can
  179. either use setFirstCommandTarget() to specify a single target, or override this method
  180. if you need more complex logic to choose one.
  181. It may return nullptr if no targets are available.
  182. @see getTargetForCommand, invoke, invokeDirectly
  183. */
  184. virtual ApplicationCommandTarget* getFirstCommandTarget (CommandID commandID);
  185. /** Sets a target to be returned by getFirstCommandTarget().
  186. If this is set to nullptr, then getFirstCommandTarget() will by default return the
  187. result of findDefaultComponentTarget().
  188. If you use this to set a target, make sure you call setFirstCommandTarget(nullptr)
  189. before deleting the target object.
  190. */
  191. void setFirstCommandTarget (ApplicationCommandTarget* newTarget) noexcept;
  192. /** Tries to find the best target to use to perform a given command.
  193. This will call getFirstCommandTarget() to find the preferred target, and will
  194. check whether that target can handle the given command. If it can't, then it'll use
  195. ApplicationCommandTarget::getNextCommandTarget() to find the next one to try, and
  196. so on until no more are available.
  197. If no targets are found that can perform the command, this method will return nullptr.
  198. If a target is found, then it will get the target to fill-in the upToDateInfo
  199. structure with the latest info about that command, so that the caller can see
  200. whether the command is disabled, ticked, etc.
  201. */
  202. ApplicationCommandTarget* getTargetForCommand (CommandID commandID,
  203. ApplicationCommandInfo& upToDateInfo);
  204. //==============================================================================
  205. /** Registers a listener that will be called when various events occur. */
  206. void addListener (ApplicationCommandManagerListener* listener);
  207. /** Deregisters a previously-added listener. */
  208. void removeListener (ApplicationCommandManagerListener* listener);
  209. //==============================================================================
  210. /** Looks for a suitable command target based on which Components have the keyboard focus.
  211. This is used by the default implementation of ApplicationCommandTarget::getFirstCommandTarget(),
  212. but is exposed here in case it's useful.
  213. It tries to pick the best ApplicationCommandTarget by looking at focused components, top level
  214. windows, etc., and using the findTargetForComponent() method.
  215. */
  216. static ApplicationCommandTarget* findDefaultComponentTarget();
  217. /** Examines this component and all its parents in turn, looking for the first one
  218. which is an ApplicationCommandTarget.
  219. Returns the first ApplicationCommandTarget that it finds, or nullptr if none of them
  220. implement that class.
  221. */
  222. static ApplicationCommandTarget* findTargetForComponent (Component*);
  223. private:
  224. //==============================================================================
  225. OwnedArray<ApplicationCommandInfo> commands;
  226. ListenerList<ApplicationCommandManagerListener> listeners;
  227. ScopedPointer<KeyPressMappingSet> keyMappings;
  228. ApplicationCommandTarget* firstTarget;
  229. void sendListenerInvokeCallback (const ApplicationCommandTarget::InvocationInfo&);
  230. void handleAsyncUpdate() override;
  231. void globalFocusChanged (Component*) override;
  232. ApplicationCommandInfo* getMutableCommandForID (CommandID) const noexcept;
  233. #if JUCE_CATCH_DEPRECATED_CODE_MISUSE
  234. // This is just here to cause a compile error in old code that hasn't been changed to use the new
  235. // version of this method.
  236. virtual short getFirstCommandTarget() { return 0; }
  237. #endif
  238. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ApplicationCommandManager)
  239. };
  240. //==============================================================================
  241. /**
  242. A listener that receives callbacks from an ApplicationCommandManager when
  243. commands are invoked or the command list is changed.
  244. @see ApplicationCommandManager::addListener, ApplicationCommandManager::removeListener
  245. */
  246. class JUCE_API ApplicationCommandManagerListener
  247. {
  248. public:
  249. //==============================================================================
  250. /** Destructor. */
  251. virtual ~ApplicationCommandManagerListener() {}
  252. /** Called when an app command is about to be invoked. */
  253. virtual void applicationCommandInvoked (const ApplicationCommandTarget::InvocationInfo&) = 0;
  254. /** Called when commands are registered or deregistered from the
  255. command manager, or when commands are made active or inactive.
  256. Note that if you're using this to watch for changes to whether a command is disabled,
  257. you'll need to make sure that ApplicationCommandManager::commandStatusChanged() is called
  258. whenever the status of your command might have changed.
  259. */
  260. virtual void applicationCommandListChanged() = 0;
  261. };