Removed useless #ifdef SIZEOF_LONG_LONG.
[wine/multimedia.git] / objects / clipping.c
blob08382a4b6a714d17a2c20aa4d1949dbef06a289b
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 <stdarg.h>
22 #include <stdlib.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "wownt32.h"
27 #include "wine/winuser16.h"
28 #include "gdi.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 )
42 if (!dc->hVisRgn)
44 ERR("hVisRgn is zero. Please report this.\n" );
45 exit(1);
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 )
69 INT retval;
70 RECT rect;
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 );
80 return retval;
83 if (!hrgn)
85 if (fnMode == RGN_COPY)
87 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
88 dc->hClipRgn = 0;
90 else
92 FIXME("Unimplemented: hrgn NULL in mode: %d\n", fnMode);
93 GDI_ReleaseObj( hdc );
94 return ERROR;
97 else
99 if (!dc->hClipRgn)
101 RECT rect;
102 GetRgnBox( dc->hVisRgn, &rect );
103 dc->hClipRgn = CreateRectRgnIndirect( &rect );
106 if(fnMode == RGN_COPY)
107 CombineRgn( dc->hClipRgn, hrgn, 0, fnMode );
108 else
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 )
123 int retval;
124 HDC hdc = HDC_32( hdc16 );
125 DC * dc;
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 );
137 return retval;
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 );
160 return ret;
164 /***********************************************************************
165 * OffsetVisRgn (GDI.102)
167 INT16 WINAPI OffsetVisRgn16( HDC16 hdc16, INT16 x, INT16 y )
169 INT16 retval;
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 );
177 return retval;
181 /***********************************************************************
182 * ExcludeClipRect (GDI32.@)
184 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
185 INT right, INT bottom )
187 HRGN newRgn;
188 INT ret;
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 );
196 else
198 POINT pt[2];
200 pt[0].x = left;
201 pt[0].y = top;
202 pt[1].x = right;
203 pt[1].y = bottom;
204 LPtoDP( hdc, pt, 2 );
205 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
206 else
208 if (!dc->hClipRgn)
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 );
219 return ret;
223 /***********************************************************************
224 * IntersectClipRect (GDI32.@)
226 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
228 INT ret;
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 );
236 else
238 POINT pt[2];
240 pt[0].x = left;
241 pt[0].y = top;
242 pt[1].x = right;
243 pt[1].y = bottom;
245 LPtoDP( hdc, pt, 2 );
247 if (!dc->hClipRgn)
249 dc->hClipRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y );
250 ret = SIMPLEREGION;
252 else
254 HRGN newRgn;
256 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
257 else
259 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_AND );
260 DeleteObject( newRgn );
263 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
265 GDI_ReleaseObj( hdc );
266 return ret;
270 /***********************************************************************
271 * ExcludeVisRect (GDI.73)
273 INT16 WINAPI ExcludeVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
275 HRGN tempRgn;
276 INT16 ret;
277 POINT pt[2];
278 HDC hdc = HDC_32( hdc16 );
279 DC * dc = DC_GetDCUpdate( hdc );
280 if (!dc) return ERROR;
282 pt[0].x = left;
283 pt[0].y = top;
284 pt[1].x = right;
285 pt[1].y = bottom;
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;
292 else
294 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_DIFF );
295 DeleteObject( tempRgn );
297 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
298 GDI_ReleaseObj( hdc );
299 return ret;
303 /***********************************************************************
304 * IntersectVisRect (GDI.98)
306 INT16 WINAPI IntersectVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
308 HRGN tempRgn;
309 INT16 ret;
310 POINT pt[2];
311 HDC hdc = HDC_32( hdc16 );
312 DC * dc = DC_GetDCUpdate( hdc );
313 if (!dc) return ERROR;
315 pt[0].x = left;
316 pt[0].y = top;
317 pt[1].x = right;
318 pt[1].y = bottom;
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;
326 else
328 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_AND );
329 DeleteObject( tempRgn );
331 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
332 GDI_ReleaseObj( hdc );
333 return ret;
337 /***********************************************************************
338 * PtVisible (GDI32.@)
340 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
342 POINT pt;
343 BOOL ret;
344 DC *dc = DC_GetDCUpdate( hdc );
346 TRACE("%p %d,%d\n", hdc, x, y );
347 if (!dc) return FALSE;
349 pt.x = x;
350 pt.y = y;
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 );
355 return ret;
359 /***********************************************************************
360 * RectVisible (GDI32.@)
362 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
364 RECT tmpRect;
365 BOOL ret;
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 );
370 tmpRect = *rect;
371 LPtoDP( hdc, (POINT *)&tmpRect, 2 );
373 if (dc->hClipRgn)
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 );
382 return ret;
386 /***********************************************************************
387 * GetClipBox (GDI32.@)
389 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
391 INT ret;
392 DC *dc = DC_GetDCUpdate( hdc );
393 if (!dc) return ERROR;
394 if (dc->hClipRgn)
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 );
404 return ret;
408 /***********************************************************************
409 * GetClipRgn (GDI32.@)
411 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
413 INT ret = -1;
414 DC * dc;
415 if (hRgn && (dc = DC_GetDCPtr( hdc )))
417 if( dc->hClipRgn )
419 if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR ) ret = 1;
421 else ret = 0;
422 GDI_ReleaseObj( hdc );
424 return ret;
427 /***********************************************************************
428 * SaveVisRgn (GDI.129)
430 HRGN16 WINAPI SaveVisRgn16( HDC16 hdc16 )
432 HRGN copy;
433 GDIOBJHDR *obj, *copyObj;
434 HDC hdc = HDC_32( hdc16 );
435 DC *dc = DC_GetDCUpdate( hdc );
437 if (!dc) return 0;
438 TRACE("%p\n", hdc );
440 if (!(obj = GDI_GetObjPtr( dc->hVisRgn, REGION_MAGIC )))
442 GDI_ReleaseObj( hdc );
443 return 0;
445 if (!(copy = CreateRectRgn( 0, 0, 0, 0 )))
447 GDI_ReleaseObj( dc->hVisRgn );
448 GDI_ReleaseObj( hdc );
449 return 0;
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 );
457 return 0;
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 )
473 HRGN saved;
474 GDIOBJHDR *obj, *savedObj;
475 HDC hdc = HDC_32( hdc16 );
476 DC *dc = DC_GetDCPtr( hdc );
477 INT16 ret = ERROR;
479 if (!dc) return ERROR;
481 TRACE("%p\n", hdc );
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 );
496 done:
497 GDI_ReleaseObj( hdc );
498 return ret;
502 /***********************************************************************
503 * GetRandomRgn [GDI32.@]
505 * NOTES
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)
518 switch (dwCode)
520 case SYSRGN: /* == 4 */
522 DC *dc = DC_GetDCPtr (hDC);
523 if (!dc) return -1;
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.
530 * On Windows 95/98,
531 * the region returned is in window coordinates
533 if (!(GetVersion() & 0x80000000))
535 POINT org;
536 GetDCOrgEx(hDC, &org);
537 OffsetRgn(hRgn, org.x, org.y);
539 return 1;
542 case 1: /* clip region */
543 return GetClipRgn (hDC, hRgn);
545 default:
546 WARN("Unknown dwCode %ld\n", dwCode);
547 return -1;
550 return -1;
554 /***********************************************************************
555 * GetMetaRgn (GDI32.@)
557 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
559 FIXME( "stub\n" );
561 return 0;
565 /***********************************************************************
566 * SetMetaRgn (GDI32.@)
568 INT WINAPI SetMetaRgn( HDC hdc )
570 FIXME( "stub\n" );
572 return ERROR;