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 )
44 ERR("hVisRgn is zero. Please report this.\n" );
48 if (dc->flags & DC_DIRTY) ERR( "DC is dirty. Please report this.\n" );
50 if (dc->funcs->pSetDeviceClipping)
51 dc->funcs->pSetDeviceClipping( dc->physDev, dc->hVisRgn, dc->hClipRgn );
54 /***********************************************************************
55 * create_default_clip_rgn
57 * Create a default clipping region when none already exists.
59 static inline void create_default_clip_region( DC * dc )
61 dc->hClipRgn = CreateRectRgn(0, 0,
62 GetDeviceCaps( dc->hSelf, HORZRES ),
63 GetDeviceCaps( dc->hSelf, VERTRES ));
67 /***********************************************************************
68 * SelectClipRgn (GDI32.@)
70 INT WINAPI SelectClipRgn( HDC hdc, HRGN hrgn )
72 return ExtSelectClipRgn( hdc, hrgn, RGN_COPY );
76 /******************************************************************************
77 * ExtSelectClipRgn [GDI32.@]
79 INT WINAPI ExtSelectClipRgn( HDC hdc, HRGN hrgn, INT fnMode )
83 DC * dc = DC_GetDCUpdate( hdc );
84 if (!dc) return ERROR;
86 TRACE("%p %p %d\n", hdc, hrgn, fnMode );
88 if (dc->funcs->pExtSelectClipRgn)
90 retval = dc->funcs->pExtSelectClipRgn( dc->physDev, hrgn, fnMode );
91 GDI_ReleaseObj( hdc );
97 if (fnMode == RGN_COPY)
99 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
104 FIXME("Unimplemented: hrgn NULL in mode: %d\n", fnMode);
105 GDI_ReleaseObj( hdc );
112 create_default_clip_region( dc );
114 if(fnMode == RGN_COPY)
115 CombineRgn( dc->hClipRgn, hrgn, 0, fnMode );
117 CombineRgn( dc->hClipRgn, dc->hClipRgn, hrgn, fnMode);
120 CLIPPING_UpdateGCRegion( dc );
121 GDI_ReleaseObj( hdc );
123 return GetClipBox(hdc, &rect);
126 /***********************************************************************
127 * SelectVisRgn (GDI.105)
129 INT16 WINAPI SelectVisRgn16( HDC16 hdc16, HRGN16 hrgn )
132 HDC hdc = HDC_32( hdc16 );
135 if (!hrgn) return ERROR;
136 if (!(dc = DC_GetDCPtr( hdc ))) return ERROR;
138 TRACE("%p %04x\n", hdc, hrgn );
140 dc->flags &= ~DC_DIRTY;
142 retval = CombineRgn( dc->hVisRgn, HRGN_32(hrgn), 0, RGN_COPY );
143 CLIPPING_UpdateGCRegion( dc );
144 GDI_ReleaseObj( hdc );
149 /***********************************************************************
150 * OffsetClipRgn (GDI32.@)
152 INT WINAPI OffsetClipRgn( HDC hdc, INT x, INT y )
154 INT ret = SIMPLEREGION;
155 DC *dc = DC_GetDCUpdate( hdc );
156 if (!dc) return ERROR;
158 TRACE("%p %d,%d\n", hdc, x, y );
160 if(dc->funcs->pOffsetClipRgn)
161 ret = dc->funcs->pOffsetClipRgn( dc->physDev, x, y );
162 else if (dc->hClipRgn) {
163 ret = OffsetRgn( dc->hClipRgn, MulDiv( x, dc->vportExtX, dc->wndExtX ),
164 MulDiv( y, dc->vportExtY, dc->wndExtY ) );
165 CLIPPING_UpdateGCRegion( dc );
167 GDI_ReleaseObj( hdc );
172 /***********************************************************************
173 * OffsetVisRgn (GDI.102)
175 INT16 WINAPI OffsetVisRgn16( HDC16 hdc16, INT16 x, INT16 y )
178 HDC hdc = HDC_32( hdc16 );
179 DC * dc = DC_GetDCUpdate( hdc );
180 if (!dc) return ERROR;
181 TRACE("%p %d,%d\n", hdc, x, y );
182 retval = OffsetRgn( dc->hVisRgn, x, y );
183 CLIPPING_UpdateGCRegion( dc );
184 GDI_ReleaseObj( hdc );
189 /***********************************************************************
190 * ExcludeClipRect (GDI32.@)
192 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
193 INT right, INT bottom )
197 DC *dc = DC_GetDCUpdate( hdc );
198 if (!dc) return ERROR;
200 TRACE("%p %dx%d,%dx%d\n", hdc, left, top, right, bottom );
202 if(dc->funcs->pExcludeClipRect)
203 ret = dc->funcs->pExcludeClipRect( dc->physDev, left, top, right, bottom );
212 LPtoDP( hdc, pt, 2 );
213 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
217 create_default_clip_region( dc );
218 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_DIFF );
219 DeleteObject( newRgn );
221 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
223 GDI_ReleaseObj( hdc );
228 /***********************************************************************
229 * IntersectClipRect (GDI32.@)
231 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
234 DC *dc = DC_GetDCUpdate( hdc );
235 if (!dc) return ERROR;
237 TRACE("%p %d,%d - %d,%d\n", hdc, left, top, right, bottom );
239 if(dc->funcs->pIntersectClipRect)
240 ret = dc->funcs->pIntersectClipRect( dc->physDev, left, top, right, bottom );
250 LPtoDP( hdc, pt, 2 );
254 dc->hClipRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y );
261 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
264 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_AND );
265 DeleteObject( newRgn );
268 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
270 GDI_ReleaseObj( hdc );
275 /***********************************************************************
276 * ExcludeVisRect (GDI.73)
278 INT16 WINAPI ExcludeVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
283 HDC hdc = HDC_32( hdc16 );
284 DC * dc = DC_GetDCUpdate( hdc );
285 if (!dc) return ERROR;
292 LPtoDP( hdc, pt, 2 );
294 TRACE("%p %ld,%ld - %ld,%ld\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
296 if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
299 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_DIFF );
300 DeleteObject( tempRgn );
302 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
303 GDI_ReleaseObj( hdc );
308 /***********************************************************************
309 * IntersectVisRect (GDI.98)
311 INT16 WINAPI IntersectVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
316 HDC hdc = HDC_32( hdc16 );
317 DC * dc = DC_GetDCUpdate( hdc );
318 if (!dc) return ERROR;
325 LPtoDP( hdc, pt, 2 );
327 TRACE("%p %ld,%ld - %ld,%ld\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
330 if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
333 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_AND );
334 DeleteObject( tempRgn );
336 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
337 GDI_ReleaseObj( hdc );
342 /***********************************************************************
343 * PtVisible (GDI32.@)
345 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;
356 LPtoDP( hdc, &pt, 1 );
357 ret = PtInRegion( dc->hVisRgn, pt.x, pt.y );
358 if (ret && dc->hClipRgn) ret = PtInRegion( dc->hClipRgn, pt.x, pt.y );
359 GDI_ReleaseObj( hdc );
364 /***********************************************************************
365 * RectVisible (GDI32.@)
367 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 );
376 LPtoDP( hdc, (POINT *)&tmpRect, 2 );
380 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
381 CombineRgn( hrgn, dc->hVisRgn, dc->hClipRgn, RGN_AND );
382 ret = RectInRegion( hrgn, &tmpRect );
383 DeleteObject( hrgn );
385 else ret = RectInRegion( dc->hVisRgn, &tmpRect );
386 GDI_ReleaseObj( hdc );
391 /***********************************************************************
392 * GetClipBox (GDI32.@)
394 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
397 DC *dc = DC_GetDCUpdate( hdc );
398 if (!dc) return ERROR;
401 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
402 CombineRgn( hrgn, dc->hVisRgn, dc->hClipRgn, RGN_AND );
403 ret = GetRgnBox( hrgn, rect );
404 DeleteObject( hrgn );
406 else ret = GetRgnBox( dc->hVisRgn, rect );
407 DPtoLP( hdc, (LPPOINT)rect, 2 );
408 GDI_ReleaseObj( hdc );
413 /***********************************************************************
414 * GetClipRgn (GDI32.@)
416 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
420 if (hRgn && (dc = DC_GetDCPtr( hdc )))
424 if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR ) ret = 1;
427 GDI_ReleaseObj( hdc );
432 /***********************************************************************
433 * SaveVisRgn (GDI.129)
435 HRGN16 WINAPI SaveVisRgn16( HDC16 hdc16 )
438 GDIOBJHDR *obj, *copyObj;
439 HDC hdc = HDC_32( hdc16 );
440 DC *dc = DC_GetDCUpdate( hdc );
445 if (!(obj = GDI_GetObjPtr( dc->hVisRgn, REGION_MAGIC )))
447 GDI_ReleaseObj( hdc );
450 if (!(copy = CreateRectRgn( 0, 0, 0, 0 )))
452 GDI_ReleaseObj( dc->hVisRgn );
453 GDI_ReleaseObj( hdc );
456 CombineRgn( copy, dc->hVisRgn, 0, RGN_COPY );
457 if (!(copyObj = GDI_GetObjPtr( copy, REGION_MAGIC )))
459 DeleteObject( copy );
460 GDI_ReleaseObj( dc->hVisRgn );
461 GDI_ReleaseObj( hdc );
464 copyObj->hNext = obj->hNext;
465 obj->hNext = HRGN_16(copy);
466 GDI_ReleaseObj( copy );
467 GDI_ReleaseObj( dc->hVisRgn );
468 GDI_ReleaseObj( hdc );
469 return HRGN_16(copy);
473 /***********************************************************************
474 * RestoreVisRgn (GDI.130)
476 INT16 WINAPI RestoreVisRgn16( HDC16 hdc16 )
479 GDIOBJHDR *obj, *savedObj;
480 HDC hdc = HDC_32( hdc16 );
481 DC *dc = DC_GetDCPtr( hdc );
484 if (!dc) return ERROR;
488 if (!(obj = GDI_GetObjPtr( dc->hVisRgn, REGION_MAGIC ))) goto done;
489 saved = HRGN_32(obj->hNext);
491 if ((savedObj = GDI_GetObjPtr( saved, REGION_MAGIC )))
493 ret = CombineRgn( dc->hVisRgn, saved, 0, RGN_COPY );
494 obj->hNext = savedObj->hNext;
495 GDI_ReleaseObj( saved );
496 DeleteObject( saved );
497 dc->flags &= ~DC_DIRTY;
498 CLIPPING_UpdateGCRegion( dc );
500 GDI_ReleaseObj( dc->hVisRgn );
502 GDI_ReleaseObj( hdc );
507 /***********************************************************************
508 * GetRandomRgn [GDI32.@]
511 * This function is documented in MSDN online for the case of
512 * iCode == SYSRGN (4).
514 * For iCode == 1 it should return the clip region
515 * 2 " " " the meta region
516 * 3 " " " the intersection of the clip with
517 * the meta region (== 'Rao' region).
519 * See http://www.codeproject.com/gdi/cliprgnguide.asp
521 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, INT iCode)
525 case SYSRGN: /* == 4 */
527 DC *dc = DC_GetDCPtr (hDC);
530 CombineRgn (hRgn, dc->hVisRgn, 0, RGN_COPY);
531 GDI_ReleaseObj( hDC );
533 * On Windows NT/2000,
534 * the region returned is in screen coordinates.
536 * the region returned is in window coordinates
538 if (!(GetVersion() & 0x80000000))
541 GetDCOrgEx(hDC, &org);
542 OffsetRgn(hRgn, org.x, org.y);
547 case 1: /* clip region */
548 return GetClipRgn (hDC, hRgn);
551 WARN("Unknown iCode %d\n", iCode);
559 /***********************************************************************
560 * GetMetaRgn (GDI32.@)
562 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
570 /***********************************************************************
571 * SetMetaRgn (GDI32.@)
573 INT WINAPI SetMetaRgn( HDC hdc )