Remove unused outXform.
[wine/wine-kai.git] / windows / dce.c
blob6a1e70fa362cd747907b8e2f8f7f2294a752fb52
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 "win.h"
24 #include "gdi.h"
25 #include "region.h"
26 #include "heap.h"
27 #include "user.h"
28 #include "debugtools.h"
29 #include "windef.h"
30 #include "wingdi.h"
31 #include "wine/winbase16.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 DPRINTF("DCE:\n");
56 while( dce )
58 DPRINTF("\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 FARPROC16 hookProc;
76 DCE * dce;
77 WND* wnd;
79 if (!(dce = HeapAlloc( SystemHeap, 0, sizeof(DCE) ))) return NULL;
80 if (!(dce->hDC = CreateDCA( "DISPLAY", NULL, NULL, NULL )))
82 HeapFree( SystemHeap, 0, dce );
83 return 0;
86 wnd = WIN_FindWndPtr(hWnd);
88 /* store DCE handle in DC hook data field */
90 hookProc = GetProcAddress16( GetModuleHandle16("USER"), (LPCSTR)362 );
91 SetDCHook( dce->hDC, hookProc, (DWORD)dce );
93 dce->hwndCurrent = hWnd;
94 dce->hClipRgn = 0;
95 dce->next = firstDCE;
96 firstDCE = dce;
98 if( type != DCE_CACHE_DC ) /* owned or class DC */
100 dce->DCXflags = DCX_DCEBUSY;
101 if( hWnd )
103 if( wnd->dwStyle & WS_CLIPCHILDREN ) dce->DCXflags |= DCX_CLIPCHILDREN;
104 if( wnd->dwStyle & WS_CLIPSIBLINGS ) dce->DCXflags |= DCX_CLIPSIBLINGS;
106 SetHookFlags16(dce->hDC,DCHF_INVALIDATEVISRGN);
108 else dce->DCXflags = DCX_CACHE | DCX_DCEEMPTY;
110 WIN_ReleaseWndPtr(wnd);
112 return dce;
116 /***********************************************************************
117 * DCE_FreeDCE
119 DCE* DCE_FreeDCE( DCE *dce )
121 DCE **ppDCE;
123 if (!dce) return NULL;
125 WIN_LockWnds();
127 ppDCE = &firstDCE;
129 while (*ppDCE && (*ppDCE != dce)) ppDCE = &(*ppDCE)->next;
130 if (*ppDCE == dce) *ppDCE = dce->next;
132 SetDCHook(dce->hDC, NULL, 0L);
134 DeleteDC( dce->hDC );
135 if( dce->hClipRgn && !(dce->DCXflags & DCX_KEEPCLIPRGN) )
136 DeleteObject(dce->hClipRgn);
137 HeapFree( SystemHeap, 0, dce );
139 WIN_UnlockWnds();
141 return *ppDCE;
144 /***********************************************************************
145 * DCE_FreeWindowDCE
147 * Remove owned DCE and reset unreleased cache DCEs.
149 void DCE_FreeWindowDCE( WND* pWnd )
151 DCE *pDCE;
153 WIN_LockWnds();
154 pDCE = firstDCE;
156 while( pDCE )
158 if( pDCE->hwndCurrent == pWnd->hwndSelf )
160 if( pDCE == pWnd->dce ) /* owned or Class DCE*/
162 if (pWnd->clsStyle & CS_OWNDC) /* owned DCE*/
164 pDCE = DCE_FreeDCE( pDCE );
165 pWnd->dce = NULL;
166 continue;
168 else if( pDCE->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN) ) /* Class DCE*/
170 DCE_DeleteClipRgn( pDCE );
171 pDCE->hwndCurrent = 0;
174 else
176 if( pDCE->DCXflags & DCX_DCEBUSY ) /* shared cache DCE */
178 /* FIXME: AFAICS we are doing the right thing here so
179 * this should be a WARN. But this is best left as an ERR
180 * because the 'application error' is likely to come from
181 * another part of Wine (i.e. it's our fault after all).
182 * We should change this to WARN when Wine is more stable
183 * (for 1.0?).
185 ERR("[%04x] GetDC() without ReleaseDC()!\n",
186 pWnd->hwndSelf);
187 DCE_ReleaseDC( pDCE );
190 pDCE->DCXflags &= DCX_CACHE;
191 pDCE->DCXflags |= DCX_DCEEMPTY;
192 pDCE->hwndCurrent = 0;
195 pDCE = pDCE->next;
198 WIN_UnlockWnds();
202 /***********************************************************************
203 * DCE_DeleteClipRgn
205 static void DCE_DeleteClipRgn( DCE* dce )
207 dce->DCXflags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN | DCX_WINDOWPAINT);
209 if( dce->DCXflags & DCX_KEEPCLIPRGN )
210 dce->DCXflags &= ~DCX_KEEPCLIPRGN;
211 else
212 if( dce->hClipRgn > 1 )
213 DeleteObject( dce->hClipRgn );
215 dce->hClipRgn = 0;
217 TRACE("\trestoring VisRgn\n");
219 RestoreVisRgn16(dce->hDC);
223 /***********************************************************************
224 * DCE_ReleaseDC
226 static INT DCE_ReleaseDC( DCE* dce )
228 if ((dce->DCXflags & (DCX_DCEEMPTY | DCX_DCEBUSY)) != DCX_DCEBUSY) return 0;
230 /* restore previous visible region */
232 if ((dce->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
233 (dce->DCXflags & (DCX_CACHE | DCX_WINDOWPAINT)) )
234 DCE_DeleteClipRgn( dce );
236 if (dce->DCXflags & DCX_CACHE)
238 SetDCState16( dce->hDC, defaultDCstate );
239 dce->DCXflags &= ~DCX_DCEBUSY;
240 if (dce->DCXflags & DCX_DCEDIRTY)
242 /* don't keep around invalidated entries
243 * because SetDCState() disables hVisRgn updates
244 * by removing dirty bit. */
246 dce->hwndCurrent = 0;
247 dce->DCXflags &= DCX_CACHE;
248 dce->DCXflags |= DCX_DCEEMPTY;
251 return 1;
255 /***********************************************************************
256 * DCE_InvalidateDCE
258 * It is called from SetWindowPos() and EVENT_MapNotify - we have to
259 * mark as dirty all busy DCEs for windows that have pWnd->parent as
260 * an ancestor and whose client rect intersects with specified update
261 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
262 * DCX_CLIPCHILDREN flag is set. */
263 BOOL DCE_InvalidateDCE(WND* pWnd, const RECT* pRectUpdate)
265 WND* wndScope = WIN_LockWndPtr(pWnd->parent);
266 WND *pDesktop = WIN_GetDesktop();
267 BOOL bRet = FALSE;
269 if( wndScope )
271 DCE *dce;
273 TRACE("scope hwnd = %04x, (%i,%i - %i,%i)\n",
274 wndScope->hwndSelf, pRectUpdate->left,pRectUpdate->top,
275 pRectUpdate->right,pRectUpdate->bottom);
276 if(TRACE_ON(dc))
277 DCE_DumpCache();
279 /* walk all DCEs and fixup non-empty entries */
281 for (dce = firstDCE; (dce); dce = dce->next)
283 if( !(dce->DCXflags & DCX_DCEEMPTY) )
285 WND* wndCurrent = WIN_FindWndPtr(dce->hwndCurrent);
287 if( wndCurrent )
289 WND* wnd = NULL;
290 INT xoffset = 0, yoffset = 0;
292 if( (wndCurrent == wndScope) && !(dce->DCXflags & DCX_CLIPCHILDREN) )
294 /* child window positions don't bother us */
295 WIN_ReleaseWndPtr(wndCurrent);
296 continue;
299 if( !Options.desktopGeometry && wndCurrent == pDesktop )
301 /* don't bother with fake desktop */
302 WIN_ReleaseWndPtr(wndCurrent);
303 continue;
306 /* check if DCE window is within the z-order scope */
308 for( wnd = WIN_LockWndPtr(wndCurrent); wnd; WIN_UpdateWndPtr(&wnd,wnd->parent))
310 if( wnd == wndScope )
312 RECT wndRect;
314 wndRect = wndCurrent->rectWindow;
316 OffsetRect( &wndRect, xoffset - wndCurrent->rectClient.left,
317 yoffset - wndCurrent->rectClient.top);
319 if (pWnd == wndCurrent ||
320 IntersectRect( &wndRect, &wndRect, pRectUpdate ))
322 if( !(dce->DCXflags & DCX_DCEBUSY) )
324 /* Don't bother with visible regions of unused DCEs */
326 TRACE("\tpurged %08x dce [%04x]\n",
327 (unsigned)dce, wndCurrent->hwndSelf);
329 dce->hwndCurrent = 0;
330 dce->DCXflags &= DCX_CACHE;
331 dce->DCXflags |= DCX_DCEEMPTY;
333 else
335 /* Set dirty bits in the hDC and DCE structs */
337 TRACE("\tfixed up %08x dce [%04x]\n",
338 (unsigned)dce, wndCurrent->hwndSelf);
340 dce->DCXflags |= DCX_DCEDIRTY;
341 SetHookFlags16(dce->hDC, DCHF_INVALIDATEVISRGN);
342 bRet = TRUE;
345 WIN_ReleaseWndPtr(wnd);
346 break;
348 xoffset += wnd->rectClient.left;
349 yoffset += wnd->rectClient.top;
352 WIN_ReleaseWndPtr(wndCurrent);
354 } /* dce list */
355 WIN_ReleaseWndPtr(wndScope);
357 WIN_ReleaseDesktop();
358 return bRet;
361 /***********************************************************************
362 * DCE_Init
364 void DCE_Init(void)
366 int i;
367 DCE * dce;
369 for (i = 0; i < NB_DCE; i++)
371 if (!(dce = DCE_AllocDCE( 0, DCE_CACHE_DC ))) return;
372 if (!defaultDCstate) defaultDCstate = GetDCState16( dce->hDC );
377 /***********************************************************************
378 * DCE_GetVisRect
380 * Calculate the visible rectangle of a window (i.e. the client or
381 * window area clipped by the client area of all ancestors) in the
382 * corresponding coordinates. Return FALSE if the visible region is empty.
384 static BOOL DCE_GetVisRect( WND *wndPtr, BOOL clientArea, RECT *lprect )
386 *lprect = clientArea ? wndPtr->rectClient : wndPtr->rectWindow;
388 if (wndPtr->dwStyle & WS_VISIBLE)
390 INT xoffset = lprect->left;
391 INT yoffset = lprect->top;
393 while( !(wndPtr->flags & WIN_NATIVE) &&
394 ( wndPtr = WIN_LockWndPtr(wndPtr->parent)) )
396 if ( (wndPtr->dwStyle & (WS_ICONIC | WS_VISIBLE)) != WS_VISIBLE )
398 WIN_ReleaseWndPtr(wndPtr);
399 goto fail;
402 xoffset += wndPtr->rectClient.left;
403 yoffset += wndPtr->rectClient.top;
404 OffsetRect( lprect, wndPtr->rectClient.left,
405 wndPtr->rectClient.top );
407 if( (wndPtr->rectClient.left >= wndPtr->rectClient.right) ||
408 (wndPtr->rectClient.top >= wndPtr->rectClient.bottom) ||
409 (lprect->left >= wndPtr->rectClient.right) ||
410 (lprect->right <= wndPtr->rectClient.left) ||
411 (lprect->top >= wndPtr->rectClient.bottom) ||
412 (lprect->bottom <= wndPtr->rectClient.top) )
414 WIN_ReleaseWndPtr(wndPtr);
415 goto fail;
418 lprect->left = max( lprect->left, wndPtr->rectClient.left );
419 lprect->right = min( lprect->right, wndPtr->rectClient.right );
420 lprect->top = max( lprect->top, wndPtr->rectClient.top );
421 lprect->bottom = min( lprect->bottom, wndPtr->rectClient.bottom );
423 WIN_ReleaseWndPtr(wndPtr);
425 OffsetRect( lprect, -xoffset, -yoffset );
426 return TRUE;
429 fail:
430 SetRectEmpty( lprect );
431 return FALSE;
435 /***********************************************************************
436 * DCE_AddClipRects
438 * Go through the linked list of windows from pWndStart to pWndEnd,
439 * adding to the clip region the intersection of the target rectangle
440 * with an offset window rectangle.
442 static BOOL DCE_AddClipRects( WND *pWndStart, WND *pWndEnd,
443 HRGN hrgnClip, LPRECT lpRect, int x, int y )
445 RECT rect;
447 if( pWndStart->pDriver->pIsSelfClipping( pWndStart ) )
448 return TRUE; /* The driver itself will do the clipping */
450 for (WIN_LockWndPtr(pWndStart); (pWndStart && (pWndStart != pWndEnd)); WIN_UpdateWndPtr(&pWndStart,pWndStart->next))
452 if( !(pWndStart->dwStyle & WS_VISIBLE) ) continue;
454 rect.left = pWndStart->rectWindow.left + x;
455 rect.top = pWndStart->rectWindow.top + y;
456 rect.right = pWndStart->rectWindow.right + x;
457 rect.bottom = pWndStart->rectWindow.bottom + y;
459 if( IntersectRect( &rect, &rect, lpRect ))
461 if(!REGION_UnionRectWithRgn( hrgnClip, &rect )) break;
464 WIN_ReleaseWndPtr(pWndStart);
465 return (pWndStart == pWndEnd);
469 /***********************************************************************
470 * DCE_GetVisRgn
472 * Return the visible region of a window, i.e. the client or window area
473 * clipped by the client area of all ancestors, and then optionally
474 * by siblings and children.
476 HRGN DCE_GetVisRgn( HWND hwnd, WORD flags, HWND hwndChild, WORD cflags )
478 HRGN hrgnVis = 0;
479 RECT rect;
480 WND *wndPtr = WIN_FindWndPtr( hwnd );
481 WND *childWnd = WIN_FindWndPtr( hwndChild );
483 /* Get visible rectangle and create a region with it. */
485 if (wndPtr && DCE_GetVisRect(wndPtr, !(flags & DCX_WINDOW), &rect))
487 if((hrgnVis = CreateRectRgnIndirect( &rect )))
489 HRGN hrgnClip = CreateRectRgn( 0, 0, 0, 0 );
490 INT xoffset, yoffset;
492 if( hrgnClip )
494 /* Compute obscured region for the visible rectangle by
495 * clipping children, siblings, and ancestors. Note that
496 * DCE_GetVisRect() returns a rectangle either in client
497 * or in window coordinates (for DCX_WINDOW request). */
499 if( (flags & DCX_CLIPCHILDREN) && wndPtr->child )
501 if( flags & DCX_WINDOW )
503 /* adjust offsets since child window rectangles are
504 * in client coordinates */
506 xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
507 yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
509 else
510 xoffset = yoffset = 0;
512 DCE_AddClipRects( wndPtr->child, NULL, hrgnClip,
513 &rect, xoffset, yoffset );
516 /* We may need to clip children of child window, if a window with PARENTDC
517 * class style and CLIPCHILDREN window style (like in Free Agent 16
518 * preference dialogs) gets here, we take the region for the parent window
519 * but apparently still need to clip the children of the child window... */
521 if( (cflags & DCX_CLIPCHILDREN) && childWnd && childWnd->child )
523 if( flags & DCX_WINDOW )
525 /* adjust offsets since child window rectangles are
526 * in client coordinates */
528 xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
529 yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
531 else
532 xoffset = yoffset = 0;
534 /* client coordinates of child window */
535 xoffset += childWnd->rectClient.left;
536 yoffset += childWnd->rectClient.top;
538 DCE_AddClipRects( childWnd->child, NULL, hrgnClip,
539 &rect, xoffset, yoffset );
542 /* sibling window rectangles are in client
543 * coordinates of the parent window */
545 if (flags & DCX_WINDOW)
547 xoffset = -wndPtr->rectWindow.left;
548 yoffset = -wndPtr->rectWindow.top;
550 else
552 xoffset = -wndPtr->rectClient.left;
553 yoffset = -wndPtr->rectClient.top;
556 if (flags & DCX_CLIPSIBLINGS && wndPtr->parent )
557 DCE_AddClipRects( wndPtr->parent->child,
558 wndPtr, hrgnClip, &rect, xoffset, yoffset );
560 /* Clip siblings of all ancestors that have the
561 * WS_CLIPSIBLINGS style
564 while (wndPtr->dwStyle & WS_CHILD)
566 WIN_UpdateWndPtr(&wndPtr,wndPtr->parent);
567 xoffset -= wndPtr->rectClient.left;
568 yoffset -= wndPtr->rectClient.top;
569 if(wndPtr->dwStyle & WS_CLIPSIBLINGS && wndPtr->parent)
571 DCE_AddClipRects( wndPtr->parent->child, wndPtr,
572 hrgnClip, &rect, xoffset, yoffset );
576 /* Now once we've got a jumbo clip region we have
577 * to substract it from the visible rectangle.
580 CombineRgn( hrgnVis, hrgnVis, hrgnClip, RGN_DIFF );
581 DeleteObject( hrgnClip );
583 else
585 DeleteObject( hrgnVis );
586 hrgnVis = 0;
590 else
591 hrgnVis = CreateRectRgn(0, 0, 0, 0); /* empty */
592 WIN_ReleaseWndPtr(wndPtr);
593 WIN_ReleaseWndPtr(childWnd);
594 return hrgnVis;
597 /***********************************************************************
598 * DCE_OffsetVisRgn
600 * Change region from DC-origin relative coordinates to screen coords.
603 static void DCE_OffsetVisRgn( HDC hDC, HRGN hVisRgn )
605 DC *dc;
606 if (!(dc = DC_GetDCPtr( hDC ))) return;
608 OffsetRgn( hVisRgn, dc->DCOrgX, dc->DCOrgY );
610 GDI_ReleaseObj( hDC );
613 /***********************************************************************
614 * DCE_ExcludeRgn
616 * Translate given region from the wnd client to the DC coordinates
617 * and add it to the clipping region.
619 INT DCE_ExcludeRgn( HDC hDC, WND* wnd, HRGN hRgn )
621 POINT pt = {0, 0};
622 DCE *dce = firstDCE;
624 while (dce && (dce->hDC != hDC)) dce = dce->next;
625 if( dce )
627 MapWindowPoints( wnd->hwndSelf, dce->hwndCurrent, &pt, 1);
628 if( dce->DCXflags & DCX_WINDOW )
630 wnd = WIN_FindWndPtr(dce->hwndCurrent);
631 pt.x += wnd->rectClient.left - wnd->rectWindow.left;
632 pt.y += wnd->rectClient.top - wnd->rectWindow.top;
633 WIN_ReleaseWndPtr(wnd);
636 else return ERROR;
637 OffsetRgn(hRgn, pt.x, pt.y);
639 return ExtSelectClipRgn( hDC, hRgn, RGN_DIFF );
643 /***********************************************************************
644 * GetDCEx (USER.359)
646 HDC16 WINAPI GetDCEx16( HWND16 hwnd, HRGN16 hrgnClip, DWORD flags )
648 return (HDC16)GetDCEx( hwnd, hrgnClip, flags );
652 /***********************************************************************
653 * GetDCEx (USER32.@)
655 * Unimplemented flags: DCX_LOCKWINDOWUPDATE
657 * FIXME: Full support for hrgnClip == 1 (alias for entire window).
659 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
661 HRGN hrgnVisible = 0;
662 HDC hdc = 0;
663 DCE * dce;
664 DC * dc;
665 WND * wndPtr;
666 DWORD dcxFlags = 0;
667 BOOL bUpdateVisRgn = TRUE;
668 BOOL bUpdateClipOrigin = FALSE;
670 TRACE("hwnd %04x, hrgnClip %04x, flags %08x\n",
671 hwnd, hrgnClip, (unsigned)flags);
673 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
675 /* fixup flags */
677 if (!wndPtr->dce) flags |= DCX_CACHE;
679 if (flags & DCX_USESTYLE)
681 flags &= ~( DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
683 if( wndPtr->dwStyle & WS_CLIPSIBLINGS )
684 flags |= DCX_CLIPSIBLINGS;
686 if ( !(flags & DCX_WINDOW) )
688 if (wndPtr->clsStyle & CS_PARENTDC) flags |= DCX_PARENTCLIP;
690 if (wndPtr->dwStyle & WS_CLIPCHILDREN &&
691 !(wndPtr->dwStyle & WS_MINIMIZE) ) flags |= DCX_CLIPCHILDREN;
693 else flags |= DCX_CACHE;
696 if( flags & DCX_NOCLIPCHILDREN )
698 flags |= DCX_CACHE;
699 flags &= ~(DCX_PARENTCLIP | DCX_CLIPCHILDREN);
702 if (flags & DCX_WINDOW)
703 flags = (flags & ~DCX_CLIPCHILDREN) | DCX_CACHE;
705 if (!(wndPtr->dwStyle & WS_CHILD) || !wndPtr->parent )
706 flags &= ~DCX_PARENTCLIP;
707 else if( flags & DCX_PARENTCLIP )
709 flags |= DCX_CACHE;
710 if( !(flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) )
711 if( (wndPtr->dwStyle & WS_VISIBLE) && (wndPtr->parent->dwStyle & WS_VISIBLE) )
713 flags &= ~DCX_CLIPCHILDREN;
714 if( wndPtr->parent->dwStyle & WS_CLIPSIBLINGS )
715 flags |= DCX_CLIPSIBLINGS;
719 /* find a suitable DCE */
721 dcxFlags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
722 DCX_CACHE | DCX_WINDOW);
724 if (flags & DCX_CACHE)
726 DCE* dceEmpty;
727 DCE* dceUnused;
729 dceEmpty = dceUnused = NULL;
731 /* Strategy: First, we attempt to find a non-empty but unused DCE with
732 * compatible flags. Next, we look for an empty entry. If the cache is
733 * full we have to purge one of the unused entries.
736 for (dce = firstDCE; (dce); dce = dce->next)
738 if ((dce->DCXflags & (DCX_CACHE | DCX_DCEBUSY)) == DCX_CACHE )
740 dceUnused = dce;
742 if (dce->DCXflags & DCX_DCEEMPTY)
743 dceEmpty = dce;
744 else
745 if ((dce->hwndCurrent == hwnd) &&
746 ((dce->DCXflags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
747 DCX_CACHE | DCX_WINDOW | DCX_PARENTCLIP)) == dcxFlags))
749 TRACE("\tfound valid %08x dce [%04x], flags %08x\n",
750 (unsigned)dce, hwnd, (unsigned)dcxFlags );
751 bUpdateVisRgn = FALSE;
752 bUpdateClipOrigin = TRUE;
753 break;
758 if (!dce) dce = (dceEmpty) ? dceEmpty : dceUnused;
760 /* if there's no dce empty or unused, allocate a new one */
761 if (!dce)
763 dce = DCE_AllocDCE( 0, DCE_CACHE_DC );
766 else
768 dce = wndPtr->dce;
769 if( dce->hwndCurrent == hwnd )
771 TRACE("\tskipping hVisRgn update\n");
772 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
774 /* Abey - 16Jul99. to take care of the nested GetDC. first one
775 with DCX_EXCLUDERGN or DCX_INTERSECTRGN flags and the next
776 one with or without these flags. */
778 if(dce->DCXflags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN))
780 /* This is likely to be a nested BeginPaint().
781 or a BeginPaint() followed by a GetDC()*/
783 if( dce->hClipRgn != hrgnClip )
785 FIXME("new hrgnClip[%04x] smashes the previous[%04x]\n",
786 hrgnClip, dce->hClipRgn );
787 DCE_DeleteClipRgn( dce );
789 else
790 RestoreVisRgn16(dce->hDC);
794 if (!dce)
796 hdc = 0;
797 goto END;
800 dce->hwndCurrent = hwnd;
801 dce->hClipRgn = 0;
802 dce->DCXflags = dcxFlags | (flags & DCX_WINDOWPAINT) | DCX_DCEBUSY;
803 hdc = dce->hDC;
805 if (!(dc = DC_GetDCPtr( hdc )))
807 hdc = 0;
808 goto END;
810 bUpdateVisRgn = bUpdateVisRgn || (dc->flags & DC_DIRTY);
812 /* recompute visible region */
813 wndPtr->pDriver->pSetDrawable( wndPtr, hdc, flags, bUpdateClipOrigin );
814 dc->flags &= ~DC_DIRTY;
815 GDI_ReleaseObj( hdc );
817 if( bUpdateVisRgn )
819 TRACE("updating visrgn for %08x dce, hwnd [%04x]\n", (unsigned)dce, hwnd);
821 if (flags & DCX_PARENTCLIP)
823 WND *parentPtr = WIN_LockWndPtr(wndPtr->parent);
825 if( wndPtr->dwStyle & WS_VISIBLE && !(parentPtr->dwStyle & WS_MINIMIZE) )
827 if( parentPtr->dwStyle & WS_CLIPSIBLINGS )
828 dcxFlags = DCX_CLIPSIBLINGS | (flags & ~(DCX_CLIPCHILDREN | DCX_WINDOW));
829 else
830 dcxFlags = flags & ~(DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW);
832 hrgnVisible = DCE_GetVisRgn( parentPtr->hwndSelf, dcxFlags,
833 wndPtr->hwndSelf, flags );
834 if( flags & DCX_WINDOW )
835 OffsetRgn( hrgnVisible, -wndPtr->rectWindow.left,
836 -wndPtr->rectWindow.top );
837 else
838 OffsetRgn( hrgnVisible, -wndPtr->rectClient.left,
839 -wndPtr->rectClient.top );
840 DCE_OffsetVisRgn( hdc, hrgnVisible );
842 else
843 hrgnVisible = CreateRectRgn( 0, 0, 0, 0 );
844 WIN_ReleaseWndPtr(parentPtr);
846 else
847 if ((hwnd == GetDesktopWindow()) && !USER_Driver.pIsSingleWindow())
848 hrgnVisible = CreateRectRgn( 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN) );
849 else
851 hrgnVisible = DCE_GetVisRgn( hwnd, flags, 0, 0 );
852 DCE_OffsetVisRgn( hdc, hrgnVisible );
855 dce->DCXflags &= ~DCX_DCEDIRTY;
856 SelectVisRgn16( hdc, hrgnVisible );
858 else
859 TRACE("no visrgn update %08x dce, hwnd [%04x]\n", (unsigned)dce, hwnd);
861 /* apply additional region operation (if any) */
863 if( flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN) )
865 if( !hrgnVisible ) hrgnVisible = CreateRectRgn( 0, 0, 0, 0 );
867 dce->DCXflags |= flags & (DCX_KEEPCLIPRGN | DCX_INTERSECTRGN | DCX_EXCLUDERGN);
868 dce->hClipRgn = hrgnClip;
870 TRACE("\tsaved VisRgn, clipRgn = %04x\n", hrgnClip);
872 SaveVisRgn16( hdc );
873 CombineRgn( hrgnVisible, hrgnClip, 0, RGN_COPY );
874 DCE_OffsetVisRgn( hdc, hrgnVisible );
875 CombineRgn( hrgnVisible, InquireVisRgn16( hdc ), hrgnVisible,
876 (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
877 SelectVisRgn16( hdc, hrgnVisible );
880 if( hrgnVisible ) DeleteObject( hrgnVisible );
882 TRACE("(%04x,%04x,0x%lx): returning %04x\n",
883 hwnd, hrgnClip, flags, hdc);
884 END:
885 WIN_ReleaseWndPtr(wndPtr);
886 return hdc;
890 /***********************************************************************
891 * GetDC (USER.66)
893 HDC16 WINAPI GetDC16( HWND16 hwnd )
895 return (HDC16)GetDC( hwnd );
899 /***********************************************************************
900 * GetDC (USER32.@)
901 * RETURNS
902 * :Handle to DC
903 * NULL: Failure
905 HDC WINAPI GetDC(
906 HWND hwnd /* [in] handle of window */
908 if (!hwnd)
909 return GetDCEx( GetDesktopWindow(), 0, DCX_CACHE | DCX_WINDOW );
910 return GetDCEx( hwnd, 0, DCX_USESTYLE );
914 /***********************************************************************
915 * GetWindowDC (USER.67)
917 HDC16 WINAPI GetWindowDC16( HWND16 hwnd )
919 if (!hwnd) hwnd = GetDesktopWindow16();
920 return GetDCEx16( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
924 /***********************************************************************
925 * GetWindowDC (USER32.@)
927 HDC WINAPI GetWindowDC( HWND hwnd )
929 if (!hwnd) hwnd = GetDesktopWindow();
930 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
934 /***********************************************************************
935 * ReleaseDC (USER.68)
937 INT16 WINAPI ReleaseDC16( HWND16 hwnd, HDC16 hdc )
939 return (INT16)ReleaseDC( hwnd, hdc );
943 /***********************************************************************
944 * ReleaseDC (USER32.@)
946 * RETURNS
947 * 1: Success
948 * 0: Failure
950 INT WINAPI ReleaseDC(
951 HWND hwnd /* [in] Handle of window - ignored */,
952 HDC hdc /* [in] Handle of device context */
954 DCE * dce;
955 INT nRet = 0;
957 WIN_LockWnds();
958 dce = firstDCE;
960 TRACE("%04x %04x\n", hwnd, hdc );
962 while (dce && (dce->hDC != hdc)) dce = dce->next;
964 if ( dce )
965 if ( dce->DCXflags & DCX_DCEBUSY )
966 nRet = DCE_ReleaseDC( dce );
968 WIN_UnlockWnds();
970 return nRet;
973 /***********************************************************************
974 * DCHook (USER.362)
976 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
978 BOOL16 WINAPI DCHook16( HDC16 hDC, WORD code, DWORD data, LPARAM lParam )
980 BOOL retv = TRUE;
981 HRGN hVisRgn;
982 DCE *dce = (DCE *)data;
983 WND *wndPtr;
985 TRACE("hDC = %04x, %i\n", hDC, code);
987 if (!dce) return 0;
988 assert(dce->hDC == hDC);
990 /* Grab the windows lock before doing anything else */
991 WIN_LockWnds();
993 switch( code )
995 case DCHC_INVALIDVISRGN:
997 /* GDI code calls this when it detects that the
998 * DC is dirty (usually after SetHookFlags()). This
999 * means that we have to recompute the visible region.
1002 if( dce->DCXflags & DCX_DCEBUSY )
1005 /* Update stale DC in DCX */
1006 wndPtr = WIN_FindWndPtr( dce->hwndCurrent);
1007 if (wndPtr) wndPtr->pDriver->pSetDrawable( wndPtr, dce->hDC, dce->DCXflags, TRUE);
1009 SetHookFlags16(hDC, DCHF_VALIDATEVISRGN);
1010 hVisRgn = DCE_GetVisRgn(dce->hwndCurrent, dce->DCXflags, 0, 0);
1012 TRACE("\tapplying saved clipRgn\n");
1014 /* clip this region with saved clipping region */
1016 if ( (dce->DCXflags & DCX_INTERSECTRGN && dce->hClipRgn != 1) ||
1017 ( dce->DCXflags & DCX_EXCLUDERGN && dce->hClipRgn) )
1020 if( (!dce->hClipRgn && dce->DCXflags & DCX_INTERSECTRGN) ||
1021 (dce->hClipRgn == 1 && dce->DCXflags & DCX_EXCLUDERGN) )
1022 SetRectRgn(hVisRgn,0,0,0,0);
1023 else
1024 CombineRgn(hVisRgn, hVisRgn, dce->hClipRgn,
1025 (dce->DCXflags & DCX_EXCLUDERGN)? RGN_DIFF:RGN_AND);
1027 dce->DCXflags &= ~DCX_DCEDIRTY;
1028 DCE_OffsetVisRgn( hDC, hVisRgn );
1029 SelectVisRgn16(hDC, hVisRgn);
1030 DeleteObject( hVisRgn );
1031 WIN_ReleaseWndPtr( wndPtr ); /* Release WIN_FindWndPtr lock */
1033 else /* non-fatal but shouldn't happen */
1034 WARN("DC is not in use!\n");
1035 break;
1037 case DCHC_DELETEDC:
1039 * Windows will not let you delete a DC that is busy
1040 * (between GetDC and ReleaseDC)
1043 if ( dce->DCXflags & DCX_DCEBUSY )
1045 WARN("Application trying to delete a busy DC\n");
1046 retv = FALSE;
1048 break;
1050 default:
1051 FIXME("unknown code\n");
1054 WIN_UnlockWnds(); /* Release the wnd lock */
1055 return retv;
1059 /**********************************************************************
1060 * WindowFromDC (USER.117)
1062 HWND16 WINAPI WindowFromDC16( HDC16 hDC )
1064 return (HWND16)WindowFromDC( hDC );
1068 /**********************************************************************
1069 * WindowFromDC (USER32.@)
1071 HWND WINAPI WindowFromDC( HDC hDC )
1073 DCE *dce;
1074 HWND hwnd;
1076 WIN_LockWnds();
1077 dce = firstDCE;
1079 while (dce && (dce->hDC != hDC)) dce = dce->next;
1081 hwnd = dce ? dce->hwndCurrent : 0;
1082 WIN_UnlockWnds();
1084 return hwnd;
1088 /***********************************************************************
1089 * LockWindowUpdate (USER.294)
1091 BOOL16 WINAPI LockWindowUpdate16( HWND16 hwnd )
1093 return LockWindowUpdate( hwnd );
1097 /***********************************************************************
1098 * LockWindowUpdate (USER32.@)
1100 BOOL WINAPI LockWindowUpdate( HWND hwnd )
1102 FIXME("DCX_LOCKWINDOWUPDATE is unimplemented\n");
1103 return TRUE;