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 )
56 ERR("hVisRgn is zero. Please report this.\n" );
60 /* update the intersection of meta and clip regions */
61 if (dc->hMetaRgn && dc->hClipRgn)
63 if (!dc->hMetaClipRgn) dc->hMetaClipRgn = CreateRectRgn( 0, 0, 0, 0 );
64 CombineRgn( dc->hMetaClipRgn, dc->hClipRgn, dc->hMetaRgn, RGN_AND );
65 clip_rgn = dc->hMetaClipRgn;
67 else /* only one is set, no need for an intersection */
69 if (dc->hMetaClipRgn) DeleteObject( dc->hMetaClipRgn );
71 clip_rgn = dc->hMetaRgn ? dc->hMetaRgn : dc->hClipRgn;
74 if (dc->funcs->pSetDeviceClipping)
75 dc->funcs->pSetDeviceClipping( dc->physDev, dc->hVisRgn, clip_rgn );
78 /***********************************************************************
79 * create_default_clip_region
81 * Create a default clipping region when none already exists.
83 static inline void create_default_clip_region( DC * dc )
87 if (dc->header.type == OBJ_MEMDC)
91 GetObjectW( dc->hBitmap, sizeof(bitmap), &bitmap );
92 width = bitmap.bmWidth;
93 height = bitmap.bmHeight;
97 width = GetDeviceCaps( dc->hSelf, DESKTOPHORZRES );
98 height = GetDeviceCaps( dc->hSelf, DESKTOPVERTRES );
100 dc->hClipRgn = CreateRectRgn( 0, 0, width, height );
104 /***********************************************************************
105 * SelectClipRgn (GDI32.@)
107 INT WINAPI SelectClipRgn( HDC hdc, HRGN hrgn )
109 return ExtSelectClipRgn( hdc, hrgn, RGN_COPY );
113 /******************************************************************************
114 * ExtSelectClipRgn [GDI32.@]
116 INT WINAPI ExtSelectClipRgn( HDC hdc, HRGN hrgn, INT fnMode )
120 DC * dc = get_dc_ptr( hdc );
121 if (!dc) return ERROR;
123 TRACE("%p %p %d\n", hdc, hrgn, fnMode );
126 if (dc->funcs->pExtSelectClipRgn)
128 retval = dc->funcs->pExtSelectClipRgn( dc->physDev, hrgn, fnMode );
129 release_dc_ptr( dc );
135 if (fnMode == RGN_COPY)
137 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
142 FIXME("Unimplemented: hrgn NULL in mode: %d\n", fnMode);
143 release_dc_ptr( 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 release_dc_ptr( dc );
161 return GetClipBox(hdc, &rect);
164 /***********************************************************************
165 * SelectVisRgn (GDI32.@)
167 * Note: not exported on Windows, only the 16-bit version is exported.
169 INT WINAPI SelectVisRgn( HDC hdc, HRGN hrgn )
174 if (!hrgn) return ERROR;
175 if (!(dc = get_dc_ptr( hdc ))) return ERROR;
177 TRACE("%p %p\n", hdc, hrgn );
181 retval = CombineRgn( dc->hVisRgn, 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 * ExcludeClipRect (GDI32.@)
218 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
219 INT right, INT bottom )
223 DC *dc = get_dc_ptr( hdc );
224 if (!dc) return ERROR;
226 TRACE("%p %dx%d,%dx%d\n", hdc, left, top, right, bottom );
229 if(dc->funcs->pExcludeClipRect)
231 ret = dc->funcs->pExcludeClipRect( dc->physDev, left, top, right, bottom );
232 /* FIXME: ret is just a success flag, we should return a proper value */
242 LPtoDP( hdc, pt, 2 );
243 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
247 create_default_clip_region( dc );
248 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_DIFF );
249 DeleteObject( newRgn );
251 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
253 release_dc_ptr( dc );
258 /***********************************************************************
259 * IntersectClipRect (GDI32.@)
261 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
264 DC *dc = get_dc_ptr( hdc );
265 if (!dc) return ERROR;
267 TRACE("%p %d,%d - %d,%d\n", hdc, left, top, right, bottom );
270 if(dc->funcs->pIntersectClipRect)
272 ret = dc->funcs->pIntersectClipRect( dc->physDev, left, top, right, bottom );
273 /* FIXME: ret is just a success flag, we should return a proper value */
284 LPtoDP( hdc, pt, 2 );
288 dc->hClipRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y );
295 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
298 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_AND );
299 DeleteObject( newRgn );
302 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
304 release_dc_ptr( dc );
309 /***********************************************************************
310 * PtVisible (GDI32.@)
312 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
317 DC *dc = get_dc_ptr( hdc );
319 TRACE("%p %d,%d\n", hdc, x, y );
320 if (!dc) return FALSE;
324 LPtoDP( hdc, &pt, 1 );
326 ret = PtInRegion( dc->hVisRgn, pt.x, pt.y );
327 if (ret && (clip = get_clip_region(dc))) ret = PtInRegion( clip, pt.x, pt.y );
328 release_dc_ptr( dc );
333 /***********************************************************************
334 * RectVisible (GDI32.@)
336 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
341 DC *dc = get_dc_ptr( hdc );
342 if (!dc) return FALSE;
343 TRACE("%p %d,%dx%d,%d\n", hdc, rect->left, rect->top, rect->right, rect->bottom );
346 LPtoDP( hdc, (POINT *)&tmpRect, 2 );
349 if ((clip = get_clip_region(dc)))
351 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
352 CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
353 ret = RectInRegion( hrgn, &tmpRect );
354 DeleteObject( hrgn );
356 else ret = RectInRegion( dc->hVisRgn, &tmpRect );
357 release_dc_ptr( dc );
362 /***********************************************************************
363 * GetClipBox (GDI32.@)
365 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
369 DC *dc = get_dc_ptr( hdc );
370 if (!dc) return ERROR;
373 if ((clip = get_clip_region(dc)))
375 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
376 CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
377 ret = GetRgnBox( hrgn, rect );
378 DeleteObject( hrgn );
380 else ret = GetRgnBox( dc->hVisRgn, rect );
381 DPtoLP( hdc, (LPPOINT)rect, 2 );
382 release_dc_ptr( dc );
387 /***********************************************************************
388 * GetClipRgn (GDI32.@)
390 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
394 if (hRgn && (dc = get_dc_ptr( hdc )))
398 if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR ) ret = 1;
401 release_dc_ptr( dc );
407 /***********************************************************************
408 * GetMetaRgn (GDI32.@)
410 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
413 DC * dc = get_dc_ptr( hdc );
417 if (dc->hMetaRgn && CombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY ) != ERROR)
419 release_dc_ptr( dc );
425 /***********************************************************************
426 * GetRandomRgn [GDI32.@]
429 * This function is documented in MSDN online for the case of
430 * iCode == SYSRGN (4).
432 * For iCode == 1 it should return the clip region
433 * 2 " " " the meta region
434 * 3 " " " the intersection of the clip with
435 * the meta region (== 'Rao' region).
437 * See http://www.codeproject.com/gdi/cliprgnguide.asp
439 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, INT iCode)
442 DC *dc = get_dc_ptr( hDC );
455 rgn = dc->hMetaClipRgn;
456 if(!rgn) rgn = dc->hClipRgn;
457 if(!rgn) rgn = dc->hMetaRgn;
459 case SYSRGN: /* == 4 */
464 WARN("Unknown code %d\n", iCode);
465 release_dc_ptr( dc );
468 if (rgn) CombineRgn( hRgn, rgn, 0, RGN_COPY );
469 release_dc_ptr( dc );
471 /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
472 if (iCode == SYSRGN && !(GetVersion() & 0x80000000))
475 GetDCOrgEx( hDC, &org );
476 OffsetRgn( hRgn, org.x, org.y );
482 /***********************************************************************
483 * SetMetaRgn (GDI32.@)
485 INT WINAPI SetMetaRgn( HDC hdc )
489 DC *dc = get_dc_ptr( hdc );
491 if (!dc) return ERROR;
493 if (dc->hMetaClipRgn)
495 /* the intersection becomes the new meta region */
496 DeleteObject( dc->hMetaRgn );
497 DeleteObject( dc->hClipRgn );
498 dc->hMetaRgn = dc->hMetaClipRgn;
500 dc->hMetaClipRgn = 0;
502 else if (dc->hClipRgn)
504 dc->hMetaRgn = dc->hClipRgn;
507 /* else nothing to do */
509 /* Note: no need to call CLIPPING_UpdateGCRegion, the overall clip region hasn't changed */
511 ret = GetRgnBox( dc->hMetaRgn, &dummy );
512 release_dc_ptr( dc );