Fix some types and function prototypes according to Platform SDK
[wine/multimedia.git] / windows / dce.c
blobdb82f9ae71fe6cc8b0ae0625a9ac4c64ed302261
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_KEEPCLIPRGN - ReleaseDC() should not delete the clipping region
31 * DCX_WINDOWPAINT - BeginPaint() is in effect
34 #include <assert.h>
35 #include "dce.h"
36 #include "win.h"
37 #include "gdi.h"
38 #include "user.h"
39 #include "wine/debug.h"
40 #include "windef.h"
41 #include "wingdi.h"
42 #include "wine/winbase16.h"
43 #include "wine/winuser16.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(dc);
47 static DCE *firstDCE;
48 static HDC defaultDCstate;
50 static void DCE_DeleteClipRgn( DCE* );
51 static INT DCE_ReleaseDC( DCE* );
54 /***********************************************************************
55 * DCE_DumpCache
57 static void DCE_DumpCache(void)
59 DCE *dce;
61 USER_Lock();
62 dce = firstDCE;
64 DPRINTF("DCE:\n");
65 while( dce )
67 DPRINTF("\t[0x%08x] hWnd 0x%04x, dcx %08x, %s %s\n",
68 (unsigned)dce, dce->hwndCurrent, (unsigned)dce->DCXflags,
69 (dce->DCXflags & DCX_CACHE) ? "Cache" : "Owned",
70 (dce->DCXflags & DCX_DCEBUSY) ? "InUse" : "" );
71 dce = dce->next;
74 USER_Unlock();
77 /***********************************************************************
78 * DCE_AllocDCE
80 * Allocate a new DCE.
82 DCE *DCE_AllocDCE( HWND hWnd, DCE_TYPE type )
84 DCE * dce;
86 if (!(dce = HeapAlloc( GetProcessHeap(), 0, sizeof(DCE) ))) return NULL;
87 if (!(dce->hDC = CreateDCA( "DISPLAY", NULL, NULL, NULL )))
89 HeapFree( GetProcessHeap(), 0, dce );
90 return 0;
92 if (!defaultDCstate) defaultDCstate = GetDCState16( dce->hDC );
94 /* store DCE handle in DC hook data field */
96 SetDCHook( dce->hDC, DCHook16, (DWORD)dce );
98 dce->hwndCurrent = WIN_GetFullHandle( hWnd );
99 dce->hClipRgn = 0;
101 if( type != DCE_CACHE_DC ) /* owned or class DC */
103 dce->DCXflags = DCX_DCEBUSY;
104 if( hWnd )
106 LONG style = GetWindowLongW( hWnd, GWL_STYLE );
107 if (style & WS_CLIPCHILDREN) dce->DCXflags |= DCX_CLIPCHILDREN;
108 if (style & WS_CLIPSIBLINGS) dce->DCXflags |= DCX_CLIPSIBLINGS;
110 SetHookFlags16(dce->hDC,DCHF_INVALIDATEVISRGN);
112 else dce->DCXflags = DCX_CACHE | DCX_DCEEMPTY;
114 USER_Lock();
115 dce->next = firstDCE;
116 firstDCE = dce;
117 USER_Unlock();
118 return dce;
122 /***********************************************************************
123 * DCE_FreeDCE
125 DCE* DCE_FreeDCE( DCE *dce )
127 DCE **ppDCE, *ret;
129 if (!dce) return NULL;
131 USER_Lock();
133 ppDCE = &firstDCE;
135 while (*ppDCE && (*ppDCE != dce)) ppDCE = &(*ppDCE)->next;
136 if (*ppDCE == dce) *ppDCE = dce->next;
137 ret = *ppDCE;
138 USER_Unlock();
140 SetDCHook(dce->hDC, NULL, 0L);
142 DeleteDC( dce->hDC );
143 if( dce->hClipRgn && !(dce->DCXflags & DCX_KEEPCLIPRGN) )
144 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 DCE_DeleteClipRgn( pDCE );
176 pDCE->hwndCurrent = 0;
179 else
181 if( pDCE->DCXflags & DCX_DCEBUSY ) /* shared cache DCE */
183 /* FIXME: AFAICS we are doing the right thing here so
184 * this should be a WARN. But this is best left as an ERR
185 * because the 'application error' is likely to come from
186 * another part of Wine (i.e. it's our fault after all).
187 * We should change this to WARN when Wine is more stable
188 * (for 1.0?).
190 ERR("[%08x] GetDC() without ReleaseDC()!\n",hwnd);
191 DCE_ReleaseDC( pDCE );
194 pDCE->DCXflags &= DCX_CACHE;
195 pDCE->DCXflags |= DCX_DCEEMPTY;
196 pDCE->hwndCurrent = 0;
199 pDCE = pDCE->next;
201 WIN_ReleasePtr( pWnd );
205 /***********************************************************************
206 * DCE_DeleteClipRgn
208 static void DCE_DeleteClipRgn( DCE* dce )
210 dce->DCXflags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN | DCX_WINDOWPAINT);
212 if( dce->DCXflags & DCX_KEEPCLIPRGN )
213 dce->DCXflags &= ~DCX_KEEPCLIPRGN;
214 else
215 if( dce->hClipRgn > 1 )
216 DeleteObject( dce->hClipRgn );
218 dce->hClipRgn = 0;
220 /* make it dirty so that the vis rgn gets recomputed next time */
221 dce->DCXflags |= DCX_DCEDIRTY;
222 SetHookFlags16( dce->hDC, DCHF_INVALIDATEVISRGN );
226 /***********************************************************************
227 * DCE_ReleaseDC
229 static INT DCE_ReleaseDC( DCE* dce )
231 if ((dce->DCXflags & (DCX_DCEEMPTY | DCX_DCEBUSY)) != DCX_DCEBUSY) return 0;
233 /* restore previous visible region */
235 if ((dce->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
236 (dce->DCXflags & (DCX_CACHE | DCX_WINDOWPAINT)) )
237 DCE_DeleteClipRgn( dce );
239 if (dce->DCXflags & DCX_CACHE)
241 /* make the DC clean so that SetDCState doesn't try to update the vis rgn */
242 SetHookFlags16( dce->hDC, DCHF_VALIDATEVISRGN );
243 SetDCState16( dce->hDC, defaultDCstate );
244 dce->DCXflags &= ~DCX_DCEBUSY;
245 if (dce->DCXflags & DCX_DCEDIRTY)
247 /* don't keep around invalidated entries
248 * because SetDCState() disables hVisRgn updates
249 * by removing dirty bit. */
251 dce->hwndCurrent = 0;
252 dce->DCXflags &= DCX_CACHE;
253 dce->DCXflags |= DCX_DCEEMPTY;
256 return 1;
260 /***********************************************************************
261 * DCE_InvalidateDCE
263 * It is called from SetWindowPos() and EVENT_MapNotify - we have to
264 * mark as dirty all busy DCEs for windows that have pWnd->parent as
265 * an ancestor and whose client rect intersects with specified update
266 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
267 * DCX_CLIPCHILDREN flag is set. */
268 BOOL DCE_InvalidateDCE(HWND hwnd, const RECT* pRectUpdate)
270 HWND hwndScope = GetAncestor( hwnd, GA_PARENT );
271 BOOL bRet = FALSE;
273 if( hwndScope )
275 DCE *dce;
277 TRACE("scope hwnd = %04x, (%i,%i - %i,%i)\n",
278 hwndScope, pRectUpdate->left,pRectUpdate->top,
279 pRectUpdate->right,pRectUpdate->bottom);
280 if(TRACE_ON(dc))
281 DCE_DumpCache();
283 /* walk all DCEs and fixup non-empty entries */
285 for (dce = firstDCE; (dce); dce = dce->next)
287 if (dce->DCXflags & DCX_DCEEMPTY) continue;
288 if ((dce->hwndCurrent == hwndScope) && !(dce->DCXflags & DCX_CLIPCHILDREN))
289 continue; /* child window positions don't bother us */
291 /* check if DCE window is within the z-order scope */
293 if (hwndScope == dce->hwndCurrent || IsChild( hwndScope, dce->hwndCurrent ))
295 if (hwnd != dce->hwndCurrent)
297 /* check if the window rectangle intersects this DCE window */
298 RECT rect;
299 GetWindowRect( dce->hwndCurrent, &rect );
300 MapWindowPoints( 0, hwndScope, (POINT *)&rect, 2 );
301 if (!IntersectRect( &rect, &rect, pRectUpdate )) continue;
304 if( !(dce->DCXflags & DCX_DCEBUSY) )
306 /* Don't bother with visible regions of unused DCEs */
308 TRACE("\tpurged %p dce [%04x]\n", dce, dce->hwndCurrent);
309 dce->hwndCurrent = 0;
310 dce->DCXflags &= DCX_CACHE;
311 dce->DCXflags |= DCX_DCEEMPTY;
313 else
315 /* Set dirty bits in the hDC and DCE structs */
317 TRACE("\tfixed up %p dce [%04x]\n", dce, dce->hwndCurrent);
318 dce->DCXflags |= DCX_DCEDIRTY;
319 SetHookFlags16(dce->hDC, DCHF_INVALIDATEVISRGN);
320 bRet = TRUE;
323 } /* dce list */
325 return bRet;
329 /***********************************************************************
330 * DCE_ExcludeRgn
332 * Translate given region from the wnd client to the DC coordinates
333 * and add it to the clipping region.
335 INT DCE_ExcludeRgn( HDC hDC, HWND hwnd, HRGN hRgn )
337 POINT pt = {0, 0};
338 DCE *dce = firstDCE;
340 while (dce && (dce->hDC != hDC)) dce = dce->next;
341 if (!dce) return ERROR;
343 MapWindowPoints( hwnd, dce->hwndCurrent, &pt, 1);
344 if( dce->DCXflags & DCX_WINDOW )
346 WND *wnd = WIN_FindWndPtr(dce->hwndCurrent);
347 pt.x += wnd->rectClient.left - wnd->rectWindow.left;
348 pt.y += wnd->rectClient.top - wnd->rectWindow.top;
349 WIN_ReleaseWndPtr(wnd);
351 OffsetRgn(hRgn, pt.x, pt.y);
353 return ExtSelectClipRgn( hDC, hRgn, RGN_DIFF );
357 /***********************************************************************
358 * GetDCEx (USER32.@)
360 * Unimplemented flags: DCX_LOCKWINDOWUPDATE
362 * FIXME: Full support for hrgnClip == 1 (alias for entire window).
364 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
366 HDC hdc = 0;
367 DCE * dce;
368 WND * wndPtr;
369 DWORD dcxFlags = 0;
370 BOOL bUpdateVisRgn = TRUE;
371 BOOL bUpdateClipOrigin = FALSE;
372 HWND parent, full;
374 TRACE("hwnd %04x, hrgnClip %04x, flags %08x\n",
375 hwnd, hrgnClip, (unsigned)flags);
377 if (!hwnd) hwnd = GetDesktopWindow();
378 if (!(full = WIN_IsCurrentProcess( hwnd )))
380 FIXME( "not supported yet on other process window %x\n", hwnd );
381 return 0;
383 hwnd = full;
384 if (!(wndPtr = WIN_GetPtr( hwnd ))) return 0;
386 /* fixup flags */
388 if (flags & (DCX_WINDOW | DCX_PARENTCLIP)) flags |= DCX_CACHE;
390 if (flags & DCX_USESTYLE)
392 flags &= ~( DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
394 if( wndPtr->dwStyle & WS_CLIPSIBLINGS )
395 flags |= DCX_CLIPSIBLINGS;
397 if ( !(flags & DCX_WINDOW) )
399 if (wndPtr->clsStyle & CS_PARENTDC) flags |= DCX_PARENTCLIP;
401 if (wndPtr->dwStyle & WS_CLIPCHILDREN &&
402 !(wndPtr->dwStyle & WS_MINIMIZE) ) flags |= DCX_CLIPCHILDREN;
403 if (!wndPtr->dce) flags |= DCX_CACHE;
407 if (flags & DCX_WINDOW) flags &= ~DCX_CLIPCHILDREN;
409 parent = GetAncestor( hwnd, GA_PARENT );
410 if (!parent || (parent == GetDesktopWindow()))
411 flags = (flags & ~DCX_PARENTCLIP) | DCX_CLIPSIBLINGS;
413 /* it seems parent clip is ignored when clipping siblings or children */
414 if (flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) flags &= ~DCX_PARENTCLIP;
416 if( flags & DCX_PARENTCLIP )
418 LONG parent_style = GetWindowLongW( parent, GWL_STYLE );
419 if( (wndPtr->dwStyle & WS_VISIBLE) && (parent_style & WS_VISIBLE) )
421 flags &= ~DCX_CLIPCHILDREN;
422 if (parent_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
426 /* find a suitable DCE */
428 dcxFlags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
429 DCX_CACHE | DCX_WINDOW);
431 if (flags & DCX_CACHE)
433 DCE* dceEmpty;
434 DCE* dceUnused;
436 dceEmpty = dceUnused = NULL;
438 /* Strategy: First, we attempt to find a non-empty but unused DCE with
439 * compatible flags. Next, we look for an empty entry. If the cache is
440 * full we have to purge one of the unused entries.
443 for (dce = firstDCE; (dce); dce = dce->next)
445 if ((dce->DCXflags & (DCX_CACHE | DCX_DCEBUSY)) == DCX_CACHE )
447 dceUnused = dce;
449 if (dce->DCXflags & DCX_DCEEMPTY)
450 dceEmpty = dce;
451 else
452 if ((dce->hwndCurrent == hwnd) &&
453 ((dce->DCXflags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
454 DCX_CACHE | DCX_WINDOW | DCX_PARENTCLIP)) == dcxFlags))
456 TRACE("\tfound valid %08x dce [%04x], flags %08x\n",
457 (unsigned)dce, hwnd, (unsigned)dcxFlags );
458 bUpdateVisRgn = FALSE;
459 bUpdateClipOrigin = TRUE;
460 break;
465 if (!dce) dce = (dceEmpty) ? dceEmpty : dceUnused;
467 /* if there's no dce empty or unused, allocate a new one */
468 if (!dce)
470 dce = DCE_AllocDCE( 0, DCE_CACHE_DC );
473 else
475 dce = wndPtr->dce;
476 if (dce && dce->hwndCurrent == hwnd)
478 TRACE("\tskipping hVisRgn update\n");
479 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
482 if (!dce)
484 hdc = 0;
485 goto END;
488 if (!(flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN))) hrgnClip = 0;
490 if (((flags ^ dce->DCXflags) & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
491 (dce->hClipRgn != hrgnClip))
493 /* if the extra clip region has changed, get rid of the old one */
494 DCE_DeleteClipRgn( dce );
497 dce->hwndCurrent = hwnd;
498 dce->hClipRgn = hrgnClip;
499 dce->DCXflags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
500 DCX_CACHE | DCX_WINDOW | DCX_WINDOWPAINT |
501 DCX_KEEPCLIPRGN | DCX_INTERSECTRGN | DCX_EXCLUDERGN);
502 dce->DCXflags |= DCX_DCEBUSY;
503 dce->DCXflags &= ~DCX_DCEDIRTY;
504 hdc = dce->hDC;
506 if (bUpdateVisRgn) SetHookFlags16( hdc, DCHF_INVALIDATEVISRGN ); /* force update */
508 if (!USER_Driver.pGetDC( hwnd, hdc, hrgnClip, flags )) hdc = 0;
510 TRACE("(%04x,%04x,0x%lx): returning %04x\n", hwnd, hrgnClip, flags, hdc);
511 END:
512 WIN_ReleasePtr(wndPtr);
513 return hdc;
517 /***********************************************************************
518 * GetDC (USER32.@)
519 * RETURNS
520 * :Handle to DC
521 * NULL: Failure
523 HDC WINAPI GetDC(
524 HWND hwnd /* [in] handle of window */
526 if (!hwnd)
527 return GetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
528 return GetDCEx( hwnd, 0, DCX_USESTYLE );
532 /***********************************************************************
533 * GetWindowDC (USER32.@)
535 HDC WINAPI GetWindowDC( HWND hwnd )
537 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
541 /***********************************************************************
542 * ReleaseDC (USER32.@)
544 * RETURNS
545 * 1: Success
546 * 0: Failure
548 INT WINAPI ReleaseDC(
549 HWND hwnd /* [in] Handle of window - ignored */,
550 HDC hdc /* [in] Handle of device context */
552 DCE * dce;
553 INT nRet = 0;
555 USER_Lock();
556 dce = firstDCE;
558 TRACE("%04x %04x\n", hwnd, hdc );
560 while (dce && (dce->hDC != hdc)) dce = dce->next;
562 if ( dce )
563 if ( dce->DCXflags & DCX_DCEBUSY )
564 nRet = DCE_ReleaseDC( dce );
566 USER_Unlock();
568 return nRet;
571 /***********************************************************************
572 * DCHook (USER.362)
574 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
576 BOOL16 WINAPI DCHook16( HDC16 hDC, WORD code, DWORD data, LPARAM lParam )
578 BOOL retv = TRUE;
579 DCE *dce = (DCE *)data;
581 TRACE("hDC = %04x, %i\n", hDC, code);
583 if (!dce) return 0;
584 assert(dce->hDC == hDC);
586 /* Grab the windows lock before doing anything else */
587 USER_Lock();
589 switch( code )
591 case DCHC_INVALIDVISRGN:
592 /* GDI code calls this when it detects that the
593 * DC is dirty (usually after SetHookFlags()). This
594 * means that we have to recompute the visible region.
596 if( dce->DCXflags & DCX_DCEBUSY )
598 /* Dirty bit has been cleared by caller, set it again so that
599 * pGetDC recomputes the visible region. */
600 SetHookFlags16( dce->hDC, DCHF_INVALIDATEVISRGN );
601 USER_Driver.pGetDC( dce->hwndCurrent, dce->hDC, dce->hClipRgn, dce->DCXflags );
603 else /* non-fatal but shouldn't happen */
604 WARN("DC is not in use!\n");
605 break;
607 case DCHC_DELETEDC:
609 * Windows will not let you delete a DC that is busy
610 * (between GetDC and ReleaseDC)
613 if ( dce->DCXflags & DCX_DCEBUSY )
615 WARN("Application trying to delete a busy DC\n");
616 retv = FALSE;
618 else DCE_FreeDCE( dce );
619 break;
621 default:
622 FIXME("unknown code\n");
625 USER_Unlock(); /* Release the wnd lock */
626 return retv;
630 /**********************************************************************
631 * WindowFromDC (USER32.@)
633 HWND WINAPI WindowFromDC( HDC hDC )
635 DCE *dce;
636 HWND hwnd;
638 USER_Lock();
639 dce = firstDCE;
641 while (dce && (dce->hDC != hDC)) dce = dce->next;
643 hwnd = dce ? dce->hwndCurrent : 0;
644 USER_Unlock();
646 return hwnd;
650 /***********************************************************************
651 * LockWindowUpdate (USER32.@)
653 BOOL WINAPI LockWindowUpdate( HWND hwnd )
655 static HWND lockedWnd;
657 FIXME("(%x), partial stub!\n",hwnd);
659 USER_Lock();
660 if (lockedWnd)
662 if (!hwnd)
664 /* Unlock lockedWnd */
665 /* FIXME: Do something */
667 else
669 /* Attempted to lock a second window */
670 /* Return FALSE and do nothing */
671 USER_Unlock();
672 return FALSE;
675 lockedWnd = hwnd;
676 USER_Unlock();
677 return TRUE;