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