Code added to re-evaluate stale DC information in DCE cache.
[wine] / windows / dce.c
1 /*
2  * USER DCE functions
3  *
4  * Copyright 1993 Alexandre Julliard
5  *           1996,1997 Alex Korobka
6  *
7  *
8  * Note: Visible regions of CS_OWNDC/CS_CLASSDC window DCs 
9  * have to be updated dynamically. 
10  * 
11  * Internal DCX flags:
12  *
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
18  */
19
20 #include "desktop.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 "sysmetrics.h"
29 #include "local.h"
30 #include "debug.h"
31 #include "wine/winuser16.h"
32
33 DEFAULT_DEBUG_CHANNEL(dc)
34
35 #define NB_DCE    5  /* Number of DCEs created at startup */
36
37 static DCE *firstDCE = 0;
38 static HDC defaultDCstate = 0;
39
40 static void DCE_DeleteClipRgn( DCE* );
41 static INT DCE_ReleaseDC( DCE* );
42
43
44 /***********************************************************************
45  *           DCE_DumpCache
46  */
47 static void DCE_DumpCache(void)
48 {
49     DCE *dce;
50     
51     WIN_LockWnds();
52     dce = firstDCE;
53     
54     DUMP("DCE:\n");
55     while( dce )
56     {
57         DUMP("\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;
62     }
63
64     WIN_UnlockWnds();
65 }
66
67 /***********************************************************************
68  *           DCE_AllocDCE
69  *
70  * Allocate a new DCE.
71  */
72 DCE *DCE_AllocDCE( HWND hWnd, DCE_TYPE type )
73 {
74     DCE * dce;
75     WND* wnd;
76     
77     if (!(dce = HeapAlloc( SystemHeap, 0, sizeof(DCE) ))) return NULL;
78     if (!(dce->hDC = CreateDC16( "DISPLAY", NULL, NULL, NULL )))
79     {
80         HeapFree( SystemHeap, 0, dce );
81         return 0;
82     }
83
84     wnd = WIN_FindWndPtr(hWnd);
85     
86     /* store DCE handle in DC hook data field */
87
88     SetDCHook( dce->hDC, (FARPROC16)DCHook16, (DWORD)dce );
89
90     dce->hwndCurrent = hWnd;
91     dce->hClipRgn    = 0;
92     dce->next        = firstDCE;
93     firstDCE = dce;
94
95     if( type != DCE_CACHE_DC ) /* owned or class DC */
96     {
97         dce->DCXflags = DCX_DCEBUSY;
98         if( hWnd )
99         {
100             if( wnd->dwStyle & WS_CLIPCHILDREN ) dce->DCXflags |= DCX_CLIPCHILDREN;
101             if( wnd->dwStyle & WS_CLIPSIBLINGS ) dce->DCXflags |= DCX_CLIPSIBLINGS;
102         }
103         SetHookFlags16(dce->hDC,DCHF_INVALIDATEVISRGN);
104     }
105     else dce->DCXflags = DCX_CACHE | DCX_DCEEMPTY;
106
107     WIN_ReleaseWndPtr(wnd);
108     
109     return dce;
110 }
111
112
113 /***********************************************************************
114  *           DCE_FreeDCE
115  */
116 DCE* DCE_FreeDCE( DCE *dce )
117 {
118     DCE **ppDCE;
119
120     if (!dce) return NULL;
121
122     WIN_LockWnds();
123
124     ppDCE = &firstDCE;
125
126     while (*ppDCE && (*ppDCE != dce)) ppDCE = &(*ppDCE)->next;
127     if (*ppDCE == dce) *ppDCE = dce->next;
128
129     SetDCHook(dce->hDC, NULL, 0L);
130
131     DeleteDC( dce->hDC );
132     if( dce->hClipRgn && !(dce->DCXflags & DCX_KEEPCLIPRGN) )
133         DeleteObject(dce->hClipRgn);
134     HeapFree( SystemHeap, 0, dce );
135
136     WIN_UnlockWnds();
137     
138     return *ppDCE;
139 }
140
141 /***********************************************************************
142  *           DCE_FreeWindowDCE
143  *
144  * Remove owned DCE and reset unreleased cache DCEs.
145  */
146 void DCE_FreeWindowDCE( WND* pWnd )
147 {
148     DCE *pDCE;
149
150     WIN_LockWnds();
151     pDCE = firstDCE;
152
153     while( pDCE )
154     {
155         if( pDCE->hwndCurrent == pWnd->hwndSelf )
156         {
157             if( pDCE == pWnd->dce ) /* owned DCE */
158             {
159                 pDCE = DCE_FreeDCE( pDCE );
160                 pWnd->dce = NULL;
161                 continue;
162             }
163             else
164             {
165                 if(!(pDCE->DCXflags & DCX_CACHE) ) /* class DCE */
166                 {
167                     if( pDCE->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN) )
168                         DCE_DeleteClipRgn( pDCE );
169                 }
170                 else if( pDCE->DCXflags & DCX_DCEBUSY ) /* shared cache DCE */
171                 {
172                     ERR(dc,"[%04x] GetDC() without ReleaseDC()!\n", 
173                         pWnd->hwndSelf);
174                     DCE_ReleaseDC( pDCE );
175                 }
176
177                 pDCE->DCXflags &= DCX_CACHE;
178                 pDCE->DCXflags |= DCX_DCEEMPTY;
179                 pDCE->hwndCurrent = 0;
180             }
181         }
182         pDCE = pDCE->next;
183     }
184     
185     WIN_UnlockWnds();
186 }
187
188
189 /***********************************************************************
190  *   DCE_DeleteClipRgn
191  */
192 static void DCE_DeleteClipRgn( DCE* dce )
193 {
194     dce->DCXflags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN | DCX_WINDOWPAINT);
195
196     if( dce->DCXflags & DCX_KEEPCLIPRGN )
197         dce->DCXflags &= ~DCX_KEEPCLIPRGN;
198     else
199         if( dce->hClipRgn > 1 )
200             DeleteObject( dce->hClipRgn );
201
202     dce->hClipRgn = 0;
203
204     TRACE(dc,"\trestoring VisRgn\n");
205
206     RestoreVisRgn16(dce->hDC);
207 }
208
209
210 /***********************************************************************
211  *   DCE_ReleaseDC
212  */
213 static INT DCE_ReleaseDC( DCE* dce )
214 {
215     if ((dce->DCXflags & (DCX_DCEEMPTY | DCX_DCEBUSY)) != DCX_DCEBUSY) return 0;
216
217     /* restore previous visible region */
218
219     if ((dce->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
220         (dce->DCXflags & (DCX_CACHE | DCX_WINDOWPAINT)) )
221         DCE_DeleteClipRgn( dce );
222
223     if (dce->DCXflags & DCX_CACHE)
224     {
225         SetDCState16( dce->hDC, defaultDCstate );
226         dce->DCXflags &= ~DCX_DCEBUSY;
227         if (dce->DCXflags & DCX_DCEDIRTY)
228         {
229             /* don't keep around invalidated entries 
230              * because SetDCState() disables hVisRgn updates
231              * by removing dirty bit. */
232
233             dce->hwndCurrent = 0;
234             dce->DCXflags &= DCX_CACHE;
235             dce->DCXflags |= DCX_DCEEMPTY;
236         }
237     }
238     return 1;
239 }
240
241
242 /***********************************************************************
243  *   DCE_InvalidateDCE
244  *
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.
249  */
250 BOOL DCE_InvalidateDCE(WND* pWnd, const RECT* pRectUpdate)
251 {
252     WND* wndScope = WIN_LockWndPtr(pWnd->parent);
253     WND *pDesktop = WIN_GetDesktop();
254     BOOL bRet = FALSE;
255
256     if( wndScope )
257     {
258         DCE *dce;
259
260         TRACE(dc,"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();
265
266         /* walk all DCEs and fixup non-empty entries */
267
268         for (dce = firstDCE; (dce); dce = dce->next)
269         {
270             if( !(dce->DCXflags & DCX_DCEEMPTY) )
271             {
272                 WND* wndCurrent = WIN_FindWndPtr(dce->hwndCurrent);
273
274                 if( wndCurrent )
275                 {
276                     WND* wnd = NULL;
277                     INT xoffset = 0, yoffset = 0;
278
279                     if( (wndCurrent == wndScope) && !(dce->DCXflags & DCX_CLIPCHILDREN) )
280                     {
281                         /* child window positions don't bother us */
282                         WIN_ReleaseWndPtr(wndCurrent);
283                         continue;
284                     }
285
286                     if( !Options.desktopGeometry && wndCurrent == pDesktop )
287                     {
288                         /* don't bother with fake desktop */
289                         WIN_ReleaseWndPtr(wndCurrent);
290                         continue;
291                     }
292
293                     /* check if DCE window is within the z-order scope */
294
295                     for( wnd = WIN_LockWndPtr(wndCurrent); wnd; WIN_UpdateWndPtr(&wnd,wnd->parent))
296                     {
297                         if( wnd == wndScope )
298                         {
299                             RECT wndRect;
300
301                             wndRect = wndCurrent->rectWindow;
302
303                             OffsetRect( &wndRect, xoffset - wndCurrent->rectClient.left, 
304                                                     yoffset - wndCurrent->rectClient.top);
305
306                             if (pWnd == wndCurrent ||
307                                 IntersectRect( &wndRect, &wndRect, pRectUpdate ))
308                             { 
309                                 if( !(dce->DCXflags & DCX_DCEBUSY) )
310                                 {
311                                     /* Don't bother with visible regions of unused DCEs */
312
313                                     TRACE(dc,"\tpurged %08x dce [%04x]\n", 
314                                                 (unsigned)dce, wndCurrent->hwndSelf);
315
316                                     dce->hwndCurrent = 0;
317                                     dce->DCXflags &= DCX_CACHE;
318                                     dce->DCXflags |= DCX_DCEEMPTY;
319                                 }
320                                 else
321                                 {
322                                     /* Set dirty bits in the hDC and DCE structs */
323
324                                     TRACE(dc,"\tfixed up %08x dce [%04x]\n", 
325                                                 (unsigned)dce, wndCurrent->hwndSelf);
326
327                                     dce->DCXflags |= DCX_DCEDIRTY;
328                                     SetHookFlags16(dce->hDC, DCHF_INVALIDATEVISRGN);
329                                     bRet = TRUE;
330                                 }
331                             }
332                             WIN_ReleaseWndPtr(wnd);
333                             break;
334                         }
335                         xoffset += wnd->rectClient.left;
336                         yoffset += wnd->rectClient.top;
337                     }
338                 }
339                 WIN_ReleaseWndPtr(wndCurrent);
340             }
341         } /* dce list */
342         WIN_ReleaseWndPtr(wndScope);
343     }
344     WIN_ReleaseDesktop();
345     return bRet;
346 }
347
348 /***********************************************************************
349  *           DCE_Init
350  */
351 void DCE_Init(void)
352 {
353     int i;
354     DCE * dce;
355         
356     for (i = 0; i < NB_DCE; i++)
357     {
358         if (!(dce = DCE_AllocDCE( 0, DCE_CACHE_DC ))) return;
359         if (!defaultDCstate) defaultDCstate = GetDCState16( dce->hDC );
360     }
361 }
362
363
364 /***********************************************************************
365  *           DCE_GetVisRect
366  *
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.
370  */
371 static BOOL DCE_GetVisRect( WND *wndPtr, BOOL clientArea, RECT *lprect )
372 {
373     *lprect = clientArea ? wndPtr->rectClient : wndPtr->rectWindow;
374
375     if (wndPtr->dwStyle & WS_VISIBLE)
376     {
377         INT xoffset = lprect->left;
378         INT yoffset = lprect->top;
379
380         while (wndPtr->dwStyle & WS_CHILD)
381         {
382             wndPtr = WIN_LockWndPtr(wndPtr->parent);
383
384             if ( (wndPtr->dwStyle & (WS_ICONIC | WS_VISIBLE)) != WS_VISIBLE )
385             {
386                 WIN_ReleaseWndPtr(wndPtr);
387                 goto fail;
388             }
389
390             xoffset += wndPtr->rectClient.left;
391             yoffset += wndPtr->rectClient.top;
392             OffsetRect( lprect, wndPtr->rectClient.left,
393                                   wndPtr->rectClient.top );
394
395             if( (wndPtr->rectClient.left >= wndPtr->rectClient.right) ||
396                 (wndPtr->rectClient.top >= wndPtr->rectClient.bottom) ||
397                 (lprect->left >= wndPtr->rectClient.right) ||
398                 (lprect->right <= wndPtr->rectClient.left) ||
399                 (lprect->top >= wndPtr->rectClient.bottom) ||
400                 (lprect->bottom <= wndPtr->rectClient.top) )
401             {
402                 WIN_ReleaseWndPtr(wndPtr);
403                 goto fail;
404             }
405
406             lprect->left = MAX( lprect->left, wndPtr->rectClient.left );
407             lprect->right = MIN( lprect->right, wndPtr->rectClient.right );
408             lprect->top = MAX( lprect->top, wndPtr->rectClient.top );
409             lprect->bottom = MIN( lprect->bottom, wndPtr->rectClient.bottom );
410
411             WIN_ReleaseWndPtr(wndPtr);
412         }
413         OffsetRect( lprect, -xoffset, -yoffset );
414         return TRUE;
415     }
416
417 fail:
418     SetRectEmpty( lprect );
419     return FALSE;
420 }
421
422
423 /***********************************************************************
424  *           DCE_AddClipRects
425  *
426  * Go through the linked list of windows from pWndStart to pWndEnd,
427  * adding to the clip region the intersection of the target rectangle
428  * with an offset window rectangle.
429  */
430 static BOOL DCE_AddClipRects( WND *pWndStart, WND *pWndEnd, 
431                                 HRGN hrgnClip, LPRECT lpRect, int x, int y )
432 {
433     RECT rect;
434
435     if( pWndStart->pDriver->pIsSelfClipping( pWndStart ) )
436         return TRUE; /* The driver itself will do the clipping */
437
438     for (WIN_LockWndPtr(pWndStart); pWndStart != pWndEnd; WIN_UpdateWndPtr(&pWndStart,pWndStart->next))
439     {
440         if( !(pWndStart->dwStyle & WS_VISIBLE) ) continue;
441             
442         rect.left = pWndStart->rectWindow.left + x;
443         rect.top = pWndStart->rectWindow.top + y;
444         rect.right = pWndStart->rectWindow.right + x;
445         rect.bottom = pWndStart->rectWindow.bottom + y;
446
447         if( IntersectRect( &rect, &rect, lpRect ))
448         {
449             if(!REGION_UnionRectWithRgn( hrgnClip, &rect )) break;
450         }
451     }
452     WIN_ReleaseWndPtr(pWndStart);
453     return (pWndStart == pWndEnd);
454 }
455
456
457 /***********************************************************************
458  *           DCE_GetVisRgn
459  *
460  * Return the visible region of a window, i.e. the client or window area
461  * clipped by the client area of all ancestors, and then optionally
462  * by siblings and children.
463  */
464 HRGN DCE_GetVisRgn( HWND hwnd, WORD flags, HWND hwndChild, WORD cflags )
465 {
466     HRGN hrgnVis = 0;
467     RECT rect;
468     WND *wndPtr = WIN_FindWndPtr( hwnd );
469     WND *childWnd = WIN_FindWndPtr( hwndChild );
470
471     /* Get visible rectangle and create a region with it. */
472
473     if (wndPtr && DCE_GetVisRect(wndPtr, !(flags & DCX_WINDOW), &rect))
474     {
475         if((hrgnVis = CreateRectRgnIndirect( &rect )))
476         {
477             HRGN hrgnClip = CreateRectRgn( 0, 0, 0, 0 );
478             INT xoffset, yoffset;
479
480             if( hrgnClip )
481             {
482                 /* Compute obscured region for the visible rectangle by 
483                  * clipping children, siblings, and ancestors. Note that
484                  * DCE_GetVisRect() returns a rectangle either in client
485                  * or in window coordinates (for DCX_WINDOW request). */
486
487                 if( (flags & DCX_CLIPCHILDREN) && wndPtr->child )
488                 {
489                     if( flags & DCX_WINDOW )
490                     {
491                         /* adjust offsets since child window rectangles are 
492                          * in client coordinates */
493
494                         xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
495                         yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
496                     }
497                     else 
498                         xoffset = yoffset = 0;
499
500                     DCE_AddClipRects( wndPtr->child, NULL, hrgnClip, 
501                                       &rect, xoffset, yoffset );
502                 }
503
504                 /* We may need to clip children of child window, if a window with PARENTDC
505                  * class style and CLIPCHILDREN window style (like in Free Agent 16
506                  * preference dialogs) gets here, we take the region for the parent window
507                  * but apparently still need to clip the children of the child window... */
508
509                 if( (cflags & DCX_CLIPCHILDREN) && childWnd && childWnd->child )
510                 {
511                     if( flags & DCX_WINDOW )
512                     {
513                         /* adjust offsets since child window rectangles are 
514                          * in client coordinates */
515
516                         xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
517                         yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
518                     }
519                     else 
520                         xoffset = yoffset = 0;
521
522                     /* client coordinates of child window */
523                     xoffset += childWnd->rectClient.left;
524                     yoffset += childWnd->rectClient.top;
525
526                     DCE_AddClipRects( childWnd->child, NULL, hrgnClip, 
527                                       &rect, xoffset, yoffset );
528                 }
529
530                 /* sibling window rectangles are in client 
531                  * coordinates of the parent window */
532
533                 if (flags & DCX_WINDOW)
534                 {
535                     xoffset = -wndPtr->rectWindow.left;
536                     yoffset = -wndPtr->rectWindow.top;
537                 }
538                 else
539                 {
540                     xoffset = -wndPtr->rectClient.left;
541                     yoffset = -wndPtr->rectClient.top;
542                 }
543
544                 if (flags & DCX_CLIPSIBLINGS && wndPtr->parent )
545                     DCE_AddClipRects( wndPtr->parent->child,
546                                       wndPtr, hrgnClip, &rect, xoffset, yoffset );
547
548                 /* Clip siblings of all ancestors that have the
549                  * WS_CLIPSIBLINGS style
550                  */
551
552                 while (wndPtr->dwStyle & WS_CHILD)
553                 {
554                     WIN_UpdateWndPtr(&wndPtr,wndPtr->parent);
555                     xoffset -= wndPtr->rectClient.left;
556                     yoffset -= wndPtr->rectClient.top;
557                     if(wndPtr->dwStyle & WS_CLIPSIBLINGS && wndPtr->parent)
558                     {
559                         DCE_AddClipRects( wndPtr->parent->child, wndPtr,
560                                           hrgnClip, &rect, xoffset, yoffset );
561                     }
562                 }
563
564                 /* Now once we've got a jumbo clip region we have
565                  * to substract it from the visible rectangle.
566                  */
567
568                 CombineRgn( hrgnVis, hrgnVis, hrgnClip, RGN_DIFF );
569                 DeleteObject( hrgnClip );
570             }
571             else
572             {
573                 DeleteObject( hrgnVis );
574                 hrgnVis = 0;
575             }
576         }
577     }
578     else
579         hrgnVis = CreateRectRgn(0, 0, 0, 0); /* empty */
580     WIN_ReleaseWndPtr(wndPtr);
581     WIN_ReleaseWndPtr(childWnd);
582     return hrgnVis;
583 }
584
585 /***********************************************************************
586  *           DCE_OffsetVisRgn
587  *
588  * Change region from DC-origin relative coordinates to screen coords.
589  */
590
591 static void DCE_OffsetVisRgn( HDC hDC, HRGN hVisRgn )
592 {
593     DC *dc;
594     if (!(dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC ))) return;
595
596     OffsetRgn( hVisRgn, dc->w.DCOrgX, dc->w.DCOrgY );
597
598     GDI_HEAP_UNLOCK( hDC );
599 }
600
601 /***********************************************************************
602  *           DCE_ExcludeRgn
603  * 
604  *  Translate given region from the wnd client to the DC coordinates
605  *  and add it to the clipping region.
606  */
607 INT16 DCE_ExcludeRgn( HDC hDC, WND* wnd, HRGN hRgn )
608 {
609   POINT  pt = {0, 0};
610   DCE     *dce = firstDCE;
611
612   while (dce && (dce->hDC != hDC)) dce = dce->next;
613   if( dce )
614   {
615       MapWindowPoints( wnd->hwndSelf, dce->hwndCurrent, &pt, 1);
616       if( dce->DCXflags & DCX_WINDOW )
617       { 
618           wnd = WIN_FindWndPtr(dce->hwndCurrent);
619           pt.x += wnd->rectClient.left - wnd->rectWindow.left;
620           pt.y += wnd->rectClient.top - wnd->rectWindow.top;
621           WIN_ReleaseWndPtr(wnd);
622       }
623   }
624   else return ERROR;
625   OffsetRgn(hRgn, pt.x, pt.y);
626
627   return ExtSelectClipRgn( hDC, hRgn, RGN_DIFF );
628 }
629
630 /***********************************************************************
631  *           GetDCEx16    (USER.359)
632  */
633 HDC16 WINAPI GetDCEx16( HWND16 hwnd, HRGN16 hrgnClip, DWORD flags )
634 {
635     return (HDC16)GetDCEx( hwnd, hrgnClip, flags );
636 }
637
638
639 /***********************************************************************
640  *           GetDCEx32    (USER32.231)
641  *
642  * Unimplemented flags: DCX_LOCKWINDOWUPDATE
643  *
644  * FIXME: Full support for hrgnClip == 1 (alias for entire window).
645  */
646 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
647 {
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;
656
657     TRACE(dc,"hwnd %04x, hrgnClip %04x, flags %08x\n", 
658                                 hwnd, hrgnClip, (unsigned)flags);
659     
660     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
661
662     /* fixup flags */
663
664     if (!(wndPtr->class->style & (CS_OWNDC | CS_CLASSDC))) flags |= DCX_CACHE;
665
666     if (flags & DCX_USESTYLE)
667     {
668         flags &= ~( DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
669
670         if( wndPtr->dwStyle & WS_CLIPSIBLINGS )
671             flags |= DCX_CLIPSIBLINGS;
672
673         if ( !(flags & DCX_WINDOW) )
674         {
675             if (wndPtr->class->style & CS_PARENTDC) flags |= DCX_PARENTCLIP;
676
677             if (wndPtr->dwStyle & WS_CLIPCHILDREN &&
678                      !(wndPtr->dwStyle & WS_MINIMIZE) ) flags |= DCX_CLIPCHILDREN;
679         }
680         else flags |= DCX_CACHE;
681     }
682
683     if( flags & DCX_NOCLIPCHILDREN )
684     {
685         flags |= DCX_CACHE;
686         flags &= ~(DCX_PARENTCLIP | DCX_CLIPCHILDREN);
687     }
688
689     if (flags & DCX_WINDOW) 
690         flags = (flags & ~DCX_CLIPCHILDREN) | DCX_CACHE;
691
692     if (!(wndPtr->dwStyle & WS_CHILD) || !wndPtr->parent ) 
693         flags &= ~DCX_PARENTCLIP;
694     else if( flags & DCX_PARENTCLIP )
695     {
696         flags |= DCX_CACHE;
697         if( !(flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) )
698             if( (wndPtr->dwStyle & WS_VISIBLE) && (wndPtr->parent->dwStyle & WS_VISIBLE) )
699             {
700                 flags &= ~DCX_CLIPCHILDREN;
701                 if( wndPtr->parent->dwStyle & WS_CLIPSIBLINGS )
702                     flags |= DCX_CLIPSIBLINGS;
703             }
704     }
705
706     /* find a suitable DCE */
707
708     dcxFlags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | 
709                         DCX_CACHE | DCX_WINDOW);
710
711     if (flags & DCX_CACHE)
712     {
713         DCE*    dceEmpty;
714         DCE*    dceUnused;
715
716         dceEmpty = dceUnused = NULL;
717
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.
721          */
722
723         for (dce = firstDCE; (dce); dce = dce->next)
724         {
725             if ((dce->DCXflags & (DCX_CACHE | DCX_DCEBUSY)) == DCX_CACHE )
726             {
727                 dceUnused = dce;
728
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))
735                 {
736                     TRACE(dc,"\tfound valid %08x dce [%04x], flags %08x\n", 
737                                         (unsigned)dce, hwnd, (unsigned)dcxFlags );
738                     bUpdateVisRgn = FALSE; 
739                     bUpdateClipOrigin = TRUE;
740                     break;
741                 }
742             }
743         }
744
745         if (!dce) dce = (dceEmpty) ? dceEmpty : dceUnused;
746         
747         /* if there's no dce empty or unused, allocate a new one */
748         if (!dce)
749         {
750             dce = DCE_AllocDCE( 0, DCE_CACHE_DC );
751         }
752     }
753     else 
754     {
755         dce = (wndPtr->class->style & CS_OWNDC) ? wndPtr->dce : wndPtr->class->dce;
756         if( dce->hwndCurrent == hwnd )
757         {
758             TRACE(dc,"\tskipping hVisRgn update\n");
759             bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
760
761             if( (dce->DCXflags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN)) &&
762                 (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN)) )
763             {
764                 /* This is likely to be a nested BeginPaint(). */
765
766                 if( dce->hClipRgn != hrgnClip )
767                 {
768                     FIXME(dc,"new hrgnClip[%04x] smashes the previous[%04x]\n",
769                           hrgnClip, dce->hClipRgn );
770                     DCE_DeleteClipRgn( dce );
771                 }
772                 else 
773                     RestoreVisRgn16(dce->hDC);
774             }
775         }
776     }
777     if (!dce)
778     {
779         hdc = 0;
780         goto END;
781     }
782
783     dce->hwndCurrent = hwnd;
784     dce->hClipRgn = 0;
785     dce->DCXflags = dcxFlags | (flags & DCX_WINDOWPAINT) | DCX_DCEBUSY;
786     hdc = dce->hDC;
787     
788     if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC )))
789     {
790         hdc = 0;
791         goto END;
792     }
793     bUpdateVisRgn = bUpdateVisRgn || (dc->w.flags & DC_DIRTY);
794
795     /* recompute visible region */
796
797     wndPtr->pDriver->pSetDrawable( wndPtr, dc, flags, bUpdateClipOrigin );
798     if( bUpdateVisRgn )
799     {
800         TRACE(dc,"updating visrgn for %08x dce, hwnd [%04x]\n", (unsigned)dce, hwnd);
801
802         if (flags & DCX_PARENTCLIP)
803         {
804             WND *parentPtr = WIN_LockWndPtr(wndPtr->parent);
805
806             if( wndPtr->dwStyle & WS_VISIBLE && !(parentPtr->dwStyle & WS_MINIMIZE) )
807             {
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);
812
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 );
822             }
823             else
824                 hrgnVisible = CreateRectRgn( 0, 0, 0, 0 );
825             WIN_ReleaseWndPtr(parentPtr);
826         }
827         else
828             if ((hwnd == GetDesktopWindow()) && !DESKTOP_IsSingleWindow())
829                  hrgnVisible = CreateRectRgn( 0, 0, SYSMETRICS_CXSCREEN, SYSMETRICS_CYSCREEN );
830             else 
831             {
832                 hrgnVisible = DCE_GetVisRgn( hwnd, flags, 0, 0 );
833                 DCE_OffsetVisRgn( hdc, hrgnVisible );
834             }
835
836         dc->w.flags &= ~DC_DIRTY;
837         dce->DCXflags &= ~DCX_DCEDIRTY;
838         SelectVisRgn16( hdc, hrgnVisible );
839     }
840     else
841         TRACE(dc,"no visrgn update %08x dce, hwnd [%04x]\n", (unsigned)dce, hwnd);
842
843     /* apply additional region operation (if any) */
844
845     if( flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN) )
846     {
847         if( !hrgnVisible ) hrgnVisible = CreateRectRgn( 0, 0, 0, 0 );
848
849         dce->DCXflags |= flags & (DCX_KEEPCLIPRGN | DCX_INTERSECTRGN | DCX_EXCLUDERGN);
850         dce->hClipRgn = hrgnClip;
851
852         TRACE(dc, "\tsaved VisRgn, clipRgn = %04x\n", hrgnClip);
853
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 );
860     }
861
862     if( hrgnVisible ) DeleteObject( hrgnVisible );
863
864     TRACE(dc, "(%04x,%04x,0x%lx): returning %04x\n", 
865                hwnd, hrgnClip, flags, hdc);
866 END:
867     WIN_ReleaseWndPtr(wndPtr);
868     return hdc;
869 }
870
871
872 /***********************************************************************
873  *           GetDC16    (USER.66)
874  */
875 HDC16 WINAPI GetDC16( HWND16 hwnd )
876 {
877     return (HDC16)GetDC( hwnd );
878 }
879
880
881 /***********************************************************************
882  *           GetDC32    (USER32.230)
883  * RETURNS
884  *      :Handle to DC
885  *      NULL: Failure
886  */
887 HDC WINAPI GetDC(
888              HWND hwnd /* handle of window */
889 ) {
890     if (!hwnd)
891         return GetDCEx( GetDesktopWindow(), 0, DCX_CACHE | DCX_WINDOW );
892     return GetDCEx( hwnd, 0, DCX_USESTYLE );
893 }
894
895
896 /***********************************************************************
897  *           GetWindowDC16    (USER.67)
898  */
899 HDC16 WINAPI GetWindowDC16( HWND16 hwnd )
900 {
901     if (!hwnd) hwnd = GetDesktopWindow16();
902     return GetDCEx16( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
903 }
904
905
906 /***********************************************************************
907  *           GetWindowDC32    (USER32.304)
908  */
909 HDC WINAPI GetWindowDC( HWND hwnd )
910 {
911     if (!hwnd) hwnd = GetDesktopWindow();
912     return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
913 }
914
915
916 /***********************************************************************
917  *           ReleaseDC16    (USER.68)
918  */
919 INT16 WINAPI ReleaseDC16( HWND16 hwnd, HDC16 hdc )
920 {
921     return (INT16)ReleaseDC( hwnd, hdc );
922 }
923
924
925 /***********************************************************************
926  *           ReleaseDC32    (USER32.440)
927  *
928  * RETURNS
929  *      1: Success
930  *      0: Failure
931  */
932 INT WINAPI ReleaseDC( 
933              HWND hwnd /* Handle of window - ignored */, 
934              HDC hdc   /* Handle of device context */
935 ) {
936     DCE * dce;
937     INT nRet = 0;
938
939     WIN_LockWnds();
940     dce = firstDCE;
941     
942     TRACE(dc, "%04x %04x\n", hwnd, hdc );
943         
944     while (dce && (dce->hDC != hdc)) dce = dce->next;
945
946     if ( dce ) 
947         if ( dce->DCXflags & DCX_DCEBUSY )
948             nRet = DCE_ReleaseDC( dce );
949
950     WIN_UnlockWnds();
951
952     return nRet;
953 }
954
955 /***********************************************************************
956  *           DCHook    (USER.362)
957  *
958  * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..  
959  */
960 BOOL16 WINAPI DCHook16( HDC16 hDC, WORD code, DWORD data, LPARAM lParam )
961 {
962     HRGN hVisRgn;
963     DCE *dce;
964     DC  *dc;
965     WND *wndPtr;
966
967     /* Grab the windows lock before doing anything else  */
968     WIN_LockWnds();
969
970     dce = firstDCE;
971
972     TRACE(dc,"hDC = %04x, %i\n", hDC, code);
973
974     while (dce && (dce->hDC != hDC)) dce = dce->next;
975
976     if (!dce) goto END;
977
978     switch( code )
979     {
980       case DCHC_INVALIDVISRGN:
981
982            /* GDI code calls this when it detects that the
983             * DC is dirty (usually after SetHookFlags()). This
984             * means that we have to recompute the visible region.
985             */
986
987            if( dce->DCXflags & DCX_DCEBUSY )
988            {
989
990                /* Update stale DC in DCX */
991                wndPtr = WIN_FindWndPtr( dce->hwndCurrent);
992                dc = (DC *) GDI_GetObjPtr( dce->hDC, DC_MAGIC);
993                if( dc && wndPtr)
994                  wndPtr->pDriver->pSetDrawable( wndPtr, dc,dce->DCXflags,TRUE);
995
996                SetHookFlags16(hDC, DCHF_VALIDATEVISRGN);
997                hVisRgn = DCE_GetVisRgn(dce->hwndCurrent, dce->DCXflags, 0, 0);
998
999                TRACE(dc,"\tapplying saved clipRgn\n");
1000   
1001                /* clip this region with saved clipping region */
1002
1003                if ( (dce->DCXflags & DCX_INTERSECTRGN && dce->hClipRgn != 1) ||
1004                   (  dce->DCXflags & DCX_EXCLUDERGN && dce->hClipRgn) )
1005                {
1006
1007                     if( (!dce->hClipRgn && dce->DCXflags & DCX_INTERSECTRGN) ||
1008                          (dce->hClipRgn == 1 && dce->DCXflags & DCX_EXCLUDERGN) )            
1009                          SetRectRgn(hVisRgn,0,0,0,0);
1010                     else
1011                          CombineRgn(hVisRgn, hVisRgn, dce->hClipRgn, 
1012                                       (dce->DCXflags & DCX_EXCLUDERGN)? RGN_DIFF:RGN_AND);
1013                }
1014                dce->DCXflags &= ~DCX_DCEDIRTY;
1015                DCE_OffsetVisRgn( hDC, hVisRgn );
1016                SelectVisRgn16(hDC, hVisRgn);
1017                DeleteObject( hVisRgn );
1018            }
1019            else /* non-fatal but shouldn't happen */
1020              WARN(dc, "DC is not in use!\n");
1021            break;
1022
1023       case DCHC_DELETEDC: /* FIXME: ?? */
1024            break;
1025
1026       default:
1027            FIXME(dc,"unknown code\n");
1028     }
1029
1030 END:
1031   WIN_UnlockWnds();  /* Release the wnd lock */
1032   return 0;
1033 }
1034
1035
1036 /**********************************************************************
1037  *          WindowFromDC16   (USER.117)
1038  */
1039 HWND16 WINAPI WindowFromDC16( HDC16 hDC )
1040 {
1041     return (HWND16)WindowFromDC( hDC );
1042 }
1043
1044
1045 /**********************************************************************
1046  *          WindowFromDC32   (USER32.581)
1047  */
1048 HWND WINAPI WindowFromDC( HDC hDC )
1049 {
1050     DCE *dce;
1051     HWND hwnd;
1052
1053     WIN_LockWnds();
1054     dce = firstDCE;
1055     
1056     while (dce && (dce->hDC != hDC)) dce = dce->next;
1057
1058     hwnd = dce ? dce->hwndCurrent : 0;
1059     WIN_UnlockWnds();
1060     
1061     return hwnd;
1062 }
1063
1064
1065 /***********************************************************************
1066  *           LockWindowUpdate16   (USER.294)
1067  */
1068 BOOL16 WINAPI LockWindowUpdate16( HWND16 hwnd )
1069 {
1070     return LockWindowUpdate( hwnd );
1071 }
1072
1073
1074 /***********************************************************************
1075  *           LockWindowUpdate32   (USER32.378)
1076  */
1077 BOOL WINAPI LockWindowUpdate( HWND hwnd )
1078 {
1079     /* FIXME? DCX_LOCKWINDOWUPDATE is unimplemented */
1080     return TRUE;
1081 }