Grab the thread object in case the thread terminates with pending
[wine] / windows / rect.c
1 /*
2  * Rectangle-related functions
3  *
4  * Copyright 1993, 1996 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "windef.h"
22 #include "wingdi.h"
23 #include "wine/winuser16.h"
24 #include "winuser.h"
25
26 /***********************************************************************
27  *              SetRect (USER.72)
28  */
29 void WINAPI SetRect16( LPRECT16 rect, INT16 left, INT16 top,
30                        INT16 right, INT16 bottom )
31 {
32     rect->left   = left;
33     rect->right  = right;
34     rect->top    = top;
35     rect->bottom = bottom;
36 }
37
38
39 /***********************************************************************
40  *              SetRect (USER32.@)
41  */
42 BOOL WINAPI SetRect( LPRECT rect, INT left, INT top,
43                        INT right, INT bottom )
44 {
45     if (!rect) return FALSE;
46     rect->left   = left;
47     rect->right  = right;
48     rect->top    = top;
49     rect->bottom = bottom;
50     return TRUE;
51 }
52
53
54 /***********************************************************************
55  *              SetRectEmpty (USER.73)
56  */
57 void WINAPI SetRectEmpty16( LPRECT16 rect )
58 {
59     rect->left = rect->right = rect->top = rect->bottom = 0;
60 }
61
62
63 /***********************************************************************
64  *              SetRectEmpty (USER32.@)
65  */
66 BOOL WINAPI SetRectEmpty( LPRECT rect )
67 {
68     if (!rect) return FALSE;
69     rect->left = rect->right = rect->top = rect->bottom = 0;
70     return TRUE;
71 }
72
73
74 /***********************************************************************
75  *              CopyRect (USER.74)
76  */
77 BOOL16 WINAPI CopyRect16( RECT16 *dest, const RECT16 *src )
78 {
79     *dest = *src;
80     return TRUE;
81 }
82
83
84 /***********************************************************************
85  *              CopyRect (USER32.@)
86  */
87 BOOL WINAPI CopyRect( RECT *dest, const RECT *src )
88 {
89     if (!dest || !src)
90         return FALSE;
91     *dest = *src;
92     return TRUE;
93 }
94
95
96 /***********************************************************************
97  *              IsRectEmpty (USER.75)
98  *
99  * Bug compat: Windows checks for 0 or negative width/height.
100  */
101 BOOL16 WINAPI IsRectEmpty16( const RECT16 *rect )
102 {
103     return ((rect->left >= rect->right) || (rect->top >= rect->bottom));
104 }
105
106
107 /***********************************************************************
108  *              IsRectEmpty (USER32.@)
109  *
110  * Bug compat: Windows checks for 0 or negative width/height.
111  */
112 BOOL WINAPI IsRectEmpty( const RECT *rect )
113 {
114     if (!rect) return TRUE;
115     return ((rect->left >= rect->right) || (rect->top >= rect->bottom));
116 }
117
118
119 /***********************************************************************
120  *              PtInRect (USER.76)
121  */
122 BOOL16 WINAPI PtInRect16( const RECT16 *rect, POINT16 pt )
123 {
124     return ((pt.x >= rect->left) && (pt.x < rect->right) &&
125             (pt.y >= rect->top) && (pt.y < rect->bottom));
126 }
127
128
129 /***********************************************************************
130  *              PtInRect (USER32.@)
131  */
132 BOOL WINAPI PtInRect( const RECT *rect, POINT pt )
133 {
134     if (!rect) return FALSE;
135     return ((pt.x >= rect->left) && (pt.x < rect->right) &&
136             (pt.y >= rect->top) && (pt.y < rect->bottom));
137 }
138
139
140 /***********************************************************************
141  *              OffsetRect (USER.77)
142  */
143 void WINAPI OffsetRect16( LPRECT16 rect, INT16 x, INT16 y )
144 {
145     rect->left   += x;
146     rect->right  += x;
147     rect->top    += y;
148     rect->bottom += y;
149 }
150
151
152 /***********************************************************************
153  *              OffsetRect (USER32.@)
154  */
155 BOOL WINAPI OffsetRect( LPRECT rect, INT x, INT y )
156 {
157     if (!rect) return FALSE;
158     rect->left   += x;
159     rect->right  += x;
160     rect->top    += y;
161     rect->bottom += y;
162     return TRUE;
163 }
164
165
166 /***********************************************************************
167  *              InflateRect (USER.78)
168  */
169 void WINAPI InflateRect16( LPRECT16 rect, INT16 x, INT16 y )
170 {
171     rect->left   -= x;
172     rect->top    -= y;
173     rect->right  += x;
174     rect->bottom += y;
175 }
176
177
178 /***********************************************************************
179  *              InflateRect (USER32.@)
180  */
181 BOOL WINAPI InflateRect( LPRECT rect, INT x, INT y )
182 {
183     if (!rect) return FALSE;
184     rect->left   -= x;
185     rect->top    -= y;
186     rect->right  += x;
187     rect->bottom += y;
188     return TRUE;
189 }
190
191
192 /***********************************************************************
193  *              IntersectRect (USER.79)
194  */
195 BOOL16 WINAPI IntersectRect16( LPRECT16 dest, const RECT16 *src1,
196                                const RECT16 *src2 )
197 {
198     if (IsRectEmpty16(src1) || IsRectEmpty16(src2) ||
199         (src1->left >= src2->right) || (src2->left >= src1->right) ||
200         (src1->top >= src2->bottom) || (src2->top >= src1->bottom))
201     {
202         SetRectEmpty16( dest );
203         return FALSE;
204     }
205     dest->left   = max( src1->left, src2->left );
206     dest->right  = min( src1->right, src2->right );
207     dest->top    = max( src1->top, src2->top );
208     dest->bottom = min( src1->bottom, src2->bottom );
209     return TRUE;
210 }
211
212
213 /***********************************************************************
214  *              IntersectRect (USER32.@)
215  */
216 BOOL WINAPI IntersectRect( LPRECT dest, const RECT *src1,
217                                const RECT *src2 )
218 {
219     if (!dest || !src1 || !src2) return FALSE;
220     if (IsRectEmpty(src1) || IsRectEmpty(src2) ||
221         (src1->left >= src2->right) || (src2->left >= src1->right) ||
222         (src1->top >= src2->bottom) || (src2->top >= src1->bottom))
223     {
224         SetRectEmpty( dest );
225         return FALSE;
226     }
227     dest->left   = max( src1->left, src2->left );
228     dest->right  = min( src1->right, src2->right );
229     dest->top    = max( src1->top, src2->top );
230     dest->bottom = min( src1->bottom, src2->bottom );
231     return TRUE;
232 }
233
234
235 /***********************************************************************
236  *              UnionRect (USER.80)
237  */
238 BOOL16 WINAPI UnionRect16( LPRECT16 dest, const RECT16 *src1,
239                            const RECT16 *src2 )
240 {
241     if (IsRectEmpty16(src1))
242     {
243         if (IsRectEmpty16(src2))
244         {
245             SetRectEmpty16( dest );
246             return FALSE;
247         }
248         else *dest = *src2;
249     }
250     else
251     {
252         if (IsRectEmpty16(src2)) *dest = *src1;
253         else
254         {
255             dest->left   = min( src1->left, src2->left );
256             dest->right  = max( src1->right, src2->right );
257             dest->top    = min( src1->top, src2->top );
258             dest->bottom = max( src1->bottom, src2->bottom );
259         }
260     }
261     return TRUE;
262 }
263
264
265 /***********************************************************************
266  *              UnionRect (USER32.@)
267  */
268 BOOL WINAPI UnionRect( LPRECT dest, const RECT *src1,
269                            const RECT *src2 )
270 {
271     if (!dest) return FALSE;
272     if (IsRectEmpty(src1))
273     {
274         if (IsRectEmpty(src2))
275         {
276             SetRectEmpty( dest );
277             return FALSE;
278         }
279         else *dest = *src2;
280     }
281     else
282     {
283         if (IsRectEmpty(src2)) *dest = *src1;
284         else
285         {
286             dest->left   = min( src1->left, src2->left );
287             dest->right  = max( src1->right, src2->right );
288             dest->top    = min( src1->top, src2->top );
289             dest->bottom = max( src1->bottom, src2->bottom );
290         }
291     }
292     return TRUE;
293 }
294
295
296 /***********************************************************************
297  *              EqualRect (USER.244)
298  */
299 BOOL16 WINAPI EqualRect16( const RECT16* rect1, const RECT16* rect2 )
300 {
301     return ((rect1->left == rect2->left) && (rect1->right == rect2->right) &&
302             (rect1->top == rect2->top) && (rect1->bottom == rect2->bottom));
303 }
304
305
306 /***********************************************************************
307  *              EqualRect (USER32.@)
308  */
309 BOOL WINAPI EqualRect( const RECT* rect1, const RECT* rect2 )
310 {
311     if (!rect1 || !rect2) return FALSE;
312     return ((rect1->left == rect2->left) && (rect1->right == rect2->right) &&
313             (rect1->top == rect2->top) && (rect1->bottom == rect2->bottom));
314 }
315
316
317 /***********************************************************************
318  *              SubtractRect (USER.373)
319  */
320 BOOL16 WINAPI SubtractRect16( LPRECT16 dest, const RECT16 *src1,
321                               const RECT16 *src2 )
322 {
323     RECT16 tmp;
324
325     if (IsRectEmpty16( src1 ))
326     {
327         SetRectEmpty16( dest );
328         return FALSE;
329     }
330     *dest = *src1;
331     if (IntersectRect16( &tmp, src1, src2 ))
332     {
333         if (EqualRect16( &tmp, dest ))
334         {
335             SetRectEmpty16( dest );
336             return FALSE;
337         }
338         if ((tmp.top == dest->top) && (tmp.bottom == dest->bottom))
339         {
340             if (tmp.left == dest->left) dest->left = tmp.right;
341             else if (tmp.right == dest->right) dest->right = tmp.left;
342         }
343         else if ((tmp.left == dest->left) && (tmp.right == dest->right))
344         {
345             if (tmp.top == dest->top) dest->top = tmp.bottom;
346             else if (tmp.bottom == dest->bottom) dest->bottom = tmp.top;
347         }
348     }
349     return TRUE;
350 }
351
352
353 /***********************************************************************
354  *              SubtractRect (USER32.@)
355  */
356 BOOL WINAPI SubtractRect( LPRECT dest, const RECT *src1,
357                               const RECT *src2 )
358 {
359     RECT tmp;
360
361     if (!dest) return FALSE;
362     if (IsRectEmpty( src1 ))
363     {
364         SetRectEmpty( dest );
365         return FALSE;
366     }
367     *dest = *src1;
368     if (IntersectRect( &tmp, src1, src2 ))
369     {
370         if (EqualRect( &tmp, dest ))
371         {
372             SetRectEmpty( dest );
373             return FALSE;
374         }
375         if ((tmp.top == dest->top) && (tmp.bottom == dest->bottom))
376         {
377             if (tmp.left == dest->left) dest->left = tmp.right;
378             else if (tmp.right == dest->right) dest->right = tmp.left;
379         }
380         else if ((tmp.left == dest->left) && (tmp.right == dest->right))
381         {
382             if (tmp.top == dest->top) dest->top = tmp.bottom;
383             else if (tmp.bottom == dest->bottom) dest->bottom = tmp.top;
384         }
385     }
386     return TRUE;
387 }