server: Added support for HWND_TOPMOST and HWND_NOTOPMOST.
[wine/multimedia.git] / dlls / user32 / winpos.c
blob21c1089d758352883274b554c5c5a193c94a8952
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 "winerror.h"
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winerror.h"
34 #include "wine/server.h"
35 #include "controls.h"
36 #include "user_private.h"
37 #include "win.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(win);
42 #define SWP_AGG_NOGEOMETRYCHANGE \
43 (SWP_NOSIZE | SWP_NOCLIENTSIZE | SWP_NOZORDER)
44 #define SWP_AGG_NOPOSCHANGE \
45 (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
46 #define SWP_AGG_STATUSFLAGS \
47 (SWP_AGG_NOPOSCHANGE | SWP_FRAMECHANGED | SWP_HIDEWINDOW | SWP_SHOWWINDOW)
49 #define HAS_DLGFRAME(style,exStyle) \
50 (((exStyle) & WS_EX_DLGMODALFRAME) || \
51 (((style) & WS_DLGFRAME) && !((style) & WS_BORDER)))
53 #define HAS_THICKFRAME(style) \
54 (((style) & WS_THICKFRAME) && \
55 !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
57 #define EMPTYPOINT(pt) ((*(LONG*)&(pt)) == -1)
59 #define PLACE_MIN 0x0001
60 #define PLACE_MAX 0x0002
61 #define PLACE_RECT 0x0004
64 #define DWP_MAGIC ((INT)('W' | ('P' << 8) | ('O' << 16) | ('S' << 24)))
66 typedef struct
68 INT actualCount;
69 INT suggestedCount;
70 BOOL valid;
71 INT wMagic;
72 HWND hwndParent;
73 WINDOWPOS winPos[1];
74 } DWP;
76 typedef struct
78 RECT16 rectNormal;
79 POINT16 ptIconPos;
80 POINT16 ptMaxPos;
81 HWND hwndIconTitle;
82 } INTERNALPOS, *LPINTERNALPOS;
84 /* ----- internal functions ----- */
86 static const WCHAR SysIP_W[] = { 'S','y','s','I','P',0 };
88 static inline INTERNALPOS *get_internal_pos( HWND hwnd )
90 return GetPropW( hwnd, SysIP_W );
93 static inline void set_internal_pos( HWND hwnd, INTERNALPOS *pos )
95 SetPropW( hwnd, SysIP_W, pos );
98 /***********************************************************************
99 * WINPOS_CheckInternalPos
101 * Called when a window is destroyed.
103 void WINPOS_CheckInternalPos( HWND hwnd )
105 LPINTERNALPOS lpPos = get_internal_pos( hwnd );
107 if ( lpPos )
109 if( IsWindow(lpPos->hwndIconTitle) )
110 DestroyWindow( lpPos->hwndIconTitle );
111 HeapFree( GetProcessHeap(), 0, lpPos );
115 /***********************************************************************
116 * ArrangeIconicWindows (USER32.@)
118 UINT WINAPI ArrangeIconicWindows( HWND parent )
120 RECT rectParent;
121 HWND hwndChild;
122 INT x, y, xspacing, yspacing;
124 GetClientRect( parent, &rectParent );
125 x = rectParent.left;
126 y = rectParent.bottom;
127 xspacing = GetSystemMetrics(SM_CXICONSPACING);
128 yspacing = GetSystemMetrics(SM_CYICONSPACING);
130 hwndChild = GetWindow( parent, GW_CHILD );
131 while (hwndChild)
133 if( IsIconic( hwndChild ) )
135 WINPOS_ShowIconTitle( hwndChild, FALSE );
137 SetWindowPos( hwndChild, 0, x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
138 y - yspacing - GetSystemMetrics(SM_CYICON)/2, 0, 0,
139 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
140 if( IsWindow(hwndChild) )
141 WINPOS_ShowIconTitle(hwndChild , TRUE );
143 if (x <= rectParent.right - xspacing) x += xspacing;
144 else
146 x = rectParent.left;
147 y -= yspacing;
150 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
152 return yspacing;
156 /***********************************************************************
157 * SwitchToThisWindow (USER32.@)
159 void WINAPI SwitchToThisWindow( HWND hwnd, BOOL restore )
161 ShowWindow( hwnd, restore ? SW_RESTORE : SW_SHOWMINIMIZED );
165 /***********************************************************************
166 * GetWindowRect (USER32.@)
168 BOOL WINAPI GetWindowRect( HWND hwnd, LPRECT rect )
170 BOOL ret = WIN_GetRectangles( hwnd, rect, NULL );
171 if (ret)
173 MapWindowPoints( GetAncestor( hwnd, GA_PARENT ), 0, (POINT *)rect, 2 );
174 TRACE( "hwnd %p (%d,%d)-(%d,%d)\n",
175 hwnd, rect->left, rect->top, rect->right, rect->bottom);
177 return ret;
181 /***********************************************************************
182 * GetWindowRgn (USER32.@)
184 int WINAPI GetWindowRgn ( HWND hwnd, HRGN hrgn )
186 int nRet = ERROR;
187 NTSTATUS status;
188 HRGN win_rgn = 0;
189 RGNDATA *data;
190 size_t size = 256;
194 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
196 SetLastError( ERROR_OUTOFMEMORY );
197 return ERROR;
199 SERVER_START_REQ( get_window_region )
201 req->window = hwnd;
202 wine_server_set_reply( req, data->Buffer, size );
203 if (!(status = wine_server_call( req )))
205 size_t reply_size = wine_server_reply_size( reply );
206 if (reply_size)
208 data->rdh.dwSize = sizeof(data->rdh);
209 data->rdh.iType = RDH_RECTANGLES;
210 data->rdh.nCount = reply_size / sizeof(RECT);
211 data->rdh.nRgnSize = reply_size;
212 win_rgn = ExtCreateRegion( NULL, size, data );
215 else size = reply->total_size;
217 SERVER_END_REQ;
218 HeapFree( GetProcessHeap(), 0, data );
219 } while (status == STATUS_BUFFER_OVERFLOW);
221 if (status) SetLastError( RtlNtStatusToDosError(status) );
222 else if (win_rgn)
224 nRet = CombineRgn( hrgn, win_rgn, 0, RGN_COPY );
225 DeleteObject( win_rgn );
227 return nRet;
231 /***********************************************************************
232 * SetWindowRgn (USER32.@)
234 int WINAPI SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL bRedraw )
236 static const RECT empty_rect;
237 BOOL ret;
239 if (hrgn)
241 RGNDATA *data;
242 DWORD size;
244 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
245 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
246 if (!GetRegionData( hrgn, size, data ))
248 HeapFree( GetProcessHeap(), 0, data );
249 return FALSE;
251 SERVER_START_REQ( set_window_region )
253 req->window = hwnd;
254 req->redraw = (bRedraw != 0);
255 if (data->rdh.nCount)
256 wine_server_add_data( req, data->Buffer, data->rdh.nCount * sizeof(RECT) );
257 else
258 wine_server_add_data( req, &empty_rect, sizeof(empty_rect) );
259 ret = !wine_server_call_err( req );
261 SERVER_END_REQ;
263 else /* clear existing region */
265 SERVER_START_REQ( set_window_region )
267 req->window = hwnd;
268 req->redraw = (bRedraw != 0);
269 ret = !wine_server_call_err( req );
271 SERVER_END_REQ;
274 if (ret) ret = USER_Driver->pSetWindowRgn( hwnd, hrgn, bRedraw );
276 if (ret)
278 UINT swp_flags = SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_FRAMECHANGED;
279 if (hrgn) swp_flags |= SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE;
280 if (!bRedraw) swp_flags |= SWP_NOREDRAW;
281 SetWindowPos( hwnd, 0, 0, 0, 0, 0, swp_flags );
283 return ret;
287 /***********************************************************************
288 * GetClientRect (USER32.@)
290 BOOL WINAPI GetClientRect( HWND hwnd, LPRECT rect )
292 BOOL ret;
294 if ((ret = WIN_GetRectangles( hwnd, NULL, rect )))
296 rect->right -= rect->left;
297 rect->bottom -= rect->top;
298 rect->left = rect->top = 0;
300 return ret;
304 /*******************************************************************
305 * ClientToScreen (USER32.@)
307 BOOL WINAPI ClientToScreen( HWND hwnd, LPPOINT lppnt )
309 MapWindowPoints( hwnd, 0, lppnt, 1 );
310 return TRUE;
314 /*******************************************************************
315 * ScreenToClient (USER32.@)
317 BOOL WINAPI ScreenToClient( HWND hwnd, LPPOINT lppnt )
319 MapWindowPoints( 0, hwnd, lppnt, 1 );
320 return TRUE;
324 /***********************************************************************
325 * list_children_from_point
327 * Get the list of children that can contain point from the server.
328 * Point is in screen coordinates.
329 * Returned list must be freed by caller.
331 static HWND *list_children_from_point( HWND hwnd, POINT pt )
333 HWND *list;
334 int size = 32;
336 for (;;)
338 int count = 0;
340 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
342 SERVER_START_REQ( get_window_children_from_point )
344 req->parent = hwnd;
345 req->x = pt.x;
346 req->y = pt.y;
347 wine_server_set_reply( req, list, (size-1) * sizeof(HWND) );
348 if (!wine_server_call( req )) count = reply->count;
350 SERVER_END_REQ;
351 if (count && count < size)
353 list[count] = 0;
354 return list;
356 HeapFree( GetProcessHeap(), 0, list );
357 if (!count) break;
358 size = count + 1; /* restart with a large enough buffer */
360 return NULL;
364 /***********************************************************************
365 * WINPOS_WindowFromPoint
367 * Find the window and hittest for a given point.
369 HWND WINPOS_WindowFromPoint( HWND hwndScope, POINT pt, INT *hittest )
371 int i, res;
372 HWND ret, *list;
374 if (!hwndScope) hwndScope = GetDesktopWindow();
376 *hittest = HTNOWHERE;
378 if (!(list = list_children_from_point( hwndScope, pt ))) return 0;
380 /* now determine the hittest */
382 for (i = 0; list[i]; i++)
384 LONG style = GetWindowLongW( list[i], GWL_STYLE );
386 /* If window is minimized or disabled, return at once */
387 if (style & WS_MINIMIZE)
389 *hittest = HTCAPTION;
390 break;
392 if (style & WS_DISABLED)
394 *hittest = HTERROR;
395 break;
397 /* Send WM_NCCHITTEST (if same thread) */
398 if (!WIN_IsCurrentThread( list[i] ))
400 *hittest = HTCLIENT;
401 break;
403 res = SendMessageW( list[i], WM_NCHITTEST, 0, MAKELONG(pt.x,pt.y) );
404 if (res != HTTRANSPARENT)
406 *hittest = res; /* Found the window */
407 break;
409 /* continue search with next window in z-order */
411 ret = list[i];
412 HeapFree( GetProcessHeap(), 0, list );
413 TRACE( "scope %p (%d,%d) returning %p\n", hwndScope, pt.x, pt.y, ret );
414 return ret;
418 /*******************************************************************
419 * WindowFromPoint (USER32.@)
421 HWND WINAPI WindowFromPoint( POINT pt )
423 INT hittest;
424 return WINPOS_WindowFromPoint( 0, pt, &hittest );
428 /*******************************************************************
429 * ChildWindowFromPoint (USER32.@)
431 HWND WINAPI ChildWindowFromPoint( HWND hwndParent, POINT pt )
433 return ChildWindowFromPointEx( hwndParent, pt, CWP_ALL );
436 /*******************************************************************
437 * RealChildWindowFromPoint (USER32.@)
439 HWND WINAPI RealChildWindowFromPoint( HWND hwndParent, POINT pt )
441 return ChildWindowFromPointEx( hwndParent, pt, CWP_SKIPTRANSPARENT );
444 /*******************************************************************
445 * ChildWindowFromPointEx (USER32.@)
447 HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
449 /* pt is in the client coordinates */
450 HWND *list;
451 int i;
452 RECT rect;
453 HWND retvalue;
455 GetClientRect( hwndParent, &rect );
456 if (!PtInRect( &rect, pt )) return 0;
457 if (!(list = WIN_ListChildren( hwndParent ))) return hwndParent;
459 for (i = 0; list[i]; i++)
461 if (!WIN_GetRectangles( list[i], &rect, NULL )) continue;
462 if (!PtInRect( &rect, pt )) continue;
463 if (uFlags & (CWP_SKIPINVISIBLE|CWP_SKIPDISABLED))
465 LONG style = GetWindowLongW( list[i], GWL_STYLE );
466 if ((uFlags & CWP_SKIPINVISIBLE) && !(style & WS_VISIBLE)) continue;
467 if ((uFlags & CWP_SKIPDISABLED) && (style & WS_DISABLED)) continue;
469 if (uFlags & CWP_SKIPTRANSPARENT)
471 if (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TRANSPARENT) continue;
473 break;
475 retvalue = list[i];
476 HeapFree( GetProcessHeap(), 0, list );
477 if (!retvalue) retvalue = hwndParent;
478 return retvalue;
482 /*******************************************************************
483 * WINPOS_GetWinOffset
485 * Calculate the offset between the origin of the two windows. Used
486 * to implement MapWindowPoints.
488 static void WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo, POINT *offset )
490 WND * wndPtr;
492 offset->x = offset->y = 0;
494 /* Translate source window origin to screen coords */
495 if (hwndFrom)
497 HWND hwnd = hwndFrom;
499 while (hwnd)
501 if (hwnd == hwndTo) return;
502 if (!(wndPtr = WIN_GetPtr( hwnd )))
504 ERR( "bad hwndFrom = %p\n", hwnd );
505 return;
507 if (wndPtr == WND_DESKTOP) break;
508 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
509 offset->x += wndPtr->rectClient.left;
510 offset->y += wndPtr->rectClient.top;
511 hwnd = wndPtr->parent;
512 WIN_ReleasePtr( wndPtr );
516 /* Translate origin to destination window coords */
517 if (hwndTo)
519 HWND hwnd = hwndTo;
521 while (hwnd)
523 if (!(wndPtr = WIN_GetPtr( hwnd )))
525 ERR( "bad hwndTo = %p\n", hwnd );
526 return;
528 if (wndPtr == WND_DESKTOP) break;
529 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
530 offset->x -= wndPtr->rectClient.left;
531 offset->y -= wndPtr->rectClient.top;
532 hwnd = wndPtr->parent;
533 WIN_ReleasePtr( wndPtr );
536 return;
538 other_process: /* one of the parents may belong to another process, do it the hard way */
539 offset->x = offset->y = 0;
540 SERVER_START_REQ( get_windows_offset )
542 req->from = hwndFrom;
543 req->to = hwndTo;
544 if (!wine_server_call( req ))
546 offset->x = reply->x;
547 offset->y = reply->y;
550 SERVER_END_REQ;
554 /*******************************************************************
555 * MapWindowPoints (USER.258)
557 void WINAPI MapWindowPoints16( HWND16 hwndFrom, HWND16 hwndTo,
558 LPPOINT16 lppt, UINT16 count )
560 POINT offset;
562 WINPOS_GetWinOffset( WIN_Handle32(hwndFrom), WIN_Handle32(hwndTo), &offset );
563 while (count--)
565 lppt->x += offset.x;
566 lppt->y += offset.y;
567 lppt++;
572 /*******************************************************************
573 * MapWindowPoints (USER32.@)
575 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
577 POINT offset;
579 WINPOS_GetWinOffset( hwndFrom, hwndTo, &offset );
580 while (count--)
582 lppt->x += offset.x;
583 lppt->y += offset.y;
584 lppt++;
586 return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
590 /***********************************************************************
591 * IsIconic (USER32.@)
593 BOOL WINAPI IsIconic(HWND hWnd)
595 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
599 /***********************************************************************
600 * IsZoomed (USER32.@)
602 BOOL WINAPI IsZoomed(HWND hWnd)
604 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
608 /*******************************************************************
609 * AllowSetForegroundWindow (USER32.@)
611 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
613 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
614 * implemented, then fix this function. */
615 return TRUE;
619 /*******************************************************************
620 * LockSetForegroundWindow (USER32.@)
622 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
624 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
625 * implemented, then fix this function. */
626 return TRUE;
630 /***********************************************************************
631 * BringWindowToTop (USER32.@)
633 BOOL WINAPI BringWindowToTop( HWND hwnd )
635 return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
639 /***********************************************************************
640 * MoveWindow (USER32.@)
642 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
643 BOOL repaint )
645 int flags = SWP_NOZORDER | SWP_NOACTIVATE;
646 if (!repaint) flags |= SWP_NOREDRAW;
647 TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
648 return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
651 /***********************************************************************
652 * WINPOS_InitInternalPos
654 static LPINTERNALPOS WINPOS_InitInternalPos( WND* wnd )
656 LPINTERNALPOS lpPos = get_internal_pos( wnd->hwndSelf );
657 if( !lpPos )
659 /* this happens when the window is minimized/maximized
660 * for the first time (rectWindow is not adjusted yet) */
662 lpPos = HeapAlloc( GetProcessHeap(), 0, sizeof(INTERNALPOS) );
663 if( !lpPos ) return NULL;
665 set_internal_pos( wnd->hwndSelf, lpPos );
666 lpPos->hwndIconTitle = 0; /* defer until needs to be shown */
667 lpPos->rectNormal.left = wnd->rectWindow.left;
668 lpPos->rectNormal.top = wnd->rectWindow.top;
669 lpPos->rectNormal.right = wnd->rectWindow.right;
670 lpPos->rectNormal.bottom = wnd->rectWindow.bottom;
671 lpPos->ptIconPos.x = lpPos->ptIconPos.y = -1;
672 lpPos->ptMaxPos.x = lpPos->ptMaxPos.y = -1;
675 if( wnd->dwStyle & WS_MINIMIZE )
677 lpPos->ptIconPos.x = wnd->rectWindow.left;
678 lpPos->ptIconPos.y = wnd->rectWindow.top;
680 else if( wnd->dwStyle & WS_MAXIMIZE )
682 lpPos->ptMaxPos.x = wnd->rectWindow.left;
683 lpPos->ptMaxPos.y = wnd->rectWindow.top;
685 else
687 lpPos->rectNormal.left = wnd->rectWindow.left;
688 lpPos->rectNormal.top = wnd->rectWindow.top;
689 lpPos->rectNormal.right = wnd->rectWindow.right;
690 lpPos->rectNormal.bottom = wnd->rectWindow.bottom;
692 return lpPos;
695 /***********************************************************************
696 * WINPOS_RedrawIconTitle
698 BOOL WINPOS_RedrawIconTitle( HWND hWnd )
700 LPINTERNALPOS lpPos = get_internal_pos( hWnd );
701 if( lpPos )
703 if( lpPos->hwndIconTitle )
705 SendMessageW( lpPos->hwndIconTitle, WM_SHOWWINDOW, TRUE, 0);
706 InvalidateRect( lpPos->hwndIconTitle, NULL, TRUE );
707 return TRUE;
710 return FALSE;
713 /***********************************************************************
714 * WINPOS_ShowIconTitle
716 BOOL WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
718 LPINTERNALPOS lpPos = get_internal_pos( hwnd );
720 if (lpPos && !GetPropA( hwnd, "__wine_x11_managed" ))
722 HWND title = lpPos->hwndIconTitle;
724 TRACE("%p %i\n", hwnd, (bShow != 0) );
726 if( !title )
727 lpPos->hwndIconTitle = title = ICONTITLE_Create( hwnd );
728 if( bShow )
730 if (!IsWindowVisible(title))
732 SendMessageW( title, WM_SHOWWINDOW, TRUE, 0 );
733 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
734 SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
737 else ShowWindow( title, SW_HIDE );
739 return FALSE;
742 /*******************************************************************
743 * WINPOS_GetMinMaxInfo
745 * Get the minimized and maximized information for a window.
747 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
748 POINT *minTrack, POINT *maxTrack )
750 LPINTERNALPOS lpPos;
751 MINMAXINFO MinMax;
752 HMONITOR monitor;
753 INT xinc, yinc;
754 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
755 LONG exstyle = GetWindowLongA( hwnd, GWL_EXSTYLE );
756 RECT rc;
758 /* Compute default values */
760 GetWindowRect(hwnd, &rc);
761 MinMax.ptReserved.x = rc.left;
762 MinMax.ptReserved.y = rc.top;
764 if (style & WS_CHILD)
766 if ((style & WS_CAPTION) == WS_CAPTION)
767 style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
769 GetClientRect(GetAncestor(hwnd,GA_PARENT), &rc);
770 AdjustWindowRectEx(&rc, style, ((style & WS_POPUP) && GetMenu(hwnd)), exstyle);
772 /* avoid calculating this twice */
773 style &= ~(WS_DLGFRAME | WS_BORDER | WS_THICKFRAME);
775 MinMax.ptMaxSize.x = rc.right - rc.left;
776 MinMax.ptMaxSize.y = rc.bottom - rc.top;
778 else
780 MinMax.ptMaxSize.x = GetSystemMetrics(SM_CXSCREEN);
781 MinMax.ptMaxSize.y = GetSystemMetrics(SM_CYSCREEN);
783 MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
784 MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
785 MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXMAXTRACK);
786 MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYMAXTRACK);
788 if (HAS_DLGFRAME( style, exstyle ))
790 xinc = GetSystemMetrics(SM_CXDLGFRAME);
791 yinc = GetSystemMetrics(SM_CYDLGFRAME);
793 else
795 xinc = yinc = 0;
796 if (HAS_THICKFRAME(style))
798 xinc += GetSystemMetrics(SM_CXFRAME);
799 yinc += GetSystemMetrics(SM_CYFRAME);
801 if (style & WS_BORDER)
803 xinc += GetSystemMetrics(SM_CXBORDER);
804 yinc += GetSystemMetrics(SM_CYBORDER);
807 MinMax.ptMaxSize.x += 2 * xinc;
808 MinMax.ptMaxSize.y += 2 * yinc;
810 lpPos = get_internal_pos( hwnd );
811 if( lpPos && !EMPTYPOINT(lpPos->ptMaxPos) )
813 MinMax.ptMaxPosition.x = lpPos->ptMaxPos.x;
814 MinMax.ptMaxPosition.y = lpPos->ptMaxPos.y;
816 else
818 MinMax.ptMaxPosition.x = -xinc;
819 MinMax.ptMaxPosition.y = -yinc;
822 SendMessageW( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
824 /* if the app didn't change the values, adapt them for the current monitor */
826 if ((monitor = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY )))
828 MONITORINFO mon_info;
830 mon_info.cbSize = sizeof(mon_info);
831 GetMonitorInfoW( monitor, &mon_info );
833 if (MinMax.ptMaxSize.x == GetSystemMetrics(SM_CXSCREEN) + 2 * xinc &&
834 MinMax.ptMaxSize.y == GetSystemMetrics(SM_CYSCREEN) + 2 * yinc)
836 MinMax.ptMaxSize.x = (mon_info.rcWork.right - mon_info.rcWork.left) + 2 * xinc;
837 MinMax.ptMaxSize.y = (mon_info.rcWork.bottom - mon_info.rcWork.top) + 2 * yinc;
839 if (MinMax.ptMaxPosition.x == -xinc && MinMax.ptMaxPosition.y == -yinc)
841 MinMax.ptMaxPosition.x = mon_info.rcWork.left - xinc;
842 MinMax.ptMaxPosition.y = mon_info.rcWork.top - yinc;
846 /* Some sanity checks */
848 TRACE("%d %d / %d %d / %d %d / %d %d\n",
849 MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
850 MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
851 MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
852 MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
853 MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
854 MinMax.ptMinTrackSize.x );
855 MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
856 MinMax.ptMinTrackSize.y );
858 if (maxSize) *maxSize = MinMax.ptMaxSize;
859 if (maxPos) *maxPos = MinMax.ptMaxPosition;
860 if (minTrack) *minTrack = MinMax.ptMinTrackSize;
861 if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
864 /***********************************************************************
865 * ShowWindowAsync (USER32.@)
867 * doesn't wait; returns immediately.
868 * used by threads to toggle windows in other (possibly hanging) threads
870 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
872 HWND full_handle;
874 if (is_broadcast(hwnd))
876 SetLastError( ERROR_INVALID_PARAMETER );
877 return FALSE;
880 if ((full_handle = WIN_IsCurrentThread( hwnd )))
881 return USER_Driver->pShowWindow( full_handle, cmd );
883 return SendNotifyMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
887 /***********************************************************************
888 * ShowWindow (USER32.@)
890 BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
892 HWND full_handle;
894 if (is_broadcast(hwnd))
896 SetLastError( ERROR_INVALID_PARAMETER );
897 return FALSE;
899 if ((full_handle = WIN_IsCurrentThread( hwnd )))
900 return USER_Driver->pShowWindow( full_handle, cmd );
902 return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
906 /***********************************************************************
907 * GetInternalWindowPos (USER32.@)
909 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
910 LPPOINT ptIcon )
912 WINDOWPLACEMENT wndpl;
913 if (GetWindowPlacement( hwnd, &wndpl ))
915 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
916 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
917 return wndpl.showCmd;
919 return 0;
923 /***********************************************************************
924 * GetWindowPlacement (USER32.@)
926 * Win95:
927 * Fails if wndpl->length of Win95 (!) apps is invalid.
929 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
931 WND *pWnd = WIN_GetPtr( hwnd );
932 LPINTERNALPOS lpPos;
934 if (!pWnd || pWnd == WND_DESKTOP) return FALSE;
935 if (pWnd == WND_OTHER_PROCESS)
937 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
938 return FALSE;
941 lpPos = WINPOS_InitInternalPos( pWnd );
942 wndpl->length = sizeof(*wndpl);
943 if( pWnd->dwStyle & WS_MINIMIZE )
944 wndpl->showCmd = SW_SHOWMINIMIZED;
945 else
946 wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
947 if( pWnd->flags & WIN_RESTORE_MAX )
948 wndpl->flags = WPF_RESTORETOMAXIMIZED;
949 else
950 wndpl->flags = 0;
951 wndpl->ptMinPosition.x = lpPos->ptIconPos.x;
952 wndpl->ptMinPosition.y = lpPos->ptIconPos.y;
953 wndpl->ptMaxPosition.x = lpPos->ptMaxPos.x;
954 wndpl->ptMaxPosition.y = lpPos->ptMaxPos.y;
955 wndpl->rcNormalPosition.left = lpPos->rectNormal.left;
956 wndpl->rcNormalPosition.top = lpPos->rectNormal.top;
957 wndpl->rcNormalPosition.right = lpPos->rectNormal.right;
958 wndpl->rcNormalPosition.bottom = lpPos->rectNormal.bottom;
959 WIN_ReleasePtr( pWnd );
960 return TRUE;
964 /***********************************************************************
965 * WINPOS_SetPlacement
967 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
969 LPINTERNALPOS lpPos;
970 DWORD style;
971 WND *pWnd = WIN_GetPtr( hwnd );
973 if (!pWnd || pWnd == WND_OTHER_PROCESS || pWnd == WND_DESKTOP) return FALSE;
974 lpPos = WINPOS_InitInternalPos( pWnd );
976 if( flags & PLACE_MIN )
978 lpPos->ptIconPos.x = wndpl->ptMinPosition.x;
979 lpPos->ptIconPos.y = wndpl->ptMinPosition.y;
981 if( flags & PLACE_MAX )
983 lpPos->ptMaxPos.x = wndpl->ptMaxPosition.x;
984 lpPos->ptMaxPos.y = wndpl->ptMaxPosition.y;
986 if( flags & PLACE_RECT)
988 lpPos->rectNormal.left = wndpl->rcNormalPosition.left;
989 lpPos->rectNormal.top = wndpl->rcNormalPosition.top;
990 lpPos->rectNormal.right = wndpl->rcNormalPosition.right;
991 lpPos->rectNormal.bottom = wndpl->rcNormalPosition.bottom;
994 style = pWnd->dwStyle;
995 WIN_ReleasePtr( pWnd );
997 if( style & WS_MINIMIZE )
999 WINPOS_ShowIconTitle( hwnd, FALSE );
1000 if( wndpl->flags & WPF_SETMINPOSITION && !EMPTYPOINT(lpPos->ptIconPos))
1001 SetWindowPos( hwnd, 0, lpPos->ptIconPos.x, lpPos->ptIconPos.y,
1002 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1004 else if( style & WS_MAXIMIZE )
1006 if( !EMPTYPOINT(lpPos->ptMaxPos) )
1007 SetWindowPos( hwnd, 0, lpPos->ptMaxPos.x, lpPos->ptMaxPos.y,
1008 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1010 else if( flags & PLACE_RECT )
1011 SetWindowPos( hwnd, 0, lpPos->rectNormal.left, lpPos->rectNormal.top,
1012 lpPos->rectNormal.right - lpPos->rectNormal.left,
1013 lpPos->rectNormal.bottom - lpPos->rectNormal.top,
1014 SWP_NOZORDER | SWP_NOACTIVATE );
1016 ShowWindow( hwnd, wndpl->showCmd );
1018 if (IsIconic( hwnd ))
1020 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE) WINPOS_ShowIconTitle( hwnd, TRUE );
1022 /* SDK: ...valid only the next time... */
1023 if( wndpl->flags & WPF_RESTORETOMAXIMIZED )
1025 pWnd = WIN_GetPtr( hwnd );
1026 if (pWnd && pWnd != WND_OTHER_PROCESS)
1028 pWnd->flags |= WIN_RESTORE_MAX;
1029 WIN_ReleasePtr( pWnd );
1033 return TRUE;
1037 /***********************************************************************
1038 * SetWindowPlacement (USER32.@)
1040 * Win95:
1041 * Fails if wndpl->length of Win95 (!) apps is invalid.
1043 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
1045 if (!wpl) return FALSE;
1046 return WINPOS_SetPlacement( hwnd, wpl, PLACE_MIN | PLACE_MAX | PLACE_RECT );
1050 /***********************************************************************
1051 * AnimateWindow (USER32.@)
1052 * Shows/Hides a window with an animation
1053 * NO ANIMATION YET
1055 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
1057 FIXME("partial stub\n");
1059 /* If trying to show/hide and it's already *
1060 * shown/hidden or invalid window, fail with *
1061 * invalid parameter */
1062 if(!IsWindow(hwnd) ||
1063 (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
1064 (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
1066 SetLastError(ERROR_INVALID_PARAMETER);
1067 return FALSE;
1070 ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1072 return TRUE;
1075 /***********************************************************************
1076 * SetInternalWindowPos (USER32.@)
1078 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1079 LPRECT rect, LPPOINT pt )
1081 if( IsWindow(hwnd) )
1083 WINDOWPLACEMENT wndpl;
1084 UINT flags;
1086 wndpl.length = sizeof(wndpl);
1087 wndpl.showCmd = showCmd;
1088 wndpl.flags = flags = 0;
1090 if( pt )
1092 flags |= PLACE_MIN;
1093 wndpl.flags |= WPF_SETMINPOSITION;
1094 wndpl.ptMinPosition = *pt;
1096 if( rect )
1098 flags |= PLACE_RECT;
1099 wndpl.rcNormalPosition = *rect;
1101 WINPOS_SetPlacement( hwnd, &wndpl, flags );
1106 /*******************************************************************
1107 * can_activate_window
1109 * Check if we can activate the specified window.
1111 static BOOL can_activate_window( HWND hwnd )
1113 LONG style;
1115 if (!hwnd) return FALSE;
1116 style = GetWindowLongW( hwnd, GWL_STYLE );
1117 if (!(style & WS_VISIBLE)) return FALSE;
1118 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1119 return !(style & WS_DISABLED);
1123 /*******************************************************************
1124 * WINPOS_ActivateOtherWindow
1126 * Activates window other than pWnd.
1128 void WINPOS_ActivateOtherWindow(HWND hwnd)
1130 HWND hwndTo, fg;
1132 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1134 hwndTo = GetAncestor( hwndTo, GA_ROOT );
1135 if (can_activate_window( hwndTo )) goto done;
1138 hwndTo = hwnd;
1139 for (;;)
1141 if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1142 if (can_activate_window( hwndTo )) break;
1145 done:
1146 fg = GetForegroundWindow();
1147 TRACE("win = %p fg = %p\n", hwndTo, fg);
1148 if (!fg || (hwnd == fg))
1150 if (SetForegroundWindow( hwndTo )) return;
1152 if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1156 /***********************************************************************
1157 * WINPOS_HandleWindowPosChanging
1159 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1161 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1163 POINT minTrack, maxTrack;
1164 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1166 if (winpos->flags & SWP_NOSIZE) return 0;
1167 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1169 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1170 if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1171 if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1172 if (!(style & WS_MINIMIZE))
1174 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1175 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1178 return 0;
1182 /***********************************************************************
1183 * dump_winpos_flags
1185 static void dump_winpos_flags(UINT flags)
1187 TRACE("flags:");
1188 if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1189 if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1190 if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1191 if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1192 if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1193 if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1194 if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1195 if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1196 if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1197 if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1198 if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1199 if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1200 if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1202 #define DUMPED_FLAGS \
1203 (SWP_NOSIZE | \
1204 SWP_NOMOVE | \
1205 SWP_NOZORDER | \
1206 SWP_NOREDRAW | \
1207 SWP_NOACTIVATE | \
1208 SWP_FRAMECHANGED | \
1209 SWP_SHOWWINDOW | \
1210 SWP_HIDEWINDOW | \
1211 SWP_NOCOPYBITS | \
1212 SWP_NOOWNERZORDER | \
1213 SWP_NOSENDCHANGING | \
1214 SWP_DEFERERASE | \
1215 SWP_ASYNCWINDOWPOS)
1217 if(flags & ~DUMPED_FLAGS) TRACE(" %08x", flags & ~DUMPED_FLAGS);
1218 TRACE("\n");
1219 #undef DUMPED_FLAGS
1222 /***********************************************************************
1223 * SWP_DoWinPosChanging
1225 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
1227 WND *wndPtr;
1229 /* Send WM_WINDOWPOSCHANGING message */
1231 if (!(pWinpos->flags & SWP_NOSENDCHANGING))
1232 SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
1234 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) ||
1235 wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
1237 /* Calculate new position and size */
1239 *pNewWindowRect = wndPtr->rectWindow;
1240 *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
1241 : wndPtr->rectClient;
1243 if (!(pWinpos->flags & SWP_NOSIZE))
1245 pNewWindowRect->right = pNewWindowRect->left + pWinpos->cx;
1246 pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
1248 if (!(pWinpos->flags & SWP_NOMOVE))
1250 pNewWindowRect->left = pWinpos->x;
1251 pNewWindowRect->top = pWinpos->y;
1252 pNewWindowRect->right += pWinpos->x - wndPtr->rectWindow.left;
1253 pNewWindowRect->bottom += pWinpos->y - wndPtr->rectWindow.top;
1255 OffsetRect( pNewClientRect, pWinpos->x - wndPtr->rectWindow.left,
1256 pWinpos->y - wndPtr->rectWindow.top );
1258 pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
1260 TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
1261 pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
1262 pWinpos->cx, pWinpos->cy, pWinpos->flags );
1263 TRACE( "current %s style %08x new %s\n",
1264 wine_dbgstr_rect( &wndPtr->rectWindow ), wndPtr->dwStyle,
1265 wine_dbgstr_rect( pNewWindowRect ));
1267 WIN_ReleasePtr( wndPtr );
1268 return TRUE;
1271 /***********************************************************************
1272 * get_valid_rects
1274 * Compute the valid rects from the old and new client rect and WVR_* flags.
1275 * Helper for WM_NCCALCSIZE handling.
1277 static inline void get_valid_rects( const RECT *old_client, const RECT *new_client, UINT flags,
1278 RECT *valid )
1280 int cx, cy;
1282 if (flags & WVR_REDRAW)
1284 SetRectEmpty( &valid[0] );
1285 SetRectEmpty( &valid[1] );
1286 return;
1289 if (flags & WVR_VALIDRECTS)
1291 if (!IntersectRect( &valid[0], &valid[0], new_client ) ||
1292 !IntersectRect( &valid[1], &valid[1], old_client ))
1294 SetRectEmpty( &valid[0] );
1295 SetRectEmpty( &valid[1] );
1296 return;
1298 flags = WVR_ALIGNLEFT | WVR_ALIGNTOP;
1300 else
1302 valid[0] = *new_client;
1303 valid[1] = *old_client;
1306 /* make sure the rectangles have the same size */
1307 cx = min( valid[0].right - valid[0].left, valid[1].right - valid[1].left );
1308 cy = min( valid[0].bottom - valid[0].top, valid[1].bottom - valid[1].top );
1310 if (flags & WVR_ALIGNBOTTOM)
1312 valid[0].top = valid[0].bottom - cy;
1313 valid[1].top = valid[1].bottom - cy;
1315 else
1317 valid[0].bottom = valid[0].top + cy;
1318 valid[1].bottom = valid[1].top + cy;
1320 if (flags & WVR_ALIGNRIGHT)
1322 valid[0].left = valid[0].right - cx;
1323 valid[1].left = valid[1].right - cx;
1325 else
1327 valid[0].right = valid[0].left + cx;
1328 valid[1].right = valid[1].left + cx;
1332 struct move_owned_info
1334 HWND owner;
1335 HWND insert_after;
1338 static BOOL CALLBACK move_owned_popups( HWND hwnd, LPARAM lparam )
1340 struct move_owned_info *info = (struct move_owned_info *)lparam;
1342 if (hwnd == info->owner) return FALSE;
1343 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) &&
1344 GetWindow( hwnd, GW_OWNER ) == info->owner)
1346 SetWindowPos( hwnd, info->insert_after, 0, 0, 0, 0,
1347 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE |
1348 SWP_NOSENDCHANGING | SWP_DEFERERASE );
1349 info->insert_after = hwnd;
1351 return TRUE;
1354 /***********************************************************************
1355 * SWP_DoOwnedPopups
1357 * fix Z order taking into account owned popups -
1358 * basically we need to maintain them above the window that owns them
1360 * FIXME: hide/show owned popups when owner visibility changes.
1362 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
1364 HWND owner = GetWindow( hwnd, GW_OWNER );
1365 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1366 struct move_owned_info info;
1368 TRACE("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
1370 if ((style & WS_POPUP) && owner)
1372 /* make sure this popup stays above the owner */
1374 if (hwndInsertAfter != HWND_TOP && hwndInsertAfter != HWND_TOPMOST)
1376 HWND hwndLocalPrev = HWND_TOP;
1377 HWND prev = GetWindow( owner, GW_HWNDPREV );
1379 while (prev && prev != hwndInsertAfter)
1381 if (hwndLocalPrev == HWND_TOP && GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE)
1382 hwndLocalPrev = prev;
1383 prev = GetWindow( prev, GW_HWNDPREV );
1385 if (!prev) hwndInsertAfter = hwndLocalPrev;
1388 else if (style & WS_CHILD) return hwndInsertAfter;
1390 info.owner = hwnd;
1391 info.insert_after = hwndInsertAfter;
1392 EnumWindows( move_owned_popups, (LPARAM)&info );
1393 return info.insert_after;
1396 /***********************************************************************
1397 * SWP_DoNCCalcSize
1399 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect,
1400 RECT *validRects )
1402 UINT wvrFlags = 0;
1403 WND *wndPtr;
1405 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
1407 /* Send WM_NCCALCSIZE message to get new client area */
1408 if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
1410 NCCALCSIZE_PARAMS params;
1411 WINDOWPOS winposCopy;
1413 params.rgrc[0] = *pNewWindowRect;
1414 params.rgrc[1] = wndPtr->rectWindow;
1415 params.rgrc[2] = wndPtr->rectClient;
1416 params.lppos = &winposCopy;
1417 winposCopy = *pWinpos;
1418 WIN_ReleasePtr( wndPtr );
1420 wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
1422 *pNewClientRect = params.rgrc[0];
1424 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
1426 TRACE( "hwnd %p old win %s old client %s new win %s new client %s\n", pWinpos->hwnd,
1427 wine_dbgstr_rect(&wndPtr->rectWindow), wine_dbgstr_rect(&wndPtr->rectClient),
1428 wine_dbgstr_rect(pNewWindowRect), wine_dbgstr_rect(pNewClientRect) );
1430 if( pNewClientRect->left != wndPtr->rectClient.left ||
1431 pNewClientRect->top != wndPtr->rectClient.top )
1432 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1434 if( (pNewClientRect->right - pNewClientRect->left !=
1435 wndPtr->rectClient.right - wndPtr->rectClient.left))
1436 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1437 else
1438 wvrFlags &= ~WVR_HREDRAW;
1440 if (pNewClientRect->bottom - pNewClientRect->top !=
1441 wndPtr->rectClient.bottom - wndPtr->rectClient.top)
1442 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1443 else
1444 wvrFlags &= ~WVR_VREDRAW;
1446 validRects[0] = params.rgrc[1];
1447 validRects[1] = params.rgrc[2];
1449 else
1451 if (!(pWinpos->flags & SWP_NOMOVE) &&
1452 (pNewClientRect->left != wndPtr->rectClient.left ||
1453 pNewClientRect->top != wndPtr->rectClient.top))
1454 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1457 if (pWinpos->flags & (SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
1459 SetRectEmpty( &validRects[0] );
1460 SetRectEmpty( &validRects[1] );
1462 else get_valid_rects( &wndPtr->rectClient, pNewClientRect, wvrFlags, validRects );
1464 WIN_ReleasePtr( wndPtr );
1465 return wvrFlags;
1468 /* fix redundant flags and values in the WINDOWPOS structure */
1469 static BOOL fixup_flags( WINDOWPOS *winpos )
1471 HWND parent;
1472 WND *wndPtr = WIN_GetPtr( winpos->hwnd );
1473 BOOL ret = TRUE;
1475 if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
1477 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1478 return FALSE;
1480 winpos->hwnd = wndPtr->hwndSelf; /* make it a full handle */
1482 /* Finally make sure that all coordinates are valid */
1483 if (winpos->x < -32768) winpos->x = -32768;
1484 else if (winpos->x > 32767) winpos->x = 32767;
1485 if (winpos->y < -32768) winpos->y = -32768;
1486 else if (winpos->y > 32767) winpos->y = 32767;
1488 if (winpos->cx < 0) winpos->cx = 0;
1489 else if (winpos->cx > 32767) winpos->cx = 32767;
1490 if (winpos->cy < 0) winpos->cy = 0;
1491 else if (winpos->cy > 32767) winpos->cy = 32767;
1493 parent = GetAncestor( winpos->hwnd, GA_PARENT );
1494 if (!IsWindowVisible( parent )) winpos->flags |= SWP_NOREDRAW;
1496 if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
1497 else
1499 winpos->flags &= ~SWP_HIDEWINDOW;
1500 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
1503 if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
1504 (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
1505 winpos->flags |= SWP_NOSIZE; /* Already the right size */
1507 if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
1508 winpos->flags |= SWP_NOMOVE; /* Already the right position */
1510 if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
1512 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)) && /* Bring to the top when activating */
1513 (winpos->flags & SWP_NOZORDER ||
1514 (winpos->hwndInsertAfter != HWND_TOPMOST && winpos->hwndInsertAfter != HWND_NOTOPMOST)))
1516 winpos->flags &= ~SWP_NOZORDER;
1517 winpos->hwndInsertAfter = HWND_TOP;
1521 /* Check hwndInsertAfter */
1522 if (winpos->flags & SWP_NOZORDER) goto done;
1524 /* fix sign extension */
1525 if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
1526 else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
1528 /* hwndInsertAfter must be a sibling of the window */
1529 if (winpos->hwndInsertAfter == HWND_TOP)
1531 if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1532 winpos->flags |= SWP_NOZORDER;
1534 else if (winpos->hwndInsertAfter == HWND_BOTTOM)
1536 if (!(wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
1537 winpos->flags |= SWP_NOZORDER;
1539 else if (winpos->hwndInsertAfter == HWND_TOPMOST)
1541 if ((wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1542 winpos->flags |= SWP_NOZORDER;
1544 else if (winpos->hwndInsertAfter == HWND_NOTOPMOST)
1546 if (!(wndPtr->dwExStyle & WS_EX_TOPMOST))
1547 winpos->flags |= SWP_NOZORDER;
1549 else
1551 if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != parent) ret = FALSE;
1552 else
1554 /* don't need to change the Zorder of hwnd if it's already inserted
1555 * after hwndInsertAfter or when inserting hwnd after itself.
1557 if ((winpos->hwnd == winpos->hwndInsertAfter) ||
1558 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
1559 winpos->flags |= SWP_NOZORDER;
1562 done:
1563 WIN_ReleasePtr( wndPtr );
1564 return ret;
1567 /***********************************************************************
1568 * USER_SetWindowPos
1570 * User32 internal function
1572 BOOL USER_SetWindowPos( WINDOWPOS * winpos )
1574 RECT newWindowRect, newClientRect, valid_rects[2];
1575 UINT orig_flags;
1577 orig_flags = winpos->flags;
1579 /* First make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
1580 if (!(winpos->flags & SWP_NOMOVE))
1582 if (winpos->x < -32768) winpos->x = -32768;
1583 else if (winpos->x > 32767) winpos->x = 32767;
1584 if (winpos->y < -32768) winpos->y = -32768;
1585 else if (winpos->y > 32767) winpos->y = 32767;
1587 if (!(winpos->flags & SWP_NOSIZE))
1589 if (winpos->cx < 0) winpos->cx = 0;
1590 else if (winpos->cx > 32767) winpos->cx = 32767;
1591 if (winpos->cy < 0) winpos->cy = 0;
1592 else if (winpos->cy > 32767) winpos->cy = 32767;
1595 if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
1597 /* Fix redundant flags */
1598 if (!fixup_flags( winpos )) return FALSE;
1600 if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
1602 if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
1603 winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
1606 /* Common operations */
1608 SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect, valid_rects );
1610 if(!USER_Driver->pSetWindowPos( winpos->hwnd, winpos->hwndInsertAfter,
1611 &newWindowRect, &newClientRect, orig_flags, valid_rects ))
1612 return FALSE;
1614 /* erase parent when hiding or resizing child */
1615 if (!(orig_flags & SWP_DEFERERASE) &&
1616 ((orig_flags & SWP_HIDEWINDOW) ||
1617 (!(orig_flags & SWP_SHOWWINDOW) &&
1618 (winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOGEOMETRYCHANGE)))
1620 HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
1621 if (!parent || parent == GetDesktopWindow()) parent = winpos->hwnd;
1622 erase_now( parent, 0 );
1625 if( winpos->flags & SWP_HIDEWINDOW )
1626 HideCaret(winpos->hwnd);
1627 else if (winpos->flags & SWP_SHOWWINDOW)
1628 ShowCaret(winpos->hwnd);
1630 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)))
1632 /* child windows get WM_CHILDACTIVATE message */
1633 if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
1634 SendMessageW( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
1635 else
1636 SetForegroundWindow( winpos->hwnd );
1639 /* And last, send the WM_WINDOWPOSCHANGED message */
1641 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
1643 if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
1645 /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
1646 and always contains final window position.
1648 winpos->x = newWindowRect.left;
1649 winpos->y = newWindowRect.top;
1650 winpos->cx = newWindowRect.right - newWindowRect.left;
1651 winpos->cy = newWindowRect.bottom - newWindowRect.top;
1652 SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
1654 return TRUE;
1657 /***********************************************************************
1658 * SetWindowPos (USER32.@)
1660 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
1661 INT x, INT y, INT cx, INT cy, UINT flags )
1663 WINDOWPOS winpos;
1665 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1666 hwnd, hwndInsertAfter, x, y, cx, cy, flags);
1667 if(TRACE_ON(win)) dump_winpos_flags(flags);
1669 if (is_broadcast(hwnd))
1671 SetLastError( ERROR_INVALID_PARAMETER );
1672 return FALSE;
1675 winpos.hwnd = WIN_GetFullHandle(hwnd);
1676 winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
1677 winpos.x = x;
1678 winpos.y = y;
1679 winpos.cx = cx;
1680 winpos.cy = cy;
1681 winpos.flags = flags;
1683 if (WIN_IsCurrentThread( hwnd ))
1684 return USER_SetWindowPos(&winpos);
1686 return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
1690 /***********************************************************************
1691 * BeginDeferWindowPos (USER32.@)
1693 HDWP WINAPI BeginDeferWindowPos( INT count )
1695 HDWP handle;
1696 DWP *pDWP;
1698 TRACE("%d\n", count);
1700 if (count < 0)
1702 SetLastError(ERROR_INVALID_PARAMETER);
1703 return 0;
1705 /* Windows allows zero count, in which case it allocates context for 8 moves */
1706 if (count == 0) count = 8;
1708 handle = USER_HEAP_ALLOC( sizeof(DWP) + (count-1)*sizeof(WINDOWPOS) );
1709 if (!handle) return 0;
1710 pDWP = (DWP *) USER_HEAP_LIN_ADDR( handle );
1711 pDWP->actualCount = 0;
1712 pDWP->suggestedCount = count;
1713 pDWP->valid = TRUE;
1714 pDWP->wMagic = DWP_MAGIC;
1715 pDWP->hwndParent = 0;
1717 TRACE("returning hdwp %p\n", handle);
1718 return handle;
1722 /***********************************************************************
1723 * DeferWindowPos (USER32.@)
1725 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
1726 INT x, INT y, INT cx, INT cy,
1727 UINT flags )
1729 DWP *pDWP;
1730 int i;
1731 HDWP newhdwp = hdwp,retvalue;
1733 TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1734 hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
1736 hwnd = WIN_GetFullHandle( hwnd );
1737 if (hwnd == GetDesktopWindow()) return 0;
1739 if (!(pDWP = USER_HEAP_LIN_ADDR( hdwp ))) return 0;
1741 USER_Lock();
1743 for (i = 0; i < pDWP->actualCount; i++)
1745 if (pDWP->winPos[i].hwnd == hwnd)
1747 /* Merge with the other changes */
1748 if (!(flags & SWP_NOZORDER))
1750 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
1752 if (!(flags & SWP_NOMOVE))
1754 pDWP->winPos[i].x = x;
1755 pDWP->winPos[i].y = y;
1757 if (!(flags & SWP_NOSIZE))
1759 pDWP->winPos[i].cx = cx;
1760 pDWP->winPos[i].cy = cy;
1762 pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
1763 SWP_NOZORDER | SWP_NOREDRAW |
1764 SWP_NOACTIVATE | SWP_NOCOPYBITS|
1765 SWP_NOOWNERZORDER);
1766 pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
1767 SWP_FRAMECHANGED);
1768 retvalue = hdwp;
1769 goto END;
1772 if (pDWP->actualCount >= pDWP->suggestedCount)
1774 newhdwp = USER_HEAP_REALLOC( hdwp,
1775 sizeof(DWP) + pDWP->suggestedCount*sizeof(WINDOWPOS) );
1776 if (!newhdwp)
1778 retvalue = 0;
1779 goto END;
1781 pDWP = (DWP *) USER_HEAP_LIN_ADDR( newhdwp );
1782 pDWP->suggestedCount++;
1784 pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
1785 pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
1786 pDWP->winPos[pDWP->actualCount].x = x;
1787 pDWP->winPos[pDWP->actualCount].y = y;
1788 pDWP->winPos[pDWP->actualCount].cx = cx;
1789 pDWP->winPos[pDWP->actualCount].cy = cy;
1790 pDWP->winPos[pDWP->actualCount].flags = flags;
1791 pDWP->actualCount++;
1792 retvalue = newhdwp;
1793 END:
1794 USER_Unlock();
1795 return retvalue;
1799 /***********************************************************************
1800 * EndDeferWindowPos (USER32.@)
1802 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
1804 DWP *pDWP;
1805 WINDOWPOS *winpos;
1806 BOOL res = TRUE;
1807 int i;
1809 TRACE("%p\n", hdwp);
1811 pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
1812 if (!pDWP) return FALSE;
1813 for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
1815 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1816 winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
1817 winpos->cx, winpos->cy, winpos->flags);
1819 if (!(res = USER_SetWindowPos( winpos ))) break;
1821 USER_HEAP_FREE( hdwp );
1822 return res;
1826 /***********************************************************************
1827 * TileChildWindows (USER.199)
1829 void WINAPI TileChildWindows16( HWND16 parent, WORD action )
1831 FIXME("(%04x, %d): stub\n", parent, action);
1834 /***********************************************************************
1835 * CascadeChildWindows (USER.198)
1837 void WINAPI CascadeChildWindows16( HWND16 parent, WORD action )
1839 FIXME("(%04x, %d): stub\n", parent, action);