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.

115 lines
3.8KB

  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. ResizableEdgeComponent::ResizableEdgeComponent (Component* const componentToResize,
  22. ComponentBoundsConstrainer* const constrainer_,
  23. Edge edge_)
  24. : component (componentToResize),
  25. constrainer (constrainer_),
  26. edge (edge_)
  27. {
  28. setRepaintsOnMouseActivity (true);
  29. setMouseCursor (isVertical() ? MouseCursor::LeftRightResizeCursor
  30. : MouseCursor::UpDownResizeCursor);
  31. }
  32. ResizableEdgeComponent::~ResizableEdgeComponent()
  33. {
  34. }
  35. //==============================================================================
  36. bool ResizableEdgeComponent::isVertical() const noexcept
  37. {
  38. return edge == leftEdge || edge == rightEdge;
  39. }
  40. void ResizableEdgeComponent::paint (Graphics& g)
  41. {
  42. getLookAndFeel().drawStretchableLayoutResizerBar (g, getWidth(), getHeight(), isVertical(),
  43. isMouseOver(), isMouseButtonDown());
  44. }
  45. void ResizableEdgeComponent::mouseDown (const MouseEvent&)
  46. {
  47. if (component == nullptr)
  48. {
  49. jassertfalse; // You've deleted the component that this resizer was supposed to be using!
  50. return;
  51. }
  52. originalBounds = component->getBounds();
  53. if (constrainer != nullptr)
  54. constrainer->resizeStart();
  55. }
  56. void ResizableEdgeComponent::mouseDrag (const MouseEvent& e)
  57. {
  58. if (component == nullptr)
  59. {
  60. jassertfalse; // You've deleted the component that this resizer was supposed to be using!
  61. return;
  62. }
  63. Rectangle<int> newBounds (originalBounds);
  64. switch (edge)
  65. {
  66. case leftEdge: newBounds.setLeft (jmin (newBounds.getRight(), newBounds.getX() + e.getDistanceFromDragStartX())); break;
  67. case rightEdge: newBounds.setWidth (jmax (0, newBounds.getWidth() + e.getDistanceFromDragStartX())); break;
  68. case topEdge: newBounds.setTop (jmin (newBounds.getBottom(), newBounds.getY() + e.getDistanceFromDragStartY())); break;
  69. case bottomEdge: newBounds.setHeight (jmax (0, newBounds.getHeight() + e.getDistanceFromDragStartY())); break;
  70. default: jassertfalse; break;
  71. }
  72. if (constrainer != nullptr)
  73. {
  74. constrainer->setBoundsForComponent (component, newBounds,
  75. edge == topEdge,
  76. edge == leftEdge,
  77. edge == bottomEdge,
  78. edge == rightEdge);
  79. }
  80. else
  81. {
  82. if (Component::Positioner* const pos = component->getPositioner())
  83. pos->applyNewBounds (newBounds);
  84. else
  85. component->setBounds (newBounds);
  86. }
  87. }
  88. void ResizableEdgeComponent::mouseUp (const MouseEvent&)
  89. {
  90. if (constrainer != nullptr)
  91. constrainer->resizeEnd();
  92. }
  93. } // namespace juce