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