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.

105 lines
3.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. ResizableEdgeComponent::ResizableEdgeComponent (Component* componentToResize,
  16. ComponentBoundsConstrainer* boundsConstrainer,
  17. Edge e)
  18. : component (componentToResize),
  19. constrainer (boundsConstrainer),
  20. edge (e)
  21. {
  22. setRepaintsOnMouseActivity (true);
  23. setMouseCursor (isVertical() ? MouseCursor::LeftRightResizeCursor
  24. : MouseCursor::UpDownResizeCursor);
  25. }
  26. ResizableEdgeComponent::~ResizableEdgeComponent() = default;
  27. //==============================================================================
  28. bool ResizableEdgeComponent::isVertical() const noexcept
  29. {
  30. return edge == leftEdge || edge == rightEdge;
  31. }
  32. void ResizableEdgeComponent::paint (Graphics& g)
  33. {
  34. getLookAndFeel().drawStretchableLayoutResizerBar (g, getWidth(), getHeight(), isVertical(),
  35. isMouseOver(), isMouseButtonDown());
  36. }
  37. void ResizableEdgeComponent::mouseDown (const MouseEvent&)
  38. {
  39. if (component == nullptr)
  40. {
  41. jassertfalse; // You've deleted the component that this resizer was supposed to be using!
  42. return;
  43. }
  44. originalBounds = component->getBounds();
  45. if (constrainer != nullptr)
  46. constrainer->resizeStart();
  47. }
  48. void ResizableEdgeComponent::mouseDrag (const MouseEvent& e)
  49. {
  50. if (component == nullptr)
  51. {
  52. jassertfalse; // You've deleted the component that this resizer was supposed to be using!
  53. return;
  54. }
  55. auto newBounds = originalBounds;
  56. switch (edge)
  57. {
  58. case leftEdge: newBounds.setLeft (jmin (newBounds.getRight(), newBounds.getX() + e.getDistanceFromDragStartX())); break;
  59. case rightEdge: newBounds.setWidth (jmax (0, newBounds.getWidth() + e.getDistanceFromDragStartX())); break;
  60. case topEdge: newBounds.setTop (jmin (newBounds.getBottom(), newBounds.getY() + e.getDistanceFromDragStartY())); break;
  61. case bottomEdge: newBounds.setHeight (jmax (0, newBounds.getHeight() + e.getDistanceFromDragStartY())); break;
  62. default: jassertfalse; break;
  63. }
  64. if (constrainer != nullptr)
  65. {
  66. constrainer->setBoundsForComponent (component, newBounds,
  67. edge == topEdge,
  68. edge == leftEdge,
  69. edge == bottomEdge,
  70. edge == rightEdge);
  71. }
  72. else
  73. {
  74. if (auto* p = component->getPositioner())
  75. p->applyNewBounds (newBounds);
  76. else
  77. component->setBounds (newBounds);
  78. }
  79. }
  80. void ResizableEdgeComponent::mouseUp (const MouseEvent&)
  81. {
  82. if (constrainer != nullptr)
  83. constrainer->resizeEnd();
  84. }
  85. } // namespace juce