dplay: Directplay should initialize session Guid, with conformance tests.
[wine/testsucceed.git] / dlls / user32 / winpos.c
blob46f9a262e2eb802945d83d17d9ea1fe972d0799a
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/winuser16.h"
35 #include "wine/server.h"
36 #include "controls.h"
37 #include "user_private.h"
38 #include "win.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(win);
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) ((*(LONG*)&(pt)) == -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;
75 typedef struct
77 RECT16 rectNormal;
78 POINT16 ptIconPos;
79 POINT16 ptMaxPos;
80 HWND hwndIconTitle;
81 } INTERNALPOS, *LPINTERNALPOS;
83 /* ----- internal functions ----- */
85 static const WCHAR SysIP_W[] = { 'S','y','s','I','P',0 };
87 static inline INTERNALPOS *get_internal_pos( HWND hwnd )
89 return GetPropW( hwnd, SysIP_W );
92 static inline void set_internal_pos( HWND hwnd, INTERNALPOS *pos )
94 SetPropW( hwnd, SysIP_W, pos );
97 /***********************************************************************
98 * WINPOS_CheckInternalPos
100 * Called when a window is destroyed.
102 void WINPOS_CheckInternalPos( HWND hwnd )
104 LPINTERNALPOS lpPos = get_internal_pos( hwnd );
106 if ( lpPos )
108 if( IsWindow(lpPos->hwndIconTitle) )
109 DestroyWindow( lpPos->hwndIconTitle );
110 HeapFree( GetProcessHeap(), 0, lpPos );
114 /***********************************************************************
115 * ArrangeIconicWindows (USER32.@)
117 UINT WINAPI ArrangeIconicWindows( HWND parent )
119 RECT rectParent;
120 HWND hwndChild;
121 INT x, y, xspacing, yspacing;
123 GetClientRect( parent, &rectParent );
124 x = rectParent.left;
125 y = rectParent.bottom;
126 xspacing = GetSystemMetrics(SM_CXICONSPACING);
127 yspacing = GetSystemMetrics(SM_CYICONSPACING);
129 hwndChild = GetWindow( parent, GW_CHILD );
130 while (hwndChild)
132 if( IsIconic( hwndChild ) )
134 WINPOS_ShowIconTitle( hwndChild, FALSE );
136 SetWindowPos( hwndChild, 0, x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
137 y - yspacing - GetSystemMetrics(SM_CYICON)/2, 0, 0,
138 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
139 if( IsWindow(hwndChild) )
140 WINPOS_ShowIconTitle(hwndChild , TRUE );
142 if (x <= rectParent.right - xspacing) x += xspacing;
143 else
145 x = rectParent.left;
146 y -= yspacing;
149 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
151 return yspacing;
155 /***********************************************************************
156 * SwitchToThisWindow (USER32.@)
158 void WINAPI SwitchToThisWindow( HWND hwnd, BOOL restore )
160 ShowWindow( hwnd, restore ? SW_RESTORE : SW_SHOWMINIMIZED );
164 /***********************************************************************
165 * GetWindowRect (USER32.@)
167 BOOL WINAPI GetWindowRect( HWND hwnd, LPRECT rect )
169 BOOL ret = WIN_GetRectangles( hwnd, rect, NULL );
170 if (ret)
172 MapWindowPoints( GetAncestor( hwnd, GA_PARENT ), 0, (POINT *)rect, 2 );
173 TRACE( "hwnd %p (%d,%d)-(%d,%d)\n",
174 hwnd, rect->left, rect->top, rect->right, rect->bottom);
176 return ret;
180 /***********************************************************************
181 * GetWindowRgn (USER32.@)
183 int WINAPI GetWindowRgn ( HWND hwnd, HRGN hrgn )
185 int nRet = ERROR;
186 NTSTATUS status;
187 HRGN win_rgn = 0;
188 RGNDATA *data;
189 size_t size = 256;
193 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
195 SetLastError( ERROR_OUTOFMEMORY );
196 return ERROR;
198 SERVER_START_REQ( get_window_region )
200 req->window = hwnd;
201 wine_server_set_reply( req, data->Buffer, size );
202 if (!(status = wine_server_call( req )))
204 size_t reply_size = wine_server_reply_size( reply );
205 if (reply_size)
207 data->rdh.dwSize = sizeof(data->rdh);
208 data->rdh.iType = RDH_RECTANGLES;
209 data->rdh.nCount = reply_size / sizeof(RECT);
210 data->rdh.nRgnSize = reply_size;
211 win_rgn = ExtCreateRegion( NULL, size, data );
214 else size = reply->total_size;
216 SERVER_END_REQ;
217 HeapFree( GetProcessHeap(), 0, data );
218 } while (status == STATUS_BUFFER_OVERFLOW);
220 if (status) SetLastError( RtlNtStatusToDosError(status) );
221 else if (win_rgn)
223 nRet = CombineRgn( hrgn, win_rgn, 0, RGN_COPY );
224 DeleteObject( win_rgn );
226 return nRet;
230 /***********************************************************************
231 * SetWindowRgn (USER32.@)
233 int WINAPI SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL bRedraw )
235 static const RECT empty_rect;
236 BOOL ret;
238 if (hrgn)
240 RGNDATA *data;
241 DWORD size;
243 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
244 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
245 if (!GetRegionData( hrgn, size, data ))
247 HeapFree( GetProcessHeap(), 0, data );
248 return FALSE;
250 SERVER_START_REQ( set_window_region )
252 req->window = hwnd;
253 if (data->rdh.nCount)
254 wine_server_add_data( req, data->Buffer, data->rdh.nCount * sizeof(RECT) );
255 else
256 wine_server_add_data( req, &empty_rect, sizeof(empty_rect) );
257 ret = !wine_server_call_err( req );
259 SERVER_END_REQ;
261 else /* clear existing region */
263 SERVER_START_REQ( set_window_region )
265 req->window = hwnd;
266 ret = !wine_server_call_err( req );
268 SERVER_END_REQ;
271 if (ret) ret = USER_Driver->pSetWindowRgn( hwnd, hrgn, bRedraw );
273 if (ret)
275 UINT swp_flags = SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_FRAMECHANGED;
276 if (hrgn) swp_flags |= SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE;
277 if (!bRedraw) swp_flags |= SWP_NOREDRAW;
278 SetWindowPos( hwnd, 0, 0, 0, 0, 0, swp_flags );
280 return ret;
284 /***********************************************************************
285 * GetClientRect (USER32.@)
287 BOOL WINAPI GetClientRect( HWND hwnd, LPRECT rect )
289 BOOL ret;
291 if ((ret = WIN_GetRectangles( hwnd, NULL, rect )))
293 rect->right -= rect->left;
294 rect->bottom -= rect->top;
295 rect->left = rect->top = 0;
297 return ret;
301 /*******************************************************************
302 * ClientToScreen (USER32.@)
304 BOOL WINAPI ClientToScreen( HWND hwnd, LPPOINT lppnt )
306 MapWindowPoints( hwnd, 0, lppnt, 1 );
307 return TRUE;
311 /*******************************************************************
312 * ScreenToClient (USER32.@)
314 BOOL WINAPI ScreenToClient( HWND hwnd, LPPOINT lppnt )
316 MapWindowPoints( 0, hwnd, lppnt, 1 );
317 return TRUE;
321 /***********************************************************************
322 * list_children_from_point
324 * Get the list of children that can contain point from the server.
325 * Point is in screen coordinates.
326 * Returned list must be freed by caller.
328 static HWND *list_children_from_point( HWND hwnd, POINT pt )
330 HWND *list;
331 int size = 32;
333 for (;;)
335 int count = 0;
337 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
339 SERVER_START_REQ( get_window_children_from_point )
341 req->parent = hwnd;
342 req->x = pt.x;
343 req->y = pt.y;
344 wine_server_set_reply( req, list, (size-1) * sizeof(HWND) );
345 if (!wine_server_call( req )) count = reply->count;
347 SERVER_END_REQ;
348 if (count && count < size)
350 list[count] = 0;
351 return list;
353 HeapFree( GetProcessHeap(), 0, list );
354 if (!count) break;
355 size = count + 1; /* restart with a large enough buffer */
357 return NULL;
361 /***********************************************************************
362 * WINPOS_WindowFromPoint
364 * Find the window and hittest for a given point.
366 HWND WINPOS_WindowFromPoint( HWND hwndScope, POINT pt, INT *hittest )
368 int i, res;
369 HWND ret, *list;
371 if (!hwndScope) hwndScope = GetDesktopWindow();
373 *hittest = HTNOWHERE;
375 if (!(list = list_children_from_point( hwndScope, pt ))) return 0;
377 /* now determine the hittest */
379 for (i = 0; list[i]; i++)
381 LONG style = GetWindowLongW( list[i], GWL_STYLE );
383 /* If window is minimized or disabled, return at once */
384 if (style & WS_MINIMIZE)
386 *hittest = HTCAPTION;
387 break;
389 if (style & WS_DISABLED)
391 *hittest = HTERROR;
392 break;
394 /* Send WM_NCCHITTEST (if same thread) */
395 if (!WIN_IsCurrentThread( list[i] ))
397 *hittest = HTCLIENT;
398 break;
400 res = SendMessageW( list[i], WM_NCHITTEST, 0, MAKELONG(pt.x,pt.y) );
401 if (res != HTTRANSPARENT)
403 *hittest = res; /* Found the window */
404 break;
406 /* continue search with next window in z-order */
408 ret = list[i];
409 HeapFree( GetProcessHeap(), 0, list );
410 TRACE( "scope %p (%d,%d) returning %p\n", hwndScope, pt.x, pt.y, ret );
411 return ret;
415 /*******************************************************************
416 * WindowFromPoint (USER32.@)
418 HWND WINAPI WindowFromPoint( POINT pt )
420 INT hittest;
421 return WINPOS_WindowFromPoint( 0, pt, &hittest );
425 /*******************************************************************
426 * ChildWindowFromPoint (USER32.@)
428 HWND WINAPI ChildWindowFromPoint( HWND hwndParent, POINT pt )
430 return ChildWindowFromPointEx( hwndParent, pt, CWP_ALL );
433 /*******************************************************************
434 * ChildWindowFromPointEx (USER32.@)
436 HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
438 /* pt is in the client coordinates */
439 HWND *list;
440 int i;
441 RECT rect;
442 HWND retvalue;
444 GetClientRect( hwndParent, &rect );
445 if (!PtInRect( &rect, pt )) return 0;
446 if (!(list = WIN_ListChildren( hwndParent ))) return hwndParent;
448 for (i = 0; list[i]; i++)
450 if (!WIN_GetRectangles( list[i], &rect, NULL )) continue;
451 if (!PtInRect( &rect, pt )) continue;
452 if (uFlags & (CWP_SKIPINVISIBLE|CWP_SKIPDISABLED))
454 LONG style = GetWindowLongW( list[i], GWL_STYLE );
455 if ((uFlags & CWP_SKIPINVISIBLE) && !(style & WS_VISIBLE)) continue;
456 if ((uFlags & CWP_SKIPDISABLED) && (style & WS_DISABLED)) continue;
458 if (uFlags & CWP_SKIPTRANSPARENT)
460 if (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TRANSPARENT) continue;
462 break;
464 retvalue = list[i];
465 HeapFree( GetProcessHeap(), 0, list );
466 if (!retvalue) retvalue = hwndParent;
467 return retvalue;
471 /*******************************************************************
472 * WINPOS_GetWinOffset
474 * Calculate the offset between the origin of the two windows. Used
475 * to implement MapWindowPoints.
477 static void WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo, POINT *offset )
479 WND * wndPtr;
481 offset->x = offset->y = 0;
483 /* Translate source window origin to screen coords */
484 if (hwndFrom)
486 HWND hwnd = hwndFrom;
488 while (hwnd)
490 if (hwnd == hwndTo) return;
491 if (!(wndPtr = WIN_GetPtr( hwnd )))
493 ERR( "bad hwndFrom = %p\n", hwnd );
494 return;
496 if (wndPtr == WND_DESKTOP) break;
497 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
498 offset->x += wndPtr->rectClient.left;
499 offset->y += wndPtr->rectClient.top;
500 hwnd = wndPtr->parent;
501 WIN_ReleasePtr( wndPtr );
505 /* Translate origin to destination window coords */
506 if (hwndTo)
508 HWND hwnd = hwndTo;
510 while (hwnd)
512 if (!(wndPtr = WIN_GetPtr( hwnd )))
514 ERR( "bad hwndTo = %p\n", hwnd );
515 return;
517 if (wndPtr == WND_DESKTOP) break;
518 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
519 offset->x -= wndPtr->rectClient.left;
520 offset->y -= wndPtr->rectClient.top;
521 hwnd = wndPtr->parent;
522 WIN_ReleasePtr( wndPtr );
525 return;
527 other_process: /* one of the parents may belong to another process, do it the hard way */
528 offset->x = offset->y = 0;
529 SERVER_START_REQ( get_windows_offset )
531 req->from = hwndFrom;
532 req->to = hwndTo;
533 if (!wine_server_call( req ))
535 offset->x = reply->x;
536 offset->y = reply->y;
539 SERVER_END_REQ;
543 /*******************************************************************
544 * MapWindowPoints (USER.258)
546 void WINAPI MapWindowPoints16( HWND16 hwndFrom, HWND16 hwndTo,
547 LPPOINT16 lppt, UINT16 count )
549 POINT offset;
551 WINPOS_GetWinOffset( WIN_Handle32(hwndFrom), WIN_Handle32(hwndTo), &offset );
552 while (count--)
554 lppt->x += offset.x;
555 lppt->y += offset.y;
556 lppt++;
561 /*******************************************************************
562 * MapWindowPoints (USER32.@)
564 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
566 POINT offset;
568 WINPOS_GetWinOffset( hwndFrom, hwndTo, &offset );
569 while (count--)
571 lppt->x += offset.x;
572 lppt->y += offset.y;
573 lppt++;
575 return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
579 /***********************************************************************
580 * IsIconic (USER32.@)
582 BOOL WINAPI IsIconic(HWND hWnd)
584 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
588 /***********************************************************************
589 * IsZoomed (USER32.@)
591 BOOL WINAPI IsZoomed(HWND hWnd)
593 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
597 /*******************************************************************
598 * AllowSetForegroundWindow (USER32.@)
600 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
602 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
603 * implemented, then fix this function. */
604 return TRUE;
608 /*******************************************************************
609 * LockSetForegroundWindow (USER32.@)
611 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
613 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
614 * implemented, then fix this function. */
615 return TRUE;
619 /***********************************************************************
620 * BringWindowToTop (USER32.@)
622 BOOL WINAPI BringWindowToTop( HWND hwnd )
624 return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
628 /***********************************************************************
629 * MoveWindow (USER32.@)
631 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
632 BOOL repaint )
634 int flags = SWP_NOZORDER | SWP_NOACTIVATE;
635 if (!repaint) flags |= SWP_NOREDRAW;
636 TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
637 return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
640 /***********************************************************************
641 * WINPOS_InitInternalPos
643 static LPINTERNALPOS WINPOS_InitInternalPos( WND* wnd )
645 LPINTERNALPOS lpPos = get_internal_pos( wnd->hwndSelf );
646 if( !lpPos )
648 /* this happens when the window is minimized/maximized
649 * for the first time (rectWindow is not adjusted yet) */
651 lpPos = HeapAlloc( GetProcessHeap(), 0, sizeof(INTERNALPOS) );
652 if( !lpPos ) return NULL;
654 set_internal_pos( wnd->hwndSelf, lpPos );
655 lpPos->hwndIconTitle = 0; /* defer until needs to be shown */
656 lpPos->rectNormal.left = wnd->rectWindow.left;
657 lpPos->rectNormal.top = wnd->rectWindow.top;
658 lpPos->rectNormal.right = wnd->rectWindow.right;
659 lpPos->rectNormal.bottom = wnd->rectWindow.bottom;
660 lpPos->ptIconPos.x = lpPos->ptIconPos.y = -1;
661 lpPos->ptMaxPos.x = lpPos->ptMaxPos.y = -1;
664 if( wnd->dwStyle & WS_MINIMIZE )
666 lpPos->ptIconPos.x = wnd->rectWindow.left;
667 lpPos->ptIconPos.y = wnd->rectWindow.top;
669 else if( wnd->dwStyle & WS_MAXIMIZE )
671 lpPos->ptMaxPos.x = wnd->rectWindow.left;
672 lpPos->ptMaxPos.y = wnd->rectWindow.top;
674 else
676 lpPos->rectNormal.left = wnd->rectWindow.left;
677 lpPos->rectNormal.top = wnd->rectWindow.top;
678 lpPos->rectNormal.right = wnd->rectWindow.right;
679 lpPos->rectNormal.bottom = wnd->rectWindow.bottom;
681 return lpPos;
684 /***********************************************************************
685 * WINPOS_RedrawIconTitle
687 BOOL WINPOS_RedrawIconTitle( HWND hWnd )
689 LPINTERNALPOS lpPos = get_internal_pos( hWnd );
690 if( lpPos )
692 if( lpPos->hwndIconTitle )
694 SendMessageW( lpPos->hwndIconTitle, WM_SHOWWINDOW, TRUE, 0);
695 InvalidateRect( lpPos->hwndIconTitle, NULL, TRUE );
696 return TRUE;
699 return FALSE;
702 /***********************************************************************
703 * WINPOS_ShowIconTitle
705 BOOL WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
707 LPINTERNALPOS lpPos = get_internal_pos( hwnd );
709 if (lpPos && !GetPropA( hwnd, "__wine_x11_managed" ))
711 HWND title = lpPos->hwndIconTitle;
713 TRACE("%p %i\n", hwnd, (bShow != 0) );
715 if( !title )
716 lpPos->hwndIconTitle = title = ICONTITLE_Create( hwnd );
717 if( bShow )
719 if (!IsWindowVisible(title))
721 SendMessageW( title, WM_SHOWWINDOW, TRUE, 0 );
722 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
723 SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
726 else ShowWindow( title, SW_HIDE );
728 return FALSE;
731 /*******************************************************************
732 * WINPOS_GetMinMaxInfo
734 * Get the minimized and maximized information for a window.
736 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
737 POINT *minTrack, POINT *maxTrack )
739 LPINTERNALPOS lpPos;
740 MINMAXINFO MinMax;
741 HMONITOR monitor;
742 INT xinc, yinc;
743 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
744 LONG exstyle = GetWindowLongA( hwnd, GWL_EXSTYLE );
745 RECT rc;
747 /* Compute default values */
749 GetWindowRect(hwnd, &rc);
750 MinMax.ptReserved.x = rc.left;
751 MinMax.ptReserved.y = rc.top;
753 if (style & WS_CHILD)
755 if ((style & WS_CAPTION) == WS_CAPTION)
756 style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
758 GetClientRect(GetAncestor(hwnd,GA_PARENT), &rc);
759 AdjustWindowRectEx(&rc, style, ((style & WS_POPUP) && GetMenu(hwnd)), exstyle);
761 /* avoid calculating this twice */
762 style &= ~(WS_DLGFRAME | WS_BORDER | WS_THICKFRAME);
764 MinMax.ptMaxSize.x = rc.right - rc.left;
765 MinMax.ptMaxSize.y = rc.bottom - rc.top;
767 else
769 MinMax.ptMaxSize.x = GetSystemMetrics(SM_CXSCREEN);
770 MinMax.ptMaxSize.y = GetSystemMetrics(SM_CYSCREEN);
772 MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
773 MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
774 MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXMAXTRACK);
775 MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYMAXTRACK);
777 if (HAS_DLGFRAME( style, exstyle ))
779 xinc = GetSystemMetrics(SM_CXDLGFRAME);
780 yinc = GetSystemMetrics(SM_CYDLGFRAME);
782 else
784 xinc = yinc = 0;
785 if (HAS_THICKFRAME(style))
787 xinc += GetSystemMetrics(SM_CXFRAME);
788 yinc += GetSystemMetrics(SM_CYFRAME);
790 if (style & WS_BORDER)
792 xinc += GetSystemMetrics(SM_CXBORDER);
793 yinc += GetSystemMetrics(SM_CYBORDER);
796 MinMax.ptMaxSize.x += 2 * xinc;
797 MinMax.ptMaxSize.y += 2 * yinc;
799 lpPos = get_internal_pos( hwnd );
800 if( lpPos && !EMPTYPOINT(lpPos->ptMaxPos) )
802 MinMax.ptMaxPosition.x = lpPos->ptMaxPos.x;
803 MinMax.ptMaxPosition.y = lpPos->ptMaxPos.y;
805 else
807 MinMax.ptMaxPosition.x = -xinc;
808 MinMax.ptMaxPosition.y = -yinc;
811 SendMessageW( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
813 /* if the app didn't change the values, adapt them for the current monitor */
815 if ((monitor = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY )))
817 MONITORINFO mon_info;
819 mon_info.cbSize = sizeof(mon_info);
820 GetMonitorInfoW( monitor, &mon_info );
822 if (MinMax.ptMaxSize.x == GetSystemMetrics(SM_CXSCREEN) + 2 * xinc &&
823 MinMax.ptMaxSize.y == GetSystemMetrics(SM_CYSCREEN) + 2 * yinc)
825 MinMax.ptMaxSize.x = (mon_info.rcWork.right - mon_info.rcWork.left) + 2 * xinc;
826 MinMax.ptMaxSize.y = (mon_info.rcWork.bottom - mon_info.rcWork.top) + 2 * yinc;
828 if (MinMax.ptMaxPosition.x == -xinc && MinMax.ptMaxPosition.y == -yinc)
830 MinMax.ptMaxPosition.x = mon_info.rcWork.left - xinc;
831 MinMax.ptMaxPosition.y = mon_info.rcWork.top - yinc;
835 /* Some sanity checks */
837 TRACE("%d %d / %d %d / %d %d / %d %d\n",
838 MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
839 MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
840 MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
841 MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
842 MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
843 MinMax.ptMinTrackSize.x );
844 MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
845 MinMax.ptMinTrackSize.y );
847 if (maxSize) *maxSize = MinMax.ptMaxSize;
848 if (maxPos) *maxPos = MinMax.ptMaxPosition;
849 if (minTrack) *minTrack = MinMax.ptMinTrackSize;
850 if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
853 /***********************************************************************
854 * ShowWindowAsync (USER32.@)
856 * doesn't wait; returns immediately.
857 * used by threads to toggle windows in other (possibly hanging) threads
859 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
861 HWND full_handle;
863 if (is_broadcast(hwnd))
865 SetLastError( ERROR_INVALID_PARAMETER );
866 return FALSE;
869 if ((full_handle = WIN_IsCurrentThread( hwnd )))
870 return USER_Driver->pShowWindow( full_handle, cmd );
872 return SendNotifyMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
876 /***********************************************************************
877 * ShowWindow (USER32.@)
879 BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
881 HWND full_handle;
883 if (is_broadcast(hwnd))
885 SetLastError( ERROR_INVALID_PARAMETER );
886 return FALSE;
888 if ((full_handle = WIN_IsCurrentThread( hwnd )))
889 return USER_Driver->pShowWindow( full_handle, cmd );
891 return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
895 /***********************************************************************
896 * GetInternalWindowPos (USER32.@)
898 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
899 LPPOINT ptIcon )
901 WINDOWPLACEMENT wndpl;
902 if (GetWindowPlacement( hwnd, &wndpl ))
904 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
905 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
906 return wndpl.showCmd;
908 return 0;
912 /***********************************************************************
913 * GetWindowPlacement (USER32.@)
915 * Win95:
916 * Fails if wndpl->length of Win95 (!) apps is invalid.
918 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
920 WND *pWnd = WIN_GetPtr( hwnd );
921 LPINTERNALPOS lpPos;
923 if (!pWnd || pWnd == WND_DESKTOP) return FALSE;
924 if (pWnd == WND_OTHER_PROCESS)
926 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
927 return FALSE;
930 lpPos = WINPOS_InitInternalPos( pWnd );
931 wndpl->length = sizeof(*wndpl);
932 if( pWnd->dwStyle & WS_MINIMIZE )
933 wndpl->showCmd = SW_SHOWMINIMIZED;
934 else
935 wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
936 if( pWnd->flags & WIN_RESTORE_MAX )
937 wndpl->flags = WPF_RESTORETOMAXIMIZED;
938 else
939 wndpl->flags = 0;
940 wndpl->ptMinPosition.x = lpPos->ptIconPos.x;
941 wndpl->ptMinPosition.y = lpPos->ptIconPos.y;
942 wndpl->ptMaxPosition.x = lpPos->ptMaxPos.x;
943 wndpl->ptMaxPosition.y = lpPos->ptMaxPos.y;
944 wndpl->rcNormalPosition.left = lpPos->rectNormal.left;
945 wndpl->rcNormalPosition.top = lpPos->rectNormal.top;
946 wndpl->rcNormalPosition.right = lpPos->rectNormal.right;
947 wndpl->rcNormalPosition.bottom = lpPos->rectNormal.bottom;
948 WIN_ReleasePtr( pWnd );
949 return TRUE;
953 /***********************************************************************
954 * WINPOS_SetPlacement
956 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
958 LPINTERNALPOS lpPos;
959 DWORD style;
960 WND *pWnd = WIN_GetPtr( hwnd );
962 if (!pWnd || pWnd == WND_OTHER_PROCESS || pWnd == WND_DESKTOP) return FALSE;
963 lpPos = WINPOS_InitInternalPos( pWnd );
965 if( flags & PLACE_MIN )
967 lpPos->ptIconPos.x = wndpl->ptMinPosition.x;
968 lpPos->ptIconPos.y = wndpl->ptMinPosition.y;
970 if( flags & PLACE_MAX )
972 lpPos->ptMaxPos.x = wndpl->ptMaxPosition.x;
973 lpPos->ptMaxPos.y = wndpl->ptMaxPosition.y;
975 if( flags & PLACE_RECT)
977 lpPos->rectNormal.left = wndpl->rcNormalPosition.left;
978 lpPos->rectNormal.top = wndpl->rcNormalPosition.top;
979 lpPos->rectNormal.right = wndpl->rcNormalPosition.right;
980 lpPos->rectNormal.bottom = wndpl->rcNormalPosition.bottom;
983 style = pWnd->dwStyle;
984 WIN_ReleasePtr( pWnd );
986 if( style & WS_MINIMIZE )
988 WINPOS_ShowIconTitle( hwnd, FALSE );
989 if( wndpl->flags & WPF_SETMINPOSITION && !EMPTYPOINT(lpPos->ptIconPos))
990 SetWindowPos( hwnd, 0, lpPos->ptIconPos.x, lpPos->ptIconPos.y,
991 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
993 else if( style & WS_MAXIMIZE )
995 if( !EMPTYPOINT(lpPos->ptMaxPos) )
996 SetWindowPos( hwnd, 0, lpPos->ptMaxPos.x, lpPos->ptMaxPos.y,
997 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
999 else if( flags & PLACE_RECT )
1000 SetWindowPos( hwnd, 0, lpPos->rectNormal.left, lpPos->rectNormal.top,
1001 lpPos->rectNormal.right - lpPos->rectNormal.left,
1002 lpPos->rectNormal.bottom - lpPos->rectNormal.top,
1003 SWP_NOZORDER | SWP_NOACTIVATE );
1005 ShowWindow( hwnd, wndpl->showCmd );
1007 if (IsIconic( hwnd ))
1009 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE) WINPOS_ShowIconTitle( hwnd, TRUE );
1011 /* SDK: ...valid only the next time... */
1012 if( wndpl->flags & WPF_RESTORETOMAXIMIZED )
1014 pWnd = WIN_GetPtr( hwnd );
1015 if (pWnd && pWnd != WND_OTHER_PROCESS)
1017 pWnd->flags |= WIN_RESTORE_MAX;
1018 WIN_ReleasePtr( pWnd );
1022 return TRUE;
1026 /***********************************************************************
1027 * SetWindowPlacement (USER32.@)
1029 * Win95:
1030 * Fails if wndpl->length of Win95 (!) apps is invalid.
1032 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
1034 if (!wpl) return FALSE;
1035 return WINPOS_SetPlacement( hwnd, wpl, PLACE_MIN | PLACE_MAX | PLACE_RECT );
1039 /***********************************************************************
1040 * AnimateWindow (USER32.@)
1041 * Shows/Hides a window with an animation
1042 * NO ANIMATION YET
1044 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
1046 FIXME("partial stub\n");
1048 /* If trying to show/hide and it's already *
1049 * shown/hidden or invalid window, fail with *
1050 * invalid parameter */
1051 if(!IsWindow(hwnd) ||
1052 (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
1053 (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
1055 SetLastError(ERROR_INVALID_PARAMETER);
1056 return FALSE;
1059 ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1061 return TRUE;
1064 /***********************************************************************
1065 * SetInternalWindowPos (USER32.@)
1067 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1068 LPRECT rect, LPPOINT pt )
1070 if( IsWindow(hwnd) )
1072 WINDOWPLACEMENT wndpl;
1073 UINT flags;
1075 wndpl.length = sizeof(wndpl);
1076 wndpl.showCmd = showCmd;
1077 wndpl.flags = flags = 0;
1079 if( pt )
1081 flags |= PLACE_MIN;
1082 wndpl.flags |= WPF_SETMINPOSITION;
1083 wndpl.ptMinPosition = *pt;
1085 if( rect )
1087 flags |= PLACE_RECT;
1088 wndpl.rcNormalPosition = *rect;
1090 WINPOS_SetPlacement( hwnd, &wndpl, flags );
1095 /*******************************************************************
1096 * can_activate_window
1098 * Check if we can activate the specified window.
1100 static BOOL can_activate_window( HWND hwnd )
1102 LONG style;
1104 if (!hwnd) return FALSE;
1105 style = GetWindowLongW( hwnd, GWL_STYLE );
1106 if (!(style & WS_VISIBLE)) return FALSE;
1107 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1108 return !(style & WS_DISABLED);
1112 /*******************************************************************
1113 * WINPOS_ActivateOtherWindow
1115 * Activates window other than pWnd.
1117 void WINPOS_ActivateOtherWindow(HWND hwnd)
1119 HWND hwndTo, fg;
1121 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1123 hwndTo = GetAncestor( hwndTo, GA_ROOT );
1124 if (can_activate_window( hwndTo )) goto done;
1127 hwndTo = hwnd;
1128 for (;;)
1130 if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1131 if (can_activate_window( hwndTo )) break;
1134 done:
1135 fg = GetForegroundWindow();
1136 TRACE("win = %p fg = %p\n", hwndTo, fg);
1137 if (!fg || (hwnd == fg))
1139 if (SetForegroundWindow( hwndTo )) return;
1141 if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1145 /***********************************************************************
1146 * WINPOS_HandleWindowPosChanging
1148 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1150 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1152 POINT minTrack, maxTrack;
1153 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1155 if (winpos->flags & SWP_NOSIZE) return 0;
1156 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1158 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1159 if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1160 if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1161 if (!(style & WS_MINIMIZE))
1163 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1164 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1167 return 0;
1171 /***********************************************************************
1172 * dump_winpos_flags
1174 static void dump_winpos_flags(UINT flags)
1176 TRACE("flags:");
1177 if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1178 if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1179 if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1180 if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1181 if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1182 if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1183 if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1184 if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1185 if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1186 if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1187 if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1188 if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1189 if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1191 #define DUMPED_FLAGS \
1192 (SWP_NOSIZE | \
1193 SWP_NOMOVE | \
1194 SWP_NOZORDER | \
1195 SWP_NOREDRAW | \
1196 SWP_NOACTIVATE | \
1197 SWP_FRAMECHANGED | \
1198 SWP_SHOWWINDOW | \
1199 SWP_HIDEWINDOW | \
1200 SWP_NOCOPYBITS | \
1201 SWP_NOOWNERZORDER | \
1202 SWP_NOSENDCHANGING | \
1203 SWP_DEFERERASE | \
1204 SWP_ASYNCWINDOWPOS)
1206 if(flags & ~DUMPED_FLAGS) TRACE(" %08x", flags & ~DUMPED_FLAGS);
1207 TRACE("\n");
1208 #undef DUMPED_FLAGS
1211 /***********************************************************************
1212 * SWP_DoWinPosChanging
1214 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
1216 WND *wndPtr;
1218 /* Send WM_WINDOWPOSCHANGING message */
1220 if (!(pWinpos->flags & SWP_NOSENDCHANGING))
1221 SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
1223 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) ||
1224 wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
1226 /* Calculate new position and size */
1228 *pNewWindowRect = wndPtr->rectWindow;
1229 *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
1230 : wndPtr->rectClient;
1232 if (!(pWinpos->flags & SWP_NOSIZE))
1234 pNewWindowRect->right = pNewWindowRect->left + pWinpos->cx;
1235 pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
1237 if (!(pWinpos->flags & SWP_NOMOVE))
1239 pNewWindowRect->left = pWinpos->x;
1240 pNewWindowRect->top = pWinpos->y;
1241 pNewWindowRect->right += pWinpos->x - wndPtr->rectWindow.left;
1242 pNewWindowRect->bottom += pWinpos->y - wndPtr->rectWindow.top;
1244 OffsetRect( pNewClientRect, pWinpos->x - wndPtr->rectWindow.left,
1245 pWinpos->y - wndPtr->rectWindow.top );
1247 pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
1249 TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
1250 pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
1251 pWinpos->cx, pWinpos->cy, pWinpos->flags );
1252 TRACE( "current %s style %08x new %s\n",
1253 wine_dbgstr_rect( &wndPtr->rectWindow ), wndPtr->dwStyle,
1254 wine_dbgstr_rect( pNewWindowRect ));
1256 WIN_ReleasePtr( wndPtr );
1257 return TRUE;
1260 /***********************************************************************
1261 * get_valid_rects
1263 * Compute the valid rects from the old and new client rect and WVR_* flags.
1264 * Helper for WM_NCCALCSIZE handling.
1266 static inline void get_valid_rects( const RECT *old_client, const RECT *new_client, UINT flags,
1267 RECT *valid )
1269 int cx, cy;
1271 if (flags & WVR_REDRAW)
1273 SetRectEmpty( &valid[0] );
1274 SetRectEmpty( &valid[1] );
1275 return;
1278 if (flags & WVR_VALIDRECTS)
1280 if (!IntersectRect( &valid[0], &valid[0], new_client ) ||
1281 !IntersectRect( &valid[1], &valid[1], old_client ))
1283 SetRectEmpty( &valid[0] );
1284 SetRectEmpty( &valid[1] );
1285 return;
1287 flags = WVR_ALIGNLEFT | WVR_ALIGNTOP;
1289 else
1291 valid[0] = *new_client;
1292 valid[1] = *old_client;
1295 /* make sure the rectangles have the same size */
1296 cx = min( valid[0].right - valid[0].left, valid[1].right - valid[1].left );
1297 cy = min( valid[0].bottom - valid[0].top, valid[1].bottom - valid[1].top );
1299 if (flags & WVR_ALIGNBOTTOM)
1301 valid[0].top = valid[0].bottom - cy;
1302 valid[1].top = valid[1].bottom - cy;
1304 else
1306 valid[0].bottom = valid[0].top + cy;
1307 valid[1].bottom = valid[1].top + cy;
1309 if (flags & WVR_ALIGNRIGHT)
1311 valid[0].left = valid[0].right - cx;
1312 valid[1].left = valid[1].right - cx;
1314 else
1316 valid[0].right = valid[0].left + cx;
1317 valid[1].right = valid[1].left + cx;
1321 struct move_owned_info
1323 HWND owner;
1324 HWND insert_after;
1327 static BOOL CALLBACK move_owned_popups( HWND hwnd, LPARAM lparam )
1329 struct move_owned_info *info = (struct move_owned_info *)lparam;
1331 if (hwnd == info->owner) return FALSE;
1332 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) &&
1333 GetWindow( hwnd, GW_OWNER ) == info->owner)
1335 SetWindowPos( hwnd, info->insert_after, 0, 0, 0, 0,
1336 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE |
1337 SWP_NOSENDCHANGING | SWP_DEFERERASE );
1338 info->insert_after = hwnd;
1340 return TRUE;
1343 /***********************************************************************
1344 * SWP_DoOwnedPopups
1346 * fix Z order taking into account owned popups -
1347 * basically we need to maintain them above the window that owns them
1349 * FIXME: hide/show owned popups when owner visibility changes.
1351 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
1353 HWND owner = GetWindow( hwnd, GW_OWNER );
1354 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1355 struct move_owned_info info;
1357 TRACE("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
1359 if ((style & WS_POPUP) && owner)
1361 /* make sure this popup stays above the owner */
1363 if( hwndInsertAfter != HWND_TOP )
1365 HWND hwndLocalPrev = HWND_TOP;
1366 HWND prev = GetWindow( owner, GW_HWNDPREV );
1368 while (prev && prev != hwndInsertAfter)
1370 if (hwndLocalPrev == HWND_TOP && GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE)
1371 hwndLocalPrev = prev;
1372 prev = GetWindow( prev, GW_HWNDPREV );
1374 if (!prev) hwndInsertAfter = hwndLocalPrev;
1377 else if (style & WS_CHILD) return hwndInsertAfter;
1379 info.owner = hwnd;
1380 info.insert_after = hwndInsertAfter;
1381 EnumWindows( move_owned_popups, (LPARAM)&info );
1382 return info.insert_after;
1385 /***********************************************************************
1386 * SWP_DoNCCalcSize
1388 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect,
1389 RECT *validRects )
1391 UINT wvrFlags = 0;
1392 WND *wndPtr;
1394 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
1396 /* Send WM_NCCALCSIZE message to get new client area */
1397 if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
1399 NCCALCSIZE_PARAMS params;
1400 WINDOWPOS winposCopy;
1402 params.rgrc[0] = *pNewWindowRect;
1403 params.rgrc[1] = wndPtr->rectWindow;
1404 params.rgrc[2] = wndPtr->rectClient;
1405 params.lppos = &winposCopy;
1406 winposCopy = *pWinpos;
1407 WIN_ReleasePtr( wndPtr );
1409 wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
1411 *pNewClientRect = params.rgrc[0];
1413 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
1415 TRACE( "hwnd %p old win %s old client %s new win %s new client %s\n", pWinpos->hwnd,
1416 wine_dbgstr_rect(&wndPtr->rectWindow), wine_dbgstr_rect(&wndPtr->rectClient),
1417 wine_dbgstr_rect(pNewWindowRect), wine_dbgstr_rect(pNewClientRect) );
1419 if( pNewClientRect->left != wndPtr->rectClient.left ||
1420 pNewClientRect->top != wndPtr->rectClient.top )
1421 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1423 if( (pNewClientRect->right - pNewClientRect->left !=
1424 wndPtr->rectClient.right - wndPtr->rectClient.left))
1425 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1426 else
1427 wvrFlags &= ~WVR_HREDRAW;
1429 if (pNewClientRect->bottom - pNewClientRect->top !=
1430 wndPtr->rectClient.bottom - wndPtr->rectClient.top)
1431 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1432 else
1433 wvrFlags &= ~WVR_VREDRAW;
1435 validRects[0] = params.rgrc[1];
1436 validRects[1] = params.rgrc[2];
1438 else
1440 if (!(pWinpos->flags & SWP_NOMOVE) &&
1441 (pNewClientRect->left != wndPtr->rectClient.left ||
1442 pNewClientRect->top != wndPtr->rectClient.top))
1443 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1446 if (pWinpos->flags & (SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
1448 SetRectEmpty( &validRects[0] );
1449 SetRectEmpty( &validRects[1] );
1451 else get_valid_rects( &wndPtr->rectClient, pNewClientRect, wvrFlags, validRects );
1453 WIN_ReleasePtr( wndPtr );
1454 return wvrFlags;
1457 /* fix redundant flags and values in the WINDOWPOS structure */
1458 static BOOL fixup_flags( WINDOWPOS *winpos )
1460 HWND parent;
1461 WND *wndPtr = WIN_GetPtr( winpos->hwnd );
1462 BOOL ret = TRUE;
1464 if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
1466 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1467 return FALSE;
1469 winpos->hwnd = wndPtr->hwndSelf; /* make it a full handle */
1471 /* Finally make sure that all coordinates are valid */
1472 if (winpos->x < -32768) winpos->x = -32768;
1473 else if (winpos->x > 32767) winpos->x = 32767;
1474 if (winpos->y < -32768) winpos->y = -32768;
1475 else if (winpos->y > 32767) winpos->y = 32767;
1477 if (winpos->cx < 0) winpos->cx = 0;
1478 else if (winpos->cx > 32767) winpos->cx = 32767;
1479 if (winpos->cy < 0) winpos->cy = 0;
1480 else if (winpos->cy > 32767) winpos->cy = 32767;
1482 parent = GetAncestor( winpos->hwnd, GA_PARENT );
1483 if (!IsWindowVisible( parent )) winpos->flags |= SWP_NOREDRAW;
1485 if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
1486 else
1488 winpos->flags &= ~SWP_HIDEWINDOW;
1489 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
1492 if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
1493 (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
1494 winpos->flags |= SWP_NOSIZE; /* Already the right size */
1496 if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
1497 winpos->flags |= SWP_NOMOVE; /* Already the right position */
1499 if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
1501 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) /* Bring to the top when activating */
1503 winpos->flags &= ~SWP_NOZORDER;
1504 winpos->hwndInsertAfter = HWND_TOP;
1508 /* Check hwndInsertAfter */
1509 if (winpos->flags & SWP_NOZORDER) goto done;
1511 /* fix sign extension */
1512 if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
1513 else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
1515 /* FIXME: TOPMOST not supported yet */
1516 if ((winpos->hwndInsertAfter == HWND_TOPMOST) ||
1517 (winpos->hwndInsertAfter == HWND_NOTOPMOST)) winpos->hwndInsertAfter = HWND_TOP;
1519 /* hwndInsertAfter must be a sibling of the window */
1520 if (winpos->hwndInsertAfter == HWND_TOP)
1522 if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1523 winpos->flags |= SWP_NOZORDER;
1525 else if (winpos->hwndInsertAfter == HWND_BOTTOM)
1527 if (GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
1528 winpos->flags |= SWP_NOZORDER;
1530 else
1532 if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != parent) ret = FALSE;
1533 else
1535 /* don't need to change the Zorder of hwnd if it's already inserted
1536 * after hwndInsertAfter or when inserting hwnd after itself.
1538 if ((winpos->hwnd == winpos->hwndInsertAfter) ||
1539 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
1540 winpos->flags |= SWP_NOZORDER;
1543 done:
1544 WIN_ReleasePtr( wndPtr );
1545 return ret;
1548 /***********************************************************************
1549 * USER_SetWindowPos
1551 * User32 internal function
1553 BOOL USER_SetWindowPos( WINDOWPOS * winpos )
1555 RECT newWindowRect, newClientRect, valid_rects[2];
1556 UINT orig_flags;
1558 orig_flags = winpos->flags;
1560 /* First make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
1561 if (!(winpos->flags & SWP_NOMOVE))
1563 if (winpos->x < -32768) winpos->x = -32768;
1564 else if (winpos->x > 32767) winpos->x = 32767;
1565 if (winpos->y < -32768) winpos->y = -32768;
1566 else if (winpos->y > 32767) winpos->y = 32767;
1568 if (!(winpos->flags & SWP_NOSIZE))
1570 if (winpos->cx < 0) winpos->cx = 0;
1571 else if (winpos->cx > 32767) winpos->cx = 32767;
1572 if (winpos->cy < 0) winpos->cy = 0;
1573 else if (winpos->cy > 32767) winpos->cy = 32767;
1576 if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
1578 /* Fix redundant flags */
1579 if (!fixup_flags( winpos )) return FALSE;
1581 if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
1583 if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
1584 winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
1587 /* Common operations */
1589 SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect, valid_rects );
1591 if(!USER_Driver->pSetWindowPos( winpos->hwnd, winpos->hwndInsertAfter,
1592 &newWindowRect, &newClientRect, orig_flags, valid_rects ))
1593 return FALSE;
1595 if (!(orig_flags & SWP_SHOWWINDOW))
1597 UINT rdw_flags = RDW_FRAME | RDW_ERASE;
1598 if ( !(orig_flags & SWP_DEFERERASE) ) rdw_flags |= RDW_ERASENOW;
1599 RedrawWindow( winpos->hwnd, NULL, NULL, rdw_flags );
1602 if( winpos->flags & SWP_HIDEWINDOW )
1603 HideCaret(winpos->hwnd);
1604 else if (winpos->flags & SWP_SHOWWINDOW)
1605 ShowCaret(winpos->hwnd);
1607 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)))
1609 /* child windows get WM_CHILDACTIVATE message */
1610 if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
1611 SendMessageW( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
1612 else
1613 SetForegroundWindow( winpos->hwnd );
1616 /* And last, send the WM_WINDOWPOSCHANGED message */
1618 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
1620 if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
1622 /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
1623 and always contains final window position.
1625 winpos->x = newWindowRect.left;
1626 winpos->y = newWindowRect.top;
1627 winpos->cx = newWindowRect.right - newWindowRect.left;
1628 winpos->cy = newWindowRect.bottom - newWindowRect.top;
1629 SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
1631 return TRUE;
1634 /***********************************************************************
1635 * SetWindowPos (USER32.@)
1637 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
1638 INT x, INT y, INT cx, INT cy, UINT flags )
1640 WINDOWPOS winpos;
1642 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1643 hwnd, hwndInsertAfter, x, y, cx, cy, flags);
1644 if(TRACE_ON(win)) dump_winpos_flags(flags);
1646 if (is_broadcast(hwnd))
1648 SetLastError( ERROR_INVALID_PARAMETER );
1649 return FALSE;
1652 winpos.hwnd = WIN_GetFullHandle(hwnd);
1653 winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
1654 winpos.x = x;
1655 winpos.y = y;
1656 winpos.cx = cx;
1657 winpos.cy = cy;
1658 winpos.flags = flags;
1660 if (WIN_IsCurrentThread( hwnd ))
1661 return USER_SetWindowPos(&winpos);
1663 return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
1667 /***********************************************************************
1668 * BeginDeferWindowPos (USER32.@)
1670 HDWP WINAPI BeginDeferWindowPos( INT count )
1672 HDWP handle;
1673 DWP *pDWP;
1675 TRACE("%d\n", count);
1677 if (count < 0)
1679 SetLastError(ERROR_INVALID_PARAMETER);
1680 return 0;
1682 /* Windows allows zero count, in which case it allocates context for 8 moves */
1683 if (count == 0) count = 8;
1685 handle = USER_HEAP_ALLOC( sizeof(DWP) + (count-1)*sizeof(WINDOWPOS) );
1686 if (!handle) return 0;
1687 pDWP = (DWP *) USER_HEAP_LIN_ADDR( handle );
1688 pDWP->actualCount = 0;
1689 pDWP->suggestedCount = count;
1690 pDWP->valid = TRUE;
1691 pDWP->wMagic = DWP_MAGIC;
1692 pDWP->hwndParent = 0;
1694 TRACE("returning hdwp %p\n", handle);
1695 return handle;
1699 /***********************************************************************
1700 * DeferWindowPos (USER32.@)
1702 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
1703 INT x, INT y, INT cx, INT cy,
1704 UINT flags )
1706 DWP *pDWP;
1707 int i;
1708 HDWP newhdwp = hdwp,retvalue;
1710 TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1711 hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
1713 hwnd = WIN_GetFullHandle( hwnd );
1714 if (hwnd == GetDesktopWindow()) return 0;
1716 if (!(pDWP = USER_HEAP_LIN_ADDR( hdwp ))) return 0;
1718 USER_Lock();
1720 for (i = 0; i < pDWP->actualCount; i++)
1722 if (pDWP->winPos[i].hwnd == hwnd)
1724 /* Merge with the other changes */
1725 if (!(flags & SWP_NOZORDER))
1727 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
1729 if (!(flags & SWP_NOMOVE))
1731 pDWP->winPos[i].x = x;
1732 pDWP->winPos[i].y = y;
1734 if (!(flags & SWP_NOSIZE))
1736 pDWP->winPos[i].cx = cx;
1737 pDWP->winPos[i].cy = cy;
1739 pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
1740 SWP_NOZORDER | SWP_NOREDRAW |
1741 SWP_NOACTIVATE | SWP_NOCOPYBITS|
1742 SWP_NOOWNERZORDER);
1743 pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
1744 SWP_FRAMECHANGED);
1745 retvalue = hdwp;
1746 goto END;
1749 if (pDWP->actualCount >= pDWP->suggestedCount)
1751 newhdwp = USER_HEAP_REALLOC( hdwp,
1752 sizeof(DWP) + pDWP->suggestedCount*sizeof(WINDOWPOS) );
1753 if (!newhdwp)
1755 retvalue = 0;
1756 goto END;
1758 pDWP = (DWP *) USER_HEAP_LIN_ADDR( newhdwp );
1759 pDWP->suggestedCount++;
1761 pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
1762 pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
1763 pDWP->winPos[pDWP->actualCount].x = x;
1764 pDWP->winPos[pDWP->actualCount].y = y;
1765 pDWP->winPos[pDWP->actualCount].cx = cx;
1766 pDWP->winPos[pDWP->actualCount].cy = cy;
1767 pDWP->winPos[pDWP->actualCount].flags = flags;
1768 pDWP->actualCount++;
1769 retvalue = newhdwp;
1770 END:
1771 USER_Unlock();
1772 return retvalue;
1776 /***********************************************************************
1777 * EndDeferWindowPos (USER32.@)
1779 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
1781 DWP *pDWP;
1782 WINDOWPOS *winpos;
1783 BOOL res = TRUE;
1784 int i;
1786 TRACE("%p\n", hdwp);
1788 pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
1789 if (!pDWP) return FALSE;
1790 for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
1792 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1793 winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
1794 winpos->cx, winpos->cy, winpos->flags);
1796 if (!(res = USER_SetWindowPos( winpos ))) break;
1798 USER_HEAP_FREE( hdwp );
1799 return res;
1803 /***********************************************************************
1804 * TileChildWindows (USER.199)
1806 void WINAPI TileChildWindows16( HWND16 parent, WORD action )
1808 FIXME("(%04x, %d): stub\n", parent, action);
1811 /***********************************************************************
1812 * CascadeChildWindows (USER.198)
1814 void WINAPI CascadeChildWindows16( HWND16 parent, WORD action )
1816 FIXME("(%04x, %d): stub\n", parent, action);