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