kernel32: Fixed buffer overrun in get_registry_locale_info.
[wine] / dlls / user / 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 HAS_DLGFRAME(style,exStyle) \
44     (((exStyle) & WS_EX_DLGMODALFRAME) || \
45      (((style) & WS_DLGFRAME) && !((style) & WS_BORDER)))
46
47 #define HAS_THICKFRAME(style) \
48     (((style) & WS_THICKFRAME) && \
49      !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
50
51 #define EMPTYPOINT(pt)          ((*(LONG*)&(pt)) == -1)
52
53 #define PLACE_MIN               0x0001
54 #define PLACE_MAX               0x0002
55 #define PLACE_RECT              0x0004
56
57
58 #define DWP_MAGIC  ((INT)('W' | ('P' << 8) | ('O' << 16) | ('S' << 24)))
59
60 typedef struct
61 {
62     INT       actualCount;
63     INT       suggestedCount;
64     BOOL      valid;
65     INT       wMagic;
66     HWND      hwndParent;
67     WINDOWPOS winPos[1];
68 } DWP;
69
70 typedef struct
71 {
72     RECT16   rectNormal;
73     POINT16  ptIconPos;
74     POINT16  ptMaxPos;
75     HWND     hwndIconTitle;
76 } INTERNALPOS, *LPINTERNALPOS;
77
78 /* ----- internal functions ----- */
79
80 static const WCHAR SysIP_W[] = { 'S','y','s','I','P',0 };
81
82 static inline INTERNALPOS *get_internal_pos( HWND hwnd )
83 {
84     return GetPropW( hwnd, SysIP_W );
85 }
86
87 static inline void set_internal_pos( HWND hwnd, INTERNALPOS *pos )
88 {
89     SetPropW( hwnd, SysIP_W, pos );
90 }
91
92 /***********************************************************************
93  *           WINPOS_CheckInternalPos
94  *
95  * Called when a window is destroyed.
96  */
97 void WINPOS_CheckInternalPos( HWND hwnd )
98 {
99     LPINTERNALPOS lpPos = get_internal_pos( hwnd );
100
101     if ( lpPos )
102     {
103         if( IsWindow(lpPos->hwndIconTitle) )
104             DestroyWindow( lpPos->hwndIconTitle );
105         HeapFree( GetProcessHeap(), 0, lpPos );
106     }
107 }
108
109 /***********************************************************************
110  *              ArrangeIconicWindows (USER32.@)
111  */
112 UINT WINAPI ArrangeIconicWindows( HWND parent )
113 {
114     RECT rectParent;
115     HWND hwndChild;
116     INT x, y, xspacing, yspacing;
117
118     GetClientRect( parent, &rectParent );
119     x = rectParent.left;
120     y = rectParent.bottom;
121     xspacing = GetSystemMetrics(SM_CXICONSPACING);
122     yspacing = GetSystemMetrics(SM_CYICONSPACING);
123
124     hwndChild = GetWindow( parent, GW_CHILD );
125     while (hwndChild)
126     {
127         if( IsIconic( hwndChild ) )
128         {
129             WINPOS_ShowIconTitle( hwndChild, FALSE );
130
131             SetWindowPos( hwndChild, 0, x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
132                             y - yspacing - GetSystemMetrics(SM_CYICON)/2, 0, 0,
133                             SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
134             if( IsWindow(hwndChild) )
135                 WINPOS_ShowIconTitle(hwndChild , TRUE );
136
137             if (x <= rectParent.right - xspacing) x += xspacing;
138             else
139             {
140                 x = rectParent.left;
141                 y -= yspacing;
142             }
143         }
144         hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
145     }
146     return yspacing;
147 }
148
149
150 /***********************************************************************
151  *              SwitchToThisWindow (USER32.@)
152  */
153 void WINAPI SwitchToThisWindow( HWND hwnd, BOOL restore )
154 {
155     ShowWindow( hwnd, restore ? SW_RESTORE : SW_SHOWMINIMIZED );
156 }
157
158
159 /***********************************************************************
160  *              GetWindowRect (USER32.@)
161  */
162 BOOL WINAPI GetWindowRect( HWND hwnd, LPRECT rect )
163 {
164     BOOL ret = WIN_GetRectangles( hwnd, rect, NULL );
165     if (ret)
166     {
167         MapWindowPoints( GetAncestor( hwnd, GA_PARENT ), 0, (POINT *)rect, 2 );
168         TRACE( "hwnd %p (%d,%d)-(%d,%d)\n",
169                hwnd, rect->left, rect->top, rect->right, rect->bottom);
170     }
171     return ret;
172 }
173
174
175 /***********************************************************************
176  *              GetWindowRgn (USER32.@)
177  */
178 int WINAPI GetWindowRgn ( HWND hwnd, HRGN hrgn )
179 {
180     int nRet = ERROR;
181     NTSTATUS status;
182     HRGN win_rgn = 0;
183     RGNDATA *data;
184     size_t size = 256;
185
186     do
187     {
188         if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
189         {
190             SetLastError( ERROR_OUTOFMEMORY );
191             return ERROR;
192         }
193         SERVER_START_REQ( get_window_region )
194         {
195             req->window = hwnd;
196             wine_server_set_reply( req, data->Buffer, size );
197             if (!(status = wine_server_call( req )))
198             {
199                 size_t reply_size = wine_server_reply_size( reply );
200                 if (reply_size)
201                 {
202                     data->rdh.dwSize   = sizeof(data->rdh);
203                     data->rdh.iType    = RDH_RECTANGLES;
204                     data->rdh.nCount   = reply_size / sizeof(RECT);
205                     data->rdh.nRgnSize = reply_size;
206                     win_rgn = ExtCreateRegion( NULL, size, data );
207                 }
208             }
209             else size = reply->total_size;
210         }
211         SERVER_END_REQ;
212         HeapFree( GetProcessHeap(), 0, data );
213     } while (status == STATUS_BUFFER_OVERFLOW);
214
215     if (status) SetLastError( RtlNtStatusToDosError(status) );
216     else if (win_rgn)
217     {
218         nRet = CombineRgn( hrgn, win_rgn, 0, RGN_COPY );
219         DeleteObject( win_rgn );
220     }
221     return nRet;
222 }
223
224
225 /***********************************************************************
226  *              SetWindowRgn (USER32.@)
227  */
228 int WINAPI SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL bRedraw )
229 {
230     static const RECT empty_rect;
231     BOOL ret;
232
233     if (hrgn)
234     {
235         RGNDATA *data;
236         DWORD size;
237
238         if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
239         if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
240         if (!GetRegionData( hrgn, size, data ))
241         {
242             HeapFree( GetProcessHeap(), 0, data );
243             return FALSE;
244         }
245         SERVER_START_REQ( set_window_region )
246         {
247             req->window = hwnd;
248             if (data->rdh.nCount)
249                 wine_server_add_data( req, data->Buffer, data->rdh.nCount * sizeof(RECT) );
250             else
251                 wine_server_add_data( req, &empty_rect, sizeof(empty_rect) );
252             ret = !wine_server_call_err( req );
253         }
254         SERVER_END_REQ;
255     }
256     else  /* clear existing region */
257     {
258         SERVER_START_REQ( set_window_region )
259         {
260             req->window = hwnd;
261             ret = !wine_server_call_err( req );
262         }
263         SERVER_END_REQ;
264     }
265
266     if (ret) ret = USER_Driver->pSetWindowRgn( hwnd, hrgn, bRedraw );
267
268     if (ret)
269     {
270         UINT swp_flags = SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_FRAMECHANGED;
271         if (hrgn) swp_flags |= SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE;
272         if (!bRedraw) swp_flags |= SWP_NOREDRAW;
273         SetWindowPos( hwnd, 0, 0, 0, 0, 0, swp_flags );
274     }
275     return ret;
276 }
277
278
279 /***********************************************************************
280  *              GetClientRect (USER32.@)
281  */
282 BOOL WINAPI GetClientRect( HWND hwnd, LPRECT rect )
283 {
284     BOOL ret;
285
286     if ((ret = WIN_GetRectangles( hwnd, NULL, rect )))
287     {
288         rect->right -= rect->left;
289         rect->bottom -= rect->top;
290         rect->left = rect->top = 0;
291     }
292     return ret;
293 }
294
295
296 /*******************************************************************
297  *              ClientToScreen (USER32.@)
298  */
299 BOOL WINAPI ClientToScreen( HWND hwnd, LPPOINT lppnt )
300 {
301     MapWindowPoints( hwnd, 0, lppnt, 1 );
302     return TRUE;
303 }
304
305
306 /*******************************************************************
307  *              ScreenToClient (USER32.@)
308  */
309 BOOL WINAPI ScreenToClient( HWND hwnd, LPPOINT lppnt )
310 {
311     MapWindowPoints( 0, hwnd, lppnt, 1 );
312     return TRUE;
313 }
314
315
316 /***********************************************************************
317  *           list_children_from_point
318  *
319  * Get the list of children that can contain point from the server.
320  * Point is in screen coordinates.
321  * Returned list must be freed by caller.
322  */
323 static HWND *list_children_from_point( HWND hwnd, POINT pt )
324 {
325     HWND *list;
326     int size = 32;
327
328     for (;;)
329     {
330         int count = 0;
331
332         if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
333
334         SERVER_START_REQ( get_window_children_from_point )
335         {
336             req->parent = hwnd;
337             req->x = pt.x;
338             req->y = pt.y;
339             wine_server_set_reply( req, list, (size-1) * sizeof(HWND) );
340             if (!wine_server_call( req )) count = reply->count;
341         }
342         SERVER_END_REQ;
343         if (count && count < size)
344         {
345             list[count] = 0;
346             return list;
347         }
348         HeapFree( GetProcessHeap(), 0, list );
349         if (!count) break;
350         size = count + 1;  /* restart with a large enough buffer */
351     }
352     return NULL;
353 }
354
355
356 /***********************************************************************
357  *           WINPOS_WindowFromPoint
358  *
359  * Find the window and hittest for a given point.
360  */
361 HWND WINPOS_WindowFromPoint( HWND hwndScope, POINT pt, INT *hittest )
362 {
363     int i, res;
364     HWND ret, *list;
365
366     if (!hwndScope) hwndScope = GetDesktopWindow();
367
368     *hittest = HTNOWHERE;
369
370     if (!(list = list_children_from_point( hwndScope, pt ))) return 0;
371
372     /* now determine the hittest */
373
374     for (i = 0; list[i]; i++)
375     {
376         LONG style = GetWindowLongW( list[i], GWL_STYLE );
377
378         /* If window is minimized or disabled, return at once */
379         if (style & WS_MINIMIZE)
380         {
381             *hittest = HTCAPTION;
382             break;
383         }
384         if (style & WS_DISABLED)
385         {
386             *hittest = HTERROR;
387             break;
388         }
389         /* Send WM_NCCHITTEST (if same thread) */
390         if (!WIN_IsCurrentThread( list[i] ))
391         {
392             *hittest = HTCLIENT;
393             break;
394         }
395         res = SendMessageW( list[i], WM_NCHITTEST, 0, MAKELONG(pt.x,pt.y) );
396         if (res != HTTRANSPARENT)
397         {
398             *hittest = res;  /* Found the window */
399             break;
400         }
401         /* continue search with next window in z-order */
402     }
403     ret = list[i];
404     HeapFree( GetProcessHeap(), 0, list );
405     TRACE( "scope %p (%d,%d) returning %p\n", hwndScope, pt.x, pt.y, ret );
406     return ret;
407 }
408
409
410 /*******************************************************************
411  *              WindowFromPoint (USER32.@)
412  */
413 HWND WINAPI WindowFromPoint( POINT pt )
414 {
415     INT hittest;
416     return WINPOS_WindowFromPoint( 0, pt, &hittest );
417 }
418
419
420 /*******************************************************************
421  *              ChildWindowFromPoint (USER32.@)
422  */
423 HWND WINAPI ChildWindowFromPoint( HWND hwndParent, POINT pt )
424 {
425     return ChildWindowFromPointEx( hwndParent, pt, CWP_ALL );
426 }
427
428 /*******************************************************************
429  *              ChildWindowFromPointEx (USER32.@)
430  */
431 HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
432 {
433     /* pt is in the client coordinates */
434     HWND *list;
435     int i;
436     RECT rect;
437     HWND retvalue;
438
439     GetClientRect( hwndParent, &rect );
440     if (!PtInRect( &rect, pt )) return 0;
441     if (!(list = WIN_ListChildren( hwndParent ))) return hwndParent;
442
443     for (i = 0; list[i]; i++)
444     {
445         if (!WIN_GetRectangles( list[i], &rect, NULL )) continue;
446         if (!PtInRect( &rect, pt )) continue;
447         if (uFlags & (CWP_SKIPINVISIBLE|CWP_SKIPDISABLED))
448         {
449             LONG style = GetWindowLongW( list[i], GWL_STYLE );
450             if ((uFlags & CWP_SKIPINVISIBLE) && !(style & WS_VISIBLE)) continue;
451             if ((uFlags & CWP_SKIPDISABLED) && (style & WS_DISABLED)) continue;
452         }
453         if (uFlags & CWP_SKIPTRANSPARENT)
454         {
455             if (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TRANSPARENT) continue;
456         }
457         break;
458     }
459     retvalue = list[i];
460     HeapFree( GetProcessHeap(), 0, list );
461     if (!retvalue) retvalue = hwndParent;
462     return retvalue;
463 }
464
465
466 /*******************************************************************
467  *         WINPOS_GetWinOffset
468  *
469  * Calculate the offset between the origin of the two windows. Used
470  * to implement MapWindowPoints.
471  */
472 static void WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo, POINT *offset )
473 {
474     WND * wndPtr;
475
476     offset->x = offset->y = 0;
477
478     /* Translate source window origin to screen coords */
479     if (hwndFrom)
480     {
481         HWND hwnd = hwndFrom;
482
483         while (hwnd)
484         {
485             if (hwnd == hwndTo) return;
486             if (!(wndPtr = WIN_GetPtr( hwnd )))
487             {
488                 ERR( "bad hwndFrom = %p\n", hwnd );
489                 return;
490             }
491             if (wndPtr == WND_DESKTOP) break;
492             if (wndPtr == WND_OTHER_PROCESS) goto other_process;
493             offset->x += wndPtr->rectClient.left;
494             offset->y += wndPtr->rectClient.top;
495             hwnd = wndPtr->parent;
496             WIN_ReleasePtr( wndPtr );
497         }
498     }
499
500     /* Translate origin to destination window coords */
501     if (hwndTo)
502     {
503         HWND hwnd = hwndTo;
504
505         while (hwnd)
506         {
507             if (!(wndPtr = WIN_GetPtr( hwnd )))
508             {
509                 ERR( "bad hwndTo = %p\n", hwnd );
510                 return;
511             }
512             if (wndPtr == WND_DESKTOP) break;
513             if (wndPtr == WND_OTHER_PROCESS) goto other_process;
514             offset->x -= wndPtr->rectClient.left;
515             offset->y -= wndPtr->rectClient.top;
516             hwnd = wndPtr->parent;
517             WIN_ReleasePtr( wndPtr );
518         }
519     }
520     return;
521
522  other_process:  /* one of the parents may belong to another process, do it the hard way */
523     offset->x = offset->y = 0;
524     SERVER_START_REQ( get_windows_offset )
525     {
526         req->from = hwndFrom;
527         req->to   = hwndTo;
528         if (!wine_server_call( req ))
529         {
530             offset->x = reply->x;
531             offset->y = reply->y;
532         }
533     }
534     SERVER_END_REQ;
535 }
536
537
538 /*******************************************************************
539  *              MapWindowPoints (USER.258)
540  */
541 void WINAPI MapWindowPoints16( HWND16 hwndFrom, HWND16 hwndTo,
542                                LPPOINT16 lppt, UINT16 count )
543 {
544     POINT offset;
545
546     WINPOS_GetWinOffset( WIN_Handle32(hwndFrom), WIN_Handle32(hwndTo), &offset );
547     while (count--)
548     {
549         lppt->x += offset.x;
550         lppt->y += offset.y;
551         lppt++;
552     }
553 }
554
555
556 /*******************************************************************
557  *              MapWindowPoints (USER32.@)
558  */
559 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
560 {
561     POINT offset;
562
563     WINPOS_GetWinOffset( hwndFrom, hwndTo, &offset );
564     while (count--)
565     {
566         lppt->x += offset.x;
567         lppt->y += offset.y;
568         lppt++;
569     }
570     return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
571 }
572
573
574 /***********************************************************************
575  *              IsIconic (USER32.@)
576  */
577 BOOL WINAPI IsIconic(HWND hWnd)
578 {
579     return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
580 }
581
582
583 /***********************************************************************
584  *              IsZoomed (USER32.@)
585  */
586 BOOL WINAPI IsZoomed(HWND hWnd)
587 {
588     return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
589 }
590
591
592 /*******************************************************************
593  *              AllowSetForegroundWindow (USER32.@)
594  */
595 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
596 {
597     /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
598      * implemented, then fix this function. */
599     return TRUE;
600 }
601
602
603 /*******************************************************************
604  *              LockSetForegroundWindow (USER32.@)
605  */
606 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
607 {
608     /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
609      * implemented, then fix this function. */
610     return TRUE;
611 }
612
613
614 /***********************************************************************
615  *              BringWindowToTop (USER32.@)
616  */
617 BOOL WINAPI BringWindowToTop( HWND hwnd )
618 {
619     return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
620 }
621
622
623 /***********************************************************************
624  *              MoveWindow (USER32.@)
625  */
626 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
627                             BOOL repaint )
628 {
629     int flags = SWP_NOZORDER | SWP_NOACTIVATE;
630     if (!repaint) flags |= SWP_NOREDRAW;
631     TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
632     return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
633 }
634
635 /***********************************************************************
636  *           WINPOS_InitInternalPos
637  */
638 static LPINTERNALPOS WINPOS_InitInternalPos( WND* wnd )
639 {
640     LPINTERNALPOS lpPos = get_internal_pos( wnd->hwndSelf );
641     if( !lpPos )
642     {
643         /* this happens when the window is minimized/maximized
644          * for the first time (rectWindow is not adjusted yet) */
645
646         lpPos = HeapAlloc( GetProcessHeap(), 0, sizeof(INTERNALPOS) );
647         if( !lpPos ) return NULL;
648
649         set_internal_pos( wnd->hwndSelf, lpPos );
650         lpPos->hwndIconTitle = 0; /* defer until needs to be shown */
651         lpPos->rectNormal.left   = wnd->rectWindow.left;
652         lpPos->rectNormal.top    = wnd->rectWindow.top;
653         lpPos->rectNormal.right  = wnd->rectWindow.right;
654         lpPos->rectNormal.bottom = wnd->rectWindow.bottom;
655         lpPos->ptIconPos.x = lpPos->ptIconPos.y = -1;
656         lpPos->ptMaxPos.x = lpPos->ptMaxPos.y = -1;
657     }
658
659     if( wnd->dwStyle & WS_MINIMIZE )
660     {
661         lpPos->ptIconPos.x = wnd->rectWindow.left;
662         lpPos->ptIconPos.y = wnd->rectWindow.top;
663     }
664     else if( wnd->dwStyle & WS_MAXIMIZE )
665     {
666         lpPos->ptMaxPos.x = wnd->rectWindow.left;
667         lpPos->ptMaxPos.y = wnd->rectWindow.top;
668     }
669     else
670     {
671         lpPos->rectNormal.left   = wnd->rectWindow.left;
672         lpPos->rectNormal.top    = wnd->rectWindow.top;
673         lpPos->rectNormal.right  = wnd->rectWindow.right;
674         lpPos->rectNormal.bottom = wnd->rectWindow.bottom;
675     }
676     return lpPos;
677 }
678
679 /***********************************************************************
680  *           WINPOS_RedrawIconTitle
681  */
682 BOOL WINPOS_RedrawIconTitle( HWND hWnd )
683 {
684     LPINTERNALPOS lpPos = get_internal_pos( hWnd );
685     if( lpPos )
686     {
687         if( lpPos->hwndIconTitle )
688         {
689             SendMessageW( lpPos->hwndIconTitle, WM_SHOWWINDOW, TRUE, 0);
690             InvalidateRect( lpPos->hwndIconTitle, NULL, TRUE );
691             return TRUE;
692         }
693     }
694     return FALSE;
695 }
696
697 /***********************************************************************
698  *           WINPOS_ShowIconTitle
699  */
700 BOOL WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
701 {
702     LPINTERNALPOS lpPos = get_internal_pos( hwnd );
703
704     if (lpPos && !GetPropA( hwnd, "__wine_x11_managed" ))
705     {
706         HWND title = lpPos->hwndIconTitle;
707
708         TRACE("%p %i\n", hwnd, (bShow != 0) );
709
710         if( !title )
711             lpPos->hwndIconTitle = title = ICONTITLE_Create( hwnd );
712         if( bShow )
713         {
714             if (!IsWindowVisible(title))
715             {
716                 SendMessageW( title, WM_SHOWWINDOW, TRUE, 0 );
717                 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
718                               SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
719             }
720         }
721         else ShowWindow( title, SW_HIDE );
722     }
723     return FALSE;
724 }
725
726 /*******************************************************************
727  *           WINPOS_GetMinMaxInfo
728  *
729  * Get the minimized and maximized information for a window.
730  */
731 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
732                            POINT *minTrack, POINT *maxTrack )
733 {
734     LPINTERNALPOS lpPos;
735     MINMAXINFO MinMax;
736     HMONITOR monitor;
737     INT xinc, yinc;
738     LONG style = GetWindowLongA( hwnd, GWL_STYLE );
739     LONG exstyle = GetWindowLongA( hwnd, GWL_EXSTYLE );
740     RECT rc;
741
742     /* Compute default values */
743
744     GetWindowRect(hwnd, &rc);
745     MinMax.ptReserved.x = rc.left;
746     MinMax.ptReserved.y = rc.top;
747
748     if (style & WS_CHILD)
749     {
750         if ((style & WS_CAPTION) == WS_CAPTION)
751             style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
752
753         GetClientRect(GetAncestor(hwnd,GA_PARENT), &rc);
754         AdjustWindowRectEx(&rc, style, ((style & WS_POPUP) && GetMenu(hwnd)), exstyle);
755
756         /* avoid calculating this twice */
757         style &= ~(WS_DLGFRAME | WS_BORDER | WS_THICKFRAME);
758
759         MinMax.ptMaxSize.x = rc.right - rc.left;
760         MinMax.ptMaxSize.y = rc.bottom - rc.top;
761     }
762     else
763     {
764         MinMax.ptMaxSize.x = GetSystemMetrics(SM_CXSCREEN);
765         MinMax.ptMaxSize.y = GetSystemMetrics(SM_CYSCREEN);
766     }
767     MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
768     MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
769     MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXMAXTRACK);
770     MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYMAXTRACK);
771
772     if (HAS_DLGFRAME( style, exstyle ))
773     {
774         xinc = GetSystemMetrics(SM_CXDLGFRAME);
775         yinc = GetSystemMetrics(SM_CYDLGFRAME);
776     }
777     else
778     {
779         xinc = yinc = 0;
780         if (HAS_THICKFRAME(style))
781         {
782             xinc += GetSystemMetrics(SM_CXFRAME);
783             yinc += GetSystemMetrics(SM_CYFRAME);
784         }
785         if (style & WS_BORDER)
786         {
787             xinc += GetSystemMetrics(SM_CXBORDER);
788             yinc += GetSystemMetrics(SM_CYBORDER);
789         }
790     }
791     MinMax.ptMaxSize.x += 2 * xinc;
792     MinMax.ptMaxSize.y += 2 * yinc;
793
794     lpPos = get_internal_pos( hwnd );
795     if( lpPos && !EMPTYPOINT(lpPos->ptMaxPos) )
796     {
797         MinMax.ptMaxPosition.x = lpPos->ptMaxPos.x;
798         MinMax.ptMaxPosition.y = lpPos->ptMaxPos.y;
799     }
800     else
801     {
802         MinMax.ptMaxPosition.x = -xinc;
803         MinMax.ptMaxPosition.y = -yinc;
804     }
805
806     SendMessageW( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
807
808     /* if the app didn't change the values, adapt them for the current monitor */
809
810     if ((monitor = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY )))
811     {
812         MONITORINFO mon_info;
813
814         mon_info.cbSize = sizeof(mon_info);
815         GetMonitorInfoW( monitor, &mon_info );
816
817         if (MinMax.ptMaxSize.x == GetSystemMetrics(SM_CXSCREEN) + 2 * xinc &&
818             MinMax.ptMaxSize.y == GetSystemMetrics(SM_CYSCREEN) + 2 * yinc)
819         {
820             MinMax.ptMaxSize.x = (mon_info.rcWork.right - mon_info.rcWork.left) + 2 * xinc;
821             MinMax.ptMaxSize.y = (mon_info.rcWork.bottom - mon_info.rcWork.top) + 2 * yinc;
822         }
823         if (MinMax.ptMaxPosition.x == -xinc && MinMax.ptMaxPosition.y == -yinc)
824         {
825             MinMax.ptMaxPosition.x = mon_info.rcWork.left - xinc;
826             MinMax.ptMaxPosition.y = mon_info.rcWork.top - yinc;
827         }
828     }
829
830       /* Some sanity checks */
831
832     TRACE("%d %d / %d %d / %d %d / %d %d\n",
833                       MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
834                       MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
835                       MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
836                       MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
837     MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
838                                    MinMax.ptMinTrackSize.x );
839     MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
840                                    MinMax.ptMinTrackSize.y );
841
842     if (maxSize) *maxSize = MinMax.ptMaxSize;
843     if (maxPos) *maxPos = MinMax.ptMaxPosition;
844     if (minTrack) *minTrack = MinMax.ptMinTrackSize;
845     if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
846 }
847
848 /***********************************************************************
849  *              ShowWindowAsync (USER32.@)
850  *
851  * doesn't wait; returns immediately.
852  * used by threads to toggle windows in other (possibly hanging) threads
853  */
854 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
855 {
856     HWND full_handle;
857
858     if (is_broadcast(hwnd))
859     {
860         SetLastError( ERROR_INVALID_PARAMETER );
861         return FALSE;
862     }
863
864     if ((full_handle = WIN_IsCurrentThread( hwnd )))
865         return USER_Driver->pShowWindow( full_handle, cmd );
866
867     return SendNotifyMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
868 }
869
870
871 /***********************************************************************
872  *              ShowWindow (USER32.@)
873  */
874 BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
875 {
876     HWND full_handle;
877
878     if (is_broadcast(hwnd))
879     {
880         SetLastError( ERROR_INVALID_PARAMETER );
881         return FALSE;
882     }
883     if ((full_handle = WIN_IsCurrentThread( hwnd )))
884         return USER_Driver->pShowWindow( full_handle, cmd );
885
886     return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
887 }
888
889
890 /***********************************************************************
891  *              GetInternalWindowPos (USER32.@)
892  */
893 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
894                                       LPPOINT ptIcon )
895 {
896     WINDOWPLACEMENT wndpl;
897     if (GetWindowPlacement( hwnd, &wndpl ))
898     {
899         if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
900         if (ptIcon)  *ptIcon = wndpl.ptMinPosition;
901         return wndpl.showCmd;
902     }
903     return 0;
904 }
905
906
907 /***********************************************************************
908  *              GetWindowPlacement (USER32.@)
909  *
910  * Win95:
911  * Fails if wndpl->length of Win95 (!) apps is invalid.
912  */
913 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
914 {
915     WND *pWnd = WIN_GetPtr( hwnd );
916     LPINTERNALPOS lpPos;
917
918     if (!pWnd || pWnd == WND_DESKTOP) return FALSE;
919     if (pWnd == WND_OTHER_PROCESS)
920     {
921         if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
922         return FALSE;
923     }
924
925     lpPos = WINPOS_InitInternalPos( pWnd );
926     wndpl->length  = sizeof(*wndpl);
927     if( pWnd->dwStyle & WS_MINIMIZE )
928         wndpl->showCmd = SW_SHOWMINIMIZED;
929     else
930         wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
931     if( pWnd->flags & WIN_RESTORE_MAX )
932         wndpl->flags = WPF_RESTORETOMAXIMIZED;
933     else
934         wndpl->flags = 0;
935     wndpl->ptMinPosition.x = lpPos->ptIconPos.x;
936     wndpl->ptMinPosition.y = lpPos->ptIconPos.y;
937     wndpl->ptMaxPosition.x = lpPos->ptMaxPos.x;
938     wndpl->ptMaxPosition.y = lpPos->ptMaxPos.y;
939     wndpl->rcNormalPosition.left   = lpPos->rectNormal.left;
940     wndpl->rcNormalPosition.top    = lpPos->rectNormal.top;
941     wndpl->rcNormalPosition.right  = lpPos->rectNormal.right;
942     wndpl->rcNormalPosition.bottom = lpPos->rectNormal.bottom;
943     WIN_ReleasePtr( pWnd );
944     return TRUE;
945 }
946
947
948 /***********************************************************************
949  *           WINPOS_SetPlacement
950  */
951 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
952 {
953     LPINTERNALPOS lpPos;
954     DWORD style;
955     WND *pWnd = WIN_GetPtr( hwnd );
956
957     if (!pWnd || pWnd == WND_OTHER_PROCESS || pWnd == WND_DESKTOP) return FALSE;
958     lpPos = WINPOS_InitInternalPos( pWnd );
959
960     if( flags & PLACE_MIN )
961     {
962         lpPos->ptIconPos.x = wndpl->ptMinPosition.x;
963         lpPos->ptIconPos.y = wndpl->ptMinPosition.y;
964     }
965     if( flags & PLACE_MAX )
966     {
967         lpPos->ptMaxPos.x = wndpl->ptMaxPosition.x;
968         lpPos->ptMaxPos.y = wndpl->ptMaxPosition.y;
969     }
970     if( flags & PLACE_RECT)
971     {
972         lpPos->rectNormal.left   = wndpl->rcNormalPosition.left;
973         lpPos->rectNormal.top    = wndpl->rcNormalPosition.top;
974         lpPos->rectNormal.right  = wndpl->rcNormalPosition.right;
975         lpPos->rectNormal.bottom = wndpl->rcNormalPosition.bottom;
976     }
977
978     style = pWnd->dwStyle;
979     WIN_ReleasePtr( pWnd );
980
981     if( style & WS_MINIMIZE )
982     {
983         WINPOS_ShowIconTitle( hwnd, FALSE );
984         if( wndpl->flags & WPF_SETMINPOSITION && !EMPTYPOINT(lpPos->ptIconPos))
985             SetWindowPos( hwnd, 0, lpPos->ptIconPos.x, lpPos->ptIconPos.y,
986                           0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
987     }
988     else if( style & WS_MAXIMIZE )
989     {
990         if( !EMPTYPOINT(lpPos->ptMaxPos) )
991             SetWindowPos( hwnd, 0, lpPos->ptMaxPos.x, lpPos->ptMaxPos.y,
992                           0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
993     }
994     else if( flags & PLACE_RECT )
995         SetWindowPos( hwnd, 0, lpPos->rectNormal.left, lpPos->rectNormal.top,
996                       lpPos->rectNormal.right - lpPos->rectNormal.left,
997                       lpPos->rectNormal.bottom - lpPos->rectNormal.top,
998                       SWP_NOZORDER | SWP_NOACTIVATE );
999
1000     ShowWindow( hwnd, wndpl->showCmd );
1001
1002     if (IsIconic( hwnd ))
1003     {
1004         if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE) WINPOS_ShowIconTitle( hwnd, TRUE );
1005
1006         /* SDK: ...valid only the next time... */
1007         if( wndpl->flags & WPF_RESTORETOMAXIMIZED )
1008         {
1009             pWnd = WIN_GetPtr( hwnd );
1010             if (pWnd && pWnd != WND_OTHER_PROCESS)
1011             {
1012                 pWnd->flags |= WIN_RESTORE_MAX;
1013                 WIN_ReleasePtr( pWnd );
1014             }
1015         }
1016     }
1017     return TRUE;
1018 }
1019
1020
1021 /***********************************************************************
1022  *              SetWindowPlacement (USER32.@)
1023  *
1024  * Win95:
1025  * Fails if wndpl->length of Win95 (!) apps is invalid.
1026  */
1027 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
1028 {
1029     if (!wpl) return FALSE;
1030     return WINPOS_SetPlacement( hwnd, wpl, PLACE_MIN | PLACE_MAX | PLACE_RECT );
1031 }
1032
1033
1034 /***********************************************************************
1035  *              AnimateWindow (USER32.@)
1036  *              Shows/Hides a window with an animation
1037  *              NO ANIMATION YET
1038  */
1039 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
1040 {
1041         FIXME("partial stub\n");
1042
1043         /* If trying to show/hide and it's already   *
1044          * shown/hidden or invalid window, fail with *
1045          * invalid parameter                         */
1046         if(!IsWindow(hwnd) ||
1047            (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
1048            (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
1049         {
1050                 SetLastError(ERROR_INVALID_PARAMETER);
1051                 return FALSE;
1052         }
1053
1054         ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1055
1056         return TRUE;
1057 }
1058
1059 /***********************************************************************
1060  *              SetInternalWindowPos (USER32.@)
1061  */
1062 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1063                                     LPRECT rect, LPPOINT pt )
1064 {
1065     if( IsWindow(hwnd) )
1066     {
1067         WINDOWPLACEMENT wndpl;
1068         UINT flags;
1069
1070         wndpl.length  = sizeof(wndpl);
1071         wndpl.showCmd = showCmd;
1072         wndpl.flags = flags = 0;
1073
1074         if( pt )
1075         {
1076             flags |= PLACE_MIN;
1077             wndpl.flags |= WPF_SETMINPOSITION;
1078             wndpl.ptMinPosition = *pt;
1079         }
1080         if( rect )
1081         {
1082             flags |= PLACE_RECT;
1083             wndpl.rcNormalPosition = *rect;
1084         }
1085         WINPOS_SetPlacement( hwnd, &wndpl, flags );
1086     }
1087 }
1088
1089
1090 /*******************************************************************
1091  *         can_activate_window
1092  *
1093  * Check if we can activate the specified window.
1094  */
1095 static BOOL can_activate_window( HWND hwnd )
1096 {
1097     LONG style;
1098
1099     if (!hwnd) return FALSE;
1100     style = GetWindowLongW( hwnd, GWL_STYLE );
1101     if (!(style & WS_VISIBLE)) return FALSE;
1102     if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1103     return !(style & WS_DISABLED);
1104 }
1105
1106
1107 /*******************************************************************
1108  *         WINPOS_ActivateOtherWindow
1109  *
1110  *  Activates window other than pWnd.
1111  */
1112 void WINPOS_ActivateOtherWindow(HWND hwnd)
1113 {
1114     HWND hwndTo, fg;
1115
1116     if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1117     {
1118         hwndTo = GetAncestor( hwndTo, GA_ROOT );
1119         if (can_activate_window( hwndTo )) goto done;
1120     }
1121
1122     hwndTo = hwnd;
1123     for (;;)
1124     {
1125         if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1126         if (can_activate_window( hwndTo )) break;
1127     }
1128
1129  done:
1130     fg = GetForegroundWindow();
1131     TRACE("win = %p fg = %p\n", hwndTo, fg);
1132     if (!fg || (hwnd == fg))
1133     {
1134         if (SetForegroundWindow( hwndTo )) return;
1135     }
1136     if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1137 }
1138
1139
1140 /***********************************************************************
1141  *           WINPOS_HandleWindowPosChanging
1142  *
1143  * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1144  */
1145 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1146 {
1147     POINT minTrack, maxTrack;
1148     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1149
1150     if (winpos->flags & SWP_NOSIZE) return 0;
1151     if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1152     {
1153         WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1154         if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1155         if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1156         if (!(style & WS_MINIMIZE))
1157         {
1158             if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1159             if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1160         }
1161     }
1162     return 0;
1163 }
1164
1165
1166 /***********************************************************************
1167  *           dump_winpos_flags
1168  */
1169 static void dump_winpos_flags(UINT flags)
1170 {
1171     TRACE("flags:");
1172     if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1173     if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1174     if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1175     if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1176     if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1177     if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1178     if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1179     if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1180     if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1181     if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1182     if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1183     if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1184     if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1185
1186 #define DUMPED_FLAGS \
1187     (SWP_NOSIZE | \
1188     SWP_NOMOVE | \
1189     SWP_NOZORDER | \
1190     SWP_NOREDRAW | \
1191     SWP_NOACTIVATE | \
1192     SWP_FRAMECHANGED | \
1193     SWP_SHOWWINDOW | \
1194     SWP_HIDEWINDOW | \
1195     SWP_NOCOPYBITS | \
1196     SWP_NOOWNERZORDER | \
1197     SWP_NOSENDCHANGING | \
1198     SWP_DEFERERASE | \
1199     SWP_ASYNCWINDOWPOS)
1200
1201     if(flags & ~DUMPED_FLAGS) TRACE(" %08x", flags & ~DUMPED_FLAGS);
1202     TRACE("\n");
1203 #undef DUMPED_FLAGS
1204 }
1205
1206 /***********************************************************************
1207  *              SetWindowPos (USER32.@)
1208  */
1209 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
1210                           INT x, INT y, INT cx, INT cy, UINT flags )
1211 {
1212     WINDOWPOS winpos;
1213
1214     TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1215           hwnd, hwndInsertAfter, x, y, cx, cy, flags);
1216     if(TRACE_ON(win)) dump_winpos_flags(flags);
1217
1218     if (is_broadcast(hwnd))
1219     {
1220         SetLastError( ERROR_INVALID_PARAMETER );
1221         return FALSE;
1222     }
1223
1224     winpos.hwnd = WIN_GetFullHandle(hwnd);
1225     winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
1226     winpos.x = x;
1227     winpos.y = y;
1228     winpos.cx = cx;
1229     winpos.cy = cy;
1230     winpos.flags = flags;
1231     if (WIN_IsCurrentThread( hwnd ))
1232         return USER_Driver->pSetWindowPos( &winpos );
1233
1234     return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
1235 }
1236
1237
1238 /***********************************************************************
1239  *              BeginDeferWindowPos (USER32.@)
1240  */
1241 HDWP WINAPI BeginDeferWindowPos( INT count )
1242 {
1243     HDWP handle;
1244     DWP *pDWP;
1245
1246     TRACE("%d\n", count);
1247
1248     if (count < 0)
1249     {
1250         SetLastError(ERROR_INVALID_PARAMETER);
1251         return 0;
1252     }
1253     /* Windows allows zero count, in which case it allocates context for 8 moves */
1254     if (count == 0) count = 8;
1255
1256     handle = USER_HEAP_ALLOC( sizeof(DWP) + (count-1)*sizeof(WINDOWPOS) );
1257     if (!handle) return 0;
1258     pDWP = (DWP *) USER_HEAP_LIN_ADDR( handle );
1259     pDWP->actualCount    = 0;
1260     pDWP->suggestedCount = count;
1261     pDWP->valid          = TRUE;
1262     pDWP->wMagic         = DWP_MAGIC;
1263     pDWP->hwndParent     = 0;
1264
1265     TRACE("returning hdwp %p\n", handle);
1266     return handle;
1267 }
1268
1269
1270 /***********************************************************************
1271  *              DeferWindowPos (USER32.@)
1272  */
1273 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
1274                                 INT x, INT y, INT cx, INT cy,
1275                                 UINT flags )
1276 {
1277     DWP *pDWP;
1278     int i;
1279     HDWP newhdwp = hdwp,retvalue;
1280
1281     TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1282           hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
1283
1284     hwnd = WIN_GetFullHandle( hwnd );
1285     if (hwnd == GetDesktopWindow()) return 0;
1286
1287     if (!(pDWP = USER_HEAP_LIN_ADDR( hdwp ))) return 0;
1288
1289     USER_Lock();
1290
1291     for (i = 0; i < pDWP->actualCount; i++)
1292     {
1293         if (pDWP->winPos[i].hwnd == hwnd)
1294         {
1295               /* Merge with the other changes */
1296             if (!(flags & SWP_NOZORDER))
1297             {
1298                 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
1299             }
1300             if (!(flags & SWP_NOMOVE))
1301             {
1302                 pDWP->winPos[i].x = x;
1303                 pDWP->winPos[i].y = y;
1304             }
1305             if (!(flags & SWP_NOSIZE))
1306             {
1307                 pDWP->winPos[i].cx = cx;
1308                 pDWP->winPos[i].cy = cy;
1309             }
1310             pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
1311                                                SWP_NOZORDER | SWP_NOREDRAW |
1312                                                SWP_NOACTIVATE | SWP_NOCOPYBITS|
1313                                                SWP_NOOWNERZORDER);
1314             pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
1315                                               SWP_FRAMECHANGED);
1316             retvalue = hdwp;
1317             goto END;
1318         }
1319     }
1320     if (pDWP->actualCount >= pDWP->suggestedCount)
1321     {
1322         newhdwp = USER_HEAP_REALLOC( hdwp,
1323                       sizeof(DWP) + pDWP->suggestedCount*sizeof(WINDOWPOS) );
1324         if (!newhdwp)
1325         {
1326             retvalue = 0;
1327             goto END;
1328         }
1329         pDWP = (DWP *) USER_HEAP_LIN_ADDR( newhdwp );
1330         pDWP->suggestedCount++;
1331     }
1332     pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
1333     pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
1334     pDWP->winPos[pDWP->actualCount].x = x;
1335     pDWP->winPos[pDWP->actualCount].y = y;
1336     pDWP->winPos[pDWP->actualCount].cx = cx;
1337     pDWP->winPos[pDWP->actualCount].cy = cy;
1338     pDWP->winPos[pDWP->actualCount].flags = flags;
1339     pDWP->actualCount++;
1340     retvalue = newhdwp;
1341 END:
1342     USER_Unlock();
1343     return retvalue;
1344 }
1345
1346
1347 /***********************************************************************
1348  *              EndDeferWindowPos (USER32.@)
1349  */
1350 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
1351 {
1352     DWP *pDWP;
1353     WINDOWPOS *winpos;
1354     BOOL res = TRUE;
1355     int i;
1356
1357     TRACE("%p\n", hdwp);
1358
1359     pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
1360     if (!pDWP) return FALSE;
1361     for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
1362     {
1363         TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1364                winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
1365                winpos->cx, winpos->cy, winpos->flags);
1366
1367         if (!(res = USER_Driver->pSetWindowPos( winpos ))) break;
1368     }
1369     USER_HEAP_FREE( hdwp );
1370     return res;
1371 }
1372
1373
1374 /***********************************************************************
1375  *              TileChildWindows (USER.199)
1376  */
1377 void WINAPI TileChildWindows16( HWND16 parent, WORD action )
1378 {
1379     FIXME("(%04x, %d): stub\n", parent, action);
1380 }
1381
1382 /***********************************************************************
1383  *              CascadeChildWindows (USER.198)
1384  */
1385 void WINAPI CascadeChildWindows16( HWND16 parent, WORD action )
1386 {
1387     FIXME("(%04x, %d): stub\n", parent, action);
1388 }