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.

192 lines
7.7KB

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