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) 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. #pragma once
  18. //==============================================================================
  19. /**
  20. Manages the system's stack of modal components.
  21. Normally you'll just use the Component methods to invoke modal states in components,
  22. and won't have to deal with this class directly, but this is the singleton object that's
  23. used internally to manage the stack.
  24. @see Component::enterModalState, Component::exitModalState, Component::isCurrentlyModal,
  25. Component::getCurrentlyModalComponent, Component::isCurrentlyBlockedByAnotherModalComponent
  26. */
  27. class JUCE_API ModalComponentManager : private AsyncUpdater,
  28. private DeletedAtShutdown
  29. {
  30. public:
  31. //==============================================================================
  32. /** Receives callbacks when a modal component is dismissed.
  33. You can register a callback using Component::enterModalState() or
  34. ModalComponentManager::attachCallback().
  35. For some quick ways of creating callback objects, see the ModalCallbackFunction class.
  36. @see ModalCallbackFunction
  37. */
  38. class JUCE_API Callback
  39. {
  40. public:
  41. /** */
  42. Callback() {}
  43. /** Destructor. */
  44. virtual ~Callback() {}
  45. /** Called to indicate that a modal component has been dismissed.
  46. You can register a callback using Component::enterModalState() or
  47. ModalComponentManager::attachCallback().
  48. The returnValue parameter is the value that was passed to Component::exitModalState()
  49. when the component was dismissed.
  50. The callback object will be deleted shortly after this method is called.
  51. */
  52. virtual void modalStateFinished (int returnValue) = 0;
  53. };
  54. //==============================================================================
  55. juce_DeclareSingleton_SingleThreaded_Minimal (ModalComponentManager)
  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 (Component* component) const;
  67. /** Returns true if the specified component is currently the topmost modal component. */
  68. bool isFrontModalComponent (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();
  99. /** @internal */
  100. void handleAsyncUpdate() override;
  101. private:
  102. //==============================================================================
  103. class ModalItem;
  104. class ReturnValueRetriever;
  105. friend class Component;
  106. friend struct ContainerDeletePolicy<ModalItem>;
  107. OwnedArray<ModalItem> stack;
  108. void startModal (Component*, bool autoDelete);
  109. void endModal (Component*, int returnValue);
  110. void endModal (Component*);
  111. JUCE_DECLARE_NON_COPYABLE (ModalComponentManager)
  112. };
  113. //==============================================================================
  114. /**
  115. This class provides some handy utility methods for creating ModalComponentManager::Callback
  116. objects that will invoke a static function with some parameters when a modal component is dismissed.
  117. */
  118. class ModalCallbackFunction
  119. {
  120. public:
  121. //==============================================================================
  122. /** This is a utility function to create a ModalComponentManager::Callback that will
  123. call a static function with a parameter.
  124. The function that you supply must take two parameters - the first being an int, which is
  125. the result code that was used when the modal component was dismissed, and the second
  126. can be a custom type. Note that this custom value will be copied and stored, so it must
  127. be a primitive type or a class that provides copy-by-value semantics.
  128. E.g. @code
  129. static void myCallbackFunction (int modalResult, double customValue)
  130. {
  131. if (modalResult == 1)
  132. doSomethingWith (customValue);
  133. }
  134. Component* someKindOfComp;
  135. ...
  136. someKindOfComp->enterModalState (ModalCallbackFunction::create (myCallbackFunction, 3.0));
  137. @endcode
  138. @see ModalComponentManager::Callback
  139. */
  140. template <typename ParamType>
  141. static ModalComponentManager::Callback* create (void (*functionToCall) (int, ParamType),
  142. ParamType parameterValue)
  143. {
  144. return new FunctionCaller1<ParamType> (functionToCall, parameterValue);
  145. }
  146. #if JUCE_COMPILER_SUPPORTS_LAMBDAS
  147. /** This is a utility function to create a ModalComponentManager::Callback that will
  148. call a lambda function.
  149. The lambda that you supply must take an integer parameter, which is the result code that
  150. was returned when the modal component was dismissed.
  151. @see ModalComponentManager::Callback
  152. */
  153. static ModalComponentManager::Callback* create (std::function<void(int)>);
  154. #endif
  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. };