Release 960623
[wine.git] / windows / dce.c
blobf334bb9a706dc08780144af02e71309f8c53952a
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 "options.h"
19 #include "dce.h"
20 #include "class.h"
21 #include "win.h"
22 #include "gdi.h"
23 #include "user.h"
24 #include "sysmetrics.h"
25 #include "stddebug.h"
26 /* #define DEBUG_DC */
27 #include "debug.h"
29 #define NB_DCE 5 /* Number of DCEs created at startup */
31 static HANDLE firstDCE = 0;
32 static HDC defaultDCstate = 0;
34 BOOL DCHook(HDC, WORD, DWORD, DWORD);
36 /***********************************************************************
37 * DCE_AllocDCE
39 * Allocate a new DCE.
41 HANDLE DCE_AllocDCE( HWND hWnd, DCE_TYPE type )
43 DCE * dce;
44 HANDLE handle = USER_HEAP_ALLOC( sizeof(DCE) );
45 if (!handle) return 0;
46 dce = (DCE *) USER_HEAP_LIN_ADDR( handle );
47 if (!(dce->hDC = CreateDC( "DISPLAY", NULL, NULL, NULL )))
49 USER_HEAP_FREE( handle );
50 return 0;
53 /* store DCE handle in DC hook data field */
55 SetDCHook(dce->hDC, GDI_GetDefDCHook(), MAKELONG(handle,DC_MAGIC));
57 dce->hwndCurrent = hWnd;
58 dce->hNext = firstDCE;
59 dce->hClipRgn = 0;
60 firstDCE = handle;
62 if( type != DCE_CACHE_DC )
64 dce->DCXflags = DCX_DCEBUSY;
65 if( hWnd )
67 WND* wnd = WIN_FindWndPtr(hWnd);
69 if( wnd->dwStyle & WS_CLIPCHILDREN ) dce->DCXflags |= DCX_CLIPCHILDREN;
70 if( wnd->dwStyle & WS_CLIPSIBLINGS ) dce->DCXflags |= DCX_CLIPSIBLINGS;
72 SetHookFlags(dce->hDC,DCHF_INVALIDATEVISRGN);
74 else dce->DCXflags = DCX_CACHE;
76 return handle;
80 /***********************************************************************
81 * DCE_FreeDCE
83 void DCE_FreeDCE( HANDLE hdce )
85 DCE * dce;
86 HANDLE *handle = &firstDCE;
88 if (!(dce = (DCE *) USER_HEAP_LIN_ADDR( hdce ))) return;
89 while (*handle && (*handle != hdce))
91 DCE * prev = (DCE *) USER_HEAP_LIN_ADDR( *handle );
92 handle = &prev->hNext;
94 if (*handle == hdce) *handle = dce->hNext;
96 SetDCHook(dce->hDC,(SEGPTR)NULL,0L);
98 DeleteDC( dce->hDC );
99 if( dce->hClipRgn && !(dce->DCXflags & DCX_KEEPCLIPRGN) )
100 DeleteObject(dce->hClipRgn);
101 USER_HEAP_FREE( hdce );
104 /***********************************************************************
105 * DCE_FindDCE
107 HANDLE DCE_FindDCE(HDC hDC)
109 HANDLE hdce = firstDCE;
110 DCE* dce;
112 while( hdce )
114 dce = (DCE *) USER_HEAP_LIN_ADDR(hdce);
115 if( dce->hDC == hDC ) break;
116 hdce = dce->hNext;
118 return hdce;
121 /***********************************************************************
122 * DCE_InvalidateDCE
124 * It is called from SetWindowPos - we have to invalidate all busy
125 * DCE's for windows whose client rect intersects with update rectangle
127 BOOL DCE_InvalidateDCE(WND* wndScope, RECT16* pRectUpdate)
129 HANDLE hdce;
130 DCE* dce;
132 if( !wndScope ) return 0;
134 dprintf_dc(stddeb,"InvalidateDCE: scope hwnd = %04x, (%i,%i - %i,%i)\n",
135 wndScope->hwndSelf, pRectUpdate->left,pRectUpdate->top,
136 pRectUpdate->right,pRectUpdate->bottom);
137 /* walk all DCE's */
139 for( hdce = firstDCE; (hdce); hdce=dce->hNext)
141 dce = (DCE*)USER_HEAP_LIN_ADDR(hdce);
143 if( dce->DCXflags & DCX_DCEBUSY )
145 WND * wndCurrent, * wnd;
147 wnd = wndCurrent = WIN_FindWndPtr(dce->hwndCurrent);
149 /* desktop is not critical (DC is not owned anyway) */
151 if( wnd == WIN_GetDesktop() ) continue;
153 /* check if DCE window is within z-order scope */
155 for( ; wnd ; wnd = wnd->parent )
156 if( wnd == wndScope )
158 RECT16 wndRect = wndCurrent->rectWindow;
160 dprintf_dc(stddeb,"\tgot hwnd %04x\n", wndCurrent->hwndSelf);
162 MapWindowPoints16(wndCurrent->parent->hwndSelf, wndScope->hwndSelf,
163 (LPPOINT16)&wndRect, 2);
164 if( IntersectRect16(&wndRect,&wndRect,pRectUpdate) )
165 SetHookFlags(dce->hDC, DCHF_INVALIDATEVISRGN);
166 break;
170 return 1;
173 /***********************************************************************
174 * DCE_Init
176 void DCE_Init()
178 int i;
179 HANDLE handle;
180 DCE * dce;
182 for (i = 0; i < NB_DCE; i++)
184 if (!(handle = DCE_AllocDCE( 0, DCE_CACHE_DC ))) return;
185 dce = (DCE *) USER_HEAP_LIN_ADDR( handle );
186 if (!defaultDCstate) defaultDCstate = GetDCState( dce->hDC );
191 /***********************************************************************
192 * DCE_GetVisRect
194 * Calc the visible rectangle of a window, i.e. the client or
195 * window area clipped by the client area of all ancestors.
196 * Return FALSE if the visible region is empty.
198 static BOOL DCE_GetVisRect( WND *wndPtr, BOOL clientArea, RECT16 *lprect )
200 int xoffset, yoffset;
202 *lprect = clientArea ? wndPtr->rectClient : wndPtr->rectWindow;
203 xoffset = lprect->left;
204 yoffset = lprect->top;
206 if (!(wndPtr->dwStyle & WS_VISIBLE) || (wndPtr->flags & WIN_NO_REDRAW))
208 SetRectEmpty16( lprect ); /* Clip everything */
209 return FALSE;
212 while (wndPtr->parent)
214 wndPtr = wndPtr->parent;
215 if (!(wndPtr->dwStyle & WS_VISIBLE) ||
216 (wndPtr->flags & WIN_NO_REDRAW) ||
217 (wndPtr->dwStyle & WS_ICONIC))
219 SetRectEmpty16( lprect ); /* Clip everything */
220 return FALSE;
222 xoffset += wndPtr->rectClient.left;
223 yoffset += wndPtr->rectClient.top;
224 OffsetRect16( lprect, wndPtr->rectClient.left,
225 wndPtr->rectClient.top );
227 /* Warning!! we assume that IntersectRect() handles the case */
228 /* where the destination is the same as one of the sources. */
229 if (!IntersectRect16( lprect, lprect, &wndPtr->rectClient ))
230 return FALSE; /* Visible rectangle is empty */
232 OffsetRect16( lprect, -xoffset, -yoffset );
233 return TRUE;
237 /***********************************************************************
238 * DCE_ClipWindows
240 * Go through the linked list of windows from hwndStart to hwndEnd,
241 * removing from the given region the rectangle of each window offset
242 * by a given amount. The new region is returned, and the original one
243 * is destroyed. Used to implement DCX_CLIPSIBLINGS and
244 * DCX_CLIPCHILDREN styles.
246 static HRGN DCE_ClipWindows( WND *pWndStart, WND *pWndEnd,
247 HRGN hrgn, int xoffset, int yoffset )
249 HRGN hrgnNew;
251 if (!pWndStart) return hrgn;
252 if (!(hrgnNew = CreateRectRgn( 0, 0, 0, 0 )))
254 DeleteObject( hrgn );
255 return 0;
257 for (; pWndStart != pWndEnd; pWndStart = pWndStart->next)
259 if (!(pWndStart->dwStyle & WS_VISIBLE)) continue;
260 SetRectRgn( hrgnNew, pWndStart->rectWindow.left + xoffset,
261 pWndStart->rectWindow.top + yoffset,
262 pWndStart->rectWindow.right + xoffset,
263 pWndStart->rectWindow.bottom + yoffset );
264 if (!CombineRgn( hrgn, hrgn, hrgnNew, RGN_DIFF )) break;
266 DeleteObject( hrgnNew );
267 if (pWndStart != pWndEnd) /* something went wrong */
269 DeleteObject( hrgn );
270 return 0;
272 return hrgn;
276 /***********************************************************************
277 * DCE_GetVisRgn
279 * Return the visible region of a window, i.e. the client or window area
280 * clipped by the client area of all ancestors, and then optionally
281 * by siblings and children.
283 HRGN DCE_GetVisRgn( HWND hwnd, WORD flags )
285 RECT16 rect;
286 HRGN hrgn;
287 int xoffset, yoffset;
288 WND *wndPtr = WIN_FindWndPtr( hwnd );
290 /* Get visible rectangle and create a region with it
291 * FIXME: do we really need to calculate vis rgns for X windows?
294 if (!wndPtr || !DCE_GetVisRect( wndPtr, !(flags & DCX_WINDOW), &rect ))
296 return CreateRectRgn( 0, 0, 0, 0 ); /* Visible region is empty */
298 if (!(hrgn = CreateRectRgnIndirect16( &rect ))) return 0;
300 /* Clip all children from the visible region */
302 if (flags & DCX_CLIPCHILDREN)
304 if (flags & DCX_WINDOW)
306 xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
307 yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
309 else xoffset = yoffset = 0;
310 hrgn = DCE_ClipWindows( wndPtr->child, NULL, hrgn, xoffset, yoffset );
311 if (!hrgn) return 0;
314 /* Clip siblings placed above this window */
316 if (flags & DCX_WINDOW)
318 xoffset = -wndPtr->rectWindow.left;
319 yoffset = -wndPtr->rectWindow.top;
321 else
323 xoffset = -wndPtr->rectClient.left;
324 yoffset = -wndPtr->rectClient.top;
326 if (flags & DCX_CLIPSIBLINGS)
328 hrgn = DCE_ClipWindows( wndPtr->parent ? wndPtr->parent->child : NULL,
329 wndPtr, hrgn, xoffset, yoffset );
330 if (!hrgn) return 0;
333 /* Clip siblings of all ancestors that have the WS_CLIPSIBLINGS style */
335 while (wndPtr->dwStyle & WS_CHILD)
337 wndPtr = wndPtr->parent;
338 xoffset -= wndPtr->rectClient.left;
339 yoffset -= wndPtr->rectClient.top;
340 hrgn = DCE_ClipWindows( wndPtr->parent->child, wndPtr,
341 hrgn, xoffset, yoffset );
342 if (!hrgn) return 0;
344 return hrgn;
348 /***********************************************************************
349 * DCE_SetDrawable
351 * Set the drawable, origin and dimensions for the DC associated to
352 * a given window.
354 static void DCE_SetDrawable( WND *wndPtr, DC *dc, WORD flags )
356 if (!wndPtr) /* Get a DC for the whole screen */
358 dc->w.DCOrgX = 0;
359 dc->w.DCOrgY = 0;
360 dc->u.x.drawable = rootWindow;
361 XSetSubwindowMode( display, dc->u.x.gc, IncludeInferiors );
363 else
365 if (flags & DCX_WINDOW)
367 dc->w.DCOrgX = wndPtr->rectWindow.left;
368 dc->w.DCOrgY = wndPtr->rectWindow.top;
370 else
372 dc->w.DCOrgX = wndPtr->rectClient.left;
373 dc->w.DCOrgY = wndPtr->rectClient.top;
375 while (!wndPtr->window)
377 wndPtr = wndPtr->parent;
378 dc->w.DCOrgX += wndPtr->rectClient.left;
379 dc->w.DCOrgY += wndPtr->rectClient.top;
381 dc->w.DCOrgX -= wndPtr->rectWindow.left;
382 dc->w.DCOrgY -= wndPtr->rectWindow.top;
383 dc->u.x.drawable = wndPtr->window;
387 /***********************************************************************
388 * GetDCEx (USER.359)
390 * Unimplemented flags: DCX_LOCKWINDOWUPDATE
392 HDC GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
394 HANDLE hdce;
395 HRGN hrgnVisible;
396 HDC hdc = 0;
397 DCE * dce;
398 DC * dc;
399 WND * wndPtr;
400 DWORD dcx_flags = 0;
401 BOOL need_update = TRUE;
403 dprintf_dc(stddeb,"GetDCEx: hwnd %04x, hrgnClip %04x, flags %08x\n", hwnd, hrgnClip, (unsigned)flags);
405 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
407 if (flags & DCX_USESTYLE)
409 flags &= ~( DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
411 if( wndPtr->dwStyle & WS_CLIPSIBLINGS )
412 flags |= DCX_CLIPSIBLINGS;
414 if ( !(flags & DCX_WINDOW) )
416 if (!(wndPtr->class->style & (CS_OWNDC | CS_CLASSDC)))
417 flags |= DCX_CACHE;
419 if (wndPtr->class->style & CS_PARENTDC) flags |= DCX_PARENTCLIP;
421 if (wndPtr->dwStyle & WS_CLIPCHILDREN &&
422 !(wndPtr->dwStyle & WS_MINIMIZE) ) flags |= DCX_CLIPCHILDREN;
424 else flags |= DCX_CACHE;
427 if( flags & DCX_NOCLIPCHILDREN )
429 flags |= DCX_CACHE;
430 flags &= ~(DCX_PARENTCLIP | DCX_CLIPCHILDREN);
433 if (hwnd==GetDesktopWindow() || !(wndPtr->dwStyle & WS_CHILD)) flags &= ~DCX_PARENTCLIP;
435 if (flags & DCX_WINDOW) flags = (flags & ~DCX_CLIPCHILDREN) | DCX_CACHE;
437 if( flags & DCX_PARENTCLIP )
439 flags |= DCX_CACHE;
440 if( !(flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) )
441 if( (wndPtr->dwStyle & WS_VISIBLE) && (wndPtr->parent->dwStyle & WS_VISIBLE) )
443 flags &= ~DCX_CLIPCHILDREN;
444 if( wndPtr->parent->dwStyle & WS_CLIPSIBLINGS )
445 flags |= DCX_CLIPSIBLINGS;
449 if (flags & DCX_CACHE)
451 for (hdce = firstDCE; (hdce); hdce = dce->hNext)
453 if (!(dce = (DCE *) USER_HEAP_LIN_ADDR( hdce ))) return 0;
454 if ((dce->DCXflags & DCX_CACHE) && !(dce->DCXflags & DCX_DCEBUSY)) break;
457 else
459 hdce = (wndPtr->class->style & CS_OWNDC)?wndPtr->hdce:wndPtr->class->hdce;
460 dce = (DCE *) USER_HEAP_LIN_ADDR( hdce );
462 if( dce->hwndCurrent == hwnd )
464 dprintf_dc(stddeb,"\tskipping hVisRgn update\n");
465 need_update = FALSE;
468 if( hrgnClip && dce->hClipRgn && !(dce->DCXflags & DCX_KEEPCLIPRGN))
470 fprintf(stdnimp,"GetDCEx: hClipRgn collision!\n");
471 DeleteObject(dce->hClipRgn);
472 need_update = TRUE;
476 dcx_flags = flags & ( DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_CACHE | DCX_WINDOW | DCX_WINDOWPAINT);
478 if (!hdce) return 0;
479 dce = (DCE *) USER_HEAP_LIN_ADDR( hdce );
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==GetDesktopWindow() && !Options.desktopGeometry )
509 hrgnVisible = CreateRectRgn( 0, 0, SYSMETRICS_CXSCREEN,
510 SYSMETRICS_CYSCREEN);
511 else hrgnVisible = DCE_GetVisRgn( hwnd, flags );
513 dc->w.flags &= ~DC_DIRTY;
515 SelectVisRgn( hdc, hrgnVisible );
517 else hrgnVisible = CreateRectRgn(0,0,0,0);
519 if ((flags & DCX_INTERSECTRGN) || (flags & DCX_EXCLUDERGN))
521 dce->DCXflags |= flags & (DCX_KEEPCLIPRGN | DCX_INTERSECTRGN | DCX_EXCLUDERGN);
522 dce->hClipRgn = hrgnClip;
524 dprintf_dc(stddeb, "\tsaved VisRgn, clipRgn = %04x\n", hrgnClip);
526 SaveVisRgn( hdc );
527 CombineRgn( hrgnVisible, InquireVisRgn( hdc ), hrgnClip,
528 (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
529 SelectVisRgn( hdc, hrgnVisible );
531 DeleteObject( hrgnVisible );
533 dprintf_dc(stddeb, "GetDCEx(%04x,%04x,0x%lx): returning %04x\n",
534 hwnd, hrgnClip, flags, hdc);
535 return hdc;
538 /***********************************************************************
539 * GetDC (USER.66)
541 HDC GetDC( HWND hwnd )
543 if( !hwnd ) return GetDCEx( GetDesktopWindow(), 0, DCX_CACHE | DCX_WINDOW );
545 return GetDCEx( hwnd, 0, DCX_USESTYLE );
549 /***********************************************************************
550 * GetWindowDC (USER.67)
552 HDC GetWindowDC( HWND hwnd )
554 if (hwnd)
556 WND * wndPtr;
557 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
559 else hwnd = GetDesktopWindow();
561 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
565 /***********************************************************************
566 * ReleaseDC (USER.68)
568 int ReleaseDC( HWND hwnd, HDC hdc )
570 HANDLE hdce;
571 DCE * dce = NULL;
573 dprintf_dc(stddeb, "ReleaseDC: %04x %04x\n", hwnd, hdc );
575 for (hdce = firstDCE; (hdce); hdce = dce->hNext)
577 if (!(dce = (DCE *) USER_HEAP_LIN_ADDR( hdce ))) return 0;
578 if (dce->hDC == hdc) break;
580 if (!hdce) return 0;
581 if (!(dce->DCXflags & DCX_DCEBUSY) ) return 0;
583 /* restore previous visible region */
585 if ( dce->DCXflags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN) &&
586 (dce->DCXflags & DCX_CACHE || dce->DCXflags & DCX_WINDOWPAINT) )
588 dprintf_dc(stddeb,"\tcleaning up visrgn...\n");
589 dce->DCXflags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN | DCX_WINDOWPAINT);
591 if( dce->DCXflags & DCX_KEEPCLIPRGN )
592 dce->DCXflags &= ~DCX_KEEPCLIPRGN;
593 else
594 if( dce->hClipRgn > 1 )
595 DeleteObject( dce->hClipRgn );
597 dce->hClipRgn = 0;
598 RestoreVisRgn(dce->hDC);
601 if (dce->DCXflags & DCX_CACHE)
603 SetDCState( dce->hDC, defaultDCstate );
604 dce->DCXflags = DCX_CACHE;
606 return 1;
609 /***********************************************************************
610 * DCHook (USER.362)
612 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
614 BOOL DCHook(HDC hDC, WORD code, DWORD data, DWORD lParam)
616 HANDLE hdce;
617 HRGN hVisRgn;
619 dprintf_dc(stddeb,"DCHook: hDC = %04x, %i\n", hDC, code);
621 if( HIWORD(data) == DC_MAGIC )
622 hdce = (HANDLE)LOWORD(data);
623 else
624 hdce = DCE_FindDCE(hDC);
626 if( !hdce ) return 0;
628 switch( code )
630 case DCHC_INVALIDVISRGN:
632 DCE* dce = (DCE*) USER_HEAP_LIN_ADDR(hdce);
634 if( dce->DCXflags & DCX_DCEBUSY )
636 SetHookFlags(hDC, DCHF_VALIDATEVISRGN);
637 hVisRgn = DCE_GetVisRgn(dce->hwndCurrent, dce->DCXflags);
639 dprintf_dc(stddeb,"\tapplying saved clipRgn\n");
641 /* clip this region with saved clipping region */
643 if ( (dce->DCXflags & DCX_INTERSECTRGN && dce->hClipRgn != 1) ||
644 ( dce->DCXflags & DCX_EXCLUDERGN && dce->hClipRgn) )
647 if( (!dce->hClipRgn && dce->DCXflags & DCX_INTERSECTRGN) ||
648 (dce->hClipRgn == 1 && dce->DCXflags & DCX_EXCLUDERGN) )
649 SetRectRgn(hVisRgn,0,0,0,0);
650 else
651 CombineRgn(hVisRgn, hVisRgn, dce->hClipRgn,
652 (dce->DCXflags & DCX_EXCLUDERGN)? RGN_DIFF:RGN_AND);
654 SelectVisRgn(hDC, hVisRgn);
655 DeleteObject(hVisRgn);
657 else
658 dprintf_dc(stddeb,"DCHook: DC is not in use!\n");
660 break;
662 case DCHC_DELETEDC: /* FIXME: ?? */
663 break;
665 default:
666 fprintf(stdnimp,"DCHook: unknown code\n");
668 return 0;