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
24 #include "wine/winuser16.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(clipping);
32 /***********************************************************************
33 * CLIPPING_UpdateGCRegion
35 * Update the GC clip region when the ClipRgn or VisRgn have changed.
37 void CLIPPING_UpdateGCRegion( DC * dc )
39 if (!dc->hGCClipRgn) dc->hGCClipRgn = CreateRectRgn( 0, 0, 0, 0 );
43 ERR("hVisRgn is zero. Please report this.\n" );
47 if (dc->flags & DC_DIRTY) ERR( "DC is dirty. Please report this.\n" );
50 CombineRgn( dc->hGCClipRgn, dc->hVisRgn, 0, RGN_COPY );
52 CombineRgn(dc->hGCClipRgn, dc->hClipRgn, dc->hVisRgn, RGN_AND);
53 if (dc->funcs->pSetDeviceClipping) dc->funcs->pSetDeviceClipping( dc );
57 /***********************************************************************
58 * SelectClipRgn (GDI.44)
60 INT16 WINAPI SelectClipRgn16( HDC16 hdc, HRGN16 hrgn )
62 return (INT16)SelectClipRgn( hdc, hrgn );
66 /***********************************************************************
67 * SelectClipRgn (GDI32.@)
69 INT WINAPI SelectClipRgn( HDC hdc, HRGN hrgn )
71 return ExtSelectClipRgn( hdc, hrgn, RGN_COPY );
74 /******************************************************************************
75 * ExtSelectClipRgn [GDI.508]
77 INT16 WINAPI ExtSelectClipRgn16( HDC16 hdc, HRGN16 hrgn, INT16 fnMode )
79 return (INT16) ExtSelectClipRgn((HDC) hdc, (HRGN) hrgn, fnMode);
82 /******************************************************************************
83 * ExtSelectClipRgn [GDI32.@]
85 INT WINAPI ExtSelectClipRgn( HDC hdc, HRGN hrgn, INT fnMode )
88 DC * dc = DC_GetDCUpdate( hdc );
89 if (!dc) return ERROR;
91 TRACE("%04x %04x %d\n", hdc, hrgn, fnMode );
95 if (fnMode == RGN_COPY)
97 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
99 retval = SIMPLEREGION; /* Clip region == whole DC */
103 FIXME("Unimplemented: hrgn NULL in mode: %d\n", fnMode);
104 GDI_ReleaseObj( hdc );
113 GetRgnBox( dc->hVisRgn, &rect );
114 dc->hClipRgn = CreateRectRgnIndirect( &rect );
117 OffsetRgn( dc->hClipRgn, -dc->DCOrgX, -dc->DCOrgY );
118 if(fnMode == RGN_COPY)
119 retval = CombineRgn( dc->hClipRgn, hrgn, 0, fnMode );
121 retval = CombineRgn( dc->hClipRgn, dc->hClipRgn, hrgn, fnMode);
122 OffsetRgn( dc->hClipRgn, dc->DCOrgX, dc->DCOrgY );
125 CLIPPING_UpdateGCRegion( dc );
126 GDI_ReleaseObj( hdc );
130 /***********************************************************************
131 * SelectVisRgn (GDI.105)
133 INT16 WINAPI SelectVisRgn16( HDC16 hdc, HRGN16 hrgn )
138 if (!hrgn) return ERROR;
139 if (!(dc = DC_GetDCPtr( hdc ))) return ERROR;
141 TRACE("%04x %04x\n", hdc, hrgn );
143 dc->flags &= ~DC_DIRTY;
145 retval = CombineRgn16( dc->hVisRgn, hrgn, 0, RGN_COPY );
146 CLIPPING_UpdateGCRegion( dc );
147 GDI_ReleaseObj( hdc );
152 /***********************************************************************
153 * OffsetClipRgn (GDI.32)
155 INT16 WINAPI OffsetClipRgn16( HDC16 hdc, INT16 x, INT16 y )
157 return (INT16)OffsetClipRgn( hdc, x, y );
161 /***********************************************************************
162 * OffsetClipRgn (GDI32.@)
164 INT WINAPI OffsetClipRgn( HDC hdc, INT x, INT y )
166 INT ret = SIMPLEREGION;
167 DC *dc = DC_GetDCUpdate( hdc );
168 if (!dc) return ERROR;
170 TRACE("%04x %d,%d\n", hdc, x, y );
172 if(dc->funcs->pOffsetClipRgn)
173 ret = dc->funcs->pOffsetClipRgn( dc, x, y );
174 else if (dc->hClipRgn) {
175 ret = OffsetRgn( dc->hClipRgn, XLSTODS(dc,x), YLSTODS(dc,y));
176 CLIPPING_UpdateGCRegion( dc );
178 GDI_ReleaseObj( hdc );
183 /***********************************************************************
184 * OffsetVisRgn (GDI.102)
186 INT16 WINAPI OffsetVisRgn16( HDC16 hdc, INT16 x, INT16 y )
189 DC * dc = DC_GetDCUpdate( hdc );
190 if (!dc) return ERROR;
191 TRACE("%04x %d,%d\n", hdc, x, y );
192 retval = OffsetRgn( dc->hVisRgn, x, y );
193 CLIPPING_UpdateGCRegion( dc );
194 GDI_ReleaseObj( hdc );
199 /***********************************************************************
200 * CLIPPING_IntersectClipRect
202 * Helper function for {Intersect,Exclude}ClipRect, can be called from
203 * elsewhere (like ExtTextOut()) to skip redundant metafile update and
204 * coordinate conversion.
206 INT CLIPPING_IntersectClipRect( DC * dc, INT left, INT top,
207 INT right, INT bottom, UINT flags )
215 bottom += dc->DCOrgY;
217 if (!(newRgn = CreateRectRgn( left, top, right, bottom ))) return ERROR;
220 if( flags & CLIP_INTERSECT )
222 dc->hClipRgn = newRgn;
223 CLIPPING_UpdateGCRegion( dc );
226 else if( flags & CLIP_EXCLUDE )
228 dc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
229 CombineRgn( dc->hClipRgn, dc->hVisRgn, 0, RGN_COPY );
231 else WARN("No hClipRgn and flags are %x\n",flags);
234 ret = CombineRgn( newRgn, dc->hClipRgn, newRgn,
235 (flags & CLIP_EXCLUDE) ? RGN_DIFF : RGN_AND );
238 if (!(flags & CLIP_KEEPRGN)) DeleteObject( dc->hClipRgn );
239 dc->hClipRgn = newRgn;
240 CLIPPING_UpdateGCRegion( dc );
242 else DeleteObject( newRgn );
247 /***********************************************************************
248 * ExcludeClipRect (GDI.21)
250 INT16 WINAPI ExcludeClipRect16( HDC16 hdc, INT16 left, INT16 top,
251 INT16 right, INT16 bottom )
253 return (INT16)ExcludeClipRect( hdc, left, top, right, bottom );
257 /***********************************************************************
258 * ExcludeClipRect (GDI32.@)
260 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
261 INT right, INT bottom )
264 DC *dc = DC_GetDCUpdate( hdc );
265 if (!dc) return ERROR;
267 TRACE("%04x %dx%d,%dx%d\n", hdc, left, top, right, bottom );
269 if(dc->funcs->pExcludeClipRect)
270 ret = dc->funcs->pExcludeClipRect( dc, left, top, right, bottom );
272 left = XLPTODP( dc, left );
273 right = XLPTODP( dc, right );
274 top = YLPTODP( dc, top );
275 bottom = YLPTODP( dc, bottom );
277 ret = CLIPPING_IntersectClipRect( dc, left, top, right, bottom, CLIP_EXCLUDE );
279 GDI_ReleaseObj( hdc );
284 /***********************************************************************
285 * IntersectClipRect (GDI.22)
287 INT16 WINAPI IntersectClipRect16( HDC16 hdc, INT16 left, INT16 top,
288 INT16 right, INT16 bottom )
290 return (INT16)IntersectClipRect( hdc, left, top, right, bottom );
294 /***********************************************************************
295 * IntersectClipRect (GDI32.@)
297 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top,
298 INT right, INT bottom )
301 DC *dc = DC_GetDCUpdate( hdc );
302 if (!dc) return ERROR;
304 TRACE("%04x %dx%d,%dx%d\n", hdc, left, top, right, bottom );
306 if(dc->funcs->pIntersectClipRect)
307 ret = dc->funcs->pIntersectClipRect( dc, left, top, right, bottom );
309 left = XLPTODP( dc, left );
310 right = XLPTODP( dc, right );
311 top = YLPTODP( dc, top );
312 bottom = YLPTODP( dc, bottom );
314 ret = CLIPPING_IntersectClipRect( dc, left, top, right, bottom, CLIP_INTERSECT );
316 GDI_ReleaseObj( hdc );
321 /***********************************************************************
322 * CLIPPING_IntersectVisRect
324 * Helper function for {Intersect,Exclude}VisRect, can be called from
325 * elsewhere (like ExtTextOut()) to skip redundant metafile update and
326 * coordinate conversion.
328 INT CLIPPING_IntersectVisRect( DC * dc, INT left, INT top,
329 INT right, INT bottom,
332 HRGN tempRgn, newRgn;
338 bottom += dc->DCOrgY;
340 if (!(newRgn = CreateRectRgn( 0, 0, 0, 0 ))) return ERROR;
341 if (!(tempRgn = CreateRectRgn( left, top, right, bottom )))
343 DeleteObject( newRgn );
346 ret = CombineRgn( newRgn, dc->hVisRgn, tempRgn,
347 exclude ? RGN_DIFF : RGN_AND );
348 DeleteObject( tempRgn );
352 RGNOBJ *newObj = (RGNOBJ*)GDI_GetObjPtr( newRgn, REGION_MAGIC);
355 RGNOBJ *prevObj = (RGNOBJ*)GDI_GetObjPtr( dc->hVisRgn, REGION_MAGIC);
358 newObj->header.hNext = prevObj->header.hNext;
359 GDI_ReleaseObj( dc->hVisRgn );
361 GDI_ReleaseObj( newRgn );
363 DeleteObject( dc->hVisRgn );
364 dc->hVisRgn = newRgn;
365 CLIPPING_UpdateGCRegion( dc );
367 else DeleteObject( newRgn );
372 /***********************************************************************
373 * ExcludeVisRect (GDI.73)
375 INT16 WINAPI ExcludeVisRect16( HDC16 hdc, INT16 left, INT16 top,
376 INT16 right, INT16 bottom )
379 DC * dc = DC_GetDCUpdate( hdc );
380 if (!dc) return ERROR;
382 left = XLPTODP( dc, left );
383 right = XLPTODP( dc, right );
384 top = YLPTODP( dc, top );
385 bottom = YLPTODP( dc, bottom );
387 TRACE("%04x %dx%d,%dx%d\n", hdc, left, top, right, bottom );
389 ret = CLIPPING_IntersectVisRect( dc, left, top, right, bottom, TRUE );
390 GDI_ReleaseObj( hdc );
395 /***********************************************************************
396 * IntersectVisRect (GDI.98)
398 INT16 WINAPI IntersectVisRect16( HDC16 hdc, INT16 left, INT16 top,
399 INT16 right, INT16 bottom )
402 DC * dc = DC_GetDCUpdate( hdc );
403 if (!dc) return ERROR;
405 left = XLPTODP( dc, left );
406 right = XLPTODP( dc, right );
407 top = YLPTODP( dc, top );
408 bottom = YLPTODP( dc, bottom );
410 TRACE("%04x %dx%d,%dx%d\n", hdc, left, top, right, bottom );
412 ret = CLIPPING_IntersectVisRect( dc, left, top, right, bottom, FALSE );
413 GDI_ReleaseObj( hdc );
418 /***********************************************************************
419 * PtVisible (GDI.103)
421 BOOL16 WINAPI PtVisible16( HDC16 hdc, INT16 x, INT16 y )
423 return PtVisible( hdc, x, y );
427 /***********************************************************************
428 * PtVisible (GDI32.@)
430 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
433 DC *dc = DC_GetDCUpdate( hdc );
435 TRACE("%04x %d,%d\n", hdc, x, y );
436 if (!dc) return FALSE;
439 ret = PtInRegion( dc->hGCClipRgn, XLPTODP(dc,x) + dc->DCOrgX,
440 YLPTODP(dc,y) + dc->DCOrgY );
442 GDI_ReleaseObj( hdc );
447 /***********************************************************************
448 * RectVisible (GDI.465)
449 * RectVisibleOld (GDI.104)
451 BOOL16 WINAPI RectVisible16( HDC16 hdc, const RECT16* rect16 )
454 CONV_RECT16TO32( rect16, &rect );
455 return RectVisible( hdc, &rect );
459 /***********************************************************************
460 * RectVisible (GDI32.@)
462 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
466 DC *dc = DC_GetDCUpdate( hdc );
467 if (!dc) return FALSE;
468 TRACE("%04x %d,%dx%d,%d\n",
469 hdc, rect->left, rect->top, rect->right, rect->bottom );
472 /* copy rectangle to avoid overwriting by LPtoDP */
474 LPtoDP( hdc, (LPPOINT)&tmpRect, 2 );
475 tmpRect.left += dc->DCOrgX;
476 tmpRect.right += dc->DCOrgX;
477 tmpRect.top += dc->DCOrgY;
478 tmpRect.bottom += dc->DCOrgY;
479 ret = RectInRegion( dc->hGCClipRgn, &tmpRect );
481 GDI_ReleaseObj( hdc );
486 /***********************************************************************
487 * GetClipBox (GDI.77)
489 INT16 WINAPI GetClipBox16( HDC16 hdc, LPRECT16 rect )
492 DC *dc = DC_GetDCUpdate( hdc );
493 if (!dc) return ERROR;
494 ret = GetRgnBox16( dc->hGCClipRgn, rect );
495 rect->left -= dc->DCOrgX;
496 rect->right -= dc->DCOrgX;
497 rect->top -= dc->DCOrgY;
498 rect->bottom -= dc->DCOrgY;
499 DPtoLP16( hdc, (LPPOINT16)rect, 2 );
500 TRACE("%d,%d-%d,%d\n", rect->left,rect->top,rect->right,rect->bottom );
501 GDI_ReleaseObj( hdc );
506 /***********************************************************************
507 * GetClipBox (GDI32.@)
509 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
512 DC *dc = DC_GetDCUpdate( hdc );
513 if (!dc) return ERROR;
514 ret = GetRgnBox( dc->hGCClipRgn, rect );
515 rect->left -= dc->DCOrgX;
516 rect->right -= dc->DCOrgX;
517 rect->top -= dc->DCOrgY;
518 rect->bottom -= dc->DCOrgY;
519 DPtoLP( hdc, (LPPOINT)rect, 2 );
520 GDI_ReleaseObj( hdc );
525 /***********************************************************************
526 * GetClipRgn (GDI32.@)
528 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
532 if (hRgn && (dc = DC_GetDCPtr( hdc )))
536 /* this assumes that dc->hClipRgn is in coordinates
537 relative to the device (not DC origin) */
539 if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR )
541 OffsetRgn( hRgn, -dc->DCOrgX, -dc->DCOrgY );
546 GDI_ReleaseObj( hdc );
551 /***********************************************************************
552 * SaveVisRgn (GDI.129)
554 HRGN16 WINAPI SaveVisRgn16( HDC16 hdc )
557 RGNOBJ *obj, *copyObj;
558 DC *dc = DC_GetDCUpdate( hdc );
561 TRACE("%04x\n", hdc );
563 if (!(obj = (RGNOBJ *) GDI_GetObjPtr( dc->hVisRgn, REGION_MAGIC )))
565 GDI_ReleaseObj( hdc );
568 if (!(copy = CreateRectRgn( 0, 0, 0, 0 )))
570 GDI_ReleaseObj( dc->hVisRgn );
571 GDI_ReleaseObj( hdc );
574 CombineRgn( copy, dc->hVisRgn, 0, RGN_COPY );
575 if (!(copyObj = (RGNOBJ *) GDI_GetObjPtr( copy, REGION_MAGIC )))
577 DeleteObject( copy );
578 GDI_ReleaseObj( dc->hVisRgn );
579 GDI_ReleaseObj( hdc );
582 copyObj->header.hNext = obj->header.hNext;
583 obj->header.hNext = copy;
584 GDI_ReleaseObj( copy );
585 GDI_ReleaseObj( dc->hVisRgn );
586 GDI_ReleaseObj( hdc );
591 /***********************************************************************
592 * RestoreVisRgn (GDI.130)
594 INT16 WINAPI RestoreVisRgn16( HDC16 hdc )
597 RGNOBJ *obj, *savedObj;
598 DC *dc = DC_GetDCPtr( hdc );
601 if (!dc) return ERROR;
602 if (!dc->hVisRgn) goto done;
603 TRACE("%04x\n", hdc );
604 if (!(obj = (RGNOBJ *) GDI_GetObjPtr( dc->hVisRgn, REGION_MAGIC ))) goto done;
606 saved = obj->header.hNext;
607 GDI_ReleaseObj( dc->hVisRgn );
608 if (!saved || !(savedObj = (RGNOBJ *) GDI_GetObjPtr( saved, REGION_MAGIC ))) goto done;
610 DeleteObject( dc->hVisRgn );
612 dc->flags &= ~DC_DIRTY;
613 CLIPPING_UpdateGCRegion( dc );
614 ret = savedObj->rgn->type; /* FIXME */
615 GDI_ReleaseObj( saved );
617 GDI_ReleaseObj( hdc );