Fixed handling of visible region in Save/RestoreDC.
[wine.git] / windows / dce.c
blob2cc7383f72313f78e43e3c97ff360e3f260c9f03
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_KEEPCLIPRGN - ReleaseDC() should not delete the clipping region
31 * DCX_WINDOWPAINT - BeginPaint() is in effect
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"
46 WINE_DEFAULT_DEBUG_CHANNEL(dc);
48 static DCE *firstDCE;
49 static HDC defaultDCstate;
51 static void DCE_DeleteClipRgn( DCE* );
52 static INT DCE_ReleaseDC( DCE* );
55 /***********************************************************************
56 * DCE_DumpCache
58 static void DCE_DumpCache(void)
60 DCE *dce;
62 USER_Lock();
63 dce = firstDCE;
65 DPRINTF("DCE:\n");
66 while( dce )
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;
75 USER_Unlock();
78 /***********************************************************************
79 * DCE_AllocDCE
81 * Allocate a new DCE.
83 DCE *DCE_AllocDCE( HWND hWnd, DCE_TYPE type )
85 DCE * dce;
87 if (!(dce = HeapAlloc( GetProcessHeap(), 0, sizeof(DCE) ))) return NULL;
88 if (!(dce->hDC = CreateDCA( "DISPLAY", NULL, NULL, NULL )))
90 HeapFree( GetProcessHeap(), 0, dce );
91 return 0;
93 if (!defaultDCstate) defaultDCstate = GetDCState16( dce->hDC );
95 /* store DCE handle in DC hook data field */
97 SetDCHook( dce->hDC, DCHook16, (DWORD)dce );
99 dce->hwndCurrent = WIN_GetFullHandle( hWnd );
100 dce->hClipRgn = 0;
102 if( type != DCE_CACHE_DC ) /* owned or class DC */
104 dce->DCXflags = DCX_DCEBUSY;
105 if( hWnd )
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;
111 SetHookFlags16(dce->hDC,DCHF_INVALIDATEVISRGN);
113 else dce->DCXflags = DCX_CACHE | DCX_DCEEMPTY;
115 USER_Lock();
116 dce->next = firstDCE;
117 firstDCE = dce;
118 USER_Unlock();
119 return dce;
123 /***********************************************************************
124 * DCE_FreeDCE
126 DCE* DCE_FreeDCE( DCE *dce )
128 DCE **ppDCE, *ret;
130 if (!dce) return NULL;
132 USER_Lock();
134 ppDCE = &firstDCE;
136 while (*ppDCE && (*ppDCE != dce)) ppDCE = &(*ppDCE)->next;
137 if (*ppDCE == dce) *ppDCE = dce->next;
138 ret = *ppDCE;
139 USER_Unlock();
141 SetDCHook(dce->hDC, NULL, 0L);
143 DeleteDC( dce->hDC );
144 if( dce->hClipRgn && !(dce->DCXflags & DCX_KEEPCLIPRGN) )
145 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 DCE_DeleteClipRgn( pDCE );
177 pDCE->hwndCurrent = 0;
180 else
182 if( pDCE->DCXflags & DCX_DCEBUSY ) /* shared cache DCE */
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?).
191 ERR("[%08x] GetDC() without ReleaseDC()!\n",hwnd);
192 DCE_ReleaseDC( pDCE );
195 pDCE->DCXflags &= DCX_CACHE;
196 pDCE->DCXflags |= DCX_DCEEMPTY;
197 pDCE->hwndCurrent = 0;
200 pDCE = pDCE->next;
202 WIN_ReleasePtr( pWnd );
206 /***********************************************************************
207 * DCE_DeleteClipRgn
209 static void DCE_DeleteClipRgn( DCE* dce )
211 dce->DCXflags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN | DCX_WINDOWPAINT);
213 if( dce->DCXflags & DCX_KEEPCLIPRGN )
214 dce->DCXflags &= ~DCX_KEEPCLIPRGN;
215 else
216 if( dce->hClipRgn > 1 )
217 DeleteObject( dce->hClipRgn );
219 dce->hClipRgn = 0;
221 /* make it dirty so that the vis rgn gets recomputed next time */
222 dce->DCXflags |= DCX_DCEDIRTY;
223 SetHookFlags16( dce->hDC, DCHF_INVALIDATEVISRGN );
227 /***********************************************************************
228 * DCE_ReleaseDC
230 static INT DCE_ReleaseDC( DCE* dce )
232 if ((dce->DCXflags & (DCX_DCEEMPTY | DCX_DCEBUSY)) != DCX_DCEBUSY) return 0;
234 /* restore previous visible region */
236 if ((dce->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
237 (dce->DCXflags & (DCX_CACHE | DCX_WINDOWPAINT)) )
238 DCE_DeleteClipRgn( dce );
240 if (dce->DCXflags & DCX_CACHE)
242 SetDCState16( dce->hDC, defaultDCstate );
243 dce->DCXflags &= ~DCX_DCEBUSY;
244 if (dce->DCXflags & DCX_DCEDIRTY)
246 /* don't keep around invalidated entries
247 * because SetDCState() disables hVisRgn updates
248 * by removing dirty bit. */
250 dce->hwndCurrent = 0;
251 dce->DCXflags &= DCX_CACHE;
252 dce->DCXflags |= DCX_DCEEMPTY;
255 return 1;
259 /***********************************************************************
260 * DCE_InvalidateDCE
262 * It is called from SetWindowPos() and EVENT_MapNotify - we have to
263 * mark as dirty all busy DCEs for windows that have pWnd->parent as
264 * an ancestor and whose client rect intersects with specified update
265 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
266 * DCX_CLIPCHILDREN flag is set. */
267 BOOL DCE_InvalidateDCE(HWND hwnd, const RECT* pRectUpdate)
269 HWND hwndScope = GetAncestor( hwnd, GA_PARENT );
270 BOOL bRet = FALSE;
272 if( hwndScope )
274 DCE *dce;
276 TRACE("scope hwnd = %04x, (%i,%i - %i,%i)\n",
277 hwndScope, pRectUpdate->left,pRectUpdate->top,
278 pRectUpdate->right,pRectUpdate->bottom);
279 if(TRACE_ON(dc))
280 DCE_DumpCache();
282 /* walk all DCEs and fixup non-empty entries */
284 for (dce = firstDCE; (dce); dce = dce->next)
286 if (dce->DCXflags & DCX_DCEEMPTY) continue;
287 if ((dce->hwndCurrent == hwndScope) && !(dce->DCXflags & DCX_CLIPCHILDREN))
288 continue; /* child window positions don't bother us */
290 /* check if DCE window is within the z-order scope */
292 if (hwndScope == dce->hwndCurrent || IsChild( hwndScope, dce->hwndCurrent ))
294 if (hwnd != dce->hwndCurrent)
296 /* check if the window rectangle intersects this DCE window */
297 RECT rect;
298 GetWindowRect( dce->hwndCurrent, &rect );
299 MapWindowPoints( 0, hwndScope, (POINT *)&rect, 2 );
300 if (!IntersectRect( &rect, &rect, pRectUpdate )) continue;
303 if( !(dce->DCXflags & DCX_DCEBUSY) )
305 /* Don't bother with visible regions of unused DCEs */
307 TRACE("\tpurged %p dce [%04x]\n", dce, dce->hwndCurrent);
308 dce->hwndCurrent = 0;
309 dce->DCXflags &= DCX_CACHE;
310 dce->DCXflags |= DCX_DCEEMPTY;
312 else
314 /* Set dirty bits in the hDC and DCE structs */
316 TRACE("\tfixed up %p dce [%04x]\n", dce, dce->hwndCurrent);
317 dce->DCXflags |= DCX_DCEDIRTY;
318 SetHookFlags16(dce->hDC, DCHF_INVALIDATEVISRGN);
319 bRet = TRUE;
322 } /* dce list */
324 return bRet;
328 /***********************************************************************
329 * DCE_ExcludeRgn
331 * Translate given region from the wnd client to the DC coordinates
332 * and add it to the clipping region.
334 INT DCE_ExcludeRgn( HDC hDC, HWND hwnd, HRGN hRgn )
336 POINT pt = {0, 0};
337 DCE *dce = firstDCE;
339 while (dce && (dce->hDC != hDC)) dce = dce->next;
340 if (!dce) return ERROR;
342 MapWindowPoints( hwnd, dce->hwndCurrent, &pt, 1);
343 if( dce->DCXflags & DCX_WINDOW )
345 WND *wnd = WIN_FindWndPtr(dce->hwndCurrent);
346 pt.x += wnd->rectClient.left - wnd->rectWindow.left;
347 pt.y += wnd->rectClient.top - wnd->rectWindow.top;
348 WIN_ReleaseWndPtr(wnd);
350 OffsetRgn(hRgn, pt.x, pt.y);
352 return ExtSelectClipRgn( hDC, hRgn, RGN_DIFF );
356 /***********************************************************************
357 * GetDCEx (USER32.@)
359 * Unimplemented flags: DCX_LOCKWINDOWUPDATE
361 * FIXME: Full support for hrgnClip == 1 (alias for entire window).
363 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
365 HDC hdc = 0;
366 DCE * dce;
367 WND * wndPtr;
368 DWORD dcxFlags = 0;
369 BOOL bUpdateVisRgn = TRUE;
370 BOOL bUpdateClipOrigin = FALSE;
371 HWND parent, full;
373 TRACE("hwnd %04x, hrgnClip %04x, flags %08x\n",
374 hwnd, hrgnClip, (unsigned)flags);
376 if (!hwnd) hwnd = GetDesktopWindow();
377 if (!(full = WIN_IsCurrentProcess( hwnd )))
379 FIXME( "not supported yet on other process window %x\n", hwnd );
380 return 0;
382 hwnd = full;
383 if (!(wndPtr = WIN_GetPtr( hwnd ))) return 0;
385 /* fixup flags */
387 if (flags & (DCX_WINDOW | DCX_PARENTCLIP)) flags |= DCX_CACHE;
389 if (flags & DCX_USESTYLE)
391 flags &= ~( DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
393 if( wndPtr->dwStyle & WS_CLIPSIBLINGS )
394 flags |= DCX_CLIPSIBLINGS;
396 if ( !(flags & DCX_WINDOW) )
398 if (wndPtr->clsStyle & CS_PARENTDC) flags |= DCX_PARENTCLIP;
400 if (wndPtr->dwStyle & WS_CLIPCHILDREN &&
401 !(wndPtr->dwStyle & WS_MINIMIZE) ) flags |= DCX_CLIPCHILDREN;
402 if (!wndPtr->dce) flags |= DCX_CACHE;
406 if (flags & DCX_WINDOW) flags &= ~DCX_CLIPCHILDREN;
408 parent = GetAncestor( hwnd, GA_PARENT );
409 if (!parent || (parent == GetDesktopWindow()))
410 flags = (flags & ~DCX_PARENTCLIP) | DCX_CLIPSIBLINGS;
412 /* it seems parent clip is ignored when clipping siblings or children */
413 if (flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) flags &= ~DCX_PARENTCLIP;
415 if( flags & DCX_PARENTCLIP )
417 LONG parent_style = GetWindowLongW( parent, GWL_STYLE );
418 if( (wndPtr->dwStyle & WS_VISIBLE) && (parent_style & WS_VISIBLE) )
420 flags &= ~DCX_CLIPCHILDREN;
421 if (parent_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
425 /* find a suitable DCE */
427 dcxFlags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
428 DCX_CACHE | DCX_WINDOW);
430 if (flags & DCX_CACHE)
432 DCE* dceEmpty;
433 DCE* dceUnused;
435 dceEmpty = dceUnused = NULL;
437 /* Strategy: First, we attempt to find a non-empty but unused DCE with
438 * compatible flags. Next, we look for an empty entry. If the cache is
439 * full we have to purge one of the unused entries.
442 for (dce = firstDCE; (dce); dce = dce->next)
444 if ((dce->DCXflags & (DCX_CACHE | DCX_DCEBUSY)) == DCX_CACHE )
446 dceUnused = dce;
448 if (dce->DCXflags & DCX_DCEEMPTY)
449 dceEmpty = dce;
450 else
451 if ((dce->hwndCurrent == hwnd) &&
452 ((dce->DCXflags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
453 DCX_CACHE | DCX_WINDOW | DCX_PARENTCLIP)) == dcxFlags))
455 TRACE("\tfound valid %08x dce [%04x], flags %08x\n",
456 (unsigned)dce, hwnd, (unsigned)dcxFlags );
457 bUpdateVisRgn = FALSE;
458 bUpdateClipOrigin = TRUE;
459 break;
464 if (!dce) dce = (dceEmpty) ? dceEmpty : dceUnused;
466 /* if there's no dce empty or unused, allocate a new one */
467 if (!dce)
469 dce = DCE_AllocDCE( 0, DCE_CACHE_DC );
472 else
474 dce = wndPtr->dce;
475 if (dce && dce->hwndCurrent == hwnd)
477 TRACE("\tskipping hVisRgn update\n");
478 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
481 if (!dce)
483 hdc = 0;
484 goto END;
487 if (!(flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN))) hrgnClip = 0;
489 if (((flags ^ dce->DCXflags) & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
490 (dce->hClipRgn != hrgnClip))
492 /* if the extra clip region has changed, get rid of the old one */
493 DCE_DeleteClipRgn( dce );
496 dce->hwndCurrent = hwnd;
497 dce->hClipRgn = hrgnClip;
498 dce->DCXflags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
499 DCX_CACHE | DCX_WINDOW | DCX_WINDOWPAINT |
500 DCX_KEEPCLIPRGN | DCX_INTERSECTRGN | DCX_EXCLUDERGN);
501 dce->DCXflags |= DCX_DCEBUSY;
502 dce->DCXflags &= ~DCX_DCEDIRTY;
503 hdc = dce->hDC;
505 if (bUpdateVisRgn) SetHookFlags16( hdc, DCHF_INVALIDATEVISRGN ); /* force update */
507 if (!USER_Driver.pGetDC( hwnd, hdc, hrgnClip, flags )) hdc = 0;
509 TRACE("(%04x,%04x,0x%lx): returning %04x\n", hwnd, hrgnClip, flags, hdc);
510 END:
511 WIN_ReleasePtr(wndPtr);
512 return hdc;
516 /***********************************************************************
517 * GetDC (USER32.@)
518 * RETURNS
519 * :Handle to DC
520 * NULL: Failure
522 HDC WINAPI GetDC(
523 HWND hwnd /* [in] handle of window */
525 if (!hwnd)
526 return GetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
527 return GetDCEx( hwnd, 0, DCX_USESTYLE );
531 /***********************************************************************
532 * GetWindowDC (USER32.@)
534 HDC WINAPI GetWindowDC( HWND hwnd )
536 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
540 /***********************************************************************
541 * ReleaseDC (USER32.@)
543 * RETURNS
544 * 1: Success
545 * 0: Failure
547 INT WINAPI ReleaseDC(
548 HWND hwnd /* [in] Handle of window - ignored */,
549 HDC hdc /* [in] Handle of device context */
551 DCE * dce;
552 INT nRet = 0;
554 USER_Lock();
555 dce = firstDCE;
557 TRACE("%04x %04x\n", hwnd, hdc );
559 while (dce && (dce->hDC != hdc)) dce = dce->next;
561 if ( dce )
562 if ( dce->DCXflags & DCX_DCEBUSY )
563 nRet = DCE_ReleaseDC( dce );
565 USER_Unlock();
567 return nRet;
570 /***********************************************************************
571 * DCHook (USER.362)
573 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
575 BOOL16 WINAPI DCHook16( HDC16 hDC, WORD code, DWORD data, LPARAM lParam )
577 BOOL retv = TRUE;
578 DCE *dce = (DCE *)data;
580 TRACE("hDC = %04x, %i\n", hDC, code);
582 if (!dce) return 0;
583 assert(dce->hDC == hDC);
585 /* Grab the windows lock before doing anything else */
586 USER_Lock();
588 switch( code )
590 case DCHC_INVALIDVISRGN:
591 /* GDI code calls this when it detects that the
592 * DC is dirty (usually after SetHookFlags()). This
593 * means that we have to recompute the visible region.
595 if( dce->DCXflags & DCX_DCEBUSY )
597 /* Dirty bit has been cleared by caller, set it again so that
598 * pGetDC recomputes the visible region. */
599 SetHookFlags16( dce->hDC, DCHF_INVALIDATEVISRGN );
600 USER_Driver.pGetDC( dce->hwndCurrent, dce->hDC, dce->hClipRgn, dce->DCXflags );
602 else /* non-fatal but shouldn't happen */
603 WARN("DC is not in use!\n");
604 break;
606 case DCHC_DELETEDC:
608 * Windows will not let you delete a DC that is busy
609 * (between GetDC and ReleaseDC)
612 if ( dce->DCXflags & DCX_DCEBUSY )
614 WARN("Application trying to delete a busy DC\n");
615 retv = FALSE;
617 else DCE_FreeDCE( dce );
618 break;
620 default:
621 FIXME("unknown code\n");
624 USER_Unlock(); /* Release the wnd lock */
625 return retv;
629 /**********************************************************************
630 * WindowFromDC (USER32.@)
632 HWND WINAPI WindowFromDC( HDC hDC )
634 DCE *dce;
635 HWND hwnd;
637 USER_Lock();
638 dce = firstDCE;
640 while (dce && (dce->hDC != hDC)) dce = dce->next;
642 hwnd = dce ? dce->hwndCurrent : 0;
643 USER_Unlock();
645 return hwnd;
649 /***********************************************************************
650 * LockWindowUpdate (USER32.@)
652 BOOL WINAPI LockWindowUpdate( HWND hwnd )
654 FIXME("(%x), stub!\n",hwnd);
655 return TRUE;