2 * DC clipping functions
4 * Copyright 1993 Alexandre Julliard
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.
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.
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
27 #include "wine/winuser16.h"
29 #include "gdi_private.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(clipping);
35 /***********************************************************************
38 * Return the total clip region (if any).
40 static inline HRGN get_clip_region( DC * dc )
42 if (dc->hMetaClipRgn) return dc->hMetaClipRgn;
43 if (dc->hMetaRgn) return dc->hMetaRgn;
48 /***********************************************************************
49 * CLIPPING_UpdateGCRegion
51 * Update the GC clip region when the ClipRgn or VisRgn have changed.
53 void CLIPPING_UpdateGCRegion( DC * dc )
59 ERR("hVisRgn is zero. Please report this.\n" );
63 if (dc->flags & DC_DIRTY) ERR( "DC is dirty. Please report this.\n" );
65 /* update the intersection of meta and clip regions */
66 if (dc->hMetaRgn && dc->hClipRgn)
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;
72 else /* only one is set, no need for an intersection */
74 if (dc->hMetaClipRgn) DeleteObject( dc->hMetaClipRgn );
76 clip_rgn = dc->hMetaRgn ? dc->hMetaRgn : dc->hClipRgn;
79 if (dc->funcs->pSetDeviceClipping)
80 dc->funcs->pSetDeviceClipping( dc->physDev, dc->hVisRgn, clip_rgn );
83 /***********************************************************************
84 * create_default_clip_region
86 * Create a default clipping region when none already exists.
88 static inline void create_default_clip_region( DC * dc )
90 dc->hClipRgn = CreateRectRgn(0, 0,
91 GetDeviceCaps( dc->hSelf, HORZRES ),
92 GetDeviceCaps( dc->hSelf, VERTRES ));
96 /***********************************************************************
97 * SelectClipRgn (GDI32.@)
99 INT WINAPI SelectClipRgn( HDC hdc, HRGN hrgn )
101 return ExtSelectClipRgn( hdc, hrgn, RGN_COPY );
105 /******************************************************************************
106 * ExtSelectClipRgn [GDI32.@]
108 INT WINAPI ExtSelectClipRgn( HDC hdc, HRGN hrgn, INT fnMode )
112 DC * dc = DC_GetDCUpdate( hdc );
113 if (!dc) return ERROR;
115 TRACE("%p %p %d\n", hdc, hrgn, fnMode );
117 if (dc->funcs->pExtSelectClipRgn)
119 retval = dc->funcs->pExtSelectClipRgn( dc->physDev, hrgn, fnMode );
120 GDI_ReleaseObj( hdc );
126 if (fnMode == RGN_COPY)
128 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
133 FIXME("Unimplemented: hrgn NULL in mode: %d\n", fnMode);
134 GDI_ReleaseObj( hdc );
141 create_default_clip_region( dc );
143 if(fnMode == RGN_COPY)
144 CombineRgn( dc->hClipRgn, hrgn, 0, fnMode );
146 CombineRgn( dc->hClipRgn, dc->hClipRgn, hrgn, fnMode);
149 CLIPPING_UpdateGCRegion( dc );
150 GDI_ReleaseObj( hdc );
152 return GetClipBox(hdc, &rect);
155 /***********************************************************************
156 * SelectVisRgn (GDI.105)
158 INT16 WINAPI SelectVisRgn16( HDC16 hdc16, HRGN16 hrgn )
161 HDC hdc = HDC_32( hdc16 );
164 if (!hrgn) return ERROR;
165 if (!(dc = DC_GetDCPtr( hdc ))) return ERROR;
167 TRACE("%p %04x\n", hdc, hrgn );
169 dc->flags &= ~DC_DIRTY;
171 retval = CombineRgn( dc->hVisRgn, HRGN_32(hrgn), 0, RGN_COPY );
172 CLIPPING_UpdateGCRegion( dc );
173 GDI_ReleaseObj( hdc );
178 /***********************************************************************
179 * OffsetClipRgn (GDI32.@)
181 INT WINAPI OffsetClipRgn( HDC hdc, INT x, INT y )
183 INT ret = SIMPLEREGION;
184 DC *dc = DC_GetDCUpdate( hdc );
185 if (!dc) return ERROR;
187 TRACE("%p %d,%d\n", hdc, x, y );
189 if(dc->funcs->pOffsetClipRgn)
191 ret = dc->funcs->pOffsetClipRgn( dc->physDev, x, y );
192 /* FIXME: ret is just a success flag, we should return a proper value */
194 else if (dc->hClipRgn) {
195 ret = OffsetRgn( dc->hClipRgn, MulDiv( x, dc->vportExtX, dc->wndExtX ),
196 MulDiv( y, dc->vportExtY, dc->wndExtY ) );
197 CLIPPING_UpdateGCRegion( dc );
199 GDI_ReleaseObj( hdc );
204 /***********************************************************************
205 * OffsetVisRgn (GDI.102)
207 INT16 WINAPI OffsetVisRgn16( HDC16 hdc16, INT16 x, INT16 y )
210 HDC hdc = HDC_32( hdc16 );
211 DC * dc = DC_GetDCUpdate( hdc );
212 if (!dc) return ERROR;
213 TRACE("%p %d,%d\n", hdc, x, y );
214 retval = OffsetRgn( dc->hVisRgn, x, y );
215 CLIPPING_UpdateGCRegion( dc );
216 GDI_ReleaseObj( hdc );
221 /***********************************************************************
222 * ExcludeClipRect (GDI32.@)
224 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
225 INT right, INT bottom )
229 DC *dc = DC_GetDCUpdate( hdc );
230 if (!dc) return ERROR;
232 TRACE("%p %dx%d,%dx%d\n", hdc, left, top, right, bottom );
234 if(dc->funcs->pExcludeClipRect)
236 ret = dc->funcs->pExcludeClipRect( dc->physDev, left, top, right, bottom );
237 /* FIXME: ret is just a success flag, we should return a proper value */
247 LPtoDP( hdc, pt, 2 );
248 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
252 create_default_clip_region( dc );
253 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_DIFF );
254 DeleteObject( newRgn );
256 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
258 GDI_ReleaseObj( hdc );
263 /***********************************************************************
264 * IntersectClipRect (GDI32.@)
266 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
269 DC *dc = DC_GetDCUpdate( hdc );
270 if (!dc) return ERROR;
272 TRACE("%p %d,%d - %d,%d\n", hdc, left, top, right, bottom );
274 if(dc->funcs->pIntersectClipRect)
276 ret = dc->funcs->pIntersectClipRect( dc->physDev, left, top, right, bottom );
277 /* FIXME: ret is just a success flag, we should return a proper value */
288 LPtoDP( hdc, pt, 2 );
292 dc->hClipRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y );
299 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
302 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_AND );
303 DeleteObject( newRgn );
306 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
308 GDI_ReleaseObj( hdc );
313 /***********************************************************************
314 * ExcludeVisRect (GDI.73)
316 INT16 WINAPI ExcludeVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
321 HDC hdc = HDC_32( hdc16 );
322 DC * dc = DC_GetDCUpdate( hdc );
323 if (!dc) return ERROR;
330 LPtoDP( hdc, pt, 2 );
332 TRACE("%p %ld,%ld - %ld,%ld\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
334 if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
337 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_DIFF );
338 DeleteObject( tempRgn );
340 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
341 GDI_ReleaseObj( hdc );
346 /***********************************************************************
347 * IntersectVisRect (GDI.98)
349 INT16 WINAPI IntersectVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
354 HDC hdc = HDC_32( hdc16 );
355 DC * dc = DC_GetDCUpdate( hdc );
356 if (!dc) return ERROR;
363 LPtoDP( hdc, pt, 2 );
365 TRACE("%p %ld,%ld - %ld,%ld\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
368 if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
371 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_AND );
372 DeleteObject( tempRgn );
374 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
375 GDI_ReleaseObj( hdc );
380 /***********************************************************************
381 * PtVisible (GDI32.@)
383 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
388 DC *dc = DC_GetDCUpdate( hdc );
390 TRACE("%p %d,%d\n", hdc, x, y );
391 if (!dc) return FALSE;
395 LPtoDP( hdc, &pt, 1 );
396 ret = PtInRegion( dc->hVisRgn, pt.x, pt.y );
397 if (ret && (clip = get_clip_region(dc))) ret = PtInRegion( clip, pt.x, pt.y );
398 GDI_ReleaseObj( hdc );
403 /***********************************************************************
404 * RectVisible (GDI32.@)
406 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
411 DC *dc = DC_GetDCUpdate( hdc );
412 if (!dc) return FALSE;
413 TRACE("%p %ld,%ldx%ld,%ld\n", hdc, rect->left, rect->top, rect->right, rect->bottom );
416 LPtoDP( hdc, (POINT *)&tmpRect, 2 );
418 if ((clip = get_clip_region(dc)))
420 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
421 CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
422 ret = RectInRegion( hrgn, &tmpRect );
423 DeleteObject( hrgn );
425 else ret = RectInRegion( dc->hVisRgn, &tmpRect );
426 GDI_ReleaseObj( hdc );
431 /***********************************************************************
432 * GetClipBox (GDI32.@)
434 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
438 DC *dc = DC_GetDCUpdate( hdc );
439 if (!dc) return ERROR;
440 if ((clip = get_clip_region(dc)))
442 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
443 CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
444 ret = GetRgnBox( hrgn, rect );
445 DeleteObject( hrgn );
447 else ret = GetRgnBox( dc->hVisRgn, rect );
448 DPtoLP( hdc, (LPPOINT)rect, 2 );
449 GDI_ReleaseObj( hdc );
454 /***********************************************************************
455 * GetClipRgn (GDI32.@)
457 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
461 if (hRgn && (dc = DC_GetDCPtr( hdc )))
465 if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR ) ret = 1;
468 GDI_ReleaseObj( hdc );
474 /***********************************************************************
475 * GetMetaRgn (GDI32.@)
477 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
480 DC * dc = DC_GetDCPtr( hdc );
484 if (dc->hMetaRgn && CombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY ) != ERROR)
486 GDI_ReleaseObj( hdc );
492 /***********************************************************************
493 * SaveVisRgn (GDI.129)
495 HRGN16 WINAPI SaveVisRgn16( HDC16 hdc16 )
497 struct saved_visrgn *saved;
498 HDC hdc = HDC_32( hdc16 );
499 DC *dc = DC_GetDCUpdate( hdc );
504 if (!(saved = HeapAlloc( GetProcessHeap(), 0, sizeof(*saved) ))) goto error;
505 if (!(saved->hrgn = CreateRectRgn( 0, 0, 0, 0 ))) goto error;
506 CombineRgn( saved->hrgn, dc->hVisRgn, 0, RGN_COPY );
507 saved->next = dc->saved_visrgn;
508 dc->saved_visrgn = saved;
509 GDI_ReleaseObj( hdc );
510 return HRGN_16(saved->hrgn);
513 GDI_ReleaseObj( hdc );
514 HeapFree( GetProcessHeap(), 0, saved );
519 /***********************************************************************
520 * RestoreVisRgn (GDI.130)
522 INT16 WINAPI RestoreVisRgn16( HDC16 hdc16 )
524 struct saved_visrgn *saved;
525 HDC hdc = HDC_32( hdc16 );
526 DC *dc = DC_GetDCPtr( hdc );
529 if (!dc) return ERROR;
533 if (!(saved = dc->saved_visrgn)) goto done;
535 ret = CombineRgn( dc->hVisRgn, saved->hrgn, 0, RGN_COPY );
536 dc->saved_visrgn = saved->next;
537 DeleteObject( saved->hrgn );
538 HeapFree( GetProcessHeap(), 0, saved );
539 dc->flags &= ~DC_DIRTY;
540 CLIPPING_UpdateGCRegion( dc );
542 GDI_ReleaseObj( hdc );
547 /***********************************************************************
548 * GetRandomRgn [GDI32.@]
551 * This function is documented in MSDN online for the case of
552 * iCode == SYSRGN (4).
554 * For iCode == 1 it should return the clip region
555 * 2 " " " the meta region
556 * 3 " " " the intersection of the clip with
557 * the meta region (== 'Rao' region).
559 * See http://www.codeproject.com/gdi/cliprgnguide.asp
561 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, INT iCode)
564 DC *dc = DC_GetDCPtr( hDC );
577 rgn = dc->hMetaClipRgn;
578 if(!rgn) rgn = dc->hClipRgn;
579 if(!rgn) rgn = dc->hMetaRgn;
581 case SYSRGN: /* == 4 */
585 WARN("Unknown code %d\n", iCode);
586 GDI_ReleaseObj( hDC );
589 if (rgn) CombineRgn( hRgn, rgn, 0, RGN_COPY );
590 GDI_ReleaseObj( hDC );
592 /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
593 if (iCode == SYSRGN && !(GetVersion() & 0x80000000))
596 GetDCOrgEx( hDC, &org );
597 OffsetRgn( hRgn, org.x, org.y );
603 /***********************************************************************
604 * SetMetaRgn (GDI32.@)
606 INT WINAPI SetMetaRgn( HDC hdc )
610 DC *dc = DC_GetDCPtr( hdc );
612 if (!dc) return ERROR;
614 if (dc->hMetaClipRgn)
616 /* the intersection becomes the new meta region */
617 DeleteObject( dc->hMetaRgn );
618 DeleteObject( dc->hClipRgn );
619 dc->hMetaRgn = dc->hMetaClipRgn;
621 dc->hMetaClipRgn = 0;
623 else if (dc->hClipRgn)
625 dc->hMetaRgn = dc->hClipRgn;
628 /* else nothing to do */
630 /* Note: no need to call CLIPPING_UpdateGCRegion, the overall clip region hasn't changed */
632 ret = GetRgnBox( dc->hMetaRgn, &dummy );
633 GDI_ReleaseObj( hdc );