Release 960805
[wine] / windows / dce.c
1 /*
2  * USER DCE functions
3  *
4  * Copyright 1993 Alexandre Julliard
5  *           1996 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_DCEBUSY     - dce structure is in use
14  * DCX_KEEPCLIPRGN - do not delete clipping region in ReleaseDC
15  * DCX_WINDOWPAINT - BeginPaint specific flag
16  */
17
18 #include "dce.h"
19 #include "class.h"
20 #include "win.h"
21 #include "gdi.h"
22 #include "user.h"
23 #include "sysmetrics.h"
24 #include "stddebug.h"
25 /* #define DEBUG_DC */
26 #include "debug.h"
27
28 #define NB_DCE    5  /* Number of DCEs created at startup */
29
30 static HANDLE firstDCE = 0;
31 static HDC defaultDCstate = 0;
32
33 BOOL   DCHook(HDC, WORD, DWORD, DWORD);
34
35 /***********************************************************************
36  *           DCE_AllocDCE
37 *
38  * Allocate a new DCE.
39  */
40 HANDLE DCE_AllocDCE( HWND hWnd, DCE_TYPE type )
41 {
42     DCE * dce;
43     HANDLE handle = USER_HEAP_ALLOC( sizeof(DCE) );
44     if (!handle) return 0;
45     dce = (DCE *) USER_HEAP_LIN_ADDR( handle );
46     if (!(dce->hDC = CreateDC( "DISPLAY", NULL, NULL, NULL )))
47     {
48         USER_HEAP_FREE( handle );
49         return 0;
50     }
51
52     /* store DCE handle in DC hook data field */
53
54     SetDCHook(dce->hDC, GDI_GetDefDCHook(), MAKELONG(handle,DC_MAGIC));
55
56     dce->hwndCurrent = hWnd;
57     dce->hNext = firstDCE;
58     dce->hClipRgn = 0;
59     firstDCE = handle;
60
61     if( type != DCE_CACHE_DC )
62       {
63         dce->DCXflags = DCX_DCEBUSY;
64         if( hWnd )
65           {
66             WND* wnd = WIN_FindWndPtr(hWnd);
67         
68             if( wnd->dwStyle & WS_CLIPCHILDREN ) dce->DCXflags |= DCX_CLIPCHILDREN;
69             if( wnd->dwStyle & WS_CLIPSIBLINGS ) dce->DCXflags |= DCX_CLIPSIBLINGS;
70           }
71         SetHookFlags(dce->hDC,DCHF_INVALIDATEVISRGN);
72       }
73     else dce->DCXflags = DCX_CACHE;
74
75     return handle;
76 }
77
78
79 /***********************************************************************
80  *           DCE_FreeDCE
81  */
82 void DCE_FreeDCE( HANDLE hdce )
83 {
84     DCE * dce;
85     HANDLE *handle = &firstDCE;
86
87     if (!(dce = (DCE *) USER_HEAP_LIN_ADDR( hdce ))) return;
88     while (*handle && (*handle != hdce))
89     {
90         DCE * prev = (DCE *) USER_HEAP_LIN_ADDR( *handle );     
91         handle = &prev->hNext;
92     }
93     if (*handle == hdce) *handle = dce->hNext;
94
95     SetDCHook(dce->hDC,(SEGPTR)NULL,0L);
96
97     DeleteDC( dce->hDC );
98     if( dce->hClipRgn && !(dce->DCXflags & DCX_KEEPCLIPRGN) )
99         DeleteObject(dce->hClipRgn);
100     USER_HEAP_FREE( hdce );
101 }
102
103 /***********************************************************************
104  *           DCE_FindDCE
105  */
106 HANDLE DCE_FindDCE(HDC hDC)
107 {
108  HANDLE hdce = firstDCE;
109  DCE*   dce;
110
111  while( hdce )
112   {
113     dce = (DCE *) USER_HEAP_LIN_ADDR(hdce);
114     if( dce->hDC == hDC ) break;
115     hdce = dce->hNext;
116   }
117  return hdce;
118 }
119
120 /**********************************************************************
121  *          WindowFromDC   (USER.117) (USER32.580)
122  */
123 HWND16 WindowFromDC( HDC32 hDC )
124 {
125  HANDLE16 hdce = DCE_FindDCE(hDC); 
126
127  if( hdce )
128    {
129      DCE* dce = (DCE *) USER_HEAP_LIN_ADDR(hdce);
130      return dce->hwndCurrent;
131    }
132
133  return 0;
134 }
135
136 /***********************************************************************
137  *   DCE_InvalidateDCE
138  *
139  * It is called from SetWindowPos - we have to invalidate all busy
140  * DCE's for windows whose client rect intersects with update rectangle 
141  */
142 BOOL DCE_InvalidateDCE(WND* wndScope, RECT16* pRectUpdate)
143 {
144  HANDLE hdce;
145  DCE*   dce;
146
147  if( !wndScope ) return 0;
148
149  dprintf_dc(stddeb,"InvalidateDCE: scope hwnd = %04x, (%i,%i - %i,%i)\n",
150                     wndScope->hwndSelf, pRectUpdate->left,pRectUpdate->top,
151                                         pRectUpdate->right,pRectUpdate->bottom);
152  /* walk all DCE's */
153
154  for( hdce = firstDCE; (hdce); hdce=dce->hNext)
155   { 
156     dce = (DCE*)USER_HEAP_LIN_ADDR(hdce);
157
158     if( dce->DCXflags & DCX_DCEBUSY )
159       {
160         WND * wndCurrent, * wnd; 
161
162         wnd = wndCurrent = WIN_FindWndPtr(dce->hwndCurrent);
163
164         /* desktop is not critical (DC is not owned anyway) */
165
166         if( wnd == WIN_GetDesktop() ) continue; 
167
168         /* check if DCE window is within z-order scope */
169
170         for( ; wnd ; wnd = wnd->parent )
171             if( wnd == wndScope )
172               { 
173                 RECT16 wndRect = wndCurrent->rectWindow;
174
175                 dprintf_dc(stddeb,"\tgot hwnd %04x\n", wndCurrent->hwndSelf);
176   
177                 MapWindowPoints16(wndCurrent->parent->hwndSelf, wndScope->hwndSelf,
178                                                                (LPPOINT16)&wndRect, 2);
179                 if( IntersectRect16(&wndRect,&wndRect,pRectUpdate) )
180                     SetHookFlags(dce->hDC, DCHF_INVALIDATEVISRGN);
181                 break;
182               }
183       }
184   }
185  return 1;
186 }
187
188 /***********************************************************************
189  *           DCE_Init
190  */
191 void DCE_Init()
192 {
193     int i;
194     HANDLE handle;
195     DCE * dce;
196         
197     for (i = 0; i < NB_DCE; i++)
198     {
199         if (!(handle = DCE_AllocDCE( 0, DCE_CACHE_DC ))) return;
200         dce = (DCE *) USER_HEAP_LIN_ADDR( handle );     
201         if (!defaultDCstate) defaultDCstate = GetDCState( dce->hDC );
202     }
203 }
204
205
206 /***********************************************************************
207  *           DCE_GetVisRect
208  *
209  * Calc the visible rectangle of a window, i.e. the client or
210  * window area clipped by the client area of all ancestors.
211  * Return FALSE if the visible region is empty.
212  */
213 static BOOL DCE_GetVisRect( WND *wndPtr, BOOL clientArea, RECT16 *lprect )
214 {
215     int xoffset, yoffset;
216
217     *lprect = clientArea ? wndPtr->rectClient : wndPtr->rectWindow;
218     xoffset = lprect->left;
219     yoffset = lprect->top;
220
221     if (!(wndPtr->dwStyle & WS_VISIBLE) || (wndPtr->flags & WIN_NO_REDRAW))
222     {
223         SetRectEmpty16( lprect );  /* Clip everything */
224         return FALSE;
225     }
226
227     while (wndPtr->parent)
228     {
229         wndPtr = wndPtr->parent;
230         if (!(wndPtr->dwStyle & WS_VISIBLE) ||
231             (wndPtr->flags & WIN_NO_REDRAW) ||
232             (wndPtr->dwStyle & WS_ICONIC))
233         {
234             SetRectEmpty16( lprect );  /* Clip everything */
235             return FALSE;
236         }
237         xoffset += wndPtr->rectClient.left;
238         yoffset += wndPtr->rectClient.top;
239         OffsetRect16( lprect, wndPtr->rectClient.left,
240                       wndPtr->rectClient.top );
241
242           /* Warning!! we assume that IntersectRect() handles the case */
243           /* where the destination is the same as one of the sources.  */
244         if (!IntersectRect16( lprect, lprect, &wndPtr->rectClient ))
245             return FALSE;  /* Visible rectangle is empty */
246     }
247     OffsetRect16( lprect, -xoffset, -yoffset );
248     return TRUE;
249 }
250
251
252 /***********************************************************************
253  *           DCE_ClipWindows
254  *
255  * Go through the linked list of windows from hwndStart to hwndEnd,
256  * removing from the given region the rectangle of each window offset
257  * by a given amount.  The new region is returned, and the original one
258  * is destroyed.  Used to implement DCX_CLIPSIBLINGS and
259  * DCX_CLIPCHILDREN styles.
260  */
261 static HRGN DCE_ClipWindows( WND *pWndStart, WND *pWndEnd,
262                              HRGN hrgn, int xoffset, int yoffset )
263 {
264     HRGN hrgnNew;
265
266     if (!pWndStart) return hrgn;
267     if (!(hrgnNew = CreateRectRgn( 0, 0, 0, 0 )))
268     {
269         DeleteObject( hrgn );
270         return 0;
271     }
272     for (; pWndStart != pWndEnd; pWndStart = pWndStart->next)
273     {
274         if (!(pWndStart->dwStyle & WS_VISIBLE)) continue;
275         SetRectRgn( hrgnNew, pWndStart->rectWindow.left + xoffset,
276                     pWndStart->rectWindow.top + yoffset,
277                     pWndStart->rectWindow.right + xoffset,
278                     pWndStart->rectWindow.bottom + yoffset );
279         if (!CombineRgn( hrgn, hrgn, hrgnNew, RGN_DIFF )) break;
280     }
281     DeleteObject( hrgnNew );
282     if (pWndStart != pWndEnd)  /* something went wrong */
283     {
284         DeleteObject( hrgn );
285         return 0;
286     }
287     return hrgn;
288 }
289
290
291 /***********************************************************************
292  *           DCE_GetVisRgn
293  *
294  * Return the visible region of a window, i.e. the client or window area
295  * clipped by the client area of all ancestors, and then optionally
296  * by siblings and children.
297  */
298 HRGN DCE_GetVisRgn( HWND hwnd, WORD flags )
299 {
300     RECT16 rect;
301     HRGN hrgn;
302     int xoffset, yoffset;
303     WND *wndPtr = WIN_FindWndPtr( hwnd );
304
305       /* Get visible rectangle and create a region with it 
306        * FIXME: do we really need to calculate vis rgns for X windows? 
307        */
308
309     if (!wndPtr || !DCE_GetVisRect( wndPtr, !(flags & DCX_WINDOW), &rect ))
310     {
311         return CreateRectRgn( 0, 0, 0, 0 );  /* Visible region is empty */
312     }
313     if (!(hrgn = CreateRectRgnIndirect16( &rect ))) return 0;
314
315       /* Clip all children from the visible region */
316
317     if (flags & DCX_CLIPCHILDREN)
318     {
319         if (flags & DCX_WINDOW)
320         {
321             xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
322             yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
323         }
324         else xoffset = yoffset = 0;
325         hrgn = DCE_ClipWindows( wndPtr->child, NULL, hrgn, xoffset, yoffset );
326         if (!hrgn) return 0;
327     }
328
329       /* Clip siblings placed above this window */
330
331     if (flags & DCX_WINDOW)
332     {
333         xoffset = -wndPtr->rectWindow.left;
334         yoffset = -wndPtr->rectWindow.top;
335     }
336     else
337     {
338         xoffset = -wndPtr->rectClient.left;
339         yoffset = -wndPtr->rectClient.top;
340     }
341     if (flags & DCX_CLIPSIBLINGS)
342     {
343         hrgn = DCE_ClipWindows( wndPtr->parent ? wndPtr->parent->child : NULL,
344                                 wndPtr, hrgn, xoffset, yoffset );
345         if (!hrgn) return 0;
346     }
347
348       /* Clip siblings of all ancestors that have the WS_CLIPSIBLINGS style */
349
350     while (wndPtr->dwStyle & WS_CHILD)
351     {
352         wndPtr = wndPtr->parent;
353         xoffset -= wndPtr->rectClient.left;
354         yoffset -= wndPtr->rectClient.top;
355         hrgn = DCE_ClipWindows( wndPtr->parent->child, wndPtr,
356                                 hrgn, xoffset, yoffset );
357         if (!hrgn) return 0;
358     }
359     return hrgn;
360 }
361
362
363 /***********************************************************************
364  *           DCE_SetDrawable
365  *
366  * Set the drawable, origin and dimensions for the DC associated to
367  * a given window.
368  */
369 static void DCE_SetDrawable( WND *wndPtr, DC *dc, WORD flags )
370 {
371     if (!wndPtr)  /* Get a DC for the whole screen */
372     {
373         dc->w.DCOrgX = 0;
374         dc->w.DCOrgY = 0;
375         dc->u.x.drawable = rootWindow;
376         XSetSubwindowMode( display, dc->u.x.gc, IncludeInferiors );
377     }
378     else
379     {
380         if (flags & DCX_WINDOW)
381         {
382             dc->w.DCOrgX  = wndPtr->rectWindow.left;
383             dc->w.DCOrgY  = wndPtr->rectWindow.top;
384         }
385         else
386         {
387             dc->w.DCOrgX  = wndPtr->rectClient.left;
388             dc->w.DCOrgY  = wndPtr->rectClient.top;
389         }
390         while (!wndPtr->window)
391         {
392             wndPtr = wndPtr->parent;
393             dc->w.DCOrgX += wndPtr->rectClient.left;
394             dc->w.DCOrgY += wndPtr->rectClient.top;
395         }
396         dc->w.DCOrgX -= wndPtr->rectWindow.left;
397         dc->w.DCOrgY -= wndPtr->rectWindow.top;
398         dc->u.x.drawable = wndPtr->window;
399     }
400 }
401
402 /***********************************************************************
403  *           GetDCEx    (USER.359)
404  *
405  * Unimplemented flags: DCX_LOCKWINDOWUPDATE
406  */
407 HDC GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
408 {
409     HANDLE      hdce;
410     HRGN        hrgnVisible;
411     HDC         hdc = 0;
412     DCE *       dce;
413     DC *        dc;
414     WND *       wndPtr;
415     DWORD       dcx_flags = 0;
416     BOOL        need_update = TRUE;
417
418     dprintf_dc(stddeb,"GetDCEx: hwnd %04x, hrgnClip %04x, flags %08x\n", hwnd, hrgnClip, (unsigned)flags);
419     
420     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
421
422     if (flags & DCX_USESTYLE)
423     {
424         flags &= ~( DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
425
426         if( wndPtr->dwStyle & WS_CLIPSIBLINGS )
427             flags |= DCX_CLIPSIBLINGS;
428
429         if ( !(flags & DCX_WINDOW) )
430         {
431             if (!(wndPtr->class->style & (CS_OWNDC | CS_CLASSDC)))
432                 flags |= DCX_CACHE;
433
434             if (wndPtr->class->style & CS_PARENTDC) flags |= DCX_PARENTCLIP;
435
436             if (wndPtr->dwStyle & WS_CLIPCHILDREN &&
437                      !(wndPtr->dwStyle & WS_MINIMIZE) ) flags |= DCX_CLIPCHILDREN;
438         }
439         else flags |= DCX_CACHE;
440     }
441
442     if( flags & DCX_NOCLIPCHILDREN )
443       {
444         flags |= DCX_CACHE;
445         flags &= ~(DCX_PARENTCLIP | DCX_CLIPCHILDREN);
446       }
447
448     if (hwnd==GetDesktopWindow() || !(wndPtr->dwStyle & WS_CHILD)) flags &= ~DCX_PARENTCLIP;
449
450     if (flags & DCX_WINDOW) flags = (flags & ~DCX_CLIPCHILDREN) | DCX_CACHE;
451
452     if( flags & DCX_PARENTCLIP )
453       {
454         flags |= DCX_CACHE;
455         if( !(flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) )
456           if( (wndPtr->dwStyle & WS_VISIBLE) && (wndPtr->parent->dwStyle & WS_VISIBLE) )
457             {
458               flags &= ~DCX_CLIPCHILDREN;
459               if( wndPtr->parent->dwStyle & WS_CLIPSIBLINGS )
460                 flags |= DCX_CLIPSIBLINGS;
461             }
462       }
463
464     if (flags & DCX_CACHE)
465     {
466         for (hdce = firstDCE; (hdce); hdce = dce->hNext)
467         {
468             if (!(dce = (DCE *) USER_HEAP_LIN_ADDR( hdce ))) return 0;
469             if ((dce->DCXflags & DCX_CACHE) && !(dce->DCXflags & DCX_DCEBUSY)) break;
470         }
471     }
472     else 
473     {
474         hdce = (wndPtr->class->style & CS_OWNDC)?wndPtr->hdce:wndPtr->class->hdce;
475         dce = (DCE *) USER_HEAP_LIN_ADDR( hdce );
476
477         if( dce->hwndCurrent == hwnd )
478           {
479             dprintf_dc(stddeb,"\tskipping hVisRgn update\n");
480             need_update = FALSE;
481           }
482
483         if( hrgnClip && dce->hClipRgn && !(dce->DCXflags & DCX_KEEPCLIPRGN))
484           {
485             fprintf(stdnimp,"GetDCEx: hClipRgn collision!\n");
486             DeleteObject(dce->hClipRgn); 
487             need_update = TRUE;
488           }
489     }
490
491     dcx_flags = flags & ( DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_CACHE | DCX_WINDOW | DCX_WINDOWPAINT);
492
493     if (!hdce) return 0;
494     dce = (DCE *) USER_HEAP_LIN_ADDR( hdce );
495     dce->hwndCurrent = hwnd;
496     dce->hClipRgn = 0;
497     dce->DCXflags = dcx_flags | DCX_DCEBUSY;
498     hdc = dce->hDC;
499     
500     if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
501
502     DCE_SetDrawable( wndPtr, dc, flags );
503     if( need_update || dc->w.flags & DC_DIRTY )
504      {
505       dprintf_dc(stddeb,"updating hDC anyway\n");
506
507       if (flags & DCX_PARENTCLIP)
508         {
509             WND *parentPtr = wndPtr->parent;
510             dcx_flags = flags & ~(DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
511                                        DCX_WINDOW);
512             if (parentPtr->dwStyle & WS_CLIPSIBLINGS)
513                 dcx_flags |= DCX_CLIPSIBLINGS;
514             hrgnVisible = DCE_GetVisRgn( parentPtr->hwndSelf, dcx_flags );
515             if (flags & DCX_WINDOW)
516                 OffsetRgn( hrgnVisible, -wndPtr->rectWindow.left,
517                                         -wndPtr->rectWindow.top );
518             else OffsetRgn( hrgnVisible, -wndPtr->rectClient.left,
519                                          -wndPtr->rectClient.top );
520         }
521            /* optimize away GetVisRgn for desktop if it isn't there */
522
523       else if ((hwnd == GetDesktopWindow()) &&
524                (rootWindow == DefaultRootWindow(display)))
525                hrgnVisible = CreateRectRgn( 0, 0, SYSMETRICS_CXSCREEN,
526                                                   SYSMETRICS_CYSCREEN);
527       else hrgnVisible = DCE_GetVisRgn( hwnd, flags );
528
529       dc->w.flags &= ~DC_DIRTY;
530
531       SelectVisRgn( hdc, hrgnVisible );
532      }
533     else hrgnVisible = CreateRectRgn(0,0,0,0);
534
535     if ((flags & DCX_INTERSECTRGN) || (flags & DCX_EXCLUDERGN))
536     {
537         dce->DCXflags |= flags & (DCX_KEEPCLIPRGN | DCX_INTERSECTRGN | DCX_EXCLUDERGN);
538         dce->hClipRgn = hrgnClip;
539
540         dprintf_dc(stddeb, "\tsaved VisRgn, clipRgn = %04x\n", hrgnClip);
541
542         SaveVisRgn( hdc );
543         CombineRgn( hrgnVisible, InquireVisRgn( hdc ), hrgnClip,
544                     (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
545         SelectVisRgn( hdc, hrgnVisible );
546     }
547     DeleteObject( hrgnVisible );
548
549     dprintf_dc(stddeb, "GetDCEx(%04x,%04x,0x%lx): returning %04x\n", 
550                hwnd, hrgnClip, flags, hdc);
551     return hdc;
552 }
553
554 /***********************************************************************
555  *           GetDC    (USER.66)
556  */
557 HDC GetDC( HWND hwnd )
558 {
559     if( !hwnd ) return GetDCEx( GetDesktopWindow(), 0, DCX_CACHE | DCX_WINDOW );
560
561     return GetDCEx( hwnd, 0, DCX_USESTYLE );
562 }
563
564
565 /***********************************************************************
566  *           GetWindowDC    (USER.67)
567  */
568 HDC GetWindowDC( HWND hwnd )
569 {
570     if (hwnd)
571     {
572         WND * wndPtr;
573         if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
574     }
575     else hwnd = GetDesktopWindow();
576
577     return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
578 }
579
580
581 /***********************************************************************
582  *           ReleaseDC    (USER.68)
583  */
584 int ReleaseDC( HWND hwnd, HDC hdc )
585 {
586     HANDLE hdce;
587     DCE * dce = NULL;
588     
589     dprintf_dc(stddeb, "ReleaseDC: %04x %04x\n", hwnd, hdc );
590         
591     for (hdce = firstDCE; (hdce); hdce = dce->hNext)
592     {
593         if (!(dce = (DCE *) USER_HEAP_LIN_ADDR( hdce ))) return 0;
594         if (dce->hDC == hdc) break;
595     }
596     if (!hdce) return 0;
597     if (!(dce->DCXflags & DCX_DCEBUSY) ) return 0;
598
599     /* restore previous visible region */
600
601     if ( dce->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN) &&
602         (dce->DCXflags & DCX_CACHE || dce->DCXflags & DCX_WINDOWPAINT) )
603     {
604         dprintf_dc(stddeb,"\tcleaning up visrgn...\n");
605         dce->DCXflags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN | DCX_WINDOWPAINT);
606
607         if( dce->DCXflags & DCX_KEEPCLIPRGN )
608             dce->DCXflags &= ~DCX_KEEPCLIPRGN;
609         else
610             if( dce->hClipRgn > 1 )
611                 DeleteObject( dce->hClipRgn );
612
613         dce->hClipRgn = 0;
614         RestoreVisRgn(dce->hDC);
615     }
616
617     if (dce->DCXflags & DCX_CACHE)
618     {
619         SetDCState( dce->hDC, defaultDCstate );
620         dce->DCXflags = DCX_CACHE;
621         dce->hwndCurrent = 0;
622     }
623     return 1;
624 }
625
626 /***********************************************************************
627  *           DCHook    (USER.362)
628  *
629  * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..  
630  */
631 BOOL DCHook(HDC hDC, WORD code, DWORD data, DWORD lParam)
632 {
633   HANDLE hdce;
634   HRGN hVisRgn;
635
636   dprintf_dc(stddeb,"DCHook: hDC = %04x, %i\n", hDC, code);
637
638   if( HIWORD(data) == DC_MAGIC )
639       hdce = (HANDLE)LOWORD(data);
640   else
641       hdce = DCE_FindDCE(hDC);
642
643   if( !hdce ) return 0;
644
645   switch( code )
646     {
647       case DCHC_INVALIDVISRGN:
648          {
649            DCE* dce = (DCE*) USER_HEAP_LIN_ADDR(hdce);
650
651            if( dce->DCXflags & DCX_DCEBUSY )
652              {
653                SetHookFlags(hDC, DCHF_VALIDATEVISRGN);
654                hVisRgn = DCE_GetVisRgn(dce->hwndCurrent, dce->DCXflags);
655
656                dprintf_dc(stddeb,"\tapplying saved clipRgn\n");
657   
658                /* clip this region with saved clipping region */
659
660                if ( (dce->DCXflags & DCX_INTERSECTRGN && dce->hClipRgn != 1) ||
661                   (  dce->DCXflags & DCX_EXCLUDERGN && dce->hClipRgn) )
662                   {
663
664                     if( (!dce->hClipRgn && dce->DCXflags & DCX_INTERSECTRGN) ||
665                          (dce->hClipRgn == 1 && dce->DCXflags & DCX_EXCLUDERGN) )            
666                          SetRectRgn(hVisRgn,0,0,0,0);
667                     else
668                          CombineRgn(hVisRgn, hVisRgn, dce->hClipRgn, 
669                                       (dce->DCXflags & DCX_EXCLUDERGN)? RGN_DIFF:RGN_AND);
670                   }  
671                SelectVisRgn(hDC, hVisRgn);
672                DeleteObject(hVisRgn);
673              }
674            else
675              dprintf_dc(stddeb,"DCHook: DC is not in use!\n");
676          }
677          break;
678
679       case DCHC_DELETEDC: /* FIXME: ?? */
680          break;
681
682       default:
683          fprintf(stdnimp,"DCHook: unknown code\n");
684     }
685   return 0;
686 }
687