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.

361 lines
10KB

  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. /** Keeps track of the active top level window. */
  19. class TopLevelWindowManager : private Timer,
  20. private DeletedAtShutdown
  21. {
  22. public:
  23. //==============================================================================
  24. TopLevelWindowManager()
  25. : currentActive (nullptr)
  26. {
  27. }
  28. ~TopLevelWindowManager()
  29. {
  30. clearSingletonInstance();
  31. }
  32. juce_DeclareSingleton_SingleThreaded_Minimal (TopLevelWindowManager);
  33. void checkFocusAsync()
  34. {
  35. startTimer (10);
  36. }
  37. void checkFocus()
  38. {
  39. startTimer (jmin (1731, getTimerInterval() * 2));
  40. TopLevelWindow* active = nullptr;
  41. if (Process::isForegroundProcess())
  42. {
  43. active = currentActive;
  44. Component* const c = Component::getCurrentlyFocusedComponent();
  45. TopLevelWindow* tlw = dynamic_cast <TopLevelWindow*> (c);
  46. if (tlw == nullptr && c != nullptr)
  47. tlw = c->findParentComponentOfClass<TopLevelWindow>();
  48. if (tlw != nullptr)
  49. active = tlw;
  50. }
  51. if (active != currentActive)
  52. {
  53. currentActive = active;
  54. for (int i = windows.size(); --i >= 0;)
  55. {
  56. TopLevelWindow* const tlw = windows.getUnchecked (i);
  57. tlw->setWindowActive (isWindowActive (tlw));
  58. i = jmin (i, windows.size() - 1);
  59. }
  60. Desktop::getInstance().triggerFocusCallback();
  61. }
  62. }
  63. bool addWindow (TopLevelWindow* const w)
  64. {
  65. windows.add (w);
  66. checkFocusAsync();
  67. return isWindowActive (w);
  68. }
  69. void removeWindow (TopLevelWindow* const w)
  70. {
  71. checkFocusAsync();
  72. if (currentActive == w)
  73. currentActive = nullptr;
  74. windows.removeFirstMatchingValue (w);
  75. if (windows.size() == 0)
  76. deleteInstance();
  77. }
  78. Array <TopLevelWindow*> windows;
  79. private:
  80. TopLevelWindow* currentActive;
  81. void timerCallback()
  82. {
  83. checkFocus();
  84. }
  85. bool isWindowActive (TopLevelWindow* const tlw) const
  86. {
  87. return (tlw == currentActive
  88. || tlw->isParentOf (currentActive)
  89. || tlw->hasKeyboardFocus (true))
  90. && tlw->isShowing();
  91. }
  92. JUCE_DECLARE_NON_COPYABLE (TopLevelWindowManager);
  93. };
  94. juce_ImplementSingleton_SingleThreaded (TopLevelWindowManager)
  95. void juce_CheckCurrentlyFocusedTopLevelWindow();
  96. void juce_CheckCurrentlyFocusedTopLevelWindow()
  97. {
  98. TopLevelWindowManager* const wm = TopLevelWindowManager::getInstanceWithoutCreating();
  99. if (wm != nullptr)
  100. wm->checkFocusAsync();
  101. }
  102. //==============================================================================
  103. TopLevelWindow::TopLevelWindow (const String& name,
  104. const bool addToDesktop_)
  105. : Component (name),
  106. useDropShadow (true),
  107. useNativeTitleBar (false),
  108. windowIsActive_ (false)
  109. {
  110. setOpaque (true);
  111. if (addToDesktop_)
  112. Component::addToDesktop (TopLevelWindow::getDesktopWindowStyleFlags());
  113. else
  114. setDropShadowEnabled (true);
  115. setWantsKeyboardFocus (true);
  116. setBroughtToFrontOnMouseClick (true);
  117. windowIsActive_ = TopLevelWindowManager::getInstance()->addWindow (this);
  118. }
  119. TopLevelWindow::~TopLevelWindow()
  120. {
  121. shadower = nullptr;
  122. TopLevelWindowManager::getInstance()->removeWindow (this);
  123. }
  124. //==============================================================================
  125. void TopLevelWindow::focusOfChildComponentChanged (FocusChangeType)
  126. {
  127. TopLevelWindowManager* const wm = TopLevelWindowManager::getInstance();
  128. if (hasKeyboardFocus (true))
  129. wm->checkFocus();
  130. else
  131. wm->checkFocusAsync();
  132. }
  133. void TopLevelWindow::setWindowActive (const bool isNowActive)
  134. {
  135. if (windowIsActive_ != isNowActive)
  136. {
  137. windowIsActive_ = isNowActive;
  138. activeWindowStatusChanged();
  139. }
  140. }
  141. void TopLevelWindow::activeWindowStatusChanged()
  142. {
  143. }
  144. bool TopLevelWindow::isUsingNativeTitleBar() const noexcept
  145. {
  146. return useNativeTitleBar && (isOnDesktop() || ! isShowing());
  147. }
  148. void TopLevelWindow::visibilityChanged()
  149. {
  150. if (isShowing()
  151. && (getPeer()->getStyleFlags() & (ComponentPeer::windowIsTemporary
  152. | ComponentPeer::windowIgnoresKeyPresses)) == 0)
  153. {
  154. toFront (true);
  155. }
  156. }
  157. void TopLevelWindow::parentHierarchyChanged()
  158. {
  159. setDropShadowEnabled (useDropShadow);
  160. }
  161. int TopLevelWindow::getDesktopWindowStyleFlags() const
  162. {
  163. int styleFlags = ComponentPeer::windowAppearsOnTaskbar;
  164. if (useDropShadow)
  165. styleFlags |= ComponentPeer::windowHasDropShadow;
  166. if (useNativeTitleBar)
  167. styleFlags |= ComponentPeer::windowHasTitleBar;
  168. return styleFlags;
  169. }
  170. void TopLevelWindow::setDropShadowEnabled (const bool useShadow)
  171. {
  172. useDropShadow = useShadow;
  173. if (isOnDesktop())
  174. {
  175. shadower = nullptr;
  176. Component::addToDesktop (getDesktopWindowStyleFlags());
  177. }
  178. else
  179. {
  180. if (useShadow && isOpaque())
  181. {
  182. if (shadower == nullptr)
  183. {
  184. shadower = getLookAndFeel().createDropShadowerForComponent (this);
  185. if (shadower != nullptr)
  186. shadower->setOwner (this);
  187. }
  188. }
  189. else
  190. {
  191. shadower = nullptr;
  192. }
  193. }
  194. }
  195. void TopLevelWindow::setUsingNativeTitleBar (const bool useNativeTitleBar_)
  196. {
  197. if (useNativeTitleBar != useNativeTitleBar_)
  198. {
  199. useNativeTitleBar = useNativeTitleBar_;
  200. recreateDesktopWindow();
  201. sendLookAndFeelChange();
  202. }
  203. }
  204. void TopLevelWindow::recreateDesktopWindow()
  205. {
  206. if (isOnDesktop())
  207. {
  208. Component::addToDesktop (getDesktopWindowStyleFlags());
  209. toFront (true);
  210. }
  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 (getParentComponent() != nullptr)
  241. {
  242. targetCentre = getParentComponent()->getLocalPoint (nullptr, targetCentre);
  243. parentArea = getParentComponent()->getLocalBounds();
  244. }
  245. parentArea.reduce (12, 12);
  246. setBounds (jlimit (parentArea.getX(), jmax (parentArea.getX(), parentArea.getRight() - width), targetCentre.getX() - width / 2),
  247. jlimit (parentArea.getY(), jmax (parentArea.getY(), parentArea.getBottom() - height), targetCentre.getY() - height / 2),
  248. width, height);
  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 static_cast <TopLevelWindow*> (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. const Component* c = tlw->getParentComponent();
  271. while (c != nullptr)
  272. {
  273. if (dynamic_cast <const TopLevelWindow*> (c) != nullptr)
  274. ++numTWLParents;
  275. c = c->getParentComponent();
  276. }
  277. if (bestNumTWLParents < numTWLParents)
  278. {
  279. best = tlw;
  280. bestNumTWLParents = numTWLParents;
  281. }
  282. }
  283. }
  284. return best;
  285. }