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);
48 static void DCE_DeleteClipRgn( DCE* );
49 static INT DCE_ReleaseDC( DCE* );
52 /***********************************************************************
55 static void DCE_DumpCache(void)
61 for(dce = firstDCE; dce; dce = dce->next)
63 TRACE("\t[0x%08x] hWnd %p, dcx %08x, %s %s\n",
64 (unsigned)dce, dce->hwndCurrent, (unsigned)dce->DCXflags,
65 (dce->DCXflags & DCX_CACHE) ? "Cache" : "Owned",
66 (dce->DCXflags & DCX_DCEBUSY) ? "InUse" : "" );
72 /***********************************************************************
77 DCE *DCE_AllocDCE( HWND hWnd, DCE_TYPE type )
79 static const WCHAR szDisplayW[] = { 'D','I','S','P','L','A','Y','\0' };
82 TRACE("(%p,%d)\n", hWnd, type);
84 if (!(dce = HeapAlloc( GetProcessHeap(), 0, sizeof(DCE) ))) return NULL;
85 if (!(dce->hDC = CreateDCW( szDisplayW, NULL, NULL, NULL )))
87 HeapFree( GetProcessHeap(), 0, dce );
92 /* store DCE handle in DC hook data field */
94 SetDCHook( dce->hDC, DCHook16, (DWORD)dce );
96 dce->hwndCurrent = hWnd;
99 if( type != DCE_CACHE_DC ) /* owned or class DC */
101 dce->DCXflags = DCX_DCEBUSY;
104 LONG style = GetWindowLongW( hWnd, GWL_STYLE );
105 if (style & WS_CLIPCHILDREN) dce->DCXflags |= DCX_CLIPCHILDREN;
106 if (style & WS_CLIPSIBLINGS) dce->DCXflags |= DCX_CLIPSIBLINGS;
108 SetHookFlags16( HDC_16(dce->hDC), DCHF_INVALIDATEVISRGN );
110 else dce->DCXflags = DCX_CACHE | DCX_DCEEMPTY;
113 dce->next = firstDCE;
120 /***********************************************************************
123 DCE* DCE_FreeDCE( DCE *dce )
127 if (!dce) return NULL;
133 while (*ppDCE && (*ppDCE != dce)) ppDCE = &(*ppDCE)->next;
134 if (*ppDCE == dce) *ppDCE = dce->next;
138 SetDCHook(dce->hDC, NULL, 0L);
140 DeleteDC( dce->hDC );
141 if (dce->hClipRgn) DeleteObject(dce->hClipRgn);
142 HeapFree( GetProcessHeap(), 0, dce );
147 /***********************************************************************
150 * Remove owned DCE and reset unreleased cache DCEs.
152 void DCE_FreeWindowDCE( HWND hwnd )
155 WND *pWnd = WIN_GetPtr( hwnd );
160 if( pDCE->hwndCurrent == hwnd )
162 if( pDCE == pWnd->dce ) /* owned or Class DCE*/
164 if (pWnd->clsStyle & CS_OWNDC) /* owned DCE*/
166 pDCE = DCE_FreeDCE( pDCE );
170 else if( pDCE->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN) ) /* Class DCE*/
172 if (USER_Driver.pReleaseDC)
173 USER_Driver.pReleaseDC( pDCE->hwndCurrent, pDCE->hDC );
174 DCE_DeleteClipRgn( pDCE );
175 pDCE->hwndCurrent = 0;
180 if( pDCE->DCXflags & DCX_DCEBUSY ) /* shared cache DCE */
182 /* FIXME: AFAICS we are doing the right thing here so
183 * this should be a WARN. But this is best left as an ERR
184 * because the 'application error' is likely to come from
185 * another part of Wine (i.e. it's our fault after all).
186 * We should change this to WARN when Wine is more stable
189 ERR("[%p] GetDC() without ReleaseDC()!\n",hwnd);
190 DCE_ReleaseDC( pDCE );
193 if (pDCE->hwndCurrent && USER_Driver.pReleaseDC)
194 USER_Driver.pReleaseDC( pDCE->hwndCurrent, pDCE->hDC );
195 pDCE->DCXflags &= DCX_CACHE;
196 pDCE->DCXflags |= DCX_DCEEMPTY;
197 pDCE->hwndCurrent = 0;
202 WIN_ReleasePtr( pWnd );
206 /***********************************************************************
209 static void DCE_DeleteClipRgn( DCE* dce )
211 dce->DCXflags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN | DCX_WINDOWPAINT);
213 if (dce->hClipRgn) DeleteObject( dce->hClipRgn );
216 /* make it dirty so that the vis rgn gets recomputed next time */
217 dce->DCXflags |= DCX_DCEDIRTY;
218 SetHookFlags16( HDC_16(dce->hDC), DCHF_INVALIDATEVISRGN );
222 /***********************************************************************
225 static INT DCE_ReleaseDC( DCE* dce )
227 if ((dce->DCXflags & (DCX_DCEEMPTY | DCX_DCEBUSY)) != DCX_DCEBUSY) return 0;
229 /* restore previous visible region */
231 if ((dce->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
232 (dce->DCXflags & (DCX_CACHE | DCX_WINDOWPAINT)) )
233 DCE_DeleteClipRgn( dce );
235 if (dce->DCXflags & DCX_CACHE)
237 /* make the DC clean so that RestoreDC doesn't try to update the vis rgn */
238 SetHookFlags16( HDC_16(dce->hDC), DCHF_VALIDATEVISRGN );
239 RestoreDC( dce->hDC, 1 ); /* initial save level is always 1 */
240 SaveDC( dce->hDC ); /* save the state again for next time */
241 dce->DCXflags &= ~DCX_DCEBUSY;
242 if (dce->DCXflags & DCX_DCEDIRTY)
244 /* don't keep around invalidated entries
245 * because RestoreDC() disables hVisRgn updates
246 * by removing dirty bit. */
247 if (dce->hwndCurrent && USER_Driver.pReleaseDC)
248 USER_Driver.pReleaseDC( dce->hwndCurrent, dce->hDC );
249 dce->hwndCurrent = 0;
250 dce->DCXflags &= DCX_CACHE;
251 dce->DCXflags |= DCX_DCEEMPTY;
258 /***********************************************************************
261 * It is called from SetWindowPos() and EVENT_MapNotify - we have to
262 * mark as dirty all busy DCEs for windows that have pWnd->parent as
263 * an ancestor and whose client rect intersects with specified update
264 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
265 * DCX_CLIPCHILDREN flag is set. */
266 BOOL DCE_InvalidateDCE(HWND hwnd, const RECT* pRectUpdate)
268 HWND hwndScope = GetAncestor( hwnd, GA_PARENT );
275 TRACE("scope hwnd = %p, (%ld,%ld - %ld,%ld)\n",
276 hwndScope, pRectUpdate->left,pRectUpdate->top,
277 pRectUpdate->right,pRectUpdate->bottom);
281 /* walk all DCEs and fixup non-empty entries */
283 for (dce = firstDCE; (dce); dce = dce->next)
285 if (dce->DCXflags & DCX_DCEEMPTY) continue;
286 if ((dce->hwndCurrent == hwndScope) && !(dce->DCXflags & DCX_CLIPCHILDREN))
287 continue; /* child window positions don't bother us */
289 /* check if DCE window is within the z-order scope */
291 if (hwndScope == dce->hwndCurrent || IsChild( hwndScope, dce->hwndCurrent ))
293 if (hwnd != dce->hwndCurrent)
295 /* check if the window rectangle intersects this DCE window */
297 GetWindowRect( dce->hwndCurrent, &rect );
298 MapWindowPoints( 0, hwndScope, (POINT *)&rect, 2 );
299 if (!IntersectRect( &rect, &rect, pRectUpdate )) continue;
302 if( !(dce->DCXflags & DCX_DCEBUSY) )
304 /* Don't bother with visible regions of unused DCEs */
306 TRACE("\tpurged %p dce [%p]\n", dce, dce->hwndCurrent);
307 if (dce->hwndCurrent && USER_Driver.pReleaseDC)
308 USER_Driver.pReleaseDC( dce->hwndCurrent, dce->hDC );
309 dce->hwndCurrent = 0;
310 dce->DCXflags &= DCX_CACHE;
311 dce->DCXflags |= DCX_DCEEMPTY;
315 /* Set dirty bits in the hDC and DCE structs */
317 TRACE("\tfixed up %p dce [%p]\n", dce, dce->hwndCurrent);
318 dce->DCXflags |= DCX_DCEDIRTY;
319 SetHookFlags16( HDC_16(dce->hDC), DCHF_INVALIDATEVISRGN );
329 /***********************************************************************
332 * Unimplemented flags: DCX_LOCKWINDOWUPDATE
334 * FIXME: Full support for hrgnClip == 1 (alias for entire window).
336 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
342 BOOL bUpdateVisRgn = TRUE;
343 BOOL bUpdateClipOrigin = FALSE;
346 TRACE("hwnd %p, hrgnClip %p, flags %08lx\n", hwnd, hrgnClip, flags);
348 if (flags & (DCX_LOCKWINDOWUPDATE)) {
349 FIXME("not yet supported - see source\n");
350 /* See the comment in LockWindowUpdate for more explanation. This flag is not implemented
351 * by that patch, but we need LockWindowUpdate implemented correctly before this can be done.
355 if (!hwnd) hwnd = GetDesktopWindow();
356 if (!(full = WIN_IsCurrentProcess( hwnd )))
358 FIXME( "not supported yet on other process window %p\n", hwnd );
362 if (!(wndPtr = WIN_GetPtr( hwnd ))) return 0;
366 if (flags & (DCX_WINDOW | DCX_PARENTCLIP)) flags |= DCX_CACHE;
368 if (flags & DCX_USESTYLE)
370 flags &= ~( DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
372 if( wndPtr->dwStyle & WS_CLIPSIBLINGS )
373 flags |= DCX_CLIPSIBLINGS;
375 if ( !(flags & DCX_WINDOW) )
377 if (wndPtr->clsStyle & CS_PARENTDC) flags |= DCX_PARENTCLIP;
379 if (wndPtr->dwStyle & WS_CLIPCHILDREN &&
380 !(wndPtr->dwStyle & WS_MINIMIZE) ) flags |= DCX_CLIPCHILDREN;
381 if (!wndPtr->dce) flags |= DCX_CACHE;
385 if (flags & DCX_WINDOW) flags &= ~DCX_CLIPCHILDREN;
387 parent = GetAncestor( hwnd, GA_PARENT );
388 if (!parent || (parent == GetDesktopWindow()))
389 flags = (flags & ~DCX_PARENTCLIP) | DCX_CLIPSIBLINGS;
391 /* it seems parent clip is ignored when clipping siblings or children */
392 if (flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) flags &= ~DCX_PARENTCLIP;
394 if( flags & DCX_PARENTCLIP )
396 LONG parent_style = GetWindowLongW( parent, GWL_STYLE );
397 if( (wndPtr->dwStyle & WS_VISIBLE) && (parent_style & WS_VISIBLE) )
399 flags &= ~DCX_CLIPCHILDREN;
400 if (parent_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
404 /* find a suitable DCE */
406 dcxFlags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
407 DCX_CACHE | DCX_WINDOW);
409 if (flags & DCX_CACHE)
414 dceEmpty = dceUnused = NULL;
416 /* Strategy: First, we attempt to find a non-empty but unused DCE with
417 * compatible flags. Next, we look for an empty entry. If the cache is
418 * full we have to purge one of the unused entries.
421 for (dce = firstDCE; (dce); dce = dce->next)
423 if ((dce->DCXflags & (DCX_CACHE | DCX_DCEBUSY)) == DCX_CACHE )
427 if (dce->DCXflags & DCX_DCEEMPTY)
430 if ((dce->hwndCurrent == hwnd) &&
431 ((dce->DCXflags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
432 DCX_CACHE | DCX_WINDOW | DCX_PARENTCLIP)) == dcxFlags))
434 TRACE("\tfound valid %p dce [%p], flags %08lx\n",
435 dce, hwnd, dcxFlags );
436 bUpdateVisRgn = FALSE;
437 bUpdateClipOrigin = TRUE;
443 if (!dce) dce = (dceEmpty) ? dceEmpty : dceUnused;
445 /* if there's no dce empty or unused, allocate a new one */
448 dce = DCE_AllocDCE( 0, DCE_CACHE_DC );
454 if (dce && dce->hwndCurrent == hwnd)
456 TRACE("\tskipping hVisRgn update\n");
457 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
466 if (!(flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN))) hrgnClip = 0;
468 if (((flags ^ dce->DCXflags) & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
469 (dce->hClipRgn != hrgnClip))
471 /* if the extra clip region has changed, get rid of the old one */
472 DCE_DeleteClipRgn( dce );
475 dce->hwndCurrent = hwnd;
476 dce->hClipRgn = hrgnClip;
477 dce->DCXflags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
478 DCX_CACHE | DCX_WINDOW | DCX_WINDOWPAINT |
479 DCX_INTERSECTRGN | DCX_EXCLUDERGN);
480 dce->DCXflags |= DCX_DCEBUSY;
481 dce->DCXflags &= ~DCX_DCEDIRTY;
484 if (bUpdateVisRgn) SetHookFlags16( HDC_16(hdc), DCHF_INVALIDATEVISRGN ); /* force update */
486 if (!USER_Driver.pGetDC || !USER_Driver.pGetDC( hwnd, hdc, hrgnClip, flags ))
489 TRACE("(%p,%p,0x%lx): returning %p\n", hwnd, hrgnClip, flags, hdc);
491 WIN_ReleasePtr(wndPtr);
496 /***********************************************************************
499 * Get a device context.
502 * Success: Handle to the device context
505 HDC WINAPI GetDC( HWND hwnd )
508 return GetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
509 return GetDCEx( hwnd, 0, DCX_USESTYLE );
513 /***********************************************************************
514 * GetWindowDC (USER32.@)
516 HDC WINAPI GetWindowDC( HWND hwnd )
518 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
522 /***********************************************************************
523 * ReleaseDC (USER32.@)
525 * Release a device context.
528 * Success: Non-zero. Resources used by hdc are released.
531 INT WINAPI ReleaseDC( HWND hwnd, HDC hdc )
539 TRACE("%p %p\n", hwnd, hdc );
541 while (dce && (dce->hDC != hdc)) dce = dce->next;
544 if ( dce->DCXflags & DCX_DCEBUSY )
545 nRet = DCE_ReleaseDC( dce );
552 /***********************************************************************
555 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
557 BOOL16 WINAPI DCHook16( HDC16 hDC, WORD code, DWORD data, LPARAM lParam )
560 DCE *dce = (DCE *)data;
562 TRACE("hDC = %04x, %i\n", hDC, code);
565 assert( HDC_16(dce->hDC) == hDC );
567 /* Grab the windows lock before doing anything else */
572 case DCHC_INVALIDVISRGN:
573 /* GDI code calls this when it detects that the
574 * DC is dirty (usually after SetHookFlags()). This
575 * means that we have to recompute the visible region.
577 if( dce->DCXflags & DCX_DCEBUSY )
579 /* Dirty bit has been cleared by caller, set it again so that
580 * pGetDC recomputes the visible region. */
581 SetHookFlags16( hDC, DCHF_INVALIDATEVISRGN );
582 if (USER_Driver.pGetDC)
583 USER_Driver.pGetDC( dce->hwndCurrent, dce->hDC, dce->hClipRgn, dce->DCXflags );
585 else /* non-fatal but shouldn't happen */
586 WARN("DC is not in use!\n");
591 * Windows will not let you delete a DC that is busy
592 * (between GetDC and ReleaseDC)
595 if ( dce->DCXflags & DCX_DCEBUSY )
597 WARN("Application trying to delete a busy DC\n");
600 else DCE_FreeDCE( dce );
604 FIXME("unknown code\n");
607 USER_Unlock(); /* Release the wnd lock */
612 /**********************************************************************
613 * WindowFromDC (USER32.@)
615 HWND WINAPI WindowFromDC( HDC hDC )
623 while (dce && (dce->hDC != hDC)) dce = dce->next;
625 hwnd = dce ? dce->hwndCurrent : 0;
632 /***********************************************************************
633 * LockWindowUpdate (USER32.@)
635 BOOL WINAPI LockWindowUpdate( HWND hwnd )
637 static HWND lockedWnd;
639 /* This function is fully implemented by the following patch:
641 * http://www.winehq.org/hypermail/wine-patches/2004/01/0142.html
643 * but in order to work properly, it needs the ability to invalidate
644 * DCEs in other processes when the lock window is changed, which
645 * isn't possible yet.
649 FIXME("(%p), partial stub!\n",hwnd);
656 /* Unlock lockedWnd */
657 /* FIXME: Do something */
661 /* Attempted to lock a second window */
662 /* Return FALSE and do nothing */