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_ResizableEdgeComponent.cpp 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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&)
  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. if (constrainer != nullptr)
  51. constrainer->resizeStart();
  52. }
  53. void ResizableEdgeComponent::mouseDrag (const MouseEvent& e)
  54. {
  55. if (component == nullptr)
  56. {
  57. jassertfalse; // You've deleted the component that this resizer was supposed to be using!
  58. return;
  59. }
  60. auto newBounds = originalBounds;
  61. switch (edge)
  62. {
  63. case leftEdge: newBounds.setLeft (jmin (newBounds.getRight(), newBounds.getX() + e.getDistanceFromDragStartX())); break;
  64. case rightEdge: newBounds.setWidth (jmax (0, newBounds.getWidth() + e.getDistanceFromDragStartX())); break;
  65. case topEdge: newBounds.setTop (jmin (newBounds.getBottom(), newBounds.getY() + e.getDistanceFromDragStartY())); break;
  66. case bottomEdge: newBounds.setHeight (jmax (0, newBounds.getHeight() + e.getDistanceFromDragStartY())); break;
  67. default: jassertfalse; break;
  68. }
  69. if (constrainer != nullptr)
  70. {
  71. constrainer->setBoundsForComponent (component, newBounds,
  72. edge == topEdge,
  73. edge == leftEdge,
  74. edge == bottomEdge,
  75. edge == rightEdge);
  76. }
  77. else
  78. {
  79. if (auto* p = component->getPositioner())
  80. p->applyNewBounds (newBounds);
  81. else
  82. component->setBounds (newBounds);
  83. }
  84. }
  85. void ResizableEdgeComponent::mouseUp (const MouseEvent&)
  86. {
  87. if (constrainer != nullptr)
  88. constrainer->resizeEnd();
  89. }
  90. } // namespace juce