1093582a88488632aa821df57abb1b402a2cb666
[reactos.git] / reactos / lib / 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: rect.c,v 1.15 2004/05/14 17:02:40 navaraf Exp $
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 <windows.h>
32 #include <user32.h>
33 #include <debug.h>
34
35 /* FUNCTIONS *****************************************************************/
36
37 /*
38 * @implemented
39 */
40 BOOL STDCALL
41 CopyRect(LPRECT lprcDst, CONST RECT *lprcSrc)
42 {
43 if(lprcDst == NULL || lprcSrc == NULL)
44 return(FALSE);
45
46 *lprcDst = *lprcSrc;
47 return(TRUE);
48 }
49
50
51 /*
52 * @implemented
53 */
54 BOOL STDCALL
55 EqualRect(
56 CONST RECT *lprc1,
57 CONST RECT *lprc2)
58 {
59 if (lprc1 == NULL || lprc2 == NULL)
60 return FALSE;
61
62 return (lprc1->left == lprc2->left) && (lprc1->top == lprc2->top) &&
63 (lprc1->right == lprc2->right) && (lprc1->bottom == lprc2->bottom);
64 }
65
66
67 /*
68 * @implemented
69 */
70 BOOL STDCALL
71 InflateRect(LPRECT rect, int dx, int dy)
72 {
73 rect->left -= dx;
74 rect->top -= dy;
75 rect->right += dx;
76 rect->bottom += dy;
77 return(TRUE);
78 }
79
80
81 /*
82 * @implemented
83 */
84 BOOL STDCALL
85 IntersectRect(LPRECT lprcDst,
86 CONST RECT *lprcSrc1,
87 CONST RECT *lprcSrc2)
88 {
89 if (IsRectEmpty(lprcSrc1) || IsRectEmpty(lprcSrc2) ||
90 lprcSrc1->left >= lprcSrc2->right ||
91 lprcSrc2->left >= lprcSrc1->right ||
92 lprcSrc1->top >= lprcSrc2->bottom ||
93 lprcSrc2->top >= lprcSrc1->bottom)
94 {
95 SetRectEmpty(lprcDst);
96 return(FALSE);
97 }
98 lprcDst->left = max(lprcSrc1->left, lprcSrc2->left);
99 lprcDst->right = min(lprcSrc1->right, lprcSrc2->right);
100 lprcDst->top = max(lprcSrc1->top, lprcSrc2->top);
101 lprcDst->bottom = min(lprcSrc1->bottom, lprcSrc2->bottom);
102 return(TRUE);
103 }
104
105
106 /*
107 * @implemented
108 */
109 BOOL STDCALL
110 IsRectEmpty(CONST RECT *lprc)
111 {
112 return((lprc->left >= lprc->right) || (lprc->top >= lprc->bottom));
113 }
114
115
116 /*
117 * @implemented
118 */
119 BOOL STDCALL
120 OffsetRect(LPRECT rect, int dx, int dy)
121 {
122 if(rect == NULL)
123 return(FALSE);
124
125 rect->left += dx;
126 rect->top += dy;
127 rect->right += dx;
128 rect->bottom += dy;
129 return(TRUE);
130 }
131
132
133 /*
134 * @implemented
135 */
136 BOOL STDCALL
137 PtInRect(CONST RECT *lprc, POINT pt)
138 {
139 return((pt.x >= lprc->left) && (pt.x < lprc->right) &&
140 (pt.y >= lprc->top) && (pt.y < lprc->bottom));
141 }
142
143 BOOL STDCALL
144 SetRect(LPRECT lprc, int xLeft, int yTop, int xRight, int yBottom)
145 {
146 lprc->left = xLeft;
147 lprc->top = yTop;
148 lprc->right = xRight;
149 lprc->bottom = yBottom;
150 return(TRUE);
151 }
152
153
154 /*
155 * @implemented
156 */
157 BOOL STDCALL
158 SetRectEmpty(LPRECT lprc)
159 {
160 lprc->left = lprc->right = lprc->top = lprc->bottom = 0;
161 return(TRUE);
162 }
163
164
165 /*
166 * @implemented
167 */
168 BOOL STDCALL
169 SubtractRect(LPRECT lprcDst, CONST RECT *lprcSrc1, CONST RECT *lprcSrc2)
170 {
171 RECT tempRect;
172
173 if(lprcDst == NULL || lprcSrc1 == NULL || lprcSrc2 == NULL)
174 return(FALSE);
175
176 CopyRect(lprcDst, lprcSrc1);
177
178 if(!IntersectRect(&tempRect, lprcSrc1, lprcSrc2))
179 return(TRUE);
180
181 if (EqualRect(&tempRect, lprcDst))
182 {
183 SetRectEmpty(lprcDst);
184 return FALSE;
185 }
186 if(lprcDst->top == tempRect.top && lprcDst->bottom == tempRect.bottom)
187 {
188 if(lprcDst->left == tempRect.left)
189 lprcDst->left = tempRect.right;
190 else if(lprcDst->right == tempRect.right)
191 lprcDst->right = tempRect.left;
192 }
193 else if(lprcDst->left == tempRect.left && lprcDst->right == tempRect.right)
194 {
195 if(lprcDst->top == tempRect.top)
196 lprcDst->top = tempRect.bottom;
197 else if(lprcDst->right == tempRect.right)
198 lprcDst->right = tempRect.left;
199 }
200
201 return(TRUE);
202 }
203
204
205 /*
206 * @implemented
207 */
208 BOOL STDCALL
209 UnionRect(LPRECT lprcDst, CONST RECT *lprcSrc1, CONST RECT *lprcSrc2)
210 {
211 if (IsRectEmpty(lprcSrc1))
212 {
213 if (IsRectEmpty(lprcSrc2))
214 {
215 SetRectEmpty(lprcDst);
216 return(FALSE);
217 }
218 else
219 {
220 *lprcDst = *lprcSrc2;
221 }
222 }
223 else
224 {
225 if (IsRectEmpty(lprcSrc2))
226 {
227 *lprcDst = *lprcSrc1;
228 }
229 else
230 {
231 lprcDst->left = min(lprcSrc1->left, lprcSrc2->left);
232 lprcDst->top = min(lprcSrc1->top, lprcSrc2->top);
233 lprcDst->right = max(lprcSrc1->right, lprcSrc2->right);
234 lprcDst->bottom = max(lprcSrc1->bottom, lprcSrc2->bottom);
235 }
236 }
237 return(TRUE);
238 }