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 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
->vis_rect
.left
, -dc
->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
->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 (GetRgnBox( 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
= CreateRectRgn( 0, 0, 0, 0 );
109 CombineRgn( dc
->region
, regions
[0], regions
[1], RGN_AND
);
110 if (count
> 2) CombineRgn( 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
)
146 DC
*dc
= get_nulldrv_dc( dev
);
154 if (dc
->hClipRgn
) DeleteObject( dc
->hClipRgn
);
163 FIXME("Unimplemented: hrgn NULL in mode: %d\n", mode
);
171 if (dc
->layout
& LAYOUT_RTL
)
173 if (!(mirrored
= CreateRectRgn( 0, 0, 0, 0 ))) return ERROR
;
174 mirror_region( mirrored
, rgn
, dc
->vis_rect
.right
- dc
->vis_rect
.left
);
179 create_default_clip_region( dc
);
181 if (mode
== RGN_COPY
)
182 ret
= CombineRgn( dc
->hClipRgn
, rgn
, 0, mode
);
184 ret
= CombineRgn( dc
->hClipRgn
, dc
->hClipRgn
, rgn
, mode
);
186 if (mirrored
) DeleteObject( mirrored
);
188 update_dc_clipping( dc
);
192 INT CDECL
nulldrv_ExcludeClipRect( PHYSDEV dev
, INT left
, INT top
, INT right
, INT bottom
)
194 DC
*dc
= get_nulldrv_dc( dev
);
195 RECT rect
= get_clip_rect( dc
, left
, top
, right
, bottom
);
199 if (!(rgn
= CreateRectRgnIndirect( &rect
))) return ERROR
;
200 if (!dc
->hClipRgn
) create_default_clip_region( dc
);
201 ret
= CombineRgn( dc
->hClipRgn
, dc
->hClipRgn
, rgn
, RGN_DIFF
);
203 if (ret
!= ERROR
) update_dc_clipping( dc
);
207 INT CDECL
nulldrv_IntersectClipRect( PHYSDEV dev
, INT left
, INT top
, INT right
, INT bottom
)
209 DC
*dc
= get_nulldrv_dc( dev
);
210 RECT rect
= get_clip_rect( dc
, left
, top
, right
, bottom
);
216 dc
->hClipRgn
= CreateRectRgnIndirect( &rect
);
221 if (!(rgn
= CreateRectRgnIndirect( &rect
))) return ERROR
;
222 ret
= CombineRgn( dc
->hClipRgn
, dc
->hClipRgn
, rgn
, RGN_AND
);
225 if (ret
!= ERROR
) update_dc_clipping( dc
);
229 INT CDECL
nulldrv_OffsetClipRgn( PHYSDEV dev
, INT x
, INT y
)
231 DC
*dc
= get_nulldrv_dc( dev
);
232 INT ret
= NULLREGION
;
236 x
= MulDiv( x
, dc
->vport_ext
.cx
, dc
->wnd_ext
.cx
);
237 y
= MulDiv( y
, dc
->vport_ext
.cy
, dc
->wnd_ext
.cy
);
238 if (dc
->layout
& LAYOUT_RTL
) x
= -x
;
239 ret
= OffsetRgn( dc
->hClipRgn
, x
, y
);
240 update_dc_clipping( dc
);
246 /***********************************************************************
247 * SelectClipRgn (GDI32.@)
249 INT WINAPI
SelectClipRgn( HDC hdc
, HRGN hrgn
)
251 return ExtSelectClipRgn( hdc
, hrgn
, RGN_COPY
);
255 /******************************************************************************
256 * ExtSelectClipRgn [GDI32.@]
258 INT WINAPI
ExtSelectClipRgn( HDC hdc
, HRGN hrgn
, INT fnMode
)
262 DC
* dc
= get_dc_ptr( hdc
);
264 TRACE("%p %p %d\n", hdc
, hrgn
, fnMode
);
266 if (!dc
) return ERROR
;
268 physdev
= GET_DC_PHYSDEV( dc
, pExtSelectClipRgn
);
269 retval
= physdev
->funcs
->pExtSelectClipRgn( physdev
, hrgn
, fnMode
);
270 release_dc_ptr( dc
);
274 /***********************************************************************
275 * __wine_set_visible_region (GDI32.@)
277 void CDECL
__wine_set_visible_region( HDC hdc
, HRGN hrgn
, const RECT
*vis_rect
, const RECT
*device_rect
,
278 struct window_surface
*surface
)
282 if (!(dc
= get_dc_ptr( hdc
))) return;
284 TRACE( "%p %p %s %s %p\n", hdc
, hrgn
,
285 wine_dbgstr_rect(vis_rect
), wine_dbgstr_rect(device_rect
), surface
);
287 /* map region to DC coordinates */
288 OffsetRgn( hrgn
, -vis_rect
->left
, -vis_rect
->top
);
290 if (dc
->hVisRgn
) DeleteObject( dc
->hVisRgn
);
292 dc
->vis_rect
= *vis_rect
;
293 dc
->device_rect
= *device_rect
;
295 dibdrv_set_window_surface( dc
, surface
);
296 DC_UpdateXforms( dc
);
297 update_dc_clipping( dc
);
298 release_dc_ptr( dc
);
302 /***********************************************************************
303 * OffsetClipRgn (GDI32.@)
305 INT WINAPI
OffsetClipRgn( HDC hdc
, INT x
, INT y
)
309 DC
*dc
= get_dc_ptr( hdc
);
311 TRACE("%p %d,%d\n", hdc
, x
, y
);
313 if (!dc
) return ERROR
;
315 physdev
= GET_DC_PHYSDEV( dc
, pOffsetClipRgn
);
316 ret
= physdev
->funcs
->pOffsetClipRgn( physdev
, x
, y
);
317 release_dc_ptr( dc
);
322 /***********************************************************************
323 * ExcludeClipRect (GDI32.@)
325 INT WINAPI
ExcludeClipRect( HDC hdc
, INT left
, INT top
,
326 INT right
, INT bottom
)
330 DC
*dc
= get_dc_ptr( hdc
);
332 TRACE("%p %d,%d-%d,%d\n", hdc
, left
, top
, right
, bottom
);
334 if (!dc
) return ERROR
;
336 physdev
= GET_DC_PHYSDEV( dc
, pExcludeClipRect
);
337 ret
= physdev
->funcs
->pExcludeClipRect( physdev
, left
, top
, right
, bottom
);
338 release_dc_ptr( dc
);
343 /***********************************************************************
344 * IntersectClipRect (GDI32.@)
346 INT WINAPI
IntersectClipRect( HDC hdc
, INT left
, INT top
, INT right
, INT bottom
)
350 DC
*dc
= get_dc_ptr( hdc
);
352 TRACE("%p %d,%d - %d,%d\n", hdc
, left
, top
, right
, bottom
);
354 if (!dc
) return ERROR
;
356 physdev
= GET_DC_PHYSDEV( dc
, pIntersectClipRect
);
357 ret
= physdev
->funcs
->pIntersectClipRect( physdev
, left
, top
, right
, bottom
);
358 release_dc_ptr( dc
);
363 /***********************************************************************
364 * PtVisible (GDI32.@)
366 BOOL WINAPI
PtVisible( HDC hdc
, INT x
, INT y
)
371 DC
*dc
= get_dc_ptr( hdc
);
373 TRACE("%p %d,%d\n", hdc
, x
, y
);
374 if (!dc
) return FALSE
;
378 lp_to_dp( dc
, &pt
, 1 );
380 ret
= (!get_dc_device_rect( dc
, &visrect
) ||
381 (pt
.x
>= visrect
.left
&& pt
.x
< visrect
.right
&&
382 pt
.y
>= visrect
.top
&& pt
.y
< visrect
.bottom
));
383 if (ret
&& get_dc_region( dc
)) ret
= PtInRegion( get_dc_region( dc
), pt
.x
, pt
.y
);
384 release_dc_ptr( dc
);
389 /***********************************************************************
390 * RectVisible (GDI32.@)
392 BOOL WINAPI
RectVisible( HDC hdc
, const RECT
* rect
)
394 RECT tmpRect
, visrect
;
396 DC
*dc
= get_dc_ptr( hdc
);
397 if (!dc
) return FALSE
;
398 TRACE("%p %s\n", hdc
, wine_dbgstr_rect( rect
));
401 lp_to_dp( dc
, (POINT
*)&tmpRect
, 2 );
402 order_rect( &tmpRect
);
405 ret
= (!get_dc_device_rect( dc
, &visrect
) || intersect_rect( &visrect
, &visrect
, &tmpRect
));
406 if (ret
&& get_dc_region( dc
)) ret
= RectInRegion( get_dc_region( dc
), &tmpRect
);
407 release_dc_ptr( dc
);
412 /***********************************************************************
413 * GetClipBox (GDI32.@)
415 INT WINAPI
GetClipBox( HDC hdc
, LPRECT rect
)
419 DC
*dc
= get_dc_ptr( hdc
);
420 if (!dc
) return ERROR
;
423 if (get_dc_region( dc
))
425 ret
= GetRgnBox( get_dc_region( dc
), rect
);
429 ret
= is_rect_empty( &dc
->vis_rect
) ? ERROR
: SIMPLEREGION
;
430 *rect
= dc
->vis_rect
;
433 if (get_dc_device_rect( dc
, &visrect
) && !intersect_rect( rect
, rect
, &visrect
)) ret
= NULLREGION
;
435 if (dc
->layout
& LAYOUT_RTL
)
437 int tmp
= rect
->left
;
438 rect
->left
= rect
->right
- 1;
439 rect
->right
= tmp
- 1;
441 dp_to_lp( dc
, (LPPOINT
)rect
, 2 );
442 release_dc_ptr( dc
);
443 TRACE("%p => %d %s\n", hdc
, ret
, wine_dbgstr_rect( rect
));
448 /***********************************************************************
449 * GetClipRgn (GDI32.@)
451 INT WINAPI
GetClipRgn( HDC hdc
, HRGN hRgn
)
455 if ((dc
= get_dc_ptr( hdc
)))
459 if( CombineRgn(hRgn
, dc
->hClipRgn
, 0, RGN_COPY
) != ERROR
)
462 if (dc
->layout
& LAYOUT_RTL
)
463 mirror_region( hRgn
, hRgn
, dc
->vis_rect
.right
- dc
->vis_rect
.left
);
467 release_dc_ptr( dc
);
473 /***********************************************************************
474 * GetMetaRgn (GDI32.@)
476 INT WINAPI
GetMetaRgn( HDC hdc
, HRGN hRgn
)
479 DC
* dc
= get_dc_ptr( hdc
);
483 if (dc
->hMetaRgn
&& CombineRgn( hRgn
, dc
->hMetaRgn
, 0, RGN_COPY
) != ERROR
)
486 if (dc
->layout
& LAYOUT_RTL
)
487 mirror_region( hRgn
, hRgn
, dc
->vis_rect
.right
- dc
->vis_rect
.left
);
489 release_dc_ptr( dc
);
495 /***********************************************************************
496 * GetRandomRgn [GDI32.@]
499 * This function is documented in MSDN online for the case of
500 * iCode == SYSRGN (4).
502 * For iCode == 1 it should return the clip region
503 * 2 " " " the meta region
504 * 3 " " " the intersection of the clip with
505 * the meta region (== 'Rao' region).
507 * See http://www.codeproject.com/gdi/cliprgnguide.asp
509 INT WINAPI
GetRandomRgn(HDC hDC
, HRGN hRgn
, INT iCode
)
512 DC
*dc
= get_dc_ptr( hDC
);
519 if (dc
->hClipRgn
) CombineRgn( hRgn
, dc
->hClipRgn
, 0, RGN_COPY
);
523 if (dc
->hMetaRgn
) CombineRgn( hRgn
, dc
->hMetaRgn
, 0, RGN_COPY
);
527 if (dc
->hClipRgn
&& dc
->hMetaRgn
) CombineRgn( hRgn
, dc
->hClipRgn
, dc
->hMetaRgn
, RGN_AND
);
528 else if (dc
->hClipRgn
) CombineRgn( hRgn
, dc
->hClipRgn
, 0, RGN_COPY
);
529 else if (dc
->hMetaRgn
) CombineRgn( hRgn
, dc
->hMetaRgn
, 0, RGN_COPY
);
532 case SYSRGN
: /* == 4 */
536 CombineRgn( hRgn
, dc
->hVisRgn
, 0, RGN_COPY
);
537 /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
538 if (!(GetVersion() & 0x80000000)) OffsetRgn( hRgn
, dc
->vis_rect
.left
, dc
->vis_rect
.top
);
540 else if (!is_rect_empty( &dc
->device_rect
))
541 SetRectRgn( hRgn
, dc
->device_rect
.left
, dc
->device_rect
.top
,
542 dc
->device_rect
.right
, dc
->device_rect
.bottom
);
547 WARN("Unknown code %d\n", iCode
);
551 release_dc_ptr( dc
);
556 /***********************************************************************
557 * SetMetaRgn (GDI32.@)
559 INT WINAPI
SetMetaRgn( HDC hdc
)
563 DC
*dc
= get_dc_ptr( hdc
);
565 if (!dc
) return ERROR
;
571 /* the intersection becomes the new meta region */
572 CombineRgn( dc
->hMetaRgn
, dc
->hMetaRgn
, dc
->hClipRgn
, RGN_AND
);
573 DeleteObject( dc
->hClipRgn
);
578 dc
->hMetaRgn
= dc
->hClipRgn
;
582 /* else nothing to do */
584 /* Note: no need to call update_dc_clipping, the overall clip region hasn't changed */
586 ret
= GetRgnBox( dc
->hMetaRgn
, &dummy
);
587 release_dc_ptr( dc
);