Authors: Mike McCormack <mike@codeweavers.com>, Aric Stewart <aric@codeweavers.com...
[wine/multimedia.git] / windows / dce.c
blob4d489f8d399f4909f2d55e3175843a697d6c27fb
1 /*
2 * USER DCE functions
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.
25 * Internal DCX flags:
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
33 #include <assert.h>
34 #include "dce.h"
35 #include "win.h"
36 #include "user_private.h"
37 #include "wine/debug.h"
38 #include "windef.h"
39 #include "wingdi.h"
40 #include "wownt32.h"
41 #include "wine/winbase16.h"
42 #include "wine/winuser16.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(dc);
46 static DCE *firstDCE;
48 static void DCE_DeleteClipRgn( DCE* );
49 static INT DCE_ReleaseDC( DCE* );
52 /***********************************************************************
53 * DCE_DumpCache
55 static void DCE_DumpCache(void)
57 DCE *dce;
59 USER_Lock();
60 dce = firstDCE;
62 DPRINTF("DCE:\n");
63 while( dce )
65 DPRINTF("\t[0x%08x] hWnd %p, dcx %08x, %s %s\n",
66 (unsigned)dce, dce->hwndCurrent, (unsigned)dce->DCXflags,
67 (dce->DCXflags & DCX_CACHE) ? "Cache" : "Owned",
68 (dce->DCXflags & DCX_DCEBUSY) ? "InUse" : "" );
69 dce = dce->next;
72 USER_Unlock();
75 /***********************************************************************
76 * DCE_AllocDCE
78 * Allocate a new DCE.
80 DCE *DCE_AllocDCE( HWND hWnd, DCE_TYPE type )
82 static const WCHAR szDisplayW[] = { 'D','I','S','P','L','A','Y','\0' };
83 DCE * dce;
85 TRACE("(%p,%d)\n", hWnd, type);
87 if (!(dce = HeapAlloc( GetProcessHeap(), 0, sizeof(DCE) ))) return NULL;
88 if (!(dce->hDC = CreateDCW( szDisplayW, NULL, NULL, NULL )))
90 HeapFree( GetProcessHeap(), 0, dce );
91 return 0;
93 SaveDC( dce->hDC );
95 /* store DCE handle in DC hook data field */
97 SetDCHook( dce->hDC, DCHook16, (DWORD)dce );
99 dce->hwndCurrent = hWnd;
100 dce->hClipRgn = 0;
102 if( type != DCE_CACHE_DC ) /* owned or class DC */
104 dce->DCXflags = DCX_DCEBUSY;
105 if( hWnd )
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;
115 USER_Lock();
116 dce->next = firstDCE;
117 firstDCE = dce;
118 USER_Unlock();
119 return dce;
123 /***********************************************************************
124 * DCE_FreeDCE
126 DCE* DCE_FreeDCE( DCE *dce )
128 DCE **ppDCE, *ret;
130 if (!dce) return NULL;
132 USER_Lock();
134 ppDCE = &firstDCE;
136 while (*ppDCE && (*ppDCE != dce)) ppDCE = &(*ppDCE)->next;
137 if (*ppDCE == dce) *ppDCE = dce->next;
138 ret = *ppDCE;
139 USER_Unlock();
141 SetDCHook(dce->hDC, NULL, 0L);
143 DeleteDC( dce->hDC );
144 if (dce->hClipRgn) DeleteObject(dce->hClipRgn);
145 HeapFree( GetProcessHeap(), 0, dce );
147 return ret;
150 /***********************************************************************
151 * DCE_FreeWindowDCE
153 * Remove owned DCE and reset unreleased cache DCEs.
155 void DCE_FreeWindowDCE( HWND hwnd )
157 DCE *pDCE;
158 WND *pWnd = WIN_GetPtr( hwnd );
160 pDCE = firstDCE;
161 while( pDCE )
163 if( pDCE->hwndCurrent == hwnd )
165 if( pDCE == pWnd->dce ) /* owned or Class DCE*/
167 if (pWnd->clsStyle & CS_OWNDC) /* owned DCE*/
169 pDCE = DCE_FreeDCE( pDCE );
170 pWnd->dce = NULL;
171 continue;
173 else if( pDCE->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN) ) /* Class DCE*/
175 if (USER_Driver.pReleaseDC)
176 USER_Driver.pReleaseDC( pDCE->hwndCurrent, pDCE->hDC );
177 DCE_DeleteClipRgn( pDCE );
178 pDCE->hwndCurrent = 0;
181 else
183 if( pDCE->DCXflags & DCX_DCEBUSY ) /* shared cache DCE */
185 /* FIXME: AFAICS we are doing the right thing here so
186 * this should be a WARN. But this is best left as an ERR
187 * because the 'application error' is likely to come from
188 * another part of Wine (i.e. it's our fault after all).
189 * We should change this to WARN when Wine is more stable
190 * (for 1.0?).
192 ERR("[%p] GetDC() without ReleaseDC()!\n",hwnd);
193 DCE_ReleaseDC( pDCE );
196 if (pDCE->hwndCurrent && USER_Driver.pReleaseDC)
197 USER_Driver.pReleaseDC( pDCE->hwndCurrent, pDCE->hDC );
198 pDCE->DCXflags &= DCX_CACHE;
199 pDCE->DCXflags |= DCX_DCEEMPTY;
200 pDCE->hwndCurrent = 0;
203 pDCE = pDCE->next;
205 WIN_ReleasePtr( pWnd );
209 /***********************************************************************
210 * DCE_DeleteClipRgn
212 static void DCE_DeleteClipRgn( DCE* dce )
214 dce->DCXflags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN | DCX_WINDOWPAINT);
216 if (dce->hClipRgn) DeleteObject( dce->hClipRgn );
217 dce->hClipRgn = 0;
219 /* make it dirty so that the vis rgn gets recomputed next time */
220 dce->DCXflags |= DCX_DCEDIRTY;
221 SetHookFlags16( HDC_16(dce->hDC), DCHF_INVALIDATEVISRGN );
225 /***********************************************************************
226 * DCE_ReleaseDC
228 static INT DCE_ReleaseDC( DCE* dce )
230 if ((dce->DCXflags & (DCX_DCEEMPTY | DCX_DCEBUSY)) != DCX_DCEBUSY) return 0;
232 /* restore previous visible region */
234 if ((dce->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
235 (dce->DCXflags & (DCX_CACHE | DCX_WINDOWPAINT)) )
236 DCE_DeleteClipRgn( dce );
238 if (dce->DCXflags & DCX_CACHE)
240 /* make the DC clean so that RestoreDC doesn't try to update the vis rgn */
241 SetHookFlags16( HDC_16(dce->hDC), DCHF_VALIDATEVISRGN );
242 RestoreDC( dce->hDC, 1 ); /* initial save level is always 1 */
243 SaveDC( dce->hDC ); /* save the state again for next time */
244 dce->DCXflags &= ~DCX_DCEBUSY;
245 if (dce->DCXflags & DCX_DCEDIRTY)
247 /* don't keep around invalidated entries
248 * because RestoreDC() 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;
257 return 1;
261 /***********************************************************************
262 * DCE_InvalidateDCE
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 );
272 BOOL bRet = FALSE;
274 if( hwndScope )
276 DCE *dce;
278 TRACE("scope hwnd = %p, (%ld,%ld - %ld,%ld)\n",
279 hwndScope, pRectUpdate->left,pRectUpdate->top,
280 pRectUpdate->right,pRectUpdate->bottom);
281 if(TRACE_ON(dc))
282 DCE_DumpCache();
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 */
299 RECT rect;
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;
316 else
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 );
323 bRet = TRUE;
326 } /* dce list */
328 return bRet;
332 /***********************************************************************
333 * GetDCEx (USER32.@)
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 )
341 HDC hdc = 0;
342 DCE * dce;
343 WND * wndPtr;
344 DWORD dcxFlags = 0;
345 BOOL bUpdateVisRgn = TRUE;
346 BOOL bUpdateClipOrigin = FALSE;
347 HWND parent, full;
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 );
362 return 0;
364 hwnd = full;
365 if (!(wndPtr = WIN_GetPtr( hwnd ))) return 0;
367 /* fixup flags */
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)
414 DCE* dceEmpty;
415 DCE* dceUnused;
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 )
428 dceUnused = dce;
430 if (dce->DCXflags & DCX_DCEEMPTY)
431 dceEmpty = dce;
432 else
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;
441 break;
446 if (!dce) dce = (dceEmpty) ? dceEmpty : dceUnused;
448 /* if there's no dce empty or unused, allocate a new one */
449 if (!dce)
451 dce = DCE_AllocDCE( 0, DCE_CACHE_DC );
454 else
456 dce = wndPtr->dce;
457 if (dce && dce->hwndCurrent == hwnd)
459 TRACE("\tskipping hVisRgn update\n");
460 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
463 if (!dce)
465 hdc = 0;
466 goto END;
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;
485 hdc = dce->hDC;
487 if (bUpdateVisRgn) SetHookFlags16( HDC_16(hdc), DCHF_INVALIDATEVISRGN ); /* force update */
489 if (!USER_Driver.pGetDC || !USER_Driver.pGetDC( hwnd, hdc, hrgnClip, flags ))
490 hdc = 0;
492 TRACE("(%p,%p,0x%lx): returning %p\n", hwnd, hrgnClip, flags, hdc);
493 END:
494 WIN_ReleasePtr(wndPtr);
495 return hdc;
499 /***********************************************************************
500 * GetDC (USER32.@)
502 * Get a device context.
504 * RETURNS
505 * Success: Handle to the device context
506 * Failure: NULL.
508 HDC WINAPI GetDC( HWND hwnd )
510 if (!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.
530 * RETURNS
531 * Success: Non-zero. Resources used by hdc are released.
532 * Failure: 0.
534 INT WINAPI ReleaseDC( HWND hwnd, HDC hdc )
536 DCE * dce;
537 INT nRet = 0;
539 USER_Lock();
540 dce = firstDCE;
542 TRACE("%p %p\n", hwnd, hdc );
544 while (dce && (dce->hDC != hdc)) dce = dce->next;
546 if ( dce )
547 if ( dce->DCXflags & DCX_DCEBUSY )
548 nRet = DCE_ReleaseDC( dce );
550 USER_Unlock();
552 return nRet;
555 /***********************************************************************
556 * DCHook (USER.362)
558 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
560 BOOL16 WINAPI DCHook16( HDC16 hDC, WORD code, DWORD data, LPARAM lParam )
562 BOOL retv = TRUE;
563 DCE *dce = (DCE *)data;
565 TRACE("hDC = %04x, %i\n", hDC, code);
567 if (!dce) return 0;
568 assert( HDC_16(dce->hDC) == hDC );
570 /* Grab the windows lock before doing anything else */
571 USER_Lock();
573 switch( code )
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");
590 break;
592 case DCHC_DELETEDC:
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");
601 retv = FALSE;
603 else DCE_FreeDCE( dce );
604 break;
606 default:
607 FIXME("unknown code\n");
610 USER_Unlock(); /* Release the wnd lock */
611 return retv;
615 /**********************************************************************
616 * WindowFromDC (USER32.@)
618 HWND WINAPI WindowFromDC( HDC hDC )
620 DCE *dce;
621 HWND hwnd;
623 USER_Lock();
624 dce = firstDCE;
626 while (dce && (dce->hDC != hDC)) dce = dce->next;
628 hwnd = dce ? dce->hwndCurrent : 0;
629 USER_Unlock();
631 return hwnd;
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.
649 * -mike
652 FIXME("(%p), partial stub!\n",hwnd);
654 USER_Lock();
655 if (lockedWnd)
657 if (!hwnd)
659 /* Unlock lockedWnd */
660 /* FIXME: Do something */
662 else
664 /* Attempted to lock a second window */
665 /* Return FALSE and do nothing */
666 USER_Unlock();
667 return FALSE;
670 lockedWnd = hwnd;
671 USER_Unlock();
672 return TRUE;