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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "gdi_private.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(clipping);
33 /***********************************************************************
36 * Return the total clip region (if any).
38 static inline HRGN get_clip_region( DC * dc )
40 if (dc->hMetaClipRgn) return dc->hMetaClipRgn;
41 if (dc->hMetaRgn) return dc->hMetaRgn;
46 /***********************************************************************
47 * CLIPPING_UpdateGCRegion
49 * Update the GC clip region when the ClipRgn or VisRgn have changed.
51 void CLIPPING_UpdateGCRegion( DC * dc )
57 ERR("hVisRgn is zero. Please report this.\n" );
61 /* update the intersection of meta and clip regions */
62 if (dc->hMetaRgn && dc->hClipRgn)
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;
68 else /* only one is set, no need for an intersection */
70 if (dc->hMetaClipRgn) DeleteObject( dc->hMetaClipRgn );
72 clip_rgn = dc->hMetaRgn ? dc->hMetaRgn : dc->hClipRgn;
75 if (dc->funcs->pSetDeviceClipping)
76 dc->funcs->pSetDeviceClipping( dc->physDev, dc->hVisRgn, clip_rgn );
79 /***********************************************************************
80 * create_default_clip_region
82 * Create a default clipping region when none already exists.
84 static inline void create_default_clip_region( DC * dc )
88 if (GDIMAGIC( dc->header.wMagic ) == MEMORY_DC_MAGIC)
92 GetObjectW( dc->hBitmap, sizeof(bitmap), &bitmap );
93 width = bitmap.bmWidth;
94 height = bitmap.bmHeight;
98 width = GetDeviceCaps( dc->hSelf, DESKTOPHORZRES );
99 height = GetDeviceCaps( dc->hSelf, DESKTOPVERTRES );
101 dc->hClipRgn = CreateRectRgn( 0, 0, width, height );
105 /***********************************************************************
106 * SelectClipRgn (GDI32.@)
108 INT WINAPI SelectClipRgn( HDC hdc, HRGN hrgn )
110 return ExtSelectClipRgn( hdc, hrgn, RGN_COPY );
114 /******************************************************************************
115 * ExtSelectClipRgn [GDI32.@]
117 INT WINAPI ExtSelectClipRgn( HDC hdc, HRGN hrgn, INT fnMode )
121 DC * dc = get_dc_ptr( hdc );
122 if (!dc) return ERROR;
124 TRACE("%p %p %d\n", hdc, hrgn, fnMode );
127 if (dc->funcs->pExtSelectClipRgn)
129 retval = dc->funcs->pExtSelectClipRgn( dc->physDev, hrgn, fnMode );
130 release_dc_ptr( dc );
136 if (fnMode == RGN_COPY)
138 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
143 FIXME("Unimplemented: hrgn NULL in mode: %d\n", fnMode);
144 release_dc_ptr( dc );
151 create_default_clip_region( dc );
153 if(fnMode == RGN_COPY)
154 CombineRgn( dc->hClipRgn, hrgn, 0, fnMode );
156 CombineRgn( dc->hClipRgn, dc->hClipRgn, hrgn, fnMode);
159 CLIPPING_UpdateGCRegion( dc );
160 release_dc_ptr( dc );
162 return GetClipBox(hdc, &rect);
165 /***********************************************************************
166 * SelectVisRgn (GDI.105)
168 INT16 WINAPI SelectVisRgn16( HDC16 hdc16, HRGN16 hrgn )
171 HDC hdc = HDC_32( hdc16 );
174 if (!hrgn) return ERROR;
175 if (!(dc = get_dc_ptr( hdc ))) return ERROR;
177 TRACE("%p %04x\n", hdc, hrgn );
181 retval = CombineRgn( dc->hVisRgn, HRGN_32(hrgn), 0, RGN_COPY );
182 CLIPPING_UpdateGCRegion( dc );
183 release_dc_ptr( dc );
188 /***********************************************************************
189 * OffsetClipRgn (GDI32.@)
191 INT WINAPI OffsetClipRgn( HDC hdc, INT x, INT y )
193 INT ret = SIMPLEREGION;
194 DC *dc = get_dc_ptr( hdc );
195 if (!dc) return ERROR;
197 TRACE("%p %d,%d\n", hdc, x, y );
200 if(dc->funcs->pOffsetClipRgn)
202 ret = dc->funcs->pOffsetClipRgn( dc->physDev, x, y );
203 /* FIXME: ret is just a success flag, we should return a proper value */
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 );
210 release_dc_ptr( dc );
215 /***********************************************************************
216 * OffsetVisRgn (GDI.102)
218 INT16 WINAPI OffsetVisRgn16( HDC16 hdc16, INT16 x, INT16 y )
221 HDC hdc = HDC_32( hdc16 );
222 DC * dc = get_dc_ptr( hdc );
224 if (!dc) return ERROR;
225 TRACE("%p %d,%d\n", hdc, x, y );
227 retval = OffsetRgn( dc->hVisRgn, x, y );
228 CLIPPING_UpdateGCRegion( dc );
229 release_dc_ptr( dc );
234 /***********************************************************************
235 * ExcludeClipRect (GDI32.@)
237 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
238 INT right, INT bottom )
242 DC *dc = get_dc_ptr( hdc );
243 if (!dc) return ERROR;
245 TRACE("%p %dx%d,%dx%d\n", hdc, left, top, right, bottom );
248 if(dc->funcs->pExcludeClipRect)
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 */
261 LPtoDP( hdc, pt, 2 );
262 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
266 create_default_clip_region( dc );
267 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_DIFF );
268 DeleteObject( newRgn );
270 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
272 release_dc_ptr( dc );
277 /***********************************************************************
278 * IntersectClipRect (GDI32.@)
280 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
283 DC *dc = get_dc_ptr( hdc );
284 if (!dc) return ERROR;
286 TRACE("%p %d,%d - %d,%d\n", hdc, left, top, right, bottom );
289 if(dc->funcs->pIntersectClipRect)
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 */
303 LPtoDP( hdc, pt, 2 );
307 dc->hClipRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y );
314 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
317 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_AND );
318 DeleteObject( newRgn );
321 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
323 release_dc_ptr( dc );
328 /***********************************************************************
329 * ExcludeVisRect (GDI.73)
331 INT16 WINAPI ExcludeVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
336 HDC hdc = HDC_32( hdc16 );
337 DC * dc = get_dc_ptr( hdc );
338 if (!dc) return ERROR;
345 LPtoDP( hdc, pt, 2 );
347 TRACE("%p %d,%d - %d,%d\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
349 if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
353 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_DIFF );
354 DeleteObject( tempRgn );
356 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
357 release_dc_ptr( dc );
362 /***********************************************************************
363 * IntersectVisRect (GDI.98)
365 INT16 WINAPI IntersectVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
370 HDC hdc = HDC_32( hdc16 );
371 DC * dc = get_dc_ptr( hdc );
372 if (!dc) return ERROR;
379 LPtoDP( hdc, pt, 2 );
381 TRACE("%p %d,%d - %d,%d\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
383 if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
387 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_AND );
388 DeleteObject( tempRgn );
390 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
391 release_dc_ptr( dc );
396 /***********************************************************************
397 * PtVisible (GDI32.@)
399 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
404 DC *dc = get_dc_ptr( hdc );
406 TRACE("%p %d,%d\n", hdc, x, y );
407 if (!dc) return FALSE;
411 LPtoDP( hdc, &pt, 1 );
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 );
420 /***********************************************************************
421 * RectVisible (GDI32.@)
423 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
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 );
433 LPtoDP( hdc, (POINT *)&tmpRect, 2 );
436 if ((clip = get_clip_region(dc)))
438 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
439 CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
440 ret = RectInRegion( hrgn, &tmpRect );
441 DeleteObject( hrgn );
443 else ret = RectInRegion( dc->hVisRgn, &tmpRect );
444 release_dc_ptr( dc );
449 /***********************************************************************
450 * GetClipBox (GDI32.@)
452 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
456 DC *dc = get_dc_ptr( hdc );
457 if (!dc) return ERROR;
460 if ((clip = get_clip_region(dc)))
462 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
463 CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
464 ret = GetRgnBox( hrgn, rect );
465 DeleteObject( hrgn );
467 else ret = GetRgnBox( dc->hVisRgn, rect );
468 DPtoLP( hdc, (LPPOINT)rect, 2 );
469 release_dc_ptr( dc );
474 /***********************************************************************
475 * GetClipRgn (GDI32.@)
477 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
481 if (hRgn && (dc = get_dc_ptr( hdc )))
485 if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR ) ret = 1;
488 release_dc_ptr( dc );
494 /***********************************************************************
495 * GetMetaRgn (GDI32.@)
497 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
500 DC * dc = get_dc_ptr( hdc );
504 if (dc->hMetaRgn && CombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY ) != ERROR)
506 release_dc_ptr( dc );
512 /***********************************************************************
513 * SaveVisRgn (GDI.129)
515 HRGN16 WINAPI SaveVisRgn16( HDC16 hdc16 )
517 struct saved_visrgn *saved;
518 HDC hdc = HDC_32( hdc16 );
519 DC *dc = get_dc_ptr( hdc );
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);
534 release_dc_ptr( dc );
535 HeapFree( GetProcessHeap(), 0, saved );
540 /***********************************************************************
541 * RestoreVisRgn (GDI.130)
543 INT16 WINAPI RestoreVisRgn16( HDC16 hdc16 )
545 struct saved_visrgn *saved;
546 HDC hdc = HDC_32( hdc16 );
547 DC *dc = get_dc_ptr( hdc );
550 if (!dc) return ERROR;
554 if (!(saved = dc->saved_visrgn)) goto done;
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 );
562 release_dc_ptr( dc );
567 /***********************************************************************
568 * GetRandomRgn [GDI32.@]
571 * This function is documented in MSDN online for the case of
572 * iCode == SYSRGN (4).
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).
579 * See http://www.codeproject.com/gdi/cliprgnguide.asp
581 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, INT iCode)
584 DC *dc = get_dc_ptr( hDC );
597 rgn = dc->hMetaClipRgn;
598 if(!rgn) rgn = dc->hClipRgn;
599 if(!rgn) rgn = dc->hMetaRgn;
601 case SYSRGN: /* == 4 */
606 WARN("Unknown code %d\n", iCode);
607 release_dc_ptr( dc );
610 if (rgn) CombineRgn( hRgn, rgn, 0, RGN_COPY );
611 release_dc_ptr( dc );
613 /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
614 if (iCode == SYSRGN && !(GetVersion() & 0x80000000))
617 GetDCOrgEx( hDC, &org );
618 OffsetRgn( hRgn, org.x, org.y );
624 /***********************************************************************
625 * SetMetaRgn (GDI32.@)
627 INT WINAPI SetMetaRgn( HDC hdc )
631 DC *dc = get_dc_ptr( hdc );
633 if (!dc) return ERROR;
635 if (dc->hMetaClipRgn)
637 /* the intersection becomes the new meta region */
638 DeleteObject( dc->hMetaRgn );
639 DeleteObject( dc->hClipRgn );
640 dc->hMetaRgn = dc->hMetaClipRgn;
642 dc->hMetaClipRgn = 0;
644 else if (dc->hClipRgn)
646 dc->hMetaRgn = dc->hClipRgn;
649 /* else nothing to do */
651 /* Note: no need to call CLIPPING_UpdateGCRegion, the overall clip region hasn't changed */
653 ret = GetRgnBox( dc->hMetaRgn, &dummy );
654 release_dc_ptr( dc );