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.

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