wined3d: Remove some unneeded context cache members from the device.
[wine/multimedia.git] / dlls / gdi32 / clipping.c
blobfc59c181f88660e64a347f567535d7749d6fc22e
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 "wine/winuser16.h"
28 #include "gdi_private.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(clipping);
34 /***********************************************************************
35 * get_clip_region
37 * Return the total clip region (if any).
39 static inline HRGN get_clip_region( DC * dc )
41 if (dc->hMetaClipRgn) return dc->hMetaClipRgn;
42 if (dc->hMetaRgn) return dc->hMetaRgn;
43 return dc->hClipRgn;
47 /***********************************************************************
48 * CLIPPING_UpdateGCRegion
50 * Update the GC clip region when the ClipRgn or VisRgn have changed.
52 void CLIPPING_UpdateGCRegion( DC * dc )
54 HRGN clip_rgn;
56 if (!dc->hVisRgn)
58 ERR("hVisRgn is zero. Please report this.\n" );
59 exit(1);
62 if (dc->flags & DC_DIRTY) ERR( "DC is dirty. Please report this.\n" );
64 /* update the intersection of meta and clip regions */
65 if (dc->hMetaRgn && dc->hClipRgn)
67 if (!dc->hMetaClipRgn) dc->hMetaClipRgn = CreateRectRgn( 0, 0, 0, 0 );
68 CombineRgn( dc->hMetaClipRgn, dc->hClipRgn, dc->hMetaRgn, RGN_AND );
69 clip_rgn = dc->hMetaClipRgn;
71 else /* only one is set, no need for an intersection */
73 if (dc->hMetaClipRgn) DeleteObject( dc->hMetaClipRgn );
74 dc->hMetaClipRgn = 0;
75 clip_rgn = dc->hMetaRgn ? dc->hMetaRgn : dc->hClipRgn;
78 if (dc->funcs->pSetDeviceClipping)
79 dc->funcs->pSetDeviceClipping( dc->physDev, dc->hVisRgn, clip_rgn );
82 /***********************************************************************
83 * create_default_clip_region
85 * Create a default clipping region when none already exists.
87 static inline void create_default_clip_region( DC * dc )
89 UINT width, height;
91 if (GDIMAGIC( dc->header.wMagic ) == MEMORY_DC_MAGIC)
93 BITMAP bitmap;
95 GetObjectW( dc->hBitmap, sizeof(bitmap), &bitmap );
96 width = bitmap.bmWidth;
97 height = bitmap.bmHeight;
99 else
101 width = GetDeviceCaps( dc->hSelf, DESKTOPHORZRES );
102 height = GetDeviceCaps( dc->hSelf, DESKTOPVERTRES );
104 dc->hClipRgn = CreateRectRgn( 0, 0, width, height );
108 /***********************************************************************
109 * SelectClipRgn (GDI32.@)
111 INT WINAPI SelectClipRgn( HDC hdc, HRGN hrgn )
113 return ExtSelectClipRgn( hdc, hrgn, RGN_COPY );
117 /******************************************************************************
118 * ExtSelectClipRgn [GDI32.@]
120 INT WINAPI ExtSelectClipRgn( HDC hdc, HRGN hrgn, INT fnMode )
122 INT retval;
123 RECT rect;
124 DC * dc = DC_GetDCUpdate( hdc );
125 if (!dc) return ERROR;
127 TRACE("%p %p %d\n", hdc, hrgn, fnMode );
129 if (dc->funcs->pExtSelectClipRgn)
131 retval = dc->funcs->pExtSelectClipRgn( dc->physDev, hrgn, fnMode );
132 GDI_ReleaseObj( hdc );
133 return retval;
136 if (!hrgn)
138 if (fnMode == RGN_COPY)
140 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
141 dc->hClipRgn = 0;
143 else
145 FIXME("Unimplemented: hrgn NULL in mode: %d\n", fnMode);
146 GDI_ReleaseObj( hdc );
147 return ERROR;
150 else
152 if (!dc->hClipRgn)
153 create_default_clip_region( dc );
155 if(fnMode == RGN_COPY)
156 CombineRgn( dc->hClipRgn, hrgn, 0, fnMode );
157 else
158 CombineRgn( dc->hClipRgn, dc->hClipRgn, hrgn, fnMode);
161 CLIPPING_UpdateGCRegion( dc );
162 GDI_ReleaseObj( hdc );
164 return GetClipBox(hdc, &rect);
167 /***********************************************************************
168 * SelectVisRgn (GDI.105)
170 INT16 WINAPI SelectVisRgn16( HDC16 hdc16, HRGN16 hrgn )
172 int retval;
173 HDC hdc = HDC_32( hdc16 );
174 DC * dc;
176 if (!hrgn) return ERROR;
177 if (!(dc = DC_GetDCPtr( hdc ))) return ERROR;
179 TRACE("%p %04x\n", hdc, hrgn );
181 dc->flags &= ~DC_DIRTY;
183 retval = CombineRgn( dc->hVisRgn, HRGN_32(hrgn), 0, RGN_COPY );
184 CLIPPING_UpdateGCRegion( dc );
185 GDI_ReleaseObj( hdc );
186 return retval;
190 /***********************************************************************
191 * OffsetClipRgn (GDI32.@)
193 INT WINAPI OffsetClipRgn( HDC hdc, INT x, INT y )
195 INT ret = SIMPLEREGION;
196 DC *dc = DC_GetDCUpdate( hdc );
197 if (!dc) return ERROR;
199 TRACE("%p %d,%d\n", hdc, x, y );
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 GDI_ReleaseObj( hdc );
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 = DC_GetDCUpdate( hdc );
224 if (!dc) return ERROR;
225 TRACE("%p %d,%d\n", hdc, x, y );
226 retval = OffsetRgn( dc->hVisRgn, x, y );
227 CLIPPING_UpdateGCRegion( dc );
228 GDI_ReleaseObj( hdc );
229 return retval;
233 /***********************************************************************
234 * ExcludeClipRect (GDI32.@)
236 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
237 INT right, INT bottom )
239 HRGN newRgn;
240 INT ret;
241 DC *dc = DC_GetDCUpdate( hdc );
242 if (!dc) return ERROR;
244 TRACE("%p %dx%d,%dx%d\n", hdc, left, top, right, bottom );
246 if(dc->funcs->pExcludeClipRect)
248 ret = dc->funcs->pExcludeClipRect( dc->physDev, left, top, right, bottom );
249 /* FIXME: ret is just a success flag, we should return a proper value */
251 else
253 POINT pt[2];
255 pt[0].x = left;
256 pt[0].y = top;
257 pt[1].x = right;
258 pt[1].y = bottom;
259 LPtoDP( hdc, pt, 2 );
260 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
261 else
263 if (!dc->hClipRgn)
264 create_default_clip_region( dc );
265 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_DIFF );
266 DeleteObject( newRgn );
268 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
270 GDI_ReleaseObj( hdc );
271 return ret;
275 /***********************************************************************
276 * IntersectClipRect (GDI32.@)
278 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
280 INT ret;
281 DC *dc = DC_GetDCUpdate( hdc );
282 if (!dc) return ERROR;
284 TRACE("%p %d,%d - %d,%d\n", hdc, left, top, right, bottom );
286 if(dc->funcs->pIntersectClipRect)
288 ret = dc->funcs->pIntersectClipRect( dc->physDev, left, top, right, bottom );
289 /* FIXME: ret is just a success flag, we should return a proper value */
291 else
293 POINT pt[2];
295 pt[0].x = left;
296 pt[0].y = top;
297 pt[1].x = right;
298 pt[1].y = bottom;
300 LPtoDP( hdc, pt, 2 );
302 if (!dc->hClipRgn)
304 dc->hClipRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y );
305 ret = SIMPLEREGION;
307 else
309 HRGN newRgn;
311 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
312 else
314 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_AND );
315 DeleteObject( newRgn );
318 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
320 GDI_ReleaseObj( hdc );
321 return ret;
325 /***********************************************************************
326 * ExcludeVisRect (GDI.73)
328 INT16 WINAPI ExcludeVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
330 HRGN tempRgn;
331 INT16 ret;
332 POINT pt[2];
333 HDC hdc = HDC_32( hdc16 );
334 DC * dc = DC_GetDCUpdate( hdc );
335 if (!dc) return ERROR;
337 pt[0].x = left;
338 pt[0].y = top;
339 pt[1].x = right;
340 pt[1].y = bottom;
342 LPtoDP( hdc, pt, 2 );
344 TRACE("%p %d,%d - %d,%d\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
346 if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
347 else
349 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_DIFF );
350 DeleteObject( tempRgn );
352 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
353 GDI_ReleaseObj( hdc );
354 return ret;
358 /***********************************************************************
359 * IntersectVisRect (GDI.98)
361 INT16 WINAPI IntersectVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
363 HRGN tempRgn;
364 INT16 ret;
365 POINT pt[2];
366 HDC hdc = HDC_32( hdc16 );
367 DC * dc = DC_GetDCUpdate( hdc );
368 if (!dc) return ERROR;
370 pt[0].x = left;
371 pt[0].y = top;
372 pt[1].x = right;
373 pt[1].y = bottom;
375 LPtoDP( hdc, pt, 2 );
377 TRACE("%p %d,%d - %d,%d\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
380 if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
381 else
383 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_AND );
384 DeleteObject( tempRgn );
386 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
387 GDI_ReleaseObj( hdc );
388 return ret;
392 /***********************************************************************
393 * PtVisible (GDI32.@)
395 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
397 POINT pt;
398 BOOL ret;
399 HRGN clip;
400 DC *dc = DC_GetDCUpdate( hdc );
402 TRACE("%p %d,%d\n", hdc, x, y );
403 if (!dc) return FALSE;
405 pt.x = x;
406 pt.y = y;
407 LPtoDP( hdc, &pt, 1 );
408 ret = PtInRegion( dc->hVisRgn, pt.x, pt.y );
409 if (ret && (clip = get_clip_region(dc))) ret = PtInRegion( clip, pt.x, pt.y );
410 GDI_ReleaseObj( hdc );
411 return ret;
415 /***********************************************************************
416 * RectVisible (GDI32.@)
418 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
420 RECT tmpRect;
421 BOOL ret;
422 HRGN clip;
423 DC *dc = DC_GetDCUpdate( hdc );
424 if (!dc) return FALSE;
425 TRACE("%p %d,%dx%d,%d\n", hdc, rect->left, rect->top, rect->right, rect->bottom );
427 tmpRect = *rect;
428 LPtoDP( hdc, (POINT *)&tmpRect, 2 );
430 if ((clip = get_clip_region(dc)))
432 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
433 CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
434 ret = RectInRegion( hrgn, &tmpRect );
435 DeleteObject( hrgn );
437 else ret = RectInRegion( dc->hVisRgn, &tmpRect );
438 GDI_ReleaseObj( hdc );
439 return ret;
443 /***********************************************************************
444 * GetClipBox (GDI32.@)
446 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
448 INT ret;
449 HRGN clip;
450 DC *dc = DC_GetDCUpdate( hdc );
451 if (!dc) return ERROR;
452 if ((clip = get_clip_region(dc)))
454 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
455 CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
456 ret = GetRgnBox( hrgn, rect );
457 DeleteObject( hrgn );
459 else ret = GetRgnBox( dc->hVisRgn, rect );
460 DPtoLP( hdc, (LPPOINT)rect, 2 );
461 GDI_ReleaseObj( hdc );
462 return ret;
466 /***********************************************************************
467 * GetClipRgn (GDI32.@)
469 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
471 INT ret = -1;
472 DC * dc;
473 if (hRgn && (dc = DC_GetDCPtr( hdc )))
475 if( dc->hClipRgn )
477 if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR ) ret = 1;
479 else ret = 0;
480 GDI_ReleaseObj( hdc );
482 return ret;
486 /***********************************************************************
487 * GetMetaRgn (GDI32.@)
489 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
491 INT ret = 0;
492 DC * dc = DC_GetDCPtr( hdc );
494 if (dc)
496 if (dc->hMetaRgn && CombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY ) != ERROR)
497 ret = 1;
498 GDI_ReleaseObj( hdc );
500 return ret;
504 /***********************************************************************
505 * SaveVisRgn (GDI.129)
507 HRGN16 WINAPI SaveVisRgn16( HDC16 hdc16 )
509 struct saved_visrgn *saved;
510 HDC hdc = HDC_32( hdc16 );
511 DC *dc = DC_GetDCUpdate( hdc );
513 if (!dc) return 0;
514 TRACE("%p\n", hdc );
516 if (!(saved = HeapAlloc( GetProcessHeap(), 0, sizeof(*saved) ))) goto error;
517 if (!(saved->hrgn = CreateRectRgn( 0, 0, 0, 0 ))) goto error;
518 CombineRgn( saved->hrgn, dc->hVisRgn, 0, RGN_COPY );
519 saved->next = dc->saved_visrgn;
520 dc->saved_visrgn = saved;
521 GDI_ReleaseObj( hdc );
522 return HRGN_16(saved->hrgn);
524 error:
525 GDI_ReleaseObj( hdc );
526 HeapFree( GetProcessHeap(), 0, saved );
527 return 0;
531 /***********************************************************************
532 * RestoreVisRgn (GDI.130)
534 INT16 WINAPI RestoreVisRgn16( HDC16 hdc16 )
536 struct saved_visrgn *saved;
537 HDC hdc = HDC_32( hdc16 );
538 DC *dc = DC_GetDCPtr( hdc );
539 INT16 ret = ERROR;
541 if (!dc) return ERROR;
543 TRACE("%p\n", hdc );
545 if (!(saved = dc->saved_visrgn)) goto done;
547 ret = CombineRgn( dc->hVisRgn, saved->hrgn, 0, RGN_COPY );
548 dc->saved_visrgn = saved->next;
549 DeleteObject( saved->hrgn );
550 HeapFree( GetProcessHeap(), 0, saved );
551 dc->flags &= ~DC_DIRTY;
552 CLIPPING_UpdateGCRegion( dc );
553 done:
554 GDI_ReleaseObj( hdc );
555 return ret;
559 /***********************************************************************
560 * GetRandomRgn [GDI32.@]
562 * NOTES
563 * This function is documented in MSDN online for the case of
564 * iCode == SYSRGN (4).
566 * For iCode == 1 it should return the clip region
567 * 2 " " " the meta region
568 * 3 " " " the intersection of the clip with
569 * the meta region (== 'Rao' region).
571 * See http://www.codeproject.com/gdi/cliprgnguide.asp
573 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, INT iCode)
575 HRGN rgn;
576 DC *dc = DC_GetDCPtr( hDC );
578 if (!dc) return -1;
580 switch (iCode)
582 case 1:
583 rgn = dc->hClipRgn;
584 break;
585 case 2:
586 rgn = dc->hMetaRgn;
587 break;
588 case 3:
589 rgn = dc->hMetaClipRgn;
590 if(!rgn) rgn = dc->hClipRgn;
591 if(!rgn) rgn = dc->hMetaRgn;
592 break;
593 case SYSRGN: /* == 4 */
594 rgn = dc->hVisRgn;
595 break;
596 default:
597 WARN("Unknown code %d\n", iCode);
598 GDI_ReleaseObj( hDC );
599 return -1;
601 if (rgn) CombineRgn( hRgn, rgn, 0, RGN_COPY );
602 GDI_ReleaseObj( hDC );
604 /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
605 if (iCode == SYSRGN && !(GetVersion() & 0x80000000))
607 POINT org;
608 GetDCOrgEx( hDC, &org );
609 OffsetRgn( hRgn, org.x, org.y );
611 return (rgn != 0);
615 /***********************************************************************
616 * SetMetaRgn (GDI32.@)
618 INT WINAPI SetMetaRgn( HDC hdc )
620 INT ret;
621 RECT dummy;
622 DC *dc = DC_GetDCPtr( hdc );
624 if (!dc) return ERROR;
626 if (dc->hMetaClipRgn)
628 /* the intersection becomes the new meta region */
629 DeleteObject( dc->hMetaRgn );
630 DeleteObject( dc->hClipRgn );
631 dc->hMetaRgn = dc->hMetaClipRgn;
632 dc->hClipRgn = 0;
633 dc->hMetaClipRgn = 0;
635 else if (dc->hClipRgn)
637 dc->hMetaRgn = dc->hClipRgn;
638 dc->hClipRgn = 0;
640 /* else nothing to do */
642 /* Note: no need to call CLIPPING_UpdateGCRegion, the overall clip region hasn't changed */
644 ret = GetRgnBox( dc->hMetaRgn, &dummy );
645 GDI_ReleaseObj( hdc );
646 return ret;