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.h 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_RESIZABLEBORDERCOMPONENT_H_INCLUDED
  18. #define JUCE_RESIZABLEBORDERCOMPONENT_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. A component that resizes its parent component when dragged.
  22. This component forms a frame around the edge of a component, allowing it to
  23. be dragged by the edges or corners to resize it - like the way windows are
  24. resized in MSWindows or Linux.
  25. To use it, just add it to your component, making it fill the entire parent component
  26. (there's a mouse hit-test that only traps mouse-events which land around the
  27. edge of the component, so it's even ok to put it on top of any other components
  28. you're using). Make sure you rescale the resizer component to fill the parent
  29. each time the parent's size changes.
  30. @see ResizableCornerComponent
  31. */
  32. class JUCE_API ResizableBorderComponent : public Component
  33. {
  34. public:
  35. //==============================================================================
  36. /** Creates a resizer.
  37. Pass in the target component which you want to be resized when this one is
  38. dragged.
  39. The target component will usually be a parent of the resizer component, but this
  40. isn't mandatory.
  41. Remember that when the target component is resized, it'll need to move and
  42. resize this component to keep it in place, as this won't happen automatically.
  43. If the constrainer parameter is non-zero, then this object will be used to enforce
  44. limits on the size and position that the component can be stretched to. Make sure
  45. that the constrainer isn't deleted while still in use by this object.
  46. @see ComponentBoundsConstrainer
  47. */
  48. ResizableBorderComponent (Component* componentToResize,
  49. ComponentBoundsConstrainer* constrainer);
  50. /** Destructor. */
  51. ~ResizableBorderComponent();
  52. //==============================================================================
  53. /** Specifies how many pixels wide the draggable edges of this component are.
  54. @see getBorderThickness
  55. */
  56. void setBorderThickness (const BorderSize<int>& newBorderSize);
  57. /** Returns the number of pixels wide that the draggable edges of this component are.
  58. @see setBorderThickness
  59. */
  60. BorderSize<int> getBorderThickness() const;
  61. //==============================================================================
  62. /** Represents the different sections of a resizable border, which allow it to
  63. resized in different ways.
  64. */
  65. class Zone
  66. {
  67. public:
  68. //==============================================================================
  69. enum Zones
  70. {
  71. centre = 0,
  72. left = 1,
  73. top = 2,
  74. right = 4,
  75. bottom = 8
  76. };
  77. //==============================================================================
  78. /** Creates a Zone from a combination of the flags in \enum Zones. */
  79. explicit Zone (int zoneFlags) noexcept;
  80. Zone() noexcept;
  81. Zone (const Zone&) noexcept;
  82. Zone& operator= (const Zone&) noexcept;
  83. bool operator== (const Zone&) const noexcept;
  84. bool operator!= (const Zone&) const noexcept;
  85. //==============================================================================
  86. /** Given a point within a rectangle with a resizable border, this returns the
  87. zone that the point lies within.
  88. */
  89. static Zone fromPositionOnBorder (const Rectangle<int>& totalSize,
  90. const BorderSize<int>& border,
  91. Point<int> position);
  92. /** Returns an appropriate mouse-cursor for this resize zone. */
  93. MouseCursor getMouseCursor() const noexcept;
  94. /** Returns true if dragging this zone will move the enire object without resizing it. */
  95. bool isDraggingWholeObject() const noexcept { return zone == centre; }
  96. /** Returns true if dragging this zone will move the object's left edge. */
  97. bool isDraggingLeftEdge() const noexcept { return (zone & left) != 0; }
  98. /** Returns true if dragging this zone will move the object's right edge. */
  99. bool isDraggingRightEdge() const noexcept { return (zone & right) != 0; }
  100. /** Returns true if dragging this zone will move the object's top edge. */
  101. bool isDraggingTopEdge() const noexcept { return (zone & top) != 0; }
  102. /** Returns true if dragging this zone will move the object's bottom edge. */
  103. bool isDraggingBottomEdge() const noexcept { return (zone & bottom) != 0; }
  104. /** Resizes this rectangle by the given amount, moving just the edges that this zone
  105. applies to.
  106. */
  107. template <typename ValueType>
  108. Rectangle<ValueType> resizeRectangleBy (Rectangle<ValueType> original,
  109. const Point<ValueType>& distance) const noexcept
  110. {
  111. if (isDraggingWholeObject())
  112. return original + distance;
  113. if (isDraggingLeftEdge()) original.setLeft (jmin (original.getRight(), original.getX() + distance.x));
  114. if (isDraggingRightEdge()) original.setWidth (jmax (ValueType(), original.getWidth() + distance.x));
  115. if (isDraggingTopEdge()) original.setTop (jmin (original.getBottom(), original.getY() + distance.y));
  116. if (isDraggingBottomEdge()) original.setHeight (jmax (ValueType(), original.getHeight() + distance.y));
  117. return original;
  118. }
  119. /** Returns the raw flags for this zone. */
  120. int getZoneFlags() const noexcept { return zone; }
  121. private:
  122. //==============================================================================
  123. int zone;
  124. };
  125. /** Returns the zone in which the mouse was last seen. */
  126. Zone getCurrentZone() const noexcept { return mouseZone; }
  127. protected:
  128. /** @internal */
  129. void paint (Graphics&) override;
  130. /** @internal */
  131. void mouseEnter (const MouseEvent&) override;
  132. /** @internal */
  133. void mouseMove (const MouseEvent&) override;
  134. /** @internal */
  135. void mouseDown (const MouseEvent&) override;
  136. /** @internal */
  137. void mouseDrag (const MouseEvent&) override;
  138. /** @internal */
  139. void mouseUp (const MouseEvent&) override;
  140. /** @internal */
  141. bool hitTest (int x, int y) override;
  142. private:
  143. WeakReference<Component> component;
  144. ComponentBoundsConstrainer* constrainer;
  145. BorderSize<int> borderSize;
  146. Rectangle<int> originalBounds;
  147. Zone mouseZone;
  148. void updateMouseZone (const MouseEvent&);
  149. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ResizableBorderComponent)
  150. };
  151. #endif // JUCE_RESIZABLEBORDERCOMPONENT_H_INCLUDED