Sync trunk.
[reactos.git] / dll / win32 / 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 /* $Id$
20 *
21 * PROJECT: ReactOS user32.dll
22 * FILE: lib/user32/windows/input.c
23 * PURPOSE: Input
24 * PROGRAMMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
25 * UPDATE HISTORY:
26 * 09-05-2001 CSH Created
27 */
28
29 /* INCLUDES ******************************************************************/
30
31 #include <user32.h>
32
33 #include <wine/debug.h>
34
35 /* FUNCTIONS *****************************************************************/
36
37 /*
38 * @implemented
39 */
40 BOOL
41 WINAPI
42 CopyRect(LPRECT lprcDst,
43 CONST RECT *lprcSrc)
44 {
45 if(lprcDst == NULL || lprcSrc == NULL)
46 return(FALSE);
47
48 *lprcDst = *lprcSrc;
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 rect->left -= dx;
79 rect->top -= dy;
80 rect->right += dx;
81 rect->bottom += dy;
82 return(TRUE);
83 }
84
85
86 /*
87 * @implemented
88 */
89 BOOL
90 WINAPI
91 IntersectRect(LPRECT lprcDst,
92 CONST RECT *lprcSrc1,
93 CONST RECT *lprcSrc2)
94 {
95 if (IsRectEmpty(lprcSrc1) || IsRectEmpty(lprcSrc2) ||
96 lprcSrc1->left >= lprcSrc2->right ||
97 lprcSrc2->left >= lprcSrc1->right ||
98 lprcSrc1->top >= lprcSrc2->bottom ||
99 lprcSrc2->top >= lprcSrc1->bottom)
100 {
101 SetRectEmpty(lprcDst);
102 return(FALSE);
103 }
104 lprcDst->left = max(lprcSrc1->left, lprcSrc2->left);
105 lprcDst->right = min(lprcSrc1->right, lprcSrc2->right);
106 lprcDst->top = max(lprcSrc1->top, lprcSrc2->top);
107 lprcDst->bottom = min(lprcSrc1->bottom, lprcSrc2->bottom);
108 return(TRUE);
109 }
110
111
112 /*
113 * @implemented
114 */
115 BOOL
116 WINAPI
117 IsRectEmpty(CONST RECT *lprc)
118 {
119 return((lprc->left >= lprc->right) || (lprc->top >= lprc->bottom));
120 }
121
122
123 /*
124 * @implemented
125 */
126 BOOL
127 WINAPI
128 OffsetRect(LPRECT rect,
129 int dx,
130 int dy)
131 {
132 if(rect == NULL)
133 return(FALSE);
134
135 rect->left += dx;
136 rect->top += dy;
137 rect->right += dx;
138 rect->bottom += dy;
139 return(TRUE);
140 }
141
142
143 /*
144 * @implemented
145 */
146 BOOL
147 WINAPI
148 PtInRect(CONST RECT *lprc,
149 POINT pt)
150 {
151 return((pt.x >= lprc->left) && (pt.x < lprc->right) &&
152 (pt.y >= lprc->top) && (pt.y < lprc->bottom));
153 }
154
155 BOOL
156 WINAPI
157 SetRect(LPRECT lprc,
158 int xLeft,
159 int yTop,
160 int xRight,
161 int yBottom)
162 {
163 lprc->left = xLeft;
164 lprc->top = yTop;
165 lprc->right = xRight;
166 lprc->bottom = yBottom;
167 return(TRUE);
168 }
169
170
171 /*
172 * @implemented
173 */
174 BOOL
175 WINAPI
176 SetRectEmpty(LPRECT lprc)
177 {
178 lprc->left = lprc->right = lprc->top = lprc->bottom = 0;
179 return(TRUE);
180 }
181
182
183 /*
184 * @implemented
185 */
186 BOOL
187 WINAPI
188 SubtractRect(LPRECT lprcDst,
189 CONST RECT *lprcSrc1,
190 CONST RECT *lprcSrc2)
191 {
192 RECT tempRect;
193
194 if(lprcDst == NULL || lprcSrc1 == NULL || lprcSrc2 == NULL)
195 return(FALSE);
196
197 CopyRect(lprcDst, lprcSrc1);
198
199 if(!IntersectRect(&tempRect, lprcSrc1, lprcSrc2))
200 return(TRUE);
201
202 if (EqualRect(&tempRect, lprcDst))
203 {
204 SetRectEmpty(lprcDst);
205 return FALSE;
206 }
207 if(lprcDst->top == tempRect.top && lprcDst->bottom == tempRect.bottom)
208 {
209 if(lprcDst->left == tempRect.left)
210 lprcDst->left = tempRect.right;
211 else if(lprcDst->right == tempRect.right)
212 lprcDst->right = tempRect.left;
213 }
214 else if(lprcDst->left == tempRect.left && lprcDst->right == tempRect.right)
215 {
216 if(lprcDst->top == tempRect.top)
217 lprcDst->top = tempRect.bottom;
218 else if(lprcDst->right == tempRect.right)
219 lprcDst->right = tempRect.left;
220 }
221
222 return(TRUE);
223 }
224
225
226 /*
227 * @implemented
228 */
229 BOOL
230 WINAPI
231 UnionRect(LPRECT lprcDst,
232 CONST RECT *lprcSrc1,
233 CONST RECT *lprcSrc2)
234 {
235 if (IsRectEmpty(lprcSrc1))
236 {
237 if (IsRectEmpty(lprcSrc2))
238 {
239 SetRectEmpty(lprcDst);
240 return(FALSE);
241 }
242 else
243 {
244 *lprcDst = *lprcSrc2;
245 }
246 }
247 else
248 {
249 if (IsRectEmpty(lprcSrc2))
250 {
251 *lprcDst = *lprcSrc1;
252 }
253 else
254 {
255 lprcDst->left = min(lprcSrc1->left, lprcSrc2->left);
256 lprcDst->top = min(lprcSrc1->top, lprcSrc2->top);
257 lprcDst->right = max(lprcSrc1->right, lprcSrc2->right);
258 lprcDst->bottom = max(lprcSrc1->bottom, lprcSrc2->bottom);
259 }
260 }
261
262 return(TRUE);
263 }