Release 960623
[wine] / windows / winpos.c
1 /*
2  * Window position related functions.
3  *
4  * Copyright 1993, 1994, 1995 Alexandre Julliard
5  *                       1995,1996 Alex Korobka
6  */
7
8 #include "sysmetrics.h"
9 #include "heap.h"
10 #include "module.h"
11 #include "user.h"
12 #include "win.h"
13 #include "hook.h"
14 #include "message.h"
15 #include "queue.h"
16 #include "stackframe.h"
17 #include "winpos.h"
18 #include "dce.h"
19 #include "nonclient.h"
20 #include "stddebug.h"
21 /* #define DEBUG_WIN */
22 #include "debug.h"
23
24 #define  SWP_NOPOSCHANGE        (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE)
25
26 /* ----- external functions ----- */
27
28 extern void     FOCUS_SwitchFocus( HWND , HWND );
29 extern HRGN     DCE_GetVisRgn( HWND, WORD );
30 extern HWND     CARET_GetHwnd();
31 extern BOOL     DCE_InvalidateDCE(WND*, RECT16* );
32
33 /* ----- internal variables ----- */
34
35 static HWND hwndActive      = 0;  /* Currently active window */
36 static HWND hwndPrevActive  = 0;  /* Previously active window */
37
38 /***********************************************************************
39  *           WINPOS_FindIconPos
40  *
41  * Find a suitable place for an iconic window.
42  * The new position is stored into wndPtr->ptIconPos.
43  */
44 void WINPOS_FindIconPos( HWND hwnd )
45 {
46     RECT16 rectParent;
47     short x, y, xspacing, yspacing;
48     WND * wndPtr = WIN_FindWndPtr( hwnd );
49
50     if (!wndPtr || !wndPtr->parent) return;
51     GetClientRect16( wndPtr->parent->hwndSelf, &rectParent );
52     if ((wndPtr->ptIconPos.x >= rectParent.left) &&
53         (wndPtr->ptIconPos.x + SYSMETRICS_CXICON < rectParent.right) &&
54         (wndPtr->ptIconPos.y >= rectParent.top) &&
55         (wndPtr->ptIconPos.y + SYSMETRICS_CYICON < rectParent.bottom))
56         return;  /* The icon already has a suitable position */
57
58     xspacing = yspacing = 70;  /* FIXME: This should come from WIN.INI */
59     y = rectParent.bottom;
60     for (;;)
61     {
62         for (x = rectParent.left; x<=rectParent.right-xspacing; x += xspacing)
63         {
64               /* Check if another icon already occupies this spot */
65             WND *childPtr = wndPtr->parent->child;
66             while (childPtr)
67             {
68                 if ((childPtr->dwStyle & WS_MINIMIZE) && (childPtr != wndPtr))
69                 {
70                     if ((childPtr->rectWindow.left < x + xspacing) &&
71                         (childPtr->rectWindow.right >= x) &&
72                         (childPtr->rectWindow.top <= y) &&
73                         (childPtr->rectWindow.bottom > y - yspacing))
74                         break;  /* There's a window in there */
75                 }
76                 childPtr = childPtr->next;
77             }
78             if (!childPtr)
79             {
80                   /* No window was found, so it's OK for us */
81                 wndPtr->ptIconPos.x = x + (xspacing - SYSMETRICS_CXICON) / 2;
82                 wndPtr->ptIconPos.y = y - (yspacing + SYSMETRICS_CYICON) / 2;
83                 return;
84             }
85         }
86         y -= yspacing;
87     }
88 }
89
90
91 /***********************************************************************
92  *           ArrangeIconicWindows   (USER.170)
93  */
94 UINT ArrangeIconicWindows( HWND parent )
95 {
96     RECT16 rectParent;
97     HWND hwndChild;
98     INT x, y, xspacing, yspacing;
99
100     GetClientRect16( parent, &rectParent );
101     x = rectParent.left;
102     y = rectParent.bottom;
103     xspacing = yspacing = 70;  /* FIXME: This should come from WIN.INI */
104     hwndChild = GetWindow( parent, GW_CHILD );
105     while (hwndChild)
106     {
107         if (IsIconic( hwndChild ))
108         {
109             SetWindowPos( hwndChild, 0, x + (xspacing - SYSMETRICS_CXICON) / 2,
110                           y - (yspacing + SYSMETRICS_CYICON) / 2, 0, 0,
111                           SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
112             if (x <= rectParent.right - xspacing) x += xspacing;
113             else
114             {
115                 x = rectParent.left;
116                 y -= yspacing;
117             }
118         }
119         hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
120     }
121     return yspacing;
122 }
123
124
125 /***********************************************************************
126  *           GetWindowRect16   (USER.32)
127  */
128 void GetWindowRect16( HWND16 hwnd, LPRECT16 rect ) 
129 {
130     WND * wndPtr = WIN_FindWndPtr( hwnd ); 
131     if (!wndPtr) return;
132     
133     *rect = wndPtr->rectWindow;
134     if (wndPtr->dwStyle & WS_CHILD)
135         MapWindowPoints16( wndPtr->parent->hwndSelf, 0, (POINT16 *)rect, 2 );
136 }
137
138
139 /***********************************************************************
140  *           GetWindowRect32   (USER.32)
141  */
142 void GetWindowRect32( HWND32 hwnd, LPRECT32 rect ) 
143 {
144     WND * wndPtr = WIN_FindWndPtr( hwnd ); 
145     if (!wndPtr) return;
146     
147     CONV_RECT16TO32( &wndPtr->rectWindow, rect );
148     if (wndPtr->dwStyle & WS_CHILD)
149         MapWindowPoints32( wndPtr->parent->hwndSelf, 0, (POINT32 *)rect, 2 );
150 }
151
152
153 /***********************************************************************
154  *           GetClientRect16   (USER.33)
155  */
156 void GetClientRect16( HWND16 hwnd, LPRECT16 rect ) 
157 {
158     WND * wndPtr = WIN_FindWndPtr( hwnd );
159
160     rect->left = rect->top = rect->right = rect->bottom = 0;
161     if (wndPtr) 
162     {
163         rect->right  = wndPtr->rectClient.right - wndPtr->rectClient.left;
164         rect->bottom = wndPtr->rectClient.bottom - wndPtr->rectClient.top;
165     }
166 }
167
168
169 /***********************************************************************
170  *           GetClientRect32   (USER32.219)
171  */
172 void GetClientRect32( HWND32 hwnd, LPRECT32 rect ) 
173 {
174     WND * wndPtr = WIN_FindWndPtr( hwnd );
175
176     rect->left = rect->top = rect->right = rect->bottom = 0;
177     if (wndPtr) 
178     {
179         rect->right  = wndPtr->rectClient.right - wndPtr->rectClient.left;
180         rect->bottom = wndPtr->rectClient.bottom - wndPtr->rectClient.top;
181     }
182 }
183
184
185 /*******************************************************************
186  *         ClientToScreen16   (USER.28)
187  */
188 BOOL16 ClientToScreen16( HWND16 hwnd, LPPOINT16 lppnt )
189 {
190     MapWindowPoints16( hwnd, 0, lppnt, 1 );
191     return TRUE;
192 }
193
194
195 /*******************************************************************
196  *         ClientToScreen32   (USER32.51)
197  */
198 BOOL32 ClientToScreen32( HWND32 hwnd, LPPOINT32 lppnt )
199 {
200     MapWindowPoints32( hwnd, 0, lppnt, 1 );
201     return TRUE;
202 }
203
204
205 /*******************************************************************
206  *         ScreenToClient16   (USER.29)
207  */
208 void ScreenToClient16( HWND16 hwnd, LPPOINT16 lppnt )
209 {
210     MapWindowPoints16( 0, hwnd, lppnt, 1 );
211 }
212
213
214 /*******************************************************************
215  *         ScreenToClient32   (USER32.446)
216  */
217 void ScreenToClient32( HWND32 hwnd, LPPOINT32 lppnt )
218 {
219     MapWindowPoints32( 0, hwnd, lppnt, 1 );
220 }
221
222
223 /***********************************************************************
224  *           WINPOS_WindowFromPoint
225  *
226  * Find the window and hittest for a given point.
227  */
228 INT16 WINPOS_WindowFromPoint( POINT16 pt, WND **ppWnd )
229 {
230     WND *wndPtr;
231     INT16 hittest = HTERROR;
232     INT16 x, y;
233
234     *ppWnd = NULL;
235     x = pt.x;
236     y = pt.y;
237     wndPtr = WIN_GetDesktop()->child;
238     for (;;)
239     {
240         while (wndPtr)
241         {
242             /* If point is in window, and window is visible, and it  */
243             /* is enabled (or it's a top-level window), then explore */
244             /* its children. Otherwise, go to the next window.       */
245
246             if ((wndPtr->dwStyle & WS_VISIBLE) &&
247                 (!(wndPtr->dwStyle & WS_DISABLED) ||
248                  ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)) &&
249                 (x >= wndPtr->rectWindow.left) &&
250                 (x < wndPtr->rectWindow.right) &&
251                 (y >= wndPtr->rectWindow.top) &&
252                 (y < wndPtr->rectWindow.bottom))
253             {
254                 *ppWnd = wndPtr;  /* Got a suitable window */
255
256                 /* If window is minimized or disabled, return at once */
257                 if (wndPtr->dwStyle & WS_MINIMIZE) return HTCAPTION;
258                 if (wndPtr->dwStyle & WS_DISABLED) return HTERROR;
259
260                 /* If point is not in client area, ignore the children */
261                 if ((x < wndPtr->rectClient.left) ||
262                     (x >= wndPtr->rectClient.right) ||
263                     (y < wndPtr->rectClient.top) ||
264                     (y >= wndPtr->rectClient.bottom)) break;
265
266                 x -= wndPtr->rectClient.left;
267                 y -= wndPtr->rectClient.top;
268                 wndPtr = wndPtr->child;
269             }
270             else wndPtr = wndPtr->next;
271         }
272
273         /* If nothing found, return the desktop window */
274         if (!*ppWnd)
275         {
276             *ppWnd = WIN_GetDesktop();
277             return HTCLIENT;
278         }
279
280         /* Send the WM_NCHITTEST message (only if to the same task) */
281         if ((*ppWnd)->hmemTaskQ != GetTaskQueue(0)) return HTCLIENT;
282         hittest = (INT)SendMessage16( (*ppWnd)->hwndSelf, WM_NCHITTEST, 0,
283                                     MAKELONG( pt.x, pt.y ) );
284         if (hittest != HTTRANSPARENT) return hittest;  /* Found the window */
285
286         /* If no children found in last search, make point relative to parent*/
287         if (!wndPtr)
288         {
289             x += (*ppWnd)->rectClient.left;
290             y += (*ppWnd)->rectClient.top;
291         }
292
293         /* Restart the search from the next sibling */
294         wndPtr = (*ppWnd)->next;
295         *ppWnd = (*ppWnd)->parent;
296     }
297 }
298
299
300 /*******************************************************************
301  *         WindowFromPoint16   (USER.30)
302  */
303 HWND16  WindowFromPoint16( POINT16 pt )
304 {
305     WND *pWnd;
306     WINPOS_WindowFromPoint( pt, &pWnd );
307     return pWnd->hwndSelf;
308 }
309
310
311 /*******************************************************************
312  *         WindowFromPoint32   (USER32.581)
313  */
314 HWND32 WindowFromPoint32( POINT32 pt )
315 {
316     WND *pWnd;
317     POINT16 pt16;
318     CONV_POINT32TO16( &pt, &pt16 );
319     WINPOS_WindowFromPoint( pt16, &pWnd );
320     return (HWND32)pWnd->hwndSelf;
321 }
322
323
324 /*******************************************************************
325  *         ChildWindowFromPoint16   (USER.191)
326  */
327 HWND16 ChildWindowFromPoint16( HWND16 hwndParent, POINT16 pt )
328 {
329     /* pt is in the client coordinates */
330
331     WND* wnd = WIN_FindWndPtr(hwndParent);
332     RECT16 rect;
333
334     if( !wnd ) return 0;
335
336     /* get client rect fast */
337     rect.top = rect.left = 0;
338     rect.right = wnd->rectClient.right - wnd->rectClient.left;
339     rect.bottom = wnd->rectClient.bottom - wnd->rectClient.top;
340
341     if (!PtInRect16( &rect, pt )) return 0;
342
343     wnd = wnd->child;
344     while ( wnd )
345     {
346         if (PtInRect16( &wnd->rectWindow, pt )) return wnd->hwndSelf;
347         wnd = wnd->next;
348     }
349     return hwndParent;
350 }
351
352
353 /*******************************************************************
354  *         ChildWindowFromPoint32   (USER32.)
355  */
356 HWND32 ChildWindowFromPoint32( HWND32 hwndParent, POINT32 pt )
357 {
358     POINT16 pt16;
359     CONV_POINT32TO16( &pt, &pt16 );
360     return (HWND32)ChildWindowFromPoint16( hwndParent, pt16 );
361 }
362
363
364 /*******************************************************************
365  *         WINPOS_GetWinOffset
366  *
367  * Calculate the offset between the origin of the two windows. Used
368  * to implement MapWindowPoints.
369  */
370 static void WINPOS_GetWinOffset( HWND32 hwndFrom, HWND32 hwndTo,
371                                  POINT32 *offset )
372 {
373     WND * wndPtr;
374
375     offset->x = offset->y = 0;
376     if (hwndFrom == hwndTo ) return;
377
378       /* Translate source window origin to screen coords */
379     if (hwndFrom)
380     {
381         if (!(wndPtr = WIN_FindWndPtr( hwndFrom )))
382         {
383             fprintf(stderr,"MapWindowPoints: bad hwndFrom = %04x\n",hwndFrom);
384             return;
385         }
386         while (wndPtr->parent)
387         {
388             offset->x += wndPtr->rectClient.left;
389             offset->y += wndPtr->rectClient.top;
390             wndPtr = wndPtr->parent;
391         }
392     }
393
394       /* Translate origin to destination window coords */
395     if (hwndTo)
396     {
397         if (!(wndPtr = WIN_FindWndPtr( hwndTo )))
398         {
399             fprintf(stderr,"MapWindowPoints: bad hwndTo = %04x\n", hwndTo );
400             return;
401         }
402         while (wndPtr->parent)
403         {
404             offset->x -= wndPtr->rectClient.left;
405             offset->y -= wndPtr->rectClient.top;
406             wndPtr = wndPtr->parent;
407         }    
408     }
409 }
410
411
412 /*******************************************************************
413  *         MapWindowPoints16   (USER.258)
414  */
415 void MapWindowPoints16( HWND16 hwndFrom, HWND16 hwndTo,
416                         LPPOINT16 lppt, UINT16 count )
417 {
418     POINT32 offset;
419
420     WINPOS_GetWinOffset( hwndFrom, hwndTo, &offset );
421     while (count--)
422     {
423         lppt->x += offset.x;
424         lppt->y += offset.y;
425         lppt++;
426     }
427 }
428
429
430 /*******************************************************************
431  *         MapWindowPoints32   (USER32.385)
432  */
433 void MapWindowPoints32( HWND32 hwndFrom, HWND32 hwndTo,
434                         LPPOINT32 lppt, UINT32 count )
435 {
436     POINT32 offset;
437
438     WINPOS_GetWinOffset( hwndFrom, hwndTo, &offset );
439     while (count--)
440     {
441         lppt->x += offset.x;
442         lppt->y += offset.y;
443         lppt++;
444     }
445 }
446
447
448 /***********************************************************************
449  *           IsIconic   (USER.31)
450  */
451 BOOL IsIconic(HWND hWnd)
452 {
453     WND * wndPtr = WIN_FindWndPtr(hWnd);
454     if (wndPtr == NULL) return FALSE;
455     return (wndPtr->dwStyle & WS_MINIMIZE) != 0;
456 }
457  
458  
459 /***********************************************************************
460  *           IsZoomed   (USER.272)
461  */
462 BOOL IsZoomed(HWND hWnd)
463 {
464     WND * wndPtr = WIN_FindWndPtr(hWnd);
465     if (wndPtr == NULL) return FALSE;
466     return (wndPtr->dwStyle & WS_MAXIMIZE) != 0;
467 }
468
469
470 /*******************************************************************
471  *         GetActiveWindow    (USER.60)
472  */
473 HWND GetActiveWindow()
474 {
475     return hwndActive;
476 }
477
478
479 /*******************************************************************
480  *         SetActiveWindow    (USER.59)
481  */
482 HWND SetActiveWindow( HWND hwnd )
483 {
484     HWND prev = hwndActive;
485     WND *wndPtr = WIN_FindWndPtr( hwnd );
486
487     if (!wndPtr || (wndPtr->dwStyle & WS_DISABLED) ||
488         !(wndPtr->dwStyle & WS_VISIBLE)) return 0;
489
490     WINPOS_SetActiveWindow( hwnd, 0, 0 );
491     return prev;
492 }
493
494
495 /***********************************************************************
496  *           BringWindowToTop   (USER.45)
497  */
498 BOOL BringWindowToTop( HWND hwnd )
499 {
500     return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
501 }
502
503
504 /***********************************************************************
505  *           MoveWindow   (USER.56)
506  */
507 BOOL MoveWindow( HWND hwnd, short x, short y, short cx, short cy, BOOL repaint)
508 {    
509     int flags = SWP_NOZORDER | SWP_NOACTIVATE;
510     if (!repaint) flags |= SWP_NOREDRAW;
511     dprintf_win(stddeb, "MoveWindow: %04x %d,%d %dx%d %d\n", 
512             hwnd, x, y, cx, cy, repaint );
513     return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
514 }
515
516
517 /***********************************************************************
518  *           ShowWindow   (USER.42)
519  */
520 BOOL ShowWindow( HWND hwnd, int cmd ) 
521 {    
522     WND * wndPtr = WIN_FindWndPtr( hwnd );
523     BOOL wasVisible;
524     POINT16 maxSize;
525     int swpflags = 0;
526     short x = 0, y = 0, cx = 0, cy = 0;
527
528     if (!wndPtr) return FALSE;
529
530     dprintf_win(stddeb,"ShowWindow: hwnd=%04x, cmd=%d\n", hwnd, cmd);
531
532     wasVisible = (wndPtr->dwStyle & WS_VISIBLE) != 0;
533
534     switch(cmd)
535     {
536         case SW_HIDE:
537             if (!wasVisible) return FALSE;
538             swpflags |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE | 
539                         SWP_NOACTIVATE | SWP_NOZORDER;
540             break;
541
542         case SW_SHOWMINNOACTIVE:
543             swpflags |= SWP_NOACTIVATE | SWP_NOZORDER;
544             /* fall through */
545         case SW_SHOWMINIMIZED:
546             swpflags |= SWP_SHOWWINDOW;
547             /* fall through */
548         case SW_MINIMIZE:
549             swpflags |= SWP_FRAMECHANGED;
550             if (!(wndPtr->dwStyle & WS_MINIMIZE))
551             {
552                 if (wndPtr->dwStyle & WS_MAXIMIZE)
553                 {
554                     wndPtr->flags |= WIN_RESTORE_MAX;
555                     wndPtr->dwStyle &= ~WS_MAXIMIZE;
556                 }
557                 else
558                 {
559                     wndPtr->flags &= ~WIN_RESTORE_MAX;
560                     wndPtr->rectNormal = wndPtr->rectWindow;
561                 }
562                 wndPtr->dwStyle |= WS_MINIMIZE;
563                 WINPOS_FindIconPos( hwnd );
564                 x  = wndPtr->ptIconPos.x;
565                 y  = wndPtr->ptIconPos.y;
566                 cx = SYSMETRICS_CXICON;
567                 cy = SYSMETRICS_CYICON;
568             }
569             else swpflags |= SWP_NOSIZE | SWP_NOMOVE;
570             break;
571
572         case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE: */
573             swpflags |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
574             if (!(wndPtr->dwStyle & WS_MAXIMIZE))
575             {
576                   /* Store the current position and find the maximized size */
577                 if (!(wndPtr->dwStyle & WS_MINIMIZE))
578                     wndPtr->rectNormal = wndPtr->rectWindow; 
579
580                 NC_GetMinMaxInfo( hwnd, &maxSize,
581                                   &wndPtr->ptMaxPos, NULL, NULL );
582                 x  = wndPtr->ptMaxPos.x;
583                 y  = wndPtr->ptMaxPos.y;
584
585                 if( wndPtr->dwStyle & WS_MINIMIZE )
586                     if( !SendMessage16( hwnd, WM_QUERYOPEN, 0, 0L ) )
587                         {
588                          swpflags |= SWP_NOSIZE;
589                          break;
590                         }
591
592                 cx = maxSize.x;
593                 cy = maxSize.y;
594                 wndPtr->dwStyle &= ~WS_MINIMIZE;
595                 wndPtr->dwStyle |= WS_MAXIMIZE;
596             }
597             else swpflags |= SWP_NOSIZE | SWP_NOMOVE;
598             break;
599
600         case SW_SHOWNA:
601             swpflags |= SWP_NOACTIVATE | SWP_NOZORDER;
602             /* fall through */
603         case SW_SHOW:
604             swpflags |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
605             break;
606
607         case SW_SHOWNOACTIVATE:
608             swpflags |= SWP_NOZORDER;
609             if (GetActiveWindow()) swpflags |= SWP_NOACTIVATE;
610             /* fall through */
611         case SW_SHOWNORMAL:  /* same as SW_NORMAL: */
612         case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
613         case SW_RESTORE:
614             swpflags |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
615
616             if (wndPtr->dwStyle & WS_MINIMIZE)
617             {
618                 if( !SendMessage16( hwnd, WM_QUERYOPEN, 0, 0L) )
619                   {
620                     swpflags |= SWP_NOSIZE;
621                     break;
622                   }
623                 wndPtr->ptIconPos.x = wndPtr->rectWindow.left;
624                 wndPtr->ptIconPos.y = wndPtr->rectWindow.top;
625                 wndPtr->dwStyle &= ~WS_MINIMIZE;
626                 if (wndPtr->flags & WIN_RESTORE_MAX)
627                 {
628                     /* Restore to maximized position */
629                     NC_GetMinMaxInfo( hwnd, &maxSize, &wndPtr->ptMaxPos,
630                                       NULL, NULL );
631                     x  = wndPtr->ptMaxPos.x;
632                     y  = wndPtr->ptMaxPos.y;
633                     cx = maxSize.x;
634                     cy = maxSize.y;
635                    wndPtr->dwStyle |= WS_MAXIMIZE;
636                 }
637                 else  /* Restore to normal position */
638                 {
639                     x  = wndPtr->rectNormal.left;
640                     y  = wndPtr->rectNormal.top;
641                     cx = wndPtr->rectNormal.right - wndPtr->rectNormal.left;
642                     cy = wndPtr->rectNormal.bottom - wndPtr->rectNormal.top;
643                 }
644             }
645             else if (wndPtr->dwStyle & WS_MAXIMIZE)
646             {
647                 wndPtr->ptMaxPos.x = wndPtr->rectWindow.left;
648                 wndPtr->ptMaxPos.y = wndPtr->rectWindow.top;
649                 wndPtr->dwStyle &= ~WS_MAXIMIZE;
650                 x  = wndPtr->rectNormal.left;
651                 y  = wndPtr->rectNormal.top;
652                 cx = wndPtr->rectNormal.right - wndPtr->rectNormal.left;
653                 cy = wndPtr->rectNormal.bottom - wndPtr->rectNormal.top;
654             }
655             else swpflags |= SWP_NOSIZE | SWP_NOMOVE;
656             break;
657     }
658
659     SendMessage16( hwnd, WM_SHOWWINDOW, (cmd != SW_HIDE), 0 );
660     SetWindowPos( hwnd, HWND_TOP, x, y, cx, cy, swpflags );
661
662     if (wndPtr->flags & WIN_NEED_SIZE)
663     {
664         int wParam = SIZE_RESTORED;
665
666         wndPtr->flags &= ~WIN_NEED_SIZE;
667         if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
668         else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
669         SendMessage16( hwnd, WM_SIZE, wParam,
670                      MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
671                             wndPtr->rectClient.bottom-wndPtr->rectClient.top));
672         SendMessage16( hwnd, WM_MOVE, 0,
673                    MAKELONG(wndPtr->rectClient.left, wndPtr->rectClient.top) );
674     }
675
676     if (hwnd == GetFocus())
677     { 
678         SetFocus( (wndPtr->dwStyle & WS_CHILD)? wndPtr->parent->hwndSelf: 0 );
679         if (hwnd == CARET_GetHwnd()) DestroyCaret();
680     }
681
682     return wasVisible;
683 }
684
685
686 /***********************************************************************
687  *           GetInternalWindowPos16   (USER.460)
688  */
689 UINT16 GetInternalWindowPos16( HWND16 hwnd, LPRECT16 rectWnd, LPPOINT16 ptIcon)
690 {
691     WINDOWPLACEMENT16 wndpl;
692     if (!GetWindowPlacement16( hwnd, &wndpl )) return 0;
693     if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
694     if (ptIcon)  *ptIcon = wndpl.ptMinPosition;
695     return wndpl.showCmd;
696 }
697
698
699 /***********************************************************************
700  *           GetInternalWindowPos32   (USER32.244)
701  */
702 UINT32 GetInternalWindowPos32( HWND32 hwnd, LPRECT32 rectWnd, LPPOINT32 ptIcon)
703 {
704     WINDOWPLACEMENT32 wndpl;
705     if (!GetWindowPlacement32( hwnd, &wndpl )) return 0;
706     if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
707     if (ptIcon)  *ptIcon = wndpl.ptMinPosition;
708     return wndpl.showCmd;
709 }
710
711
712 /***********************************************************************
713  *           SetInternalWindowPos16   (USER.461)
714  */
715 void SetInternalWindowPos16( HWND16 hwnd, UINT16 showCmd,
716                              LPRECT16 rect, LPPOINT16 pt )
717 {
718     WINDOWPLACEMENT16 wndpl;
719     WND *wndPtr = WIN_FindWndPtr( hwnd );
720
721     wndpl.length  = sizeof(wndpl);
722     wndpl.flags   = (pt != NULL) ? WPF_SETMINPOSITION : 0;
723     wndpl.showCmd = showCmd;
724     if (pt) wndpl.ptMinPosition = *pt;
725     wndpl.rcNormalPosition = (rect != NULL) ? *rect : wndPtr->rectNormal;
726     wndpl.ptMaxPosition = wndPtr->ptMaxPos;
727     SetWindowPlacement16( hwnd, &wndpl );
728 }
729
730
731 /***********************************************************************
732  *           SetInternalWindowPos32   (USER32.482)
733  */
734 void SetInternalWindowPos32( HWND32 hwnd, UINT32 showCmd,
735                              LPRECT32 rect, LPPOINT32 pt )
736 {
737     WINDOWPLACEMENT32 wndpl;
738     WND *wndPtr = WIN_FindWndPtr( hwnd );
739
740     wndpl.length  = sizeof(wndpl);
741     wndpl.flags   = (pt != NULL) ? WPF_SETMINPOSITION : 0;
742     wndpl.showCmd = showCmd;
743     if (pt) wndpl.ptMinPosition = *pt;
744     if (rect) wndpl.rcNormalPosition = *rect;
745     else CONV_RECT16TO32( &wndPtr->rectNormal, &wndpl.rcNormalPosition );
746     CONV_POINT16TO32( &wndPtr->ptMaxPos, &wndpl.ptMaxPosition );
747     SetWindowPlacement32( hwnd, &wndpl );
748 }
749
750
751 /***********************************************************************
752  *           GetWindowPlacement16   (USER.370)
753  */
754 BOOL16 GetWindowPlacement16( HWND16 hwnd, WINDOWPLACEMENT16 *wndpl )
755 {
756     WND *wndPtr = WIN_FindWndPtr( hwnd );
757     if (!wndPtr) return FALSE;
758
759     wndpl->length  = sizeof(*wndpl);
760     wndpl->flags   = 0;
761     wndpl->showCmd = IsZoomed(hwnd) ? SW_SHOWMAXIMIZED : 
762                      (IsIconic(hwnd) ? SW_SHOWMINIMIZED : SW_SHOWNORMAL);
763     wndpl->ptMinPosition = wndPtr->ptIconPos;
764     wndpl->ptMaxPosition = wndPtr->ptMaxPos;
765     wndpl->rcNormalPosition = wndPtr->rectNormal;
766     return TRUE;
767 }
768
769
770 /***********************************************************************
771  *           GetWindowPlacement32   (USER32.306)
772  */
773 BOOL32 GetWindowPlacement32( HWND32 hwnd, WINDOWPLACEMENT32 *wndpl )
774 {
775     WND *wndPtr = WIN_FindWndPtr( hwnd );
776     if (!wndPtr) return FALSE;
777
778     wndpl->length  = sizeof(*wndpl);
779     wndpl->flags   = 0;
780     wndpl->showCmd = IsZoomed(hwnd) ? SW_SHOWMAXIMIZED : 
781                      (IsIconic(hwnd) ? SW_SHOWMINIMIZED : SW_SHOWNORMAL);
782     CONV_POINT16TO32( &wndPtr->ptIconPos, &wndpl->ptMinPosition );
783     CONV_POINT16TO32( &wndPtr->ptMaxPos, &wndpl->ptMaxPosition );
784     CONV_RECT16TO32( &wndPtr->rectNormal, &wndpl->rcNormalPosition );
785     return TRUE;
786 }
787
788
789 /***********************************************************************
790  *           SetWindowPlacement16   (USER.371)
791  */
792 BOOL16 SetWindowPlacement16( HWND16 hwnd, const WINDOWPLACEMENT16 *wndpl )
793 {
794     WND *wndPtr = WIN_FindWndPtr( hwnd );
795     if (!wndPtr) return FALSE;
796
797     if (wndpl->flags & WPF_SETMINPOSITION)
798         wndPtr->ptIconPos = wndpl->ptMinPosition;
799     if ((wndpl->flags & WPF_RESTORETOMAXIMIZED) &&
800         (wndpl->showCmd == SW_SHOWMINIMIZED)) wndPtr->flags |= WIN_RESTORE_MAX;
801     wndPtr->ptMaxPos   = wndpl->ptMaxPosition;
802     wndPtr->rectNormal = wndpl->rcNormalPosition;
803     ShowWindow( hwnd, wndpl->showCmd );
804     return TRUE;
805 }
806
807
808 /***********************************************************************
809  *           SetWindowPlacement32   (USER32.518)
810  */
811 BOOL32 SetWindowPlacement32( HWND32 hwnd, const WINDOWPLACEMENT32 *wndpl )
812 {
813     WND *wndPtr = WIN_FindWndPtr( hwnd );
814     if (!wndPtr) return FALSE;
815
816     if (wndpl->flags & WPF_SETMINPOSITION)
817         CONV_POINT32TO16( &wndpl->ptMinPosition, &wndPtr->ptIconPos );
818     if ((wndpl->flags & WPF_RESTORETOMAXIMIZED) &&
819         (wndpl->showCmd == SW_SHOWMINIMIZED)) wndPtr->flags |= WIN_RESTORE_MAX;
820     CONV_POINT32TO16( &wndpl->ptMaxPosition, &wndPtr->ptMaxPos );
821     CONV_RECT32TO16( &wndpl->rcNormalPosition, &wndPtr->rectNormal );
822     ShowWindow( hwnd, wndpl->showCmd );
823     return TRUE;
824 }
825
826
827 /*******************************************************************
828  *         ACTIVATEAPP_callback
829  */
830 BOOL ACTIVATEAPP_callback(HWND hWnd, LPARAM lParam)
831 {
832     ACTIVATESTRUCT  *lpActStruct = (ACTIVATESTRUCT*)lParam;
833  
834     if (GetWindowTask(hWnd) != lpActStruct->hTaskSendTo) return 1;
835
836     SendMessage16( hWnd, WM_ACTIVATEAPP, lpActStruct->wFlag,
837                 (LPARAM)((lpActStruct->hWindowTask)?lpActStruct->hWindowTask:0));
838     return 1;
839 }
840
841
842 /*******************************************************************
843  *         WINPOS_SetActiveWindow
844  *
845  * back-end to SetActiveWindow
846  */
847 BOOL WINPOS_SetActiveWindow( HWND hWnd, BOOL fMouse, BOOL fChangeFocus )
848 {
849     WND                   *wndPtr          = WIN_FindWndPtr(hWnd);
850     WND                   *wndTemp         = WIN_FindWndPtr(hwndActive);
851     CBTACTIVATESTRUCT16   *cbtStruct;
852     FARPROC                enumCallback    = MODULE_GetWndProcEntry16("ActivateAppProc");
853     ACTIVATESTRUCT         actStruct;
854     WORD                   wIconized=0;
855
856     /* FIXME: When proper support for cooperative multitasking is in place 
857      *        hActiveQ will be global 
858      */
859
860     HANDLE                 hActiveQ = 0;   
861
862     /* paranoid checks */
863     if( !hWnd || hWnd == GetDesktopWindow() || hWnd == hwndActive )
864         return 0;
865
866     if( GetTaskQueue(0) != wndPtr->hmemTaskQ )
867         return 0;
868
869     if( wndTemp )
870         wIconized = HIWORD(wndTemp->dwStyle & WS_MINIMIZE);
871     else
872         dprintf_win(stddeb,"WINPOS_ActivateWindow: no current active window.\n");
873
874     /* call CBT hook chain */
875     if ((cbtStruct = SEGPTR_NEW(CBTACTIVATESTRUCT16)))
876     {
877         LRESULT wRet;
878         cbtStruct->fMouse     = fMouse;
879         cbtStruct->hWndActive = hwndActive;
880         wRet = HOOK_CallHooks( WH_CBT, HCBT_ACTIVATE, (WPARAM)hWnd,
881                                (LPARAM)SEGPTR_GET(cbtStruct) );
882         SEGPTR_FREE(cbtStruct);
883         if (wRet) return wRet;
884     }
885
886     /* set prev active wnd to current active wnd and send notification */
887     if ((hwndPrevActive = hwndActive) && IsWindow(hwndPrevActive))
888     {
889         if (!SendMessage16( hwndPrevActive, WM_NCACTIVATE, FALSE, 0 ))
890         {
891             if (GetSysModalWindow() != hWnd) return 0;
892             /* disregard refusal if hWnd is sysmodal */
893         }
894
895         SendMessage32A( hwndPrevActive, WM_ACTIVATE,
896                         MAKEWPARAM( WA_INACTIVE, wIconized ),
897                         (LPARAM)hWnd );
898
899         /* check if something happened during message processing */
900         if( hwndPrevActive != hwndActive ) return 0;
901     }
902
903     /* set active wnd */
904     hwndActive = hWnd;
905
906     /* send palette messages */
907     if( SendMessage16( hWnd, WM_QUERYNEWPALETTE, 0, 0L) )
908         SendMessage16((HWND16)-1, WM_PALETTEISCHANGING, (WPARAM)hWnd, 0L );
909
910     /* if prev wnd is minimized redraw icon title 
911   if( hwndPrevActive )
912     {
913         wndTemp = WIN_FindWndPtr( WIN_GetTopParent( hwndPrevActive ) );
914         if(wndTemp)
915           if(wndTemp->dwStyle & WS_MINIMIZE)
916             RedrawIconTitle(hwndPrevActive); 
917       } 
918   */
919
920     /* managed windows will get ConfigureNotify event */  
921     if (!(wndPtr->dwStyle & WS_CHILD) && !(wndPtr->flags & WIN_MANAGED))
922     {
923         /* check Z-order and bring hWnd to the top */
924         for (wndTemp = WIN_GetDesktop()->child; wndTemp; wndTemp = wndTemp->next)
925             if (wndTemp->dwStyle & WS_VISIBLE) break;
926
927         if( wndTemp != wndPtr )
928             SetWindowPos(hWnd, HWND_TOP, 0,0,0,0, 
929                          SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
930     }
931
932     if( !IsWindow(hWnd) ) return 0;
933
934     if (hwndPrevActive)
935     {
936         wndTemp = WIN_FindWndPtr( hwndPrevActive );
937         if (wndTemp) hActiveQ = wndTemp->hmemTaskQ;
938     }
939
940     /* send WM_ACTIVATEAPP if necessary */
941     if (hActiveQ != wndPtr->hmemTaskQ)
942     {
943         HTASK hT = QUEUE_GetQueueTask( hActiveQ );
944
945         actStruct.wFlag = 0;                  /* deactivate */
946         actStruct.hWindowTask = QUEUE_GetQueueTask(wndPtr->hmemTaskQ);
947         actStruct.hTaskSendTo = hT;
948
949         /* send WM_ACTIVATEAPP to top-level windows
950          * that belong to the actStruct.hTaskSendTo task
951          */
952         EnumWindows( enumCallback , (LPARAM)&actStruct );
953
954         actStruct.wFlag = 1;                  /* activate */
955         actStruct.hWindowTask = hT;
956         actStruct.hTaskSendTo = QUEUE_GetQueueTask( wndPtr->hmemTaskQ );
957
958         EnumWindows( enumCallback , (LPARAM)&actStruct );
959
960         if( !IsWindow(hWnd) ) return 0;
961     }
962
963     /* walk up to the first unowned window */
964     wndTemp = wndPtr;
965     while (wndTemp->owner) wndTemp = wndTemp->owner;
966     /* and set last active owned popup */
967     wndTemp->hwndLastActive = hWnd;
968
969     wIconized = HIWORD(wndTemp->dwStyle & WS_MINIMIZE);
970     SendMessage16( hWnd, WM_NCACTIVATE, TRUE, 0 );
971 #ifdef WINELIB32
972     SendMessage32A( hWnd, WM_ACTIVATE,
973                  MAKEWPARAM( (fMouse)?WA_CLICKACTIVE:WA_ACTIVE, wIconized),
974                  (LPARAM)hwndPrevActive );
975 #else
976     SendMessage16( hWnd, WM_ACTIVATE, (fMouse)? WA_CLICKACTIVE : WA_ACTIVE,
977                  MAKELONG(hwndPrevActive,wIconized));
978 #endif
979
980     if( !IsWindow(hWnd) ) return 0;
981
982     /* change focus if possible */
983     if( fChangeFocus && GetFocus() )
984         if( WIN_GetTopParent(GetFocus()) != hwndActive )
985             FOCUS_SwitchFocus( GetFocus(),
986                                (wndPtr->dwStyle & WS_MINIMIZE)? 0: hwndActive);
987
988     /* if active wnd is minimized redraw icon title 
989   if( hwndActive )
990       {
991         wndPtr = WIN_FindWndPtr(hwndActive);
992         if(wndPtr->dwStyle & WS_MINIMIZE)
993            RedrawIconTitle(hwndActive);
994     }
995   */
996     return (hWnd == hwndActive);
997 }
998
999
1000 /*******************************************************************
1001  *         WINPOS_ChangeActiveWindow
1002  *
1003  */
1004 BOOL WINPOS_ChangeActiveWindow( HWND hWnd, BOOL mouseMsg )
1005 {
1006     WND *wndPtr = WIN_FindWndPtr(hWnd);
1007
1008     if( !wndPtr ) return FALSE;
1009
1010     /* child windows get WM_CHILDACTIVATE message */
1011     if( (wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) == WS_CHILD )
1012         return SendMessage16(hWnd, WM_CHILDACTIVATE, 0, 0L);
1013
1014         /* owned popups imply owner activation - not sure */
1015     if ((wndPtr->dwStyle & WS_POPUP) && wndPtr->owner &&
1016         !(wndPtr->owner->dwStyle & WS_DISABLED ))
1017     {
1018         if (!(wndPtr = wndPtr->owner)) return FALSE;
1019         hWnd = wndPtr->hwndSelf;
1020     }
1021
1022     if( hWnd == hwndActive ) return FALSE;
1023
1024     if( !WINPOS_SetActiveWindow(hWnd ,mouseMsg ,TRUE) )
1025         return FALSE;
1026
1027     /* switch desktop queue to current active */
1028     if( wndPtr->parent == WIN_GetDesktop())
1029         WIN_GetDesktop()->hmemTaskQ = wndPtr->hmemTaskQ;
1030
1031     return TRUE;
1032 }
1033
1034
1035 /***********************************************************************
1036  *           WINPOS_SendNCCalcSize
1037  *
1038  * Send a WM_NCCALCSIZE message to a window.
1039  * All parameters are read-only except newClientRect.
1040  * oldWindowRect, oldClientRect and winpos must be non-NULL only
1041  * when calcValidRect is TRUE.
1042  */
1043 LONG WINPOS_SendNCCalcSize( HWND hwnd, BOOL calcValidRect,
1044                             RECT16 *newWindowRect, RECT16 *oldWindowRect,
1045                             RECT16 *oldClientRect, SEGPTR winpos,
1046                             RECT16 *newClientRect )
1047 {
1048     NCCALCSIZE_PARAMS16 *params;
1049     LONG result;
1050
1051     if (!(params = SEGPTR_NEW(NCCALCSIZE_PARAMS16))) return 0;
1052     params->rgrc[0] = *newWindowRect;
1053     if (calcValidRect)
1054     {
1055         params->rgrc[1] = *oldWindowRect;
1056         params->rgrc[2] = *oldClientRect;
1057         params->lppos = winpos;
1058     }
1059     result = SendMessage16( hwnd, WM_NCCALCSIZE, calcValidRect,
1060                           (LPARAM)SEGPTR_GET( params ) );
1061     dprintf_win(stddeb, "WINPOS_SendNCCalcSize: %d %d %d %d\n",
1062                 (int)params->rgrc[0].top,    (int)params->rgrc[0].left,
1063                 (int)params->rgrc[0].bottom, (int)params->rgrc[0].right);
1064     *newClientRect = params->rgrc[0];
1065     SEGPTR_FREE(params);
1066     return result;
1067 }
1068
1069
1070 /***********************************************************************
1071  *           WINPOS_HandleWindowPosChanging16
1072  *
1073  * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1074  */
1075 LONG WINPOS_HandleWindowPosChanging16( WND *wndPtr, WINDOWPOS16 *winpos )
1076 {
1077     POINT16 maxSize;
1078     if (winpos->flags & SWP_NOSIZE) return 0;
1079     if ((wndPtr->dwStyle & WS_THICKFRAME) ||
1080         ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) == 0))
1081     {
1082         NC_GetMinMaxInfo( winpos->hwnd, &maxSize, NULL, NULL, NULL );
1083         winpos->cx = MIN( winpos->cx, maxSize.x );
1084         winpos->cy = MIN( winpos->cy, maxSize.y );
1085     }
1086     return 0;
1087 }
1088
1089
1090 /***********************************************************************
1091  *           WINPOS_HandleWindowPosChanging32
1092  *
1093  * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1094  */
1095 LONG WINPOS_HandleWindowPosChanging32( WND *wndPtr, WINDOWPOS32 *winpos )
1096 {
1097     POINT16 maxSize;
1098     if (winpos->flags & SWP_NOSIZE) return 0;
1099     if ((wndPtr->dwStyle & WS_THICKFRAME) ||
1100         ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) == 0))
1101     {
1102         NC_GetMinMaxInfo( winpos->hwnd, &maxSize, NULL, NULL, NULL );
1103         winpos->cx = MIN( winpos->cx, maxSize.x );
1104         winpos->cy = MIN( winpos->cy, maxSize.y );
1105     }
1106     return 0;
1107 }
1108
1109
1110 /***********************************************************************
1111  *           WINPOS_MoveWindowZOrder
1112  *
1113  * Move a window in Z order, invalidating everything that needs it.
1114  * Only necessary for windows without associated X window.
1115  */
1116 static void WINPOS_MoveWindowZOrder( HWND hwnd, HWND hwndAfter )
1117 {
1118     BOOL movingUp;
1119     WND *pWndAfter, *pWndCur, *wndPtr = WIN_FindWndPtr( hwnd );
1120
1121     /* We have two possible cases:
1122      * - The window is moving up: we have to invalidate all areas
1123      *   of the window that were covered by other windows
1124      * - The window is moving down: we have to invalidate areas
1125      *   of other windows covered by this one.
1126      */
1127
1128     if (hwndAfter == HWND_TOP)
1129     {
1130         movingUp = TRUE;
1131     }
1132     else if (hwndAfter == HWND_BOTTOM)
1133     {
1134         if (!wndPtr->next) return;  /* Already at the bottom */
1135         movingUp = FALSE;
1136     }
1137     else
1138     {
1139         if (!(pWndAfter = WIN_FindWndPtr( hwndAfter ))) return;
1140         if (wndPtr->next == pWndAfter) return;  /* Already placed right */
1141
1142           /* Determine which window we encounter first in Z-order */
1143         pWndCur = wndPtr->parent->child;
1144         while ((pWndCur != wndPtr) && (pWndCur != pWndAfter))
1145             pWndCur = pWndCur->next;
1146         movingUp = (pWndCur == pWndAfter);
1147     }
1148
1149     if (movingUp)
1150     {
1151         WND *pWndPrevAfter = wndPtr->next;
1152         WIN_UnlinkWindow( hwnd );
1153         WIN_LinkWindow( hwnd, hwndAfter );
1154         pWndCur = wndPtr->next;
1155         while (pWndCur != pWndPrevAfter)
1156         {
1157             RECT16 rect = pWndCur->rectWindow;
1158             OffsetRect16( &rect, -wndPtr->rectClient.left,
1159                           -wndPtr->rectClient.top );
1160             RedrawWindow16( hwnd, &rect, 0, RDW_INVALIDATE | RDW_ALLCHILDREN |
1161                             RDW_FRAME | RDW_ERASE );
1162             pWndCur = pWndCur->next;
1163         }
1164     }
1165     else  /* Moving down */
1166     {
1167         pWndCur = wndPtr->next;
1168         WIN_UnlinkWindow( hwnd );
1169         WIN_LinkWindow( hwnd, hwndAfter );
1170         while (pWndCur != wndPtr)
1171         {
1172             RECT16 rect = wndPtr->rectWindow;
1173             OffsetRect16( &rect, -pWndCur->rectClient.left,
1174                           -pWndCur->rectClient.top );
1175             RedrawWindow16( pWndCur->hwndSelf, &rect, 0, RDW_INVALIDATE |
1176                             RDW_ALLCHILDREN | RDW_FRAME | RDW_ERASE );
1177             pWndCur = pWndCur->next;
1178         }
1179     }
1180 }
1181
1182 /***********************************************************************
1183  *           WINPOS_ReorderOwnedPopups
1184  *
1185  * fix Z order taking into account owned popups -
1186  * basically we need to maintain them above owner window
1187  */
1188 HWND WINPOS_ReorderOwnedPopups(HWND hwndInsertAfter, WND* wndPtr, WORD flags)
1189 {
1190  WND*   w = WIN_GetDesktop();
1191
1192  w = w->child;
1193
1194  /* if we are dealing with owned popup... 
1195   */
1196  if( wndPtr->dwStyle & WS_POPUP && wndPtr->owner && hwndInsertAfter != HWND_TOP )
1197    {
1198      BOOL bFound = FALSE;
1199      HWND hwndLocalPrev = HWND_TOP;
1200      HWND hwndNewAfter = 0;
1201
1202      while( w )
1203        {
1204          if( !bFound && hwndInsertAfter == hwndLocalPrev )
1205              hwndInsertAfter = HWND_TOP;
1206
1207          if( w->dwStyle & WS_POPUP && w->owner == wndPtr->owner )
1208            {
1209              bFound = TRUE;
1210
1211              if( hwndInsertAfter == HWND_TOP )
1212                {
1213                  hwndInsertAfter = hwndLocalPrev;
1214                  break;
1215                }
1216              hwndNewAfter = hwndLocalPrev;
1217            }
1218
1219          if( w == wndPtr->owner )
1220            {
1221              /* basically HWND_BOTTOM */
1222              hwndInsertAfter = hwndLocalPrev;
1223
1224              if( bFound )
1225                  hwndInsertAfter = hwndNewAfter;
1226              break;
1227            }
1228
1229            if( w != wndPtr )
1230                hwndLocalPrev = w->hwndSelf;
1231
1232            w = w->next;
1233         }
1234    }
1235  else 
1236    /* or overlapped top-level window... 
1237     */
1238    if( !(wndPtr->dwStyle & WS_CHILD) )
1239       while( w )
1240         {
1241           if( w == wndPtr ) break;
1242
1243           if( w->dwStyle & WS_POPUP && w->owner == wndPtr )
1244             {
1245               SetWindowPos(w->hwndSelf, hwndInsertAfter, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE |
1246                                         SWP_NOACTIVATE | SWP_NOSENDCHANGING | SWP_DEFERERASE);
1247               hwndInsertAfter = w->hwndSelf;
1248             }
1249           w = w->next;
1250         }
1251
1252   return hwndInsertAfter;
1253 }
1254
1255 /***********************************************************************
1256  *           WINPOS_SizeMoveClean
1257  *
1258  * Make window look nice without excessive repainting
1259  *
1260  * the pain:
1261  *
1262  * visible regions are in window coordinates
1263  * update regions are in window client coordinates
1264  * client and window rectangles are in parent client coordinates
1265  */
1266 static void WINPOS_SizeMoveClean(WND* Wnd, HRGN oldVisRgn, LPRECT16 lpOldWndRect, LPRECT16 lpOldClientRect, BOOL bNoCopy )
1267 {
1268  HRGN newVisRgn    = DCE_GetVisRgn(Wnd->hwndSelf, DCX_WINDOW | DCX_CLIPSIBLINGS );
1269  HRGN dirtyRgn     = CreateRectRgn(0,0,0,0);
1270  int  other, my;
1271
1272  dprintf_win(stddeb,"cleaning up...new wnd=(%i %i-%i %i) old wnd=(%i %i-%i %i)\n\
1273 \t\tnew client=(%i %i-%i %i) old client=(%i %i-%i %i)\n",
1274                    Wnd->rectWindow.left, Wnd->rectWindow.top, Wnd->rectWindow.right, Wnd->rectWindow.bottom,
1275                    lpOldWndRect->left, lpOldWndRect->top, lpOldWndRect->right, lpOldWndRect->bottom,
1276                    Wnd->rectClient.left,Wnd->rectClient.top,Wnd->rectClient.right,Wnd->rectClient.bottom,
1277                    lpOldClientRect->left,lpOldClientRect->top,lpOldClientRect->right,lpOldClientRect->bottom);
1278
1279  CombineRgn( dirtyRgn, newVisRgn, 0, RGN_COPY);
1280
1281  if( !bNoCopy )
1282    {
1283      HRGN hRgn = CreateRectRgn( lpOldClientRect->left - lpOldWndRect->left, lpOldClientRect->top - lpOldWndRect->top,
1284                                 lpOldClientRect->right - lpOldWndRect->left, lpOldClientRect->bottom - lpOldWndRect->top);
1285      CombineRgn( newVisRgn, newVisRgn, oldVisRgn, RGN_AND ); 
1286      CombineRgn( newVisRgn, newVisRgn, hRgn, RGN_AND );
1287      DeleteObject(hRgn);
1288    }
1289
1290  /* map regions to the parent client area */
1291  
1292  OffsetRgn(dirtyRgn, Wnd->rectWindow.left, Wnd->rectWindow.top);
1293  OffsetRgn(oldVisRgn, lpOldWndRect->left, lpOldWndRect->top);
1294
1295  /* compute invalidated region outside Wnd - (in client coordinates of the parent window) */
1296
1297  other = CombineRgn(dirtyRgn, oldVisRgn, dirtyRgn, RGN_DIFF);
1298
1299  /* map visible region to the Wnd client area */
1300
1301  OffsetRgn( newVisRgn, Wnd->rectWindow.left - Wnd->rectClient.left,
1302                        Wnd->rectWindow.top - Wnd->rectClient.top );
1303
1304  /* substract previously invalidated region from the Wnd visible region */
1305
1306  my =  (Wnd->hrgnUpdate > 1)? CombineRgn( newVisRgn, newVisRgn, Wnd->hrgnUpdate, RGN_DIFF)
1307                             : COMPLEXREGION;
1308
1309  if( bNoCopy )          /* invalidate Wnd visible region */
1310    {
1311      if (my != NULLREGION)  RedrawWindow32( Wnd->hwndSelf, NULL, newVisRgn, RDW_INVALIDATE |
1312                             RDW_FRAME | RDW_ALLCHILDREN | RDW_ERASE );
1313    } 
1314  else                   /* bitblt old client area */
1315    { 
1316      HDC   hDC;
1317      int   update;
1318      HRGN  updateRgn;
1319
1320      /* client rect */
1321
1322      updateRgn = CreateRectRgn( 0,0, Wnd->rectClient.right - Wnd->rectClient.left,
1323                                 Wnd->rectClient.bottom - Wnd->rectClient.top );
1324
1325      /* clip visible region with client rect */
1326
1327      my = CombineRgn( newVisRgn, newVisRgn, updateRgn, RGN_AND );
1328
1329      /* substract result from client rect to get region that won't be copied */
1330
1331      update = CombineRgn( updateRgn, updateRgn, newVisRgn, RGN_DIFF );
1332
1333      /* Blt valid bits using parent window DC */
1334
1335      if( my != NULLREGION )
1336        {
1337          int xfrom = lpOldClientRect->left;
1338          int yfrom = lpOldClientRect->top;
1339          int xto = Wnd->rectClient.left;
1340          int yto = Wnd->rectClient.top;
1341
1342          /* check if we can skip copying */
1343
1344          if( xfrom != xto || yfrom != yto )
1345            {
1346              /* compute clipping region in parent client coordinates */
1347
1348              OffsetRgn( newVisRgn, Wnd->rectClient.left, Wnd->rectClient.top);
1349              CombineRgn( oldVisRgn, oldVisRgn, newVisRgn, RGN_OR );
1350
1351              hDC = GetDCEx( Wnd->parent->hwndSelf, oldVisRgn, DCX_KEEPCLIPRGN | DCX_INTERSECTRGN | DCX_CACHE | DCX_CLIPSIBLINGS);
1352
1353              BitBlt(hDC, xto, yto, lpOldClientRect->right - lpOldClientRect->left, 
1354                                    lpOldClientRect->bottom - lpOldClientRect->top,
1355                                    hDC, xfrom, yfrom, SRCCOPY );
1356     
1357              ReleaseDC( Wnd->parent->hwndSelf, hDC); 
1358           }
1359        }
1360
1361      if( update != NULLREGION )
1362          RedrawWindow32( Wnd->hwndSelf, NULL, updateRgn, RDW_INVALIDATE |
1363                          RDW_FRAME | RDW_ALLCHILDREN | RDW_ERASE );
1364      DeleteObject( updateRgn );
1365    }
1366
1367  /* erase uncovered areas */
1368
1369  if( other != NULLREGION )
1370      RedrawWindow32( Wnd->parent->hwndSelf, NULL, dirtyRgn,
1371                      RDW_INVALIDATE | RDW_ALLCHILDREN | RDW_ERASE );
1372
1373  DeleteObject(dirtyRgn);
1374  DeleteObject(newVisRgn);
1375 }
1376
1377 /***********************************************************************
1378  *           WINPOS_SetXWindowPos
1379  *
1380  * SetWindowPos() for an X window. Used by the real SetWindowPos().
1381  */
1382 static void WINPOS_SetXWindowPos( WINDOWPOS16 *winpos )
1383 {
1384     XWindowChanges winChanges;
1385     int changeMask = 0;
1386     WND *wndPtr = WIN_FindWndPtr( winpos->hwnd );
1387
1388     if (!(winpos->flags & SWP_NOSIZE))
1389     {
1390         winChanges.width     = winpos->cx;
1391         winChanges.height    = winpos->cy;
1392         changeMask |= CWWidth | CWHeight;
1393     }
1394     if (!(winpos->flags & SWP_NOMOVE))
1395     {
1396         winChanges.x = winpos->x;
1397         winChanges.y = winpos->y;
1398         changeMask |= CWX | CWY;
1399     }
1400     if (!(winpos->flags & SWP_NOZORDER))
1401     {
1402         if (winpos->hwndInsertAfter == HWND_TOP) winChanges.stack_mode = Above;
1403         else winChanges.stack_mode = Below;
1404         if ((winpos->hwndInsertAfter != HWND_TOP) &&
1405             (winpos->hwndInsertAfter != HWND_BOTTOM))
1406         {
1407             WND * insertPtr = WIN_FindWndPtr( winpos->hwndInsertAfter );
1408             winChanges.sibling = insertPtr->window;
1409             changeMask |= CWSibling;
1410         }
1411         changeMask |= CWStackMode;
1412     }
1413     if (changeMask)
1414         XConfigureWindow( display, wndPtr->window, changeMask, &winChanges );
1415 }
1416
1417
1418 /***********************************************************************
1419  *           SetWindowPos   (USER.232)
1420  */
1421 BOOL SetWindowPos( HWND hwnd, HWND hwndInsertAfter, INT x, INT y,
1422                    INT cx, INT cy, WORD flags )
1423 {
1424     WINDOWPOS16 winpos;
1425     WND *       wndPtr;
1426     RECT16      newWindowRect, newClientRect;
1427     HRGN        visRgn = 0;
1428     int         result = 0;
1429
1430     dprintf_win(stddeb,"SetWindowPos: hwnd %04x, flags %08x\n", hwnd, flags);  
1431
1432       /* Check window handle */
1433
1434     if (hwnd == GetDesktopWindow()) return FALSE;
1435     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
1436
1437     /* Check for windows that may not be resized 
1438        FIXME: this should be done only for Windows 3.0 programs */
1439     if (flags ==(SWP_SHOWWINDOW) || flags ==(SWP_HIDEWINDOW ) )
1440        flags |= SWP_NOSIZE | SWP_NOMOVE;
1441
1442       /* Check dimensions */
1443
1444     if (cx <= 0) cx = 1;
1445     if (cy <= 0) cy = 1;
1446
1447       /* Check flags */
1448
1449     if (hwnd == hwndActive) flags |= SWP_NOACTIVATE;   /* Already active */
1450     if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == cx) &&
1451         (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == cy))
1452         flags |= SWP_NOSIZE;    /* Already the right size */
1453     if ((wndPtr->rectWindow.left == x) && (wndPtr->rectWindow.top == y))
1454         flags |= SWP_NOMOVE;    /* Already the right position */
1455
1456       /* Check hwndInsertAfter */
1457
1458     if (!(flags & (SWP_NOZORDER | SWP_NOACTIVATE)))
1459     {
1460           /* Ignore TOPMOST flags when activating a window */
1461           /* _and_ moving it in Z order. */
1462         if ((hwndInsertAfter == HWND_TOPMOST) ||
1463             (hwndInsertAfter == HWND_NOTOPMOST))
1464             hwndInsertAfter = HWND_TOP; 
1465     }
1466       /* TOPMOST not supported yet */
1467     if ((hwndInsertAfter == HWND_TOPMOST) ||
1468         (hwndInsertAfter == HWND_NOTOPMOST)) hwndInsertAfter = HWND_TOP;
1469
1470       /* hwndInsertAfter must be a sibling of the window */
1471     if ((hwndInsertAfter != HWND_TOP) && (hwndInsertAfter != HWND_BOTTOM))
1472        {
1473          WND* wnd = WIN_FindWndPtr(hwndInsertAfter);
1474          if( wnd->parent != wndPtr->parent ) return FALSE;
1475          if( wnd->next == wndPtr ) flags |= SWP_NOZORDER;
1476        }
1477     else
1478        if (hwndInsertAfter == HWND_TOP)
1479            flags |= ( wndPtr->parent->child == wndPtr)? SWP_NOZORDER: 0;
1480        else /* HWND_BOTTOM */
1481            flags |= ( wndPtr->next )? 0: SWP_NOZORDER;
1482
1483       /* Fill the WINDOWPOS structure */
1484
1485     winpos.hwnd = hwnd;
1486     winpos.hwndInsertAfter = hwndInsertAfter;
1487     winpos.x = x;
1488     winpos.y = y;
1489     winpos.cx = cx;
1490     winpos.cy = cy;
1491     winpos.flags = flags;
1492     
1493       /* Send WM_WINDOWPOSCHANGING message */
1494
1495     if (!(flags & SWP_NOSENDCHANGING))
1496         SendMessage16( hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)MAKE_SEGPTR(&winpos) );
1497
1498       /* Calculate new position and size */
1499
1500     newWindowRect = wndPtr->rectWindow;
1501     newClientRect = wndPtr->rectClient;
1502
1503     if (!(winpos.flags & SWP_NOSIZE))
1504     {
1505         newWindowRect.right  = newWindowRect.left + winpos.cx;
1506         newWindowRect.bottom = newWindowRect.top + winpos.cy;
1507     }
1508     if (!(winpos.flags & SWP_NOMOVE))
1509     {
1510         newWindowRect.left    = winpos.x;
1511         newWindowRect.top     = winpos.y;
1512         newWindowRect.right  += winpos.x - wndPtr->rectWindow.left;
1513         newWindowRect.bottom += winpos.y - wndPtr->rectWindow.top;
1514
1515         OffsetRect16( &newClientRect, winpos.x - wndPtr->rectWindow.left, 
1516                                       winpos.y - wndPtr->rectWindow.top );
1517     }
1518
1519     winpos.flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
1520
1521       /* Reposition window in Z order */
1522
1523     if (!(winpos.flags & SWP_NOZORDER))
1524     {
1525         /* reorder owned popups if hwnd is top-level window 
1526          */
1527         if( wndPtr->parent == WIN_GetDesktop() )
1528             hwndInsertAfter = WINPOS_ReorderOwnedPopups( hwndInsertAfter,
1529                                                          wndPtr, flags );
1530
1531         if (wndPtr->window)
1532         {
1533             WIN_UnlinkWindow( winpos.hwnd );
1534             WIN_LinkWindow( winpos.hwnd, hwndInsertAfter );
1535         }
1536         else WINPOS_MoveWindowZOrder( winpos.hwnd, hwndInsertAfter );
1537     }
1538
1539     if ( !wndPtr->window && !(flags & SWP_NOREDRAW) && 
1540         (!(flags & SWP_NOMOVE) || !(flags & SWP_NOSIZE) || (flags & SWP_FRAMECHANGED)) )
1541           visRgn = DCE_GetVisRgn(hwnd, DCX_WINDOW | DCX_CLIPSIBLINGS);
1542
1543
1544       /* Send WM_NCCALCSIZE message to get new client area */
1545     if( (flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
1546       {
1547          result = WINPOS_SendNCCalcSize( winpos.hwnd, TRUE, &newWindowRect,
1548                                     &wndPtr->rectWindow, &wndPtr->rectClient,
1549                                     MAKE_SEGPTR(&winpos), &newClientRect );
1550
1551          /* FIXME: WVR_ALIGNxxx */
1552
1553          if( newClientRect.left != wndPtr->rectClient.left ||
1554              newClientRect.top != wndPtr->rectClient.top )
1555              winpos.flags &= ~SWP_NOCLIENTMOVE;
1556
1557          if( (newClientRect.right - newClientRect.left !=
1558              wndPtr->rectClient.right - wndPtr->rectClient.left) ||
1559             (newClientRect.bottom - newClientRect.top !=
1560              wndPtr->rectClient.bottom - wndPtr->rectClient.top) )
1561              winpos.flags &= ~SWP_NOCLIENTSIZE;
1562       }
1563     else
1564       if( !(flags & SWP_NOMOVE) && (newClientRect.left != wndPtr->rectClient.left ||
1565                                     newClientRect.top != wndPtr->rectClient.top) )
1566             winpos.flags &= ~SWP_NOCLIENTMOVE;
1567
1568     /* Update active DCEs */
1569
1570     if( !(flags & SWP_NOZORDER) || (flags & SWP_HIDEWINDOW) || (flags & SWP_SHOWWINDOW)
1571                                 || (memcmp(&newWindowRect,&wndPtr->rectWindow,sizeof(RECT16))
1572                                     && wndPtr->dwStyle & WS_VISIBLE ) )
1573       {
1574         RECT16 rect;
1575
1576         UnionRect16(&rect,&newWindowRect,&wndPtr->rectWindow);
1577         DCE_InvalidateDCE(wndPtr->parent, &rect);
1578       }
1579
1580     /* Perform the moving and resizing */
1581
1582     if (wndPtr->window)
1583     {
1584         RECT16 oldWindowRect = wndPtr->rectWindow;
1585         RECT16 oldClientRect = wndPtr->rectClient;
1586
1587         HWND bogusInsertAfter = winpos.hwndInsertAfter;
1588
1589         winpos.hwndInsertAfter = hwndInsertAfter;
1590         WINPOS_SetXWindowPos( &winpos );
1591
1592         wndPtr->rectWindow = newWindowRect;
1593         wndPtr->rectClient = newClientRect;
1594         winpos.hwndInsertAfter = bogusInsertAfter;
1595
1596         /*  FIXME: should do something like WINPOS_SizeMoveClean */
1597
1598         if( (oldClientRect.left - oldWindowRect.left !=
1599              newClientRect.left - newWindowRect.left) ||
1600             (oldClientRect.top - oldWindowRect.top !=
1601              newClientRect.top - newWindowRect.top) )
1602
1603             RedrawWindow32( wndPtr->hwndSelf, NULL, 0, RDW_INVALIDATE |
1604                             RDW_ALLCHILDREN | RDW_FRAME | RDW_ERASE );
1605         else
1606             if( winpos.flags & SWP_FRAMECHANGED )
1607               {
1608                 WORD wErase = 0;
1609                 RECT32 rect;
1610
1611                 if( oldClientRect.right > newClientRect.right ) 
1612                 {
1613                     rect.left = newClientRect.right; rect.top = newClientRect.top;
1614                     rect.right = oldClientRect.right; rect.bottom = newClientRect.bottom;
1615                     wErase = 1;
1616                     RedrawWindow32( wndPtr->hwndSelf, &rect, 0,
1617                                 RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN );
1618                 }
1619                 if( oldClientRect.bottom > newClientRect.bottom )
1620                 {
1621                     rect.left = newClientRect.left; rect.top = newClientRect.bottom;
1622                     rect.right = (wErase)?oldClientRect.right:newClientRect.right;
1623                     rect.bottom = oldClientRect.bottom;
1624                     wErase = 1;
1625                     RedrawWindow32( wndPtr->hwndSelf, &rect, 0,
1626                                 RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN );
1627                 }
1628                 if( !wErase ) wndPtr->flags |= WIN_NEEDS_NCPAINT;
1629               }
1630     }
1631     else
1632     {
1633         RECT16 oldWindowRect = wndPtr->rectWindow;
1634         RECT16 oldClientRect = wndPtr->rectClient;
1635
1636         wndPtr->rectWindow = newWindowRect;
1637         wndPtr->rectClient = newClientRect;
1638
1639         if( !(flags & SWP_NOREDRAW) )
1640           {
1641             BOOL bNoCopy = (flags & SWP_NOCOPYBITS) || 
1642                            (result >= WVR_HREDRAW && result < WVR_VALIDRECTS);
1643
1644             if( (winpos.flags & SWP_NOPOSCHANGE) != SWP_NOPOSCHANGE )
1645                 /* optimize cleanup by BitBlt'ing where possible */
1646
1647                 WINPOS_SizeMoveClean(wndPtr, visRgn, &oldWindowRect, &oldClientRect, bNoCopy);
1648             else
1649                if( winpos.flags & SWP_FRAMECHANGED )
1650                   RedrawWindow32( winpos.hwnd, NULL, 0, RDW_NOCHILDREN | RDW_FRAME ); 
1651
1652           }
1653         DeleteObject(visRgn);
1654     }
1655
1656     if (flags & SWP_SHOWWINDOW)
1657     {
1658         wndPtr->dwStyle |= WS_VISIBLE;
1659         if (wndPtr->window)
1660         {
1661             XMapWindow( display, wndPtr->window );
1662         }
1663         else
1664         {
1665             if (!(flags & SWP_NOREDRAW))
1666                 RedrawWindow32( winpos.hwnd, NULL, 0,
1667                                 RDW_INVALIDATE | RDW_ALLCHILDREN |
1668                                 RDW_FRAME | RDW_ERASE );
1669         }
1670     }
1671     else if (flags & SWP_HIDEWINDOW)
1672     {
1673         wndPtr->dwStyle &= ~WS_VISIBLE;
1674         if (wndPtr->window)
1675         {
1676             XUnmapWindow( display, wndPtr->window );
1677         }
1678         else
1679         {
1680             if (!(flags & SWP_NOREDRAW))
1681                 RedrawWindow16( wndPtr->parent->hwndSelf, &wndPtr->rectWindow, 0,
1682                                 RDW_INVALIDATE | RDW_ALLCHILDREN | RDW_ERASE );
1683         }
1684
1685         if ((winpos.hwnd == GetFocus()) || IsChild(winpos.hwnd, GetFocus()))
1686             SetFocus( GetParent(winpos.hwnd) );  /* Revert focus to parent */
1687
1688         if (winpos.hwnd == hwndActive)
1689         {
1690               /* Activate previously active window if possible */
1691             HWND newActive = hwndPrevActive;
1692             if (!IsWindow(newActive) || (newActive == winpos.hwnd))
1693             {
1694                 newActive = GetTopWindow( GetDesktopWindow() );
1695                 if (newActive == winpos.hwnd)
1696                     newActive = wndPtr->next ? wndPtr->next->hwndSelf : 0;
1697             }       
1698             WINPOS_ChangeActiveWindow( newActive, FALSE );
1699         }
1700     }
1701
1702       /* Activate the window */
1703
1704     if (!(flags & SWP_NOACTIVATE))
1705             WINPOS_ChangeActiveWindow( winpos.hwnd, FALSE );
1706     
1707       /* Repaint the window */
1708
1709     if (wndPtr->window) EVENT_Synchronize();  /* Wait for all expose events */
1710
1711     EVENT_DummyMotionNotify(); /* Simulate a mouse event to set the cursor */
1712
1713     if (!(flags & SWP_DEFERERASE))
1714         RedrawWindow32( wndPtr->parent->hwndSelf, NULL, 0,
1715                         RDW_ALLCHILDREN | RDW_ERASENOW );
1716
1717       /* And last, send the WM_WINDOWPOSCHANGED message */
1718
1719     if (!(winpos.flags & SWP_NOSENDCHANGING))
1720         SendMessage16( winpos.hwnd, WM_WINDOWPOSCHANGED,
1721                        0, (LPARAM)MAKE_SEGPTR(&winpos) );
1722
1723     return TRUE;
1724 }
1725
1726                                         
1727 /***********************************************************************
1728  *           BeginDeferWindowPos   (USER.259)
1729  */
1730 HDWP16 BeginDeferWindowPos( INT count )
1731 {
1732     HDWP16 handle;
1733     DWP *pDWP;
1734
1735     if (count <= 0) return 0;
1736     handle = USER_HEAP_ALLOC( sizeof(DWP) + (count-1)*sizeof(WINDOWPOS16) );
1737     if (!handle) return 0;
1738     pDWP = (DWP *) USER_HEAP_LIN_ADDR( handle );
1739     pDWP->actualCount    = 0;
1740     pDWP->suggestedCount = count;
1741     pDWP->valid          = TRUE;
1742     pDWP->wMagic         = DWP_MAGIC;
1743     pDWP->hwndParent     = 0;
1744     return handle;
1745 }
1746
1747
1748 /***********************************************************************
1749  *           DeferWindowPos   (USER.260)
1750  */
1751 HDWP16 DeferWindowPos( HDWP16 hdwp, HWND hwnd, HWND hwndAfter, INT x, INT y,
1752                        INT cx, INT cy, UINT flags )
1753 {
1754     DWP *pDWP;
1755     int i;
1756     HDWP16 newhdwp = hdwp;
1757     HWND parent;
1758
1759     pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
1760     if (!pDWP) return 0;
1761     if (hwnd == GetDesktopWindow()) return 0;
1762
1763       /* All the windows of a DeferWindowPos() must have the same parent */
1764
1765     parent = WIN_FindWndPtr( hwnd )->parent->hwndSelf;
1766     if (pDWP->actualCount == 0) pDWP->hwndParent = parent;
1767     else if (parent != pDWP->hwndParent)
1768     {
1769         USER_HEAP_FREE( hdwp );
1770         return 0;
1771     }
1772
1773     for (i = 0; i < pDWP->actualCount; i++)
1774     {
1775         if (pDWP->winPos[i].hwnd == hwnd)
1776         {
1777               /* Merge with the other changes */
1778             if (!(flags & SWP_NOZORDER))
1779             {
1780                 pDWP->winPos[i].hwndInsertAfter = hwndAfter;
1781             }
1782             if (!(flags & SWP_NOMOVE))
1783             {
1784                 pDWP->winPos[i].x = x;
1785                 pDWP->winPos[i].y = y;
1786             }                
1787             if (!(flags & SWP_NOSIZE))
1788             {
1789                 pDWP->winPos[i].cx = cx;
1790                 pDWP->winPos[i].cy = cy;
1791             }
1792             pDWP->winPos[i].flags &= flags & (SWP_NOSIZE | SWP_NOMOVE |
1793                                               SWP_NOZORDER | SWP_NOREDRAW |
1794                                               SWP_NOACTIVATE | SWP_NOCOPYBITS |
1795                                               SWP_NOOWNERZORDER);
1796             pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
1797                                               SWP_FRAMECHANGED);
1798             return hdwp;
1799         }
1800     }
1801     if (pDWP->actualCount >= pDWP->suggestedCount)
1802     {
1803         newhdwp = USER_HEAP_REALLOC( hdwp,
1804                       sizeof(DWP) + pDWP->suggestedCount*sizeof(WINDOWPOS16) );
1805         if (!newhdwp) return 0;
1806         pDWP = (DWP *) USER_HEAP_LIN_ADDR( newhdwp );
1807         pDWP->suggestedCount++;
1808     }
1809     pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
1810     pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
1811     pDWP->winPos[pDWP->actualCount].x = x;
1812     pDWP->winPos[pDWP->actualCount].y = y;
1813     pDWP->winPos[pDWP->actualCount].cx = cx;
1814     pDWP->winPos[pDWP->actualCount].cy = cy;
1815     pDWP->winPos[pDWP->actualCount].flags = flags;
1816     pDWP->actualCount++;
1817     return newhdwp;
1818 }
1819
1820
1821 /***********************************************************************
1822  *           EndDeferWindowPos   (USER.261)
1823  */
1824 BOOL EndDeferWindowPos( HDWP16 hdwp )
1825 {
1826     DWP *pDWP;
1827     WINDOWPOS16 *winpos;
1828     BOOL res = TRUE;
1829     int i;
1830
1831     pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
1832     if (!pDWP) return FALSE;
1833     for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
1834     {
1835         if (!(res = SetWindowPos( winpos->hwnd, winpos->hwndInsertAfter,
1836                                   winpos->x, winpos->y, winpos->cx, winpos->cy,
1837                                   winpos->flags ))) break;
1838     }
1839     USER_HEAP_FREE( hdwp );
1840     return res;
1841 }
1842
1843
1844 /***********************************************************************
1845  *           TileChildWindows   (USER.199)
1846  */
1847 void TileChildWindows( HWND parent, WORD action )
1848 {
1849     printf("STUB TileChildWindows(%04x, %d)\n", parent, action);
1850 }
1851
1852 /***********************************************************************
1853  *           CascageChildWindows   (USER.198)
1854  */
1855 void CascadeChildWindows( HWND parent, WORD action )
1856 {
1857     printf("STUB CascadeChildWindows(%04x, %d)\n", parent, action);
1858 }