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 (dc->header.type == OBJ_MEMDC)
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 (GDI32.@)
168 * Note: not exported on Windows, only the 16-bit version is exported.
170 INT WINAPI SelectVisRgn( HDC hdc, HRGN hrgn )
175 if (!hrgn) return ERROR;
176 if (!(dc = get_dc_ptr( hdc ))) return ERROR;
178 TRACE("%p %p\n", hdc, hrgn );
182 retval = CombineRgn( dc->hVisRgn, hrgn, 0, RGN_COPY );
183 CLIPPING_UpdateGCRegion( dc );
184 release_dc_ptr( dc );
189 /***********************************************************************
190 * OffsetClipRgn (GDI32.@)
192 INT WINAPI OffsetClipRgn( HDC hdc, INT x, INT y )
194 INT ret = SIMPLEREGION;
195 DC *dc = get_dc_ptr( hdc );
196 if (!dc) return ERROR;
198 TRACE("%p %d,%d\n", hdc, x, y );
201 if(dc->funcs->pOffsetClipRgn)
203 ret = dc->funcs->pOffsetClipRgn( dc->physDev, x, y );
204 /* FIXME: ret is just a success flag, we should return a proper value */
206 else if (dc->hClipRgn) {
207 ret = OffsetRgn( dc->hClipRgn, MulDiv( x, dc->vportExtX, dc->wndExtX ),
208 MulDiv( y, dc->vportExtY, dc->wndExtY ) );
209 CLIPPING_UpdateGCRegion( dc );
211 release_dc_ptr( dc );
216 /***********************************************************************
217 * OffsetVisRgn (GDI.102)
219 INT16 WINAPI OffsetVisRgn16( HDC16 hdc16, INT16 x, INT16 y )
222 HDC hdc = HDC_32( hdc16 );
223 DC * dc = get_dc_ptr( hdc );
225 if (!dc) return ERROR;
226 TRACE("%p %d,%d\n", hdc, x, y );
228 retval = OffsetRgn( dc->hVisRgn, x, y );
229 CLIPPING_UpdateGCRegion( dc );
230 release_dc_ptr( dc );
235 /***********************************************************************
236 * ExcludeClipRect (GDI32.@)
238 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
239 INT right, INT bottom )
243 DC *dc = get_dc_ptr( hdc );
244 if (!dc) return ERROR;
246 TRACE("%p %dx%d,%dx%d\n", hdc, left, top, right, bottom );
249 if(dc->funcs->pExcludeClipRect)
251 ret = dc->funcs->pExcludeClipRect( dc->physDev, left, top, right, bottom );
252 /* FIXME: ret is just a success flag, we should return a proper value */
262 LPtoDP( hdc, pt, 2 );
263 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
267 create_default_clip_region( dc );
268 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_DIFF );
269 DeleteObject( newRgn );
271 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
273 release_dc_ptr( dc );
278 /***********************************************************************
279 * IntersectClipRect (GDI32.@)
281 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
284 DC *dc = get_dc_ptr( hdc );
285 if (!dc) return ERROR;
287 TRACE("%p %d,%d - %d,%d\n", hdc, left, top, right, bottom );
290 if(dc->funcs->pIntersectClipRect)
292 ret = dc->funcs->pIntersectClipRect( dc->physDev, left, top, right, bottom );
293 /* FIXME: ret is just a success flag, we should return a proper value */
304 LPtoDP( hdc, pt, 2 );
308 dc->hClipRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y );
315 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
318 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_AND );
319 DeleteObject( newRgn );
322 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
324 release_dc_ptr( dc );
329 /***********************************************************************
330 * ExcludeVisRect (GDI.73)
332 INT16 WINAPI ExcludeVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
337 HDC hdc = HDC_32( hdc16 );
338 DC * dc = get_dc_ptr( hdc );
339 if (!dc) return ERROR;
346 LPtoDP( hdc, pt, 2 );
348 TRACE("%p %d,%d - %d,%d\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
350 if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
354 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_DIFF );
355 DeleteObject( tempRgn );
357 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
358 release_dc_ptr( dc );
363 /***********************************************************************
364 * IntersectVisRect (GDI.98)
366 INT16 WINAPI IntersectVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
371 HDC hdc = HDC_32( hdc16 );
372 DC * dc = get_dc_ptr( hdc );
373 if (!dc) return ERROR;
380 LPtoDP( hdc, pt, 2 );
382 TRACE("%p %d,%d - %d,%d\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
384 if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
388 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_AND );
389 DeleteObject( tempRgn );
391 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
392 release_dc_ptr( dc );
397 /***********************************************************************
398 * PtVisible (GDI32.@)
400 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
405 DC *dc = get_dc_ptr( hdc );
407 TRACE("%p %d,%d\n", hdc, x, y );
408 if (!dc) return FALSE;
412 LPtoDP( hdc, &pt, 1 );
414 ret = PtInRegion( dc->hVisRgn, pt.x, pt.y );
415 if (ret && (clip = get_clip_region(dc))) ret = PtInRegion( clip, pt.x, pt.y );
416 release_dc_ptr( dc );
421 /***********************************************************************
422 * RectVisible (GDI32.@)
424 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
429 DC *dc = get_dc_ptr( hdc );
430 if (!dc) return FALSE;
431 TRACE("%p %d,%dx%d,%d\n", hdc, rect->left, rect->top, rect->right, rect->bottom );
434 LPtoDP( hdc, (POINT *)&tmpRect, 2 );
437 if ((clip = get_clip_region(dc)))
439 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
440 CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
441 ret = RectInRegion( hrgn, &tmpRect );
442 DeleteObject( hrgn );
444 else ret = RectInRegion( dc->hVisRgn, &tmpRect );
445 release_dc_ptr( dc );
450 /***********************************************************************
451 * GetClipBox (GDI32.@)
453 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
457 DC *dc = get_dc_ptr( hdc );
458 if (!dc) return ERROR;
461 if ((clip = get_clip_region(dc)))
463 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
464 CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
465 ret = GetRgnBox( hrgn, rect );
466 DeleteObject( hrgn );
468 else ret = GetRgnBox( dc->hVisRgn, rect );
469 DPtoLP( hdc, (LPPOINT)rect, 2 );
470 release_dc_ptr( dc );
475 /***********************************************************************
476 * GetClipRgn (GDI32.@)
478 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
482 if (hRgn && (dc = get_dc_ptr( hdc )))
486 if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR ) ret = 1;
489 release_dc_ptr( dc );
495 /***********************************************************************
496 * GetMetaRgn (GDI32.@)
498 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
501 DC * dc = get_dc_ptr( hdc );
505 if (dc->hMetaRgn && CombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY ) != ERROR)
507 release_dc_ptr( dc );
513 /***********************************************************************
514 * SaveVisRgn (GDI.129)
516 HRGN16 WINAPI SaveVisRgn16( HDC16 hdc16 )
518 struct saved_visrgn *saved;
519 HDC hdc = HDC_32( hdc16 );
520 DC *dc = get_dc_ptr( hdc );
526 if (!(saved = HeapAlloc( GetProcessHeap(), 0, sizeof(*saved) ))) goto error;
527 if (!(saved->hrgn = CreateRectRgn( 0, 0, 0, 0 ))) goto error;
528 CombineRgn( saved->hrgn, dc->hVisRgn, 0, RGN_COPY );
529 saved->next = dc->saved_visrgn;
530 dc->saved_visrgn = saved;
531 release_dc_ptr( dc );
532 return HRGN_16(saved->hrgn);
535 release_dc_ptr( dc );
536 HeapFree( GetProcessHeap(), 0, saved );
541 /***********************************************************************
542 * RestoreVisRgn (GDI.130)
544 INT16 WINAPI RestoreVisRgn16( HDC16 hdc16 )
546 struct saved_visrgn *saved;
547 HDC hdc = HDC_32( hdc16 );
548 DC *dc = get_dc_ptr( hdc );
551 if (!dc) return ERROR;
555 if (!(saved = dc->saved_visrgn)) goto done;
557 ret = CombineRgn( dc->hVisRgn, saved->hrgn, 0, RGN_COPY );
558 dc->saved_visrgn = saved->next;
559 DeleteObject( saved->hrgn );
560 HeapFree( GetProcessHeap(), 0, saved );
561 CLIPPING_UpdateGCRegion( dc );
563 release_dc_ptr( dc );
568 /***********************************************************************
569 * GetRandomRgn [GDI32.@]
572 * This function is documented in MSDN online for the case of
573 * iCode == SYSRGN (4).
575 * For iCode == 1 it should return the clip region
576 * 2 " " " the meta region
577 * 3 " " " the intersection of the clip with
578 * the meta region (== 'Rao' region).
580 * See http://www.codeproject.com/gdi/cliprgnguide.asp
582 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, INT iCode)
585 DC *dc = get_dc_ptr( hDC );
598 rgn = dc->hMetaClipRgn;
599 if(!rgn) rgn = dc->hClipRgn;
600 if(!rgn) rgn = dc->hMetaRgn;
602 case SYSRGN: /* == 4 */
607 WARN("Unknown code %d\n", iCode);
608 release_dc_ptr( dc );
611 if (rgn) CombineRgn( hRgn, rgn, 0, RGN_COPY );
612 release_dc_ptr( dc );
614 /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
615 if (iCode == SYSRGN && !(GetVersion() & 0x80000000))
618 GetDCOrgEx( hDC, &org );
619 OffsetRgn( hRgn, org.x, org.y );
625 /***********************************************************************
626 * SetMetaRgn (GDI32.@)
628 INT WINAPI SetMetaRgn( HDC hdc )
632 DC *dc = get_dc_ptr( hdc );
634 if (!dc) return ERROR;
636 if (dc->hMetaClipRgn)
638 /* the intersection becomes the new meta region */
639 DeleteObject( dc->hMetaRgn );
640 DeleteObject( dc->hClipRgn );
641 dc->hMetaRgn = dc->hMetaClipRgn;
643 dc->hMetaClipRgn = 0;
645 else if (dc->hClipRgn)
647 dc->hMetaRgn = dc->hClipRgn;
650 /* else nothing to do */
652 /* Note: no need to call CLIPPING_UpdateGCRegion, the overall clip region hasn't changed */
654 ret = GetRgnBox( dc->hMetaRgn, &dummy );
655 release_dc_ptr( dc );