Release 961023
[wine/multimedia.git] / windows / dce.c
blob0af441d24c964a8bcf07bd0de9fbffdd5fa8194a
1 /*
2 * USER DCE functions
4 * Copyright 1993 Alexandre Julliard
5 * 1996 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_DCEBUSY - dce structure is in use
14 * DCX_KEEPCLIPRGN - do not delete clipping region in ReleaseDC
15 * DCX_WINDOWPAINT - BeginPaint specific flag
18 #include "dce.h"
19 #include "class.h"
20 #include "win.h"
21 #include "gdi.h"
22 #include "heap.h"
23 #include "sysmetrics.h"
24 #include "stddebug.h"
25 /* #define DEBUG_DC */
26 #include "debug.h"
28 #define NB_DCE 5 /* Number of DCEs created at startup */
30 static DCE *firstDCE = 0;
31 static HDC32 defaultDCstate = 0;
33 /***********************************************************************
34 * DCE_AllocDCE
36 * Allocate a new DCE.
38 DCE *DCE_AllocDCE( HWND32 hWnd, DCE_TYPE type )
40 DCE * dce;
41 if (!(dce = HeapAlloc( SystemHeap, 0, sizeof(DCE) ))) return NULL;
42 if (!(dce->hDC = CreateDC( "DISPLAY", NULL, NULL, NULL )))
44 HeapFree( SystemHeap, 0, dce );
45 return 0;
48 /* store DCE handle in DC hook data field */
50 SetDCHook( dce->hDC, (FARPROC16)DCHook, (DWORD)dce );
52 dce->hwndCurrent = hWnd;
53 dce->hClipRgn = 0;
54 dce->next = firstDCE;
55 firstDCE = dce;
57 if( type != DCE_CACHE_DC )
59 dce->DCXflags = DCX_DCEBUSY;
60 if( hWnd )
62 WND* wnd = WIN_FindWndPtr(hWnd);
64 if( wnd->dwStyle & WS_CLIPCHILDREN ) dce->DCXflags |= DCX_CLIPCHILDREN;
65 if( wnd->dwStyle & WS_CLIPSIBLINGS ) dce->DCXflags |= DCX_CLIPSIBLINGS;
67 SetHookFlags(dce->hDC,DCHF_INVALIDATEVISRGN);
69 else dce->DCXflags = DCX_CACHE;
71 return dce;
75 /***********************************************************************
76 * DCE_FreeDCE
78 void DCE_FreeDCE( DCE *dce )
80 DCE **ppDCE = &firstDCE;
82 if (!dce) return;
83 while (*ppDCE && (*ppDCE != dce)) ppDCE = &(*ppDCE)->next;
84 if (*ppDCE == dce) *ppDCE = dce->next;
86 SetDCHook(dce->hDC, NULL, 0L);
88 DeleteDC( dce->hDC );
89 if( dce->hClipRgn && !(dce->DCXflags & DCX_KEEPCLIPRGN) )
90 DeleteObject(dce->hClipRgn);
91 HeapFree( SystemHeap, 0, dce );
95 /**********************************************************************
96 * WindowFromDC16 (USER32.580)
98 HWND16 WindowFromDC16( HDC16 hDC )
100 return (HWND16)WindowFromDC32( hDC );
104 /**********************************************************************
105 * WindowFromDC32 (USER32.580)
107 HWND32 WindowFromDC32( HDC32 hDC )
109 DCE *dce = firstDCE;
110 while (dce && (dce->hDC != hDC)) dce = dce->next;
111 return dce ? dce->hwndCurrent : 0;
115 /***********************************************************************
116 * DCE_InvalidateDCE
118 * It is called from SetWindowPos - we have to invalidate all busy
119 * DCE's for windows whose client rect intersects with update rectangle
121 BOOL32 DCE_InvalidateDCE(WND* wndScope, RECT16* pRectUpdate)
123 BOOL32 bRet = FALSE;
124 DCE *dce;
126 if( !wndScope ) return 0;
128 dprintf_dc(stddeb,"InvalidateDCE: scope hwnd = %04x, (%i,%i - %i,%i)\n",
129 wndScope->hwndSelf, pRectUpdate->left,pRectUpdate->top,
130 pRectUpdate->right,pRectUpdate->bottom);
131 /* walk all DCE's */
133 for (dce = firstDCE; (dce); dce = dce->next)
135 if( dce->DCXflags & DCX_DCEBUSY )
137 WND * wndCurrent, * wnd;
139 wnd = wndCurrent = WIN_FindWndPtr(dce->hwndCurrent);
141 /* desktop is not critical (DC is not owned anyway) */
143 if( wnd == WIN_GetDesktop() ) continue;
145 /* check if DCE window is within z-order scope */
147 for( ; wnd ; wnd = wnd->parent )
148 if( wnd == wndScope )
150 RECT16 wndRect = wndCurrent->rectWindow;
152 dprintf_dc(stddeb,"\tgot hwnd %04x\n", wndCurrent->hwndSelf);
154 MapWindowPoints16(wndCurrent->parent->hwndSelf, wndScope->hwndSelf,
155 (LPPOINT16)&wndRect, 2);
156 if( IntersectRect16(&wndRect,&wndRect,pRectUpdate) )
158 SetHookFlags(dce->hDC, DCHF_INVALIDATEVISRGN);
159 bRet = TRUE;
161 break;
165 return bRet;
168 /***********************************************************************
169 * DCE_Init
171 void DCE_Init()
173 int i;
174 DCE * dce;
176 for (i = 0; i < NB_DCE; i++)
178 if (!(dce = DCE_AllocDCE( 0, DCE_CACHE_DC ))) return;
179 if (!defaultDCstate) defaultDCstate = GetDCState( dce->hDC );
184 /***********************************************************************
185 * DCE_GetVisRect
187 * Calc the visible rectangle of a window, i.e. the client or
188 * window area clipped by the client area of all ancestors.
189 * Return FALSE if the visible region is empty.
191 static BOOL DCE_GetVisRect( WND *wndPtr, BOOL clientArea, RECT16 *lprect )
193 int xoffset, yoffset;
195 *lprect = clientArea ? wndPtr->rectClient : wndPtr->rectWindow;
196 xoffset = lprect->left;
197 yoffset = lprect->top;
199 if (!(wndPtr->dwStyle & WS_VISIBLE) || (wndPtr->flags & WIN_NO_REDRAW))
201 SetRectEmpty16( lprect ); /* Clip everything */
202 return FALSE;
205 while (wndPtr->parent)
207 wndPtr = wndPtr->parent;
208 if (!(wndPtr->dwStyle & WS_VISIBLE) ||
209 (wndPtr->flags & WIN_NO_REDRAW) ||
210 (wndPtr->dwStyle & WS_ICONIC))
212 SetRectEmpty16( lprect ); /* Clip everything */
213 return FALSE;
215 xoffset += wndPtr->rectClient.left;
216 yoffset += wndPtr->rectClient.top;
217 OffsetRect16( lprect, wndPtr->rectClient.left,
218 wndPtr->rectClient.top );
220 /* Warning!! we assume that IntersectRect() handles the case */
221 /* where the destination is the same as one of the sources. */
222 if (!IntersectRect16( lprect, lprect, &wndPtr->rectClient ))
223 return FALSE; /* Visible rectangle is empty */
225 OffsetRect16( lprect, -xoffset, -yoffset );
226 return TRUE;
230 /***********************************************************************
231 * DCE_ClipWindows
233 * Go through the linked list of windows from hwndStart to hwndEnd,
234 * removing from the given region the rectangle of each window offset
235 * by a given amount. The new region is returned, and the original one
236 * is destroyed. Used to implement DCX_CLIPSIBLINGS and
237 * DCX_CLIPCHILDREN styles.
239 static HRGN32 DCE_ClipWindows( WND *pWndStart, WND *pWndEnd,
240 HRGN32 hrgn, int xoffset, int yoffset )
242 HRGN32 hrgnNew;
244 if (!pWndStart) return hrgn;
245 if (!(hrgnNew = CreateRectRgn( 0, 0, 0, 0 )))
247 DeleteObject( hrgn );
248 return 0;
250 for (; pWndStart != pWndEnd; pWndStart = pWndStart->next)
252 if (!(pWndStart->dwStyle & WS_VISIBLE)) continue;
253 SetRectRgn( hrgnNew, pWndStart->rectWindow.left + xoffset,
254 pWndStart->rectWindow.top + yoffset,
255 pWndStart->rectWindow.right + xoffset,
256 pWndStart->rectWindow.bottom + yoffset );
257 if (!CombineRgn( hrgn, hrgn, hrgnNew, RGN_DIFF )) break;
259 DeleteObject( hrgnNew );
260 if (pWndStart != pWndEnd) /* something went wrong */
262 DeleteObject( hrgn );
263 return 0;
265 return hrgn;
269 /***********************************************************************
270 * DCE_GetVisRgn
272 * Return the visible region of a window, i.e. the client or window area
273 * clipped by the client area of all ancestors, and then optionally
274 * by siblings and children.
276 HRGN32 DCE_GetVisRgn( HWND hwnd, WORD flags )
278 RECT16 rect;
279 HRGN32 hrgn;
280 int xoffset, yoffset;
281 WND *wndPtr = WIN_FindWndPtr( hwnd );
283 /* Get visible rectangle and create a region with it.
284 * do we really need to calculate vis rgns for X windows?
285 * - yes, to clip child windows.
288 if (!wndPtr || !DCE_GetVisRect( wndPtr, !(flags & DCX_WINDOW), &rect ))
290 return CreateRectRgn( 0, 0, 0, 0 ); /* Visible region is empty */
292 if (!(hrgn = CreateRectRgnIndirect16( &rect ))) return 0;
294 /* Clip all children from the visible region */
296 if (flags & DCX_CLIPCHILDREN)
298 if (flags & DCX_WINDOW)
300 xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
301 yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
303 else xoffset = yoffset = 0;
304 hrgn = DCE_ClipWindows( wndPtr->child, NULL, hrgn, xoffset, yoffset );
305 if (!hrgn) return 0;
308 /* Clip siblings placed above this window */
310 if (flags & DCX_WINDOW)
312 xoffset = -wndPtr->rectWindow.left;
313 yoffset = -wndPtr->rectWindow.top;
315 else
317 xoffset = -wndPtr->rectClient.left;
318 yoffset = -wndPtr->rectClient.top;
320 if (flags & DCX_CLIPSIBLINGS)
322 hrgn = DCE_ClipWindows( wndPtr->parent ? wndPtr->parent->child : NULL,
323 wndPtr, hrgn, xoffset, yoffset );
324 if (!hrgn) return 0;
327 /* Clip siblings of all ancestors that have the WS_CLIPSIBLINGS style */
329 while (wndPtr->dwStyle & WS_CHILD)
331 wndPtr = wndPtr->parent;
332 xoffset -= wndPtr->rectClient.left;
333 yoffset -= wndPtr->rectClient.top;
334 hrgn = DCE_ClipWindows( wndPtr->parent->child, wndPtr,
335 hrgn, xoffset, yoffset );
336 if (!hrgn) return 0;
338 return hrgn;
342 /***********************************************************************
343 * DCE_SetDrawable
345 * Set the drawable, origin and dimensions for the DC associated to
346 * a given window.
348 static void DCE_SetDrawable( WND *wndPtr, DC *dc, WORD flags )
350 if (!wndPtr) /* Get a DC for the whole screen */
352 dc->w.DCOrgX = 0;
353 dc->w.DCOrgY = 0;
354 dc->u.x.drawable = rootWindow;
355 XSetSubwindowMode( display, dc->u.x.gc, IncludeInferiors );
357 else
359 if (flags & DCX_WINDOW)
361 dc->w.DCOrgX = wndPtr->rectWindow.left;
362 dc->w.DCOrgY = wndPtr->rectWindow.top;
364 else
366 dc->w.DCOrgX = wndPtr->rectClient.left;
367 dc->w.DCOrgY = wndPtr->rectClient.top;
369 while (!wndPtr->window)
371 wndPtr = wndPtr->parent;
372 dc->w.DCOrgX += wndPtr->rectClient.left;
373 dc->w.DCOrgY += wndPtr->rectClient.top;
375 dc->w.DCOrgX -= wndPtr->rectWindow.left;
376 dc->w.DCOrgY -= wndPtr->rectWindow.top;
377 dc->u.x.drawable = wndPtr->window;
382 /***********************************************************************
383 * GetDCEx16 (USER.359)
385 HDC16 GetDCEx16( HWND16 hwnd, HRGN16 hrgnClip, DWORD flags )
387 return (HDC16)GetDCEx32( hwnd, hrgnClip, flags );
391 /***********************************************************************
392 * GetDCEx32 (USER32.230)
394 * Unimplemented flags: DCX_LOCKWINDOWUPDATE
396 HDC32 GetDCEx32( HWND32 hwnd, HRGN32 hrgnClip, DWORD flags )
398 HRGN32 hrgnVisible;
399 HDC32 hdc = 0;
400 DCE * dce;
401 DC * dc;
402 WND * wndPtr;
403 DWORD dcx_flags = 0;
404 BOOL need_update = TRUE;
406 dprintf_dc(stddeb,"GetDCEx: hwnd %04x, hrgnClip %04x, flags %08x\n", hwnd, hrgnClip, (unsigned)flags);
408 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
410 if (flags & DCX_USESTYLE)
412 flags &= ~( DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
414 if( wndPtr->dwStyle & WS_CLIPSIBLINGS )
415 flags |= DCX_CLIPSIBLINGS;
417 if ( !(flags & DCX_WINDOW) )
419 if (!(wndPtr->class->style & (CS_OWNDC | CS_CLASSDC)))
420 flags |= DCX_CACHE;
422 if (wndPtr->class->style & CS_PARENTDC) flags |= DCX_PARENTCLIP;
424 if (wndPtr->dwStyle & WS_CLIPCHILDREN &&
425 !(wndPtr->dwStyle & WS_MINIMIZE) ) flags |= DCX_CLIPCHILDREN;
427 else flags |= DCX_CACHE;
430 if( flags & DCX_NOCLIPCHILDREN )
432 flags |= DCX_CACHE;
433 flags &= ~(DCX_PARENTCLIP | DCX_CLIPCHILDREN);
436 if (hwnd == GetDesktopWindow32() || !(wndPtr->dwStyle & WS_CHILD))
437 flags &= ~DCX_PARENTCLIP;
439 if (flags & DCX_WINDOW) flags = (flags & ~DCX_CLIPCHILDREN) | DCX_CACHE;
441 if( flags & DCX_PARENTCLIP )
443 flags |= DCX_CACHE;
444 if( !(flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) )
445 if( (wndPtr->dwStyle & WS_VISIBLE) && (wndPtr->parent->dwStyle & WS_VISIBLE) )
447 flags &= ~DCX_CLIPCHILDREN;
448 if( wndPtr->parent->dwStyle & WS_CLIPSIBLINGS )
449 flags |= DCX_CLIPSIBLINGS;
453 if (flags & DCX_CACHE)
455 for (dce = firstDCE; (dce); dce = dce->next)
457 if ((dce->DCXflags & DCX_CACHE) && !(dce->DCXflags & DCX_DCEBUSY)) break;
460 else
462 dce = (wndPtr->class->style & CS_OWNDC)?wndPtr->dce:wndPtr->class->dce;
463 if( dce->hwndCurrent == hwnd )
465 dprintf_dc(stddeb,"\tskipping hVisRgn update\n");
466 need_update = FALSE;
469 if( hrgnClip && dce->hClipRgn && !(dce->DCXflags & DCX_KEEPCLIPRGN))
471 fprintf(stdnimp,"GetDCEx: hClipRgn collision!\n");
472 DeleteObject(dce->hClipRgn);
473 need_update = TRUE;
477 dcx_flags = flags & ( DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_CACHE | DCX_WINDOW | DCX_WINDOWPAINT);
479 if (!dce) return 0;
480 dce->hwndCurrent = hwnd;
481 dce->hClipRgn = 0;
482 dce->DCXflags = dcx_flags | DCX_DCEBUSY;
483 hdc = dce->hDC;
485 if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
487 DCE_SetDrawable( wndPtr, dc, flags );
488 if( need_update || dc->w.flags & DC_DIRTY )
490 dprintf_dc(stddeb,"updating hDC anyway\n");
492 if (flags & DCX_PARENTCLIP)
494 WND *parentPtr = wndPtr->parent;
495 dcx_flags = flags & ~(DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN |
496 DCX_WINDOW);
497 if (parentPtr->dwStyle & WS_CLIPSIBLINGS)
498 dcx_flags |= DCX_CLIPSIBLINGS;
499 hrgnVisible = DCE_GetVisRgn( parentPtr->hwndSelf, dcx_flags );
500 if (flags & DCX_WINDOW)
501 OffsetRgn( hrgnVisible, -wndPtr->rectWindow.left,
502 -wndPtr->rectWindow.top );
503 else OffsetRgn( hrgnVisible, -wndPtr->rectClient.left,
504 -wndPtr->rectClient.top );
506 /* optimize away GetVisRgn for desktop if it isn't there */
508 else if ((hwnd == GetDesktopWindow32()) &&
509 (rootWindow == DefaultRootWindow(display)))
510 hrgnVisible = CreateRectRgn( 0, 0, SYSMETRICS_CXSCREEN,
511 SYSMETRICS_CYSCREEN);
512 else hrgnVisible = DCE_GetVisRgn( hwnd, flags );
514 if( wndPtr->parent && wndPtr->window )
516 WND* wnd = wndPtr->parent->child;
517 RECT16 rect;
519 for( ; wnd != wndPtr; wnd = wnd->next )
520 if( wnd->class->style & CS_SAVEBITS &&
521 wnd->dwStyle & WS_VISIBLE &&
522 IntersectRect16(&rect, &wndPtr->rectClient, &wnd->rectClient) )
523 wnd->flags |= WIN_SAVEUNDER_OVERRIDE;
526 dc->w.flags &= ~DC_DIRTY;
527 SelectVisRgn( hdc, hrgnVisible );
529 else hrgnVisible = CreateRectRgn(0,0,0,0);
531 if ((flags & DCX_INTERSECTRGN) || (flags & DCX_EXCLUDERGN))
533 dce->DCXflags |= flags & (DCX_KEEPCLIPRGN | DCX_INTERSECTRGN | DCX_EXCLUDERGN);
534 dce->hClipRgn = hrgnClip;
536 dprintf_dc(stddeb, "\tsaved VisRgn, clipRgn = %04x\n", hrgnClip);
538 SaveVisRgn( hdc );
539 CombineRgn( hrgnVisible, InquireVisRgn( hdc ), hrgnClip,
540 (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
541 SelectVisRgn( hdc, hrgnVisible );
543 DeleteObject( hrgnVisible );
545 dprintf_dc(stddeb, "GetDCEx(%04x,%04x,0x%lx): returning %04x\n",
546 hwnd, hrgnClip, flags, hdc);
547 return hdc;
551 /***********************************************************************
552 * GetDC16 (USER.66)
554 HDC16 GetDC16( HWND16 hwnd )
556 return (HDC16)GetDC32( hwnd );
560 /***********************************************************************
561 * GetDC32 (USER32.229)
563 HDC32 GetDC32( HWND32 hwnd )
565 if (!hwnd)
566 return GetDCEx32( GetDesktopWindow32(), 0, DCX_CACHE | DCX_WINDOW );
567 return GetDCEx32( hwnd, 0, DCX_USESTYLE );
571 /***********************************************************************
572 * GetWindowDC16 (USER.67)
574 HDC16 GetWindowDC16( HWND16 hwnd )
576 if (!hwnd) hwnd = GetDesktopWindow16();
577 return GetDCEx16( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
581 /***********************************************************************
582 * GetWindowDC32 (USER32.)
584 HDC32 GetWindowDC32( HWND32 hwnd )
586 if (!hwnd) hwnd = GetDesktopWindow32();
587 return GetDCEx32( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
591 /***********************************************************************
592 * ReleaseDC16 (USER.68)
594 INT16 ReleaseDC16( HWND16 hwnd, HDC16 hdc )
596 return (INT32)ReleaseDC32( hwnd, hdc );
600 /***********************************************************************
601 * ReleaseDC32 (USER32.439)
603 INT32 ReleaseDC32( HWND32 hwnd, HDC32 hdc )
605 DCE * dce = firstDCE;
607 dprintf_dc(stddeb, "ReleaseDC: %04x %04x\n", hwnd, hdc );
609 while (dce && (dce->hDC != hdc)) dce = dce->next;
610 if (!dce) return 0;
611 if (!(dce->DCXflags & DCX_DCEBUSY) ) return 0;
613 /* restore previous visible region */
615 if ( dce->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN) &&
616 (dce->DCXflags & DCX_CACHE || dce->DCXflags & DCX_WINDOWPAINT) )
618 dprintf_dc(stddeb,"\tcleaning up visrgn...\n");
619 dce->DCXflags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN | DCX_WINDOWPAINT);
621 if( dce->DCXflags & DCX_KEEPCLIPRGN )
622 dce->DCXflags &= ~DCX_KEEPCLIPRGN;
623 else
624 if( dce->hClipRgn > 1 )
625 DeleteObject( dce->hClipRgn );
627 dce->hClipRgn = 0;
628 RestoreVisRgn(dce->hDC);
631 if (dce->DCXflags & DCX_CACHE)
633 SetDCState( dce->hDC, defaultDCstate );
634 dce->DCXflags = DCX_CACHE;
635 dce->hwndCurrent = 0;
637 return 1;
640 /***********************************************************************
641 * DCHook (USER.362)
643 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
645 BOOL16 DCHook( HDC16 hDC, WORD code, DWORD data, LPARAM lParam )
647 HRGN32 hVisRgn;
648 DCE *dce = firstDCE;;
650 dprintf_dc(stddeb,"DCHook: hDC = %04x, %i\n", hDC, code);
652 while (dce && (dce->hDC != hDC)) dce = dce->next;
653 if (!dce) return 0;
655 switch( code )
657 case DCHC_INVALIDVISRGN:
659 if( dce->DCXflags & DCX_DCEBUSY )
661 SetHookFlags(hDC, DCHF_VALIDATEVISRGN);
662 hVisRgn = DCE_GetVisRgn(dce->hwndCurrent, dce->DCXflags);
664 dprintf_dc(stddeb,"\tapplying saved clipRgn\n");
666 /* clip this region with saved clipping region */
668 if ( (dce->DCXflags & DCX_INTERSECTRGN && dce->hClipRgn != 1) ||
669 ( dce->DCXflags & DCX_EXCLUDERGN && dce->hClipRgn) )
672 if( (!dce->hClipRgn && dce->DCXflags & DCX_INTERSECTRGN) ||
673 (dce->hClipRgn == 1 && dce->DCXflags & DCX_EXCLUDERGN) )
674 SetRectRgn(hVisRgn,0,0,0,0);
675 else
676 CombineRgn(hVisRgn, hVisRgn, dce->hClipRgn,
677 (dce->DCXflags & DCX_EXCLUDERGN)? RGN_DIFF:RGN_AND);
679 SelectVisRgn(hDC, hVisRgn);
680 DeleteObject(hVisRgn);
682 else
683 dprintf_dc(stddeb,"DCHook: DC is not in use!\n");
685 break;
687 case DCHC_DELETEDC: /* FIXME: ?? */
688 break;
690 default:
691 fprintf(stdnimp,"DCHook: unknown code\n");
693 return 0;