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