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