Utility to convert between Unix and Windows paths at the command
[wine] / windows / dce.c
1 /*
2  * USER DCE functions
3  *
4  * Copyright 1993 Alexandre Julliard
5  *           1996,1997 Alex Korobka
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  *
22  * Note: Visible regions of CS_OWNDC/CS_CLASSDC window DCs 
23  * have to be updated dynamically. 
24  * 
25  * Internal DCX flags:
26  *
27  * DCX_DCEEMPTY    - dce is uninitialized
28  * DCX_DCEBUSY     - dce is in use
29  * DCX_DCEDIRTY    - ReleaseDC() should wipe instead of caching
30  * DCX_KEEPCLIPRGN - ReleaseDC() should not delete the clipping region
31  * DCX_WINDOWPAINT - BeginPaint() is in effect
32  */
33
34 #include <assert.h>
35 #include "dce.h"
36 #include "win.h"
37 #include "gdi.h"
38 #include "region.h"
39 #include "user.h"
40 #include "wine/debug.h"
41 #include "windef.h"
42 #include "wingdi.h"
43 #include "wine/winbase16.h"
44 #include "wine/winuser16.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(dc);
47
48 static DCE *firstDCE;
49 static HDC defaultDCstate;
50
51 static void DCE_DeleteClipRgn( DCE* );
52 static INT DCE_ReleaseDC( DCE* );
53
54
55 /***********************************************************************
56  *           DCE_DumpCache
57  */
58 static void DCE_DumpCache(void)
59 {
60     DCE *dce;
61     
62     USER_Lock();
63     dce = firstDCE;
64     
65     DPRINTF("DCE:\n");
66     while( dce )
67     {
68         DPRINTF("\t[0x%08x] hWnd 0x%04x, dcx %08x, %s %s\n",
69              (unsigned)dce, dce->hwndCurrent, (unsigned)dce->DCXflags, 
70              (dce->DCXflags & DCX_CACHE) ? "Cache" : "Owned", 
71              (dce->DCXflags & DCX_DCEBUSY) ? "InUse" : "" );
72         dce = dce->next;
73     }
74
75     USER_Unlock();
76 }
77
78 /***********************************************************************
79  *           DCE_AllocDCE
80  *
81  * Allocate a new DCE.
82  */
83 DCE *DCE_AllocDCE( HWND hWnd, DCE_TYPE type )
84 {
85     DCE * dce;
86     
87     if (!(dce = HeapAlloc( GetProcessHeap(), 0, sizeof(DCE) ))) return NULL;
88     if (!(dce->hDC = CreateDCA( "DISPLAY", NULL, NULL, NULL )))
89     {
90         HeapFree( GetProcessHeap(), 0, dce );
91         return 0;
92     }
93     if (!defaultDCstate) defaultDCstate = GetDCState16( dce->hDC );
94
95     /* store DCE handle in DC hook data field */
96
97     SetDCHook( dce->hDC, DCHook16, (DWORD)dce );
98
99     dce->hwndCurrent = WIN_GetFullHandle( hWnd );
100     dce->hClipRgn    = 0;
101
102     if( type != DCE_CACHE_DC ) /* owned or class DC */
103     {
104         dce->DCXflags = DCX_DCEBUSY;
105         if( hWnd )
106         {
107             LONG style = GetWindowLongW( hWnd, GWL_STYLE );
108             if (style & WS_CLIPCHILDREN) dce->DCXflags |= DCX_CLIPCHILDREN;
109             if (style & WS_CLIPSIBLINGS) dce->DCXflags |= DCX_CLIPSIBLINGS;
110         }
111         SetHookFlags16(dce->hDC,DCHF_INVALIDATEVISRGN);
112     }
113     else dce->DCXflags = DCX_CACHE | DCX_DCEEMPTY;
114     
115     USER_Lock();
116     dce->next = firstDCE;
117     firstDCE = dce;
118     USER_Unlock();
119     return dce;
120 }
121
122
123 /***********************************************************************
124  *           DCE_FreeDCE
125  */
126 DCE* DCE_FreeDCE( DCE *dce )
127 {
128     DCE **ppDCE, *ret;
129
130     if (!dce) return NULL;
131
132     USER_Lock();
133
134     ppDCE = &firstDCE;
135
136     while (*ppDCE && (*ppDCE != dce)) ppDCE = &(*ppDCE)->next;
137     if (*ppDCE == dce) *ppDCE = dce->next;
138     ret = *ppDCE;
139     USER_Unlock();
140
141     SetDCHook(dce->hDC, NULL, 0L);
142
143     DeleteDC( dce->hDC );
144     if( dce->hClipRgn && !(dce->DCXflags & DCX_KEEPCLIPRGN) )
145         DeleteObject(dce->hClipRgn);
146     HeapFree( GetProcessHeap(), 0, dce );
147
148     return ret;
149 }
150
151 /***********************************************************************
152  *           DCE_FreeWindowDCE
153  *
154  * Remove owned DCE and reset unreleased cache DCEs.
155  */
156 void DCE_FreeWindowDCE( HWND hwnd )
157 {
158     DCE *pDCE;
159     WND *pWnd = WIN_GetPtr( hwnd );
160
161     pDCE = firstDCE;
162     while( pDCE )
163     {
164         if( pDCE->hwndCurrent == hwnd )
165         {
166             if( pDCE == pWnd->dce ) /* owned or Class DCE*/
167             {
168                 if (pWnd->clsStyle & CS_OWNDC)  /* owned DCE*/
169                 {
170                     pDCE = DCE_FreeDCE( pDCE );
171                     pWnd->dce = NULL;
172                     continue;
173                 }
174                 else if( pDCE->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN) ) /* Class DCE*/
175                 {
176                     DCE_DeleteClipRgn( pDCE );
177                     pDCE->hwndCurrent = 0;
178                 }
179             }
180             else
181             {
182                 if( pDCE->DCXflags & DCX_DCEBUSY ) /* shared cache DCE */
183                 {
184                     /* FIXME: AFAICS we are doing the right thing here so 
185                      * this should be a WARN. But this is best left as an ERR 
186                      * because the 'application error' is likely to come from 
187                      * another part of Wine (i.e. it's our fault after all). 
188                      * We should change this to WARN when Wine is more stable
189                      * (for 1.0?).
190                      */
191                     ERR("[%08x] GetDC() without ReleaseDC()!\n",hwnd);
192                     DCE_ReleaseDC( pDCE );
193                 }
194
195                 pDCE->DCXflags &= DCX_CACHE;
196                 pDCE->DCXflags |= DCX_DCEEMPTY;
197                 pDCE->hwndCurrent = 0;
198             }
199         }
200         pDCE = pDCE->next;
201     }
202     WIN_ReleasePtr( pWnd );
203 }
204
205
206 /***********************************************************************
207  *   DCE_DeleteClipRgn
208  */
209 static void DCE_DeleteClipRgn( DCE* dce )
210 {
211     dce->DCXflags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN | DCX_WINDOWPAINT);
212
213     if( dce->DCXflags & DCX_KEEPCLIPRGN )
214         dce->DCXflags &= ~DCX_KEEPCLIPRGN;
215     else
216         if( dce->hClipRgn > 1 )
217             DeleteObject( dce->hClipRgn );
218
219     dce->hClipRgn = 0;
220
221     /* make it dirty so that the vis rgn gets recomputed next time */
222     dce->DCXflags |= DCX_DCEDIRTY;
223     SetHookFlags16( dce->hDC, DCHF_INVALIDATEVISRGN );
224 }
225
226
227 /***********************************************************************
228  *   DCE_ReleaseDC
229  */
230 static INT DCE_ReleaseDC( DCE* dce )
231 {
232     if ((dce->DCXflags & (DCX_DCEEMPTY | DCX_DCEBUSY)) != DCX_DCEBUSY) return 0;
233
234     /* restore previous visible region */
235
236     if ((dce->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
237         (dce->DCXflags & (DCX_CACHE | DCX_WINDOWPAINT)) )
238         DCE_DeleteClipRgn( dce );
239
240     if (dce->DCXflags & DCX_CACHE)
241     {
242         /* make the DC clean so that SetDCState doesn't try to update the vis rgn */
243         SetHookFlags16( dce->hDC, DCHF_VALIDATEVISRGN );
244         SetDCState16( dce->hDC, defaultDCstate );
245         dce->DCXflags &= ~DCX_DCEBUSY;
246         if (dce->DCXflags & DCX_DCEDIRTY)
247         {
248             /* don't keep around invalidated entries 
249              * because SetDCState() disables hVisRgn updates
250              * by removing dirty bit. */
251
252             dce->hwndCurrent = 0;
253             dce->DCXflags &= DCX_CACHE;
254             dce->DCXflags |= DCX_DCEEMPTY;
255         }
256     }
257     return 1;
258 }
259
260
261 /***********************************************************************
262  *   DCE_InvalidateDCE
263  *
264  * It is called from SetWindowPos() and EVENT_MapNotify - we have to
265  * mark as dirty all busy DCEs for windows that have pWnd->parent as
266  * an ancestor and whose client rect intersects with specified update
267  * rectangle. In addition, pWnd->parent DCEs may need to be updated if
268  * DCX_CLIPCHILDREN flag is set.  */
269 BOOL DCE_InvalidateDCE(HWND hwnd, const RECT* pRectUpdate)
270 {
271     HWND hwndScope = GetAncestor( hwnd, GA_PARENT );
272     BOOL bRet = FALSE;
273
274     if( hwndScope )
275     {
276         DCE *dce;
277
278         TRACE("scope hwnd = %04x, (%i,%i - %i,%i)\n",
279                      hwndScope, pRectUpdate->left,pRectUpdate->top,
280                      pRectUpdate->right,pRectUpdate->bottom);
281         if(TRACE_ON(dc)) 
282           DCE_DumpCache();
283
284         /* walk all DCEs and fixup non-empty entries */
285
286         for (dce = firstDCE; (dce); dce = dce->next)
287         {
288             if (dce->DCXflags & DCX_DCEEMPTY) continue;
289             if ((dce->hwndCurrent == hwndScope) && !(dce->DCXflags & DCX_CLIPCHILDREN))
290                 continue;  /* child window positions don't bother us */
291
292             /* check if DCE window is within the z-order scope */
293
294             if (hwndScope == dce->hwndCurrent || IsChild( hwndScope, dce->hwndCurrent ))
295             {
296                 if (hwnd != dce->hwndCurrent)
297                 {
298                     /* check if the window rectangle intersects this DCE window */
299                     RECT rect;
300                     GetWindowRect( dce->hwndCurrent, &rect );
301                     MapWindowPoints( 0, hwndScope, (POINT *)&rect, 2 );
302                     if (!IntersectRect( &rect, &rect, pRectUpdate )) continue;
303
304                 }
305                 if( !(dce->DCXflags & DCX_DCEBUSY) )
306                 {
307                     /* Don't bother with visible regions of unused DCEs */
308
309                     TRACE("\tpurged %p dce [%04x]\n", dce, dce->hwndCurrent);
310                     dce->hwndCurrent = 0;
311                     dce->DCXflags &= DCX_CACHE;
312                     dce->DCXflags |= DCX_DCEEMPTY;
313                 }
314                 else
315                 {
316                     /* Set dirty bits in the hDC and DCE structs */
317
318                     TRACE("\tfixed up %p dce [%04x]\n", dce, dce->hwndCurrent);
319                     dce->DCXflags |= DCX_DCEDIRTY;
320                     SetHookFlags16(dce->hDC, DCHF_INVALIDATEVISRGN);
321                     bRet = TRUE;
322                 }
323             }
324         } /* dce list */
325     }
326     return bRet;
327 }
328
329
330 /***********************************************************************
331  *           DCE_ExcludeRgn
332  * 
333  *  Translate given region from the wnd client to the DC coordinates
334  *  and add it to the clipping region.
335  */
336 INT DCE_ExcludeRgn( HDC hDC, HWND hwnd, HRGN hRgn )
337 {
338   POINT  pt = {0, 0};
339   DCE     *dce = firstDCE;
340
341   while (dce && (dce->hDC != hDC)) dce = dce->next;
342   if (!dce) return ERROR;
343
344   MapWindowPoints( hwnd, dce->hwndCurrent, &pt, 1);
345   if( dce->DCXflags & DCX_WINDOW )
346   {
347       WND *wnd = WIN_FindWndPtr(dce->hwndCurrent);
348       pt.x += wnd->rectClient.left - wnd->rectWindow.left;
349       pt.y += wnd->rectClient.top - wnd->rectWindow.top;
350       WIN_ReleaseWndPtr(wnd);
351   }
352   OffsetRgn(hRgn, pt.x, pt.y);
353
354   return ExtSelectClipRgn( hDC, hRgn, RGN_DIFF );
355 }
356
357
358 /***********************************************************************
359  *              GetDCEx (USER32.@)
360  *
361  * Unimplemented flags: DCX_LOCKWINDOWUPDATE
362  *
363  * FIXME: Full support for hrgnClip == 1 (alias for entire window).
364  */
365 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
366 {
367     HDC         hdc = 0;
368     DCE *       dce;
369     WND *       wndPtr;
370     DWORD       dcxFlags = 0;
371     BOOL        bUpdateVisRgn = TRUE;
372     BOOL        bUpdateClipOrigin = FALSE;
373     HWND parent, full;
374
375     TRACE("hwnd %04x, hrgnClip %04x, flags %08x\n", 
376           hwnd, hrgnClip, (unsigned)flags);
377
378     if (!hwnd) hwnd = GetDesktopWindow();
379     if (!(full = WIN_IsCurrentProcess( hwnd )))
380     {
381         FIXME( "not supported yet on other process window %x\n", hwnd );
382         return 0;
383     }
384     hwnd = full;
385     if (!(wndPtr = WIN_GetPtr( hwnd ))) return 0;
386
387     /* fixup flags */
388
389     if (flags & (DCX_WINDOW | DCX_PARENTCLIP)) flags |= DCX_CACHE;
390
391     if (flags & DCX_USESTYLE)
392     {
393         flags &= ~( DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
394
395         if( wndPtr->dwStyle & WS_CLIPSIBLINGS )
396             flags |= DCX_CLIPSIBLINGS;
397
398         if ( !(flags & DCX_WINDOW) )
399         {
400             if (wndPtr->clsStyle & CS_PARENTDC) flags |= DCX_PARENTCLIP;
401
402             if (wndPtr->dwStyle & WS_CLIPCHILDREN &&
403                      !(wndPtr->dwStyle & WS_MINIMIZE) ) flags |= DCX_CLIPCHILDREN;
404             if (!wndPtr->dce) flags |= DCX_CACHE;
405         }
406     }
407
408     if (flags & DCX_WINDOW) flags &= ~DCX_CLIPCHILDREN;
409
410     parent = GetAncestor( hwnd, GA_PARENT );
411     if (!parent || (parent == GetDesktopWindow()))
412         flags = (flags & ~DCX_PARENTCLIP) | DCX_CLIPSIBLINGS;
413
414     /* it seems parent clip is ignored when clipping siblings or children */
415     if (flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) flags &= ~DCX_PARENTCLIP;
416
417     if( flags & DCX_PARENTCLIP )
418     {
419         LONG parent_style = GetWindowLongW( parent, GWL_STYLE );
420         if( (wndPtr->dwStyle & WS_VISIBLE) && (parent_style & WS_VISIBLE) )
421         {
422             flags &= ~DCX_CLIPCHILDREN;
423             if (parent_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
424         }
425     }
426
427     /* find a suitable DCE */
428
429     dcxFlags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | 
430                         DCX_CACHE | DCX_WINDOW);
431
432     if (flags & DCX_CACHE)
433     {
434         DCE*    dceEmpty;
435         DCE*    dceUnused;
436
437         dceEmpty = dceUnused = NULL;
438
439         /* Strategy: First, we attempt to find a non-empty but unused DCE with
440          * compatible flags. Next, we look for an empty entry. If the cache is
441          * full we have to purge one of the unused entries.
442          */
443
444         for (dce = firstDCE; (dce); dce = dce->next)
445         {
446             if ((dce->DCXflags & (DCX_CACHE | DCX_DCEBUSY)) == DCX_CACHE )
447             {
448                 dceUnused = dce;
449
450                 if (dce->DCXflags & DCX_DCEEMPTY)
451                     dceEmpty = dce;
452                 else
453                 if ((dce->hwndCurrent == hwnd) &&
454                    ((dce->DCXflags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
455                                       DCX_CACHE | DCX_WINDOW | DCX_PARENTCLIP)) == dcxFlags))
456                 {
457                     TRACE("\tfound valid %08x dce [%04x], flags %08x\n", 
458                                         (unsigned)dce, hwnd, (unsigned)dcxFlags );
459                     bUpdateVisRgn = FALSE; 
460                     bUpdateClipOrigin = TRUE;
461                     break;
462                 }
463             }
464         }
465
466         if (!dce) dce = (dceEmpty) ? dceEmpty : dceUnused;
467         
468         /* if there's no dce empty or unused, allocate a new one */
469         if (!dce)
470         {
471             dce = DCE_AllocDCE( 0, DCE_CACHE_DC );
472         }
473     }
474     else 
475     {
476         dce = wndPtr->dce;
477         if (dce && dce->hwndCurrent == hwnd)
478         {
479             TRACE("\tskipping hVisRgn update\n");
480             bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
481         }
482     }
483     if (!dce)
484     {
485         hdc = 0;
486         goto END;
487     }
488
489     if (!(flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN))) hrgnClip = 0;
490
491     if (((flags ^ dce->DCXflags) & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
492         (dce->hClipRgn != hrgnClip))
493     {
494         /* if the extra clip region has changed, get rid of the old one */
495         DCE_DeleteClipRgn( dce );
496     }
497
498     dce->hwndCurrent = hwnd;
499     dce->hClipRgn = hrgnClip;
500     dce->DCXflags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
501                              DCX_CACHE | DCX_WINDOW | DCX_WINDOWPAINT |
502                              DCX_KEEPCLIPRGN | DCX_INTERSECTRGN | DCX_EXCLUDERGN);
503     dce->DCXflags |= DCX_DCEBUSY;
504     dce->DCXflags &= ~DCX_DCEDIRTY;
505     hdc = dce->hDC;
506
507     if (bUpdateVisRgn) SetHookFlags16( hdc, DCHF_INVALIDATEVISRGN ); /* force update */
508
509     if (!USER_Driver.pGetDC( hwnd, hdc, hrgnClip, flags )) hdc = 0;
510
511     TRACE("(%04x,%04x,0x%lx): returning %04x\n", hwnd, hrgnClip, flags, hdc);
512 END:
513     WIN_ReleasePtr(wndPtr);
514     return hdc;
515 }
516
517
518 /***********************************************************************
519  *              GetDC (USER32.@)
520  * RETURNS
521  *      :Handle to DC
522  *      NULL: Failure
523  */
524 HDC WINAPI GetDC(
525              HWND hwnd /* [in] handle of window */
526 ) {
527     if (!hwnd)
528         return GetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
529     return GetDCEx( hwnd, 0, DCX_USESTYLE );
530 }
531
532
533 /***********************************************************************
534  *              GetWindowDC (USER32.@)
535  */
536 HDC WINAPI GetWindowDC( HWND hwnd )
537 {
538     return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
539 }
540
541
542 /***********************************************************************
543  *              ReleaseDC (USER32.@)
544  *
545  * RETURNS
546  *      1: Success
547  *      0: Failure
548  */
549 INT WINAPI ReleaseDC( 
550              HWND hwnd /* [in] Handle of window - ignored */, 
551              HDC hdc   /* [in] Handle of device context */
552 ) {
553     DCE * dce;
554     INT nRet = 0;
555
556     USER_Lock();
557     dce = firstDCE;
558     
559     TRACE("%04x %04x\n", hwnd, hdc );
560         
561     while (dce && (dce->hDC != hdc)) dce = dce->next;
562
563     if ( dce ) 
564         if ( dce->DCXflags & DCX_DCEBUSY )
565             nRet = DCE_ReleaseDC( dce );
566
567     USER_Unlock();
568
569     return nRet;
570 }
571
572 /***********************************************************************
573  *              DCHook (USER.362)
574  *
575  * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..  
576  */
577 BOOL16 WINAPI DCHook16( HDC16 hDC, WORD code, DWORD data, LPARAM lParam )
578 {
579     BOOL retv = TRUE;
580     DCE *dce = (DCE *)data;
581
582     TRACE("hDC = %04x, %i\n", hDC, code);
583
584     if (!dce) return 0;
585     assert(dce->hDC == hDC);
586
587     /* Grab the windows lock before doing anything else  */
588     USER_Lock();
589
590     switch( code )
591     {
592       case DCHC_INVALIDVISRGN:
593            /* GDI code calls this when it detects that the
594             * DC is dirty (usually after SetHookFlags()). This
595             * means that we have to recompute the visible region.
596             */
597            if( dce->DCXflags & DCX_DCEBUSY )
598            {
599                /* Dirty bit has been cleared by caller, set it again so that
600                 * pGetDC recomputes the visible region. */
601                SetHookFlags16( dce->hDC, DCHF_INVALIDATEVISRGN );
602                USER_Driver.pGetDC( dce->hwndCurrent, dce->hDC, dce->hClipRgn, dce->DCXflags );
603            }
604            else /* non-fatal but shouldn't happen */
605              WARN("DC is not in use!\n");
606            break;
607
608       case DCHC_DELETEDC:
609            /*
610             * Windows will not let you delete a DC that is busy
611             * (between GetDC and ReleaseDC)
612             */
613
614            if ( dce->DCXflags & DCX_DCEBUSY )
615            {
616                WARN("Application trying to delete a busy DC\n");
617                retv = FALSE;
618            }
619            else DCE_FreeDCE( dce );
620            break;
621
622       default:
623            FIXME("unknown code\n");
624     }
625
626   USER_Unlock();  /* Release the wnd lock */
627   return retv;
628 }
629
630
631 /**********************************************************************
632  *              WindowFromDC (USER32.@)
633  */
634 HWND WINAPI WindowFromDC( HDC hDC )
635 {
636     DCE *dce;
637     HWND hwnd;
638
639     USER_Lock();
640     dce = firstDCE;
641     
642     while (dce && (dce->hDC != hDC)) dce = dce->next;
643
644     hwnd = dce ? dce->hwndCurrent : 0;
645     USER_Unlock();
646     
647     return hwnd;
648 }
649
650
651 /***********************************************************************
652  *              LockWindowUpdate (USER32.@)
653  */
654 BOOL WINAPI LockWindowUpdate( HWND hwnd )
655 {
656     FIXME("(%x), stub!\n",hwnd);
657     return TRUE;
658 }