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.

206 lines
7.2KB

  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. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. ResizableBorderComponent::Zone::Zone (const int zoneFlags) noexcept
  21. : zone (zoneFlags)
  22. {}
  23. ResizableBorderComponent::Zone::Zone (const ResizableBorderComponent::Zone& other) noexcept
  24. : zone (other.zone)
  25. {}
  26. ResizableBorderComponent::Zone& ResizableBorderComponent::Zone::operator= (const ResizableBorderComponent::Zone& other) noexcept
  27. {
  28. zone = other.zone;
  29. return *this;
  30. }
  31. bool ResizableBorderComponent::Zone::operator== (const ResizableBorderComponent::Zone& other) const noexcept { return zone == other.zone; }
  32. bool ResizableBorderComponent::Zone::operator!= (const ResizableBorderComponent::Zone& other) const noexcept { return zone != other.zone; }
  33. const ResizableBorderComponent::Zone ResizableBorderComponent::Zone::fromPositionOnBorder (const Rectangle<int>& totalSize,
  34. const BorderSize<int>& border,
  35. const Point<int>& position)
  36. {
  37. int z = 0;
  38. if (totalSize.contains (position)
  39. && ! border.subtractedFrom (totalSize).contains (position))
  40. {
  41. const int minW = jmax (totalSize.getWidth() / 10, jmin (10, totalSize.getWidth() / 3));
  42. if (position.getX() < jmax (border.getLeft(), minW) && border.getLeft() > 0)
  43. z |= left;
  44. else if (position.getX() >= totalSize.getWidth() - jmax (border.getRight(), minW) && border.getRight() > 0)
  45. z |= right;
  46. const int minH = jmax (totalSize.getHeight() / 10, jmin (10, totalSize.getHeight() / 3));
  47. if (position.getY() < jmax (border.getTop(), minH) && border.getTop() > 0)
  48. z |= top;
  49. else if (position.getY() >= totalSize.getHeight() - jmax (border.getBottom(), minH) && border.getBottom() > 0)
  50. z |= bottom;
  51. }
  52. return Zone (z);
  53. }
  54. MouseCursor ResizableBorderComponent::Zone::getMouseCursor() const noexcept
  55. {
  56. MouseCursor::StandardCursorType mc = MouseCursor::NormalCursor;
  57. switch (zone)
  58. {
  59. case (left | top): mc = MouseCursor::TopLeftCornerResizeCursor; break;
  60. case top: mc = MouseCursor::TopEdgeResizeCursor; break;
  61. case (right | top): mc = MouseCursor::TopRightCornerResizeCursor; break;
  62. case left: mc = MouseCursor::LeftEdgeResizeCursor; break;
  63. case right: mc = MouseCursor::RightEdgeResizeCursor; break;
  64. case (left | bottom): mc = MouseCursor::BottomLeftCornerResizeCursor; break;
  65. case bottom: mc = MouseCursor::BottomEdgeResizeCursor; break;
  66. case (right | bottom): mc = MouseCursor::BottomRightCornerResizeCursor; break;
  67. default: break;
  68. }
  69. return mc;
  70. }
  71. //==============================================================================
  72. ResizableBorderComponent::ResizableBorderComponent (Component* const componentToResize,
  73. ComponentBoundsConstrainer* const constrainer_)
  74. : component (componentToResize),
  75. constrainer (constrainer_),
  76. borderSize (5),
  77. mouseZone (0)
  78. {
  79. }
  80. ResizableBorderComponent::~ResizableBorderComponent()
  81. {
  82. }
  83. //==============================================================================
  84. void ResizableBorderComponent::paint (Graphics& g)
  85. {
  86. getLookAndFeel().drawResizableFrame (g, getWidth(), getHeight(), borderSize);
  87. }
  88. void ResizableBorderComponent::mouseEnter (const MouseEvent& e)
  89. {
  90. updateMouseZone (e);
  91. }
  92. void ResizableBorderComponent::mouseMove (const MouseEvent& e)
  93. {
  94. updateMouseZone (e);
  95. }
  96. void ResizableBorderComponent::mouseDown (const MouseEvent& e)
  97. {
  98. if (component == nullptr)
  99. {
  100. jassertfalse; // You've deleted the component that this resizer was supposed to be using!
  101. return;
  102. }
  103. updateMouseZone (e);
  104. originalBounds = component->getBounds();
  105. if (constrainer != nullptr)
  106. constrainer->resizeStart();
  107. }
  108. void ResizableBorderComponent::mouseDrag (const MouseEvent& e)
  109. {
  110. if (component == nullptr)
  111. {
  112. jassertfalse; // You've deleted the component that this resizer was supposed to be using!
  113. return;
  114. }
  115. const Rectangle<int> newBounds (mouseZone.resizeRectangleBy (originalBounds, e.getOffsetFromDragStart()));
  116. if (constrainer != nullptr)
  117. {
  118. constrainer->setBoundsForComponent (component, newBounds,
  119. mouseZone.isDraggingTopEdge(),
  120. mouseZone.isDraggingLeftEdge(),
  121. mouseZone.isDraggingBottomEdge(),
  122. mouseZone.isDraggingRightEdge());
  123. }
  124. else
  125. {
  126. Component::Positioner* const pos = component->getPositioner();
  127. if (pos != nullptr)
  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. const 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. }
  166. END_JUCE_NAMESPACE