WSOCK32/WINSOCK uses SetLastError() to keep its own WSALastErrors,
[wine/dcerpc.git] / windows / dce.c
blob191fb4cb980ae28cce886c2f7dd298773484a822
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 "sysmetrics.h"
30 #include "local.h"
31 #include "debug.h"
32 #include "wine/winuser16.h"
34 DEFAULT_DEBUG_CHANNEL(dc)
36 #define NB_DCE 5 /* Number of DCEs created at startup */
38 static DCE *firstDCE = 0;
39 static HDC defaultDCstate = 0;
41 static void DCE_DeleteClipRgn( DCE* );
42 static INT DCE_ReleaseDC( DCE* );
45 /***********************************************************************
46 * DCE_DumpCache
48 static void DCE_DumpCache(void)
50 DCE *dce;
52 WIN_LockWnds();
53 dce = firstDCE;
55 DUMP("DCE:\n");
56 while( dce )
58 DUMP("\t[0x%08x] hWnd 0x%04x, dcx %08x, %s %s\n",
59 (unsigned)dce, dce->hwndCurrent, (unsigned)dce->DCXflags,
60 (dce->DCXflags & DCX_CACHE) ? "Cache" : "Owned",
61 (dce->DCXflags & DCX_DCEBUSY) ? "InUse" : "" );
62 dce = dce->next;
65 WIN_UnlockWnds();
68 /***********************************************************************
69 * DCE_AllocDCE
71 * Allocate a new DCE.
73 DCE *DCE_AllocDCE( HWND hWnd, DCE_TYPE type )
75 DCE * dce;
76 WND* wnd;
78 if (!(dce = HeapAlloc( SystemHeap, 0, sizeof(DCE) ))) return NULL;
79 if (!(dce->hDC = CreateDC16( "DISPLAY", NULL, NULL, NULL )))
81 HeapFree( SystemHeap, 0, dce );
82 return 0;
85 wnd = WIN_FindWndPtr(hWnd);
87 /* store DCE handle in DC hook data field */
89 SetDCHook( dce->hDC, (FARPROC16)DCHook16, (DWORD)dce );
91 dce->hwndCurrent = hWnd;
92 dce->hClipRgn = 0;
93 dce->next = firstDCE;
94 firstDCE = dce;
96 if( type != DCE_CACHE_DC ) /* owned or class DC */
98 dce->DCXflags = DCX_DCEBUSY;
99 if( hWnd )
101 if( wnd->dwStyle & WS_CLIPCHILDREN ) dce->DCXflags |= DCX_CLIPCHILDREN;
102 if( wnd->dwStyle & WS_CLIPSIBLINGS ) dce->DCXflags |= DCX_CLIPSIBLINGS;
104 SetHookFlags16(dce->hDC,DCHF_INVALIDATEVISRGN);
106 else dce->DCXflags = DCX_CACHE | DCX_DCEEMPTY;
108 WIN_ReleaseWndPtr(wnd);
110 return dce;
114 /***********************************************************************
115 * DCE_FreeDCE
117 DCE* DCE_FreeDCE( DCE *dce )
119 DCE **ppDCE;
121 if (!dce) return NULL;
123 WIN_LockWnds();
125 ppDCE = &firstDCE;
127 while (*ppDCE && (*ppDCE != dce)) ppDCE = &(*ppDCE)->next;
128 if (*ppDCE == dce) *ppDCE = dce->next;
130 SetDCHook(dce->hDC, NULL, 0L);
132 DeleteDC( dce->hDC );
133 if( dce->hClipRgn && !(dce->DCXflags & DCX_KEEPCLIPRGN) )
134 DeleteObject(dce->hClipRgn);
135 HeapFree( SystemHeap, 0, dce );
137 WIN_UnlockWnds();
139 return *ppDCE;
142 /***********************************************************************
143 * DCE_FreeWindowDCE
145 * Remove owned DCE and reset unreleased cache DCEs.
147 void DCE_FreeWindowDCE( WND* pWnd )
149 DCE *pDCE;
151 WIN_LockWnds();
152 pDCE = firstDCE;
154 while( pDCE )
156 if( pDCE->hwndCurrent == pWnd->hwndSelf )
158 if( pDCE == pWnd->dce ) /* owned DCE */
160 pDCE = DCE_FreeDCE( pDCE );
161 pWnd->dce = NULL;
162 continue;
164 else
166 if(!(pDCE->DCXflags & DCX_CACHE) ) /* class DCE */
168 if( pDCE->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN) )
169 DCE_DeleteClipRgn( pDCE );
171 else if( pDCE->DCXflags & DCX_DCEBUSY ) /* shared cache DCE */
173 ERR(dc,"[%04x] GetDC() without ReleaseDC()!\n",
174 pWnd->hwndSelf);
175 DCE_ReleaseDC( pDCE );
178 pDCE->DCXflags &= DCX_CACHE;
179 pDCE->DCXflags |= DCX_DCEEMPTY;
180 pDCE->hwndCurrent = 0;
183 pDCE = pDCE->next;
186 WIN_UnlockWnds();
190 /***********************************************************************
191 * DCE_DeleteClipRgn
193 static void DCE_DeleteClipRgn( DCE* dce )
195 dce->DCXflags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN | DCX_WINDOWPAINT);
197 if( dce->DCXflags & DCX_KEEPCLIPRGN )
198 dce->DCXflags &= ~DCX_KEEPCLIPRGN;
199 else
200 if( dce->hClipRgn > 1 )
201 DeleteObject( dce->hClipRgn );
203 dce->hClipRgn = 0;
205 TRACE(dc,"\trestoring VisRgn\n");
207 RestoreVisRgn16(dce->hDC);
211 /***********************************************************************
212 * DCE_ReleaseDC
214 static INT DCE_ReleaseDC( DCE* dce )
216 if ((dce->DCXflags & (DCX_DCEEMPTY | DCX_DCEBUSY)) != DCX_DCEBUSY) return 0;
218 /* restore previous visible region */
220 if ((dce->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
221 (dce->DCXflags & (DCX_CACHE | DCX_WINDOWPAINT)) )
222 DCE_DeleteClipRgn( dce );
224 if (dce->DCXflags & DCX_CACHE)
226 SetDCState16( dce->hDC, defaultDCstate );
227 dce->DCXflags &= ~DCX_DCEBUSY;
228 if (dce->DCXflags & DCX_DCEDIRTY)
230 /* don't keep around invalidated entries
231 * because SetDCState() disables hVisRgn updates
232 * by removing dirty bit. */
234 dce->hwndCurrent = 0;
235 dce->DCXflags &= DCX_CACHE;
236 dce->DCXflags |= DCX_DCEEMPTY;
239 return 1;
243 /***********************************************************************
244 * DCE_InvalidateDCE
246 * It is called from SetWindowPos() - we have to mark as dirty all busy
247 * DCEs for windows that have pWnd->parent as an ansector and whose client
248 * rect intersects with specified update rectangle. In addition, pWnd->parent
249 * DCEs may need to be updated if DCX_CLIPCHILDREN flag is set.
251 BOOL DCE_InvalidateDCE(WND* pWnd, const RECT* pRectUpdate)
253 WND* wndScope = WIN_LockWndPtr(pWnd->parent);
254 WND *pDesktop = WIN_GetDesktop();
255 BOOL bRet = FALSE;
257 if( wndScope )
259 DCE *dce;
261 TRACE(dc,"scope hwnd = %04x, (%i,%i - %i,%i)\n",
262 wndScope->hwndSelf, pRectUpdate->left,pRectUpdate->top,
263 pRectUpdate->right,pRectUpdate->bottom);
264 if(TRACE_ON(dc))
265 DCE_DumpCache();
267 /* walk all DCEs and fixup non-empty entries */
269 for (dce = firstDCE; (dce); dce = dce->next)
271 if( !(dce->DCXflags & DCX_DCEEMPTY) )
273 WND* wndCurrent = WIN_FindWndPtr(dce->hwndCurrent);
275 if( wndCurrent )
277 WND* wnd = NULL;
278 INT xoffset = 0, yoffset = 0;
280 if( (wndCurrent == wndScope) && !(dce->DCXflags & DCX_CLIPCHILDREN) )
282 /* child window positions don't bother us */
283 WIN_ReleaseWndPtr(wndCurrent);
284 continue;
287 if( !Options.desktopGeometry && wndCurrent == pDesktop )
289 /* don't bother with fake desktop */
290 WIN_ReleaseWndPtr(wndCurrent);
291 continue;
294 /* check if DCE window is within the z-order scope */
296 for( wnd = WIN_LockWndPtr(wndCurrent); wnd; WIN_UpdateWndPtr(&wnd,wnd->parent))
298 if( wnd == wndScope )
300 RECT wndRect;
302 wndRect = wndCurrent->rectWindow;
304 OffsetRect( &wndRect, xoffset - wndCurrent->rectClient.left,
305 yoffset - wndCurrent->rectClient.top);
307 if (pWnd == wndCurrent ||
308 IntersectRect( &wndRect, &wndRect, pRectUpdate ))
310 if( !(dce->DCXflags & DCX_DCEBUSY) )
312 /* Don't bother with visible regions of unused DCEs */
314 TRACE(dc,"\tpurged %08x dce [%04x]\n",
315 (unsigned)dce, wndCurrent->hwndSelf);
317 dce->hwndCurrent = 0;
318 dce->DCXflags &= DCX_CACHE;
319 dce->DCXflags |= DCX_DCEEMPTY;
321 else
323 /* Set dirty bits in the hDC and DCE structs */
325 TRACE(dc,"\tfixed up %08x dce [%04x]\n",
326 (unsigned)dce, wndCurrent->hwndSelf);
328 dce->DCXflags |= DCX_DCEDIRTY;
329 SetHookFlags16(dce->hDC, DCHF_INVALIDATEVISRGN);
330 bRet = TRUE;
333 WIN_ReleaseWndPtr(wnd);
334 break;
336 xoffset += wnd->rectClient.left;
337 yoffset += wnd->rectClient.top;
340 WIN_ReleaseWndPtr(wndCurrent);
342 } /* dce list */
343 WIN_ReleaseWndPtr(wndScope);
345 WIN_ReleaseDesktop();
346 return bRet;
349 /***********************************************************************
350 * DCE_Init
352 void DCE_Init(void)
354 int i;
355 DCE * dce;
357 for (i = 0; i < NB_DCE; i++)
359 if (!(dce = DCE_AllocDCE( 0, DCE_CACHE_DC ))) return;
360 if (!defaultDCstate) defaultDCstate = GetDCState16( dce->hDC );
365 /***********************************************************************
366 * DCE_GetVisRect
368 * Calculate the visible rectangle of a window (i.e. the client or
369 * window area clipped by the client area of all ancestors) in the
370 * corresponding coordinates. Return FALSE if the visible region is empty.
372 static BOOL DCE_GetVisRect( WND *wndPtr, BOOL clientArea, RECT *lprect )
374 *lprect = clientArea ? wndPtr->rectClient : wndPtr->rectWindow;
376 if (wndPtr->dwStyle & WS_VISIBLE)
378 INT xoffset = lprect->left;
379 INT yoffset = lprect->top;
381 while( !(wndPtr->flags & WIN_NATIVE) )
383 wndPtr = WIN_LockWndPtr(wndPtr->parent);
385 if ( (wndPtr->dwStyle & (WS_ICONIC | WS_VISIBLE)) != WS_VISIBLE )
387 WIN_ReleaseWndPtr(wndPtr);
388 goto fail;
391 xoffset += wndPtr->rectClient.left;
392 yoffset += wndPtr->rectClient.top;
393 OffsetRect( lprect, wndPtr->rectClient.left,
394 wndPtr->rectClient.top );
396 if( (wndPtr->rectClient.left >= wndPtr->rectClient.right) ||
397 (wndPtr->rectClient.top >= wndPtr->rectClient.bottom) ||
398 (lprect->left >= wndPtr->rectClient.right) ||
399 (lprect->right <= wndPtr->rectClient.left) ||
400 (lprect->top >= wndPtr->rectClient.bottom) ||
401 (lprect->bottom <= wndPtr->rectClient.top) )
403 WIN_ReleaseWndPtr(wndPtr);
404 goto fail;
407 lprect->left = MAX( lprect->left, wndPtr->rectClient.left );
408 lprect->right = MIN( lprect->right, wndPtr->rectClient.right );
409 lprect->top = MAX( lprect->top, wndPtr->rectClient.top );
410 lprect->bottom = MIN( lprect->bottom, wndPtr->rectClient.bottom );
412 WIN_ReleaseWndPtr(wndPtr);
414 OffsetRect( lprect, -xoffset, -yoffset );
415 return TRUE;
418 fail:
419 SetRectEmpty( lprect );
420 return FALSE;
424 /***********************************************************************
425 * DCE_AddClipRects
427 * Go through the linked list of windows from pWndStart to pWndEnd,
428 * adding to the clip region the intersection of the target rectangle
429 * with an offset window rectangle.
431 static BOOL DCE_AddClipRects( WND *pWndStart, WND *pWndEnd,
432 HRGN hrgnClip, LPRECT lpRect, int x, int y )
434 RECT rect;
436 if( pWndStart->pDriver->pIsSelfClipping( pWndStart ) )
437 return TRUE; /* The driver itself will do the clipping */
439 for (WIN_LockWndPtr(pWndStart); pWndStart != pWndEnd; WIN_UpdateWndPtr(&pWndStart,pWndStart->next))
441 if( !(pWndStart->dwStyle & WS_VISIBLE) ) continue;
443 rect.left = pWndStart->rectWindow.left + x;
444 rect.top = pWndStart->rectWindow.top + y;
445 rect.right = pWndStart->rectWindow.right + x;
446 rect.bottom = pWndStart->rectWindow.bottom + y;
448 if( IntersectRect( &rect, &rect, lpRect ))
450 if(!REGION_UnionRectWithRgn( hrgnClip, &rect )) break;
453 WIN_ReleaseWndPtr(pWndStart);
454 return (pWndStart == pWndEnd);
458 /***********************************************************************
459 * DCE_GetVisRgn
461 * Return the visible region of a window, i.e. the client or window area
462 * clipped by the client area of all ancestors, and then optionally
463 * by siblings and children.
465 HRGN DCE_GetVisRgn( HWND hwnd, WORD flags, HWND hwndChild, WORD cflags )
467 HRGN hrgnVis = 0;
468 RECT rect;
469 WND *wndPtr = WIN_FindWndPtr( hwnd );
470 WND *childWnd = WIN_FindWndPtr( hwndChild );
472 /* Get visible rectangle and create a region with it. */
474 if (wndPtr && DCE_GetVisRect(wndPtr, !(flags & DCX_WINDOW), &rect))
476 if((hrgnVis = CreateRectRgnIndirect( &rect )))
478 HRGN hrgnClip = CreateRectRgn( 0, 0, 0, 0 );
479 INT xoffset, yoffset;
481 if( hrgnClip )
483 /* Compute obscured region for the visible rectangle by
484 * clipping children, siblings, and ancestors. Note that
485 * DCE_GetVisRect() returns a rectangle either in client
486 * or in window coordinates (for DCX_WINDOW request). */
488 if( (flags & DCX_CLIPCHILDREN) && wndPtr->child )
490 if( flags & DCX_WINDOW )
492 /* adjust offsets since child window rectangles are
493 * in client coordinates */
495 xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
496 yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
498 else
499 xoffset = yoffset = 0;
501 DCE_AddClipRects( wndPtr->child, NULL, hrgnClip,
502 &rect, xoffset, yoffset );
505 /* We may need to clip children of child window, if a window with PARENTDC
506 * class style and CLIPCHILDREN window style (like in Free Agent 16
507 * preference dialogs) gets here, we take the region for the parent window
508 * but apparently still need to clip the children of the child window... */
510 if( (cflags & DCX_CLIPCHILDREN) && childWnd && childWnd->child )
512 if( flags & DCX_WINDOW )
514 /* adjust offsets since child window rectangles are
515 * in client coordinates */
517 xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
518 yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
520 else
521 xoffset = yoffset = 0;
523 /* client coordinates of child window */
524 xoffset += childWnd->rectClient.left;
525 yoffset += childWnd->rectClient.top;
527 DCE_AddClipRects( childWnd->child, NULL, hrgnClip,
528 &rect, xoffset, yoffset );
531 /* sibling window rectangles are in client
532 * coordinates of the parent window */
534 if (flags & DCX_WINDOW)
536 xoffset = -wndPtr->rectWindow.left;
537 yoffset = -wndPtr->rectWindow.top;
539 else
541 xoffset = -wndPtr->rectClient.left;
542 yoffset = -wndPtr->rectClient.top;
545 if (flags & DCX_CLIPSIBLINGS && wndPtr->parent )
546 DCE_AddClipRects( wndPtr->parent->child,
547 wndPtr, hrgnClip, &rect, xoffset, yoffset );
549 /* Clip siblings of all ancestors that have the
550 * WS_CLIPSIBLINGS style
553 while (wndPtr->dwStyle & WS_CHILD)
555 WIN_UpdateWndPtr(&wndPtr,wndPtr->parent);
556 xoffset -= wndPtr->rectClient.left;
557 yoffset -= wndPtr->rectClient.top;
558 if(wndPtr->dwStyle & WS_CLIPSIBLINGS && wndPtr->parent)
560 DCE_AddClipRects( wndPtr->parent->child, wndPtr,
561 hrgnClip, &rect, xoffset, yoffset );
565 /* Now once we've got a jumbo clip region we have
566 * to substract it from the visible rectangle.
569 CombineRgn( hrgnVis, hrgnVis, hrgnClip, RGN_DIFF );
570 DeleteObject( hrgnClip );
572 else
574 DeleteObject( hrgnVis );
575 hrgnVis = 0;
579 else
580 hrgnVis = CreateRectRgn(0, 0, 0, 0); /* empty */
581 WIN_ReleaseWndPtr(wndPtr);
582 WIN_ReleaseWndPtr(childWnd);
583 return hrgnVis;
586 /***********************************************************************
587 * DCE_OffsetVisRgn
589 * Change region from DC-origin relative coordinates to screen coords.
592 static void DCE_OffsetVisRgn( HDC hDC, HRGN hVisRgn )
594 DC *dc;
595 if (!(dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC ))) return;
597 OffsetRgn( hVisRgn, dc->w.DCOrgX, dc->w.DCOrgY );
599 GDI_HEAP_UNLOCK( hDC );
602 /***********************************************************************
603 * DCE_ExcludeRgn
605 * Translate given region from the wnd client to the DC coordinates
606 * and add it to the clipping region.
608 INT16 DCE_ExcludeRgn( HDC hDC, WND* wnd, HRGN hRgn )
610 POINT pt = {0, 0};
611 DCE *dce = firstDCE;
613 while (dce && (dce->hDC != hDC)) dce = dce->next;
614 if( dce )
616 MapWindowPoints( wnd->hwndSelf, dce->hwndCurrent, &pt, 1);
617 if( dce->DCXflags & DCX_WINDOW )
619 wnd = WIN_FindWndPtr(dce->hwndCurrent);
620 pt.x += wnd->rectClient.left - wnd->rectWindow.left;
621 pt.y += wnd->rectClient.top - wnd->rectWindow.top;
622 WIN_ReleaseWndPtr(wnd);
625 else return ERROR;
626 OffsetRgn(hRgn, pt.x, pt.y);
628 return ExtSelectClipRgn( hDC, hRgn, RGN_DIFF );
632 /***********************************************************************
633 * GetDCEx16 (USER.359)
635 HDC16 WINAPI GetDCEx16( HWND16 hwnd, HRGN16 hrgnClip, DWORD flags )
637 return (HDC16)GetDCEx( hwnd, hrgnClip, flags );
641 /***********************************************************************
642 * GetDCEx32 (USER32.231)
644 * Unimplemented flags: DCX_LOCKWINDOWUPDATE
646 * FIXME: Full support for hrgnClip == 1 (alias for entire window).
648 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
650 HRGN hrgnVisible = 0;
651 HDC hdc = 0;
652 DCE * dce;
653 DC * dc;
654 WND * wndPtr;
655 DWORD dcxFlags = 0;
656 BOOL bUpdateVisRgn = TRUE;
657 BOOL bUpdateClipOrigin = FALSE;
659 TRACE(dc,"hwnd %04x, hrgnClip %04x, flags %08x\n",
660 hwnd, hrgnClip, (unsigned)flags);
662 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
664 /* fixup flags */
666 if (!(wndPtr->class->style & (CS_OWNDC | CS_CLASSDC))) flags |= DCX_CACHE;
668 if (flags & DCX_USESTYLE)
670 flags &= ~( DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
672 if( wndPtr->dwStyle & WS_CLIPSIBLINGS )
673 flags |= DCX_CLIPSIBLINGS;
675 if ( !(flags & DCX_WINDOW) )
677 if (wndPtr->class->style & CS_PARENTDC) flags |= DCX_PARENTCLIP;
679 if (wndPtr->dwStyle & WS_CLIPCHILDREN &&
680 !(wndPtr->dwStyle & WS_MINIMIZE) ) flags |= DCX_CLIPCHILDREN;
682 else flags |= DCX_CACHE;
685 if( flags & DCX_NOCLIPCHILDREN )
687 flags |= DCX_CACHE;
688 flags &= ~(DCX_PARENTCLIP | DCX_CLIPCHILDREN);
691 if (flags & DCX_WINDOW)
692 flags = (flags & ~DCX_CLIPCHILDREN) | DCX_CACHE;
694 if (!(wndPtr->dwStyle & WS_CHILD) || !wndPtr->parent )
695 flags &= ~DCX_PARENTCLIP;
696 else if( flags & DCX_PARENTCLIP )
698 flags |= DCX_CACHE;
699 if( !(flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) )
700 if( (wndPtr->dwStyle & WS_VISIBLE) && (wndPtr->parent->dwStyle & WS_VISIBLE) )
702 flags &= ~DCX_CLIPCHILDREN;
703 if( wndPtr->parent->dwStyle & WS_CLIPSIBLINGS )
704 flags |= DCX_CLIPSIBLINGS;
708 /* find a suitable DCE */
710 dcxFlags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
711 DCX_CACHE | DCX_WINDOW);
713 if (flags & DCX_CACHE)
715 DCE* dceEmpty;
716 DCE* dceUnused;
718 dceEmpty = dceUnused = NULL;
720 /* Strategy: First, we attempt to find a non-empty but unused DCE with
721 * compatible flags. Next, we look for an empty entry. If the cache is
722 * full we have to purge one of the unused entries.
725 for (dce = firstDCE; (dce); dce = dce->next)
727 if ((dce->DCXflags & (DCX_CACHE | DCX_DCEBUSY)) == DCX_CACHE )
729 dceUnused = dce;
731 if (dce->DCXflags & DCX_DCEEMPTY)
732 dceEmpty = dce;
733 else
734 if ((dce->hwndCurrent == hwnd) &&
735 ((dce->DCXflags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
736 DCX_CACHE | DCX_WINDOW | DCX_PARENTCLIP)) == dcxFlags))
738 TRACE(dc,"\tfound valid %08x dce [%04x], flags %08x\n",
739 (unsigned)dce, hwnd, (unsigned)dcxFlags );
740 bUpdateVisRgn = FALSE;
741 bUpdateClipOrigin = TRUE;
742 break;
747 if (!dce) dce = (dceEmpty) ? dceEmpty : dceUnused;
749 /* if there's no dce empty or unused, allocate a new one */
750 if (!dce)
752 dce = DCE_AllocDCE( 0, DCE_CACHE_DC );
755 else
757 dce = (wndPtr->class->style & CS_OWNDC) ? wndPtr->dce : wndPtr->class->dce;
758 if( dce->hwndCurrent == hwnd )
760 TRACE(dc,"\tskipping hVisRgn update\n");
761 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
763 if( (dce->DCXflags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN)) &&
764 (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN)) )
766 /* This is likely to be a nested BeginPaint(). */
768 if( dce->hClipRgn != hrgnClip )
770 FIXME(dc,"new hrgnClip[%04x] smashes the previous[%04x]\n",
771 hrgnClip, dce->hClipRgn );
772 DCE_DeleteClipRgn( dce );
774 else
775 RestoreVisRgn16(dce->hDC);
779 if (!dce)
781 hdc = 0;
782 goto END;
785 dce->hwndCurrent = hwnd;
786 dce->hClipRgn = 0;
787 dce->DCXflags = dcxFlags | (flags & DCX_WINDOWPAINT) | DCX_DCEBUSY;
788 hdc = dce->hDC;
790 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC )))
792 hdc = 0;
793 goto END;
795 bUpdateVisRgn = bUpdateVisRgn || (dc->w.flags & DC_DIRTY);
797 /* recompute visible region */
799 wndPtr->pDriver->pSetDrawable( wndPtr, dc, flags, bUpdateClipOrigin );
800 if( bUpdateVisRgn )
802 TRACE(dc,"updating visrgn for %08x dce, hwnd [%04x]\n", (unsigned)dce, hwnd);
804 if (flags & DCX_PARENTCLIP)
806 WND *parentPtr = WIN_LockWndPtr(wndPtr->parent);
808 if( wndPtr->dwStyle & WS_VISIBLE && !(parentPtr->dwStyle & WS_MINIMIZE) )
810 if( parentPtr->dwStyle & WS_CLIPSIBLINGS )
811 dcxFlags = DCX_CLIPSIBLINGS | (flags & ~(DCX_CLIPCHILDREN | DCX_WINDOW));
812 else
813 dcxFlags = flags & ~(DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW);
815 hrgnVisible = DCE_GetVisRgn( parentPtr->hwndSelf, dcxFlags,
816 wndPtr->hwndSelf, flags );
817 if( flags & DCX_WINDOW )
818 OffsetRgn( hrgnVisible, -wndPtr->rectWindow.left,
819 -wndPtr->rectWindow.top );
820 else
821 OffsetRgn( hrgnVisible, -wndPtr->rectClient.left,
822 -wndPtr->rectClient.top );
823 DCE_OffsetVisRgn( hdc, hrgnVisible );
825 else
826 hrgnVisible = CreateRectRgn( 0, 0, 0, 0 );
827 WIN_ReleaseWndPtr(parentPtr);
829 else
830 if ((hwnd == GetDesktopWindow()) && !DESKTOP_IsSingleWindow())
831 hrgnVisible = CreateRectRgn( 0, 0, SYSMETRICS_CXSCREEN, SYSMETRICS_CYSCREEN );
832 else
834 hrgnVisible = DCE_GetVisRgn( hwnd, flags, 0, 0 );
835 DCE_OffsetVisRgn( hdc, hrgnVisible );
838 dc->w.flags &= ~DC_DIRTY;
839 dce->DCXflags &= ~DCX_DCEDIRTY;
840 SelectVisRgn16( hdc, hrgnVisible );
842 else
843 TRACE(dc,"no visrgn update %08x dce, hwnd [%04x]\n", (unsigned)dce, hwnd);
845 /* apply additional region operation (if any) */
847 if( flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN) )
849 if( !hrgnVisible ) hrgnVisible = CreateRectRgn( 0, 0, 0, 0 );
851 dce->DCXflags |= flags & (DCX_KEEPCLIPRGN | DCX_INTERSECTRGN | DCX_EXCLUDERGN);
852 dce->hClipRgn = hrgnClip;
854 TRACE(dc, "\tsaved VisRgn, clipRgn = %04x\n", hrgnClip);
856 SaveVisRgn16( hdc );
857 CombineRgn( hrgnVisible, hrgnClip, 0, RGN_COPY );
858 DCE_OffsetVisRgn( hdc, hrgnVisible );
859 CombineRgn( hrgnVisible, InquireVisRgn16( hdc ), hrgnVisible,
860 (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
861 SelectVisRgn16( hdc, hrgnVisible );
864 if( hrgnVisible ) DeleteObject( hrgnVisible );
866 TRACE(dc, "(%04x,%04x,0x%lx): returning %04x\n",
867 hwnd, hrgnClip, flags, hdc);
868 END:
869 WIN_ReleaseWndPtr(wndPtr);
870 return hdc;
874 /***********************************************************************
875 * GetDC16 (USER.66)
877 HDC16 WINAPI GetDC16( HWND16 hwnd )
879 return (HDC16)GetDC( hwnd );
883 /***********************************************************************
884 * GetDC32 (USER32.230)
885 * RETURNS
886 * :Handle to DC
887 * NULL: Failure
889 HDC WINAPI GetDC(
890 HWND hwnd /* handle of window */
892 if (!hwnd)
893 return GetDCEx( GetDesktopWindow(), 0, DCX_CACHE | DCX_WINDOW );
894 return GetDCEx( hwnd, 0, DCX_USESTYLE );
898 /***********************************************************************
899 * GetWindowDC16 (USER.67)
901 HDC16 WINAPI GetWindowDC16( HWND16 hwnd )
903 if (!hwnd) hwnd = GetDesktopWindow16();
904 return GetDCEx16( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
908 /***********************************************************************
909 * GetWindowDC32 (USER32.304)
911 HDC WINAPI GetWindowDC( HWND hwnd )
913 if (!hwnd) hwnd = GetDesktopWindow();
914 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
918 /***********************************************************************
919 * ReleaseDC16 (USER.68)
921 INT16 WINAPI ReleaseDC16( HWND16 hwnd, HDC16 hdc )
923 return (INT16)ReleaseDC( hwnd, hdc );
927 /***********************************************************************
928 * ReleaseDC32 (USER32.440)
930 * RETURNS
931 * 1: Success
932 * 0: Failure
934 INT WINAPI ReleaseDC(
935 HWND hwnd /* Handle of window - ignored */,
936 HDC hdc /* Handle of device context */
938 DCE * dce;
939 INT nRet = 0;
941 WIN_LockWnds();
942 dce = firstDCE;
944 TRACE(dc, "%04x %04x\n", hwnd, hdc );
946 while (dce && (dce->hDC != hdc)) dce = dce->next;
948 if ( dce )
949 if ( dce->DCXflags & DCX_DCEBUSY )
950 nRet = DCE_ReleaseDC( dce );
952 WIN_UnlockWnds();
954 return nRet;
957 /***********************************************************************
958 * DCHook (USER.362)
960 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
962 BOOL16 WINAPI DCHook16( HDC16 hDC, WORD code, DWORD data, LPARAM lParam )
964 BOOL retv = TRUE;
965 HRGN hVisRgn;
966 DCE *dce = (DCE *)data;
967 DC *dc;
968 WND *wndPtr;
970 TRACE(dc,"hDC = %04x, %i\n", hDC, code);
972 if (!dce) return 0;
973 assert(dce->hDC == hDC);
975 /* Grab the windows lock before doing anything else */
976 WIN_LockWnds();
978 switch( code )
980 case DCHC_INVALIDVISRGN:
982 /* GDI code calls this when it detects that the
983 * DC is dirty (usually after SetHookFlags()). This
984 * means that we have to recompute the visible region.
987 if( dce->DCXflags & DCX_DCEBUSY )
990 /* Update stale DC in DCX */
991 wndPtr = WIN_FindWndPtr( dce->hwndCurrent);
992 dc = (DC *) GDI_GetObjPtr( dce->hDC, DC_MAGIC);
993 if( dc && wndPtr)
994 wndPtr->pDriver->pSetDrawable( wndPtr, dc,dce->DCXflags,TRUE);
996 SetHookFlags16(hDC, DCHF_VALIDATEVISRGN);
997 hVisRgn = DCE_GetVisRgn(dce->hwndCurrent, dce->DCXflags, 0, 0);
999 TRACE(dc,"\tapplying saved clipRgn\n");
1001 /* clip this region with saved clipping region */
1003 if ( (dce->DCXflags & DCX_INTERSECTRGN && dce->hClipRgn != 1) ||
1004 ( dce->DCXflags & DCX_EXCLUDERGN && dce->hClipRgn) )
1007 if( (!dce->hClipRgn && dce->DCXflags & DCX_INTERSECTRGN) ||
1008 (dce->hClipRgn == 1 && dce->DCXflags & DCX_EXCLUDERGN) )
1009 SetRectRgn(hVisRgn,0,0,0,0);
1010 else
1011 CombineRgn(hVisRgn, hVisRgn, dce->hClipRgn,
1012 (dce->DCXflags & DCX_EXCLUDERGN)? RGN_DIFF:RGN_AND);
1014 dce->DCXflags &= ~DCX_DCEDIRTY;
1015 DCE_OffsetVisRgn( hDC, hVisRgn );
1016 SelectVisRgn16(hDC, hVisRgn);
1017 DeleteObject( hVisRgn );
1018 WIN_ReleaseWndPtr( wndPtr ); /* Release WIN_FindWndPtr lock */
1020 else /* non-fatal but shouldn't happen */
1021 WARN(dc, "DC is not in use!\n");
1022 break;
1024 case DCHC_DELETEDC:
1026 * Windows will not let you delete a DC that is busy
1027 * (between GetDC and ReleaseDC)
1030 if ( dce->DCXflags & DCX_DCEBUSY )
1032 WARN(dc, "Application trying to delete a busy DC\n");
1033 retv = FALSE;
1035 break;
1037 default:
1038 FIXME(dc,"unknown code\n");
1041 WIN_UnlockWnds(); /* Release the wnd lock */
1042 return retv;
1046 /**********************************************************************
1047 * WindowFromDC16 (USER.117)
1049 HWND16 WINAPI WindowFromDC16( HDC16 hDC )
1051 return (HWND16)WindowFromDC( hDC );
1055 /**********************************************************************
1056 * WindowFromDC32 (USER32.581)
1058 HWND WINAPI WindowFromDC( HDC hDC )
1060 DCE *dce;
1061 HWND hwnd;
1063 WIN_LockWnds();
1064 dce = firstDCE;
1066 while (dce && (dce->hDC != hDC)) dce = dce->next;
1068 hwnd = dce ? dce->hwndCurrent : 0;
1069 WIN_UnlockWnds();
1071 return hwnd;
1075 /***********************************************************************
1076 * LockWindowUpdate16 (USER.294)
1078 BOOL16 WINAPI LockWindowUpdate16( HWND16 hwnd )
1080 return LockWindowUpdate( hwnd );
1084 /***********************************************************************
1085 * LockWindowUpdate32 (USER32.378)
1087 BOOL WINAPI LockWindowUpdate( HWND hwnd )
1089 /* FIXME? DCX_LOCKWINDOWUPDATE is unimplemented */
1090 return TRUE;