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