shdocvw: The default value for the "CLSID\%CLSID_Internet" key isn't set on Windows...
[wine] / dlls / user32 / winpos.c
1 /*
2  * Window position related functions.
3  *
4  * Copyright 1993, 1994, 1995 Alexandre Julliard
5  *                       1995, 1996, 1999 Alex Korobka
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <stdarg.h>
26 #include <string.h>
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winerror.h"
33 #include "wine/server.h"
34 #include "controls.h"
35 #include "user_private.h"
36 #include "win.h"
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(win);
40
41 #define SWP_AGG_NOGEOMETRYCHANGE \
42     (SWP_NOSIZE | SWP_NOCLIENTSIZE | SWP_NOZORDER)
43 #define SWP_AGG_NOPOSCHANGE \
44     (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
45 #define SWP_AGG_STATUSFLAGS \
46     (SWP_AGG_NOPOSCHANGE | SWP_FRAMECHANGED | SWP_HIDEWINDOW | SWP_SHOWWINDOW)
47
48 #define HAS_DLGFRAME(style,exStyle) \
49     (((exStyle) & WS_EX_DLGMODALFRAME) || \
50      (((style) & WS_DLGFRAME) && !((style) & WS_BORDER)))
51
52 #define HAS_THICKFRAME(style) \
53     (((style) & WS_THICKFRAME) && \
54      !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
55
56 #define EMPTYPOINT(pt)          ((*(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 (%s)\n", hwnd, wine_dbgstr_rect(rect) );
174     }
175     return ret;
176 }
177
178
179 /***********************************************************************
180  *              GetWindowRgn (USER32.@)
181  */
182 int WINAPI GetWindowRgn ( HWND hwnd, HRGN hrgn )
183 {
184     int nRet = ERROR;
185     NTSTATUS status;
186     HRGN win_rgn = 0;
187     RGNDATA *data;
188     size_t size = 256;
189
190     do
191     {
192         if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
193         {
194             SetLastError( ERROR_OUTOFMEMORY );
195             return ERROR;
196         }
197         SERVER_START_REQ( get_window_region )
198         {
199             req->window = hwnd;
200             wine_server_set_reply( req, data->Buffer, size );
201             if (!(status = wine_server_call( req )))
202             {
203                 size_t reply_size = wine_server_reply_size( reply );
204                 if (reply_size)
205                 {
206                     data->rdh.dwSize   = sizeof(data->rdh);
207                     data->rdh.iType    = RDH_RECTANGLES;
208                     data->rdh.nCount   = reply_size / sizeof(RECT);
209                     data->rdh.nRgnSize = reply_size;
210                     win_rgn = ExtCreateRegion( NULL, size, data );
211                 }
212             }
213             else size = reply->total_size;
214         }
215         SERVER_END_REQ;
216         HeapFree( GetProcessHeap(), 0, data );
217     } while (status == STATUS_BUFFER_OVERFLOW);
218
219     if (status) SetLastError( RtlNtStatusToDosError(status) );
220     else if (win_rgn)
221     {
222         nRet = CombineRgn( hrgn, win_rgn, 0, RGN_COPY );
223         DeleteObject( win_rgn );
224     }
225     return nRet;
226 }
227
228
229 /***********************************************************************
230  *              SetWindowRgn (USER32.@)
231  */
232 int WINAPI SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL bRedraw )
233 {
234     static const RECT empty_rect;
235     BOOL ret;
236
237     if (hrgn)
238     {
239         RGNDATA *data;
240         DWORD size;
241
242         if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
243         if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
244         if (!GetRegionData( hrgn, size, data ))
245         {
246             HeapFree( GetProcessHeap(), 0, data );
247             return FALSE;
248         }
249         SERVER_START_REQ( set_window_region )
250         {
251             req->window = hwnd;
252             req->redraw = (bRedraw != 0);
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             req->redraw = (bRedraw != 0);
267             ret = !wine_server_call_err( req );
268         }
269         SERVER_END_REQ;
270     }
271
272     if (ret) ret = USER_Driver->pSetWindowRgn( hwnd, hrgn, bRedraw );
273
274     if (ret)
275     {
276         UINT swp_flags = SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_FRAMECHANGED|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  *              RealChildWindowFromPoint (USER32.@)
435  */
436 HWND WINAPI RealChildWindowFromPoint( HWND hwndParent, POINT pt )
437 {
438     return ChildWindowFromPointEx( hwndParent, pt, CWP_SKIPTRANSPARENT );
439 }
440
441 /*******************************************************************
442  *              ChildWindowFromPointEx (USER32.@)
443  */
444 HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
445 {
446     /* pt is in the client coordinates */
447     HWND *list;
448     int i;
449     RECT rect;
450     HWND retvalue;
451
452     GetClientRect( hwndParent, &rect );
453     if (!PtInRect( &rect, pt )) return 0;
454     if (!(list = WIN_ListChildren( hwndParent ))) return hwndParent;
455
456     for (i = 0; list[i]; i++)
457     {
458         if (!WIN_GetRectangles( list[i], &rect, NULL )) continue;
459         if (!PtInRect( &rect, pt )) continue;
460         if (uFlags & (CWP_SKIPINVISIBLE|CWP_SKIPDISABLED))
461         {
462             LONG style = GetWindowLongW( list[i], GWL_STYLE );
463             if ((uFlags & CWP_SKIPINVISIBLE) && !(style & WS_VISIBLE)) continue;
464             if ((uFlags & CWP_SKIPDISABLED) && (style & WS_DISABLED)) continue;
465         }
466         if (uFlags & CWP_SKIPTRANSPARENT)
467         {
468             if (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TRANSPARENT) continue;
469         }
470         break;
471     }
472     retvalue = list[i];
473     HeapFree( GetProcessHeap(), 0, list );
474     if (!retvalue) retvalue = hwndParent;
475     return retvalue;
476 }
477
478
479 /*******************************************************************
480  *         WINPOS_GetWinOffset
481  *
482  * Calculate the offset between the origin of the two windows. Used
483  * to implement MapWindowPoints.
484  */
485 static void WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo, POINT *offset )
486 {
487     WND * wndPtr;
488
489     offset->x = offset->y = 0;
490
491     /* Translate source window origin to screen coords */
492     if (hwndFrom)
493     {
494         HWND hwnd = hwndFrom;
495
496         while (hwnd)
497         {
498             if (hwnd == hwndTo) return;
499             if (!(wndPtr = WIN_GetPtr( hwnd )))
500             {
501                 ERR( "bad hwndFrom = %p\n", hwnd );
502                 return;
503             }
504             if (wndPtr == WND_DESKTOP) break;
505             if (wndPtr == WND_OTHER_PROCESS) goto other_process;
506             offset->x += wndPtr->rectClient.left;
507             offset->y += wndPtr->rectClient.top;
508             hwnd = wndPtr->parent;
509             WIN_ReleasePtr( wndPtr );
510         }
511     }
512
513     /* Translate origin to destination window coords */
514     if (hwndTo)
515     {
516         HWND hwnd = hwndTo;
517
518         while (hwnd)
519         {
520             if (!(wndPtr = WIN_GetPtr( hwnd )))
521             {
522                 ERR( "bad hwndTo = %p\n", hwnd );
523                 return;
524             }
525             if (wndPtr == WND_DESKTOP) break;
526             if (wndPtr == WND_OTHER_PROCESS) goto other_process;
527             offset->x -= wndPtr->rectClient.left;
528             offset->y -= wndPtr->rectClient.top;
529             hwnd = wndPtr->parent;
530             WIN_ReleasePtr( wndPtr );
531         }
532     }
533     return;
534
535  other_process:  /* one of the parents may belong to another process, do it the hard way */
536     offset->x = offset->y = 0;
537     SERVER_START_REQ( get_windows_offset )
538     {
539         req->from = hwndFrom;
540         req->to   = hwndTo;
541         if (!wine_server_call( req ))
542         {
543             offset->x = reply->x;
544             offset->y = reply->y;
545         }
546     }
547     SERVER_END_REQ;
548 }
549
550
551 /*******************************************************************
552  *              MapWindowPoints (USER.258)
553  */
554 void WINAPI MapWindowPoints16( HWND16 hwndFrom, HWND16 hwndTo,
555                                LPPOINT16 lppt, UINT16 count )
556 {
557     POINT offset;
558
559     WINPOS_GetWinOffset( WIN_Handle32(hwndFrom), WIN_Handle32(hwndTo), &offset );
560     while (count--)
561     {
562         lppt->x += offset.x;
563         lppt->y += offset.y;
564         lppt++;
565     }
566 }
567
568
569 /*******************************************************************
570  *              MapWindowPoints (USER32.@)
571  */
572 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
573 {
574     POINT offset;
575
576     WINPOS_GetWinOffset( hwndFrom, hwndTo, &offset );
577     while (count--)
578     {
579         lppt->x += offset.x;
580         lppt->y += offset.y;
581         lppt++;
582     }
583     return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
584 }
585
586
587 /***********************************************************************
588  *              IsIconic (USER32.@)
589  */
590 BOOL WINAPI IsIconic(HWND hWnd)
591 {
592     return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
593 }
594
595
596 /***********************************************************************
597  *              IsZoomed (USER32.@)
598  */
599 BOOL WINAPI IsZoomed(HWND hWnd)
600 {
601     return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
602 }
603
604
605 /*******************************************************************
606  *              AllowSetForegroundWindow (USER32.@)
607  */
608 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
609 {
610     /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
611      * implemented, then fix this function. */
612     return TRUE;
613 }
614
615
616 /*******************************************************************
617  *              LockSetForegroundWindow (USER32.@)
618  */
619 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
620 {
621     /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
622      * implemented, then fix this function. */
623     return TRUE;
624 }
625
626
627 /***********************************************************************
628  *              BringWindowToTop (USER32.@)
629  */
630 BOOL WINAPI BringWindowToTop( HWND hwnd )
631 {
632     return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
633 }
634
635
636 /***********************************************************************
637  *              MoveWindow (USER32.@)
638  */
639 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
640                             BOOL repaint )
641 {
642     int flags = SWP_NOZORDER | SWP_NOACTIVATE;
643     if (!repaint) flags |= SWP_NOREDRAW;
644     TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
645     return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
646 }
647
648 /***********************************************************************
649  *           WINPOS_InitInternalPos
650  */
651 static LPINTERNALPOS WINPOS_InitInternalPos( WND* wnd )
652 {
653     LPINTERNALPOS lpPos = get_internal_pos( wnd->hwndSelf );
654     if( !lpPos )
655     {
656         /* this happens when the window is minimized/maximized
657          * for the first time (rectWindow is not adjusted yet) */
658
659         lpPos = HeapAlloc( GetProcessHeap(), 0, sizeof(INTERNALPOS) );
660         if( !lpPos ) return NULL;
661
662         set_internal_pos( wnd->hwndSelf, lpPos );
663         lpPos->hwndIconTitle = 0; /* defer until needs to be shown */
664         lpPos->rectNormal.left   = wnd->rectWindow.left;
665         lpPos->rectNormal.top    = wnd->rectWindow.top;
666         lpPos->rectNormal.right  = wnd->rectWindow.right;
667         lpPos->rectNormal.bottom = wnd->rectWindow.bottom;
668         lpPos->ptIconPos.x = lpPos->ptIconPos.y = -1;
669         lpPos->ptMaxPos.x = lpPos->ptMaxPos.y = -1;
670     }
671
672     if( wnd->dwStyle & WS_MINIMIZE )
673     {
674         lpPos->ptIconPos.x = wnd->rectWindow.left;
675         lpPos->ptIconPos.y = wnd->rectWindow.top;
676     }
677     else if( wnd->dwStyle & WS_MAXIMIZE )
678     {
679         lpPos->ptMaxPos.x = wnd->rectWindow.left;
680         lpPos->ptMaxPos.y = wnd->rectWindow.top;
681     }
682     else
683     {
684         lpPos->rectNormal.left   = wnd->rectWindow.left;
685         lpPos->rectNormal.top    = wnd->rectWindow.top;
686         lpPos->rectNormal.right  = wnd->rectWindow.right;
687         lpPos->rectNormal.bottom = wnd->rectWindow.bottom;
688     }
689     return lpPos;
690 }
691
692 /***********************************************************************
693  *           WINPOS_RedrawIconTitle
694  */
695 BOOL WINPOS_RedrawIconTitle( HWND hWnd )
696 {
697     LPINTERNALPOS lpPos = get_internal_pos( hWnd );
698     if( lpPos )
699     {
700         if( lpPos->hwndIconTitle )
701         {
702             SendMessageW( lpPos->hwndIconTitle, WM_SHOWWINDOW, TRUE, 0);
703             InvalidateRect( lpPos->hwndIconTitle, NULL, TRUE );
704             return TRUE;
705         }
706     }
707     return FALSE;
708 }
709
710 /***********************************************************************
711  *           WINPOS_ShowIconTitle
712  */
713 BOOL WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
714 {
715     LPINTERNALPOS lpPos = get_internal_pos( hwnd );
716
717     if (lpPos && !GetPropA( hwnd, "__wine_x11_managed" ))
718     {
719         HWND title = lpPos->hwndIconTitle;
720
721         TRACE("%p %i\n", hwnd, (bShow != 0) );
722
723         if( !title )
724             lpPos->hwndIconTitle = title = ICONTITLE_Create( hwnd );
725         if( bShow )
726         {
727             if (!IsWindowVisible(title))
728             {
729                 SendMessageW( title, WM_SHOWWINDOW, TRUE, 0 );
730                 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
731                               SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
732             }
733         }
734         else ShowWindow( title, SW_HIDE );
735     }
736     return FALSE;
737 }
738
739 /*******************************************************************
740  *           WINPOS_GetMinMaxInfo
741  *
742  * Get the minimized and maximized information for a window.
743  */
744 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
745                            POINT *minTrack, POINT *maxTrack )
746 {
747     LPINTERNALPOS lpPos;
748     MINMAXINFO MinMax;
749     HMONITOR monitor;
750     INT xinc, yinc;
751     LONG style = GetWindowLongA( hwnd, GWL_STYLE );
752     LONG exstyle = GetWindowLongA( hwnd, GWL_EXSTYLE );
753     RECT rc;
754
755     /* Compute default values */
756
757     GetWindowRect(hwnd, &rc);
758     MinMax.ptReserved.x = rc.left;
759     MinMax.ptReserved.y = rc.top;
760
761     if (style & WS_CHILD)
762     {
763         if ((style & WS_CAPTION) == WS_CAPTION)
764             style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
765
766         GetClientRect(GetAncestor(hwnd,GA_PARENT), &rc);
767         AdjustWindowRectEx(&rc, style, ((style & WS_POPUP) && GetMenu(hwnd)), exstyle);
768
769         /* avoid calculating this twice */
770         style &= ~(WS_DLGFRAME | WS_BORDER | WS_THICKFRAME);
771
772         MinMax.ptMaxSize.x = rc.right - rc.left;
773         MinMax.ptMaxSize.y = rc.bottom - rc.top;
774     }
775     else
776     {
777         MinMax.ptMaxSize.x = GetSystemMetrics(SM_CXSCREEN);
778         MinMax.ptMaxSize.y = GetSystemMetrics(SM_CYSCREEN);
779     }
780     MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
781     MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
782     MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXMAXTRACK);
783     MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYMAXTRACK);
784
785     if (HAS_DLGFRAME( style, exstyle ))
786     {
787         xinc = GetSystemMetrics(SM_CXDLGFRAME);
788         yinc = GetSystemMetrics(SM_CYDLGFRAME);
789     }
790     else
791     {
792         xinc = yinc = 0;
793         if (HAS_THICKFRAME(style))
794         {
795             xinc += GetSystemMetrics(SM_CXFRAME);
796             yinc += GetSystemMetrics(SM_CYFRAME);
797         }
798         if (style & WS_BORDER)
799         {
800             xinc += GetSystemMetrics(SM_CXBORDER);
801             yinc += GetSystemMetrics(SM_CYBORDER);
802         }
803     }
804     MinMax.ptMaxSize.x += 2 * xinc;
805     MinMax.ptMaxSize.y += 2 * yinc;
806
807     lpPos = get_internal_pos( hwnd );
808     if( lpPos && !EMPTYPOINT(lpPos->ptMaxPos) )
809     {
810         MinMax.ptMaxPosition.x = lpPos->ptMaxPos.x;
811         MinMax.ptMaxPosition.y = lpPos->ptMaxPos.y;
812     }
813     else
814     {
815         MinMax.ptMaxPosition.x = -xinc;
816         MinMax.ptMaxPosition.y = -yinc;
817     }
818
819     SendMessageW( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
820
821     /* if the app didn't change the values, adapt them for the current monitor */
822
823     if ((monitor = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY )))
824     {
825         MONITORINFO mon_info;
826
827         mon_info.cbSize = sizeof(mon_info);
828         GetMonitorInfoW( monitor, &mon_info );
829
830         if (MinMax.ptMaxSize.x == GetSystemMetrics(SM_CXSCREEN) + 2 * xinc &&
831             MinMax.ptMaxSize.y == GetSystemMetrics(SM_CYSCREEN) + 2 * yinc)
832         {
833             MinMax.ptMaxSize.x = (mon_info.rcWork.right - mon_info.rcWork.left) + 2 * xinc;
834             MinMax.ptMaxSize.y = (mon_info.rcWork.bottom - mon_info.rcWork.top) + 2 * yinc;
835         }
836         if (MinMax.ptMaxPosition.x == -xinc && MinMax.ptMaxPosition.y == -yinc)
837         {
838             MinMax.ptMaxPosition.x = mon_info.rcWork.left - xinc;
839             MinMax.ptMaxPosition.y = mon_info.rcWork.top - yinc;
840         }
841     }
842
843       /* Some sanity checks */
844
845     TRACE("%d %d / %d %d / %d %d / %d %d\n",
846                       MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
847                       MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
848                       MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
849                       MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
850     MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
851                                    MinMax.ptMinTrackSize.x );
852     MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
853                                    MinMax.ptMinTrackSize.y );
854
855     if (maxSize) *maxSize = MinMax.ptMaxSize;
856     if (maxPos) *maxPos = MinMax.ptMaxPosition;
857     if (minTrack) *minTrack = MinMax.ptMinTrackSize;
858     if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
859 }
860
861 /***********************************************************************
862  *              ShowWindowAsync (USER32.@)
863  *
864  * doesn't wait; returns immediately.
865  * used by threads to toggle windows in other (possibly hanging) threads
866  */
867 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
868 {
869     HWND full_handle;
870
871     if (is_broadcast(hwnd))
872     {
873         SetLastError( ERROR_INVALID_PARAMETER );
874         return FALSE;
875     }
876
877     if ((full_handle = WIN_IsCurrentThread( hwnd )))
878         return USER_Driver->pShowWindow( full_handle, cmd );
879
880     return SendNotifyMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
881 }
882
883
884 /***********************************************************************
885  *              ShowWindow (USER32.@)
886  */
887 BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
888 {
889     HWND full_handle;
890
891     if (is_broadcast(hwnd))
892     {
893         SetLastError( ERROR_INVALID_PARAMETER );
894         return FALSE;
895     }
896     if ((full_handle = WIN_IsCurrentThread( hwnd )))
897         return USER_Driver->pShowWindow( full_handle, cmd );
898
899     return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
900 }
901
902
903 /***********************************************************************
904  *              GetInternalWindowPos (USER32.@)
905  */
906 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
907                                       LPPOINT ptIcon )
908 {
909     WINDOWPLACEMENT wndpl;
910     if (GetWindowPlacement( hwnd, &wndpl ))
911     {
912         if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
913         if (ptIcon)  *ptIcon = wndpl.ptMinPosition;
914         return wndpl.showCmd;
915     }
916     return 0;
917 }
918
919
920 /***********************************************************************
921  *              GetWindowPlacement (USER32.@)
922  *
923  * Win95:
924  * Fails if wndpl->length of Win95 (!) apps is invalid.
925  */
926 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
927 {
928     WND *pWnd = WIN_GetPtr( hwnd );
929     LPINTERNALPOS lpPos;
930
931     if (!pWnd || pWnd == WND_DESKTOP) return FALSE;
932     if (pWnd == WND_OTHER_PROCESS)
933     {
934         if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
935         return FALSE;
936     }
937
938     lpPos = WINPOS_InitInternalPos( pWnd );
939     wndpl->length  = sizeof(*wndpl);
940     if( pWnd->dwStyle & WS_MINIMIZE )
941         wndpl->showCmd = SW_SHOWMINIMIZED;
942     else
943         wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
944     if( pWnd->flags & WIN_RESTORE_MAX )
945         wndpl->flags = WPF_RESTORETOMAXIMIZED;
946     else
947         wndpl->flags = 0;
948     wndpl->ptMinPosition.x = lpPos->ptIconPos.x;
949     wndpl->ptMinPosition.y = lpPos->ptIconPos.y;
950     wndpl->ptMaxPosition.x = lpPos->ptMaxPos.x;
951     wndpl->ptMaxPosition.y = lpPos->ptMaxPos.y;
952     wndpl->rcNormalPosition.left   = lpPos->rectNormal.left;
953     wndpl->rcNormalPosition.top    = lpPos->rectNormal.top;
954     wndpl->rcNormalPosition.right  = lpPos->rectNormal.right;
955     wndpl->rcNormalPosition.bottom = lpPos->rectNormal.bottom;
956     WIN_ReleasePtr( pWnd );
957     return TRUE;
958 }
959
960
961 /***********************************************************************
962  *           WINPOS_SetPlacement
963  */
964 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
965 {
966     LPINTERNALPOS lpPos;
967     DWORD style;
968     WND *pWnd = WIN_GetPtr( hwnd );
969
970     if (!pWnd || pWnd == WND_OTHER_PROCESS || pWnd == WND_DESKTOP) return FALSE;
971     lpPos = WINPOS_InitInternalPos( pWnd );
972
973     if( flags & PLACE_MIN )
974     {
975         lpPos->ptIconPos.x = wndpl->ptMinPosition.x;
976         lpPos->ptIconPos.y = wndpl->ptMinPosition.y;
977     }
978     if( flags & PLACE_MAX )
979     {
980         lpPos->ptMaxPos.x = wndpl->ptMaxPosition.x;
981         lpPos->ptMaxPos.y = wndpl->ptMaxPosition.y;
982     }
983     if( flags & PLACE_RECT)
984     {
985         lpPos->rectNormal.left   = wndpl->rcNormalPosition.left;
986         lpPos->rectNormal.top    = wndpl->rcNormalPosition.top;
987         lpPos->rectNormal.right  = wndpl->rcNormalPosition.right;
988         lpPos->rectNormal.bottom = wndpl->rcNormalPosition.bottom;
989     }
990
991     style = pWnd->dwStyle;
992     WIN_ReleasePtr( pWnd );
993
994     if( style & WS_MINIMIZE )
995     {
996         WINPOS_ShowIconTitle( hwnd, FALSE );
997         if( wndpl->flags & WPF_SETMINPOSITION && !EMPTYPOINT(lpPos->ptIconPos))
998             SetWindowPos( hwnd, 0, lpPos->ptIconPos.x, lpPos->ptIconPos.y,
999                           0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1000     }
1001     else if( style & WS_MAXIMIZE )
1002     {
1003         if( !EMPTYPOINT(lpPos->ptMaxPos) )
1004             SetWindowPos( hwnd, 0, lpPos->ptMaxPos.x, lpPos->ptMaxPos.y,
1005                           0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1006     }
1007     else if( flags & PLACE_RECT )
1008         SetWindowPos( hwnd, 0, lpPos->rectNormal.left, lpPos->rectNormal.top,
1009                       lpPos->rectNormal.right - lpPos->rectNormal.left,
1010                       lpPos->rectNormal.bottom - lpPos->rectNormal.top,
1011                       SWP_NOZORDER | SWP_NOACTIVATE );
1012
1013     ShowWindow( hwnd, wndpl->showCmd );
1014
1015     if (IsIconic( hwnd ))
1016     {
1017         if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE) WINPOS_ShowIconTitle( hwnd, TRUE );
1018
1019         /* SDK: ...valid only the next time... */
1020         if( wndpl->flags & WPF_RESTORETOMAXIMIZED )
1021         {
1022             pWnd = WIN_GetPtr( hwnd );
1023             if (pWnd && pWnd != WND_OTHER_PROCESS)
1024             {
1025                 pWnd->flags |= WIN_RESTORE_MAX;
1026                 WIN_ReleasePtr( pWnd );
1027             }
1028         }
1029     }
1030     return TRUE;
1031 }
1032
1033
1034 /***********************************************************************
1035  *              SetWindowPlacement (USER32.@)
1036  *
1037  * Win95:
1038  * Fails if wndpl->length of Win95 (!) apps is invalid.
1039  */
1040 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
1041 {
1042     if (!wpl) return FALSE;
1043     return WINPOS_SetPlacement( hwnd, wpl, PLACE_MIN | PLACE_MAX | PLACE_RECT );
1044 }
1045
1046
1047 /***********************************************************************
1048  *              AnimateWindow (USER32.@)
1049  *              Shows/Hides a window with an animation
1050  *              NO ANIMATION YET
1051  */
1052 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
1053 {
1054         FIXME("partial stub\n");
1055
1056         /* If trying to show/hide and it's already   *
1057          * shown/hidden or invalid window, fail with *
1058          * invalid parameter                         */
1059         if(!IsWindow(hwnd) ||
1060            (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
1061            (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
1062         {
1063                 SetLastError(ERROR_INVALID_PARAMETER);
1064                 return FALSE;
1065         }
1066
1067         ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1068
1069         return TRUE;
1070 }
1071
1072 /***********************************************************************
1073  *              SetInternalWindowPos (USER32.@)
1074  */
1075 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1076                                     LPRECT rect, LPPOINT pt )
1077 {
1078     if( IsWindow(hwnd) )
1079     {
1080         WINDOWPLACEMENT wndpl;
1081         UINT flags;
1082
1083         wndpl.length  = sizeof(wndpl);
1084         wndpl.showCmd = showCmd;
1085         wndpl.flags = flags = 0;
1086
1087         if( pt )
1088         {
1089             flags |= PLACE_MIN;
1090             wndpl.flags |= WPF_SETMINPOSITION;
1091             wndpl.ptMinPosition = *pt;
1092         }
1093         if( rect )
1094         {
1095             flags |= PLACE_RECT;
1096             wndpl.rcNormalPosition = *rect;
1097         }
1098         WINPOS_SetPlacement( hwnd, &wndpl, flags );
1099     }
1100 }
1101
1102
1103 /*******************************************************************
1104  *         can_activate_window
1105  *
1106  * Check if we can activate the specified window.
1107  */
1108 static BOOL can_activate_window( HWND hwnd )
1109 {
1110     LONG style;
1111
1112     if (!hwnd) return FALSE;
1113     style = GetWindowLongW( hwnd, GWL_STYLE );
1114     if (!(style & WS_VISIBLE)) return FALSE;
1115     if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1116     return !(style & WS_DISABLED);
1117 }
1118
1119
1120 /*******************************************************************
1121  *         WINPOS_ActivateOtherWindow
1122  *
1123  *  Activates window other than pWnd.
1124  */
1125 void WINPOS_ActivateOtherWindow(HWND hwnd)
1126 {
1127     HWND hwndTo, fg;
1128
1129     if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1130     {
1131         hwndTo = GetAncestor( hwndTo, GA_ROOT );
1132         if (can_activate_window( hwndTo )) goto done;
1133     }
1134
1135     hwndTo = hwnd;
1136     for (;;)
1137     {
1138         if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1139         if (can_activate_window( hwndTo )) break;
1140     }
1141
1142  done:
1143     fg = GetForegroundWindow();
1144     TRACE("win = %p fg = %p\n", hwndTo, fg);
1145     if (!fg || (hwnd == fg))
1146     {
1147         if (SetForegroundWindow( hwndTo )) return;
1148     }
1149     if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1150 }
1151
1152
1153 /***********************************************************************
1154  *           WINPOS_HandleWindowPosChanging
1155  *
1156  * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1157  */
1158 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1159 {
1160     POINT minTrack, maxTrack;
1161     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1162
1163     if (winpos->flags & SWP_NOSIZE) return 0;
1164     if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1165     {
1166         WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1167         if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1168         if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1169         if (!(style & WS_MINIMIZE))
1170         {
1171             if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1172             if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1173         }
1174     }
1175     return 0;
1176 }
1177
1178
1179 /***********************************************************************
1180  *           dump_winpos_flags
1181  */
1182 static void dump_winpos_flags(UINT flags)
1183 {
1184     TRACE("flags:");
1185     if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1186     if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1187     if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1188     if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1189     if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1190     if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1191     if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1192     if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1193     if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1194     if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1195     if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1196     if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1197     if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1198
1199 #define DUMPED_FLAGS \
1200     (SWP_NOSIZE | \
1201     SWP_NOMOVE | \
1202     SWP_NOZORDER | \
1203     SWP_NOREDRAW | \
1204     SWP_NOACTIVATE | \
1205     SWP_FRAMECHANGED | \
1206     SWP_SHOWWINDOW | \
1207     SWP_HIDEWINDOW | \
1208     SWP_NOCOPYBITS | \
1209     SWP_NOOWNERZORDER | \
1210     SWP_NOSENDCHANGING | \
1211     SWP_DEFERERASE | \
1212     SWP_ASYNCWINDOWPOS)
1213
1214     if(flags & ~DUMPED_FLAGS) TRACE(" %08x", flags & ~DUMPED_FLAGS);
1215     TRACE("\n");
1216 #undef DUMPED_FLAGS
1217 }
1218
1219 /***********************************************************************
1220  *           SWP_DoWinPosChanging
1221  */
1222 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
1223 {
1224     WND *wndPtr;
1225
1226     /* Send WM_WINDOWPOSCHANGING message */
1227
1228     if (!(pWinpos->flags & SWP_NOSENDCHANGING))
1229         SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
1230
1231     if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) ||
1232         wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
1233
1234     /* Calculate new position and size */
1235
1236     *pNewWindowRect = wndPtr->rectWindow;
1237     *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
1238                                                     : wndPtr->rectClient;
1239
1240     if (!(pWinpos->flags & SWP_NOSIZE))
1241     {
1242         pNewWindowRect->right  = pNewWindowRect->left + pWinpos->cx;
1243         pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
1244     }
1245     if (!(pWinpos->flags & SWP_NOMOVE))
1246     {
1247         pNewWindowRect->left    = pWinpos->x;
1248         pNewWindowRect->top     = pWinpos->y;
1249         pNewWindowRect->right  += pWinpos->x - wndPtr->rectWindow.left;
1250         pNewWindowRect->bottom += pWinpos->y - wndPtr->rectWindow.top;
1251
1252         OffsetRect( pNewClientRect, pWinpos->x - wndPtr->rectWindow.left,
1253                                     pWinpos->y - wndPtr->rectWindow.top );
1254     }
1255     pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
1256
1257     TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
1258            pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
1259            pWinpos->cx, pWinpos->cy, pWinpos->flags );
1260     TRACE( "current %s style %08x new %s\n",
1261            wine_dbgstr_rect( &wndPtr->rectWindow ), wndPtr->dwStyle,
1262            wine_dbgstr_rect( pNewWindowRect ));
1263
1264     WIN_ReleasePtr( wndPtr );
1265     return TRUE;
1266 }
1267
1268 /***********************************************************************
1269  *           get_valid_rects
1270  *
1271  * Compute the valid rects from the old and new client rect and WVR_* flags.
1272  * Helper for WM_NCCALCSIZE handling.
1273  */
1274 static inline void get_valid_rects( const RECT *old_client, const RECT *new_client, UINT flags,
1275                                     RECT *valid )
1276 {
1277     int cx, cy;
1278
1279     if (flags & WVR_REDRAW)
1280     {
1281         SetRectEmpty( &valid[0] );
1282         SetRectEmpty( &valid[1] );
1283         return;
1284     }
1285
1286     if (flags & WVR_VALIDRECTS)
1287     {
1288         if (!IntersectRect( &valid[0], &valid[0], new_client ) ||
1289             !IntersectRect( &valid[1], &valid[1], old_client ))
1290         {
1291             SetRectEmpty( &valid[0] );
1292             SetRectEmpty( &valid[1] );
1293             return;
1294         }
1295         flags = WVR_ALIGNLEFT | WVR_ALIGNTOP;
1296     }
1297     else
1298     {
1299         valid[0] = *new_client;
1300         valid[1] = *old_client;
1301     }
1302
1303     /* make sure the rectangles have the same size */
1304     cx = min( valid[0].right - valid[0].left, valid[1].right - valid[1].left );
1305     cy = min( valid[0].bottom - valid[0].top, valid[1].bottom - valid[1].top );
1306
1307     if (flags & WVR_ALIGNBOTTOM)
1308     {
1309         valid[0].top = valid[0].bottom - cy;
1310         valid[1].top = valid[1].bottom - cy;
1311     }
1312     else
1313     {
1314         valid[0].bottom = valid[0].top + cy;
1315         valid[1].bottom = valid[1].top + cy;
1316     }
1317     if (flags & WVR_ALIGNRIGHT)
1318     {
1319         valid[0].left = valid[0].right - cx;
1320         valid[1].left = valid[1].right - cx;
1321     }
1322     else
1323     {
1324         valid[0].right = valid[0].left + cx;
1325         valid[1].right = valid[1].left + cx;
1326     }
1327 }
1328
1329
1330 /***********************************************************************
1331  *           SWP_DoOwnedPopups
1332  *
1333  * fix Z order taking into account owned popups -
1334  * basically we need to maintain them above the window that owns them
1335  *
1336  * FIXME: hide/show owned popups when owner visibility changes.
1337  */
1338 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
1339 {
1340     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1341     HWND owner, *list = NULL;
1342     unsigned int i;
1343
1344     TRACE("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
1345
1346     if ((style & WS_POPUP) && (owner = GetWindow( hwnd, GW_OWNER )))
1347     {
1348         /* make sure this popup stays above the owner */
1349
1350         if (hwndInsertAfter != HWND_TOP && hwndInsertAfter != HWND_TOPMOST)
1351         {
1352             if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return hwndInsertAfter;
1353
1354             for (i = 0; list[i]; i++)
1355             {
1356                 if (list[i] == owner)
1357                 {
1358                     if (i > 0) hwndInsertAfter = list[i-1];
1359                     else hwndInsertAfter = HWND_TOP;
1360                     break;
1361                 }
1362
1363                 if (hwndInsertAfter == HWND_NOTOPMOST)
1364                 {
1365                     if (!(GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST)) break;
1366                 }
1367                 else if (list[i] == hwndInsertAfter) break;
1368             }
1369         }
1370     }
1371     else if (style & WS_CHILD) return hwndInsertAfter;
1372
1373     if (hwndInsertAfter == HWND_BOTTOM) goto done;
1374     if (!list && !(list = WIN_ListChildren( GetDesktopWindow() ))) goto done;
1375
1376     i = 0;
1377     if (hwndInsertAfter == HWND_TOP || hwndInsertAfter == HWND_NOTOPMOST)
1378     {
1379         if (hwndInsertAfter == HWND_NOTOPMOST || !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_TOPMOST))
1380         {
1381             /* skip all the topmost windows */
1382             while (list[i] && (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST)) i++;
1383         }
1384     }
1385     else if (hwndInsertAfter != HWND_TOPMOST)
1386     {
1387         /* skip windows that are already placed correctly */
1388         for (i = 0; list[i]; i++)
1389         {
1390             if (list[i] == hwndInsertAfter) break;
1391             if (list[i] == hwnd) goto done;  /* nothing to do if window is moving backwards in z-order */
1392         }
1393     }
1394
1395     for ( ; list[i]; i++)
1396     {
1397         if (list[i] == hwnd) break;
1398         if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_POPUP)) continue;
1399         if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
1400         TRACE( "moving %p owned by %p after %p\n", list[i], hwnd, hwndInsertAfter );
1401         SetWindowPos( list[i], hwndInsertAfter, 0, 0, 0, 0,
1402                       SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING | SWP_DEFERERASE );
1403         hwndInsertAfter = list[i];
1404     }
1405
1406 done:
1407     HeapFree( GetProcessHeap(), 0, list );
1408     return hwndInsertAfter;
1409 }
1410
1411 /***********************************************************************
1412  *           SWP_DoNCCalcSize
1413  */
1414 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect,
1415                               RECT *validRects )
1416 {
1417     UINT wvrFlags = 0;
1418     WND *wndPtr;
1419
1420     if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
1421
1422       /* Send WM_NCCALCSIZE message to get new client area */
1423     if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
1424     {
1425         NCCALCSIZE_PARAMS params;
1426         WINDOWPOS winposCopy;
1427
1428         params.rgrc[0] = *pNewWindowRect;
1429         params.rgrc[1] = wndPtr->rectWindow;
1430         params.rgrc[2] = wndPtr->rectClient;
1431         params.lppos = &winposCopy;
1432         winposCopy = *pWinpos;
1433         WIN_ReleasePtr( wndPtr );
1434
1435         wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
1436
1437         *pNewClientRect = params.rgrc[0];
1438
1439         if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
1440
1441         TRACE( "hwnd %p old win %s old client %s new win %s new client %s\n", pWinpos->hwnd,
1442                wine_dbgstr_rect(&wndPtr->rectWindow), wine_dbgstr_rect(&wndPtr->rectClient),
1443                wine_dbgstr_rect(pNewWindowRect), wine_dbgstr_rect(pNewClientRect) );
1444
1445         if( pNewClientRect->left != wndPtr->rectClient.left ||
1446             pNewClientRect->top != wndPtr->rectClient.top )
1447             pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1448
1449         if( (pNewClientRect->right - pNewClientRect->left !=
1450              wndPtr->rectClient.right - wndPtr->rectClient.left))
1451             pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1452         else
1453             wvrFlags &= ~WVR_HREDRAW;
1454
1455         if (pNewClientRect->bottom - pNewClientRect->top !=
1456              wndPtr->rectClient.bottom - wndPtr->rectClient.top)
1457             pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1458         else
1459             wvrFlags &= ~WVR_VREDRAW;
1460
1461         validRects[0] = params.rgrc[1];
1462         validRects[1] = params.rgrc[2];
1463     }
1464     else
1465     {
1466         if (!(pWinpos->flags & SWP_NOMOVE) &&
1467             (pNewClientRect->left != wndPtr->rectClient.left ||
1468              pNewClientRect->top != wndPtr->rectClient.top))
1469             pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1470     }
1471
1472     if (pWinpos->flags & (SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
1473     {
1474         SetRectEmpty( &validRects[0] );
1475         SetRectEmpty( &validRects[1] );
1476     }
1477     else get_valid_rects( &wndPtr->rectClient, pNewClientRect, wvrFlags, validRects );
1478
1479     WIN_ReleasePtr( wndPtr );
1480     return wvrFlags;
1481 }
1482
1483 /* fix redundant flags and values in the WINDOWPOS structure */
1484 static BOOL fixup_flags( WINDOWPOS *winpos )
1485 {
1486     HWND parent;
1487     WND *wndPtr = WIN_GetPtr( winpos->hwnd );
1488     BOOL ret = TRUE;
1489
1490     if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
1491     {
1492         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1493         return FALSE;
1494     }
1495     winpos->hwnd = wndPtr->hwndSelf;  /* make it a full handle */
1496
1497     /* Finally make sure that all coordinates are valid */
1498     if (winpos->x < -32768) winpos->x = -32768;
1499     else if (winpos->x > 32767) winpos->x = 32767;
1500     if (winpos->y < -32768) winpos->y = -32768;
1501     else if (winpos->y > 32767) winpos->y = 32767;
1502
1503     if (winpos->cx < 0) winpos->cx = 0;
1504     else if (winpos->cx > 32767) winpos->cx = 32767;
1505     if (winpos->cy < 0) winpos->cy = 0;
1506     else if (winpos->cy > 32767) winpos->cy = 32767;
1507
1508     parent = GetAncestor( winpos->hwnd, GA_PARENT );
1509     if (!IsWindowVisible( parent )) winpos->flags |= SWP_NOREDRAW;
1510
1511     if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
1512     else
1513     {
1514         winpos->flags &= ~SWP_HIDEWINDOW;
1515         if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
1516     }
1517
1518     if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
1519         (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
1520         winpos->flags |= SWP_NOSIZE;    /* Already the right size */
1521
1522     if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
1523         winpos->flags |= SWP_NOMOVE;    /* Already the right position */
1524
1525     if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
1526     {
1527         if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)) && /* Bring to the top when activating */
1528             (winpos->flags & SWP_NOZORDER ||
1529              (winpos->hwndInsertAfter != HWND_TOPMOST && winpos->hwndInsertAfter != HWND_NOTOPMOST)))
1530         {
1531             winpos->flags &= ~SWP_NOZORDER;
1532             winpos->hwndInsertAfter = HWND_TOP;
1533         }
1534     }
1535
1536     /* Check hwndInsertAfter */
1537     if (winpos->flags & SWP_NOZORDER) goto done;
1538
1539     /* fix sign extension */
1540     if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
1541     else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
1542
1543     /* hwndInsertAfter must be a sibling of the window */
1544     if (winpos->hwndInsertAfter == HWND_TOP)
1545     {
1546         if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1547             winpos->flags |= SWP_NOZORDER;
1548     }
1549     else if (winpos->hwndInsertAfter == HWND_BOTTOM)
1550     {
1551         if (!(wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
1552             winpos->flags |= SWP_NOZORDER;
1553     }
1554     else if (winpos->hwndInsertAfter == HWND_TOPMOST)
1555     {
1556         if ((wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1557             winpos->flags |= SWP_NOZORDER;
1558     }
1559     else if (winpos->hwndInsertAfter == HWND_NOTOPMOST)
1560     {
1561         if (!(wndPtr->dwExStyle & WS_EX_TOPMOST))
1562             winpos->flags |= SWP_NOZORDER;
1563     }
1564     else
1565     {
1566         if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != parent) ret = FALSE;
1567         else
1568         {
1569             /* don't need to change the Zorder of hwnd if it's already inserted
1570              * after hwndInsertAfter or when inserting hwnd after itself.
1571              */
1572             if ((winpos->hwnd == winpos->hwndInsertAfter) ||
1573                 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
1574                 winpos->flags |= SWP_NOZORDER;
1575         }
1576     }
1577  done:
1578     WIN_ReleasePtr( wndPtr );
1579     return ret;
1580 }
1581
1582
1583 /***********************************************************************
1584  *              set_window_pos
1585  *
1586  * Backend implementation of SetWindowPos.
1587  */
1588 BOOL set_window_pos( HWND hwnd, HWND insert_after, UINT swp_flags,
1589                      const RECT *window_rect, const RECT *client_rect, const RECT *valid_rects )
1590 {
1591     WND *win;
1592     BOOL ret;
1593     RECT visible_rect;
1594
1595     if (!(win = WIN_GetPtr( hwnd ))) return FALSE;
1596     if (win == WND_DESKTOP || win == WND_OTHER_PROCESS) return FALSE;
1597
1598     SERVER_START_REQ( set_window_pos )
1599     {
1600         req->handle        = hwnd;
1601         req->previous      = insert_after;
1602         req->flags         = swp_flags;
1603         req->window.left   = window_rect->left;
1604         req->window.top    = window_rect->top;
1605         req->window.right  = window_rect->right;
1606         req->window.bottom = window_rect->bottom;
1607         req->client.left   = client_rect->left;
1608         req->client.top    = client_rect->top;
1609         req->client.right  = client_rect->right;
1610         req->client.bottom = client_rect->bottom;
1611         if (!IsRectEmpty( &valid_rects[0] ))
1612             wine_server_add_data( req, valid_rects, 2 * sizeof(*valid_rects) );
1613         if ((ret = !wine_server_call( req )))
1614         {
1615             win->dwStyle    = reply->new_style;
1616             win->dwExStyle  = reply->new_ex_style;
1617             win->rectWindow = *window_rect;
1618             win->rectClient = *client_rect;
1619             visible_rect.left   = reply->visible.left;
1620             visible_rect.top    = reply->visible.top;
1621             visible_rect.right  = reply->visible.right;
1622             visible_rect.bottom = reply->visible.bottom;
1623         }
1624     }
1625     SERVER_END_REQ;
1626     WIN_ReleasePtr( win );
1627
1628     if (ret)
1629         USER_Driver->pSetWindowPos( hwnd, insert_after, swp_flags, window_rect,
1630                                     client_rect, &visible_rect, valid_rects );
1631     return ret;
1632 }
1633
1634
1635 /***********************************************************************
1636  *              USER_SetWindowPos
1637  *
1638  *     User32 internal function
1639  */
1640 BOOL USER_SetWindowPos( WINDOWPOS * winpos )
1641 {
1642     RECT newWindowRect, newClientRect, valid_rects[2];
1643     UINT orig_flags;
1644     
1645     orig_flags = winpos->flags;
1646     
1647     /* First make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
1648     if (!(winpos->flags & SWP_NOMOVE))
1649     {
1650         if (winpos->x < -32768) winpos->x = -32768;
1651         else if (winpos->x > 32767) winpos->x = 32767;
1652         if (winpos->y < -32768) winpos->y = -32768;
1653         else if (winpos->y > 32767) winpos->y = 32767;
1654     }
1655     if (!(winpos->flags & SWP_NOSIZE))
1656     {
1657         if (winpos->cx < 0) winpos->cx = 0;
1658         else if (winpos->cx > 32767) winpos->cx = 32767;
1659         if (winpos->cy < 0) winpos->cy = 0;
1660         else if (winpos->cy > 32767) winpos->cy = 32767;
1661     }
1662
1663     if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
1664
1665     /* Fix redundant flags */
1666     if (!fixup_flags( winpos )) return FALSE;
1667
1668     if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
1669     {
1670         if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
1671             winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
1672     }
1673
1674     /* Common operations */
1675
1676     SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect, valid_rects );
1677
1678     if (!set_window_pos( winpos->hwnd, winpos->hwndInsertAfter, winpos->flags,
1679                          &newWindowRect, &newClientRect, valid_rects ))
1680         return FALSE;
1681
1682     /* erase parent when hiding or resizing child */
1683     if (!(orig_flags & SWP_DEFERERASE) &&
1684         ((orig_flags & SWP_HIDEWINDOW) ||
1685          (!(orig_flags & SWP_SHOWWINDOW) &&
1686           (winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOGEOMETRYCHANGE)))
1687     {
1688         HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
1689         if (!parent || parent == GetDesktopWindow()) parent = winpos->hwnd;
1690         erase_now( parent, 0 );
1691     }
1692
1693     if( winpos->flags & SWP_HIDEWINDOW )
1694         HideCaret(winpos->hwnd);
1695     else if (winpos->flags & SWP_SHOWWINDOW)
1696         ShowCaret(winpos->hwnd);
1697
1698     if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)))
1699     {
1700         /* child windows get WM_CHILDACTIVATE message */
1701         if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
1702             SendMessageW( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
1703         else
1704             SetForegroundWindow( winpos->hwnd );
1705     }
1706
1707       /* And last, send the WM_WINDOWPOSCHANGED message */
1708
1709     TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
1710
1711     if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
1712     {
1713         /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
1714            and always contains final window position.
1715          */
1716         winpos->x = newWindowRect.left;
1717         winpos->y = newWindowRect.top;
1718         winpos->cx = newWindowRect.right - newWindowRect.left;
1719         winpos->cy = newWindowRect.bottom - newWindowRect.top;
1720         SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
1721     }
1722     return TRUE;
1723 }
1724
1725 /***********************************************************************
1726  *              SetWindowPos (USER32.@)
1727  */
1728 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
1729                           INT x, INT y, INT cx, INT cy, UINT flags )
1730 {
1731     WINDOWPOS winpos;
1732
1733     TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1734           hwnd, hwndInsertAfter, x, y, cx, cy, flags);
1735     if(TRACE_ON(win)) dump_winpos_flags(flags);
1736
1737     if (is_broadcast(hwnd))
1738     {
1739         SetLastError( ERROR_INVALID_PARAMETER );
1740         return FALSE;
1741     }
1742
1743     winpos.hwnd = WIN_GetFullHandle(hwnd);
1744     winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
1745     winpos.x = x;
1746     winpos.y = y;
1747     winpos.cx = cx;
1748     winpos.cy = cy;
1749     winpos.flags = flags;
1750     
1751     if (WIN_IsCurrentThread( hwnd ))
1752         return USER_SetWindowPos(&winpos);
1753
1754     return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
1755 }
1756
1757
1758 /***********************************************************************
1759  *              BeginDeferWindowPos (USER32.@)
1760  */
1761 HDWP WINAPI BeginDeferWindowPos( INT count )
1762 {
1763     HDWP handle;
1764     DWP *pDWP;
1765
1766     TRACE("%d\n", count);
1767
1768     if (count < 0)
1769     {
1770         SetLastError(ERROR_INVALID_PARAMETER);
1771         return 0;
1772     }
1773     /* Windows allows zero count, in which case it allocates context for 8 moves */
1774     if (count == 0) count = 8;
1775
1776     handle = USER_HEAP_ALLOC( sizeof(DWP) + (count-1)*sizeof(WINDOWPOS) );
1777     if (!handle) return 0;
1778     pDWP = (DWP *) USER_HEAP_LIN_ADDR( handle );
1779     pDWP->actualCount    = 0;
1780     pDWP->suggestedCount = count;
1781     pDWP->valid          = TRUE;
1782     pDWP->wMagic         = DWP_MAGIC;
1783     pDWP->hwndParent     = 0;
1784
1785     TRACE("returning hdwp %p\n", handle);
1786     return handle;
1787 }
1788
1789
1790 /***********************************************************************
1791  *              DeferWindowPos (USER32.@)
1792  */
1793 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
1794                                 INT x, INT y, INT cx, INT cy,
1795                                 UINT flags )
1796 {
1797     DWP *pDWP;
1798     int i;
1799     HDWP newhdwp = hdwp,retvalue;
1800
1801     TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1802           hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
1803
1804     hwnd = WIN_GetFullHandle( hwnd );
1805     if (hwnd == GetDesktopWindow()) return 0;
1806
1807     if (!(pDWP = USER_HEAP_LIN_ADDR( hdwp ))) return 0;
1808
1809     USER_Lock();
1810
1811     for (i = 0; i < pDWP->actualCount; i++)
1812     {
1813         if (pDWP->winPos[i].hwnd == hwnd)
1814         {
1815               /* Merge with the other changes */
1816             if (!(flags & SWP_NOZORDER))
1817             {
1818                 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
1819             }
1820             if (!(flags & SWP_NOMOVE))
1821             {
1822                 pDWP->winPos[i].x = x;
1823                 pDWP->winPos[i].y = y;
1824             }
1825             if (!(flags & SWP_NOSIZE))
1826             {
1827                 pDWP->winPos[i].cx = cx;
1828                 pDWP->winPos[i].cy = cy;
1829             }
1830             pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
1831                                                SWP_NOZORDER | SWP_NOREDRAW |
1832                                                SWP_NOACTIVATE | SWP_NOCOPYBITS|
1833                                                SWP_NOOWNERZORDER);
1834             pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
1835                                               SWP_FRAMECHANGED);
1836             retvalue = hdwp;
1837             goto END;
1838         }
1839     }
1840     if (pDWP->actualCount >= pDWP->suggestedCount)
1841     {
1842         newhdwp = USER_HEAP_REALLOC( hdwp,
1843                       sizeof(DWP) + pDWP->suggestedCount*sizeof(WINDOWPOS) );
1844         if (!newhdwp)
1845         {
1846             retvalue = 0;
1847             goto END;
1848         }
1849         pDWP = (DWP *) USER_HEAP_LIN_ADDR( newhdwp );
1850         pDWP->suggestedCount++;
1851     }
1852     pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
1853     pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
1854     pDWP->winPos[pDWP->actualCount].x = x;
1855     pDWP->winPos[pDWP->actualCount].y = y;
1856     pDWP->winPos[pDWP->actualCount].cx = cx;
1857     pDWP->winPos[pDWP->actualCount].cy = cy;
1858     pDWP->winPos[pDWP->actualCount].flags = flags;
1859     pDWP->actualCount++;
1860     retvalue = newhdwp;
1861 END:
1862     USER_Unlock();
1863     return retvalue;
1864 }
1865
1866
1867 /***********************************************************************
1868  *              EndDeferWindowPos (USER32.@)
1869  */
1870 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
1871 {
1872     DWP *pDWP;
1873     WINDOWPOS *winpos;
1874     BOOL res = TRUE;
1875     int i;
1876
1877     TRACE("%p\n", hdwp);
1878
1879     pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
1880     if (!pDWP) return FALSE;
1881     for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
1882     {
1883         TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1884                winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
1885                winpos->cx, winpos->cy, winpos->flags);
1886
1887         if (!(res = USER_SetWindowPos( winpos ))) break;
1888     }
1889     USER_HEAP_FREE( hdwp );
1890     return res;
1891 }
1892
1893
1894 /***********************************************************************
1895  *              TileChildWindows (USER.199)
1896  */
1897 void WINAPI TileChildWindows16( HWND16 parent, WORD action )
1898 {
1899     FIXME("(%04x, %d): stub\n", parent, action);
1900 }
1901
1902 /***********************************************************************
1903  *              CascadeChildWindows (USER.198)
1904  */
1905 void WINAPI CascadeChildWindows16( HWND16 parent, WORD action )
1906 {
1907     FIXME("(%04x, %d): stub\n", parent, action);
1908 }