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.

353 lines
10KB

  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. useNativeTitleBar = shouldUseNativeTitleBar;
  194. recreateDesktopWindow();
  195. sendLookAndFeelChange();
  196. }
  197. }
  198. void TopLevelWindow::recreateDesktopWindow()
  199. {
  200. if (isOnDesktop())
  201. {
  202. Component::addToDesktop (getDesktopWindowStyleFlags());
  203. toFront (true);
  204. }
  205. }
  206. void TopLevelWindow::addToDesktop()
  207. {
  208. shadower = nullptr;
  209. Component::addToDesktop (getDesktopWindowStyleFlags());
  210. setDropShadowEnabled (isDropShadowEnabled()); // force an update to clear away any fake shadows if necessary.
  211. }
  212. void TopLevelWindow::addToDesktop (int windowStyleFlags, void* nativeWindowToAttachTo)
  213. {
  214. /* It's not recommended to change the desktop window flags directly for a TopLevelWindow,
  215. because this class needs to make sure its layout corresponds with settings like whether
  216. it's got a native title bar or not.
  217. If you need custom flags for your window, you can override the getDesktopWindowStyleFlags()
  218. method. If you do this, it's best to call the base class's getDesktopWindowStyleFlags()
  219. method, then add or remove whatever flags are necessary from this value before returning it.
  220. */
  221. jassert ((windowStyleFlags & ~ComponentPeer::windowIsSemiTransparent)
  222. == (getDesktopWindowStyleFlags() & ~ComponentPeer::windowIsSemiTransparent));
  223. Component::addToDesktop (windowStyleFlags, nativeWindowToAttachTo);
  224. if (windowStyleFlags != getDesktopWindowStyleFlags())
  225. sendLookAndFeelChange();
  226. }
  227. //==============================================================================
  228. void TopLevelWindow::centreAroundComponent (Component* c, const int width, const int height)
  229. {
  230. if (c == nullptr)
  231. c = TopLevelWindow::getActiveTopLevelWindow();
  232. if (c == nullptr || c->getBounds().isEmpty())
  233. {
  234. centreWithSize (width, height);
  235. }
  236. else
  237. {
  238. Point<int> targetCentre (c->localPointToGlobal (c->getLocalBounds().getCentre()));
  239. Rectangle<int> parentArea (c->getParentMonitorArea());
  240. if (Component* const parent = getParentComponent())
  241. {
  242. targetCentre = parent->getLocalPoint (nullptr, targetCentre);
  243. parentArea = parent->getLocalBounds();
  244. }
  245. setBounds (Rectangle<int> (targetCentre.x - width / 2,
  246. targetCentre.y - height / 2,
  247. width, height)
  248. .constrainedWithin (parentArea.reduced (12, 12)));
  249. }
  250. }
  251. //==============================================================================
  252. int TopLevelWindow::getNumTopLevelWindows() noexcept
  253. {
  254. return TopLevelWindowManager::getInstance()->windows.size();
  255. }
  256. TopLevelWindow* TopLevelWindow::getTopLevelWindow (const int index) noexcept
  257. {
  258. return TopLevelWindowManager::getInstance()->windows [index];
  259. }
  260. TopLevelWindow* TopLevelWindow::getActiveTopLevelWindow() noexcept
  261. {
  262. TopLevelWindow* best = nullptr;
  263. int bestNumTWLParents = -1;
  264. for (int i = TopLevelWindow::getNumTopLevelWindows(); --i >= 0;)
  265. {
  266. TopLevelWindow* const tlw = TopLevelWindow::getTopLevelWindow (i);
  267. if (tlw->isActiveWindow())
  268. {
  269. int numTWLParents = 0;
  270. for (const Component* c = tlw->getParentComponent(); c != nullptr; c = c->getParentComponent())
  271. if (dynamic_cast<const TopLevelWindow*> (c) != nullptr)
  272. ++numTWLParents;
  273. if (bestNumTWLParents < numTWLParents)
  274. {
  275. best = tlw;
  276. bestNumTWLParents = numTWLParents;
  277. }
  278. }
  279. }
  280. return best;
  281. }