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.

360 lines
11KB

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