gdi32: Allow the visible region to be zero since this is the case for metafiles.
[wine] / dlls / gdi32 / clipping.c
1 /*
2  * DC clipping functions
3  *
4  * Copyright 1993 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "gdi_private.h"
27 #include "wine/debug.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(clipping);
30
31
32 /***********************************************************************
33  *           get_clip_region
34  *
35  * Return the total clip region (if any).
36  */
37 static inline HRGN get_clip_region( DC * dc )
38 {
39     if (dc->hMetaClipRgn) return dc->hMetaClipRgn;
40     if (dc->hMetaRgn) return dc->hMetaRgn;
41     return dc->hClipRgn;
42 }
43
44
45 /***********************************************************************
46  *           CLIPPING_UpdateGCRegion
47  *
48  * Update the GC clip region when the ClipRgn or VisRgn have changed.
49  */
50 void CLIPPING_UpdateGCRegion( DC * dc )
51 {
52     HRGN clip_rgn;
53
54     /* update the intersection of meta and clip regions */
55     if (dc->hMetaRgn && dc->hClipRgn)
56     {
57         if (!dc->hMetaClipRgn) dc->hMetaClipRgn = CreateRectRgn( 0, 0, 0, 0 );
58         CombineRgn( dc->hMetaClipRgn, dc->hClipRgn, dc->hMetaRgn, RGN_AND );
59         clip_rgn = dc->hMetaClipRgn;
60     }
61     else  /* only one is set, no need for an intersection */
62     {
63         if (dc->hMetaClipRgn) DeleteObject( dc->hMetaClipRgn );
64         dc->hMetaClipRgn = 0;
65         clip_rgn = dc->hMetaRgn ? dc->hMetaRgn : dc->hClipRgn;
66     }
67
68     if (dc->funcs->pSetDeviceClipping)
69         dc->funcs->pSetDeviceClipping( dc->physDev, dc->hVisRgn, clip_rgn );
70 }
71
72 /***********************************************************************
73  *           create_default_clip_region
74  *
75  * Create a default clipping region when none already exists.
76  */
77 static inline void create_default_clip_region( DC * dc )
78 {
79     UINT width, height;
80
81     if (dc->header.type == OBJ_MEMDC)
82     {
83         BITMAP bitmap;
84
85         GetObjectW( dc->hBitmap, sizeof(bitmap), &bitmap );
86         width = bitmap.bmWidth;
87         height = bitmap.bmHeight;
88     }
89     else
90     {
91         width = GetDeviceCaps( dc->hSelf, DESKTOPHORZRES );
92         height = GetDeviceCaps( dc->hSelf, DESKTOPVERTRES );
93     }
94     dc->hClipRgn = CreateRectRgn( 0, 0, width, height );
95 }
96
97
98 /***********************************************************************
99  *           SelectClipRgn    (GDI32.@)
100  */
101 INT WINAPI SelectClipRgn( HDC hdc, HRGN hrgn )
102 {
103     return ExtSelectClipRgn( hdc, hrgn, RGN_COPY );
104 }
105
106
107 /******************************************************************************
108  *              ExtSelectClipRgn        [GDI32.@]
109  */
110 INT WINAPI ExtSelectClipRgn( HDC hdc, HRGN hrgn, INT fnMode )
111 {
112     INT retval;
113     RECT rect;
114     DC * dc = get_dc_ptr( hdc );
115     if (!dc) return ERROR;
116
117     TRACE("%p %p %d\n", hdc, hrgn, fnMode );
118
119     update_dc( dc );
120     if (dc->funcs->pExtSelectClipRgn)
121     {
122         retval = dc->funcs->pExtSelectClipRgn( dc->physDev, hrgn, fnMode );
123         release_dc_ptr( dc );
124         return retval;
125     }
126
127     if (!hrgn)
128     {
129         if (fnMode == RGN_COPY)
130         {
131             if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
132             dc->hClipRgn = 0;
133         }
134         else
135         {
136             FIXME("Unimplemented: hrgn NULL in mode: %d\n", fnMode);
137             release_dc_ptr( dc );
138             return ERROR;
139         }
140     }
141     else
142     {
143         if (!dc->hClipRgn)
144             create_default_clip_region( dc );
145
146         if(fnMode == RGN_COPY)
147             CombineRgn( dc->hClipRgn, hrgn, 0, fnMode );
148         else
149             CombineRgn( dc->hClipRgn, dc->hClipRgn, hrgn, fnMode);
150     }
151
152     CLIPPING_UpdateGCRegion( dc );
153     release_dc_ptr( dc );
154
155     return GetClipBox(hdc, &rect);
156 }
157
158 /***********************************************************************
159  *           SelectVisRgn   (GDI32.@)
160  *
161  * Note: not exported on Windows, only the 16-bit version is exported.
162  */
163 INT WINAPI SelectVisRgn( HDC hdc, HRGN hrgn )
164 {
165     int retval;
166     DC * dc;
167
168     if (!hrgn) return ERROR;
169     if (!(dc = get_dc_ptr( hdc ))) return ERROR;
170
171     TRACE("%p %p\n", hdc, hrgn );
172
173     dc->dirty = 0;
174
175     retval = CombineRgn( dc->hVisRgn, hrgn, 0, RGN_COPY );
176     CLIPPING_UpdateGCRegion( dc );
177     release_dc_ptr( dc );
178     return retval;
179 }
180
181
182 /***********************************************************************
183  *           OffsetClipRgn    (GDI32.@)
184  */
185 INT WINAPI OffsetClipRgn( HDC hdc, INT x, INT y )
186 {
187     INT ret = SIMPLEREGION;
188     DC *dc = get_dc_ptr( hdc );
189     if (!dc) return ERROR;
190
191     TRACE("%p %d,%d\n", hdc, x, y );
192
193     update_dc( dc );
194     if(dc->funcs->pOffsetClipRgn)
195     {
196         ret = dc->funcs->pOffsetClipRgn( dc->physDev, x, y );
197         /* FIXME: ret is just a success flag, we should return a proper value */
198     }
199     else if (dc->hClipRgn) {
200         ret = OffsetRgn( dc->hClipRgn, MulDiv( x, dc->vportExtX, dc->wndExtX ),
201                          MulDiv( y, dc->vportExtY, dc->wndExtY ) );
202         CLIPPING_UpdateGCRegion( dc );
203     }
204     release_dc_ptr( dc );
205     return ret;
206 }
207
208
209 /***********************************************************************
210  *           ExcludeClipRect    (GDI32.@)
211  */
212 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
213                                 INT right, INT bottom )
214 {
215     HRGN newRgn;
216     INT ret;
217     DC *dc = get_dc_ptr( hdc );
218     if (!dc) return ERROR;
219
220     TRACE("%p %dx%d,%dx%d\n", hdc, left, top, right, bottom );
221
222     update_dc( dc );
223     if(dc->funcs->pExcludeClipRect)
224     {
225         ret = dc->funcs->pExcludeClipRect( dc->physDev, left, top, right, bottom );
226         /* FIXME: ret is just a success flag, we should return a proper value */
227     }
228     else
229     {
230         POINT pt[2];
231
232         pt[0].x = left;
233         pt[0].y = top;
234         pt[1].x = right;
235         pt[1].y = bottom;
236         LPtoDP( hdc, pt, 2 );
237         if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
238         else
239         {
240             if (!dc->hClipRgn)
241                 create_default_clip_region( dc );
242             ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_DIFF );
243             DeleteObject( newRgn );
244         }
245         if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
246     }
247     release_dc_ptr( dc );
248     return ret;
249 }
250
251
252 /***********************************************************************
253  *           IntersectClipRect    (GDI32.@)
254  */
255 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
256 {
257     INT ret;
258     DC *dc = get_dc_ptr( hdc );
259     if (!dc) return ERROR;
260
261     TRACE("%p %d,%d - %d,%d\n", hdc, left, top, right, bottom );
262
263     update_dc( dc );
264     if(dc->funcs->pIntersectClipRect)
265     {
266         ret = dc->funcs->pIntersectClipRect( dc->physDev, left, top, right, bottom );
267         /* FIXME: ret is just a success flag, we should return a proper value */
268     }
269     else
270     {
271         POINT pt[2];
272
273         pt[0].x = left;
274         pt[0].y = top;
275         pt[1].x = right;
276         pt[1].y = bottom;
277
278         LPtoDP( hdc, pt, 2 );
279
280         if (!dc->hClipRgn)
281         {
282             dc->hClipRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y );
283             ret = SIMPLEREGION;
284         }
285         else
286         {
287             HRGN newRgn;
288
289             if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
290             else
291             {
292                 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_AND );
293                 DeleteObject( newRgn );
294             }
295         }
296         if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
297     }
298     release_dc_ptr( dc );
299     return ret;
300 }
301
302
303 /***********************************************************************
304  *           PtVisible    (GDI32.@)
305  */
306 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
307 {
308     POINT pt;
309     BOOL ret;
310     HRGN clip;
311     DC *dc = get_dc_ptr( hdc );
312
313     TRACE("%p %d,%d\n", hdc, x, y );
314     if (!dc) return FALSE;
315
316     pt.x = x;
317     pt.y = y;
318     LPtoDP( hdc, &pt, 1 );
319     update_dc( dc );
320     ret = PtInRegion( dc->hVisRgn, pt.x, pt.y );
321     if (ret && (clip = get_clip_region(dc))) ret = PtInRegion( clip, pt.x, pt.y );
322     release_dc_ptr( dc );
323     return ret;
324 }
325
326
327 /***********************************************************************
328  *           RectVisible    (GDI32.@)
329  */
330 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
331 {
332     RECT tmpRect;
333     BOOL ret;
334     HRGN clip;
335     DC *dc = get_dc_ptr( hdc );
336     if (!dc) return FALSE;
337     TRACE("%p %d,%dx%d,%d\n", hdc, rect->left, rect->top, rect->right, rect->bottom );
338
339     tmpRect = *rect;
340     LPtoDP( hdc, (POINT *)&tmpRect, 2 );
341
342     update_dc( dc );
343     if ((clip = get_clip_region(dc)))
344     {
345         HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
346         CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
347         ret = RectInRegion( hrgn, &tmpRect );
348         DeleteObject( hrgn );
349     }
350     else ret = RectInRegion( dc->hVisRgn, &tmpRect );
351     release_dc_ptr( dc );
352     return ret;
353 }
354
355
356 /***********************************************************************
357  *           GetClipBox    (GDI32.@)
358  */
359 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
360 {
361     INT ret;
362     HRGN clip;
363     DC *dc = get_dc_ptr( hdc );
364     if (!dc) return ERROR;
365
366     update_dc( dc );
367     if ((clip = get_clip_region(dc)))
368     {
369         HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
370         CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
371         ret = GetRgnBox( hrgn, rect );
372         DeleteObject( hrgn );
373     }
374     else ret = GetRgnBox( dc->hVisRgn, rect );
375     DPtoLP( hdc, (LPPOINT)rect, 2 );
376     release_dc_ptr( dc );
377     return ret;
378 }
379
380
381 /***********************************************************************
382  *           GetClipRgn  (GDI32.@)
383  */
384 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
385 {
386     INT ret = -1;
387     DC * dc;
388     if (hRgn && (dc = get_dc_ptr( hdc )))
389     {
390       if( dc->hClipRgn )
391       {
392           if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR ) ret = 1;
393       }
394       else ret = 0;
395       release_dc_ptr( dc );
396     }
397     return ret;
398 }
399
400
401 /***********************************************************************
402  *           GetMetaRgn    (GDI32.@)
403  */
404 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
405 {
406     INT ret = 0;
407     DC * dc = get_dc_ptr( hdc );
408
409     if (dc)
410     {
411         if (dc->hMetaRgn && CombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY ) != ERROR)
412             ret = 1;
413         release_dc_ptr( dc );
414     }
415     return ret;
416 }
417
418
419 /***********************************************************************
420  * GetRandomRgn [GDI32.@]
421  *
422  * NOTES
423  *     This function is documented in MSDN online for the case of
424  *     iCode == SYSRGN (4).
425  *
426  *     For iCode == 1 it should return the clip region
427  *                  2 "    "       "   the meta region
428  *                  3 "    "       "   the intersection of the clip with
429  *                                     the meta region (== 'Rao' region).
430  *
431  *     See http://www.codeproject.com/gdi/cliprgnguide.asp
432  */
433 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, INT iCode)
434 {
435     HRGN rgn;
436     DC *dc = get_dc_ptr( hDC );
437
438     if (!dc) return -1;
439
440     switch (iCode)
441     {
442     case 1:
443         rgn = dc->hClipRgn;
444         break;
445     case 2:
446         rgn = dc->hMetaRgn;
447         break;
448     case 3:
449         rgn = dc->hMetaClipRgn;
450         if(!rgn) rgn = dc->hClipRgn;
451         if(!rgn) rgn = dc->hMetaRgn;
452         break;
453     case SYSRGN: /* == 4 */
454         update_dc( dc );
455         rgn = dc->hVisRgn;
456         break;
457     default:
458         WARN("Unknown code %d\n", iCode);
459         release_dc_ptr( dc );
460         return -1;
461     }
462     if (rgn) CombineRgn( hRgn, rgn, 0, RGN_COPY );
463     release_dc_ptr( dc );
464
465     /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
466     if (iCode == SYSRGN && !(GetVersion() & 0x80000000))
467     {
468         POINT org;
469         GetDCOrgEx( hDC, &org );
470         OffsetRgn( hRgn, org.x, org.y );
471     }
472     return (rgn != 0);
473 }
474
475
476 /***********************************************************************
477  *           SetMetaRgn    (GDI32.@)
478  */
479 INT WINAPI SetMetaRgn( HDC hdc )
480 {
481     INT ret;
482     RECT dummy;
483     DC *dc = get_dc_ptr( hdc );
484
485     if (!dc) return ERROR;
486
487     if (dc->hMetaClipRgn)
488     {
489         /* the intersection becomes the new meta region */
490         DeleteObject( dc->hMetaRgn );
491         DeleteObject( dc->hClipRgn );
492         dc->hMetaRgn = dc->hMetaClipRgn;
493         dc->hClipRgn = 0;
494         dc->hMetaClipRgn = 0;
495     }
496     else if (dc->hClipRgn)
497     {
498         dc->hMetaRgn = dc->hClipRgn;
499         dc->hClipRgn = 0;
500     }
501     /* else nothing to do */
502
503     /* Note: no need to call CLIPPING_UpdateGCRegion, the overall clip region hasn't changed */
504
505     ret = GetRgnBox( dc->hMetaRgn, &dummy );
506     release_dc_ptr( dc );
507     return ret;
508 }