comctl32: Constify some variables.
[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 "winerror.h"
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winerror.h"
34 #include "wine/winuser16.h"
35 #include "wine/server.h"
36 #include "controls.h"
37 #include "user_private.h"
38 #include "win.h"
39 #include "wine/debug.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(win);
42
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)          ((*(LONG*)&(pt)) == -1)
57
58 #define PLACE_MIN               0x0001
59 #define PLACE_MAX               0x0002
60 #define PLACE_RECT              0x0004
61
62
63 #define DWP_MAGIC  ((INT)('W' | ('P' << 8) | ('O' << 16) | ('S' << 24)))
64
65 typedef struct
66 {
67     INT       actualCount;
68     INT       suggestedCount;
69     BOOL      valid;
70     INT       wMagic;
71     HWND      hwndParent;
72     WINDOWPOS winPos[1];
73 } DWP;
74
75 typedef struct
76 {
77     RECT16   rectNormal;
78     POINT16  ptIconPos;
79     POINT16  ptMaxPos;
80     HWND     hwndIconTitle;
81 } INTERNALPOS, *LPINTERNALPOS;
82
83 /* ----- internal functions ----- */
84
85 static const WCHAR SysIP_W[] = { 'S','y','s','I','P',0 };
86
87 static inline INTERNALPOS *get_internal_pos( HWND hwnd )
88 {
89     return GetPropW( hwnd, SysIP_W );
90 }
91
92 static inline void set_internal_pos( HWND hwnd, INTERNALPOS *pos )
93 {
94     SetPropW( hwnd, SysIP_W, pos );
95 }
96
97 /***********************************************************************
98  *           WINPOS_CheckInternalPos
99  *
100  * Called when a window is destroyed.
101  */
102 void WINPOS_CheckInternalPos( HWND hwnd )
103 {
104     LPINTERNALPOS lpPos = get_internal_pos( hwnd );
105
106     if ( lpPos )
107     {
108         if( IsWindow(lpPos->hwndIconTitle) )
109             DestroyWindow( lpPos->hwndIconTitle );
110         HeapFree( GetProcessHeap(), 0, lpPos );
111     }
112 }
113
114 /***********************************************************************
115  *              ArrangeIconicWindows (USER32.@)
116  */
117 UINT WINAPI ArrangeIconicWindows( HWND parent )
118 {
119     RECT rectParent;
120     HWND hwndChild;
121     INT x, y, xspacing, yspacing;
122
123     GetClientRect( parent, &rectParent );
124     x = rectParent.left;
125     y = rectParent.bottom;
126     xspacing = GetSystemMetrics(SM_CXICONSPACING);
127     yspacing = GetSystemMetrics(SM_CYICONSPACING);
128
129     hwndChild = GetWindow( parent, GW_CHILD );
130     while (hwndChild)
131     {
132         if( IsIconic( hwndChild ) )
133         {
134             WINPOS_ShowIconTitle( hwndChild, FALSE );
135
136             SetWindowPos( hwndChild, 0, x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
137                             y - yspacing - GetSystemMetrics(SM_CYICON)/2, 0, 0,
138                             SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
139             if( IsWindow(hwndChild) )
140                 WINPOS_ShowIconTitle(hwndChild , TRUE );
141
142             if (x <= rectParent.right - xspacing) x += xspacing;
143             else
144             {
145                 x = rectParent.left;
146                 y -= yspacing;
147             }
148         }
149         hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
150     }
151     return yspacing;
152 }
153
154
155 /***********************************************************************
156  *              SwitchToThisWindow (USER32.@)
157  */
158 void WINAPI SwitchToThisWindow( HWND hwnd, BOOL restore )
159 {
160     ShowWindow( hwnd, restore ? SW_RESTORE : SW_SHOWMINIMIZED );
161 }
162
163
164 /***********************************************************************
165  *              GetWindowRect (USER32.@)
166  */
167 BOOL WINAPI GetWindowRect( HWND hwnd, LPRECT rect )
168 {
169     BOOL ret = WIN_GetRectangles( hwnd, rect, NULL );
170     if (ret)
171     {
172         MapWindowPoints( GetAncestor( hwnd, GA_PARENT ), 0, (POINT *)rect, 2 );
173         TRACE( "hwnd %p (%d,%d)-(%d,%d)\n",
174                hwnd, rect->left, rect->top, rect->right, rect->bottom);
175     }
176     return ret;
177 }
178
179
180 /***********************************************************************
181  *              GetWindowRgn (USER32.@)
182  */
183 int WINAPI GetWindowRgn ( HWND hwnd, HRGN hrgn )
184 {
185     int nRet = ERROR;
186     NTSTATUS status;
187     HRGN win_rgn = 0;
188     RGNDATA *data;
189     size_t size = 256;
190
191     do
192     {
193         if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
194         {
195             SetLastError( ERROR_OUTOFMEMORY );
196             return ERROR;
197         }
198         SERVER_START_REQ( get_window_region )
199         {
200             req->window = hwnd;
201             wine_server_set_reply( req, data->Buffer, size );
202             if (!(status = wine_server_call( req )))
203             {
204                 size_t reply_size = wine_server_reply_size( reply );
205                 if (reply_size)
206                 {
207                     data->rdh.dwSize   = sizeof(data->rdh);
208                     data->rdh.iType    = RDH_RECTANGLES;
209                     data->rdh.nCount   = reply_size / sizeof(RECT);
210                     data->rdh.nRgnSize = reply_size;
211                     win_rgn = ExtCreateRegion( NULL, size, data );
212                 }
213             }
214             else size = reply->total_size;
215         }
216         SERVER_END_REQ;
217         HeapFree( GetProcessHeap(), 0, data );
218     } while (status == STATUS_BUFFER_OVERFLOW);
219
220     if (status) SetLastError( RtlNtStatusToDosError(status) );
221     else if (win_rgn)
222     {
223         nRet = CombineRgn( hrgn, win_rgn, 0, RGN_COPY );
224         DeleteObject( win_rgn );
225     }
226     return nRet;
227 }
228
229
230 /***********************************************************************
231  *              SetWindowRgn (USER32.@)
232  */
233 int WINAPI SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL bRedraw )
234 {
235     static const RECT empty_rect;
236     BOOL ret;
237
238     if (hrgn)
239     {
240         RGNDATA *data;
241         DWORD size;
242
243         if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
244         if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
245         if (!GetRegionData( hrgn, size, data ))
246         {
247             HeapFree( GetProcessHeap(), 0, data );
248             return FALSE;
249         }
250         SERVER_START_REQ( set_window_region )
251         {
252             req->window = hwnd;
253             req->redraw = (bRedraw != 0);
254             if (data->rdh.nCount)
255                 wine_server_add_data( req, data->Buffer, data->rdh.nCount * sizeof(RECT) );
256             else
257                 wine_server_add_data( req, &empty_rect, sizeof(empty_rect) );
258             ret = !wine_server_call_err( req );
259         }
260         SERVER_END_REQ;
261     }
262     else  /* clear existing region */
263     {
264         SERVER_START_REQ( set_window_region )
265         {
266             req->window = hwnd;
267             req->redraw = (bRedraw != 0);
268             ret = !wine_server_call_err( req );
269         }
270         SERVER_END_REQ;
271     }
272
273     if (ret) ret = USER_Driver->pSetWindowRgn( hwnd, hrgn, bRedraw );
274
275     if (ret)
276     {
277         UINT swp_flags = SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_FRAMECHANGED;
278         if (hrgn) swp_flags |= SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE;
279         if (!bRedraw) swp_flags |= SWP_NOREDRAW;
280         SetWindowPos( hwnd, 0, 0, 0, 0, 0, swp_flags );
281     }
282     return ret;
283 }
284
285
286 /***********************************************************************
287  *              GetClientRect (USER32.@)
288  */
289 BOOL WINAPI GetClientRect( HWND hwnd, LPRECT rect )
290 {
291     BOOL ret;
292
293     if ((ret = WIN_GetRectangles( hwnd, NULL, rect )))
294     {
295         rect->right -= rect->left;
296         rect->bottom -= rect->top;
297         rect->left = rect->top = 0;
298     }
299     return ret;
300 }
301
302
303 /*******************************************************************
304  *              ClientToScreen (USER32.@)
305  */
306 BOOL WINAPI ClientToScreen( HWND hwnd, LPPOINT lppnt )
307 {
308     MapWindowPoints( hwnd, 0, lppnt, 1 );
309     return TRUE;
310 }
311
312
313 /*******************************************************************
314  *              ScreenToClient (USER32.@)
315  */
316 BOOL WINAPI ScreenToClient( HWND hwnd, LPPOINT lppnt )
317 {
318     MapWindowPoints( 0, hwnd, lppnt, 1 );
319     return TRUE;
320 }
321
322
323 /***********************************************************************
324  *           list_children_from_point
325  *
326  * Get the list of children that can contain point from the server.
327  * Point is in screen coordinates.
328  * Returned list must be freed by caller.
329  */
330 static HWND *list_children_from_point( HWND hwnd, POINT pt )
331 {
332     HWND *list;
333     int size = 32;
334
335     for (;;)
336     {
337         int count = 0;
338
339         if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
340
341         SERVER_START_REQ( get_window_children_from_point )
342         {
343             req->parent = hwnd;
344             req->x = pt.x;
345             req->y = pt.y;
346             wine_server_set_reply( req, list, (size-1) * sizeof(HWND) );
347             if (!wine_server_call( req )) count = reply->count;
348         }
349         SERVER_END_REQ;
350         if (count && count < size)
351         {
352             list[count] = 0;
353             return list;
354         }
355         HeapFree( GetProcessHeap(), 0, list );
356         if (!count) break;
357         size = count + 1;  /* restart with a large enough buffer */
358     }
359     return NULL;
360 }
361
362
363 /***********************************************************************
364  *           WINPOS_WindowFromPoint
365  *
366  * Find the window and hittest for a given point.
367  */
368 HWND WINPOS_WindowFromPoint( HWND hwndScope, POINT pt, INT *hittest )
369 {
370     int i, res;
371     HWND ret, *list;
372
373     if (!hwndScope) hwndScope = GetDesktopWindow();
374
375     *hittest = HTNOWHERE;
376
377     if (!(list = list_children_from_point( hwndScope, pt ))) return 0;
378
379     /* now determine the hittest */
380
381     for (i = 0; list[i]; i++)
382     {
383         LONG style = GetWindowLongW( list[i], GWL_STYLE );
384
385         /* If window is minimized or disabled, return at once */
386         if (style & WS_MINIMIZE)
387         {
388             *hittest = HTCAPTION;
389             break;
390         }
391         if (style & WS_DISABLED)
392         {
393             *hittest = HTERROR;
394             break;
395         }
396         /* Send WM_NCCHITTEST (if same thread) */
397         if (!WIN_IsCurrentThread( list[i] ))
398         {
399             *hittest = HTCLIENT;
400             break;
401         }
402         res = SendMessageW( list[i], WM_NCHITTEST, 0, MAKELONG(pt.x,pt.y) );
403         if (res != HTTRANSPARENT)
404         {
405             *hittest = res;  /* Found the window */
406             break;
407         }
408         /* continue search with next window in z-order */
409     }
410     ret = list[i];
411     HeapFree( GetProcessHeap(), 0, list );
412     TRACE( "scope %p (%d,%d) returning %p\n", hwndScope, pt.x, pt.y, ret );
413     return ret;
414 }
415
416
417 /*******************************************************************
418  *              WindowFromPoint (USER32.@)
419  */
420 HWND WINAPI WindowFromPoint( POINT pt )
421 {
422     INT hittest;
423     return WINPOS_WindowFromPoint( 0, pt, &hittest );
424 }
425
426
427 /*******************************************************************
428  *              ChildWindowFromPoint (USER32.@)
429  */
430 HWND WINAPI ChildWindowFromPoint( HWND hwndParent, POINT pt )
431 {
432     return ChildWindowFromPointEx( hwndParent, pt, CWP_ALL );
433 }
434
435 /*******************************************************************
436  *              ChildWindowFromPointEx (USER32.@)
437  */
438 HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
439 {
440     /* pt is in the client coordinates */
441     HWND *list;
442     int i;
443     RECT rect;
444     HWND retvalue;
445
446     GetClientRect( hwndParent, &rect );
447     if (!PtInRect( &rect, pt )) return 0;
448     if (!(list = WIN_ListChildren( hwndParent ))) return hwndParent;
449
450     for (i = 0; list[i]; i++)
451     {
452         if (!WIN_GetRectangles( list[i], &rect, NULL )) continue;
453         if (!PtInRect( &rect, pt )) continue;
454         if (uFlags & (CWP_SKIPINVISIBLE|CWP_SKIPDISABLED))
455         {
456             LONG style = GetWindowLongW( list[i], GWL_STYLE );
457             if ((uFlags & CWP_SKIPINVISIBLE) && !(style & WS_VISIBLE)) continue;
458             if ((uFlags & CWP_SKIPDISABLED) && (style & WS_DISABLED)) continue;
459         }
460         if (uFlags & CWP_SKIPTRANSPARENT)
461         {
462             if (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TRANSPARENT) continue;
463         }
464         break;
465     }
466     retvalue = list[i];
467     HeapFree( GetProcessHeap(), 0, list );
468     if (!retvalue) retvalue = hwndParent;
469     return retvalue;
470 }
471
472
473 /*******************************************************************
474  *         WINPOS_GetWinOffset
475  *
476  * Calculate the offset between the origin of the two windows. Used
477  * to implement MapWindowPoints.
478  */
479 static void WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo, POINT *offset )
480 {
481     WND * wndPtr;
482
483     offset->x = offset->y = 0;
484
485     /* Translate source window origin to screen coords */
486     if (hwndFrom)
487     {
488         HWND hwnd = hwndFrom;
489
490         while (hwnd)
491         {
492             if (hwnd == hwndTo) return;
493             if (!(wndPtr = WIN_GetPtr( hwnd )))
494             {
495                 ERR( "bad hwndFrom = %p\n", hwnd );
496                 return;
497             }
498             if (wndPtr == WND_DESKTOP) break;
499             if (wndPtr == WND_OTHER_PROCESS) goto other_process;
500             offset->x += wndPtr->rectClient.left;
501             offset->y += wndPtr->rectClient.top;
502             hwnd = wndPtr->parent;
503             WIN_ReleasePtr( wndPtr );
504         }
505     }
506
507     /* Translate origin to destination window coords */
508     if (hwndTo)
509     {
510         HWND hwnd = hwndTo;
511
512         while (hwnd)
513         {
514             if (!(wndPtr = WIN_GetPtr( hwnd )))
515             {
516                 ERR( "bad hwndTo = %p\n", hwnd );
517                 return;
518             }
519             if (wndPtr == WND_DESKTOP) break;
520             if (wndPtr == WND_OTHER_PROCESS) goto other_process;
521             offset->x -= wndPtr->rectClient.left;
522             offset->y -= wndPtr->rectClient.top;
523             hwnd = wndPtr->parent;
524             WIN_ReleasePtr( wndPtr );
525         }
526     }
527     return;
528
529  other_process:  /* one of the parents may belong to another process, do it the hard way */
530     offset->x = offset->y = 0;
531     SERVER_START_REQ( get_windows_offset )
532     {
533         req->from = hwndFrom;
534         req->to   = hwndTo;
535         if (!wine_server_call( req ))
536         {
537             offset->x = reply->x;
538             offset->y = reply->y;
539         }
540     }
541     SERVER_END_REQ;
542 }
543
544
545 /*******************************************************************
546  *              MapWindowPoints (USER.258)
547  */
548 void WINAPI MapWindowPoints16( HWND16 hwndFrom, HWND16 hwndTo,
549                                LPPOINT16 lppt, UINT16 count )
550 {
551     POINT offset;
552
553     WINPOS_GetWinOffset( WIN_Handle32(hwndFrom), WIN_Handle32(hwndTo), &offset );
554     while (count--)
555     {
556         lppt->x += offset.x;
557         lppt->y += offset.y;
558         lppt++;
559     }
560 }
561
562
563 /*******************************************************************
564  *              MapWindowPoints (USER32.@)
565  */
566 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
567 {
568     POINT offset;
569
570     WINPOS_GetWinOffset( hwndFrom, hwndTo, &offset );
571     while (count--)
572     {
573         lppt->x += offset.x;
574         lppt->y += offset.y;
575         lppt++;
576     }
577     return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
578 }
579
580
581 /***********************************************************************
582  *              IsIconic (USER32.@)
583  */
584 BOOL WINAPI IsIconic(HWND hWnd)
585 {
586     return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
587 }
588
589
590 /***********************************************************************
591  *              IsZoomed (USER32.@)
592  */
593 BOOL WINAPI IsZoomed(HWND hWnd)
594 {
595     return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
596 }
597
598
599 /*******************************************************************
600  *              AllowSetForegroundWindow (USER32.@)
601  */
602 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
603 {
604     /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
605      * implemented, then fix this function. */
606     return TRUE;
607 }
608
609
610 /*******************************************************************
611  *              LockSetForegroundWindow (USER32.@)
612  */
613 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
614 {
615     /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
616      * implemented, then fix this function. */
617     return TRUE;
618 }
619
620
621 /***********************************************************************
622  *              BringWindowToTop (USER32.@)
623  */
624 BOOL WINAPI BringWindowToTop( HWND hwnd )
625 {
626     return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
627 }
628
629
630 /***********************************************************************
631  *              MoveWindow (USER32.@)
632  */
633 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
634                             BOOL repaint )
635 {
636     int flags = SWP_NOZORDER | SWP_NOACTIVATE;
637     if (!repaint) flags |= SWP_NOREDRAW;
638     TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
639     return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
640 }
641
642 /***********************************************************************
643  *           WINPOS_InitInternalPos
644  */
645 static LPINTERNALPOS WINPOS_InitInternalPos( WND* wnd )
646 {
647     LPINTERNALPOS lpPos = get_internal_pos( wnd->hwndSelf );
648     if( !lpPos )
649     {
650         /* this happens when the window is minimized/maximized
651          * for the first time (rectWindow is not adjusted yet) */
652
653         lpPos = HeapAlloc( GetProcessHeap(), 0, sizeof(INTERNALPOS) );
654         if( !lpPos ) return NULL;
655
656         set_internal_pos( wnd->hwndSelf, lpPos );
657         lpPos->hwndIconTitle = 0; /* defer until needs to be shown */
658         lpPos->rectNormal.left   = wnd->rectWindow.left;
659         lpPos->rectNormal.top    = wnd->rectWindow.top;
660         lpPos->rectNormal.right  = wnd->rectWindow.right;
661         lpPos->rectNormal.bottom = wnd->rectWindow.bottom;
662         lpPos->ptIconPos.x = lpPos->ptIconPos.y = -1;
663         lpPos->ptMaxPos.x = lpPos->ptMaxPos.y = -1;
664     }
665
666     if( wnd->dwStyle & WS_MINIMIZE )
667     {
668         lpPos->ptIconPos.x = wnd->rectWindow.left;
669         lpPos->ptIconPos.y = wnd->rectWindow.top;
670     }
671     else if( wnd->dwStyle & WS_MAXIMIZE )
672     {
673         lpPos->ptMaxPos.x = wnd->rectWindow.left;
674         lpPos->ptMaxPos.y = wnd->rectWindow.top;
675     }
676     else
677     {
678         lpPos->rectNormal.left   = wnd->rectWindow.left;
679         lpPos->rectNormal.top    = wnd->rectWindow.top;
680         lpPos->rectNormal.right  = wnd->rectWindow.right;
681         lpPos->rectNormal.bottom = wnd->rectWindow.bottom;
682     }
683     return lpPos;
684 }
685
686 /***********************************************************************
687  *           WINPOS_RedrawIconTitle
688  */
689 BOOL WINPOS_RedrawIconTitle( HWND hWnd )
690 {
691     LPINTERNALPOS lpPos = get_internal_pos( hWnd );
692     if( lpPos )
693     {
694         if( lpPos->hwndIconTitle )
695         {
696             SendMessageW( lpPos->hwndIconTitle, WM_SHOWWINDOW, TRUE, 0);
697             InvalidateRect( lpPos->hwndIconTitle, NULL, TRUE );
698             return TRUE;
699         }
700     }
701     return FALSE;
702 }
703
704 /***********************************************************************
705  *           WINPOS_ShowIconTitle
706  */
707 BOOL WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
708 {
709     LPINTERNALPOS lpPos = get_internal_pos( hwnd );
710
711     if (lpPos && !GetPropA( hwnd, "__wine_x11_managed" ))
712     {
713         HWND title = lpPos->hwndIconTitle;
714
715         TRACE("%p %i\n", hwnd, (bShow != 0) );
716
717         if( !title )
718             lpPos->hwndIconTitle = title = ICONTITLE_Create( hwnd );
719         if( bShow )
720         {
721             if (!IsWindowVisible(title))
722             {
723                 SendMessageW( title, WM_SHOWWINDOW, TRUE, 0 );
724                 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
725                               SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
726             }
727         }
728         else ShowWindow( title, SW_HIDE );
729     }
730     return FALSE;
731 }
732
733 /*******************************************************************
734  *           WINPOS_GetMinMaxInfo
735  *
736  * Get the minimized and maximized information for a window.
737  */
738 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
739                            POINT *minTrack, POINT *maxTrack )
740 {
741     LPINTERNALPOS lpPos;
742     MINMAXINFO MinMax;
743     HMONITOR monitor;
744     INT xinc, yinc;
745     LONG style = GetWindowLongA( hwnd, GWL_STYLE );
746     LONG exstyle = GetWindowLongA( hwnd, GWL_EXSTYLE );
747     RECT rc;
748
749     /* Compute default values */
750
751     GetWindowRect(hwnd, &rc);
752     MinMax.ptReserved.x = rc.left;
753     MinMax.ptReserved.y = rc.top;
754
755     if (style & WS_CHILD)
756     {
757         if ((style & WS_CAPTION) == WS_CAPTION)
758             style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
759
760         GetClientRect(GetAncestor(hwnd,GA_PARENT), &rc);
761         AdjustWindowRectEx(&rc, style, ((style & WS_POPUP) && GetMenu(hwnd)), exstyle);
762
763         /* avoid calculating this twice */
764         style &= ~(WS_DLGFRAME | WS_BORDER | WS_THICKFRAME);
765
766         MinMax.ptMaxSize.x = rc.right - rc.left;
767         MinMax.ptMaxSize.y = rc.bottom - rc.top;
768     }
769     else
770     {
771         MinMax.ptMaxSize.x = GetSystemMetrics(SM_CXSCREEN);
772         MinMax.ptMaxSize.y = GetSystemMetrics(SM_CYSCREEN);
773     }
774     MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
775     MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
776     MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXMAXTRACK);
777     MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYMAXTRACK);
778
779     if (HAS_DLGFRAME( style, exstyle ))
780     {
781         xinc = GetSystemMetrics(SM_CXDLGFRAME);
782         yinc = GetSystemMetrics(SM_CYDLGFRAME);
783     }
784     else
785     {
786         xinc = yinc = 0;
787         if (HAS_THICKFRAME(style))
788         {
789             xinc += GetSystemMetrics(SM_CXFRAME);
790             yinc += GetSystemMetrics(SM_CYFRAME);
791         }
792         if (style & WS_BORDER)
793         {
794             xinc += GetSystemMetrics(SM_CXBORDER);
795             yinc += GetSystemMetrics(SM_CYBORDER);
796         }
797     }
798     MinMax.ptMaxSize.x += 2 * xinc;
799     MinMax.ptMaxSize.y += 2 * yinc;
800
801     lpPos = get_internal_pos( hwnd );
802     if( lpPos && !EMPTYPOINT(lpPos->ptMaxPos) )
803     {
804         MinMax.ptMaxPosition.x = lpPos->ptMaxPos.x;
805         MinMax.ptMaxPosition.y = lpPos->ptMaxPos.y;
806     }
807     else
808     {
809         MinMax.ptMaxPosition.x = -xinc;
810         MinMax.ptMaxPosition.y = -yinc;
811     }
812
813     SendMessageW( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
814
815     /* if the app didn't change the values, adapt them for the current monitor */
816
817     if ((monitor = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY )))
818     {
819         MONITORINFO mon_info;
820
821         mon_info.cbSize = sizeof(mon_info);
822         GetMonitorInfoW( monitor, &mon_info );
823
824         if (MinMax.ptMaxSize.x == GetSystemMetrics(SM_CXSCREEN) + 2 * xinc &&
825             MinMax.ptMaxSize.y == GetSystemMetrics(SM_CYSCREEN) + 2 * yinc)
826         {
827             MinMax.ptMaxSize.x = (mon_info.rcWork.right - mon_info.rcWork.left) + 2 * xinc;
828             MinMax.ptMaxSize.y = (mon_info.rcWork.bottom - mon_info.rcWork.top) + 2 * yinc;
829         }
830         if (MinMax.ptMaxPosition.x == -xinc && MinMax.ptMaxPosition.y == -yinc)
831         {
832             MinMax.ptMaxPosition.x = mon_info.rcWork.left - xinc;
833             MinMax.ptMaxPosition.y = mon_info.rcWork.top - yinc;
834         }
835     }
836
837       /* Some sanity checks */
838
839     TRACE("%d %d / %d %d / %d %d / %d %d\n",
840                       MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
841                       MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
842                       MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
843                       MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
844     MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
845                                    MinMax.ptMinTrackSize.x );
846     MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
847                                    MinMax.ptMinTrackSize.y );
848
849     if (maxSize) *maxSize = MinMax.ptMaxSize;
850     if (maxPos) *maxPos = MinMax.ptMaxPosition;
851     if (minTrack) *minTrack = MinMax.ptMinTrackSize;
852     if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
853 }
854
855 /***********************************************************************
856  *              ShowWindowAsync (USER32.@)
857  *
858  * doesn't wait; returns immediately.
859  * used by threads to toggle windows in other (possibly hanging) threads
860  */
861 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
862 {
863     HWND full_handle;
864
865     if (is_broadcast(hwnd))
866     {
867         SetLastError( ERROR_INVALID_PARAMETER );
868         return FALSE;
869     }
870
871     if ((full_handle = WIN_IsCurrentThread( hwnd )))
872         return USER_Driver->pShowWindow( full_handle, cmd );
873
874     return SendNotifyMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
875 }
876
877
878 /***********************************************************************
879  *              ShowWindow (USER32.@)
880  */
881 BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
882 {
883     HWND full_handle;
884
885     if (is_broadcast(hwnd))
886     {
887         SetLastError( ERROR_INVALID_PARAMETER );
888         return FALSE;
889     }
890     if ((full_handle = WIN_IsCurrentThread( hwnd )))
891         return USER_Driver->pShowWindow( full_handle, cmd );
892
893     return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
894 }
895
896
897 /***********************************************************************
898  *              GetInternalWindowPos (USER32.@)
899  */
900 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
901                                       LPPOINT ptIcon )
902 {
903     WINDOWPLACEMENT wndpl;
904     if (GetWindowPlacement( hwnd, &wndpl ))
905     {
906         if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
907         if (ptIcon)  *ptIcon = wndpl.ptMinPosition;
908         return wndpl.showCmd;
909     }
910     return 0;
911 }
912
913
914 /***********************************************************************
915  *              GetWindowPlacement (USER32.@)
916  *
917  * Win95:
918  * Fails if wndpl->length of Win95 (!) apps is invalid.
919  */
920 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
921 {
922     WND *pWnd = WIN_GetPtr( hwnd );
923     LPINTERNALPOS lpPos;
924
925     if (!pWnd || pWnd == WND_DESKTOP) return FALSE;
926     if (pWnd == WND_OTHER_PROCESS)
927     {
928         if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
929         return FALSE;
930     }
931
932     lpPos = WINPOS_InitInternalPos( pWnd );
933     wndpl->length  = sizeof(*wndpl);
934     if( pWnd->dwStyle & WS_MINIMIZE )
935         wndpl->showCmd = SW_SHOWMINIMIZED;
936     else
937         wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
938     if( pWnd->flags & WIN_RESTORE_MAX )
939         wndpl->flags = WPF_RESTORETOMAXIMIZED;
940     else
941         wndpl->flags = 0;
942     wndpl->ptMinPosition.x = lpPos->ptIconPos.x;
943     wndpl->ptMinPosition.y = lpPos->ptIconPos.y;
944     wndpl->ptMaxPosition.x = lpPos->ptMaxPos.x;
945     wndpl->ptMaxPosition.y = lpPos->ptMaxPos.y;
946     wndpl->rcNormalPosition.left   = lpPos->rectNormal.left;
947     wndpl->rcNormalPosition.top    = lpPos->rectNormal.top;
948     wndpl->rcNormalPosition.right  = lpPos->rectNormal.right;
949     wndpl->rcNormalPosition.bottom = lpPos->rectNormal.bottom;
950     WIN_ReleasePtr( pWnd );
951     return TRUE;
952 }
953
954
955 /***********************************************************************
956  *           WINPOS_SetPlacement
957  */
958 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
959 {
960     LPINTERNALPOS lpPos;
961     DWORD style;
962     WND *pWnd = WIN_GetPtr( hwnd );
963
964     if (!pWnd || pWnd == WND_OTHER_PROCESS || pWnd == WND_DESKTOP) return FALSE;
965     lpPos = WINPOS_InitInternalPos( pWnd );
966
967     if( flags & PLACE_MIN )
968     {
969         lpPos->ptIconPos.x = wndpl->ptMinPosition.x;
970         lpPos->ptIconPos.y = wndpl->ptMinPosition.y;
971     }
972     if( flags & PLACE_MAX )
973     {
974         lpPos->ptMaxPos.x = wndpl->ptMaxPosition.x;
975         lpPos->ptMaxPos.y = wndpl->ptMaxPosition.y;
976     }
977     if( flags & PLACE_RECT)
978     {
979         lpPos->rectNormal.left   = wndpl->rcNormalPosition.left;
980         lpPos->rectNormal.top    = wndpl->rcNormalPosition.top;
981         lpPos->rectNormal.right  = wndpl->rcNormalPosition.right;
982         lpPos->rectNormal.bottom = wndpl->rcNormalPosition.bottom;
983     }
984
985     style = pWnd->dwStyle;
986     WIN_ReleasePtr( pWnd );
987
988     if( style & WS_MINIMIZE )
989     {
990         WINPOS_ShowIconTitle( hwnd, FALSE );
991         if( wndpl->flags & WPF_SETMINPOSITION && !EMPTYPOINT(lpPos->ptIconPos))
992             SetWindowPos( hwnd, 0, lpPos->ptIconPos.x, lpPos->ptIconPos.y,
993                           0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
994     }
995     else if( style & WS_MAXIMIZE )
996     {
997         if( !EMPTYPOINT(lpPos->ptMaxPos) )
998             SetWindowPos( hwnd, 0, lpPos->ptMaxPos.x, lpPos->ptMaxPos.y,
999                           0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1000     }
1001     else if( flags & PLACE_RECT )
1002         SetWindowPos( hwnd, 0, lpPos->rectNormal.left, lpPos->rectNormal.top,
1003                       lpPos->rectNormal.right - lpPos->rectNormal.left,
1004                       lpPos->rectNormal.bottom - lpPos->rectNormal.top,
1005                       SWP_NOZORDER | SWP_NOACTIVATE );
1006
1007     ShowWindow( hwnd, wndpl->showCmd );
1008
1009     if (IsIconic( hwnd ))
1010     {
1011         if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE) WINPOS_ShowIconTitle( hwnd, TRUE );
1012
1013         /* SDK: ...valid only the next time... */
1014         if( wndpl->flags & WPF_RESTORETOMAXIMIZED )
1015         {
1016             pWnd = WIN_GetPtr( hwnd );
1017             if (pWnd && pWnd != WND_OTHER_PROCESS)
1018             {
1019                 pWnd->flags |= WIN_RESTORE_MAX;
1020                 WIN_ReleasePtr( pWnd );
1021             }
1022         }
1023     }
1024     return TRUE;
1025 }
1026
1027
1028 /***********************************************************************
1029  *              SetWindowPlacement (USER32.@)
1030  *
1031  * Win95:
1032  * Fails if wndpl->length of Win95 (!) apps is invalid.
1033  */
1034 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
1035 {
1036     if (!wpl) return FALSE;
1037     return WINPOS_SetPlacement( hwnd, wpl, PLACE_MIN | PLACE_MAX | PLACE_RECT );
1038 }
1039
1040
1041 /***********************************************************************
1042  *              AnimateWindow (USER32.@)
1043  *              Shows/Hides a window with an animation
1044  *              NO ANIMATION YET
1045  */
1046 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
1047 {
1048         FIXME("partial stub\n");
1049
1050         /* If trying to show/hide and it's already   *
1051          * shown/hidden or invalid window, fail with *
1052          * invalid parameter                         */
1053         if(!IsWindow(hwnd) ||
1054            (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
1055            (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
1056         {
1057                 SetLastError(ERROR_INVALID_PARAMETER);
1058                 return FALSE;
1059         }
1060
1061         ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1062
1063         return TRUE;
1064 }
1065
1066 /***********************************************************************
1067  *              SetInternalWindowPos (USER32.@)
1068  */
1069 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1070                                     LPRECT rect, LPPOINT pt )
1071 {
1072     if( IsWindow(hwnd) )
1073     {
1074         WINDOWPLACEMENT wndpl;
1075         UINT flags;
1076
1077         wndpl.length  = sizeof(wndpl);
1078         wndpl.showCmd = showCmd;
1079         wndpl.flags = flags = 0;
1080
1081         if( pt )
1082         {
1083             flags |= PLACE_MIN;
1084             wndpl.flags |= WPF_SETMINPOSITION;
1085             wndpl.ptMinPosition = *pt;
1086         }
1087         if( rect )
1088         {
1089             flags |= PLACE_RECT;
1090             wndpl.rcNormalPosition = *rect;
1091         }
1092         WINPOS_SetPlacement( hwnd, &wndpl, flags );
1093     }
1094 }
1095
1096
1097 /*******************************************************************
1098  *         can_activate_window
1099  *
1100  * Check if we can activate the specified window.
1101  */
1102 static BOOL can_activate_window( HWND hwnd )
1103 {
1104     LONG style;
1105
1106     if (!hwnd) return FALSE;
1107     style = GetWindowLongW( hwnd, GWL_STYLE );
1108     if (!(style & WS_VISIBLE)) return FALSE;
1109     if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1110     return !(style & WS_DISABLED);
1111 }
1112
1113
1114 /*******************************************************************
1115  *         WINPOS_ActivateOtherWindow
1116  *
1117  *  Activates window other than pWnd.
1118  */
1119 void WINPOS_ActivateOtherWindow(HWND hwnd)
1120 {
1121     HWND hwndTo, fg;
1122
1123     if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1124     {
1125         hwndTo = GetAncestor( hwndTo, GA_ROOT );
1126         if (can_activate_window( hwndTo )) goto done;
1127     }
1128
1129     hwndTo = hwnd;
1130     for (;;)
1131     {
1132         if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1133         if (can_activate_window( hwndTo )) break;
1134     }
1135
1136  done:
1137     fg = GetForegroundWindow();
1138     TRACE("win = %p fg = %p\n", hwndTo, fg);
1139     if (!fg || (hwnd == fg))
1140     {
1141         if (SetForegroundWindow( hwndTo )) return;
1142     }
1143     if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1144 }
1145
1146
1147 /***********************************************************************
1148  *           WINPOS_HandleWindowPosChanging
1149  *
1150  * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1151  */
1152 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1153 {
1154     POINT minTrack, maxTrack;
1155     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1156
1157     if (winpos->flags & SWP_NOSIZE) return 0;
1158     if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1159     {
1160         WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1161         if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1162         if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1163         if (!(style & WS_MINIMIZE))
1164         {
1165             if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1166             if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1167         }
1168     }
1169     return 0;
1170 }
1171
1172
1173 /***********************************************************************
1174  *           dump_winpos_flags
1175  */
1176 static void dump_winpos_flags(UINT flags)
1177 {
1178     TRACE("flags:");
1179     if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1180     if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1181     if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1182     if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1183     if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1184     if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1185     if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1186     if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1187     if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1188     if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1189     if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1190     if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1191     if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1192
1193 #define DUMPED_FLAGS \
1194     (SWP_NOSIZE | \
1195     SWP_NOMOVE | \
1196     SWP_NOZORDER | \
1197     SWP_NOREDRAW | \
1198     SWP_NOACTIVATE | \
1199     SWP_FRAMECHANGED | \
1200     SWP_SHOWWINDOW | \
1201     SWP_HIDEWINDOW | \
1202     SWP_NOCOPYBITS | \
1203     SWP_NOOWNERZORDER | \
1204     SWP_NOSENDCHANGING | \
1205     SWP_DEFERERASE | \
1206     SWP_ASYNCWINDOWPOS)
1207
1208     if(flags & ~DUMPED_FLAGS) TRACE(" %08x", flags & ~DUMPED_FLAGS);
1209     TRACE("\n");
1210 #undef DUMPED_FLAGS
1211 }
1212
1213 /***********************************************************************
1214  *           SWP_DoWinPosChanging
1215  */
1216 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
1217 {
1218     WND *wndPtr;
1219
1220     /* Send WM_WINDOWPOSCHANGING message */
1221
1222     if (!(pWinpos->flags & SWP_NOSENDCHANGING))
1223         SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
1224
1225     if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) ||
1226         wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
1227
1228     /* Calculate new position and size */
1229
1230     *pNewWindowRect = wndPtr->rectWindow;
1231     *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
1232                                                     : wndPtr->rectClient;
1233
1234     if (!(pWinpos->flags & SWP_NOSIZE))
1235     {
1236         pNewWindowRect->right  = pNewWindowRect->left + pWinpos->cx;
1237         pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
1238     }
1239     if (!(pWinpos->flags & SWP_NOMOVE))
1240     {
1241         pNewWindowRect->left    = pWinpos->x;
1242         pNewWindowRect->top     = pWinpos->y;
1243         pNewWindowRect->right  += pWinpos->x - wndPtr->rectWindow.left;
1244         pNewWindowRect->bottom += pWinpos->y - wndPtr->rectWindow.top;
1245
1246         OffsetRect( pNewClientRect, pWinpos->x - wndPtr->rectWindow.left,
1247                                     pWinpos->y - wndPtr->rectWindow.top );
1248     }
1249     pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
1250
1251     TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
1252            pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
1253            pWinpos->cx, pWinpos->cy, pWinpos->flags );
1254     TRACE( "current %s style %08x new %s\n",
1255            wine_dbgstr_rect( &wndPtr->rectWindow ), wndPtr->dwStyle,
1256            wine_dbgstr_rect( pNewWindowRect ));
1257
1258     WIN_ReleasePtr( wndPtr );
1259     return TRUE;
1260 }
1261
1262 /***********************************************************************
1263  *           get_valid_rects
1264  *
1265  * Compute the valid rects from the old and new client rect and WVR_* flags.
1266  * Helper for WM_NCCALCSIZE handling.
1267  */
1268 static inline void get_valid_rects( const RECT *old_client, const RECT *new_client, UINT flags,
1269                                     RECT *valid )
1270 {
1271     int cx, cy;
1272
1273     if (flags & WVR_REDRAW)
1274     {
1275         SetRectEmpty( &valid[0] );
1276         SetRectEmpty( &valid[1] );
1277         return;
1278     }
1279
1280     if (flags & WVR_VALIDRECTS)
1281     {
1282         if (!IntersectRect( &valid[0], &valid[0], new_client ) ||
1283             !IntersectRect( &valid[1], &valid[1], old_client ))
1284         {
1285             SetRectEmpty( &valid[0] );
1286             SetRectEmpty( &valid[1] );
1287             return;
1288         }
1289         flags = WVR_ALIGNLEFT | WVR_ALIGNTOP;
1290     }
1291     else
1292     {
1293         valid[0] = *new_client;
1294         valid[1] = *old_client;
1295     }
1296
1297     /* make sure the rectangles have the same size */
1298     cx = min( valid[0].right - valid[0].left, valid[1].right - valid[1].left );
1299     cy = min( valid[0].bottom - valid[0].top, valid[1].bottom - valid[1].top );
1300
1301     if (flags & WVR_ALIGNBOTTOM)
1302     {
1303         valid[0].top = valid[0].bottom - cy;
1304         valid[1].top = valid[1].bottom - cy;
1305     }
1306     else
1307     {
1308         valid[0].bottom = valid[0].top + cy;
1309         valid[1].bottom = valid[1].top + cy;
1310     }
1311     if (flags & WVR_ALIGNRIGHT)
1312     {
1313         valid[0].left = valid[0].right - cx;
1314         valid[1].left = valid[1].right - cx;
1315     }
1316     else
1317     {
1318         valid[0].right = valid[0].left + cx;
1319         valid[1].right = valid[1].left + cx;
1320     }
1321 }
1322
1323 struct move_owned_info
1324 {
1325     HWND owner;
1326     HWND insert_after;
1327 };
1328
1329 static BOOL CALLBACK move_owned_popups( HWND hwnd, LPARAM lparam )
1330 {
1331     struct move_owned_info *info = (struct move_owned_info *)lparam;
1332
1333     if (hwnd == info->owner) return FALSE;
1334     if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) &&
1335         GetWindow( hwnd, GW_OWNER ) == info->owner)
1336     {
1337         SetWindowPos( hwnd, info->insert_after, 0, 0, 0, 0,
1338                       SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE |
1339                       SWP_NOSENDCHANGING | SWP_DEFERERASE );
1340         info->insert_after = hwnd;
1341     }
1342     return TRUE;
1343 }
1344
1345 /***********************************************************************
1346  *           SWP_DoOwnedPopups
1347  *
1348  * fix Z order taking into account owned popups -
1349  * basically we need to maintain them above the window that owns them
1350  *
1351  * FIXME: hide/show owned popups when owner visibility changes.
1352  */
1353 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
1354 {
1355     HWND owner = GetWindow( hwnd, GW_OWNER );
1356     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1357     struct move_owned_info info;
1358
1359     TRACE("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
1360
1361     if ((style & WS_POPUP) && owner)
1362     {
1363         /* make sure this popup stays above the owner */
1364
1365         if( hwndInsertAfter != HWND_TOP )
1366         {
1367             HWND hwndLocalPrev = HWND_TOP;
1368             HWND prev = GetWindow( owner, GW_HWNDPREV );
1369
1370             while (prev && prev != hwndInsertAfter)
1371             {
1372                 if (hwndLocalPrev == HWND_TOP && GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE)
1373                     hwndLocalPrev = prev;
1374                 prev = GetWindow( prev, GW_HWNDPREV );
1375             }
1376             if (!prev) hwndInsertAfter = hwndLocalPrev;
1377         }
1378     }
1379     else if (style & WS_CHILD) return hwndInsertAfter;
1380
1381     info.owner = hwnd;
1382     info.insert_after = hwndInsertAfter;
1383     EnumWindows( move_owned_popups, (LPARAM)&info );
1384     return info.insert_after;
1385 }
1386
1387 /***********************************************************************
1388  *           SWP_DoNCCalcSize
1389  */
1390 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect,
1391                               RECT *validRects )
1392 {
1393     UINT wvrFlags = 0;
1394     WND *wndPtr;
1395
1396     if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
1397
1398       /* Send WM_NCCALCSIZE message to get new client area */
1399     if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
1400     {
1401         NCCALCSIZE_PARAMS params;
1402         WINDOWPOS winposCopy;
1403
1404         params.rgrc[0] = *pNewWindowRect;
1405         params.rgrc[1] = wndPtr->rectWindow;
1406         params.rgrc[2] = wndPtr->rectClient;
1407         params.lppos = &winposCopy;
1408         winposCopy = *pWinpos;
1409         WIN_ReleasePtr( wndPtr );
1410
1411         wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
1412
1413         *pNewClientRect = params.rgrc[0];
1414
1415         if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
1416
1417         TRACE( "hwnd %p old win %s old client %s new win %s new client %s\n", pWinpos->hwnd,
1418                wine_dbgstr_rect(&wndPtr->rectWindow), wine_dbgstr_rect(&wndPtr->rectClient),
1419                wine_dbgstr_rect(pNewWindowRect), wine_dbgstr_rect(pNewClientRect) );
1420
1421         if( pNewClientRect->left != wndPtr->rectClient.left ||
1422             pNewClientRect->top != wndPtr->rectClient.top )
1423             pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1424
1425         if( (pNewClientRect->right - pNewClientRect->left !=
1426              wndPtr->rectClient.right - wndPtr->rectClient.left))
1427             pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1428         else
1429             wvrFlags &= ~WVR_HREDRAW;
1430
1431         if (pNewClientRect->bottom - pNewClientRect->top !=
1432              wndPtr->rectClient.bottom - wndPtr->rectClient.top)
1433             pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1434         else
1435             wvrFlags &= ~WVR_VREDRAW;
1436
1437         validRects[0] = params.rgrc[1];
1438         validRects[1] = params.rgrc[2];
1439     }
1440     else
1441     {
1442         if (!(pWinpos->flags & SWP_NOMOVE) &&
1443             (pNewClientRect->left != wndPtr->rectClient.left ||
1444              pNewClientRect->top != wndPtr->rectClient.top))
1445             pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1446     }
1447
1448     if (pWinpos->flags & (SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
1449     {
1450         SetRectEmpty( &validRects[0] );
1451         SetRectEmpty( &validRects[1] );
1452     }
1453     else get_valid_rects( &wndPtr->rectClient, pNewClientRect, wvrFlags, validRects );
1454
1455     WIN_ReleasePtr( wndPtr );
1456     return wvrFlags;
1457 }
1458
1459 /* fix redundant flags and values in the WINDOWPOS structure */
1460 static BOOL fixup_flags( WINDOWPOS *winpos )
1461 {
1462     HWND parent;
1463     WND *wndPtr = WIN_GetPtr( winpos->hwnd );
1464     BOOL ret = TRUE;
1465
1466     if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
1467     {
1468         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1469         return FALSE;
1470     }
1471     winpos->hwnd = wndPtr->hwndSelf;  /* make it a full handle */
1472
1473     /* Finally make sure that all coordinates are valid */
1474     if (winpos->x < -32768) winpos->x = -32768;
1475     else if (winpos->x > 32767) winpos->x = 32767;
1476     if (winpos->y < -32768) winpos->y = -32768;
1477     else if (winpos->y > 32767) winpos->y = 32767;
1478
1479     if (winpos->cx < 0) winpos->cx = 0;
1480     else if (winpos->cx > 32767) winpos->cx = 32767;
1481     if (winpos->cy < 0) winpos->cy = 0;
1482     else if (winpos->cy > 32767) winpos->cy = 32767;
1483
1484     parent = GetAncestor( winpos->hwnd, GA_PARENT );
1485     if (!IsWindowVisible( parent )) winpos->flags |= SWP_NOREDRAW;
1486
1487     if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
1488     else
1489     {
1490         winpos->flags &= ~SWP_HIDEWINDOW;
1491         if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
1492     }
1493
1494     if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
1495         (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
1496         winpos->flags |= SWP_NOSIZE;    /* Already the right size */
1497
1498     if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
1499         winpos->flags |= SWP_NOMOVE;    /* Already the right position */
1500
1501     if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
1502     {
1503         if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) /* Bring to the top when activating */
1504         {
1505             winpos->flags &= ~SWP_NOZORDER;
1506             winpos->hwndInsertAfter = HWND_TOP;
1507         }
1508     }
1509
1510     /* Check hwndInsertAfter */
1511     if (winpos->flags & SWP_NOZORDER) goto done;
1512
1513     /* fix sign extension */
1514     if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
1515     else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
1516
1517       /* FIXME: TOPMOST not supported yet */
1518     if ((winpos->hwndInsertAfter == HWND_TOPMOST) ||
1519         (winpos->hwndInsertAfter == HWND_NOTOPMOST)) winpos->hwndInsertAfter = HWND_TOP;
1520
1521     /* hwndInsertAfter must be a sibling of the window */
1522     if (winpos->hwndInsertAfter == HWND_TOP)
1523     {
1524         if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1525             winpos->flags |= SWP_NOZORDER;
1526     }
1527     else if (winpos->hwndInsertAfter == HWND_BOTTOM)
1528     {
1529         if (GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
1530             winpos->flags |= SWP_NOZORDER;
1531     }
1532     else
1533     {
1534         if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != parent) ret = FALSE;
1535         else
1536         {
1537             /* don't need to change the Zorder of hwnd if it's already inserted
1538              * after hwndInsertAfter or when inserting hwnd after itself.
1539              */
1540             if ((winpos->hwnd == winpos->hwndInsertAfter) ||
1541                 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
1542                 winpos->flags |= SWP_NOZORDER;
1543         }
1544     }
1545  done:
1546     WIN_ReleasePtr( wndPtr );
1547     return ret;
1548 }
1549
1550 /***********************************************************************
1551  *              USER_SetWindowPos
1552  *
1553  *     User32 internal function
1554  */
1555 BOOL USER_SetWindowPos( WINDOWPOS * winpos )
1556 {
1557     RECT newWindowRect, newClientRect, valid_rects[2];
1558     UINT orig_flags;
1559     
1560     orig_flags = winpos->flags;
1561     
1562     /* First make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
1563     if (!(winpos->flags & SWP_NOMOVE))
1564     {
1565         if (winpos->x < -32768) winpos->x = -32768;
1566         else if (winpos->x > 32767) winpos->x = 32767;
1567         if (winpos->y < -32768) winpos->y = -32768;
1568         else if (winpos->y > 32767) winpos->y = 32767;
1569     }
1570     if (!(winpos->flags & SWP_NOSIZE))
1571     {
1572         if (winpos->cx < 0) winpos->cx = 0;
1573         else if (winpos->cx > 32767) winpos->cx = 32767;
1574         if (winpos->cy < 0) winpos->cy = 0;
1575         else if (winpos->cy > 32767) winpos->cy = 32767;
1576     }
1577
1578     if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
1579
1580     /* Fix redundant flags */
1581     if (!fixup_flags( winpos )) return FALSE;
1582
1583     if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
1584     {
1585         if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
1586             winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
1587     }
1588
1589     /* Common operations */
1590
1591     SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect, valid_rects );
1592
1593     if(!USER_Driver->pSetWindowPos( winpos->hwnd, winpos->hwndInsertAfter, 
1594                             &newWindowRect, &newClientRect, orig_flags, valid_rects ))
1595         return FALSE;
1596
1597     if (!(orig_flags & SWP_SHOWWINDOW))
1598     {
1599         UINT rdw_flags = RDW_FRAME | RDW_ERASE;
1600         if ( !(orig_flags & SWP_DEFERERASE) ) rdw_flags |= RDW_ERASENOW;
1601         RedrawWindow( winpos->hwnd, NULL, NULL, rdw_flags );
1602     }
1603
1604     if( winpos->flags & SWP_HIDEWINDOW )
1605         HideCaret(winpos->hwnd);
1606     else if (winpos->flags & SWP_SHOWWINDOW)
1607         ShowCaret(winpos->hwnd);
1608
1609     if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)))
1610     {
1611         /* child windows get WM_CHILDACTIVATE message */
1612         if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
1613             SendMessageW( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
1614         else
1615             SetForegroundWindow( winpos->hwnd );
1616     }
1617
1618       /* And last, send the WM_WINDOWPOSCHANGED message */
1619
1620     TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
1621
1622     if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
1623     {
1624         /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
1625            and always contains final window position.
1626          */
1627         winpos->x = newWindowRect.left;
1628         winpos->y = newWindowRect.top;
1629         winpos->cx = newWindowRect.right - newWindowRect.left;
1630         winpos->cy = newWindowRect.bottom - newWindowRect.top;
1631         SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
1632     }
1633     return TRUE;
1634 }
1635
1636 /***********************************************************************
1637  *              SetWindowPos (USER32.@)
1638  */
1639 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
1640                           INT x, INT y, INT cx, INT cy, UINT flags )
1641 {
1642     WINDOWPOS winpos;
1643
1644     TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1645           hwnd, hwndInsertAfter, x, y, cx, cy, flags);
1646     if(TRACE_ON(win)) dump_winpos_flags(flags);
1647
1648     if (is_broadcast(hwnd))
1649     {
1650         SetLastError( ERROR_INVALID_PARAMETER );
1651         return FALSE;
1652     }
1653
1654     winpos.hwnd = WIN_GetFullHandle(hwnd);
1655     winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
1656     winpos.x = x;
1657     winpos.y = y;
1658     winpos.cx = cx;
1659     winpos.cy = cy;
1660     winpos.flags = flags;
1661     
1662     if (WIN_IsCurrentThread( hwnd ))
1663         return USER_SetWindowPos(&winpos);
1664
1665     return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
1666 }
1667
1668
1669 /***********************************************************************
1670  *              BeginDeferWindowPos (USER32.@)
1671  */
1672 HDWP WINAPI BeginDeferWindowPos( INT count )
1673 {
1674     HDWP handle;
1675     DWP *pDWP;
1676
1677     TRACE("%d\n", count);
1678
1679     if (count < 0)
1680     {
1681         SetLastError(ERROR_INVALID_PARAMETER);
1682         return 0;
1683     }
1684     /* Windows allows zero count, in which case it allocates context for 8 moves */
1685     if (count == 0) count = 8;
1686
1687     handle = USER_HEAP_ALLOC( sizeof(DWP) + (count-1)*sizeof(WINDOWPOS) );
1688     if (!handle) return 0;
1689     pDWP = (DWP *) USER_HEAP_LIN_ADDR( handle );
1690     pDWP->actualCount    = 0;
1691     pDWP->suggestedCount = count;
1692     pDWP->valid          = TRUE;
1693     pDWP->wMagic         = DWP_MAGIC;
1694     pDWP->hwndParent     = 0;
1695
1696     TRACE("returning hdwp %p\n", handle);
1697     return handle;
1698 }
1699
1700
1701 /***********************************************************************
1702  *              DeferWindowPos (USER32.@)
1703  */
1704 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
1705                                 INT x, INT y, INT cx, INT cy,
1706                                 UINT flags )
1707 {
1708     DWP *pDWP;
1709     int i;
1710     HDWP newhdwp = hdwp,retvalue;
1711
1712     TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1713           hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
1714
1715     hwnd = WIN_GetFullHandle( hwnd );
1716     if (hwnd == GetDesktopWindow()) return 0;
1717
1718     if (!(pDWP = USER_HEAP_LIN_ADDR( hdwp ))) return 0;
1719
1720     USER_Lock();
1721
1722     for (i = 0; i < pDWP->actualCount; i++)
1723     {
1724         if (pDWP->winPos[i].hwnd == hwnd)
1725         {
1726               /* Merge with the other changes */
1727             if (!(flags & SWP_NOZORDER))
1728             {
1729                 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
1730             }
1731             if (!(flags & SWP_NOMOVE))
1732             {
1733                 pDWP->winPos[i].x = x;
1734                 pDWP->winPos[i].y = y;
1735             }
1736             if (!(flags & SWP_NOSIZE))
1737             {
1738                 pDWP->winPos[i].cx = cx;
1739                 pDWP->winPos[i].cy = cy;
1740             }
1741             pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
1742                                                SWP_NOZORDER | SWP_NOREDRAW |
1743                                                SWP_NOACTIVATE | SWP_NOCOPYBITS|
1744                                                SWP_NOOWNERZORDER);
1745             pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
1746                                               SWP_FRAMECHANGED);
1747             retvalue = hdwp;
1748             goto END;
1749         }
1750     }
1751     if (pDWP->actualCount >= pDWP->suggestedCount)
1752     {
1753         newhdwp = USER_HEAP_REALLOC( hdwp,
1754                       sizeof(DWP) + pDWP->suggestedCount*sizeof(WINDOWPOS) );
1755         if (!newhdwp)
1756         {
1757             retvalue = 0;
1758             goto END;
1759         }
1760         pDWP = (DWP *) USER_HEAP_LIN_ADDR( newhdwp );
1761         pDWP->suggestedCount++;
1762     }
1763     pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
1764     pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
1765     pDWP->winPos[pDWP->actualCount].x = x;
1766     pDWP->winPos[pDWP->actualCount].y = y;
1767     pDWP->winPos[pDWP->actualCount].cx = cx;
1768     pDWP->winPos[pDWP->actualCount].cy = cy;
1769     pDWP->winPos[pDWP->actualCount].flags = flags;
1770     pDWP->actualCount++;
1771     retvalue = newhdwp;
1772 END:
1773     USER_Unlock();
1774     return retvalue;
1775 }
1776
1777
1778 /***********************************************************************
1779  *              EndDeferWindowPos (USER32.@)
1780  */
1781 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
1782 {
1783     DWP *pDWP;
1784     WINDOWPOS *winpos;
1785     BOOL res = TRUE;
1786     int i;
1787
1788     TRACE("%p\n", hdwp);
1789
1790     pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
1791     if (!pDWP) return FALSE;
1792     for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
1793     {
1794         TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1795                winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
1796                winpos->cx, winpos->cy, winpos->flags);
1797
1798         if (!(res = USER_SetWindowPos( winpos ))) break;
1799     }
1800     USER_HEAP_FREE( hdwp );
1801     return res;
1802 }
1803
1804
1805 /***********************************************************************
1806  *              TileChildWindows (USER.199)
1807  */
1808 void WINAPI TileChildWindows16( HWND16 parent, WORD action )
1809 {
1810     FIXME("(%04x, %d): stub\n", parent, action);
1811 }
1812
1813 /***********************************************************************
1814  *              CascadeChildWindows (USER.198)
1815  */
1816 void WINAPI CascadeChildWindows16( HWND16 parent, WORD action )
1817 {
1818     FIXME("(%04x, %d): stub\n", parent, action);
1819 }