Update scroll info on WM_SETTEXT.
[wine.git] / windows / dce.c
blobfa8fdfa411b76d24e3ab845bf0b5312bee36689f
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;
48 static void DCE_DeleteClipRgn( DCE* );
49 static INT DCE_ReleaseDC( DCE* );
52 /***********************************************************************
53 * DCE_DumpCache
55 static void DCE_DumpCache(void)
57 DCE *dce;
59 USER_Lock();
61 for(dce = firstDCE; dce; dce = dce->next)
63 TRACE("\t[0x%08x] hWnd %p, dcx %08x, %s %s\n",
64 (unsigned)dce, dce->hwndCurrent, (unsigned)dce->DCXflags,
65 (dce->DCXflags & DCX_CACHE) ? "Cache" : "Owned",
66 (dce->DCXflags & DCX_DCEBUSY) ? "InUse" : "" );
69 USER_Unlock();
72 /***********************************************************************
73 * DCE_AllocDCE
75 * Allocate a new DCE.
77 DCE *DCE_AllocDCE( HWND hWnd, DCE_TYPE type )
79 static const WCHAR szDisplayW[] = { 'D','I','S','P','L','A','Y','\0' };
80 DCE * dce;
82 TRACE("(%p,%d)\n", hWnd, type);
84 if (!(dce = HeapAlloc( GetProcessHeap(), 0, sizeof(DCE) ))) return NULL;
85 if (!(dce->hDC = CreateDCW( szDisplayW, NULL, NULL, NULL )))
87 HeapFree( GetProcessHeap(), 0, dce );
88 return 0;
90 SaveDC( dce->hDC );
92 /* store DCE handle in DC hook data field */
94 SetDCHook( dce->hDC, DCHook16, (DWORD)dce );
96 dce->hwndCurrent = hWnd;
97 dce->hClipRgn = 0;
99 if( type != DCE_CACHE_DC ) /* owned or class DC */
101 dce->DCXflags = DCX_DCEBUSY;
102 if( hWnd )
104 LONG style = GetWindowLongW( hWnd, GWL_STYLE );
105 if (style & WS_CLIPCHILDREN) dce->DCXflags |= DCX_CLIPCHILDREN;
106 if (style & WS_CLIPSIBLINGS) dce->DCXflags |= DCX_CLIPSIBLINGS;
108 SetHookFlags16( HDC_16(dce->hDC), DCHF_INVALIDATEVISRGN );
110 else dce->DCXflags = DCX_CACHE | DCX_DCEEMPTY;
112 USER_Lock();
113 dce->next = firstDCE;
114 firstDCE = dce;
115 USER_Unlock();
116 return dce;
120 /***********************************************************************
121 * DCE_FreeDCE
123 DCE* DCE_FreeDCE( DCE *dce )
125 DCE **ppDCE, *ret;
127 if (!dce) return NULL;
129 USER_Lock();
131 ppDCE = &firstDCE;
133 while (*ppDCE && (*ppDCE != dce)) ppDCE = &(*ppDCE)->next;
134 if (*ppDCE == dce) *ppDCE = dce->next;
135 ret = *ppDCE;
136 USER_Unlock();
138 SetDCHook(dce->hDC, NULL, 0L);
140 DeleteDC( dce->hDC );
141 if (dce->hClipRgn) DeleteObject(dce->hClipRgn);
142 HeapFree( GetProcessHeap(), 0, dce );
144 return ret;
147 /***********************************************************************
148 * DCE_FreeWindowDCE
150 * Remove owned DCE and reset unreleased cache DCEs.
152 void DCE_FreeWindowDCE( HWND hwnd )
154 DCE *pDCE;
155 WND *pWnd = WIN_GetPtr( hwnd );
157 pDCE = firstDCE;
158 while( pDCE )
160 if( pDCE->hwndCurrent == hwnd )
162 if( pDCE == pWnd->dce ) /* owned or Class DCE*/
164 if (pWnd->clsStyle & CS_OWNDC) /* owned DCE*/
166 pDCE = DCE_FreeDCE( pDCE );
167 pWnd->dce = NULL;
168 continue;
170 else if( pDCE->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN) ) /* Class DCE*/
172 if (USER_Driver.pReleaseDC)
173 USER_Driver.pReleaseDC( pDCE->hwndCurrent, pDCE->hDC );
174 DCE_DeleteClipRgn( pDCE );
175 pDCE->hwndCurrent = 0;
178 else
180 if( pDCE->DCXflags & DCX_DCEBUSY ) /* shared cache DCE */
182 /* FIXME: AFAICS we are doing the right thing here so
183 * this should be a WARN. But this is best left as an ERR
184 * because the 'application error' is likely to come from
185 * another part of Wine (i.e. it's our fault after all).
186 * We should change this to WARN when Wine is more stable
187 * (for 1.0?).
189 ERR("[%p] GetDC() without ReleaseDC()!\n",hwnd);
190 DCE_ReleaseDC( pDCE );
193 if (pDCE->hwndCurrent && USER_Driver.pReleaseDC)
194 USER_Driver.pReleaseDC( pDCE->hwndCurrent, pDCE->hDC );
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->hClipRgn) DeleteObject( dce->hClipRgn );
214 dce->hClipRgn = 0;
216 /* make it dirty so that the vis rgn gets recomputed next time */
217 dce->DCXflags |= DCX_DCEDIRTY;
218 SetHookFlags16( HDC_16(dce->hDC), DCHF_INVALIDATEVISRGN );
222 /***********************************************************************
223 * DCE_ReleaseDC
225 static INT DCE_ReleaseDC( DCE* dce )
227 if ((dce->DCXflags & (DCX_DCEEMPTY | DCX_DCEBUSY)) != DCX_DCEBUSY) return 0;
229 /* restore previous visible region */
231 if ((dce->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
232 (dce->DCXflags & (DCX_CACHE | DCX_WINDOWPAINT)) )
233 DCE_DeleteClipRgn( dce );
235 if (dce->DCXflags & DCX_CACHE)
237 /* make the DC clean so that RestoreDC doesn't try to update the vis rgn */
238 SetHookFlags16( HDC_16(dce->hDC), DCHF_VALIDATEVISRGN );
239 RestoreDC( dce->hDC, 1 ); /* initial save level is always 1 */
240 SaveDC( dce->hDC ); /* save the state again for next time */
241 dce->DCXflags &= ~DCX_DCEBUSY;
242 if (dce->DCXflags & DCX_DCEDIRTY)
244 /* don't keep around invalidated entries
245 * because RestoreDC() disables hVisRgn updates
246 * by removing dirty bit. */
247 if (dce->hwndCurrent && USER_Driver.pReleaseDC)
248 USER_Driver.pReleaseDC( dce->hwndCurrent, dce->hDC );
249 dce->hwndCurrent = 0;
250 dce->DCXflags &= DCX_CACHE;
251 dce->DCXflags |= DCX_DCEEMPTY;
254 return 1;
258 /***********************************************************************
259 * DCE_InvalidateDCE
261 * It is called from SetWindowPos() and EVENT_MapNotify - we have to
262 * mark as dirty all busy DCEs for windows that have pWnd->parent as
263 * an ancestor and whose client rect intersects with specified update
264 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
265 * DCX_CLIPCHILDREN flag is set. */
266 BOOL DCE_InvalidateDCE(HWND hwnd, const RECT* pRectUpdate)
268 HWND hwndScope = GetAncestor( hwnd, GA_PARENT );
269 BOOL bRet = FALSE;
271 if( hwndScope )
273 DCE *dce;
275 TRACE("scope hwnd = %p, (%ld,%ld - %ld,%ld)\n",
276 hwndScope, pRectUpdate->left,pRectUpdate->top,
277 pRectUpdate->right,pRectUpdate->bottom);
278 if(TRACE_ON(dc))
279 DCE_DumpCache();
281 /* walk all DCEs and fixup non-empty entries */
283 for (dce = firstDCE; (dce); dce = dce->next)
285 if (dce->DCXflags & DCX_DCEEMPTY) continue;
286 if ((dce->hwndCurrent == hwndScope) && !(dce->DCXflags & DCX_CLIPCHILDREN))
287 continue; /* child window positions don't bother us */
289 /* check if DCE window is within the z-order scope */
291 if (hwndScope == dce->hwndCurrent || IsChild( hwndScope, dce->hwndCurrent ))
293 if (hwnd != dce->hwndCurrent)
295 /* check if the window rectangle intersects this DCE window */
296 RECT rect;
297 GetWindowRect( dce->hwndCurrent, &rect );
298 MapWindowPoints( 0, hwndScope, (POINT *)&rect, 2 );
299 if (!IntersectRect( &rect, &rect, pRectUpdate )) continue;
302 if( !(dce->DCXflags & DCX_DCEBUSY) )
304 /* Don't bother with visible regions of unused DCEs */
306 TRACE("\tpurged %p dce [%p]\n", dce, dce->hwndCurrent);
307 if (dce->hwndCurrent && USER_Driver.pReleaseDC)
308 USER_Driver.pReleaseDC( dce->hwndCurrent, dce->hDC );
309 dce->hwndCurrent = 0;
310 dce->DCXflags &= DCX_CACHE;
311 dce->DCXflags |= DCX_DCEEMPTY;
313 else
315 /* Set dirty bits in the hDC and DCE structs */
317 TRACE("\tfixed up %p dce [%p]\n", dce, dce->hwndCurrent);
318 dce->DCXflags |= DCX_DCEDIRTY;
319 SetHookFlags16( HDC_16(dce->hDC), DCHF_INVALIDATEVISRGN );
320 bRet = TRUE;
323 } /* dce list */
325 return bRet;
329 /***********************************************************************
330 * GetDCEx (USER32.@)
332 * Unimplemented flags: DCX_LOCKWINDOWUPDATE
334 * FIXME: Full support for hrgnClip == 1 (alias for entire window).
336 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
338 HDC hdc = 0;
339 DCE * dce;
340 WND * wndPtr;
341 DWORD dcxFlags = 0;
342 BOOL bUpdateVisRgn = TRUE;
343 BOOL bUpdateClipOrigin = FALSE;
344 HWND parent, full;
346 TRACE("hwnd %p, hrgnClip %p, flags %08lx\n", hwnd, hrgnClip, flags);
348 if (flags & (DCX_LOCKWINDOWUPDATE)) {
349 FIXME("not yet supported - see source\n");
350 /* See the comment in LockWindowUpdate for more explanation. This flag is not implemented
351 * by that patch, but we need LockWindowUpdate implemented correctly before this can be done.
355 if (!hwnd) hwnd = GetDesktopWindow();
356 if (!(full = WIN_IsCurrentProcess( hwnd )))
358 FIXME( "not supported yet on other process window %p\n", hwnd );
359 return 0;
361 hwnd = full;
362 if (!(wndPtr = WIN_GetPtr( hwnd ))) return 0;
364 /* fixup flags */
366 if (flags & (DCX_WINDOW | DCX_PARENTCLIP)) flags |= DCX_CACHE;
368 if (flags & DCX_USESTYLE)
370 flags &= ~( DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
372 if( wndPtr->dwStyle & WS_CLIPSIBLINGS )
373 flags |= DCX_CLIPSIBLINGS;
375 if ( !(flags & DCX_WINDOW) )
377 if (wndPtr->clsStyle & CS_PARENTDC) flags |= DCX_PARENTCLIP;
379 if (wndPtr->dwStyle & WS_CLIPCHILDREN &&
380 !(wndPtr->dwStyle & WS_MINIMIZE) ) flags |= DCX_CLIPCHILDREN;
381 if (!wndPtr->dce) flags |= DCX_CACHE;
385 if (flags & DCX_WINDOW) flags &= ~DCX_CLIPCHILDREN;
387 parent = GetAncestor( hwnd, GA_PARENT );
388 if (!parent || (parent == GetDesktopWindow()))
389 flags = (flags & ~DCX_PARENTCLIP) | DCX_CLIPSIBLINGS;
391 /* it seems parent clip is ignored when clipping siblings or children */
392 if (flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) flags &= ~DCX_PARENTCLIP;
394 if( flags & DCX_PARENTCLIP )
396 LONG parent_style = GetWindowLongW( parent, GWL_STYLE );
397 if( (wndPtr->dwStyle & WS_VISIBLE) && (parent_style & WS_VISIBLE) )
399 flags &= ~DCX_CLIPCHILDREN;
400 if (parent_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
404 /* find a suitable DCE */
406 dcxFlags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
407 DCX_CACHE | DCX_WINDOW);
409 if (flags & DCX_CACHE)
411 DCE* dceEmpty;
412 DCE* dceUnused;
414 dceEmpty = dceUnused = NULL;
416 /* Strategy: First, we attempt to find a non-empty but unused DCE with
417 * compatible flags. Next, we look for an empty entry. If the cache is
418 * full we have to purge one of the unused entries.
421 for (dce = firstDCE; (dce); dce = dce->next)
423 if ((dce->DCXflags & (DCX_CACHE | DCX_DCEBUSY)) == DCX_CACHE )
425 dceUnused = dce;
427 if (dce->DCXflags & DCX_DCEEMPTY)
428 dceEmpty = dce;
429 else
430 if ((dce->hwndCurrent == hwnd) &&
431 ((dce->DCXflags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
432 DCX_CACHE | DCX_WINDOW | DCX_PARENTCLIP)) == dcxFlags))
434 TRACE("\tfound valid %p dce [%p], flags %08lx\n",
435 dce, hwnd, dcxFlags );
436 bUpdateVisRgn = FALSE;
437 bUpdateClipOrigin = TRUE;
438 break;
443 if (!dce) dce = (dceEmpty) ? dceEmpty : dceUnused;
445 /* if there's no dce empty or unused, allocate a new one */
446 if (!dce)
448 dce = DCE_AllocDCE( 0, DCE_CACHE_DC );
451 else
453 dce = wndPtr->dce;
454 if (dce && dce->hwndCurrent == hwnd)
456 TRACE("\tskipping hVisRgn update\n");
457 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
460 if (!dce)
462 hdc = 0;
463 goto END;
466 if (!(flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN))) hrgnClip = 0;
468 if (((flags ^ dce->DCXflags) & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
469 (dce->hClipRgn != hrgnClip))
471 /* if the extra clip region has changed, get rid of the old one */
472 DCE_DeleteClipRgn( dce );
475 dce->hwndCurrent = hwnd;
476 dce->hClipRgn = hrgnClip;
477 dce->DCXflags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
478 DCX_CACHE | DCX_WINDOW | DCX_WINDOWPAINT |
479 DCX_INTERSECTRGN | DCX_EXCLUDERGN);
480 dce->DCXflags |= DCX_DCEBUSY;
481 dce->DCXflags &= ~DCX_DCEDIRTY;
482 hdc = dce->hDC;
484 if (bUpdateVisRgn) SetHookFlags16( HDC_16(hdc), DCHF_INVALIDATEVISRGN ); /* force update */
486 if (!USER_Driver.pGetDC || !USER_Driver.pGetDC( hwnd, hdc, hrgnClip, flags ))
487 hdc = 0;
489 TRACE("(%p,%p,0x%lx): returning %p\n", hwnd, hrgnClip, flags, hdc);
490 END:
491 WIN_ReleasePtr(wndPtr);
492 return hdc;
496 /***********************************************************************
497 * GetDC (USER32.@)
499 * Get a device context.
501 * RETURNS
502 * Success: Handle to the device context
503 * Failure: NULL.
505 HDC WINAPI GetDC( HWND hwnd )
507 if (!hwnd)
508 return GetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
509 return GetDCEx( hwnd, 0, DCX_USESTYLE );
513 /***********************************************************************
514 * GetWindowDC (USER32.@)
516 HDC WINAPI GetWindowDC( HWND hwnd )
518 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
522 /***********************************************************************
523 * ReleaseDC (USER32.@)
525 * Release a device context.
527 * RETURNS
528 * Success: Non-zero. Resources used by hdc are released.
529 * Failure: 0.
531 INT WINAPI ReleaseDC( HWND hwnd, HDC hdc )
533 DCE * dce;
534 INT nRet = 0;
536 USER_Lock();
537 dce = firstDCE;
539 TRACE("%p %p\n", hwnd, hdc );
541 while (dce && (dce->hDC != hdc)) dce = dce->next;
543 if ( dce )
544 if ( dce->DCXflags & DCX_DCEBUSY )
545 nRet = DCE_ReleaseDC( dce );
547 USER_Unlock();
549 return nRet;
552 /***********************************************************************
553 * DCHook (USER.362)
555 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
557 BOOL16 WINAPI DCHook16( HDC16 hDC, WORD code, DWORD data, LPARAM lParam )
559 BOOL retv = TRUE;
560 DCE *dce = (DCE *)data;
562 TRACE("hDC = %04x, %i\n", hDC, code);
564 if (!dce) return 0;
565 assert( HDC_16(dce->hDC) == hDC );
567 /* Grab the windows lock before doing anything else */
568 USER_Lock();
570 switch( code )
572 case DCHC_INVALIDVISRGN:
573 /* GDI code calls this when it detects that the
574 * DC is dirty (usually after SetHookFlags()). This
575 * means that we have to recompute the visible region.
577 if( dce->DCXflags & DCX_DCEBUSY )
579 /* Dirty bit has been cleared by caller, set it again so that
580 * pGetDC recomputes the visible region. */
581 SetHookFlags16( hDC, DCHF_INVALIDATEVISRGN );
582 if (USER_Driver.pGetDC)
583 USER_Driver.pGetDC( dce->hwndCurrent, dce->hDC, dce->hClipRgn, dce->DCXflags );
585 else /* non-fatal but shouldn't happen */
586 WARN("DC is not in use!\n");
587 break;
589 case DCHC_DELETEDC:
591 * Windows will not let you delete a DC that is busy
592 * (between GetDC and ReleaseDC)
595 if ( dce->DCXflags & DCX_DCEBUSY )
597 WARN("Application trying to delete a busy DC\n");
598 retv = FALSE;
600 else DCE_FreeDCE( dce );
601 break;
603 default:
604 FIXME("unknown code\n");
607 USER_Unlock(); /* Release the wnd lock */
608 return retv;
612 /**********************************************************************
613 * WindowFromDC (USER32.@)
615 HWND WINAPI WindowFromDC( HDC hDC )
617 DCE *dce;
618 HWND hwnd;
620 USER_Lock();
621 dce = firstDCE;
623 while (dce && (dce->hDC != hDC)) dce = dce->next;
625 hwnd = dce ? dce->hwndCurrent : 0;
626 USER_Unlock();
628 return hwnd;
632 /***********************************************************************
633 * LockWindowUpdate (USER32.@)
635 BOOL WINAPI LockWindowUpdate( HWND hwnd )
637 static HWND lockedWnd;
639 /* This function is fully implemented by the following patch:
641 * http://www.winehq.org/hypermail/wine-patches/2004/01/0142.html
643 * but in order to work properly, it needs the ability to invalidate
644 * DCEs in other processes when the lock window is changed, which
645 * isn't possible yet.
646 * -mike
649 FIXME("(%p), partial stub!\n",hwnd);
651 USER_Lock();
652 if (lockedWnd)
654 if (!hwnd)
656 /* Unlock lockedWnd */
657 /* FIXME: Do something */
659 else
661 /* Attempted to lock a second window */
662 /* Return FALSE and do nothing */
663 USER_Unlock();
664 return FALSE;
667 lockedWnd = hwnd;
668 USER_Unlock();
669 return TRUE;