Release 990226.
[wine/gsoc-2012-control.git] / windows / dce.c
blob25bf2361480dd52162bfebbb506fb8173ed5920b
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 "x11drv.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 "sysmetrics.h"
30 #include "local.h"
31 #include "debug.h"
32 #include "wine/winuser16.h"
34 #define NB_DCE 5 /* Number of DCEs created at startup */
36 static DCE *firstDCE = 0;
37 static HDC defaultDCstate = 0;
39 static void DCE_DeleteClipRgn( DCE* );
40 static INT DCE_ReleaseDC( DCE* );
43 /***********************************************************************
44 * DCE_DumpCache
46 static void DCE_DumpCache(void)
48 DCE* dce = firstDCE;
50 DUMP("DCE:\n");
51 while( dce )
53 DUMP("\t[0x%08x] hWnd 0x%04x, dcx %08x, %s %s\n",
54 (unsigned)dce, dce->hwndCurrent, (unsigned)dce->DCXflags,
55 (dce->DCXflags & DCX_CACHE) ? "Cache" : "Owned",
56 (dce->DCXflags & DCX_DCEBUSY) ? "InUse" : "" );
57 dce = dce->next;
61 /***********************************************************************
62 * DCE_AllocDCE
64 * Allocate a new DCE.
66 DCE *DCE_AllocDCE( HWND hWnd, DCE_TYPE type )
68 DCE * dce;
69 if (!(dce = HeapAlloc( SystemHeap, 0, sizeof(DCE) ))) return NULL;
70 if (!(dce->hDC = CreateDC16( "DISPLAY", NULL, NULL, NULL )))
72 HeapFree( SystemHeap, 0, dce );
73 return 0;
76 /* store DCE handle in DC hook data field */
78 SetDCHook( dce->hDC, (FARPROC16)DCHook16, (DWORD)dce );
80 dce->hwndCurrent = hWnd;
81 dce->hClipRgn = 0;
82 dce->next = firstDCE;
83 firstDCE = dce;
85 if( type != DCE_CACHE_DC ) /* owned or class DC */
87 dce->DCXflags = DCX_DCEBUSY;
88 if( hWnd )
90 WND* wnd = WIN_FindWndPtr(hWnd);
92 if( wnd->dwStyle & WS_CLIPCHILDREN ) dce->DCXflags |= DCX_CLIPCHILDREN;
93 if( wnd->dwStyle & WS_CLIPSIBLINGS ) dce->DCXflags |= DCX_CLIPSIBLINGS;
95 SetHookFlags16(dce->hDC,DCHF_INVALIDATEVISRGN);
97 else dce->DCXflags = DCX_CACHE | DCX_DCEEMPTY;
99 return dce;
103 /***********************************************************************
104 * DCE_FreeDCE
106 DCE* DCE_FreeDCE( DCE *dce )
108 DCE **ppDCE = &firstDCE;
110 if (!dce) return NULL;
111 while (*ppDCE && (*ppDCE != dce)) ppDCE = &(*ppDCE)->next;
112 if (*ppDCE == dce) *ppDCE = dce->next;
114 SetDCHook(dce->hDC, NULL, 0L);
116 DeleteDC( dce->hDC );
117 if( dce->hClipRgn && !(dce->DCXflags & DCX_KEEPCLIPRGN) )
118 DeleteObject(dce->hClipRgn);
119 HeapFree( SystemHeap, 0, dce );
120 return *ppDCE;
123 /***********************************************************************
124 * DCE_FreeWindowDCE
126 * Remove owned DCE and reset unreleased cache DCEs.
128 void DCE_FreeWindowDCE( WND* pWnd )
130 DCE *pDCE = firstDCE;
132 while( pDCE )
134 if( pDCE->hwndCurrent == pWnd->hwndSelf )
136 if( pDCE == pWnd->dce ) /* owned DCE */
138 pDCE = DCE_FreeDCE( pDCE );
139 pWnd->dce = NULL;
140 continue;
142 else
144 if(!(pDCE->DCXflags & DCX_CACHE) ) /* class DCE */
146 if( pDCE->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN) )
147 DCE_DeleteClipRgn( pDCE );
149 else if( pDCE->DCXflags & DCX_DCEBUSY ) /* shared cache DCE */
151 ERR(dc,"[%04x] GetDC() without ReleaseDC()!\n",
152 pWnd->hwndSelf);
153 DCE_ReleaseDC( pDCE );
156 pDCE->DCXflags &= DCX_CACHE;
157 pDCE->DCXflags |= DCX_DCEEMPTY;
158 pDCE->hwndCurrent = 0;
161 pDCE = pDCE->next;
166 /***********************************************************************
167 * DCE_DeleteClipRgn
169 static void DCE_DeleteClipRgn( DCE* dce )
171 dce->DCXflags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN | DCX_WINDOWPAINT);
173 if( dce->DCXflags & DCX_KEEPCLIPRGN )
174 dce->DCXflags &= ~DCX_KEEPCLIPRGN;
175 else
176 if( dce->hClipRgn > 1 )
177 DeleteObject( dce->hClipRgn );
179 dce->hClipRgn = 0;
181 TRACE(dc,"\trestoring VisRgn\n");
183 RestoreVisRgn16(dce->hDC);
187 /***********************************************************************
188 * DCE_ReleaseDC
190 static INT DCE_ReleaseDC( DCE* dce )
192 if ((dce->DCXflags & (DCX_DCEEMPTY | DCX_DCEBUSY)) != DCX_DCEBUSY) return 0;
194 /* restore previous visible region */
196 if ((dce->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
197 (dce->DCXflags & (DCX_CACHE | DCX_WINDOWPAINT)) )
198 DCE_DeleteClipRgn( dce );
200 if (dce->DCXflags & DCX_CACHE)
202 SetDCState16( dce->hDC, defaultDCstate );
203 dce->DCXflags &= ~DCX_DCEBUSY;
204 if (dce->DCXflags & DCX_DCEDIRTY)
206 /* don't keep around invalidated entries
207 * because SetDCState() disables hVisRgn updates
208 * by removing dirty bit. */
210 dce->hwndCurrent = 0;
211 dce->DCXflags &= DCX_CACHE;
212 dce->DCXflags |= DCX_DCEEMPTY;
215 return 1;
219 /***********************************************************************
220 * DCE_InvalidateDCE
222 * It is called from SetWindowPos() - we have to mark as dirty all busy
223 * DCE's for windows that have pWnd->parent as an ansector and whose client
224 * rect intersects with specified update rectangle.
226 BOOL DCE_InvalidateDCE(WND* pWnd, const RECT* pRectUpdate)
228 WND* wndScope = pWnd->parent;
229 BOOL bRet = FALSE;
231 if( wndScope )
233 DCE *dce;
235 TRACE(dc,"scope hwnd = %04x, (%i,%i - %i,%i)\n",
236 wndScope->hwndSelf, pRectUpdate->left,pRectUpdate->top,
237 pRectUpdate->right,pRectUpdate->bottom);
238 if(TRACE_ON(dc))
239 DCE_DumpCache();
241 /* walk all DCEs and fixup non-empty entries */
243 for (dce = firstDCE; (dce); dce = dce->next)
245 if( !(dce->DCXflags & DCX_DCEEMPTY) )
247 WND* wndCurrent = WIN_FindWndPtr(dce->hwndCurrent);
249 if( wndCurrent && wndCurrent != WIN_GetDesktop() )
251 WND* wnd = wndCurrent;
252 INT xoffset = 0, yoffset = 0;
254 if( (wndCurrent == wndScope) && !(dce->DCXflags & DCX_CLIPCHILDREN) ) continue;
256 /* check if DCE window is within the z-order scope */
258 for( wnd = wndCurrent; wnd; wnd = wnd->parent )
260 if( wnd == wndScope )
262 RECT wndRect;
264 wndRect = wndCurrent->rectWindow;
266 OffsetRect( &wndRect, xoffset - wndCurrent->rectClient.left,
267 yoffset - wndCurrent->rectClient.top);
269 if (pWnd == wndCurrent ||
270 IntersectRect( &wndRect, &wndRect, pRectUpdate ))
272 if( !(dce->DCXflags & DCX_DCEBUSY) )
274 /* Don't bother with visible regions of unused DCEs */
276 TRACE(dc,"\tpurged %08x dce [%04x]\n",
277 (unsigned)dce, wndCurrent->hwndSelf);
279 dce->hwndCurrent = 0;
280 dce->DCXflags &= DCX_CACHE;
281 dce->DCXflags |= DCX_DCEEMPTY;
283 else
285 /* Set dirty bits in the hDC and DCE structs */
287 TRACE(dc,"\tfixed up %08x dce [%04x]\n",
288 (unsigned)dce, wndCurrent->hwndSelf);
290 dce->DCXflags |= DCX_DCEDIRTY;
291 SetHookFlags16(dce->hDC, DCHF_INVALIDATEVISRGN);
292 bRet = TRUE;
295 break;
297 xoffset += wnd->rectClient.left;
298 yoffset += wnd->rectClient.top;
302 } /* dce list */
304 return bRet;
307 /***********************************************************************
308 * DCE_Init
310 void DCE_Init(void)
312 int i;
313 DCE * dce;
315 for (i = 0; i < NB_DCE; i++)
317 if (!(dce = DCE_AllocDCE( 0, DCE_CACHE_DC ))) return;
318 if (!defaultDCstate) defaultDCstate = GetDCState16( dce->hDC );
323 /***********************************************************************
324 * DCE_GetVisRect
326 * Calculate the visible rectangle of a window (i.e. the client or
327 * window area clipped by the client area of all ancestors) in the
328 * corresponding coordinates. Return FALSE if the visible region is empty.
330 static BOOL DCE_GetVisRect( WND *wndPtr, BOOL clientArea, RECT *lprect )
332 *lprect = clientArea ? wndPtr->rectClient : wndPtr->rectWindow;
334 if (wndPtr->dwStyle & WS_VISIBLE)
336 INT xoffset = lprect->left;
337 INT yoffset = lprect->top;
339 while (wndPtr->dwStyle & WS_CHILD)
341 wndPtr = wndPtr->parent;
343 if ( (wndPtr->dwStyle & (WS_ICONIC | WS_VISIBLE)) != WS_VISIBLE )
344 goto fail;
346 xoffset += wndPtr->rectClient.left;
347 yoffset += wndPtr->rectClient.top;
348 OffsetRect( lprect, wndPtr->rectClient.left,
349 wndPtr->rectClient.top );
351 if( (wndPtr->rectClient.left >= wndPtr->rectClient.right) ||
352 (wndPtr->rectClient.top >= wndPtr->rectClient.bottom) ||
353 (lprect->left >= wndPtr->rectClient.right) ||
354 (lprect->right <= wndPtr->rectClient.left) ||
355 (lprect->top >= wndPtr->rectClient.bottom) ||
356 (lprect->bottom <= wndPtr->rectClient.top) )
357 goto fail;
359 lprect->left = MAX( lprect->left, wndPtr->rectClient.left );
360 lprect->right = MIN( lprect->right, wndPtr->rectClient.right );
361 lprect->top = MAX( lprect->top, wndPtr->rectClient.top );
362 lprect->bottom = MIN( lprect->bottom, wndPtr->rectClient.bottom );
364 OffsetRect( lprect, -xoffset, -yoffset );
365 return TRUE;
368 fail:
369 SetRectEmpty( lprect );
370 return FALSE;
374 /***********************************************************************
375 * DCE_AddClipRects
377 * Go through the linked list of windows from pWndStart to pWndEnd,
378 * adding to the clip region the intersection of the target rectangle
379 * with an offset window rectangle.
381 static BOOL DCE_AddClipRects( WND *pWndStart, WND *pWndEnd,
382 HRGN hrgnClip, LPRECT lpRect, int x, int y )
384 RECT rect;
386 if( pWndStart->pDriver->pIsSelfClipping( pWndStart ) )
387 return TRUE; /* The driver itself will do the clipping */
389 for (; pWndStart != pWndEnd; pWndStart = pWndStart->next)
391 if( !(pWndStart->dwStyle & WS_VISIBLE) ) continue;
393 rect.left = pWndStart->rectWindow.left + x;
394 rect.top = pWndStart->rectWindow.top + y;
395 rect.right = pWndStart->rectWindow.right + x;
396 rect.bottom = pWndStart->rectWindow.bottom + y;
398 if( IntersectRect( &rect, &rect, lpRect ))
399 if(!REGION_UnionRectWithRgn( hrgnClip, &rect ))
400 break;
402 return (pWndStart == pWndEnd);
406 /***********************************************************************
407 * DCE_GetVisRgn
409 * Return the visible region of a window, i.e. the client or window area
410 * clipped by the client area of all ancestors, and then optionally
411 * by siblings and children.
413 HRGN DCE_GetVisRgn( HWND hwnd, WORD flags, HWND hwndChild, WORD cflags )
415 HRGN hrgnVis = 0;
416 RECT rect;
417 WND *wndPtr = WIN_FindWndPtr( hwnd );
418 WND *childWnd = WIN_FindWndPtr( hwndChild );
420 /* Get visible rectangle and create a region with it. */
422 if (wndPtr && DCE_GetVisRect(wndPtr, !(flags & DCX_WINDOW), &rect))
424 if((hrgnVis = CreateRectRgnIndirect( &rect )))
426 HRGN hrgnClip = CreateRectRgn( 0, 0, 0, 0 );
427 INT xoffset, yoffset;
429 if( hrgnClip )
431 /* Compute obscured region for the visible rectangle by
432 * clipping children, siblings, and ancestors. Note that
433 * DCE_GetVisRect() returns a rectangle either in client
434 * or in window coordinates (for DCX_WINDOW request). */
436 if( (flags & DCX_CLIPCHILDREN) && wndPtr->child )
438 if( flags & DCX_WINDOW )
440 /* adjust offsets since child window rectangles are
441 * in client coordinates */
443 xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
444 yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
446 else
447 xoffset = yoffset = 0;
449 DCE_AddClipRects( wndPtr->child, NULL, hrgnClip,
450 &rect, xoffset, yoffset );
453 /* We may need to clip children of child window, if a window with PARENTDC
454 * class style and CLIPCHILDREN window style (like in Free Agent 16
455 * preference dialogs) gets here, we take the region for the parent window
456 * but apparently still need to clip the children of the child window... */
458 if( (cflags & DCX_CLIPCHILDREN) && childWnd && childWnd->child )
460 if( flags & DCX_WINDOW )
462 /* adjust offsets since child window rectangles are
463 * in client coordinates */
465 xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
466 yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
468 else
469 xoffset = yoffset = 0;
471 /* client coordinates of child window */
472 xoffset += childWnd->rectClient.left;
473 yoffset += childWnd->rectClient.top;
475 DCE_AddClipRects( childWnd->child, NULL, hrgnClip,
476 &rect, xoffset, yoffset );
479 /* sibling window rectangles are in client
480 * coordinates of the parent window */
482 if (flags & DCX_WINDOW)
484 xoffset = -wndPtr->rectWindow.left;
485 yoffset = -wndPtr->rectWindow.top;
487 else
489 xoffset = -wndPtr->rectClient.left;
490 yoffset = -wndPtr->rectClient.top;
493 if (flags & DCX_CLIPSIBLINGS && wndPtr->parent )
494 DCE_AddClipRects( wndPtr->parent->child,
495 wndPtr, hrgnClip, &rect, xoffset, yoffset );
497 /* Clip siblings of all ancestors that have the
498 * WS_CLIPSIBLINGS style
501 while (wndPtr->dwStyle & WS_CHILD)
503 wndPtr = wndPtr->parent;
504 xoffset -= wndPtr->rectClient.left;
505 yoffset -= wndPtr->rectClient.top;
506 if(wndPtr->dwStyle & WS_CLIPSIBLINGS && wndPtr->parent)
508 DCE_AddClipRects( wndPtr->parent->child, wndPtr,
509 hrgnClip, &rect, xoffset, yoffset );
513 /* Now once we've got a jumbo clip region we have
514 * to substract it from the visible rectangle.
517 CombineRgn( hrgnVis, hrgnVis, hrgnClip, RGN_DIFF );
518 DeleteObject( hrgnClip );
520 else
522 DeleteObject( hrgnVis );
523 hrgnVis = 0;
527 else
528 hrgnVis = CreateRectRgn(0, 0, 0, 0); /* empty */
529 return hrgnVis;
532 /***********************************************************************
533 * DCE_OffsetVisRgn
535 * Change region from DC-origin relative coordinates to screen coords.
538 static void DCE_OffsetVisRgn( HDC hDC, HRGN hVisRgn )
540 DC *dc;
541 if (!(dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC ))) return;
543 OffsetRgn( hVisRgn, dc->w.DCOrgX, dc->w.DCOrgY );
545 GDI_HEAP_UNLOCK( hDC );
548 /***********************************************************************
549 * DCE_ExcludeRgn
551 * Translate given region from the wnd client to the DC coordinates
552 * and add it to the clipping region.
554 INT16 DCE_ExcludeRgn( HDC hDC, WND* wnd, HRGN hRgn )
556 POINT pt = {0, 0};
557 DCE *dce = firstDCE;
559 while (dce && (dce->hDC != hDC)) dce = dce->next;
560 if( dce )
562 MapWindowPoints( wnd->hwndSelf, dce->hwndCurrent, &pt, 1);
563 if( dce->DCXflags & DCX_WINDOW )
565 wnd = WIN_FindWndPtr(dce->hwndCurrent);
566 pt.x += wnd->rectClient.left - wnd->rectWindow.left;
567 pt.y += wnd->rectClient.top - wnd->rectWindow.top;
570 else return ERROR;
571 OffsetRgn(hRgn, pt.x, pt.y);
573 return ExtSelectClipRgn( hDC, hRgn, RGN_DIFF );
576 /***********************************************************************
577 * GetDCEx16 (USER.359)
579 HDC16 WINAPI GetDCEx16( HWND16 hwnd, HRGN16 hrgnClip, DWORD flags )
581 return (HDC16)GetDCEx( hwnd, hrgnClip, flags );
585 /***********************************************************************
586 * GetDCEx32 (USER32.231)
588 * Unimplemented flags: DCX_LOCKWINDOWUPDATE
590 * FIXME: Full support for hrgnClip == 1 (alias for entire window).
592 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
594 HRGN hrgnVisible = 0;
595 HDC hdc = 0;
596 DCE * dce;
597 DC * dc;
598 WND * wndPtr;
599 DWORD dcxFlags = 0;
600 BOOL bUpdateVisRgn = TRUE;
601 BOOL bUpdateClipOrigin = FALSE;
603 TRACE(dc,"hwnd %04x, hrgnClip %04x, flags %08x\n",
604 hwnd, hrgnClip, (unsigned)flags);
606 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
608 /* fixup flags */
610 if (!(wndPtr->class->style & (CS_OWNDC | CS_CLASSDC))) flags |= DCX_CACHE;
612 if (flags & DCX_USESTYLE)
614 flags &= ~( DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
616 if( wndPtr->dwStyle & WS_CLIPSIBLINGS )
617 flags |= DCX_CLIPSIBLINGS;
619 if ( !(flags & DCX_WINDOW) )
621 if (wndPtr->class->style & CS_PARENTDC) flags |= DCX_PARENTCLIP;
623 if (wndPtr->dwStyle & WS_CLIPCHILDREN &&
624 !(wndPtr->dwStyle & WS_MINIMIZE) ) flags |= DCX_CLIPCHILDREN;
626 else flags |= DCX_CACHE;
629 if( flags & DCX_NOCLIPCHILDREN )
631 flags |= DCX_CACHE;
632 flags &= ~(DCX_PARENTCLIP | DCX_CLIPCHILDREN);
635 if (flags & DCX_WINDOW)
636 flags = (flags & ~DCX_CLIPCHILDREN) | DCX_CACHE;
638 if (!(wndPtr->dwStyle & WS_CHILD) || !wndPtr->parent )
639 flags &= ~DCX_PARENTCLIP;
640 else if( flags & DCX_PARENTCLIP )
642 flags |= DCX_CACHE;
643 if( !(flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) )
644 if( (wndPtr->dwStyle & WS_VISIBLE) && (wndPtr->parent->dwStyle & WS_VISIBLE) )
646 flags &= ~DCX_CLIPCHILDREN;
647 if( wndPtr->parent->dwStyle & WS_CLIPSIBLINGS )
648 flags |= DCX_CLIPSIBLINGS;
652 /* find a suitable DCE */
654 dcxFlags = flags & (DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
655 DCX_CACHE | DCX_WINDOW);
657 if (flags & DCX_CACHE)
659 DCE* dceEmpty;
660 DCE* dceUnused;
662 dceEmpty = dceUnused = NULL;
664 /* Strategy: First, we attempt to find a non-empty but unused DCE with
665 * compatible flags. Next, we look for an empty entry. If the cache is
666 * full we have to purge one of the unused entries.
669 for (dce = firstDCE; (dce); dce = dce->next)
671 if ((dce->DCXflags & (DCX_CACHE | DCX_DCEBUSY)) == DCX_CACHE )
673 dceUnused = dce;
675 if (dce->DCXflags & DCX_DCEEMPTY)
676 dceEmpty = dce;
677 else
678 if ((dce->hwndCurrent == hwnd) &&
679 ((dce->DCXflags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
680 DCX_CACHE | DCX_WINDOW | DCX_PARENTCLIP)) == dcxFlags))
682 TRACE(dc,"\tfound valid %08x dce [%04x], flags %08x\n",
683 (unsigned)dce, hwnd, (unsigned)dcxFlags );
684 bUpdateVisRgn = FALSE;
685 bUpdateClipOrigin = TRUE;
686 break;
690 if (!dce) dce = (dceEmpty) ? dceEmpty : dceUnused;
692 else
694 dce = (wndPtr->class->style & CS_OWNDC) ? wndPtr->dce : wndPtr->class->dce;
695 if( dce->hwndCurrent == hwnd )
697 TRACE(dc,"\tskipping hVisRgn update\n");
698 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
700 if( (dce->DCXflags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN)) &&
701 (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN)) )
703 /* This is likely to be a nested BeginPaint(). */
705 if( dce->hClipRgn != hrgnClip )
707 FIXME(dc,"new hrgnClip[%04x] smashes the previous[%04x]\n",
708 hrgnClip, dce->hClipRgn );
709 DCE_DeleteClipRgn( dce );
711 else
712 RestoreVisRgn16(dce->hDC);
716 if (!dce) return 0;
718 dce->hwndCurrent = hwnd;
719 dce->hClipRgn = 0;
720 dce->DCXflags = dcxFlags | (flags & DCX_WINDOWPAINT) | DCX_DCEBUSY;
721 hdc = dce->hDC;
723 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
724 bUpdateVisRgn = bUpdateVisRgn || (dc->w.flags & DC_DIRTY);
726 /* recompute visible region */
728 wndPtr->pDriver->pSetDrawable( wndPtr, dc, flags, bUpdateClipOrigin );
729 if( bUpdateVisRgn )
731 TRACE(dc,"updating visrgn for %08x dce, hwnd [%04x]\n", (unsigned)dce, hwnd);
733 if (flags & DCX_PARENTCLIP)
735 WND *parentPtr = wndPtr->parent;
737 if( wndPtr->dwStyle & WS_VISIBLE && !(parentPtr->dwStyle & WS_MINIMIZE) )
739 if( parentPtr->dwStyle & WS_CLIPSIBLINGS )
740 dcxFlags = DCX_CLIPSIBLINGS | (flags & ~(DCX_CLIPCHILDREN | DCX_WINDOW));
741 else
742 dcxFlags = flags & ~(DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW);
744 hrgnVisible = DCE_GetVisRgn( parentPtr->hwndSelf, dcxFlags,
745 wndPtr->hwndSelf, flags );
746 if( flags & DCX_WINDOW )
747 OffsetRgn( hrgnVisible, -wndPtr->rectWindow.left,
748 -wndPtr->rectWindow.top );
749 else
750 OffsetRgn( hrgnVisible, -wndPtr->rectClient.left,
751 -wndPtr->rectClient.top );
752 DCE_OffsetVisRgn( hdc, hrgnVisible );
754 else
755 hrgnVisible = CreateRectRgn( 0, 0, 0, 0 );
757 else
758 if ((hwnd == GetDesktopWindow()) &&
759 (X11DRV_WND_GetXRootWindow(wndPtr) == DefaultRootWindow(display)))
760 hrgnVisible = CreateRectRgn( 0, 0, SYSMETRICS_CXSCREEN,
761 SYSMETRICS_CYSCREEN );
762 else
764 hrgnVisible = DCE_GetVisRgn( hwnd, flags, 0, 0 );
765 DCE_OffsetVisRgn( hdc, hrgnVisible );
768 dc->w.flags &= ~DC_DIRTY;
769 dce->DCXflags &= ~DCX_DCEDIRTY;
770 SelectVisRgn16( hdc, hrgnVisible );
772 else
773 TRACE(dc,"no visrgn update %08x dce, hwnd [%04x]\n", (unsigned)dce, hwnd);
775 /* apply additional region operation (if any) */
777 if( flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN) )
779 if( !hrgnVisible ) hrgnVisible = CreateRectRgn( 0, 0, 0, 0 );
781 dce->DCXflags |= flags & (DCX_KEEPCLIPRGN | DCX_INTERSECTRGN | DCX_EXCLUDERGN);
782 dce->hClipRgn = hrgnClip;
784 TRACE(dc, "\tsaved VisRgn, clipRgn = %04x\n", hrgnClip);
786 SaveVisRgn16( hdc );
787 CombineRgn( hrgnVisible, hrgnClip, 0, RGN_COPY );
788 DCE_OffsetVisRgn( hdc, hrgnVisible );
789 CombineRgn( hrgnVisible, InquireVisRgn16( hdc ), hrgnVisible,
790 (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
791 SelectVisRgn16( hdc, hrgnVisible );
794 if( hrgnVisible ) DeleteObject( hrgnVisible );
796 TRACE(dc, "(%04x,%04x,0x%lx): returning %04x\n",
797 hwnd, hrgnClip, flags, hdc);
798 return hdc;
802 /***********************************************************************
803 * GetDC16 (USER.66)
805 HDC16 WINAPI GetDC16( HWND16 hwnd )
807 return (HDC16)GetDC( hwnd );
811 /***********************************************************************
812 * GetDC32 (USER32.230)
813 * RETURNS
814 * :Handle to DC
815 * NULL: Failure
817 HDC WINAPI GetDC(
818 HWND hwnd /* handle of window */
820 if (!hwnd)
821 return GetDCEx( GetDesktopWindow(), 0, DCX_CACHE | DCX_WINDOW );
822 return GetDCEx( hwnd, 0, DCX_USESTYLE );
826 /***********************************************************************
827 * GetWindowDC16 (USER.67)
829 HDC16 WINAPI GetWindowDC16( HWND16 hwnd )
831 if (!hwnd) hwnd = GetDesktopWindow16();
832 return GetDCEx16( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
836 /***********************************************************************
837 * GetWindowDC32 (USER32.304)
839 HDC WINAPI GetWindowDC( HWND hwnd )
841 if (!hwnd) hwnd = GetDesktopWindow();
842 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
846 /***********************************************************************
847 * ReleaseDC16 (USER.68)
849 INT16 WINAPI ReleaseDC16( HWND16 hwnd, HDC16 hdc )
851 return (INT16)ReleaseDC( hwnd, hdc );
855 /***********************************************************************
856 * ReleaseDC32 (USER32.440)
858 * RETURNS
859 * 1: Success
860 * 0: Failure
862 INT WINAPI ReleaseDC(
863 HWND hwnd /* Handle of window - ignored */,
864 HDC hdc /* Handle of device context */
866 DCE * dce = firstDCE;
868 TRACE(dc, "%04x %04x\n", hwnd, hdc );
870 while (dce && (dce->hDC != hdc)) dce = dce->next;
872 if ( dce )
873 if ( dce->DCXflags & DCX_DCEBUSY )
874 return DCE_ReleaseDC( dce );
875 return 0;
878 /***********************************************************************
879 * DCHook (USER.362)
881 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
883 BOOL16 WINAPI DCHook16( HDC16 hDC, WORD code, DWORD data, LPARAM lParam )
885 HRGN hVisRgn;
886 DCE *dce = firstDCE;;
888 TRACE(dc,"hDC = %04x, %i\n", hDC, code);
890 while (dce && (dce->hDC != hDC)) dce = dce->next;
891 if (!dce) return 0;
893 switch( code )
895 case DCHC_INVALIDVISRGN:
897 /* GDI code calls this when it detects that the
898 * DC is dirty (usually after SetHookFlags()). This
899 * means that we have to recompute the visible region.
902 if( dce->DCXflags & DCX_DCEBUSY )
904 SetHookFlags16(hDC, DCHF_VALIDATEVISRGN);
905 hVisRgn = DCE_GetVisRgn(dce->hwndCurrent, dce->DCXflags, 0, 0);
907 TRACE(dc,"\tapplying saved clipRgn\n");
909 /* clip this region with saved clipping region */
911 if ( (dce->DCXflags & DCX_INTERSECTRGN && dce->hClipRgn != 1) ||
912 ( dce->DCXflags & DCX_EXCLUDERGN && dce->hClipRgn) )
915 if( (!dce->hClipRgn && dce->DCXflags & DCX_INTERSECTRGN) ||
916 (dce->hClipRgn == 1 && dce->DCXflags & DCX_EXCLUDERGN) )
917 SetRectRgn(hVisRgn,0,0,0,0);
918 else
919 CombineRgn(hVisRgn, hVisRgn, dce->hClipRgn,
920 (dce->DCXflags & DCX_EXCLUDERGN)? RGN_DIFF:RGN_AND);
922 dce->DCXflags &= ~DCX_DCEDIRTY;
923 DCE_OffsetVisRgn( hDC, hVisRgn );
924 SelectVisRgn16(hDC, hVisRgn);
925 DeleteObject( hVisRgn );
927 else /* non-fatal but shouldn't happen */
928 WARN(dc, "DC is not in use!\n");
929 break;
931 case DCHC_DELETEDC: /* FIXME: ?? */
932 break;
934 default:
935 FIXME(dc,"unknown code\n");
937 return 0;
941 /**********************************************************************
942 * WindowFromDC16 (USER.117)
944 HWND16 WINAPI WindowFromDC16( HDC16 hDC )
946 return (HWND16)WindowFromDC( hDC );
950 /**********************************************************************
951 * WindowFromDC32 (USER32.581)
953 HWND WINAPI WindowFromDC( HDC hDC )
955 DCE *dce = firstDCE;
956 while (dce && (dce->hDC != hDC)) dce = dce->next;
957 return dce ? dce->hwndCurrent : 0;
961 /***********************************************************************
962 * LockWindowUpdate16 (USER.294)
964 BOOL16 WINAPI LockWindowUpdate16( HWND16 hwnd )
966 return LockWindowUpdate( hwnd );
970 /***********************************************************************
971 * LockWindowUpdate32 (USER32.378)
973 BOOL WINAPI LockWindowUpdate( HWND hwnd )
975 /* FIXME? DCX_LOCKWINDOWUPDATE is unimplemented */
976 return TRUE;