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.

220 lines
6.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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. class DropShadower::ShadowWindow : public Component
  21. {
  22. public:
  23. ShadowWindow (Component* comp, const DropShadow& ds)
  24. : target (comp), shadow (ds)
  25. {
  26. setVisible (true);
  27. setInterceptsMouseClicks (false, false);
  28. if (comp->isOnDesktop())
  29. {
  30. setSize (1, 1); // to keep the OS happy by not having zero-size windows
  31. addToDesktop (ComponentPeer::windowIgnoresMouseClicks
  32. | ComponentPeer::windowIsTemporary
  33. | ComponentPeer::windowIgnoresKeyPresses);
  34. }
  35. else if (Component* const parent = comp->getParentComponent())
  36. {
  37. parent->addChildComponent (this);
  38. }
  39. }
  40. void paint (Graphics& g) override
  41. {
  42. if (Component* c = target)
  43. shadow.drawForRectangle (g, getLocalArea (c, c->getLocalBounds()));
  44. }
  45. void resized() override
  46. {
  47. repaint(); // (needed for correct repainting)
  48. }
  49. float getDesktopScaleFactor() const override
  50. {
  51. if (target != nullptr)
  52. return target->getDesktopScaleFactor();
  53. return Component::getDesktopScaleFactor();
  54. }
  55. private:
  56. WeakReference<Component> target;
  57. DropShadow shadow;
  58. JUCE_DECLARE_NON_COPYABLE (ShadowWindow)
  59. };
  60. //==============================================================================
  61. DropShadower::DropShadower (const DropShadow& ds) : shadow (ds) {}
  62. DropShadower::~DropShadower()
  63. {
  64. if (owner != nullptr)
  65. {
  66. owner->removeComponentListener (this);
  67. owner = nullptr;
  68. }
  69. updateParent();
  70. const ScopedValueSetter<bool> setter (reentrant, true);
  71. shadowWindows.clear();
  72. }
  73. void DropShadower::setOwner (Component* componentToFollow)
  74. {
  75. if (componentToFollow != owner)
  76. {
  77. if (owner != nullptr)
  78. owner->removeComponentListener (this);
  79. // (the component can't be null)
  80. jassert (componentToFollow != nullptr);
  81. owner = componentToFollow;
  82. jassert (owner != nullptr);
  83. updateParent();
  84. owner->addComponentListener (this);
  85. updateShadows();
  86. }
  87. }
  88. void DropShadower::updateParent()
  89. {
  90. if (Component* p = lastParentComp)
  91. p->removeComponentListener (this);
  92. lastParentComp = owner != nullptr ? owner->getParentComponent() : nullptr;
  93. if (Component* p = lastParentComp)
  94. p->addComponentListener (this);
  95. }
  96. void DropShadower::componentMovedOrResized (Component& c, bool /*wasMoved*/, bool /*wasResized*/)
  97. {
  98. if (owner == &c)
  99. updateShadows();
  100. }
  101. void DropShadower::componentBroughtToFront (Component& c)
  102. {
  103. if (owner == &c)
  104. updateShadows();
  105. }
  106. void DropShadower::componentChildrenChanged (Component&)
  107. {
  108. updateShadows();
  109. }
  110. void DropShadower::componentParentHierarchyChanged (Component& c)
  111. {
  112. if (owner == &c)
  113. {
  114. updateParent();
  115. updateShadows();
  116. }
  117. }
  118. void DropShadower::componentVisibilityChanged (Component& c)
  119. {
  120. if (owner == &c)
  121. updateShadows();
  122. }
  123. void DropShadower::updateShadows()
  124. {
  125. if (reentrant)
  126. return;
  127. const ScopedValueSetter<bool> setter (reentrant, true);
  128. if (owner == nullptr)
  129. {
  130. shadowWindows.clear();
  131. return;
  132. }
  133. if (owner->isShowing()
  134. && owner->getWidth() > 0 && owner->getHeight() > 0
  135. && (Desktop::canUseSemiTransparentWindows() || owner->getParentComponent() != nullptr))
  136. {
  137. while (shadowWindows.size() < 4)
  138. shadowWindows.add (new ShadowWindow (owner, shadow));
  139. const int shadowEdge = jmax (shadow.offset.x, shadow.offset.y) + shadow.radius;
  140. const int x = owner->getX();
  141. const int y = owner->getY() - shadowEdge;
  142. const int w = owner->getWidth();
  143. const int h = owner->getHeight() + shadowEdge + shadowEdge;
  144. for (int i = 4; --i >= 0;)
  145. {
  146. // there seem to be rare situations where the dropshadower may be deleted by
  147. // callbacks during this loop, so use a weak ref to watch out for this..
  148. WeakReference<Component> sw (shadowWindows[i]);
  149. if (sw != nullptr)
  150. {
  151. sw->setAlwaysOnTop (owner->isAlwaysOnTop());
  152. if (sw == nullptr)
  153. return;
  154. switch (i)
  155. {
  156. case 0: sw->setBounds (x - shadowEdge, y, shadowEdge, h); break;
  157. case 1: sw->setBounds (x + w, y, shadowEdge, h); break;
  158. case 2: sw->setBounds (x, y, w, shadowEdge); break;
  159. case 3: sw->setBounds (x, owner->getBottom(), w, shadowEdge); break;
  160. default: break;
  161. }
  162. if (sw == nullptr)
  163. return;
  164. sw->toBehind (i == 3 ? owner.get() : shadowWindows.getUnchecked (i + 1));
  165. }
  166. }
  167. }
  168. else
  169. {
  170. shadowWindows.clear();
  171. }
  172. }
  173. } // namespace juce