Added some missing GUIDs.
[wine/testsucceed.git] / windows / dce.c
blobd20645876cdb3b8096f7641eef182265122cdb9a
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;
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 USER_Lock();
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 USER_Unlock();
138 return *ppDCE;
141 /***********************************************************************
142 * DCE_FreeWindowDCE
144 * Remove owned DCE and reset unreleased cache DCEs.
146 void DCE_FreeWindowDCE( HWND hwnd )
148 DCE *pDCE;
150 USER_Lock();
151 pDCE = firstDCE;
153 while( pDCE )
155 if( pDCE->hwndCurrent == hwnd )
157 WND *pWnd = WIN_FindWndPtr( hwnd );
158 if( pDCE == pWnd->dce ) /* owned or Class DCE*/
160 if (pWnd->clsStyle & CS_OWNDC) /* owned DCE*/
162 pDCE = DCE_FreeDCE( pDCE );
163 pWnd->dce = NULL;
164 WIN_ReleaseWndPtr( pWnd );
165 continue;
167 else if( pDCE->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN) ) /* Class DCE*/
169 DCE_DeleteClipRgn( pDCE );
170 pDCE->hwndCurrent = 0;
173 else
175 if( pDCE->DCXflags & DCX_DCEBUSY ) /* shared cache DCE */
177 /* FIXME: AFAICS we are doing the right thing here so
178 * this should be a WARN. But this is best left as an ERR
179 * because the 'application error' is likely to come from
180 * another part of Wine (i.e. it's our fault after all).
181 * We should change this to WARN when Wine is more stable
182 * (for 1.0?).
184 ERR("[%04x] GetDC() without ReleaseDC()!\n",
185 pWnd->hwndSelf);
186 DCE_ReleaseDC( pDCE );
189 pDCE->DCXflags &= DCX_CACHE;
190 pDCE->DCXflags |= DCX_DCEEMPTY;
191 pDCE->hwndCurrent = 0;
193 WIN_ReleaseWndPtr( pWnd );
195 pDCE = pDCE->next;
198 USER_Unlock();
202 /***********************************************************************
203 * DCE_DeleteClipRgn
205 static void DCE_DeleteClipRgn( DCE* dce )
207 dce->DCXflags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN | DCX_WINDOWPAINT);
209 if( dce->DCXflags & DCX_KEEPCLIPRGN )
210 dce->DCXflags &= ~DCX_KEEPCLIPRGN;
211 else
212 if( dce->hClipRgn > 1 )
213 DeleteObject( dce->hClipRgn );
215 dce->hClipRgn = 0;
217 /* make it dirty so that the vis rgn gets recomputed next time */
218 dce->DCXflags |= DCX_DCEDIRTY;
219 SetHookFlags16( dce->hDC, DCHF_INVALIDATEVISRGN );
223 /***********************************************************************
224 * DCE_ReleaseDC
226 static INT DCE_ReleaseDC( DCE* dce )
228 if ((dce->DCXflags & (DCX_DCEEMPTY | DCX_DCEBUSY)) != DCX_DCEBUSY) return 0;
230 /* restore previous visible region */
232 if ((dce->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
233 (dce->DCXflags & (DCX_CACHE | DCX_WINDOWPAINT)) )
234 DCE_DeleteClipRgn( dce );
236 if (dce->DCXflags & DCX_CACHE)
238 SetDCState16( dce->hDC, defaultDCstate );
239 dce->DCXflags &= ~DCX_DCEBUSY;
240 if (dce->DCXflags & DCX_DCEDIRTY)
242 /* don't keep around invalidated entries
243 * because SetDCState() disables hVisRgn updates
244 * by removing dirty bit. */
246 dce->hwndCurrent = 0;
247 dce->DCXflags &= DCX_CACHE;
248 dce->DCXflags |= DCX_DCEEMPTY;
251 return 1;
255 /***********************************************************************
256 * DCE_InvalidateDCE
258 * It is called from SetWindowPos() and EVENT_MapNotify - we have to
259 * mark as dirty all busy DCEs for windows that have pWnd->parent as
260 * an ancestor and whose client rect intersects with specified update
261 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
262 * DCX_CLIPCHILDREN flag is set. */
263 BOOL DCE_InvalidateDCE(HWND hwnd, const RECT* pRectUpdate)
265 HWND hwndScope = GetAncestor( hwnd, GA_PARENT );
266 BOOL bRet = FALSE;
268 if( hwndScope )
270 DCE *dce;
272 TRACE("scope hwnd = %04x, (%i,%i - %i,%i)\n",
273 hwndScope, pRectUpdate->left,pRectUpdate->top,
274 pRectUpdate->right,pRectUpdate->bottom);
275 if(TRACE_ON(dc))
276 DCE_DumpCache();
278 /* walk all DCEs and fixup non-empty entries */
280 for (dce = firstDCE; (dce); dce = dce->next)
282 WND* wndCurrent;
283 HWND tmp;
284 INT xoffset = 0, yoffset = 0;
286 if (dce->DCXflags & DCX_DCEEMPTY) continue;
287 if ((dce->hwndCurrent == hwndScope) && !(dce->DCXflags & DCX_CLIPCHILDREN))
288 continue; /* child window positions don't bother us */
289 if (!(wndCurrent = WIN_FindWndPtr(dce->hwndCurrent))) continue;
291 /* check if DCE window is within the z-order scope */
293 for (tmp = dce->hwndCurrent; tmp; tmp = GetAncestor( tmp, GA_PARENT ))
295 if (tmp == hwndScope )
297 RECT wndRect;
299 wndRect = wndCurrent->rectWindow;
301 OffsetRect( &wndRect, xoffset - wndCurrent->rectClient.left,
302 yoffset - wndCurrent->rectClient.top);
304 if (hwnd == wndCurrent->hwndSelf ||
305 IntersectRect( &wndRect, &wndRect, pRectUpdate ))
307 if( !(dce->DCXflags & DCX_DCEBUSY) )
309 /* Don't bother with visible regions of unused DCEs */
311 TRACE("\tpurged %08x dce [%04x]\n",
312 (unsigned)dce, wndCurrent->hwndSelf);
314 dce->hwndCurrent = 0;
315 dce->DCXflags &= DCX_CACHE;
316 dce->DCXflags |= DCX_DCEEMPTY;
318 else
320 /* Set dirty bits in the hDC and DCE structs */
322 TRACE("\tfixed up %08x dce [%04x]\n",
323 (unsigned)dce, wndCurrent->hwndSelf);
325 dce->DCXflags |= DCX_DCEDIRTY;
326 SetHookFlags16(dce->hDC, DCHF_INVALIDATEVISRGN);
327 bRet = TRUE;
330 break;
332 else
334 WND* wnd = WIN_FindWndPtr( tmp );
335 xoffset += wnd->rectClient.left;
336 yoffset += wnd->rectClient.top;
337 WIN_ReleaseWndPtr( wnd );
340 WIN_ReleaseWndPtr(wndCurrent);
341 } /* dce list */
343 return bRet;
347 /***********************************************************************
348 * DCE_ExcludeRgn
350 * Translate given region from the wnd client to the DC coordinates
351 * and add it to the clipping region.
353 INT DCE_ExcludeRgn( HDC hDC, HWND hwnd, HRGN hRgn )
355 POINT pt = {0, 0};
356 DCE *dce = firstDCE;
358 while (dce && (dce->hDC != hDC)) dce = dce->next;
359 if (!dce) return ERROR;
361 MapWindowPoints( hwnd, dce->hwndCurrent, &pt, 1);
362 if( dce->DCXflags & DCX_WINDOW )
364 WND *wnd = WIN_FindWndPtr(dce->hwndCurrent);
365 pt.x += wnd->rectClient.left - wnd->rectWindow.left;
366 pt.y += wnd->rectClient.top - wnd->rectWindow.top;
367 WIN_ReleaseWndPtr(wnd);
369 OffsetRgn(hRgn, pt.x, pt.y);
371 return ExtSelectClipRgn( hDC, hRgn, RGN_DIFF );
375 /***********************************************************************
376 * GetDCEx (USER.359)
378 HDC16 WINAPI GetDCEx16( HWND16 hwnd, HRGN16 hrgnClip, DWORD flags )
380 return (HDC16)GetDCEx( hwnd, hrgnClip, flags );
384 /***********************************************************************
385 * GetDCEx (USER32.@)
387 * Unimplemented flags: DCX_LOCKWINDOWUPDATE
389 * FIXME: Full support for hrgnClip == 1 (alias for entire window).
391 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
393 HDC hdc = 0;
394 DCE * dce;
395 WND * wndPtr;
396 DWORD dcxFlags = 0;
397 BOOL bUpdateVisRgn = TRUE;
398 BOOL bUpdateClipOrigin = FALSE;
399 HWND parent;
401 TRACE("hwnd %04x, hrgnClip %04x, flags %08x\n",
402 hwnd, hrgnClip, (unsigned)flags);
404 if (!hwnd) hwnd = GetDesktopWindow();
405 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
407 /* fixup flags */
409 if (flags & (DCX_WINDOW | DCX_PARENTCLIP)) flags |= DCX_CACHE;
411 if (flags & DCX_USESTYLE)
413 flags &= ~( DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
415 if( wndPtr->dwStyle & WS_CLIPSIBLINGS )
416 flags |= DCX_CLIPSIBLINGS;
418 if ( !(flags & DCX_WINDOW) )
420 if (wndPtr->clsStyle & CS_PARENTDC) flags |= DCX_PARENTCLIP;
422 if (wndPtr->dwStyle & WS_CLIPCHILDREN &&
423 !(wndPtr->dwStyle & WS_MINIMIZE) ) flags |= DCX_CLIPCHILDREN;
424 if (!wndPtr->dce) flags |= DCX_CACHE;
428 if (flags & DCX_WINDOW) flags &= ~DCX_CLIPCHILDREN;
430 parent = GetAncestor( hwnd, GA_PARENT );
431 if (!parent || (parent == GetDesktopWindow()))
432 flags = (flags & ~DCX_PARENTCLIP) | DCX_CLIPSIBLINGS;
434 /* it seems parent clip is ignored when clipping siblings or children */
435 if (flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) flags &= ~DCX_PARENTCLIP;
437 if( flags & DCX_PARENTCLIP )
439 LONG parent_style = GetWindowLongW( parent, GWL_STYLE );
440 if( (wndPtr->dwStyle & WS_VISIBLE) && (parent_style & WS_VISIBLE) )
442 flags &= ~DCX_CLIPCHILDREN;
443 if (parent_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
447 /* find a suitable DCE */
449 dcxFlags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
450 DCX_CACHE | DCX_WINDOW);
452 if (flags & DCX_CACHE)
454 DCE* dceEmpty;
455 DCE* dceUnused;
457 dceEmpty = dceUnused = NULL;
459 /* Strategy: First, we attempt to find a non-empty but unused DCE with
460 * compatible flags. Next, we look for an empty entry. If the cache is
461 * full we have to purge one of the unused entries.
464 for (dce = firstDCE; (dce); dce = dce->next)
466 if ((dce->DCXflags & (DCX_CACHE | DCX_DCEBUSY)) == DCX_CACHE )
468 dceUnused = dce;
470 if (dce->DCXflags & DCX_DCEEMPTY)
471 dceEmpty = dce;
472 else
473 if ((dce->hwndCurrent == hwnd) &&
474 ((dce->DCXflags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
475 DCX_CACHE | DCX_WINDOW | DCX_PARENTCLIP)) == dcxFlags))
477 TRACE("\tfound valid %08x dce [%04x], flags %08x\n",
478 (unsigned)dce, hwnd, (unsigned)dcxFlags );
479 bUpdateVisRgn = FALSE;
480 bUpdateClipOrigin = TRUE;
481 break;
486 if (!dce) dce = (dceEmpty) ? dceEmpty : dceUnused;
488 /* if there's no dce empty or unused, allocate a new one */
489 if (!dce)
491 dce = DCE_AllocDCE( 0, DCE_CACHE_DC );
494 else
496 dce = wndPtr->dce;
497 if (dce && dce->hwndCurrent == hwnd)
499 TRACE("\tskipping hVisRgn update\n");
500 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
503 if (!dce)
505 hdc = 0;
506 goto END;
509 if (!(flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN))) hrgnClip = 0;
511 if (((flags ^ dce->DCXflags) & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
512 (dce->hClipRgn != hrgnClip))
514 /* if the extra clip region has changed, get rid of the old one */
515 DCE_DeleteClipRgn( dce );
518 dce->hwndCurrent = hwnd;
519 dce->hClipRgn = hrgnClip;
520 dce->DCXflags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
521 DCX_CACHE | DCX_WINDOW | DCX_WINDOWPAINT |
522 DCX_KEEPCLIPRGN | DCX_INTERSECTRGN | DCX_EXCLUDERGN);
523 dce->DCXflags |= DCX_DCEBUSY;
524 dce->DCXflags &= ~DCX_DCEDIRTY;
525 hdc = dce->hDC;
527 if (bUpdateVisRgn) SetHookFlags16( hdc, DCHF_INVALIDATEVISRGN ); /* force update */
529 if (!USER_Driver.pGetDC( hwnd, hdc, hrgnClip, flags )) hdc = 0;
531 TRACE("(%04x,%04x,0x%lx): returning %04x\n", hwnd, hrgnClip, flags, hdc);
532 END:
533 WIN_ReleaseWndPtr(wndPtr);
534 return hdc;
538 /***********************************************************************
539 * GetDC (USER.66)
541 HDC16 WINAPI GetDC16( HWND16 hwnd )
543 return (HDC16)GetDC( hwnd );
547 /***********************************************************************
548 * GetDC (USER32.@)
549 * RETURNS
550 * :Handle to DC
551 * NULL: Failure
553 HDC WINAPI GetDC(
554 HWND hwnd /* [in] handle of window */
556 if (!hwnd)
557 return GetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
558 return GetDCEx( hwnd, 0, DCX_USESTYLE );
562 /***********************************************************************
563 * GetWindowDC (USER.67)
565 HDC16 WINAPI GetWindowDC16( HWND16 hwnd )
567 return GetDCEx16( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
571 /***********************************************************************
572 * GetWindowDC (USER32.@)
574 HDC WINAPI GetWindowDC( HWND hwnd )
576 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
580 /***********************************************************************
581 * ReleaseDC (USER.68)
583 INT16 WINAPI ReleaseDC16( HWND16 hwnd, HDC16 hdc )
585 return (INT16)ReleaseDC( hwnd, hdc );
589 /***********************************************************************
590 * ReleaseDC (USER32.@)
592 * RETURNS
593 * 1: Success
594 * 0: Failure
596 INT WINAPI ReleaseDC(
597 HWND hwnd /* [in] Handle of window - ignored */,
598 HDC hdc /* [in] Handle of device context */
600 DCE * dce;
601 INT nRet = 0;
603 USER_Lock();
604 dce = firstDCE;
606 TRACE("%04x %04x\n", hwnd, hdc );
608 while (dce && (dce->hDC != hdc)) dce = dce->next;
610 if ( dce )
611 if ( dce->DCXflags & DCX_DCEBUSY )
612 nRet = DCE_ReleaseDC( dce );
614 USER_Unlock();
616 return nRet;
619 /***********************************************************************
620 * DCHook (USER.362)
622 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
624 BOOL16 WINAPI DCHook16( HDC16 hDC, WORD code, DWORD data, LPARAM lParam )
626 BOOL retv = TRUE;
627 DCE *dce = (DCE *)data;
629 TRACE("hDC = %04x, %i\n", hDC, code);
631 if (!dce) return 0;
632 assert(dce->hDC == hDC);
634 /* Grab the windows lock before doing anything else */
635 USER_Lock();
637 switch( code )
639 case DCHC_INVALIDVISRGN:
640 /* GDI code calls this when it detects that the
641 * DC is dirty (usually after SetHookFlags()). This
642 * means that we have to recompute the visible region.
644 if( dce->DCXflags & DCX_DCEBUSY )
646 /* Dirty bit has been cleared by caller, set it again so that
647 * pGetDC recomputes the visible region. */
648 SetHookFlags16( dce->hDC, DCHF_INVALIDATEVISRGN );
649 USER_Driver.pGetDC( dce->hwndCurrent, dce->hDC, dce->hClipRgn, dce->DCXflags );
651 else /* non-fatal but shouldn't happen */
652 WARN("DC is not in use!\n");
653 break;
655 case DCHC_DELETEDC:
657 * Windows will not let you delete a DC that is busy
658 * (between GetDC and ReleaseDC)
661 if ( dce->DCXflags & DCX_DCEBUSY )
663 WARN("Application trying to delete a busy DC\n");
664 retv = FALSE;
666 break;
668 default:
669 FIXME("unknown code\n");
672 USER_Unlock(); /* Release the wnd lock */
673 return retv;
677 /**********************************************************************
678 * WindowFromDC (USER.117)
680 HWND16 WINAPI WindowFromDC16( HDC16 hDC )
682 return (HWND16)WindowFromDC( hDC );
686 /**********************************************************************
687 * WindowFromDC (USER32.@)
689 HWND WINAPI WindowFromDC( HDC hDC )
691 DCE *dce;
692 HWND hwnd;
694 USER_Lock();
695 dce = firstDCE;
697 while (dce && (dce->hDC != hDC)) dce = dce->next;
699 hwnd = dce ? dce->hwndCurrent : 0;
700 USER_Unlock();
702 return hwnd;
706 /***********************************************************************
707 * LockWindowUpdate (USER.294)
709 BOOL16 WINAPI LockWindowUpdate16( HWND16 hwnd )
711 return LockWindowUpdate( hwnd );
714 /***********************************************************************
715 * LockWindowUpdate (USER32.@)
717 BOOL WINAPI LockWindowUpdate( HWND hwnd )
719 FIXME("(%x), stub!\n",hwnd);
720 return TRUE;