2 * DC clipping functions
4 * Copyright 1993 Alexandre Julliard
10 #include "wine/winuser16.h"
13 #include "debugtools.h"
15 DEFAULT_DEBUG_CHANNEL(clipping
);
18 /***********************************************************************
19 * CLIPPING_UpdateGCRegion
21 * Update the GC clip region when the ClipRgn or VisRgn have changed.
23 void CLIPPING_UpdateGCRegion( DC
* dc
)
25 if (!dc
->hGCClipRgn
) dc
->hGCClipRgn
= CreateRectRgn( 0, 0, 0, 0 );
29 ERR("hVisRgn is zero. Please report this.\n" );
33 if (dc
->flags
& DC_DIRTY
) ERR( "DC is dirty. Please report this.\n" );
36 CombineRgn( dc
->hGCClipRgn
, dc
->hVisRgn
, 0, RGN_COPY
);
38 CombineRgn(dc
->hGCClipRgn
, dc
->hClipRgn
, dc
->hVisRgn
, RGN_AND
);
39 if (dc
->funcs
->pSetDeviceClipping
) dc
->funcs
->pSetDeviceClipping( dc
);
43 /***********************************************************************
44 * SelectClipRgn (GDI.44)
46 INT16 WINAPI
SelectClipRgn16( HDC16 hdc
, HRGN16 hrgn
)
48 return (INT16
)SelectClipRgn( hdc
, hrgn
);
52 /***********************************************************************
53 * SelectClipRgn (GDI32.@)
55 INT WINAPI
SelectClipRgn( HDC hdc
, HRGN hrgn
)
57 return ExtSelectClipRgn( hdc
, hrgn
, RGN_COPY
);
60 /******************************************************************************
61 * ExtSelectClipRgn [GDI.508]
63 INT16 WINAPI
ExtSelectClipRgn16( HDC16 hdc
, HRGN16 hrgn
, INT16 fnMode
)
65 return (INT16
) ExtSelectClipRgn((HDC
) hdc
, (HRGN
) hrgn
, fnMode
);
68 /******************************************************************************
69 * ExtSelectClipRgn [GDI32.@]
71 INT WINAPI
ExtSelectClipRgn( HDC hdc
, HRGN hrgn
, INT fnMode
)
74 DC
* dc
= DC_GetDCUpdate( hdc
);
75 if (!dc
) return ERROR
;
77 TRACE("%04x %04x %d\n", hdc
, hrgn
, fnMode
);
81 if (fnMode
== RGN_COPY
)
83 if (dc
->hClipRgn
) DeleteObject( dc
->hClipRgn
);
85 retval
= SIMPLEREGION
; /* Clip region == whole DC */
89 FIXME("Unimplemented: hrgn NULL in mode: %d\n", fnMode
);
90 GDI_ReleaseObj( hdc
);
99 GetRgnBox( dc
->hVisRgn
, &rect
);
100 dc
->hClipRgn
= CreateRectRgnIndirect( &rect
);
103 OffsetRgn( dc
->hClipRgn
, -dc
->DCOrgX
, -dc
->DCOrgY
);
104 if(fnMode
== RGN_COPY
)
105 retval
= CombineRgn( dc
->hClipRgn
, hrgn
, 0, fnMode
);
107 retval
= CombineRgn( dc
->hClipRgn
, dc
->hClipRgn
, hrgn
, fnMode
);
108 OffsetRgn( dc
->hClipRgn
, dc
->DCOrgX
, dc
->DCOrgY
);
111 CLIPPING_UpdateGCRegion( dc
);
112 GDI_ReleaseObj( hdc
);
116 /***********************************************************************
117 * SelectVisRgn (GDI.105)
119 INT16 WINAPI
SelectVisRgn16( HDC16 hdc
, HRGN16 hrgn
)
124 if (!hrgn
) return ERROR
;
125 if (!(dc
= DC_GetDCPtr( hdc
))) return ERROR
;
127 TRACE("%04x %04x\n", hdc
, hrgn
);
129 dc
->flags
&= ~DC_DIRTY
;
131 retval
= CombineRgn16( dc
->hVisRgn
, hrgn
, 0, RGN_COPY
);
132 CLIPPING_UpdateGCRegion( dc
);
133 GDI_ReleaseObj( hdc
);
138 /***********************************************************************
139 * OffsetClipRgn (GDI.32)
141 INT16 WINAPI
OffsetClipRgn16( HDC16 hdc
, INT16 x
, INT16 y
)
143 return (INT16
)OffsetClipRgn( hdc
, x
, y
);
147 /***********************************************************************
148 * OffsetClipRgn (GDI32.@)
150 INT WINAPI
OffsetClipRgn( HDC hdc
, INT x
, INT y
)
152 INT ret
= SIMPLEREGION
;
153 DC
*dc
= DC_GetDCUpdate( hdc
);
154 if (!dc
) return ERROR
;
156 TRACE("%04x %d,%d\n", hdc
, x
, y
);
158 if(dc
->funcs
->pOffsetClipRgn
)
159 ret
= dc
->funcs
->pOffsetClipRgn( dc
, x
, y
);
160 else if (dc
->hClipRgn
) {
161 ret
= OffsetRgn( dc
->hClipRgn
, XLSTODS(dc
,x
), YLSTODS(dc
,y
));
162 CLIPPING_UpdateGCRegion( dc
);
164 GDI_ReleaseObj( hdc
);
169 /***********************************************************************
170 * OffsetVisRgn (GDI.102)
172 INT16 WINAPI
OffsetVisRgn16( HDC16 hdc
, INT16 x
, INT16 y
)
175 DC
* dc
= DC_GetDCUpdate( hdc
);
176 if (!dc
) return ERROR
;
177 TRACE("%04x %d,%d\n", hdc
, x
, y
);
178 retval
= OffsetRgn( dc
->hVisRgn
, x
, y
);
179 CLIPPING_UpdateGCRegion( dc
);
180 GDI_ReleaseObj( hdc
);
185 /***********************************************************************
186 * CLIPPING_IntersectClipRect
188 * Helper function for {Intersect,Exclude}ClipRect, can be called from
189 * elsewhere (like ExtTextOut()) to skip redundant metafile update and
190 * coordinate conversion.
192 INT
CLIPPING_IntersectClipRect( DC
* dc
, INT left
, INT top
,
193 INT right
, INT bottom
, UINT flags
)
201 bottom
+= dc
->DCOrgY
;
203 if (!(newRgn
= CreateRectRgn( left
, top
, right
, bottom
))) return ERROR
;
206 if( flags
& CLIP_INTERSECT
)
208 dc
->hClipRgn
= newRgn
;
209 CLIPPING_UpdateGCRegion( dc
);
212 else if( flags
& CLIP_EXCLUDE
)
214 dc
->hClipRgn
= CreateRectRgn( 0, 0, 0, 0 );
215 CombineRgn( dc
->hClipRgn
, dc
->hVisRgn
, 0, RGN_COPY
);
217 else WARN("No hClipRgn and flags are %x\n",flags
);
220 ret
= CombineRgn( newRgn
, dc
->hClipRgn
, newRgn
,
221 (flags
& CLIP_EXCLUDE
) ? RGN_DIFF
: RGN_AND
);
224 if (!(flags
& CLIP_KEEPRGN
)) DeleteObject( dc
->hClipRgn
);
225 dc
->hClipRgn
= newRgn
;
226 CLIPPING_UpdateGCRegion( dc
);
228 else DeleteObject( newRgn
);
233 /***********************************************************************
234 * ExcludeClipRect (GDI.21)
236 INT16 WINAPI
ExcludeClipRect16( HDC16 hdc
, INT16 left
, INT16 top
,
237 INT16 right
, INT16 bottom
)
239 return (INT16
)ExcludeClipRect( hdc
, left
, top
, right
, bottom
);
243 /***********************************************************************
244 * ExcludeClipRect (GDI32.@)
246 INT WINAPI
ExcludeClipRect( HDC hdc
, INT left
, INT top
,
247 INT right
, INT bottom
)
250 DC
*dc
= DC_GetDCUpdate( hdc
);
251 if (!dc
) return ERROR
;
253 TRACE("%04x %dx%d,%dx%d\n", hdc
, left
, top
, right
, bottom
);
255 if(dc
->funcs
->pExcludeClipRect
)
256 ret
= dc
->funcs
->pExcludeClipRect( dc
, left
, top
, right
, bottom
);
258 left
= XLPTODP( dc
, left
);
259 right
= XLPTODP( dc
, right
);
260 top
= YLPTODP( dc
, top
);
261 bottom
= YLPTODP( dc
, bottom
);
263 ret
= CLIPPING_IntersectClipRect( dc
, left
, top
, right
, bottom
, CLIP_EXCLUDE
);
265 GDI_ReleaseObj( hdc
);
270 /***********************************************************************
271 * IntersectClipRect (GDI.22)
273 INT16 WINAPI
IntersectClipRect16( HDC16 hdc
, INT16 left
, INT16 top
,
274 INT16 right
, INT16 bottom
)
276 return (INT16
)IntersectClipRect( hdc
, left
, top
, right
, bottom
);
280 /***********************************************************************
281 * IntersectClipRect (GDI32.@)
283 INT WINAPI
IntersectClipRect( HDC hdc
, INT left
, INT top
,
284 INT right
, INT bottom
)
287 DC
*dc
= DC_GetDCUpdate( hdc
);
288 if (!dc
) return ERROR
;
290 TRACE("%04x %dx%d,%dx%d\n", hdc
, left
, top
, right
, bottom
);
292 if(dc
->funcs
->pIntersectClipRect
)
293 ret
= dc
->funcs
->pIntersectClipRect( dc
, left
, top
, right
, bottom
);
295 left
= XLPTODP( dc
, left
);
296 right
= XLPTODP( dc
, right
);
297 top
= YLPTODP( dc
, top
);
298 bottom
= YLPTODP( dc
, bottom
);
300 ret
= CLIPPING_IntersectClipRect( dc
, left
, top
, right
, bottom
, CLIP_INTERSECT
);
302 GDI_ReleaseObj( hdc
);
307 /***********************************************************************
308 * CLIPPING_IntersectVisRect
310 * Helper function for {Intersect,Exclude}VisRect, can be called from
311 * elsewhere (like ExtTextOut()) to skip redundant metafile update and
312 * coordinate conversion.
314 INT
CLIPPING_IntersectVisRect( DC
* dc
, INT left
, INT top
,
315 INT right
, INT bottom
,
318 HRGN tempRgn
, newRgn
;
324 bottom
+= dc
->DCOrgY
;
326 if (!(newRgn
= CreateRectRgn( 0, 0, 0, 0 ))) return ERROR
;
327 if (!(tempRgn
= CreateRectRgn( left
, top
, right
, bottom
)))
329 DeleteObject( newRgn
);
332 ret
= CombineRgn( newRgn
, dc
->hVisRgn
, tempRgn
,
333 exclude
? RGN_DIFF
: RGN_AND
);
334 DeleteObject( tempRgn
);
338 RGNOBJ
*newObj
= (RGNOBJ
*)GDI_GetObjPtr( newRgn
, REGION_MAGIC
);
341 RGNOBJ
*prevObj
= (RGNOBJ
*)GDI_GetObjPtr( dc
->hVisRgn
, REGION_MAGIC
);
344 newObj
->header
.hNext
= prevObj
->header
.hNext
;
345 GDI_ReleaseObj( dc
->hVisRgn
);
347 GDI_ReleaseObj( newRgn
);
349 DeleteObject( dc
->hVisRgn
);
350 dc
->hVisRgn
= newRgn
;
351 CLIPPING_UpdateGCRegion( dc
);
353 else DeleteObject( newRgn
);
358 /***********************************************************************
359 * ExcludeVisRect (GDI.73)
361 INT16 WINAPI
ExcludeVisRect16( HDC16 hdc
, INT16 left
, INT16 top
,
362 INT16 right
, INT16 bottom
)
365 DC
* dc
= DC_GetDCUpdate( hdc
);
366 if (!dc
) return ERROR
;
368 left
= XLPTODP( dc
, left
);
369 right
= XLPTODP( dc
, right
);
370 top
= YLPTODP( dc
, top
);
371 bottom
= YLPTODP( dc
, bottom
);
373 TRACE("%04x %dx%d,%dx%d\n", hdc
, left
, top
, right
, bottom
);
375 ret
= CLIPPING_IntersectVisRect( dc
, left
, top
, right
, bottom
, TRUE
);
376 GDI_ReleaseObj( hdc
);
381 /***********************************************************************
382 * IntersectVisRect (GDI.98)
384 INT16 WINAPI
IntersectVisRect16( HDC16 hdc
, INT16 left
, INT16 top
,
385 INT16 right
, INT16 bottom
)
388 DC
* dc
= DC_GetDCUpdate( hdc
);
389 if (!dc
) return ERROR
;
391 left
= XLPTODP( dc
, left
);
392 right
= XLPTODP( dc
, right
);
393 top
= YLPTODP( dc
, top
);
394 bottom
= YLPTODP( dc
, bottom
);
396 TRACE("%04x %dx%d,%dx%d\n", hdc
, left
, top
, right
, bottom
);
398 ret
= CLIPPING_IntersectVisRect( dc
, left
, top
, right
, bottom
, FALSE
);
399 GDI_ReleaseObj( hdc
);
404 /***********************************************************************
405 * PtVisible (GDI.103)
407 BOOL16 WINAPI
PtVisible16( HDC16 hdc
, INT16 x
, INT16 y
)
409 return PtVisible( hdc
, x
, y
);
413 /***********************************************************************
414 * PtVisible (GDI32.@)
416 BOOL WINAPI
PtVisible( HDC hdc
, INT x
, INT y
)
419 DC
*dc
= DC_GetDCUpdate( hdc
);
421 TRACE("%04x %d,%d\n", hdc
, x
, y
);
422 if (!dc
) return FALSE
;
425 ret
= PtInRegion( dc
->hGCClipRgn
, XLPTODP(dc
,x
) + dc
->DCOrgX
,
426 YLPTODP(dc
,y
) + dc
->DCOrgY
);
428 GDI_ReleaseObj( hdc
);
433 /***********************************************************************
434 * RectVisible (GDI.465)
435 * RectVisibleOld (GDI.104)
437 BOOL16 WINAPI
RectVisible16( HDC16 hdc
, const RECT16
* rect16
)
440 CONV_RECT16TO32( rect16
, &rect
);
441 return RectVisible( hdc
, &rect
);
445 /***********************************************************************
446 * RectVisible (GDI32.@)
448 BOOL WINAPI
RectVisible( HDC hdc
, const RECT
* rect
)
452 DC
*dc
= DC_GetDCUpdate( hdc
);
453 if (!dc
) return FALSE
;
454 TRACE("%04x %d,%dx%d,%d\n",
455 hdc
, rect
->left
, rect
->top
, rect
->right
, rect
->bottom
);
458 /* copy rectangle to avoid overwriting by LPtoDP */
460 LPtoDP( hdc
, (LPPOINT
)&tmpRect
, 2 );
461 tmpRect
.left
+= dc
->DCOrgX
;
462 tmpRect
.right
+= dc
->DCOrgX
;
463 tmpRect
.top
+= dc
->DCOrgY
;
464 tmpRect
.bottom
+= dc
->DCOrgY
;
465 ret
= RectInRegion( dc
->hGCClipRgn
, &tmpRect
);
467 GDI_ReleaseObj( hdc
);
472 /***********************************************************************
473 * GetClipBox (GDI.77)
475 INT16 WINAPI
GetClipBox16( HDC16 hdc
, LPRECT16 rect
)
478 DC
*dc
= DC_GetDCUpdate( hdc
);
479 if (!dc
) return ERROR
;
480 ret
= GetRgnBox16( dc
->hGCClipRgn
, rect
);
481 rect
->left
-= dc
->DCOrgX
;
482 rect
->right
-= dc
->DCOrgX
;
483 rect
->top
-= dc
->DCOrgY
;
484 rect
->bottom
-= dc
->DCOrgY
;
485 DPtoLP16( hdc
, (LPPOINT16
)rect
, 2 );
486 TRACE("%d,%d-%d,%d\n", rect
->left
,rect
->top
,rect
->right
,rect
->bottom
);
487 GDI_ReleaseObj( hdc
);
492 /***********************************************************************
493 * GetClipBox (GDI32.@)
495 INT WINAPI
GetClipBox( HDC hdc
, LPRECT rect
)
498 DC
*dc
= DC_GetDCUpdate( hdc
);
499 if (!dc
) return ERROR
;
500 ret
= GetRgnBox( dc
->hGCClipRgn
, rect
);
501 rect
->left
-= dc
->DCOrgX
;
502 rect
->right
-= dc
->DCOrgX
;
503 rect
->top
-= dc
->DCOrgY
;
504 rect
->bottom
-= dc
->DCOrgY
;
505 DPtoLP( hdc
, (LPPOINT
)rect
, 2 );
506 GDI_ReleaseObj( hdc
);
511 /***********************************************************************
512 * GetClipRgn (GDI32.@)
514 INT WINAPI
GetClipRgn( HDC hdc
, HRGN hRgn
)
518 if (hRgn
&& (dc
= DC_GetDCPtr( hdc
)))
522 /* this assumes that dc->hClipRgn is in coordinates
523 relative to the device (not DC origin) */
525 if( CombineRgn(hRgn
, dc
->hClipRgn
, 0, RGN_COPY
) != ERROR
)
527 OffsetRgn( hRgn
, -dc
->DCOrgX
, -dc
->DCOrgY
);
532 GDI_ReleaseObj( hdc
);
537 /***********************************************************************
538 * SaveVisRgn (GDI.129)
540 HRGN16 WINAPI
SaveVisRgn16( HDC16 hdc
)
543 RGNOBJ
*obj
, *copyObj
;
544 DC
*dc
= DC_GetDCUpdate( hdc
);
547 TRACE("%04x\n", hdc
);
549 if (!(obj
= (RGNOBJ
*) GDI_GetObjPtr( dc
->hVisRgn
, REGION_MAGIC
)))
551 GDI_ReleaseObj( hdc
);
554 if (!(copy
= CreateRectRgn( 0, 0, 0, 0 )))
556 GDI_ReleaseObj( dc
->hVisRgn
);
557 GDI_ReleaseObj( hdc
);
560 CombineRgn( copy
, dc
->hVisRgn
, 0, RGN_COPY
);
561 if (!(copyObj
= (RGNOBJ
*) GDI_GetObjPtr( copy
, REGION_MAGIC
)))
563 DeleteObject( copy
);
564 GDI_ReleaseObj( dc
->hVisRgn
);
565 GDI_ReleaseObj( hdc
);
568 copyObj
->header
.hNext
= obj
->header
.hNext
;
569 obj
->header
.hNext
= copy
;
570 GDI_ReleaseObj( copy
);
571 GDI_ReleaseObj( dc
->hVisRgn
);
572 GDI_ReleaseObj( hdc
);
577 /***********************************************************************
578 * RestoreVisRgn (GDI.130)
580 INT16 WINAPI
RestoreVisRgn16( HDC16 hdc
)
583 RGNOBJ
*obj
, *savedObj
;
584 DC
*dc
= DC_GetDCPtr( hdc
);
587 if (!dc
) return ERROR
;
588 if (!dc
->hVisRgn
) goto done
;
589 TRACE("%04x\n", hdc
);
590 if (!(obj
= (RGNOBJ
*) GDI_GetObjPtr( dc
->hVisRgn
, REGION_MAGIC
))) goto done
;
592 saved
= obj
->header
.hNext
;
593 GDI_ReleaseObj( dc
->hVisRgn
);
594 if (!saved
|| !(savedObj
= (RGNOBJ
*) GDI_GetObjPtr( saved
, REGION_MAGIC
))) goto done
;
596 DeleteObject( dc
->hVisRgn
);
598 dc
->flags
&= ~DC_DIRTY
;
599 CLIPPING_UpdateGCRegion( dc
);
600 ret
= savedObj
->rgn
->type
; /* FIXME */
601 GDI_ReleaseObj( saved
);
603 GDI_ReleaseObj( hdc
);