Release 981025.
[wine/gsoc-2012-control.git] / objects / clipping.c
blob1d874101d99aa461d6ab191c9bf6eface5431403
1 /*
2 * DC clipping functions
4 * Copyright 1993 Alexandre Julliard
5 */
7 #include "dc.h"
8 #include "metafile.h"
9 #include "region.h"
10 #include "debug.h"
12 #define UPDATE_DIRTY_DC(dc) \
13 do { \
14 if ((dc)->hookProc && !((dc)->w.flags & (DC_SAVED | DC_MEMORY))) \
15 (dc)->hookProc( (dc)->hSelf, DCHC_INVALIDVISRGN, (dc)->dwHookData, 0 ); \
16 } while(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 );
29 if (!dc->w.hVisRgn)
31 ERR(region, "hVisRgn is zero. Please report this.\n" );
32 exit(1);
35 if (dc->w.flags & DC_DIRTY)
37 UPDATE_DIRTY_DC(dc);
38 dc->w.flags &= ~DC_DIRTY;
41 if (!dc->w.hClipRgn)
42 CombineRgn32( dc->w.hGCClipRgn, dc->w.hVisRgn, 0, RGN_COPY );
43 else
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 )
71 INT32 retval;
72 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
73 if (!dc) return ERROR;
75 TRACE( clipping, "%04x %04x %d\n", hdc, hrgn, fnMode );
77 if (!hrgn)
79 if (fnMode == RGN_COPY)
81 if (dc->w.hClipRgn) DeleteObject16( dc->w.hClipRgn );
82 dc->w.hClipRgn = 0;
83 retval = SIMPLEREGION; /* Clip region == whole DC */
85 else
87 FIXME(clipping, "Unimplemented: hrgn NULL in mode: %d\n", fnMode);
88 return ERROR;
91 else
93 if (!dc->w.hClipRgn)
95 RECT32 rect;
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 );
108 return retval;
111 /***********************************************************************
112 * SelectVisRgn (GDI.105)
114 INT16 WINAPI SelectVisRgn( HDC16 hdc, HRGN16 hrgn )
116 int retval;
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 );
127 return retval;
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 );
146 if (!dc)
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 );
157 if (dc->w.hClipRgn)
159 INT32 ret = OffsetRgn32( dc->w.hClipRgn, XLPTODP(dc,x), YLPTODP(dc,y));
160 CLIPPING_UpdateGCRegion( dc );
161 GDI_HEAP_UNLOCK( hdc );
162 return ret;
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 )
174 INT16 retval;
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 );
181 return retval;
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 )
195 HRGN32 newRgn;
196 INT32 ret;
198 left += dc->w.DCOrgX;
199 right += dc->w.DCOrgX;
200 top += dc->w.DCOrgY;
201 bottom += dc->w.DCOrgY;
203 if (!(newRgn = CreateRectRgn32( left, top, right, bottom ))) return ERROR;
204 if (!dc->w.hClipRgn)
206 if( flags & CLIP_INTERSECT )
208 dc->w.hClipRgn = newRgn;
209 CLIPPING_UpdateGCRegion( dc );
210 return SIMPLEREGION;
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 );
222 if (ret != ERROR)
224 if (!(flags & CLIP_KEEPRGN)) DeleteObject32( dc->w.hClipRgn );
225 dc->w.hClipRgn = newRgn;
226 CLIPPING_UpdateGCRegion( dc );
228 else DeleteObject32( newRgn );
229 return ret;
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 )
249 INT32 ret;
250 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
251 if (!dc)
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 );
269 return ret;
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 )
289 INT32 ret;
290 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
291 if (!dc)
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 );
309 return ret;
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,
322 BOOL32 exclude )
324 HRGN32 tempRgn, newRgn;
325 INT32 ret;
327 left += dc->w.DCOrgX;
328 right += dc->w.DCOrgX;
329 top += dc->w.DCOrgY;
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 );
336 return ERROR;
338 ret = CombineRgn32( newRgn, dc->w.hVisRgn, tempRgn,
339 exclude ? RGN_DIFF : RGN_AND );
340 DeleteObject32( tempRgn );
342 if (ret != ERROR)
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 );
353 return ret;
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, LPRECT16 rect )
432 RECT16 tmpRect;
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 */
439 tmpRect = *rect;
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, LPRECT32 rect )
451 RECT16 rect16;
452 CONV_RECT32TO16( rect, &rect16 );
453 return RectVisible16( (HDC16)hdc, &rect16 );
457 /***********************************************************************
458 * GetClipBox16 (GDI.77)
460 INT16 WINAPI GetClipBox16( HDC16 hdc, LPRECT16 rect )
462 int ret;
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 );
468 return ret;
472 /***********************************************************************
473 * GetClipBox32 (GDI32.162)
475 INT32 WINAPI GetClipBox32( HDC32 hdc, LPRECT32 rect )
477 INT32 ret;
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 );
483 return ret;
487 /***********************************************************************
488 * GetClipRgn32 (GDI32.163)
490 INT32 WINAPI GetClipRgn32( HDC32 hdc, HRGN32 hRgn )
492 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
493 if( dc && hRgn )
494 if( dc->w.hClipRgn )
496 /* this assumes that dc->w.hClipRgn is in coordinates
497 relative to the device (not DC origin) */
499 if( CombineRgn32(hRgn, dc->w.hClipRgn, 0, RGN_COPY) != ERROR )
501 OffsetRgn32( hRgn, -dc->w.DCOrgX, -dc->w.DCOrgY );
502 return 1;
505 else return 0;
506 return -1;
509 /***********************************************************************
510 * SaveVisRgn (GDI.129)
512 HRGN16 WINAPI SaveVisRgn( HDC16 hdc )
514 HRGN32 copy;
515 RGNOBJ *obj, *copyObj;
516 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
517 if (!dc) return 0;
518 TRACE(clipping, "%04x\n", hdc );
519 if (!dc->w.hVisRgn)
521 ERR(region, "hVisRgn is zero. Please report this.\n" );
522 exit(1);
524 if( dc->w.flags & DC_DIRTY ) UPDATE_DIRTY_DC(dc);
525 dc->w.flags &= ~DC_DIRTY;
527 if (!(obj = (RGNOBJ *) GDI_GetObjPtr( dc->w.hVisRgn, REGION_MAGIC )))
529 GDI_HEAP_UNLOCK( hdc );
530 return 0;
532 if (!(copy = CreateRectRgn32( 0, 0, 0, 0 )))
534 GDI_HEAP_UNLOCK( dc->w.hVisRgn );
535 GDI_HEAP_UNLOCK( hdc );
536 return 0;
538 CombineRgn32( copy, dc->w.hVisRgn, 0, RGN_COPY );
539 if (!(copyObj = (RGNOBJ *) GDI_GetObjPtr( copy, REGION_MAGIC )))
541 GDI_HEAP_UNLOCK( dc->w.hVisRgn );
542 GDI_HEAP_UNLOCK( hdc );
543 return 0;
545 copyObj->header.hNext = obj->header.hNext;
546 obj->header.hNext = copy;
547 GDI_HEAP_UNLOCK( dc->w.hVisRgn );
548 GDI_HEAP_UNLOCK( hdc );
549 GDI_HEAP_UNLOCK( copy );
550 return copy;
554 /***********************************************************************
555 * RestoreVisRgn (GDI.130)
557 INT16 WINAPI RestoreVisRgn( HDC16 hdc )
559 HRGN32 saved;
560 RGNOBJ *obj, *savedObj;
561 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
562 INT16 ret;
564 if (!dc) return ERROR;
565 if (!dc->w.hVisRgn)
567 GDI_HEAP_UNLOCK( hdc );
568 return ERROR;
570 TRACE(clipping, "%04x\n", hdc );
571 if (!(obj = (RGNOBJ *) GDI_GetObjPtr( dc->w.hVisRgn, REGION_MAGIC )))
573 GDI_HEAP_UNLOCK( hdc );
574 return ERROR;
576 if (!(saved = obj->header.hNext))
578 GDI_HEAP_UNLOCK( dc->w.hVisRgn );
579 GDI_HEAP_UNLOCK( hdc );
580 return ERROR;
582 if (!(savedObj = (RGNOBJ *) GDI_GetObjPtr( saved, REGION_MAGIC )))
584 GDI_HEAP_UNLOCK( dc->w.hVisRgn );
585 GDI_HEAP_UNLOCK( hdc );
586 return ERROR;
588 DeleteObject32( dc->w.hVisRgn );
589 dc->w.hVisRgn = saved;
590 CLIPPING_UpdateGCRegion( dc );
591 GDI_HEAP_UNLOCK( hdc );
592 ret = savedObj->rgn->type; /* FIXME */
593 GDI_HEAP_UNLOCK( saved );
594 return ret;