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
)
75 if (!(dce
= HeapAlloc( GetProcessHeap(), 0, sizeof(DCE
) ))) return NULL
;
76 if (!(dce
->hDC
= CreateDCA( "DISPLAY", NULL
, NULL
, NULL
)))
78 HeapFree( GetProcessHeap(), 0, dce
);
81 if (!defaultDCstate
) defaultDCstate
= GetDCState16( dce
->hDC
);
83 wnd
= WIN_FindWndPtr(hWnd
);
85 /* store DCE handle in DC hook data field */
87 hookProc
= GetProcAddress16( GetModuleHandle16("USER"), (LPCSTR
)362 );
88 SetDCHook( dce
->hDC
, hookProc
, (DWORD
)dce
);
90 dce
->hwndCurrent
= hWnd
;
95 if( type
!= DCE_CACHE_DC
) /* owned or class DC */
97 dce
->DCXflags
= DCX_DCEBUSY
;
100 if( wnd
->dwStyle
& WS_CLIPCHILDREN
) dce
->DCXflags
|= DCX_CLIPCHILDREN
;
101 if( wnd
->dwStyle
& WS_CLIPSIBLINGS
) dce
->DCXflags
|= DCX_CLIPSIBLINGS
;
103 SetHookFlags16(dce
->hDC
,DCHF_INVALIDATEVISRGN
);
105 else dce
->DCXflags
= DCX_CACHE
| DCX_DCEEMPTY
;
107 WIN_ReleaseWndPtr(wnd
);
113 /***********************************************************************
116 DCE
* DCE_FreeDCE( DCE
*dce
)
120 if (!dce
) return NULL
;
126 while (*ppDCE
&& (*ppDCE
!= dce
)) ppDCE
= &(*ppDCE
)->next
;
127 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
);
141 /***********************************************************************
144 * Remove owned DCE and reset unreleased cache DCEs.
146 void DCE_FreeWindowDCE( HWND hwnd
)
155 if( pDCE
->hwndCurrent
== hwnd
)
157 WND
*pWnd
= WIN_FindWndPtr( hwnd
);
158 if( pDCE
== pWnd
->dce
) /* owned or Class DCE*/
160 if (pWnd
->clsStyle
& CS_OWNDC
) /* owned DCE*/
162 pDCE
= DCE_FreeDCE( pDCE
);
164 WIN_ReleaseWndPtr( pWnd
);
167 else if( pDCE
->DCXflags
& (DCX_INTERSECTRGN
| DCX_EXCLUDERGN
) ) /* Class DCE*/
169 DCE_DeleteClipRgn( pDCE
);
170 pDCE
->hwndCurrent
= 0;
175 if( pDCE
->DCXflags
& DCX_DCEBUSY
) /* shared cache DCE */
177 /* FIXME: AFAICS we are doing the right thing here so
178 * this should be a WARN. But this is best left as an ERR
179 * because the 'application error' is likely to come from
180 * another part of Wine (i.e. it's our fault after all).
181 * We should change this to WARN when Wine is more stable
184 ERR("[%04x] GetDC() without ReleaseDC()!\n",
186 DCE_ReleaseDC( pDCE
);
189 pDCE
->DCXflags
&= DCX_CACHE
;
190 pDCE
->DCXflags
|= DCX_DCEEMPTY
;
191 pDCE
->hwndCurrent
= 0;
193 WIN_ReleaseWndPtr( pWnd
);
202 /***********************************************************************
205 static void DCE_DeleteClipRgn( DCE
* dce
)
207 dce
->DCXflags
&= ~(DCX_EXCLUDERGN
| DCX_INTERSECTRGN
| DCX_WINDOWPAINT
);
209 if( dce
->DCXflags
& DCX_KEEPCLIPRGN
)
210 dce
->DCXflags
&= ~DCX_KEEPCLIPRGN
;
212 if( dce
->hClipRgn
> 1 )
213 DeleteObject( dce
->hClipRgn
);
217 /* make it dirty so that the vis rgn gets recomputed next time */
218 dce
->DCXflags
|= DCX_DCEDIRTY
;
219 SetHookFlags16( dce
->hDC
, DCHF_INVALIDATEVISRGN
);
223 /***********************************************************************
226 static INT
DCE_ReleaseDC( DCE
* dce
)
228 if ((dce
->DCXflags
& (DCX_DCEEMPTY
| DCX_DCEBUSY
)) != DCX_DCEBUSY
) return 0;
230 /* restore previous visible region */
232 if ((dce
->DCXflags
& (DCX_INTERSECTRGN
| DCX_EXCLUDERGN
)) &&
233 (dce
->DCXflags
& (DCX_CACHE
| DCX_WINDOWPAINT
)) )
234 DCE_DeleteClipRgn( dce
);
236 if (dce
->DCXflags
& DCX_CACHE
)
238 SetDCState16( dce
->hDC
, defaultDCstate
);
239 dce
->DCXflags
&= ~DCX_DCEBUSY
;
240 if (dce
->DCXflags
& DCX_DCEDIRTY
)
242 /* don't keep around invalidated entries
243 * because SetDCState() disables hVisRgn updates
244 * by removing dirty bit. */
246 dce
->hwndCurrent
= 0;
247 dce
->DCXflags
&= DCX_CACHE
;
248 dce
->DCXflags
|= DCX_DCEEMPTY
;
255 /***********************************************************************
258 * It is called from SetWindowPos() and EVENT_MapNotify - we have to
259 * mark as dirty all busy DCEs for windows that have pWnd->parent as
260 * an ancestor and whose client rect intersects with specified update
261 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
262 * DCX_CLIPCHILDREN flag is set. */
263 BOOL
DCE_InvalidateDCE(HWND hwnd
, const RECT
* pRectUpdate
)
265 HWND hwndScope
= GetAncestor( hwnd
, GA_PARENT
);
272 TRACE("scope hwnd = %04x, (%i,%i - %i,%i)\n",
273 hwndScope
, pRectUpdate
->left
,pRectUpdate
->top
,
274 pRectUpdate
->right
,pRectUpdate
->bottom
);
278 /* walk all DCEs and fixup non-empty entries */
280 for (dce
= firstDCE
; (dce
); dce
= dce
->next
)
284 INT xoffset
= 0, yoffset
= 0;
286 if (dce
->DCXflags
& DCX_DCEEMPTY
) continue;
287 if ((dce
->hwndCurrent
== hwndScope
) && !(dce
->DCXflags
& DCX_CLIPCHILDREN
))
288 continue; /* child window positions don't bother us */
289 if (!(wndCurrent
= WIN_FindWndPtr(dce
->hwndCurrent
))) continue;
291 /* check if DCE window is within the z-order scope */
293 for (tmp
= dce
->hwndCurrent
; tmp
; tmp
= GetAncestor( tmp
, GA_PARENT
))
295 if (tmp
== hwndScope
)
299 wndRect
= wndCurrent
->rectWindow
;
301 OffsetRect( &wndRect
, xoffset
- wndCurrent
->rectClient
.left
,
302 yoffset
- wndCurrent
->rectClient
.top
);
304 if (hwnd
== wndCurrent
->hwndSelf
||
305 IntersectRect( &wndRect
, &wndRect
, pRectUpdate
))
307 if( !(dce
->DCXflags
& DCX_DCEBUSY
) )
309 /* Don't bother with visible regions of unused DCEs */
311 TRACE("\tpurged %08x dce [%04x]\n",
312 (unsigned)dce
, wndCurrent
->hwndSelf
);
314 dce
->hwndCurrent
= 0;
315 dce
->DCXflags
&= DCX_CACHE
;
316 dce
->DCXflags
|= DCX_DCEEMPTY
;
320 /* Set dirty bits in the hDC and DCE structs */
322 TRACE("\tfixed up %08x dce [%04x]\n",
323 (unsigned)dce
, wndCurrent
->hwndSelf
);
325 dce
->DCXflags
|= DCX_DCEDIRTY
;
326 SetHookFlags16(dce
->hDC
, DCHF_INVALIDATEVISRGN
);
334 WND
* wnd
= WIN_FindWndPtr( tmp
);
335 xoffset
+= wnd
->rectClient
.left
;
336 yoffset
+= wnd
->rectClient
.top
;
337 WIN_ReleaseWndPtr( wnd
);
340 WIN_ReleaseWndPtr(wndCurrent
);
347 /***********************************************************************
350 * Translate given region from the wnd client to the DC coordinates
351 * and add it to the clipping region.
353 INT
DCE_ExcludeRgn( HDC hDC
, HWND hwnd
, HRGN hRgn
)
358 while (dce
&& (dce
->hDC
!= hDC
)) dce
= dce
->next
;
359 if (!dce
) return ERROR
;
361 MapWindowPoints( hwnd
, dce
->hwndCurrent
, &pt
, 1);
362 if( dce
->DCXflags
& DCX_WINDOW
)
364 WND
*wnd
= WIN_FindWndPtr(dce
->hwndCurrent
);
365 pt
.x
+= wnd
->rectClient
.left
- wnd
->rectWindow
.left
;
366 pt
.y
+= wnd
->rectClient
.top
- wnd
->rectWindow
.top
;
367 WIN_ReleaseWndPtr(wnd
);
369 OffsetRgn(hRgn
, pt
.x
, pt
.y
);
371 return ExtSelectClipRgn( hDC
, hRgn
, RGN_DIFF
);
375 /***********************************************************************
378 HDC16 WINAPI
GetDCEx16( HWND16 hwnd
, HRGN16 hrgnClip
, DWORD flags
)
380 return (HDC16
)GetDCEx( hwnd
, hrgnClip
, flags
);
384 /***********************************************************************
387 * Unimplemented flags: DCX_LOCKWINDOWUPDATE
389 * FIXME: Full support for hrgnClip == 1 (alias for entire window).
391 HDC WINAPI
GetDCEx( HWND hwnd
, HRGN hrgnClip
, DWORD flags
)
397 BOOL bUpdateVisRgn
= TRUE
;
398 BOOL bUpdateClipOrigin
= FALSE
;
401 TRACE("hwnd %04x, hrgnClip %04x, flags %08x\n",
402 hwnd
, hrgnClip
, (unsigned)flags
);
404 if (!hwnd
) hwnd
= GetDesktopWindow();
405 if (!(wndPtr
= WIN_FindWndPtr( hwnd
))) return 0;
409 if (flags
& (DCX_WINDOW
| DCX_PARENTCLIP
)) flags
|= DCX_CACHE
;
411 if (flags
& DCX_USESTYLE
)
413 flags
&= ~( DCX_CLIPCHILDREN
| DCX_CLIPSIBLINGS
| DCX_PARENTCLIP
);
415 if( wndPtr
->dwStyle
& WS_CLIPSIBLINGS
)
416 flags
|= DCX_CLIPSIBLINGS
;
418 if ( !(flags
& DCX_WINDOW
) )
420 if (wndPtr
->clsStyle
& CS_PARENTDC
) flags
|= DCX_PARENTCLIP
;
422 if (wndPtr
->dwStyle
& WS_CLIPCHILDREN
&&
423 !(wndPtr
->dwStyle
& WS_MINIMIZE
) ) flags
|= DCX_CLIPCHILDREN
;
424 if (!wndPtr
->dce
) flags
|= DCX_CACHE
;
428 if (flags
& DCX_WINDOW
) flags
&= ~DCX_CLIPCHILDREN
;
430 parent
= GetAncestor( hwnd
, GA_PARENT
);
431 if (!parent
|| (parent
== GetDesktopWindow()))
432 flags
= (flags
& ~DCX_PARENTCLIP
) | DCX_CLIPSIBLINGS
;
434 /* it seems parent clip is ignored when clipping siblings or children */
435 if (flags
& (DCX_CLIPSIBLINGS
| DCX_CLIPCHILDREN
)) flags
&= ~DCX_PARENTCLIP
;
437 if( flags
& DCX_PARENTCLIP
)
439 LONG parent_style
= GetWindowLongW( parent
, GWL_STYLE
);
440 if( (wndPtr
->dwStyle
& WS_VISIBLE
) && (parent_style
& WS_VISIBLE
) )
442 flags
&= ~DCX_CLIPCHILDREN
;
443 if (parent_style
& WS_CLIPSIBLINGS
) flags
|= DCX_CLIPSIBLINGS
;
447 /* find a suitable DCE */
449 dcxFlags
= flags
& (DCX_PARENTCLIP
| DCX_CLIPSIBLINGS
| DCX_CLIPCHILDREN
|
450 DCX_CACHE
| DCX_WINDOW
);
452 if (flags
& DCX_CACHE
)
457 dceEmpty
= dceUnused
= NULL
;
459 /* Strategy: First, we attempt to find a non-empty but unused DCE with
460 * compatible flags. Next, we look for an empty entry. If the cache is
461 * full we have to purge one of the unused entries.
464 for (dce
= firstDCE
; (dce
); dce
= dce
->next
)
466 if ((dce
->DCXflags
& (DCX_CACHE
| DCX_DCEBUSY
)) == DCX_CACHE
)
470 if (dce
->DCXflags
& DCX_DCEEMPTY
)
473 if ((dce
->hwndCurrent
== hwnd
) &&
474 ((dce
->DCXflags
& (DCX_CLIPSIBLINGS
| DCX_CLIPCHILDREN
|
475 DCX_CACHE
| DCX_WINDOW
| DCX_PARENTCLIP
)) == dcxFlags
))
477 TRACE("\tfound valid %08x dce [%04x], flags %08x\n",
478 (unsigned)dce
, hwnd
, (unsigned)dcxFlags
);
479 bUpdateVisRgn
= FALSE
;
480 bUpdateClipOrigin
= TRUE
;
486 if (!dce
) dce
= (dceEmpty
) ? dceEmpty
: dceUnused
;
488 /* if there's no dce empty or unused, allocate a new one */
491 dce
= DCE_AllocDCE( 0, DCE_CACHE_DC
);
497 if (dce
&& dce
->hwndCurrent
== hwnd
)
499 TRACE("\tskipping hVisRgn update\n");
500 bUpdateVisRgn
= FALSE
; /* updated automatically, via DCHook() */
509 if (!(flags
& (DCX_INTERSECTRGN
| DCX_EXCLUDERGN
))) hrgnClip
= 0;
511 if (((flags
^ dce
->DCXflags
) & (DCX_INTERSECTRGN
| DCX_EXCLUDERGN
)) &&
512 (dce
->hClipRgn
!= hrgnClip
))
514 /* if the extra clip region has changed, get rid of the old one */
515 DCE_DeleteClipRgn( dce
);
518 dce
->hwndCurrent
= hwnd
;
519 dce
->hClipRgn
= hrgnClip
;
520 dce
->DCXflags
= flags
& (DCX_PARENTCLIP
| DCX_CLIPSIBLINGS
| DCX_CLIPCHILDREN
|
521 DCX_CACHE
| DCX_WINDOW
| DCX_WINDOWPAINT
|
522 DCX_KEEPCLIPRGN
| DCX_INTERSECTRGN
| DCX_EXCLUDERGN
);
523 dce
->DCXflags
|= DCX_DCEBUSY
;
524 dce
->DCXflags
&= ~DCX_DCEDIRTY
;
527 if (bUpdateVisRgn
) SetHookFlags16( hdc
, DCHF_INVALIDATEVISRGN
); /* force update */
529 if (!USER_Driver
.pGetDC( hwnd
, hdc
, hrgnClip
, flags
)) hdc
= 0;
531 TRACE("(%04x,%04x,0x%lx): returning %04x\n", hwnd
, hrgnClip
, flags
, hdc
);
533 WIN_ReleaseWndPtr(wndPtr
);
538 /***********************************************************************
541 HDC16 WINAPI
GetDC16( HWND16 hwnd
)
543 return (HDC16
)GetDC( hwnd
);
547 /***********************************************************************
554 HWND hwnd
/* [in] handle of window */
557 return GetDCEx( 0, 0, DCX_CACHE
| DCX_WINDOW
);
558 return GetDCEx( hwnd
, 0, DCX_USESTYLE
);
562 /***********************************************************************
563 * GetWindowDC (USER.67)
565 HDC16 WINAPI
GetWindowDC16( HWND16 hwnd
)
567 return GetDCEx16( hwnd
, 0, DCX_USESTYLE
| DCX_WINDOW
);
571 /***********************************************************************
572 * GetWindowDC (USER32.@)
574 HDC WINAPI
GetWindowDC( HWND hwnd
)
576 return GetDCEx( hwnd
, 0, DCX_USESTYLE
| DCX_WINDOW
);
580 /***********************************************************************
581 * ReleaseDC (USER.68)
583 INT16 WINAPI
ReleaseDC16( HWND16 hwnd
, HDC16 hdc
)
585 return (INT16
)ReleaseDC( hwnd
, hdc
);
589 /***********************************************************************
590 * ReleaseDC (USER32.@)
596 INT WINAPI
ReleaseDC(
597 HWND hwnd
/* [in] Handle of window - ignored */,
598 HDC hdc
/* [in] Handle of device context */
606 TRACE("%04x %04x\n", hwnd
, hdc
);
608 while (dce
&& (dce
->hDC
!= hdc
)) dce
= dce
->next
;
611 if ( dce
->DCXflags
& DCX_DCEBUSY
)
612 nRet
= DCE_ReleaseDC( dce
);
619 /***********************************************************************
622 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
624 BOOL16 WINAPI
DCHook16( HDC16 hDC
, WORD code
, DWORD data
, LPARAM lParam
)
627 DCE
*dce
= (DCE
*)data
;
629 TRACE("hDC = %04x, %i\n", hDC
, code
);
632 assert(dce
->hDC
== hDC
);
634 /* Grab the windows lock before doing anything else */
639 case DCHC_INVALIDVISRGN
:
640 /* GDI code calls this when it detects that the
641 * DC is dirty (usually after SetHookFlags()). This
642 * means that we have to recompute the visible region.
644 if( dce
->DCXflags
& DCX_DCEBUSY
)
646 /* Dirty bit has been cleared by caller, set it again so that
647 * pGetDC recomputes the visible region. */
648 SetHookFlags16( dce
->hDC
, DCHF_INVALIDATEVISRGN
);
649 USER_Driver
.pGetDC( dce
->hwndCurrent
, dce
->hDC
, dce
->hClipRgn
, dce
->DCXflags
);
651 else /* non-fatal but shouldn't happen */
652 WARN("DC is not in use!\n");
657 * Windows will not let you delete a DC that is busy
658 * (between GetDC and ReleaseDC)
661 if ( dce
->DCXflags
& DCX_DCEBUSY
)
663 WARN("Application trying to delete a busy DC\n");
669 FIXME("unknown code\n");
672 USER_Unlock(); /* Release the wnd lock */
677 /**********************************************************************
678 * WindowFromDC (USER.117)
680 HWND16 WINAPI
WindowFromDC16( HDC16 hDC
)
682 return (HWND16
)WindowFromDC( hDC
);
686 /**********************************************************************
687 * WindowFromDC (USER32.@)
689 HWND WINAPI
WindowFromDC( HDC hDC
)
697 while (dce
&& (dce
->hDC
!= hDC
)) dce
= dce
->next
;
699 hwnd
= dce
? dce
->hwndCurrent
: 0;
706 /***********************************************************************
707 * LockWindowUpdate (USER.294)
709 BOOL16 WINAPI
LockWindowUpdate16( HWND16 hwnd
)
711 return LockWindowUpdate( hwnd
);
714 /***********************************************************************
715 * LockWindowUpdate (USER32.@)
717 BOOL WINAPI
LockWindowUpdate( HWND hwnd
)
719 FIXME("(%x), stub!\n",hwnd
);