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.

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