Audio plugin host https://kx.studio/carla
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.

juce_ResizableBorderComponent.cpp 6.9KB

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