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
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 /***********************************************************************
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
)
53 lp_to_dp( dc
, (POINT
*)&rect
, 2 );
54 if (dc
->attr
->layout
& LAYOUT_RTL
)
57 rect
.left
= rect
.right
+ 1;
63 /***********************************************************************
66 * Clip a rectangle to the whole DC surface.
68 BOOL
clip_device_rect( DC
*dc
, RECT
*dst
, const RECT
*src
)
72 if (get_dc_device_rect( dc
, &clip
)) return intersect_rect( dst
, src
, &clip
);
77 /***********************************************************************
80 * Clip a rectangle to the DC visible rect.
82 BOOL
clip_visrect( DC
*dc
, RECT
*dst
, const RECT
*src
)
86 if (!clip_device_rect( dc
, dst
, src
)) return FALSE
;
87 if (NtGdiGetRgnBox( get_dc_region(dc
), &clip
)) return intersect_rect( dst
, dst
, &clip
);
91 /***********************************************************************
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
);
102 if (dc
->hVisRgn
) regions
[count
++] = dc
->hVisRgn
;
103 if (dc
->hClipRgn
) regions
[count
++] = dc
->hClipRgn
;
104 if (dc
->hMetaRgn
) regions
[count
++] = dc
->hMetaRgn
;
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
);
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
)
129 if (!get_dc_device_rect( dc
, &rect
))
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
)
149 /******************************************************************************
150 * NtGdiExtSelectClipRgn (win32u.@)
152 INT WINAPI
NtGdiExtSelectClipRgn( HDC hdc
, HRGN rgn
, INT mode
)
157 if (!(dc
= get_dc_ptr( hdc
))) return ERROR
;
165 if (dc
->hClipRgn
) DeleteObject( dc
->hClipRgn
);
174 FIXME("Unimplemented: hrgn NULL in mode: %d\n", mode
);
182 if (dc
->attr
->layout
& LAYOUT_RTL
)
184 if (!(mirrored
= NtGdiCreateRectRgn( 0, 0, 0, 0 )))
186 release_dc_ptr( dc
);
189 mirror_region( mirrored
, rgn
, dc
->attr
->vis_rect
.right
- dc
->attr
->vis_rect
.left
);
194 create_default_clip_region( dc
);
196 if (mode
== RGN_COPY
)
197 ret
= NtGdiCombineRgn( dc
->hClipRgn
, rgn
, 0, mode
);
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
);
208 INT CDECL
nulldrv_ExcludeClipRect( PHYSDEV dev
, INT left
, INT top
, INT right
, INT bottom
)
213 INT CDECL
nulldrv_IntersectClipRect( PHYSDEV dev
, INT left
, INT top
, INT right
, INT bottom
)
218 INT CDECL
nulldrv_OffsetClipRgn( PHYSDEV dev
, INT x
, INT y
)
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
)
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
);
241 dc
->attr
->vis_rect
= *vis_rect
;
242 dc
->device_rect
= *device_rect
;
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
;
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
);
275 /***********************************************************************
276 * NtGdiExcludeClipRect (win32u.@)
278 INT WINAPI
NtGdiExcludeClipRect( HDC hdc
, INT left
, INT top
, INT right
, INT bottom
)
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
;
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
);
297 if (ret
!= ERROR
) update_dc_clipping( dc
);
299 release_dc_ptr( dc
);
304 /***********************************************************************
305 * NtGdiIntersectClipRect (win32u.@)
307 INT WINAPI
NtGdiIntersectClipRect( HDC hdc
, INT left
, INT top
, INT right
, INT bottom
)
314 if (!(dc
= get_dc_ptr( hdc
))) return ERROR
;
317 rect
= get_clip_rect( dc
, left
, top
, right
, bottom
);
320 if ((dc
->hClipRgn
= CreateRectRgnIndirect( &rect
)))
323 else if ((rgn
= CreateRectRgnIndirect( &rect
)))
325 ret
= NtGdiCombineRgn( dc
->hClipRgn
, dc
->hClipRgn
, rgn
, RGN_AND
);
328 if (ret
!= ERROR
) update_dc_clipping( dc
);
329 release_dc_ptr( dc
);
334 /***********************************************************************
335 * NtGdiPtVisible (win32u.@)
337 BOOL WINAPI
NtGdiPtVisible( HDC hdc
, INT x
, INT y
)
342 DC
*dc
= get_dc_ptr( hdc
);
344 TRACE("%p %d,%d\n", hdc
, x
, y
);
345 if (!dc
) return FALSE
;
349 lp_to_dp( dc
, &pt
, 1 );
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
);
360 /***********************************************************************
361 * NtGdiRectVisible (win32u.@)
363 BOOL WINAPI
NtGdiRectVisible( HDC hdc
, const RECT
*rect
)
365 RECT tmpRect
, visrect
;
367 DC
*dc
= get_dc_ptr( hdc
);
368 if (!dc
) return FALSE
;
369 TRACE("%p %s\n", hdc
, wine_dbgstr_rect( rect
));
372 lp_to_dp( dc
, (POINT
*)&tmpRect
, 2 );
373 order_rect( &tmpRect
);
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
);
383 /***********************************************************************
384 * GetClipBox (GDI32.@)
386 INT WINAPI
GetClipBox( HDC hdc
, LPRECT rect
)
390 DC
*dc
= get_dc_ptr( hdc
);
391 if (!dc
) return ERROR
;
394 if (get_dc_region( dc
))
396 ret
= NtGdiGetRgnBox( get_dc_region( dc
), rect
);
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
));
419 /***********************************************************************
420 * GetClipRgn (GDI32.@)
422 INT WINAPI
GetClipRgn( HDC hdc
, HRGN hRgn
)
426 if ((dc
= get_dc_ptr( hdc
)))
430 if (NtGdiCombineRgn( hRgn
, dc
->hClipRgn
, 0, RGN_COPY
) != ERROR
)
433 if (dc
->attr
->layout
& LAYOUT_RTL
)
434 mirror_region( hRgn
, hRgn
, dc
->attr
->vis_rect
.right
- dc
->attr
->vis_rect
.left
);
438 release_dc_ptr( dc
);
444 /***********************************************************************
445 * GetMetaRgn (GDI32.@)
447 INT WINAPI
GetMetaRgn( HDC hdc
, HRGN hRgn
)
450 DC
* dc
= get_dc_ptr( hdc
);
454 if (dc
->hMetaRgn
&& NtGdiCombineRgn( hRgn
, dc
->hMetaRgn
, 0, RGN_COPY
) != ERROR
)
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
);
466 /***********************************************************************
467 * GetRandomRgn [GDI32.@]
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
)
483 DC
*dc
= get_dc_ptr( hDC
);
490 if (dc
->hClipRgn
) NtGdiCombineRgn( hRgn
, dc
->hClipRgn
, 0, RGN_COPY
);
494 if (dc
->hMetaRgn
) NtGdiCombineRgn( hRgn
, dc
->hMetaRgn
, 0, RGN_COPY
);
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
);
503 case SYSRGN
: /* == 4 */
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
);
519 WARN("Unknown code %d\n", iCode
);
523 release_dc_ptr( dc
);
528 /***********************************************************************
529 * SetMetaRgn (GDI32.@)
531 INT WINAPI
SetMetaRgn( HDC hdc
)
535 DC
*dc
= get_dc_ptr( hdc
);
537 if (!dc
) return ERROR
;
543 /* the intersection becomes the new meta region */
544 NtGdiCombineRgn( dc
->hMetaRgn
, dc
->hMetaRgn
, dc
->hClipRgn
, RGN_AND
);
545 DeleteObject( dc
->hClipRgn
);
550 dc
->hMetaRgn
= dc
->hClipRgn
;
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
);