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