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