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 * Compute a clip rectangle from its logical coordinates.
37 static inline RECT get_clip_rect( DC * dc, int left, int top, int right, int bottom )
45 LPtoDP( dc->hSelf, (POINT *)&rect, 2 );
46 if (dc->layout & LAYOUT_RTL)
49 rect.left = rect.right + 1;
55 /***********************************************************************
58 * Clip a rectangle to the DC visible rect.
60 BOOL clip_visrect( DC *dc, RECT *dst, const RECT *src )
64 if (!GetRgnBox( get_dc_region(dc), &clip ))
67 return !is_rect_empty( dst );
69 return intersect_rect( dst, src, &clip );
72 /***********************************************************************
73 * CLIPPING_UpdateGCRegion
75 * Update the GC clip region when the ClipRgn or VisRgn have changed.
77 void CLIPPING_UpdateGCRegion( DC * dc )
80 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetDeviceClipping );
82 /* update the intersection of meta and clip regions */
83 if (dc->hMetaRgn && dc->hClipRgn)
85 if (!dc->hMetaClipRgn) dc->hMetaClipRgn = CreateRectRgn( 0, 0, 0, 0 );
86 CombineRgn( dc->hMetaClipRgn, dc->hClipRgn, dc->hMetaRgn, RGN_AND );
88 else /* only one is set, no need for an intersection */
90 if (dc->hMetaClipRgn) DeleteObject( dc->hMetaClipRgn );
93 clip_rgn = get_clip_region( dc );
94 if (clip_rgn || dc->hVisRgn)
96 if (!dc->region) dc->region = CreateRectRgn( 0, 0, 0, 0 );
97 CombineRgn( dc->region, dc->hVisRgn, clip_rgn, clip_rgn ? RGN_AND : RGN_COPY );
101 if (dc->region) DeleteObject( dc->region );
104 physdev->funcs->pSetDeviceClipping( physdev, dc->region );
107 /***********************************************************************
108 * create_default_clip_region
110 * Create a default clipping region when none already exists.
112 static inline void create_default_clip_region( DC * dc )
116 if (dc->header.type == OBJ_MEMDC)
120 GetObjectW( dc->hBitmap, sizeof(bitmap), &bitmap );
121 width = bitmap.bmWidth;
122 height = bitmap.bmHeight;
126 width = GetDeviceCaps( dc->hSelf, DESKTOPHORZRES );
127 height = GetDeviceCaps( dc->hSelf, DESKTOPVERTRES );
129 dc->hClipRgn = CreateRectRgn( 0, 0, width, height );
133 /***********************************************************************
134 * null driver fallback implementations
137 INT nulldrv_ExtSelectClipRgn( PHYSDEV dev, HRGN rgn, INT mode )
139 DC *dc = get_nulldrv_dc( dev );
144 if (mode != RGN_COPY)
146 FIXME("Unimplemented: hrgn NULL in mode: %d\n", mode);
149 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
157 if (dc->layout & LAYOUT_RTL)
159 if (!(mirrored = CreateRectRgn( 0, 0, 0, 0 ))) return ERROR;
160 mirror_region( mirrored, rgn, dc->vis_rect.right - dc->vis_rect.left );
165 create_default_clip_region( dc );
167 if (mode == RGN_COPY)
168 ret = CombineRgn( dc->hClipRgn, rgn, 0, mode );
170 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, rgn, mode);
172 if (mirrored) DeleteObject( mirrored );
174 CLIPPING_UpdateGCRegion( dc );
178 INT nulldrv_ExcludeClipRect( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
180 DC *dc = get_nulldrv_dc( dev );
181 RECT rect = get_clip_rect( dc, left, top, right, bottom );
185 if (!(rgn = CreateRectRgnIndirect( &rect ))) return ERROR;
186 if (!dc->hClipRgn) create_default_clip_region( dc );
187 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, rgn, RGN_DIFF );
189 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
193 INT nulldrv_IntersectClipRect( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
195 DC *dc = get_nulldrv_dc( dev );
196 RECT rect = get_clip_rect( dc, left, top, right, bottom );
202 dc->hClipRgn = CreateRectRgnIndirect( &rect );
207 if (!(rgn = CreateRectRgnIndirect( &rect ))) return ERROR;
208 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, rgn, RGN_AND );
211 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
215 INT nulldrv_OffsetClipRgn( PHYSDEV dev, INT x, INT y )
217 DC *dc = get_nulldrv_dc( dev );
218 INT ret = NULLREGION;
222 x = MulDiv( x, dc->vportExtX, dc->wndExtX );
223 y = MulDiv( y, dc->vportExtY, dc->wndExtY );
224 if (dc->layout & LAYOUT_RTL) x = -x;
225 ret = OffsetRgn( dc->hClipRgn, x, y );
226 CLIPPING_UpdateGCRegion( dc );
232 /***********************************************************************
233 * SelectClipRgn (GDI32.@)
235 INT WINAPI SelectClipRgn( HDC hdc, HRGN hrgn )
237 return ExtSelectClipRgn( hdc, hrgn, RGN_COPY );
241 /******************************************************************************
242 * ExtSelectClipRgn [GDI32.@]
244 INT WINAPI ExtSelectClipRgn( HDC hdc, HRGN hrgn, INT fnMode )
247 DC * dc = get_dc_ptr( hdc );
249 TRACE("%p %p %d\n", hdc, hrgn, fnMode );
253 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pExtSelectClipRgn );
255 retval = physdev->funcs->pExtSelectClipRgn( physdev, hrgn, fnMode );
256 release_dc_ptr( dc );
261 /***********************************************************************
262 * __wine_set_visible_region (GDI32.@)
264 void CDECL __wine_set_visible_region( HDC hdc, HRGN hrgn, const RECT *vis_rect )
268 if (!(dc = get_dc_ptr( hdc ))) return;
270 TRACE( "%p %p %s\n", hdc, hrgn, wine_dbgstr_rect(vis_rect) );
272 /* map region to DC coordinates */
273 OffsetRgn( hrgn, -vis_rect->left, -vis_rect->top );
275 DeleteObject( dc->hVisRgn );
277 dc->vis_rect = *vis_rect;
279 DC_UpdateXforms( dc );
280 CLIPPING_UpdateGCRegion( dc );
281 release_dc_ptr( dc );
285 /***********************************************************************
286 * OffsetClipRgn (GDI32.@)
288 INT WINAPI OffsetClipRgn( HDC hdc, INT x, INT y )
291 DC *dc = get_dc_ptr( hdc );
293 TRACE("%p %d,%d\n", hdc, x, y );
297 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pOffsetClipRgn );
299 ret = physdev->funcs->pOffsetClipRgn( physdev, x, y );
300 release_dc_ptr( dc );
306 /***********************************************************************
307 * ExcludeClipRect (GDI32.@)
309 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
310 INT right, INT bottom )
313 DC *dc = get_dc_ptr( hdc );
315 TRACE("%p %d,%d-%d,%d\n", hdc, left, top, right, bottom );
319 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pExcludeClipRect );
321 ret = physdev->funcs->pExcludeClipRect( physdev, left, top, right, bottom );
322 release_dc_ptr( dc );
328 /***********************************************************************
329 * IntersectClipRect (GDI32.@)
331 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
334 DC *dc = get_dc_ptr( hdc );
336 TRACE("%p %d,%d - %d,%d\n", hdc, left, top, right, bottom );
340 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pIntersectClipRect );
342 ret = physdev->funcs->pIntersectClipRect( physdev, left, top, right, bottom );
343 release_dc_ptr( dc );
349 /***********************************************************************
350 * PtVisible (GDI32.@)
352 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
356 DC *dc = get_dc_ptr( hdc );
358 TRACE("%p %d,%d\n", hdc, x, y );
359 if (!dc) return FALSE;
363 LPtoDP( hdc, &pt, 1 );
365 ret = PtInRegion( get_dc_region(dc), pt.x, pt.y );
366 release_dc_ptr( dc );
371 /***********************************************************************
372 * RectVisible (GDI32.@)
374 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
378 DC *dc = get_dc_ptr( hdc );
379 if (!dc) return FALSE;
380 TRACE("%p %s\n", hdc, wine_dbgstr_rect( rect ));
383 LPtoDP( hdc, (POINT *)&tmpRect, 2 );
386 ret = RectInRegion( get_dc_region(dc), &tmpRect );
387 release_dc_ptr( dc );
392 /***********************************************************************
393 * GetClipBox (GDI32.@)
395 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
398 DC *dc = get_dc_ptr( hdc );
399 if (!dc) return ERROR;
402 ret = GetRgnBox( get_dc_region(dc), rect );
403 if (dc->layout & LAYOUT_RTL)
405 int tmp = rect->left;
406 rect->left = rect->right - 1;
407 rect->right = tmp - 1;
409 DPtoLP( hdc, (LPPOINT)rect, 2 );
410 release_dc_ptr( dc );
411 TRACE("%p => %d %s\n", hdc, ret, wine_dbgstr_rect( rect ));
416 /***********************************************************************
417 * GetClipRgn (GDI32.@)
419 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
423 if ((dc = get_dc_ptr( hdc )))
427 if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR )
430 if (dc->layout & LAYOUT_RTL)
431 mirror_region( hRgn, hRgn, dc->vis_rect.right - dc->vis_rect.left );
435 release_dc_ptr( dc );
441 /***********************************************************************
442 * GetMetaRgn (GDI32.@)
444 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
447 DC * dc = get_dc_ptr( hdc );
451 if (dc->hMetaRgn && CombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY ) != ERROR)
454 if (dc->layout & LAYOUT_RTL)
455 mirror_region( hRgn, hRgn, dc->vis_rect.right - dc->vis_rect.left );
457 release_dc_ptr( dc );
463 /***********************************************************************
464 * GetRandomRgn [GDI32.@]
467 * This function is documented in MSDN online for the case of
468 * iCode == SYSRGN (4).
470 * For iCode == 1 it should return the clip region
471 * 2 " " " the meta region
472 * 3 " " " the intersection of the clip with
473 * the meta region (== 'Rao' region).
475 * See http://www.codeproject.com/gdi/cliprgnguide.asp
477 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, INT iCode)
480 DC *dc = get_dc_ptr( hDC );
493 rgn = dc->hMetaClipRgn;
494 if(!rgn) rgn = dc->hClipRgn;
495 if(!rgn) rgn = dc->hMetaRgn;
497 case SYSRGN: /* == 4 */
502 WARN("Unknown code %d\n", iCode);
503 release_dc_ptr( dc );
506 if (rgn) CombineRgn( hRgn, rgn, 0, RGN_COPY );
507 release_dc_ptr( dc );
509 /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
510 if (iCode == SYSRGN && !(GetVersion() & 0x80000000))
511 OffsetRgn( hRgn, dc->vis_rect.left, dc->vis_rect.top );
517 /***********************************************************************
518 * SetMetaRgn (GDI32.@)
520 INT WINAPI SetMetaRgn( HDC hdc )
524 DC *dc = get_dc_ptr( hdc );
526 if (!dc) return ERROR;
528 if (dc->hMetaClipRgn)
530 /* the intersection becomes the new meta region */
531 DeleteObject( dc->hMetaRgn );
532 DeleteObject( dc->hClipRgn );
533 dc->hMetaRgn = dc->hMetaClipRgn;
535 dc->hMetaClipRgn = 0;
537 else if (dc->hClipRgn)
539 dc->hMetaRgn = dc->hClipRgn;
542 /* else nothing to do */
544 /* Note: no need to call CLIPPING_UpdateGCRegion, the overall clip region hasn't changed */
546 ret = GetRgnBox( dc->hMetaRgn, &dummy );
547 release_dc_ptr( dc );