1253599a2SIngo Weinhold/*
2532c8733SJohn Scipione * Copyright 2001-2014 Haiku, Inc. All rights reserved.
3253599a2SIngo Weinhold * Distributed under the terms of the MIT License.
4253599a2SIngo Weinhold *
5253599a2SIngo Weinhold * Authors:
6253599a2SIngo Weinhold *		Frans van Nispen
7532c8733SJohn Scipione *		John Scipione, jscipione@gmail.com
8253599a2SIngo Weinhold */
9253599a2SIngo Weinhold
1052a38012Sejakowatz
1152a38012Sejakowatz#include <Rect.h>
1252a38012Sejakowatz
13532c8733SJohn Scipione#include <algorithm>
14532c8733SJohn Scipione
15532c8733SJohn Scipione#include <stdio.h>
16532c8733SJohn Scipione
1752a38012Sejakowatz
1808354652Shaydentechvoid
191f46fc6dSJohn ScipioneBRect::SetLeftTop(const BPoint point)
2008354652Shaydentech{
211f46fc6dSJohn Scipione	left = point.x;
221f46fc6dSJohn Scipione	top = point.y;
2308354652Shaydentech}
24253599a2SIngo Weinhold
25253599a2SIngo Weinhold
2608354652Shaydentechvoid
271f46fc6dSJohn ScipioneBRect::SetRightBottom(const BPoint point)
2808354652Shaydentech{
291f46fc6dSJohn Scipione	right = point.x;
301f46fc6dSJohn Scipione	bottom = point.y;
3108354652Shaydentech}
32253599a2SIngo Weinhold
33253599a2SIngo Weinhold
3408354652Shaydentechvoid
351f46fc6dSJohn ScipioneBRect::SetLeftBottom(const BPoint point)
3608354652Shaydentech{
371f46fc6dSJohn Scipione	left = point.x;
381f46fc6dSJohn Scipione	bottom = point.y;
3908354652Shaydentech}
40253599a2SIngo Weinhold
41253599a2SIngo Weinhold
4208354652Shaydentechvoid
431f46fc6dSJohn ScipioneBRect::SetRightTop(const BPoint point)
4408354652Shaydentech{
451f46fc6dSJohn Scipione	right = point.x;
461f46fc6dSJohn Scipione	top = point.y;
4708354652Shaydentech}
48253599a2SIngo Weinhold
49253599a2SIngo Weinhold
50b17e9812SStefano Ceccherinivoid
51b17e9812SStefano CeccheriniBRect::InsetBy(BPoint point)
5252a38012Sejakowatz{
5370adf6d5SJohn Scipione	left += point.x;
5470adf6d5SJohn Scipione	right -= point.x;
5570adf6d5SJohn Scipione	top += point.y;
5670adf6d5SJohn Scipione	bottom -= point.y;
5752a38012Sejakowatz}
58253599a2SIngo Weinhold
59253599a2SIngo Weinhold
60b17e9812SStefano Ceccherinivoid
61b17e9812SStefano CeccheriniBRect::InsetBy(float dx, float dy)
6252a38012Sejakowatz{
6370adf6d5SJohn Scipione	left += dx;
6470adf6d5SJohn Scipione	right -= dx;
6570adf6d5SJohn Scipione	top += dy;
6670adf6d5SJohn Scipione	bottom -= dy;
6752a38012Sejakowatz}
68253599a2SIngo Weinhold
69253599a2SIngo Weinhold
70b17e9812SStefano CeccheriniBRect&
71b17e9812SStefano CeccheriniBRect::InsetBySelf(BPoint point)
7252a38012Sejakowatz{
73b17e9812SStefano Ceccherini	InsetBy(point);
7452a38012Sejakowatz	return *this;
7552a38012Sejakowatz}
76253599a2SIngo Weinhold
77253599a2SIngo Weinhold
78b17e9812SStefano CeccheriniBRect&
79b17e9812SStefano CeccheriniBRect::InsetBySelf(float dx, float dy)
8052a38012Sejakowatz{
81b17e9812SStefano Ceccherini	InsetBy(dx, dy);
8252a38012Sejakowatz	return *this;
8352a38012Sejakowatz}
84253599a2SIngo Weinhold
85253599a2SIngo Weinhold
86b17e9812SStefano CeccheriniBRect
876643ead5SAxel DörflerBRect::InsetByCopy(BPoint point) const
8852a38012Sejakowatz{
8952a38012Sejakowatz	BRect copy(*this);
90b17e9812SStefano Ceccherini	copy.InsetBy(point);
9152a38012Sejakowatz	return copy;
9252a38012Sejakowatz}
93253599a2SIngo Weinhold
94253599a2SIngo Weinhold
95b17e9812SStefano CeccheriniBRect
966643ead5SAxel DörflerBRect::InsetByCopy(float dx, float dy) const
9752a38012Sejakowatz{
9852a38012Sejakowatz	BRect copy(*this);
9952a38012Sejakowatz	copy.InsetBy(dx, dy);
10052a38012Sejakowatz	return copy;
10152a38012Sejakowatz}
102253599a2SIngo Weinhold
103253599a2SIngo Weinhold
104b17e9812SStefano Ceccherinivoid
105b17e9812SStefano CeccheriniBRect::OffsetBy(BPoint point)
10652a38012Sejakowatz{
10770adf6d5SJohn Scipione	left += point.x;
10870adf6d5SJohn Scipione	right += point.x;
10970adf6d5SJohn Scipione	top += point.y;
11070adf6d5SJohn Scipione	bottom += point.y;
11152a38012Sejakowatz}
112253599a2SIngo Weinhold
113253599a2SIngo Weinhold
114b17e9812SStefano Ceccherinivoid
115b17e9812SStefano CeccheriniBRect::OffsetBy(float dx, float dy)
11652a38012Sejakowatz{
11770adf6d5SJohn Scipione	left += dx;
11870adf6d5SJohn Scipione	right += dx;
11970adf6d5SJohn Scipione	top += dy;
12070adf6d5SJohn Scipione	bottom += dy;
12152a38012Sejakowatz}
122253599a2SIngo Weinhold
123253599a2SIngo Weinhold
124b17e9812SStefano CeccheriniBRect&
125b17e9812SStefano CeccheriniBRect::OffsetBySelf(BPoint point)
12652a38012Sejakowatz{
127b17e9812SStefano Ceccherini	OffsetBy(point);
12852a38012Sejakowatz	return *this;
12952a38012Sejakowatz}
130253599a2SIngo Weinhold
131253599a2SIngo Weinhold
132b17e9812SStefano CeccheriniBRect&
133b17e9812SStefano CeccheriniBRect::OffsetBySelf(float dx, float dy)
13452a38012Sejakowatz{
135b17e9812SStefano Ceccherini	OffsetBy(dx, dy);
13652a38012Sejakowatz	return *this;
13752a38012Sejakowatz}
138253599a2SIngo Weinhold
139253599a2SIngo Weinhold
140b17e9812SStefano CeccheriniBRect
1416643ead5SAxel DörflerBRect::OffsetByCopy(BPoint point) const
14252a38012Sejakowatz{
14352a38012Sejakowatz	BRect copy(*this);
144b17e9812SStefano Ceccherini	copy.OffsetBy(point);
14552a38012Sejakowatz	return copy;
14652a38012Sejakowatz}
147253599a2SIngo Weinhold
148253599a2SIngo Weinhold
149b17e9812SStefano CeccheriniBRect
1506643ead5SAxel DörflerBRect::OffsetByCopy(float dx, float dy) const
15152a38012Sejakowatz{
15252a38012Sejakowatz	BRect copy(*this);
15352a38012Sejakowatz	copy.OffsetBy(dx, dy);
15452a38012Sejakowatz	return copy;
15552a38012Sejakowatz}
156253599a2SIngo Weinhold
157253599a2SIngo Weinhold
158b17e9812SStefano Ceccherinivoid
159b17e9812SStefano CeccheriniBRect::OffsetTo(BPoint point)
16052a38012Sejakowatz{
16170adf6d5SJohn Scipione	right = (right - left) + point.x;
16270adf6d5SJohn Scipione	left = point.x;
16370adf6d5SJohn Scipione	bottom = (bottom - top) + point.y;
16470adf6d5SJohn Scipione	top = point.y;
16552a38012Sejakowatz}
166253599a2SIngo Weinhold
167253599a2SIngo Weinhold
168b17e9812SStefano Ceccherinivoid
169b17e9812SStefano CeccheriniBRect::OffsetTo(float x, float y)
17052a38012Sejakowatz{
17170adf6d5SJohn Scipione	right = (right - left) + x;
17270adf6d5SJohn Scipione	left = x;
17370adf6d5SJohn Scipione	bottom = (bottom - top) + y;
17470adf6d5SJohn Scipione	top=y;
17552a38012Sejakowatz}
176253599a2SIngo Weinhold
177253599a2SIngo Weinhold
178b17e9812SStefano CeccheriniBRect&
179b17e9812SStefano CeccheriniBRect::OffsetToSelf(BPoint point)
18052a38012Sejakowatz{
181b17e9812SStefano Ceccherini	OffsetTo(point);
18252a38012Sejakowatz	return *this;
18352a38012Sejakowatz}
184253599a2SIngo Weinhold
185253599a2SIngo Weinhold
186b17e9812SStefano CeccheriniBRect&
1871f46fc6dSJohn ScipioneBRect::OffsetToSelf(float x, float y)
18852a38012Sejakowatz{
1891f46fc6dSJohn Scipione	OffsetTo(x, y);
19052a38012Sejakowatz	return *this;
19152a38012Sejakowatz}
192253599a2SIngo Weinhold
193253599a2SIngo Weinhold
194b17e9812SStefano CeccheriniBRect
1956643ead5SAxel DörflerBRect::OffsetToCopy(BPoint point) const
19652a38012Sejakowatz{
19752a38012Sejakowatz	BRect copy(*this);
198b17e9812SStefano Ceccherini	copy.OffsetTo(point);
19952a38012Sejakowatz	return copy;
20052a38012Sejakowatz}
201253599a2SIngo Weinhold
202253599a2SIngo Weinhold
203b17e9812SStefano CeccheriniBRect
2041f46fc6dSJohn ScipioneBRect::OffsetToCopy(float x, float y) const
20552a38012Sejakowatz{
20652a38012Sejakowatz	BRect copy(*this);
2071f46fc6dSJohn Scipione	copy.OffsetTo(x, y);
20852a38012Sejakowatz	return copy;
20952a38012Sejakowatz}
210253599a2SIngo Weinhold
211253599a2SIngo Weinhold
212b17e9812SStefano Ceccherinivoid
213b17e9812SStefano CeccheriniBRect::PrintToStream() const
21452a38012Sejakowatz{
215492b2e29SJérôme Duval	printf("BRect(l:%.1f, t:%.1f, r:%.1f, b:%.1f)\n", left, top, right, bottom);
21652a38012Sejakowatz}
217253599a2SIngo Weinhold
218253599a2SIngo Weinhold
219b17e9812SStefano Ceccherinibool
2201f46fc6dSJohn ScipioneBRect::operator==(BRect other) const
22152a38012Sejakowatz{
2221f46fc6dSJohn Scipione	return left == other.left && right == other.right &&
2231f46fc6dSJohn Scipione		top == other.top && bottom == other.bottom;
22452a38012Sejakowatz}
225253599a2SIngo Weinhold
226253599a2SIngo Weinhold
227b17e9812SStefano Ceccherinibool
2281f46fc6dSJohn ScipioneBRect::operator!=(BRect other) const
22952a38012Sejakowatz{
2301f46fc6dSJohn Scipione	return !(*this == other);
23152a38012Sejakowatz}
232253599a2SIngo Weinhold
233253599a2SIngo Weinhold
234b17e9812SStefano CeccheriniBRect
2351f46fc6dSJohn ScipioneBRect::operator&(BRect other) const
23652a38012Sejakowatz{
237532c8733SJohn Scipione	return BRect(std::max(left, other.left), std::max(top, other.top),
238532c8733SJohn Scipione		std::min(right, other.right), std::min(bottom, other.bottom));
23952a38012Sejakowatz}
240253599a2SIngo Weinhold
241253599a2SIngo Weinhold
242b17e9812SStefano CeccheriniBRect
2431f46fc6dSJohn ScipioneBRect::operator|(BRect other) const
24452a38012Sejakowatz{
245532c8733SJohn Scipione	return BRect(std::min(left, other.left), std::min(top, other.top),
246532c8733SJohn Scipione		std::max(right, other.right), std::max(bottom, other.bottom));
24752a38012Sejakowatz}
248253599a2SIngo Weinhold
249253599a2SIngo Weinhold
250b17e9812SStefano Ceccherinibool
251b17e9812SStefano CeccheriniBRect::Intersects(BRect rect) const
25252a38012Sejakowatz{
25306a66521SStefano Ceccherini	if (!IsValid() || !rect.IsValid())
25406a66521SStefano Ceccherini		return false;
25506a66521SStefano Ceccherini
25606a66521SStefano Ceccherini	return !(rect.left > right || rect.right < left
2576643ead5SAxel Dörfler		|| rect.top > bottom || rect.bottom < top);
25852a38012Sejakowatz}
259253599a2SIngo Weinhold
260253599a2SIngo Weinhold
261b17e9812SStefano Ceccherinibool
262b17e9812SStefano CeccheriniBRect::Contains(BPoint point) const
26352a38012Sejakowatz{
264b17e9812SStefano Ceccherini	return point.x >= left && point.x <= right
2656643ead5SAxel Dörfler		&& point.y >= top && point.y <= bottom;
26652a38012Sejakowatz}
267253599a2SIngo Weinhold
268253599a2SIngo Weinhold
269b17e9812SStefano Ceccherinibool
270b17e9812SStefano CeccheriniBRect::Contains(BRect rect) const
27152a38012Sejakowatz{
272b17e9812SStefano Ceccherini	return rect.left >= left && rect.right <= right
2736643ead5SAxel Dörfler		&& rect.top >= top && rect.bottom <= bottom;
27452a38012Sejakowatz}
2756643ead5SAxel Dörfler
2766643ead5SAxel Dörfler
2776643ead5SAxel Dörfler// #pragma mark - BeOS compatibility only
2786643ead5SAxel Dörfler#if __GNUC__ == 2
2796643ead5SAxel Dörfler
2806643ead5SAxel Dörfler
2816643ead5SAxel Dörflerextern "C" BRect
2826643ead5SAxel DörflerInsetByCopy__5BRectG6BPoint(BRect* self, BPoint point)
2836643ead5SAxel Dörfler{
2846643ead5SAxel Dörfler	BRect copy(*self);
2856643ead5SAxel Dörfler	copy.InsetBy(point);
2866643ead5SAxel Dörfler	return copy;
2876643ead5SAxel Dörfler}
2886643ead5SAxel Dörfler
2896643ead5SAxel Dörfler
2906643ead5SAxel Dörflerextern "C" BRect
2916643ead5SAxel DörflerInsetByCopy__5BRectff(BRect* self, float dx, float dy)
2926643ead5SAxel Dörfler{
2936643ead5SAxel Dörfler	BRect copy(*self);
2946643ead5SAxel Dörfler	copy.InsetBy(dx, dy);
2956643ead5SAxel Dörfler	return copy;
2966643ead5SAxel Dörfler}
2976643ead5SAxel Dörfler
2986643ead5SAxel Dörfler
2996643ead5SAxel Dörflerextern "C" BRect
3006643ead5SAxel DörflerOffsetByCopy__5BRectG6BPoint(BRect* self, BPoint point)
3016643ead5SAxel Dörfler{
3026643ead5SAxel Dörfler	BRect copy(*self);
3036643ead5SAxel Dörfler	copy.OffsetBy(point);
3046643ead5SAxel Dörfler	return copy;
3056643ead5SAxel Dörfler}
3066643ead5SAxel Dörfler
3076643ead5SAxel Dörfler
3086643ead5SAxel Dörflerextern "C" BRect
3096643ead5SAxel DörflerOffsetByCopy__5BRectff(BRect* self, float dx, float dy)
3106643ead5SAxel Dörfler{
3116643ead5SAxel Dörfler	BRect copy(*self);
3126643ead5SAxel Dörfler	copy.OffsetBy(dx, dy);
3136643ead5SAxel Dörfler	return copy;
3146643ead5SAxel Dörfler}
3156643ead5SAxel Dörfler
3166643ead5SAxel Dörfler
3176643ead5SAxel Dörflerextern "C" BRect
3186643ead5SAxel DörflerOffsetToCopy__5BRectG6BPoint(BRect* self, BPoint point)
3196643ead5SAxel Dörfler{
3206643ead5SAxel Dörfler	BRect copy(*self);
3216643ead5SAxel Dörfler	copy.OffsetTo(point);
3226643ead5SAxel Dörfler	return copy;
3236643ead5SAxel Dörfler}
3246643ead5SAxel Dörfler
3256643ead5SAxel Dörfler
3266643ead5SAxel Dörflerextern "C" BRect
3276643ead5SAxel DörflerOffsetToCopy__5BRectff(BRect* self, float dx, float dy)
3286643ead5SAxel Dörfler{
3296643ead5SAxel Dörfler	BRect copy(*self);
3306643ead5SAxel Dörfler	copy.OffsetTo(dx, dy);
3316643ead5SAxel Dörfler	return copy;
3326643ead5SAxel Dörfler}
3336643ead5SAxel Dörfler
3346643ead5SAxel Dörfler
335949cf6acSMurai Takashi#elif __GNUC__ >= 4
3361cd0e20cSAxel Dörfler// TODO: remove this when new GCC 4 packages have to be built anyway
3371cd0e20cSAxel Dörfler
3381cd0e20cSAxel Dörfler
3391cd0e20cSAxel Dörflerextern "C" BRect
3401cd0e20cSAxel Dörfler_ZN5BRect11InsetByCopyE6BPoint(BRect* self, BPoint point)
3411cd0e20cSAxel Dörfler{
3421cd0e20cSAxel Dörfler	BRect copy(*self);
3431cd0e20cSAxel Dörfler	copy.InsetBy(point);
3441cd0e20cSAxel Dörfler	return copy;
3451cd0e20cSAxel Dörfler}
3461cd0e20cSAxel Dörfler
3471cd0e20cSAxel Dörfler
3481cd0e20cSAxel Dörflerextern "C" BRect
3491cd0e20cSAxel Dörfler_ZN5BRect11InsetByCopyEff(BRect* self, float dx, float dy)
3501cd0e20cSAxel Dörfler{
3511cd0e20cSAxel Dörfler	BRect copy(*self);
3521cd0e20cSAxel Dörfler	copy.InsetBy(dx, dy);
3531cd0e20cSAxel Dörfler	return copy;
3541cd0e20cSAxel Dörfler}
3551cd0e20cSAxel Dörfler
3561cd0e20cSAxel Dörfler
3571cd0e20cSAxel Dörflerextern "C" BRect
3581cd0e20cSAxel Dörfler_ZN5BRect12OffsetByCopyE6BPoint(BRect* self, BPoint point)
3591cd0e20cSAxel Dörfler{
3601cd0e20cSAxel Dörfler	BRect copy(*self);
3611cd0e20cSAxel Dörfler	copy.OffsetBy(point);
3621cd0e20cSAxel Dörfler	return copy;
3631cd0e20cSAxel Dörfler}
3641cd0e20cSAxel Dörfler
3651cd0e20cSAxel Dörfler
3661cd0e20cSAxel Dörflerextern "C" BRect
3671cd0e20cSAxel Dörfler_ZN5BRect12OffsetByCopyEff(BRect* self, float dx, float dy)
3681cd0e20cSAxel Dörfler{
3691cd0e20cSAxel Dörfler	BRect copy(*self);
3701cd0e20cSAxel Dörfler	copy.OffsetBy(dx, dy);
3711cd0e20cSAxel Dörfler	return copy;
3721cd0e20cSAxel Dörfler}
3731cd0e20cSAxel Dörfler
3741cd0e20cSAxel Dörfler
3751cd0e20cSAxel Dörflerextern "C" BRect
3761cd0e20cSAxel Dörfler_ZN5BRect12OffsetToCopyE6BPoint(BRect* self, BPoint point)
3771cd0e20cSAxel Dörfler{
3781cd0e20cSAxel Dörfler	BRect copy(*self);
3791cd0e20cSAxel Dörfler	copy.OffsetTo(point);
3801cd0e20cSAxel Dörfler	return copy;
3811cd0e20cSAxel Dörfler}
3821cd0e20cSAxel Dörfler
3831cd0e20cSAxel Dörfler
3841cd0e20cSAxel Dörflerextern "C" BRect
3851cd0e20cSAxel Dörfler_ZN5BRect12OffsetToCopyEff(BRect* self, float dx, float dy)
3861cd0e20cSAxel Dörfler{
3871cd0e20cSAxel Dörfler	BRect copy(*self);
3881cd0e20cSAxel Dörfler	copy.OffsetTo(dx, dy);
3891cd0e20cSAxel Dörfler	return copy;
3901cd0e20cSAxel Dörfler}
3911cd0e20cSAxel Dörfler
3921cd0e20cSAxel Dörfler
393949cf6acSMurai Takashi#endif	// __GNUC__ >= 4
394