juce_ModalComponentManager.h 13 KB

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