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.

111 lines
4.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. ResizableEdgeComponent::ResizableEdgeComponent (Component* const componentToResize,
  19. ComponentBoundsConstrainer* const constrainer_,
  20. Edge edge_)
  21. : component (componentToResize),
  22. constrainer (constrainer_),
  23. edge (edge_)
  24. {
  25. setRepaintsOnMouseActivity (true);
  26. setMouseCursor (MouseCursor (isVertical() ? MouseCursor::LeftRightResizeCursor
  27. : MouseCursor::UpDownResizeCursor));
  28. }
  29. ResizableEdgeComponent::~ResizableEdgeComponent()
  30. {
  31. }
  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. Rectangle<int> 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. Component::Positioner* const pos = component->getPositioner();
  80. if (pos != nullptr)
  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. }