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.

200 lines
6.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. ResizableBorderComponent::Zone::Zone() noexcept {}
  21. ResizableBorderComponent::Zone::Zone (int zoneFlags) noexcept : zone (zoneFlags) {}
  22. ResizableBorderComponent::Zone::Zone (const ResizableBorderComponent::Zone& other) noexcept : zone (other.zone) {}
  23. ResizableBorderComponent::Zone& ResizableBorderComponent::Zone::operator= (const ResizableBorderComponent::Zone& other) noexcept
  24. {
  25. zone = other.zone;
  26. return *this;
  27. }
  28. bool ResizableBorderComponent::Zone::operator== (const ResizableBorderComponent::Zone& other) const noexcept { return zone == other.zone; }
  29. bool ResizableBorderComponent::Zone::operator!= (const ResizableBorderComponent::Zone& other) const noexcept { return zone != other.zone; }
  30. ResizableBorderComponent::Zone ResizableBorderComponent::Zone::fromPositionOnBorder (Rectangle<int> totalSize,
  31. BorderSize<int> border,
  32. Point<int> position)
  33. {
  34. int z = 0;
  35. if (totalSize.contains (position)
  36. && ! border.subtractedFrom (totalSize).contains (position))
  37. {
  38. auto minW = jmax (totalSize.getWidth() / 10, jmin (10, totalSize.getWidth() / 3));
  39. if (position.x < jmax (border.getLeft(), minW) && border.getLeft() > 0)
  40. z |= left;
  41. else if (position.x >= totalSize.getWidth() - jmax (border.getRight(), minW) && border.getRight() > 0)
  42. z |= right;
  43. auto minH = jmax (totalSize.getHeight() / 10, jmin (10, totalSize.getHeight() / 3));
  44. if (position.y < jmax (border.getTop(), minH) && border.getTop() > 0)
  45. z |= top;
  46. else if (position.y >= totalSize.getHeight() - jmax (border.getBottom(), minH) && border.getBottom() > 0)
  47. z |= bottom;
  48. }
  49. return Zone (z);
  50. }
  51. MouseCursor ResizableBorderComponent::Zone::getMouseCursor() const noexcept
  52. {
  53. auto mc = MouseCursor::NormalCursor;
  54. switch (zone)
  55. {
  56. case (left | top): mc = MouseCursor::TopLeftCornerResizeCursor; break;
  57. case top: mc = MouseCursor::TopEdgeResizeCursor; break;
  58. case (right | top): mc = MouseCursor::TopRightCornerResizeCursor; break;
  59. case left: mc = MouseCursor::LeftEdgeResizeCursor; break;
  60. case right: mc = MouseCursor::RightEdgeResizeCursor; break;
  61. case (left | bottom): mc = MouseCursor::BottomLeftCornerResizeCursor; break;
  62. case bottom: mc = MouseCursor::BottomEdgeResizeCursor; break;
  63. case (right | bottom): mc = MouseCursor::BottomRightCornerResizeCursor; break;
  64. default: break;
  65. }
  66. return mc;
  67. }
  68. //==============================================================================
  69. ResizableBorderComponent::ResizableBorderComponent (Component* componentToResize,
  70. ComponentBoundsConstrainer* boundsConstrainer)
  71. : component (componentToResize),
  72. constrainer (boundsConstrainer),
  73. borderSize (5)
  74. {
  75. }
  76. ResizableBorderComponent::~ResizableBorderComponent() = default;
  77. //==============================================================================
  78. void ResizableBorderComponent::paint (Graphics& g)
  79. {
  80. getLookAndFeel().drawResizableFrame (g, getWidth(), getHeight(), borderSize);
  81. }
  82. void ResizableBorderComponent::mouseEnter (const MouseEvent& e)
  83. {
  84. updateMouseZone (e);
  85. }
  86. void ResizableBorderComponent::mouseMove (const MouseEvent& e)
  87. {
  88. updateMouseZone (e);
  89. }
  90. void ResizableBorderComponent::mouseDown (const MouseEvent& e)
  91. {
  92. if (component == nullptr)
  93. {
  94. jassertfalse; // You've deleted the component that this resizer was supposed to be using!
  95. return;
  96. }
  97. updateMouseZone (e);
  98. originalBounds = component->getBounds();
  99. if (auto* peer = component->getPeer())
  100. if (&peer->getComponent() == component)
  101. peer->startHostManagedResize (peer->globalToLocal (localPointToGlobal (e.getPosition())), mouseZone);
  102. if (constrainer != nullptr)
  103. constrainer->resizeStart();
  104. }
  105. void ResizableBorderComponent::mouseDrag (const MouseEvent& e)
  106. {
  107. if (component == nullptr)
  108. {
  109. jassertfalse; // You've deleted the component that this resizer was supposed to be using!
  110. return;
  111. }
  112. auto newBounds = mouseZone.resizeRectangleBy (originalBounds, e.getOffsetFromDragStart());
  113. if (constrainer != nullptr)
  114. {
  115. constrainer->setBoundsForComponent (component, newBounds,
  116. mouseZone.isDraggingTopEdge(),
  117. mouseZone.isDraggingLeftEdge(),
  118. mouseZone.isDraggingBottomEdge(),
  119. mouseZone.isDraggingRightEdge());
  120. }
  121. else
  122. {
  123. if (auto* p = component->getPositioner())
  124. p->applyNewBounds (newBounds);
  125. else
  126. component->setBounds (newBounds);
  127. }
  128. }
  129. void ResizableBorderComponent::mouseUp (const MouseEvent&)
  130. {
  131. if (constrainer != nullptr)
  132. constrainer->resizeEnd();
  133. }
  134. bool ResizableBorderComponent::hitTest (int x, int y)
  135. {
  136. return ! borderSize.subtractedFrom (getLocalBounds()).contains (x, y);
  137. }
  138. void ResizableBorderComponent::setBorderThickness (BorderSize<int> newBorderSize)
  139. {
  140. if (borderSize != newBorderSize)
  141. {
  142. borderSize = newBorderSize;
  143. repaint();
  144. }
  145. }
  146. BorderSize<int> ResizableBorderComponent::getBorderThickness() const
  147. {
  148. return borderSize;
  149. }
  150. void ResizableBorderComponent::updateMouseZone (const MouseEvent& e)
  151. {
  152. auto newZone = Zone::fromPositionOnBorder (getLocalBounds(), borderSize, e.getPosition());
  153. if (mouseZone != newZone)
  154. {
  155. mouseZone = newZone;
  156. setMouseCursor (newZone.getMouseCursor());
  157. }
  158. }
  159. } // namespace juce