juce_ResizableEdgeComponent.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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* componentToResize,
  22. ComponentBoundsConstrainer* boundsConstrainer,
  23. Edge e)
  24. : component (componentToResize),
  25. constrainer (boundsConstrainer),
  26. edge (e)
  27. {
  28. setRepaintsOnMouseActivity (true);
  29. setMouseCursor (isVertical() ? MouseCursor::LeftRightResizeCursor
  30. : MouseCursor::UpDownResizeCursor);
  31. }
  32. ResizableEdgeComponent::~ResizableEdgeComponent() = default;
  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. auto 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 (auto* p = component->getPositioner())
  81. p->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. }
  91. } // namespace juce