appwiz.cpl: Use the fancy new icon.
[wine/multimedia.git] / dlls / gdi32 / clipping.c
bloba942fe19cf824d4617e158f9f933713012d54c7e
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 * SelectVisRgn (GDI32.@)
161 * Note: not exported on Windows, only the 16-bit version is exported.
163 INT WINAPI SelectVisRgn( HDC hdc, HRGN hrgn )
165 int retval;
166 DC * dc;
168 if (!hrgn) return ERROR;
169 if (!(dc = get_dc_ptr( hdc ))) return ERROR;
171 TRACE("%p %p\n", hdc, hrgn );
173 dc->dirty = 0;
175 retval = CombineRgn( dc->hVisRgn, hrgn, 0, RGN_COPY );
176 CLIPPING_UpdateGCRegion( dc );
177 release_dc_ptr( dc );
178 return retval;
182 /***********************************************************************
183 * OffsetClipRgn (GDI32.@)
185 INT WINAPI OffsetClipRgn( HDC hdc, INT x, INT y )
187 INT ret = SIMPLEREGION;
188 DC *dc = get_dc_ptr( hdc );
189 if (!dc) return ERROR;
191 TRACE("%p %d,%d\n", hdc, x, y );
193 update_dc( dc );
194 if(dc->funcs->pOffsetClipRgn)
196 ret = dc->funcs->pOffsetClipRgn( dc->physDev, x, y );
197 /* FIXME: ret is just a success flag, we should return a proper value */
199 else if (dc->hClipRgn) {
200 ret = OffsetRgn( dc->hClipRgn, MulDiv( x, dc->vportExtX, dc->wndExtX ),
201 MulDiv( y, dc->vportExtY, dc->wndExtY ) );
202 CLIPPING_UpdateGCRegion( dc );
204 release_dc_ptr( dc );
205 return ret;
209 /***********************************************************************
210 * ExcludeClipRect (GDI32.@)
212 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
213 INT right, INT bottom )
215 HRGN newRgn;
216 INT ret;
217 DC *dc = get_dc_ptr( hdc );
218 if (!dc) return ERROR;
220 TRACE("%p %dx%d,%dx%d\n", hdc, left, top, right, bottom );
222 update_dc( dc );
223 if(dc->funcs->pExcludeClipRect)
225 ret = dc->funcs->pExcludeClipRect( dc->physDev, left, top, right, bottom );
226 /* FIXME: ret is just a success flag, we should return a proper value */
228 else
230 POINT pt[2];
232 pt[0].x = left;
233 pt[0].y = top;
234 pt[1].x = right;
235 pt[1].y = bottom;
236 LPtoDP( hdc, pt, 2 );
237 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
238 else
240 if (!dc->hClipRgn)
241 create_default_clip_region( dc );
242 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_DIFF );
243 DeleteObject( newRgn );
245 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
247 release_dc_ptr( dc );
248 return ret;
252 /***********************************************************************
253 * IntersectClipRect (GDI32.@)
255 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
257 INT ret;
258 DC *dc = get_dc_ptr( hdc );
259 if (!dc) return ERROR;
261 TRACE("%p %d,%d - %d,%d\n", hdc, left, top, right, bottom );
263 update_dc( dc );
264 if(dc->funcs->pIntersectClipRect)
266 ret = dc->funcs->pIntersectClipRect( dc->physDev, left, top, right, bottom );
267 /* FIXME: ret is just a success flag, we should return a proper value */
269 else
271 POINT pt[2];
273 pt[0].x = left;
274 pt[0].y = top;
275 pt[1].x = right;
276 pt[1].y = bottom;
278 LPtoDP( hdc, pt, 2 );
280 if (!dc->hClipRgn)
282 dc->hClipRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y );
283 ret = SIMPLEREGION;
285 else
287 HRGN newRgn;
289 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
290 else
292 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_AND );
293 DeleteObject( newRgn );
296 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
298 release_dc_ptr( dc );
299 return ret;
303 /***********************************************************************
304 * PtVisible (GDI32.@)
306 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
308 POINT pt;
309 BOOL ret;
310 HRGN clip;
311 DC *dc = get_dc_ptr( hdc );
313 TRACE("%p %d,%d\n", hdc, x, y );
314 if (!dc) return FALSE;
316 pt.x = x;
317 pt.y = y;
318 LPtoDP( hdc, &pt, 1 );
319 update_dc( dc );
320 ret = PtInRegion( dc->hVisRgn, pt.x, pt.y );
321 if (ret && (clip = get_clip_region(dc))) ret = PtInRegion( clip, pt.x, pt.y );
322 release_dc_ptr( dc );
323 return ret;
327 /***********************************************************************
328 * RectVisible (GDI32.@)
330 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
332 RECT tmpRect;
333 BOOL ret;
334 HRGN clip;
335 DC *dc = get_dc_ptr( hdc );
336 if (!dc) return FALSE;
337 TRACE("%p %d,%dx%d,%d\n", hdc, rect->left, rect->top, rect->right, rect->bottom );
339 tmpRect = *rect;
340 LPtoDP( hdc, (POINT *)&tmpRect, 2 );
342 update_dc( dc );
343 if ((clip = get_clip_region(dc)))
345 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
346 CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
347 ret = RectInRegion( hrgn, &tmpRect );
348 DeleteObject( hrgn );
350 else ret = RectInRegion( dc->hVisRgn, &tmpRect );
351 release_dc_ptr( dc );
352 return ret;
356 /***********************************************************************
357 * GetClipBox (GDI32.@)
359 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
361 INT ret;
362 HRGN clip;
363 DC *dc = get_dc_ptr( hdc );
364 if (!dc) return ERROR;
366 update_dc( dc );
367 if ((clip = get_clip_region(dc)))
369 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
370 CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
371 ret = GetRgnBox( hrgn, rect );
372 DeleteObject( hrgn );
374 else ret = GetRgnBox( dc->hVisRgn, rect );
375 DPtoLP( hdc, (LPPOINT)rect, 2 );
376 release_dc_ptr( dc );
377 return ret;
381 /***********************************************************************
382 * GetClipRgn (GDI32.@)
384 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
386 INT ret = -1;
387 DC * dc;
388 if ((dc = get_dc_ptr( hdc )))
390 if( dc->hClipRgn )
392 if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR ) ret = 1;
394 else ret = 0;
395 release_dc_ptr( dc );
397 return ret;
401 /***********************************************************************
402 * GetMetaRgn (GDI32.@)
404 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
406 INT ret = 0;
407 DC * dc = get_dc_ptr( hdc );
409 if (dc)
411 if (dc->hMetaRgn && CombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY ) != ERROR)
412 ret = 1;
413 release_dc_ptr( dc );
415 return ret;
419 /***********************************************************************
420 * GetRandomRgn [GDI32.@]
422 * NOTES
423 * This function is documented in MSDN online for the case of
424 * iCode == SYSRGN (4).
426 * For iCode == 1 it should return the clip region
427 * 2 " " " the meta region
428 * 3 " " " the intersection of the clip with
429 * the meta region (== 'Rao' region).
431 * See http://www.codeproject.com/gdi/cliprgnguide.asp
433 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, INT iCode)
435 HRGN rgn;
436 DC *dc = get_dc_ptr( hDC );
438 if (!dc) return -1;
440 switch (iCode)
442 case 1:
443 rgn = dc->hClipRgn;
444 break;
445 case 2:
446 rgn = dc->hMetaRgn;
447 break;
448 case 3:
449 rgn = dc->hMetaClipRgn;
450 if(!rgn) rgn = dc->hClipRgn;
451 if(!rgn) rgn = dc->hMetaRgn;
452 break;
453 case SYSRGN: /* == 4 */
454 update_dc( dc );
455 rgn = dc->hVisRgn;
456 break;
457 default:
458 WARN("Unknown code %d\n", iCode);
459 release_dc_ptr( dc );
460 return -1;
462 if (rgn) CombineRgn( hRgn, rgn, 0, RGN_COPY );
463 release_dc_ptr( dc );
465 /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
466 if (iCode == SYSRGN && !(GetVersion() & 0x80000000))
468 POINT org;
469 GetDCOrgEx( hDC, &org );
470 OffsetRgn( hRgn, org.x, org.y );
472 return (rgn != 0);
476 /***********************************************************************
477 * SetMetaRgn (GDI32.@)
479 INT WINAPI SetMetaRgn( HDC hdc )
481 INT ret;
482 RECT dummy;
483 DC *dc = get_dc_ptr( hdc );
485 if (!dc) return ERROR;
487 if (dc->hMetaClipRgn)
489 /* the intersection becomes the new meta region */
490 DeleteObject( dc->hMetaRgn );
491 DeleteObject( dc->hClipRgn );
492 dc->hMetaRgn = dc->hMetaClipRgn;
493 dc->hClipRgn = 0;
494 dc->hMetaClipRgn = 0;
496 else if (dc->hClipRgn)
498 dc->hMetaRgn = dc->hClipRgn;
499 dc->hClipRgn = 0;
501 /* else nothing to do */
503 /* Note: no need to call CLIPPING_UpdateGCRegion, the overall clip region hasn't changed */
505 ret = GetRgnBox( dc->hMetaRgn, &dummy );
506 release_dc_ptr( dc );
507 return ret;