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
)
77 /* update the intersection of meta and clip regions */
78 if (dc
->hMetaRgn
&& dc
->hClipRgn
)
80 if (!dc
->hMetaClipRgn
) dc
->hMetaClipRgn
= CreateRectRgn( 0, 0, 0, 0 );
81 CombineRgn( dc
->hMetaClipRgn
, dc
->hClipRgn
, dc
->hMetaRgn
, RGN_AND
);
82 clip_rgn
= dc
->hMetaClipRgn
;
84 else /* only one is set, no need for an intersection */
86 if (dc
->hMetaClipRgn
) DeleteObject( dc
->hMetaClipRgn
);
88 clip_rgn
= dc
->hMetaRgn
? dc
->hMetaRgn
: dc
->hClipRgn
;
91 if (dc
->funcs
->pSetDeviceClipping
)
92 dc
->funcs
->pSetDeviceClipping( dc
->physDev
, dc
->hVisRgn
, clip_rgn
);
95 /***********************************************************************
96 * create_default_clip_region
98 * Create a default clipping region when none already exists.
100 static inline void create_default_clip_region( DC
* dc
)
104 if (dc
->header
.type
== OBJ_MEMDC
)
108 GetObjectW( dc
->hBitmap
, sizeof(bitmap
), &bitmap
);
109 width
= bitmap
.bmWidth
;
110 height
= bitmap
.bmHeight
;
114 width
= GetDeviceCaps( dc
->hSelf
, DESKTOPHORZRES
);
115 height
= GetDeviceCaps( dc
->hSelf
, DESKTOPVERTRES
);
117 dc
->hClipRgn
= CreateRectRgn( 0, 0, width
, height
);
121 /***********************************************************************
122 * SelectClipRgn (GDI32.@)
124 INT WINAPI
SelectClipRgn( HDC hdc
, HRGN hrgn
)
126 return ExtSelectClipRgn( hdc
, hrgn
, RGN_COPY
);
130 /******************************************************************************
131 * ExtSelectClipRgn [GDI32.@]
133 INT WINAPI
ExtSelectClipRgn( HDC hdc
, HRGN hrgn
, INT fnMode
)
137 DC
* dc
= get_dc_ptr( hdc
);
138 if (!dc
) return ERROR
;
140 TRACE("%p %p %d\n", hdc
, hrgn
, fnMode
);
143 if (dc
->funcs
->pExtSelectClipRgn
)
145 retval
= dc
->funcs
->pExtSelectClipRgn( dc
->physDev
, hrgn
, fnMode
);
146 release_dc_ptr( dc
);
152 if (fnMode
== RGN_COPY
)
154 if (dc
->hClipRgn
) DeleteObject( dc
->hClipRgn
);
159 FIXME("Unimplemented: hrgn NULL in mode: %d\n", fnMode
);
160 release_dc_ptr( dc
);
168 if (dc
->layout
& LAYOUT_RTL
)
170 if (!(mirrored
= CreateRectRgn( 0, 0, 0, 0 )))
172 release_dc_ptr( dc
);
175 mirror_region( mirrored
, hrgn
, dc
->vis_rect
.right
- dc
->vis_rect
.left
);
180 create_default_clip_region( dc
);
182 if(fnMode
== RGN_COPY
)
183 CombineRgn( dc
->hClipRgn
, hrgn
, 0, fnMode
);
185 CombineRgn( dc
->hClipRgn
, dc
->hClipRgn
, hrgn
, fnMode
);
187 if (mirrored
) DeleteObject( mirrored
);
190 CLIPPING_UpdateGCRegion( dc
);
191 release_dc_ptr( dc
);
193 return GetClipBox(hdc
, &rect
);
196 /***********************************************************************
197 * __wine_set_visible_region (GDI32.@)
199 void CDECL
__wine_set_visible_region( HDC hdc
, HRGN hrgn
, const RECT
*vis_rect
)
203 if (!(dc
= get_dc_ptr( hdc
))) return;
205 TRACE( "%p %p %s\n", hdc
, hrgn
, wine_dbgstr_rect(vis_rect
) );
207 /* map region to DC coordinates */
208 OffsetRgn( hrgn
, -vis_rect
->left
, -vis_rect
->top
);
210 DeleteObject( dc
->hVisRgn
);
212 dc
->vis_rect
= *vis_rect
;
214 CLIPPING_UpdateGCRegion( dc
);
215 release_dc_ptr( dc
);
219 /***********************************************************************
220 * OffsetClipRgn (GDI32.@)
222 INT WINAPI
OffsetClipRgn( HDC hdc
, INT x
, INT y
)
224 INT ret
= SIMPLEREGION
;
225 DC
*dc
= get_dc_ptr( hdc
);
226 if (!dc
) return ERROR
;
228 TRACE("%p %d,%d\n", hdc
, x
, y
);
231 if(dc
->funcs
->pOffsetClipRgn
)
233 ret
= dc
->funcs
->pOffsetClipRgn( dc
->physDev
, x
, y
);
234 /* FIXME: ret is just a success flag, we should return a proper value */
236 else if (dc
->hClipRgn
) {
237 x
= MulDiv( x
, dc
->vportExtX
, dc
->wndExtX
);
238 y
= MulDiv( y
, dc
->vportExtY
, dc
->wndExtY
);
239 if (dc
->layout
& LAYOUT_RTL
) x
= -x
;
240 ret
= OffsetRgn( dc
->hClipRgn
, x
, y
);
241 CLIPPING_UpdateGCRegion( dc
);
243 release_dc_ptr( dc
);
248 /***********************************************************************
249 * ExcludeClipRect (GDI32.@)
251 INT WINAPI
ExcludeClipRect( HDC hdc
, INT left
, INT top
,
252 INT right
, INT bottom
)
256 DC
*dc
= get_dc_ptr( hdc
);
257 if (!dc
) return ERROR
;
259 TRACE("%p %dx%d,%dx%d\n", hdc
, left
, top
, right
, bottom
);
262 if(dc
->funcs
->pExcludeClipRect
)
264 ret
= dc
->funcs
->pExcludeClipRect( dc
->physDev
, left
, top
, right
, bottom
);
265 /* FIXME: ret is just a success flag, we should return a proper value */
269 RECT rect
= get_clip_rect( dc
, left
, top
, right
, bottom
);
271 if (!(newRgn
= CreateRectRgnIndirect( &rect
))) ret
= ERROR
;
275 create_default_clip_region( dc
);
276 ret
= CombineRgn( dc
->hClipRgn
, dc
->hClipRgn
, newRgn
, RGN_DIFF
);
277 DeleteObject( newRgn
);
279 if (ret
!= ERROR
) CLIPPING_UpdateGCRegion( dc
);
281 release_dc_ptr( dc
);
286 /***********************************************************************
287 * IntersectClipRect (GDI32.@)
289 INT WINAPI
IntersectClipRect( HDC hdc
, INT left
, INT top
, INT right
, INT bottom
)
292 DC
*dc
= get_dc_ptr( hdc
);
293 if (!dc
) return ERROR
;
295 TRACE("%p %d,%d - %d,%d\n", hdc
, left
, top
, right
, bottom
);
298 if(dc
->funcs
->pIntersectClipRect
)
300 ret
= dc
->funcs
->pIntersectClipRect( dc
->physDev
, left
, top
, right
, bottom
);
301 /* FIXME: ret is just a success flag, we should return a proper value */
305 RECT rect
= get_clip_rect( dc
, left
, top
, right
, bottom
);
309 dc
->hClipRgn
= CreateRectRgnIndirect( &rect
);
316 if (!(newRgn
= CreateRectRgnIndirect( &rect
))) ret
= ERROR
;
319 ret
= CombineRgn( dc
->hClipRgn
, dc
->hClipRgn
, newRgn
, RGN_AND
);
320 DeleteObject( newRgn
);
323 if (ret
!= ERROR
) CLIPPING_UpdateGCRegion( dc
);
325 release_dc_ptr( dc
);
330 /***********************************************************************
331 * PtVisible (GDI32.@)
333 BOOL WINAPI
PtVisible( HDC hdc
, INT x
, INT y
)
338 DC
*dc
= get_dc_ptr( hdc
);
340 TRACE("%p %d,%d\n", hdc
, x
, y
);
341 if (!dc
) return FALSE
;
345 LPtoDP( hdc
, &pt
, 1 );
347 ret
= PtInRegion( dc
->hVisRgn
, pt
.x
, pt
.y
);
348 if (ret
&& (clip
= get_clip_region(dc
))) ret
= PtInRegion( clip
, pt
.x
, pt
.y
);
349 release_dc_ptr( dc
);
354 /***********************************************************************
355 * RectVisible (GDI32.@)
357 BOOL WINAPI
RectVisible( HDC hdc
, const RECT
* rect
)
362 DC
*dc
= get_dc_ptr( hdc
);
363 if (!dc
) return FALSE
;
364 TRACE("%p %d,%dx%d,%d\n", hdc
, rect
->left
, rect
->top
, rect
->right
, rect
->bottom
);
367 LPtoDP( hdc
, (POINT
*)&tmpRect
, 2 );
370 if ((clip
= get_clip_region(dc
)))
372 HRGN hrgn
= CreateRectRgn( 0, 0, 0, 0 );
373 CombineRgn( hrgn
, dc
->hVisRgn
, clip
, RGN_AND
);
374 ret
= RectInRegion( hrgn
, &tmpRect
);
375 DeleteObject( hrgn
);
377 else ret
= RectInRegion( dc
->hVisRgn
, &tmpRect
);
378 release_dc_ptr( dc
);
383 /***********************************************************************
384 * GetClipBox (GDI32.@)
386 INT WINAPI
GetClipBox( HDC hdc
, LPRECT rect
)
390 DC
*dc
= get_dc_ptr( hdc
);
391 if (!dc
) return ERROR
;
394 if ((clip
= get_clip_region(dc
)))
396 HRGN hrgn
= CreateRectRgn( 0, 0, 0, 0 );
397 CombineRgn( hrgn
, dc
->hVisRgn
, clip
, RGN_AND
);
398 ret
= GetRgnBox( hrgn
, rect
);
399 DeleteObject( hrgn
);
401 else ret
= GetRgnBox( dc
->hVisRgn
, rect
);
402 DPtoLP( hdc
, (LPPOINT
)rect
, 2 );
403 release_dc_ptr( dc
);
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
);