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.

256 lines
7.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. TopLevelWindow::TopLevelWindow (const String& name, const bool shouldAddToDesktop)
  22. : Component (name)
  23. {
  24. setTitle (name);
  25. setOpaque (true);
  26. if (shouldAddToDesktop)
  27. Component::addToDesktop (TopLevelWindow::getDesktopWindowStyleFlags());
  28. else
  29. setDropShadowEnabled (true);
  30. setWantsKeyboardFocus (true);
  31. setBroughtToFrontOnMouseClick (true);
  32. isCurrentlyActive = detail::TopLevelWindowManager::getInstance()->addWindow (this);
  33. }
  34. TopLevelWindow::~TopLevelWindow()
  35. {
  36. shadower = nullptr;
  37. detail::TopLevelWindowManager::getInstance()->removeWindow (this);
  38. }
  39. //==============================================================================
  40. void TopLevelWindow::focusOfChildComponentChanged (FocusChangeType)
  41. {
  42. auto* wm = detail::TopLevelWindowManager::getInstance();
  43. if (hasKeyboardFocus (true))
  44. wm->checkFocus();
  45. else
  46. wm->checkFocusAsync();
  47. }
  48. void TopLevelWindow::setWindowActive (const bool isNowActive)
  49. {
  50. if (isCurrentlyActive != isNowActive)
  51. {
  52. isCurrentlyActive = isNowActive;
  53. activeWindowStatusChanged();
  54. }
  55. }
  56. void TopLevelWindow::activeWindowStatusChanged()
  57. {
  58. }
  59. bool TopLevelWindow::isUsingNativeTitleBar() const noexcept
  60. {
  61. return useNativeTitleBar && (isOnDesktop() || ! isShowing());
  62. }
  63. void TopLevelWindow::visibilityChanged()
  64. {
  65. if (isShowing())
  66. if (auto* p = getPeer())
  67. if ((p->getStyleFlags() & (ComponentPeer::windowIsTemporary
  68. | ComponentPeer::windowIgnoresKeyPresses)) == 0)
  69. toFront (true);
  70. }
  71. void TopLevelWindow::parentHierarchyChanged()
  72. {
  73. setDropShadowEnabled (useDropShadow);
  74. }
  75. int TopLevelWindow::getDesktopWindowStyleFlags() const
  76. {
  77. int styleFlags = ComponentPeer::windowAppearsOnTaskbar;
  78. if (useDropShadow) styleFlags |= ComponentPeer::windowHasDropShadow;
  79. if (useNativeTitleBar) styleFlags |= ComponentPeer::windowHasTitleBar;
  80. return styleFlags;
  81. }
  82. void TopLevelWindow::setDropShadowEnabled (const bool useShadow)
  83. {
  84. useDropShadow = useShadow;
  85. if (isOnDesktop())
  86. {
  87. shadower = nullptr;
  88. Component::addToDesktop (getDesktopWindowStyleFlags());
  89. }
  90. else
  91. {
  92. if (useShadow && isOpaque())
  93. {
  94. if (shadower == nullptr)
  95. {
  96. shadower = getLookAndFeel().createDropShadowerForComponent (*this);
  97. if (shadower != nullptr)
  98. shadower->setOwner (this);
  99. }
  100. }
  101. else
  102. {
  103. shadower = nullptr;
  104. }
  105. }
  106. }
  107. void TopLevelWindow::setUsingNativeTitleBar (const bool shouldUseNativeTitleBar)
  108. {
  109. if (useNativeTitleBar != shouldUseNativeTitleBar)
  110. {
  111. detail::FocusRestorer focusRestorer;
  112. useNativeTitleBar = shouldUseNativeTitleBar;
  113. recreateDesktopWindow();
  114. sendLookAndFeelChange();
  115. }
  116. }
  117. void TopLevelWindow::recreateDesktopWindow()
  118. {
  119. if (isOnDesktop())
  120. {
  121. Component::addToDesktop (getDesktopWindowStyleFlags());
  122. toFront (true);
  123. }
  124. }
  125. void TopLevelWindow::addToDesktop()
  126. {
  127. shadower = nullptr;
  128. Component::addToDesktop (getDesktopWindowStyleFlags());
  129. setDropShadowEnabled (isDropShadowEnabled()); // force an update to clear away any fake shadows if necessary.
  130. }
  131. void TopLevelWindow::addToDesktop (int windowStyleFlags, void* nativeWindowToAttachTo)
  132. {
  133. /* It's not recommended to change the desktop window flags directly for a TopLevelWindow,
  134. because this class needs to make sure its layout corresponds with settings like whether
  135. it's got a native title bar or not.
  136. If you need custom flags for your window, you can override the getDesktopWindowStyleFlags()
  137. method. If you do this, it's best to call the base class's getDesktopWindowStyleFlags()
  138. method, then add or remove whatever flags are necessary from this value before returning it.
  139. */
  140. jassert ((windowStyleFlags & ~ComponentPeer::windowIsSemiTransparent)
  141. == (getDesktopWindowStyleFlags() & ~ComponentPeer::windowIsSemiTransparent));
  142. Component::addToDesktop (windowStyleFlags, nativeWindowToAttachTo);
  143. if (windowStyleFlags != getDesktopWindowStyleFlags())
  144. sendLookAndFeelChange();
  145. }
  146. std::unique_ptr<AccessibilityHandler> TopLevelWindow::createAccessibilityHandler()
  147. {
  148. return std::make_unique<AccessibilityHandler> (*this, AccessibilityRole::window);
  149. }
  150. //==============================================================================
  151. void TopLevelWindow::centreAroundComponent (Component* c, const int width, const int height)
  152. {
  153. if (c == nullptr)
  154. c = TopLevelWindow::getActiveTopLevelWindow();
  155. if (c == nullptr || c->getBounds().isEmpty())
  156. {
  157. centreWithSize (width, height);
  158. }
  159. else
  160. {
  161. const auto scale = getDesktopScaleFactor() / Desktop::getInstance().getGlobalScaleFactor();
  162. const auto [targetCentre, parentArea] = [&]
  163. {
  164. const auto globalTargetCentre = c->localPointToGlobal (c->getLocalBounds().getCentre()) / scale;
  165. if (auto* parent = getParentComponent())
  166. return std::make_pair (parent->getLocalPoint (nullptr, globalTargetCentre), parent->getLocalBounds());
  167. return std::make_pair (globalTargetCentre, c->getParentMonitorArea() / scale);
  168. }();
  169. setBounds (Rectangle<int> (targetCentre.x - width / 2,
  170. targetCentre.y - height / 2,
  171. width, height)
  172. .constrainedWithin (parentArea.reduced (12, 12)));
  173. }
  174. }
  175. //==============================================================================
  176. int TopLevelWindow::getNumTopLevelWindows() noexcept
  177. {
  178. return detail::TopLevelWindowManager::getInstance()->windows.size();
  179. }
  180. TopLevelWindow* TopLevelWindow::getTopLevelWindow (const int index) noexcept
  181. {
  182. return detail::TopLevelWindowManager::getInstance()->windows [index];
  183. }
  184. TopLevelWindow* TopLevelWindow::getActiveTopLevelWindow() noexcept
  185. {
  186. TopLevelWindow* best = nullptr;
  187. int bestNumTWLParents = -1;
  188. for (int i = TopLevelWindow::getNumTopLevelWindows(); --i >= 0;)
  189. {
  190. auto* tlw = TopLevelWindow::getTopLevelWindow (i);
  191. if (tlw->isActiveWindow())
  192. {
  193. int numTWLParents = 0;
  194. for (auto* c = tlw->getParentComponent(); c != nullptr; c = c->getParentComponent())
  195. if (dynamic_cast<const TopLevelWindow*> (c) != nullptr)
  196. ++numTWLParents;
  197. if (bestNumTWLParents < numTWLParents)
  198. {
  199. best = tlw;
  200. bestNumTWLParents = numTWLParents;
  201. }
  202. }
  203. }
  204. return best;
  205. }
  206. } // namespace juce