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.

209 lines
5.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. class DropShadower::ShadowWindow : public Component
  19. {
  20. public:
  21. ShadowWindow (Component* comp, const DropShadow& ds)
  22. : target (comp), shadow (ds)
  23. {
  24. setVisible (true);
  25. setInterceptsMouseClicks (false, false);
  26. if (comp->isOnDesktop())
  27. {
  28. setSize (1, 1); // to keep the OS happy by not having zero-size windows
  29. addToDesktop (ComponentPeer::windowIgnoresMouseClicks
  30. | ComponentPeer::windowIsTemporary
  31. | ComponentPeer::windowIgnoresKeyPresses);
  32. }
  33. else if (Component* const parent = comp->getParentComponent())
  34. {
  35. parent->addChildComponent (this);
  36. }
  37. }
  38. void paint (Graphics& g)
  39. {
  40. if (Component* c = target)
  41. shadow.drawForRectangle (g, getLocalArea (c, c->getLocalBounds()));
  42. }
  43. void resized()
  44. {
  45. repaint(); // (needed for correct repainting)
  46. }
  47. private:
  48. WeakReference<Component> target;
  49. DropShadow shadow;
  50. JUCE_DECLARE_NON_COPYABLE (ShadowWindow)
  51. };
  52. //==============================================================================
  53. DropShadower::DropShadower (const DropShadow& ds)
  54. : owner (nullptr), shadow (ds), reentrant (false)
  55. {
  56. }
  57. DropShadower::~DropShadower()
  58. {
  59. if (owner != nullptr)
  60. {
  61. owner->removeComponentListener (this);
  62. owner = nullptr;
  63. }
  64. updateParent();
  65. reentrant = true;
  66. shadowWindows.clear();
  67. }
  68. void DropShadower::setOwner (Component* componentToFollow)
  69. {
  70. if (componentToFollow != owner)
  71. {
  72. if (owner != nullptr)
  73. owner->removeComponentListener (this);
  74. // (the component can't be null)
  75. jassert (componentToFollow != nullptr);
  76. owner = componentToFollow;
  77. jassert (owner != nullptr);
  78. updateParent();
  79. owner->addComponentListener (this);
  80. updateShadows();
  81. }
  82. }
  83. void DropShadower::updateParent()
  84. {
  85. if (Component* p = lastParentComp)
  86. p->removeComponentListener (this);
  87. lastParentComp = owner != nullptr ? owner->getParentComponent() : nullptr;
  88. if (Component* p = lastParentComp)
  89. p->addComponentListener (this);
  90. }
  91. void DropShadower::componentMovedOrResized (Component& c, bool /*wasMoved*/, bool /*wasResized*/)
  92. {
  93. if (owner == &c)
  94. updateShadows();
  95. }
  96. void DropShadower::componentBroughtToFront (Component& c)
  97. {
  98. if (owner == &c)
  99. updateShadows();
  100. }
  101. void DropShadower::componentChildrenChanged (Component&)
  102. {
  103. updateShadows();
  104. }
  105. void DropShadower::componentParentHierarchyChanged (Component& c)
  106. {
  107. if (owner == &c)
  108. {
  109. updateParent();
  110. updateShadows();
  111. }
  112. }
  113. void DropShadower::componentVisibilityChanged (Component& c)
  114. {
  115. if (owner == &c)
  116. updateShadows();
  117. }
  118. void DropShadower::updateShadows()
  119. {
  120. if (reentrant)
  121. return;
  122. const ScopedValueSetter<bool> setter (reentrant, true, false);
  123. if (owner == nullptr)
  124. {
  125. shadowWindows.clear();
  126. return;
  127. }
  128. if (owner->isShowing()
  129. && owner->getWidth() > 0 && owner->getHeight() > 0
  130. && (Desktop::canUseSemiTransparentWindows() || owner->getParentComponent() != nullptr))
  131. {
  132. while (shadowWindows.size() < 4)
  133. shadowWindows.add (new ShadowWindow (owner, shadow));
  134. const int shadowEdge = jmax (shadow.offset.x, shadow.offset.y) + shadow.radius;
  135. const int x = owner->getX();
  136. const int y = owner->getY() - shadowEdge;
  137. const int w = owner->getWidth();
  138. const int h = owner->getHeight() + shadowEdge + shadowEdge;
  139. for (int i = 4; --i >= 0;)
  140. {
  141. // there seem to be rare situations where the dropshadower may be deleted by
  142. // callbacks during this loop, so use a weak ref to watch out for this..
  143. WeakReference<Component> sw (shadowWindows[i]);
  144. if (sw != nullptr)
  145. sw->setAlwaysOnTop (owner->isAlwaysOnTop());
  146. if (sw != nullptr)
  147. {
  148. switch (i)
  149. {
  150. case 0: sw->setBounds (x - shadowEdge, y, shadowEdge, h); break;
  151. case 1: sw->setBounds (x + w, y, shadowEdge, h); break;
  152. case 2: sw->setBounds (x, y, w, shadowEdge); break;
  153. case 3: sw->setBounds (x, owner->getBottom(), w, shadowEdge); break;
  154. default: break;
  155. }
  156. }
  157. if (sw != nullptr)
  158. sw->toBehind (i == 3 ? owner : shadowWindows.getUnchecked (i + 1));
  159. if (sw == nullptr)
  160. return;
  161. }
  162. }
  163. else
  164. {
  165. shadowWindows.clear();
  166. }
  167. }