juce_ResizableBorderComponent.h 7.8 KB

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