Merged the two serializer and unserializer functions into one, cleaned
[wine.git] / objects / clipping.c
blobd9af64c40e2e59f596505d16cb2d6dc9fdead83e
1 /*
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
21 #include <stdlib.h>
22 #include "windef.h"
23 #include "wingdi.h"
24 #include "wine/winuser16.h"
25 #include "gdi.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(clipping);
31 /***********************************************************************
32 * CLIPPING_UpdateGCRegion
34 * Update the GC clip region when the ClipRgn or VisRgn have changed.
36 void CLIPPING_UpdateGCRegion( DC * dc )
38 if (!dc->hGCClipRgn) dc->hGCClipRgn = CreateRectRgn( 0, 0, 0, 0 );
40 if (!dc->hVisRgn)
42 ERR("hVisRgn is zero. Please report this.\n" );
43 exit(1);
46 if (dc->flags & DC_DIRTY) ERR( "DC is dirty. Please report this.\n" );
48 if (!dc->hClipRgn)
49 CombineRgn( dc->hGCClipRgn, dc->hVisRgn, 0, RGN_COPY );
50 else
51 CombineRgn(dc->hGCClipRgn, dc->hClipRgn, dc->hVisRgn, RGN_AND);
52 if (dc->funcs->pSetDeviceClipping)
53 dc->funcs->pSetDeviceClipping( dc->physDev, dc->hGCClipRgn );
57 /***********************************************************************
58 * SelectClipRgn (GDI.44)
60 INT16 WINAPI SelectClipRgn16( HDC16 hdc, HRGN16 hrgn )
62 return (INT16)SelectClipRgn( hdc, hrgn );
66 /***********************************************************************
67 * SelectClipRgn (GDI32.@)
69 INT WINAPI SelectClipRgn( HDC hdc, HRGN hrgn )
71 return ExtSelectClipRgn( hdc, hrgn, RGN_COPY );
74 /******************************************************************************
75 * ExtSelectClipRgn [GDI.508]
77 INT16 WINAPI ExtSelectClipRgn16( HDC16 hdc, HRGN16 hrgn, INT16 fnMode )
79 return (INT16) ExtSelectClipRgn((HDC) hdc, (HRGN) hrgn, fnMode);
82 /******************************************************************************
83 * ExtSelectClipRgn [GDI32.@]
85 INT WINAPI ExtSelectClipRgn( HDC hdc, HRGN hrgn, INT fnMode )
87 INT retval;
88 DC * dc = DC_GetDCUpdate( hdc );
89 if (!dc) return ERROR;
91 TRACE("%04x %04x %d\n", hdc, hrgn, fnMode );
93 if (dc->funcs->pExtSelectClipRgn)
95 retval = dc->funcs->pExtSelectClipRgn( dc->physDev, hrgn, fnMode );
96 GDI_ReleaseObj( hdc );
97 return retval;
100 if (!hrgn)
102 if (fnMode == RGN_COPY)
104 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
105 dc->hClipRgn = 0;
106 retval = SIMPLEREGION; /* Clip region == whole DC */
108 else
110 FIXME("Unimplemented: hrgn NULL in mode: %d\n", fnMode);
111 GDI_ReleaseObj( hdc );
112 return ERROR;
115 else
117 if (!dc->hClipRgn)
119 RECT rect;
120 GetRgnBox( dc->hVisRgn, &rect );
121 dc->hClipRgn = CreateRectRgnIndirect( &rect );
124 OffsetRgn( dc->hClipRgn, -dc->DCOrgX, -dc->DCOrgY );
125 if(fnMode == RGN_COPY)
126 retval = CombineRgn( dc->hClipRgn, hrgn, 0, fnMode );
127 else
128 retval = CombineRgn( dc->hClipRgn, dc->hClipRgn, hrgn, fnMode);
129 OffsetRgn( dc->hClipRgn, dc->DCOrgX, dc->DCOrgY );
132 CLIPPING_UpdateGCRegion( dc );
133 GDI_ReleaseObj( hdc );
134 return retval;
137 /***********************************************************************
138 * SelectVisRgn (GDI.105)
140 INT16 WINAPI SelectVisRgn16( HDC16 hdc, HRGN16 hrgn )
142 int retval;
143 DC * dc;
145 if (!hrgn) return ERROR;
146 if (!(dc = DC_GetDCPtr( hdc ))) return ERROR;
148 TRACE("%04x %04x\n", hdc, hrgn );
150 dc->flags &= ~DC_DIRTY;
152 retval = CombineRgn16( dc->hVisRgn, hrgn, 0, RGN_COPY );
153 CLIPPING_UpdateGCRegion( dc );
154 GDI_ReleaseObj( hdc );
155 return retval;
159 /***********************************************************************
160 * OffsetClipRgn (GDI.32)
162 INT16 WINAPI OffsetClipRgn16( HDC16 hdc, INT16 x, INT16 y )
164 return (INT16)OffsetClipRgn( hdc, x, y );
168 /***********************************************************************
169 * OffsetClipRgn (GDI32.@)
171 INT WINAPI OffsetClipRgn( HDC hdc, INT x, INT y )
173 INT ret = SIMPLEREGION;
174 DC *dc = DC_GetDCUpdate( hdc );
175 if (!dc) return ERROR;
177 TRACE("%04x %d,%d\n", hdc, x, y );
179 if(dc->funcs->pOffsetClipRgn)
180 ret = dc->funcs->pOffsetClipRgn( dc->physDev, x, y );
181 else if (dc->hClipRgn) {
182 ret = OffsetRgn( dc->hClipRgn, XLSTODS(dc,x), YLSTODS(dc,y));
183 CLIPPING_UpdateGCRegion( dc );
185 GDI_ReleaseObj( hdc );
186 return ret;
190 /***********************************************************************
191 * OffsetVisRgn (GDI.102)
193 INT16 WINAPI OffsetVisRgn16( HDC16 hdc, INT16 x, INT16 y )
195 INT16 retval;
196 DC * dc = DC_GetDCUpdate( hdc );
197 if (!dc) return ERROR;
198 TRACE("%04x %d,%d\n", hdc, x, y );
199 retval = OffsetRgn( dc->hVisRgn, x, y );
200 CLIPPING_UpdateGCRegion( dc );
201 GDI_ReleaseObj( hdc );
202 return retval;
206 /***********************************************************************
207 * ExcludeClipRect (GDI.21)
209 INT16 WINAPI ExcludeClipRect16( HDC16 hdc, INT16 left, INT16 top,
210 INT16 right, INT16 bottom )
212 return (INT16)ExcludeClipRect( hdc, left, top, right, bottom );
216 /***********************************************************************
217 * ExcludeClipRect (GDI32.@)
219 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
220 INT right, INT bottom )
222 HRGN newRgn;
223 INT ret;
224 DC *dc = DC_GetDCUpdate( hdc );
225 if (!dc) return ERROR;
227 TRACE("%04x %dx%d,%dx%d\n", hdc, left, top, right, bottom );
229 if(dc->funcs->pExcludeClipRect)
230 ret = dc->funcs->pExcludeClipRect( dc->physDev, left, top, right, bottom );
231 else
233 left = dc->DCOrgX + XLPTODP( dc, left );
234 right = dc->DCOrgX + XLPTODP( dc, right );
235 top = dc->DCOrgY + YLPTODP( dc, top );
236 bottom = dc->DCOrgY + YLPTODP( dc, bottom );
238 if (!(newRgn = CreateRectRgn( left, top, right, bottom ))) ret = ERROR;
239 else
241 if (!dc->hClipRgn)
243 dc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
244 CombineRgn( dc->hClipRgn, dc->hVisRgn, 0, RGN_COPY );
246 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_DIFF );
247 DeleteObject( newRgn );
249 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
251 GDI_ReleaseObj( hdc );
252 return ret;
256 /***********************************************************************
257 * IntersectClipRect (GDI.22)
259 INT16 WINAPI IntersectClipRect16( HDC16 hdc, INT16 left, INT16 top,
260 INT16 right, INT16 bottom )
262 return (INT16)IntersectClipRect( hdc, left, top, right, bottom );
266 /***********************************************************************
267 * IntersectClipRect (GDI32.@)
269 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top,
270 INT right, INT bottom )
272 INT ret;
273 DC *dc = DC_GetDCUpdate( hdc );
274 if (!dc) return ERROR;
276 TRACE("%04x %dx%d,%dx%d\n", hdc, left, top, right, bottom );
278 if(dc->funcs->pIntersectClipRect)
279 ret = dc->funcs->pIntersectClipRect( dc->physDev, left, top, right, bottom );
280 else
282 left = dc->DCOrgX + XLPTODP( dc, left );
283 right = dc->DCOrgX + XLPTODP( dc, right );
284 top = dc->DCOrgY + YLPTODP( dc, top );
285 bottom = dc->DCOrgY + YLPTODP( dc, bottom );
287 if (!dc->hClipRgn)
289 dc->hClipRgn = CreateRectRgn( left, top, right, bottom );
290 ret = SIMPLEREGION;
292 else
294 HRGN newRgn;
296 if (!(newRgn = CreateRectRgn( left, top, right, bottom ))) ret = ERROR;
297 else
299 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_AND );
300 DeleteObject( newRgn );
303 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
305 GDI_ReleaseObj( hdc );
306 return ret;
310 /***********************************************************************
311 * ExcludeVisRect (GDI.73)
313 INT16 WINAPI ExcludeVisRect16( HDC16 hdc, INT16 left, INT16 top,
314 INT16 right, INT16 bottom )
316 HRGN tempRgn;
317 INT16 ret;
318 DC * dc = DC_GetDCUpdate( hdc );
319 if (!dc) return ERROR;
321 left = dc->DCOrgX + XLPTODP( dc, left );
322 right = dc->DCOrgX + XLPTODP( dc, right );
323 top = dc->DCOrgY + YLPTODP( dc, top );
324 bottom = dc->DCOrgY + YLPTODP( dc, bottom );
326 TRACE("%04x %dx%d,%dx%d\n", hdc, left, top, right, bottom );
328 if (!(tempRgn = CreateRectRgn( left, top, right, bottom ))) ret = ERROR;
329 else
331 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_DIFF );
332 DeleteObject( tempRgn );
334 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
335 GDI_ReleaseObj( hdc );
336 return ret;
340 /***********************************************************************
341 * IntersectVisRect (GDI.98)
343 INT16 WINAPI IntersectVisRect16( HDC16 hdc, INT16 left, INT16 top,
344 INT16 right, INT16 bottom )
346 HRGN tempRgn;
347 INT16 ret;
348 DC * dc = DC_GetDCUpdate( hdc );
349 if (!dc) return ERROR;
351 left = dc->DCOrgX + XLPTODP( dc, left );
352 right = dc->DCOrgX + XLPTODP( dc, right );
353 top = dc->DCOrgY + YLPTODP( dc, top );
354 bottom = dc->DCOrgY + YLPTODP( dc, bottom );
356 TRACE("%04x %dx%d,%dx%d\n", hdc, left, top, right, bottom );
358 if (!(tempRgn = CreateRectRgn( left, top, right, bottom ))) ret = ERROR;
359 else
361 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_AND );
362 DeleteObject( tempRgn );
364 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
365 GDI_ReleaseObj( hdc );
366 return ret;
370 /***********************************************************************
371 * PtVisible (GDI.103)
373 BOOL16 WINAPI PtVisible16( HDC16 hdc, INT16 x, INT16 y )
375 return PtVisible( hdc, x, y );
379 /***********************************************************************
380 * PtVisible (GDI32.@)
382 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
384 BOOL ret = FALSE;
385 DC *dc = DC_GetDCUpdate( hdc );
387 TRACE("%04x %d,%d\n", hdc, x, y );
388 if (!dc) return FALSE;
389 if (dc->hGCClipRgn)
391 ret = PtInRegion( dc->hGCClipRgn, XLPTODP(dc,x) + dc->DCOrgX,
392 YLPTODP(dc,y) + dc->DCOrgY );
394 GDI_ReleaseObj( hdc );
395 return ret;
399 /***********************************************************************
400 * RectVisible (GDI.465)
401 * RectVisibleOld (GDI.104)
403 BOOL16 WINAPI RectVisible16( HDC16 hdc, const RECT16* rect16 )
405 RECT rect;
406 CONV_RECT16TO32( rect16, &rect );
407 return RectVisible( hdc, &rect );
411 /***********************************************************************
412 * RectVisible (GDI32.@)
414 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
416 BOOL ret = FALSE;
417 RECT tmpRect;
418 DC *dc = DC_GetDCUpdate( hdc );
419 if (!dc) return FALSE;
420 TRACE("%04x %d,%dx%d,%d\n",
421 hdc, rect->left, rect->top, rect->right, rect->bottom );
422 if (dc->hGCClipRgn)
424 /* copy rectangle to avoid overwriting by LPtoDP */
425 tmpRect = *rect;
426 LPtoDP( hdc, (LPPOINT)&tmpRect, 2 );
427 tmpRect.left += dc->DCOrgX;
428 tmpRect.right += dc->DCOrgX;
429 tmpRect.top += dc->DCOrgY;
430 tmpRect.bottom += dc->DCOrgY;
431 ret = RectInRegion( dc->hGCClipRgn, &tmpRect );
433 GDI_ReleaseObj( hdc );
434 return ret;
438 /***********************************************************************
439 * GetClipBox (GDI.77)
441 INT16 WINAPI GetClipBox16( HDC16 hdc, LPRECT16 rect )
443 int ret;
444 DC *dc = DC_GetDCUpdate( hdc );
445 if (!dc) return ERROR;
446 ret = GetRgnBox16( dc->hGCClipRgn, rect );
447 rect->left -= dc->DCOrgX;
448 rect->right -= dc->DCOrgX;
449 rect->top -= dc->DCOrgY;
450 rect->bottom -= dc->DCOrgY;
451 DPtoLP16( hdc, (LPPOINT16)rect, 2 );
452 TRACE("%d,%d-%d,%d\n", rect->left,rect->top,rect->right,rect->bottom );
453 GDI_ReleaseObj( hdc );
454 return ret;
458 /***********************************************************************
459 * GetClipBox (GDI32.@)
461 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
463 INT ret;
464 DC *dc = DC_GetDCUpdate( hdc );
465 if (!dc) return ERROR;
466 ret = GetRgnBox( dc->hGCClipRgn, rect );
467 rect->left -= dc->DCOrgX;
468 rect->right -= dc->DCOrgX;
469 rect->top -= dc->DCOrgY;
470 rect->bottom -= dc->DCOrgY;
471 DPtoLP( hdc, (LPPOINT)rect, 2 );
472 GDI_ReleaseObj( hdc );
473 return ret;
477 /***********************************************************************
478 * GetClipRgn (GDI32.@)
480 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
482 INT ret = -1;
483 DC * dc;
484 if (hRgn && (dc = DC_GetDCPtr( hdc )))
486 if( dc->hClipRgn )
488 /* this assumes that dc->hClipRgn is in coordinates
489 relative to the device (not DC origin) */
491 if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR )
493 OffsetRgn( hRgn, -dc->DCOrgX, -dc->DCOrgY );
494 ret = 1;
497 else ret = 0;
498 GDI_ReleaseObj( hdc );
500 return ret;
503 /***********************************************************************
504 * SaveVisRgn (GDI.129)
506 HRGN16 WINAPI SaveVisRgn16( HDC16 hdc )
508 HRGN copy;
509 GDIOBJHDR *obj, *copyObj;
510 DC *dc = DC_GetDCUpdate( hdc );
512 if (!dc) return 0;
513 TRACE("%04x\n", hdc );
515 if (!(obj = GDI_GetObjPtr( dc->hVisRgn, REGION_MAGIC )))
517 GDI_ReleaseObj( hdc );
518 return 0;
520 if (!(copy = CreateRectRgn( 0, 0, 0, 0 )))
522 GDI_ReleaseObj( dc->hVisRgn );
523 GDI_ReleaseObj( hdc );
524 return 0;
526 CombineRgn( copy, dc->hVisRgn, 0, RGN_COPY );
527 if (!(copyObj = GDI_GetObjPtr( copy, REGION_MAGIC )))
529 DeleteObject( copy );
530 GDI_ReleaseObj( dc->hVisRgn );
531 GDI_ReleaseObj( hdc );
532 return 0;
534 copyObj->hNext = obj->hNext;
535 obj->hNext = copy;
536 GDI_ReleaseObj( copy );
537 GDI_ReleaseObj( dc->hVisRgn );
538 GDI_ReleaseObj( hdc );
539 return copy;
543 /***********************************************************************
544 * RestoreVisRgn (GDI.130)
546 INT16 WINAPI RestoreVisRgn16( HDC16 hdc )
548 HRGN saved;
549 GDIOBJHDR *obj, *savedObj;
550 DC *dc = DC_GetDCPtr( hdc );
551 INT16 ret = ERROR;
553 if (!dc) return ERROR;
555 TRACE("%04x\n", hdc );
557 if (!(obj = GDI_GetObjPtr( dc->hVisRgn, REGION_MAGIC ))) goto done;
558 saved = obj->hNext;
560 if ((savedObj = GDI_GetObjPtr( saved, REGION_MAGIC )))
562 ret = CombineRgn( dc->hVisRgn, saved, 0, RGN_COPY );
563 obj->hNext = savedObj->hNext;
564 GDI_ReleaseObj( saved );
565 DeleteObject( saved );
566 dc->flags &= ~DC_DIRTY;
567 CLIPPING_UpdateGCRegion( dc );
569 GDI_ReleaseObj( dc->hVisRgn );
570 done:
571 GDI_ReleaseObj( hdc );
572 return ret;