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