1 /*
2 * Copyright 2001-2006, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 * Frans van Nispen
7 * Stephan Aßmus <superstippi@gmx.de>
8 */
9
10 #ifndef INT_RECT_H
11 #define INT_RECT_H
12
13
14 #include <Region.h>
15
16 #include "IntPoint.h"
17
18 class IntRect {
19 public:
20 int32 left;
21 int32 top;
22 int32 right;
23 int32 bottom;
24
25 IntRect();
26 IntRect(const IntRect& r);
27 IntRect(const BRect& r);
28 IntRect(int32 l, int32 t, int32 r, int32 b);
29 IntRect(const IntPoint& lt,
30 const IntPoint& rb);
31
32 IntRect& operator=(const IntRect &r);
33 void Set(int32 l, int32 t, int32 r, int32 b);
34
35 void PrintToStream() const;
36
37 IntPoint LeftTop() const;
38 IntPoint RightBottom() const;
39 IntPoint LeftBottom() const;
40 IntPoint RightTop() const;
41
42 void SetLeftTop(const IntPoint& p);
43 void SetRightBottom(const IntPoint& p);
44 void SetLeftBottom(const IntPoint& p);
45 void SetRightTop(const IntPoint& p);
46
47 // transformation
48 void InsetBy(const IntPoint& p);
49 void InsetBy(int32 dx, int32 dy);
50 void OffsetBy(const IntPoint& p);
51 void OffsetBy(int32 dx, int32 dy);
52 void OffsetTo(const IntPoint& p);
53 void OffsetTo(int32 x, int32 y);
54
55 // expression transformations
56 IntRect& InsetBySelf(const IntPoint& p);
57 IntRect& InsetBySelf(int32 dx, int32 dy);
58 IntRect InsetByCopy(const IntPoint& p);
59 IntRect InsetByCopy(int32 dx, int32 dy);
60 IntRect& OffsetBySelf(const IntPoint& p);
61 IntRect& OffsetBySelf(int32 dx, int32 dy);
62 IntRect OffsetByCopy(const IntPoint& p);
63 IntRect OffsetByCopy(int32 dx, int32 dy);
64 IntRect& OffsetToSelf(const IntPoint& p);
65 IntRect& OffsetToSelf(int32 dx, int32 dy);
66 IntRect OffsetToCopy(const IntPoint& p);
67 IntRect OffsetToCopy(int32 dx, int32 dy);
68
69 // comparison
70 bool operator==(const IntRect& r) const;
71 bool operator!=(const IntRect& r) const;
72
73 // intersection and union
74 IntRect operator&(const IntRect& r) const;
75 IntRect operator|(const IntRect& r) const;
76
77 // conversion to BRect and clipping_rect
78 operator clipping_rect() const;
BRect()79 operator BRect() const
80 { return BRect(left, top,
81 right, bottom); }
82
83 bool Intersects(const IntRect& r) const;
84 bool IsValid() const;
85 int32 Width() const;
86 int32 IntegerWidth() const;
87 int32 Height() const;
88 int32 IntegerHeight() const;
89 bool Contains(const IntPoint& p) const;
90 bool Contains(const IntRect& r) const;
91 };
92
93
94 // inline definitions ----------------------------------------------------------
95
96 inline IntPoint
LeftTop()97 IntRect::LeftTop() const
98 {
99 return *(const IntPoint*)&left;
100 }
101
102
103 inline IntPoint
RightBottom()104 IntRect::RightBottom() const
105 {
106 return *(const IntPoint*)&right;
107 }
108
109
110 inline IntPoint
LeftBottom()111 IntRect::LeftBottom() const
112 {
113 return IntPoint(left, bottom);
114 }
115
116
117 inline IntPoint
RightTop()118 IntRect::RightTop() const
119 {
120 return IntPoint(right, top);
121 }
122
123
124 inline
IntRect()125 IntRect::IntRect()
126 {
127 top = left = 0;
128 bottom = right = -1;
129 }
130
131
132 inline
IntRect(int32 l,int32 t,int32 r,int32 b)133 IntRect::IntRect(int32 l, int32 t, int32 r, int32 b)
134 {
135 left = l;
136 top = t;
137 right = r;
138 bottom = b;
139 }
140
141
142 inline
IntRect(const IntRect & r)143 IntRect::IntRect(const IntRect &r)
144 {
145 left = r.left;
146 top = r.top;
147 right = r.right;
148 bottom = r.bottom;
149 }
150
151
152 inline
IntRect(const BRect & r)153 IntRect::IntRect(const BRect &r)
154 {
155 left = (int32)r.left;
156 top = (int32)r.top;
157 right = (int32)r.right;
158 bottom = (int32)r.bottom;
159 }
160
161
162 inline
IntRect(const IntPoint & leftTop,const IntPoint & rightBottom)163 IntRect::IntRect(const IntPoint& leftTop, const IntPoint& rightBottom)
164 {
165 left = leftTop.x;
166 top = leftTop.y;
167 right = rightBottom.x;
168 bottom = rightBottom.y;
169 }
170
171
172 inline IntRect &
173 IntRect::operator=(const IntRect& from)
174 {
175 left = from.left;
176 top = from.top;
177 right = from.right;
178 bottom = from.bottom;
179 return *this;
180 }
181
182
183 inline void
Set(int32 l,int32 t,int32 r,int32 b)184 IntRect::Set(int32 l, int32 t, int32 r, int32 b)
185 {
186 left = l;
187 top = t;
188 right = r;
189 bottom = b;
190 }
191
192
193 inline bool
IsValid()194 IntRect::IsValid() const
195 {
196 return left <= right && top <= bottom;
197 }
198
199
200 inline int32
IntegerWidth()201 IntRect::IntegerWidth() const
202 {
203 return right - left;
204 }
205
206
207 inline int32
Width()208 IntRect::Width() const
209 {
210 return right - left;
211 }
212
213
214 inline int32
IntegerHeight()215 IntRect::IntegerHeight() const
216 {
217 return bottom - top;
218 }
219
220
221 inline int32
Height()222 IntRect::Height() const
223 {
224 return bottom - top;
225 }
226
227 inline
clipping_rect()228 IntRect::operator clipping_rect() const
229 {
230 clipping_rect r;
231 r.left = left;
232 r.top = top;
233 r.right = right;
234 r.bottom = bottom;
235 return r;
236 }
237
238
239 #endif // INT_RECT_H
240