Fixed OleLoadFromStream: Dereference ppvObj and get IPersistStream
[wine.git] / windows / dce.c
blobdffaca34c0d6fba43c72d58858bef90e3da4d87a
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 "class.h"
24 #include "win.h"
25 #include "gdi.h"
26 #include "region.h"
27 #include "heap.h"
28 #include "local.h"
29 #include "module.h"
30 #include "user.h"
31 #include "debugtools.h"
32 #include "windef.h"
33 #include "wingdi.h"
34 #include "wine/winbase16.h"
35 #include "wine/winuser16.h"
37 DEFAULT_DEBUG_CHANNEL(dc);
39 #define NB_DCE 5 /* Number of DCEs created at startup */
41 static DCE *firstDCE = 0;
42 static HDC defaultDCstate = 0;
44 static void DCE_DeleteClipRgn( DCE* );
45 static INT DCE_ReleaseDC( DCE* );
48 /***********************************************************************
49 * DCE_DumpCache
51 static void DCE_DumpCache(void)
53 DCE *dce;
55 WIN_LockWnds();
56 dce = firstDCE;
58 DPRINTF("DCE:\n");
59 while( dce )
61 DPRINTF("\t[0x%08x] hWnd 0x%04x, dcx %08x, %s %s\n",
62 (unsigned)dce, dce->hwndCurrent, (unsigned)dce->DCXflags,
63 (dce->DCXflags & DCX_CACHE) ? "Cache" : "Owned",
64 (dce->DCXflags & DCX_DCEBUSY) ? "InUse" : "" );
65 dce = dce->next;
68 WIN_UnlockWnds();
71 /***********************************************************************
72 * DCE_AllocDCE
74 * Allocate a new DCE.
76 DCE *DCE_AllocDCE( HWND hWnd, DCE_TYPE type )
78 FARPROC16 hookProc;
79 DCE * dce;
80 WND* wnd;
82 if (!(dce = HeapAlloc( SystemHeap, 0, sizeof(DCE) ))) return NULL;
83 if (!(dce->hDC = CreateDCA( "DISPLAY", NULL, NULL, NULL )))
85 HeapFree( SystemHeap, 0, dce );
86 return 0;
89 wnd = WIN_FindWndPtr(hWnd);
91 /* store DCE handle in DC hook data field */
93 hookProc = (FARPROC16)NE_GetEntryPoint( GetModuleHandle16("USER"), 362 );
94 SetDCHook( dce->hDC, hookProc, (DWORD)dce );
96 dce->hwndCurrent = hWnd;
97 dce->hClipRgn = 0;
98 dce->next = firstDCE;
99 firstDCE = dce;
101 if( type != DCE_CACHE_DC ) /* owned or class DC */
103 dce->DCXflags = DCX_DCEBUSY;
104 if( hWnd )
106 if( wnd->dwStyle & WS_CLIPCHILDREN ) dce->DCXflags |= DCX_CLIPCHILDREN;
107 if( wnd->dwStyle & WS_CLIPSIBLINGS ) dce->DCXflags |= DCX_CLIPSIBLINGS;
109 SetHookFlags16(dce->hDC,DCHF_INVALIDATEVISRGN);
111 else dce->DCXflags = DCX_CACHE | DCX_DCEEMPTY;
113 WIN_ReleaseWndPtr(wnd);
115 return dce;
119 /***********************************************************************
120 * DCE_FreeDCE
122 DCE* DCE_FreeDCE( DCE *dce )
124 DCE **ppDCE;
126 if (!dce) return NULL;
128 WIN_LockWnds();
130 ppDCE = &firstDCE;
132 while (*ppDCE && (*ppDCE != dce)) ppDCE = &(*ppDCE)->next;
133 if (*ppDCE == dce) *ppDCE = dce->next;
135 SetDCHook(dce->hDC, NULL, 0L);
137 DeleteDC( dce->hDC );
138 if( dce->hClipRgn && !(dce->DCXflags & DCX_KEEPCLIPRGN) )
139 DeleteObject(dce->hClipRgn);
140 HeapFree( SystemHeap, 0, dce );
142 WIN_UnlockWnds();
144 return *ppDCE;
147 /***********************************************************************
148 * DCE_FreeWindowDCE
150 * Remove owned DCE and reset unreleased cache DCEs.
152 void DCE_FreeWindowDCE( WND* pWnd )
154 DCE *pDCE;
156 WIN_LockWnds();
157 pDCE = firstDCE;
159 while( pDCE )
161 if( pDCE->hwndCurrent == pWnd->hwndSelf )
163 if( pDCE == pWnd->dce ) /* owned or Class DCE*/
165 if (pWnd->class->style & CS_OWNDC) /* owned DCE*/
167 pDCE = DCE_FreeDCE( pDCE );
168 pWnd->dce = NULL;
169 continue;
171 else if( pDCE->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN) ) /* Class DCE*/
173 DCE_DeleteClipRgn( pDCE );
174 pDCE->hwndCurrent = 0;
177 else
179 if( pDCE->DCXflags & DCX_DCEBUSY ) /* shared cache DCE */
181 ERR("[%04x] GetDC() without ReleaseDC()!\n",
182 pWnd->hwndSelf);
183 DCE_ReleaseDC( pDCE );
186 pDCE->DCXflags &= DCX_CACHE;
187 pDCE->DCXflags |= DCX_DCEEMPTY;
188 pDCE->hwndCurrent = 0;
191 pDCE = pDCE->next;
194 WIN_UnlockWnds();
198 /***********************************************************************
199 * DCE_DeleteClipRgn
201 static void DCE_DeleteClipRgn( DCE* dce )
203 dce->DCXflags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN | DCX_WINDOWPAINT);
205 if( dce->DCXflags & DCX_KEEPCLIPRGN )
206 dce->DCXflags &= ~DCX_KEEPCLIPRGN;
207 else
208 if( dce->hClipRgn > 1 )
209 DeleteObject( dce->hClipRgn );
211 dce->hClipRgn = 0;
213 TRACE("\trestoring VisRgn\n");
215 RestoreVisRgn16(dce->hDC);
219 /***********************************************************************
220 * DCE_ReleaseDC
222 static INT DCE_ReleaseDC( DCE* dce )
224 if ((dce->DCXflags & (DCX_DCEEMPTY | DCX_DCEBUSY)) != DCX_DCEBUSY) return 0;
226 /* restore previous visible region */
228 if ((dce->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
229 (dce->DCXflags & (DCX_CACHE | DCX_WINDOWPAINT)) )
230 DCE_DeleteClipRgn( dce );
232 if (dce->DCXflags & DCX_CACHE)
234 SetDCState16( dce->hDC, defaultDCstate );
235 dce->DCXflags &= ~DCX_DCEBUSY;
236 if (dce->DCXflags & DCX_DCEDIRTY)
238 /* don't keep around invalidated entries
239 * because SetDCState() disables hVisRgn updates
240 * by removing dirty bit. */
242 dce->hwndCurrent = 0;
243 dce->DCXflags &= DCX_CACHE;
244 dce->DCXflags |= DCX_DCEEMPTY;
247 return 1;
251 /***********************************************************************
252 * DCE_InvalidateDCE
254 * It is called from SetWindowPos() and EVENT_MapNotify - we have to
255 * mark as dirty all busy DCEs for windows that have pWnd->parent as
256 * an ansector and whose client rect intersects with specified update
257 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
258 * DCX_CLIPCHILDREN flag is set. */
259 BOOL DCE_InvalidateDCE(WND* pWnd, const RECT* pRectUpdate)
261 WND* wndScope = WIN_LockWndPtr(pWnd->parent);
262 WND *pDesktop = WIN_GetDesktop();
263 BOOL bRet = FALSE;
265 if( wndScope )
267 DCE *dce;
269 TRACE("scope hwnd = %04x, (%i,%i - %i,%i)\n",
270 wndScope->hwndSelf, pRectUpdate->left,pRectUpdate->top,
271 pRectUpdate->right,pRectUpdate->bottom);
272 if(TRACE_ON(dc))
273 DCE_DumpCache();
275 /* walk all DCEs and fixup non-empty entries */
277 for (dce = firstDCE; (dce); dce = dce->next)
279 if( !(dce->DCXflags & DCX_DCEEMPTY) )
281 WND* wndCurrent = WIN_FindWndPtr(dce->hwndCurrent);
283 if( wndCurrent )
285 WND* wnd = NULL;
286 INT xoffset = 0, yoffset = 0;
288 if( (wndCurrent == wndScope) && !(dce->DCXflags & DCX_CLIPCHILDREN) )
290 /* child window positions don't bother us */
291 WIN_ReleaseWndPtr(wndCurrent);
292 continue;
295 if( !Options.desktopGeometry && wndCurrent == pDesktop )
297 /* don't bother with fake desktop */
298 WIN_ReleaseWndPtr(wndCurrent);
299 continue;
302 /* check if DCE window is within the z-order scope */
304 for( wnd = WIN_LockWndPtr(wndCurrent); wnd; WIN_UpdateWndPtr(&wnd,wnd->parent))
306 if( wnd == wndScope )
308 RECT wndRect;
310 wndRect = wndCurrent->rectWindow;
312 OffsetRect( &wndRect, xoffset - wndCurrent->rectClient.left,
313 yoffset - wndCurrent->rectClient.top);
315 if (pWnd == wndCurrent ||
316 IntersectRect( &wndRect, &wndRect, pRectUpdate ))
318 if( !(dce->DCXflags & DCX_DCEBUSY) )
320 /* Don't bother with visible regions of unused DCEs */
322 TRACE("\tpurged %08x dce [%04x]\n",
323 (unsigned)dce, wndCurrent->hwndSelf);
325 dce->hwndCurrent = 0;
326 dce->DCXflags &= DCX_CACHE;
327 dce->DCXflags |= DCX_DCEEMPTY;
329 else
331 /* Set dirty bits in the hDC and DCE structs */
333 TRACE("\tfixed up %08x dce [%04x]\n",
334 (unsigned)dce, wndCurrent->hwndSelf);
336 dce->DCXflags |= DCX_DCEDIRTY;
337 SetHookFlags16(dce->hDC, DCHF_INVALIDATEVISRGN);
338 bRet = TRUE;
341 WIN_ReleaseWndPtr(wnd);
342 break;
344 xoffset += wnd->rectClient.left;
345 yoffset += wnd->rectClient.top;
348 WIN_ReleaseWndPtr(wndCurrent);
350 } /* dce list */
351 WIN_ReleaseWndPtr(wndScope);
353 WIN_ReleaseDesktop();
354 return bRet;
357 /***********************************************************************
358 * DCE_Init
360 void DCE_Init(void)
362 int i;
363 DCE * dce;
365 for (i = 0; i < NB_DCE; i++)
367 if (!(dce = DCE_AllocDCE( 0, DCE_CACHE_DC ))) return;
368 if (!defaultDCstate) defaultDCstate = GetDCState16( dce->hDC );
373 /***********************************************************************
374 * DCE_GetVisRect
376 * Calculate the visible rectangle of a window (i.e. the client or
377 * window area clipped by the client area of all ancestors) in the
378 * corresponding coordinates. Return FALSE if the visible region is empty.
380 static BOOL DCE_GetVisRect( WND *wndPtr, BOOL clientArea, RECT *lprect )
382 *lprect = clientArea ? wndPtr->rectClient : wndPtr->rectWindow;
384 if (wndPtr->dwStyle & WS_VISIBLE)
386 INT xoffset = lprect->left;
387 INT yoffset = lprect->top;
389 while( !(wndPtr->flags & WIN_NATIVE) &&
390 ( wndPtr = WIN_LockWndPtr(wndPtr->parent)) )
392 if ( (wndPtr->dwStyle & (WS_ICONIC | WS_VISIBLE)) != WS_VISIBLE )
394 WIN_ReleaseWndPtr(wndPtr);
395 goto fail;
398 xoffset += wndPtr->rectClient.left;
399 yoffset += wndPtr->rectClient.top;
400 OffsetRect( lprect, wndPtr->rectClient.left,
401 wndPtr->rectClient.top );
403 if( (wndPtr->rectClient.left >= wndPtr->rectClient.right) ||
404 (wndPtr->rectClient.top >= wndPtr->rectClient.bottom) ||
405 (lprect->left >= wndPtr->rectClient.right) ||
406 (lprect->right <= wndPtr->rectClient.left) ||
407 (lprect->top >= wndPtr->rectClient.bottom) ||
408 (lprect->bottom <= wndPtr->rectClient.top) )
410 WIN_ReleaseWndPtr(wndPtr);
411 goto fail;
414 lprect->left = max( lprect->left, wndPtr->rectClient.left );
415 lprect->right = min( lprect->right, wndPtr->rectClient.right );
416 lprect->top = max( lprect->top, wndPtr->rectClient.top );
417 lprect->bottom = min( lprect->bottom, wndPtr->rectClient.bottom );
419 WIN_ReleaseWndPtr(wndPtr);
421 OffsetRect( lprect, -xoffset, -yoffset );
422 return TRUE;
425 fail:
426 SetRectEmpty( lprect );
427 return FALSE;
431 /***********************************************************************
432 * DCE_AddClipRects
434 * Go through the linked list of windows from pWndStart to pWndEnd,
435 * adding to the clip region the intersection of the target rectangle
436 * with an offset window rectangle.
438 static BOOL DCE_AddClipRects( WND *pWndStart, WND *pWndEnd,
439 HRGN hrgnClip, LPRECT lpRect, int x, int y )
441 RECT rect;
443 if( pWndStart->pDriver->pIsSelfClipping( pWndStart ) )
444 return TRUE; /* The driver itself will do the clipping */
446 for (WIN_LockWndPtr(pWndStart); (pWndStart && (pWndStart != pWndEnd)); WIN_UpdateWndPtr(&pWndStart,pWndStart->next))
448 if( !(pWndStart->dwStyle & WS_VISIBLE) ) continue;
450 rect.left = pWndStart->rectWindow.left + x;
451 rect.top = pWndStart->rectWindow.top + y;
452 rect.right = pWndStart->rectWindow.right + x;
453 rect.bottom = pWndStart->rectWindow.bottom + y;
455 if( IntersectRect( &rect, &rect, lpRect ))
457 if(!REGION_UnionRectWithRgn( hrgnClip, &rect )) break;
460 WIN_ReleaseWndPtr(pWndStart);
461 return (pWndStart == pWndEnd);
465 /***********************************************************************
466 * DCE_GetVisRgn
468 * Return the visible region of a window, i.e. the client or window area
469 * clipped by the client area of all ancestors, and then optionally
470 * by siblings and children.
472 HRGN DCE_GetVisRgn( HWND hwnd, WORD flags, HWND hwndChild, WORD cflags )
474 HRGN hrgnVis = 0;
475 RECT rect;
476 WND *wndPtr = WIN_FindWndPtr( hwnd );
477 WND *childWnd = WIN_FindWndPtr( hwndChild );
479 /* Get visible rectangle and create a region with it. */
481 if (wndPtr && DCE_GetVisRect(wndPtr, !(flags & DCX_WINDOW), &rect))
483 if((hrgnVis = CreateRectRgnIndirect( &rect )))
485 HRGN hrgnClip = CreateRectRgn( 0, 0, 0, 0 );
486 INT xoffset, yoffset;
488 if( hrgnClip )
490 /* Compute obscured region for the visible rectangle by
491 * clipping children, siblings, and ancestors. Note that
492 * DCE_GetVisRect() returns a rectangle either in client
493 * or in window coordinates (for DCX_WINDOW request). */
495 if( (flags & DCX_CLIPCHILDREN) && wndPtr->child )
497 if( flags & DCX_WINDOW )
499 /* adjust offsets since child window rectangles are
500 * in client coordinates */
502 xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
503 yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
505 else
506 xoffset = yoffset = 0;
508 DCE_AddClipRects( wndPtr->child, NULL, hrgnClip,
509 &rect, xoffset, yoffset );
512 /* We may need to clip children of child window, if a window with PARENTDC
513 * class style and CLIPCHILDREN window style (like in Free Agent 16
514 * preference dialogs) gets here, we take the region for the parent window
515 * but apparently still need to clip the children of the child window... */
517 if( (cflags & DCX_CLIPCHILDREN) && childWnd && childWnd->child )
519 if( flags & DCX_WINDOW )
521 /* adjust offsets since child window rectangles are
522 * in client coordinates */
524 xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
525 yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
527 else
528 xoffset = yoffset = 0;
530 /* client coordinates of child window */
531 xoffset += childWnd->rectClient.left;
532 yoffset += childWnd->rectClient.top;
534 DCE_AddClipRects( childWnd->child, NULL, hrgnClip,
535 &rect, xoffset, yoffset );
538 /* sibling window rectangles are in client
539 * coordinates of the parent window */
541 if (flags & DCX_WINDOW)
543 xoffset = -wndPtr->rectWindow.left;
544 yoffset = -wndPtr->rectWindow.top;
546 else
548 xoffset = -wndPtr->rectClient.left;
549 yoffset = -wndPtr->rectClient.top;
552 if (flags & DCX_CLIPSIBLINGS && wndPtr->parent )
553 DCE_AddClipRects( wndPtr->parent->child,
554 wndPtr, hrgnClip, &rect, xoffset, yoffset );
556 /* Clip siblings of all ancestors that have the
557 * WS_CLIPSIBLINGS style
560 while (wndPtr->dwStyle & WS_CHILD)
562 WIN_UpdateWndPtr(&wndPtr,wndPtr->parent);
563 xoffset -= wndPtr->rectClient.left;
564 yoffset -= wndPtr->rectClient.top;
565 if(wndPtr->dwStyle & WS_CLIPSIBLINGS && wndPtr->parent)
567 DCE_AddClipRects( wndPtr->parent->child, wndPtr,
568 hrgnClip, &rect, xoffset, yoffset );
572 /* Now once we've got a jumbo clip region we have
573 * to substract it from the visible rectangle.
576 CombineRgn( hrgnVis, hrgnVis, hrgnClip, RGN_DIFF );
577 DeleteObject( hrgnClip );
579 else
581 DeleteObject( hrgnVis );
582 hrgnVis = 0;
586 else
587 hrgnVis = CreateRectRgn(0, 0, 0, 0); /* empty */
588 WIN_ReleaseWndPtr(wndPtr);
589 WIN_ReleaseWndPtr(childWnd);
590 return hrgnVis;
593 /***********************************************************************
594 * DCE_OffsetVisRgn
596 * Change region from DC-origin relative coordinates to screen coords.
599 static void DCE_OffsetVisRgn( HDC hDC, HRGN hVisRgn )
601 DC *dc;
602 if (!(dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC ))) return;
604 OffsetRgn( hVisRgn, dc->w.DCOrgX, dc->w.DCOrgY );
606 GDI_ReleaseObj( hDC );
609 /***********************************************************************
610 * DCE_ExcludeRgn
612 * Translate given region from the wnd client to the DC coordinates
613 * and add it to the clipping region.
615 INT DCE_ExcludeRgn( HDC hDC, WND* wnd, HRGN hRgn )
617 POINT pt = {0, 0};
618 DCE *dce = firstDCE;
620 while (dce && (dce->hDC != hDC)) dce = dce->next;
621 if( dce )
623 MapWindowPoints( wnd->hwndSelf, dce->hwndCurrent, &pt, 1);
624 if( dce->DCXflags & DCX_WINDOW )
626 wnd = WIN_FindWndPtr(dce->hwndCurrent);
627 pt.x += wnd->rectClient.left - wnd->rectWindow.left;
628 pt.y += wnd->rectClient.top - wnd->rectWindow.top;
629 WIN_ReleaseWndPtr(wnd);
632 else return ERROR;
633 OffsetRgn(hRgn, pt.x, pt.y);
635 return ExtSelectClipRgn( hDC, hRgn, RGN_DIFF );
639 /***********************************************************************
640 * GetDCEx16 (USER.359)
642 HDC16 WINAPI GetDCEx16( HWND16 hwnd, HRGN16 hrgnClip, DWORD flags )
644 return (HDC16)GetDCEx( hwnd, hrgnClip, flags );
648 /***********************************************************************
649 * GetDCEx (USER32.231)
651 * Unimplemented flags: DCX_LOCKWINDOWUPDATE
653 * FIXME: Full support for hrgnClip == 1 (alias for entire window).
655 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
657 HRGN hrgnVisible = 0;
658 HDC hdc = 0;
659 DCE * dce;
660 DC * dc;
661 WND * wndPtr;
662 DWORD dcxFlags = 0;
663 BOOL bUpdateVisRgn = TRUE;
664 BOOL bUpdateClipOrigin = FALSE;
666 TRACE("hwnd %04x, hrgnClip %04x, flags %08x\n",
667 hwnd, hrgnClip, (unsigned)flags);
669 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
671 /* fixup flags */
673 if (!(wndPtr->class->style & (CS_OWNDC | CS_CLASSDC))) flags |= DCX_CACHE;
675 if (flags & DCX_USESTYLE)
677 flags &= ~( DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
679 if( wndPtr->dwStyle & WS_CLIPSIBLINGS )
680 flags |= DCX_CLIPSIBLINGS;
682 if ( !(flags & DCX_WINDOW) )
684 if (wndPtr->class->style & CS_PARENTDC) flags |= DCX_PARENTCLIP;
686 if (wndPtr->dwStyle & WS_CLIPCHILDREN &&
687 !(wndPtr->dwStyle & WS_MINIMIZE) ) flags |= DCX_CLIPCHILDREN;
689 else flags |= DCX_CACHE;
692 if( flags & DCX_NOCLIPCHILDREN )
694 flags |= DCX_CACHE;
695 flags &= ~(DCX_PARENTCLIP | DCX_CLIPCHILDREN);
698 if (flags & DCX_WINDOW)
699 flags = (flags & ~DCX_CLIPCHILDREN) | DCX_CACHE;
701 if (!(wndPtr->dwStyle & WS_CHILD) || !wndPtr->parent )
702 flags &= ~DCX_PARENTCLIP;
703 else if( flags & DCX_PARENTCLIP )
705 flags |= DCX_CACHE;
706 if( !(flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) )
707 if( (wndPtr->dwStyle & WS_VISIBLE) && (wndPtr->parent->dwStyle & WS_VISIBLE) )
709 flags &= ~DCX_CLIPCHILDREN;
710 if( wndPtr->parent->dwStyle & WS_CLIPSIBLINGS )
711 flags |= DCX_CLIPSIBLINGS;
715 /* find a suitable DCE */
717 dcxFlags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
718 DCX_CACHE | DCX_WINDOW);
720 if (flags & DCX_CACHE)
722 DCE* dceEmpty;
723 DCE* dceUnused;
725 dceEmpty = dceUnused = NULL;
727 /* Strategy: First, we attempt to find a non-empty but unused DCE with
728 * compatible flags. Next, we look for an empty entry. If the cache is
729 * full we have to purge one of the unused entries.
732 for (dce = firstDCE; (dce); dce = dce->next)
734 if ((dce->DCXflags & (DCX_CACHE | DCX_DCEBUSY)) == DCX_CACHE )
736 dceUnused = dce;
738 if (dce->DCXflags & DCX_DCEEMPTY)
739 dceEmpty = dce;
740 else
741 if ((dce->hwndCurrent == hwnd) &&
742 ((dce->DCXflags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
743 DCX_CACHE | DCX_WINDOW | DCX_PARENTCLIP)) == dcxFlags))
745 TRACE("\tfound valid %08x dce [%04x], flags %08x\n",
746 (unsigned)dce, hwnd, (unsigned)dcxFlags );
747 bUpdateVisRgn = FALSE;
748 bUpdateClipOrigin = TRUE;
749 break;
754 if (!dce) dce = (dceEmpty) ? dceEmpty : dceUnused;
756 /* if there's no dce empty or unused, allocate a new one */
757 if (!dce)
759 dce = DCE_AllocDCE( 0, DCE_CACHE_DC );
762 else
764 dce = (wndPtr->class->style & CS_OWNDC) ? wndPtr->dce : wndPtr->class->dce;
765 if( dce->hwndCurrent == hwnd )
767 TRACE("\tskipping hVisRgn update\n");
768 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
770 /* Abey - 16Jul99. to take care of the nested GetDC. first one
771 with DCX_EXCLUDERGN or DCX_INTERSECTRGN flags and the next
772 one with or without these flags. */
774 if(dce->DCXflags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN))
776 /* This is likely to be a nested BeginPaint().
777 or a BeginPaint() followed by a GetDC()*/
779 if( dce->hClipRgn != hrgnClip )
781 FIXME("new hrgnClip[%04x] smashes the previous[%04x]\n",
782 hrgnClip, dce->hClipRgn );
783 DCE_DeleteClipRgn( dce );
785 else
786 RestoreVisRgn16(dce->hDC);
790 if (!dce)
792 hdc = 0;
793 goto END;
796 dce->hwndCurrent = hwnd;
797 dce->hClipRgn = 0;
798 dce->DCXflags = dcxFlags | (flags & DCX_WINDOWPAINT) | DCX_DCEBUSY;
799 hdc = dce->hDC;
801 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC )))
803 hdc = 0;
804 goto END;
806 bUpdateVisRgn = bUpdateVisRgn || (dc->w.flags & DC_DIRTY);
808 /* recompute visible region */
809 wndPtr->pDriver->pSetDrawable( wndPtr, hdc, flags, bUpdateClipOrigin );
810 dc->w.flags &= ~DC_DIRTY;
811 GDI_ReleaseObj( hdc );
813 if( bUpdateVisRgn )
815 TRACE("updating visrgn for %08x dce, hwnd [%04x]\n", (unsigned)dce, hwnd);
817 if (flags & DCX_PARENTCLIP)
819 WND *parentPtr = WIN_LockWndPtr(wndPtr->parent);
821 if( wndPtr->dwStyle & WS_VISIBLE && !(parentPtr->dwStyle & WS_MINIMIZE) )
823 if( parentPtr->dwStyle & WS_CLIPSIBLINGS )
824 dcxFlags = DCX_CLIPSIBLINGS | (flags & ~(DCX_CLIPCHILDREN | DCX_WINDOW));
825 else
826 dcxFlags = flags & ~(DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW);
828 hrgnVisible = DCE_GetVisRgn( parentPtr->hwndSelf, dcxFlags,
829 wndPtr->hwndSelf, flags );
830 if( flags & DCX_WINDOW )
831 OffsetRgn( hrgnVisible, -wndPtr->rectWindow.left,
832 -wndPtr->rectWindow.top );
833 else
834 OffsetRgn( hrgnVisible, -wndPtr->rectClient.left,
835 -wndPtr->rectClient.top );
836 DCE_OffsetVisRgn( hdc, hrgnVisible );
838 else
839 hrgnVisible = CreateRectRgn( 0, 0, 0, 0 );
840 WIN_ReleaseWndPtr(parentPtr);
842 else
843 if ((hwnd == GetDesktopWindow()) && !USER_Driver.pIsSingleWindow())
844 hrgnVisible = CreateRectRgn( 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN) );
845 else
847 hrgnVisible = DCE_GetVisRgn( hwnd, flags, 0, 0 );
848 DCE_OffsetVisRgn( hdc, hrgnVisible );
851 dce->DCXflags &= ~DCX_DCEDIRTY;
852 SelectVisRgn16( hdc, hrgnVisible );
854 else
855 TRACE("no visrgn update %08x dce, hwnd [%04x]\n", (unsigned)dce, hwnd);
857 /* apply additional region operation (if any) */
859 if( flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN) )
861 if( !hrgnVisible ) hrgnVisible = CreateRectRgn( 0, 0, 0, 0 );
863 dce->DCXflags |= flags & (DCX_KEEPCLIPRGN | DCX_INTERSECTRGN | DCX_EXCLUDERGN);
864 dce->hClipRgn = hrgnClip;
866 TRACE("\tsaved VisRgn, clipRgn = %04x\n", hrgnClip);
868 SaveVisRgn16( hdc );
869 CombineRgn( hrgnVisible, hrgnClip, 0, RGN_COPY );
870 DCE_OffsetVisRgn( hdc, hrgnVisible );
871 CombineRgn( hrgnVisible, InquireVisRgn16( hdc ), hrgnVisible,
872 (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
873 SelectVisRgn16( hdc, hrgnVisible );
876 if( hrgnVisible ) DeleteObject( hrgnVisible );
878 TRACE("(%04x,%04x,0x%lx): returning %04x\n",
879 hwnd, hrgnClip, flags, hdc);
880 END:
881 WIN_ReleaseWndPtr(wndPtr);
882 return hdc;
886 /***********************************************************************
887 * GetDC16 (USER.66)
889 HDC16 WINAPI GetDC16( HWND16 hwnd )
891 return (HDC16)GetDC( hwnd );
895 /***********************************************************************
896 * GetDC (USER32.230)
897 * RETURNS
898 * :Handle to DC
899 * NULL: Failure
901 HDC WINAPI GetDC(
902 HWND hwnd /* handle of window */
904 if (!hwnd)
905 return GetDCEx( GetDesktopWindow(), 0, DCX_CACHE | DCX_WINDOW );
906 return GetDCEx( hwnd, 0, DCX_USESTYLE );
910 /***********************************************************************
911 * GetWindowDC16 (USER.67)
913 HDC16 WINAPI GetWindowDC16( HWND16 hwnd )
915 if (!hwnd) hwnd = GetDesktopWindow16();
916 return GetDCEx16( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
920 /***********************************************************************
921 * GetWindowDC (USER32.304)
923 HDC WINAPI GetWindowDC( HWND hwnd )
925 if (!hwnd) hwnd = GetDesktopWindow();
926 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
930 /***********************************************************************
931 * ReleaseDC16 (USER.68)
933 INT16 WINAPI ReleaseDC16( HWND16 hwnd, HDC16 hdc )
935 return (INT16)ReleaseDC( hwnd, hdc );
939 /***********************************************************************
940 * ReleaseDC (USER32.440)
942 * RETURNS
943 * 1: Success
944 * 0: Failure
946 INT WINAPI ReleaseDC(
947 HWND hwnd /* Handle of window - ignored */,
948 HDC hdc /* Handle of device context */
950 DCE * dce;
951 INT nRet = 0;
953 WIN_LockWnds();
954 dce = firstDCE;
956 TRACE("%04x %04x\n", hwnd, hdc );
958 while (dce && (dce->hDC != hdc)) dce = dce->next;
960 if ( dce )
961 if ( dce->DCXflags & DCX_DCEBUSY )
962 nRet = DCE_ReleaseDC( dce );
964 WIN_UnlockWnds();
966 return nRet;
969 /***********************************************************************
970 * DCHook (USER.362)
972 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
974 BOOL16 WINAPI DCHook16( HDC16 hDC, WORD code, DWORD data, LPARAM lParam )
976 BOOL retv = TRUE;
977 HRGN hVisRgn;
978 DCE *dce = (DCE *)data;
979 WND *wndPtr;
981 TRACE("hDC = %04x, %i\n", hDC, code);
983 if (!dce) return 0;
984 assert(dce->hDC == hDC);
986 /* Grab the windows lock before doing anything else */
987 WIN_LockWnds();
989 switch( code )
991 case DCHC_INVALIDVISRGN:
993 /* GDI code calls this when it detects that the
994 * DC is dirty (usually after SetHookFlags()). This
995 * means that we have to recompute the visible region.
998 if( dce->DCXflags & DCX_DCEBUSY )
1001 /* Update stale DC in DCX */
1002 wndPtr = WIN_FindWndPtr( dce->hwndCurrent);
1003 if (wndPtr) wndPtr->pDriver->pSetDrawable( wndPtr, dce->hDC, dce->DCXflags, TRUE);
1005 SetHookFlags16(hDC, DCHF_VALIDATEVISRGN);
1006 hVisRgn = DCE_GetVisRgn(dce->hwndCurrent, dce->DCXflags, 0, 0);
1008 TRACE("\tapplying saved clipRgn\n");
1010 /* clip this region with saved clipping region */
1012 if ( (dce->DCXflags & DCX_INTERSECTRGN && dce->hClipRgn != 1) ||
1013 ( dce->DCXflags & DCX_EXCLUDERGN && dce->hClipRgn) )
1016 if( (!dce->hClipRgn && dce->DCXflags & DCX_INTERSECTRGN) ||
1017 (dce->hClipRgn == 1 && dce->DCXflags & DCX_EXCLUDERGN) )
1018 SetRectRgn(hVisRgn,0,0,0,0);
1019 else
1020 CombineRgn(hVisRgn, hVisRgn, dce->hClipRgn,
1021 (dce->DCXflags & DCX_EXCLUDERGN)? RGN_DIFF:RGN_AND);
1023 dce->DCXflags &= ~DCX_DCEDIRTY;
1024 DCE_OffsetVisRgn( hDC, hVisRgn );
1025 SelectVisRgn16(hDC, hVisRgn);
1026 DeleteObject( hVisRgn );
1027 WIN_ReleaseWndPtr( wndPtr ); /* Release WIN_FindWndPtr lock */
1029 else /* non-fatal but shouldn't happen */
1030 WARN("DC is not in use!\n");
1031 break;
1033 case DCHC_DELETEDC:
1035 * Windows will not let you delete a DC that is busy
1036 * (between GetDC and ReleaseDC)
1039 if ( dce->DCXflags & DCX_DCEBUSY )
1041 WARN("Application trying to delete a busy DC\n");
1042 retv = FALSE;
1044 break;
1046 default:
1047 FIXME("unknown code\n");
1050 WIN_UnlockWnds(); /* Release the wnd lock */
1051 return retv;
1055 /**********************************************************************
1056 * WindowFromDC16 (USER.117)
1058 HWND16 WINAPI WindowFromDC16( HDC16 hDC )
1060 return (HWND16)WindowFromDC( hDC );
1064 /**********************************************************************
1065 * WindowFromDC (USER32.581)
1067 HWND WINAPI WindowFromDC( HDC hDC )
1069 DCE *dce;
1070 HWND hwnd;
1072 WIN_LockWnds();
1073 dce = firstDCE;
1075 while (dce && (dce->hDC != hDC)) dce = dce->next;
1077 hwnd = dce ? dce->hwndCurrent : 0;
1078 WIN_UnlockWnds();
1080 return hwnd;
1084 /***********************************************************************
1085 * LockWindowUpdate16 (USER.294)
1087 BOOL16 WINAPI LockWindowUpdate16( HWND16 hwnd )
1089 return LockWindowUpdate( hwnd );
1093 /***********************************************************************
1094 * LockWindowUpdate (USER32.378)
1096 BOOL WINAPI LockWindowUpdate( HWND hwnd )
1098 /* FIXME? DCX_LOCKWINDOWUPDATE is unimplemented */
1099 return TRUE;