[Win32SS]
[reactos.git] / reactos / win32ss / user / user32 / windows / rect.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /*
20 * PROJECT: ReactOS user32.dll
21 * FILE: win32ss/user/user32/windows/rect.c
22 * PURPOSE: Input
23 * PROGRAMMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
24 * UPDATE HISTORY:
25 * 09-05-2001 CSH Created
26 */
27
28 /* INCLUDES ******************************************************************/
29
30 #include <user32.h>
31
32 #include <wine/debug.h>
33
34 /* FUNCTIONS *****************************************************************/
35
36 /*
37 * @implemented
38 */
39 BOOL
40 WINAPI
41 CopyRect(LPRECT lprcDst,
42 CONST RECT *lprcSrc)
43 {
44 if (lprcDst == NULL || lprcSrc == NULL)
45 return FALSE;
46
47 *lprcDst = *lprcSrc;
48
49 return TRUE;
50 }
51
52
53 /*
54 * @implemented
55 */
56 BOOL
57 WINAPI
58 EqualRect(CONST RECT *lprc1,
59 CONST RECT *lprc2)
60 {
61 if (lprc1 == NULL || lprc2 == NULL)
62 return FALSE;
63
64 return (lprc1->left == lprc2->left) && (lprc1->top == lprc2->top) &&
65 (lprc1->right == lprc2->right) && (lprc1->bottom == lprc2->bottom);
66 }
67
68
69 /*
70 * @implemented
71 */
72 BOOL
73 WINAPI
74 InflateRect(LPRECT rect,
75 int dx,
76 int dy)
77 {
78 if (rect == NULL)
79 return FALSE;
80
81 rect->left -= dx;
82 rect->top -= dy;
83 rect->right += dx;
84 rect->bottom += dy;
85
86 return TRUE;
87 }
88
89
90 /*
91 * @implemented
92 */
93 BOOL
94 WINAPI
95 IntersectRect(LPRECT lprcDst,
96 CONST RECT *lprcSrc1,
97 CONST RECT *lprcSrc2)
98 {
99 if (lprcDst == NULL || lprcSrc1 == NULL || lprcSrc2 == NULL)
100 return FALSE;
101
102 if (IsRectEmpty(lprcSrc1) || IsRectEmpty(lprcSrc2) ||
103 lprcSrc1->left >= lprcSrc2->right ||
104 lprcSrc2->left >= lprcSrc1->right ||
105 lprcSrc1->top >= lprcSrc2->bottom ||
106 lprcSrc2->top >= lprcSrc1->bottom)
107 {
108 SetRectEmpty(lprcDst);
109 return FALSE;
110 }
111
112 lprcDst->left = max(lprcSrc1->left, lprcSrc2->left);
113 lprcDst->right = min(lprcSrc1->right, lprcSrc2->right);
114 lprcDst->top = max(lprcSrc1->top, lprcSrc2->top);
115 lprcDst->bottom = min(lprcSrc1->bottom, lprcSrc2->bottom);
116
117 return TRUE;
118 }
119
120
121 /*
122 * @implemented
123 */
124 BOOL
125 WINAPI
126 IsRectEmpty(CONST RECT *lprc)
127 {
128 if (lprc == NULL)
129 return TRUE;
130
131 return ((lprc->left >= lprc->right) || (lprc->top >= lprc->bottom));
132 }
133
134
135 /*
136 * @implemented
137 */
138 BOOL
139 WINAPI
140 OffsetRect(LPRECT rect,
141 int dx,
142 int dy)
143 {
144 if (rect == NULL)
145 return FALSE;
146
147 rect->left += dx;
148 rect->top += dy;
149 rect->right += dx;
150 rect->bottom += dy;
151
152 return TRUE;
153 }
154
155
156 /*
157 * @implemented
158 */
159 BOOL
160 WINAPI
161 PtInRect(CONST RECT *lprc,
162 POINT pt)
163 {
164 if (lprc == NULL)
165 return FALSE;
166
167 return((pt.x >= lprc->left) && (pt.x < lprc->right) &&
168 (pt.y >= lprc->top) && (pt.y < lprc->bottom));
169 }
170
171 BOOL
172 WINAPI
173 SetRect(LPRECT lprc,
174 int xLeft,
175 int yTop,
176 int xRight,
177 int yBottom)
178 {
179 if (lprc == NULL)
180 return FALSE;
181
182 lprc->left = xLeft;
183 lprc->top = yTop;
184 lprc->right = xRight;
185 lprc->bottom = yBottom;
186
187 return TRUE;
188 }
189
190
191 /*
192 * @implemented
193 */
194 BOOL
195 WINAPI
196 SetRectEmpty(LPRECT lprc)
197 {
198 if (lprc == NULL)
199 return FALSE;
200
201 lprc->left = lprc->right = lprc->top = lprc->bottom = 0;
202
203 return TRUE;
204 }
205
206
207 /*
208 * @implemented
209 */
210 BOOL
211 WINAPI
212 SubtractRect(LPRECT lprcDst,
213 CONST RECT *lprcSrc1,
214 CONST RECT *lprcSrc2)
215 {
216 RECT tempRect;
217
218 if (lprcDst == NULL || lprcSrc1 == NULL || lprcSrc2 == NULL)
219 return FALSE;
220
221 if (!IntersectRect(&tempRect, lprcSrc1, lprcSrc2))
222 {
223 *lprcDst = *lprcSrc1;
224 return TRUE;
225 }
226
227 if (EqualRect(&tempRect, lprcSrc1))
228 {
229 SetRectEmpty(lprcDst);
230 return FALSE;
231 }
232
233 *lprcDst = *lprcSrc1;
234
235 if (lprcDst->top == tempRect.top && lprcDst->bottom == tempRect.bottom)
236 {
237 if (lprcDst->left == tempRect.left)
238 lprcDst->left = tempRect.right;
239 else if (lprcDst->right == tempRect.right)
240 lprcDst->right = tempRect.left;
241 }
242 else if (lprcDst->left == tempRect.left && lprcDst->right == tempRect.right)
243 {
244 if (lprcDst->top == tempRect.top)
245 lprcDst->top = tempRect.bottom;
246 else if (lprcDst->bottom == tempRect.bottom)
247 lprcDst->bottom = tempRect.top;
248 }
249
250 return TRUE;
251 }
252
253
254 /*
255 * @implemented
256 */
257 BOOL
258 WINAPI
259 UnionRect(LPRECT lprcDst,
260 CONST RECT *lprcSrc1,
261 CONST RECT *lprcSrc2)
262 {
263 if (lprcDst == NULL || lprcSrc1 == NULL || lprcSrc2 == NULL)
264 return FALSE;
265
266 if (IsRectEmpty(lprcSrc1))
267 {
268 if (IsRectEmpty(lprcSrc2))
269 {
270 SetRectEmpty(lprcDst);
271 return FALSE;
272 }
273 else
274 {
275 *lprcDst = *lprcSrc2;
276 }
277 }
278 else
279 {
280 if (IsRectEmpty(lprcSrc2))
281 {
282 *lprcDst = *lprcSrc1;
283 }
284 else
285 {
286 lprcDst->left = min(lprcSrc1->left, lprcSrc2->left);
287 lprcDst->top = min(lprcSrc1->top, lprcSrc2->top);
288 lprcDst->right = max(lprcSrc1->right, lprcSrc2->right);
289 lprcDst->bottom = max(lprcSrc1->bottom, lprcSrc2->bottom);
290 }
291 }
292
293 return TRUE;
294 }