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.

197 lines
7.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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. //==============================================================================
  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. @tags{GUI}
  33. */
  34. class JUCE_API ResizableBorderComponent : public Component
  35. {
  36. public:
  37. //==============================================================================
  38. /** Creates a resizer.
  39. Pass in the target component which you want to be resized when this one is
  40. dragged.
  41. The target component will usually be a parent of the resizer component, but this
  42. isn't mandatory.
  43. Remember that when the target component is resized, it'll need to move and
  44. resize this component to keep it in place, as this won't happen automatically.
  45. If the constrainer parameter is not a nullptr, then this object will be used to
  46. enforce limits on the size and position that the component can be stretched to.
  47. Make sure that the constrainer isn't deleted while still in use by this object.
  48. @see ComponentBoundsConstrainer
  49. */
  50. ResizableBorderComponent (Component* componentToResize,
  51. ComponentBoundsConstrainer* constrainer);
  52. /** Destructor. */
  53. ~ResizableBorderComponent() override;
  54. //==============================================================================
  55. /** Specifies how many pixels wide the draggable edges of this component are.
  56. @see getBorderThickness
  57. */
  58. void setBorderThickness (BorderSize<int> newBorderSize);
  59. /** Returns the number of pixels wide that the draggable edges of this component are.
  60. @see setBorderThickness
  61. */
  62. BorderSize<int> getBorderThickness() const;
  63. //==============================================================================
  64. /** Represents the different sections of a resizable border, which allow it to
  65. resized in different ways.
  66. */
  67. class Zone
  68. {
  69. public:
  70. //==============================================================================
  71. enum Zones
  72. {
  73. centre = 0,
  74. left = 1,
  75. top = 2,
  76. right = 4,
  77. bottom = 8
  78. };
  79. //==============================================================================
  80. /** Creates a Zone from a combination of the flags in zoneFlags. */
  81. explicit Zone (int zoneFlags) noexcept;
  82. Zone() noexcept;
  83. Zone (const Zone&) noexcept;
  84. Zone& operator= (const Zone&) noexcept;
  85. bool operator== (const Zone&) const noexcept;
  86. bool operator!= (const Zone&) const noexcept;
  87. //==============================================================================
  88. /** Given a point within a rectangle with a resizable border, this returns the
  89. zone that the point lies within.
  90. */
  91. static Zone fromPositionOnBorder (Rectangle<int> totalSize,
  92. BorderSize<int> border,
  93. Point<int> position);
  94. /** Returns an appropriate mouse-cursor for this resize zone. */
  95. MouseCursor getMouseCursor() const noexcept;
  96. /** Returns true if dragging this zone will move the enire object without resizing it. */
  97. bool isDraggingWholeObject() const noexcept { return zone == centre; }
  98. /** Returns true if dragging this zone will move the object's left edge. */
  99. bool isDraggingLeftEdge() const noexcept { return (zone & left) != 0; }
  100. /** Returns true if dragging this zone will move the object's right edge. */
  101. bool isDraggingRightEdge() const noexcept { return (zone & right) != 0; }
  102. /** Returns true if dragging this zone will move the object's top edge. */
  103. bool isDraggingTopEdge() const noexcept { return (zone & top) != 0; }
  104. /** Returns true if dragging this zone will move the object's bottom edge. */
  105. bool isDraggingBottomEdge() const noexcept { return (zone & bottom) != 0; }
  106. /** Resizes this rectangle by the given amount, moving just the edges that this zone
  107. applies to.
  108. */
  109. template <typename ValueType>
  110. Rectangle<ValueType> resizeRectangleBy (Rectangle<ValueType> original,
  111. const Point<ValueType>& distance) const noexcept
  112. {
  113. if (isDraggingWholeObject())
  114. return original + distance;
  115. if (isDraggingLeftEdge()) original.setLeft (jmin (original.getRight(), original.getX() + distance.x));
  116. if (isDraggingRightEdge()) original.setWidth (jmax (ValueType(), original.getWidth() + distance.x));
  117. if (isDraggingTopEdge()) original.setTop (jmin (original.getBottom(), original.getY() + distance.y));
  118. if (isDraggingBottomEdge()) original.setHeight (jmax (ValueType(), original.getHeight() + distance.y));
  119. return original;
  120. }
  121. /** Returns the raw flags for this zone. */
  122. int getZoneFlags() const noexcept { return zone; }
  123. private:
  124. //==============================================================================
  125. int zone = centre;
  126. };
  127. /** Returns the zone in which the mouse was last seen. */
  128. Zone getCurrentZone() const noexcept { return mouseZone; }
  129. protected:
  130. /** @internal */
  131. void paint (Graphics&) override;
  132. /** @internal */
  133. void mouseEnter (const MouseEvent&) override;
  134. /** @internal */
  135. void mouseMove (const MouseEvent&) override;
  136. /** @internal */
  137. void mouseDown (const MouseEvent&) override;
  138. /** @internal */
  139. void mouseDrag (const MouseEvent&) override;
  140. /** @internal */
  141. void mouseUp (const MouseEvent&) override;
  142. /** @internal */
  143. bool hitTest (int x, int y) override;
  144. private:
  145. WeakReference<Component> component;
  146. ComponentBoundsConstrainer* constrainer;
  147. BorderSize<int> borderSize;
  148. Rectangle<int> originalBounds;
  149. Zone mouseZone;
  150. void updateMouseZone (const MouseEvent&);
  151. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ResizableBorderComponent)
  152. };
  153. } // namespace juce