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.

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