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