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
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 /***********************************************************************
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
)
57 lp_to_dp( dc
, (POINT
*)&rect
, 2 );
58 if (dc
->attr
->layout
& LAYOUT_RTL
)
61 rect
.left
= rect
.right
+ 1;
67 /***********************************************************************
70 * Clip a rectangle to the whole DC surface.
72 BOOL
clip_device_rect( DC
*dc
, RECT
*dst
, const RECT
*src
)
76 if (get_dc_device_rect( dc
, &clip
)) return intersect_rect( dst
, src
, &clip
);
81 /***********************************************************************
84 * Clip a rectangle to the DC visible rect.
86 BOOL
clip_visrect( DC
*dc
, RECT
*dst
, const RECT
*src
)
90 if (!clip_device_rect( dc
, dst
, src
)) return FALSE
;
91 if (NtGdiGetRgnBox( get_dc_region(dc
), &clip
)) return intersect_rect( dst
, dst
, &clip
);
95 /***********************************************************************
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
);
106 if (dc
->hVisRgn
) regions
[count
++] = dc
->hVisRgn
;
107 if (dc
->hClipRgn
) regions
[count
++] = dc
->hClipRgn
;
108 if (dc
->hMetaRgn
) regions
[count
++] = dc
->hMetaRgn
;
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
);
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
)
133 if (!get_dc_device_rect( dc
, &rect
))
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
)
152 if (!(dc
= get_dc_ptr( hdc
))) return ERROR
;
160 if (dc
->hClipRgn
) NtGdiDeleteObjectApp( dc
->hClipRgn
);
169 FIXME("Unimplemented: hrgn NULL in mode: %d\n", mode
);
177 if (dc
->attr
->layout
& LAYOUT_RTL
)
179 if (!(mirrored
= NtGdiCreateRectRgn( 0, 0, 0, 0 )))
181 release_dc_ptr( dc
);
184 mirror_region( mirrored
, rgn
, dc
->attr
->vis_rect
.right
- dc
->attr
->vis_rect
.left
);
189 create_default_clip_region( dc
);
191 if (mode
== RGN_COPY
)
192 ret
= NtGdiCombineRgn( dc
->hClipRgn
, rgn
, 0, mode
);
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
);
203 /***********************************************************************
206 void set_visible_region( HDC hdc
, HRGN hrgn
, const RECT
*vis_rect
, const RECT
*device_rect
,
207 struct window_surface
*surface
)
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
);
221 dc
->attr
->vis_rect
= *vis_rect
;
222 dc
->device_rect
= *device_rect
;
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
;
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
);
255 /***********************************************************************
256 * NtGdiExcludeClipRect (win32u.@)
258 INT WINAPI
NtGdiExcludeClipRect( HDC hdc
, INT left
, INT top
, INT right
, INT bottom
)
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
;
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
);
284 /***********************************************************************
285 * NtGdiIntersectClipRect (win32u.@)
287 INT WINAPI
NtGdiIntersectClipRect( HDC hdc
, INT left
, INT top
, INT right
, INT bottom
)
294 if (!(dc
= get_dc_ptr( hdc
))) return ERROR
;
297 rect
= get_clip_rect( dc
, left
, top
, right
, bottom
);
300 if ((dc
->hClipRgn
= NtGdiCreateRectRgn( rect
.left
, rect
.top
, rect
.right
, rect
.bottom
)))
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
);
314 /***********************************************************************
315 * NtGdiPtVisible (win32u.@)
317 BOOL WINAPI
NtGdiPtVisible( HDC hdc
, INT x
, INT y
)
322 DC
*dc
= get_dc_ptr( hdc
);
324 TRACE("%p %d,%d\n", hdc
, x
, y
);
325 if (!dc
) return FALSE
;
329 lp_to_dp( dc
, &pt
, 1 );
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
);
340 /***********************************************************************
341 * NtGdiRectVisible (win32u.@)
343 BOOL WINAPI
NtGdiRectVisible( HDC hdc
, const RECT
*rect
)
345 RECT tmpRect
, visrect
;
347 DC
*dc
= get_dc_ptr( hdc
);
348 if (!dc
) return FALSE
;
349 TRACE("%p %s\n", hdc
, wine_dbgstr_rect( rect
));
352 lp_to_dp( dc
, (POINT
*)&tmpRect
, 2 );
353 order_rect( &tmpRect
);
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
);
363 /***********************************************************************
364 * NtGdiGetAppClipBox (win32u.@)
366 INT WINAPI
NtGdiGetAppClipBox( HDC hdc
, RECT
*rect
)
370 DC
*dc
= get_dc_ptr( hdc
);
371 if (!dc
) return ERROR
;
374 if (get_dc_region( dc
))
376 ret
= NtGdiGetRgnBox( get_dc_region( dc
), rect
);
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
));
399 /***********************************************************************
400 * NtGdiGetRandomRgn (win32u.@)
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
)
416 DC
*dc
= get_dc_ptr( hDC
);
420 switch (iCode
& ~NTGDI_RGN_MIRROR_RTL
)
423 if (!dc
->hClipRgn
) ret
= 0;
424 else if (!NtGdiCombineRgn( hRgn
, dc
->hClipRgn
, 0, RGN_COPY
)) ret
= -1;
427 if (!dc
->hMetaRgn
) ret
= 0;
428 else if (!NtGdiCombineRgn( hRgn
, dc
->hMetaRgn
, 0, RGN_COPY
)) ret
= -1;
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
);
436 case SYSRGN
: /* == 4 */
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
);
452 WARN("Unknown code %d\n", iCode
);
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
);
466 /***********************************************************************
467 * NtGdiSetMetaRgn (win32u.@)
469 INT WINAPI
NtGdiSetMetaRgn( HDC hdc
)
473 DC
*dc
= get_dc_ptr( hdc
);
475 if (!dc
) return ERROR
;
481 /* the intersection becomes the new meta region */
482 NtGdiCombineRgn( dc
->hMetaRgn
, dc
->hMetaRgn
, dc
->hClipRgn
, RGN_AND
);
483 NtGdiDeleteObjectApp( dc
->hClipRgn
);
488 dc
->hMetaRgn
= dc
->hClipRgn
;
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
);