Some more recursive include fixes/optimizations.
[wine] / windows / rect.c
1 /*
2  * Rectangle-related functions
3  *
4  * Copyright 1993, 1996 Alexandre Julliard
5  *
6  */
7
8 #include "winuser.h"
9 #include "wine/winuser16.h"
10
11
12 /***********************************************************************
13  *           SetRect16    (USER.72)
14  */
15 void WINAPI SetRect16( LPRECT16 rect, INT16 left, INT16 top,
16                        INT16 right, INT16 bottom )
17 {
18     rect->left   = left;
19     rect->right  = right;
20     rect->top    = top;
21     rect->bottom = bottom;
22 }
23
24
25 /***********************************************************************
26  *           SetRect32    (USER32.499)
27  */
28 BOOL32 WINAPI SetRect32( LPRECT32 rect, INT32 left, INT32 top,
29                        INT32 right, INT32 bottom )
30 {
31     rect->left   = left;
32     rect->right  = right;
33     rect->top    = top;
34     rect->bottom = bottom;
35     return TRUE;
36 }
37
38
39 /***********************************************************************
40  *           SetRectEmpty16    (USER.73)
41  */
42 void WINAPI SetRectEmpty16( LPRECT16 rect )
43 {
44     rect->left = rect->right = rect->top = rect->bottom = 0;
45 }
46
47
48 /***********************************************************************
49  *           SetRectEmpty32    (USER32.500)
50  */
51 BOOL32 WINAPI SetRectEmpty32( LPRECT32 rect )
52 {
53     rect->left = rect->right = rect->top = rect->bottom = 0;
54     return TRUE;
55 }
56
57
58 /***********************************************************************
59  *           CopyRect16    (USER.74)
60  */
61 BOOL16 WINAPI CopyRect16( RECT16 *dest, const RECT16 *src )
62 {
63     *dest = *src;
64     return TRUE;
65 }
66
67
68 /***********************************************************************
69  *           CopyRect32    (USER32.62)
70  */
71 BOOL32 WINAPI CopyRect32( RECT32 *dest, const RECT32 *src )
72 {
73     *dest = *src;
74     return TRUE;
75 }
76
77
78 /***********************************************************************
79  *           IsRectEmpty16    (USER.75)
80  */
81 BOOL16 WINAPI IsRectEmpty16( const RECT16 *rect )
82 {
83     return ((rect->left == rect->right) || (rect->top == rect->bottom));
84 }
85
86
87 /***********************************************************************
88  *           IsRectEmpty32    (USER32.347)
89  */
90 BOOL32 WINAPI IsRectEmpty32( const RECT32 *rect )
91 {
92     return ((rect->left == rect->right) || (rect->top == rect->bottom));
93 }
94
95
96 /***********************************************************************
97  *           PtInRect16    (USER.76)
98  */
99 BOOL16 WINAPI PtInRect16( const RECT16 *rect, POINT16 pt )
100 {
101     return ((pt.x >= rect->left) && (pt.x < rect->right) &&
102             (pt.y >= rect->top) && (pt.y < rect->bottom));
103 }
104
105
106 /***********************************************************************
107  *           PtInRect32    (USER32.424)
108  */
109 BOOL32 WINAPI PtInRect32( const RECT32 *rect, POINT32 pt )
110 {
111     return ((pt.x >= rect->left) && (pt.x < rect->right) &&
112             (pt.y >= rect->top) && (pt.y < rect->bottom));
113 }
114
115
116 /***********************************************************************
117  *           OffsetRect16    (USER.77)
118  */
119 void WINAPI OffsetRect16( LPRECT16 rect, INT16 x, INT16 y )
120 {
121     rect->left   += x;
122     rect->right  += x;
123     rect->top    += y;
124     rect->bottom += y;    
125 }
126
127
128 /***********************************************************************
129  *           OffsetRect32    (USER32.406)
130  */
131 BOOL32 WINAPI OffsetRect32( LPRECT32 rect, INT32 x, INT32 y )
132 {
133     rect->left   += x;
134     rect->right  += x;
135     rect->top    += y;
136     rect->bottom += y;    
137     return TRUE;
138 }
139
140
141 /***********************************************************************
142  *           InflateRect16    (USER.78)
143  */
144 void WINAPI InflateRect16( LPRECT16 rect, INT16 x, INT16 y )
145 {
146     rect->left   -= x;
147     rect->top    -= y;
148     rect->right  += x;
149     rect->bottom += y;
150 }
151
152
153 /***********************************************************************
154  *           InflateRect32    (USER32.321)
155  */
156 BOOL32 WINAPI InflateRect32( LPRECT32 rect, INT32 x, INT32 y )
157 {
158     rect->left   -= x;
159     rect->top    -= y;
160     rect->right  += x;
161     rect->bottom += y;
162     return TRUE;
163 }
164
165
166 /***********************************************************************
167  *           IntersectRect16    (USER.79)
168  */
169 BOOL16 WINAPI IntersectRect16( LPRECT16 dest, const RECT16 *src1,
170                                const RECT16 *src2 )
171 {
172     if (IsRectEmpty16(src1) || IsRectEmpty16(src2) ||
173         (src1->left >= src2->right) || (src2->left >= src1->right) ||
174         (src1->top >= src2->bottom) || (src2->top >= src1->bottom))
175     {
176         SetRectEmpty16( dest );
177         return FALSE;
178     }
179     dest->left   = MAX( src1->left, src2->left );
180     dest->right  = MIN( src1->right, src2->right );
181     dest->top    = MAX( src1->top, src2->top );
182     dest->bottom = MIN( src1->bottom, src2->bottom );
183     return TRUE;
184 }
185
186
187 /***********************************************************************
188  *           IntersectRect32    (USER32.327)
189  */
190 BOOL32 WINAPI IntersectRect32( LPRECT32 dest, const RECT32 *src1,
191                                const RECT32 *src2 )
192 {
193     if (IsRectEmpty32(src1) || IsRectEmpty32(src2) ||
194         (src1->left >= src2->right) || (src2->left >= src1->right) ||
195         (src1->top >= src2->bottom) || (src2->top >= src1->bottom))
196     {
197         SetRectEmpty32( dest );
198         return FALSE;
199     }
200     dest->left   = MAX( src1->left, src2->left );
201     dest->right  = MIN( src1->right, src2->right );
202     dest->top    = MAX( src1->top, src2->top );
203     dest->bottom = MIN( src1->bottom, src2->bottom );
204     return TRUE;
205 }
206
207
208 /***********************************************************************
209  *           UnionRect16    (USER.80)
210  */
211 BOOL16 WINAPI UnionRect16( LPRECT16 dest, const RECT16 *src1,
212                            const RECT16 *src2 )
213 {
214     if (IsRectEmpty16(src1))
215     {
216         if (IsRectEmpty16(src2))
217         {
218             SetRectEmpty16( dest );
219             return FALSE;
220         }
221         else *dest = *src2;
222     }
223     else
224     {
225         if (IsRectEmpty16(src2)) *dest = *src1;
226         else
227         {
228             dest->left   = MIN( src1->left, src2->left );
229             dest->right  = MAX( src1->right, src2->right );
230             dest->top    = MIN( src1->top, src2->top );
231             dest->bottom = MAX( src1->bottom, src2->bottom );       
232         }
233     }
234     return TRUE;
235 }
236
237
238 /***********************************************************************
239  *           UnionRect32    (USER32.559)
240  */
241 BOOL32 WINAPI UnionRect32( LPRECT32 dest, const RECT32 *src1,
242                            const RECT32 *src2 )
243 {
244     if (IsRectEmpty32(src1))
245     {
246         if (IsRectEmpty32(src2))
247         {
248             SetRectEmpty32( dest );
249             return FALSE;
250         }
251         else *dest = *src2;
252     }
253     else
254     {
255         if (IsRectEmpty32(src2)) *dest = *src1;
256         else
257         {
258             dest->left   = MIN( src1->left, src2->left );
259             dest->right  = MAX( src1->right, src2->right );
260             dest->top    = MIN( src1->top, src2->top );
261             dest->bottom = MAX( src1->bottom, src2->bottom );       
262         }
263     }
264     return TRUE;
265 }
266
267
268 /***********************************************************************
269  *           EqualRect16    (USER.244)
270  */
271 BOOL16 WINAPI EqualRect16( const RECT16* rect1, const RECT16* rect2 )
272 {
273     return ((rect1->left == rect2->left) && (rect1->right == rect2->right) &&
274             (rect1->top == rect2->top) && (rect1->bottom == rect2->bottom));
275 }
276
277
278 /***********************************************************************
279  *           EqualRect32    (USER32.194)
280  */
281 BOOL32 WINAPI EqualRect32( const RECT32* rect1, const RECT32* rect2 )
282 {
283     return ((rect1->left == rect2->left) && (rect1->right == rect2->right) &&
284             (rect1->top == rect2->top) && (rect1->bottom == rect2->bottom));
285 }
286
287
288 /***********************************************************************
289  *           SubtractRect16    (USER.373)
290  */
291 BOOL16 WINAPI SubtractRect16( LPRECT16 dest, const RECT16 *src1,
292                               const RECT16 *src2 )
293 {
294     RECT16 tmp;
295
296     if (IsRectEmpty16( src1 ))
297     {
298         SetRectEmpty16( dest );
299         return FALSE;
300     }
301     *dest = *src1;
302     if (IntersectRect16( &tmp, src1, src2 ))
303     {
304         if (EqualRect16( &tmp, dest ))
305         {
306             SetRectEmpty16( dest );
307             return FALSE;
308         }
309         if ((tmp.top == dest->top) && (tmp.bottom == dest->bottom))
310         {
311             if (tmp.left == dest->left) dest->left = tmp.right;
312             else if (tmp.right == dest->right) dest->right = tmp.left;
313         }
314         else if ((tmp.left == dest->left) && (tmp.right == dest->right))
315         {
316             if (tmp.top == dest->top) dest->top = tmp.bottom;
317             else if (tmp.bottom == dest->bottom) dest->bottom = tmp.top;
318         }
319     }
320     return TRUE;
321 }
322
323
324 /***********************************************************************
325  *           SubtractRect32    (USER32.536)
326  */
327 BOOL32 WINAPI SubtractRect32( LPRECT32 dest, const RECT32 *src1,
328                               const RECT32 *src2 )
329 {
330     RECT32 tmp;
331
332     if (IsRectEmpty32( src1 ))
333     {
334         SetRectEmpty32( dest );
335         return FALSE;
336     }
337     *dest = *src1;
338     if (IntersectRect32( &tmp, src1, src2 ))
339     {
340         if (EqualRect32( &tmp, dest ))
341         {
342             SetRectEmpty32( dest );
343             return FALSE;
344         }
345         if ((tmp.top == dest->top) && (tmp.bottom == dest->bottom))
346         {
347             if (tmp.left == dest->left) dest->left = tmp.right;
348             else if (tmp.right == dest->right) dest->right = tmp.left;
349         }
350         else if ((tmp.left == dest->left) && (tmp.right == dest->right))
351         {
352             if (tmp.top == dest->top) dest->top = tmp.bottom;
353             else if (tmp.bottom == dest->bottom) dest->bottom = tmp.top;
354         }
355     }
356     return TRUE;
357 }