Make sure that HWND comparisons are always done with full 32-bit
[wine/multimedia.git] / windows / winpos.c
blob05b2d6993a0f3c20263833bf279fc3adcb978c0e
1 /*
2 * Window position related functions.
4 * Copyright 1993, 1994, 1995 Alexandre Julliard
5 * 1995, 1996, 1999 Alex Korobka
6 */
8 #include <string.h>
9 #include "winerror.h"
10 #include "windef.h"
11 #include "wingdi.h"
12 #include "winerror.h"
13 #include "wine/winuser16.h"
14 #include "controls.h"
15 #include "user.h"
16 #include "region.h"
17 #include "win.h"
18 #include "hook.h"
19 #include "message.h"
20 #include "queue.h"
21 #include "winpos.h"
22 #include "dce.h"
23 #include "nonclient.h"
24 #include "debugtools.h"
25 #include "input.h"
27 DEFAULT_DEBUG_CHANNEL(win);
29 #define HAS_DLGFRAME(style,exStyle) \
30 (((exStyle) & WS_EX_DLGMODALFRAME) || \
31 (((style) & WS_DLGFRAME) && !((style) & WS_BORDER)))
33 #define HAS_THICKFRAME(style) \
34 (((style) & WS_THICKFRAME) && \
35 !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
37 #define EMPTYPOINT(pt) ((*(LONG*)&(pt)) == -1)
39 #define PLACE_MIN 0x0001
40 #define PLACE_MAX 0x0002
41 #define PLACE_RECT 0x0004
44 #define DWP_MAGIC ((INT)('W' | ('P' << 8) | ('O' << 16) | ('S' << 24)))
46 typedef struct
48 INT actualCount;
49 INT suggestedCount;
50 BOOL valid;
51 INT wMagic;
52 HWND hwndParent;
53 WINDOWPOS winPos[1];
54 } DWP;
56 /* ----- internal variables ----- */
58 static HWND hwndPrevActive = 0; /* Previously active window */
59 static HWND hGlobalShellWindow=0; /*the shell*/
60 static HWND hGlobalTaskmanWindow=0;
61 static HWND hGlobalProgmanWindow=0;
63 static LPCSTR atomInternalPos;
65 extern HQUEUE16 hActiveQueue;
67 /***********************************************************************
68 * WINPOS_CreateInternalPosAtom
70 BOOL WINPOS_CreateInternalPosAtom()
72 LPSTR str = "SysIP";
73 atomInternalPos = (LPCSTR)(DWORD)GlobalAddAtomA(str);
74 return (atomInternalPos) ? TRUE : FALSE;
77 /***********************************************************************
78 * WINPOS_CheckInternalPos
80 * Called when a window is destroyed.
82 void WINPOS_CheckInternalPos( HWND hwnd )
84 LPINTERNALPOS lpPos;
85 MESSAGEQUEUE *pMsgQ = 0;
86 WND *wndPtr = WIN_FindWndPtr( hwnd );
88 lpPos = (LPINTERNALPOS) GetPropA( hwnd, atomInternalPos );
90 /* Retrieve the message queue associated with this window */
91 pMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( wndPtr->hmemTaskQ );
92 if ( !pMsgQ )
94 WARN("\tMessage queue not found. Exiting!\n" );
95 WIN_ReleaseWndPtr( wndPtr );
96 return;
99 if( hwnd == hwndPrevActive ) hwndPrevActive = 0;
101 if( hwnd == PERQDATA_GetActiveWnd( pMsgQ->pQData ) )
103 PERQDATA_SetActiveWnd( pMsgQ->pQData, 0 );
104 WARN("\tattempt to activate destroyed window!\n");
107 if( lpPos )
109 if( IsWindow(lpPos->hwndIconTitle) )
110 DestroyWindow( lpPos->hwndIconTitle );
111 HeapFree( GetProcessHeap(), 0, lpPos );
114 QUEUE_Unlock( pMsgQ );
115 WIN_ReleaseWndPtr( wndPtr );
116 return;
119 /***********************************************************************
120 * ArrangeIconicWindows (USER.170)
122 UINT16 WINAPI ArrangeIconicWindows16( HWND16 parent)
124 return ArrangeIconicWindows(parent);
126 /***********************************************************************
127 * ArrangeIconicWindows (USER32.@)
129 UINT WINAPI ArrangeIconicWindows( HWND parent )
131 RECT rectParent;
132 HWND hwndChild;
133 INT x, y, xspacing, yspacing;
135 GetClientRect( parent, &rectParent );
136 x = rectParent.left;
137 y = rectParent.bottom;
138 xspacing = GetSystemMetrics(SM_CXICONSPACING);
139 yspacing = GetSystemMetrics(SM_CYICONSPACING);
141 hwndChild = GetWindow( parent, GW_CHILD );
142 while (hwndChild)
144 if( IsIconic( hwndChild ) )
146 WINPOS_ShowIconTitle( hwndChild, FALSE );
148 SetWindowPos( hwndChild, 0, x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
149 y - yspacing - GetSystemMetrics(SM_CYICON)/2, 0, 0,
150 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
151 if( IsWindow(hwndChild) )
152 WINPOS_ShowIconTitle(hwndChild , TRUE );
154 if (x <= rectParent.right - xspacing) x += xspacing;
155 else
157 x = rectParent.left;
158 y -= yspacing;
161 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
163 return yspacing;
167 /***********************************************************************
168 * SwitchToThisWindow (USER.172)
170 void WINAPI SwitchToThisWindow16( HWND16 hwnd, BOOL16 restore )
172 SwitchToThisWindow( hwnd, restore );
176 /***********************************************************************
177 * SwitchToThisWindow (USER32.@)
179 void WINAPI SwitchToThisWindow( HWND hwnd, BOOL restore )
181 ShowWindow( hwnd, restore ? SW_RESTORE : SW_SHOWMINIMIZED );
185 /***********************************************************************
186 * GetWindowRect (USER.32)
188 void WINAPI GetWindowRect16( HWND16 hwnd, LPRECT16 rect )
190 RECT rect32;
192 GetWindowRect( hwnd, &rect32 );
193 rect->left = rect32.left;
194 rect->top = rect32.top;
195 rect->right = rect32.right;
196 rect->bottom = rect32.bottom;
200 /***********************************************************************
201 * GetWindowRect (USER32.@)
203 BOOL WINAPI GetWindowRect( HWND hwnd, LPRECT rect )
205 WND * wndPtr = WIN_FindWndPtr( hwnd );
206 if (!wndPtr) return FALSE;
207 *rect = wndPtr->rectWindow;
208 WIN_ReleaseWndPtr(wndPtr);
209 MapWindowPoints( GetAncestor( hwnd, GA_PARENT ), 0, (POINT *)rect, 2 );
210 TRACE("hwnd %04x (%d,%d)-(%d,%d)\n",
211 hwnd, rect->left, rect->top, rect->right, rect->bottom);
212 return TRUE;
216 /***********************************************************************
217 * GetWindowRgn (USER32.@)
219 int WINAPI GetWindowRgn ( HWND hwnd, HRGN hrgn )
221 int nRet = ERROR;
222 WND *wndPtr = WIN_FindWndPtr( hwnd );
223 if (wndPtr)
225 if (wndPtr->hrgnWnd) nRet = CombineRgn( hrgn, wndPtr->hrgnWnd, 0, RGN_COPY );
226 WIN_ReleaseWndPtr(wndPtr);
228 return nRet;
231 /***********************************************************************
232 * SetWindowRgn (USER32.@)
234 int WINAPI SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL bRedraw )
236 RECT rect;
237 WND *wndPtr;
238 int ret = FALSE;
240 if (USER_Driver.pSetWindowRgn)
241 return USER_Driver.pSetWindowRgn( hwnd, hrgn, bRedraw );
243 if (!(wndPtr = WIN_FindWndPtr(hwnd))) return FALSE;
245 if (wndPtr->hrgnWnd == hrgn)
247 ret = TRUE;
248 goto done;
251 if (hrgn) /* verify that region really exists */
253 if (GetRgnBox( hrgn, &rect ) == ERROR) goto done;
256 if (wndPtr->hrgnWnd)
258 /* delete previous region */
259 DeleteObject(wndPtr->hrgnWnd);
260 wndPtr->hrgnWnd = 0;
262 wndPtr->hrgnWnd = hrgn;
264 /* Size the window to the rectangle of the new region (if it isn't NULL) */
265 if (hrgn) SetWindowPos( hwnd, 0, rect.left, rect.top,
266 rect.right - rect.left, rect.bottom - rect.top,
267 SWP_NOSIZE | SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOACTIVATE |
268 SWP_NOZORDER | (bRedraw ? 0 : SWP_NOREDRAW) );
269 ret = TRUE;
271 done:
272 WIN_ReleaseWndPtr(wndPtr);
273 return ret;
276 /***********************************************************************
277 * SetWindowRgn (USER.668)
279 INT16 WINAPI SetWindowRgn16( HWND16 hwnd, HRGN16 hrgn,BOOL16 bRedraw)
283 FIXME("SetWindowRgn16: stub\n");
284 return TRUE;
288 /***********************************************************************
289 * GetClientRect (USER.33)
291 void WINAPI GetClientRect16( HWND16 hwnd, LPRECT16 rect )
293 WND * wndPtr = WIN_FindWndPtr( hwnd );
295 rect->left = rect->top = rect->right = rect->bottom = 0;
296 if (wndPtr)
298 rect->right = wndPtr->rectClient.right - wndPtr->rectClient.left;
299 rect->bottom = wndPtr->rectClient.bottom - wndPtr->rectClient.top;
301 WIN_ReleaseWndPtr(wndPtr);
305 /***********************************************************************
306 * GetClientRect (USER32.@)
308 BOOL WINAPI GetClientRect( HWND hwnd, LPRECT rect )
310 WND * wndPtr = WIN_FindWndPtr( hwnd );
312 rect->left = rect->top = rect->right = rect->bottom = 0;
313 if (!wndPtr) return FALSE;
314 rect->right = wndPtr->rectClient.right - wndPtr->rectClient.left;
315 rect->bottom = wndPtr->rectClient.bottom - wndPtr->rectClient.top;
317 WIN_ReleaseWndPtr(wndPtr);
318 TRACE("hwnd %04x (%d,%d)-(%d,%d)\n",
319 hwnd, rect->left, rect->top, rect->right, rect->bottom);
320 return TRUE;
324 /*******************************************************************
325 * ClientToScreen (USER.28)
327 void WINAPI ClientToScreen16( HWND16 hwnd, LPPOINT16 lppnt )
329 MapWindowPoints16( hwnd, 0, lppnt, 1 );
333 /*******************************************************************
334 * ClientToScreen (USER32.@)
336 BOOL WINAPI ClientToScreen( HWND hwnd, LPPOINT lppnt )
338 MapWindowPoints( hwnd, 0, lppnt, 1 );
339 return TRUE;
343 /*******************************************************************
344 * ScreenToClient (USER.29)
346 void WINAPI ScreenToClient16( HWND16 hwnd, LPPOINT16 lppnt )
348 MapWindowPoints16( 0, hwnd, lppnt, 1 );
352 /*******************************************************************
353 * ScreenToClient (USER32.@)
355 BOOL WINAPI ScreenToClient( HWND hwnd, LPPOINT lppnt )
357 MapWindowPoints( 0, hwnd, lppnt, 1 );
358 return TRUE;
362 /***********************************************************************
363 * WINPOS_WindowFromPoint
365 * Find the window and hittest for a given point.
367 HWND WINPOS_WindowFromPoint( HWND hwndScope, POINT pt, INT *hittest )
369 WND *wndScope, *wndPtr, *wndTmp;
370 HWND hwnd_ret = 0;
371 POINT xy = pt;
373 TRACE("scope %04x %ld,%ld\n", hwndScope, pt.x, pt.y);
375 if (!hwndScope) hwndScope = GetDesktopWindow();
376 if (!(wndScope = WIN_FindWndPtr( hwndScope ))) return 0;
377 hwndScope = wndScope->hwndSelf; /* make it a full handle */
379 *hittest = HTERROR;
380 wndPtr = WIN_LockWndPtr(wndScope->child);
382 if( wndScope->dwStyle & WS_DISABLED )
384 *hittest = HTERROR;
385 goto end;
388 if (wndScope->parent)
389 MapWindowPoints( GetDesktopWindow(), wndScope->parent->hwndSelf, &xy, 1 );
391 if (xy.x < wndScope->rectClient.left || pt.x >= wndScope->rectClient.right ||
392 xy.y < wndScope->rectClient.top || pt.y >= wndScope->rectClient.bottom ||
393 wndScope->dwStyle & WS_MINIMIZE)
394 goto hittest;
396 xy.x -= wndScope->rectClient.left;
397 xy.y -= wndScope->rectClient.top;
399 for (;;)
401 while (wndPtr)
403 /* If point is in window, and window is visible, and it */
404 /* is enabled (or it's a top-level window), then explore */
405 /* its children. Otherwise, go to the next window. */
407 if ((wndPtr->dwStyle & WS_VISIBLE) &&
408 ((wndPtr->dwExStyle & (WS_EX_LAYERED | WS_EX_TRANSPARENT)) != (WS_EX_LAYERED | WS_EX_TRANSPARENT)) &&
409 (!(wndPtr->dwStyle & WS_DISABLED) ||
410 ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)) &&
411 (wndPtr->hrgnWnd ?
412 PtInRegion(wndPtr->hrgnWnd, xy.x - wndPtr->rectWindow.left,
413 xy.y - wndPtr->rectWindow.top) :
414 ((xy.x >= wndPtr->rectWindow.left) &&
415 (xy.x < wndPtr->rectWindow.right) &&
416 (xy.y >= wndPtr->rectWindow.top) &&
417 (xy.y < wndPtr->rectWindow.bottom))))
419 TRACE("%ld,%ld is inside %04x\n", xy.x, xy.y, wndPtr->hwndSelf);
420 hwnd_ret = wndPtr->hwndSelf; /* Got a suitable window */
422 /* If window is minimized or disabled, return at once */
423 if (wndPtr->dwStyle & WS_MINIMIZE)
425 *hittest = HTCAPTION;
426 goto end;
428 if (wndPtr->dwStyle & WS_DISABLED)
430 *hittest = HTERROR;
431 goto end;
434 /* If point is not in client area, ignore the children */
435 if ((xy.x < wndPtr->rectClient.left) ||
436 (xy.x >= wndPtr->rectClient.right) ||
437 (xy.y < wndPtr->rectClient.top) ||
438 (xy.y >= wndPtr->rectClient.bottom)) break;
440 xy.x -= wndPtr->rectClient.left;
441 xy.y -= wndPtr->rectClient.top;
442 WIN_UpdateWndPtr(&wndPtr,wndPtr->child);
444 else
446 WIN_UpdateWndPtr(&wndPtr,wndPtr->next);
450 hittest:
451 /* If nothing found, try the scope window */
452 if (!hwnd_ret) hwnd_ret = hwndScope;
454 /* Send the WM_NCHITTEST message (only if to the same task) */
455 if (GetWindowThreadProcessId( hwnd_ret, NULL ) == GetCurrentThreadId())
457 INT res = SendMessageA( hwnd_ret, WM_NCHITTEST, 0, MAKELONG( pt.x, pt.y ) );
458 if (res != HTTRANSPARENT)
460 *hittest = res; /* Found the window */
461 goto end;
464 else
466 *hittest = HTCLIENT;
467 goto end;
470 if (!(wndTmp = WIN_FindWndPtr( hwnd_ret ))) break;
472 /* If no children found in last search, make point relative to parent */
473 if (!wndPtr)
475 xy.x += wndTmp->rectClient.left;
476 xy.y += wndTmp->rectClient.top;
479 /* Restart the search from the next sibling */
480 WIN_UpdateWndPtr(&wndPtr,wndTmp->next);
481 hwnd_ret = wndTmp->parent ? wndTmp->parent->hwndSelf : 0;
482 WIN_ReleaseWndPtr( wndTmp );
485 end:
486 WIN_ReleaseWndPtr(wndPtr);
487 WIN_ReleaseWndPtr(wndScope);
488 return hwnd_ret;
492 /*******************************************************************
493 * WindowFromPoint (USER.30)
495 HWND16 WINAPI WindowFromPoint16( POINT16 pt )
497 POINT pt32;
499 CONV_POINT16TO32( &pt, &pt32 );
500 return WindowFromPoint( pt32 );
504 /*******************************************************************
505 * WindowFromPoint (USER32.@)
507 HWND WINAPI WindowFromPoint( POINT pt )
509 INT hittest;
510 return WINPOS_WindowFromPoint( 0, pt, &hittest );
514 /*******************************************************************
515 * ChildWindowFromPoint (USER.191)
517 HWND16 WINAPI ChildWindowFromPoint16( HWND16 hwndParent, POINT16 pt )
519 POINT pt32;
520 CONV_POINT16TO32( &pt, &pt32 );
521 return (HWND16)ChildWindowFromPoint( hwndParent, pt32 );
525 /*******************************************************************
526 * ChildWindowFromPoint (USER32.@)
528 HWND WINAPI ChildWindowFromPoint( HWND hwndParent, POINT pt )
530 return ChildWindowFromPointEx( hwndParent, pt, CWP_ALL );
533 /*******************************************************************
534 * ChildWindowFromPointEx (USER.399)
536 HWND16 WINAPI ChildWindowFromPointEx16( HWND16 hwndParent, POINT16 pt, UINT16 uFlags)
538 POINT pt32;
539 CONV_POINT16TO32( &pt, &pt32 );
540 return (HWND16)ChildWindowFromPointEx( hwndParent, pt32, uFlags );
544 /*******************************************************************
545 * ChildWindowFromPointEx (USER32.@)
547 HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
549 /* pt is in the client coordinates */
550 HWND *list;
551 int i;
552 RECT rect;
553 HWND retvalue = 0;
555 GetClientRect( hwndParent, &rect );
556 if (!PtInRect( &rect, pt )) return 0;
557 if (!(list = WIN_ListChildren( hwndParent ))) return 0;
559 for (i = 0; list[i] && !retvalue; i++)
561 WND *wnd = WIN_FindWndPtr( list[i] );
562 if (!wnd) continue;
563 if (PtInRect( &wnd->rectWindow, pt ))
565 if ( (uFlags & CWP_SKIPINVISIBLE) &&
566 !(wnd->dwStyle & WS_VISIBLE) );
567 else if ( (uFlags & CWP_SKIPDISABLED) &&
568 (wnd->dwStyle & WS_DISABLED) );
569 else if ( (uFlags & CWP_SKIPTRANSPARENT) &&
570 (wnd->dwExStyle & WS_EX_TRANSPARENT) );
571 else retvalue = list[i];
573 WIN_ReleaseWndPtr( wnd );
575 HeapFree( GetProcessHeap(), 0, list );
576 if (!retvalue) retvalue = hwndParent;
577 return retvalue;
581 /*******************************************************************
582 * WINPOS_GetWinOffset
584 * Calculate the offset between the origin of the two windows. Used
585 * to implement MapWindowPoints.
587 static void WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo,
588 POINT *offset )
590 WND * wndPtr = 0;
592 offset->x = offset->y = 0;
593 if (hwndFrom == hwndTo ) return;
595 /* Translate source window origin to screen coords */
596 if (hwndFrom)
598 if (!(wndPtr = WIN_FindWndPtr( hwndFrom )))
600 ERR("bad hwndFrom = %04x\n",hwndFrom);
601 return;
603 while (wndPtr->parent)
605 offset->x += wndPtr->rectClient.left;
606 offset->y += wndPtr->rectClient.top;
607 WIN_UpdateWndPtr(&wndPtr,wndPtr->parent);
609 WIN_ReleaseWndPtr(wndPtr);
612 /* Translate origin to destination window coords */
613 if (hwndTo)
615 if (!(wndPtr = WIN_FindWndPtr( hwndTo )))
617 ERR("bad hwndTo = %04x\n", hwndTo );
618 return;
620 while (wndPtr->parent)
622 offset->x -= wndPtr->rectClient.left;
623 offset->y -= wndPtr->rectClient.top;
624 WIN_UpdateWndPtr(&wndPtr,wndPtr->parent);
626 WIN_ReleaseWndPtr(wndPtr);
631 /*******************************************************************
632 * MapWindowPoints (USER.258)
634 void WINAPI MapWindowPoints16( HWND16 hwndFrom, HWND16 hwndTo,
635 LPPOINT16 lppt, UINT16 count )
637 POINT offset;
639 WINPOS_GetWinOffset( WIN_Handle32(hwndFrom), WIN_Handle32(hwndTo), &offset );
640 while (count--)
642 lppt->x += offset.x;
643 lppt->y += offset.y;
644 lppt++;
649 /*******************************************************************
650 * MapWindowPoints (USER32.@)
652 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo,
653 LPPOINT lppt, UINT count )
655 POINT offset;
657 WINPOS_GetWinOffset( WIN_GetFullHandle(hwndFrom), WIN_GetFullHandle(hwndTo), &offset );
658 while (count--)
660 lppt->x += offset.x;
661 lppt->y += offset.y;
662 lppt++;
664 return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
668 /***********************************************************************
669 * IsIconic (USER.31)
671 BOOL16 WINAPI IsIconic16(HWND16 hWnd)
673 return IsIconic(hWnd);
677 /***********************************************************************
678 * IsIconic (USER32.@)
680 BOOL WINAPI IsIconic(HWND hWnd)
682 BOOL retvalue;
683 WND * wndPtr = WIN_FindWndPtr(hWnd);
684 if (wndPtr == NULL) return FALSE;
685 retvalue = (wndPtr->dwStyle & WS_MINIMIZE) != 0;
686 WIN_ReleaseWndPtr(wndPtr);
687 return retvalue;
691 /***********************************************************************
692 * IsZoomed (USER.272)
694 BOOL16 WINAPI IsZoomed16(HWND16 hWnd)
696 return IsZoomed(hWnd);
700 /***********************************************************************
701 * IsZoomed (USER32.@)
703 BOOL WINAPI IsZoomed(HWND hWnd)
705 BOOL retvalue;
706 WND * wndPtr = WIN_FindWndPtr(hWnd);
707 if (wndPtr == NULL) return FALSE;
708 retvalue = (wndPtr->dwStyle & WS_MAXIMIZE) != 0;
709 WIN_ReleaseWndPtr(wndPtr);
710 return retvalue;
714 /*******************************************************************
715 * GetActiveWindow (USER.60)
717 HWND16 WINAPI GetActiveWindow16(void)
719 return (HWND16)GetActiveWindow();
722 /*******************************************************************
723 * GetActiveWindow (USER32.@)
725 HWND WINAPI GetActiveWindow(void)
727 MESSAGEQUEUE *pCurMsgQ = 0;
729 /* Get the messageQ for the current thread */
730 if (!(pCurMsgQ = QUEUE_Current()))
732 WARN("\tCurrent message queue not found. Exiting!\n" );
733 return 0;
736 /* Return the current active window from the perQ data of the current message Q */
737 return PERQDATA_GetActiveWnd( pCurMsgQ->pQData );
741 /*******************************************************************
742 * WINPOS_CanActivate
744 static BOOL WINPOS_CanActivate(HWND hwnd)
746 if (!hwnd) return FALSE;
747 return ((GetWindowLongW( hwnd, GWL_STYLE ) & (WS_DISABLED|WS_VISIBLE|WS_CHILD)) == WS_VISIBLE);
751 /*******************************************************************
752 * SetActiveWindow (USER.59)
754 HWND16 WINAPI SetActiveWindow16( HWND16 hwnd )
756 return SetActiveWindow(hwnd);
760 /*******************************************************************
761 * SetActiveWindow (USER32.@)
763 HWND WINAPI SetActiveWindow( HWND hwnd )
765 HWND prev = 0;
766 WND *wndPtr = WIN_FindWndPtr( hwnd );
767 MESSAGEQUEUE *pMsgQ = 0, *pCurMsgQ = 0;
769 if (!wndPtr || (wndPtr->dwStyle & (WS_DISABLED | WS_CHILD)))
771 prev = 0;
772 goto end;
775 /* Get the messageQ for the current thread */
776 if (!(pCurMsgQ = QUEUE_Current()))
778 WARN("\tCurrent message queue not found. Exiting!\n" );
779 goto CLEANUP;
782 /* Retrieve the message queue associated with this window */
783 pMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( wndPtr->hmemTaskQ );
784 if ( !pMsgQ )
786 WARN("\tWindow message queue not found. Exiting!\n" );
787 goto CLEANUP;
790 /* Make sure that the window is associated with the calling threads
791 * message queue. It must share the same perQ data.
794 if ( pCurMsgQ->pQData != pMsgQ->pQData )
795 goto CLEANUP;
797 /* Save current active window */
798 prev = PERQDATA_GetActiveWnd( pMsgQ->pQData );
800 WINPOS_SetActiveWindow( hwnd, 0, 0 );
802 CLEANUP:
803 /* Unlock the queues before returning */
804 if ( pMsgQ )
805 QUEUE_Unlock( pMsgQ );
807 end:
808 WIN_ReleaseWndPtr(wndPtr);
809 return prev;
813 /*******************************************************************
814 * GetForegroundWindow (USER.608)
816 HWND16 WINAPI GetForegroundWindow16(void)
818 return (HWND16)GetForegroundWindow();
822 /*******************************************************************
823 * SetForegroundWindow (USER.609)
825 BOOL16 WINAPI SetForegroundWindow16( HWND16 hwnd )
827 return SetForegroundWindow( hwnd );
831 /*******************************************************************
832 * GetForegroundWindow (USER32.@)
834 HWND WINAPI GetForegroundWindow(void)
836 HWND hwndActive = 0;
838 /* Get the foreground window (active window of hActiveQueue) */
839 if ( hActiveQueue )
841 MESSAGEQUEUE *pActiveQueue = QUEUE_Lock( hActiveQueue );
842 if ( pActiveQueue )
843 hwndActive = PERQDATA_GetActiveWnd( pActiveQueue->pQData );
845 QUEUE_Unlock( pActiveQueue );
848 return hwndActive;
851 /*******************************************************************
852 * SetForegroundWindow (USER32.@)
854 BOOL WINAPI SetForegroundWindow( HWND hwnd )
856 return WINPOS_ChangeActiveWindow( hwnd, FALSE );
860 /*******************************************************************
861 * AllowSetForegroundWindow (USER32.@)
863 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
865 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
866 * implemented, then fix this function. */
867 return TRUE;
871 /*******************************************************************
872 * LockSetForegroundWindow (USER32.@)
874 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
876 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
877 * implemented, then fix this function. */
878 return TRUE;
882 /*******************************************************************
883 * GetShellWindow (USER.600)
885 HWND16 WINAPI GetShellWindow16(void)
887 return GetShellWindow();
890 /*******************************************************************
891 * SetShellWindow (USER32.@)
893 HWND WINAPI SetShellWindow(HWND hwndshell)
894 { WARN("(hWnd=%08x) semi stub\n",hwndshell );
896 hGlobalShellWindow = WIN_GetFullHandle( hwndshell );
897 return hGlobalShellWindow;
901 /*******************************************************************
902 * GetShellWindow (USER32.@)
904 HWND WINAPI GetShellWindow(void)
905 { WARN("(hWnd=%x) semi stub\n",hGlobalShellWindow );
907 return hGlobalShellWindow;
911 /***********************************************************************
912 * BringWindowToTop (USER.45)
914 BOOL16 WINAPI BringWindowToTop16( HWND16 hwnd )
916 return BringWindowToTop(hwnd);
920 /***********************************************************************
921 * BringWindowToTop (USER32.@)
923 BOOL WINAPI BringWindowToTop( HWND hwnd )
925 return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
929 /***********************************************************************
930 * MoveWindow (USER.56)
932 BOOL16 WINAPI MoveWindow16( HWND16 hwnd, INT16 x, INT16 y, INT16 cx, INT16 cy,
933 BOOL16 repaint )
935 return MoveWindow(hwnd,x,y,cx,cy,repaint);
939 /***********************************************************************
940 * MoveWindow (USER32.@)
942 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
943 BOOL repaint )
945 int flags = SWP_NOZORDER | SWP_NOACTIVATE;
946 if (!repaint) flags |= SWP_NOREDRAW;
947 TRACE("%04x %d,%d %dx%d %d\n",
948 hwnd, x, y, cx, cy, repaint );
949 return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
952 /***********************************************************************
953 * WINPOS_InitInternalPos
955 static LPINTERNALPOS WINPOS_InitInternalPos( WND* wnd, POINT pt,
956 LPRECT restoreRect )
958 LPINTERNALPOS lpPos = (LPINTERNALPOS) GetPropA( wnd->hwndSelf,
959 atomInternalPos );
960 if( !lpPos )
962 /* this happens when the window is minimized/maximized
963 * for the first time (rectWindow is not adjusted yet) */
965 lpPos = HeapAlloc( GetProcessHeap(), 0, sizeof(INTERNALPOS) );
966 if( !lpPos ) return NULL;
968 SetPropA( wnd->hwndSelf, atomInternalPos, (HANDLE)lpPos );
969 lpPos->hwndIconTitle = 0; /* defer until needs to be shown */
970 CONV_RECT32TO16( &wnd->rectWindow, &lpPos->rectNormal );
971 *(UINT*)&lpPos->ptIconPos = *(UINT*)&lpPos->ptMaxPos = 0xFFFFFFFF;
974 if( wnd->dwStyle & WS_MINIMIZE )
975 CONV_POINT32TO16( &pt, &lpPos->ptIconPos );
976 else if( wnd->dwStyle & WS_MAXIMIZE )
977 CONV_POINT32TO16( &pt, &lpPos->ptMaxPos );
978 else if( restoreRect )
979 CONV_RECT32TO16( restoreRect, &lpPos->rectNormal );
981 return lpPos;
984 /***********************************************************************
985 * WINPOS_RedrawIconTitle
987 BOOL WINPOS_RedrawIconTitle( HWND hWnd )
989 LPINTERNALPOS lpPos = (LPINTERNALPOS)GetPropA( hWnd, atomInternalPos );
990 if( lpPos )
992 if( lpPos->hwndIconTitle )
994 SendMessageA( lpPos->hwndIconTitle, WM_SHOWWINDOW, TRUE, 0);
995 InvalidateRect( lpPos->hwndIconTitle, NULL, TRUE );
996 return TRUE;
999 return FALSE;
1002 /***********************************************************************
1003 * WINPOS_ShowIconTitle
1005 BOOL WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
1007 LPINTERNALPOS lpPos = (LPINTERNALPOS)GetPropA( hwnd, atomInternalPos );
1009 if( lpPos && !(GetWindowLongA( hwnd, GWL_EXSTYLE) & WS_EX_MANAGED))
1011 HWND title = lpPos->hwndIconTitle;
1013 TRACE("0x%04x %i\n", hwnd, (bShow != 0) );
1015 if( !title )
1016 lpPos->hwndIconTitle = title = ICONTITLE_Create( hwnd );
1017 if( bShow )
1019 if (!IsWindowVisible(title))
1021 SendMessageA( title, WM_SHOWWINDOW, TRUE, 0 );
1022 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
1023 SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
1026 else ShowWindow( title, SW_HIDE );
1028 return FALSE;
1031 /*******************************************************************
1032 * WINPOS_GetMinMaxInfo
1034 * Get the minimized and maximized information for a window.
1036 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
1037 POINT *minTrack, POINT *maxTrack )
1039 LPINTERNALPOS lpPos;
1040 MINMAXINFO MinMax;
1041 INT xinc, yinc;
1042 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
1043 LONG exstyle = GetWindowLongA( hwnd, GWL_EXSTYLE );
1045 /* Compute default values */
1047 MinMax.ptMaxSize.x = GetSystemMetrics(SM_CXSCREEN);
1048 MinMax.ptMaxSize.y = GetSystemMetrics(SM_CYSCREEN);
1049 MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
1050 MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
1051 MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXSCREEN);
1052 MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYSCREEN);
1054 if (HAS_DLGFRAME( style, exstyle ))
1056 xinc = GetSystemMetrics(SM_CXDLGFRAME);
1057 yinc = GetSystemMetrics(SM_CYDLGFRAME);
1059 else
1061 xinc = yinc = 0;
1062 if (HAS_THICKFRAME(style))
1064 xinc += GetSystemMetrics(SM_CXFRAME);
1065 yinc += GetSystemMetrics(SM_CYFRAME);
1067 if (style & WS_BORDER)
1069 xinc += GetSystemMetrics(SM_CXBORDER);
1070 yinc += GetSystemMetrics(SM_CYBORDER);
1073 MinMax.ptMaxSize.x += 2 * xinc;
1074 MinMax.ptMaxSize.y += 2 * yinc;
1076 lpPos = (LPINTERNALPOS)GetPropA( hwnd, atomInternalPos );
1077 if( lpPos && !EMPTYPOINT(lpPos->ptMaxPos) )
1078 CONV_POINT16TO32( &lpPos->ptMaxPos, &MinMax.ptMaxPosition );
1079 else
1081 MinMax.ptMaxPosition.x = -xinc;
1082 MinMax.ptMaxPosition.y = -yinc;
1085 SendMessageA( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
1087 /* Some sanity checks */
1089 TRACE("%ld %ld / %ld %ld / %ld %ld / %ld %ld\n",
1090 MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
1091 MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
1092 MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
1093 MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
1094 MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
1095 MinMax.ptMinTrackSize.x );
1096 MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
1097 MinMax.ptMinTrackSize.y );
1099 if (maxSize) *maxSize = MinMax.ptMaxSize;
1100 if (maxPos) *maxPos = MinMax.ptMaxPosition;
1101 if (minTrack) *minTrack = MinMax.ptMinTrackSize;
1102 if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
1105 /***********************************************************************
1106 * ShowWindowAsync (USER32.@)
1108 * doesn't wait; returns immediately.
1109 * used by threads to toggle windows in other (possibly hanging) threads
1111 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
1113 /* FIXME: does ShowWindow() return immediately ? */
1114 return ShowWindow(hwnd, cmd);
1118 /***********************************************************************
1119 * ShowWindow (USER.42)
1121 BOOL16 WINAPI ShowWindow16( HWND16 hwnd, INT16 cmd )
1123 return ShowWindow(hwnd,cmd);
1127 /***********************************************************************
1128 * ShowWindow (USER32.@)
1130 BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
1132 return USER_Driver.pShowWindow( hwnd, cmd );
1136 /***********************************************************************
1137 * GetInternalWindowPos (USER.460)
1139 UINT16 WINAPI GetInternalWindowPos16( HWND16 hwnd, LPRECT16 rectWnd,
1140 LPPOINT16 ptIcon )
1142 WINDOWPLACEMENT16 wndpl;
1143 if (GetWindowPlacement16( hwnd, &wndpl ))
1145 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
1146 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
1147 return wndpl.showCmd;
1149 return 0;
1153 /***********************************************************************
1154 * GetInternalWindowPos (USER32.@)
1156 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
1157 LPPOINT ptIcon )
1159 WINDOWPLACEMENT wndpl;
1160 if (GetWindowPlacement( hwnd, &wndpl ))
1162 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
1163 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
1164 return wndpl.showCmd;
1166 return 0;
1169 /***********************************************************************
1170 * GetWindowPlacement (USER.370)
1172 BOOL16 WINAPI GetWindowPlacement16( HWND16 hwnd, WINDOWPLACEMENT16 *wndpl )
1174 WND *pWnd = WIN_FindWndPtr( hwnd );
1175 LPINTERNALPOS lpPos;
1177 if(!pWnd ) return FALSE;
1179 lpPos = (LPINTERNALPOS)WINPOS_InitInternalPos( pWnd,
1180 *(LPPOINT)&pWnd->rectWindow.left, &pWnd->rectWindow );
1181 wndpl->length = sizeof(*wndpl);
1182 if( pWnd->dwStyle & WS_MINIMIZE )
1183 wndpl->showCmd = SW_SHOWMINIMIZED;
1184 else
1185 wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE )
1186 ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
1187 if( pWnd->flags & WIN_RESTORE_MAX )
1188 wndpl->flags = WPF_RESTORETOMAXIMIZED;
1189 else
1190 wndpl->flags = 0;
1191 wndpl->ptMinPosition = lpPos->ptIconPos;
1192 wndpl->ptMaxPosition = lpPos->ptMaxPos;
1193 wndpl->rcNormalPosition = lpPos->rectNormal;
1195 WIN_ReleaseWndPtr(pWnd);
1196 return TRUE;
1200 /***********************************************************************
1201 * GetWindowPlacement (USER32.@)
1203 * Win95:
1204 * Fails if wndpl->length of Win95 (!) apps is invalid.
1206 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *pwpl32 )
1208 if( pwpl32 )
1210 WINDOWPLACEMENT16 wpl;
1211 wpl.length = sizeof(wpl);
1212 if( GetWindowPlacement16( hwnd, &wpl ) )
1214 pwpl32->length = sizeof(*pwpl32);
1215 pwpl32->flags = wpl.flags;
1216 pwpl32->showCmd = wpl.showCmd;
1217 CONV_POINT16TO32( &wpl.ptMinPosition, &pwpl32->ptMinPosition );
1218 CONV_POINT16TO32( &wpl.ptMaxPosition, &pwpl32->ptMaxPosition );
1219 CONV_RECT16TO32( &wpl.rcNormalPosition, &pwpl32->rcNormalPosition );
1220 return TRUE;
1223 return FALSE;
1227 /***********************************************************************
1228 * WINPOS_SetPlacement
1230 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT16 *wndpl,
1231 UINT flags )
1233 WND *pWnd = WIN_FindWndPtr( hwnd );
1234 if( pWnd )
1236 LPINTERNALPOS lpPos = (LPINTERNALPOS)WINPOS_InitInternalPos( pWnd,
1237 *(LPPOINT)&pWnd->rectWindow.left, &pWnd->rectWindow );
1239 if( flags & PLACE_MIN ) lpPos->ptIconPos = wndpl->ptMinPosition;
1240 if( flags & PLACE_MAX ) lpPos->ptMaxPos = wndpl->ptMaxPosition;
1241 if( flags & PLACE_RECT) lpPos->rectNormal = wndpl->rcNormalPosition;
1243 if( pWnd->dwStyle & WS_MINIMIZE )
1245 WINPOS_ShowIconTitle( pWnd->hwndSelf, FALSE );
1246 if( wndpl->flags & WPF_SETMINPOSITION && !EMPTYPOINT(lpPos->ptIconPos))
1247 SetWindowPos( hwnd, 0, lpPos->ptIconPos.x, lpPos->ptIconPos.y,
1248 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1250 else if( pWnd->dwStyle & WS_MAXIMIZE )
1252 if( !EMPTYPOINT(lpPos->ptMaxPos) )
1253 SetWindowPos( hwnd, 0, lpPos->ptMaxPos.x, lpPos->ptMaxPos.y,
1254 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1256 else if( flags & PLACE_RECT )
1257 SetWindowPos( hwnd, 0, lpPos->rectNormal.left, lpPos->rectNormal.top,
1258 lpPos->rectNormal.right - lpPos->rectNormal.left,
1259 lpPos->rectNormal.bottom - lpPos->rectNormal.top,
1260 SWP_NOZORDER | SWP_NOACTIVATE );
1262 ShowWindow( hwnd, wndpl->showCmd );
1263 if( IsWindow(hwnd) && pWnd->dwStyle & WS_MINIMIZE )
1265 if( pWnd->dwStyle & WS_VISIBLE ) WINPOS_ShowIconTitle( pWnd->hwndSelf, TRUE );
1267 /* SDK: ...valid only the next time... */
1268 if( wndpl->flags & WPF_RESTORETOMAXIMIZED ) pWnd->flags |= WIN_RESTORE_MAX;
1270 WIN_ReleaseWndPtr(pWnd);
1271 return TRUE;
1273 return FALSE;
1277 /***********************************************************************
1278 * SetWindowPlacement (USER.371)
1280 BOOL16 WINAPI SetWindowPlacement16(HWND16 hwnd, const WINDOWPLACEMENT16 *wndpl)
1282 return WINPOS_SetPlacement( hwnd, wndpl,
1283 PLACE_MIN | PLACE_MAX | PLACE_RECT );
1286 /***********************************************************************
1287 * SetWindowPlacement (USER32.@)
1289 * Win95:
1290 * Fails if wndpl->length of Win95 (!) apps is invalid.
1292 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *pwpl32 )
1294 if( pwpl32 )
1296 WINDOWPLACEMENT16 wpl;
1298 wpl.length = sizeof(WINDOWPLACEMENT16);
1299 wpl.flags = pwpl32->flags;
1300 wpl.showCmd = pwpl32->showCmd;
1301 wpl.ptMinPosition.x = pwpl32->ptMinPosition.x;
1302 wpl.ptMinPosition.y = pwpl32->ptMinPosition.y;
1303 wpl.ptMaxPosition.x = pwpl32->ptMaxPosition.x;
1304 wpl.ptMaxPosition.y = pwpl32->ptMaxPosition.y;
1305 wpl.rcNormalPosition.left = pwpl32->rcNormalPosition.left;
1306 wpl.rcNormalPosition.top = pwpl32->rcNormalPosition.top;
1307 wpl.rcNormalPosition.right = pwpl32->rcNormalPosition.right;
1308 wpl.rcNormalPosition.bottom = pwpl32->rcNormalPosition.bottom;
1310 return WINPOS_SetPlacement( hwnd, &wpl, PLACE_MIN | PLACE_MAX | PLACE_RECT );
1312 return FALSE;
1316 /***********************************************************************
1317 * SetInternalWindowPos (USER.461)
1319 void WINAPI SetInternalWindowPos16( HWND16 hwnd, UINT16 showCmd,
1320 LPRECT16 rect, LPPOINT16 pt )
1322 if( IsWindow16(hwnd) )
1324 WINDOWPLACEMENT16 wndpl;
1325 UINT flags;
1327 wndpl.length = sizeof(wndpl);
1328 wndpl.showCmd = showCmd;
1329 wndpl.flags = flags = 0;
1331 if( pt )
1333 flags |= PLACE_MIN;
1334 wndpl.flags |= WPF_SETMINPOSITION;
1335 wndpl.ptMinPosition = *pt;
1337 if( rect )
1339 flags |= PLACE_RECT;
1340 wndpl.rcNormalPosition = *rect;
1342 WINPOS_SetPlacement( hwnd, &wndpl, flags );
1346 /***********************************************************************
1347 * AnimateWindow (USER32.@)
1348 * Shows/Hides a window with an animation
1349 * NO ANIMATION YET
1351 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
1353 FIXME("partial stub\n");
1355 /* If trying to show/hide and it's already *
1356 * shown/hidden or invalid window, fail with *
1357 * invalid parameter */
1358 if(!IsWindow(hwnd) ||
1359 (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
1360 (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
1362 SetLastError(ERROR_INVALID_PARAMETER);
1363 return FALSE;
1366 ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1368 return TRUE;
1371 /***********************************************************************
1372 * SetInternalWindowPos (USER32.@)
1374 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1375 LPRECT rect, LPPOINT pt )
1377 if( IsWindow(hwnd) )
1379 WINDOWPLACEMENT16 wndpl;
1380 UINT flags;
1382 wndpl.length = sizeof(wndpl);
1383 wndpl.showCmd = showCmd;
1384 wndpl.flags = flags = 0;
1386 if( pt )
1388 flags |= PLACE_MIN;
1389 wndpl.flags |= WPF_SETMINPOSITION;
1390 CONV_POINT32TO16( pt, &wndpl.ptMinPosition );
1392 if( rect )
1394 flags |= PLACE_RECT;
1395 CONV_RECT32TO16( rect, &wndpl.rcNormalPosition );
1397 WINPOS_SetPlacement( hwnd, &wndpl, flags );
1401 /*******************************************************************
1402 * WINPOS_SetActiveWindow
1404 * SetActiveWindow() back-end. This is the only function that
1405 * can assign active status to a window. It must be called only
1406 * for the top level windows.
1408 BOOL WINPOS_SetActiveWindow( HWND hWnd, BOOL fMouse, BOOL fChangeFocus)
1410 WND* wndPtr=0, *wndTemp;
1411 HQUEUE16 hOldActiveQueue, hNewActiveQueue;
1412 MESSAGEQUEUE *pOldActiveQueue = 0, *pNewActiveQueue = 0;
1413 WORD wIconized = 0;
1414 HWND hwndActive = 0;
1415 BOOL bRet = 0;
1417 TRACE("(%04x, %d, %d)\n", hWnd, fMouse, fChangeFocus );
1419 /* Get current active window from the active queue */
1420 if ( hActiveQueue )
1422 pOldActiveQueue = QUEUE_Lock( hActiveQueue );
1423 if ( pOldActiveQueue )
1424 hwndActive = PERQDATA_GetActiveWnd( pOldActiveQueue->pQData );
1427 if ((wndPtr = WIN_FindWndPtr(hWnd)))
1428 hWnd = wndPtr->hwndSelf; /* make it a full handle */
1430 /* paranoid checks */
1431 if( hWnd == GetDesktopWindow() || (bRet = (hWnd == hwndActive)) )
1432 goto CLEANUP_END;
1434 /* if (wndPtr && (GetFastQueue16() != wndPtr->hmemTaskQ))
1435 * return 0;
1437 hOldActiveQueue = hActiveQueue;
1439 if( (wndTemp = WIN_FindWndPtr(hwndActive)) )
1441 wIconized = HIWORD(wndTemp->dwStyle & WS_MINIMIZE);
1442 WIN_ReleaseWndPtr(wndTemp);
1444 else
1445 TRACE("no current active window.\n");
1447 /* call CBT hook chain */
1448 if (HOOK_IsHooked( WH_CBT ))
1450 CBTACTIVATESTRUCT cbt;
1451 cbt.fMouse = fMouse;
1452 cbt.hWndActive = hwndActive;
1453 if (HOOK_CallHooksA( WH_CBT, HCBT_ACTIVATE, hWnd, (LPARAM)&cbt )) goto CLEANUP_END;
1456 /* set prev active wnd to current active wnd and send notification */
1457 if ((hwndPrevActive = hwndActive) && IsWindow(hwndPrevActive))
1459 MESSAGEQUEUE *pTempActiveQueue = 0;
1461 if (!SendMessageA( hwndPrevActive, WM_NCACTIVATE, FALSE, 0 ))
1463 if (GetSysModalWindow16() != hWnd)
1464 goto CLEANUP_END;
1465 /* disregard refusal if hWnd is sysmodal */
1468 SendMessageA( hwndPrevActive, WM_ACTIVATE,
1469 MAKEWPARAM( WA_INACTIVE, wIconized ),
1470 (LPARAM)hWnd );
1472 /* check if something happened during message processing
1473 * (global active queue may have changed)
1475 pTempActiveQueue = QUEUE_Lock( hActiveQueue );
1476 if(!pTempActiveQueue)
1477 goto CLEANUP_END;
1479 hwndActive = PERQDATA_GetActiveWnd( pTempActiveQueue->pQData );
1480 QUEUE_Unlock( pTempActiveQueue );
1481 if( hwndPrevActive != hwndActive )
1482 goto CLEANUP_END;
1485 /* Set new active window in the message queue */
1486 hwndActive = hWnd;
1487 if ( wndPtr )
1489 pNewActiveQueue = QUEUE_Lock( wndPtr->hmemTaskQ );
1490 if ( pNewActiveQueue )
1491 PERQDATA_SetActiveWnd( pNewActiveQueue->pQData, hwndActive );
1493 else /* have to do this or MDI frame activation goes to hell */
1494 if( pOldActiveQueue )
1495 PERQDATA_SetActiveWnd( pOldActiveQueue->pQData, 0 );
1497 /* send palette messages */
1498 if (hWnd && SendMessageW( hWnd, WM_QUERYNEWPALETTE, 0, 0L))
1499 SendMessageW( HWND_BROADCAST, WM_PALETTEISCHANGING, (WPARAM)hWnd, 0 );
1501 /* if prev wnd is minimized redraw icon title */
1502 if( IsIconic( hwndPrevActive ) ) WINPOS_RedrawIconTitle(hwndPrevActive);
1504 /* managed windows will get ConfigureNotify event */
1505 if (wndPtr && !(wndPtr->dwStyle & WS_CHILD) && !(wndPtr->dwExStyle & WS_EX_MANAGED))
1507 /* check Z-order and bring hWnd to the top */
1508 HWND tmp = GetTopWindow(0);
1509 while (tmp && !(GetWindowLongA( tmp, GWL_STYLE ) & WS_VISIBLE))
1510 tmp = GetWindow( tmp, GW_HWNDNEXT );
1512 if( tmp != hWnd )
1513 SetWindowPos(hWnd, HWND_TOP, 0,0,0,0,
1514 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
1515 if (!IsWindow(hWnd))
1516 goto CLEANUP;
1519 /* Get a handle to the new active queue */
1520 hNewActiveQueue = wndPtr ? wndPtr->hmemTaskQ : 0;
1522 /* send WM_ACTIVATEAPP if necessary */
1523 if (hOldActiveQueue != hNewActiveQueue)
1525 HWND *list, *phwnd;
1526 DWORD old_thread = GetWindowThreadProcessId( hwndPrevActive, NULL );
1527 DWORD new_thread = GetWindowThreadProcessId( hwndActive, NULL );
1529 if ((list = WIN_ListChildren( GetDesktopWindow() )))
1531 for (phwnd = list; *phwnd; phwnd++)
1533 if (!IsWindow( *phwnd )) continue;
1534 if (GetWindowThreadProcessId( *phwnd, NULL ) == old_thread)
1535 SendMessageW( *phwnd, WM_ACTIVATEAPP, 0, new_thread );
1537 HeapFree( GetProcessHeap(), 0, list );
1540 hActiveQueue = hNewActiveQueue;
1542 if ((list = WIN_ListChildren( GetDesktopWindow() )))
1544 for (phwnd = list; *phwnd; phwnd++)
1546 if (!IsWindow( *phwnd )) continue;
1547 if (GetWindowThreadProcessId( *phwnd, NULL ) == new_thread)
1548 SendMessageW( *phwnd, WM_ACTIVATEAPP, 1, old_thread );
1550 HeapFree( GetProcessHeap(), 0, list );
1553 if (hWnd && !IsWindow(hWnd)) goto CLEANUP;
1556 if (hWnd)
1558 /* walk up to the first unowned window */
1559 HWND tmp = GetAncestor( hWnd, GA_ROOTOWNER );
1560 wndTemp = WIN_FindWndPtr( tmp );
1561 /* and set last active owned popup */
1562 wndTemp->hwndLastActive = hWnd;
1564 wIconized = HIWORD(wndTemp->dwStyle & WS_MINIMIZE);
1565 WIN_ReleaseWndPtr(wndTemp);
1566 SendMessageA( hWnd, WM_NCACTIVATE, TRUE, 0 );
1567 SendMessageA( hWnd, WM_ACTIVATE,
1568 MAKEWPARAM( (fMouse) ? WA_CLICKACTIVE : WA_ACTIVE, wIconized),
1569 (LPARAM)hwndPrevActive );
1570 if( !IsWindow(hWnd) ) goto CLEANUP;
1573 /* change focus if possible */
1574 if ( fChangeFocus )
1576 if ( pNewActiveQueue )
1578 HWND hOldFocus = PERQDATA_GetFocusWnd( pNewActiveQueue->pQData );
1580 if ( hOldFocus && GetAncestor( hOldFocus, GA_ROOT ) != hwndActive )
1581 FOCUS_SwitchFocus( pNewActiveQueue, hOldFocus,
1582 (wndPtr && (wndPtr->dwStyle & WS_MINIMIZE))?
1583 0 : hwndActive );
1586 if ( pOldActiveQueue &&
1587 ( !pNewActiveQueue ||
1588 pNewActiveQueue->pQData != pOldActiveQueue->pQData ) )
1590 HWND hOldFocus = PERQDATA_GetFocusWnd( pOldActiveQueue->pQData );
1591 if ( hOldFocus )
1592 FOCUS_SwitchFocus( pOldActiveQueue, hOldFocus, 0 );
1596 if( !hwndPrevActive && wndPtr )
1598 if (USER_Driver.pForceWindowRaise) USER_Driver.pForceWindowRaise( wndPtr->hwndSelf );
1601 /* if active wnd is minimized redraw icon title */
1602 if( IsIconic(hwndActive) ) WINPOS_RedrawIconTitle(hwndActive);
1604 bRet = (hWnd == hwndActive); /* Success? */
1606 CLEANUP: /* Unlock the message queues before returning */
1608 if ( pNewActiveQueue )
1609 QUEUE_Unlock( pNewActiveQueue );
1611 CLEANUP_END:
1613 if ( pOldActiveQueue )
1614 QUEUE_Unlock( pOldActiveQueue );
1616 WIN_ReleaseWndPtr(wndPtr);
1617 return bRet;
1620 /*******************************************************************
1621 * WINPOS_ActivateOtherWindow
1623 * Activates window other than pWnd.
1625 BOOL WINPOS_ActivateOtherWindow(HWND hwnd)
1627 BOOL bRet = 0;
1628 WND *pWnd;
1629 HWND hwndActive = 0;
1630 HWND hwndTo = 0;
1631 HWND owner;
1633 /* Get current active window from the active queue */
1634 if ( hActiveQueue )
1636 MESSAGEQUEUE *pActiveQueue = QUEUE_Lock( hActiveQueue );
1637 if ( pActiveQueue )
1639 hwndActive = PERQDATA_GetActiveWnd( pActiveQueue->pQData );
1640 QUEUE_Unlock( pActiveQueue );
1644 pWnd = WIN_FindWndPtr( hwnd );
1645 hwnd = pWnd->hwndSelf;
1647 if( hwnd == hwndPrevActive )
1648 hwndPrevActive = 0;
1650 if( hwndActive != hwnd &&
1651 ( hwndActive || QUEUE_IsExitingQueue(pWnd->hmemTaskQ)) )
1653 WIN_ReleaseWndPtr( pWnd );
1654 return 0;
1657 owner = GetWindow( hwnd, GW_OWNER );
1658 if( !(pWnd->dwStyle & WS_POPUP) || !owner ||
1659 !WINPOS_CanActivate((hwndTo = GetAncestor( owner, GA_ROOT ))) )
1661 HWND tmp = GetAncestor( hwnd, GA_ROOT );
1662 hwndTo = hwndPrevActive;
1664 while( !WINPOS_CanActivate(hwndTo) )
1666 /* by now owned windows should've been taken care of */
1667 tmp = hwndTo = GetWindow( tmp, GW_HWNDNEXT );
1668 if( !hwndTo ) break;
1671 WIN_ReleaseWndPtr( pWnd );
1673 bRet = WINPOS_SetActiveWindow( hwndTo, FALSE, TRUE );
1675 hwndPrevActive = 0;
1676 return bRet;
1679 /*******************************************************************
1680 * WINPOS_ChangeActiveWindow
1683 BOOL WINPOS_ChangeActiveWindow( HWND hWnd, BOOL mouseMsg )
1685 WND *wndPtr;
1686 BOOL retvalue;
1687 HWND hwndActive = 0;
1689 /* Get current active window from the active queue */
1690 if ( hActiveQueue )
1692 MESSAGEQUEUE *pActiveQueue = QUEUE_Lock( hActiveQueue );
1693 if ( pActiveQueue )
1695 hwndActive = PERQDATA_GetActiveWnd( pActiveQueue->pQData );
1696 QUEUE_Unlock( pActiveQueue );
1700 if (!hWnd)
1701 return WINPOS_SetActiveWindow( 0, mouseMsg, TRUE );
1703 if (!(wndPtr = WIN_FindWndPtr(hWnd))) return FALSE;
1704 hWnd = wndPtr->hwndSelf;
1706 /* child windows get WM_CHILDACTIVATE message */
1707 if( (wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) == WS_CHILD )
1709 WIN_ReleaseWndPtr(wndPtr);
1710 return SendMessageA(hWnd, WM_CHILDACTIVATE, 0, 0L);
1713 if( hWnd == hwndActive )
1715 retvalue = FALSE;
1716 goto end;
1719 if( !WINPOS_SetActiveWindow(hWnd ,mouseMsg ,TRUE) )
1721 retvalue = FALSE;
1722 goto end;
1725 retvalue = TRUE;
1726 end:
1727 WIN_ReleaseWndPtr(wndPtr);
1728 return retvalue;
1732 /***********************************************************************
1733 * WINPOS_SendNCCalcSize
1735 * Send a WM_NCCALCSIZE message to a window.
1736 * All parameters are read-only except newClientRect.
1737 * oldWindowRect, oldClientRect and winpos must be non-NULL only
1738 * when calcValidRect is TRUE.
1740 LONG WINPOS_SendNCCalcSize( HWND hwnd, BOOL calcValidRect,
1741 RECT *newWindowRect, RECT *oldWindowRect,
1742 RECT *oldClientRect, WINDOWPOS *winpos,
1743 RECT *newClientRect )
1745 NCCALCSIZE_PARAMS params;
1746 WINDOWPOS winposCopy;
1747 LONG result;
1749 params.rgrc[0] = *newWindowRect;
1750 if (calcValidRect)
1752 winposCopy = *winpos;
1753 params.rgrc[1] = *oldWindowRect;
1754 params.rgrc[2] = *oldClientRect;
1755 params.lppos = &winposCopy;
1757 result = SendMessageA( hwnd, WM_NCCALCSIZE, calcValidRect,
1758 (LPARAM)&params );
1759 TRACE("%d,%d-%d,%d\n",
1760 params.rgrc[0].left, params.rgrc[0].top,
1761 params.rgrc[0].right, params.rgrc[0].bottom );
1763 /* If the application send back garbage, ignore it */
1764 if (params.rgrc[0].left <= params.rgrc[0].right && params.rgrc[0].top <= params.rgrc[0].bottom)
1765 *newClientRect = params.rgrc[0];
1767 return result;
1771 /***********************************************************************
1772 * WINPOS_HandleWindowPosChanging16
1774 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1776 LONG WINPOS_HandleWindowPosChanging16( HWND hwnd, WINDOWPOS16 *winpos )
1778 POINT maxSize, minTrack;
1779 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
1781 if (winpos->flags & SWP_NOSIZE) return 0;
1782 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1784 WINPOS_GetMinMaxInfo( hwnd, &maxSize, NULL, &minTrack, NULL );
1785 if (maxSize.x < winpos->cx) winpos->cx = maxSize.x;
1786 if (maxSize.y < winpos->cy) winpos->cy = maxSize.y;
1787 if (!(style & WS_MINIMIZE))
1789 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1790 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1793 return 0;
1797 /***********************************************************************
1798 * WINPOS_HandleWindowPosChanging
1800 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1802 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1804 POINT maxSize, minTrack;
1805 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
1807 if (winpos->flags & SWP_NOSIZE) return 0;
1808 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1810 WINPOS_GetMinMaxInfo( hwnd, &maxSize, NULL, &minTrack, NULL );
1811 winpos->cx = min( winpos->cx, maxSize.x );
1812 winpos->cy = min( winpos->cy, maxSize.y );
1813 if (!(style & WS_MINIMIZE))
1815 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1816 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1819 return 0;
1822 /***********************************************************************
1823 * SetWindowPos (USER.232)
1825 BOOL16 WINAPI SetWindowPos16( HWND16 hwnd, HWND16 hwndInsertAfter,
1826 INT16 x, INT16 y, INT16 cx, INT16 cy, WORD flags)
1828 return SetWindowPos(hwnd,(INT)(INT16)hwndInsertAfter,x,y,cx,cy,flags);
1831 /***********************************************************************
1832 * SetWindowPos (USER32.@)
1834 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
1835 INT x, INT y, INT cx, INT cy, UINT flags )
1837 WINDOWPOS winpos;
1839 winpos.hwnd = hwnd;
1840 winpos.hwndInsertAfter = hwndInsertAfter;
1841 winpos.x = x;
1842 winpos.y = y;
1843 winpos.cx = cx;
1844 winpos.cy = cy;
1845 winpos.flags = flags;
1846 return USER_Driver.pSetWindowPos( &winpos );
1850 /***********************************************************************
1851 * BeginDeferWindowPos (USER.259)
1853 HDWP16 WINAPI BeginDeferWindowPos16( INT16 count )
1855 return BeginDeferWindowPos( count );
1859 /***********************************************************************
1860 * BeginDeferWindowPos (USER32.@)
1862 HDWP WINAPI BeginDeferWindowPos( INT count )
1864 HDWP handle;
1865 DWP *pDWP;
1867 if (count < 0)
1869 SetLastError(ERROR_INVALID_PARAMETER);
1870 return 0;
1872 /* Windows allows zero count, in which case it allocates context for 8 moves */
1873 if (count == 0) count = 8;
1875 handle = USER_HEAP_ALLOC( sizeof(DWP) + (count-1)*sizeof(WINDOWPOS) );
1876 if (!handle) return 0;
1877 pDWP = (DWP *) USER_HEAP_LIN_ADDR( handle );
1878 pDWP->actualCount = 0;
1879 pDWP->suggestedCount = count;
1880 pDWP->valid = TRUE;
1881 pDWP->wMagic = DWP_MAGIC;
1882 pDWP->hwndParent = 0;
1883 return handle;
1887 /***********************************************************************
1888 * DeferWindowPos (USER.260)
1890 HDWP16 WINAPI DeferWindowPos16( HDWP16 hdwp, HWND16 hwnd, HWND16 hwndAfter,
1891 INT16 x, INT16 y, INT16 cx, INT16 cy,
1892 UINT16 flags )
1894 return DeferWindowPos( hdwp, hwnd, (INT)(INT16)hwndAfter,
1895 x, y, cx, cy, flags );
1899 /***********************************************************************
1900 * DeferWindowPos (USER32.@)
1902 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
1903 INT x, INT y, INT cx, INT cy,
1904 UINT flags )
1906 DWP *pDWP;
1907 int i;
1908 HDWP newhdwp = hdwp,retvalue;
1909 /* HWND parent; */
1910 WND *pWnd;
1912 hwnd = WIN_GetFullHandle( hwnd );
1913 if (hwnd == GetDesktopWindow()) return 0;
1915 if (!(pDWP = USER_HEAP_LIN_ADDR( hdwp ))) return 0;
1917 if (!(pWnd = WIN_FindWndPtr( hwnd ))) return 0;
1919 /* Numega Bounds Checker Demo dislikes the following code.
1920 In fact, I've not been able to find any "same parent" requirement in any docu
1921 [AM 980509]
1923 #if 0
1924 /* All the windows of a DeferWindowPos() must have the same parent */
1925 parent = pWnd->parent->hwndSelf;
1926 if (pDWP->actualCount == 0) pDWP->hwndParent = parent;
1927 else if (parent != pDWP->hwndParent)
1929 USER_HEAP_FREE( hdwp );
1930 retvalue = 0;
1931 goto END;
1933 #endif
1935 for (i = 0; i < pDWP->actualCount; i++)
1937 if (pDWP->winPos[i].hwnd == hwnd)
1939 /* Merge with the other changes */
1940 if (!(flags & SWP_NOZORDER))
1942 pDWP->winPos[i].hwndInsertAfter = hwndAfter;
1944 if (!(flags & SWP_NOMOVE))
1946 pDWP->winPos[i].x = x;
1947 pDWP->winPos[i].y = y;
1949 if (!(flags & SWP_NOSIZE))
1951 pDWP->winPos[i].cx = cx;
1952 pDWP->winPos[i].cy = cy;
1954 pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
1955 SWP_NOZORDER | SWP_NOREDRAW |
1956 SWP_NOACTIVATE | SWP_NOCOPYBITS|
1957 SWP_NOOWNERZORDER);
1958 pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
1959 SWP_FRAMECHANGED);
1960 retvalue = hdwp;
1961 goto END;
1964 if (pDWP->actualCount >= pDWP->suggestedCount)
1966 newhdwp = USER_HEAP_REALLOC( hdwp,
1967 sizeof(DWP) + pDWP->suggestedCount*sizeof(WINDOWPOS) );
1968 if (!newhdwp)
1970 retvalue = 0;
1971 goto END;
1973 pDWP = (DWP *) USER_HEAP_LIN_ADDR( newhdwp );
1974 pDWP->suggestedCount++;
1976 pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
1977 pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
1978 pDWP->winPos[pDWP->actualCount].x = x;
1979 pDWP->winPos[pDWP->actualCount].y = y;
1980 pDWP->winPos[pDWP->actualCount].cx = cx;
1981 pDWP->winPos[pDWP->actualCount].cy = cy;
1982 pDWP->winPos[pDWP->actualCount].flags = flags;
1983 pDWP->actualCount++;
1984 retvalue = newhdwp;
1985 END:
1986 WIN_ReleaseWndPtr(pWnd);
1987 return retvalue;
1991 /***********************************************************************
1992 * EndDeferWindowPos (USER.261)
1994 BOOL16 WINAPI EndDeferWindowPos16( HDWP16 hdwp )
1996 return EndDeferWindowPos( hdwp );
2000 /***********************************************************************
2001 * EndDeferWindowPos (USER32.@)
2003 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
2005 DWP *pDWP;
2006 WINDOWPOS *winpos;
2007 BOOL res = TRUE;
2008 int i;
2010 pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
2011 if (!pDWP) return FALSE;
2012 for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
2014 if (!(res = USER_Driver.pSetWindowPos( winpos ))) break;
2016 USER_HEAP_FREE( hdwp );
2017 return res;
2021 /***********************************************************************
2022 * TileChildWindows (USER.199)
2024 void WINAPI TileChildWindows16( HWND16 parent, WORD action )
2026 FIXME("(%04x, %d): stub\n", parent, action);
2029 /***********************************************************************
2030 * CascadeChildWindows (USER.198)
2032 void WINAPI CascadeChildWindows16( HWND16 parent, WORD action )
2034 FIXME("(%04x, %d): stub\n", parent, action);
2037 /***********************************************************************
2038 * SetProgmanWindow (USER32.@)
2040 HRESULT WINAPI SetProgmanWindow ( HWND hwnd )
2042 hGlobalProgmanWindow = hwnd;
2043 return hGlobalProgmanWindow;
2046 /***********************************************************************
2047 * GetProgmanWindow (USER32.@)
2049 HRESULT WINAPI GetProgmanWindow ( )
2051 return hGlobalProgmanWindow;
2054 /***********************************************************************
2055 * SetShellWindowEx (USER32.@)
2056 * hwndProgman = Progman[Program Manager]
2057 * |-> SHELLDLL_DefView
2058 * hwndListView = | |-> SysListView32
2059 * | | |-> tooltips_class32
2060 * | |
2061 * | |-> SysHeader32
2062 * |
2063 * |-> ProxyTarget
2065 HRESULT WINAPI SetShellWindowEx ( HWND hwndProgman, HWND hwndListView )
2067 FIXME("0x%08x 0x%08x stub\n",hwndProgman ,hwndListView );
2068 hGlobalShellWindow = hwndProgman;
2069 return hGlobalShellWindow;
2073 /***********************************************************************
2074 * SetTaskmanWindow (USER32.@)
2075 * NOTES
2076 * hwnd = MSTaskSwWClass
2077 * |-> SysTabControl32
2079 HRESULT WINAPI SetTaskmanWindow ( HWND hwnd )
2081 hGlobalTaskmanWindow = hwnd;
2082 return hGlobalTaskmanWindow;
2085 /***********************************************************************
2086 * GetTaskmanWindow (USER32.@)
2088 HRESULT WINAPI GetTaskmanWindow ( )
2090 return hGlobalTaskmanWindow;