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 /* return the DC device rectangle if not empty */
33 static inline BOOL get_dc_device_rect( DC *dc, RECT *rect )
35 *rect = dc->device_rect;
36 offset_rect( rect, -dc->vis_rect.left, -dc->vis_rect.top );
37 return !is_rect_empty( rect );
40 /***********************************************************************
43 * Compute a clip rectangle from its logical coordinates.
45 static inline RECT get_clip_rect( DC * dc, int left, int top, int right, int bottom )
53 LPtoDP( dc->hSelf, (POINT *)&rect, 2 );
54 if (dc->layout & LAYOUT_RTL)
57 rect.left = rect.right + 1;
63 /***********************************************************************
66 * Clip a rectangle to the whole DC surface.
68 BOOL clip_device_rect( DC *dc, RECT *dst, const RECT *src )
72 if (get_dc_device_rect( dc, &clip )) return intersect_rect( dst, src, &clip );
77 /***********************************************************************
80 * Clip a rectangle to the DC visible rect.
82 BOOL clip_visrect( DC *dc, RECT *dst, const RECT *src )
86 if (!clip_device_rect( dc, dst, src )) return FALSE;
87 if (GetRgnBox( get_dc_region(dc), &clip )) return intersect_rect( dst, dst, &clip );
91 /***********************************************************************
94 * Update the DC and device clip regions when the ClipRgn or VisRgn have changed.
96 void update_dc_clipping( DC * dc )
98 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetDeviceClipping );
102 if (dc->hVisRgn) regions[count++] = dc->hVisRgn;
103 if (dc->hClipRgn) regions[count++] = dc->hClipRgn;
104 if (dc->hMetaRgn) regions[count++] = dc->hMetaRgn;
108 if (!dc->region) dc->region = CreateRectRgn( 0, 0, 0, 0 );
109 CombineRgn( dc->region, regions[0], regions[1], RGN_AND );
110 if (count > 2) CombineRgn( dc->region, dc->region, regions[2], RGN_AND );
112 else /* only one region, we don't need the total region */
114 if (dc->region) DeleteObject( dc->region );
117 physdev->funcs->pSetDeviceClipping( physdev, get_dc_region( dc ));
120 /***********************************************************************
121 * create_default_clip_region
123 * Create a default clipping region when none already exists.
125 static inline void create_default_clip_region( DC * dc )
129 if (!get_dc_device_rect( dc, &rect ))
133 rect.right = GetDeviceCaps( dc->hSelf, DESKTOPHORZRES );
134 rect.bottom = GetDeviceCaps( dc->hSelf, DESKTOPVERTRES );
136 dc->hClipRgn = CreateRectRgnIndirect( &rect );
140 /***********************************************************************
141 * null driver fallback implementations
144 INT nulldrv_ExtSelectClipRgn( PHYSDEV dev, HRGN rgn, INT mode )
146 DC *dc = get_nulldrv_dc( dev );
151 if (mode != RGN_COPY)
153 FIXME("Unimplemented: hrgn NULL in mode: %d\n", mode);
156 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
164 if (dc->layout & LAYOUT_RTL)
166 if (!(mirrored = CreateRectRgn( 0, 0, 0, 0 ))) return ERROR;
167 mirror_region( mirrored, rgn, dc->vis_rect.right - dc->vis_rect.left );
172 create_default_clip_region( dc );
174 if (mode == RGN_COPY)
175 ret = CombineRgn( dc->hClipRgn, rgn, 0, mode );
177 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, rgn, mode);
179 if (mirrored) DeleteObject( mirrored );
181 update_dc_clipping( dc );
185 INT nulldrv_ExcludeClipRect( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
187 DC *dc = get_nulldrv_dc( dev );
188 RECT rect = get_clip_rect( dc, left, top, right, bottom );
192 if (!(rgn = CreateRectRgnIndirect( &rect ))) return ERROR;
193 if (!dc->hClipRgn) create_default_clip_region( dc );
194 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, rgn, RGN_DIFF );
196 if (ret != ERROR) update_dc_clipping( dc );
200 INT nulldrv_IntersectClipRect( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
202 DC *dc = get_nulldrv_dc( dev );
203 RECT rect = get_clip_rect( dc, left, top, right, bottom );
209 dc->hClipRgn = CreateRectRgnIndirect( &rect );
214 if (!(rgn = CreateRectRgnIndirect( &rect ))) return ERROR;
215 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, rgn, RGN_AND );
218 if (ret != ERROR) update_dc_clipping( dc );
222 INT nulldrv_OffsetClipRgn( PHYSDEV dev, INT x, INT y )
224 DC *dc = get_nulldrv_dc( dev );
225 INT ret = NULLREGION;
229 x = MulDiv( x, dc->vportExtX, dc->wndExtX );
230 y = MulDiv( y, dc->vportExtY, dc->wndExtY );
231 if (dc->layout & LAYOUT_RTL) x = -x;
232 ret = OffsetRgn( dc->hClipRgn, x, y );
233 update_dc_clipping( dc );
239 /***********************************************************************
240 * SelectClipRgn (GDI32.@)
242 INT WINAPI SelectClipRgn( HDC hdc, HRGN hrgn )
244 return ExtSelectClipRgn( hdc, hrgn, RGN_COPY );
248 /******************************************************************************
249 * ExtSelectClipRgn [GDI32.@]
251 INT WINAPI ExtSelectClipRgn( HDC hdc, HRGN hrgn, INT fnMode )
254 DC * dc = get_dc_ptr( hdc );
256 TRACE("%p %p %d\n", hdc, hrgn, fnMode );
260 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pExtSelectClipRgn );
262 retval = physdev->funcs->pExtSelectClipRgn( physdev, hrgn, fnMode );
263 release_dc_ptr( dc );
268 /***********************************************************************
269 * __wine_set_visible_region (GDI32.@)
271 void CDECL __wine_set_visible_region( HDC hdc, HRGN hrgn, const RECT *vis_rect )
275 if (!(dc = get_dc_ptr( hdc ))) return;
277 TRACE( "%p %p %s\n", hdc, hrgn, wine_dbgstr_rect(vis_rect) );
279 /* map region to DC coordinates */
280 OffsetRgn( hrgn, -vis_rect->left, -vis_rect->top );
282 if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
284 dc->vis_rect = *vis_rect;
286 DC_UpdateXforms( dc );
287 update_dc_clipping( dc );
288 release_dc_ptr( dc );
292 /***********************************************************************
293 * OffsetClipRgn (GDI32.@)
295 INT WINAPI OffsetClipRgn( HDC hdc, INT x, INT y )
298 DC *dc = get_dc_ptr( hdc );
300 TRACE("%p %d,%d\n", hdc, x, y );
304 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pOffsetClipRgn );
306 ret = physdev->funcs->pOffsetClipRgn( physdev, x, y );
307 release_dc_ptr( dc );
313 /***********************************************************************
314 * ExcludeClipRect (GDI32.@)
316 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
317 INT right, INT bottom )
320 DC *dc = get_dc_ptr( hdc );
322 TRACE("%p %d,%d-%d,%d\n", hdc, left, top, right, bottom );
326 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pExcludeClipRect );
328 ret = physdev->funcs->pExcludeClipRect( physdev, left, top, right, bottom );
329 release_dc_ptr( dc );
335 /***********************************************************************
336 * IntersectClipRect (GDI32.@)
338 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
341 DC *dc = get_dc_ptr( hdc );
343 TRACE("%p %d,%d - %d,%d\n", hdc, left, top, right, bottom );
347 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pIntersectClipRect );
349 ret = physdev->funcs->pIntersectClipRect( physdev, left, top, right, bottom );
350 release_dc_ptr( dc );
356 /***********************************************************************
357 * PtVisible (GDI32.@)
359 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
364 DC *dc = get_dc_ptr( hdc );
366 TRACE("%p %d,%d\n", hdc, x, y );
367 if (!dc) return FALSE;
371 LPtoDP( hdc, &pt, 1 );
373 ret = (!get_dc_device_rect( dc, &visrect ) ||
374 (pt.x >= visrect.left && pt.x < visrect.right &&
375 pt.y >= visrect.top && pt.y < visrect.bottom));
376 if (ret && get_dc_region( dc )) ret = PtInRegion( get_dc_region( dc ), pt.x, pt.y );
377 release_dc_ptr( dc );
382 /***********************************************************************
383 * RectVisible (GDI32.@)
385 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
387 RECT tmpRect, visrect;
389 DC *dc = get_dc_ptr( hdc );
390 if (!dc) return FALSE;
391 TRACE("%p %s\n", hdc, wine_dbgstr_rect( rect ));
394 LPtoDP( hdc, (POINT *)&tmpRect, 2 );
397 ret = (!get_dc_device_rect( dc, &visrect ) || intersect_rect( &visrect, &visrect, &tmpRect ));
398 if (ret && get_dc_region( dc )) ret = RectInRegion( get_dc_region( dc ), &tmpRect );
399 release_dc_ptr( dc );
404 /***********************************************************************
405 * GetClipBox (GDI32.@)
407 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
411 DC *dc = get_dc_ptr( hdc );
412 if (!dc) return ERROR;
415 if (get_dc_region( dc ))
417 ret = GetRgnBox( get_dc_region( dc ), rect );
418 if (get_dc_device_rect( dc, &visrect ) && !intersect_rect( rect, rect, &visrect ))
423 ret = is_rect_empty( &dc->vis_rect ) ? ERROR : SIMPLEREGION;
424 *rect = dc->vis_rect;
427 if (dc->layout & LAYOUT_RTL)
429 int tmp = rect->left;
430 rect->left = rect->right - 1;
431 rect->right = tmp - 1;
433 DPtoLP( hdc, (LPPOINT)rect, 2 );
434 release_dc_ptr( dc );
435 TRACE("%p => %d %s\n", hdc, ret, wine_dbgstr_rect( rect ));
440 /***********************************************************************
441 * GetClipRgn (GDI32.@)
443 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
447 if ((dc = get_dc_ptr( hdc )))
451 if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR )
454 if (dc->layout & LAYOUT_RTL)
455 mirror_region( hRgn, hRgn, dc->vis_rect.right - dc->vis_rect.left );
459 release_dc_ptr( dc );
465 /***********************************************************************
466 * GetMetaRgn (GDI32.@)
468 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
471 DC * dc = get_dc_ptr( hdc );
475 if (dc->hMetaRgn && CombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY ) != ERROR)
478 if (dc->layout & LAYOUT_RTL)
479 mirror_region( hRgn, hRgn, dc->vis_rect.right - dc->vis_rect.left );
481 release_dc_ptr( dc );
487 /***********************************************************************
488 * GetRandomRgn [GDI32.@]
491 * This function is documented in MSDN online for the case of
492 * iCode == SYSRGN (4).
494 * For iCode == 1 it should return the clip region
495 * 2 " " " the meta region
496 * 3 " " " the intersection of the clip with
497 * the meta region (== 'Rao' region).
499 * See http://www.codeproject.com/gdi/cliprgnguide.asp
501 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, INT iCode)
504 DC *dc = get_dc_ptr( hDC );
511 if (dc->hClipRgn) CombineRgn( hRgn, dc->hClipRgn, 0, RGN_COPY );
515 if (dc->hMetaRgn) CombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY );
519 if (dc->hClipRgn && dc->hMetaRgn) CombineRgn( hRgn, dc->hClipRgn, dc->hMetaRgn, RGN_AND );
520 else if (dc->hClipRgn) CombineRgn( hRgn, dc->hClipRgn, 0, RGN_COPY );
521 else if (dc->hMetaRgn) CombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY );
524 case SYSRGN: /* == 4 */
528 CombineRgn( hRgn, dc->hVisRgn, 0, RGN_COPY );
529 /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
530 if (!(GetVersion() & 0x80000000)) OffsetRgn( hRgn, dc->vis_rect.left, dc->vis_rect.top );
532 else if (!is_rect_empty( &dc->device_rect ))
533 SetRectRgn( hRgn, dc->device_rect.left, dc->device_rect.top,
534 dc->device_rect.right, dc->device_rect.bottom );
539 WARN("Unknown code %d\n", iCode);
543 release_dc_ptr( dc );
548 /***********************************************************************
549 * SetMetaRgn (GDI32.@)
551 INT WINAPI SetMetaRgn( HDC hdc )
555 DC *dc = get_dc_ptr( hdc );
557 if (!dc) return ERROR;
563 /* the intersection becomes the new meta region */
564 CombineRgn( dc->hMetaRgn, dc->hMetaRgn, dc->hClipRgn, RGN_AND );
565 DeleteObject( dc->hClipRgn );
570 dc->hMetaRgn = dc->hClipRgn;
574 /* else nothing to do */
576 /* Note: no need to call update_dc_clipping, the overall clip region hasn't changed */
578 ret = GetRgnBox( dc->hMetaRgn, &dummy );
579 release_dc_ptr( dc );