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.

juce_TopLevelWindow.cpp 10KB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. TopLevelWindowManager() : currentActive (nullptr)
  23. {
  24. }
  25. ~TopLevelWindowManager()
  26. {
  27. clearSingletonInstance();
  28. }
  29. juce_DeclareSingleton_SingleThreaded_Minimal (TopLevelWindowManager);
  30. void checkFocusAsync()
  31. {
  32. startTimer (10);
  33. }
  34. void checkFocus()
  35. {
  36. startTimer (jmin (1731, getTimerInterval() * 2));
  37. TopLevelWindow* newActive = findCurrentlyActiveWindow();
  38. if (newActive != currentActive)
  39. {
  40. currentActive = newActive;
  41. for (int i = windows.size(); --i >= 0;)
  42. if (TopLevelWindow* tlw = windows[i])
  43. tlw->setWindowActive (isWindowActive (tlw));
  44. Desktop::getInstance().triggerFocusCallback();
  45. }
  46. }
  47. bool addWindow (TopLevelWindow* const w)
  48. {
  49. windows.add (w);
  50. checkFocusAsync();
  51. return isWindowActive (w);
  52. }
  53. void removeWindow (TopLevelWindow* const w)
  54. {
  55. checkFocusAsync();
  56. if (currentActive == w)
  57. currentActive = nullptr;
  58. windows.removeFirstMatchingValue (w);
  59. if (windows.size() == 0)
  60. deleteInstance();
  61. }
  62. Array<TopLevelWindow*> windows;
  63. private:
  64. TopLevelWindow* currentActive;
  65. void timerCallback() override
  66. {
  67. checkFocus();
  68. }
  69. bool isWindowActive (TopLevelWindow* const tlw) const
  70. {
  71. return (tlw == currentActive
  72. || tlw->isParentOf (currentActive)
  73. || tlw->hasKeyboardFocus (true))
  74. && tlw->isShowing();
  75. }
  76. TopLevelWindow* findCurrentlyActiveWindow() const
  77. {
  78. if (Process::isForegroundProcess())
  79. {
  80. Component* const focusedComp = Component::getCurrentlyFocusedComponent();
  81. TopLevelWindow* w = dynamic_cast<TopLevelWindow*> (focusedComp);
  82. if (w == nullptr && focusedComp != nullptr)
  83. w = focusedComp->findParentComponentOfClass<TopLevelWindow>();
  84. if (w == nullptr)
  85. w = currentActive;
  86. if (w != nullptr && w->isShowing())
  87. return w;
  88. }
  89. return nullptr;
  90. }
  91. JUCE_DECLARE_NON_COPYABLE (TopLevelWindowManager)
  92. };
  93. juce_ImplementSingleton_SingleThreaded (TopLevelWindowManager)
  94. void juce_checkCurrentlyFocusedTopLevelWindow();
  95. void juce_checkCurrentlyFocusedTopLevelWindow()
  96. {
  97. if (TopLevelWindowManager* const wm = TopLevelWindowManager::getInstanceWithoutCreating())
  98. wm->checkFocusAsync();
  99. }
  100. //==============================================================================
  101. TopLevelWindow::TopLevelWindow (const String& name, const bool shouldAddToDesktop)
  102. : Component (name),
  103. useDropShadow (true),
  104. useNativeTitleBar (false),
  105. isCurrentlyActive (false)
  106. {
  107. setOpaque (true);
  108. if (shouldAddToDesktop)
  109. Component::addToDesktop (TopLevelWindow::getDesktopWindowStyleFlags());
  110. else
  111. setDropShadowEnabled (true);
  112. setWantsKeyboardFocus (true);
  113. setBroughtToFrontOnMouseClick (true);
  114. isCurrentlyActive = TopLevelWindowManager::getInstance()->addWindow (this);
  115. }
  116. TopLevelWindow::~TopLevelWindow()
  117. {
  118. shadower = nullptr;
  119. TopLevelWindowManager::getInstance()->removeWindow (this);
  120. }
  121. //==============================================================================
  122. void TopLevelWindow::focusOfChildComponentChanged (FocusChangeType)
  123. {
  124. TopLevelWindowManager* const wm = TopLevelWindowManager::getInstance();
  125. if (hasKeyboardFocus (true))
  126. wm->checkFocus();
  127. else
  128. wm->checkFocusAsync();
  129. }
  130. void TopLevelWindow::setWindowActive (const bool isNowActive)
  131. {
  132. if (isCurrentlyActive != isNowActive)
  133. {
  134. isCurrentlyActive = isNowActive;
  135. activeWindowStatusChanged();
  136. }
  137. }
  138. void TopLevelWindow::activeWindowStatusChanged()
  139. {
  140. }
  141. bool TopLevelWindow::isUsingNativeTitleBar() const noexcept
  142. {
  143. return useNativeTitleBar && (isOnDesktop() || ! isShowing());
  144. }
  145. void TopLevelWindow::visibilityChanged()
  146. {
  147. if (isShowing())
  148. if (ComponentPeer* p = getPeer())
  149. if ((p->getStyleFlags() & (ComponentPeer::windowIsTemporary
  150. | ComponentPeer::windowIgnoresKeyPresses)) == 0)
  151. toFront (true);
  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. FocusRestorer focusRestorer;
  194. useNativeTitleBar = shouldUseNativeTitleBar;
  195. recreateDesktopWindow();
  196. sendLookAndFeelChange();
  197. }
  198. }
  199. void TopLevelWindow::recreateDesktopWindow()
  200. {
  201. if (isOnDesktop())
  202. {
  203. Component::addToDesktop (getDesktopWindowStyleFlags());
  204. toFront (true);
  205. }
  206. }
  207. void TopLevelWindow::addToDesktop()
  208. {
  209. shadower = nullptr;
  210. Component::addToDesktop (getDesktopWindowStyleFlags());
  211. setDropShadowEnabled (isDropShadowEnabled()); // force an update to clear away any fake shadows if necessary.
  212. }
  213. void TopLevelWindow::addToDesktop (int windowStyleFlags, void* nativeWindowToAttachTo)
  214. {
  215. /* It's not recommended to change the desktop window flags directly for a TopLevelWindow,
  216. because this class needs to make sure its layout corresponds with settings like whether
  217. it's got a native title bar or not.
  218. If you need custom flags for your window, you can override the getDesktopWindowStyleFlags()
  219. method. If you do this, it's best to call the base class's getDesktopWindowStyleFlags()
  220. method, then add or remove whatever flags are necessary from this value before returning it.
  221. */
  222. jassert ((windowStyleFlags & ~ComponentPeer::windowIsSemiTransparent)
  223. == (getDesktopWindowStyleFlags() & ~ComponentPeer::windowIsSemiTransparent));
  224. Component::addToDesktop (windowStyleFlags, nativeWindowToAttachTo);
  225. if (windowStyleFlags != getDesktopWindowStyleFlags())
  226. sendLookAndFeelChange();
  227. }
  228. //==============================================================================
  229. void TopLevelWindow::centreAroundComponent (Component* c, const int width, const int height)
  230. {
  231. if (c == nullptr)
  232. c = TopLevelWindow::getActiveTopLevelWindow();
  233. if (c == nullptr || c->getBounds().isEmpty())
  234. {
  235. centreWithSize (width, height);
  236. }
  237. else
  238. {
  239. Point<int> targetCentre (c->localPointToGlobal (c->getLocalBounds().getCentre()));
  240. Rectangle<int> parentArea (c->getParentMonitorArea());
  241. if (Component* const parent = getParentComponent())
  242. {
  243. targetCentre = parent->getLocalPoint (nullptr, targetCentre);
  244. parentArea = parent->getLocalBounds();
  245. }
  246. setBounds (Rectangle<int> (targetCentre.x - width / 2,
  247. targetCentre.y - height / 2,
  248. width, height)
  249. .constrainedWithin (parentArea.reduced (12, 12)));
  250. }
  251. }
  252. //==============================================================================
  253. int TopLevelWindow::getNumTopLevelWindows() noexcept
  254. {
  255. return TopLevelWindowManager::getInstance()->windows.size();
  256. }
  257. TopLevelWindow* TopLevelWindow::getTopLevelWindow (const int index) noexcept
  258. {
  259. return TopLevelWindowManager::getInstance()->windows [index];
  260. }
  261. TopLevelWindow* TopLevelWindow::getActiveTopLevelWindow() noexcept
  262. {
  263. TopLevelWindow* best = nullptr;
  264. int bestNumTWLParents = -1;
  265. for (int i = TopLevelWindow::getNumTopLevelWindows(); --i >= 0;)
  266. {
  267. TopLevelWindow* const tlw = TopLevelWindow::getTopLevelWindow (i);
  268. if (tlw->isActiveWindow())
  269. {
  270. int numTWLParents = 0;
  271. for (const Component* c = tlw->getParentComponent(); c != nullptr; c = c->getParentComponent())
  272. if (dynamic_cast<const TopLevelWindow*> (c) != nullptr)
  273. ++numTWLParents;
  274. if (bestNumTWLParents < numTWLParents)
  275. {
  276. best = tlw;
  277. bestNumTWLParents = numTWLParents;
  278. }
  279. }
  280. }
  281. return best;
  282. }