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_KEEPCLIPRGN - ReleaseDC() should not delete the clipping region
31 * DCX_WINDOWPAINT - BeginPaint() is in effect
39 #include "wine/debug.h"
43 #include "wine/winbase16.h"
44 #include "wine/winuser16.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(dc);
49 static HDC16 defaultDCstate;
51 static void DCE_DeleteClipRgn( DCE* );
52 static INT DCE_ReleaseDC( DCE* );
55 /***********************************************************************
58 static void DCE_DumpCache(void)
68 DPRINTF("\t[0x%08x] hWnd %p, dcx %08x, %s %s\n",
69 (unsigned)dce, dce->hwndCurrent, (unsigned)dce->DCXflags,
70 (dce->DCXflags & DCX_CACHE) ? "Cache" : "Owned",
71 (dce->DCXflags & DCX_DCEBUSY) ? "InUse" : "" );
78 /***********************************************************************
83 DCE *DCE_AllocDCE( HWND hWnd, DCE_TYPE type )
87 if (!(dce = HeapAlloc( GetProcessHeap(), 0, sizeof(DCE) ))) return NULL;
88 if (!(dce->hDC = CreateDCA( "DISPLAY", NULL, NULL, NULL )))
90 HeapFree( GetProcessHeap(), 0, dce );
93 if (!defaultDCstate) defaultDCstate = GetDCState16( HDC_16(dce->hDC) );
95 /* store DCE handle in DC hook data field */
97 SetDCHook( dce->hDC, DCHook16, (DWORD)dce );
99 dce->hwndCurrent = WIN_GetFullHandle( hWnd );
102 if( type != DCE_CACHE_DC ) /* owned or class DC */
104 dce->DCXflags = DCX_DCEBUSY;
107 LONG style = GetWindowLongW( hWnd, GWL_STYLE );
108 if (style & WS_CLIPCHILDREN) dce->DCXflags |= DCX_CLIPCHILDREN;
109 if (style & WS_CLIPSIBLINGS) dce->DCXflags |= DCX_CLIPSIBLINGS;
111 SetHookFlags16( HDC_16(dce->hDC), DCHF_INVALIDATEVISRGN );
113 else dce->DCXflags = DCX_CACHE | DCX_DCEEMPTY;
116 dce->next = firstDCE;
123 /***********************************************************************
126 DCE* DCE_FreeDCE( DCE *dce )
130 if (!dce) return NULL;
136 while (*ppDCE && (*ppDCE != dce)) ppDCE = &(*ppDCE)->next;
137 if (*ppDCE == dce) *ppDCE = dce->next;
141 SetDCHook(dce->hDC, NULL, 0L);
143 DeleteDC( dce->hDC );
144 if( dce->hClipRgn && !(dce->DCXflags & DCX_KEEPCLIPRGN) )
145 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->DCXflags & DCX_KEEPCLIPRGN )
218 dce->DCXflags &= ~DCX_KEEPCLIPRGN;
220 if( dce->hClipRgn > (HRGN)1 )
221 DeleteObject( dce->hClipRgn );
225 /* make it dirty so that the vis rgn gets recomputed next time */
226 dce->DCXflags |= DCX_DCEDIRTY;
227 SetHookFlags16( HDC_16(dce->hDC), DCHF_INVALIDATEVISRGN );
231 /***********************************************************************
234 static INT DCE_ReleaseDC( DCE* dce )
236 if ((dce->DCXflags & (DCX_DCEEMPTY | DCX_DCEBUSY)) != DCX_DCEBUSY) return 0;
238 /* restore previous visible region */
240 if ((dce->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
241 (dce->DCXflags & (DCX_CACHE | DCX_WINDOWPAINT)) )
242 DCE_DeleteClipRgn( dce );
244 if (dce->DCXflags & DCX_CACHE)
246 /* make the DC clean so that SetDCState doesn't try to update the vis rgn */
247 SetHookFlags16( HDC_16(dce->hDC), DCHF_VALIDATEVISRGN );
248 SetDCState16( HDC_16(dce->hDC), defaultDCstate );
249 dce->DCXflags &= ~DCX_DCEBUSY;
250 if (dce->DCXflags & DCX_DCEDIRTY)
252 /* don't keep around invalidated entries
253 * because SetDCState() disables hVisRgn updates
254 * by removing dirty bit. */
255 if (dce->hwndCurrent && USER_Driver.pReleaseDC)
256 USER_Driver.pReleaseDC( dce->hwndCurrent, dce->hDC );
257 dce->hwndCurrent = 0;
258 dce->DCXflags &= DCX_CACHE;
259 dce->DCXflags |= DCX_DCEEMPTY;
266 /***********************************************************************
269 * It is called from SetWindowPos() and EVENT_MapNotify - we have to
270 * mark as dirty all busy DCEs for windows that have pWnd->parent as
271 * an ancestor and whose client rect intersects with specified update
272 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
273 * DCX_CLIPCHILDREN flag is set. */
274 BOOL DCE_InvalidateDCE(HWND hwnd, const RECT* pRectUpdate)
276 HWND hwndScope = GetAncestor( hwnd, GA_PARENT );
283 TRACE("scope hwnd = %p, (%ld,%ld - %ld,%ld)\n",
284 hwndScope, pRectUpdate->left,pRectUpdate->top,
285 pRectUpdate->right,pRectUpdate->bottom);
289 /* walk all DCEs and fixup non-empty entries */
291 for (dce = firstDCE; (dce); dce = dce->next)
293 if (dce->DCXflags & DCX_DCEEMPTY) continue;
294 if ((dce->hwndCurrent == hwndScope) && !(dce->DCXflags & DCX_CLIPCHILDREN))
295 continue; /* child window positions don't bother us */
297 /* check if DCE window is within the z-order scope */
299 if (hwndScope == dce->hwndCurrent || IsChild( hwndScope, dce->hwndCurrent ))
301 if (hwnd != dce->hwndCurrent)
303 /* check if the window rectangle intersects this DCE window */
305 GetWindowRect( dce->hwndCurrent, &rect );
306 MapWindowPoints( 0, hwndScope, (POINT *)&rect, 2 );
307 if (!IntersectRect( &rect, &rect, pRectUpdate )) continue;
310 if( !(dce->DCXflags & DCX_DCEBUSY) )
312 /* Don't bother with visible regions of unused DCEs */
314 TRACE("\tpurged %p dce [%p]\n", dce, dce->hwndCurrent);
315 if (dce->hwndCurrent && USER_Driver.pReleaseDC)
316 USER_Driver.pReleaseDC( dce->hwndCurrent, dce->hDC );
317 dce->hwndCurrent = 0;
318 dce->DCXflags &= DCX_CACHE;
319 dce->DCXflags |= DCX_DCEEMPTY;
323 /* Set dirty bits in the hDC and DCE structs */
325 TRACE("\tfixed up %p dce [%p]\n", dce, dce->hwndCurrent);
326 dce->DCXflags |= DCX_DCEDIRTY;
327 SetHookFlags16( HDC_16(dce->hDC), DCHF_INVALIDATEVISRGN );
337 /***********************************************************************
340 * Translate given region from the wnd client to the DC coordinates
341 * and add it to the clipping region.
343 INT DCE_ExcludeRgn( HDC hDC, HWND hwnd, HRGN hRgn )
348 while (dce && (dce->hDC != hDC)) dce = dce->next;
349 if (!dce) return ERROR;
351 MapWindowPoints( hwnd, dce->hwndCurrent, &pt, 1);
352 if( dce->DCXflags & DCX_WINDOW )
354 WND *wnd = WIN_FindWndPtr(dce->hwndCurrent);
355 pt.x += wnd->rectClient.left - wnd->rectWindow.left;
356 pt.y += wnd->rectClient.top - wnd->rectWindow.top;
357 WIN_ReleaseWndPtr(wnd);
359 OffsetRgn(hRgn, pt.x, pt.y);
361 return ExtSelectClipRgn( hDC, hRgn, RGN_DIFF );
365 /***********************************************************************
368 * Unimplemented flags: DCX_LOCKWINDOWUPDATE
370 * FIXME: Full support for hrgnClip == 1 (alias for entire window).
372 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
378 BOOL bUpdateVisRgn = TRUE;
379 BOOL bUpdateClipOrigin = FALSE;
382 TRACE("hwnd %p, hrgnClip %p, flags %08lx\n", hwnd, hrgnClip, flags);
384 if (!hwnd) hwnd = GetDesktopWindow();
385 if (!(full = WIN_IsCurrentProcess( hwnd )))
387 FIXME( "not supported yet on other process window %p\n", hwnd );
391 if (!(wndPtr = WIN_GetPtr( hwnd ))) return 0;
395 if (flags & (DCX_WINDOW | DCX_PARENTCLIP)) flags |= DCX_CACHE;
397 if (flags & DCX_USESTYLE)
399 flags &= ~( DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
401 if( wndPtr->dwStyle & WS_CLIPSIBLINGS )
402 flags |= DCX_CLIPSIBLINGS;
404 if ( !(flags & DCX_WINDOW) )
406 if (wndPtr->clsStyle & CS_PARENTDC) flags |= DCX_PARENTCLIP;
408 if (wndPtr->dwStyle & WS_CLIPCHILDREN &&
409 !(wndPtr->dwStyle & WS_MINIMIZE) ) flags |= DCX_CLIPCHILDREN;
410 if (!wndPtr->dce) flags |= DCX_CACHE;
414 if (flags & DCX_WINDOW) flags &= ~DCX_CLIPCHILDREN;
416 parent = GetAncestor( hwnd, GA_PARENT );
417 if (!parent || (parent == GetDesktopWindow()))
418 flags = (flags & ~DCX_PARENTCLIP) | DCX_CLIPSIBLINGS;
420 /* it seems parent clip is ignored when clipping siblings or children */
421 if (flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) flags &= ~DCX_PARENTCLIP;
423 if( flags & DCX_PARENTCLIP )
425 LONG parent_style = GetWindowLongW( parent, GWL_STYLE );
426 if( (wndPtr->dwStyle & WS_VISIBLE) && (parent_style & WS_VISIBLE) )
428 flags &= ~DCX_CLIPCHILDREN;
429 if (parent_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
433 /* find a suitable DCE */
435 dcxFlags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
436 DCX_CACHE | DCX_WINDOW);
438 if (flags & DCX_CACHE)
443 dceEmpty = dceUnused = NULL;
445 /* Strategy: First, we attempt to find a non-empty but unused DCE with
446 * compatible flags. Next, we look for an empty entry. If the cache is
447 * full we have to purge one of the unused entries.
450 for (dce = firstDCE; (dce); dce = dce->next)
452 if ((dce->DCXflags & (DCX_CACHE | DCX_DCEBUSY)) == DCX_CACHE )
456 if (dce->DCXflags & DCX_DCEEMPTY)
459 if ((dce->hwndCurrent == hwnd) &&
460 ((dce->DCXflags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
461 DCX_CACHE | DCX_WINDOW | DCX_PARENTCLIP)) == dcxFlags))
463 TRACE("\tfound valid %p dce [%p], flags %08lx\n",
464 dce, hwnd, dcxFlags );
465 bUpdateVisRgn = FALSE;
466 bUpdateClipOrigin = TRUE;
472 if (!dce) dce = (dceEmpty) ? dceEmpty : dceUnused;
474 /* if there's no dce empty or unused, allocate a new one */
477 dce = DCE_AllocDCE( 0, DCE_CACHE_DC );
483 if (dce && dce->hwndCurrent == hwnd)
485 TRACE("\tskipping hVisRgn update\n");
486 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
495 if (!(flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN))) hrgnClip = 0;
497 if (((flags ^ dce->DCXflags) & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
498 (dce->hClipRgn != hrgnClip))
500 /* if the extra clip region has changed, get rid of the old one */
501 DCE_DeleteClipRgn( dce );
504 dce->hwndCurrent = hwnd;
505 dce->hClipRgn = hrgnClip;
506 dce->DCXflags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
507 DCX_CACHE | DCX_WINDOW | DCX_WINDOWPAINT |
508 DCX_KEEPCLIPRGN | DCX_INTERSECTRGN | DCX_EXCLUDERGN);
509 dce->DCXflags |= DCX_DCEBUSY;
510 dce->DCXflags &= ~DCX_DCEDIRTY;
513 if (bUpdateVisRgn) SetHookFlags16( HDC_16(hdc), DCHF_INVALIDATEVISRGN ); /* force update */
515 if (!USER_Driver.pGetDC( hwnd, hdc, hrgnClip, flags )) hdc = 0;
517 TRACE("(%p,%p,0x%lx): returning %p\n", hwnd, hrgnClip, flags, hdc);
519 WIN_ReleasePtr(wndPtr);
524 /***********************************************************************
527 * Get a device context.
530 * Success: Handle to the device context
534 HWND hwnd /* [in] handle of window - may be NULL */
537 return GetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
538 return GetDCEx( hwnd, 0, DCX_USESTYLE );
542 /***********************************************************************
543 * GetWindowDC (USER32.@)
545 HDC WINAPI GetWindowDC( HWND hwnd )
547 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
551 /***********************************************************************
552 * ReleaseDC (USER32.@)
554 * Release a device context.
557 * Success: Non-zero. Resources used by hdc are released.
560 INT WINAPI ReleaseDC(
561 HWND hwnd, /* [in] Handle of window - ignored */
562 HDC hdc /* [in] Handle of device context */
570 TRACE("%p %p\n", hwnd, hdc );
572 while (dce && (dce->hDC != hdc)) dce = dce->next;
575 if ( dce->DCXflags & DCX_DCEBUSY )
576 nRet = DCE_ReleaseDC( dce );
583 /***********************************************************************
586 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
588 BOOL16 WINAPI DCHook16( HDC16 hDC, WORD code, DWORD data, LPARAM lParam )
591 DCE *dce = (DCE *)data;
593 TRACE("hDC = %04x, %i\n", hDC, code);
596 assert( HDC_16(dce->hDC) == hDC );
598 /* Grab the windows lock before doing anything else */
603 case DCHC_INVALIDVISRGN:
604 /* GDI code calls this when it detects that the
605 * DC is dirty (usually after SetHookFlags()). This
606 * means that we have to recompute the visible region.
608 if( dce->DCXflags & DCX_DCEBUSY )
610 /* Dirty bit has been cleared by caller, set it again so that
611 * pGetDC recomputes the visible region. */
612 SetHookFlags16( hDC, DCHF_INVALIDATEVISRGN );
613 USER_Driver.pGetDC( dce->hwndCurrent, dce->hDC, dce->hClipRgn, dce->DCXflags );
615 else /* non-fatal but shouldn't happen */
616 WARN("DC is not in use!\n");
621 * Windows will not let you delete a DC that is busy
622 * (between GetDC and ReleaseDC)
625 if ( dce->DCXflags & DCX_DCEBUSY )
627 WARN("Application trying to delete a busy DC\n");
630 else DCE_FreeDCE( dce );
634 FIXME("unknown code\n");
637 USER_Unlock(); /* Release the wnd lock */
642 /**********************************************************************
643 * WindowFromDC (USER32.@)
645 HWND WINAPI WindowFromDC( HDC hDC )
653 while (dce && (dce->hDC != hDC)) dce = dce->next;
655 hwnd = dce ? dce->hwndCurrent : 0;
662 /***********************************************************************
663 * LockWindowUpdate (USER32.@)
665 BOOL WINAPI LockWindowUpdate( HWND hwnd )
667 static HWND lockedWnd;
669 FIXME("(%p), partial stub!\n",hwnd);
676 /* Unlock lockedWnd */
677 /* FIXME: Do something */
681 /* Attempted to lock a second window */
682 /* Return FALSE and do nothing */