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 "gdi_private.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(clipping
);
32 /* return the DC visible rectangle if not empty */
33 static inline BOOL
get_dc_visrect( DC
*dc
, RECT
*rect
)
35 if (dc
->header
.type
!= OBJ_MEMDC
) return FALSE
;
38 rect
->right
= dc
->vis_rect
.right
- dc
->vis_rect
.left
;
39 rect
->bottom
= dc
->vis_rect
.bottom
- dc
->vis_rect
.top
;
43 /***********************************************************************
46 * Compute a clip rectangle from its logical coordinates.
48 static inline RECT
get_clip_rect( DC
* dc
, int left
, int top
, int right
, int bottom
)
56 LPtoDP( dc
->hSelf
, (POINT
*)&rect
, 2 );
57 if (dc
->layout
& LAYOUT_RTL
)
60 rect
.left
= rect
.right
+ 1;
66 /***********************************************************************
69 * Clip a rectangle to the DC visible rect.
71 BOOL
clip_visrect( DC
*dc
, RECT
*dst
, const RECT
*src
)
75 if (get_dc_visrect( dc
, &clip
)) intersect_rect( dst
, src
, &clip
);
78 if (GetRgnBox( get_dc_region(dc
), &clip
)) intersect_rect( dst
, dst
, &clip
);
79 return !is_rect_empty( dst
);
82 /***********************************************************************
85 * Update the DC and device clip regions when the ClipRgn or VisRgn have changed.
87 void update_dc_clipping( DC
* dc
)
89 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pSetDeviceClipping
);
93 if (dc
->hVisRgn
) regions
[count
++] = dc
->hVisRgn
;
94 if (dc
->hClipRgn
) regions
[count
++] = dc
->hClipRgn
;
95 if (dc
->hMetaRgn
) regions
[count
++] = dc
->hMetaRgn
;
99 if (!dc
->region
) dc
->region
= CreateRectRgn( 0, 0, 0, 0 );
100 CombineRgn( dc
->region
, regions
[0], regions
[1], RGN_AND
);
101 if (count
> 2) CombineRgn( dc
->region
, dc
->region
, regions
[2], RGN_AND
);
103 else /* only one region, we don't need the total region */
105 if (dc
->region
) DeleteObject( dc
->region
);
108 physdev
->funcs
->pSetDeviceClipping( physdev
, get_dc_region( dc
));
111 /***********************************************************************
112 * create_default_clip_region
114 * Create a default clipping region when none already exists.
116 static inline void create_default_clip_region( DC
* dc
)
120 if (!get_dc_visrect( dc
, &rect
))
124 rect
.right
= GetDeviceCaps( dc
->hSelf
, DESKTOPHORZRES
);
125 rect
.bottom
= GetDeviceCaps( dc
->hSelf
, DESKTOPVERTRES
);
127 dc
->hClipRgn
= CreateRectRgnIndirect( &rect
);
131 /***********************************************************************
132 * null driver fallback implementations
135 INT
nulldrv_ExtSelectClipRgn( PHYSDEV dev
, HRGN rgn
, INT mode
)
137 DC
*dc
= get_nulldrv_dc( dev
);
142 if (mode
!= RGN_COPY
)
144 FIXME("Unimplemented: hrgn NULL in mode: %d\n", mode
);
147 if (dc
->hClipRgn
) DeleteObject( dc
->hClipRgn
);
155 if (dc
->layout
& LAYOUT_RTL
)
157 if (!(mirrored
= CreateRectRgn( 0, 0, 0, 0 ))) return ERROR
;
158 mirror_region( mirrored
, rgn
, dc
->vis_rect
.right
- dc
->vis_rect
.left
);
163 create_default_clip_region( dc
);
165 if (mode
== RGN_COPY
)
166 ret
= CombineRgn( dc
->hClipRgn
, rgn
, 0, mode
);
168 ret
= CombineRgn( dc
->hClipRgn
, dc
->hClipRgn
, rgn
, mode
);
170 if (mirrored
) DeleteObject( mirrored
);
172 update_dc_clipping( dc
);
176 INT
nulldrv_ExcludeClipRect( PHYSDEV dev
, INT left
, INT top
, INT right
, INT bottom
)
178 DC
*dc
= get_nulldrv_dc( dev
);
179 RECT rect
= get_clip_rect( dc
, left
, top
, right
, bottom
);
183 if (!(rgn
= CreateRectRgnIndirect( &rect
))) return ERROR
;
184 if (!dc
->hClipRgn
) create_default_clip_region( dc
);
185 ret
= CombineRgn( dc
->hClipRgn
, dc
->hClipRgn
, rgn
, RGN_DIFF
);
187 if (ret
!= ERROR
) update_dc_clipping( dc
);
191 INT
nulldrv_IntersectClipRect( PHYSDEV dev
, INT left
, INT top
, INT right
, INT bottom
)
193 DC
*dc
= get_nulldrv_dc( dev
);
194 RECT rect
= get_clip_rect( dc
, left
, top
, right
, bottom
);
200 dc
->hClipRgn
= CreateRectRgnIndirect( &rect
);
205 if (!(rgn
= CreateRectRgnIndirect( &rect
))) return ERROR
;
206 ret
= CombineRgn( dc
->hClipRgn
, dc
->hClipRgn
, rgn
, RGN_AND
);
209 if (ret
!= ERROR
) update_dc_clipping( dc
);
213 INT
nulldrv_OffsetClipRgn( PHYSDEV dev
, INT x
, INT y
)
215 DC
*dc
= get_nulldrv_dc( dev
);
216 INT ret
= NULLREGION
;
220 x
= MulDiv( x
, dc
->vportExtX
, dc
->wndExtX
);
221 y
= MulDiv( y
, dc
->vportExtY
, dc
->wndExtY
);
222 if (dc
->layout
& LAYOUT_RTL
) x
= -x
;
223 ret
= OffsetRgn( dc
->hClipRgn
, x
, y
);
224 update_dc_clipping( dc
);
230 /***********************************************************************
231 * SelectClipRgn (GDI32.@)
233 INT WINAPI
SelectClipRgn( HDC hdc
, HRGN hrgn
)
235 return ExtSelectClipRgn( hdc
, hrgn
, RGN_COPY
);
239 /******************************************************************************
240 * ExtSelectClipRgn [GDI32.@]
242 INT WINAPI
ExtSelectClipRgn( HDC hdc
, HRGN hrgn
, INT fnMode
)
245 DC
* dc
= get_dc_ptr( hdc
);
247 TRACE("%p %p %d\n", hdc
, hrgn
, fnMode
);
251 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pExtSelectClipRgn
);
253 retval
= physdev
->funcs
->pExtSelectClipRgn( physdev
, hrgn
, fnMode
);
254 release_dc_ptr( dc
);
259 /***********************************************************************
260 * __wine_set_visible_region (GDI32.@)
262 void CDECL
__wine_set_visible_region( HDC hdc
, HRGN hrgn
, const RECT
*vis_rect
)
266 if (!(dc
= get_dc_ptr( hdc
))) return;
268 TRACE( "%p %p %s\n", hdc
, hrgn
, wine_dbgstr_rect(vis_rect
) );
270 /* map region to DC coordinates */
271 OffsetRgn( hrgn
, -vis_rect
->left
, -vis_rect
->top
);
273 if (dc
->hVisRgn
) DeleteObject( dc
->hVisRgn
);
275 dc
->vis_rect
= *vis_rect
;
277 DC_UpdateXforms( dc
);
278 update_dc_clipping( dc
);
279 release_dc_ptr( dc
);
283 /***********************************************************************
284 * OffsetClipRgn (GDI32.@)
286 INT WINAPI
OffsetClipRgn( HDC hdc
, INT x
, INT y
)
289 DC
*dc
= get_dc_ptr( hdc
);
291 TRACE("%p %d,%d\n", hdc
, x
, y
);
295 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pOffsetClipRgn
);
297 ret
= physdev
->funcs
->pOffsetClipRgn( physdev
, x
, y
);
298 release_dc_ptr( dc
);
304 /***********************************************************************
305 * ExcludeClipRect (GDI32.@)
307 INT WINAPI
ExcludeClipRect( HDC hdc
, INT left
, INT top
,
308 INT right
, INT bottom
)
311 DC
*dc
= get_dc_ptr( hdc
);
313 TRACE("%p %d,%d-%d,%d\n", hdc
, left
, top
, right
, bottom
);
317 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pExcludeClipRect
);
319 ret
= physdev
->funcs
->pExcludeClipRect( physdev
, left
, top
, right
, bottom
);
320 release_dc_ptr( dc
);
326 /***********************************************************************
327 * IntersectClipRect (GDI32.@)
329 INT WINAPI
IntersectClipRect( HDC hdc
, INT left
, INT top
, INT right
, INT bottom
)
332 DC
*dc
= get_dc_ptr( hdc
);
334 TRACE("%p %d,%d - %d,%d\n", hdc
, left
, top
, right
, bottom
);
338 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pIntersectClipRect
);
340 ret
= physdev
->funcs
->pIntersectClipRect( physdev
, left
, top
, right
, bottom
);
341 release_dc_ptr( dc
);
347 /***********************************************************************
348 * PtVisible (GDI32.@)
350 BOOL WINAPI
PtVisible( HDC hdc
, INT x
, INT y
)
355 DC
*dc
= get_dc_ptr( hdc
);
357 TRACE("%p %d,%d\n", hdc
, x
, y
);
358 if (!dc
) return FALSE
;
362 LPtoDP( hdc
, &pt
, 1 );
364 ret
= (!get_dc_visrect( dc
, &visrect
) ||
365 (pt
.x
>= visrect
.left
&& pt
.x
< visrect
.right
&&
366 pt
.y
>= visrect
.top
&& pt
.y
< visrect
.bottom
));
367 if (ret
&& get_dc_region( dc
)) ret
= PtInRegion( get_dc_region( dc
), pt
.x
, pt
.y
);
368 release_dc_ptr( dc
);
373 /***********************************************************************
374 * RectVisible (GDI32.@)
376 BOOL WINAPI
RectVisible( HDC hdc
, const RECT
* rect
)
378 RECT tmpRect
, visrect
;
380 DC
*dc
= get_dc_ptr( hdc
);
381 if (!dc
) return FALSE
;
382 TRACE("%p %s\n", hdc
, wine_dbgstr_rect( rect
));
385 LPtoDP( hdc
, (POINT
*)&tmpRect
, 2 );
388 ret
= (!get_dc_visrect( dc
, &visrect
) || intersect_rect( &visrect
, &visrect
, &tmpRect
));
389 if (ret
&& get_dc_region( dc
)) ret
= RectInRegion( get_dc_region( dc
), &tmpRect
);
390 release_dc_ptr( dc
);
395 /***********************************************************************
396 * GetClipBox (GDI32.@)
398 INT WINAPI
GetClipBox( HDC hdc
, LPRECT rect
)
402 DC
*dc
= get_dc_ptr( hdc
);
403 if (!dc
) return ERROR
;
406 if (get_dc_region( dc
))
408 ret
= GetRgnBox( get_dc_region( dc
), rect
);
409 if (get_dc_visrect( dc
, &visrect
) && !intersect_rect( rect
, rect
, &visrect
))
414 ret
= is_rect_empty( &dc
->vis_rect
) ? ERROR
: SIMPLEREGION
;
415 *rect
= dc
->vis_rect
;
418 if (dc
->layout
& LAYOUT_RTL
)
420 int tmp
= rect
->left
;
421 rect
->left
= rect
->right
- 1;
422 rect
->right
= tmp
- 1;
424 DPtoLP( hdc
, (LPPOINT
)rect
, 2 );
425 release_dc_ptr( dc
);
426 TRACE("%p => %d %s\n", hdc
, ret
, wine_dbgstr_rect( rect
));
431 /***********************************************************************
432 * GetClipRgn (GDI32.@)
434 INT WINAPI
GetClipRgn( HDC hdc
, HRGN hRgn
)
438 if ((dc
= get_dc_ptr( hdc
)))
442 if( CombineRgn(hRgn
, dc
->hClipRgn
, 0, RGN_COPY
) != ERROR
)
445 if (dc
->layout
& LAYOUT_RTL
)
446 mirror_region( hRgn
, hRgn
, dc
->vis_rect
.right
- dc
->vis_rect
.left
);
450 release_dc_ptr( dc
);
456 /***********************************************************************
457 * GetMetaRgn (GDI32.@)
459 INT WINAPI
GetMetaRgn( HDC hdc
, HRGN hRgn
)
462 DC
* dc
= get_dc_ptr( hdc
);
466 if (dc
->hMetaRgn
&& CombineRgn( hRgn
, dc
->hMetaRgn
, 0, RGN_COPY
) != ERROR
)
469 if (dc
->layout
& LAYOUT_RTL
)
470 mirror_region( hRgn
, hRgn
, dc
->vis_rect
.right
- dc
->vis_rect
.left
);
472 release_dc_ptr( dc
);
478 /***********************************************************************
479 * GetRandomRgn [GDI32.@]
482 * This function is documented in MSDN online for the case of
483 * iCode == SYSRGN (4).
485 * For iCode == 1 it should return the clip region
486 * 2 " " " the meta region
487 * 3 " " " the intersection of the clip with
488 * the meta region (== 'Rao' region).
490 * See http://www.codeproject.com/gdi/cliprgnguide.asp
492 INT WINAPI
GetRandomRgn(HDC hDC
, HRGN hRgn
, INT iCode
)
496 DC
*dc
= get_dc_ptr( hDC
);
503 if (dc
->hClipRgn
) CombineRgn( hRgn
, dc
->hClipRgn
, 0, RGN_COPY
);
507 if (dc
->hMetaRgn
) CombineRgn( hRgn
, dc
->hMetaRgn
, 0, RGN_COPY
);
511 if (dc
->hClipRgn
&& dc
->hMetaRgn
) CombineRgn( hRgn
, dc
->hClipRgn
, dc
->hMetaRgn
, RGN_AND
);
512 else if (dc
->hClipRgn
) CombineRgn( hRgn
, dc
->hClipRgn
, 0, RGN_COPY
);
513 else if (dc
->hMetaRgn
) CombineRgn( hRgn
, dc
->hMetaRgn
, 0, RGN_COPY
);
516 case SYSRGN
: /* == 4 */
519 CombineRgn( hRgn
, dc
->hVisRgn
, 0, RGN_COPY
);
520 else if (get_dc_visrect( dc
, &visrect
))
521 SetRectRgn( hRgn
, visrect
.left
, visrect
.top
, visrect
.right
, visrect
.bottom
);
524 /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
525 if (ret
&& !(GetVersion() & 0x80000000)) OffsetRgn( hRgn
, dc
->vis_rect
.left
, dc
->vis_rect
.top
);
528 WARN("Unknown code %d\n", iCode
);
532 release_dc_ptr( dc
);
537 /***********************************************************************
538 * SetMetaRgn (GDI32.@)
540 INT WINAPI
SetMetaRgn( HDC hdc
)
544 DC
*dc
= get_dc_ptr( hdc
);
546 if (!dc
) return ERROR
;
552 /* the intersection becomes the new meta region */
553 CombineRgn( dc
->hMetaRgn
, dc
->hMetaRgn
, dc
->hClipRgn
, RGN_AND
);
554 DeleteObject( dc
->hClipRgn
);
559 dc
->hMetaRgn
= dc
->hClipRgn
;
563 /* else nothing to do */
565 /* Note: no need to call update_dc_clipping, the overall clip region hasn't changed */
567 ret
= GetRgnBox( dc
->hMetaRgn
, &dummy
);
568 release_dc_ptr( dc
);