gdi32: Don't hold the gdi lock while calling DC driver functions.
[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   (GDI.105)
167  */
168 INT16 WINAPI SelectVisRgn16( HDC16 hdc16, HRGN16 hrgn )
169 {
170     int retval;
171     HDC hdc = HDC_32( hdc16 );
172     DC * dc;
173
174     if (!hrgn) return ERROR;
175     if (!(dc = get_dc_ptr( hdc ))) return ERROR;
176
177     TRACE("%p %04x\n", hdc, hrgn );
178
179     dc->dirty = 0;
180
181     retval = CombineRgn( dc->hVisRgn, HRGN_32(hrgn), 0, RGN_COPY );
182     CLIPPING_UpdateGCRegion( dc );
183     release_dc_ptr( dc );
184     return retval;
185 }
186
187
188 /***********************************************************************
189  *           OffsetClipRgn    (GDI32.@)
190  */
191 INT WINAPI OffsetClipRgn( HDC hdc, INT x, INT y )
192 {
193     INT ret = SIMPLEREGION;
194     DC *dc = get_dc_ptr( hdc );
195     if (!dc) return ERROR;
196
197     TRACE("%p %d,%d\n", hdc, x, y );
198
199     update_dc( dc );
200     if(dc->funcs->pOffsetClipRgn)
201     {
202         ret = dc->funcs->pOffsetClipRgn( dc->physDev, x, y );
203         /* FIXME: ret is just a success flag, we should return a proper value */
204     }
205     else if (dc->hClipRgn) {
206         ret = OffsetRgn( dc->hClipRgn, MulDiv( x, dc->vportExtX, dc->wndExtX ),
207                          MulDiv( y, dc->vportExtY, dc->wndExtY ) );
208         CLIPPING_UpdateGCRegion( dc );
209     }
210     release_dc_ptr( dc );
211     return ret;
212 }
213
214
215 /***********************************************************************
216  *           OffsetVisRgn    (GDI.102)
217  */
218 INT16 WINAPI OffsetVisRgn16( HDC16 hdc16, INT16 x, INT16 y )
219 {
220     INT16 retval;
221     HDC hdc = HDC_32( hdc16 );
222     DC * dc = get_dc_ptr( hdc );
223
224     if (!dc) return ERROR;
225     TRACE("%p %d,%d\n", hdc, x, y );
226     update_dc( dc );
227     retval = OffsetRgn( dc->hVisRgn, x, y );
228     CLIPPING_UpdateGCRegion( dc );
229     release_dc_ptr( dc );
230     return retval;
231 }
232
233
234 /***********************************************************************
235  *           ExcludeClipRect    (GDI32.@)
236  */
237 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
238                                 INT right, INT bottom )
239 {
240     HRGN newRgn;
241     INT ret;
242     DC *dc = get_dc_ptr( hdc );
243     if (!dc) return ERROR;
244
245     TRACE("%p %dx%d,%dx%d\n", hdc, left, top, right, bottom );
246
247     update_dc( dc );
248     if(dc->funcs->pExcludeClipRect)
249     {
250         ret = dc->funcs->pExcludeClipRect( dc->physDev, left, top, right, bottom );
251         /* FIXME: ret is just a success flag, we should return a proper value */
252     }
253     else
254     {
255         POINT pt[2];
256
257         pt[0].x = left;
258         pt[0].y = top;
259         pt[1].x = right;
260         pt[1].y = bottom;
261         LPtoDP( hdc, pt, 2 );
262         if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
263         else
264         {
265             if (!dc->hClipRgn)
266                 create_default_clip_region( dc );
267             ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_DIFF );
268             DeleteObject( newRgn );
269         }
270         if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
271     }
272     release_dc_ptr( dc );
273     return ret;
274 }
275
276
277 /***********************************************************************
278  *           IntersectClipRect    (GDI32.@)
279  */
280 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
281 {
282     INT ret;
283     DC *dc = get_dc_ptr( hdc );
284     if (!dc) return ERROR;
285
286     TRACE("%p %d,%d - %d,%d\n", hdc, left, top, right, bottom );
287
288     update_dc( dc );
289     if(dc->funcs->pIntersectClipRect)
290     {
291         ret = dc->funcs->pIntersectClipRect( dc->physDev, left, top, right, bottom );
292         /* FIXME: ret is just a success flag, we should return a proper value */
293     }
294     else
295     {
296         POINT pt[2];
297
298         pt[0].x = left;
299         pt[0].y = top;
300         pt[1].x = right;
301         pt[1].y = bottom;
302
303         LPtoDP( hdc, pt, 2 );
304
305         if (!dc->hClipRgn)
306         {
307             dc->hClipRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y );
308             ret = SIMPLEREGION;
309         }
310         else
311         {
312             HRGN newRgn;
313
314             if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
315             else
316             {
317                 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_AND );
318                 DeleteObject( newRgn );
319             }
320         }
321         if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
322     }
323     release_dc_ptr( dc );
324     return ret;
325 }
326
327
328 /***********************************************************************
329  *           ExcludeVisRect   (GDI.73)
330  */
331 INT16 WINAPI ExcludeVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
332 {
333     HRGN tempRgn;
334     INT16 ret;
335     POINT pt[2];
336     HDC hdc = HDC_32( hdc16 );
337     DC * dc = get_dc_ptr( hdc );
338     if (!dc) return ERROR;
339
340     pt[0].x = left;
341     pt[0].y = top;
342     pt[1].x = right;
343     pt[1].y = bottom;
344
345     LPtoDP( hdc, pt, 2 );
346
347     TRACE("%p %d,%d - %d,%d\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
348
349     if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
350     else
351     {
352         update_dc( dc );
353         ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_DIFF );
354         DeleteObject( tempRgn );
355     }
356     if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
357     release_dc_ptr( dc );
358     return ret;
359 }
360
361
362 /***********************************************************************
363  *           IntersectVisRect   (GDI.98)
364  */
365 INT16 WINAPI IntersectVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
366 {
367     HRGN tempRgn;
368     INT16 ret;
369     POINT pt[2];
370     HDC hdc = HDC_32( hdc16 );
371     DC * dc = get_dc_ptr( hdc );
372     if (!dc) return ERROR;
373
374     pt[0].x = left;
375     pt[0].y = top;
376     pt[1].x = right;
377     pt[1].y = bottom;
378
379     LPtoDP( hdc, pt, 2 );
380
381     TRACE("%p %d,%d - %d,%d\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
382
383     if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
384     else
385     {
386         update_dc( dc );
387         ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_AND );
388         DeleteObject( tempRgn );
389     }
390     if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
391     release_dc_ptr( dc );
392     return ret;
393 }
394
395
396 /***********************************************************************
397  *           PtVisible    (GDI32.@)
398  */
399 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
400 {
401     POINT pt;
402     BOOL ret;
403     HRGN clip;
404     DC *dc = get_dc_ptr( hdc );
405
406     TRACE("%p %d,%d\n", hdc, x, y );
407     if (!dc) return FALSE;
408
409     pt.x = x;
410     pt.y = y;
411     LPtoDP( hdc, &pt, 1 );
412     update_dc( dc );
413     ret = PtInRegion( dc->hVisRgn, pt.x, pt.y );
414     if (ret && (clip = get_clip_region(dc))) ret = PtInRegion( clip, pt.x, pt.y );
415     release_dc_ptr( dc );
416     return ret;
417 }
418
419
420 /***********************************************************************
421  *           RectVisible    (GDI32.@)
422  */
423 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
424 {
425     RECT tmpRect;
426     BOOL ret;
427     HRGN clip;
428     DC *dc = get_dc_ptr( hdc );
429     if (!dc) return FALSE;
430     TRACE("%p %d,%dx%d,%d\n", hdc, rect->left, rect->top, rect->right, rect->bottom );
431
432     tmpRect = *rect;
433     LPtoDP( hdc, (POINT *)&tmpRect, 2 );
434
435     update_dc( dc );
436     if ((clip = get_clip_region(dc)))
437     {
438         HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
439         CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
440         ret = RectInRegion( hrgn, &tmpRect );
441         DeleteObject( hrgn );
442     }
443     else ret = RectInRegion( dc->hVisRgn, &tmpRect );
444     release_dc_ptr( dc );
445     return ret;
446 }
447
448
449 /***********************************************************************
450  *           GetClipBox    (GDI32.@)
451  */
452 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
453 {
454     INT ret;
455     HRGN clip;
456     DC *dc = get_dc_ptr( hdc );
457     if (!dc) return ERROR;
458
459     update_dc( dc );
460     if ((clip = get_clip_region(dc)))
461     {
462         HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
463         CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
464         ret = GetRgnBox( hrgn, rect );
465         DeleteObject( hrgn );
466     }
467     else ret = GetRgnBox( dc->hVisRgn, rect );
468     DPtoLP( hdc, (LPPOINT)rect, 2 );
469     release_dc_ptr( dc );
470     return ret;
471 }
472
473
474 /***********************************************************************
475  *           GetClipRgn  (GDI32.@)
476  */
477 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
478 {
479     INT ret = -1;
480     DC * dc;
481     if (hRgn && (dc = get_dc_ptr( hdc )))
482     {
483       if( dc->hClipRgn )
484       {
485           if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR ) ret = 1;
486       }
487       else ret = 0;
488       release_dc_ptr( dc );
489     }
490     return ret;
491 }
492
493
494 /***********************************************************************
495  *           GetMetaRgn    (GDI32.@)
496  */
497 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
498 {
499     INT ret = 0;
500     DC * dc = get_dc_ptr( hdc );
501
502     if (dc)
503     {
504         if (dc->hMetaRgn && CombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY ) != ERROR)
505             ret = 1;
506         release_dc_ptr( dc );
507     }
508     return ret;
509 }
510
511
512 /***********************************************************************
513  *           SaveVisRgn   (GDI.129)
514  */
515 HRGN16 WINAPI SaveVisRgn16( HDC16 hdc16 )
516 {
517     struct saved_visrgn *saved;
518     HDC hdc = HDC_32( hdc16 );
519     DC *dc = get_dc_ptr( hdc );
520
521     if (!dc) return 0;
522     TRACE("%p\n", hdc );
523
524     update_dc( dc );
525     if (!(saved = HeapAlloc( GetProcessHeap(), 0, sizeof(*saved) ))) goto error;
526     if (!(saved->hrgn = CreateRectRgn( 0, 0, 0, 0 ))) goto error;
527     CombineRgn( saved->hrgn, dc->hVisRgn, 0, RGN_COPY );
528     saved->next = dc->saved_visrgn;
529     dc->saved_visrgn = saved;
530     release_dc_ptr( dc );
531     return HRGN_16(saved->hrgn);
532
533 error:
534     release_dc_ptr( dc );
535     HeapFree( GetProcessHeap(), 0, saved );
536     return 0;
537 }
538
539
540 /***********************************************************************
541  *           RestoreVisRgn   (GDI.130)
542  */
543 INT16 WINAPI RestoreVisRgn16( HDC16 hdc16 )
544 {
545     struct saved_visrgn *saved;
546     HDC hdc = HDC_32( hdc16 );
547     DC *dc = get_dc_ptr( hdc );
548     INT16 ret = ERROR;
549
550     if (!dc) return ERROR;
551
552     TRACE("%p\n", hdc );
553
554     if (!(saved = dc->saved_visrgn)) goto done;
555
556     ret = CombineRgn( dc->hVisRgn, saved->hrgn, 0, RGN_COPY );
557     dc->saved_visrgn = saved->next;
558     DeleteObject( saved->hrgn );
559     HeapFree( GetProcessHeap(), 0, saved );
560     CLIPPING_UpdateGCRegion( dc );
561  done:
562     release_dc_ptr( dc );
563     return ret;
564 }
565
566
567 /***********************************************************************
568  * GetRandomRgn [GDI32.@]
569  *
570  * NOTES
571  *     This function is documented in MSDN online for the case of
572  *     iCode == SYSRGN (4).
573  *
574  *     For iCode == 1 it should return the clip region
575  *                  2 "    "       "   the meta region
576  *                  3 "    "       "   the intersection of the clip with
577  *                                     the meta region (== 'Rao' region).
578  *
579  *     See http://www.codeproject.com/gdi/cliprgnguide.asp
580  */
581 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, INT iCode)
582 {
583     HRGN rgn;
584     DC *dc = get_dc_ptr( hDC );
585
586     if (!dc) return -1;
587
588     switch (iCode)
589     {
590     case 1:
591         rgn = dc->hClipRgn;
592         break;
593     case 2:
594         rgn = dc->hMetaRgn;
595         break;
596     case 3:
597         rgn = dc->hMetaClipRgn;
598         if(!rgn) rgn = dc->hClipRgn;
599         if(!rgn) rgn = dc->hMetaRgn;
600         break;
601     case SYSRGN: /* == 4 */
602         update_dc( dc );
603         rgn = dc->hVisRgn;
604         break;
605     default:
606         WARN("Unknown code %d\n", iCode);
607         release_dc_ptr( dc );
608         return -1;
609     }
610     if (rgn) CombineRgn( hRgn, rgn, 0, RGN_COPY );
611     release_dc_ptr( dc );
612
613     /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
614     if (iCode == SYSRGN && !(GetVersion() & 0x80000000))
615     {
616         POINT org;
617         GetDCOrgEx( hDC, &org );
618         OffsetRgn( hRgn, org.x, org.y );
619     }
620     return (rgn != 0);
621 }
622
623
624 /***********************************************************************
625  *           SetMetaRgn    (GDI32.@)
626  */
627 INT WINAPI SetMetaRgn( HDC hdc )
628 {
629     INT ret;
630     RECT dummy;
631     DC *dc = get_dc_ptr( hdc );
632
633     if (!dc) return ERROR;
634
635     if (dc->hMetaClipRgn)
636     {
637         /* the intersection becomes the new meta region */
638         DeleteObject( dc->hMetaRgn );
639         DeleteObject( dc->hClipRgn );
640         dc->hMetaRgn = dc->hMetaClipRgn;
641         dc->hClipRgn = 0;
642         dc->hMetaClipRgn = 0;
643     }
644     else if (dc->hClipRgn)
645     {
646         dc->hMetaRgn = dc->hClipRgn;
647         dc->hClipRgn = 0;
648     }
649     /* else nothing to do */
650
651     /* Note: no need to call CLIPPING_UpdateGCRegion, the overall clip region hasn't changed */
652
653     ret = GetRgnBox( dc->hMetaRgn, &dummy );
654     release_dc_ptr( dc );
655     return ret;
656 }