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