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