gdi32: Replace DC_GetDCUpdate by get_dc_ptr+update_dc in the clipping functions.
[wine/hacks.git] / dlls / gdi32 / clipping.c
blob8b019c62023e3a4ebff6f95350934224458345b5
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 (GDI.105)
168 INT16 WINAPI SelectVisRgn16( HDC16 hdc16, HRGN16 hrgn )
170 int retval;
171 HDC hdc = HDC_32( hdc16 );
172 DC * dc;
174 if (!hrgn) return ERROR;
175 if (!(dc = DC_GetDCPtr( hdc ))) return ERROR;
177 TRACE("%p %04x\n", hdc, hrgn );
179 dc->dirty = 0;
181 retval = CombineRgn( dc->hVisRgn, HRGN_32(hrgn), 0, RGN_COPY );
182 CLIPPING_UpdateGCRegion( dc );
183 DC_ReleaseDCPtr( dc );
184 return retval;
188 /***********************************************************************
189 * OffsetClipRgn (GDI32.@)
191 INT WINAPI OffsetClipRgn( HDC hdc, INT x, INT y )
193 INT ret = SIMPLEREGION;
194 DC *dc = get_dc_ptr( hdc );
195 if (!dc) return ERROR;
197 TRACE("%p %d,%d\n", hdc, x, y );
199 update_dc( dc );
200 if(dc->funcs->pOffsetClipRgn)
202 ret = dc->funcs->pOffsetClipRgn( dc->physDev, x, y );
203 /* FIXME: ret is just a success flag, we should return a proper value */
205 else if (dc->hClipRgn) {
206 ret = OffsetRgn( dc->hClipRgn, MulDiv( x, dc->vportExtX, dc->wndExtX ),
207 MulDiv( y, dc->vportExtY, dc->wndExtY ) );
208 CLIPPING_UpdateGCRegion( dc );
210 release_dc_ptr( dc );
211 return ret;
215 /***********************************************************************
216 * OffsetVisRgn (GDI.102)
218 INT16 WINAPI OffsetVisRgn16( HDC16 hdc16, INT16 x, INT16 y )
220 INT16 retval;
221 HDC hdc = HDC_32( hdc16 );
222 DC * dc = get_dc_ptr( hdc );
224 if (!dc) return ERROR;
225 TRACE("%p %d,%d\n", hdc, x, y );
226 update_dc( dc );
227 retval = OffsetRgn( dc->hVisRgn, x, y );
228 CLIPPING_UpdateGCRegion( dc );
229 release_dc_ptr( dc );
230 return retval;
234 /***********************************************************************
235 * ExcludeClipRect (GDI32.@)
237 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
238 INT right, INT bottom )
240 HRGN newRgn;
241 INT ret;
242 DC *dc = get_dc_ptr( hdc );
243 if (!dc) return ERROR;
245 TRACE("%p %dx%d,%dx%d\n", hdc, left, top, right, bottom );
247 update_dc( dc );
248 if(dc->funcs->pExcludeClipRect)
250 ret = dc->funcs->pExcludeClipRect( dc->physDev, left, top, right, bottom );
251 /* FIXME: ret is just a success flag, we should return a proper value */
253 else
255 POINT pt[2];
257 pt[0].x = left;
258 pt[0].y = top;
259 pt[1].x = right;
260 pt[1].y = bottom;
261 LPtoDP( hdc, pt, 2 );
262 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
263 else
265 if (!dc->hClipRgn)
266 create_default_clip_region( dc );
267 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_DIFF );
268 DeleteObject( newRgn );
270 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
272 release_dc_ptr( dc );
273 return ret;
277 /***********************************************************************
278 * IntersectClipRect (GDI32.@)
280 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
282 INT ret;
283 DC *dc = get_dc_ptr( hdc );
284 if (!dc) return ERROR;
286 TRACE("%p %d,%d - %d,%d\n", hdc, left, top, right, bottom );
288 update_dc( dc );
289 if(dc->funcs->pIntersectClipRect)
291 ret = dc->funcs->pIntersectClipRect( dc->physDev, left, top, right, bottom );
292 /* FIXME: ret is just a success flag, we should return a proper value */
294 else
296 POINT pt[2];
298 pt[0].x = left;
299 pt[0].y = top;
300 pt[1].x = right;
301 pt[1].y = bottom;
303 LPtoDP( hdc, pt, 2 );
305 if (!dc->hClipRgn)
307 dc->hClipRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y );
308 ret = SIMPLEREGION;
310 else
312 HRGN newRgn;
314 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
315 else
317 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_AND );
318 DeleteObject( newRgn );
321 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
323 release_dc_ptr( dc );
324 return ret;
328 /***********************************************************************
329 * ExcludeVisRect (GDI.73)
331 INT16 WINAPI ExcludeVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
333 HRGN tempRgn;
334 INT16 ret;
335 POINT pt[2];
336 HDC hdc = HDC_32( hdc16 );
337 DC * dc = get_dc_ptr( hdc );
338 if (!dc) return ERROR;
340 pt[0].x = left;
341 pt[0].y = top;
342 pt[1].x = right;
343 pt[1].y = bottom;
345 LPtoDP( hdc, pt, 2 );
347 TRACE("%p %d,%d - %d,%d\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
349 if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
350 else
352 update_dc( dc );
353 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_DIFF );
354 DeleteObject( tempRgn );
356 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
357 release_dc_ptr( dc );
358 return ret;
362 /***********************************************************************
363 * IntersectVisRect (GDI.98)
365 INT16 WINAPI IntersectVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
367 HRGN tempRgn;
368 INT16 ret;
369 POINT pt[2];
370 HDC hdc = HDC_32( hdc16 );
371 DC * dc = get_dc_ptr( hdc );
372 if (!dc) return ERROR;
374 pt[0].x = left;
375 pt[0].y = top;
376 pt[1].x = right;
377 pt[1].y = bottom;
379 LPtoDP( hdc, pt, 2 );
381 TRACE("%p %d,%d - %d,%d\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
383 if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
384 else
386 update_dc( dc );
387 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_AND );
388 DeleteObject( tempRgn );
390 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
391 release_dc_ptr( dc );
392 return ret;
396 /***********************************************************************
397 * PtVisible (GDI32.@)
399 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
401 POINT pt;
402 BOOL ret;
403 HRGN clip;
404 DC *dc = get_dc_ptr( hdc );
406 TRACE("%p %d,%d\n", hdc, x, y );
407 if (!dc) return FALSE;
409 pt.x = x;
410 pt.y = y;
411 LPtoDP( hdc, &pt, 1 );
412 update_dc( dc );
413 ret = PtInRegion( dc->hVisRgn, pt.x, pt.y );
414 if (ret && (clip = get_clip_region(dc))) ret = PtInRegion( clip, pt.x, pt.y );
415 release_dc_ptr( dc );
416 return ret;
420 /***********************************************************************
421 * RectVisible (GDI32.@)
423 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
425 RECT tmpRect;
426 BOOL ret;
427 HRGN clip;
428 DC *dc = get_dc_ptr( hdc );
429 if (!dc) return FALSE;
430 TRACE("%p %d,%dx%d,%d\n", hdc, rect->left, rect->top, rect->right, rect->bottom );
432 tmpRect = *rect;
433 LPtoDP( hdc, (POINT *)&tmpRect, 2 );
435 update_dc( dc );
436 if ((clip = get_clip_region(dc)))
438 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
439 CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
440 ret = RectInRegion( hrgn, &tmpRect );
441 DeleteObject( hrgn );
443 else ret = RectInRegion( dc->hVisRgn, &tmpRect );
444 release_dc_ptr( dc );
445 return ret;
449 /***********************************************************************
450 * GetClipBox (GDI32.@)
452 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
454 INT ret;
455 HRGN clip;
456 DC *dc = get_dc_ptr( hdc );
457 if (!dc) return ERROR;
459 update_dc( dc );
460 if ((clip = get_clip_region(dc)))
462 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
463 CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
464 ret = GetRgnBox( hrgn, rect );
465 DeleteObject( hrgn );
467 else ret = GetRgnBox( dc->hVisRgn, rect );
468 DPtoLP( hdc, (LPPOINT)rect, 2 );
469 release_dc_ptr( dc );
470 return ret;
474 /***********************************************************************
475 * GetClipRgn (GDI32.@)
477 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
479 INT ret = -1;
480 DC * dc;
481 if (hRgn && (dc = DC_GetDCPtr( hdc )))
483 if( dc->hClipRgn )
485 if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR ) ret = 1;
487 else ret = 0;
488 DC_ReleaseDCPtr( dc );
490 return ret;
494 /***********************************************************************
495 * GetMetaRgn (GDI32.@)
497 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
499 INT ret = 0;
500 DC * dc = DC_GetDCPtr( hdc );
502 if (dc)
504 if (dc->hMetaRgn && CombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY ) != ERROR)
505 ret = 1;
506 DC_ReleaseDCPtr( dc );
508 return ret;
512 /***********************************************************************
513 * SaveVisRgn (GDI.129)
515 HRGN16 WINAPI SaveVisRgn16( HDC16 hdc16 )
517 struct saved_visrgn *saved;
518 HDC hdc = HDC_32( hdc16 );
519 DC *dc = get_dc_ptr( hdc );
521 if (!dc) return 0;
522 TRACE("%p\n", hdc );
524 update_dc( dc );
525 if (!(saved = HeapAlloc( GetProcessHeap(), 0, sizeof(*saved) ))) goto error;
526 if (!(saved->hrgn = CreateRectRgn( 0, 0, 0, 0 ))) goto error;
527 CombineRgn( saved->hrgn, dc->hVisRgn, 0, RGN_COPY );
528 saved->next = dc->saved_visrgn;
529 dc->saved_visrgn = saved;
530 release_dc_ptr( dc );
531 return HRGN_16(saved->hrgn);
533 error:
534 release_dc_ptr( dc );
535 HeapFree( GetProcessHeap(), 0, saved );
536 return 0;
540 /***********************************************************************
541 * RestoreVisRgn (GDI.130)
543 INT16 WINAPI RestoreVisRgn16( HDC16 hdc16 )
545 struct saved_visrgn *saved;
546 HDC hdc = HDC_32( hdc16 );
547 DC *dc = DC_GetDCPtr( hdc );
548 INT16 ret = ERROR;
550 if (!dc) return ERROR;
552 TRACE("%p\n", hdc );
554 if (!(saved = dc->saved_visrgn)) goto done;
556 ret = CombineRgn( dc->hVisRgn, saved->hrgn, 0, RGN_COPY );
557 dc->saved_visrgn = saved->next;
558 DeleteObject( saved->hrgn );
559 HeapFree( GetProcessHeap(), 0, saved );
560 CLIPPING_UpdateGCRegion( dc );
561 done:
562 DC_ReleaseDCPtr( dc );
563 return ret;
567 /***********************************************************************
568 * GetRandomRgn [GDI32.@]
570 * NOTES
571 * This function is documented in MSDN online for the case of
572 * iCode == SYSRGN (4).
574 * For iCode == 1 it should return the clip region
575 * 2 " " " the meta region
576 * 3 " " " the intersection of the clip with
577 * the meta region (== 'Rao' region).
579 * See http://www.codeproject.com/gdi/cliprgnguide.asp
581 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, INT iCode)
583 HRGN rgn;
584 DC *dc = get_dc_ptr( hDC );
586 if (!dc) return -1;
588 switch (iCode)
590 case 1:
591 rgn = dc->hClipRgn;
592 break;
593 case 2:
594 rgn = dc->hMetaRgn;
595 break;
596 case 3:
597 rgn = dc->hMetaClipRgn;
598 if(!rgn) rgn = dc->hClipRgn;
599 if(!rgn) rgn = dc->hMetaRgn;
600 break;
601 case SYSRGN: /* == 4 */
602 update_dc( dc );
603 rgn = dc->hVisRgn;
604 break;
605 default:
606 WARN("Unknown code %d\n", iCode);
607 release_dc_ptr( dc );
608 return -1;
610 if (rgn) CombineRgn( hRgn, rgn, 0, RGN_COPY );
611 release_dc_ptr( dc );
613 /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
614 if (iCode == SYSRGN && !(GetVersion() & 0x80000000))
616 POINT org;
617 GetDCOrgEx( hDC, &org );
618 OffsetRgn( hRgn, org.x, org.y );
620 return (rgn != 0);
624 /***********************************************************************
625 * SetMetaRgn (GDI32.@)
627 INT WINAPI SetMetaRgn( HDC hdc )
629 INT ret;
630 RECT dummy;
631 DC *dc = DC_GetDCPtr( hdc );
633 if (!dc) return ERROR;
635 if (dc->hMetaClipRgn)
637 /* the intersection becomes the new meta region */
638 DeleteObject( dc->hMetaRgn );
639 DeleteObject( dc->hClipRgn );
640 dc->hMetaRgn = dc->hMetaClipRgn;
641 dc->hClipRgn = 0;
642 dc->hMetaClipRgn = 0;
644 else if (dc->hClipRgn)
646 dc->hMetaRgn = dc->hClipRgn;
647 dc->hClipRgn = 0;
649 /* else nothing to do */
651 /* Note: no need to call CLIPPING_UpdateGCRegion, the overall clip region hasn't changed */
653 ret = GetRgnBox( dc->hMetaRgn, &dummy );
654 DC_ReleaseDCPtr( dc );
655 return ret;