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.

365 lines
15KB

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