oleaut32: Fix handle cast warnings on 64-bit.
[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 "wownt32.h"
27 #include "gdi_private.h"
28 #include "wine/debug.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(clipping);
31
32
33 /***********************************************************************
34  *           get_clip_region
35  *
36  * Return the total clip region (if any).
37  */
38 static inline HRGN get_clip_region( DC * dc )
39 {
40     if (dc->hMetaClipRgn) return dc->hMetaClipRgn;
41     if (dc->hMetaRgn) return dc->hMetaRgn;
42     return dc->hClipRgn;
43 }
44
45
46 /***********************************************************************
47  *           CLIPPING_UpdateGCRegion
48  *
49  * Update the GC clip region when the ClipRgn or VisRgn have changed.
50  */
51 void CLIPPING_UpdateGCRegion( DC * dc )
52 {
53     HRGN clip_rgn;
54
55     if (!dc->hVisRgn)
56     {
57         ERR("hVisRgn is zero. Please report this.\n" );
58         exit(1);
59     }
60
61     /* update the intersection of meta and clip regions */
62     if (dc->hMetaRgn && dc->hClipRgn)
63     {
64         if (!dc->hMetaClipRgn) dc->hMetaClipRgn = CreateRectRgn( 0, 0, 0, 0 );
65         CombineRgn( dc->hMetaClipRgn, dc->hClipRgn, dc->hMetaRgn, RGN_AND );
66         clip_rgn = dc->hMetaClipRgn;
67     }
68     else  /* only one is set, no need for an intersection */
69     {
70         if (dc->hMetaClipRgn) DeleteObject( dc->hMetaClipRgn );
71         dc->hMetaClipRgn = 0;
72         clip_rgn = dc->hMetaRgn ? dc->hMetaRgn : dc->hClipRgn;
73     }
74
75     if (dc->funcs->pSetDeviceClipping)
76         dc->funcs->pSetDeviceClipping( dc->physDev, dc->hVisRgn, clip_rgn );
77 }
78
79 /***********************************************************************
80  *           create_default_clip_region
81  *
82  * Create a default clipping region when none already exists.
83  */
84 static inline void create_default_clip_region( DC * dc )
85 {
86     UINT width, height;
87
88     if (GDIMAGIC( dc->header.wMagic ) == MEMORY_DC_MAGIC)
89     {
90         BITMAP bitmap;
91
92         GetObjectW( dc->hBitmap, sizeof(bitmap), &bitmap );
93         width = bitmap.bmWidth;
94         height = bitmap.bmHeight;
95     }
96     else
97     {
98         width = GetDeviceCaps( dc->hSelf, DESKTOPHORZRES );
99         height = GetDeviceCaps( dc->hSelf, DESKTOPVERTRES );
100     }
101     dc->hClipRgn = CreateRectRgn( 0, 0, width, height );
102 }
103
104
105 /***********************************************************************
106  *           SelectClipRgn    (GDI32.@)
107  */
108 INT WINAPI SelectClipRgn( HDC hdc, HRGN hrgn )
109 {
110     return ExtSelectClipRgn( hdc, hrgn, RGN_COPY );
111 }
112
113
114 /******************************************************************************
115  *              ExtSelectClipRgn        [GDI32.@]
116  */
117 INT WINAPI ExtSelectClipRgn( HDC hdc, HRGN hrgn, INT fnMode )
118 {
119     INT retval;
120     RECT rect;
121     DC * dc = get_dc_ptr( hdc );
122     if (!dc) return ERROR;
123
124     TRACE("%p %p %d\n", hdc, hrgn, fnMode );
125
126     update_dc( dc );
127     if (dc->funcs->pExtSelectClipRgn)
128     {
129         retval = dc->funcs->pExtSelectClipRgn( dc->physDev, hrgn, fnMode );
130         release_dc_ptr( dc );
131         return retval;
132     }
133
134     if (!hrgn)
135     {
136         if (fnMode == RGN_COPY)
137         {
138             if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
139             dc->hClipRgn = 0;
140         }
141         else
142         {
143             FIXME("Unimplemented: hrgn NULL in mode: %d\n", fnMode);
144             release_dc_ptr( dc );
145             return ERROR;
146         }
147     }
148     else
149     {
150         if (!dc->hClipRgn)
151             create_default_clip_region( dc );
152
153         if(fnMode == RGN_COPY)
154             CombineRgn( dc->hClipRgn, hrgn, 0, fnMode );
155         else
156             CombineRgn( dc->hClipRgn, dc->hClipRgn, hrgn, fnMode);
157     }
158
159     CLIPPING_UpdateGCRegion( dc );
160     release_dc_ptr( dc );
161
162     return GetClipBox(hdc, &rect);
163 }
164
165 /***********************************************************************
166  *           SelectVisRgn   (GDI32.@)
167  *
168  * Note: not exported on Windows, only the 16-bit version is exported.
169  */
170 INT WINAPI SelectVisRgn( HDC hdc, HRGN hrgn )
171 {
172     int retval;
173     DC * dc;
174
175     if (!hrgn) return ERROR;
176     if (!(dc = get_dc_ptr( hdc ))) return ERROR;
177
178     TRACE("%p %p\n", hdc, hrgn );
179
180     dc->dirty = 0;
181
182     retval = CombineRgn( dc->hVisRgn, hrgn, 0, RGN_COPY );
183     CLIPPING_UpdateGCRegion( dc );
184     release_dc_ptr( dc );
185     return retval;
186 }
187
188
189 /***********************************************************************
190  *           OffsetClipRgn    (GDI32.@)
191  */
192 INT WINAPI OffsetClipRgn( HDC hdc, INT x, INT y )
193 {
194     INT ret = SIMPLEREGION;
195     DC *dc = get_dc_ptr( hdc );
196     if (!dc) return ERROR;
197
198     TRACE("%p %d,%d\n", hdc, x, y );
199
200     update_dc( dc );
201     if(dc->funcs->pOffsetClipRgn)
202     {
203         ret = dc->funcs->pOffsetClipRgn( dc->physDev, x, y );
204         /* FIXME: ret is just a success flag, we should return a proper value */
205     }
206     else if (dc->hClipRgn) {
207         ret = OffsetRgn( dc->hClipRgn, MulDiv( x, dc->vportExtX, dc->wndExtX ),
208                          MulDiv( y, dc->vportExtY, dc->wndExtY ) );
209         CLIPPING_UpdateGCRegion( dc );
210     }
211     release_dc_ptr( dc );
212     return ret;
213 }
214
215
216 /***********************************************************************
217  *           OffsetVisRgn    (GDI.102)
218  */
219 INT16 WINAPI OffsetVisRgn16( HDC16 hdc16, INT16 x, INT16 y )
220 {
221     INT16 retval;
222     HDC hdc = HDC_32( hdc16 );
223     DC * dc = get_dc_ptr( hdc );
224
225     if (!dc) return ERROR;
226     TRACE("%p %d,%d\n", hdc, x, y );
227     update_dc( dc );
228     retval = OffsetRgn( dc->hVisRgn, x, y );
229     CLIPPING_UpdateGCRegion( dc );
230     release_dc_ptr( dc );
231     return retval;
232 }
233
234
235 /***********************************************************************
236  *           ExcludeClipRect    (GDI32.@)
237  */
238 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
239                                 INT right, INT bottom )
240 {
241     HRGN newRgn;
242     INT ret;
243     DC *dc = get_dc_ptr( hdc );
244     if (!dc) return ERROR;
245
246     TRACE("%p %dx%d,%dx%d\n", hdc, left, top, right, bottom );
247
248     update_dc( dc );
249     if(dc->funcs->pExcludeClipRect)
250     {
251         ret = dc->funcs->pExcludeClipRect( dc->physDev, left, top, right, bottom );
252         /* FIXME: ret is just a success flag, we should return a proper value */
253     }
254     else
255     {
256         POINT pt[2];
257
258         pt[0].x = left;
259         pt[0].y = top;
260         pt[1].x = right;
261         pt[1].y = bottom;
262         LPtoDP( hdc, pt, 2 );
263         if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
264         else
265         {
266             if (!dc->hClipRgn)
267                 create_default_clip_region( dc );
268             ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_DIFF );
269             DeleteObject( newRgn );
270         }
271         if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
272     }
273     release_dc_ptr( dc );
274     return ret;
275 }
276
277
278 /***********************************************************************
279  *           IntersectClipRect    (GDI32.@)
280  */
281 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
282 {
283     INT ret;
284     DC *dc = get_dc_ptr( hdc );
285     if (!dc) return ERROR;
286
287     TRACE("%p %d,%d - %d,%d\n", hdc, left, top, right, bottom );
288
289     update_dc( dc );
290     if(dc->funcs->pIntersectClipRect)
291     {
292         ret = dc->funcs->pIntersectClipRect( dc->physDev, left, top, right, bottom );
293         /* FIXME: ret is just a success flag, we should return a proper value */
294     }
295     else
296     {
297         POINT pt[2];
298
299         pt[0].x = left;
300         pt[0].y = top;
301         pt[1].x = right;
302         pt[1].y = bottom;
303
304         LPtoDP( hdc, pt, 2 );
305
306         if (!dc->hClipRgn)
307         {
308             dc->hClipRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y );
309             ret = SIMPLEREGION;
310         }
311         else
312         {
313             HRGN newRgn;
314
315             if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
316             else
317             {
318                 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_AND );
319                 DeleteObject( newRgn );
320             }
321         }
322         if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
323     }
324     release_dc_ptr( dc );
325     return ret;
326 }
327
328
329 /***********************************************************************
330  *           ExcludeVisRect   (GDI.73)
331  */
332 INT16 WINAPI ExcludeVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
333 {
334     HRGN tempRgn;
335     INT16 ret;
336     POINT pt[2];
337     HDC hdc = HDC_32( hdc16 );
338     DC * dc = get_dc_ptr( hdc );
339     if (!dc) return ERROR;
340
341     pt[0].x = left;
342     pt[0].y = top;
343     pt[1].x = right;
344     pt[1].y = bottom;
345
346     LPtoDP( hdc, pt, 2 );
347
348     TRACE("%p %d,%d - %d,%d\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
349
350     if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
351     else
352     {
353         update_dc( dc );
354         ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_DIFF );
355         DeleteObject( tempRgn );
356     }
357     if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
358     release_dc_ptr( dc );
359     return ret;
360 }
361
362
363 /***********************************************************************
364  *           IntersectVisRect   (GDI.98)
365  */
366 INT16 WINAPI IntersectVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
367 {
368     HRGN tempRgn;
369     INT16 ret;
370     POINT pt[2];
371     HDC hdc = HDC_32( hdc16 );
372     DC * dc = get_dc_ptr( hdc );
373     if (!dc) return ERROR;
374
375     pt[0].x = left;
376     pt[0].y = top;
377     pt[1].x = right;
378     pt[1].y = bottom;
379
380     LPtoDP( hdc, pt, 2 );
381
382     TRACE("%p %d,%d - %d,%d\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
383
384     if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
385     else
386     {
387         update_dc( dc );
388         ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_AND );
389         DeleteObject( tempRgn );
390     }
391     if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
392     release_dc_ptr( dc );
393     return ret;
394 }
395
396
397 /***********************************************************************
398  *           PtVisible    (GDI32.@)
399  */
400 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
401 {
402     POINT pt;
403     BOOL ret;
404     HRGN clip;
405     DC *dc = get_dc_ptr( hdc );
406
407     TRACE("%p %d,%d\n", hdc, x, y );
408     if (!dc) return FALSE;
409
410     pt.x = x;
411     pt.y = y;
412     LPtoDP( hdc, &pt, 1 );
413     update_dc( dc );
414     ret = PtInRegion( dc->hVisRgn, pt.x, pt.y );
415     if (ret && (clip = get_clip_region(dc))) ret = PtInRegion( clip, pt.x, pt.y );
416     release_dc_ptr( dc );
417     return ret;
418 }
419
420
421 /***********************************************************************
422  *           RectVisible    (GDI32.@)
423  */
424 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
425 {
426     RECT tmpRect;
427     BOOL ret;
428     HRGN clip;
429     DC *dc = get_dc_ptr( hdc );
430     if (!dc) return FALSE;
431     TRACE("%p %d,%dx%d,%d\n", hdc, rect->left, rect->top, rect->right, rect->bottom );
432
433     tmpRect = *rect;
434     LPtoDP( hdc, (POINT *)&tmpRect, 2 );
435
436     update_dc( dc );
437     if ((clip = get_clip_region(dc)))
438     {
439         HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
440         CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
441         ret = RectInRegion( hrgn, &tmpRect );
442         DeleteObject( hrgn );
443     }
444     else ret = RectInRegion( dc->hVisRgn, &tmpRect );
445     release_dc_ptr( dc );
446     return ret;
447 }
448
449
450 /***********************************************************************
451  *           GetClipBox    (GDI32.@)
452  */
453 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
454 {
455     INT ret;
456     HRGN clip;
457     DC *dc = get_dc_ptr( hdc );
458     if (!dc) return ERROR;
459
460     update_dc( dc );
461     if ((clip = get_clip_region(dc)))
462     {
463         HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
464         CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
465         ret = GetRgnBox( hrgn, rect );
466         DeleteObject( hrgn );
467     }
468     else ret = GetRgnBox( dc->hVisRgn, rect );
469     DPtoLP( hdc, (LPPOINT)rect, 2 );
470     release_dc_ptr( dc );
471     return ret;
472 }
473
474
475 /***********************************************************************
476  *           GetClipRgn  (GDI32.@)
477  */
478 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
479 {
480     INT ret = -1;
481     DC * dc;
482     if (hRgn && (dc = get_dc_ptr( hdc )))
483     {
484       if( dc->hClipRgn )
485       {
486           if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR ) ret = 1;
487       }
488       else ret = 0;
489       release_dc_ptr( dc );
490     }
491     return ret;
492 }
493
494
495 /***********************************************************************
496  *           GetMetaRgn    (GDI32.@)
497  */
498 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
499 {
500     INT ret = 0;
501     DC * dc = get_dc_ptr( hdc );
502
503     if (dc)
504     {
505         if (dc->hMetaRgn && CombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY ) != ERROR)
506             ret = 1;
507         release_dc_ptr( dc );
508     }
509     return ret;
510 }
511
512
513 /***********************************************************************
514  *           SaveVisRgn   (GDI.129)
515  */
516 HRGN16 WINAPI SaveVisRgn16( HDC16 hdc16 )
517 {
518     struct saved_visrgn *saved;
519     HDC hdc = HDC_32( hdc16 );
520     DC *dc = get_dc_ptr( hdc );
521
522     if (!dc) return 0;
523     TRACE("%p\n", hdc );
524
525     update_dc( dc );
526     if (!(saved = HeapAlloc( GetProcessHeap(), 0, sizeof(*saved) ))) goto error;
527     if (!(saved->hrgn = CreateRectRgn( 0, 0, 0, 0 ))) goto error;
528     CombineRgn( saved->hrgn, dc->hVisRgn, 0, RGN_COPY );
529     saved->next = dc->saved_visrgn;
530     dc->saved_visrgn = saved;
531     release_dc_ptr( dc );
532     return HRGN_16(saved->hrgn);
533
534 error:
535     release_dc_ptr( dc );
536     HeapFree( GetProcessHeap(), 0, saved );
537     return 0;
538 }
539
540
541 /***********************************************************************
542  *           RestoreVisRgn   (GDI.130)
543  */
544 INT16 WINAPI RestoreVisRgn16( HDC16 hdc16 )
545 {
546     struct saved_visrgn *saved;
547     HDC hdc = HDC_32( hdc16 );
548     DC *dc = get_dc_ptr( hdc );
549     INT16 ret = ERROR;
550
551     if (!dc) return ERROR;
552
553     TRACE("%p\n", hdc );
554
555     if (!(saved = dc->saved_visrgn)) goto done;
556
557     ret = CombineRgn( dc->hVisRgn, saved->hrgn, 0, RGN_COPY );
558     dc->saved_visrgn = saved->next;
559     DeleteObject( saved->hrgn );
560     HeapFree( GetProcessHeap(), 0, saved );
561     CLIPPING_UpdateGCRegion( dc );
562  done:
563     release_dc_ptr( dc );
564     return ret;
565 }
566
567
568 /***********************************************************************
569  * GetRandomRgn [GDI32.@]
570  *
571  * NOTES
572  *     This function is documented in MSDN online for the case of
573  *     iCode == SYSRGN (4).
574  *
575  *     For iCode == 1 it should return the clip region
576  *                  2 "    "       "   the meta region
577  *                  3 "    "       "   the intersection of the clip with
578  *                                     the meta region (== 'Rao' region).
579  *
580  *     See http://www.codeproject.com/gdi/cliprgnguide.asp
581  */
582 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, INT iCode)
583 {
584     HRGN rgn;
585     DC *dc = get_dc_ptr( hDC );
586
587     if (!dc) return -1;
588
589     switch (iCode)
590     {
591     case 1:
592         rgn = dc->hClipRgn;
593         break;
594     case 2:
595         rgn = dc->hMetaRgn;
596         break;
597     case 3:
598         rgn = dc->hMetaClipRgn;
599         if(!rgn) rgn = dc->hClipRgn;
600         if(!rgn) rgn = dc->hMetaRgn;
601         break;
602     case SYSRGN: /* == 4 */
603         update_dc( dc );
604         rgn = dc->hVisRgn;
605         break;
606     default:
607         WARN("Unknown code %d\n", iCode);
608         release_dc_ptr( dc );
609         return -1;
610     }
611     if (rgn) CombineRgn( hRgn, rgn, 0, RGN_COPY );
612     release_dc_ptr( dc );
613
614     /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
615     if (iCode == SYSRGN && !(GetVersion() & 0x80000000))
616     {
617         POINT org;
618         GetDCOrgEx( hDC, &org );
619         OffsetRgn( hRgn, org.x, org.y );
620     }
621     return (rgn != 0);
622 }
623
624
625 /***********************************************************************
626  *           SetMetaRgn    (GDI32.@)
627  */
628 INT WINAPI SetMetaRgn( HDC hdc )
629 {
630     INT ret;
631     RECT dummy;
632     DC *dc = get_dc_ptr( hdc );
633
634     if (!dc) return ERROR;
635
636     if (dc->hMetaClipRgn)
637     {
638         /* the intersection becomes the new meta region */
639         DeleteObject( dc->hMetaRgn );
640         DeleteObject( dc->hClipRgn );
641         dc->hMetaRgn = dc->hMetaClipRgn;
642         dc->hClipRgn = 0;
643         dc->hMetaClipRgn = 0;
644     }
645     else if (dc->hClipRgn)
646     {
647         dc->hMetaRgn = dc->hClipRgn;
648         dc->hClipRgn = 0;
649     }
650     /* else nothing to do */
651
652     /* Note: no need to call CLIPPING_UpdateGCRegion, the overall clip region hasn't changed */
653
654     ret = GetRgnBox( dc->hMetaRgn, &dummy );
655     release_dc_ptr( dc );
656     return ret;
657 }