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.

223 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)
  62. : owner (nullptr), shadow (ds), reentrant (false)
  63. {
  64. }
  65. DropShadower::~DropShadower()
  66. {
  67. if (owner != nullptr)
  68. {
  69. owner->removeComponentListener (this);
  70. owner = nullptr;
  71. }
  72. updateParent();
  73. reentrant = true;
  74. shadowWindows.clear();
  75. }
  76. void DropShadower::setOwner (Component* componentToFollow)
  77. {
  78. if (componentToFollow != owner)
  79. {
  80. if (owner != nullptr)
  81. owner->removeComponentListener (this);
  82. // (the component can't be null)
  83. jassert (componentToFollow != nullptr);
  84. owner = componentToFollow;
  85. jassert (owner != nullptr);
  86. updateParent();
  87. owner->addComponentListener (this);
  88. updateShadows();
  89. }
  90. }
  91. void DropShadower::updateParent()
  92. {
  93. if (Component* p = lastParentComp)
  94. p->removeComponentListener (this);
  95. lastParentComp = owner != nullptr ? owner->getParentComponent() : nullptr;
  96. if (Component* p = lastParentComp)
  97. p->addComponentListener (this);
  98. }
  99. void DropShadower::componentMovedOrResized (Component& c, bool /*wasMoved*/, bool /*wasResized*/)
  100. {
  101. if (owner == &c)
  102. updateShadows();
  103. }
  104. void DropShadower::componentBroughtToFront (Component& c)
  105. {
  106. if (owner == &c)
  107. updateShadows();
  108. }
  109. void DropShadower::componentChildrenChanged (Component&)
  110. {
  111. updateShadows();
  112. }
  113. void DropShadower::componentParentHierarchyChanged (Component& c)
  114. {
  115. if (owner == &c)
  116. {
  117. updateParent();
  118. updateShadows();
  119. }
  120. }
  121. void DropShadower::componentVisibilityChanged (Component& c)
  122. {
  123. if (owner == &c)
  124. updateShadows();
  125. }
  126. void DropShadower::updateShadows()
  127. {
  128. if (reentrant)
  129. return;
  130. const ScopedValueSetter<bool> setter (reentrant, true, false);
  131. if (owner == nullptr)
  132. {
  133. shadowWindows.clear();
  134. return;
  135. }
  136. if (owner->isShowing()
  137. && owner->getWidth() > 0 && owner->getHeight() > 0
  138. && (Desktop::canUseSemiTransparentWindows() || owner->getParentComponent() != nullptr))
  139. {
  140. while (shadowWindows.size() < 4)
  141. shadowWindows.add (new ShadowWindow (owner, shadow));
  142. const int shadowEdge = jmax (shadow.offset.x, shadow.offset.y) + shadow.radius;
  143. const int x = owner->getX();
  144. const int y = owner->getY() - shadowEdge;
  145. const int w = owner->getWidth();
  146. const int h = owner->getHeight() + shadowEdge + shadowEdge;
  147. for (int i = 4; --i >= 0;)
  148. {
  149. // there seem to be rare situations where the dropshadower may be deleted by
  150. // callbacks during this loop, so use a weak ref to watch out for this..
  151. WeakReference<Component> sw (shadowWindows[i]);
  152. if (sw != nullptr)
  153. {
  154. sw->setAlwaysOnTop (owner->isAlwaysOnTop());
  155. if (sw == nullptr)
  156. return;
  157. switch (i)
  158. {
  159. case 0: sw->setBounds (x - shadowEdge, y, shadowEdge, h); break;
  160. case 1: sw->setBounds (x + w, y, shadowEdge, h); break;
  161. case 2: sw->setBounds (x, y, w, shadowEdge); break;
  162. case 3: sw->setBounds (x, owner->getBottom(), w, shadowEdge); break;
  163. default: break;
  164. }
  165. if (sw == nullptr)
  166. return;
  167. sw->toBehind (i == 3 ? owner : shadowWindows.getUnchecked (i + 1));
  168. }
  169. }
  170. }
  171. else
  172. {
  173. shadowWindows.clear();
  174. }
  175. }
  176. } // namespace juce