4 * Copyright 1993 Alexandre Julliard
5 * 1996,1997 Alex Korobka
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Note: Visible regions of CS_OWNDC/CS_CLASSDC window DCs
23 * have to be updated dynamically.
27 * DCX_DCEEMPTY - dce is uninitialized
28 * DCX_DCEBUSY - dce is in use
29 * DCX_DCEDIRTY - ReleaseDC() should wipe instead of caching
30 * DCX_WINDOWPAINT - BeginPaint() is in effect
36 #include "user_private.h"
37 #include "wine/debug.h"
41 #include "wine/winbase16.h"
42 #include "wine/winuser16.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(dc
);
47 static HDC16 defaultDCstate
;
49 static void DCE_DeleteClipRgn( DCE
* );
50 static INT
DCE_ReleaseDC( DCE
* );
53 /***********************************************************************
56 static void DCE_DumpCache(void)
66 DPRINTF("\t[0x%08x] hWnd %p, dcx %08x, %s %s\n",
67 (unsigned)dce
, dce
->hwndCurrent
, (unsigned)dce
->DCXflags
,
68 (dce
->DCXflags
& DCX_CACHE
) ? "Cache" : "Owned",
69 (dce
->DCXflags
& DCX_DCEBUSY
) ? "InUse" : "" );
76 /***********************************************************************
81 DCE
*DCE_AllocDCE( HWND hWnd
, DCE_TYPE type
)
83 static const WCHAR szDisplayW
[] = { 'D','I','S','P','L','A','Y','\0' };
86 TRACE("(%p,%d)\n", hWnd
, type
);
88 if (!(dce
= HeapAlloc( GetProcessHeap(), 0, sizeof(DCE
) ))) return NULL
;
89 if (!(dce
->hDC
= CreateDCW( szDisplayW
, NULL
, NULL
, NULL
)))
91 HeapFree( GetProcessHeap(), 0, dce
);
94 if (!defaultDCstate
) defaultDCstate
= GetDCState16( HDC_16(dce
->hDC
) );
96 /* store DCE handle in DC hook data field */
98 SetDCHook( dce
->hDC
, DCHook16
, (DWORD
)dce
);
100 dce
->hwndCurrent
= WIN_GetFullHandle( hWnd
);
103 if( type
!= DCE_CACHE_DC
) /* owned or class DC */
105 dce
->DCXflags
= DCX_DCEBUSY
;
108 LONG style
= GetWindowLongW( hWnd
, GWL_STYLE
);
109 if (style
& WS_CLIPCHILDREN
) dce
->DCXflags
|= DCX_CLIPCHILDREN
;
110 if (style
& WS_CLIPSIBLINGS
) dce
->DCXflags
|= DCX_CLIPSIBLINGS
;
112 SetHookFlags16( HDC_16(dce
->hDC
), DCHF_INVALIDATEVISRGN
);
114 else dce
->DCXflags
= DCX_CACHE
| DCX_DCEEMPTY
;
117 dce
->next
= firstDCE
;
124 /***********************************************************************
127 DCE
* DCE_FreeDCE( DCE
*dce
)
131 if (!dce
) return NULL
;
137 while (*ppDCE
&& (*ppDCE
!= dce
)) ppDCE
= &(*ppDCE
)->next
;
138 if (*ppDCE
== dce
) *ppDCE
= dce
->next
;
142 SetDCHook(dce
->hDC
, NULL
, 0L);
144 DeleteDC( dce
->hDC
);
145 if (dce
->hClipRgn
) DeleteObject(dce
->hClipRgn
);
146 HeapFree( GetProcessHeap(), 0, dce
);
151 /***********************************************************************
154 * Remove owned DCE and reset unreleased cache DCEs.
156 void DCE_FreeWindowDCE( HWND hwnd
)
159 WND
*pWnd
= WIN_GetPtr( hwnd
);
164 if( pDCE
->hwndCurrent
== hwnd
)
166 if( pDCE
== pWnd
->dce
) /* owned or Class DCE*/
168 if (pWnd
->clsStyle
& CS_OWNDC
) /* owned DCE*/
170 pDCE
= DCE_FreeDCE( pDCE
);
174 else if( pDCE
->DCXflags
& (DCX_INTERSECTRGN
| DCX_EXCLUDERGN
) ) /* Class DCE*/
176 if (USER_Driver
.pReleaseDC
)
177 USER_Driver
.pReleaseDC( pDCE
->hwndCurrent
, pDCE
->hDC
);
178 DCE_DeleteClipRgn( pDCE
);
179 pDCE
->hwndCurrent
= 0;
184 if( pDCE
->DCXflags
& DCX_DCEBUSY
) /* shared cache DCE */
186 /* FIXME: AFAICS we are doing the right thing here so
187 * this should be a WARN. But this is best left as an ERR
188 * because the 'application error' is likely to come from
189 * another part of Wine (i.e. it's our fault after all).
190 * We should change this to WARN when Wine is more stable
193 ERR("[%p] GetDC() without ReleaseDC()!\n",hwnd
);
194 DCE_ReleaseDC( pDCE
);
197 if (pDCE
->hwndCurrent
&& USER_Driver
.pReleaseDC
)
198 USER_Driver
.pReleaseDC( pDCE
->hwndCurrent
, pDCE
->hDC
);
199 pDCE
->DCXflags
&= DCX_CACHE
;
200 pDCE
->DCXflags
|= DCX_DCEEMPTY
;
201 pDCE
->hwndCurrent
= 0;
206 WIN_ReleasePtr( pWnd
);
210 /***********************************************************************
213 static void DCE_DeleteClipRgn( DCE
* dce
)
215 dce
->DCXflags
&= ~(DCX_EXCLUDERGN
| DCX_INTERSECTRGN
| DCX_WINDOWPAINT
);
217 if (dce
->hClipRgn
) DeleteObject( dce
->hClipRgn
);
220 /* make it dirty so that the vis rgn gets recomputed next time */
221 dce
->DCXflags
|= DCX_DCEDIRTY
;
222 SetHookFlags16( HDC_16(dce
->hDC
), DCHF_INVALIDATEVISRGN
);
226 /***********************************************************************
229 static INT
DCE_ReleaseDC( DCE
* dce
)
231 if ((dce
->DCXflags
& (DCX_DCEEMPTY
| DCX_DCEBUSY
)) != DCX_DCEBUSY
) return 0;
233 /* restore previous visible region */
235 if ((dce
->DCXflags
& (DCX_INTERSECTRGN
| DCX_EXCLUDERGN
)) &&
236 (dce
->DCXflags
& (DCX_CACHE
| DCX_WINDOWPAINT
)) )
237 DCE_DeleteClipRgn( dce
);
239 if (dce
->DCXflags
& DCX_CACHE
)
241 /* make the DC clean so that SetDCState doesn't try to update the vis rgn */
242 SetHookFlags16( HDC_16(dce
->hDC
), DCHF_VALIDATEVISRGN
);
243 SetDCState16( HDC_16(dce
->hDC
), defaultDCstate
);
244 dce
->DCXflags
&= ~DCX_DCEBUSY
;
245 if (dce
->DCXflags
& DCX_DCEDIRTY
)
247 /* don't keep around invalidated entries
248 * because SetDCState() disables hVisRgn updates
249 * by removing dirty bit. */
250 if (dce
->hwndCurrent
&& USER_Driver
.pReleaseDC
)
251 USER_Driver
.pReleaseDC( dce
->hwndCurrent
, dce
->hDC
);
252 dce
->hwndCurrent
= 0;
253 dce
->DCXflags
&= DCX_CACHE
;
254 dce
->DCXflags
|= DCX_DCEEMPTY
;
261 /***********************************************************************
264 * It is called from SetWindowPos() and EVENT_MapNotify - we have to
265 * mark as dirty all busy DCEs for windows that have pWnd->parent as
266 * an ancestor and whose client rect intersects with specified update
267 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
268 * DCX_CLIPCHILDREN flag is set. */
269 BOOL
DCE_InvalidateDCE(HWND hwnd
, const RECT
* pRectUpdate
)
271 HWND hwndScope
= GetAncestor( hwnd
, GA_PARENT
);
278 TRACE("scope hwnd = %p, (%ld,%ld - %ld,%ld)\n",
279 hwndScope
, pRectUpdate
->left
,pRectUpdate
->top
,
280 pRectUpdate
->right
,pRectUpdate
->bottom
);
284 /* walk all DCEs and fixup non-empty entries */
286 for (dce
= firstDCE
; (dce
); dce
= dce
->next
)
288 if (dce
->DCXflags
& DCX_DCEEMPTY
) continue;
289 if ((dce
->hwndCurrent
== hwndScope
) && !(dce
->DCXflags
& DCX_CLIPCHILDREN
))
290 continue; /* child window positions don't bother us */
292 /* check if DCE window is within the z-order scope */
294 if (hwndScope
== dce
->hwndCurrent
|| IsChild( hwndScope
, dce
->hwndCurrent
))
296 if (hwnd
!= dce
->hwndCurrent
)
298 /* check if the window rectangle intersects this DCE window */
300 GetWindowRect( dce
->hwndCurrent
, &rect
);
301 MapWindowPoints( 0, hwndScope
, (POINT
*)&rect
, 2 );
302 if (!IntersectRect( &rect
, &rect
, pRectUpdate
)) continue;
305 if( !(dce
->DCXflags
& DCX_DCEBUSY
) )
307 /* Don't bother with visible regions of unused DCEs */
309 TRACE("\tpurged %p dce [%p]\n", dce
, dce
->hwndCurrent
);
310 if (dce
->hwndCurrent
&& USER_Driver
.pReleaseDC
)
311 USER_Driver
.pReleaseDC( dce
->hwndCurrent
, dce
->hDC
);
312 dce
->hwndCurrent
= 0;
313 dce
->DCXflags
&= DCX_CACHE
;
314 dce
->DCXflags
|= DCX_DCEEMPTY
;
318 /* Set dirty bits in the hDC and DCE structs */
320 TRACE("\tfixed up %p dce [%p]\n", dce
, dce
->hwndCurrent
);
321 dce
->DCXflags
|= DCX_DCEDIRTY
;
322 SetHookFlags16( HDC_16(dce
->hDC
), DCHF_INVALIDATEVISRGN
);
332 /***********************************************************************
335 * Unimplemented flags: DCX_LOCKWINDOWUPDATE
337 * FIXME: Full support for hrgnClip == 1 (alias for entire window).
339 HDC WINAPI
GetDCEx( HWND hwnd
, HRGN hrgnClip
, DWORD flags
)
345 BOOL bUpdateVisRgn
= TRUE
;
346 BOOL bUpdateClipOrigin
= FALSE
;
349 TRACE("hwnd %p, hrgnClip %p, flags %08lx\n", hwnd
, hrgnClip
, flags
);
351 if (flags
& (DCX_LOCKWINDOWUPDATE
)) {
352 FIXME("not yet supported - see source\n");
353 /* See the comment in LockWindowUpdate for more explanation. This flag is not implemented
354 * by that patch, but we need LockWindowUpdate implemented correctly before this can be done.
358 if (!hwnd
) hwnd
= GetDesktopWindow();
359 if (!(full
= WIN_IsCurrentProcess( hwnd
)))
361 FIXME( "not supported yet on other process window %p\n", hwnd
);
365 if (!(wndPtr
= WIN_GetPtr( hwnd
))) return 0;
369 if (flags
& (DCX_WINDOW
| DCX_PARENTCLIP
)) flags
|= DCX_CACHE
;
371 if (flags
& DCX_USESTYLE
)
373 flags
&= ~( DCX_CLIPCHILDREN
| DCX_CLIPSIBLINGS
| DCX_PARENTCLIP
);
375 if( wndPtr
->dwStyle
& WS_CLIPSIBLINGS
)
376 flags
|= DCX_CLIPSIBLINGS
;
378 if ( !(flags
& DCX_WINDOW
) )
380 if (wndPtr
->clsStyle
& CS_PARENTDC
) flags
|= DCX_PARENTCLIP
;
382 if (wndPtr
->dwStyle
& WS_CLIPCHILDREN
&&
383 !(wndPtr
->dwStyle
& WS_MINIMIZE
) ) flags
|= DCX_CLIPCHILDREN
;
384 if (!wndPtr
->dce
) flags
|= DCX_CACHE
;
388 if (flags
& DCX_WINDOW
) flags
&= ~DCX_CLIPCHILDREN
;
390 parent
= GetAncestor( hwnd
, GA_PARENT
);
391 if (!parent
|| (parent
== GetDesktopWindow()))
392 flags
= (flags
& ~DCX_PARENTCLIP
) | DCX_CLIPSIBLINGS
;
394 /* it seems parent clip is ignored when clipping siblings or children */
395 if (flags
& (DCX_CLIPSIBLINGS
| DCX_CLIPCHILDREN
)) flags
&= ~DCX_PARENTCLIP
;
397 if( flags
& DCX_PARENTCLIP
)
399 LONG parent_style
= GetWindowLongW( parent
, GWL_STYLE
);
400 if( (wndPtr
->dwStyle
& WS_VISIBLE
) && (parent_style
& WS_VISIBLE
) )
402 flags
&= ~DCX_CLIPCHILDREN
;
403 if (parent_style
& WS_CLIPSIBLINGS
) flags
|= DCX_CLIPSIBLINGS
;
407 /* find a suitable DCE */
409 dcxFlags
= flags
& (DCX_PARENTCLIP
| DCX_CLIPSIBLINGS
| DCX_CLIPCHILDREN
|
410 DCX_CACHE
| DCX_WINDOW
);
412 if (flags
& DCX_CACHE
)
417 dceEmpty
= dceUnused
= NULL
;
419 /* Strategy: First, we attempt to find a non-empty but unused DCE with
420 * compatible flags. Next, we look for an empty entry. If the cache is
421 * full we have to purge one of the unused entries.
424 for (dce
= firstDCE
; (dce
); dce
= dce
->next
)
426 if ((dce
->DCXflags
& (DCX_CACHE
| DCX_DCEBUSY
)) == DCX_CACHE
)
430 if (dce
->DCXflags
& DCX_DCEEMPTY
)
433 if ((dce
->hwndCurrent
== hwnd
) &&
434 ((dce
->DCXflags
& (DCX_CLIPSIBLINGS
| DCX_CLIPCHILDREN
|
435 DCX_CACHE
| DCX_WINDOW
| DCX_PARENTCLIP
)) == dcxFlags
))
437 TRACE("\tfound valid %p dce [%p], flags %08lx\n",
438 dce
, hwnd
, dcxFlags
);
439 bUpdateVisRgn
= FALSE
;
440 bUpdateClipOrigin
= TRUE
;
446 if (!dce
) dce
= (dceEmpty
) ? dceEmpty
: dceUnused
;
448 /* if there's no dce empty or unused, allocate a new one */
451 dce
= DCE_AllocDCE( 0, DCE_CACHE_DC
);
457 if (dce
&& dce
->hwndCurrent
== hwnd
)
459 TRACE("\tskipping hVisRgn update\n");
460 bUpdateVisRgn
= FALSE
; /* updated automatically, via DCHook() */
469 if (!(flags
& (DCX_INTERSECTRGN
| DCX_EXCLUDERGN
))) hrgnClip
= 0;
471 if (((flags
^ dce
->DCXflags
) & (DCX_INTERSECTRGN
| DCX_EXCLUDERGN
)) &&
472 (dce
->hClipRgn
!= hrgnClip
))
474 /* if the extra clip region has changed, get rid of the old one */
475 DCE_DeleteClipRgn( dce
);
478 dce
->hwndCurrent
= hwnd
;
479 dce
->hClipRgn
= hrgnClip
;
480 dce
->DCXflags
= flags
& (DCX_PARENTCLIP
| DCX_CLIPSIBLINGS
| DCX_CLIPCHILDREN
|
481 DCX_CACHE
| DCX_WINDOW
| DCX_WINDOWPAINT
|
482 DCX_INTERSECTRGN
| DCX_EXCLUDERGN
);
483 dce
->DCXflags
|= DCX_DCEBUSY
;
484 dce
->DCXflags
&= ~DCX_DCEDIRTY
;
487 if (bUpdateVisRgn
) SetHookFlags16( HDC_16(hdc
), DCHF_INVALIDATEVISRGN
); /* force update */
489 if (!USER_Driver
.pGetDC
|| !USER_Driver
.pGetDC( hwnd
, hdc
, hrgnClip
, flags
))
492 TRACE("(%p,%p,0x%lx): returning %p\n", hwnd
, hrgnClip
, flags
, hdc
);
494 WIN_ReleasePtr(wndPtr
);
499 /***********************************************************************
502 * Get a device context.
505 * Success: Handle to the device context
508 HDC WINAPI
GetDC( HWND hwnd
)
511 return GetDCEx( 0, 0, DCX_CACHE
| DCX_WINDOW
);
512 return GetDCEx( hwnd
, 0, DCX_USESTYLE
);
516 /***********************************************************************
517 * GetWindowDC (USER32.@)
519 HDC WINAPI
GetWindowDC( HWND hwnd
)
521 return GetDCEx( hwnd
, 0, DCX_USESTYLE
| DCX_WINDOW
);
525 /***********************************************************************
526 * ReleaseDC (USER32.@)
528 * Release a device context.
531 * Success: Non-zero. Resources used by hdc are released.
534 INT WINAPI
ReleaseDC( HWND hwnd
, HDC hdc
)
542 TRACE("%p %p\n", hwnd
, hdc
);
544 while (dce
&& (dce
->hDC
!= hdc
)) dce
= dce
->next
;
547 if ( dce
->DCXflags
& DCX_DCEBUSY
)
548 nRet
= DCE_ReleaseDC( dce
);
555 /***********************************************************************
558 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
560 BOOL16 WINAPI
DCHook16( HDC16 hDC
, WORD code
, DWORD data
, LPARAM lParam
)
563 DCE
*dce
= (DCE
*)data
;
565 TRACE("hDC = %04x, %i\n", hDC
, code
);
568 assert( HDC_16(dce
->hDC
) == hDC
);
570 /* Grab the windows lock before doing anything else */
575 case DCHC_INVALIDVISRGN
:
576 /* GDI code calls this when it detects that the
577 * DC is dirty (usually after SetHookFlags()). This
578 * means that we have to recompute the visible region.
580 if( dce
->DCXflags
& DCX_DCEBUSY
)
582 /* Dirty bit has been cleared by caller, set it again so that
583 * pGetDC recomputes the visible region. */
584 SetHookFlags16( hDC
, DCHF_INVALIDATEVISRGN
);
585 if (USER_Driver
.pGetDC
)
586 USER_Driver
.pGetDC( dce
->hwndCurrent
, dce
->hDC
, dce
->hClipRgn
, dce
->DCXflags
);
588 else /* non-fatal but shouldn't happen */
589 WARN("DC is not in use!\n");
594 * Windows will not let you delete a DC that is busy
595 * (between GetDC and ReleaseDC)
598 if ( dce
->DCXflags
& DCX_DCEBUSY
)
600 WARN("Application trying to delete a busy DC\n");
603 else DCE_FreeDCE( dce
);
607 FIXME("unknown code\n");
610 USER_Unlock(); /* Release the wnd lock */
615 /**********************************************************************
616 * WindowFromDC (USER32.@)
618 HWND WINAPI
WindowFromDC( HDC hDC
)
626 while (dce
&& (dce
->hDC
!= hDC
)) dce
= dce
->next
;
628 hwnd
= dce
? dce
->hwndCurrent
: 0;
635 /***********************************************************************
636 * LockWindowUpdate (USER32.@)
638 BOOL WINAPI
LockWindowUpdate( HWND hwnd
)
640 static HWND lockedWnd
;
642 /* This function is fully implemented by the following patch:
644 * http://www.winehq.org/hypermail/wine-patches/2004/01/0142.html
646 * but in order to work properly, it needs the ability to invalidate
647 * DCEs in other processes when the lock window is changed, which
648 * isn't possible yet.
652 FIXME("(%p), partial stub!\n",hwnd
);
659 /* Unlock lockedWnd */
660 /* FIXME: Do something */
664 /* Attempted to lock a second window */
665 /* Return FALSE and do nothing */