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 visible rectangle if not empty */
33 static inline BOOL get_dc_visrect( DC *dc, RECT *rect )
37 rect->right = dc->vis_rect.right - dc->vis_rect.left;
38 rect->bottom = dc->vis_rect.bottom - dc->vis_rect.top;
39 return !is_rect_empty( rect );
42 /***********************************************************************
45 * Compute a clip rectangle from its logical coordinates.
47 static inline RECT get_clip_rect( DC * dc, int left, int top, int right, int bottom )
55 LPtoDP( dc->hSelf, (POINT *)&rect, 2 );
56 if (dc->layout & LAYOUT_RTL)
59 rect.left = rect.right + 1;
65 /***********************************************************************
68 * Clip a rectangle to the DC visible rect.
70 BOOL clip_visrect( DC *dc, RECT *dst, const RECT *src )
74 if (get_dc_visrect( dc, &clip )) intersect_rect( dst, src, &clip );
77 if (GetRgnBox( get_dc_region(dc), &clip )) intersect_rect( dst, dst, &clip );
78 return !is_rect_empty( dst );
81 /***********************************************************************
84 * Update the DC and device clip regions when the ClipRgn or VisRgn have changed.
86 void update_dc_clipping( DC * dc )
88 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetDeviceClipping );
92 if (dc->hVisRgn) regions[count++] = dc->hVisRgn;
93 if (dc->hClipRgn) regions[count++] = dc->hClipRgn;
94 if (dc->hMetaRgn) regions[count++] = dc->hMetaRgn;
98 if (!dc->region) dc->region = CreateRectRgn( 0, 0, 0, 0 );
99 CombineRgn( dc->region, regions[0], regions[1], RGN_AND );
100 if (count > 2) CombineRgn( dc->region, dc->region, regions[2], RGN_AND );
102 else /* only one region, we don't need the total region */
104 if (dc->region) DeleteObject( dc->region );
107 physdev->funcs->pSetDeviceClipping( physdev, get_dc_region( dc ));
110 /***********************************************************************
111 * create_default_clip_region
113 * Create a default clipping region when none already exists.
115 static inline void create_default_clip_region( DC * dc )
119 if (dc->header.type == OBJ_MEMDC)
121 width = dc->vis_rect.right - dc->vis_rect.left;
122 height = dc->vis_rect.bottom - dc->vis_rect.top;
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 update_dc_clipping( 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) update_dc_clipping( 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) update_dc_clipping( 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 update_dc_clipping( 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 if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
277 dc->vis_rect = *vis_rect;
279 DC_UpdateXforms( dc );
280 update_dc_clipping( 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 )
357 DC *dc = get_dc_ptr( hdc );
359 TRACE("%p %d,%d\n", hdc, x, y );
360 if (!dc) return FALSE;
364 LPtoDP( hdc, &pt, 1 );
366 ret = (get_dc_visrect( dc, &visrect ) &&
367 pt.x >= visrect.left && pt.x < visrect.right &&
368 pt.y >= visrect.top && pt.y < visrect.bottom);
369 if (ret && get_dc_region( dc )) ret = PtInRegion( get_dc_region( dc ), pt.x, pt.y );
370 release_dc_ptr( dc );
375 /***********************************************************************
376 * RectVisible (GDI32.@)
378 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
380 RECT tmpRect, visrect;
382 DC *dc = get_dc_ptr( hdc );
383 if (!dc) return FALSE;
384 TRACE("%p %s\n", hdc, wine_dbgstr_rect( rect ));
387 LPtoDP( hdc, (POINT *)&tmpRect, 2 );
390 ret = (get_dc_visrect( dc, &visrect ) && intersect_rect( &visrect, &visrect, &tmpRect ));
391 if (ret && get_dc_region( dc )) ret = RectInRegion( get_dc_region( dc ), &tmpRect );
392 release_dc_ptr( dc );
397 /***********************************************************************
398 * GetClipBox (GDI32.@)
400 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
404 DC *dc = get_dc_ptr( hdc );
405 if (!dc) return ERROR;
408 if (get_dc_region( dc ))
410 ret = GetRgnBox( get_dc_region( dc ), rect );
411 if (get_dc_visrect( dc, &visrect ) && !intersect_rect( rect, rect, &visrect ))
414 else ret = get_dc_visrect( dc, rect ) ? SIMPLEREGION : NULLREGION;
416 if (dc->layout & LAYOUT_RTL)
418 int tmp = rect->left;
419 rect->left = rect->right - 1;
420 rect->right = tmp - 1;
422 DPtoLP( hdc, (LPPOINT)rect, 2 );
423 release_dc_ptr( dc );
424 TRACE("%p => %d %s\n", hdc, ret, wine_dbgstr_rect( rect ));
429 /***********************************************************************
430 * GetClipRgn (GDI32.@)
432 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
436 if ((dc = get_dc_ptr( hdc )))
440 if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR )
443 if (dc->layout & LAYOUT_RTL)
444 mirror_region( hRgn, hRgn, dc->vis_rect.right - dc->vis_rect.left );
448 release_dc_ptr( dc );
454 /***********************************************************************
455 * GetMetaRgn (GDI32.@)
457 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
460 DC * dc = get_dc_ptr( hdc );
464 if (dc->hMetaRgn && CombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY ) != ERROR)
467 if (dc->layout & LAYOUT_RTL)
468 mirror_region( hRgn, hRgn, dc->vis_rect.right - dc->vis_rect.left );
470 release_dc_ptr( dc );
476 /***********************************************************************
477 * GetRandomRgn [GDI32.@]
480 * This function is documented in MSDN online for the case of
481 * iCode == SYSRGN (4).
483 * For iCode == 1 it should return the clip region
484 * 2 " " " the meta region
485 * 3 " " " the intersection of the clip with
486 * the meta region (== 'Rao' region).
488 * See http://www.codeproject.com/gdi/cliprgnguide.asp
490 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, INT iCode)
494 DC *dc = get_dc_ptr( hDC );
501 if (dc->hClipRgn) CombineRgn( hRgn, dc->hClipRgn, 0, RGN_COPY );
505 if (dc->hMetaRgn) CombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY );
509 if (dc->hClipRgn && dc->hMetaRgn) CombineRgn( hRgn, dc->hClipRgn, dc->hMetaRgn, RGN_AND );
510 else if (dc->hClipRgn) CombineRgn( hRgn, dc->hClipRgn, 0, RGN_COPY );
511 else if (dc->hMetaRgn) CombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY );
514 case SYSRGN: /* == 4 */
517 CombineRgn( hRgn, dc->hVisRgn, 0, RGN_COPY );
518 else if (get_dc_visrect( dc, &visrect ))
519 SetRectRgn( hRgn, visrect.left, visrect.top, visrect.right, visrect.bottom );
522 /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
523 if (ret && !(GetVersion() & 0x80000000)) OffsetRgn( hRgn, dc->vis_rect.left, dc->vis_rect.top );
526 WARN("Unknown code %d\n", iCode);
530 release_dc_ptr( dc );
535 /***********************************************************************
536 * SetMetaRgn (GDI32.@)
538 INT WINAPI SetMetaRgn( HDC hdc )
542 DC *dc = get_dc_ptr( hdc );
544 if (!dc) return ERROR;
550 /* the intersection becomes the new meta region */
551 CombineRgn( dc->hMetaRgn, dc->hMetaRgn, dc->hClipRgn, RGN_AND );
552 DeleteObject( dc->hClipRgn );
557 dc->hMetaRgn = dc->hClipRgn;
561 /* else nothing to do */
563 /* Note: no need to call update_dc_clipping, the overall clip region hasn't changed */
565 ret = GetRgnBox( dc->hMetaRgn, &dummy );
566 release_dc_ptr( dc );