dinput: Move critical section to the base device class.
[wine/dcerpc.git] / dlls / gdi32 / clipping.c
blob40a556c14aa9d0eddf11ce5f2d2e8cd32c812c42
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 dc->hClipRgn = CreateRectRgn(0, 0,
90 GetDeviceCaps( dc->hSelf, HORZRES ),
91 GetDeviceCaps( dc->hSelf, VERTRES ));
95 /***********************************************************************
96 * SelectClipRgn (GDI32.@)
98 INT WINAPI SelectClipRgn( HDC hdc, HRGN hrgn )
100 return ExtSelectClipRgn( hdc, hrgn, RGN_COPY );
104 /******************************************************************************
105 * ExtSelectClipRgn [GDI32.@]
107 INT WINAPI ExtSelectClipRgn( HDC hdc, HRGN hrgn, INT fnMode )
109 INT retval;
110 RECT rect;
111 DC * dc = DC_GetDCUpdate( hdc );
112 if (!dc) return ERROR;
114 TRACE("%p %p %d\n", hdc, hrgn, fnMode );
116 if (dc->funcs->pExtSelectClipRgn)
118 retval = dc->funcs->pExtSelectClipRgn( dc->physDev, hrgn, fnMode );
119 GDI_ReleaseObj( hdc );
120 return retval;
123 if (!hrgn)
125 if (fnMode == RGN_COPY)
127 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
128 dc->hClipRgn = 0;
130 else
132 FIXME("Unimplemented: hrgn NULL in mode: %d\n", fnMode);
133 GDI_ReleaseObj( hdc );
134 return ERROR;
137 else
139 if (!dc->hClipRgn)
140 create_default_clip_region( dc );
142 if(fnMode == RGN_COPY)
143 CombineRgn( dc->hClipRgn, hrgn, 0, fnMode );
144 else
145 CombineRgn( dc->hClipRgn, dc->hClipRgn, hrgn, fnMode);
148 CLIPPING_UpdateGCRegion( dc );
149 GDI_ReleaseObj( hdc );
151 return GetClipBox(hdc, &rect);
154 /***********************************************************************
155 * SelectVisRgn (GDI.105)
157 INT16 WINAPI SelectVisRgn16( HDC16 hdc16, HRGN16 hrgn )
159 int retval;
160 HDC hdc = HDC_32( hdc16 );
161 DC * dc;
163 if (!hrgn) return ERROR;
164 if (!(dc = DC_GetDCPtr( hdc ))) return ERROR;
166 TRACE("%p %04x\n", hdc, hrgn );
168 dc->flags &= ~DC_DIRTY;
170 retval = CombineRgn( dc->hVisRgn, HRGN_32(hrgn), 0, RGN_COPY );
171 CLIPPING_UpdateGCRegion( dc );
172 GDI_ReleaseObj( hdc );
173 return retval;
177 /***********************************************************************
178 * OffsetClipRgn (GDI32.@)
180 INT WINAPI OffsetClipRgn( HDC hdc, INT x, INT y )
182 INT ret = SIMPLEREGION;
183 DC *dc = DC_GetDCUpdate( hdc );
184 if (!dc) return ERROR;
186 TRACE("%p %d,%d\n", hdc, x, y );
188 if(dc->funcs->pOffsetClipRgn)
190 ret = dc->funcs->pOffsetClipRgn( dc->physDev, x, y );
191 /* FIXME: ret is just a success flag, we should return a proper value */
193 else if (dc->hClipRgn) {
194 ret = OffsetRgn( dc->hClipRgn, MulDiv( x, dc->vportExtX, dc->wndExtX ),
195 MulDiv( y, dc->vportExtY, dc->wndExtY ) );
196 CLIPPING_UpdateGCRegion( dc );
198 GDI_ReleaseObj( hdc );
199 return ret;
203 /***********************************************************************
204 * OffsetVisRgn (GDI.102)
206 INT16 WINAPI OffsetVisRgn16( HDC16 hdc16, INT16 x, INT16 y )
208 INT16 retval;
209 HDC hdc = HDC_32( hdc16 );
210 DC * dc = DC_GetDCUpdate( hdc );
211 if (!dc) return ERROR;
212 TRACE("%p %d,%d\n", hdc, x, y );
213 retval = OffsetRgn( dc->hVisRgn, x, y );
214 CLIPPING_UpdateGCRegion( dc );
215 GDI_ReleaseObj( hdc );
216 return retval;
220 /***********************************************************************
221 * ExcludeClipRect (GDI32.@)
223 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
224 INT right, INT bottom )
226 HRGN newRgn;
227 INT ret;
228 DC *dc = DC_GetDCUpdate( hdc );
229 if (!dc) return ERROR;
231 TRACE("%p %dx%d,%dx%d\n", hdc, left, top, right, bottom );
233 if(dc->funcs->pExcludeClipRect)
235 ret = dc->funcs->pExcludeClipRect( dc->physDev, left, top, right, bottom );
236 /* FIXME: ret is just a success flag, we should return a proper value */
238 else
240 POINT pt[2];
242 pt[0].x = left;
243 pt[0].y = top;
244 pt[1].x = right;
245 pt[1].y = bottom;
246 LPtoDP( hdc, pt, 2 );
247 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
248 else
250 if (!dc->hClipRgn)
251 create_default_clip_region( dc );
252 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_DIFF );
253 DeleteObject( newRgn );
255 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
257 GDI_ReleaseObj( hdc );
258 return ret;
262 /***********************************************************************
263 * IntersectClipRect (GDI32.@)
265 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
267 INT ret;
268 DC *dc = DC_GetDCUpdate( hdc );
269 if (!dc) return ERROR;
271 TRACE("%p %d,%d - %d,%d\n", hdc, left, top, right, bottom );
273 if(dc->funcs->pIntersectClipRect)
275 ret = dc->funcs->pIntersectClipRect( dc->physDev, left, top, right, bottom );
276 /* FIXME: ret is just a success flag, we should return a proper value */
278 else
280 POINT pt[2];
282 pt[0].x = left;
283 pt[0].y = top;
284 pt[1].x = right;
285 pt[1].y = bottom;
287 LPtoDP( hdc, pt, 2 );
289 if (!dc->hClipRgn)
291 dc->hClipRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y );
292 ret = SIMPLEREGION;
294 else
296 HRGN newRgn;
298 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
299 else
301 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_AND );
302 DeleteObject( newRgn );
305 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
307 GDI_ReleaseObj( hdc );
308 return ret;
312 /***********************************************************************
313 * ExcludeVisRect (GDI.73)
315 INT16 WINAPI ExcludeVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
317 HRGN tempRgn;
318 INT16 ret;
319 POINT pt[2];
320 HDC hdc = HDC_32( hdc16 );
321 DC * dc = DC_GetDCUpdate( hdc );
322 if (!dc) return ERROR;
324 pt[0].x = left;
325 pt[0].y = top;
326 pt[1].x = right;
327 pt[1].y = bottom;
329 LPtoDP( hdc, pt, 2 );
331 TRACE("%p %d,%d - %d,%d\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
333 if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
334 else
336 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_DIFF );
337 DeleteObject( tempRgn );
339 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
340 GDI_ReleaseObj( hdc );
341 return ret;
345 /***********************************************************************
346 * IntersectVisRect (GDI.98)
348 INT16 WINAPI IntersectVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
350 HRGN tempRgn;
351 INT16 ret;
352 POINT pt[2];
353 HDC hdc = HDC_32( hdc16 );
354 DC * dc = DC_GetDCUpdate( hdc );
355 if (!dc) return ERROR;
357 pt[0].x = left;
358 pt[0].y = top;
359 pt[1].x = right;
360 pt[1].y = bottom;
362 LPtoDP( hdc, pt, 2 );
364 TRACE("%p %d,%d - %d,%d\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
367 if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
368 else
370 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_AND );
371 DeleteObject( tempRgn );
373 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
374 GDI_ReleaseObj( hdc );
375 return ret;
379 /***********************************************************************
380 * PtVisible (GDI32.@)
382 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
384 POINT pt;
385 BOOL ret;
386 HRGN clip;
387 DC *dc = DC_GetDCUpdate( hdc );
389 TRACE("%p %d,%d\n", hdc, x, y );
390 if (!dc) return FALSE;
392 pt.x = x;
393 pt.y = y;
394 LPtoDP( hdc, &pt, 1 );
395 ret = PtInRegion( dc->hVisRgn, pt.x, pt.y );
396 if (ret && (clip = get_clip_region(dc))) ret = PtInRegion( clip, pt.x, pt.y );
397 GDI_ReleaseObj( hdc );
398 return ret;
402 /***********************************************************************
403 * RectVisible (GDI32.@)
405 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
407 RECT tmpRect;
408 BOOL ret;
409 HRGN clip;
410 DC *dc = DC_GetDCUpdate( hdc );
411 if (!dc) return FALSE;
412 TRACE("%p %d,%dx%d,%d\n", hdc, rect->left, rect->top, rect->right, rect->bottom );
414 tmpRect = *rect;
415 LPtoDP( hdc, (POINT *)&tmpRect, 2 );
417 if ((clip = get_clip_region(dc)))
419 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
420 CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
421 ret = RectInRegion( hrgn, &tmpRect );
422 DeleteObject( hrgn );
424 else ret = RectInRegion( dc->hVisRgn, &tmpRect );
425 GDI_ReleaseObj( hdc );
426 return ret;
430 /***********************************************************************
431 * GetClipBox (GDI32.@)
433 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
435 INT ret;
436 HRGN clip;
437 DC *dc = DC_GetDCUpdate( hdc );
438 if (!dc) return ERROR;
439 if ((clip = get_clip_region(dc)))
441 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
442 CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
443 ret = GetRgnBox( hrgn, rect );
444 DeleteObject( hrgn );
446 else ret = GetRgnBox( dc->hVisRgn, rect );
447 DPtoLP( hdc, (LPPOINT)rect, 2 );
448 GDI_ReleaseObj( hdc );
449 return ret;
453 /***********************************************************************
454 * GetClipRgn (GDI32.@)
456 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
458 INT ret = -1;
459 DC * dc;
460 if (hRgn && (dc = DC_GetDCPtr( hdc )))
462 if( dc->hClipRgn )
464 if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR ) ret = 1;
466 else ret = 0;
467 GDI_ReleaseObj( hdc );
469 return ret;
473 /***********************************************************************
474 * GetMetaRgn (GDI32.@)
476 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
478 INT ret = 0;
479 DC * dc = DC_GetDCPtr( hdc );
481 if (dc)
483 if (dc->hMetaRgn && CombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY ) != ERROR)
484 ret = 1;
485 GDI_ReleaseObj( hdc );
487 return ret;
491 /***********************************************************************
492 * SaveVisRgn (GDI.129)
494 HRGN16 WINAPI SaveVisRgn16( HDC16 hdc16 )
496 struct saved_visrgn *saved;
497 HDC hdc = HDC_32( hdc16 );
498 DC *dc = DC_GetDCUpdate( hdc );
500 if (!dc) return 0;
501 TRACE("%p\n", hdc );
503 if (!(saved = HeapAlloc( GetProcessHeap(), 0, sizeof(*saved) ))) goto error;
504 if (!(saved->hrgn = CreateRectRgn( 0, 0, 0, 0 ))) goto error;
505 CombineRgn( saved->hrgn, dc->hVisRgn, 0, RGN_COPY );
506 saved->next = dc->saved_visrgn;
507 dc->saved_visrgn = saved;
508 GDI_ReleaseObj( hdc );
509 return HRGN_16(saved->hrgn);
511 error:
512 GDI_ReleaseObj( hdc );
513 HeapFree( GetProcessHeap(), 0, saved );
514 return 0;
518 /***********************************************************************
519 * RestoreVisRgn (GDI.130)
521 INT16 WINAPI RestoreVisRgn16( HDC16 hdc16 )
523 struct saved_visrgn *saved;
524 HDC hdc = HDC_32( hdc16 );
525 DC *dc = DC_GetDCPtr( hdc );
526 INT16 ret = ERROR;
528 if (!dc) return ERROR;
530 TRACE("%p\n", hdc );
532 if (!(saved = dc->saved_visrgn)) goto done;
534 ret = CombineRgn( dc->hVisRgn, saved->hrgn, 0, RGN_COPY );
535 dc->saved_visrgn = saved->next;
536 DeleteObject( saved->hrgn );
537 HeapFree( GetProcessHeap(), 0, saved );
538 dc->flags &= ~DC_DIRTY;
539 CLIPPING_UpdateGCRegion( dc );
540 done:
541 GDI_ReleaseObj( hdc );
542 return ret;
546 /***********************************************************************
547 * GetRandomRgn [GDI32.@]
549 * NOTES
550 * This function is documented in MSDN online for the case of
551 * iCode == SYSRGN (4).
553 * For iCode == 1 it should return the clip region
554 * 2 " " " the meta region
555 * 3 " " " the intersection of the clip with
556 * the meta region (== 'Rao' region).
558 * See http://www.codeproject.com/gdi/cliprgnguide.asp
560 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, INT iCode)
562 HRGN rgn;
563 DC *dc = DC_GetDCPtr( hDC );
565 if (!dc) return -1;
567 switch (iCode)
569 case 1:
570 rgn = dc->hClipRgn;
571 break;
572 case 2:
573 rgn = dc->hMetaRgn;
574 break;
575 case 3:
576 rgn = dc->hMetaClipRgn;
577 if(!rgn) rgn = dc->hClipRgn;
578 if(!rgn) rgn = dc->hMetaRgn;
579 break;
580 case SYSRGN: /* == 4 */
581 rgn = dc->hVisRgn;
582 break;
583 default:
584 WARN("Unknown code %d\n", iCode);
585 GDI_ReleaseObj( hDC );
586 return -1;
588 if (rgn) CombineRgn( hRgn, rgn, 0, RGN_COPY );
589 GDI_ReleaseObj( hDC );
591 /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
592 if (iCode == SYSRGN && !(GetVersion() & 0x80000000))
594 POINT org;
595 GetDCOrgEx( hDC, &org );
596 OffsetRgn( hRgn, org.x, org.y );
598 return (rgn != 0);
602 /***********************************************************************
603 * SetMetaRgn (GDI32.@)
605 INT WINAPI SetMetaRgn( HDC hdc )
607 INT ret;
608 RECT dummy;
609 DC *dc = DC_GetDCPtr( hdc );
611 if (!dc) return ERROR;
613 if (dc->hMetaClipRgn)
615 /* the intersection becomes the new meta region */
616 DeleteObject( dc->hMetaRgn );
617 DeleteObject( dc->hClipRgn );
618 dc->hMetaRgn = dc->hMetaClipRgn;
619 dc->hClipRgn = 0;
620 dc->hMetaClipRgn = 0;
622 else if (dc->hClipRgn)
624 dc->hMetaRgn = dc->hClipRgn;
625 dc->hClipRgn = 0;
627 /* else nothing to do */
629 /* Note: no need to call CLIPPING_UpdateGCRegion, the overall clip region hasn't changed */
631 ret = GetRgnBox( dc->hMetaRgn, &dummy );
632 GDI_ReleaseObj( hdc );
633 return ret;