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.

131 lines
4.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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. ResizableEdgeComponent::ResizableEdgeComponent (Component* componentToResize,
  21. ComponentBoundsConstrainer* boundsConstrainer,
  22. Edge e)
  23. : component (componentToResize),
  24. constrainer (boundsConstrainer),
  25. edge (e)
  26. {
  27. setRepaintsOnMouseActivity (true);
  28. setMouseCursor (isVertical() ? MouseCursor::LeftRightResizeCursor
  29. : MouseCursor::UpDownResizeCursor);
  30. }
  31. ResizableEdgeComponent::~ResizableEdgeComponent() = default;
  32. //==============================================================================
  33. bool ResizableEdgeComponent::isVertical() const noexcept
  34. {
  35. return edge == leftEdge || edge == rightEdge;
  36. }
  37. void ResizableEdgeComponent::paint (Graphics& g)
  38. {
  39. getLookAndFeel().drawStretchableLayoutResizerBar (g, getWidth(), getHeight(), isVertical(),
  40. isMouseOver(), isMouseButtonDown());
  41. }
  42. void ResizableEdgeComponent::mouseDown (const MouseEvent& e)
  43. {
  44. if (component == nullptr)
  45. {
  46. jassertfalse; // You've deleted the component that this resizer was supposed to be using!
  47. return;
  48. }
  49. originalBounds = component->getBounds();
  50. using Zone = ResizableBorderComponent::Zone;
  51. const Zone zone { [&]
  52. {
  53. switch (edge)
  54. {
  55. case Edge::leftEdge: return Zone::left;
  56. case Edge::rightEdge: return Zone::right;
  57. case Edge::topEdge: return Zone::top;
  58. case Edge::bottomEdge: return Zone::bottom;
  59. }
  60. return Zone::centre;
  61. }() };
  62. if (auto* peer = component->getPeer())
  63. if (&peer->getComponent() == component)
  64. peer->startHostManagedResize (peer->globalToLocal (localPointToGlobal (e.getPosition())), zone);
  65. if (constrainer != nullptr)
  66. constrainer->resizeStart();
  67. }
  68. void ResizableEdgeComponent::mouseDrag (const MouseEvent& e)
  69. {
  70. if (component == nullptr)
  71. {
  72. jassertfalse; // You've deleted the component that this resizer was supposed to be using!
  73. return;
  74. }
  75. auto newBounds = originalBounds;
  76. switch (edge)
  77. {
  78. case leftEdge: newBounds.setLeft (jmin (newBounds.getRight(), newBounds.getX() + e.getDistanceFromDragStartX())); break;
  79. case rightEdge: newBounds.setWidth (jmax (0, newBounds.getWidth() + e.getDistanceFromDragStartX())); break;
  80. case topEdge: newBounds.setTop (jmin (newBounds.getBottom(), newBounds.getY() + e.getDistanceFromDragStartY())); break;
  81. case bottomEdge: newBounds.setHeight (jmax (0, newBounds.getHeight() + e.getDistanceFromDragStartY())); break;
  82. default: jassertfalse; break;
  83. }
  84. if (constrainer != nullptr)
  85. {
  86. constrainer->setBoundsForComponent (component, newBounds,
  87. edge == topEdge,
  88. edge == leftEdge,
  89. edge == bottomEdge,
  90. edge == rightEdge);
  91. }
  92. else
  93. {
  94. if (auto* p = component->getPositioner())
  95. p->applyNewBounds (newBounds);
  96. else
  97. component->setBounds (newBounds);
  98. }
  99. }
  100. void ResizableEdgeComponent::mouseUp (const MouseEvent&)
  101. {
  102. if (constrainer != nullptr)
  103. constrainer->resizeEnd();
  104. }
  105. } // namespace juce