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, HWND top, UINT flags )
53 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 ))) return 0;
54 SERVER_START_REQ( get_visible_region )
59 wine_server_set_reply( req, data->Buffer, size );
60 if (!(status = wine_server_call( req )))
62 size_t reply_size = wine_server_reply_size( reply );
63 data->rdh.dwSize = sizeof(data->rdh);
64 data->rdh.iType = RDH_RECTANGLES;
65 data->rdh.nCount = reply_size / sizeof(RECT);
66 data->rdh.nRgnSize = reply_size;
67 ret = ExtCreateRegion( NULL, size, data );
69 else size = reply->total_size;
72 HeapFree( GetProcessHeap(), 0, data );
73 } while (status == STATUS_BUFFER_OVERFLOW);
75 if (status) SetLastError( RtlNtStatusToDosError(status) );
80 /***********************************************************************
83 * Set a window position and Z order.
85 static void set_window_pos( HWND hwnd, HWND insert_after, const RECT *rectWindow,
86 const RECT *rectClient, UINT swp_flags, UINT wvr_flags )
88 WND *win = WIN_GetPtr( hwnd );
92 if (win == WND_OTHER_PROCESS)
94 if (IsWindow( hwnd )) ERR( "cannot set rectangles of other process window %p\n", hwnd );
97 SERVER_START_REQ( set_window_pos )
101 req->previous = insert_after;
102 req->flags = swp_flags;
103 req->redraw_flags = wvr_flags;
104 req->window.left = rectWindow->left;
105 req->window.top = rectWindow->top;
106 req->window.right = rectWindow->right;
107 req->window.bottom = rectWindow->bottom;
108 req->client.left = rectClient->left;
109 req->client.top = rectClient->top;
110 req->client.right = rectClient->right;
111 req->client.bottom = rectClient->bottom;
112 ret = !wine_server_call( req );
117 win->rectWindow = *rectWindow;
118 win->rectClient = *rectClient;
120 TRACE( "win %p window (%ld,%ld)-(%ld,%ld) client (%ld,%ld)-(%ld,%ld)\n", hwnd,
121 rectWindow->left, rectWindow->top, rectWindow->right, rectWindow->bottom,
122 rectClient->left, rectClient->top, rectClient->right, rectClient->bottom );
124 WIN_ReleasePtr( win );
128 /**********************************************************************
129 * CreateWindow (TTYDRV.@)
131 BOOL TTYDRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
135 HWND parent, hwndLinkAfter;
138 TRACE("(%p)\n", hwnd);
140 /* initialize the dimensions before sending WM_GETMINMAXINFO */
141 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
142 set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER, 0 );
144 parent = GetAncestor( hwnd, GA_PARENT );
145 if (!parent) /* desktop window */
147 SetPropA( hwnd, "__wine_ttydrv_window", root_window );
152 /* Only create top-level windows */
153 if (parent == GetDesktopWindow())
156 const INT cellWidth=8, cellHeight=8; /* FIXME: Hardcoded */
158 window = subwin( root_window, cs->cy/cellHeight, cs->cx/cellWidth,
159 cs->y/cellHeight, cs->x/cellWidth);
162 SetPropA( hwnd, "__wine_ttydrv_window", window );
164 #else /* defined(WINE_CURSES) */
165 FIXME("(%p): stub\n", hwnd);
166 #endif /* defined(WINE_CURSES) */
168 /* Call the WH_CBT hook */
170 hwndLinkAfter = ((cs->style & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
173 cbtc.hwndInsertAfter = hwndLinkAfter;
174 if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode ))
176 TRACE("CBT-hook returned !0\n");
182 ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
183 if (ret) ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
187 ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
188 if (ret) ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
193 /***********************************************************************
194 * DestroyWindow (TTYDRV.@)
196 BOOL TTYDRV_DestroyWindow( HWND hwnd )
199 WINDOW *window = GetPropA( hwnd, "__wine_ttydrv_window" );
201 TRACE("(%p)\n", hwnd);
203 if (window && window != root_window) delwin(window);
204 #else /* defined(WINE_CURSES) */
205 FIXME("(%p): stub\n", hwnd);
206 #endif /* defined(WINE_CURSES) */
211 /***********************************************************************
214 * Set the drawable, origin and dimensions for the DC associated to
217 BOOL TTYDRV_GetDC( HWND hwnd, HDC hdc, HRGN hrgn, DWORD flags )
219 struct ttydrv_escape_set_drawable escape;
221 if(flags & DCX_WINDOW)
224 GetWindowRect( hwnd, &rect );
225 escape.org.x = rect.left;
226 escape.org.y = rect.top;
230 escape.org.x = escape.org.y = 0;
231 MapWindowPoints( hwnd, 0, &escape.org, 1 );
234 escape.code = TTYDRV_SET_DRAWABLE;
235 ExtEscape( hdc, TTYDRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
237 if (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN) ||
238 SetHookFlags16( HDC_16(hdc), DCHF_VALIDATEVISRGN )) /* DC was dirty */
240 /* need to recompute the visible region */
241 HRGN visRgn = get_server_visible_region( hwnd, GetDesktopWindow(), flags );
243 if (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN))
244 CombineRgn( visRgn, visRgn, hrgn, (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
246 SelectVisRgn16( HDC_16(hdc), HRGN_16(visRgn) );
247 DeleteObject( visRgn );
253 /***********************************************************************
254 * SetWindowPos (TTYDRV.@)
256 BOOL TTYDRV_SetWindowPos( WINDOWPOS *winpos )
259 RECT newWindowRect, newClientRect;
262 HWND hwndActive = GetForegroundWindow();
264 TRACE( "hwnd %p, swp (%i,%i)-(%i,%i) flags %08x\n",
265 winpos->hwnd, winpos->x, winpos->y,
266 winpos->x + winpos->cx, winpos->y + winpos->cy, winpos->flags);
268 /* ------------------------------------------------------------------------ CHECKS */
270 /* Check window handle */
272 if (winpos->hwnd == GetDesktopWindow()) return FALSE;
273 if (!(wndPtr = WIN_FindWndPtr( winpos->hwnd ))) return FALSE;
275 TRACE("\tcurrent (%ld,%ld)-(%ld,%ld), style %08x\n",
276 wndPtr->rectWindow.left, wndPtr->rectWindow.top,
277 wndPtr->rectWindow.right, wndPtr->rectWindow.bottom, (unsigned)wndPtr->dwStyle );
279 /* Fix redundant flags */
281 if(wndPtr->dwStyle & WS_VISIBLE)
282 winpos->flags &= ~SWP_SHOWWINDOW;
285 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
286 winpos->flags &= ~SWP_HIDEWINDOW;
289 if ( winpos->cx < 0 ) winpos->cx = 0;
290 if ( winpos->cy < 0 ) winpos->cy = 0;
292 if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
293 (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
294 winpos->flags |= SWP_NOSIZE; /* Already the right size */
296 if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
297 winpos->flags |= SWP_NOMOVE; /* Already the right position */
299 if (winpos->hwnd == hwndActive)
300 winpos->flags |= SWP_NOACTIVATE; /* Already active */
301 else if ( (wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD )
303 if(!(winpos->flags & SWP_NOACTIVATE)) /* Bring to the top when activating */
305 winpos->flags &= ~SWP_NOZORDER;
306 winpos->hwndInsertAfter = HWND_TOP;
311 /* Check hwndInsertAfter */
313 /* FIXME: TOPMOST not supported yet */
314 if ((winpos->hwndInsertAfter == HWND_TOPMOST) ||
315 (winpos->hwndInsertAfter == HWND_NOTOPMOST)) winpos->hwndInsertAfter = HWND_TOP;
317 /* hwndInsertAfter must be a sibling of the window */
318 if ((winpos->hwndInsertAfter != HWND_TOP) && (winpos->hwndInsertAfter != HWND_BOTTOM))
320 if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != wndPtr->parent)
325 /* don't need to change the Zorder of hwnd if it's already inserted
326 * after hwndInsertAfter or when inserting hwnd after itself.
328 if ((winpos->hwnd == winpos->hwndInsertAfter) ||
329 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
330 winpos->flags |= SWP_NOZORDER;
333 Pos: /* ------------------------------------------------------------------------ MAIN part */
335 /* Send WM_WINDOWPOSCHANGING message */
337 if (!(winpos->flags & SWP_NOSENDCHANGING))
338 SendMessageA( wndPtr->hwndSelf, WM_WINDOWPOSCHANGING, 0, (LPARAM)winpos );
340 /* Calculate new position and size */
342 newWindowRect = wndPtr->rectWindow;
343 newClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
344 : wndPtr->rectClient;
346 if (!(winpos->flags & SWP_NOSIZE))
348 newWindowRect.right = newWindowRect.left + winpos->cx;
349 newWindowRect.bottom = newWindowRect.top + winpos->cy;
351 if (!(winpos->flags & SWP_NOMOVE))
353 newWindowRect.left = winpos->x;
354 newWindowRect.top = winpos->y;
355 newWindowRect.right += winpos->x - wndPtr->rectWindow.left;
356 newWindowRect.bottom += winpos->y - wndPtr->rectWindow.top;
358 OffsetRect( &newClientRect, winpos->x - wndPtr->rectWindow.left,
359 winpos->y - wndPtr->rectWindow.top );
362 if( winpos->hwndInsertAfter == HWND_TOP )
364 if (GetWindow( wndPtr->hwndSelf, GW_HWNDFIRST ) == wndPtr->hwndSelf)
365 winpos->flags |= SWP_NOZORDER;
368 if( winpos->hwndInsertAfter == HWND_BOTTOM )
370 if (!GetWindow( wndPtr->hwndSelf, GW_HWNDNEXT ))
371 winpos->flags |= SWP_NOZORDER;
374 if( !(winpos->flags & SWP_NOZORDER) )
375 if( GetWindow(winpos->hwndInsertAfter, GW_HWNDNEXT) == wndPtr->hwndSelf )
376 winpos->flags |= SWP_NOZORDER;
378 /* Common operations */
380 /* Send WM_NCCALCSIZE message to get new client area */
381 if( (winpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
383 NCCALCSIZE_PARAMS params;
384 WINDOWPOS winposCopy;
386 params.rgrc[0] = newWindowRect;
387 params.rgrc[1] = wndPtr->rectWindow;
388 params.rgrc[2] = wndPtr->rectClient;
389 params.lppos = &winposCopy;
390 winposCopy = *winpos;
392 wvrFlags = SendMessageW( winpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)¶ms );
394 TRACE( "%ld,%ld-%ld,%ld\n", params.rgrc[0].left, params.rgrc[0].top,
395 params.rgrc[0].right, params.rgrc[0].bottom );
397 /* If the application send back garbage, ignore it */
398 if (params.rgrc[0].left <= params.rgrc[0].right &&
399 params.rgrc[0].top <= params.rgrc[0].bottom)
400 newClientRect = params.rgrc[0];
402 /* FIXME: WVR_ALIGNxxx */
404 if( newClientRect.left != wndPtr->rectClient.left ||
405 newClientRect.top != wndPtr->rectClient.top )
406 winpos->flags &= ~SWP_NOCLIENTMOVE;
408 if( (newClientRect.right - newClientRect.left !=
409 wndPtr->rectClient.right - wndPtr->rectClient.left) ||
410 (newClientRect.bottom - newClientRect.top !=
411 wndPtr->rectClient.bottom - wndPtr->rectClient.top) )
412 winpos->flags &= ~SWP_NOCLIENTSIZE;
415 /* FIXME: actually do something with WVR_VALIDRECTS */
417 set_window_pos( winpos->hwnd, winpos->hwndInsertAfter,
418 &newWindowRect, &newClientRect, winpos->flags, wvrFlags );
420 if( winpos->flags & SWP_SHOWWINDOW )
421 WIN_SetStyle( winpos->hwnd, WS_VISIBLE, 0 );
422 else if( winpos->flags & SWP_HIDEWINDOW )
423 WIN_SetStyle( winpos->hwnd, 0, WS_VISIBLE );
425 /* ------------------------------------------------------------------------ FINAL */
427 /* repaint invalidated region (if any)
429 * FIXME: if SWP_NOACTIVATE is not set then set invalid regions here without any painting
430 * and force update after ChangeActiveWindow() to avoid painting frames twice.
433 if( !(winpos->flags & SWP_NOREDRAW) )
435 RedrawWindow( wndPtr->parent, NULL, 0,
436 RDW_ERASE | RDW_INVALIDATE | RDW_ALLCHILDREN );
437 if (wndPtr->parent == GetDesktopWindow())
438 RedrawWindow( wndPtr->parent, NULL, 0,
439 RDW_ERASENOW | RDW_NOCHILDREN );
442 if (!(winpos->flags & SWP_NOACTIVATE)) SetActiveWindow( winpos->hwnd );
444 /* And last, send the WM_WINDOWPOSCHANGED message */
446 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
448 if ((((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE) &&
449 !(winpos->flags & SWP_NOSENDCHANGING)) )
450 SendMessageA( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
454 WIN_ReleaseWndPtr(wndPtr);
459 /***********************************************************************
460 * WINPOS_MinMaximize (internal)
462 *Lifted from x11 driver
464 static UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
469 TRACE("%p %u\n", hwnd, cmd );
470 FIXME("(%p): stub\n", hwnd);
472 wpl.length = sizeof(wpl);
473 GetWindowPlacement( hwnd, &wpl );
475 /* If I glark this right, yields an immutable window*/
476 swpFlags = SWP_NOSIZE | SWP_NOMOVE;
478 /*cmd handling goes here. see dlls/x1drv/winpos.c*/
483 /***********************************************************************
484 * ShowWindow (TTYDRV.@)
486 *Lifted from x11 driver
487 *Sets the specified windows' show state.
489 BOOL TTYDRV_ShowWindow( HWND hwnd, INT cmd )
491 WND* wndPtr = WIN_FindWndPtr( hwnd );
492 BOOL wasVisible, showFlag;
493 RECT newPos = {0, 0, 0, 0};
496 if (!wndPtr) return FALSE;
497 hwnd = wndPtr->hwndSelf; /* make it a full handle */
499 TRACE("hwnd=%p, cmd=%d\n", hwnd, cmd);
501 wasVisible = (wndPtr->dwStyle & WS_VISIBLE) != 0;
506 if (!wasVisible) goto END;
507 swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE |
508 SWP_NOACTIVATE | SWP_NOZORDER;
511 case SW_SHOWMINNOACTIVE:
512 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
514 case SW_SHOWMINIMIZED:
515 swp |= SWP_SHOWWINDOW;
518 swp |= SWP_FRAMECHANGED;
519 if( !(wndPtr->dwStyle & WS_MINIMIZE) )
520 swp |= WINPOS_MinMaximize( hwnd, SW_MINIMIZE, &newPos );
521 else swp |= SWP_NOSIZE | SWP_NOMOVE;
524 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
525 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
526 if( !(wndPtr->dwStyle & WS_MAXIMIZE) )
527 swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
528 else swp |= SWP_NOSIZE | SWP_NOMOVE;
532 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
535 swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
538 * ShowWindow has a little peculiar behavior that if the
539 * window is already the topmost window, it will not
542 if (GetTopWindow(NULL)==hwnd && (wasVisible || GetActiveWindow() == hwnd))
543 swp |= SWP_NOACTIVATE;
547 case SW_SHOWNOACTIVATE:
549 if (GetActiveWindow()) swp |= SWP_NOACTIVATE;
551 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
552 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
554 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
556 if( wndPtr->dwStyle & (WS_MINIMIZE | WS_MAXIMIZE) )
557 swp |= WINPOS_MinMaximize( hwnd, SW_RESTORE, &newPos );
558 else swp |= SWP_NOSIZE | SWP_NOMOVE;
562 showFlag = (cmd != SW_HIDE);
563 if (showFlag != wasVisible)
565 SendMessageA( hwnd, WM_SHOWWINDOW, showFlag, 0 );
566 if (!IsWindow( hwnd )) goto END;
569 /* We can't activate a child window */
570 if ((wndPtr->dwStyle & WS_CHILD) &&
571 !(wndPtr->dwExStyle & WS_EX_MDICHILD))
572 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
574 SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
575 newPos.right, newPos.bottom, LOWORD(swp) );
578 /* FIXME: This will cause the window to be activated irrespective
579 * of whether it is owned by the same thread. Has to be done
583 if (hwnd == GetActiveWindow())
584 WINPOS_ActivateOtherWindow(hwnd);
586 /* Revert focus to parent */
587 if (hwnd == GetFocus() || IsChild(hwnd, GetFocus()))
588 SetFocus( GetParent(hwnd) );
590 if (!IsWindow( hwnd )) goto END;
591 else if( wndPtr->dwStyle & WS_MINIMIZE ) WINPOS_ShowIconTitle( hwnd, TRUE );
593 if (wndPtr->flags & WIN_NEED_SIZE)
595 /* should happen only in CreateWindowEx() */
596 int wParam = SIZE_RESTORED;
598 wndPtr->flags &= ~WIN_NEED_SIZE;
599 if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
600 else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
601 SendMessageA( hwnd, WM_SIZE, wParam,
602 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
603 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
604 SendMessageA( hwnd, WM_MOVE, 0,
605 MAKELONG(wndPtr->rectClient.left, wndPtr->rectClient.top) );
609 WIN_ReleaseWndPtr(wndPtr);