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