user32: Default to the dummy surface if the driver doesn't provide something else.
[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 BOOL WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
664 {
665     if (!GetPropA( hwnd, "__wine_x11_managed" ))
666     {
667         WND *win = WIN_GetPtr( hwnd );
668         HWND title = 0;
669
670         TRACE("%p %i\n", hwnd, (bShow != 0) );
671
672         if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP) return FALSE;
673         title = win->icon_title;
674         WIN_ReleasePtr( win );
675
676         if( bShow )
677         {
678             if (!title)
679             {
680                 title = ICONTITLE_Create( hwnd );
681                 if (!(win = WIN_GetPtr( hwnd )) || win == WND_OTHER_PROCESS)
682                 {
683                     DestroyWindow( title );
684                     return FALSE;
685                 }
686                 win->icon_title = title;
687                 WIN_ReleasePtr( win );
688             }
689             if (!IsWindowVisible(title))
690             {
691                 SendMessageW( title, WM_SHOWWINDOW, TRUE, 0 );
692                 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
693                               SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
694             }
695         }
696         else if (title) ShowWindow( title, SW_HIDE );
697     }
698     return FALSE;
699 }
700
701 /*******************************************************************
702  *           WINPOS_GetMinMaxInfo
703  *
704  * Get the minimized and maximized information for a window.
705  */
706 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
707                            POINT *minTrack, POINT *maxTrack )
708 {
709     MINMAXINFO MinMax;
710     HMONITOR monitor;
711     INT xinc, yinc;
712     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
713     LONG adjustedStyle;
714     LONG exstyle = GetWindowLongW( hwnd, GWL_EXSTYLE );
715     RECT rc;
716     WND *win;
717
718     /* Compute default values */
719
720     GetWindowRect(hwnd, &rc);
721     MinMax.ptReserved.x = rc.left;
722     MinMax.ptReserved.y = rc.top;
723
724     if ((style & WS_CAPTION) == WS_CAPTION)
725         adjustedStyle = style & ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
726     else
727         adjustedStyle = style;
728
729     GetClientRect(GetAncestor(hwnd,GA_PARENT), &rc);
730     AdjustWindowRectEx(&rc, adjustedStyle, ((style & WS_POPUP) && GetMenu(hwnd)), exstyle);
731
732     xinc = -rc.left;
733     yinc = -rc.top;
734
735     MinMax.ptMaxSize.x = rc.right - rc.left;
736     MinMax.ptMaxSize.y = rc.bottom - rc.top;
737     if (style & (WS_DLGFRAME | WS_BORDER))
738     {
739         MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
740         MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
741     }
742     else
743     {
744         MinMax.ptMinTrackSize.x = 2 * xinc;
745         MinMax.ptMinTrackSize.y = 2 * yinc;
746     }
747     MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXMAXTRACK);
748     MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYMAXTRACK);
749     MinMax.ptMaxPosition.x = -xinc;
750     MinMax.ptMaxPosition.y = -yinc;
751
752     if ((win = WIN_GetPtr( hwnd )) && win != WND_DESKTOP && win != WND_OTHER_PROCESS)
753     {
754         if (!EMPTYPOINT(win->max_pos)) MinMax.ptMaxPosition = win->max_pos;
755         WIN_ReleasePtr( win );
756     }
757
758     SendMessageW( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
759
760     /* if the app didn't change the values, adapt them for the current monitor */
761
762     if ((monitor = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY )))
763     {
764         RECT rc_work;
765         MONITORINFO mon_info;
766
767         mon_info.cbSize = sizeof(mon_info);
768         GetMonitorInfoW( monitor, &mon_info );
769
770         rc_work = mon_info.rcMonitor;
771
772         if (style & WS_MAXIMIZEBOX)
773         {
774             if ((style & WS_CAPTION) == WS_CAPTION || !(style & (WS_CHILD | WS_POPUP)))
775                 rc_work = mon_info.rcWork;
776         }
777
778         if (MinMax.ptMaxSize.x == GetSystemMetrics(SM_CXSCREEN) + 2 * xinc &&
779             MinMax.ptMaxSize.y == GetSystemMetrics(SM_CYSCREEN) + 2 * yinc)
780         {
781             MinMax.ptMaxSize.x = (rc_work.right - rc_work.left) + 2 * xinc;
782             MinMax.ptMaxSize.y = (rc_work.bottom - rc_work.top) + 2 * yinc;
783         }
784         if (MinMax.ptMaxPosition.x == -xinc && MinMax.ptMaxPosition.y == -yinc)
785         {
786             MinMax.ptMaxPosition.x = rc_work.left - xinc;
787             MinMax.ptMaxPosition.y = rc_work.top - yinc;
788         }
789     }
790
791       /* Some sanity checks */
792
793     TRACE("%d %d / %d %d / %d %d / %d %d\n",
794                       MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
795                       MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
796                       MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
797                       MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
798     MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
799                                    MinMax.ptMinTrackSize.x );
800     MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
801                                    MinMax.ptMinTrackSize.y );
802
803     if (maxSize) *maxSize = MinMax.ptMaxSize;
804     if (maxPos) *maxPos = MinMax.ptMaxPosition;
805     if (minTrack) *minTrack = MinMax.ptMinTrackSize;
806     if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
807 }
808
809
810 /***********************************************************************
811  *           WINPOS_FindIconPos
812  *
813  * Find a suitable place for an iconic window.
814  */
815 static POINT WINPOS_FindIconPos( HWND hwnd, POINT pt )
816 {
817     RECT rect, rectParent;
818     HWND parent, child;
819     HRGN hrgn, tmp;
820     int xspacing, yspacing;
821
822     parent = GetAncestor( hwnd, GA_PARENT );
823     GetClientRect( parent, &rectParent );
824     if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
825         (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
826         return pt;  /* The icon already has a suitable position */
827
828     xspacing = GetSystemMetrics(SM_CXICONSPACING);
829     yspacing = GetSystemMetrics(SM_CYICONSPACING);
830
831     /* Check if another icon already occupies this spot */
832     /* FIXME: this is completely inefficient */
833
834     hrgn = CreateRectRgn( 0, 0, 0, 0 );
835     tmp = CreateRectRgn( 0, 0, 0, 0 );
836     for (child = GetWindow( parent, GW_HWNDFIRST ); child; child = GetWindow( child, GW_HWNDNEXT ))
837     {
838         if (child == hwnd) continue;
839         if ((GetWindowLongW( child, GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != (WS_VISIBLE|WS_MINIMIZE))
840             continue;
841         if (WIN_GetRectangles( child, COORDS_PARENT, &rect, NULL ))
842         {
843             SetRectRgn( tmp, rect.left, rect.top, rect.right, rect.bottom );
844             CombineRgn( hrgn, hrgn, tmp, RGN_OR );
845         }
846     }
847     DeleteObject( tmp );
848
849     for (rect.bottom = rectParent.bottom; rect.bottom >= yspacing; rect.bottom -= yspacing)
850     {
851         for (rect.left = rectParent.left; rect.left <= rectParent.right - xspacing; rect.left += xspacing)
852         {
853             rect.right = rect.left + xspacing;
854             rect.top = rect.bottom - yspacing;
855             if (!RectInRegion( hrgn, &rect ))
856             {
857                 /* No window was found, so it's OK for us */
858                 pt.x = rect.left + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
859                 pt.y = rect.top + (yspacing - GetSystemMetrics(SM_CYICON)) / 2;
860                 DeleteObject( hrgn );
861                 return pt;
862             }
863         }
864     }
865     DeleteObject( hrgn );
866     pt.x = pt.y = 0;
867     return pt;
868 }
869
870
871 /***********************************************************************
872  *           WINPOS_MinMaximize
873  */
874 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
875 {
876     WND *wndPtr;
877     UINT swpFlags = 0;
878     POINT size;
879     LONG old_style;
880     WINDOWPLACEMENT wpl;
881
882     TRACE("%p %u\n", hwnd, cmd );
883
884     wpl.length = sizeof(wpl);
885     GetWindowPlacement( hwnd, &wpl );
886
887     if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
888         return SWP_NOSIZE | SWP_NOMOVE;
889
890     if (IsIconic( hwnd ))
891     {
892         switch (cmd)
893         {
894         case SW_SHOWMINNOACTIVE:
895         case SW_SHOWMINIMIZED:
896         case SW_FORCEMINIMIZE:
897         case SW_MINIMIZE:
898             return SWP_NOSIZE | SWP_NOMOVE;
899         }
900         if (!SendMessageW( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
901         swpFlags |= SWP_NOCOPYBITS;
902     }
903
904     switch( cmd )
905     {
906     case SW_SHOWMINNOACTIVE:
907     case SW_SHOWMINIMIZED:
908     case SW_FORCEMINIMIZE:
909     case SW_MINIMIZE:
910         if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
911         if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
912         else wndPtr->flags &= ~WIN_RESTORE_MAX;
913         WIN_ReleasePtr( wndPtr );
914
915         old_style = WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
916
917         wpl.ptMinPosition = WINPOS_FindIconPos( hwnd, wpl.ptMinPosition );
918
919         if (!(old_style & WS_MINIMIZE)) swpFlags |= SWP_STATECHANGED;
920         SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
921                  wpl.ptMinPosition.x + GetSystemMetrics(SM_CXICON),
922                  wpl.ptMinPosition.y + GetSystemMetrics(SM_CYICON) );
923         swpFlags |= SWP_NOCOPYBITS;
924         break;
925
926     case SW_MAXIMIZE:
927         old_style = GetWindowLongW( hwnd, GWL_STYLE );
928         if ((old_style & WS_MAXIMIZE) && (old_style & WS_VISIBLE)) return SWP_NOSIZE | SWP_NOMOVE;
929
930         WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
931
932         old_style = WIN_SetStyle( hwnd, WS_MAXIMIZE, WS_MINIMIZE );
933         if (old_style & WS_MINIMIZE)
934         {
935             if ((wndPtr = WIN_GetPtr( hwnd )) && wndPtr != WND_OTHER_PROCESS)
936             {
937                 wndPtr->flags |= WIN_RESTORE_MAX;
938                 WIN_ReleasePtr( wndPtr );
939             }
940             WINPOS_ShowIconTitle( hwnd, FALSE );
941         }
942
943         if (!(old_style & WS_MAXIMIZE)) swpFlags |= SWP_STATECHANGED;
944         SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y,
945                  wpl.ptMaxPosition.x +  size.x, wpl.ptMaxPosition.y + size.y );
946         break;
947
948     case SW_SHOWNOACTIVATE:
949         if ((wndPtr = WIN_GetPtr( hwnd )) && wndPtr != WND_OTHER_PROCESS)
950         {
951             wndPtr->flags &= ~WIN_RESTORE_MAX;
952             WIN_ReleasePtr( wndPtr );
953         }
954         /* fall through */
955     case SW_SHOWNORMAL:
956     case SW_RESTORE:
957     case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
958         old_style = WIN_SetStyle( hwnd, 0, WS_MINIMIZE | WS_MAXIMIZE );
959         if (old_style & WS_MINIMIZE)
960         {
961             BOOL restore_max;
962
963             WINPOS_ShowIconTitle( hwnd, FALSE );
964
965             if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
966             restore_max = (wndPtr->flags & WIN_RESTORE_MAX) != 0;
967             WIN_ReleasePtr( wndPtr );
968             if (restore_max)
969             {
970                 /* Restore to maximized position */
971                 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
972                 WIN_SetStyle( hwnd, WS_MAXIMIZE, 0 );
973                 swpFlags |= SWP_STATECHANGED;
974                 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y,
975                          wpl.ptMaxPosition.x + size.x, wpl.ptMaxPosition.y + size.y );
976                 break;
977             }
978         }
979         else if (!(old_style & WS_MAXIMIZE)) break;
980
981         swpFlags |= SWP_STATECHANGED;
982
983         /* Restore to normal position */
984
985         *rect = wpl.rcNormalPosition;
986         break;
987     }
988
989     return swpFlags;
990 }
991
992
993 /***********************************************************************
994  *              show_window
995  *
996  * Implementation of ShowWindow and ShowWindowAsync.
997  */
998 static BOOL show_window( HWND hwnd, INT cmd )
999 {
1000     WND *wndPtr;
1001     HWND parent;
1002     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1003     BOOL wasVisible = (style & WS_VISIBLE) != 0;
1004     BOOL showFlag = TRUE;
1005     RECT newPos = {0, 0, 0, 0};
1006     UINT swp = 0;
1007
1008     TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);
1009
1010     switch(cmd)
1011     {
1012         case SW_HIDE:
1013             if (!wasVisible) return FALSE;
1014             showFlag = FALSE;
1015             swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1016             if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1017             break;
1018
1019         case SW_SHOWMINNOACTIVE:
1020         case SW_MINIMIZE:
1021         case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
1022             swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1023             /* fall through */
1024         case SW_SHOWMINIMIZED:
1025             swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1026             swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
1027             if ((style & WS_MINIMIZE) && wasVisible) return TRUE;
1028             break;
1029
1030         case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
1031             if (!wasVisible) swp |= SWP_SHOWWINDOW;
1032             swp |= SWP_FRAMECHANGED;
1033             swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
1034             if ((style & WS_MAXIMIZE) && wasVisible) return TRUE;
1035             break;
1036
1037         case SW_SHOWNA:
1038             swp |= SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1039             if (style & WS_CHILD) swp |= SWP_NOZORDER;
1040             break;
1041         case SW_SHOW:
1042             if (wasVisible) return TRUE;
1043             swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1044             if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1045             break;
1046
1047         case SW_SHOWNOACTIVATE:
1048             swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1049             /* fall through */
1050         case SW_RESTORE:
1051             /* fall through */
1052         case SW_SHOWNORMAL:  /* same as SW_NORMAL: */
1053         case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
1054             if (!wasVisible) swp |= SWP_SHOWWINDOW;
1055             if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1056             {
1057                 swp |= SWP_FRAMECHANGED;
1058                 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
1059             }
1060             else
1061             {
1062                 if (wasVisible) return TRUE;
1063                 swp |= SWP_NOSIZE | SWP_NOMOVE;
1064             }
1065             if (style & WS_CHILD && !(swp & SWP_STATECHANGED)) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1066             break;
1067         default:
1068             return wasVisible;
1069     }
1070
1071     if ((showFlag != wasVisible || cmd == SW_SHOWNA) && cmd != SW_SHOWMAXIMIZED && !(swp & SWP_STATECHANGED))
1072     {
1073         SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
1074         if (!IsWindow( hwnd )) return wasVisible;
1075     }
1076
1077     swp = USER_Driver->pShowWindow( hwnd, cmd, &newPos, swp );
1078
1079     parent = GetAncestor( hwnd, GA_PARENT );
1080     if (parent && !IsWindowVisible( parent ) && !(swp & SWP_STATECHANGED))
1081     {
1082         /* if parent is not visible simply toggle WS_VISIBLE and return */
1083         if (showFlag) WIN_SetStyle( hwnd, WS_VISIBLE, 0 );
1084         else WIN_SetStyle( hwnd, 0, WS_VISIBLE );
1085     }
1086     else
1087         SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1088                       newPos.right - newPos.left, newPos.bottom - newPos.top, swp );
1089
1090     if (cmd == SW_HIDE)
1091     {
1092         HWND hFocus;
1093
1094         WINPOS_ShowIconTitle( hwnd, FALSE );
1095
1096         /* FIXME: This will cause the window to be activated irrespective
1097          * of whether it is owned by the same thread. Has to be done
1098          * asynchronously.
1099          */
1100
1101         if (hwnd == GetActiveWindow())
1102             WINPOS_ActivateOtherWindow(hwnd);
1103
1104         /* Revert focus to parent */
1105         hFocus = GetFocus();
1106         if (hwnd == hFocus)
1107         {
1108             HWND parent = GetAncestor(hwnd, GA_PARENT);
1109             if (parent == GetDesktopWindow()) parent = 0;
1110             SetFocus(parent);
1111         }
1112         return wasVisible;
1113     }
1114
1115     if (IsIconic(hwnd)) WINPOS_ShowIconTitle( hwnd, TRUE );
1116
1117     if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return wasVisible;
1118
1119     if (wndPtr->flags & WIN_NEED_SIZE)
1120     {
1121         /* should happen only in CreateWindowEx() */
1122         int wParam = SIZE_RESTORED;
1123         RECT client;
1124         LPARAM lparam;
1125
1126         WIN_GetRectangles( hwnd, COORDS_PARENT, NULL, &client );
1127         lparam = MAKELONG( client.right - client.left, client.bottom - client.top );
1128         wndPtr->flags &= ~WIN_NEED_SIZE;
1129         if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1130         else if (wndPtr->dwStyle & WS_MINIMIZE)
1131         {
1132             wParam = SIZE_MINIMIZED;
1133             lparam = 0;
1134         }
1135         WIN_ReleasePtr( wndPtr );
1136
1137         SendMessageW( hwnd, WM_SIZE, wParam, lparam );
1138         SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( client.left, client.top ));
1139     }
1140     else WIN_ReleasePtr( wndPtr );
1141
1142     /* if previous state was minimized Windows sets focus to the window */
1143     if (style & WS_MINIMIZE) SetFocus( hwnd );
1144
1145     return wasVisible;
1146 }
1147
1148
1149 /***********************************************************************
1150  *              ShowWindowAsync (USER32.@)
1151  *
1152  * doesn't wait; returns immediately.
1153  * used by threads to toggle windows in other (possibly hanging) threads
1154  */
1155 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
1156 {
1157     HWND full_handle;
1158
1159     if (is_broadcast(hwnd))
1160     {
1161         SetLastError( ERROR_INVALID_PARAMETER );
1162         return FALSE;
1163     }
1164
1165     if ((full_handle = WIN_IsCurrentThread( hwnd )))
1166         return show_window( full_handle, cmd );
1167
1168     return SendNotifyMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
1169 }
1170
1171
1172 /***********************************************************************
1173  *              ShowWindow (USER32.@)
1174  */
1175 BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
1176 {
1177     HWND full_handle;
1178
1179     if (is_broadcast(hwnd))
1180     {
1181         SetLastError( ERROR_INVALID_PARAMETER );
1182         return FALSE;
1183     }
1184     if ((full_handle = WIN_IsCurrentThread( hwnd )))
1185         return show_window( full_handle, cmd );
1186
1187     return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
1188 }
1189
1190
1191 /***********************************************************************
1192  *              GetInternalWindowPos (USER32.@)
1193  */
1194 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
1195                                       LPPOINT ptIcon )
1196 {
1197     WINDOWPLACEMENT wndpl;
1198     if (GetWindowPlacement( hwnd, &wndpl ))
1199     {
1200         if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
1201         if (ptIcon)  *ptIcon = wndpl.ptMinPosition;
1202         return wndpl.showCmd;
1203     }
1204     return 0;
1205 }
1206
1207
1208 /***********************************************************************
1209  *              GetWindowPlacement (USER32.@)
1210  *
1211  * Win95:
1212  * Fails if wndpl->length of Win95 (!) apps is invalid.
1213  */
1214 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
1215 {
1216     WND *pWnd = WIN_GetPtr( hwnd );
1217
1218     if (!pWnd) return FALSE;
1219
1220     if (pWnd == WND_DESKTOP)
1221     {
1222         wndpl->length  = sizeof(*wndpl);
1223         wndpl->showCmd = SW_SHOWNORMAL;
1224         wndpl->flags = 0;
1225         wndpl->ptMinPosition.x = -1;
1226         wndpl->ptMinPosition.y = -1;
1227         wndpl->ptMaxPosition.x = -1;
1228         wndpl->ptMaxPosition.y = -1;
1229         GetWindowRect( hwnd, &wndpl->rcNormalPosition );
1230         return TRUE;
1231     }
1232     if (pWnd == WND_OTHER_PROCESS)
1233     {
1234         if (!IsWindow( hwnd )) return FALSE;
1235         FIXME( "not supported on other process window %p\n", hwnd );
1236         /* provide some dummy information */
1237         wndpl->length  = sizeof(*wndpl);
1238         wndpl->showCmd = SW_SHOWNORMAL;
1239         wndpl->flags = 0;
1240         wndpl->ptMinPosition.x = -1;
1241         wndpl->ptMinPosition.y = -1;
1242         wndpl->ptMaxPosition.x = -1;
1243         wndpl->ptMaxPosition.y = -1;
1244         GetWindowRect( hwnd, &wndpl->rcNormalPosition );
1245         return TRUE;
1246     }
1247
1248     /* update the placement according to the current style */
1249     if (pWnd->dwStyle & WS_MINIMIZE)
1250     {
1251         pWnd->min_pos.x = pWnd->rectWindow.left;
1252         pWnd->min_pos.y = pWnd->rectWindow.top;
1253     }
1254     else if (pWnd->dwStyle & WS_MAXIMIZE)
1255     {
1256         pWnd->max_pos.x = pWnd->rectWindow.left;
1257         pWnd->max_pos.y = pWnd->rectWindow.top;
1258     }
1259     else
1260     {
1261         pWnd->normal_rect = pWnd->rectWindow;
1262     }
1263
1264     wndpl->length  = sizeof(*wndpl);
1265     if( pWnd->dwStyle & WS_MINIMIZE )
1266         wndpl->showCmd = SW_SHOWMINIMIZED;
1267     else
1268         wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
1269     if( pWnd->flags & WIN_RESTORE_MAX )
1270         wndpl->flags = WPF_RESTORETOMAXIMIZED;
1271     else
1272         wndpl->flags = 0;
1273     wndpl->ptMinPosition    = pWnd->min_pos;
1274     wndpl->ptMaxPosition    = pWnd->max_pos;
1275     wndpl->rcNormalPosition = pWnd->normal_rect;
1276     WIN_ReleasePtr( pWnd );
1277
1278     TRACE( "%p: returning min %d,%d max %d,%d normal %s\n",
1279            hwnd, wndpl->ptMinPosition.x, wndpl->ptMinPosition.y,
1280            wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y,
1281            wine_dbgstr_rect(&wndpl->rcNormalPosition) );
1282     return TRUE;
1283 }
1284
1285 /* make sure the specified rect is visible on screen */
1286 static void make_rect_onscreen( RECT *rect )
1287 {
1288     MONITORINFO info;
1289     HMONITOR monitor = MonitorFromRect( rect, MONITOR_DEFAULTTONEAREST );
1290
1291     info.cbSize = sizeof(info);
1292     if (!monitor || !GetMonitorInfoW( monitor, &info )) return;
1293     /* FIXME: map coordinates from rcWork to rcMonitor */
1294     if (rect->right <= info.rcWork.left)
1295     {
1296         rect->right += info.rcWork.left - rect->left;
1297         rect->left = info.rcWork.left;
1298     }
1299     else if (rect->left >= info.rcWork.right)
1300     {
1301         rect->left += info.rcWork.right - rect->right;
1302         rect->right = info.rcWork.right;
1303     }
1304     if (rect->bottom <= info.rcWork.top)
1305     {
1306         rect->bottom += info.rcWork.top - rect->top;
1307         rect->top = info.rcWork.top;
1308     }
1309     else if (rect->top >= info.rcWork.bottom)
1310     {
1311         rect->top += info.rcWork.bottom - rect->bottom;
1312         rect->bottom = info.rcWork.bottom;
1313     }
1314 }
1315
1316 /* make sure the specified point is visible on screen */
1317 static void make_point_onscreen( POINT *pt )
1318 {
1319     RECT rect;
1320
1321     SetRect( &rect, pt->x, pt->y, pt->x + 1, pt->y + 1 );
1322     make_rect_onscreen( &rect );
1323     pt->x = rect.left;
1324     pt->y = rect.top;
1325 }
1326
1327
1328 /***********************************************************************
1329  *           WINPOS_SetPlacement
1330  */
1331 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
1332 {
1333     DWORD style;
1334     WND *pWnd = WIN_GetPtr( hwnd );
1335     WINDOWPLACEMENT wp = *wndpl;
1336
1337     if (flags & PLACE_MIN) make_point_onscreen( &wp.ptMinPosition );
1338     if (flags & PLACE_MAX) make_point_onscreen( &wp.ptMaxPosition );
1339     if (flags & PLACE_RECT) make_rect_onscreen( &wp.rcNormalPosition );
1340
1341     TRACE( "%p: setting min %d,%d max %d,%d normal %s flags %x ajusted to min %d,%d max %d,%d normal %s\n",
1342            hwnd, wndpl->ptMinPosition.x, wndpl->ptMinPosition.y,
1343            wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y,
1344            wine_dbgstr_rect(&wndpl->rcNormalPosition), flags,
1345            wp.ptMinPosition.x, wp.ptMinPosition.y, wp.ptMaxPosition.x, wp.ptMaxPosition.y,
1346            wine_dbgstr_rect(&wp.rcNormalPosition) );
1347
1348     if (!pWnd || pWnd == WND_OTHER_PROCESS || pWnd == WND_DESKTOP) return FALSE;
1349
1350     if( flags & PLACE_MIN ) pWnd->min_pos = wp.ptMinPosition;
1351     if( flags & PLACE_MAX ) pWnd->max_pos = wp.ptMaxPosition;
1352     if( flags & PLACE_RECT) pWnd->normal_rect = wp.rcNormalPosition;
1353
1354     style = pWnd->dwStyle;
1355
1356     WIN_ReleasePtr( pWnd );
1357
1358     if( style & WS_MINIMIZE )
1359     {
1360         if (flags & PLACE_MIN)
1361         {
1362             WINPOS_ShowIconTitle( hwnd, FALSE );
1363             SetWindowPos( hwnd, 0, wp.ptMinPosition.x, wp.ptMinPosition.y, 0, 0,
1364                           SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1365         }
1366     }
1367     else if( style & WS_MAXIMIZE )
1368     {
1369         if (flags & PLACE_MAX)
1370             SetWindowPos( hwnd, 0, wp.ptMaxPosition.x, wp.ptMaxPosition.y, 0, 0,
1371                           SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1372     }
1373     else if( flags & PLACE_RECT )
1374         SetWindowPos( hwnd, 0, wp.rcNormalPosition.left, wp.rcNormalPosition.top,
1375                       wp.rcNormalPosition.right - wp.rcNormalPosition.left,
1376                       wp.rcNormalPosition.bottom - wp.rcNormalPosition.top,
1377                       SWP_NOZORDER | SWP_NOACTIVATE );
1378
1379     ShowWindow( hwnd, wndpl->showCmd );
1380
1381     if (IsIconic( hwnd ))
1382     {
1383         if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE) WINPOS_ShowIconTitle( hwnd, TRUE );
1384
1385         /* SDK: ...valid only the next time... */
1386         if( wndpl->flags & WPF_RESTORETOMAXIMIZED )
1387         {
1388             pWnd = WIN_GetPtr( hwnd );
1389             if (pWnd && pWnd != WND_OTHER_PROCESS)
1390             {
1391                 pWnd->flags |= WIN_RESTORE_MAX;
1392                 WIN_ReleasePtr( pWnd );
1393             }
1394         }
1395     }
1396     return TRUE;
1397 }
1398
1399
1400 /***********************************************************************
1401  *              SetWindowPlacement (USER32.@)
1402  *
1403  * Win95:
1404  * Fails if wndpl->length of Win95 (!) apps is invalid.
1405  */
1406 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
1407 {
1408     UINT flags = PLACE_MAX | PLACE_RECT;
1409     if (!wpl) return FALSE;
1410     if (wpl->flags & WPF_SETMINPOSITION) flags |= PLACE_MIN;
1411     return WINPOS_SetPlacement( hwnd, wpl, flags );
1412 }
1413
1414
1415 /***********************************************************************
1416  *              AnimateWindow (USER32.@)
1417  *              Shows/Hides a window with an animation
1418  *              NO ANIMATION YET
1419  */
1420 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
1421 {
1422         FIXME("partial stub\n");
1423
1424         /* If trying to show/hide and it's already   *
1425          * shown/hidden or invalid window, fail with *
1426          * invalid parameter                         */
1427         if(!IsWindow(hwnd) ||
1428            (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
1429            (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
1430         {
1431                 SetLastError(ERROR_INVALID_PARAMETER);
1432                 return FALSE;
1433         }
1434
1435         ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1436
1437         return TRUE;
1438 }
1439
1440 /***********************************************************************
1441  *              SetInternalWindowPos (USER32.@)
1442  */
1443 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1444                                     LPRECT rect, LPPOINT pt )
1445 {
1446     WINDOWPLACEMENT wndpl;
1447     UINT flags;
1448
1449     wndpl.length  = sizeof(wndpl);
1450     wndpl.showCmd = showCmd;
1451     wndpl.flags = flags = 0;
1452
1453     if( pt )
1454     {
1455         flags |= PLACE_MIN;
1456         wndpl.flags |= WPF_SETMINPOSITION;
1457         wndpl.ptMinPosition = *pt;
1458     }
1459     if( rect )
1460     {
1461         flags |= PLACE_RECT;
1462         wndpl.rcNormalPosition = *rect;
1463     }
1464     WINPOS_SetPlacement( hwnd, &wndpl, flags );
1465 }
1466
1467
1468 /*******************************************************************
1469  *         can_activate_window
1470  *
1471  * Check if we can activate the specified window.
1472  */
1473 static BOOL can_activate_window( HWND hwnd )
1474 {
1475     LONG style;
1476
1477     if (!hwnd) return FALSE;
1478     style = GetWindowLongW( hwnd, GWL_STYLE );
1479     if (!(style & WS_VISIBLE)) return FALSE;
1480     if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1481     return !(style & WS_DISABLED);
1482 }
1483
1484
1485 /*******************************************************************
1486  *         WINPOS_ActivateOtherWindow
1487  *
1488  *  Activates window other than pWnd.
1489  */
1490 void WINPOS_ActivateOtherWindow(HWND hwnd)
1491 {
1492     HWND hwndTo, fg;
1493
1494     if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1495     {
1496         hwndTo = GetAncestor( hwndTo, GA_ROOT );
1497         if (can_activate_window( hwndTo )) goto done;
1498     }
1499
1500     hwndTo = hwnd;
1501     for (;;)
1502     {
1503         if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1504         if (can_activate_window( hwndTo )) break;
1505     }
1506
1507  done:
1508     fg = GetForegroundWindow();
1509     TRACE("win = %p fg = %p\n", hwndTo, fg);
1510     if (!fg || (hwnd == fg))
1511     {
1512         if (SetForegroundWindow( hwndTo )) return;
1513     }
1514     if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1515 }
1516
1517
1518 /***********************************************************************
1519  *           WINPOS_HandleWindowPosChanging
1520  *
1521  * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1522  */
1523 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1524 {
1525     POINT minTrack, maxTrack;
1526     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1527
1528     if (winpos->flags & SWP_NOSIZE) return 0;
1529     if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1530     {
1531         WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1532         if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1533         if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1534         if (!(style & WS_MINIMIZE))
1535         {
1536             if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1537             if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1538         }
1539     }
1540     return 0;
1541 }
1542
1543
1544 /***********************************************************************
1545  *           dump_winpos_flags
1546  */
1547 static void dump_winpos_flags(UINT flags)
1548 {
1549     static const DWORD dumped_flags = (SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOREDRAW |
1550                                        SWP_NOACTIVATE | SWP_FRAMECHANGED | SWP_SHOWWINDOW |
1551                                        SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOOWNERZORDER |
1552                                        SWP_NOSENDCHANGING | SWP_DEFERERASE | SWP_ASYNCWINDOWPOS |
1553                                        SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_STATECHANGED);
1554     TRACE("flags:");
1555     if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1556     if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1557     if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1558     if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1559     if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1560     if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1561     if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1562     if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1563     if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1564     if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1565     if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1566     if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1567     if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1568     if(flags & SWP_NOCLIENTSIZE) TRACE(" SWP_NOCLIENTSIZE");
1569     if(flags & SWP_NOCLIENTMOVE) TRACE(" SWP_NOCLIENTMOVE");
1570     if(flags & SWP_STATECHANGED) TRACE(" SWP_STATECHANGED");
1571
1572     if(flags & ~dumped_flags) TRACE(" %08x", flags & ~dumped_flags);
1573     TRACE("\n");
1574 }
1575
1576 /***********************************************************************
1577  *           SWP_DoWinPosChanging
1578  */
1579 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
1580 {
1581     WND *wndPtr;
1582     RECT window_rect, client_rect;
1583
1584     /* Send WM_WINDOWPOSCHANGING message */
1585
1586     if (!(pWinpos->flags & SWP_NOSENDCHANGING))
1587         SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
1588
1589     if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) ||
1590         wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
1591
1592     /* Calculate new position and size */
1593
1594     WIN_GetRectangles( pWinpos->hwnd, COORDS_PARENT, &window_rect, &client_rect );
1595     *pNewWindowRect = window_rect;
1596     *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? window_rect : client_rect;
1597
1598     if (!(pWinpos->flags & SWP_NOSIZE))
1599     {
1600         if (wndPtr->dwStyle & WS_MINIMIZE)
1601         {
1602             pNewWindowRect->right  = pNewWindowRect->left + GetSystemMetrics(SM_CXICON);
1603             pNewWindowRect->bottom = pNewWindowRect->top + GetSystemMetrics(SM_CYICON);
1604         }
1605         else
1606         {
1607             pNewWindowRect->right  = pNewWindowRect->left + pWinpos->cx;
1608             pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
1609         }
1610     }
1611     if (!(pWinpos->flags & SWP_NOMOVE))
1612     {
1613         pNewWindowRect->left    = pWinpos->x;
1614         pNewWindowRect->top     = pWinpos->y;
1615         pNewWindowRect->right  += pWinpos->x - window_rect.left;
1616         pNewWindowRect->bottom += pWinpos->y - window_rect.top;
1617
1618         OffsetRect( pNewClientRect, pWinpos->x - window_rect.left,
1619                                     pWinpos->y - window_rect.top );
1620     }
1621     pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
1622
1623     TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
1624            pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
1625            pWinpos->cx, pWinpos->cy, pWinpos->flags );
1626     TRACE( "current %s style %08x new %s\n",
1627            wine_dbgstr_rect( &window_rect ), wndPtr->dwStyle,
1628            wine_dbgstr_rect( pNewWindowRect ));
1629
1630     WIN_ReleasePtr( wndPtr );
1631     return TRUE;
1632 }
1633
1634 /***********************************************************************
1635  *           get_valid_rects
1636  *
1637  * Compute the valid rects from the old and new client rect and WVR_* flags.
1638  * Helper for WM_NCCALCSIZE handling.
1639  */
1640 static inline void get_valid_rects( const RECT *old_client, const RECT *new_client, UINT flags,
1641                                     RECT *valid )
1642 {
1643     int cx, cy;
1644
1645     if (flags & WVR_REDRAW)
1646     {
1647         SetRectEmpty( &valid[0] );
1648         SetRectEmpty( &valid[1] );
1649         return;
1650     }
1651
1652     if (flags & WVR_VALIDRECTS)
1653     {
1654         if (!IntersectRect( &valid[0], &valid[0], new_client ) ||
1655             !IntersectRect( &valid[1], &valid[1], old_client ))
1656         {
1657             SetRectEmpty( &valid[0] );
1658             SetRectEmpty( &valid[1] );
1659             return;
1660         }
1661         flags = WVR_ALIGNLEFT | WVR_ALIGNTOP;
1662     }
1663     else
1664     {
1665         valid[0] = *new_client;
1666         valid[1] = *old_client;
1667     }
1668
1669     /* make sure the rectangles have the same size */
1670     cx = min( valid[0].right - valid[0].left, valid[1].right - valid[1].left );
1671     cy = min( valid[0].bottom - valid[0].top, valid[1].bottom - valid[1].top );
1672
1673     if (flags & WVR_ALIGNBOTTOM)
1674     {
1675         valid[0].top = valid[0].bottom - cy;
1676         valid[1].top = valid[1].bottom - cy;
1677     }
1678     else
1679     {
1680         valid[0].bottom = valid[0].top + cy;
1681         valid[1].bottom = valid[1].top + cy;
1682     }
1683     if (flags & WVR_ALIGNRIGHT)
1684     {
1685         valid[0].left = valid[0].right - cx;
1686         valid[1].left = valid[1].right - cx;
1687     }
1688     else
1689     {
1690         valid[0].right = valid[0].left + cx;
1691         valid[1].right = valid[1].left + cx;
1692     }
1693 }
1694
1695
1696 /***********************************************************************
1697  *           SWP_DoOwnedPopups
1698  *
1699  * fix Z order taking into account owned popups -
1700  * basically we need to maintain them above the window that owns them
1701  *
1702  * FIXME: hide/show owned popups when owner visibility changes.
1703  */
1704 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
1705 {
1706     HWND owner, *list = NULL;
1707     unsigned int i;
1708
1709     TRACE("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
1710
1711     if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) return hwndInsertAfter;
1712
1713     if ((owner = GetWindow( hwnd, GW_OWNER )))
1714     {
1715         /* make sure this popup stays above the owner */
1716
1717         if (hwndInsertAfter != HWND_TOPMOST)
1718         {
1719             if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return hwndInsertAfter;
1720
1721             for (i = 0; list[i]; i++)
1722             {
1723                 BOOL topmost = (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST) != 0;
1724
1725                 if (list[i] == owner)
1726                 {
1727                     if (i > 0) hwndInsertAfter = list[i-1];
1728                     else hwndInsertAfter = topmost ? HWND_TOPMOST : HWND_TOP;
1729                     break;
1730                 }
1731
1732                 if (hwndInsertAfter == HWND_TOP || hwndInsertAfter == HWND_NOTOPMOST)
1733                 {
1734                     if (!topmost) break;
1735                 }
1736                 else if (list[i] == hwndInsertAfter) break;
1737             }
1738         }
1739     }
1740
1741     if (hwndInsertAfter == HWND_BOTTOM) goto done;
1742     if (!list && !(list = WIN_ListChildren( GetDesktopWindow() ))) goto done;
1743
1744     i = 0;
1745     if (hwndInsertAfter == HWND_TOP || hwndInsertAfter == HWND_NOTOPMOST)
1746     {
1747         if (hwndInsertAfter == HWND_NOTOPMOST || !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_TOPMOST))
1748         {
1749             /* skip all the topmost windows */
1750             while (list[i] && (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST)) i++;
1751         }
1752     }
1753     else if (hwndInsertAfter != HWND_TOPMOST)
1754     {
1755         /* skip windows that are already placed correctly */
1756         for (i = 0; list[i]; i++)
1757         {
1758             if (list[i] == hwndInsertAfter) break;
1759             if (list[i] == hwnd) goto done;  /* nothing to do if window is moving backwards in z-order */
1760         }
1761     }
1762
1763     for ( ; list[i]; i++)
1764     {
1765         if (list[i] == hwnd) break;
1766         if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
1767         TRACE( "moving %p owned by %p after %p\n", list[i], hwnd, hwndInsertAfter );
1768         SetWindowPos( list[i], hwndInsertAfter, 0, 0, 0, 0,
1769                       SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING | SWP_DEFERERASE );
1770         hwndInsertAfter = list[i];
1771     }
1772
1773 done:
1774     HeapFree( GetProcessHeap(), 0, list );
1775     return hwndInsertAfter;
1776 }
1777
1778 /***********************************************************************
1779  *           SWP_DoNCCalcSize
1780  */
1781 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect,
1782                               RECT *validRects )
1783 {
1784     UINT wvrFlags = 0;
1785     RECT window_rect, client_rect;
1786
1787     WIN_GetRectangles( pWinpos->hwnd, COORDS_PARENT, &window_rect, &client_rect );
1788
1789       /* Send WM_NCCALCSIZE message to get new client area */
1790     if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
1791     {
1792         NCCALCSIZE_PARAMS params;
1793         WINDOWPOS winposCopy;
1794
1795         params.rgrc[0] = *pNewWindowRect;
1796         params.rgrc[1] = window_rect;
1797         params.rgrc[2] = client_rect;
1798         params.lppos = &winposCopy;
1799         winposCopy = *pWinpos;
1800
1801         wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
1802
1803         *pNewClientRect = params.rgrc[0];
1804
1805         TRACE( "hwnd %p old win %s old client %s new win %s new client %s\n", pWinpos->hwnd,
1806                wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(&client_rect),
1807                wine_dbgstr_rect(pNewWindowRect), wine_dbgstr_rect(pNewClientRect) );
1808
1809         if( pNewClientRect->left != client_rect.left ||
1810             pNewClientRect->top != client_rect.top )
1811             pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1812
1813         if( (pNewClientRect->right - pNewClientRect->left !=
1814              client_rect.right - client_rect.left))
1815             pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1816         else
1817             wvrFlags &= ~WVR_HREDRAW;
1818
1819         if (pNewClientRect->bottom - pNewClientRect->top !=
1820              client_rect.bottom - client_rect.top)
1821             pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1822         else
1823             wvrFlags &= ~WVR_VREDRAW;
1824
1825         validRects[0] = params.rgrc[1];
1826         validRects[1] = params.rgrc[2];
1827     }
1828     else
1829     {
1830         if (!(pWinpos->flags & SWP_NOMOVE) &&
1831             (pNewClientRect->left != client_rect.left ||
1832              pNewClientRect->top != client_rect.top))
1833             pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1834     }
1835
1836     if (pWinpos->flags & (SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
1837     {
1838         SetRectEmpty( &validRects[0] );
1839         SetRectEmpty( &validRects[1] );
1840     }
1841     else get_valid_rects( &client_rect, pNewClientRect, wvrFlags, validRects );
1842
1843     return wvrFlags;
1844 }
1845
1846 /* fix redundant flags and values in the WINDOWPOS structure */
1847 static BOOL fixup_flags( WINDOWPOS *winpos )
1848 {
1849     HWND parent;
1850     RECT window_rect;
1851     POINT pt;
1852     WND *wndPtr = WIN_GetPtr( winpos->hwnd );
1853     BOOL ret = TRUE;
1854
1855     if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
1856     {
1857         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1858         return FALSE;
1859     }
1860     winpos->hwnd = wndPtr->obj.handle;  /* make it a full handle */
1861
1862     /* Finally make sure that all coordinates are valid */
1863     if (winpos->x < -32768) winpos->x = -32768;
1864     else if (winpos->x > 32767) winpos->x = 32767;
1865     if (winpos->y < -32768) winpos->y = -32768;
1866     else if (winpos->y > 32767) winpos->y = 32767;
1867
1868     if (winpos->cx < 0) winpos->cx = 0;
1869     else if (winpos->cx > 32767) winpos->cx = 32767;
1870     if (winpos->cy < 0) winpos->cy = 0;
1871     else if (winpos->cy > 32767) winpos->cy = 32767;
1872
1873     parent = GetAncestor( winpos->hwnd, GA_PARENT );
1874     if (!IsWindowVisible( parent )) winpos->flags |= SWP_NOREDRAW;
1875
1876     if (winpos->flags & SWP_HIDEWINDOW) wndPtr->flags |= WIN_HIDDEN;
1877     else if (winpos->flags & SWP_SHOWWINDOW) wndPtr->flags &= ~WIN_HIDDEN;
1878
1879     if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
1880     else
1881     {
1882         winpos->flags &= ~SWP_HIDEWINDOW;
1883         if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
1884     }
1885
1886     WIN_GetRectangles( winpos->hwnd, COORDS_SCREEN, &window_rect, NULL );
1887     if ((window_rect.right - window_rect.left == winpos->cx) &&
1888         (window_rect.bottom - window_rect.top == winpos->cy))
1889         winpos->flags |= SWP_NOSIZE;    /* Already the right size */
1890
1891     pt.x = winpos->x;
1892     pt.y = winpos->y;
1893     ClientToScreen( parent, &pt );
1894     if ((window_rect.left == pt.x) && (window_rect.top == pt.y))
1895         winpos->flags |= SWP_NOMOVE;    /* Already the right position */
1896
1897     if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
1898     {
1899         if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)) && /* Bring to the top when activating */
1900             (winpos->flags & SWP_NOZORDER ||
1901              (winpos->hwndInsertAfter != HWND_TOPMOST && winpos->hwndInsertAfter != HWND_NOTOPMOST)))
1902         {
1903             winpos->flags &= ~SWP_NOZORDER;
1904             winpos->hwndInsertAfter = HWND_TOP;
1905         }
1906     }
1907
1908     /* Check hwndInsertAfter */
1909     if (winpos->flags & SWP_NOZORDER) goto done;
1910
1911     if (winpos->hwndInsertAfter == HWND_TOP)
1912     {
1913         if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1914             winpos->flags |= SWP_NOZORDER;
1915     }
1916     else if (winpos->hwndInsertAfter == HWND_BOTTOM)
1917     {
1918         if (!(wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
1919             winpos->flags |= SWP_NOZORDER;
1920     }
1921     else if (winpos->hwndInsertAfter == HWND_TOPMOST)
1922     {
1923         if ((wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1924             winpos->flags |= SWP_NOZORDER;
1925     }
1926     else if (winpos->hwndInsertAfter == HWND_NOTOPMOST)
1927     {
1928         if (!(wndPtr->dwExStyle & WS_EX_TOPMOST))
1929             winpos->flags |= SWP_NOZORDER;
1930     }
1931     else
1932     {
1933         if ((winpos->hwnd == winpos->hwndInsertAfter) ||
1934             (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
1935             winpos->flags |= SWP_NOZORDER;
1936     }
1937  done:
1938     WIN_ReleasePtr( wndPtr );
1939     return ret;
1940 }
1941
1942
1943 /***********************************************************************
1944  *              set_window_pos
1945  *
1946  * Backend implementation of SetWindowPos.
1947  */
1948 BOOL set_window_pos( HWND hwnd, HWND insert_after, UINT swp_flags,
1949                      const RECT *window_rect, const RECT *client_rect, const RECT *valid_rects )
1950 {
1951     WND *win;
1952     HWND parent = GetAncestor( hwnd, GA_PARENT );
1953     BOOL ret;
1954     int old_width;
1955     RECT visible_rect, old_visible_rect, old_window_rect;
1956     struct window_surface *old_surface, *new_surface = NULL;
1957
1958     if (!parent || parent == GetDesktopWindow())
1959     {
1960         new_surface = &dummy_surface;  /* provide a default surface for top-level windows */
1961         window_surface_add_ref( new_surface );
1962     }
1963     visible_rect = *window_rect;
1964     USER_Driver->pWindowPosChanging( hwnd, insert_after, swp_flags,
1965                                      window_rect, client_rect, &visible_rect, &new_surface );
1966
1967     WIN_GetRectangles( hwnd, COORDS_SCREEN, &old_window_rect, NULL );
1968
1969     if (!(win = WIN_GetPtr( hwnd )) || win == WND_DESKTOP || win == WND_OTHER_PROCESS)
1970     {
1971         if (new_surface) window_surface_release( new_surface );
1972         return FALSE;
1973     }
1974     old_width = win->rectClient.right - win->rectClient.left;
1975     old_visible_rect = win->visible_rect;
1976
1977     SERVER_START_REQ( set_window_pos )
1978     {
1979         req->handle        = wine_server_user_handle( hwnd );
1980         req->previous      = wine_server_user_handle( insert_after );
1981         req->flags         = swp_flags;
1982         req->window.left   = window_rect->left;
1983         req->window.top    = window_rect->top;
1984         req->window.right  = window_rect->right;
1985         req->window.bottom = window_rect->bottom;
1986         req->client.left   = client_rect->left;
1987         req->client.top    = client_rect->top;
1988         req->client.right  = client_rect->right;
1989         req->client.bottom = client_rect->bottom;
1990         if (memcmp( window_rect, &visible_rect, sizeof(RECT) ) || !IsRectEmpty( &valid_rects[0] ))
1991         {
1992             wine_server_add_data( req, &visible_rect, sizeof(visible_rect) );
1993             if (!IsRectEmpty( &valid_rects[0] ))
1994                 wine_server_add_data( req, valid_rects, 2 * sizeof(*valid_rects) );
1995         }
1996         if ((ret = !wine_server_call( req )))
1997         {
1998             win->dwStyle    = reply->new_style;
1999             win->dwExStyle  = reply->new_ex_style;
2000             win->rectWindow = *window_rect;
2001             win->rectClient = *client_rect;
2002             win->visible_rect = visible_rect;
2003             old_surface       = win->surface;
2004             win->surface      = new_surface;
2005             if (GetWindowLongW( win->parent, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
2006             {
2007                 RECT client;
2008                 GetClientRect( win->parent, &client );
2009                 mirror_rect( &client, &win->rectWindow );
2010                 mirror_rect( &client, &win->rectClient );
2011                 mirror_rect( &client, &win->visible_rect );
2012             }
2013             /* if an RTL window is resized the children have moved */
2014             if (win->dwExStyle & WS_EX_LAYOUTRTL && client_rect->right - client_rect->left != old_width)
2015                 win->flags |= WIN_CHILDREN_MOVED;
2016         }
2017     }
2018     SERVER_END_REQ;
2019
2020     if (ret && (((swp_flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) ||
2021                 (swp_flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW | SWP_STATECHANGED | SWP_FRAMECHANGED))))
2022         invalidate_dce( win, &old_window_rect );
2023
2024     WIN_ReleasePtr( win );
2025
2026     if (ret)
2027     {
2028         TRACE( "win %p surface %p -> %p\n", hwnd, old_surface, new_surface );
2029         register_window_surface( old_surface, new_surface );
2030         if (old_surface)
2031         {
2032             if (!IsRectEmpty( valid_rects ))
2033             {
2034                 move_window_bits( hwnd, old_surface, new_surface, &visible_rect,
2035                                   &old_visible_rect, client_rect, valid_rects );
2036                 valid_rects = NULL;  /* prevent the driver from trying to also move the bits */
2037             }
2038             window_surface_release( old_surface );
2039         }
2040         USER_Driver->pWindowPosChanged( hwnd, insert_after, swp_flags, window_rect,
2041                                         client_rect, &visible_rect, valid_rects, new_surface );
2042     }
2043     else if (new_surface) window_surface_release( new_surface );
2044
2045     return ret;
2046 }
2047
2048
2049 /***********************************************************************
2050  *              USER_SetWindowPos
2051  *
2052  *     User32 internal function
2053  */
2054 BOOL USER_SetWindowPos( WINDOWPOS * winpos )
2055 {
2056     RECT newWindowRect, newClientRect, valid_rects[2];
2057     UINT orig_flags;
2058     
2059     orig_flags = winpos->flags;
2060
2061     /* First, check z-order arguments.  */
2062     if (!(winpos->flags & SWP_NOZORDER))
2063     {
2064         /* fix sign extension */
2065         if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
2066         else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
2067
2068         if (!(winpos->hwndInsertAfter == HWND_TOP ||
2069               winpos->hwndInsertAfter == HWND_BOTTOM ||
2070               winpos->hwndInsertAfter == HWND_TOPMOST ||
2071               winpos->hwndInsertAfter == HWND_NOTOPMOST))
2072         {
2073             HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
2074             HWND insertafter_parent = GetAncestor( winpos->hwndInsertAfter, GA_PARENT );
2075
2076             /* hwndInsertAfter must be a sibling of the window */
2077             if (!insertafter_parent) return FALSE;
2078             if (insertafter_parent != parent) return TRUE;
2079         }
2080     }
2081
2082     /* Make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
2083     if (!(winpos->flags & SWP_NOMOVE))
2084     {
2085         if (winpos->x < -32768) winpos->x = -32768;
2086         else if (winpos->x > 32767) winpos->x = 32767;
2087         if (winpos->y < -32768) winpos->y = -32768;
2088         else if (winpos->y > 32767) winpos->y = 32767;
2089     }
2090     if (!(winpos->flags & SWP_NOSIZE))
2091     {
2092         if (winpos->cx < 0) winpos->cx = 0;
2093         else if (winpos->cx > 32767) winpos->cx = 32767;
2094         if (winpos->cy < 0) winpos->cy = 0;
2095         else if (winpos->cy > 32767) winpos->cy = 32767;
2096     }
2097
2098     if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
2099
2100     /* Fix redundant flags */
2101     if (!fixup_flags( winpos )) return FALSE;
2102
2103     if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
2104     {
2105         if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
2106             winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
2107     }
2108
2109     /* Common operations */
2110
2111     SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect, valid_rects );
2112
2113     if (!set_window_pos( winpos->hwnd, winpos->hwndInsertAfter, winpos->flags,
2114                          &newWindowRect, &newClientRect, valid_rects ))
2115         return FALSE;
2116
2117     /* erase parent when hiding or resizing child */
2118     if (!(orig_flags & SWP_DEFERERASE) &&
2119         ((orig_flags & SWP_HIDEWINDOW) ||
2120          (!(orig_flags & SWP_SHOWWINDOW) &&
2121           (winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOGEOMETRYCHANGE)))
2122     {
2123         HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
2124         if (!parent || parent == GetDesktopWindow()) parent = winpos->hwnd;
2125         erase_now( parent, 0 );
2126     }
2127
2128     if( winpos->flags & SWP_HIDEWINDOW )
2129         HideCaret(winpos->hwnd);
2130     else if (winpos->flags & SWP_SHOWWINDOW)
2131         ShowCaret(winpos->hwnd);
2132
2133     if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)))
2134     {
2135         /* child windows get WM_CHILDACTIVATE message */
2136         if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
2137             SendMessageW( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
2138         else
2139             SetForegroundWindow( winpos->hwnd );
2140     }
2141
2142       /* And last, send the WM_WINDOWPOSCHANGED message */
2143
2144     TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
2145
2146     if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
2147     {
2148         /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
2149            and always contains final window position.
2150          */
2151         winpos->x = newWindowRect.left;
2152         winpos->y = newWindowRect.top;
2153         winpos->cx = newWindowRect.right - newWindowRect.left;
2154         winpos->cy = newWindowRect.bottom - newWindowRect.top;
2155         SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
2156     }
2157     return TRUE;
2158 }
2159
2160 /***********************************************************************
2161  *              SetWindowPos (USER32.@)
2162  */
2163 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
2164                           INT x, INT y, INT cx, INT cy, UINT flags )
2165 {
2166     WINDOWPOS winpos;
2167
2168     TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2169           hwnd, hwndInsertAfter, x, y, cx, cy, flags);
2170     if(TRACE_ON(win)) dump_winpos_flags(flags);
2171
2172     if (is_broadcast(hwnd))
2173     {
2174         SetLastError( ERROR_INVALID_PARAMETER );
2175         return FALSE;
2176     }
2177
2178     winpos.hwnd = WIN_GetFullHandle(hwnd);
2179     winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
2180     winpos.x = x;
2181     winpos.y = y;
2182     winpos.cx = cx;
2183     winpos.cy = cy;
2184     winpos.flags = flags;
2185     
2186     if (WIN_IsCurrentThread( hwnd ))
2187         return USER_SetWindowPos(&winpos);
2188
2189     return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
2190 }
2191
2192
2193 /***********************************************************************
2194  *              BeginDeferWindowPos (USER32.@)
2195  */
2196 HDWP WINAPI BeginDeferWindowPos( INT count )
2197 {
2198     HDWP handle = 0;
2199     DWP *pDWP;
2200
2201     TRACE("%d\n", count);
2202
2203     if (count < 0)
2204     {
2205         SetLastError(ERROR_INVALID_PARAMETER);
2206         return 0;
2207     }
2208     /* Windows allows zero count, in which case it allocates context for 8 moves */
2209     if (count == 0) count = 8;
2210
2211     if (!(pDWP = HeapAlloc( GetProcessHeap(), 0, sizeof(DWP)))) return 0;
2212
2213     pDWP->actualCount    = 0;
2214     pDWP->suggestedCount = count;
2215     pDWP->hwndParent     = 0;
2216
2217     if (!(pDWP->winPos = HeapAlloc( GetProcessHeap(), 0, count * sizeof(WINDOWPOS) )) ||
2218         !(handle = alloc_user_handle( &pDWP->obj, USER_DWP )))
2219     {
2220         HeapFree( GetProcessHeap(), 0, pDWP->winPos );
2221         HeapFree( GetProcessHeap(), 0, pDWP );
2222     }
2223
2224     TRACE("returning hdwp %p\n", handle);
2225     return handle;
2226 }
2227
2228
2229 /***********************************************************************
2230  *              DeferWindowPos (USER32.@)
2231  */
2232 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
2233                                 INT x, INT y, INT cx, INT cy,
2234                                 UINT flags )
2235 {
2236     DWP *pDWP;
2237     int i;
2238     HDWP retvalue = hdwp;
2239
2240     TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2241           hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
2242
2243     hwnd = WIN_GetFullHandle( hwnd );
2244     if (is_desktop_window( hwnd )) return 0;
2245
2246     if (!(pDWP = get_user_handle_ptr( hdwp, USER_DWP ))) return 0;
2247     if (pDWP == OBJ_OTHER_PROCESS)
2248     {
2249         FIXME( "other process handle %p?\n", hdwp );
2250         return 0;
2251     }
2252
2253     for (i = 0; i < pDWP->actualCount; i++)
2254     {
2255         if (pDWP->winPos[i].hwnd == hwnd)
2256         {
2257               /* Merge with the other changes */
2258             if (!(flags & SWP_NOZORDER))
2259             {
2260                 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
2261             }
2262             if (!(flags & SWP_NOMOVE))
2263             {
2264                 pDWP->winPos[i].x = x;
2265                 pDWP->winPos[i].y = y;
2266             }
2267             if (!(flags & SWP_NOSIZE))
2268             {
2269                 pDWP->winPos[i].cx = cx;
2270                 pDWP->winPos[i].cy = cy;
2271             }
2272             pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
2273                                                SWP_NOZORDER | SWP_NOREDRAW |
2274                                                SWP_NOACTIVATE | SWP_NOCOPYBITS|
2275                                                SWP_NOOWNERZORDER);
2276             pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
2277                                               SWP_FRAMECHANGED);
2278             goto END;
2279         }
2280     }
2281     if (pDWP->actualCount >= pDWP->suggestedCount)
2282     {
2283         WINDOWPOS *newpos = HeapReAlloc( GetProcessHeap(), 0, pDWP->winPos,
2284                                          pDWP->suggestedCount * 2 * sizeof(WINDOWPOS) );
2285         if (!newpos)
2286         {
2287             retvalue = 0;
2288             goto END;
2289         }
2290         pDWP->suggestedCount *= 2;
2291         pDWP->winPos = newpos;
2292     }
2293     pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
2294     pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
2295     pDWP->winPos[pDWP->actualCount].x = x;
2296     pDWP->winPos[pDWP->actualCount].y = y;
2297     pDWP->winPos[pDWP->actualCount].cx = cx;
2298     pDWP->winPos[pDWP->actualCount].cy = cy;
2299     pDWP->winPos[pDWP->actualCount].flags = flags;
2300     pDWP->actualCount++;
2301 END:
2302     release_user_handle_ptr( pDWP );
2303     return retvalue;
2304 }
2305
2306
2307 /***********************************************************************
2308  *              EndDeferWindowPos (USER32.@)
2309  */
2310 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
2311 {
2312     DWP *pDWP;
2313     WINDOWPOS *winpos;
2314     BOOL res = TRUE;
2315     int i;
2316
2317     TRACE("%p\n", hdwp);
2318
2319     if (!(pDWP = free_user_handle( hdwp, USER_DWP ))) return FALSE;
2320     if (pDWP == OBJ_OTHER_PROCESS)
2321     {
2322         FIXME( "other process handle %p?\n", hdwp );
2323         return FALSE;
2324     }
2325
2326     for (i = 0, winpos = pDWP->winPos; res && i < pDWP->actualCount; i++, winpos++)
2327     {
2328         TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2329                winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
2330                winpos->cx, winpos->cy, winpos->flags);
2331
2332         if (WIN_IsCurrentThread( winpos->hwnd ))
2333             res = USER_SetWindowPos( winpos );
2334         else
2335             res = SendMessageW( winpos->hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)winpos );
2336     }
2337     HeapFree( GetProcessHeap(), 0, pDWP->winPos );
2338     HeapFree( GetProcessHeap(), 0, pDWP );
2339     return res;
2340 }
2341
2342
2343 /***********************************************************************
2344  *              ArrangeIconicWindows (USER32.@)
2345  */
2346 UINT WINAPI ArrangeIconicWindows( HWND parent )
2347 {
2348     RECT rectParent;
2349     HWND hwndChild;
2350     INT x, y, xspacing, yspacing;
2351
2352     GetClientRect( parent, &rectParent );
2353     x = rectParent.left;
2354     y = rectParent.bottom;
2355     xspacing = GetSystemMetrics(SM_CXICONSPACING);
2356     yspacing = GetSystemMetrics(SM_CYICONSPACING);
2357
2358     hwndChild = GetWindow( parent, GW_CHILD );
2359     while (hwndChild)
2360     {
2361         if( IsIconic( hwndChild ) )
2362         {
2363             WINPOS_ShowIconTitle( hwndChild, FALSE );
2364
2365             SetWindowPos( hwndChild, 0, x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
2366                             y - yspacing - GetSystemMetrics(SM_CYICON)/2, 0, 0,
2367                             SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
2368             if( IsWindow(hwndChild) )
2369                 WINPOS_ShowIconTitle(hwndChild , TRUE );
2370
2371             if (x <= rectParent.right - xspacing) x += xspacing;
2372             else
2373             {
2374                 x = rectParent.left;
2375                 y -= yspacing;
2376             }
2377         }
2378         hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
2379     }
2380     return yspacing;
2381 }
2382
2383
2384 /***********************************************************************
2385  *           draw_moving_frame
2386  *
2387  * Draw the frame used when moving or resizing window.
2388  */
2389 static void draw_moving_frame( HWND parent, HDC hdc, RECT *screen_rect, BOOL thickframe )
2390 {
2391     RECT rect = *screen_rect;
2392
2393     if (parent) MapWindowPoints( 0, parent, (POINT *)&rect, 2 );
2394     if (thickframe)
2395     {
2396         const int width = GetSystemMetrics(SM_CXFRAME);
2397         const int height = GetSystemMetrics(SM_CYFRAME);
2398
2399         HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
2400         PatBlt( hdc, rect.left, rect.top,
2401                 rect.right - rect.left - width, height, PATINVERT );
2402         PatBlt( hdc, rect.left, rect.top + height, width,
2403                 rect.bottom - rect.top - height, PATINVERT );
2404         PatBlt( hdc, rect.left + width, rect.bottom - 1,
2405                 rect.right - rect.left - width, -height, PATINVERT );
2406         PatBlt( hdc, rect.right - 1, rect.top, -width,
2407                 rect.bottom - rect.top - height, PATINVERT );
2408         SelectObject( hdc, hbrush );
2409     }
2410     else DrawFocusRect( hdc, &rect );
2411 }
2412
2413
2414 /***********************************************************************
2415  *           start_size_move
2416  *
2417  * Initialization of a move or resize, when initiated from a menu choice.
2418  * Return hit test code for caption or sizing border.
2419  */
2420 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
2421 {
2422     LONG hittest = 0;
2423     POINT pt;
2424     MSG msg;
2425     RECT rectWindow;
2426
2427     GetWindowRect( hwnd, &rectWindow );
2428
2429     if ((wParam & 0xfff0) == SC_MOVE)
2430     {
2431         /* Move pointer at the center of the caption */
2432         RECT rect = rectWindow;
2433         /* Note: to be exactly centered we should take the different types
2434          * of border into account, but it shouldn't make more than a few pixels
2435          * of difference so let's not bother with that */
2436         rect.top += GetSystemMetrics(SM_CYBORDER);
2437         if (style & WS_SYSMENU)
2438             rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
2439         if (style & WS_MINIMIZEBOX)
2440             rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
2441         if (style & WS_MAXIMIZEBOX)
2442             rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
2443         pt.x = (rect.right + rect.left) / 2;
2444         pt.y = rect.top + GetSystemMetrics(SM_CYSIZE)/2;
2445         hittest = HTCAPTION;
2446         *capturePoint = pt;
2447     }
2448     else  /* SC_SIZE */
2449     {
2450         SetCursor( LoadCursorW( 0, (LPWSTR)IDC_SIZEALL ) );
2451         pt.x = pt.y = 0;
2452         while(!hittest)
2453         {
2454             if (!GetMessageW( &msg, 0, 0, 0 )) return 0;
2455             if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2456
2457             switch(msg.message)
2458             {
2459             case WM_MOUSEMOVE:
2460                 pt.x = min( max( msg.pt.x, rectWindow.left ), rectWindow.right - 1 );
2461                 pt.y = min( max( msg.pt.y, rectWindow.top ), rectWindow.bottom - 1 );
2462                 hittest = SendMessageW( hwnd, WM_NCHITTEST, 0, MAKELONG( pt.x, pt.y ) );
2463                 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT)) hittest = 0;
2464                 break;
2465
2466             case WM_LBUTTONUP:
2467                 return 0;
2468
2469             case WM_KEYDOWN:
2470                 switch(msg.wParam)
2471                 {
2472                 case VK_UP:
2473                     hittest = HTTOP;
2474                     pt.x =(rectWindow.left+rectWindow.right)/2;
2475                     pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
2476                     break;
2477                 case VK_DOWN:
2478                     hittest = HTBOTTOM;
2479                     pt.x =(rectWindow.left+rectWindow.right)/2;
2480                     pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
2481                     break;
2482                 case VK_LEFT:
2483                     hittest = HTLEFT;
2484                     pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
2485                     pt.y =(rectWindow.top+rectWindow.bottom)/2;
2486                     break;
2487                 case VK_RIGHT:
2488                     hittest = HTRIGHT;
2489                     pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
2490                     pt.y =(rectWindow.top+rectWindow.bottom)/2;
2491                     break;
2492                 case VK_RETURN:
2493                 case VK_ESCAPE:
2494                     return 0;
2495                 }
2496                 break;
2497             default:
2498                 TranslateMessage( &msg );
2499                 DispatchMessageW( &msg );
2500                 break;
2501             }
2502         }
2503         *capturePoint = pt;
2504     }
2505     SetCursorPos( pt.x, pt.y );
2506     SendMessageW( hwnd, WM_SETCURSOR, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
2507     return hittest;
2508 }
2509
2510
2511 /***********************************************************************
2512  *           WINPOS_SysCommandSizeMove
2513  *
2514  * Perform SC_MOVE and SC_SIZE commands.
2515  */
2516 void WINPOS_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
2517 {
2518     MSG msg;
2519     RECT sizingRect, mouseRect, origRect;
2520     HDC hdc;
2521     HWND parent;
2522     LONG hittest = (LONG)(wParam & 0x0f);
2523     WPARAM syscommand = wParam & 0xfff0;
2524     HCURSOR hDragCursor = 0, hOldCursor = 0;
2525     POINT minTrack, maxTrack;
2526     POINT capturePoint, pt;
2527     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
2528     BOOL    thickframe = HAS_THICKFRAME( style );
2529     BOOL    iconic = style & WS_MINIMIZE;
2530     BOOL    moved = FALSE;
2531     DWORD     dwPoint = GetMessagePos ();
2532     BOOL DragFullWindows = TRUE;
2533
2534     if (IsZoomed(hwnd) || !IsWindowVisible(hwnd)) return;
2535
2536     pt.x = (short)LOWORD(dwPoint);
2537     pt.y = (short)HIWORD(dwPoint);
2538     capturePoint = pt;
2539     ClipCursor( NULL );
2540
2541     TRACE("hwnd %p command %04lx, hittest %d, pos %d,%d\n",
2542           hwnd, syscommand, hittest, pt.x, pt.y);
2543
2544     if (syscommand == SC_MOVE)
2545     {
2546         if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
2547         if (!hittest) return;
2548     }
2549     else  /* SC_SIZE */
2550     {
2551         if ( hittest && (syscommand != SC_MOUSEMENU) )
2552             hittest += (HTLEFT - WMSZ_LEFT);
2553         else
2554         {
2555             set_capture_window( hwnd, GUI_INMOVESIZE, NULL );
2556             hittest = start_size_move( hwnd, wParam, &capturePoint, style );
2557             if (!hittest)
2558             {
2559                 set_capture_window( 0, GUI_INMOVESIZE, NULL );
2560                 return;
2561             }
2562         }
2563     }
2564
2565       /* Get min/max info */
2566
2567     WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
2568     WIN_GetRectangles( hwnd, COORDS_PARENT, &sizingRect, NULL );
2569     origRect = sizingRect;
2570     if (style & WS_CHILD)
2571     {
2572         parent = GetParent(hwnd);
2573         GetClientRect( parent, &mouseRect );
2574         MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
2575         MapWindowPoints( parent, 0, (LPPOINT)&sizingRect, 2 );
2576     }
2577     else
2578     {
2579         parent = 0;
2580         mouseRect = get_virtual_screen_rect();
2581     }
2582
2583     if (ON_LEFT_BORDER(hittest))
2584     {
2585         mouseRect.left  = max( mouseRect.left, sizingRect.right-maxTrack.x );
2586         mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
2587     }
2588     else if (ON_RIGHT_BORDER(hittest))
2589     {
2590         mouseRect.left  = max( mouseRect.left, sizingRect.left+minTrack.x );
2591         mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
2592     }
2593     if (ON_TOP_BORDER(hittest))
2594     {
2595         mouseRect.top    = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
2596         mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
2597     }
2598     else if (ON_BOTTOM_BORDER(hittest))
2599     {
2600         mouseRect.top    = max( mouseRect.top, sizingRect.top+minTrack.y );
2601         mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
2602     }
2603
2604     /* Retrieve a default cache DC (without using the window style) */
2605     hdc = GetDCEx( parent, 0, DCX_CACHE );
2606
2607     if( iconic ) /* create a cursor for dragging */
2608     {
2609         hDragCursor = (HCURSOR)GetClassLongPtrW( hwnd, GCLP_HICON);
2610         if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageW( hwnd, WM_QUERYDRAGICON, 0, 0L);
2611         if( !hDragCursor ) iconic = FALSE;
2612     }
2613
2614     /* we only allow disabling the full window drag for child windows */
2615     if (parent) SystemParametersInfoW( SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0 );
2616
2617     /* repaint the window before moving it around */
2618     RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
2619
2620     SendMessageW( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
2621     set_capture_window( hwnd, GUI_INMOVESIZE, NULL );
2622
2623     while(1)
2624     {
2625         int dx = 0, dy = 0;
2626
2627         if (!GetMessageW( &msg, 0, 0, 0 )) break;
2628         if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2629
2630         /* Exit on button-up, Return, or Esc */
2631         if ((msg.message == WM_LBUTTONUP) ||
2632             ((msg.message == WM_KEYDOWN) &&
2633              ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
2634
2635         if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
2636         {
2637             TranslateMessage( &msg );
2638             DispatchMessageW( &msg );
2639             continue;  /* We are not interested in other messages */
2640         }
2641
2642         pt = msg.pt;
2643
2644         if (msg.message == WM_KEYDOWN) switch(msg.wParam)
2645         {
2646         case VK_UP:    pt.y -= 8; break;
2647         case VK_DOWN:  pt.y += 8; break;
2648         case VK_LEFT:  pt.x -= 8; break;
2649         case VK_RIGHT: pt.x += 8; break;
2650         }
2651
2652         pt.x = max( pt.x, mouseRect.left );
2653         pt.x = min( pt.x, mouseRect.right );
2654         pt.y = max( pt.y, mouseRect.top );
2655         pt.y = min( pt.y, mouseRect.bottom );
2656
2657         dx = pt.x - capturePoint.x;
2658         dy = pt.y - capturePoint.y;
2659
2660         if (dx || dy)
2661         {
2662             if( !moved )
2663             {
2664                 moved = TRUE;
2665
2666                 if( iconic ) /* ok, no system popup tracking */
2667                 {
2668                     hOldCursor = SetCursor(hDragCursor);
2669                     ShowCursor( TRUE );
2670                     WINPOS_ShowIconTitle( hwnd, FALSE );
2671                 }
2672                 else if(!DragFullWindows)
2673                     draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2674             }
2675
2676             if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
2677             else
2678             {
2679                 WPARAM wpSizingHit = 0;
2680
2681                 if(!iconic && !DragFullWindows) draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2682                 if (hittest == HTCAPTION) OffsetRect( &sizingRect, dx, dy );
2683                 if (ON_LEFT_BORDER(hittest)) sizingRect.left += dx;
2684                 else if (ON_RIGHT_BORDER(hittest)) sizingRect.right += dx;
2685                 if (ON_TOP_BORDER(hittest)) sizingRect.top += dy;
2686                 else if (ON_BOTTOM_BORDER(hittest)) sizingRect.bottom += dy;
2687                 capturePoint = pt;
2688
2689                 /* determine the hit location */
2690                 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
2691                     wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
2692                 SendMessageW( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&sizingRect );
2693
2694                 if (!iconic)
2695                 {
2696                     if(!DragFullWindows)
2697                         draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2698                     else
2699                     {
2700                         RECT rect = sizingRect;
2701                         MapWindowPoints( 0, parent, (POINT *)&rect, 2 );
2702                         SetWindowPos( hwnd, 0, rect.left, rect.top,
2703                                       rect.right - rect.left, rect.bottom - rect.top,
2704                                       ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2705                     }
2706                 }
2707             }
2708         }
2709     }
2710
2711     if( iconic )
2712     {
2713         if( moved ) /* restore cursors, show icon title later on */
2714         {
2715             ShowCursor( FALSE );
2716             SetCursor( hOldCursor );
2717         }
2718     }
2719     else if (moved && !DragFullWindows)
2720     {
2721         draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2722     }
2723
2724     set_capture_window( 0, GUI_INMOVESIZE, NULL );
2725     ReleaseDC( parent, hdc );
2726     if (parent) MapWindowPoints( 0, parent, (POINT *)&sizingRect, 2 );
2727
2728     if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
2729         moved = FALSE;
2730
2731     SendMessageW( hwnd, WM_EXITSIZEMOVE, 0, 0 );
2732     SendMessageW( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
2733
2734     /* window moved or resized */
2735     if (moved)
2736     {
2737         /* if the moving/resizing isn't canceled call SetWindowPos
2738          * with the new position or the new size of the window
2739          */
2740         if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
2741         {
2742             /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
2743             if(!DragFullWindows || iconic)
2744                 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
2745                               sizingRect.right - sizingRect.left,
2746                               sizingRect.bottom - sizingRect.top,
2747                               ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2748         }
2749         else
2750         { /* restore previous size/position */
2751             if(DragFullWindows)
2752                 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
2753                               origRect.right - origRect.left,
2754                               origRect.bottom - origRect.top,
2755                               ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2756         }
2757     }
2758
2759     if (IsIconic(hwnd))
2760     {
2761         /* Single click brings up the system menu when iconized */
2762
2763         if( !moved )
2764         {
2765             if(style & WS_SYSMENU )
2766                 SendMessageW( hwnd, WM_SYSCOMMAND,
2767                               SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
2768         }
2769         else WINPOS_ShowIconTitle( hwnd, TRUE );
2770     }
2771 }