mlang/tests: Fix a pointer casting warning on 64-bit.
[wine/testsucceed.git] / dlls / gdi32 / clipping.c
blobe3a58518771365634c6fea7c2fbd380733a68f2d
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "gdi_private.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(clipping);
33 /***********************************************************************
34 * get_clip_region
36 * Return the total clip region (if any).
38 static inline HRGN get_clip_region( DC * dc )
40 if (dc->hMetaClipRgn) return dc->hMetaClipRgn;
41 if (dc->hMetaRgn) return dc->hMetaRgn;
42 return dc->hClipRgn;
46 /***********************************************************************
47 * CLIPPING_UpdateGCRegion
49 * Update the GC clip region when the ClipRgn or VisRgn have changed.
51 void CLIPPING_UpdateGCRegion( DC * dc )
53 HRGN clip_rgn;
55 if (!dc->hVisRgn)
57 ERR("hVisRgn is zero. Please report this.\n" );
58 exit(1);
61 /* update the intersection of meta and clip regions */
62 if (dc->hMetaRgn && dc->hClipRgn)
64 if (!dc->hMetaClipRgn) dc->hMetaClipRgn = CreateRectRgn( 0, 0, 0, 0 );
65 CombineRgn( dc->hMetaClipRgn, dc->hClipRgn, dc->hMetaRgn, RGN_AND );
66 clip_rgn = dc->hMetaClipRgn;
68 else /* only one is set, no need for an intersection */
70 if (dc->hMetaClipRgn) DeleteObject( dc->hMetaClipRgn );
71 dc->hMetaClipRgn = 0;
72 clip_rgn = dc->hMetaRgn ? dc->hMetaRgn : dc->hClipRgn;
75 if (dc->funcs->pSetDeviceClipping)
76 dc->funcs->pSetDeviceClipping( dc->physDev, dc->hVisRgn, clip_rgn );
79 /***********************************************************************
80 * create_default_clip_region
82 * Create a default clipping region when none already exists.
84 static inline void create_default_clip_region( DC * dc )
86 UINT width, height;
88 if (GDIMAGIC( dc->header.wMagic ) == MEMORY_DC_MAGIC)
90 BITMAP bitmap;
92 GetObjectW( dc->hBitmap, sizeof(bitmap), &bitmap );
93 width = bitmap.bmWidth;
94 height = bitmap.bmHeight;
96 else
98 width = GetDeviceCaps( dc->hSelf, DESKTOPHORZRES );
99 height = GetDeviceCaps( dc->hSelf, DESKTOPVERTRES );
101 dc->hClipRgn = CreateRectRgn( 0, 0, width, height );
105 /***********************************************************************
106 * SelectClipRgn (GDI32.@)
108 INT WINAPI SelectClipRgn( HDC hdc, HRGN hrgn )
110 return ExtSelectClipRgn( hdc, hrgn, RGN_COPY );
114 /******************************************************************************
115 * ExtSelectClipRgn [GDI32.@]
117 INT WINAPI ExtSelectClipRgn( HDC hdc, HRGN hrgn, INT fnMode )
119 INT retval;
120 RECT rect;
121 DC * dc = get_dc_ptr( hdc );
122 if (!dc) return ERROR;
124 TRACE("%p %p %d\n", hdc, hrgn, fnMode );
126 update_dc( dc );
127 if (dc->funcs->pExtSelectClipRgn)
129 retval = dc->funcs->pExtSelectClipRgn( dc->physDev, hrgn, fnMode );
130 release_dc_ptr( dc );
131 return retval;
134 if (!hrgn)
136 if (fnMode == RGN_COPY)
138 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
139 dc->hClipRgn = 0;
141 else
143 FIXME("Unimplemented: hrgn NULL in mode: %d\n", fnMode);
144 release_dc_ptr( dc );
145 return ERROR;
148 else
150 if (!dc->hClipRgn)
151 create_default_clip_region( dc );
153 if(fnMode == RGN_COPY)
154 CombineRgn( dc->hClipRgn, hrgn, 0, fnMode );
155 else
156 CombineRgn( dc->hClipRgn, dc->hClipRgn, hrgn, fnMode);
159 CLIPPING_UpdateGCRegion( dc );
160 release_dc_ptr( dc );
162 return GetClipBox(hdc, &rect);
165 /***********************************************************************
166 * SelectVisRgn (GDI32.@)
168 * Note: not exported on Windows, only the 16-bit version is exported.
170 INT WINAPI SelectVisRgn( HDC hdc, HRGN hrgn )
172 int retval;
173 DC * dc;
175 if (!hrgn) return ERROR;
176 if (!(dc = get_dc_ptr( hdc ))) return ERROR;
178 TRACE("%p %p\n", hdc, hrgn );
180 dc->dirty = 0;
182 retval = CombineRgn( dc->hVisRgn, hrgn, 0, RGN_COPY );
183 CLIPPING_UpdateGCRegion( dc );
184 release_dc_ptr( dc );
185 return retval;
189 /***********************************************************************
190 * OffsetClipRgn (GDI32.@)
192 INT WINAPI OffsetClipRgn( HDC hdc, INT x, INT y )
194 INT ret = SIMPLEREGION;
195 DC *dc = get_dc_ptr( hdc );
196 if (!dc) return ERROR;
198 TRACE("%p %d,%d\n", hdc, x, y );
200 update_dc( dc );
201 if(dc->funcs->pOffsetClipRgn)
203 ret = dc->funcs->pOffsetClipRgn( dc->physDev, x, y );
204 /* FIXME: ret is just a success flag, we should return a proper value */
206 else if (dc->hClipRgn) {
207 ret = OffsetRgn( dc->hClipRgn, MulDiv( x, dc->vportExtX, dc->wndExtX ),
208 MulDiv( y, dc->vportExtY, dc->wndExtY ) );
209 CLIPPING_UpdateGCRegion( dc );
211 release_dc_ptr( dc );
212 return ret;
216 /***********************************************************************
217 * OffsetVisRgn (GDI.102)
219 INT16 WINAPI OffsetVisRgn16( HDC16 hdc16, INT16 x, INT16 y )
221 INT16 retval;
222 HDC hdc = HDC_32( hdc16 );
223 DC * dc = get_dc_ptr( hdc );
225 if (!dc) return ERROR;
226 TRACE("%p %d,%d\n", hdc, x, y );
227 update_dc( dc );
228 retval = OffsetRgn( dc->hVisRgn, x, y );
229 CLIPPING_UpdateGCRegion( dc );
230 release_dc_ptr( dc );
231 return retval;
235 /***********************************************************************
236 * ExcludeClipRect (GDI32.@)
238 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
239 INT right, INT bottom )
241 HRGN newRgn;
242 INT ret;
243 DC *dc = get_dc_ptr( hdc );
244 if (!dc) return ERROR;
246 TRACE("%p %dx%d,%dx%d\n", hdc, left, top, right, bottom );
248 update_dc( dc );
249 if(dc->funcs->pExcludeClipRect)
251 ret = dc->funcs->pExcludeClipRect( dc->physDev, left, top, right, bottom );
252 /* FIXME: ret is just a success flag, we should return a proper value */
254 else
256 POINT pt[2];
258 pt[0].x = left;
259 pt[0].y = top;
260 pt[1].x = right;
261 pt[1].y = bottom;
262 LPtoDP( hdc, pt, 2 );
263 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
264 else
266 if (!dc->hClipRgn)
267 create_default_clip_region( dc );
268 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_DIFF );
269 DeleteObject( newRgn );
271 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
273 release_dc_ptr( dc );
274 return ret;
278 /***********************************************************************
279 * IntersectClipRect (GDI32.@)
281 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
283 INT ret;
284 DC *dc = get_dc_ptr( hdc );
285 if (!dc) return ERROR;
287 TRACE("%p %d,%d - %d,%d\n", hdc, left, top, right, bottom );
289 update_dc( dc );
290 if(dc->funcs->pIntersectClipRect)
292 ret = dc->funcs->pIntersectClipRect( dc->physDev, left, top, right, bottom );
293 /* FIXME: ret is just a success flag, we should return a proper value */
295 else
297 POINT pt[2];
299 pt[0].x = left;
300 pt[0].y = top;
301 pt[1].x = right;
302 pt[1].y = bottom;
304 LPtoDP( hdc, pt, 2 );
306 if (!dc->hClipRgn)
308 dc->hClipRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y );
309 ret = SIMPLEREGION;
311 else
313 HRGN newRgn;
315 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
316 else
318 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_AND );
319 DeleteObject( newRgn );
322 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
324 release_dc_ptr( dc );
325 return ret;
329 /***********************************************************************
330 * ExcludeVisRect (GDI.73)
332 INT16 WINAPI ExcludeVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
334 HRGN tempRgn;
335 INT16 ret;
336 POINT pt[2];
337 HDC hdc = HDC_32( hdc16 );
338 DC * dc = get_dc_ptr( hdc );
339 if (!dc) return ERROR;
341 pt[0].x = left;
342 pt[0].y = top;
343 pt[1].x = right;
344 pt[1].y = bottom;
346 LPtoDP( hdc, pt, 2 );
348 TRACE("%p %d,%d - %d,%d\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
350 if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
351 else
353 update_dc( dc );
354 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_DIFF );
355 DeleteObject( tempRgn );
357 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
358 release_dc_ptr( dc );
359 return ret;
363 /***********************************************************************
364 * IntersectVisRect (GDI.98)
366 INT16 WINAPI IntersectVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
368 HRGN tempRgn;
369 INT16 ret;
370 POINT pt[2];
371 HDC hdc = HDC_32( hdc16 );
372 DC * dc = get_dc_ptr( hdc );
373 if (!dc) return ERROR;
375 pt[0].x = left;
376 pt[0].y = top;
377 pt[1].x = right;
378 pt[1].y = bottom;
380 LPtoDP( hdc, pt, 2 );
382 TRACE("%p %d,%d - %d,%d\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
384 if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
385 else
387 update_dc( dc );
388 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_AND );
389 DeleteObject( tempRgn );
391 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
392 release_dc_ptr( dc );
393 return ret;
397 /***********************************************************************
398 * PtVisible (GDI32.@)
400 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
402 POINT pt;
403 BOOL ret;
404 HRGN clip;
405 DC *dc = get_dc_ptr( hdc );
407 TRACE("%p %d,%d\n", hdc, x, y );
408 if (!dc) return FALSE;
410 pt.x = x;
411 pt.y = y;
412 LPtoDP( hdc, &pt, 1 );
413 update_dc( dc );
414 ret = PtInRegion( dc->hVisRgn, pt.x, pt.y );
415 if (ret && (clip = get_clip_region(dc))) ret = PtInRegion( clip, pt.x, pt.y );
416 release_dc_ptr( dc );
417 return ret;
421 /***********************************************************************
422 * RectVisible (GDI32.@)
424 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
426 RECT tmpRect;
427 BOOL ret;
428 HRGN clip;
429 DC *dc = get_dc_ptr( hdc );
430 if (!dc) return FALSE;
431 TRACE("%p %d,%dx%d,%d\n", hdc, rect->left, rect->top, rect->right, rect->bottom );
433 tmpRect = *rect;
434 LPtoDP( hdc, (POINT *)&tmpRect, 2 );
436 update_dc( dc );
437 if ((clip = get_clip_region(dc)))
439 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
440 CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
441 ret = RectInRegion( hrgn, &tmpRect );
442 DeleteObject( hrgn );
444 else ret = RectInRegion( dc->hVisRgn, &tmpRect );
445 release_dc_ptr( dc );
446 return ret;
450 /***********************************************************************
451 * GetClipBox (GDI32.@)
453 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
455 INT ret;
456 HRGN clip;
457 DC *dc = get_dc_ptr( hdc );
458 if (!dc) return ERROR;
460 update_dc( dc );
461 if ((clip = get_clip_region(dc)))
463 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
464 CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
465 ret = GetRgnBox( hrgn, rect );
466 DeleteObject( hrgn );
468 else ret = GetRgnBox( dc->hVisRgn, rect );
469 DPtoLP( hdc, (LPPOINT)rect, 2 );
470 release_dc_ptr( dc );
471 return ret;
475 /***********************************************************************
476 * GetClipRgn (GDI32.@)
478 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
480 INT ret = -1;
481 DC * dc;
482 if (hRgn && (dc = get_dc_ptr( hdc )))
484 if( dc->hClipRgn )
486 if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR ) ret = 1;
488 else ret = 0;
489 release_dc_ptr( dc );
491 return ret;
495 /***********************************************************************
496 * GetMetaRgn (GDI32.@)
498 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
500 INT ret = 0;
501 DC * dc = get_dc_ptr( hdc );
503 if (dc)
505 if (dc->hMetaRgn && CombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY ) != ERROR)
506 ret = 1;
507 release_dc_ptr( dc );
509 return ret;
513 /***********************************************************************
514 * SaveVisRgn (GDI.129)
516 HRGN16 WINAPI SaveVisRgn16( HDC16 hdc16 )
518 struct saved_visrgn *saved;
519 HDC hdc = HDC_32( hdc16 );
520 DC *dc = get_dc_ptr( hdc );
522 if (!dc) return 0;
523 TRACE("%p\n", hdc );
525 update_dc( dc );
526 if (!(saved = HeapAlloc( GetProcessHeap(), 0, sizeof(*saved) ))) goto error;
527 if (!(saved->hrgn = CreateRectRgn( 0, 0, 0, 0 ))) goto error;
528 CombineRgn( saved->hrgn, dc->hVisRgn, 0, RGN_COPY );
529 saved->next = dc->saved_visrgn;
530 dc->saved_visrgn = saved;
531 release_dc_ptr( dc );
532 return HRGN_16(saved->hrgn);
534 error:
535 release_dc_ptr( dc );
536 HeapFree( GetProcessHeap(), 0, saved );
537 return 0;
541 /***********************************************************************
542 * RestoreVisRgn (GDI.130)
544 INT16 WINAPI RestoreVisRgn16( HDC16 hdc16 )
546 struct saved_visrgn *saved;
547 HDC hdc = HDC_32( hdc16 );
548 DC *dc = get_dc_ptr( hdc );
549 INT16 ret = ERROR;
551 if (!dc) return ERROR;
553 TRACE("%p\n", hdc );
555 if (!(saved = dc->saved_visrgn)) goto done;
557 ret = CombineRgn( dc->hVisRgn, saved->hrgn, 0, RGN_COPY );
558 dc->saved_visrgn = saved->next;
559 DeleteObject( saved->hrgn );
560 HeapFree( GetProcessHeap(), 0, saved );
561 CLIPPING_UpdateGCRegion( dc );
562 done:
563 release_dc_ptr( dc );
564 return ret;
568 /***********************************************************************
569 * GetRandomRgn [GDI32.@]
571 * NOTES
572 * This function is documented in MSDN online for the case of
573 * iCode == SYSRGN (4).
575 * For iCode == 1 it should return the clip region
576 * 2 " " " the meta region
577 * 3 " " " the intersection of the clip with
578 * the meta region (== 'Rao' region).
580 * See http://www.codeproject.com/gdi/cliprgnguide.asp
582 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, INT iCode)
584 HRGN rgn;
585 DC *dc = get_dc_ptr( hDC );
587 if (!dc) return -1;
589 switch (iCode)
591 case 1:
592 rgn = dc->hClipRgn;
593 break;
594 case 2:
595 rgn = dc->hMetaRgn;
596 break;
597 case 3:
598 rgn = dc->hMetaClipRgn;
599 if(!rgn) rgn = dc->hClipRgn;
600 if(!rgn) rgn = dc->hMetaRgn;
601 break;
602 case SYSRGN: /* == 4 */
603 update_dc( dc );
604 rgn = dc->hVisRgn;
605 break;
606 default:
607 WARN("Unknown code %d\n", iCode);
608 release_dc_ptr( dc );
609 return -1;
611 if (rgn) CombineRgn( hRgn, rgn, 0, RGN_COPY );
612 release_dc_ptr( dc );
614 /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
615 if (iCode == SYSRGN && !(GetVersion() & 0x80000000))
617 POINT org;
618 GetDCOrgEx( hDC, &org );
619 OffsetRgn( hRgn, org.x, org.y );
621 return (rgn != 0);
625 /***********************************************************************
626 * SetMetaRgn (GDI32.@)
628 INT WINAPI SetMetaRgn( HDC hdc )
630 INT ret;
631 RECT dummy;
632 DC *dc = get_dc_ptr( hdc );
634 if (!dc) return ERROR;
636 if (dc->hMetaClipRgn)
638 /* the intersection becomes the new meta region */
639 DeleteObject( dc->hMetaRgn );
640 DeleteObject( dc->hClipRgn );
641 dc->hMetaRgn = dc->hMetaClipRgn;
642 dc->hClipRgn = 0;
643 dc->hMetaClipRgn = 0;
645 else if (dc->hClipRgn)
647 dc->hMetaRgn = dc->hClipRgn;
648 dc->hClipRgn = 0;
650 /* else nothing to do */
652 /* Note: no need to call CLIPPING_UpdateGCRegion, the overall clip region hasn't changed */
654 ret = GetRgnBox( dc->hMetaRgn, &dummy );
655 release_dc_ptr( dc );
656 return ret;