Fixed silly EINTR bug with starting dosmod.
[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 <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 "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->flags & WIN_NATIVE) ) 
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 /***********************************************************************
632  *           GetDCEx16    (USER.359)
633  */
634 HDC16 WINAPI GetDCEx16( HWND16 hwnd, HRGN16 hrgnClip, DWORD flags )
635 {
636     return (HDC16)GetDCEx( hwnd, hrgnClip, flags );
637 }
638
639
640 /***********************************************************************
641  *           GetDCEx32    (USER32.231)
642  *
643  * Unimplemented flags: DCX_LOCKWINDOWUPDATE
644  *
645  * FIXME: Full support for hrgnClip == 1 (alias for entire window).
646  */
647 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
648 {
649     HRGN        hrgnVisible = 0;
650     HDC         hdc = 0;
651     DCE *       dce;
652     DC *        dc;
653     WND *       wndPtr;
654     DWORD       dcxFlags = 0;
655     BOOL        bUpdateVisRgn = TRUE;
656     BOOL        bUpdateClipOrigin = FALSE;
657
658     TRACE(dc,"hwnd %04x, hrgnClip %04x, flags %08x\n", 
659                                 hwnd, hrgnClip, (unsigned)flags);
660     
661     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
662
663     /* fixup flags */
664
665     if (!(wndPtr->class->style & (CS_OWNDC | CS_CLASSDC))) flags |= DCX_CACHE;
666
667     if (flags & DCX_USESTYLE)
668     {
669         flags &= ~( DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
670
671         if( wndPtr->dwStyle & WS_CLIPSIBLINGS )
672             flags |= DCX_CLIPSIBLINGS;
673
674         if ( !(flags & DCX_WINDOW) )
675         {
676             if (wndPtr->class->style & CS_PARENTDC) flags |= DCX_PARENTCLIP;
677
678             if (wndPtr->dwStyle & WS_CLIPCHILDREN &&
679                      !(wndPtr->dwStyle & WS_MINIMIZE) ) flags |= DCX_CLIPCHILDREN;
680         }
681         else flags |= DCX_CACHE;
682     }
683
684     if( flags & DCX_NOCLIPCHILDREN )
685     {
686         flags |= DCX_CACHE;
687         flags &= ~(DCX_PARENTCLIP | DCX_CLIPCHILDREN);
688     }
689
690     if (flags & DCX_WINDOW) 
691         flags = (flags & ~DCX_CLIPCHILDREN) | DCX_CACHE;
692
693     if (!(wndPtr->dwStyle & WS_CHILD) || !wndPtr->parent ) 
694         flags &= ~DCX_PARENTCLIP;
695     else if( flags & DCX_PARENTCLIP )
696     {
697         flags |= DCX_CACHE;
698         if( !(flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) )
699             if( (wndPtr->dwStyle & WS_VISIBLE) && (wndPtr->parent->dwStyle & WS_VISIBLE) )
700             {
701                 flags &= ~DCX_CLIPCHILDREN;
702                 if( wndPtr->parent->dwStyle & WS_CLIPSIBLINGS )
703                     flags |= DCX_CLIPSIBLINGS;
704             }
705     }
706
707     /* find a suitable DCE */
708
709     dcxFlags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | 
710                         DCX_CACHE | DCX_WINDOW);
711
712     if (flags & DCX_CACHE)
713     {
714         DCE*    dceEmpty;
715         DCE*    dceUnused;
716
717         dceEmpty = dceUnused = NULL;
718
719         /* Strategy: First, we attempt to find a non-empty but unused DCE with
720          * compatible flags. Next, we look for an empty entry. If the cache is
721          * full we have to purge one of the unused entries.
722          */
723
724         for (dce = firstDCE; (dce); dce = dce->next)
725         {
726             if ((dce->DCXflags & (DCX_CACHE | DCX_DCEBUSY)) == DCX_CACHE )
727             {
728                 dceUnused = dce;
729
730                 if (dce->DCXflags & DCX_DCEEMPTY)
731                     dceEmpty = dce;
732                 else
733                 if ((dce->hwndCurrent == hwnd) &&
734                    ((dce->DCXflags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
735                                       DCX_CACHE | DCX_WINDOW | DCX_PARENTCLIP)) == dcxFlags))
736                 {
737                     TRACE(dc,"\tfound valid %08x dce [%04x], flags %08x\n", 
738                                         (unsigned)dce, hwnd, (unsigned)dcxFlags );
739                     bUpdateVisRgn = FALSE; 
740                     bUpdateClipOrigin = TRUE;
741                     break;
742                 }
743             }
744         }
745
746         if (!dce) dce = (dceEmpty) ? dceEmpty : dceUnused;
747         
748         /* if there's no dce empty or unused, allocate a new one */
749         if (!dce)
750         {
751             dce = DCE_AllocDCE( 0, DCE_CACHE_DC );
752         }
753     }
754     else 
755     {
756         dce = (wndPtr->class->style & CS_OWNDC) ? wndPtr->dce : wndPtr->class->dce;
757         if( dce->hwndCurrent == hwnd )
758         {
759             TRACE(dc,"\tskipping hVisRgn update\n");
760             bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
761
762             if( (dce->DCXflags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN)) &&
763                 (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN)) )
764             {
765                 /* This is likely to be a nested BeginPaint(). */
766
767                 if( dce->hClipRgn != hrgnClip )
768                 {
769                     FIXME(dc,"new hrgnClip[%04x] smashes the previous[%04x]\n",
770                           hrgnClip, dce->hClipRgn );
771                     DCE_DeleteClipRgn( dce );
772                 }
773                 else 
774                     RestoreVisRgn16(dce->hDC);
775             }
776         }
777     }
778     if (!dce)
779     {
780         hdc = 0;
781         goto END;
782     }
783
784     dce->hwndCurrent = hwnd;
785     dce->hClipRgn = 0;
786     dce->DCXflags = dcxFlags | (flags & DCX_WINDOWPAINT) | DCX_DCEBUSY;
787     hdc = dce->hDC;
788     
789     if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC )))
790     {
791         hdc = 0;
792         goto END;
793     }
794     bUpdateVisRgn = bUpdateVisRgn || (dc->w.flags & DC_DIRTY);
795
796     /* recompute visible region */
797
798     wndPtr->pDriver->pSetDrawable( wndPtr, dc, flags, bUpdateClipOrigin );
799     if( bUpdateVisRgn )
800     {
801         TRACE(dc,"updating visrgn for %08x dce, hwnd [%04x]\n", (unsigned)dce, hwnd);
802
803         if (flags & DCX_PARENTCLIP)
804         {
805             WND *parentPtr = WIN_LockWndPtr(wndPtr->parent);
806
807             if( wndPtr->dwStyle & WS_VISIBLE && !(parentPtr->dwStyle & WS_MINIMIZE) )
808             {
809                 if( parentPtr->dwStyle & WS_CLIPSIBLINGS ) 
810                     dcxFlags = DCX_CLIPSIBLINGS | (flags & ~(DCX_CLIPCHILDREN | DCX_WINDOW));
811                 else
812                     dcxFlags = flags & ~(DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW);
813
814                 hrgnVisible = DCE_GetVisRgn( parentPtr->hwndSelf, dcxFlags,
815                                              wndPtr->hwndSelf, flags );
816                 if( flags & DCX_WINDOW )
817                     OffsetRgn( hrgnVisible, -wndPtr->rectWindow.left,
818                                               -wndPtr->rectWindow.top );
819                 else
820                     OffsetRgn( hrgnVisible, -wndPtr->rectClient.left,
821                                               -wndPtr->rectClient.top );
822                 DCE_OffsetVisRgn( hdc, hrgnVisible );
823             }
824             else
825                 hrgnVisible = CreateRectRgn( 0, 0, 0, 0 );
826             WIN_ReleaseWndPtr(parentPtr);
827         }
828         else
829             if ((hwnd == GetDesktopWindow()) && !DESKTOP_IsSingleWindow())
830                  hrgnVisible = CreateRectRgn( 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN) );
831             else 
832             {
833                 hrgnVisible = DCE_GetVisRgn( hwnd, flags, 0, 0 );
834                 DCE_OffsetVisRgn( hdc, hrgnVisible );
835             }
836
837         dc->w.flags &= ~DC_DIRTY;
838         dce->DCXflags &= ~DCX_DCEDIRTY;
839         SelectVisRgn16( hdc, hrgnVisible );
840     }
841     else
842         TRACE(dc,"no visrgn update %08x dce, hwnd [%04x]\n", (unsigned)dce, hwnd);
843
844     /* apply additional region operation (if any) */
845
846     if( flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN) )
847     {
848         if( !hrgnVisible ) hrgnVisible = CreateRectRgn( 0, 0, 0, 0 );
849
850         dce->DCXflags |= flags & (DCX_KEEPCLIPRGN | DCX_INTERSECTRGN | DCX_EXCLUDERGN);
851         dce->hClipRgn = hrgnClip;
852
853         TRACE(dc, "\tsaved VisRgn, clipRgn = %04x\n", hrgnClip);
854
855         SaveVisRgn16( hdc );
856         CombineRgn( hrgnVisible, hrgnClip, 0, RGN_COPY );
857         DCE_OffsetVisRgn( hdc, hrgnVisible );
858         CombineRgn( hrgnVisible, InquireVisRgn16( hdc ), hrgnVisible,
859                       (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
860         SelectVisRgn16( hdc, hrgnVisible );
861     }
862
863     if( hrgnVisible ) DeleteObject( hrgnVisible );
864
865     TRACE(dc, "(%04x,%04x,0x%lx): returning %04x\n", 
866                hwnd, hrgnClip, flags, hdc);
867 END:
868     WIN_ReleaseWndPtr(wndPtr);
869     return hdc;
870 }
871
872
873 /***********************************************************************
874  *           GetDC16    (USER.66)
875  */
876 HDC16 WINAPI GetDC16( HWND16 hwnd )
877 {
878     return (HDC16)GetDC( hwnd );
879 }
880
881
882 /***********************************************************************
883  *           GetDC32    (USER32.230)
884  * RETURNS
885  *      :Handle to DC
886  *      NULL: Failure
887  */
888 HDC WINAPI GetDC(
889              HWND hwnd /* handle of window */
890 ) {
891     if (!hwnd)
892         return GetDCEx( GetDesktopWindow(), 0, DCX_CACHE | DCX_WINDOW );
893     return GetDCEx( hwnd, 0, DCX_USESTYLE );
894 }
895
896
897 /***********************************************************************
898  *           GetWindowDC16    (USER.67)
899  */
900 HDC16 WINAPI GetWindowDC16( HWND16 hwnd )
901 {
902     if (!hwnd) hwnd = GetDesktopWindow16();
903     return GetDCEx16( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
904 }
905
906
907 /***********************************************************************
908  *           GetWindowDC32    (USER32.304)
909  */
910 HDC WINAPI GetWindowDC( HWND hwnd )
911 {
912     if (!hwnd) hwnd = GetDesktopWindow();
913     return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
914 }
915
916
917 /***********************************************************************
918  *           ReleaseDC16    (USER.68)
919  */
920 INT16 WINAPI ReleaseDC16( HWND16 hwnd, HDC16 hdc )
921 {
922     return (INT16)ReleaseDC( hwnd, hdc );
923 }
924
925
926 /***********************************************************************
927  *           ReleaseDC32    (USER32.440)
928  *
929  * RETURNS
930  *      1: Success
931  *      0: Failure
932  */
933 INT WINAPI ReleaseDC( 
934              HWND hwnd /* Handle of window - ignored */, 
935              HDC hdc   /* Handle of device context */
936 ) {
937     DCE * dce;
938     INT nRet = 0;
939
940     WIN_LockWnds();
941     dce = firstDCE;
942     
943     TRACE(dc, "%04x %04x\n", hwnd, hdc );
944         
945     while (dce && (dce->hDC != hdc)) dce = dce->next;
946
947     if ( dce ) 
948         if ( dce->DCXflags & DCX_DCEBUSY )
949             nRet = DCE_ReleaseDC( dce );
950
951     WIN_UnlockWnds();
952
953     return nRet;
954 }
955
956 /***********************************************************************
957  *           DCHook    (USER.362)
958  *
959  * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..  
960  */
961 BOOL16 WINAPI DCHook16( HDC16 hDC, WORD code, DWORD data, LPARAM lParam )
962 {
963     BOOL retv = TRUE;
964     HRGN hVisRgn;
965     DCE *dce = (DCE *)data;
966     DC  *dc;
967     WND *wndPtr;
968
969     TRACE(dc,"hDC = %04x, %i\n", hDC, code);
970
971     if (!dce) return 0;
972     assert(dce->hDC == hDC);
973
974     /* Grab the windows lock before doing anything else  */
975     WIN_LockWnds();
976
977     switch( code )
978     {
979       case DCHC_INVALIDVISRGN:
980
981            /* GDI code calls this when it detects that the
982             * DC is dirty (usually after SetHookFlags()). This
983             * means that we have to recompute the visible region.
984             */
985
986            if( dce->DCXflags & DCX_DCEBUSY )
987            {
988
989                /* Update stale DC in DCX */
990                wndPtr = WIN_FindWndPtr( dce->hwndCurrent);
991                dc = (DC *) GDI_GetObjPtr( dce->hDC, DC_MAGIC);
992                if( dc && wndPtr)
993                  wndPtr->pDriver->pSetDrawable( wndPtr, dc,dce->DCXflags,TRUE);
994
995                SetHookFlags16(hDC, DCHF_VALIDATEVISRGN);
996                hVisRgn = DCE_GetVisRgn(dce->hwndCurrent, dce->DCXflags, 0, 0);
997
998                TRACE(dc,"\tapplying saved clipRgn\n");
999   
1000                /* clip this region with saved clipping region */
1001
1002                if ( (dce->DCXflags & DCX_INTERSECTRGN && dce->hClipRgn != 1) ||
1003                   (  dce->DCXflags & DCX_EXCLUDERGN && dce->hClipRgn) )
1004                {
1005
1006                     if( (!dce->hClipRgn && dce->DCXflags & DCX_INTERSECTRGN) ||
1007                          (dce->hClipRgn == 1 && dce->DCXflags & DCX_EXCLUDERGN) )            
1008                          SetRectRgn(hVisRgn,0,0,0,0);
1009                     else
1010                          CombineRgn(hVisRgn, hVisRgn, dce->hClipRgn, 
1011                                       (dce->DCXflags & DCX_EXCLUDERGN)? RGN_DIFF:RGN_AND);
1012                }
1013                dce->DCXflags &= ~DCX_DCEDIRTY;
1014                DCE_OffsetVisRgn( hDC, hVisRgn );
1015                SelectVisRgn16(hDC, hVisRgn);
1016                DeleteObject( hVisRgn );
1017               WIN_ReleaseWndPtr( wndPtr );  /* Release WIN_FindWndPtr lock */
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:
1024            /*
1025             * Windows will not let you delete a DC that is busy
1026             * (between GetDC and ReleaseDC)
1027             */
1028
1029            if ( dce->DCXflags & DCX_DCEBUSY )
1030            {
1031                WARN(dc, "Application trying to delete a busy DC\n");
1032                retv = FALSE;
1033            }
1034            break;
1035
1036       default:
1037            FIXME(dc,"unknown code\n");
1038     }
1039
1040   WIN_UnlockWnds();  /* Release the wnd lock */
1041   return retv;
1042 }
1043
1044
1045 /**********************************************************************
1046  *          WindowFromDC16   (USER.117)
1047  */
1048 HWND16 WINAPI WindowFromDC16( HDC16 hDC )
1049 {
1050     return (HWND16)WindowFromDC( hDC );
1051 }
1052
1053
1054 /**********************************************************************
1055  *          WindowFromDC32   (USER32.581)
1056  */
1057 HWND WINAPI WindowFromDC( HDC hDC )
1058 {
1059     DCE *dce;
1060     HWND hwnd;
1061
1062     WIN_LockWnds();
1063     dce = firstDCE;
1064     
1065     while (dce && (dce->hDC != hDC)) dce = dce->next;
1066
1067     hwnd = dce ? dce->hwndCurrent : 0;
1068     WIN_UnlockWnds();
1069     
1070     return hwnd;
1071 }
1072
1073
1074 /***********************************************************************
1075  *           LockWindowUpdate16   (USER.294)
1076  */
1077 BOOL16 WINAPI LockWindowUpdate16( HWND16 hwnd )
1078 {
1079     return LockWindowUpdate( hwnd );
1080 }
1081
1082
1083 /***********************************************************************
1084  *           LockWindowUpdate32   (USER32.378)
1085  */
1086 BOOL WINAPI LockWindowUpdate( HWND hwnd )
1087 {
1088     /* FIXME? DCX_LOCKWINDOWUPDATE is unimplemented */
1089     return TRUE;
1090 }