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.

198 lines
7.9KB

  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_JUCEHEADER__
  18. #define __JUCE_RESIZABLEBORDERCOMPONENT_JUCEHEADER__
  19. #include "juce_ComponentBoundsConstrainer.h"
  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 non-zero, then this object will be used to enforce
  45. limits on the size and position that the component can be stretched to. Make sure
  46. 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. };
  152. #endif // __JUCE_RESIZABLEBORDERCOMPONENT_JUCEHEADER__