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 );
229 /***********************************************************************
230 * GetClientRect (USER32.@)
232 BOOL WINAPI GetClientRect( HWND hwnd, LPRECT rect )
234 return WIN_GetRectangles( hwnd, COORDS_CLIENT, NULL, rect );
238 /*******************************************************************
239 * ClientToScreen (USER32.@)
241 BOOL WINAPI ClientToScreen( HWND hwnd, LPPOINT lppnt )
243 MapWindowPoints( hwnd, 0, lppnt, 1 );
248 /*******************************************************************
249 * ScreenToClient (USER32.@)
251 BOOL WINAPI ScreenToClient( HWND hwnd, LPPOINT lppnt )
253 MapWindowPoints( 0, hwnd, lppnt, 1 );
258 /***********************************************************************
259 * list_children_from_point
261 * Get the list of children that can contain point from the server.
262 * Point is in screen coordinates.
263 * Returned list must be freed by caller.
265 static HWND *list_children_from_point( HWND hwnd, POINT pt )
274 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
276 SERVER_START_REQ( get_window_children_from_point )
278 req->parent = wine_server_user_handle( hwnd );
281 wine_server_set_reply( req, list, (size-1) * sizeof(user_handle_t) );
282 if (!wine_server_call( req )) count = reply->count;
285 if (count && count < size)
287 /* start from the end since HWND is potentially larger than user_handle_t */
288 for (i = count - 1; i >= 0; i--)
289 list[i] = wine_server_ptr_handle( ((user_handle_t *)list)[i] );
293 HeapFree( GetProcessHeap(), 0, list );
295 size = count + 1; /* restart with a large enough buffer */
301 /***********************************************************************
302 * WINPOS_WindowFromPoint
304 * Find the window and hittest for a given point.
306 HWND WINPOS_WindowFromPoint( HWND hwndScope, POINT pt, INT *hittest )
311 if (!hwndScope) hwndScope = GetDesktopWindow();
313 *hittest = HTNOWHERE;
315 if (!(list = list_children_from_point( hwndScope, pt ))) return 0;
317 /* now determine the hittest */
319 for (i = 0; list[i]; i++)
321 LONG style = GetWindowLongW( list[i], GWL_STYLE );
323 /* If window is minimized or disabled, return at once */
324 if (style & WS_MINIMIZE)
326 *hittest = HTCAPTION;
329 if (style & WS_DISABLED)
334 /* Send WM_NCCHITTEST (if same thread) */
335 if (!WIN_IsCurrentThread( list[i] ))
340 res = SendMessageW( list[i], WM_NCHITTEST, 0, MAKELONG(pt.x,pt.y) );
341 if (res != HTTRANSPARENT)
343 *hittest = res; /* Found the window */
346 /* continue search with next window in z-order */
349 HeapFree( GetProcessHeap(), 0, list );
350 TRACE( "scope %p (%d,%d) returning %p\n", hwndScope, pt.x, pt.y, ret );
355 /*******************************************************************
356 * WindowFromPoint (USER32.@)
358 HWND WINAPI WindowFromPoint( POINT pt )
361 return WINPOS_WindowFromPoint( 0, pt, &hittest );
365 /*******************************************************************
366 * ChildWindowFromPoint (USER32.@)
368 HWND WINAPI ChildWindowFromPoint( HWND hwndParent, POINT pt )
370 return ChildWindowFromPointEx( hwndParent, pt, CWP_ALL );
373 /*******************************************************************
374 * RealChildWindowFromPoint (USER32.@)
376 HWND WINAPI RealChildWindowFromPoint( HWND hwndParent, POINT pt )
378 return ChildWindowFromPointEx( hwndParent, pt, CWP_SKIPTRANSPARENT );
381 /*******************************************************************
382 * ChildWindowFromPointEx (USER32.@)
384 HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
386 /* pt is in the client coordinates */
392 GetClientRect( hwndParent, &rect );
393 if (!PtInRect( &rect, pt )) return 0;
394 if (!(list = WIN_ListChildren( hwndParent ))) return hwndParent;
396 for (i = 0; list[i]; i++)
398 if (!WIN_GetRectangles( list[i], COORDS_PARENT, &rect, NULL )) continue;
399 if (!PtInRect( &rect, pt )) continue;
400 if (uFlags & (CWP_SKIPINVISIBLE|CWP_SKIPDISABLED))
402 LONG style = GetWindowLongW( list[i], GWL_STYLE );
403 if ((uFlags & CWP_SKIPINVISIBLE) && !(style & WS_VISIBLE)) continue;
404 if ((uFlags & CWP_SKIPDISABLED) && (style & WS_DISABLED)) continue;
406 if (uFlags & CWP_SKIPTRANSPARENT)
408 if (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TRANSPARENT) continue;
413 HeapFree( GetProcessHeap(), 0, list );
414 if (!retvalue) retvalue = hwndParent;
419 /*******************************************************************
420 * WINPOS_GetWinOffset
422 * Calculate the offset between the origin of the two windows. Used
423 * to implement MapWindowPoints.
425 static void WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo, POINT *offset )
429 offset->x = offset->y = 0;
431 /* Translate source window origin to screen coords */
434 HWND hwnd = hwndFrom;
438 if (hwnd == hwndTo) return;
439 if (!(wndPtr = WIN_GetPtr( hwnd )))
441 ERR( "bad hwndFrom = %p\n", hwnd );
444 if (wndPtr == WND_DESKTOP) break;
445 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
448 offset->x += wndPtr->rectClient.left;
449 offset->y += wndPtr->rectClient.top;
451 hwnd = wndPtr->parent;
452 WIN_ReleasePtr( wndPtr );
456 /* Translate origin to destination window coords */
463 if (!(wndPtr = WIN_GetPtr( hwnd )))
465 ERR( "bad hwndTo = %p\n", hwnd );
468 if (wndPtr == WND_DESKTOP) break;
469 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
472 offset->x -= wndPtr->rectClient.left;
473 offset->y -= wndPtr->rectClient.top;
475 hwnd = wndPtr->parent;
476 WIN_ReleasePtr( wndPtr );
481 other_process: /* one of the parents may belong to another process, do it the hard way */
482 offset->x = offset->y = 0;
483 SERVER_START_REQ( get_windows_offset )
485 req->from = wine_server_user_handle( hwndFrom );
486 req->to = wine_server_user_handle( hwndTo );
487 if (!wine_server_call( req ))
489 offset->x = reply->x;
490 offset->y = reply->y;
497 /*******************************************************************
498 * MapWindowPoints (USER32.@)
500 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
504 WINPOS_GetWinOffset( hwndFrom, hwndTo, &offset );
511 return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
515 /***********************************************************************
516 * IsIconic (USER32.@)
518 BOOL WINAPI IsIconic(HWND hWnd)
520 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
524 /***********************************************************************
525 * IsZoomed (USER32.@)
527 BOOL WINAPI IsZoomed(HWND hWnd)
529 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
533 /*******************************************************************
534 * AllowSetForegroundWindow (USER32.@)
536 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
538 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
539 * implemented, then fix this function. */
544 /*******************************************************************
545 * LockSetForegroundWindow (USER32.@)
547 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
549 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
550 * implemented, then fix this function. */
555 /***********************************************************************
556 * BringWindowToTop (USER32.@)
558 BOOL WINAPI BringWindowToTop( HWND hwnd )
560 return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
564 /***********************************************************************
565 * MoveWindow (USER32.@)
567 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
570 int flags = SWP_NOZORDER | SWP_NOACTIVATE;
571 if (!repaint) flags |= SWP_NOREDRAW;
572 TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
573 return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
577 /***********************************************************************
578 * WINPOS_RedrawIconTitle
580 BOOL WINPOS_RedrawIconTitle( HWND hWnd )
583 WND *win = WIN_GetPtr( hWnd );
585 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
587 icon_title = win->icon_title;
588 WIN_ReleasePtr( win );
590 if (!icon_title) return FALSE;
591 SendMessageW( icon_title, WM_SHOWWINDOW, TRUE, 0 );
592 InvalidateRect( icon_title, NULL, TRUE );
596 /***********************************************************************
597 * WINPOS_ShowIconTitle
599 static BOOL WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
601 if (!GetPropA( hwnd, "__wine_x11_managed" ))
603 WND *win = WIN_GetPtr( hwnd );
606 TRACE("%p %i\n", hwnd, (bShow != 0) );
608 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP) return FALSE;
609 title = win->icon_title;
610 WIN_ReleasePtr( win );
616 title = ICONTITLE_Create( hwnd );
617 if (!(win = WIN_GetPtr( hwnd )) || win == WND_OTHER_PROCESS)
619 DestroyWindow( title );
622 win->icon_title = title;
623 WIN_ReleasePtr( win );
625 if (!IsWindowVisible(title))
627 SendMessageW( title, WM_SHOWWINDOW, TRUE, 0 );
628 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
629 SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
632 else if (title) ShowWindow( title, SW_HIDE );
637 /*******************************************************************
638 * WINPOS_GetMinMaxInfo
640 * Get the minimized and maximized information for a window.
642 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
643 POINT *minTrack, POINT *maxTrack )
648 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
650 LONG exstyle = GetWindowLongW( hwnd, GWL_EXSTYLE );
654 /* Compute default values */
656 GetWindowRect(hwnd, &rc);
657 MinMax.ptReserved.x = rc.left;
658 MinMax.ptReserved.y = rc.top;
660 if ((style & WS_CAPTION) == WS_CAPTION)
661 adjustedStyle = style & ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
663 adjustedStyle = style;
665 GetClientRect(GetAncestor(hwnd,GA_PARENT), &rc);
666 AdjustWindowRectEx(&rc, adjustedStyle, ((style & WS_POPUP) && GetMenu(hwnd)), exstyle);
671 MinMax.ptMaxSize.x = rc.right - rc.left;
672 MinMax.ptMaxSize.y = rc.bottom - rc.top;
673 if (style & (WS_DLGFRAME | WS_BORDER))
675 MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
676 MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
680 MinMax.ptMinTrackSize.x = 2 * xinc;
681 MinMax.ptMinTrackSize.y = 2 * yinc;
683 MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXMAXTRACK);
684 MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYMAXTRACK);
685 MinMax.ptMaxPosition.x = -xinc;
686 MinMax.ptMaxPosition.y = -yinc;
688 if ((win = WIN_GetPtr( hwnd )) && win != WND_DESKTOP && win != WND_OTHER_PROCESS)
690 if (!EMPTYPOINT(win->max_pos)) MinMax.ptMaxPosition = win->max_pos;
691 WIN_ReleasePtr( win );
694 SendMessageW( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
696 /* if the app didn't change the values, adapt them for the current monitor */
698 if ((monitor = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY )))
701 MONITORINFO mon_info;
703 mon_info.cbSize = sizeof(mon_info);
704 GetMonitorInfoW( monitor, &mon_info );
706 rc_work = mon_info.rcMonitor;
708 if (style & WS_MAXIMIZEBOX)
710 if ((style & WS_CAPTION) == WS_CAPTION || !(style & (WS_CHILD | WS_POPUP)))
711 rc_work = mon_info.rcWork;
714 if (MinMax.ptMaxSize.x == GetSystemMetrics(SM_CXSCREEN) + 2 * xinc &&
715 MinMax.ptMaxSize.y == GetSystemMetrics(SM_CYSCREEN) + 2 * yinc)
717 MinMax.ptMaxSize.x = (rc_work.right - rc_work.left) + 2 * xinc;
718 MinMax.ptMaxSize.y = (rc_work.bottom - rc_work.top) + 2 * yinc;
720 if (MinMax.ptMaxPosition.x == -xinc && MinMax.ptMaxPosition.y == -yinc)
722 MinMax.ptMaxPosition.x = rc_work.left - xinc;
723 MinMax.ptMaxPosition.y = rc_work.top - yinc;
727 /* Some sanity checks */
729 TRACE("%d %d / %d %d / %d %d / %d %d\n",
730 MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
731 MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
732 MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
733 MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
734 MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
735 MinMax.ptMinTrackSize.x );
736 MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
737 MinMax.ptMinTrackSize.y );
739 if (maxSize) *maxSize = MinMax.ptMaxSize;
740 if (maxPos) *maxPos = MinMax.ptMaxPosition;
741 if (minTrack) *minTrack = MinMax.ptMinTrackSize;
742 if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
746 /***********************************************************************
749 * Find a suitable place for an iconic window.
751 static POINT WINPOS_FindIconPos( HWND hwnd, POINT pt )
753 RECT rect, rectParent;
756 int xspacing, yspacing;
758 parent = GetAncestor( hwnd, GA_PARENT );
759 GetClientRect( parent, &rectParent );
760 if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
761 (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
762 return pt; /* The icon already has a suitable position */
764 xspacing = GetSystemMetrics(SM_CXICONSPACING);
765 yspacing = GetSystemMetrics(SM_CYICONSPACING);
767 /* Check if another icon already occupies this spot */
768 /* FIXME: this is completely inefficient */
770 hrgn = CreateRectRgn( 0, 0, 0, 0 );
771 tmp = CreateRectRgn( 0, 0, 0, 0 );
772 for (child = GetWindow( parent, GW_HWNDFIRST ); child; child = GetWindow( child, GW_HWNDNEXT ))
774 if (child == hwnd) continue;
775 if ((GetWindowLongW( child, GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != (WS_VISIBLE|WS_MINIMIZE))
777 if (WIN_GetRectangles( child, COORDS_PARENT, &rect, NULL ))
779 SetRectRgn( tmp, rect.left, rect.top, rect.right, rect.bottom );
780 CombineRgn( hrgn, hrgn, tmp, RGN_OR );
785 for (rect.bottom = rectParent.bottom; rect.bottom >= yspacing; rect.bottom -= yspacing)
787 for (rect.left = rectParent.left; rect.left <= rectParent.right - xspacing; rect.left += xspacing)
789 rect.right = rect.left + xspacing;
790 rect.top = rect.bottom - yspacing;
791 if (!RectInRegion( hrgn, &rect ))
793 /* No window was found, so it's OK for us */
794 pt.x = rect.left + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
795 pt.y = rect.top + (yspacing - GetSystemMetrics(SM_CYICON)) / 2;
796 DeleteObject( hrgn );
801 DeleteObject( hrgn );
807 /***********************************************************************
810 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
818 TRACE("%p %u\n", hwnd, cmd );
820 wpl.length = sizeof(wpl);
821 GetWindowPlacement( hwnd, &wpl );
823 if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
824 return SWP_NOSIZE | SWP_NOMOVE;
826 if (IsIconic( hwnd ))
830 case SW_SHOWMINNOACTIVE:
831 case SW_SHOWMINIMIZED:
832 case SW_FORCEMINIMIZE:
834 return SWP_NOSIZE | SWP_NOMOVE;
836 if (!SendMessageW( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
837 swpFlags |= SWP_NOCOPYBITS;
842 case SW_SHOWMINNOACTIVE:
843 case SW_SHOWMINIMIZED:
844 case SW_FORCEMINIMIZE:
846 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
847 if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
848 else wndPtr->flags &= ~WIN_RESTORE_MAX;
849 WIN_ReleasePtr( wndPtr );
851 old_style = WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
853 wpl.ptMinPosition = WINPOS_FindIconPos( hwnd, wpl.ptMinPosition );
855 if (!(old_style & WS_MINIMIZE)) swpFlags |= SWP_STATECHANGED;
856 SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
857 wpl.ptMinPosition.x + GetSystemMetrics(SM_CXICON),
858 wpl.ptMinPosition.y + GetSystemMetrics(SM_CYICON) );
859 swpFlags |= SWP_NOCOPYBITS;
863 old_style = GetWindowLongW( hwnd, GWL_STYLE );
864 if ((old_style & WS_MAXIMIZE) && (old_style & WS_VISIBLE)) return SWP_NOSIZE | SWP_NOMOVE;
866 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
868 old_style = WIN_SetStyle( hwnd, WS_MAXIMIZE, WS_MINIMIZE );
869 if (old_style & WS_MINIMIZE)
871 if ((wndPtr = WIN_GetPtr( hwnd )) && wndPtr != WND_OTHER_PROCESS)
873 wndPtr->flags |= WIN_RESTORE_MAX;
874 WIN_ReleasePtr( wndPtr );
876 WINPOS_ShowIconTitle( hwnd, FALSE );
879 if (!(old_style & WS_MAXIMIZE)) swpFlags |= SWP_STATECHANGED;
880 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y,
881 wpl.ptMaxPosition.x + size.x, wpl.ptMaxPosition.y + size.y );
884 case SW_SHOWNOACTIVATE:
885 if ((wndPtr = WIN_GetPtr( hwnd )) && wndPtr != WND_OTHER_PROCESS)
887 wndPtr->flags &= ~WIN_RESTORE_MAX;
888 WIN_ReleasePtr( wndPtr );
893 old_style = WIN_SetStyle( hwnd, 0, WS_MINIMIZE | WS_MAXIMIZE );
894 if (old_style & WS_MINIMIZE)
898 WINPOS_ShowIconTitle( hwnd, FALSE );
900 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
901 restore_max = (wndPtr->flags & WIN_RESTORE_MAX) != 0;
902 WIN_ReleasePtr( wndPtr );
905 /* Restore to maximized position */
906 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
907 WIN_SetStyle( hwnd, WS_MAXIMIZE, 0 );
908 swpFlags |= SWP_STATECHANGED;
909 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y,
910 wpl.ptMaxPosition.x + size.x, wpl.ptMaxPosition.y + size.y );
914 else if (!(old_style & WS_MAXIMIZE)) break;
916 swpFlags |= SWP_STATECHANGED;
918 /* Restore to normal position */
920 *rect = wpl.rcNormalPosition;
928 /***********************************************************************
931 * Implementation of ShowWindow and ShowWindowAsync.
933 static BOOL show_window( HWND hwnd, INT cmd )
937 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
938 BOOL wasVisible = (style & WS_VISIBLE) != 0;
939 BOOL showFlag = TRUE;
940 RECT newPos = {0, 0, 0, 0};
943 TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);
948 if (!wasVisible) return FALSE;
950 swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE;
951 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
954 case SW_SHOWMINNOACTIVE:
956 case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
957 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
959 case SW_SHOWMINIMIZED:
960 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
961 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
962 if ((style & WS_MINIMIZE) && wasVisible) return TRUE;
965 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
966 if (!wasVisible) swp |= SWP_SHOWWINDOW;
967 swp |= SWP_FRAMECHANGED;
968 swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
969 if ((style & WS_MAXIMIZE) && wasVisible) return TRUE;
973 swp |= SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
974 if (style & WS_CHILD) swp |= SWP_NOZORDER;
977 if (wasVisible) return TRUE;
978 swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
979 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
982 case SW_SHOWNOACTIVATE:
983 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
987 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
988 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
989 if (!wasVisible) swp |= SWP_SHOWWINDOW;
990 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
992 swp |= SWP_FRAMECHANGED;
993 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
997 if (wasVisible) return TRUE;
998 swp |= SWP_NOSIZE | SWP_NOMOVE;
1000 if (style & WS_CHILD && !(swp & SWP_STATECHANGED)) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1006 if ((showFlag != wasVisible || cmd == SW_SHOWNA) && cmd != SW_SHOWMAXIMIZED && !(swp & SWP_STATECHANGED))
1008 SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
1009 if (!IsWindow( hwnd )) return wasVisible;
1012 swp = USER_Driver->pShowWindow( hwnd, cmd, &newPos, swp );
1014 parent = GetAncestor( hwnd, GA_PARENT );
1015 if (parent && !IsWindowVisible( parent ) && !(swp & SWP_STATECHANGED))
1017 /* if parent is not visible simply toggle WS_VISIBLE and return */
1018 if (showFlag) WIN_SetStyle( hwnd, WS_VISIBLE, 0 );
1019 else WIN_SetStyle( hwnd, 0, WS_VISIBLE );
1022 SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1023 newPos.right - newPos.left, newPos.bottom - newPos.top, swp );
1029 WINPOS_ShowIconTitle( hwnd, FALSE );
1031 /* FIXME: This will cause the window to be activated irrespective
1032 * of whether it is owned by the same thread. Has to be done
1036 if (hwnd == GetActiveWindow())
1037 WINPOS_ActivateOtherWindow(hwnd);
1039 /* Revert focus to parent */
1040 hFocus = GetFocus();
1043 HWND parent = GetAncestor(hwnd, GA_PARENT);
1044 if (parent == GetDesktopWindow()) parent = 0;
1050 if (IsIconic(hwnd)) WINPOS_ShowIconTitle( hwnd, TRUE );
1052 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return wasVisible;
1054 if (wndPtr->flags & WIN_NEED_SIZE)
1056 /* should happen only in CreateWindowEx() */
1057 int wParam = SIZE_RESTORED;
1061 WIN_GetRectangles( hwnd, COORDS_PARENT, NULL, &client );
1062 lparam = MAKELONG( client.right - client.left, client.bottom - client.top );
1063 wndPtr->flags &= ~WIN_NEED_SIZE;
1064 if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1065 else if (wndPtr->dwStyle & WS_MINIMIZE)
1067 wParam = SIZE_MINIMIZED;
1070 WIN_ReleasePtr( wndPtr );
1072 SendMessageW( hwnd, WM_SIZE, wParam, lparam );
1073 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( client.left, client.top ));
1075 else WIN_ReleasePtr( wndPtr );
1077 /* if previous state was minimized Windows sets focus to the window */
1078 if (style & WS_MINIMIZE) SetFocus( hwnd );
1084 /***********************************************************************
1085 * ShowWindowAsync (USER32.@)
1087 * doesn't wait; returns immediately.
1088 * used by threads to toggle windows in other (possibly hanging) threads
1090 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
1094 if (is_broadcast(hwnd))
1096 SetLastError( ERROR_INVALID_PARAMETER );
1100 if ((full_handle = WIN_IsCurrentThread( hwnd )))
1101 return show_window( full_handle, cmd );
1103 return SendNotifyMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
1107 /***********************************************************************
1108 * ShowWindow (USER32.@)
1110 BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
1114 if (is_broadcast(hwnd))
1116 SetLastError( ERROR_INVALID_PARAMETER );
1119 if ((full_handle = WIN_IsCurrentThread( hwnd )))
1120 return show_window( full_handle, cmd );
1122 return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
1126 /***********************************************************************
1127 * GetInternalWindowPos (USER32.@)
1129 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
1132 WINDOWPLACEMENT wndpl;
1133 if (GetWindowPlacement( hwnd, &wndpl ))
1135 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
1136 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
1137 return wndpl.showCmd;
1143 /***********************************************************************
1144 * GetWindowPlacement (USER32.@)
1147 * Fails if wndpl->length of Win95 (!) apps is invalid.
1149 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
1151 WND *pWnd = WIN_GetPtr( hwnd );
1153 if (!pWnd) return FALSE;
1155 if (pWnd == WND_DESKTOP)
1157 wndpl->length = sizeof(*wndpl);
1158 wndpl->showCmd = SW_SHOWNORMAL;
1160 wndpl->ptMinPosition.x = -1;
1161 wndpl->ptMinPosition.y = -1;
1162 wndpl->ptMaxPosition.x = -1;
1163 wndpl->ptMaxPosition.y = -1;
1164 GetWindowRect( hwnd, &wndpl->rcNormalPosition );
1167 if (pWnd == WND_OTHER_PROCESS)
1169 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
1173 /* update the placement according to the current style */
1174 if (pWnd->dwStyle & WS_MINIMIZE)
1176 pWnd->min_pos.x = pWnd->rectWindow.left;
1177 pWnd->min_pos.y = pWnd->rectWindow.top;
1179 else if (pWnd->dwStyle & WS_MAXIMIZE)
1181 pWnd->max_pos.x = pWnd->rectWindow.left;
1182 pWnd->max_pos.y = pWnd->rectWindow.top;
1186 pWnd->normal_rect = pWnd->rectWindow;
1189 wndpl->length = sizeof(*wndpl);
1190 if( pWnd->dwStyle & WS_MINIMIZE )
1191 wndpl->showCmd = SW_SHOWMINIMIZED;
1193 wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
1194 if( pWnd->flags & WIN_RESTORE_MAX )
1195 wndpl->flags = WPF_RESTORETOMAXIMIZED;
1198 wndpl->ptMinPosition = pWnd->min_pos;
1199 wndpl->ptMaxPosition = pWnd->max_pos;
1200 wndpl->rcNormalPosition = pWnd->normal_rect;
1201 WIN_ReleasePtr( pWnd );
1203 TRACE( "%p: returning min %d,%d max %d,%d normal %s\n",
1204 hwnd, wndpl->ptMinPosition.x, wndpl->ptMinPosition.y,
1205 wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y,
1206 wine_dbgstr_rect(&wndpl->rcNormalPosition) );
1210 /* make sure the specified rect is visible on screen */
1211 static void make_rect_onscreen( RECT *rect )
1214 HMONITOR monitor = MonitorFromRect( rect, MONITOR_DEFAULTTONEAREST );
1216 info.cbSize = sizeof(info);
1217 if (!monitor || !GetMonitorInfoW( monitor, &info )) return;
1218 /* FIXME: map coordinates from rcWork to rcMonitor */
1219 if (rect->right <= info.rcWork.left)
1221 rect->right += info.rcWork.left - rect->left;
1222 rect->left = info.rcWork.left;
1224 else if (rect->left >= info.rcWork.right)
1226 rect->left += info.rcWork.right - rect->right;
1227 rect->right = info.rcWork.right;
1229 if (rect->bottom <= info.rcWork.top)
1231 rect->bottom += info.rcWork.top - rect->top;
1232 rect->top = info.rcWork.top;
1234 else if (rect->top >= info.rcWork.bottom)
1236 rect->top += info.rcWork.bottom - rect->bottom;
1237 rect->bottom = info.rcWork.bottom;
1241 /* make sure the specified point is visible on screen */
1242 static void make_point_onscreen( POINT *pt )
1246 SetRect( &rect, pt->x, pt->y, pt->x + 1, pt->y + 1 );
1247 make_rect_onscreen( &rect );
1253 /***********************************************************************
1254 * WINPOS_SetPlacement
1256 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
1259 WND *pWnd = WIN_GetPtr( hwnd );
1260 WINDOWPLACEMENT wp = *wndpl;
1262 if (flags & PLACE_MIN) make_point_onscreen( &wp.ptMinPosition );
1263 if (flags & PLACE_MAX) make_point_onscreen( &wp.ptMaxPosition );
1264 if (flags & PLACE_RECT) make_rect_onscreen( &wp.rcNormalPosition );
1266 TRACE( "%p: setting min %d,%d max %d,%d normal %s flags %x ajusted to min %d,%d max %d,%d normal %s\n",
1267 hwnd, wndpl->ptMinPosition.x, wndpl->ptMinPosition.y,
1268 wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y,
1269 wine_dbgstr_rect(&wndpl->rcNormalPosition), flags,
1270 wp.ptMinPosition.x, wp.ptMinPosition.y, wp.ptMaxPosition.x, wp.ptMaxPosition.y,
1271 wine_dbgstr_rect(&wp.rcNormalPosition) );
1273 if (!pWnd || pWnd == WND_OTHER_PROCESS || pWnd == WND_DESKTOP) return FALSE;
1275 if( flags & PLACE_MIN ) pWnd->min_pos = wp.ptMinPosition;
1276 if( flags & PLACE_MAX ) pWnd->max_pos = wp.ptMaxPosition;
1277 if( flags & PLACE_RECT) pWnd->normal_rect = wp.rcNormalPosition;
1279 style = pWnd->dwStyle;
1281 WIN_ReleasePtr( pWnd );
1283 if( style & WS_MINIMIZE )
1285 if (flags & PLACE_MIN)
1287 WINPOS_ShowIconTitle( hwnd, FALSE );
1288 SetWindowPos( hwnd, 0, wp.ptMinPosition.x, wp.ptMinPosition.y, 0, 0,
1289 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1292 else if( style & WS_MAXIMIZE )
1294 if (flags & PLACE_MAX)
1295 SetWindowPos( hwnd, 0, wp.ptMaxPosition.x, wp.ptMaxPosition.y, 0, 0,
1296 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1298 else if( flags & PLACE_RECT )
1299 SetWindowPos( hwnd, 0, wp.rcNormalPosition.left, wp.rcNormalPosition.top,
1300 wp.rcNormalPosition.right - wp.rcNormalPosition.left,
1301 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top,
1302 SWP_NOZORDER | SWP_NOACTIVATE );
1304 ShowWindow( hwnd, wndpl->showCmd );
1306 if (IsIconic( hwnd ))
1308 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE) WINPOS_ShowIconTitle( hwnd, TRUE );
1310 /* SDK: ...valid only the next time... */
1311 if( wndpl->flags & WPF_RESTORETOMAXIMIZED )
1313 pWnd = WIN_GetPtr( hwnd );
1314 if (pWnd && pWnd != WND_OTHER_PROCESS)
1316 pWnd->flags |= WIN_RESTORE_MAX;
1317 WIN_ReleasePtr( pWnd );
1325 /***********************************************************************
1326 * SetWindowPlacement (USER32.@)
1329 * Fails if wndpl->length of Win95 (!) apps is invalid.
1331 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
1333 UINT flags = PLACE_MAX | PLACE_RECT;
1334 if (!wpl) return FALSE;
1335 if (wpl->flags & WPF_SETMINPOSITION) flags |= PLACE_MIN;
1336 return WINPOS_SetPlacement( hwnd, wpl, flags );
1340 /***********************************************************************
1341 * AnimateWindow (USER32.@)
1342 * Shows/Hides a window with an animation
1345 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
1347 FIXME("partial stub\n");
1349 /* If trying to show/hide and it's already *
1350 * shown/hidden or invalid window, fail with *
1351 * invalid parameter */
1352 if(!IsWindow(hwnd) ||
1353 (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
1354 (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
1356 SetLastError(ERROR_INVALID_PARAMETER);
1360 ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1365 /***********************************************************************
1366 * SetInternalWindowPos (USER32.@)
1368 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1369 LPRECT rect, LPPOINT pt )
1371 WINDOWPLACEMENT wndpl;
1374 wndpl.length = sizeof(wndpl);
1375 wndpl.showCmd = showCmd;
1376 wndpl.flags = flags = 0;
1381 wndpl.flags |= WPF_SETMINPOSITION;
1382 wndpl.ptMinPosition = *pt;
1386 flags |= PLACE_RECT;
1387 wndpl.rcNormalPosition = *rect;
1389 WINPOS_SetPlacement( hwnd, &wndpl, flags );
1393 /*******************************************************************
1394 * can_activate_window
1396 * Check if we can activate the specified window.
1398 static BOOL can_activate_window( HWND hwnd )
1402 if (!hwnd) return FALSE;
1403 style = GetWindowLongW( hwnd, GWL_STYLE );
1404 if (!(style & WS_VISIBLE)) return FALSE;
1405 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1406 return !(style & WS_DISABLED);
1410 /*******************************************************************
1411 * WINPOS_ActivateOtherWindow
1413 * Activates window other than pWnd.
1415 void WINPOS_ActivateOtherWindow(HWND hwnd)
1419 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1421 hwndTo = GetAncestor( hwndTo, GA_ROOT );
1422 if (can_activate_window( hwndTo )) goto done;
1428 if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1429 if (can_activate_window( hwndTo )) break;
1433 fg = GetForegroundWindow();
1434 TRACE("win = %p fg = %p\n", hwndTo, fg);
1435 if (!fg || (hwnd == fg))
1437 if (SetForegroundWindow( hwndTo )) return;
1439 if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1443 /***********************************************************************
1444 * WINPOS_HandleWindowPosChanging
1446 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1448 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1450 POINT minTrack, maxTrack;
1451 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1453 if (winpos->flags & SWP_NOSIZE) return 0;
1454 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1456 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1457 if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1458 if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1459 if (!(style & WS_MINIMIZE))
1461 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1462 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1469 /***********************************************************************
1472 static void dump_winpos_flags(UINT flags)
1474 static const DWORD dumped_flags = (SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOREDRAW |
1475 SWP_NOACTIVATE | SWP_FRAMECHANGED | SWP_SHOWWINDOW |
1476 SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOOWNERZORDER |
1477 SWP_NOSENDCHANGING | SWP_DEFERERASE | SWP_ASYNCWINDOWPOS |
1478 SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_STATECHANGED);
1480 if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1481 if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1482 if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1483 if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1484 if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1485 if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1486 if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1487 if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1488 if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1489 if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1490 if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1491 if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1492 if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1493 if(flags & SWP_NOCLIENTSIZE) TRACE(" SWP_NOCLIENTSIZE");
1494 if(flags & SWP_NOCLIENTMOVE) TRACE(" SWP_NOCLIENTMOVE");
1495 if(flags & SWP_STATECHANGED) TRACE(" SWP_STATECHANGED");
1497 if(flags & ~dumped_flags) TRACE(" %08x", flags & ~dumped_flags);
1501 /***********************************************************************
1502 * SWP_DoWinPosChanging
1504 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
1507 RECT window_rect, client_rect;
1509 /* Send WM_WINDOWPOSCHANGING message */
1511 if (!(pWinpos->flags & SWP_NOSENDCHANGING))
1512 SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
1514 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) ||
1515 wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
1517 /* Calculate new position and size */
1519 WIN_GetRectangles( pWinpos->hwnd, COORDS_PARENT, &window_rect, &client_rect );
1520 *pNewWindowRect = window_rect;
1521 *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? window_rect : client_rect;
1523 if (!(pWinpos->flags & SWP_NOSIZE))
1525 if (wndPtr->dwStyle & WS_MINIMIZE)
1527 pNewWindowRect->right = pNewWindowRect->left + GetSystemMetrics(SM_CXICON);
1528 pNewWindowRect->bottom = pNewWindowRect->top + GetSystemMetrics(SM_CYICON);
1532 pNewWindowRect->right = pNewWindowRect->left + pWinpos->cx;
1533 pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
1536 if (!(pWinpos->flags & SWP_NOMOVE))
1538 pNewWindowRect->left = pWinpos->x;
1539 pNewWindowRect->top = pWinpos->y;
1540 pNewWindowRect->right += pWinpos->x - window_rect.left;
1541 pNewWindowRect->bottom += pWinpos->y - window_rect.top;
1543 OffsetRect( pNewClientRect, pWinpos->x - window_rect.left,
1544 pWinpos->y - window_rect.top );
1546 pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
1548 TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
1549 pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
1550 pWinpos->cx, pWinpos->cy, pWinpos->flags );
1551 TRACE( "current %s style %08x new %s\n",
1552 wine_dbgstr_rect( &window_rect ), wndPtr->dwStyle,
1553 wine_dbgstr_rect( pNewWindowRect ));
1555 WIN_ReleasePtr( wndPtr );
1559 /***********************************************************************
1562 * Compute the valid rects from the old and new client rect and WVR_* flags.
1563 * Helper for WM_NCCALCSIZE handling.
1565 static inline void get_valid_rects( const RECT *old_client, const RECT *new_client, UINT flags,
1570 if (flags & WVR_REDRAW)
1572 SetRectEmpty( &valid[0] );
1573 SetRectEmpty( &valid[1] );
1577 if (flags & WVR_VALIDRECTS)
1579 if (!IntersectRect( &valid[0], &valid[0], new_client ) ||
1580 !IntersectRect( &valid[1], &valid[1], old_client ))
1582 SetRectEmpty( &valid[0] );
1583 SetRectEmpty( &valid[1] );
1586 flags = WVR_ALIGNLEFT | WVR_ALIGNTOP;
1590 valid[0] = *new_client;
1591 valid[1] = *old_client;
1594 /* make sure the rectangles have the same size */
1595 cx = min( valid[0].right - valid[0].left, valid[1].right - valid[1].left );
1596 cy = min( valid[0].bottom - valid[0].top, valid[1].bottom - valid[1].top );
1598 if (flags & WVR_ALIGNBOTTOM)
1600 valid[0].top = valid[0].bottom - cy;
1601 valid[1].top = valid[1].bottom - cy;
1605 valid[0].bottom = valid[0].top + cy;
1606 valid[1].bottom = valid[1].top + cy;
1608 if (flags & WVR_ALIGNRIGHT)
1610 valid[0].left = valid[0].right - cx;
1611 valid[1].left = valid[1].right - cx;
1615 valid[0].right = valid[0].left + cx;
1616 valid[1].right = valid[1].left + cx;
1621 /***********************************************************************
1624 * fix Z order taking into account owned popups -
1625 * basically we need to maintain them above the window that owns them
1627 * FIXME: hide/show owned popups when owner visibility changes.
1629 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
1631 HWND owner, *list = NULL;
1634 TRACE("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
1636 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) return hwndInsertAfter;
1638 if ((owner = GetWindow( hwnd, GW_OWNER )))
1640 /* make sure this popup stays above the owner */
1642 if (hwndInsertAfter != HWND_TOPMOST)
1644 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return hwndInsertAfter;
1646 for (i = 0; list[i]; i++)
1648 BOOL topmost = (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST) != 0;
1650 if (list[i] == owner)
1652 if (i > 0) hwndInsertAfter = list[i-1];
1653 else hwndInsertAfter = topmost ? HWND_TOPMOST : HWND_TOP;
1657 if (hwndInsertAfter == HWND_TOP || hwndInsertAfter == HWND_NOTOPMOST)
1659 if (!topmost) break;
1661 else if (list[i] == hwndInsertAfter) break;
1666 if (hwndInsertAfter == HWND_BOTTOM) goto done;
1667 if (!list && !(list = WIN_ListChildren( GetDesktopWindow() ))) goto done;
1670 if (hwndInsertAfter == HWND_TOP || hwndInsertAfter == HWND_NOTOPMOST)
1672 if (hwndInsertAfter == HWND_NOTOPMOST || !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_TOPMOST))
1674 /* skip all the topmost windows */
1675 while (list[i] && (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST)) i++;
1678 else if (hwndInsertAfter != HWND_TOPMOST)
1680 /* skip windows that are already placed correctly */
1681 for (i = 0; list[i]; i++)
1683 if (list[i] == hwndInsertAfter) break;
1684 if (list[i] == hwnd) goto done; /* nothing to do if window is moving backwards in z-order */
1688 for ( ; list[i]; i++)
1690 if (list[i] == hwnd) break;
1691 if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
1692 TRACE( "moving %p owned by %p after %p\n", list[i], hwnd, hwndInsertAfter );
1693 SetWindowPos( list[i], hwndInsertAfter, 0, 0, 0, 0,
1694 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING | SWP_DEFERERASE );
1695 hwndInsertAfter = list[i];
1699 HeapFree( GetProcessHeap(), 0, list );
1700 return hwndInsertAfter;
1703 /***********************************************************************
1706 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect,
1710 RECT window_rect, client_rect;
1712 WIN_GetRectangles( pWinpos->hwnd, COORDS_PARENT, &window_rect, &client_rect );
1714 /* Send WM_NCCALCSIZE message to get new client area */
1715 if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
1717 NCCALCSIZE_PARAMS params;
1718 WINDOWPOS winposCopy;
1720 params.rgrc[0] = *pNewWindowRect;
1721 params.rgrc[1] = window_rect;
1722 params.rgrc[2] = client_rect;
1723 params.lppos = &winposCopy;
1724 winposCopy = *pWinpos;
1726 wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)¶ms );
1728 *pNewClientRect = params.rgrc[0];
1730 TRACE( "hwnd %p old win %s old client %s new win %s new client %s\n", pWinpos->hwnd,
1731 wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(&client_rect),
1732 wine_dbgstr_rect(pNewWindowRect), wine_dbgstr_rect(pNewClientRect) );
1734 if( pNewClientRect->left != client_rect.left ||
1735 pNewClientRect->top != client_rect.top )
1736 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1738 if( (pNewClientRect->right - pNewClientRect->left !=
1739 client_rect.right - client_rect.left))
1740 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1742 wvrFlags &= ~WVR_HREDRAW;
1744 if (pNewClientRect->bottom - pNewClientRect->top !=
1745 client_rect.bottom - client_rect.top)
1746 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1748 wvrFlags &= ~WVR_VREDRAW;
1750 validRects[0] = params.rgrc[1];
1751 validRects[1] = params.rgrc[2];
1755 if (!(pWinpos->flags & SWP_NOMOVE) &&
1756 (pNewClientRect->left != client_rect.left ||
1757 pNewClientRect->top != client_rect.top))
1758 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1761 if (pWinpos->flags & (SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
1763 SetRectEmpty( &validRects[0] );
1764 SetRectEmpty( &validRects[1] );
1766 else get_valid_rects( &client_rect, pNewClientRect, wvrFlags, validRects );
1771 /* fix redundant flags and values in the WINDOWPOS structure */
1772 static BOOL fixup_flags( WINDOWPOS *winpos )
1776 WND *wndPtr = WIN_GetPtr( winpos->hwnd );
1779 if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
1781 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1784 winpos->hwnd = wndPtr->obj.handle; /* make it a full handle */
1786 /* Finally make sure that all coordinates are valid */
1787 if (winpos->x < -32768) winpos->x = -32768;
1788 else if (winpos->x > 32767) winpos->x = 32767;
1789 if (winpos->y < -32768) winpos->y = -32768;
1790 else if (winpos->y > 32767) winpos->y = 32767;
1792 if (winpos->cx < 0) winpos->cx = 0;
1793 else if (winpos->cx > 32767) winpos->cx = 32767;
1794 if (winpos->cy < 0) winpos->cy = 0;
1795 else if (winpos->cy > 32767) winpos->cy = 32767;
1797 parent = GetAncestor( winpos->hwnd, GA_PARENT );
1798 if (!IsWindowVisible( parent )) winpos->flags |= SWP_NOREDRAW;
1800 if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
1803 winpos->flags &= ~SWP_HIDEWINDOW;
1804 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
1807 WIN_GetRectangles( winpos->hwnd, COORDS_PARENT, &window_rect, NULL );
1808 if ((window_rect.right - window_rect.left == winpos->cx) &&
1809 (window_rect.bottom - window_rect.top == winpos->cy))
1810 winpos->flags |= SWP_NOSIZE; /* Already the right size */
1812 if ((window_rect.left == winpos->x) && (window_rect.top == winpos->y))
1813 winpos->flags |= SWP_NOMOVE; /* Already the right position */
1815 if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
1817 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)) && /* Bring to the top when activating */
1818 (winpos->flags & SWP_NOZORDER ||
1819 (winpos->hwndInsertAfter != HWND_TOPMOST && winpos->hwndInsertAfter != HWND_NOTOPMOST)))
1821 winpos->flags &= ~SWP_NOZORDER;
1822 winpos->hwndInsertAfter = HWND_TOP;
1826 /* Check hwndInsertAfter */
1827 if (winpos->flags & SWP_NOZORDER) goto done;
1829 /* fix sign extension */
1830 if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
1831 else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
1833 /* hwndInsertAfter must be a sibling of the window */
1834 if (winpos->hwndInsertAfter == HWND_TOP)
1836 if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1837 winpos->flags |= SWP_NOZORDER;
1839 else if (winpos->hwndInsertAfter == HWND_BOTTOM)
1841 if (!(wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
1842 winpos->flags |= SWP_NOZORDER;
1844 else if (winpos->hwndInsertAfter == HWND_TOPMOST)
1846 if ((wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1847 winpos->flags |= SWP_NOZORDER;
1849 else if (winpos->hwndInsertAfter == HWND_NOTOPMOST)
1851 if (!(wndPtr->dwExStyle & WS_EX_TOPMOST))
1852 winpos->flags |= SWP_NOZORDER;
1856 if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != parent) ret = FALSE;
1859 /* don't need to change the Zorder of hwnd if it's already inserted
1860 * after hwndInsertAfter or when inserting hwnd after itself.
1862 if ((winpos->hwnd == winpos->hwndInsertAfter) ||
1863 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
1864 winpos->flags |= SWP_NOZORDER;
1868 WIN_ReleasePtr( wndPtr );
1873 /***********************************************************************
1876 * Backend implementation of SetWindowPos.
1878 BOOL set_window_pos( HWND hwnd, HWND insert_after, UINT swp_flags,
1879 const RECT *window_rect, const RECT *client_rect, const RECT *valid_rects )
1883 RECT visible_rect, old_window_rect;
1885 visible_rect = *window_rect;
1886 USER_Driver->pWindowPosChanging( hwnd, insert_after, swp_flags,
1887 window_rect, client_rect, &visible_rect );
1889 WIN_GetRectangles( hwnd, COORDS_SCREEN, &old_window_rect, NULL );
1891 if (!(win = WIN_GetPtr( hwnd ))) return FALSE;
1892 if (win == WND_DESKTOP || win == WND_OTHER_PROCESS) return FALSE;
1894 SERVER_START_REQ( set_window_pos )
1896 req->handle = wine_server_user_handle( hwnd );
1897 req->previous = wine_server_user_handle( insert_after );
1898 req->flags = swp_flags;
1899 req->window.left = window_rect->left;
1900 req->window.top = window_rect->top;
1901 req->window.right = window_rect->right;
1902 req->window.bottom = window_rect->bottom;
1903 req->client.left = client_rect->left;
1904 req->client.top = client_rect->top;
1905 req->client.right = client_rect->right;
1906 req->client.bottom = client_rect->bottom;
1907 if (memcmp( window_rect, &visible_rect, sizeof(RECT) ) || !IsRectEmpty( &valid_rects[0] ))
1909 wine_server_add_data( req, &visible_rect, sizeof(visible_rect) );
1910 if (!IsRectEmpty( &valid_rects[0] ))
1911 wine_server_add_data( req, valid_rects, 2 * sizeof(*valid_rects) );
1913 if ((ret = !wine_server_call( req )))
1915 win->dwStyle = reply->new_style;
1916 win->dwExStyle = reply->new_ex_style;
1917 win->rectWindow = *window_rect;
1918 win->rectClient = *client_rect;
1922 WIN_ReleasePtr( win );
1926 if (((swp_flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) ||
1927 (swp_flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW | SWP_STATECHANGED)))
1928 invalidate_dce( hwnd, &old_window_rect );
1930 USER_Driver->pWindowPosChanged( hwnd, insert_after, swp_flags, window_rect,
1931 client_rect, &visible_rect, valid_rects );
1937 /***********************************************************************
1940 * User32 internal function
1942 BOOL USER_SetWindowPos( WINDOWPOS * winpos )
1944 RECT newWindowRect, newClientRect, valid_rects[2];
1947 orig_flags = winpos->flags;
1949 /* First make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
1950 if (!(winpos->flags & SWP_NOMOVE))
1952 if (winpos->x < -32768) winpos->x = -32768;
1953 else if (winpos->x > 32767) winpos->x = 32767;
1954 if (winpos->y < -32768) winpos->y = -32768;
1955 else if (winpos->y > 32767) winpos->y = 32767;
1957 if (!(winpos->flags & SWP_NOSIZE))
1959 if (winpos->cx < 0) winpos->cx = 0;
1960 else if (winpos->cx > 32767) winpos->cx = 32767;
1961 if (winpos->cy < 0) winpos->cy = 0;
1962 else if (winpos->cy > 32767) winpos->cy = 32767;
1965 if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
1967 /* Fix redundant flags */
1968 if (!fixup_flags( winpos )) return FALSE;
1970 if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
1972 if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
1973 winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
1976 /* Common operations */
1978 SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect, valid_rects );
1980 if (!set_window_pos( winpos->hwnd, winpos->hwndInsertAfter, winpos->flags,
1981 &newWindowRect, &newClientRect, valid_rects ))
1984 /* erase parent when hiding or resizing child */
1985 if (!(orig_flags & SWP_DEFERERASE) &&
1986 ((orig_flags & SWP_HIDEWINDOW) ||
1987 (!(orig_flags & SWP_SHOWWINDOW) &&
1988 (winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOGEOMETRYCHANGE)))
1990 HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
1991 if (!parent || parent == GetDesktopWindow()) parent = winpos->hwnd;
1992 erase_now( parent, 0 );
1995 if( winpos->flags & SWP_HIDEWINDOW )
1996 HideCaret(winpos->hwnd);
1997 else if (winpos->flags & SWP_SHOWWINDOW)
1998 ShowCaret(winpos->hwnd);
2000 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)))
2002 /* child windows get WM_CHILDACTIVATE message */
2003 if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
2004 SendMessageW( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
2006 SetForegroundWindow( winpos->hwnd );
2009 /* And last, send the WM_WINDOWPOSCHANGED message */
2011 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
2013 if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
2015 /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
2016 and always contains final window position.
2018 winpos->x = newWindowRect.left;
2019 winpos->y = newWindowRect.top;
2020 winpos->cx = newWindowRect.right - newWindowRect.left;
2021 winpos->cy = newWindowRect.bottom - newWindowRect.top;
2022 SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
2027 /***********************************************************************
2028 * SetWindowPos (USER32.@)
2030 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
2031 INT x, INT y, INT cx, INT cy, UINT flags )
2035 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2036 hwnd, hwndInsertAfter, x, y, cx, cy, flags);
2037 if(TRACE_ON(win)) dump_winpos_flags(flags);
2039 if (is_broadcast(hwnd))
2041 SetLastError( ERROR_INVALID_PARAMETER );
2045 winpos.hwnd = WIN_GetFullHandle(hwnd);
2046 winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
2051 winpos.flags = flags;
2053 if (WIN_IsCurrentThread( hwnd ))
2054 return USER_SetWindowPos(&winpos);
2056 return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
2060 /***********************************************************************
2061 * BeginDeferWindowPos (USER32.@)
2063 HDWP WINAPI BeginDeferWindowPos( INT count )
2068 TRACE("%d\n", count);
2072 SetLastError(ERROR_INVALID_PARAMETER);
2075 /* Windows allows zero count, in which case it allocates context for 8 moves */
2076 if (count == 0) count = 8;
2078 if (!(pDWP = HeapAlloc( GetProcessHeap(), 0, sizeof(DWP)))) return 0;
2080 pDWP->actualCount = 0;
2081 pDWP->suggestedCount = count;
2082 pDWP->hwndParent = 0;
2084 if (!(pDWP->winPos = HeapAlloc( GetProcessHeap(), 0, count * sizeof(WINDOWPOS) )) ||
2085 !(handle = alloc_user_handle( &pDWP->obj, USER_DWP )))
2087 HeapFree( GetProcessHeap(), 0, pDWP->winPos );
2088 HeapFree( GetProcessHeap(), 0, pDWP );
2091 TRACE("returning hdwp %p\n", handle);
2096 /***********************************************************************
2097 * DeferWindowPos (USER32.@)
2099 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
2100 INT x, INT y, INT cx, INT cy,
2105 HDWP retvalue = hdwp;
2107 TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2108 hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
2110 hwnd = WIN_GetFullHandle( hwnd );
2111 if (is_desktop_window( hwnd )) return 0;
2113 if (!(pDWP = get_user_handle_ptr( hdwp, USER_DWP ))) return 0;
2114 if (pDWP == OBJ_OTHER_PROCESS)
2116 FIXME( "other process handle %p?\n", hdwp );
2120 for (i = 0; i < pDWP->actualCount; i++)
2122 if (pDWP->winPos[i].hwnd == hwnd)
2124 /* Merge with the other changes */
2125 if (!(flags & SWP_NOZORDER))
2127 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
2129 if (!(flags & SWP_NOMOVE))
2131 pDWP->winPos[i].x = x;
2132 pDWP->winPos[i].y = y;
2134 if (!(flags & SWP_NOSIZE))
2136 pDWP->winPos[i].cx = cx;
2137 pDWP->winPos[i].cy = cy;
2139 pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
2140 SWP_NOZORDER | SWP_NOREDRAW |
2141 SWP_NOACTIVATE | SWP_NOCOPYBITS|
2143 pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
2148 if (pDWP->actualCount >= pDWP->suggestedCount)
2150 WINDOWPOS *newpos = HeapReAlloc( GetProcessHeap(), 0, pDWP->winPos,
2151 pDWP->suggestedCount * 2 * sizeof(WINDOWPOS) );
2157 pDWP->suggestedCount *= 2;
2158 pDWP->winPos = newpos;
2160 pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
2161 pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
2162 pDWP->winPos[pDWP->actualCount].x = x;
2163 pDWP->winPos[pDWP->actualCount].y = y;
2164 pDWP->winPos[pDWP->actualCount].cx = cx;
2165 pDWP->winPos[pDWP->actualCount].cy = cy;
2166 pDWP->winPos[pDWP->actualCount].flags = flags;
2167 pDWP->actualCount++;
2169 release_user_handle_ptr( pDWP );
2174 /***********************************************************************
2175 * EndDeferWindowPos (USER32.@)
2177 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
2184 TRACE("%p\n", hdwp);
2186 if (!(pDWP = free_user_handle( hdwp, USER_DWP ))) return FALSE;
2187 if (pDWP == OBJ_OTHER_PROCESS)
2189 FIXME( "other process handle %p?\n", hdwp );
2193 for (i = 0, winpos = pDWP->winPos; res && i < pDWP->actualCount; i++, winpos++)
2195 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2196 winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
2197 winpos->cx, winpos->cy, winpos->flags);
2199 if (WIN_IsCurrentThread( winpos->hwnd ))
2200 res = USER_SetWindowPos( winpos );
2202 res = SendMessageW( winpos->hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)winpos );
2204 HeapFree( GetProcessHeap(), 0, pDWP->winPos );
2205 HeapFree( GetProcessHeap(), 0, pDWP );
2210 /***********************************************************************
2211 * ArrangeIconicWindows (USER32.@)
2213 UINT WINAPI ArrangeIconicWindows( HWND parent )
2217 INT x, y, xspacing, yspacing;
2219 GetClientRect( parent, &rectParent );
2220 x = rectParent.left;
2221 y = rectParent.bottom;
2222 xspacing = GetSystemMetrics(SM_CXICONSPACING);
2223 yspacing = GetSystemMetrics(SM_CYICONSPACING);
2225 hwndChild = GetWindow( parent, GW_CHILD );
2228 if( IsIconic( hwndChild ) )
2230 WINPOS_ShowIconTitle( hwndChild, FALSE );
2232 SetWindowPos( hwndChild, 0, x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
2233 y - yspacing - GetSystemMetrics(SM_CYICON)/2, 0, 0,
2234 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
2235 if( IsWindow(hwndChild) )
2236 WINPOS_ShowIconTitle(hwndChild , TRUE );
2238 if (x <= rectParent.right - xspacing) x += xspacing;
2241 x = rectParent.left;
2245 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
2251 /***********************************************************************
2254 * Draw the frame used when moving or resizing window.
2256 static void draw_moving_frame( HDC hdc, RECT *rect, BOOL thickframe )
2260 const int width = GetSystemMetrics(SM_CXFRAME);
2261 const int height = GetSystemMetrics(SM_CYFRAME);
2263 HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
2264 PatBlt( hdc, rect->left, rect->top,
2265 rect->right - rect->left - width, height, PATINVERT );
2266 PatBlt( hdc, rect->left, rect->top + height, width,
2267 rect->bottom - rect->top - height, PATINVERT );
2268 PatBlt( hdc, rect->left + width, rect->bottom - 1,
2269 rect->right - rect->left - width, -height, PATINVERT );
2270 PatBlt( hdc, rect->right - 1, rect->top, -width,
2271 rect->bottom - rect->top - height, PATINVERT );
2272 SelectObject( hdc, hbrush );
2274 else DrawFocusRect( hdc, rect );
2278 /***********************************************************************
2281 * Initialization of a move or resize, when initiated from a menu choice.
2282 * Return hit test code for caption or sizing border.
2284 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
2291 GetWindowRect( hwnd, &rectWindow );
2293 if ((wParam & 0xfff0) == SC_MOVE)
2295 /* Move pointer at the center of the caption */
2296 RECT rect = rectWindow;
2297 /* Note: to be exactly centered we should take the different types
2298 * of border into account, but it shouldn't make more than a few pixels
2299 * of difference so let's not bother with that */
2300 rect.top += GetSystemMetrics(SM_CYBORDER);
2301 if (style & WS_SYSMENU)
2302 rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
2303 if (style & WS_MINIMIZEBOX)
2304 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
2305 if (style & WS_MAXIMIZEBOX)
2306 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
2307 pt.x = (rect.right + rect.left) / 2;
2308 pt.y = rect.top + GetSystemMetrics(SM_CYSIZE)/2;
2309 hittest = HTCAPTION;
2314 SetCursor( LoadCursorW( 0, (LPWSTR)IDC_SIZEALL ) );
2318 if (!GetMessageW( &msg, 0, 0, 0 )) return 0;
2319 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2324 pt.x = min( max( msg.pt.x, rectWindow.left ), rectWindow.right - 1 );
2325 pt.y = min( max( msg.pt.y, rectWindow.top ), rectWindow.bottom - 1 );
2326 hittest = SendMessageW( hwnd, WM_NCHITTEST, 0, MAKELONG( pt.x, pt.y ) );
2327 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT)) hittest = 0;
2338 pt.x =(rectWindow.left+rectWindow.right)/2;
2339 pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
2343 pt.x =(rectWindow.left+rectWindow.right)/2;
2344 pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
2348 pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
2349 pt.y =(rectWindow.top+rectWindow.bottom)/2;
2353 pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
2354 pt.y =(rectWindow.top+rectWindow.bottom)/2;
2362 TranslateMessage( &msg );
2363 DispatchMessageW( &msg );
2369 SetCursorPos( pt.x, pt.y );
2370 SendMessageW( hwnd, WM_SETCURSOR, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
2375 /***********************************************************************
2376 * WINPOS_SysCommandSizeMove
2378 * Perform SC_MOVE and SC_SIZE commands.
2380 void WINPOS_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
2383 RECT sizingRect, mouseRect, origRect;
2386 LONG hittest = (LONG)(wParam & 0x0f);
2387 WPARAM syscommand = wParam & 0xfff0;
2388 HCURSOR hDragCursor = 0, hOldCursor = 0;
2389 POINT minTrack, maxTrack;
2390 POINT capturePoint, pt;
2391 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
2392 BOOL thickframe = HAS_THICKFRAME( style );
2393 BOOL iconic = style & WS_MINIMIZE;
2395 DWORD dwPoint = GetMessagePos ();
2396 BOOL DragFullWindows = TRUE;
2398 if (IsZoomed(hwnd) || !IsWindowVisible(hwnd)) return;
2400 pt.x = (short)LOWORD(dwPoint);
2401 pt.y = (short)HIWORD(dwPoint);
2404 TRACE("hwnd %p command %04lx, hittest %d, pos %d,%d\n",
2405 hwnd, syscommand, hittest, pt.x, pt.y);
2407 if (syscommand == SC_MOVE)
2409 if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
2410 if (!hittest) return;
2414 if ( hittest && (syscommand != SC_MOUSEMENU) )
2415 hittest += (HTLEFT - WMSZ_LEFT);
2418 set_capture_window( hwnd, GUI_INMOVESIZE, NULL );
2419 hittest = start_size_move( hwnd, wParam, &capturePoint, style );
2422 set_capture_window( 0, GUI_INMOVESIZE, NULL );
2428 /* Get min/max info */
2430 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
2431 WIN_GetRectangles( hwnd, COORDS_PARENT, &sizingRect, NULL );
2432 if (style & WS_CHILD)
2434 parent = GetParent(hwnd);
2435 GetClientRect( parent, &mouseRect );
2440 GetClientRect( GetDesktopWindow(), &mouseRect );
2441 mouseRect.left = GetSystemMetrics( SM_XVIRTUALSCREEN );
2442 mouseRect.top = GetSystemMetrics( SM_YVIRTUALSCREEN );
2443 mouseRect.right = mouseRect.left + GetSystemMetrics( SM_CXVIRTUALSCREEN );
2444 mouseRect.bottom = mouseRect.top + GetSystemMetrics( SM_CYVIRTUALSCREEN );
2446 origRect = sizingRect;
2448 if (ON_LEFT_BORDER(hittest))
2450 mouseRect.left = max( mouseRect.left, sizingRect.right-maxTrack.x );
2451 mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
2453 else if (ON_RIGHT_BORDER(hittest))
2455 mouseRect.left = max( mouseRect.left, sizingRect.left+minTrack.x );
2456 mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
2458 if (ON_TOP_BORDER(hittest))
2460 mouseRect.top = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
2461 mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
2463 else if (ON_BOTTOM_BORDER(hittest))
2465 mouseRect.top = max( mouseRect.top, sizingRect.top+minTrack.y );
2466 mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
2468 if (parent) MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
2470 /* Retrieve a default cache DC (without using the window style) */
2471 hdc = GetDCEx( parent, 0, DCX_CACHE );
2473 if( iconic ) /* create a cursor for dragging */
2475 hDragCursor = (HCURSOR)GetClassLongPtrW( hwnd, GCLP_HICON);
2476 if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageW( hwnd, WM_QUERYDRAGICON, 0, 0L);
2477 if( !hDragCursor ) iconic = FALSE;
2480 /* we only allow disabling the full window drag for child windows */
2481 if (parent) SystemParametersInfoW( SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0 );
2483 /* repaint the window before moving it around */
2484 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
2486 SendMessageW( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
2487 set_capture_window( hwnd, GUI_INMOVESIZE, NULL );
2493 if (!GetMessageW( &msg, 0, 0, 0 )) break;
2494 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2496 /* Exit on button-up, Return, or Esc */
2497 if ((msg.message == WM_LBUTTONUP) ||
2498 ((msg.message == WM_KEYDOWN) &&
2499 ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
2501 if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
2503 TranslateMessage( &msg );
2504 DispatchMessageW( &msg );
2505 continue; /* We are not interested in other messages */
2510 if (msg.message == WM_KEYDOWN) switch(msg.wParam)
2512 case VK_UP: pt.y -= 8; break;
2513 case VK_DOWN: pt.y += 8; break;
2514 case VK_LEFT: pt.x -= 8; break;
2515 case VK_RIGHT: pt.x += 8; break;
2518 pt.x = max( pt.x, mouseRect.left );
2519 pt.x = min( pt.x, mouseRect.right );
2520 pt.y = max( pt.y, mouseRect.top );
2521 pt.y = min( pt.y, mouseRect.bottom );
2523 dx = pt.x - capturePoint.x;
2524 dy = pt.y - capturePoint.y;
2532 if( iconic ) /* ok, no system popup tracking */
2534 hOldCursor = SetCursor(hDragCursor);
2536 WINPOS_ShowIconTitle( hwnd, FALSE );
2538 else if(!DragFullWindows)
2539 draw_moving_frame( hdc, &sizingRect, thickframe );
2542 if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
2545 RECT newRect = sizingRect;
2546 WPARAM wpSizingHit = 0;
2548 if (hittest == HTCAPTION) OffsetRect( &newRect, dx, dy );
2549 if (ON_LEFT_BORDER(hittest)) newRect.left += dx;
2550 else if (ON_RIGHT_BORDER(hittest)) newRect.right += dx;
2551 if (ON_TOP_BORDER(hittest)) newRect.top += dy;
2552 else if (ON_BOTTOM_BORDER(hittest)) newRect.bottom += dy;
2553 if(!iconic && !DragFullWindows) draw_moving_frame( hdc, &sizingRect, thickframe );
2556 /* determine the hit location */
2557 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
2558 wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
2559 SendMessageW( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&newRect );
2563 if(!DragFullWindows)
2564 draw_moving_frame( hdc, &newRect, thickframe );
2566 SetWindowPos( hwnd, 0, newRect.left, newRect.top,
2567 newRect.right - newRect.left,
2568 newRect.bottom - newRect.top,
2569 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2571 sizingRect = newRect;
2578 if( moved ) /* restore cursors, show icon title later on */
2580 ShowCursor( FALSE );
2581 SetCursor( hOldCursor );
2584 else if (moved && !DragFullWindows)
2586 draw_moving_frame( hdc, &sizingRect, thickframe );
2589 set_capture_window( 0, GUI_INMOVESIZE, NULL );
2590 ReleaseDC( parent, hdc );
2592 if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
2595 SendMessageW( hwnd, WM_EXITSIZEMOVE, 0, 0 );
2596 SendMessageW( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
2598 /* window moved or resized */
2601 /* if the moving/resizing isn't canceled call SetWindowPos
2602 * with the new position or the new size of the window
2604 if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
2606 /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
2607 if(!DragFullWindows || iconic)
2608 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
2609 sizingRect.right - sizingRect.left,
2610 sizingRect.bottom - sizingRect.top,
2611 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2614 { /* restore previous size/position */
2616 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
2617 origRect.right - origRect.left,
2618 origRect.bottom - origRect.top,
2619 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2625 /* Single click brings up the system menu when iconized */
2629 if(style & WS_SYSMENU )
2630 SendMessageW( hwnd, WM_SYSCOMMAND,
2631 SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
2633 else WINPOS_ShowIconTitle( hwnd, TRUE );