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
33 #include "wine/server.h"
35 #include "user_private.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(win);
41 #define SWP_AGG_NOGEOMETRYCHANGE \
42 (SWP_NOSIZE | SWP_NOCLIENTSIZE | SWP_NOZORDER)
43 #define SWP_AGG_NOPOSCHANGE \
44 (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
45 #define SWP_AGG_STATUSFLAGS \
46 (SWP_AGG_NOPOSCHANGE | SWP_FRAMECHANGED | SWP_HIDEWINDOW | SWP_SHOWWINDOW)
48 #define HAS_DLGFRAME(style,exStyle) \
49 (((exStyle) & WS_EX_DLGMODALFRAME) || \
50 (((style) & WS_DLGFRAME) && !((style) & WS_BORDER)))
52 #define HAS_THICKFRAME(style) \
53 (((style) & WS_THICKFRAME) && \
54 !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
56 #define EMPTYPOINT(pt) ((pt).x == -1 && (pt).y == -1)
58 #define ON_LEFT_BORDER(hit) \
59 (((hit) == HTLEFT) || ((hit) == HTTOPLEFT) || ((hit) == HTBOTTOMLEFT))
60 #define ON_RIGHT_BORDER(hit) \
61 (((hit) == HTRIGHT) || ((hit) == HTTOPRIGHT) || ((hit) == HTBOTTOMRIGHT))
62 #define ON_TOP_BORDER(hit) \
63 (((hit) == HTTOP) || ((hit) == HTTOPLEFT) || ((hit) == HTTOPRIGHT))
64 #define ON_BOTTOM_BORDER(hit) \
65 (((hit) == HTBOTTOM) || ((hit) == HTBOTTOMLEFT) || ((hit) == HTBOTTOMRIGHT))
67 #define PLACE_MIN 0x0001
68 #define PLACE_MAX 0x0002
69 #define PLACE_RECT 0x0004
73 struct user_object obj;
81 /***********************************************************************
82 * SwitchToThisWindow (USER32.@)
84 void WINAPI SwitchToThisWindow( HWND hwnd, BOOL alt_tab )
86 if (IsIconic( hwnd )) ShowWindow( hwnd, SW_RESTORE );
87 else BringWindowToTop( hwnd );
91 /***********************************************************************
92 * GetWindowRect (USER32.@)
94 BOOL WINAPI GetWindowRect( HWND hwnd, LPRECT rect )
96 BOOL ret = WIN_GetRectangles( hwnd, COORDS_SCREEN, rect, NULL );
97 if (ret) TRACE( "hwnd %p %s\n", hwnd, wine_dbgstr_rect(rect) );
102 /***********************************************************************
103 * GetWindowRgn (USER32.@)
105 int WINAPI GetWindowRgn ( HWND hwnd, HRGN hrgn )
115 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
117 SetLastError( ERROR_OUTOFMEMORY );
120 SERVER_START_REQ( get_window_region )
122 req->window = wine_server_user_handle( hwnd );
123 wine_server_set_reply( req, data->Buffer, size );
124 if (!(status = wine_server_call( req )))
126 size_t reply_size = wine_server_reply_size( reply );
129 data->rdh.dwSize = sizeof(data->rdh);
130 data->rdh.iType = RDH_RECTANGLES;
131 data->rdh.nCount = reply_size / sizeof(RECT);
132 data->rdh.nRgnSize = reply_size;
133 win_rgn = ExtCreateRegion( NULL, size, data );
136 else size = reply->total_size;
139 HeapFree( GetProcessHeap(), 0, data );
140 } while (status == STATUS_BUFFER_OVERFLOW);
142 if (status) SetLastError( RtlNtStatusToDosError(status) );
145 nRet = CombineRgn( hrgn, win_rgn, 0, RGN_COPY );
146 DeleteObject( win_rgn );
151 /***********************************************************************
152 * GetWindowRgnBox (USER32.@)
154 int WINAPI GetWindowRgnBox( HWND hwnd, LPRECT prect )
162 if ((hrgn = CreateRectRgn(0, 0, 0, 0)))
164 if ((ret = GetWindowRgn( hwnd, hrgn )) != ERROR )
165 ret = GetRgnBox( hrgn, prect );
172 /***********************************************************************
173 * SetWindowRgn (USER32.@)
175 int WINAPI SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL bRedraw )
177 static const RECT empty_rect;
185 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
186 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
187 if (!GetRegionData( hrgn, size, data ))
189 HeapFree( GetProcessHeap(), 0, data );
192 SERVER_START_REQ( set_window_region )
194 req->window = wine_server_user_handle( hwnd );
195 req->redraw = (bRedraw != 0);
196 if (data->rdh.nCount)
197 wine_server_add_data( req, data->Buffer, data->rdh.nCount * sizeof(RECT) );
199 wine_server_add_data( req, &empty_rect, sizeof(empty_rect) );
200 ret = !wine_server_call_err( req );
203 HeapFree( GetProcessHeap(), 0, data );
205 else /* clear existing region */
207 SERVER_START_REQ( set_window_region )
209 req->window = wine_server_user_handle( hwnd );
210 req->redraw = (bRedraw != 0);
211 ret = !wine_server_call_err( req );
216 if (ret) ret = USER_Driver->pSetWindowRgn( hwnd, hrgn, bRedraw );
220 UINT swp_flags = SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE;
221 if (!bRedraw) swp_flags |= SWP_NOREDRAW;
222 SetWindowPos( hwnd, 0, 0, 0, 0, 0, swp_flags );
223 invalidate_dce( hwnd, NULL );
224 if (hrgn) DeleteObject( hrgn );
230 /***********************************************************************
231 * GetClientRect (USER32.@)
233 BOOL WINAPI GetClientRect( HWND hwnd, LPRECT rect )
235 return WIN_GetRectangles( hwnd, COORDS_CLIENT, NULL, rect );
239 /*******************************************************************
240 * ClientToScreen (USER32.@)
242 BOOL WINAPI ClientToScreen( HWND hwnd, LPPOINT lppnt )
244 MapWindowPoints( hwnd, 0, lppnt, 1 );
249 /*******************************************************************
250 * ScreenToClient (USER32.@)
252 BOOL WINAPI ScreenToClient( HWND hwnd, LPPOINT lppnt )
254 MapWindowPoints( 0, hwnd, lppnt, 1 );
259 /***********************************************************************
260 * list_children_from_point
262 * Get the list of children that can contain point from the server.
263 * Point is in screen coordinates.
264 * Returned list must be freed by caller.
266 static HWND *list_children_from_point( HWND hwnd, POINT pt )
275 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
277 SERVER_START_REQ( get_window_children_from_point )
279 req->parent = wine_server_user_handle( hwnd );
282 wine_server_set_reply( req, list, (size-1) * sizeof(user_handle_t) );
283 if (!wine_server_call( req )) count = reply->count;
286 if (count && count < size)
288 /* start from the end since HWND is potentially larger than user_handle_t */
289 for (i = count - 1; i >= 0; i--)
290 list[i] = wine_server_ptr_handle( ((user_handle_t *)list)[i] );
294 HeapFree( GetProcessHeap(), 0, list );
296 size = count + 1; /* restart with a large enough buffer */
302 /***********************************************************************
303 * WINPOS_WindowFromPoint
305 * Find the window and hittest for a given point.
307 HWND WINPOS_WindowFromPoint( HWND hwndScope, POINT pt, INT *hittest )
312 if (!hwndScope) hwndScope = GetDesktopWindow();
314 *hittest = HTNOWHERE;
316 if (!(list = list_children_from_point( hwndScope, pt ))) return 0;
318 /* now determine the hittest */
320 for (i = 0; list[i]; i++)
322 LONG style = GetWindowLongW( list[i], GWL_STYLE );
324 /* If window is minimized or disabled, return at once */
325 if (style & WS_MINIMIZE)
327 *hittest = HTCAPTION;
330 if (style & WS_DISABLED)
335 /* Send WM_NCCHITTEST (if same thread) */
336 if (!WIN_IsCurrentThread( list[i] ))
341 res = SendMessageW( list[i], WM_NCHITTEST, 0, MAKELONG(pt.x,pt.y) );
342 if (res != HTTRANSPARENT)
344 *hittest = res; /* Found the window */
347 /* continue search with next window in z-order */
350 HeapFree( GetProcessHeap(), 0, list );
351 TRACE( "scope %p (%d,%d) returning %p\n", hwndScope, pt.x, pt.y, ret );
356 /*******************************************************************
357 * WindowFromPoint (USER32.@)
359 HWND WINAPI WindowFromPoint( POINT pt )
362 return WINPOS_WindowFromPoint( 0, pt, &hittest );
366 /*******************************************************************
367 * ChildWindowFromPoint (USER32.@)
369 HWND WINAPI ChildWindowFromPoint( HWND hwndParent, POINT pt )
371 return ChildWindowFromPointEx( hwndParent, pt, CWP_ALL );
374 /*******************************************************************
375 * RealChildWindowFromPoint (USER32.@)
377 HWND WINAPI RealChildWindowFromPoint( HWND hwndParent, POINT pt )
379 return ChildWindowFromPointEx( hwndParent, pt, CWP_SKIPTRANSPARENT );
382 /*******************************************************************
383 * ChildWindowFromPointEx (USER32.@)
385 HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
387 /* pt is in the client coordinates */
393 GetClientRect( hwndParent, &rect );
394 if (!PtInRect( &rect, pt )) return 0;
395 if (!(list = WIN_ListChildren( hwndParent ))) return hwndParent;
397 for (i = 0; list[i]; i++)
399 if (!WIN_GetRectangles( list[i], COORDS_PARENT, &rect, NULL )) continue;
400 if (!PtInRect( &rect, pt )) continue;
401 if (uFlags & (CWP_SKIPINVISIBLE|CWP_SKIPDISABLED))
403 LONG style = GetWindowLongW( list[i], GWL_STYLE );
404 if ((uFlags & CWP_SKIPINVISIBLE) && !(style & WS_VISIBLE)) continue;
405 if ((uFlags & CWP_SKIPDISABLED) && (style & WS_DISABLED)) continue;
407 if (uFlags & CWP_SKIPTRANSPARENT)
409 if (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TRANSPARENT) continue;
414 HeapFree( GetProcessHeap(), 0, list );
415 if (!retvalue) retvalue = hwndParent;
420 /*******************************************************************
421 * WINPOS_GetWinOffset
423 * Calculate the offset between the origin of the two windows. Used
424 * to implement MapWindowPoints.
426 static POINT WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo, BOOL *mirrored )
430 BOOL mirror_from, mirror_to;
433 offset.x = offset.y = 0;
434 *mirrored = mirror_from = mirror_to = FALSE;
436 /* Translate source window origin to screen coords */
439 if (!(wndPtr = WIN_GetPtr( hwndFrom ))) return offset;
440 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
441 if (wndPtr != WND_DESKTOP)
443 if (wndPtr->dwExStyle & WS_EX_LAYOUTRTL)
446 offset.x += wndPtr->rectClient.right - wndPtr->rectClient.left;
448 while (wndPtr != WND_DESKTOP)
450 offset.x += wndPtr->rectClient.left;
451 offset.y += wndPtr->rectClient.top;
452 hwnd = wndPtr->parent;
453 WIN_ReleasePtr( wndPtr );
454 if (!(wndPtr = WIN_GetPtr( hwnd ))) break;
455 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
460 /* Translate origin to destination window coords */
463 if (!(wndPtr = WIN_GetPtr( hwndTo ))) return offset;
464 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
465 if (wndPtr != WND_DESKTOP)
467 if (wndPtr->dwExStyle & WS_EX_LAYOUTRTL)
470 offset.x -= wndPtr->rectClient.right - wndPtr->rectClient.left;
472 while (wndPtr != WND_DESKTOP)
474 offset.x -= wndPtr->rectClient.left;
475 offset.y -= wndPtr->rectClient.top;
476 hwnd = wndPtr->parent;
477 WIN_ReleasePtr( wndPtr );
478 if (!(wndPtr = WIN_GetPtr( hwnd ))) break;
479 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
484 *mirrored = mirror_from ^ mirror_to;
485 if (mirror_from) offset.x = -offset.x;
488 other_process: /* one of the parents may belong to another process, do it the hard way */
489 offset.x = offset.y = 0;
490 SERVER_START_REQ( get_windows_offset )
492 req->from = wine_server_user_handle( hwndFrom );
493 req->to = wine_server_user_handle( hwndTo );
494 if (!wine_server_call( req ))
498 *mirrored = reply->mirror;
505 /* map coordinates of a window region */
506 void map_window_region( HWND from, HWND to, HRGN hrgn )
509 POINT offset = WINPOS_GetWinOffset( from, to, &mirrored );
517 OffsetRgn( hrgn, offset.x, offset.y );
520 if (!(size = GetRegionData( hrgn, 0, NULL ))) return;
521 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return;
522 GetRegionData( hrgn, size, data );
523 rect = (RECT *)data->Buffer;
524 for (i = 0; i < data->rdh.nCount; i++)
526 int tmp = -(rect[i].left + offset.x);
527 rect[i].left = -(rect[i].right + offset.x);
529 rect[i].top += offset.y;
530 rect[i].bottom += offset.y;
532 if ((new_rgn = ExtCreateRegion( NULL, data->rdh.nCount, data )))
534 CombineRgn( hrgn, new_rgn, 0, RGN_COPY );
535 DeleteObject( new_rgn );
537 HeapFree( GetProcessHeap(), 0, data );
541 /*******************************************************************
542 * MapWindowPoints (USER32.@)
544 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
547 POINT offset = WINPOS_GetWinOffset( hwndFrom, hwndTo, &mirrored );
550 for (i = 0; i < count; i++)
552 lppt[i].x += offset.x;
553 lppt[i].y += offset.y;
554 if (mirrored) lppt[i].x = -lppt[i].x;
556 if (mirrored && count == 2) /* special case for rectangle */
559 lppt[0].x = lppt[1].x;
562 return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
566 /***********************************************************************
567 * IsIconic (USER32.@)
569 BOOL WINAPI IsIconic(HWND hWnd)
571 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
575 /***********************************************************************
576 * IsZoomed (USER32.@)
578 BOOL WINAPI IsZoomed(HWND hWnd)
580 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
584 /*******************************************************************
585 * AllowSetForegroundWindow (USER32.@)
587 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
589 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
590 * implemented, then fix this function. */
595 /*******************************************************************
596 * LockSetForegroundWindow (USER32.@)
598 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
600 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
601 * implemented, then fix this function. */
606 /***********************************************************************
607 * BringWindowToTop (USER32.@)
609 BOOL WINAPI BringWindowToTop( HWND hwnd )
611 return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
615 /***********************************************************************
616 * MoveWindow (USER32.@)
618 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
621 int flags = SWP_NOZORDER | SWP_NOACTIVATE;
622 if (!repaint) flags |= SWP_NOREDRAW;
623 TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
624 return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
628 /***********************************************************************
629 * WINPOS_RedrawIconTitle
631 BOOL WINPOS_RedrawIconTitle( HWND hWnd )
634 WND *win = WIN_GetPtr( hWnd );
636 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
638 icon_title = win->icon_title;
639 WIN_ReleasePtr( win );
641 if (!icon_title) return FALSE;
642 SendMessageW( icon_title, WM_SHOWWINDOW, TRUE, 0 );
643 InvalidateRect( icon_title, NULL, TRUE );
647 /***********************************************************************
648 * WINPOS_ShowIconTitle
650 static BOOL WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
652 if (!GetPropA( hwnd, "__wine_x11_managed" ))
654 WND *win = WIN_GetPtr( hwnd );
657 TRACE("%p %i\n", hwnd, (bShow != 0) );
659 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP) return FALSE;
660 title = win->icon_title;
661 WIN_ReleasePtr( win );
667 title = ICONTITLE_Create( hwnd );
668 if (!(win = WIN_GetPtr( hwnd )) || win == WND_OTHER_PROCESS)
670 DestroyWindow( title );
673 win->icon_title = title;
674 WIN_ReleasePtr( win );
676 if (!IsWindowVisible(title))
678 SendMessageW( title, WM_SHOWWINDOW, TRUE, 0 );
679 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
680 SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
683 else if (title) ShowWindow( title, SW_HIDE );
688 /*******************************************************************
689 * WINPOS_GetMinMaxInfo
691 * Get the minimized and maximized information for a window.
693 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
694 POINT *minTrack, POINT *maxTrack )
699 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
701 LONG exstyle = GetWindowLongW( hwnd, GWL_EXSTYLE );
705 /* Compute default values */
707 GetWindowRect(hwnd, &rc);
708 MinMax.ptReserved.x = rc.left;
709 MinMax.ptReserved.y = rc.top;
711 if ((style & WS_CAPTION) == WS_CAPTION)
712 adjustedStyle = style & ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
714 adjustedStyle = style;
716 GetClientRect(GetAncestor(hwnd,GA_PARENT), &rc);
717 AdjustWindowRectEx(&rc, adjustedStyle, ((style & WS_POPUP) && GetMenu(hwnd)), exstyle);
722 MinMax.ptMaxSize.x = rc.right - rc.left;
723 MinMax.ptMaxSize.y = rc.bottom - rc.top;
724 if (style & (WS_DLGFRAME | WS_BORDER))
726 MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
727 MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
731 MinMax.ptMinTrackSize.x = 2 * xinc;
732 MinMax.ptMinTrackSize.y = 2 * yinc;
734 MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXMAXTRACK);
735 MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYMAXTRACK);
736 MinMax.ptMaxPosition.x = -xinc;
737 MinMax.ptMaxPosition.y = -yinc;
739 if ((win = WIN_GetPtr( hwnd )) && win != WND_DESKTOP && win != WND_OTHER_PROCESS)
741 if (!EMPTYPOINT(win->max_pos)) MinMax.ptMaxPosition = win->max_pos;
742 WIN_ReleasePtr( win );
745 SendMessageW( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
747 /* if the app didn't change the values, adapt them for the current monitor */
749 if ((monitor = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY )))
752 MONITORINFO mon_info;
754 mon_info.cbSize = sizeof(mon_info);
755 GetMonitorInfoW( monitor, &mon_info );
757 rc_work = mon_info.rcMonitor;
759 if (style & WS_MAXIMIZEBOX)
761 if ((style & WS_CAPTION) == WS_CAPTION || !(style & (WS_CHILD | WS_POPUP)))
762 rc_work = mon_info.rcWork;
765 if (MinMax.ptMaxSize.x == GetSystemMetrics(SM_CXSCREEN) + 2 * xinc &&
766 MinMax.ptMaxSize.y == GetSystemMetrics(SM_CYSCREEN) + 2 * yinc)
768 MinMax.ptMaxSize.x = (rc_work.right - rc_work.left) + 2 * xinc;
769 MinMax.ptMaxSize.y = (rc_work.bottom - rc_work.top) + 2 * yinc;
771 if (MinMax.ptMaxPosition.x == -xinc && MinMax.ptMaxPosition.y == -yinc)
773 MinMax.ptMaxPosition.x = rc_work.left - xinc;
774 MinMax.ptMaxPosition.y = rc_work.top - yinc;
778 /* Some sanity checks */
780 TRACE("%d %d / %d %d / %d %d / %d %d\n",
781 MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
782 MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
783 MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
784 MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
785 MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
786 MinMax.ptMinTrackSize.x );
787 MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
788 MinMax.ptMinTrackSize.y );
790 if (maxSize) *maxSize = MinMax.ptMaxSize;
791 if (maxPos) *maxPos = MinMax.ptMaxPosition;
792 if (minTrack) *minTrack = MinMax.ptMinTrackSize;
793 if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
797 /***********************************************************************
800 * Find a suitable place for an iconic window.
802 static POINT WINPOS_FindIconPos( HWND hwnd, POINT pt )
804 RECT rect, rectParent;
807 int xspacing, yspacing;
809 parent = GetAncestor( hwnd, GA_PARENT );
810 GetClientRect( parent, &rectParent );
811 if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
812 (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
813 return pt; /* The icon already has a suitable position */
815 xspacing = GetSystemMetrics(SM_CXICONSPACING);
816 yspacing = GetSystemMetrics(SM_CYICONSPACING);
818 /* Check if another icon already occupies this spot */
819 /* FIXME: this is completely inefficient */
821 hrgn = CreateRectRgn( 0, 0, 0, 0 );
822 tmp = CreateRectRgn( 0, 0, 0, 0 );
823 for (child = GetWindow( parent, GW_HWNDFIRST ); child; child = GetWindow( child, GW_HWNDNEXT ))
825 if (child == hwnd) continue;
826 if ((GetWindowLongW( child, GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != (WS_VISIBLE|WS_MINIMIZE))
828 if (WIN_GetRectangles( child, COORDS_PARENT, &rect, NULL ))
830 SetRectRgn( tmp, rect.left, rect.top, rect.right, rect.bottom );
831 CombineRgn( hrgn, hrgn, tmp, RGN_OR );
836 for (rect.bottom = rectParent.bottom; rect.bottom >= yspacing; rect.bottom -= yspacing)
838 for (rect.left = rectParent.left; rect.left <= rectParent.right - xspacing; rect.left += xspacing)
840 rect.right = rect.left + xspacing;
841 rect.top = rect.bottom - yspacing;
842 if (!RectInRegion( hrgn, &rect ))
844 /* No window was found, so it's OK for us */
845 pt.x = rect.left + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
846 pt.y = rect.top + (yspacing - GetSystemMetrics(SM_CYICON)) / 2;
847 DeleteObject( hrgn );
852 DeleteObject( hrgn );
858 /***********************************************************************
861 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
869 TRACE("%p %u\n", hwnd, cmd );
871 wpl.length = sizeof(wpl);
872 GetWindowPlacement( hwnd, &wpl );
874 if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
875 return SWP_NOSIZE | SWP_NOMOVE;
877 if (IsIconic( hwnd ))
881 case SW_SHOWMINNOACTIVE:
882 case SW_SHOWMINIMIZED:
883 case SW_FORCEMINIMIZE:
885 return SWP_NOSIZE | SWP_NOMOVE;
887 if (!SendMessageW( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
888 swpFlags |= SWP_NOCOPYBITS;
893 case SW_SHOWMINNOACTIVE:
894 case SW_SHOWMINIMIZED:
895 case SW_FORCEMINIMIZE:
897 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
898 if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
899 else wndPtr->flags &= ~WIN_RESTORE_MAX;
900 WIN_ReleasePtr( wndPtr );
902 old_style = WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
904 wpl.ptMinPosition = WINPOS_FindIconPos( hwnd, wpl.ptMinPosition );
906 if (!(old_style & WS_MINIMIZE)) swpFlags |= SWP_STATECHANGED;
907 SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
908 wpl.ptMinPosition.x + GetSystemMetrics(SM_CXICON),
909 wpl.ptMinPosition.y + GetSystemMetrics(SM_CYICON) );
910 swpFlags |= SWP_NOCOPYBITS;
914 old_style = GetWindowLongW( hwnd, GWL_STYLE );
915 if ((old_style & WS_MAXIMIZE) && (old_style & WS_VISIBLE)) return SWP_NOSIZE | SWP_NOMOVE;
917 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
919 old_style = WIN_SetStyle( hwnd, WS_MAXIMIZE, WS_MINIMIZE );
920 if (old_style & WS_MINIMIZE)
922 if ((wndPtr = WIN_GetPtr( hwnd )) && wndPtr != WND_OTHER_PROCESS)
924 wndPtr->flags |= WIN_RESTORE_MAX;
925 WIN_ReleasePtr( wndPtr );
927 WINPOS_ShowIconTitle( hwnd, FALSE );
930 if (!(old_style & WS_MAXIMIZE)) swpFlags |= SWP_STATECHANGED;
931 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y,
932 wpl.ptMaxPosition.x + size.x, wpl.ptMaxPosition.y + size.y );
935 case SW_SHOWNOACTIVATE:
936 if ((wndPtr = WIN_GetPtr( hwnd )) && wndPtr != WND_OTHER_PROCESS)
938 wndPtr->flags &= ~WIN_RESTORE_MAX;
939 WIN_ReleasePtr( wndPtr );
944 old_style = WIN_SetStyle( hwnd, 0, WS_MINIMIZE | WS_MAXIMIZE );
945 if (old_style & WS_MINIMIZE)
949 WINPOS_ShowIconTitle( hwnd, FALSE );
951 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
952 restore_max = (wndPtr->flags & WIN_RESTORE_MAX) != 0;
953 WIN_ReleasePtr( wndPtr );
956 /* Restore to maximized position */
957 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
958 WIN_SetStyle( hwnd, WS_MAXIMIZE, 0 );
959 swpFlags |= SWP_STATECHANGED;
960 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y,
961 wpl.ptMaxPosition.x + size.x, wpl.ptMaxPosition.y + size.y );
965 else if (!(old_style & WS_MAXIMIZE)) break;
967 swpFlags |= SWP_STATECHANGED;
969 /* Restore to normal position */
971 *rect = wpl.rcNormalPosition;
979 /***********************************************************************
982 * Implementation of ShowWindow and ShowWindowAsync.
984 static BOOL show_window( HWND hwnd, INT cmd )
988 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
989 BOOL wasVisible = (style & WS_VISIBLE) != 0;
990 BOOL showFlag = TRUE;
991 RECT newPos = {0, 0, 0, 0};
994 TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);
999 if (!wasVisible) return FALSE;
1001 swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1002 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1005 case SW_SHOWMINNOACTIVE:
1007 case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
1008 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1010 case SW_SHOWMINIMIZED:
1011 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1012 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
1013 if ((style & WS_MINIMIZE) && wasVisible) return TRUE;
1016 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
1017 if (!wasVisible) swp |= SWP_SHOWWINDOW;
1018 swp |= SWP_FRAMECHANGED;
1019 swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
1020 if ((style & WS_MAXIMIZE) && wasVisible) return TRUE;
1024 swp |= SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1025 if (style & WS_CHILD) swp |= SWP_NOZORDER;
1028 if (wasVisible) return TRUE;
1029 swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1030 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1033 case SW_SHOWNOACTIVATE:
1034 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1038 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
1039 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
1040 if (!wasVisible) swp |= SWP_SHOWWINDOW;
1041 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1043 swp |= SWP_FRAMECHANGED;
1044 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
1048 if (wasVisible) return TRUE;
1049 swp |= SWP_NOSIZE | SWP_NOMOVE;
1051 if (style & WS_CHILD && !(swp & SWP_STATECHANGED)) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1057 if ((showFlag != wasVisible || cmd == SW_SHOWNA) && cmd != SW_SHOWMAXIMIZED && !(swp & SWP_STATECHANGED))
1059 SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
1060 if (!IsWindow( hwnd )) return wasVisible;
1063 swp = USER_Driver->pShowWindow( hwnd, cmd, &newPos, swp );
1065 parent = GetAncestor( hwnd, GA_PARENT );
1066 if (parent && !IsWindowVisible( parent ) && !(swp & SWP_STATECHANGED))
1068 /* if parent is not visible simply toggle WS_VISIBLE and return */
1069 if (showFlag) WIN_SetStyle( hwnd, WS_VISIBLE, 0 );
1070 else WIN_SetStyle( hwnd, 0, WS_VISIBLE );
1073 SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1074 newPos.right - newPos.left, newPos.bottom - newPos.top, swp );
1080 WINPOS_ShowIconTitle( hwnd, FALSE );
1082 /* FIXME: This will cause the window to be activated irrespective
1083 * of whether it is owned by the same thread. Has to be done
1087 if (hwnd == GetActiveWindow())
1088 WINPOS_ActivateOtherWindow(hwnd);
1090 /* Revert focus to parent */
1091 hFocus = GetFocus();
1094 HWND parent = GetAncestor(hwnd, GA_PARENT);
1095 if (parent == GetDesktopWindow()) parent = 0;
1101 if (IsIconic(hwnd)) WINPOS_ShowIconTitle( hwnd, TRUE );
1103 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return wasVisible;
1105 if (wndPtr->flags & WIN_NEED_SIZE)
1107 /* should happen only in CreateWindowEx() */
1108 int wParam = SIZE_RESTORED;
1112 WIN_GetRectangles( hwnd, COORDS_PARENT, NULL, &client );
1113 lparam = MAKELONG( client.right - client.left, client.bottom - client.top );
1114 wndPtr->flags &= ~WIN_NEED_SIZE;
1115 if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1116 else if (wndPtr->dwStyle & WS_MINIMIZE)
1118 wParam = SIZE_MINIMIZED;
1121 WIN_ReleasePtr( wndPtr );
1123 SendMessageW( hwnd, WM_SIZE, wParam, lparam );
1124 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( client.left, client.top ));
1126 else WIN_ReleasePtr( wndPtr );
1128 /* if previous state was minimized Windows sets focus to the window */
1129 if (style & WS_MINIMIZE) SetFocus( hwnd );
1135 /***********************************************************************
1136 * ShowWindowAsync (USER32.@)
1138 * doesn't wait; returns immediately.
1139 * used by threads to toggle windows in other (possibly hanging) threads
1141 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
1145 if (is_broadcast(hwnd))
1147 SetLastError( ERROR_INVALID_PARAMETER );
1151 if ((full_handle = WIN_IsCurrentThread( hwnd )))
1152 return show_window( full_handle, cmd );
1154 return SendNotifyMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
1158 /***********************************************************************
1159 * ShowWindow (USER32.@)
1161 BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
1165 if (is_broadcast(hwnd))
1167 SetLastError( ERROR_INVALID_PARAMETER );
1170 if ((full_handle = WIN_IsCurrentThread( hwnd )))
1171 return show_window( full_handle, cmd );
1173 return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
1177 /***********************************************************************
1178 * GetInternalWindowPos (USER32.@)
1180 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
1183 WINDOWPLACEMENT wndpl;
1184 if (GetWindowPlacement( hwnd, &wndpl ))
1186 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
1187 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
1188 return wndpl.showCmd;
1194 /***********************************************************************
1195 * GetWindowPlacement (USER32.@)
1198 * Fails if wndpl->length of Win95 (!) apps is invalid.
1200 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
1202 WND *pWnd = WIN_GetPtr( hwnd );
1204 if (!pWnd) return FALSE;
1206 if (pWnd == WND_DESKTOP)
1208 wndpl->length = sizeof(*wndpl);
1209 wndpl->showCmd = SW_SHOWNORMAL;
1211 wndpl->ptMinPosition.x = -1;
1212 wndpl->ptMinPosition.y = -1;
1213 wndpl->ptMaxPosition.x = -1;
1214 wndpl->ptMaxPosition.y = -1;
1215 GetWindowRect( hwnd, &wndpl->rcNormalPosition );
1218 if (pWnd == WND_OTHER_PROCESS)
1220 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
1224 /* update the placement according to the current style */
1225 if (pWnd->dwStyle & WS_MINIMIZE)
1227 pWnd->min_pos.x = pWnd->rectWindow.left;
1228 pWnd->min_pos.y = pWnd->rectWindow.top;
1230 else if (pWnd->dwStyle & WS_MAXIMIZE)
1232 pWnd->max_pos.x = pWnd->rectWindow.left;
1233 pWnd->max_pos.y = pWnd->rectWindow.top;
1237 pWnd->normal_rect = pWnd->rectWindow;
1240 wndpl->length = sizeof(*wndpl);
1241 if( pWnd->dwStyle & WS_MINIMIZE )
1242 wndpl->showCmd = SW_SHOWMINIMIZED;
1244 wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
1245 if( pWnd->flags & WIN_RESTORE_MAX )
1246 wndpl->flags = WPF_RESTORETOMAXIMIZED;
1249 wndpl->ptMinPosition = pWnd->min_pos;
1250 wndpl->ptMaxPosition = pWnd->max_pos;
1251 wndpl->rcNormalPosition = pWnd->normal_rect;
1252 WIN_ReleasePtr( pWnd );
1254 TRACE( "%p: returning min %d,%d max %d,%d normal %s\n",
1255 hwnd, wndpl->ptMinPosition.x, wndpl->ptMinPosition.y,
1256 wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y,
1257 wine_dbgstr_rect(&wndpl->rcNormalPosition) );
1261 /* make sure the specified rect is visible on screen */
1262 static void make_rect_onscreen( RECT *rect )
1265 HMONITOR monitor = MonitorFromRect( rect, MONITOR_DEFAULTTONEAREST );
1267 info.cbSize = sizeof(info);
1268 if (!monitor || !GetMonitorInfoW( monitor, &info )) return;
1269 /* FIXME: map coordinates from rcWork to rcMonitor */
1270 if (rect->right <= info.rcWork.left)
1272 rect->right += info.rcWork.left - rect->left;
1273 rect->left = info.rcWork.left;
1275 else if (rect->left >= info.rcWork.right)
1277 rect->left += info.rcWork.right - rect->right;
1278 rect->right = info.rcWork.right;
1280 if (rect->bottom <= info.rcWork.top)
1282 rect->bottom += info.rcWork.top - rect->top;
1283 rect->top = info.rcWork.top;
1285 else if (rect->top >= info.rcWork.bottom)
1287 rect->top += info.rcWork.bottom - rect->bottom;
1288 rect->bottom = info.rcWork.bottom;
1292 /* make sure the specified point is visible on screen */
1293 static void make_point_onscreen( POINT *pt )
1297 SetRect( &rect, pt->x, pt->y, pt->x + 1, pt->y + 1 );
1298 make_rect_onscreen( &rect );
1304 /***********************************************************************
1305 * WINPOS_SetPlacement
1307 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
1310 WND *pWnd = WIN_GetPtr( hwnd );
1311 WINDOWPLACEMENT wp = *wndpl;
1313 if (flags & PLACE_MIN) make_point_onscreen( &wp.ptMinPosition );
1314 if (flags & PLACE_MAX) make_point_onscreen( &wp.ptMaxPosition );
1315 if (flags & PLACE_RECT) make_rect_onscreen( &wp.rcNormalPosition );
1317 TRACE( "%p: setting min %d,%d max %d,%d normal %s flags %x ajusted to min %d,%d max %d,%d normal %s\n",
1318 hwnd, wndpl->ptMinPosition.x, wndpl->ptMinPosition.y,
1319 wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y,
1320 wine_dbgstr_rect(&wndpl->rcNormalPosition), flags,
1321 wp.ptMinPosition.x, wp.ptMinPosition.y, wp.ptMaxPosition.x, wp.ptMaxPosition.y,
1322 wine_dbgstr_rect(&wp.rcNormalPosition) );
1324 if (!pWnd || pWnd == WND_OTHER_PROCESS || pWnd == WND_DESKTOP) return FALSE;
1326 if( flags & PLACE_MIN ) pWnd->min_pos = wp.ptMinPosition;
1327 if( flags & PLACE_MAX ) pWnd->max_pos = wp.ptMaxPosition;
1328 if( flags & PLACE_RECT) pWnd->normal_rect = wp.rcNormalPosition;
1330 style = pWnd->dwStyle;
1332 WIN_ReleasePtr( pWnd );
1334 if( style & WS_MINIMIZE )
1336 if (flags & PLACE_MIN)
1338 WINPOS_ShowIconTitle( hwnd, FALSE );
1339 SetWindowPos( hwnd, 0, wp.ptMinPosition.x, wp.ptMinPosition.y, 0, 0,
1340 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1343 else if( style & WS_MAXIMIZE )
1345 if (flags & PLACE_MAX)
1346 SetWindowPos( hwnd, 0, wp.ptMaxPosition.x, wp.ptMaxPosition.y, 0, 0,
1347 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1349 else if( flags & PLACE_RECT )
1350 SetWindowPos( hwnd, 0, wp.rcNormalPosition.left, wp.rcNormalPosition.top,
1351 wp.rcNormalPosition.right - wp.rcNormalPosition.left,
1352 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top,
1353 SWP_NOZORDER | SWP_NOACTIVATE );
1355 ShowWindow( hwnd, wndpl->showCmd );
1357 if (IsIconic( hwnd ))
1359 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE) WINPOS_ShowIconTitle( hwnd, TRUE );
1361 /* SDK: ...valid only the next time... */
1362 if( wndpl->flags & WPF_RESTORETOMAXIMIZED )
1364 pWnd = WIN_GetPtr( hwnd );
1365 if (pWnd && pWnd != WND_OTHER_PROCESS)
1367 pWnd->flags |= WIN_RESTORE_MAX;
1368 WIN_ReleasePtr( pWnd );
1376 /***********************************************************************
1377 * SetWindowPlacement (USER32.@)
1380 * Fails if wndpl->length of Win95 (!) apps is invalid.
1382 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
1384 UINT flags = PLACE_MAX | PLACE_RECT;
1385 if (!wpl) return FALSE;
1386 if (wpl->flags & WPF_SETMINPOSITION) flags |= PLACE_MIN;
1387 return WINPOS_SetPlacement( hwnd, wpl, flags );
1391 /***********************************************************************
1392 * AnimateWindow (USER32.@)
1393 * Shows/Hides a window with an animation
1396 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
1398 FIXME("partial stub\n");
1400 /* If trying to show/hide and it's already *
1401 * shown/hidden or invalid window, fail with *
1402 * invalid parameter */
1403 if(!IsWindow(hwnd) ||
1404 (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
1405 (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
1407 SetLastError(ERROR_INVALID_PARAMETER);
1411 ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1416 /***********************************************************************
1417 * SetInternalWindowPos (USER32.@)
1419 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1420 LPRECT rect, LPPOINT pt )
1422 WINDOWPLACEMENT wndpl;
1425 wndpl.length = sizeof(wndpl);
1426 wndpl.showCmd = showCmd;
1427 wndpl.flags = flags = 0;
1432 wndpl.flags |= WPF_SETMINPOSITION;
1433 wndpl.ptMinPosition = *pt;
1437 flags |= PLACE_RECT;
1438 wndpl.rcNormalPosition = *rect;
1440 WINPOS_SetPlacement( hwnd, &wndpl, flags );
1444 /*******************************************************************
1445 * can_activate_window
1447 * Check if we can activate the specified window.
1449 static BOOL can_activate_window( HWND hwnd )
1453 if (!hwnd) return FALSE;
1454 style = GetWindowLongW( hwnd, GWL_STYLE );
1455 if (!(style & WS_VISIBLE)) return FALSE;
1456 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1457 return !(style & WS_DISABLED);
1461 /*******************************************************************
1462 * WINPOS_ActivateOtherWindow
1464 * Activates window other than pWnd.
1466 void WINPOS_ActivateOtherWindow(HWND hwnd)
1470 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1472 hwndTo = GetAncestor( hwndTo, GA_ROOT );
1473 if (can_activate_window( hwndTo )) goto done;
1479 if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1480 if (can_activate_window( hwndTo )) break;
1484 fg = GetForegroundWindow();
1485 TRACE("win = %p fg = %p\n", hwndTo, fg);
1486 if (!fg || (hwnd == fg))
1488 if (SetForegroundWindow( hwndTo )) return;
1490 if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1494 /***********************************************************************
1495 * WINPOS_HandleWindowPosChanging
1497 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1499 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1501 POINT minTrack, maxTrack;
1502 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1504 if (winpos->flags & SWP_NOSIZE) return 0;
1505 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1507 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1508 if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1509 if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1510 if (!(style & WS_MINIMIZE))
1512 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1513 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1520 /***********************************************************************
1523 static void dump_winpos_flags(UINT flags)
1525 static const DWORD dumped_flags = (SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOREDRAW |
1526 SWP_NOACTIVATE | SWP_FRAMECHANGED | SWP_SHOWWINDOW |
1527 SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOOWNERZORDER |
1528 SWP_NOSENDCHANGING | SWP_DEFERERASE | SWP_ASYNCWINDOWPOS |
1529 SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_STATECHANGED);
1531 if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1532 if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1533 if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1534 if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1535 if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1536 if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1537 if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1538 if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1539 if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1540 if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1541 if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1542 if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1543 if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1544 if(flags & SWP_NOCLIENTSIZE) TRACE(" SWP_NOCLIENTSIZE");
1545 if(flags & SWP_NOCLIENTMOVE) TRACE(" SWP_NOCLIENTMOVE");
1546 if(flags & SWP_STATECHANGED) TRACE(" SWP_STATECHANGED");
1548 if(flags & ~dumped_flags) TRACE(" %08x", flags & ~dumped_flags);
1552 /***********************************************************************
1553 * SWP_DoWinPosChanging
1555 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
1558 RECT window_rect, client_rect;
1560 /* Send WM_WINDOWPOSCHANGING message */
1562 if (!(pWinpos->flags & SWP_NOSENDCHANGING))
1563 SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
1565 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) ||
1566 wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
1568 /* Calculate new position and size */
1570 WIN_GetRectangles( pWinpos->hwnd, COORDS_PARENT, &window_rect, &client_rect );
1571 *pNewWindowRect = window_rect;
1572 *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? window_rect : client_rect;
1574 if (!(pWinpos->flags & SWP_NOSIZE))
1576 if (wndPtr->dwStyle & WS_MINIMIZE)
1578 pNewWindowRect->right = pNewWindowRect->left + GetSystemMetrics(SM_CXICON);
1579 pNewWindowRect->bottom = pNewWindowRect->top + GetSystemMetrics(SM_CYICON);
1583 pNewWindowRect->right = pNewWindowRect->left + pWinpos->cx;
1584 pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
1587 if (!(pWinpos->flags & SWP_NOMOVE))
1589 pNewWindowRect->left = pWinpos->x;
1590 pNewWindowRect->top = pWinpos->y;
1591 pNewWindowRect->right += pWinpos->x - window_rect.left;
1592 pNewWindowRect->bottom += pWinpos->y - window_rect.top;
1594 OffsetRect( pNewClientRect, pWinpos->x - window_rect.left,
1595 pWinpos->y - window_rect.top );
1597 pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
1599 TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
1600 pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
1601 pWinpos->cx, pWinpos->cy, pWinpos->flags );
1602 TRACE( "current %s style %08x new %s\n",
1603 wine_dbgstr_rect( &window_rect ), wndPtr->dwStyle,
1604 wine_dbgstr_rect( pNewWindowRect ));
1606 WIN_ReleasePtr( wndPtr );
1610 /***********************************************************************
1613 * Compute the valid rects from the old and new client rect and WVR_* flags.
1614 * Helper for WM_NCCALCSIZE handling.
1616 static inline void get_valid_rects( const RECT *old_client, const RECT *new_client, UINT flags,
1621 if (flags & WVR_REDRAW)
1623 SetRectEmpty( &valid[0] );
1624 SetRectEmpty( &valid[1] );
1628 if (flags & WVR_VALIDRECTS)
1630 if (!IntersectRect( &valid[0], &valid[0], new_client ) ||
1631 !IntersectRect( &valid[1], &valid[1], old_client ))
1633 SetRectEmpty( &valid[0] );
1634 SetRectEmpty( &valid[1] );
1637 flags = WVR_ALIGNLEFT | WVR_ALIGNTOP;
1641 valid[0] = *new_client;
1642 valid[1] = *old_client;
1645 /* make sure the rectangles have the same size */
1646 cx = min( valid[0].right - valid[0].left, valid[1].right - valid[1].left );
1647 cy = min( valid[0].bottom - valid[0].top, valid[1].bottom - valid[1].top );
1649 if (flags & WVR_ALIGNBOTTOM)
1651 valid[0].top = valid[0].bottom - cy;
1652 valid[1].top = valid[1].bottom - cy;
1656 valid[0].bottom = valid[0].top + cy;
1657 valid[1].bottom = valid[1].top + cy;
1659 if (flags & WVR_ALIGNRIGHT)
1661 valid[0].left = valid[0].right - cx;
1662 valid[1].left = valid[1].right - cx;
1666 valid[0].right = valid[0].left + cx;
1667 valid[1].right = valid[1].left + cx;
1672 /***********************************************************************
1675 * fix Z order taking into account owned popups -
1676 * basically we need to maintain them above the window that owns them
1678 * FIXME: hide/show owned popups when owner visibility changes.
1680 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
1682 HWND owner, *list = NULL;
1685 TRACE("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
1687 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) return hwndInsertAfter;
1689 if ((owner = GetWindow( hwnd, GW_OWNER )))
1691 /* make sure this popup stays above the owner */
1693 if (hwndInsertAfter != HWND_TOPMOST)
1695 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return hwndInsertAfter;
1697 for (i = 0; list[i]; i++)
1699 BOOL topmost = (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST) != 0;
1701 if (list[i] == owner)
1703 if (i > 0) hwndInsertAfter = list[i-1];
1704 else hwndInsertAfter = topmost ? HWND_TOPMOST : HWND_TOP;
1708 if (hwndInsertAfter == HWND_TOP || hwndInsertAfter == HWND_NOTOPMOST)
1710 if (!topmost) break;
1712 else if (list[i] == hwndInsertAfter) break;
1717 if (hwndInsertAfter == HWND_BOTTOM) goto done;
1718 if (!list && !(list = WIN_ListChildren( GetDesktopWindow() ))) goto done;
1721 if (hwndInsertAfter == HWND_TOP || hwndInsertAfter == HWND_NOTOPMOST)
1723 if (hwndInsertAfter == HWND_NOTOPMOST || !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_TOPMOST))
1725 /* skip all the topmost windows */
1726 while (list[i] && (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST)) i++;
1729 else if (hwndInsertAfter != HWND_TOPMOST)
1731 /* skip windows that are already placed correctly */
1732 for (i = 0; list[i]; i++)
1734 if (list[i] == hwndInsertAfter) break;
1735 if (list[i] == hwnd) goto done; /* nothing to do if window is moving backwards in z-order */
1739 for ( ; list[i]; i++)
1741 if (list[i] == hwnd) break;
1742 if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
1743 TRACE( "moving %p owned by %p after %p\n", list[i], hwnd, hwndInsertAfter );
1744 SetWindowPos( list[i], hwndInsertAfter, 0, 0, 0, 0,
1745 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING | SWP_DEFERERASE );
1746 hwndInsertAfter = list[i];
1750 HeapFree( GetProcessHeap(), 0, list );
1751 return hwndInsertAfter;
1754 /***********************************************************************
1757 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect,
1761 RECT window_rect, client_rect;
1763 WIN_GetRectangles( pWinpos->hwnd, COORDS_PARENT, &window_rect, &client_rect );
1765 /* Send WM_NCCALCSIZE message to get new client area */
1766 if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
1768 NCCALCSIZE_PARAMS params;
1769 WINDOWPOS winposCopy;
1771 params.rgrc[0] = *pNewWindowRect;
1772 params.rgrc[1] = window_rect;
1773 params.rgrc[2] = client_rect;
1774 params.lppos = &winposCopy;
1775 winposCopy = *pWinpos;
1777 wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)¶ms );
1779 *pNewClientRect = params.rgrc[0];
1781 TRACE( "hwnd %p old win %s old client %s new win %s new client %s\n", pWinpos->hwnd,
1782 wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(&client_rect),
1783 wine_dbgstr_rect(pNewWindowRect), wine_dbgstr_rect(pNewClientRect) );
1785 if( pNewClientRect->left != client_rect.left ||
1786 pNewClientRect->top != client_rect.top )
1787 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1789 if( (pNewClientRect->right - pNewClientRect->left !=
1790 client_rect.right - client_rect.left))
1791 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1793 wvrFlags &= ~WVR_HREDRAW;
1795 if (pNewClientRect->bottom - pNewClientRect->top !=
1796 client_rect.bottom - client_rect.top)
1797 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1799 wvrFlags &= ~WVR_VREDRAW;
1801 validRects[0] = params.rgrc[1];
1802 validRects[1] = params.rgrc[2];
1806 if (!(pWinpos->flags & SWP_NOMOVE) &&
1807 (pNewClientRect->left != client_rect.left ||
1808 pNewClientRect->top != client_rect.top))
1809 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1812 if (pWinpos->flags & (SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
1814 SetRectEmpty( &validRects[0] );
1815 SetRectEmpty( &validRects[1] );
1817 else get_valid_rects( &client_rect, pNewClientRect, wvrFlags, validRects );
1822 /* fix redundant flags and values in the WINDOWPOS structure */
1823 static BOOL fixup_flags( WINDOWPOS *winpos )
1827 WND *wndPtr = WIN_GetPtr( winpos->hwnd );
1830 if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
1832 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1835 winpos->hwnd = wndPtr->obj.handle; /* make it a full handle */
1837 /* Finally make sure that all coordinates are valid */
1838 if (winpos->x < -32768) winpos->x = -32768;
1839 else if (winpos->x > 32767) winpos->x = 32767;
1840 if (winpos->y < -32768) winpos->y = -32768;
1841 else if (winpos->y > 32767) winpos->y = 32767;
1843 if (winpos->cx < 0) winpos->cx = 0;
1844 else if (winpos->cx > 32767) winpos->cx = 32767;
1845 if (winpos->cy < 0) winpos->cy = 0;
1846 else if (winpos->cy > 32767) winpos->cy = 32767;
1848 parent = GetAncestor( winpos->hwnd, GA_PARENT );
1849 if (!IsWindowVisible( parent )) winpos->flags |= SWP_NOREDRAW;
1851 if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
1854 winpos->flags &= ~SWP_HIDEWINDOW;
1855 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
1858 WIN_GetRectangles( winpos->hwnd, COORDS_PARENT, &window_rect, NULL );
1859 if ((window_rect.right - window_rect.left == winpos->cx) &&
1860 (window_rect.bottom - window_rect.top == winpos->cy))
1861 winpos->flags |= SWP_NOSIZE; /* Already the right size */
1863 if ((window_rect.left == winpos->x) && (window_rect.top == winpos->y))
1864 winpos->flags |= SWP_NOMOVE; /* Already the right position */
1866 if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
1868 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)) && /* Bring to the top when activating */
1869 (winpos->flags & SWP_NOZORDER ||
1870 (winpos->hwndInsertAfter != HWND_TOPMOST && winpos->hwndInsertAfter != HWND_NOTOPMOST)))
1872 winpos->flags &= ~SWP_NOZORDER;
1873 winpos->hwndInsertAfter = HWND_TOP;
1877 /* Check hwndInsertAfter */
1878 if (winpos->flags & SWP_NOZORDER) goto done;
1880 /* fix sign extension */
1881 if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
1882 else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
1884 /* hwndInsertAfter must be a sibling of the window */
1885 if (winpos->hwndInsertAfter == HWND_TOP)
1887 if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1888 winpos->flags |= SWP_NOZORDER;
1890 else if (winpos->hwndInsertAfter == HWND_BOTTOM)
1892 if (!(wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
1893 winpos->flags |= SWP_NOZORDER;
1895 else if (winpos->hwndInsertAfter == HWND_TOPMOST)
1897 if ((wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1898 winpos->flags |= SWP_NOZORDER;
1900 else if (winpos->hwndInsertAfter == HWND_NOTOPMOST)
1902 if (!(wndPtr->dwExStyle & WS_EX_TOPMOST))
1903 winpos->flags |= SWP_NOZORDER;
1907 if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != parent) ret = FALSE;
1910 /* don't need to change the Zorder of hwnd if it's already inserted
1911 * after hwndInsertAfter or when inserting hwnd after itself.
1913 if ((winpos->hwnd == winpos->hwndInsertAfter) ||
1914 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
1915 winpos->flags |= SWP_NOZORDER;
1919 WIN_ReleasePtr( wndPtr );
1924 /***********************************************************************
1927 * Backend implementation of SetWindowPos.
1929 BOOL set_window_pos( HWND hwnd, HWND insert_after, UINT swp_flags,
1930 const RECT *window_rect, const RECT *client_rect, const RECT *valid_rects )
1934 RECT visible_rect, old_window_rect;
1936 visible_rect = *window_rect;
1937 USER_Driver->pWindowPosChanging( hwnd, insert_after, swp_flags,
1938 window_rect, client_rect, &visible_rect );
1940 WIN_GetRectangles( hwnd, COORDS_SCREEN, &old_window_rect, NULL );
1942 if (!(win = WIN_GetPtr( hwnd ))) return FALSE;
1943 if (win == WND_DESKTOP || win == WND_OTHER_PROCESS) return FALSE;
1945 SERVER_START_REQ( set_window_pos )
1947 req->handle = wine_server_user_handle( hwnd );
1948 req->previous = wine_server_user_handle( insert_after );
1949 req->flags = swp_flags;
1950 req->window.left = window_rect->left;
1951 req->window.top = window_rect->top;
1952 req->window.right = window_rect->right;
1953 req->window.bottom = window_rect->bottom;
1954 req->client.left = client_rect->left;
1955 req->client.top = client_rect->top;
1956 req->client.right = client_rect->right;
1957 req->client.bottom = client_rect->bottom;
1958 if (memcmp( window_rect, &visible_rect, sizeof(RECT) ) || !IsRectEmpty( &valid_rects[0] ))
1960 wine_server_add_data( req, &visible_rect, sizeof(visible_rect) );
1961 if (!IsRectEmpty( &valid_rects[0] ))
1962 wine_server_add_data( req, valid_rects, 2 * sizeof(*valid_rects) );
1964 if ((ret = !wine_server_call( req )))
1966 win->dwStyle = reply->new_style;
1967 win->dwExStyle = reply->new_ex_style;
1968 win->rectWindow = *window_rect;
1969 win->rectClient = *client_rect;
1970 if (GetWindowLongW( win->parent, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
1973 GetClientRect( win->parent, &client );
1974 mirror_rect( &client, &win->rectWindow );
1975 mirror_rect( &client, &win->rectClient );
1980 WIN_ReleasePtr( win );
1984 if (((swp_flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) ||
1985 (swp_flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW | SWP_STATECHANGED)))
1986 invalidate_dce( hwnd, &old_window_rect );
1988 USER_Driver->pWindowPosChanged( hwnd, insert_after, swp_flags, window_rect,
1989 client_rect, &visible_rect, valid_rects );
1995 /***********************************************************************
1998 * User32 internal function
2000 BOOL USER_SetWindowPos( WINDOWPOS * winpos )
2002 RECT newWindowRect, newClientRect, valid_rects[2];
2005 orig_flags = winpos->flags;
2007 /* First make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
2008 if (!(winpos->flags & SWP_NOMOVE))
2010 if (winpos->x < -32768) winpos->x = -32768;
2011 else if (winpos->x > 32767) winpos->x = 32767;
2012 if (winpos->y < -32768) winpos->y = -32768;
2013 else if (winpos->y > 32767) winpos->y = 32767;
2015 if (!(winpos->flags & SWP_NOSIZE))
2017 if (winpos->cx < 0) winpos->cx = 0;
2018 else if (winpos->cx > 32767) winpos->cx = 32767;
2019 if (winpos->cy < 0) winpos->cy = 0;
2020 else if (winpos->cy > 32767) winpos->cy = 32767;
2023 if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
2025 /* Fix redundant flags */
2026 if (!fixup_flags( winpos )) return FALSE;
2028 if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
2030 if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
2031 winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
2034 /* Common operations */
2036 SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect, valid_rects );
2038 if (!set_window_pos( winpos->hwnd, winpos->hwndInsertAfter, winpos->flags,
2039 &newWindowRect, &newClientRect, valid_rects ))
2042 /* erase parent when hiding or resizing child */
2043 if (!(orig_flags & SWP_DEFERERASE) &&
2044 ((orig_flags & SWP_HIDEWINDOW) ||
2045 (!(orig_flags & SWP_SHOWWINDOW) &&
2046 (winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOGEOMETRYCHANGE)))
2048 HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
2049 if (!parent || parent == GetDesktopWindow()) parent = winpos->hwnd;
2050 erase_now( parent, 0 );
2053 if( winpos->flags & SWP_HIDEWINDOW )
2054 HideCaret(winpos->hwnd);
2055 else if (winpos->flags & SWP_SHOWWINDOW)
2056 ShowCaret(winpos->hwnd);
2058 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)))
2060 /* child windows get WM_CHILDACTIVATE message */
2061 if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
2062 SendMessageW( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
2064 SetForegroundWindow( winpos->hwnd );
2067 /* And last, send the WM_WINDOWPOSCHANGED message */
2069 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
2071 if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
2073 /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
2074 and always contains final window position.
2076 winpos->x = newWindowRect.left;
2077 winpos->y = newWindowRect.top;
2078 winpos->cx = newWindowRect.right - newWindowRect.left;
2079 winpos->cy = newWindowRect.bottom - newWindowRect.top;
2080 SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
2085 /***********************************************************************
2086 * SetWindowPos (USER32.@)
2088 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
2089 INT x, INT y, INT cx, INT cy, UINT flags )
2093 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2094 hwnd, hwndInsertAfter, x, y, cx, cy, flags);
2095 if(TRACE_ON(win)) dump_winpos_flags(flags);
2097 if (is_broadcast(hwnd))
2099 SetLastError( ERROR_INVALID_PARAMETER );
2103 winpos.hwnd = WIN_GetFullHandle(hwnd);
2104 winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
2109 winpos.flags = flags;
2111 if (WIN_IsCurrentThread( hwnd ))
2112 return USER_SetWindowPos(&winpos);
2114 return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
2118 /***********************************************************************
2119 * BeginDeferWindowPos (USER32.@)
2121 HDWP WINAPI BeginDeferWindowPos( INT count )
2126 TRACE("%d\n", count);
2130 SetLastError(ERROR_INVALID_PARAMETER);
2133 /* Windows allows zero count, in which case it allocates context for 8 moves */
2134 if (count == 0) count = 8;
2136 if (!(pDWP = HeapAlloc( GetProcessHeap(), 0, sizeof(DWP)))) return 0;
2138 pDWP->actualCount = 0;
2139 pDWP->suggestedCount = count;
2140 pDWP->hwndParent = 0;
2142 if (!(pDWP->winPos = HeapAlloc( GetProcessHeap(), 0, count * sizeof(WINDOWPOS) )) ||
2143 !(handle = alloc_user_handle( &pDWP->obj, USER_DWP )))
2145 HeapFree( GetProcessHeap(), 0, pDWP->winPos );
2146 HeapFree( GetProcessHeap(), 0, pDWP );
2149 TRACE("returning hdwp %p\n", handle);
2154 /***********************************************************************
2155 * DeferWindowPos (USER32.@)
2157 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
2158 INT x, INT y, INT cx, INT cy,
2163 HDWP retvalue = hdwp;
2165 TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2166 hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
2168 hwnd = WIN_GetFullHandle( hwnd );
2169 if (is_desktop_window( hwnd )) return 0;
2171 if (!(pDWP = get_user_handle_ptr( hdwp, USER_DWP ))) return 0;
2172 if (pDWP == OBJ_OTHER_PROCESS)
2174 FIXME( "other process handle %p?\n", hdwp );
2178 for (i = 0; i < pDWP->actualCount; i++)
2180 if (pDWP->winPos[i].hwnd == hwnd)
2182 /* Merge with the other changes */
2183 if (!(flags & SWP_NOZORDER))
2185 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
2187 if (!(flags & SWP_NOMOVE))
2189 pDWP->winPos[i].x = x;
2190 pDWP->winPos[i].y = y;
2192 if (!(flags & SWP_NOSIZE))
2194 pDWP->winPos[i].cx = cx;
2195 pDWP->winPos[i].cy = cy;
2197 pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
2198 SWP_NOZORDER | SWP_NOREDRAW |
2199 SWP_NOACTIVATE | SWP_NOCOPYBITS|
2201 pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
2206 if (pDWP->actualCount >= pDWP->suggestedCount)
2208 WINDOWPOS *newpos = HeapReAlloc( GetProcessHeap(), 0, pDWP->winPos,
2209 pDWP->suggestedCount * 2 * sizeof(WINDOWPOS) );
2215 pDWP->suggestedCount *= 2;
2216 pDWP->winPos = newpos;
2218 pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
2219 pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
2220 pDWP->winPos[pDWP->actualCount].x = x;
2221 pDWP->winPos[pDWP->actualCount].y = y;
2222 pDWP->winPos[pDWP->actualCount].cx = cx;
2223 pDWP->winPos[pDWP->actualCount].cy = cy;
2224 pDWP->winPos[pDWP->actualCount].flags = flags;
2225 pDWP->actualCount++;
2227 release_user_handle_ptr( pDWP );
2232 /***********************************************************************
2233 * EndDeferWindowPos (USER32.@)
2235 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
2242 TRACE("%p\n", hdwp);
2244 if (!(pDWP = free_user_handle( hdwp, USER_DWP ))) return FALSE;
2245 if (pDWP == OBJ_OTHER_PROCESS)
2247 FIXME( "other process handle %p?\n", hdwp );
2251 for (i = 0, winpos = pDWP->winPos; res && i < pDWP->actualCount; i++, winpos++)
2253 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2254 winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
2255 winpos->cx, winpos->cy, winpos->flags);
2257 if (WIN_IsCurrentThread( winpos->hwnd ))
2258 res = USER_SetWindowPos( winpos );
2260 res = SendMessageW( winpos->hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)winpos );
2262 HeapFree( GetProcessHeap(), 0, pDWP->winPos );
2263 HeapFree( GetProcessHeap(), 0, pDWP );
2268 /***********************************************************************
2269 * ArrangeIconicWindows (USER32.@)
2271 UINT WINAPI ArrangeIconicWindows( HWND parent )
2275 INT x, y, xspacing, yspacing;
2277 GetClientRect( parent, &rectParent );
2278 x = rectParent.left;
2279 y = rectParent.bottom;
2280 xspacing = GetSystemMetrics(SM_CXICONSPACING);
2281 yspacing = GetSystemMetrics(SM_CYICONSPACING);
2283 hwndChild = GetWindow( parent, GW_CHILD );
2286 if( IsIconic( hwndChild ) )
2288 WINPOS_ShowIconTitle( hwndChild, FALSE );
2290 SetWindowPos( hwndChild, 0, x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
2291 y - yspacing - GetSystemMetrics(SM_CYICON)/2, 0, 0,
2292 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
2293 if( IsWindow(hwndChild) )
2294 WINPOS_ShowIconTitle(hwndChild , TRUE );
2296 if (x <= rectParent.right - xspacing) x += xspacing;
2299 x = rectParent.left;
2303 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
2309 /***********************************************************************
2312 * Draw the frame used when moving or resizing window.
2314 static void draw_moving_frame( HWND parent, HDC hdc, RECT *screen_rect, BOOL thickframe )
2316 RECT rect = *screen_rect;
2318 if (parent) MapWindowPoints( 0, parent, (POINT *)&rect, 2 );
2321 const int width = GetSystemMetrics(SM_CXFRAME);
2322 const int height = GetSystemMetrics(SM_CYFRAME);
2324 HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
2325 PatBlt( hdc, rect.left, rect.top,
2326 rect.right - rect.left - width, height, PATINVERT );
2327 PatBlt( hdc, rect.left, rect.top + height, width,
2328 rect.bottom - rect.top - height, PATINVERT );
2329 PatBlt( hdc, rect.left + width, rect.bottom - 1,
2330 rect.right - rect.left - width, -height, PATINVERT );
2331 PatBlt( hdc, rect.right - 1, rect.top, -width,
2332 rect.bottom - rect.top - height, PATINVERT );
2333 SelectObject( hdc, hbrush );
2335 else DrawFocusRect( hdc, &rect );
2339 /***********************************************************************
2342 * Initialization of a move or resize, when initiated from a menu choice.
2343 * Return hit test code for caption or sizing border.
2345 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
2352 GetWindowRect( hwnd, &rectWindow );
2354 if ((wParam & 0xfff0) == SC_MOVE)
2356 /* Move pointer at the center of the caption */
2357 RECT rect = rectWindow;
2358 /* Note: to be exactly centered we should take the different types
2359 * of border into account, but it shouldn't make more than a few pixels
2360 * of difference so let's not bother with that */
2361 rect.top += GetSystemMetrics(SM_CYBORDER);
2362 if (style & WS_SYSMENU)
2363 rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
2364 if (style & WS_MINIMIZEBOX)
2365 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
2366 if (style & WS_MAXIMIZEBOX)
2367 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
2368 pt.x = (rect.right + rect.left) / 2;
2369 pt.y = rect.top + GetSystemMetrics(SM_CYSIZE)/2;
2370 hittest = HTCAPTION;
2375 SetCursor( LoadCursorW( 0, (LPWSTR)IDC_SIZEALL ) );
2379 if (!GetMessageW( &msg, 0, 0, 0 )) return 0;
2380 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2385 pt.x = min( max( msg.pt.x, rectWindow.left ), rectWindow.right - 1 );
2386 pt.y = min( max( msg.pt.y, rectWindow.top ), rectWindow.bottom - 1 );
2387 hittest = SendMessageW( hwnd, WM_NCHITTEST, 0, MAKELONG( pt.x, pt.y ) );
2388 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT)) hittest = 0;
2399 pt.x =(rectWindow.left+rectWindow.right)/2;
2400 pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
2404 pt.x =(rectWindow.left+rectWindow.right)/2;
2405 pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
2409 pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
2410 pt.y =(rectWindow.top+rectWindow.bottom)/2;
2414 pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
2415 pt.y =(rectWindow.top+rectWindow.bottom)/2;
2423 TranslateMessage( &msg );
2424 DispatchMessageW( &msg );
2430 SetCursorPos( pt.x, pt.y );
2431 SendMessageW( hwnd, WM_SETCURSOR, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
2436 /***********************************************************************
2437 * WINPOS_SysCommandSizeMove
2439 * Perform SC_MOVE and SC_SIZE commands.
2441 void WINPOS_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
2444 RECT sizingRect, mouseRect, origRect;
2447 LONG hittest = (LONG)(wParam & 0x0f);
2448 WPARAM syscommand = wParam & 0xfff0;
2449 HCURSOR hDragCursor = 0, hOldCursor = 0;
2450 POINT minTrack, maxTrack;
2451 POINT capturePoint, pt;
2452 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
2453 BOOL thickframe = HAS_THICKFRAME( style );
2454 BOOL iconic = style & WS_MINIMIZE;
2456 DWORD dwPoint = GetMessagePos ();
2457 BOOL DragFullWindows = TRUE;
2459 if (IsZoomed(hwnd) || !IsWindowVisible(hwnd)) return;
2461 pt.x = (short)LOWORD(dwPoint);
2462 pt.y = (short)HIWORD(dwPoint);
2465 TRACE("hwnd %p command %04lx, hittest %d, pos %d,%d\n",
2466 hwnd, syscommand, hittest, pt.x, pt.y);
2468 if (syscommand == SC_MOVE)
2470 if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
2471 if (!hittest) return;
2475 if ( hittest && (syscommand != SC_MOUSEMENU) )
2476 hittest += (HTLEFT - WMSZ_LEFT);
2479 set_capture_window( hwnd, GUI_INMOVESIZE, NULL );
2480 hittest = start_size_move( hwnd, wParam, &capturePoint, style );
2483 set_capture_window( 0, GUI_INMOVESIZE, NULL );
2489 /* Get min/max info */
2491 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
2492 WIN_GetRectangles( hwnd, COORDS_PARENT, &sizingRect, NULL );
2493 origRect = sizingRect;
2494 if (style & WS_CHILD)
2496 parent = GetParent(hwnd);
2497 GetClientRect( parent, &mouseRect );
2498 MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
2499 MapWindowPoints( parent, 0, (LPPOINT)&sizingRect, 2 );
2504 GetClientRect( GetDesktopWindow(), &mouseRect );
2505 mouseRect.left = GetSystemMetrics( SM_XVIRTUALSCREEN );
2506 mouseRect.top = GetSystemMetrics( SM_YVIRTUALSCREEN );
2507 mouseRect.right = mouseRect.left + GetSystemMetrics( SM_CXVIRTUALSCREEN );
2508 mouseRect.bottom = mouseRect.top + GetSystemMetrics( SM_CYVIRTUALSCREEN );
2511 if (ON_LEFT_BORDER(hittest))
2513 mouseRect.left = max( mouseRect.left, sizingRect.right-maxTrack.x );
2514 mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
2516 else if (ON_RIGHT_BORDER(hittest))
2518 mouseRect.left = max( mouseRect.left, sizingRect.left+minTrack.x );
2519 mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
2521 if (ON_TOP_BORDER(hittest))
2523 mouseRect.top = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
2524 mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
2526 else if (ON_BOTTOM_BORDER(hittest))
2528 mouseRect.top = max( mouseRect.top, sizingRect.top+minTrack.y );
2529 mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
2532 /* Retrieve a default cache DC (without using the window style) */
2533 hdc = GetDCEx( parent, 0, DCX_CACHE );
2535 if( iconic ) /* create a cursor for dragging */
2537 hDragCursor = (HCURSOR)GetClassLongPtrW( hwnd, GCLP_HICON);
2538 if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageW( hwnd, WM_QUERYDRAGICON, 0, 0L);
2539 if( !hDragCursor ) iconic = FALSE;
2542 /* we only allow disabling the full window drag for child windows */
2543 if (parent) SystemParametersInfoW( SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0 );
2545 /* repaint the window before moving it around */
2546 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
2548 SendMessageW( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
2549 set_capture_window( hwnd, GUI_INMOVESIZE, NULL );
2555 if (!GetMessageW( &msg, 0, 0, 0 )) break;
2556 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2558 /* Exit on button-up, Return, or Esc */
2559 if ((msg.message == WM_LBUTTONUP) ||
2560 ((msg.message == WM_KEYDOWN) &&
2561 ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
2563 if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
2565 TranslateMessage( &msg );
2566 DispatchMessageW( &msg );
2567 continue; /* We are not interested in other messages */
2572 if (msg.message == WM_KEYDOWN) switch(msg.wParam)
2574 case VK_UP: pt.y -= 8; break;
2575 case VK_DOWN: pt.y += 8; break;
2576 case VK_LEFT: pt.x -= 8; break;
2577 case VK_RIGHT: pt.x += 8; break;
2580 pt.x = max( pt.x, mouseRect.left );
2581 pt.x = min( pt.x, mouseRect.right );
2582 pt.y = max( pt.y, mouseRect.top );
2583 pt.y = min( pt.y, mouseRect.bottom );
2585 dx = pt.x - capturePoint.x;
2586 dy = pt.y - capturePoint.y;
2594 if( iconic ) /* ok, no system popup tracking */
2596 hOldCursor = SetCursor(hDragCursor);
2598 WINPOS_ShowIconTitle( hwnd, FALSE );
2600 else if(!DragFullWindows)
2601 draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2604 if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
2607 WPARAM wpSizingHit = 0;
2609 if(!iconic && !DragFullWindows) draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2610 if (hittest == HTCAPTION) OffsetRect( &sizingRect, dx, dy );
2611 if (ON_LEFT_BORDER(hittest)) sizingRect.left += dx;
2612 else if (ON_RIGHT_BORDER(hittest)) sizingRect.right += dx;
2613 if (ON_TOP_BORDER(hittest)) sizingRect.top += dy;
2614 else if (ON_BOTTOM_BORDER(hittest)) sizingRect.bottom += dy;
2617 /* determine the hit location */
2618 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
2619 wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
2620 SendMessageW( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&sizingRect );
2624 if(!DragFullWindows)
2625 draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2628 RECT rect = sizingRect;
2629 MapWindowPoints( 0, parent, (POINT *)&rect, 2 );
2630 SetWindowPos( hwnd, 0, rect.left, rect.top,
2631 rect.right - rect.left, rect.bottom - rect.top,
2632 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2641 if( moved ) /* restore cursors, show icon title later on */
2643 ShowCursor( FALSE );
2644 SetCursor( hOldCursor );
2647 else if (moved && !DragFullWindows)
2649 draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2652 set_capture_window( 0, GUI_INMOVESIZE, NULL );
2653 ReleaseDC( parent, hdc );
2654 if (parent) MapWindowPoints( 0, parent, (POINT *)&sizingRect, 2 );
2656 if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
2659 SendMessageW( hwnd, WM_EXITSIZEMOVE, 0, 0 );
2660 SendMessageW( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
2662 /* window moved or resized */
2665 /* if the moving/resizing isn't canceled call SetWindowPos
2666 * with the new position or the new size of the window
2668 if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
2670 /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
2671 if(!DragFullWindows || iconic)
2672 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
2673 sizingRect.right - sizingRect.left,
2674 sizingRect.bottom - sizingRect.top,
2675 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2678 { /* restore previous size/position */
2680 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
2681 origRect.right - origRect.left,
2682 origRect.bottom - origRect.top,
2683 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2689 /* Single click brings up the system menu when iconized */
2693 if(style & WS_SYSMENU )
2694 SendMessageW( hwnd, WM_SYSCOMMAND,
2695 SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
2697 else WINPOS_ShowIconTitle( hwnd, TRUE );