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