Add remaining files
[juce-lv2.git] / juce / source / src / gui / graphics / contexts / juce_RectanglePlacement.cpp
blob9419ea6b3da01c096ae083ea41862843cb0a437f
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 #include "../../../core/juce_StandardHeader.h"
28 BEGIN_JUCE_NAMESPACE
30 #include "juce_RectanglePlacement.h"
33 //==============================================================================
34 RectanglePlacement::RectanglePlacement (const RectanglePlacement& other) noexcept
35 : flags (other.flags)
39 RectanglePlacement& RectanglePlacement::operator= (const RectanglePlacement& other) noexcept
41 flags = other.flags;
42 return *this;
45 bool RectanglePlacement::operator== (const RectanglePlacement& other) const noexcept
47 return flags == other.flags;
50 bool RectanglePlacement::operator!= (const RectanglePlacement& other) const noexcept
52 return flags != other.flags;
55 void RectanglePlacement::applyTo (double& x, double& y, double& w, double& h,
56 const double dx, const double dy, const double dw, const double dh) const noexcept
58 if (w == 0 || h == 0)
59 return;
61 if ((flags & stretchToFit) != 0)
63 x = dx;
64 y = dy;
65 w = dw;
66 h = dh;
68 else
70 double scale = (flags & fillDestination) != 0 ? jmax (dw / w, dh / h)
71 : jmin (dw / w, dh / h);
73 if ((flags & onlyReduceInSize) != 0)
74 scale = jmin (scale, 1.0);
76 if ((flags & onlyIncreaseInSize) != 0)
77 scale = jmax (scale, 1.0);
79 w *= scale;
80 h *= scale;
82 if ((flags & xLeft) != 0)
83 x = dx;
84 else if ((flags & xRight) != 0)
85 x = dx + dw - w;
86 else
87 x = dx + (dw - w) * 0.5;
89 if ((flags & yTop) != 0)
90 y = dy;
91 else if ((flags & yBottom) != 0)
92 y = dy + dh - h;
93 else
94 y = dy + (dh - h) * 0.5;
98 const AffineTransform RectanglePlacement::getTransformToFit (const Rectangle<float>& source, const Rectangle<float>& destination) const noexcept
100 if (source.isEmpty())
101 return AffineTransform::identity;
103 float newX = destination.getX();
104 float newY = destination.getY();
106 float scaleX = destination.getWidth() / source.getWidth();
107 float scaleY = destination.getHeight() / source.getHeight();
109 if ((flags & stretchToFit) == 0)
111 scaleX = (flags & fillDestination) != 0 ? jmax (scaleX, scaleY)
112 : jmin (scaleX, scaleY);
114 if ((flags & onlyReduceInSize) != 0)
115 scaleX = jmin (scaleX, 1.0f);
117 if ((flags & onlyIncreaseInSize) != 0)
118 scaleX = jmax (scaleX, 1.0f);
120 scaleY = scaleX;
122 if ((flags & xRight) != 0)
123 newX += destination.getWidth() - source.getWidth() * scaleX; // right
124 else if ((flags & xLeft) == 0)
125 newX += (destination.getWidth() - source.getWidth() * scaleX) / 2.0f; // centre
127 if ((flags & yBottom) != 0)
128 newY += destination.getHeight() - source.getHeight() * scaleX; // bottom
129 else if ((flags & yTop) == 0)
130 newY += (destination.getHeight() - source.getHeight() * scaleX) / 2.0f; // centre
133 return AffineTransform::translation (-source.getX(), -source.getY())
134 .scaled (scaleX, scaleY)
135 .translated (newX, newY);
139 END_JUCE_NAMESPACE