4 * Copyright 1998,1999 Patrik Stridvall
14 #include "debugtools.h"
16 DEFAULT_DEBUG_CHANNEL(ttydrv);
18 WND_DRIVER TTYDRV_WND_Driver =
20 TTYDRV_WND_ForceWindowRaise
23 #define SWP_AGG_NOGEOMETRYCHANGE \
24 (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE)
25 #define SWP_AGG_NOPOSCHANGE \
26 (SWP_AGG_NOGEOMETRYCHANGE | SWP_NOZORDER)
27 #define SWP_AGG_STATUSFLAGS \
28 (SWP_AGG_NOPOSCHANGE | SWP_FRAMECHANGED | SWP_HIDEWINDOW | SWP_SHOWWINDOW)
30 /**********************************************************************
31 * CreateWindow (TTYDRV.@)
33 BOOL TTYDRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
38 WND *wndPtr = WIN_FindWndPtr( hwnd );
40 INT cellWidth=8, cellHeight=8; /* FIXME: Hardcoded */
42 TRACE("(%x)\n", hwnd);
44 /* Only create top-level windows */
45 if (!(wndPtr->dwStyle & WS_CHILD))
47 if (!wndPtr->parent) /* desktop */
51 int x = wndPtr->rectWindow.left;
52 int y = wndPtr->rectWindow.top;
53 int cx = wndPtr->rectWindow.right - wndPtr->rectWindow.left;
54 int cy = wndPtr->rectWindow.bottom - wndPtr->rectWindow.top;
56 window = subwin( root_window, cy/cellHeight, cx/cellWidth,
57 y/cellHeight, x/cellWidth);
61 wndPtr->pDriverData = window;
63 WIN_ReleaseWndPtr( wndPtr );
64 #else /* defined(WINE_CURSES) */
65 FIXME("(%x): stub\n", hwnd);
66 #endif /* defined(WINE_CURSES) */
70 ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
71 if (ret) ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
75 ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
76 if (ret) ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
81 /***********************************************************************
82 * DestroyWindow (TTYDRV.@)
84 BOOL TTYDRV_DestroyWindow( HWND hwnd )
87 WND *wndPtr = WIN_FindWndPtr( hwnd );
88 WINDOW *window = wndPtr->pDriverData;
90 TRACE("(%x)\n", hwnd);
92 if (window && window != root_window) delwin(window);
93 wndPtr->pDriverData = NULL;
94 WIN_ReleaseWndPtr( wndPtr );
95 #else /* defined(WINE_CURSES) */
96 FIXME("(%x): stub\n", hwnd);
97 #endif /* defined(WINE_CURSES) */
101 /***********************************************************************
102 * TTYDRV_WND_ForceWindowRaise
104 void TTYDRV_WND_ForceWindowRaise(WND *wndPtr)
106 FIXME("(%p): stub\n", wndPtr);
110 /***********************************************************************
113 * Change region from DC-origin relative coordinates to screen coords.
116 static void DCE_OffsetVisRgn( HDC hDC, HRGN hVisRgn )
119 if (!(dc = DC_GetDCPtr( hDC ))) return;
121 OffsetRgn( hVisRgn, dc->DCOrgX, dc->DCOrgY );
123 GDI_ReleaseObj( hDC );
127 /***********************************************************************
130 * Calculate the visible rectangle of a window (i.e. the client or
131 * window area clipped by the client area of all ancestors) in the
132 * corresponding coordinates. Return FALSE if the visible region is empty.
134 static BOOL DCE_GetVisRect( WND *wndPtr, BOOL clientArea, RECT *lprect )
136 *lprect = clientArea ? wndPtr->rectClient : wndPtr->rectWindow;
138 if (wndPtr->dwStyle & WS_VISIBLE)
140 INT xoffset = lprect->left;
141 INT yoffset = lprect->top;
143 while ((wndPtr = WIN_LockWndPtr(wndPtr->parent)))
145 if ( (wndPtr->dwStyle & (WS_ICONIC | WS_VISIBLE)) != WS_VISIBLE )
147 WIN_ReleaseWndPtr(wndPtr);
151 xoffset += wndPtr->rectClient.left;
152 yoffset += wndPtr->rectClient.top;
153 OffsetRect( lprect, wndPtr->rectClient.left,
154 wndPtr->rectClient.top );
156 if( (wndPtr->rectClient.left >= wndPtr->rectClient.right) ||
157 (wndPtr->rectClient.top >= wndPtr->rectClient.bottom) ||
158 (lprect->left >= wndPtr->rectClient.right) ||
159 (lprect->right <= wndPtr->rectClient.left) ||
160 (lprect->top >= wndPtr->rectClient.bottom) ||
161 (lprect->bottom <= wndPtr->rectClient.top) )
163 WIN_ReleaseWndPtr(wndPtr);
167 lprect->left = max( lprect->left, wndPtr->rectClient.left );
168 lprect->right = min( lprect->right, wndPtr->rectClient.right );
169 lprect->top = max( lprect->top, wndPtr->rectClient.top );
170 lprect->bottom = min( lprect->bottom, wndPtr->rectClient.bottom );
172 WIN_ReleaseWndPtr(wndPtr);
174 OffsetRect( lprect, -xoffset, -yoffset );
179 SetRectEmpty( lprect );
184 /***********************************************************************
187 * Go through the linked list of windows from pWndStart to pWndEnd,
188 * adding to the clip region the intersection of the target rectangle
189 * with an offset window rectangle.
191 static BOOL DCE_AddClipRects( WND *pWndStart, WND *pWndEnd,
192 HRGN hrgnClip, LPRECT lpRect, int x, int y )
196 for (WIN_LockWndPtr(pWndStart); (pWndStart && (pWndStart != pWndEnd)); WIN_UpdateWndPtr(&pWndStart,pWndStart->next))
198 if( !(pWndStart->dwStyle & WS_VISIBLE) ) continue;
200 rect.left = pWndStart->rectWindow.left + x;
201 rect.top = pWndStart->rectWindow.top + y;
202 rect.right = pWndStart->rectWindow.right + x;
203 rect.bottom = pWndStart->rectWindow.bottom + y;
205 if( IntersectRect( &rect, &rect, lpRect ))
207 if(!REGION_UnionRectWithRgn( hrgnClip, &rect )) break;
210 WIN_ReleaseWndPtr(pWndStart);
211 return (pWndStart == pWndEnd);
215 /***********************************************************************
218 * Return the visible region of a window, i.e. the client or window area
219 * clipped by the client area of all ancestors, and then optionally
220 * by siblings and children.
222 static HRGN DCE_GetVisRgn( HWND hwnd, WORD flags, HWND hwndChild, WORD cflags )
226 WND *wndPtr = WIN_FindWndPtr( hwnd );
227 WND *childWnd = WIN_FindWndPtr( hwndChild );
229 /* Get visible rectangle and create a region with it. */
231 if (wndPtr && DCE_GetVisRect(wndPtr, !(flags & DCX_WINDOW), &rect))
233 if((hrgnVis = CreateRectRgnIndirect( &rect )))
235 HRGN hrgnClip = CreateRectRgn( 0, 0, 0, 0 );
236 INT xoffset, yoffset;
240 /* Compute obscured region for the visible rectangle by
241 * clipping children, siblings, and ancestors. Note that
242 * DCE_GetVisRect() returns a rectangle either in client
243 * or in window coordinates (for DCX_WINDOW request). */
245 if( (flags & DCX_CLIPCHILDREN) && wndPtr->child )
247 if( flags & DCX_WINDOW )
249 /* adjust offsets since child window rectangles are
250 * in client coordinates */
252 xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
253 yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
256 xoffset = yoffset = 0;
258 DCE_AddClipRects( wndPtr->child, NULL, hrgnClip, &rect, xoffset, yoffset );
261 /* We may need to clip children of child window, if a window with PARENTDC
262 * class style and CLIPCHILDREN window style (like in Free Agent 16
263 * preference dialogs) gets here, we take the region for the parent window
264 * but apparently still need to clip the children of the child window... */
266 if( (cflags & DCX_CLIPCHILDREN) && childWnd && childWnd->child )
268 if( flags & DCX_WINDOW )
270 /* adjust offsets since child window rectangles are
271 * in client coordinates */
273 xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
274 yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
277 xoffset = yoffset = 0;
279 /* client coordinates of child window */
280 xoffset += childWnd->rectClient.left;
281 yoffset += childWnd->rectClient.top;
283 DCE_AddClipRects( childWnd->child, NULL, hrgnClip,
284 &rect, xoffset, yoffset );
287 /* sibling window rectangles are in client
288 * coordinates of the parent window */
290 if (flags & DCX_WINDOW)
292 xoffset = -wndPtr->rectWindow.left;
293 yoffset = -wndPtr->rectWindow.top;
297 xoffset = -wndPtr->rectClient.left;
298 yoffset = -wndPtr->rectClient.top;
301 if (flags & DCX_CLIPSIBLINGS && wndPtr->parent )
302 DCE_AddClipRects( wndPtr->parent->child,
303 wndPtr, hrgnClip, &rect, xoffset, yoffset );
305 /* Clip siblings of all ancestors that have the
306 * WS_CLIPSIBLINGS style
309 while (wndPtr->parent)
311 WIN_UpdateWndPtr(&wndPtr,wndPtr->parent);
312 xoffset -= wndPtr->rectClient.left;
313 yoffset -= wndPtr->rectClient.top;
314 if(wndPtr->dwStyle & WS_CLIPSIBLINGS && wndPtr->parent)
316 DCE_AddClipRects( wndPtr->parent->child, wndPtr,
317 hrgnClip, &rect, xoffset, yoffset );
321 /* Now once we've got a jumbo clip region we have
322 * to substract it from the visible rectangle.
324 CombineRgn( hrgnVis, hrgnVis, hrgnClip, RGN_DIFF );
325 DeleteObject( hrgnClip );
329 DeleteObject( hrgnVis );
335 hrgnVis = CreateRectRgn(0, 0, 0, 0); /* empty */
336 WIN_ReleaseWndPtr(wndPtr);
337 WIN_ReleaseWndPtr(childWnd);
342 /***********************************************************************
345 * Set the drawable, origin and dimensions for the DC associated to
348 BOOL TTYDRV_GetDC( HWND hwnd, HDC hdc, HRGN hrgn, DWORD flags )
350 WND *wndPtr = WIN_FindWndPtr(hwnd);
353 HRGN hrgnVisible = 0;
355 if (!wndPtr) return FALSE;
357 if (!(dc = DC_GetDCPtr( hdc )))
359 WIN_ReleaseWndPtr( wndPtr );
363 if(flags & DCX_WINDOW)
365 dc->DCOrgX = wndPtr->rectWindow.left;
366 dc->DCOrgY = wndPtr->rectWindow.top;
370 dc->DCOrgX = wndPtr->rectClient.left;
371 dc->DCOrgY = wndPtr->rectClient.top;
373 updateVisRgn = (dc->flags & DC_DIRTY) != 0;
374 GDI_ReleaseObj( hdc );
378 if (flags & DCX_PARENTCLIP)
380 WND *parentPtr = WIN_LockWndPtr(wndPtr->parent);
382 if( wndPtr->dwStyle & WS_VISIBLE && !(parentPtr->dwStyle & WS_MINIMIZE) )
386 if( parentPtr->dwStyle & WS_CLIPSIBLINGS )
387 dcxFlags = DCX_CLIPSIBLINGS | (flags & ~(DCX_CLIPCHILDREN | DCX_WINDOW));
389 dcxFlags = flags & ~(DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW);
391 hrgnVisible = DCE_GetVisRgn( parentPtr->hwndSelf, dcxFlags,
392 wndPtr->hwndSelf, flags );
393 if( flags & DCX_WINDOW )
394 OffsetRgn( hrgnVisible, -wndPtr->rectWindow.left,
395 -wndPtr->rectWindow.top );
397 OffsetRgn( hrgnVisible, -wndPtr->rectClient.left,
398 -wndPtr->rectClient.top );
399 DCE_OffsetVisRgn( hdc, hrgnVisible );
402 hrgnVisible = CreateRectRgn( 0, 0, 0, 0 );
403 WIN_ReleaseWndPtr(parentPtr);
407 hrgnVisible = DCE_GetVisRgn( hwnd, flags, 0, 0 );
408 DCE_OffsetVisRgn( hdc, hrgnVisible );
410 SelectVisRgn16( hdc, hrgnVisible );
413 /* apply additional region operation (if any) */
415 if( flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN) )
417 if( !hrgnVisible ) hrgnVisible = CreateRectRgn( 0, 0, 0, 0 );
419 TRACE("\tsaved VisRgn, clipRgn = %04x\n", hrgn);
422 CombineRgn( hrgnVisible, hrgn, 0, RGN_COPY );
423 DCE_OffsetVisRgn( hdc, hrgnVisible );
424 CombineRgn( hrgnVisible, InquireVisRgn16( hdc ), hrgnVisible,
425 (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
426 SelectVisRgn16( hdc, hrgnVisible );
429 if (hrgnVisible) DeleteObject( hrgnVisible );
431 WIN_ReleaseWndPtr( wndPtr );
436 /***********************************************************************
437 * SetWindowPos (TTYDRV.@)
439 BOOL TTYDRV_SetWindowPos( WINDOWPOS *winpos )
442 RECT newWindowRect, newClientRect;
444 HWND hwndActive = GetForegroundWindow();
446 TRACE( "hwnd %04x, swp (%i,%i)-(%i,%i) flags %08x\n",
447 winpos->hwnd, winpos->x, winpos->y,
448 winpos->x + winpos->cx, winpos->y + winpos->cy, winpos->flags);
450 /* ------------------------------------------------------------------------ CHECKS */
452 /* Check window handle */
454 if (winpos->hwnd == GetDesktopWindow()) return FALSE;
455 if (!(wndPtr = WIN_FindWndPtr( winpos->hwnd ))) return FALSE;
457 TRACE("\tcurrent (%i,%i)-(%i,%i), style %08x\n",
458 wndPtr->rectWindow.left, wndPtr->rectWindow.top,
459 wndPtr->rectWindow.right, wndPtr->rectWindow.bottom, (unsigned)wndPtr->dwStyle );
461 /* Fix redundant flags */
463 if(wndPtr->dwStyle & WS_VISIBLE)
464 winpos->flags &= ~SWP_SHOWWINDOW;
467 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
468 winpos->flags &= ~SWP_HIDEWINDOW;
471 if ( winpos->cx < 0 ) winpos->cx = 0;
472 if ( winpos->cy < 0 ) winpos->cy = 0;
474 if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
475 (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
476 winpos->flags |= SWP_NOSIZE; /* Already the right size */
478 if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
479 winpos->flags |= SWP_NOMOVE; /* Already the right position */
481 if (winpos->hwnd == hwndActive)
482 winpos->flags |= SWP_NOACTIVATE; /* Already active */
483 else if ( (wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD )
485 if(!(winpos->flags & SWP_NOACTIVATE)) /* Bring to the top when activating */
487 winpos->flags &= ~SWP_NOZORDER;
488 winpos->hwndInsertAfter = HWND_TOP;
493 /* Check hwndInsertAfter */
495 /* FIXME: TOPMOST not supported yet */
496 if ((winpos->hwndInsertAfter == HWND_TOPMOST) ||
497 (winpos->hwndInsertAfter == HWND_NOTOPMOST)) winpos->hwndInsertAfter = HWND_TOP;
499 /* hwndInsertAfter must be a sibling of the window */
500 if ((winpos->hwndInsertAfter != HWND_TOP) && (winpos->hwndInsertAfter != HWND_BOTTOM))
502 WND* wnd = WIN_FindWndPtr(winpos->hwndInsertAfter);
505 if( wnd->parent != wndPtr->parent )
508 WIN_ReleaseWndPtr(wnd);
511 /* don't need to change the Zorder of hwnd if it's already inserted
512 * after hwndInsertAfter or when inserting hwnd after itself.
514 if(( wnd->next == wndPtr ) || (winpos->hwnd == winpos->hwndInsertAfter))
515 winpos->flags |= SWP_NOZORDER;
517 WIN_ReleaseWndPtr(wnd);
520 Pos: /* ------------------------------------------------------------------------ MAIN part */
522 /* Send WM_WINDOWPOSCHANGING message */
524 if (!(winpos->flags & SWP_NOSENDCHANGING))
525 SendMessageA( wndPtr->hwndSelf, WM_WINDOWPOSCHANGING, 0, (LPARAM)winpos );
527 /* Calculate new position and size */
529 newWindowRect = wndPtr->rectWindow;
530 newClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
531 : wndPtr->rectClient;
533 if (!(winpos->flags & SWP_NOSIZE))
535 newWindowRect.right = newWindowRect.left + winpos->cx;
536 newWindowRect.bottom = newWindowRect.top + winpos->cy;
538 if (!(winpos->flags & SWP_NOMOVE))
540 newWindowRect.left = winpos->x;
541 newWindowRect.top = winpos->y;
542 newWindowRect.right += winpos->x - wndPtr->rectWindow.left;
543 newWindowRect.bottom += winpos->y - wndPtr->rectWindow.top;
545 OffsetRect( &newClientRect, winpos->x - wndPtr->rectWindow.left,
546 winpos->y - wndPtr->rectWindow.top );
549 if( winpos->hwndInsertAfter == HWND_TOP )
550 winpos->flags |= ( wndPtr->parent->child == wndPtr)? SWP_NOZORDER: 0;
552 if( winpos->hwndInsertAfter == HWND_BOTTOM )
553 winpos->flags |= ( wndPtr->next )? 0: SWP_NOZORDER;
555 if( !(winpos->flags & SWP_NOZORDER) )
556 if( GetWindow(winpos->hwndInsertAfter, GW_HWNDNEXT) == wndPtr->hwndSelf )
557 winpos->flags |= SWP_NOZORDER;
559 /* Common operations */
561 /* Send WM_NCCALCSIZE message to get new client area */
562 if( (winpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
564 WINPOS_SendNCCalcSize( winpos->hwnd, TRUE, &newWindowRect,
565 &wndPtr->rectWindow, &wndPtr->rectClient,
566 winpos, &newClientRect );
569 if(!(winpos->flags & SWP_NOZORDER) && winpos->hwnd != winpos->hwndInsertAfter)
571 if ( WIN_UnlinkWindow( winpos->hwnd ) )
572 WIN_LinkWindow( winpos->hwnd, winpos->hwndInsertAfter );
575 /* FIXME: actually do something with WVR_VALIDRECTS */
577 wndPtr->rectWindow = newWindowRect;
578 wndPtr->rectClient = newClientRect;
580 if( winpos->flags & SWP_SHOWWINDOW )
582 wndPtr->dwStyle |= WS_VISIBLE;
584 else if( winpos->flags & SWP_HIDEWINDOW )
586 wndPtr->dwStyle &= ~WS_VISIBLE;
589 /* ------------------------------------------------------------------------ FINAL */
591 /* repaint invalidated region (if any)
593 * FIXME: if SWP_NOACTIVATE is not set then set invalid regions here without any painting
594 * and force update after ChangeActiveWindow() to avoid painting frames twice.
597 if( !(winpos->flags & SWP_NOREDRAW) )
599 RedrawWindow( wndPtr->parent->hwndSelf, NULL, 0,
600 RDW_ERASE | RDW_INVALIDATE | RDW_ALLCHILDREN );
601 if (wndPtr->parent->hwndSelf == GetDesktopWindow() ||
602 wndPtr->parent->parent->hwndSelf == GetDesktopWindow())
604 RedrawWindow( wndPtr->parent->hwndSelf, NULL, 0,
605 RDW_ERASENOW | RDW_NOCHILDREN );
609 if (!(winpos->flags & SWP_NOACTIVATE))
610 WINPOS_ChangeActiveWindow( winpos->hwnd, FALSE );
612 /* And last, send the WM_WINDOWPOSCHANGED message */
614 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
616 if ((((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE) &&
617 !(winpos->flags & SWP_NOSENDCHANGING)) )
618 SendMessageA( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
622 WIN_ReleaseWndPtr(wndPtr);