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.

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