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.

295 lines
12KB

  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. 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 lambda function.
  122. The lambda 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. static ModalComponentManager::Callback* create (std::function<void(int)>);
  127. //==============================================================================
  128. /** This is a utility function to create a ModalComponentManager::Callback that will
  129. call a static function with a parameter.
  130. The function that you supply must take two parameters - the first being an int, which is
  131. the result code that was used when the modal component was dismissed, and the second
  132. can be a custom type. Note that this custom value will be copied and stored, so it must
  133. be a primitive type or a class that provides copy-by-value semantics.
  134. E.g. @code
  135. static void myCallbackFunction (int modalResult, double customValue)
  136. {
  137. if (modalResult == 1)
  138. doSomethingWith (customValue);
  139. }
  140. Component* someKindOfComp;
  141. ...
  142. someKindOfComp->enterModalState (true, ModalCallbackFunction::create (myCallbackFunction, 3.0));
  143. @endcode
  144. @see ModalComponentManager::Callback
  145. */
  146. template <typename ParamType>
  147. static ModalComponentManager::Callback* create (void (*functionToCall) (int, ParamType),
  148. ParamType parameterValue)
  149. {
  150. return create ([=] (int r) { functionToCall (r, parameterValue); });
  151. }
  152. //==============================================================================
  153. /** This is a utility function to create a ModalComponentManager::Callback that will
  154. call a static function with two custom parameters.
  155. The function that you supply must take three parameters - the first being an int, which is
  156. the result code that was used when the modal component was dismissed, and the next two are
  157. your custom types. Note that these custom values will be copied and stored, so they must
  158. be primitive types or classes that provide copy-by-value semantics.
  159. E.g. @code
  160. static void myCallbackFunction (int modalResult, double customValue1, String customValue2)
  161. {
  162. if (modalResult == 1)
  163. doSomethingWith (customValue1, customValue2);
  164. }
  165. Component* someKindOfComp;
  166. ...
  167. someKindOfComp->enterModalState (true, ModalCallbackFunction::create (myCallbackFunction, 3.0, String ("xyz")));
  168. @endcode
  169. @see ModalComponentManager::Callback
  170. */
  171. template <typename ParamType1, typename ParamType2>
  172. static ModalComponentManager::Callback* withParam (void (*functionToCall) (int, ParamType1, ParamType2),
  173. ParamType1 parameterValue1,
  174. ParamType2 parameterValue2)
  175. {
  176. return create ([=] (int r) { functionToCall (r, parameterValue1, parameterValue2); });
  177. }
  178. //==============================================================================
  179. /** This is a utility function to create a ModalComponentManager::Callback that will
  180. call a static function with a component.
  181. The function that you supply must take two parameters - the first being an int, which is
  182. the result code that was used when the modal component was dismissed, and the second
  183. can be a Component class. The component will be stored as a WeakReference, so that if it gets
  184. deleted before this callback is invoked, the pointer that is passed to the function will be null.
  185. E.g. @code
  186. static void myCallbackFunction (int modalResult, Slider* mySlider)
  187. {
  188. if (modalResult == 1 && mySlider != nullptr) // (must check that mySlider isn't null in case it was deleted..)
  189. mySlider->setValue (0.0);
  190. }
  191. Component* someKindOfComp;
  192. Slider* mySlider;
  193. ...
  194. someKindOfComp->enterModalState (true, ModalCallbackFunction::forComponent (myCallbackFunction, mySlider));
  195. @endcode
  196. @see ModalComponentManager::Callback
  197. */
  198. template <class ComponentType>
  199. static ModalComponentManager::Callback* forComponent (void (*functionToCall) (int, ComponentType*),
  200. ComponentType* component)
  201. {
  202. WeakReference<Component> comp (component);
  203. return create ([=] (int r) { functionToCall (r, static_cast<ComponentType*> (comp.get())); });
  204. }
  205. //==============================================================================
  206. /** Creates a ModalComponentManager::Callback that will call a static function with a component.
  207. The function that you supply must take three parameters - the first being an int, which is
  208. the result code that was used when the modal component was dismissed, the second being a Component
  209. class, and the third being a custom type (which must be a primitive type or have copy-by-value semantics).
  210. The component will be stored as a WeakReference, so that if it gets deleted before this callback is
  211. invoked, the pointer that is passed into the function will be null.
  212. E.g. @code
  213. static void myCallbackFunction (int modalResult, Slider* mySlider, String customParam)
  214. {
  215. if (modalResult == 1 && mySlider != nullptr) // (must check that mySlider isn't null in case it was deleted..)
  216. mySlider->setName (customParam);
  217. }
  218. Component* someKindOfComp;
  219. Slider* mySlider;
  220. ...
  221. someKindOfComp->enterModalState (true, ModalCallbackFunction::forComponent (myCallbackFunction, mySlider, String ("hello")));
  222. @endcode
  223. @see ModalComponentManager::Callback
  224. */
  225. template <class ComponentType, typename ParamType>
  226. static ModalComponentManager::Callback* forComponent (void (*functionToCall) (int, ComponentType*, ParamType),
  227. ComponentType* component,
  228. ParamType param)
  229. {
  230. WeakReference<Component> comp (component);
  231. return create ([=] (int r) { functionToCall (r, static_cast<ComponentType*> (comp.get()), param); });
  232. }
  233. private:
  234. ModalCallbackFunction() = delete;
  235. ~ModalCallbackFunction() = delete;
  236. };
  237. } // namespace juce