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_TopLevelWindow.cpp 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. /** Keeps track of the active top level window. */
  18. class TopLevelWindowManager : private Timer,
  19. private DeletedAtShutdown
  20. {
  21. public:
  22. //==============================================================================
  23. TopLevelWindowManager() : currentActive (nullptr)
  24. {
  25. }
  26. ~TopLevelWindowManager()
  27. {
  28. clearSingletonInstance();
  29. }
  30. juce_DeclareSingleton_SingleThreaded_Minimal (TopLevelWindowManager);
  31. void checkFocusAsync()
  32. {
  33. startTimer (10);
  34. }
  35. void checkFocus()
  36. {
  37. startTimer (jmin (1731, getTimerInterval() * 2));
  38. TopLevelWindow* active = nullptr;
  39. if (Process::isForegroundProcess())
  40. {
  41. active = currentActive;
  42. Component* const c = Component::getCurrentlyFocusedComponent();
  43. TopLevelWindow* tlw = dynamic_cast <TopLevelWindow*> (c);
  44. if (tlw == nullptr && c != nullptr)
  45. tlw = c->findParentComponentOfClass<TopLevelWindow>();
  46. if (tlw != nullptr)
  47. active = tlw;
  48. }
  49. if (active != currentActive)
  50. {
  51. currentActive = active;
  52. for (int i = windows.size(); --i >= 0;)
  53. {
  54. TopLevelWindow* const tlw = windows.getUnchecked (i);
  55. tlw->setWindowActive (isWindowActive (tlw));
  56. i = jmin (i, windows.size() - 1);
  57. }
  58. Desktop::getInstance().triggerFocusCallback();
  59. }
  60. }
  61. bool addWindow (TopLevelWindow* const w)
  62. {
  63. windows.add (w);
  64. checkFocusAsync();
  65. return isWindowActive (w);
  66. }
  67. void removeWindow (TopLevelWindow* const w)
  68. {
  69. checkFocusAsync();
  70. if (currentActive == w)
  71. currentActive = nullptr;
  72. windows.removeFirstMatchingValue (w);
  73. if (windows.size() == 0)
  74. deleteInstance();
  75. }
  76. Array <TopLevelWindow*> windows;
  77. private:
  78. TopLevelWindow* currentActive;
  79. void timerCallback() override
  80. {
  81. checkFocus();
  82. }
  83. bool isWindowActive (TopLevelWindow* const tlw) const
  84. {
  85. return (tlw == currentActive
  86. || tlw->isParentOf (currentActive)
  87. || tlw->hasKeyboardFocus (true))
  88. && tlw->isShowing();
  89. }
  90. JUCE_DECLARE_NON_COPYABLE (TopLevelWindowManager)
  91. };
  92. juce_ImplementSingleton_SingleThreaded (TopLevelWindowManager)
  93. void juce_checkCurrentlyFocusedTopLevelWindow();
  94. void juce_checkCurrentlyFocusedTopLevelWindow()
  95. {
  96. if (TopLevelWindowManager* const wm = TopLevelWindowManager::getInstanceWithoutCreating())
  97. wm->checkFocusAsync();
  98. }
  99. //==============================================================================
  100. TopLevelWindow::TopLevelWindow (const String& name, const bool shouldAddToDesktop)
  101. : Component (name),
  102. useDropShadow (true),
  103. useNativeTitleBar (false),
  104. isCurrentlyActive (false)
  105. {
  106. setOpaque (true);
  107. if (shouldAddToDesktop)
  108. Component::addToDesktop (TopLevelWindow::getDesktopWindowStyleFlags());
  109. else
  110. setDropShadowEnabled (true);
  111. setWantsKeyboardFocus (true);
  112. setBroughtToFrontOnMouseClick (true);
  113. isCurrentlyActive = TopLevelWindowManager::getInstance()->addWindow (this);
  114. }
  115. TopLevelWindow::~TopLevelWindow()
  116. {
  117. shadower = nullptr;
  118. TopLevelWindowManager::getInstance()->removeWindow (this);
  119. }
  120. //==============================================================================
  121. void TopLevelWindow::focusOfChildComponentChanged (FocusChangeType)
  122. {
  123. TopLevelWindowManager* const wm = TopLevelWindowManager::getInstance();
  124. if (hasKeyboardFocus (true))
  125. wm->checkFocus();
  126. else
  127. wm->checkFocusAsync();
  128. }
  129. void TopLevelWindow::setWindowActive (const bool isNowActive)
  130. {
  131. if (isCurrentlyActive != isNowActive)
  132. {
  133. isCurrentlyActive = isNowActive;
  134. activeWindowStatusChanged();
  135. }
  136. }
  137. void TopLevelWindow::activeWindowStatusChanged()
  138. {
  139. }
  140. bool TopLevelWindow::isUsingNativeTitleBar() const noexcept
  141. {
  142. return useNativeTitleBar && (isOnDesktop() || ! isShowing());
  143. }
  144. void TopLevelWindow::visibilityChanged()
  145. {
  146. if (isShowing()
  147. && (getPeer()->getStyleFlags() & (ComponentPeer::windowIsTemporary
  148. | ComponentPeer::windowIgnoresKeyPresses)) == 0)
  149. {
  150. toFront (true);
  151. }
  152. }
  153. void TopLevelWindow::parentHierarchyChanged()
  154. {
  155. setDropShadowEnabled (useDropShadow);
  156. }
  157. int TopLevelWindow::getDesktopWindowStyleFlags() const
  158. {
  159. int styleFlags = ComponentPeer::windowAppearsOnTaskbar;
  160. if (useDropShadow) styleFlags |= ComponentPeer::windowHasDropShadow;
  161. if (useNativeTitleBar) styleFlags |= ComponentPeer::windowHasTitleBar;
  162. return styleFlags;
  163. }
  164. void TopLevelWindow::setDropShadowEnabled (const bool useShadow)
  165. {
  166. useDropShadow = useShadow;
  167. if (isOnDesktop())
  168. {
  169. shadower = nullptr;
  170. Component::addToDesktop (getDesktopWindowStyleFlags());
  171. }
  172. else
  173. {
  174. if (useShadow && isOpaque())
  175. {
  176. if (shadower == nullptr)
  177. {
  178. shadower = getLookAndFeel().createDropShadowerForComponent (this);
  179. if (shadower != nullptr)
  180. shadower->setOwner (this);
  181. }
  182. }
  183. else
  184. {
  185. shadower = nullptr;
  186. }
  187. }
  188. }
  189. void TopLevelWindow::setUsingNativeTitleBar (const bool shouldUseNativeTitleBar)
  190. {
  191. if (useNativeTitleBar != shouldUseNativeTitleBar)
  192. {
  193. FocusRestorer focusRestorer;
  194. useNativeTitleBar = shouldUseNativeTitleBar;
  195. recreateDesktopWindow();
  196. sendLookAndFeelChange();
  197. }
  198. }
  199. void TopLevelWindow::recreateDesktopWindow()
  200. {
  201. if (isOnDesktop())
  202. {
  203. Component::addToDesktop (getDesktopWindowStyleFlags());
  204. toFront (true);
  205. }
  206. }
  207. void TopLevelWindow::addToDesktop()
  208. {
  209. shadower = nullptr;
  210. Component::addToDesktop (getDesktopWindowStyleFlags());
  211. setDropShadowEnabled (isDropShadowEnabled()); // force an update to clear away any fake shadows if necessary.
  212. }
  213. void TopLevelWindow::addToDesktop (int windowStyleFlags, void* nativeWindowToAttachTo)
  214. {
  215. /* It's not recommended to change the desktop window flags directly for a TopLevelWindow,
  216. because this class needs to make sure its layout corresponds with settings like whether
  217. it's got a native title bar or not.
  218. If you need custom flags for your window, you can override the getDesktopWindowStyleFlags()
  219. method. If you do this, it's best to call the base class's getDesktopWindowStyleFlags()
  220. method, then add or remove whatever flags are necessary from this value before returning it.
  221. */
  222. jassert ((windowStyleFlags & ~ComponentPeer::windowIsSemiTransparent)
  223. == (getDesktopWindowStyleFlags() & ~ComponentPeer::windowIsSemiTransparent));
  224. Component::addToDesktop (windowStyleFlags, nativeWindowToAttachTo);
  225. if (windowStyleFlags != getDesktopWindowStyleFlags())
  226. sendLookAndFeelChange();
  227. }
  228. //==============================================================================
  229. void TopLevelWindow::centreAroundComponent (Component* c, const int width, const int height)
  230. {
  231. if (c == nullptr)
  232. c = TopLevelWindow::getActiveTopLevelWindow();
  233. if (c == nullptr || c->getBounds().isEmpty())
  234. {
  235. centreWithSize (width, height);
  236. }
  237. else
  238. {
  239. Point<int> targetCentre (c->localPointToGlobal (c->getLocalBounds().getCentre()));
  240. Rectangle<int> parentArea (c->getParentMonitorArea());
  241. if (Component* const parent = getParentComponent())
  242. {
  243. targetCentre = parent->getLocalPoint (nullptr, targetCentre);
  244. parentArea = parent->getLocalBounds();
  245. }
  246. setBounds (Rectangle<int> (targetCentre.x - width / 2,
  247. targetCentre.y - height / 2,
  248. width, height)
  249. .constrainedWithin (parentArea.reduced (12, 12)));
  250. }
  251. }
  252. //==============================================================================
  253. int TopLevelWindow::getNumTopLevelWindows() noexcept
  254. {
  255. return TopLevelWindowManager::getInstance()->windows.size();
  256. }
  257. TopLevelWindow* TopLevelWindow::getTopLevelWindow (const int index) noexcept
  258. {
  259. return TopLevelWindowManager::getInstance()->windows [index];
  260. }
  261. TopLevelWindow* TopLevelWindow::getActiveTopLevelWindow() noexcept
  262. {
  263. TopLevelWindow* best = nullptr;
  264. int bestNumTWLParents = -1;
  265. for (int i = TopLevelWindow::getNumTopLevelWindows(); --i >= 0;)
  266. {
  267. TopLevelWindow* const tlw = TopLevelWindow::getTopLevelWindow (i);
  268. if (tlw->isActiveWindow())
  269. {
  270. int numTWLParents = 0;
  271. for (const Component* c = tlw->getParentComponent(); c != nullptr; c = c->getParentComponent())
  272. if (dynamic_cast<const TopLevelWindow*> (c) != nullptr)
  273. ++numTWLParents;
  274. if (bestNumTWLParents < numTWLParents)
  275. {
  276. best = tlw;
  277. bestNumTWLParents = numTWLParents;
  278. }
  279. }
  280. }
  281. return best;
  282. }