Authors: Chris Morgan <cmorgan@wpi.edu>, James Abbatiello <abbeyj@wpi.edu>
[wine.git] / windows / dce.c
blobbecdd9c51cc5c963639b49a91a4faccfca930a8a
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 "desktop.h"
22 #include "options.h"
23 #include "dce.h"
24 #include "class.h"
25 #include "win.h"
26 #include "gdi.h"
27 #include "region.h"
28 #include "heap.h"
29 #include "local.h"
30 #include "debug.h"
31 #include "wine/winuser16.h"
33 DEFAULT_DEBUG_CHANNEL(dc)
35 #define NB_DCE 5 /* Number of DCEs created at startup */
37 static DCE *firstDCE = 0;
38 static HDC defaultDCstate = 0;
40 static void DCE_DeleteClipRgn( DCE* );
41 static INT DCE_ReleaseDC( DCE* );
44 /***********************************************************************
45 * DCE_DumpCache
47 static void DCE_DumpCache(void)
49 DCE *dce;
51 WIN_LockWnds();
52 dce = firstDCE;
54 DUMP("DCE:\n");
55 while( dce )
57 DUMP("\t[0x%08x] hWnd 0x%04x, dcx %08x, %s %s\n",
58 (unsigned)dce, dce->hwndCurrent, (unsigned)dce->DCXflags,
59 (dce->DCXflags & DCX_CACHE) ? "Cache" : "Owned",
60 (dce->DCXflags & DCX_DCEBUSY) ? "InUse" : "" );
61 dce = dce->next;
64 WIN_UnlockWnds();
67 /***********************************************************************
68 * DCE_AllocDCE
70 * Allocate a new DCE.
72 DCE *DCE_AllocDCE( HWND hWnd, DCE_TYPE type )
74 DCE * dce;
75 WND* wnd;
77 if (!(dce = HeapAlloc( SystemHeap, 0, sizeof(DCE) ))) return NULL;
78 if (!(dce->hDC = CreateDC16( "DISPLAY", NULL, NULL, NULL )))
80 HeapFree( SystemHeap, 0, dce );
81 return 0;
84 wnd = WIN_FindWndPtr(hWnd);
86 /* store DCE handle in DC hook data field */
88 SetDCHook( dce->hDC, (FARPROC16)DCHook16, (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( SystemHeap, 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 DCE */
159 pDCE = DCE_FreeDCE( pDCE );
160 pWnd->dce = NULL;
161 continue;
163 else
165 if(!(pDCE->DCXflags & DCX_CACHE) ) /* class DCE */
167 if( pDCE->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN) )
168 DCE_DeleteClipRgn( pDCE );
170 else if( pDCE->DCXflags & DCX_DCEBUSY ) /* shared cache DCE */
172 ERR(dc,"[%04x] GetDC() without ReleaseDC()!\n",
173 pWnd->hwndSelf);
174 DCE_ReleaseDC( pDCE );
177 pDCE->DCXflags &= DCX_CACHE;
178 pDCE->DCXflags |= DCX_DCEEMPTY;
179 pDCE->hwndCurrent = 0;
182 pDCE = pDCE->next;
185 WIN_UnlockWnds();
189 /***********************************************************************
190 * DCE_DeleteClipRgn
192 static void DCE_DeleteClipRgn( DCE* dce )
194 dce->DCXflags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN | DCX_WINDOWPAINT);
196 if( dce->DCXflags & DCX_KEEPCLIPRGN )
197 dce->DCXflags &= ~DCX_KEEPCLIPRGN;
198 else
199 if( dce->hClipRgn > 1 )
200 DeleteObject( dce->hClipRgn );
202 dce->hClipRgn = 0;
204 TRACE(dc,"\trestoring VisRgn\n");
206 RestoreVisRgn16(dce->hDC);
210 /***********************************************************************
211 * DCE_ReleaseDC
213 static INT DCE_ReleaseDC( DCE* dce )
215 if ((dce->DCXflags & (DCX_DCEEMPTY | DCX_DCEBUSY)) != DCX_DCEBUSY) return 0;
217 /* restore previous visible region */
219 if ((dce->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
220 (dce->DCXflags & (DCX_CACHE | DCX_WINDOWPAINT)) )
221 DCE_DeleteClipRgn( dce );
223 if (dce->DCXflags & DCX_CACHE)
225 SetDCState16( dce->hDC, defaultDCstate );
226 dce->DCXflags &= ~DCX_DCEBUSY;
227 if (dce->DCXflags & DCX_DCEDIRTY)
229 /* don't keep around invalidated entries
230 * because SetDCState() disables hVisRgn updates
231 * by removing dirty bit. */
233 dce->hwndCurrent = 0;
234 dce->DCXflags &= DCX_CACHE;
235 dce->DCXflags |= DCX_DCEEMPTY;
238 return 1;
242 /***********************************************************************
243 * DCE_InvalidateDCE
245 * It is called from SetWindowPos() - we have to mark as dirty all busy
246 * DCEs for windows that have pWnd->parent as an ansector and whose client
247 * rect intersects with specified update rectangle. In addition, pWnd->parent
248 * DCEs may need to be updated if DCX_CLIPCHILDREN flag is set.
250 BOOL DCE_InvalidateDCE(WND* pWnd, const RECT* pRectUpdate)
252 WND* wndScope = WIN_LockWndPtr(pWnd->parent);
253 WND *pDesktop = WIN_GetDesktop();
254 BOOL bRet = FALSE;
256 if( wndScope )
258 DCE *dce;
260 TRACE(dc,"scope hwnd = %04x, (%i,%i - %i,%i)\n",
261 wndScope->hwndSelf, pRectUpdate->left,pRectUpdate->top,
262 pRectUpdate->right,pRectUpdate->bottom);
263 if(TRACE_ON(dc))
264 DCE_DumpCache();
266 /* walk all DCEs and fixup non-empty entries */
268 for (dce = firstDCE; (dce); dce = dce->next)
270 if( !(dce->DCXflags & DCX_DCEEMPTY) )
272 WND* wndCurrent = WIN_FindWndPtr(dce->hwndCurrent);
274 if( wndCurrent )
276 WND* wnd = NULL;
277 INT xoffset = 0, yoffset = 0;
279 if( (wndCurrent == wndScope) && !(dce->DCXflags & DCX_CLIPCHILDREN) )
281 /* child window positions don't bother us */
282 WIN_ReleaseWndPtr(wndCurrent);
283 continue;
286 if( !Options.desktopGeometry && wndCurrent == pDesktop )
288 /* don't bother with fake desktop */
289 WIN_ReleaseWndPtr(wndCurrent);
290 continue;
293 /* check if DCE window is within the z-order scope */
295 for( wnd = WIN_LockWndPtr(wndCurrent); wnd; WIN_UpdateWndPtr(&wnd,wnd->parent))
297 if( wnd == wndScope )
299 RECT wndRect;
301 wndRect = wndCurrent->rectWindow;
303 OffsetRect( &wndRect, xoffset - wndCurrent->rectClient.left,
304 yoffset - wndCurrent->rectClient.top);
306 if (pWnd == wndCurrent ||
307 IntersectRect( &wndRect, &wndRect, pRectUpdate ))
309 if( !(dce->DCXflags & DCX_DCEBUSY) )
311 /* Don't bother with visible regions of unused DCEs */
313 TRACE(dc,"\tpurged %08x dce [%04x]\n",
314 (unsigned)dce, wndCurrent->hwndSelf);
316 dce->hwndCurrent = 0;
317 dce->DCXflags &= DCX_CACHE;
318 dce->DCXflags |= DCX_DCEEMPTY;
320 else
322 /* Set dirty bits in the hDC and DCE structs */
324 TRACE(dc,"\tfixed up %08x dce [%04x]\n",
325 (unsigned)dce, wndCurrent->hwndSelf);
327 dce->DCXflags |= DCX_DCEDIRTY;
328 SetHookFlags16(dce->hDC, DCHF_INVALIDATEVISRGN);
329 bRet = TRUE;
332 WIN_ReleaseWndPtr(wnd);
333 break;
335 xoffset += wnd->rectClient.left;
336 yoffset += wnd->rectClient.top;
339 WIN_ReleaseWndPtr(wndCurrent);
341 } /* dce list */
342 WIN_ReleaseWndPtr(wndScope);
344 WIN_ReleaseDesktop();
345 return bRet;
348 /***********************************************************************
349 * DCE_Init
351 void DCE_Init(void)
353 int i;
354 DCE * dce;
356 for (i = 0; i < NB_DCE; i++)
358 if (!(dce = DCE_AllocDCE( 0, DCE_CACHE_DC ))) return;
359 if (!defaultDCstate) defaultDCstate = GetDCState16( dce->hDC );
364 /***********************************************************************
365 * DCE_GetVisRect
367 * Calculate the visible rectangle of a window (i.e. the client or
368 * window area clipped by the client area of all ancestors) in the
369 * corresponding coordinates. Return FALSE if the visible region is empty.
371 static BOOL DCE_GetVisRect( WND *wndPtr, BOOL clientArea, RECT *lprect )
373 *lprect = clientArea ? wndPtr->rectClient : wndPtr->rectWindow;
375 if (wndPtr->dwStyle & WS_VISIBLE)
377 INT xoffset = lprect->left;
378 INT yoffset = lprect->top;
380 while( !(wndPtr->flags & WIN_NATIVE) )
382 wndPtr = WIN_LockWndPtr(wndPtr->parent);
384 if ( (wndPtr->dwStyle & (WS_ICONIC | WS_VISIBLE)) != WS_VISIBLE )
386 WIN_ReleaseWndPtr(wndPtr);
387 goto fail;
390 xoffset += wndPtr->rectClient.left;
391 yoffset += wndPtr->rectClient.top;
392 OffsetRect( lprect, wndPtr->rectClient.left,
393 wndPtr->rectClient.top );
395 if( (wndPtr->rectClient.left >= wndPtr->rectClient.right) ||
396 (wndPtr->rectClient.top >= wndPtr->rectClient.bottom) ||
397 (lprect->left >= wndPtr->rectClient.right) ||
398 (lprect->right <= wndPtr->rectClient.left) ||
399 (lprect->top >= wndPtr->rectClient.bottom) ||
400 (lprect->bottom <= wndPtr->rectClient.top) )
402 WIN_ReleaseWndPtr(wndPtr);
403 goto fail;
406 lprect->left = MAX( lprect->left, wndPtr->rectClient.left );
407 lprect->right = MIN( lprect->right, wndPtr->rectClient.right );
408 lprect->top = MAX( lprect->top, wndPtr->rectClient.top );
409 lprect->bottom = MIN( lprect->bottom, wndPtr->rectClient.bottom );
411 WIN_ReleaseWndPtr(wndPtr);
413 OffsetRect( lprect, -xoffset, -yoffset );
414 return TRUE;
417 fail:
418 SetRectEmpty( lprect );
419 return FALSE;
423 /***********************************************************************
424 * DCE_AddClipRects
426 * Go through the linked list of windows from pWndStart to pWndEnd,
427 * adding to the clip region the intersection of the target rectangle
428 * with an offset window rectangle.
430 static BOOL DCE_AddClipRects( WND *pWndStart, WND *pWndEnd,
431 HRGN hrgnClip, LPRECT lpRect, int x, int y )
433 RECT rect;
435 if( pWndStart->pDriver->pIsSelfClipping( pWndStart ) )
436 return TRUE; /* The driver itself will do the clipping */
438 for (WIN_LockWndPtr(pWndStart); pWndStart != pWndEnd; WIN_UpdateWndPtr(&pWndStart,pWndStart->next))
440 if( !(pWndStart->dwStyle & WS_VISIBLE) ) continue;
442 rect.left = pWndStart->rectWindow.left + x;
443 rect.top = pWndStart->rectWindow.top + y;
444 rect.right = pWndStart->rectWindow.right + x;
445 rect.bottom = pWndStart->rectWindow.bottom + y;
447 if( IntersectRect( &rect, &rect, lpRect ))
449 if(!REGION_UnionRectWithRgn( hrgnClip, &rect )) break;
452 WIN_ReleaseWndPtr(pWndStart);
453 return (pWndStart == pWndEnd);
457 /***********************************************************************
458 * DCE_GetVisRgn
460 * Return the visible region of a window, i.e. the client or window area
461 * clipped by the client area of all ancestors, and then optionally
462 * by siblings and children.
464 HRGN DCE_GetVisRgn( HWND hwnd, WORD flags, HWND hwndChild, WORD cflags )
466 HRGN hrgnVis = 0;
467 RECT rect;
468 WND *wndPtr = WIN_FindWndPtr( hwnd );
469 WND *childWnd = WIN_FindWndPtr( hwndChild );
471 /* Get visible rectangle and create a region with it. */
473 if (wndPtr && DCE_GetVisRect(wndPtr, !(flags & DCX_WINDOW), &rect))
475 if((hrgnVis = CreateRectRgnIndirect( &rect )))
477 HRGN hrgnClip = CreateRectRgn( 0, 0, 0, 0 );
478 INT xoffset, yoffset;
480 if( hrgnClip )
482 /* Compute obscured region for the visible rectangle by
483 * clipping children, siblings, and ancestors. Note that
484 * DCE_GetVisRect() returns a rectangle either in client
485 * or in window coordinates (for DCX_WINDOW request). */
487 if( (flags & DCX_CLIPCHILDREN) && wndPtr->child )
489 if( flags & DCX_WINDOW )
491 /* adjust offsets since child window rectangles are
492 * in client coordinates */
494 xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
495 yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
497 else
498 xoffset = yoffset = 0;
500 DCE_AddClipRects( wndPtr->child, NULL, hrgnClip,
501 &rect, xoffset, yoffset );
504 /* We may need to clip children of child window, if a window with PARENTDC
505 * class style and CLIPCHILDREN window style (like in Free Agent 16
506 * preference dialogs) gets here, we take the region for the parent window
507 * but apparently still need to clip the children of the child window... */
509 if( (cflags & DCX_CLIPCHILDREN) && childWnd && childWnd->child )
511 if( flags & DCX_WINDOW )
513 /* adjust offsets since child window rectangles are
514 * in client coordinates */
516 xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
517 yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
519 else
520 xoffset = yoffset = 0;
522 /* client coordinates of child window */
523 xoffset += childWnd->rectClient.left;
524 yoffset += childWnd->rectClient.top;
526 DCE_AddClipRects( childWnd->child, NULL, hrgnClip,
527 &rect, xoffset, yoffset );
530 /* sibling window rectangles are in client
531 * coordinates of the parent window */
533 if (flags & DCX_WINDOW)
535 xoffset = -wndPtr->rectWindow.left;
536 yoffset = -wndPtr->rectWindow.top;
538 else
540 xoffset = -wndPtr->rectClient.left;
541 yoffset = -wndPtr->rectClient.top;
544 if (flags & DCX_CLIPSIBLINGS && wndPtr->parent )
545 DCE_AddClipRects( wndPtr->parent->child,
546 wndPtr, hrgnClip, &rect, xoffset, yoffset );
548 /* Clip siblings of all ancestors that have the
549 * WS_CLIPSIBLINGS style
552 while (wndPtr->dwStyle & WS_CHILD)
554 WIN_UpdateWndPtr(&wndPtr,wndPtr->parent);
555 xoffset -= wndPtr->rectClient.left;
556 yoffset -= wndPtr->rectClient.top;
557 if(wndPtr->dwStyle & WS_CLIPSIBLINGS && wndPtr->parent)
559 DCE_AddClipRects( wndPtr->parent->child, wndPtr,
560 hrgnClip, &rect, xoffset, yoffset );
564 /* Now once we've got a jumbo clip region we have
565 * to substract it from the visible rectangle.
568 CombineRgn( hrgnVis, hrgnVis, hrgnClip, RGN_DIFF );
569 DeleteObject( hrgnClip );
571 else
573 DeleteObject( hrgnVis );
574 hrgnVis = 0;
578 else
579 hrgnVis = CreateRectRgn(0, 0, 0, 0); /* empty */
580 WIN_ReleaseWndPtr(wndPtr);
581 WIN_ReleaseWndPtr(childWnd);
582 return hrgnVis;
585 /***********************************************************************
586 * DCE_OffsetVisRgn
588 * Change region from DC-origin relative coordinates to screen coords.
591 static void DCE_OffsetVisRgn( HDC hDC, HRGN hVisRgn )
593 DC *dc;
594 if (!(dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC ))) return;
596 OffsetRgn( hVisRgn, dc->w.DCOrgX, dc->w.DCOrgY );
598 GDI_HEAP_UNLOCK( hDC );
601 /***********************************************************************
602 * DCE_ExcludeRgn
604 * Translate given region from the wnd client to the DC coordinates
605 * and add it to the clipping region.
607 INT16 DCE_ExcludeRgn( HDC hDC, WND* wnd, HRGN hRgn )
609 POINT pt = {0, 0};
610 DCE *dce = firstDCE;
612 while (dce && (dce->hDC != hDC)) dce = dce->next;
613 if( dce )
615 MapWindowPoints( wnd->hwndSelf, dce->hwndCurrent, &pt, 1);
616 if( dce->DCXflags & DCX_WINDOW )
618 wnd = WIN_FindWndPtr(dce->hwndCurrent);
619 pt.x += wnd->rectClient.left - wnd->rectWindow.left;
620 pt.y += wnd->rectClient.top - wnd->rectWindow.top;
621 WIN_ReleaseWndPtr(wnd);
624 else return ERROR;
625 OffsetRgn(hRgn, pt.x, pt.y);
627 return ExtSelectClipRgn( hDC, hRgn, RGN_DIFF );
631 /***********************************************************************
632 * GetDCEx16 (USER.359)
634 HDC16 WINAPI GetDCEx16( HWND16 hwnd, HRGN16 hrgnClip, DWORD flags )
636 return (HDC16)GetDCEx( hwnd, hrgnClip, flags );
640 /***********************************************************************
641 * GetDCEx32 (USER32.231)
643 * Unimplemented flags: DCX_LOCKWINDOWUPDATE
645 * FIXME: Full support for hrgnClip == 1 (alias for entire window).
647 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
649 HRGN hrgnVisible = 0;
650 HDC hdc = 0;
651 DCE * dce;
652 DC * dc;
653 WND * wndPtr;
654 DWORD dcxFlags = 0;
655 BOOL bUpdateVisRgn = TRUE;
656 BOOL bUpdateClipOrigin = FALSE;
658 TRACE(dc,"hwnd %04x, hrgnClip %04x, flags %08x\n",
659 hwnd, hrgnClip, (unsigned)flags);
661 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
663 /* fixup flags */
665 if (!(wndPtr->class->style & (CS_OWNDC | CS_CLASSDC))) flags |= DCX_CACHE;
667 if (flags & DCX_USESTYLE)
669 flags &= ~( DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
671 if( wndPtr->dwStyle & WS_CLIPSIBLINGS )
672 flags |= DCX_CLIPSIBLINGS;
674 if ( !(flags & DCX_WINDOW) )
676 if (wndPtr->class->style & CS_PARENTDC) flags |= DCX_PARENTCLIP;
678 if (wndPtr->dwStyle & WS_CLIPCHILDREN &&
679 !(wndPtr->dwStyle & WS_MINIMIZE) ) flags |= DCX_CLIPCHILDREN;
681 else flags |= DCX_CACHE;
684 if( flags & DCX_NOCLIPCHILDREN )
686 flags |= DCX_CACHE;
687 flags &= ~(DCX_PARENTCLIP | DCX_CLIPCHILDREN);
690 if (flags & DCX_WINDOW)
691 flags = (flags & ~DCX_CLIPCHILDREN) | DCX_CACHE;
693 if (!(wndPtr->dwStyle & WS_CHILD) || !wndPtr->parent )
694 flags &= ~DCX_PARENTCLIP;
695 else if( flags & DCX_PARENTCLIP )
697 flags |= DCX_CACHE;
698 if( !(flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) )
699 if( (wndPtr->dwStyle & WS_VISIBLE) && (wndPtr->parent->dwStyle & WS_VISIBLE) )
701 flags &= ~DCX_CLIPCHILDREN;
702 if( wndPtr->parent->dwStyle & WS_CLIPSIBLINGS )
703 flags |= DCX_CLIPSIBLINGS;
707 /* find a suitable DCE */
709 dcxFlags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
710 DCX_CACHE | DCX_WINDOW);
712 if (flags & DCX_CACHE)
714 DCE* dceEmpty;
715 DCE* dceUnused;
717 dceEmpty = dceUnused = NULL;
719 /* Strategy: First, we attempt to find a non-empty but unused DCE with
720 * compatible flags. Next, we look for an empty entry. If the cache is
721 * full we have to purge one of the unused entries.
724 for (dce = firstDCE; (dce); dce = dce->next)
726 if ((dce->DCXflags & (DCX_CACHE | DCX_DCEBUSY)) == DCX_CACHE )
728 dceUnused = dce;
730 if (dce->DCXflags & DCX_DCEEMPTY)
731 dceEmpty = dce;
732 else
733 if ((dce->hwndCurrent == hwnd) &&
734 ((dce->DCXflags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
735 DCX_CACHE | DCX_WINDOW | DCX_PARENTCLIP)) == dcxFlags))
737 TRACE(dc,"\tfound valid %08x dce [%04x], flags %08x\n",
738 (unsigned)dce, hwnd, (unsigned)dcxFlags );
739 bUpdateVisRgn = FALSE;
740 bUpdateClipOrigin = TRUE;
741 break;
746 if (!dce) dce = (dceEmpty) ? dceEmpty : dceUnused;
748 /* if there's no dce empty or unused, allocate a new one */
749 if (!dce)
751 dce = DCE_AllocDCE( 0, DCE_CACHE_DC );
754 else
756 dce = (wndPtr->class->style & CS_OWNDC) ? wndPtr->dce : wndPtr->class->dce;
757 if( dce->hwndCurrent == hwnd )
759 TRACE(dc,"\tskipping hVisRgn update\n");
760 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
762 if( (dce->DCXflags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN)) &&
763 (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN)) )
765 /* This is likely to be a nested BeginPaint(). */
767 if( dce->hClipRgn != hrgnClip )
769 FIXME(dc,"new hrgnClip[%04x] smashes the previous[%04x]\n",
770 hrgnClip, dce->hClipRgn );
771 DCE_DeleteClipRgn( dce );
773 else
774 RestoreVisRgn16(dce->hDC);
778 if (!dce)
780 hdc = 0;
781 goto END;
784 dce->hwndCurrent = hwnd;
785 dce->hClipRgn = 0;
786 dce->DCXflags = dcxFlags | (flags & DCX_WINDOWPAINT) | DCX_DCEBUSY;
787 hdc = dce->hDC;
789 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC )))
791 hdc = 0;
792 goto END;
794 bUpdateVisRgn = bUpdateVisRgn || (dc->w.flags & DC_DIRTY);
796 /* recompute visible region */
798 wndPtr->pDriver->pSetDrawable( wndPtr, dc, flags, bUpdateClipOrigin );
799 if( bUpdateVisRgn )
801 TRACE(dc,"updating visrgn for %08x dce, hwnd [%04x]\n", (unsigned)dce, hwnd);
803 if (flags & DCX_PARENTCLIP)
805 WND *parentPtr = WIN_LockWndPtr(wndPtr->parent);
807 if( wndPtr->dwStyle & WS_VISIBLE && !(parentPtr->dwStyle & WS_MINIMIZE) )
809 if( parentPtr->dwStyle & WS_CLIPSIBLINGS )
810 dcxFlags = DCX_CLIPSIBLINGS | (flags & ~(DCX_CLIPCHILDREN | DCX_WINDOW));
811 else
812 dcxFlags = flags & ~(DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW);
814 hrgnVisible = DCE_GetVisRgn( parentPtr->hwndSelf, dcxFlags,
815 wndPtr->hwndSelf, flags );
816 if( flags & DCX_WINDOW )
817 OffsetRgn( hrgnVisible, -wndPtr->rectWindow.left,
818 -wndPtr->rectWindow.top );
819 else
820 OffsetRgn( hrgnVisible, -wndPtr->rectClient.left,
821 -wndPtr->rectClient.top );
822 DCE_OffsetVisRgn( hdc, hrgnVisible );
824 else
825 hrgnVisible = CreateRectRgn( 0, 0, 0, 0 );
826 WIN_ReleaseWndPtr(parentPtr);
828 else
829 if ((hwnd == GetDesktopWindow()) && !DESKTOP_IsSingleWindow())
830 hrgnVisible = CreateRectRgn( 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN) );
831 else
833 hrgnVisible = DCE_GetVisRgn( hwnd, flags, 0, 0 );
834 DCE_OffsetVisRgn( hdc, hrgnVisible );
837 dc->w.flags &= ~DC_DIRTY;
838 dce->DCXflags &= ~DCX_DCEDIRTY;
839 SelectVisRgn16( hdc, hrgnVisible );
841 else
842 TRACE(dc,"no visrgn update %08x dce, hwnd [%04x]\n", (unsigned)dce, hwnd);
844 /* apply additional region operation (if any) */
846 if( flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN) )
848 if( !hrgnVisible ) hrgnVisible = CreateRectRgn( 0, 0, 0, 0 );
850 dce->DCXflags |= flags & (DCX_KEEPCLIPRGN | DCX_INTERSECTRGN | DCX_EXCLUDERGN);
851 dce->hClipRgn = hrgnClip;
853 TRACE(dc, "\tsaved VisRgn, clipRgn = %04x\n", hrgnClip);
855 SaveVisRgn16( hdc );
856 CombineRgn( hrgnVisible, hrgnClip, 0, RGN_COPY );
857 DCE_OffsetVisRgn( hdc, hrgnVisible );
858 CombineRgn( hrgnVisible, InquireVisRgn16( hdc ), hrgnVisible,
859 (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
860 SelectVisRgn16( hdc, hrgnVisible );
863 if( hrgnVisible ) DeleteObject( hrgnVisible );
865 TRACE(dc, "(%04x,%04x,0x%lx): returning %04x\n",
866 hwnd, hrgnClip, flags, hdc);
867 END:
868 WIN_ReleaseWndPtr(wndPtr);
869 return hdc;
873 /***********************************************************************
874 * GetDC16 (USER.66)
876 HDC16 WINAPI GetDC16( HWND16 hwnd )
878 return (HDC16)GetDC( hwnd );
882 /***********************************************************************
883 * GetDC32 (USER32.230)
884 * RETURNS
885 * :Handle to DC
886 * NULL: Failure
888 HDC WINAPI GetDC(
889 HWND hwnd /* handle of window */
891 if (!hwnd)
892 return GetDCEx( GetDesktopWindow(), 0, DCX_CACHE | DCX_WINDOW );
893 return GetDCEx( hwnd, 0, DCX_USESTYLE );
897 /***********************************************************************
898 * GetWindowDC16 (USER.67)
900 HDC16 WINAPI GetWindowDC16( HWND16 hwnd )
902 if (!hwnd) hwnd = GetDesktopWindow16();
903 return GetDCEx16( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
907 /***********************************************************************
908 * GetWindowDC32 (USER32.304)
910 HDC WINAPI GetWindowDC( HWND hwnd )
912 if (!hwnd) hwnd = GetDesktopWindow();
913 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
917 /***********************************************************************
918 * ReleaseDC16 (USER.68)
920 INT16 WINAPI ReleaseDC16( HWND16 hwnd, HDC16 hdc )
922 return (INT16)ReleaseDC( hwnd, hdc );
926 /***********************************************************************
927 * ReleaseDC32 (USER32.440)
929 * RETURNS
930 * 1: Success
931 * 0: Failure
933 INT WINAPI ReleaseDC(
934 HWND hwnd /* Handle of window - ignored */,
935 HDC hdc /* Handle of device context */
937 DCE * dce;
938 INT nRet = 0;
940 WIN_LockWnds();
941 dce = firstDCE;
943 TRACE(dc, "%04x %04x\n", hwnd, hdc );
945 while (dce && (dce->hDC != hdc)) dce = dce->next;
947 if ( dce )
948 if ( dce->DCXflags & DCX_DCEBUSY )
949 nRet = DCE_ReleaseDC( dce );
951 WIN_UnlockWnds();
953 return nRet;
956 /***********************************************************************
957 * DCHook (USER.362)
959 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
961 BOOL16 WINAPI DCHook16( HDC16 hDC, WORD code, DWORD data, LPARAM lParam )
963 BOOL retv = TRUE;
964 HRGN hVisRgn;
965 DCE *dce = (DCE *)data;
966 DC *dc;
967 WND *wndPtr;
969 TRACE(dc,"hDC = %04x, %i\n", hDC, code);
971 if (!dce) return 0;
972 assert(dce->hDC == hDC);
974 /* Grab the windows lock before doing anything else */
975 WIN_LockWnds();
977 switch( code )
979 case DCHC_INVALIDVISRGN:
981 /* GDI code calls this when it detects that the
982 * DC is dirty (usually after SetHookFlags()). This
983 * means that we have to recompute the visible region.
986 if( dce->DCXflags & DCX_DCEBUSY )
989 /* Update stale DC in DCX */
990 wndPtr = WIN_FindWndPtr( dce->hwndCurrent);
991 dc = (DC *) GDI_GetObjPtr( dce->hDC, DC_MAGIC);
992 if( dc && wndPtr)
993 wndPtr->pDriver->pSetDrawable( wndPtr, dc,dce->DCXflags,TRUE);
995 SetHookFlags16(hDC, DCHF_VALIDATEVISRGN);
996 hVisRgn = DCE_GetVisRgn(dce->hwndCurrent, dce->DCXflags, 0, 0);
998 TRACE(dc,"\tapplying saved clipRgn\n");
1000 /* clip this region with saved clipping region */
1002 if ( (dce->DCXflags & DCX_INTERSECTRGN && dce->hClipRgn != 1) ||
1003 ( dce->DCXflags & DCX_EXCLUDERGN && dce->hClipRgn) )
1006 if( (!dce->hClipRgn && dce->DCXflags & DCX_INTERSECTRGN) ||
1007 (dce->hClipRgn == 1 && dce->DCXflags & DCX_EXCLUDERGN) )
1008 SetRectRgn(hVisRgn,0,0,0,0);
1009 else
1010 CombineRgn(hVisRgn, hVisRgn, dce->hClipRgn,
1011 (dce->DCXflags & DCX_EXCLUDERGN)? RGN_DIFF:RGN_AND);
1013 dce->DCXflags &= ~DCX_DCEDIRTY;
1014 DCE_OffsetVisRgn( hDC, hVisRgn );
1015 SelectVisRgn16(hDC, hVisRgn);
1016 DeleteObject( hVisRgn );
1017 WIN_ReleaseWndPtr( wndPtr ); /* Release WIN_FindWndPtr lock */
1019 else /* non-fatal but shouldn't happen */
1020 WARN(dc, "DC is not in use!\n");
1021 break;
1023 case DCHC_DELETEDC:
1025 * Windows will not let you delete a DC that is busy
1026 * (between GetDC and ReleaseDC)
1029 if ( dce->DCXflags & DCX_DCEBUSY )
1031 WARN(dc, "Application trying to delete a busy DC\n");
1032 retv = FALSE;
1034 break;
1036 default:
1037 FIXME(dc,"unknown code\n");
1040 WIN_UnlockWnds(); /* Release the wnd lock */
1041 return retv;
1045 /**********************************************************************
1046 * WindowFromDC16 (USER.117)
1048 HWND16 WINAPI WindowFromDC16( HDC16 hDC )
1050 return (HWND16)WindowFromDC( hDC );
1054 /**********************************************************************
1055 * WindowFromDC32 (USER32.581)
1057 HWND WINAPI WindowFromDC( HDC hDC )
1059 DCE *dce;
1060 HWND hwnd;
1062 WIN_LockWnds();
1063 dce = firstDCE;
1065 while (dce && (dce->hDC != hDC)) dce = dce->next;
1067 hwnd = dce ? dce->hwndCurrent : 0;
1068 WIN_UnlockWnds();
1070 return hwnd;
1074 /***********************************************************************
1075 * LockWindowUpdate16 (USER.294)
1077 BOOL16 WINAPI LockWindowUpdate16( HWND16 hwnd )
1079 return LockWindowUpdate( hwnd );
1083 /***********************************************************************
1084 * LockWindowUpdate32 (USER32.378)
1086 BOOL WINAPI LockWindowUpdate( HWND hwnd )
1088 /* FIXME? DCX_LOCKWINDOWUPDATE is unimplemented */
1089 return TRUE;