4 * Copyright 1993 Alexandre Julliard
8 * Note: Visible regions of CS_OWNDC/CS_CLASSDC window DCs
9 * have to be updated dynamically.
13 * DCX_DCEBUSY - dce structure is in use
14 * DCX_KEEPCLIPRGN - do not delete clipping region in ReleaseDC
15 * DCX_WINDOWPAINT - BeginPaint specific flag
24 #include "sysmetrics.h"
26 /* #define DEBUG_DC */
29 #define NB_DCE 5 /* Number of DCEs created at startup */
31 static HANDLE firstDCE
= 0;
32 static HDC defaultDCstate
= 0;
34 BOOL
DCHook(HDC
, WORD
, DWORD
, DWORD
);
36 /***********************************************************************
41 HANDLE
DCE_AllocDCE( HWND hWnd
, DCE_TYPE type
)
44 HANDLE handle
= USER_HEAP_ALLOC( sizeof(DCE
) );
45 if (!handle
) return 0;
46 dce
= (DCE
*) USER_HEAP_LIN_ADDR( handle
);
47 if (!(dce
->hDC
= CreateDC( "DISPLAY", NULL
, NULL
, NULL
)))
49 USER_HEAP_FREE( handle
);
53 /* store DCE handle in DC hook data field */
55 SetDCHook(dce
->hDC
, GDI_GetDefDCHook(), MAKELONG(handle
,DC_MAGIC
));
57 dce
->hwndCurrent
= hWnd
;
58 dce
->hNext
= firstDCE
;
62 if( type
!= DCE_CACHE_DC
)
64 dce
->DCXflags
= DCX_DCEBUSY
;
67 WND
* wnd
= WIN_FindWndPtr(hWnd
);
69 if( wnd
->dwStyle
& WS_CLIPCHILDREN
) dce
->DCXflags
|= DCX_CLIPCHILDREN
;
70 if( wnd
->dwStyle
& WS_CLIPSIBLINGS
) dce
->DCXflags
|= DCX_CLIPSIBLINGS
;
72 SetHookFlags(dce
->hDC
,DCHF_INVALIDATEVISRGN
);
74 else dce
->DCXflags
= DCX_CACHE
;
80 /***********************************************************************
83 void DCE_FreeDCE( HANDLE hdce
)
86 HANDLE
*handle
= &firstDCE
;
88 if (!(dce
= (DCE
*) USER_HEAP_LIN_ADDR( hdce
))) return;
89 while (*handle
&& (*handle
!= hdce
))
91 DCE
* prev
= (DCE
*) USER_HEAP_LIN_ADDR( *handle
);
92 handle
= &prev
->hNext
;
94 if (*handle
== hdce
) *handle
= dce
->hNext
;
96 SetDCHook(dce
->hDC
,(SEGPTR
)NULL
,0L);
99 if( dce
->hClipRgn
&& !(dce
->DCXflags
& DCX_KEEPCLIPRGN
) )
100 DeleteObject(dce
->hClipRgn
);
101 USER_HEAP_FREE( hdce
);
104 /***********************************************************************
107 HANDLE
DCE_FindDCE(HDC hDC
)
109 HANDLE hdce
= firstDCE
;
114 dce
= (DCE
*) USER_HEAP_LIN_ADDR(hdce
);
115 if( dce
->hDC
== hDC
) break;
121 /***********************************************************************
124 * It is called from SetWindowPos - we have to invalidate all busy
125 * DCE's for windows whose client rect intersects with update rectangle
127 BOOL
DCE_InvalidateDCE(WND
* wndScope
, RECT16
* pRectUpdate
)
132 if( !wndScope
) return 0;
134 dprintf_dc(stddeb
,"InvalidateDCE: scope hwnd = %04x, (%i,%i - %i,%i)\n",
135 wndScope
->hwndSelf
, pRectUpdate
->left
,pRectUpdate
->top
,
136 pRectUpdate
->right
,pRectUpdate
->bottom
);
139 for( hdce
= firstDCE
; (hdce
); hdce
=dce
->hNext
)
141 dce
= (DCE
*)USER_HEAP_LIN_ADDR(hdce
);
143 if( dce
->DCXflags
& DCX_DCEBUSY
)
145 WND
* wndCurrent
, * wnd
;
147 wnd
= wndCurrent
= WIN_FindWndPtr(dce
->hwndCurrent
);
149 /* desktop is not critical (DC is not owned anyway) */
151 if( wnd
== WIN_GetDesktop() ) continue;
153 /* check if DCE window is within z-order scope */
155 for( ; wnd
; wnd
= wnd
->parent
)
156 if( wnd
== wndScope
)
158 RECT16 wndRect
= wndCurrent
->rectWindow
;
160 dprintf_dc(stddeb
,"\tgot hwnd %04x\n", wndCurrent
->hwndSelf
);
162 MapWindowPoints16(wndCurrent
->parent
->hwndSelf
, wndScope
->hwndSelf
,
163 (LPPOINT16
)&wndRect
, 2);
164 if( IntersectRect16(&wndRect
,&wndRect
,pRectUpdate
) )
165 SetHookFlags(dce
->hDC
, DCHF_INVALIDATEVISRGN
);
173 /***********************************************************************
182 for (i
= 0; i
< NB_DCE
; i
++)
184 if (!(handle
= DCE_AllocDCE( 0, DCE_CACHE_DC
))) return;
185 dce
= (DCE
*) USER_HEAP_LIN_ADDR( handle
);
186 if (!defaultDCstate
) defaultDCstate
= GetDCState( dce
->hDC
);
191 /***********************************************************************
194 * Calc the visible rectangle of a window, i.e. the client or
195 * window area clipped by the client area of all ancestors.
196 * Return FALSE if the visible region is empty.
198 static BOOL
DCE_GetVisRect( WND
*wndPtr
, BOOL clientArea
, RECT16
*lprect
)
200 int xoffset
, yoffset
;
202 *lprect
= clientArea
? wndPtr
->rectClient
: wndPtr
->rectWindow
;
203 xoffset
= lprect
->left
;
204 yoffset
= lprect
->top
;
206 if (!(wndPtr
->dwStyle
& WS_VISIBLE
) || (wndPtr
->flags
& WIN_NO_REDRAW
))
208 SetRectEmpty16( lprect
); /* Clip everything */
212 while (wndPtr
->parent
)
214 wndPtr
= wndPtr
->parent
;
215 if (!(wndPtr
->dwStyle
& WS_VISIBLE
) ||
216 (wndPtr
->flags
& WIN_NO_REDRAW
) ||
217 (wndPtr
->dwStyle
& WS_ICONIC
))
219 SetRectEmpty16( lprect
); /* Clip everything */
222 xoffset
+= wndPtr
->rectClient
.left
;
223 yoffset
+= wndPtr
->rectClient
.top
;
224 OffsetRect16( lprect
, wndPtr
->rectClient
.left
,
225 wndPtr
->rectClient
.top
);
227 /* Warning!! we assume that IntersectRect() handles the case */
228 /* where the destination is the same as one of the sources. */
229 if (!IntersectRect16( lprect
, lprect
, &wndPtr
->rectClient
))
230 return FALSE
; /* Visible rectangle is empty */
232 OffsetRect16( lprect
, -xoffset
, -yoffset
);
237 /***********************************************************************
240 * Go through the linked list of windows from hwndStart to hwndEnd,
241 * removing from the given region the rectangle of each window offset
242 * by a given amount. The new region is returned, and the original one
243 * is destroyed. Used to implement DCX_CLIPSIBLINGS and
244 * DCX_CLIPCHILDREN styles.
246 static HRGN
DCE_ClipWindows( WND
*pWndStart
, WND
*pWndEnd
,
247 HRGN hrgn
, int xoffset
, int yoffset
)
251 if (!pWndStart
) return hrgn
;
252 if (!(hrgnNew
= CreateRectRgn( 0, 0, 0, 0 )))
254 DeleteObject( hrgn
);
257 for (; pWndStart
!= pWndEnd
; pWndStart
= pWndStart
->next
)
259 if (!(pWndStart
->dwStyle
& WS_VISIBLE
)) continue;
260 SetRectRgn( hrgnNew
, pWndStart
->rectWindow
.left
+ xoffset
,
261 pWndStart
->rectWindow
.top
+ yoffset
,
262 pWndStart
->rectWindow
.right
+ xoffset
,
263 pWndStart
->rectWindow
.bottom
+ yoffset
);
264 if (!CombineRgn( hrgn
, hrgn
, hrgnNew
, RGN_DIFF
)) break;
266 DeleteObject( hrgnNew
);
267 if (pWndStart
!= pWndEnd
) /* something went wrong */
269 DeleteObject( hrgn
);
276 /***********************************************************************
279 * Return the visible region of a window, i.e. the client or window area
280 * clipped by the client area of all ancestors, and then optionally
281 * by siblings and children.
283 HRGN
DCE_GetVisRgn( HWND hwnd
, WORD flags
)
287 int xoffset
, yoffset
;
288 WND
*wndPtr
= WIN_FindWndPtr( hwnd
);
290 /* Get visible rectangle and create a region with it
291 * FIXME: do we really need to calculate vis rgns for X windows?
294 if (!wndPtr
|| !DCE_GetVisRect( wndPtr
, !(flags
& DCX_WINDOW
), &rect
))
296 return CreateRectRgn( 0, 0, 0, 0 ); /* Visible region is empty */
298 if (!(hrgn
= CreateRectRgnIndirect16( &rect
))) return 0;
300 /* Clip all children from the visible region */
302 if (flags
& DCX_CLIPCHILDREN
)
304 if (flags
& DCX_WINDOW
)
306 xoffset
= wndPtr
->rectClient
.left
- wndPtr
->rectWindow
.left
;
307 yoffset
= wndPtr
->rectClient
.top
- wndPtr
->rectWindow
.top
;
309 else xoffset
= yoffset
= 0;
310 hrgn
= DCE_ClipWindows( wndPtr
->child
, NULL
, hrgn
, xoffset
, yoffset
);
314 /* Clip siblings placed above this window */
316 if (flags
& DCX_WINDOW
)
318 xoffset
= -wndPtr
->rectWindow
.left
;
319 yoffset
= -wndPtr
->rectWindow
.top
;
323 xoffset
= -wndPtr
->rectClient
.left
;
324 yoffset
= -wndPtr
->rectClient
.top
;
326 if (flags
& DCX_CLIPSIBLINGS
)
328 hrgn
= DCE_ClipWindows( wndPtr
->parent
? wndPtr
->parent
->child
: NULL
,
329 wndPtr
, hrgn
, xoffset
, yoffset
);
333 /* Clip siblings of all ancestors that have the WS_CLIPSIBLINGS style */
335 while (wndPtr
->dwStyle
& WS_CHILD
)
337 wndPtr
= wndPtr
->parent
;
338 xoffset
-= wndPtr
->rectClient
.left
;
339 yoffset
-= wndPtr
->rectClient
.top
;
340 hrgn
= DCE_ClipWindows( wndPtr
->parent
->child
, wndPtr
,
341 hrgn
, xoffset
, yoffset
);
348 /***********************************************************************
351 * Set the drawable, origin and dimensions for the DC associated to
354 static void DCE_SetDrawable( WND
*wndPtr
, DC
*dc
, WORD flags
)
356 if (!wndPtr
) /* Get a DC for the whole screen */
360 dc
->u
.x
.drawable
= rootWindow
;
361 XSetSubwindowMode( display
, dc
->u
.x
.gc
, IncludeInferiors
);
365 if (flags
& DCX_WINDOW
)
367 dc
->w
.DCOrgX
= wndPtr
->rectWindow
.left
;
368 dc
->w
.DCOrgY
= wndPtr
->rectWindow
.top
;
372 dc
->w
.DCOrgX
= wndPtr
->rectClient
.left
;
373 dc
->w
.DCOrgY
= wndPtr
->rectClient
.top
;
375 while (!wndPtr
->window
)
377 wndPtr
= wndPtr
->parent
;
378 dc
->w
.DCOrgX
+= wndPtr
->rectClient
.left
;
379 dc
->w
.DCOrgY
+= wndPtr
->rectClient
.top
;
381 dc
->w
.DCOrgX
-= wndPtr
->rectWindow
.left
;
382 dc
->w
.DCOrgY
-= wndPtr
->rectWindow
.top
;
383 dc
->u
.x
.drawable
= wndPtr
->window
;
387 /***********************************************************************
390 * Unimplemented flags: DCX_LOCKWINDOWUPDATE
392 HDC
GetDCEx( HWND hwnd
, HRGN hrgnClip
, DWORD flags
)
401 BOOL need_update
= TRUE
;
403 dprintf_dc(stddeb
,"GetDCEx: hwnd %04x, hrgnClip %04x, flags %08x\n", hwnd
, hrgnClip
, (unsigned)flags
);
405 if (!(wndPtr
= WIN_FindWndPtr( hwnd
))) return 0;
407 if (flags
& DCX_USESTYLE
)
409 flags
&= ~( DCX_CLIPCHILDREN
| DCX_CLIPSIBLINGS
| DCX_PARENTCLIP
);
411 if( wndPtr
->dwStyle
& WS_CLIPSIBLINGS
)
412 flags
|= DCX_CLIPSIBLINGS
;
414 if ( !(flags
& DCX_WINDOW
) )
416 if (!(wndPtr
->class->style
& (CS_OWNDC
| CS_CLASSDC
)))
419 if (wndPtr
->class->style
& CS_PARENTDC
) flags
|= DCX_PARENTCLIP
;
421 if (wndPtr
->dwStyle
& WS_CLIPCHILDREN
&&
422 !(wndPtr
->dwStyle
& WS_MINIMIZE
) ) flags
|= DCX_CLIPCHILDREN
;
424 else flags
|= DCX_CACHE
;
427 if( flags
& DCX_NOCLIPCHILDREN
)
430 flags
&= ~(DCX_PARENTCLIP
| DCX_CLIPCHILDREN
);
433 if (hwnd
==GetDesktopWindow() || !(wndPtr
->dwStyle
& WS_CHILD
)) flags
&= ~DCX_PARENTCLIP
;
435 if (flags
& DCX_WINDOW
) flags
= (flags
& ~DCX_CLIPCHILDREN
) | DCX_CACHE
;
437 if( flags
& DCX_PARENTCLIP
)
440 if( !(flags
& (DCX_CLIPSIBLINGS
| DCX_CLIPCHILDREN
)) )
441 if( (wndPtr
->dwStyle
& WS_VISIBLE
) && (wndPtr
->parent
->dwStyle
& WS_VISIBLE
) )
443 flags
&= ~DCX_CLIPCHILDREN
;
444 if( wndPtr
->parent
->dwStyle
& WS_CLIPSIBLINGS
)
445 flags
|= DCX_CLIPSIBLINGS
;
449 if (flags
& DCX_CACHE
)
451 for (hdce
= firstDCE
; (hdce
); hdce
= dce
->hNext
)
453 if (!(dce
= (DCE
*) USER_HEAP_LIN_ADDR( hdce
))) return 0;
454 if ((dce
->DCXflags
& DCX_CACHE
) && !(dce
->DCXflags
& DCX_DCEBUSY
)) break;
459 hdce
= (wndPtr
->class->style
& CS_OWNDC
)?wndPtr
->hdce
:wndPtr
->class->hdce
;
460 dce
= (DCE
*) USER_HEAP_LIN_ADDR( hdce
);
462 if( dce
->hwndCurrent
== hwnd
)
464 dprintf_dc(stddeb
,"\tskipping hVisRgn update\n");
468 if( hrgnClip
&& dce
->hClipRgn
&& !(dce
->DCXflags
& DCX_KEEPCLIPRGN
))
470 fprintf(stdnimp
,"GetDCEx: hClipRgn collision!\n");
471 DeleteObject(dce
->hClipRgn
);
476 dcx_flags
= flags
& ( DCX_CLIPSIBLINGS
| DCX_CLIPCHILDREN
| DCX_CACHE
| DCX_WINDOW
| DCX_WINDOWPAINT
);
479 dce
= (DCE
*) USER_HEAP_LIN_ADDR( hdce
);
480 dce
->hwndCurrent
= hwnd
;
482 dce
->DCXflags
= dcx_flags
| DCX_DCEBUSY
;
485 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
))) return 0;
487 DCE_SetDrawable( wndPtr
, dc
, flags
);
488 if( need_update
|| dc
->w
.flags
& DC_DIRTY
)
490 dprintf_dc(stddeb
,"updating hDC anyway\n");
492 if (flags
& DCX_PARENTCLIP
)
494 WND
*parentPtr
= wndPtr
->parent
;
495 dcx_flags
= flags
& ~(DCX_CLIPSIBLINGS
| DCX_CLIPCHILDREN
|
497 if (parentPtr
->dwStyle
& WS_CLIPSIBLINGS
)
498 dcx_flags
|= DCX_CLIPSIBLINGS
;
499 hrgnVisible
= DCE_GetVisRgn( parentPtr
->hwndSelf
, dcx_flags
);
500 if (flags
& DCX_WINDOW
)
501 OffsetRgn( hrgnVisible
, -wndPtr
->rectWindow
.left
,
502 -wndPtr
->rectWindow
.top
);
503 else OffsetRgn( hrgnVisible
, -wndPtr
->rectClient
.left
,
504 -wndPtr
->rectClient
.top
);
506 /* optimize away GetVisRgn for desktop if it isn't there */
508 else if( hwnd
==GetDesktopWindow() && !Options
.desktopGeometry
)
509 hrgnVisible
= CreateRectRgn( 0, 0, SYSMETRICS_CXSCREEN
,
510 SYSMETRICS_CYSCREEN
);
511 else hrgnVisible
= DCE_GetVisRgn( hwnd
, flags
);
513 dc
->w
.flags
&= ~DC_DIRTY
;
515 SelectVisRgn( hdc
, hrgnVisible
);
517 else hrgnVisible
= CreateRectRgn(0,0,0,0);
519 if ((flags
& DCX_INTERSECTRGN
) || (flags
& DCX_EXCLUDERGN
))
521 dce
->DCXflags
|= flags
& (DCX_KEEPCLIPRGN
| DCX_INTERSECTRGN
| DCX_EXCLUDERGN
);
522 dce
->hClipRgn
= hrgnClip
;
524 dprintf_dc(stddeb
, "\tsaved VisRgn, clipRgn = %04x\n", hrgnClip
);
527 CombineRgn( hrgnVisible
, InquireVisRgn( hdc
), hrgnClip
,
528 (flags
& DCX_INTERSECTRGN
) ? RGN_AND
: RGN_DIFF
);
529 SelectVisRgn( hdc
, hrgnVisible
);
531 DeleteObject( hrgnVisible
);
533 dprintf_dc(stddeb
, "GetDCEx(%04x,%04x,0x%lx): returning %04x\n",
534 hwnd
, hrgnClip
, flags
, hdc
);
538 /***********************************************************************
541 HDC
GetDC( HWND hwnd
)
543 if( !hwnd
) return GetDCEx( GetDesktopWindow(), 0, DCX_CACHE
| DCX_WINDOW
);
545 return GetDCEx( hwnd
, 0, DCX_USESTYLE
);
549 /***********************************************************************
550 * GetWindowDC (USER.67)
552 HDC
GetWindowDC( HWND hwnd
)
557 if (!(wndPtr
= WIN_FindWndPtr( hwnd
))) return 0;
559 else hwnd
= GetDesktopWindow();
561 return GetDCEx( hwnd
, 0, DCX_USESTYLE
| DCX_WINDOW
);
565 /***********************************************************************
566 * ReleaseDC (USER.68)
568 int ReleaseDC( HWND hwnd
, HDC hdc
)
573 dprintf_dc(stddeb
, "ReleaseDC: %04x %04x\n", hwnd
, hdc
);
575 for (hdce
= firstDCE
; (hdce
); hdce
= dce
->hNext
)
577 if (!(dce
= (DCE
*) USER_HEAP_LIN_ADDR( hdce
))) return 0;
578 if (dce
->hDC
== hdc
) break;
581 if (!(dce
->DCXflags
& DCX_DCEBUSY
) ) return 0;
583 /* restore previous visible region */
585 if ( dce
->DCXflags
& (DCX_INTERSECTRGN
| DCX_EXCLUDERGN
) &&
586 (dce
->DCXflags
& DCX_CACHE
|| dce
->DCXflags
& DCX_WINDOWPAINT
) )
588 dprintf_dc(stddeb
,"\tcleaning up visrgn...\n");
589 dce
->DCXflags
&= ~(DCX_EXCLUDERGN
| DCX_INTERSECTRGN
| DCX_WINDOWPAINT
);
591 if( dce
->DCXflags
& DCX_KEEPCLIPRGN
)
592 dce
->DCXflags
&= ~DCX_KEEPCLIPRGN
;
594 if( dce
->hClipRgn
> 1 )
595 DeleteObject( dce
->hClipRgn
);
598 RestoreVisRgn(dce
->hDC
);
601 if (dce
->DCXflags
& DCX_CACHE
)
603 SetDCState( dce
->hDC
, defaultDCstate
);
604 dce
->DCXflags
= DCX_CACHE
;
609 /***********************************************************************
612 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
614 BOOL
DCHook(HDC hDC
, WORD code
, DWORD data
, DWORD lParam
)
619 dprintf_dc(stddeb
,"DCHook: hDC = %04x, %i\n", hDC
, code
);
621 if( HIWORD(data
) == DC_MAGIC
)
622 hdce
= (HANDLE
)LOWORD(data
);
624 hdce
= DCE_FindDCE(hDC
);
626 if( !hdce
) return 0;
630 case DCHC_INVALIDVISRGN
:
632 DCE
* dce
= (DCE
*) USER_HEAP_LIN_ADDR(hdce
);
634 if( dce
->DCXflags
& DCX_DCEBUSY
)
636 SetHookFlags(hDC
, DCHF_VALIDATEVISRGN
);
637 hVisRgn
= DCE_GetVisRgn(dce
->hwndCurrent
, dce
->DCXflags
);
639 dprintf_dc(stddeb
,"\tapplying saved clipRgn\n");
641 /* clip this region with saved clipping region */
643 if ( (dce
->DCXflags
& DCX_INTERSECTRGN
&& dce
->hClipRgn
!= 1) ||
644 ( dce
->DCXflags
& DCX_EXCLUDERGN
&& dce
->hClipRgn
) )
647 if( (!dce
->hClipRgn
&& dce
->DCXflags
& DCX_INTERSECTRGN
) ||
648 (dce
->hClipRgn
== 1 && dce
->DCXflags
& DCX_EXCLUDERGN
) )
649 SetRectRgn(hVisRgn
,0,0,0,0);
651 CombineRgn(hVisRgn
, hVisRgn
, dce
->hClipRgn
,
652 (dce
->DCXflags
& DCX_EXCLUDERGN
)? RGN_DIFF
:RGN_AND
);
654 SelectVisRgn(hDC
, hVisRgn
);
655 DeleteObject(hVisRgn
);
658 dprintf_dc(stddeb
,"DCHook: DC is not in use!\n");
662 case DCHC_DELETEDC
: /* FIXME: ?? */
666 fprintf(stdnimp
,"DCHook: unknown code\n");