gdi32: Store the total visible rectangle in the DC.
[wine/multimedia.git] / dlls / gdi32 / clipping.c
blob142c520a1d22ec48178422ff1117ce08ac5a9773
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 "gdi_private.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(clipping);
32 /***********************************************************************
33 * get_clip_region
35 * Return the total clip region (if any).
37 static inline HRGN get_clip_region( DC * dc )
39 if (dc->hMetaClipRgn) return dc->hMetaClipRgn;
40 if (dc->hMetaRgn) return dc->hMetaRgn;
41 return dc->hClipRgn;
45 /***********************************************************************
46 * CLIPPING_UpdateGCRegion
48 * Update the GC clip region when the ClipRgn or VisRgn have changed.
50 void CLIPPING_UpdateGCRegion( DC * dc )
52 HRGN clip_rgn;
54 /* update the intersection of meta and clip regions */
55 if (dc->hMetaRgn && dc->hClipRgn)
57 if (!dc->hMetaClipRgn) dc->hMetaClipRgn = CreateRectRgn( 0, 0, 0, 0 );
58 CombineRgn( dc->hMetaClipRgn, dc->hClipRgn, dc->hMetaRgn, RGN_AND );
59 clip_rgn = dc->hMetaClipRgn;
61 else /* only one is set, no need for an intersection */
63 if (dc->hMetaClipRgn) DeleteObject( dc->hMetaClipRgn );
64 dc->hMetaClipRgn = 0;
65 clip_rgn = dc->hMetaRgn ? dc->hMetaRgn : dc->hClipRgn;
68 if (dc->funcs->pSetDeviceClipping)
69 dc->funcs->pSetDeviceClipping( dc->physDev, dc->hVisRgn, clip_rgn );
72 /***********************************************************************
73 * create_default_clip_region
75 * Create a default clipping region when none already exists.
77 static inline void create_default_clip_region( DC * dc )
79 UINT width, height;
81 if (dc->header.type == OBJ_MEMDC)
83 BITMAP bitmap;
85 GetObjectW( dc->hBitmap, sizeof(bitmap), &bitmap );
86 width = bitmap.bmWidth;
87 height = bitmap.bmHeight;
89 else
91 width = GetDeviceCaps( dc->hSelf, DESKTOPHORZRES );
92 height = GetDeviceCaps( dc->hSelf, DESKTOPVERTRES );
94 dc->hClipRgn = CreateRectRgn( 0, 0, width, height );
98 /***********************************************************************
99 * SelectClipRgn (GDI32.@)
101 INT WINAPI SelectClipRgn( HDC hdc, HRGN hrgn )
103 return ExtSelectClipRgn( hdc, hrgn, RGN_COPY );
107 /******************************************************************************
108 * ExtSelectClipRgn [GDI32.@]
110 INT WINAPI ExtSelectClipRgn( HDC hdc, HRGN hrgn, INT fnMode )
112 INT retval;
113 RECT rect;
114 DC * dc = get_dc_ptr( hdc );
115 if (!dc) return ERROR;
117 TRACE("%p %p %d\n", hdc, hrgn, fnMode );
119 update_dc( dc );
120 if (dc->funcs->pExtSelectClipRgn)
122 retval = dc->funcs->pExtSelectClipRgn( dc->physDev, hrgn, fnMode );
123 release_dc_ptr( dc );
124 return retval;
127 if (!hrgn)
129 if (fnMode == RGN_COPY)
131 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
132 dc->hClipRgn = 0;
134 else
136 FIXME("Unimplemented: hrgn NULL in mode: %d\n", fnMode);
137 release_dc_ptr( dc );
138 return ERROR;
141 else
143 if (!dc->hClipRgn)
144 create_default_clip_region( dc );
146 if(fnMode == RGN_COPY)
147 CombineRgn( dc->hClipRgn, hrgn, 0, fnMode );
148 else
149 CombineRgn( dc->hClipRgn, dc->hClipRgn, hrgn, fnMode);
152 CLIPPING_UpdateGCRegion( dc );
153 release_dc_ptr( dc );
155 return GetClipBox(hdc, &rect);
158 /***********************************************************************
159 * __wine_set_visible_region (GDI32.@)
161 void CDECL __wine_set_visible_region( HDC hdc, HRGN hrgn, const RECT *vis_rect )
163 DC * dc;
165 if (!(dc = get_dc_ptr( hdc ))) return;
167 TRACE( "%p %p %s\n", hdc, hrgn, wine_dbgstr_rect(vis_rect) );
169 /* map region to DC coordinates */
170 OffsetRgn( hrgn, -vis_rect->left, -vis_rect->top );
172 DeleteObject( dc->hVisRgn );
173 dc->dirty = 0;
174 dc->vis_rect = *vis_rect;
175 dc->hVisRgn = hrgn;
176 CLIPPING_UpdateGCRegion( dc );
177 release_dc_ptr( dc );
181 /***********************************************************************
182 * OffsetClipRgn (GDI32.@)
184 INT WINAPI OffsetClipRgn( HDC hdc, INT x, INT y )
186 INT ret = SIMPLEREGION;
187 DC *dc = get_dc_ptr( hdc );
188 if (!dc) return ERROR;
190 TRACE("%p %d,%d\n", hdc, x, y );
192 update_dc( dc );
193 if(dc->funcs->pOffsetClipRgn)
195 ret = dc->funcs->pOffsetClipRgn( dc->physDev, x, y );
196 /* FIXME: ret is just a success flag, we should return a proper value */
198 else if (dc->hClipRgn) {
199 ret = OffsetRgn( dc->hClipRgn, MulDiv( x, dc->vportExtX, dc->wndExtX ),
200 MulDiv( y, dc->vportExtY, dc->wndExtY ) );
201 CLIPPING_UpdateGCRegion( dc );
203 release_dc_ptr( dc );
204 return ret;
208 /***********************************************************************
209 * ExcludeClipRect (GDI32.@)
211 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
212 INT right, INT bottom )
214 HRGN newRgn;
215 INT ret;
216 DC *dc = get_dc_ptr( hdc );
217 if (!dc) return ERROR;
219 TRACE("%p %dx%d,%dx%d\n", hdc, left, top, right, bottom );
221 update_dc( dc );
222 if(dc->funcs->pExcludeClipRect)
224 ret = dc->funcs->pExcludeClipRect( dc->physDev, left, top, right, bottom );
225 /* FIXME: ret is just a success flag, we should return a proper value */
227 else
229 POINT pt[2];
231 pt[0].x = left;
232 pt[0].y = top;
233 pt[1].x = right;
234 pt[1].y = bottom;
235 LPtoDP( hdc, pt, 2 );
236 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
237 else
239 if (!dc->hClipRgn)
240 create_default_clip_region( dc );
241 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_DIFF );
242 DeleteObject( newRgn );
244 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
246 release_dc_ptr( dc );
247 return ret;
251 /***********************************************************************
252 * IntersectClipRect (GDI32.@)
254 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
256 INT ret;
257 DC *dc = get_dc_ptr( hdc );
258 if (!dc) return ERROR;
260 TRACE("%p %d,%d - %d,%d\n", hdc, left, top, right, bottom );
262 update_dc( dc );
263 if(dc->funcs->pIntersectClipRect)
265 ret = dc->funcs->pIntersectClipRect( dc->physDev, left, top, right, bottom );
266 /* FIXME: ret is just a success flag, we should return a proper value */
268 else
270 POINT pt[2];
272 pt[0].x = left;
273 pt[0].y = top;
274 pt[1].x = right;
275 pt[1].y = bottom;
277 LPtoDP( hdc, pt, 2 );
279 if (!dc->hClipRgn)
281 dc->hClipRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y );
282 ret = SIMPLEREGION;
284 else
286 HRGN newRgn;
288 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
289 else
291 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_AND );
292 DeleteObject( newRgn );
295 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
297 release_dc_ptr( dc );
298 return ret;
302 /***********************************************************************
303 * PtVisible (GDI32.@)
305 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
307 POINT pt;
308 BOOL ret;
309 HRGN clip;
310 DC *dc = get_dc_ptr( hdc );
312 TRACE("%p %d,%d\n", hdc, x, y );
313 if (!dc) return FALSE;
315 pt.x = x;
316 pt.y = y;
317 LPtoDP( hdc, &pt, 1 );
318 update_dc( dc );
319 ret = PtInRegion( dc->hVisRgn, pt.x, pt.y );
320 if (ret && (clip = get_clip_region(dc))) ret = PtInRegion( clip, pt.x, pt.y );
321 release_dc_ptr( dc );
322 return ret;
326 /***********************************************************************
327 * RectVisible (GDI32.@)
329 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
331 RECT tmpRect;
332 BOOL ret;
333 HRGN clip;
334 DC *dc = get_dc_ptr( hdc );
335 if (!dc) return FALSE;
336 TRACE("%p %d,%dx%d,%d\n", hdc, rect->left, rect->top, rect->right, rect->bottom );
338 tmpRect = *rect;
339 LPtoDP( hdc, (POINT *)&tmpRect, 2 );
341 update_dc( dc );
342 if ((clip = get_clip_region(dc)))
344 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
345 CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
346 ret = RectInRegion( hrgn, &tmpRect );
347 DeleteObject( hrgn );
349 else ret = RectInRegion( dc->hVisRgn, &tmpRect );
350 release_dc_ptr( dc );
351 return ret;
355 /***********************************************************************
356 * GetClipBox (GDI32.@)
358 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
360 INT ret;
361 HRGN clip;
362 DC *dc = get_dc_ptr( hdc );
363 if (!dc) return ERROR;
365 update_dc( dc );
366 if ((clip = get_clip_region(dc)))
368 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
369 CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
370 ret = GetRgnBox( hrgn, rect );
371 DeleteObject( hrgn );
373 else ret = GetRgnBox( dc->hVisRgn, rect );
374 DPtoLP( hdc, (LPPOINT)rect, 2 );
375 release_dc_ptr( dc );
376 return ret;
380 /***********************************************************************
381 * GetClipRgn (GDI32.@)
383 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
385 INT ret = -1;
386 DC * dc;
387 if ((dc = get_dc_ptr( hdc )))
389 if( dc->hClipRgn )
391 if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR ) ret = 1;
393 else ret = 0;
394 release_dc_ptr( dc );
396 return ret;
400 /***********************************************************************
401 * GetMetaRgn (GDI32.@)
403 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
405 INT ret = 0;
406 DC * dc = get_dc_ptr( hdc );
408 if (dc)
410 if (dc->hMetaRgn && CombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY ) != ERROR)
411 ret = 1;
412 release_dc_ptr( dc );
414 return ret;
418 /***********************************************************************
419 * GetRandomRgn [GDI32.@]
421 * NOTES
422 * This function is documented in MSDN online for the case of
423 * iCode == SYSRGN (4).
425 * For iCode == 1 it should return the clip region
426 * 2 " " " the meta region
427 * 3 " " " the intersection of the clip with
428 * the meta region (== 'Rao' region).
430 * See http://www.codeproject.com/gdi/cliprgnguide.asp
432 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, INT iCode)
434 HRGN rgn;
435 DC *dc = get_dc_ptr( hDC );
437 if (!dc) return -1;
439 switch (iCode)
441 case 1:
442 rgn = dc->hClipRgn;
443 break;
444 case 2:
445 rgn = dc->hMetaRgn;
446 break;
447 case 3:
448 rgn = dc->hMetaClipRgn;
449 if(!rgn) rgn = dc->hClipRgn;
450 if(!rgn) rgn = dc->hMetaRgn;
451 break;
452 case SYSRGN: /* == 4 */
453 update_dc( dc );
454 rgn = dc->hVisRgn;
455 break;
456 default:
457 WARN("Unknown code %d\n", iCode);
458 release_dc_ptr( dc );
459 return -1;
461 if (rgn) CombineRgn( hRgn, rgn, 0, RGN_COPY );
462 release_dc_ptr( dc );
464 /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
465 if (iCode == SYSRGN && !(GetVersion() & 0x80000000))
467 POINT org;
468 GetDCOrgEx( hDC, &org );
469 OffsetRgn( hRgn, org.x, org.y );
471 return (rgn != 0);
475 /***********************************************************************
476 * SetMetaRgn (GDI32.@)
478 INT WINAPI SetMetaRgn( HDC hdc )
480 INT ret;
481 RECT dummy;
482 DC *dc = get_dc_ptr( hdc );
484 if (!dc) return ERROR;
486 if (dc->hMetaClipRgn)
488 /* the intersection becomes the new meta region */
489 DeleteObject( dc->hMetaRgn );
490 DeleteObject( dc->hClipRgn );
491 dc->hMetaRgn = dc->hMetaClipRgn;
492 dc->hClipRgn = 0;
493 dc->hMetaClipRgn = 0;
495 else if (dc->hClipRgn)
497 dc->hMetaRgn = dc->hClipRgn;
498 dc->hClipRgn = 0;
500 /* else nothing to do */
502 /* Note: no need to call CLIPPING_UpdateGCRegion, the overall clip region hasn't changed */
504 ret = GetRgnBox( dc->hMetaRgn, &dummy );
505 release_dc_ptr( dc );
506 return ret;