JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_ComponentBoundsConstrainer.cpp
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - 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 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-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
26namespace juce
27{
28
31
32//==============================================================================
33void ComponentBoundsConstrainer::setMinimumWidth (int minimumWidth) noexcept { minW = minimumWidth; }
34void ComponentBoundsConstrainer::setMaximumWidth (int maximumWidth) noexcept { maxW = maximumWidth; }
37
38void ComponentBoundsConstrainer::setMinimumSize (int minimumWidth, int minimumHeight) noexcept
39{
40 jassert (maxW >= minimumWidth);
41 jassert (maxH >= minimumHeight);
42 jassert (minimumWidth > 0 && minimumHeight > 0);
43
44 minW = minimumWidth;
45 minH = minimumHeight;
46
47 if (minW > maxW) maxW = minW;
48 if (minH > maxH) maxH = minH;
49}
50
51void ComponentBoundsConstrainer::setMaximumSize (int maximumWidth, int maximumHeight) noexcept
52{
53 jassert (maximumWidth >= minW);
54 jassert (maximumHeight >= minH);
55 jassert (maximumWidth > 0 && maximumHeight > 0);
56
57 maxW = jmax (minW, maximumWidth);
58 maxH = jmax (minH, maximumHeight);
59}
60
62 int minimumHeight,
63 int maximumWidth,
64 int maximumHeight) noexcept
65{
66 jassert (maximumWidth >= minimumWidth);
68 jassert (maximumWidth > 0 && maximumHeight > 0);
69 jassert (minimumWidth > 0 && minimumHeight > 0);
70
71 minW = jmax (0, minimumWidth);
72 minH = jmax (0, minimumHeight);
73 maxW = jmax (minW, maximumWidth);
74 maxH = jmax (minH, maximumHeight);
75}
76
87
89{
90 aspectRatio = jmax (0.0, widthOverHeight);
91}
92
94{
95 return aspectRatio;
96}
97
100 bool isStretchingTop,
101 bool isStretchingLeft,
104{
105 jassert (component != nullptr);
106
107 auto bounds = targetBounds;
108
109 auto limits = [&]() -> Rectangle<int>
110 {
111 if (auto* parent = component->getParentComponent())
112 return { parent->getWidth(), parent->getHeight() };
113
114 const auto globalBounds = component->localAreaToGlobal (targetBounds - component->getPosition());
115
116 if (auto* display = Desktop::getInstance().getDisplays().getDisplayForPoint (globalBounds.getCentre()))
117 return component->getLocalArea (nullptr, display->userArea) + component->getPosition();
118
119 const auto max = std::numeric_limits<int>::max();
120 return { max, max };
121 }();
122
123 auto border = [&]() -> BorderSize<int>
124 {
125 if (component->getParentComponent() == nullptr)
126 if (auto* peer = component->getPeer())
127 if (const auto frameSize = peer->getFrameSizeIfPresent())
128 return *frameSize;
129
130 return {};
131 }();
132
133 border.addTo (bounds);
134
135 checkBounds (bounds,
136 border.addedTo (component->getBounds()), limits,
139
140 border.subtractFrom (bounds);
141
142 applyBoundsToComponent (*component, bounds);
143}
144
146{
147 setBoundsForComponent (component, component->getBounds(),
148 false, false, false, false);
149}
150
152{
153 if (auto* positioner = component.getPositioner())
154 positioner->applyNewBounds (bounds);
155 else
156 component.setBounds (bounds);
157}
158
159//==============================================================================
163
167
168//==============================================================================
170 const Rectangle<int>& old,
171 const Rectangle<int>& limits,
172 bool isStretchingTop,
173 bool isStretchingLeft,
176{
178 bounds.setLeft (jlimit (old.getRight() - maxW, old.getRight() - minW, bounds.getX()));
179 else
180 bounds.setWidth (jlimit (minW, maxW, bounds.getWidth()));
181
182 if (isStretchingTop)
183 bounds.setTop (jlimit (old.getBottom() - maxH, old.getBottom() - minH, bounds.getY()));
184 else
185 bounds.setHeight (jlimit (minH, maxH, bounds.getHeight()));
186
187 if (bounds.isEmpty())
188 return;
189
190 if (minOffTop > 0)
191 {
192 const int limit = limits.getY() + jmin (minOffTop - bounds.getHeight(), 0);
193
194 if (bounds.getY() < limit)
195 {
196 if (isStretchingTop)
197 bounds.setTop (limits.getY());
198 else
199 bounds.setY (limit);
200 }
201 }
202
203 if (minOffLeft > 0)
204 {
205 const int limit = limits.getX() + jmin (minOffLeft - bounds.getWidth(), 0);
206
207 if (bounds.getX() < limit)
208 {
210 bounds.setLeft (limits.getX());
211 else
212 bounds.setX (limit);
213 }
214 }
215
216 if (minOffBottom > 0)
217 {
218 const int limit = limits.getBottom() - jmin (minOffBottom, bounds.getHeight());
219
220 if (bounds.getY() > limit)
221 {
223 bounds.setBottom (limits.getBottom());
224 else
225 bounds.setY (limit);
226 }
227 }
228
229 if (minOffRight > 0)
230 {
231 const int limit = limits.getRight() - jmin (minOffRight, bounds.getWidth());
232
233 if (bounds.getX() > limit)
234 {
236 bounds.setRight (limits.getRight());
237 else
238 bounds.setX (limit);
239 }
240 }
241
242 // constrain the aspect ratio if one has been specified..
243 if (aspectRatio > 0.0)
244 {
245 bool adjustWidth;
246
248 {
249 adjustWidth = true;
250 }
252 {
253 adjustWidth = false;
254 }
255 else
256 {
257 const double oldRatio = (old.getHeight() > 0) ? std::abs (old.getWidth() / (double) old.getHeight()) : 0.0;
258 const double newRatio = std::abs (bounds.getWidth() / (double) bounds.getHeight());
259
261 }
262
263 if (adjustWidth)
264 {
265 bounds.setWidth (roundToInt (bounds.getHeight() * aspectRatio));
266
267 if (bounds.getWidth() > maxW || bounds.getWidth() < minW)
268 {
269 bounds.setWidth (jlimit (minW, maxW, bounds.getWidth()));
270 bounds.setHeight (roundToInt (bounds.getWidth() / aspectRatio));
271 }
272 }
273 else
274 {
275 bounds.setHeight (roundToInt (bounds.getWidth() / aspectRatio));
276
277 if (bounds.getHeight() > maxH || bounds.getHeight() < minH)
278 {
279 bounds.setHeight (jlimit (minH, maxH, bounds.getHeight()));
280 bounds.setWidth (roundToInt (bounds.getHeight() * aspectRatio));
281 }
282 }
283
285 {
286 bounds.setX (old.getX() + (old.getWidth() - bounds.getWidth()) / 2);
287 }
289 {
290 bounds.setY (old.getY() + (old.getHeight() - bounds.getHeight()) / 2);
291 }
292 else
293 {
295 bounds.setX (old.getRight() - bounds.getWidth());
296
297 if (isStretchingTop)
298 bounds.setY (old.getBottom() - bounds.getHeight());
299 }
300 }
301
302 jassert (! bounds.isEmpty());
303}
304
305} // namespace juce
Specifies a set of gaps to be left around the sides of a rectangle.
void addTo(Rectangle< ValueType > &rectangle) const noexcept
Adds this border around a given rectangle.
double getFixedAspectRatio() const noexcept
Returns the aspect ratio that was set with setFixedAspectRatio().
void setMaximumHeight(int maximumHeight) noexcept
Imposes a maximum height limit.
void setMinimumSize(int minimumWidth, int minimumHeight) noexcept
Imposes a minimum width and height limit.
void setFixedAspectRatio(double widthOverHeight) noexcept
Specifies a width-to-height ratio that the resizer should always maintain.
void setMinimumOnscreenAmounts(int minimumWhenOffTheTop, int minimumWhenOffTheLeft, int minimumWhenOffTheBottom, int minimumWhenOffTheRight) noexcept
Sets the amount by which the component is allowed to go off-screen.
void setBoundsForComponent(Component *component, Rectangle< int > bounds, bool isStretchingTop, bool isStretchingLeft, bool isStretchingBottom, bool isStretchingRight)
Checks the given bounds, and then sets the component to the corrected size.
void setMinimumWidth(int minimumWidth) noexcept
Imposes a minimum width limit.
void setMaximumSize(int maximumWidth, int maximumHeight) noexcept
Imposes a maximum width and height limit.
void setMinimumHeight(int minimumHeight) noexcept
Imposes a minimum height limit.
virtual void resizeEnd()
This callback happens when the resizer has finished dragging.
virtual void applyBoundsToComponent(Component &, Rectangle< int > bounds)
Called by setBoundsForComponent() to apply a new constrained size to a component.
void setMaximumWidth(int maximumWidth) noexcept
Imposes a maximum width limit.
ComponentBoundsConstrainer() noexcept
When first created, the object will not impose any restrictions on the components.
void setSizeLimits(int minimumWidth, int minimumHeight, int maximumWidth, int maximumHeight) noexcept
Set all the maximum and minimum dimensions.
virtual void resizeStart()
This callback happens when the resizer is about to start dragging.
void checkComponentBounds(Component *component)
Performs a check on the current size of a component, and moves or resizes it if it fails the constrai...
virtual void checkBounds(Rectangle< int > &bounds, const Rectangle< int > &previousBounds, const Rectangle< int > &limits, bool isStretchingTop, bool isStretchingLeft, bool isStretchingBottom, bool isStretchingRight)
This callback changes the given coordinates to impose whatever the current constraints are set to be.
The base class for all JUCE user-interface objects.
Component * getParentComponent() const noexcept
Returns the component which this component is inside.
Point< int > getPosition() const noexcept
Returns the component's top-left position as a Point.
Rectangle< int > getLocalArea(const Component *sourceComponent, Rectangle< int > areaRelativeToSourceComponent) const
Converts a rectangle to be relative to this component's coordinate space.
Positioner * getPositioner() const noexcept
Returns the Positioner object that has been set for this component.
Rectangle< int > getBounds() const noexcept
Returns this component's bounding box.
Rectangle< int > localAreaToGlobal(Rectangle< int > localArea) const
Converts a rectangle from this component's coordinate space to a screen coordinate.
void setBounds(int x, int y, int width, int height)
Changes the component's position and size.
ComponentPeer * getPeer() const
Returns the heavyweight window that contains this component.
const Displays & getDisplays() const noexcept
Returns the Displays object representing the connected displays.
static Desktop &JUCE_CALLTYPE getInstance()
There's only one desktop object, and this method will return it.
const Display * getDisplayForPoint(Point< int > point, bool isPhysical=false) const noexcept
Returns the Display object representing the display containing a given Point (either in logical or ph...
Manages a rectangle and allows geometric operations to be performed on it.
ValueType getRight() const noexcept
Returns the x coordinate of the rectangle's right-hand-side.
void setLeft(ValueType newLeft) noexcept
Moves the x position, adjusting the width so that the right-hand edge remains in the same place.
void setY(ValueType newY) noexcept
Changes the rectangle's Y coordinate.
ValueType getX() const noexcept
Returns the x coordinate of the rectangle's left-hand-side.
void setRight(ValueType newRight) noexcept
Adjusts the width so that the right-hand edge of the rectangle has this new value.
void setHeight(ValueType newHeight) noexcept
Changes the rectangle's height.
void setBottom(ValueType newBottom) noexcept
Adjusts the height so that the bottom edge of the rectangle has this new value.
ValueType getBottom() const noexcept
Returns the y coordinate of the rectangle's bottom edge.
ValueType getWidth() const noexcept
Returns the width of the rectangle.
void setTop(ValueType newTop) noexcept
Moves the y position, adjusting the height so that the bottom edge remains in the same place.
ValueType getY() const noexcept
Returns the y coordinate of the rectangle's top edge.
bool isEmpty() const noexcept
Returns true if the rectangle's width or height are zero or less.
void setX(ValueType newX) noexcept
Changes the rectangle's X coordinate.
void setWidth(ValueType newWidth) noexcept
Changes the rectangle's width.
ValueType getHeight() const noexcept
Returns the height of the rectangle.
#define jassert(expression)
Platform-independent assertion macro.
typedef double
T max(T... args)
JUCE Namespace.
constexpr Type jmin(Type a, Type b)
Returns the smaller of two values.
constexpr Type jmax(Type a, Type b)
Returns the larger of two values.
Type jlimit(Type lowerLimit, Type upperLimit, Type valueToConstrain) noexcept
Constrains a value to keep it within a given range.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
int roundToInt(const FloatType value) noexcept
Fast floating-point-to-integer conversion.