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