2 * DC clipping functions
4 * Copyright 1993 Alexandre Julliard
12 #define UPDATE_DIRTY_DC(dc) \
14 if ((dc)->hookProc && !((dc)->w.flags & (DC_SAVED | DC_MEMORY))) \
15 (dc)->hookProc( (dc)->hSelf, DCHC_INVALIDVISRGN, (dc)->dwHookData, 0 ); \
20 /***********************************************************************
21 * CLIPPING_UpdateGCRegion
23 * Update the GC clip region when the ClipRgn or VisRgn have changed.
25 void CLIPPING_UpdateGCRegion( DC * dc )
27 if (!dc->w.hGCClipRgn) dc->w.hGCClipRgn = CreateRectRgn32( 0, 0, 0, 0 );
31 ERR(region, "hVisRgn is zero. Please report this.\n" );
35 if (dc->w.flags & DC_DIRTY)
38 dc->w.flags &= ~DC_DIRTY;
42 CombineRgn32( dc->w.hGCClipRgn, dc->w.hVisRgn, 0, RGN_COPY );
44 CombineRgn32(dc->w.hGCClipRgn, dc->w.hClipRgn, dc->w.hVisRgn, RGN_AND);
45 if (dc->funcs->pSetDeviceClipping) dc->funcs->pSetDeviceClipping( dc );
49 /***********************************************************************
50 * SelectClipRgn16 (GDI.44)
52 INT16 WINAPI SelectClipRgn16( HDC16 hdc, HRGN16 hrgn )
54 return (INT16)SelectClipRgn32( hdc, hrgn );
58 /***********************************************************************
59 * SelectClipRgn32 (GDI32.297)
61 INT32 WINAPI SelectClipRgn32( HDC32 hdc, HRGN32 hrgn )
63 return ExtSelectClipRgn( hdc, hrgn, RGN_COPY );
66 /******************************************************************************
67 * ExtSelectClipRgn [GDI32.97]
69 INT32 WINAPI ExtSelectClipRgn( HDC32 hdc, HRGN32 hrgn, INT32 fnMode )
72 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
73 if (!dc) return ERROR;
75 TRACE( clipping, "%04x %04x %d\n", hdc, hrgn, fnMode );
79 if (fnMode == RGN_COPY)
81 if (dc->w.hClipRgn) DeleteObject16( dc->w.hClipRgn );
83 retval = SIMPLEREGION; /* Clip region == whole DC */
87 FIXME(clipping, "Unimplemented: hrgn NULL in mode: %d\n", fnMode);
96 GetRgnBox32( dc->w.hVisRgn, &rect );
97 dc->w.hClipRgn = CreateRectRgnIndirect32( &rect );
100 OffsetRgn32( dc->w.hClipRgn, -dc->w.DCOrgX, -dc->w.DCOrgY );
101 retval = CombineRgn32( dc->w.hClipRgn, dc->w.hClipRgn, hrgn, fnMode );
102 OffsetRgn32( dc->w.hClipRgn, dc->w.DCOrgX, dc->w.DCOrgY );
106 CLIPPING_UpdateGCRegion( dc );
107 GDI_HEAP_UNLOCK( hdc );
111 /***********************************************************************
112 * SelectVisRgn (GDI.105)
114 INT16 WINAPI SelectVisRgn( HDC16 hdc, HRGN16 hrgn )
117 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
118 if (!dc || !hrgn) return ERROR;
120 TRACE(clipping, "%04x %04x\n", hdc, hrgn );
122 dc->w.flags &= ~DC_DIRTY;
124 retval = CombineRgn16( dc->w.hVisRgn, hrgn, 0, RGN_COPY );
125 CLIPPING_UpdateGCRegion( dc );
126 GDI_HEAP_UNLOCK( hdc );
131 /***********************************************************************
132 * OffsetClipRgn16 (GDI.32)
134 INT16 WINAPI OffsetClipRgn16( HDC16 hdc, INT16 x, INT16 y )
136 return (INT16)OffsetClipRgn32( hdc, x, y );
140 /***********************************************************************
141 * OffsetClipRgn32 (GDI32.255)
143 INT32 WINAPI OffsetClipRgn32( HDC32 hdc, INT32 x, INT32 y )
145 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
148 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
149 if (!dc) return ERROR;
150 MF_MetaParam2(dc, META_OFFSETCLIPRGN, x, y);
151 GDI_HEAP_UNLOCK( hdc );
152 return NULLREGION; /* ?? */
155 TRACE(clipping, "%04x %d,%d\n", hdc, x, y );
159 INT32 ret = OffsetRgn32( dc->w.hClipRgn, XLPTODP(dc,x), YLPTODP(dc,y));
160 CLIPPING_UpdateGCRegion( dc );
161 GDI_HEAP_UNLOCK( hdc );
164 GDI_HEAP_UNLOCK( hdc );
165 return SIMPLEREGION; /* Clip region == client area */
169 /***********************************************************************
170 * OffsetVisRgn (GDI.102)
172 INT16 WINAPI OffsetVisRgn( HDC16 hdc, INT16 x, INT16 y )
175 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
176 if (!dc) return ERROR;
177 TRACE(clipping, "%04x %d,%d\n", hdc, x, y );
178 retval = OffsetRgn32( dc->w.hVisRgn, x, y );
179 CLIPPING_UpdateGCRegion( dc );
180 GDI_HEAP_UNLOCK( hdc );
185 /***********************************************************************
186 * CLIPPING_IntersectClipRect
188 * Helper function for {Intersect,Exclude}ClipRect, can be called from
189 * elsewhere (like ExtTextOut()) to skip redundant metafile update and
190 * coordinate conversion.
192 INT32 CLIPPING_IntersectClipRect( DC * dc, INT32 left, INT32 top,
193 INT32 right, INT32 bottom, UINT32 flags )
198 left += dc->w.DCOrgX;
199 right += dc->w.DCOrgX;
201 bottom += dc->w.DCOrgY;
203 if (!(newRgn = CreateRectRgn32( left, top, right, bottom ))) return ERROR;
206 if( flags & CLIP_INTERSECT )
208 dc->w.hClipRgn = newRgn;
209 CLIPPING_UpdateGCRegion( dc );
212 else if( flags & CLIP_EXCLUDE )
214 dc->w.hClipRgn = CreateRectRgn32( 0, 0, 0, 0 );
215 CombineRgn32( dc->w.hClipRgn, dc->w.hVisRgn, 0, RGN_COPY );
217 else WARN(clipping,"No hClipRgn and flags are %x\n",flags);
220 ret = CombineRgn32( newRgn, dc->w.hClipRgn, newRgn,
221 (flags & CLIP_EXCLUDE) ? RGN_DIFF : RGN_AND );
224 if (!(flags & CLIP_KEEPRGN)) DeleteObject32( dc->w.hClipRgn );
225 dc->w.hClipRgn = newRgn;
226 CLIPPING_UpdateGCRegion( dc );
228 else DeleteObject32( newRgn );
233 /***********************************************************************
234 * ExcludeClipRect16 (GDI.21)
236 INT16 WINAPI ExcludeClipRect16( HDC16 hdc, INT16 left, INT16 top,
237 INT16 right, INT16 bottom )
239 return (INT16)ExcludeClipRect32( hdc, left, top, right, bottom );
243 /***********************************************************************
244 * ExcludeClipRect32 (GDI32.92)
246 INT32 WINAPI ExcludeClipRect32( HDC32 hdc, INT32 left, INT32 top,
247 INT32 right, INT32 bottom )
250 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
253 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
254 if (!dc) return ERROR;
255 MF_MetaParam4(dc, META_EXCLUDECLIPRECT, left, top, right, bottom);
256 GDI_HEAP_UNLOCK( hdc );
257 return NULLREGION; /* ?? */
260 left = XLPTODP( dc, left );
261 right = XLPTODP( dc, right );
262 top = YLPTODP( dc, top );
263 bottom = YLPTODP( dc, bottom );
265 TRACE(clipping, "%04x %dx%d,%dx%d\n",
266 hdc, left, top, right, bottom );
267 ret = CLIPPING_IntersectClipRect( dc, left, top, right, bottom, CLIP_EXCLUDE );
268 GDI_HEAP_UNLOCK( hdc );
273 /***********************************************************************
274 * IntersectClipRect16 (GDI.22)
276 INT16 WINAPI IntersectClipRect16( HDC16 hdc, INT16 left, INT16 top,
277 INT16 right, INT16 bottom )
279 return (INT16)IntersectClipRect32( hdc, left, top, right, bottom );
283 /***********************************************************************
284 * IntersectClipRect32 (GDI32.245)
286 INT32 WINAPI IntersectClipRect32( HDC32 hdc, INT32 left, INT32 top,
287 INT32 right, INT32 bottom )
290 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
293 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
294 if (!dc) return ERROR;
295 MF_MetaParam4(dc, META_INTERSECTCLIPRECT, left, top, right, bottom);
296 GDI_HEAP_UNLOCK( hdc );
297 return NULLREGION; /* ?? */
300 left = XLPTODP( dc, left );
301 right = XLPTODP( dc, right );
302 top = YLPTODP( dc, top );
303 bottom = YLPTODP( dc, bottom );
305 TRACE(clipping, "%04x %dx%d,%dx%d\n",
306 hdc, left, top, right, bottom );
307 ret = CLIPPING_IntersectClipRect( dc, left, top, right, bottom, CLIP_INTERSECT );
308 GDI_HEAP_UNLOCK( hdc );
313 /***********************************************************************
314 * CLIPPING_IntersectVisRect
316 * Helper function for {Intersect,Exclude}VisRect, can be called from
317 * elsewhere (like ExtTextOut()) to skip redundant metafile update and
318 * coordinate conversion.
320 INT32 CLIPPING_IntersectVisRect( DC * dc, INT32 left, INT32 top,
321 INT32 right, INT32 bottom,
324 HRGN32 tempRgn, newRgn;
327 left += dc->w.DCOrgX;
328 right += dc->w.DCOrgX;
330 bottom += dc->w.DCOrgY;
332 if (!(newRgn = CreateRectRgn32( 0, 0, 0, 0 ))) return ERROR;
333 if (!(tempRgn = CreateRectRgn32( left, top, right, bottom )))
335 DeleteObject32( newRgn );
338 ret = CombineRgn32( newRgn, dc->w.hVisRgn, tempRgn,
339 exclude ? RGN_DIFF : RGN_AND );
340 DeleteObject32( tempRgn );
344 RGNOBJ *newObj = (RGNOBJ*)GDI_GetObjPtr( newRgn, REGION_MAGIC);
345 RGNOBJ *prevObj = (RGNOBJ*)GDI_GetObjPtr( dc->w.hVisRgn, REGION_MAGIC);
346 if (newObj && prevObj) newObj->header.hNext = prevObj->header.hNext;
347 DeleteObject32( dc->w.hVisRgn );
348 dc->w.hVisRgn = newRgn;
349 CLIPPING_UpdateGCRegion( dc );
350 GDI_HEAP_UNLOCK( newRgn );
352 else DeleteObject32( newRgn );
357 /***********************************************************************
358 * ExcludeVisRect (GDI.73)
360 INT16 WINAPI ExcludeVisRect( HDC16 hdc, INT16 left, INT16 top,
361 INT16 right, INT16 bottom )
363 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
364 if (!dc) return ERROR;
366 left = XLPTODP( dc, left );
367 right = XLPTODP( dc, right );
368 top = YLPTODP( dc, top );
369 bottom = YLPTODP( dc, bottom );
371 TRACE(clipping, "%04x %dx%d,%dx%d\n",
372 hdc, left, top, right, bottom );
374 return CLIPPING_IntersectVisRect( dc, left, top, right, bottom, TRUE );
378 /***********************************************************************
379 * IntersectVisRect (GDI.98)
381 INT16 WINAPI IntersectVisRect( HDC16 hdc, INT16 left, INT16 top,
382 INT16 right, INT16 bottom )
384 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
385 if (!dc) return ERROR;
387 left = XLPTODP( dc, left );
388 right = XLPTODP( dc, right );
389 top = YLPTODP( dc, top );
390 bottom = YLPTODP( dc, bottom );
392 TRACE(clipping, "%04x %dx%d,%dx%d\n",
393 hdc, left, top, right, bottom );
395 return CLIPPING_IntersectVisRect( dc, left, top, right, bottom, FALSE );
399 /***********************************************************************
400 * PtVisible16 (GDI.103)
402 BOOL16 WINAPI PtVisible16( HDC16 hdc, INT16 x, INT16 y )
404 return PtVisible32( hdc, x, y );
408 /***********************************************************************
409 * PtVisible32 (GDI32.279)
411 BOOL32 WINAPI PtVisible32( HDC32 hdc, INT32 x, INT32 y )
413 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
414 if (!dc) return ERROR;
416 TRACE(clipping, "%04x %d,%d\n", hdc, x, y );
417 if (!dc->w.hGCClipRgn) return FALSE;
419 if( dc->w.flags & DC_DIRTY ) UPDATE_DIRTY_DC(dc);
420 dc->w.flags &= ~DC_DIRTY;
422 return PtInRegion32( dc->w.hGCClipRgn, XLPTODP(dc,x) + dc->w.DCOrgX,
423 YLPTODP(dc,y) + dc->w.DCOrgY );
427 /***********************************************************************
428 * RectVisible16 (GDI.104)
430 BOOL16 WINAPI RectVisible16( HDC16 hdc, const RECT16* rect )
433 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
434 if (!dc) return FALSE;
435 TRACE(clipping,"%04x %d,%dx%d,%d\n",
436 hdc, rect->left, rect->top, rect->right, rect->bottom );
437 if (!dc->w.hGCClipRgn) return FALSE;
438 /* copy rectangle to avoid overwriting by LPtoDP */
440 LPtoDP16( hdc, (LPPOINT16)&tmpRect, 2 );
441 OffsetRect16( &tmpRect, dc->w.DCOrgX, dc->w.DCOrgY );
442 return RectInRegion16( dc->w.hGCClipRgn, &tmpRect );
446 /***********************************************************************
447 * RectVisible32 (GDI32.282)
449 BOOL32 WINAPI RectVisible32( HDC32 hdc, const RECT32* rect )
452 CONV_RECT32TO16( rect, &rect16 );
453 return RectVisible16( (HDC16)hdc, &rect16 );
457 /***********************************************************************
458 * GetClipBox16 (GDI.77)
460 INT16 WINAPI GetClipBox16( HDC16 hdc, LPRECT16 rect )
463 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
464 if (!dc) return ERROR;
465 ret = GetRgnBox16( dc->w.hGCClipRgn, rect );
466 OffsetRect16( rect, -dc->w.DCOrgX, -dc->w.DCOrgY );
467 DPtoLP16( hdc, (LPPOINT16)rect, 2 );
472 /***********************************************************************
473 * GetClipBox32 (GDI32.162)
475 INT32 WINAPI GetClipBox32( HDC32 hdc, LPRECT32 rect )
478 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
479 if (!dc) return ERROR;
480 ret = GetRgnBox32( dc->w.hGCClipRgn, rect );
481 OffsetRect32( rect, -dc->w.DCOrgX, -dc->w.DCOrgY );
482 DPtoLP32( hdc, (LPPOINT32)rect, 2 );
487 /***********************************************************************
488 * GetClipRgn32 (GDI32.163)
490 INT32 WINAPI GetClipRgn32( HDC32 hdc, HRGN32 hRgn )
492 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
497 /* this assumes that dc->w.hClipRgn is in coordinates
498 relative to the device (not DC origin) */
500 if( CombineRgn32(hRgn, dc->w.hClipRgn, 0, RGN_COPY) != ERROR )
502 OffsetRgn32( hRgn, -dc->w.DCOrgX, -dc->w.DCOrgY );
511 /***********************************************************************
512 * SaveVisRgn (GDI.129)
514 HRGN16 WINAPI SaveVisRgn( HDC16 hdc )
517 RGNOBJ *obj, *copyObj;
518 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
520 TRACE(clipping, "%04x\n", hdc );
523 ERR(region, "hVisRgn is zero. Please report this.\n" );
526 if( dc->w.flags & DC_DIRTY ) UPDATE_DIRTY_DC(dc);
527 dc->w.flags &= ~DC_DIRTY;
529 if (!(obj = (RGNOBJ *) GDI_GetObjPtr( dc->w.hVisRgn, REGION_MAGIC )))
531 GDI_HEAP_UNLOCK( hdc );
534 if (!(copy = CreateRectRgn32( 0, 0, 0, 0 )))
536 GDI_HEAP_UNLOCK( dc->w.hVisRgn );
537 GDI_HEAP_UNLOCK( hdc );
540 CombineRgn32( copy, dc->w.hVisRgn, 0, RGN_COPY );
541 if (!(copyObj = (RGNOBJ *) GDI_GetObjPtr( copy, REGION_MAGIC )))
543 GDI_HEAP_UNLOCK( dc->w.hVisRgn );
544 GDI_HEAP_UNLOCK( hdc );
547 copyObj->header.hNext = obj->header.hNext;
548 obj->header.hNext = copy;
549 GDI_HEAP_UNLOCK( dc->w.hVisRgn );
550 GDI_HEAP_UNLOCK( hdc );
551 GDI_HEAP_UNLOCK( copy );
556 /***********************************************************************
557 * RestoreVisRgn (GDI.130)
559 INT16 WINAPI RestoreVisRgn( HDC16 hdc )
562 RGNOBJ *obj, *savedObj;
563 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
566 if (!dc) return ERROR;
569 GDI_HEAP_UNLOCK( hdc );
572 TRACE(clipping, "%04x\n", hdc );
573 if (!(obj = (RGNOBJ *) GDI_GetObjPtr( dc->w.hVisRgn, REGION_MAGIC )))
575 GDI_HEAP_UNLOCK( hdc );
578 if (!(saved = obj->header.hNext))
580 GDI_HEAP_UNLOCK( dc->w.hVisRgn );
581 GDI_HEAP_UNLOCK( hdc );
584 if (!(savedObj = (RGNOBJ *) GDI_GetObjPtr( saved, REGION_MAGIC )))
586 GDI_HEAP_UNLOCK( dc->w.hVisRgn );
587 GDI_HEAP_UNLOCK( hdc );
590 DeleteObject32( dc->w.hVisRgn );
591 dc->w.hVisRgn = saved;
592 CLIPPING_UpdateGCRegion( dc );
593 GDI_HEAP_UNLOCK( hdc );
594 ret = savedObj->rgn->type; /* FIXME */
595 GDI_HEAP_UNLOCK( saved );