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.

238 lines
9.8KB

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