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 /***********************************************************************
56 * CLIPPING_UpdateGCRegion
58 * Update the GC clip region when the ClipRgn or VisRgn have changed.
60 void CLIPPING_UpdateGCRegion( DC
* dc
)
63 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pSetDeviceClipping
);
65 /* update the intersection of meta and clip regions */
66 if (dc
->hMetaRgn
&& dc
->hClipRgn
)
68 if (!dc
->hMetaClipRgn
) dc
->hMetaClipRgn
= CreateRectRgn( 0, 0, 0, 0 );
69 CombineRgn( dc
->hMetaClipRgn
, dc
->hClipRgn
, dc
->hMetaRgn
, RGN_AND
);
70 clip_rgn
= dc
->hMetaClipRgn
;
72 else /* only one is set, no need for an intersection */
74 if (dc
->hMetaClipRgn
) DeleteObject( dc
->hMetaClipRgn
);
76 clip_rgn
= dc
->hMetaRgn
? dc
->hMetaRgn
: dc
->hClipRgn
;
78 physdev
->funcs
->pSetDeviceClipping( physdev
, dc
->hVisRgn
, clip_rgn
);
81 /***********************************************************************
82 * create_default_clip_region
84 * Create a default clipping region when none already exists.
86 static inline void create_default_clip_region( DC
* dc
)
90 if (dc
->header
.type
== OBJ_MEMDC
)
94 GetObjectW( dc
->hBitmap
, sizeof(bitmap
), &bitmap
);
95 width
= bitmap
.bmWidth
;
96 height
= bitmap
.bmHeight
;
100 width
= GetDeviceCaps( dc
->hSelf
, DESKTOPHORZRES
);
101 height
= GetDeviceCaps( dc
->hSelf
, DESKTOPVERTRES
);
103 dc
->hClipRgn
= CreateRectRgn( 0, 0, width
, height
);
107 /***********************************************************************
108 * null driver fallback implementations
111 INT CDECL
nulldrv_ExtSelectClipRgn( PHYSDEV dev
, HRGN rgn
, INT mode
)
113 DC
*dc
= get_nulldrv_dc( dev
);
118 if (mode
!= RGN_COPY
)
120 FIXME("Unimplemented: hrgn NULL in mode: %d\n", mode
);
123 if (dc
->hClipRgn
) DeleteObject( dc
->hClipRgn
);
131 if (dc
->layout
& LAYOUT_RTL
)
133 if (!(mirrored
= CreateRectRgn( 0, 0, 0, 0 ))) return ERROR
;
134 mirror_region( mirrored
, rgn
, dc
->vis_rect
.right
- dc
->vis_rect
.left
);
139 create_default_clip_region( dc
);
141 if (mode
== RGN_COPY
)
142 ret
= CombineRgn( dc
->hClipRgn
, rgn
, 0, mode
);
144 ret
= CombineRgn( dc
->hClipRgn
, dc
->hClipRgn
, rgn
, mode
);
146 if (mirrored
) DeleteObject( mirrored
);
148 CLIPPING_UpdateGCRegion( dc
);
152 INT CDECL
nulldrv_ExcludeClipRect( PHYSDEV dev
, INT left
, INT top
, INT right
, INT bottom
)
154 DC
*dc
= get_nulldrv_dc( dev
);
155 RECT rect
= get_clip_rect( dc
, left
, top
, right
, bottom
);
159 if (!(rgn
= CreateRectRgnIndirect( &rect
))) return ERROR
;
160 if (!dc
->hClipRgn
) create_default_clip_region( dc
);
161 ret
= CombineRgn( dc
->hClipRgn
, dc
->hClipRgn
, rgn
, RGN_DIFF
);
163 if (ret
!= ERROR
) CLIPPING_UpdateGCRegion( dc
);
167 INT CDECL
nulldrv_IntersectClipRect( PHYSDEV dev
, INT left
, INT top
, INT right
, INT bottom
)
169 DC
*dc
= get_nulldrv_dc( dev
);
170 RECT rect
= get_clip_rect( dc
, left
, top
, right
, bottom
);
176 dc
->hClipRgn
= CreateRectRgnIndirect( &rect
);
181 if (!(rgn
= CreateRectRgnIndirect( &rect
))) return ERROR
;
182 ret
= CombineRgn( dc
->hClipRgn
, dc
->hClipRgn
, rgn
, RGN_AND
);
185 if (ret
!= ERROR
) CLIPPING_UpdateGCRegion( dc
);
189 INT CDECL
nulldrv_OffsetClipRgn( PHYSDEV dev
, INT x
, INT y
)
191 DC
*dc
= get_nulldrv_dc( dev
);
192 INT ret
= NULLREGION
;
196 x
= MulDiv( x
, dc
->vportExtX
, dc
->wndExtX
);
197 y
= MulDiv( y
, dc
->vportExtY
, dc
->wndExtY
);
198 if (dc
->layout
& LAYOUT_RTL
) x
= -x
;
199 ret
= OffsetRgn( dc
->hClipRgn
, x
, y
);
200 CLIPPING_UpdateGCRegion( dc
);
206 /***********************************************************************
207 * SelectClipRgn (GDI32.@)
209 INT WINAPI
SelectClipRgn( HDC hdc
, HRGN hrgn
)
211 return ExtSelectClipRgn( hdc
, hrgn
, RGN_COPY
);
215 /******************************************************************************
216 * ExtSelectClipRgn [GDI32.@]
218 INT WINAPI
ExtSelectClipRgn( HDC hdc
, HRGN hrgn
, INT fnMode
)
221 DC
* dc
= get_dc_ptr( hdc
);
223 TRACE("%p %p %d\n", hdc
, hrgn
, fnMode
);
227 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pExtSelectClipRgn
);
229 retval
= physdev
->funcs
->pExtSelectClipRgn( physdev
, hrgn
, fnMode
);
230 release_dc_ptr( dc
);
235 /***********************************************************************
236 * __wine_set_visible_region (GDI32.@)
238 void CDECL
__wine_set_visible_region( HDC hdc
, HRGN hrgn
, const RECT
*vis_rect
)
242 if (!(dc
= get_dc_ptr( hdc
))) return;
244 TRACE( "%p %p %s\n", hdc
, hrgn
, wine_dbgstr_rect(vis_rect
) );
246 /* map region to DC coordinates */
247 OffsetRgn( hrgn
, -vis_rect
->left
, -vis_rect
->top
);
249 DeleteObject( dc
->hVisRgn
);
251 dc
->vis_rect
= *vis_rect
;
253 DC_UpdateXforms( dc
);
254 CLIPPING_UpdateGCRegion( dc
);
255 release_dc_ptr( dc
);
259 /***********************************************************************
260 * OffsetClipRgn (GDI32.@)
262 INT WINAPI
OffsetClipRgn( HDC hdc
, INT x
, INT y
)
265 DC
*dc
= get_dc_ptr( hdc
);
267 TRACE("%p %d,%d\n", hdc
, x
, y
);
271 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pOffsetClipRgn
);
273 ret
= physdev
->funcs
->pOffsetClipRgn( physdev
, x
, y
);
274 release_dc_ptr( dc
);
280 /***********************************************************************
281 * ExcludeClipRect (GDI32.@)
283 INT WINAPI
ExcludeClipRect( HDC hdc
, INT left
, INT top
,
284 INT right
, INT bottom
)
287 DC
*dc
= get_dc_ptr( hdc
);
289 TRACE("%p %d,%d-%d,%d\n", hdc
, left
, top
, right
, bottom
);
293 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pExcludeClipRect
);
295 ret
= physdev
->funcs
->pExcludeClipRect( physdev
, left
, top
, right
, bottom
);
296 release_dc_ptr( dc
);
302 /***********************************************************************
303 * IntersectClipRect (GDI32.@)
305 INT WINAPI
IntersectClipRect( HDC hdc
, INT left
, INT top
, INT right
, INT bottom
)
308 DC
*dc
= get_dc_ptr( hdc
);
310 TRACE("%p %d,%d - %d,%d\n", hdc
, left
, top
, right
, bottom
);
314 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pIntersectClipRect
);
316 ret
= physdev
->funcs
->pIntersectClipRect( physdev
, left
, top
, right
, bottom
);
317 release_dc_ptr( dc
);
323 /***********************************************************************
324 * PtVisible (GDI32.@)
326 BOOL WINAPI
PtVisible( HDC hdc
, INT x
, INT y
)
331 DC
*dc
= get_dc_ptr( hdc
);
333 TRACE("%p %d,%d\n", hdc
, x
, y
);
334 if (!dc
) return FALSE
;
338 LPtoDP( hdc
, &pt
, 1 );
340 ret
= PtInRegion( dc
->hVisRgn
, pt
.x
, pt
.y
);
341 if (ret
&& (clip
= get_clip_region(dc
))) ret
= PtInRegion( clip
, pt
.x
, pt
.y
);
342 release_dc_ptr( dc
);
347 /***********************************************************************
348 * RectVisible (GDI32.@)
350 BOOL WINAPI
RectVisible( HDC hdc
, const RECT
* rect
)
355 DC
*dc
= get_dc_ptr( hdc
);
356 if (!dc
) return FALSE
;
357 TRACE("%p %s\n", hdc
, wine_dbgstr_rect( rect
));
360 LPtoDP( hdc
, (POINT
*)&tmpRect
, 2 );
363 if ((clip
= get_clip_region(dc
)))
365 HRGN hrgn
= CreateRectRgn( 0, 0, 0, 0 );
366 CombineRgn( hrgn
, dc
->hVisRgn
, clip
, RGN_AND
);
367 ret
= RectInRegion( hrgn
, &tmpRect
);
368 DeleteObject( hrgn
);
370 else ret
= RectInRegion( dc
->hVisRgn
, &tmpRect
);
371 release_dc_ptr( dc
);
376 /***********************************************************************
377 * GetClipBox (GDI32.@)
379 INT WINAPI
GetClipBox( HDC hdc
, LPRECT rect
)
383 DC
*dc
= get_dc_ptr( hdc
);
384 if (!dc
) return ERROR
;
387 if ((clip
= get_clip_region(dc
)))
389 HRGN hrgn
= CreateRectRgn( 0, 0, 0, 0 );
390 CombineRgn( hrgn
, dc
->hVisRgn
, clip
, RGN_AND
);
391 ret
= GetRgnBox( hrgn
, rect
);
392 DeleteObject( hrgn
);
394 else ret
= GetRgnBox( dc
->hVisRgn
, rect
);
395 if (dc
->layout
& LAYOUT_RTL
)
397 int tmp
= rect
->left
;
398 rect
->left
= rect
->right
- 1;
399 rect
->right
= tmp
- 1;
401 DPtoLP( hdc
, (LPPOINT
)rect
, 2 );
402 release_dc_ptr( dc
);
403 TRACE("%p => %d %s\n", hdc
, ret
, wine_dbgstr_rect( rect
));
408 /***********************************************************************
409 * GetClipRgn (GDI32.@)
411 INT WINAPI
GetClipRgn( HDC hdc
, HRGN hRgn
)
415 if ((dc
= get_dc_ptr( hdc
)))
419 if( CombineRgn(hRgn
, dc
->hClipRgn
, 0, RGN_COPY
) != ERROR
)
422 if (dc
->layout
& LAYOUT_RTL
)
423 mirror_region( hRgn
, hRgn
, dc
->vis_rect
.right
- dc
->vis_rect
.left
);
427 release_dc_ptr( dc
);
433 /***********************************************************************
434 * GetMetaRgn (GDI32.@)
436 INT WINAPI
GetMetaRgn( HDC hdc
, HRGN hRgn
)
439 DC
* dc
= get_dc_ptr( hdc
);
443 if (dc
->hMetaRgn
&& CombineRgn( hRgn
, dc
->hMetaRgn
, 0, RGN_COPY
) != ERROR
)
446 if (dc
->layout
& LAYOUT_RTL
)
447 mirror_region( hRgn
, hRgn
, dc
->vis_rect
.right
- dc
->vis_rect
.left
);
449 release_dc_ptr( dc
);
455 /***********************************************************************
456 * GetRandomRgn [GDI32.@]
459 * This function is documented in MSDN online for the case of
460 * iCode == SYSRGN (4).
462 * For iCode == 1 it should return the clip region
463 * 2 " " " the meta region
464 * 3 " " " the intersection of the clip with
465 * the meta region (== 'Rao' region).
467 * See http://www.codeproject.com/gdi/cliprgnguide.asp
469 INT WINAPI
GetRandomRgn(HDC hDC
, HRGN hRgn
, INT iCode
)
472 DC
*dc
= get_dc_ptr( hDC
);
485 rgn
= dc
->hMetaClipRgn
;
486 if(!rgn
) rgn
= dc
->hClipRgn
;
487 if(!rgn
) rgn
= dc
->hMetaRgn
;
489 case SYSRGN
: /* == 4 */
494 WARN("Unknown code %d\n", iCode
);
495 release_dc_ptr( dc
);
498 if (rgn
) CombineRgn( hRgn
, rgn
, 0, RGN_COPY
);
499 release_dc_ptr( dc
);
501 /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
502 if (iCode
== SYSRGN
&& !(GetVersion() & 0x80000000))
503 OffsetRgn( hRgn
, dc
->vis_rect
.left
, dc
->vis_rect
.top
);
509 /***********************************************************************
510 * SetMetaRgn (GDI32.@)
512 INT WINAPI
SetMetaRgn( HDC hdc
)
516 DC
*dc
= get_dc_ptr( hdc
);
518 if (!dc
) return ERROR
;
520 if (dc
->hMetaClipRgn
)
522 /* the intersection becomes the new meta region */
523 DeleteObject( dc
->hMetaRgn
);
524 DeleteObject( dc
->hClipRgn
);
525 dc
->hMetaRgn
= dc
->hMetaClipRgn
;
527 dc
->hMetaClipRgn
= 0;
529 else if (dc
->hClipRgn
)
531 dc
->hMetaRgn
= dc
->hClipRgn
;
534 /* else nothing to do */
536 /* Note: no need to call CLIPPING_UpdateGCRegion, the overall clip region hasn't changed */
538 ret
= GetRgnBox( dc
->hMetaRgn
, &dummy
);
539 release_dc_ptr( dc
);