Add proxy authentication dialog.
[wine/testsucceed.git] / windows / dce.c
blob8f0c63e68bc8566bc7f0aa0e51aa99bcbb6b5c5d
1 /*
2 * USER DCE functions
4 * Copyright 1993 Alexandre Julliard
5 * 1996,1997 Alex Korobka
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.
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.
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
22 * Note: Visible regions of CS_OWNDC/CS_CLASSDC window DCs
23 * have to be updated dynamically.
25 * Internal DCX flags:
27 * DCX_DCEEMPTY - dce is uninitialized
28 * DCX_DCEBUSY - dce is in use
29 * DCX_DCEDIRTY - ReleaseDC() should wipe instead of caching
30 * DCX_WINDOWPAINT - BeginPaint() is in effect
33 #include <assert.h>
34 #include "dce.h"
35 #include "win.h"
36 #include "user_private.h"
37 #include "wine/debug.h"
38 #include "windef.h"
39 #include "wingdi.h"
40 #include "wownt32.h"
41 #include "wine/winbase16.h"
42 #include "wine/winuser16.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(dc);
46 static DCE *firstDCE;
47 static HDC16 defaultDCstate;
49 static void DCE_DeleteClipRgn( DCE* );
50 static INT DCE_ReleaseDC( DCE* );
53 /***********************************************************************
54 * DCE_DumpCache
56 static void DCE_DumpCache(void)
58 DCE *dce;
60 USER_Lock();
61 dce = firstDCE;
63 DPRINTF("DCE:\n");
64 while( dce )
66 DPRINTF("\t[0x%08x] hWnd %p, dcx %08x, %s %s\n",
67 (unsigned)dce, dce->hwndCurrent, (unsigned)dce->DCXflags,
68 (dce->DCXflags & DCX_CACHE) ? "Cache" : "Owned",
69 (dce->DCXflags & DCX_DCEBUSY) ? "InUse" : "" );
70 dce = dce->next;
73 USER_Unlock();
76 /***********************************************************************
77 * DCE_AllocDCE
79 * Allocate a new DCE.
81 DCE *DCE_AllocDCE( HWND hWnd, DCE_TYPE type )
83 static const WCHAR szDisplayW[] = { 'D','I','S','P','L','A','Y','\0' };
84 DCE * dce;
86 TRACE("(%p,%d)\n", hWnd, type);
88 if (!(dce = HeapAlloc( GetProcessHeap(), 0, sizeof(DCE) ))) return NULL;
89 if (!(dce->hDC = CreateDCW( szDisplayW, NULL, NULL, NULL )))
91 HeapFree( GetProcessHeap(), 0, dce );
92 return 0;
94 if (!defaultDCstate) defaultDCstate = GetDCState16( HDC_16(dce->hDC) );
96 /* store DCE handle in DC hook data field */
98 SetDCHook( dce->hDC, DCHook16, (DWORD)dce );
100 dce->hwndCurrent = WIN_GetFullHandle( hWnd );
101 dce->hClipRgn = 0;
103 if( type != DCE_CACHE_DC ) /* owned or class DC */
105 dce->DCXflags = DCX_DCEBUSY;
106 if( hWnd )
108 LONG style = GetWindowLongW( hWnd, GWL_STYLE );
109 if (style & WS_CLIPCHILDREN) dce->DCXflags |= DCX_CLIPCHILDREN;
110 if (style & WS_CLIPSIBLINGS) dce->DCXflags |= DCX_CLIPSIBLINGS;
112 SetHookFlags16( HDC_16(dce->hDC), DCHF_INVALIDATEVISRGN );
114 else dce->DCXflags = DCX_CACHE | DCX_DCEEMPTY;
116 USER_Lock();
117 dce->next = firstDCE;
118 firstDCE = dce;
119 USER_Unlock();
120 return dce;
124 /***********************************************************************
125 * DCE_FreeDCE
127 DCE* DCE_FreeDCE( DCE *dce )
129 DCE **ppDCE, *ret;
131 if (!dce) return NULL;
133 USER_Lock();
135 ppDCE = &firstDCE;
137 while (*ppDCE && (*ppDCE != dce)) ppDCE = &(*ppDCE)->next;
138 if (*ppDCE == dce) *ppDCE = dce->next;
139 ret = *ppDCE;
140 USER_Unlock();
142 SetDCHook(dce->hDC, NULL, 0L);
144 DeleteDC( dce->hDC );
145 if (dce->hClipRgn) DeleteObject(dce->hClipRgn);
146 HeapFree( GetProcessHeap(), 0, dce );
148 return ret;
151 /***********************************************************************
152 * DCE_FreeWindowDCE
154 * Remove owned DCE and reset unreleased cache DCEs.
156 void DCE_FreeWindowDCE( HWND hwnd )
158 DCE *pDCE;
159 WND *pWnd = WIN_GetPtr( hwnd );
161 pDCE = firstDCE;
162 while( pDCE )
164 if( pDCE->hwndCurrent == hwnd )
166 if( pDCE == pWnd->dce ) /* owned or Class DCE*/
168 if (pWnd->clsStyle & CS_OWNDC) /* owned DCE*/
170 pDCE = DCE_FreeDCE( pDCE );
171 pWnd->dce = NULL;
172 continue;
174 else if( pDCE->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN) ) /* Class DCE*/
176 if (USER_Driver.pReleaseDC)
177 USER_Driver.pReleaseDC( pDCE->hwndCurrent, pDCE->hDC );
178 DCE_DeleteClipRgn( pDCE );
179 pDCE->hwndCurrent = 0;
182 else
184 if( pDCE->DCXflags & DCX_DCEBUSY ) /* shared cache DCE */
186 /* FIXME: AFAICS we are doing the right thing here so
187 * this should be a WARN. But this is best left as an ERR
188 * because the 'application error' is likely to come from
189 * another part of Wine (i.e. it's our fault after all).
190 * We should change this to WARN when Wine is more stable
191 * (for 1.0?).
193 ERR("[%p] GetDC() without ReleaseDC()!\n",hwnd);
194 DCE_ReleaseDC( pDCE );
197 if (pDCE->hwndCurrent && USER_Driver.pReleaseDC)
198 USER_Driver.pReleaseDC( pDCE->hwndCurrent, pDCE->hDC );
199 pDCE->DCXflags &= DCX_CACHE;
200 pDCE->DCXflags |= DCX_DCEEMPTY;
201 pDCE->hwndCurrent = 0;
204 pDCE = pDCE->next;
206 WIN_ReleasePtr( pWnd );
210 /***********************************************************************
211 * DCE_DeleteClipRgn
213 static void DCE_DeleteClipRgn( DCE* dce )
215 dce->DCXflags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN | DCX_WINDOWPAINT);
217 if (dce->hClipRgn) DeleteObject( dce->hClipRgn );
218 dce->hClipRgn = 0;
220 /* make it dirty so that the vis rgn gets recomputed next time */
221 dce->DCXflags |= DCX_DCEDIRTY;
222 SetHookFlags16( HDC_16(dce->hDC), DCHF_INVALIDATEVISRGN );
226 /***********************************************************************
227 * DCE_ReleaseDC
229 static INT DCE_ReleaseDC( DCE* dce )
231 if ((dce->DCXflags & (DCX_DCEEMPTY | DCX_DCEBUSY)) != DCX_DCEBUSY) return 0;
233 /* restore previous visible region */
235 if ((dce->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
236 (dce->DCXflags & (DCX_CACHE | DCX_WINDOWPAINT)) )
237 DCE_DeleteClipRgn( dce );
239 if (dce->DCXflags & DCX_CACHE)
241 /* make the DC clean so that SetDCState doesn't try to update the vis rgn */
242 SetHookFlags16( HDC_16(dce->hDC), DCHF_VALIDATEVISRGN );
243 SetDCState16( HDC_16(dce->hDC), defaultDCstate );
244 dce->DCXflags &= ~DCX_DCEBUSY;
245 if (dce->DCXflags & DCX_DCEDIRTY)
247 /* don't keep around invalidated entries
248 * because SetDCState() disables hVisRgn updates
249 * by removing dirty bit. */
250 if (dce->hwndCurrent && USER_Driver.pReleaseDC)
251 USER_Driver.pReleaseDC( dce->hwndCurrent, dce->hDC );
252 dce->hwndCurrent = 0;
253 dce->DCXflags &= DCX_CACHE;
254 dce->DCXflags |= DCX_DCEEMPTY;
257 return 1;
261 /***********************************************************************
262 * DCE_InvalidateDCE
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)
271 HWND hwndScope = GetAncestor( hwnd, GA_PARENT );
272 BOOL bRet = FALSE;
274 if( hwndScope )
276 DCE *dce;
278 TRACE("scope hwnd = %p, (%ld,%ld - %ld,%ld)\n",
279 hwndScope, pRectUpdate->left,pRectUpdate->top,
280 pRectUpdate->right,pRectUpdate->bottom);
281 if(TRACE_ON(dc))
282 DCE_DumpCache();
284 /* walk all DCEs and fixup non-empty entries */
286 for (dce = firstDCE; (dce); dce = dce->next)
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 */
292 /* check if DCE window is within the z-order scope */
294 if (hwndScope == dce->hwndCurrent || IsChild( hwndScope, dce->hwndCurrent ))
296 if (hwnd != dce->hwndCurrent)
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;
305 if( !(dce->DCXflags & DCX_DCEBUSY) )
307 /* Don't bother with visible regions of unused DCEs */
309 TRACE("\tpurged %p dce [%p]\n", dce, dce->hwndCurrent);
310 if (dce->hwndCurrent && USER_Driver.pReleaseDC)
311 USER_Driver.pReleaseDC( dce->hwndCurrent, dce->hDC );
312 dce->hwndCurrent = 0;
313 dce->DCXflags &= DCX_CACHE;
314 dce->DCXflags |= DCX_DCEEMPTY;
316 else
318 /* Set dirty bits in the hDC and DCE structs */
320 TRACE("\tfixed up %p dce [%p]\n", dce, dce->hwndCurrent);
321 dce->DCXflags |= DCX_DCEDIRTY;
322 SetHookFlags16( HDC_16(dce->hDC), DCHF_INVALIDATEVISRGN );
323 bRet = TRUE;
326 } /* dce list */
328 return bRet;
332 /***********************************************************************
333 * GetDCEx (USER32.@)
335 * Unimplemented flags: DCX_LOCKWINDOWUPDATE
337 * FIXME: Full support for hrgnClip == 1 (alias for entire window).
339 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
341 HDC hdc = 0;
342 DCE * dce;
343 WND * wndPtr;
344 DWORD dcxFlags = 0;
345 BOOL bUpdateVisRgn = TRUE;
346 BOOL bUpdateClipOrigin = FALSE;
347 HWND parent, full;
349 TRACE("hwnd %p, hrgnClip %p, flags %08lx\n", hwnd, hrgnClip, flags);
351 if (flags & (DCX_LOCKWINDOWUPDATE)) {
352 FIXME("not yet supported - see source\n");
353 /* See the comment in LockWindowUpdate for more explanation. This flag is not implemented
354 * by that patch, but we need LockWindowUpdate implemented correctly before this can be done.
358 if (!hwnd) hwnd = GetDesktopWindow();
359 if (!(full = WIN_IsCurrentProcess( hwnd )))
361 FIXME( "not supported yet on other process window %p\n", hwnd );
362 return 0;
364 hwnd = full;
365 if (!(wndPtr = WIN_GetPtr( hwnd ))) return 0;
367 /* fixup flags */
369 if (flags & (DCX_WINDOW | DCX_PARENTCLIP)) flags |= DCX_CACHE;
371 if (flags & DCX_USESTYLE)
373 flags &= ~( DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
375 if( wndPtr->dwStyle & WS_CLIPSIBLINGS )
376 flags |= DCX_CLIPSIBLINGS;
378 if ( !(flags & DCX_WINDOW) )
380 if (wndPtr->clsStyle & CS_PARENTDC) flags |= DCX_PARENTCLIP;
382 if (wndPtr->dwStyle & WS_CLIPCHILDREN &&
383 !(wndPtr->dwStyle & WS_MINIMIZE) ) flags |= DCX_CLIPCHILDREN;
384 if (!wndPtr->dce) flags |= DCX_CACHE;
388 if (flags & DCX_WINDOW) flags &= ~DCX_CLIPCHILDREN;
390 parent = GetAncestor( hwnd, GA_PARENT );
391 if (!parent || (parent == GetDesktopWindow()))
392 flags = (flags & ~DCX_PARENTCLIP) | DCX_CLIPSIBLINGS;
394 /* it seems parent clip is ignored when clipping siblings or children */
395 if (flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) flags &= ~DCX_PARENTCLIP;
397 if( flags & DCX_PARENTCLIP )
399 LONG parent_style = GetWindowLongW( parent, GWL_STYLE );
400 if( (wndPtr->dwStyle & WS_VISIBLE) && (parent_style & WS_VISIBLE) )
402 flags &= ~DCX_CLIPCHILDREN;
403 if (parent_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
407 /* find a suitable DCE */
409 dcxFlags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
410 DCX_CACHE | DCX_WINDOW);
412 if (flags & DCX_CACHE)
414 DCE* dceEmpty;
415 DCE* dceUnused;
417 dceEmpty = dceUnused = NULL;
419 /* Strategy: First, we attempt to find a non-empty but unused DCE with
420 * compatible flags. Next, we look for an empty entry. If the cache is
421 * full we have to purge one of the unused entries.
424 for (dce = firstDCE; (dce); dce = dce->next)
426 if ((dce->DCXflags & (DCX_CACHE | DCX_DCEBUSY)) == DCX_CACHE )
428 dceUnused = dce;
430 if (dce->DCXflags & DCX_DCEEMPTY)
431 dceEmpty = dce;
432 else
433 if ((dce->hwndCurrent == hwnd) &&
434 ((dce->DCXflags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
435 DCX_CACHE | DCX_WINDOW | DCX_PARENTCLIP)) == dcxFlags))
437 TRACE("\tfound valid %p dce [%p], flags %08lx\n",
438 dce, hwnd, dcxFlags );
439 bUpdateVisRgn = FALSE;
440 bUpdateClipOrigin = TRUE;
441 break;
446 if (!dce) dce = (dceEmpty) ? dceEmpty : dceUnused;
448 /* if there's no dce empty or unused, allocate a new one */
449 if (!dce)
451 dce = DCE_AllocDCE( 0, DCE_CACHE_DC );
454 else
456 dce = wndPtr->dce;
457 if (dce && dce->hwndCurrent == hwnd)
459 TRACE("\tskipping hVisRgn update\n");
460 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
463 if (!dce)
465 hdc = 0;
466 goto END;
469 if (!(flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN))) hrgnClip = 0;
471 if (((flags ^ dce->DCXflags) & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
472 (dce->hClipRgn != hrgnClip))
474 /* if the extra clip region has changed, get rid of the old one */
475 DCE_DeleteClipRgn( dce );
478 dce->hwndCurrent = hwnd;
479 dce->hClipRgn = hrgnClip;
480 dce->DCXflags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
481 DCX_CACHE | DCX_WINDOW | DCX_WINDOWPAINT |
482 DCX_INTERSECTRGN | DCX_EXCLUDERGN);
483 dce->DCXflags |= DCX_DCEBUSY;
484 dce->DCXflags &= ~DCX_DCEDIRTY;
485 hdc = dce->hDC;
487 if (bUpdateVisRgn) SetHookFlags16( HDC_16(hdc), DCHF_INVALIDATEVISRGN ); /* force update */
489 if (!USER_Driver.pGetDC || !USER_Driver.pGetDC( hwnd, hdc, hrgnClip, flags ))
490 hdc = 0;
492 TRACE("(%p,%p,0x%lx): returning %p\n", hwnd, hrgnClip, flags, hdc);
493 END:
494 WIN_ReleasePtr(wndPtr);
495 return hdc;
499 /***********************************************************************
500 * GetDC (USER32.@)
502 * Get a device context.
504 * RETURNS
505 * Success: Handle to the device context
506 * Failure: NULL.
508 HDC WINAPI GetDC( HWND hwnd )
510 if (!hwnd)
511 return GetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
512 return GetDCEx( hwnd, 0, DCX_USESTYLE );
516 /***********************************************************************
517 * GetWindowDC (USER32.@)
519 HDC WINAPI GetWindowDC( HWND hwnd )
521 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
525 /***********************************************************************
526 * ReleaseDC (USER32.@)
528 * Release a device context.
530 * RETURNS
531 * Success: Non-zero. Resources used by hdc are released.
532 * Failure: 0.
534 INT WINAPI ReleaseDC( HWND hwnd, HDC hdc )
536 DCE * dce;
537 INT nRet = 0;
539 USER_Lock();
540 dce = firstDCE;
542 TRACE("%p %p\n", hwnd, hdc );
544 while (dce && (dce->hDC != hdc)) dce = dce->next;
546 if ( dce )
547 if ( dce->DCXflags & DCX_DCEBUSY )
548 nRet = DCE_ReleaseDC( dce );
550 USER_Unlock();
552 return nRet;
555 /***********************************************************************
556 * DCHook (USER.362)
558 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
560 BOOL16 WINAPI DCHook16( HDC16 hDC, WORD code, DWORD data, LPARAM lParam )
562 BOOL retv = TRUE;
563 DCE *dce = (DCE *)data;
565 TRACE("hDC = %04x, %i\n", hDC, code);
567 if (!dce) return 0;
568 assert( HDC_16(dce->hDC) == hDC );
570 /* Grab the windows lock before doing anything else */
571 USER_Lock();
573 switch( code )
575 case DCHC_INVALIDVISRGN:
576 /* GDI code calls this when it detects that the
577 * DC is dirty (usually after SetHookFlags()). This
578 * means that we have to recompute the visible region.
580 if( dce->DCXflags & DCX_DCEBUSY )
582 /* Dirty bit has been cleared by caller, set it again so that
583 * pGetDC recomputes the visible region. */
584 SetHookFlags16( hDC, DCHF_INVALIDATEVISRGN );
585 if (USER_Driver.pGetDC)
586 USER_Driver.pGetDC( dce->hwndCurrent, dce->hDC, dce->hClipRgn, dce->DCXflags );
588 else /* non-fatal but shouldn't happen */
589 WARN("DC is not in use!\n");
590 break;
592 case DCHC_DELETEDC:
594 * Windows will not let you delete a DC that is busy
595 * (between GetDC and ReleaseDC)
598 if ( dce->DCXflags & DCX_DCEBUSY )
600 WARN("Application trying to delete a busy DC\n");
601 retv = FALSE;
603 else DCE_FreeDCE( dce );
604 break;
606 default:
607 FIXME("unknown code\n");
610 USER_Unlock(); /* Release the wnd lock */
611 return retv;
615 /**********************************************************************
616 * WindowFromDC (USER32.@)
618 HWND WINAPI WindowFromDC( HDC hDC )
620 DCE *dce;
621 HWND hwnd;
623 USER_Lock();
624 dce = firstDCE;
626 while (dce && (dce->hDC != hDC)) dce = dce->next;
628 hwnd = dce ? dce->hwndCurrent : 0;
629 USER_Unlock();
631 return hwnd;
635 /***********************************************************************
636 * LockWindowUpdate (USER32.@)
638 BOOL WINAPI LockWindowUpdate( HWND hwnd )
640 static HWND lockedWnd;
642 /* This function is fully implemented by the following patch:
644 * http://www.winehq.org/hypermail/wine-patches/2004/01/0142.html
646 * but in order to work properly, it needs the ability to invalidate
647 * DCEs in other processes when the lock window is changed, which
648 * isn't possible yet.
649 * -mike
652 FIXME("(%p), partial stub!\n",hwnd);
654 USER_Lock();
655 if (lockedWnd)
657 if (!hwnd)
659 /* Unlock lockedWnd */
660 /* FIXME: Do something */
662 else
664 /* Attempted to lock a second window */
665 /* Return FALSE and do nothing */
666 USER_Unlock();
667 return FALSE;
670 lockedWnd = hwnd;
671 USER_Unlock();
672 return TRUE;