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.
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
39 #include "wine/debug.h"
42 #include "wine/winbase16.h"
43 #include "wine/winuser16.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(dc
);
48 static HDC defaultDCstate
;
50 static void DCE_DeleteClipRgn( DCE
* );
51 static INT
DCE_ReleaseDC( DCE
* );
54 /***********************************************************************
57 static void DCE_DumpCache(void)
67 DPRINTF("\t[0x%08x] hWnd 0x%04x, dcx %08x, %s %s\n",
68 (unsigned)dce
, dce
->hwndCurrent
, (unsigned)dce
->DCXflags
,
69 (dce
->DCXflags
& DCX_CACHE
) ? "Cache" : "Owned",
70 (dce
->DCXflags
& DCX_DCEBUSY
) ? "InUse" : "" );
77 /***********************************************************************
82 DCE
*DCE_AllocDCE( HWND hWnd
, DCE_TYPE type
)
86 if (!(dce
= HeapAlloc( GetProcessHeap(), 0, sizeof(DCE
) ))) return NULL
;
87 if (!(dce
->hDC
= CreateDCA( "DISPLAY", NULL
, NULL
, NULL
)))
89 HeapFree( GetProcessHeap(), 0, dce
);
92 if (!defaultDCstate
) defaultDCstate
= GetDCState16( dce
->hDC
);
94 /* store DCE handle in DC hook data field */
96 SetDCHook( dce
->hDC
, DCHook16
, (DWORD
)dce
);
98 dce
->hwndCurrent
= WIN_GetFullHandle( hWnd
);
101 if( type
!= DCE_CACHE_DC
) /* owned or class DC */
103 dce
->DCXflags
= DCX_DCEBUSY
;
106 LONG style
= GetWindowLongW( hWnd
, GWL_STYLE
);
107 if (style
& WS_CLIPCHILDREN
) dce
->DCXflags
|= DCX_CLIPCHILDREN
;
108 if (style
& WS_CLIPSIBLINGS
) dce
->DCXflags
|= DCX_CLIPSIBLINGS
;
110 SetHookFlags16(dce
->hDC
,DCHF_INVALIDATEVISRGN
);
112 else dce
->DCXflags
= DCX_CACHE
| DCX_DCEEMPTY
;
115 dce
->next
= firstDCE
;
122 /***********************************************************************
125 DCE
* DCE_FreeDCE( DCE
*dce
)
129 if (!dce
) return NULL
;
135 while (*ppDCE
&& (*ppDCE
!= dce
)) ppDCE
= &(*ppDCE
)->next
;
136 if (*ppDCE
== dce
) *ppDCE
= dce
->next
;
140 SetDCHook(dce
->hDC
, NULL
, 0L);
142 DeleteDC( dce
->hDC
);
143 if( dce
->hClipRgn
&& !(dce
->DCXflags
& DCX_KEEPCLIPRGN
) )
144 DeleteObject(dce
->hClipRgn
);
145 HeapFree( GetProcessHeap(), 0, dce
);
150 /***********************************************************************
153 * Remove owned DCE and reset unreleased cache DCEs.
155 void DCE_FreeWindowDCE( HWND hwnd
)
158 WND
*pWnd
= WIN_GetPtr( hwnd
);
163 if( pDCE
->hwndCurrent
== hwnd
)
165 if( pDCE
== pWnd
->dce
) /* owned or Class DCE*/
167 if (pWnd
->clsStyle
& CS_OWNDC
) /* owned DCE*/
169 pDCE
= DCE_FreeDCE( pDCE
);
173 else if( pDCE
->DCXflags
& (DCX_INTERSECTRGN
| DCX_EXCLUDERGN
) ) /* Class DCE*/
175 DCE_DeleteClipRgn( pDCE
);
176 pDCE
->hwndCurrent
= 0;
181 if( pDCE
->DCXflags
& DCX_DCEBUSY
) /* shared cache DCE */
183 /* FIXME: AFAICS we are doing the right thing here so
184 * this should be a WARN. But this is best left as an ERR
185 * because the 'application error' is likely to come from
186 * another part of Wine (i.e. it's our fault after all).
187 * We should change this to WARN when Wine is more stable
190 ERR("[%08x] GetDC() without ReleaseDC()!\n",hwnd
);
191 DCE_ReleaseDC( pDCE
);
194 pDCE
->DCXflags
&= DCX_CACHE
;
195 pDCE
->DCXflags
|= DCX_DCEEMPTY
;
196 pDCE
->hwndCurrent
= 0;
201 WIN_ReleasePtr( pWnd
);
205 /***********************************************************************
208 static void DCE_DeleteClipRgn( DCE
* dce
)
210 dce
->DCXflags
&= ~(DCX_EXCLUDERGN
| DCX_INTERSECTRGN
| DCX_WINDOWPAINT
);
212 if( dce
->DCXflags
& DCX_KEEPCLIPRGN
)
213 dce
->DCXflags
&= ~DCX_KEEPCLIPRGN
;
215 if( dce
->hClipRgn
> 1 )
216 DeleteObject( dce
->hClipRgn
);
220 /* make it dirty so that the vis rgn gets recomputed next time */
221 dce
->DCXflags
|= DCX_DCEDIRTY
;
222 SetHookFlags16( dce
->hDC
, DCHF_INVALIDATEVISRGN
);
226 /***********************************************************************
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( dce
->hDC
, DCHF_VALIDATEVISRGN
);
243 SetDCState16( 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. */
251 dce
->hwndCurrent
= 0;
252 dce
->DCXflags
&= DCX_CACHE
;
253 dce
->DCXflags
|= DCX_DCEEMPTY
;
260 /***********************************************************************
263 * It is called from SetWindowPos() and EVENT_MapNotify - we have to
264 * mark as dirty all busy DCEs for windows that have pWnd->parent as
265 * an ancestor and whose client rect intersects with specified update
266 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
267 * DCX_CLIPCHILDREN flag is set. */
268 BOOL
DCE_InvalidateDCE(HWND hwnd
, const RECT
* pRectUpdate
)
270 HWND hwndScope
= GetAncestor( hwnd
, GA_PARENT
);
277 TRACE("scope hwnd = %04x, (%i,%i - %i,%i)\n",
278 hwndScope
, pRectUpdate
->left
,pRectUpdate
->top
,
279 pRectUpdate
->right
,pRectUpdate
->bottom
);
283 /* walk all DCEs and fixup non-empty entries */
285 for (dce
= firstDCE
; (dce
); dce
= dce
->next
)
287 if (dce
->DCXflags
& DCX_DCEEMPTY
) continue;
288 if ((dce
->hwndCurrent
== hwndScope
) && !(dce
->DCXflags
& DCX_CLIPCHILDREN
))
289 continue; /* child window positions don't bother us */
291 /* check if DCE window is within the z-order scope */
293 if (hwndScope
== dce
->hwndCurrent
|| IsChild( hwndScope
, dce
->hwndCurrent
))
295 if (hwnd
!= dce
->hwndCurrent
)
297 /* check if the window rectangle intersects this DCE window */
299 GetWindowRect( dce
->hwndCurrent
, &rect
);
300 MapWindowPoints( 0, hwndScope
, (POINT
*)&rect
, 2 );
301 if (!IntersectRect( &rect
, &rect
, pRectUpdate
)) continue;
304 if( !(dce
->DCXflags
& DCX_DCEBUSY
) )
306 /* Don't bother with visible regions of unused DCEs */
308 TRACE("\tpurged %p dce [%04x]\n", dce
, dce
->hwndCurrent
);
309 dce
->hwndCurrent
= 0;
310 dce
->DCXflags
&= DCX_CACHE
;
311 dce
->DCXflags
|= DCX_DCEEMPTY
;
315 /* Set dirty bits in the hDC and DCE structs */
317 TRACE("\tfixed up %p dce [%04x]\n", dce
, dce
->hwndCurrent
);
318 dce
->DCXflags
|= DCX_DCEDIRTY
;
319 SetHookFlags16(dce
->hDC
, DCHF_INVALIDATEVISRGN
);
329 /***********************************************************************
332 * Translate given region from the wnd client to the DC coordinates
333 * and add it to the clipping region.
335 INT
DCE_ExcludeRgn( HDC hDC
, HWND hwnd
, HRGN hRgn
)
340 while (dce
&& (dce
->hDC
!= hDC
)) dce
= dce
->next
;
341 if (!dce
) return ERROR
;
343 MapWindowPoints( hwnd
, dce
->hwndCurrent
, &pt
, 1);
344 if( dce
->DCXflags
& DCX_WINDOW
)
346 WND
*wnd
= WIN_FindWndPtr(dce
->hwndCurrent
);
347 pt
.x
+= wnd
->rectClient
.left
- wnd
->rectWindow
.left
;
348 pt
.y
+= wnd
->rectClient
.top
- wnd
->rectWindow
.top
;
349 WIN_ReleaseWndPtr(wnd
);
351 OffsetRgn(hRgn
, pt
.x
, pt
.y
);
353 return ExtSelectClipRgn( hDC
, hRgn
, RGN_DIFF
);
357 /***********************************************************************
360 * Unimplemented flags: DCX_LOCKWINDOWUPDATE
362 * FIXME: Full support for hrgnClip == 1 (alias for entire window).
364 HDC WINAPI
GetDCEx( HWND hwnd
, HRGN hrgnClip
, DWORD flags
)
370 BOOL bUpdateVisRgn
= TRUE
;
371 BOOL bUpdateClipOrigin
= FALSE
;
374 TRACE("hwnd %04x, hrgnClip %04x, flags %08x\n",
375 hwnd
, hrgnClip
, (unsigned)flags
);
377 if (!hwnd
) hwnd
= GetDesktopWindow();
378 if (!(full
= WIN_IsCurrentProcess( hwnd
)))
380 FIXME( "not supported yet on other process window %x\n", hwnd
);
384 if (!(wndPtr
= WIN_GetPtr( hwnd
))) return 0;
388 if (flags
& (DCX_WINDOW
| DCX_PARENTCLIP
)) flags
|= DCX_CACHE
;
390 if (flags
& DCX_USESTYLE
)
392 flags
&= ~( DCX_CLIPCHILDREN
| DCX_CLIPSIBLINGS
| DCX_PARENTCLIP
);
394 if( wndPtr
->dwStyle
& WS_CLIPSIBLINGS
)
395 flags
|= DCX_CLIPSIBLINGS
;
397 if ( !(flags
& DCX_WINDOW
) )
399 if (wndPtr
->clsStyle
& CS_PARENTDC
) flags
|= DCX_PARENTCLIP
;
401 if (wndPtr
->dwStyle
& WS_CLIPCHILDREN
&&
402 !(wndPtr
->dwStyle
& WS_MINIMIZE
) ) flags
|= DCX_CLIPCHILDREN
;
403 if (!wndPtr
->dce
) flags
|= DCX_CACHE
;
407 if (flags
& DCX_WINDOW
) flags
&= ~DCX_CLIPCHILDREN
;
409 parent
= GetAncestor( hwnd
, GA_PARENT
);
410 if (!parent
|| (parent
== GetDesktopWindow()))
411 flags
= (flags
& ~DCX_PARENTCLIP
) | DCX_CLIPSIBLINGS
;
413 /* it seems parent clip is ignored when clipping siblings or children */
414 if (flags
& (DCX_CLIPSIBLINGS
| DCX_CLIPCHILDREN
)) flags
&= ~DCX_PARENTCLIP
;
416 if( flags
& DCX_PARENTCLIP
)
418 LONG parent_style
= GetWindowLongW( parent
, GWL_STYLE
);
419 if( (wndPtr
->dwStyle
& WS_VISIBLE
) && (parent_style
& WS_VISIBLE
) )
421 flags
&= ~DCX_CLIPCHILDREN
;
422 if (parent_style
& WS_CLIPSIBLINGS
) flags
|= DCX_CLIPSIBLINGS
;
426 /* find a suitable DCE */
428 dcxFlags
= flags
& (DCX_PARENTCLIP
| DCX_CLIPSIBLINGS
| DCX_CLIPCHILDREN
|
429 DCX_CACHE
| DCX_WINDOW
);
431 if (flags
& DCX_CACHE
)
436 dceEmpty
= dceUnused
= NULL
;
438 /* Strategy: First, we attempt to find a non-empty but unused DCE with
439 * compatible flags. Next, we look for an empty entry. If the cache is
440 * full we have to purge one of the unused entries.
443 for (dce
= firstDCE
; (dce
); dce
= dce
->next
)
445 if ((dce
->DCXflags
& (DCX_CACHE
| DCX_DCEBUSY
)) == DCX_CACHE
)
449 if (dce
->DCXflags
& DCX_DCEEMPTY
)
452 if ((dce
->hwndCurrent
== hwnd
) &&
453 ((dce
->DCXflags
& (DCX_CLIPSIBLINGS
| DCX_CLIPCHILDREN
|
454 DCX_CACHE
| DCX_WINDOW
| DCX_PARENTCLIP
)) == dcxFlags
))
456 TRACE("\tfound valid %08x dce [%04x], flags %08x\n",
457 (unsigned)dce
, hwnd
, (unsigned)dcxFlags
);
458 bUpdateVisRgn
= FALSE
;
459 bUpdateClipOrigin
= TRUE
;
465 if (!dce
) dce
= (dceEmpty
) ? dceEmpty
: dceUnused
;
467 /* if there's no dce empty or unused, allocate a new one */
470 dce
= DCE_AllocDCE( 0, DCE_CACHE_DC
);
476 if (dce
&& dce
->hwndCurrent
== hwnd
)
478 TRACE("\tskipping hVisRgn update\n");
479 bUpdateVisRgn
= FALSE
; /* updated automatically, via DCHook() */
488 if (!(flags
& (DCX_INTERSECTRGN
| DCX_EXCLUDERGN
))) hrgnClip
= 0;
490 if (((flags
^ dce
->DCXflags
) & (DCX_INTERSECTRGN
| DCX_EXCLUDERGN
)) &&
491 (dce
->hClipRgn
!= hrgnClip
))
493 /* if the extra clip region has changed, get rid of the old one */
494 DCE_DeleteClipRgn( dce
);
497 dce
->hwndCurrent
= hwnd
;
498 dce
->hClipRgn
= hrgnClip
;
499 dce
->DCXflags
= flags
& (DCX_PARENTCLIP
| DCX_CLIPSIBLINGS
| DCX_CLIPCHILDREN
|
500 DCX_CACHE
| DCX_WINDOW
| DCX_WINDOWPAINT
|
501 DCX_KEEPCLIPRGN
| DCX_INTERSECTRGN
| DCX_EXCLUDERGN
);
502 dce
->DCXflags
|= DCX_DCEBUSY
;
503 dce
->DCXflags
&= ~DCX_DCEDIRTY
;
506 if (bUpdateVisRgn
) SetHookFlags16( hdc
, DCHF_INVALIDATEVISRGN
); /* force update */
508 if (!USER_Driver
.pGetDC( hwnd
, hdc
, hrgnClip
, flags
)) hdc
= 0;
510 TRACE("(%04x,%04x,0x%lx): returning %04x\n", hwnd
, hrgnClip
, flags
, hdc
);
512 WIN_ReleasePtr(wndPtr
);
517 /***********************************************************************
524 HWND hwnd
/* [in] handle of window */
527 return GetDCEx( 0, 0, DCX_CACHE
| DCX_WINDOW
);
528 return GetDCEx( hwnd
, 0, DCX_USESTYLE
);
532 /***********************************************************************
533 * GetWindowDC (USER32.@)
535 HDC WINAPI
GetWindowDC( HWND hwnd
)
537 return GetDCEx( hwnd
, 0, DCX_USESTYLE
| DCX_WINDOW
);
541 /***********************************************************************
542 * ReleaseDC (USER32.@)
548 INT WINAPI
ReleaseDC(
549 HWND hwnd
/* [in] Handle of window - ignored */,
550 HDC hdc
/* [in] Handle of device context */
558 TRACE("%04x %04x\n", hwnd
, hdc
);
560 while (dce
&& (dce
->hDC
!= hdc
)) dce
= dce
->next
;
563 if ( dce
->DCXflags
& DCX_DCEBUSY
)
564 nRet
= DCE_ReleaseDC( dce
);
571 /***********************************************************************
574 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
576 BOOL16 WINAPI
DCHook16( HDC16 hDC
, WORD code
, DWORD data
, LPARAM lParam
)
579 DCE
*dce
= (DCE
*)data
;
581 TRACE("hDC = %04x, %i\n", hDC
, code
);
584 assert(dce
->hDC
== hDC
);
586 /* Grab the windows lock before doing anything else */
591 case DCHC_INVALIDVISRGN
:
592 /* GDI code calls this when it detects that the
593 * DC is dirty (usually after SetHookFlags()). This
594 * means that we have to recompute the visible region.
596 if( dce
->DCXflags
& DCX_DCEBUSY
)
598 /* Dirty bit has been cleared by caller, set it again so that
599 * pGetDC recomputes the visible region. */
600 SetHookFlags16( dce
->hDC
, DCHF_INVALIDATEVISRGN
);
601 USER_Driver
.pGetDC( dce
->hwndCurrent
, dce
->hDC
, dce
->hClipRgn
, dce
->DCXflags
);
603 else /* non-fatal but shouldn't happen */
604 WARN("DC is not in use!\n");
609 * Windows will not let you delete a DC that is busy
610 * (between GetDC and ReleaseDC)
613 if ( dce
->DCXflags
& DCX_DCEBUSY
)
615 WARN("Application trying to delete a busy DC\n");
618 else DCE_FreeDCE( dce
);
622 FIXME("unknown code\n");
625 USER_Unlock(); /* Release the wnd lock */
630 /**********************************************************************
631 * WindowFromDC (USER32.@)
633 HWND WINAPI
WindowFromDC( HDC hDC
)
641 while (dce
&& (dce
->hDC
!= hDC
)) dce
= dce
->next
;
643 hwnd
= dce
? dce
->hwndCurrent
: 0;
650 /***********************************************************************
651 * LockWindowUpdate (USER32.@)
653 BOOL WINAPI
LockWindowUpdate( HWND hwnd
)
655 FIXME("(%x), stub!\n",hwnd
);