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 "wine/winuser16.h"
28 #include "gdi_private.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(clipping);
34 /***********************************************************************
37 * Return the total clip region (if any).
39 static inline HRGN get_clip_region( DC * dc )
41 if (dc->hMetaClipRgn) return dc->hMetaClipRgn;
42 if (dc->hMetaRgn) return dc->hMetaRgn;
47 /***********************************************************************
48 * CLIPPING_UpdateGCRegion
50 * Update the GC clip region when the ClipRgn or VisRgn have changed.
52 void CLIPPING_UpdateGCRegion( DC * dc )
58 ERR("hVisRgn is zero. Please report this.\n" );
62 if (dc->flags & DC_DIRTY) ERR( "DC is dirty. Please report this.\n" );
64 /* update the intersection of meta and clip regions */
65 if (dc->hMetaRgn && dc->hClipRgn)
67 if (!dc->hMetaClipRgn) dc->hMetaClipRgn = CreateRectRgn( 0, 0, 0, 0 );
68 CombineRgn( dc->hMetaClipRgn, dc->hClipRgn, dc->hMetaRgn, RGN_AND );
69 clip_rgn = dc->hMetaClipRgn;
71 else /* only one is set, no need for an intersection */
73 if (dc->hMetaClipRgn) DeleteObject( dc->hMetaClipRgn );
75 clip_rgn = dc->hMetaRgn ? dc->hMetaRgn : dc->hClipRgn;
78 if (dc->funcs->pSetDeviceClipping)
79 dc->funcs->pSetDeviceClipping( dc->physDev, dc->hVisRgn, clip_rgn );
82 /***********************************************************************
83 * create_default_clip_region
85 * Create a default clipping region when none already exists.
87 static inline void create_default_clip_region( DC * dc )
91 if (GDIMAGIC( dc->header.wMagic ) == MEMORY_DC_MAGIC)
95 GetObjectW( dc->hBitmap, sizeof(bitmap), &bitmap );
96 width = bitmap.bmWidth;
97 height = bitmap.bmHeight;
101 width = GetDeviceCaps( dc->hSelf, DESKTOPHORZRES );
102 height = GetDeviceCaps( dc->hSelf, DESKTOPVERTRES );
104 dc->hClipRgn = CreateRectRgn( 0, 0, width, height );
108 /***********************************************************************
109 * SelectClipRgn (GDI32.@)
111 INT WINAPI SelectClipRgn( HDC hdc, HRGN hrgn )
113 return ExtSelectClipRgn( hdc, hrgn, RGN_COPY );
117 /******************************************************************************
118 * ExtSelectClipRgn [GDI32.@]
120 INT WINAPI ExtSelectClipRgn( HDC hdc, HRGN hrgn, INT fnMode )
124 DC * dc = DC_GetDCUpdate( hdc );
125 if (!dc) return ERROR;
127 TRACE("%p %p %d\n", hdc, hrgn, fnMode );
129 if (dc->funcs->pExtSelectClipRgn)
131 retval = dc->funcs->pExtSelectClipRgn( dc->physDev, hrgn, fnMode );
132 GDI_ReleaseObj( hdc );
138 if (fnMode == RGN_COPY)
140 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
145 FIXME("Unimplemented: hrgn NULL in mode: %d\n", fnMode);
146 GDI_ReleaseObj( hdc );
153 create_default_clip_region( dc );
155 if(fnMode == RGN_COPY)
156 CombineRgn( dc->hClipRgn, hrgn, 0, fnMode );
158 CombineRgn( dc->hClipRgn, dc->hClipRgn, hrgn, fnMode);
161 CLIPPING_UpdateGCRegion( dc );
162 GDI_ReleaseObj( hdc );
164 return GetClipBox(hdc, &rect);
167 /***********************************************************************
168 * SelectVisRgn (GDI.105)
170 INT16 WINAPI SelectVisRgn16( HDC16 hdc16, HRGN16 hrgn )
173 HDC hdc = HDC_32( hdc16 );
176 if (!hrgn) return ERROR;
177 if (!(dc = DC_GetDCPtr( hdc ))) return ERROR;
179 TRACE("%p %04x\n", hdc, hrgn );
181 dc->flags &= ~DC_DIRTY;
183 retval = CombineRgn( dc->hVisRgn, HRGN_32(hrgn), 0, RGN_COPY );
184 CLIPPING_UpdateGCRegion( dc );
185 GDI_ReleaseObj( hdc );
190 /***********************************************************************
191 * OffsetClipRgn (GDI32.@)
193 INT WINAPI OffsetClipRgn( HDC hdc, INT x, INT y )
195 INT ret = SIMPLEREGION;
196 DC *dc = DC_GetDCUpdate( hdc );
197 if (!dc) return ERROR;
199 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 GDI_ReleaseObj( hdc );
216 /***********************************************************************
217 * OffsetVisRgn (GDI.102)
219 INT16 WINAPI OffsetVisRgn16( HDC16 hdc16, INT16 x, INT16 y )
222 HDC hdc = HDC_32( hdc16 );
223 DC * dc = DC_GetDCUpdate( hdc );
224 if (!dc) return ERROR;
225 TRACE("%p %d,%d\n", hdc, x, y );
226 retval = OffsetRgn( dc->hVisRgn, x, y );
227 CLIPPING_UpdateGCRegion( dc );
228 GDI_ReleaseObj( hdc );
233 /***********************************************************************
234 * ExcludeClipRect (GDI32.@)
236 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
237 INT right, INT bottom )
241 DC *dc = DC_GetDCUpdate( hdc );
242 if (!dc) return ERROR;
244 TRACE("%p %dx%d,%dx%d\n", hdc, left, top, right, bottom );
246 if(dc->funcs->pExcludeClipRect)
248 ret = dc->funcs->pExcludeClipRect( dc->physDev, left, top, right, bottom );
249 /* FIXME: ret is just a success flag, we should return a proper value */
259 LPtoDP( hdc, pt, 2 );
260 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
264 create_default_clip_region( dc );
265 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_DIFF );
266 DeleteObject( newRgn );
268 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
270 GDI_ReleaseObj( hdc );
275 /***********************************************************************
276 * IntersectClipRect (GDI32.@)
278 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
281 DC *dc = DC_GetDCUpdate( hdc );
282 if (!dc) return ERROR;
284 TRACE("%p %d,%d - %d,%d\n", hdc, left, top, right, bottom );
286 if(dc->funcs->pIntersectClipRect)
288 ret = dc->funcs->pIntersectClipRect( dc->physDev, left, top, right, bottom );
289 /* FIXME: ret is just a success flag, we should return a proper value */
300 LPtoDP( hdc, pt, 2 );
304 dc->hClipRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y );
311 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
314 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_AND );
315 DeleteObject( newRgn );
318 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
320 GDI_ReleaseObj( hdc );
325 /***********************************************************************
326 * ExcludeVisRect (GDI.73)
328 INT16 WINAPI ExcludeVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
333 HDC hdc = HDC_32( hdc16 );
334 DC * dc = DC_GetDCUpdate( hdc );
335 if (!dc) return ERROR;
342 LPtoDP( hdc, pt, 2 );
344 TRACE("%p %d,%d - %d,%d\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
346 if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
349 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_DIFF );
350 DeleteObject( tempRgn );
352 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
353 GDI_ReleaseObj( hdc );
358 /***********************************************************************
359 * IntersectVisRect (GDI.98)
361 INT16 WINAPI IntersectVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
366 HDC hdc = HDC_32( hdc16 );
367 DC * dc = DC_GetDCUpdate( hdc );
368 if (!dc) return ERROR;
375 LPtoDP( hdc, pt, 2 );
377 TRACE("%p %d,%d - %d,%d\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
380 if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
383 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_AND );
384 DeleteObject( tempRgn );
386 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
387 GDI_ReleaseObj( hdc );
392 /***********************************************************************
393 * PtVisible (GDI32.@)
395 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
400 DC *dc = DC_GetDCUpdate( hdc );
402 TRACE("%p %d,%d\n", hdc, x, y );
403 if (!dc) return FALSE;
407 LPtoDP( hdc, &pt, 1 );
408 ret = PtInRegion( dc->hVisRgn, pt.x, pt.y );
409 if (ret && (clip = get_clip_region(dc))) ret = PtInRegion( clip, pt.x, pt.y );
410 GDI_ReleaseObj( hdc );
415 /***********************************************************************
416 * RectVisible (GDI32.@)
418 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
423 DC *dc = DC_GetDCUpdate( hdc );
424 if (!dc) return FALSE;
425 TRACE("%p %d,%dx%d,%d\n", hdc, rect->left, rect->top, rect->right, rect->bottom );
428 LPtoDP( hdc, (POINT *)&tmpRect, 2 );
430 if ((clip = get_clip_region(dc)))
432 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
433 CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
434 ret = RectInRegion( hrgn, &tmpRect );
435 DeleteObject( hrgn );
437 else ret = RectInRegion( dc->hVisRgn, &tmpRect );
438 GDI_ReleaseObj( hdc );
443 /***********************************************************************
444 * GetClipBox (GDI32.@)
446 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
450 DC *dc = DC_GetDCUpdate( hdc );
451 if (!dc) return ERROR;
452 if ((clip = get_clip_region(dc)))
454 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
455 CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
456 ret = GetRgnBox( hrgn, rect );
457 DeleteObject( hrgn );
459 else ret = GetRgnBox( dc->hVisRgn, rect );
460 DPtoLP( hdc, (LPPOINT)rect, 2 );
461 GDI_ReleaseObj( hdc );
466 /***********************************************************************
467 * GetClipRgn (GDI32.@)
469 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
473 if (hRgn && (dc = DC_GetDCPtr( hdc )))
477 if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR ) ret = 1;
480 GDI_ReleaseObj( hdc );
486 /***********************************************************************
487 * GetMetaRgn (GDI32.@)
489 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
492 DC * dc = DC_GetDCPtr( hdc );
496 if (dc->hMetaRgn && CombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY ) != ERROR)
498 GDI_ReleaseObj( hdc );
504 /***********************************************************************
505 * SaveVisRgn (GDI.129)
507 HRGN16 WINAPI SaveVisRgn16( HDC16 hdc16 )
509 struct saved_visrgn *saved;
510 HDC hdc = HDC_32( hdc16 );
511 DC *dc = DC_GetDCUpdate( hdc );
516 if (!(saved = HeapAlloc( GetProcessHeap(), 0, sizeof(*saved) ))) goto error;
517 if (!(saved->hrgn = CreateRectRgn( 0, 0, 0, 0 ))) goto error;
518 CombineRgn( saved->hrgn, dc->hVisRgn, 0, RGN_COPY );
519 saved->next = dc->saved_visrgn;
520 dc->saved_visrgn = saved;
521 GDI_ReleaseObj( hdc );
522 return HRGN_16(saved->hrgn);
525 GDI_ReleaseObj( hdc );
526 HeapFree( GetProcessHeap(), 0, saved );
531 /***********************************************************************
532 * RestoreVisRgn (GDI.130)
534 INT16 WINAPI RestoreVisRgn16( HDC16 hdc16 )
536 struct saved_visrgn *saved;
537 HDC hdc = HDC_32( hdc16 );
538 DC *dc = DC_GetDCPtr( hdc );
541 if (!dc) return ERROR;
545 if (!(saved = dc->saved_visrgn)) goto done;
547 ret = CombineRgn( dc->hVisRgn, saved->hrgn, 0, RGN_COPY );
548 dc->saved_visrgn = saved->next;
549 DeleteObject( saved->hrgn );
550 HeapFree( GetProcessHeap(), 0, saved );
551 dc->flags &= ~DC_DIRTY;
552 CLIPPING_UpdateGCRegion( dc );
554 GDI_ReleaseObj( hdc );
559 /***********************************************************************
560 * GetRandomRgn [GDI32.@]
563 * This function is documented in MSDN online for the case of
564 * iCode == SYSRGN (4).
566 * For iCode == 1 it should return the clip region
567 * 2 " " " the meta region
568 * 3 " " " the intersection of the clip with
569 * the meta region (== 'Rao' region).
571 * See http://www.codeproject.com/gdi/cliprgnguide.asp
573 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, INT iCode)
576 DC *dc = DC_GetDCPtr( hDC );
589 rgn = dc->hMetaClipRgn;
590 if(!rgn) rgn = dc->hClipRgn;
591 if(!rgn) rgn = dc->hMetaRgn;
593 case SYSRGN: /* == 4 */
597 WARN("Unknown code %d\n", iCode);
598 GDI_ReleaseObj( hDC );
601 if (rgn) CombineRgn( hRgn, rgn, 0, RGN_COPY );
602 GDI_ReleaseObj( hDC );
604 /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
605 if (iCode == SYSRGN && !(GetVersion() & 0x80000000))
608 GetDCOrgEx( hDC, &org );
609 OffsetRgn( hRgn, org.x, org.y );
615 /***********************************************************************
616 * SetMetaRgn (GDI32.@)
618 INT WINAPI SetMetaRgn( HDC hdc )
622 DC *dc = DC_GetDCPtr( hdc );
624 if (!dc) return ERROR;
626 if (dc->hMetaClipRgn)
628 /* the intersection becomes the new meta region */
629 DeleteObject( dc->hMetaRgn );
630 DeleteObject( dc->hClipRgn );
631 dc->hMetaRgn = dc->hMetaClipRgn;
633 dc->hMetaClipRgn = 0;
635 else if (dc->hClipRgn)
637 dc->hMetaRgn = dc->hClipRgn;
640 /* else nothing to do */
642 /* Note: no need to call CLIPPING_UpdateGCRegion, the overall clip region hasn't changed */
644 ret = GetRgnBox( dc->hMetaRgn, &dummy );
645 GDI_ReleaseObj( hdc );