update for HEAD-2003091401
[reactos.git] / 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$
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 WINBOOL 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 WINBOOL
55 STDCALL
56 EqualRect(
57   CONST RECT *lprc1,
58   CONST RECT *lprc2)
59 {
60   if ((lprc1->left   == lprc2->left)
61    && (lprc1->top    == lprc2->top)
62    && (lprc1->right  == lprc2->right)
63    && (lprc1->bottom == lprc2->bottom))
64   {
65     return TRUE;
66   }
67   /* TODO: return the correct error code. */
68   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
69   return FALSE;
70 }
71
72
73 /*
74  * @implemented
75  */
76 WINBOOL STDCALL
77 InflateRect(LPRECT rect, int dx, int dy)
78 {
79   rect->left -= dx;
80   rect->top -= dy;
81   rect->right += dx;
82   rect->bottom += dy;
83   return(TRUE);
84 }
85
86
87 /*
88  * @implemented
89  */
90 WINBOOL STDCALL
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 WINBOOL STDCALL
116 IsRectEmpty(CONST RECT *lprc)
117 {
118   return((lprc->left >= lprc->right) || (lprc->top >= lprc->bottom));
119 }
120
121
122 /*
123  * @implemented
124  */
125 WINBOOL STDCALL
126 OffsetRect(LPRECT rect, int dx, int dy)
127 {
128   if(rect == NULL)
129     return(FALSE);
130   
131   rect->left += dx;
132   rect->top += dy;
133   rect->right += dx;
134   rect->bottom += dy;
135   return(TRUE);  
136 }
137
138
139 /*
140  * @implemented
141  */
142 WINBOOL STDCALL
143 PtInRect(CONST RECT *lprc, POINT pt)
144 {
145   return((pt.x >= lprc->left) && (pt.x < lprc->right) &&
146          (pt.y >= lprc->top) && (pt.y < lprc->bottom));
147 }
148
149 WINBOOL STDCALL
150 SetRect(LPRECT lprc, int xLeft, int yTop, int xRight, int yBottom)
151 {
152   lprc->left = xLeft;
153   lprc->top = yTop;
154   lprc->right = xRight;
155   lprc->bottom = yBottom;
156   return(TRUE);
157 }
158
159
160 /*
161  * @implemented
162  */
163 BOOL STDCALL
164 SetRectEmpty(LPRECT lprc)
165 {
166   lprc->left = lprc->right = lprc->top = lprc->bottom = 0;
167   return(TRUE);
168 }
169
170
171 /*
172  * @implemented
173  */
174 WINBOOL STDCALL
175 SubtractRect(LPRECT lprcDst, CONST RECT *lprcSrc1, CONST RECT *lprcSrc2)
176 {
177   RECT tempRect;
178   
179   if(lprcDst == NULL || lprcSrc1 == NULL || lprcSrc2 == NULL)
180     return(FALSE);
181   
182   CopyRect(lprcDst, lprcSrc1);
183   
184   if(!IntersectRect(&tempRect, lprcSrc1, lprcSrc2))
185     return(FALSE);
186   
187   if(lprcDst->top == tempRect.top && lprcDst->bottom == tempRect.bottom)
188   {
189     if(lprcDst->left == tempRect.left)
190       lprcDst->left = tempRect.right;
191     else if(lprcDst->right == tempRect.right)
192       lprcDst->right = tempRect.left;
193   }
194   else if(lprcDst->left == tempRect.left && lprcDst->right == tempRect.right)
195   {
196     if(lprcDst->top == tempRect.top)
197       lprcDst->top = tempRect.bottom;
198     else if(lprcDst->right == tempRect.right)
199       lprcDst->right = tempRect.left;
200   }
201   
202   return(TRUE);
203 }
204
205
206 /*
207  * @implemented
208  */
209 WINBOOL STDCALL
210 UnionRect(LPRECT lprcDst, CONST RECT *lprcSrc1, CONST RECT *lprcSrc2)
211 {
212   if (IsRectEmpty(lprcSrc1))
213     {
214       if (IsRectEmpty(lprcSrc2))
215         {
216           SetRectEmpty(lprcDst);
217           return(FALSE);
218         }
219       else
220         {
221           *lprcDst = *lprcSrc2;
222         }
223     }
224   else
225     {
226       if (IsRectEmpty(lprcSrc2))
227         {
228           *lprcDst = *lprcSrc1;
229         }
230       else
231         {
232           lprcDst->left = min(lprcSrc1->left, lprcSrc2->left);
233           lprcDst->top = min(lprcSrc1->top, lprcSrc2->top);
234           lprcDst->right = max(lprcSrc1->right, lprcSrc2->right);
235           lprcDst->bottom = max(lprcSrc1->bottom, lprcSrc2->bottom);
236         }
237     }
238   return(TRUE);
239 }