mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / gdi32 / clipping.c
blob7ff77a60db59f30dcbe350e1f2e29be516065636
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 /* return the DC device rectangle if not empty */
33 static inline BOOL get_dc_device_rect( DC *dc, RECT *rect )
35 *rect = dc->device_rect;
36 offset_rect( rect, -dc->vis_rect.left, -dc->vis_rect.top );
37 return !is_rect_empty( rect );
40 /***********************************************************************
41 * get_clip_rect
43 * Compute a clip rectangle from its logical coordinates.
45 static inline RECT get_clip_rect( DC * dc, int left, int top, int right, int bottom )
47 RECT rect;
49 rect.left = left;
50 rect.top = top;
51 rect.right = right;
52 rect.bottom = bottom;
53 lp_to_dp( dc, (POINT *)&rect, 2 );
54 if (dc->layout & LAYOUT_RTL)
56 int tmp = rect.left;
57 rect.left = rect.right + 1;
58 rect.right = tmp + 1;
60 return rect;
63 /***********************************************************************
64 * clip_device_rect
66 * Clip a rectangle to the whole DC surface.
68 BOOL clip_device_rect( DC *dc, RECT *dst, const RECT *src )
70 RECT clip;
72 if (get_dc_device_rect( dc, &clip )) return intersect_rect( dst, src, &clip );
73 *dst = *src;
74 return TRUE;
77 /***********************************************************************
78 * clip_visrect
80 * Clip a rectangle to the DC visible rect.
82 BOOL clip_visrect( DC *dc, RECT *dst, const RECT *src )
84 RECT clip;
86 if (!clip_device_rect( dc, dst, src )) return FALSE;
87 if (GetRgnBox( get_dc_region(dc), &clip )) return intersect_rect( dst, dst, &clip );
88 return TRUE;
91 /***********************************************************************
92 * update_dc_clipping
94 * Update the DC and device clip regions when the ClipRgn or VisRgn have changed.
96 void update_dc_clipping( DC * dc )
98 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetDeviceClipping );
99 HRGN regions[3];
100 int count = 0;
102 if (dc->hVisRgn) regions[count++] = dc->hVisRgn;
103 if (dc->hClipRgn) regions[count++] = dc->hClipRgn;
104 if (dc->hMetaRgn) regions[count++] = dc->hMetaRgn;
106 if (count > 1)
108 if (!dc->region) dc->region = CreateRectRgn( 0, 0, 0, 0 );
109 CombineRgn( dc->region, regions[0], regions[1], RGN_AND );
110 if (count > 2) CombineRgn( dc->region, dc->region, regions[2], RGN_AND );
112 else /* only one region, we don't need the total region */
114 if (dc->region) DeleteObject( dc->region );
115 dc->region = 0;
117 physdev->funcs->pSetDeviceClipping( physdev, get_dc_region( dc ));
120 /***********************************************************************
121 * create_default_clip_region
123 * Create a default clipping region when none already exists.
125 static inline void create_default_clip_region( DC * dc )
127 RECT rect;
129 if (!get_dc_device_rect( dc, &rect ))
131 rect.left = 0;
132 rect.top = 0;
133 rect.right = GetDeviceCaps( dc->hSelf, DESKTOPHORZRES );
134 rect.bottom = GetDeviceCaps( dc->hSelf, DESKTOPVERTRES );
136 dc->hClipRgn = CreateRectRgnIndirect( &rect );
140 /***********************************************************************
141 * null driver fallback implementations
144 INT CDECL nulldrv_ExtSelectClipRgn( PHYSDEV dev, HRGN rgn, INT mode )
146 DC *dc = get_nulldrv_dc( dev );
147 INT ret;
149 if (!rgn)
151 switch (mode)
153 case RGN_COPY:
154 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
155 dc->hClipRgn = 0;
156 ret = SIMPLEREGION;
157 break;
159 case RGN_DIFF:
160 return ERROR;
162 default:
163 FIXME("Unimplemented: hrgn NULL in mode: %d\n", mode);
164 return ERROR;
167 else
169 HRGN mirrored = 0;
171 if (dc->layout & LAYOUT_RTL)
173 if (!(mirrored = CreateRectRgn( 0, 0, 0, 0 ))) return ERROR;
174 mirror_region( mirrored, rgn, dc->vis_rect.right - dc->vis_rect.left );
175 rgn = mirrored;
178 if (!dc->hClipRgn)
179 create_default_clip_region( dc );
181 if (mode == RGN_COPY)
182 ret = CombineRgn( dc->hClipRgn, rgn, 0, mode );
183 else
184 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, rgn, mode);
186 if (mirrored) DeleteObject( mirrored );
188 update_dc_clipping( dc );
189 return ret;
192 INT CDECL nulldrv_ExcludeClipRect( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
194 DC *dc = get_nulldrv_dc( dev );
195 RECT rect = get_clip_rect( dc, left, top, right, bottom );
196 INT ret;
197 HRGN rgn;
199 if (!(rgn = CreateRectRgnIndirect( &rect ))) return ERROR;
200 if (!dc->hClipRgn) create_default_clip_region( dc );
201 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, rgn, RGN_DIFF );
202 DeleteObject( rgn );
203 if (ret != ERROR) update_dc_clipping( dc );
204 return ret;
207 INT CDECL nulldrv_IntersectClipRect( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
209 DC *dc = get_nulldrv_dc( dev );
210 RECT rect = get_clip_rect( dc, left, top, right, bottom );
211 INT ret;
212 HRGN rgn;
214 if (!dc->hClipRgn)
216 dc->hClipRgn = CreateRectRgnIndirect( &rect );
217 ret = SIMPLEREGION;
219 else
221 if (!(rgn = CreateRectRgnIndirect( &rect ))) return ERROR;
222 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, rgn, RGN_AND );
223 DeleteObject( rgn );
225 if (ret != ERROR) update_dc_clipping( dc );
226 return ret;
229 INT CDECL nulldrv_OffsetClipRgn( PHYSDEV dev, INT x, INT y )
231 DC *dc = get_nulldrv_dc( dev );
232 INT ret = NULLREGION;
234 if (dc->hClipRgn)
236 x = MulDiv( x, dc->vport_ext.cx, dc->wnd_ext.cx );
237 y = MulDiv( y, dc->vport_ext.cy, dc->wnd_ext.cy );
238 if (dc->layout & LAYOUT_RTL) x = -x;
239 ret = OffsetRgn( dc->hClipRgn, x, y );
240 update_dc_clipping( dc );
242 return ret;
246 /***********************************************************************
247 * SelectClipRgn (GDI32.@)
249 INT WINAPI SelectClipRgn( HDC hdc, HRGN hrgn )
251 return ExtSelectClipRgn( hdc, hrgn, RGN_COPY );
255 /******************************************************************************
256 * ExtSelectClipRgn [GDI32.@]
258 INT WINAPI ExtSelectClipRgn( HDC hdc, HRGN hrgn, INT fnMode )
260 PHYSDEV physdev;
261 INT retval;
262 DC * dc = get_dc_ptr( hdc );
264 TRACE("%p %p %d\n", hdc, hrgn, fnMode );
266 if (!dc) return ERROR;
267 update_dc( dc );
268 physdev = GET_DC_PHYSDEV( dc, pExtSelectClipRgn );
269 retval = physdev->funcs->pExtSelectClipRgn( physdev, hrgn, fnMode );
270 release_dc_ptr( dc );
271 return retval;
274 /***********************************************************************
275 * __wine_set_visible_region (GDI32.@)
277 void CDECL __wine_set_visible_region( HDC hdc, HRGN hrgn, const RECT *vis_rect, const RECT *device_rect,
278 struct window_surface *surface )
280 DC * dc;
282 if (!(dc = get_dc_ptr( hdc ))) return;
284 TRACE( "%p %p %s %s %p\n", hdc, hrgn,
285 wine_dbgstr_rect(vis_rect), wine_dbgstr_rect(device_rect), surface );
287 /* map region to DC coordinates */
288 OffsetRgn( hrgn, -vis_rect->left, -vis_rect->top );
290 if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
291 dc->dirty = 0;
292 dc->vis_rect = *vis_rect;
293 dc->device_rect = *device_rect;
294 dc->hVisRgn = hrgn;
295 dibdrv_set_window_surface( dc, surface );
296 DC_UpdateXforms( dc );
297 update_dc_clipping( dc );
298 release_dc_ptr( dc );
302 /***********************************************************************
303 * OffsetClipRgn (GDI32.@)
305 INT WINAPI OffsetClipRgn( HDC hdc, INT x, INT y )
307 PHYSDEV physdev;
308 INT ret;
309 DC *dc = get_dc_ptr( hdc );
311 TRACE("%p %d,%d\n", hdc, x, y );
313 if (!dc) return ERROR;
314 update_dc( dc );
315 physdev = GET_DC_PHYSDEV( dc, pOffsetClipRgn );
316 ret = physdev->funcs->pOffsetClipRgn( physdev, x, y );
317 release_dc_ptr( dc );
318 return ret;
322 /***********************************************************************
323 * ExcludeClipRect (GDI32.@)
325 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
326 INT right, INT bottom )
328 PHYSDEV physdev;
329 INT ret;
330 DC *dc = get_dc_ptr( hdc );
332 TRACE("%p %d,%d-%d,%d\n", hdc, left, top, right, bottom );
334 if (!dc) return ERROR;
335 update_dc( dc );
336 physdev = GET_DC_PHYSDEV( dc, pExcludeClipRect );
337 ret = physdev->funcs->pExcludeClipRect( physdev, left, top, right, bottom );
338 release_dc_ptr( dc );
339 return ret;
343 /***********************************************************************
344 * IntersectClipRect (GDI32.@)
346 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
348 PHYSDEV physdev;
349 INT ret;
350 DC *dc = get_dc_ptr( hdc );
352 TRACE("%p %d,%d - %d,%d\n", hdc, left, top, right, bottom );
354 if (!dc) return ERROR;
355 update_dc( dc );
356 physdev = GET_DC_PHYSDEV( dc, pIntersectClipRect );
357 ret = physdev->funcs->pIntersectClipRect( physdev, left, top, right, bottom );
358 release_dc_ptr( dc );
359 return ret;
363 /***********************************************************************
364 * PtVisible (GDI32.@)
366 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
368 POINT pt;
369 RECT visrect;
370 BOOL ret;
371 DC *dc = get_dc_ptr( hdc );
373 TRACE("%p %d,%d\n", hdc, x, y );
374 if (!dc) return FALSE;
376 pt.x = x;
377 pt.y = y;
378 lp_to_dp( dc, &pt, 1 );
379 update_dc( dc );
380 ret = (!get_dc_device_rect( dc, &visrect ) ||
381 (pt.x >= visrect.left && pt.x < visrect.right &&
382 pt.y >= visrect.top && pt.y < visrect.bottom));
383 if (ret && get_dc_region( dc )) ret = PtInRegion( get_dc_region( dc ), pt.x, pt.y );
384 release_dc_ptr( dc );
385 return ret;
389 /***********************************************************************
390 * RectVisible (GDI32.@)
392 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
394 RECT tmpRect, visrect;
395 BOOL ret;
396 DC *dc = get_dc_ptr( hdc );
397 if (!dc) return FALSE;
398 TRACE("%p %s\n", hdc, wine_dbgstr_rect( rect ));
400 tmpRect = *rect;
401 lp_to_dp( dc, (POINT *)&tmpRect, 2 );
402 order_rect( &tmpRect );
404 update_dc( dc );
405 ret = (!get_dc_device_rect( dc, &visrect ) || intersect_rect( &visrect, &visrect, &tmpRect ));
406 if (ret && get_dc_region( dc )) ret = RectInRegion( get_dc_region( dc ), &tmpRect );
407 release_dc_ptr( dc );
408 return ret;
412 /***********************************************************************
413 * GetClipBox (GDI32.@)
415 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
417 RECT visrect;
418 INT ret;
419 DC *dc = get_dc_ptr( hdc );
420 if (!dc) return ERROR;
422 update_dc( dc );
423 if (get_dc_region( dc ))
425 ret = GetRgnBox( get_dc_region( dc ), rect );
427 else
429 ret = is_rect_empty( &dc->vis_rect ) ? ERROR : SIMPLEREGION;
430 *rect = dc->vis_rect;
433 if (get_dc_device_rect( dc, &visrect ) && !intersect_rect( rect, rect, &visrect )) ret = NULLREGION;
435 if (dc->layout & LAYOUT_RTL)
437 int tmp = rect->left;
438 rect->left = rect->right - 1;
439 rect->right = tmp - 1;
441 dp_to_lp( dc, (LPPOINT)rect, 2 );
442 release_dc_ptr( dc );
443 TRACE("%p => %d %s\n", hdc, ret, wine_dbgstr_rect( rect ));
444 return ret;
448 /***********************************************************************
449 * GetClipRgn (GDI32.@)
451 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
453 INT ret = -1;
454 DC * dc;
455 if ((dc = get_dc_ptr( hdc )))
457 if( dc->hClipRgn )
459 if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR )
461 ret = 1;
462 if (dc->layout & LAYOUT_RTL)
463 mirror_region( hRgn, hRgn, dc->vis_rect.right - dc->vis_rect.left );
466 else ret = 0;
467 release_dc_ptr( dc );
469 return ret;
473 /***********************************************************************
474 * GetMetaRgn (GDI32.@)
476 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
478 INT ret = 0;
479 DC * dc = get_dc_ptr( hdc );
481 if (dc)
483 if (dc->hMetaRgn && CombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY ) != ERROR)
485 ret = 1;
486 if (dc->layout & LAYOUT_RTL)
487 mirror_region( hRgn, hRgn, dc->vis_rect.right - dc->vis_rect.left );
489 release_dc_ptr( dc );
491 return ret;
495 /***********************************************************************
496 * GetRandomRgn [GDI32.@]
498 * NOTES
499 * This function is documented in MSDN online for the case of
500 * iCode == SYSRGN (4).
502 * For iCode == 1 it should return the clip region
503 * 2 " " " the meta region
504 * 3 " " " the intersection of the clip with
505 * the meta region (== 'Rao' region).
507 * See http://www.codeproject.com/gdi/cliprgnguide.asp
509 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, INT iCode)
511 INT ret = 1;
512 DC *dc = get_dc_ptr( hDC );
514 if (!dc) return -1;
516 switch (iCode)
518 case 1:
519 if (dc->hClipRgn) CombineRgn( hRgn, dc->hClipRgn, 0, RGN_COPY );
520 else ret = 0;
521 break;
522 case 2:
523 if (dc->hMetaRgn) CombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY );
524 else ret = 0;
525 break;
526 case 3:
527 if (dc->hClipRgn && dc->hMetaRgn) CombineRgn( hRgn, dc->hClipRgn, dc->hMetaRgn, RGN_AND );
528 else if (dc->hClipRgn) CombineRgn( hRgn, dc->hClipRgn, 0, RGN_COPY );
529 else if (dc->hMetaRgn) CombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY );
530 else ret = 0;
531 break;
532 case SYSRGN: /* == 4 */
533 update_dc( dc );
534 if (dc->hVisRgn)
536 CombineRgn( hRgn, dc->hVisRgn, 0, RGN_COPY );
537 /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
538 if (!(GetVersion() & 0x80000000)) OffsetRgn( hRgn, dc->vis_rect.left, dc->vis_rect.top );
540 else if (!is_rect_empty( &dc->device_rect ))
541 SetRectRgn( hRgn, dc->device_rect.left, dc->device_rect.top,
542 dc->device_rect.right, dc->device_rect.bottom );
543 else
544 ret = 0;
545 break;
546 default:
547 WARN("Unknown code %d\n", iCode);
548 ret = -1;
549 break;
551 release_dc_ptr( dc );
552 return ret;
556 /***********************************************************************
557 * SetMetaRgn (GDI32.@)
559 INT WINAPI SetMetaRgn( HDC hdc )
561 INT ret;
562 RECT dummy;
563 DC *dc = get_dc_ptr( hdc );
565 if (!dc) return ERROR;
567 if (dc->hClipRgn)
569 if (dc->hMetaRgn)
571 /* the intersection becomes the new meta region */
572 CombineRgn( dc->hMetaRgn, dc->hMetaRgn, dc->hClipRgn, RGN_AND );
573 DeleteObject( dc->hClipRgn );
574 dc->hClipRgn = 0;
576 else
578 dc->hMetaRgn = dc->hClipRgn;
579 dc->hClipRgn = 0;
582 /* else nothing to do */
584 /* Note: no need to call update_dc_clipping, the overall clip region hasn't changed */
586 ret = GetRgnBox( dc->hMetaRgn, &dummy );
587 release_dc_ptr( dc );
588 return ret;