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
26 #include "gdi_private.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(clipping);
32 /***********************************************************************
35 * Return the total clip region (if any).
37 static inline HRGN get_clip_region( DC * dc )
39 if (dc->hMetaClipRgn) return dc->hMetaClipRgn;
40 if (dc->hMetaRgn) return dc->hMetaRgn;
45 /***********************************************************************
46 * CLIPPING_UpdateGCRegion
48 * Update the GC clip region when the ClipRgn or VisRgn have changed.
50 void CLIPPING_UpdateGCRegion( DC * dc )
54 /* update the intersection of meta and clip regions */
55 if (dc->hMetaRgn && dc->hClipRgn)
57 if (!dc->hMetaClipRgn) dc->hMetaClipRgn = CreateRectRgn( 0, 0, 0, 0 );
58 CombineRgn( dc->hMetaClipRgn, dc->hClipRgn, dc->hMetaRgn, RGN_AND );
59 clip_rgn = dc->hMetaClipRgn;
61 else /* only one is set, no need for an intersection */
63 if (dc->hMetaClipRgn) DeleteObject( dc->hMetaClipRgn );
65 clip_rgn = dc->hMetaRgn ? dc->hMetaRgn : dc->hClipRgn;
68 if (dc->funcs->pSetDeviceClipping)
69 dc->funcs->pSetDeviceClipping( dc->physDev, dc->hVisRgn, clip_rgn );
72 /***********************************************************************
73 * create_default_clip_region
75 * Create a default clipping region when none already exists.
77 static inline void create_default_clip_region( DC * dc )
81 if (dc->header.type == OBJ_MEMDC)
85 GetObjectW( dc->hBitmap, sizeof(bitmap), &bitmap );
86 width = bitmap.bmWidth;
87 height = bitmap.bmHeight;
91 width = GetDeviceCaps( dc->hSelf, DESKTOPHORZRES );
92 height = GetDeviceCaps( dc->hSelf, DESKTOPVERTRES );
94 dc->hClipRgn = CreateRectRgn( 0, 0, width, height );
98 /***********************************************************************
99 * SelectClipRgn (GDI32.@)
101 INT WINAPI SelectClipRgn( HDC hdc, HRGN hrgn )
103 return ExtSelectClipRgn( hdc, hrgn, RGN_COPY );
107 /******************************************************************************
108 * ExtSelectClipRgn [GDI32.@]
110 INT WINAPI ExtSelectClipRgn( HDC hdc, HRGN hrgn, INT fnMode )
114 DC * dc = get_dc_ptr( hdc );
115 if (!dc) return ERROR;
117 TRACE("%p %p %d\n", hdc, hrgn, fnMode );
120 if (dc->funcs->pExtSelectClipRgn)
122 retval = dc->funcs->pExtSelectClipRgn( dc->physDev, hrgn, fnMode );
123 release_dc_ptr( dc );
129 if (fnMode == RGN_COPY)
131 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
136 FIXME("Unimplemented: hrgn NULL in mode: %d\n", fnMode);
137 release_dc_ptr( dc );
144 create_default_clip_region( dc );
146 if(fnMode == RGN_COPY)
147 CombineRgn( dc->hClipRgn, hrgn, 0, fnMode );
149 CombineRgn( dc->hClipRgn, dc->hClipRgn, hrgn, fnMode);
152 CLIPPING_UpdateGCRegion( dc );
153 release_dc_ptr( dc );
155 return GetClipBox(hdc, &rect);
158 /***********************************************************************
159 * SelectVisRgn (GDI32.@)
161 * Note: not exported on Windows, only the 16-bit version is exported.
163 INT WINAPI SelectVisRgn( HDC hdc, HRGN hrgn )
168 if (!hrgn) return ERROR;
169 if (!(dc = get_dc_ptr( hdc ))) return ERROR;
171 TRACE("%p %p\n", hdc, hrgn );
175 retval = CombineRgn( dc->hVisRgn, hrgn, 0, RGN_COPY );
176 CLIPPING_UpdateGCRegion( dc );
177 release_dc_ptr( dc );
182 /***********************************************************************
183 * OffsetClipRgn (GDI32.@)
185 INT WINAPI OffsetClipRgn( HDC hdc, INT x, INT y )
187 INT ret = SIMPLEREGION;
188 DC *dc = get_dc_ptr( hdc );
189 if (!dc) return ERROR;
191 TRACE("%p %d,%d\n", hdc, x, y );
194 if(dc->funcs->pOffsetClipRgn)
196 ret = dc->funcs->pOffsetClipRgn( dc->physDev, x, y );
197 /* FIXME: ret is just a success flag, we should return a proper value */
199 else if (dc->hClipRgn) {
200 ret = OffsetRgn( dc->hClipRgn, MulDiv( x, dc->vportExtX, dc->wndExtX ),
201 MulDiv( y, dc->vportExtY, dc->wndExtY ) );
202 CLIPPING_UpdateGCRegion( dc );
204 release_dc_ptr( dc );
209 /***********************************************************************
210 * ExcludeClipRect (GDI32.@)
212 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
213 INT right, INT bottom )
217 DC *dc = get_dc_ptr( hdc );
218 if (!dc) return ERROR;
220 TRACE("%p %dx%d,%dx%d\n", hdc, left, top, right, bottom );
223 if(dc->funcs->pExcludeClipRect)
225 ret = dc->funcs->pExcludeClipRect( dc->physDev, left, top, right, bottom );
226 /* FIXME: ret is just a success flag, we should return a proper value */
236 LPtoDP( hdc, pt, 2 );
237 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
241 create_default_clip_region( dc );
242 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_DIFF );
243 DeleteObject( newRgn );
245 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
247 release_dc_ptr( dc );
252 /***********************************************************************
253 * IntersectClipRect (GDI32.@)
255 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
258 DC *dc = get_dc_ptr( hdc );
259 if (!dc) return ERROR;
261 TRACE("%p %d,%d - %d,%d\n", hdc, left, top, right, bottom );
264 if(dc->funcs->pIntersectClipRect)
266 ret = dc->funcs->pIntersectClipRect( dc->physDev, left, top, right, bottom );
267 /* FIXME: ret is just a success flag, we should return a proper value */
278 LPtoDP( hdc, pt, 2 );
282 dc->hClipRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y );
289 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
292 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_AND );
293 DeleteObject( newRgn );
296 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
298 release_dc_ptr( dc );
303 /***********************************************************************
304 * PtVisible (GDI32.@)
306 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
311 DC *dc = get_dc_ptr( hdc );
313 TRACE("%p %d,%d\n", hdc, x, y );
314 if (!dc) return FALSE;
318 LPtoDP( hdc, &pt, 1 );
320 ret = PtInRegion( dc->hVisRgn, pt.x, pt.y );
321 if (ret && (clip = get_clip_region(dc))) ret = PtInRegion( clip, pt.x, pt.y );
322 release_dc_ptr( dc );
327 /***********************************************************************
328 * RectVisible (GDI32.@)
330 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
335 DC *dc = get_dc_ptr( hdc );
336 if (!dc) return FALSE;
337 TRACE("%p %d,%dx%d,%d\n", hdc, rect->left, rect->top, rect->right, rect->bottom );
340 LPtoDP( hdc, (POINT *)&tmpRect, 2 );
343 if ((clip = get_clip_region(dc)))
345 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
346 CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
347 ret = RectInRegion( hrgn, &tmpRect );
348 DeleteObject( hrgn );
350 else ret = RectInRegion( dc->hVisRgn, &tmpRect );
351 release_dc_ptr( dc );
356 /***********************************************************************
357 * GetClipBox (GDI32.@)
359 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
363 DC *dc = get_dc_ptr( hdc );
364 if (!dc) return ERROR;
367 if ((clip = get_clip_region(dc)))
369 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
370 CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
371 ret = GetRgnBox( hrgn, rect );
372 DeleteObject( hrgn );
374 else ret = GetRgnBox( dc->hVisRgn, rect );
375 DPtoLP( hdc, (LPPOINT)rect, 2 );
376 release_dc_ptr( dc );
381 /***********************************************************************
382 * GetClipRgn (GDI32.@)
384 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
388 if (hRgn && (dc = get_dc_ptr( hdc )))
392 if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR ) ret = 1;
395 release_dc_ptr( dc );
401 /***********************************************************************
402 * GetMetaRgn (GDI32.@)
404 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
407 DC * dc = get_dc_ptr( hdc );
411 if (dc->hMetaRgn && CombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY ) != ERROR)
413 release_dc_ptr( dc );
419 /***********************************************************************
420 * GetRandomRgn [GDI32.@]
423 * This function is documented in MSDN online for the case of
424 * iCode == SYSRGN (4).
426 * For iCode == 1 it should return the clip region
427 * 2 " " " the meta region
428 * 3 " " " the intersection of the clip with
429 * the meta region (== 'Rao' region).
431 * See http://www.codeproject.com/gdi/cliprgnguide.asp
433 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, INT iCode)
436 DC *dc = get_dc_ptr( hDC );
449 rgn = dc->hMetaClipRgn;
450 if(!rgn) rgn = dc->hClipRgn;
451 if(!rgn) rgn = dc->hMetaRgn;
453 case SYSRGN: /* == 4 */
458 WARN("Unknown code %d\n", iCode);
459 release_dc_ptr( dc );
462 if (rgn) CombineRgn( hRgn, rgn, 0, RGN_COPY );
463 release_dc_ptr( dc );
465 /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
466 if (iCode == SYSRGN && !(GetVersion() & 0x80000000))
469 GetDCOrgEx( hDC, &org );
470 OffsetRgn( hRgn, org.x, org.y );
476 /***********************************************************************
477 * SetMetaRgn (GDI32.@)
479 INT WINAPI SetMetaRgn( HDC hdc )
483 DC *dc = get_dc_ptr( hdc );
485 if (!dc) return ERROR;
487 if (dc->hMetaClipRgn)
489 /* the intersection becomes the new meta region */
490 DeleteObject( dc->hMetaRgn );
491 DeleteObject( dc->hClipRgn );
492 dc->hMetaRgn = dc->hMetaClipRgn;
494 dc->hMetaClipRgn = 0;
496 else if (dc->hClipRgn)
498 dc->hMetaRgn = dc->hClipRgn;
501 /* else nothing to do */
503 /* Note: no need to call CLIPPING_UpdateGCRegion, the overall clip region hasn't changed */
505 ret = GetRgnBox( dc->hMetaRgn, &dummy );
506 release_dc_ptr( dc );