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