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 );
55 /***********************************************************************
56 * SelectClipRgn (GDI32.@)
58 INT WINAPI SelectClipRgn( HDC hdc, HRGN hrgn )
60 return ExtSelectClipRgn( hdc, hrgn, RGN_COPY );
64 /******************************************************************************
65 * ExtSelectClipRgn [GDI32.@]
67 INT WINAPI ExtSelectClipRgn( HDC hdc, HRGN hrgn, INT fnMode )
71 DC * dc = DC_GetDCUpdate( hdc );
72 if (!dc) return ERROR;
74 TRACE("%p %p %d\n", hdc, hrgn, fnMode );
76 if (dc->funcs->pExtSelectClipRgn)
78 retval = dc->funcs->pExtSelectClipRgn( dc->physDev, hrgn, fnMode );
79 GDI_ReleaseObj( hdc );
85 if (fnMode == RGN_COPY)
87 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
92 FIXME("Unimplemented: hrgn NULL in mode: %d\n", fnMode);
93 GDI_ReleaseObj( hdc );
102 GetRgnBox( dc->hVisRgn, &rect );
103 dc->hClipRgn = CreateRectRgnIndirect( &rect );
106 if(fnMode == RGN_COPY)
107 CombineRgn( dc->hClipRgn, hrgn, 0, fnMode );
109 CombineRgn( dc->hClipRgn, dc->hClipRgn, hrgn, fnMode);
112 CLIPPING_UpdateGCRegion( dc );
113 GDI_ReleaseObj( hdc );
115 return GetClipBox(hdc, &rect);
118 /***********************************************************************
119 * SelectVisRgn (GDI.105)
121 INT16 WINAPI SelectVisRgn16( HDC16 hdc16, HRGN16 hrgn )
124 HDC hdc = HDC_32( hdc16 );
127 if (!hrgn) return ERROR;
128 if (!(dc = DC_GetDCPtr( hdc ))) return ERROR;
130 TRACE("%p %04x\n", hdc, hrgn );
132 dc->flags &= ~DC_DIRTY;
134 retval = CombineRgn( dc->hVisRgn, HRGN_32(hrgn), 0, RGN_COPY );
135 CLIPPING_UpdateGCRegion( dc );
136 GDI_ReleaseObj( hdc );
141 /***********************************************************************
142 * OffsetClipRgn (GDI32.@)
144 INT WINAPI OffsetClipRgn( HDC hdc, INT x, INT y )
146 INT ret = SIMPLEREGION;
147 DC *dc = DC_GetDCUpdate( hdc );
148 if (!dc) return ERROR;
150 TRACE("%p %d,%d\n", hdc, x, y );
152 if(dc->funcs->pOffsetClipRgn)
153 ret = dc->funcs->pOffsetClipRgn( dc->physDev, x, y );
154 else if (dc->hClipRgn) {
155 ret = OffsetRgn( dc->hClipRgn, MulDiv( x, dc->vportExtX, dc->wndExtX ),
156 MulDiv( y, dc->vportExtY, dc->wndExtY ) );
157 CLIPPING_UpdateGCRegion( dc );
159 GDI_ReleaseObj( hdc );
164 /***********************************************************************
165 * OffsetVisRgn (GDI.102)
167 INT16 WINAPI OffsetVisRgn16( HDC16 hdc16, INT16 x, INT16 y )
170 HDC hdc = HDC_32( hdc16 );
171 DC * dc = DC_GetDCUpdate( hdc );
172 if (!dc) return ERROR;
173 TRACE("%p %d,%d\n", hdc, x, y );
174 retval = OffsetRgn( dc->hVisRgn, x, y );
175 CLIPPING_UpdateGCRegion( dc );
176 GDI_ReleaseObj( hdc );
181 /***********************************************************************
182 * ExcludeClipRect (GDI32.@)
184 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
185 INT right, INT bottom )
189 DC *dc = DC_GetDCUpdate( hdc );
190 if (!dc) return ERROR;
192 TRACE("%p %dx%d,%dx%d\n", hdc, left, top, right, bottom );
194 if(dc->funcs->pExcludeClipRect)
195 ret = dc->funcs->pExcludeClipRect( dc->physDev, left, top, right, bottom );
204 LPtoDP( hdc, pt, 2 );
205 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
210 dc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
211 CombineRgn( dc->hClipRgn, dc->hVisRgn, 0, RGN_COPY );
213 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_DIFF );
214 DeleteObject( newRgn );
216 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
218 GDI_ReleaseObj( hdc );
223 /***********************************************************************
224 * IntersectClipRect (GDI32.@)
226 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
229 DC *dc = DC_GetDCUpdate( hdc );
230 if (!dc) return ERROR;
232 TRACE("%p %d,%d - %d,%d\n", hdc, left, top, right, bottom );
234 if(dc->funcs->pIntersectClipRect)
235 ret = dc->funcs->pIntersectClipRect( dc->physDev, left, top, right, bottom );
245 LPtoDP( hdc, pt, 2 );
249 dc->hClipRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y );
256 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
259 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_AND );
260 DeleteObject( newRgn );
263 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
265 GDI_ReleaseObj( hdc );
270 /***********************************************************************
271 * ExcludeVisRect (GDI.73)
273 INT16 WINAPI ExcludeVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
278 HDC hdc = HDC_32( hdc16 );
279 DC * dc = DC_GetDCUpdate( hdc );
280 if (!dc) return ERROR;
287 LPtoDP( hdc, pt, 2 );
289 TRACE("%p %ld,%ld - %ld,%ld\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
291 if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
294 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_DIFF );
295 DeleteObject( tempRgn );
297 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
298 GDI_ReleaseObj( hdc );
303 /***********************************************************************
304 * IntersectVisRect (GDI.98)
306 INT16 WINAPI IntersectVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
311 HDC hdc = HDC_32( hdc16 );
312 DC * dc = DC_GetDCUpdate( hdc );
313 if (!dc) return ERROR;
320 LPtoDP( hdc, pt, 2 );
322 TRACE("%p %ld,%ld - %ld,%ld\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
325 if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
328 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_AND );
329 DeleteObject( tempRgn );
331 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
332 GDI_ReleaseObj( hdc );
337 /***********************************************************************
338 * PtVisible (GDI32.@)
340 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
344 DC *dc = DC_GetDCUpdate( hdc );
346 TRACE("%p %d,%d\n", hdc, x, y );
347 if (!dc) return FALSE;
351 LPtoDP( hdc, &pt, 1 );
352 ret = PtInRegion( dc->hVisRgn, pt.x, pt.y );
353 if (ret && dc->hClipRgn) ret = PtInRegion( dc->hClipRgn, pt.x, pt.y );
354 GDI_ReleaseObj( hdc );
359 /***********************************************************************
360 * RectVisible (GDI32.@)
362 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
366 DC *dc = DC_GetDCUpdate( hdc );
367 if (!dc) return FALSE;
368 TRACE("%p %ld,%ldx%ld,%ld\n", hdc, rect->left, rect->top, rect->right, rect->bottom );
371 LPtoDP( hdc, (POINT *)&tmpRect, 2 );
375 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
376 CombineRgn( hrgn, dc->hVisRgn, dc->hClipRgn, RGN_AND );
377 ret = RectInRegion( hrgn, &tmpRect );
378 DeleteObject( hrgn );
380 else ret = RectInRegion( dc->hVisRgn, &tmpRect );
381 GDI_ReleaseObj( hdc );
386 /***********************************************************************
387 * GetClipBox (GDI32.@)
389 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
392 DC *dc = DC_GetDCUpdate( hdc );
393 if (!dc) return ERROR;
396 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
397 CombineRgn( hrgn, dc->hVisRgn, dc->hClipRgn, RGN_AND );
398 ret = GetRgnBox( hrgn, rect );
399 DeleteObject( hrgn );
401 else ret = GetRgnBox( dc->hVisRgn, rect );
402 DPtoLP( hdc, (LPPOINT)rect, 2 );
403 GDI_ReleaseObj( hdc );
408 /***********************************************************************
409 * GetClipRgn (GDI32.@)
411 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
415 if (hRgn && (dc = DC_GetDCPtr( hdc )))
419 if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR ) ret = 1;
422 GDI_ReleaseObj( hdc );
427 /***********************************************************************
428 * SaveVisRgn (GDI.129)
430 HRGN16 WINAPI SaveVisRgn16( HDC16 hdc16 )
433 GDIOBJHDR *obj, *copyObj;
434 HDC hdc = HDC_32( hdc16 );
435 DC *dc = DC_GetDCUpdate( hdc );
440 if (!(obj = GDI_GetObjPtr( dc->hVisRgn, REGION_MAGIC )))
442 GDI_ReleaseObj( hdc );
445 if (!(copy = CreateRectRgn( 0, 0, 0, 0 )))
447 GDI_ReleaseObj( dc->hVisRgn );
448 GDI_ReleaseObj( hdc );
451 CombineRgn( copy, dc->hVisRgn, 0, RGN_COPY );
452 if (!(copyObj = GDI_GetObjPtr( copy, REGION_MAGIC )))
454 DeleteObject( copy );
455 GDI_ReleaseObj( dc->hVisRgn );
456 GDI_ReleaseObj( hdc );
459 copyObj->hNext = obj->hNext;
460 obj->hNext = HRGN_16(copy);
461 GDI_ReleaseObj( copy );
462 GDI_ReleaseObj( dc->hVisRgn );
463 GDI_ReleaseObj( hdc );
464 return HRGN_16(copy);
468 /***********************************************************************
469 * RestoreVisRgn (GDI.130)
471 INT16 WINAPI RestoreVisRgn16( HDC16 hdc16 )
474 GDIOBJHDR *obj, *savedObj;
475 HDC hdc = HDC_32( hdc16 );
476 DC *dc = DC_GetDCPtr( hdc );
479 if (!dc) return ERROR;
483 if (!(obj = GDI_GetObjPtr( dc->hVisRgn, REGION_MAGIC ))) goto done;
484 saved = HRGN_32(obj->hNext);
486 if ((savedObj = GDI_GetObjPtr( saved, REGION_MAGIC )))
488 ret = CombineRgn( dc->hVisRgn, saved, 0, RGN_COPY );
489 obj->hNext = savedObj->hNext;
490 GDI_ReleaseObj( saved );
491 DeleteObject( saved );
492 dc->flags &= ~DC_DIRTY;
493 CLIPPING_UpdateGCRegion( dc );
495 GDI_ReleaseObj( dc->hVisRgn );
497 GDI_ReleaseObj( hdc );
502 /***********************************************************************
503 * GetRandomRgn [GDI32.@]
506 * This function is documented in MSDN online for the case of
507 * dwCode == SYSRGN (4).
509 * For dwCode == 1 it should return the clip region
510 * 2 " " " the meta region
511 * 3 " " " the intersection of the clip with
512 * the meta region (== 'Rao' region).
514 * See http://www.codeproject.com/gdi/cliprgnguide.asp
516 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, DWORD dwCode)
520 case SYSRGN: /* == 4 */
522 DC *dc = DC_GetDCPtr (hDC);
525 CombineRgn (hRgn, dc->hVisRgn, 0, RGN_COPY);
526 GDI_ReleaseObj( hDC );
528 * On Windows NT/2000,
529 * the region returned is in screen coordinates.
531 * the region returned is in window coordinates
533 if (!(GetVersion() & 0x80000000))
536 GetDCOrgEx(hDC, &org);
537 OffsetRgn(hRgn, org.x, org.y);
542 case 1: /* clip region */
543 return GetClipRgn (hDC, hRgn);
546 WARN("Unknown dwCode %ld\n", dwCode);
554 /***********************************************************************
555 * GetMetaRgn (GDI32.@)
557 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
565 /***********************************************************************
566 * SetMetaRgn (GDI32.@)
568 INT WINAPI SetMetaRgn( HDC hdc )