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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
28 #define WIN32_NO_STATUS
34 #include "wine/winuser16.h"
35 #include "wine/server.h"
37 #include "user_private.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(win);
43 #define HAS_DLGFRAME(style,exStyle) \
44 (((exStyle) & WS_EX_DLGMODALFRAME) || \
45 (((style) & WS_DLGFRAME) && !((style) & WS_BORDER)))
47 #define HAS_THICKFRAME(style) \
48 (((style) & WS_THICKFRAME) && \
49 !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
51 #define EMPTYPOINT(pt) ((*(LONG*)&(pt)) == -1)
53 #define PLACE_MIN 0x0001
54 #define PLACE_MAX 0x0002
55 #define PLACE_RECT 0x0004
58 #define DWP_MAGIC ((INT)('W' | ('P' << 8) | ('O' << 16) | ('S' << 24)))
76 } INTERNALPOS, *LPINTERNALPOS;
78 /* ----- internal functions ----- */
80 static const WCHAR SysIP_W[] = { 'S','y','s','I','P',0 };
82 static inline INTERNALPOS *get_internal_pos( HWND hwnd )
84 return GetPropW( hwnd, SysIP_W );
87 static inline void set_internal_pos( HWND hwnd, INTERNALPOS *pos )
89 SetPropW( hwnd, SysIP_W, pos );
92 /***********************************************************************
93 * WINPOS_CheckInternalPos
95 * Called when a window is destroyed.
97 void WINPOS_CheckInternalPos( HWND hwnd )
99 LPINTERNALPOS lpPos = get_internal_pos( hwnd );
103 if( IsWindow(lpPos->hwndIconTitle) )
104 DestroyWindow( lpPos->hwndIconTitle );
105 HeapFree( GetProcessHeap(), 0, lpPos );
109 /***********************************************************************
110 * ArrangeIconicWindows (USER32.@)
112 UINT WINAPI ArrangeIconicWindows( HWND parent )
116 INT x, y, xspacing, yspacing;
118 GetClientRect( parent, &rectParent );
120 y = rectParent.bottom;
121 xspacing = GetSystemMetrics(SM_CXICONSPACING);
122 yspacing = GetSystemMetrics(SM_CYICONSPACING);
124 hwndChild = GetWindow( parent, GW_CHILD );
127 if( IsIconic( hwndChild ) )
129 WINPOS_ShowIconTitle( hwndChild, FALSE );
131 SetWindowPos( hwndChild, 0, x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
132 y - yspacing - GetSystemMetrics(SM_CYICON)/2, 0, 0,
133 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
134 if( IsWindow(hwndChild) )
135 WINPOS_ShowIconTitle(hwndChild , TRUE );
137 if (x <= rectParent.right - xspacing) x += xspacing;
144 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
150 /***********************************************************************
151 * SwitchToThisWindow (USER32.@)
153 void WINAPI SwitchToThisWindow( HWND hwnd, BOOL restore )
155 ShowWindow( hwnd, restore ? SW_RESTORE : SW_SHOWMINIMIZED );
159 /***********************************************************************
160 * GetWindowRect (USER32.@)
162 BOOL WINAPI GetWindowRect( HWND hwnd, LPRECT rect )
164 BOOL ret = WIN_GetRectangles( hwnd, rect, NULL );
167 MapWindowPoints( GetAncestor( hwnd, GA_PARENT ), 0, (POINT *)rect, 2 );
168 TRACE( "hwnd %p (%d,%d)-(%d,%d)\n",
169 hwnd, rect->left, rect->top, rect->right, rect->bottom);
175 /***********************************************************************
176 * GetWindowRgn (USER32.@)
178 int WINAPI GetWindowRgn ( HWND hwnd, HRGN hrgn )
188 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
190 SetLastError( ERROR_OUTOFMEMORY );
193 SERVER_START_REQ( get_window_region )
196 wine_server_set_reply( req, data->Buffer, size );
197 if (!(status = wine_server_call( req )))
199 size_t reply_size = wine_server_reply_size( reply );
202 data->rdh.dwSize = sizeof(data->rdh);
203 data->rdh.iType = RDH_RECTANGLES;
204 data->rdh.nCount = reply_size / sizeof(RECT);
205 data->rdh.nRgnSize = reply_size;
206 win_rgn = ExtCreateRegion( NULL, size, data );
209 else size = reply->total_size;
212 HeapFree( GetProcessHeap(), 0, data );
213 } while (status == STATUS_BUFFER_OVERFLOW);
215 if (status) SetLastError( RtlNtStatusToDosError(status) );
218 nRet = CombineRgn( hrgn, win_rgn, 0, RGN_COPY );
219 DeleteObject( win_rgn );
225 /***********************************************************************
226 * SetWindowRgn (USER32.@)
228 int WINAPI SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL bRedraw )
230 static const RECT empty_rect;
238 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
239 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
240 if (!GetRegionData( hrgn, size, data ))
242 HeapFree( GetProcessHeap(), 0, data );
245 SERVER_START_REQ( set_window_region )
248 if (data->rdh.nCount)
249 wine_server_add_data( req, data->Buffer, data->rdh.nCount * sizeof(RECT) );
251 wine_server_add_data( req, &empty_rect, sizeof(empty_rect) );
252 ret = !wine_server_call_err( req );
256 else /* clear existing region */
258 SERVER_START_REQ( set_window_region )
261 ret = !wine_server_call_err( req );
266 if (ret) ret = USER_Driver->pSetWindowRgn( hwnd, hrgn, bRedraw );
270 UINT swp_flags = SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_FRAMECHANGED;
271 if (hrgn) swp_flags |= SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE;
272 if (!bRedraw) swp_flags |= SWP_NOREDRAW;
273 SetWindowPos( hwnd, 0, 0, 0, 0, 0, swp_flags );
279 /***********************************************************************
280 * GetClientRect (USER32.@)
282 BOOL WINAPI GetClientRect( HWND hwnd, LPRECT rect )
286 if ((ret = WIN_GetRectangles( hwnd, NULL, rect )))
288 rect->right -= rect->left;
289 rect->bottom -= rect->top;
290 rect->left = rect->top = 0;
296 /*******************************************************************
297 * ClientToScreen (USER32.@)
299 BOOL WINAPI ClientToScreen( HWND hwnd, LPPOINT lppnt )
301 MapWindowPoints( hwnd, 0, lppnt, 1 );
306 /*******************************************************************
307 * ScreenToClient (USER32.@)
309 BOOL WINAPI ScreenToClient( HWND hwnd, LPPOINT lppnt )
311 MapWindowPoints( 0, hwnd, lppnt, 1 );
316 /***********************************************************************
317 * list_children_from_point
319 * Get the list of children that can contain point from the server.
320 * Point is in screen coordinates.
321 * Returned list must be freed by caller.
323 static HWND *list_children_from_point( HWND hwnd, POINT pt )
332 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
334 SERVER_START_REQ( get_window_children_from_point )
339 wine_server_set_reply( req, list, (size-1) * sizeof(HWND) );
340 if (!wine_server_call( req )) count = reply->count;
343 if (count && count < size)
348 HeapFree( GetProcessHeap(), 0, list );
350 size = count + 1; /* restart with a large enough buffer */
356 /***********************************************************************
357 * WINPOS_WindowFromPoint
359 * Find the window and hittest for a given point.
361 HWND WINPOS_WindowFromPoint( HWND hwndScope, POINT pt, INT *hittest )
366 if (!hwndScope) hwndScope = GetDesktopWindow();
368 *hittest = HTNOWHERE;
370 if (!(list = list_children_from_point( hwndScope, pt ))) return 0;
372 /* now determine the hittest */
374 for (i = 0; list[i]; i++)
376 LONG style = GetWindowLongW( list[i], GWL_STYLE );
378 /* If window is minimized or disabled, return at once */
379 if (style & WS_MINIMIZE)
381 *hittest = HTCAPTION;
384 if (style & WS_DISABLED)
389 /* Send WM_NCCHITTEST (if same thread) */
390 if (!WIN_IsCurrentThread( list[i] ))
395 res = SendMessageW( list[i], WM_NCHITTEST, 0, MAKELONG(pt.x,pt.y) );
396 if (res != HTTRANSPARENT)
398 *hittest = res; /* Found the window */
401 /* continue search with next window in z-order */
404 HeapFree( GetProcessHeap(), 0, list );
405 TRACE( "scope %p (%d,%d) returning %p\n", hwndScope, pt.x, pt.y, ret );
410 /*******************************************************************
411 * WindowFromPoint (USER32.@)
413 HWND WINAPI WindowFromPoint( POINT pt )
416 return WINPOS_WindowFromPoint( 0, pt, &hittest );
420 /*******************************************************************
421 * ChildWindowFromPoint (USER32.@)
423 HWND WINAPI ChildWindowFromPoint( HWND hwndParent, POINT pt )
425 return ChildWindowFromPointEx( hwndParent, pt, CWP_ALL );
428 /*******************************************************************
429 * ChildWindowFromPointEx (USER32.@)
431 HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
433 /* pt is in the client coordinates */
439 GetClientRect( hwndParent, &rect );
440 if (!PtInRect( &rect, pt )) return 0;
441 if (!(list = WIN_ListChildren( hwndParent ))) return hwndParent;
443 for (i = 0; list[i]; i++)
445 if (!WIN_GetRectangles( list[i], &rect, NULL )) continue;
446 if (!PtInRect( &rect, pt )) continue;
447 if (uFlags & (CWP_SKIPINVISIBLE|CWP_SKIPDISABLED))
449 LONG style = GetWindowLongW( list[i], GWL_STYLE );
450 if ((uFlags & CWP_SKIPINVISIBLE) && !(style & WS_VISIBLE)) continue;
451 if ((uFlags & CWP_SKIPDISABLED) && (style & WS_DISABLED)) continue;
453 if (uFlags & CWP_SKIPTRANSPARENT)
455 if (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TRANSPARENT) continue;
460 HeapFree( GetProcessHeap(), 0, list );
461 if (!retvalue) retvalue = hwndParent;
466 /*******************************************************************
467 * WINPOS_GetWinOffset
469 * Calculate the offset between the origin of the two windows. Used
470 * to implement MapWindowPoints.
472 static void WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo, POINT *offset )
476 offset->x = offset->y = 0;
478 /* Translate source window origin to screen coords */
481 HWND hwnd = hwndFrom;
485 if (hwnd == hwndTo) return;
486 if (!(wndPtr = WIN_GetPtr( hwnd )))
488 ERR( "bad hwndFrom = %p\n", hwnd );
491 if (wndPtr == WND_DESKTOP) break;
492 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
493 offset->x += wndPtr->rectClient.left;
494 offset->y += wndPtr->rectClient.top;
495 hwnd = wndPtr->parent;
496 WIN_ReleasePtr( wndPtr );
500 /* Translate origin to destination window coords */
507 if (!(wndPtr = WIN_GetPtr( hwnd )))
509 ERR( "bad hwndTo = %p\n", hwnd );
512 if (wndPtr == WND_DESKTOP) break;
513 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
514 offset->x -= wndPtr->rectClient.left;
515 offset->y -= wndPtr->rectClient.top;
516 hwnd = wndPtr->parent;
517 WIN_ReleasePtr( wndPtr );
522 other_process: /* one of the parents may belong to another process, do it the hard way */
523 offset->x = offset->y = 0;
524 SERVER_START_REQ( get_windows_offset )
526 req->from = hwndFrom;
528 if (!wine_server_call( req ))
530 offset->x = reply->x;
531 offset->y = reply->y;
538 /*******************************************************************
539 * MapWindowPoints (USER.258)
541 void WINAPI MapWindowPoints16( HWND16 hwndFrom, HWND16 hwndTo,
542 LPPOINT16 lppt, UINT16 count )
546 WINPOS_GetWinOffset( WIN_Handle32(hwndFrom), WIN_Handle32(hwndTo), &offset );
556 /*******************************************************************
557 * MapWindowPoints (USER32.@)
559 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
563 WINPOS_GetWinOffset( hwndFrom, hwndTo, &offset );
570 return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
574 /***********************************************************************
575 * IsIconic (USER32.@)
577 BOOL WINAPI IsIconic(HWND hWnd)
579 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
583 /***********************************************************************
584 * IsZoomed (USER32.@)
586 BOOL WINAPI IsZoomed(HWND hWnd)
588 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
592 /*******************************************************************
593 * AllowSetForegroundWindow (USER32.@)
595 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
597 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
598 * implemented, then fix this function. */
603 /*******************************************************************
604 * LockSetForegroundWindow (USER32.@)
606 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
608 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
609 * implemented, then fix this function. */
614 /***********************************************************************
615 * BringWindowToTop (USER32.@)
617 BOOL WINAPI BringWindowToTop( HWND hwnd )
619 return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
623 /***********************************************************************
624 * MoveWindow (USER32.@)
626 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
629 int flags = SWP_NOZORDER | SWP_NOACTIVATE;
630 if (!repaint) flags |= SWP_NOREDRAW;
631 TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
632 return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
635 /***********************************************************************
636 * WINPOS_InitInternalPos
638 static LPINTERNALPOS WINPOS_InitInternalPos( WND* wnd )
640 LPINTERNALPOS lpPos = get_internal_pos( wnd->hwndSelf );
643 /* this happens when the window is minimized/maximized
644 * for the first time (rectWindow is not adjusted yet) */
646 lpPos = HeapAlloc( GetProcessHeap(), 0, sizeof(INTERNALPOS) );
647 if( !lpPos ) return NULL;
649 set_internal_pos( wnd->hwndSelf, lpPos );
650 lpPos->hwndIconTitle = 0; /* defer until needs to be shown */
651 lpPos->rectNormal.left = wnd->rectWindow.left;
652 lpPos->rectNormal.top = wnd->rectWindow.top;
653 lpPos->rectNormal.right = wnd->rectWindow.right;
654 lpPos->rectNormal.bottom = wnd->rectWindow.bottom;
655 lpPos->ptIconPos.x = lpPos->ptIconPos.y = -1;
656 lpPos->ptMaxPos.x = lpPos->ptMaxPos.y = -1;
659 if( wnd->dwStyle & WS_MINIMIZE )
661 lpPos->ptIconPos.x = wnd->rectWindow.left;
662 lpPos->ptIconPos.y = wnd->rectWindow.top;
664 else if( wnd->dwStyle & WS_MAXIMIZE )
666 lpPos->ptMaxPos.x = wnd->rectWindow.left;
667 lpPos->ptMaxPos.y = wnd->rectWindow.top;
671 lpPos->rectNormal.left = wnd->rectWindow.left;
672 lpPos->rectNormal.top = wnd->rectWindow.top;
673 lpPos->rectNormal.right = wnd->rectWindow.right;
674 lpPos->rectNormal.bottom = wnd->rectWindow.bottom;
679 /***********************************************************************
680 * WINPOS_RedrawIconTitle
682 BOOL WINPOS_RedrawIconTitle( HWND hWnd )
684 LPINTERNALPOS lpPos = get_internal_pos( hWnd );
687 if( lpPos->hwndIconTitle )
689 SendMessageW( lpPos->hwndIconTitle, WM_SHOWWINDOW, TRUE, 0);
690 InvalidateRect( lpPos->hwndIconTitle, NULL, TRUE );
697 /***********************************************************************
698 * WINPOS_ShowIconTitle
700 BOOL WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
702 LPINTERNALPOS lpPos = get_internal_pos( hwnd );
704 if (lpPos && !GetPropA( hwnd, "__wine_x11_managed" ))
706 HWND title = lpPos->hwndIconTitle;
708 TRACE("%p %i\n", hwnd, (bShow != 0) );
711 lpPos->hwndIconTitle = title = ICONTITLE_Create( hwnd );
714 if (!IsWindowVisible(title))
716 SendMessageW( title, WM_SHOWWINDOW, TRUE, 0 );
717 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
718 SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
721 else ShowWindow( title, SW_HIDE );
726 /*******************************************************************
727 * WINPOS_GetMinMaxInfo
729 * Get the minimized and maximized information for a window.
731 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
732 POINT *minTrack, POINT *maxTrack )
738 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
739 LONG exstyle = GetWindowLongA( hwnd, GWL_EXSTYLE );
742 /* Compute default values */
744 GetWindowRect(hwnd, &rc);
745 MinMax.ptReserved.x = rc.left;
746 MinMax.ptReserved.y = rc.top;
748 if (style & WS_CHILD)
750 if ((style & WS_CAPTION) == WS_CAPTION)
751 style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
753 GetClientRect(GetAncestor(hwnd,GA_PARENT), &rc);
754 AdjustWindowRectEx(&rc, style, ((style & WS_POPUP) && GetMenu(hwnd)), exstyle);
756 /* avoid calculating this twice */
757 style &= ~(WS_DLGFRAME | WS_BORDER | WS_THICKFRAME);
759 MinMax.ptMaxSize.x = rc.right - rc.left;
760 MinMax.ptMaxSize.y = rc.bottom - rc.top;
764 MinMax.ptMaxSize.x = GetSystemMetrics(SM_CXSCREEN);
765 MinMax.ptMaxSize.y = GetSystemMetrics(SM_CYSCREEN);
767 MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
768 MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
769 MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXMAXTRACK);
770 MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYMAXTRACK);
772 if (HAS_DLGFRAME( style, exstyle ))
774 xinc = GetSystemMetrics(SM_CXDLGFRAME);
775 yinc = GetSystemMetrics(SM_CYDLGFRAME);
780 if (HAS_THICKFRAME(style))
782 xinc += GetSystemMetrics(SM_CXFRAME);
783 yinc += GetSystemMetrics(SM_CYFRAME);
785 if (style & WS_BORDER)
787 xinc += GetSystemMetrics(SM_CXBORDER);
788 yinc += GetSystemMetrics(SM_CYBORDER);
791 MinMax.ptMaxSize.x += 2 * xinc;
792 MinMax.ptMaxSize.y += 2 * yinc;
794 lpPos = get_internal_pos( hwnd );
795 if( lpPos && !EMPTYPOINT(lpPos->ptMaxPos) )
797 MinMax.ptMaxPosition.x = lpPos->ptMaxPos.x;
798 MinMax.ptMaxPosition.y = lpPos->ptMaxPos.y;
802 MinMax.ptMaxPosition.x = -xinc;
803 MinMax.ptMaxPosition.y = -yinc;
806 SendMessageW( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
808 /* if the app didn't change the values, adapt them for the current monitor */
810 if ((monitor = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY )))
812 MONITORINFO mon_info;
814 mon_info.cbSize = sizeof(mon_info);
815 GetMonitorInfoW( monitor, &mon_info );
817 if (MinMax.ptMaxSize.x == GetSystemMetrics(SM_CXSCREEN) + 2 * xinc &&
818 MinMax.ptMaxSize.y == GetSystemMetrics(SM_CYSCREEN) + 2 * yinc)
820 MinMax.ptMaxSize.x = (mon_info.rcWork.right - mon_info.rcWork.left) + 2 * xinc;
821 MinMax.ptMaxSize.y = (mon_info.rcWork.bottom - mon_info.rcWork.top) + 2 * yinc;
823 if (MinMax.ptMaxPosition.x == -xinc && MinMax.ptMaxPosition.y == -yinc)
825 MinMax.ptMaxPosition.x = mon_info.rcWork.left - xinc;
826 MinMax.ptMaxPosition.y = mon_info.rcWork.top - yinc;
830 /* Some sanity checks */
832 TRACE("%d %d / %d %d / %d %d / %d %d\n",
833 MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
834 MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
835 MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
836 MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
837 MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
838 MinMax.ptMinTrackSize.x );
839 MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
840 MinMax.ptMinTrackSize.y );
842 if (maxSize) *maxSize = MinMax.ptMaxSize;
843 if (maxPos) *maxPos = MinMax.ptMaxPosition;
844 if (minTrack) *minTrack = MinMax.ptMinTrackSize;
845 if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
848 /***********************************************************************
849 * ShowWindowAsync (USER32.@)
851 * doesn't wait; returns immediately.
852 * used by threads to toggle windows in other (possibly hanging) threads
854 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
858 if (is_broadcast(hwnd))
860 SetLastError( ERROR_INVALID_PARAMETER );
864 if ((full_handle = WIN_IsCurrentThread( hwnd )))
865 return USER_Driver->pShowWindow( full_handle, cmd );
867 return SendNotifyMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
871 /***********************************************************************
872 * ShowWindow (USER32.@)
874 BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
878 if (is_broadcast(hwnd))
880 SetLastError( ERROR_INVALID_PARAMETER );
883 if ((full_handle = WIN_IsCurrentThread( hwnd )))
884 return USER_Driver->pShowWindow( full_handle, cmd );
886 return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
890 /***********************************************************************
891 * GetInternalWindowPos (USER32.@)
893 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
896 WINDOWPLACEMENT wndpl;
897 if (GetWindowPlacement( hwnd, &wndpl ))
899 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
900 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
901 return wndpl.showCmd;
907 /***********************************************************************
908 * GetWindowPlacement (USER32.@)
911 * Fails if wndpl->length of Win95 (!) apps is invalid.
913 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
915 WND *pWnd = WIN_GetPtr( hwnd );
918 if (!pWnd || pWnd == WND_DESKTOP) return FALSE;
919 if (pWnd == WND_OTHER_PROCESS)
921 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
925 lpPos = WINPOS_InitInternalPos( pWnd );
926 wndpl->length = sizeof(*wndpl);
927 if( pWnd->dwStyle & WS_MINIMIZE )
928 wndpl->showCmd = SW_SHOWMINIMIZED;
930 wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
931 if( pWnd->flags & WIN_RESTORE_MAX )
932 wndpl->flags = WPF_RESTORETOMAXIMIZED;
935 wndpl->ptMinPosition.x = lpPos->ptIconPos.x;
936 wndpl->ptMinPosition.y = lpPos->ptIconPos.y;
937 wndpl->ptMaxPosition.x = lpPos->ptMaxPos.x;
938 wndpl->ptMaxPosition.y = lpPos->ptMaxPos.y;
939 wndpl->rcNormalPosition.left = lpPos->rectNormal.left;
940 wndpl->rcNormalPosition.top = lpPos->rectNormal.top;
941 wndpl->rcNormalPosition.right = lpPos->rectNormal.right;
942 wndpl->rcNormalPosition.bottom = lpPos->rectNormal.bottom;
943 WIN_ReleasePtr( pWnd );
948 /***********************************************************************
949 * WINPOS_SetPlacement
951 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
955 WND *pWnd = WIN_GetPtr( hwnd );
957 if (!pWnd || pWnd == WND_OTHER_PROCESS || pWnd == WND_DESKTOP) return FALSE;
958 lpPos = WINPOS_InitInternalPos( pWnd );
960 if( flags & PLACE_MIN )
962 lpPos->ptIconPos.x = wndpl->ptMinPosition.x;
963 lpPos->ptIconPos.y = wndpl->ptMinPosition.y;
965 if( flags & PLACE_MAX )
967 lpPos->ptMaxPos.x = wndpl->ptMaxPosition.x;
968 lpPos->ptMaxPos.y = wndpl->ptMaxPosition.y;
970 if( flags & PLACE_RECT)
972 lpPos->rectNormal.left = wndpl->rcNormalPosition.left;
973 lpPos->rectNormal.top = wndpl->rcNormalPosition.top;
974 lpPos->rectNormal.right = wndpl->rcNormalPosition.right;
975 lpPos->rectNormal.bottom = wndpl->rcNormalPosition.bottom;
978 style = pWnd->dwStyle;
979 WIN_ReleasePtr( pWnd );
981 if( style & WS_MINIMIZE )
983 WINPOS_ShowIconTitle( hwnd, FALSE );
984 if( wndpl->flags & WPF_SETMINPOSITION && !EMPTYPOINT(lpPos->ptIconPos))
985 SetWindowPos( hwnd, 0, lpPos->ptIconPos.x, lpPos->ptIconPos.y,
986 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
988 else if( style & WS_MAXIMIZE )
990 if( !EMPTYPOINT(lpPos->ptMaxPos) )
991 SetWindowPos( hwnd, 0, lpPos->ptMaxPos.x, lpPos->ptMaxPos.y,
992 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
994 else if( flags & PLACE_RECT )
995 SetWindowPos( hwnd, 0, lpPos->rectNormal.left, lpPos->rectNormal.top,
996 lpPos->rectNormal.right - lpPos->rectNormal.left,
997 lpPos->rectNormal.bottom - lpPos->rectNormal.top,
998 SWP_NOZORDER | SWP_NOACTIVATE );
1000 ShowWindow( hwnd, wndpl->showCmd );
1002 if (IsIconic( hwnd ))
1004 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE) WINPOS_ShowIconTitle( hwnd, TRUE );
1006 /* SDK: ...valid only the next time... */
1007 if( wndpl->flags & WPF_RESTORETOMAXIMIZED )
1009 pWnd = WIN_GetPtr( hwnd );
1010 if (pWnd && pWnd != WND_OTHER_PROCESS)
1012 pWnd->flags |= WIN_RESTORE_MAX;
1013 WIN_ReleasePtr( pWnd );
1021 /***********************************************************************
1022 * SetWindowPlacement (USER32.@)
1025 * Fails if wndpl->length of Win95 (!) apps is invalid.
1027 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
1029 if (!wpl) return FALSE;
1030 return WINPOS_SetPlacement( hwnd, wpl, PLACE_MIN | PLACE_MAX | PLACE_RECT );
1034 /***********************************************************************
1035 * AnimateWindow (USER32.@)
1036 * Shows/Hides a window with an animation
1039 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
1041 FIXME("partial stub\n");
1043 /* If trying to show/hide and it's already *
1044 * shown/hidden or invalid window, fail with *
1045 * invalid parameter */
1046 if(!IsWindow(hwnd) ||
1047 (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
1048 (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
1050 SetLastError(ERROR_INVALID_PARAMETER);
1054 ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1059 /***********************************************************************
1060 * SetInternalWindowPos (USER32.@)
1062 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1063 LPRECT rect, LPPOINT pt )
1065 if( IsWindow(hwnd) )
1067 WINDOWPLACEMENT wndpl;
1070 wndpl.length = sizeof(wndpl);
1071 wndpl.showCmd = showCmd;
1072 wndpl.flags = flags = 0;
1077 wndpl.flags |= WPF_SETMINPOSITION;
1078 wndpl.ptMinPosition = *pt;
1082 flags |= PLACE_RECT;
1083 wndpl.rcNormalPosition = *rect;
1085 WINPOS_SetPlacement( hwnd, &wndpl, flags );
1090 /*******************************************************************
1091 * can_activate_window
1093 * Check if we can activate the specified window.
1095 static BOOL can_activate_window( HWND hwnd )
1099 if (!hwnd) return FALSE;
1100 style = GetWindowLongW( hwnd, GWL_STYLE );
1101 if (!(style & WS_VISIBLE)) return FALSE;
1102 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1103 return !(style & WS_DISABLED);
1107 /*******************************************************************
1108 * WINPOS_ActivateOtherWindow
1110 * Activates window other than pWnd.
1112 void WINPOS_ActivateOtherWindow(HWND hwnd)
1116 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1118 hwndTo = GetAncestor( hwndTo, GA_ROOT );
1119 if (can_activate_window( hwndTo )) goto done;
1125 if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1126 if (can_activate_window( hwndTo )) break;
1130 fg = GetForegroundWindow();
1131 TRACE("win = %p fg = %p\n", hwndTo, fg);
1132 if (!fg || (hwnd == fg))
1134 if (SetForegroundWindow( hwndTo )) return;
1136 if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1140 /***********************************************************************
1141 * WINPOS_HandleWindowPosChanging
1143 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1145 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1147 POINT minTrack, maxTrack;
1148 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1150 if (winpos->flags & SWP_NOSIZE) return 0;
1151 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1153 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1154 if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1155 if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1156 if (!(style & WS_MINIMIZE))
1158 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1159 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1166 /***********************************************************************
1169 static void dump_winpos_flags(UINT flags)
1172 if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1173 if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1174 if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1175 if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1176 if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1177 if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1178 if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1179 if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1180 if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1181 if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1182 if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1183 if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1184 if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1186 #define DUMPED_FLAGS \
1192 SWP_FRAMECHANGED | \
1196 SWP_NOOWNERZORDER | \
1197 SWP_NOSENDCHANGING | \
1201 if(flags & ~DUMPED_FLAGS) TRACE(" %08x", flags & ~DUMPED_FLAGS);
1206 /***********************************************************************
1207 * SetWindowPos (USER32.@)
1209 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
1210 INT x, INT y, INT cx, INT cy, UINT flags )
1214 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1215 hwnd, hwndInsertAfter, x, y, cx, cy, flags);
1216 if(TRACE_ON(win)) dump_winpos_flags(flags);
1218 if (is_broadcast(hwnd))
1220 SetLastError( ERROR_INVALID_PARAMETER );
1224 winpos.hwnd = WIN_GetFullHandle(hwnd);
1225 winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
1230 winpos.flags = flags;
1231 if (WIN_IsCurrentThread( hwnd ))
1232 return USER_Driver->pSetWindowPos( &winpos );
1234 return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
1238 /***********************************************************************
1239 * BeginDeferWindowPos (USER32.@)
1241 HDWP WINAPI BeginDeferWindowPos( INT count )
1246 TRACE("%d\n", count);
1250 SetLastError(ERROR_INVALID_PARAMETER);
1253 /* Windows allows zero count, in which case it allocates context for 8 moves */
1254 if (count == 0) count = 8;
1256 handle = USER_HEAP_ALLOC( sizeof(DWP) + (count-1)*sizeof(WINDOWPOS) );
1257 if (!handle) return 0;
1258 pDWP = (DWP *) USER_HEAP_LIN_ADDR( handle );
1259 pDWP->actualCount = 0;
1260 pDWP->suggestedCount = count;
1262 pDWP->wMagic = DWP_MAGIC;
1263 pDWP->hwndParent = 0;
1265 TRACE("returning hdwp %p\n", handle);
1270 /***********************************************************************
1271 * DeferWindowPos (USER32.@)
1273 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
1274 INT x, INT y, INT cx, INT cy,
1279 HDWP newhdwp = hdwp,retvalue;
1281 TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1282 hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
1284 hwnd = WIN_GetFullHandle( hwnd );
1285 if (hwnd == GetDesktopWindow()) return 0;
1287 if (!(pDWP = USER_HEAP_LIN_ADDR( hdwp ))) return 0;
1291 for (i = 0; i < pDWP->actualCount; i++)
1293 if (pDWP->winPos[i].hwnd == hwnd)
1295 /* Merge with the other changes */
1296 if (!(flags & SWP_NOZORDER))
1298 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
1300 if (!(flags & SWP_NOMOVE))
1302 pDWP->winPos[i].x = x;
1303 pDWP->winPos[i].y = y;
1305 if (!(flags & SWP_NOSIZE))
1307 pDWP->winPos[i].cx = cx;
1308 pDWP->winPos[i].cy = cy;
1310 pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
1311 SWP_NOZORDER | SWP_NOREDRAW |
1312 SWP_NOACTIVATE | SWP_NOCOPYBITS|
1314 pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
1320 if (pDWP->actualCount >= pDWP->suggestedCount)
1322 newhdwp = USER_HEAP_REALLOC( hdwp,
1323 sizeof(DWP) + pDWP->suggestedCount*sizeof(WINDOWPOS) );
1329 pDWP = (DWP *) USER_HEAP_LIN_ADDR( newhdwp );
1330 pDWP->suggestedCount++;
1332 pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
1333 pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
1334 pDWP->winPos[pDWP->actualCount].x = x;
1335 pDWP->winPos[pDWP->actualCount].y = y;
1336 pDWP->winPos[pDWP->actualCount].cx = cx;
1337 pDWP->winPos[pDWP->actualCount].cy = cy;
1338 pDWP->winPos[pDWP->actualCount].flags = flags;
1339 pDWP->actualCount++;
1347 /***********************************************************************
1348 * EndDeferWindowPos (USER32.@)
1350 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
1357 TRACE("%p\n", hdwp);
1359 pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
1360 if (!pDWP) return FALSE;
1361 for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
1363 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1364 winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
1365 winpos->cx, winpos->cy, winpos->flags);
1367 if (!(res = USER_Driver->pSetWindowPos( winpos ))) break;
1369 USER_HEAP_FREE( hdwp );
1374 /***********************************************************************
1375 * TileChildWindows (USER.199)
1377 void WINAPI TileChildWindows16( HWND16 parent, WORD action )
1379 FIXME("(%04x, %d): stub\n", parent, action);
1382 /***********************************************************************
1383 * CascadeChildWindows (USER.198)
1385 void WINAPI CascadeChildWindows16( HWND16 parent, WORD action )
1387 FIXME("(%04x, %d): stub\n", parent, action);