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.

318 lines
13KB

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