Made HEAP_strdup* functions inline (temporary).
[wine/multimedia.git] / windows / dce.c
blobac95b8a377fd95a4b165493db8c4a0638bf1d8e0
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 "options.h"
22 #include "dce.h"
23 #include "class.h"
24 #include "win.h"
25 #include "gdi.h"
26 #include "region.h"
27 #include "heap.h"
28 #include "local.h"
29 #include "module.h"
30 #include "user.h"
31 #include "debugtools.h"
32 #include "windef.h"
33 #include "wingdi.h"
34 #include "wine/winbase16.h"
35 #include "wine/winuser16.h"
37 DEFAULT_DEBUG_CHANNEL(dc);
39 #define NB_DCE 5 /* Number of DCEs created at startup */
41 static DCE *firstDCE = 0;
42 static HDC defaultDCstate = 0;
44 static void DCE_DeleteClipRgn( DCE* );
45 static INT DCE_ReleaseDC( DCE* );
48 /***********************************************************************
49 * DCE_DumpCache
51 static void DCE_DumpCache(void)
53 DCE *dce;
55 WIN_LockWnds();
56 dce = firstDCE;
58 DPRINTF("DCE:\n");
59 while( dce )
61 DPRINTF("\t[0x%08x] hWnd 0x%04x, dcx %08x, %s %s\n",
62 (unsigned)dce, dce->hwndCurrent, (unsigned)dce->DCXflags,
63 (dce->DCXflags & DCX_CACHE) ? "Cache" : "Owned",
64 (dce->DCXflags & DCX_DCEBUSY) ? "InUse" : "" );
65 dce = dce->next;
68 WIN_UnlockWnds();
71 /***********************************************************************
72 * DCE_AllocDCE
74 * Allocate a new DCE.
76 DCE *DCE_AllocDCE( HWND hWnd, DCE_TYPE type )
78 FARPROC16 hookProc;
79 DCE * dce;
80 WND* wnd;
82 if (!(dce = HeapAlloc( SystemHeap, 0, sizeof(DCE) ))) return NULL;
83 if (!(dce->hDC = CreateDCA( "DISPLAY", NULL, NULL, NULL )))
85 HeapFree( SystemHeap, 0, dce );
86 return 0;
89 wnd = WIN_FindWndPtr(hWnd);
91 /* store DCE handle in DC hook data field */
93 hookProc = (FARPROC16)NE_GetEntryPoint( GetModuleHandle16("USER"), 362 );
94 SetDCHook( dce->hDC, hookProc, (DWORD)dce );
96 dce->hwndCurrent = hWnd;
97 dce->hClipRgn = 0;
98 dce->next = firstDCE;
99 firstDCE = dce;
101 if( type != DCE_CACHE_DC ) /* owned or class DC */
103 dce->DCXflags = DCX_DCEBUSY;
104 if( hWnd )
106 if( wnd->dwStyle & WS_CLIPCHILDREN ) dce->DCXflags |= DCX_CLIPCHILDREN;
107 if( wnd->dwStyle & WS_CLIPSIBLINGS ) dce->DCXflags |= DCX_CLIPSIBLINGS;
109 SetHookFlags16(dce->hDC,DCHF_INVALIDATEVISRGN);
111 else dce->DCXflags = DCX_CACHE | DCX_DCEEMPTY;
113 WIN_ReleaseWndPtr(wnd);
115 return dce;
119 /***********************************************************************
120 * DCE_FreeDCE
122 DCE* DCE_FreeDCE( DCE *dce )
124 DCE **ppDCE;
126 if (!dce) return NULL;
128 WIN_LockWnds();
130 ppDCE = &firstDCE;
132 while (*ppDCE && (*ppDCE != dce)) ppDCE = &(*ppDCE)->next;
133 if (*ppDCE == dce) *ppDCE = dce->next;
135 SetDCHook(dce->hDC, NULL, 0L);
137 DeleteDC( dce->hDC );
138 if( dce->hClipRgn && !(dce->DCXflags & DCX_KEEPCLIPRGN) )
139 DeleteObject(dce->hClipRgn);
140 HeapFree( SystemHeap, 0, dce );
142 WIN_UnlockWnds();
144 return *ppDCE;
147 /***********************************************************************
148 * DCE_FreeWindowDCE
150 * Remove owned DCE and reset unreleased cache DCEs.
152 void DCE_FreeWindowDCE( WND* pWnd )
154 DCE *pDCE;
156 WIN_LockWnds();
157 pDCE = firstDCE;
159 while( pDCE )
161 if( pDCE->hwndCurrent == pWnd->hwndSelf )
163 if( pDCE == pWnd->dce ) /* owned or Class DCE*/
165 if (pWnd->class->style & CS_OWNDC) /* owned DCE*/
167 pDCE = DCE_FreeDCE( pDCE );
168 pWnd->dce = NULL;
169 continue;
171 else if( pDCE->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN) ) /* Class DCE*/
173 DCE_DeleteClipRgn( pDCE );
174 pDCE->hwndCurrent = 0;
177 else
179 if( pDCE->DCXflags & DCX_DCEBUSY ) /* shared cache DCE */
181 /* FIXME: AFAICS we are doing the right thing here so
182 * this should be a WARN. But this is best left as an ERR
183 * because the 'application error' is likely to come from
184 * another part of Wine (i.e. it's our fault after all).
185 * We should change this to WARN when Wine is more stable
186 * (for 1.0?).
188 ERR("[%04x] GetDC() without ReleaseDC()!\n",
189 pWnd->hwndSelf);
190 DCE_ReleaseDC( pDCE );
193 pDCE->DCXflags &= DCX_CACHE;
194 pDCE->DCXflags |= DCX_DCEEMPTY;
195 pDCE->hwndCurrent = 0;
198 pDCE = pDCE->next;
201 WIN_UnlockWnds();
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 TRACE("\trestoring VisRgn\n");
222 RestoreVisRgn16(dce->hDC);
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 SetDCState16( dce->hDC, defaultDCstate );
242 dce->DCXflags &= ~DCX_DCEBUSY;
243 if (dce->DCXflags & DCX_DCEDIRTY)
245 /* don't keep around invalidated entries
246 * because SetDCState() disables hVisRgn updates
247 * by removing dirty bit. */
249 dce->hwndCurrent = 0;
250 dce->DCXflags &= DCX_CACHE;
251 dce->DCXflags |= DCX_DCEEMPTY;
254 return 1;
258 /***********************************************************************
259 * DCE_InvalidateDCE
261 * It is called from SetWindowPos() and EVENT_MapNotify - we have to
262 * mark as dirty all busy DCEs for windows that have pWnd->parent as
263 * an ansector and whose client rect intersects with specified update
264 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
265 * DCX_CLIPCHILDREN flag is set. */
266 BOOL DCE_InvalidateDCE(WND* pWnd, const RECT* pRectUpdate)
268 WND* wndScope = WIN_LockWndPtr(pWnd->parent);
269 WND *pDesktop = WIN_GetDesktop();
270 BOOL bRet = FALSE;
272 if( wndScope )
274 DCE *dce;
276 TRACE("scope hwnd = %04x, (%i,%i - %i,%i)\n",
277 wndScope->hwndSelf, pRectUpdate->left,pRectUpdate->top,
278 pRectUpdate->right,pRectUpdate->bottom);
279 if(TRACE_ON(dc))
280 DCE_DumpCache();
282 /* walk all DCEs and fixup non-empty entries */
284 for (dce = firstDCE; (dce); dce = dce->next)
286 if( !(dce->DCXflags & DCX_DCEEMPTY) )
288 WND* wndCurrent = WIN_FindWndPtr(dce->hwndCurrent);
290 if( wndCurrent )
292 WND* wnd = NULL;
293 INT xoffset = 0, yoffset = 0;
295 if( (wndCurrent == wndScope) && !(dce->DCXflags & DCX_CLIPCHILDREN) )
297 /* child window positions don't bother us */
298 WIN_ReleaseWndPtr(wndCurrent);
299 continue;
302 if( !Options.desktopGeometry && wndCurrent == pDesktop )
304 /* don't bother with fake desktop */
305 WIN_ReleaseWndPtr(wndCurrent);
306 continue;
309 /* check if DCE window is within the z-order scope */
311 for( wnd = WIN_LockWndPtr(wndCurrent); wnd; WIN_UpdateWndPtr(&wnd,wnd->parent))
313 if( wnd == wndScope )
315 RECT wndRect;
317 wndRect = wndCurrent->rectWindow;
319 OffsetRect( &wndRect, xoffset - wndCurrent->rectClient.left,
320 yoffset - wndCurrent->rectClient.top);
322 if (pWnd == wndCurrent ||
323 IntersectRect( &wndRect, &wndRect, pRectUpdate ))
325 if( !(dce->DCXflags & DCX_DCEBUSY) )
327 /* Don't bother with visible regions of unused DCEs */
329 TRACE("\tpurged %08x dce [%04x]\n",
330 (unsigned)dce, wndCurrent->hwndSelf);
332 dce->hwndCurrent = 0;
333 dce->DCXflags &= DCX_CACHE;
334 dce->DCXflags |= DCX_DCEEMPTY;
336 else
338 /* Set dirty bits in the hDC and DCE structs */
340 TRACE("\tfixed up %08x dce [%04x]\n",
341 (unsigned)dce, wndCurrent->hwndSelf);
343 dce->DCXflags |= DCX_DCEDIRTY;
344 SetHookFlags16(dce->hDC, DCHF_INVALIDATEVISRGN);
345 bRet = TRUE;
348 WIN_ReleaseWndPtr(wnd);
349 break;
351 xoffset += wnd->rectClient.left;
352 yoffset += wnd->rectClient.top;
355 WIN_ReleaseWndPtr(wndCurrent);
357 } /* dce list */
358 WIN_ReleaseWndPtr(wndScope);
360 WIN_ReleaseDesktop();
361 return bRet;
364 /***********************************************************************
365 * DCE_Init
367 void DCE_Init(void)
369 int i;
370 DCE * dce;
372 for (i = 0; i < NB_DCE; i++)
374 if (!(dce = DCE_AllocDCE( 0, DCE_CACHE_DC ))) return;
375 if (!defaultDCstate) defaultDCstate = GetDCState16( dce->hDC );
380 /***********************************************************************
381 * DCE_GetVisRect
383 * Calculate the visible rectangle of a window (i.e. the client or
384 * window area clipped by the client area of all ancestors) in the
385 * corresponding coordinates. Return FALSE if the visible region is empty.
387 static BOOL DCE_GetVisRect( WND *wndPtr, BOOL clientArea, RECT *lprect )
389 *lprect = clientArea ? wndPtr->rectClient : wndPtr->rectWindow;
391 if (wndPtr->dwStyle & WS_VISIBLE)
393 INT xoffset = lprect->left;
394 INT yoffset = lprect->top;
396 while( !(wndPtr->flags & WIN_NATIVE) &&
397 ( wndPtr = WIN_LockWndPtr(wndPtr->parent)) )
399 if ( (wndPtr->dwStyle & (WS_ICONIC | WS_VISIBLE)) != WS_VISIBLE )
401 WIN_ReleaseWndPtr(wndPtr);
402 goto fail;
405 xoffset += wndPtr->rectClient.left;
406 yoffset += wndPtr->rectClient.top;
407 OffsetRect( lprect, wndPtr->rectClient.left,
408 wndPtr->rectClient.top );
410 if( (wndPtr->rectClient.left >= wndPtr->rectClient.right) ||
411 (wndPtr->rectClient.top >= wndPtr->rectClient.bottom) ||
412 (lprect->left >= wndPtr->rectClient.right) ||
413 (lprect->right <= wndPtr->rectClient.left) ||
414 (lprect->top >= wndPtr->rectClient.bottom) ||
415 (lprect->bottom <= wndPtr->rectClient.top) )
417 WIN_ReleaseWndPtr(wndPtr);
418 goto fail;
421 lprect->left = max( lprect->left, wndPtr->rectClient.left );
422 lprect->right = min( lprect->right, wndPtr->rectClient.right );
423 lprect->top = max( lprect->top, wndPtr->rectClient.top );
424 lprect->bottom = min( lprect->bottom, wndPtr->rectClient.bottom );
426 WIN_ReleaseWndPtr(wndPtr);
428 OffsetRect( lprect, -xoffset, -yoffset );
429 return TRUE;
432 fail:
433 SetRectEmpty( lprect );
434 return FALSE;
438 /***********************************************************************
439 * DCE_AddClipRects
441 * Go through the linked list of windows from pWndStart to pWndEnd,
442 * adding to the clip region the intersection of the target rectangle
443 * with an offset window rectangle.
445 static BOOL DCE_AddClipRects( WND *pWndStart, WND *pWndEnd,
446 HRGN hrgnClip, LPRECT lpRect, int x, int y )
448 RECT rect;
450 if( pWndStart->pDriver->pIsSelfClipping( pWndStart ) )
451 return TRUE; /* The driver itself will do the clipping */
453 for (WIN_LockWndPtr(pWndStart); (pWndStart && (pWndStart != pWndEnd)); WIN_UpdateWndPtr(&pWndStart,pWndStart->next))
455 if( !(pWndStart->dwStyle & WS_VISIBLE) ) continue;
457 rect.left = pWndStart->rectWindow.left + x;
458 rect.top = pWndStart->rectWindow.top + y;
459 rect.right = pWndStart->rectWindow.right + x;
460 rect.bottom = pWndStart->rectWindow.bottom + y;
462 if( IntersectRect( &rect, &rect, lpRect ))
464 if(!REGION_UnionRectWithRgn( hrgnClip, &rect )) break;
467 WIN_ReleaseWndPtr(pWndStart);
468 return (pWndStart == pWndEnd);
472 /***********************************************************************
473 * DCE_GetVisRgn
475 * Return the visible region of a window, i.e. the client or window area
476 * clipped by the client area of all ancestors, and then optionally
477 * by siblings and children.
479 HRGN DCE_GetVisRgn( HWND hwnd, WORD flags, HWND hwndChild, WORD cflags )
481 HRGN hrgnVis = 0;
482 RECT rect;
483 WND *wndPtr = WIN_FindWndPtr( hwnd );
484 WND *childWnd = WIN_FindWndPtr( hwndChild );
486 /* Get visible rectangle and create a region with it. */
488 if (wndPtr && DCE_GetVisRect(wndPtr, !(flags & DCX_WINDOW), &rect))
490 if((hrgnVis = CreateRectRgnIndirect( &rect )))
492 HRGN hrgnClip = CreateRectRgn( 0, 0, 0, 0 );
493 INT xoffset, yoffset;
495 if( hrgnClip )
497 /* Compute obscured region for the visible rectangle by
498 * clipping children, siblings, and ancestors. Note that
499 * DCE_GetVisRect() returns a rectangle either in client
500 * or in window coordinates (for DCX_WINDOW request). */
502 if( (flags & DCX_CLIPCHILDREN) && wndPtr->child )
504 if( flags & DCX_WINDOW )
506 /* adjust offsets since child window rectangles are
507 * in client coordinates */
509 xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
510 yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
512 else
513 xoffset = yoffset = 0;
515 DCE_AddClipRects( wndPtr->child, NULL, hrgnClip,
516 &rect, xoffset, yoffset );
519 /* We may need to clip children of child window, if a window with PARENTDC
520 * class style and CLIPCHILDREN window style (like in Free Agent 16
521 * preference dialogs) gets here, we take the region for the parent window
522 * but apparently still need to clip the children of the child window... */
524 if( (cflags & DCX_CLIPCHILDREN) && childWnd && childWnd->child )
526 if( flags & DCX_WINDOW )
528 /* adjust offsets since child window rectangles are
529 * in client coordinates */
531 xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
532 yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
534 else
535 xoffset = yoffset = 0;
537 /* client coordinates of child window */
538 xoffset += childWnd->rectClient.left;
539 yoffset += childWnd->rectClient.top;
541 DCE_AddClipRects( childWnd->child, NULL, hrgnClip,
542 &rect, xoffset, yoffset );
545 /* sibling window rectangles are in client
546 * coordinates of the parent window */
548 if (flags & DCX_WINDOW)
550 xoffset = -wndPtr->rectWindow.left;
551 yoffset = -wndPtr->rectWindow.top;
553 else
555 xoffset = -wndPtr->rectClient.left;
556 yoffset = -wndPtr->rectClient.top;
559 if (flags & DCX_CLIPSIBLINGS && wndPtr->parent )
560 DCE_AddClipRects( wndPtr->parent->child,
561 wndPtr, hrgnClip, &rect, xoffset, yoffset );
563 /* Clip siblings of all ancestors that have the
564 * WS_CLIPSIBLINGS style
567 while (wndPtr->dwStyle & WS_CHILD)
569 WIN_UpdateWndPtr(&wndPtr,wndPtr->parent);
570 xoffset -= wndPtr->rectClient.left;
571 yoffset -= wndPtr->rectClient.top;
572 if(wndPtr->dwStyle & WS_CLIPSIBLINGS && wndPtr->parent)
574 DCE_AddClipRects( wndPtr->parent->child, wndPtr,
575 hrgnClip, &rect, xoffset, yoffset );
579 /* Now once we've got a jumbo clip region we have
580 * to substract it from the visible rectangle.
583 CombineRgn( hrgnVis, hrgnVis, hrgnClip, RGN_DIFF );
584 DeleteObject( hrgnClip );
586 else
588 DeleteObject( hrgnVis );
589 hrgnVis = 0;
593 else
594 hrgnVis = CreateRectRgn(0, 0, 0, 0); /* empty */
595 WIN_ReleaseWndPtr(wndPtr);
596 WIN_ReleaseWndPtr(childWnd);
597 return hrgnVis;
600 /***********************************************************************
601 * DCE_OffsetVisRgn
603 * Change region from DC-origin relative coordinates to screen coords.
606 static void DCE_OffsetVisRgn( HDC hDC, HRGN hVisRgn )
608 DC *dc;
609 if (!(dc = DC_GetDCPtr( hDC ))) return;
611 OffsetRgn( hVisRgn, dc->DCOrgX, dc->DCOrgY );
613 GDI_ReleaseObj( hDC );
616 /***********************************************************************
617 * DCE_ExcludeRgn
619 * Translate given region from the wnd client to the DC coordinates
620 * and add it to the clipping region.
622 INT DCE_ExcludeRgn( HDC hDC, WND* wnd, HRGN hRgn )
624 POINT pt = {0, 0};
625 DCE *dce = firstDCE;
627 while (dce && (dce->hDC != hDC)) dce = dce->next;
628 if( dce )
630 MapWindowPoints( wnd->hwndSelf, dce->hwndCurrent, &pt, 1);
631 if( dce->DCXflags & DCX_WINDOW )
633 wnd = WIN_FindWndPtr(dce->hwndCurrent);
634 pt.x += wnd->rectClient.left - wnd->rectWindow.left;
635 pt.y += wnd->rectClient.top - wnd->rectWindow.top;
636 WIN_ReleaseWndPtr(wnd);
639 else return ERROR;
640 OffsetRgn(hRgn, pt.x, pt.y);
642 return ExtSelectClipRgn( hDC, hRgn, RGN_DIFF );
646 /***********************************************************************
647 * GetDCEx16 (USER.359)
649 HDC16 WINAPI GetDCEx16( HWND16 hwnd, HRGN16 hrgnClip, DWORD flags )
651 return (HDC16)GetDCEx( hwnd, hrgnClip, flags );
655 /***********************************************************************
656 * GetDCEx (USER32.231)
658 * Unimplemented flags: DCX_LOCKWINDOWUPDATE
660 * FIXME: Full support for hrgnClip == 1 (alias for entire window).
662 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
664 HRGN hrgnVisible = 0;
665 HDC hdc = 0;
666 DCE * dce;
667 DC * dc;
668 WND * wndPtr;
669 DWORD dcxFlags = 0;
670 BOOL bUpdateVisRgn = TRUE;
671 BOOL bUpdateClipOrigin = FALSE;
673 TRACE("hwnd %04x, hrgnClip %04x, flags %08x\n",
674 hwnd, hrgnClip, (unsigned)flags);
676 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
678 /* fixup flags */
680 if (!(wndPtr->class->style & (CS_OWNDC | CS_CLASSDC))) flags |= DCX_CACHE;
682 if (flags & DCX_USESTYLE)
684 flags &= ~( DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
686 if( wndPtr->dwStyle & WS_CLIPSIBLINGS )
687 flags |= DCX_CLIPSIBLINGS;
689 if ( !(flags & DCX_WINDOW) )
691 if (wndPtr->class->style & CS_PARENTDC) flags |= DCX_PARENTCLIP;
693 if (wndPtr->dwStyle & WS_CLIPCHILDREN &&
694 !(wndPtr->dwStyle & WS_MINIMIZE) ) flags |= DCX_CLIPCHILDREN;
696 else flags |= DCX_CACHE;
699 if( flags & DCX_NOCLIPCHILDREN )
701 flags |= DCX_CACHE;
702 flags &= ~(DCX_PARENTCLIP | DCX_CLIPCHILDREN);
705 if (flags & DCX_WINDOW)
706 flags = (flags & ~DCX_CLIPCHILDREN) | DCX_CACHE;
708 if (!(wndPtr->dwStyle & WS_CHILD) || !wndPtr->parent )
709 flags &= ~DCX_PARENTCLIP;
710 else if( flags & DCX_PARENTCLIP )
712 flags |= DCX_CACHE;
713 if( !(flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) )
714 if( (wndPtr->dwStyle & WS_VISIBLE) && (wndPtr->parent->dwStyle & WS_VISIBLE) )
716 flags &= ~DCX_CLIPCHILDREN;
717 if( wndPtr->parent->dwStyle & WS_CLIPSIBLINGS )
718 flags |= DCX_CLIPSIBLINGS;
722 /* find a suitable DCE */
724 dcxFlags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
725 DCX_CACHE | DCX_WINDOW);
727 if (flags & DCX_CACHE)
729 DCE* dceEmpty;
730 DCE* dceUnused;
732 dceEmpty = dceUnused = NULL;
734 /* Strategy: First, we attempt to find a non-empty but unused DCE with
735 * compatible flags. Next, we look for an empty entry. If the cache is
736 * full we have to purge one of the unused entries.
739 for (dce = firstDCE; (dce); dce = dce->next)
741 if ((dce->DCXflags & (DCX_CACHE | DCX_DCEBUSY)) == DCX_CACHE )
743 dceUnused = dce;
745 if (dce->DCXflags & DCX_DCEEMPTY)
746 dceEmpty = dce;
747 else
748 if ((dce->hwndCurrent == hwnd) &&
749 ((dce->DCXflags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
750 DCX_CACHE | DCX_WINDOW | DCX_PARENTCLIP)) == dcxFlags))
752 TRACE("\tfound valid %08x dce [%04x], flags %08x\n",
753 (unsigned)dce, hwnd, (unsigned)dcxFlags );
754 bUpdateVisRgn = FALSE;
755 bUpdateClipOrigin = TRUE;
756 break;
761 if (!dce) dce = (dceEmpty) ? dceEmpty : dceUnused;
763 /* if there's no dce empty or unused, allocate a new one */
764 if (!dce)
766 dce = DCE_AllocDCE( 0, DCE_CACHE_DC );
769 else
771 dce = (wndPtr->class->style & CS_OWNDC) ? wndPtr->dce : wndPtr->class->dce;
772 if( dce->hwndCurrent == hwnd )
774 TRACE("\tskipping hVisRgn update\n");
775 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
777 /* Abey - 16Jul99. to take care of the nested GetDC. first one
778 with DCX_EXCLUDERGN or DCX_INTERSECTRGN flags and the next
779 one with or without these flags. */
781 if(dce->DCXflags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN))
783 /* This is likely to be a nested BeginPaint().
784 or a BeginPaint() followed by a GetDC()*/
786 if( dce->hClipRgn != hrgnClip )
788 FIXME("new hrgnClip[%04x] smashes the previous[%04x]\n",
789 hrgnClip, dce->hClipRgn );
790 DCE_DeleteClipRgn( dce );
792 else
793 RestoreVisRgn16(dce->hDC);
797 if (!dce)
799 hdc = 0;
800 goto END;
803 dce->hwndCurrent = hwnd;
804 dce->hClipRgn = 0;
805 dce->DCXflags = dcxFlags | (flags & DCX_WINDOWPAINT) | DCX_DCEBUSY;
806 hdc = dce->hDC;
808 if (!(dc = DC_GetDCPtr( hdc )))
810 hdc = 0;
811 goto END;
813 bUpdateVisRgn = bUpdateVisRgn || (dc->flags & DC_DIRTY);
815 /* recompute visible region */
816 wndPtr->pDriver->pSetDrawable( wndPtr, hdc, flags, bUpdateClipOrigin );
817 dc->flags &= ~DC_DIRTY;
818 GDI_ReleaseObj( hdc );
820 if( bUpdateVisRgn )
822 TRACE("updating visrgn for %08x dce, hwnd [%04x]\n", (unsigned)dce, hwnd);
824 if (flags & DCX_PARENTCLIP)
826 WND *parentPtr = WIN_LockWndPtr(wndPtr->parent);
828 if( wndPtr->dwStyle & WS_VISIBLE && !(parentPtr->dwStyle & WS_MINIMIZE) )
830 if( parentPtr->dwStyle & WS_CLIPSIBLINGS )
831 dcxFlags = DCX_CLIPSIBLINGS | (flags & ~(DCX_CLIPCHILDREN | DCX_WINDOW));
832 else
833 dcxFlags = flags & ~(DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW);
835 hrgnVisible = DCE_GetVisRgn( parentPtr->hwndSelf, dcxFlags,
836 wndPtr->hwndSelf, flags );
837 if( flags & DCX_WINDOW )
838 OffsetRgn( hrgnVisible, -wndPtr->rectWindow.left,
839 -wndPtr->rectWindow.top );
840 else
841 OffsetRgn( hrgnVisible, -wndPtr->rectClient.left,
842 -wndPtr->rectClient.top );
843 DCE_OffsetVisRgn( hdc, hrgnVisible );
845 else
846 hrgnVisible = CreateRectRgn( 0, 0, 0, 0 );
847 WIN_ReleaseWndPtr(parentPtr);
849 else
850 if ((hwnd == GetDesktopWindow()) && !USER_Driver.pIsSingleWindow())
851 hrgnVisible = CreateRectRgn( 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN) );
852 else
854 hrgnVisible = DCE_GetVisRgn( hwnd, flags, 0, 0 );
855 DCE_OffsetVisRgn( hdc, hrgnVisible );
858 dce->DCXflags &= ~DCX_DCEDIRTY;
859 SelectVisRgn16( hdc, hrgnVisible );
861 else
862 TRACE("no visrgn update %08x dce, hwnd [%04x]\n", (unsigned)dce, hwnd);
864 /* apply additional region operation (if any) */
866 if( flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN) )
868 if( !hrgnVisible ) hrgnVisible = CreateRectRgn( 0, 0, 0, 0 );
870 dce->DCXflags |= flags & (DCX_KEEPCLIPRGN | DCX_INTERSECTRGN | DCX_EXCLUDERGN);
871 dce->hClipRgn = hrgnClip;
873 TRACE("\tsaved VisRgn, clipRgn = %04x\n", hrgnClip);
875 SaveVisRgn16( hdc );
876 CombineRgn( hrgnVisible, hrgnClip, 0, RGN_COPY );
877 DCE_OffsetVisRgn( hdc, hrgnVisible );
878 CombineRgn( hrgnVisible, InquireVisRgn16( hdc ), hrgnVisible,
879 (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
880 SelectVisRgn16( hdc, hrgnVisible );
883 if( hrgnVisible ) DeleteObject( hrgnVisible );
885 TRACE("(%04x,%04x,0x%lx): returning %04x\n",
886 hwnd, hrgnClip, flags, hdc);
887 END:
888 WIN_ReleaseWndPtr(wndPtr);
889 return hdc;
893 /***********************************************************************
894 * GetDC16 (USER.66)
896 HDC16 WINAPI GetDC16( HWND16 hwnd )
898 return (HDC16)GetDC( hwnd );
902 /***********************************************************************
903 * GetDC (USER32.230)
904 * RETURNS
905 * :Handle to DC
906 * NULL: Failure
908 HDC WINAPI GetDC(
909 HWND hwnd /* handle of window */
911 if (!hwnd)
912 return GetDCEx( GetDesktopWindow(), 0, DCX_CACHE | DCX_WINDOW );
913 return GetDCEx( hwnd, 0, DCX_USESTYLE );
917 /***********************************************************************
918 * GetWindowDC16 (USER.67)
920 HDC16 WINAPI GetWindowDC16( HWND16 hwnd )
922 if (!hwnd) hwnd = GetDesktopWindow16();
923 return GetDCEx16( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
927 /***********************************************************************
928 * GetWindowDC (USER32.304)
930 HDC WINAPI GetWindowDC( HWND hwnd )
932 if (!hwnd) hwnd = GetDesktopWindow();
933 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
937 /***********************************************************************
938 * ReleaseDC16 (USER.68)
940 INT16 WINAPI ReleaseDC16( HWND16 hwnd, HDC16 hdc )
942 return (INT16)ReleaseDC( hwnd, hdc );
946 /***********************************************************************
947 * ReleaseDC (USER32.440)
949 * RETURNS
950 * 1: Success
951 * 0: Failure
953 INT WINAPI ReleaseDC(
954 HWND hwnd /* Handle of window - ignored */,
955 HDC hdc /* Handle of device context */
957 DCE * dce;
958 INT nRet = 0;
960 WIN_LockWnds();
961 dce = firstDCE;
963 TRACE("%04x %04x\n", hwnd, hdc );
965 while (dce && (dce->hDC != hdc)) dce = dce->next;
967 if ( dce )
968 if ( dce->DCXflags & DCX_DCEBUSY )
969 nRet = DCE_ReleaseDC( dce );
971 WIN_UnlockWnds();
973 return nRet;
976 /***********************************************************************
977 * DCHook (USER.362)
979 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
981 BOOL16 WINAPI DCHook16( HDC16 hDC, WORD code, DWORD data, LPARAM lParam )
983 BOOL retv = TRUE;
984 HRGN hVisRgn;
985 DCE *dce = (DCE *)data;
986 WND *wndPtr;
988 TRACE("hDC = %04x, %i\n", hDC, code);
990 if (!dce) return 0;
991 assert(dce->hDC == hDC);
993 /* Grab the windows lock before doing anything else */
994 WIN_LockWnds();
996 switch( code )
998 case DCHC_INVALIDVISRGN:
1000 /* GDI code calls this when it detects that the
1001 * DC is dirty (usually after SetHookFlags()). This
1002 * means that we have to recompute the visible region.
1005 if( dce->DCXflags & DCX_DCEBUSY )
1008 /* Update stale DC in DCX */
1009 wndPtr = WIN_FindWndPtr( dce->hwndCurrent);
1010 if (wndPtr) wndPtr->pDriver->pSetDrawable( wndPtr, dce->hDC, dce->DCXflags, TRUE);
1012 SetHookFlags16(hDC, DCHF_VALIDATEVISRGN);
1013 hVisRgn = DCE_GetVisRgn(dce->hwndCurrent, dce->DCXflags, 0, 0);
1015 TRACE("\tapplying saved clipRgn\n");
1017 /* clip this region with saved clipping region */
1019 if ( (dce->DCXflags & DCX_INTERSECTRGN && dce->hClipRgn != 1) ||
1020 ( dce->DCXflags & DCX_EXCLUDERGN && dce->hClipRgn) )
1023 if( (!dce->hClipRgn && dce->DCXflags & DCX_INTERSECTRGN) ||
1024 (dce->hClipRgn == 1 && dce->DCXflags & DCX_EXCLUDERGN) )
1025 SetRectRgn(hVisRgn,0,0,0,0);
1026 else
1027 CombineRgn(hVisRgn, hVisRgn, dce->hClipRgn,
1028 (dce->DCXflags & DCX_EXCLUDERGN)? RGN_DIFF:RGN_AND);
1030 dce->DCXflags &= ~DCX_DCEDIRTY;
1031 DCE_OffsetVisRgn( hDC, hVisRgn );
1032 SelectVisRgn16(hDC, hVisRgn);
1033 DeleteObject( hVisRgn );
1034 WIN_ReleaseWndPtr( wndPtr ); /* Release WIN_FindWndPtr lock */
1036 else /* non-fatal but shouldn't happen */
1037 WARN("DC is not in use!\n");
1038 break;
1040 case DCHC_DELETEDC:
1042 * Windows will not let you delete a DC that is busy
1043 * (between GetDC and ReleaseDC)
1046 if ( dce->DCXflags & DCX_DCEBUSY )
1048 WARN("Application trying to delete a busy DC\n");
1049 retv = FALSE;
1051 break;
1053 default:
1054 FIXME("unknown code\n");
1057 WIN_UnlockWnds(); /* Release the wnd lock */
1058 return retv;
1062 /**********************************************************************
1063 * WindowFromDC16 (USER.117)
1065 HWND16 WINAPI WindowFromDC16( HDC16 hDC )
1067 return (HWND16)WindowFromDC( hDC );
1071 /**********************************************************************
1072 * WindowFromDC (USER32.581)
1074 HWND WINAPI WindowFromDC( HDC hDC )
1076 DCE *dce;
1077 HWND hwnd;
1079 WIN_LockWnds();
1080 dce = firstDCE;
1082 while (dce && (dce->hDC != hDC)) dce = dce->next;
1084 hwnd = dce ? dce->hwndCurrent : 0;
1085 WIN_UnlockWnds();
1087 return hwnd;
1091 /***********************************************************************
1092 * LockWindowUpdate16 (USER.294)
1094 BOOL16 WINAPI LockWindowUpdate16( HWND16 hwnd )
1096 return LockWindowUpdate( hwnd );
1100 /***********************************************************************
1101 * LockWindowUpdate (USER32.378)
1103 BOOL WINAPI LockWindowUpdate( HWND hwnd )
1105 /* FIXME? DCX_LOCKWINDOWUPDATE is unimplemented */
1106 return TRUE;