Fixed window handle check in GetDCEx.
[wine/multimedia.git] / windows / dce.c
blob49d9ff4eb161e2c3972c1fa45daa62f62007e223
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 USER_Lock();
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 USER_Unlock();
64 /***********************************************************************
65 * DCE_AllocDCE
67 * Allocate a new DCE.
69 DCE *DCE_AllocDCE( HWND hWnd, DCE_TYPE type )
71 FARPROC16 hookProc;
72 DCE * dce;
74 if (!(dce = HeapAlloc( GetProcessHeap(), 0, sizeof(DCE) ))) return NULL;
75 if (!(dce->hDC = CreateDCA( "DISPLAY", NULL, NULL, NULL )))
77 HeapFree( GetProcessHeap(), 0, dce );
78 return 0;
80 if (!defaultDCstate) defaultDCstate = GetDCState16( dce->hDC );
82 /* store DCE handle in DC hook data field */
84 hookProc = GetProcAddress16( GetModuleHandle16("USER"), (LPCSTR)362 );
85 SetDCHook( dce->hDC, hookProc, (DWORD)dce );
87 dce->hwndCurrent = WIN_GetFullHandle( hWnd );
88 dce->hClipRgn = 0;
90 if( type != DCE_CACHE_DC ) /* owned or class DC */
92 dce->DCXflags = DCX_DCEBUSY;
93 if( hWnd )
95 LONG style = GetWindowLongW( hWnd, GWL_STYLE );
96 if (style & WS_CLIPCHILDREN) dce->DCXflags |= DCX_CLIPCHILDREN;
97 if (style & WS_CLIPSIBLINGS) dce->DCXflags |= DCX_CLIPSIBLINGS;
99 SetHookFlags16(dce->hDC,DCHF_INVALIDATEVISRGN);
101 else dce->DCXflags = DCX_CACHE | DCX_DCEEMPTY;
103 USER_Lock();
104 dce->next = firstDCE;
105 firstDCE = dce;
106 USER_Unlock();
107 return dce;
111 /***********************************************************************
112 * DCE_FreeDCE
114 DCE* DCE_FreeDCE( DCE *dce )
116 DCE **ppDCE, *ret;
118 if (!dce) return NULL;
120 USER_Lock();
122 ppDCE = &firstDCE;
124 while (*ppDCE && (*ppDCE != dce)) ppDCE = &(*ppDCE)->next;
125 if (*ppDCE == dce) *ppDCE = dce->next;
126 ret = *ppDCE;
127 USER_Unlock();
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 return ret;
139 /***********************************************************************
140 * DCE_FreeWindowDCE
142 * Remove owned DCE and reset unreleased cache DCEs.
144 void DCE_FreeWindowDCE( HWND hwnd )
146 DCE *pDCE;
147 WND *pWnd = WIN_GetPtr( hwnd );
149 pDCE = firstDCE;
150 while( pDCE )
152 if( pDCE->hwndCurrent == hwnd )
154 if( pDCE == pWnd->dce ) /* owned or Class DCE*/
156 if (pWnd->clsStyle & CS_OWNDC) /* owned DCE*/
158 pDCE = DCE_FreeDCE( pDCE );
159 pWnd->dce = NULL;
160 continue;
162 else if( pDCE->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN) ) /* Class DCE*/
164 DCE_DeleteClipRgn( pDCE );
165 pDCE->hwndCurrent = 0;
168 else
170 if( pDCE->DCXflags & DCX_DCEBUSY ) /* shared cache DCE */
172 /* FIXME: AFAICS we are doing the right thing here so
173 * this should be a WARN. But this is best left as an ERR
174 * because the 'application error' is likely to come from
175 * another part of Wine (i.e. it's our fault after all).
176 * We should change this to WARN when Wine is more stable
177 * (for 1.0?).
179 ERR("[%08x] GetDC() without ReleaseDC()!\n",hwnd);
180 DCE_ReleaseDC( pDCE );
183 pDCE->DCXflags &= DCX_CACHE;
184 pDCE->DCXflags |= DCX_DCEEMPTY;
185 pDCE->hwndCurrent = 0;
188 pDCE = pDCE->next;
190 WIN_ReleasePtr( pWnd );
194 /***********************************************************************
195 * DCE_DeleteClipRgn
197 static void DCE_DeleteClipRgn( DCE* dce )
199 dce->DCXflags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN | DCX_WINDOWPAINT);
201 if( dce->DCXflags & DCX_KEEPCLIPRGN )
202 dce->DCXflags &= ~DCX_KEEPCLIPRGN;
203 else
204 if( dce->hClipRgn > 1 )
205 DeleteObject( dce->hClipRgn );
207 dce->hClipRgn = 0;
209 /* make it dirty so that the vis rgn gets recomputed next time */
210 dce->DCXflags |= DCX_DCEDIRTY;
211 SetHookFlags16( dce->hDC, DCHF_INVALIDATEVISRGN );
215 /***********************************************************************
216 * DCE_ReleaseDC
218 static INT DCE_ReleaseDC( DCE* dce )
220 if ((dce->DCXflags & (DCX_DCEEMPTY | DCX_DCEBUSY)) != DCX_DCEBUSY) return 0;
222 /* restore previous visible region */
224 if ((dce->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
225 (dce->DCXflags & (DCX_CACHE | DCX_WINDOWPAINT)) )
226 DCE_DeleteClipRgn( dce );
228 if (dce->DCXflags & DCX_CACHE)
230 SetDCState16( dce->hDC, defaultDCstate );
231 dce->DCXflags &= ~DCX_DCEBUSY;
232 if (dce->DCXflags & DCX_DCEDIRTY)
234 /* don't keep around invalidated entries
235 * because SetDCState() disables hVisRgn updates
236 * by removing dirty bit. */
238 dce->hwndCurrent = 0;
239 dce->DCXflags &= DCX_CACHE;
240 dce->DCXflags |= DCX_DCEEMPTY;
243 return 1;
247 /***********************************************************************
248 * DCE_InvalidateDCE
250 * It is called from SetWindowPos() and EVENT_MapNotify - we have to
251 * mark as dirty all busy DCEs for windows that have pWnd->parent as
252 * an ancestor and whose client rect intersects with specified update
253 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
254 * DCX_CLIPCHILDREN flag is set. */
255 BOOL DCE_InvalidateDCE(HWND hwnd, const RECT* pRectUpdate)
257 HWND hwndScope = GetAncestor( hwnd, GA_PARENT );
258 BOOL bRet = FALSE;
260 if( hwndScope )
262 DCE *dce;
264 TRACE("scope hwnd = %04x, (%i,%i - %i,%i)\n",
265 hwndScope, pRectUpdate->left,pRectUpdate->top,
266 pRectUpdate->right,pRectUpdate->bottom);
267 if(TRACE_ON(dc))
268 DCE_DumpCache();
270 /* walk all DCEs and fixup non-empty entries */
272 for (dce = firstDCE; (dce); dce = dce->next)
274 if (dce->DCXflags & DCX_DCEEMPTY) continue;
275 if ((dce->hwndCurrent == hwndScope) && !(dce->DCXflags & DCX_CLIPCHILDREN))
276 continue; /* child window positions don't bother us */
278 /* check if DCE window is within the z-order scope */
280 if (hwndScope == dce->hwndCurrent || IsChild( hwndScope, dce->hwndCurrent ))
282 if (hwnd != dce->hwndCurrent)
284 /* check if the window rectangle intersects this DCE window */
285 RECT rect;
286 GetWindowRect( dce->hwndCurrent, &rect );
287 MapWindowPoints( 0, hwndScope, (POINT *)&rect, 2 );
288 if (!IntersectRect( &rect, &rect, pRectUpdate )) continue;
291 if( !(dce->DCXflags & DCX_DCEBUSY) )
293 /* Don't bother with visible regions of unused DCEs */
295 TRACE("\tpurged %p dce [%04x]\n", dce, dce->hwndCurrent);
296 dce->hwndCurrent = 0;
297 dce->DCXflags &= DCX_CACHE;
298 dce->DCXflags |= DCX_DCEEMPTY;
300 else
302 /* Set dirty bits in the hDC and DCE structs */
304 TRACE("\tfixed up %p dce [%04x]\n", dce, dce->hwndCurrent);
305 dce->DCXflags |= DCX_DCEDIRTY;
306 SetHookFlags16(dce->hDC, DCHF_INVALIDATEVISRGN);
307 bRet = TRUE;
310 } /* dce list */
312 return bRet;
316 /***********************************************************************
317 * DCE_ExcludeRgn
319 * Translate given region from the wnd client to the DC coordinates
320 * and add it to the clipping region.
322 INT DCE_ExcludeRgn( HDC hDC, HWND hwnd, HRGN hRgn )
324 POINT pt = {0, 0};
325 DCE *dce = firstDCE;
327 while (dce && (dce->hDC != hDC)) dce = dce->next;
328 if (!dce) return ERROR;
330 MapWindowPoints( hwnd, dce->hwndCurrent, &pt, 1);
331 if( dce->DCXflags & DCX_WINDOW )
333 WND *wnd = WIN_FindWndPtr(dce->hwndCurrent);
334 pt.x += wnd->rectClient.left - wnd->rectWindow.left;
335 pt.y += wnd->rectClient.top - wnd->rectWindow.top;
336 WIN_ReleaseWndPtr(wnd);
338 OffsetRgn(hRgn, pt.x, pt.y);
340 return ExtSelectClipRgn( hDC, hRgn, RGN_DIFF );
344 /***********************************************************************
345 * GetDCEx (USER32.@)
347 * Unimplemented flags: DCX_LOCKWINDOWUPDATE
349 * FIXME: Full support for hrgnClip == 1 (alias for entire window).
351 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
353 HDC hdc = 0;
354 DCE * dce;
355 WND * wndPtr;
356 DWORD dcxFlags = 0;
357 BOOL bUpdateVisRgn = TRUE;
358 BOOL bUpdateClipOrigin = FALSE;
359 HWND parent, full;
361 TRACE("hwnd %04x, hrgnClip %04x, flags %08x\n",
362 hwnd, hrgnClip, (unsigned)flags);
364 if (!hwnd) hwnd = GetDesktopWindow();
365 if (!(full = WIN_IsCurrentProcess( hwnd )))
367 FIXME( "not supported yet on other process window %x\n", hwnd );
368 return 0;
370 hwnd = full;
371 if (!(wndPtr = WIN_GetPtr( hwnd ))) return 0;
373 /* fixup flags */
375 if (flags & (DCX_WINDOW | DCX_PARENTCLIP)) flags |= DCX_CACHE;
377 if (flags & DCX_USESTYLE)
379 flags &= ~( DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
381 if( wndPtr->dwStyle & WS_CLIPSIBLINGS )
382 flags |= DCX_CLIPSIBLINGS;
384 if ( !(flags & DCX_WINDOW) )
386 if (wndPtr->clsStyle & CS_PARENTDC) flags |= DCX_PARENTCLIP;
388 if (wndPtr->dwStyle & WS_CLIPCHILDREN &&
389 !(wndPtr->dwStyle & WS_MINIMIZE) ) flags |= DCX_CLIPCHILDREN;
390 if (!wndPtr->dce) flags |= DCX_CACHE;
394 if (flags & DCX_WINDOW) flags &= ~DCX_CLIPCHILDREN;
396 parent = GetAncestor( hwnd, GA_PARENT );
397 if (!parent || (parent == GetDesktopWindow()))
398 flags = (flags & ~DCX_PARENTCLIP) | DCX_CLIPSIBLINGS;
400 /* it seems parent clip is ignored when clipping siblings or children */
401 if (flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) flags &= ~DCX_PARENTCLIP;
403 if( flags & DCX_PARENTCLIP )
405 LONG parent_style = GetWindowLongW( parent, GWL_STYLE );
406 if( (wndPtr->dwStyle & WS_VISIBLE) && (parent_style & WS_VISIBLE) )
408 flags &= ~DCX_CLIPCHILDREN;
409 if (parent_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
413 /* find a suitable DCE */
415 dcxFlags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
416 DCX_CACHE | DCX_WINDOW);
418 if (flags & DCX_CACHE)
420 DCE* dceEmpty;
421 DCE* dceUnused;
423 dceEmpty = dceUnused = NULL;
425 /* Strategy: First, we attempt to find a non-empty but unused DCE with
426 * compatible flags. Next, we look for an empty entry. If the cache is
427 * full we have to purge one of the unused entries.
430 for (dce = firstDCE; (dce); dce = dce->next)
432 if ((dce->DCXflags & (DCX_CACHE | DCX_DCEBUSY)) == DCX_CACHE )
434 dceUnused = dce;
436 if (dce->DCXflags & DCX_DCEEMPTY)
437 dceEmpty = dce;
438 else
439 if ((dce->hwndCurrent == hwnd) &&
440 ((dce->DCXflags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
441 DCX_CACHE | DCX_WINDOW | DCX_PARENTCLIP)) == dcxFlags))
443 TRACE("\tfound valid %08x dce [%04x], flags %08x\n",
444 (unsigned)dce, hwnd, (unsigned)dcxFlags );
445 bUpdateVisRgn = FALSE;
446 bUpdateClipOrigin = TRUE;
447 break;
452 if (!dce) dce = (dceEmpty) ? dceEmpty : dceUnused;
454 /* if there's no dce empty or unused, allocate a new one */
455 if (!dce)
457 dce = DCE_AllocDCE( 0, DCE_CACHE_DC );
460 else
462 dce = wndPtr->dce;
463 if (dce && dce->hwndCurrent == hwnd)
465 TRACE("\tskipping hVisRgn update\n");
466 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
469 if (!dce)
471 hdc = 0;
472 goto END;
475 if (!(flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN))) hrgnClip = 0;
477 if (((flags ^ dce->DCXflags) & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
478 (dce->hClipRgn != hrgnClip))
480 /* if the extra clip region has changed, get rid of the old one */
481 DCE_DeleteClipRgn( dce );
484 dce->hwndCurrent = hwnd;
485 dce->hClipRgn = hrgnClip;
486 dce->DCXflags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
487 DCX_CACHE | DCX_WINDOW | DCX_WINDOWPAINT |
488 DCX_KEEPCLIPRGN | DCX_INTERSECTRGN | DCX_EXCLUDERGN);
489 dce->DCXflags |= DCX_DCEBUSY;
490 dce->DCXflags &= ~DCX_DCEDIRTY;
491 hdc = dce->hDC;
493 if (bUpdateVisRgn) SetHookFlags16( hdc, DCHF_INVALIDATEVISRGN ); /* force update */
495 if (!USER_Driver.pGetDC( hwnd, hdc, hrgnClip, flags )) hdc = 0;
497 TRACE("(%04x,%04x,0x%lx): returning %04x\n", hwnd, hrgnClip, flags, hdc);
498 END:
499 WIN_ReleasePtr(wndPtr);
500 return hdc;
504 /***********************************************************************
505 * GetDC (USER32.@)
506 * RETURNS
507 * :Handle to DC
508 * NULL: Failure
510 HDC WINAPI GetDC(
511 HWND hwnd /* [in] handle of window */
513 if (!hwnd)
514 return GetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
515 return GetDCEx( hwnd, 0, DCX_USESTYLE );
519 /***********************************************************************
520 * GetWindowDC (USER32.@)
522 HDC WINAPI GetWindowDC( HWND hwnd )
524 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
528 /***********************************************************************
529 * ReleaseDC (USER32.@)
531 * RETURNS
532 * 1: Success
533 * 0: Failure
535 INT WINAPI ReleaseDC(
536 HWND hwnd /* [in] Handle of window - ignored */,
537 HDC hdc /* [in] Handle of device context */
539 DCE * dce;
540 INT nRet = 0;
542 USER_Lock();
543 dce = firstDCE;
545 TRACE("%04x %04x\n", hwnd, hdc );
547 while (dce && (dce->hDC != hdc)) dce = dce->next;
549 if ( dce )
550 if ( dce->DCXflags & DCX_DCEBUSY )
551 nRet = DCE_ReleaseDC( dce );
553 USER_Unlock();
555 return nRet;
558 /***********************************************************************
559 * DCHook (USER.362)
561 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
563 BOOL16 WINAPI DCHook16( HDC16 hDC, WORD code, DWORD data, LPARAM lParam )
565 BOOL retv = TRUE;
566 DCE *dce = (DCE *)data;
568 TRACE("hDC = %04x, %i\n", hDC, code);
570 if (!dce) return 0;
571 assert(dce->hDC == hDC);
573 /* Grab the windows lock before doing anything else */
574 USER_Lock();
576 switch( code )
578 case DCHC_INVALIDVISRGN:
579 /* GDI code calls this when it detects that the
580 * DC is dirty (usually after SetHookFlags()). This
581 * means that we have to recompute the visible region.
583 if( dce->DCXflags & DCX_DCEBUSY )
585 /* Dirty bit has been cleared by caller, set it again so that
586 * pGetDC recomputes the visible region. */
587 SetHookFlags16( dce->hDC, DCHF_INVALIDATEVISRGN );
588 USER_Driver.pGetDC( dce->hwndCurrent, dce->hDC, dce->hClipRgn, dce->DCXflags );
590 else /* non-fatal but shouldn't happen */
591 WARN("DC is not in use!\n");
592 break;
594 case DCHC_DELETEDC:
596 * Windows will not let you delete a DC that is busy
597 * (between GetDC and ReleaseDC)
600 if ( dce->DCXflags & DCX_DCEBUSY )
602 WARN("Application trying to delete a busy DC\n");
603 retv = FALSE;
605 else DCE_FreeDCE( dce );
606 break;
608 default:
609 FIXME("unknown code\n");
612 USER_Unlock(); /* Release the wnd lock */
613 return retv;
617 /**********************************************************************
618 * WindowFromDC (USER32.@)
620 HWND WINAPI WindowFromDC( HDC hDC )
622 DCE *dce;
623 HWND hwnd;
625 USER_Lock();
626 dce = firstDCE;
628 while (dce && (dce->hDC != hDC)) dce = dce->next;
630 hwnd = dce ? dce->hwndCurrent : 0;
631 USER_Unlock();
633 return hwnd;
637 /***********************************************************************
638 * LockWindowUpdate (USER32.@)
640 BOOL WINAPI LockWindowUpdate( HWND hwnd )
642 FIXME("(%x), stub!\n",hwnd);
643 return TRUE;