Make access to the DCE list thread safe by ensuring we have acquired
[wine/dcerpc.git] / windows / dce.c
blobda386eadf27407b08d73d86c0740c4e4e34cf47c
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 "desktop.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 "sysmetrics.h"
29 #include "local.h"
30 #include "debug.h"
31 #include "wine/winuser16.h"
33 #define NB_DCE 5 /* Number of DCEs created at startup */
35 static DCE *firstDCE = 0;
36 static HDC defaultDCstate = 0;
38 static void DCE_DeleteClipRgn( DCE* );
39 static INT DCE_ReleaseDC( DCE* );
42 /***********************************************************************
43 * DCE_DumpCache
45 static void DCE_DumpCache(void)
47 DCE *dce;
49 WIN_LockWnds();
50 dce = firstDCE;
52 DUMP("DCE:\n");
53 while( dce )
55 DUMP("\t[0x%08x] hWnd 0x%04x, dcx %08x, %s %s\n",
56 (unsigned)dce, dce->hwndCurrent, (unsigned)dce->DCXflags,
57 (dce->DCXflags & DCX_CACHE) ? "Cache" : "Owned",
58 (dce->DCXflags & DCX_DCEBUSY) ? "InUse" : "" );
59 dce = dce->next;
62 WIN_UnlockWnds();
65 /***********************************************************************
66 * DCE_AllocDCE
68 * Allocate a new DCE.
70 DCE *DCE_AllocDCE( HWND hWnd, DCE_TYPE type )
72 DCE * dce;
73 WND* wnd;
75 if (!(dce = HeapAlloc( SystemHeap, 0, sizeof(DCE) ))) return NULL;
76 if (!(dce->hDC = CreateDC16( "DISPLAY", NULL, NULL, NULL )))
78 HeapFree( SystemHeap, 0, dce );
79 return 0;
82 wnd = WIN_FindWndPtr(hWnd);
84 /* store DCE handle in DC hook data field */
86 SetDCHook( dce->hDC, (FARPROC16)DCHook16, (DWORD)dce );
88 dce->hwndCurrent = hWnd;
89 dce->hClipRgn = 0;
90 dce->next = firstDCE;
91 firstDCE = dce;
93 if( type != DCE_CACHE_DC ) /* owned or class DC */
95 dce->DCXflags = DCX_DCEBUSY;
96 if( hWnd )
98 if( wnd->dwStyle & WS_CLIPCHILDREN ) dce->DCXflags |= DCX_CLIPCHILDREN;
99 if( wnd->dwStyle & WS_CLIPSIBLINGS ) dce->DCXflags |= DCX_CLIPSIBLINGS;
101 SetHookFlags16(dce->hDC,DCHF_INVALIDATEVISRGN);
103 else dce->DCXflags = DCX_CACHE | DCX_DCEEMPTY;
105 WIN_ReleaseWndPtr(wnd);
107 return dce;
111 /***********************************************************************
112 * DCE_FreeDCE
114 DCE* DCE_FreeDCE( DCE *dce )
116 DCE **ppDCE;
118 if (!dce) return NULL;
120 WIN_LockWnds();
122 ppDCE = &firstDCE;
124 while (*ppDCE && (*ppDCE != dce)) ppDCE = &(*ppDCE)->next;
125 if (*ppDCE == dce) *ppDCE = dce->next;
127 SetDCHook(dce->hDC, NULL, 0L);
129 DeleteDC( dce->hDC );
130 if( dce->hClipRgn && !(dce->DCXflags & DCX_KEEPCLIPRGN) )
131 DeleteObject(dce->hClipRgn);
132 HeapFree( SystemHeap, 0, dce );
134 WIN_UnlockWnds();
136 return *ppDCE;
139 /***********************************************************************
140 * DCE_FreeWindowDCE
142 * Remove owned DCE and reset unreleased cache DCEs.
144 void DCE_FreeWindowDCE( WND* pWnd )
146 DCE *pDCE;
148 WIN_LockWnds();
149 pDCE = firstDCE;
151 while( pDCE )
153 if( pDCE->hwndCurrent == pWnd->hwndSelf )
155 if( pDCE == pWnd->dce ) /* owned DCE */
157 pDCE = DCE_FreeDCE( pDCE );
158 pWnd->dce = NULL;
159 continue;
161 else
163 if(!(pDCE->DCXflags & DCX_CACHE) ) /* class DCE */
165 if( pDCE->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN) )
166 DCE_DeleteClipRgn( pDCE );
168 else if( pDCE->DCXflags & DCX_DCEBUSY ) /* shared cache DCE */
170 ERR(dc,"[%04x] GetDC() without ReleaseDC()!\n",
171 pWnd->hwndSelf);
172 DCE_ReleaseDC( pDCE );
175 pDCE->DCXflags &= DCX_CACHE;
176 pDCE->DCXflags |= DCX_DCEEMPTY;
177 pDCE->hwndCurrent = 0;
180 pDCE = pDCE->next;
183 WIN_UnlockWnds();
187 /***********************************************************************
188 * DCE_DeleteClipRgn
190 static void DCE_DeleteClipRgn( DCE* dce )
192 dce->DCXflags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN | DCX_WINDOWPAINT);
194 if( dce->DCXflags & DCX_KEEPCLIPRGN )
195 dce->DCXflags &= ~DCX_KEEPCLIPRGN;
196 else
197 if( dce->hClipRgn > 1 )
198 DeleteObject( dce->hClipRgn );
200 dce->hClipRgn = 0;
202 TRACE(dc,"\trestoring VisRgn\n");
204 RestoreVisRgn16(dce->hDC);
208 /***********************************************************************
209 * DCE_ReleaseDC
211 static INT DCE_ReleaseDC( DCE* dce )
213 if ((dce->DCXflags & (DCX_DCEEMPTY | DCX_DCEBUSY)) != DCX_DCEBUSY) return 0;
215 /* restore previous visible region */
217 if ((dce->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
218 (dce->DCXflags & (DCX_CACHE | DCX_WINDOWPAINT)) )
219 DCE_DeleteClipRgn( dce );
221 if (dce->DCXflags & DCX_CACHE)
223 SetDCState16( dce->hDC, defaultDCstate );
224 dce->DCXflags &= ~DCX_DCEBUSY;
225 if (dce->DCXflags & DCX_DCEDIRTY)
227 /* don't keep around invalidated entries
228 * because SetDCState() disables hVisRgn updates
229 * by removing dirty bit. */
231 dce->hwndCurrent = 0;
232 dce->DCXflags &= DCX_CACHE;
233 dce->DCXflags |= DCX_DCEEMPTY;
236 return 1;
240 /***********************************************************************
241 * DCE_InvalidateDCE
243 * It is called from SetWindowPos() - we have to mark as dirty all busy
244 * DCEs for windows that have pWnd->parent as an ansector and whose client
245 * rect intersects with specified update rectangle. In addition, pWnd->parent
246 * DCEs may need to be updated if DCX_CLIPCHILDREN flag is set.
248 BOOL DCE_InvalidateDCE(WND* pWnd, const RECT* pRectUpdate)
250 WND* wndScope = WIN_LockWndPtr(pWnd->parent);
251 WND *pDesktop = WIN_GetDesktop();
252 BOOL bRet = FALSE;
254 if( wndScope )
256 DCE *dce;
258 TRACE(dc,"scope hwnd = %04x, (%i,%i - %i,%i)\n",
259 wndScope->hwndSelf, pRectUpdate->left,pRectUpdate->top,
260 pRectUpdate->right,pRectUpdate->bottom);
261 if(TRACE_ON(dc))
262 DCE_DumpCache();
264 /* walk all DCEs and fixup non-empty entries */
266 for (dce = firstDCE; (dce); dce = dce->next)
268 if( !(dce->DCXflags & DCX_DCEEMPTY) )
270 WND* wndCurrent = WIN_FindWndPtr(dce->hwndCurrent);
272 if( wndCurrent )
274 WND* wnd = NULL;
275 INT xoffset = 0, yoffset = 0;
277 if( (wndCurrent == wndScope) && !(dce->DCXflags & DCX_CLIPCHILDREN) )
279 /* child window positions don't bother us */
280 WIN_ReleaseWndPtr(wndCurrent);
281 continue;
284 if( !Options.desktopGeometry && wndCurrent == pDesktop )
286 /* don't bother with fake desktop */
287 WIN_ReleaseWndPtr(wndCurrent);
288 continue;
291 /* check if DCE window is within the z-order scope */
293 for( wnd = WIN_LockWndPtr(wndCurrent); wnd; WIN_UpdateWndPtr(&wnd,wnd->parent))
295 if( wnd == wndScope )
297 RECT wndRect;
299 wndRect = wndCurrent->rectWindow;
301 OffsetRect( &wndRect, xoffset - wndCurrent->rectClient.left,
302 yoffset - wndCurrent->rectClient.top);
304 if (pWnd == wndCurrent ||
305 IntersectRect( &wndRect, &wndRect, pRectUpdate ))
307 if( !(dce->DCXflags & DCX_DCEBUSY) )
309 /* Don't bother with visible regions of unused DCEs */
311 TRACE(dc,"\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(dc,"\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 WIN_ReleaseWndPtr(wnd);
331 break;
333 xoffset += wnd->rectClient.left;
334 yoffset += wnd->rectClient.top;
337 WIN_ReleaseWndPtr(wndCurrent);
339 } /* dce list */
340 WIN_ReleaseWndPtr(wndScope);
342 WIN_ReleaseDesktop();
343 return bRet;
346 /***********************************************************************
347 * DCE_Init
349 void DCE_Init(void)
351 int i;
352 DCE * dce;
354 for (i = 0; i < NB_DCE; i++)
356 if (!(dce = DCE_AllocDCE( 0, DCE_CACHE_DC ))) return;
357 if (!defaultDCstate) defaultDCstate = GetDCState16( dce->hDC );
362 /***********************************************************************
363 * DCE_GetVisRect
365 * Calculate the visible rectangle of a window (i.e. the client or
366 * window area clipped by the client area of all ancestors) in the
367 * corresponding coordinates. Return FALSE if the visible region is empty.
369 static BOOL DCE_GetVisRect( WND *wndPtr, BOOL clientArea, RECT *lprect )
371 *lprect = clientArea ? wndPtr->rectClient : wndPtr->rectWindow;
373 if (wndPtr->dwStyle & WS_VISIBLE)
375 INT xoffset = lprect->left;
376 INT yoffset = lprect->top;
378 while (wndPtr->dwStyle & WS_CHILD)
380 wndPtr = WIN_LockWndPtr(wndPtr->parent);
382 if ( (wndPtr->dwStyle & (WS_ICONIC | WS_VISIBLE)) != WS_VISIBLE )
384 WIN_ReleaseWndPtr(wndPtr);
385 goto fail;
388 xoffset += wndPtr->rectClient.left;
389 yoffset += wndPtr->rectClient.top;
390 OffsetRect( lprect, wndPtr->rectClient.left,
391 wndPtr->rectClient.top );
393 if( (wndPtr->rectClient.left >= wndPtr->rectClient.right) ||
394 (wndPtr->rectClient.top >= wndPtr->rectClient.bottom) ||
395 (lprect->left >= wndPtr->rectClient.right) ||
396 (lprect->right <= wndPtr->rectClient.left) ||
397 (lprect->top >= wndPtr->rectClient.bottom) ||
398 (lprect->bottom <= wndPtr->rectClient.top) )
400 WIN_ReleaseWndPtr(wndPtr);
401 goto fail;
404 lprect->left = MAX( lprect->left, wndPtr->rectClient.left );
405 lprect->right = MIN( lprect->right, wndPtr->rectClient.right );
406 lprect->top = MAX( lprect->top, wndPtr->rectClient.top );
407 lprect->bottom = MIN( lprect->bottom, wndPtr->rectClient.bottom );
409 WIN_ReleaseWndPtr(wndPtr);
411 OffsetRect( lprect, -xoffset, -yoffset );
412 return TRUE;
415 fail:
416 SetRectEmpty( lprect );
417 return FALSE;
421 /***********************************************************************
422 * DCE_AddClipRects
424 * Go through the linked list of windows from pWndStart to pWndEnd,
425 * adding to the clip region the intersection of the target rectangle
426 * with an offset window rectangle.
428 static BOOL DCE_AddClipRects( WND *pWndStart, WND *pWndEnd,
429 HRGN hrgnClip, LPRECT lpRect, int x, int y )
431 RECT rect;
433 if( pWndStart->pDriver->pIsSelfClipping( pWndStart ) )
434 return TRUE; /* The driver itself will do the clipping */
436 for (WIN_LockWndPtr(pWndStart); pWndStart != pWndEnd; WIN_UpdateWndPtr(&pWndStart,pWndStart->next))
438 if( !(pWndStart->dwStyle & WS_VISIBLE) ) continue;
440 rect.left = pWndStart->rectWindow.left + x;
441 rect.top = pWndStart->rectWindow.top + y;
442 rect.right = pWndStart->rectWindow.right + x;
443 rect.bottom = pWndStart->rectWindow.bottom + y;
445 if( IntersectRect( &rect, &rect, lpRect ))
447 if(!REGION_UnionRectWithRgn( hrgnClip, &rect )) break;
450 WIN_ReleaseWndPtr(pWndStart);
451 return (pWndStart == pWndEnd);
455 /***********************************************************************
456 * DCE_GetVisRgn
458 * Return the visible region of a window, i.e. the client or window area
459 * clipped by the client area of all ancestors, and then optionally
460 * by siblings and children.
462 HRGN DCE_GetVisRgn( HWND hwnd, WORD flags, HWND hwndChild, WORD cflags )
464 HRGN hrgnVis = 0;
465 RECT rect;
466 WND *wndPtr = WIN_FindWndPtr( hwnd );
467 WND *childWnd = WIN_FindWndPtr( hwndChild );
469 /* Get visible rectangle and create a region with it. */
471 if (wndPtr && DCE_GetVisRect(wndPtr, !(flags & DCX_WINDOW), &rect))
473 if((hrgnVis = CreateRectRgnIndirect( &rect )))
475 HRGN hrgnClip = CreateRectRgn( 0, 0, 0, 0 );
476 INT xoffset, yoffset;
478 if( hrgnClip )
480 /* Compute obscured region for the visible rectangle by
481 * clipping children, siblings, and ancestors. Note that
482 * DCE_GetVisRect() returns a rectangle either in client
483 * or in window coordinates (for DCX_WINDOW request). */
485 if( (flags & DCX_CLIPCHILDREN) && wndPtr->child )
487 if( flags & DCX_WINDOW )
489 /* adjust offsets since child window rectangles are
490 * in client coordinates */
492 xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
493 yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
495 else
496 xoffset = yoffset = 0;
498 DCE_AddClipRects( wndPtr->child, NULL, hrgnClip,
499 &rect, xoffset, yoffset );
502 /* We may need to clip children of child window, if a window with PARENTDC
503 * class style and CLIPCHILDREN window style (like in Free Agent 16
504 * preference dialogs) gets here, we take the region for the parent window
505 * but apparently still need to clip the children of the child window... */
507 if( (cflags & DCX_CLIPCHILDREN) && childWnd && childWnd->child )
509 if( flags & DCX_WINDOW )
511 /* adjust offsets since child window rectangles are
512 * in client coordinates */
514 xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
515 yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
517 else
518 xoffset = yoffset = 0;
520 /* client coordinates of child window */
521 xoffset += childWnd->rectClient.left;
522 yoffset += childWnd->rectClient.top;
524 DCE_AddClipRects( childWnd->child, NULL, hrgnClip,
525 &rect, xoffset, yoffset );
528 /* sibling window rectangles are in client
529 * coordinates of the parent window */
531 if (flags & DCX_WINDOW)
533 xoffset = -wndPtr->rectWindow.left;
534 yoffset = -wndPtr->rectWindow.top;
536 else
538 xoffset = -wndPtr->rectClient.left;
539 yoffset = -wndPtr->rectClient.top;
542 if (flags & DCX_CLIPSIBLINGS && wndPtr->parent )
543 DCE_AddClipRects( wndPtr->parent->child,
544 wndPtr, hrgnClip, &rect, xoffset, yoffset );
546 /* Clip siblings of all ancestors that have the
547 * WS_CLIPSIBLINGS style
550 while (wndPtr->dwStyle & WS_CHILD)
552 WIN_UpdateWndPtr(&wndPtr,wndPtr->parent);
553 xoffset -= wndPtr->rectClient.left;
554 yoffset -= wndPtr->rectClient.top;
555 if(wndPtr->dwStyle & WS_CLIPSIBLINGS && wndPtr->parent)
557 DCE_AddClipRects( wndPtr->parent->child, wndPtr,
558 hrgnClip, &rect, xoffset, yoffset );
562 /* Now once we've got a jumbo clip region we have
563 * to substract it from the visible rectangle.
566 CombineRgn( hrgnVis, hrgnVis, hrgnClip, RGN_DIFF );
567 DeleteObject( hrgnClip );
569 else
571 DeleteObject( hrgnVis );
572 hrgnVis = 0;
576 else
577 hrgnVis = CreateRectRgn(0, 0, 0, 0); /* empty */
578 WIN_ReleaseWndPtr(wndPtr);
579 WIN_ReleaseWndPtr(childWnd);
580 return hrgnVis;
583 /***********************************************************************
584 * DCE_OffsetVisRgn
586 * Change region from DC-origin relative coordinates to screen coords.
589 static void DCE_OffsetVisRgn( HDC hDC, HRGN hVisRgn )
591 DC *dc;
592 if (!(dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC ))) return;
594 OffsetRgn( hVisRgn, dc->w.DCOrgX, dc->w.DCOrgY );
596 GDI_HEAP_UNLOCK( hDC );
599 /***********************************************************************
600 * DCE_ExcludeRgn
602 * Translate given region from the wnd client to the DC coordinates
603 * and add it to the clipping region.
605 INT16 DCE_ExcludeRgn( HDC hDC, WND* wnd, HRGN hRgn )
607 POINT pt = {0, 0};
608 DCE *dce = firstDCE;
610 while (dce && (dce->hDC != hDC)) dce = dce->next;
611 if( dce )
613 MapWindowPoints( wnd->hwndSelf, dce->hwndCurrent, &pt, 1);
614 if( dce->DCXflags & DCX_WINDOW )
616 wnd = WIN_FindWndPtr(dce->hwndCurrent);
617 pt.x += wnd->rectClient.left - wnd->rectWindow.left;
618 pt.y += wnd->rectClient.top - wnd->rectWindow.top;
619 WIN_ReleaseWndPtr(wnd);
622 else return ERROR;
623 OffsetRgn(hRgn, pt.x, pt.y);
625 return ExtSelectClipRgn( hDC, hRgn, RGN_DIFF );
628 /***********************************************************************
629 * GetDCEx16 (USER.359)
631 HDC16 WINAPI GetDCEx16( HWND16 hwnd, HRGN16 hrgnClip, DWORD flags )
633 return (HDC16)GetDCEx( hwnd, hrgnClip, flags );
637 /***********************************************************************
638 * GetDCEx32 (USER32.231)
640 * Unimplemented flags: DCX_LOCKWINDOWUPDATE
642 * FIXME: Full support for hrgnClip == 1 (alias for entire window).
644 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
646 HRGN hrgnVisible = 0;
647 HDC hdc = 0;
648 DCE * dce;
649 DC * dc;
650 WND * wndPtr;
651 DWORD dcxFlags = 0;
652 BOOL bUpdateVisRgn = TRUE;
653 BOOL bUpdateClipOrigin = FALSE;
655 TRACE(dc,"hwnd %04x, hrgnClip %04x, flags %08x\n",
656 hwnd, hrgnClip, (unsigned)flags);
658 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
660 /* fixup flags */
662 if (!(wndPtr->class->style & (CS_OWNDC | CS_CLASSDC))) flags |= DCX_CACHE;
664 if (flags & DCX_USESTYLE)
666 flags &= ~( DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
668 if( wndPtr->dwStyle & WS_CLIPSIBLINGS )
669 flags |= DCX_CLIPSIBLINGS;
671 if ( !(flags & DCX_WINDOW) )
673 if (wndPtr->class->style & CS_PARENTDC) flags |= DCX_PARENTCLIP;
675 if (wndPtr->dwStyle & WS_CLIPCHILDREN &&
676 !(wndPtr->dwStyle & WS_MINIMIZE) ) flags |= DCX_CLIPCHILDREN;
678 else flags |= DCX_CACHE;
681 if( flags & DCX_NOCLIPCHILDREN )
683 flags |= DCX_CACHE;
684 flags &= ~(DCX_PARENTCLIP | DCX_CLIPCHILDREN);
687 if (flags & DCX_WINDOW)
688 flags = (flags & ~DCX_CLIPCHILDREN) | DCX_CACHE;
690 if (!(wndPtr->dwStyle & WS_CHILD) || !wndPtr->parent )
691 flags &= ~DCX_PARENTCLIP;
692 else if( flags & DCX_PARENTCLIP )
694 flags |= DCX_CACHE;
695 if( !(flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) )
696 if( (wndPtr->dwStyle & WS_VISIBLE) && (wndPtr->parent->dwStyle & WS_VISIBLE) )
698 flags &= ~DCX_CLIPCHILDREN;
699 if( wndPtr->parent->dwStyle & WS_CLIPSIBLINGS )
700 flags |= DCX_CLIPSIBLINGS;
704 /* find a suitable DCE */
706 dcxFlags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
707 DCX_CACHE | DCX_WINDOW);
709 if (flags & DCX_CACHE)
711 DCE* dceEmpty;
712 DCE* dceUnused;
714 dceEmpty = dceUnused = NULL;
716 /* Strategy: First, we attempt to find a non-empty but unused DCE with
717 * compatible flags. Next, we look for an empty entry. If the cache is
718 * full we have to purge one of the unused entries.
721 for (dce = firstDCE; (dce); dce = dce->next)
723 if ((dce->DCXflags & (DCX_CACHE | DCX_DCEBUSY)) == DCX_CACHE )
725 dceUnused = dce;
727 if (dce->DCXflags & DCX_DCEEMPTY)
728 dceEmpty = dce;
729 else
730 if ((dce->hwndCurrent == hwnd) &&
731 ((dce->DCXflags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
732 DCX_CACHE | DCX_WINDOW | DCX_PARENTCLIP)) == dcxFlags))
734 TRACE(dc,"\tfound valid %08x dce [%04x], flags %08x\n",
735 (unsigned)dce, hwnd, (unsigned)dcxFlags );
736 bUpdateVisRgn = FALSE;
737 bUpdateClipOrigin = TRUE;
738 break;
743 if (!dce) dce = (dceEmpty) ? dceEmpty : dceUnused;
745 /* if there's no dce empty or unused, allocate a new one */
746 if (!dce)
748 dce = DCE_AllocDCE( 0, DCE_CACHE_DC );
751 else
753 dce = (wndPtr->class->style & CS_OWNDC) ? wndPtr->dce : wndPtr->class->dce;
754 if( dce->hwndCurrent == hwnd )
756 TRACE(dc,"\tskipping hVisRgn update\n");
757 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
759 if( (dce->DCXflags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN)) &&
760 (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN)) )
762 /* This is likely to be a nested BeginPaint(). */
764 if( dce->hClipRgn != hrgnClip )
766 FIXME(dc,"new hrgnClip[%04x] smashes the previous[%04x]\n",
767 hrgnClip, dce->hClipRgn );
768 DCE_DeleteClipRgn( dce );
770 else
771 RestoreVisRgn16(dce->hDC);
775 if (!dce)
777 hdc = 0;
778 goto END;
781 dce->hwndCurrent = hwnd;
782 dce->hClipRgn = 0;
783 dce->DCXflags = dcxFlags | (flags & DCX_WINDOWPAINT) | DCX_DCEBUSY;
784 hdc = dce->hDC;
786 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC )))
788 hdc = 0;
789 goto END;
791 bUpdateVisRgn = bUpdateVisRgn || (dc->w.flags & DC_DIRTY);
793 /* recompute visible region */
795 wndPtr->pDriver->pSetDrawable( wndPtr, dc, flags, bUpdateClipOrigin );
796 if( bUpdateVisRgn )
798 TRACE(dc,"updating visrgn for %08x dce, hwnd [%04x]\n", (unsigned)dce, hwnd);
800 if (flags & DCX_PARENTCLIP)
802 WND *parentPtr = WIN_LockWndPtr(wndPtr->parent);
804 if( wndPtr->dwStyle & WS_VISIBLE && !(parentPtr->dwStyle & WS_MINIMIZE) )
806 if( parentPtr->dwStyle & WS_CLIPSIBLINGS )
807 dcxFlags = DCX_CLIPSIBLINGS | (flags & ~(DCX_CLIPCHILDREN | DCX_WINDOW));
808 else
809 dcxFlags = flags & ~(DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW);
811 hrgnVisible = DCE_GetVisRgn( parentPtr->hwndSelf, dcxFlags,
812 wndPtr->hwndSelf, flags );
813 if( flags & DCX_WINDOW )
814 OffsetRgn( hrgnVisible, -wndPtr->rectWindow.left,
815 -wndPtr->rectWindow.top );
816 else
817 OffsetRgn( hrgnVisible, -wndPtr->rectClient.left,
818 -wndPtr->rectClient.top );
819 DCE_OffsetVisRgn( hdc, hrgnVisible );
821 else
822 hrgnVisible = CreateRectRgn( 0, 0, 0, 0 );
823 WIN_ReleaseWndPtr(parentPtr);
825 else
826 if ((hwnd == GetDesktopWindow()) && !DESKTOP_IsSingleWindow())
827 hrgnVisible = CreateRectRgn( 0, 0, SYSMETRICS_CXSCREEN, SYSMETRICS_CYSCREEN );
828 else
830 hrgnVisible = DCE_GetVisRgn( hwnd, flags, 0, 0 );
831 DCE_OffsetVisRgn( hdc, hrgnVisible );
834 dc->w.flags &= ~DC_DIRTY;
835 dce->DCXflags &= ~DCX_DCEDIRTY;
836 SelectVisRgn16( hdc, hrgnVisible );
838 else
839 TRACE(dc,"no visrgn update %08x dce, hwnd [%04x]\n", (unsigned)dce, hwnd);
841 /* apply additional region operation (if any) */
843 if( flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN) )
845 if( !hrgnVisible ) hrgnVisible = CreateRectRgn( 0, 0, 0, 0 );
847 dce->DCXflags |= flags & (DCX_KEEPCLIPRGN | DCX_INTERSECTRGN | DCX_EXCLUDERGN);
848 dce->hClipRgn = hrgnClip;
850 TRACE(dc, "\tsaved VisRgn, clipRgn = %04x\n", hrgnClip);
852 SaveVisRgn16( hdc );
853 CombineRgn( hrgnVisible, hrgnClip, 0, RGN_COPY );
854 DCE_OffsetVisRgn( hdc, hrgnVisible );
855 CombineRgn( hrgnVisible, InquireVisRgn16( hdc ), hrgnVisible,
856 (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
857 SelectVisRgn16( hdc, hrgnVisible );
860 if( hrgnVisible ) DeleteObject( hrgnVisible );
862 TRACE(dc, "(%04x,%04x,0x%lx): returning %04x\n",
863 hwnd, hrgnClip, flags, hdc);
864 END:
865 WIN_ReleaseWndPtr(wndPtr);
866 return hdc;
870 /***********************************************************************
871 * GetDC16 (USER.66)
873 HDC16 WINAPI GetDC16( HWND16 hwnd )
875 return (HDC16)GetDC( hwnd );
879 /***********************************************************************
880 * GetDC32 (USER32.230)
881 * RETURNS
882 * :Handle to DC
883 * NULL: Failure
885 HDC WINAPI GetDC(
886 HWND hwnd /* handle of window */
888 if (!hwnd)
889 return GetDCEx( GetDesktopWindow(), 0, DCX_CACHE | DCX_WINDOW );
890 return GetDCEx( hwnd, 0, DCX_USESTYLE );
894 /***********************************************************************
895 * GetWindowDC16 (USER.67)
897 HDC16 WINAPI GetWindowDC16( HWND16 hwnd )
899 if (!hwnd) hwnd = GetDesktopWindow16();
900 return GetDCEx16( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
904 /***********************************************************************
905 * GetWindowDC32 (USER32.304)
907 HDC WINAPI GetWindowDC( HWND hwnd )
909 if (!hwnd) hwnd = GetDesktopWindow();
910 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
914 /***********************************************************************
915 * ReleaseDC16 (USER.68)
917 INT16 WINAPI ReleaseDC16( HWND16 hwnd, HDC16 hdc )
919 return (INT16)ReleaseDC( hwnd, hdc );
923 /***********************************************************************
924 * ReleaseDC32 (USER32.440)
926 * RETURNS
927 * 1: Success
928 * 0: Failure
930 INT WINAPI ReleaseDC(
931 HWND hwnd /* Handle of window - ignored */,
932 HDC hdc /* Handle of device context */
934 DCE * dce;
935 INT nRet = 0;
937 WIN_LockWnds();
938 dce = firstDCE;
940 TRACE(dc, "%04x %04x\n", hwnd, hdc );
942 while (dce && (dce->hDC != hdc)) dce = dce->next;
944 if ( dce )
945 if ( dce->DCXflags & DCX_DCEBUSY )
946 nRet = DCE_ReleaseDC( dce );
948 WIN_UnlockWnds();
950 return nRet;
953 /***********************************************************************
954 * DCHook (USER.362)
956 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
958 BOOL16 WINAPI DCHook16( HDC16 hDC, WORD code, DWORD data, LPARAM lParam )
960 HRGN hVisRgn;
961 DCE *dce;
963 /* Grab the windows lock before doing anything else */
964 WIN_LockWnds();
966 dce = firstDCE;
968 TRACE(dc,"hDC = %04x, %i\n", hDC, code);
970 while (dce && (dce->hDC != hDC)) dce = dce->next;
972 if (!dce) goto END;
974 switch( code )
976 case DCHC_INVALIDVISRGN:
978 /* GDI code calls this when it detects that the
979 * DC is dirty (usually after SetHookFlags()). This
980 * means that we have to recompute the visible region.
983 if( dce->DCXflags & DCX_DCEBUSY )
985 SetHookFlags16(hDC, DCHF_VALIDATEVISRGN);
986 hVisRgn = DCE_GetVisRgn(dce->hwndCurrent, dce->DCXflags, 0, 0);
988 TRACE(dc,"\tapplying saved clipRgn\n");
990 /* clip this region with saved clipping region */
992 if ( (dce->DCXflags & DCX_INTERSECTRGN && dce->hClipRgn != 1) ||
993 ( dce->DCXflags & DCX_EXCLUDERGN && dce->hClipRgn) )
996 if( (!dce->hClipRgn && dce->DCXflags & DCX_INTERSECTRGN) ||
997 (dce->hClipRgn == 1 && dce->DCXflags & DCX_EXCLUDERGN) )
998 SetRectRgn(hVisRgn,0,0,0,0);
999 else
1000 CombineRgn(hVisRgn, hVisRgn, dce->hClipRgn,
1001 (dce->DCXflags & DCX_EXCLUDERGN)? RGN_DIFF:RGN_AND);
1003 dce->DCXflags &= ~DCX_DCEDIRTY;
1004 DCE_OffsetVisRgn( hDC, hVisRgn );
1005 SelectVisRgn16(hDC, hVisRgn);
1006 DeleteObject( hVisRgn );
1008 else /* non-fatal but shouldn't happen */
1009 WARN(dc, "DC is not in use!\n");
1010 break;
1012 case DCHC_DELETEDC: /* FIXME: ?? */
1013 break;
1015 default:
1016 FIXME(dc,"unknown code\n");
1019 END:
1020 WIN_UnlockWnds(); /* Release the wnd lock */
1021 return 0;
1025 /**********************************************************************
1026 * WindowFromDC16 (USER.117)
1028 HWND16 WINAPI WindowFromDC16( HDC16 hDC )
1030 return (HWND16)WindowFromDC( hDC );
1034 /**********************************************************************
1035 * WindowFromDC32 (USER32.581)
1037 HWND WINAPI WindowFromDC( HDC hDC )
1039 DCE *dce;
1040 HWND hwnd;
1042 WIN_LockWnds();
1043 dce = firstDCE;
1045 while (dce && (dce->hDC != hDC)) dce = dce->next;
1047 hwnd = dce ? dce->hwndCurrent : 0;
1048 WIN_UnlockWnds();
1050 return hwnd;
1054 /***********************************************************************
1055 * LockWindowUpdate16 (USER.294)
1057 BOOL16 WINAPI LockWindowUpdate16( HWND16 hwnd )
1059 return LockWindowUpdate( hwnd );
1063 /***********************************************************************
1064 * LockWindowUpdate32 (USER32.378)
1066 BOOL WINAPI LockWindowUpdate( HWND hwnd )
1068 /* FIXME? DCX_LOCKWINDOWUPDATE is unimplemented */
1069 return TRUE;