4 * Copyright 1993 Alexandre Julliard
5 * 1996,1997 Alex Korobka
8 * Note: Visible regions of CS_OWNDC/CS_CLASSDC window DCs
9 * have to be updated dynamically.
13 * DCX_DCEEMPTY - dce is uninitialized
14 * DCX_DCEBUSY - dce is in use
15 * DCX_DCEDIRTY - ReleaseDC() should wipe instead of caching
16 * DCX_KEEPCLIPRGN - ReleaseDC() should not delete the clipping region
17 * DCX_WINDOWPAINT - BeginPaint() is in effect
26 #include "debugtools.h"
29 #include "wine/winbase16.h"
30 #include "wine/winuser16.h"
32 DEFAULT_DEBUG_CHANNEL(dc
);
35 static HDC defaultDCstate
;
37 static void DCE_DeleteClipRgn( DCE
* );
38 static INT
DCE_ReleaseDC( DCE
* );
41 /***********************************************************************
44 static void DCE_DumpCache(void)
54 DPRINTF("\t[0x%08x] hWnd 0x%04x, dcx %08x, %s %s\n",
55 (unsigned)dce
, dce
->hwndCurrent
, (unsigned)dce
->DCXflags
,
56 (dce
->DCXflags
& DCX_CACHE
) ? "Cache" : "Owned",
57 (dce
->DCXflags
& DCX_DCEBUSY
) ? "InUse" : "" );
64 /***********************************************************************
69 DCE
*DCE_AllocDCE( HWND hWnd
, DCE_TYPE type
)
74 if (!(dce
= HeapAlloc( GetProcessHeap(), 0, sizeof(DCE
) ))) return NULL
;
75 if (!(dce
->hDC
= CreateDCA( "DISPLAY", NULL
, NULL
, NULL
)))
77 HeapFree( GetProcessHeap(), 0, dce
);
80 if (!defaultDCstate
) defaultDCstate
= GetDCState16( dce
->hDC
);
82 /* store DCE handle in DC hook data field */
84 hookProc
= GetProcAddress16( GetModuleHandle16("USER"), (LPCSTR
)362 );
85 SetDCHook( dce
->hDC
, hookProc
, (DWORD
)dce
);
87 dce
->hwndCurrent
= WIN_GetFullHandle( hWnd
);
90 if( type
!= DCE_CACHE_DC
) /* owned or class DC */
92 dce
->DCXflags
= DCX_DCEBUSY
;
95 LONG style
= GetWindowLongW( hWnd
, GWL_STYLE
);
96 if (style
& WS_CLIPCHILDREN
) dce
->DCXflags
|= DCX_CLIPCHILDREN
;
97 if (style
& WS_CLIPSIBLINGS
) dce
->DCXflags
|= DCX_CLIPSIBLINGS
;
99 SetHookFlags16(dce
->hDC
,DCHF_INVALIDATEVISRGN
);
101 else dce
->DCXflags
= DCX_CACHE
| DCX_DCEEMPTY
;
104 dce
->next
= firstDCE
;
111 /***********************************************************************
114 DCE
* DCE_FreeDCE( DCE
*dce
)
118 if (!dce
) return NULL
;
124 while (*ppDCE
&& (*ppDCE
!= dce
)) ppDCE
= &(*ppDCE
)->next
;
125 if (*ppDCE
== dce
) *ppDCE
= dce
->next
;
129 SetDCHook(dce
->hDC
, NULL
, 0L);
131 DeleteDC( dce
->hDC
);
132 if( dce
->hClipRgn
&& !(dce
->DCXflags
& DCX_KEEPCLIPRGN
) )
133 DeleteObject(dce
->hClipRgn
);
134 HeapFree( GetProcessHeap(), 0, dce
);
139 /***********************************************************************
142 * Remove owned DCE and reset unreleased cache DCEs.
144 void DCE_FreeWindowDCE( HWND hwnd
)
147 WND
*pWnd
= WIN_GetPtr( hwnd
);
152 if( pDCE
->hwndCurrent
== hwnd
)
154 if( pDCE
== pWnd
->dce
) /* owned or Class DCE*/
156 if (pWnd
->clsStyle
& CS_OWNDC
) /* owned DCE*/
158 pDCE
= DCE_FreeDCE( pDCE
);
162 else if( pDCE
->DCXflags
& (DCX_INTERSECTRGN
| DCX_EXCLUDERGN
) ) /* Class DCE*/
164 DCE_DeleteClipRgn( pDCE
);
165 pDCE
->hwndCurrent
= 0;
170 if( pDCE
->DCXflags
& DCX_DCEBUSY
) /* shared cache DCE */
172 /* FIXME: AFAICS we are doing the right thing here so
173 * this should be a WARN. But this is best left as an ERR
174 * because the 'application error' is likely to come from
175 * another part of Wine (i.e. it's our fault after all).
176 * We should change this to WARN when Wine is more stable
179 ERR("[%08x] GetDC() without ReleaseDC()!\n",hwnd
);
180 DCE_ReleaseDC( pDCE
);
183 pDCE
->DCXflags
&= DCX_CACHE
;
184 pDCE
->DCXflags
|= DCX_DCEEMPTY
;
185 pDCE
->hwndCurrent
= 0;
190 WIN_ReleasePtr( pWnd
);
194 /***********************************************************************
197 static void DCE_DeleteClipRgn( DCE
* dce
)
199 dce
->DCXflags
&= ~(DCX_EXCLUDERGN
| DCX_INTERSECTRGN
| DCX_WINDOWPAINT
);
201 if( dce
->DCXflags
& DCX_KEEPCLIPRGN
)
202 dce
->DCXflags
&= ~DCX_KEEPCLIPRGN
;
204 if( dce
->hClipRgn
> 1 )
205 DeleteObject( dce
->hClipRgn
);
209 /* make it dirty so that the vis rgn gets recomputed next time */
210 dce
->DCXflags
|= DCX_DCEDIRTY
;
211 SetHookFlags16( dce
->hDC
, DCHF_INVALIDATEVISRGN
);
215 /***********************************************************************
218 static INT
DCE_ReleaseDC( DCE
* dce
)
220 if ((dce
->DCXflags
& (DCX_DCEEMPTY
| DCX_DCEBUSY
)) != DCX_DCEBUSY
) return 0;
222 /* restore previous visible region */
224 if ((dce
->DCXflags
& (DCX_INTERSECTRGN
| DCX_EXCLUDERGN
)) &&
225 (dce
->DCXflags
& (DCX_CACHE
| DCX_WINDOWPAINT
)) )
226 DCE_DeleteClipRgn( dce
);
228 if (dce
->DCXflags
& DCX_CACHE
)
230 SetDCState16( dce
->hDC
, defaultDCstate
);
231 dce
->DCXflags
&= ~DCX_DCEBUSY
;
232 if (dce
->DCXflags
& DCX_DCEDIRTY
)
234 /* don't keep around invalidated entries
235 * because SetDCState() disables hVisRgn updates
236 * by removing dirty bit. */
238 dce
->hwndCurrent
= 0;
239 dce
->DCXflags
&= DCX_CACHE
;
240 dce
->DCXflags
|= DCX_DCEEMPTY
;
247 /***********************************************************************
250 * It is called from SetWindowPos() and EVENT_MapNotify - we have to
251 * mark as dirty all busy DCEs for windows that have pWnd->parent as
252 * an ancestor and whose client rect intersects with specified update
253 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
254 * DCX_CLIPCHILDREN flag is set. */
255 BOOL
DCE_InvalidateDCE(HWND hwnd
, const RECT
* pRectUpdate
)
257 HWND hwndScope
= GetAncestor( hwnd
, GA_PARENT
);
264 TRACE("scope hwnd = %04x, (%i,%i - %i,%i)\n",
265 hwndScope
, pRectUpdate
->left
,pRectUpdate
->top
,
266 pRectUpdate
->right
,pRectUpdate
->bottom
);
270 /* walk all DCEs and fixup non-empty entries */
272 for (dce
= firstDCE
; (dce
); dce
= dce
->next
)
274 if (dce
->DCXflags
& DCX_DCEEMPTY
) continue;
275 if ((dce
->hwndCurrent
== hwndScope
) && !(dce
->DCXflags
& DCX_CLIPCHILDREN
))
276 continue; /* child window positions don't bother us */
278 /* check if DCE window is within the z-order scope */
280 if (hwndScope
== dce
->hwndCurrent
|| IsChild( hwndScope
, dce
->hwndCurrent
))
282 if (hwnd
!= dce
->hwndCurrent
)
284 /* check if the window rectangle intersects this DCE window */
286 GetWindowRect( dce
->hwndCurrent
, &rect
);
287 MapWindowPoints( 0, hwndScope
, (POINT
*)&rect
, 2 );
288 if (!IntersectRect( &rect
, &rect
, pRectUpdate
)) continue;
291 if( !(dce
->DCXflags
& DCX_DCEBUSY
) )
293 /* Don't bother with visible regions of unused DCEs */
295 TRACE("\tpurged %p dce [%04x]\n", dce
, dce
->hwndCurrent
);
296 dce
->hwndCurrent
= 0;
297 dce
->DCXflags
&= DCX_CACHE
;
298 dce
->DCXflags
|= DCX_DCEEMPTY
;
302 /* Set dirty bits in the hDC and DCE structs */
304 TRACE("\tfixed up %p dce [%04x]\n", dce
, dce
->hwndCurrent
);
305 dce
->DCXflags
|= DCX_DCEDIRTY
;
306 SetHookFlags16(dce
->hDC
, DCHF_INVALIDATEVISRGN
);
316 /***********************************************************************
319 * Translate given region from the wnd client to the DC coordinates
320 * and add it to the clipping region.
322 INT
DCE_ExcludeRgn( HDC hDC
, HWND hwnd
, HRGN hRgn
)
327 while (dce
&& (dce
->hDC
!= hDC
)) dce
= dce
->next
;
328 if (!dce
) return ERROR
;
330 MapWindowPoints( hwnd
, dce
->hwndCurrent
, &pt
, 1);
331 if( dce
->DCXflags
& DCX_WINDOW
)
333 WND
*wnd
= WIN_FindWndPtr(dce
->hwndCurrent
);
334 pt
.x
+= wnd
->rectClient
.left
- wnd
->rectWindow
.left
;
335 pt
.y
+= wnd
->rectClient
.top
- wnd
->rectWindow
.top
;
336 WIN_ReleaseWndPtr(wnd
);
338 OffsetRgn(hRgn
, pt
.x
, pt
.y
);
340 return ExtSelectClipRgn( hDC
, hRgn
, RGN_DIFF
);
344 /***********************************************************************
347 * Unimplemented flags: DCX_LOCKWINDOWUPDATE
349 * FIXME: Full support for hrgnClip == 1 (alias for entire window).
351 HDC WINAPI
GetDCEx( HWND hwnd
, HRGN hrgnClip
, DWORD flags
)
357 BOOL bUpdateVisRgn
= TRUE
;
358 BOOL bUpdateClipOrigin
= FALSE
;
361 TRACE("hwnd %04x, hrgnClip %04x, flags %08x\n",
362 hwnd
, hrgnClip
, (unsigned)flags
);
364 if (!hwnd
) hwnd
= GetDesktopWindow();
365 if (!(full
= WIN_IsCurrentProcess( hwnd
)))
367 FIXME( "not supported yet on other process window %x\n", hwnd
);
371 if (!(wndPtr
= WIN_GetPtr( hwnd
))) return 0;
375 if (flags
& (DCX_WINDOW
| DCX_PARENTCLIP
)) flags
|= DCX_CACHE
;
377 if (flags
& DCX_USESTYLE
)
379 flags
&= ~( DCX_CLIPCHILDREN
| DCX_CLIPSIBLINGS
| DCX_PARENTCLIP
);
381 if( wndPtr
->dwStyle
& WS_CLIPSIBLINGS
)
382 flags
|= DCX_CLIPSIBLINGS
;
384 if ( !(flags
& DCX_WINDOW
) )
386 if (wndPtr
->clsStyle
& CS_PARENTDC
) flags
|= DCX_PARENTCLIP
;
388 if (wndPtr
->dwStyle
& WS_CLIPCHILDREN
&&
389 !(wndPtr
->dwStyle
& WS_MINIMIZE
) ) flags
|= DCX_CLIPCHILDREN
;
390 if (!wndPtr
->dce
) flags
|= DCX_CACHE
;
394 if (flags
& DCX_WINDOW
) flags
&= ~DCX_CLIPCHILDREN
;
396 parent
= GetAncestor( hwnd
, GA_PARENT
);
397 if (!parent
|| (parent
== GetDesktopWindow()))
398 flags
= (flags
& ~DCX_PARENTCLIP
) | DCX_CLIPSIBLINGS
;
400 /* it seems parent clip is ignored when clipping siblings or children */
401 if (flags
& (DCX_CLIPSIBLINGS
| DCX_CLIPCHILDREN
)) flags
&= ~DCX_PARENTCLIP
;
403 if( flags
& DCX_PARENTCLIP
)
405 LONG parent_style
= GetWindowLongW( parent
, GWL_STYLE
);
406 if( (wndPtr
->dwStyle
& WS_VISIBLE
) && (parent_style
& WS_VISIBLE
) )
408 flags
&= ~DCX_CLIPCHILDREN
;
409 if (parent_style
& WS_CLIPSIBLINGS
) flags
|= DCX_CLIPSIBLINGS
;
413 /* find a suitable DCE */
415 dcxFlags
= flags
& (DCX_PARENTCLIP
| DCX_CLIPSIBLINGS
| DCX_CLIPCHILDREN
|
416 DCX_CACHE
| DCX_WINDOW
);
418 if (flags
& DCX_CACHE
)
423 dceEmpty
= dceUnused
= NULL
;
425 /* Strategy: First, we attempt to find a non-empty but unused DCE with
426 * compatible flags. Next, we look for an empty entry. If the cache is
427 * full we have to purge one of the unused entries.
430 for (dce
= firstDCE
; (dce
); dce
= dce
->next
)
432 if ((dce
->DCXflags
& (DCX_CACHE
| DCX_DCEBUSY
)) == DCX_CACHE
)
436 if (dce
->DCXflags
& DCX_DCEEMPTY
)
439 if ((dce
->hwndCurrent
== hwnd
) &&
440 ((dce
->DCXflags
& (DCX_CLIPSIBLINGS
| DCX_CLIPCHILDREN
|
441 DCX_CACHE
| DCX_WINDOW
| DCX_PARENTCLIP
)) == dcxFlags
))
443 TRACE("\tfound valid %08x dce [%04x], flags %08x\n",
444 (unsigned)dce
, hwnd
, (unsigned)dcxFlags
);
445 bUpdateVisRgn
= FALSE
;
446 bUpdateClipOrigin
= TRUE
;
452 if (!dce
) dce
= (dceEmpty
) ? dceEmpty
: dceUnused
;
454 /* if there's no dce empty or unused, allocate a new one */
457 dce
= DCE_AllocDCE( 0, DCE_CACHE_DC
);
463 if (dce
&& dce
->hwndCurrent
== hwnd
)
465 TRACE("\tskipping hVisRgn update\n");
466 bUpdateVisRgn
= FALSE
; /* updated automatically, via DCHook() */
475 if (!(flags
& (DCX_INTERSECTRGN
| DCX_EXCLUDERGN
))) hrgnClip
= 0;
477 if (((flags
^ dce
->DCXflags
) & (DCX_INTERSECTRGN
| DCX_EXCLUDERGN
)) &&
478 (dce
->hClipRgn
!= hrgnClip
))
480 /* if the extra clip region has changed, get rid of the old one */
481 DCE_DeleteClipRgn( dce
);
484 dce
->hwndCurrent
= hwnd
;
485 dce
->hClipRgn
= hrgnClip
;
486 dce
->DCXflags
= flags
& (DCX_PARENTCLIP
| DCX_CLIPSIBLINGS
| DCX_CLIPCHILDREN
|
487 DCX_CACHE
| DCX_WINDOW
| DCX_WINDOWPAINT
|
488 DCX_KEEPCLIPRGN
| DCX_INTERSECTRGN
| DCX_EXCLUDERGN
);
489 dce
->DCXflags
|= DCX_DCEBUSY
;
490 dce
->DCXflags
&= ~DCX_DCEDIRTY
;
493 if (bUpdateVisRgn
) SetHookFlags16( hdc
, DCHF_INVALIDATEVISRGN
); /* force update */
495 if (!USER_Driver
.pGetDC( hwnd
, hdc
, hrgnClip
, flags
)) hdc
= 0;
497 TRACE("(%04x,%04x,0x%lx): returning %04x\n", hwnd
, hrgnClip
, flags
, hdc
);
499 WIN_ReleasePtr(wndPtr
);
504 /***********************************************************************
511 HWND hwnd
/* [in] handle of window */
514 return GetDCEx( 0, 0, DCX_CACHE
| DCX_WINDOW
);
515 return GetDCEx( hwnd
, 0, DCX_USESTYLE
);
519 /***********************************************************************
520 * GetWindowDC (USER32.@)
522 HDC WINAPI
GetWindowDC( HWND hwnd
)
524 return GetDCEx( hwnd
, 0, DCX_USESTYLE
| DCX_WINDOW
);
528 /***********************************************************************
529 * ReleaseDC (USER32.@)
535 INT WINAPI
ReleaseDC(
536 HWND hwnd
/* [in] Handle of window - ignored */,
537 HDC hdc
/* [in] Handle of device context */
545 TRACE("%04x %04x\n", hwnd
, hdc
);
547 while (dce
&& (dce
->hDC
!= hdc
)) dce
= dce
->next
;
550 if ( dce
->DCXflags
& DCX_DCEBUSY
)
551 nRet
= DCE_ReleaseDC( dce
);
558 /***********************************************************************
561 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
563 BOOL16 WINAPI
DCHook16( HDC16 hDC
, WORD code
, DWORD data
, LPARAM lParam
)
566 DCE
*dce
= (DCE
*)data
;
568 TRACE("hDC = %04x, %i\n", hDC
, code
);
571 assert(dce
->hDC
== hDC
);
573 /* Grab the windows lock before doing anything else */
578 case DCHC_INVALIDVISRGN
:
579 /* GDI code calls this when it detects that the
580 * DC is dirty (usually after SetHookFlags()). This
581 * means that we have to recompute the visible region.
583 if( dce
->DCXflags
& DCX_DCEBUSY
)
585 /* Dirty bit has been cleared by caller, set it again so that
586 * pGetDC recomputes the visible region. */
587 SetHookFlags16( dce
->hDC
, DCHF_INVALIDATEVISRGN
);
588 USER_Driver
.pGetDC( dce
->hwndCurrent
, dce
->hDC
, dce
->hClipRgn
, dce
->DCXflags
);
590 else /* non-fatal but shouldn't happen */
591 WARN("DC is not in use!\n");
596 * Windows will not let you delete a DC that is busy
597 * (between GetDC and ReleaseDC)
600 if ( dce
->DCXflags
& DCX_DCEBUSY
)
602 WARN("Application trying to delete a busy DC\n");
605 else DCE_FreeDCE( dce
);
609 FIXME("unknown code\n");
612 USER_Unlock(); /* Release the wnd lock */
617 /**********************************************************************
618 * WindowFromDC (USER32.@)
620 HWND WINAPI
WindowFromDC( HDC hDC
)
628 while (dce
&& (dce
->hDC
!= hDC
)) dce
= dce
->next
;
630 hwnd
= dce
? dce
->hwndCurrent
: 0;
637 /***********************************************************************
638 * LockWindowUpdate (USER32.@)
640 BOOL WINAPI
LockWindowUpdate( HWND hwnd
)
642 FIXME("(%x), stub!\n",hwnd
);