Fixed compile warning.
[wine/multimedia.git] / objects / clipping.c
blob11fd7bb39af393c5f38e1205b4c3842cbb8faf90
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 "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(clipping);
34 /***********************************************************************
35 * CLIPPING_UpdateGCRegion
37 * Update the GC clip region when the ClipRgn or VisRgn have changed.
39 void CLIPPING_UpdateGCRegion( DC * dc )
41 if (!dc->hGCClipRgn) dc->hGCClipRgn = CreateRectRgn( 0, 0, 0, 0 );
43 if (!dc->hVisRgn)
45 ERR("hVisRgn is zero. Please report this.\n" );
46 exit(1);
49 if (dc->flags & DC_DIRTY) ERR( "DC is dirty. Please report this.\n" );
51 if (!dc->hClipRgn)
52 CombineRgn( dc->hGCClipRgn, dc->hVisRgn, 0, RGN_COPY );
53 else
54 CombineRgn(dc->hGCClipRgn, dc->hClipRgn, dc->hVisRgn, RGN_AND);
55 if (dc->funcs->pSetDeviceClipping)
56 dc->funcs->pSetDeviceClipping( dc->physDev, dc->hGCClipRgn );
60 /***********************************************************************
61 * SelectClipRgn (GDI32.@)
63 INT WINAPI SelectClipRgn( HDC hdc, HRGN hrgn )
65 return ExtSelectClipRgn( hdc, hrgn, RGN_COPY );
69 /******************************************************************************
70 * ExtSelectClipRgn [GDI32.@]
72 INT WINAPI ExtSelectClipRgn( HDC hdc, HRGN hrgn, INT fnMode )
74 INT retval;
75 RECT rect;
76 DC * dc = DC_GetDCUpdate( hdc );
77 if (!dc) return ERROR;
79 TRACE("%p %p %d\n", hdc, hrgn, fnMode );
81 if (dc->funcs->pExtSelectClipRgn)
83 retval = dc->funcs->pExtSelectClipRgn( dc->physDev, hrgn, fnMode );
84 GDI_ReleaseObj( hdc );
85 return retval;
88 if (!hrgn)
90 if (fnMode == RGN_COPY)
92 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
93 dc->hClipRgn = 0;
95 else
97 FIXME("Unimplemented: hrgn NULL in mode: %d\n", fnMode);
98 GDI_ReleaseObj( hdc );
99 return ERROR;
102 else
104 if (!dc->hClipRgn)
106 RECT rect;
107 GetRgnBox( dc->hVisRgn, &rect );
108 dc->hClipRgn = CreateRectRgnIndirect( &rect );
111 if(fnMode == RGN_COPY)
112 CombineRgn( dc->hClipRgn, hrgn, 0, fnMode );
113 else
114 CombineRgn( dc->hClipRgn, dc->hClipRgn, hrgn, fnMode);
117 CLIPPING_UpdateGCRegion( dc );
118 GDI_ReleaseObj( hdc );
120 return GetClipBox(hdc, &rect);
123 /***********************************************************************
124 * SelectVisRgn (GDI.105)
126 INT16 WINAPI SelectVisRgn16( HDC16 hdc16, HRGN16 hrgn )
128 int retval;
129 HDC hdc = HDC_32( hdc16 );
130 DC * dc;
132 if (!hrgn) return ERROR;
133 if (!(dc = DC_GetDCPtr( hdc ))) return ERROR;
135 TRACE("%p %04x\n", hdc, hrgn );
137 dc->flags &= ~DC_DIRTY;
139 retval = CombineRgn( dc->hVisRgn, HRGN_32(hrgn), 0, RGN_COPY );
140 CLIPPING_UpdateGCRegion( dc );
141 GDI_ReleaseObj( hdc );
142 return retval;
146 /***********************************************************************
147 * OffsetClipRgn (GDI32.@)
149 INT WINAPI OffsetClipRgn( HDC hdc, INT x, INT y )
151 INT ret = SIMPLEREGION;
152 DC *dc = DC_GetDCUpdate( hdc );
153 if (!dc) return ERROR;
155 TRACE("%p %d,%d\n", hdc, x, y );
157 if(dc->funcs->pOffsetClipRgn)
158 ret = dc->funcs->pOffsetClipRgn( dc->physDev, x, y );
159 else if (dc->hClipRgn) {
160 ret = OffsetRgn( dc->hClipRgn, XLSTODS(dc,x), YLSTODS(dc,y));
161 CLIPPING_UpdateGCRegion( dc );
163 GDI_ReleaseObj( hdc );
164 return ret;
168 /***********************************************************************
169 * OffsetVisRgn (GDI.102)
171 INT16 WINAPI OffsetVisRgn16( HDC16 hdc16, INT16 x, INT16 y )
173 INT16 retval;
174 HDC hdc = HDC_32( hdc16 );
175 DC * dc = DC_GetDCUpdate( hdc );
176 if (!dc) return ERROR;
177 TRACE("%p %d,%d\n", hdc, x, y );
178 retval = OffsetRgn( dc->hVisRgn, x, y );
179 CLIPPING_UpdateGCRegion( dc );
180 GDI_ReleaseObj( hdc );
181 return retval;
185 /***********************************************************************
186 * ExcludeClipRect (GDI32.@)
188 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
189 INT right, INT bottom )
191 HRGN newRgn;
192 INT ret;
193 DC *dc = DC_GetDCUpdate( hdc );
194 if (!dc) return ERROR;
196 TRACE("%p %dx%d,%dx%d\n", hdc, left, top, right, bottom );
198 if(dc->funcs->pExcludeClipRect)
199 ret = dc->funcs->pExcludeClipRect( dc->physDev, left, top, right, bottom );
200 else
202 POINT pt[2];
204 pt[0].x = left;
205 pt[0].y = top;
206 pt[1].x = right;
207 pt[1].y = bottom;
208 LPtoDP( hdc, pt, 2 );
209 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
210 else
212 if (!dc->hClipRgn)
214 dc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
215 CombineRgn( dc->hClipRgn, dc->hVisRgn, 0, RGN_COPY );
217 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_DIFF );
218 DeleteObject( newRgn );
220 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
222 GDI_ReleaseObj( hdc );
223 return ret;
227 /***********************************************************************
228 * IntersectClipRect (GDI32.@)
230 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
232 INT ret;
233 DC *dc = DC_GetDCUpdate( hdc );
234 if (!dc) return ERROR;
236 TRACE("%p %d,%d - %d,%d\n", hdc, left, top, right, bottom );
238 if(dc->funcs->pIntersectClipRect)
239 ret = dc->funcs->pIntersectClipRect( dc->physDev, left, top, right, bottom );
240 else
242 POINT pt[2];
244 pt[0].x = left;
245 pt[0].y = top;
246 pt[1].x = right;
247 pt[1].y = bottom;
249 LPtoDP( hdc, pt, 2 );
251 if (!dc->hClipRgn)
253 dc->hClipRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y );
254 ret = SIMPLEREGION;
256 else
258 HRGN newRgn;
260 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
261 else
263 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_AND );
264 DeleteObject( newRgn );
267 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
269 GDI_ReleaseObj( hdc );
270 return ret;
274 /***********************************************************************
275 * ExcludeVisRect (GDI.73)
277 INT16 WINAPI ExcludeVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
279 HRGN tempRgn;
280 INT16 ret;
281 POINT pt[2];
282 HDC hdc = HDC_32( hdc16 );
283 DC * dc = DC_GetDCUpdate( hdc );
284 if (!dc) return ERROR;
286 pt[0].x = left;
287 pt[0].y = top;
288 pt[1].x = right;
289 pt[1].y = bottom;
291 LPtoDP( hdc, pt, 2 );
293 TRACE("%p %ld,%ld - %ld,%ld\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
295 if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
296 else
298 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_DIFF );
299 DeleteObject( tempRgn );
301 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
302 GDI_ReleaseObj( hdc );
303 return ret;
307 /***********************************************************************
308 * IntersectVisRect (GDI.98)
310 INT16 WINAPI IntersectVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
312 HRGN tempRgn;
313 INT16 ret;
314 POINT pt[2];
315 HDC hdc = HDC_32( hdc16 );
316 DC * dc = DC_GetDCUpdate( hdc );
317 if (!dc) return ERROR;
319 pt[0].x = left;
320 pt[0].y = top;
321 pt[1].x = right;
322 pt[1].y = bottom;
324 LPtoDP( hdc, pt, 2 );
326 TRACE("%p %ld,%ld - %ld,%ld\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
329 if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
330 else
332 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_AND );
333 DeleteObject( tempRgn );
335 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
336 GDI_ReleaseObj( hdc );
337 return ret;
341 /***********************************************************************
342 * PtVisible (GDI32.@)
344 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
346 BOOL ret = FALSE;
347 DC *dc = DC_GetDCUpdate( hdc );
349 TRACE("%p %d,%d\n", hdc, x, y );
350 if (!dc) return FALSE;
351 if (dc->hGCClipRgn)
353 POINT pt;
355 pt.x = x;
356 pt.y = y;
357 LPtoDP( hdc, &pt, 1 );
358 ret = PtInRegion( dc->hGCClipRgn, pt.x, pt.y );
360 GDI_ReleaseObj( hdc );
361 return ret;
365 /***********************************************************************
366 * RectVisible (GDI32.@)
368 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
370 BOOL ret = FALSE;
371 DC *dc = DC_GetDCUpdate( hdc );
372 if (!dc) return FALSE;
373 TRACE("%p %ld,%ldx%ld,%ld\n", hdc, rect->left, rect->top, rect->right, rect->bottom );
374 if (dc->hGCClipRgn)
376 POINT pt[2];
377 RECT tmpRect;
379 pt[0].x = rect->left;
380 pt[0].y = rect->top;
381 pt[1].x = rect->right;
382 pt[1].y = rect->bottom;
383 LPtoDP( hdc, pt, 2 );
384 tmpRect.left = pt[0].x;
385 tmpRect.top = pt[0].y;
386 tmpRect.right = pt[1].x;
387 tmpRect.bottom = pt[1].y;
388 ret = RectInRegion( dc->hGCClipRgn, &tmpRect );
390 GDI_ReleaseObj( hdc );
391 return ret;
395 /***********************************************************************
396 * GetClipBox (GDI32.@)
398 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
400 INT ret;
401 DC *dc = DC_GetDCUpdate( hdc );
402 if (!dc) return ERROR;
403 ret = GetRgnBox( dc->hGCClipRgn, rect );
404 DPtoLP( hdc, (LPPOINT)rect, 2 );
405 GDI_ReleaseObj( hdc );
406 return ret;
410 /***********************************************************************
411 * GetClipRgn (GDI32.@)
413 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
415 INT ret = -1;
416 DC * dc;
417 if (hRgn && (dc = DC_GetDCPtr( hdc )))
419 if( dc->hClipRgn )
421 if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR ) ret = 1;
423 else ret = 0;
424 GDI_ReleaseObj( hdc );
426 return ret;
429 /***********************************************************************
430 * SaveVisRgn (GDI.129)
432 HRGN16 WINAPI SaveVisRgn16( HDC16 hdc16 )
434 HRGN copy;
435 GDIOBJHDR *obj, *copyObj;
436 HDC hdc = HDC_32( hdc16 );
437 DC *dc = DC_GetDCUpdate( hdc );
439 if (!dc) return 0;
440 TRACE("%p\n", hdc );
442 if (!(obj = GDI_GetObjPtr( dc->hVisRgn, REGION_MAGIC )))
444 GDI_ReleaseObj( hdc );
445 return 0;
447 if (!(copy = CreateRectRgn( 0, 0, 0, 0 )))
449 GDI_ReleaseObj( dc->hVisRgn );
450 GDI_ReleaseObj( hdc );
451 return 0;
453 CombineRgn( copy, dc->hVisRgn, 0, RGN_COPY );
454 if (!(copyObj = GDI_GetObjPtr( copy, REGION_MAGIC )))
456 DeleteObject( copy );
457 GDI_ReleaseObj( dc->hVisRgn );
458 GDI_ReleaseObj( hdc );
459 return 0;
461 copyObj->hNext = obj->hNext;
462 obj->hNext = HRGN_16(copy);
463 GDI_ReleaseObj( copy );
464 GDI_ReleaseObj( dc->hVisRgn );
465 GDI_ReleaseObj( hdc );
466 return HRGN_16(copy);
470 /***********************************************************************
471 * RestoreVisRgn (GDI.130)
473 INT16 WINAPI RestoreVisRgn16( HDC16 hdc16 )
475 HRGN saved;
476 GDIOBJHDR *obj, *savedObj;
477 HDC hdc = HDC_32( hdc16 );
478 DC *dc = DC_GetDCPtr( hdc );
479 INT16 ret = ERROR;
481 if (!dc) return ERROR;
483 TRACE("%p\n", hdc );
485 if (!(obj = GDI_GetObjPtr( dc->hVisRgn, REGION_MAGIC ))) goto done;
486 saved = HRGN_32(obj->hNext);
488 if ((savedObj = GDI_GetObjPtr( saved, REGION_MAGIC )))
490 ret = CombineRgn( dc->hVisRgn, saved, 0, RGN_COPY );
491 obj->hNext = savedObj->hNext;
492 GDI_ReleaseObj( saved );
493 DeleteObject( saved );
494 dc->flags &= ~DC_DIRTY;
495 CLIPPING_UpdateGCRegion( dc );
497 GDI_ReleaseObj( dc->hVisRgn );
498 done:
499 GDI_ReleaseObj( hdc );
500 return ret;
504 /***********************************************************************
505 * GetRandomRgn [GDI32.@]
507 * NOTES
508 * This function is documented in MSDN online for the case of
509 * dwCode == SYSRGN (4).
511 * For dwCode == 1 it should return the clip region
512 * 2 " " " the meta region
513 * 3 " " " the intersection of the clip with
514 * the meta region (== 'Rao' region).
516 * See http://www.codeproject.com/gdi/cliprgnguide.asp
518 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, DWORD dwCode)
520 switch (dwCode)
522 case SYSRGN: /* == 4 */
524 DC *dc = DC_GetDCPtr (hDC);
525 if (!dc) return -1;
527 CombineRgn (hRgn, dc->hVisRgn, 0, RGN_COPY);
528 GDI_ReleaseObj( hDC );
530 * On Windows NT/2000,
531 * the region returned is in screen coordinates.
532 * On Windows 95/98,
533 * the region returned is in window coordinates
535 if (!(GetVersion() & 0x80000000))
537 POINT org;
538 GetDCOrgEx(hDC, &org);
539 OffsetRgn(hRgn, org.x, org.y);
541 return 1;
544 case 1: /* clip region */
545 return GetClipRgn (hDC, hRgn);
547 default:
548 WARN("Unknown dwCode %ld\n", dwCode);
549 return -1;
552 return -1;
556 /***********************************************************************
557 * GetMetaRgn (GDI32.@)
559 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
561 FIXME( "stub\n" );
563 return 0;
567 /***********************************************************************
568 * SetMetaRgn (GDI32.@)
570 INT WINAPI SetMetaRgn( HDC hdc )
572 FIXME( "stub\n" );
574 return ERROR;