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.

203 lines
7.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. ResizableBorderComponent::Zone::Zone() noexcept
  19. : zone (0)
  20. {}
  21. ResizableBorderComponent::Zone::Zone (const int zoneFlags) noexcept
  22. : zone (zoneFlags)
  23. {}
  24. ResizableBorderComponent::Zone::Zone (const ResizableBorderComponent::Zone& other) noexcept
  25. : zone (other.zone)
  26. {}
  27. ResizableBorderComponent::Zone& ResizableBorderComponent::Zone::operator= (const ResizableBorderComponent::Zone& other) noexcept
  28. {
  29. zone = other.zone;
  30. return *this;
  31. }
  32. bool ResizableBorderComponent::Zone::operator== (const ResizableBorderComponent::Zone& other) const noexcept { return zone == other.zone; }
  33. bool ResizableBorderComponent::Zone::operator!= (const ResizableBorderComponent::Zone& other) const noexcept { return zone != other.zone; }
  34. ResizableBorderComponent::Zone ResizableBorderComponent::Zone::fromPositionOnBorder (const Rectangle<int>& totalSize,
  35. const BorderSize<int>& border,
  36. Point<int> position)
  37. {
  38. int z = 0;
  39. if (totalSize.contains (position)
  40. && ! border.subtractedFrom (totalSize).contains (position))
  41. {
  42. const int minW = jmax (totalSize.getWidth() / 10, jmin (10, totalSize.getWidth() / 3));
  43. if (position.x < jmax (border.getLeft(), minW) && border.getLeft() > 0)
  44. z |= left;
  45. else if (position.x >= totalSize.getWidth() - jmax (border.getRight(), minW) && border.getRight() > 0)
  46. z |= right;
  47. const int minH = jmax (totalSize.getHeight() / 10, jmin (10, totalSize.getHeight() / 3));
  48. if (position.y < jmax (border.getTop(), minH) && border.getTop() > 0)
  49. z |= top;
  50. else if (position.y >= totalSize.getHeight() - jmax (border.getBottom(), minH) && border.getBottom() > 0)
  51. z |= bottom;
  52. }
  53. return Zone (z);
  54. }
  55. MouseCursor ResizableBorderComponent::Zone::getMouseCursor() const noexcept
  56. {
  57. MouseCursor::StandardCursorType mc = MouseCursor::NormalCursor;
  58. switch (zone)
  59. {
  60. case (left | top): mc = MouseCursor::TopLeftCornerResizeCursor; break;
  61. case top: mc = MouseCursor::TopEdgeResizeCursor; break;
  62. case (right | top): mc = MouseCursor::TopRightCornerResizeCursor; break;
  63. case left: mc = MouseCursor::LeftEdgeResizeCursor; break;
  64. case right: mc = MouseCursor::RightEdgeResizeCursor; break;
  65. case (left | bottom): mc = MouseCursor::BottomLeftCornerResizeCursor; break;
  66. case bottom: mc = MouseCursor::BottomEdgeResizeCursor; break;
  67. case (right | bottom): mc = MouseCursor::BottomRightCornerResizeCursor; break;
  68. default: break;
  69. }
  70. return mc;
  71. }
  72. //==============================================================================
  73. ResizableBorderComponent::ResizableBorderComponent (Component* const componentToResize,
  74. ComponentBoundsConstrainer* const constrainer_)
  75. : component (componentToResize),
  76. constrainer (constrainer_),
  77. borderSize (5),
  78. mouseZone (0)
  79. {
  80. }
  81. ResizableBorderComponent::~ResizableBorderComponent()
  82. {
  83. }
  84. //==============================================================================
  85. void ResizableBorderComponent::paint (Graphics& g)
  86. {
  87. getLookAndFeel().drawResizableFrame (g, getWidth(), getHeight(), borderSize);
  88. }
  89. void ResizableBorderComponent::mouseEnter (const MouseEvent& e)
  90. {
  91. updateMouseZone (e);
  92. }
  93. void ResizableBorderComponent::mouseMove (const MouseEvent& e)
  94. {
  95. updateMouseZone (e);
  96. }
  97. void ResizableBorderComponent::mouseDown (const MouseEvent& e)
  98. {
  99. if (component == nullptr)
  100. {
  101. jassertfalse; // You've deleted the component that this resizer was supposed to be using!
  102. return;
  103. }
  104. updateMouseZone (e);
  105. originalBounds = component->getBounds();
  106. if (constrainer != nullptr)
  107. constrainer->resizeStart();
  108. }
  109. void ResizableBorderComponent::mouseDrag (const MouseEvent& e)
  110. {
  111. if (component == nullptr)
  112. {
  113. jassertfalse; // You've deleted the component that this resizer was supposed to be using!
  114. return;
  115. }
  116. const Rectangle<int> newBounds (mouseZone.resizeRectangleBy (originalBounds, e.getOffsetFromDragStart()));
  117. if (constrainer != nullptr)
  118. {
  119. constrainer->setBoundsForComponent (component, newBounds,
  120. mouseZone.isDraggingTopEdge(),
  121. mouseZone.isDraggingLeftEdge(),
  122. mouseZone.isDraggingBottomEdge(),
  123. mouseZone.isDraggingRightEdge());
  124. }
  125. else
  126. {
  127. if (Component::Positioner* const pos = component->getPositioner())
  128. pos->applyNewBounds (newBounds);
  129. else
  130. component->setBounds (newBounds);
  131. }
  132. }
  133. void ResizableBorderComponent::mouseUp (const MouseEvent&)
  134. {
  135. if (constrainer != nullptr)
  136. constrainer->resizeEnd();
  137. }
  138. bool ResizableBorderComponent::hitTest (int x, int y)
  139. {
  140. return x < borderSize.getLeft()
  141. || x >= getWidth() - borderSize.getRight()
  142. || y < borderSize.getTop()
  143. || y >= getHeight() - borderSize.getBottom();
  144. }
  145. void ResizableBorderComponent::setBorderThickness (const BorderSize<int>& newBorderSize)
  146. {
  147. if (borderSize != newBorderSize)
  148. {
  149. borderSize = newBorderSize;
  150. repaint();
  151. }
  152. }
  153. BorderSize<int> ResizableBorderComponent::getBorderThickness() const
  154. {
  155. return borderSize;
  156. }
  157. void ResizableBorderComponent::updateMouseZone (const MouseEvent& e)
  158. {
  159. Zone newZone (Zone::fromPositionOnBorder (getLocalBounds(), borderSize, e.getPosition()));
  160. if (mouseZone != newZone)
  161. {
  162. mouseZone = newZone;
  163. setMouseCursor (newZone.getMouseCursor());
  164. }
  165. }