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 "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(clipping);
34 /***********************************************************************
35 * CLIPPING_UpdateGCRegion
37 * Update the GC clip region when the ClipRgn or VisRgn have changed.
39 void CLIPPING_UpdateGCRegion( DC * dc )
41 if (!dc->hGCClipRgn) dc->hGCClipRgn = CreateRectRgn( 0, 0, 0, 0 );
45 ERR("hVisRgn is zero. Please report this.\n" );
49 if (dc->flags & DC_DIRTY) ERR( "DC is dirty. Please report this.\n" );
52 CombineRgn( dc->hGCClipRgn, dc->hVisRgn, 0, RGN_COPY );
54 CombineRgn(dc->hGCClipRgn, dc->hClipRgn, dc->hVisRgn, RGN_AND);
55 if (dc->funcs->pSetDeviceClipping)
56 dc->funcs->pSetDeviceClipping( dc->physDev, dc->hGCClipRgn );
60 /***********************************************************************
61 * SelectClipRgn (GDI32.@)
63 INT WINAPI SelectClipRgn( HDC hdc, HRGN hrgn )
65 return ExtSelectClipRgn( hdc, hrgn, RGN_COPY );
69 /******************************************************************************
70 * ExtSelectClipRgn [GDI32.@]
72 INT WINAPI ExtSelectClipRgn( HDC hdc, HRGN hrgn, INT fnMode )
76 DC * dc = DC_GetDCUpdate( hdc );
77 if (!dc) return ERROR;
79 TRACE("%p %p %d\n", hdc, hrgn, fnMode );
81 if (dc->funcs->pExtSelectClipRgn)
83 retval = dc->funcs->pExtSelectClipRgn( dc->physDev, hrgn, fnMode );
84 GDI_ReleaseObj( hdc );
90 if (fnMode == RGN_COPY)
92 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
97 FIXME("Unimplemented: hrgn NULL in mode: %d\n", fnMode);
98 GDI_ReleaseObj( hdc );
107 GetRgnBox( dc->hVisRgn, &rect );
108 dc->hClipRgn = CreateRectRgnIndirect( &rect );
111 if(fnMode == RGN_COPY)
112 CombineRgn( dc->hClipRgn, hrgn, 0, fnMode );
114 CombineRgn( dc->hClipRgn, dc->hClipRgn, hrgn, fnMode);
117 CLIPPING_UpdateGCRegion( dc );
118 GDI_ReleaseObj( hdc );
120 return GetClipBox(hdc, &rect);
123 /***********************************************************************
124 * SelectVisRgn (GDI.105)
126 INT16 WINAPI SelectVisRgn16( HDC16 hdc16, HRGN16 hrgn )
129 HDC hdc = HDC_32( hdc16 );
132 if (!hrgn) return ERROR;
133 if (!(dc = DC_GetDCPtr( hdc ))) return ERROR;
135 TRACE("%p %04x\n", hdc, hrgn );
137 dc->flags &= ~DC_DIRTY;
139 retval = CombineRgn( dc->hVisRgn, HRGN_32(hrgn), 0, RGN_COPY );
140 CLIPPING_UpdateGCRegion( dc );
141 GDI_ReleaseObj( hdc );
146 /***********************************************************************
147 * OffsetClipRgn (GDI32.@)
149 INT WINAPI OffsetClipRgn( HDC hdc, INT x, INT y )
151 INT ret = SIMPLEREGION;
152 DC *dc = DC_GetDCUpdate( hdc );
153 if (!dc) return ERROR;
155 TRACE("%p %d,%d\n", hdc, x, y );
157 if(dc->funcs->pOffsetClipRgn)
158 ret = dc->funcs->pOffsetClipRgn( dc->physDev, x, y );
159 else if (dc->hClipRgn) {
160 ret = OffsetRgn( dc->hClipRgn, XLSTODS(dc,x), YLSTODS(dc,y));
161 CLIPPING_UpdateGCRegion( dc );
163 GDI_ReleaseObj( hdc );
168 /***********************************************************************
169 * OffsetVisRgn (GDI.102)
171 INT16 WINAPI OffsetVisRgn16( HDC16 hdc16, INT16 x, INT16 y )
174 HDC hdc = HDC_32( hdc16 );
175 DC * dc = DC_GetDCUpdate( hdc );
176 if (!dc) return ERROR;
177 TRACE("%p %d,%d\n", hdc, x, y );
178 retval = OffsetRgn( dc->hVisRgn, x, y );
179 CLIPPING_UpdateGCRegion( dc );
180 GDI_ReleaseObj( hdc );
185 /***********************************************************************
186 * ExcludeClipRect (GDI32.@)
188 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
189 INT right, INT bottom )
193 DC *dc = DC_GetDCUpdate( hdc );
194 if (!dc) return ERROR;
196 TRACE("%p %dx%d,%dx%d\n", hdc, left, top, right, bottom );
198 if(dc->funcs->pExcludeClipRect)
199 ret = dc->funcs->pExcludeClipRect( dc->physDev, left, top, right, bottom );
208 LPtoDP( hdc, pt, 2 );
209 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
214 dc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
215 CombineRgn( dc->hClipRgn, dc->hVisRgn, 0, RGN_COPY );
217 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_DIFF );
218 DeleteObject( newRgn );
220 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
222 GDI_ReleaseObj( hdc );
227 /***********************************************************************
228 * IntersectClipRect (GDI32.@)
230 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
233 DC *dc = DC_GetDCUpdate( hdc );
234 if (!dc) return ERROR;
236 TRACE("%p %d,%d - %d,%d\n", hdc, left, top, right, bottom );
238 if(dc->funcs->pIntersectClipRect)
239 ret = dc->funcs->pIntersectClipRect( dc->physDev, left, top, right, bottom );
249 LPtoDP( hdc, pt, 2 );
253 dc->hClipRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y );
260 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
263 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_AND );
264 DeleteObject( newRgn );
267 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
269 GDI_ReleaseObj( hdc );
274 /***********************************************************************
275 * ExcludeVisRect (GDI.73)
277 INT16 WINAPI ExcludeVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
282 HDC hdc = HDC_32( hdc16 );
283 DC * dc = DC_GetDCUpdate( hdc );
284 if (!dc) return ERROR;
291 LPtoDP( hdc, pt, 2 );
293 TRACE("%p %ld,%ld - %ld,%ld\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
295 if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
298 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_DIFF );
299 DeleteObject( tempRgn );
301 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
302 GDI_ReleaseObj( hdc );
307 /***********************************************************************
308 * IntersectVisRect (GDI.98)
310 INT16 WINAPI IntersectVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
315 HDC hdc = HDC_32( hdc16 );
316 DC * dc = DC_GetDCUpdate( hdc );
317 if (!dc) return ERROR;
324 LPtoDP( hdc, pt, 2 );
326 TRACE("%p %ld,%ld - %ld,%ld\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
329 if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
332 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_AND );
333 DeleteObject( tempRgn );
335 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
336 GDI_ReleaseObj( hdc );
341 /***********************************************************************
342 * PtVisible (GDI32.@)
344 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
347 DC *dc = DC_GetDCUpdate( hdc );
349 TRACE("%p %d,%d\n", hdc, x, y );
350 if (!dc) return FALSE;
357 LPtoDP( hdc, &pt, 1 );
358 ret = PtInRegion( dc->hGCClipRgn, pt.x, pt.y );
360 GDI_ReleaseObj( hdc );
365 /***********************************************************************
366 * RectVisible (GDI32.@)
368 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
371 DC *dc = DC_GetDCUpdate( hdc );
372 if (!dc) return FALSE;
373 TRACE("%p %ld,%ldx%ld,%ld\n", hdc, rect->left, rect->top, rect->right, rect->bottom );
379 pt[0].x = rect->left;
381 pt[1].x = rect->right;
382 pt[1].y = rect->bottom;
383 LPtoDP( hdc, pt, 2 );
384 tmpRect.left = pt[0].x;
385 tmpRect.top = pt[0].y;
386 tmpRect.right = pt[1].x;
387 tmpRect.bottom = pt[1].y;
388 ret = RectInRegion( dc->hGCClipRgn, &tmpRect );
390 GDI_ReleaseObj( hdc );
395 /***********************************************************************
396 * GetClipBox (GDI32.@)
398 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
401 DC *dc = DC_GetDCUpdate( hdc );
402 if (!dc) return ERROR;
403 ret = GetRgnBox( dc->hGCClipRgn, rect );
404 DPtoLP( hdc, (LPPOINT)rect, 2 );
405 GDI_ReleaseObj( hdc );
410 /***********************************************************************
411 * GetClipRgn (GDI32.@)
413 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
417 if (hRgn && (dc = DC_GetDCPtr( hdc )))
421 if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR ) ret = 1;
424 GDI_ReleaseObj( hdc );
429 /***********************************************************************
430 * SaveVisRgn (GDI.129)
432 HRGN16 WINAPI SaveVisRgn16( HDC16 hdc16 )
435 GDIOBJHDR *obj, *copyObj;
436 HDC hdc = HDC_32( hdc16 );
437 DC *dc = DC_GetDCUpdate( hdc );
442 if (!(obj = GDI_GetObjPtr( dc->hVisRgn, REGION_MAGIC )))
444 GDI_ReleaseObj( hdc );
447 if (!(copy = CreateRectRgn( 0, 0, 0, 0 )))
449 GDI_ReleaseObj( dc->hVisRgn );
450 GDI_ReleaseObj( hdc );
453 CombineRgn( copy, dc->hVisRgn, 0, RGN_COPY );
454 if (!(copyObj = GDI_GetObjPtr( copy, REGION_MAGIC )))
456 DeleteObject( copy );
457 GDI_ReleaseObj( dc->hVisRgn );
458 GDI_ReleaseObj( hdc );
461 copyObj->hNext = obj->hNext;
462 obj->hNext = HRGN_16(copy);
463 GDI_ReleaseObj( copy );
464 GDI_ReleaseObj( dc->hVisRgn );
465 GDI_ReleaseObj( hdc );
466 return HRGN_16(copy);
470 /***********************************************************************
471 * RestoreVisRgn (GDI.130)
473 INT16 WINAPI RestoreVisRgn16( HDC16 hdc16 )
476 GDIOBJHDR *obj, *savedObj;
477 HDC hdc = HDC_32( hdc16 );
478 DC *dc = DC_GetDCPtr( hdc );
481 if (!dc) return ERROR;
485 if (!(obj = GDI_GetObjPtr( dc->hVisRgn, REGION_MAGIC ))) goto done;
486 saved = HRGN_32(obj->hNext);
488 if ((savedObj = GDI_GetObjPtr( saved, REGION_MAGIC )))
490 ret = CombineRgn( dc->hVisRgn, saved, 0, RGN_COPY );
491 obj->hNext = savedObj->hNext;
492 GDI_ReleaseObj( saved );
493 DeleteObject( saved );
494 dc->flags &= ~DC_DIRTY;
495 CLIPPING_UpdateGCRegion( dc );
497 GDI_ReleaseObj( dc->hVisRgn );
499 GDI_ReleaseObj( hdc );
504 /***********************************************************************
505 * GetRandomRgn [GDI32.@]
508 * This function is documented in MSDN online for the case of
509 * dwCode == SYSRGN (4).
511 * For dwCode == 1 it should return the clip region
512 * 2 " " " the meta region
513 * 3 " " " the intersection of the clip with
514 * the meta region (== 'Rao' region).
516 * See http://www.codeproject.com/gdi/cliprgnguide.asp
518 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, DWORD dwCode)
522 case SYSRGN: /* == 4 */
524 DC *dc = DC_GetDCPtr (hDC);
527 CombineRgn (hRgn, dc->hVisRgn, 0, RGN_COPY);
528 GDI_ReleaseObj( hDC );
530 * On Windows NT/2000,
531 * the region returned is in screen coordinates.
533 * the region returned is in window coordinates
535 if (!(GetVersion() & 0x80000000))
538 GetDCOrgEx(hDC, &org);
539 OffsetRgn(hRgn, org.x, org.y);
544 case 1: /* clip region */
545 return GetClipRgn (hDC, hRgn);
548 WARN("Unknown dwCode %ld\n", dwCode);
556 /***********************************************************************
557 * GetMetaRgn (GDI32.@)
559 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
567 /***********************************************************************
568 * SetMetaRgn (GDI32.@)
570 INT WINAPI SetMetaRgn( HDC hdc )