Rewrote PSDRV_SetDeviceClipping to use GetRegionData API.
[wine.git] / windows / dce.c
blob092026a4c2afa836fc78aa626211d7f4c3719f09
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 "local.h"
30 #include "debugtools.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 DCE * dce;
75 WND* wnd;
77 if (!(dce = HeapAlloc( SystemHeap, 0, sizeof(DCE) ))) return NULL;
78 if (!(dce->hDC = CreateDC16( "DISPLAY", NULL, NULL, NULL )))
80 HeapFree( SystemHeap, 0, dce );
81 return 0;
84 wnd = WIN_FindWndPtr(hWnd);
86 /* store DCE handle in DC hook data field */
88 SetDCHook( dce->hDC, (FARPROC16)DCHook16, (DWORD)dce );
90 dce->hwndCurrent = hWnd;
91 dce->hClipRgn = 0;
92 dce->next = firstDCE;
93 firstDCE = dce;
95 if( type != DCE_CACHE_DC ) /* owned or class DC */
97 dce->DCXflags = DCX_DCEBUSY;
98 if( hWnd )
100 if( wnd->dwStyle & WS_CLIPCHILDREN ) dce->DCXflags |= DCX_CLIPCHILDREN;
101 if( wnd->dwStyle & WS_CLIPSIBLINGS ) dce->DCXflags |= DCX_CLIPSIBLINGS;
103 SetHookFlags16(dce->hDC,DCHF_INVALIDATEVISRGN);
105 else dce->DCXflags = DCX_CACHE | DCX_DCEEMPTY;
107 WIN_ReleaseWndPtr(wnd);
109 return dce;
113 /***********************************************************************
114 * DCE_FreeDCE
116 DCE* DCE_FreeDCE( DCE *dce )
118 DCE **ppDCE;
120 if (!dce) return NULL;
122 WIN_LockWnds();
124 ppDCE = &firstDCE;
126 while (*ppDCE && (*ppDCE != dce)) ppDCE = &(*ppDCE)->next;
127 if (*ppDCE == dce) *ppDCE = dce->next;
129 SetDCHook(dce->hDC, NULL, 0L);
131 DeleteDC( dce->hDC );
132 if( dce->hClipRgn && !(dce->DCXflags & DCX_KEEPCLIPRGN) )
133 DeleteObject(dce->hClipRgn);
134 HeapFree( SystemHeap, 0, dce );
136 WIN_UnlockWnds();
138 return *ppDCE;
141 /***********************************************************************
142 * DCE_FreeWindowDCE
144 * Remove owned DCE and reset unreleased cache DCEs.
146 void DCE_FreeWindowDCE( WND* pWnd )
148 DCE *pDCE;
150 WIN_LockWnds();
151 pDCE = firstDCE;
153 while( pDCE )
155 if( pDCE->hwndCurrent == pWnd->hwndSelf )
157 if( pDCE == pWnd->dce ) /* owned DCE */
159 pDCE = DCE_FreeDCE( pDCE );
160 pWnd->dce = NULL;
161 continue;
163 else
165 if(!(pDCE->DCXflags & DCX_CACHE) ) /* class DCE */
167 if( pDCE->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN) )
168 DCE_DeleteClipRgn( pDCE );
170 else if( pDCE->DCXflags & DCX_DCEBUSY ) /* shared cache DCE */
172 ERR("[%04x] GetDC() without ReleaseDC()!\n",
173 pWnd->hwndSelf);
174 DCE_ReleaseDC( pDCE );
177 pDCE->DCXflags &= DCX_CACHE;
178 pDCE->DCXflags |= DCX_DCEEMPTY;
179 pDCE->hwndCurrent = 0;
182 pDCE = pDCE->next;
185 WIN_UnlockWnds();
189 /***********************************************************************
190 * DCE_DeleteClipRgn
192 static void DCE_DeleteClipRgn( DCE* dce )
194 dce->DCXflags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN | DCX_WINDOWPAINT);
196 if( dce->DCXflags & DCX_KEEPCLIPRGN )
197 dce->DCXflags &= ~DCX_KEEPCLIPRGN;
198 else
199 if( dce->hClipRgn > 1 )
200 DeleteObject( dce->hClipRgn );
202 dce->hClipRgn = 0;
204 TRACE("\trestoring VisRgn\n");
206 RestoreVisRgn16(dce->hDC);
210 /***********************************************************************
211 * DCE_ReleaseDC
213 static INT DCE_ReleaseDC( DCE* dce )
215 if ((dce->DCXflags & (DCX_DCEEMPTY | DCX_DCEBUSY)) != DCX_DCEBUSY) return 0;
217 /* restore previous visible region */
219 if ((dce->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
220 (dce->DCXflags & (DCX_CACHE | DCX_WINDOWPAINT)) )
221 DCE_DeleteClipRgn( dce );
223 if (dce->DCXflags & DCX_CACHE)
225 SetDCState16( dce->hDC, defaultDCstate );
226 dce->DCXflags &= ~DCX_DCEBUSY;
227 if (dce->DCXflags & DCX_DCEDIRTY)
229 /* don't keep around invalidated entries
230 * because SetDCState() disables hVisRgn updates
231 * by removing dirty bit. */
233 dce->hwndCurrent = 0;
234 dce->DCXflags &= DCX_CACHE;
235 dce->DCXflags |= DCX_DCEEMPTY;
238 return 1;
242 /***********************************************************************
243 * DCE_InvalidateDCE
245 * It is called from SetWindowPos() - we have to mark as dirty all busy
246 * DCEs for windows that have pWnd->parent as an ansector and whose client
247 * rect intersects with specified update rectangle. In addition, pWnd->parent
248 * DCEs may need to be updated if DCX_CLIPCHILDREN flag is set.
250 BOOL DCE_InvalidateDCE(WND* pWnd, const RECT* pRectUpdate)
252 WND* wndScope = WIN_LockWndPtr(pWnd->parent);
253 WND *pDesktop = WIN_GetDesktop();
254 BOOL bRet = FALSE;
256 if( wndScope )
258 DCE *dce;
260 TRACE("scope hwnd = %04x, (%i,%i - %i,%i)\n",
261 wndScope->hwndSelf, pRectUpdate->left,pRectUpdate->top,
262 pRectUpdate->right,pRectUpdate->bottom);
263 if(TRACE_ON(dc))
264 DCE_DumpCache();
266 /* walk all DCEs and fixup non-empty entries */
268 for (dce = firstDCE; (dce); dce = dce->next)
270 if( !(dce->DCXflags & DCX_DCEEMPTY) )
272 WND* wndCurrent = WIN_FindWndPtr(dce->hwndCurrent);
274 if( wndCurrent )
276 WND* wnd = NULL;
277 INT xoffset = 0, yoffset = 0;
279 if( (wndCurrent == wndScope) && !(dce->DCXflags & DCX_CLIPCHILDREN) )
281 /* child window positions don't bother us */
282 WIN_ReleaseWndPtr(wndCurrent);
283 continue;
286 if( !Options.desktopGeometry && wndCurrent == pDesktop )
288 /* don't bother with fake desktop */
289 WIN_ReleaseWndPtr(wndCurrent);
290 continue;
293 /* check if DCE window is within the z-order scope */
295 for( wnd = WIN_LockWndPtr(wndCurrent); wnd; WIN_UpdateWndPtr(&wnd,wnd->parent))
297 if( wnd == wndScope )
299 RECT wndRect;
301 wndRect = wndCurrent->rectWindow;
303 OffsetRect( &wndRect, xoffset - wndCurrent->rectClient.left,
304 yoffset - wndCurrent->rectClient.top);
306 if (pWnd == wndCurrent ||
307 IntersectRect( &wndRect, &wndRect, pRectUpdate ))
309 if( !(dce->DCXflags & DCX_DCEBUSY) )
311 /* Don't bother with visible regions of unused DCEs */
313 TRACE("\tpurged %08x dce [%04x]\n",
314 (unsigned)dce, wndCurrent->hwndSelf);
316 dce->hwndCurrent = 0;
317 dce->DCXflags &= DCX_CACHE;
318 dce->DCXflags |= DCX_DCEEMPTY;
320 else
322 /* Set dirty bits in the hDC and DCE structs */
324 TRACE("\tfixed up %08x dce [%04x]\n",
325 (unsigned)dce, wndCurrent->hwndSelf);
327 dce->DCXflags |= DCX_DCEDIRTY;
328 SetHookFlags16(dce->hDC, DCHF_INVALIDATEVISRGN);
329 bRet = TRUE;
332 WIN_ReleaseWndPtr(wnd);
333 break;
335 xoffset += wnd->rectClient.left;
336 yoffset += wnd->rectClient.top;
339 WIN_ReleaseWndPtr(wndCurrent);
341 } /* dce list */
342 WIN_ReleaseWndPtr(wndScope);
344 WIN_ReleaseDesktop();
345 return bRet;
348 /***********************************************************************
349 * DCE_Init
351 void DCE_Init(void)
353 int i;
354 DCE * dce;
356 for (i = 0; i < NB_DCE; i++)
358 if (!(dce = DCE_AllocDCE( 0, DCE_CACHE_DC ))) return;
359 if (!defaultDCstate) defaultDCstate = GetDCState16( dce->hDC );
364 /***********************************************************************
365 * DCE_GetVisRect
367 * Calculate the visible rectangle of a window (i.e. the client or
368 * window area clipped by the client area of all ancestors) in the
369 * corresponding coordinates. Return FALSE if the visible region is empty.
371 static BOOL DCE_GetVisRect( WND *wndPtr, BOOL clientArea, RECT *lprect )
373 *lprect = clientArea ? wndPtr->rectClient : wndPtr->rectWindow;
375 if (wndPtr->dwStyle & WS_VISIBLE)
377 INT xoffset = lprect->left;
378 INT yoffset = lprect->top;
380 while( !(wndPtr->flags & WIN_NATIVE) &&
381 ( wndPtr = WIN_LockWndPtr(wndPtr->parent)) )
383 if ( (wndPtr->dwStyle & (WS_ICONIC | WS_VISIBLE)) != WS_VISIBLE )
385 WIN_ReleaseWndPtr(wndPtr);
386 goto fail;
389 xoffset += wndPtr->rectClient.left;
390 yoffset += wndPtr->rectClient.top;
391 OffsetRect( lprect, wndPtr->rectClient.left,
392 wndPtr->rectClient.top );
394 if( (wndPtr->rectClient.left >= wndPtr->rectClient.right) ||
395 (wndPtr->rectClient.top >= wndPtr->rectClient.bottom) ||
396 (lprect->left >= wndPtr->rectClient.right) ||
397 (lprect->right <= wndPtr->rectClient.left) ||
398 (lprect->top >= wndPtr->rectClient.bottom) ||
399 (lprect->bottom <= wndPtr->rectClient.top) )
401 WIN_ReleaseWndPtr(wndPtr);
402 goto fail;
405 lprect->left = MAX( lprect->left, wndPtr->rectClient.left );
406 lprect->right = MIN( lprect->right, wndPtr->rectClient.right );
407 lprect->top = MAX( lprect->top, wndPtr->rectClient.top );
408 lprect->bottom = MIN( lprect->bottom, wndPtr->rectClient.bottom );
410 WIN_ReleaseWndPtr(wndPtr);
412 OffsetRect( lprect, -xoffset, -yoffset );
413 return TRUE;
416 fail:
417 SetRectEmpty( lprect );
418 return FALSE;
422 /***********************************************************************
423 * DCE_AddClipRects
425 * Go through the linked list of windows from pWndStart to pWndEnd,
426 * adding to the clip region the intersection of the target rectangle
427 * with an offset window rectangle.
429 static BOOL DCE_AddClipRects( WND *pWndStart, WND *pWndEnd,
430 HRGN hrgnClip, LPRECT lpRect, int x, int y )
432 RECT rect;
434 if( pWndStart->pDriver->pIsSelfClipping( pWndStart ) )
435 return TRUE; /* The driver itself will do the clipping */
437 for (WIN_LockWndPtr(pWndStart); pWndStart != pWndEnd; WIN_UpdateWndPtr(&pWndStart,pWndStart->next))
439 if( !(pWndStart->dwStyle & WS_VISIBLE) ) continue;
441 rect.left = pWndStart->rectWindow.left + x;
442 rect.top = pWndStart->rectWindow.top + y;
443 rect.right = pWndStart->rectWindow.right + x;
444 rect.bottom = pWndStart->rectWindow.bottom + y;
446 if( IntersectRect( &rect, &rect, lpRect ))
448 if(!REGION_UnionRectWithRgn( hrgnClip, &rect )) break;
451 WIN_ReleaseWndPtr(pWndStart);
452 return (pWndStart == pWndEnd);
456 /***********************************************************************
457 * DCE_GetVisRgn
459 * Return the visible region of a window, i.e. the client or window area
460 * clipped by the client area of all ancestors, and then optionally
461 * by siblings and children.
463 HRGN DCE_GetVisRgn( HWND hwnd, WORD flags, HWND hwndChild, WORD cflags )
465 HRGN hrgnVis = 0;
466 RECT rect;
467 WND *wndPtr = WIN_FindWndPtr( hwnd );
468 WND *childWnd = WIN_FindWndPtr( hwndChild );
470 /* Get visible rectangle and create a region with it. */
472 if (wndPtr && DCE_GetVisRect(wndPtr, !(flags & DCX_WINDOW), &rect))
474 if((hrgnVis = CreateRectRgnIndirect( &rect )))
476 HRGN hrgnClip = CreateRectRgn( 0, 0, 0, 0 );
477 INT xoffset, yoffset;
479 if( hrgnClip )
481 /* Compute obscured region for the visible rectangle by
482 * clipping children, siblings, and ancestors. Note that
483 * DCE_GetVisRect() returns a rectangle either in client
484 * or in window coordinates (for DCX_WINDOW request). */
486 if( (flags & DCX_CLIPCHILDREN) && wndPtr->child )
488 if( flags & DCX_WINDOW )
490 /* adjust offsets since child window rectangles are
491 * in client coordinates */
493 xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
494 yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
496 else
497 xoffset = yoffset = 0;
499 DCE_AddClipRects( wndPtr->child, NULL, hrgnClip,
500 &rect, xoffset, yoffset );
503 /* We may need to clip children of child window, if a window with PARENTDC
504 * class style and CLIPCHILDREN window style (like in Free Agent 16
505 * preference dialogs) gets here, we take the region for the parent window
506 * but apparently still need to clip the children of the child window... */
508 if( (cflags & DCX_CLIPCHILDREN) && childWnd && childWnd->child )
510 if( flags & DCX_WINDOW )
512 /* adjust offsets since child window rectangles are
513 * in client coordinates */
515 xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
516 yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
518 else
519 xoffset = yoffset = 0;
521 /* client coordinates of child window */
522 xoffset += childWnd->rectClient.left;
523 yoffset += childWnd->rectClient.top;
525 DCE_AddClipRects( childWnd->child, NULL, hrgnClip,
526 &rect, xoffset, yoffset );
529 /* sibling window rectangles are in client
530 * coordinates of the parent window */
532 if (flags & DCX_WINDOW)
534 xoffset = -wndPtr->rectWindow.left;
535 yoffset = -wndPtr->rectWindow.top;
537 else
539 xoffset = -wndPtr->rectClient.left;
540 yoffset = -wndPtr->rectClient.top;
543 if (flags & DCX_CLIPSIBLINGS && wndPtr->parent )
544 DCE_AddClipRects( wndPtr->parent->child,
545 wndPtr, hrgnClip, &rect, xoffset, yoffset );
547 /* Clip siblings of all ancestors that have the
548 * WS_CLIPSIBLINGS style
551 while (wndPtr->dwStyle & WS_CHILD)
553 WIN_UpdateWndPtr(&wndPtr,wndPtr->parent);
554 xoffset -= wndPtr->rectClient.left;
555 yoffset -= wndPtr->rectClient.top;
556 if(wndPtr->dwStyle & WS_CLIPSIBLINGS && wndPtr->parent)
558 DCE_AddClipRects( wndPtr->parent->child, wndPtr,
559 hrgnClip, &rect, xoffset, yoffset );
563 /* Now once we've got a jumbo clip region we have
564 * to substract it from the visible rectangle.
567 CombineRgn( hrgnVis, hrgnVis, hrgnClip, RGN_DIFF );
568 DeleteObject( hrgnClip );
570 else
572 DeleteObject( hrgnVis );
573 hrgnVis = 0;
577 else
578 hrgnVis = CreateRectRgn(0, 0, 0, 0); /* empty */
579 WIN_ReleaseWndPtr(wndPtr);
580 WIN_ReleaseWndPtr(childWnd);
581 return hrgnVis;
584 /***********************************************************************
585 * DCE_OffsetVisRgn
587 * Change region from DC-origin relative coordinates to screen coords.
590 static void DCE_OffsetVisRgn( HDC hDC, HRGN hVisRgn )
592 DC *dc;
593 if (!(dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC ))) return;
595 OffsetRgn( hVisRgn, dc->w.DCOrgX, dc->w.DCOrgY );
597 GDI_HEAP_UNLOCK( hDC );
600 /***********************************************************************
601 * DCE_ExcludeRgn
603 * Translate given region from the wnd client to the DC coordinates
604 * and add it to the clipping region.
606 INT16 DCE_ExcludeRgn( HDC hDC, WND* wnd, HRGN hRgn )
608 POINT pt = {0, 0};
609 DCE *dce = firstDCE;
611 while (dce && (dce->hDC != hDC)) dce = dce->next;
612 if( dce )
614 MapWindowPoints( wnd->hwndSelf, dce->hwndCurrent, &pt, 1);
615 if( dce->DCXflags & DCX_WINDOW )
617 wnd = WIN_FindWndPtr(dce->hwndCurrent);
618 pt.x += wnd->rectClient.left - wnd->rectWindow.left;
619 pt.y += wnd->rectClient.top - wnd->rectWindow.top;
620 WIN_ReleaseWndPtr(wnd);
623 else return ERROR;
624 OffsetRgn(hRgn, pt.x, pt.y);
626 return ExtSelectClipRgn( hDC, hRgn, RGN_DIFF );
630 /***********************************************************************
631 * GetDCEx16 (USER.359)
633 HDC16 WINAPI GetDCEx16( HWND16 hwnd, HRGN16 hrgnClip, DWORD flags )
635 return (HDC16)GetDCEx( hwnd, hrgnClip, flags );
639 /***********************************************************************
640 * GetDCEx32 (USER32.231)
642 * Unimplemented flags: DCX_LOCKWINDOWUPDATE
644 * FIXME: Full support for hrgnClip == 1 (alias for entire window).
646 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
648 HRGN hrgnVisible = 0;
649 HDC hdc = 0;
650 DCE * dce;
651 DC * dc;
652 WND * wndPtr;
653 DWORD dcxFlags = 0;
654 BOOL bUpdateVisRgn = TRUE;
655 BOOL bUpdateClipOrigin = FALSE;
657 TRACE("hwnd %04x, hrgnClip %04x, flags %08x\n",
658 hwnd, hrgnClip, (unsigned)flags);
660 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
662 /* fixup flags */
664 if (!(wndPtr->class->style & (CS_OWNDC | CS_CLASSDC))) flags |= DCX_CACHE;
666 if (flags & DCX_USESTYLE)
668 flags &= ~( DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
670 if( wndPtr->dwStyle & WS_CLIPSIBLINGS )
671 flags |= DCX_CLIPSIBLINGS;
673 if ( !(flags & DCX_WINDOW) )
675 if (wndPtr->class->style & CS_PARENTDC) flags |= DCX_PARENTCLIP;
677 if (wndPtr->dwStyle & WS_CLIPCHILDREN &&
678 !(wndPtr->dwStyle & WS_MINIMIZE) ) flags |= DCX_CLIPCHILDREN;
680 else flags |= DCX_CACHE;
683 if( flags & DCX_NOCLIPCHILDREN )
685 flags |= DCX_CACHE;
686 flags &= ~(DCX_PARENTCLIP | DCX_CLIPCHILDREN);
689 if (flags & DCX_WINDOW)
690 flags = (flags & ~DCX_CLIPCHILDREN) | DCX_CACHE;
692 if (!(wndPtr->dwStyle & WS_CHILD) || !wndPtr->parent )
693 flags &= ~DCX_PARENTCLIP;
694 else if( flags & DCX_PARENTCLIP )
696 flags |= DCX_CACHE;
697 if( !(flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) )
698 if( (wndPtr->dwStyle & WS_VISIBLE) && (wndPtr->parent->dwStyle & WS_VISIBLE) )
700 flags &= ~DCX_CLIPCHILDREN;
701 if( wndPtr->parent->dwStyle & WS_CLIPSIBLINGS )
702 flags |= DCX_CLIPSIBLINGS;
706 /* find a suitable DCE */
708 dcxFlags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
709 DCX_CACHE | DCX_WINDOW);
711 if (flags & DCX_CACHE)
713 DCE* dceEmpty;
714 DCE* dceUnused;
716 dceEmpty = dceUnused = NULL;
718 /* Strategy: First, we attempt to find a non-empty but unused DCE with
719 * compatible flags. Next, we look for an empty entry. If the cache is
720 * full we have to purge one of the unused entries.
723 for (dce = firstDCE; (dce); dce = dce->next)
725 if ((dce->DCXflags & (DCX_CACHE | DCX_DCEBUSY)) == DCX_CACHE )
727 dceUnused = dce;
729 if (dce->DCXflags & DCX_DCEEMPTY)
730 dceEmpty = dce;
731 else
732 if ((dce->hwndCurrent == hwnd) &&
733 ((dce->DCXflags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
734 DCX_CACHE | DCX_WINDOW | DCX_PARENTCLIP)) == dcxFlags))
736 TRACE("\tfound valid %08x dce [%04x], flags %08x\n",
737 (unsigned)dce, hwnd, (unsigned)dcxFlags );
738 bUpdateVisRgn = FALSE;
739 bUpdateClipOrigin = TRUE;
740 break;
745 if (!dce) dce = (dceEmpty) ? dceEmpty : dceUnused;
747 /* if there's no dce empty or unused, allocate a new one */
748 if (!dce)
750 dce = DCE_AllocDCE( 0, DCE_CACHE_DC );
753 else
755 dce = (wndPtr->class->style & CS_OWNDC) ? wndPtr->dce : wndPtr->class->dce;
756 if( dce->hwndCurrent == hwnd )
758 TRACE("\tskipping hVisRgn update\n");
759 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
761 if( (dce->DCXflags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN)) &&
762 (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN)) )
764 /* This is likely to be a nested BeginPaint(). */
766 if( dce->hClipRgn != hrgnClip )
768 FIXME("new hrgnClip[%04x] smashes the previous[%04x]\n",
769 hrgnClip, dce->hClipRgn );
770 DCE_DeleteClipRgn( dce );
772 else
773 RestoreVisRgn16(dce->hDC);
777 if (!dce)
779 hdc = 0;
780 goto END;
783 dce->hwndCurrent = hwnd;
784 dce->hClipRgn = 0;
785 dce->DCXflags = dcxFlags | (flags & DCX_WINDOWPAINT) | DCX_DCEBUSY;
786 hdc = dce->hDC;
788 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC )))
790 hdc = 0;
791 goto END;
793 bUpdateVisRgn = bUpdateVisRgn || (dc->w.flags & DC_DIRTY);
795 /* recompute visible region */
797 wndPtr->pDriver->pSetDrawable( wndPtr, dc, flags, bUpdateClipOrigin );
798 if( bUpdateVisRgn )
800 TRACE("updating visrgn for %08x dce, hwnd [%04x]\n", (unsigned)dce, hwnd);
802 if (flags & DCX_PARENTCLIP)
804 WND *parentPtr = WIN_LockWndPtr(wndPtr->parent);
806 if( wndPtr->dwStyle & WS_VISIBLE && !(parentPtr->dwStyle & WS_MINIMIZE) )
808 if( parentPtr->dwStyle & WS_CLIPSIBLINGS )
809 dcxFlags = DCX_CLIPSIBLINGS | (flags & ~(DCX_CLIPCHILDREN | DCX_WINDOW));
810 else
811 dcxFlags = flags & ~(DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW);
813 hrgnVisible = DCE_GetVisRgn( parentPtr->hwndSelf, dcxFlags,
814 wndPtr->hwndSelf, flags );
815 if( flags & DCX_WINDOW )
816 OffsetRgn( hrgnVisible, -wndPtr->rectWindow.left,
817 -wndPtr->rectWindow.top );
818 else
819 OffsetRgn( hrgnVisible, -wndPtr->rectClient.left,
820 -wndPtr->rectClient.top );
821 DCE_OffsetVisRgn( hdc, hrgnVisible );
823 else
824 hrgnVisible = CreateRectRgn( 0, 0, 0, 0 );
825 WIN_ReleaseWndPtr(parentPtr);
827 else
828 if ((hwnd == GetDesktopWindow()) && !DESKTOP_IsSingleWindow())
829 hrgnVisible = CreateRectRgn( 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN) );
830 else
832 hrgnVisible = DCE_GetVisRgn( hwnd, flags, 0, 0 );
833 DCE_OffsetVisRgn( hdc, hrgnVisible );
836 dc->w.flags &= ~DC_DIRTY;
837 dce->DCXflags &= ~DCX_DCEDIRTY;
838 SelectVisRgn16( hdc, hrgnVisible );
840 else
841 TRACE("no visrgn update %08x dce, hwnd [%04x]\n", (unsigned)dce, hwnd);
843 /* apply additional region operation (if any) */
845 if( flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN) )
847 if( !hrgnVisible ) hrgnVisible = CreateRectRgn( 0, 0, 0, 0 );
849 dce->DCXflags |= flags & (DCX_KEEPCLIPRGN | DCX_INTERSECTRGN | DCX_EXCLUDERGN);
850 dce->hClipRgn = hrgnClip;
852 TRACE("\tsaved VisRgn, clipRgn = %04x\n", hrgnClip);
854 SaveVisRgn16( hdc );
855 CombineRgn( hrgnVisible, hrgnClip, 0, RGN_COPY );
856 DCE_OffsetVisRgn( hdc, hrgnVisible );
857 CombineRgn( hrgnVisible, InquireVisRgn16( hdc ), hrgnVisible,
858 (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
859 SelectVisRgn16( hdc, hrgnVisible );
862 if( hrgnVisible ) DeleteObject( hrgnVisible );
864 TRACE("(%04x,%04x,0x%lx): returning %04x\n",
865 hwnd, hrgnClip, flags, hdc);
866 END:
867 WIN_ReleaseWndPtr(wndPtr);
868 return hdc;
872 /***********************************************************************
873 * GetDC16 (USER.66)
875 HDC16 WINAPI GetDC16( HWND16 hwnd )
877 return (HDC16)GetDC( hwnd );
881 /***********************************************************************
882 * GetDC32 (USER32.230)
883 * RETURNS
884 * :Handle to DC
885 * NULL: Failure
887 HDC WINAPI GetDC(
888 HWND hwnd /* handle of window */
890 if (!hwnd)
891 return GetDCEx( GetDesktopWindow(), 0, DCX_CACHE | DCX_WINDOW );
892 return GetDCEx( hwnd, 0, DCX_USESTYLE );
896 /***********************************************************************
897 * GetWindowDC16 (USER.67)
899 HDC16 WINAPI GetWindowDC16( HWND16 hwnd )
901 if (!hwnd) hwnd = GetDesktopWindow16();
902 return GetDCEx16( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
906 /***********************************************************************
907 * GetWindowDC32 (USER32.304)
909 HDC WINAPI GetWindowDC( HWND hwnd )
911 if (!hwnd) hwnd = GetDesktopWindow();
912 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
916 /***********************************************************************
917 * ReleaseDC16 (USER.68)
919 INT16 WINAPI ReleaseDC16( HWND16 hwnd, HDC16 hdc )
921 return (INT16)ReleaseDC( hwnd, hdc );
925 /***********************************************************************
926 * ReleaseDC32 (USER32.440)
928 * RETURNS
929 * 1: Success
930 * 0: Failure
932 INT WINAPI ReleaseDC(
933 HWND hwnd /* Handle of window - ignored */,
934 HDC hdc /* Handle of device context */
936 DCE * dce;
937 INT nRet = 0;
939 WIN_LockWnds();
940 dce = firstDCE;
942 TRACE("%04x %04x\n", hwnd, hdc );
944 while (dce && (dce->hDC != hdc)) dce = dce->next;
946 if ( dce )
947 if ( dce->DCXflags & DCX_DCEBUSY )
948 nRet = DCE_ReleaseDC( dce );
950 WIN_UnlockWnds();
952 return nRet;
955 /***********************************************************************
956 * DCHook (USER.362)
958 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
960 BOOL16 WINAPI DCHook16( HDC16 hDC, WORD code, DWORD data, LPARAM lParam )
962 BOOL retv = TRUE;
963 HRGN hVisRgn;
964 DCE *dce = (DCE *)data;
965 DC *dc;
966 WND *wndPtr;
968 TRACE("hDC = %04x, %i\n", hDC, code);
970 if (!dce) return 0;
971 assert(dce->hDC == hDC);
973 /* Grab the windows lock before doing anything else */
974 WIN_LockWnds();
976 switch( code )
978 case DCHC_INVALIDVISRGN:
980 /* GDI code calls this when it detects that the
981 * DC is dirty (usually after SetHookFlags()). This
982 * means that we have to recompute the visible region.
985 if( dce->DCXflags & DCX_DCEBUSY )
988 /* Update stale DC in DCX */
989 wndPtr = WIN_FindWndPtr( dce->hwndCurrent);
990 dc = (DC *) GDI_GetObjPtr( dce->hDC, DC_MAGIC);
991 if( dc && wndPtr)
992 wndPtr->pDriver->pSetDrawable( wndPtr, dc,dce->DCXflags,TRUE);
994 SetHookFlags16(hDC, DCHF_VALIDATEVISRGN);
995 hVisRgn = DCE_GetVisRgn(dce->hwndCurrent, dce->DCXflags, 0, 0);
997 TRACE("\tapplying saved clipRgn\n");
999 /* clip this region with saved clipping region */
1001 if ( (dce->DCXflags & DCX_INTERSECTRGN && dce->hClipRgn != 1) ||
1002 ( dce->DCXflags & DCX_EXCLUDERGN && dce->hClipRgn) )
1005 if( (!dce->hClipRgn && dce->DCXflags & DCX_INTERSECTRGN) ||
1006 (dce->hClipRgn == 1 && dce->DCXflags & DCX_EXCLUDERGN) )
1007 SetRectRgn(hVisRgn,0,0,0,0);
1008 else
1009 CombineRgn(hVisRgn, hVisRgn, dce->hClipRgn,
1010 (dce->DCXflags & DCX_EXCLUDERGN)? RGN_DIFF:RGN_AND);
1012 dce->DCXflags &= ~DCX_DCEDIRTY;
1013 DCE_OffsetVisRgn( hDC, hVisRgn );
1014 SelectVisRgn16(hDC, hVisRgn);
1015 DeleteObject( hVisRgn );
1016 WIN_ReleaseWndPtr( wndPtr ); /* Release WIN_FindWndPtr lock */
1018 else /* non-fatal but shouldn't happen */
1019 WARN("DC is not in use!\n");
1020 break;
1022 case DCHC_DELETEDC:
1024 * Windows will not let you delete a DC that is busy
1025 * (between GetDC and ReleaseDC)
1028 if ( dce->DCXflags & DCX_DCEBUSY )
1030 WARN("Application trying to delete a busy DC\n");
1031 retv = FALSE;
1033 break;
1035 default:
1036 FIXME("unknown code\n");
1039 WIN_UnlockWnds(); /* Release the wnd lock */
1040 return retv;
1044 /**********************************************************************
1045 * WindowFromDC16 (USER.117)
1047 HWND16 WINAPI WindowFromDC16( HDC16 hDC )
1049 return (HWND16)WindowFromDC( hDC );
1053 /**********************************************************************
1054 * WindowFromDC32 (USER32.581)
1056 HWND WINAPI WindowFromDC( HDC hDC )
1058 DCE *dce;
1059 HWND hwnd;
1061 WIN_LockWnds();
1062 dce = firstDCE;
1064 while (dce && (dce->hDC != hDC)) dce = dce->next;
1066 hwnd = dce ? dce->hwndCurrent : 0;
1067 WIN_UnlockWnds();
1069 return hwnd;
1073 /***********************************************************************
1074 * LockWindowUpdate16 (USER.294)
1076 BOOL16 WINAPI LockWindowUpdate16( HWND16 hwnd )
1078 return LockWindowUpdate( hwnd );
1082 /***********************************************************************
1083 * LockWindowUpdate32 (USER32.378)
1085 BOOL WINAPI LockWindowUpdate( HWND hwnd )
1087 /* FIXME? DCX_LOCKWINDOWUPDATE is unimplemented */
1088 return TRUE;