Add remaining files
[juce-lv2.git] / juce / source / src / gui / components / layout / juce_ResizableBorderComponent.h
blob8f94087231a1f1cbf7d160d07932061e9076d416
1 /*
2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCE_RESIZABLEBORDERCOMPONENT_JUCEHEADER__
27 #define __JUCE_RESIZABLEBORDERCOMPONENT_JUCEHEADER__
29 #include "juce_ComponentBoundsConstrainer.h"
32 //==============================================================================
33 /**
34 A component that resizes its parent component when dragged.
36 This component forms a frame around the edge of a component, allowing it to
37 be dragged by the edges or corners to resize it - like the way windows are
38 resized in MSWindows or Linux.
40 To use it, just add it to your component, making it fill the entire parent component
41 (there's a mouse hit-test that only traps mouse-events which land around the
42 edge of the component, so it's even ok to put it on top of any other components
43 you're using). Make sure you rescale the resizer component to fill the parent
44 each time the parent's size changes.
46 @see ResizableCornerComponent
48 class JUCE_API ResizableBorderComponent : public Component
50 public:
51 //==============================================================================
52 /** Creates a resizer.
54 Pass in the target component which you want to be resized when this one is
55 dragged.
57 The target component will usually be a parent of the resizer component, but this
58 isn't mandatory.
60 Remember that when the target component is resized, it'll need to move and
61 resize this component to keep it in place, as this won't happen automatically.
63 If the constrainer parameter is non-zero, then this object will be used to enforce
64 limits on the size and position that the component can be stretched to. Make sure
65 that the constrainer isn't deleted while still in use by this object.
67 @see ComponentBoundsConstrainer
69 ResizableBorderComponent (Component* componentToResize,
70 ComponentBoundsConstrainer* constrainer);
72 /** Destructor. */
73 ~ResizableBorderComponent();
76 //==============================================================================
77 /** Specifies how many pixels wide the draggable edges of this component are.
79 @see getBorderThickness
81 void setBorderThickness (const BorderSize<int>& newBorderSize);
83 /** Returns the number of pixels wide that the draggable edges of this component are.
85 @see setBorderThickness
87 const BorderSize<int> getBorderThickness() const;
90 //==============================================================================
91 /** Represents the different sections of a resizable border, which allow it to
92 resized in different ways.
94 class Zone
96 public:
97 //==============================================================================
98 enum Zones
100 centre = 0,
101 left = 1,
102 top = 2,
103 right = 4,
104 bottom = 8
107 //==============================================================================
108 /** Creates a Zone from a combination of the flags in \enum Zones. */
109 explicit Zone (int zoneFlags = 0) noexcept;
110 Zone (const Zone& other) noexcept;
111 Zone& operator= (const Zone& other) noexcept;
113 bool operator== (const Zone& other) const noexcept;
114 bool operator!= (const Zone& other) const noexcept;
116 //==============================================================================
117 /** Given a point within a rectangle with a resizable border, this returns the
118 zone that the point lies within.
120 static const Zone fromPositionOnBorder (const Rectangle<int>& totalSize,
121 const BorderSize<int>& border,
122 const Point<int>& position);
124 /** Returns an appropriate mouse-cursor for this resize zone. */
125 const MouseCursor getMouseCursor() const noexcept;
127 /** Returns true if dragging this zone will move the enire object without resizing it. */
128 bool isDraggingWholeObject() const noexcept { return zone == centre; }
129 /** Returns true if dragging this zone will move the object's left edge. */
130 bool isDraggingLeftEdge() const noexcept { return (zone & left) != 0; }
131 /** Returns true if dragging this zone will move the object's right edge. */
132 bool isDraggingRightEdge() const noexcept { return (zone & right) != 0; }
133 /** Returns true if dragging this zone will move the object's top edge. */
134 bool isDraggingTopEdge() const noexcept { return (zone & top) != 0; }
135 /** Returns true if dragging this zone will move the object's bottom edge. */
136 bool isDraggingBottomEdge() const noexcept { return (zone & bottom) != 0; }
138 /** Resizes this rectangle by the given amount, moving just the edges that this zone
139 applies to.
141 template <typename ValueType>
142 const Rectangle<ValueType> resizeRectangleBy (Rectangle<ValueType> original,
143 const Point<ValueType>& distance) const noexcept
145 if (isDraggingWholeObject())
146 return original + distance;
148 if (isDraggingLeftEdge())
149 original.setLeft (jmin (original.getRight(), original.getX() + distance.getX()));
151 if (isDraggingRightEdge())
152 original.setWidth (jmax (ValueType(), original.getWidth() + distance.getX()));
154 if (isDraggingTopEdge())
155 original.setTop (jmin (original.getBottom(), original.getY() + distance.getY()));
157 if (isDraggingBottomEdge())
158 original.setHeight (jmax (ValueType(), original.getHeight() + distance.getY()));
160 return original;
163 /** Returns the raw flags for this zone. */
164 int getZoneFlags() const noexcept { return zone; }
166 private:
167 //==============================================================================
168 int zone;
172 protected:
173 //==============================================================================
174 /** @internal */
175 void paint (Graphics& g);
176 /** @internal */
177 void mouseEnter (const MouseEvent& e);
178 /** @internal */
179 void mouseMove (const MouseEvent& e);
180 /** @internal */
181 void mouseDown (const MouseEvent& e);
182 /** @internal */
183 void mouseDrag (const MouseEvent& e);
184 /** @internal */
185 void mouseUp (const MouseEvent& e);
186 /** @internal */
187 bool hitTest (int x, int y);
189 private:
190 WeakReference<Component> component;
191 ComponentBoundsConstrainer* constrainer;
192 BorderSize<int> borderSize;
193 Rectangle<int> originalBounds;
194 Zone mouseZone;
196 void updateMouseZone (const MouseEvent& e);
198 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ResizableBorderComponent);
202 #endif // __JUCE_RESIZABLEBORDERCOMPONENT_JUCEHEADER__