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 /***********************************************************************
48 * Compute a clip rectangle from its logical coordinates.
50 static inline RECT get_clip_rect( DC * dc, int left, int top, int right, int bottom )
58 LPtoDP( dc->hSelf, (POINT *)&rect, 2 );
59 if (dc->layout & LAYOUT_RTL)
62 rect.left = rect.right + 1;
68 /***********************************************************************
69 * CLIPPING_UpdateGCRegion
71 * Update the GC clip region when the ClipRgn or VisRgn have changed.
73 void CLIPPING_UpdateGCRegion( DC * dc )
76 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetDeviceClipping );
78 /* update the intersection of meta and clip regions */
79 if (dc->hMetaRgn && dc->hClipRgn)
81 if (!dc->hMetaClipRgn) dc->hMetaClipRgn = CreateRectRgn( 0, 0, 0, 0 );
82 CombineRgn( dc->hMetaClipRgn, dc->hClipRgn, dc->hMetaRgn, RGN_AND );
83 clip_rgn = dc->hMetaClipRgn;
85 else /* only one is set, no need for an intersection */
87 if (dc->hMetaClipRgn) DeleteObject( dc->hMetaClipRgn );
89 clip_rgn = dc->hMetaRgn ? dc->hMetaRgn : dc->hClipRgn;
91 physdev->funcs->pSetDeviceClipping( physdev, dc->hVisRgn, clip_rgn );
94 /***********************************************************************
95 * create_default_clip_region
97 * Create a default clipping region when none already exists.
99 static inline void create_default_clip_region( DC * dc )
103 if (dc->header.type == OBJ_MEMDC)
107 GetObjectW( dc->hBitmap, sizeof(bitmap), &bitmap );
108 width = bitmap.bmWidth;
109 height = bitmap.bmHeight;
113 width = GetDeviceCaps( dc->hSelf, DESKTOPHORZRES );
114 height = GetDeviceCaps( dc->hSelf, DESKTOPVERTRES );
116 dc->hClipRgn = CreateRectRgn( 0, 0, width, height );
120 /***********************************************************************
121 * null driver fallback implementations
124 INT CDECL nulldrv_ExtSelectClipRgn( PHYSDEV dev, HRGN rgn, INT mode )
126 DC *dc = get_nulldrv_dc( dev );
131 if (mode != RGN_COPY)
133 FIXME("Unimplemented: hrgn NULL in mode: %d\n", mode);
136 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
144 if (dc->layout & LAYOUT_RTL)
146 if (!(mirrored = CreateRectRgn( 0, 0, 0, 0 ))) return ERROR;
147 mirror_region( mirrored, rgn, dc->vis_rect.right - dc->vis_rect.left );
152 create_default_clip_region( dc );
154 if (mode == RGN_COPY)
155 ret = CombineRgn( dc->hClipRgn, rgn, 0, mode );
157 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, rgn, mode);
159 if (mirrored) DeleteObject( mirrored );
161 CLIPPING_UpdateGCRegion( dc );
165 INT CDECL nulldrv_ExcludeClipRect( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
167 DC *dc = get_nulldrv_dc( dev );
168 RECT rect = get_clip_rect( dc, left, top, right, bottom );
172 if (!(rgn = CreateRectRgnIndirect( &rect ))) return ERROR;
173 if (!dc->hClipRgn) create_default_clip_region( dc );
174 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, rgn, RGN_DIFF );
176 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
180 INT CDECL nulldrv_IntersectClipRect( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
182 DC *dc = get_nulldrv_dc( dev );
183 RECT rect = get_clip_rect( dc, left, top, right, bottom );
189 dc->hClipRgn = CreateRectRgnIndirect( &rect );
194 if (!(rgn = CreateRectRgnIndirect( &rect ))) return ERROR;
195 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, rgn, RGN_AND );
198 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
202 INT CDECL nulldrv_OffsetClipRgn( PHYSDEV dev, INT x, INT y )
204 DC *dc = get_nulldrv_dc( dev );
205 INT ret = NULLREGION;
209 x = MulDiv( x, dc->vportExtX, dc->wndExtX );
210 y = MulDiv( y, dc->vportExtY, dc->wndExtY );
211 if (dc->layout & LAYOUT_RTL) x = -x;
212 ret = OffsetRgn( dc->hClipRgn, x, y );
213 CLIPPING_UpdateGCRegion( dc );
219 /***********************************************************************
220 * SelectClipRgn (GDI32.@)
222 INT WINAPI SelectClipRgn( HDC hdc, HRGN hrgn )
224 return ExtSelectClipRgn( hdc, hrgn, RGN_COPY );
228 /******************************************************************************
229 * ExtSelectClipRgn [GDI32.@]
231 INT WINAPI ExtSelectClipRgn( HDC hdc, HRGN hrgn, INT fnMode )
234 DC * dc = get_dc_ptr( hdc );
236 TRACE("%p %p %d\n", hdc, hrgn, fnMode );
240 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pExtSelectClipRgn );
242 retval = physdev->funcs->pExtSelectClipRgn( physdev, hrgn, fnMode );
243 release_dc_ptr( dc );
248 /***********************************************************************
249 * __wine_set_visible_region (GDI32.@)
251 void CDECL __wine_set_visible_region( HDC hdc, HRGN hrgn, const RECT *vis_rect )
255 if (!(dc = get_dc_ptr( hdc ))) return;
257 TRACE( "%p %p %s\n", hdc, hrgn, wine_dbgstr_rect(vis_rect) );
259 /* map region to DC coordinates */
260 OffsetRgn( hrgn, -vis_rect->left, -vis_rect->top );
262 DeleteObject( dc->hVisRgn );
264 dc->vis_rect = *vis_rect;
266 DC_UpdateXforms( dc );
267 CLIPPING_UpdateGCRegion( dc );
268 release_dc_ptr( dc );
272 /***********************************************************************
273 * OffsetClipRgn (GDI32.@)
275 INT WINAPI OffsetClipRgn( HDC hdc, INT x, INT y )
278 DC *dc = get_dc_ptr( hdc );
280 TRACE("%p %d,%d\n", hdc, x, y );
284 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pOffsetClipRgn );
286 ret = physdev->funcs->pOffsetClipRgn( physdev, x, y );
287 release_dc_ptr( dc );
293 /***********************************************************************
294 * ExcludeClipRect (GDI32.@)
296 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
297 INT right, INT bottom )
300 DC *dc = get_dc_ptr( hdc );
302 TRACE("%p %d,%d-%d,%d\n", hdc, left, top, right, bottom );
306 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pExcludeClipRect );
308 ret = physdev->funcs->pExcludeClipRect( physdev, left, top, right, bottom );
309 release_dc_ptr( dc );
315 /***********************************************************************
316 * IntersectClipRect (GDI32.@)
318 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
321 DC *dc = get_dc_ptr( hdc );
323 TRACE("%p %d,%d - %d,%d\n", hdc, left, top, right, bottom );
327 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pIntersectClipRect );
329 ret = physdev->funcs->pIntersectClipRect( physdev, left, top, right, bottom );
330 release_dc_ptr( dc );
336 /***********************************************************************
337 * PtVisible (GDI32.@)
339 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
344 DC *dc = get_dc_ptr( hdc );
346 TRACE("%p %d,%d\n", hdc, x, y );
347 if (!dc) return FALSE;
351 LPtoDP( hdc, &pt, 1 );
353 ret = PtInRegion( dc->hVisRgn, pt.x, pt.y );
354 if (ret && (clip = get_clip_region(dc))) ret = PtInRegion( clip, pt.x, pt.y );
355 release_dc_ptr( dc );
360 /***********************************************************************
361 * RectVisible (GDI32.@)
363 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
368 DC *dc = get_dc_ptr( hdc );
369 if (!dc) return FALSE;
370 TRACE("%p %s\n", hdc, wine_dbgstr_rect( rect ));
373 LPtoDP( hdc, (POINT *)&tmpRect, 2 );
376 if ((clip = get_clip_region(dc)))
378 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
379 CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
380 ret = RectInRegion( hrgn, &tmpRect );
381 DeleteObject( hrgn );
383 else ret = RectInRegion( dc->hVisRgn, &tmpRect );
384 release_dc_ptr( dc );
389 /***********************************************************************
390 * GetClipBox (GDI32.@)
392 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
396 DC *dc = get_dc_ptr( hdc );
397 if (!dc) return ERROR;
400 if ((clip = get_clip_region(dc)))
402 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
403 CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
404 ret = GetRgnBox( hrgn, rect );
405 DeleteObject( hrgn );
407 else ret = GetRgnBox( dc->hVisRgn, rect );
408 if (dc->layout & LAYOUT_RTL)
410 int tmp = rect->left;
411 rect->left = rect->right - 1;
412 rect->right = tmp - 1;
414 DPtoLP( hdc, (LPPOINT)rect, 2 );
415 release_dc_ptr( dc );
416 TRACE("%p => %d %s\n", hdc, ret, wine_dbgstr_rect( rect ));
421 /***********************************************************************
422 * GetClipRgn (GDI32.@)
424 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
428 if ((dc = get_dc_ptr( hdc )))
432 if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR )
435 if (dc->layout & LAYOUT_RTL)
436 mirror_region( hRgn, hRgn, dc->vis_rect.right - dc->vis_rect.left );
440 release_dc_ptr( dc );
446 /***********************************************************************
447 * GetMetaRgn (GDI32.@)
449 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
452 DC * dc = get_dc_ptr( hdc );
456 if (dc->hMetaRgn && CombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY ) != ERROR)
459 if (dc->layout & LAYOUT_RTL)
460 mirror_region( hRgn, hRgn, dc->vis_rect.right - dc->vis_rect.left );
462 release_dc_ptr( dc );
468 /***********************************************************************
469 * GetRandomRgn [GDI32.@]
472 * This function is documented in MSDN online for the case of
473 * iCode == SYSRGN (4).
475 * For iCode == 1 it should return the clip region
476 * 2 " " " the meta region
477 * 3 " " " the intersection of the clip with
478 * the meta region (== 'Rao' region).
480 * See http://www.codeproject.com/gdi/cliprgnguide.asp
482 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, INT iCode)
485 DC *dc = get_dc_ptr( hDC );
498 rgn = dc->hMetaClipRgn;
499 if(!rgn) rgn = dc->hClipRgn;
500 if(!rgn) rgn = dc->hMetaRgn;
502 case SYSRGN: /* == 4 */
507 WARN("Unknown code %d\n", iCode);
508 release_dc_ptr( dc );
511 if (rgn) CombineRgn( hRgn, rgn, 0, RGN_COPY );
512 release_dc_ptr( dc );
514 /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
515 if (iCode == SYSRGN && !(GetVersion() & 0x80000000))
516 OffsetRgn( hRgn, dc->vis_rect.left, dc->vis_rect.top );
522 /***********************************************************************
523 * SetMetaRgn (GDI32.@)
525 INT WINAPI SetMetaRgn( HDC hdc )
529 DC *dc = get_dc_ptr( hdc );
531 if (!dc) return ERROR;
533 if (dc->hMetaClipRgn)
535 /* the intersection becomes the new meta region */
536 DeleteObject( dc->hMetaRgn );
537 DeleteObject( dc->hClipRgn );
538 dc->hMetaRgn = dc->hMetaClipRgn;
540 dc->hMetaClipRgn = 0;
542 else if (dc->hClipRgn)
544 dc->hMetaRgn = dc->hClipRgn;
547 /* else nothing to do */
549 /* Note: no need to call CLIPPING_UpdateGCRegion, the overall clip region hasn't changed */
551 ret = GetRgnBox( dc->hMetaRgn, &dummy );
552 release_dc_ptr( dc );