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
27 #include "wine/winuser16.h"
28 #include "gdi_private.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(clipping
);
34 /***********************************************************************
37 * Return the total clip region (if any).
39 static inline HRGN
get_clip_region( DC
* dc
)
41 if (dc
->hMetaClipRgn
) return dc
->hMetaClipRgn
;
42 if (dc
->hMetaRgn
) return dc
->hMetaRgn
;
47 /***********************************************************************
48 * CLIPPING_UpdateGCRegion
50 * Update the GC clip region when the ClipRgn or VisRgn have changed.
52 void CLIPPING_UpdateGCRegion( DC
* dc
)
58 ERR("hVisRgn is zero. Please report this.\n" );
62 if (dc
->flags
& DC_DIRTY
) ERR( "DC is dirty. Please report this.\n" );
64 /* update the intersection of meta and clip regions */
65 if (dc
->hMetaRgn
&& dc
->hClipRgn
)
67 if (!dc
->hMetaClipRgn
) dc
->hMetaClipRgn
= CreateRectRgn( 0, 0, 0, 0 );
68 CombineRgn( dc
->hMetaClipRgn
, dc
->hClipRgn
, dc
->hMetaRgn
, RGN_AND
);
69 clip_rgn
= dc
->hMetaClipRgn
;
71 else /* only one is set, no need for an intersection */
73 if (dc
->hMetaClipRgn
) DeleteObject( dc
->hMetaClipRgn
);
75 clip_rgn
= dc
->hMetaRgn
? dc
->hMetaRgn
: dc
->hClipRgn
;
78 if (dc
->funcs
->pSetDeviceClipping
)
79 dc
->funcs
->pSetDeviceClipping( dc
->physDev
, dc
->hVisRgn
, clip_rgn
);
82 /***********************************************************************
83 * create_default_clip_region
85 * Create a default clipping region when none already exists.
87 static inline void create_default_clip_region( DC
* dc
)
89 dc
->hClipRgn
= CreateRectRgn(0, 0,
90 GetDeviceCaps( dc
->hSelf
, HORZRES
),
91 GetDeviceCaps( dc
->hSelf
, VERTRES
));
95 /***********************************************************************
96 * SelectClipRgn (GDI32.@)
98 INT WINAPI
SelectClipRgn( HDC hdc
, HRGN hrgn
)
100 return ExtSelectClipRgn( hdc
, hrgn
, RGN_COPY
);
104 /******************************************************************************
105 * ExtSelectClipRgn [GDI32.@]
107 INT WINAPI
ExtSelectClipRgn( HDC hdc
, HRGN hrgn
, INT fnMode
)
111 DC
* dc
= DC_GetDCUpdate( hdc
);
112 if (!dc
) return ERROR
;
114 TRACE("%p %p %d\n", hdc
, hrgn
, fnMode
);
116 if (dc
->funcs
->pExtSelectClipRgn
)
118 retval
= dc
->funcs
->pExtSelectClipRgn( dc
->physDev
, hrgn
, fnMode
);
119 GDI_ReleaseObj( hdc
);
125 if (fnMode
== RGN_COPY
)
127 if (dc
->hClipRgn
) DeleteObject( dc
->hClipRgn
);
132 FIXME("Unimplemented: hrgn NULL in mode: %d\n", fnMode
);
133 GDI_ReleaseObj( hdc
);
140 create_default_clip_region( dc
);
142 if(fnMode
== RGN_COPY
)
143 CombineRgn( dc
->hClipRgn
, hrgn
, 0, fnMode
);
145 CombineRgn( dc
->hClipRgn
, dc
->hClipRgn
, hrgn
, fnMode
);
148 CLIPPING_UpdateGCRegion( dc
);
149 GDI_ReleaseObj( hdc
);
151 return GetClipBox(hdc
, &rect
);
154 /***********************************************************************
155 * SelectVisRgn (GDI.105)
157 INT16 WINAPI
SelectVisRgn16( HDC16 hdc16
, HRGN16 hrgn
)
160 HDC hdc
= HDC_32( hdc16
);
163 if (!hrgn
) return ERROR
;
164 if (!(dc
= DC_GetDCPtr( hdc
))) return ERROR
;
166 TRACE("%p %04x\n", hdc
, hrgn
);
168 dc
->flags
&= ~DC_DIRTY
;
170 retval
= CombineRgn( dc
->hVisRgn
, HRGN_32(hrgn
), 0, RGN_COPY
);
171 CLIPPING_UpdateGCRegion( dc
);
172 GDI_ReleaseObj( hdc
);
177 /***********************************************************************
178 * OffsetClipRgn (GDI32.@)
180 INT WINAPI
OffsetClipRgn( HDC hdc
, INT x
, INT y
)
182 INT ret
= SIMPLEREGION
;
183 DC
*dc
= DC_GetDCUpdate( hdc
);
184 if (!dc
) return ERROR
;
186 TRACE("%p %d,%d\n", hdc
, x
, y
);
188 if(dc
->funcs
->pOffsetClipRgn
)
190 ret
= dc
->funcs
->pOffsetClipRgn( dc
->physDev
, x
, y
);
191 /* FIXME: ret is just a success flag, we should return a proper value */
193 else if (dc
->hClipRgn
) {
194 ret
= OffsetRgn( dc
->hClipRgn
, MulDiv( x
, dc
->vportExtX
, dc
->wndExtX
),
195 MulDiv( y
, dc
->vportExtY
, dc
->wndExtY
) );
196 CLIPPING_UpdateGCRegion( dc
);
198 GDI_ReleaseObj( hdc
);
203 /***********************************************************************
204 * OffsetVisRgn (GDI.102)
206 INT16 WINAPI
OffsetVisRgn16( HDC16 hdc16
, INT16 x
, INT16 y
)
209 HDC hdc
= HDC_32( hdc16
);
210 DC
* dc
= DC_GetDCUpdate( hdc
);
211 if (!dc
) return ERROR
;
212 TRACE("%p %d,%d\n", hdc
, x
, y
);
213 retval
= OffsetRgn( dc
->hVisRgn
, x
, y
);
214 CLIPPING_UpdateGCRegion( dc
);
215 GDI_ReleaseObj( hdc
);
220 /***********************************************************************
221 * ExcludeClipRect (GDI32.@)
223 INT WINAPI
ExcludeClipRect( HDC hdc
, INT left
, INT top
,
224 INT right
, INT bottom
)
228 DC
*dc
= DC_GetDCUpdate( hdc
);
229 if (!dc
) return ERROR
;
231 TRACE("%p %dx%d,%dx%d\n", hdc
, left
, top
, right
, bottom
);
233 if(dc
->funcs
->pExcludeClipRect
)
235 ret
= dc
->funcs
->pExcludeClipRect( dc
->physDev
, left
, top
, right
, bottom
);
236 /* FIXME: ret is just a success flag, we should return a proper value */
246 LPtoDP( hdc
, pt
, 2 );
247 if (!(newRgn
= CreateRectRgn( pt
[0].x
, pt
[0].y
, pt
[1].x
, pt
[1].y
))) ret
= ERROR
;
251 create_default_clip_region( dc
);
252 ret
= CombineRgn( dc
->hClipRgn
, dc
->hClipRgn
, newRgn
, RGN_DIFF
);
253 DeleteObject( newRgn
);
255 if (ret
!= ERROR
) CLIPPING_UpdateGCRegion( dc
);
257 GDI_ReleaseObj( hdc
);
262 /***********************************************************************
263 * IntersectClipRect (GDI32.@)
265 INT WINAPI
IntersectClipRect( HDC hdc
, INT left
, INT top
, INT right
, INT bottom
)
268 DC
*dc
= DC_GetDCUpdate( hdc
);
269 if (!dc
) return ERROR
;
271 TRACE("%p %d,%d - %d,%d\n", hdc
, left
, top
, right
, bottom
);
273 if(dc
->funcs
->pIntersectClipRect
)
275 ret
= dc
->funcs
->pIntersectClipRect( dc
->physDev
, left
, top
, right
, bottom
);
276 /* FIXME: ret is just a success flag, we should return a proper value */
287 LPtoDP( hdc
, pt
, 2 );
291 dc
->hClipRgn
= CreateRectRgn( pt
[0].x
, pt
[0].y
, pt
[1].x
, pt
[1].y
);
298 if (!(newRgn
= CreateRectRgn( pt
[0].x
, pt
[0].y
, pt
[1].x
, pt
[1].y
))) ret
= ERROR
;
301 ret
= CombineRgn( dc
->hClipRgn
, dc
->hClipRgn
, newRgn
, RGN_AND
);
302 DeleteObject( newRgn
);
305 if (ret
!= ERROR
) CLIPPING_UpdateGCRegion( dc
);
307 GDI_ReleaseObj( hdc
);
312 /***********************************************************************
313 * ExcludeVisRect (GDI.73)
315 INT16 WINAPI
ExcludeVisRect16( HDC16 hdc16
, INT16 left
, INT16 top
, INT16 right
, INT16 bottom
)
320 HDC hdc
= HDC_32( hdc16
);
321 DC
* dc
= DC_GetDCUpdate( hdc
);
322 if (!dc
) return ERROR
;
329 LPtoDP( hdc
, pt
, 2 );
331 TRACE("%p %d,%d - %d,%d\n", hdc
, pt
[0].x
, pt
[0].y
, pt
[1].x
, pt
[1].y
);
333 if (!(tempRgn
= CreateRectRgn( pt
[0].x
, pt
[0].y
, pt
[1].x
, pt
[1].y
))) ret
= ERROR
;
336 ret
= CombineRgn( dc
->hVisRgn
, dc
->hVisRgn
, tempRgn
, RGN_DIFF
);
337 DeleteObject( tempRgn
);
339 if (ret
!= ERROR
) CLIPPING_UpdateGCRegion( dc
);
340 GDI_ReleaseObj( hdc
);
345 /***********************************************************************
346 * IntersectVisRect (GDI.98)
348 INT16 WINAPI
IntersectVisRect16( HDC16 hdc16
, INT16 left
, INT16 top
, INT16 right
, INT16 bottom
)
353 HDC hdc
= HDC_32( hdc16
);
354 DC
* dc
= DC_GetDCUpdate( hdc
);
355 if (!dc
) return ERROR
;
362 LPtoDP( hdc
, pt
, 2 );
364 TRACE("%p %d,%d - %d,%d\n", hdc
, pt
[0].x
, pt
[0].y
, pt
[1].x
, pt
[1].y
);
367 if (!(tempRgn
= CreateRectRgn( pt
[0].x
, pt
[0].y
, pt
[1].x
, pt
[1].y
))) ret
= ERROR
;
370 ret
= CombineRgn( dc
->hVisRgn
, dc
->hVisRgn
, tempRgn
, RGN_AND
);
371 DeleteObject( tempRgn
);
373 if (ret
!= ERROR
) CLIPPING_UpdateGCRegion( dc
);
374 GDI_ReleaseObj( hdc
);
379 /***********************************************************************
380 * PtVisible (GDI32.@)
382 BOOL WINAPI
PtVisible( HDC hdc
, INT x
, INT y
)
387 DC
*dc
= DC_GetDCUpdate( hdc
);
389 TRACE("%p %d,%d\n", hdc
, x
, y
);
390 if (!dc
) return FALSE
;
394 LPtoDP( hdc
, &pt
, 1 );
395 ret
= PtInRegion( dc
->hVisRgn
, pt
.x
, pt
.y
);
396 if (ret
&& (clip
= get_clip_region(dc
))) ret
= PtInRegion( clip
, pt
.x
, pt
.y
);
397 GDI_ReleaseObj( hdc
);
402 /***********************************************************************
403 * RectVisible (GDI32.@)
405 BOOL WINAPI
RectVisible( HDC hdc
, const RECT
* rect
)
410 DC
*dc
= DC_GetDCUpdate( hdc
);
411 if (!dc
) return FALSE
;
412 TRACE("%p %d,%dx%d,%d\n", hdc
, rect
->left
, rect
->top
, rect
->right
, rect
->bottom
);
415 LPtoDP( hdc
, (POINT
*)&tmpRect
, 2 );
417 if ((clip
= get_clip_region(dc
)))
419 HRGN hrgn
= CreateRectRgn( 0, 0, 0, 0 );
420 CombineRgn( hrgn
, dc
->hVisRgn
, clip
, RGN_AND
);
421 ret
= RectInRegion( hrgn
, &tmpRect
);
422 DeleteObject( hrgn
);
424 else ret
= RectInRegion( dc
->hVisRgn
, &tmpRect
);
425 GDI_ReleaseObj( hdc
);
430 /***********************************************************************
431 * GetClipBox (GDI32.@)
433 INT WINAPI
GetClipBox( HDC hdc
, LPRECT rect
)
437 DC
*dc
= DC_GetDCUpdate( hdc
);
438 if (!dc
) return ERROR
;
439 if ((clip
= get_clip_region(dc
)))
441 HRGN hrgn
= CreateRectRgn( 0, 0, 0, 0 );
442 CombineRgn( hrgn
, dc
->hVisRgn
, clip
, RGN_AND
);
443 ret
= GetRgnBox( hrgn
, rect
);
444 DeleteObject( hrgn
);
446 else ret
= GetRgnBox( dc
->hVisRgn
, rect
);
447 DPtoLP( hdc
, (LPPOINT
)rect
, 2 );
448 GDI_ReleaseObj( hdc
);
453 /***********************************************************************
454 * GetClipRgn (GDI32.@)
456 INT WINAPI
GetClipRgn( HDC hdc
, HRGN hRgn
)
460 if (hRgn
&& (dc
= DC_GetDCPtr( hdc
)))
464 if( CombineRgn(hRgn
, dc
->hClipRgn
, 0, RGN_COPY
) != ERROR
) ret
= 1;
467 GDI_ReleaseObj( hdc
);
473 /***********************************************************************
474 * GetMetaRgn (GDI32.@)
476 INT WINAPI
GetMetaRgn( HDC hdc
, HRGN hRgn
)
479 DC
* dc
= DC_GetDCPtr( hdc
);
483 if (dc
->hMetaRgn
&& CombineRgn( hRgn
, dc
->hMetaRgn
, 0, RGN_COPY
) != ERROR
)
485 GDI_ReleaseObj( hdc
);
491 /***********************************************************************
492 * SaveVisRgn (GDI.129)
494 HRGN16 WINAPI
SaveVisRgn16( HDC16 hdc16
)
496 struct saved_visrgn
*saved
;
497 HDC hdc
= HDC_32( hdc16
);
498 DC
*dc
= DC_GetDCUpdate( hdc
);
503 if (!(saved
= HeapAlloc( GetProcessHeap(), 0, sizeof(*saved
) ))) goto error
;
504 if (!(saved
->hrgn
= CreateRectRgn( 0, 0, 0, 0 ))) goto error
;
505 CombineRgn( saved
->hrgn
, dc
->hVisRgn
, 0, RGN_COPY
);
506 saved
->next
= dc
->saved_visrgn
;
507 dc
->saved_visrgn
= saved
;
508 GDI_ReleaseObj( hdc
);
509 return HRGN_16(saved
->hrgn
);
512 GDI_ReleaseObj( hdc
);
513 HeapFree( GetProcessHeap(), 0, saved
);
518 /***********************************************************************
519 * RestoreVisRgn (GDI.130)
521 INT16 WINAPI
RestoreVisRgn16( HDC16 hdc16
)
523 struct saved_visrgn
*saved
;
524 HDC hdc
= HDC_32( hdc16
);
525 DC
*dc
= DC_GetDCPtr( hdc
);
528 if (!dc
) return ERROR
;
532 if (!(saved
= dc
->saved_visrgn
)) goto done
;
534 ret
= CombineRgn( dc
->hVisRgn
, saved
->hrgn
, 0, RGN_COPY
);
535 dc
->saved_visrgn
= saved
->next
;
536 DeleteObject( saved
->hrgn
);
537 HeapFree( GetProcessHeap(), 0, saved
);
538 dc
->flags
&= ~DC_DIRTY
;
539 CLIPPING_UpdateGCRegion( dc
);
541 GDI_ReleaseObj( hdc
);
546 /***********************************************************************
547 * GetRandomRgn [GDI32.@]
550 * This function is documented in MSDN online for the case of
551 * iCode == SYSRGN (4).
553 * For iCode == 1 it should return the clip region
554 * 2 " " " the meta region
555 * 3 " " " the intersection of the clip with
556 * the meta region (== 'Rao' region).
558 * See http://www.codeproject.com/gdi/cliprgnguide.asp
560 INT WINAPI
GetRandomRgn(HDC hDC
, HRGN hRgn
, INT iCode
)
563 DC
*dc
= DC_GetDCPtr( hDC
);
576 rgn
= dc
->hMetaClipRgn
;
577 if(!rgn
) rgn
= dc
->hClipRgn
;
578 if(!rgn
) rgn
= dc
->hMetaRgn
;
580 case SYSRGN
: /* == 4 */
584 WARN("Unknown code %d\n", iCode
);
585 GDI_ReleaseObj( hDC
);
588 if (rgn
) CombineRgn( hRgn
, rgn
, 0, RGN_COPY
);
589 GDI_ReleaseObj( hDC
);
591 /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
592 if (iCode
== SYSRGN
&& !(GetVersion() & 0x80000000))
595 GetDCOrgEx( hDC
, &org
);
596 OffsetRgn( hRgn
, org
.x
, org
.y
);
602 /***********************************************************************
603 * SetMetaRgn (GDI32.@)
605 INT WINAPI
SetMetaRgn( HDC hdc
)
609 DC
*dc
= DC_GetDCPtr( hdc
);
611 if (!dc
) return ERROR
;
613 if (dc
->hMetaClipRgn
)
615 /* the intersection becomes the new meta region */
616 DeleteObject( dc
->hMetaRgn
);
617 DeleteObject( dc
->hClipRgn
);
618 dc
->hMetaRgn
= dc
->hMetaClipRgn
;
620 dc
->hMetaClipRgn
= 0;
622 else if (dc
->hClipRgn
)
624 dc
->hMetaRgn
= dc
->hClipRgn
;
627 /* else nothing to do */
629 /* Note: no need to call CLIPPING_UpdateGCRegion, the overall clip region hasn't changed */
631 ret
= GetRgnBox( dc
->hMetaRgn
, &dummy
);
632 GDI_ReleaseObj( hdc
);