winex11: add owned windows to taskbar if owner is not mapped
[wine] / dlls / user32 / winpos.c
1 /*
2  * Window position related functions.
3  *
4  * Copyright 1993, 1994, 1995 Alexandre Julliard
5  *                       1995, 1996, 1999 Alex Korobka
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <stdarg.h>
26 #include <string.h>
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winerror.h"
33 #include "wine/server.h"
34 #include "controls.h"
35 #include "user_private.h"
36 #include "win.h"
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(win);
40
41 #define SWP_AGG_NOGEOMETRYCHANGE \
42     (SWP_NOSIZE | SWP_NOCLIENTSIZE | SWP_NOZORDER)
43 #define SWP_AGG_NOPOSCHANGE \
44     (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
45 #define SWP_AGG_STATUSFLAGS \
46     (SWP_AGG_NOPOSCHANGE | SWP_FRAMECHANGED | SWP_HIDEWINDOW | SWP_SHOWWINDOW)
47
48 #define HAS_DLGFRAME(style,exStyle) \
49     (((exStyle) & WS_EX_DLGMODALFRAME) || \
50      (((style) & WS_DLGFRAME) && !((style) & WS_BORDER)))
51
52 #define HAS_THICKFRAME(style) \
53     (((style) & WS_THICKFRAME) && \
54      !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
55
56 #define EMPTYPOINT(pt) ((pt).x == -1 && (pt).y == -1)
57
58 #define ON_LEFT_BORDER(hit) \
59  (((hit) == HTLEFT) || ((hit) == HTTOPLEFT) || ((hit) == HTBOTTOMLEFT))
60 #define ON_RIGHT_BORDER(hit) \
61  (((hit) == HTRIGHT) || ((hit) == HTTOPRIGHT) || ((hit) == HTBOTTOMRIGHT))
62 #define ON_TOP_BORDER(hit) \
63  (((hit) == HTTOP) || ((hit) == HTTOPLEFT) || ((hit) == HTTOPRIGHT))
64 #define ON_BOTTOM_BORDER(hit) \
65  (((hit) == HTBOTTOM) || ((hit) == HTBOTTOMLEFT) || ((hit) == HTBOTTOMRIGHT))
66
67 #define PLACE_MIN               0x0001
68 #define PLACE_MAX               0x0002
69 #define PLACE_RECT              0x0004
70
71
72 #define DWP_MAGIC  ((INT)('W' | ('P' << 8) | ('O' << 16) | ('S' << 24)))
73
74 typedef struct
75 {
76     INT       actualCount;
77     INT       suggestedCount;
78     BOOL      valid;
79     INT       wMagic;
80     HWND      hwndParent;
81     WINDOWPOS winPos[1];
82 } DWP;
83
84
85 /***********************************************************************
86  *              SwitchToThisWindow (USER32.@)
87  */
88 void WINAPI SwitchToThisWindow( HWND hwnd, BOOL restore )
89 {
90     ShowWindow( hwnd, restore ? SW_RESTORE : SW_SHOWMINIMIZED );
91 }
92
93
94 /***********************************************************************
95  *              GetWindowRect (USER32.@)
96  */
97 BOOL WINAPI GetWindowRect( HWND hwnd, LPRECT rect )
98 {
99     BOOL ret = WIN_GetRectangles( hwnd, rect, NULL );
100     if (ret)
101     {
102         MapWindowPoints( GetAncestor( hwnd, GA_PARENT ), 0, (POINT *)rect, 2 );
103         TRACE( "hwnd %p (%s)\n", hwnd, wine_dbgstr_rect(rect) );
104     }
105     return ret;
106 }
107
108
109 /***********************************************************************
110  *              GetWindowRgn (USER32.@)
111  */
112 int WINAPI GetWindowRgn ( HWND hwnd, HRGN hrgn )
113 {
114     int nRet = ERROR;
115     NTSTATUS status;
116     HRGN win_rgn = 0;
117     RGNDATA *data;
118     size_t size = 256;
119
120     do
121     {
122         if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
123         {
124             SetLastError( ERROR_OUTOFMEMORY );
125             return ERROR;
126         }
127         SERVER_START_REQ( get_window_region )
128         {
129             req->window = hwnd;
130             wine_server_set_reply( req, data->Buffer, size );
131             if (!(status = wine_server_call( req )))
132             {
133                 size_t reply_size = wine_server_reply_size( reply );
134                 if (reply_size)
135                 {
136                     data->rdh.dwSize   = sizeof(data->rdh);
137                     data->rdh.iType    = RDH_RECTANGLES;
138                     data->rdh.nCount   = reply_size / sizeof(RECT);
139                     data->rdh.nRgnSize = reply_size;
140                     win_rgn = ExtCreateRegion( NULL, size, data );
141                 }
142             }
143             else size = reply->total_size;
144         }
145         SERVER_END_REQ;
146         HeapFree( GetProcessHeap(), 0, data );
147     } while (status == STATUS_BUFFER_OVERFLOW);
148
149     if (status) SetLastError( RtlNtStatusToDosError(status) );
150     else if (win_rgn)
151     {
152         nRet = CombineRgn( hrgn, win_rgn, 0, RGN_COPY );
153         DeleteObject( win_rgn );
154     }
155     return nRet;
156 }
157
158
159 /***********************************************************************
160  *              SetWindowRgn (USER32.@)
161  */
162 int WINAPI SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL bRedraw )
163 {
164     static const RECT empty_rect;
165     BOOL ret;
166
167     if (hrgn)
168     {
169         RGNDATA *data;
170         DWORD size;
171
172         if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
173         if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
174         if (!GetRegionData( hrgn, size, data ))
175         {
176             HeapFree( GetProcessHeap(), 0, data );
177             return FALSE;
178         }
179         SERVER_START_REQ( set_window_region )
180         {
181             req->window = hwnd;
182             req->redraw = (bRedraw != 0);
183             if (data->rdh.nCount)
184                 wine_server_add_data( req, data->Buffer, data->rdh.nCount * sizeof(RECT) );
185             else
186                 wine_server_add_data( req, &empty_rect, sizeof(empty_rect) );
187             ret = !wine_server_call_err( req );
188         }
189         SERVER_END_REQ;
190     }
191     else  /* clear existing region */
192     {
193         SERVER_START_REQ( set_window_region )
194         {
195             req->window = hwnd;
196             req->redraw = (bRedraw != 0);
197             ret = !wine_server_call_err( req );
198         }
199         SERVER_END_REQ;
200     }
201
202     if (ret) ret = USER_Driver->pSetWindowRgn( hwnd, hrgn, bRedraw );
203
204     if (ret)
205     {
206         UINT swp_flags = SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE;
207         if (!bRedraw) swp_flags |= SWP_NOREDRAW;
208         SetWindowPos( hwnd, 0, 0, 0, 0, 0, swp_flags );
209         invalidate_dce( hwnd, NULL );
210     }
211     return ret;
212 }
213
214
215 /***********************************************************************
216  *              GetClientRect (USER32.@)
217  */
218 BOOL WINAPI GetClientRect( HWND hwnd, LPRECT rect )
219 {
220     BOOL ret;
221
222     if ((ret = WIN_GetRectangles( hwnd, NULL, rect )))
223     {
224         rect->right -= rect->left;
225         rect->bottom -= rect->top;
226         rect->left = rect->top = 0;
227     }
228     return ret;
229 }
230
231
232 /*******************************************************************
233  *              ClientToScreen (USER32.@)
234  */
235 BOOL WINAPI ClientToScreen( HWND hwnd, LPPOINT lppnt )
236 {
237     MapWindowPoints( hwnd, 0, lppnt, 1 );
238     return TRUE;
239 }
240
241
242 /*******************************************************************
243  *              ScreenToClient (USER32.@)
244  */
245 BOOL WINAPI ScreenToClient( HWND hwnd, LPPOINT lppnt )
246 {
247     MapWindowPoints( 0, hwnd, lppnt, 1 );
248     return TRUE;
249 }
250
251
252 /***********************************************************************
253  *           list_children_from_point
254  *
255  * Get the list of children that can contain point from the server.
256  * Point is in screen coordinates.
257  * Returned list must be freed by caller.
258  */
259 static HWND *list_children_from_point( HWND hwnd, POINT pt )
260 {
261     HWND *list;
262     int size = 128;
263
264     for (;;)
265     {
266         int count = 0;
267
268         if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
269
270         SERVER_START_REQ( get_window_children_from_point )
271         {
272             req->parent = hwnd;
273             req->x = pt.x;
274             req->y = pt.y;
275             wine_server_set_reply( req, list, (size-1) * sizeof(HWND) );
276             if (!wine_server_call( req )) count = reply->count;
277         }
278         SERVER_END_REQ;
279         if (count && count < size)
280         {
281             list[count] = 0;
282             return list;
283         }
284         HeapFree( GetProcessHeap(), 0, list );
285         if (!count) break;
286         size = count + 1;  /* restart with a large enough buffer */
287     }
288     return NULL;
289 }
290
291
292 /***********************************************************************
293  *           WINPOS_WindowFromPoint
294  *
295  * Find the window and hittest for a given point.
296  */
297 HWND WINPOS_WindowFromPoint( HWND hwndScope, POINT pt, INT *hittest )
298 {
299     int i, res;
300     HWND ret, *list;
301
302     if (!hwndScope) hwndScope = GetDesktopWindow();
303
304     *hittest = HTNOWHERE;
305
306     if (!(list = list_children_from_point( hwndScope, pt ))) return 0;
307
308     /* now determine the hittest */
309
310     for (i = 0; list[i]; i++)
311     {
312         LONG style = GetWindowLongW( list[i], GWL_STYLE );
313
314         /* If window is minimized or disabled, return at once */
315         if (style & WS_MINIMIZE)
316         {
317             *hittest = HTCAPTION;
318             break;
319         }
320         if (style & WS_DISABLED)
321         {
322             *hittest = HTERROR;
323             break;
324         }
325         /* Send WM_NCCHITTEST (if same thread) */
326         if (!WIN_IsCurrentThread( list[i] ))
327         {
328             *hittest = HTCLIENT;
329             break;
330         }
331         res = SendMessageW( list[i], WM_NCHITTEST, 0, MAKELONG(pt.x,pt.y) );
332         if (res != HTTRANSPARENT)
333         {
334             *hittest = res;  /* Found the window */
335             break;
336         }
337         /* continue search with next window in z-order */
338     }
339     ret = list[i];
340     HeapFree( GetProcessHeap(), 0, list );
341     TRACE( "scope %p (%d,%d) returning %p\n", hwndScope, pt.x, pt.y, ret );
342     return ret;
343 }
344
345
346 /*******************************************************************
347  *              WindowFromPoint (USER32.@)
348  */
349 HWND WINAPI WindowFromPoint( POINT pt )
350 {
351     INT hittest;
352     return WINPOS_WindowFromPoint( 0, pt, &hittest );
353 }
354
355
356 /*******************************************************************
357  *              ChildWindowFromPoint (USER32.@)
358  */
359 HWND WINAPI ChildWindowFromPoint( HWND hwndParent, POINT pt )
360 {
361     return ChildWindowFromPointEx( hwndParent, pt, CWP_ALL );
362 }
363
364 /*******************************************************************
365  *              RealChildWindowFromPoint (USER32.@)
366  */
367 HWND WINAPI RealChildWindowFromPoint( HWND hwndParent, POINT pt )
368 {
369     return ChildWindowFromPointEx( hwndParent, pt, CWP_SKIPTRANSPARENT );
370 }
371
372 /*******************************************************************
373  *              ChildWindowFromPointEx (USER32.@)
374  */
375 HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
376 {
377     /* pt is in the client coordinates */
378     HWND *list;
379     int i;
380     RECT rect;
381     HWND retvalue;
382
383     GetClientRect( hwndParent, &rect );
384     if (!PtInRect( &rect, pt )) return 0;
385     if (!(list = WIN_ListChildren( hwndParent ))) return hwndParent;
386
387     for (i = 0; list[i]; i++)
388     {
389         if (!WIN_GetRectangles( list[i], &rect, NULL )) continue;
390         if (!PtInRect( &rect, pt )) continue;
391         if (uFlags & (CWP_SKIPINVISIBLE|CWP_SKIPDISABLED))
392         {
393             LONG style = GetWindowLongW( list[i], GWL_STYLE );
394             if ((uFlags & CWP_SKIPINVISIBLE) && !(style & WS_VISIBLE)) continue;
395             if ((uFlags & CWP_SKIPDISABLED) && (style & WS_DISABLED)) continue;
396         }
397         if (uFlags & CWP_SKIPTRANSPARENT)
398         {
399             if (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TRANSPARENT) continue;
400         }
401         break;
402     }
403     retvalue = list[i];
404     HeapFree( GetProcessHeap(), 0, list );
405     if (!retvalue) retvalue = hwndParent;
406     return retvalue;
407 }
408
409
410 /*******************************************************************
411  *         WINPOS_GetWinOffset
412  *
413  * Calculate the offset between the origin of the two windows. Used
414  * to implement MapWindowPoints.
415  */
416 static void WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo, POINT *offset )
417 {
418     WND * wndPtr;
419
420     offset->x = offset->y = 0;
421
422     /* Translate source window origin to screen coords */
423     if (hwndFrom)
424     {
425         HWND hwnd = hwndFrom;
426
427         while (hwnd)
428         {
429             if (hwnd == hwndTo) return;
430             if (!(wndPtr = WIN_GetPtr( hwnd )))
431             {
432                 ERR( "bad hwndFrom = %p\n", hwnd );
433                 return;
434             }
435             if (wndPtr == WND_DESKTOP) break;
436             if (wndPtr == WND_OTHER_PROCESS) goto other_process;
437             offset->x += wndPtr->rectClient.left;
438             offset->y += wndPtr->rectClient.top;
439             hwnd = wndPtr->parent;
440             WIN_ReleasePtr( wndPtr );
441         }
442     }
443
444     /* Translate origin to destination window coords */
445     if (hwndTo)
446     {
447         HWND hwnd = hwndTo;
448
449         while (hwnd)
450         {
451             if (!(wndPtr = WIN_GetPtr( hwnd )))
452             {
453                 ERR( "bad hwndTo = %p\n", hwnd );
454                 return;
455             }
456             if (wndPtr == WND_DESKTOP) break;
457             if (wndPtr == WND_OTHER_PROCESS) goto other_process;
458             offset->x -= wndPtr->rectClient.left;
459             offset->y -= wndPtr->rectClient.top;
460             hwnd = wndPtr->parent;
461             WIN_ReleasePtr( wndPtr );
462         }
463     }
464     return;
465
466  other_process:  /* one of the parents may belong to another process, do it the hard way */
467     offset->x = offset->y = 0;
468     SERVER_START_REQ( get_windows_offset )
469     {
470         req->from = hwndFrom;
471         req->to   = hwndTo;
472         if (!wine_server_call( req ))
473         {
474             offset->x = reply->x;
475             offset->y = reply->y;
476         }
477     }
478     SERVER_END_REQ;
479 }
480
481
482 /*******************************************************************
483  *              MapWindowPoints (USER.258)
484  */
485 void WINAPI MapWindowPoints16( HWND16 hwndFrom, HWND16 hwndTo,
486                                LPPOINT16 lppt, UINT16 count )
487 {
488     POINT offset;
489
490     WINPOS_GetWinOffset( WIN_Handle32(hwndFrom), WIN_Handle32(hwndTo), &offset );
491     while (count--)
492     {
493         lppt->x += offset.x;
494         lppt->y += offset.y;
495         lppt++;
496     }
497 }
498
499
500 /*******************************************************************
501  *              MapWindowPoints (USER32.@)
502  */
503 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
504 {
505     POINT offset;
506
507     WINPOS_GetWinOffset( hwndFrom, hwndTo, &offset );
508     while (count--)
509     {
510         lppt->x += offset.x;
511         lppt->y += offset.y;
512         lppt++;
513     }
514     return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
515 }
516
517
518 /***********************************************************************
519  *              IsIconic (USER32.@)
520  */
521 BOOL WINAPI IsIconic(HWND hWnd)
522 {
523     return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
524 }
525
526
527 /***********************************************************************
528  *              IsZoomed (USER32.@)
529  */
530 BOOL WINAPI IsZoomed(HWND hWnd)
531 {
532     return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
533 }
534
535
536 /*******************************************************************
537  *              AllowSetForegroundWindow (USER32.@)
538  */
539 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
540 {
541     /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
542      * implemented, then fix this function. */
543     return TRUE;
544 }
545
546
547 /*******************************************************************
548  *              LockSetForegroundWindow (USER32.@)
549  */
550 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
551 {
552     /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
553      * implemented, then fix this function. */
554     return TRUE;
555 }
556
557
558 /***********************************************************************
559  *              BringWindowToTop (USER32.@)
560  */
561 BOOL WINAPI BringWindowToTop( HWND hwnd )
562 {
563     return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
564 }
565
566
567 /***********************************************************************
568  *              MoveWindow (USER32.@)
569  */
570 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
571                             BOOL repaint )
572 {
573     int flags = SWP_NOZORDER | SWP_NOACTIVATE;
574     if (!repaint) flags |= SWP_NOREDRAW;
575     TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
576     return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
577 }
578
579 /***********************************************************************
580  *           WINPOS_InitPlacement
581  */
582 static void WINPOS_InitPlacement( WND* wnd )
583 {
584     if (IsRectEmpty( &wnd->normal_rect ))
585     {
586         /* this happens when the window is minimized/maximized
587          * for the first time (rectWindow is not adjusted yet) */
588
589         wnd->normal_rect = wnd->rectWindow;
590         wnd->min_pos.x = wnd->min_pos.y = -1;
591         wnd->max_pos.x = wnd->max_pos.y = -1;
592     }
593
594     if( wnd->dwStyle & WS_MINIMIZE )
595     {
596         wnd->min_pos.x = wnd->rectWindow.left;
597         wnd->min_pos.y = wnd->rectWindow.top;
598     }
599     else if( wnd->dwStyle & WS_MAXIMIZE )
600     {
601         wnd->max_pos.x = wnd->rectWindow.left;
602         wnd->max_pos.y = wnd->rectWindow.top;
603     }
604     else
605     {
606         wnd->normal_rect = wnd->rectWindow;
607     }
608 }
609
610 /***********************************************************************
611  *           WINPOS_RedrawIconTitle
612  */
613 BOOL WINPOS_RedrawIconTitle( HWND hWnd )
614 {
615     HWND icon_title = 0;
616     WND *win = WIN_GetPtr( hWnd );
617
618     if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
619     {
620         icon_title = win->icon_title;
621         WIN_ReleasePtr( win );
622     }
623     if (!icon_title) return FALSE;
624     SendMessageW( icon_title, WM_SHOWWINDOW, TRUE, 0 );
625     InvalidateRect( icon_title, NULL, TRUE );
626     return TRUE;
627 }
628
629 /***********************************************************************
630  *           WINPOS_ShowIconTitle
631  */
632 static BOOL WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
633 {
634     if (!GetPropA( hwnd, "__wine_x11_managed" ))
635     {
636         WND *win = WIN_GetPtr( hwnd );
637         HWND title = 0;
638
639         TRACE("%p %i\n", hwnd, (bShow != 0) );
640
641         if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP) return FALSE;
642         title = win->icon_title;
643         WIN_ReleasePtr( win );
644
645         if( bShow )
646         {
647             if (!title)
648             {
649                 title = ICONTITLE_Create( hwnd );
650                 if (!(win = WIN_GetPtr( hwnd )) || win == WND_OTHER_PROCESS)
651                 {
652                     DestroyWindow( title );
653                     return FALSE;
654                 }
655                 win->icon_title = title;
656                 WIN_ReleasePtr( win );
657             }
658             if (!IsWindowVisible(title))
659             {
660                 SendMessageW( title, WM_SHOWWINDOW, TRUE, 0 );
661                 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
662                               SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
663             }
664         }
665         else if (title) ShowWindow( title, SW_HIDE );
666     }
667     return FALSE;
668 }
669
670 /*******************************************************************
671  *           WINPOS_GetMinMaxInfo
672  *
673  * Get the minimized and maximized information for a window.
674  */
675 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
676                            POINT *minTrack, POINT *maxTrack )
677 {
678     MINMAXINFO MinMax;
679     HMONITOR monitor;
680     INT xinc, yinc;
681     LONG style = GetWindowLongA( hwnd, GWL_STYLE );
682     LONG exstyle = GetWindowLongA( hwnd, GWL_EXSTYLE );
683     RECT rc;
684     WND *win;
685
686     /* Compute default values */
687
688     GetWindowRect(hwnd, &rc);
689     MinMax.ptReserved.x = rc.left;
690     MinMax.ptReserved.y = rc.top;
691
692     if (style & WS_CHILD)
693     {
694         if ((style & WS_CAPTION) == WS_CAPTION)
695             style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
696
697         GetClientRect(GetAncestor(hwnd,GA_PARENT), &rc);
698         AdjustWindowRectEx(&rc, style, ((style & WS_POPUP) && GetMenu(hwnd)), exstyle);
699
700         /* avoid calculating this twice */
701         style &= ~(WS_DLGFRAME | WS_BORDER | WS_THICKFRAME);
702
703         MinMax.ptMaxSize.x = rc.right - rc.left;
704         MinMax.ptMaxSize.y = rc.bottom - rc.top;
705     }
706     else
707     {
708         MinMax.ptMaxSize.x = GetSystemMetrics(SM_CXSCREEN);
709         MinMax.ptMaxSize.y = GetSystemMetrics(SM_CYSCREEN);
710     }
711     MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
712     MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
713     MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXMAXTRACK);
714     MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYMAXTRACK);
715
716     if (HAS_DLGFRAME( style, exstyle ))
717     {
718         xinc = GetSystemMetrics(SM_CXDLGFRAME);
719         yinc = GetSystemMetrics(SM_CYDLGFRAME);
720     }
721     else
722     {
723         xinc = yinc = 0;
724         if (HAS_THICKFRAME(style))
725         {
726             xinc += GetSystemMetrics(SM_CXFRAME);
727             yinc += GetSystemMetrics(SM_CYFRAME);
728         }
729         if (style & WS_BORDER)
730         {
731             xinc += GetSystemMetrics(SM_CXBORDER);
732             yinc += GetSystemMetrics(SM_CYBORDER);
733         }
734     }
735     MinMax.ptMaxSize.x += 2 * xinc;
736     MinMax.ptMaxSize.y += 2 * yinc;
737
738     MinMax.ptMaxPosition.x = -xinc;
739     MinMax.ptMaxPosition.y = -yinc;
740     if ((win = WIN_GetPtr( hwnd )) && win != WND_DESKTOP && win != WND_OTHER_PROCESS)
741     {
742         if (!EMPTYPOINT(win->max_pos)) MinMax.ptMaxPosition = win->max_pos;
743         WIN_ReleasePtr( win );
744     }
745
746     SendMessageW( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
747
748     /* if the app didn't change the values, adapt them for the current monitor */
749
750     if ((monitor = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY )))
751     {
752         MONITORINFO mon_info;
753
754         mon_info.cbSize = sizeof(mon_info);
755         GetMonitorInfoW( monitor, &mon_info );
756
757         if (MinMax.ptMaxSize.x == GetSystemMetrics(SM_CXSCREEN) + 2 * xinc &&
758             MinMax.ptMaxSize.y == GetSystemMetrics(SM_CYSCREEN) + 2 * yinc)
759         {
760             MinMax.ptMaxSize.x = (mon_info.rcWork.right - mon_info.rcWork.left) + 2 * xinc;
761             MinMax.ptMaxSize.y = (mon_info.rcWork.bottom - mon_info.rcWork.top) + 2 * yinc;
762         }
763         if (MinMax.ptMaxPosition.x == -xinc && MinMax.ptMaxPosition.y == -yinc)
764         {
765             MinMax.ptMaxPosition.x = mon_info.rcWork.left - xinc;
766             MinMax.ptMaxPosition.y = mon_info.rcWork.top - yinc;
767         }
768     }
769
770       /* Some sanity checks */
771
772     TRACE("%d %d / %d %d / %d %d / %d %d\n",
773                       MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
774                       MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
775                       MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
776                       MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
777     MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
778                                    MinMax.ptMinTrackSize.x );
779     MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
780                                    MinMax.ptMinTrackSize.y );
781
782     if (maxSize) *maxSize = MinMax.ptMaxSize;
783     if (maxPos) *maxPos = MinMax.ptMaxPosition;
784     if (minTrack) *minTrack = MinMax.ptMinTrackSize;
785     if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
786 }
787
788
789 /***********************************************************************
790  *           WINPOS_FindIconPos
791  *
792  * Find a suitable place for an iconic window.
793  */
794 static POINT WINPOS_FindIconPos( HWND hwnd, POINT pt )
795 {
796     RECT rect, rectParent;
797     HWND parent, child;
798     HRGN hrgn, tmp;
799     int xspacing, yspacing;
800
801     parent = GetAncestor( hwnd, GA_PARENT );
802     GetClientRect( parent, &rectParent );
803     if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
804         (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
805         return pt;  /* The icon already has a suitable position */
806
807     xspacing = GetSystemMetrics(SM_CXICONSPACING);
808     yspacing = GetSystemMetrics(SM_CYICONSPACING);
809
810     /* Check if another icon already occupies this spot */
811     /* FIXME: this is completely inefficient */
812
813     hrgn = CreateRectRgn( 0, 0, 0, 0 );
814     tmp = CreateRectRgn( 0, 0, 0, 0 );
815     for (child = GetWindow( parent, GW_HWNDFIRST ); child; child = GetWindow( child, GW_HWNDNEXT ))
816     {
817         WND *childPtr;
818         if (child == hwnd) continue;
819         if ((GetWindowLongW( child, GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != (WS_VISIBLE|WS_MINIMIZE))
820             continue;
821         if (!(childPtr = WIN_GetPtr( child )) || childPtr == WND_OTHER_PROCESS)
822             continue;
823         SetRectRgn( tmp, childPtr->rectWindow.left, childPtr->rectWindow.top,
824                     childPtr->rectWindow.right, childPtr->rectWindow.bottom );
825         CombineRgn( hrgn, hrgn, tmp, RGN_OR );
826         WIN_ReleasePtr( childPtr );
827     }
828     DeleteObject( tmp );
829
830     for (rect.bottom = rectParent.bottom; rect.bottom >= yspacing; rect.bottom -= yspacing)
831     {
832         for (rect.left = rectParent.left; rect.left <= rectParent.right - xspacing; rect.left += xspacing)
833         {
834             rect.right = rect.left + xspacing;
835             rect.top = rect.bottom - yspacing;
836             if (!RectInRegion( hrgn, &rect ))
837             {
838                 /* No window was found, so it's OK for us */
839                 pt.x = rect.left + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
840                 pt.y = rect.top + (yspacing - GetSystemMetrics(SM_CYICON)) / 2;
841                 DeleteObject( hrgn );
842                 return pt;
843             }
844         }
845     }
846     DeleteObject( hrgn );
847     pt.x = pt.y = 0;
848     return pt;
849 }
850
851
852 /***********************************************************************
853  *           WINPOS_MinMaximize
854  */
855 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
856 {
857     WND *wndPtr;
858     UINT swpFlags = 0;
859     POINT size;
860     LONG old_style;
861     WINDOWPLACEMENT wpl;
862
863     TRACE("%p %u\n", hwnd, cmd );
864
865     wpl.length = sizeof(wpl);
866     GetWindowPlacement( hwnd, &wpl );
867
868     if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
869         return SWP_NOSIZE | SWP_NOMOVE;
870
871     if (IsIconic( hwnd ))
872     {
873         switch (cmd)
874         {
875         case SW_SHOWMINNOACTIVE:
876         case SW_SHOWMINIMIZED:
877         case SW_FORCEMINIMIZE:
878         case SW_MINIMIZE:
879             return SWP_NOSIZE | SWP_NOMOVE;
880         }
881         if (!SendMessageW( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
882         swpFlags |= SWP_NOCOPYBITS;
883     }
884
885     switch( cmd )
886     {
887     case SW_SHOWMINNOACTIVE:
888     case SW_SHOWMINIMIZED:
889     case SW_FORCEMINIMIZE:
890     case SW_MINIMIZE:
891         if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
892         if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
893         else wndPtr->flags &= ~WIN_RESTORE_MAX;
894         WIN_ReleasePtr( wndPtr );
895
896         old_style = WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
897
898         wpl.ptMinPosition = WINPOS_FindIconPos( hwnd, wpl.ptMinPosition );
899
900         if (!(old_style & WS_MINIMIZE)) swpFlags |= SWP_STATECHANGED;
901         SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
902                  GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON) );
903         swpFlags |= SWP_NOCOPYBITS;
904         break;
905
906     case SW_MAXIMIZE:
907         old_style = GetWindowLongW( hwnd, GWL_STYLE );
908         if ((old_style & WS_MAXIMIZE) && (old_style & WS_VISIBLE)) return SWP_NOSIZE | SWP_NOMOVE;
909
910         WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
911
912         old_style = WIN_SetStyle( hwnd, WS_MAXIMIZE, WS_MINIMIZE );
913         if (old_style & WS_MINIMIZE) WINPOS_ShowIconTitle( hwnd, FALSE );
914
915         if (!(old_style & WS_MAXIMIZE)) swpFlags |= SWP_STATECHANGED;
916         SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
917         break;
918
919     case SW_SHOWNOACTIVATE:
920     case SW_SHOWNORMAL:
921     case SW_RESTORE:
922         old_style = WIN_SetStyle( hwnd, 0, WS_MINIMIZE | WS_MAXIMIZE );
923         if (old_style & WS_MINIMIZE)
924         {
925             BOOL restore_max;
926
927             WINPOS_ShowIconTitle( hwnd, FALSE );
928
929             if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
930             restore_max = (wndPtr->flags & WIN_RESTORE_MAX) != 0;
931             WIN_ReleasePtr( wndPtr );
932             if (restore_max)
933             {
934                 /* Restore to maximized position */
935                 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
936                 WIN_SetStyle( hwnd, WS_MAXIMIZE, 0 );
937                 swpFlags |= SWP_STATECHANGED;
938                 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
939                 break;
940             }
941         }
942         else if (!(old_style & WS_MAXIMIZE)) break;
943
944         swpFlags |= SWP_STATECHANGED;
945
946         /* Restore to normal position */
947
948         *rect = wpl.rcNormalPosition;
949         rect->right -= rect->left;
950         rect->bottom -= rect->top;
951
952         break;
953     }
954
955     return swpFlags;
956 }
957
958
959 /***********************************************************************
960  *              show_window
961  *
962  * Implementation of ShowWindow and ShowWindowAsync.
963  */
964 static BOOL show_window( HWND hwnd, INT cmd )
965 {
966     WND *wndPtr;
967     HWND parent;
968     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
969     BOOL wasVisible = (style & WS_VISIBLE) != 0;
970     BOOL showFlag = TRUE;
971     RECT newPos = {0, 0, 0, 0};
972     UINT swp = 0;
973
974     TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);
975
976     switch(cmd)
977     {
978         case SW_HIDE:
979             if (!wasVisible) return FALSE;
980             showFlag = FALSE;
981             swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE;
982             if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
983             break;
984
985         case SW_SHOWMINNOACTIVE:
986         case SW_MINIMIZE:
987         case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
988             swp |= SWP_NOACTIVATE | SWP_NOZORDER;
989             /* fall through */
990         case SW_SHOWMINIMIZED:
991             swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
992             swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
993             if ((style & WS_MINIMIZE) && wasVisible) return TRUE;
994             break;
995
996         case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
997             if (!wasVisible) swp |= SWP_SHOWWINDOW;
998             swp |= SWP_FRAMECHANGED;
999             swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
1000             if ((style & WS_MAXIMIZE) && wasVisible) return TRUE;
1001             break;
1002
1003         case SW_SHOWNA:
1004             swp |= SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1005             if (style & WS_CHILD) swp |= SWP_NOZORDER;
1006             break;
1007         case SW_SHOW:
1008             if (wasVisible) return TRUE;
1009             swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1010             if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1011             break;
1012
1013         case SW_SHOWNOACTIVATE:
1014             swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1015             /* fall through */
1016         case SW_RESTORE:
1017             /* fall through */
1018         case SW_SHOWNORMAL:  /* same as SW_NORMAL: */
1019         case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
1020             if (!wasVisible) swp |= SWP_SHOWWINDOW;
1021             if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1022             {
1023                 swp |= SWP_FRAMECHANGED;
1024                 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
1025             }
1026             else
1027             {
1028                 if (wasVisible) return TRUE;
1029                 swp |= SWP_NOSIZE | SWP_NOMOVE;
1030             }
1031             if (style & WS_CHILD && !(swp & SWP_STATECHANGED)) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1032             break;
1033         default:
1034             return wasVisible;
1035     }
1036
1037     if ((showFlag != wasVisible || cmd == SW_SHOWNA) && cmd != SW_SHOWMAXIMIZED && !(swp & SWP_STATECHANGED))
1038     {
1039         SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
1040         if (!IsWindow( hwnd )) return wasVisible;
1041     }
1042
1043     parent = GetAncestor( hwnd, GA_PARENT );
1044     if (parent && !IsWindowVisible( parent ) && !(swp & SWP_STATECHANGED))
1045     {
1046         /* if parent is not visible simply toggle WS_VISIBLE and return */
1047         if (showFlag) WIN_SetStyle( hwnd, WS_VISIBLE, 0 );
1048         else WIN_SetStyle( hwnd, 0, WS_VISIBLE );
1049     }
1050     else
1051         SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1052                       newPos.right, newPos.bottom, LOWORD(swp) );
1053
1054     if (cmd == SW_HIDE)
1055     {
1056         HWND hFocus;
1057
1058         WINPOS_ShowIconTitle( hwnd, FALSE );
1059
1060         /* FIXME: This will cause the window to be activated irrespective
1061          * of whether it is owned by the same thread. Has to be done
1062          * asynchronously.
1063          */
1064
1065         if (hwnd == GetActiveWindow())
1066             WINPOS_ActivateOtherWindow(hwnd);
1067
1068         /* Revert focus to parent */
1069         hFocus = GetFocus();
1070         if (hwnd == hFocus || IsChild(hwnd, hFocus))
1071         {
1072             HWND parent = GetAncestor(hwnd, GA_PARENT);
1073             if (parent == GetDesktopWindow()) parent = 0;
1074             SetFocus(parent);
1075         }
1076     }
1077
1078     if (IsIconic(hwnd)) WINPOS_ShowIconTitle( hwnd, TRUE );
1079
1080     if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return wasVisible;
1081
1082     if (wndPtr->flags & WIN_NEED_SIZE)
1083     {
1084         /* should happen only in CreateWindowEx() */
1085         int wParam = SIZE_RESTORED;
1086         RECT client = wndPtr->rectClient;
1087
1088         wndPtr->flags &= ~WIN_NEED_SIZE;
1089         if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1090         else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
1091         WIN_ReleasePtr( wndPtr );
1092
1093         SendMessageW( hwnd, WM_SIZE, wParam,
1094                       MAKELONG( client.right - client.left, client.bottom - client.top ));
1095         SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( client.left, client.top ));
1096     }
1097     else WIN_ReleasePtr( wndPtr );
1098
1099     /* if previous state was minimized Windows sets focus to the window */
1100     if (style & WS_MINIMIZE) SetFocus( hwnd );
1101
1102     return wasVisible;
1103 }
1104
1105
1106 /***********************************************************************
1107  *              ShowWindowAsync (USER32.@)
1108  *
1109  * doesn't wait; returns immediately.
1110  * used by threads to toggle windows in other (possibly hanging) threads
1111  */
1112 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
1113 {
1114     HWND full_handle;
1115
1116     if (is_broadcast(hwnd))
1117     {
1118         SetLastError( ERROR_INVALID_PARAMETER );
1119         return FALSE;
1120     }
1121
1122     if ((full_handle = WIN_IsCurrentThread( hwnd )))
1123         return show_window( full_handle, cmd );
1124
1125     return SendNotifyMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
1126 }
1127
1128
1129 /***********************************************************************
1130  *              ShowWindow (USER32.@)
1131  */
1132 BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
1133 {
1134     HWND full_handle;
1135
1136     if (is_broadcast(hwnd))
1137     {
1138         SetLastError( ERROR_INVALID_PARAMETER );
1139         return FALSE;
1140     }
1141     if ((full_handle = WIN_IsCurrentThread( hwnd )))
1142         return show_window( full_handle, cmd );
1143
1144     return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
1145 }
1146
1147
1148 /***********************************************************************
1149  *              GetInternalWindowPos (USER32.@)
1150  */
1151 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
1152                                       LPPOINT ptIcon )
1153 {
1154     WINDOWPLACEMENT wndpl;
1155     if (GetWindowPlacement( hwnd, &wndpl ))
1156     {
1157         if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
1158         if (ptIcon)  *ptIcon = wndpl.ptMinPosition;
1159         return wndpl.showCmd;
1160     }
1161     return 0;
1162 }
1163
1164
1165 /***********************************************************************
1166  *              GetWindowPlacement (USER32.@)
1167  *
1168  * Win95:
1169  * Fails if wndpl->length of Win95 (!) apps is invalid.
1170  */
1171 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
1172 {
1173     WND *pWnd = WIN_GetPtr( hwnd );
1174
1175     if (!pWnd || pWnd == WND_DESKTOP) return FALSE;
1176     if (pWnd == WND_OTHER_PROCESS)
1177     {
1178         if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
1179         return FALSE;
1180     }
1181
1182     WINPOS_InitPlacement( pWnd );
1183     wndpl->length  = sizeof(*wndpl);
1184     if( pWnd->dwStyle & WS_MINIMIZE )
1185         wndpl->showCmd = SW_SHOWMINIMIZED;
1186     else
1187         wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
1188     if( pWnd->flags & WIN_RESTORE_MAX )
1189         wndpl->flags = WPF_RESTORETOMAXIMIZED;
1190     else
1191         wndpl->flags = 0;
1192     wndpl->ptMinPosition    = pWnd->min_pos;
1193     wndpl->ptMaxPosition    = pWnd->max_pos;
1194     wndpl->rcNormalPosition = pWnd->normal_rect;
1195     WIN_ReleasePtr( pWnd );
1196
1197     TRACE( "%p: returning min %d,%d max %d,%d normal %s\n",
1198            hwnd, wndpl->ptMinPosition.x, wndpl->ptMinPosition.y,
1199            wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y,
1200            wine_dbgstr_rect(&wndpl->rcNormalPosition) );
1201     return TRUE;
1202 }
1203
1204 /* make sure the specified rect is visible on screen */
1205 static void make_rect_onscreen( RECT *rect )
1206 {
1207     MONITORINFO info;
1208     HMONITOR monitor = MonitorFromRect( rect, MONITOR_DEFAULTTONEAREST );
1209
1210     info.cbSize = sizeof(info);
1211     if (!monitor || !GetMonitorInfoW( monitor, &info )) return;
1212     /* FIXME: map coordinates from rcWork to rcMonitor */
1213     if (rect->right <= info.rcWork.left)
1214     {
1215         rect->right += info.rcWork.left - rect->left;
1216         rect->left = info.rcWork.left;
1217     }
1218     else if (rect->left >= info.rcWork.right)
1219     {
1220         rect->left += info.rcWork.right - rect->right;
1221         rect->right = info.rcWork.right;
1222     }
1223     if (rect->bottom <= info.rcWork.top)
1224     {
1225         rect->bottom += info.rcWork.top - rect->top;
1226         rect->top = info.rcWork.top;
1227     }
1228     else if (rect->top >= info.rcWork.bottom)
1229     {
1230         rect->top += info.rcWork.bottom - rect->bottom;
1231         rect->bottom = info.rcWork.bottom;
1232     }
1233 }
1234
1235 /* make sure the specified point is visible on screen */
1236 static void make_point_onscreen( POINT *pt )
1237 {
1238     RECT rect;
1239
1240     SetRect( &rect, pt->x, pt->y, pt->x + 1, pt->y + 1 );
1241     make_rect_onscreen( &rect );
1242     pt->x = rect.left;
1243     pt->y = rect.top;
1244 }
1245
1246
1247 /***********************************************************************
1248  *           WINPOS_SetPlacement
1249  */
1250 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
1251 {
1252     DWORD style;
1253     WND *pWnd = WIN_GetPtr( hwnd );
1254     WINDOWPLACEMENT wp = *wndpl;
1255
1256     if (flags & PLACE_MIN) make_point_onscreen( &wp.ptMinPosition );
1257     if (flags & PLACE_MAX) make_point_onscreen( &wp.ptMaxPosition );
1258     if (flags & PLACE_RECT) make_rect_onscreen( &wp.rcNormalPosition );
1259
1260     TRACE( "%p: setting min %d,%d max %d,%d normal %s flags %x ajusted to min %d,%d max %d,%d normal %s\n",
1261            hwnd, wndpl->ptMinPosition.x, wndpl->ptMinPosition.y,
1262            wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y,
1263            wine_dbgstr_rect(&wndpl->rcNormalPosition), flags,
1264            wp.ptMinPosition.x, wp.ptMinPosition.y, wp.ptMaxPosition.x, wp.ptMaxPosition.y,
1265            wine_dbgstr_rect(&wp.rcNormalPosition) );
1266
1267     if (!pWnd || pWnd == WND_OTHER_PROCESS || pWnd == WND_DESKTOP) return FALSE;
1268
1269     if( flags & PLACE_MIN ) pWnd->min_pos = wp.ptMinPosition;
1270     if( flags & PLACE_MAX ) pWnd->max_pos = wp.ptMaxPosition;
1271     if( flags & PLACE_RECT) pWnd->normal_rect = wp.rcNormalPosition;
1272
1273     style = pWnd->dwStyle;
1274
1275     WIN_ReleasePtr( pWnd );
1276
1277     if( style & WS_MINIMIZE )
1278     {
1279         if (flags & PLACE_MIN)
1280         {
1281             WINPOS_ShowIconTitle( hwnd, FALSE );
1282             SetWindowPos( hwnd, 0, wp.ptMinPosition.x, wp.ptMinPosition.y, 0, 0,
1283                           SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1284         }
1285     }
1286     else if( style & WS_MAXIMIZE )
1287     {
1288         if (flags & PLACE_MAX)
1289             SetWindowPos( hwnd, 0, wp.ptMaxPosition.x, wp.ptMaxPosition.y, 0, 0,
1290                           SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1291     }
1292     else if( flags & PLACE_RECT )
1293         SetWindowPos( hwnd, 0, wp.rcNormalPosition.left, wp.rcNormalPosition.top,
1294                       wp.rcNormalPosition.right - wp.rcNormalPosition.left,
1295                       wp.rcNormalPosition.bottom - wp.rcNormalPosition.top,
1296                       SWP_NOZORDER | SWP_NOACTIVATE );
1297
1298     ShowWindow( hwnd, wndpl->showCmd );
1299
1300     if (IsIconic( hwnd ))
1301     {
1302         if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE) WINPOS_ShowIconTitle( hwnd, TRUE );
1303
1304         /* SDK: ...valid only the next time... */
1305         if( wndpl->flags & WPF_RESTORETOMAXIMIZED )
1306         {
1307             pWnd = WIN_GetPtr( hwnd );
1308             if (pWnd && pWnd != WND_OTHER_PROCESS)
1309             {
1310                 pWnd->flags |= WIN_RESTORE_MAX;
1311                 WIN_ReleasePtr( pWnd );
1312             }
1313         }
1314     }
1315     return TRUE;
1316 }
1317
1318
1319 /***********************************************************************
1320  *              SetWindowPlacement (USER32.@)
1321  *
1322  * Win95:
1323  * Fails if wndpl->length of Win95 (!) apps is invalid.
1324  */
1325 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
1326 {
1327     UINT flags = PLACE_MAX | PLACE_RECT;
1328     if (!wpl) return FALSE;
1329     if (wpl->flags & WPF_SETMINPOSITION) flags |= PLACE_MIN;
1330     return WINPOS_SetPlacement( hwnd, wpl, flags );
1331 }
1332
1333
1334 /***********************************************************************
1335  *              AnimateWindow (USER32.@)
1336  *              Shows/Hides a window with an animation
1337  *              NO ANIMATION YET
1338  */
1339 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
1340 {
1341         FIXME("partial stub\n");
1342
1343         /* If trying to show/hide and it's already   *
1344          * shown/hidden or invalid window, fail with *
1345          * invalid parameter                         */
1346         if(!IsWindow(hwnd) ||
1347            (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
1348            (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
1349         {
1350                 SetLastError(ERROR_INVALID_PARAMETER);
1351                 return FALSE;
1352         }
1353
1354         ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1355
1356         return TRUE;
1357 }
1358
1359 /***********************************************************************
1360  *              SetInternalWindowPos (USER32.@)
1361  */
1362 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1363                                     LPRECT rect, LPPOINT pt )
1364 {
1365     WINDOWPLACEMENT wndpl;
1366     UINT flags;
1367
1368     wndpl.length  = sizeof(wndpl);
1369     wndpl.showCmd = showCmd;
1370     wndpl.flags = flags = 0;
1371
1372     if( pt )
1373     {
1374         flags |= PLACE_MIN;
1375         wndpl.flags |= WPF_SETMINPOSITION;
1376         wndpl.ptMinPosition = *pt;
1377     }
1378     if( rect )
1379     {
1380         flags |= PLACE_RECT;
1381         wndpl.rcNormalPosition = *rect;
1382     }
1383     WINPOS_SetPlacement( hwnd, &wndpl, flags );
1384 }
1385
1386
1387 /*******************************************************************
1388  *         can_activate_window
1389  *
1390  * Check if we can activate the specified window.
1391  */
1392 static BOOL can_activate_window( HWND hwnd )
1393 {
1394     LONG style;
1395
1396     if (!hwnd) return FALSE;
1397     style = GetWindowLongW( hwnd, GWL_STYLE );
1398     if (!(style & WS_VISIBLE)) return FALSE;
1399     if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1400     return !(style & WS_DISABLED);
1401 }
1402
1403
1404 /*******************************************************************
1405  *         WINPOS_ActivateOtherWindow
1406  *
1407  *  Activates window other than pWnd.
1408  */
1409 void WINPOS_ActivateOtherWindow(HWND hwnd)
1410 {
1411     HWND hwndTo, fg;
1412
1413     if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1414     {
1415         hwndTo = GetAncestor( hwndTo, GA_ROOT );
1416         if (can_activate_window( hwndTo )) goto done;
1417     }
1418
1419     hwndTo = hwnd;
1420     for (;;)
1421     {
1422         if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1423         if (can_activate_window( hwndTo )) break;
1424     }
1425
1426  done:
1427     fg = GetForegroundWindow();
1428     TRACE("win = %p fg = %p\n", hwndTo, fg);
1429     if (!fg || (hwnd == fg))
1430     {
1431         if (SetForegroundWindow( hwndTo )) return;
1432     }
1433     if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1434 }
1435
1436
1437 /***********************************************************************
1438  *           WINPOS_HandleWindowPosChanging
1439  *
1440  * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1441  */
1442 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1443 {
1444     POINT minTrack, maxTrack;
1445     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1446
1447     if (winpos->flags & SWP_NOSIZE) return 0;
1448     if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1449     {
1450         WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1451         if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1452         if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1453         if (!(style & WS_MINIMIZE))
1454         {
1455             if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1456             if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1457         }
1458     }
1459     return 0;
1460 }
1461
1462
1463 /***********************************************************************
1464  *           dump_winpos_flags
1465  */
1466 static void dump_winpos_flags(UINT flags)
1467 {
1468     TRACE("flags:");
1469     if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1470     if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1471     if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1472     if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1473     if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1474     if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1475     if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1476     if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1477     if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1478     if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1479     if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1480     if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1481     if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1482
1483 #define DUMPED_FLAGS \
1484     (SWP_NOSIZE | \
1485     SWP_NOMOVE | \
1486     SWP_NOZORDER | \
1487     SWP_NOREDRAW | \
1488     SWP_NOACTIVATE | \
1489     SWP_FRAMECHANGED | \
1490     SWP_SHOWWINDOW | \
1491     SWP_HIDEWINDOW | \
1492     SWP_NOCOPYBITS | \
1493     SWP_NOOWNERZORDER | \
1494     SWP_NOSENDCHANGING | \
1495     SWP_DEFERERASE | \
1496     SWP_ASYNCWINDOWPOS)
1497
1498     if(flags & ~DUMPED_FLAGS) TRACE(" %08x", flags & ~DUMPED_FLAGS);
1499     TRACE("\n");
1500 #undef DUMPED_FLAGS
1501 }
1502
1503 /***********************************************************************
1504  *           SWP_DoWinPosChanging
1505  */
1506 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
1507 {
1508     WND *wndPtr;
1509
1510     /* Send WM_WINDOWPOSCHANGING message */
1511
1512     if (!(pWinpos->flags & SWP_NOSENDCHANGING))
1513         SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
1514
1515     if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) ||
1516         wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
1517
1518     /* Calculate new position and size */
1519
1520     *pNewWindowRect = wndPtr->rectWindow;
1521     *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
1522                                                     : wndPtr->rectClient;
1523
1524     if (!(pWinpos->flags & SWP_NOSIZE))
1525     {
1526         pNewWindowRect->right  = pNewWindowRect->left + pWinpos->cx;
1527         pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
1528     }
1529     if (!(pWinpos->flags & SWP_NOMOVE))
1530     {
1531         pNewWindowRect->left    = pWinpos->x;
1532         pNewWindowRect->top     = pWinpos->y;
1533         pNewWindowRect->right  += pWinpos->x - wndPtr->rectWindow.left;
1534         pNewWindowRect->bottom += pWinpos->y - wndPtr->rectWindow.top;
1535
1536         OffsetRect( pNewClientRect, pWinpos->x - wndPtr->rectWindow.left,
1537                                     pWinpos->y - wndPtr->rectWindow.top );
1538     }
1539     pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
1540
1541     TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
1542            pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
1543            pWinpos->cx, pWinpos->cy, pWinpos->flags );
1544     TRACE( "current %s style %08x new %s\n",
1545            wine_dbgstr_rect( &wndPtr->rectWindow ), wndPtr->dwStyle,
1546            wine_dbgstr_rect( pNewWindowRect ));
1547
1548     WIN_ReleasePtr( wndPtr );
1549     return TRUE;
1550 }
1551
1552 /***********************************************************************
1553  *           get_valid_rects
1554  *
1555  * Compute the valid rects from the old and new client rect and WVR_* flags.
1556  * Helper for WM_NCCALCSIZE handling.
1557  */
1558 static inline void get_valid_rects( const RECT *old_client, const RECT *new_client, UINT flags,
1559                                     RECT *valid )
1560 {
1561     int cx, cy;
1562
1563     if (flags & WVR_REDRAW)
1564     {
1565         SetRectEmpty( &valid[0] );
1566         SetRectEmpty( &valid[1] );
1567         return;
1568     }
1569
1570     if (flags & WVR_VALIDRECTS)
1571     {
1572         if (!IntersectRect( &valid[0], &valid[0], new_client ) ||
1573             !IntersectRect( &valid[1], &valid[1], old_client ))
1574         {
1575             SetRectEmpty( &valid[0] );
1576             SetRectEmpty( &valid[1] );
1577             return;
1578         }
1579         flags = WVR_ALIGNLEFT | WVR_ALIGNTOP;
1580     }
1581     else
1582     {
1583         valid[0] = *new_client;
1584         valid[1] = *old_client;
1585     }
1586
1587     /* make sure the rectangles have the same size */
1588     cx = min( valid[0].right - valid[0].left, valid[1].right - valid[1].left );
1589     cy = min( valid[0].bottom - valid[0].top, valid[1].bottom - valid[1].top );
1590
1591     if (flags & WVR_ALIGNBOTTOM)
1592     {
1593         valid[0].top = valid[0].bottom - cy;
1594         valid[1].top = valid[1].bottom - cy;
1595     }
1596     else
1597     {
1598         valid[0].bottom = valid[0].top + cy;
1599         valid[1].bottom = valid[1].top + cy;
1600     }
1601     if (flags & WVR_ALIGNRIGHT)
1602     {
1603         valid[0].left = valid[0].right - cx;
1604         valid[1].left = valid[1].right - cx;
1605     }
1606     else
1607     {
1608         valid[0].right = valid[0].left + cx;
1609         valid[1].right = valid[1].left + cx;
1610     }
1611 }
1612
1613
1614 /***********************************************************************
1615  *           SWP_DoOwnedPopups
1616  *
1617  * fix Z order taking into account owned popups -
1618  * basically we need to maintain them above the window that owns them
1619  *
1620  * FIXME: hide/show owned popups when owner visibility changes.
1621  */
1622 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
1623 {
1624     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1625     HWND owner, *list = NULL;
1626     unsigned int i;
1627
1628     TRACE("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
1629
1630     if ((style & WS_POPUP) && (owner = GetWindow( hwnd, GW_OWNER )))
1631     {
1632         /* make sure this popup stays above the owner */
1633
1634         if (hwndInsertAfter != HWND_TOP && hwndInsertAfter != HWND_TOPMOST)
1635         {
1636             if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return hwndInsertAfter;
1637
1638             for (i = 0; list[i]; i++)
1639             {
1640                 if (list[i] == owner)
1641                 {
1642                     if (i > 0) hwndInsertAfter = list[i-1];
1643                     else hwndInsertAfter = HWND_TOP;
1644                     break;
1645                 }
1646
1647                 if (hwndInsertAfter == HWND_NOTOPMOST)
1648                 {
1649                     if (!(GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST)) break;
1650                 }
1651                 else if (list[i] == hwndInsertAfter) break;
1652             }
1653         }
1654     }
1655     else if (style & WS_CHILD) return hwndInsertAfter;
1656
1657     if (hwndInsertAfter == HWND_BOTTOM) goto done;
1658     if (!list && !(list = WIN_ListChildren( GetDesktopWindow() ))) goto done;
1659
1660     i = 0;
1661     if (hwndInsertAfter == HWND_TOP || hwndInsertAfter == HWND_NOTOPMOST)
1662     {
1663         if (hwndInsertAfter == HWND_NOTOPMOST || !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_TOPMOST))
1664         {
1665             /* skip all the topmost windows */
1666             while (list[i] && (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST)) i++;
1667         }
1668     }
1669     else if (hwndInsertAfter != HWND_TOPMOST)
1670     {
1671         /* skip windows that are already placed correctly */
1672         for (i = 0; list[i]; i++)
1673         {
1674             if (list[i] == hwndInsertAfter) break;
1675             if (list[i] == hwnd) goto done;  /* nothing to do if window is moving backwards in z-order */
1676         }
1677     }
1678
1679     for ( ; list[i]; i++)
1680     {
1681         if (list[i] == hwnd) break;
1682         if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_POPUP)) continue;
1683         if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
1684         TRACE( "moving %p owned by %p after %p\n", list[i], hwnd, hwndInsertAfter );
1685         SetWindowPos( list[i], hwndInsertAfter, 0, 0, 0, 0,
1686                       SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING | SWP_DEFERERASE );
1687         hwndInsertAfter = list[i];
1688     }
1689
1690 done:
1691     HeapFree( GetProcessHeap(), 0, list );
1692     return hwndInsertAfter;
1693 }
1694
1695 /***********************************************************************
1696  *           SWP_DoNCCalcSize
1697  */
1698 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect,
1699                               RECT *validRects )
1700 {
1701     UINT wvrFlags = 0;
1702     WND *wndPtr;
1703
1704     if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
1705
1706       /* Send WM_NCCALCSIZE message to get new client area */
1707     if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
1708     {
1709         NCCALCSIZE_PARAMS params;
1710         WINDOWPOS winposCopy;
1711
1712         params.rgrc[0] = *pNewWindowRect;
1713         params.rgrc[1] = wndPtr->rectWindow;
1714         params.rgrc[2] = wndPtr->rectClient;
1715         params.lppos = &winposCopy;
1716         winposCopy = *pWinpos;
1717         WIN_ReleasePtr( wndPtr );
1718
1719         wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
1720
1721         *pNewClientRect = params.rgrc[0];
1722
1723         if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
1724
1725         TRACE( "hwnd %p old win %s old client %s new win %s new client %s\n", pWinpos->hwnd,
1726                wine_dbgstr_rect(&wndPtr->rectWindow), wine_dbgstr_rect(&wndPtr->rectClient),
1727                wine_dbgstr_rect(pNewWindowRect), wine_dbgstr_rect(pNewClientRect) );
1728
1729         if( pNewClientRect->left != wndPtr->rectClient.left ||
1730             pNewClientRect->top != wndPtr->rectClient.top )
1731             pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1732
1733         if( (pNewClientRect->right - pNewClientRect->left !=
1734              wndPtr->rectClient.right - wndPtr->rectClient.left))
1735             pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1736         else
1737             wvrFlags &= ~WVR_HREDRAW;
1738
1739         if (pNewClientRect->bottom - pNewClientRect->top !=
1740              wndPtr->rectClient.bottom - wndPtr->rectClient.top)
1741             pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1742         else
1743             wvrFlags &= ~WVR_VREDRAW;
1744
1745         validRects[0] = params.rgrc[1];
1746         validRects[1] = params.rgrc[2];
1747     }
1748     else
1749     {
1750         if (!(pWinpos->flags & SWP_NOMOVE) &&
1751             (pNewClientRect->left != wndPtr->rectClient.left ||
1752              pNewClientRect->top != wndPtr->rectClient.top))
1753             pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1754     }
1755
1756     if (pWinpos->flags & (SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
1757     {
1758         SetRectEmpty( &validRects[0] );
1759         SetRectEmpty( &validRects[1] );
1760     }
1761     else get_valid_rects( &wndPtr->rectClient, pNewClientRect, wvrFlags, validRects );
1762
1763     WIN_ReleasePtr( wndPtr );
1764     return wvrFlags;
1765 }
1766
1767 /* fix redundant flags and values in the WINDOWPOS structure */
1768 static BOOL fixup_flags( WINDOWPOS *winpos )
1769 {
1770     HWND parent;
1771     WND *wndPtr = WIN_GetPtr( winpos->hwnd );
1772     BOOL ret = TRUE;
1773
1774     if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
1775     {
1776         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1777         return FALSE;
1778     }
1779     winpos->hwnd = wndPtr->hwndSelf;  /* make it a full handle */
1780
1781     /* Finally make sure that all coordinates are valid */
1782     if (winpos->x < -32768) winpos->x = -32768;
1783     else if (winpos->x > 32767) winpos->x = 32767;
1784     if (winpos->y < -32768) winpos->y = -32768;
1785     else if (winpos->y > 32767) winpos->y = 32767;
1786
1787     if (winpos->cx < 0) winpos->cx = 0;
1788     else if (winpos->cx > 32767) winpos->cx = 32767;
1789     if (winpos->cy < 0) winpos->cy = 0;
1790     else if (winpos->cy > 32767) winpos->cy = 32767;
1791
1792     parent = GetAncestor( winpos->hwnd, GA_PARENT );
1793     if (!IsWindowVisible( parent )) winpos->flags |= SWP_NOREDRAW;
1794
1795     if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
1796     else
1797     {
1798         winpos->flags &= ~SWP_HIDEWINDOW;
1799         if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
1800     }
1801
1802     if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
1803         (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
1804         winpos->flags |= SWP_NOSIZE;    /* Already the right size */
1805
1806     if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
1807         winpos->flags |= SWP_NOMOVE;    /* Already the right position */
1808
1809     if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
1810     {
1811         if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)) && /* Bring to the top when activating */
1812             (winpos->flags & SWP_NOZORDER ||
1813              (winpos->hwndInsertAfter != HWND_TOPMOST && winpos->hwndInsertAfter != HWND_NOTOPMOST)))
1814         {
1815             winpos->flags &= ~SWP_NOZORDER;
1816             winpos->hwndInsertAfter = HWND_TOP;
1817         }
1818     }
1819
1820     /* Check hwndInsertAfter */
1821     if (winpos->flags & SWP_NOZORDER) goto done;
1822
1823     /* fix sign extension */
1824     if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
1825     else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
1826
1827     /* hwndInsertAfter must be a sibling of the window */
1828     if (winpos->hwndInsertAfter == HWND_TOP)
1829     {
1830         if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1831             winpos->flags |= SWP_NOZORDER;
1832     }
1833     else if (winpos->hwndInsertAfter == HWND_BOTTOM)
1834     {
1835         if (!(wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
1836             winpos->flags |= SWP_NOZORDER;
1837     }
1838     else if (winpos->hwndInsertAfter == HWND_TOPMOST)
1839     {
1840         if ((wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1841             winpos->flags |= SWP_NOZORDER;
1842     }
1843     else if (winpos->hwndInsertAfter == HWND_NOTOPMOST)
1844     {
1845         if (!(wndPtr->dwExStyle & WS_EX_TOPMOST))
1846             winpos->flags |= SWP_NOZORDER;
1847     }
1848     else
1849     {
1850         if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != parent) ret = FALSE;
1851         else
1852         {
1853             /* don't need to change the Zorder of hwnd if it's already inserted
1854              * after hwndInsertAfter or when inserting hwnd after itself.
1855              */
1856             if ((winpos->hwnd == winpos->hwndInsertAfter) ||
1857                 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
1858                 winpos->flags |= SWP_NOZORDER;
1859         }
1860     }
1861  done:
1862     WIN_ReleasePtr( wndPtr );
1863     return ret;
1864 }
1865
1866
1867 /***********************************************************************
1868  *              set_window_pos
1869  *
1870  * Backend implementation of SetWindowPos.
1871  */
1872 BOOL set_window_pos( HWND hwnd, HWND insert_after, UINT swp_flags,
1873                      const RECT *window_rect, const RECT *client_rect, const RECT *valid_rects )
1874 {
1875     WND *win;
1876     BOOL ret;
1877     RECT visible_rect, old_window_rect;
1878     DWORD new_style;
1879
1880     if (!(win = WIN_GetPtr( hwnd ))) return FALSE;
1881     if (win == WND_DESKTOP || win == WND_OTHER_PROCESS) return FALSE;
1882
1883     old_window_rect = win->rectWindow;
1884     SERVER_START_REQ( set_window_pos )
1885     {
1886         req->handle        = hwnd;
1887         req->previous      = insert_after;
1888         req->flags         = swp_flags;
1889         req->window.left   = window_rect->left;
1890         req->window.top    = window_rect->top;
1891         req->window.right  = window_rect->right;
1892         req->window.bottom = window_rect->bottom;
1893         req->client.left   = client_rect->left;
1894         req->client.top    = client_rect->top;
1895         req->client.right  = client_rect->right;
1896         req->client.bottom = client_rect->bottom;
1897         if (!IsRectEmpty( &valid_rects[0] ))
1898             wine_server_add_data( req, valid_rects, 2 * sizeof(*valid_rects) );
1899         if ((ret = !wine_server_call( req )))
1900         {
1901             win->dwStyle    = reply->new_style;
1902             win->dwExStyle  = reply->new_ex_style;
1903             win->rectWindow = *window_rect;
1904             win->rectClient = *client_rect;
1905             visible_rect.left   = reply->visible.left;
1906             visible_rect.top    = reply->visible.top;
1907             visible_rect.right  = reply->visible.right;
1908             visible_rect.bottom = reply->visible.bottom;
1909         }
1910     }
1911     SERVER_END_REQ;
1912     new_style = win->dwStyle;
1913     WIN_ReleasePtr( win );
1914
1915     if (ret)
1916     {
1917         USER_Driver->pSetWindowPos( hwnd, insert_after, swp_flags, window_rect,
1918                                     client_rect, &visible_rect, valid_rects );
1919
1920         if ((((swp_flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) && (new_style & WS_VISIBLE)) ||
1921             (swp_flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW)))
1922             invalidate_dce( hwnd, &old_window_rect );
1923     }
1924     return ret;
1925 }
1926
1927
1928 /***********************************************************************
1929  *              USER_SetWindowPos
1930  *
1931  *     User32 internal function
1932  */
1933 BOOL USER_SetWindowPos( WINDOWPOS * winpos )
1934 {
1935     RECT newWindowRect, newClientRect, valid_rects[2];
1936     UINT orig_flags;
1937     
1938     orig_flags = winpos->flags;
1939     
1940     /* First make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
1941     if (!(winpos->flags & SWP_NOMOVE))
1942     {
1943         if (winpos->x < -32768) winpos->x = -32768;
1944         else if (winpos->x > 32767) winpos->x = 32767;
1945         if (winpos->y < -32768) winpos->y = -32768;
1946         else if (winpos->y > 32767) winpos->y = 32767;
1947     }
1948     if (!(winpos->flags & SWP_NOSIZE))
1949     {
1950         if (winpos->cx < 0) winpos->cx = 0;
1951         else if (winpos->cx > 32767) winpos->cx = 32767;
1952         if (winpos->cy < 0) winpos->cy = 0;
1953         else if (winpos->cy > 32767) winpos->cy = 32767;
1954     }
1955
1956     if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
1957
1958     /* Fix redundant flags */
1959     if (!fixup_flags( winpos )) return FALSE;
1960
1961     if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
1962     {
1963         if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
1964             winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
1965     }
1966
1967     /* Common operations */
1968
1969     SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect, valid_rects );
1970
1971     if (!set_window_pos( winpos->hwnd, winpos->hwndInsertAfter, winpos->flags,
1972                          &newWindowRect, &newClientRect, valid_rects ))
1973         return FALSE;
1974
1975     /* erase parent when hiding or resizing child */
1976     if (!(orig_flags & SWP_DEFERERASE) &&
1977         ((orig_flags & SWP_HIDEWINDOW) ||
1978          (!(orig_flags & SWP_SHOWWINDOW) &&
1979           (winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOGEOMETRYCHANGE)))
1980     {
1981         HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
1982         if (!parent || parent == GetDesktopWindow()) parent = winpos->hwnd;
1983         erase_now( parent, 0 );
1984     }
1985
1986     if( winpos->flags & SWP_HIDEWINDOW )
1987         HideCaret(winpos->hwnd);
1988     else if (winpos->flags & SWP_SHOWWINDOW)
1989         ShowCaret(winpos->hwnd);
1990
1991     if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)))
1992     {
1993         /* child windows get WM_CHILDACTIVATE message */
1994         if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
1995             SendMessageW( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
1996         else
1997             SetForegroundWindow( winpos->hwnd );
1998     }
1999
2000       /* And last, send the WM_WINDOWPOSCHANGED message */
2001
2002     TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
2003
2004     if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
2005     {
2006         /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
2007            and always contains final window position.
2008          */
2009         winpos->x = newWindowRect.left;
2010         winpos->y = newWindowRect.top;
2011         winpos->cx = newWindowRect.right - newWindowRect.left;
2012         winpos->cy = newWindowRect.bottom - newWindowRect.top;
2013         SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
2014     }
2015     return TRUE;
2016 }
2017
2018 /***********************************************************************
2019  *              SetWindowPos (USER32.@)
2020  */
2021 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
2022                           INT x, INT y, INT cx, INT cy, UINT flags )
2023 {
2024     WINDOWPOS winpos;
2025
2026     TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2027           hwnd, hwndInsertAfter, x, y, cx, cy, flags);
2028     if(TRACE_ON(win)) dump_winpos_flags(flags);
2029
2030     if (is_broadcast(hwnd))
2031     {
2032         SetLastError( ERROR_INVALID_PARAMETER );
2033         return FALSE;
2034     }
2035
2036     winpos.hwnd = WIN_GetFullHandle(hwnd);
2037     winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
2038     winpos.x = x;
2039     winpos.y = y;
2040     winpos.cx = cx;
2041     winpos.cy = cy;
2042     winpos.flags = flags;
2043     
2044     if (WIN_IsCurrentThread( hwnd ))
2045         return USER_SetWindowPos(&winpos);
2046
2047     return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
2048 }
2049
2050
2051 /***********************************************************************
2052  *              BeginDeferWindowPos (USER32.@)
2053  */
2054 HDWP WINAPI BeginDeferWindowPos( INT count )
2055 {
2056     HDWP handle;
2057     DWP *pDWP;
2058
2059     TRACE("%d\n", count);
2060
2061     if (count < 0)
2062     {
2063         SetLastError(ERROR_INVALID_PARAMETER);
2064         return 0;
2065     }
2066     /* Windows allows zero count, in which case it allocates context for 8 moves */
2067     if (count == 0) count = 8;
2068
2069     handle = USER_HEAP_ALLOC( sizeof(DWP) + (count-1)*sizeof(WINDOWPOS) );
2070     if (!handle) return 0;
2071     pDWP = (DWP *) USER_HEAP_LIN_ADDR( handle );
2072     pDWP->actualCount    = 0;
2073     pDWP->suggestedCount = count;
2074     pDWP->valid          = TRUE;
2075     pDWP->wMagic         = DWP_MAGIC;
2076     pDWP->hwndParent     = 0;
2077
2078     TRACE("returning hdwp %p\n", handle);
2079     return handle;
2080 }
2081
2082
2083 /***********************************************************************
2084  *              DeferWindowPos (USER32.@)
2085  */
2086 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
2087                                 INT x, INT y, INT cx, INT cy,
2088                                 UINT flags )
2089 {
2090     DWP *pDWP;
2091     int i;
2092     HDWP newhdwp = hdwp,retvalue;
2093
2094     TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2095           hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
2096
2097     hwnd = WIN_GetFullHandle( hwnd );
2098     if (hwnd == GetDesktopWindow()) return 0;
2099
2100     if (!(pDWP = USER_HEAP_LIN_ADDR( hdwp ))) return 0;
2101
2102     USER_Lock();
2103
2104     for (i = 0; i < pDWP->actualCount; i++)
2105     {
2106         if (pDWP->winPos[i].hwnd == hwnd)
2107         {
2108               /* Merge with the other changes */
2109             if (!(flags & SWP_NOZORDER))
2110             {
2111                 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
2112             }
2113             if (!(flags & SWP_NOMOVE))
2114             {
2115                 pDWP->winPos[i].x = x;
2116                 pDWP->winPos[i].y = y;
2117             }
2118             if (!(flags & SWP_NOSIZE))
2119             {
2120                 pDWP->winPos[i].cx = cx;
2121                 pDWP->winPos[i].cy = cy;
2122             }
2123             pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
2124                                                SWP_NOZORDER | SWP_NOREDRAW |
2125                                                SWP_NOACTIVATE | SWP_NOCOPYBITS|
2126                                                SWP_NOOWNERZORDER);
2127             pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
2128                                               SWP_FRAMECHANGED);
2129             retvalue = hdwp;
2130             goto END;
2131         }
2132     }
2133     if (pDWP->actualCount >= pDWP->suggestedCount)
2134     {
2135         newhdwp = USER_HEAP_REALLOC( hdwp,
2136                       sizeof(DWP) + pDWP->suggestedCount*sizeof(WINDOWPOS) );
2137         if (!newhdwp)
2138         {
2139             retvalue = 0;
2140             goto END;
2141         }
2142         pDWP = (DWP *) USER_HEAP_LIN_ADDR( newhdwp );
2143         pDWP->suggestedCount++;
2144     }
2145     pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
2146     pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
2147     pDWP->winPos[pDWP->actualCount].x = x;
2148     pDWP->winPos[pDWP->actualCount].y = y;
2149     pDWP->winPos[pDWP->actualCount].cx = cx;
2150     pDWP->winPos[pDWP->actualCount].cy = cy;
2151     pDWP->winPos[pDWP->actualCount].flags = flags;
2152     pDWP->actualCount++;
2153     retvalue = newhdwp;
2154 END:
2155     USER_Unlock();
2156     return retvalue;
2157 }
2158
2159
2160 /***********************************************************************
2161  *              EndDeferWindowPos (USER32.@)
2162  */
2163 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
2164 {
2165     DWP *pDWP;
2166     WINDOWPOS *winpos;
2167     BOOL res = TRUE;
2168     int i;
2169
2170     TRACE("%p\n", hdwp);
2171
2172     pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
2173     if (!pDWP) return FALSE;
2174     for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
2175     {
2176         TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2177                winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
2178                winpos->cx, winpos->cy, winpos->flags);
2179
2180         if (!(res = USER_SetWindowPos( winpos ))) break;
2181     }
2182     USER_HEAP_FREE( hdwp );
2183     return res;
2184 }
2185
2186
2187 /***********************************************************************
2188  *              ArrangeIconicWindows (USER32.@)
2189  */
2190 UINT WINAPI ArrangeIconicWindows( HWND parent )
2191 {
2192     RECT rectParent;
2193     HWND hwndChild;
2194     INT x, y, xspacing, yspacing;
2195
2196     GetClientRect( parent, &rectParent );
2197     x = rectParent.left;
2198     y = rectParent.bottom;
2199     xspacing = GetSystemMetrics(SM_CXICONSPACING);
2200     yspacing = GetSystemMetrics(SM_CYICONSPACING);
2201
2202     hwndChild = GetWindow( parent, GW_CHILD );
2203     while (hwndChild)
2204     {
2205         if( IsIconic( hwndChild ) )
2206         {
2207             WINPOS_ShowIconTitle( hwndChild, FALSE );
2208
2209             SetWindowPos( hwndChild, 0, x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
2210                             y - yspacing - GetSystemMetrics(SM_CYICON)/2, 0, 0,
2211                             SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
2212             if( IsWindow(hwndChild) )
2213                 WINPOS_ShowIconTitle(hwndChild , TRUE );
2214
2215             if (x <= rectParent.right - xspacing) x += xspacing;
2216             else
2217             {
2218                 x = rectParent.left;
2219                 y -= yspacing;
2220             }
2221         }
2222         hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
2223     }
2224     return yspacing;
2225 }
2226
2227
2228 /***********************************************************************
2229  *           draw_moving_frame
2230  *
2231  * Draw the frame used when moving or resizing window.
2232  */
2233 static void draw_moving_frame( HDC hdc, RECT *rect, BOOL thickframe )
2234 {
2235     if (thickframe)
2236     {
2237         const int width = GetSystemMetrics(SM_CXFRAME);
2238         const int height = GetSystemMetrics(SM_CYFRAME);
2239
2240         HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
2241         PatBlt( hdc, rect->left, rect->top,
2242                 rect->right - rect->left - width, height, PATINVERT );
2243         PatBlt( hdc, rect->left, rect->top + height, width,
2244                 rect->bottom - rect->top - height, PATINVERT );
2245         PatBlt( hdc, rect->left + width, rect->bottom - 1,
2246                 rect->right - rect->left - width, -height, PATINVERT );
2247         PatBlt( hdc, rect->right - 1, rect->top, -width,
2248                 rect->bottom - rect->top - height, PATINVERT );
2249         SelectObject( hdc, hbrush );
2250     }
2251     else DrawFocusRect( hdc, rect );
2252 }
2253
2254
2255 /***********************************************************************
2256  *           start_size_move
2257  *
2258  * Initialization of a move or resize, when initiated from a menu choice.
2259  * Return hit test code for caption or sizing border.
2260  */
2261 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
2262 {
2263     LONG hittest = 0;
2264     POINT pt;
2265     MSG msg;
2266     RECT rectWindow;
2267
2268     GetWindowRect( hwnd, &rectWindow );
2269
2270     if ((wParam & 0xfff0) == SC_MOVE)
2271     {
2272         /* Move pointer at the center of the caption */
2273         RECT rect = rectWindow;
2274         /* Note: to be exactly centered we should take the different types
2275          * of border into account, but it shouldn't make more than a few pixels
2276          * of difference so let's not bother with that */
2277         rect.top += GetSystemMetrics(SM_CYBORDER);
2278         if (style & WS_SYSMENU)
2279             rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
2280         if (style & WS_MINIMIZEBOX)
2281             rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
2282         if (style & WS_MAXIMIZEBOX)
2283             rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
2284         pt.x = (rect.right + rect.left) / 2;
2285         pt.y = rect.top + GetSystemMetrics(SM_CYSIZE)/2;
2286         hittest = HTCAPTION;
2287         *capturePoint = pt;
2288     }
2289     else  /* SC_SIZE */
2290     {
2291         SetCursor( LoadCursorW( 0, (LPWSTR)IDC_SIZEALL ) );
2292         pt.x = pt.y = 0;
2293         while(!hittest)
2294         {
2295             if (!GetMessageW( &msg, 0, 0, 0 )) return 0;
2296             if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2297
2298             switch(msg.message)
2299             {
2300             case WM_MOUSEMOVE:
2301                 pt.x = min( max( msg.pt.x, rectWindow.left ), rectWindow.right - 1 );
2302                 pt.y = min( max( msg.pt.y, rectWindow.top ), rectWindow.bottom - 1 );
2303                 hittest = SendMessageW( hwnd, WM_NCHITTEST, 0, MAKELONG( pt.x, pt.y ) );
2304                 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT)) hittest = 0;
2305                 break;
2306
2307             case WM_LBUTTONUP:
2308                 return 0;
2309
2310             case WM_KEYDOWN:
2311                 switch(msg.wParam)
2312                 {
2313                 case VK_UP:
2314                     hittest = HTTOP;
2315                     pt.x =(rectWindow.left+rectWindow.right)/2;
2316                     pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
2317                     break;
2318                 case VK_DOWN:
2319                     hittest = HTBOTTOM;
2320                     pt.x =(rectWindow.left+rectWindow.right)/2;
2321                     pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
2322                     break;
2323                 case VK_LEFT:
2324                     hittest = HTLEFT;
2325                     pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
2326                     pt.y =(rectWindow.top+rectWindow.bottom)/2;
2327                     break;
2328                 case VK_RIGHT:
2329                     hittest = HTRIGHT;
2330                     pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
2331                     pt.y =(rectWindow.top+rectWindow.bottom)/2;
2332                     break;
2333                 case VK_RETURN:
2334                 case VK_ESCAPE:
2335                     return 0;
2336                 }
2337                 break;
2338             default:
2339                 TranslateMessage( &msg );
2340                 DispatchMessageW( &msg );
2341                 break;
2342             }
2343         }
2344         *capturePoint = pt;
2345     }
2346     SetCursorPos( pt.x, pt.y );
2347     SendMessageW( hwnd, WM_SETCURSOR, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
2348     return hittest;
2349 }
2350
2351
2352 /***********************************************************************
2353  *           WINPOS_SysCommandSizeMove
2354  *
2355  * Perform SC_MOVE and SC_SIZE commands.
2356  */
2357 void WINPOS_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
2358 {
2359     MSG msg;
2360     RECT sizingRect, mouseRect, origRect;
2361     HDC hdc;
2362     HWND parent;
2363     LONG hittest = (LONG)(wParam & 0x0f);
2364     WPARAM syscommand = wParam & 0xfff0;
2365     HCURSOR hDragCursor = 0, hOldCursor = 0;
2366     POINT minTrack, maxTrack;
2367     POINT capturePoint, pt;
2368     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
2369     BOOL    thickframe = HAS_THICKFRAME( style );
2370     BOOL    iconic = style & WS_MINIMIZE;
2371     BOOL    moved = FALSE;
2372     DWORD     dwPoint = GetMessagePos ();
2373     BOOL DragFullWindows = TRUE;
2374
2375     if (IsZoomed(hwnd) || !IsWindowVisible(hwnd)) return;
2376
2377     pt.x = (short)LOWORD(dwPoint);
2378     pt.y = (short)HIWORD(dwPoint);
2379     capturePoint = pt;
2380
2381     TRACE("hwnd %p command %04lx, hittest %d, pos %d,%d\n",
2382           hwnd, syscommand, hittest, pt.x, pt.y);
2383
2384     if (syscommand == SC_MOVE)
2385     {
2386         if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
2387         if (!hittest) return;
2388     }
2389     else  /* SC_SIZE */
2390     {
2391         if ( hittest && (syscommand != SC_MOUSEMENU) )
2392             hittest += (HTLEFT - WMSZ_LEFT);
2393         else
2394         {
2395             set_capture_window( hwnd, GUI_INMOVESIZE, NULL );
2396             hittest = start_size_move( hwnd, wParam, &capturePoint, style );
2397             if (!hittest)
2398             {
2399                 set_capture_window( 0, GUI_INMOVESIZE, NULL );
2400                 return;
2401             }
2402         }
2403     }
2404
2405       /* Get min/max info */
2406
2407     WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
2408     GetWindowRect( hwnd, &sizingRect );
2409     if (style & WS_CHILD)
2410     {
2411         parent = GetParent(hwnd);
2412         /* make sizing rect relative to parent */
2413         MapWindowPoints( 0, parent, (POINT*)&sizingRect, 2 );
2414         GetClientRect( parent, &mouseRect );
2415     }
2416     else
2417     {
2418         parent = 0;
2419         GetClientRect( GetDesktopWindow(), &mouseRect );
2420         mouseRect.left = GetSystemMetrics( SM_XVIRTUALSCREEN );
2421         mouseRect.top = GetSystemMetrics( SM_YVIRTUALSCREEN );
2422         mouseRect.right = mouseRect.left + GetSystemMetrics( SM_CXVIRTUALSCREEN );
2423         mouseRect.bottom = mouseRect.top + GetSystemMetrics( SM_CYVIRTUALSCREEN );
2424     }
2425     origRect = sizingRect;
2426
2427     if (ON_LEFT_BORDER(hittest))
2428     {
2429         mouseRect.left  = max( mouseRect.left, sizingRect.right-maxTrack.x );
2430         mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
2431     }
2432     else if (ON_RIGHT_BORDER(hittest))
2433     {
2434         mouseRect.left  = max( mouseRect.left, sizingRect.left+minTrack.x );
2435         mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
2436     }
2437     if (ON_TOP_BORDER(hittest))
2438     {
2439         mouseRect.top    = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
2440         mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
2441     }
2442     else if (ON_BOTTOM_BORDER(hittest))
2443     {
2444         mouseRect.top    = max( mouseRect.top, sizingRect.top+minTrack.y );
2445         mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
2446     }
2447     if (parent) MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
2448
2449     /* Retrieve a default cache DC (without using the window style) */
2450     hdc = GetDCEx( parent, 0, DCX_CACHE );
2451
2452     if( iconic ) /* create a cursor for dragging */
2453     {
2454         hDragCursor = (HCURSOR)GetClassLongPtrW( hwnd, GCLP_HICON);
2455         if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageW( hwnd, WM_QUERYDRAGICON, 0, 0L);
2456         if( !hDragCursor ) iconic = FALSE;
2457     }
2458
2459     /* we only allow disabling the full window drag for child windows */
2460     if (parent) SystemParametersInfoW( SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0 );
2461
2462     /* repaint the window before moving it around */
2463     RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
2464
2465     SendMessageW( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
2466     set_capture_window( hwnd, GUI_INMOVESIZE, NULL );
2467
2468     while(1)
2469     {
2470         int dx = 0, dy = 0;
2471
2472         if (!GetMessageW( &msg, 0, 0, 0 )) break;
2473         if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2474
2475         /* Exit on button-up, Return, or Esc */
2476         if ((msg.message == WM_LBUTTONUP) ||
2477             ((msg.message == WM_KEYDOWN) &&
2478              ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
2479
2480         if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
2481         {
2482             TranslateMessage( &msg );
2483             DispatchMessageW( &msg );
2484             continue;  /* We are not interested in other messages */
2485         }
2486
2487         pt = msg.pt;
2488
2489         if (msg.message == WM_KEYDOWN) switch(msg.wParam)
2490         {
2491         case VK_UP:    pt.y -= 8; break;
2492         case VK_DOWN:  pt.y += 8; break;
2493         case VK_LEFT:  pt.x -= 8; break;
2494         case VK_RIGHT: pt.x += 8; break;
2495         }
2496
2497         pt.x = max( pt.x, mouseRect.left );
2498         pt.x = min( pt.x, mouseRect.right );
2499         pt.y = max( pt.y, mouseRect.top );
2500         pt.y = min( pt.y, mouseRect.bottom );
2501
2502         dx = pt.x - capturePoint.x;
2503         dy = pt.y - capturePoint.y;
2504
2505         if (dx || dy)
2506         {
2507             if( !moved )
2508             {
2509                 moved = TRUE;
2510
2511                 if( iconic ) /* ok, no system popup tracking */
2512                 {
2513                     hOldCursor = SetCursor(hDragCursor);
2514                     ShowCursor( TRUE );
2515                     WINPOS_ShowIconTitle( hwnd, FALSE );
2516                 }
2517                 else if(!DragFullWindows)
2518                     draw_moving_frame( hdc, &sizingRect, thickframe );
2519             }
2520
2521             if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
2522             else
2523             {
2524                 RECT newRect = sizingRect;
2525                 WPARAM wpSizingHit = 0;
2526
2527                 if (hittest == HTCAPTION) OffsetRect( &newRect, dx, dy );
2528                 if (ON_LEFT_BORDER(hittest)) newRect.left += dx;
2529                 else if (ON_RIGHT_BORDER(hittest)) newRect.right += dx;
2530                 if (ON_TOP_BORDER(hittest)) newRect.top += dy;
2531                 else if (ON_BOTTOM_BORDER(hittest)) newRect.bottom += dy;
2532                 if(!iconic && !DragFullWindows) draw_moving_frame( hdc, &sizingRect, thickframe );
2533                 capturePoint = pt;
2534
2535                 /* determine the hit location */
2536                 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
2537                     wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
2538                 SendMessageW( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&newRect );
2539
2540                 if (!iconic)
2541                 {
2542                     if(!DragFullWindows)
2543                         draw_moving_frame( hdc, &newRect, thickframe );
2544                     else
2545                         SetWindowPos( hwnd, 0, newRect.left, newRect.top,
2546                                       newRect.right - newRect.left,
2547                                       newRect.bottom - newRect.top,
2548                                       ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2549                 }
2550                 sizingRect = newRect;
2551             }
2552         }
2553     }
2554
2555     if( iconic )
2556     {
2557         if( moved ) /* restore cursors, show icon title later on */
2558         {
2559             ShowCursor( FALSE );
2560             SetCursor( hOldCursor );
2561         }
2562     }
2563     else if (moved && !DragFullWindows)
2564     {
2565         draw_moving_frame( hdc, &sizingRect, thickframe );
2566     }
2567
2568     set_capture_window( 0, GUI_INMOVESIZE, NULL );
2569     ReleaseDC( parent, hdc );
2570
2571     if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
2572         moved = FALSE;
2573
2574     SendMessageW( hwnd, WM_EXITSIZEMOVE, 0, 0 );
2575     SendMessageW( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
2576
2577     /* window moved or resized */
2578     if (moved)
2579     {
2580         /* if the moving/resizing isn't canceled call SetWindowPos
2581          * with the new position or the new size of the window
2582          */
2583         if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
2584         {
2585             /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
2586             if(!DragFullWindows)
2587                 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
2588                               sizingRect.right - sizingRect.left,
2589                               sizingRect.bottom - sizingRect.top,
2590                               ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2591         }
2592         else
2593         { /* restore previous size/position */
2594             if(DragFullWindows)
2595                 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
2596                               origRect.right - origRect.left,
2597                               origRect.bottom - origRect.top,
2598                               ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2599         }
2600     }
2601
2602     if (IsIconic(hwnd))
2603     {
2604         /* Single click brings up the system menu when iconized */
2605
2606         if( !moved )
2607         {
2608             if(style & WS_SYSMENU )
2609                 SendMessageW( hwnd, WM_SYSCOMMAND,
2610                               SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
2611         }
2612         else WINPOS_ShowIconTitle( hwnd, TRUE );
2613     }
2614 }