1 /* 2 ============================================================================== 3 4 This file is part of the JUCE library. 5 Copyright (c) 2020 - Raw Material Software Limited 6 7 JUCE is an open source library subject to commercial or open-source 8 licensing. 9 10 By using JUCE, you agree to the terms of both the JUCE 6 End-User License 11 Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020). 12 13 End User License Agreement: www.juce.com/juce-6-licence 14 Privacy Policy: www.juce.com/juce-privacy-policy 15 16 Or: You may also use this code under the terms of the GPL v3 (see 17 www.gnu.org/licenses). 18 19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER 20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE 21 DISCLAIMED. 22 23 ============================================================================== 24 */ 25 26 namespace juce 27 { 28 29 //============================================================================== 30 /** 31 A component that resizes its parent component when dragged. 32 33 This component forms a frame around the edge of a component, allowing it to 34 be dragged by the edges or corners to resize it - like the way windows are 35 resized in MSWindows or Linux. 36 37 To use it, just add it to your component, making it fill the entire parent component 38 (there's a mouse hit-test that only traps mouse-events which land around the 39 edge of the component, so it's even ok to put it on top of any other components 40 you're using). Make sure you rescale the resizer component to fill the parent 41 each time the parent's size changes. 42 43 @see ResizableCornerComponent 44 45 @tags{GUI} 46 */ 47 class JUCE_API ResizableBorderComponent : public Component 48 { 49 public: 50 //============================================================================== 51 /** Creates a resizer. 52 53 Pass in the target component which you want to be resized when this one is 54 dragged. 55 56 The target component will usually be a parent of the resizer component, but this 57 isn't mandatory. 58 59 Remember that when the target component is resized, it'll need to move and 60 resize this component to keep it in place, as this won't happen automatically. 61 62 If the constrainer parameter is not a nullptr, then this object will be used to 63 enforce limits on the size and position that the component can be stretched to. 64 Make sure that the constrainer isn't deleted while still in use by this object. 65 66 @see ComponentBoundsConstrainer 67 */ 68 ResizableBorderComponent (Component* componentToResize, 69 ComponentBoundsConstrainer* constrainer); 70 71 /** Destructor. */ 72 ~ResizableBorderComponent() override; 73 74 75 //============================================================================== 76 /** Specifies how many pixels wide the draggable edges of this component are. 77 78 @see getBorderThickness 79 */ 80 void setBorderThickness (BorderSize<int> newBorderSize); 81 82 /** Returns the number of pixels wide that the draggable edges of this component are. 83 84 @see setBorderThickness 85 */ 86 BorderSize<int> getBorderThickness() const; 87 88 89 //============================================================================== 90 /** Represents the different sections of a resizable border, which allow it to 91 resized in different ways. 92 */ 93 class Zone 94 { 95 public: 96 //============================================================================== 97 enum Zones 98 { 99 centre = 0, 100 left = 1, 101 top = 2, 102 right = 4, 103 bottom = 8 104 }; 105 106 //============================================================================== 107 /** Creates a Zone from a combination of the flags in zoneFlags. */ 108 explicit Zone (int zoneFlags) noexcept; 109 110 Zone() noexcept; 111 Zone (const Zone&) noexcept; 112 Zone& operator= (const Zone&) noexcept; 113 114 bool operator== (const Zone&) const noexcept; 115 bool operator!= (const Zone&) const noexcept; 116 117 //============================================================================== 118 /** Given a point within a rectangle with a resizable border, this returns the 119 zone that the point lies within. 120 */ 121 static Zone fromPositionOnBorder (Rectangle<int> totalSize, 122 BorderSize<int> border, 123 Point<int> position); 124 125 /** Returns an appropriate mouse-cursor for this resize zone. */ 126 MouseCursor getMouseCursor() const noexcept; 127 128 /** Returns true if dragging this zone will move the enire object without resizing it. */ isDraggingWholeObject()129 bool isDraggingWholeObject() const noexcept { return zone == centre; } 130 /** Returns true if dragging this zone will move the object's left edge. */ isDraggingLeftEdge()131 bool isDraggingLeftEdge() const noexcept { return (zone & left) != 0; } 132 /** Returns true if dragging this zone will move the object's right edge. */ isDraggingRightEdge()133 bool isDraggingRightEdge() const noexcept { return (zone & right) != 0; } 134 /** Returns true if dragging this zone will move the object's top edge. */ isDraggingTopEdge()135 bool isDraggingTopEdge() const noexcept { return (zone & top) != 0; } 136 /** Returns true if dragging this zone will move the object's bottom edge. */ isDraggingBottomEdge()137 bool isDraggingBottomEdge() const noexcept { return (zone & bottom) != 0; } 138 139 /** Resizes this rectangle by the given amount, moving just the edges that this zone 140 applies to. 141 */ 142 template <typename ValueType> resizeRectangleBy(Rectangle<ValueType> original,const Point<ValueType> & distance)143 Rectangle<ValueType> resizeRectangleBy (Rectangle<ValueType> original, 144 const Point<ValueType>& distance) const noexcept 145 { 146 if (isDraggingWholeObject()) 147 return original + distance; 148 149 if (isDraggingLeftEdge()) original.setLeft (jmin (original.getRight(), original.getX() + distance.x)); 150 if (isDraggingRightEdge()) original.setWidth (jmax (ValueType(), original.getWidth() + distance.x)); 151 if (isDraggingTopEdge()) original.setTop (jmin (original.getBottom(), original.getY() + distance.y)); 152 if (isDraggingBottomEdge()) original.setHeight (jmax (ValueType(), original.getHeight() + distance.y)); 153 154 return original; 155 } 156 157 /** Returns the raw flags for this zone. */ getZoneFlags()158 int getZoneFlags() const noexcept { return zone; } 159 160 private: 161 //============================================================================== 162 int zone = centre; 163 }; 164 165 /** Returns the zone in which the mouse was last seen. */ getCurrentZone()166 Zone getCurrentZone() const noexcept { return mouseZone; } 167 168 protected: 169 /** @internal */ 170 void paint (Graphics&) override; 171 /** @internal */ 172 void mouseEnter (const MouseEvent&) override; 173 /** @internal */ 174 void mouseMove (const MouseEvent&) override; 175 /** @internal */ 176 void mouseDown (const MouseEvent&) override; 177 /** @internal */ 178 void mouseDrag (const MouseEvent&) override; 179 /** @internal */ 180 void mouseUp (const MouseEvent&) override; 181 /** @internal */ 182 bool hitTest (int x, int y) override; 183 184 private: 185 WeakReference<Component> component; 186 ComponentBoundsConstrainer* constrainer; 187 BorderSize<int> borderSize; 188 Rectangle<int> originalBounds; 189 Zone mouseZone; 190 191 void updateMouseZone (const MouseEvent&); 192 193 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ResizableBorderComponent) 194 }; 195 196 } // namespace juce 197