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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "wine/winuser16.h"
29 #include "gdi_private.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(clipping
);
35 /***********************************************************************
36 * CLIPPING_UpdateGCRegion
38 * Update the GC clip region when the ClipRgn or VisRgn have changed.
40 void CLIPPING_UpdateGCRegion( DC
* dc
)
42 if (!dc
->hGCClipRgn
) dc
->hGCClipRgn
= CreateRectRgn( 0, 0, 0, 0 );
46 ERR("hVisRgn is zero. Please report this.\n" );
50 if (dc
->flags
& DC_DIRTY
) ERR( "DC is dirty. Please report this.\n" );
53 CombineRgn( dc
->hGCClipRgn
, dc
->hVisRgn
, 0, RGN_COPY
);
55 CombineRgn(dc
->hGCClipRgn
, dc
->hClipRgn
, dc
->hVisRgn
, RGN_AND
);
56 if (dc
->funcs
->pSetDeviceClipping
)
57 dc
->funcs
->pSetDeviceClipping( dc
->physDev
, dc
->hGCClipRgn
);
61 /***********************************************************************
62 * SelectClipRgn (GDI32.@)
64 INT WINAPI
SelectClipRgn( HDC hdc
, HRGN hrgn
)
66 return ExtSelectClipRgn( hdc
, hrgn
, RGN_COPY
);
70 /******************************************************************************
71 * ExtSelectClipRgn [GDI32.@]
73 INT WINAPI
ExtSelectClipRgn( HDC hdc
, HRGN hrgn
, INT fnMode
)
77 DC
* dc
= DC_GetDCUpdate( hdc
);
78 if (!dc
) return ERROR
;
80 TRACE("%p %p %d\n", hdc
, hrgn
, fnMode
);
82 if (dc
->funcs
->pExtSelectClipRgn
)
84 retval
= dc
->funcs
->pExtSelectClipRgn( dc
->physDev
, hrgn
, fnMode
);
85 GDI_ReleaseObj( hdc
);
91 if (fnMode
== RGN_COPY
)
93 if (dc
->hClipRgn
) DeleteObject( dc
->hClipRgn
);
98 FIXME("Unimplemented: hrgn NULL in mode: %d\n", fnMode
);
99 GDI_ReleaseObj( hdc
);
108 GetRgnBox( dc
->hVisRgn
, &rect
);
109 dc
->hClipRgn
= CreateRectRgnIndirect( &rect
);
112 if(fnMode
== RGN_COPY
)
113 CombineRgn( dc
->hClipRgn
, hrgn
, 0, fnMode
);
115 CombineRgn( dc
->hClipRgn
, dc
->hClipRgn
, hrgn
, fnMode
);
118 CLIPPING_UpdateGCRegion( dc
);
119 GDI_ReleaseObj( hdc
);
121 return GetClipBox(hdc
, &rect
);
124 /***********************************************************************
125 * SelectVisRgn (GDI.105)
127 INT16 WINAPI
SelectVisRgn16( HDC16 hdc16
, HRGN16 hrgn
)
130 HDC hdc
= HDC_32( hdc16
);
133 if (!hrgn
) return ERROR
;
134 if (!(dc
= DC_GetDCPtr( hdc
))) return ERROR
;
136 TRACE("%p %04x\n", hdc
, hrgn
);
138 dc
->flags
&= ~DC_DIRTY
;
140 retval
= CombineRgn( dc
->hVisRgn
, HRGN_32(hrgn
), 0, RGN_COPY
);
141 CLIPPING_UpdateGCRegion( dc
);
142 GDI_ReleaseObj( hdc
);
147 /***********************************************************************
148 * OffsetClipRgn (GDI32.@)
150 INT WINAPI
OffsetClipRgn( HDC hdc
, INT x
, INT y
)
152 INT ret
= SIMPLEREGION
;
153 DC
*dc
= DC_GetDCUpdate( hdc
);
154 if (!dc
) return ERROR
;
156 TRACE("%p %d,%d\n", hdc
, x
, y
);
158 if(dc
->funcs
->pOffsetClipRgn
)
159 ret
= dc
->funcs
->pOffsetClipRgn( dc
->physDev
, x
, y
);
160 else if (dc
->hClipRgn
) {
161 ret
= OffsetRgn( dc
->hClipRgn
, MulDiv( x
, dc
->vportExtX
, dc
->wndExtX
),
162 MulDiv( y
, dc
->vportExtY
, dc
->wndExtY
) );
163 CLIPPING_UpdateGCRegion( dc
);
165 GDI_ReleaseObj( hdc
);
170 /***********************************************************************
171 * OffsetVisRgn (GDI.102)
173 INT16 WINAPI
OffsetVisRgn16( HDC16 hdc16
, INT16 x
, INT16 y
)
176 HDC hdc
= HDC_32( hdc16
);
177 DC
* dc
= DC_GetDCUpdate( hdc
);
178 if (!dc
) return ERROR
;
179 TRACE("%p %d,%d\n", hdc
, x
, y
);
180 retval
= OffsetRgn( dc
->hVisRgn
, x
, y
);
181 CLIPPING_UpdateGCRegion( dc
);
182 GDI_ReleaseObj( hdc
);
187 /***********************************************************************
188 * ExcludeClipRect (GDI32.@)
190 INT WINAPI
ExcludeClipRect( HDC hdc
, INT left
, INT top
,
191 INT right
, INT bottom
)
195 DC
*dc
= DC_GetDCUpdate( hdc
);
196 if (!dc
) return ERROR
;
198 TRACE("%p %dx%d,%dx%d\n", hdc
, left
, top
, right
, bottom
);
200 if(dc
->funcs
->pExcludeClipRect
)
201 ret
= dc
->funcs
->pExcludeClipRect( dc
->physDev
, left
, top
, right
, bottom
);
210 LPtoDP( hdc
, pt
, 2 );
211 if (!(newRgn
= CreateRectRgn( pt
[0].x
, pt
[0].y
, pt
[1].x
, pt
[1].y
))) ret
= ERROR
;
216 dc
->hClipRgn
= CreateRectRgn( 0, 0, 0, 0 );
217 CombineRgn( dc
->hClipRgn
, dc
->hVisRgn
, 0, RGN_COPY
);
219 ret
= CombineRgn( dc
->hClipRgn
, dc
->hClipRgn
, newRgn
, RGN_DIFF
);
220 DeleteObject( newRgn
);
222 if (ret
!= ERROR
) CLIPPING_UpdateGCRegion( dc
);
224 GDI_ReleaseObj( hdc
);
229 /***********************************************************************
230 * IntersectClipRect (GDI32.@)
232 INT WINAPI
IntersectClipRect( HDC hdc
, INT left
, INT top
, INT right
, INT bottom
)
235 DC
*dc
= DC_GetDCUpdate( hdc
);
236 if (!dc
) return ERROR
;
238 TRACE("%p %d,%d - %d,%d\n", hdc
, left
, top
, right
, bottom
);
240 if(dc
->funcs
->pIntersectClipRect
)
241 ret
= dc
->funcs
->pIntersectClipRect( dc
->physDev
, left
, top
, right
, bottom
);
251 LPtoDP( hdc
, pt
, 2 );
255 dc
->hClipRgn
= CreateRectRgn( pt
[0].x
, pt
[0].y
, pt
[1].x
, pt
[1].y
);
262 if (!(newRgn
= CreateRectRgn( pt
[0].x
, pt
[0].y
, pt
[1].x
, pt
[1].y
))) ret
= ERROR
;
265 ret
= CombineRgn( dc
->hClipRgn
, dc
->hClipRgn
, newRgn
, RGN_AND
);
266 DeleteObject( newRgn
);
269 if (ret
!= ERROR
) CLIPPING_UpdateGCRegion( dc
);
271 GDI_ReleaseObj( hdc
);
276 /***********************************************************************
277 * ExcludeVisRect (GDI.73)
279 INT16 WINAPI
ExcludeVisRect16( HDC16 hdc16
, INT16 left
, INT16 top
, INT16 right
, INT16 bottom
)
284 HDC hdc
= HDC_32( hdc16
);
285 DC
* dc
= DC_GetDCUpdate( hdc
);
286 if (!dc
) return ERROR
;
293 LPtoDP( hdc
, pt
, 2 );
295 TRACE("%p %ld,%ld - %ld,%ld\n", hdc
, pt
[0].x
, pt
[0].y
, pt
[1].x
, pt
[1].y
);
297 if (!(tempRgn
= CreateRectRgn( pt
[0].x
, pt
[0].y
, pt
[1].x
, pt
[1].y
))) ret
= ERROR
;
300 ret
= CombineRgn( dc
->hVisRgn
, dc
->hVisRgn
, tempRgn
, RGN_DIFF
);
301 DeleteObject( tempRgn
);
303 if (ret
!= ERROR
) CLIPPING_UpdateGCRegion( dc
);
304 GDI_ReleaseObj( hdc
);
309 /***********************************************************************
310 * IntersectVisRect (GDI.98)
312 INT16 WINAPI
IntersectVisRect16( HDC16 hdc16
, INT16 left
, INT16 top
, INT16 right
, INT16 bottom
)
317 HDC hdc
= HDC_32( hdc16
);
318 DC
* dc
= DC_GetDCUpdate( hdc
);
319 if (!dc
) return ERROR
;
326 LPtoDP( hdc
, pt
, 2 );
328 TRACE("%p %ld,%ld - %ld,%ld\n", hdc
, pt
[0].x
, pt
[0].y
, pt
[1].x
, pt
[1].y
);
331 if (!(tempRgn
= CreateRectRgn( pt
[0].x
, pt
[0].y
, pt
[1].x
, pt
[1].y
))) ret
= ERROR
;
334 ret
= CombineRgn( dc
->hVisRgn
, dc
->hVisRgn
, tempRgn
, RGN_AND
);
335 DeleteObject( tempRgn
);
337 if (ret
!= ERROR
) CLIPPING_UpdateGCRegion( dc
);
338 GDI_ReleaseObj( hdc
);
343 /***********************************************************************
344 * PtVisible (GDI32.@)
346 BOOL WINAPI
PtVisible( HDC hdc
, INT x
, INT y
)
349 DC
*dc
= DC_GetDCUpdate( hdc
);
351 TRACE("%p %d,%d\n", hdc
, x
, y
);
352 if (!dc
) return FALSE
;
359 LPtoDP( hdc
, &pt
, 1 );
360 ret
= PtInRegion( dc
->hGCClipRgn
, pt
.x
, pt
.y
);
362 GDI_ReleaseObj( hdc
);
367 /***********************************************************************
368 * RectVisible (GDI32.@)
370 BOOL WINAPI
RectVisible( HDC hdc
, const RECT
* rect
)
373 DC
*dc
= DC_GetDCUpdate( hdc
);
374 if (!dc
) return FALSE
;
375 TRACE("%p %ld,%ldx%ld,%ld\n", hdc
, rect
->left
, rect
->top
, rect
->right
, rect
->bottom
);
381 pt
[0].x
= rect
->left
;
383 pt
[1].x
= rect
->right
;
384 pt
[1].y
= rect
->bottom
;
385 LPtoDP( hdc
, pt
, 2 );
386 tmpRect
.left
= pt
[0].x
;
387 tmpRect
.top
= pt
[0].y
;
388 tmpRect
.right
= pt
[1].x
;
389 tmpRect
.bottom
= pt
[1].y
;
390 ret
= RectInRegion( dc
->hGCClipRgn
, &tmpRect
);
392 GDI_ReleaseObj( hdc
);
397 /***********************************************************************
398 * GetClipBox (GDI32.@)
400 INT WINAPI
GetClipBox( HDC hdc
, LPRECT rect
)
403 DC
*dc
= DC_GetDCUpdate( hdc
);
404 if (!dc
) return ERROR
;
405 ret
= GetRgnBox( dc
->hGCClipRgn
, rect
);
406 DPtoLP( hdc
, (LPPOINT
)rect
, 2 );
407 GDI_ReleaseObj( hdc
);
412 /***********************************************************************
413 * GetClipRgn (GDI32.@)
415 INT WINAPI
GetClipRgn( HDC hdc
, HRGN hRgn
)
419 if (hRgn
&& (dc
= DC_GetDCPtr( hdc
)))
423 if( CombineRgn(hRgn
, dc
->hClipRgn
, 0, RGN_COPY
) != ERROR
) ret
= 1;
426 GDI_ReleaseObj( hdc
);
431 /***********************************************************************
432 * SaveVisRgn (GDI.129)
434 HRGN16 WINAPI
SaveVisRgn16( HDC16 hdc16
)
437 GDIOBJHDR
*obj
, *copyObj
;
438 HDC hdc
= HDC_32( hdc16
);
439 DC
*dc
= DC_GetDCUpdate( hdc
);
444 if (!(obj
= GDI_GetObjPtr( dc
->hVisRgn
, REGION_MAGIC
)))
446 GDI_ReleaseObj( hdc
);
449 if (!(copy
= CreateRectRgn( 0, 0, 0, 0 )))
451 GDI_ReleaseObj( dc
->hVisRgn
);
452 GDI_ReleaseObj( hdc
);
455 CombineRgn( copy
, dc
->hVisRgn
, 0, RGN_COPY
);
456 if (!(copyObj
= GDI_GetObjPtr( copy
, REGION_MAGIC
)))
458 DeleteObject( copy
);
459 GDI_ReleaseObj( dc
->hVisRgn
);
460 GDI_ReleaseObj( hdc
);
463 copyObj
->hNext
= obj
->hNext
;
464 obj
->hNext
= HRGN_16(copy
);
465 GDI_ReleaseObj( copy
);
466 GDI_ReleaseObj( dc
->hVisRgn
);
467 GDI_ReleaseObj( hdc
);
468 return HRGN_16(copy
);
472 /***********************************************************************
473 * RestoreVisRgn (GDI.130)
475 INT16 WINAPI
RestoreVisRgn16( HDC16 hdc16
)
478 GDIOBJHDR
*obj
, *savedObj
;
479 HDC hdc
= HDC_32( hdc16
);
480 DC
*dc
= DC_GetDCPtr( hdc
);
483 if (!dc
) return ERROR
;
487 if (!(obj
= GDI_GetObjPtr( dc
->hVisRgn
, REGION_MAGIC
))) goto done
;
488 saved
= HRGN_32(obj
->hNext
);
490 if ((savedObj
= GDI_GetObjPtr( saved
, REGION_MAGIC
)))
492 ret
= CombineRgn( dc
->hVisRgn
, saved
, 0, RGN_COPY
);
493 obj
->hNext
= savedObj
->hNext
;
494 GDI_ReleaseObj( saved
);
495 DeleteObject( saved
);
496 dc
->flags
&= ~DC_DIRTY
;
497 CLIPPING_UpdateGCRegion( dc
);
499 GDI_ReleaseObj( dc
->hVisRgn
);
501 GDI_ReleaseObj( hdc
);
506 /***********************************************************************
507 * GetRandomRgn [GDI32.@]
510 * This function is documented in MSDN online for the case of
511 * dwCode == SYSRGN (4).
513 * For dwCode == 1 it should return the clip region
514 * 2 " " " the meta region
515 * 3 " " " the intersection of the clip with
516 * the meta region (== 'Rao' region).
518 * See http://www.codeproject.com/gdi/cliprgnguide.asp
520 INT WINAPI
GetRandomRgn(HDC hDC
, HRGN hRgn
, DWORD dwCode
)
524 case SYSRGN
: /* == 4 */
526 DC
*dc
= DC_GetDCPtr (hDC
);
529 CombineRgn (hRgn
, dc
->hVisRgn
, 0, RGN_COPY
);
530 GDI_ReleaseObj( hDC
);
532 * On Windows NT/2000,
533 * the region returned is in screen coordinates.
535 * the region returned is in window coordinates
537 if (!(GetVersion() & 0x80000000))
540 GetDCOrgEx(hDC
, &org
);
541 OffsetRgn(hRgn
, org
.x
, org
.y
);
546 case 1: /* clip region */
547 return GetClipRgn (hDC
, hRgn
);
550 WARN("Unknown dwCode %ld\n", dwCode
);
558 /***********************************************************************
559 * GetMetaRgn (GDI32.@)
561 INT WINAPI
GetMetaRgn( HDC hdc
, HRGN hRgn
)
569 /***********************************************************************
570 * SetMetaRgn (GDI32.@)
572 INT WINAPI
SetMetaRgn( HDC hdc
)