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