user32/tests: Fix some window test failures on various Windows platforms.
[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 = wine_server_user_handle( 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 = wine_server_user_handle( 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 = wine_server_user_handle( 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 i, 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 = wine_server_user_handle( hwnd );
273             req->x = pt.x;
274             req->y = pt.y;
275             wine_server_set_reply( req, list, (size-1) * sizeof(user_handle_t) );
276             if (!wine_server_call( req )) count = reply->count;
277         }
278         SERVER_END_REQ;
279         if (count && count < size)
280         {
281             /* start from the end since HWND is potentially larger than user_handle_t */
282             for (i = count - 1; i >= 0; i--)
283                 list[i] = wine_server_ptr_handle( ((user_handle_t *)list)[i] );
284             list[count] = 0;
285             return list;
286         }
287         HeapFree( GetProcessHeap(), 0, list );
288         if (!count) break;
289         size = count + 1;  /* restart with a large enough buffer */
290     }
291     return NULL;
292 }
293
294
295 /***********************************************************************
296  *           WINPOS_WindowFromPoint
297  *
298  * Find the window and hittest for a given point.
299  */
300 HWND WINPOS_WindowFromPoint( HWND hwndScope, POINT pt, INT *hittest )
301 {
302     int i, res;
303     HWND ret, *list;
304
305     if (!hwndScope) hwndScope = GetDesktopWindow();
306
307     *hittest = HTNOWHERE;
308
309     if (!(list = list_children_from_point( hwndScope, pt ))) return 0;
310
311     /* now determine the hittest */
312
313     for (i = 0; list[i]; i++)
314     {
315         LONG style = GetWindowLongW( list[i], GWL_STYLE );
316
317         /* If window is minimized or disabled, return at once */
318         if (style & WS_MINIMIZE)
319         {
320             *hittest = HTCAPTION;
321             break;
322         }
323         if (style & WS_DISABLED)
324         {
325             *hittest = HTERROR;
326             break;
327         }
328         /* Send WM_NCCHITTEST (if same thread) */
329         if (!WIN_IsCurrentThread( list[i] ))
330         {
331             *hittest = HTCLIENT;
332             break;
333         }
334         res = SendMessageW( list[i], WM_NCHITTEST, 0, MAKELONG(pt.x,pt.y) );
335         if (res != HTTRANSPARENT)
336         {
337             *hittest = res;  /* Found the window */
338             break;
339         }
340         /* continue search with next window in z-order */
341     }
342     ret = list[i];
343     HeapFree( GetProcessHeap(), 0, list );
344     TRACE( "scope %p (%d,%d) returning %p\n", hwndScope, pt.x, pt.y, ret );
345     return ret;
346 }
347
348
349 /*******************************************************************
350  *              WindowFromPoint (USER32.@)
351  */
352 HWND WINAPI WindowFromPoint( POINT pt )
353 {
354     INT hittest;
355     return WINPOS_WindowFromPoint( 0, pt, &hittest );
356 }
357
358
359 /*******************************************************************
360  *              ChildWindowFromPoint (USER32.@)
361  */
362 HWND WINAPI ChildWindowFromPoint( HWND hwndParent, POINT pt )
363 {
364     return ChildWindowFromPointEx( hwndParent, pt, CWP_ALL );
365 }
366
367 /*******************************************************************
368  *              RealChildWindowFromPoint (USER32.@)
369  */
370 HWND WINAPI RealChildWindowFromPoint( HWND hwndParent, POINT pt )
371 {
372     return ChildWindowFromPointEx( hwndParent, pt, CWP_SKIPTRANSPARENT );
373 }
374
375 /*******************************************************************
376  *              ChildWindowFromPointEx (USER32.@)
377  */
378 HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
379 {
380     /* pt is in the client coordinates */
381     HWND *list;
382     int i;
383     RECT rect;
384     HWND retvalue;
385
386     GetClientRect( hwndParent, &rect );
387     if (!PtInRect( &rect, pt )) return 0;
388     if (!(list = WIN_ListChildren( hwndParent ))) return hwndParent;
389
390     for (i = 0; list[i]; i++)
391     {
392         if (!WIN_GetRectangles( list[i], &rect, NULL )) continue;
393         if (!PtInRect( &rect, pt )) continue;
394         if (uFlags & (CWP_SKIPINVISIBLE|CWP_SKIPDISABLED))
395         {
396             LONG style = GetWindowLongW( list[i], GWL_STYLE );
397             if ((uFlags & CWP_SKIPINVISIBLE) && !(style & WS_VISIBLE)) continue;
398             if ((uFlags & CWP_SKIPDISABLED) && (style & WS_DISABLED)) continue;
399         }
400         if (uFlags & CWP_SKIPTRANSPARENT)
401         {
402             if (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TRANSPARENT) continue;
403         }
404         break;
405     }
406     retvalue = list[i];
407     HeapFree( GetProcessHeap(), 0, list );
408     if (!retvalue) retvalue = hwndParent;
409     return retvalue;
410 }
411
412
413 /*******************************************************************
414  *         WINPOS_GetWinOffset
415  *
416  * Calculate the offset between the origin of the two windows. Used
417  * to implement MapWindowPoints.
418  */
419 static void WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo, POINT *offset )
420 {
421     WND * wndPtr;
422
423     offset->x = offset->y = 0;
424
425     /* Translate source window origin to screen coords */
426     if (hwndFrom)
427     {
428         HWND hwnd = hwndFrom;
429
430         while (hwnd)
431         {
432             if (hwnd == hwndTo) return;
433             if (!(wndPtr = WIN_GetPtr( hwnd )))
434             {
435                 ERR( "bad hwndFrom = %p\n", hwnd );
436                 return;
437             }
438             if (wndPtr == WND_DESKTOP) break;
439             if (wndPtr == WND_OTHER_PROCESS) goto other_process;
440             if (wndPtr->parent)
441             {
442                 offset->x += wndPtr->rectClient.left;
443                 offset->y += wndPtr->rectClient.top;
444             }
445             hwnd = wndPtr->parent;
446             WIN_ReleasePtr( wndPtr );
447         }
448     }
449
450     /* Translate origin to destination window coords */
451     if (hwndTo)
452     {
453         HWND hwnd = hwndTo;
454
455         while (hwnd)
456         {
457             if (!(wndPtr = WIN_GetPtr( hwnd )))
458             {
459                 ERR( "bad hwndTo = %p\n", hwnd );
460                 return;
461             }
462             if (wndPtr == WND_DESKTOP) break;
463             if (wndPtr == WND_OTHER_PROCESS) goto other_process;
464             if (wndPtr->parent)
465             {
466                 offset->x -= wndPtr->rectClient.left;
467                 offset->y -= wndPtr->rectClient.top;
468             }
469             hwnd = wndPtr->parent;
470             WIN_ReleasePtr( wndPtr );
471         }
472     }
473     return;
474
475  other_process:  /* one of the parents may belong to another process, do it the hard way */
476     offset->x = offset->y = 0;
477     SERVER_START_REQ( get_windows_offset )
478     {
479         req->from = wine_server_user_handle( hwndFrom );
480         req->to   = wine_server_user_handle( hwndTo );
481         if (!wine_server_call( req ))
482         {
483             offset->x = reply->x;
484             offset->y = reply->y;
485         }
486     }
487     SERVER_END_REQ;
488 }
489
490
491 /*******************************************************************
492  *              MapWindowPoints (USER.258)
493  */
494 void WINAPI MapWindowPoints16( HWND16 hwndFrom, HWND16 hwndTo,
495                                LPPOINT16 lppt, UINT16 count )
496 {
497     POINT offset;
498
499     WINPOS_GetWinOffset( WIN_Handle32(hwndFrom), WIN_Handle32(hwndTo), &offset );
500     while (count--)
501     {
502         lppt->x += offset.x;
503         lppt->y += offset.y;
504         lppt++;
505     }
506 }
507
508
509 /*******************************************************************
510  *              MapWindowPoints (USER32.@)
511  */
512 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
513 {
514     POINT offset;
515
516     WINPOS_GetWinOffset( hwndFrom, hwndTo, &offset );
517     while (count--)
518     {
519         lppt->x += offset.x;
520         lppt->y += offset.y;
521         lppt++;
522     }
523     return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
524 }
525
526
527 /***********************************************************************
528  *              IsIconic (USER32.@)
529  */
530 BOOL WINAPI IsIconic(HWND hWnd)
531 {
532     return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
533 }
534
535
536 /***********************************************************************
537  *              IsZoomed (USER32.@)
538  */
539 BOOL WINAPI IsZoomed(HWND hWnd)
540 {
541     return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
542 }
543
544
545 /*******************************************************************
546  *              AllowSetForegroundWindow (USER32.@)
547  */
548 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
549 {
550     /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
551      * implemented, then fix this function. */
552     return TRUE;
553 }
554
555
556 /*******************************************************************
557  *              LockSetForegroundWindow (USER32.@)
558  */
559 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
560 {
561     /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
562      * implemented, then fix this function. */
563     return TRUE;
564 }
565
566
567 /***********************************************************************
568  *              BringWindowToTop (USER32.@)
569  */
570 BOOL WINAPI BringWindowToTop( HWND hwnd )
571 {
572     return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
573 }
574
575
576 /***********************************************************************
577  *              MoveWindow (USER32.@)
578  */
579 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
580                             BOOL repaint )
581 {
582     int flags = SWP_NOZORDER | SWP_NOACTIVATE;
583     if (!repaint) flags |= SWP_NOREDRAW;
584     TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
585     return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
586 }
587
588
589 /***********************************************************************
590  *           WINPOS_RedrawIconTitle
591  */
592 BOOL WINPOS_RedrawIconTitle( HWND hWnd )
593 {
594     HWND icon_title = 0;
595     WND *win = WIN_GetPtr( hWnd );
596
597     if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
598     {
599         icon_title = win->icon_title;
600         WIN_ReleasePtr( win );
601     }
602     if (!icon_title) return FALSE;
603     SendMessageW( icon_title, WM_SHOWWINDOW, TRUE, 0 );
604     InvalidateRect( icon_title, NULL, TRUE );
605     return TRUE;
606 }
607
608 /***********************************************************************
609  *           WINPOS_ShowIconTitle
610  */
611 static BOOL WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
612 {
613     if (!GetPropA( hwnd, "__wine_x11_managed" ))
614     {
615         WND *win = WIN_GetPtr( hwnd );
616         HWND title = 0;
617
618         TRACE("%p %i\n", hwnd, (bShow != 0) );
619
620         if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP) return FALSE;
621         title = win->icon_title;
622         WIN_ReleasePtr( win );
623
624         if( bShow )
625         {
626             if (!title)
627             {
628                 title = ICONTITLE_Create( hwnd );
629                 if (!(win = WIN_GetPtr( hwnd )) || win == WND_OTHER_PROCESS)
630                 {
631                     DestroyWindow( title );
632                     return FALSE;
633                 }
634                 win->icon_title = title;
635                 WIN_ReleasePtr( win );
636             }
637             if (!IsWindowVisible(title))
638             {
639                 SendMessageW( title, WM_SHOWWINDOW, TRUE, 0 );
640                 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
641                               SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
642             }
643         }
644         else if (title) ShowWindow( title, SW_HIDE );
645     }
646     return FALSE;
647 }
648
649 /*******************************************************************
650  *           WINPOS_GetMinMaxInfo
651  *
652  * Get the minimized and maximized information for a window.
653  */
654 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
655                            POINT *minTrack, POINT *maxTrack )
656 {
657     MINMAXINFO MinMax;
658     HMONITOR monitor;
659     INT xinc, yinc;
660     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
661     LONG adjustedStyle;
662     LONG exstyle = GetWindowLongW( hwnd, GWL_EXSTYLE );
663     RECT rc;
664     WND *win;
665
666     /* Compute default values */
667
668     GetWindowRect(hwnd, &rc);
669     MinMax.ptReserved.x = rc.left;
670     MinMax.ptReserved.y = rc.top;
671
672     if ((style & WS_CAPTION) == WS_CAPTION)
673         adjustedStyle = style & ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
674     else
675         adjustedStyle = style;
676
677     GetClientRect(GetAncestor(hwnd,GA_PARENT), &rc);
678     AdjustWindowRectEx(&rc, adjustedStyle, ((style & WS_POPUP) && GetMenu(hwnd)), exstyle);
679
680     xinc = -rc.left;
681     yinc = -rc.top;
682
683     MinMax.ptMaxSize.x = rc.right - rc.left;
684     MinMax.ptMaxSize.y = rc.bottom - rc.top;
685     if (style & (WS_DLGFRAME | WS_BORDER))
686     {
687         MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
688         MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
689     }
690     else
691     {
692         MinMax.ptMinTrackSize.x = 2 * xinc;
693         MinMax.ptMinTrackSize.y = 2 * yinc;
694     }
695     MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXMAXTRACK);
696     MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYMAXTRACK);
697     MinMax.ptMaxPosition.x = -xinc;
698     MinMax.ptMaxPosition.y = -yinc;
699
700     if ((win = WIN_GetPtr( hwnd )) && win != WND_DESKTOP && win != WND_OTHER_PROCESS)
701     {
702         if (!EMPTYPOINT(win->max_pos)) MinMax.ptMaxPosition = win->max_pos;
703         WIN_ReleasePtr( win );
704     }
705
706     SendMessageW( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
707
708     /* if the app didn't change the values, adapt them for the current monitor */
709
710     if ((monitor = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY )))
711     {
712         RECT rc_work;
713         MONITORINFO mon_info;
714
715         mon_info.cbSize = sizeof(mon_info);
716         GetMonitorInfoW( monitor, &mon_info );
717
718         rc_work = mon_info.rcMonitor;
719
720         if (style & WS_MAXIMIZEBOX)
721         {
722             if ((style & WS_CAPTION) == WS_CAPTION || !(style & (WS_CHILD | WS_POPUP)))
723                 rc_work = mon_info.rcWork;
724         }
725
726         if (MinMax.ptMaxSize.x == GetSystemMetrics(SM_CXSCREEN) + 2 * xinc &&
727             MinMax.ptMaxSize.y == GetSystemMetrics(SM_CYSCREEN) + 2 * yinc)
728         {
729             MinMax.ptMaxSize.x = (rc_work.right - rc_work.left) + 2 * xinc;
730             MinMax.ptMaxSize.y = (rc_work.bottom - rc_work.top) + 2 * yinc;
731         }
732         if (MinMax.ptMaxPosition.x == -xinc && MinMax.ptMaxPosition.y == -yinc)
733         {
734             MinMax.ptMaxPosition.x = rc_work.left - xinc;
735             MinMax.ptMaxPosition.y = rc_work.top - yinc;
736         }
737     }
738
739       /* Some sanity checks */
740
741     TRACE("%d %d / %d %d / %d %d / %d %d\n",
742                       MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
743                       MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
744                       MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
745                       MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
746     MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
747                                    MinMax.ptMinTrackSize.x );
748     MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
749                                    MinMax.ptMinTrackSize.y );
750
751     if (maxSize) *maxSize = MinMax.ptMaxSize;
752     if (maxPos) *maxPos = MinMax.ptMaxPosition;
753     if (minTrack) *minTrack = MinMax.ptMinTrackSize;
754     if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
755 }
756
757
758 /***********************************************************************
759  *           WINPOS_FindIconPos
760  *
761  * Find a suitable place for an iconic window.
762  */
763 static POINT WINPOS_FindIconPos( HWND hwnd, POINT pt )
764 {
765     RECT rect, rectParent;
766     HWND parent, child;
767     HRGN hrgn, tmp;
768     int xspacing, yspacing;
769
770     parent = GetAncestor( hwnd, GA_PARENT );
771     GetClientRect( parent, &rectParent );
772     if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
773         (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
774         return pt;  /* The icon already has a suitable position */
775
776     xspacing = GetSystemMetrics(SM_CXICONSPACING);
777     yspacing = GetSystemMetrics(SM_CYICONSPACING);
778
779     /* Check if another icon already occupies this spot */
780     /* FIXME: this is completely inefficient */
781
782     hrgn = CreateRectRgn( 0, 0, 0, 0 );
783     tmp = CreateRectRgn( 0, 0, 0, 0 );
784     for (child = GetWindow( parent, GW_HWNDFIRST ); child; child = GetWindow( child, GW_HWNDNEXT ))
785     {
786         WND *childPtr;
787         if (child == hwnd) continue;
788         if ((GetWindowLongW( child, GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != (WS_VISIBLE|WS_MINIMIZE))
789             continue;
790         if (!(childPtr = WIN_GetPtr( child )) || childPtr == WND_OTHER_PROCESS)
791             continue;
792         SetRectRgn( tmp, childPtr->rectWindow.left, childPtr->rectWindow.top,
793                     childPtr->rectWindow.right, childPtr->rectWindow.bottom );
794         CombineRgn( hrgn, hrgn, tmp, RGN_OR );
795         WIN_ReleasePtr( childPtr );
796     }
797     DeleteObject( tmp );
798
799     for (rect.bottom = rectParent.bottom; rect.bottom >= yspacing; rect.bottom -= yspacing)
800     {
801         for (rect.left = rectParent.left; rect.left <= rectParent.right - xspacing; rect.left += xspacing)
802         {
803             rect.right = rect.left + xspacing;
804             rect.top = rect.bottom - yspacing;
805             if (!RectInRegion( hrgn, &rect ))
806             {
807                 /* No window was found, so it's OK for us */
808                 pt.x = rect.left + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
809                 pt.y = rect.top + (yspacing - GetSystemMetrics(SM_CYICON)) / 2;
810                 DeleteObject( hrgn );
811                 return pt;
812             }
813         }
814     }
815     DeleteObject( hrgn );
816     pt.x = pt.y = 0;
817     return pt;
818 }
819
820
821 /***********************************************************************
822  *           WINPOS_MinMaximize
823  */
824 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
825 {
826     WND *wndPtr;
827     UINT swpFlags = 0;
828     POINT size;
829     LONG old_style;
830     WINDOWPLACEMENT wpl;
831
832     TRACE("%p %u\n", hwnd, cmd );
833
834     wpl.length = sizeof(wpl);
835     GetWindowPlacement( hwnd, &wpl );
836
837     if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
838         return SWP_NOSIZE | SWP_NOMOVE;
839
840     if (IsIconic( hwnd ))
841     {
842         switch (cmd)
843         {
844         case SW_SHOWMINNOACTIVE:
845         case SW_SHOWMINIMIZED:
846         case SW_FORCEMINIMIZE:
847         case SW_MINIMIZE:
848             return SWP_NOSIZE | SWP_NOMOVE;
849         }
850         if (!SendMessageW( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
851         swpFlags |= SWP_NOCOPYBITS;
852     }
853
854     switch( cmd )
855     {
856     case SW_SHOWMINNOACTIVE:
857     case SW_SHOWMINIMIZED:
858     case SW_FORCEMINIMIZE:
859     case SW_MINIMIZE:
860         if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
861         if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
862         else wndPtr->flags &= ~WIN_RESTORE_MAX;
863         WIN_ReleasePtr( wndPtr );
864
865         old_style = WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
866
867         wpl.ptMinPosition = WINPOS_FindIconPos( hwnd, wpl.ptMinPosition );
868
869         if (!(old_style & WS_MINIMIZE)) swpFlags |= SWP_STATECHANGED;
870         SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
871                  wpl.ptMinPosition.x + GetSystemMetrics(SM_CXICON),
872                  wpl.ptMinPosition.y + GetSystemMetrics(SM_CYICON) );
873         swpFlags |= SWP_NOCOPYBITS;
874         break;
875
876     case SW_MAXIMIZE:
877         old_style = GetWindowLongW( hwnd, GWL_STYLE );
878         if ((old_style & WS_MAXIMIZE) && (old_style & WS_VISIBLE)) return SWP_NOSIZE | SWP_NOMOVE;
879
880         WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
881
882         old_style = WIN_SetStyle( hwnd, WS_MAXIMIZE, WS_MINIMIZE );
883         if (old_style & WS_MINIMIZE) WINPOS_ShowIconTitle( hwnd, FALSE );
884
885         if (!(old_style & WS_MAXIMIZE)) swpFlags |= SWP_STATECHANGED;
886         SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y,
887                  wpl.ptMaxPosition.x +  size.x, wpl.ptMaxPosition.y + size.y );
888         break;
889
890     case SW_SHOWNOACTIVATE:
891     case SW_SHOWNORMAL:
892     case SW_RESTORE:
893         old_style = WIN_SetStyle( hwnd, 0, WS_MINIMIZE | WS_MAXIMIZE );
894         if (old_style & WS_MINIMIZE)
895         {
896             BOOL restore_max;
897
898             WINPOS_ShowIconTitle( hwnd, FALSE );
899
900             if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
901             restore_max = (wndPtr->flags & WIN_RESTORE_MAX) != 0;
902             WIN_ReleasePtr( wndPtr );
903             if (restore_max)
904             {
905                 /* Restore to maximized position */
906                 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
907                 WIN_SetStyle( hwnd, WS_MAXIMIZE, 0 );
908                 swpFlags |= SWP_STATECHANGED;
909                 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y,
910                          wpl.ptMaxPosition.x + size.x, wpl.ptMaxPosition.y + size.y );
911                 break;
912             }
913         }
914         else if (!(old_style & WS_MAXIMIZE)) break;
915
916         swpFlags |= SWP_STATECHANGED;
917
918         /* Restore to normal position */
919
920         *rect = wpl.rcNormalPosition;
921         break;
922     }
923
924     return swpFlags;
925 }
926
927
928 /***********************************************************************
929  *              show_window
930  *
931  * Implementation of ShowWindow and ShowWindowAsync.
932  */
933 static BOOL show_window( HWND hwnd, INT cmd )
934 {
935     WND *wndPtr;
936     HWND parent;
937     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
938     BOOL wasVisible = (style & WS_VISIBLE) != 0;
939     BOOL showFlag = TRUE;
940     RECT newPos = {0, 0, 0, 0};
941     UINT swp = 0;
942
943     TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);
944
945     switch(cmd)
946     {
947         case SW_HIDE:
948             if (!wasVisible) return FALSE;
949             showFlag = FALSE;
950             swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE;
951             if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
952             break;
953
954         case SW_SHOWMINNOACTIVE:
955         case SW_MINIMIZE:
956         case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
957             swp |= SWP_NOACTIVATE | SWP_NOZORDER;
958             /* fall through */
959         case SW_SHOWMINIMIZED:
960             swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
961             swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
962             if ((style & WS_MINIMIZE) && wasVisible) return TRUE;
963             break;
964
965         case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
966             if (!wasVisible) swp |= SWP_SHOWWINDOW;
967             swp |= SWP_FRAMECHANGED;
968             swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
969             if ((style & WS_MAXIMIZE) && wasVisible) return TRUE;
970             break;
971
972         case SW_SHOWNA:
973             swp |= SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
974             if (style & WS_CHILD) swp |= SWP_NOZORDER;
975             break;
976         case SW_SHOW:
977             if (wasVisible) return TRUE;
978             swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
979             if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
980             break;
981
982         case SW_SHOWNOACTIVATE:
983             swp |= SWP_NOACTIVATE | SWP_NOZORDER;
984             /* fall through */
985         case SW_RESTORE:
986             /* fall through */
987         case SW_SHOWNORMAL:  /* same as SW_NORMAL: */
988         case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
989             if (!wasVisible) swp |= SWP_SHOWWINDOW;
990             if (style & (WS_MINIMIZE | WS_MAXIMIZE))
991             {
992                 swp |= SWP_FRAMECHANGED;
993                 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
994             }
995             else
996             {
997                 if (wasVisible) return TRUE;
998                 swp |= SWP_NOSIZE | SWP_NOMOVE;
999             }
1000             if (style & WS_CHILD && !(swp & SWP_STATECHANGED)) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1001             break;
1002         default:
1003             return wasVisible;
1004     }
1005
1006     if ((showFlag != wasVisible || cmd == SW_SHOWNA) && cmd != SW_SHOWMAXIMIZED && !(swp & SWP_STATECHANGED))
1007     {
1008         SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
1009         if (!IsWindow( hwnd )) return wasVisible;
1010     }
1011
1012     swp = USER_Driver->pShowWindow( hwnd, cmd, &newPos, swp );
1013
1014     parent = GetAncestor( hwnd, GA_PARENT );
1015     if (parent && !IsWindowVisible( parent ) && !(swp & SWP_STATECHANGED))
1016     {
1017         /* if parent is not visible simply toggle WS_VISIBLE and return */
1018         if (showFlag) WIN_SetStyle( hwnd, WS_VISIBLE, 0 );
1019         else WIN_SetStyle( hwnd, 0, WS_VISIBLE );
1020     }
1021     else
1022         SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1023                       newPos.right - newPos.left, newPos.bottom - newPos.top, swp );
1024
1025     if (cmd == SW_HIDE)
1026     {
1027         HWND hFocus;
1028
1029         WINPOS_ShowIconTitle( hwnd, FALSE );
1030
1031         /* FIXME: This will cause the window to be activated irrespective
1032          * of whether it is owned by the same thread. Has to be done
1033          * asynchronously.
1034          */
1035
1036         if (hwnd == GetActiveWindow())
1037             WINPOS_ActivateOtherWindow(hwnd);
1038
1039         /* Revert focus to parent */
1040         hFocus = GetFocus();
1041         if (hwnd == hFocus || IsChild(hwnd, hFocus))
1042         {
1043             HWND parent = GetAncestor(hwnd, GA_PARENT);
1044             if (parent == GetDesktopWindow()) parent = 0;
1045             SetFocus(parent);
1046         }
1047         return wasVisible;
1048     }
1049
1050     if (IsIconic(hwnd)) WINPOS_ShowIconTitle( hwnd, TRUE );
1051
1052     if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return wasVisible;
1053
1054     if (wndPtr->flags & WIN_NEED_SIZE)
1055     {
1056         /* should happen only in CreateWindowEx() */
1057         int wParam = SIZE_RESTORED;
1058         RECT client = wndPtr->rectClient;
1059
1060         wndPtr->flags &= ~WIN_NEED_SIZE;
1061         if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1062         else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
1063         WIN_ReleasePtr( wndPtr );
1064
1065         SendMessageW( hwnd, WM_SIZE, wParam,
1066                       MAKELONG( client.right - client.left, client.bottom - client.top ));
1067         SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( client.left, client.top ));
1068     }
1069     else WIN_ReleasePtr( wndPtr );
1070
1071     /* if previous state was minimized Windows sets focus to the window */
1072     if (style & WS_MINIMIZE) SetFocus( hwnd );
1073
1074     return wasVisible;
1075 }
1076
1077
1078 /***********************************************************************
1079  *              ShowWindowAsync (USER32.@)
1080  *
1081  * doesn't wait; returns immediately.
1082  * used by threads to toggle windows in other (possibly hanging) threads
1083  */
1084 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
1085 {
1086     HWND full_handle;
1087
1088     if (is_broadcast(hwnd))
1089     {
1090         SetLastError( ERROR_INVALID_PARAMETER );
1091         return FALSE;
1092     }
1093
1094     if ((full_handle = WIN_IsCurrentThread( hwnd )))
1095         return show_window( full_handle, cmd );
1096
1097     return SendNotifyMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
1098 }
1099
1100
1101 /***********************************************************************
1102  *              ShowWindow (USER32.@)
1103  */
1104 BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
1105 {
1106     HWND full_handle;
1107
1108     if (is_broadcast(hwnd))
1109     {
1110         SetLastError( ERROR_INVALID_PARAMETER );
1111         return FALSE;
1112     }
1113     if ((full_handle = WIN_IsCurrentThread( hwnd )))
1114         return show_window( full_handle, cmd );
1115
1116     return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
1117 }
1118
1119
1120 /***********************************************************************
1121  *              GetInternalWindowPos (USER32.@)
1122  */
1123 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
1124                                       LPPOINT ptIcon )
1125 {
1126     WINDOWPLACEMENT wndpl;
1127     if (GetWindowPlacement( hwnd, &wndpl ))
1128     {
1129         if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
1130         if (ptIcon)  *ptIcon = wndpl.ptMinPosition;
1131         return wndpl.showCmd;
1132     }
1133     return 0;
1134 }
1135
1136
1137 /***********************************************************************
1138  *              GetWindowPlacement (USER32.@)
1139  *
1140  * Win95:
1141  * Fails if wndpl->length of Win95 (!) apps is invalid.
1142  */
1143 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
1144 {
1145     WND *pWnd = WIN_GetPtr( hwnd );
1146
1147     if (!pWnd) return FALSE;
1148
1149     if (pWnd == WND_DESKTOP)
1150     {
1151         wndpl->length  = sizeof(*wndpl);
1152         wndpl->showCmd = SW_SHOWNORMAL;
1153         wndpl->flags = 0;
1154         wndpl->ptMinPosition.x = -1;
1155         wndpl->ptMinPosition.y = -1;
1156         wndpl->ptMaxPosition.x = -1;
1157         wndpl->ptMaxPosition.y = -1;
1158         GetWindowRect( hwnd, &wndpl->rcNormalPosition );
1159         return TRUE;
1160     }
1161     if (pWnd == WND_OTHER_PROCESS)
1162     {
1163         if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
1164         return FALSE;
1165     }
1166
1167     /* update the placement according to the current style */
1168     if (pWnd->dwStyle & WS_MINIMIZE)
1169     {
1170         pWnd->min_pos.x = pWnd->rectWindow.left;
1171         pWnd->min_pos.y = pWnd->rectWindow.top;
1172     }
1173     else if (pWnd->dwStyle & WS_MAXIMIZE)
1174     {
1175         pWnd->max_pos.x = pWnd->rectWindow.left;
1176         pWnd->max_pos.y = pWnd->rectWindow.top;
1177     }
1178     else
1179     {
1180         pWnd->normal_rect = pWnd->rectWindow;
1181     }
1182
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     static const DWORD dumped_flags = (SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOREDRAW |
1469                                        SWP_NOACTIVATE | SWP_FRAMECHANGED | SWP_SHOWWINDOW |
1470                                        SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOOWNERZORDER |
1471                                        SWP_NOSENDCHANGING | SWP_DEFERERASE | SWP_ASYNCWINDOWPOS |
1472                                        SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_STATECHANGED);
1473     TRACE("flags:");
1474     if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1475     if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1476     if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1477     if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1478     if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1479     if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1480     if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1481     if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1482     if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1483     if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1484     if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1485     if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1486     if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1487     if(flags & SWP_NOCLIENTSIZE) TRACE(" SWP_NOCLIENTSIZE");
1488     if(flags & SWP_NOCLIENTMOVE) TRACE(" SWP_NOCLIENTMOVE");
1489     if(flags & SWP_STATECHANGED) TRACE(" SWP_STATECHANGED");
1490
1491     if(flags & ~dumped_flags) TRACE(" %08x", flags & ~dumped_flags);
1492     TRACE("\n");
1493 }
1494
1495 /***********************************************************************
1496  *           SWP_DoWinPosChanging
1497  */
1498 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
1499 {
1500     WND *wndPtr;
1501
1502     /* Send WM_WINDOWPOSCHANGING message */
1503
1504     if (!(pWinpos->flags & SWP_NOSENDCHANGING))
1505         SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
1506
1507     if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) ||
1508         wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
1509
1510     /* Calculate new position and size */
1511
1512     *pNewWindowRect = wndPtr->rectWindow;
1513     *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
1514                                                     : wndPtr->rectClient;
1515
1516     if (!(pWinpos->flags & SWP_NOSIZE))
1517     {
1518         pNewWindowRect->right  = pNewWindowRect->left + pWinpos->cx;
1519         pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
1520     }
1521     if (!(pWinpos->flags & SWP_NOMOVE))
1522     {
1523         pNewWindowRect->left    = pWinpos->x;
1524         pNewWindowRect->top     = pWinpos->y;
1525         pNewWindowRect->right  += pWinpos->x - wndPtr->rectWindow.left;
1526         pNewWindowRect->bottom += pWinpos->y - wndPtr->rectWindow.top;
1527
1528         OffsetRect( pNewClientRect, pWinpos->x - wndPtr->rectWindow.left,
1529                                     pWinpos->y - wndPtr->rectWindow.top );
1530     }
1531     pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
1532
1533     TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
1534            pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
1535            pWinpos->cx, pWinpos->cy, pWinpos->flags );
1536     TRACE( "current %s style %08x new %s\n",
1537            wine_dbgstr_rect( &wndPtr->rectWindow ), wndPtr->dwStyle,
1538            wine_dbgstr_rect( pNewWindowRect ));
1539
1540     WIN_ReleasePtr( wndPtr );
1541     return TRUE;
1542 }
1543
1544 /***********************************************************************
1545  *           get_valid_rects
1546  *
1547  * Compute the valid rects from the old and new client rect and WVR_* flags.
1548  * Helper for WM_NCCALCSIZE handling.
1549  */
1550 static inline void get_valid_rects( const RECT *old_client, const RECT *new_client, UINT flags,
1551                                     RECT *valid )
1552 {
1553     int cx, cy;
1554
1555     if (flags & WVR_REDRAW)
1556     {
1557         SetRectEmpty( &valid[0] );
1558         SetRectEmpty( &valid[1] );
1559         return;
1560     }
1561
1562     if (flags & WVR_VALIDRECTS)
1563     {
1564         if (!IntersectRect( &valid[0], &valid[0], new_client ) ||
1565             !IntersectRect( &valid[1], &valid[1], old_client ))
1566         {
1567             SetRectEmpty( &valid[0] );
1568             SetRectEmpty( &valid[1] );
1569             return;
1570         }
1571         flags = WVR_ALIGNLEFT | WVR_ALIGNTOP;
1572     }
1573     else
1574     {
1575         valid[0] = *new_client;
1576         valid[1] = *old_client;
1577     }
1578
1579     /* make sure the rectangles have the same size */
1580     cx = min( valid[0].right - valid[0].left, valid[1].right - valid[1].left );
1581     cy = min( valid[0].bottom - valid[0].top, valid[1].bottom - valid[1].top );
1582
1583     if (flags & WVR_ALIGNBOTTOM)
1584     {
1585         valid[0].top = valid[0].bottom - cy;
1586         valid[1].top = valid[1].bottom - cy;
1587     }
1588     else
1589     {
1590         valid[0].bottom = valid[0].top + cy;
1591         valid[1].bottom = valid[1].top + cy;
1592     }
1593     if (flags & WVR_ALIGNRIGHT)
1594     {
1595         valid[0].left = valid[0].right - cx;
1596         valid[1].left = valid[1].right - cx;
1597     }
1598     else
1599     {
1600         valid[0].right = valid[0].left + cx;
1601         valid[1].right = valid[1].left + cx;
1602     }
1603 }
1604
1605
1606 /***********************************************************************
1607  *           SWP_DoOwnedPopups
1608  *
1609  * fix Z order taking into account owned popups -
1610  * basically we need to maintain them above the window that owns them
1611  *
1612  * FIXME: hide/show owned popups when owner visibility changes.
1613  */
1614 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
1615 {
1616     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1617     HWND owner, *list = NULL;
1618     unsigned int i;
1619
1620     TRACE("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
1621
1622     if ((style & WS_POPUP) && (owner = GetWindow( hwnd, GW_OWNER )))
1623     {
1624         /* make sure this popup stays above the owner */
1625
1626         if (hwndInsertAfter != HWND_TOP && hwndInsertAfter != HWND_TOPMOST)
1627         {
1628             if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return hwndInsertAfter;
1629
1630             for (i = 0; list[i]; i++)
1631             {
1632                 if (list[i] == owner)
1633                 {
1634                     if (i > 0) hwndInsertAfter = list[i-1];
1635                     else hwndInsertAfter = HWND_TOP;
1636                     break;
1637                 }
1638
1639                 if (hwndInsertAfter == HWND_NOTOPMOST)
1640                 {
1641                     if (!(GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST)) break;
1642                 }
1643                 else if (list[i] == hwndInsertAfter) break;
1644             }
1645         }
1646     }
1647     else if (style & WS_CHILD) return hwndInsertAfter;
1648
1649     if (hwndInsertAfter == HWND_BOTTOM) goto done;
1650     if (!list && !(list = WIN_ListChildren( GetDesktopWindow() ))) goto done;
1651
1652     i = 0;
1653     if (hwndInsertAfter == HWND_TOP || hwndInsertAfter == HWND_NOTOPMOST)
1654     {
1655         if (hwndInsertAfter == HWND_NOTOPMOST || !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_TOPMOST))
1656         {
1657             /* skip all the topmost windows */
1658             while (list[i] && (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST)) i++;
1659         }
1660     }
1661     else if (hwndInsertAfter != HWND_TOPMOST)
1662     {
1663         /* skip windows that are already placed correctly */
1664         for (i = 0; list[i]; i++)
1665         {
1666             if (list[i] == hwndInsertAfter) break;
1667             if (list[i] == hwnd) goto done;  /* nothing to do if window is moving backwards in z-order */
1668         }
1669     }
1670
1671     for ( ; list[i]; i++)
1672     {
1673         if (list[i] == hwnd) break;
1674         if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_POPUP)) continue;
1675         if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
1676         TRACE( "moving %p owned by %p after %p\n", list[i], hwnd, hwndInsertAfter );
1677         SetWindowPos( list[i], hwndInsertAfter, 0, 0, 0, 0,
1678                       SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING | SWP_DEFERERASE );
1679         hwndInsertAfter = list[i];
1680     }
1681
1682 done:
1683     HeapFree( GetProcessHeap(), 0, list );
1684     return hwndInsertAfter;
1685 }
1686
1687 /***********************************************************************
1688  *           SWP_DoNCCalcSize
1689  */
1690 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect,
1691                               RECT *validRects )
1692 {
1693     UINT wvrFlags = 0;
1694     WND *wndPtr;
1695
1696     if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
1697
1698       /* Send WM_NCCALCSIZE message to get new client area */
1699     if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
1700     {
1701         NCCALCSIZE_PARAMS params;
1702         WINDOWPOS winposCopy;
1703
1704         params.rgrc[0] = *pNewWindowRect;
1705         params.rgrc[1] = wndPtr->rectWindow;
1706         params.rgrc[2] = wndPtr->rectClient;
1707         params.lppos = &winposCopy;
1708         winposCopy = *pWinpos;
1709         WIN_ReleasePtr( wndPtr );
1710
1711         wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
1712
1713         *pNewClientRect = params.rgrc[0];
1714
1715         if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
1716
1717         TRACE( "hwnd %p old win %s old client %s new win %s new client %s\n", pWinpos->hwnd,
1718                wine_dbgstr_rect(&wndPtr->rectWindow), wine_dbgstr_rect(&wndPtr->rectClient),
1719                wine_dbgstr_rect(pNewWindowRect), wine_dbgstr_rect(pNewClientRect) );
1720
1721         if( pNewClientRect->left != wndPtr->rectClient.left ||
1722             pNewClientRect->top != wndPtr->rectClient.top )
1723             pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1724
1725         if( (pNewClientRect->right - pNewClientRect->left !=
1726              wndPtr->rectClient.right - wndPtr->rectClient.left))
1727             pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1728         else
1729             wvrFlags &= ~WVR_HREDRAW;
1730
1731         if (pNewClientRect->bottom - pNewClientRect->top !=
1732              wndPtr->rectClient.bottom - wndPtr->rectClient.top)
1733             pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1734         else
1735             wvrFlags &= ~WVR_VREDRAW;
1736
1737         validRects[0] = params.rgrc[1];
1738         validRects[1] = params.rgrc[2];
1739     }
1740     else
1741     {
1742         if (!(pWinpos->flags & SWP_NOMOVE) &&
1743             (pNewClientRect->left != wndPtr->rectClient.left ||
1744              pNewClientRect->top != wndPtr->rectClient.top))
1745             pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1746     }
1747
1748     if (pWinpos->flags & (SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
1749     {
1750         SetRectEmpty( &validRects[0] );
1751         SetRectEmpty( &validRects[1] );
1752     }
1753     else get_valid_rects( &wndPtr->rectClient, pNewClientRect, wvrFlags, validRects );
1754
1755     WIN_ReleasePtr( wndPtr );
1756     return wvrFlags;
1757 }
1758
1759 /* fix redundant flags and values in the WINDOWPOS structure */
1760 static BOOL fixup_flags( WINDOWPOS *winpos )
1761 {
1762     HWND parent;
1763     WND *wndPtr = WIN_GetPtr( winpos->hwnd );
1764     BOOL ret = TRUE;
1765
1766     if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
1767     {
1768         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1769         return FALSE;
1770     }
1771     winpos->hwnd = wndPtr->hwndSelf;  /* make it a full handle */
1772
1773     /* Finally make sure that all coordinates are valid */
1774     if (winpos->x < -32768) winpos->x = -32768;
1775     else if (winpos->x > 32767) winpos->x = 32767;
1776     if (winpos->y < -32768) winpos->y = -32768;
1777     else if (winpos->y > 32767) winpos->y = 32767;
1778
1779     if (winpos->cx < 0) winpos->cx = 0;
1780     else if (winpos->cx > 32767) winpos->cx = 32767;
1781     if (winpos->cy < 0) winpos->cy = 0;
1782     else if (winpos->cy > 32767) winpos->cy = 32767;
1783
1784     parent = GetAncestor( winpos->hwnd, GA_PARENT );
1785     if (!IsWindowVisible( parent )) winpos->flags |= SWP_NOREDRAW;
1786
1787     if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
1788     else
1789     {
1790         winpos->flags &= ~SWP_HIDEWINDOW;
1791         if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
1792     }
1793
1794     if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
1795         (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
1796         winpos->flags |= SWP_NOSIZE;    /* Already the right size */
1797
1798     if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
1799         winpos->flags |= SWP_NOMOVE;    /* Already the right position */
1800
1801     if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
1802     {
1803         if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)) && /* Bring to the top when activating */
1804             (winpos->flags & SWP_NOZORDER ||
1805              (winpos->hwndInsertAfter != HWND_TOPMOST && winpos->hwndInsertAfter != HWND_NOTOPMOST)))
1806         {
1807             winpos->flags &= ~SWP_NOZORDER;
1808             winpos->hwndInsertAfter = HWND_TOP;
1809         }
1810     }
1811
1812     /* Check hwndInsertAfter */
1813     if (winpos->flags & SWP_NOZORDER) goto done;
1814
1815     /* fix sign extension */
1816     if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
1817     else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
1818
1819     /* hwndInsertAfter must be a sibling of the window */
1820     if (winpos->hwndInsertAfter == HWND_TOP)
1821     {
1822         if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1823             winpos->flags |= SWP_NOZORDER;
1824     }
1825     else if (winpos->hwndInsertAfter == HWND_BOTTOM)
1826     {
1827         if (!(wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
1828             winpos->flags |= SWP_NOZORDER;
1829     }
1830     else if (winpos->hwndInsertAfter == HWND_TOPMOST)
1831     {
1832         if ((wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1833             winpos->flags |= SWP_NOZORDER;
1834     }
1835     else if (winpos->hwndInsertAfter == HWND_NOTOPMOST)
1836     {
1837         if (!(wndPtr->dwExStyle & WS_EX_TOPMOST))
1838             winpos->flags |= SWP_NOZORDER;
1839     }
1840     else
1841     {
1842         if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != parent) ret = FALSE;
1843         else
1844         {
1845             /* don't need to change the Zorder of hwnd if it's already inserted
1846              * after hwndInsertAfter or when inserting hwnd after itself.
1847              */
1848             if ((winpos->hwnd == winpos->hwndInsertAfter) ||
1849                 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
1850                 winpos->flags |= SWP_NOZORDER;
1851         }
1852     }
1853  done:
1854     WIN_ReleasePtr( wndPtr );
1855     return ret;
1856 }
1857
1858
1859 /***********************************************************************
1860  *              set_window_pos
1861  *
1862  * Backend implementation of SetWindowPos.
1863  */
1864 BOOL set_window_pos( HWND hwnd, HWND insert_after, UINT swp_flags,
1865                      const RECT *window_rect, const RECT *client_rect, const RECT *valid_rects )
1866 {
1867     WND *win;
1868     BOOL ret;
1869     RECT visible_rect, old_window_rect;
1870
1871     visible_rect = *window_rect;
1872     USER_Driver->pWindowPosChanging( hwnd, insert_after, swp_flags,
1873                                      window_rect, client_rect, &visible_rect );
1874
1875     if (!(win = WIN_GetPtr( hwnd ))) return FALSE;
1876     if (win == WND_DESKTOP || win == WND_OTHER_PROCESS) return FALSE;
1877
1878     old_window_rect = win->rectWindow;
1879     SERVER_START_REQ( set_window_pos )
1880     {
1881         req->handle        = wine_server_user_handle( hwnd );
1882         req->previous      = wine_server_user_handle( insert_after );
1883         req->flags         = swp_flags;
1884         req->window.left   = window_rect->left;
1885         req->window.top    = window_rect->top;
1886         req->window.right  = window_rect->right;
1887         req->window.bottom = window_rect->bottom;
1888         req->client.left   = client_rect->left;
1889         req->client.top    = client_rect->top;
1890         req->client.right  = client_rect->right;
1891         req->client.bottom = client_rect->bottom;
1892         if (memcmp( window_rect, &visible_rect, sizeof(RECT) ) || !IsRectEmpty( &valid_rects[0] ))
1893         {
1894             wine_server_add_data( req, &visible_rect, sizeof(visible_rect) );
1895             if (!IsRectEmpty( &valid_rects[0] ))
1896                 wine_server_add_data( req, valid_rects, 2 * sizeof(*valid_rects) );
1897         }
1898         if ((ret = !wine_server_call( req )))
1899         {
1900             win->dwStyle    = reply->new_style;
1901             win->dwExStyle  = reply->new_ex_style;
1902             win->rectWindow = *window_rect;
1903             win->rectClient = *client_rect;
1904         }
1905     }
1906     SERVER_END_REQ;
1907     WIN_ReleasePtr( win );
1908
1909     if (ret)
1910     {
1911         if (((swp_flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) ||
1912             (swp_flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW | SWP_STATECHANGED)))
1913             invalidate_dce( hwnd, &old_window_rect );
1914
1915         USER_Driver->pWindowPosChanged( hwnd, insert_after, swp_flags, window_rect,
1916                                         client_rect, &visible_rect, valid_rects );
1917     }
1918     return ret;
1919 }
1920
1921
1922 /***********************************************************************
1923  *              USER_SetWindowPos
1924  *
1925  *     User32 internal function
1926  */
1927 BOOL USER_SetWindowPos( WINDOWPOS * winpos )
1928 {
1929     RECT newWindowRect, newClientRect, valid_rects[2];
1930     UINT orig_flags;
1931     
1932     orig_flags = winpos->flags;
1933     
1934     /* First make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
1935     if (!(winpos->flags & SWP_NOMOVE))
1936     {
1937         if (winpos->x < -32768) winpos->x = -32768;
1938         else if (winpos->x > 32767) winpos->x = 32767;
1939         if (winpos->y < -32768) winpos->y = -32768;
1940         else if (winpos->y > 32767) winpos->y = 32767;
1941     }
1942     if (!(winpos->flags & SWP_NOSIZE))
1943     {
1944         if (winpos->cx < 0) winpos->cx = 0;
1945         else if (winpos->cx > 32767) winpos->cx = 32767;
1946         if (winpos->cy < 0) winpos->cy = 0;
1947         else if (winpos->cy > 32767) winpos->cy = 32767;
1948     }
1949
1950     if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
1951
1952     /* Fix redundant flags */
1953     if (!fixup_flags( winpos )) return FALSE;
1954
1955     if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
1956     {
1957         if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
1958             winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
1959     }
1960
1961     /* Common operations */
1962
1963     SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect, valid_rects );
1964
1965     if (!set_window_pos( winpos->hwnd, winpos->hwndInsertAfter, winpos->flags,
1966                          &newWindowRect, &newClientRect, valid_rects ))
1967         return FALSE;
1968
1969     /* erase parent when hiding or resizing child */
1970     if (!(orig_flags & SWP_DEFERERASE) &&
1971         ((orig_flags & SWP_HIDEWINDOW) ||
1972          (!(orig_flags & SWP_SHOWWINDOW) &&
1973           (winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOGEOMETRYCHANGE)))
1974     {
1975         HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
1976         if (!parent || parent == GetDesktopWindow()) parent = winpos->hwnd;
1977         erase_now( parent, 0 );
1978     }
1979
1980     if( winpos->flags & SWP_HIDEWINDOW )
1981         HideCaret(winpos->hwnd);
1982     else if (winpos->flags & SWP_SHOWWINDOW)
1983         ShowCaret(winpos->hwnd);
1984
1985     if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)))
1986     {
1987         /* child windows get WM_CHILDACTIVATE message */
1988         if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
1989             SendMessageW( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
1990         else
1991             SetForegroundWindow( winpos->hwnd );
1992     }
1993
1994       /* And last, send the WM_WINDOWPOSCHANGED message */
1995
1996     TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
1997
1998     if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
1999     {
2000         /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
2001            and always contains final window position.
2002          */
2003         winpos->x = newWindowRect.left;
2004         winpos->y = newWindowRect.top;
2005         winpos->cx = newWindowRect.right - newWindowRect.left;
2006         winpos->cy = newWindowRect.bottom - newWindowRect.top;
2007         SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
2008     }
2009     return TRUE;
2010 }
2011
2012 /***********************************************************************
2013  *              SetWindowPos (USER32.@)
2014  */
2015 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
2016                           INT x, INT y, INT cx, INT cy, UINT flags )
2017 {
2018     WINDOWPOS winpos;
2019
2020     TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2021           hwnd, hwndInsertAfter, x, y, cx, cy, flags);
2022     if(TRACE_ON(win)) dump_winpos_flags(flags);
2023
2024     if (is_broadcast(hwnd))
2025     {
2026         SetLastError( ERROR_INVALID_PARAMETER );
2027         return FALSE;
2028     }
2029
2030     winpos.hwnd = WIN_GetFullHandle(hwnd);
2031     winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
2032     winpos.x = x;
2033     winpos.y = y;
2034     winpos.cx = cx;
2035     winpos.cy = cy;
2036     winpos.flags = flags;
2037     
2038     if (WIN_IsCurrentThread( hwnd ))
2039         return USER_SetWindowPos(&winpos);
2040
2041     return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
2042 }
2043
2044
2045 /***********************************************************************
2046  *              BeginDeferWindowPos (USER32.@)
2047  */
2048 HDWP WINAPI BeginDeferWindowPos( INT count )
2049 {
2050     HDWP handle;
2051     DWP *pDWP;
2052
2053     TRACE("%d\n", count);
2054
2055     if (count < 0)
2056     {
2057         SetLastError(ERROR_INVALID_PARAMETER);
2058         return 0;
2059     }
2060     /* Windows allows zero count, in which case it allocates context for 8 moves */
2061     if (count == 0) count = 8;
2062
2063     handle = USER_HEAP_ALLOC( sizeof(DWP) + (count-1)*sizeof(WINDOWPOS) );
2064     if (!handle) return 0;
2065     pDWP = (DWP *) USER_HEAP_LIN_ADDR( handle );
2066     pDWP->actualCount    = 0;
2067     pDWP->suggestedCount = count;
2068     pDWP->valid          = TRUE;
2069     pDWP->wMagic         = DWP_MAGIC;
2070     pDWP->hwndParent     = 0;
2071
2072     TRACE("returning hdwp %p\n", handle);
2073     return handle;
2074 }
2075
2076
2077 /***********************************************************************
2078  *              DeferWindowPos (USER32.@)
2079  */
2080 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
2081                                 INT x, INT y, INT cx, INT cy,
2082                                 UINT flags )
2083 {
2084     DWP *pDWP;
2085     int i;
2086     HDWP newhdwp = hdwp,retvalue;
2087
2088     TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2089           hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
2090
2091     hwnd = WIN_GetFullHandle( hwnd );
2092     if (is_desktop_window( hwnd )) return 0;
2093
2094     if (!(pDWP = USER_HEAP_LIN_ADDR( hdwp ))) return 0;
2095
2096     USER_Lock();
2097
2098     for (i = 0; i < pDWP->actualCount; i++)
2099     {
2100         if (pDWP->winPos[i].hwnd == hwnd)
2101         {
2102               /* Merge with the other changes */
2103             if (!(flags & SWP_NOZORDER))
2104             {
2105                 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
2106             }
2107             if (!(flags & SWP_NOMOVE))
2108             {
2109                 pDWP->winPos[i].x = x;
2110                 pDWP->winPos[i].y = y;
2111             }
2112             if (!(flags & SWP_NOSIZE))
2113             {
2114                 pDWP->winPos[i].cx = cx;
2115                 pDWP->winPos[i].cy = cy;
2116             }
2117             pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
2118                                                SWP_NOZORDER | SWP_NOREDRAW |
2119                                                SWP_NOACTIVATE | SWP_NOCOPYBITS|
2120                                                SWP_NOOWNERZORDER);
2121             pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
2122                                               SWP_FRAMECHANGED);
2123             retvalue = hdwp;
2124             goto END;
2125         }
2126     }
2127     if (pDWP->actualCount >= pDWP->suggestedCount)
2128     {
2129         newhdwp = USER_HEAP_REALLOC( hdwp,
2130                       sizeof(DWP) + pDWP->suggestedCount*sizeof(WINDOWPOS) );
2131         if (!newhdwp)
2132         {
2133             retvalue = 0;
2134             goto END;
2135         }
2136         pDWP = (DWP *) USER_HEAP_LIN_ADDR( newhdwp );
2137         pDWP->suggestedCount++;
2138     }
2139     pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
2140     pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
2141     pDWP->winPos[pDWP->actualCount].x = x;
2142     pDWP->winPos[pDWP->actualCount].y = y;
2143     pDWP->winPos[pDWP->actualCount].cx = cx;
2144     pDWP->winPos[pDWP->actualCount].cy = cy;
2145     pDWP->winPos[pDWP->actualCount].flags = flags;
2146     pDWP->actualCount++;
2147     retvalue = newhdwp;
2148 END:
2149     USER_Unlock();
2150     return retvalue;
2151 }
2152
2153
2154 /***********************************************************************
2155  *              EndDeferWindowPos (USER32.@)
2156  */
2157 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
2158 {
2159     DWP *pDWP;
2160     WINDOWPOS *winpos;
2161     BOOL res = TRUE;
2162     int i;
2163
2164     TRACE("%p\n", hdwp);
2165
2166     pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
2167     if (!pDWP) return FALSE;
2168     for (i = 0, winpos = pDWP->winPos; res && i < pDWP->actualCount; i++, winpos++)
2169     {
2170         TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2171                winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
2172                winpos->cx, winpos->cy, winpos->flags);
2173
2174         if (WIN_IsCurrentThread( winpos->hwnd ))
2175             res = USER_SetWindowPos( winpos );
2176         else
2177             res = SendMessageW( winpos->hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)winpos );
2178     }
2179     USER_HEAP_FREE( hdwp );
2180     return res;
2181 }
2182
2183
2184 /***********************************************************************
2185  *              ArrangeIconicWindows (USER32.@)
2186  */
2187 UINT WINAPI ArrangeIconicWindows( HWND parent )
2188 {
2189     RECT rectParent;
2190     HWND hwndChild;
2191     INT x, y, xspacing, yspacing;
2192
2193     GetClientRect( parent, &rectParent );
2194     x = rectParent.left;
2195     y = rectParent.bottom;
2196     xspacing = GetSystemMetrics(SM_CXICONSPACING);
2197     yspacing = GetSystemMetrics(SM_CYICONSPACING);
2198
2199     hwndChild = GetWindow( parent, GW_CHILD );
2200     while (hwndChild)
2201     {
2202         if( IsIconic( hwndChild ) )
2203         {
2204             WINPOS_ShowIconTitle( hwndChild, FALSE );
2205
2206             SetWindowPos( hwndChild, 0, x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
2207                             y - yspacing - GetSystemMetrics(SM_CYICON)/2, 0, 0,
2208                             SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
2209             if( IsWindow(hwndChild) )
2210                 WINPOS_ShowIconTitle(hwndChild , TRUE );
2211
2212             if (x <= rectParent.right - xspacing) x += xspacing;
2213             else
2214             {
2215                 x = rectParent.left;
2216                 y -= yspacing;
2217             }
2218         }
2219         hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
2220     }
2221     return yspacing;
2222 }
2223
2224
2225 /***********************************************************************
2226  *           draw_moving_frame
2227  *
2228  * Draw the frame used when moving or resizing window.
2229  */
2230 static void draw_moving_frame( HDC hdc, RECT *rect, BOOL thickframe )
2231 {
2232     if (thickframe)
2233     {
2234         const int width = GetSystemMetrics(SM_CXFRAME);
2235         const int height = GetSystemMetrics(SM_CYFRAME);
2236
2237         HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
2238         PatBlt( hdc, rect->left, rect->top,
2239                 rect->right - rect->left - width, height, PATINVERT );
2240         PatBlt( hdc, rect->left, rect->top + height, width,
2241                 rect->bottom - rect->top - height, PATINVERT );
2242         PatBlt( hdc, rect->left + width, rect->bottom - 1,
2243                 rect->right - rect->left - width, -height, PATINVERT );
2244         PatBlt( hdc, rect->right - 1, rect->top, -width,
2245                 rect->bottom - rect->top - height, PATINVERT );
2246         SelectObject( hdc, hbrush );
2247     }
2248     else DrawFocusRect( hdc, rect );
2249 }
2250
2251
2252 /***********************************************************************
2253  *           start_size_move
2254  *
2255  * Initialization of a move or resize, when initiated from a menu choice.
2256  * Return hit test code for caption or sizing border.
2257  */
2258 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
2259 {
2260     LONG hittest = 0;
2261     POINT pt;
2262     MSG msg;
2263     RECT rectWindow;
2264
2265     GetWindowRect( hwnd, &rectWindow );
2266
2267     if ((wParam & 0xfff0) == SC_MOVE)
2268     {
2269         /* Move pointer at the center of the caption */
2270         RECT rect = rectWindow;
2271         /* Note: to be exactly centered we should take the different types
2272          * of border into account, but it shouldn't make more than a few pixels
2273          * of difference so let's not bother with that */
2274         rect.top += GetSystemMetrics(SM_CYBORDER);
2275         if (style & WS_SYSMENU)
2276             rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
2277         if (style & WS_MINIMIZEBOX)
2278             rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
2279         if (style & WS_MAXIMIZEBOX)
2280             rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
2281         pt.x = (rect.right + rect.left) / 2;
2282         pt.y = rect.top + GetSystemMetrics(SM_CYSIZE)/2;
2283         hittest = HTCAPTION;
2284         *capturePoint = pt;
2285     }
2286     else  /* SC_SIZE */
2287     {
2288         SetCursor( LoadCursorW( 0, (LPWSTR)IDC_SIZEALL ) );
2289         pt.x = pt.y = 0;
2290         while(!hittest)
2291         {
2292             if (!GetMessageW( &msg, 0, 0, 0 )) return 0;
2293             if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2294
2295             switch(msg.message)
2296             {
2297             case WM_MOUSEMOVE:
2298                 pt.x = min( max( msg.pt.x, rectWindow.left ), rectWindow.right - 1 );
2299                 pt.y = min( max( msg.pt.y, rectWindow.top ), rectWindow.bottom - 1 );
2300                 hittest = SendMessageW( hwnd, WM_NCHITTEST, 0, MAKELONG( pt.x, pt.y ) );
2301                 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT)) hittest = 0;
2302                 break;
2303
2304             case WM_LBUTTONUP:
2305                 return 0;
2306
2307             case WM_KEYDOWN:
2308                 switch(msg.wParam)
2309                 {
2310                 case VK_UP:
2311                     hittest = HTTOP;
2312                     pt.x =(rectWindow.left+rectWindow.right)/2;
2313                     pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
2314                     break;
2315                 case VK_DOWN:
2316                     hittest = HTBOTTOM;
2317                     pt.x =(rectWindow.left+rectWindow.right)/2;
2318                     pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
2319                     break;
2320                 case VK_LEFT:
2321                     hittest = HTLEFT;
2322                     pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
2323                     pt.y =(rectWindow.top+rectWindow.bottom)/2;
2324                     break;
2325                 case VK_RIGHT:
2326                     hittest = HTRIGHT;
2327                     pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
2328                     pt.y =(rectWindow.top+rectWindow.bottom)/2;
2329                     break;
2330                 case VK_RETURN:
2331                 case VK_ESCAPE:
2332                     return 0;
2333                 }
2334                 break;
2335             default:
2336                 TranslateMessage( &msg );
2337                 DispatchMessageW( &msg );
2338                 break;
2339             }
2340         }
2341         *capturePoint = pt;
2342     }
2343     SetCursorPos( pt.x, pt.y );
2344     SendMessageW( hwnd, WM_SETCURSOR, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
2345     return hittest;
2346 }
2347
2348
2349 /***********************************************************************
2350  *           WINPOS_SysCommandSizeMove
2351  *
2352  * Perform SC_MOVE and SC_SIZE commands.
2353  */
2354 void WINPOS_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
2355 {
2356     MSG msg;
2357     RECT sizingRect, mouseRect, origRect;
2358     HDC hdc;
2359     HWND parent;
2360     LONG hittest = (LONG)(wParam & 0x0f);
2361     WPARAM syscommand = wParam & 0xfff0;
2362     HCURSOR hDragCursor = 0, hOldCursor = 0;
2363     POINT minTrack, maxTrack;
2364     POINT capturePoint, pt;
2365     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
2366     BOOL    thickframe = HAS_THICKFRAME( style );
2367     BOOL    iconic = style & WS_MINIMIZE;
2368     BOOL    moved = FALSE;
2369     DWORD     dwPoint = GetMessagePos ();
2370     BOOL DragFullWindows = TRUE;
2371
2372     if (IsZoomed(hwnd) || !IsWindowVisible(hwnd)) return;
2373
2374     pt.x = (short)LOWORD(dwPoint);
2375     pt.y = (short)HIWORD(dwPoint);
2376     capturePoint = pt;
2377
2378     TRACE("hwnd %p command %04lx, hittest %d, pos %d,%d\n",
2379           hwnd, syscommand, hittest, pt.x, pt.y);
2380
2381     if (syscommand == SC_MOVE)
2382     {
2383         if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
2384         if (!hittest) return;
2385     }
2386     else  /* SC_SIZE */
2387     {
2388         if ( hittest && (syscommand != SC_MOUSEMENU) )
2389             hittest += (HTLEFT - WMSZ_LEFT);
2390         else
2391         {
2392             set_capture_window( hwnd, GUI_INMOVESIZE, NULL );
2393             hittest = start_size_move( hwnd, wParam, &capturePoint, style );
2394             if (!hittest)
2395             {
2396                 set_capture_window( 0, GUI_INMOVESIZE, NULL );
2397                 return;
2398             }
2399         }
2400     }
2401
2402       /* Get min/max info */
2403
2404     WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
2405     GetWindowRect( hwnd, &sizingRect );
2406     if (style & WS_CHILD)
2407     {
2408         parent = GetParent(hwnd);
2409         /* make sizing rect relative to parent */
2410         MapWindowPoints( 0, parent, (POINT*)&sizingRect, 2 );
2411         GetClientRect( parent, &mouseRect );
2412     }
2413     else
2414     {
2415         parent = 0;
2416         GetClientRect( GetDesktopWindow(), &mouseRect );
2417         mouseRect.left = GetSystemMetrics( SM_XVIRTUALSCREEN );
2418         mouseRect.top = GetSystemMetrics( SM_YVIRTUALSCREEN );
2419         mouseRect.right = mouseRect.left + GetSystemMetrics( SM_CXVIRTUALSCREEN );
2420         mouseRect.bottom = mouseRect.top + GetSystemMetrics( SM_CYVIRTUALSCREEN );
2421     }
2422     origRect = sizingRect;
2423
2424     if (ON_LEFT_BORDER(hittest))
2425     {
2426         mouseRect.left  = max( mouseRect.left, sizingRect.right-maxTrack.x );
2427         mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
2428     }
2429     else if (ON_RIGHT_BORDER(hittest))
2430     {
2431         mouseRect.left  = max( mouseRect.left, sizingRect.left+minTrack.x );
2432         mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
2433     }
2434     if (ON_TOP_BORDER(hittest))
2435     {
2436         mouseRect.top    = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
2437         mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
2438     }
2439     else if (ON_BOTTOM_BORDER(hittest))
2440     {
2441         mouseRect.top    = max( mouseRect.top, sizingRect.top+minTrack.y );
2442         mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
2443     }
2444     if (parent) MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
2445
2446     /* Retrieve a default cache DC (without using the window style) */
2447     hdc = GetDCEx( parent, 0, DCX_CACHE );
2448
2449     if( iconic ) /* create a cursor for dragging */
2450     {
2451         hDragCursor = (HCURSOR)GetClassLongPtrW( hwnd, GCLP_HICON);
2452         if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageW( hwnd, WM_QUERYDRAGICON, 0, 0L);
2453         if( !hDragCursor ) iconic = FALSE;
2454     }
2455
2456     /* we only allow disabling the full window drag for child windows */
2457     if (parent) SystemParametersInfoW( SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0 );
2458
2459     /* repaint the window before moving it around */
2460     RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
2461
2462     SendMessageW( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
2463     set_capture_window( hwnd, GUI_INMOVESIZE, NULL );
2464
2465     while(1)
2466     {
2467         int dx = 0, dy = 0;
2468
2469         if (!GetMessageW( &msg, 0, 0, 0 )) break;
2470         if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2471
2472         /* Exit on button-up, Return, or Esc */
2473         if ((msg.message == WM_LBUTTONUP) ||
2474             ((msg.message == WM_KEYDOWN) &&
2475              ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
2476
2477         if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
2478         {
2479             TranslateMessage( &msg );
2480             DispatchMessageW( &msg );
2481             continue;  /* We are not interested in other messages */
2482         }
2483
2484         pt = msg.pt;
2485
2486         if (msg.message == WM_KEYDOWN) switch(msg.wParam)
2487         {
2488         case VK_UP:    pt.y -= 8; break;
2489         case VK_DOWN:  pt.y += 8; break;
2490         case VK_LEFT:  pt.x -= 8; break;
2491         case VK_RIGHT: pt.x += 8; break;
2492         }
2493
2494         pt.x = max( pt.x, mouseRect.left );
2495         pt.x = min( pt.x, mouseRect.right );
2496         pt.y = max( pt.y, mouseRect.top );
2497         pt.y = min( pt.y, mouseRect.bottom );
2498
2499         dx = pt.x - capturePoint.x;
2500         dy = pt.y - capturePoint.y;
2501
2502         if (dx || dy)
2503         {
2504             if( !moved )
2505             {
2506                 moved = TRUE;
2507
2508                 if( iconic ) /* ok, no system popup tracking */
2509                 {
2510                     hOldCursor = SetCursor(hDragCursor);
2511                     ShowCursor( TRUE );
2512                     WINPOS_ShowIconTitle( hwnd, FALSE );
2513                 }
2514                 else if(!DragFullWindows)
2515                     draw_moving_frame( hdc, &sizingRect, thickframe );
2516             }
2517
2518             if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
2519             else
2520             {
2521                 RECT newRect = sizingRect;
2522                 WPARAM wpSizingHit = 0;
2523
2524                 if (hittest == HTCAPTION) OffsetRect( &newRect, dx, dy );
2525                 if (ON_LEFT_BORDER(hittest)) newRect.left += dx;
2526                 else if (ON_RIGHT_BORDER(hittest)) newRect.right += dx;
2527                 if (ON_TOP_BORDER(hittest)) newRect.top += dy;
2528                 else if (ON_BOTTOM_BORDER(hittest)) newRect.bottom += dy;
2529                 if(!iconic && !DragFullWindows) draw_moving_frame( hdc, &sizingRect, thickframe );
2530                 capturePoint = pt;
2531
2532                 /* determine the hit location */
2533                 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
2534                     wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
2535                 SendMessageW( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&newRect );
2536
2537                 if (!iconic)
2538                 {
2539                     if(!DragFullWindows)
2540                         draw_moving_frame( hdc, &newRect, thickframe );
2541                     else
2542                         SetWindowPos( hwnd, 0, newRect.left, newRect.top,
2543                                       newRect.right - newRect.left,
2544                                       newRect.bottom - newRect.top,
2545                                       ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2546                 }
2547                 sizingRect = newRect;
2548             }
2549         }
2550     }
2551
2552     if( iconic )
2553     {
2554         if( moved ) /* restore cursors, show icon title later on */
2555         {
2556             ShowCursor( FALSE );
2557             SetCursor( hOldCursor );
2558         }
2559     }
2560     else if (moved && !DragFullWindows)
2561     {
2562         draw_moving_frame( hdc, &sizingRect, thickframe );
2563     }
2564
2565     set_capture_window( 0, GUI_INMOVESIZE, NULL );
2566     ReleaseDC( parent, hdc );
2567
2568     if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
2569         moved = FALSE;
2570
2571     SendMessageW( hwnd, WM_EXITSIZEMOVE, 0, 0 );
2572     SendMessageW( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
2573
2574     /* window moved or resized */
2575     if (moved)
2576     {
2577         /* if the moving/resizing isn't canceled call SetWindowPos
2578          * with the new position or the new size of the window
2579          */
2580         if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
2581         {
2582             /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
2583             if(!DragFullWindows || iconic)
2584                 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
2585                               sizingRect.right - sizingRect.left,
2586                               sizingRect.bottom - sizingRect.top,
2587                               ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2588         }
2589         else
2590         { /* restore previous size/position */
2591             if(DragFullWindows)
2592                 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
2593                               origRect.right - origRect.left,
2594                               origRect.bottom - origRect.top,
2595                               ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2596         }
2597     }
2598
2599     if (IsIconic(hwnd))
2600     {
2601         /* Single click brings up the system menu when iconized */
2602
2603         if( !moved )
2604         {
2605             if(style & WS_SYSMENU )
2606                 SendMessageW( hwnd, WM_SYSCOMMAND,
2607                               SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
2608         }
2609         else WINPOS_ShowIconTitle( hwnd, TRUE );
2610     }
2611 }