2 * Window position related functions.
4 * Copyright 1993, 1994, 1995 Alexandre Julliard
5 * 1995, 1996, 1999 Alex Korobka
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.
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.
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
23 #include "wine/port.h"
33 #include "wine/winuser16.h"
34 #include "wine/server.h"
36 #include "user_private.h"
40 #include "nonclient.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(win);
45 #define HAS_DLGFRAME(style,exStyle) \
46 (((exStyle) & WS_EX_DLGMODALFRAME) || \
47 (((style) & WS_DLGFRAME) && !((style) & WS_BORDER)))
49 #define HAS_THICKFRAME(style) \
50 (((style) & WS_THICKFRAME) && \
51 !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
53 #define EMPTYPOINT(pt) ((*(LONG*)&(pt)) == -1)
55 #define PLACE_MIN 0x0001
56 #define PLACE_MAX 0x0002
57 #define PLACE_RECT 0x0004
60 #define DWP_MAGIC ((INT)('W' | ('P' << 8) | ('O' << 16) | ('S' << 24)))
78 } INTERNALPOS, *LPINTERNALPOS;
80 /* ----- internal variables ----- */
82 static LPCSTR atomInternalPos;
85 /***********************************************************************
86 * WINPOS_CreateInternalPosAtom
88 BOOL WINPOS_CreateInternalPosAtom()
91 atomInternalPos = (LPCSTR)(DWORD)GlobalAddAtomA(str);
92 return (atomInternalPos) ? TRUE : FALSE;
95 /***********************************************************************
96 * WINPOS_CheckInternalPos
98 * Called when a window is destroyed.
100 void WINPOS_CheckInternalPos( HWND hwnd )
102 LPINTERNALPOS lpPos = (LPINTERNALPOS) GetPropA( hwnd, atomInternalPos );
106 if( IsWindow(lpPos->hwndIconTitle) )
107 DestroyWindow( lpPos->hwndIconTitle );
108 HeapFree( GetProcessHeap(), 0, lpPos );
112 /***********************************************************************
113 * ArrangeIconicWindows (USER32.@)
115 UINT WINAPI ArrangeIconicWindows( HWND parent )
119 INT x, y, xspacing, yspacing;
121 GetClientRect( parent, &rectParent );
123 y = rectParent.bottom;
124 xspacing = GetSystemMetrics(SM_CXICONSPACING);
125 yspacing = GetSystemMetrics(SM_CYICONSPACING);
127 hwndChild = GetWindow( parent, GW_CHILD );
130 if( IsIconic( hwndChild ) )
132 WINPOS_ShowIconTitle( hwndChild, FALSE );
134 SetWindowPos( hwndChild, 0, x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
135 y - yspacing - GetSystemMetrics(SM_CYICON)/2, 0, 0,
136 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
137 if( IsWindow(hwndChild) )
138 WINPOS_ShowIconTitle(hwndChild , TRUE );
140 if (x <= rectParent.right - xspacing) x += xspacing;
147 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
153 /***********************************************************************
154 * SwitchToThisWindow (USER32.@)
156 void WINAPI SwitchToThisWindow( HWND hwnd, BOOL restore )
158 ShowWindow( hwnd, restore ? SW_RESTORE : SW_SHOWMINIMIZED );
162 /***********************************************************************
163 * GetWindowRect (USER32.@)
165 BOOL WINAPI GetWindowRect( HWND hwnd, LPRECT rect )
167 BOOL ret = WIN_GetRectangles( hwnd, rect, NULL );
170 MapWindowPoints( GetAncestor( hwnd, GA_PARENT ), 0, (POINT *)rect, 2 );
171 TRACE( "hwnd %p (%ld,%ld)-(%ld,%ld)\n",
172 hwnd, rect->left, rect->top, rect->right, rect->bottom);
178 /***********************************************************************
179 * GetWindowRgn (USER32.@)
181 int WINAPI GetWindowRgn ( HWND hwnd, HRGN hrgn )
191 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
193 SetLastError( ERROR_OUTOFMEMORY );
196 SERVER_START_REQ( get_window_region )
199 wine_server_set_reply( req, data->Buffer, size );
200 if (!(status = wine_server_call( req )))
202 size_t reply_size = wine_server_reply_size( reply );
205 data->rdh.dwSize = sizeof(data->rdh);
206 data->rdh.iType = RDH_RECTANGLES;
207 data->rdh.nCount = reply_size / sizeof(RECT);
208 data->rdh.nRgnSize = reply_size;
209 win_rgn = ExtCreateRegion( NULL, size, data );
212 else size = reply->total_size;
215 HeapFree( GetProcessHeap(), 0, data );
216 } while (status == STATUS_BUFFER_OVERFLOW);
218 if (status) SetLastError( RtlNtStatusToDosError(status) );
221 nRet = CombineRgn( hrgn, win_rgn, 0, RGN_COPY );
222 DeleteObject( win_rgn );
228 /***********************************************************************
229 * SetWindowRgn (USER32.@)
231 int WINAPI SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL bRedraw )
233 static const RECT empty_rect;
241 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
242 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
243 if (!GetRegionData( hrgn, size, data ))
245 HeapFree( GetProcessHeap(), 0, data );
248 SERVER_START_REQ( set_window_region )
251 if (data->rdh.nCount)
252 wine_server_add_data( req, data->Buffer, data->rdh.nCount * sizeof(RECT) );
254 wine_server_add_data( req, &empty_rect, sizeof(empty_rect) );
255 ret = !wine_server_call_err( req );
259 else /* clear existing region */
261 SERVER_START_REQ( set_window_region )
264 ret = !wine_server_call_err( req );
269 if (ret && USER_Driver.pSetWindowRgn)
270 ret = USER_Driver.pSetWindowRgn( hwnd, hrgn, bRedraw );
272 if (ret && bRedraw) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_INVALIDATE | RDW_ERASE );
277 /***********************************************************************
278 * GetClientRect (USER32.@)
280 BOOL WINAPI GetClientRect( HWND hwnd, LPRECT rect )
284 rect->right = rect->bottom = 0;
285 if ((ret = WIN_GetRectangles( hwnd, NULL, rect )))
287 rect->right -= rect->left;
288 rect->bottom -= rect->top;
290 rect->left = rect->top = 0;
295 /*******************************************************************
296 * ClientToScreen (USER32.@)
298 BOOL WINAPI ClientToScreen( HWND hwnd, LPPOINT lppnt )
300 MapWindowPoints( hwnd, 0, lppnt, 1 );
305 /*******************************************************************
306 * ScreenToClient (USER32.@)
308 BOOL WINAPI ScreenToClient( HWND hwnd, LPPOINT lppnt )
310 MapWindowPoints( 0, hwnd, lppnt, 1 );
315 /***********************************************************************
316 * list_children_from_point
318 * Get the list of children that can contain point from the server.
319 * Point is in screen coordinates.
320 * Returned list must be freed by caller.
322 static HWND *list_children_from_point( HWND hwnd, POINT pt )
331 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
333 SERVER_START_REQ( get_window_children_from_point )
338 wine_server_set_reply( req, list, (size-1) * sizeof(HWND) );
339 if (!wine_server_call( req )) count = reply->count;
342 if (count && count < size)
347 HeapFree( GetProcessHeap(), 0, list );
349 size = count + 1; /* restart with a large enough buffer */
355 /***********************************************************************
356 * WINPOS_WindowFromPoint
358 * Find the window and hittest for a given point.
360 HWND WINPOS_WindowFromPoint( HWND hwndScope, POINT pt, INT *hittest )
365 if (!hwndScope) hwndScope = GetDesktopWindow();
367 *hittest = HTNOWHERE;
369 if (!(list = list_children_from_point( hwndScope, pt ))) return 0;
371 /* now determine the hittest */
373 for (i = 0; list[i]; i++)
375 LONG style = GetWindowLongW( list[i], GWL_STYLE );
377 /* If window is minimized or disabled, return at once */
378 if (style & WS_MINIMIZE)
380 *hittest = HTCAPTION;
383 if (style & WS_DISABLED)
388 /* Send WM_NCCHITTEST (if same thread) */
389 if (!WIN_IsCurrentThread( list[i] ))
394 res = SendMessageA( list[i], WM_NCHITTEST, 0, MAKELONG(pt.x,pt.y) );
395 if (res != HTTRANSPARENT)
397 *hittest = res; /* Found the window */
400 /* continue search with next window in z-order */
403 HeapFree( GetProcessHeap(), 0, list );
404 TRACE( "scope %p (%ld,%ld) returning %p\n", hwndScope, pt.x, pt.y, ret );
409 /*******************************************************************
410 * WindowFromPoint (USER32.@)
412 HWND WINAPI WindowFromPoint( POINT pt )
415 return WINPOS_WindowFromPoint( 0, pt, &hittest );
419 /*******************************************************************
420 * ChildWindowFromPoint (USER32.@)
422 HWND WINAPI ChildWindowFromPoint( HWND hwndParent, POINT pt )
424 return ChildWindowFromPointEx( hwndParent, pt, CWP_ALL );
427 /*******************************************************************
428 * ChildWindowFromPointEx (USER32.@)
430 HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
432 /* pt is in the client coordinates */
438 GetClientRect( hwndParent, &rect );
439 if (!PtInRect( &rect, pt )) return 0;
440 if (!(list = WIN_ListChildren( hwndParent ))) return 0;
442 for (i = 0; list[i]; i++)
444 if (!WIN_GetRectangles( list[i], &rect, NULL )) continue;
445 if (!PtInRect( &rect, pt )) continue;
446 if (uFlags & (CWP_SKIPINVISIBLE|CWP_SKIPDISABLED))
448 LONG style = GetWindowLongW( list[i], GWL_STYLE );
449 if ((uFlags & CWP_SKIPINVISIBLE) && !(style & WS_VISIBLE)) continue;
450 if ((uFlags & CWP_SKIPDISABLED) && (style & WS_DISABLED)) continue;
452 if (uFlags & CWP_SKIPTRANSPARENT)
454 if (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TRANSPARENT) continue;
459 HeapFree( GetProcessHeap(), 0, list );
460 if (!retvalue) retvalue = hwndParent;
465 /*******************************************************************
466 * WINPOS_GetWinOffset
468 * Calculate the offset between the origin of the two windows. Used
469 * to implement MapWindowPoints.
471 static void WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo, POINT *offset )
475 offset->x = offset->y = 0;
477 /* Translate source window origin to screen coords */
480 HWND hwnd = hwndFrom;
484 if (hwnd == hwndTo) return;
485 if (!(wndPtr = WIN_GetPtr( hwnd )))
487 ERR( "bad hwndFrom = %p\n", hwnd );
490 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
491 offset->x += wndPtr->rectClient.left;
492 offset->y += wndPtr->rectClient.top;
493 hwnd = wndPtr->parent;
494 WIN_ReleasePtr( wndPtr );
498 /* Translate origin to destination window coords */
505 if (!(wndPtr = WIN_GetPtr( hwnd )))
507 ERR( "bad hwndTo = %p\n", hwnd );
510 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
511 offset->x -= wndPtr->rectClient.left;
512 offset->y -= wndPtr->rectClient.top;
513 hwnd = wndPtr->parent;
514 WIN_ReleasePtr( wndPtr );
519 other_process: /* one of the parents may belong to another process, do it the hard way */
520 offset->x = offset->y = 0;
521 SERVER_START_REQ( get_windows_offset )
523 req->from = hwndFrom;
525 if (!wine_server_call( req ))
527 offset->x = reply->x;
528 offset->y = reply->y;
535 /*******************************************************************
536 * MapWindowPoints (USER.258)
538 void WINAPI MapWindowPoints16( HWND16 hwndFrom, HWND16 hwndTo,
539 LPPOINT16 lppt, UINT16 count )
543 WINPOS_GetWinOffset( WIN_Handle32(hwndFrom), WIN_Handle32(hwndTo), &offset );
553 /*******************************************************************
554 * MapWindowPoints (USER32.@)
556 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
560 WINPOS_GetWinOffset( hwndFrom, hwndTo, &offset );
567 return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
571 /***********************************************************************
572 * IsIconic (USER32.@)
574 BOOL WINAPI IsIconic(HWND hWnd)
576 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
580 /***********************************************************************
581 * IsZoomed (USER32.@)
583 BOOL WINAPI IsZoomed(HWND hWnd)
585 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
589 /*******************************************************************
590 * AllowSetForegroundWindow (USER32.@)
592 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
594 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
595 * implemented, then fix this function. */
600 /*******************************************************************
601 * LockSetForegroundWindow (USER32.@)
603 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
605 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
606 * implemented, then fix this function. */
611 /***********************************************************************
612 * BringWindowToTop (USER32.@)
614 BOOL WINAPI BringWindowToTop( HWND hwnd )
616 return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
620 /***********************************************************************
621 * MoveWindow (USER32.@)
623 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
626 int flags = SWP_NOZORDER | SWP_NOACTIVATE;
627 if (!repaint) flags |= SWP_NOREDRAW;
628 TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
629 return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
632 /***********************************************************************
633 * WINPOS_InitInternalPos
635 static LPINTERNALPOS WINPOS_InitInternalPos( WND* wnd, POINT pt, const RECT *restoreRect )
637 LPINTERNALPOS lpPos = (LPINTERNALPOS) GetPropA( wnd->hwndSelf,
641 /* this happens when the window is minimized/maximized
642 * for the first time (rectWindow is not adjusted yet) */
644 lpPos = HeapAlloc( GetProcessHeap(), 0, sizeof(INTERNALPOS) );
645 if( !lpPos ) return NULL;
647 SetPropA( wnd->hwndSelf, atomInternalPos, (HANDLE)lpPos );
648 lpPos->hwndIconTitle = 0; /* defer until needs to be shown */
649 lpPos->rectNormal.left = wnd->rectWindow.left;
650 lpPos->rectNormal.top = wnd->rectWindow.top;
651 lpPos->rectNormal.right = wnd->rectWindow.right;
652 lpPos->rectNormal.bottom = wnd->rectWindow.bottom;
653 lpPos->ptIconPos.x = lpPos->ptIconPos.y = -1;
654 lpPos->ptMaxPos.x = lpPos->ptMaxPos.y = -1;
657 if( wnd->dwStyle & WS_MINIMIZE )
659 lpPos->ptIconPos.x = pt.x;
660 lpPos->ptIconPos.y = pt.y;
662 else if( wnd->dwStyle & WS_MAXIMIZE )
664 lpPos->ptMaxPos.x = pt.x;
665 lpPos->ptMaxPos.y = pt.y;
667 else if( restoreRect )
669 lpPos->rectNormal.left = restoreRect->left;
670 lpPos->rectNormal.top = restoreRect->top;
671 lpPos->rectNormal.right = restoreRect->right;
672 lpPos->rectNormal.bottom = restoreRect->bottom;
677 /***********************************************************************
678 * WINPOS_RedrawIconTitle
680 BOOL WINPOS_RedrawIconTitle( HWND hWnd )
682 LPINTERNALPOS lpPos = (LPINTERNALPOS)GetPropA( hWnd, atomInternalPos );
685 if( lpPos->hwndIconTitle )
687 SendMessageA( lpPos->hwndIconTitle, WM_SHOWWINDOW, TRUE, 0);
688 InvalidateRect( lpPos->hwndIconTitle, NULL, TRUE );
695 /***********************************************************************
696 * WINPOS_ShowIconTitle
698 BOOL WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
700 LPINTERNALPOS lpPos = (LPINTERNALPOS)GetPropA( hwnd, atomInternalPos );
702 if( lpPos && !(GetWindowLongA( hwnd, GWL_EXSTYLE) & WS_EX_MANAGED))
704 HWND title = lpPos->hwndIconTitle;
706 TRACE("%p %i\n", hwnd, (bShow != 0) );
709 lpPos->hwndIconTitle = title = ICONTITLE_Create( hwnd );
712 if (!IsWindowVisible(title))
714 SendMessageA( title, WM_SHOWWINDOW, TRUE, 0 );
715 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
716 SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
719 else ShowWindow( title, SW_HIDE );
724 /*******************************************************************
725 * WINPOS_GetMinMaxInfo
727 * Get the minimized and maximized information for a window.
729 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
730 POINT *minTrack, POINT *maxTrack )
735 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
736 LONG exstyle = GetWindowLongA( hwnd, GWL_EXSTYLE );
739 /* Compute default values */
741 GetWindowRect(hwnd, &rc);
742 MinMax.ptReserved.x = rc.left;
743 MinMax.ptReserved.y = rc.top;
745 if (style & WS_CHILD)
747 if ((style & WS_CAPTION) == WS_CAPTION)
748 style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
750 GetClientRect(GetParent(hwnd), &rc);
751 AdjustWindowRectEx(&rc, style, 0, exstyle);
753 /* avoid calculating this twice */
754 style &= ~(WS_DLGFRAME | WS_BORDER | WS_THICKFRAME);
756 MinMax.ptMaxSize.x = rc.right - rc.left;
757 MinMax.ptMaxSize.y = rc.bottom - rc.top;
761 MinMax.ptMaxSize.x = GetSystemMetrics(SM_CXSCREEN);
762 MinMax.ptMaxSize.y = GetSystemMetrics(SM_CYSCREEN);
764 MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
765 MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
766 MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXSCREEN);
767 MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYSCREEN);
769 if (HAS_DLGFRAME( style, exstyle ))
771 xinc = GetSystemMetrics(SM_CXDLGFRAME);
772 yinc = GetSystemMetrics(SM_CYDLGFRAME);
777 if (HAS_THICKFRAME(style))
779 xinc += GetSystemMetrics(SM_CXFRAME);
780 yinc += GetSystemMetrics(SM_CYFRAME);
782 if (style & WS_BORDER)
784 xinc += GetSystemMetrics(SM_CXBORDER);
785 yinc += GetSystemMetrics(SM_CYBORDER);
788 MinMax.ptMaxSize.x += 2 * xinc;
789 MinMax.ptMaxSize.y += 2 * yinc;
791 lpPos = (LPINTERNALPOS)GetPropA( hwnd, atomInternalPos );
792 if( lpPos && !EMPTYPOINT(lpPos->ptMaxPos) )
794 MinMax.ptMaxPosition.x = lpPos->ptMaxPos.x;
795 MinMax.ptMaxPosition.y = lpPos->ptMaxPos.y;
799 MinMax.ptMaxPosition.x = -xinc;
800 MinMax.ptMaxPosition.y = -yinc;
803 SendMessageA( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
805 /* Some sanity checks */
807 TRACE("%ld %ld / %ld %ld / %ld %ld / %ld %ld\n",
808 MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
809 MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
810 MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
811 MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
812 MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
813 MinMax.ptMinTrackSize.x );
814 MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
815 MinMax.ptMinTrackSize.y );
817 if (maxSize) *maxSize = MinMax.ptMaxSize;
818 if (maxPos) *maxPos = MinMax.ptMaxPosition;
819 if (minTrack) *minTrack = MinMax.ptMinTrackSize;
820 if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
823 /***********************************************************************
824 * ShowWindowAsync (USER32.@)
826 * doesn't wait; returns immediately.
827 * used by threads to toggle windows in other (possibly hanging) threads
829 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
833 if (is_broadcast(hwnd))
835 SetLastError( ERROR_INVALID_PARAMETER );
839 if ((full_handle = WIN_IsCurrentThread( hwnd )))
841 if (USER_Driver.pShowWindow)
842 return USER_Driver.pShowWindow( full_handle, cmd );
845 return SendNotifyMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
849 /***********************************************************************
850 * ShowWindow (USER32.@)
852 BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
856 if (is_broadcast(hwnd))
858 SetLastError( ERROR_INVALID_PARAMETER );
861 if ((full_handle = WIN_IsCurrentThread( hwnd )))
863 if (USER_Driver.pShowWindow)
864 return USER_Driver.pShowWindow( full_handle, cmd );
867 return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
871 /***********************************************************************
872 * GetInternalWindowPos (USER32.@)
874 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
877 WINDOWPLACEMENT wndpl;
878 if (GetWindowPlacement( hwnd, &wndpl ))
880 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
881 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
882 return wndpl.showCmd;
888 /***********************************************************************
889 * GetWindowPlacement (USER32.@)
892 * Fails if wndpl->length of Win95 (!) apps is invalid.
894 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
896 WND *pWnd = WIN_FindWndPtr( hwnd );
899 if(!pWnd ) return FALSE;
901 lpPos = WINPOS_InitInternalPos( pWnd, *(LPPOINT)&pWnd->rectWindow.left, &pWnd->rectWindow );
902 wndpl->length = sizeof(*wndpl);
903 if( pWnd->dwStyle & WS_MINIMIZE )
904 wndpl->showCmd = SW_SHOWMINIMIZED;
906 wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
907 if( pWnd->flags & WIN_RESTORE_MAX )
908 wndpl->flags = WPF_RESTORETOMAXIMIZED;
911 wndpl->ptMinPosition.x = lpPos->ptIconPos.x;
912 wndpl->ptMinPosition.y = lpPos->ptIconPos.y;
913 wndpl->ptMaxPosition.x = lpPos->ptMaxPos.x;
914 wndpl->ptMaxPosition.y = lpPos->ptMaxPos.y;
915 wndpl->rcNormalPosition.left = lpPos->rectNormal.left;
916 wndpl->rcNormalPosition.top = lpPos->rectNormal.top;
917 wndpl->rcNormalPosition.right = lpPos->rectNormal.right;
918 wndpl->rcNormalPosition.bottom = lpPos->rectNormal.bottom;
919 WIN_ReleaseWndPtr(pWnd);
924 /***********************************************************************
925 * WINPOS_SetPlacement
927 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
929 WND *pWnd = WIN_FindWndPtr( hwnd );
932 LPINTERNALPOS lpPos = (LPINTERNALPOS)WINPOS_InitInternalPos( pWnd,
933 *(LPPOINT)&pWnd->rectWindow.left, &pWnd->rectWindow );
935 if( flags & PLACE_MIN )
937 lpPos->ptIconPos.x = wndpl->ptMinPosition.x;
938 lpPos->ptIconPos.y = wndpl->ptMinPosition.y;
940 if( flags & PLACE_MAX )
942 lpPos->ptMaxPos.x = wndpl->ptMaxPosition.x;
943 lpPos->ptMaxPos.y = wndpl->ptMaxPosition.y;
945 if( flags & PLACE_RECT)
947 lpPos->rectNormal.left = wndpl->rcNormalPosition.left;
948 lpPos->rectNormal.top = wndpl->rcNormalPosition.top;
949 lpPos->rectNormal.right = wndpl->rcNormalPosition.right;
950 lpPos->rectNormal.bottom = wndpl->rcNormalPosition.bottom;
952 if( pWnd->dwStyle & WS_MINIMIZE )
954 WINPOS_ShowIconTitle( pWnd->hwndSelf, FALSE );
955 if( wndpl->flags & WPF_SETMINPOSITION && !EMPTYPOINT(lpPos->ptIconPos))
956 SetWindowPos( hwnd, 0, lpPos->ptIconPos.x, lpPos->ptIconPos.y,
957 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
959 else if( pWnd->dwStyle & WS_MAXIMIZE )
961 if( !EMPTYPOINT(lpPos->ptMaxPos) )
962 SetWindowPos( hwnd, 0, lpPos->ptMaxPos.x, lpPos->ptMaxPos.y,
963 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
965 else if( flags & PLACE_RECT )
966 SetWindowPos( hwnd, 0, lpPos->rectNormal.left, lpPos->rectNormal.top,
967 lpPos->rectNormal.right - lpPos->rectNormal.left,
968 lpPos->rectNormal.bottom - lpPos->rectNormal.top,
969 SWP_NOZORDER | SWP_NOACTIVATE );
971 ShowWindow( hwnd, wndpl->showCmd );
972 if( IsWindow(hwnd) && pWnd->dwStyle & WS_MINIMIZE )
974 if( pWnd->dwStyle & WS_VISIBLE ) WINPOS_ShowIconTitle( pWnd->hwndSelf, TRUE );
976 /* SDK: ...valid only the next time... */
977 if( wndpl->flags & WPF_RESTORETOMAXIMIZED ) pWnd->flags |= WIN_RESTORE_MAX;
979 WIN_ReleaseWndPtr(pWnd);
986 /***********************************************************************
987 * SetWindowPlacement (USER32.@)
990 * Fails if wndpl->length of Win95 (!) apps is invalid.
992 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
994 if (!wpl) return FALSE;
995 return WINPOS_SetPlacement( hwnd, wpl, PLACE_MIN | PLACE_MAX | PLACE_RECT );
999 /***********************************************************************
1000 * AnimateWindow (USER32.@)
1001 * Shows/Hides a window with an animation
1004 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
1006 FIXME("partial stub\n");
1008 /* If trying to show/hide and it's already *
1009 * shown/hidden or invalid window, fail with *
1010 * invalid parameter */
1011 if(!IsWindow(hwnd) ||
1012 (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
1013 (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
1015 SetLastError(ERROR_INVALID_PARAMETER);
1019 ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1024 /***********************************************************************
1025 * SetInternalWindowPos (USER32.@)
1027 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1028 LPRECT rect, LPPOINT pt )
1030 if( IsWindow(hwnd) )
1032 WINDOWPLACEMENT wndpl;
1035 wndpl.length = sizeof(wndpl);
1036 wndpl.showCmd = showCmd;
1037 wndpl.flags = flags = 0;
1042 wndpl.flags |= WPF_SETMINPOSITION;
1043 wndpl.ptMinPosition = *pt;
1047 flags |= PLACE_RECT;
1048 wndpl.rcNormalPosition = *rect;
1050 WINPOS_SetPlacement( hwnd, &wndpl, flags );
1055 /*******************************************************************
1056 * can_activate_window
1058 * Check if we can activate the specified window.
1060 static BOOL can_activate_window( HWND hwnd )
1064 if (!hwnd) return FALSE;
1065 style = GetWindowLongW( hwnd, GWL_STYLE );
1066 if (!(style & WS_VISIBLE)) return FALSE;
1067 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1068 return !(style & WS_DISABLED);
1072 /*******************************************************************
1073 * WINPOS_ActivateOtherWindow
1075 * Activates window other than pWnd.
1077 void WINPOS_ActivateOtherWindow(HWND hwnd)
1081 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1083 hwndTo = GetAncestor( hwndTo, GA_ROOT );
1084 if (can_activate_window( hwndTo )) goto done;
1090 if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1091 if (can_activate_window( hwndTo )) break;
1095 fg = GetForegroundWindow();
1096 TRACE("win = %p fg = %p\n", hwndTo, fg);
1097 if (!fg || (hwnd == fg))
1099 if (SetForegroundWindow( hwndTo )) return;
1101 if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1105 /***********************************************************************
1106 * WINPOS_HandleWindowPosChanging16
1108 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1110 LONG WINPOS_HandleWindowPosChanging16( HWND hwnd, WINDOWPOS16 *winpos )
1112 POINT minTrack, maxTrack;
1113 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1115 if (winpos->flags & SWP_NOSIZE) return 0;
1116 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1118 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1119 if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1120 if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1121 if (!(style & WS_MINIMIZE))
1123 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1124 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1131 /***********************************************************************
1132 * WINPOS_HandleWindowPosChanging
1134 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1136 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1138 POINT minTrack, maxTrack;
1139 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1141 if (winpos->flags & SWP_NOSIZE) return 0;
1142 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1144 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1145 if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1146 if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1147 if (!(style & WS_MINIMIZE))
1149 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1150 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1157 /***********************************************************************
1160 static void dump_winpos_flags(UINT flags)
1163 if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1164 if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1165 if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1166 if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1167 if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1168 if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1169 if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1170 if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1171 if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1172 if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1173 if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1174 if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1175 if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1177 #define DUMPED_FLAGS \
1183 SWP_FRAMECHANGED | \
1187 SWP_NOOWNERZORDER | \
1188 SWP_NOSENDCHANGING | \
1192 if(flags & ~DUMPED_FLAGS) TRACE(" %08x", flags & ~DUMPED_FLAGS);
1197 /***********************************************************************
1198 * SetWindowPos (USER32.@)
1200 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
1201 INT x, INT y, INT cx, INT cy, UINT flags )
1205 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1206 hwnd, hwndInsertAfter, x, y, cx, cy, flags);
1207 if(TRACE_ON(win)) dump_winpos_flags(flags);
1209 if (is_broadcast(hwnd))
1211 SetLastError( ERROR_INVALID_PARAMETER );
1215 winpos.hwnd = WIN_GetFullHandle(hwnd);
1216 winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
1221 winpos.flags = flags;
1222 if (WIN_IsCurrentThread( hwnd ))
1224 if (USER_Driver.pSetWindowPos)
1225 return USER_Driver.pSetWindowPos( &winpos );
1228 return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
1232 /***********************************************************************
1233 * BeginDeferWindowPos (USER32.@)
1235 HDWP WINAPI BeginDeferWindowPos( INT count )
1240 TRACE("%d\n", count);
1244 SetLastError(ERROR_INVALID_PARAMETER);
1247 /* Windows allows zero count, in which case it allocates context for 8 moves */
1248 if (count == 0) count = 8;
1250 handle = USER_HEAP_ALLOC( sizeof(DWP) + (count-1)*sizeof(WINDOWPOS) );
1251 if (!handle) return 0;
1252 pDWP = (DWP *) USER_HEAP_LIN_ADDR( handle );
1253 pDWP->actualCount = 0;
1254 pDWP->suggestedCount = count;
1256 pDWP->wMagic = DWP_MAGIC;
1257 pDWP->hwndParent = 0;
1259 TRACE("returning hdwp %p\n", handle);
1264 /***********************************************************************
1265 * DeferWindowPos (USER32.@)
1267 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
1268 INT x, INT y, INT cx, INT cy,
1273 HDWP newhdwp = hdwp,retvalue;
1275 TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1276 hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
1278 hwnd = WIN_GetFullHandle( hwnd );
1279 if (hwnd == GetDesktopWindow()) return 0;
1281 if (!(pDWP = USER_HEAP_LIN_ADDR( hdwp ))) return 0;
1285 for (i = 0; i < pDWP->actualCount; i++)
1287 if (pDWP->winPos[i].hwnd == hwnd)
1289 /* Merge with the other changes */
1290 if (!(flags & SWP_NOZORDER))
1292 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
1294 if (!(flags & SWP_NOMOVE))
1296 pDWP->winPos[i].x = x;
1297 pDWP->winPos[i].y = y;
1299 if (!(flags & SWP_NOSIZE))
1301 pDWP->winPos[i].cx = cx;
1302 pDWP->winPos[i].cy = cy;
1304 pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
1305 SWP_NOZORDER | SWP_NOREDRAW |
1306 SWP_NOACTIVATE | SWP_NOCOPYBITS|
1308 pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
1314 if (pDWP->actualCount >= pDWP->suggestedCount)
1316 newhdwp = USER_HEAP_REALLOC( hdwp,
1317 sizeof(DWP) + pDWP->suggestedCount*sizeof(WINDOWPOS) );
1323 pDWP = (DWP *) USER_HEAP_LIN_ADDR( newhdwp );
1324 pDWP->suggestedCount++;
1326 pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
1327 pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
1328 pDWP->winPos[pDWP->actualCount].x = x;
1329 pDWP->winPos[pDWP->actualCount].y = y;
1330 pDWP->winPos[pDWP->actualCount].cx = cx;
1331 pDWP->winPos[pDWP->actualCount].cy = cy;
1332 pDWP->winPos[pDWP->actualCount].flags = flags;
1333 pDWP->actualCount++;
1341 /***********************************************************************
1342 * EndDeferWindowPos (USER32.@)
1344 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
1351 TRACE("%p\n", hdwp);
1353 pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
1354 if (!pDWP) return FALSE;
1355 for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
1357 if (!USER_Driver.pSetWindowPos || !(res = USER_Driver.pSetWindowPos( winpos ))) break;
1359 USER_HEAP_FREE( hdwp );
1364 /***********************************************************************
1365 * TileChildWindows (USER.199)
1367 void WINAPI TileChildWindows16( HWND16 parent, WORD action )
1369 FIXME("(%04x, %d): stub\n", parent, action);
1372 /***********************************************************************
1373 * CascadeChildWindows (USER.198)
1375 void WINAPI CascadeChildWindows16( HWND16 parent, WORD action )
1377 FIXME("(%04x, %d): stub\n", parent, action);