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.

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