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 = DC_GetDCUpdate( hdc );
122 if (!dc) return ERROR;
124 TRACE("%p %p %d\n", hdc, hrgn, fnMode );
126 if (dc->funcs->pExtSelectClipRgn)
128 retval = dc->funcs->pExtSelectClipRgn( dc->physDev, hrgn, fnMode );
129 DC_ReleaseDCPtr( dc );
135 if (fnMode == RGN_COPY)
137 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
142 FIXME("Unimplemented: hrgn NULL in mode: %d\n", fnMode);
143 DC_ReleaseDCPtr( dc );
150 create_default_clip_region( dc );
152 if(fnMode == RGN_COPY)
153 CombineRgn( dc->hClipRgn, hrgn, 0, fnMode );
155 CombineRgn( dc->hClipRgn, dc->hClipRgn, hrgn, fnMode);
158 CLIPPING_UpdateGCRegion( dc );
159 DC_ReleaseDCPtr( dc );
161 return GetClipBox(hdc, &rect);
164 /***********************************************************************
165 * SelectVisRgn (GDI.105)
167 INT16 WINAPI SelectVisRgn16( HDC16 hdc16, HRGN16 hrgn )
170 HDC hdc = HDC_32( hdc16 );
173 if (!hrgn) return ERROR;
174 if (!(dc = DC_GetDCPtr( hdc ))) return ERROR;
176 TRACE("%p %04x\n", hdc, hrgn );
180 retval = CombineRgn( dc->hVisRgn, HRGN_32(hrgn), 0, RGN_COPY );
181 CLIPPING_UpdateGCRegion( dc );
182 DC_ReleaseDCPtr( dc );
187 /***********************************************************************
188 * OffsetClipRgn (GDI32.@)
190 INT WINAPI OffsetClipRgn( HDC hdc, INT x, INT y )
192 INT ret = SIMPLEREGION;
193 DC *dc = DC_GetDCUpdate( hdc );
194 if (!dc) return ERROR;
196 TRACE("%p %d,%d\n", hdc, x, y );
198 if(dc->funcs->pOffsetClipRgn)
200 ret = dc->funcs->pOffsetClipRgn( dc->physDev, x, y );
201 /* FIXME: ret is just a success flag, we should return a proper value */
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 );
208 DC_ReleaseDCPtr( dc );
213 /***********************************************************************
214 * OffsetVisRgn (GDI.102)
216 INT16 WINAPI OffsetVisRgn16( HDC16 hdc16, INT16 x, INT16 y )
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 );
230 /***********************************************************************
231 * ExcludeClipRect (GDI32.@)
233 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
234 INT right, INT bottom )
238 DC *dc = DC_GetDCUpdate( hdc );
239 if (!dc) return ERROR;
241 TRACE("%p %dx%d,%dx%d\n", hdc, left, top, right, bottom );
243 if(dc->funcs->pExcludeClipRect)
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 */
256 LPtoDP( hdc, pt, 2 );
257 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
261 create_default_clip_region( dc );
262 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_DIFF );
263 DeleteObject( newRgn );
265 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
267 DC_ReleaseDCPtr( dc );
272 /***********************************************************************
273 * IntersectClipRect (GDI32.@)
275 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
278 DC *dc = DC_GetDCUpdate( hdc );
279 if (!dc) return ERROR;
281 TRACE("%p %d,%d - %d,%d\n", hdc, left, top, right, bottom );
283 if(dc->funcs->pIntersectClipRect)
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 */
297 LPtoDP( hdc, pt, 2 );
301 dc->hClipRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y );
308 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
311 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_AND );
312 DeleteObject( newRgn );
315 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
317 DC_ReleaseDCPtr( dc );
322 /***********************************************************************
323 * ExcludeVisRect (GDI.73)
325 INT16 WINAPI ExcludeVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
330 HDC hdc = HDC_32( hdc16 );
331 DC * dc = DC_GetDCUpdate( hdc );
332 if (!dc) return ERROR;
339 LPtoDP( hdc, pt, 2 );
341 TRACE("%p %d,%d - %d,%d\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
343 if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
346 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_DIFF );
347 DeleteObject( tempRgn );
349 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
350 DC_ReleaseDCPtr( dc );
355 /***********************************************************************
356 * IntersectVisRect (GDI.98)
358 INT16 WINAPI IntersectVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
363 HDC hdc = HDC_32( hdc16 );
364 DC * dc = DC_GetDCUpdate( hdc );
365 if (!dc) return ERROR;
372 LPtoDP( hdc, pt, 2 );
374 TRACE("%p %d,%d - %d,%d\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
377 if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
380 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_AND );
381 DeleteObject( tempRgn );
383 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
384 DC_ReleaseDCPtr( dc );
389 /***********************************************************************
390 * PtVisible (GDI32.@)
392 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
397 DC *dc = DC_GetDCUpdate( hdc );
399 TRACE("%p %d,%d\n", hdc, x, y );
400 if (!dc) return FALSE;
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 );
412 /***********************************************************************
413 * RectVisible (GDI32.@)
415 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
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 );
425 LPtoDP( hdc, (POINT *)&tmpRect, 2 );
427 if ((clip = get_clip_region(dc)))
429 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
430 CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
431 ret = RectInRegion( hrgn, &tmpRect );
432 DeleteObject( hrgn );
434 else ret = RectInRegion( dc->hVisRgn, &tmpRect );
435 DC_ReleaseDCPtr( dc );
440 /***********************************************************************
441 * GetClipBox (GDI32.@)
443 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
447 DC *dc = DC_GetDCUpdate( hdc );
448 if (!dc) return ERROR;
449 if ((clip = get_clip_region(dc)))
451 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
452 CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
453 ret = GetRgnBox( hrgn, rect );
454 DeleteObject( hrgn );
456 else ret = GetRgnBox( dc->hVisRgn, rect );
457 DPtoLP( hdc, (LPPOINT)rect, 2 );
458 DC_ReleaseDCPtr( dc );
463 /***********************************************************************
464 * GetClipRgn (GDI32.@)
466 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
470 if (hRgn && (dc = DC_GetDCPtr( hdc )))
474 if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR ) ret = 1;
477 DC_ReleaseDCPtr( dc );
483 /***********************************************************************
484 * GetMetaRgn (GDI32.@)
486 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
489 DC * dc = DC_GetDCPtr( hdc );
493 if (dc->hMetaRgn && CombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY ) != ERROR)
495 DC_ReleaseDCPtr( dc );
501 /***********************************************************************
502 * SaveVisRgn (GDI.129)
504 HRGN16 WINAPI SaveVisRgn16( HDC16 hdc16 )
506 struct saved_visrgn *saved;
507 HDC hdc = HDC_32( hdc16 );
508 DC *dc = DC_GetDCUpdate( hdc );
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);
522 DC_ReleaseDCPtr( dc );
523 HeapFree( GetProcessHeap(), 0, saved );
528 /***********************************************************************
529 * RestoreVisRgn (GDI.130)
531 INT16 WINAPI RestoreVisRgn16( HDC16 hdc16 )
533 struct saved_visrgn *saved;
534 HDC hdc = HDC_32( hdc16 );
535 DC *dc = DC_GetDCPtr( hdc );
538 if (!dc) return ERROR;
542 if (!(saved = dc->saved_visrgn)) goto done;
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 );
550 DC_ReleaseDCPtr( dc );
555 /***********************************************************************
556 * GetRandomRgn [GDI32.@]
559 * This function is documented in MSDN online for the case of
560 * iCode == SYSRGN (4).
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).
567 * See http://www.codeproject.com/gdi/cliprgnguide.asp
569 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, INT iCode)
572 DC *dc = DC_GetDCPtr( hDC );
585 rgn = dc->hMetaClipRgn;
586 if(!rgn) rgn = dc->hClipRgn;
587 if(!rgn) rgn = dc->hMetaRgn;
589 case SYSRGN: /* == 4 */
593 WARN("Unknown code %d\n", iCode);
594 DC_ReleaseDCPtr( dc );
597 if (rgn) CombineRgn( hRgn, rgn, 0, RGN_COPY );
598 DC_ReleaseDCPtr( dc );
600 /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
601 if (iCode == SYSRGN && !(GetVersion() & 0x80000000))
604 GetDCOrgEx( hDC, &org );
605 OffsetRgn( hRgn, org.x, org.y );
611 /***********************************************************************
612 * SetMetaRgn (GDI32.@)
614 INT WINAPI SetMetaRgn( HDC hdc )
618 DC *dc = DC_GetDCPtr( hdc );
620 if (!dc) return ERROR;
622 if (dc->hMetaClipRgn)
624 /* the intersection becomes the new meta region */
625 DeleteObject( dc->hMetaRgn );
626 DeleteObject( dc->hClipRgn );
627 dc->hMetaRgn = dc->hMetaClipRgn;
629 dc->hMetaClipRgn = 0;
631 else if (dc->hClipRgn)
633 dc->hMetaRgn = dc->hClipRgn;
636 /* else nothing to do */
638 /* Note: no need to call CLIPPING_UpdateGCRegion, the overall clip region hasn't changed */
640 ret = GetRgnBox( dc->hMetaRgn, &dummy );
641 DC_ReleaseDCPtr( dc );