Get rid of the remaining WIN_FindWndPtr calls.
[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 "ntstatus.h"
25 #include "win.h"
26 #include "winpos.h"
27 #include "wownt32.h"
28 #include "wine/wingdi16.h"
29 #include "wine/server.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(ttydrv);
33
34 #define SWP_AGG_NOGEOMETRYCHANGE \
35     (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE)
36 #define SWP_AGG_NOPOSCHANGE \
37     (SWP_AGG_NOGEOMETRYCHANGE | SWP_NOZORDER)
38 #define SWP_AGG_STATUSFLAGS \
39     (SWP_AGG_NOPOSCHANGE | SWP_FRAMECHANGED | SWP_HIDEWINDOW | SWP_SHOWWINDOW)
40
41 /***********************************************************************
42  *              get_server_visible_region
43  */
44 static HRGN get_server_visible_region( HWND hwnd, UINT flags )
45 {
46     RGNDATA *data;
47     NTSTATUS status;
48     HRGN ret = 0;
49     size_t size = 256;
50
51     do
52     {
53         if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 ))) return 0;
54         SERVER_START_REQ( get_visible_region )
55         {
56             req->window  = hwnd;
57             req->flags   = flags;
58             wine_server_set_reply( req, data->Buffer, size );
59             if (!(status = wine_server_call( req )))
60             {
61                 size_t reply_size = wine_server_reply_size( reply );
62                 data->rdh.dwSize   = sizeof(data->rdh);
63                 data->rdh.iType    = RDH_RECTANGLES;
64                 data->rdh.nCount   = reply_size / sizeof(RECT);
65                 data->rdh.nRgnSize = reply_size;
66                 ret = ExtCreateRegion( NULL, size, data );
67             }
68             else size = reply->total_size;
69         }
70         SERVER_END_REQ;
71         HeapFree( GetProcessHeap(), 0, data );
72     } while (status == STATUS_BUFFER_OVERFLOW);
73
74     if (status) SetLastError( RtlNtStatusToDosError(status) );
75     return ret;
76 }
77
78
79 /***********************************************************************
80  *           set_window_pos
81  *
82  * Set a window position and Z order.
83  */
84 static BOOL set_window_pos( HWND hwnd, HWND insert_after, const RECT *rectWindow,
85                             const RECT *rectClient, UINT swp_flags )
86 {
87     WND *win = WIN_GetPtr( hwnd );
88     BOOL ret;
89
90     if (!win) return FALSE;
91     if (win == WND_OTHER_PROCESS)
92     {
93         if (IsWindow( hwnd )) ERR( "cannot set rectangles of other process window %p\n", hwnd );
94         return FALSE;
95     }
96     SERVER_START_REQ( set_window_pos )
97     {
98         req->handle        = hwnd;
99         req->previous      = insert_after;
100         req->flags         = swp_flags;
101         req->window.left   = rectWindow->left;
102         req->window.top    = rectWindow->top;
103         req->window.right  = rectWindow->right;
104         req->window.bottom = rectWindow->bottom;
105         req->client.left   = rectClient->left;
106         req->client.top    = rectClient->top;
107         req->client.right  = rectClient->right;
108         req->client.bottom = rectClient->bottom;
109         ret = !wine_server_call( req );
110     }
111     SERVER_END_REQ;
112     if (ret)
113     {
114         win->rectWindow = *rectWindow;
115         win->rectClient = *rectClient;
116
117         TRACE( "win %p window (%ld,%ld)-(%ld,%ld) client (%ld,%ld)-(%ld,%ld)\n", hwnd,
118                rectWindow->left, rectWindow->top, rectWindow->right, rectWindow->bottom,
119                rectClient->left, rectClient->top, rectClient->right, rectClient->bottom );
120     }
121     WIN_ReleasePtr( win );
122     return ret;
123 }
124
125
126 /**********************************************************************
127  *              CreateWindow   (TTYDRV.@)
128  */
129 BOOL TTYDRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
130 {
131     BOOL ret;
132     RECT rect;
133     HWND parent, hwndLinkAfter;
134     CBT_CREATEWNDA cbtc;
135
136     TRACE("(%p)\n", hwnd);
137
138     /* initialize the dimensions before sending WM_GETMINMAXINFO */
139     SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
140     set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER );
141
142     parent = GetAncestor( hwnd, GA_PARENT );
143     if (!parent)  /* desktop window */
144     {
145         SetPropA( hwnd, "__wine_ttydrv_window", root_window );
146         return TRUE;
147     }
148
149 #ifdef WINE_CURSES
150     /* Only create top-level windows */
151     if (parent == GetDesktopWindow())
152     {
153         WINDOW *window;
154         const INT cellWidth=8, cellHeight=8; /* FIXME: Hardcoded */
155
156         window = subwin( root_window, cs->cy/cellHeight, cs->cx/cellWidth,
157                          cs->y/cellHeight, cs->x/cellWidth);
158         werase(window);
159         wrefresh(window);
160         SetPropA( hwnd, "__wine_ttydrv_window", window );
161     }
162 #else /* defined(WINE_CURSES) */
163     FIXME("(%p): stub\n", hwnd);
164 #endif /* defined(WINE_CURSES) */
165
166     /* Call the WH_CBT hook */
167
168     hwndLinkAfter = ((cs->style & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
169
170     cbtc.lpcs = cs;
171     cbtc.hwndInsertAfter = hwndLinkAfter;
172     if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode ))
173     {
174         TRACE("CBT-hook returned !0\n");
175         return FALSE;
176     }
177
178     if (unicode)
179     {
180         ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
181         if (ret) ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
182     }
183     else
184     {
185         ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
186         if (ret) ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
187     }
188     if (ret) NotifyWinEvent(EVENT_OBJECT_CREATE, hwnd, OBJID_WINDOW, 0);
189     return ret;
190 }
191
192 /***********************************************************************
193  *              DestroyWindow   (TTYDRV.@)
194  */
195 BOOL TTYDRV_DestroyWindow( HWND hwnd )
196 {
197 #ifdef WINE_CURSES
198     WINDOW *window = GetPropA( hwnd, "__wine_ttydrv_window" );
199
200     TRACE("(%p)\n", hwnd);
201
202     if (window && window != root_window) delwin(window);
203 #else /* defined(WINE_CURSES) */
204     FIXME("(%p): stub\n", hwnd);
205 #endif /* defined(WINE_CURSES) */
206     return TRUE;
207 }
208
209
210 /***********************************************************************
211  *              GetDC   (TTYDRV.@)
212  *
213  * Set the drawable, origin and dimensions for the DC associated to
214  * a given window.
215  */
216 BOOL TTYDRV_GetDC( HWND hwnd, HDC hdc, HRGN hrgn, DWORD flags )
217 {
218     struct ttydrv_escape_set_drawable escape;
219
220     if(flags & DCX_WINDOW)
221     {
222         RECT rect;
223         GetWindowRect( hwnd, &rect );
224         escape.org.x = rect.left;
225         escape.org.y = rect.top;
226     }
227     else
228     {
229         escape.org.x = escape.org.y = 0;
230         MapWindowPoints( hwnd, 0, &escape.org, 1 );
231     }
232
233     escape.code = TTYDRV_SET_DRAWABLE;
234     ExtEscape( hdc, TTYDRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
235
236     if (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN) ||
237         SetHookFlags16( HDC_16(hdc), DCHF_VALIDATEVISRGN ))  /* DC was dirty */
238     {
239         /* need to recompute the visible region */
240         HRGN visRgn = get_server_visible_region( hwnd, flags );
241
242         if (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN))
243             CombineRgn( visRgn, visRgn, hrgn, (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
244
245         SelectVisRgn16( HDC_16(hdc), HRGN_16(visRgn) );
246         DeleteObject( visRgn );
247     }
248     return TRUE;
249 }
250
251
252 /* fix redundant flags and values in the WINDOWPOS structure */
253 static BOOL fixup_flags( WINDOWPOS *winpos )
254 {
255     HWND parent;
256     WND *wndPtr = WIN_GetPtr( winpos->hwnd );
257     BOOL ret = TRUE;
258
259     if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
260     {
261         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
262         return FALSE;
263     }
264     winpos->hwnd = wndPtr->hwndSelf;  /* make it a full handle */
265
266     /* Finally make sure that all coordinates are valid */
267     if (winpos->x < -32768) winpos->x = -32768;
268     else if (winpos->x > 32767) winpos->x = 32767;
269     if (winpos->y < -32768) winpos->y = -32768;
270     else if (winpos->y > 32767) winpos->y = 32767;
271
272     if (winpos->cx < 0) winpos->cx = 0;
273     else if (winpos->cx > 32767) winpos->cx = 32767;
274     if (winpos->cy < 0) winpos->cy = 0;
275     else if (winpos->cy > 32767) winpos->cy = 32767;
276
277     parent = GetAncestor( winpos->hwnd, GA_PARENT );
278     if (!IsWindowVisible( parent )) winpos->flags |= SWP_NOREDRAW;
279
280     if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
281     else
282     {
283         winpos->flags &= ~SWP_HIDEWINDOW;
284         if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
285     }
286
287     if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
288         (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
289         winpos->flags |= SWP_NOSIZE;    /* Already the right size */
290
291     if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
292         winpos->flags |= SWP_NOMOVE;    /* Already the right position */
293
294     if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
295     {
296         if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) /* Bring to the top when activating */
297         {
298             winpos->flags &= ~SWP_NOZORDER;
299             winpos->hwndInsertAfter = HWND_TOP;
300         }
301     }
302
303     /* Check hwndInsertAfter */
304     if (winpos->flags & SWP_NOZORDER) goto done;
305
306     /* fix sign extension */
307     if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
308     else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
309
310       /* FIXME: TOPMOST not supported yet */
311     if ((winpos->hwndInsertAfter == HWND_TOPMOST) ||
312         (winpos->hwndInsertAfter == HWND_NOTOPMOST)) winpos->hwndInsertAfter = HWND_TOP;
313
314     /* hwndInsertAfter must be a sibling of the window */
315     if (winpos->hwndInsertAfter == HWND_TOP)
316     {
317         if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
318             winpos->flags |= SWP_NOZORDER;
319     }
320     else if (winpos->hwndInsertAfter == HWND_BOTTOM)
321     {
322         if (GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
323             winpos->flags |= SWP_NOZORDER;
324     }
325     else
326     {
327         if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != parent) ret = FALSE;
328         else
329         {
330             /* don't need to change the Zorder of hwnd if it's already inserted
331              * after hwndInsertAfter or when inserting hwnd after itself.
332              */
333             if ((winpos->hwnd == winpos->hwndInsertAfter) ||
334                 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
335                 winpos->flags |= SWP_NOZORDER;
336         }
337     }
338  done:
339     WIN_ReleasePtr( wndPtr );
340     return ret;
341 }
342
343
344 /***********************************************************************
345  *           SWP_DoNCCalcSize
346  */
347 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect )
348 {
349     UINT wvrFlags = 0;
350     WND *wndPtr;
351
352     if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
353
354       /* Send WM_NCCALCSIZE message to get new client area */
355     if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
356     {
357         NCCALCSIZE_PARAMS params;
358         WINDOWPOS winposCopy;
359
360         params.rgrc[0] = *pNewWindowRect;
361         params.rgrc[1] = wndPtr->rectWindow;
362         params.rgrc[2] = wndPtr->rectClient;
363         params.lppos = &winposCopy;
364         winposCopy = *pWinpos;
365         WIN_ReleasePtr( wndPtr );
366
367         wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
368
369         *pNewClientRect = params.rgrc[0];
370
371         if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
372
373         TRACE( "hwnd %p old win %s old client %s new win %s new client %s\n", pWinpos->hwnd,
374                wine_dbgstr_rect(&wndPtr->rectWindow), wine_dbgstr_rect(&wndPtr->rectClient),
375                wine_dbgstr_rect(pNewWindowRect), wine_dbgstr_rect(pNewClientRect) );
376
377         if( pNewClientRect->left != wndPtr->rectClient.left ||
378             pNewClientRect->top != wndPtr->rectClient.top )
379             pWinpos->flags &= ~SWP_NOCLIENTMOVE;
380
381         if( (pNewClientRect->right - pNewClientRect->left !=
382              wndPtr->rectClient.right - wndPtr->rectClient.left))
383             pWinpos->flags &= ~SWP_NOCLIENTSIZE;
384         else
385             wvrFlags &= ~WVR_HREDRAW;
386
387         if (pNewClientRect->bottom - pNewClientRect->top !=
388              wndPtr->rectClient.bottom - wndPtr->rectClient.top)
389             pWinpos->flags &= ~SWP_NOCLIENTSIZE;
390         else
391             wvrFlags &= ~WVR_VREDRAW;
392     }
393     else
394     {
395         if (!(pWinpos->flags & SWP_NOMOVE) &&
396             (pNewClientRect->left != wndPtr->rectClient.left ||
397              pNewClientRect->top != wndPtr->rectClient.top))
398             pWinpos->flags &= ~SWP_NOCLIENTMOVE;
399     }
400
401     WIN_ReleasePtr( wndPtr );
402     return wvrFlags;
403 }
404
405
406 /***********************************************************************
407  *           SWP_DoOwnedPopups
408  *
409  * fix Z order taking into account owned popups -
410  * basically we need to maintain them above the window that owns them
411  *
412  * FIXME: hide/show owned popups when owner visibility changes.
413  */
414 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
415 {
416     HWND *list = NULL;
417     HWND owner = GetWindow( hwnd, GW_OWNER );
418     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
419
420     WARN("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
421
422     if ((style & WS_POPUP) && owner)
423     {
424         /* make sure this popup stays above the owner */
425
426         HWND hwndLocalPrev = HWND_TOP;
427
428         if( hwndInsertAfter != HWND_TOP )
429         {
430             if ((list = WIN_ListChildren( GetDesktopWindow() )))
431             {
432                 int i;
433                 for (i = 0; list[i]; i++)
434                 {
435                     if (list[i] == owner) break;
436                     if (list[i] != hwnd) hwndLocalPrev = list[i];
437                     if (hwndLocalPrev == hwndInsertAfter) break;
438                 }
439                 hwndInsertAfter = hwndLocalPrev;
440             }
441         }
442     }
443     else if (style & WS_CHILD) return hwndInsertAfter;
444
445     if (!list) list = WIN_ListChildren( GetDesktopWindow() );
446     if (list)
447     {
448         int i;
449         for (i = 0; list[i]; i++)
450         {
451             if (list[i] == hwnd) break;
452             if ((GetWindowLongW( list[i], GWL_STYLE ) & WS_POPUP) &&
453                 GetWindow( list[i], GW_OWNER ) == hwnd)
454             {
455                 SetWindowPos( list[i], hwndInsertAfter, 0, 0, 0, 0,
456                               SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE |
457                               SWP_NOSENDCHANGING | SWP_DEFERERASE );
458                 hwndInsertAfter = list[i];
459             }
460         }
461         HeapFree( GetProcessHeap(), 0, list );
462     }
463
464     return hwndInsertAfter;
465 }
466
467
468 /***********************************************************************
469  *           SWP_DoWinPosChanging
470  */
471 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
472 {
473     WND *wndPtr;
474
475     /* Send WM_WINDOWPOSCHANGING message */
476
477     if (!(pWinpos->flags & SWP_NOSENDCHANGING))
478         SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
479
480     if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return FALSE;
481
482     /* Calculate new position and size */
483
484     *pNewWindowRect = wndPtr->rectWindow;
485     *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
486                                                     : wndPtr->rectClient;
487
488     if (!(pWinpos->flags & SWP_NOSIZE))
489     {
490         pNewWindowRect->right  = pNewWindowRect->left + pWinpos->cx;
491         pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
492     }
493     if (!(pWinpos->flags & SWP_NOMOVE))
494     {
495         pNewWindowRect->left    = pWinpos->x;
496         pNewWindowRect->top     = pWinpos->y;
497         pNewWindowRect->right  += pWinpos->x - wndPtr->rectWindow.left;
498         pNewWindowRect->bottom += pWinpos->y - wndPtr->rectWindow.top;
499
500         OffsetRect( pNewClientRect, pWinpos->x - wndPtr->rectWindow.left,
501                                     pWinpos->y - wndPtr->rectWindow.top );
502     }
503     pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
504
505     TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
506            pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
507            pWinpos->cx, pWinpos->cy, pWinpos->flags );
508     TRACE( "current %s style %08lx new %s\n",
509            wine_dbgstr_rect( &wndPtr->rectWindow ), wndPtr->dwStyle,
510            wine_dbgstr_rect( pNewWindowRect ));
511
512     WIN_ReleasePtr( wndPtr );
513     return TRUE;
514 }
515
516
517 /***********************************************************************
518  *              SetWindowPos   (TTYDRV.@)
519  */
520 BOOL TTYDRV_SetWindowPos( WINDOWPOS *winpos )
521 {
522     RECT newWindowRect, newClientRect;
523     UINT orig_flags;
524
525     TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
526            winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
527            winpos->cx, winpos->cy, winpos->flags);
528
529     orig_flags = winpos->flags;
530     winpos->flags &= ~SWP_WINE_NOHOSTMOVE;
531
532     /* Check window handle */
533     if (winpos->hwnd == GetDesktopWindow()) return FALSE;
534
535     /* First make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
536     if (!(winpos->flags & SWP_NOMOVE))
537     {
538         if (winpos->x < -32768) winpos->x = -32768;
539         else if (winpos->x > 32767) winpos->x = 32767;
540         if (winpos->y < -32768) winpos->y = -32768;
541         else if (winpos->y > 32767) winpos->y = 32767;
542     }
543     if (!(winpos->flags & SWP_NOSIZE))
544     {
545         if (winpos->cx < 0) winpos->cx = 0;
546         else if (winpos->cx > 32767) winpos->cx = 32767;
547         if (winpos->cy < 0) winpos->cy = 0;
548         else if (winpos->cy > 32767) winpos->cy = 32767;
549     }
550
551     if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
552
553     /* Fix redundant flags */
554     if (!fixup_flags( winpos )) return FALSE;
555
556     if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
557     {
558         if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
559             winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
560     }
561
562     /* Common operations */
563
564     SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect );
565
566     if (!set_window_pos( winpos->hwnd, winpos->hwndInsertAfter,
567                          &newWindowRect, &newClientRect, orig_flags ))
568         return FALSE;
569
570     if( winpos->flags & SWP_HIDEWINDOW )
571         HideCaret(winpos->hwnd);
572     else if (winpos->flags & SWP_SHOWWINDOW)
573         ShowCaret(winpos->hwnd);
574
575     if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)))
576     {
577         /* child windows get WM_CHILDACTIVATE message */
578         if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
579             SendMessageA( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
580         else
581             SetForegroundWindow( winpos->hwnd );
582     }
583
584       /* And last, send the WM_WINDOWPOSCHANGED message */
585
586     TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
587
588     if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
589     {
590         /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
591            and always contains final window position.
592          */
593         winpos->x = newWindowRect.left;
594         winpos->y = newWindowRect.top;
595         winpos->cx = newWindowRect.right - newWindowRect.left;
596         winpos->cy = newWindowRect.bottom - newWindowRect.top;
597         SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
598     }
599
600     return TRUE;
601 }
602
603
604 /***********************************************************************
605  *              WINPOS_MinMaximize   (internal)
606  *
607  *Lifted from x11 driver
608  */
609 static UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
610 {
611     UINT swpFlags = 0;
612     WINDOWPLACEMENT wpl;
613
614     TRACE("%p %u\n", hwnd, cmd );
615     FIXME("(%p): stub\n", hwnd);
616
617     wpl.length = sizeof(wpl);
618     GetWindowPlacement( hwnd, &wpl );
619
620     /* If I glark this right, yields an immutable window*/
621     swpFlags = SWP_NOSIZE | SWP_NOMOVE;
622
623     /*cmd handling goes here.  see dlls/x1drv/winpos.c*/
624
625     return swpFlags;
626 }
627
628 /***********************************************************************
629  *              ShowWindow   (TTYDRV.@)
630  *
631  *Lifted from x11 driver
632  *Sets the specified windows' show state.
633  */
634 BOOL TTYDRV_ShowWindow( HWND hwnd, INT cmd )
635 {
636     WND *wndPtr;
637     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
638     BOOL wasVisible = (style & WS_VISIBLE) != 0;
639     BOOL showFlag = TRUE;
640     RECT newPos = {0, 0, 0, 0};
641     UINT swp = 0;
642
643
644     TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);
645
646     switch(cmd)
647     {
648         case SW_HIDE:
649             if (!wasVisible) return FALSE;
650             showFlag = FALSE;
651             swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER;
652             break;
653
654         case SW_SHOWMINNOACTIVE:
655             swp |= SWP_NOACTIVATE | SWP_NOZORDER;
656             /* fall through */
657         case SW_SHOWMINIMIZED:
658         case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
659             swp |= SWP_SHOWWINDOW;
660             /* fall through */
661         case SW_MINIMIZE:
662             swp |= SWP_FRAMECHANGED;
663             if( !(style & WS_MINIMIZE) )
664                  swp |= WINPOS_MinMaximize( hwnd, SW_MINIMIZE, &newPos );
665             else swp |= SWP_NOSIZE | SWP_NOMOVE;
666             break;
667
668         case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
669             swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
670             if( !(style & WS_MAXIMIZE) )
671                  swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
672             else swp |= SWP_NOSIZE | SWP_NOMOVE;
673             break;
674
675         case SW_SHOWNA:
676             swp |= SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
677             if (style & WS_CHILD) swp |= SWP_NOZORDER;
678             break;
679         case SW_SHOW:
680             if (wasVisible) return TRUE;
681             swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
682             break;
683
684         case SW_RESTORE:
685             swp |= SWP_FRAMECHANGED;
686             /* fall through */
687         case SW_SHOWNOACTIVATE:
688             swp |= SWP_NOACTIVATE | SWP_NOZORDER;
689             /* fall through */
690         case SW_SHOWNORMAL:  /* same as SW_NORMAL: */
691         case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
692             swp |= SWP_SHOWWINDOW;
693
694             if (style & (WS_MINIMIZE | WS_MAXIMIZE))
695                  swp |= WINPOS_MinMaximize( hwnd, SW_RESTORE, &newPos );
696             else swp |= SWP_NOSIZE | SWP_NOMOVE;
697             break;
698     }
699
700     if (showFlag != wasVisible || cmd == SW_SHOWNA)
701     {
702         SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
703         if (!IsWindow( hwnd )) return wasVisible;
704     }
705
706     /* ShowWindow won't activate a not being maximized child window */
707     if ((style & WS_CHILD) && cmd != SW_MAXIMIZE)
708         swp |= SWP_NOACTIVATE | SWP_NOZORDER;
709
710     SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
711                   newPos.right, newPos.bottom, LOWORD(swp) );
712     if (cmd == SW_HIDE)
713     {
714         HWND hFocus;
715
716         /* FIXME: This will cause the window to be activated irrespective
717          * of whether it is owned by the same thread. Has to be done
718          * asynchronously.
719          */
720
721         if (hwnd == GetActiveWindow())
722             WINPOS_ActivateOtherWindow(hwnd);
723
724         /* Revert focus to parent */
725         hFocus = GetFocus();
726         if (hwnd == hFocus || IsChild(hwnd, hFocus))
727         {
728             HWND parent = GetAncestor(hwnd, GA_PARENT);
729             if (parent == GetDesktopWindow()) parent = 0;
730             SetFocus(parent);
731         }
732     }
733
734     if (IsIconic(hwnd)) WINPOS_ShowIconTitle( hwnd, TRUE );
735
736     if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return wasVisible;
737
738     if (wndPtr->flags & WIN_NEED_SIZE)
739     {
740         /* should happen only in CreateWindowEx() */
741         int wParam = SIZE_RESTORED;
742         RECT client = wndPtr->rectClient;
743
744         wndPtr->flags &= ~WIN_NEED_SIZE;
745         if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
746         else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
747         WIN_ReleasePtr( wndPtr );
748
749         SendMessageW( hwnd, WM_SIZE, wParam,
750                       MAKELONG( client.right - client.left, client.bottom - client.top ));
751         SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( client.left, client.top ));
752     }
753     else WIN_ReleasePtr( wndPtr );
754
755     return wasVisible;
756 }