4 * Copyright 1998,1999 Patrik Stridvall
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "wine/wingdi16.h"
29 #include "wine/server.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(ttydrv);
34 #define SWP_AGG_NOGEOMETRYCHANGE \
35 (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE)
36 #define SWP_AGG_NOPOSCHANGE \
37 (SWP_AGG_NOGEOMETRYCHANGE | SWP_NOZORDER)
38 #define SWP_AGG_STATUSFLAGS \
39 (SWP_AGG_NOPOSCHANGE | SWP_FRAMECHANGED | SWP_HIDEWINDOW | SWP_SHOWWINDOW)
41 /***********************************************************************
42 * get_server_visible_region
44 static HRGN get_server_visible_region( HWND hwnd, UINT flags )
53 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 ))) return 0;
54 SERVER_START_REQ( get_visible_region )
58 wine_server_set_reply( req, data->Buffer, size );
59 if (!(status = wine_server_call( req )))
61 size_t reply_size = wine_server_reply_size( reply );
62 data->rdh.dwSize = sizeof(data->rdh);
63 data->rdh.iType = RDH_RECTANGLES;
64 data->rdh.nCount = reply_size / sizeof(RECT);
65 data->rdh.nRgnSize = reply_size;
66 ret = ExtCreateRegion( NULL, size, data );
68 else size = reply->total_size;
71 HeapFree( GetProcessHeap(), 0, data );
72 } while (status == STATUS_BUFFER_OVERFLOW);
74 if (status) SetLastError( RtlNtStatusToDosError(status) );
79 /***********************************************************************
82 * Set a window position and Z order.
84 static void set_window_pos( HWND hwnd, HWND insert_after, const RECT *rectWindow,
85 const RECT *rectClient, UINT swp_flags )
87 WND *win = WIN_GetPtr( hwnd );
91 if (win == WND_OTHER_PROCESS)
93 if (IsWindow( hwnd )) ERR( "cannot set rectangles of other process window %p\n", hwnd );
96 SERVER_START_REQ( set_window_pos )
99 req->previous = insert_after;
100 req->flags = swp_flags;
101 req->window.left = rectWindow->left;
102 req->window.top = rectWindow->top;
103 req->window.right = rectWindow->right;
104 req->window.bottom = rectWindow->bottom;
105 req->client.left = rectClient->left;
106 req->client.top = rectClient->top;
107 req->client.right = rectClient->right;
108 req->client.bottom = rectClient->bottom;
109 ret = !wine_server_call( req );
114 win->rectWindow = *rectWindow;
115 win->rectClient = *rectClient;
117 TRACE( "win %p window (%ld,%ld)-(%ld,%ld) client (%ld,%ld)-(%ld,%ld)\n", hwnd,
118 rectWindow->left, rectWindow->top, rectWindow->right, rectWindow->bottom,
119 rectClient->left, rectClient->top, rectClient->right, rectClient->bottom );
121 WIN_ReleasePtr( win );
125 /**********************************************************************
126 * CreateWindow (TTYDRV.@)
128 BOOL TTYDRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
132 HWND parent, hwndLinkAfter;
135 TRACE("(%p)\n", hwnd);
137 /* initialize the dimensions before sending WM_GETMINMAXINFO */
138 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
139 set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER );
141 parent = GetAncestor( hwnd, GA_PARENT );
142 if (!parent) /* desktop window */
144 SetPropA( hwnd, "__wine_ttydrv_window", root_window );
149 /* Only create top-level windows */
150 if (parent == GetDesktopWindow())
153 const INT cellWidth=8, cellHeight=8; /* FIXME: Hardcoded */
155 window = subwin( root_window, cs->cy/cellHeight, cs->cx/cellWidth,
156 cs->y/cellHeight, cs->x/cellWidth);
159 SetPropA( hwnd, "__wine_ttydrv_window", window );
161 #else /* defined(WINE_CURSES) */
162 FIXME("(%p): stub\n", hwnd);
163 #endif /* defined(WINE_CURSES) */
165 /* Call the WH_CBT hook */
167 hwndLinkAfter = ((cs->style & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
170 cbtc.hwndInsertAfter = hwndLinkAfter;
171 if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode ))
173 TRACE("CBT-hook returned !0\n");
179 ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
180 if (ret) ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
184 ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
185 if (ret) ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
187 if (ret) NotifyWinEvent(EVENT_OBJECT_CREATE, hwnd, OBJID_WINDOW, 0);
191 /***********************************************************************
192 * DestroyWindow (TTYDRV.@)
194 BOOL TTYDRV_DestroyWindow( HWND hwnd )
197 WINDOW *window = GetPropA( hwnd, "__wine_ttydrv_window" );
199 TRACE("(%p)\n", hwnd);
201 if (window && window != root_window) delwin(window);
202 #else /* defined(WINE_CURSES) */
203 FIXME("(%p): stub\n", hwnd);
204 #endif /* defined(WINE_CURSES) */
209 /***********************************************************************
212 * Set the drawable, origin and dimensions for the DC associated to
215 BOOL TTYDRV_GetDC( HWND hwnd, HDC hdc, HRGN hrgn, DWORD flags )
217 struct ttydrv_escape_set_drawable escape;
219 if(flags & DCX_WINDOW)
222 GetWindowRect( hwnd, &rect );
223 escape.org.x = rect.left;
224 escape.org.y = rect.top;
228 escape.org.x = escape.org.y = 0;
229 MapWindowPoints( hwnd, 0, &escape.org, 1 );
232 escape.code = TTYDRV_SET_DRAWABLE;
233 ExtEscape( hdc, TTYDRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
235 if (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN) ||
236 SetHookFlags16( HDC_16(hdc), DCHF_VALIDATEVISRGN )) /* DC was dirty */
238 /* need to recompute the visible region */
239 HRGN visRgn = get_server_visible_region( hwnd, flags );
241 if (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN))
242 CombineRgn( visRgn, visRgn, hrgn, (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
244 SelectVisRgn16( HDC_16(hdc), HRGN_16(visRgn) );
245 DeleteObject( visRgn );
251 /***********************************************************************
252 * SetWindowPos (TTYDRV.@)
254 BOOL TTYDRV_SetWindowPos( WINDOWPOS *winpos )
257 RECT newWindowRect, newClientRect;
260 HWND hwndActive = GetForegroundWindow();
262 TRACE( "hwnd %p, swp (%i,%i)-(%i,%i) flags %08x\n",
263 winpos->hwnd, winpos->x, winpos->y,
264 winpos->x + winpos->cx, winpos->y + winpos->cy, winpos->flags);
266 /* ------------------------------------------------------------------------ CHECKS */
268 /* Check window handle */
270 if (winpos->hwnd == GetDesktopWindow()) return FALSE;
271 if (!(wndPtr = WIN_FindWndPtr( winpos->hwnd ))) return FALSE;
273 TRACE("\tcurrent (%ld,%ld)-(%ld,%ld), style %08x\n",
274 wndPtr->rectWindow.left, wndPtr->rectWindow.top,
275 wndPtr->rectWindow.right, wndPtr->rectWindow.bottom, (unsigned)wndPtr->dwStyle );
277 /* Fix redundant flags */
279 if(wndPtr->dwStyle & WS_VISIBLE)
280 winpos->flags &= ~SWP_SHOWWINDOW;
283 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
284 winpos->flags &= ~SWP_HIDEWINDOW;
287 if ( winpos->cx < 0 ) winpos->cx = 0;
288 if ( winpos->cy < 0 ) winpos->cy = 0;
290 if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
291 (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
292 winpos->flags |= SWP_NOSIZE; /* Already the right size */
294 if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
295 winpos->flags |= SWP_NOMOVE; /* Already the right position */
297 if (winpos->hwnd == hwndActive)
298 winpos->flags |= SWP_NOACTIVATE; /* Already active */
299 else if ( (wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD )
301 if(!(winpos->flags & SWP_NOACTIVATE)) /* Bring to the top when activating */
303 winpos->flags &= ~SWP_NOZORDER;
304 winpos->hwndInsertAfter = HWND_TOP;
309 /* Check hwndInsertAfter */
311 /* FIXME: TOPMOST not supported yet */
312 if ((winpos->hwndInsertAfter == HWND_TOPMOST) ||
313 (winpos->hwndInsertAfter == HWND_NOTOPMOST)) winpos->hwndInsertAfter = HWND_TOP;
315 /* hwndInsertAfter must be a sibling of the window */
316 if ((winpos->hwndInsertAfter != HWND_TOP) && (winpos->hwndInsertAfter != HWND_BOTTOM))
318 if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != wndPtr->parent)
323 /* don't need to change the Zorder of hwnd if it's already inserted
324 * after hwndInsertAfter or when inserting hwnd after itself.
326 if ((winpos->hwnd == winpos->hwndInsertAfter) ||
327 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
328 winpos->flags |= SWP_NOZORDER;
331 Pos: /* ------------------------------------------------------------------------ MAIN part */
333 /* Send WM_WINDOWPOSCHANGING message */
335 if (!(winpos->flags & SWP_NOSENDCHANGING))
336 SendMessageA( wndPtr->hwndSelf, WM_WINDOWPOSCHANGING, 0, (LPARAM)winpos );
338 /* Calculate new position and size */
340 newWindowRect = wndPtr->rectWindow;
341 newClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
342 : wndPtr->rectClient;
344 if (!(winpos->flags & SWP_NOSIZE))
346 newWindowRect.right = newWindowRect.left + winpos->cx;
347 newWindowRect.bottom = newWindowRect.top + winpos->cy;
349 if (!(winpos->flags & SWP_NOMOVE))
351 newWindowRect.left = winpos->x;
352 newWindowRect.top = winpos->y;
353 newWindowRect.right += winpos->x - wndPtr->rectWindow.left;
354 newWindowRect.bottom += winpos->y - wndPtr->rectWindow.top;
356 OffsetRect( &newClientRect, winpos->x - wndPtr->rectWindow.left,
357 winpos->y - wndPtr->rectWindow.top );
360 if( winpos->hwndInsertAfter == HWND_TOP )
362 if (GetWindow( wndPtr->hwndSelf, GW_HWNDFIRST ) == wndPtr->hwndSelf)
363 winpos->flags |= SWP_NOZORDER;
366 if( winpos->hwndInsertAfter == HWND_BOTTOM )
368 if (!GetWindow( wndPtr->hwndSelf, GW_HWNDNEXT ))
369 winpos->flags |= SWP_NOZORDER;
372 if( !(winpos->flags & SWP_NOZORDER) )
373 if( GetWindow(winpos->hwndInsertAfter, GW_HWNDNEXT) == wndPtr->hwndSelf )
374 winpos->flags |= SWP_NOZORDER;
376 /* Common operations */
378 /* Send WM_NCCALCSIZE message to get new client area */
379 if( (winpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
381 NCCALCSIZE_PARAMS params;
382 WINDOWPOS winposCopy;
384 params.rgrc[0] = newWindowRect;
385 params.rgrc[1] = wndPtr->rectWindow;
386 params.rgrc[2] = wndPtr->rectClient;
387 params.lppos = &winposCopy;
388 winposCopy = *winpos;
390 wvrFlags = SendMessageW( winpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)¶ms );
392 TRACE( "%ld,%ld-%ld,%ld\n", params.rgrc[0].left, params.rgrc[0].top,
393 params.rgrc[0].right, params.rgrc[0].bottom );
395 /* If the application send back garbage, ignore it */
396 if (params.rgrc[0].left <= params.rgrc[0].right &&
397 params.rgrc[0].top <= params.rgrc[0].bottom)
398 newClientRect = params.rgrc[0];
400 /* FIXME: WVR_ALIGNxxx */
402 if( newClientRect.left != wndPtr->rectClient.left ||
403 newClientRect.top != wndPtr->rectClient.top )
404 winpos->flags &= ~SWP_NOCLIENTMOVE;
406 if( (newClientRect.right - newClientRect.left !=
407 wndPtr->rectClient.right - wndPtr->rectClient.left) ||
408 (newClientRect.bottom - newClientRect.top !=
409 wndPtr->rectClient.bottom - wndPtr->rectClient.top) )
410 winpos->flags &= ~SWP_NOCLIENTSIZE;
413 /* FIXME: actually do something with WVR_VALIDRECTS */
415 set_window_pos( winpos->hwnd, winpos->hwndInsertAfter,
416 &newWindowRect, &newClientRect, winpos->flags );
418 if( winpos->flags & SWP_SHOWWINDOW )
419 WIN_SetStyle( winpos->hwnd, WS_VISIBLE, 0 );
420 else if( winpos->flags & SWP_HIDEWINDOW )
421 WIN_SetStyle( winpos->hwnd, 0, WS_VISIBLE );
423 /* ------------------------------------------------------------------------ FINAL */
425 /* repaint invalidated region (if any)
427 * FIXME: if SWP_NOACTIVATE is not set then set invalid regions here without any painting
428 * and force update after ChangeActiveWindow() to avoid painting frames twice.
431 if( !(winpos->flags & SWP_NOREDRAW) )
433 RedrawWindow( wndPtr->parent, NULL, 0,
434 RDW_ERASE | RDW_INVALIDATE | RDW_ALLCHILDREN );
435 if (wndPtr->parent == GetDesktopWindow())
436 RedrawWindow( wndPtr->parent, NULL, 0,
437 RDW_ERASENOW | RDW_NOCHILDREN );
440 if (!(winpos->flags & SWP_NOACTIVATE)) SetActiveWindow( winpos->hwnd );
442 /* And last, send the WM_WINDOWPOSCHANGED message */
444 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
446 if ((((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE) &&
447 !(winpos->flags & SWP_NOSENDCHANGING)) )
448 SendMessageA( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
452 WIN_ReleaseWndPtr(wndPtr);
457 /***********************************************************************
458 * WINPOS_MinMaximize (internal)
460 *Lifted from x11 driver
462 static UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
467 TRACE("%p %u\n", hwnd, cmd );
468 FIXME("(%p): stub\n", hwnd);
470 wpl.length = sizeof(wpl);
471 GetWindowPlacement( hwnd, &wpl );
473 /* If I glark this right, yields an immutable window*/
474 swpFlags = SWP_NOSIZE | SWP_NOMOVE;
476 /*cmd handling goes here. see dlls/x1drv/winpos.c*/
481 /***********************************************************************
482 * ShowWindow (TTYDRV.@)
484 *Lifted from x11 driver
485 *Sets the specified windows' show state.
487 BOOL TTYDRV_ShowWindow( HWND hwnd, INT cmd )
489 WND* wndPtr = WIN_FindWndPtr( hwnd );
490 BOOL wasVisible, showFlag;
491 RECT newPos = {0, 0, 0, 0};
494 if (!wndPtr) return FALSE;
495 hwnd = wndPtr->hwndSelf; /* make it a full handle */
497 TRACE("hwnd=%p, cmd=%d\n", hwnd, cmd);
499 wasVisible = (wndPtr->dwStyle & WS_VISIBLE) != 0;
504 if (!wasVisible) goto END;
505 swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE |
506 SWP_NOACTIVATE | SWP_NOZORDER;
509 case SW_SHOWMINNOACTIVE:
510 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
512 case SW_SHOWMINIMIZED:
513 swp |= SWP_SHOWWINDOW;
516 swp |= SWP_FRAMECHANGED;
517 if( !(wndPtr->dwStyle & WS_MINIMIZE) )
518 swp |= WINPOS_MinMaximize( hwnd, SW_MINIMIZE, &newPos );
519 else swp |= SWP_NOSIZE | SWP_NOMOVE;
522 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
523 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
524 if( !(wndPtr->dwStyle & WS_MAXIMIZE) )
525 swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
526 else swp |= SWP_NOSIZE | SWP_NOMOVE;
530 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
533 swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
536 * ShowWindow has a little peculiar behavior that if the
537 * window is already the topmost window, it will not
540 if (GetTopWindow(NULL)==hwnd && (wasVisible || GetActiveWindow() == hwnd))
541 swp |= SWP_NOACTIVATE;
545 case SW_SHOWNOACTIVATE:
547 if (GetActiveWindow()) swp |= SWP_NOACTIVATE;
549 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
550 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
552 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
554 if( wndPtr->dwStyle & (WS_MINIMIZE | WS_MAXIMIZE) )
555 swp |= WINPOS_MinMaximize( hwnd, SW_RESTORE, &newPos );
556 else swp |= SWP_NOSIZE | SWP_NOMOVE;
560 showFlag = (cmd != SW_HIDE);
561 if (showFlag != wasVisible)
563 SendMessageA( hwnd, WM_SHOWWINDOW, showFlag, 0 );
564 if (!IsWindow( hwnd )) goto END;
567 /* We can't activate a child window */
568 if ((wndPtr->dwStyle & WS_CHILD) &&
569 !(wndPtr->dwExStyle & WS_EX_MDICHILD))
570 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
572 SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
573 newPos.right, newPos.bottom, LOWORD(swp) );
576 /* FIXME: This will cause the window to be activated irrespective
577 * of whether it is owned by the same thread. Has to be done
581 if (hwnd == GetActiveWindow())
582 WINPOS_ActivateOtherWindow(hwnd);
584 /* Revert focus to parent */
585 if (hwnd == GetFocus() || IsChild(hwnd, GetFocus()))
586 SetFocus( GetParent(hwnd) );
588 if (!IsWindow( hwnd )) goto END;
589 else if( wndPtr->dwStyle & WS_MINIMIZE ) WINPOS_ShowIconTitle( hwnd, TRUE );
591 if (wndPtr->flags & WIN_NEED_SIZE)
593 /* should happen only in CreateWindowEx() */
594 int wParam = SIZE_RESTORED;
596 wndPtr->flags &= ~WIN_NEED_SIZE;
597 if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
598 else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
599 SendMessageA( hwnd, WM_SIZE, wParam,
600 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
601 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
602 SendMessageA( hwnd, WM_MOVE, 0,
603 MAKELONG(wndPtr->rectClient.left, wndPtr->rectClient.top) );
607 WIN_ReleaseWndPtr(wndPtr);