:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[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 WINBOOL STDCALL
38 CopyRect(LPRECT lprcDst, CONST RECT *lprcSrc)
39 {
40   if(lprcDst == NULL || lprcSrc == NULL)
41     return(FALSE);
42   
43   *lprcDst = *lprcSrc;
44   return(TRUE);
45 }
46
47 WINBOOL
48 STDCALL
49 EqualRect(
50   CONST RECT *lprc1,
51   CONST RECT *lprc2)
52 {
53   if ((lprc1->left   == lprc2->left)
54    && (lprc1->top    == lprc2->top)
55    && (lprc1->right  == lprc2->right)
56    && (lprc1->bottom == lprc2->bottom))
57   {
58     return TRUE;
59   }
60   /* TODO: return the correct error code. */
61   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
62   return FALSE;
63 }
64
65 WINBOOL STDCALL
66 InflateRect(LPRECT rect, int dx, int dy)
67 {
68   rect->left -= dx;
69   rect->top -= dy;
70   rect->right += dx;
71   rect->bottom += dy;
72   return(TRUE);
73 }
74
75 WINBOOL STDCALL
76 IntersectRect(LPRECT lprcDst,
77               CONST RECT *lprcSrc1,
78               CONST RECT *lprcSrc2)
79 {
80   if (IsRectEmpty(lprcSrc1) || IsRectEmpty(lprcSrc2) ||
81       lprcSrc1->left >= lprcSrc2->right || 
82       lprcSrc2->left >= lprcSrc1->right ||
83       lprcSrc1->top >= lprcSrc2->bottom || 
84       lprcSrc2->top >= lprcSrc1->bottom)
85     {
86       SetRectEmpty(lprcDst);
87       return(FALSE);
88     }
89   lprcDst->left = max(lprcSrc1->left, lprcSrc2->left);
90   lprcDst->right = min(lprcSrc1->right, lprcSrc2->right);
91   lprcDst->top = max(lprcSrc1->top, lprcSrc2->top);
92   lprcDst->bottom = min(lprcSrc1->bottom, lprcSrc2->bottom);
93   return(TRUE);
94 }
95
96 WINBOOL STDCALL
97 IsRectEmpty(CONST RECT *lprc)
98 {
99   return((lprc->left >= lprc->right) || (lprc->top >= lprc->bottom));
100 }
101
102 WINBOOL STDCALL
103 OffsetRect(LPRECT rect, int dx, int dy)
104 {
105   if(rect == NULL)
106     return(FALSE);
107   
108   rect->left += dx;
109   rect->top += dy;
110   rect->right += dx;
111   rect->bottom += dy;
112   return(TRUE);  
113 }
114
115 WINBOOL STDCALL
116 PtInRect(CONST RECT *lprc, POINT pt)
117 {
118   return((pt.x >= lprc->left) && (pt.x < lprc->right) &&
119          (pt.y >= lprc->top) && (pt.y < lprc->bottom));
120 }
121
122 WINBOOL STDCALL
123 SetRect(LPRECT lprc, int xLeft, int yTop, int xRight, int yBottom)
124 {
125   lprc->left = xLeft;
126   lprc->top = yTop;
127   lprc->right = xRight;
128   lprc->bottom = yBottom;
129   return(TRUE);
130 }
131
132 BOOL STDCALL
133 SetRectEmpty(LPRECT lprc)
134 {
135   lprc->left = lprc->right = lprc->top = lprc->bottom = 0;
136   return(TRUE);
137 }
138
139 WINBOOL STDCALL
140 SubtractRect(LPRECT lprcDst, CONST RECT *lprcSrc1, CONST RECT *lprcSrc2)
141 {
142   RECT tempRect;
143   
144   if(lprcDst == NULL || lprcSrc1 == NULL || lprcSrc2 == NULL)
145     return(FALSE);
146   
147   CopyRect(lprcDst, lprcSrc1);
148   
149   if(!IntersectRect(&tempRect, lprcSrc1, lprcSrc2))
150     return(FALSE);
151   
152   if(lprcDst->top == tempRect.top && lprcDst->bottom == tempRect.bottom)
153   {
154     if(lprcDst->left == tempRect.left)
155       lprcDst->left = tempRect.right;
156     else if(lprcDst->right == tempRect.right)
157       lprcDst->right = tempRect.left;
158   }
159   else if(lprcDst->left == tempRect.left && lprcDst->right == tempRect.right)
160   {
161     if(lprcDst->top == tempRect.top)
162       lprcDst->top = tempRect.bottom;
163     else if(lprcDst->right == tempRect.right)
164       lprcDst->right = tempRect.left;
165   }
166   
167   return(TRUE);
168 }
169
170 WINBOOL STDCALL
171 UnionRect(LPRECT lprcDst, CONST RECT *lprcSrc1, CONST RECT *lprcSrc2)
172 {
173   if (IsRectEmpty(lprcSrc1))
174     {
175       if (IsRectEmpty(lprcSrc2))
176         {
177           SetRectEmpty(lprcDst);
178           return(FALSE);
179         }
180       else
181         {
182           *lprcDst = *lprcSrc2;
183         }
184     }
185   else
186     {
187       if (IsRectEmpty(lprcSrc2))
188         {
189           *lprcDst = *lprcSrc1;
190         }
191       else
192         {
193           lprcDst->left = min(lprcSrc1->left, lprcSrc2->left);
194           lprcDst->top = min(lprcSrc1->top, lprcSrc2->top);
195           lprcDst->right = max(lprcSrc1->right, lprcSrc2->right);
196           lprcDst->bottom = max(lprcSrc1->bottom, lprcSrc2->bottom);
197         }
198     }
199   return(TRUE);
200 }