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.

juce_ModalComponentManager.h 15KB

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