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