- Begun implementation of a C statements parser.
[wine/multimedia.git] / windows / dce.c
blobfc11d3c4cf01cb528535b177efb731d22cc90f52
1 /*
2 * USER DCE functions
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.
11 * Internal DCX flags:
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
20 #include <assert.h>
21 #include "dce.h"
22 #include "win.h"
23 #include "gdi.h"
24 #include "region.h"
25 #include "user.h"
26 #include "debugtools.h"
27 #include "windef.h"
28 #include "wingdi.h"
29 #include "wine/winbase16.h"
30 #include "wine/winuser16.h"
32 DEFAULT_DEBUG_CHANNEL(dc);
34 static DCE *firstDCE;
35 static HDC defaultDCstate;
37 static void DCE_DeleteClipRgn( DCE* );
38 static INT DCE_ReleaseDC( DCE* );
41 /***********************************************************************
42 * DCE_DumpCache
44 static void DCE_DumpCache(void)
46 DCE *dce;
48 WIN_LockWnds();
49 dce = firstDCE;
51 DPRINTF("DCE:\n");
52 while( dce )
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" : "" );
58 dce = dce->next;
61 WIN_UnlockWnds();
64 /***********************************************************************
65 * DCE_AllocDCE
67 * Allocate a new DCE.
69 DCE *DCE_AllocDCE( HWND hWnd, DCE_TYPE type )
71 FARPROC16 hookProc;
72 DCE * dce;
73 WND* wnd;
75 if (!(dce = HeapAlloc( GetProcessHeap(), 0, sizeof(DCE) ))) return NULL;
76 if (!(dce->hDC = CreateDCA( "DISPLAY", NULL, NULL, NULL )))
78 HeapFree( GetProcessHeap(), 0, dce );
79 return 0;
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;
91 dce->hClipRgn = 0;
92 dce->next = firstDCE;
93 firstDCE = dce;
95 if( type != DCE_CACHE_DC ) /* owned or class DC */
97 dce->DCXflags = DCX_DCEBUSY;
98 if( hWnd )
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);
109 return dce;
113 /***********************************************************************
114 * DCE_FreeDCE
116 DCE* DCE_FreeDCE( DCE *dce )
118 DCE **ppDCE;
120 if (!dce) return NULL;
122 WIN_LockWnds();
124 ppDCE = &firstDCE;
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 );
136 WIN_UnlockWnds();
138 return *ppDCE;
141 /***********************************************************************
142 * DCE_FreeWindowDCE
144 * Remove owned DCE and reset unreleased cache DCEs.
146 void DCE_FreeWindowDCE( WND* pWnd )
148 DCE *pDCE;
150 WIN_LockWnds();
151 pDCE = firstDCE;
153 while( pDCE )
155 if( pDCE->hwndCurrent == pWnd->hwndSelf )
157 if( pDCE == pWnd->dce ) /* owned or Class DCE*/
159 if (pWnd->clsStyle & CS_OWNDC) /* owned DCE*/
161 pDCE = DCE_FreeDCE( pDCE );
162 pWnd->dce = NULL;
163 continue;
165 else if( pDCE->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN) ) /* Class DCE*/
167 DCE_DeleteClipRgn( pDCE );
168 pDCE->hwndCurrent = 0;
171 else
173 if( pDCE->DCXflags & DCX_DCEBUSY ) /* shared cache DCE */
175 /* FIXME: AFAICS we are doing the right thing here so
176 * this should be a WARN. But this is best left as an ERR
177 * because the 'application error' is likely to come from
178 * another part of Wine (i.e. it's our fault after all).
179 * We should change this to WARN when Wine is more stable
180 * (for 1.0?).
182 ERR("[%04x] GetDC() without ReleaseDC()!\n",
183 pWnd->hwndSelf);
184 DCE_ReleaseDC( pDCE );
187 pDCE->DCXflags &= DCX_CACHE;
188 pDCE->DCXflags |= DCX_DCEEMPTY;
189 pDCE->hwndCurrent = 0;
192 pDCE = pDCE->next;
195 WIN_UnlockWnds();
199 /***********************************************************************
200 * DCE_DeleteClipRgn
202 static void DCE_DeleteClipRgn( DCE* dce )
204 dce->DCXflags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN | DCX_WINDOWPAINT);
206 if( dce->DCXflags & DCX_KEEPCLIPRGN )
207 dce->DCXflags &= ~DCX_KEEPCLIPRGN;
208 else
209 if( dce->hClipRgn > 1 )
210 DeleteObject( dce->hClipRgn );
212 dce->hClipRgn = 0;
214 /* make it dirty so that the vis rgn gets recomputed next time */
215 dce->DCXflags |= DCX_DCEDIRTY;
216 SetHookFlags16( dce->hDC, DCHF_INVALIDATEVISRGN );
220 /***********************************************************************
221 * DCE_ReleaseDC
223 static INT DCE_ReleaseDC( DCE* dce )
225 if ((dce->DCXflags & (DCX_DCEEMPTY | DCX_DCEBUSY)) != DCX_DCEBUSY) return 0;
227 /* restore previous visible region */
229 if ((dce->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
230 (dce->DCXflags & (DCX_CACHE | DCX_WINDOWPAINT)) )
231 DCE_DeleteClipRgn( dce );
233 if (dce->DCXflags & DCX_CACHE)
235 SetDCState16( dce->hDC, defaultDCstate );
236 dce->DCXflags &= ~DCX_DCEBUSY;
237 if (dce->DCXflags & DCX_DCEDIRTY)
239 /* don't keep around invalidated entries
240 * because SetDCState() disables hVisRgn updates
241 * by removing dirty bit. */
243 dce->hwndCurrent = 0;
244 dce->DCXflags &= DCX_CACHE;
245 dce->DCXflags |= DCX_DCEEMPTY;
248 return 1;
252 /***********************************************************************
253 * DCE_InvalidateDCE
255 * It is called from SetWindowPos() and EVENT_MapNotify - we have to
256 * mark as dirty all busy DCEs for windows that have pWnd->parent as
257 * an ancestor and whose client rect intersects with specified update
258 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
259 * DCX_CLIPCHILDREN flag is set. */
260 BOOL DCE_InvalidateDCE(WND* pWnd, const RECT* pRectUpdate)
262 WND* wndScope = WIN_LockWndPtr(pWnd->parent);
263 BOOL bRet = FALSE;
265 if( wndScope )
267 DCE *dce;
269 TRACE("scope hwnd = %04x, (%i,%i - %i,%i)\n",
270 wndScope->hwndSelf, pRectUpdate->left,pRectUpdate->top,
271 pRectUpdate->right,pRectUpdate->bottom);
272 if(TRACE_ON(dc))
273 DCE_DumpCache();
275 /* walk all DCEs and fixup non-empty entries */
277 for (dce = firstDCE; (dce); dce = dce->next)
279 if( !(dce->DCXflags & DCX_DCEEMPTY) )
281 WND* wndCurrent = WIN_FindWndPtr(dce->hwndCurrent);
283 if( wndCurrent )
285 WND* wnd = NULL;
286 INT xoffset = 0, yoffset = 0;
288 if( (wndCurrent == wndScope) && !(dce->DCXflags & DCX_CLIPCHILDREN) )
290 /* child window positions don't bother us */
291 WIN_ReleaseWndPtr(wndCurrent);
292 continue;
295 /* check if DCE window is within the z-order scope */
297 for( wnd = WIN_LockWndPtr(wndCurrent); wnd; WIN_UpdateWndPtr(&wnd,wnd->parent))
299 if( wnd == wndScope )
301 RECT wndRect;
303 wndRect = wndCurrent->rectWindow;
305 OffsetRect( &wndRect, xoffset - wndCurrent->rectClient.left,
306 yoffset - wndCurrent->rectClient.top);
308 if (pWnd == wndCurrent ||
309 IntersectRect( &wndRect, &wndRect, pRectUpdate ))
311 if( !(dce->DCXflags & DCX_DCEBUSY) )
313 /* Don't bother with visible regions of unused DCEs */
315 TRACE("\tpurged %08x dce [%04x]\n",
316 (unsigned)dce, wndCurrent->hwndSelf);
318 dce->hwndCurrent = 0;
319 dce->DCXflags &= DCX_CACHE;
320 dce->DCXflags |= DCX_DCEEMPTY;
322 else
324 /* Set dirty bits in the hDC and DCE structs */
326 TRACE("\tfixed up %08x dce [%04x]\n",
327 (unsigned)dce, wndCurrent->hwndSelf);
329 dce->DCXflags |= DCX_DCEDIRTY;
330 SetHookFlags16(dce->hDC, DCHF_INVALIDATEVISRGN);
331 bRet = TRUE;
334 WIN_ReleaseWndPtr(wnd);
335 break;
337 xoffset += wnd->rectClient.left;
338 yoffset += wnd->rectClient.top;
341 WIN_ReleaseWndPtr(wndCurrent);
343 } /* dce list */
344 WIN_ReleaseWndPtr(wndScope);
346 return bRet;
350 /***********************************************************************
351 * DCE_ExcludeRgn
353 * Translate given region from the wnd client to the DC coordinates
354 * and add it to the clipping region.
356 INT DCE_ExcludeRgn( HDC hDC, WND* wnd, HRGN hRgn )
358 POINT pt = {0, 0};
359 DCE *dce = firstDCE;
361 while (dce && (dce->hDC != hDC)) dce = dce->next;
362 if( dce )
364 MapWindowPoints( wnd->hwndSelf, dce->hwndCurrent, &pt, 1);
365 if( dce->DCXflags & DCX_WINDOW )
367 wnd = WIN_FindWndPtr(dce->hwndCurrent);
368 pt.x += wnd->rectClient.left - wnd->rectWindow.left;
369 pt.y += wnd->rectClient.top - wnd->rectWindow.top;
370 WIN_ReleaseWndPtr(wnd);
373 else return ERROR;
374 OffsetRgn(hRgn, pt.x, pt.y);
376 return ExtSelectClipRgn( hDC, hRgn, RGN_DIFF );
380 /***********************************************************************
381 * GetDCEx (USER.359)
383 HDC16 WINAPI GetDCEx16( HWND16 hwnd, HRGN16 hrgnClip, DWORD flags )
385 return (HDC16)GetDCEx( hwnd, hrgnClip, flags );
389 /***********************************************************************
390 * GetDCEx (USER32.@)
392 * Unimplemented flags: DCX_LOCKWINDOWUPDATE
394 * FIXME: Full support for hrgnClip == 1 (alias for entire window).
396 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
398 HDC hdc = 0;
399 DCE * dce;
400 WND * wndPtr;
401 DWORD dcxFlags = 0;
402 BOOL bUpdateVisRgn = TRUE;
403 BOOL bUpdateClipOrigin = FALSE;
405 TRACE("hwnd %04x, hrgnClip %04x, flags %08x\n",
406 hwnd, hrgnClip, (unsigned)flags);
408 if (!hwnd) hwnd = GetDesktopWindow();
409 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
411 /* fixup flags */
413 if (flags & (DCX_WINDOW | DCX_PARENTCLIP)) flags |= DCX_CACHE;
415 if (flags & DCX_USESTYLE)
417 flags &= ~( DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
419 if( wndPtr->dwStyle & WS_CLIPSIBLINGS )
420 flags |= DCX_CLIPSIBLINGS;
422 if ( !(flags & DCX_WINDOW) )
424 if (wndPtr->clsStyle & CS_PARENTDC) flags |= DCX_PARENTCLIP;
426 if (wndPtr->dwStyle & WS_CLIPCHILDREN &&
427 !(wndPtr->dwStyle & WS_MINIMIZE) ) flags |= DCX_CLIPCHILDREN;
428 if (!wndPtr->dce) flags |= DCX_CACHE;
432 if (flags & DCX_WINDOW) flags &= ~DCX_CLIPCHILDREN;
434 if (!wndPtr->parent || (wndPtr->parent->hwndSelf == GetDesktopWindow()))
435 flags = (flags & ~DCX_PARENTCLIP) | DCX_CLIPSIBLINGS;
437 /* it seems parent clip is ignored when clipping siblings or children */
438 if (flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) flags &= ~DCX_PARENTCLIP;
440 if( flags & DCX_PARENTCLIP )
442 if( (wndPtr->dwStyle & WS_VISIBLE) && (wndPtr->parent->dwStyle & WS_VISIBLE) )
444 flags &= ~DCX_CLIPCHILDREN;
445 if( wndPtr->parent->dwStyle & WS_CLIPSIBLINGS )
446 flags |= DCX_CLIPSIBLINGS;
450 /* find a suitable DCE */
452 dcxFlags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
453 DCX_CACHE | DCX_WINDOW);
455 if (flags & DCX_CACHE)
457 DCE* dceEmpty;
458 DCE* dceUnused;
460 dceEmpty = dceUnused = NULL;
462 /* Strategy: First, we attempt to find a non-empty but unused DCE with
463 * compatible flags. Next, we look for an empty entry. If the cache is
464 * full we have to purge one of the unused entries.
467 for (dce = firstDCE; (dce); dce = dce->next)
469 if ((dce->DCXflags & (DCX_CACHE | DCX_DCEBUSY)) == DCX_CACHE )
471 dceUnused = dce;
473 if (dce->DCXflags & DCX_DCEEMPTY)
474 dceEmpty = dce;
475 else
476 if ((dce->hwndCurrent == hwnd) &&
477 ((dce->DCXflags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
478 DCX_CACHE | DCX_WINDOW | DCX_PARENTCLIP)) == dcxFlags))
480 TRACE("\tfound valid %08x dce [%04x], flags %08x\n",
481 (unsigned)dce, hwnd, (unsigned)dcxFlags );
482 bUpdateVisRgn = FALSE;
483 bUpdateClipOrigin = TRUE;
484 break;
489 if (!dce) dce = (dceEmpty) ? dceEmpty : dceUnused;
491 /* if there's no dce empty or unused, allocate a new one */
492 if (!dce)
494 dce = DCE_AllocDCE( 0, DCE_CACHE_DC );
497 else
499 dce = wndPtr->dce;
500 if (dce && dce->hwndCurrent == hwnd)
502 TRACE("\tskipping hVisRgn update\n");
503 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
506 if (!dce)
508 hdc = 0;
509 goto END;
512 if (!(flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN))) hrgnClip = 0;
514 if (((flags ^ dce->DCXflags) & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
515 (dce->hClipRgn != hrgnClip))
517 /* if the extra clip region has changed, get rid of the old one */
518 DCE_DeleteClipRgn( dce );
521 dce->hwndCurrent = hwnd;
522 dce->hClipRgn = hrgnClip;
523 dce->DCXflags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
524 DCX_CACHE | DCX_WINDOW | DCX_WINDOWPAINT |
525 DCX_KEEPCLIPRGN | DCX_INTERSECTRGN | DCX_EXCLUDERGN);
526 dce->DCXflags |= DCX_DCEBUSY;
527 dce->DCXflags &= ~DCX_DCEDIRTY;
528 hdc = dce->hDC;
530 if (bUpdateVisRgn) SetHookFlags16( hdc, DCHF_INVALIDATEVISRGN ); /* force update */
532 if (!USER_Driver.pGetDC( hwnd, hdc, hrgnClip, flags )) hdc = 0;
534 TRACE("(%04x,%04x,0x%lx): returning %04x\n", hwnd, hrgnClip, flags, hdc);
535 END:
536 WIN_ReleaseWndPtr(wndPtr);
537 return hdc;
541 /***********************************************************************
542 * GetDC (USER.66)
544 HDC16 WINAPI GetDC16( HWND16 hwnd )
546 return (HDC16)GetDC( hwnd );
550 /***********************************************************************
551 * GetDC (USER32.@)
552 * RETURNS
553 * :Handle to DC
554 * NULL: Failure
556 HDC WINAPI GetDC(
557 HWND hwnd /* [in] handle of window */
559 if (!hwnd)
560 return GetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
561 return GetDCEx( hwnd, 0, DCX_USESTYLE );
565 /***********************************************************************
566 * GetWindowDC (USER.67)
568 HDC16 WINAPI GetWindowDC16( HWND16 hwnd )
570 return GetDCEx16( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
574 /***********************************************************************
575 * GetWindowDC (USER32.@)
577 HDC WINAPI GetWindowDC( HWND hwnd )
579 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
583 /***********************************************************************
584 * ReleaseDC (USER.68)
586 INT16 WINAPI ReleaseDC16( HWND16 hwnd, HDC16 hdc )
588 return (INT16)ReleaseDC( hwnd, hdc );
592 /***********************************************************************
593 * ReleaseDC (USER32.@)
595 * RETURNS
596 * 1: Success
597 * 0: Failure
599 INT WINAPI ReleaseDC(
600 HWND hwnd /* [in] Handle of window - ignored */,
601 HDC hdc /* [in] Handle of device context */
603 DCE * dce;
604 INT nRet = 0;
606 WIN_LockWnds();
607 dce = firstDCE;
609 TRACE("%04x %04x\n", hwnd, hdc );
611 while (dce && (dce->hDC != hdc)) dce = dce->next;
613 if ( dce )
614 if ( dce->DCXflags & DCX_DCEBUSY )
615 nRet = DCE_ReleaseDC( dce );
617 WIN_UnlockWnds();
619 return nRet;
622 /***********************************************************************
623 * DCHook (USER.362)
625 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
627 BOOL16 WINAPI DCHook16( HDC16 hDC, WORD code, DWORD data, LPARAM lParam )
629 BOOL retv = TRUE;
630 DCE *dce = (DCE *)data;
632 TRACE("hDC = %04x, %i\n", hDC, code);
634 if (!dce) return 0;
635 assert(dce->hDC == hDC);
637 /* Grab the windows lock before doing anything else */
638 WIN_LockWnds();
640 switch( code )
642 case DCHC_INVALIDVISRGN:
643 /* GDI code calls this when it detects that the
644 * DC is dirty (usually after SetHookFlags()). This
645 * means that we have to recompute the visible region.
647 if( dce->DCXflags & DCX_DCEBUSY )
649 /* Dirty bit has been cleared by caller, set it again so that
650 * pGetDC recomputes the visible region. */
651 SetHookFlags16( dce->hDC, DCHF_INVALIDATEVISRGN );
652 USER_Driver.pGetDC( dce->hwndCurrent, dce->hDC, dce->hClipRgn, dce->DCXflags );
654 else /* non-fatal but shouldn't happen */
655 WARN("DC is not in use!\n");
656 break;
658 case DCHC_DELETEDC:
660 * Windows will not let you delete a DC that is busy
661 * (between GetDC and ReleaseDC)
664 if ( dce->DCXflags & DCX_DCEBUSY )
666 WARN("Application trying to delete a busy DC\n");
667 retv = FALSE;
669 break;
671 default:
672 FIXME("unknown code\n");
675 WIN_UnlockWnds(); /* Release the wnd lock */
676 return retv;
680 /**********************************************************************
681 * WindowFromDC (USER.117)
683 HWND16 WINAPI WindowFromDC16( HDC16 hDC )
685 return (HWND16)WindowFromDC( hDC );
689 /**********************************************************************
690 * WindowFromDC (USER32.@)
692 HWND WINAPI WindowFromDC( HDC hDC )
694 DCE *dce;
695 HWND hwnd;
697 WIN_LockWnds();
698 dce = firstDCE;
700 while (dce && (dce->hDC != hDC)) dce = dce->next;
702 hwnd = dce ? dce->hwndCurrent : 0;
703 WIN_UnlockWnds();
705 return hwnd;
709 /***********************************************************************
710 * LockWindowUpdate (USER.294)
712 BOOL16 WINAPI LockWindowUpdate16( HWND16 hwnd )
714 return LockWindowUpdate( hwnd );
717 /***********************************************************************
718 * LockWindowUpdate (USER32.@)
720 BOOL WINAPI LockWindowUpdate( HWND hwnd )
722 FIXME("(%x), stub!\n",hwnd);
723 return TRUE;