2 * DC clipping functions
4 * Copyright 1993 Alexandre Julliard
10 #include "debugtools.h"
11 #include "wine/winuser16.h"
13 DECLARE_DEBUG_CHANNEL(clipping
)
14 DECLARE_DEBUG_CHANNEL(region
)
16 #define UPDATE_DIRTY_DC(dc) \
18 if ((dc)->hookThunk && !((dc)->w.flags & (DC_SAVED | DC_MEMORY))) \
19 (dc)->hookThunk( (dc)->hSelf, DCHC_INVALIDVISRGN, (dc)->dwHookData, 0 ); \
24 /***********************************************************************
25 * CLIPPING_UpdateGCRegion
27 * Update the GC clip region when the ClipRgn or VisRgn have changed.
29 void CLIPPING_UpdateGCRegion( DC
* dc
)
31 if (!dc
->w
.hGCClipRgn
) dc
->w
.hGCClipRgn
= CreateRectRgn( 0, 0, 0, 0 );
35 ERR_(region
)("hVisRgn is zero. Please report this.\n" );
39 if (dc
->w
.flags
& DC_DIRTY
)
42 dc
->w
.flags
&= ~DC_DIRTY
;
46 CombineRgn( dc
->w
.hGCClipRgn
, dc
->w
.hVisRgn
, 0, RGN_COPY
);
48 CombineRgn(dc
->w
.hGCClipRgn
, dc
->w
.hClipRgn
, dc
->w
.hVisRgn
, RGN_AND
);
49 if (dc
->funcs
->pSetDeviceClipping
) dc
->funcs
->pSetDeviceClipping( dc
);
53 /***********************************************************************
54 * SelectClipRgn16 (GDI.44)
56 INT16 WINAPI
SelectClipRgn16( HDC16 hdc
, HRGN16 hrgn
)
58 return (INT16
)SelectClipRgn( hdc
, hrgn
);
62 /***********************************************************************
63 * SelectClipRgn32 (GDI32.297)
65 INT WINAPI
SelectClipRgn( HDC hdc
, HRGN hrgn
)
67 return ExtSelectClipRgn( hdc
, hrgn
, RGN_COPY
);
70 /******************************************************************************
71 * ExtSelectClipRgn16 [GDI.508]
73 INT16 WINAPI
ExtSelectClipRgn16( HDC16 hdc
, HRGN16 hrgn
, INT16 fnMode
)
75 return (INT16
) ExtSelectClipRgn((HDC
) hdc
, (HRGN
) hrgn
, fnMode
);
78 /******************************************************************************
79 * ExtSelectClipRgn [GDI32.97]
81 INT WINAPI
ExtSelectClipRgn( HDC hdc
, HRGN hrgn
, INT fnMode
)
84 DC
* dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
85 if (!dc
) return ERROR
;
87 TRACE_(clipping
)("%04x %04x %d\n", hdc
, hrgn
, fnMode
);
91 if (fnMode
== RGN_COPY
)
93 if (dc
->w
.hClipRgn
) DeleteObject16( dc
->w
.hClipRgn
);
95 retval
= SIMPLEREGION
; /* Clip region == whole DC */
99 FIXME_(clipping
)("Unimplemented: hrgn NULL in mode: %d\n", fnMode
);
108 GetRgnBox( dc
->w
.hVisRgn
, &rect
);
109 dc
->w
.hClipRgn
= CreateRectRgnIndirect( &rect
);
112 OffsetRgn( dc
->w
.hClipRgn
, -dc
->w
.DCOrgX
, -dc
->w
.DCOrgY
);
113 if(fnMode
== RGN_COPY
)
114 retval
= CombineRgn( dc
->w
.hClipRgn
, hrgn
, 0, fnMode
);
116 retval
= CombineRgn( dc
->w
.hClipRgn
, dc
->w
.hClipRgn
, hrgn
, fnMode
);
117 OffsetRgn( dc
->w
.hClipRgn
, dc
->w
.DCOrgX
, dc
->w
.DCOrgY
);
121 CLIPPING_UpdateGCRegion( dc
);
122 GDI_HEAP_UNLOCK( hdc
);
126 /***********************************************************************
127 * SelectVisRgn (GDI.105)
129 INT16 WINAPI
SelectVisRgn16( HDC16 hdc
, HRGN16 hrgn
)
132 DC
* dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
133 if (!dc
|| !hrgn
) return ERROR
;
135 TRACE_(clipping
)("%04x %04x\n", hdc
, hrgn
);
137 dc
->w
.flags
&= ~DC_DIRTY
;
139 retval
= CombineRgn16( dc
->w
.hVisRgn
, hrgn
, 0, RGN_COPY
);
140 CLIPPING_UpdateGCRegion( dc
);
141 GDI_HEAP_UNLOCK( hdc
);
146 /***********************************************************************
147 * OffsetClipRgn16 (GDI.32)
149 INT16 WINAPI
OffsetClipRgn16( HDC16 hdc
, INT16 x
, INT16 y
)
151 return (INT16
)OffsetClipRgn( hdc
, x
, y
);
155 /***********************************************************************
156 * OffsetClipRgn32 (GDI32.255)
158 INT WINAPI
OffsetClipRgn( HDC hdc
, INT x
, INT y
)
160 INT ret
= SIMPLEREGION
;
161 DC
*dc
= DC_GetDCPtr( hdc
);
162 if (!dc
) return ERROR
;
164 TRACE_(clipping
)("%04x %d,%d\n", hdc
, x
, y
);
166 if(dc
->funcs
->pOffsetClipRgn
)
167 ret
= dc
->funcs
->pOffsetClipRgn( dc
, x
, y
);
168 else if (dc
->w
.hClipRgn
) {
169 ret
= OffsetRgn( dc
->w
.hClipRgn
, XLSTODS(dc
,x
), YLSTODS(dc
,y
));
170 CLIPPING_UpdateGCRegion( dc
);
172 GDI_HEAP_UNLOCK( hdc
);
177 /***********************************************************************
178 * OffsetVisRgn (GDI.102)
180 INT16 WINAPI
OffsetVisRgn16( HDC16 hdc
, INT16 x
, INT16 y
)
183 DC
* dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
184 if (!dc
) return ERROR
;
185 TRACE_(clipping
)("%04x %d,%d\n", hdc
, x
, y
);
186 retval
= OffsetRgn( dc
->w
.hVisRgn
, x
, y
);
187 CLIPPING_UpdateGCRegion( dc
);
188 GDI_HEAP_UNLOCK( hdc
);
193 /***********************************************************************
194 * CLIPPING_IntersectClipRect
196 * Helper function for {Intersect,Exclude}ClipRect, can be called from
197 * elsewhere (like ExtTextOut()) to skip redundant metafile update and
198 * coordinate conversion.
200 INT
CLIPPING_IntersectClipRect( DC
* dc
, INT left
, INT top
,
201 INT right
, INT bottom
, UINT flags
)
206 left
+= dc
->w
.DCOrgX
;
207 right
+= dc
->w
.DCOrgX
;
209 bottom
+= dc
->w
.DCOrgY
;
211 if (!(newRgn
= CreateRectRgn( left
, top
, right
, bottom
))) return ERROR
;
214 if( flags
& CLIP_INTERSECT
)
216 dc
->w
.hClipRgn
= newRgn
;
217 CLIPPING_UpdateGCRegion( dc
);
220 else if( flags
& CLIP_EXCLUDE
)
222 dc
->w
.hClipRgn
= CreateRectRgn( 0, 0, 0, 0 );
223 CombineRgn( dc
->w
.hClipRgn
, dc
->w
.hVisRgn
, 0, RGN_COPY
);
225 else WARN_(clipping
)("No hClipRgn and flags are %x\n",flags
);
228 ret
= CombineRgn( newRgn
, dc
->w
.hClipRgn
, newRgn
,
229 (flags
& CLIP_EXCLUDE
) ? RGN_DIFF
: RGN_AND
);
232 if (!(flags
& CLIP_KEEPRGN
)) DeleteObject( dc
->w
.hClipRgn
);
233 dc
->w
.hClipRgn
= newRgn
;
234 CLIPPING_UpdateGCRegion( dc
);
236 else DeleteObject( newRgn
);
241 /***********************************************************************
242 * ExcludeClipRect16 (GDI.21)
244 INT16 WINAPI
ExcludeClipRect16( HDC16 hdc
, INT16 left
, INT16 top
,
245 INT16 right
, INT16 bottom
)
247 return (INT16
)ExcludeClipRect( hdc
, left
, top
, right
, bottom
);
251 /***********************************************************************
252 * ExcludeClipRect32 (GDI32.92)
254 INT WINAPI
ExcludeClipRect( HDC hdc
, INT left
, INT top
,
255 INT right
, INT bottom
)
258 DC
*dc
= DC_GetDCPtr( hdc
);
259 if (!dc
) return ERROR
;
261 TRACE_(clipping
)("%04x %dx%d,%dx%d\n",
262 hdc
, left
, top
, right
, bottom
);
264 if(dc
->funcs
->pExcludeClipRect
)
265 ret
= dc
->funcs
->pExcludeClipRect( dc
, left
, top
, right
, bottom
);
267 left
= XLPTODP( dc
, left
);
268 right
= XLPTODP( dc
, right
);
269 top
= YLPTODP( dc
, top
);
270 bottom
= YLPTODP( dc
, bottom
);
272 ret
= CLIPPING_IntersectClipRect( dc
, left
, top
, right
, bottom
, CLIP_EXCLUDE
);
274 GDI_HEAP_UNLOCK( hdc
);
279 /***********************************************************************
280 * IntersectClipRect16 (GDI.22)
282 INT16 WINAPI
IntersectClipRect16( HDC16 hdc
, INT16 left
, INT16 top
,
283 INT16 right
, INT16 bottom
)
285 return (INT16
)IntersectClipRect( hdc
, left
, top
, right
, bottom
);
289 /***********************************************************************
290 * IntersectClipRect32 (GDI32.245)
292 INT WINAPI
IntersectClipRect( HDC hdc
, INT left
, INT top
,
293 INT right
, INT bottom
)
296 DC
*dc
= DC_GetDCPtr( hdc
);
297 if (!dc
) return ERROR
;
299 TRACE_(clipping
)("%04x %dx%d,%dx%d\n",
300 hdc
, left
, top
, right
, bottom
);
302 if(dc
->funcs
->pIntersectClipRect
)
303 ret
= dc
->funcs
->pIntersectClipRect( dc
, left
, top
, right
, bottom
);
305 left
= XLPTODP( dc
, left
);
306 right
= XLPTODP( dc
, right
);
307 top
= YLPTODP( dc
, top
);
308 bottom
= YLPTODP( dc
, bottom
);
310 ret
= CLIPPING_IntersectClipRect( dc
, left
, top
, right
, bottom
, CLIP_INTERSECT
);
312 GDI_HEAP_UNLOCK( hdc
);
317 /***********************************************************************
318 * CLIPPING_IntersectVisRect
320 * Helper function for {Intersect,Exclude}VisRect, can be called from
321 * elsewhere (like ExtTextOut()) to skip redundant metafile update and
322 * coordinate conversion.
324 INT
CLIPPING_IntersectVisRect( DC
* dc
, INT left
, INT top
,
325 INT right
, INT bottom
,
328 HRGN tempRgn
, newRgn
;
331 left
+= dc
->w
.DCOrgX
;
332 right
+= dc
->w
.DCOrgX
;
334 bottom
+= dc
->w
.DCOrgY
;
336 if (!(newRgn
= CreateRectRgn( 0, 0, 0, 0 ))) return ERROR
;
337 if (!(tempRgn
= CreateRectRgn( left
, top
, right
, bottom
)))
339 DeleteObject( newRgn
);
342 ret
= CombineRgn( newRgn
, dc
->w
.hVisRgn
, tempRgn
,
343 exclude
? RGN_DIFF
: RGN_AND
);
344 DeleteObject( tempRgn
);
348 RGNOBJ
*newObj
= (RGNOBJ
*)GDI_GetObjPtr( newRgn
, REGION_MAGIC
);
349 RGNOBJ
*prevObj
= (RGNOBJ
*)GDI_GetObjPtr( dc
->w
.hVisRgn
, REGION_MAGIC
);
350 if (newObj
&& prevObj
) newObj
->header
.hNext
= prevObj
->header
.hNext
;
351 DeleteObject( dc
->w
.hVisRgn
);
352 dc
->w
.hVisRgn
= newRgn
;
353 CLIPPING_UpdateGCRegion( dc
);
354 GDI_HEAP_UNLOCK( newRgn
);
356 else DeleteObject( newRgn
);
361 /***********************************************************************
362 * ExcludeVisRect (GDI.73)
364 INT16 WINAPI
ExcludeVisRect16( HDC16 hdc
, INT16 left
, INT16 top
,
365 INT16 right
, INT16 bottom
)
367 DC
* dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
368 if (!dc
) return ERROR
;
370 left
= XLPTODP( dc
, left
);
371 right
= XLPTODP( dc
, right
);
372 top
= YLPTODP( dc
, top
);
373 bottom
= YLPTODP( dc
, bottom
);
375 TRACE_(clipping
)("%04x %dx%d,%dx%d\n",
376 hdc
, left
, top
, right
, bottom
);
378 return CLIPPING_IntersectVisRect( dc
, left
, top
, right
, bottom
, TRUE
);
382 /***********************************************************************
383 * IntersectVisRect (GDI.98)
385 INT16 WINAPI
IntersectVisRect16( HDC16 hdc
, INT16 left
, INT16 top
,
386 INT16 right
, INT16 bottom
)
388 DC
* dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
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_(clipping
)("%04x %dx%d,%dx%d\n",
397 hdc
, left
, top
, right
, bottom
);
399 return CLIPPING_IntersectVisRect( dc
, left
, top
, right
, bottom
, FALSE
);
403 /***********************************************************************
404 * PtVisible16 (GDI.103)
406 BOOL16 WINAPI
PtVisible16( HDC16 hdc
, INT16 x
, INT16 y
)
408 return PtVisible( hdc
, x
, y
);
412 /***********************************************************************
413 * PtVisible32 (GDI32.279)
415 BOOL WINAPI
PtVisible( HDC hdc
, INT x
, INT y
)
417 DC
* dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
418 if (!dc
) return ERROR
;
420 TRACE_(clipping
)("%04x %d,%d\n", hdc
, x
, y
);
421 if (!dc
->w
.hGCClipRgn
) return FALSE
;
423 if( dc
->w
.flags
& DC_DIRTY
) UPDATE_DIRTY_DC(dc
);
424 dc
->w
.flags
&= ~DC_DIRTY
;
426 return PtInRegion( dc
->w
.hGCClipRgn
, XLPTODP(dc
,x
) + dc
->w
.DCOrgX
,
427 YLPTODP(dc
,y
) + dc
->w
.DCOrgY
);
431 /***********************************************************************
432 * RectVisible16 (GDI.104)
434 BOOL16 WINAPI
RectVisible16( HDC16 hdc
, const RECT16
* rect
)
437 DC
* dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
438 if (!dc
) return FALSE
;
439 TRACE_(clipping
)("%04x %d,%dx%d,%d\n",
440 hdc
, rect
->left
, rect
->top
, rect
->right
, rect
->bottom
);
441 if (!dc
->w
.hGCClipRgn
) return FALSE
;
442 /* copy rectangle to avoid overwriting by LPtoDP */
444 LPtoDP16( hdc
, (LPPOINT16
)&tmpRect
, 2 );
445 OffsetRect16( &tmpRect
, dc
->w
.DCOrgX
, dc
->w
.DCOrgY
);
446 return RectInRegion16( dc
->w
.hGCClipRgn
, &tmpRect
);
450 /***********************************************************************
451 * RectVisible32 (GDI32.282)
453 BOOL WINAPI
RectVisible( HDC hdc
, const RECT
* rect
)
456 CONV_RECT32TO16( rect
, &rect16
);
457 return RectVisible16( (HDC16
)hdc
, &rect16
);
461 /***********************************************************************
462 * GetClipBox16 (GDI.77)
464 INT16 WINAPI
GetClipBox16( HDC16 hdc
, LPRECT16 rect
)
467 DC
* dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
468 if (!dc
) return ERROR
;
469 ret
= GetRgnBox16( dc
->w
.hGCClipRgn
, rect
);
470 OffsetRect16( rect
, -dc
->w
.DCOrgX
, -dc
->w
.DCOrgY
);
471 DPtoLP16( hdc
, (LPPOINT16
)rect
, 2 );
472 TRACE_(clipping
)("%d,%d-%d,%d\n",
473 rect
->left
,rect
->top
,rect
->right
,rect
->bottom
);
478 /***********************************************************************
479 * GetClipBox32 (GDI32.162)
481 INT WINAPI
GetClipBox( HDC hdc
, LPRECT rect
)
484 DC
* dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
485 if (!dc
) return ERROR
;
486 ret
= GetRgnBox( dc
->w
.hGCClipRgn
, rect
);
487 OffsetRect( rect
, -dc
->w
.DCOrgX
, -dc
->w
.DCOrgY
);
488 DPtoLP( hdc
, (LPPOINT
)rect
, 2 );
493 /***********************************************************************
494 * GetClipRgn32 (GDI32.163)
496 INT WINAPI
GetClipRgn( HDC hdc
, HRGN hRgn
)
498 DC
* dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
503 /* this assumes that dc->w.hClipRgn is in coordinates
504 relative to the device (not DC origin) */
506 if( CombineRgn(hRgn
, dc
->w
.hClipRgn
, 0, RGN_COPY
) != ERROR
)
508 OffsetRgn( hRgn
, -dc
->w
.DCOrgX
, -dc
->w
.DCOrgY
);
517 /***********************************************************************
518 * SaveVisRgn (GDI.129)
520 HRGN16 WINAPI
SaveVisRgn16( HDC16 hdc
)
523 RGNOBJ
*obj
, *copyObj
;
524 DC
* dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
526 TRACE_(clipping
)("%04x\n", hdc
);
529 ERR_(region
)("hVisRgn is zero. Please report this.\n" );
532 if( dc
->w
.flags
& DC_DIRTY
) UPDATE_DIRTY_DC(dc
);
533 dc
->w
.flags
&= ~DC_DIRTY
;
535 if (!(obj
= (RGNOBJ
*) GDI_GetObjPtr( dc
->w
.hVisRgn
, REGION_MAGIC
)))
537 GDI_HEAP_UNLOCK( hdc
);
540 if (!(copy
= CreateRectRgn( 0, 0, 0, 0 )))
542 GDI_HEAP_UNLOCK( dc
->w
.hVisRgn
);
543 GDI_HEAP_UNLOCK( hdc
);
546 CombineRgn( copy
, dc
->w
.hVisRgn
, 0, RGN_COPY
);
547 if (!(copyObj
= (RGNOBJ
*) GDI_GetObjPtr( copy
, REGION_MAGIC
)))
549 GDI_HEAP_UNLOCK( dc
->w
.hVisRgn
);
550 GDI_HEAP_UNLOCK( hdc
);
553 copyObj
->header
.hNext
= obj
->header
.hNext
;
554 obj
->header
.hNext
= copy
;
555 GDI_HEAP_UNLOCK( dc
->w
.hVisRgn
);
556 GDI_HEAP_UNLOCK( hdc
);
557 GDI_HEAP_UNLOCK( copy
);
562 /***********************************************************************
563 * RestoreVisRgn (GDI.130)
565 INT16 WINAPI
RestoreVisRgn16( HDC16 hdc
)
568 RGNOBJ
*obj
, *savedObj
;
569 DC
* dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
572 if (!dc
) return ERROR
;
575 GDI_HEAP_UNLOCK( hdc
);
578 TRACE_(clipping
)("%04x\n", hdc
);
579 if (!(obj
= (RGNOBJ
*) GDI_GetObjPtr( dc
->w
.hVisRgn
, REGION_MAGIC
)))
581 GDI_HEAP_UNLOCK( hdc
);
584 if (!(saved
= obj
->header
.hNext
))
586 GDI_HEAP_UNLOCK( dc
->w
.hVisRgn
);
587 GDI_HEAP_UNLOCK( hdc
);
590 if (!(savedObj
= (RGNOBJ
*) GDI_GetObjPtr( saved
, REGION_MAGIC
)))
592 GDI_HEAP_UNLOCK( dc
->w
.hVisRgn
);
593 GDI_HEAP_UNLOCK( hdc
);
596 DeleteObject( dc
->w
.hVisRgn
);
597 dc
->w
.hVisRgn
= saved
;
598 CLIPPING_UpdateGCRegion( dc
);
599 GDI_HEAP_UNLOCK( hdc
);
600 ret
= savedObj
->rgn
->type
; /* FIXME */
601 GDI_HEAP_UNLOCK( saved
);