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

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