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.

370 lines
15KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. juce_DeclareSingleton_SingleThreaded_Minimal (ModalComponentManager)
  57. //==============================================================================
  58. /** Returns the number of components currently being shown modally.
  59. @see getModalComponent
  60. */
  61. int getNumModalComponents() const;
  62. /** Returns one of the components being shown modally.
  63. An index of 0 is the most recently-shown, topmost component.
  64. */
  65. Component* getModalComponent (int index) const;
  66. /** Returns true if the specified component is in a modal state. */
  67. bool isModal (Component* component) const;
  68. /** Returns true if the specified component is currently the topmost modal component. */
  69. bool isFrontModalComponent (Component* component) const;
  70. /** Adds a new callback that will be called when the specified modal component is dismissed.
  71. If the component is modal, then when it is dismissed, either by being hidden, or by calling
  72. Component::exitModalState(), then the Callback::modalStateFinished() method will be
  73. called.
  74. Each component can have any number of callbacks associated with it, and this one is added
  75. to that list.
  76. The object that is passed in will be deleted by the manager when it's no longer needed. If
  77. the given component is not currently modal, the callback object is deleted immediately and
  78. no action is taken.
  79. */
  80. void attachCallback (Component* component, Callback* callback);
  81. /** Brings any modal components to the front. */
  82. void bringModalComponentsToFront (bool topOneShouldGrabFocus = true);
  83. /** Calls exitModalState (0) on any components that are currently modal.
  84. @returns true if any components were modal; false if nothing needed cancelling
  85. */
  86. bool cancelAllModalComponents();
  87. #if JUCE_MODAL_LOOPS_PERMITTED
  88. /** Runs the event loop until the currently topmost modal component is dismissed, and
  89. returns the exit code for that component.
  90. */
  91. int runEventLoopForCurrentComponent();
  92. #endif
  93. protected:
  94. /** Creates a ModalComponentManager.
  95. You shouldn't ever call the constructor - it's a singleton, so use ModalComponentManager::getInstance()
  96. */
  97. ModalComponentManager();
  98. /** Destructor. */
  99. ~ModalComponentManager();
  100. /** @internal */
  101. void handleAsyncUpdate() override;
  102. private:
  103. //==============================================================================
  104. class ModalItem;
  105. class ReturnValueRetriever;
  106. friend class Component;
  107. friend struct ContainerDeletePolicy<ModalItem>;
  108. OwnedArray<ModalItem> stack;
  109. void startModal (Component*, bool autoDelete);
  110. void endModal (Component*, int returnValue);
  111. void endModal (Component*);
  112. JUCE_DECLARE_NON_COPYABLE (ModalComponentManager)
  113. };
  114. //==============================================================================
  115. /**
  116. This class provides some handy utility methods for creating ModalComponentManager::Callback
  117. objects that will invoke a static function with some parameters when a modal component is dismissed.
  118. */
  119. class ModalCallbackFunction
  120. {
  121. public:
  122. //==============================================================================
  123. /** This is a utility function to create a ModalComponentManager::Callback that will
  124. call a static function with a parameter.
  125. The function that you supply must take two parameters - the first being an int, which is
  126. the result code that was used when the modal component was dismissed, and the second
  127. can be a custom type. Note that this custom value will be copied and stored, so it must
  128. be a primitive type or a class that provides copy-by-value semantics.
  129. E.g. @code
  130. static void myCallbackFunction (int modalResult, double customValue)
  131. {
  132. if (modalResult == 1)
  133. doSomethingWith (customValue);
  134. }
  135. Component* someKindOfComp;
  136. ...
  137. someKindOfComp->enterModalState (ModalCallbackFunction::create (myCallbackFunction, 3.0));
  138. @endcode
  139. @see ModalComponentManager::Callback
  140. */
  141. template <typename ParamType>
  142. static ModalComponentManager::Callback* create (void (*functionToCall) (int, ParamType),
  143. ParamType parameterValue)
  144. {
  145. return new FunctionCaller1 <ParamType> (functionToCall, parameterValue);
  146. }
  147. //==============================================================================
  148. /** This is a utility function to create a ModalComponentManager::Callback that will
  149. call a static function with two custom parameters.
  150. The function that you supply must take three parameters - the first being an int, which is
  151. the result code that was used when the modal component was dismissed, and the next two are
  152. your custom types. Note that these custom values will be copied and stored, so they must
  153. be primitive types or classes that provide copy-by-value semantics.
  154. E.g. @code
  155. static void myCallbackFunction (int modalResult, double customValue1, String customValue2)
  156. {
  157. if (modalResult == 1)
  158. doSomethingWith (customValue1, customValue2);
  159. }
  160. Component* someKindOfComp;
  161. ...
  162. someKindOfComp->enterModalState (ModalCallbackFunction::create (myCallbackFunction, 3.0, String ("xyz")));
  163. @endcode
  164. @see ModalComponentManager::Callback
  165. */
  166. template <typename ParamType1, typename ParamType2>
  167. static ModalComponentManager::Callback* withParam (void (*functionToCall) (int, ParamType1, ParamType2),
  168. ParamType1 parameterValue1,
  169. ParamType2 parameterValue2)
  170. {
  171. return new FunctionCaller2 <ParamType1, ParamType2> (functionToCall, parameterValue1, parameterValue2);
  172. }
  173. //==============================================================================
  174. /** This is a utility function to create a ModalComponentManager::Callback that will
  175. call a static function with a component.
  176. The function that you supply must take two parameters - the first being an int, which is
  177. the result code that was used when the modal component was dismissed, and the second
  178. can be a Component class. The component will be stored as a WeakReference, so that if it gets
  179. deleted before this callback is invoked, the pointer that is passed to the function will be null.
  180. E.g. @code
  181. static void myCallbackFunction (int modalResult, Slider* mySlider)
  182. {
  183. if (modalResult == 1 && mySlider != nullptr) // (must check that mySlider isn't null in case it was deleted..)
  184. mySlider->setValue (0.0);
  185. }
  186. Component* someKindOfComp;
  187. Slider* mySlider;
  188. ...
  189. someKindOfComp->enterModalState (ModalCallbackFunction::forComponent (myCallbackFunction, mySlider));
  190. @endcode
  191. @see ModalComponentManager::Callback
  192. */
  193. template <class ComponentType>
  194. static ModalComponentManager::Callback* forComponent (void (*functionToCall) (int, ComponentType*),
  195. ComponentType* component)
  196. {
  197. return new ComponentCaller1 <ComponentType> (functionToCall, component);
  198. }
  199. //==============================================================================
  200. /** Creates a ModalComponentManager::Callback that will call a static function with a component.
  201. The function that you supply must take three parameters - the first being an int, which is
  202. the result code that was used when the modal component was dismissed, the second being a Component
  203. class, and the third being a custom type (which must be a primitive type or have copy-by-value semantics).
  204. The component will be stored as a WeakReference, so that if it gets deleted before this callback is
  205. invoked, the pointer that is passed into the function will be null.
  206. E.g. @code
  207. static void myCallbackFunction (int modalResult, Slider* mySlider, String customParam)
  208. {
  209. if (modalResult == 1 && mySlider != nullptr) // (must check that mySlider isn't null in case it was deleted..)
  210. mySlider->setName (customParam);
  211. }
  212. Component* someKindOfComp;
  213. Slider* mySlider;
  214. ...
  215. someKindOfComp->enterModalState (ModalCallbackFunction::forComponent (myCallbackFunction, mySlider, String ("hello")));
  216. @endcode
  217. @see ModalComponentManager::Callback
  218. */
  219. template <class ComponentType, typename ParamType>
  220. static ModalComponentManager::Callback* forComponent (void (*functionToCall) (int, ComponentType*, ParamType),
  221. ComponentType* component,
  222. ParamType param)
  223. {
  224. return new ComponentCaller2 <ComponentType, ParamType> (functionToCall, component, param);
  225. }
  226. private:
  227. //==============================================================================
  228. template <typename ParamType>
  229. class FunctionCaller1 : public ModalComponentManager::Callback
  230. {
  231. public:
  232. typedef void (*FunctionType) (int, ParamType);
  233. FunctionCaller1 (FunctionType& f, ParamType& p1)
  234. : function (f), param (p1) {}
  235. void modalStateFinished (int returnValue) { function (returnValue, param); }
  236. private:
  237. const FunctionType function;
  238. ParamType param;
  239. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FunctionCaller1)
  240. };
  241. template <typename ParamType1, typename ParamType2>
  242. class FunctionCaller2 : public ModalComponentManager::Callback
  243. {
  244. public:
  245. typedef void (*FunctionType) (int, ParamType1, ParamType2);
  246. FunctionCaller2 (FunctionType& f, ParamType1& p1, ParamType2& p2)
  247. : function (f), param1 (p1), param2 (p2) {}
  248. void modalStateFinished (int returnValue) { function (returnValue, param1, param2); }
  249. private:
  250. const FunctionType function;
  251. ParamType1 param1;
  252. ParamType2 param2;
  253. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FunctionCaller2)
  254. };
  255. template <typename ComponentType>
  256. class ComponentCaller1 : public ModalComponentManager::Callback
  257. {
  258. public:
  259. typedef void (*FunctionType) (int, ComponentType*);
  260. ComponentCaller1 (FunctionType& f, ComponentType* c)
  261. : function (f), comp (c) {}
  262. void modalStateFinished (int returnValue)
  263. {
  264. function (returnValue, static_cast<ComponentType*> (comp.get()));
  265. }
  266. private:
  267. const FunctionType function;
  268. WeakReference<Component> comp;
  269. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComponentCaller1)
  270. };
  271. template <typename ComponentType, typename ParamType1>
  272. class ComponentCaller2 : public ModalComponentManager::Callback
  273. {
  274. public:
  275. typedef void (*FunctionType) (int, ComponentType*, ParamType1);
  276. ComponentCaller2 (FunctionType& f, ComponentType* c, ParamType1 p1)
  277. : function (f), comp (c), param1 (p1) {}
  278. void modalStateFinished (int returnValue)
  279. {
  280. function (returnValue, static_cast<ComponentType*> (comp.get()), param1);
  281. }
  282. private:
  283. const FunctionType function;
  284. WeakReference<Component> comp;
  285. ParamType1 param1;
  286. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComponentCaller2)
  287. };
  288. ModalCallbackFunction();
  289. ~ModalCallbackFunction();
  290. JUCE_DECLARE_NON_COPYABLE (ModalCallbackFunction)
  291. };
  292. #endif // JUCE_MODALCOMPONENTMANAGER_H_INCLUDED