Use poll() on the client-side during server waits to implement
[wine.git] / windows / dce.c
blob34b7451ebf4a25f6ae7657c7b53dbea3e19327d4
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 "user.h"
27 #include "debugtools.h"
28 #include "windef.h"
29 #include "wingdi.h"
30 #include "wine/winbase16.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 DPRINTF("DCE:\n");
55 while( dce )
57 DPRINTF("\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 FARPROC16 hookProc;
75 DCE * dce;
76 WND* wnd;
78 if (!(dce = HeapAlloc( GetProcessHeap(), 0, sizeof(DCE) ))) return NULL;
79 if (!(dce->hDC = CreateDCA( "DISPLAY", NULL, NULL, NULL )))
81 HeapFree( GetProcessHeap(), 0, dce );
82 return 0;
85 wnd = WIN_FindWndPtr(hWnd);
87 /* store DCE handle in DC hook data field */
89 hookProc = GetProcAddress16( GetModuleHandle16("USER"), (LPCSTR)362 );
90 SetDCHook( dce->hDC, hookProc, (DWORD)dce );
92 dce->hwndCurrent = hWnd;
93 dce->hClipRgn = 0;
94 dce->next = firstDCE;
95 firstDCE = dce;
97 if( type != DCE_CACHE_DC ) /* owned or class DC */
99 dce->DCXflags = DCX_DCEBUSY;
100 if( hWnd )
102 if( wnd->dwStyle & WS_CLIPCHILDREN ) dce->DCXflags |= DCX_CLIPCHILDREN;
103 if( wnd->dwStyle & WS_CLIPSIBLINGS ) dce->DCXflags |= DCX_CLIPSIBLINGS;
105 SetHookFlags16(dce->hDC,DCHF_INVALIDATEVISRGN);
107 else dce->DCXflags = DCX_CACHE | DCX_DCEEMPTY;
109 WIN_ReleaseWndPtr(wnd);
111 return dce;
115 /***********************************************************************
116 * DCE_FreeDCE
118 DCE* DCE_FreeDCE( DCE *dce )
120 DCE **ppDCE;
122 if (!dce) return NULL;
124 WIN_LockWnds();
126 ppDCE = &firstDCE;
128 while (*ppDCE && (*ppDCE != dce)) ppDCE = &(*ppDCE)->next;
129 if (*ppDCE == dce) *ppDCE = dce->next;
131 SetDCHook(dce->hDC, NULL, 0L);
133 DeleteDC( dce->hDC );
134 if( dce->hClipRgn && !(dce->DCXflags & DCX_KEEPCLIPRGN) )
135 DeleteObject(dce->hClipRgn);
136 HeapFree( GetProcessHeap(), 0, dce );
138 WIN_UnlockWnds();
140 return *ppDCE;
143 /***********************************************************************
144 * DCE_FreeWindowDCE
146 * Remove owned DCE and reset unreleased cache DCEs.
148 void DCE_FreeWindowDCE( WND* pWnd )
150 DCE *pDCE;
152 WIN_LockWnds();
153 pDCE = firstDCE;
155 while( pDCE )
157 if( pDCE->hwndCurrent == pWnd->hwndSelf )
159 if( pDCE == pWnd->dce ) /* owned or Class DCE*/
161 if (pWnd->clsStyle & CS_OWNDC) /* owned DCE*/
163 pDCE = DCE_FreeDCE( pDCE );
164 pWnd->dce = NULL;
165 continue;
167 else if( pDCE->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN) ) /* Class DCE*/
169 DCE_DeleteClipRgn( pDCE );
170 pDCE->hwndCurrent = 0;
173 else
175 if( pDCE->DCXflags & DCX_DCEBUSY ) /* shared cache DCE */
177 /* FIXME: AFAICS we are doing the right thing here so
178 * this should be a WARN. But this is best left as an ERR
179 * because the 'application error' is likely to come from
180 * another part of Wine (i.e. it's our fault after all).
181 * We should change this to WARN when Wine is more stable
182 * (for 1.0?).
184 ERR("[%04x] GetDC() without ReleaseDC()!\n",
185 pWnd->hwndSelf);
186 DCE_ReleaseDC( pDCE );
189 pDCE->DCXflags &= DCX_CACHE;
190 pDCE->DCXflags |= DCX_DCEEMPTY;
191 pDCE->hwndCurrent = 0;
194 pDCE = pDCE->next;
197 WIN_UnlockWnds();
201 /***********************************************************************
202 * DCE_DeleteClipRgn
204 static void DCE_DeleteClipRgn( DCE* dce )
206 dce->DCXflags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN | DCX_WINDOWPAINT);
208 if( dce->DCXflags & DCX_KEEPCLIPRGN )
209 dce->DCXflags &= ~DCX_KEEPCLIPRGN;
210 else
211 if( dce->hClipRgn > 1 )
212 DeleteObject( dce->hClipRgn );
214 dce->hClipRgn = 0;
216 TRACE("\trestoring VisRgn\n");
218 RestoreVisRgn16(dce->hDC);
222 /***********************************************************************
223 * DCE_ReleaseDC
225 static INT DCE_ReleaseDC( DCE* dce )
227 if ((dce->DCXflags & (DCX_DCEEMPTY | DCX_DCEBUSY)) != DCX_DCEBUSY) return 0;
229 /* restore previous visible region */
231 if ((dce->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
232 (dce->DCXflags & (DCX_CACHE | DCX_WINDOWPAINT)) )
233 DCE_DeleteClipRgn( dce );
235 if (dce->DCXflags & DCX_CACHE)
237 SetDCState16( dce->hDC, defaultDCstate );
238 dce->DCXflags &= ~DCX_DCEBUSY;
239 if (dce->DCXflags & DCX_DCEDIRTY)
241 /* don't keep around invalidated entries
242 * because SetDCState() disables hVisRgn updates
243 * by removing dirty bit. */
245 dce->hwndCurrent = 0;
246 dce->DCXflags &= DCX_CACHE;
247 dce->DCXflags |= DCX_DCEEMPTY;
250 return 1;
254 /***********************************************************************
255 * DCE_InvalidateDCE
257 * It is called from SetWindowPos() and EVENT_MapNotify - we have to
258 * mark as dirty all busy DCEs for windows that have pWnd->parent as
259 * an ancestor and whose client rect intersects with specified update
260 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
261 * DCX_CLIPCHILDREN flag is set. */
262 BOOL DCE_InvalidateDCE(WND* pWnd, const RECT* pRectUpdate)
264 WND* wndScope = WIN_LockWndPtr(pWnd->parent);
265 WND *pDesktop = WIN_GetDesktop();
266 BOOL bRet = FALSE;
268 if( wndScope )
270 DCE *dce;
272 TRACE("scope hwnd = %04x, (%i,%i - %i,%i)\n",
273 wndScope->hwndSelf, pRectUpdate->left,pRectUpdate->top,
274 pRectUpdate->right,pRectUpdate->bottom);
275 if(TRACE_ON(dc))
276 DCE_DumpCache();
278 /* walk all DCEs and fixup non-empty entries */
280 for (dce = firstDCE; (dce); dce = dce->next)
282 if( !(dce->DCXflags & DCX_DCEEMPTY) )
284 WND* wndCurrent = WIN_FindWndPtr(dce->hwndCurrent);
286 if( wndCurrent )
288 WND* wnd = NULL;
289 INT xoffset = 0, yoffset = 0;
291 if( (wndCurrent == wndScope) && !(dce->DCXflags & DCX_CLIPCHILDREN) )
293 /* child window positions don't bother us */
294 WIN_ReleaseWndPtr(wndCurrent);
295 continue;
298 if( !Options.desktopGeometry && wndCurrent == pDesktop )
300 /* don't bother with fake desktop */
301 WIN_ReleaseWndPtr(wndCurrent);
302 continue;
305 /* check if DCE window is within the z-order scope */
307 for( wnd = WIN_LockWndPtr(wndCurrent); wnd; WIN_UpdateWndPtr(&wnd,wnd->parent))
309 if( wnd == wndScope )
311 RECT wndRect;
313 wndRect = wndCurrent->rectWindow;
315 OffsetRect( &wndRect, xoffset - wndCurrent->rectClient.left,
316 yoffset - wndCurrent->rectClient.top);
318 if (pWnd == wndCurrent ||
319 IntersectRect( &wndRect, &wndRect, pRectUpdate ))
321 if( !(dce->DCXflags & DCX_DCEBUSY) )
323 /* Don't bother with visible regions of unused DCEs */
325 TRACE("\tpurged %08x dce [%04x]\n",
326 (unsigned)dce, wndCurrent->hwndSelf);
328 dce->hwndCurrent = 0;
329 dce->DCXflags &= DCX_CACHE;
330 dce->DCXflags |= DCX_DCEEMPTY;
332 else
334 /* Set dirty bits in the hDC and DCE structs */
336 TRACE("\tfixed up %08x dce [%04x]\n",
337 (unsigned)dce, wndCurrent->hwndSelf);
339 dce->DCXflags |= DCX_DCEDIRTY;
340 SetHookFlags16(dce->hDC, DCHF_INVALIDATEVISRGN);
341 bRet = TRUE;
344 WIN_ReleaseWndPtr(wnd);
345 break;
347 xoffset += wnd->rectClient.left;
348 yoffset += wnd->rectClient.top;
351 WIN_ReleaseWndPtr(wndCurrent);
353 } /* dce list */
354 WIN_ReleaseWndPtr(wndScope);
356 WIN_ReleaseDesktop();
357 return bRet;
360 /***********************************************************************
361 * DCE_Init
363 void DCE_Init(void)
365 int i;
366 DCE * dce;
368 for (i = 0; i < NB_DCE; i++)
370 if (!(dce = DCE_AllocDCE( 0, DCE_CACHE_DC ))) return;
371 if (!defaultDCstate) defaultDCstate = GetDCState16( dce->hDC );
376 /***********************************************************************
377 * DCE_GetVisRect
379 * Calculate the visible rectangle of a window (i.e. the client or
380 * window area clipped by the client area of all ancestors) in the
381 * corresponding coordinates. Return FALSE if the visible region is empty.
383 static BOOL DCE_GetVisRect( WND *wndPtr, BOOL clientArea, RECT *lprect )
385 *lprect = clientArea ? wndPtr->rectClient : wndPtr->rectWindow;
387 if (wndPtr->dwStyle & WS_VISIBLE)
389 INT xoffset = lprect->left;
390 INT yoffset = lprect->top;
392 while( !(wndPtr->flags & WIN_NATIVE) &&
393 ( wndPtr = WIN_LockWndPtr(wndPtr->parent)) )
395 if ( (wndPtr->dwStyle & (WS_ICONIC | WS_VISIBLE)) != WS_VISIBLE )
397 WIN_ReleaseWndPtr(wndPtr);
398 goto fail;
401 xoffset += wndPtr->rectClient.left;
402 yoffset += wndPtr->rectClient.top;
403 OffsetRect( lprect, wndPtr->rectClient.left,
404 wndPtr->rectClient.top );
406 if( (wndPtr->rectClient.left >= wndPtr->rectClient.right) ||
407 (wndPtr->rectClient.top >= wndPtr->rectClient.bottom) ||
408 (lprect->left >= wndPtr->rectClient.right) ||
409 (lprect->right <= wndPtr->rectClient.left) ||
410 (lprect->top >= wndPtr->rectClient.bottom) ||
411 (lprect->bottom <= wndPtr->rectClient.top) )
413 WIN_ReleaseWndPtr(wndPtr);
414 goto fail;
417 lprect->left = max( lprect->left, wndPtr->rectClient.left );
418 lprect->right = min( lprect->right, wndPtr->rectClient.right );
419 lprect->top = max( lprect->top, wndPtr->rectClient.top );
420 lprect->bottom = min( lprect->bottom, wndPtr->rectClient.bottom );
422 WIN_ReleaseWndPtr(wndPtr);
424 OffsetRect( lprect, -xoffset, -yoffset );
425 return TRUE;
428 fail:
429 SetRectEmpty( lprect );
430 return FALSE;
434 /***********************************************************************
435 * DCE_AddClipRects
437 * Go through the linked list of windows from pWndStart to pWndEnd,
438 * adding to the clip region the intersection of the target rectangle
439 * with an offset window rectangle.
441 static BOOL DCE_AddClipRects( WND *pWndStart, WND *pWndEnd,
442 HRGN hrgnClip, LPRECT lpRect, int x, int y )
444 RECT rect;
446 if( pWndStart->pDriver->pIsSelfClipping( pWndStart ) )
447 return TRUE; /* The driver itself will do the clipping */
449 for (WIN_LockWndPtr(pWndStart); (pWndStart && (pWndStart != pWndEnd)); WIN_UpdateWndPtr(&pWndStart,pWndStart->next))
451 if( !(pWndStart->dwStyle & WS_VISIBLE) ) continue;
453 rect.left = pWndStart->rectWindow.left + x;
454 rect.top = pWndStart->rectWindow.top + y;
455 rect.right = pWndStart->rectWindow.right + x;
456 rect.bottom = pWndStart->rectWindow.bottom + y;
458 if( IntersectRect( &rect, &rect, lpRect ))
460 if(!REGION_UnionRectWithRgn( hrgnClip, &rect )) break;
463 WIN_ReleaseWndPtr(pWndStart);
464 return (pWndStart == pWndEnd);
468 /***********************************************************************
469 * DCE_GetVisRgn
471 * Return the visible region of a window, i.e. the client or window area
472 * clipped by the client area of all ancestors, and then optionally
473 * by siblings and children.
475 HRGN DCE_GetVisRgn( HWND hwnd, WORD flags, HWND hwndChild, WORD cflags )
477 HRGN hrgnVis = 0;
478 RECT rect;
479 WND *wndPtr = WIN_FindWndPtr( hwnd );
480 WND *childWnd = WIN_FindWndPtr( hwndChild );
482 /* Get visible rectangle and create a region with it. */
484 if (wndPtr && DCE_GetVisRect(wndPtr, !(flags & DCX_WINDOW), &rect))
486 if((hrgnVis = CreateRectRgnIndirect( &rect )))
488 HRGN hrgnClip = CreateRectRgn( 0, 0, 0, 0 );
489 INT xoffset, yoffset;
491 if( hrgnClip )
493 /* Compute obscured region for the visible rectangle by
494 * clipping children, siblings, and ancestors. Note that
495 * DCE_GetVisRect() returns a rectangle either in client
496 * or in window coordinates (for DCX_WINDOW request). */
498 if( (flags & DCX_CLIPCHILDREN) && wndPtr->child )
500 if( flags & DCX_WINDOW )
502 /* adjust offsets since child window rectangles are
503 * in client coordinates */
505 xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
506 yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
508 else
509 xoffset = yoffset = 0;
511 DCE_AddClipRects( wndPtr->child, NULL, hrgnClip,
512 &rect, xoffset, yoffset );
515 /* We may need to clip children of child window, if a window with PARENTDC
516 * class style and CLIPCHILDREN window style (like in Free Agent 16
517 * preference dialogs) gets here, we take the region for the parent window
518 * but apparently still need to clip the children of the child window... */
520 if( (cflags & DCX_CLIPCHILDREN) && childWnd && childWnd->child )
522 if( flags & DCX_WINDOW )
524 /* adjust offsets since child window rectangles are
525 * in client coordinates */
527 xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
528 yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
530 else
531 xoffset = yoffset = 0;
533 /* client coordinates of child window */
534 xoffset += childWnd->rectClient.left;
535 yoffset += childWnd->rectClient.top;
537 DCE_AddClipRects( childWnd->child, NULL, hrgnClip,
538 &rect, xoffset, yoffset );
541 /* sibling window rectangles are in client
542 * coordinates of the parent window */
544 if (flags & DCX_WINDOW)
546 xoffset = -wndPtr->rectWindow.left;
547 yoffset = -wndPtr->rectWindow.top;
549 else
551 xoffset = -wndPtr->rectClient.left;
552 yoffset = -wndPtr->rectClient.top;
555 if (flags & DCX_CLIPSIBLINGS && wndPtr->parent )
556 DCE_AddClipRects( wndPtr->parent->child,
557 wndPtr, hrgnClip, &rect, xoffset, yoffset );
559 /* Clip siblings of all ancestors that have the
560 * WS_CLIPSIBLINGS style
563 while (wndPtr->parent)
565 WIN_UpdateWndPtr(&wndPtr,wndPtr->parent);
566 xoffset -= wndPtr->rectClient.left;
567 yoffset -= wndPtr->rectClient.top;
568 if(wndPtr->dwStyle & WS_CLIPSIBLINGS && wndPtr->parent)
570 DCE_AddClipRects( wndPtr->parent->child, wndPtr,
571 hrgnClip, &rect, xoffset, yoffset );
575 /* Now once we've got a jumbo clip region we have
576 * to substract it from the visible rectangle.
579 CombineRgn( hrgnVis, hrgnVis, hrgnClip, RGN_DIFF );
580 DeleteObject( hrgnClip );
582 else
584 DeleteObject( hrgnVis );
585 hrgnVis = 0;
589 else
590 hrgnVis = CreateRectRgn(0, 0, 0, 0); /* empty */
591 WIN_ReleaseWndPtr(wndPtr);
592 WIN_ReleaseWndPtr(childWnd);
593 return hrgnVis;
596 /***********************************************************************
597 * DCE_OffsetVisRgn
599 * Change region from DC-origin relative coordinates to screen coords.
602 static void DCE_OffsetVisRgn( HDC hDC, HRGN hVisRgn )
604 DC *dc;
605 if (!(dc = DC_GetDCPtr( hDC ))) return;
607 OffsetRgn( hVisRgn, dc->DCOrgX, dc->DCOrgY );
609 GDI_ReleaseObj( hDC );
612 /***********************************************************************
613 * DCE_ExcludeRgn
615 * Translate given region from the wnd client to the DC coordinates
616 * and add it to the clipping region.
618 INT DCE_ExcludeRgn( HDC hDC, WND* wnd, HRGN hRgn )
620 POINT pt = {0, 0};
621 DCE *dce = firstDCE;
623 while (dce && (dce->hDC != hDC)) dce = dce->next;
624 if( dce )
626 MapWindowPoints( wnd->hwndSelf, dce->hwndCurrent, &pt, 1);
627 if( dce->DCXflags & DCX_WINDOW )
629 wnd = WIN_FindWndPtr(dce->hwndCurrent);
630 pt.x += wnd->rectClient.left - wnd->rectWindow.left;
631 pt.y += wnd->rectClient.top - wnd->rectWindow.top;
632 WIN_ReleaseWndPtr(wnd);
635 else return ERROR;
636 OffsetRgn(hRgn, pt.x, pt.y);
638 return ExtSelectClipRgn( hDC, hRgn, RGN_DIFF );
642 /***********************************************************************
643 * GetDCEx (USER.359)
645 HDC16 WINAPI GetDCEx16( HWND16 hwnd, HRGN16 hrgnClip, DWORD flags )
647 return (HDC16)GetDCEx( hwnd, hrgnClip, flags );
651 /***********************************************************************
652 * GetDCEx (USER32.@)
654 * Unimplemented flags: DCX_LOCKWINDOWUPDATE
656 * FIXME: Full support for hrgnClip == 1 (alias for entire window).
658 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
660 HRGN hrgnVisible = 0;
661 HDC hdc = 0;
662 DCE * dce;
663 DC * dc;
664 WND * wndPtr;
665 DWORD dcxFlags = 0;
666 BOOL bUpdateVisRgn = TRUE;
667 BOOL bUpdateClipOrigin = FALSE;
669 TRACE("hwnd %04x, hrgnClip %04x, flags %08x\n",
670 hwnd, hrgnClip, (unsigned)flags);
672 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
674 /* fixup flags */
676 if (!wndPtr->dce) flags |= DCX_CACHE;
678 if (flags & DCX_USESTYLE)
680 flags &= ~( DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
682 if( wndPtr->dwStyle & WS_CLIPSIBLINGS )
683 flags |= DCX_CLIPSIBLINGS;
685 if ( !(flags & DCX_WINDOW) )
687 if (wndPtr->clsStyle & CS_PARENTDC) flags |= DCX_PARENTCLIP;
689 if (wndPtr->dwStyle & WS_CLIPCHILDREN &&
690 !(wndPtr->dwStyle & WS_MINIMIZE) ) flags |= DCX_CLIPCHILDREN;
692 else flags |= DCX_CACHE;
695 if( flags & DCX_NOCLIPCHILDREN )
697 flags |= DCX_CACHE;
698 flags &= ~(DCX_PARENTCLIP | DCX_CLIPCHILDREN);
701 if (flags & DCX_WINDOW)
702 flags = (flags & ~DCX_CLIPCHILDREN) | DCX_CACHE;
704 if (!(wndPtr->dwStyle & WS_CHILD) || !wndPtr->parent )
705 flags &= ~DCX_PARENTCLIP;
706 else if( flags & DCX_PARENTCLIP )
708 flags |= DCX_CACHE;
709 if( !(flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) )
710 if( (wndPtr->dwStyle & WS_VISIBLE) && (wndPtr->parent->dwStyle & WS_VISIBLE) )
712 flags &= ~DCX_CLIPCHILDREN;
713 if( wndPtr->parent->dwStyle & WS_CLIPSIBLINGS )
714 flags |= DCX_CLIPSIBLINGS;
718 /* find a suitable DCE */
720 dcxFlags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
721 DCX_CACHE | DCX_WINDOW);
723 if (flags & DCX_CACHE)
725 DCE* dceEmpty;
726 DCE* dceUnused;
728 dceEmpty = dceUnused = NULL;
730 /* Strategy: First, we attempt to find a non-empty but unused DCE with
731 * compatible flags. Next, we look for an empty entry. If the cache is
732 * full we have to purge one of the unused entries.
735 for (dce = firstDCE; (dce); dce = dce->next)
737 if ((dce->DCXflags & (DCX_CACHE | DCX_DCEBUSY)) == DCX_CACHE )
739 dceUnused = dce;
741 if (dce->DCXflags & DCX_DCEEMPTY)
742 dceEmpty = dce;
743 else
744 if ((dce->hwndCurrent == hwnd) &&
745 ((dce->DCXflags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
746 DCX_CACHE | DCX_WINDOW | DCX_PARENTCLIP)) == dcxFlags))
748 TRACE("\tfound valid %08x dce [%04x], flags %08x\n",
749 (unsigned)dce, hwnd, (unsigned)dcxFlags );
750 bUpdateVisRgn = FALSE;
751 bUpdateClipOrigin = TRUE;
752 break;
757 if (!dce) dce = (dceEmpty) ? dceEmpty : dceUnused;
759 /* if there's no dce empty or unused, allocate a new one */
760 if (!dce)
762 dce = DCE_AllocDCE( 0, DCE_CACHE_DC );
765 else
767 dce = wndPtr->dce;
768 if( dce->hwndCurrent == hwnd )
770 TRACE("\tskipping hVisRgn update\n");
771 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
773 /* Abey - 16Jul99. to take care of the nested GetDC. first one
774 with DCX_EXCLUDERGN or DCX_INTERSECTRGN flags and the next
775 one with or without these flags. */
777 if(dce->DCXflags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN))
779 /* This is likely to be a nested BeginPaint().
780 or a BeginPaint() followed by a GetDC()*/
782 if( dce->hClipRgn != hrgnClip )
784 FIXME("new hrgnClip[%04x] smashes the previous[%04x]\n",
785 hrgnClip, dce->hClipRgn );
786 DCE_DeleteClipRgn( dce );
788 else
789 RestoreVisRgn16(dce->hDC);
793 if (!dce)
795 hdc = 0;
796 goto END;
799 dce->hwndCurrent = hwnd;
800 dce->hClipRgn = 0;
801 dce->DCXflags = dcxFlags | (flags & DCX_WINDOWPAINT) | DCX_DCEBUSY;
802 hdc = dce->hDC;
804 if (!(dc = DC_GetDCPtr( hdc )))
806 hdc = 0;
807 goto END;
809 bUpdateVisRgn = bUpdateVisRgn || (dc->flags & DC_DIRTY);
811 /* recompute visible region */
812 wndPtr->pDriver->pSetDrawable( wndPtr, hdc, flags, bUpdateClipOrigin );
813 dc->flags &= ~DC_DIRTY;
814 GDI_ReleaseObj( hdc );
816 if( bUpdateVisRgn )
818 TRACE("updating visrgn for %08x dce, hwnd [%04x]\n", (unsigned)dce, hwnd);
820 if (flags & DCX_PARENTCLIP)
822 WND *parentPtr = WIN_LockWndPtr(wndPtr->parent);
824 if( wndPtr->dwStyle & WS_VISIBLE && !(parentPtr->dwStyle & WS_MINIMIZE) )
826 if( parentPtr->dwStyle & WS_CLIPSIBLINGS )
827 dcxFlags = DCX_CLIPSIBLINGS | (flags & ~(DCX_CLIPCHILDREN | DCX_WINDOW));
828 else
829 dcxFlags = flags & ~(DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW);
831 hrgnVisible = DCE_GetVisRgn( parentPtr->hwndSelf, dcxFlags,
832 wndPtr->hwndSelf, flags );
833 if( flags & DCX_WINDOW )
834 OffsetRgn( hrgnVisible, -wndPtr->rectWindow.left,
835 -wndPtr->rectWindow.top );
836 else
837 OffsetRgn( hrgnVisible, -wndPtr->rectClient.left,
838 -wndPtr->rectClient.top );
839 DCE_OffsetVisRgn( hdc, hrgnVisible );
841 else
842 hrgnVisible = CreateRectRgn( 0, 0, 0, 0 );
843 WIN_ReleaseWndPtr(parentPtr);
845 else
846 if ((hwnd == GetDesktopWindow()) && !USER_Driver.pIsSingleWindow())
847 hrgnVisible = CreateRectRgn( 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN) );
848 else
850 hrgnVisible = DCE_GetVisRgn( hwnd, flags, 0, 0 );
851 DCE_OffsetVisRgn( hdc, hrgnVisible );
854 dce->DCXflags &= ~DCX_DCEDIRTY;
855 SelectVisRgn16( hdc, hrgnVisible );
857 else
858 TRACE("no visrgn update %08x dce, hwnd [%04x]\n", (unsigned)dce, hwnd);
860 /* apply additional region operation (if any) */
862 if( flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN) )
864 if( !hrgnVisible ) hrgnVisible = CreateRectRgn( 0, 0, 0, 0 );
866 dce->DCXflags |= flags & (DCX_KEEPCLIPRGN | DCX_INTERSECTRGN | DCX_EXCLUDERGN);
867 dce->hClipRgn = hrgnClip;
869 TRACE("\tsaved VisRgn, clipRgn = %04x\n", hrgnClip);
871 SaveVisRgn16( hdc );
872 CombineRgn( hrgnVisible, hrgnClip, 0, RGN_COPY );
873 DCE_OffsetVisRgn( hdc, hrgnVisible );
874 CombineRgn( hrgnVisible, InquireVisRgn16( hdc ), hrgnVisible,
875 (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
876 SelectVisRgn16( hdc, hrgnVisible );
879 if( hrgnVisible ) DeleteObject( hrgnVisible );
881 TRACE("(%04x,%04x,0x%lx): returning %04x\n",
882 hwnd, hrgnClip, flags, hdc);
883 END:
884 WIN_ReleaseWndPtr(wndPtr);
885 return hdc;
889 /***********************************************************************
890 * GetDC (USER.66)
892 HDC16 WINAPI GetDC16( HWND16 hwnd )
894 return (HDC16)GetDC( hwnd );
898 /***********************************************************************
899 * GetDC (USER32.@)
900 * RETURNS
901 * :Handle to DC
902 * NULL: Failure
904 HDC WINAPI GetDC(
905 HWND hwnd /* [in] handle of window */
907 if (!hwnd)
908 return GetDCEx( GetDesktopWindow(), 0, DCX_CACHE | DCX_WINDOW );
909 return GetDCEx( hwnd, 0, DCX_USESTYLE );
913 /***********************************************************************
914 * GetWindowDC (USER.67)
916 HDC16 WINAPI GetWindowDC16( HWND16 hwnd )
918 if (!hwnd) hwnd = GetDesktopWindow16();
919 return GetDCEx16( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
923 /***********************************************************************
924 * GetWindowDC (USER32.@)
926 HDC WINAPI GetWindowDC( HWND hwnd )
928 if (!hwnd) hwnd = GetDesktopWindow();
929 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
933 /***********************************************************************
934 * ReleaseDC (USER.68)
936 INT16 WINAPI ReleaseDC16( HWND16 hwnd, HDC16 hdc )
938 return (INT16)ReleaseDC( hwnd, hdc );
942 /***********************************************************************
943 * ReleaseDC (USER32.@)
945 * RETURNS
946 * 1: Success
947 * 0: Failure
949 INT WINAPI ReleaseDC(
950 HWND hwnd /* [in] Handle of window - ignored */,
951 HDC hdc /* [in] Handle of device context */
953 DCE * dce;
954 INT nRet = 0;
956 WIN_LockWnds();
957 dce = firstDCE;
959 TRACE("%04x %04x\n", hwnd, hdc );
961 while (dce && (dce->hDC != hdc)) dce = dce->next;
963 if ( dce )
964 if ( dce->DCXflags & DCX_DCEBUSY )
965 nRet = DCE_ReleaseDC( dce );
967 WIN_UnlockWnds();
969 return nRet;
972 /***********************************************************************
973 * DCHook (USER.362)
975 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
977 BOOL16 WINAPI DCHook16( HDC16 hDC, WORD code, DWORD data, LPARAM lParam )
979 BOOL retv = TRUE;
980 HRGN hVisRgn;
981 DCE *dce = (DCE *)data;
982 WND *wndPtr;
984 TRACE("hDC = %04x, %i\n", hDC, code);
986 if (!dce) return 0;
987 assert(dce->hDC == hDC);
989 /* Grab the windows lock before doing anything else */
990 WIN_LockWnds();
992 switch( code )
994 case DCHC_INVALIDVISRGN:
996 /* GDI code calls this when it detects that the
997 * DC is dirty (usually after SetHookFlags()). This
998 * means that we have to recompute the visible region.
1001 if( dce->DCXflags & DCX_DCEBUSY )
1004 /* Update stale DC in DCX */
1005 wndPtr = WIN_FindWndPtr( dce->hwndCurrent);
1006 if (wndPtr) wndPtr->pDriver->pSetDrawable( wndPtr, dce->hDC, dce->DCXflags, TRUE);
1008 SetHookFlags16(hDC, DCHF_VALIDATEVISRGN);
1009 hVisRgn = DCE_GetVisRgn(dce->hwndCurrent, dce->DCXflags, 0, 0);
1011 TRACE("\tapplying saved clipRgn\n");
1013 /* clip this region with saved clipping region */
1015 if ( (dce->DCXflags & DCX_INTERSECTRGN && dce->hClipRgn != 1) ||
1016 ( dce->DCXflags & DCX_EXCLUDERGN && dce->hClipRgn) )
1019 if( (!dce->hClipRgn && dce->DCXflags & DCX_INTERSECTRGN) ||
1020 (dce->hClipRgn == 1 && dce->DCXflags & DCX_EXCLUDERGN) )
1021 SetRectRgn(hVisRgn,0,0,0,0);
1022 else
1023 CombineRgn(hVisRgn, hVisRgn, dce->hClipRgn,
1024 (dce->DCXflags & DCX_EXCLUDERGN)? RGN_DIFF:RGN_AND);
1026 dce->DCXflags &= ~DCX_DCEDIRTY;
1027 DCE_OffsetVisRgn( hDC, hVisRgn );
1028 SelectVisRgn16(hDC, hVisRgn);
1029 DeleteObject( hVisRgn );
1030 WIN_ReleaseWndPtr( wndPtr ); /* Release WIN_FindWndPtr lock */
1032 else /* non-fatal but shouldn't happen */
1033 WARN("DC is not in use!\n");
1034 break;
1036 case DCHC_DELETEDC:
1038 * Windows will not let you delete a DC that is busy
1039 * (between GetDC and ReleaseDC)
1042 if ( dce->DCXflags & DCX_DCEBUSY )
1044 WARN("Application trying to delete a busy DC\n");
1045 retv = FALSE;
1047 break;
1049 default:
1050 FIXME("unknown code\n");
1053 WIN_UnlockWnds(); /* Release the wnd lock */
1054 return retv;
1058 /**********************************************************************
1059 * WindowFromDC (USER.117)
1061 HWND16 WINAPI WindowFromDC16( HDC16 hDC )
1063 return (HWND16)WindowFromDC( hDC );
1067 /**********************************************************************
1068 * WindowFromDC (USER32.@)
1070 HWND WINAPI WindowFromDC( HDC hDC )
1072 DCE *dce;
1073 HWND hwnd;
1075 WIN_LockWnds();
1076 dce = firstDCE;
1078 while (dce && (dce->hDC != hDC)) dce = dce->next;
1080 hwnd = dce ? dce->hwndCurrent : 0;
1081 WIN_UnlockWnds();
1083 return hwnd;
1087 /***********************************************************************
1088 * LockWindowUpdate (USER.294)
1090 BOOL16 WINAPI LockWindowUpdate16( HWND16 hwnd )
1092 return LockWindowUpdate( hwnd );
1096 /***********************************************************************
1097 * LockWindowUpdate (USER32.@)
1099 BOOL WINAPI LockWindowUpdate( HWND hwnd )
1101 FIXME("DCX_LOCKWINDOWUPDATE is unimplemented\n");
1102 return TRUE;