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.

juce_ApplicationCommandTarget.h 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. A command target publishes a list of command IDs that it can perform.
  24. An ApplicationCommandManager despatches commands to targets, which must be
  25. able to provide information about what commands they can handle.
  26. To create a target, you'll need to inherit from this class, implementing all of
  27. its pure virtual methods.
  28. For info about how a target is chosen to receive a command, see
  29. ApplicationCommandManager::getFirstCommandTarget().
  30. @see ApplicationCommandManager, ApplicationCommandInfo
  31. */
  32. class JUCE_API ApplicationCommandTarget
  33. {
  34. public:
  35. //==============================================================================
  36. /** Creates a command target. */
  37. ApplicationCommandTarget();
  38. /** Destructor. */
  39. virtual ~ApplicationCommandTarget();
  40. //==============================================================================
  41. /**
  42. Contains contextual details about the invocation of a command.
  43. */
  44. struct JUCE_API InvocationInfo
  45. {
  46. //==============================================================================
  47. InvocationInfo (const CommandID commandID);
  48. //==============================================================================
  49. /** The UID of the command that should be performed. */
  50. CommandID commandID;
  51. /** The command's flags.
  52. See ApplicationCommandInfo for a description of these flag values.
  53. */
  54. int commandFlags;
  55. //==============================================================================
  56. /** The types of context in which the command might be called. */
  57. enum InvocationMethod
  58. {
  59. direct = 0, /**< The command is being invoked directly by a piece of code. */
  60. fromKeyPress, /**< The command is being invoked by a key-press. */
  61. fromMenu, /**< The command is being invoked by a menu selection. */
  62. fromButton /**< The command is being invoked by a button click. */
  63. };
  64. /** The type of event that triggered this command. */
  65. InvocationMethod invocationMethod;
  66. //==============================================================================
  67. /** If triggered by a keypress or menu, this will be the component that had the
  68. keyboard focus at the time.
  69. If triggered by a button, it may be set to that component, or it may be null.
  70. */
  71. Component* originatingComponent;
  72. //==============================================================================
  73. /** The keypress that was used to invoke it.
  74. Note that this will be an invalid keypress if the command was invoked
  75. by some other means than a keyboard shortcut.
  76. */
  77. KeyPress keyPress;
  78. /** True if the callback is being invoked when the key is pressed,
  79. false if the key is being released.
  80. @see KeyPressMappingSet::addCommand()
  81. */
  82. bool isKeyDown;
  83. /** If the key is being released, this indicates how long it had been held
  84. down for.
  85. (Only relevant if isKeyDown is false.)
  86. */
  87. int millisecsSinceKeyPressed;
  88. };
  89. //==============================================================================
  90. /** This must return the next target to try after this one.
  91. When a command is being sent, and the first target can't handle
  92. that command, this method is used to determine the next target that should
  93. be tried.
  94. It may return nullptr if it doesn't know of another target.
  95. If your target is a Component, you would usually use the findFirstTargetParentComponent()
  96. method to return a parent component that might want to handle it.
  97. @see invoke
  98. */
  99. virtual ApplicationCommandTarget* getNextCommandTarget() = 0;
  100. /** This must return a complete list of commands that this target can handle.
  101. Your target should add all the command IDs that it handles to the array that is
  102. passed-in.
  103. */
  104. virtual void getAllCommands (Array<CommandID>& commands) = 0;
  105. /** This must provide details about one of the commands that this target can perform.
  106. This will be called with one of the command IDs that the target provided in its
  107. getAllCommands() methods.
  108. It should fill-in all appropriate fields of the ApplicationCommandInfo structure with
  109. suitable information about the command. (The commandID field will already have been filled-in
  110. by the caller).
  111. The easiest way to set the info is using the ApplicationCommandInfo::setInfo() method to
  112. set all the fields at once.
  113. If the command is currently inactive for some reason, this method must use
  114. ApplicationCommandInfo::setActive() to make that clear, (or it should set the isDisabled
  115. bit of the ApplicationCommandInfo::flags field).
  116. Any default key-presses for the command should be appended to the
  117. ApplicationCommandInfo::defaultKeypresses field.
  118. Note that if you change something that affects the status of the commands
  119. that would be returned by this method (e.g. something that makes some commands
  120. active or inactive), you should call ApplicationCommandManager::commandStatusChanged()
  121. to cause the manager to refresh its status.
  122. */
  123. virtual void getCommandInfo (CommandID commandID, ApplicationCommandInfo& result) = 0;
  124. /** This must actually perform the specified command.
  125. If this target is able to perform the command specified by the commandID field of the
  126. InvocationInfo structure, then it should do so, and must return true.
  127. If it can't handle this command, it should return false, which tells the caller to pass
  128. the command on to the next target in line.
  129. @see invoke, ApplicationCommandManager::invoke
  130. */
  131. virtual bool perform (const InvocationInfo& info) = 0;
  132. //==============================================================================
  133. /** Makes this target invoke a command.
  134. Your code can call this method to invoke a command on this target, but normally
  135. you'd call it indirectly via ApplicationCommandManager::invoke() or
  136. ApplicationCommandManager::invokeDirectly().
  137. If this target can perform the given command, it will call its perform() method to
  138. do so. If not, then getNextCommandTarget() will be used to determine the next target
  139. to try, and the command will be passed along to it.
  140. @param invocationInfo this must be correctly filled-in, describing the context for
  141. the invocation.
  142. @param asynchronously if false, the command will be performed before this method returns.
  143. If true, a message will be posted so that the command will be performed
  144. later on the message thread, and this method will return immediately.
  145. @see perform, ApplicationCommandManager::invoke
  146. */
  147. bool invoke (const InvocationInfo& invocationInfo,
  148. const bool asynchronously);
  149. /** Invokes a given command directly on this target.
  150. This is just an easy way to call invoke() without having to fill out the InvocationInfo
  151. structure.
  152. */
  153. bool invokeDirectly (const CommandID commandID,
  154. const bool asynchronously);
  155. //==============================================================================
  156. /** Searches this target and all subsequent ones for the first one that can handle
  157. the specified command.
  158. This will use getNextCommandTarget() to determine the chain of targets to try
  159. after this one.
  160. */
  161. ApplicationCommandTarget* getTargetForCommand (const CommandID commandID);
  162. /** Checks whether this command can currently be performed by this target.
  163. This will return true only if a call to getCommandInfo() doesn't set the
  164. isDisabled flag to indicate that the command is inactive.
  165. */
  166. bool isCommandActive (const CommandID commandID);
  167. /** If this object is a Component, this method will search upwards in its current
  168. UI hierarchy for the next parent component that implements the
  169. ApplicationCommandTarget class.
  170. If your target is a Component, this is a very handy method to use in your
  171. getNextCommandTarget() implementation.
  172. */
  173. ApplicationCommandTarget* findFirstTargetParentComponent();
  174. private:
  175. //==============================================================================
  176. class CommandMessage;
  177. friend class CommandMessage;
  178. bool tryToInvoke (const InvocationInfo&, bool async);
  179. JUCE_DECLARE_WEAK_REFERENCEABLE (ApplicationCommandTarget)
  180. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ApplicationCommandTarget)
  181. };
  182. } // namespace juce