The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

373 lines
15KB

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