Added a new flag to wrc, to be used to assess translations
[wine.git] / windows / dce.c
blob927c9de7ed98898bfeba52ca2b113596f59c6531
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 "user.h"
38 #include "wine/debug.h"
39 #include "windef.h"
40 #include "wingdi.h"
41 #include "wownt32.h"
42 #include "wine/winbase16.h"
43 #include "wine/winuser16.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(dc);
47 static DCE *firstDCE;
48 static HDC16 defaultDCstate;
50 static void DCE_DeleteClipRgn( DCE* );
51 static INT DCE_ReleaseDC( DCE* );
54 /***********************************************************************
55 * DCE_DumpCache
57 static void DCE_DumpCache(void)
59 DCE *dce;
61 USER_Lock();
62 dce = firstDCE;
64 DPRINTF("DCE:\n");
65 while( dce )
67 DPRINTF("\t[0x%08x] hWnd %p, 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" : "" );
71 dce = dce->next;
74 USER_Unlock();
77 /***********************************************************************
78 * DCE_AllocDCE
80 * Allocate a new DCE.
82 DCE *DCE_AllocDCE( HWND hWnd, DCE_TYPE type )
84 DCE * dce;
86 if (!(dce = HeapAlloc( GetProcessHeap(), 0, sizeof(DCE) ))) return NULL;
87 if (!(dce->hDC = CreateDCA( "DISPLAY", NULL, NULL, NULL )))
89 HeapFree( GetProcessHeap(), 0, dce );
90 return 0;
92 if (!defaultDCstate) defaultDCstate = GetDCState16( HDC_16(dce->hDC) );
94 /* store DCE handle in DC hook data field */
96 SetDCHook( dce->hDC, DCHook16, (DWORD)dce );
98 dce->hwndCurrent = WIN_GetFullHandle( hWnd );
99 dce->hClipRgn = 0;
101 if( type != DCE_CACHE_DC ) /* owned or class DC */
103 dce->DCXflags = DCX_DCEBUSY;
104 if( hWnd )
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( HDC_16(dce->hDC), DCHF_INVALIDATEVISRGN );
112 else dce->DCXflags = DCX_CACHE | DCX_DCEEMPTY;
114 USER_Lock();
115 dce->next = firstDCE;
116 firstDCE = dce;
117 USER_Unlock();
118 return dce;
122 /***********************************************************************
123 * DCE_FreeDCE
125 DCE* DCE_FreeDCE( DCE *dce )
127 DCE **ppDCE, *ret;
129 if (!dce) return NULL;
131 USER_Lock();
133 ppDCE = &firstDCE;
135 while (*ppDCE && (*ppDCE != dce)) ppDCE = &(*ppDCE)->next;
136 if (*ppDCE == dce) *ppDCE = dce->next;
137 ret = *ppDCE;
138 USER_Unlock();
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 );
147 return ret;
150 /***********************************************************************
151 * DCE_FreeWindowDCE
153 * Remove owned DCE and reset unreleased cache DCEs.
155 void DCE_FreeWindowDCE( HWND hwnd )
157 DCE *pDCE;
158 WND *pWnd = WIN_GetPtr( hwnd );
160 pDCE = firstDCE;
161 while( pDCE )
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 );
170 pWnd->dce = NULL;
171 continue;
173 else if( pDCE->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN) ) /* Class DCE*/
175 if (USER_Driver.pReleaseDC)
176 USER_Driver.pReleaseDC( pDCE->hwndCurrent, pDCE->hDC );
177 DCE_DeleteClipRgn( pDCE );
178 pDCE->hwndCurrent = 0;
181 else
183 if( pDCE->DCXflags & DCX_DCEBUSY ) /* shared cache DCE */
185 /* FIXME: AFAICS we are doing the right thing here so
186 * this should be a WARN. But this is best left as an ERR
187 * because the 'application error' is likely to come from
188 * another part of Wine (i.e. it's our fault after all).
189 * We should change this to WARN when Wine is more stable
190 * (for 1.0?).
192 ERR("[%p] GetDC() without ReleaseDC()!\n",hwnd);
193 DCE_ReleaseDC( pDCE );
196 if (pDCE->hwndCurrent && USER_Driver.pReleaseDC)
197 USER_Driver.pReleaseDC( pDCE->hwndCurrent, pDCE->hDC );
198 pDCE->DCXflags &= DCX_CACHE;
199 pDCE->DCXflags |= DCX_DCEEMPTY;
200 pDCE->hwndCurrent = 0;
203 pDCE = pDCE->next;
205 WIN_ReleasePtr( pWnd );
209 /***********************************************************************
210 * DCE_DeleteClipRgn
212 static void DCE_DeleteClipRgn( DCE* dce )
214 dce->DCXflags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN | DCX_WINDOWPAINT);
216 if( dce->DCXflags & DCX_KEEPCLIPRGN )
217 dce->DCXflags &= ~DCX_KEEPCLIPRGN;
218 else
219 if( dce->hClipRgn > (HRGN)1 )
220 DeleteObject( dce->hClipRgn );
222 dce->hClipRgn = 0;
224 /* make it dirty so that the vis rgn gets recomputed next time */
225 dce->DCXflags |= DCX_DCEDIRTY;
226 SetHookFlags16( HDC_16(dce->hDC), DCHF_INVALIDATEVISRGN );
230 /***********************************************************************
231 * DCE_ReleaseDC
233 static INT DCE_ReleaseDC( DCE* dce )
235 if ((dce->DCXflags & (DCX_DCEEMPTY | DCX_DCEBUSY)) != DCX_DCEBUSY) return 0;
237 /* restore previous visible region */
239 if ((dce->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
240 (dce->DCXflags & (DCX_CACHE | DCX_WINDOWPAINT)) )
241 DCE_DeleteClipRgn( dce );
243 if (dce->DCXflags & DCX_CACHE)
245 /* make the DC clean so that SetDCState doesn't try to update the vis rgn */
246 SetHookFlags16( HDC_16(dce->hDC), DCHF_VALIDATEVISRGN );
247 SetDCState16( HDC_16(dce->hDC), defaultDCstate );
248 dce->DCXflags &= ~DCX_DCEBUSY;
249 if (dce->DCXflags & DCX_DCEDIRTY)
251 /* don't keep around invalidated entries
252 * because SetDCState() disables hVisRgn updates
253 * by removing dirty bit. */
254 if (dce->hwndCurrent && USER_Driver.pReleaseDC)
255 USER_Driver.pReleaseDC( dce->hwndCurrent, dce->hDC );
256 dce->hwndCurrent = 0;
257 dce->DCXflags &= DCX_CACHE;
258 dce->DCXflags |= DCX_DCEEMPTY;
261 return 1;
265 /***********************************************************************
266 * DCE_InvalidateDCE
268 * It is called from SetWindowPos() and EVENT_MapNotify - we have to
269 * mark as dirty all busy DCEs for windows that have pWnd->parent as
270 * an ancestor and whose client rect intersects with specified update
271 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
272 * DCX_CLIPCHILDREN flag is set. */
273 BOOL DCE_InvalidateDCE(HWND hwnd, const RECT* pRectUpdate)
275 HWND hwndScope = GetAncestor( hwnd, GA_PARENT );
276 BOOL bRet = FALSE;
278 if( hwndScope )
280 DCE *dce;
282 TRACE("scope hwnd = %p, (%ld,%ld - %ld,%ld)\n",
283 hwndScope, pRectUpdate->left,pRectUpdate->top,
284 pRectUpdate->right,pRectUpdate->bottom);
285 if(TRACE_ON(dc))
286 DCE_DumpCache();
288 /* walk all DCEs and fixup non-empty entries */
290 for (dce = firstDCE; (dce); dce = dce->next)
292 if (dce->DCXflags & DCX_DCEEMPTY) continue;
293 if ((dce->hwndCurrent == hwndScope) && !(dce->DCXflags & DCX_CLIPCHILDREN))
294 continue; /* child window positions don't bother us */
296 /* check if DCE window is within the z-order scope */
298 if (hwndScope == dce->hwndCurrent || IsChild( hwndScope, dce->hwndCurrent ))
300 if (hwnd != dce->hwndCurrent)
302 /* check if the window rectangle intersects this DCE window */
303 RECT rect;
304 GetWindowRect( dce->hwndCurrent, &rect );
305 MapWindowPoints( 0, hwndScope, (POINT *)&rect, 2 );
306 if (!IntersectRect( &rect, &rect, pRectUpdate )) continue;
309 if( !(dce->DCXflags & DCX_DCEBUSY) )
311 /* Don't bother with visible regions of unused DCEs */
313 TRACE("\tpurged %p dce [%p]\n", dce, dce->hwndCurrent);
314 if (dce->hwndCurrent && USER_Driver.pReleaseDC)
315 USER_Driver.pReleaseDC( dce->hwndCurrent, dce->hDC );
316 dce->hwndCurrent = 0;
317 dce->DCXflags &= DCX_CACHE;
318 dce->DCXflags |= DCX_DCEEMPTY;
320 else
322 /* Set dirty bits in the hDC and DCE structs */
324 TRACE("\tfixed up %p dce [%p]\n", dce, dce->hwndCurrent);
325 dce->DCXflags |= DCX_DCEDIRTY;
326 SetHookFlags16( HDC_16(dce->hDC), DCHF_INVALIDATEVISRGN );
327 bRet = TRUE;
330 } /* dce list */
332 return bRet;
336 /***********************************************************************
337 * DCE_ExcludeRgn
339 * Translate given region from the wnd client to the DC coordinates
340 * and add it to the clipping region.
342 INT DCE_ExcludeRgn( HDC hDC, HWND hwnd, HRGN hRgn )
344 POINT pt = {0, 0};
345 DCE *dce = firstDCE;
347 while (dce && (dce->hDC != hDC)) dce = dce->next;
348 if (!dce) return ERROR;
350 MapWindowPoints( hwnd, dce->hwndCurrent, &pt, 1);
351 if( dce->DCXflags & DCX_WINDOW )
353 WND *wnd = WIN_FindWndPtr(dce->hwndCurrent);
354 pt.x += wnd->rectClient.left - wnd->rectWindow.left;
355 pt.y += wnd->rectClient.top - wnd->rectWindow.top;
356 WIN_ReleaseWndPtr(wnd);
358 OffsetRgn(hRgn, pt.x, pt.y);
360 return ExtSelectClipRgn( hDC, hRgn, RGN_DIFF );
364 /***********************************************************************
365 * GetDCEx (USER32.@)
367 * Unimplemented flags: DCX_LOCKWINDOWUPDATE
369 * FIXME: Full support for hrgnClip == 1 (alias for entire window).
371 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
373 HDC hdc = 0;
374 DCE * dce;
375 WND * wndPtr;
376 DWORD dcxFlags = 0;
377 BOOL bUpdateVisRgn = TRUE;
378 BOOL bUpdateClipOrigin = FALSE;
379 HWND parent, full;
381 TRACE("hwnd %p, hrgnClip %p, flags %08lx\n", hwnd, hrgnClip, flags);
383 if (!hwnd) hwnd = GetDesktopWindow();
384 if (!(full = WIN_IsCurrentProcess( hwnd )))
386 FIXME( "not supported yet on other process window %p\n", hwnd );
387 return 0;
389 hwnd = full;
390 if (!(wndPtr = WIN_GetPtr( hwnd ))) return 0;
392 /* fixup flags */
394 if (flags & (DCX_WINDOW | DCX_PARENTCLIP)) flags |= DCX_CACHE;
396 if (flags & DCX_USESTYLE)
398 flags &= ~( DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
400 if( wndPtr->dwStyle & WS_CLIPSIBLINGS )
401 flags |= DCX_CLIPSIBLINGS;
403 if ( !(flags & DCX_WINDOW) )
405 if (wndPtr->clsStyle & CS_PARENTDC) flags |= DCX_PARENTCLIP;
407 if (wndPtr->dwStyle & WS_CLIPCHILDREN &&
408 !(wndPtr->dwStyle & WS_MINIMIZE) ) flags |= DCX_CLIPCHILDREN;
409 if (!wndPtr->dce) flags |= DCX_CACHE;
413 if (flags & DCX_WINDOW) flags &= ~DCX_CLIPCHILDREN;
415 parent = GetAncestor( hwnd, GA_PARENT );
416 if (!parent || (parent == GetDesktopWindow()))
417 flags = (flags & ~DCX_PARENTCLIP) | DCX_CLIPSIBLINGS;
419 /* it seems parent clip is ignored when clipping siblings or children */
420 if (flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) flags &= ~DCX_PARENTCLIP;
422 if( flags & DCX_PARENTCLIP )
424 LONG parent_style = GetWindowLongW( parent, GWL_STYLE );
425 if( (wndPtr->dwStyle & WS_VISIBLE) && (parent_style & WS_VISIBLE) )
427 flags &= ~DCX_CLIPCHILDREN;
428 if (parent_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
432 /* find a suitable DCE */
434 dcxFlags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
435 DCX_CACHE | DCX_WINDOW);
437 if (flags & DCX_CACHE)
439 DCE* dceEmpty;
440 DCE* dceUnused;
442 dceEmpty = dceUnused = NULL;
444 /* Strategy: First, we attempt to find a non-empty but unused DCE with
445 * compatible flags. Next, we look for an empty entry. If the cache is
446 * full we have to purge one of the unused entries.
449 for (dce = firstDCE; (dce); dce = dce->next)
451 if ((dce->DCXflags & (DCX_CACHE | DCX_DCEBUSY)) == DCX_CACHE )
453 dceUnused = dce;
455 if (dce->DCXflags & DCX_DCEEMPTY)
456 dceEmpty = dce;
457 else
458 if ((dce->hwndCurrent == hwnd) &&
459 ((dce->DCXflags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
460 DCX_CACHE | DCX_WINDOW | DCX_PARENTCLIP)) == dcxFlags))
462 TRACE("\tfound valid %p dce [%p], flags %08lx\n",
463 dce, hwnd, dcxFlags );
464 bUpdateVisRgn = FALSE;
465 bUpdateClipOrigin = TRUE;
466 break;
471 if (!dce) dce = (dceEmpty) ? dceEmpty : dceUnused;
473 /* if there's no dce empty or unused, allocate a new one */
474 if (!dce)
476 dce = DCE_AllocDCE( 0, DCE_CACHE_DC );
479 else
481 dce = wndPtr->dce;
482 if (dce && dce->hwndCurrent == hwnd)
484 TRACE("\tskipping hVisRgn update\n");
485 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
488 if (!dce)
490 hdc = 0;
491 goto END;
494 if (!(flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN))) hrgnClip = 0;
496 if (((flags ^ dce->DCXflags) & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
497 (dce->hClipRgn != hrgnClip))
499 /* if the extra clip region has changed, get rid of the old one */
500 DCE_DeleteClipRgn( dce );
503 dce->hwndCurrent = hwnd;
504 dce->hClipRgn = hrgnClip;
505 dce->DCXflags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
506 DCX_CACHE | DCX_WINDOW | DCX_WINDOWPAINT |
507 DCX_KEEPCLIPRGN | DCX_INTERSECTRGN | DCX_EXCLUDERGN);
508 dce->DCXflags |= DCX_DCEBUSY;
509 dce->DCXflags &= ~DCX_DCEDIRTY;
510 hdc = dce->hDC;
512 if (bUpdateVisRgn) SetHookFlags16( HDC_16(hdc), DCHF_INVALIDATEVISRGN ); /* force update */
514 if (!USER_Driver.pGetDC( hwnd, hdc, hrgnClip, flags )) hdc = 0;
516 TRACE("(%p,%p,0x%lx): returning %p\n", hwnd, hrgnClip, flags, hdc);
517 END:
518 WIN_ReleasePtr(wndPtr);
519 return hdc;
523 /***********************************************************************
524 * GetDC (USER32.@)
526 * Get a device context.
528 * RETURNS
529 * Success: Handle to the device context
530 * Failure: NULL.
532 HDC WINAPI GetDC(
533 HWND hwnd /* [in] handle of window - may be NULL */
535 if (!hwnd)
536 return GetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
537 return GetDCEx( hwnd, 0, DCX_USESTYLE );
541 /***********************************************************************
542 * GetWindowDC (USER32.@)
544 HDC WINAPI GetWindowDC( HWND hwnd )
546 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
550 /***********************************************************************
551 * ReleaseDC (USER32.@)
553 * Release a device context.
555 * RETURNS
556 * Success: Non-zero. Resources used by hdc are released.
557 * Failure: 0.
559 INT WINAPI ReleaseDC(
560 HWND hwnd, /* [in] Handle of window - ignored */
561 HDC hdc /* [in] Handle of device context */
563 DCE * dce;
564 INT nRet = 0;
566 USER_Lock();
567 dce = firstDCE;
569 TRACE("%p %p\n", hwnd, hdc );
571 while (dce && (dce->hDC != hdc)) dce = dce->next;
573 if ( dce )
574 if ( dce->DCXflags & DCX_DCEBUSY )
575 nRet = DCE_ReleaseDC( dce );
577 USER_Unlock();
579 return nRet;
582 /***********************************************************************
583 * DCHook (USER.362)
585 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
587 BOOL16 WINAPI DCHook16( HDC16 hDC, WORD code, DWORD data, LPARAM lParam )
589 BOOL retv = TRUE;
590 DCE *dce = (DCE *)data;
592 TRACE("hDC = %04x, %i\n", hDC, code);
594 if (!dce) return 0;
595 assert( HDC_16(dce->hDC) == hDC );
597 /* Grab the windows lock before doing anything else */
598 USER_Lock();
600 switch( code )
602 case DCHC_INVALIDVISRGN:
603 /* GDI code calls this when it detects that the
604 * DC is dirty (usually after SetHookFlags()). This
605 * means that we have to recompute the visible region.
607 if( dce->DCXflags & DCX_DCEBUSY )
609 /* Dirty bit has been cleared by caller, set it again so that
610 * pGetDC recomputes the visible region. */
611 SetHookFlags16( hDC, DCHF_INVALIDATEVISRGN );
612 USER_Driver.pGetDC( dce->hwndCurrent, dce->hDC, dce->hClipRgn, dce->DCXflags );
614 else /* non-fatal but shouldn't happen */
615 WARN("DC is not in use!\n");
616 break;
618 case DCHC_DELETEDC:
620 * Windows will not let you delete a DC that is busy
621 * (between GetDC and ReleaseDC)
624 if ( dce->DCXflags & DCX_DCEBUSY )
626 WARN("Application trying to delete a busy DC\n");
627 retv = FALSE;
629 else DCE_FreeDCE( dce );
630 break;
632 default:
633 FIXME("unknown code\n");
636 USER_Unlock(); /* Release the wnd lock */
637 return retv;
641 /**********************************************************************
642 * WindowFromDC (USER32.@)
644 HWND WINAPI WindowFromDC( HDC hDC )
646 DCE *dce;
647 HWND hwnd;
649 USER_Lock();
650 dce = firstDCE;
652 while (dce && (dce->hDC != hDC)) dce = dce->next;
654 hwnd = dce ? dce->hwndCurrent : 0;
655 USER_Unlock();
657 return hwnd;
661 /***********************************************************************
662 * LockWindowUpdate (USER32.@)
664 BOOL WINAPI LockWindowUpdate( HWND hwnd )
666 static HWND lockedWnd;
668 FIXME("(%p), partial stub!\n",hwnd);
670 USER_Lock();
671 if (lockedWnd)
673 if (!hwnd)
675 /* Unlock lockedWnd */
676 /* FIXME: Do something */
678 else
680 /* Attempted to lock a second window */
681 /* Return FALSE and do nothing */
682 USER_Unlock();
683 return FALSE;
686 lockedWnd = hwnd;
687 USER_Unlock();
688 return TRUE;