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.cpp 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. class ModalComponentManager::ModalItem : public ComponentMovementWatcher
  18. {
  19. public:
  20. ModalItem (Component* const comp, const bool autoDelete_)
  21. : ComponentMovementWatcher (comp),
  22. component (comp), returnValue (0),
  23. isActive (true), autoDelete (autoDelete_)
  24. {
  25. jassert (comp != nullptr);
  26. }
  27. void componentMovedOrResized (bool, bool) override {}
  28. void componentPeerChanged() override
  29. {
  30. if (! component->isShowing())
  31. cancel();
  32. }
  33. void componentVisibilityChanged() override
  34. {
  35. if (! component->isShowing())
  36. cancel();
  37. }
  38. void componentBeingDeleted (Component& comp) override
  39. {
  40. ComponentMovementWatcher::componentBeingDeleted (comp);
  41. if (component == &comp || comp.isParentOf (component))
  42. {
  43. autoDelete = false;
  44. cancel();
  45. }
  46. }
  47. void cancel()
  48. {
  49. if (isActive)
  50. {
  51. isActive = false;
  52. if (ModalComponentManager* mcm = ModalComponentManager::getInstanceWithoutCreating())
  53. mcm->triggerAsyncUpdate();
  54. }
  55. }
  56. Component* component;
  57. OwnedArray<Callback> callbacks;
  58. int returnValue;
  59. bool isActive, autoDelete;
  60. private:
  61. JUCE_DECLARE_NON_COPYABLE (ModalItem)
  62. };
  63. //==============================================================================
  64. ModalComponentManager::ModalComponentManager()
  65. {
  66. }
  67. ModalComponentManager::~ModalComponentManager()
  68. {
  69. stack.clear();
  70. clearSingletonInstance();
  71. }
  72. juce_ImplementSingleton_SingleThreaded (ModalComponentManager);
  73. //==============================================================================
  74. void ModalComponentManager::startModal (Component* component, bool autoDelete)
  75. {
  76. if (component != nullptr)
  77. stack.add (new ModalItem (component, autoDelete));
  78. }
  79. void ModalComponentManager::attachCallback (Component* component, Callback* callback)
  80. {
  81. if (callback != nullptr)
  82. {
  83. ScopedPointer<Callback> callbackDeleter (callback);
  84. for (int i = stack.size(); --i >= 0;)
  85. {
  86. ModalItem* const item = stack.getUnchecked(i);
  87. if (item->component == component)
  88. {
  89. item->callbacks.add (callback);
  90. callbackDeleter.release();
  91. break;
  92. }
  93. }
  94. }
  95. }
  96. void ModalComponentManager::endModal (Component* component)
  97. {
  98. for (int i = stack.size(); --i >= 0;)
  99. {
  100. ModalItem* const item = stack.getUnchecked(i);
  101. if (item->component == component)
  102. item->cancel();
  103. }
  104. }
  105. void ModalComponentManager::endModal (Component* component, int returnValue)
  106. {
  107. for (int i = stack.size(); --i >= 0;)
  108. {
  109. ModalItem* const item = stack.getUnchecked(i);
  110. if (item->component == component)
  111. {
  112. item->returnValue = returnValue;
  113. item->cancel();
  114. }
  115. }
  116. }
  117. int ModalComponentManager::getNumModalComponents() const
  118. {
  119. int n = 0;
  120. for (int i = 0; i < stack.size(); ++i)
  121. if (stack.getUnchecked(i)->isActive)
  122. ++n;
  123. return n;
  124. }
  125. Component* ModalComponentManager::getModalComponent (const int index) const
  126. {
  127. int n = 0;
  128. for (int i = stack.size(); --i >= 0;)
  129. {
  130. const ModalItem* const item = stack.getUnchecked(i);
  131. if (item->isActive)
  132. if (n++ == index)
  133. return item->component;
  134. }
  135. return nullptr;
  136. }
  137. bool ModalComponentManager::isModal (Component* const comp) const
  138. {
  139. for (int i = stack.size(); --i >= 0;)
  140. {
  141. const ModalItem* const item = stack.getUnchecked(i);
  142. if (item->isActive && item->component == comp)
  143. return true;
  144. }
  145. return false;
  146. }
  147. bool ModalComponentManager::isFrontModalComponent (Component* const comp) const
  148. {
  149. return comp == getModalComponent (0);
  150. }
  151. void ModalComponentManager::handleAsyncUpdate()
  152. {
  153. for (int i = stack.size(); --i >= 0;)
  154. {
  155. const ModalItem* const item = stack.getUnchecked(i);
  156. if (! item->isActive)
  157. {
  158. ScopedPointer<ModalItem> deleter (stack.removeAndReturn (i));
  159. Component::SafePointer<Component> compToDelete (item->autoDelete ? item->component : nullptr);
  160. for (int j = item->callbacks.size(); --j >= 0;)
  161. item->callbacks.getUnchecked(j)->modalStateFinished (item->returnValue);
  162. compToDelete.deleteAndZero();
  163. }
  164. }
  165. }
  166. void ModalComponentManager::bringModalComponentsToFront (bool topOneShouldGrabFocus)
  167. {
  168. ComponentPeer* lastOne = nullptr;
  169. for (int i = 0; i < getNumModalComponents(); ++i)
  170. {
  171. Component* const c = getModalComponent (i);
  172. if (c == nullptr)
  173. break;
  174. ComponentPeer* peer = c->getPeer();
  175. if (peer != nullptr && peer != lastOne)
  176. {
  177. if (lastOne == nullptr)
  178. {
  179. peer->toFront (topOneShouldGrabFocus);
  180. if (topOneShouldGrabFocus)
  181. peer->grabFocus();
  182. }
  183. else
  184. peer->toBehind (lastOne);
  185. lastOne = peer;
  186. }
  187. }
  188. }
  189. #if JUCE_MODAL_LOOPS_PERMITTED
  190. class ModalComponentManager::ReturnValueRetriever : public ModalComponentManager::Callback
  191. {
  192. public:
  193. ReturnValueRetriever (int& v, bool& done) : value (v), finished (done) {}
  194. void modalStateFinished (int returnValue)
  195. {
  196. finished = true;
  197. value = returnValue;
  198. }
  199. private:
  200. int& value;
  201. bool& finished;
  202. JUCE_DECLARE_NON_COPYABLE (ReturnValueRetriever)
  203. };
  204. int ModalComponentManager::runEventLoopForCurrentComponent()
  205. {
  206. // This can only be run from the message thread!
  207. jassert (MessageManager::getInstance()->isThisTheMessageThread());
  208. int returnValue = 0;
  209. if (Component* currentlyModal = getModalComponent (0))
  210. {
  211. FocusRestorer focusRestorer;
  212. bool finished = false;
  213. attachCallback (currentlyModal, new ReturnValueRetriever (returnValue, finished));
  214. JUCE_TRY
  215. {
  216. while (! finished)
  217. {
  218. if (! MessageManager::getInstance()->runDispatchLoopUntil (20))
  219. break;
  220. }
  221. }
  222. JUCE_CATCH_EXCEPTION
  223. }
  224. return returnValue;
  225. }
  226. #endif