Release 960302
[wine] / windows / winpos.c
1 /*
2  * Window position related functions.
3  *
4  * Copyright 1993, 1994, 1995 Alexandre Julliard
5  *                       1995,1996 Alex Korobka
6  */
7
8 #include "sysmetrics.h"
9 #include "selectors.h"
10 #include "user.h"
11 #include "win.h"
12 #include "event.h"
13 #include "hook.h"
14 #include "message.h"
15 #include "stackframe.h"
16 #include "winpos.h"
17 #include "nonclient.h"
18 #include "stddebug.h"
19 /* #define DEBUG_WIN */
20 #include "debug.h"
21
22 /* ----- external functions ----- */
23
24 void    FOCUS_SwitchFocus( HWND , HWND );
25
26 /* ----- internal variables ----- */
27
28 static HWND hwndActive      = 0;  /* Currently active window */
29 static HWND hwndPrevActive  = 0;  /* Previously active window */
30
31
32 /***********************************************************************
33  *           WINPOS_FindIconPos
34  *
35  * Find a suitable place for an iconic window.
36  * The new position is stored into wndPtr->ptIconPos.
37  */
38 void WINPOS_FindIconPos( HWND hwnd )
39 {
40     RECT rectParent;
41     short x, y, xspacing, yspacing;
42     WND * wndPtr = WIN_FindWndPtr( hwnd );
43
44     if (!wndPtr) return;
45     GetClientRect( wndPtr->hwndParent, &rectParent );
46     if ((wndPtr->ptIconPos.x >= rectParent.left) &&
47         (wndPtr->ptIconPos.x + SYSMETRICS_CXICON < rectParent.right) &&
48         (wndPtr->ptIconPos.y >= rectParent.top) &&
49         (wndPtr->ptIconPos.y + SYSMETRICS_CYICON < rectParent.bottom))
50         return;  /* The icon already has a suitable position */
51
52     xspacing = yspacing = 70;  /* FIXME: This should come from WIN.INI */
53     y = rectParent.bottom;
54     for (;;)
55     {
56         for (x = rectParent.left; x<=rectParent.right-xspacing; x += xspacing)
57         {
58               /* Check if another icon already occupies this spot */
59             HWND hwndChild = GetWindow( wndPtr->hwndParent, GW_CHILD );
60             while (hwndChild)
61             {
62                 WND *childPtr = WIN_FindWndPtr( hwndChild );
63                 if ((childPtr->dwStyle & WS_MINIMIZE) && (hwndChild != hwnd))
64                 {
65                     if ((childPtr->rectWindow.left < x + xspacing) &&
66                         (childPtr->rectWindow.right >= x) &&
67                         (childPtr->rectWindow.top <= y) &&
68                         (childPtr->rectWindow.bottom > y - yspacing))
69                         break;  /* There's a window in there */
70                 }
71                 
72                 hwndChild = childPtr->hwndNext;
73             }
74             if (!hwndChild)
75             {
76                   /* No window was found, so it's OK for us */
77                 wndPtr->ptIconPos.x = x + (xspacing - SYSMETRICS_CXICON) / 2;
78                 wndPtr->ptIconPos.y = y - (yspacing + SYSMETRICS_CYICON) / 2;
79                 return;
80             }
81         }
82         y -= yspacing;
83     }
84 }
85
86
87 /***********************************************************************
88  *           ArrangeIconicWindows   (USER.170)
89  */
90 UINT ArrangeIconicWindows( HWND parent )
91 {
92     RECT rectParent;
93     HWND hwndChild;
94     INT x, y, xspacing, yspacing;
95
96     GetClientRect( parent, &rectParent );
97     x = rectParent.left;
98     y = rectParent.bottom;
99     xspacing = yspacing = 70;  /* FIXME: This should come from WIN.INI */
100     hwndChild = GetWindow( parent, GW_CHILD );
101     while (hwndChild)
102     {
103         if (IsIconic( hwndChild ))
104         {
105             SetWindowPos( hwndChild, 0, x + (xspacing - SYSMETRICS_CXICON) / 2,
106                           y - (yspacing + SYSMETRICS_CYICON) / 2, 0, 0,
107                           SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
108             if (x <= rectParent.right - xspacing) x += xspacing;
109             else
110             {
111                 x = rectParent.left;
112                 y -= yspacing;
113             }
114         }
115         hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
116     }
117     return yspacing;
118 }
119
120
121 /***********************************************************************
122  *           GetWindowRect   (USER.32)
123  */
124 void GetWindowRect( HWND hwnd, LPRECT rect ) 
125 {
126     WND * wndPtr = WIN_FindWndPtr( hwnd ); 
127     if (!wndPtr) return;
128     
129     *rect = wndPtr->rectWindow;
130     if (wndPtr->dwStyle & WS_CHILD)
131         MapWindowPoints( wndPtr->hwndParent, 0, (POINT *)rect, 2 );
132 }
133
134
135 /***********************************************************************
136  *           GetClientRect   (USER.33)
137  */
138 void GetClientRect( HWND hwnd, LPRECT rect ) 
139 {
140     WND * wndPtr = WIN_FindWndPtr( hwnd );
141
142     rect->left = rect->top = rect->right = rect->bottom = 0;
143     if (wndPtr) 
144     {
145         rect->right  = wndPtr->rectClient.right - wndPtr->rectClient.left;
146         rect->bottom = wndPtr->rectClient.bottom - wndPtr->rectClient.top;
147     }
148 }
149
150
151 /*******************************************************************
152  *         ClientToScreen   (USER.28)
153  */
154 BOOL ClientToScreen( HWND hwnd, LPPOINT lppnt )
155 {
156     MapWindowPoints( hwnd, 0, lppnt, 1 );
157     return TRUE;
158 }
159
160
161 /*******************************************************************
162  *         ScreenToClient   (USER.29)
163  */
164 void ScreenToClient( HWND hwnd, LPPOINT lppnt )
165 {
166     MapWindowPoints( 0, hwnd, lppnt, 1 );
167 }
168
169
170 /***********************************************************************
171  *           WINPOS_WindowFromPoint
172  *
173  * Find the window and hittest for a given point.
174  */
175 INT WINPOS_WindowFromPoint( POINT pt, HWND *phwnd )
176 {
177     WND *wndPtr;
178     HWND hwnd;
179     INT hittest = HTERROR;
180     INT x, y;
181
182     *phwnd = 0;
183     x = pt.x;
184     y = pt.y;
185     hwnd = GetWindow( GetDesktopWindow(), GW_CHILD );
186     for (;;)
187     {
188         while (hwnd)
189         {
190             /* If point is in window, and window is visible, and it  */
191             /* is enabled (or it's a top-level window), then explore */
192             /* its children. Otherwise, go to the next window.       */
193
194             wndPtr = WIN_FindWndPtr( hwnd );
195             if ((wndPtr->dwStyle & WS_VISIBLE) &&
196                 (!(wndPtr->dwStyle & WS_DISABLED) ||
197                  ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)) &&
198                 (x >= wndPtr->rectWindow.left) &&
199                 (x < wndPtr->rectWindow.right) &&
200                 (y >= wndPtr->rectWindow.top) &&
201                 (y < wndPtr->rectWindow.bottom))
202             {
203                 *phwnd = hwnd;  /* Got a suitable window */
204
205                 /* If window is minimized or disabled, return at once */
206                 if (wndPtr->dwStyle & WS_MINIMIZE) return HTCAPTION;
207                 if (wndPtr->dwStyle & WS_DISABLED) return HTERROR;
208
209                 /* If point is not in client area, ignore the children */
210                 if ((x < wndPtr->rectClient.left) ||
211                     (x >= wndPtr->rectClient.right) ||
212                     (y < wndPtr->rectClient.top) ||
213                     (y >= wndPtr->rectClient.bottom)) break;
214
215                 x -= wndPtr->rectClient.left;
216                 y -= wndPtr->rectClient.top;
217                 hwnd = wndPtr->hwndChild;
218             }
219             else hwnd = wndPtr->hwndNext;
220         }
221
222         /* If nothing found, return the desktop window */
223         if (!*phwnd)
224         {
225             *phwnd = GetDesktopWindow();
226             return HTCLIENT;
227         }
228         wndPtr = WIN_FindWndPtr( *phwnd );
229
230         /* Send the WM_NCHITTEST message (only if to the same task) */
231         if (wndPtr->hmemTaskQ != GetTaskQueue(0)) return HTCLIENT;
232         hittest = (INT)SendMessage( *phwnd, WM_NCHITTEST, 0,
233                                     MAKELONG( pt.x, pt.y ) );
234         if (hittest != HTTRANSPARENT) return hittest;  /* Found the window */
235
236         /* If no children found in last search, make point relative to parent*/
237         if (!hwnd)
238         {
239             x += wndPtr->rectClient.left;
240             y += wndPtr->rectClient.top;
241         }
242
243         /* Restart the search from the next sibling */
244         hwnd = wndPtr->hwndNext;
245         *phwnd = wndPtr->hwndParent;
246     }
247 }
248
249
250 /*******************************************************************
251  *         WindowFromPoint   (USER.30)
252  */
253 HWND WindowFromPoint( POINT pt )
254 {
255     HWND hwnd;
256     WINPOS_WindowFromPoint( pt, &hwnd );
257     return hwnd;
258 }
259
260
261 /*******************************************************************
262  *         ChildWindowFromPoint   (USER.191)
263  */
264 HWND ChildWindowFromPoint( HWND hwndParent, POINT pt )
265 {
266     RECT rect;
267     HWND hwnd;
268     
269     GetWindowRect( hwndParent, &rect );
270     if (!PtInRect( &rect, pt )) return 0;
271     hwnd = GetTopWindow( hwndParent );
272     while (hwnd)
273     {
274         GetWindowRect( hwnd, &rect );
275         if (PtInRect( &rect, pt )) return hwnd;
276         hwnd = GetWindow( hwnd, GW_HWNDNEXT );
277     }
278     return hwndParent;
279 }
280
281
282 /*******************************************************************
283  *         MapWindowPoints   (USER.258)
284  */
285 void MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, WORD count )
286 {
287     WND * wndPtr;
288     POINT * curpt;
289     POINT origin = { 0, 0 };
290     WORD i;
291
292     if( hwndFrom == hwndTo ) return;
293
294       /* Translate source window origin to screen coords */
295     while(hwndFrom)
296     {
297         if (!(wndPtr = WIN_FindWndPtr( hwndFrom )))
298         {
299             fprintf( stderr, "MapWindowPoints: bad hwndFrom = "NPFMT"\n",
300                      hwndFrom); 
301             return;
302         }
303         origin.x += wndPtr->rectClient.left;
304         origin.y += wndPtr->rectClient.top;
305         hwndFrom = (wndPtr->dwStyle & WS_CHILD) ? wndPtr->hwndParent : 0;
306     }
307
308       /* Translate origin to destination window coords */
309     while(hwndTo)
310     {
311         if (!(wndPtr = WIN_FindWndPtr( hwndTo )))
312         {
313             fprintf(stderr,"MapWindowPoints: bad hwndTo = "NPFMT"\n", hwndTo );
314             return;
315         }
316         origin.x -= wndPtr->rectClient.left;
317         origin.y -= wndPtr->rectClient.top;
318         hwndTo = (wndPtr->dwStyle & WS_CHILD) ? wndPtr->hwndParent : 0;
319     }    
320
321       /* Translate points */
322     for (i = 0, curpt = lppt; i < count; i++, curpt++)
323     {
324         curpt->x += origin.x;
325         curpt->y += origin.y;
326     }
327 }
328
329
330 /***********************************************************************
331  *           IsIconic   (USER.31)
332  */
333 BOOL IsIconic(HWND hWnd)
334 {
335     WND * wndPtr = WIN_FindWndPtr(hWnd);
336     if (wndPtr == NULL) return FALSE;
337     return (wndPtr->dwStyle & WS_MINIMIZE) != 0;
338 }
339  
340  
341 /***********************************************************************
342  *           IsZoomed   (USER.272)
343  */
344 BOOL IsZoomed(HWND hWnd)
345 {
346     WND * wndPtr = WIN_FindWndPtr(hWnd);
347     if (wndPtr == NULL) return FALSE;
348     return (wndPtr->dwStyle & WS_MAXIMIZE) != 0;
349 }
350
351
352 /*******************************************************************
353  *         GetActiveWindow    (USER.60)
354  */
355 HWND GetActiveWindow()
356 {
357     return hwndActive;
358 }
359
360
361 /*******************************************************************
362  *         SetActiveWindow    (USER.59)
363  */
364 HWND SetActiveWindow( HWND hwnd )
365 {
366     HWND prev = hwndActive;
367     WND *wndPtr = WIN_FindWndPtr( hwnd );
368
369     if (!wndPtr || (wndPtr->dwStyle & WS_DISABLED) ||
370         !(wndPtr->dwStyle & WS_VISIBLE)) return 0;
371
372     WINPOS_SetActiveWindow( hwnd, 0, 0 );
373     return prev;
374 }
375
376
377 /***********************************************************************
378  *           BringWindowToTop   (USER.45)
379  */
380 BOOL BringWindowToTop( HWND hwnd )
381 {
382     return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
383 }
384
385
386 /***********************************************************************
387  *           MoveWindow   (USER.56)
388  */
389 BOOL MoveWindow( HWND hwnd, short x, short y, short cx, short cy, BOOL repaint)
390 {    
391     int flags = SWP_NOZORDER | SWP_NOACTIVATE;
392     if (!repaint) flags |= SWP_NOREDRAW;
393     dprintf_win(stddeb, "MoveWindow: "NPFMT" %d,%d %dx%d %d\n", 
394             hwnd, x, y, cx, cy, repaint );
395     return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
396 }
397
398
399 /***********************************************************************
400  *           ShowWindow   (USER.42)
401  */
402 BOOL ShowWindow( HWND hwnd, int cmd ) 
403 {    
404     WND * wndPtr = WIN_FindWndPtr( hwnd );
405     BOOL wasVisible;
406     POINT maxSize;
407     int swpflags = 0;
408     short x = 0, y = 0, cx = 0, cy = 0;
409
410     if (!wndPtr) return FALSE;
411
412     dprintf_win(stddeb,"ShowWindow: hwnd="NPFMT", cmd=%d\n", hwnd, cmd);
413
414     wasVisible = (wndPtr->dwStyle & WS_VISIBLE) != 0;
415
416     switch(cmd)
417     {
418         case SW_HIDE:
419             swpflags |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE | 
420                         SWP_NOACTIVATE | SWP_NOZORDER;
421             break;
422
423         case SW_SHOWMINNOACTIVE:
424             swpflags |= SWP_NOACTIVATE | SWP_NOZORDER;
425             /* fall through */
426         case SW_SHOWMINIMIZED:
427             swpflags |= SWP_SHOWWINDOW;
428             /* fall through */
429         case SW_MINIMIZE:
430             swpflags |= SWP_FRAMECHANGED;
431             if (!(wndPtr->dwStyle & WS_MINIMIZE))
432             {
433                 if (wndPtr->dwStyle & WS_MAXIMIZE)
434                 {
435                     wndPtr->flags |= WIN_RESTORE_MAX;
436                     wndPtr->dwStyle &= ~WS_MAXIMIZE;
437                 }
438                 else
439                 {
440                     wndPtr->flags &= ~WIN_RESTORE_MAX;
441                     wndPtr->rectNormal = wndPtr->rectWindow;
442                 }
443                 wndPtr->dwStyle |= WS_MINIMIZE;
444                 WINPOS_FindIconPos( hwnd );
445                 x  = wndPtr->ptIconPos.x;
446                 y  = wndPtr->ptIconPos.y;
447                 cx = SYSMETRICS_CXICON;
448                 cy = SYSMETRICS_CYICON;
449             }
450             else swpflags |= SWP_NOSIZE | SWP_NOMOVE;
451             break;
452
453         case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE: */
454             swpflags |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
455             if (!(wndPtr->dwStyle & WS_MAXIMIZE))
456             {
457                   /* Store the current position and find the maximized size */
458                 if (!(wndPtr->dwStyle & WS_MINIMIZE))
459                     wndPtr->rectNormal = wndPtr->rectWindow; 
460
461                 NC_GetMinMaxInfo( hwnd, &maxSize,
462                                   &wndPtr->ptMaxPos, NULL, NULL );
463                 x  = wndPtr->ptMaxPos.x;
464                 y  = wndPtr->ptMaxPos.y;
465
466                 if( wndPtr->dwStyle & WS_MINIMIZE )
467                     if( !SendMessage( hwnd, WM_QUERYOPEN, 0, 0L ) )
468                         {
469                          swpflags |= SWP_NOSIZE;
470                          break;
471                         }
472
473                 cx = maxSize.x;
474                 cy = maxSize.y;
475                 wndPtr->dwStyle &= ~WS_MINIMIZE;
476                 wndPtr->dwStyle |= WS_MAXIMIZE;
477             }
478             else swpflags |= SWP_NOSIZE | SWP_NOMOVE;
479             break;
480
481         case SW_SHOWNA:
482             swpflags |= SWP_NOACTIVATE | SWP_NOZORDER;
483             /* fall through */
484         case SW_SHOW:
485             swpflags |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
486             break;
487
488         case SW_SHOWNOACTIVATE:
489             swpflags |= SWP_NOZORDER;
490             if (GetActiveWindow()) swpflags |= SWP_NOACTIVATE;
491             /* fall through */
492         case SW_SHOWNORMAL:  /* same as SW_NORMAL: */
493         case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
494         case SW_RESTORE:
495             swpflags |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
496
497             if (wndPtr->dwStyle & WS_MINIMIZE)
498             {
499                 if( !SendMessage( hwnd, WM_QUERYOPEN, 0, 0L) )
500                   {
501                     swpflags |= SWP_NOSIZE;
502                     break;
503                   }
504                 wndPtr->ptIconPos.x = wndPtr->rectWindow.left;
505                 wndPtr->ptIconPos.y = wndPtr->rectWindow.top;
506                 wndPtr->dwStyle &= ~WS_MINIMIZE;
507                 if (wndPtr->flags & WIN_RESTORE_MAX)
508                 {
509                     /* Restore to maximized position */
510                     NC_GetMinMaxInfo( hwnd, &maxSize, &wndPtr->ptMaxPos,
511                                       NULL, NULL );
512                     x  = wndPtr->ptMaxPos.x;
513                     y  = wndPtr->ptMaxPos.y;
514                     cx = maxSize.x;
515                     cy = maxSize.y;
516                    wndPtr->dwStyle |= WS_MAXIMIZE;
517                 }
518                 else  /* Restore to normal position */
519                 {
520                     x  = wndPtr->rectNormal.left;
521                     y  = wndPtr->rectNormal.top;
522                     cx = wndPtr->rectNormal.right - wndPtr->rectNormal.left;
523                     cy = wndPtr->rectNormal.bottom - wndPtr->rectNormal.top;
524                 }
525             }
526             else if (wndPtr->dwStyle & WS_MAXIMIZE)
527             {
528                 wndPtr->ptMaxPos.x = wndPtr->rectWindow.left;
529                 wndPtr->ptMaxPos.y = wndPtr->rectWindow.top;
530                 wndPtr->dwStyle &= ~WS_MAXIMIZE;
531                 x  = wndPtr->rectNormal.left;
532                 y  = wndPtr->rectNormal.top;
533                 cx = wndPtr->rectNormal.right - wndPtr->rectNormal.left;
534                 cy = wndPtr->rectNormal.bottom - wndPtr->rectNormal.top;
535             }
536             else swpflags |= SWP_NOSIZE | SWP_NOMOVE;
537             break;
538     }
539
540     SendMessage( hwnd, WM_SHOWWINDOW, (cmd != SW_HIDE), 0 );
541     SetWindowPos( hwnd, HWND_TOP, x, y, cx, cy, swpflags );
542
543       /* Send WM_SIZE and WM_MOVE messages if not already done */
544     if (!(wndPtr->flags & WIN_GOT_SIZEMSG))
545     {
546         int wParam = SIZE_RESTORED;
547         if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
548         else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
549         wndPtr->flags |= WIN_GOT_SIZEMSG;
550         SendMessage( hwnd, WM_SIZE, wParam,
551                      MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
552                             wndPtr->rectClient.bottom-wndPtr->rectClient.top));
553         SendMessage( hwnd, WM_MOVE, 0,
554                    MAKELONG(wndPtr->rectClient.left, wndPtr->rectClient.top) );
555     }
556
557     return wasVisible;
558 }
559
560
561 /***********************************************************************
562  *           GetInternalWindowPos   (USER.460)
563  */
564 WORD GetInternalWindowPos( HWND hwnd, LPRECT rectWnd, LPPOINT ptIcon )
565 {
566     WINDOWPLACEMENT wndpl;
567     if (!GetWindowPlacement( hwnd, &wndpl )) return 0;
568     if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
569     if (ptIcon)  *ptIcon = wndpl.ptMinPosition;
570     return wndpl.showCmd;
571 }
572
573
574 /***********************************************************************
575  *           SetInternalWindowPos   (USER.461)
576  */
577 void SetInternalWindowPos( HWND hwnd, WORD showCmd, LPRECT rect, LPPOINT pt )
578 {
579     WINDOWPLACEMENT wndpl;
580     WND *wndPtr = WIN_FindWndPtr( hwnd );
581
582     wndpl.length  = sizeof(wndpl);
583     wndpl.flags   = (pt != NULL) ? WPF_SETMINPOSITION : 0;
584     wndpl.showCmd = showCmd;
585     if (pt) wndpl.ptMinPosition = *pt;
586     wndpl.rcNormalPosition = (rect != NULL) ? *rect : wndPtr->rectNormal;
587     wndpl.ptMaxPosition = wndPtr->ptMaxPos;
588     SetWindowPlacement( hwnd, &wndpl );
589 }
590
591
592 /***********************************************************************
593  *           GetWindowPlacement   (USER.370)
594  */
595 BOOL GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
596 {
597     WND *wndPtr = WIN_FindWndPtr( hwnd );
598     if (!wndPtr) return FALSE;
599
600     wndpl->length  = sizeof(*wndpl);
601     wndpl->flags   = 0;
602     wndpl->showCmd = IsZoomed(hwnd) ? SW_SHOWMAXIMIZED : 
603                      (IsIconic(hwnd) ? SW_SHOWMINIMIZED : SW_SHOWNORMAL);
604     wndpl->ptMinPosition = wndPtr->ptIconPos;
605     wndpl->ptMaxPosition = wndPtr->ptMaxPos;
606     wndpl->rcNormalPosition = wndPtr->rectNormal;
607     return TRUE;
608 }
609
610
611 /***********************************************************************
612  *           SetWindowPlacement   (USER.371)
613  */
614 BOOL SetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
615 {
616     WND *wndPtr = WIN_FindWndPtr( hwnd );
617     if (!wndPtr) return FALSE;
618
619     if (wndpl->flags & WPF_SETMINPOSITION)
620         wndPtr->ptIconPos = wndpl->ptMinPosition;
621     if ((wndpl->flags & WPF_RESTORETOMAXIMIZED) &&
622         (wndpl->showCmd == SW_SHOWMINIMIZED)) wndPtr->flags |= WIN_RESTORE_MAX;
623     wndPtr->ptMaxPos   = wndpl->ptMaxPosition;
624     wndPtr->rectNormal = wndpl->rcNormalPosition;
625     ShowWindow( hwnd, wndpl->showCmd );
626     return TRUE;
627 }
628
629 /*******************************************************************
630  *         ACTIVATEAPP_callback
631  */
632 BOOL ACTIVATEAPP_callback(HWND hWnd, LPARAM lParam)
633 {
634     ACTIVATESTRUCT  *lpActStruct = (ACTIVATESTRUCT*)lParam;
635     WND             *wndPtr = WIN_FindWndPtr( hWnd );
636
637     if( !wndPtr || hWnd == GetDesktopWindow()) return 1;
638  
639     if( MSG_GetQueueTask(wndPtr->hmemTaskQ) != lpActStruct->hTaskSendTo ) 
640         return 1;
641
642     SendMessage( hWnd, WM_ACTIVATEAPP, lpActStruct->wFlag,
643                 (LPARAM)((lpActStruct->hWindowTask)?lpActStruct->hWindowTask:0));
644     return 1;
645 }
646
647
648 /*******************************************************************
649  *         WINPOS_SetActiveWindow
650  *
651  * back-end to SetActiveWindow
652  */
653 BOOL WINPOS_SetActiveWindow( HWND hWnd, BOOL fMouse, BOOL fChangeFocus )
654 {
655     WND                   *wndPtr          = WIN_FindWndPtr(hWnd);
656     WND                   *wndTemp         = WIN_FindWndPtr(hwndActive);
657     CBTACTIVATESTRUCT      cbtStruct       = { fMouse , hwndActive };
658     FARPROC                enumCallback    = (FARPROC)GetWndProcEntry16("ActivateAppProc");
659     ACTIVATESTRUCT         actStruct;
660     WORD                   wIconized=0,wRet= 0;
661     HANDLE                 hActiveQ = 0;
662
663     /* paranoid checks */
664     if( !hWnd || hWnd == GetDesktopWindow() || hWnd == hwndActive )
665         return 0;
666
667     if( GetTaskQueue(0) != wndPtr->hmemTaskQ )
668         return 0;
669
670     if( wndTemp )
671         wIconized = HIWORD(wndTemp->dwStyle & WS_MINIMIZE);
672     else
673         dprintf_win(stddeb,"WINPOS_ActivateWindow: no current active window.\n");
674
675     /* call CBT hook chain */
676     wRet = HOOK_CallHooks(WH_CBT, HCBT_ACTIVATE, (WPARAM)hWnd,
677                           (LPARAM)MAKE_SEGPTR(&cbtStruct));
678
679     if( wRet ) return wRet;
680
681     /* set prev active wnd to current active wnd and send notification */
682     if( (hwndPrevActive = hwndActive) )
683     {
684 /* FIXME: need a Win32 translation for WINELIB32 */
685         if( !SendMessage(hwndPrevActive, WM_NCACTIVATE, 0, MAKELONG(hWnd,wIconized)) )
686         {
687             if (GetSysModalWindow() != hWnd) return 0;
688             /* disregard refusal if hWnd is sysmodal */
689         }
690
691 #ifdef WINELIB32
692         SendMessage( hwndActive, WM_ACTIVATE,
693                      MAKEWPARAM( WA_INACTIVE, wIconized ),
694                      (LPARAM)hWnd );
695 #else
696         SendMessage(hwndPrevActive, WM_ACTIVATE, WA_INACTIVE, 
697                     MAKELONG(hWnd,wIconized));
698 #endif
699
700         /* check if something happened during message processing */
701         if( hwndPrevActive != hwndActive ) return 0;
702     }
703
704     /* set active wnd */
705     hwndActive = hWnd;
706
707     /* send palette messages */
708     if( SendMessage( hWnd, WM_QUERYNEWPALETTE, 0, 0L) )
709         SendMessage((HWND)-1, WM_PALETTEISCHANGING, (WPARAM)hWnd, 0L );
710
711     /* if prev wnd is minimized redraw icon title 
712   if( hwndPrevActive )
713     {
714         wndTemp = WIN_FindWndPtr( WIN_GetTopParent( hwndPrevActive ) );
715         if(wndTemp)
716           if(wndTemp->dwStyle & WS_MINIMIZE)
717             RedrawIconTitle(hwndPrevActive); 
718       } 
719   */
720     if (!(wndPtr->dwStyle & WS_CHILD))
721     {
722         /* check Z-order and bring hWnd to the top */
723         wndTemp = WIN_FindWndPtr( GetDesktopWindow() );
724
725         for( ; wndTemp; wndTemp = WIN_FindWndPtr( wndTemp->hwndNext ))
726             if( wndTemp->dwStyle & WS_VISIBLE )
727                 break;
728
729         if( wndTemp != wndPtr )
730             SetWindowPos(hWnd, HWND_TOP, 0,0,0,0, 
731                          SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
732     }
733
734     if( !IsWindow(hWnd) ) return 0;
735
736     if (hwndPrevActive)
737     {
738         wndTemp = WIN_FindWndPtr( hwndPrevActive );
739         if (wndTemp) hActiveQ = wndTemp->hmemTaskQ;
740     }
741
742     /* send WM_ACTIVATEAPP if necessary */
743     if (hActiveQ != wndPtr->hmemTaskQ)
744     {
745         HTASK hT = MSG_GetQueueTask( hActiveQ );
746
747         actStruct.wFlag = 0;                  /* deactivate */
748         actStruct.hWindowTask = MSG_GetQueueTask(wndPtr->hmemTaskQ);
749         actStruct.hTaskSendTo = hT;
750
751         /* send WM_ACTIVATEAPP to top-level windows
752          * that belong to the actStruct.hTaskSendTo task
753          */
754         EnumWindows( enumCallback , (LPARAM)&actStruct );
755
756         actStruct.wFlag = 1;                  /* activate */
757         actStruct.hWindowTask = hT;
758         actStruct.hTaskSendTo = MSG_GetQueueTask( wndPtr->hmemTaskQ );
759
760         EnumWindows( enumCallback , (LPARAM)&actStruct );
761
762         if( !IsWindow(hWnd) ) return 0;
763     }
764
765     /* walk up to the first unowned window */
766     wndTemp = wndPtr;
767
768     while(wndTemp->hwndOwner)
769     {
770         wndTemp = WIN_FindWndPtr(wndTemp->hwndOwner);
771         if( !wndTemp)
772         {
773             /* there must be an unowned window in hierarchy */
774             dprintf_win(stddeb,"WINPOS_ActivateWindow: broken owner list\n");
775             wndTemp = wndPtr;
776             break;
777         }
778     }
779     /* and set last active owned popup */
780     wndTemp->hwndLastActive = hWnd;
781
782     wIconized = HIWORD(wndTemp->dwStyle & WS_MINIMIZE);
783 /* FIXME: Needs a Win32 translation for WINELIB32 */
784     SendMessage( hWnd, WM_NCACTIVATE, 1,
785                  MAKELONG(hwndPrevActive,wIconized));
786 #ifdef WINELIB32
787     SendMessage( hWnd, WM_ACTIVATE,
788                  MAKEWPARAM( (fMouse)?WA_CLICKACTIVE:WA_ACTIVE, wIconized),
789                  (LPARAM)hwndPrevActive );
790 #else
791     SendMessage( hWnd, WM_ACTIVATE, (fMouse)? WA_CLICKACTIVE : WA_ACTIVE,
792                  MAKELONG(hwndPrevActive,wIconized));
793 #endif
794
795     if( !IsWindow(hWnd) ) return 0;
796
797     /* change focus if possible */
798     if( fChangeFocus && GetFocus() )
799         if( WIN_GetTopParent(GetFocus()) != hwndActive )
800             FOCUS_SwitchFocus( GetFocus(),
801                                (wndPtr->dwStyle & WS_MINIMIZE)? 0: hwndActive);
802
803     /* if active wnd is minimized redraw icon title 
804   if( hwndActive )
805       {
806         wndPtr = WIN_FindWndPtr(hwndActive);
807         if(wndPtr->dwStyle & WS_MINIMIZE)
808            RedrawIconTitle(hwndActive);
809     }
810   */
811     return (hWnd == hwndActive);
812 }
813
814
815 /*******************************************************************
816  *         WINPOS_ChangeActiveWindow
817  *
818  */
819 BOOL WINPOS_ChangeActiveWindow( HWND hWnd, BOOL mouseMsg )
820 {
821     WND *wndPtr = WIN_FindWndPtr(hWnd);
822
823     if( !wndPtr ) return FALSE;
824
825     /* minors are not allowed */
826     if( (wndPtr->dwStyle & WS_CHILD) && !( wndPtr->dwStyle & WS_POPUP))
827         return SendMessage(hWnd, WM_CHILDACTIVATE, 0, 0L);
828
829     if( hWnd == hwndActive ) return FALSE;
830
831     if( !WINPOS_SetActiveWindow(hWnd ,mouseMsg ,TRUE) )
832         return FALSE;
833
834     /* switch desktop queue to current active here */
835     if( wndPtr->hwndParent == GetDesktopWindow())
836     { }
837
838     return TRUE;
839 }
840
841
842 /***********************************************************************
843  *           WINPOS_SendNCCalcSize
844  *
845  * Send a WM_NCCALCSIZE message to a window.
846  * All parameters are read-only except newClientRect.
847  * oldWindowRect, oldClientRect and winpos must be non-NULL only
848  * when calcValidRect is TRUE.
849  */
850 LONG WINPOS_SendNCCalcSize( HWND hwnd, BOOL calcValidRect, RECT *newWindowRect,
851                             RECT *oldWindowRect, RECT *oldClientRect,
852                             WINDOWPOS *winpos, RECT *newClientRect )
853 {
854     NCCALCSIZE_PARAMS params;
855     LONG result;
856
857     params.rgrc[0] = *newWindowRect;
858     if (calcValidRect)
859     {
860         params.rgrc[1] = *oldWindowRect;
861         params.rgrc[2] = *oldClientRect;
862         params.lppos = winpos;
863     }
864     result = SendMessage( hwnd, WM_NCCALCSIZE, calcValidRect,
865                           (LPARAM)MAKE_SEGPTR( &params ) );
866     dprintf_win(stddeb, "WINPOS_SendNCCalcSize: %d %d %d %d\n",
867                 (int)params.rgrc[0].top,    (int)params.rgrc[0].left,
868                 (int)params.rgrc[0].bottom, (int)params.rgrc[0].right);
869     *newClientRect = params.rgrc[0];
870     return result;
871 }
872
873
874 /***********************************************************************
875  *           WINPOS_HandleWindowPosChanging
876  *
877  * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
878  */
879 LONG WINPOS_HandleWindowPosChanging( WINDOWPOS *winpos )
880 {
881     POINT maxSize;
882     WND *wndPtr = WIN_FindWndPtr( winpos->hwnd );
883     if (!wndPtr || (winpos->flags & SWP_NOSIZE)) return 0;
884     if ((wndPtr->dwStyle & WS_THICKFRAME) ||
885         ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) == 0))
886     {
887         NC_GetMinMaxInfo( winpos->hwnd, &maxSize, NULL, NULL, NULL );
888         winpos->cx = MIN( winpos->cx, maxSize.x );
889         winpos->cy = MIN( winpos->cy, maxSize.y );
890     }
891     return 0;
892 }
893
894
895 /***********************************************************************
896  *           WINPOS_MoveWindowZOrder
897  *
898  * Move a window in Z order, invalidating everything that needs it.
899  * Only necessary for windows without associated X window.
900  */
901 static void WINPOS_MoveWindowZOrder( HWND hwnd, HWND hwndAfter )
902 {
903     BOOL movingUp;
904     HWND hwndCur;
905     WND *wndPtr = WIN_FindWndPtr( hwnd );
906
907     /* We have two possible cases:
908      * - The window is moving up: we have to invalidate all areas
909      *   of the window that were covered by other windows
910      * - The window is moving down: we have to invalidate areas
911      *   of other windows covered by this one.
912      */
913
914     if (hwndAfter == HWND_TOP)
915     {
916         movingUp = TRUE;
917     }
918     else if (hwndAfter == HWND_BOTTOM)
919     {
920         if (!wndPtr->hwndNext) return;  /* Already at the bottom */
921         movingUp = FALSE;
922     }
923     else
924     {
925         if (wndPtr->hwndNext == hwndAfter) return;  /* Already placed right */
926
927           /* Determine which window we encounter first in Z-order */
928         hwndCur = GetWindow( wndPtr->hwndParent, GW_CHILD );
929         while ((hwndCur != hwnd) && (hwndCur != hwndAfter))
930             hwndCur = GetWindow( hwndCur, GW_HWNDNEXT );
931         movingUp = (hwndCur == hwndAfter);
932     }
933
934     if (movingUp)
935     {
936         HWND hwndPrevAfter = wndPtr->hwndNext;
937         WIN_UnlinkWindow( hwnd );
938         WIN_LinkWindow( hwnd, hwndAfter );
939         hwndCur = wndPtr->hwndNext;
940         while (hwndCur != hwndPrevAfter)
941         {
942             WND *curPtr = WIN_FindWndPtr( hwndCur );
943             RECT rect = curPtr->rectWindow;
944             OffsetRect( &rect, -wndPtr->rectClient.left,
945                         -wndPtr->rectClient.top );
946             RedrawWindow( hwnd, &rect, 0, RDW_INVALIDATE | RDW_ALLCHILDREN |
947                           RDW_FRAME | RDW_ERASE );
948             hwndCur = curPtr->hwndNext;
949         }
950     }
951     else  /* Moving down */
952     {
953         hwndCur = wndPtr->hwndNext;
954         WIN_UnlinkWindow( hwnd );
955         WIN_LinkWindow( hwnd, hwndAfter );
956         while (hwndCur != hwnd)
957         {
958             WND *curPtr = WIN_FindWndPtr( hwndCur );
959             RECT rect = wndPtr->rectWindow;
960             OffsetRect( &rect, -curPtr->rectClient.left,
961                         -curPtr->rectClient.top );
962             RedrawWindow( hwndCur, &rect, 0, RDW_INVALIDATE | RDW_ALLCHILDREN |
963                           RDW_FRAME | RDW_ERASE );
964             hwndCur = curPtr->hwndNext;
965         }
966     }
967 }
968
969
970 /***********************************************************************
971  *           WINPOS_SetXWindowPos
972  *
973  * SetWindowPos() for an X window. Used by the real SetWindowPos().
974  */
975 static void WINPOS_SetXWindowPos( WINDOWPOS *winpos )
976 {
977     XWindowChanges winChanges;
978     int changeMask = 0;
979     WND *wndPtr = WIN_FindWndPtr( winpos->hwnd );
980
981     if (!(winpos->flags & SWP_NOSIZE))
982     {
983         winChanges.width     = winpos->cx;
984         winChanges.height    = winpos->cy;
985         changeMask |= CWWidth | CWHeight;
986     }
987     if (!(winpos->flags & SWP_NOMOVE))
988     {
989         winChanges.x = winpos->x;
990         winChanges.y = winpos->y;
991         changeMask |= CWX | CWY;
992     }
993     if (!(winpos->flags & SWP_NOZORDER))
994     {
995         if (winpos->hwndInsertAfter == HWND_TOP) winChanges.stack_mode = Above;
996         else winChanges.stack_mode = Below;
997         if ((winpos->hwndInsertAfter != HWND_TOP) &&
998             (winpos->hwndInsertAfter != HWND_BOTTOM))
999         {
1000             WND * insertPtr = WIN_FindWndPtr( winpos->hwndInsertAfter );
1001             winChanges.sibling = insertPtr->window;
1002             changeMask |= CWSibling;
1003         }
1004         changeMask |= CWStackMode;
1005     }
1006     if (changeMask)
1007         XConfigureWindow( display, wndPtr->window, changeMask, &winChanges );
1008 }
1009
1010
1011 /***********************************************************************
1012  *           SetWindowPos   (USER.232)
1013  */
1014 BOOL SetWindowPos( HWND hwnd, HWND hwndInsertAfter, INT x, INT y,
1015                    INT cx, INT cy, WORD flags )
1016 {
1017     WINDOWPOS winpos;
1018     WND *wndPtr;
1019     RECT newWindowRect, newClientRect;
1020     int result;
1021
1022       /* Check window handle */
1023
1024     if (hwnd == GetDesktopWindow()) return FALSE;
1025     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
1026
1027     /* Check for windows that may not be resized 
1028        FIXME: this should be done only for Windows 3.0 programs */
1029     if (flags ==(SWP_SHOWWINDOW) || flags ==(SWP_HIDEWINDOW ) )
1030        flags |= SWP_NOSIZE | SWP_NOMOVE;
1031
1032       /* Check dimensions */
1033
1034     if (cx <= 0) cx = 1;
1035     if (cy <= 0) cy = 1;
1036
1037       /* Check flags */
1038
1039     if (hwnd == hwndActive) flags |= SWP_NOACTIVATE;   /* Already active */
1040     if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == cx) &&
1041         (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == cy))
1042         flags |= SWP_NOSIZE;    /* Already the right size */
1043     if ((wndPtr->rectWindow.left == x) && (wndPtr->rectWindow.top == y))
1044         flags |= SWP_NOMOVE;    /* Already the right position */
1045
1046       /* Check hwndInsertAfter */
1047
1048     if (!(flags & (SWP_NOZORDER | SWP_NOACTIVATE)))
1049     {
1050           /* Ignore TOPMOST flags when activating a window */
1051           /* _and_ moving it in Z order. */
1052         if ((hwndInsertAfter == HWND_TOPMOST) ||
1053             (hwndInsertAfter == HWND_NOTOPMOST))
1054             hwndInsertAfter = HWND_TOP; 
1055     }
1056       /* TOPMOST not supported yet */
1057     if ((hwndInsertAfter == HWND_TOPMOST) ||
1058         (hwndInsertAfter == HWND_NOTOPMOST)) hwndInsertAfter = HWND_TOP;
1059       /* hwndInsertAfter must be a sibling of the window */
1060     if ((hwndInsertAfter != HWND_TOP) && (hwndInsertAfter != HWND_BOTTOM) &&
1061         (wndPtr->hwndParent != WIN_FindWndPtr(hwndInsertAfter)->hwndParent))
1062         return FALSE;
1063
1064       /* Fill the WINDOWPOS structure */
1065
1066     winpos.hwnd = hwnd;
1067     winpos.hwndInsertAfter = hwndInsertAfter;
1068     winpos.x = x;
1069     winpos.y = y;
1070     winpos.cx = cx;
1071     winpos.cy = cy;
1072     winpos.flags = flags;
1073     
1074       /* Send WM_WINDOWPOSCHANGING message */
1075
1076     if (!(flags & SWP_NOSENDCHANGING))
1077         SendMessage( hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)MAKE_SEGPTR(&winpos) );
1078
1079       /* Calculate new position and size */
1080
1081     newWindowRect = wndPtr->rectWindow;
1082     newClientRect = wndPtr->rectClient;
1083
1084     if (!(winpos.flags & SWP_NOSIZE))
1085     {
1086         newWindowRect.right  = newWindowRect.left + winpos.cx;
1087         newWindowRect.bottom = newWindowRect.top + winpos.cy;
1088     }
1089     if (!(winpos.flags & SWP_NOMOVE))
1090     {
1091         newWindowRect.left    = winpos.x;
1092         newWindowRect.top     = winpos.y;
1093         newWindowRect.right  += winpos.x - wndPtr->rectWindow.left;
1094         newWindowRect.bottom += winpos.y - wndPtr->rectWindow.top;
1095     }
1096
1097       /* Reposition window in Z order */
1098
1099     if (!(winpos.flags & SWP_NOZORDER))
1100     {
1101         if (wndPtr->window)
1102         {
1103             WIN_UnlinkWindow( winpos.hwnd );
1104             WIN_LinkWindow( winpos.hwnd, hwndInsertAfter );
1105         }
1106         else WINPOS_MoveWindowZOrder( winpos.hwnd, hwndInsertAfter );
1107     }
1108
1109       /* Send WM_NCCALCSIZE message to get new client area */
1110
1111     result = WINPOS_SendNCCalcSize( winpos.hwnd, TRUE, &newWindowRect,
1112                                     &wndPtr->rectWindow, &wndPtr->rectClient,
1113                                     &winpos, &newClientRect );
1114     /* FIXME: Should handle result here */
1115
1116     /* Perform the moving and resizing */
1117
1118     if (wndPtr->window)
1119     {
1120         WINPOS_SetXWindowPos( &winpos );
1121         wndPtr->rectWindow = newWindowRect;
1122         wndPtr->rectClient = newClientRect;
1123     }
1124     else
1125     {
1126         RECT oldWindowRect = wndPtr->rectWindow;
1127
1128         wndPtr->rectWindow = newWindowRect;
1129         wndPtr->rectClient = newClientRect;
1130
1131         if (!(flags & SWP_NOREDRAW) &&
1132             (!(flags & SWP_NOSIZE) || !(flags & SWP_NOMOVE) ||
1133              (!(flags & SWP_NOZORDER) && (hwndInsertAfter != HWND_TOP))))
1134         {
1135             HRGN hrgn1 = CreateRectRgnIndirect( &oldWindowRect );
1136             HRGN hrgn2 = CreateRectRgnIndirect( &wndPtr->rectWindow );
1137             HRGN hrgn3 = CreateRectRgn( 0, 0, 0, 0 );
1138             CombineRgn( hrgn3, hrgn1, hrgn2, RGN_DIFF );
1139             RedrawWindow( wndPtr->hwndParent, NULL, hrgn3,
1140                           RDW_INVALIDATE | RDW_ALLCHILDREN | RDW_ERASE );
1141
1142             /* DCE_GetVisRgn should be called for old coordinates
1143              * and for new, then OffsetRgn and CombineRgn -
1144              * voila, a nice update region to use here - AK.
1145              */ 
1146             if ((oldWindowRect.left != wndPtr->rectWindow.left) ||
1147                 (oldWindowRect.top != wndPtr->rectWindow.top))
1148             {
1149                 RedrawWindow( winpos.hwnd, NULL, 0, RDW_INVALIDATE |
1150                               RDW_FRAME | RDW_ALLCHILDREN | RDW_ERASE );
1151             }
1152             else
1153                 if( CombineRgn( hrgn3, hrgn2, hrgn1, RGN_DIFF) != NULLREGION )
1154                     RedrawWindow( winpos.hwnd, NULL, hrgn3, RDW_INVALIDATE |
1155                                   RDW_FRAME | RDW_ALLCHILDREN | RDW_ERASE );
1156
1157             DeleteObject( hrgn1 );
1158             DeleteObject( hrgn2 );
1159             DeleteObject( hrgn3 );
1160         }
1161     }
1162
1163     if (flags & SWP_SHOWWINDOW)
1164     {
1165         wndPtr->dwStyle |= WS_VISIBLE;
1166         if (wndPtr->window)
1167         {
1168             XMapWindow( display, wndPtr->window );
1169         }
1170         else
1171         {
1172             if (!(flags & SWP_NOREDRAW))
1173                 RedrawWindow( winpos.hwnd, NULL, 0,
1174                               RDW_INVALIDATE | RDW_ALLCHILDREN |
1175                               RDW_FRAME | RDW_ERASE );
1176         }
1177     }
1178     else if (flags & SWP_HIDEWINDOW)
1179     {
1180         wndPtr->dwStyle &= ~WS_VISIBLE;
1181         if (wndPtr->window)
1182         {
1183             XUnmapWindow( display, wndPtr->window );
1184         }
1185         else
1186         {
1187             if (!(flags & SWP_NOREDRAW))
1188                 RedrawWindow( wndPtr->hwndParent, &wndPtr->rectWindow, 0,
1189                               RDW_INVALIDATE | RDW_FRAME |
1190                               RDW_ALLCHILDREN | RDW_ERASE );
1191         }
1192
1193         if ((winpos.hwnd == GetFocus()) || IsChild(winpos.hwnd, GetFocus()))
1194             SetFocus( GetParent(winpos.hwnd) );  /* Revert focus to parent */
1195
1196         if (winpos.hwnd == hwndActive)
1197         {
1198               /* Activate previously active window if possible */
1199             HWND newActive = hwndPrevActive;
1200             if (!IsWindow(newActive) || (newActive == winpos.hwnd))
1201             {
1202                 newActive = GetTopWindow( GetDesktopWindow() );
1203                 if (newActive == winpos.hwnd) newActive = wndPtr->hwndNext;
1204             }       
1205             WINPOS_ChangeActiveWindow( newActive, FALSE );
1206         }
1207     }
1208
1209       /* Activate the window */
1210
1211     if (!(flags & SWP_NOACTIVATE))
1212             WINPOS_ChangeActiveWindow( winpos.hwnd, FALSE );
1213     
1214       /* Repaint the window */
1215
1216     if (wndPtr->window) MSG_Synchronize();  /* Wait for all expose events */
1217
1218     EVENT_DummyMotionNotify(); /* Simulate a mouse event to set the cursor */
1219
1220     if ((flags & SWP_FRAMECHANGED) && !(flags & SWP_NOREDRAW))
1221         RedrawWindow( winpos.hwnd, NULL, 0,
1222                       RDW_ALLCHILDREN | /*FIXME: this should not be necessary*/
1223                       RDW_INVALIDATE | RDW_FRAME | RDW_ERASE );
1224     if (!(flags & SWP_DEFERERASE))
1225         RedrawWindow( wndPtr->hwndParent, NULL, 0,
1226                       RDW_ALLCHILDREN | RDW_ERASENOW );
1227
1228       /* And last, send the WM_WINDOWPOSCHANGED message */
1229
1230     winpos.flags |= SWP_NOMOVE; /* prevent looping.. window is already moved ??? (FIXME)*/
1231
1232     if (!(winpos.flags & SWP_NOSENDCHANGING))
1233         SendMessage( winpos.hwnd, WM_WINDOWPOSCHANGED,
1234                      0, (LPARAM)MAKE_SEGPTR(&winpos) );
1235
1236     return TRUE;
1237 }
1238
1239                                         
1240 /***********************************************************************
1241  *           BeginDeferWindowPos   (USER.259)
1242  */
1243 HDWP BeginDeferWindowPos( INT count )
1244 {
1245     HDWP handle;
1246     DWP *pDWP;
1247
1248     if (count <= 0) return 0;
1249     handle = USER_HEAP_ALLOC( sizeof(DWP) + (count-1)*sizeof(WINDOWPOS) );
1250     if (!handle) return 0;
1251     pDWP = (DWP *) USER_HEAP_LIN_ADDR( handle );
1252     pDWP->actualCount    = 0;
1253     pDWP->suggestedCount = count;
1254     pDWP->valid          = TRUE;
1255     pDWP->wMagic         = DWP_MAGIC;
1256     pDWP->hwndParent     = 0;
1257     return handle;
1258 }
1259
1260
1261 /***********************************************************************
1262  *           DeferWindowPos   (USER.260)
1263  */
1264 HDWP DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter, INT x, INT y,
1265                      INT cx, INT cy, UINT flags )
1266 {
1267     DWP *pDWP;
1268     int i;
1269     HDWP newhdwp = hdwp;
1270     HWND parent;
1271
1272     pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
1273     if (!pDWP) return 0;
1274
1275       /* All the windows of a DeferWindowPos() must have the same parent */
1276
1277     parent = WIN_FindWndPtr( hwnd )->hwndParent;
1278     if (pDWP->actualCount == 0) pDWP->hwndParent = parent;
1279     else if (parent != pDWP->hwndParent)
1280     {
1281         USER_HEAP_FREE( hdwp );
1282         return 0;
1283     }
1284
1285     for (i = 0; i < pDWP->actualCount; i++)
1286     {
1287         if (pDWP->winPos[i].hwnd == hwnd)
1288         {
1289               /* Merge with the other changes */
1290             if (!(flags & SWP_NOZORDER))
1291             {
1292                 pDWP->winPos[i].hwndInsertAfter = hwndAfter;
1293             }
1294             if (!(flags & SWP_NOMOVE))
1295             {
1296                 pDWP->winPos[i].x = x;
1297                 pDWP->winPos[i].y = y;
1298             }                
1299             if (!(flags & SWP_NOSIZE))
1300             {
1301                 pDWP->winPos[i].cx = cx;
1302                 pDWP->winPos[i].cy = cy;
1303             }
1304             pDWP->winPos[i].flags &= flags & (SWP_NOSIZE | SWP_NOMOVE |
1305                                               SWP_NOZORDER | SWP_NOREDRAW |
1306                                               SWP_NOACTIVATE | SWP_NOCOPYBITS |
1307                                               SWP_NOOWNERZORDER);
1308             pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
1309                                               SWP_FRAMECHANGED);
1310             return hdwp;
1311         }
1312     }
1313     if (pDWP->actualCount >= pDWP->suggestedCount)
1314     {
1315         newhdwp = USER_HEAP_REALLOC( hdwp,
1316                       sizeof(DWP) + pDWP->suggestedCount*sizeof(WINDOWPOS) );
1317         if (!newhdwp) return 0;
1318         pDWP = (DWP *) USER_HEAP_LIN_ADDR( newhdwp );
1319         pDWP->suggestedCount++;
1320     }
1321     pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
1322     pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
1323     pDWP->winPos[pDWP->actualCount].x = x;
1324     pDWP->winPos[pDWP->actualCount].y = y;
1325     pDWP->winPos[pDWP->actualCount].cx = cx;
1326     pDWP->winPos[pDWP->actualCount].cy = cy;
1327     pDWP->winPos[pDWP->actualCount].flags = flags;
1328     pDWP->actualCount++;
1329     return newhdwp;
1330 }
1331
1332
1333 /***********************************************************************
1334  *           EndDeferWindowPos   (USER.261)
1335  */
1336 BOOL EndDeferWindowPos( HDWP hdwp )
1337 {
1338     DWP *pDWP;
1339     WINDOWPOS *winpos;
1340     BOOL res = TRUE;
1341     int i;
1342
1343     pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
1344     if (!pDWP) return FALSE;
1345     for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
1346     {
1347         if (!(res = SetWindowPos( winpos->hwnd, winpos->hwndInsertAfter,
1348                                   winpos->x, winpos->y, winpos->cx, winpos->cy,
1349                                   winpos->flags ))) break;
1350     }
1351     USER_HEAP_FREE( hdwp );
1352     return res;
1353 }
1354
1355
1356 /***********************************************************************
1357  *           TileChildWindows   (USER.199)
1358  */
1359 void TileChildWindows( HWND parent, WORD action )
1360 {
1361     printf("STUB TileChildWindows("NPFMT", %d)\n", parent, action);
1362 }
1363
1364 /***********************************************************************
1365  *           CascageChildWindows   (USER.198)
1366  */
1367 void CascadeChildWindows( HWND parent, WORD action )
1368 {
1369     printf("STUB CascadeChildWindows("NPFMT", %d)\n", parent, action);
1370 }