Moved most of the implementation of SetWindowPos and SetDrawable into
[wine/multimedia.git] / windows / dce.c
blob4c697d710c51c20b6fffb0ad39619ba23f9d3095
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 TRACE("\trestoring VisRgn\n");
216 RestoreVisRgn16(dce->hDC);
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 WND *pDesktop = WIN_GetDesktop();
264 BOOL bRet = FALSE;
266 if( wndScope )
268 DCE *dce;
270 TRACE("scope hwnd = %04x, (%i,%i - %i,%i)\n",
271 wndScope->hwndSelf, pRectUpdate->left,pRectUpdate->top,
272 pRectUpdate->right,pRectUpdate->bottom);
273 if(TRACE_ON(dc))
274 DCE_DumpCache();
276 /* walk all DCEs and fixup non-empty entries */
278 for (dce = firstDCE; (dce); dce = dce->next)
280 if( !(dce->DCXflags & DCX_DCEEMPTY) )
282 WND* wndCurrent = WIN_FindWndPtr(dce->hwndCurrent);
284 if( wndCurrent )
286 WND* wnd = NULL;
287 INT xoffset = 0, yoffset = 0;
289 if( (wndCurrent == wndScope) && !(dce->DCXflags & DCX_CLIPCHILDREN) )
291 /* child window positions don't bother us */
292 WIN_ReleaseWndPtr(wndCurrent);
293 continue;
296 if (wndCurrent == pDesktop && !(wndCurrent->flags & WIN_NATIVE))
298 /* don't bother with fake desktop */
299 WIN_ReleaseWndPtr(wndCurrent);
300 continue;
303 /* check if DCE window is within the z-order scope */
305 for( wnd = WIN_LockWndPtr(wndCurrent); wnd; WIN_UpdateWndPtr(&wnd,wnd->parent))
307 if( wnd == wndScope )
309 RECT wndRect;
311 wndRect = wndCurrent->rectWindow;
313 OffsetRect( &wndRect, xoffset - wndCurrent->rectClient.left,
314 yoffset - wndCurrent->rectClient.top);
316 if (pWnd == wndCurrent ||
317 IntersectRect( &wndRect, &wndRect, pRectUpdate ))
319 if( !(dce->DCXflags & DCX_DCEBUSY) )
321 /* Don't bother with visible regions of unused DCEs */
323 TRACE("\tpurged %08x dce [%04x]\n",
324 (unsigned)dce, wndCurrent->hwndSelf);
326 dce->hwndCurrent = 0;
327 dce->DCXflags &= DCX_CACHE;
328 dce->DCXflags |= DCX_DCEEMPTY;
330 else
332 /* Set dirty bits in the hDC and DCE structs */
334 TRACE("\tfixed up %08x dce [%04x]\n",
335 (unsigned)dce, wndCurrent->hwndSelf);
337 dce->DCXflags |= DCX_DCEDIRTY;
338 SetHookFlags16(dce->hDC, DCHF_INVALIDATEVISRGN);
339 bRet = TRUE;
342 WIN_ReleaseWndPtr(wnd);
343 break;
345 xoffset += wnd->rectClient.left;
346 yoffset += wnd->rectClient.top;
349 WIN_ReleaseWndPtr(wndCurrent);
351 } /* dce list */
352 WIN_ReleaseWndPtr(wndScope);
354 WIN_ReleaseDesktop();
355 return bRet;
359 /***********************************************************************
360 * DCE_ExcludeRgn
362 * Translate given region from the wnd client to the DC coordinates
363 * and add it to the clipping region.
365 INT DCE_ExcludeRgn( HDC hDC, WND* wnd, HRGN hRgn )
367 POINT pt = {0, 0};
368 DCE *dce = firstDCE;
370 while (dce && (dce->hDC != hDC)) dce = dce->next;
371 if( dce )
373 MapWindowPoints( wnd->hwndSelf, dce->hwndCurrent, &pt, 1);
374 if( dce->DCXflags & DCX_WINDOW )
376 wnd = WIN_FindWndPtr(dce->hwndCurrent);
377 pt.x += wnd->rectClient.left - wnd->rectWindow.left;
378 pt.y += wnd->rectClient.top - wnd->rectWindow.top;
379 WIN_ReleaseWndPtr(wnd);
382 else return ERROR;
383 OffsetRgn(hRgn, pt.x, pt.y);
385 return ExtSelectClipRgn( hDC, hRgn, RGN_DIFF );
389 /***********************************************************************
390 * GetDCEx (USER.359)
392 HDC16 WINAPI GetDCEx16( HWND16 hwnd, HRGN16 hrgnClip, DWORD flags )
394 return (HDC16)GetDCEx( hwnd, hrgnClip, flags );
398 /***********************************************************************
399 * GetDCEx (USER32.@)
401 * Unimplemented flags: DCX_LOCKWINDOWUPDATE
403 * FIXME: Full support for hrgnClip == 1 (alias for entire window).
405 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
407 HDC hdc = 0;
408 DCE * dce;
409 WND * wndPtr;
410 DWORD dcxFlags = 0;
411 BOOL bUpdateVisRgn = TRUE;
412 BOOL bUpdateClipOrigin = FALSE;
414 TRACE("hwnd %04x, hrgnClip %04x, flags %08x\n",
415 hwnd, hrgnClip, (unsigned)flags);
417 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
419 /* fixup flags */
421 if (!wndPtr->dce) flags |= DCX_CACHE;
423 if (flags & DCX_USESTYLE)
425 flags &= ~( DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
427 if( wndPtr->dwStyle & WS_CLIPSIBLINGS )
428 flags |= DCX_CLIPSIBLINGS;
430 if ( !(flags & DCX_WINDOW) )
432 if (wndPtr->clsStyle & CS_PARENTDC) flags |= DCX_PARENTCLIP;
434 if (wndPtr->dwStyle & WS_CLIPCHILDREN &&
435 !(wndPtr->dwStyle & WS_MINIMIZE) ) flags |= DCX_CLIPCHILDREN;
437 else flags |= DCX_CACHE;
440 if( flags & DCX_NOCLIPCHILDREN )
442 flags |= DCX_CACHE;
443 flags &= ~(DCX_PARENTCLIP | DCX_CLIPCHILDREN);
446 if (flags & DCX_WINDOW)
447 flags = (flags & ~DCX_CLIPCHILDREN) | DCX_CACHE;
449 if (!(wndPtr->dwStyle & WS_CHILD) || !wndPtr->parent )
450 flags &= ~DCX_PARENTCLIP;
451 else if( flags & DCX_PARENTCLIP )
453 flags |= DCX_CACHE;
454 if( !(flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) )
455 if( (wndPtr->dwStyle & WS_VISIBLE) && (wndPtr->parent->dwStyle & WS_VISIBLE) )
457 flags &= ~DCX_CLIPCHILDREN;
458 if( wndPtr->parent->dwStyle & WS_CLIPSIBLINGS )
459 flags |= DCX_CLIPSIBLINGS;
463 /* find a suitable DCE */
465 dcxFlags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
466 DCX_CACHE | DCX_WINDOW);
468 if (flags & DCX_CACHE)
470 DCE* dceEmpty;
471 DCE* dceUnused;
473 dceEmpty = dceUnused = NULL;
475 /* Strategy: First, we attempt to find a non-empty but unused DCE with
476 * compatible flags. Next, we look for an empty entry. If the cache is
477 * full we have to purge one of the unused entries.
480 for (dce = firstDCE; (dce); dce = dce->next)
482 if ((dce->DCXflags & (DCX_CACHE | DCX_DCEBUSY)) == DCX_CACHE )
484 dceUnused = dce;
486 if (dce->DCXflags & DCX_DCEEMPTY)
487 dceEmpty = dce;
488 else
489 if ((dce->hwndCurrent == hwnd) &&
490 ((dce->DCXflags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
491 DCX_CACHE | DCX_WINDOW | DCX_PARENTCLIP)) == dcxFlags))
493 TRACE("\tfound valid %08x dce [%04x], flags %08x\n",
494 (unsigned)dce, hwnd, (unsigned)dcxFlags );
495 bUpdateVisRgn = FALSE;
496 bUpdateClipOrigin = TRUE;
497 break;
502 if (!dce) dce = (dceEmpty) ? dceEmpty : dceUnused;
504 /* if there's no dce empty or unused, allocate a new one */
505 if (!dce)
507 dce = DCE_AllocDCE( 0, DCE_CACHE_DC );
510 else
512 dce = wndPtr->dce;
513 if( dce->hwndCurrent == hwnd )
515 TRACE("\tskipping hVisRgn update\n");
516 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
518 /* Abey - 16Jul99. to take care of the nested GetDC. first one
519 with DCX_EXCLUDERGN or DCX_INTERSECTRGN flags and the next
520 one with or without these flags. */
522 if(dce->DCXflags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN))
524 /* This is likely to be a nested BeginPaint().
525 or a BeginPaint() followed by a GetDC()*/
527 if( dce->hClipRgn != hrgnClip )
529 FIXME("new hrgnClip[%04x] smashes the previous[%04x]\n",
530 hrgnClip, dce->hClipRgn );
531 DCE_DeleteClipRgn( dce );
533 else
534 RestoreVisRgn16(dce->hDC);
538 if (!dce)
540 hdc = 0;
541 goto END;
544 if (!(flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN))) hrgnClip = 0;
545 dce->hwndCurrent = hwnd;
546 dce->hClipRgn = hrgnClip;
547 dce->DCXflags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
548 DCX_CACHE | DCX_WINDOW | DCX_WINDOWPAINT |
549 DCX_KEEPCLIPRGN | DCX_INTERSECTRGN | DCX_EXCLUDERGN);
550 dce->DCXflags |= DCX_DCEBUSY;
551 dce->DCXflags &= ~DCX_DCEDIRTY;
552 hdc = dce->hDC;
554 if (bUpdateVisRgn) SetHookFlags16( hdc, DCHF_INVALIDATEVISRGN ); /* force update */
556 if (!USER_Driver.pGetDC( hwnd, hdc, hrgnClip, flags )) hdc = 0;
558 TRACE("(%04x,%04x,0x%lx): returning %04x\n", hwnd, hrgnClip, flags, hdc);
559 END:
560 WIN_ReleaseWndPtr(wndPtr);
561 return hdc;
565 /***********************************************************************
566 * GetDC (USER.66)
568 HDC16 WINAPI GetDC16( HWND16 hwnd )
570 return (HDC16)GetDC( hwnd );
574 /***********************************************************************
575 * GetDC (USER32.@)
576 * RETURNS
577 * :Handle to DC
578 * NULL: Failure
580 HDC WINAPI GetDC(
581 HWND hwnd /* [in] handle of window */
583 if (!hwnd)
584 return GetDCEx( GetDesktopWindow(), 0, DCX_CACHE | DCX_WINDOW );
585 return GetDCEx( hwnd, 0, DCX_USESTYLE );
589 /***********************************************************************
590 * GetWindowDC (USER.67)
592 HDC16 WINAPI GetWindowDC16( HWND16 hwnd )
594 if (!hwnd) hwnd = GetDesktopWindow16();
595 return GetDCEx16( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
599 /***********************************************************************
600 * GetWindowDC (USER32.@)
602 HDC WINAPI GetWindowDC( HWND hwnd )
604 if (!hwnd) hwnd = GetDesktopWindow();
605 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
609 /***********************************************************************
610 * ReleaseDC (USER.68)
612 INT16 WINAPI ReleaseDC16( HWND16 hwnd, HDC16 hdc )
614 return (INT16)ReleaseDC( hwnd, hdc );
618 /***********************************************************************
619 * ReleaseDC (USER32.@)
621 * RETURNS
622 * 1: Success
623 * 0: Failure
625 INT WINAPI ReleaseDC(
626 HWND hwnd /* [in] Handle of window - ignored */,
627 HDC hdc /* [in] Handle of device context */
629 DCE * dce;
630 INT nRet = 0;
632 WIN_LockWnds();
633 dce = firstDCE;
635 TRACE("%04x %04x\n", hwnd, hdc );
637 while (dce && (dce->hDC != hdc)) dce = dce->next;
639 if ( dce )
640 if ( dce->DCXflags & DCX_DCEBUSY )
641 nRet = DCE_ReleaseDC( dce );
643 WIN_UnlockWnds();
645 return nRet;
648 /***********************************************************************
649 * DCHook (USER.362)
651 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
653 BOOL16 WINAPI DCHook16( HDC16 hDC, WORD code, DWORD data, LPARAM lParam )
655 BOOL retv = TRUE;
656 DCE *dce = (DCE *)data;
658 TRACE("hDC = %04x, %i\n", hDC, code);
660 if (!dce) return 0;
661 assert(dce->hDC == hDC);
663 /* Grab the windows lock before doing anything else */
664 WIN_LockWnds();
666 switch( code )
668 case DCHC_INVALIDVISRGN:
669 /* GDI code calls this when it detects that the
670 * DC is dirty (usually after SetHookFlags()). This
671 * means that we have to recompute the visible region.
673 if( dce->DCXflags & DCX_DCEBUSY )
674 USER_Driver.pGetDC( dce->hwndCurrent, dce->hDC, dce->hClipRgn, dce->DCXflags );
675 else /* non-fatal but shouldn't happen */
676 WARN("DC is not in use!\n");
677 break;
679 case DCHC_DELETEDC:
681 * Windows will not let you delete a DC that is busy
682 * (between GetDC and ReleaseDC)
685 if ( dce->DCXflags & DCX_DCEBUSY )
687 WARN("Application trying to delete a busy DC\n");
688 retv = FALSE;
690 break;
692 default:
693 FIXME("unknown code\n");
696 WIN_UnlockWnds(); /* Release the wnd lock */
697 return retv;
701 /**********************************************************************
702 * WindowFromDC (USER.117)
704 HWND16 WINAPI WindowFromDC16( HDC16 hDC )
706 return (HWND16)WindowFromDC( hDC );
710 /**********************************************************************
711 * WindowFromDC (USER32.@)
713 HWND WINAPI WindowFromDC( HDC hDC )
715 DCE *dce;
716 HWND hwnd;
718 WIN_LockWnds();
719 dce = firstDCE;
721 while (dce && (dce->hDC != hDC)) dce = dce->next;
723 hwnd = dce ? dce->hwndCurrent : 0;
724 WIN_UnlockWnds();
726 return hwnd;
730 /***********************************************************************
731 * LockWindowUpdate (USER.294)
733 BOOL16 WINAPI LockWindowUpdate16( HWND16 hwnd )
735 return LockWindowUpdate( hwnd );
739 /***********************************************************************
740 * LockWindowUpdate (USER32.@)
742 BOOL WINAPI LockWindowUpdate( HWND hwnd )
744 FIXME("DCX_LOCKWINDOWUPDATE is unimplemented\n");
745 return TRUE;