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.

366 lines
15KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_MODALCOMPONENTMANAGER_JUCEHEADER__
  19. #define __JUCE_MODALCOMPONENTMANAGER_JUCEHEADER__
  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 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. /** 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. #if JUCE_MODAL_LOOPS_PERMITTED
  83. /** Runs the event loop until the currently topmost modal component is dismissed, and
  84. returns the exit code for that component.
  85. */
  86. int runEventLoopForCurrentComponent();
  87. #endif
  88. //==============================================================================
  89. juce_DeclareSingleton_SingleThreaded_Minimal (ModalComponentManager);
  90. protected:
  91. /** Creates a ModalComponentManager.
  92. You shouldn't ever call the constructor - it's a singleton, so use ModalComponentManager::getInstance()
  93. */
  94. ModalComponentManager();
  95. /** Destructor. */
  96. ~ModalComponentManager();
  97. /** @internal */
  98. void handleAsyncUpdate();
  99. private:
  100. //==============================================================================
  101. class ModalItem;
  102. class ReturnValueRetriever;
  103. friend class Component;
  104. friend class OwnedArray <ModalItem>;
  105. OwnedArray <ModalItem> stack;
  106. void startModal (Component*, bool autoDelete);
  107. void endModal (Component*, int returnValue);
  108. void endModal (Component*);
  109. JUCE_DECLARE_NON_COPYABLE (ModalComponentManager);
  110. };
  111. //==============================================================================
  112. /**
  113. This class provides some handy utility methods for creating ModalComponentManager::Callback
  114. objects that will invoke a static function with some parameters when a modal component is dismissed.
  115. */
  116. class ModalCallbackFunction
  117. {
  118. public:
  119. //==============================================================================
  120. /** This is a utility function to create a ModalComponentManager::Callback that will
  121. call a static function with a parameter.
  122. The function that you supply must take two parameters - the first being an int, which is
  123. the result code that was used when the modal component was dismissed, and the second
  124. can be a custom type. Note that this custom value will be copied and stored, so it must
  125. be a primitive type or a class that provides copy-by-value semantics.
  126. E.g. @code
  127. static void myCallbackFunction (int modalResult, double customValue)
  128. {
  129. if (modalResult == 1)
  130. doSomethingWith (customValue);
  131. }
  132. Component* someKindOfComp;
  133. ...
  134. someKindOfComp->enterModalState (ModalCallbackFunction::create (myCallbackFunction, 3.0));
  135. @endcode
  136. @see ModalComponentManager::Callback
  137. */
  138. template <typename ParamType>
  139. static ModalComponentManager::Callback* create (void (*functionToCall) (int, ParamType),
  140. ParamType parameterValue)
  141. {
  142. return new FunctionCaller1 <ParamType> (functionToCall, parameterValue);
  143. }
  144. //==============================================================================
  145. /** This is a utility function to create a ModalComponentManager::Callback that will
  146. call a static function with two custom parameters.
  147. The function that you supply must take three parameters - the first being an int, which is
  148. the result code that was used when the modal component was dismissed, and the next two are
  149. your custom types. Note that these custom values will be copied and stored, so they must
  150. be primitive types or classes that provide copy-by-value semantics.
  151. E.g. @code
  152. static void myCallbackFunction (int modalResult, double customValue1, String customValue2)
  153. {
  154. if (modalResult == 1)
  155. doSomethingWith (customValue1, customValue2);
  156. }
  157. Component* someKindOfComp;
  158. ...
  159. someKindOfComp->enterModalState (ModalCallbackFunction::create (myCallbackFunction, 3.0, String ("xyz")));
  160. @endcode
  161. @see ModalComponentManager::Callback
  162. */
  163. template <typename ParamType1, typename ParamType2>
  164. static ModalComponentManager::Callback* withParam (void (*functionToCall) (int, ParamType1, ParamType2),
  165. ParamType1 parameterValue1,
  166. ParamType2 parameterValue2)
  167. {
  168. return new FunctionCaller2 <ParamType1, ParamType2> (functionToCall, parameterValue1, parameterValue2);
  169. }
  170. //==============================================================================
  171. /** This is a utility function to create a ModalComponentManager::Callback that will
  172. call a static function with a component.
  173. The function that you supply must take two parameters - the first being an int, which is
  174. the result code that was used when the modal component was dismissed, and the second
  175. can be a Component class. The component will be stored as a WeakReference, so that if it gets
  176. deleted before this callback is invoked, the pointer that is passed to the function will be null.
  177. E.g. @code
  178. static void myCallbackFunction (int modalResult, Slider* mySlider)
  179. {
  180. if (modalResult == 1 && mySlider != nullptr) // (must check that mySlider isn't null in case it was deleted..)
  181. mySlider->setValue (0.0);
  182. }
  183. Component* someKindOfComp;
  184. Slider* mySlider;
  185. ...
  186. someKindOfComp->enterModalState (ModalCallbackFunction::forComponent (myCallbackFunction, mySlider));
  187. @endcode
  188. @see ModalComponentManager::Callback
  189. */
  190. template <class ComponentType>
  191. static ModalComponentManager::Callback* forComponent (void (*functionToCall) (int, ComponentType*),
  192. ComponentType* component)
  193. {
  194. return new ComponentCaller1 <ComponentType> (functionToCall, component);
  195. }
  196. //==============================================================================
  197. /** Creates a ModalComponentManager::Callback that will call a static function with a component.
  198. The function that you supply must take three parameters - the first being an int, which is
  199. the result code that was used when the modal component was dismissed, the second being a Component
  200. class, and the third being a custom type (which must be a primitive type or have copy-by-value semantics).
  201. The component will be stored as a WeakReference, so that if it gets deleted before this callback is
  202. invoked, the pointer that is passed into the function will be null.
  203. E.g. @code
  204. static void myCallbackFunction (int modalResult, Slider* mySlider, String customParam)
  205. {
  206. if (modalResult == 1 && mySlider != nullptr) // (must check that mySlider isn't null in case it was deleted..)
  207. mySlider->setName (customParam);
  208. }
  209. Component* someKindOfComp;
  210. Slider* mySlider;
  211. ...
  212. someKindOfComp->enterModalState (ModalCallbackFunction::forComponent (myCallbackFunction, mySlider, String ("hello")));
  213. @endcode
  214. @see ModalComponentManager::Callback
  215. */
  216. template <class ComponentType, typename ParamType>
  217. static ModalComponentManager::Callback* forComponent (void (*functionToCall) (int, ComponentType*, ParamType),
  218. ComponentType* component,
  219. ParamType param)
  220. {
  221. return new ComponentCaller2 <ComponentType, ParamType> (functionToCall, component, param);
  222. }
  223. private:
  224. //==============================================================================
  225. template <typename ParamType>
  226. class FunctionCaller1 : public ModalComponentManager::Callback
  227. {
  228. public:
  229. typedef void (*FunctionType) (int, ParamType);
  230. FunctionCaller1 (FunctionType& function_, ParamType& param_)
  231. : function (function_), param (param_) {}
  232. void modalStateFinished (int returnValue) { function (returnValue, param); }
  233. private:
  234. const FunctionType function;
  235. ParamType param;
  236. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FunctionCaller1);
  237. };
  238. template <typename ParamType1, typename ParamType2>
  239. class FunctionCaller2 : public ModalComponentManager::Callback
  240. {
  241. public:
  242. typedef void (*FunctionType) (int, ParamType1, ParamType2);
  243. FunctionCaller2 (FunctionType& function_, ParamType1& param1_, ParamType2& param2_)
  244. : function (function_), param1 (param1_), param2 (param2_) {}
  245. void modalStateFinished (int returnValue) { function (returnValue, param1, param2); }
  246. private:
  247. const FunctionType function;
  248. ParamType1 param1;
  249. ParamType2 param2;
  250. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FunctionCaller2);
  251. };
  252. template <typename ComponentType>
  253. class ComponentCaller1 : public ModalComponentManager::Callback
  254. {
  255. public:
  256. typedef void (*FunctionType) (int, ComponentType*);
  257. ComponentCaller1 (FunctionType& function_, ComponentType* comp_)
  258. : function (function_), comp (comp_) {}
  259. void modalStateFinished (int returnValue)
  260. {
  261. function (returnValue, static_cast <ComponentType*> (comp.get()));
  262. }
  263. private:
  264. const FunctionType function;
  265. WeakReference<Component> comp;
  266. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComponentCaller1);
  267. };
  268. template <typename ComponentType, typename ParamType1>
  269. class ComponentCaller2 : public ModalComponentManager::Callback
  270. {
  271. public:
  272. typedef void (*FunctionType) (int, ComponentType*, ParamType1);
  273. ComponentCaller2 (FunctionType& function_, ComponentType* comp_, ParamType1 param1_)
  274. : function (function_), comp (comp_), param1 (param1_) {}
  275. void modalStateFinished (int returnValue)
  276. {
  277. function (returnValue, static_cast <ComponentType*> (comp.get()), param1);
  278. }
  279. private:
  280. const FunctionType function;
  281. WeakReference<Component> comp;
  282. ParamType1 param1;
  283. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComponentCaller2);
  284. };
  285. ModalCallbackFunction();
  286. ~ModalCallbackFunction();
  287. JUCE_DECLARE_NON_COPYABLE (ModalCallbackFunction);
  288. };
  289. #endif // __JUCE_MODALCOMPONENTMANAGER_JUCEHEADER__