Do not clear the drawing area for etched static controls.
[wine.git] / windows / dce.c
blob2e24db52a68be35b1f7e7d880503038232a89061
1 /*
2 * USER DCE functions
4 * Copyright 1993 Alexandre Julliard
5 * 1996,1997 Alex Korobka
8 * Note: Visible regions of CS_OWNDC/CS_CLASSDC window DCs
9 * have to be updated dynamically.
11 * Internal DCX flags:
13 * DCX_DCEEMPTY - dce is uninitialized
14 * DCX_DCEBUSY - dce is in use
15 * DCX_DCEDIRTY - ReleaseDC() should wipe instead of caching
16 * DCX_KEEPCLIPRGN - ReleaseDC() should not delete the clipping region
17 * DCX_WINDOWPAINT - BeginPaint() is in effect
20 #include <assert.h>
21 #include "desktop.h"
22 #include "options.h"
23 #include "dce.h"
24 #include "class.h"
25 #include "win.h"
26 #include "gdi.h"
27 #include "region.h"
28 #include "heap.h"
29 #include "local.h"
30 #include "module.h"
31 #include "debugtools.h"
32 #include "wine/winbase16.h"
33 #include "wine/winuser16.h"
35 DEFAULT_DEBUG_CHANNEL(dc)
37 #define NB_DCE 5 /* Number of DCEs created at startup */
39 static DCE *firstDCE = 0;
40 static HDC defaultDCstate = 0;
42 static void DCE_DeleteClipRgn( DCE* );
43 static INT DCE_ReleaseDC( DCE* );
46 /***********************************************************************
47 * DCE_DumpCache
49 static void DCE_DumpCache(void)
51 DCE *dce;
53 WIN_LockWnds();
54 dce = firstDCE;
56 DPRINTF("DCE:\n");
57 while( dce )
59 DPRINTF("\t[0x%08x] hWnd 0x%04x, dcx %08x, %s %s\n",
60 (unsigned)dce, dce->hwndCurrent, (unsigned)dce->DCXflags,
61 (dce->DCXflags & DCX_CACHE) ? "Cache" : "Owned",
62 (dce->DCXflags & DCX_DCEBUSY) ? "InUse" : "" );
63 dce = dce->next;
66 WIN_UnlockWnds();
69 /***********************************************************************
70 * DCE_AllocDCE
72 * Allocate a new DCE.
74 DCE *DCE_AllocDCE( HWND hWnd, DCE_TYPE type )
76 FARPROC16 hookProc;
77 DCE * dce;
78 WND* wnd;
80 if (!(dce = HeapAlloc( SystemHeap, 0, sizeof(DCE) ))) return NULL;
81 if (!(dce->hDC = CreateDC16( "DISPLAY", NULL, NULL, NULL )))
83 HeapFree( SystemHeap, 0, dce );
84 return 0;
87 wnd = WIN_FindWndPtr(hWnd);
89 /* store DCE handle in DC hook data field */
91 hookProc = (FARPROC16)NE_GetEntryPoint( GetModuleHandle16("USER"), 362 );
92 SetDCHook( dce->hDC, hookProc, (DWORD)dce );
94 dce->hwndCurrent = hWnd;
95 dce->hClipRgn = 0;
96 dce->next = firstDCE;
97 firstDCE = dce;
99 if( type != DCE_CACHE_DC ) /* owned or class DC */
101 dce->DCXflags = DCX_DCEBUSY;
102 if( hWnd )
104 if( wnd->dwStyle & WS_CLIPCHILDREN ) dce->DCXflags |= DCX_CLIPCHILDREN;
105 if( wnd->dwStyle & WS_CLIPSIBLINGS ) dce->DCXflags |= DCX_CLIPSIBLINGS;
107 SetHookFlags16(dce->hDC,DCHF_INVALIDATEVISRGN);
109 else dce->DCXflags = DCX_CACHE | DCX_DCEEMPTY;
111 WIN_ReleaseWndPtr(wnd);
113 return dce;
117 /***********************************************************************
118 * DCE_FreeDCE
120 DCE* DCE_FreeDCE( DCE *dce )
122 DCE **ppDCE;
124 if (!dce) return NULL;
126 WIN_LockWnds();
128 ppDCE = &firstDCE;
130 while (*ppDCE && (*ppDCE != dce)) ppDCE = &(*ppDCE)->next;
131 if (*ppDCE == dce) *ppDCE = dce->next;
133 SetDCHook(dce->hDC, NULL, 0L);
135 DeleteDC( dce->hDC );
136 if( dce->hClipRgn && !(dce->DCXflags & DCX_KEEPCLIPRGN) )
137 DeleteObject(dce->hClipRgn);
138 HeapFree( SystemHeap, 0, dce );
140 WIN_UnlockWnds();
142 return *ppDCE;
145 /***********************************************************************
146 * DCE_FreeWindowDCE
148 * Remove owned DCE and reset unreleased cache DCEs.
150 void DCE_FreeWindowDCE( WND* pWnd )
152 DCE *pDCE;
154 WIN_LockWnds();
155 pDCE = firstDCE;
157 while( pDCE )
159 if( pDCE->hwndCurrent == pWnd->hwndSelf )
161 if( pDCE == pWnd->dce ) /* owned DCE */
163 pDCE = DCE_FreeDCE( pDCE );
164 pWnd->dce = NULL;
165 continue;
167 else
169 if(!(pDCE->DCXflags & DCX_CACHE) ) /* class DCE */
171 if( pDCE->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN) )
172 DCE_DeleteClipRgn( pDCE );
174 else if( pDCE->DCXflags & DCX_DCEBUSY ) /* shared cache DCE */
176 ERR("[%04x] GetDC() without ReleaseDC()!\n",
177 pWnd->hwndSelf);
178 DCE_ReleaseDC( pDCE );
181 pDCE->DCXflags &= DCX_CACHE;
182 pDCE->DCXflags |= DCX_DCEEMPTY;
183 pDCE->hwndCurrent = 0;
186 pDCE = pDCE->next;
189 WIN_UnlockWnds();
193 /***********************************************************************
194 * DCE_DeleteClipRgn
196 static void DCE_DeleteClipRgn( DCE* dce )
198 dce->DCXflags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN | DCX_WINDOWPAINT);
200 if( dce->DCXflags & DCX_KEEPCLIPRGN )
201 dce->DCXflags &= ~DCX_KEEPCLIPRGN;
202 else
203 if( dce->hClipRgn > 1 )
204 DeleteObject( dce->hClipRgn );
206 dce->hClipRgn = 0;
208 TRACE("\trestoring VisRgn\n");
210 RestoreVisRgn16(dce->hDC);
214 /***********************************************************************
215 * DCE_ReleaseDC
217 static INT DCE_ReleaseDC( DCE* dce )
219 if ((dce->DCXflags & (DCX_DCEEMPTY | DCX_DCEBUSY)) != DCX_DCEBUSY) return 0;
221 /* restore previous visible region */
223 if ((dce->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
224 (dce->DCXflags & (DCX_CACHE | DCX_WINDOWPAINT)) )
225 DCE_DeleteClipRgn( dce );
227 if (dce->DCXflags & DCX_CACHE)
229 SetDCState16( dce->hDC, defaultDCstate );
230 dce->DCXflags &= ~DCX_DCEBUSY;
231 if (dce->DCXflags & DCX_DCEDIRTY)
233 /* don't keep around invalidated entries
234 * because SetDCState() disables hVisRgn updates
235 * by removing dirty bit. */
237 dce->hwndCurrent = 0;
238 dce->DCXflags &= DCX_CACHE;
239 dce->DCXflags |= DCX_DCEEMPTY;
242 return 1;
246 /***********************************************************************
247 * DCE_InvalidateDCE
249 * It is called from SetWindowPos() - we have to mark as dirty all busy
250 * DCEs for windows that have pWnd->parent as an ansector and whose client
251 * rect intersects with specified update rectangle. In addition, pWnd->parent
252 * DCEs may need to be updated if DCX_CLIPCHILDREN flag is set.
254 BOOL DCE_InvalidateDCE(WND* pWnd, const RECT* pRectUpdate)
256 WND* wndScope = WIN_LockWndPtr(pWnd->parent);
257 WND *pDesktop = WIN_GetDesktop();
258 BOOL bRet = FALSE;
260 if( wndScope )
262 DCE *dce;
264 TRACE("scope hwnd = %04x, (%i,%i - %i,%i)\n",
265 wndScope->hwndSelf, pRectUpdate->left,pRectUpdate->top,
266 pRectUpdate->right,pRectUpdate->bottom);
267 if(TRACE_ON(dc))
268 DCE_DumpCache();
270 /* walk all DCEs and fixup non-empty entries */
272 for (dce = firstDCE; (dce); dce = dce->next)
274 if( !(dce->DCXflags & DCX_DCEEMPTY) )
276 WND* wndCurrent = WIN_FindWndPtr(dce->hwndCurrent);
278 if( wndCurrent )
280 WND* wnd = NULL;
281 INT xoffset = 0, yoffset = 0;
283 if( (wndCurrent == wndScope) && !(dce->DCXflags & DCX_CLIPCHILDREN) )
285 /* child window positions don't bother us */
286 WIN_ReleaseWndPtr(wndCurrent);
287 continue;
290 if( !Options.desktopGeometry && wndCurrent == pDesktop )
292 /* don't bother with fake desktop */
293 WIN_ReleaseWndPtr(wndCurrent);
294 continue;
297 /* check if DCE window is within the z-order scope */
299 for( wnd = WIN_LockWndPtr(wndCurrent); wnd; WIN_UpdateWndPtr(&wnd,wnd->parent))
301 if( wnd == wndScope )
303 RECT wndRect;
305 wndRect = wndCurrent->rectWindow;
307 OffsetRect( &wndRect, xoffset - wndCurrent->rectClient.left,
308 yoffset - wndCurrent->rectClient.top);
310 if (pWnd == wndCurrent ||
311 IntersectRect( &wndRect, &wndRect, pRectUpdate ))
313 if( !(dce->DCXflags & DCX_DCEBUSY) )
315 /* Don't bother with visible regions of unused DCEs */
317 TRACE("\tpurged %08x dce [%04x]\n",
318 (unsigned)dce, wndCurrent->hwndSelf);
320 dce->hwndCurrent = 0;
321 dce->DCXflags &= DCX_CACHE;
322 dce->DCXflags |= DCX_DCEEMPTY;
324 else
326 /* Set dirty bits in the hDC and DCE structs */
328 TRACE("\tfixed up %08x dce [%04x]\n",
329 (unsigned)dce, wndCurrent->hwndSelf);
331 dce->DCXflags |= DCX_DCEDIRTY;
332 SetHookFlags16(dce->hDC, DCHF_INVALIDATEVISRGN);
333 bRet = TRUE;
336 WIN_ReleaseWndPtr(wnd);
337 break;
339 xoffset += wnd->rectClient.left;
340 yoffset += wnd->rectClient.top;
343 WIN_ReleaseWndPtr(wndCurrent);
345 } /* dce list */
346 WIN_ReleaseWndPtr(wndScope);
348 WIN_ReleaseDesktop();
349 return bRet;
352 /***********************************************************************
353 * DCE_Init
355 void DCE_Init(void)
357 int i;
358 DCE * dce;
360 for (i = 0; i < NB_DCE; i++)
362 if (!(dce = DCE_AllocDCE( 0, DCE_CACHE_DC ))) return;
363 if (!defaultDCstate) defaultDCstate = GetDCState16( dce->hDC );
368 /***********************************************************************
369 * DCE_GetVisRect
371 * Calculate the visible rectangle of a window (i.e. the client or
372 * window area clipped by the client area of all ancestors) in the
373 * corresponding coordinates. Return FALSE if the visible region is empty.
375 static BOOL DCE_GetVisRect( WND *wndPtr, BOOL clientArea, RECT *lprect )
377 *lprect = clientArea ? wndPtr->rectClient : wndPtr->rectWindow;
379 if (wndPtr->dwStyle & WS_VISIBLE)
381 INT xoffset = lprect->left;
382 INT yoffset = lprect->top;
384 while( !(wndPtr->flags & WIN_NATIVE) &&
385 ( wndPtr = WIN_LockWndPtr(wndPtr->parent)) )
387 if ( (wndPtr->dwStyle & (WS_ICONIC | WS_VISIBLE)) != WS_VISIBLE )
389 WIN_ReleaseWndPtr(wndPtr);
390 goto fail;
393 xoffset += wndPtr->rectClient.left;
394 yoffset += wndPtr->rectClient.top;
395 OffsetRect( lprect, wndPtr->rectClient.left,
396 wndPtr->rectClient.top );
398 if( (wndPtr->rectClient.left >= wndPtr->rectClient.right) ||
399 (wndPtr->rectClient.top >= wndPtr->rectClient.bottom) ||
400 (lprect->left >= wndPtr->rectClient.right) ||
401 (lprect->right <= wndPtr->rectClient.left) ||
402 (lprect->top >= wndPtr->rectClient.bottom) ||
403 (lprect->bottom <= wndPtr->rectClient.top) )
405 WIN_ReleaseWndPtr(wndPtr);
406 goto fail;
409 lprect->left = MAX( lprect->left, wndPtr->rectClient.left );
410 lprect->right = MIN( lprect->right, wndPtr->rectClient.right );
411 lprect->top = MAX( lprect->top, wndPtr->rectClient.top );
412 lprect->bottom = MIN( lprect->bottom, wndPtr->rectClient.bottom );
414 WIN_ReleaseWndPtr(wndPtr);
416 OffsetRect( lprect, -xoffset, -yoffset );
417 return TRUE;
420 fail:
421 SetRectEmpty( lprect );
422 return FALSE;
426 /***********************************************************************
427 * DCE_AddClipRects
429 * Go through the linked list of windows from pWndStart to pWndEnd,
430 * adding to the clip region the intersection of the target rectangle
431 * with an offset window rectangle.
433 static BOOL DCE_AddClipRects( WND *pWndStart, WND *pWndEnd,
434 HRGN hrgnClip, LPRECT lpRect, int x, int y )
436 RECT rect;
438 if( pWndStart->pDriver->pIsSelfClipping( pWndStart ) )
439 return TRUE; /* The driver itself will do the clipping */
441 for (WIN_LockWndPtr(pWndStart); pWndStart != pWndEnd; WIN_UpdateWndPtr(&pWndStart,pWndStart->next))
443 if( !(pWndStart->dwStyle & WS_VISIBLE) ) continue;
445 rect.left = pWndStart->rectWindow.left + x;
446 rect.top = pWndStart->rectWindow.top + y;
447 rect.right = pWndStart->rectWindow.right + x;
448 rect.bottom = pWndStart->rectWindow.bottom + y;
450 if( IntersectRect( &rect, &rect, lpRect ))
452 if(!REGION_UnionRectWithRgn( hrgnClip, &rect )) break;
455 WIN_ReleaseWndPtr(pWndStart);
456 return (pWndStart == pWndEnd);
460 /***********************************************************************
461 * DCE_GetVisRgn
463 * Return the visible region of a window, i.e. the client or window area
464 * clipped by the client area of all ancestors, and then optionally
465 * by siblings and children.
467 HRGN DCE_GetVisRgn( HWND hwnd, WORD flags, HWND hwndChild, WORD cflags )
469 HRGN hrgnVis = 0;
470 RECT rect;
471 WND *wndPtr = WIN_FindWndPtr( hwnd );
472 WND *childWnd = WIN_FindWndPtr( hwndChild );
474 /* Get visible rectangle and create a region with it. */
476 if (wndPtr && DCE_GetVisRect(wndPtr, !(flags & DCX_WINDOW), &rect))
478 if((hrgnVis = CreateRectRgnIndirect( &rect )))
480 HRGN hrgnClip = CreateRectRgn( 0, 0, 0, 0 );
481 INT xoffset, yoffset;
483 if( hrgnClip )
485 /* Compute obscured region for the visible rectangle by
486 * clipping children, siblings, and ancestors. Note that
487 * DCE_GetVisRect() returns a rectangle either in client
488 * or in window coordinates (for DCX_WINDOW request). */
490 if( (flags & DCX_CLIPCHILDREN) && wndPtr->child )
492 if( flags & DCX_WINDOW )
494 /* adjust offsets since child window rectangles are
495 * in client coordinates */
497 xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
498 yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
500 else
501 xoffset = yoffset = 0;
503 DCE_AddClipRects( wndPtr->child, NULL, hrgnClip,
504 &rect, xoffset, yoffset );
507 /* We may need to clip children of child window, if a window with PARENTDC
508 * class style and CLIPCHILDREN window style (like in Free Agent 16
509 * preference dialogs) gets here, we take the region for the parent window
510 * but apparently still need to clip the children of the child window... */
512 if( (cflags & DCX_CLIPCHILDREN) && childWnd && childWnd->child )
514 if( flags & DCX_WINDOW )
516 /* adjust offsets since child window rectangles are
517 * in client coordinates */
519 xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
520 yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
522 else
523 xoffset = yoffset = 0;
525 /* client coordinates of child window */
526 xoffset += childWnd->rectClient.left;
527 yoffset += childWnd->rectClient.top;
529 DCE_AddClipRects( childWnd->child, NULL, hrgnClip,
530 &rect, xoffset, yoffset );
533 /* sibling window rectangles are in client
534 * coordinates of the parent window */
536 if (flags & DCX_WINDOW)
538 xoffset = -wndPtr->rectWindow.left;
539 yoffset = -wndPtr->rectWindow.top;
541 else
543 xoffset = -wndPtr->rectClient.left;
544 yoffset = -wndPtr->rectClient.top;
547 if (flags & DCX_CLIPSIBLINGS && wndPtr->parent )
548 DCE_AddClipRects( wndPtr->parent->child,
549 wndPtr, hrgnClip, &rect, xoffset, yoffset );
551 /* Clip siblings of all ancestors that have the
552 * WS_CLIPSIBLINGS style
555 while (wndPtr->dwStyle & WS_CHILD)
557 WIN_UpdateWndPtr(&wndPtr,wndPtr->parent);
558 xoffset -= wndPtr->rectClient.left;
559 yoffset -= wndPtr->rectClient.top;
560 if(wndPtr->dwStyle & WS_CLIPSIBLINGS && wndPtr->parent)
562 DCE_AddClipRects( wndPtr->parent->child, wndPtr,
563 hrgnClip, &rect, xoffset, yoffset );
567 /* Now once we've got a jumbo clip region we have
568 * to substract it from the visible rectangle.
571 CombineRgn( hrgnVis, hrgnVis, hrgnClip, RGN_DIFF );
572 DeleteObject( hrgnClip );
574 else
576 DeleteObject( hrgnVis );
577 hrgnVis = 0;
581 else
582 hrgnVis = CreateRectRgn(0, 0, 0, 0); /* empty */
583 WIN_ReleaseWndPtr(wndPtr);
584 WIN_ReleaseWndPtr(childWnd);
585 return hrgnVis;
588 /***********************************************************************
589 * DCE_OffsetVisRgn
591 * Change region from DC-origin relative coordinates to screen coords.
594 static void DCE_OffsetVisRgn( HDC hDC, HRGN hVisRgn )
596 DC *dc;
597 if (!(dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC ))) return;
599 OffsetRgn( hVisRgn, dc->w.DCOrgX, dc->w.DCOrgY );
601 GDI_HEAP_UNLOCK( hDC );
604 /***********************************************************************
605 * DCE_ExcludeRgn
607 * Translate given region from the wnd client to the DC coordinates
608 * and add it to the clipping region.
610 INT16 DCE_ExcludeRgn( HDC hDC, WND* wnd, HRGN hRgn )
612 POINT pt = {0, 0};
613 DCE *dce = firstDCE;
615 while (dce && (dce->hDC != hDC)) dce = dce->next;
616 if( dce )
618 MapWindowPoints( wnd->hwndSelf, dce->hwndCurrent, &pt, 1);
619 if( dce->DCXflags & DCX_WINDOW )
621 wnd = WIN_FindWndPtr(dce->hwndCurrent);
622 pt.x += wnd->rectClient.left - wnd->rectWindow.left;
623 pt.y += wnd->rectClient.top - wnd->rectWindow.top;
624 WIN_ReleaseWndPtr(wnd);
627 else return ERROR;
628 OffsetRgn(hRgn, pt.x, pt.y);
630 return ExtSelectClipRgn( hDC, hRgn, RGN_DIFF );
634 /***********************************************************************
635 * GetDCEx16 (USER.359)
637 HDC16 WINAPI GetDCEx16( HWND16 hwnd, HRGN16 hrgnClip, DWORD flags )
639 return (HDC16)GetDCEx( hwnd, hrgnClip, flags );
643 /***********************************************************************
644 * GetDCEx32 (USER32.231)
646 * Unimplemented flags: DCX_LOCKWINDOWUPDATE
648 * FIXME: Full support for hrgnClip == 1 (alias for entire window).
650 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
652 HRGN hrgnVisible = 0;
653 HDC hdc = 0;
654 DCE * dce;
655 DC * dc;
656 WND * wndPtr;
657 DWORD dcxFlags = 0;
658 BOOL bUpdateVisRgn = TRUE;
659 BOOL bUpdateClipOrigin = FALSE;
661 TRACE("hwnd %04x, hrgnClip %04x, flags %08x\n",
662 hwnd, hrgnClip, (unsigned)flags);
664 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
666 /* fixup flags */
668 if (!(wndPtr->class->style & (CS_OWNDC | CS_CLASSDC))) flags |= DCX_CACHE;
670 if (flags & DCX_USESTYLE)
672 flags &= ~( DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
674 if( wndPtr->dwStyle & WS_CLIPSIBLINGS )
675 flags |= DCX_CLIPSIBLINGS;
677 if ( !(flags & DCX_WINDOW) )
679 if (wndPtr->class->style & CS_PARENTDC) flags |= DCX_PARENTCLIP;
681 if (wndPtr->dwStyle & WS_CLIPCHILDREN &&
682 !(wndPtr->dwStyle & WS_MINIMIZE) ) flags |= DCX_CLIPCHILDREN;
684 else flags |= DCX_CACHE;
687 if( flags & DCX_NOCLIPCHILDREN )
689 flags |= DCX_CACHE;
690 flags &= ~(DCX_PARENTCLIP | DCX_CLIPCHILDREN);
693 if (flags & DCX_WINDOW)
694 flags = (flags & ~DCX_CLIPCHILDREN) | DCX_CACHE;
696 if (!(wndPtr->dwStyle & WS_CHILD) || !wndPtr->parent )
697 flags &= ~DCX_PARENTCLIP;
698 else if( flags & DCX_PARENTCLIP )
700 flags |= DCX_CACHE;
701 if( !(flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) )
702 if( (wndPtr->dwStyle & WS_VISIBLE) && (wndPtr->parent->dwStyle & WS_VISIBLE) )
704 flags &= ~DCX_CLIPCHILDREN;
705 if( wndPtr->parent->dwStyle & WS_CLIPSIBLINGS )
706 flags |= DCX_CLIPSIBLINGS;
710 /* find a suitable DCE */
712 dcxFlags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
713 DCX_CACHE | DCX_WINDOW);
715 if (flags & DCX_CACHE)
717 DCE* dceEmpty;
718 DCE* dceUnused;
720 dceEmpty = dceUnused = NULL;
722 /* Strategy: First, we attempt to find a non-empty but unused DCE with
723 * compatible flags. Next, we look for an empty entry. If the cache is
724 * full we have to purge one of the unused entries.
727 for (dce = firstDCE; (dce); dce = dce->next)
729 if ((dce->DCXflags & (DCX_CACHE | DCX_DCEBUSY)) == DCX_CACHE )
731 dceUnused = dce;
733 if (dce->DCXflags & DCX_DCEEMPTY)
734 dceEmpty = dce;
735 else
736 if ((dce->hwndCurrent == hwnd) &&
737 ((dce->DCXflags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
738 DCX_CACHE | DCX_WINDOW | DCX_PARENTCLIP)) == dcxFlags))
740 TRACE("\tfound valid %08x dce [%04x], flags %08x\n",
741 (unsigned)dce, hwnd, (unsigned)dcxFlags );
742 bUpdateVisRgn = FALSE;
743 bUpdateClipOrigin = TRUE;
744 break;
749 if (!dce) dce = (dceEmpty) ? dceEmpty : dceUnused;
751 /* if there's no dce empty or unused, allocate a new one */
752 if (!dce)
754 dce = DCE_AllocDCE( 0, DCE_CACHE_DC );
757 else
759 dce = (wndPtr->class->style & CS_OWNDC) ? wndPtr->dce : wndPtr->class->dce;
760 if( dce->hwndCurrent == hwnd )
762 TRACE("\tskipping hVisRgn update\n");
763 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
765 /* Abey - 16Jul99. to take care of the nested GetDC. first one
766 with DCX_EXCLUDERGN or DCX_INTERSECTRGN flags and the next
767 one with or without these flags. */
769 if(dce->DCXflags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN))
771 /* This is likely to be a nested BeginPaint().
772 or a BeginPaint() followed by a GetDC()*/
774 if( dce->hClipRgn != hrgnClip )
776 FIXME("new hrgnClip[%04x] smashes the previous[%04x]\n",
777 hrgnClip, dce->hClipRgn );
778 DCE_DeleteClipRgn( dce );
780 else
781 RestoreVisRgn16(dce->hDC);
785 if (!dce)
787 hdc = 0;
788 goto END;
791 dce->hwndCurrent = hwnd;
792 dce->hClipRgn = 0;
793 dce->DCXflags = dcxFlags | (flags & DCX_WINDOWPAINT) | DCX_DCEBUSY;
794 hdc = dce->hDC;
796 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC )))
798 hdc = 0;
799 goto END;
801 bUpdateVisRgn = bUpdateVisRgn || (dc->w.flags & DC_DIRTY);
803 /* recompute visible region */
804 wndPtr->pDriver->pSetDrawable( wndPtr, dc, flags, bUpdateClipOrigin );
806 if( bUpdateVisRgn )
808 TRACE("updating visrgn for %08x dce, hwnd [%04x]\n", (unsigned)dce, hwnd);
810 if (flags & DCX_PARENTCLIP)
812 WND *parentPtr = WIN_LockWndPtr(wndPtr->parent);
814 if( wndPtr->dwStyle & WS_VISIBLE && !(parentPtr->dwStyle & WS_MINIMIZE) )
816 if( parentPtr->dwStyle & WS_CLIPSIBLINGS )
817 dcxFlags = DCX_CLIPSIBLINGS | (flags & ~(DCX_CLIPCHILDREN | DCX_WINDOW));
818 else
819 dcxFlags = flags & ~(DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW);
821 hrgnVisible = DCE_GetVisRgn( parentPtr->hwndSelf, dcxFlags,
822 wndPtr->hwndSelf, flags );
823 if( flags & DCX_WINDOW )
824 OffsetRgn( hrgnVisible, -wndPtr->rectWindow.left,
825 -wndPtr->rectWindow.top );
826 else
827 OffsetRgn( hrgnVisible, -wndPtr->rectClient.left,
828 -wndPtr->rectClient.top );
829 DCE_OffsetVisRgn( hdc, hrgnVisible );
831 else
832 hrgnVisible = CreateRectRgn( 0, 0, 0, 0 );
833 WIN_ReleaseWndPtr(parentPtr);
835 else
836 if ((hwnd == GetDesktopWindow()) && !DESKTOP_IsSingleWindow())
837 hrgnVisible = CreateRectRgn( 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN) );
838 else
840 hrgnVisible = DCE_GetVisRgn( hwnd, flags, 0, 0 );
841 DCE_OffsetVisRgn( hdc, hrgnVisible );
844 dc->w.flags &= ~DC_DIRTY;
845 dce->DCXflags &= ~DCX_DCEDIRTY;
846 SelectVisRgn16( hdc, hrgnVisible );
848 else
849 TRACE("no visrgn update %08x dce, hwnd [%04x]\n", (unsigned)dce, hwnd);
851 /* apply additional region operation (if any) */
853 if( flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN) )
855 if( !hrgnVisible ) hrgnVisible = CreateRectRgn( 0, 0, 0, 0 );
857 dce->DCXflags |= flags & (DCX_KEEPCLIPRGN | DCX_INTERSECTRGN | DCX_EXCLUDERGN);
858 dce->hClipRgn = hrgnClip;
860 TRACE("\tsaved VisRgn, clipRgn = %04x\n", hrgnClip);
862 SaveVisRgn16( hdc );
863 CombineRgn( hrgnVisible, hrgnClip, 0, RGN_COPY );
864 DCE_OffsetVisRgn( hdc, hrgnVisible );
865 CombineRgn( hrgnVisible, InquireVisRgn16( hdc ), hrgnVisible,
866 (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
867 SelectVisRgn16( hdc, hrgnVisible );
870 if( hrgnVisible ) DeleteObject( hrgnVisible );
872 TRACE("(%04x,%04x,0x%lx): returning %04x\n",
873 hwnd, hrgnClip, flags, hdc);
874 END:
875 WIN_ReleaseWndPtr(wndPtr);
876 return hdc;
880 /***********************************************************************
881 * GetDC16 (USER.66)
883 HDC16 WINAPI GetDC16( HWND16 hwnd )
885 return (HDC16)GetDC( hwnd );
889 /***********************************************************************
890 * GetDC32 (USER32.230)
891 * RETURNS
892 * :Handle to DC
893 * NULL: Failure
895 HDC WINAPI GetDC(
896 HWND hwnd /* handle of window */
898 if (!hwnd)
899 return GetDCEx( GetDesktopWindow(), 0, DCX_CACHE | DCX_WINDOW );
900 return GetDCEx( hwnd, 0, DCX_USESTYLE );
904 /***********************************************************************
905 * GetWindowDC16 (USER.67)
907 HDC16 WINAPI GetWindowDC16( HWND16 hwnd )
909 if (!hwnd) hwnd = GetDesktopWindow16();
910 return GetDCEx16( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
914 /***********************************************************************
915 * GetWindowDC32 (USER32.304)
917 HDC WINAPI GetWindowDC( HWND hwnd )
919 if (!hwnd) hwnd = GetDesktopWindow();
920 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
924 /***********************************************************************
925 * ReleaseDC16 (USER.68)
927 INT16 WINAPI ReleaseDC16( HWND16 hwnd, HDC16 hdc )
929 return (INT16)ReleaseDC( hwnd, hdc );
933 /***********************************************************************
934 * ReleaseDC32 (USER32.440)
936 * RETURNS
937 * 1: Success
938 * 0: Failure
940 INT WINAPI ReleaseDC(
941 HWND hwnd /* Handle of window - ignored */,
942 HDC hdc /* Handle of device context */
944 DCE * dce;
945 INT nRet = 0;
947 WIN_LockWnds();
948 dce = firstDCE;
950 TRACE("%04x %04x\n", hwnd, hdc );
952 while (dce && (dce->hDC != hdc)) dce = dce->next;
954 if ( dce )
955 if ( dce->DCXflags & DCX_DCEBUSY )
956 nRet = DCE_ReleaseDC( dce );
958 WIN_UnlockWnds();
960 return nRet;
963 /***********************************************************************
964 * DCHook (USER.362)
966 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
968 BOOL16 WINAPI DCHook16( HDC16 hDC, WORD code, DWORD data, LPARAM lParam )
970 BOOL retv = TRUE;
971 HRGN hVisRgn;
972 DCE *dce = (DCE *)data;
973 DC *dc;
974 WND *wndPtr;
976 TRACE("hDC = %04x, %i\n", hDC, code);
978 if (!dce) return 0;
979 assert(dce->hDC == hDC);
981 /* Grab the windows lock before doing anything else */
982 WIN_LockWnds();
984 switch( code )
986 case DCHC_INVALIDVISRGN:
988 /* GDI code calls this when it detects that the
989 * DC is dirty (usually after SetHookFlags()). This
990 * means that we have to recompute the visible region.
993 if( dce->DCXflags & DCX_DCEBUSY )
996 /* Update stale DC in DCX */
997 wndPtr = WIN_FindWndPtr( dce->hwndCurrent);
998 dc = (DC *) GDI_GetObjPtr( dce->hDC, DC_MAGIC);
999 if( dc && wndPtr)
1000 wndPtr->pDriver->pSetDrawable( wndPtr, dc,dce->DCXflags,TRUE);
1002 SetHookFlags16(hDC, DCHF_VALIDATEVISRGN);
1003 hVisRgn = DCE_GetVisRgn(dce->hwndCurrent, dce->DCXflags, 0, 0);
1005 TRACE("\tapplying saved clipRgn\n");
1007 /* clip this region with saved clipping region */
1009 if ( (dce->DCXflags & DCX_INTERSECTRGN && dce->hClipRgn != 1) ||
1010 ( dce->DCXflags & DCX_EXCLUDERGN && dce->hClipRgn) )
1013 if( (!dce->hClipRgn && dce->DCXflags & DCX_INTERSECTRGN) ||
1014 (dce->hClipRgn == 1 && dce->DCXflags & DCX_EXCLUDERGN) )
1015 SetRectRgn(hVisRgn,0,0,0,0);
1016 else
1017 CombineRgn(hVisRgn, hVisRgn, dce->hClipRgn,
1018 (dce->DCXflags & DCX_EXCLUDERGN)? RGN_DIFF:RGN_AND);
1020 dce->DCXflags &= ~DCX_DCEDIRTY;
1021 DCE_OffsetVisRgn( hDC, hVisRgn );
1022 SelectVisRgn16(hDC, hVisRgn);
1023 DeleteObject( hVisRgn );
1024 WIN_ReleaseWndPtr( wndPtr ); /* Release WIN_FindWndPtr lock */
1026 else /* non-fatal but shouldn't happen */
1027 WARN("DC is not in use!\n");
1028 break;
1030 case DCHC_DELETEDC:
1032 * Windows will not let you delete a DC that is busy
1033 * (between GetDC and ReleaseDC)
1036 if ( dce->DCXflags & DCX_DCEBUSY )
1038 WARN("Application trying to delete a busy DC\n");
1039 retv = FALSE;
1041 break;
1043 default:
1044 FIXME("unknown code\n");
1047 WIN_UnlockWnds(); /* Release the wnd lock */
1048 return retv;
1052 /**********************************************************************
1053 * WindowFromDC16 (USER.117)
1055 HWND16 WINAPI WindowFromDC16( HDC16 hDC )
1057 return (HWND16)WindowFromDC( hDC );
1061 /**********************************************************************
1062 * WindowFromDC32 (USER32.581)
1064 HWND WINAPI WindowFromDC( HDC hDC )
1066 DCE *dce;
1067 HWND hwnd;
1069 WIN_LockWnds();
1070 dce = firstDCE;
1072 while (dce && (dce->hDC != hDC)) dce = dce->next;
1074 hwnd = dce ? dce->hwndCurrent : 0;
1075 WIN_UnlockWnds();
1077 return hwnd;
1081 /***********************************************************************
1082 * LockWindowUpdate16 (USER.294)
1084 BOOL16 WINAPI LockWindowUpdate16( HWND16 hwnd )
1086 return LockWindowUpdate( hwnd );
1090 /***********************************************************************
1091 * LockWindowUpdate32 (USER32.378)
1093 BOOL WINAPI LockWindowUpdate( HWND hwnd )
1095 /* FIXME? DCX_LOCKWINDOWUPDATE is unimplemented */
1096 return TRUE;