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