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