We need to at least refresh the window menu in ChildActivate, so for
[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 <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "wine/winuser16.h"
27 #include "winuser.h"
28
29 /***********************************************************************
30  *              SetRect (USER.72)
31  */
32 void WINAPI SetRect16( LPRECT16 rect, INT16 left, INT16 top,
33                        INT16 right, INT16 bottom )
34 {
35     rect->left   = left;
36     rect->right  = right;
37     rect->top    = top;
38     rect->bottom = bottom;
39 }
40
41
42 /***********************************************************************
43  *              SetRect (USER32.@)
44  */
45 BOOL WINAPI SetRect( LPRECT rect, INT left, INT top,
46                        INT right, INT bottom )
47 {
48     if (!rect) return FALSE;
49     rect->left   = left;
50     rect->right  = right;
51     rect->top    = top;
52     rect->bottom = bottom;
53     return TRUE;
54 }
55
56
57 /***********************************************************************
58  *              SetRectEmpty (USER.73)
59  */
60 void WINAPI SetRectEmpty16( LPRECT16 rect )
61 {
62     rect->left = rect->right = rect->top = rect->bottom = 0;
63 }
64
65
66 /***********************************************************************
67  *              SetRectEmpty (USER32.@)
68  */
69 BOOL WINAPI SetRectEmpty( LPRECT rect )
70 {
71     if (!rect) return FALSE;
72     rect->left = rect->right = rect->top = rect->bottom = 0;
73     return TRUE;
74 }
75
76
77 /***********************************************************************
78  *              CopyRect (USER.74)
79  */
80 BOOL16 WINAPI CopyRect16( RECT16 *dest, const RECT16 *src )
81 {
82     *dest = *src;
83     return TRUE;
84 }
85
86
87 /***********************************************************************
88  *              CopyRect (USER32.@)
89  */
90 BOOL WINAPI CopyRect( RECT *dest, const RECT *src )
91 {
92     if (!dest || !src)
93         return FALSE;
94     *dest = *src;
95     return TRUE;
96 }
97
98
99 /***********************************************************************
100  *              IsRectEmpty (USER.75)
101  *
102  * Bug compat: Windows checks for 0 or negative width/height.
103  */
104 BOOL16 WINAPI IsRectEmpty16( const RECT16 *rect )
105 {
106     return ((rect->left >= rect->right) || (rect->top >= rect->bottom));
107 }
108
109
110 /***********************************************************************
111  *              IsRectEmpty (USER32.@)
112  *
113  * Bug compat: Windows checks for 0 or negative width/height.
114  */
115 BOOL WINAPI IsRectEmpty( const RECT *rect )
116 {
117     if (!rect) return TRUE;
118     return ((rect->left >= rect->right) || (rect->top >= rect->bottom));
119 }
120
121
122 /***********************************************************************
123  *              PtInRect (USER.76)
124  */
125 BOOL16 WINAPI PtInRect16( const RECT16 *rect, POINT16 pt )
126 {
127     return ((pt.x >= rect->left) && (pt.x < rect->right) &&
128             (pt.y >= rect->top) && (pt.y < rect->bottom));
129 }
130
131
132 /***********************************************************************
133  *              PtInRect (USER32.@)
134  */
135 BOOL WINAPI PtInRect( const RECT *rect, POINT pt )
136 {
137     if (!rect) return FALSE;
138     return ((pt.x >= rect->left) && (pt.x < rect->right) &&
139             (pt.y >= rect->top) && (pt.y < rect->bottom));
140 }
141
142
143 /***********************************************************************
144  *              OffsetRect (USER.77)
145  */
146 void WINAPI OffsetRect16( LPRECT16 rect, INT16 x, INT16 y )
147 {
148     rect->left   += x;
149     rect->right  += x;
150     rect->top    += y;
151     rect->bottom += y;
152 }
153
154
155 /***********************************************************************
156  *              OffsetRect (USER32.@)
157  */
158 BOOL WINAPI OffsetRect( LPRECT rect, INT x, INT y )
159 {
160     if (!rect) return FALSE;
161     rect->left   += x;
162     rect->right  += x;
163     rect->top    += y;
164     rect->bottom += y;
165     return TRUE;
166 }
167
168
169 /***********************************************************************
170  *              InflateRect (USER.78)
171  */
172 void WINAPI InflateRect16( LPRECT16 rect, INT16 x, INT16 y )
173 {
174     rect->left   -= x;
175     rect->top    -= y;
176     rect->right  += x;
177     rect->bottom += y;
178 }
179
180
181 /***********************************************************************
182  *              InflateRect (USER32.@)
183  */
184 BOOL WINAPI InflateRect( LPRECT rect, INT x, INT y )
185 {
186     if (!rect) return FALSE;
187     rect->left   -= x;
188     rect->top    -= y;
189     rect->right  += x;
190     rect->bottom += y;
191     return TRUE;
192 }
193
194
195 /***********************************************************************
196  *              IntersectRect (USER.79)
197  */
198 BOOL16 WINAPI IntersectRect16( LPRECT16 dest, const RECT16 *src1,
199                                const RECT16 *src2 )
200 {
201     if (IsRectEmpty16(src1) || IsRectEmpty16(src2) ||
202         (src1->left >= src2->right) || (src2->left >= src1->right) ||
203         (src1->top >= src2->bottom) || (src2->top >= src1->bottom))
204     {
205         SetRectEmpty16( dest );
206         return FALSE;
207     }
208     dest->left   = max( src1->left, src2->left );
209     dest->right  = min( src1->right, src2->right );
210     dest->top    = max( src1->top, src2->top );
211     dest->bottom = min( src1->bottom, src2->bottom );
212     return TRUE;
213 }
214
215
216 /***********************************************************************
217  *              IntersectRect (USER32.@)
218  */
219 BOOL WINAPI IntersectRect( LPRECT dest, const RECT *src1,
220                                const RECT *src2 )
221 {
222     if (!dest || !src1 || !src2) return FALSE;
223     if (IsRectEmpty(src1) || IsRectEmpty(src2) ||
224         (src1->left >= src2->right) || (src2->left >= src1->right) ||
225         (src1->top >= src2->bottom) || (src2->top >= src1->bottom))
226     {
227         SetRectEmpty( dest );
228         return FALSE;
229     }
230     dest->left   = max( src1->left, src2->left );
231     dest->right  = min( src1->right, src2->right );
232     dest->top    = max( src1->top, src2->top );
233     dest->bottom = min( src1->bottom, src2->bottom );
234     return TRUE;
235 }
236
237
238 /***********************************************************************
239  *              UnionRect (USER.80)
240  */
241 BOOL16 WINAPI UnionRect16( LPRECT16 dest, const RECT16 *src1,
242                            const RECT16 *src2 )
243 {
244     if (IsRectEmpty16(src1))
245     {
246         if (IsRectEmpty16(src2))
247         {
248             SetRectEmpty16( dest );
249             return FALSE;
250         }
251         else *dest = *src2;
252     }
253     else
254     {
255         if (IsRectEmpty16(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  *              UnionRect (USER32.@)
270  */
271 BOOL WINAPI UnionRect( LPRECT dest, const RECT *src1,
272                            const RECT *src2 )
273 {
274     if (!dest) return FALSE;
275     if (IsRectEmpty(src1))
276     {
277         if (IsRectEmpty(src2))
278         {
279             SetRectEmpty( dest );
280             return FALSE;
281         }
282         else *dest = *src2;
283     }
284     else
285     {
286         if (IsRectEmpty(src2)) *dest = *src1;
287         else
288         {
289             dest->left   = min( src1->left, src2->left );
290             dest->right  = max( src1->right, src2->right );
291             dest->top    = min( src1->top, src2->top );
292             dest->bottom = max( src1->bottom, src2->bottom );
293         }
294     }
295     return TRUE;
296 }
297
298
299 /***********************************************************************
300  *              EqualRect (USER.244)
301  */
302 BOOL16 WINAPI EqualRect16( const RECT16* rect1, const RECT16* rect2 )
303 {
304     return ((rect1->left == rect2->left) && (rect1->right == rect2->right) &&
305             (rect1->top == rect2->top) && (rect1->bottom == rect2->bottom));
306 }
307
308
309 /***********************************************************************
310  *              EqualRect (USER32.@)
311  */
312 BOOL WINAPI EqualRect( const RECT* rect1, const RECT* rect2 )
313 {
314     if (!rect1 || !rect2) return FALSE;
315     return ((rect1->left == rect2->left) && (rect1->right == rect2->right) &&
316             (rect1->top == rect2->top) && (rect1->bottom == rect2->bottom));
317 }
318
319
320 /***********************************************************************
321  *              SubtractRect (USER.373)
322  */
323 BOOL16 WINAPI SubtractRect16( LPRECT16 dest, const RECT16 *src1,
324                               const RECT16 *src2 )
325 {
326     RECT16 tmp;
327
328     if (IsRectEmpty16( src1 ))
329     {
330         SetRectEmpty16( dest );
331         return FALSE;
332     }
333     *dest = *src1;
334     if (IntersectRect16( &tmp, src1, src2 ))
335     {
336         if (EqualRect16( &tmp, dest ))
337         {
338             SetRectEmpty16( dest );
339             return FALSE;
340         }
341         if ((tmp.top == dest->top) && (tmp.bottom == dest->bottom))
342         {
343             if (tmp.left == dest->left) dest->left = tmp.right;
344             else if (tmp.right == dest->right) dest->right = tmp.left;
345         }
346         else if ((tmp.left == dest->left) && (tmp.right == dest->right))
347         {
348             if (tmp.top == dest->top) dest->top = tmp.bottom;
349             else if (tmp.bottom == dest->bottom) dest->bottom = tmp.top;
350         }
351     }
352     return TRUE;
353 }
354
355
356 /***********************************************************************
357  *              SubtractRect (USER32.@)
358  */
359 BOOL WINAPI SubtractRect( LPRECT dest, const RECT *src1,
360                               const RECT *src2 )
361 {
362     RECT tmp;
363
364     if (!dest) return FALSE;
365     if (IsRectEmpty( src1 ))
366     {
367         SetRectEmpty( dest );
368         return FALSE;
369     }
370     *dest = *src1;
371     if (IntersectRect( &tmp, src1, src2 ))
372     {
373         if (EqualRect( &tmp, dest ))
374         {
375             SetRectEmpty( dest );
376             return FALSE;
377         }
378         if ((tmp.top == dest->top) && (tmp.bottom == dest->bottom))
379         {
380             if (tmp.left == dest->left) dest->left = tmp.right;
381             else if (tmp.right == dest->right) dest->right = tmp.left;
382         }
383         else if ((tmp.left == dest->left) && (tmp.right == dest->right))
384         {
385             if (tmp.top == dest->top) dest->top = tmp.bottom;
386             else if (tmp.bottom == dest->bottom) dest->bottom = tmp.top;
387         }
388     }
389     return TRUE;
390 }