push a1eb882dd4a6a88419e75ca032b34641780365a0
[wine/hacks.git] / dlls / user32 / winpos.c
blob1bfd16bbd8fffe1e0adf106528b034d21cff7c5e
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 req->redraw = (bRedraw != 0);
254 if (data->rdh.nCount)
255 wine_server_add_data( req, data->Buffer, data->rdh.nCount * sizeof(RECT) );
256 else
257 wine_server_add_data( req, &empty_rect, sizeof(empty_rect) );
258 ret = !wine_server_call_err( req );
260 SERVER_END_REQ;
262 else /* clear existing region */
264 SERVER_START_REQ( set_window_region )
266 req->window = hwnd;
267 req->redraw = (bRedraw != 0);
268 ret = !wine_server_call_err( req );
270 SERVER_END_REQ;
273 if (ret) ret = USER_Driver->pSetWindowRgn( hwnd, hrgn, bRedraw );
275 if (ret)
277 UINT swp_flags = SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_FRAMECHANGED;
278 if (hrgn) swp_flags |= SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE;
279 if (!bRedraw) swp_flags |= SWP_NOREDRAW;
280 SetWindowPos( hwnd, 0, 0, 0, 0, 0, swp_flags );
282 return ret;
286 /***********************************************************************
287 * GetClientRect (USER32.@)
289 BOOL WINAPI GetClientRect( HWND hwnd, LPRECT rect )
291 BOOL ret;
293 if ((ret = WIN_GetRectangles( hwnd, NULL, rect )))
295 rect->right -= rect->left;
296 rect->bottom -= rect->top;
297 rect->left = rect->top = 0;
299 return ret;
303 /*******************************************************************
304 * ClientToScreen (USER32.@)
306 BOOL WINAPI ClientToScreen( HWND hwnd, LPPOINT lppnt )
308 MapWindowPoints( hwnd, 0, lppnt, 1 );
309 return TRUE;
313 /*******************************************************************
314 * ScreenToClient (USER32.@)
316 BOOL WINAPI ScreenToClient( HWND hwnd, LPPOINT lppnt )
318 MapWindowPoints( 0, hwnd, lppnt, 1 );
319 return TRUE;
323 /***********************************************************************
324 * list_children_from_point
326 * Get the list of children that can contain point from the server.
327 * Point is in screen coordinates.
328 * Returned list must be freed by caller.
330 static HWND *list_children_from_point( HWND hwnd, POINT pt )
332 HWND *list;
333 int size = 32;
335 for (;;)
337 int count = 0;
339 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
341 SERVER_START_REQ( get_window_children_from_point )
343 req->parent = hwnd;
344 req->x = pt.x;
345 req->y = pt.y;
346 wine_server_set_reply( req, list, (size-1) * sizeof(HWND) );
347 if (!wine_server_call( req )) count = reply->count;
349 SERVER_END_REQ;
350 if (count && count < size)
352 list[count] = 0;
353 return list;
355 HeapFree( GetProcessHeap(), 0, list );
356 if (!count) break;
357 size = count + 1; /* restart with a large enough buffer */
359 return NULL;
363 /***********************************************************************
364 * WINPOS_WindowFromPoint
366 * Find the window and hittest for a given point.
368 HWND WINPOS_WindowFromPoint( HWND hwndScope, POINT pt, INT *hittest )
370 int i, res;
371 HWND ret, *list;
373 if (!hwndScope) hwndScope = GetDesktopWindow();
375 *hittest = HTNOWHERE;
377 if (!(list = list_children_from_point( hwndScope, pt ))) return 0;
379 /* now determine the hittest */
381 for (i = 0; list[i]; i++)
383 LONG style = GetWindowLongW( list[i], GWL_STYLE );
385 /* If window is minimized or disabled, return at once */
386 if (style & WS_MINIMIZE)
388 *hittest = HTCAPTION;
389 break;
391 if (style & WS_DISABLED)
393 *hittest = HTERROR;
394 break;
396 /* Send WM_NCCHITTEST (if same thread) */
397 if (!WIN_IsCurrentThread( list[i] ))
399 *hittest = HTCLIENT;
400 break;
402 res = SendMessageW( list[i], WM_NCHITTEST, 0, MAKELONG(pt.x,pt.y) );
403 if (res != HTTRANSPARENT)
405 *hittest = res; /* Found the window */
406 break;
408 /* continue search with next window in z-order */
410 ret = list[i];
411 HeapFree( GetProcessHeap(), 0, list );
412 TRACE( "scope %p (%d,%d) returning %p\n", hwndScope, pt.x, pt.y, ret );
413 return ret;
417 /*******************************************************************
418 * WindowFromPoint (USER32.@)
420 HWND WINAPI WindowFromPoint( POINT pt )
422 INT hittest;
423 return WINPOS_WindowFromPoint( 0, pt, &hittest );
427 /*******************************************************************
428 * ChildWindowFromPoint (USER32.@)
430 HWND WINAPI ChildWindowFromPoint( HWND hwndParent, POINT pt )
432 return ChildWindowFromPointEx( hwndParent, pt, CWP_ALL );
435 /*******************************************************************
436 * RealChildWindowFromPoint (USER32.@)
438 HWND WINAPI RealChildWindowFromPoint( HWND hwndParent, POINT pt )
440 return ChildWindowFromPointEx( hwndParent, pt, CWP_SKIPTRANSPARENT );
443 /*******************************************************************
444 * ChildWindowFromPointEx (USER32.@)
446 HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
448 /* pt is in the client coordinates */
449 HWND *list;
450 int i;
451 RECT rect;
452 HWND retvalue;
454 GetClientRect( hwndParent, &rect );
455 if (!PtInRect( &rect, pt )) return 0;
456 if (!(list = WIN_ListChildren( hwndParent ))) return hwndParent;
458 for (i = 0; list[i]; i++)
460 if (!WIN_GetRectangles( list[i], &rect, NULL )) continue;
461 if (!PtInRect( &rect, pt )) continue;
462 if (uFlags & (CWP_SKIPINVISIBLE|CWP_SKIPDISABLED))
464 LONG style = GetWindowLongW( list[i], GWL_STYLE );
465 if ((uFlags & CWP_SKIPINVISIBLE) && !(style & WS_VISIBLE)) continue;
466 if ((uFlags & CWP_SKIPDISABLED) && (style & WS_DISABLED)) continue;
468 if (uFlags & CWP_SKIPTRANSPARENT)
470 if (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TRANSPARENT) continue;
472 break;
474 retvalue = list[i];
475 HeapFree( GetProcessHeap(), 0, list );
476 if (!retvalue) retvalue = hwndParent;
477 return retvalue;
481 /*******************************************************************
482 * WINPOS_GetWinOffset
484 * Calculate the offset between the origin of the two windows. Used
485 * to implement MapWindowPoints.
487 static void WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo, POINT *offset )
489 WND * wndPtr;
491 offset->x = offset->y = 0;
493 /* Translate source window origin to screen coords */
494 if (hwndFrom)
496 HWND hwnd = hwndFrom;
498 while (hwnd)
500 if (hwnd == hwndTo) return;
501 if (!(wndPtr = WIN_GetPtr( hwnd )))
503 ERR( "bad hwndFrom = %p\n", hwnd );
504 return;
506 if (wndPtr == WND_DESKTOP) break;
507 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
508 offset->x += wndPtr->rectClient.left;
509 offset->y += wndPtr->rectClient.top;
510 hwnd = wndPtr->parent;
511 WIN_ReleasePtr( wndPtr );
515 /* Translate origin to destination window coords */
516 if (hwndTo)
518 HWND hwnd = hwndTo;
520 while (hwnd)
522 if (!(wndPtr = WIN_GetPtr( hwnd )))
524 ERR( "bad hwndTo = %p\n", hwnd );
525 return;
527 if (wndPtr == WND_DESKTOP) break;
528 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
529 offset->x -= wndPtr->rectClient.left;
530 offset->y -= wndPtr->rectClient.top;
531 hwnd = wndPtr->parent;
532 WIN_ReleasePtr( wndPtr );
535 return;
537 other_process: /* one of the parents may belong to another process, do it the hard way */
538 offset->x = offset->y = 0;
539 SERVER_START_REQ( get_windows_offset )
541 req->from = hwndFrom;
542 req->to = hwndTo;
543 if (!wine_server_call( req ))
545 offset->x = reply->x;
546 offset->y = reply->y;
549 SERVER_END_REQ;
553 /*******************************************************************
554 * MapWindowPoints (USER.258)
556 void WINAPI MapWindowPoints16( HWND16 hwndFrom, HWND16 hwndTo,
557 LPPOINT16 lppt, UINT16 count )
559 POINT offset;
561 WINPOS_GetWinOffset( WIN_Handle32(hwndFrom), WIN_Handle32(hwndTo), &offset );
562 while (count--)
564 lppt->x += offset.x;
565 lppt->y += offset.y;
566 lppt++;
571 /*******************************************************************
572 * MapWindowPoints (USER32.@)
574 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
576 POINT offset;
578 WINPOS_GetWinOffset( hwndFrom, hwndTo, &offset );
579 while (count--)
581 lppt->x += offset.x;
582 lppt->y += offset.y;
583 lppt++;
585 return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
589 /***********************************************************************
590 * IsIconic (USER32.@)
592 BOOL WINAPI IsIconic(HWND hWnd)
594 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
598 /***********************************************************************
599 * IsZoomed (USER32.@)
601 BOOL WINAPI IsZoomed(HWND hWnd)
603 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
607 /*******************************************************************
608 * AllowSetForegroundWindow (USER32.@)
610 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
612 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
613 * implemented, then fix this function. */
614 return TRUE;
618 /*******************************************************************
619 * LockSetForegroundWindow (USER32.@)
621 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
623 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
624 * implemented, then fix this function. */
625 return TRUE;
629 /***********************************************************************
630 * BringWindowToTop (USER32.@)
632 BOOL WINAPI BringWindowToTop( HWND hwnd )
634 return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
638 /***********************************************************************
639 * MoveWindow (USER32.@)
641 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
642 BOOL repaint )
644 int flags = SWP_NOZORDER | SWP_NOACTIVATE;
645 if (!repaint) flags |= SWP_NOREDRAW;
646 TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
647 return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
650 /***********************************************************************
651 * WINPOS_InitInternalPos
653 static LPINTERNALPOS WINPOS_InitInternalPos( WND* wnd )
655 LPINTERNALPOS lpPos = get_internal_pos( wnd->hwndSelf );
656 if( !lpPos )
658 /* this happens when the window is minimized/maximized
659 * for the first time (rectWindow is not adjusted yet) */
661 lpPos = HeapAlloc( GetProcessHeap(), 0, sizeof(INTERNALPOS) );
662 if( !lpPos ) return NULL;
664 set_internal_pos( wnd->hwndSelf, lpPos );
665 lpPos->hwndIconTitle = 0; /* defer until needs to be shown */
666 lpPos->rectNormal.left = wnd->rectWindow.left;
667 lpPos->rectNormal.top = wnd->rectWindow.top;
668 lpPos->rectNormal.right = wnd->rectWindow.right;
669 lpPos->rectNormal.bottom = wnd->rectWindow.bottom;
670 lpPos->ptIconPos.x = lpPos->ptIconPos.y = -1;
671 lpPos->ptMaxPos.x = lpPos->ptMaxPos.y = -1;
674 if( wnd->dwStyle & WS_MINIMIZE )
676 lpPos->ptIconPos.x = wnd->rectWindow.left;
677 lpPos->ptIconPos.y = wnd->rectWindow.top;
679 else if( wnd->dwStyle & WS_MAXIMIZE )
681 lpPos->ptMaxPos.x = wnd->rectWindow.left;
682 lpPos->ptMaxPos.y = wnd->rectWindow.top;
684 else
686 lpPos->rectNormal.left = wnd->rectWindow.left;
687 lpPos->rectNormal.top = wnd->rectWindow.top;
688 lpPos->rectNormal.right = wnd->rectWindow.right;
689 lpPos->rectNormal.bottom = wnd->rectWindow.bottom;
691 return lpPos;
694 /***********************************************************************
695 * WINPOS_RedrawIconTitle
697 BOOL WINPOS_RedrawIconTitle( HWND hWnd )
699 LPINTERNALPOS lpPos = get_internal_pos( hWnd );
700 if( lpPos )
702 if( lpPos->hwndIconTitle )
704 SendMessageW( lpPos->hwndIconTitle, WM_SHOWWINDOW, TRUE, 0);
705 InvalidateRect( lpPos->hwndIconTitle, NULL, TRUE );
706 return TRUE;
709 return FALSE;
712 /***********************************************************************
713 * WINPOS_ShowIconTitle
715 BOOL WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
717 LPINTERNALPOS lpPos = get_internal_pos( hwnd );
719 if (lpPos && !GetPropA( hwnd, "__wine_x11_managed" ))
721 HWND title = lpPos->hwndIconTitle;
723 TRACE("%p %i\n", hwnd, (bShow != 0) );
725 if( !title )
726 lpPos->hwndIconTitle = title = ICONTITLE_Create( hwnd );
727 if( bShow )
729 if (!IsWindowVisible(title))
731 SendMessageW( title, WM_SHOWWINDOW, TRUE, 0 );
732 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
733 SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
736 else ShowWindow( title, SW_HIDE );
738 return FALSE;
741 /*******************************************************************
742 * WINPOS_GetMinMaxInfo
744 * Get the minimized and maximized information for a window.
746 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
747 POINT *minTrack, POINT *maxTrack )
749 LPINTERNALPOS lpPos;
750 MINMAXINFO MinMax;
751 HMONITOR monitor;
752 INT xinc, yinc;
753 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
754 LONG exstyle = GetWindowLongA( hwnd, GWL_EXSTYLE );
755 RECT rc;
757 /* Compute default values */
759 GetWindowRect(hwnd, &rc);
760 MinMax.ptReserved.x = rc.left;
761 MinMax.ptReserved.y = rc.top;
763 if (style & WS_CHILD)
765 if ((style & WS_CAPTION) == WS_CAPTION)
766 style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
768 GetClientRect(GetAncestor(hwnd,GA_PARENT), &rc);
769 AdjustWindowRectEx(&rc, style, ((style & WS_POPUP) && GetMenu(hwnd)), exstyle);
771 /* avoid calculating this twice */
772 style &= ~(WS_DLGFRAME | WS_BORDER | WS_THICKFRAME);
774 MinMax.ptMaxSize.x = rc.right - rc.left;
775 MinMax.ptMaxSize.y = rc.bottom - rc.top;
777 else
779 MinMax.ptMaxSize.x = GetSystemMetrics(SM_CXSCREEN);
780 MinMax.ptMaxSize.y = GetSystemMetrics(SM_CYSCREEN);
782 MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
783 MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
784 MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXMAXTRACK);
785 MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYMAXTRACK);
787 if (HAS_DLGFRAME( style, exstyle ))
789 xinc = GetSystemMetrics(SM_CXDLGFRAME);
790 yinc = GetSystemMetrics(SM_CYDLGFRAME);
792 else
794 xinc = yinc = 0;
795 if (HAS_THICKFRAME(style))
797 xinc += GetSystemMetrics(SM_CXFRAME);
798 yinc += GetSystemMetrics(SM_CYFRAME);
800 if (style & WS_BORDER)
802 xinc += GetSystemMetrics(SM_CXBORDER);
803 yinc += GetSystemMetrics(SM_CYBORDER);
806 MinMax.ptMaxSize.x += 2 * xinc;
807 MinMax.ptMaxSize.y += 2 * yinc;
809 lpPos = get_internal_pos( hwnd );
810 if( lpPos && !EMPTYPOINT(lpPos->ptMaxPos) )
812 MinMax.ptMaxPosition.x = lpPos->ptMaxPos.x;
813 MinMax.ptMaxPosition.y = lpPos->ptMaxPos.y;
815 else
817 MinMax.ptMaxPosition.x = -xinc;
818 MinMax.ptMaxPosition.y = -yinc;
821 SendMessageW( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
823 /* if the app didn't change the values, adapt them for the current monitor */
825 if ((monitor = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY )))
827 MONITORINFO mon_info;
829 mon_info.cbSize = sizeof(mon_info);
830 GetMonitorInfoW( monitor, &mon_info );
832 if (MinMax.ptMaxSize.x == GetSystemMetrics(SM_CXSCREEN) + 2 * xinc &&
833 MinMax.ptMaxSize.y == GetSystemMetrics(SM_CYSCREEN) + 2 * yinc)
835 MinMax.ptMaxSize.x = (mon_info.rcWork.right - mon_info.rcWork.left) + 2 * xinc;
836 MinMax.ptMaxSize.y = (mon_info.rcWork.bottom - mon_info.rcWork.top) + 2 * yinc;
838 if (MinMax.ptMaxPosition.x == -xinc && MinMax.ptMaxPosition.y == -yinc)
840 MinMax.ptMaxPosition.x = mon_info.rcWork.left - xinc;
841 MinMax.ptMaxPosition.y = mon_info.rcWork.top - yinc;
845 /* Some sanity checks */
847 TRACE("%d %d / %d %d / %d %d / %d %d\n",
848 MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
849 MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
850 MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
851 MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
852 MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
853 MinMax.ptMinTrackSize.x );
854 MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
855 MinMax.ptMinTrackSize.y );
857 if (maxSize) *maxSize = MinMax.ptMaxSize;
858 if (maxPos) *maxPos = MinMax.ptMaxPosition;
859 if (minTrack) *minTrack = MinMax.ptMinTrackSize;
860 if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
863 /***********************************************************************
864 * ShowWindowAsync (USER32.@)
866 * doesn't wait; returns immediately.
867 * used by threads to toggle windows in other (possibly hanging) threads
869 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
871 HWND full_handle;
873 if (is_broadcast(hwnd))
875 SetLastError( ERROR_INVALID_PARAMETER );
876 return FALSE;
879 if ((full_handle = WIN_IsCurrentThread( hwnd )))
880 return USER_Driver->pShowWindow( full_handle, cmd );
882 return SendNotifyMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
886 /***********************************************************************
887 * ShowWindow (USER32.@)
889 BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
891 HWND full_handle;
893 if (is_broadcast(hwnd))
895 SetLastError( ERROR_INVALID_PARAMETER );
896 return FALSE;
898 if ((full_handle = WIN_IsCurrentThread( hwnd )))
899 return USER_Driver->pShowWindow( full_handle, cmd );
901 return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
905 /***********************************************************************
906 * GetInternalWindowPos (USER32.@)
908 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
909 LPPOINT ptIcon )
911 WINDOWPLACEMENT wndpl;
912 if (GetWindowPlacement( hwnd, &wndpl ))
914 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
915 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
916 return wndpl.showCmd;
918 return 0;
922 /***********************************************************************
923 * GetWindowPlacement (USER32.@)
925 * Win95:
926 * Fails if wndpl->length of Win95 (!) apps is invalid.
928 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
930 WND *pWnd = WIN_GetPtr( hwnd );
931 LPINTERNALPOS lpPos;
933 if (!pWnd || pWnd == WND_DESKTOP) return FALSE;
934 if (pWnd == WND_OTHER_PROCESS)
936 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
937 return FALSE;
940 lpPos = WINPOS_InitInternalPos( pWnd );
941 wndpl->length = sizeof(*wndpl);
942 if( pWnd->dwStyle & WS_MINIMIZE )
943 wndpl->showCmd = SW_SHOWMINIMIZED;
944 else
945 wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
946 if( pWnd->flags & WIN_RESTORE_MAX )
947 wndpl->flags = WPF_RESTORETOMAXIMIZED;
948 else
949 wndpl->flags = 0;
950 wndpl->ptMinPosition.x = lpPos->ptIconPos.x;
951 wndpl->ptMinPosition.y = lpPos->ptIconPos.y;
952 wndpl->ptMaxPosition.x = lpPos->ptMaxPos.x;
953 wndpl->ptMaxPosition.y = lpPos->ptMaxPos.y;
954 wndpl->rcNormalPosition.left = lpPos->rectNormal.left;
955 wndpl->rcNormalPosition.top = lpPos->rectNormal.top;
956 wndpl->rcNormalPosition.right = lpPos->rectNormal.right;
957 wndpl->rcNormalPosition.bottom = lpPos->rectNormal.bottom;
958 WIN_ReleasePtr( pWnd );
959 return TRUE;
963 /***********************************************************************
964 * WINPOS_SetPlacement
966 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
968 LPINTERNALPOS lpPos;
969 DWORD style;
970 WND *pWnd = WIN_GetPtr( hwnd );
972 if (!pWnd || pWnd == WND_OTHER_PROCESS || pWnd == WND_DESKTOP) return FALSE;
973 lpPos = WINPOS_InitInternalPos( pWnd );
975 if( flags & PLACE_MIN )
977 lpPos->ptIconPos.x = wndpl->ptMinPosition.x;
978 lpPos->ptIconPos.y = wndpl->ptMinPosition.y;
980 if( flags & PLACE_MAX )
982 lpPos->ptMaxPos.x = wndpl->ptMaxPosition.x;
983 lpPos->ptMaxPos.y = wndpl->ptMaxPosition.y;
985 if( flags & PLACE_RECT)
987 lpPos->rectNormal.left = wndpl->rcNormalPosition.left;
988 lpPos->rectNormal.top = wndpl->rcNormalPosition.top;
989 lpPos->rectNormal.right = wndpl->rcNormalPosition.right;
990 lpPos->rectNormal.bottom = wndpl->rcNormalPosition.bottom;
993 style = pWnd->dwStyle;
994 WIN_ReleasePtr( pWnd );
996 if( style & WS_MINIMIZE )
998 WINPOS_ShowIconTitle( hwnd, FALSE );
999 if( wndpl->flags & WPF_SETMINPOSITION && !EMPTYPOINT(lpPos->ptIconPos))
1000 SetWindowPos( hwnd, 0, lpPos->ptIconPos.x, lpPos->ptIconPos.y,
1001 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1003 else if( style & WS_MAXIMIZE )
1005 if( !EMPTYPOINT(lpPos->ptMaxPos) )
1006 SetWindowPos( hwnd, 0, lpPos->ptMaxPos.x, lpPos->ptMaxPos.y,
1007 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1009 else if( flags & PLACE_RECT )
1010 SetWindowPos( hwnd, 0, lpPos->rectNormal.left, lpPos->rectNormal.top,
1011 lpPos->rectNormal.right - lpPos->rectNormal.left,
1012 lpPos->rectNormal.bottom - lpPos->rectNormal.top,
1013 SWP_NOZORDER | SWP_NOACTIVATE );
1015 ShowWindow( hwnd, wndpl->showCmd );
1017 if (IsIconic( hwnd ))
1019 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE) WINPOS_ShowIconTitle( hwnd, TRUE );
1021 /* SDK: ...valid only the next time... */
1022 if( wndpl->flags & WPF_RESTORETOMAXIMIZED )
1024 pWnd = WIN_GetPtr( hwnd );
1025 if (pWnd && pWnd != WND_OTHER_PROCESS)
1027 pWnd->flags |= WIN_RESTORE_MAX;
1028 WIN_ReleasePtr( pWnd );
1032 return TRUE;
1036 /***********************************************************************
1037 * SetWindowPlacement (USER32.@)
1039 * Win95:
1040 * Fails if wndpl->length of Win95 (!) apps is invalid.
1042 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
1044 if (!wpl) return FALSE;
1045 return WINPOS_SetPlacement( hwnd, wpl, PLACE_MIN | PLACE_MAX | PLACE_RECT );
1049 /***********************************************************************
1050 * AnimateWindow (USER32.@)
1051 * Shows/Hides a window with an animation
1052 * NO ANIMATION YET
1054 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
1056 FIXME("partial stub\n");
1058 /* If trying to show/hide and it's already *
1059 * shown/hidden or invalid window, fail with *
1060 * invalid parameter */
1061 if(!IsWindow(hwnd) ||
1062 (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
1063 (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
1065 SetLastError(ERROR_INVALID_PARAMETER);
1066 return FALSE;
1069 ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1071 return TRUE;
1074 /***********************************************************************
1075 * SetInternalWindowPos (USER32.@)
1077 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1078 LPRECT rect, LPPOINT pt )
1080 if( IsWindow(hwnd) )
1082 WINDOWPLACEMENT wndpl;
1083 UINT flags;
1085 wndpl.length = sizeof(wndpl);
1086 wndpl.showCmd = showCmd;
1087 wndpl.flags = flags = 0;
1089 if( pt )
1091 flags |= PLACE_MIN;
1092 wndpl.flags |= WPF_SETMINPOSITION;
1093 wndpl.ptMinPosition = *pt;
1095 if( rect )
1097 flags |= PLACE_RECT;
1098 wndpl.rcNormalPosition = *rect;
1100 WINPOS_SetPlacement( hwnd, &wndpl, flags );
1105 /*******************************************************************
1106 * can_activate_window
1108 * Check if we can activate the specified window.
1110 static BOOL can_activate_window( HWND hwnd )
1112 LONG style;
1114 if (!hwnd) return FALSE;
1115 style = GetWindowLongW( hwnd, GWL_STYLE );
1116 if (!(style & WS_VISIBLE)) return FALSE;
1117 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1118 return !(style & WS_DISABLED);
1122 /*******************************************************************
1123 * WINPOS_ActivateOtherWindow
1125 * Activates window other than pWnd.
1127 void WINPOS_ActivateOtherWindow(HWND hwnd)
1129 HWND hwndTo, fg;
1131 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1133 hwndTo = GetAncestor( hwndTo, GA_ROOT );
1134 if (can_activate_window( hwndTo )) goto done;
1137 hwndTo = hwnd;
1138 for (;;)
1140 if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1141 if (can_activate_window( hwndTo )) break;
1144 done:
1145 fg = GetForegroundWindow();
1146 TRACE("win = %p fg = %p\n", hwndTo, fg);
1147 if (!fg || (hwnd == fg))
1149 if (SetForegroundWindow( hwndTo )) return;
1151 if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1155 /***********************************************************************
1156 * WINPOS_HandleWindowPosChanging
1158 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1160 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1162 POINT minTrack, maxTrack;
1163 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1165 if (winpos->flags & SWP_NOSIZE) return 0;
1166 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1168 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1169 if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1170 if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1171 if (!(style & WS_MINIMIZE))
1173 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1174 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1177 return 0;
1181 /***********************************************************************
1182 * dump_winpos_flags
1184 static void dump_winpos_flags(UINT flags)
1186 TRACE("flags:");
1187 if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1188 if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1189 if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1190 if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1191 if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1192 if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1193 if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1194 if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1195 if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1196 if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1197 if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1198 if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1199 if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1201 #define DUMPED_FLAGS \
1202 (SWP_NOSIZE | \
1203 SWP_NOMOVE | \
1204 SWP_NOZORDER | \
1205 SWP_NOREDRAW | \
1206 SWP_NOACTIVATE | \
1207 SWP_FRAMECHANGED | \
1208 SWP_SHOWWINDOW | \
1209 SWP_HIDEWINDOW | \
1210 SWP_NOCOPYBITS | \
1211 SWP_NOOWNERZORDER | \
1212 SWP_NOSENDCHANGING | \
1213 SWP_DEFERERASE | \
1214 SWP_ASYNCWINDOWPOS)
1216 if(flags & ~DUMPED_FLAGS) TRACE(" %08x", flags & ~DUMPED_FLAGS);
1217 TRACE("\n");
1218 #undef DUMPED_FLAGS
1221 /***********************************************************************
1222 * SWP_DoWinPosChanging
1224 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
1226 WND *wndPtr;
1228 /* Send WM_WINDOWPOSCHANGING message */
1230 if (!(pWinpos->flags & SWP_NOSENDCHANGING))
1231 SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
1233 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) ||
1234 wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
1236 /* Calculate new position and size */
1238 *pNewWindowRect = wndPtr->rectWindow;
1239 *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
1240 : wndPtr->rectClient;
1242 if (!(pWinpos->flags & SWP_NOSIZE))
1244 pNewWindowRect->right = pNewWindowRect->left + pWinpos->cx;
1245 pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
1247 if (!(pWinpos->flags & SWP_NOMOVE))
1249 pNewWindowRect->left = pWinpos->x;
1250 pNewWindowRect->top = pWinpos->y;
1251 pNewWindowRect->right += pWinpos->x - wndPtr->rectWindow.left;
1252 pNewWindowRect->bottom += pWinpos->y - wndPtr->rectWindow.top;
1254 OffsetRect( pNewClientRect, pWinpos->x - wndPtr->rectWindow.left,
1255 pWinpos->y - wndPtr->rectWindow.top );
1257 pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
1259 TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
1260 pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
1261 pWinpos->cx, pWinpos->cy, pWinpos->flags );
1262 TRACE( "current %s style %08x new %s\n",
1263 wine_dbgstr_rect( &wndPtr->rectWindow ), wndPtr->dwStyle,
1264 wine_dbgstr_rect( pNewWindowRect ));
1266 WIN_ReleasePtr( wndPtr );
1267 return TRUE;
1270 /***********************************************************************
1271 * get_valid_rects
1273 * Compute the valid rects from the old and new client rect and WVR_* flags.
1274 * Helper for WM_NCCALCSIZE handling.
1276 static inline void get_valid_rects( const RECT *old_client, const RECT *new_client, UINT flags,
1277 RECT *valid )
1279 int cx, cy;
1281 if (flags & WVR_REDRAW)
1283 SetRectEmpty( &valid[0] );
1284 SetRectEmpty( &valid[1] );
1285 return;
1288 if (flags & WVR_VALIDRECTS)
1290 if (!IntersectRect( &valid[0], &valid[0], new_client ) ||
1291 !IntersectRect( &valid[1], &valid[1], old_client ))
1293 SetRectEmpty( &valid[0] );
1294 SetRectEmpty( &valid[1] );
1295 return;
1297 flags = WVR_ALIGNLEFT | WVR_ALIGNTOP;
1299 else
1301 valid[0] = *new_client;
1302 valid[1] = *old_client;
1305 /* make sure the rectangles have the same size */
1306 cx = min( valid[0].right - valid[0].left, valid[1].right - valid[1].left );
1307 cy = min( valid[0].bottom - valid[0].top, valid[1].bottom - valid[1].top );
1309 if (flags & WVR_ALIGNBOTTOM)
1311 valid[0].top = valid[0].bottom - cy;
1312 valid[1].top = valid[1].bottom - cy;
1314 else
1316 valid[0].bottom = valid[0].top + cy;
1317 valid[1].bottom = valid[1].top + cy;
1319 if (flags & WVR_ALIGNRIGHT)
1321 valid[0].left = valid[0].right - cx;
1322 valid[1].left = valid[1].right - cx;
1324 else
1326 valid[0].right = valid[0].left + cx;
1327 valid[1].right = valid[1].left + cx;
1331 struct move_owned_info
1333 HWND owner;
1334 HWND insert_after;
1337 static BOOL CALLBACK move_owned_popups( HWND hwnd, LPARAM lparam )
1339 struct move_owned_info *info = (struct move_owned_info *)lparam;
1341 if (hwnd == info->owner) return FALSE;
1342 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) &&
1343 GetWindow( hwnd, GW_OWNER ) == info->owner)
1345 SetWindowPos( hwnd, info->insert_after, 0, 0, 0, 0,
1346 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE |
1347 SWP_NOSENDCHANGING | SWP_DEFERERASE );
1348 info->insert_after = hwnd;
1350 return TRUE;
1353 /***********************************************************************
1354 * SWP_DoOwnedPopups
1356 * fix Z order taking into account owned popups -
1357 * basically we need to maintain them above the window that owns them
1359 * FIXME: hide/show owned popups when owner visibility changes.
1361 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
1363 HWND owner = GetWindow( hwnd, GW_OWNER );
1364 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1365 struct move_owned_info info;
1367 TRACE("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
1369 if ((style & WS_POPUP) && owner)
1371 /* make sure this popup stays above the owner */
1373 if( hwndInsertAfter != HWND_TOP )
1375 HWND hwndLocalPrev = HWND_TOP;
1376 HWND prev = GetWindow( owner, GW_HWNDPREV );
1378 while (prev && prev != hwndInsertAfter)
1380 if (hwndLocalPrev == HWND_TOP && GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE)
1381 hwndLocalPrev = prev;
1382 prev = GetWindow( prev, GW_HWNDPREV );
1384 if (!prev) hwndInsertAfter = hwndLocalPrev;
1387 else if (style & WS_CHILD) return hwndInsertAfter;
1389 info.owner = hwnd;
1390 info.insert_after = hwndInsertAfter;
1391 EnumWindows( move_owned_popups, (LPARAM)&info );
1392 return info.insert_after;
1395 /***********************************************************************
1396 * SWP_DoNCCalcSize
1398 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect,
1399 RECT *validRects )
1401 UINT wvrFlags = 0;
1402 WND *wndPtr;
1404 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
1406 /* Send WM_NCCALCSIZE message to get new client area */
1407 if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
1409 NCCALCSIZE_PARAMS params;
1410 WINDOWPOS winposCopy;
1412 params.rgrc[0] = *pNewWindowRect;
1413 params.rgrc[1] = wndPtr->rectWindow;
1414 params.rgrc[2] = wndPtr->rectClient;
1415 params.lppos = &winposCopy;
1416 winposCopy = *pWinpos;
1417 WIN_ReleasePtr( wndPtr );
1419 wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
1421 *pNewClientRect = params.rgrc[0];
1423 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
1425 TRACE( "hwnd %p old win %s old client %s new win %s new client %s\n", pWinpos->hwnd,
1426 wine_dbgstr_rect(&wndPtr->rectWindow), wine_dbgstr_rect(&wndPtr->rectClient),
1427 wine_dbgstr_rect(pNewWindowRect), wine_dbgstr_rect(pNewClientRect) );
1429 if( pNewClientRect->left != wndPtr->rectClient.left ||
1430 pNewClientRect->top != wndPtr->rectClient.top )
1431 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1433 if( (pNewClientRect->right - pNewClientRect->left !=
1434 wndPtr->rectClient.right - wndPtr->rectClient.left))
1435 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1436 else
1437 wvrFlags &= ~WVR_HREDRAW;
1439 if (pNewClientRect->bottom - pNewClientRect->top !=
1440 wndPtr->rectClient.bottom - wndPtr->rectClient.top)
1441 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1442 else
1443 wvrFlags &= ~WVR_VREDRAW;
1445 validRects[0] = params.rgrc[1];
1446 validRects[1] = params.rgrc[2];
1448 else
1450 if (!(pWinpos->flags & SWP_NOMOVE) &&
1451 (pNewClientRect->left != wndPtr->rectClient.left ||
1452 pNewClientRect->top != wndPtr->rectClient.top))
1453 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1456 if (pWinpos->flags & (SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
1458 SetRectEmpty( &validRects[0] );
1459 SetRectEmpty( &validRects[1] );
1461 else get_valid_rects( &wndPtr->rectClient, pNewClientRect, wvrFlags, validRects );
1463 WIN_ReleasePtr( wndPtr );
1464 return wvrFlags;
1467 /* fix redundant flags and values in the WINDOWPOS structure */
1468 static BOOL fixup_flags( WINDOWPOS *winpos )
1470 HWND parent;
1471 WND *wndPtr = WIN_GetPtr( winpos->hwnd );
1472 BOOL ret = TRUE;
1474 if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
1476 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1477 return FALSE;
1479 winpos->hwnd = wndPtr->hwndSelf; /* make it a full handle */
1481 /* Finally make sure that all coordinates are valid */
1482 if (winpos->x < -32768) winpos->x = -32768;
1483 else if (winpos->x > 32767) winpos->x = 32767;
1484 if (winpos->y < -32768) winpos->y = -32768;
1485 else if (winpos->y > 32767) winpos->y = 32767;
1487 if (winpos->cx < 0) winpos->cx = 0;
1488 else if (winpos->cx > 32767) winpos->cx = 32767;
1489 if (winpos->cy < 0) winpos->cy = 0;
1490 else if (winpos->cy > 32767) winpos->cy = 32767;
1492 parent = GetAncestor( winpos->hwnd, GA_PARENT );
1493 if (!IsWindowVisible( parent )) winpos->flags |= SWP_NOREDRAW;
1495 if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
1496 else
1498 winpos->flags &= ~SWP_HIDEWINDOW;
1499 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
1502 if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
1503 (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
1504 winpos->flags |= SWP_NOSIZE; /* Already the right size */
1506 if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
1507 winpos->flags |= SWP_NOMOVE; /* Already the right position */
1509 if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
1511 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) /* Bring to the top when activating */
1513 winpos->flags &= ~SWP_NOZORDER;
1514 winpos->hwndInsertAfter = HWND_TOP;
1518 /* Check hwndInsertAfter */
1519 if (winpos->flags & SWP_NOZORDER) goto done;
1521 /* fix sign extension */
1522 if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
1523 else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
1525 /* FIXME: TOPMOST not supported yet */
1526 if ((winpos->hwndInsertAfter == HWND_TOPMOST) ||
1527 (winpos->hwndInsertAfter == HWND_NOTOPMOST)) winpos->hwndInsertAfter = HWND_TOP;
1529 /* hwndInsertAfter must be a sibling of the window */
1530 if (winpos->hwndInsertAfter == HWND_TOP)
1532 if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1533 winpos->flags |= SWP_NOZORDER;
1535 else if (winpos->hwndInsertAfter == HWND_BOTTOM)
1537 if (GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
1538 winpos->flags |= SWP_NOZORDER;
1540 else
1542 if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != parent) ret = FALSE;
1543 else
1545 /* don't need to change the Zorder of hwnd if it's already inserted
1546 * after hwndInsertAfter or when inserting hwnd after itself.
1548 if ((winpos->hwnd == winpos->hwndInsertAfter) ||
1549 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
1550 winpos->flags |= SWP_NOZORDER;
1553 done:
1554 WIN_ReleasePtr( wndPtr );
1555 return ret;
1558 /***********************************************************************
1559 * USER_SetWindowPos
1561 * User32 internal function
1563 BOOL USER_SetWindowPos( WINDOWPOS * winpos )
1565 RECT newWindowRect, newClientRect, valid_rects[2];
1566 UINT orig_flags;
1568 orig_flags = winpos->flags;
1570 /* First make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
1571 if (!(winpos->flags & SWP_NOMOVE))
1573 if (winpos->x < -32768) winpos->x = -32768;
1574 else if (winpos->x > 32767) winpos->x = 32767;
1575 if (winpos->y < -32768) winpos->y = -32768;
1576 else if (winpos->y > 32767) winpos->y = 32767;
1578 if (!(winpos->flags & SWP_NOSIZE))
1580 if (winpos->cx < 0) winpos->cx = 0;
1581 else if (winpos->cx > 32767) winpos->cx = 32767;
1582 if (winpos->cy < 0) winpos->cy = 0;
1583 else if (winpos->cy > 32767) winpos->cy = 32767;
1586 if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
1588 /* Fix redundant flags */
1589 if (!fixup_flags( winpos )) return FALSE;
1591 if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
1593 if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
1594 winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
1597 /* Common operations */
1599 SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect, valid_rects );
1601 if(!USER_Driver->pSetWindowPos( winpos->hwnd, winpos->hwndInsertAfter,
1602 &newWindowRect, &newClientRect, orig_flags, valid_rects ))
1603 return FALSE;
1605 if (!(orig_flags & SWP_SHOWWINDOW))
1607 UINT rdw_flags = RDW_FRAME | RDW_ERASE;
1608 if ( !(orig_flags & SWP_DEFERERASE) ) rdw_flags |= RDW_ERASENOW;
1609 RedrawWindow( winpos->hwnd, NULL, NULL, rdw_flags );
1612 if( winpos->flags & SWP_HIDEWINDOW )
1613 HideCaret(winpos->hwnd);
1614 else if (winpos->flags & SWP_SHOWWINDOW)
1615 ShowCaret(winpos->hwnd);
1617 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)))
1619 /* child windows get WM_CHILDACTIVATE message */
1620 if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
1621 SendMessageW( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
1622 else
1623 SetForegroundWindow( winpos->hwnd );
1626 /* And last, send the WM_WINDOWPOSCHANGED message */
1628 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
1630 if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
1632 /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
1633 and always contains final window position.
1635 winpos->x = newWindowRect.left;
1636 winpos->y = newWindowRect.top;
1637 winpos->cx = newWindowRect.right - newWindowRect.left;
1638 winpos->cy = newWindowRect.bottom - newWindowRect.top;
1639 SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
1641 return TRUE;
1644 /***********************************************************************
1645 * SetWindowPos (USER32.@)
1647 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
1648 INT x, INT y, INT cx, INT cy, UINT flags )
1650 WINDOWPOS winpos;
1652 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1653 hwnd, hwndInsertAfter, x, y, cx, cy, flags);
1654 if(TRACE_ON(win)) dump_winpos_flags(flags);
1656 if (is_broadcast(hwnd))
1658 SetLastError( ERROR_INVALID_PARAMETER );
1659 return FALSE;
1662 winpos.hwnd = WIN_GetFullHandle(hwnd);
1663 winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
1664 winpos.x = x;
1665 winpos.y = y;
1666 winpos.cx = cx;
1667 winpos.cy = cy;
1668 winpos.flags = flags;
1670 if (WIN_IsCurrentThread( hwnd ))
1671 return USER_SetWindowPos(&winpos);
1673 return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
1677 /***********************************************************************
1678 * BeginDeferWindowPos (USER32.@)
1680 HDWP WINAPI BeginDeferWindowPos( INT count )
1682 HDWP handle;
1683 DWP *pDWP;
1685 TRACE("%d\n", count);
1687 if (count < 0)
1689 SetLastError(ERROR_INVALID_PARAMETER);
1690 return 0;
1692 /* Windows allows zero count, in which case it allocates context for 8 moves */
1693 if (count == 0) count = 8;
1695 handle = USER_HEAP_ALLOC( sizeof(DWP) + (count-1)*sizeof(WINDOWPOS) );
1696 if (!handle) return 0;
1697 pDWP = (DWP *) USER_HEAP_LIN_ADDR( handle );
1698 pDWP->actualCount = 0;
1699 pDWP->suggestedCount = count;
1700 pDWP->valid = TRUE;
1701 pDWP->wMagic = DWP_MAGIC;
1702 pDWP->hwndParent = 0;
1704 TRACE("returning hdwp %p\n", handle);
1705 return handle;
1709 /***********************************************************************
1710 * DeferWindowPos (USER32.@)
1712 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
1713 INT x, INT y, INT cx, INT cy,
1714 UINT flags )
1716 DWP *pDWP;
1717 int i;
1718 HDWP newhdwp = hdwp,retvalue;
1720 TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1721 hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
1723 hwnd = WIN_GetFullHandle( hwnd );
1724 if (hwnd == GetDesktopWindow()) return 0;
1726 if (!(pDWP = USER_HEAP_LIN_ADDR( hdwp ))) return 0;
1728 USER_Lock();
1730 for (i = 0; i < pDWP->actualCount; i++)
1732 if (pDWP->winPos[i].hwnd == hwnd)
1734 /* Merge with the other changes */
1735 if (!(flags & SWP_NOZORDER))
1737 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
1739 if (!(flags & SWP_NOMOVE))
1741 pDWP->winPos[i].x = x;
1742 pDWP->winPos[i].y = y;
1744 if (!(flags & SWP_NOSIZE))
1746 pDWP->winPos[i].cx = cx;
1747 pDWP->winPos[i].cy = cy;
1749 pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
1750 SWP_NOZORDER | SWP_NOREDRAW |
1751 SWP_NOACTIVATE | SWP_NOCOPYBITS|
1752 SWP_NOOWNERZORDER);
1753 pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
1754 SWP_FRAMECHANGED);
1755 retvalue = hdwp;
1756 goto END;
1759 if (pDWP->actualCount >= pDWP->suggestedCount)
1761 newhdwp = USER_HEAP_REALLOC( hdwp,
1762 sizeof(DWP) + pDWP->suggestedCount*sizeof(WINDOWPOS) );
1763 if (!newhdwp)
1765 retvalue = 0;
1766 goto END;
1768 pDWP = (DWP *) USER_HEAP_LIN_ADDR( newhdwp );
1769 pDWP->suggestedCount++;
1771 pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
1772 pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
1773 pDWP->winPos[pDWP->actualCount].x = x;
1774 pDWP->winPos[pDWP->actualCount].y = y;
1775 pDWP->winPos[pDWP->actualCount].cx = cx;
1776 pDWP->winPos[pDWP->actualCount].cy = cy;
1777 pDWP->winPos[pDWP->actualCount].flags = flags;
1778 pDWP->actualCount++;
1779 retvalue = newhdwp;
1780 END:
1781 USER_Unlock();
1782 return retvalue;
1786 /***********************************************************************
1787 * EndDeferWindowPos (USER32.@)
1789 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
1791 DWP *pDWP;
1792 WINDOWPOS *winpos;
1793 BOOL res = TRUE;
1794 int i;
1796 TRACE("%p\n", hdwp);
1798 pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
1799 if (!pDWP) return FALSE;
1800 for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
1802 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1803 winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
1804 winpos->cx, winpos->cy, winpos->flags);
1806 if (!(res = USER_SetWindowPos( winpos ))) break;
1808 USER_HEAP_FREE( hdwp );
1809 return res;
1813 /***********************************************************************
1814 * TileChildWindows (USER.199)
1816 void WINAPI TileChildWindows16( HWND16 parent, WORD action )
1818 FIXME("(%04x, %d): stub\n", parent, action);
1821 /***********************************************************************
1822 * CascadeChildWindows (USER.198)
1824 void WINAPI CascadeChildWindows16( HWND16 parent, WORD action )
1826 FIXME("(%04x, %d): stub\n", parent, action);