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