Various cosmetic changes.
[wine] / dlls / ttydrv / wnd.c
1 /*
2  * TTY window driver
3  *
4  * Copyright 1998,1999 Patrik Stridvall
5  */
6
7 #include "config.h"
8
9 #include "gdi.h"
10 #include "ttydrv.h"
11 #include "region.h"
12 #include "win.h"
13 #include "winpos.h"
14 #include "debugtools.h"
15 #include "hook.h"
16
17 DEFAULT_DEBUG_CHANNEL(ttydrv);
18
19 #define SWP_AGG_NOGEOMETRYCHANGE \
20     (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE)
21 #define SWP_AGG_NOPOSCHANGE \
22     (SWP_AGG_NOGEOMETRYCHANGE | SWP_NOZORDER)
23 #define SWP_AGG_STATUSFLAGS \
24     (SWP_AGG_NOPOSCHANGE | SWP_FRAMECHANGED | SWP_HIDEWINDOW | SWP_SHOWWINDOW)
25
26 /**********************************************************************
27  *              CreateWindow   (TTYDRV.@)
28  */
29 BOOL TTYDRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
30 {
31     BOOL ret;
32     HWND hwndLinkAfter;
33
34 #ifdef WINE_CURSES
35     WND *wndPtr = WIN_FindWndPtr( hwnd );
36     WINDOW *window;
37     INT cellWidth=8, cellHeight=8; /* FIXME: Hardcoded */
38
39     TRACE("(%x)\n", hwnd);
40
41     /* Only create top-level windows */
42     if (!(wndPtr->dwStyle & WS_CHILD))
43     {
44         if (!wndPtr->parent)  /* desktop */
45             window = root_window;
46         else
47         {
48             int x = wndPtr->rectWindow.left;
49             int y = wndPtr->rectWindow.top;
50             int cx = wndPtr->rectWindow.right - wndPtr->rectWindow.left;
51             int cy = wndPtr->rectWindow.bottom - wndPtr->rectWindow.top;
52
53             window = subwin( root_window, cy/cellHeight, cx/cellWidth,
54                              y/cellHeight, x/cellWidth);
55             werase(window);
56             wrefresh(window);
57         }
58         wndPtr->pDriverData = window;
59     }
60     WIN_ReleaseWndPtr( wndPtr );
61 #else /* defined(WINE_CURSES) */
62     FIXME("(%x): stub\n", hwnd);
63 #endif /* defined(WINE_CURSES) */
64
65     /* Call the WH_CBT hook */
66
67     hwndLinkAfter = ((cs->style & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
68  ? HWND_BOTTOM : HWND_TOP;
69
70     if (HOOK_IsHooked( WH_CBT ))
71     {
72         CBT_CREATEWNDA cbtc;
73         LRESULT lret;
74
75         cbtc.lpcs = cs;
76         cbtc.hwndInsertAfter = hwndLinkAfter;
77         lret = (unicode) ? HOOK_CallHooksW(WH_CBT, HCBT_CREATEWND,
78                                                        (WPARAM)hwnd, (LPARAM)&cbtc)
79                         : HOOK_CallHooksA(WH_CBT, HCBT_CREATEWND,
80                                                        (WPARAM)hwnd, (LPARAM)&cbtc);
81         if (lret)
82         {
83             TRACE("CBT-hook returned !0\n");
84             return FALSE;
85         } 
86     }
87
88     if (unicode)
89     {
90         ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
91         if (ret) ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
92     }
93     else
94     {
95         ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
96         if (ret) ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
97     }
98     return ret;
99 }
100
101 /***********************************************************************
102  *              DestroyWindow   (TTYDRV.@)
103  */
104 BOOL TTYDRV_DestroyWindow( HWND hwnd )
105 {
106 #ifdef WINE_CURSES
107     WND *wndPtr = WIN_FindWndPtr( hwnd );
108     WINDOW *window = wndPtr->pDriverData;
109
110     TRACE("(%x)\n", hwnd);
111
112     if (window && window != root_window) delwin(window);
113     wndPtr->pDriverData = NULL;
114     WIN_ReleaseWndPtr( wndPtr );
115 #else /* defined(WINE_CURSES) */
116     FIXME("(%x): stub\n", hwnd);
117 #endif /* defined(WINE_CURSES) */
118     return TRUE;
119 }
120
121
122 /***********************************************************************
123  *           DCE_OffsetVisRgn
124  *
125  * Change region from DC-origin relative coordinates to screen coords.
126  */
127
128 static void DCE_OffsetVisRgn( HDC hDC, HRGN hVisRgn )
129 {
130     DC *dc;
131     if (!(dc = DC_GetDCPtr( hDC ))) return;
132
133     OffsetRgn( hVisRgn, dc->DCOrgX, dc->DCOrgY );
134
135     GDI_ReleaseObj( hDC );
136 }
137
138
139 /***********************************************************************
140  *           DCE_GetVisRect
141  *
142  * Calculate the visible rectangle of a window (i.e. the client or
143  * window area clipped by the client area of all ancestors) in the
144  * corresponding coordinates. Return FALSE if the visible region is empty.
145  */
146 static BOOL DCE_GetVisRect( WND *wndPtr, BOOL clientArea, RECT *lprect )
147 {
148     *lprect = clientArea ? wndPtr->rectClient : wndPtr->rectWindow;
149
150     if (wndPtr->dwStyle & WS_VISIBLE)
151     {
152         INT xoffset = lprect->left;
153         INT yoffset = lprect->top;
154
155         while ((wndPtr = WIN_FindWndPtr( GetAncestor(wndPtr->hwndSelf,GA_PARENT) )))
156         {
157             if ( (wndPtr->dwStyle & (WS_ICONIC | WS_VISIBLE)) != WS_VISIBLE )
158             {
159                 WIN_ReleaseWndPtr(wndPtr);
160                 goto fail;
161             }
162
163             xoffset += wndPtr->rectClient.left;
164             yoffset += wndPtr->rectClient.top;
165             OffsetRect( lprect, wndPtr->rectClient.left,
166                         wndPtr->rectClient.top );
167
168             if( (wndPtr->rectClient.left >= wndPtr->rectClient.right) ||
169                 (wndPtr->rectClient.top >= wndPtr->rectClient.bottom) ||
170                 (lprect->left >= wndPtr->rectClient.right) ||
171                 (lprect->right <= wndPtr->rectClient.left) ||
172                 (lprect->top >= wndPtr->rectClient.bottom) ||
173                 (lprect->bottom <= wndPtr->rectClient.top) )
174             {
175                 WIN_ReleaseWndPtr(wndPtr);
176                 goto fail;
177             }
178
179             lprect->left = max( lprect->left, wndPtr->rectClient.left );
180             lprect->right = min( lprect->right, wndPtr->rectClient.right );
181             lprect->top = max( lprect->top, wndPtr->rectClient.top );
182             lprect->bottom = min( lprect->bottom, wndPtr->rectClient.bottom );
183
184             WIN_ReleaseWndPtr(wndPtr);
185         }
186         OffsetRect( lprect, -xoffset, -yoffset );
187         return TRUE;
188     }
189
190 fail:
191     SetRectEmpty( lprect );
192     return FALSE;
193 }
194
195
196 /***********************************************************************
197  *           DCE_AddClipRects
198  *
199  * Go through the linked list of windows from pWndStart to pWndEnd,
200  * adding to the clip region the intersection of the target rectangle
201  * with an offset window rectangle.
202  */
203 static void DCE_AddClipRects( HWND parent, HWND end, HRGN hrgnClip, LPRECT lpRect, int x, int y )
204 {
205     RECT rect;
206     WND *pWnd;
207     int i;
208     HWND *list = WIN_ListChildren( parent );
209
210     if (!list) return;
211     for (i = 0; list[i]; i++)
212     {
213         if (list[i] == end) break;
214         if (!(pWnd = WIN_FindWndPtr( list[i] ))) continue;
215         if (pWnd->dwStyle & WS_VISIBLE)
216         {
217             rect.left = pWnd->rectWindow.left + x;
218             rect.top = pWnd->rectWindow.top + y;
219             rect.right = pWnd->rectWindow.right + x;
220             rect.bottom = pWnd->rectWindow.bottom + y;
221             if( IntersectRect( &rect, &rect, lpRect ))
222             {
223                 if(!REGION_UnionRectWithRgn( hrgnClip, &rect ))
224                 {
225                     WIN_ReleaseWndPtr( pWnd );
226                     break;
227                 }
228             }
229         }
230         WIN_ReleaseWndPtr( pWnd );
231     }
232     HeapFree( GetProcessHeap(), 0, list );
233 }
234
235
236 /***********************************************************************
237  *           DCE_GetVisRgn
238  *
239  * Return the visible region of a window, i.e. the client or window area
240  * clipped by the client area of all ancestors, and then optionally
241  * by siblings and children.
242  */
243 static HRGN DCE_GetVisRgn( HWND hwnd, WORD flags, HWND hwndChild, WORD cflags )
244 {
245     HRGN hrgnVis = 0;
246     RECT rect;
247     WND *wndPtr = WIN_FindWndPtr( hwnd );
248     WND *childWnd = WIN_FindWndPtr( hwndChild );
249
250     /* Get visible rectangle and create a region with it. */
251
252     if (wndPtr && DCE_GetVisRect(wndPtr, !(flags & DCX_WINDOW), &rect))
253     {
254         if((hrgnVis = CreateRectRgnIndirect( &rect )))
255         {
256             HRGN hrgnClip = CreateRectRgn( 0, 0, 0, 0 );
257             INT xoffset, yoffset;
258
259             if( hrgnClip )
260             {
261                 /* Compute obscured region for the visible rectangle by 
262                  * clipping children, siblings, and ancestors. Note that
263                  * DCE_GetVisRect() returns a rectangle either in client
264                  * or in window coordinates (for DCX_WINDOW request). */
265
266                 if (flags & DCX_CLIPCHILDREN)
267                 {
268                     if( flags & DCX_WINDOW )
269                     {
270                         /* adjust offsets since child window rectangles are 
271                          * in client coordinates */
272
273                         xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
274                         yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
275                     }
276                     else
277                         xoffset = yoffset = 0;
278
279                     DCE_AddClipRects( wndPtr->hwndSelf, 0, hrgnClip, &rect, xoffset, yoffset );
280                 }
281
282                 /* We may need to clip children of child window, if a window with PARENTDC
283                  * class style and CLIPCHILDREN window style (like in Free Agent 16
284                  * preference dialogs) gets here, we take the region for the parent window
285                  * but apparently still need to clip the children of the child window... */
286
287                 if( (cflags & DCX_CLIPCHILDREN) && childWnd)
288                 {
289                     if( flags & DCX_WINDOW )
290                     {
291                         /* adjust offsets since child window rectangles are 
292                          * in client coordinates */
293
294                         xoffset = wndPtr->rectClient.left - wndPtr->rectWindow.left;
295                         yoffset = wndPtr->rectClient.top - wndPtr->rectWindow.top;
296                     }
297                     else
298                         xoffset = yoffset = 0;
299
300                     /* client coordinates of child window */
301                     xoffset += childWnd->rectClient.left;
302                     yoffset += childWnd->rectClient.top;
303
304                     DCE_AddClipRects( childWnd->hwndSelf, 0, hrgnClip,
305                                       &rect, xoffset, yoffset );
306                 }
307
308                 /* sibling window rectangles are in client 
309                  * coordinates of the parent window */
310
311                 if (flags & DCX_WINDOW)
312                 {
313                     xoffset = -wndPtr->rectWindow.left;
314                     yoffset = -wndPtr->rectWindow.top;
315                 }
316                 else
317                 {
318                     xoffset = -wndPtr->rectClient.left;
319                     yoffset = -wndPtr->rectClient.top;
320                 }
321
322                 if (flags & DCX_CLIPSIBLINGS && wndPtr->parent )
323                     DCE_AddClipRects( wndPtr->parent, wndPtr->hwndSelf,
324                                       hrgnClip, &rect, xoffset, yoffset );
325
326                 /* Clip siblings of all ancestors that have the
327                  * WS_CLIPSIBLINGS style
328                  */
329
330                 while (wndPtr->parent)
331                 {
332                     WND *ptr = WIN_FindWndPtr( wndPtr->parent );
333                     WIN_ReleaseWndPtr( wndPtr );
334                     wndPtr = ptr;
335                     xoffset -= wndPtr->rectClient.left;
336                     yoffset -= wndPtr->rectClient.top;
337                     if(wndPtr->dwStyle & WS_CLIPSIBLINGS && wndPtr->parent)
338                     {
339                         DCE_AddClipRects( wndPtr->parent, wndPtr->hwndSelf,
340                                           hrgnClip, &rect, xoffset, yoffset );
341                     }
342                 }
343
344                 /* Now once we've got a jumbo clip region we have
345                  * to substract it from the visible rectangle.
346                  */
347                 CombineRgn( hrgnVis, hrgnVis, hrgnClip, RGN_DIFF );
348                 DeleteObject( hrgnClip );
349             }
350             else
351             {
352                 DeleteObject( hrgnVis );
353                 hrgnVis = 0;
354             }
355         }
356     }
357     else
358         hrgnVis = CreateRectRgn(0, 0, 0, 0); /* empty */
359     WIN_ReleaseWndPtr(wndPtr);
360     WIN_ReleaseWndPtr(childWnd);
361     return hrgnVis;
362 }
363
364
365 /***********************************************************************
366  *              GetDC   (TTYDRV.@)
367  *
368  * Set the drawable, origin and dimensions for the DC associated to
369  * a given window.
370  */
371 BOOL TTYDRV_GetDC( HWND hwnd, HDC hdc, HRGN hrgn, DWORD flags )
372 {
373     WND *wndPtr = WIN_FindWndPtr(hwnd);
374     DC *dc;
375     BOOL updateVisRgn;
376     HRGN hrgnVisible = 0;
377
378     if (!wndPtr) return FALSE;
379
380     if (!(dc = DC_GetDCPtr( hdc )))
381     {
382         WIN_ReleaseWndPtr( wndPtr );
383         return FALSE;
384     }
385
386     if(flags & DCX_WINDOW)
387     {
388         dc->DCOrgX = wndPtr->rectWindow.left;
389         dc->DCOrgY = wndPtr->rectWindow.top;
390     }
391     else
392     {
393         dc->DCOrgX = wndPtr->rectClient.left;
394         dc->DCOrgY = wndPtr->rectClient.top;
395     }
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                 DCE_OffsetVisRgn( hdc, hrgnVisible );
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             DCE_OffsetVisRgn( hdc, hrgnVisible );
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         DCE_OffsetVisRgn( hdc, hrgnVisible );
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 }