Release 960705
[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  *         WINPOS_SetActiveWindow
829  *
830  * back-end to SetActiveWindow
831  */
832 BOOL WINPOS_SetActiveWindow( HWND hWnd, BOOL fMouse, BOOL fChangeFocus )
833 {
834     WND                   *wndPtr          = WIN_FindWndPtr(hWnd);
835     WND                   *wndTemp         = WIN_FindWndPtr(hwndActive);
836     CBTACTIVATESTRUCT16   *cbtStruct;
837     WORD                   wIconized=0;
838
839     /* FIXME: When proper support for cooperative multitasking is in place 
840      *        hActiveQ will be global 
841      */
842
843     HANDLE                 hActiveQ = 0;   
844
845     /* paranoid checks */
846     if( !hWnd || hWnd == GetDesktopWindow() || hWnd == hwndActive )
847         return 0;
848
849     if( GetTaskQueue(0) != wndPtr->hmemTaskQ )
850         return 0;
851
852     if( wndTemp )
853         wIconized = HIWORD(wndTemp->dwStyle & WS_MINIMIZE);
854     else
855         dprintf_win(stddeb,"WINPOS_ActivateWindow: no current active window.\n");
856
857     /* call CBT hook chain */
858     if ((cbtStruct = SEGPTR_NEW(CBTACTIVATESTRUCT16)))
859     {
860         LRESULT wRet;
861         cbtStruct->fMouse     = fMouse;
862         cbtStruct->hWndActive = hwndActive;
863         wRet = HOOK_CallHooks( WH_CBT, HCBT_ACTIVATE, (WPARAM)hWnd,
864                                (LPARAM)SEGPTR_GET(cbtStruct) );
865         SEGPTR_FREE(cbtStruct);
866         if (wRet) return wRet;
867     }
868
869     /* set prev active wnd to current active wnd and send notification */
870     if ((hwndPrevActive = hwndActive) && IsWindow(hwndPrevActive))
871     {
872         if (!SendMessage16( hwndPrevActive, WM_NCACTIVATE, FALSE, 0 ))
873         {
874             if (GetSysModalWindow() != hWnd) return 0;
875             /* disregard refusal if hWnd is sysmodal */
876         }
877
878         SendMessage32A( hwndPrevActive, WM_ACTIVATE,
879                         MAKEWPARAM( WA_INACTIVE, wIconized ),
880                         (LPARAM)hWnd );
881
882         /* check if something happened during message processing */
883         if( hwndPrevActive != hwndActive ) return 0;
884     }
885
886     /* set active wnd */
887     hwndActive = hWnd;
888
889     /* send palette messages */
890     if( SendMessage16( hWnd, WM_QUERYNEWPALETTE, 0, 0L) )
891         SendMessage16((HWND16)-1, WM_PALETTEISCHANGING, (WPARAM)hWnd, 0L );
892
893     /* if prev wnd is minimized redraw icon title 
894   if( hwndPrevActive )
895     {
896         wndTemp = WIN_FindWndPtr( WIN_GetTopParent( hwndPrevActive ) );
897         if(wndTemp)
898           if(wndTemp->dwStyle & WS_MINIMIZE)
899             RedrawIconTitle(hwndPrevActive); 
900       } 
901   */
902
903     /* managed windows will get ConfigureNotify event */  
904     if (!(wndPtr->dwStyle & WS_CHILD) && !(wndPtr->flags & WIN_MANAGED))
905     {
906         /* check Z-order and bring hWnd to the top */
907         for (wndTemp = WIN_GetDesktop()->child; wndTemp; wndTemp = wndTemp->next)
908             if (wndTemp->dwStyle & WS_VISIBLE) break;
909
910         if( wndTemp != wndPtr )
911             SetWindowPos(hWnd, HWND_TOP, 0,0,0,0, 
912                          SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
913     }
914
915     if( !IsWindow(hWnd) ) return 0;
916
917     if (hwndPrevActive)
918     {
919         wndTemp = WIN_FindWndPtr( hwndPrevActive );
920         if (wndTemp) hActiveQ = wndTemp->hmemTaskQ;
921     }
922
923     /* send WM_ACTIVATEAPP if necessary */
924     if (hActiveQ != wndPtr->hmemTaskQ)
925     {
926         WND **list, **ppWnd;
927
928         if ((list = WIN_BuildWinArray( WIN_GetDesktop() )))
929         {
930             for (ppWnd = list; *ppWnd; ppWnd++)
931             {
932                 /* Make sure that the window still exists */
933                 if (!IsWindow( (*ppWnd)->hwndSelf )) continue;
934                 if ((*ppWnd)->hmemTaskQ != hActiveQ) continue;
935                 SendMessage16( (*ppWnd)->hwndSelf, WM_ACTIVATEAPP,
936                                0, QUEUE_GetQueueTask(wndPtr->hmemTaskQ) );
937             }
938             HeapFree( SystemHeap, 0, list );
939         }
940
941         if ((list = WIN_BuildWinArray( WIN_GetDesktop() )))
942         {
943             for (ppWnd = list; *ppWnd; ppWnd++)
944             {
945                 /* Make sure that the window still exists */
946                 if (!IsWindow( (*ppWnd)->hwndSelf )) continue;
947                 if ((*ppWnd)->hmemTaskQ != wndPtr->hmemTaskQ) continue;
948                 SendMessage16( (*ppWnd)->hwndSelf, WM_ACTIVATEAPP,
949                                1, QUEUE_GetQueueTask( hActiveQ ) );
950             }
951             HeapFree( SystemHeap, 0, list );
952         }
953         if (!IsWindow(hWnd)) return 0;
954     }
955
956     /* walk up to the first unowned window */
957     wndTemp = wndPtr;
958     while (wndTemp->owner) wndTemp = wndTemp->owner;
959     /* and set last active owned popup */
960     wndTemp->hwndLastActive = hWnd;
961
962     wIconized = HIWORD(wndTemp->dwStyle & WS_MINIMIZE);
963     SendMessage16( hWnd, WM_NCACTIVATE, TRUE, 0 );
964 #ifdef WINELIB32
965     SendMessage32A( hWnd, WM_ACTIVATE,
966                  MAKEWPARAM( (fMouse)?WA_CLICKACTIVE:WA_ACTIVE, wIconized),
967                  (LPARAM)hwndPrevActive );
968 #else
969     SendMessage16( hWnd, WM_ACTIVATE, (fMouse)? WA_CLICKACTIVE : WA_ACTIVE,
970                  MAKELONG(hwndPrevActive,wIconized));
971 #endif
972
973     if( !IsWindow(hWnd) ) return 0;
974
975     /* change focus if possible */
976     if( fChangeFocus && GetFocus() )
977         if( WIN_GetTopParent(GetFocus()) != hwndActive )
978             FOCUS_SwitchFocus( GetFocus(),
979                                (wndPtr->dwStyle & WS_MINIMIZE)? 0: hwndActive);
980
981     /* if active wnd is minimized redraw icon title 
982   if( hwndActive )
983       {
984         wndPtr = WIN_FindWndPtr(hwndActive);
985         if(wndPtr->dwStyle & WS_MINIMIZE)
986            RedrawIconTitle(hwndActive);
987     }
988   */
989     return (hWnd == hwndActive);
990 }
991
992
993 /*******************************************************************
994  *         WINPOS_ChangeActiveWindow
995  *
996  */
997 BOOL WINPOS_ChangeActiveWindow( HWND hWnd, BOOL mouseMsg )
998 {
999     WND *wndPtr = WIN_FindWndPtr(hWnd);
1000
1001     if( !wndPtr ) return FALSE;
1002
1003     /* child windows get WM_CHILDACTIVATE message */
1004     if( (wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) == WS_CHILD )
1005         return SendMessage16(hWnd, WM_CHILDACTIVATE, 0, 0L);
1006
1007         /* owned popups imply owner activation - not sure */
1008     if ((wndPtr->dwStyle & WS_POPUP) && wndPtr->owner &&
1009         !(wndPtr->owner->dwStyle & WS_DISABLED ))
1010     {
1011         if (!(wndPtr = wndPtr->owner)) return FALSE;
1012         hWnd = wndPtr->hwndSelf;
1013     }
1014
1015     if( hWnd == hwndActive ) return FALSE;
1016
1017     if( !WINPOS_SetActiveWindow(hWnd ,mouseMsg ,TRUE) )
1018         return FALSE;
1019
1020     /* switch desktop queue to current active */
1021     if( wndPtr->parent == WIN_GetDesktop())
1022         WIN_GetDesktop()->hmemTaskQ = wndPtr->hmemTaskQ;
1023
1024     return TRUE;
1025 }
1026
1027
1028 /***********************************************************************
1029  *           WINPOS_SendNCCalcSize
1030  *
1031  * Send a WM_NCCALCSIZE message to a window.
1032  * All parameters are read-only except newClientRect.
1033  * oldWindowRect, oldClientRect and winpos must be non-NULL only
1034  * when calcValidRect is TRUE.
1035  */
1036 LONG WINPOS_SendNCCalcSize( HWND hwnd, BOOL calcValidRect,
1037                             RECT16 *newWindowRect, RECT16 *oldWindowRect,
1038                             RECT16 *oldClientRect, SEGPTR winpos,
1039                             RECT16 *newClientRect )
1040 {
1041     NCCALCSIZE_PARAMS16 *params;
1042     LONG result;
1043
1044     if (!(params = SEGPTR_NEW(NCCALCSIZE_PARAMS16))) return 0;
1045     params->rgrc[0] = *newWindowRect;
1046     if (calcValidRect)
1047     {
1048         params->rgrc[1] = *oldWindowRect;
1049         params->rgrc[2] = *oldClientRect;
1050         params->lppos = winpos;
1051     }
1052     result = SendMessage16( hwnd, WM_NCCALCSIZE, calcValidRect,
1053                           (LPARAM)SEGPTR_GET( params ) );
1054     dprintf_win(stddeb, "WINPOS_SendNCCalcSize: %d %d %d %d\n",
1055                 (int)params->rgrc[0].top,    (int)params->rgrc[0].left,
1056                 (int)params->rgrc[0].bottom, (int)params->rgrc[0].right);
1057     *newClientRect = params->rgrc[0];
1058     SEGPTR_FREE(params);
1059     return result;
1060 }
1061
1062
1063 /***********************************************************************
1064  *           WINPOS_HandleWindowPosChanging16
1065  *
1066  * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1067  */
1068 LONG WINPOS_HandleWindowPosChanging16( WND *wndPtr, WINDOWPOS16 *winpos )
1069 {
1070     POINT16 maxSize;
1071     if (winpos->flags & SWP_NOSIZE) return 0;
1072     if ((wndPtr->dwStyle & WS_THICKFRAME) ||
1073         ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) == 0))
1074     {
1075         NC_GetMinMaxInfo( winpos->hwnd, &maxSize, NULL, NULL, NULL );
1076         winpos->cx = MIN( winpos->cx, maxSize.x );
1077         winpos->cy = MIN( winpos->cy, maxSize.y );
1078     }
1079     return 0;
1080 }
1081
1082
1083 /***********************************************************************
1084  *           WINPOS_HandleWindowPosChanging32
1085  *
1086  * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1087  */
1088 LONG WINPOS_HandleWindowPosChanging32( WND *wndPtr, WINDOWPOS32 *winpos )
1089 {
1090     POINT16 maxSize;
1091     if (winpos->flags & SWP_NOSIZE) return 0;
1092     if ((wndPtr->dwStyle & WS_THICKFRAME) ||
1093         ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) == 0))
1094     {
1095         NC_GetMinMaxInfo( winpos->hwnd, &maxSize, NULL, NULL, NULL );
1096         winpos->cx = MIN( winpos->cx, maxSize.x );
1097         winpos->cy = MIN( winpos->cy, maxSize.y );
1098     }
1099     return 0;
1100 }
1101
1102
1103 /***********************************************************************
1104  *           WINPOS_MoveWindowZOrder
1105  *
1106  * Move a window in Z order, invalidating everything that needs it.
1107  * Only necessary for windows without associated X window.
1108  */
1109 static void WINPOS_MoveWindowZOrder( HWND hwnd, HWND hwndAfter )
1110 {
1111     BOOL movingUp;
1112     WND *pWndAfter, *pWndCur, *wndPtr = WIN_FindWndPtr( hwnd );
1113
1114     /* We have two possible cases:
1115      * - The window is moving up: we have to invalidate all areas
1116      *   of the window that were covered by other windows
1117      * - The window is moving down: we have to invalidate areas
1118      *   of other windows covered by this one.
1119      */
1120
1121     if (hwndAfter == HWND_TOP)
1122     {
1123         movingUp = TRUE;
1124     }
1125     else if (hwndAfter == HWND_BOTTOM)
1126     {
1127         if (!wndPtr->next) return;  /* Already at the bottom */
1128         movingUp = FALSE;
1129     }
1130     else
1131     {
1132         if (!(pWndAfter = WIN_FindWndPtr( hwndAfter ))) return;
1133         if (wndPtr->next == pWndAfter) return;  /* Already placed right */
1134
1135           /* Determine which window we encounter first in Z-order */
1136         pWndCur = wndPtr->parent->child;
1137         while ((pWndCur != wndPtr) && (pWndCur != pWndAfter))
1138             pWndCur = pWndCur->next;
1139         movingUp = (pWndCur == pWndAfter);
1140     }
1141
1142     if (movingUp)
1143     {
1144         WND *pWndPrevAfter = wndPtr->next;
1145         WIN_UnlinkWindow( hwnd );
1146         WIN_LinkWindow( hwnd, hwndAfter );
1147         pWndCur = wndPtr->next;
1148         while (pWndCur != pWndPrevAfter)
1149         {
1150             RECT16 rect = pWndCur->rectWindow;
1151             OffsetRect16( &rect, -wndPtr->rectClient.left,
1152                           -wndPtr->rectClient.top );
1153             RedrawWindow16( hwnd, &rect, 0, RDW_INVALIDATE | RDW_ALLCHILDREN |
1154                             RDW_FRAME | RDW_ERASE );
1155             pWndCur = pWndCur->next;
1156         }
1157     }
1158     else  /* Moving down */
1159     {
1160         pWndCur = wndPtr->next;
1161         WIN_UnlinkWindow( hwnd );
1162         WIN_LinkWindow( hwnd, hwndAfter );
1163         while (pWndCur != wndPtr)
1164         {
1165             RECT16 rect = wndPtr->rectWindow;
1166             OffsetRect16( &rect, -pWndCur->rectClient.left,
1167                           -pWndCur->rectClient.top );
1168             RedrawWindow16( pWndCur->hwndSelf, &rect, 0, RDW_INVALIDATE |
1169                             RDW_ALLCHILDREN | RDW_FRAME | RDW_ERASE );
1170             pWndCur = pWndCur->next;
1171         }
1172     }
1173 }
1174
1175 /***********************************************************************
1176  *           WINPOS_ReorderOwnedPopups
1177  *
1178  * fix Z order taking into account owned popups -
1179  * basically we need to maintain them above owner window
1180  */
1181 HWND WINPOS_ReorderOwnedPopups(HWND hwndInsertAfter, WND* wndPtr, WORD flags)
1182 {
1183  WND*   w = WIN_GetDesktop();
1184
1185  w = w->child;
1186
1187  /* if we are dealing with owned popup... 
1188   */
1189  if( wndPtr->dwStyle & WS_POPUP && wndPtr->owner && hwndInsertAfter != HWND_TOP )
1190    {
1191      BOOL bFound = FALSE;
1192      HWND hwndLocalPrev = HWND_TOP;
1193      HWND hwndNewAfter = 0;
1194
1195      while( w )
1196        {
1197          if( !bFound && hwndInsertAfter == hwndLocalPrev )
1198              hwndInsertAfter = HWND_TOP;
1199
1200          if( w->dwStyle & WS_POPUP && w->owner == wndPtr->owner )
1201            {
1202              bFound = TRUE;
1203
1204              if( hwndInsertAfter == HWND_TOP )
1205                {
1206                  hwndInsertAfter = hwndLocalPrev;
1207                  break;
1208                }
1209              hwndNewAfter = hwndLocalPrev;
1210            }
1211
1212          if( w == wndPtr->owner )
1213            {
1214              /* basically HWND_BOTTOM */
1215              hwndInsertAfter = hwndLocalPrev;
1216
1217              if( bFound )
1218                  hwndInsertAfter = hwndNewAfter;
1219              break;
1220            }
1221
1222            if( w != wndPtr )
1223                hwndLocalPrev = w->hwndSelf;
1224
1225            w = w->next;
1226         }
1227    }
1228  else 
1229    /* or overlapped top-level window... 
1230     */
1231    if( !(wndPtr->dwStyle & WS_CHILD) )
1232       while( w )
1233         {
1234           if( w == wndPtr ) break;
1235
1236           if( w->dwStyle & WS_POPUP && w->owner == wndPtr )
1237             {
1238               SetWindowPos(w->hwndSelf, hwndInsertAfter, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE |
1239                                         SWP_NOACTIVATE | SWP_NOSENDCHANGING | SWP_DEFERERASE);
1240               hwndInsertAfter = w->hwndSelf;
1241             }
1242           w = w->next;
1243         }
1244
1245   return hwndInsertAfter;
1246 }
1247
1248 /***********************************************************************
1249  *           WINPOS_SizeMoveClean
1250  *
1251  * Make window look nice without excessive repainting
1252  *
1253  * the pain:
1254  *
1255  * visible regions are in window coordinates
1256  * update regions are in window client coordinates
1257  * client and window rectangles are in parent client coordinates
1258  */
1259 static void WINPOS_SizeMoveClean(WND* Wnd, HRGN oldVisRgn, LPRECT16 lpOldWndRect, LPRECT16 lpOldClientRect, BOOL bNoCopy )
1260 {
1261  HRGN newVisRgn    = DCE_GetVisRgn(Wnd->hwndSelf, DCX_WINDOW | DCX_CLIPSIBLINGS );
1262  HRGN dirtyRgn     = CreateRectRgn(0,0,0,0);
1263  int  other, my;
1264
1265  dprintf_win(stddeb,"cleaning up...new wnd=(%i %i-%i %i) old wnd=(%i %i-%i %i)\n\
1266 \t\tnew client=(%i %i-%i %i) old client=(%i %i-%i %i)\n",
1267                    Wnd->rectWindow.left, Wnd->rectWindow.top, Wnd->rectWindow.right, Wnd->rectWindow.bottom,
1268                    lpOldWndRect->left, lpOldWndRect->top, lpOldWndRect->right, lpOldWndRect->bottom,
1269                    Wnd->rectClient.left,Wnd->rectClient.top,Wnd->rectClient.right,Wnd->rectClient.bottom,
1270                    lpOldClientRect->left,lpOldClientRect->top,lpOldClientRect->right,lpOldClientRect->bottom);
1271
1272  CombineRgn( dirtyRgn, newVisRgn, 0, RGN_COPY);
1273
1274  if( !bNoCopy )
1275    {
1276      HRGN hRgn = CreateRectRgn( lpOldClientRect->left - lpOldWndRect->left, lpOldClientRect->top - lpOldWndRect->top,
1277                                 lpOldClientRect->right - lpOldWndRect->left, lpOldClientRect->bottom - lpOldWndRect->top);
1278      CombineRgn( newVisRgn, newVisRgn, oldVisRgn, RGN_AND ); 
1279      CombineRgn( newVisRgn, newVisRgn, hRgn, RGN_AND );
1280      DeleteObject(hRgn);
1281    }
1282
1283  /* map regions to the parent client area */
1284  
1285  OffsetRgn(dirtyRgn, Wnd->rectWindow.left, Wnd->rectWindow.top);
1286  OffsetRgn(oldVisRgn, lpOldWndRect->left, lpOldWndRect->top);
1287
1288  /* compute invalidated region outside Wnd - (in client coordinates of the parent window) */
1289
1290  other = CombineRgn(dirtyRgn, oldVisRgn, dirtyRgn, RGN_DIFF);
1291
1292  /* map visible region to the Wnd client area */
1293
1294  OffsetRgn( newVisRgn, Wnd->rectWindow.left - Wnd->rectClient.left,
1295                        Wnd->rectWindow.top - Wnd->rectClient.top );
1296
1297  /* substract previously invalidated region from the Wnd visible region */
1298
1299  my =  (Wnd->hrgnUpdate > 1)? CombineRgn( newVisRgn, newVisRgn, Wnd->hrgnUpdate, RGN_DIFF)
1300                             : COMPLEXREGION;
1301
1302  if( bNoCopy )          /* invalidate Wnd visible region */
1303    {
1304      if (my != NULLREGION)  RedrawWindow32( Wnd->hwndSelf, NULL, newVisRgn, RDW_INVALIDATE |
1305                             RDW_FRAME | RDW_ALLCHILDREN | RDW_ERASE );
1306    } 
1307  else                   /* bitblt old client area */
1308    { 
1309      HDC   hDC;
1310      int   update;
1311      HRGN  updateRgn;
1312
1313      /* client rect */
1314
1315      updateRgn = CreateRectRgn( 0,0, Wnd->rectClient.right - Wnd->rectClient.left,
1316                                 Wnd->rectClient.bottom - Wnd->rectClient.top );
1317
1318      /* clip visible region with client rect */
1319
1320      my = CombineRgn( newVisRgn, newVisRgn, updateRgn, RGN_AND );
1321
1322      /* substract result from client rect to get region that won't be copied */
1323
1324      update = CombineRgn( updateRgn, updateRgn, newVisRgn, RGN_DIFF );
1325
1326      /* Blt valid bits using parent window DC */
1327
1328      if( my != NULLREGION )
1329        {
1330          int xfrom = lpOldClientRect->left;
1331          int yfrom = lpOldClientRect->top;
1332          int xto = Wnd->rectClient.left;
1333          int yto = Wnd->rectClient.top;
1334
1335          /* check if we can skip copying */
1336
1337          if( xfrom != xto || yfrom != yto )
1338            {
1339              /* compute clipping region in parent client coordinates */
1340
1341              OffsetRgn( newVisRgn, Wnd->rectClient.left, Wnd->rectClient.top);
1342              CombineRgn( oldVisRgn, oldVisRgn, newVisRgn, RGN_OR );
1343
1344              hDC = GetDCEx( Wnd->parent->hwndSelf, oldVisRgn, DCX_KEEPCLIPRGN | DCX_INTERSECTRGN | DCX_CACHE | DCX_CLIPSIBLINGS);
1345
1346              BitBlt(hDC, xto, yto, lpOldClientRect->right - lpOldClientRect->left, 
1347                                    lpOldClientRect->bottom - lpOldClientRect->top,
1348                                    hDC, xfrom, yfrom, SRCCOPY );
1349     
1350              ReleaseDC( Wnd->parent->hwndSelf, hDC); 
1351           }
1352        }
1353
1354      if( update != NULLREGION )
1355          RedrawWindow32( Wnd->hwndSelf, NULL, updateRgn, RDW_INVALIDATE |
1356                          RDW_FRAME | RDW_ALLCHILDREN | RDW_ERASE );
1357      DeleteObject( updateRgn );
1358    }
1359
1360  /* erase uncovered areas */
1361
1362  if( other != NULLREGION )
1363      RedrawWindow32( Wnd->parent->hwndSelf, NULL, dirtyRgn,
1364                      RDW_INVALIDATE | RDW_ALLCHILDREN | RDW_ERASE );
1365
1366  DeleteObject(dirtyRgn);
1367  DeleteObject(newVisRgn);
1368 }
1369
1370 /***********************************************************************
1371  *           WINPOS_SetXWindowPos
1372  *
1373  * SetWindowPos() for an X window. Used by the real SetWindowPos().
1374  */
1375 static void WINPOS_SetXWindowPos( WINDOWPOS16 *winpos )
1376 {
1377     XWindowChanges winChanges;
1378     int changeMask = 0;
1379     WND *wndPtr = WIN_FindWndPtr( winpos->hwnd );
1380
1381     if (!(winpos->flags & SWP_NOSIZE))
1382     {
1383         winChanges.width     = winpos->cx;
1384         winChanges.height    = winpos->cy;
1385         changeMask |= CWWidth | CWHeight;
1386     }
1387     if (!(winpos->flags & SWP_NOMOVE))
1388     {
1389         winChanges.x = winpos->x;
1390         winChanges.y = winpos->y;
1391         changeMask |= CWX | CWY;
1392     }
1393     if (!(winpos->flags & SWP_NOZORDER))
1394     {
1395         if (winpos->hwndInsertAfter == HWND_TOP) winChanges.stack_mode = Above;
1396         else winChanges.stack_mode = Below;
1397         if ((winpos->hwndInsertAfter != HWND_TOP) &&
1398             (winpos->hwndInsertAfter != HWND_BOTTOM))
1399         {
1400             WND * insertPtr = WIN_FindWndPtr( winpos->hwndInsertAfter );
1401             winChanges.sibling = insertPtr->window;
1402             changeMask |= CWSibling;
1403         }
1404         changeMask |= CWStackMode;
1405     }
1406     if (changeMask)
1407         XConfigureWindow( display, wndPtr->window, changeMask, &winChanges );
1408 }
1409
1410
1411 /***********************************************************************
1412  *           SetWindowPos   (USER.232)
1413  */
1414 BOOL SetWindowPos( HWND hwnd, HWND hwndInsertAfter, INT x, INT y,
1415                    INT cx, INT cy, WORD flags )
1416 {
1417     WINDOWPOS16 winpos;
1418     WND *       wndPtr;
1419     RECT16      newWindowRect, newClientRect;
1420     HRGN        visRgn = 0;
1421     int         result = 0;
1422
1423     dprintf_win(stddeb,"SetWindowPos: hwnd %04x, flags %08x\n", hwnd, flags);  
1424
1425       /* Check window handle */
1426
1427     if (hwnd == GetDesktopWindow()) return FALSE;
1428     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
1429
1430     /* Check for windows that may not be resized 
1431        FIXME: this should be done only for Windows 3.0 programs */
1432     if (flags ==(SWP_SHOWWINDOW) || flags ==(SWP_HIDEWINDOW ) )
1433        flags |= SWP_NOSIZE | SWP_NOMOVE;
1434
1435       /* Check dimensions */
1436
1437     if (cx <= 0) cx = 1;
1438     if (cy <= 0) cy = 1;
1439
1440       /* Check flags */
1441
1442     if (hwnd == hwndActive) flags |= SWP_NOACTIVATE;   /* Already active */
1443     if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == cx) &&
1444         (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == cy))
1445         flags |= SWP_NOSIZE;    /* Already the right size */
1446     if ((wndPtr->rectWindow.left == x) && (wndPtr->rectWindow.top == y))
1447         flags |= SWP_NOMOVE;    /* Already the right position */
1448
1449       /* Check hwndInsertAfter */
1450
1451     if (!(flags & (SWP_NOZORDER | SWP_NOACTIVATE)))
1452     {
1453           /* Ignore TOPMOST flags when activating a window */
1454           /* _and_ moving it in Z order. */
1455         if ((hwndInsertAfter == HWND_TOPMOST) ||
1456             (hwndInsertAfter == HWND_NOTOPMOST))
1457             hwndInsertAfter = HWND_TOP; 
1458     }
1459       /* TOPMOST not supported yet */
1460     if ((hwndInsertAfter == HWND_TOPMOST) ||
1461         (hwndInsertAfter == HWND_NOTOPMOST)) hwndInsertAfter = HWND_TOP;
1462
1463       /* hwndInsertAfter must be a sibling of the window */
1464     if ((hwndInsertAfter != HWND_TOP) && (hwndInsertAfter != HWND_BOTTOM))
1465        {
1466          WND* wnd = WIN_FindWndPtr(hwndInsertAfter);
1467          if( wnd->parent != wndPtr->parent ) return FALSE;
1468          if( wnd->next == wndPtr ) flags |= SWP_NOZORDER;
1469        }
1470     else
1471        if (hwndInsertAfter == HWND_TOP)
1472            flags |= ( wndPtr->parent->child == wndPtr)? SWP_NOZORDER: 0;
1473        else /* HWND_BOTTOM */
1474            flags |= ( wndPtr->next )? 0: SWP_NOZORDER;
1475
1476       /* Fill the WINDOWPOS structure */
1477
1478     winpos.hwnd = hwnd;
1479     winpos.hwndInsertAfter = hwndInsertAfter;
1480     winpos.x = x;
1481     winpos.y = y;
1482     winpos.cx = cx;
1483     winpos.cy = cy;
1484     winpos.flags = flags;
1485     
1486       /* Send WM_WINDOWPOSCHANGING message */
1487
1488     if (!(flags & SWP_NOSENDCHANGING))
1489         SendMessage16( hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)MAKE_SEGPTR(&winpos) );
1490
1491       /* Calculate new position and size */
1492
1493     newWindowRect = wndPtr->rectWindow;
1494     newClientRect = wndPtr->rectClient;
1495
1496     if (!(winpos.flags & SWP_NOSIZE))
1497     {
1498         newWindowRect.right  = newWindowRect.left + winpos.cx;
1499         newWindowRect.bottom = newWindowRect.top + winpos.cy;
1500     }
1501     if (!(winpos.flags & SWP_NOMOVE))
1502     {
1503         newWindowRect.left    = winpos.x;
1504         newWindowRect.top     = winpos.y;
1505         newWindowRect.right  += winpos.x - wndPtr->rectWindow.left;
1506         newWindowRect.bottom += winpos.y - wndPtr->rectWindow.top;
1507
1508         OffsetRect16( &newClientRect, winpos.x - wndPtr->rectWindow.left, 
1509                                       winpos.y - wndPtr->rectWindow.top );
1510     }
1511
1512     winpos.flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
1513
1514       /* Reposition window in Z order */
1515
1516     if (!(winpos.flags & SWP_NOZORDER))
1517     {
1518         /* reorder owned popups if hwnd is top-level window 
1519          */
1520         if( wndPtr->parent == WIN_GetDesktop() )
1521             hwndInsertAfter = WINPOS_ReorderOwnedPopups( hwndInsertAfter,
1522                                                          wndPtr, flags );
1523
1524         if (wndPtr->window)
1525         {
1526             WIN_UnlinkWindow( winpos.hwnd );
1527             WIN_LinkWindow( winpos.hwnd, hwndInsertAfter );
1528         }
1529         else WINPOS_MoveWindowZOrder( winpos.hwnd, hwndInsertAfter );
1530     }
1531
1532     if ( !wndPtr->window && !(flags & SWP_NOREDRAW) && 
1533         (!(flags & SWP_NOMOVE) || !(flags & SWP_NOSIZE) || (flags & SWP_FRAMECHANGED)) )
1534           visRgn = DCE_GetVisRgn(hwnd, DCX_WINDOW | DCX_CLIPSIBLINGS);
1535
1536
1537       /* Send WM_NCCALCSIZE message to get new client area */
1538     if( (flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
1539       {
1540          result = WINPOS_SendNCCalcSize( winpos.hwnd, TRUE, &newWindowRect,
1541                                     &wndPtr->rectWindow, &wndPtr->rectClient,
1542                                     MAKE_SEGPTR(&winpos), &newClientRect );
1543
1544          /* FIXME: WVR_ALIGNxxx */
1545
1546          if( newClientRect.left != wndPtr->rectClient.left ||
1547              newClientRect.top != wndPtr->rectClient.top )
1548              winpos.flags &= ~SWP_NOCLIENTMOVE;
1549
1550          if( (newClientRect.right - newClientRect.left !=
1551              wndPtr->rectClient.right - wndPtr->rectClient.left) ||
1552             (newClientRect.bottom - newClientRect.top !=
1553              wndPtr->rectClient.bottom - wndPtr->rectClient.top) )
1554              winpos.flags &= ~SWP_NOCLIENTSIZE;
1555       }
1556     else
1557       if( !(flags & SWP_NOMOVE) && (newClientRect.left != wndPtr->rectClient.left ||
1558                                     newClientRect.top != wndPtr->rectClient.top) )
1559             winpos.flags &= ~SWP_NOCLIENTMOVE;
1560
1561     /* Update active DCEs */
1562
1563     if( !(flags & SWP_NOZORDER) || (flags & SWP_HIDEWINDOW) || (flags & SWP_SHOWWINDOW)
1564                                 || (memcmp(&newWindowRect,&wndPtr->rectWindow,sizeof(RECT16))
1565                                     && wndPtr->dwStyle & WS_VISIBLE ) )
1566       {
1567         RECT16 rect;
1568
1569         UnionRect16(&rect,&newWindowRect,&wndPtr->rectWindow);
1570         DCE_InvalidateDCE(wndPtr->parent, &rect);
1571       }
1572
1573     /* Perform the moving and resizing */
1574
1575     if (wndPtr->window)
1576     {
1577         RECT16 oldWindowRect = wndPtr->rectWindow;
1578         RECT16 oldClientRect = wndPtr->rectClient;
1579
1580         HWND bogusInsertAfter = winpos.hwndInsertAfter;
1581
1582         winpos.hwndInsertAfter = hwndInsertAfter;
1583         WINPOS_SetXWindowPos( &winpos );
1584
1585         wndPtr->rectWindow = newWindowRect;
1586         wndPtr->rectClient = newClientRect;
1587         winpos.hwndInsertAfter = bogusInsertAfter;
1588
1589         /*  FIXME: should do something like WINPOS_SizeMoveClean */
1590
1591         if( (oldClientRect.left - oldWindowRect.left !=
1592              newClientRect.left - newWindowRect.left) ||
1593             (oldClientRect.top - oldWindowRect.top !=
1594              newClientRect.top - newWindowRect.top) )
1595
1596             RedrawWindow32( wndPtr->hwndSelf, NULL, 0, RDW_INVALIDATE |
1597                             RDW_ALLCHILDREN | RDW_FRAME | RDW_ERASE );
1598         else
1599             if( winpos.flags & SWP_FRAMECHANGED )
1600               {
1601                 WORD wErase = 0;
1602                 RECT32 rect;
1603
1604                 if( oldClientRect.right > newClientRect.right ) 
1605                 {
1606                     rect.left = newClientRect.right; rect.top = newClientRect.top;
1607                     rect.right = oldClientRect.right; rect.bottom = newClientRect.bottom;
1608                     wErase = 1;
1609                     RedrawWindow32( wndPtr->hwndSelf, &rect, 0,
1610                                 RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN );
1611                 }
1612                 if( oldClientRect.bottom > newClientRect.bottom )
1613                 {
1614                     rect.left = newClientRect.left; rect.top = newClientRect.bottom;
1615                     rect.right = (wErase)?oldClientRect.right:newClientRect.right;
1616                     rect.bottom = oldClientRect.bottom;
1617                     wErase = 1;
1618                     RedrawWindow32( wndPtr->hwndSelf, &rect, 0,
1619                                 RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN );
1620                 }
1621                 if( !wErase ) wndPtr->flags |= WIN_NEEDS_NCPAINT;
1622               }
1623     }
1624     else
1625     {
1626         RECT16 oldWindowRect = wndPtr->rectWindow;
1627         RECT16 oldClientRect = wndPtr->rectClient;
1628
1629         wndPtr->rectWindow = newWindowRect;
1630         wndPtr->rectClient = newClientRect;
1631
1632         if( !(flags & SWP_NOREDRAW) )
1633           {
1634             BOOL bNoCopy = (flags & SWP_NOCOPYBITS) || 
1635                            (result >= WVR_HREDRAW && result < WVR_VALIDRECTS);
1636
1637             if( (winpos.flags & SWP_NOPOSCHANGE) != SWP_NOPOSCHANGE )
1638                 /* optimize cleanup by BitBlt'ing where possible */
1639
1640                 WINPOS_SizeMoveClean(wndPtr, visRgn, &oldWindowRect, &oldClientRect, bNoCopy);
1641             else
1642                if( winpos.flags & SWP_FRAMECHANGED )
1643                   RedrawWindow32( winpos.hwnd, NULL, 0, RDW_NOCHILDREN | RDW_FRAME ); 
1644
1645           }
1646         DeleteObject(visRgn);
1647     }
1648
1649     if (flags & SWP_SHOWWINDOW)
1650     {
1651         wndPtr->dwStyle |= WS_VISIBLE;
1652         if (wndPtr->window)
1653         {
1654             XMapWindow( display, wndPtr->window );
1655         }
1656         else
1657         {
1658             if (!(flags & SWP_NOREDRAW))
1659                 RedrawWindow32( winpos.hwnd, NULL, 0,
1660                                 RDW_INVALIDATE | RDW_ALLCHILDREN |
1661                                 RDW_FRAME | RDW_ERASE );
1662         }
1663     }
1664     else if (flags & SWP_HIDEWINDOW)
1665     {
1666         wndPtr->dwStyle &= ~WS_VISIBLE;
1667         if (wndPtr->window)
1668         {
1669             XUnmapWindow( display, wndPtr->window );
1670         }
1671         else
1672         {
1673             if (!(flags & SWP_NOREDRAW))
1674                 RedrawWindow16( wndPtr->parent->hwndSelf, &wndPtr->rectWindow, 0,
1675                                 RDW_INVALIDATE | RDW_ALLCHILDREN | RDW_ERASE );
1676         }
1677
1678         if ((winpos.hwnd == GetFocus()) || IsChild(winpos.hwnd, GetFocus()))
1679             SetFocus( GetParent(winpos.hwnd) );  /* Revert focus to parent */
1680
1681         if (winpos.hwnd == hwndActive)
1682         {
1683               /* Activate previously active window if possible */
1684             HWND newActive = hwndPrevActive;
1685             if (!IsWindow(newActive) || (newActive == winpos.hwnd))
1686             {
1687                 newActive = GetTopWindow( GetDesktopWindow() );
1688                 if (newActive == winpos.hwnd)
1689                     newActive = wndPtr->next ? wndPtr->next->hwndSelf : 0;
1690             }       
1691             WINPOS_ChangeActiveWindow( newActive, FALSE );
1692         }
1693     }
1694
1695       /* Activate the window */
1696
1697     if (!(flags & SWP_NOACTIVATE))
1698             WINPOS_ChangeActiveWindow( winpos.hwnd, FALSE );
1699     
1700       /* Repaint the window */
1701
1702     if (wndPtr->window) EVENT_Synchronize();  /* Wait for all expose events */
1703
1704     EVENT_DummyMotionNotify(); /* Simulate a mouse event to set the cursor */
1705
1706     if (!(flags & SWP_DEFERERASE))
1707         RedrawWindow32( wndPtr->parent->hwndSelf, NULL, 0,
1708                         RDW_ALLCHILDREN | RDW_ERASENOW );
1709
1710       /* And last, send the WM_WINDOWPOSCHANGED message */
1711
1712     if (!(winpos.flags & SWP_NOSENDCHANGING))
1713         SendMessage16( winpos.hwnd, WM_WINDOWPOSCHANGED,
1714                        0, (LPARAM)MAKE_SEGPTR(&winpos) );
1715
1716     return TRUE;
1717 }
1718
1719                                         
1720 /***********************************************************************
1721  *           BeginDeferWindowPos   (USER.259)
1722  */
1723 HDWP16 BeginDeferWindowPos( INT count )
1724 {
1725     HDWP16 handle;
1726     DWP *pDWP;
1727
1728     if (count <= 0) return 0;
1729     handle = USER_HEAP_ALLOC( sizeof(DWP) + (count-1)*sizeof(WINDOWPOS16) );
1730     if (!handle) return 0;
1731     pDWP = (DWP *) USER_HEAP_LIN_ADDR( handle );
1732     pDWP->actualCount    = 0;
1733     pDWP->suggestedCount = count;
1734     pDWP->valid          = TRUE;
1735     pDWP->wMagic         = DWP_MAGIC;
1736     pDWP->hwndParent     = 0;
1737     return handle;
1738 }
1739
1740
1741 /***********************************************************************
1742  *           DeferWindowPos   (USER.260)
1743  */
1744 HDWP16 DeferWindowPos( HDWP16 hdwp, HWND hwnd, HWND hwndAfter, INT x, INT y,
1745                        INT cx, INT cy, UINT flags )
1746 {
1747     DWP *pDWP;
1748     int i;
1749     HDWP16 newhdwp = hdwp;
1750     HWND parent;
1751
1752     pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
1753     if (!pDWP) return 0;
1754     if (hwnd == GetDesktopWindow()) return 0;
1755
1756       /* All the windows of a DeferWindowPos() must have the same parent */
1757
1758     parent = WIN_FindWndPtr( hwnd )->parent->hwndSelf;
1759     if (pDWP->actualCount == 0) pDWP->hwndParent = parent;
1760     else if (parent != pDWP->hwndParent)
1761     {
1762         USER_HEAP_FREE( hdwp );
1763         return 0;
1764     }
1765
1766     for (i = 0; i < pDWP->actualCount; i++)
1767     {
1768         if (pDWP->winPos[i].hwnd == hwnd)
1769         {
1770               /* Merge with the other changes */
1771             if (!(flags & SWP_NOZORDER))
1772             {
1773                 pDWP->winPos[i].hwndInsertAfter = hwndAfter;
1774             }
1775             if (!(flags & SWP_NOMOVE))
1776             {
1777                 pDWP->winPos[i].x = x;
1778                 pDWP->winPos[i].y = y;
1779             }                
1780             if (!(flags & SWP_NOSIZE))
1781             {
1782                 pDWP->winPos[i].cx = cx;
1783                 pDWP->winPos[i].cy = cy;
1784             }
1785             pDWP->winPos[i].flags &= flags & (SWP_NOSIZE | SWP_NOMOVE |
1786                                               SWP_NOZORDER | SWP_NOREDRAW |
1787                                               SWP_NOACTIVATE | SWP_NOCOPYBITS |
1788                                               SWP_NOOWNERZORDER);
1789             pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
1790                                               SWP_FRAMECHANGED);
1791             return hdwp;
1792         }
1793     }
1794     if (pDWP->actualCount >= pDWP->suggestedCount)
1795     {
1796         newhdwp = USER_HEAP_REALLOC( hdwp,
1797                       sizeof(DWP) + pDWP->suggestedCount*sizeof(WINDOWPOS16) );
1798         if (!newhdwp) return 0;
1799         pDWP = (DWP *) USER_HEAP_LIN_ADDR( newhdwp );
1800         pDWP->suggestedCount++;
1801     }
1802     pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
1803     pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
1804     pDWP->winPos[pDWP->actualCount].x = x;
1805     pDWP->winPos[pDWP->actualCount].y = y;
1806     pDWP->winPos[pDWP->actualCount].cx = cx;
1807     pDWP->winPos[pDWP->actualCount].cy = cy;
1808     pDWP->winPos[pDWP->actualCount].flags = flags;
1809     pDWP->actualCount++;
1810     return newhdwp;
1811 }
1812
1813
1814 /***********************************************************************
1815  *           EndDeferWindowPos   (USER.261)
1816  */
1817 BOOL EndDeferWindowPos( HDWP16 hdwp )
1818 {
1819     DWP *pDWP;
1820     WINDOWPOS16 *winpos;
1821     BOOL res = TRUE;
1822     int i;
1823
1824     pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
1825     if (!pDWP) return FALSE;
1826     for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
1827     {
1828         if (!(res = SetWindowPos( winpos->hwnd, winpos->hwndInsertAfter,
1829                                   winpos->x, winpos->y, winpos->cx, winpos->cy,
1830                                   winpos->flags ))) break;
1831     }
1832     USER_HEAP_FREE( hdwp );
1833     return res;
1834 }
1835
1836
1837 /***********************************************************************
1838  *           TileChildWindows   (USER.199)
1839  */
1840 void TileChildWindows( HWND parent, WORD action )
1841 {
1842     printf("STUB TileChildWindows(%04x, %d)\n", parent, action);
1843 }
1844
1845 /***********************************************************************
1846  *           CascageChildWindows   (USER.198)
1847  */
1848 void CascadeChildWindows( HWND parent, WORD action )
1849 {
1850     printf("STUB CascadeChildWindows(%04x, %d)\n", parent, action);
1851 }