Bug fixes (thanks to Juergen Schmied).
[wine/multimedia.git] / objects / clipping.c
blob72affae25ee14405d7a913744853c0678472543e
1 /*
2 * DC clipping functions
4 * Copyright 1993 Alexandre Julliard
5 */
7 #include <stdlib.h>
8 #include "dc.h"
9 #include "region.h"
10 #include "debugtools.h"
11 #include "wine/winuser16.h"
13 DECLARE_DEBUG_CHANNEL(clipping)
14 DECLARE_DEBUG_CHANNEL(region)
16 #define UPDATE_DIRTY_DC(dc) \
17 do { \
18 if ((dc)->hookThunk && !((dc)->w.flags & (DC_SAVED | DC_MEMORY))) \
19 (dc)->hookThunk( (dc)->hSelf, DCHC_INVALIDVISRGN, (dc)->dwHookData, 0 ); \
20 } while(0)
24 /***********************************************************************
25 * CLIPPING_UpdateGCRegion
27 * Update the GC clip region when the ClipRgn or VisRgn have changed.
29 void CLIPPING_UpdateGCRegion( DC * dc )
31 if (!dc->w.hGCClipRgn) dc->w.hGCClipRgn = CreateRectRgn( 0, 0, 0, 0 );
33 if (!dc->w.hVisRgn)
35 ERR_(region)("hVisRgn is zero. Please report this.\n" );
36 exit(1);
39 if (dc->w.flags & DC_DIRTY)
41 UPDATE_DIRTY_DC(dc);
42 dc->w.flags &= ~DC_DIRTY;
45 if (!dc->w.hClipRgn)
46 CombineRgn( dc->w.hGCClipRgn, dc->w.hVisRgn, 0, RGN_COPY );
47 else
48 CombineRgn(dc->w.hGCClipRgn, dc->w.hClipRgn, dc->w.hVisRgn, RGN_AND);
49 if (dc->funcs->pSetDeviceClipping) dc->funcs->pSetDeviceClipping( dc );
53 /***********************************************************************
54 * SelectClipRgn16 (GDI.44)
56 INT16 WINAPI SelectClipRgn16( HDC16 hdc, HRGN16 hrgn )
58 return (INT16)SelectClipRgn( hdc, hrgn );
62 /***********************************************************************
63 * SelectClipRgn32 (GDI32.297)
65 INT WINAPI SelectClipRgn( HDC hdc, HRGN hrgn )
67 return ExtSelectClipRgn( hdc, hrgn, RGN_COPY );
70 /******************************************************************************
71 * ExtSelectClipRgn16 [GDI.508]
73 INT16 WINAPI ExtSelectClipRgn16( HDC16 hdc, HRGN16 hrgn, INT16 fnMode )
75 return (INT16) ExtSelectClipRgn((HDC) hdc, (HRGN) hrgn, fnMode);
78 /******************************************************************************
79 * ExtSelectClipRgn [GDI32.97]
81 INT WINAPI ExtSelectClipRgn( HDC hdc, HRGN hrgn, INT fnMode )
83 INT retval;
84 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
85 if (!dc) return ERROR;
87 TRACE_(clipping)("%04x %04x %d\n", hdc, hrgn, fnMode );
89 if (!hrgn)
91 if (fnMode == RGN_COPY)
93 if (dc->w.hClipRgn) DeleteObject16( dc->w.hClipRgn );
94 dc->w.hClipRgn = 0;
95 retval = SIMPLEREGION; /* Clip region == whole DC */
97 else
99 FIXME_(clipping)("Unimplemented: hrgn NULL in mode: %d\n", fnMode);
100 return ERROR;
103 else
105 if (!dc->w.hClipRgn)
107 RECT rect;
108 GetRgnBox( dc->w.hVisRgn, &rect );
109 dc->w.hClipRgn = CreateRectRgnIndirect( &rect );
112 OffsetRgn( dc->w.hClipRgn, -dc->w.DCOrgX, -dc->w.DCOrgY );
113 if(fnMode == RGN_COPY)
114 retval = CombineRgn( dc->w.hClipRgn, hrgn, 0, fnMode );
115 else
116 retval = CombineRgn( dc->w.hClipRgn, dc->w.hClipRgn, hrgn, fnMode);
117 OffsetRgn( dc->w.hClipRgn, dc->w.DCOrgX, dc->w.DCOrgY );
121 CLIPPING_UpdateGCRegion( dc );
122 GDI_HEAP_UNLOCK( hdc );
123 return retval;
126 /***********************************************************************
127 * SelectVisRgn (GDI.105)
129 INT16 WINAPI SelectVisRgn16( HDC16 hdc, HRGN16 hrgn )
131 int retval;
132 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
133 if (!dc || !hrgn) return ERROR;
135 TRACE_(clipping)("%04x %04x\n", hdc, hrgn );
137 dc->w.flags &= ~DC_DIRTY;
139 retval = CombineRgn16( dc->w.hVisRgn, hrgn, 0, RGN_COPY );
140 CLIPPING_UpdateGCRegion( dc );
141 GDI_HEAP_UNLOCK( hdc );
142 return retval;
146 /***********************************************************************
147 * OffsetClipRgn16 (GDI.32)
149 INT16 WINAPI OffsetClipRgn16( HDC16 hdc, INT16 x, INT16 y )
151 return (INT16)OffsetClipRgn( hdc, x, y );
155 /***********************************************************************
156 * OffsetClipRgn32 (GDI32.255)
158 INT WINAPI OffsetClipRgn( HDC hdc, INT x, INT y )
160 INT ret = SIMPLEREGION;
161 DC *dc = DC_GetDCPtr( hdc );
162 if (!dc) return ERROR;
164 TRACE_(clipping)("%04x %d,%d\n", hdc, x, y );
166 if(dc->funcs->pOffsetClipRgn)
167 ret = dc->funcs->pOffsetClipRgn( dc, x, y );
168 else if (dc->w.hClipRgn) {
169 ret = OffsetRgn( dc->w.hClipRgn, XLSTODS(dc,x), YLSTODS(dc,y));
170 CLIPPING_UpdateGCRegion( dc );
172 GDI_HEAP_UNLOCK( hdc );
173 return ret;
177 /***********************************************************************
178 * OffsetVisRgn (GDI.102)
180 INT16 WINAPI OffsetVisRgn16( HDC16 hdc, INT16 x, INT16 y )
182 INT16 retval;
183 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
184 if (!dc) return ERROR;
185 TRACE_(clipping)("%04x %d,%d\n", hdc, x, y );
186 retval = OffsetRgn( dc->w.hVisRgn, x, y );
187 CLIPPING_UpdateGCRegion( dc );
188 GDI_HEAP_UNLOCK( hdc );
189 return retval;
193 /***********************************************************************
194 * CLIPPING_IntersectClipRect
196 * Helper function for {Intersect,Exclude}ClipRect, can be called from
197 * elsewhere (like ExtTextOut()) to skip redundant metafile update and
198 * coordinate conversion.
200 INT CLIPPING_IntersectClipRect( DC * dc, INT left, INT top,
201 INT right, INT bottom, UINT flags )
203 HRGN newRgn;
204 INT ret;
206 left += dc->w.DCOrgX;
207 right += dc->w.DCOrgX;
208 top += dc->w.DCOrgY;
209 bottom += dc->w.DCOrgY;
211 if (!(newRgn = CreateRectRgn( left, top, right, bottom ))) return ERROR;
212 if (!dc->w.hClipRgn)
214 if( flags & CLIP_INTERSECT )
216 dc->w.hClipRgn = newRgn;
217 CLIPPING_UpdateGCRegion( dc );
218 return SIMPLEREGION;
220 else if( flags & CLIP_EXCLUDE )
222 dc->w.hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
223 CombineRgn( dc->w.hClipRgn, dc->w.hVisRgn, 0, RGN_COPY );
225 else WARN_(clipping)("No hClipRgn and flags are %x\n",flags);
228 ret = CombineRgn( newRgn, dc->w.hClipRgn, newRgn,
229 (flags & CLIP_EXCLUDE) ? RGN_DIFF : RGN_AND );
230 if (ret != ERROR)
232 if (!(flags & CLIP_KEEPRGN)) DeleteObject( dc->w.hClipRgn );
233 dc->w.hClipRgn = newRgn;
234 CLIPPING_UpdateGCRegion( dc );
236 else DeleteObject( newRgn );
237 return ret;
241 /***********************************************************************
242 * ExcludeClipRect16 (GDI.21)
244 INT16 WINAPI ExcludeClipRect16( HDC16 hdc, INT16 left, INT16 top,
245 INT16 right, INT16 bottom )
247 return (INT16)ExcludeClipRect( hdc, left, top, right, bottom );
251 /***********************************************************************
252 * ExcludeClipRect32 (GDI32.92)
254 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
255 INT right, INT bottom )
257 INT ret;
258 DC *dc = DC_GetDCPtr( hdc );
259 if (!dc) return ERROR;
261 TRACE_(clipping)("%04x %dx%d,%dx%d\n",
262 hdc, left, top, right, bottom );
264 if(dc->funcs->pExcludeClipRect)
265 ret = dc->funcs->pExcludeClipRect( dc, left, top, right, bottom );
266 else {
267 left = XLPTODP( dc, left );
268 right = XLPTODP( dc, right );
269 top = YLPTODP( dc, top );
270 bottom = YLPTODP( dc, bottom );
272 ret = CLIPPING_IntersectClipRect( dc, left, top, right, bottom, CLIP_EXCLUDE );
274 GDI_HEAP_UNLOCK( hdc );
275 return ret;
279 /***********************************************************************
280 * IntersectClipRect16 (GDI.22)
282 INT16 WINAPI IntersectClipRect16( HDC16 hdc, INT16 left, INT16 top,
283 INT16 right, INT16 bottom )
285 return (INT16)IntersectClipRect( hdc, left, top, right, bottom );
289 /***********************************************************************
290 * IntersectClipRect32 (GDI32.245)
292 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top,
293 INT right, INT bottom )
295 INT ret;
296 DC *dc = DC_GetDCPtr( hdc );
297 if (!dc) return ERROR;
299 TRACE_(clipping)("%04x %dx%d,%dx%d\n",
300 hdc, left, top, right, bottom );
302 if(dc->funcs->pIntersectClipRect)
303 ret = dc->funcs->pIntersectClipRect( dc, left, top, right, bottom );
304 else {
305 left = XLPTODP( dc, left );
306 right = XLPTODP( dc, right );
307 top = YLPTODP( dc, top );
308 bottom = YLPTODP( dc, bottom );
310 ret = CLIPPING_IntersectClipRect( dc, left, top, right, bottom, CLIP_INTERSECT );
312 GDI_HEAP_UNLOCK( hdc );
313 return ret;
317 /***********************************************************************
318 * CLIPPING_IntersectVisRect
320 * Helper function for {Intersect,Exclude}VisRect, can be called from
321 * elsewhere (like ExtTextOut()) to skip redundant metafile update and
322 * coordinate conversion.
324 INT CLIPPING_IntersectVisRect( DC * dc, INT left, INT top,
325 INT right, INT bottom,
326 BOOL exclude )
328 HRGN tempRgn, newRgn;
329 INT ret;
331 left += dc->w.DCOrgX;
332 right += dc->w.DCOrgX;
333 top += dc->w.DCOrgY;
334 bottom += dc->w.DCOrgY;
336 if (!(newRgn = CreateRectRgn( 0, 0, 0, 0 ))) return ERROR;
337 if (!(tempRgn = CreateRectRgn( left, top, right, bottom )))
339 DeleteObject( newRgn );
340 return ERROR;
342 ret = CombineRgn( newRgn, dc->w.hVisRgn, tempRgn,
343 exclude ? RGN_DIFF : RGN_AND );
344 DeleteObject( tempRgn );
346 if (ret != ERROR)
348 RGNOBJ *newObj = (RGNOBJ*)GDI_GetObjPtr( newRgn, REGION_MAGIC);
349 RGNOBJ *prevObj = (RGNOBJ*)GDI_GetObjPtr( dc->w.hVisRgn, REGION_MAGIC);
350 if (newObj && prevObj) newObj->header.hNext = prevObj->header.hNext;
351 DeleteObject( dc->w.hVisRgn );
352 dc->w.hVisRgn = newRgn;
353 CLIPPING_UpdateGCRegion( dc );
354 GDI_HEAP_UNLOCK( newRgn );
356 else DeleteObject( newRgn );
357 return ret;
361 /***********************************************************************
362 * ExcludeVisRect (GDI.73)
364 INT16 WINAPI ExcludeVisRect16( HDC16 hdc, INT16 left, INT16 top,
365 INT16 right, INT16 bottom )
367 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
368 if (!dc) return ERROR;
370 left = XLPTODP( dc, left );
371 right = XLPTODP( dc, right );
372 top = YLPTODP( dc, top );
373 bottom = YLPTODP( dc, bottom );
375 TRACE_(clipping)("%04x %dx%d,%dx%d\n",
376 hdc, left, top, right, bottom );
378 return CLIPPING_IntersectVisRect( dc, left, top, right, bottom, TRUE );
382 /***********************************************************************
383 * IntersectVisRect (GDI.98)
385 INT16 WINAPI IntersectVisRect16( HDC16 hdc, INT16 left, INT16 top,
386 INT16 right, INT16 bottom )
388 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
389 if (!dc) return ERROR;
391 left = XLPTODP( dc, left );
392 right = XLPTODP( dc, right );
393 top = YLPTODP( dc, top );
394 bottom = YLPTODP( dc, bottom );
396 TRACE_(clipping)("%04x %dx%d,%dx%d\n",
397 hdc, left, top, right, bottom );
399 return CLIPPING_IntersectVisRect( dc, left, top, right, bottom, FALSE );
403 /***********************************************************************
404 * PtVisible16 (GDI.103)
406 BOOL16 WINAPI PtVisible16( HDC16 hdc, INT16 x, INT16 y )
408 return PtVisible( hdc, x, y );
412 /***********************************************************************
413 * PtVisible32 (GDI32.279)
415 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
417 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
418 if (!dc) return ERROR;
420 TRACE_(clipping)("%04x %d,%d\n", hdc, x, y );
421 if (!dc->w.hGCClipRgn) return FALSE;
423 if( dc->w.flags & DC_DIRTY ) UPDATE_DIRTY_DC(dc);
424 dc->w.flags &= ~DC_DIRTY;
426 return PtInRegion( dc->w.hGCClipRgn, XLPTODP(dc,x) + dc->w.DCOrgX,
427 YLPTODP(dc,y) + dc->w.DCOrgY );
431 /***********************************************************************
432 * RectVisible16 (GDI.104)
434 BOOL16 WINAPI RectVisible16( HDC16 hdc, const RECT16* rect )
436 RECT16 tmpRect;
437 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
438 if (!dc) return FALSE;
439 TRACE_(clipping)("%04x %d,%dx%d,%d\n",
440 hdc, rect->left, rect->top, rect->right, rect->bottom );
441 if (!dc->w.hGCClipRgn) return FALSE;
442 /* copy rectangle to avoid overwriting by LPtoDP */
443 tmpRect = *rect;
444 LPtoDP16( hdc, (LPPOINT16)&tmpRect, 2 );
445 OffsetRect16( &tmpRect, dc->w.DCOrgX, dc->w.DCOrgY );
446 return RectInRegion16( dc->w.hGCClipRgn, &tmpRect );
450 /***********************************************************************
451 * RectVisible32 (GDI32.282)
453 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
455 RECT16 rect16;
456 CONV_RECT32TO16( rect, &rect16 );
457 return RectVisible16( (HDC16)hdc, &rect16 );
461 /***********************************************************************
462 * GetClipBox16 (GDI.77)
464 INT16 WINAPI GetClipBox16( HDC16 hdc, LPRECT16 rect )
466 int ret;
467 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
468 if (!dc) return ERROR;
469 ret = GetRgnBox16( dc->w.hGCClipRgn, rect );
470 OffsetRect16( rect, -dc->w.DCOrgX, -dc->w.DCOrgY );
471 DPtoLP16( hdc, (LPPOINT16)rect, 2 );
472 TRACE_(clipping)("%d,%d-%d,%d\n",
473 rect->left,rect->top,rect->right,rect->bottom );
474 return ret;
478 /***********************************************************************
479 * GetClipBox32 (GDI32.162)
481 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
483 INT ret;
484 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
485 if (!dc) return ERROR;
486 ret = GetRgnBox( dc->w.hGCClipRgn, rect );
487 OffsetRect( rect, -dc->w.DCOrgX, -dc->w.DCOrgY );
488 DPtoLP( hdc, (LPPOINT)rect, 2 );
489 return ret;
493 /***********************************************************************
494 * GetClipRgn32 (GDI32.163)
496 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
498 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
499 if( dc && hRgn )
501 if( dc->w.hClipRgn )
503 /* this assumes that dc->w.hClipRgn is in coordinates
504 relative to the device (not DC origin) */
506 if( CombineRgn(hRgn, dc->w.hClipRgn, 0, RGN_COPY) != ERROR )
508 OffsetRgn( hRgn, -dc->w.DCOrgX, -dc->w.DCOrgY );
509 return 1;
512 else return 0;
514 return -1;
517 /***********************************************************************
518 * SaveVisRgn (GDI.129)
520 HRGN16 WINAPI SaveVisRgn16( HDC16 hdc )
522 HRGN copy;
523 RGNOBJ *obj, *copyObj;
524 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
525 if (!dc) return 0;
526 TRACE_(clipping)("%04x\n", hdc );
527 if (!dc->w.hVisRgn)
529 ERR_(region)("hVisRgn is zero. Please report this.\n" );
530 exit(1);
532 if( dc->w.flags & DC_DIRTY ) UPDATE_DIRTY_DC(dc);
533 dc->w.flags &= ~DC_DIRTY;
535 if (!(obj = (RGNOBJ *) GDI_GetObjPtr( dc->w.hVisRgn, REGION_MAGIC )))
537 GDI_HEAP_UNLOCK( hdc );
538 return 0;
540 if (!(copy = CreateRectRgn( 0, 0, 0, 0 )))
542 GDI_HEAP_UNLOCK( dc->w.hVisRgn );
543 GDI_HEAP_UNLOCK( hdc );
544 return 0;
546 CombineRgn( copy, dc->w.hVisRgn, 0, RGN_COPY );
547 if (!(copyObj = (RGNOBJ *) GDI_GetObjPtr( copy, REGION_MAGIC )))
549 GDI_HEAP_UNLOCK( dc->w.hVisRgn );
550 GDI_HEAP_UNLOCK( hdc );
551 return 0;
553 copyObj->header.hNext = obj->header.hNext;
554 obj->header.hNext = copy;
555 GDI_HEAP_UNLOCK( dc->w.hVisRgn );
556 GDI_HEAP_UNLOCK( hdc );
557 GDI_HEAP_UNLOCK( copy );
558 return copy;
562 /***********************************************************************
563 * RestoreVisRgn (GDI.130)
565 INT16 WINAPI RestoreVisRgn16( HDC16 hdc )
567 HRGN saved;
568 RGNOBJ *obj, *savedObj;
569 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
570 INT16 ret;
572 if (!dc) return ERROR;
573 if (!dc->w.hVisRgn)
575 GDI_HEAP_UNLOCK( hdc );
576 return ERROR;
578 TRACE_(clipping)("%04x\n", hdc );
579 if (!(obj = (RGNOBJ *) GDI_GetObjPtr( dc->w.hVisRgn, REGION_MAGIC )))
581 GDI_HEAP_UNLOCK( hdc );
582 return ERROR;
584 if (!(saved = obj->header.hNext))
586 GDI_HEAP_UNLOCK( dc->w.hVisRgn );
587 GDI_HEAP_UNLOCK( hdc );
588 return ERROR;
590 if (!(savedObj = (RGNOBJ *) GDI_GetObjPtr( saved, REGION_MAGIC )))
592 GDI_HEAP_UNLOCK( dc->w.hVisRgn );
593 GDI_HEAP_UNLOCK( hdc );
594 return ERROR;
596 DeleteObject( dc->w.hVisRgn );
597 dc->w.hVisRgn = saved;
598 CLIPPING_UpdateGCRegion( dc );
599 GDI_HEAP_UNLOCK( hdc );
600 ret = savedObj->rgn->type; /* FIXME */
601 GDI_HEAP_UNLOCK( saved );
602 return ret;