user32: Store the window placement information directly in the main window structure.
[wine/wine-kai.git] / dlls / user32 / winpos.c
bloba2449d6cbcee233c8117067a88ef8c902bd356d5
1 /*
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdarg.h>
26 #include <string.h>
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winerror.h"
33 #include "wine/server.h"
34 #include "controls.h"
35 #include "user_private.h"
36 #include "win.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 PLACE_MIN 0x0001
59 #define PLACE_MAX 0x0002
60 #define PLACE_RECT 0x0004
63 #define DWP_MAGIC ((INT)('W' | ('P' << 8) | ('O' << 16) | ('S' << 24)))
65 typedef struct
67 INT actualCount;
68 INT suggestedCount;
69 BOOL valid;
70 INT wMagic;
71 HWND hwndParent;
72 WINDOWPOS winPos[1];
73 } DWP;
76 /***********************************************************************
77 * ArrangeIconicWindows (USER32.@)
79 UINT WINAPI ArrangeIconicWindows( HWND parent )
81 RECT rectParent;
82 HWND hwndChild;
83 INT x, y, xspacing, yspacing;
85 GetClientRect( parent, &rectParent );
86 x = rectParent.left;
87 y = rectParent.bottom;
88 xspacing = GetSystemMetrics(SM_CXICONSPACING);
89 yspacing = GetSystemMetrics(SM_CYICONSPACING);
91 hwndChild = GetWindow( parent, GW_CHILD );
92 while (hwndChild)
94 if( IsIconic( hwndChild ) )
96 WINPOS_ShowIconTitle( hwndChild, FALSE );
98 SetWindowPos( hwndChild, 0, x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
99 y - yspacing - GetSystemMetrics(SM_CYICON)/2, 0, 0,
100 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
101 if( IsWindow(hwndChild) )
102 WINPOS_ShowIconTitle(hwndChild , TRUE );
104 if (x <= rectParent.right - xspacing) x += xspacing;
105 else
107 x = rectParent.left;
108 y -= yspacing;
111 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
113 return yspacing;
117 /***********************************************************************
118 * SwitchToThisWindow (USER32.@)
120 void WINAPI SwitchToThisWindow( HWND hwnd, BOOL restore )
122 ShowWindow( hwnd, restore ? SW_RESTORE : SW_SHOWMINIMIZED );
126 /***********************************************************************
127 * GetWindowRect (USER32.@)
129 BOOL WINAPI GetWindowRect( HWND hwnd, LPRECT rect )
131 BOOL ret = WIN_GetRectangles( hwnd, rect, NULL );
132 if (ret)
134 MapWindowPoints( GetAncestor( hwnd, GA_PARENT ), 0, (POINT *)rect, 2 );
135 TRACE( "hwnd %p (%s)\n", hwnd, wine_dbgstr_rect(rect) );
137 return ret;
141 /***********************************************************************
142 * GetWindowRgn (USER32.@)
144 int WINAPI GetWindowRgn ( HWND hwnd, HRGN hrgn )
146 int nRet = ERROR;
147 NTSTATUS status;
148 HRGN win_rgn = 0;
149 RGNDATA *data;
150 size_t size = 256;
154 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
156 SetLastError( ERROR_OUTOFMEMORY );
157 return ERROR;
159 SERVER_START_REQ( get_window_region )
161 req->window = hwnd;
162 wine_server_set_reply( req, data->Buffer, size );
163 if (!(status = wine_server_call( req )))
165 size_t reply_size = wine_server_reply_size( reply );
166 if (reply_size)
168 data->rdh.dwSize = sizeof(data->rdh);
169 data->rdh.iType = RDH_RECTANGLES;
170 data->rdh.nCount = reply_size / sizeof(RECT);
171 data->rdh.nRgnSize = reply_size;
172 win_rgn = ExtCreateRegion( NULL, size, data );
175 else size = reply->total_size;
177 SERVER_END_REQ;
178 HeapFree( GetProcessHeap(), 0, data );
179 } while (status == STATUS_BUFFER_OVERFLOW);
181 if (status) SetLastError( RtlNtStatusToDosError(status) );
182 else if (win_rgn)
184 nRet = CombineRgn( hrgn, win_rgn, 0, RGN_COPY );
185 DeleteObject( win_rgn );
187 return nRet;
191 /***********************************************************************
192 * SetWindowRgn (USER32.@)
194 int WINAPI SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL bRedraw )
196 static const RECT empty_rect;
197 BOOL ret;
199 if (hrgn)
201 RGNDATA *data;
202 DWORD size;
204 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
205 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
206 if (!GetRegionData( hrgn, size, data ))
208 HeapFree( GetProcessHeap(), 0, data );
209 return FALSE;
211 SERVER_START_REQ( set_window_region )
213 req->window = hwnd;
214 req->redraw = (bRedraw != 0);
215 if (data->rdh.nCount)
216 wine_server_add_data( req, data->Buffer, data->rdh.nCount * sizeof(RECT) );
217 else
218 wine_server_add_data( req, &empty_rect, sizeof(empty_rect) );
219 ret = !wine_server_call_err( req );
221 SERVER_END_REQ;
223 else /* clear existing region */
225 SERVER_START_REQ( set_window_region )
227 req->window = hwnd;
228 req->redraw = (bRedraw != 0);
229 ret = !wine_server_call_err( req );
231 SERVER_END_REQ;
234 if (ret) ret = USER_Driver->pSetWindowRgn( hwnd, hrgn, bRedraw );
236 if (ret)
238 UINT swp_flags = SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE;
239 if (!bRedraw) swp_flags |= SWP_NOREDRAW;
240 SetWindowPos( hwnd, 0, 0, 0, 0, 0, swp_flags );
241 invalidate_dce( hwnd, NULL );
243 return ret;
247 /***********************************************************************
248 * GetClientRect (USER32.@)
250 BOOL WINAPI GetClientRect( HWND hwnd, LPRECT rect )
252 BOOL ret;
254 if ((ret = WIN_GetRectangles( hwnd, NULL, rect )))
256 rect->right -= rect->left;
257 rect->bottom -= rect->top;
258 rect->left = rect->top = 0;
260 return ret;
264 /*******************************************************************
265 * ClientToScreen (USER32.@)
267 BOOL WINAPI ClientToScreen( HWND hwnd, LPPOINT lppnt )
269 MapWindowPoints( hwnd, 0, lppnt, 1 );
270 return TRUE;
274 /*******************************************************************
275 * ScreenToClient (USER32.@)
277 BOOL WINAPI ScreenToClient( HWND hwnd, LPPOINT lppnt )
279 MapWindowPoints( 0, hwnd, lppnt, 1 );
280 return TRUE;
284 /***********************************************************************
285 * list_children_from_point
287 * Get the list of children that can contain point from the server.
288 * Point is in screen coordinates.
289 * Returned list must be freed by caller.
291 static HWND *list_children_from_point( HWND hwnd, POINT pt )
293 HWND *list;
294 int size = 32;
296 for (;;)
298 int count = 0;
300 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
302 SERVER_START_REQ( get_window_children_from_point )
304 req->parent = hwnd;
305 req->x = pt.x;
306 req->y = pt.y;
307 wine_server_set_reply( req, list, (size-1) * sizeof(HWND) );
308 if (!wine_server_call( req )) count = reply->count;
310 SERVER_END_REQ;
311 if (count && count < size)
313 list[count] = 0;
314 return list;
316 HeapFree( GetProcessHeap(), 0, list );
317 if (!count) break;
318 size = count + 1; /* restart with a large enough buffer */
320 return NULL;
324 /***********************************************************************
325 * WINPOS_WindowFromPoint
327 * Find the window and hittest for a given point.
329 HWND WINPOS_WindowFromPoint( HWND hwndScope, POINT pt, INT *hittest )
331 int i, res;
332 HWND ret, *list;
334 if (!hwndScope) hwndScope = GetDesktopWindow();
336 *hittest = HTNOWHERE;
338 if (!(list = list_children_from_point( hwndScope, pt ))) return 0;
340 /* now determine the hittest */
342 for (i = 0; list[i]; i++)
344 LONG style = GetWindowLongW( list[i], GWL_STYLE );
346 /* If window is minimized or disabled, return at once */
347 if (style & WS_MINIMIZE)
349 *hittest = HTCAPTION;
350 break;
352 if (style & WS_DISABLED)
354 *hittest = HTERROR;
355 break;
357 /* Send WM_NCCHITTEST (if same thread) */
358 if (!WIN_IsCurrentThread( list[i] ))
360 *hittest = HTCLIENT;
361 break;
363 res = SendMessageW( list[i], WM_NCHITTEST, 0, MAKELONG(pt.x,pt.y) );
364 if (res != HTTRANSPARENT)
366 *hittest = res; /* Found the window */
367 break;
369 /* continue search with next window in z-order */
371 ret = list[i];
372 HeapFree( GetProcessHeap(), 0, list );
373 TRACE( "scope %p (%d,%d) returning %p\n", hwndScope, pt.x, pt.y, ret );
374 return ret;
378 /*******************************************************************
379 * WindowFromPoint (USER32.@)
381 HWND WINAPI WindowFromPoint( POINT pt )
383 INT hittest;
384 return WINPOS_WindowFromPoint( 0, pt, &hittest );
388 /*******************************************************************
389 * ChildWindowFromPoint (USER32.@)
391 HWND WINAPI ChildWindowFromPoint( HWND hwndParent, POINT pt )
393 return ChildWindowFromPointEx( hwndParent, pt, CWP_ALL );
396 /*******************************************************************
397 * RealChildWindowFromPoint (USER32.@)
399 HWND WINAPI RealChildWindowFromPoint( HWND hwndParent, POINT pt )
401 return ChildWindowFromPointEx( hwndParent, pt, CWP_SKIPTRANSPARENT );
404 /*******************************************************************
405 * ChildWindowFromPointEx (USER32.@)
407 HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
409 /* pt is in the client coordinates */
410 HWND *list;
411 int i;
412 RECT rect;
413 HWND retvalue;
415 GetClientRect( hwndParent, &rect );
416 if (!PtInRect( &rect, pt )) return 0;
417 if (!(list = WIN_ListChildren( hwndParent ))) return hwndParent;
419 for (i = 0; list[i]; i++)
421 if (!WIN_GetRectangles( list[i], &rect, NULL )) continue;
422 if (!PtInRect( &rect, pt )) continue;
423 if (uFlags & (CWP_SKIPINVISIBLE|CWP_SKIPDISABLED))
425 LONG style = GetWindowLongW( list[i], GWL_STYLE );
426 if ((uFlags & CWP_SKIPINVISIBLE) && !(style & WS_VISIBLE)) continue;
427 if ((uFlags & CWP_SKIPDISABLED) && (style & WS_DISABLED)) continue;
429 if (uFlags & CWP_SKIPTRANSPARENT)
431 if (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TRANSPARENT) continue;
433 break;
435 retvalue = list[i];
436 HeapFree( GetProcessHeap(), 0, list );
437 if (!retvalue) retvalue = hwndParent;
438 return retvalue;
442 /*******************************************************************
443 * WINPOS_GetWinOffset
445 * Calculate the offset between the origin of the two windows. Used
446 * to implement MapWindowPoints.
448 static void WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo, POINT *offset )
450 WND * wndPtr;
452 offset->x = offset->y = 0;
454 /* Translate source window origin to screen coords */
455 if (hwndFrom)
457 HWND hwnd = hwndFrom;
459 while (hwnd)
461 if (hwnd == hwndTo) return;
462 if (!(wndPtr = WIN_GetPtr( hwnd )))
464 ERR( "bad hwndFrom = %p\n", hwnd );
465 return;
467 if (wndPtr == WND_DESKTOP) break;
468 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
469 offset->x += wndPtr->rectClient.left;
470 offset->y += wndPtr->rectClient.top;
471 hwnd = wndPtr->parent;
472 WIN_ReleasePtr( wndPtr );
476 /* Translate origin to destination window coords */
477 if (hwndTo)
479 HWND hwnd = hwndTo;
481 while (hwnd)
483 if (!(wndPtr = WIN_GetPtr( hwnd )))
485 ERR( "bad hwndTo = %p\n", hwnd );
486 return;
488 if (wndPtr == WND_DESKTOP) break;
489 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
490 offset->x -= wndPtr->rectClient.left;
491 offset->y -= wndPtr->rectClient.top;
492 hwnd = wndPtr->parent;
493 WIN_ReleasePtr( wndPtr );
496 return;
498 other_process: /* one of the parents may belong to another process, do it the hard way */
499 offset->x = offset->y = 0;
500 SERVER_START_REQ( get_windows_offset )
502 req->from = hwndFrom;
503 req->to = hwndTo;
504 if (!wine_server_call( req ))
506 offset->x = reply->x;
507 offset->y = reply->y;
510 SERVER_END_REQ;
514 /*******************************************************************
515 * MapWindowPoints (USER.258)
517 void WINAPI MapWindowPoints16( HWND16 hwndFrom, HWND16 hwndTo,
518 LPPOINT16 lppt, UINT16 count )
520 POINT offset;
522 WINPOS_GetWinOffset( WIN_Handle32(hwndFrom), WIN_Handle32(hwndTo), &offset );
523 while (count--)
525 lppt->x += offset.x;
526 lppt->y += offset.y;
527 lppt++;
532 /*******************************************************************
533 * MapWindowPoints (USER32.@)
535 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
537 POINT offset;
539 WINPOS_GetWinOffset( hwndFrom, hwndTo, &offset );
540 while (count--)
542 lppt->x += offset.x;
543 lppt->y += offset.y;
544 lppt++;
546 return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
550 /***********************************************************************
551 * IsIconic (USER32.@)
553 BOOL WINAPI IsIconic(HWND hWnd)
555 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
559 /***********************************************************************
560 * IsZoomed (USER32.@)
562 BOOL WINAPI IsZoomed(HWND hWnd)
564 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
568 /*******************************************************************
569 * AllowSetForegroundWindow (USER32.@)
571 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
573 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
574 * implemented, then fix this function. */
575 return TRUE;
579 /*******************************************************************
580 * LockSetForegroundWindow (USER32.@)
582 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
584 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
585 * implemented, then fix this function. */
586 return TRUE;
590 /***********************************************************************
591 * BringWindowToTop (USER32.@)
593 BOOL WINAPI BringWindowToTop( HWND hwnd )
595 return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
599 /***********************************************************************
600 * MoveWindow (USER32.@)
602 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
603 BOOL repaint )
605 int flags = SWP_NOZORDER | SWP_NOACTIVATE;
606 if (!repaint) flags |= SWP_NOREDRAW;
607 TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
608 return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
611 /***********************************************************************
612 * WINPOS_InitPlacement
614 static void WINPOS_InitPlacement( WND* wnd )
616 if (IsRectEmpty( &wnd->normal_rect ))
618 /* this happens when the window is minimized/maximized
619 * for the first time (rectWindow is not adjusted yet) */
621 wnd->normal_rect = wnd->rectWindow;
622 wnd->min_pos.x = wnd->min_pos.y = -1;
623 wnd->max_pos.x = wnd->max_pos.y = -1;
626 if( wnd->dwStyle & WS_MINIMIZE )
628 wnd->min_pos.x = wnd->rectWindow.left;
629 wnd->min_pos.y = wnd->rectWindow.top;
631 else if( wnd->dwStyle & WS_MAXIMIZE )
633 wnd->max_pos.x = wnd->rectWindow.left;
634 wnd->max_pos.y = wnd->rectWindow.top;
636 else
638 wnd->normal_rect = wnd->rectWindow;
642 /***********************************************************************
643 * WINPOS_RedrawIconTitle
645 BOOL WINPOS_RedrawIconTitle( HWND hWnd )
647 HWND icon_title = 0;
648 WND *win = WIN_GetPtr( hWnd );
650 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
652 icon_title = win->icon_title;
653 WIN_ReleasePtr( win );
655 if (!icon_title) return FALSE;
656 SendMessageW( icon_title, WM_SHOWWINDOW, TRUE, 0 );
657 InvalidateRect( icon_title, NULL, TRUE );
658 return TRUE;
661 /***********************************************************************
662 * WINPOS_ShowIconTitle
664 BOOL WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
666 if (!GetPropA( hwnd, "__wine_x11_managed" ))
668 WND *win = WIN_GetPtr( hwnd );
669 HWND title = 0;
671 TRACE("%p %i\n", hwnd, (bShow != 0) );
673 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP) return FALSE;
674 title = win->icon_title;
675 WIN_ReleasePtr( win );
677 if( bShow )
679 if (!title)
681 title = ICONTITLE_Create( hwnd );
682 if (!(win = WIN_GetPtr( hwnd )) || win == WND_OTHER_PROCESS)
684 DestroyWindow( title );
685 return FALSE;
687 win->icon_title = title;
688 WIN_ReleasePtr( win );
690 if (!IsWindowVisible(title))
692 SendMessageW( title, WM_SHOWWINDOW, TRUE, 0 );
693 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
694 SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
697 else if (title) ShowWindow( title, SW_HIDE );
699 return FALSE;
702 /*******************************************************************
703 * WINPOS_GetMinMaxInfo
705 * Get the minimized and maximized information for a window.
707 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
708 POINT *minTrack, POINT *maxTrack )
710 MINMAXINFO MinMax;
711 HMONITOR monitor;
712 INT xinc, yinc;
713 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
714 LONG exstyle = GetWindowLongA( hwnd, GWL_EXSTYLE );
715 RECT rc;
716 WND *win;
718 /* Compute default values */
720 GetWindowRect(hwnd, &rc);
721 MinMax.ptReserved.x = rc.left;
722 MinMax.ptReserved.y = rc.top;
724 if (style & WS_CHILD)
726 if ((style & WS_CAPTION) == WS_CAPTION)
727 style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
729 GetClientRect(GetAncestor(hwnd,GA_PARENT), &rc);
730 AdjustWindowRectEx(&rc, style, ((style & WS_POPUP) && GetMenu(hwnd)), exstyle);
732 /* avoid calculating this twice */
733 style &= ~(WS_DLGFRAME | WS_BORDER | WS_THICKFRAME);
735 MinMax.ptMaxSize.x = rc.right - rc.left;
736 MinMax.ptMaxSize.y = rc.bottom - rc.top;
738 else
740 MinMax.ptMaxSize.x = GetSystemMetrics(SM_CXSCREEN);
741 MinMax.ptMaxSize.y = GetSystemMetrics(SM_CYSCREEN);
743 MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
744 MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
745 MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXMAXTRACK);
746 MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYMAXTRACK);
748 if (HAS_DLGFRAME( style, exstyle ))
750 xinc = GetSystemMetrics(SM_CXDLGFRAME);
751 yinc = GetSystemMetrics(SM_CYDLGFRAME);
753 else
755 xinc = yinc = 0;
756 if (HAS_THICKFRAME(style))
758 xinc += GetSystemMetrics(SM_CXFRAME);
759 yinc += GetSystemMetrics(SM_CYFRAME);
761 if (style & WS_BORDER)
763 xinc += GetSystemMetrics(SM_CXBORDER);
764 yinc += GetSystemMetrics(SM_CYBORDER);
767 MinMax.ptMaxSize.x += 2 * xinc;
768 MinMax.ptMaxSize.y += 2 * yinc;
770 MinMax.ptMaxPosition.x = -xinc;
771 MinMax.ptMaxPosition.y = -yinc;
772 if ((win = WIN_GetPtr( hwnd )) && win != WND_DESKTOP && win != WND_OTHER_PROCESS)
774 if (!EMPTYPOINT(win->max_pos)) MinMax.ptMaxPosition = win->max_pos;
775 WIN_ReleasePtr( win );
778 SendMessageW( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
780 /* if the app didn't change the values, adapt them for the current monitor */
782 if ((monitor = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY )))
784 MONITORINFO mon_info;
786 mon_info.cbSize = sizeof(mon_info);
787 GetMonitorInfoW( monitor, &mon_info );
789 if (MinMax.ptMaxSize.x == GetSystemMetrics(SM_CXSCREEN) + 2 * xinc &&
790 MinMax.ptMaxSize.y == GetSystemMetrics(SM_CYSCREEN) + 2 * yinc)
792 MinMax.ptMaxSize.x = (mon_info.rcWork.right - mon_info.rcWork.left) + 2 * xinc;
793 MinMax.ptMaxSize.y = (mon_info.rcWork.bottom - mon_info.rcWork.top) + 2 * yinc;
795 if (MinMax.ptMaxPosition.x == -xinc && MinMax.ptMaxPosition.y == -yinc)
797 MinMax.ptMaxPosition.x = mon_info.rcWork.left - xinc;
798 MinMax.ptMaxPosition.y = mon_info.rcWork.top - yinc;
802 /* Some sanity checks */
804 TRACE("%d %d / %d %d / %d %d / %d %d\n",
805 MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
806 MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
807 MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
808 MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
809 MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
810 MinMax.ptMinTrackSize.x );
811 MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
812 MinMax.ptMinTrackSize.y );
814 if (maxSize) *maxSize = MinMax.ptMaxSize;
815 if (maxPos) *maxPos = MinMax.ptMaxPosition;
816 if (minTrack) *minTrack = MinMax.ptMinTrackSize;
817 if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
821 /***********************************************************************
822 * WINPOS_FindIconPos
824 * Find a suitable place for an iconic window.
826 static POINT WINPOS_FindIconPos( HWND hwnd, POINT pt )
828 RECT rect, rectParent;
829 HWND parent, child;
830 HRGN hrgn, tmp;
831 int xspacing, yspacing;
833 parent = GetAncestor( hwnd, GA_PARENT );
834 GetClientRect( parent, &rectParent );
835 if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
836 (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
837 return pt; /* The icon already has a suitable position */
839 xspacing = GetSystemMetrics(SM_CXICONSPACING);
840 yspacing = GetSystemMetrics(SM_CYICONSPACING);
842 /* Check if another icon already occupies this spot */
843 /* FIXME: this is completely inefficient */
845 hrgn = CreateRectRgn( 0, 0, 0, 0 );
846 tmp = CreateRectRgn( 0, 0, 0, 0 );
847 for (child = GetWindow( parent, GW_HWNDFIRST ); child; child = GetWindow( child, GW_HWNDNEXT ))
849 WND *childPtr;
850 if (child == hwnd) continue;
851 if ((GetWindowLongW( child, GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != (WS_VISIBLE|WS_MINIMIZE))
852 continue;
853 if (!(childPtr = WIN_GetPtr( child )) || childPtr == WND_OTHER_PROCESS)
854 continue;
855 SetRectRgn( tmp, childPtr->rectWindow.left, childPtr->rectWindow.top,
856 childPtr->rectWindow.right, childPtr->rectWindow.bottom );
857 CombineRgn( hrgn, hrgn, tmp, RGN_OR );
858 WIN_ReleasePtr( childPtr );
860 DeleteObject( tmp );
862 for (rect.bottom = rectParent.bottom; rect.bottom >= yspacing; rect.bottom -= yspacing)
864 for (rect.left = rectParent.left; rect.left <= rectParent.right - xspacing; rect.left += xspacing)
866 rect.right = rect.left + xspacing;
867 rect.top = rect.bottom - yspacing;
868 if (!RectInRegion( hrgn, &rect ))
870 /* No window was found, so it's OK for us */
871 pt.x = rect.left + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
872 pt.y = rect.top + (yspacing - GetSystemMetrics(SM_CYICON)) / 2;
873 DeleteObject( hrgn );
874 return pt;
878 DeleteObject( hrgn );
879 pt.x = pt.y = 0;
880 return pt;
884 /***********************************************************************
885 * WINPOS_MinMaximize
887 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
889 WND *wndPtr;
890 UINT swpFlags = 0;
891 POINT size;
892 LONG old_style;
893 WINDOWPLACEMENT wpl;
895 TRACE("%p %u\n", hwnd, cmd );
897 wpl.length = sizeof(wpl);
898 GetWindowPlacement( hwnd, &wpl );
900 if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
901 return SWP_NOSIZE | SWP_NOMOVE;
903 if (IsIconic( hwnd ))
905 switch (cmd)
907 case SW_SHOWMINNOACTIVE:
908 case SW_SHOWMINIMIZED:
909 case SW_FORCEMINIMIZE:
910 case SW_MINIMIZE:
911 return SWP_NOSIZE | SWP_NOMOVE;
913 if (!SendMessageW( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
914 swpFlags |= SWP_NOCOPYBITS;
917 switch( cmd )
919 case SW_SHOWMINNOACTIVE:
920 case SW_SHOWMINIMIZED:
921 case SW_FORCEMINIMIZE:
922 case SW_MINIMIZE:
923 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
924 if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
925 else wndPtr->flags &= ~WIN_RESTORE_MAX;
926 WIN_ReleasePtr( wndPtr );
928 old_style = WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
930 wpl.ptMinPosition = WINPOS_FindIconPos( hwnd, wpl.ptMinPosition );
932 if (!(old_style & WS_MINIMIZE)) swpFlags |= SWP_STATECHANGED;
933 SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
934 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON) );
935 swpFlags |= SWP_NOCOPYBITS;
936 break;
938 case SW_MAXIMIZE:
939 old_style = GetWindowLongW( hwnd, GWL_STYLE );
940 if ((old_style & WS_MAXIMIZE) && (old_style & WS_VISIBLE)) return SWP_NOSIZE | SWP_NOMOVE;
942 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
944 old_style = WIN_SetStyle( hwnd, WS_MAXIMIZE, WS_MINIMIZE );
945 if (old_style & WS_MINIMIZE) WINPOS_ShowIconTitle( hwnd, FALSE );
947 if (!(old_style & WS_MAXIMIZE)) swpFlags |= SWP_STATECHANGED;
948 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
949 break;
951 case SW_SHOWNOACTIVATE:
952 case SW_SHOWNORMAL:
953 case SW_RESTORE:
954 old_style = WIN_SetStyle( hwnd, 0, WS_MINIMIZE | WS_MAXIMIZE );
955 if (old_style & WS_MINIMIZE)
957 BOOL restore_max;
959 WINPOS_ShowIconTitle( hwnd, FALSE );
961 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
962 restore_max = (wndPtr->flags & WIN_RESTORE_MAX) != 0;
963 WIN_ReleasePtr( wndPtr );
964 if (restore_max)
966 /* Restore to maximized position */
967 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
968 WIN_SetStyle( hwnd, WS_MAXIMIZE, 0 );
969 swpFlags |= SWP_STATECHANGED;
970 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
971 break;
974 else if (!(old_style & WS_MAXIMIZE)) break;
976 swpFlags |= SWP_STATECHANGED;
978 /* Restore to normal position */
980 *rect = wpl.rcNormalPosition;
981 rect->right -= rect->left;
982 rect->bottom -= rect->top;
984 break;
987 return swpFlags;
991 /***********************************************************************
992 * show_window
994 * Implementation of ShowWindow and ShowWindowAsync.
996 static BOOL show_window( HWND hwnd, INT cmd )
998 WND *wndPtr;
999 HWND parent;
1000 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1001 BOOL wasVisible = (style & WS_VISIBLE) != 0;
1002 BOOL showFlag = TRUE;
1003 RECT newPos = {0, 0, 0, 0};
1004 UINT swp = 0;
1006 TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);
1008 switch(cmd)
1010 case SW_HIDE:
1011 if (!wasVisible) return FALSE;
1012 showFlag = FALSE;
1013 swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1014 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1015 break;
1017 case SW_SHOWMINNOACTIVE:
1018 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1019 /* fall through */
1020 case SW_MINIMIZE:
1021 case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
1022 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1023 /* fall through */
1024 case SW_SHOWMINIMIZED:
1025 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1026 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
1027 if ((style & WS_MINIMIZE) && wasVisible) return TRUE;
1028 break;
1030 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
1031 if (!wasVisible) swp |= SWP_SHOWWINDOW;
1032 swp |= SWP_FRAMECHANGED;
1033 swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
1034 if ((style & WS_MAXIMIZE) && wasVisible) return TRUE;
1035 break;
1037 case SW_SHOWNA:
1038 swp |= SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1039 if (style & WS_CHILD) swp |= SWP_NOZORDER;
1040 break;
1041 case SW_SHOW:
1042 if (wasVisible) return TRUE;
1043 swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1044 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1045 break;
1047 case SW_SHOWNOACTIVATE:
1048 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1049 /* fall through */
1050 case SW_RESTORE:
1051 /* fall through */
1052 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
1053 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
1054 if (!wasVisible) swp |= SWP_SHOWWINDOW;
1055 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1057 swp |= SWP_FRAMECHANGED;
1058 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
1060 else
1062 if (wasVisible) return TRUE;
1063 swp |= SWP_NOSIZE | SWP_NOMOVE;
1065 if (style & WS_CHILD && !(swp & SWP_STATECHANGED)) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1066 break;
1067 default:
1068 return wasVisible;
1071 if ((showFlag != wasVisible || cmd == SW_SHOWNA) && cmd != SW_SHOWMAXIMIZED && !(swp & SWP_STATECHANGED))
1073 SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
1074 if (!IsWindow( hwnd )) return wasVisible;
1077 parent = GetAncestor( hwnd, GA_PARENT );
1078 if (parent && !IsWindowVisible( parent ) && !(swp & SWP_STATECHANGED))
1080 /* if parent is not visible simply toggle WS_VISIBLE and return */
1081 if (showFlag) WIN_SetStyle( hwnd, WS_VISIBLE, 0 );
1082 else WIN_SetStyle( hwnd, 0, WS_VISIBLE );
1084 else
1085 SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1086 newPos.right, newPos.bottom, LOWORD(swp) );
1088 if (cmd == SW_HIDE)
1090 HWND hFocus;
1092 WINPOS_ShowIconTitle( hwnd, FALSE );
1094 /* FIXME: This will cause the window to be activated irrespective
1095 * of whether it is owned by the same thread. Has to be done
1096 * asynchronously.
1099 if (hwnd == GetActiveWindow())
1100 WINPOS_ActivateOtherWindow(hwnd);
1102 /* Revert focus to parent */
1103 hFocus = GetFocus();
1104 if (hwnd == hFocus || IsChild(hwnd, hFocus))
1106 HWND parent = GetAncestor(hwnd, GA_PARENT);
1107 if (parent == GetDesktopWindow()) parent = 0;
1108 SetFocus(parent);
1112 if (IsIconic(hwnd)) WINPOS_ShowIconTitle( hwnd, TRUE );
1114 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return wasVisible;
1116 if (wndPtr->flags & WIN_NEED_SIZE)
1118 /* should happen only in CreateWindowEx() */
1119 int wParam = SIZE_RESTORED;
1120 RECT client = wndPtr->rectClient;
1122 wndPtr->flags &= ~WIN_NEED_SIZE;
1123 if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1124 else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
1125 WIN_ReleasePtr( wndPtr );
1127 SendMessageW( hwnd, WM_SIZE, wParam,
1128 MAKELONG( client.right - client.left, client.bottom - client.top ));
1129 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( client.left, client.top ));
1131 else WIN_ReleasePtr( wndPtr );
1133 /* if previous state was minimized Windows sets focus to the window */
1134 if (style & WS_MINIMIZE) SetFocus( hwnd );
1136 return wasVisible;
1140 /***********************************************************************
1141 * ShowWindowAsync (USER32.@)
1143 * doesn't wait; returns immediately.
1144 * used by threads to toggle windows in other (possibly hanging) threads
1146 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
1148 HWND full_handle;
1150 if (is_broadcast(hwnd))
1152 SetLastError( ERROR_INVALID_PARAMETER );
1153 return FALSE;
1156 if ((full_handle = WIN_IsCurrentThread( hwnd )))
1157 return show_window( full_handle, cmd );
1159 return SendNotifyMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
1163 /***********************************************************************
1164 * ShowWindow (USER32.@)
1166 BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
1168 HWND full_handle;
1170 if (is_broadcast(hwnd))
1172 SetLastError( ERROR_INVALID_PARAMETER );
1173 return FALSE;
1175 if ((full_handle = WIN_IsCurrentThread( hwnd )))
1176 return show_window( full_handle, cmd );
1178 return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
1182 /***********************************************************************
1183 * GetInternalWindowPos (USER32.@)
1185 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
1186 LPPOINT ptIcon )
1188 WINDOWPLACEMENT wndpl;
1189 if (GetWindowPlacement( hwnd, &wndpl ))
1191 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
1192 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
1193 return wndpl.showCmd;
1195 return 0;
1199 /***********************************************************************
1200 * GetWindowPlacement (USER32.@)
1202 * Win95:
1203 * Fails if wndpl->length of Win95 (!) apps is invalid.
1205 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
1207 WND *pWnd = WIN_GetPtr( hwnd );
1209 if (!pWnd || pWnd == WND_DESKTOP) return FALSE;
1210 if (pWnd == WND_OTHER_PROCESS)
1212 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
1213 return FALSE;
1216 WINPOS_InitPlacement( pWnd );
1217 wndpl->length = sizeof(*wndpl);
1218 if( pWnd->dwStyle & WS_MINIMIZE )
1219 wndpl->showCmd = SW_SHOWMINIMIZED;
1220 else
1221 wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
1222 if( pWnd->flags & WIN_RESTORE_MAX )
1223 wndpl->flags = WPF_RESTORETOMAXIMIZED;
1224 else
1225 wndpl->flags = 0;
1226 wndpl->ptMinPosition = pWnd->min_pos;
1227 wndpl->ptMaxPosition = pWnd->max_pos;
1228 wndpl->rcNormalPosition = pWnd->normal_rect;
1229 WIN_ReleasePtr( pWnd );
1231 TRACE( "%p: returning min %d,%d max %d,%d normal %s\n",
1232 hwnd, wndpl->ptMinPosition.x, wndpl->ptMinPosition.y,
1233 wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y,
1234 wine_dbgstr_rect(&wndpl->rcNormalPosition) );
1235 return TRUE;
1239 /***********************************************************************
1240 * WINPOS_SetPlacement
1242 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
1244 DWORD style;
1245 WND *pWnd = WIN_GetPtr( hwnd );
1246 POINT pt;
1247 RECT rect;
1249 if (!pWnd || pWnd == WND_OTHER_PROCESS || pWnd == WND_DESKTOP) return FALSE;
1251 TRACE( "%p: setting min %d,%d max %d,%d normal %s flags %x\n",
1252 hwnd, wndpl->ptMinPosition.x, wndpl->ptMinPosition.y,
1253 wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y,
1254 wine_dbgstr_rect(&wndpl->rcNormalPosition), flags );
1256 if( flags & PLACE_MIN ) pWnd->min_pos = wndpl->ptMinPosition;
1257 if( flags & PLACE_MAX ) pWnd->max_pos = wndpl->ptMaxPosition;
1258 if( flags & PLACE_RECT) pWnd->normal_rect = wndpl->rcNormalPosition;
1260 style = pWnd->dwStyle;
1261 pt.x = pt.y = -1;
1262 SetRectEmpty( &rect );
1264 if( style & WS_MINIMIZE )
1266 if (wndpl->flags & WPF_SETMINPOSITION) pt = pWnd->min_pos;
1268 else if( style & WS_MAXIMIZE )
1270 pt = pWnd->max_pos;
1272 else if( flags & PLACE_RECT )
1273 rect = pWnd->normal_rect;
1275 WIN_ReleasePtr( pWnd );
1277 if( style & WS_MINIMIZE )
1279 WINPOS_ShowIconTitle( hwnd, FALSE );
1280 if (!EMPTYPOINT(pt)) SetWindowPos( hwnd, 0, pt.x, pt.y, 0, 0,
1281 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1283 else if( style & WS_MAXIMIZE )
1285 if (!EMPTYPOINT(pt)) SetWindowPos( hwnd, 0, pt.x, pt.y, 0, 0,
1286 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1288 else if( flags & PLACE_RECT )
1289 SetWindowPos( hwnd, 0, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
1290 SWP_NOZORDER | SWP_NOACTIVATE );
1292 ShowWindow( hwnd, wndpl->showCmd );
1294 if (IsIconic( hwnd ))
1296 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE) WINPOS_ShowIconTitle( hwnd, TRUE );
1298 /* SDK: ...valid only the next time... */
1299 if( wndpl->flags & WPF_RESTORETOMAXIMIZED )
1301 pWnd = WIN_GetPtr( hwnd );
1302 if (pWnd && pWnd != WND_OTHER_PROCESS)
1304 pWnd->flags |= WIN_RESTORE_MAX;
1305 WIN_ReleasePtr( pWnd );
1309 return TRUE;
1313 /***********************************************************************
1314 * SetWindowPlacement (USER32.@)
1316 * Win95:
1317 * Fails if wndpl->length of Win95 (!) apps is invalid.
1319 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
1321 if (!wpl) return FALSE;
1322 return WINPOS_SetPlacement( hwnd, wpl, PLACE_MIN | PLACE_MAX | PLACE_RECT );
1326 /***********************************************************************
1327 * AnimateWindow (USER32.@)
1328 * Shows/Hides a window with an animation
1329 * NO ANIMATION YET
1331 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
1333 FIXME("partial stub\n");
1335 /* If trying to show/hide and it's already *
1336 * shown/hidden or invalid window, fail with *
1337 * invalid parameter */
1338 if(!IsWindow(hwnd) ||
1339 (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
1340 (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
1342 SetLastError(ERROR_INVALID_PARAMETER);
1343 return FALSE;
1346 ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1348 return TRUE;
1351 /***********************************************************************
1352 * SetInternalWindowPos (USER32.@)
1354 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1355 LPRECT rect, LPPOINT pt )
1357 if( IsWindow(hwnd) )
1359 WINDOWPLACEMENT wndpl;
1360 UINT flags;
1362 wndpl.length = sizeof(wndpl);
1363 wndpl.showCmd = showCmd;
1364 wndpl.flags = flags = 0;
1366 if( pt )
1368 flags |= PLACE_MIN;
1369 wndpl.flags |= WPF_SETMINPOSITION;
1370 wndpl.ptMinPosition = *pt;
1372 if( rect )
1374 flags |= PLACE_RECT;
1375 wndpl.rcNormalPosition = *rect;
1377 WINPOS_SetPlacement( hwnd, &wndpl, flags );
1382 /*******************************************************************
1383 * can_activate_window
1385 * Check if we can activate the specified window.
1387 static BOOL can_activate_window( HWND hwnd )
1389 LONG style;
1391 if (!hwnd) return FALSE;
1392 style = GetWindowLongW( hwnd, GWL_STYLE );
1393 if (!(style & WS_VISIBLE)) return FALSE;
1394 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1395 return !(style & WS_DISABLED);
1399 /*******************************************************************
1400 * WINPOS_ActivateOtherWindow
1402 * Activates window other than pWnd.
1404 void WINPOS_ActivateOtherWindow(HWND hwnd)
1406 HWND hwndTo, fg;
1408 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1410 hwndTo = GetAncestor( hwndTo, GA_ROOT );
1411 if (can_activate_window( hwndTo )) goto done;
1414 hwndTo = hwnd;
1415 for (;;)
1417 if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1418 if (can_activate_window( hwndTo )) break;
1421 done:
1422 fg = GetForegroundWindow();
1423 TRACE("win = %p fg = %p\n", hwndTo, fg);
1424 if (!fg || (hwnd == fg))
1426 if (SetForegroundWindow( hwndTo )) return;
1428 if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1432 /***********************************************************************
1433 * WINPOS_HandleWindowPosChanging
1435 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1437 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1439 POINT minTrack, maxTrack;
1440 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1442 if (winpos->flags & SWP_NOSIZE) return 0;
1443 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1445 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1446 if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1447 if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1448 if (!(style & WS_MINIMIZE))
1450 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1451 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1454 return 0;
1458 /***********************************************************************
1459 * dump_winpos_flags
1461 static void dump_winpos_flags(UINT flags)
1463 TRACE("flags:");
1464 if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1465 if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1466 if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1467 if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1468 if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1469 if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1470 if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1471 if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1472 if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1473 if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1474 if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1475 if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1476 if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1478 #define DUMPED_FLAGS \
1479 (SWP_NOSIZE | \
1480 SWP_NOMOVE | \
1481 SWP_NOZORDER | \
1482 SWP_NOREDRAW | \
1483 SWP_NOACTIVATE | \
1484 SWP_FRAMECHANGED | \
1485 SWP_SHOWWINDOW | \
1486 SWP_HIDEWINDOW | \
1487 SWP_NOCOPYBITS | \
1488 SWP_NOOWNERZORDER | \
1489 SWP_NOSENDCHANGING | \
1490 SWP_DEFERERASE | \
1491 SWP_ASYNCWINDOWPOS)
1493 if(flags & ~DUMPED_FLAGS) TRACE(" %08x", flags & ~DUMPED_FLAGS);
1494 TRACE("\n");
1495 #undef DUMPED_FLAGS
1498 /***********************************************************************
1499 * SWP_DoWinPosChanging
1501 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
1503 WND *wndPtr;
1505 /* Send WM_WINDOWPOSCHANGING message */
1507 if (!(pWinpos->flags & SWP_NOSENDCHANGING))
1508 SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
1510 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) ||
1511 wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
1513 /* Calculate new position and size */
1515 *pNewWindowRect = wndPtr->rectWindow;
1516 *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
1517 : wndPtr->rectClient;
1519 if (!(pWinpos->flags & SWP_NOSIZE))
1521 pNewWindowRect->right = pNewWindowRect->left + pWinpos->cx;
1522 pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
1524 if (!(pWinpos->flags & SWP_NOMOVE))
1526 pNewWindowRect->left = pWinpos->x;
1527 pNewWindowRect->top = pWinpos->y;
1528 pNewWindowRect->right += pWinpos->x - wndPtr->rectWindow.left;
1529 pNewWindowRect->bottom += pWinpos->y - wndPtr->rectWindow.top;
1531 OffsetRect( pNewClientRect, pWinpos->x - wndPtr->rectWindow.left,
1532 pWinpos->y - wndPtr->rectWindow.top );
1534 pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
1536 TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
1537 pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
1538 pWinpos->cx, pWinpos->cy, pWinpos->flags );
1539 TRACE( "current %s style %08x new %s\n",
1540 wine_dbgstr_rect( &wndPtr->rectWindow ), wndPtr->dwStyle,
1541 wine_dbgstr_rect( pNewWindowRect ));
1543 WIN_ReleasePtr( wndPtr );
1544 return TRUE;
1547 /***********************************************************************
1548 * get_valid_rects
1550 * Compute the valid rects from the old and new client rect and WVR_* flags.
1551 * Helper for WM_NCCALCSIZE handling.
1553 static inline void get_valid_rects( const RECT *old_client, const RECT *new_client, UINT flags,
1554 RECT *valid )
1556 int cx, cy;
1558 if (flags & WVR_REDRAW)
1560 SetRectEmpty( &valid[0] );
1561 SetRectEmpty( &valid[1] );
1562 return;
1565 if (flags & WVR_VALIDRECTS)
1567 if (!IntersectRect( &valid[0], &valid[0], new_client ) ||
1568 !IntersectRect( &valid[1], &valid[1], old_client ))
1570 SetRectEmpty( &valid[0] );
1571 SetRectEmpty( &valid[1] );
1572 return;
1574 flags = WVR_ALIGNLEFT | WVR_ALIGNTOP;
1576 else
1578 valid[0] = *new_client;
1579 valid[1] = *old_client;
1582 /* make sure the rectangles have the same size */
1583 cx = min( valid[0].right - valid[0].left, valid[1].right - valid[1].left );
1584 cy = min( valid[0].bottom - valid[0].top, valid[1].bottom - valid[1].top );
1586 if (flags & WVR_ALIGNBOTTOM)
1588 valid[0].top = valid[0].bottom - cy;
1589 valid[1].top = valid[1].bottom - cy;
1591 else
1593 valid[0].bottom = valid[0].top + cy;
1594 valid[1].bottom = valid[1].top + cy;
1596 if (flags & WVR_ALIGNRIGHT)
1598 valid[0].left = valid[0].right - cx;
1599 valid[1].left = valid[1].right - cx;
1601 else
1603 valid[0].right = valid[0].left + cx;
1604 valid[1].right = valid[1].left + cx;
1609 /***********************************************************************
1610 * SWP_DoOwnedPopups
1612 * fix Z order taking into account owned popups -
1613 * basically we need to maintain them above the window that owns them
1615 * FIXME: hide/show owned popups when owner visibility changes.
1617 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
1619 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1620 HWND owner, *list = NULL;
1621 unsigned int i;
1623 TRACE("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
1625 if ((style & WS_POPUP) && (owner = GetWindow( hwnd, GW_OWNER )))
1627 /* make sure this popup stays above the owner */
1629 if (hwndInsertAfter != HWND_TOP && hwndInsertAfter != HWND_TOPMOST)
1631 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return hwndInsertAfter;
1633 for (i = 0; list[i]; i++)
1635 if (list[i] == owner)
1637 if (i > 0) hwndInsertAfter = list[i-1];
1638 else hwndInsertAfter = HWND_TOP;
1639 break;
1642 if (hwndInsertAfter == HWND_NOTOPMOST)
1644 if (!(GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST)) break;
1646 else if (list[i] == hwndInsertAfter) break;
1650 else if (style & WS_CHILD) return hwndInsertAfter;
1652 if (hwndInsertAfter == HWND_BOTTOM) goto done;
1653 if (!list && !(list = WIN_ListChildren( GetDesktopWindow() ))) goto done;
1655 i = 0;
1656 if (hwndInsertAfter == HWND_TOP || hwndInsertAfter == HWND_NOTOPMOST)
1658 if (hwndInsertAfter == HWND_NOTOPMOST || !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_TOPMOST))
1660 /* skip all the topmost windows */
1661 while (list[i] && (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST)) i++;
1664 else if (hwndInsertAfter != HWND_TOPMOST)
1666 /* skip windows that are already placed correctly */
1667 for (i = 0; list[i]; i++)
1669 if (list[i] == hwndInsertAfter) break;
1670 if (list[i] == hwnd) goto done; /* nothing to do if window is moving backwards in z-order */
1674 for ( ; list[i]; i++)
1676 if (list[i] == hwnd) break;
1677 if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_POPUP)) continue;
1678 if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
1679 TRACE( "moving %p owned by %p after %p\n", list[i], hwnd, hwndInsertAfter );
1680 SetWindowPos( list[i], hwndInsertAfter, 0, 0, 0, 0,
1681 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING | SWP_DEFERERASE );
1682 hwndInsertAfter = list[i];
1685 done:
1686 HeapFree( GetProcessHeap(), 0, list );
1687 return hwndInsertAfter;
1690 /***********************************************************************
1691 * SWP_DoNCCalcSize
1693 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect,
1694 RECT *validRects )
1696 UINT wvrFlags = 0;
1697 WND *wndPtr;
1699 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
1701 /* Send WM_NCCALCSIZE message to get new client area */
1702 if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
1704 NCCALCSIZE_PARAMS params;
1705 WINDOWPOS winposCopy;
1707 params.rgrc[0] = *pNewWindowRect;
1708 params.rgrc[1] = wndPtr->rectWindow;
1709 params.rgrc[2] = wndPtr->rectClient;
1710 params.lppos = &winposCopy;
1711 winposCopy = *pWinpos;
1712 WIN_ReleasePtr( wndPtr );
1714 wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
1716 *pNewClientRect = params.rgrc[0];
1718 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
1720 TRACE( "hwnd %p old win %s old client %s new win %s new client %s\n", pWinpos->hwnd,
1721 wine_dbgstr_rect(&wndPtr->rectWindow), wine_dbgstr_rect(&wndPtr->rectClient),
1722 wine_dbgstr_rect(pNewWindowRect), wine_dbgstr_rect(pNewClientRect) );
1724 if( pNewClientRect->left != wndPtr->rectClient.left ||
1725 pNewClientRect->top != wndPtr->rectClient.top )
1726 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1728 if( (pNewClientRect->right - pNewClientRect->left !=
1729 wndPtr->rectClient.right - wndPtr->rectClient.left))
1730 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1731 else
1732 wvrFlags &= ~WVR_HREDRAW;
1734 if (pNewClientRect->bottom - pNewClientRect->top !=
1735 wndPtr->rectClient.bottom - wndPtr->rectClient.top)
1736 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1737 else
1738 wvrFlags &= ~WVR_VREDRAW;
1740 validRects[0] = params.rgrc[1];
1741 validRects[1] = params.rgrc[2];
1743 else
1745 if (!(pWinpos->flags & SWP_NOMOVE) &&
1746 (pNewClientRect->left != wndPtr->rectClient.left ||
1747 pNewClientRect->top != wndPtr->rectClient.top))
1748 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1751 if (pWinpos->flags & (SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
1753 SetRectEmpty( &validRects[0] );
1754 SetRectEmpty( &validRects[1] );
1756 else get_valid_rects( &wndPtr->rectClient, pNewClientRect, wvrFlags, validRects );
1758 WIN_ReleasePtr( wndPtr );
1759 return wvrFlags;
1762 /* fix redundant flags and values in the WINDOWPOS structure */
1763 static BOOL fixup_flags( WINDOWPOS *winpos )
1765 HWND parent;
1766 WND *wndPtr = WIN_GetPtr( winpos->hwnd );
1767 BOOL ret = TRUE;
1769 if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
1771 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1772 return FALSE;
1774 winpos->hwnd = wndPtr->hwndSelf; /* make it a full handle */
1776 /* Finally make sure that all coordinates are valid */
1777 if (winpos->x < -32768) winpos->x = -32768;
1778 else if (winpos->x > 32767) winpos->x = 32767;
1779 if (winpos->y < -32768) winpos->y = -32768;
1780 else if (winpos->y > 32767) winpos->y = 32767;
1782 if (winpos->cx < 0) winpos->cx = 0;
1783 else if (winpos->cx > 32767) winpos->cx = 32767;
1784 if (winpos->cy < 0) winpos->cy = 0;
1785 else if (winpos->cy > 32767) winpos->cy = 32767;
1787 parent = GetAncestor( winpos->hwnd, GA_PARENT );
1788 if (!IsWindowVisible( parent )) winpos->flags |= SWP_NOREDRAW;
1790 if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
1791 else
1793 winpos->flags &= ~SWP_HIDEWINDOW;
1794 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
1797 if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
1798 (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
1799 winpos->flags |= SWP_NOSIZE; /* Already the right size */
1801 if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
1802 winpos->flags |= SWP_NOMOVE; /* Already the right position */
1804 if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
1806 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)) && /* Bring to the top when activating */
1807 (winpos->flags & SWP_NOZORDER ||
1808 (winpos->hwndInsertAfter != HWND_TOPMOST && winpos->hwndInsertAfter != HWND_NOTOPMOST)))
1810 winpos->flags &= ~SWP_NOZORDER;
1811 winpos->hwndInsertAfter = HWND_TOP;
1815 /* Check hwndInsertAfter */
1816 if (winpos->flags & SWP_NOZORDER) goto done;
1818 /* fix sign extension */
1819 if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
1820 else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
1822 /* hwndInsertAfter must be a sibling of the window */
1823 if (winpos->hwndInsertAfter == HWND_TOP)
1825 if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1826 winpos->flags |= SWP_NOZORDER;
1828 else if (winpos->hwndInsertAfter == HWND_BOTTOM)
1830 if (!(wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
1831 winpos->flags |= SWP_NOZORDER;
1833 else if (winpos->hwndInsertAfter == HWND_TOPMOST)
1835 if ((wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1836 winpos->flags |= SWP_NOZORDER;
1838 else if (winpos->hwndInsertAfter == HWND_NOTOPMOST)
1840 if (!(wndPtr->dwExStyle & WS_EX_TOPMOST))
1841 winpos->flags |= SWP_NOZORDER;
1843 else
1845 if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != parent) ret = FALSE;
1846 else
1848 /* don't need to change the Zorder of hwnd if it's already inserted
1849 * after hwndInsertAfter or when inserting hwnd after itself.
1851 if ((winpos->hwnd == winpos->hwndInsertAfter) ||
1852 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
1853 winpos->flags |= SWP_NOZORDER;
1856 done:
1857 WIN_ReleasePtr( wndPtr );
1858 return ret;
1862 /***********************************************************************
1863 * set_window_pos
1865 * Backend implementation of SetWindowPos.
1867 BOOL set_window_pos( HWND hwnd, HWND insert_after, UINT swp_flags,
1868 const RECT *window_rect, const RECT *client_rect, const RECT *valid_rects )
1870 WND *win;
1871 BOOL ret;
1872 RECT visible_rect, old_window_rect;
1873 DWORD new_style;
1875 if (!(win = WIN_GetPtr( hwnd ))) return FALSE;
1876 if (win == WND_DESKTOP || win == WND_OTHER_PROCESS) return FALSE;
1878 old_window_rect = win->rectWindow;
1879 SERVER_START_REQ( set_window_pos )
1881 req->handle = hwnd;
1882 req->previous = insert_after;
1883 req->flags = swp_flags;
1884 req->window.left = window_rect->left;
1885 req->window.top = window_rect->top;
1886 req->window.right = window_rect->right;
1887 req->window.bottom = window_rect->bottom;
1888 req->client.left = client_rect->left;
1889 req->client.top = client_rect->top;
1890 req->client.right = client_rect->right;
1891 req->client.bottom = client_rect->bottom;
1892 if (!IsRectEmpty( &valid_rects[0] ))
1893 wine_server_add_data( req, valid_rects, 2 * sizeof(*valid_rects) );
1894 if ((ret = !wine_server_call( req )))
1896 win->dwStyle = reply->new_style;
1897 win->dwExStyle = reply->new_ex_style;
1898 win->rectWindow = *window_rect;
1899 win->rectClient = *client_rect;
1900 visible_rect.left = reply->visible.left;
1901 visible_rect.top = reply->visible.top;
1902 visible_rect.right = reply->visible.right;
1903 visible_rect.bottom = reply->visible.bottom;
1906 SERVER_END_REQ;
1907 new_style = win->dwStyle;
1908 WIN_ReleasePtr( win );
1910 if (ret)
1912 USER_Driver->pSetWindowPos( hwnd, insert_after, swp_flags, window_rect,
1913 client_rect, &visible_rect, valid_rects );
1915 if ((((swp_flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) && (new_style & WS_VISIBLE)) ||
1916 (swp_flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW)))
1917 invalidate_dce( hwnd, &old_window_rect );
1919 return ret;
1923 /***********************************************************************
1924 * USER_SetWindowPos
1926 * User32 internal function
1928 BOOL USER_SetWindowPos( WINDOWPOS * winpos )
1930 RECT newWindowRect, newClientRect, valid_rects[2];
1931 UINT orig_flags;
1933 orig_flags = winpos->flags;
1935 /* First make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
1936 if (!(winpos->flags & SWP_NOMOVE))
1938 if (winpos->x < -32768) winpos->x = -32768;
1939 else if (winpos->x > 32767) winpos->x = 32767;
1940 if (winpos->y < -32768) winpos->y = -32768;
1941 else if (winpos->y > 32767) winpos->y = 32767;
1943 if (!(winpos->flags & SWP_NOSIZE))
1945 if (winpos->cx < 0) winpos->cx = 0;
1946 else if (winpos->cx > 32767) winpos->cx = 32767;
1947 if (winpos->cy < 0) winpos->cy = 0;
1948 else if (winpos->cy > 32767) winpos->cy = 32767;
1951 if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
1953 /* Fix redundant flags */
1954 if (!fixup_flags( winpos )) return FALSE;
1956 if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
1958 if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
1959 winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
1962 /* Common operations */
1964 SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect, valid_rects );
1966 if (!set_window_pos( winpos->hwnd, winpos->hwndInsertAfter, winpos->flags,
1967 &newWindowRect, &newClientRect, valid_rects ))
1968 return FALSE;
1970 /* erase parent when hiding or resizing child */
1971 if (!(orig_flags & SWP_DEFERERASE) &&
1972 ((orig_flags & SWP_HIDEWINDOW) ||
1973 (!(orig_flags & SWP_SHOWWINDOW) &&
1974 (winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOGEOMETRYCHANGE)))
1976 HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
1977 if (!parent || parent == GetDesktopWindow()) parent = winpos->hwnd;
1978 erase_now( parent, 0 );
1981 if( winpos->flags & SWP_HIDEWINDOW )
1982 HideCaret(winpos->hwnd);
1983 else if (winpos->flags & SWP_SHOWWINDOW)
1984 ShowCaret(winpos->hwnd);
1986 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)))
1988 /* child windows get WM_CHILDACTIVATE message */
1989 if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
1990 SendMessageW( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
1991 else
1992 SetForegroundWindow( winpos->hwnd );
1995 /* And last, send the WM_WINDOWPOSCHANGED message */
1997 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
1999 if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
2001 /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
2002 and always contains final window position.
2004 winpos->x = newWindowRect.left;
2005 winpos->y = newWindowRect.top;
2006 winpos->cx = newWindowRect.right - newWindowRect.left;
2007 winpos->cy = newWindowRect.bottom - newWindowRect.top;
2008 SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
2010 return TRUE;
2013 /***********************************************************************
2014 * SetWindowPos (USER32.@)
2016 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
2017 INT x, INT y, INT cx, INT cy, UINT flags )
2019 WINDOWPOS winpos;
2021 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2022 hwnd, hwndInsertAfter, x, y, cx, cy, flags);
2023 if(TRACE_ON(win)) dump_winpos_flags(flags);
2025 if (is_broadcast(hwnd))
2027 SetLastError( ERROR_INVALID_PARAMETER );
2028 return FALSE;
2031 winpos.hwnd = WIN_GetFullHandle(hwnd);
2032 winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
2033 winpos.x = x;
2034 winpos.y = y;
2035 winpos.cx = cx;
2036 winpos.cy = cy;
2037 winpos.flags = flags;
2039 if (WIN_IsCurrentThread( hwnd ))
2040 return USER_SetWindowPos(&winpos);
2042 return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
2046 /***********************************************************************
2047 * BeginDeferWindowPos (USER32.@)
2049 HDWP WINAPI BeginDeferWindowPos( INT count )
2051 HDWP handle;
2052 DWP *pDWP;
2054 TRACE("%d\n", count);
2056 if (count < 0)
2058 SetLastError(ERROR_INVALID_PARAMETER);
2059 return 0;
2061 /* Windows allows zero count, in which case it allocates context for 8 moves */
2062 if (count == 0) count = 8;
2064 handle = USER_HEAP_ALLOC( sizeof(DWP) + (count-1)*sizeof(WINDOWPOS) );
2065 if (!handle) return 0;
2066 pDWP = (DWP *) USER_HEAP_LIN_ADDR( handle );
2067 pDWP->actualCount = 0;
2068 pDWP->suggestedCount = count;
2069 pDWP->valid = TRUE;
2070 pDWP->wMagic = DWP_MAGIC;
2071 pDWP->hwndParent = 0;
2073 TRACE("returning hdwp %p\n", handle);
2074 return handle;
2078 /***********************************************************************
2079 * DeferWindowPos (USER32.@)
2081 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
2082 INT x, INT y, INT cx, INT cy,
2083 UINT flags )
2085 DWP *pDWP;
2086 int i;
2087 HDWP newhdwp = hdwp,retvalue;
2089 TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2090 hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
2092 hwnd = WIN_GetFullHandle( hwnd );
2093 if (hwnd == GetDesktopWindow()) return 0;
2095 if (!(pDWP = USER_HEAP_LIN_ADDR( hdwp ))) return 0;
2097 USER_Lock();
2099 for (i = 0; i < pDWP->actualCount; i++)
2101 if (pDWP->winPos[i].hwnd == hwnd)
2103 /* Merge with the other changes */
2104 if (!(flags & SWP_NOZORDER))
2106 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
2108 if (!(flags & SWP_NOMOVE))
2110 pDWP->winPos[i].x = x;
2111 pDWP->winPos[i].y = y;
2113 if (!(flags & SWP_NOSIZE))
2115 pDWP->winPos[i].cx = cx;
2116 pDWP->winPos[i].cy = cy;
2118 pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
2119 SWP_NOZORDER | SWP_NOREDRAW |
2120 SWP_NOACTIVATE | SWP_NOCOPYBITS|
2121 SWP_NOOWNERZORDER);
2122 pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
2123 SWP_FRAMECHANGED);
2124 retvalue = hdwp;
2125 goto END;
2128 if (pDWP->actualCount >= pDWP->suggestedCount)
2130 newhdwp = USER_HEAP_REALLOC( hdwp,
2131 sizeof(DWP) + pDWP->suggestedCount*sizeof(WINDOWPOS) );
2132 if (!newhdwp)
2134 retvalue = 0;
2135 goto END;
2137 pDWP = (DWP *) USER_HEAP_LIN_ADDR( newhdwp );
2138 pDWP->suggestedCount++;
2140 pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
2141 pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
2142 pDWP->winPos[pDWP->actualCount].x = x;
2143 pDWP->winPos[pDWP->actualCount].y = y;
2144 pDWP->winPos[pDWP->actualCount].cx = cx;
2145 pDWP->winPos[pDWP->actualCount].cy = cy;
2146 pDWP->winPos[pDWP->actualCount].flags = flags;
2147 pDWP->actualCount++;
2148 retvalue = newhdwp;
2149 END:
2150 USER_Unlock();
2151 return retvalue;
2155 /***********************************************************************
2156 * EndDeferWindowPos (USER32.@)
2158 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
2160 DWP *pDWP;
2161 WINDOWPOS *winpos;
2162 BOOL res = TRUE;
2163 int i;
2165 TRACE("%p\n", hdwp);
2167 pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
2168 if (!pDWP) return FALSE;
2169 for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
2171 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2172 winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
2173 winpos->cx, winpos->cy, winpos->flags);
2175 if (!(res = USER_SetWindowPos( winpos ))) break;
2177 USER_HEAP_FREE( hdwp );
2178 return res;
2182 /***********************************************************************
2183 * TileChildWindows (USER.199)
2185 void WINAPI TileChildWindows16( HWND16 parent, WORD action )
2187 FIXME("(%04x, %d): stub\n", parent, action);
2190 /***********************************************************************
2191 * CascadeChildWindows (USER.198)
2193 void WINAPI CascadeChildWindows16( HWND16 parent, WORD action )
2195 FIXME("(%04x, %d): stub\n", parent, action);