Implemented __unDName and __unDNameEx functions.
[wine] / dlls / ttydrv / wnd.c
1 /*
2  * TTY window driver
3  *
4  * Copyright 1998,1999 Patrik Stridvall
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22
23 #include "ttydrv.h"
24 #include "win.h"
25 #include "winpos.h"
26 #include "wownt32.h"
27 #include "wine/wingdi16.h"
28 #include "wine/server.h"
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(ttydrv);
32
33 #define SWP_AGG_NOGEOMETRYCHANGE \
34     (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE)
35 #define SWP_AGG_NOPOSCHANGE \
36     (SWP_AGG_NOGEOMETRYCHANGE | SWP_NOZORDER)
37 #define SWP_AGG_STATUSFLAGS \
38     (SWP_AGG_NOPOSCHANGE | SWP_FRAMECHANGED | SWP_HIDEWINDOW | SWP_SHOWWINDOW)
39
40 /***********************************************************************
41  *           set_window_rectangles
42  *
43  * Set the window and client rectangles.
44  */
45 static void set_window_rectangles( HWND hwnd, const RECT *rectWindow, const RECT *rectClient )
46 {
47     WND *win = WIN_GetPtr( hwnd );
48     BOOL ret;
49
50     if (!win) return;
51     if (win == WND_OTHER_PROCESS)
52     {
53         if (IsWindow( hwnd )) ERR( "cannot set rectangles of other process window %p\n", hwnd );
54         return;
55     }
56     SERVER_START_REQ( set_window_rectangles )
57     {
58         req->handle        = hwnd;
59         req->window.left   = rectWindow->left;
60         req->window.top    = rectWindow->top;
61         req->window.right  = rectWindow->right;
62         req->window.bottom = rectWindow->bottom;
63         req->client.left   = rectClient->left;
64         req->client.top    = rectClient->top;
65         req->client.right  = rectClient->right;
66         req->client.bottom = rectClient->bottom;
67         ret = !wine_server_call( req );
68     }
69     SERVER_END_REQ;
70     if (ret)
71     {
72         win->rectWindow = *rectWindow;
73         win->rectClient = *rectClient;
74
75         TRACE( "win %p window (%ld,%ld)-(%ld,%ld) client (%ld,%ld)-(%ld,%ld)\n", hwnd,
76                rectWindow->left, rectWindow->top, rectWindow->right, rectWindow->bottom,
77                rectClient->left, rectClient->top, rectClient->right, rectClient->bottom );
78     }
79     WIN_ReleasePtr( win );
80 }
81
82
83 /**********************************************************************
84  *              CreateWindow   (TTYDRV.@)
85  */
86 BOOL TTYDRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
87 {
88     BOOL ret;
89     RECT rect;
90     HWND hwndLinkAfter;
91     CBT_CREATEWNDA cbtc;
92     WND *wndPtr = WIN_GetPtr( hwnd );
93
94     TRACE("(%p)\n", hwnd);
95
96     /* initialize the dimensions before sending WM_GETMINMAXINFO */
97     SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
98     set_window_rectangles( hwnd, &rect, &rect );
99
100     if (!wndPtr->parent)  /* desktop window */
101     {
102         wndPtr->pDriverData = root_window;
103         WIN_ReleasePtr( wndPtr );
104         return TRUE;
105     }
106
107 #ifdef WINE_CURSES
108     /* Only create top-level windows */
109     if (!(wndPtr->dwStyle & WS_CHILD))
110     {
111         WINDOW *window;
112         const INT cellWidth=8, cellHeight=8; /* FIXME: Hardcoded */
113
114         int x = wndPtr->rectWindow.left;
115         int y = wndPtr->rectWindow.top;
116         int cx = wndPtr->rectWindow.right - wndPtr->rectWindow.left;
117         int cy = wndPtr->rectWindow.bottom - wndPtr->rectWindow.top;
118
119         window = subwin( root_window, cy/cellHeight, cx/cellWidth,
120                          y/cellHeight, x/cellWidth);
121         werase(window);
122         wrefresh(window);
123         wndPtr->pDriverData = window;
124     }
125 #else /* defined(WINE_CURSES) */
126     FIXME("(%p): stub\n", hwnd);
127 #endif /* defined(WINE_CURSES) */
128     WIN_ReleasePtr( wndPtr );
129
130     /* Call the WH_CBT hook */
131
132     hwndLinkAfter = ((cs->style & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
133
134     cbtc.lpcs = cs;
135     cbtc.hwndInsertAfter = hwndLinkAfter;
136     if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode ))
137     {
138         TRACE("CBT-hook returned !0\n");
139         return FALSE;
140     }
141
142     if (unicode)
143     {
144         ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
145         if (ret) ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
146     }
147     else
148     {
149         ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
150         if (ret) ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
151     }
152     return ret;
153 }
154
155 /***********************************************************************
156  *              DestroyWindow   (TTYDRV.@)
157  */
158 BOOL TTYDRV_DestroyWindow( HWND hwnd )
159 {
160 #ifdef WINE_CURSES
161     WND *wndPtr = WIN_GetPtr( hwnd );
162     WINDOW *window = wndPtr->pDriverData;
163
164     TRACE("(%p)\n", hwnd);
165
166     if (window && window != root_window) delwin(window);
167     wndPtr->pDriverData = NULL;
168     WIN_ReleasePtr( wndPtr );
169 #else /* defined(WINE_CURSES) */
170     FIXME("(%p): stub\n", hwnd);
171 #endif /* defined(WINE_CURSES) */
172     return TRUE;
173 }
174
175
176 /***********************************************************************
177  *           DCE_GetVisRect
178  *
179  * Calculate the visible rectangle of a window (i.e. the client or
180  * window area clipped by the client area of all ancestors) in the
181  * corresponding coordinates. Return FALSE if the visible region is empty.
182  */
183 static BOOL DCE_GetVisRect( WND *wndPtr, BOOL clientArea, RECT *lprect )
184 {
185     *lprect = clientArea ? wndPtr->rectClient : wndPtr->rectWindow;
186
187     if (wndPtr->dwStyle & WS_VISIBLE)
188     {
189         INT xoffset = lprect->left;
190         INT yoffset = lprect->top;
191
192         while ((wndPtr = WIN_FindWndPtr( GetAncestor(wndPtr->hwndSelf,GA_PARENT) )))
193         {
194             if ( (wndPtr->dwStyle & (WS_ICONIC | WS_VISIBLE)) != WS_VISIBLE )
195             {
196                 WIN_ReleaseWndPtr(wndPtr);
197                 goto fail;
198             }
199
200             xoffset += wndPtr->rectClient.left;
201             yoffset += wndPtr->rectClient.top;
202             OffsetRect( lprect, wndPtr->rectClient.left,
203                         wndPtr->rectClient.top );
204
205             if( (wndPtr->rectClient.left >= wndPtr->rectClient.right) ||
206                 (wndPtr->rectClient.top >= wndPtr->rectClient.bottom) ||
207                 (lprect->left >= wndPtr->rectClient.right) ||
208                 (lprect->right <= wndPtr->rectClient.left) ||
209                 (lprect->top >= wndPtr->rectClient.bottom) ||
210                 (lprect->bottom <= wndPtr->rectClient.top) )
211             {
212                 WIN_ReleaseWndPtr(wndPtr);
213                 goto fail;
214             }
215
216             lprect->left = max( lprect->left, wndPtr->rectClient.left );
217             lprect->right = min( lprect->right, wndPtr->rectClient.right );
218             lprect->top = max( lprect->top, wndPtr->rectClient.top );
219             lprect->bottom = min( lprect->bottom, wndPtr->rectClient.bottom );
220
221             WIN_ReleaseWndPtr(wndPtr);
222         }
223         OffsetRect( lprect, -xoffset, -yoffset );
224         return TRUE;
225     }
226
227 fail:
228     SetRectEmpty( lprect );
229     return FALSE;
230 }
231
232
233 /***********************************************************************
234  *           DCE_AddClipRects
235  *
236  * Go through the linked list of windows from pWndStart to pWndEnd,
237  * adding to the clip region the intersection of the target rectangle
238  * with an offset window rectangle.
239  */
240 static void DCE_AddClipRects( HWND parent, HWND end, HRGN hrgnClip, LPRECT lpRect, int x, int y )
241 {
242     RECT rect;
243     WND *pWnd;
244     int i;
245     HWND *list = WIN_ListChildren( parent );
246     HRGN hrgn = 0;
247
248     if (!list) return;
249     for (i = 0; list[i]; i++)
250     {
251         if (list[i] == end) break;
252         if (!(pWnd = WIN_FindWndPtr( list[i] ))) continue;
253         if (pWnd->dwStyle & WS_VISIBLE)
254         {
255             rect.left = pWnd->rectWindow.left + x;
256             rect.top = pWnd->rectWindow.top + y;
257             rect.right = pWnd->rectWindow.right + x;
258             rect.bottom = pWnd->rectWindow.bottom + y;
259             if( IntersectRect( &rect, &rect, lpRect ))
260             {
261                 if (!hrgn) hrgn = CreateRectRgnIndirect( &rect );
262                 else SetRectRgn( hrgn, rect.left, rect.top, rect.right, rect.bottom );
263                 CombineRgn( hrgnClip, hrgnClip, hrgn, RGN_OR );
264             }
265         }
266         WIN_ReleaseWndPtr( pWnd );
267     }
268     if (hrgn) DeleteObject( hrgn );
269     HeapFree( GetProcessHeap(), 0, list );
270 }
271
272
273 /***********************************************************************
274  *           DCE_GetVisRgn
275  *
276  * Return the visible region of a window, i.e. the client or window area
277  * clipped by the client area of all ancestors, and then optionally
278  * by siblings and children.
279  */
280 static HRGN DCE_GetVisRgn( HWND hwnd, WORD flags, HWND hwndChild, WORD cflags )
281 {
282     HRGN hrgnVis = 0;
283     RECT rect;
284     WND *wndPtr = WIN_FindWndPtr( hwnd );
285     WND *childWnd = WIN_FindWndPtr( hwndChild );
286
287     /* Get visible rectangle and create a region with it. */
288
289     if (wndPtr && DCE_GetVisRect(wndPtr, !(flags & DCX_WINDOW), &rect))
290     {
291         if((hrgnVis = CreateRectRgnIndirect( &rect )))
292         {
293             HRGN hrgnClip = CreateRectRgn( 0, 0, 0, 0 );
294             INT xoffset, yoffset;
295
296             if( hrgnClip )
297             {
298                 /* Compute obscured region for the visible rectangle by
299                  * clipping children, siblings, and ancestors. Note that
300                  * DCE_GetVisRect() returns a rectangle either in client
301                  * or in window coordinates (for DCX_WINDOW request). */
302
303                 if (flags & DCX_CLIPCHILDREN)
304                 {
305                     if( flags & DCX_WINDOW )
306                     {
307                         /* adjust offsets since child window rectangles are
308                          * in client coordinates */
309
310                         xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
311                         yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
312                     }
313                     else
314                         xoffset = yoffset = 0;
315
316                     DCE_AddClipRects( wndPtr->hwndSelf, 0, hrgnClip, &rect, xoffset, yoffset );
317                 }
318
319                 /* We may need to clip children of child window, if a window with PARENTDC
320                  * class style and CLIPCHILDREN window style (like in Free Agent 16
321                  * preference dialogs) gets here, we take the region for the parent window
322                  * but apparently still need to clip the children of the child window... */
323
324                 if( (cflags & DCX_CLIPCHILDREN) && childWnd)
325                 {
326                     if( flags & DCX_WINDOW )
327                     {
328                         /* adjust offsets since child window rectangles are
329                          * in client coordinates */
330
331                         xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
332                         yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
333                     }
334                     else
335                         xoffset = yoffset = 0;
336
337                     /* client coordinates of child window */
338                     xoffset += childWnd->rectClient.left;
339                     yoffset += childWnd->rectClient.top;
340
341                     DCE_AddClipRects( childWnd->hwndSelf, 0, hrgnClip,
342                                       &rect, xoffset, yoffset );
343                 }
344
345                 /* sibling window rectangles are in client
346                  * coordinates of the parent window */
347
348                 if (flags & DCX_WINDOW)
349                 {
350                     xoffset = -wndPtr->rectWindow.left;
351                     yoffset = -wndPtr->rectWindow.top;
352                 }
353                 else
354                 {
355                     xoffset = -wndPtr->rectClient.left;
356                     yoffset = -wndPtr->rectClient.top;
357                 }
358
359                 if (flags & DCX_CLIPSIBLINGS && wndPtr->parent )
360                     DCE_AddClipRects( wndPtr->parent, wndPtr->hwndSelf,
361                                       hrgnClip, &rect, xoffset, yoffset );
362
363                 /* Clip siblings of all ancestors that have the
364                  * WS_CLIPSIBLINGS style
365                  */
366
367                 while (wndPtr->parent)
368                 {
369                     WND *ptr = WIN_FindWndPtr( wndPtr->parent );
370                     WIN_ReleaseWndPtr( wndPtr );
371                     wndPtr = ptr;
372                     xoffset -= wndPtr->rectClient.left;
373                     yoffset -= wndPtr->rectClient.top;
374                     if(wndPtr->dwStyle & WS_CLIPSIBLINGS && wndPtr->parent)
375                     {
376                         DCE_AddClipRects( wndPtr->parent, wndPtr->hwndSelf,
377                                           hrgnClip, &rect, xoffset, yoffset );
378                     }
379                 }
380
381                 /* Now once we've got a jumbo clip region we have
382                  * to substract it from the visible rectangle.
383                  */
384                 CombineRgn( hrgnVis, hrgnVis, hrgnClip, RGN_DIFF );
385                 DeleteObject( hrgnClip );
386             }
387             else
388             {
389                 DeleteObject( hrgnVis );
390                 hrgnVis = 0;
391             }
392         }
393     }
394     else
395         hrgnVis = CreateRectRgn(0, 0, 0, 0); /* empty */
396     WIN_ReleaseWndPtr(wndPtr);
397     WIN_ReleaseWndPtr(childWnd);
398     return hrgnVis;
399 }
400
401
402 /***********************************************************************
403  *              GetDC   (TTYDRV.@)
404  *
405  * Set the drawable, origin and dimensions for the DC associated to
406  * a given window.
407  */
408 BOOL TTYDRV_GetDC( HWND hwnd, HDC hdc, HRGN hrgn, DWORD flags )
409 {
410     WND *wndPtr = WIN_FindWndPtr(hwnd);
411     HRGN hrgnVisible = 0;
412     POINT org;
413
414     if (!wndPtr) return FALSE;
415
416     if(flags & DCX_WINDOW)
417     {
418         org.x = wndPtr->rectWindow.left;
419         org.y = wndPtr->rectWindow.top;
420     }
421     else
422     {
423         org.x = wndPtr->rectClient.left;
424         org.y = wndPtr->rectClient.top;
425     }
426
427     SetDCOrg16( HDC_16(hdc), org.x, org.y );
428
429     if (SetHookFlags16( HDC_16(hdc), DCHF_VALIDATEVISRGN ) ||  /* DC was dirty */
430         ( flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN) ))
431     {
432         if (flags & DCX_PARENTCLIP)
433         {
434             WND *parentPtr = WIN_FindWndPtr( wndPtr->parent );
435
436             if( wndPtr->dwStyle & WS_VISIBLE && !(parentPtr->dwStyle & WS_MINIMIZE) )
437             {
438                 DWORD dcxFlags;
439
440                 if( parentPtr->dwStyle & WS_CLIPSIBLINGS )
441                     dcxFlags = DCX_CLIPSIBLINGS | (flags & ~(DCX_CLIPCHILDREN | DCX_WINDOW));
442                 else
443                     dcxFlags = flags & ~(DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW);
444
445                 hrgnVisible = DCE_GetVisRgn( parentPtr->hwndSelf, dcxFlags,
446                                              wndPtr->hwndSelf, flags );
447                 if( flags & DCX_WINDOW )
448                     OffsetRgn( hrgnVisible, -wndPtr->rectWindow.left,
449                                -wndPtr->rectWindow.top );
450                 else
451                     OffsetRgn( hrgnVisible, -wndPtr->rectClient.left,
452                                -wndPtr->rectClient.top );
453             }
454             else
455                 hrgnVisible = CreateRectRgn( 0, 0, 0, 0 );
456             WIN_ReleaseWndPtr(parentPtr);
457         }
458         else
459         {
460             hrgnVisible = DCE_GetVisRgn( hwnd, flags, 0, 0 );
461             OffsetRgn( hrgnVisible, org.x, org.y );
462         }
463
464         /* apply additional region operation (if any) */
465         if( flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN) )
466             CombineRgn( hrgnVisible, hrgnVisible, hrgn,
467                         (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
468
469         SelectVisRgn16( HDC_16(hdc), HRGN_16(hrgnVisible) );
470     }
471
472     if (hrgnVisible) DeleteObject( hrgnVisible );
473
474     WIN_ReleaseWndPtr( wndPtr );
475     return TRUE;
476 }
477
478
479 /***********************************************************************
480  *              SetWindowPos   (TTYDRV.@)
481  */
482 BOOL TTYDRV_SetWindowPos( WINDOWPOS *winpos )
483 {
484     WND *wndPtr;
485     RECT newWindowRect, newClientRect;
486     BOOL retvalue;
487     HWND hwndActive = GetForegroundWindow();
488
489     TRACE( "hwnd %p, swp (%i,%i)-(%i,%i) flags %08x\n",
490            winpos->hwnd, winpos->x, winpos->y,
491            winpos->x + winpos->cx, winpos->y + winpos->cy, winpos->flags);
492
493     /* ------------------------------------------------------------------------ CHECKS */
494
495       /* Check window handle */
496
497     if (winpos->hwnd == GetDesktopWindow()) return FALSE;
498     if (!(wndPtr = WIN_FindWndPtr( winpos->hwnd ))) return FALSE;
499
500     TRACE("\tcurrent (%ld,%ld)-(%ld,%ld), style %08x\n",
501           wndPtr->rectWindow.left, wndPtr->rectWindow.top,
502           wndPtr->rectWindow.right, wndPtr->rectWindow.bottom, (unsigned)wndPtr->dwStyle );
503
504     /* Fix redundant flags */
505
506     if(wndPtr->dwStyle & WS_VISIBLE)
507         winpos->flags &= ~SWP_SHOWWINDOW;
508     else
509     {
510         if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
511         winpos->flags &= ~SWP_HIDEWINDOW;
512     }
513
514     if ( winpos->cx < 0 ) winpos->cx = 0;
515     if ( winpos->cy < 0 ) winpos->cy = 0;
516
517     if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
518         (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
519         winpos->flags |= SWP_NOSIZE;    /* Already the right size */
520
521     if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
522         winpos->flags |= SWP_NOMOVE;    /* Already the right position */
523
524     if (winpos->hwnd == hwndActive)
525         winpos->flags |= SWP_NOACTIVATE;   /* Already active */
526     else if ( (wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD )
527     {
528         if(!(winpos->flags & SWP_NOACTIVATE)) /* Bring to the top when activating */
529         {
530             winpos->flags &= ~SWP_NOZORDER;
531             winpos->hwndInsertAfter = HWND_TOP;
532             goto Pos;
533         }
534     }
535
536     /* Check hwndInsertAfter */
537
538       /* FIXME: TOPMOST not supported yet */
539     if ((winpos->hwndInsertAfter == HWND_TOPMOST) ||
540         (winpos->hwndInsertAfter == HWND_NOTOPMOST)) winpos->hwndInsertAfter = HWND_TOP;
541
542     /* hwndInsertAfter must be a sibling of the window */
543     if ((winpos->hwndInsertAfter != HWND_TOP) && (winpos->hwndInsertAfter != HWND_BOTTOM))
544     {
545         WND* wnd = WIN_FindWndPtr(winpos->hwndInsertAfter);
546
547         if( wnd ) {
548             if( wnd->parent != wndPtr->parent )
549             {
550                 retvalue = FALSE;
551                 WIN_ReleaseWndPtr(wnd);
552                 goto END;
553             }
554             /* don't need to change the Zorder of hwnd if it's already inserted
555              * after hwndInsertAfter or when inserting hwnd after itself.
556              */
557             if ((winpos->hwnd == winpos->hwndInsertAfter) ||
558                 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
559                 winpos->flags |= SWP_NOZORDER;
560         }
561         WIN_ReleaseWndPtr(wnd);
562     }
563
564  Pos:  /* ------------------------------------------------------------------------ MAIN part */
565
566       /* Send WM_WINDOWPOSCHANGING message */
567
568     if (!(winpos->flags & SWP_NOSENDCHANGING))
569         SendMessageA( wndPtr->hwndSelf, WM_WINDOWPOSCHANGING, 0, (LPARAM)winpos );
570
571       /* Calculate new position and size */
572
573     newWindowRect = wndPtr->rectWindow;
574     newClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
575                                                     : wndPtr->rectClient;
576
577     if (!(winpos->flags & SWP_NOSIZE))
578     {
579         newWindowRect.right  = newWindowRect.left + winpos->cx;
580         newWindowRect.bottom = newWindowRect.top + winpos->cy;
581     }
582     if (!(winpos->flags & SWP_NOMOVE))
583     {
584         newWindowRect.left    = winpos->x;
585         newWindowRect.top     = winpos->y;
586         newWindowRect.right  += winpos->x - wndPtr->rectWindow.left;
587         newWindowRect.bottom += winpos->y - wndPtr->rectWindow.top;
588
589         OffsetRect( &newClientRect, winpos->x - wndPtr->rectWindow.left,
590                     winpos->y - wndPtr->rectWindow.top );
591     }
592
593     if( winpos->hwndInsertAfter == HWND_TOP )
594     {
595         if (GetWindow( wndPtr->hwndSelf, GW_HWNDFIRST ) == wndPtr->hwndSelf)
596             winpos->flags |= SWP_NOZORDER;
597     }
598     else
599         if( winpos->hwndInsertAfter == HWND_BOTTOM )
600         {
601             if (!GetWindow( wndPtr->hwndSelf, GW_HWNDNEXT ))
602                 winpos->flags |= SWP_NOZORDER;
603         }
604         else
605             if( !(winpos->flags & SWP_NOZORDER) )
606                 if( GetWindow(winpos->hwndInsertAfter, GW_HWNDNEXT) == wndPtr->hwndSelf )
607                     winpos->flags |= SWP_NOZORDER;
608
609     /* Common operations */
610
611       /* Send WM_NCCALCSIZE message to get new client area */
612     if( (winpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
613     {
614         NCCALCSIZE_PARAMS params;
615         WINDOWPOS winposCopy;
616
617         params.rgrc[0] = newWindowRect;
618         params.rgrc[1] = wndPtr->rectWindow;
619         params.rgrc[2] = wndPtr->rectClient;
620         params.lppos = &winposCopy;
621         winposCopy = *winpos;
622
623         SendMessageW( winpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
624
625         TRACE( "%ld,%ld-%ld,%ld\n", params.rgrc[0].left, params.rgrc[0].top,
626                params.rgrc[0].right, params.rgrc[0].bottom );
627
628         /* If the application send back garbage, ignore it */
629         if (params.rgrc[0].left <= params.rgrc[0].right &&
630             params.rgrc[0].top <= params.rgrc[0].bottom)
631             newClientRect = params.rgrc[0];
632
633          /* FIXME: WVR_ALIGNxxx */
634
635         if( newClientRect.left != wndPtr->rectClient.left ||
636             newClientRect.top != wndPtr->rectClient.top )
637             winpos->flags &= ~SWP_NOCLIENTMOVE;
638
639         if( (newClientRect.right - newClientRect.left !=
640              wndPtr->rectClient.right - wndPtr->rectClient.left) ||
641             (newClientRect.bottom - newClientRect.top !=
642              wndPtr->rectClient.bottom - wndPtr->rectClient.top) )
643             winpos->flags &= ~SWP_NOCLIENTSIZE;
644     }
645
646     if(!(winpos->flags & SWP_NOZORDER) && winpos->hwnd != winpos->hwndInsertAfter)
647     {
648         HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
649         if (parent) WIN_LinkWindow( winpos->hwnd, parent, winpos->hwndInsertAfter );
650     }
651
652     /* FIXME: actually do something with WVR_VALIDRECTS */
653
654     set_window_rectangles( winpos->hwnd, &newWindowRect, &newClientRect );
655
656     if( winpos->flags & SWP_SHOWWINDOW )
657         WIN_SetStyle( winpos->hwnd, wndPtr->dwStyle | WS_VISIBLE );
658     else if( winpos->flags & SWP_HIDEWINDOW )
659         WIN_SetStyle( winpos->hwnd, wndPtr->dwStyle & ~WS_VISIBLE );
660
661     /* ------------------------------------------------------------------------ FINAL */
662
663     /* repaint invalidated region (if any)
664      *
665      * FIXME: if SWP_NOACTIVATE is not set then set invalid regions here without any painting
666      *        and force update after ChangeActiveWindow() to avoid painting frames twice.
667      */
668
669     if( !(winpos->flags & SWP_NOREDRAW) )
670     {
671         RedrawWindow( wndPtr->parent, NULL, 0,
672                       RDW_ERASE | RDW_INVALIDATE | RDW_ALLCHILDREN );
673         if (wndPtr->parent == GetDesktopWindow())
674             RedrawWindow( wndPtr->parent, NULL, 0,
675                           RDW_ERASENOW | RDW_NOCHILDREN );
676     }
677
678     if (!(winpos->flags & SWP_NOACTIVATE)) SetActiveWindow( winpos->hwnd );
679
680       /* And last, send the WM_WINDOWPOSCHANGED message */
681
682     TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
683
684     if ((((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE) &&
685           !(winpos->flags & SWP_NOSENDCHANGING)) )
686         SendMessageA( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
687
688     retvalue = TRUE;
689  END:
690     WIN_ReleaseWndPtr(wndPtr);
691     return retvalue;
692 }
693
694
695 /***********************************************************************
696  *              WINPOS_MinMaximize   (internal)
697  *
698  *Lifted from x11 driver
699  */
700 static UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
701 {
702     UINT swpFlags = 0;
703     WINDOWPLACEMENT wpl;
704
705     TRACE("%p %u\n", hwnd, cmd );
706     FIXME("(%p): stub\n", hwnd);
707
708     wpl.length = sizeof(wpl);
709     GetWindowPlacement( hwnd, &wpl );
710
711     /* If I glark this right, yields an immutable window*/
712     swpFlags = SWP_NOSIZE | SWP_NOMOVE;
713
714     /*cmd handling goes here.  see dlls/x1drv/winpos.c*/
715
716     return swpFlags;
717 }
718
719 /***********************************************************************
720  *              ShowWindow   (TTYDRV.@)
721  *
722  *Lifted from x11 driver
723  *Sets the specified windows' show state.
724  */
725 BOOL TTYDRV_ShowWindow( HWND hwnd, INT cmd )
726 {
727     WND*        wndPtr = WIN_FindWndPtr( hwnd );
728     BOOL        wasVisible, showFlag;
729     RECT        newPos = {0, 0, 0, 0};
730     UINT        swp = 0;
731
732     if (!wndPtr) return FALSE;
733     hwnd = wndPtr->hwndSelf;  /* make it a full handle */
734
735     TRACE("hwnd=%p, cmd=%d\n", hwnd, cmd);
736
737     wasVisible = (wndPtr->dwStyle & WS_VISIBLE) != 0;
738
739     switch(cmd)
740     {
741         case SW_HIDE:
742             if (!wasVisible) goto END;
743             swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE |
744                         SWP_NOACTIVATE | SWP_NOZORDER;
745             break;
746
747         case SW_SHOWMINNOACTIVE:
748             swp |= SWP_NOACTIVATE | SWP_NOZORDER;
749             /* fall through */
750         case SW_SHOWMINIMIZED:
751             swp |= SWP_SHOWWINDOW;
752             /* fall through */
753         case SW_MINIMIZE:
754             swp |= SWP_FRAMECHANGED;
755             if( !(wndPtr->dwStyle & WS_MINIMIZE) )
756                  swp |= WINPOS_MinMaximize( hwnd, SW_MINIMIZE, &newPos );
757             else swp |= SWP_NOSIZE | SWP_NOMOVE;
758             break;
759
760         case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
761             swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
762             if( !(wndPtr->dwStyle & WS_MAXIMIZE) )
763                  swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
764             else swp |= SWP_NOSIZE | SWP_NOMOVE;
765             break;
766
767         case SW_SHOWNA:
768             swp |= SWP_NOACTIVATE | SWP_NOZORDER;
769             /* fall through */
770         case SW_SHOW:
771             swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
772
773             /*
774              * ShowWindow has a little peculiar behavior that if the
775              * window is already the topmost window, it will not
776              * activate it.
777              */
778             if (GetTopWindow(NULL)==hwnd && (wasVisible || GetActiveWindow() == hwnd))
779               swp |= SWP_NOACTIVATE;
780
781             break;
782
783         case SW_SHOWNOACTIVATE:
784             swp |= SWP_NOZORDER;
785             if (GetActiveWindow()) swp |= SWP_NOACTIVATE;
786             /* fall through */
787         case SW_SHOWNORMAL:  /* same as SW_NORMAL: */
788         case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
789         case SW_RESTORE:
790             swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
791
792             if( wndPtr->dwStyle & (WS_MINIMIZE | WS_MAXIMIZE) )
793                  swp |= WINPOS_MinMaximize( hwnd, SW_RESTORE, &newPos );
794             else swp |= SWP_NOSIZE | SWP_NOMOVE;
795             break;
796     }
797
798     showFlag = (cmd != SW_HIDE);
799     if (showFlag != wasVisible)
800     {
801         SendMessageA( hwnd, WM_SHOWWINDOW, showFlag, 0 );
802         if (!IsWindow( hwnd )) goto END;
803     }
804
805     /* We can't activate a child window */
806     if ((wndPtr->dwStyle & WS_CHILD) &&
807         !(wndPtr->dwExStyle & WS_EX_MDICHILD))
808         swp |= SWP_NOACTIVATE | SWP_NOZORDER;
809
810     SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
811                   newPos.right, newPos.bottom, LOWORD(swp) );
812     if (cmd == SW_HIDE)
813     {
814         /* FIXME: This will cause the window to be activated irrespective
815          * of whether it is owned by the same thread. Has to be done
816          * asynchronously.
817          */
818
819         if (hwnd == GetActiveWindow())
820             WINPOS_ActivateOtherWindow(hwnd);
821
822         /* Revert focus to parent */
823         if (hwnd == GetFocus() || IsChild(hwnd, GetFocus()))
824             SetFocus( GetParent(hwnd) );
825     }
826     if (!IsWindow( hwnd )) goto END;
827     else if( wndPtr->dwStyle & WS_MINIMIZE ) WINPOS_ShowIconTitle( hwnd, TRUE );
828
829     if (wndPtr->flags & WIN_NEED_SIZE)
830     {
831         /* should happen only in CreateWindowEx() */
832         int wParam = SIZE_RESTORED;
833
834         wndPtr->flags &= ~WIN_NEED_SIZE;
835         if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
836         else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
837         SendMessageA( hwnd, WM_SIZE, wParam,
838                      MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
839                             wndPtr->rectClient.bottom-wndPtr->rectClient.top));
840         SendMessageA( hwnd, WM_MOVE, 0,
841                    MAKELONG(wndPtr->rectClient.left, wndPtr->rectClient.top) );
842     }
843
844 END:
845     WIN_ReleaseWndPtr(wndPtr);
846     return wasVisible;
847 }