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.

325 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. Manages the system's stack of modal components.
  23. Normally you'll just use the Component methods to invoke modal states in components,
  24. and won't have to deal with this class directly, but this is the singleton object that's
  25. used internally to manage the stack.
  26. @see Component::enterModalState, Component::exitModalState, Component::isCurrentlyModal,
  27. Component::getCurrentlyModalComponent, Component::isCurrentlyBlockedByAnotherModalComponent
  28. @tags{GUI}
  29. */
  30. class JUCE_API ModalComponentManager : private AsyncUpdater,
  31. private DeletedAtShutdown
  32. {
  33. public:
  34. //==============================================================================
  35. /** Receives callbacks when a modal component is dismissed.
  36. You can register a callback using Component::enterModalState() or
  37. ModalComponentManager::attachCallback().
  38. For some quick ways of creating callback objects, see the ModalCallbackFunction class.
  39. @see ModalCallbackFunction
  40. */
  41. class JUCE_API Callback
  42. {
  43. public:
  44. /** */
  45. Callback() = default;
  46. /** Destructor. */
  47. virtual ~Callback() = default;
  48. /** Called to indicate that a modal component has been dismissed.
  49. You can register a callback using Component::enterModalState() or
  50. ModalComponentManager::attachCallback().
  51. The returnValue parameter is the value that was passed to Component::exitModalState()
  52. when the component was dismissed.
  53. The callback object will be deleted shortly after this method is called.
  54. */
  55. virtual void modalStateFinished (int returnValue) = 0;
  56. };
  57. //==============================================================================
  58. #ifndef DOXYGEN
  59. JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL (ModalComponentManager)
  60. #endif
  61. //==============================================================================
  62. /** Returns the number of components currently being shown modally.
  63. @see getModalComponent
  64. */
  65. int getNumModalComponents() const;
  66. /** Returns one of the components being shown modally.
  67. An index of 0 is the most recently-shown, topmost component.
  68. */
  69. Component* getModalComponent (int index) const;
  70. /** Returns true if the specified component is in a modal state. */
  71. bool isModal (const Component* component) const;
  72. /** Returns true if the specified component is currently the topmost modal component. */
  73. bool isFrontModalComponent (const Component* component) const;
  74. /** Adds a new callback that will be called when the specified modal component is dismissed.
  75. If the component is modal, then when it is dismissed, either by being hidden, or by calling
  76. Component::exitModalState(), then the Callback::modalStateFinished() method will be
  77. called.
  78. Each component can have any number of callbacks associated with it, and this one is added
  79. to that list.
  80. The object that is passed in will be deleted by the manager when it's no longer needed. If
  81. the given component is not currently modal, the callback object is deleted immediately and
  82. no action is taken.
  83. */
  84. void attachCallback (Component* component, Callback* callback);
  85. /** Brings any modal components to the front. */
  86. void bringModalComponentsToFront (bool topOneShouldGrabFocus = true);
  87. /** Calls exitModalState (0) on any components that are currently modal.
  88. @returns true if any components were modal; false if nothing needed cancelling
  89. */
  90. bool cancelAllModalComponents();
  91. #if JUCE_MODAL_LOOPS_PERMITTED
  92. /** Runs the event loop until the currently topmost modal component is dismissed, and
  93. returns the exit code for that component.
  94. */
  95. int runEventLoopForCurrentComponent();
  96. #endif
  97. protected:
  98. /** Creates a ModalComponentManager.
  99. You shouldn't ever call the constructor - it's a singleton, so use ModalComponentManager::getInstance()
  100. */
  101. ModalComponentManager();
  102. /** Destructor. */
  103. ~ModalComponentManager() override;
  104. /** @internal */
  105. void handleAsyncUpdate() override;
  106. private:
  107. //==============================================================================
  108. friend class Component;
  109. struct ModalItem;
  110. OwnedArray<ModalItem> stack;
  111. void startModal (Component*, bool autoDelete);
  112. void endModal (Component*, int returnValue);
  113. void endModal (Component*);
  114. JUCE_DECLARE_NON_COPYABLE (ModalComponentManager)
  115. };
  116. //==============================================================================
  117. /**
  118. This class provides some handy utility methods for creating ModalComponentManager::Callback
  119. objects that will invoke a static function with some parameters when a modal component is dismissed.
  120. @tags{GUI}
  121. */
  122. class JUCE_API ModalCallbackFunction
  123. {
  124. public:
  125. /** This is a utility function to create a ModalComponentManager::Callback that will
  126. call a callable object.
  127. The function that you supply must take an integer parameter, which is the result code that
  128. was returned when the modal component was dismissed.
  129. @see ModalComponentManager::Callback
  130. */
  131. template <typename CallbackFn>
  132. static ModalComponentManager::Callback* create (CallbackFn&& fn)
  133. {
  134. struct Callable : public ModalComponentManager::Callback
  135. {
  136. explicit Callable (CallbackFn&& f) : fn (std::forward<CallbackFn> (f)) {}
  137. void modalStateFinished (int result) override { NullCheckedInvocation::invoke (std::move (fn), result); }
  138. std::remove_reference_t<CallbackFn> fn;
  139. };
  140. return new Callable (std::forward<CallbackFn> (fn));
  141. }
  142. //==============================================================================
  143. /** This is a utility function to create a ModalComponentManager::Callback that will
  144. call a static function with a parameter.
  145. The function that you supply must take two parameters - the first being an int, which is
  146. the result code that was used when the modal component was dismissed, and the second
  147. can be a custom type. Note that this custom value will be copied and stored, so it must
  148. be a primitive type or a class that provides copy-by-value semantics.
  149. E.g. @code
  150. static void myCallbackFunction (int modalResult, double customValue)
  151. {
  152. if (modalResult == 1)
  153. doSomethingWith (customValue);
  154. }
  155. Component* someKindOfComp;
  156. ...
  157. someKindOfComp->enterModalState (true, ModalCallbackFunction::create (myCallbackFunction, 3.0));
  158. @endcode
  159. @see ModalComponentManager::Callback
  160. */
  161. template <typename ParamType>
  162. static ModalComponentManager::Callback* create (void (*functionToCall) (int, ParamType),
  163. ParamType parameterValue)
  164. {
  165. return create ([functionToCall, parameterValue] (int r)
  166. {
  167. functionToCall (r, parameterValue);
  168. });
  169. }
  170. //==============================================================================
  171. /** This is a utility function to create a ModalComponentManager::Callback that will
  172. call a static function with two custom parameters.
  173. The function that you supply must take three parameters - the first being an int, which is
  174. the result code that was used when the modal component was dismissed, and the next two are
  175. your custom types. Note that these custom values will be copied and stored, so they must
  176. be primitive types or classes that provide copy-by-value semantics.
  177. E.g. @code
  178. static void myCallbackFunction (int modalResult, double customValue1, String customValue2)
  179. {
  180. if (modalResult == 1)
  181. doSomethingWith (customValue1, customValue2);
  182. }
  183. Component* someKindOfComp;
  184. ...
  185. someKindOfComp->enterModalState (true, ModalCallbackFunction::create (myCallbackFunction, 3.0, String ("xyz")));
  186. @endcode
  187. @see ModalComponentManager::Callback
  188. */
  189. template <typename ParamType1, typename ParamType2>
  190. static ModalComponentManager::Callback* withParam (void (*functionToCall) (int, ParamType1, ParamType2),
  191. ParamType1 parameterValue1,
  192. ParamType2 parameterValue2)
  193. {
  194. return create ([functionToCall, parameterValue1, parameterValue2] (int r)
  195. {
  196. functionToCall (r, parameterValue1, parameterValue2);
  197. });
  198. }
  199. //==============================================================================
  200. /** This is a utility function to create a ModalComponentManager::Callback that will
  201. call a static function with a component.
  202. The function that you supply must take two parameters - the first being an int, which is
  203. the result code that was used when the modal component was dismissed, and the second
  204. can be a Component class. The component will be stored as a WeakReference, so that if it gets
  205. deleted before this callback is invoked, the pointer that is passed to the function will be null.
  206. E.g. @code
  207. static void myCallbackFunction (int modalResult, Slider* mySlider)
  208. {
  209. if (modalResult == 1 && mySlider != nullptr) // (must check that mySlider isn't null in case it was deleted..)
  210. mySlider->setValue (0.0);
  211. }
  212. Component* someKindOfComp;
  213. Slider* mySlider;
  214. ...
  215. someKindOfComp->enterModalState (true, ModalCallbackFunction::forComponent (myCallbackFunction, mySlider));
  216. @endcode
  217. @see ModalComponentManager::Callback
  218. */
  219. template <class ComponentType>
  220. static ModalComponentManager::Callback* forComponent (void (*functionToCall) (int, ComponentType*),
  221. ComponentType* component)
  222. {
  223. return create ([functionToCall, comp = WeakReference<Component> { component }] (int r)
  224. {
  225. functionToCall (r, static_cast<ComponentType*> (comp.get()));
  226. });
  227. }
  228. //==============================================================================
  229. /** Creates a ModalComponentManager::Callback that will call a static function with a component.
  230. The function that you supply must take three parameters - the first being an int, which is
  231. the result code that was used when the modal component was dismissed, the second being a Component
  232. class, and the third being a custom type (which must be a primitive type or have copy-by-value semantics).
  233. The component will be stored as a WeakReference, so that if it gets deleted before this callback is
  234. invoked, the pointer that is passed into the function will be null.
  235. E.g. @code
  236. static void myCallbackFunction (int modalResult, Slider* mySlider, String customParam)
  237. {
  238. if (modalResult == 1 && mySlider != nullptr) // (must check that mySlider isn't null in case it was deleted..)
  239. mySlider->setName (customParam);
  240. }
  241. Component* someKindOfComp;
  242. Slider* mySlider;
  243. ...
  244. someKindOfComp->enterModalState (true, ModalCallbackFunction::forComponent (myCallbackFunction, mySlider, String ("hello")));
  245. @endcode
  246. @see ModalComponentManager::Callback
  247. */
  248. template <class ComponentType, typename ParamType>
  249. static ModalComponentManager::Callback* forComponent (void (*functionToCall) (int, ComponentType*, ParamType),
  250. ComponentType* component,
  251. ParamType param)
  252. {
  253. return create ([functionToCall, param, comp = WeakReference<Component> { component }] (int r)
  254. {
  255. functionToCall (r, static_cast<ComponentType*> (comp.get()), param);
  256. });
  257. }
  258. private:
  259. ModalCallbackFunction() = delete;
  260. ~ModalCallbackFunction() = delete;
  261. };
  262. } // namespace juce