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 /***********************************************************************
46 * CLIPPING_UpdateGCRegion
48 * Update the GC clip region when the ClipRgn or VisRgn have changed.
50 void CLIPPING_UpdateGCRegion( DC
* dc
)
54 /* update the intersection of meta and clip regions */
55 if (dc
->hMetaRgn
&& dc
->hClipRgn
)
57 if (!dc
->hMetaClipRgn
) dc
->hMetaClipRgn
= CreateRectRgn( 0, 0, 0, 0 );
58 CombineRgn( dc
->hMetaClipRgn
, dc
->hClipRgn
, dc
->hMetaRgn
, RGN_AND
);
59 clip_rgn
= dc
->hMetaClipRgn
;
61 else /* only one is set, no need for an intersection */
63 if (dc
->hMetaClipRgn
) DeleteObject( dc
->hMetaClipRgn
);
65 clip_rgn
= dc
->hMetaRgn
? dc
->hMetaRgn
: dc
->hClipRgn
;
68 if (dc
->funcs
->pSetDeviceClipping
)
69 dc
->funcs
->pSetDeviceClipping( dc
->physDev
, dc
->hVisRgn
, clip_rgn
);
72 /***********************************************************************
73 * create_default_clip_region
75 * Create a default clipping region when none already exists.
77 static inline void create_default_clip_region( DC
* dc
)
81 if (dc
->header
.type
== OBJ_MEMDC
)
85 GetObjectW( dc
->hBitmap
, sizeof(bitmap
), &bitmap
);
86 width
= bitmap
.bmWidth
;
87 height
= bitmap
.bmHeight
;
91 width
= GetDeviceCaps( dc
->hSelf
, DESKTOPHORZRES
);
92 height
= GetDeviceCaps( dc
->hSelf
, DESKTOPVERTRES
);
94 dc
->hClipRgn
= CreateRectRgn( 0, 0, width
, height
);
98 /***********************************************************************
99 * SelectClipRgn (GDI32.@)
101 INT WINAPI
SelectClipRgn( HDC hdc
, HRGN hrgn
)
103 return ExtSelectClipRgn( hdc
, hrgn
, RGN_COPY
);
107 /******************************************************************************
108 * ExtSelectClipRgn [GDI32.@]
110 INT WINAPI
ExtSelectClipRgn( HDC hdc
, HRGN hrgn
, INT fnMode
)
114 DC
* dc
= get_dc_ptr( hdc
);
115 if (!dc
) return ERROR
;
117 TRACE("%p %p %d\n", hdc
, hrgn
, fnMode
);
120 if (dc
->funcs
->pExtSelectClipRgn
)
122 retval
= dc
->funcs
->pExtSelectClipRgn( dc
->physDev
, hrgn
, fnMode
);
123 release_dc_ptr( dc
);
129 if (fnMode
== RGN_COPY
)
131 if (dc
->hClipRgn
) DeleteObject( dc
->hClipRgn
);
136 FIXME("Unimplemented: hrgn NULL in mode: %d\n", fnMode
);
137 release_dc_ptr( dc
);
144 create_default_clip_region( dc
);
146 if(fnMode
== RGN_COPY
)
147 CombineRgn( dc
->hClipRgn
, hrgn
, 0, fnMode
);
149 CombineRgn( dc
->hClipRgn
, dc
->hClipRgn
, hrgn
, fnMode
);
152 CLIPPING_UpdateGCRegion( dc
);
153 release_dc_ptr( dc
);
155 return GetClipBox(hdc
, &rect
);
158 /***********************************************************************
159 * __wine_set_visible_region (GDI32.@)
161 void CDECL
__wine_set_visible_region( HDC hdc
, HRGN hrgn
, const RECT
*vis_rect
)
165 if (!(dc
= get_dc_ptr( hdc
))) return;
167 TRACE( "%p %p %s\n", hdc
, hrgn
, wine_dbgstr_rect(vis_rect
) );
169 /* map region to DC coordinates */
170 OffsetRgn( hrgn
, -vis_rect
->left
, -vis_rect
->top
);
172 DeleteObject( dc
->hVisRgn
);
174 dc
->vis_rect
= *vis_rect
;
176 CLIPPING_UpdateGCRegion( dc
);
177 release_dc_ptr( dc
);
181 /***********************************************************************
182 * OffsetClipRgn (GDI32.@)
184 INT WINAPI
OffsetClipRgn( HDC hdc
, INT x
, INT y
)
186 INT ret
= SIMPLEREGION
;
187 DC
*dc
= get_dc_ptr( hdc
);
188 if (!dc
) return ERROR
;
190 TRACE("%p %d,%d\n", hdc
, x
, y
);
193 if(dc
->funcs
->pOffsetClipRgn
)
195 ret
= dc
->funcs
->pOffsetClipRgn( dc
->physDev
, x
, y
);
196 /* FIXME: ret is just a success flag, we should return a proper value */
198 else if (dc
->hClipRgn
) {
199 ret
= OffsetRgn( dc
->hClipRgn
, MulDiv( x
, dc
->vportExtX
, dc
->wndExtX
),
200 MulDiv( y
, dc
->vportExtY
, dc
->wndExtY
) );
201 CLIPPING_UpdateGCRegion( dc
);
203 release_dc_ptr( dc
);
208 /***********************************************************************
209 * ExcludeClipRect (GDI32.@)
211 INT WINAPI
ExcludeClipRect( HDC hdc
, INT left
, INT top
,
212 INT right
, INT bottom
)
216 DC
*dc
= get_dc_ptr( hdc
);
217 if (!dc
) return ERROR
;
219 TRACE("%p %dx%d,%dx%d\n", hdc
, left
, top
, right
, bottom
);
222 if(dc
->funcs
->pExcludeClipRect
)
224 ret
= dc
->funcs
->pExcludeClipRect( dc
->physDev
, left
, top
, right
, bottom
);
225 /* FIXME: ret is just a success flag, we should return a proper value */
235 LPtoDP( hdc
, pt
, 2 );
236 if (!(newRgn
= CreateRectRgn( pt
[0].x
, pt
[0].y
, pt
[1].x
, pt
[1].y
))) ret
= ERROR
;
240 create_default_clip_region( dc
);
241 ret
= CombineRgn( dc
->hClipRgn
, dc
->hClipRgn
, newRgn
, RGN_DIFF
);
242 DeleteObject( newRgn
);
244 if (ret
!= ERROR
) CLIPPING_UpdateGCRegion( dc
);
246 release_dc_ptr( dc
);
251 /***********************************************************************
252 * IntersectClipRect (GDI32.@)
254 INT WINAPI
IntersectClipRect( HDC hdc
, INT left
, INT top
, INT right
, INT bottom
)
257 DC
*dc
= get_dc_ptr( hdc
);
258 if (!dc
) return ERROR
;
260 TRACE("%p %d,%d - %d,%d\n", hdc
, left
, top
, right
, bottom
);
263 if(dc
->funcs
->pIntersectClipRect
)
265 ret
= dc
->funcs
->pIntersectClipRect( dc
->physDev
, left
, top
, right
, bottom
);
266 /* FIXME: ret is just a success flag, we should return a proper value */
277 LPtoDP( hdc
, pt
, 2 );
281 dc
->hClipRgn
= CreateRectRgn( pt
[0].x
, pt
[0].y
, pt
[1].x
, pt
[1].y
);
288 if (!(newRgn
= CreateRectRgn( pt
[0].x
, pt
[0].y
, pt
[1].x
, pt
[1].y
))) ret
= ERROR
;
291 ret
= CombineRgn( dc
->hClipRgn
, dc
->hClipRgn
, newRgn
, RGN_AND
);
292 DeleteObject( newRgn
);
295 if (ret
!= ERROR
) CLIPPING_UpdateGCRegion( dc
);
297 release_dc_ptr( dc
);
302 /***********************************************************************
303 * PtVisible (GDI32.@)
305 BOOL WINAPI
PtVisible( HDC hdc
, INT x
, INT y
)
310 DC
*dc
= get_dc_ptr( hdc
);
312 TRACE("%p %d,%d\n", hdc
, x
, y
);
313 if (!dc
) return FALSE
;
317 LPtoDP( hdc
, &pt
, 1 );
319 ret
= PtInRegion( dc
->hVisRgn
, pt
.x
, pt
.y
);
320 if (ret
&& (clip
= get_clip_region(dc
))) ret
= PtInRegion( clip
, pt
.x
, pt
.y
);
321 release_dc_ptr( dc
);
326 /***********************************************************************
327 * RectVisible (GDI32.@)
329 BOOL WINAPI
RectVisible( HDC hdc
, const RECT
* rect
)
334 DC
*dc
= get_dc_ptr( hdc
);
335 if (!dc
) return FALSE
;
336 TRACE("%p %d,%dx%d,%d\n", hdc
, rect
->left
, rect
->top
, rect
->right
, rect
->bottom
);
339 LPtoDP( hdc
, (POINT
*)&tmpRect
, 2 );
342 if ((clip
= get_clip_region(dc
)))
344 HRGN hrgn
= CreateRectRgn( 0, 0, 0, 0 );
345 CombineRgn( hrgn
, dc
->hVisRgn
, clip
, RGN_AND
);
346 ret
= RectInRegion( hrgn
, &tmpRect
);
347 DeleteObject( hrgn
);
349 else ret
= RectInRegion( dc
->hVisRgn
, &tmpRect
);
350 release_dc_ptr( dc
);
355 /***********************************************************************
356 * GetClipBox (GDI32.@)
358 INT WINAPI
GetClipBox( HDC hdc
, LPRECT rect
)
362 DC
*dc
= get_dc_ptr( hdc
);
363 if (!dc
) return ERROR
;
366 if ((clip
= get_clip_region(dc
)))
368 HRGN hrgn
= CreateRectRgn( 0, 0, 0, 0 );
369 CombineRgn( hrgn
, dc
->hVisRgn
, clip
, RGN_AND
);
370 ret
= GetRgnBox( hrgn
, rect
);
371 DeleteObject( hrgn
);
373 else ret
= GetRgnBox( dc
->hVisRgn
, rect
);
374 DPtoLP( hdc
, (LPPOINT
)rect
, 2 );
375 release_dc_ptr( dc
);
380 /***********************************************************************
381 * GetClipRgn (GDI32.@)
383 INT WINAPI
GetClipRgn( HDC hdc
, HRGN hRgn
)
387 if ((dc
= get_dc_ptr( hdc
)))
391 if( CombineRgn(hRgn
, dc
->hClipRgn
, 0, RGN_COPY
) != ERROR
) ret
= 1;
394 release_dc_ptr( dc
);
400 /***********************************************************************
401 * GetMetaRgn (GDI32.@)
403 INT WINAPI
GetMetaRgn( HDC hdc
, HRGN hRgn
)
406 DC
* dc
= get_dc_ptr( hdc
);
410 if (dc
->hMetaRgn
&& CombineRgn( hRgn
, dc
->hMetaRgn
, 0, RGN_COPY
) != ERROR
)
412 release_dc_ptr( dc
);
418 /***********************************************************************
419 * GetRandomRgn [GDI32.@]
422 * This function is documented in MSDN online for the case of
423 * iCode == SYSRGN (4).
425 * For iCode == 1 it should return the clip region
426 * 2 " " " the meta region
427 * 3 " " " the intersection of the clip with
428 * the meta region (== 'Rao' region).
430 * See http://www.codeproject.com/gdi/cliprgnguide.asp
432 INT WINAPI
GetRandomRgn(HDC hDC
, HRGN hRgn
, INT iCode
)
435 DC
*dc
= get_dc_ptr( hDC
);
448 rgn
= dc
->hMetaClipRgn
;
449 if(!rgn
) rgn
= dc
->hClipRgn
;
450 if(!rgn
) rgn
= dc
->hMetaRgn
;
452 case SYSRGN
: /* == 4 */
457 WARN("Unknown code %d\n", iCode
);
458 release_dc_ptr( dc
);
461 if (rgn
) CombineRgn( hRgn
, rgn
, 0, RGN_COPY
);
462 release_dc_ptr( dc
);
464 /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
465 if (iCode
== SYSRGN
&& !(GetVersion() & 0x80000000))
466 OffsetRgn( hRgn
, dc
->vis_rect
.left
, dc
->vis_rect
.top
);
472 /***********************************************************************
473 * SetMetaRgn (GDI32.@)
475 INT WINAPI
SetMetaRgn( HDC hdc
)
479 DC
*dc
= get_dc_ptr( hdc
);
481 if (!dc
) return ERROR
;
483 if (dc
->hMetaClipRgn
)
485 /* the intersection becomes the new meta region */
486 DeleteObject( dc
->hMetaRgn
);
487 DeleteObject( dc
->hClipRgn
);
488 dc
->hMetaRgn
= dc
->hMetaClipRgn
;
490 dc
->hMetaClipRgn
= 0;
492 else if (dc
->hClipRgn
)
494 dc
->hMetaRgn
= dc
->hClipRgn
;
497 /* else nothing to do */
499 /* Note: no need to call CLIPPING_UpdateGCRegion, the overall clip region hasn't changed */
501 ret
= GetRgnBox( dc
->hMetaRgn
, &dummy
);
502 release_dc_ptr( dc
);