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