include: Be consistent in naming regarding MSF's block.
[wine.git] / dlls / win32u / clipping.c
blob11027824362a34c98abe84e66fe42d831b195648
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 #if 0
22 #pragma makedep unix
23 #endif
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "ntgdi_private.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(clipping);
36 /* return the DC device rectangle if not empty */
37 static inline BOOL get_dc_device_rect( DC *dc, RECT *rect )
39 *rect = dc->device_rect;
40 OffsetRect( rect, -dc->attr->vis_rect.left, -dc->attr->vis_rect.top );
41 return !IsRectEmpty( rect );
44 /***********************************************************************
45 * get_clip_rect
47 * Compute a clip rectangle from its logical coordinates.
49 static inline RECT get_clip_rect( DC * dc, int left, int top, int right, int bottom )
51 RECT rect;
53 rect.left = left;
54 rect.top = top;
55 rect.right = right;
56 rect.bottom = bottom;
57 lp_to_dp( dc, (POINT *)&rect, 2 );
58 if (dc->attr->layout & LAYOUT_RTL)
60 int tmp = rect.left;
61 rect.left = rect.right + 1;
62 rect.right = tmp + 1;
64 return rect;
67 /***********************************************************************
68 * clip_device_rect
70 * Clip a rectangle to the whole DC surface.
72 BOOL clip_device_rect( DC *dc, RECT *dst, const RECT *src )
74 RECT clip;
76 if (get_dc_device_rect( dc, &clip )) return intersect_rect( dst, src, &clip );
77 *dst = *src;
78 return TRUE;
81 /***********************************************************************
82 * clip_visrect
84 * Clip a rectangle to the DC visible rect.
86 BOOL clip_visrect( DC *dc, RECT *dst, const RECT *src )
88 RECT clip;
90 if (!clip_device_rect( dc, dst, src )) return FALSE;
91 if (NtGdiGetRgnBox( get_dc_region(dc), &clip )) return intersect_rect( dst, dst, &clip );
92 return TRUE;
95 /***********************************************************************
96 * update_dc_clipping
98 * Update the DC and device clip regions when the ClipRgn or VisRgn have changed.
100 void update_dc_clipping( DC * dc )
102 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetDeviceClipping );
103 HRGN regions[3];
104 int count = 0;
106 if (dc->hVisRgn) regions[count++] = dc->hVisRgn;
107 if (dc->hClipRgn) regions[count++] = dc->hClipRgn;
108 if (dc->hMetaRgn) regions[count++] = dc->hMetaRgn;
110 if (count > 1)
112 if (!dc->region) dc->region = NtGdiCreateRectRgn( 0, 0, 0, 0 );
113 NtGdiCombineRgn( dc->region, regions[0], regions[1], RGN_AND );
114 if (count > 2) NtGdiCombineRgn( dc->region, dc->region, regions[2], RGN_AND );
116 else /* only one region, we don't need the total region */
118 if (dc->region) NtGdiDeleteObjectApp( dc->region );
119 dc->region = 0;
121 physdev->funcs->pSetDeviceClipping( physdev, get_dc_region( dc ));
124 /***********************************************************************
125 * create_default_clip_region
127 * Create a default clipping region when none already exists.
129 static inline void create_default_clip_region( DC * dc )
131 RECT rect;
133 if (!get_dc_device_rect( dc, &rect ))
135 rect.left = 0;
136 rect.top = 0;
137 rect.right = NtGdiGetDeviceCaps( dc->hSelf, DESKTOPHORZRES );
138 rect.bottom = NtGdiGetDeviceCaps( dc->hSelf, DESKTOPVERTRES );
140 dc->hClipRgn = NtGdiCreateRectRgn( rect.left, rect.top, rect.right, rect.bottom );
144 /******************************************************************************
145 * NtGdiExtSelectClipRgn (win32u.@)
147 INT WINAPI NtGdiExtSelectClipRgn( HDC hdc, HRGN rgn, INT mode )
149 INT ret = ERROR;
150 DC *dc;
152 if (!(dc = get_dc_ptr( hdc ))) return ERROR;
153 update_dc( dc );
155 if (!rgn)
157 switch (mode)
159 case RGN_COPY:
160 if (dc->hClipRgn) NtGdiDeleteObjectApp( dc->hClipRgn );
161 dc->hClipRgn = 0;
162 ret = SIMPLEREGION;
163 break;
165 case RGN_DIFF:
166 break;
168 default:
169 FIXME("Unimplemented: hrgn NULL in mode: %d\n", mode);
170 break;
173 else
175 HRGN mirrored = 0;
177 if (dc->attr->layout & LAYOUT_RTL)
179 if (!(mirrored = NtGdiCreateRectRgn( 0, 0, 0, 0 )))
181 release_dc_ptr( dc );
182 return ERROR;
184 mirror_region( mirrored, rgn, dc->attr->vis_rect.right - dc->attr->vis_rect.left );
185 rgn = mirrored;
188 if (!dc->hClipRgn)
189 create_default_clip_region( dc );
191 if (mode == RGN_COPY)
192 ret = NtGdiCombineRgn( dc->hClipRgn, rgn, 0, mode );
193 else
194 ret = NtGdiCombineRgn( dc->hClipRgn, dc->hClipRgn, rgn, mode );
196 if (mirrored) NtGdiDeleteObjectApp( mirrored );
198 if (ret != ERROR) update_dc_clipping( dc );
199 release_dc_ptr( dc );
200 return ret;
203 /***********************************************************************
204 * set_visible_region
206 void set_visible_region( HDC hdc, HRGN hrgn, const RECT *vis_rect, const RECT *device_rect,
207 struct window_surface *surface )
209 DC * dc;
211 if (!(dc = get_dc_ptr( hdc ))) return;
213 TRACE( "%p %p %s %s %p\n", hdc, hrgn,
214 wine_dbgstr_rect(vis_rect), wine_dbgstr_rect(device_rect), surface );
216 /* map region to DC coordinates */
217 NtGdiOffsetRgn( hrgn, -vis_rect->left, -vis_rect->top );
219 if (dc->hVisRgn) NtGdiDeleteObjectApp( dc->hVisRgn );
220 dc->dirty = 0;
221 dc->attr->vis_rect = *vis_rect;
222 dc->device_rect = *device_rect;
223 dc->hVisRgn = hrgn;
224 dibdrv_set_window_surface( dc, surface );
225 DC_UpdateXforms( dc );
226 update_dc_clipping( dc );
227 release_dc_ptr( dc );
231 /***********************************************************************
232 * NtGdiOffsetClipRgn (win32u.@)
234 INT WINAPI NtGdiOffsetClipRgn( HDC hdc, INT x, INT y )
236 INT ret = NULLREGION;
237 DC *dc = get_dc_ptr( hdc );
239 if (!dc) return ERROR;
240 update_dc( dc );
242 if (dc->hClipRgn)
244 x = muldiv( x, dc->attr->vport_ext.cx, dc->attr->wnd_ext.cx );
245 y = muldiv( y, dc->attr->vport_ext.cy, dc->attr->wnd_ext.cy );
246 if (dc->attr->layout & LAYOUT_RTL) x = -x;
247 ret = NtGdiOffsetRgn( dc->hClipRgn, x, y );
248 update_dc_clipping( dc );
250 release_dc_ptr( dc );
251 return ret;
255 /***********************************************************************
256 * NtGdiExcludeClipRect (win32u.@)
258 INT WINAPI NtGdiExcludeClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
260 INT ret = ERROR;
261 RECT rect;
262 HRGN rgn;
263 DC *dc = get_dc_ptr( hdc );
265 TRACE("%p %d,%d-%d,%d\n", hdc, left, top, right, bottom );
267 if (!dc) return ERROR;
268 update_dc( dc );
270 rect = get_clip_rect( dc, left, top, right, bottom );
272 if ((rgn = NtGdiCreateRectRgn( rect.left, rect.top, rect.right, rect.bottom )))
274 if (!dc->hClipRgn) create_default_clip_region( dc );
275 ret = NtGdiCombineRgn( dc->hClipRgn, dc->hClipRgn, rgn, RGN_DIFF );
276 NtGdiDeleteObjectApp( rgn );
277 if (ret != ERROR) update_dc_clipping( dc );
279 release_dc_ptr( dc );
280 return ret;
284 /***********************************************************************
285 * NtGdiIntersectClipRect (win32u.@)
287 INT WINAPI NtGdiIntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
289 INT ret = ERROR;
290 RECT rect;
291 HRGN rgn;
292 DC *dc;
294 if (!(dc = get_dc_ptr( hdc ))) return ERROR;
295 update_dc( dc );
297 rect = get_clip_rect( dc, left, top, right, bottom );
298 if (!dc->hClipRgn)
300 if ((dc->hClipRgn = NtGdiCreateRectRgn( rect.left, rect.top, rect.right, rect.bottom )))
301 ret = SIMPLEREGION;
303 else if ((rgn = NtGdiCreateRectRgn( rect.left, rect.top, rect.right, rect.bottom )))
305 ret = NtGdiCombineRgn( dc->hClipRgn, dc->hClipRgn, rgn, RGN_AND );
306 NtGdiDeleteObjectApp( rgn );
308 if (ret != ERROR) update_dc_clipping( dc );
309 release_dc_ptr( dc );
310 return ret;
314 /***********************************************************************
315 * NtGdiPtVisible (win32u.@)
317 BOOL WINAPI NtGdiPtVisible( HDC hdc, INT x, INT y )
319 POINT pt;
320 RECT visrect;
321 BOOL ret;
322 DC *dc = get_dc_ptr( hdc );
324 TRACE("%p %d,%d\n", hdc, x, y );
325 if (!dc) return FALSE;
327 pt.x = x;
328 pt.y = y;
329 lp_to_dp( dc, &pt, 1 );
330 update_dc( dc );
331 ret = (!get_dc_device_rect( dc, &visrect ) ||
332 (pt.x >= visrect.left && pt.x < visrect.right &&
333 pt.y >= visrect.top && pt.y < visrect.bottom));
334 if (ret && get_dc_region( dc )) ret = NtGdiPtInRegion( get_dc_region( dc ), pt.x, pt.y );
335 release_dc_ptr( dc );
336 return ret;
340 /***********************************************************************
341 * NtGdiRectVisible (win32u.@)
343 BOOL WINAPI NtGdiRectVisible( HDC hdc, const RECT *rect )
345 RECT tmpRect, visrect;
346 BOOL ret;
347 DC *dc = get_dc_ptr( hdc );
348 if (!dc) return FALSE;
349 TRACE("%p %s\n", hdc, wine_dbgstr_rect( rect ));
351 tmpRect = *rect;
352 lp_to_dp( dc, (POINT *)&tmpRect, 2 );
353 order_rect( &tmpRect );
355 update_dc( dc );
356 ret = (!get_dc_device_rect( dc, &visrect ) || intersect_rect( &visrect, &visrect, &tmpRect ));
357 if (ret && get_dc_region( dc )) ret = NtGdiRectInRegion( get_dc_region( dc ), &tmpRect );
358 release_dc_ptr( dc );
359 return ret;
363 /***********************************************************************
364 * NtGdiGetAppClipBox (win32u.@)
366 INT WINAPI NtGdiGetAppClipBox( HDC hdc, RECT *rect )
368 RECT visrect;
369 INT ret;
370 DC *dc = get_dc_ptr( hdc );
371 if (!dc) return ERROR;
373 update_dc( dc );
374 if (get_dc_region( dc ))
376 ret = NtGdiGetRgnBox( get_dc_region( dc ), rect );
378 else
380 ret = IsRectEmpty( &dc->attr->vis_rect ) ? ERROR : SIMPLEREGION;
381 *rect = dc->attr->vis_rect;
384 if (get_dc_device_rect( dc, &visrect ) && !intersect_rect( rect, rect, &visrect )) ret = NULLREGION;
386 if (dc->attr->layout & LAYOUT_RTL)
388 int tmp = rect->left;
389 rect->left = rect->right - 1;
390 rect->right = tmp - 1;
392 dp_to_lp( dc, (LPPOINT)rect, 2 );
393 release_dc_ptr( dc );
394 TRACE("%p => %d %s\n", hdc, ret, wine_dbgstr_rect( rect ));
395 return ret;
399 /***********************************************************************
400 * NtGdiGetRandomRgn (win32u.@)
402 * NOTES
403 * This function is documented in MSDN online for the case of
404 * iCode == SYSRGN (4).
406 * For iCode == 1 it should return the clip region
407 * 2 " " " the meta region
408 * 3 " " " the intersection of the clip with
409 * the meta region (== 'Rao' region).
411 * See http://www.codeproject.com/gdi/cliprgnguide.asp
413 INT WINAPI NtGdiGetRandomRgn( HDC hDC, HRGN hRgn, INT iCode )
415 INT ret = 1;
416 DC *dc = get_dc_ptr( hDC );
418 if (!dc) return -1;
420 switch (iCode & ~NTGDI_RGN_MIRROR_RTL)
422 case 1:
423 if (!dc->hClipRgn) ret = 0;
424 else if (!NtGdiCombineRgn( hRgn, dc->hClipRgn, 0, RGN_COPY )) ret = -1;
425 break;
426 case 2:
427 if (!dc->hMetaRgn) ret = 0;
428 else if (!NtGdiCombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY )) ret = -1;
429 break;
430 case 3:
431 if (dc->hClipRgn && dc->hMetaRgn) NtGdiCombineRgn( hRgn, dc->hClipRgn, dc->hMetaRgn, RGN_AND );
432 else if (dc->hClipRgn) NtGdiCombineRgn( hRgn, dc->hClipRgn, 0, RGN_COPY );
433 else if (dc->hMetaRgn) NtGdiCombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY );
434 else ret = 0;
435 break;
436 case SYSRGN: /* == 4 */
437 update_dc( dc );
438 if (dc->hVisRgn)
440 NtGdiCombineRgn( hRgn, dc->hVisRgn, 0, RGN_COPY );
441 /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
442 if (NtCurrentTeb()->Peb->OSPlatformId != VER_PLATFORM_WIN32s)
443 NtGdiOffsetRgn( hRgn, dc->attr->vis_rect.left, dc->attr->vis_rect.top );
445 else if (!IsRectEmpty( &dc->device_rect ))
446 NtGdiSetRectRgn( hRgn, dc->device_rect.left, dc->device_rect.top,
447 dc->device_rect.right, dc->device_rect.bottom );
448 else
449 ret = 0;
450 break;
451 default:
452 WARN("Unknown code %d\n", iCode);
453 ret = -1;
454 break;
457 /* Wine extension */
458 if (ret > 0 && (iCode & NTGDI_RGN_MIRROR_RTL) && (dc->attr->layout & LAYOUT_RTL))
459 mirror_region( hRgn, hRgn, dc->attr->vis_rect.right - dc->attr->vis_rect.left );
461 release_dc_ptr( dc );
462 return ret;
466 /***********************************************************************
467 * NtGdiSetMetaRgn (win32u.@)
469 INT WINAPI NtGdiSetMetaRgn( HDC hdc )
471 INT ret;
472 RECT dummy;
473 DC *dc = get_dc_ptr( hdc );
475 if (!dc) return ERROR;
477 if (dc->hClipRgn)
479 if (dc->hMetaRgn)
481 /* the intersection becomes the new meta region */
482 NtGdiCombineRgn( dc->hMetaRgn, dc->hMetaRgn, dc->hClipRgn, RGN_AND );
483 NtGdiDeleteObjectApp( dc->hClipRgn );
484 dc->hClipRgn = 0;
486 else
488 dc->hMetaRgn = dc->hClipRgn;
489 dc->hClipRgn = 0;
492 /* else nothing to do */
494 /* Note: no need to call update_dc_clipping, the overall clip region hasn't changed */
496 ret = NtGdiGetRgnBox( dc->hMetaRgn, &dummy );
497 release_dc_ptr( dc );
498 return ret;