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_DropShadower.cpp 5.7KB

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