gdi32: Use NtGdiExtSelectClipRgn for ExtSelectClipRgn implementation.
[wine.git] / dlls / gdi32 / clipping.c
blob6ef49c674f2e84d3f67cd581b80e50e70c08959a
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 "ntgdi_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->attr->vis_rect.left, -dc->attr->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->attr->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 (NtGdiGetRgnBox( 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 = NtGdiCreateRectRgn( 0, 0, 0, 0 );
109 NtGdiCombineRgn( dc->region, regions[0], regions[1], RGN_AND );
110 if (count > 2) NtGdiCombineRgn( 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 return ERROR;
149 /******************************************************************************
150 * NtGdiExtSelectClipRgn (win32u.@)
152 INT WINAPI NtGdiExtSelectClipRgn( HDC hdc, HRGN rgn, INT mode )
154 INT ret = ERROR;
155 DC *dc;
157 if (!(dc = get_dc_ptr( hdc ))) return ERROR;
158 update_dc( dc );
160 if (!rgn)
162 switch (mode)
164 case RGN_COPY:
165 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
166 dc->hClipRgn = 0;
167 ret = SIMPLEREGION;
168 break;
170 case RGN_DIFF:
171 break;
173 default:
174 FIXME("Unimplemented: hrgn NULL in mode: %d\n", mode);
175 break;
178 else
180 HRGN mirrored = 0;
182 if (dc->attr->layout & LAYOUT_RTL)
184 if (!(mirrored = NtGdiCreateRectRgn( 0, 0, 0, 0 )))
186 release_dc_ptr( dc );
187 return ERROR;
189 mirror_region( mirrored, rgn, dc->attr->vis_rect.right - dc->attr->vis_rect.left );
190 rgn = mirrored;
193 if (!dc->hClipRgn)
194 create_default_clip_region( dc );
196 if (mode == RGN_COPY)
197 ret = NtGdiCombineRgn( dc->hClipRgn, rgn, 0, mode );
198 else
199 ret = NtGdiCombineRgn( dc->hClipRgn, dc->hClipRgn, rgn, mode );
201 if (mirrored) DeleteObject( mirrored );
203 if (ret != ERROR) update_dc_clipping( dc );
204 release_dc_ptr( dc );
205 return ret;
208 INT CDECL nulldrv_ExcludeClipRect( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
210 return ERROR;
213 INT CDECL nulldrv_IntersectClipRect( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
215 return ERROR;
218 INT CDECL nulldrv_OffsetClipRgn( PHYSDEV dev, INT x, INT y )
220 return ERROR;
223 /***********************************************************************
224 * __wine_set_visible_region (GDI32.@)
226 void CDECL __wine_set_visible_region( HDC hdc, HRGN hrgn, const RECT *vis_rect, const RECT *device_rect,
227 struct window_surface *surface )
229 DC * dc;
231 if (!(dc = get_dc_ptr( hdc ))) return;
233 TRACE( "%p %p %s %s %p\n", hdc, hrgn,
234 wine_dbgstr_rect(vis_rect), wine_dbgstr_rect(device_rect), surface );
236 /* map region to DC coordinates */
237 NtGdiOffsetRgn( hrgn, -vis_rect->left, -vis_rect->top );
239 if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
240 dc->dirty = 0;
241 dc->attr->vis_rect = *vis_rect;
242 dc->device_rect = *device_rect;
243 dc->hVisRgn = hrgn;
244 dibdrv_set_window_surface( dc, surface );
245 DC_UpdateXforms( dc );
246 update_dc_clipping( dc );
247 release_dc_ptr( dc );
251 /***********************************************************************
252 * NtGdiOffsetClipRgn (win32u.@)
254 INT WINAPI NtGdiOffsetClipRgn( HDC hdc, INT x, INT y )
256 INT ret = NULLREGION;
257 DC *dc = get_dc_ptr( hdc );
259 if (!dc) return ERROR;
260 update_dc( dc );
262 if (dc->hClipRgn)
264 x = MulDiv( x, dc->attr->vport_ext.cx, dc->attr->wnd_ext.cx );
265 y = MulDiv( y, dc->attr->vport_ext.cy, dc->attr->wnd_ext.cy );
266 if (dc->attr->layout & LAYOUT_RTL) x = -x;
267 ret = NtGdiOffsetRgn( dc->hClipRgn, x, y );
268 update_dc_clipping( dc );
270 release_dc_ptr( dc );
271 return ret;
275 /***********************************************************************
276 * NtGdiExcludeClipRect (win32u.@)
278 INT WINAPI NtGdiExcludeClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
280 INT ret = ERROR;
281 RECT rect;
282 HRGN rgn;
283 DC *dc = get_dc_ptr( hdc );
285 TRACE("%p %d,%d-%d,%d\n", hdc, left, top, right, bottom );
287 if (!dc) return ERROR;
288 update_dc( dc );
290 rect = get_clip_rect( dc, left, top, right, bottom );
292 if ((rgn = CreateRectRgnIndirect( &rect )))
294 if (!dc->hClipRgn) create_default_clip_region( dc );
295 ret = NtGdiCombineRgn( dc->hClipRgn, dc->hClipRgn, rgn, RGN_DIFF );
296 DeleteObject( rgn );
297 if (ret != ERROR) update_dc_clipping( dc );
299 release_dc_ptr( dc );
300 return ret;
304 /***********************************************************************
305 * NtGdiIntersectClipRect (win32u.@)
307 INT WINAPI NtGdiIntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
309 INT ret = ERROR;
310 RECT rect;
311 HRGN rgn;
312 DC *dc;
314 if (!(dc = get_dc_ptr( hdc ))) return ERROR;
315 update_dc( dc );
317 rect = get_clip_rect( dc, left, top, right, bottom );
318 if (!dc->hClipRgn)
320 if ((dc->hClipRgn = CreateRectRgnIndirect( &rect )))
321 ret = SIMPLEREGION;
323 else if ((rgn = CreateRectRgnIndirect( &rect )))
325 ret = NtGdiCombineRgn( dc->hClipRgn, dc->hClipRgn, rgn, RGN_AND );
326 DeleteObject( rgn );
328 if (ret != ERROR) update_dc_clipping( dc );
329 release_dc_ptr( dc );
330 return ret;
334 /***********************************************************************
335 * NtGdiPtVisible (win32u.@)
337 BOOL WINAPI NtGdiPtVisible( HDC hdc, INT x, INT y )
339 POINT pt;
340 RECT visrect;
341 BOOL ret;
342 DC *dc = get_dc_ptr( hdc );
344 TRACE("%p %d,%d\n", hdc, x, y );
345 if (!dc) return FALSE;
347 pt.x = x;
348 pt.y = y;
349 lp_to_dp( dc, &pt, 1 );
350 update_dc( dc );
351 ret = (!get_dc_device_rect( dc, &visrect ) ||
352 (pt.x >= visrect.left && pt.x < visrect.right &&
353 pt.y >= visrect.top && pt.y < visrect.bottom));
354 if (ret && get_dc_region( dc )) ret = NtGdiPtInRegion( get_dc_region( dc ), pt.x, pt.y );
355 release_dc_ptr( dc );
356 return ret;
360 /***********************************************************************
361 * NtGdiRectVisible (win32u.@)
363 BOOL WINAPI NtGdiRectVisible( HDC hdc, const RECT *rect )
365 RECT tmpRect, visrect;
366 BOOL ret;
367 DC *dc = get_dc_ptr( hdc );
368 if (!dc) return FALSE;
369 TRACE("%p %s\n", hdc, wine_dbgstr_rect( rect ));
371 tmpRect = *rect;
372 lp_to_dp( dc, (POINT *)&tmpRect, 2 );
373 order_rect( &tmpRect );
375 update_dc( dc );
376 ret = (!get_dc_device_rect( dc, &visrect ) || intersect_rect( &visrect, &visrect, &tmpRect ));
377 if (ret && get_dc_region( dc )) ret = NtGdiRectInRegion( get_dc_region( dc ), &tmpRect );
378 release_dc_ptr( dc );
379 return ret;
383 /***********************************************************************
384 * GetClipBox (GDI32.@)
386 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
388 RECT visrect;
389 INT ret;
390 DC *dc = get_dc_ptr( hdc );
391 if (!dc) return ERROR;
393 update_dc( dc );
394 if (get_dc_region( dc ))
396 ret = NtGdiGetRgnBox( get_dc_region( dc ), rect );
398 else
400 ret = is_rect_empty( &dc->attr->vis_rect ) ? ERROR : SIMPLEREGION;
401 *rect = dc->attr->vis_rect;
404 if (get_dc_device_rect( dc, &visrect ) && !intersect_rect( rect, rect, &visrect )) ret = NULLREGION;
406 if (dc->attr->layout & LAYOUT_RTL)
408 int tmp = rect->left;
409 rect->left = rect->right - 1;
410 rect->right = tmp - 1;
412 dp_to_lp( dc, (LPPOINT)rect, 2 );
413 release_dc_ptr( dc );
414 TRACE("%p => %d %s\n", hdc, ret, wine_dbgstr_rect( rect ));
415 return ret;
419 /***********************************************************************
420 * GetClipRgn (GDI32.@)
422 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
424 INT ret = -1;
425 DC * dc;
426 if ((dc = get_dc_ptr( hdc )))
428 if( dc->hClipRgn )
430 if (NtGdiCombineRgn( hRgn, dc->hClipRgn, 0, RGN_COPY ) != ERROR)
432 ret = 1;
433 if (dc->attr->layout & LAYOUT_RTL)
434 mirror_region( hRgn, hRgn, dc->attr->vis_rect.right - dc->attr->vis_rect.left );
437 else ret = 0;
438 release_dc_ptr( dc );
440 return ret;
444 /***********************************************************************
445 * GetMetaRgn (GDI32.@)
447 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
449 INT ret = 0;
450 DC * dc = get_dc_ptr( hdc );
452 if (dc)
454 if (dc->hMetaRgn && NtGdiCombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY ) != ERROR)
456 ret = 1;
457 if (dc->attr->layout & LAYOUT_RTL)
458 mirror_region( hRgn, hRgn, dc->attr->vis_rect.right - dc->attr->vis_rect.left );
460 release_dc_ptr( dc );
462 return ret;
466 /***********************************************************************
467 * GetRandomRgn [GDI32.@]
469 * NOTES
470 * This function is documented in MSDN online for the case of
471 * iCode == SYSRGN (4).
473 * For iCode == 1 it should return the clip region
474 * 2 " " " the meta region
475 * 3 " " " the intersection of the clip with
476 * the meta region (== 'Rao' region).
478 * See http://www.codeproject.com/gdi/cliprgnguide.asp
480 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, INT iCode)
482 INT ret = 1;
483 DC *dc = get_dc_ptr( hDC );
485 if (!dc) return -1;
487 switch (iCode)
489 case 1:
490 if (dc->hClipRgn) NtGdiCombineRgn( hRgn, dc->hClipRgn, 0, RGN_COPY );
491 else ret = 0;
492 break;
493 case 2:
494 if (dc->hMetaRgn) NtGdiCombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY );
495 else ret = 0;
496 break;
497 case 3:
498 if (dc->hClipRgn && dc->hMetaRgn) NtGdiCombineRgn( hRgn, dc->hClipRgn, dc->hMetaRgn, RGN_AND );
499 else if (dc->hClipRgn) NtGdiCombineRgn( hRgn, dc->hClipRgn, 0, RGN_COPY );
500 else if (dc->hMetaRgn) NtGdiCombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY );
501 else ret = 0;
502 break;
503 case SYSRGN: /* == 4 */
504 update_dc( dc );
505 if (dc->hVisRgn)
507 NtGdiCombineRgn( hRgn, dc->hVisRgn, 0, RGN_COPY );
508 /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
509 if (!(GetVersion() & 0x80000000))
510 NtGdiOffsetRgn( hRgn, dc->attr->vis_rect.left, dc->attr->vis_rect.top );
512 else if (!is_rect_empty( &dc->device_rect ))
513 NtGdiSetRectRgn( hRgn, dc->device_rect.left, dc->device_rect.top,
514 dc->device_rect.right, dc->device_rect.bottom );
515 else
516 ret = 0;
517 break;
518 default:
519 WARN("Unknown code %d\n", iCode);
520 ret = -1;
521 break;
523 release_dc_ptr( dc );
524 return ret;
528 /***********************************************************************
529 * SetMetaRgn (GDI32.@)
531 INT WINAPI SetMetaRgn( HDC hdc )
533 INT ret;
534 RECT dummy;
535 DC *dc = get_dc_ptr( hdc );
537 if (!dc) return ERROR;
539 if (dc->hClipRgn)
541 if (dc->hMetaRgn)
543 /* the intersection becomes the new meta region */
544 NtGdiCombineRgn( dc->hMetaRgn, dc->hMetaRgn, dc->hClipRgn, RGN_AND );
545 DeleteObject( dc->hClipRgn );
546 dc->hClipRgn = 0;
548 else
550 dc->hMetaRgn = dc->hClipRgn;
551 dc->hClipRgn = 0;
554 /* else nothing to do */
556 /* Note: no need to call update_dc_clipping, the overall clip region hasn't changed */
558 ret = NtGdiGetRgnBox( dc->hMetaRgn, &dummy );
559 release_dc_ptr( dc );
560 return ret;