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.

234 lines
7.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. SidePanel::SidePanel (StringRef title, int width, bool positionOnLeft,
  22. Component* contentToDisplay, bool deleteComponentWhenNoLongerNeeded)
  23. : titleLabel ("titleLabel", title),
  24. isOnLeft (positionOnLeft),
  25. panelWidth (width)
  26. {
  27. lookAndFeelChanged();
  28. addAndMakeVisible (titleLabel);
  29. dismissButton.addListener (this);
  30. addAndMakeVisible (dismissButton);
  31. Desktop::getInstance().addGlobalMouseListener (this);
  32. if (contentToDisplay != nullptr)
  33. setContent (contentToDisplay, deleteComponentWhenNoLongerNeeded);
  34. setOpaque (false);
  35. }
  36. SidePanel::~SidePanel()
  37. {
  38. if (parent != nullptr)
  39. parent->removeComponentListener (this);
  40. }
  41. void SidePanel::setContent (Component* newContent, bool deleteComponentWhenNoLongerNeeded)
  42. {
  43. if (contentComponent.get() != newContent)
  44. {
  45. if (deleteComponentWhenNoLongerNeeded)
  46. contentComponent.setOwned (newContent);
  47. else
  48. contentComponent.setNonOwned (newContent);
  49. addAndMakeVisible (contentComponent);
  50. }
  51. }
  52. void SidePanel::showOrHide (bool show)
  53. {
  54. if (parent != nullptr)
  55. {
  56. isShowing = show;
  57. Desktop::getInstance().getAnimator().animateComponent (this, calculateBoundsInParent (*parent),
  58. 1.0f, 250, true, 1.0, 0.0);
  59. }
  60. }
  61. void SidePanel::resized()
  62. {
  63. auto bounds = getLocalBounds();
  64. calculateAndRemoveShadowBounds (bounds);
  65. auto titleBounds = bounds.removeFromTop (titleBarHeight);
  66. dismissButton.setBounds (isOnLeft ? titleBounds.removeFromRight (30).withTrimmedRight (10)
  67. : titleBounds.removeFromLeft (30).withTrimmedLeft (10));
  68. titleLabel.setBounds (isOnLeft ? titleBounds.withTrimmedRight (40)
  69. : titleBounds.withTrimmedLeft (40));
  70. if (contentComponent != nullptr)
  71. contentComponent->setBounds (bounds);
  72. }
  73. void SidePanel::paint (Graphics& g)
  74. {
  75. auto& lf = getLookAndFeel();
  76. auto bgColour = lf.findColour (SidePanel::backgroundColour);
  77. auto shadowColour = lf.findColour (SidePanel::shadowBaseColour);
  78. g.setGradientFill (ColourGradient (shadowColour.withAlpha (0.7f), (isOnLeft ? shadowArea.getTopLeft()
  79. : shadowArea.getTopRight()).toFloat(),
  80. shadowColour.withAlpha (0.0f), (isOnLeft ? shadowArea.getTopRight()
  81. : shadowArea.getTopLeft()).toFloat(), false));
  82. g.fillRect (shadowArea);
  83. g.excludeClipRegion (shadowArea);
  84. g.fillAll (bgColour);
  85. }
  86. void SidePanel::parentHierarchyChanged()
  87. {
  88. auto* newParent = getParentComponent();
  89. if ((newParent != nullptr) && (parent != newParent))
  90. {
  91. if (parent != nullptr)
  92. parent->removeComponentListener (this);
  93. parent = newParent;
  94. parent->addComponentListener (this);
  95. }
  96. }
  97. void SidePanel::mouseDrag (const MouseEvent& e)
  98. {
  99. if (shouldResize)
  100. {
  101. auto currentMouseDragX = static_cast<int> (e.position.x);
  102. if (isOnLeft)
  103. {
  104. amountMoved = startingBounds.getRight() - currentMouseDragX;
  105. setBounds (getBounds().withX (startingBounds.getX() - jmax (amountMoved, 0)));
  106. }
  107. else
  108. {
  109. amountMoved = currentMouseDragX - startingBounds.getX();
  110. setBounds (getBounds().withX (startingBounds.getX() + jmax (amountMoved, 0)));
  111. }
  112. }
  113. else
  114. {
  115. auto relativeMouseDownPosition = getLocalPoint (e.eventComponent, e.getMouseDownPosition());
  116. auto relativeMouseDragPosition = getLocalPoint (e.eventComponent, e.getPosition());
  117. if (! getLocalBounds().contains (relativeMouseDownPosition)
  118. && getLocalBounds().contains (relativeMouseDragPosition))
  119. {
  120. shouldResize = true;
  121. startingBounds = getBounds();
  122. }
  123. }
  124. }
  125. void SidePanel::mouseUp (const MouseEvent&)
  126. {
  127. if (shouldResize)
  128. {
  129. showOrHide (amountMoved < (panelWidth / 2));
  130. amountMoved = 0;
  131. shouldResize = false;
  132. }
  133. }
  134. //==========================================================================
  135. void SidePanel::lookAndFeelChanged()
  136. {
  137. auto& lf = getLookAndFeel();
  138. dismissButton.setShape (lf.getSidePanelDismissButtonShape (*this), false, true, false);
  139. dismissButton.setColours (lf.findColour (SidePanel::dismissButtonNormalColour),
  140. lf.findColour (SidePanel::dismissButtonOverColour),
  141. lf.findColour (SidePanel::dismissButtonDownColour));
  142. titleLabel.setFont (lf.getSidePanelTitleFont (*this));
  143. titleLabel.setColour (Label::textColourId, findColour (SidePanel::titleTextColour));
  144. titleLabel.setJustificationType (lf.getSidePanelTitleJustification (*this));
  145. }
  146. void SidePanel::componentMovedOrResized (Component& component, bool wasMoved, bool wasResized)
  147. {
  148. ignoreUnused (wasMoved);
  149. if (wasResized && (&component == parent))
  150. setBounds (calculateBoundsInParent (component));
  151. }
  152. void SidePanel::buttonClicked (Button*)
  153. {
  154. showOrHide (false);
  155. }
  156. Rectangle<int> SidePanel::calculateBoundsInParent (Component& parentComp) const
  157. {
  158. auto parentBounds = parentComp.getBounds();
  159. if (isOnLeft)
  160. {
  161. return isShowing ? parentBounds.removeFromLeft (panelWidth)
  162. : parentBounds.withX (parentBounds.getX() - panelWidth).withWidth (panelWidth);
  163. }
  164. return isShowing ? parentBounds.removeFromRight (panelWidth)
  165. : parentBounds.withX (parentBounds.getRight()).withWidth (panelWidth);
  166. }
  167. void SidePanel::calculateAndRemoveShadowBounds (Rectangle<int>& bounds)
  168. {
  169. shadowArea = isOnLeft ? bounds.removeFromRight (shadowWidth)
  170. : bounds.removeFromLeft (shadowWidth);
  171. }
  172. bool SidePanel::isMouseEventInThisOrChildren (Component* eventComponent)
  173. {
  174. if (eventComponent == this)
  175. return true;
  176. for (auto& child : getChildren())
  177. if (eventComponent == child)
  178. return true;
  179. return false;
  180. }
  181. } // namespace juce