Added regedit unit test, a couple minor changes to regedit.
[wine] / windows / winpos.c
1 /*
2  * Window position related functions.
3  *
4  * Copyright 1993, 1994, 1995 Alexandre Julliard
5  *                       1995, 1996, 1999 Alex Korobka
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <string.h>
23 #include "winerror.h"
24 #include "windef.h"
25 #include "wingdi.h"
26 #include "winerror.h"
27 #include "wine/winuser16.h"
28 #include "wine/server.h"
29 #include "controls.h"
30 #include "user.h"
31 #include "win.h"
32 #include "hook.h"
33 #include "message.h"
34 #include "queue.h"
35 #include "winpos.h"
36 #include "dce.h"
37 #include "nonclient.h"
38 #include "wine/debug.h"
39 #include "input.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(win);
42
43 #define HAS_DLGFRAME(style,exStyle) \
44     (((exStyle) & WS_EX_DLGMODALFRAME) || \
45      (((style) & WS_DLGFRAME) && !((style) & WS_BORDER)))
46
47 #define HAS_THICKFRAME(style) \
48     (((style) & WS_THICKFRAME) && \
49      !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
50
51 #define EMPTYPOINT(pt)          ((*(LONG*)&(pt)) == -1)
52
53 #define PLACE_MIN               0x0001
54 #define PLACE_MAX               0x0002
55 #define PLACE_RECT              0x0004
56
57
58 #define DWP_MAGIC  ((INT)('W' | ('P' << 8) | ('O' << 16) | ('S' << 24)))
59
60 typedef struct
61 {
62     INT       actualCount;
63     INT       suggestedCount;
64     BOOL      valid;
65     INT       wMagic;
66     HWND      hwndParent;
67     WINDOWPOS winPos[1];
68 } DWP;
69
70 /* ----- internal variables ----- */
71
72 static HWND hwndPrevActive  = 0;  /* Previously active window */
73 static HWND hGlobalShellWindow=0; /*the shell*/
74 static HWND hGlobalTaskmanWindow=0;
75 static HWND hGlobalProgmanWindow=0;
76
77 static LPCSTR atomInternalPos;
78
79 extern HQUEUE16 hActiveQueue;
80
81 /***********************************************************************
82  *           WINPOS_CreateInternalPosAtom
83  */
84 BOOL WINPOS_CreateInternalPosAtom()
85 {
86     LPSTR str = "SysIP";
87     atomInternalPos = (LPCSTR)(DWORD)GlobalAddAtomA(str);
88     return (atomInternalPos) ? TRUE : FALSE;
89 }
90
91 /***********************************************************************
92  *           WINPOS_CheckInternalPos
93  *
94  * Called when a window is destroyed.
95  */
96 void WINPOS_CheckInternalPos( HWND hwnd )
97 {
98     LPINTERNALPOS lpPos;
99     MESSAGEQUEUE *pMsgQ = 0;
100     WND *wndPtr = WIN_GetPtr( hwnd );
101
102     if (!wndPtr || wndPtr == WND_OTHER_PROCESS) return;
103
104     lpPos = (LPINTERNALPOS) GetPropA( hwnd, atomInternalPos );
105
106     /* Retrieve the message queue associated with this window */
107     pMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( wndPtr->hmemTaskQ );
108     if ( !pMsgQ )
109     {
110         WARN("\tMessage queue not found. Exiting!\n" );
111         WIN_ReleasePtr( wndPtr );
112         return;
113     }
114
115     if( hwnd == hwndPrevActive ) hwndPrevActive = 0;
116
117     if( hwnd == PERQDATA_GetActiveWnd( pMsgQ->pQData ) )
118     {
119         PERQDATA_SetActiveWnd( pMsgQ->pQData, 0 );
120         WARN("\tattempt to activate destroyed window!\n");
121     }
122
123     if( lpPos )
124     {
125         if( IsWindow(lpPos->hwndIconTitle) )
126             DestroyWindow( lpPos->hwndIconTitle );
127         HeapFree( GetProcessHeap(), 0, lpPos );
128     }
129
130     QUEUE_Unlock( pMsgQ );
131     WIN_ReleasePtr( wndPtr );
132 }
133
134 /***********************************************************************
135  *              ArrangeIconicWindows (USER32.@)
136  */
137 UINT WINAPI ArrangeIconicWindows( HWND parent )
138 {
139     RECT rectParent;
140     HWND hwndChild;
141     INT x, y, xspacing, yspacing;
142
143     GetClientRect( parent, &rectParent );
144     x = rectParent.left;
145     y = rectParent.bottom;
146     xspacing = GetSystemMetrics(SM_CXICONSPACING);
147     yspacing = GetSystemMetrics(SM_CYICONSPACING);
148
149     hwndChild = GetWindow( parent, GW_CHILD );
150     while (hwndChild)
151     {
152         if( IsIconic( hwndChild ) )
153         {
154             WINPOS_ShowIconTitle( hwndChild, FALSE );
155
156             SetWindowPos( hwndChild, 0, x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
157                             y - yspacing - GetSystemMetrics(SM_CYICON)/2, 0, 0,
158                             SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
159             if( IsWindow(hwndChild) )
160                 WINPOS_ShowIconTitle(hwndChild , TRUE );
161
162             if (x <= rectParent.right - xspacing) x += xspacing;
163             else
164             {
165                 x = rectParent.left;
166                 y -= yspacing;
167             }
168         }
169         hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
170     }
171     return yspacing;
172 }
173
174
175 /***********************************************************************
176  *              SwitchToThisWindow (USER32.@)
177  */
178 void WINAPI SwitchToThisWindow( HWND hwnd, BOOL restore )
179 {
180     ShowWindow( hwnd, restore ? SW_RESTORE : SW_SHOWMINIMIZED );
181 }
182
183
184 /***********************************************************************
185  *              GetWindowRect (USER32.@)
186  */
187 BOOL WINAPI GetWindowRect( HWND hwnd, LPRECT rect )
188 {
189     BOOL ret = WIN_GetRectangles( hwnd, rect, NULL );
190     if (ret)
191     {
192         MapWindowPoints( GetAncestor( hwnd, GA_PARENT ), 0, (POINT *)rect, 2 );
193         TRACE( "hwnd %04x (%d,%d)-(%d,%d)\n",
194                hwnd, rect->left, rect->top, rect->right, rect->bottom);
195     }
196     return ret;
197 }
198
199
200 /***********************************************************************
201  *              GetWindowRgn (USER32.@)
202  */
203 int WINAPI GetWindowRgn ( HWND hwnd, HRGN hrgn )
204 {
205     int nRet = ERROR;
206     WND *wndPtr = WIN_GetPtr( hwnd );
207
208     if (wndPtr == WND_OTHER_PROCESS)
209     {
210         if (IsWindow( hwnd ))
211             FIXME( "not supported on other process window %x\n", hwnd );
212         wndPtr = NULL;
213     }
214     if (!wndPtr)
215     {
216         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
217         return ERROR;
218     }
219     if (wndPtr->hrgnWnd) nRet = CombineRgn( hrgn, wndPtr->hrgnWnd, 0, RGN_COPY );
220     WIN_ReleasePtr( wndPtr );
221     return nRet;
222 }
223
224
225 /***********************************************************************
226  *              SetWindowRgn (USER32.@)
227  */
228 int WINAPI SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL bRedraw )
229 {
230     RECT rect;
231     WND *wndPtr;
232
233     if (hrgn) /* verify that region really exists */
234     {
235         if (GetRgnBox( hrgn, &rect ) == ERROR) return FALSE;
236     }
237
238     if (USER_Driver.pSetWindowRgn)
239         return USER_Driver.pSetWindowRgn( hwnd, hrgn, bRedraw );
240
241     if ((wndPtr = WIN_GetPtr( hwnd )) == WND_OTHER_PROCESS)
242     {
243         if (IsWindow( hwnd ))
244             FIXME( "not supported on other process window %x\n", hwnd );
245         wndPtr = NULL;
246     }
247     if (!wndPtr)
248     {
249         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
250         return FALSE;
251     }
252
253     if (wndPtr->hrgnWnd == hrgn)
254     {
255         WIN_ReleasePtr( wndPtr );
256         return TRUE;
257     }
258
259     if (wndPtr->hrgnWnd)
260     {
261         /* delete previous region */
262         DeleteObject(wndPtr->hrgnWnd);
263         wndPtr->hrgnWnd = 0;
264     }
265     wndPtr->hrgnWnd = hrgn;
266     WIN_ReleasePtr( wndPtr );
267
268     /* Size the window to the rectangle of the new region (if it isn't NULL) */
269     if (hrgn) SetWindowPos( hwnd, 0, rect.left, rect.top,
270                             rect.right  - rect.left, rect.bottom - rect.top,
271                             SWP_NOSIZE | SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOACTIVATE |
272                             SWP_NOZORDER | (bRedraw ? 0 : SWP_NOREDRAW) );
273     return TRUE;
274 }
275
276
277 /***********************************************************************
278  *              GetClientRect (USER32.@)
279  */
280 BOOL WINAPI GetClientRect( HWND hwnd, LPRECT rect )
281 {
282     BOOL ret;
283
284     rect->right = rect->bottom = 0;
285     if ((ret = WIN_GetRectangles( hwnd, NULL, rect )))
286     {
287         rect->right -= rect->left;
288         rect->bottom -= rect->top;
289     }
290     rect->left = rect->top = 0;
291     return ret;
292 }
293
294
295 /*******************************************************************
296  *              ClientToScreen (USER32.@)
297  */
298 BOOL WINAPI ClientToScreen( HWND hwnd, LPPOINT lppnt )
299 {
300     MapWindowPoints( hwnd, 0, lppnt, 1 );
301     return TRUE;
302 }
303
304
305 /*******************************************************************
306  *              ScreenToClient (USER32.@)
307  */
308 BOOL WINAPI ScreenToClient( HWND hwnd, LPPOINT lppnt )
309 {
310     MapWindowPoints( 0, hwnd, lppnt, 1 );
311     return TRUE;
312 }
313
314
315 /***********************************************************************
316  *           find_child_from_point
317  *
318  * Find the child that contains pt. Helper for WindowFromPoint.
319  * pt is in parent client coordinates.
320  * lparam is the param to pass in the WM_NCHITTEST message.
321  */
322 static HWND find_child_from_point( HWND parent, POINT pt, INT *hittest, LPARAM lparam )
323 {
324     int i, res;
325     LONG style, exstyle;
326     RECT rectWindow, rectClient;
327     WND *wndPtr;
328     HWND *list = WIN_ListChildren( parent );
329     HWND retvalue = 0;
330
331     if (!list) return 0;
332     for (i = 0; list[i]; i++)
333     {
334         /* If point is in window, and window is visible, and it  */
335         /* is enabled (or it's a top-level window), then explore */
336         /* its children. Otherwise, go to the next window.       */
337
338         style = GetWindowLongW( list[i], GWL_STYLE );
339         if (!(style & WS_VISIBLE)) continue;  /* not visible -> skip */
340         if ((style & (WS_POPUP | WS_CHILD | WS_DISABLED)) == (WS_CHILD | WS_DISABLED))
341             continue;  /* disabled child -> skip */
342         exstyle = GetWindowLongW( list[i], GWL_EXSTYLE );
343         if ((exstyle & (WS_EX_LAYERED | WS_EX_TRANSPARENT)) == (WS_EX_LAYERED | WS_EX_TRANSPARENT))
344             continue;  /* transparent -> skip */
345
346         if (!WIN_GetRectangles( list[i], &rectWindow, &rectClient )) continue;
347         if (!PtInRect( &rectWindow, pt )) continue;  /* not in window -> skip */
348
349         /* FIXME: check window region for other processes too */
350         if ((wndPtr = WIN_GetPtr( list[i] )) && wndPtr != WND_OTHER_PROCESS)
351         {
352             if (wndPtr->hrgnWnd && !PtInRegion( wndPtr->hrgnWnd,
353                                                 pt.x - rectWindow.left, pt.y - rectWindow.top ))
354             {
355                 WIN_ReleasePtr( wndPtr );
356                 continue;  /* point outside window region -> skip */
357             }
358             WIN_ReleasePtr( wndPtr );
359         }
360
361         /* If window is minimized or disabled, return at once */
362         if (style & WS_MINIMIZE)
363         {
364             *hittest = HTCAPTION;
365             retvalue = list[i];
366             break;
367         }
368         if (style & WS_DISABLED)
369         {
370             *hittest = HTERROR;
371             retvalue = list[i];
372             break;
373         }
374
375         /* If point is in client area, explore children */
376         if (PtInRect( &rectClient, pt ))
377         {
378             POINT new_pt;
379
380             new_pt.x = pt.x - rectClient.left;
381             new_pt.y = pt.y - rectClient.top;
382             if ((retvalue = find_child_from_point( list[i], new_pt, hittest, lparam ))) break;
383         }
384
385         /* Now it's inside window, send WM_NCCHITTEST (if same thread) */
386         if (!WIN_IsCurrentThread( list[i] ))
387         {
388             *hittest = HTCLIENT;
389             retvalue = list[i];
390             break;
391         }
392         if ((res = SendMessageA( list[i], WM_NCHITTEST, 0, lparam )) != HTTRANSPARENT)
393         {
394             *hittest = res;  /* Found the window */
395             retvalue = list[i];
396             break;
397         }
398         /* continue search with next sibling */
399     }
400     HeapFree( GetProcessHeap(), 0, list );
401     return retvalue;
402 }
403
404
405 /***********************************************************************
406  *           WINPOS_WindowFromPoint
407  *
408  * Find the window and hittest for a given point.
409  */
410 HWND WINPOS_WindowFromPoint( HWND hwndScope, POINT pt, INT *hittest )
411 {
412     POINT xy = pt;
413     int res;
414     LONG style;
415
416     TRACE("scope %04x %ld,%ld\n", hwndScope, pt.x, pt.y);
417
418     if (!hwndScope) hwndScope = GetDesktopWindow();
419     style = GetWindowLongW( hwndScope, GWL_STYLE );
420
421     *hittest = HTERROR;
422     if (style & WS_DISABLED) return 0;
423
424     MapWindowPoints( GetDesktopWindow(), GetAncestor( hwndScope, GA_PARENT ), &xy, 1 );
425
426     if (!(style & WS_MINIMIZE))
427     {
428         RECT rectClient;
429         if (WIN_GetRectangles( hwndScope, NULL, &rectClient ) && PtInRect( &rectClient, xy ))
430         {
431             HWND ret;
432
433             xy.x -= rectClient.left;
434             xy.y -= rectClient.top;
435             if ((ret = find_child_from_point( hwndScope, xy, hittest, MAKELONG( pt.x, pt.y ) )))
436             {
437                 TRACE( "found child %x\n", ret );
438                 return ret;
439             }
440         }
441     }
442
443     /* If nothing found, try the scope window */
444     if (!WIN_IsCurrentThread( hwndScope ))
445     {
446         *hittest = HTCLIENT;
447         TRACE( "returning %x\n", hwndScope );
448         return hwndScope;
449     }
450     res = SendMessageA( hwndScope, WM_NCHITTEST, 0, MAKELONG( pt.x, pt.y ) );
451     if (res != HTTRANSPARENT)
452     {
453         *hittest = res;  /* Found the window */
454         TRACE( "returning %x\n", hwndScope );
455         return hwndScope;
456     }
457     *hittest = HTNOWHERE;
458     TRACE( "nothing found\n" );
459     return 0;
460 }
461
462
463 /*******************************************************************
464  *              WindowFromPoint (USER32.@)
465  */
466 HWND WINAPI WindowFromPoint( POINT pt )
467 {
468     INT hittest;
469     return WINPOS_WindowFromPoint( 0, pt, &hittest );
470 }
471
472
473 /*******************************************************************
474  *              ChildWindowFromPoint (USER32.@)
475  */
476 HWND WINAPI ChildWindowFromPoint( HWND hwndParent, POINT pt )
477 {
478     return ChildWindowFromPointEx( hwndParent, pt, CWP_ALL );
479 }
480
481 /*******************************************************************
482  *              ChildWindowFromPointEx (USER32.@)
483  */
484 HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
485 {
486     /* pt is in the client coordinates */
487     HWND *list;
488     int i;
489     RECT rect;
490     HWND retvalue;
491
492     GetClientRect( hwndParent, &rect );
493     if (!PtInRect( &rect, pt )) return 0;
494     if (!(list = WIN_ListChildren( hwndParent ))) return 0;
495
496     for (i = 0; list[i]; i++)
497     {
498         if (!WIN_GetRectangles( list[i], &rect, NULL )) continue;
499         if (!PtInRect( &rect, pt )) continue;
500         if (uFlags & (CWP_SKIPINVISIBLE|CWP_SKIPDISABLED))
501         {
502             LONG style = GetWindowLongW( list[i], GWL_STYLE );
503             if ((uFlags & CWP_SKIPINVISIBLE) && !(style & WS_VISIBLE)) continue;
504             if ((uFlags & CWP_SKIPDISABLED) && (style & WS_DISABLED)) continue;
505         }
506         if (uFlags & CWP_SKIPTRANSPARENT)
507         {
508             if (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TRANSPARENT) continue;
509         }
510         break;
511     }
512     retvalue = list[i];
513     HeapFree( GetProcessHeap(), 0, list );
514     if (!retvalue) retvalue = hwndParent;
515     return retvalue;
516 }
517
518
519 /*******************************************************************
520  *         WINPOS_GetWinOffset
521  *
522  * Calculate the offset between the origin of the two windows. Used
523  * to implement MapWindowPoints.
524  */
525 static void WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo, POINT *offset )
526 {
527     WND * wndPtr;
528
529     offset->x = offset->y = 0;
530
531     /* Translate source window origin to screen coords */
532     if (hwndFrom)
533     {
534         HWND hwnd = hwndFrom;
535
536         while (hwnd)
537         {
538             if (hwnd == hwndTo) return;
539             if (!(wndPtr = WIN_GetPtr( hwnd )))
540             {
541                 ERR( "bad hwndFrom = %04x\n", hwnd );
542                 return;
543             }
544             if (wndPtr == WND_OTHER_PROCESS) goto other_process;
545             offset->x += wndPtr->rectClient.left;
546             offset->y += wndPtr->rectClient.top;
547             hwnd = wndPtr->parent;
548             WIN_ReleasePtr( wndPtr );
549         }
550     }
551
552     /* Translate origin to destination window coords */
553     if (hwndTo)
554     {
555         HWND hwnd = hwndTo;
556
557         while (hwnd)
558         {
559             if (!(wndPtr = WIN_GetPtr( hwnd )))
560             {
561                 ERR( "bad hwndTo = %04x\n", hwnd );
562                 return;
563             }
564             if (wndPtr == WND_OTHER_PROCESS) goto other_process;
565             offset->x -= wndPtr->rectClient.left;
566             offset->y -= wndPtr->rectClient.top;
567             hwnd = wndPtr->parent;
568             WIN_ReleasePtr( wndPtr );
569         }
570     }
571     return;
572
573  other_process:  /* one of the parents may belong to another process, do it the hard way */
574     offset->x = offset->y = 0;
575     SERVER_START_REQ( get_windows_offset )
576     {
577         req->from = hwndFrom;
578         req->to   = hwndTo;
579         if (!wine_server_call( req ))
580         {
581             offset->x = reply->x;
582             offset->y = reply->y;
583         }
584     }
585     SERVER_END_REQ;
586 }
587
588
589 /*******************************************************************
590  *              MapWindowPoints (USER.258)
591  */
592 void WINAPI MapWindowPoints16( HWND16 hwndFrom, HWND16 hwndTo,
593                                LPPOINT16 lppt, UINT16 count )
594 {
595     POINT offset;
596
597     WINPOS_GetWinOffset( WIN_Handle32(hwndFrom), WIN_Handle32(hwndTo), &offset );
598     while (count--)
599     {
600         lppt->x += offset.x;
601         lppt->y += offset.y;
602         lppt++;
603     }
604 }
605
606
607 /*******************************************************************
608  *              MapWindowPoints (USER32.@)
609  */
610 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
611 {
612     POINT offset;
613
614     WINPOS_GetWinOffset( hwndFrom, hwndTo, &offset );
615     while (count--)
616     {
617         lppt->x += offset.x;
618         lppt->y += offset.y;
619         lppt++;
620     }
621     return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
622 }
623
624
625 /***********************************************************************
626  *              IsIconic (USER32.@)
627  */
628 BOOL WINAPI IsIconic(HWND hWnd)
629 {
630     return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
631 }
632
633
634 /***********************************************************************
635  *              IsZoomed (USER32.@)
636  */
637 BOOL WINAPI IsZoomed(HWND hWnd)
638 {
639     return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
640 }
641
642
643 /*******************************************************************
644  *              GetActiveWindow (USER32.@)
645  */
646 HWND WINAPI GetActiveWindow(void)
647 {
648     MESSAGEQUEUE *pCurMsgQ = 0;
649
650     /* Get the messageQ for the current thread */
651     if (!(pCurMsgQ = QUEUE_Current()))
652 {
653         WARN("\tCurrent message queue not found. Exiting!\n" );
654         return 0;
655     }
656
657     /* Return the current active window from the perQ data of the current message Q */
658     return PERQDATA_GetActiveWnd( pCurMsgQ->pQData );
659 }
660
661
662 /*******************************************************************
663  *         WINPOS_CanActivate
664  */
665 static BOOL WINPOS_CanActivate(HWND hwnd)
666 {
667     if (!hwnd) return FALSE;
668     return ((GetWindowLongW( hwnd, GWL_STYLE ) & (WS_DISABLED|WS_CHILD)) == 0);
669 }
670
671 /*******************************************************************
672  *         WINPOS_IsVisible
673  */
674 static BOOL WINPOS_IsVisible(HWND hwnd)
675 {
676     if (!hwnd) return FALSE;
677     return ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE) == WS_VISIBLE);
678 }
679
680
681 /*******************************************************************
682  *              SetActiveWindow (USER32.@)
683  */
684 HWND WINAPI SetActiveWindow( HWND hwnd )
685 {
686     HWND prev = 0;
687     WND *wndPtr = WIN_FindWndPtr( hwnd );
688     MESSAGEQUEUE *pMsgQ = 0, *pCurMsgQ = 0;
689
690     if (!wndPtr) return 0;
691
692     if (wndPtr->dwStyle & (WS_DISABLED | WS_CHILD)) goto error;
693
694     /* Get the messageQ for the current thread */
695     if (!(pCurMsgQ = QUEUE_Current()))
696     {
697         WARN("\tCurrent message queue not found. Exiting!\n" );
698         goto error;
699     }
700
701     /* Retrieve the message queue associated with this window */
702     pMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( wndPtr->hmemTaskQ );
703     if ( !pMsgQ )
704     {
705         WARN("\tWindow message queue not found. Exiting!\n" );
706         goto error;
707     }
708
709     /* Make sure that the window is associated with the calling threads
710      * message queue. It must share the same perQ data.
711      */
712     if ( pCurMsgQ->pQData != pMsgQ->pQData )
713     {
714         QUEUE_Unlock( pMsgQ );
715         goto error;
716     }
717
718     /* Save current active window */
719     prev = PERQDATA_GetActiveWnd( pMsgQ->pQData );
720     QUEUE_Unlock( pMsgQ );
721     WIN_ReleaseWndPtr(wndPtr);
722     WINPOS_SetActiveWindow( hwnd, FALSE, TRUE );
723     return prev;
724
725  error:
726     WIN_ReleaseWndPtr(wndPtr);
727     return 0;
728 }
729
730
731 /*******************************************************************
732  *              GetForegroundWindow (USER32.@)
733  */
734 HWND WINAPI GetForegroundWindow(void)
735 {
736     HWND hwndActive = 0;
737
738     /* Get the foreground window (active window of hActiveQueue) */
739     if ( hActiveQueue )
740     {
741         MESSAGEQUEUE *pActiveQueue = QUEUE_Lock( hActiveQueue );
742         if ( pActiveQueue )
743             hwndActive = PERQDATA_GetActiveWnd( pActiveQueue->pQData );
744
745         QUEUE_Unlock( pActiveQueue );
746     }
747
748     return hwndActive;
749 }
750
751 /*******************************************************************
752  *              SetForegroundWindow (USER32.@)
753  */
754 BOOL WINAPI SetForegroundWindow( HWND hwnd )
755 {
756     if (!hwnd) return WINPOS_SetActiveWindow( 0, FALSE, TRUE );
757
758     /* child windows get WM_CHILDACTIVATE message */
759     if ((GetWindowLongW( hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
760         return SendMessageA( hwnd, WM_CHILDACTIVATE, 0, 0 );
761
762     hwnd = WIN_GetFullHandle( hwnd );
763     if( hwnd == GetForegroundWindow() ) return FALSE;
764
765     return WINPOS_SetActiveWindow( hwnd, FALSE, TRUE );
766 }
767
768
769 /*******************************************************************
770  *              AllowSetForegroundWindow (USER32.@)
771  */
772 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
773 {
774     /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
775      * implemented, then fix this function. */
776     return TRUE;
777 }
778
779
780 /*******************************************************************
781  *              LockSetForegroundWindow (USER32.@)
782  */
783 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
784 {
785     /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
786      * implemented, then fix this function. */
787     return TRUE;
788 }
789
790
791 /*******************************************************************
792  *              SetShellWindow (USER32.@)
793  */
794 HWND WINAPI SetShellWindow(HWND hwndshell)
795 {   WARN("(hWnd=%08x) semi stub\n",hwndshell );
796
797     hGlobalShellWindow = WIN_GetFullHandle( hwndshell );
798     return hGlobalShellWindow;
799 }
800
801
802 /*******************************************************************
803  *              GetShellWindow (USER32.@)
804  */
805 HWND WINAPI GetShellWindow(void)
806 {   WARN("(hWnd=%x) semi stub\n",hGlobalShellWindow );
807
808     return hGlobalShellWindow;
809 }
810
811
812 /***********************************************************************
813  *              BringWindowToTop (USER32.@)
814  */
815 BOOL WINAPI BringWindowToTop( HWND hwnd )
816 {
817     return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
818 }
819
820
821 /***********************************************************************
822  *              MoveWindow (USER32.@)
823  */
824 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
825                             BOOL repaint )
826 {
827     int flags = SWP_NOZORDER | SWP_NOACTIVATE;
828     if (!repaint) flags |= SWP_NOREDRAW;
829     TRACE("%04x %d,%d %dx%d %d\n",
830             hwnd, x, y, cx, cy, repaint );
831     return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
832 }
833
834 /***********************************************************************
835  *           WINPOS_InitInternalPos
836  */
837 static LPINTERNALPOS WINPOS_InitInternalPos( WND* wnd, POINT pt, const RECT *restoreRect )
838 {
839     LPINTERNALPOS lpPos = (LPINTERNALPOS) GetPropA( wnd->hwndSelf,
840                                                       atomInternalPos );
841     if( !lpPos )
842     {
843         /* this happens when the window is minimized/maximized
844          * for the first time (rectWindow is not adjusted yet) */
845
846         lpPos = HeapAlloc( GetProcessHeap(), 0, sizeof(INTERNALPOS) );
847         if( !lpPos ) return NULL;
848
849         SetPropA( wnd->hwndSelf, atomInternalPos, (HANDLE)lpPos );
850         lpPos->hwndIconTitle = 0; /* defer until needs to be shown */
851         CONV_RECT32TO16( &wnd->rectWindow, &lpPos->rectNormal );
852         *(UINT*)&lpPos->ptIconPos = *(UINT*)&lpPos->ptMaxPos = 0xFFFFFFFF;
853     }
854
855     if( wnd->dwStyle & WS_MINIMIZE )
856         CONV_POINT32TO16( &pt, &lpPos->ptIconPos );
857     else if( wnd->dwStyle & WS_MAXIMIZE )
858         CONV_POINT32TO16( &pt, &lpPos->ptMaxPos );
859     else if( restoreRect )
860         CONV_RECT32TO16( restoreRect, &lpPos->rectNormal );
861
862     return lpPos;
863 }
864
865 /***********************************************************************
866  *           WINPOS_RedrawIconTitle
867  */
868 BOOL WINPOS_RedrawIconTitle( HWND hWnd )
869 {
870     LPINTERNALPOS lpPos = (LPINTERNALPOS)GetPropA( hWnd, atomInternalPos );
871     if( lpPos )
872     {
873         if( lpPos->hwndIconTitle )
874         {
875             SendMessageA( lpPos->hwndIconTitle, WM_SHOWWINDOW, TRUE, 0);
876             InvalidateRect( lpPos->hwndIconTitle, NULL, TRUE );
877             return TRUE;
878         }
879     }
880     return FALSE;
881 }
882
883 /***********************************************************************
884  *           WINPOS_ShowIconTitle
885  */
886 BOOL WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
887 {
888     LPINTERNALPOS lpPos = (LPINTERNALPOS)GetPropA( hwnd, atomInternalPos );
889
890     if( lpPos && !(GetWindowLongA( hwnd, GWL_EXSTYLE) & WS_EX_MANAGED))
891     {
892         HWND title = lpPos->hwndIconTitle;
893
894         TRACE("0x%04x %i\n", hwnd, (bShow != 0) );
895
896         if( !title )
897             lpPos->hwndIconTitle = title = ICONTITLE_Create( hwnd );
898         if( bShow )
899         {
900             if (!IsWindowVisible(title))
901             {
902                 SendMessageA( title, WM_SHOWWINDOW, TRUE, 0 );
903                 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
904                               SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
905             }
906         }
907         else ShowWindow( title, SW_HIDE );
908     }
909     return FALSE;
910 }
911
912 /*******************************************************************
913  *           WINPOS_GetMinMaxInfo
914  *
915  * Get the minimized and maximized information for a window.
916  */
917 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
918                            POINT *minTrack, POINT *maxTrack )
919 {
920     LPINTERNALPOS lpPos;
921     MINMAXINFO MinMax;
922     INT xinc, yinc;
923     LONG style = GetWindowLongA( hwnd, GWL_STYLE );
924     LONG exstyle = GetWindowLongA( hwnd, GWL_EXSTYLE );
925
926     /* Compute default values */
927
928     MinMax.ptMaxSize.x = GetSystemMetrics(SM_CXSCREEN);
929     MinMax.ptMaxSize.y = GetSystemMetrics(SM_CYSCREEN);
930     MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
931     MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
932     MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXSCREEN);
933     MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYSCREEN);
934
935     if (HAS_DLGFRAME( style, exstyle ))
936     {
937         xinc = GetSystemMetrics(SM_CXDLGFRAME);
938         yinc = GetSystemMetrics(SM_CYDLGFRAME);
939     }
940     else
941     {
942         xinc = yinc = 0;
943         if (HAS_THICKFRAME(style))
944         {
945             xinc += GetSystemMetrics(SM_CXFRAME);
946             yinc += GetSystemMetrics(SM_CYFRAME);
947         }
948         if (style & WS_BORDER)
949         {
950             xinc += GetSystemMetrics(SM_CXBORDER);
951             yinc += GetSystemMetrics(SM_CYBORDER);
952         }
953     }
954     MinMax.ptMaxSize.x += 2 * xinc;
955     MinMax.ptMaxSize.y += 2 * yinc;
956
957     lpPos = (LPINTERNALPOS)GetPropA( hwnd, atomInternalPos );
958     if( lpPos && !EMPTYPOINT(lpPos->ptMaxPos) )
959         CONV_POINT16TO32( &lpPos->ptMaxPos, &MinMax.ptMaxPosition );
960     else
961     {
962         MinMax.ptMaxPosition.x = -xinc;
963         MinMax.ptMaxPosition.y = -yinc;
964     }
965
966     SendMessageA( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
967
968       /* Some sanity checks */
969
970     TRACE("%ld %ld / %ld %ld / %ld %ld / %ld %ld\n",
971                       MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
972                       MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
973                       MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
974                       MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
975     MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
976                                    MinMax.ptMinTrackSize.x );
977     MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
978                                    MinMax.ptMinTrackSize.y );
979
980     if (maxSize) *maxSize = MinMax.ptMaxSize;
981     if (maxPos) *maxPos = MinMax.ptMaxPosition;
982     if (minTrack) *minTrack = MinMax.ptMinTrackSize;
983     if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
984 }
985
986 /***********************************************************************
987  *              ShowWindowAsync (USER32.@)
988  *
989  * doesn't wait; returns immediately.
990  * used by threads to toggle windows in other (possibly hanging) threads
991  */
992 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
993 {
994     /* FIXME: does ShowWindow() return immediately ? */
995     return ShowWindow(hwnd, cmd);
996 }
997
998
999 /***********************************************************************
1000  *              ShowWindow (USER32.@)
1001  */
1002 BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
1003 {
1004     HWND full_handle;
1005
1006     if ((full_handle = WIN_IsCurrentThread( hwnd )))
1007         return USER_Driver.pShowWindow( full_handle, cmd );
1008     return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
1009 }
1010
1011
1012 /***********************************************************************
1013  *              GetInternalWindowPos (USER.460)
1014  */
1015 UINT16 WINAPI GetInternalWindowPos16( HWND16 hwnd, LPRECT16 rectWnd,
1016                                       LPPOINT16 ptIcon )
1017 {
1018     WINDOWPLACEMENT16 wndpl;
1019     if (GetWindowPlacement16( hwnd, &wndpl ))
1020     {
1021         if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
1022         if (ptIcon)  *ptIcon = wndpl.ptMinPosition;
1023         return wndpl.showCmd;
1024     }
1025     return 0;
1026 }
1027
1028
1029 /***********************************************************************
1030  *              GetInternalWindowPos (USER32.@)
1031  */
1032 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
1033                                       LPPOINT ptIcon )
1034 {
1035     WINDOWPLACEMENT wndpl;
1036     if (GetWindowPlacement( hwnd, &wndpl ))
1037     {
1038         if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
1039         if (ptIcon)  *ptIcon = wndpl.ptMinPosition;
1040         return wndpl.showCmd;
1041     }
1042     return 0;
1043 }
1044
1045
1046 /***********************************************************************
1047  *              GetWindowPlacement (USER32.@)
1048  *
1049  * Win95:
1050  * Fails if wndpl->length of Win95 (!) apps is invalid.
1051  */
1052 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
1053 {
1054     WND *pWnd = WIN_FindWndPtr( hwnd );
1055     LPINTERNALPOS lpPos;
1056
1057     if(!pWnd ) return FALSE;
1058
1059     lpPos = WINPOS_InitInternalPos( pWnd, *(LPPOINT)&pWnd->rectWindow.left, &pWnd->rectWindow );
1060     wndpl->length  = sizeof(*wndpl);
1061     if( pWnd->dwStyle & WS_MINIMIZE )
1062         wndpl->showCmd = SW_SHOWMINIMIZED;
1063     else
1064         wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
1065     if( pWnd->flags & WIN_RESTORE_MAX )
1066         wndpl->flags = WPF_RESTORETOMAXIMIZED;
1067     else
1068         wndpl->flags = 0;
1069     CONV_POINT16TO32( &lpPos->ptIconPos, &wndpl->ptMinPosition );
1070     CONV_POINT16TO32( &lpPos->ptMaxPos, &wndpl->ptMaxPosition );
1071     CONV_RECT16TO32( &lpPos->rectNormal, &wndpl->rcNormalPosition );
1072     WIN_ReleaseWndPtr(pWnd);
1073     return TRUE;
1074 }
1075
1076
1077 /***********************************************************************
1078  *           WINPOS_SetPlacement
1079  */
1080 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
1081 {
1082     WND *pWnd = WIN_FindWndPtr( hwnd );
1083     if( pWnd )
1084     {
1085         LPINTERNALPOS lpPos = (LPINTERNALPOS)WINPOS_InitInternalPos( pWnd,
1086                              *(LPPOINT)&pWnd->rectWindow.left, &pWnd->rectWindow );
1087
1088         if( flags & PLACE_MIN ) CONV_POINT32TO16( &wndpl->ptMinPosition, &lpPos->ptIconPos );
1089         if( flags & PLACE_MAX ) CONV_POINT32TO16( &wndpl->ptMaxPosition, &lpPos->ptMaxPos );
1090         if( flags & PLACE_RECT) CONV_RECT32TO16( &wndpl->rcNormalPosition, &lpPos->rectNormal );
1091
1092         if( pWnd->dwStyle & WS_MINIMIZE )
1093         {
1094             WINPOS_ShowIconTitle( pWnd->hwndSelf, FALSE );
1095             if( wndpl->flags & WPF_SETMINPOSITION && !EMPTYPOINT(lpPos->ptIconPos))
1096                 SetWindowPos( hwnd, 0, lpPos->ptIconPos.x, lpPos->ptIconPos.y,
1097                                 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1098         }
1099         else if( pWnd->dwStyle & WS_MAXIMIZE )
1100         {
1101             if( !EMPTYPOINT(lpPos->ptMaxPos) )
1102                 SetWindowPos( hwnd, 0, lpPos->ptMaxPos.x, lpPos->ptMaxPos.y,
1103                                 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1104         }
1105         else if( flags & PLACE_RECT )
1106                 SetWindowPos( hwnd, 0, lpPos->rectNormal.left, lpPos->rectNormal.top,
1107                                 lpPos->rectNormal.right - lpPos->rectNormal.left,
1108                                 lpPos->rectNormal.bottom - lpPos->rectNormal.top,
1109                                 SWP_NOZORDER | SWP_NOACTIVATE );
1110
1111         ShowWindow( hwnd, wndpl->showCmd );
1112         if( IsWindow(hwnd) && pWnd->dwStyle & WS_MINIMIZE )
1113         {
1114             if( pWnd->dwStyle & WS_VISIBLE ) WINPOS_ShowIconTitle( pWnd->hwndSelf, TRUE );
1115
1116             /* SDK: ...valid only the next time... */
1117             if( wndpl->flags & WPF_RESTORETOMAXIMIZED ) pWnd->flags |= WIN_RESTORE_MAX;
1118         }
1119         WIN_ReleaseWndPtr(pWnd);
1120         return TRUE;
1121     }
1122     return FALSE;
1123 }
1124
1125
1126 /***********************************************************************
1127  *              SetWindowPlacement (USER32.@)
1128  *
1129  * Win95:
1130  * Fails if wndpl->length of Win95 (!) apps is invalid.
1131  */
1132 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
1133 {
1134     if (!wpl) return FALSE;
1135     return WINPOS_SetPlacement( hwnd, wpl, PLACE_MIN | PLACE_MAX | PLACE_RECT );
1136 }
1137
1138
1139 /***********************************************************************
1140  *              AnimateWindow (USER32.@)
1141  *              Shows/Hides a window with an animation
1142  *              NO ANIMATION YET
1143  */
1144 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
1145 {
1146         FIXME("partial stub\n");
1147
1148         /* If trying to show/hide and it's already   *
1149          * shown/hidden or invalid window, fail with *
1150          * invalid parameter                         */
1151         if(!IsWindow(hwnd) ||
1152            (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
1153            (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
1154         {
1155                 SetLastError(ERROR_INVALID_PARAMETER);
1156                 return FALSE;
1157         }
1158
1159         ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1160
1161         return TRUE;
1162 }
1163
1164 /***********************************************************************
1165  *              SetInternalWindowPos (USER32.@)
1166  */
1167 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1168                                     LPRECT rect, LPPOINT pt )
1169 {
1170     if( IsWindow(hwnd) )
1171     {
1172         WINDOWPLACEMENT wndpl;
1173         UINT flags;
1174
1175         wndpl.length  = sizeof(wndpl);
1176         wndpl.showCmd = showCmd;
1177         wndpl.flags = flags = 0;
1178
1179         if( pt )
1180         {
1181             flags |= PLACE_MIN;
1182             wndpl.flags |= WPF_SETMINPOSITION;
1183             wndpl.ptMinPosition = *pt;
1184         }
1185         if( rect )
1186         {
1187             flags |= PLACE_RECT;
1188             wndpl.rcNormalPosition = *rect;
1189         }
1190         WINPOS_SetPlacement( hwnd, &wndpl, flags );
1191     }
1192 }
1193
1194 /*******************************************************************
1195  *         WINPOS_SetActiveWindow
1196  *
1197  * SetActiveWindow() back-end. This is the only function that
1198  * can assign active status to a window. It must be called only
1199  * for the top level windows.
1200  */
1201 BOOL WINPOS_SetActiveWindow( HWND hWnd, BOOL fMouse, BOOL fChangeFocus)
1202 {
1203     WND*     wndPtr=0, *wndTemp;
1204     HQUEUE16 hOldActiveQueue, hNewActiveQueue;
1205     MESSAGEQUEUE *pOldActiveQueue = 0, *pNewActiveQueue = 0;
1206     WORD     wIconized = 0;
1207     HWND     hwndActive = 0;
1208     BOOL     bRet = 0;
1209
1210     TRACE("(%04x, %d, %d)\n", hWnd, fMouse, fChangeFocus );
1211
1212     /* Get current active window from the active queue */
1213     if ( hActiveQueue )
1214     {
1215         pOldActiveQueue = QUEUE_Lock( hActiveQueue );
1216         if ( pOldActiveQueue )
1217             hwndActive = PERQDATA_GetActiveWnd( pOldActiveQueue->pQData );
1218     }
1219
1220     if ((wndPtr = WIN_FindWndPtr(hWnd)))
1221         hWnd = wndPtr->hwndSelf;  /* make it a full handle */
1222
1223     /* paranoid checks */
1224     if( hWnd == GetDesktopWindow() || (bRet = (hWnd == hwndActive)) )
1225         goto CLEANUP_END;
1226
1227 /*  if (wndPtr && (GetFastQueue16() != wndPtr->hmemTaskQ))
1228  *      return 0;
1229  */
1230     hOldActiveQueue = hActiveQueue;
1231
1232     if( (wndTemp = WIN_FindWndPtr(hwndActive)) )
1233     {
1234         wIconized = HIWORD(wndTemp->dwStyle & WS_MINIMIZE);
1235         WIN_ReleaseWndPtr(wndTemp);
1236     }
1237     else
1238         TRACE("no current active window.\n");
1239
1240     /* call CBT hook chain */
1241     if (HOOK_IsHooked( WH_CBT ))
1242     {
1243         CBTACTIVATESTRUCT cbt;
1244         cbt.fMouse     = fMouse;
1245         cbt.hWndActive = hwndActive;
1246         if (HOOK_CallHooksA( WH_CBT, HCBT_ACTIVATE, (WPARAM)hWnd, (LPARAM)&cbt )) goto CLEANUP_END;
1247     }
1248
1249     /* set prev active wnd to current active wnd and send notification */
1250     if ((hwndPrevActive = hwndActive) && IsWindow(hwndPrevActive))
1251     {
1252         MESSAGEQUEUE *pTempActiveQueue = 0;
1253
1254         SendNotifyMessageA( hwndPrevActive, WM_NCACTIVATE, FALSE, 0 );
1255         SendNotifyMessageA( hwndPrevActive, WM_ACTIVATE,
1256                         MAKEWPARAM( WA_INACTIVE, wIconized ),
1257                         (LPARAM)hWnd );
1258
1259         /* check if something happened during message processing
1260          * (global active queue may have changed)
1261          */
1262         pTempActiveQueue = QUEUE_Lock( hActiveQueue );
1263         if(!pTempActiveQueue)
1264             goto CLEANUP_END;
1265
1266         hwndActive = PERQDATA_GetActiveWnd( pTempActiveQueue->pQData );
1267         QUEUE_Unlock( pTempActiveQueue );
1268         if( hwndPrevActive != hwndActive )
1269             goto CLEANUP_END;
1270     }
1271
1272     /* Set new active window in the message queue */
1273     hwndActive = hWnd;
1274     if ( wndPtr )
1275     {
1276         pNewActiveQueue = QUEUE_Lock( wndPtr->hmemTaskQ );
1277         if ( pNewActiveQueue )
1278             PERQDATA_SetActiveWnd( pNewActiveQueue->pQData, hwndActive );
1279     }
1280     else /* have to do this or MDI frame activation goes to hell */
1281         if( pOldActiveQueue )
1282             PERQDATA_SetActiveWnd( pOldActiveQueue->pQData, 0 );
1283
1284     /* send palette messages */
1285     if (hWnd && SendMessageW( hWnd, WM_QUERYNEWPALETTE, 0, 0L))
1286         SendMessageW( HWND_BROADCAST, WM_PALETTEISCHANGING, (WPARAM)hWnd, 0 );
1287
1288     /* if prev wnd is minimized redraw icon title */
1289     if( IsIconic( hwndPrevActive ) ) WINPOS_RedrawIconTitle(hwndPrevActive);
1290
1291     /* managed windows will get ConfigureNotify event */
1292     if (wndPtr && !(wndPtr->dwStyle & WS_CHILD) && !(wndPtr->dwExStyle & WS_EX_MANAGED))
1293     {
1294         /* check Z-order and bring hWnd to the top */
1295         HWND tmp = GetTopWindow(0);
1296         while (tmp && !(GetWindowLongA( tmp, GWL_STYLE ) & WS_VISIBLE))
1297             tmp = GetWindow( tmp, GW_HWNDNEXT );
1298
1299         if( tmp != hWnd )
1300             SetWindowPos(hWnd, HWND_TOP, 0,0,0,0,
1301                            SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
1302         if (!IsWindow(hWnd))
1303             goto CLEANUP;
1304     }
1305
1306     /* Get a handle to the new active queue */
1307     hNewActiveQueue = wndPtr ? wndPtr->hmemTaskQ : 0;
1308
1309     /* send WM_ACTIVATEAPP if necessary */
1310     if (hOldActiveQueue != hNewActiveQueue)
1311     {
1312         HWND *list, *phwnd;
1313         DWORD old_thread = GetWindowThreadProcessId( hwndPrevActive, NULL );
1314         DWORD new_thread = GetWindowThreadProcessId( hwndActive, NULL );
1315
1316         if ((list = WIN_ListChildren( GetDesktopWindow() )))
1317         {
1318             for (phwnd = list; *phwnd; phwnd++)
1319             {
1320                 if (!IsWindow( *phwnd )) continue;
1321                 if (GetWindowThreadProcessId( *phwnd, NULL ) == old_thread)
1322                     SendNotifyMessageW( *phwnd, WM_ACTIVATEAPP, 0, new_thread );
1323             }
1324             HeapFree( GetProcessHeap(), 0, list );
1325         }
1326
1327         hActiveQueue = hNewActiveQueue;
1328
1329         if ((list = WIN_ListChildren( GetDesktopWindow() )))
1330         {
1331             for (phwnd = list; *phwnd; phwnd++)
1332             {
1333                 if (!IsWindow( *phwnd )) continue;
1334                 if (GetWindowThreadProcessId( *phwnd, NULL ) == new_thread)
1335                     SendMessageW( *phwnd, WM_ACTIVATEAPP, 1, old_thread );
1336             }
1337             HeapFree( GetProcessHeap(), 0, list );
1338         }
1339
1340         if (hWnd && !IsWindow(hWnd)) goto CLEANUP;
1341     }
1342
1343     if (hWnd)
1344     {
1345         /* walk up to the first unowned window */
1346         HWND tmp = GetAncestor( hWnd, GA_ROOTOWNER );
1347         if ((wndTemp = WIN_FindWndPtr( tmp )))
1348         {
1349             /* and set last active owned popup */
1350             wndTemp->hwndLastActive = hWnd;
1351
1352             wIconized = HIWORD(wndTemp->dwStyle & WS_MINIMIZE);
1353             WIN_ReleaseWndPtr(wndTemp);
1354         }
1355         SendMessageA( hWnd, WM_NCACTIVATE, TRUE, 0 );
1356         SendMessageA( hWnd, WM_ACTIVATE,
1357                  MAKEWPARAM( (fMouse) ? WA_CLICKACTIVE : WA_ACTIVE, wIconized),
1358                  (LPARAM)hwndPrevActive );
1359         if( !IsWindow(hWnd) ) goto CLEANUP;
1360     }
1361
1362     /* change focus if possible */
1363     if ( fChangeFocus )
1364     {
1365         if ( pNewActiveQueue )
1366         {
1367             HWND hOldFocus = PERQDATA_GetFocusWnd( pNewActiveQueue->pQData );
1368
1369             if ( !hOldFocus || GetAncestor( hOldFocus, GA_ROOT ) != hwndActive )
1370                 FOCUS_SwitchFocus( pNewActiveQueue, hOldFocus,
1371                                    (wndPtr && (wndPtr->dwStyle & WS_MINIMIZE))?
1372                                    0 : hwndActive );
1373         }
1374
1375         if ( pOldActiveQueue &&
1376              ( !pNewActiveQueue ||
1377                 pNewActiveQueue->pQData != pOldActiveQueue->pQData ) )
1378         {
1379             HWND hOldFocus = PERQDATA_GetFocusWnd( pOldActiveQueue->pQData );
1380             if ( hOldFocus )
1381                 FOCUS_SwitchFocus( pOldActiveQueue, hOldFocus, 0 );
1382         }
1383     }
1384
1385     if( !hwndPrevActive && wndPtr )
1386     {
1387         if (USER_Driver.pForceWindowRaise) USER_Driver.pForceWindowRaise( wndPtr->hwndSelf );
1388     }
1389
1390     /* if active wnd is minimized redraw icon title */
1391     if( IsIconic(hwndActive) ) WINPOS_RedrawIconTitle(hwndActive);
1392
1393     bRet = (hWnd == hwndActive);  /* Success? */
1394
1395 CLEANUP: /* Unlock the message queues before returning */
1396
1397     if ( pNewActiveQueue )
1398         QUEUE_Unlock( pNewActiveQueue );
1399
1400 CLEANUP_END:
1401
1402     if ( pOldActiveQueue )
1403         QUEUE_Unlock( pOldActiveQueue );
1404
1405     WIN_ReleaseWndPtr(wndPtr);
1406     return bRet;
1407 }
1408
1409 /*******************************************************************
1410  *         WINPOS_ActivateOtherWindow
1411  *
1412  *  Activates window other than pWnd.
1413  */
1414 void WINPOS_ActivateOtherWindow(HWND hwnd)
1415 {
1416     HWND hwndActive = 0;
1417     HWND hwndTo = 0;
1418     HWND hwndDefaultTo = 0;
1419     HWND owner;
1420
1421     /* Get current active window from the active queue */
1422     if ( hActiveQueue )
1423     {
1424         MESSAGEQUEUE *pActiveQueue = QUEUE_Lock( hActiveQueue );
1425         if ( pActiveQueue )
1426         {
1427             hwndActive = PERQDATA_GetActiveWnd( pActiveQueue->pQData );
1428             QUEUE_Unlock( pActiveQueue );
1429         }
1430     }
1431
1432     if (!(hwnd = WIN_IsCurrentThread( hwnd ))) return;
1433
1434     if( hwnd == hwndPrevActive )
1435         hwndPrevActive = 0;
1436
1437     if( hwndActive != hwnd && (hwndActive || USER_IsExitingThread( GetCurrentThreadId() )))
1438         return;
1439
1440     if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) ||
1441         !(owner = GetWindow( hwnd, GW_OWNER )) ||
1442         !WINPOS_CanActivate((hwndTo = GetAncestor( owner, GA_ROOT ))) ||
1443         !WINPOS_IsVisible(hwndTo))
1444     {
1445         HWND tmp = GetAncestor( hwnd, GA_ROOT );
1446         hwndTo = hwndPrevActive;
1447
1448         while( !WINPOS_CanActivate(hwndTo) || !WINPOS_IsVisible(hwndTo))
1449         {
1450             /* by now owned windows should've been taken care of */
1451             if(!hwndDefaultTo && WINPOS_CanActivate(hwndTo))
1452                 hwndDefaultTo = hwndTo;
1453             tmp = hwndTo = GetWindow( tmp, GW_HWNDNEXT );
1454             if( !hwndTo )
1455             {
1456                 hwndTo = hwndDefaultTo;
1457                 break;
1458             }
1459         }
1460     }
1461
1462     SetActiveWindow( hwndTo );
1463     hwndPrevActive = 0;
1464 }
1465
1466
1467 /***********************************************************************
1468  *           WINPOS_HandleWindowPosChanging16
1469  *
1470  * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1471  */
1472 LONG WINPOS_HandleWindowPosChanging16( HWND hwnd, WINDOWPOS16 *winpos )
1473 {
1474     POINT maxSize, minTrack;
1475     LONG style = GetWindowLongA( hwnd, GWL_STYLE );
1476
1477     if (winpos->flags & SWP_NOSIZE) return 0;
1478     if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1479     {
1480         WINPOS_GetMinMaxInfo( hwnd, &maxSize, NULL, &minTrack, NULL );
1481         if (maxSize.x < winpos->cx) winpos->cx = maxSize.x;
1482         if (maxSize.y < winpos->cy) winpos->cy = maxSize.y;
1483         if (!(style & WS_MINIMIZE))
1484         {
1485             if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1486             if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1487         }
1488     }
1489     return 0;
1490 }
1491
1492
1493 /***********************************************************************
1494  *           WINPOS_HandleWindowPosChanging
1495  *
1496  * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1497  */
1498 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1499 {
1500     POINT maxSize, minTrack;
1501     LONG style = GetWindowLongA( hwnd, GWL_STYLE );
1502
1503     if (winpos->flags & SWP_NOSIZE) return 0;
1504     if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1505     {
1506         WINPOS_GetMinMaxInfo( hwnd, &maxSize, NULL, &minTrack, NULL );
1507         winpos->cx = min( winpos->cx, maxSize.x );
1508         winpos->cy = min( winpos->cy, maxSize.y );
1509         if (!(style & WS_MINIMIZE))
1510         {
1511             if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1512             if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1513         }
1514     }
1515     return 0;
1516 }
1517
1518
1519 /***********************************************************************
1520  *              SetWindowPos (USER32.@)
1521  */
1522 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
1523                           INT x, INT y, INT cx, INT cy, UINT flags )
1524 {
1525     WINDOWPOS winpos;
1526
1527     winpos.hwnd = hwnd;
1528     winpos.hwndInsertAfter = hwndInsertAfter;
1529     winpos.x = x;
1530     winpos.y = y;
1531     winpos.cx = cx;
1532     winpos.cy = cy;
1533     winpos.flags = flags;
1534     if (WIN_IsCurrentThread( hwnd )) return USER_Driver.pSetWindowPos( &winpos );
1535     return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
1536 }
1537
1538
1539 /***********************************************************************
1540  *              BeginDeferWindowPos (USER32.@)
1541  */
1542 HDWP WINAPI BeginDeferWindowPos( INT count )
1543 {
1544     HDWP handle;
1545     DWP *pDWP;
1546
1547     if (count < 0)
1548     {
1549         SetLastError(ERROR_INVALID_PARAMETER);
1550         return 0;
1551     }
1552     /* Windows allows zero count, in which case it allocates context for 8 moves */
1553     if (count == 0) count = 8;
1554
1555     handle = USER_HEAP_ALLOC( sizeof(DWP) + (count-1)*sizeof(WINDOWPOS) );
1556     if (!handle) return 0;
1557     pDWP = (DWP *) USER_HEAP_LIN_ADDR( handle );
1558     pDWP->actualCount    = 0;
1559     pDWP->suggestedCount = count;
1560     pDWP->valid          = TRUE;
1561     pDWP->wMagic         = DWP_MAGIC;
1562     pDWP->hwndParent     = 0;
1563     return handle;
1564 }
1565
1566
1567 /***********************************************************************
1568  *              DeferWindowPos (USER32.@)
1569  */
1570 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
1571                                 INT x, INT y, INT cx, INT cy,
1572                                 UINT flags )
1573 {
1574     DWP *pDWP;
1575     int i;
1576     HDWP newhdwp = hdwp,retvalue;
1577
1578     hwnd = WIN_GetFullHandle( hwnd );
1579     if (hwnd == GetDesktopWindow()) return 0;
1580
1581     if (!(pDWP = USER_HEAP_LIN_ADDR( hdwp ))) return 0;
1582
1583     USER_Lock();
1584
1585     for (i = 0; i < pDWP->actualCount; i++)
1586     {
1587         if (pDWP->winPos[i].hwnd == hwnd)
1588         {
1589               /* Merge with the other changes */
1590             if (!(flags & SWP_NOZORDER))
1591             {
1592                 pDWP->winPos[i].hwndInsertAfter = hwndAfter;
1593             }
1594             if (!(flags & SWP_NOMOVE))
1595             {
1596                 pDWP->winPos[i].x = x;
1597                 pDWP->winPos[i].y = y;
1598             }
1599             if (!(flags & SWP_NOSIZE))
1600             {
1601                 pDWP->winPos[i].cx = cx;
1602                 pDWP->winPos[i].cy = cy;
1603             }
1604             pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
1605                                                SWP_NOZORDER | SWP_NOREDRAW |
1606                                                SWP_NOACTIVATE | SWP_NOCOPYBITS|
1607                                                SWP_NOOWNERZORDER);
1608             pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
1609                                               SWP_FRAMECHANGED);
1610             retvalue = hdwp;
1611             goto END;
1612         }
1613     }
1614     if (pDWP->actualCount >= pDWP->suggestedCount)
1615     {
1616         newhdwp = USER_HEAP_REALLOC( hdwp,
1617                       sizeof(DWP) + pDWP->suggestedCount*sizeof(WINDOWPOS) );
1618         if (!newhdwp)
1619         {
1620             retvalue = 0;
1621             goto END;
1622         }
1623         pDWP = (DWP *) USER_HEAP_LIN_ADDR( newhdwp );
1624         pDWP->suggestedCount++;
1625     }
1626     pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
1627     pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
1628     pDWP->winPos[pDWP->actualCount].x = x;
1629     pDWP->winPos[pDWP->actualCount].y = y;
1630     pDWP->winPos[pDWP->actualCount].cx = cx;
1631     pDWP->winPos[pDWP->actualCount].cy = cy;
1632     pDWP->winPos[pDWP->actualCount].flags = flags;
1633     pDWP->actualCount++;
1634     retvalue = newhdwp;
1635 END:
1636     USER_Unlock();
1637     return retvalue;
1638 }
1639
1640
1641 /***********************************************************************
1642  *              EndDeferWindowPos (USER32.@)
1643  */
1644 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
1645 {
1646     DWP *pDWP;
1647     WINDOWPOS *winpos;
1648     BOOL res = TRUE;
1649     int i;
1650
1651     pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
1652     if (!pDWP) return FALSE;
1653     for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
1654     {
1655         if (!(res = USER_Driver.pSetWindowPos( winpos ))) break;
1656     }
1657     USER_HEAP_FREE( hdwp );
1658     return res;
1659 }
1660
1661
1662 /***********************************************************************
1663  *              TileChildWindows (USER.199)
1664  */
1665 void WINAPI TileChildWindows16( HWND16 parent, WORD action )
1666 {
1667     FIXME("(%04x, %d): stub\n", parent, action);
1668 }
1669
1670 /***********************************************************************
1671  *              CascadeChildWindows (USER.198)
1672  */
1673 void WINAPI CascadeChildWindows16( HWND16 parent, WORD action )
1674 {
1675     FIXME("(%04x, %d): stub\n", parent, action);
1676 }
1677
1678 /***********************************************************************
1679  *              SetProgmanWindow (USER32.@)
1680  */
1681 HWND WINAPI SetProgmanWindow ( HWND hwnd )
1682 {
1683         hGlobalProgmanWindow = hwnd;
1684         return hGlobalProgmanWindow;
1685 }
1686
1687 /***********************************************************************
1688  *              GetProgmanWindow (USER32.@)
1689  */
1690 HWND WINAPI GetProgmanWindow(void)
1691 {
1692         return hGlobalProgmanWindow;
1693 }
1694
1695 /***********************************************************************
1696  *              SetShellWindowEx (USER32.@)
1697  * hwndProgman =  Progman[Program Manager]
1698  *                |-> SHELLDLL_DefView
1699  * hwndListView = |   |-> SysListView32
1700  *                |   |   |-> tooltips_class32
1701  *                |   |
1702  *                |   |-> SysHeader32
1703  *                |
1704  *                |-> ProxyTarget
1705  */
1706 HWND WINAPI SetShellWindowEx ( HWND hwndProgman, HWND hwndListView )
1707 {
1708         FIXME("0x%08x 0x%08x stub\n",hwndProgman ,hwndListView );
1709         hGlobalShellWindow = hwndProgman;
1710         return hGlobalShellWindow;
1711
1712 }
1713
1714 /***********************************************************************
1715  *              SetTaskmanWindow (USER32.@)
1716  * NOTES
1717  *   hwnd = MSTaskSwWClass
1718  *          |-> SysTabControl32
1719  */
1720 HWND WINAPI SetTaskmanWindow ( HWND hwnd )
1721 {
1722         hGlobalTaskmanWindow = hwnd;
1723         return hGlobalTaskmanWindow;
1724 }
1725
1726 /***********************************************************************
1727  *              GetTaskmanWindow (USER32.@)
1728  */
1729 HWND WINAPI GetTaskmanWindow(void)
1730 {
1731         return hGlobalTaskmanWindow;
1732 }