user32: Fix a couple of message test failures.
[wine/hacks.git] / dlls / user32 / winpos.c
blobd61fe0148410ec14854875f506cf4004b69b2569
1 /*
2 * Window position related functions.
4 * Copyright 1993, 1994, 1995 Alexandre Julliard
5 * 1995, 1996, 1999 Alex Korobka
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdarg.h>
26 #include <string.h>
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winerror.h"
33 #include "wine/server.h"
34 #include "controls.h"
35 #include "user_private.h"
36 #include "win.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(win);
41 #define SWP_AGG_NOGEOMETRYCHANGE \
42 (SWP_NOSIZE | SWP_NOCLIENTSIZE | SWP_NOZORDER)
43 #define SWP_AGG_NOPOSCHANGE \
44 (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
45 #define SWP_AGG_STATUSFLAGS \
46 (SWP_AGG_NOPOSCHANGE | SWP_FRAMECHANGED | SWP_HIDEWINDOW | SWP_SHOWWINDOW)
48 #define HAS_DLGFRAME(style,exStyle) \
49 (((exStyle) & WS_EX_DLGMODALFRAME) || \
50 (((style) & WS_DLGFRAME) && !((style) & WS_BORDER)))
52 #define HAS_THICKFRAME(style) \
53 (((style) & WS_THICKFRAME) && \
54 !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
56 #define EMPTYPOINT(pt) ((*(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 (%s)\n", hwnd, wine_dbgstr_rect(rect) );
175 return ret;
179 /***********************************************************************
180 * GetWindowRgn (USER32.@)
182 int WINAPI GetWindowRgn ( HWND hwnd, HRGN hrgn )
184 int nRet = ERROR;
185 NTSTATUS status;
186 HRGN win_rgn = 0;
187 RGNDATA *data;
188 size_t size = 256;
192 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
194 SetLastError( ERROR_OUTOFMEMORY );
195 return ERROR;
197 SERVER_START_REQ( get_window_region )
199 req->window = hwnd;
200 wine_server_set_reply( req, data->Buffer, size );
201 if (!(status = wine_server_call( req )))
203 size_t reply_size = wine_server_reply_size( reply );
204 if (reply_size)
206 data->rdh.dwSize = sizeof(data->rdh);
207 data->rdh.iType = RDH_RECTANGLES;
208 data->rdh.nCount = reply_size / sizeof(RECT);
209 data->rdh.nRgnSize = reply_size;
210 win_rgn = ExtCreateRegion( NULL, size, data );
213 else size = reply->total_size;
215 SERVER_END_REQ;
216 HeapFree( GetProcessHeap(), 0, data );
217 } while (status == STATUS_BUFFER_OVERFLOW);
219 if (status) SetLastError( RtlNtStatusToDosError(status) );
220 else if (win_rgn)
222 nRet = CombineRgn( hrgn, win_rgn, 0, RGN_COPY );
223 DeleteObject( win_rgn );
225 return nRet;
229 /***********************************************************************
230 * SetWindowRgn (USER32.@)
232 int WINAPI SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL bRedraw )
234 static const RECT empty_rect;
235 BOOL ret;
237 if (hrgn)
239 RGNDATA *data;
240 DWORD size;
242 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
243 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
244 if (!GetRegionData( hrgn, size, data ))
246 HeapFree( GetProcessHeap(), 0, data );
247 return FALSE;
249 SERVER_START_REQ( set_window_region )
251 req->window = hwnd;
252 req->redraw = (bRedraw != 0);
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 req->redraw = (bRedraw != 0);
267 ret = !wine_server_call_err( req );
269 SERVER_END_REQ;
272 if (ret) ret = USER_Driver->pSetWindowRgn( hwnd, hrgn, bRedraw );
274 if (ret)
276 UINT swp_flags = SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE;
277 if (!bRedraw) swp_flags |= SWP_NOREDRAW;
278 SetWindowPos( hwnd, 0, 0, 0, 0, 0, swp_flags );
279 invalidate_dce( hwnd, NULL );
281 return ret;
285 /***********************************************************************
286 * GetClientRect (USER32.@)
288 BOOL WINAPI GetClientRect( HWND hwnd, LPRECT rect )
290 BOOL ret;
292 if ((ret = WIN_GetRectangles( hwnd, NULL, rect )))
294 rect->right -= rect->left;
295 rect->bottom -= rect->top;
296 rect->left = rect->top = 0;
298 return ret;
302 /*******************************************************************
303 * ClientToScreen (USER32.@)
305 BOOL WINAPI ClientToScreen( HWND hwnd, LPPOINT lppnt )
307 MapWindowPoints( hwnd, 0, lppnt, 1 );
308 return TRUE;
312 /*******************************************************************
313 * ScreenToClient (USER32.@)
315 BOOL WINAPI ScreenToClient( HWND hwnd, LPPOINT lppnt )
317 MapWindowPoints( 0, hwnd, lppnt, 1 );
318 return TRUE;
322 /***********************************************************************
323 * list_children_from_point
325 * Get the list of children that can contain point from the server.
326 * Point is in screen coordinates.
327 * Returned list must be freed by caller.
329 static HWND *list_children_from_point( HWND hwnd, POINT pt )
331 HWND *list;
332 int size = 32;
334 for (;;)
336 int count = 0;
338 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
340 SERVER_START_REQ( get_window_children_from_point )
342 req->parent = hwnd;
343 req->x = pt.x;
344 req->y = pt.y;
345 wine_server_set_reply( req, list, (size-1) * sizeof(HWND) );
346 if (!wine_server_call( req )) count = reply->count;
348 SERVER_END_REQ;
349 if (count && count < size)
351 list[count] = 0;
352 return list;
354 HeapFree( GetProcessHeap(), 0, list );
355 if (!count) break;
356 size = count + 1; /* restart with a large enough buffer */
358 return NULL;
362 /***********************************************************************
363 * WINPOS_WindowFromPoint
365 * Find the window and hittest for a given point.
367 HWND WINPOS_WindowFromPoint( HWND hwndScope, POINT pt, INT *hittest )
369 int i, res;
370 HWND ret, *list;
372 if (!hwndScope) hwndScope = GetDesktopWindow();
374 *hittest = HTNOWHERE;
376 if (!(list = list_children_from_point( hwndScope, pt ))) return 0;
378 /* now determine the hittest */
380 for (i = 0; list[i]; i++)
382 LONG style = GetWindowLongW( list[i], GWL_STYLE );
384 /* If window is minimized or disabled, return at once */
385 if (style & WS_MINIMIZE)
387 *hittest = HTCAPTION;
388 break;
390 if (style & WS_DISABLED)
392 *hittest = HTERROR;
393 break;
395 /* Send WM_NCCHITTEST (if same thread) */
396 if (!WIN_IsCurrentThread( list[i] ))
398 *hittest = HTCLIENT;
399 break;
401 res = SendMessageW( list[i], WM_NCHITTEST, 0, MAKELONG(pt.x,pt.y) );
402 if (res != HTTRANSPARENT)
404 *hittest = res; /* Found the window */
405 break;
407 /* continue search with next window in z-order */
409 ret = list[i];
410 HeapFree( GetProcessHeap(), 0, list );
411 TRACE( "scope %p (%d,%d) returning %p\n", hwndScope, pt.x, pt.y, ret );
412 return ret;
416 /*******************************************************************
417 * WindowFromPoint (USER32.@)
419 HWND WINAPI WindowFromPoint( POINT pt )
421 INT hittest;
422 return WINPOS_WindowFromPoint( 0, pt, &hittest );
426 /*******************************************************************
427 * ChildWindowFromPoint (USER32.@)
429 HWND WINAPI ChildWindowFromPoint( HWND hwndParent, POINT pt )
431 return ChildWindowFromPointEx( hwndParent, pt, CWP_ALL );
434 /*******************************************************************
435 * RealChildWindowFromPoint (USER32.@)
437 HWND WINAPI RealChildWindowFromPoint( HWND hwndParent, POINT pt )
439 return ChildWindowFromPointEx( hwndParent, pt, CWP_SKIPTRANSPARENT );
442 /*******************************************************************
443 * ChildWindowFromPointEx (USER32.@)
445 HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
447 /* pt is in the client coordinates */
448 HWND *list;
449 int i;
450 RECT rect;
451 HWND retvalue;
453 GetClientRect( hwndParent, &rect );
454 if (!PtInRect( &rect, pt )) return 0;
455 if (!(list = WIN_ListChildren( hwndParent ))) return hwndParent;
457 for (i = 0; list[i]; i++)
459 if (!WIN_GetRectangles( list[i], &rect, NULL )) continue;
460 if (!PtInRect( &rect, pt )) continue;
461 if (uFlags & (CWP_SKIPINVISIBLE|CWP_SKIPDISABLED))
463 LONG style = GetWindowLongW( list[i], GWL_STYLE );
464 if ((uFlags & CWP_SKIPINVISIBLE) && !(style & WS_VISIBLE)) continue;
465 if ((uFlags & CWP_SKIPDISABLED) && (style & WS_DISABLED)) continue;
467 if (uFlags & CWP_SKIPTRANSPARENT)
469 if (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TRANSPARENT) continue;
471 break;
473 retvalue = list[i];
474 HeapFree( GetProcessHeap(), 0, list );
475 if (!retvalue) retvalue = hwndParent;
476 return retvalue;
480 /*******************************************************************
481 * WINPOS_GetWinOffset
483 * Calculate the offset between the origin of the two windows. Used
484 * to implement MapWindowPoints.
486 static void WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo, POINT *offset )
488 WND * wndPtr;
490 offset->x = offset->y = 0;
492 /* Translate source window origin to screen coords */
493 if (hwndFrom)
495 HWND hwnd = hwndFrom;
497 while (hwnd)
499 if (hwnd == hwndTo) return;
500 if (!(wndPtr = WIN_GetPtr( hwnd )))
502 ERR( "bad hwndFrom = %p\n", hwnd );
503 return;
505 if (wndPtr == WND_DESKTOP) break;
506 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
507 offset->x += wndPtr->rectClient.left;
508 offset->y += wndPtr->rectClient.top;
509 hwnd = wndPtr->parent;
510 WIN_ReleasePtr( wndPtr );
514 /* Translate origin to destination window coords */
515 if (hwndTo)
517 HWND hwnd = hwndTo;
519 while (hwnd)
521 if (!(wndPtr = WIN_GetPtr( hwnd )))
523 ERR( "bad hwndTo = %p\n", hwnd );
524 return;
526 if (wndPtr == WND_DESKTOP) break;
527 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
528 offset->x -= wndPtr->rectClient.left;
529 offset->y -= wndPtr->rectClient.top;
530 hwnd = wndPtr->parent;
531 WIN_ReleasePtr( wndPtr );
534 return;
536 other_process: /* one of the parents may belong to another process, do it the hard way */
537 offset->x = offset->y = 0;
538 SERVER_START_REQ( get_windows_offset )
540 req->from = hwndFrom;
541 req->to = hwndTo;
542 if (!wine_server_call( req ))
544 offset->x = reply->x;
545 offset->y = reply->y;
548 SERVER_END_REQ;
552 /*******************************************************************
553 * MapWindowPoints (USER.258)
555 void WINAPI MapWindowPoints16( HWND16 hwndFrom, HWND16 hwndTo,
556 LPPOINT16 lppt, UINT16 count )
558 POINT offset;
560 WINPOS_GetWinOffset( WIN_Handle32(hwndFrom), WIN_Handle32(hwndTo), &offset );
561 while (count--)
563 lppt->x += offset.x;
564 lppt->y += offset.y;
565 lppt++;
570 /*******************************************************************
571 * MapWindowPoints (USER32.@)
573 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
575 POINT offset;
577 WINPOS_GetWinOffset( hwndFrom, hwndTo, &offset );
578 while (count--)
580 lppt->x += offset.x;
581 lppt->y += offset.y;
582 lppt++;
584 return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
588 /***********************************************************************
589 * IsIconic (USER32.@)
591 BOOL WINAPI IsIconic(HWND hWnd)
593 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
597 /***********************************************************************
598 * IsZoomed (USER32.@)
600 BOOL WINAPI IsZoomed(HWND hWnd)
602 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
606 /*******************************************************************
607 * AllowSetForegroundWindow (USER32.@)
609 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
611 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
612 * implemented, then fix this function. */
613 return TRUE;
617 /*******************************************************************
618 * LockSetForegroundWindow (USER32.@)
620 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
622 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
623 * implemented, then fix this function. */
624 return TRUE;
628 /***********************************************************************
629 * BringWindowToTop (USER32.@)
631 BOOL WINAPI BringWindowToTop( HWND hwnd )
633 return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
637 /***********************************************************************
638 * MoveWindow (USER32.@)
640 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
641 BOOL repaint )
643 int flags = SWP_NOZORDER | SWP_NOACTIVATE;
644 if (!repaint) flags |= SWP_NOREDRAW;
645 TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
646 return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
649 /***********************************************************************
650 * WINPOS_InitInternalPos
652 static LPINTERNALPOS WINPOS_InitInternalPos( WND* wnd )
654 LPINTERNALPOS lpPos = get_internal_pos( wnd->hwndSelf );
655 if( !lpPos )
657 /* this happens when the window is minimized/maximized
658 * for the first time (rectWindow is not adjusted yet) */
660 lpPos = HeapAlloc( GetProcessHeap(), 0, sizeof(INTERNALPOS) );
661 if( !lpPos ) return NULL;
663 set_internal_pos( wnd->hwndSelf, lpPos );
664 lpPos->hwndIconTitle = 0; /* defer until needs to be shown */
665 lpPos->rectNormal.left = wnd->rectWindow.left;
666 lpPos->rectNormal.top = wnd->rectWindow.top;
667 lpPos->rectNormal.right = wnd->rectWindow.right;
668 lpPos->rectNormal.bottom = wnd->rectWindow.bottom;
669 lpPos->ptIconPos.x = lpPos->ptIconPos.y = -1;
670 lpPos->ptMaxPos.x = lpPos->ptMaxPos.y = -1;
673 if( wnd->dwStyle & WS_MINIMIZE )
675 lpPos->ptIconPos.x = wnd->rectWindow.left;
676 lpPos->ptIconPos.y = wnd->rectWindow.top;
678 else if( wnd->dwStyle & WS_MAXIMIZE )
680 lpPos->ptMaxPos.x = wnd->rectWindow.left;
681 lpPos->ptMaxPos.y = wnd->rectWindow.top;
683 else
685 lpPos->rectNormal.left = wnd->rectWindow.left;
686 lpPos->rectNormal.top = wnd->rectWindow.top;
687 lpPos->rectNormal.right = wnd->rectWindow.right;
688 lpPos->rectNormal.bottom = wnd->rectWindow.bottom;
690 return lpPos;
693 /***********************************************************************
694 * WINPOS_RedrawIconTitle
696 BOOL WINPOS_RedrawIconTitle( HWND hWnd )
698 LPINTERNALPOS lpPos = get_internal_pos( hWnd );
699 if( lpPos )
701 if( lpPos->hwndIconTitle )
703 SendMessageW( lpPos->hwndIconTitle, WM_SHOWWINDOW, TRUE, 0);
704 InvalidateRect( lpPos->hwndIconTitle, NULL, TRUE );
705 return TRUE;
708 return FALSE;
711 /***********************************************************************
712 * WINPOS_ShowIconTitle
714 BOOL WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
716 LPINTERNALPOS lpPos = get_internal_pos( hwnd );
718 if (lpPos && !GetPropA( hwnd, "__wine_x11_managed" ))
720 HWND title = lpPos->hwndIconTitle;
722 TRACE("%p %i\n", hwnd, (bShow != 0) );
724 if( bShow )
726 if (!title) lpPos->hwndIconTitle = title = ICONTITLE_Create( hwnd );
727 if (!IsWindowVisible(title))
729 SendMessageW( title, WM_SHOWWINDOW, TRUE, 0 );
730 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
731 SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
734 else if (title) ShowWindow( title, SW_HIDE );
736 return FALSE;
739 /*******************************************************************
740 * WINPOS_GetMinMaxInfo
742 * Get the minimized and maximized information for a window.
744 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
745 POINT *minTrack, POINT *maxTrack )
747 LPINTERNALPOS lpPos;
748 MINMAXINFO MinMax;
749 HMONITOR monitor;
750 INT xinc, yinc;
751 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
752 LONG exstyle = GetWindowLongA( hwnd, GWL_EXSTYLE );
753 RECT rc;
755 /* Compute default values */
757 GetWindowRect(hwnd, &rc);
758 MinMax.ptReserved.x = rc.left;
759 MinMax.ptReserved.y = rc.top;
761 if (style & WS_CHILD)
763 if ((style & WS_CAPTION) == WS_CAPTION)
764 style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
766 GetClientRect(GetAncestor(hwnd,GA_PARENT), &rc);
767 AdjustWindowRectEx(&rc, style, ((style & WS_POPUP) && GetMenu(hwnd)), exstyle);
769 /* avoid calculating this twice */
770 style &= ~(WS_DLGFRAME | WS_BORDER | WS_THICKFRAME);
772 MinMax.ptMaxSize.x = rc.right - rc.left;
773 MinMax.ptMaxSize.y = rc.bottom - rc.top;
775 else
777 MinMax.ptMaxSize.x = GetSystemMetrics(SM_CXSCREEN);
778 MinMax.ptMaxSize.y = GetSystemMetrics(SM_CYSCREEN);
780 MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
781 MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
782 MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXMAXTRACK);
783 MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYMAXTRACK);
785 if (HAS_DLGFRAME( style, exstyle ))
787 xinc = GetSystemMetrics(SM_CXDLGFRAME);
788 yinc = GetSystemMetrics(SM_CYDLGFRAME);
790 else
792 xinc = yinc = 0;
793 if (HAS_THICKFRAME(style))
795 xinc += GetSystemMetrics(SM_CXFRAME);
796 yinc += GetSystemMetrics(SM_CYFRAME);
798 if (style & WS_BORDER)
800 xinc += GetSystemMetrics(SM_CXBORDER);
801 yinc += GetSystemMetrics(SM_CYBORDER);
804 MinMax.ptMaxSize.x += 2 * xinc;
805 MinMax.ptMaxSize.y += 2 * yinc;
807 lpPos = get_internal_pos( hwnd );
808 if( lpPos && !EMPTYPOINT(lpPos->ptMaxPos) )
810 MinMax.ptMaxPosition.x = lpPos->ptMaxPos.x;
811 MinMax.ptMaxPosition.y = lpPos->ptMaxPos.y;
813 else
815 MinMax.ptMaxPosition.x = -xinc;
816 MinMax.ptMaxPosition.y = -yinc;
819 SendMessageW( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
821 /* if the app didn't change the values, adapt them for the current monitor */
823 if ((monitor = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY )))
825 MONITORINFO mon_info;
827 mon_info.cbSize = sizeof(mon_info);
828 GetMonitorInfoW( monitor, &mon_info );
830 if (MinMax.ptMaxSize.x == GetSystemMetrics(SM_CXSCREEN) + 2 * xinc &&
831 MinMax.ptMaxSize.y == GetSystemMetrics(SM_CYSCREEN) + 2 * yinc)
833 MinMax.ptMaxSize.x = (mon_info.rcWork.right - mon_info.rcWork.left) + 2 * xinc;
834 MinMax.ptMaxSize.y = (mon_info.rcWork.bottom - mon_info.rcWork.top) + 2 * yinc;
836 if (MinMax.ptMaxPosition.x == -xinc && MinMax.ptMaxPosition.y == -yinc)
838 MinMax.ptMaxPosition.x = mon_info.rcWork.left - xinc;
839 MinMax.ptMaxPosition.y = mon_info.rcWork.top - yinc;
843 /* Some sanity checks */
845 TRACE("%d %d / %d %d / %d %d / %d %d\n",
846 MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
847 MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
848 MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
849 MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
850 MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
851 MinMax.ptMinTrackSize.x );
852 MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
853 MinMax.ptMinTrackSize.y );
855 if (maxSize) *maxSize = MinMax.ptMaxSize;
856 if (maxPos) *maxPos = MinMax.ptMaxPosition;
857 if (minTrack) *minTrack = MinMax.ptMinTrackSize;
858 if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
862 /***********************************************************************
863 * WINPOS_FindIconPos
865 * Find a suitable place for an iconic window.
867 static POINT WINPOS_FindIconPos( HWND hwnd, POINT pt )
869 RECT rect, rectParent;
870 HWND parent, child;
871 HRGN hrgn, tmp;
872 int xspacing, yspacing;
874 parent = GetAncestor( hwnd, GA_PARENT );
875 GetClientRect( parent, &rectParent );
876 if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
877 (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
878 return pt; /* The icon already has a suitable position */
880 xspacing = GetSystemMetrics(SM_CXICONSPACING);
881 yspacing = GetSystemMetrics(SM_CYICONSPACING);
883 /* Check if another icon already occupies this spot */
884 /* FIXME: this is completely inefficient */
886 hrgn = CreateRectRgn( 0, 0, 0, 0 );
887 tmp = CreateRectRgn( 0, 0, 0, 0 );
888 for (child = GetWindow( parent, GW_HWNDFIRST ); child; child = GetWindow( child, GW_HWNDNEXT ))
890 WND *childPtr;
891 if (child == hwnd) continue;
892 if ((GetWindowLongW( child, GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != (WS_VISIBLE|WS_MINIMIZE))
893 continue;
894 if (!(childPtr = WIN_GetPtr( child )) || childPtr == WND_OTHER_PROCESS)
895 continue;
896 SetRectRgn( tmp, childPtr->rectWindow.left, childPtr->rectWindow.top,
897 childPtr->rectWindow.right, childPtr->rectWindow.bottom );
898 CombineRgn( hrgn, hrgn, tmp, RGN_OR );
899 WIN_ReleasePtr( childPtr );
901 DeleteObject( tmp );
903 for (rect.bottom = rectParent.bottom; rect.bottom >= yspacing; rect.bottom -= yspacing)
905 for (rect.left = rectParent.left; rect.left <= rectParent.right - xspacing; rect.left += xspacing)
907 rect.right = rect.left + xspacing;
908 rect.top = rect.bottom - yspacing;
909 if (!RectInRegion( hrgn, &rect ))
911 /* No window was found, so it's OK for us */
912 pt.x = rect.left + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
913 pt.y = rect.top + (yspacing - GetSystemMetrics(SM_CYICON)) / 2;
914 DeleteObject( hrgn );
915 return pt;
919 DeleteObject( hrgn );
920 pt.x = pt.y = 0;
921 return pt;
925 /***********************************************************************
926 * WINPOS_MinMaximize
928 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
930 WND *wndPtr;
931 UINT swpFlags = 0;
932 POINT size;
933 LONG old_style;
934 WINDOWPLACEMENT wpl;
936 TRACE("%p %u\n", hwnd, cmd );
938 wpl.length = sizeof(wpl);
939 GetWindowPlacement( hwnd, &wpl );
941 if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
942 return SWP_NOSIZE | SWP_NOMOVE;
944 if (IsIconic( hwnd ))
946 switch (cmd)
948 case SW_SHOWMINNOACTIVE:
949 case SW_SHOWMINIMIZED:
950 case SW_FORCEMINIMIZE:
951 case SW_MINIMIZE:
952 return SWP_NOSIZE | SWP_NOMOVE;
954 if (!SendMessageW( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
955 swpFlags |= SWP_NOCOPYBITS;
958 switch( cmd )
960 case SW_SHOWMINNOACTIVE:
961 case SW_SHOWMINIMIZED:
962 case SW_FORCEMINIMIZE:
963 case SW_MINIMIZE:
964 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
965 if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
966 else wndPtr->flags &= ~WIN_RESTORE_MAX;
967 WIN_ReleasePtr( wndPtr );
969 old_style = WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
971 wpl.ptMinPosition = WINPOS_FindIconPos( hwnd, wpl.ptMinPosition );
973 if (!(old_style & WS_MINIMIZE)) swpFlags |= SWP_STATECHANGED;
974 SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
975 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON) );
976 swpFlags |= SWP_NOCOPYBITS;
977 break;
979 case SW_MAXIMIZE:
980 old_style = GetWindowLongW( hwnd, GWL_STYLE );
981 if ((old_style & WS_MAXIMIZE) && (old_style & WS_VISIBLE)) return SWP_NOSIZE | SWP_NOMOVE;
983 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
985 old_style = WIN_SetStyle( hwnd, WS_MAXIMIZE, WS_MINIMIZE );
986 if (old_style & WS_MINIMIZE) WINPOS_ShowIconTitle( hwnd, FALSE );
988 if (!(old_style & WS_MAXIMIZE)) swpFlags |= SWP_STATECHANGED;
989 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
990 break;
992 case SW_SHOWNOACTIVATE:
993 case SW_SHOWNORMAL:
994 case SW_RESTORE:
995 old_style = WIN_SetStyle( hwnd, 0, WS_MINIMIZE | WS_MAXIMIZE );
996 if (old_style & WS_MINIMIZE)
998 BOOL restore_max;
1000 WINPOS_ShowIconTitle( hwnd, FALSE );
1002 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
1003 restore_max = (wndPtr->flags & WIN_RESTORE_MAX) != 0;
1004 WIN_ReleasePtr( wndPtr );
1005 if (restore_max)
1007 /* Restore to maximized position */
1008 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
1009 WIN_SetStyle( hwnd, WS_MAXIMIZE, 0 );
1010 swpFlags |= SWP_STATECHANGED;
1011 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
1012 break;
1015 else if (!(old_style & WS_MAXIMIZE)) break;
1017 swpFlags |= SWP_STATECHANGED;
1019 /* Restore to normal position */
1021 *rect = wpl.rcNormalPosition;
1022 rect->right -= rect->left;
1023 rect->bottom -= rect->top;
1025 break;
1028 return swpFlags;
1032 /***********************************************************************
1033 * show_window
1035 * Implementation of ShowWindow and ShowWindowAsync.
1037 static BOOL show_window( HWND hwnd, INT cmd )
1039 WND *wndPtr;
1040 HWND parent;
1041 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1042 BOOL wasVisible = (style & WS_VISIBLE) != 0;
1043 BOOL showFlag = TRUE;
1044 RECT newPos = {0, 0, 0, 0};
1045 UINT swp = 0;
1047 TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);
1049 switch(cmd)
1051 case SW_HIDE:
1052 if (!wasVisible) return FALSE;
1053 showFlag = FALSE;
1054 swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1055 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1056 break;
1058 case SW_SHOWMINNOACTIVE:
1059 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1060 /* fall through */
1061 case SW_MINIMIZE:
1062 case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
1063 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1064 /* fall through */
1065 case SW_SHOWMINIMIZED:
1066 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1067 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
1068 if ((style & WS_MINIMIZE) && wasVisible) return TRUE;
1069 break;
1071 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
1072 if (!wasVisible) swp |= SWP_SHOWWINDOW;
1073 swp |= SWP_FRAMECHANGED;
1074 swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
1075 if ((style & WS_MAXIMIZE) && wasVisible) return TRUE;
1076 break;
1078 case SW_SHOWNA:
1079 swp |= SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1080 if (style & WS_CHILD) swp |= SWP_NOZORDER;
1081 break;
1082 case SW_SHOW:
1083 if (wasVisible) return TRUE;
1084 swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1085 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1086 break;
1088 case SW_SHOWNOACTIVATE:
1089 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1090 /* fall through */
1091 case SW_RESTORE:
1092 /* fall through */
1093 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
1094 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
1095 if (!wasVisible) swp |= SWP_SHOWWINDOW;
1096 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1098 swp |= SWP_FRAMECHANGED;
1099 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
1101 else
1103 if (wasVisible) return TRUE;
1104 swp |= SWP_NOSIZE | SWP_NOMOVE;
1106 if (style & WS_CHILD && !(swp & SWP_STATECHANGED)) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1107 break;
1108 default:
1109 return wasVisible;
1112 if ((showFlag != wasVisible || cmd == SW_SHOWNA) && cmd != SW_SHOWMAXIMIZED && !(swp & SWP_STATECHANGED))
1114 SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
1115 if (!IsWindow( hwnd )) return wasVisible;
1118 parent = GetAncestor( hwnd, GA_PARENT );
1119 if (parent && !IsWindowVisible( parent ) && !(swp & SWP_STATECHANGED))
1121 /* if parent is not visible simply toggle WS_VISIBLE and return */
1122 if (showFlag) WIN_SetStyle( hwnd, WS_VISIBLE, 0 );
1123 else WIN_SetStyle( hwnd, 0, WS_VISIBLE );
1125 else
1126 SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1127 newPos.right, newPos.bottom, LOWORD(swp) );
1129 if (cmd == SW_HIDE)
1131 HWND hFocus;
1133 WINPOS_ShowIconTitle( hwnd, FALSE );
1135 /* FIXME: This will cause the window to be activated irrespective
1136 * of whether it is owned by the same thread. Has to be done
1137 * asynchronously.
1140 if (hwnd == GetActiveWindow())
1141 WINPOS_ActivateOtherWindow(hwnd);
1143 /* Revert focus to parent */
1144 hFocus = GetFocus();
1145 if (hwnd == hFocus || IsChild(hwnd, hFocus))
1147 HWND parent = GetAncestor(hwnd, GA_PARENT);
1148 if (parent == GetDesktopWindow()) parent = 0;
1149 SetFocus(parent);
1153 if (IsIconic(hwnd)) WINPOS_ShowIconTitle( hwnd, TRUE );
1155 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return wasVisible;
1157 if (wndPtr->flags & WIN_NEED_SIZE)
1159 /* should happen only in CreateWindowEx() */
1160 int wParam = SIZE_RESTORED;
1161 RECT client = wndPtr->rectClient;
1163 wndPtr->flags &= ~WIN_NEED_SIZE;
1164 if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1165 else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
1166 WIN_ReleasePtr( wndPtr );
1168 SendMessageW( hwnd, WM_SIZE, wParam,
1169 MAKELONG( client.right - client.left, client.bottom - client.top ));
1170 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( client.left, client.top ));
1172 else WIN_ReleasePtr( wndPtr );
1174 /* if previous state was minimized Windows sets focus to the window */
1175 if (style & WS_MINIMIZE) SetFocus( hwnd );
1177 return wasVisible;
1181 /***********************************************************************
1182 * ShowWindowAsync (USER32.@)
1184 * doesn't wait; returns immediately.
1185 * used by threads to toggle windows in other (possibly hanging) threads
1187 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
1189 HWND full_handle;
1191 if (is_broadcast(hwnd))
1193 SetLastError( ERROR_INVALID_PARAMETER );
1194 return FALSE;
1197 if ((full_handle = WIN_IsCurrentThread( hwnd )))
1198 return show_window( full_handle, cmd );
1200 return SendNotifyMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
1204 /***********************************************************************
1205 * ShowWindow (USER32.@)
1207 BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
1209 HWND full_handle;
1211 if (is_broadcast(hwnd))
1213 SetLastError( ERROR_INVALID_PARAMETER );
1214 return FALSE;
1216 if ((full_handle = WIN_IsCurrentThread( hwnd )))
1217 return show_window( full_handle, cmd );
1219 return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
1223 /***********************************************************************
1224 * GetInternalWindowPos (USER32.@)
1226 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
1227 LPPOINT ptIcon )
1229 WINDOWPLACEMENT wndpl;
1230 if (GetWindowPlacement( hwnd, &wndpl ))
1232 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
1233 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
1234 return wndpl.showCmd;
1236 return 0;
1240 /***********************************************************************
1241 * GetWindowPlacement (USER32.@)
1243 * Win95:
1244 * Fails if wndpl->length of Win95 (!) apps is invalid.
1246 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
1248 WND *pWnd = WIN_GetPtr( hwnd );
1249 LPINTERNALPOS lpPos;
1251 if (!pWnd || pWnd == WND_DESKTOP) return FALSE;
1252 if (pWnd == WND_OTHER_PROCESS)
1254 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
1255 return FALSE;
1258 lpPos = WINPOS_InitInternalPos( pWnd );
1259 wndpl->length = sizeof(*wndpl);
1260 if( pWnd->dwStyle & WS_MINIMIZE )
1261 wndpl->showCmd = SW_SHOWMINIMIZED;
1262 else
1263 wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
1264 if( pWnd->flags & WIN_RESTORE_MAX )
1265 wndpl->flags = WPF_RESTORETOMAXIMIZED;
1266 else
1267 wndpl->flags = 0;
1268 wndpl->ptMinPosition.x = lpPos->ptIconPos.x;
1269 wndpl->ptMinPosition.y = lpPos->ptIconPos.y;
1270 wndpl->ptMaxPosition.x = lpPos->ptMaxPos.x;
1271 wndpl->ptMaxPosition.y = lpPos->ptMaxPos.y;
1272 wndpl->rcNormalPosition.left = lpPos->rectNormal.left;
1273 wndpl->rcNormalPosition.top = lpPos->rectNormal.top;
1274 wndpl->rcNormalPosition.right = lpPos->rectNormal.right;
1275 wndpl->rcNormalPosition.bottom = lpPos->rectNormal.bottom;
1276 WIN_ReleasePtr( pWnd );
1277 return TRUE;
1281 /***********************************************************************
1282 * WINPOS_SetPlacement
1284 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
1286 LPINTERNALPOS lpPos;
1287 DWORD style;
1288 WND *pWnd = WIN_GetPtr( hwnd );
1290 if (!pWnd || pWnd == WND_OTHER_PROCESS || pWnd == WND_DESKTOP) return FALSE;
1291 lpPos = WINPOS_InitInternalPos( pWnd );
1293 if( flags & PLACE_MIN )
1295 lpPos->ptIconPos.x = wndpl->ptMinPosition.x;
1296 lpPos->ptIconPos.y = wndpl->ptMinPosition.y;
1298 if( flags & PLACE_MAX )
1300 lpPos->ptMaxPos.x = wndpl->ptMaxPosition.x;
1301 lpPos->ptMaxPos.y = wndpl->ptMaxPosition.y;
1303 if( flags & PLACE_RECT)
1305 lpPos->rectNormal.left = wndpl->rcNormalPosition.left;
1306 lpPos->rectNormal.top = wndpl->rcNormalPosition.top;
1307 lpPos->rectNormal.right = wndpl->rcNormalPosition.right;
1308 lpPos->rectNormal.bottom = wndpl->rcNormalPosition.bottom;
1311 style = pWnd->dwStyle;
1312 WIN_ReleasePtr( pWnd );
1314 if( style & WS_MINIMIZE )
1316 WINPOS_ShowIconTitle( hwnd, FALSE );
1317 if( wndpl->flags & WPF_SETMINPOSITION && !EMPTYPOINT(lpPos->ptIconPos))
1318 SetWindowPos( hwnd, 0, lpPos->ptIconPos.x, lpPos->ptIconPos.y,
1319 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1321 else if( style & WS_MAXIMIZE )
1323 if( !EMPTYPOINT(lpPos->ptMaxPos) )
1324 SetWindowPos( hwnd, 0, lpPos->ptMaxPos.x, lpPos->ptMaxPos.y,
1325 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1327 else if( flags & PLACE_RECT )
1328 SetWindowPos( hwnd, 0, lpPos->rectNormal.left, lpPos->rectNormal.top,
1329 lpPos->rectNormal.right - lpPos->rectNormal.left,
1330 lpPos->rectNormal.bottom - lpPos->rectNormal.top,
1331 SWP_NOZORDER | SWP_NOACTIVATE );
1333 ShowWindow( hwnd, wndpl->showCmd );
1335 if (IsIconic( hwnd ))
1337 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE) WINPOS_ShowIconTitle( hwnd, TRUE );
1339 /* SDK: ...valid only the next time... */
1340 if( wndpl->flags & WPF_RESTORETOMAXIMIZED )
1342 pWnd = WIN_GetPtr( hwnd );
1343 if (pWnd && pWnd != WND_OTHER_PROCESS)
1345 pWnd->flags |= WIN_RESTORE_MAX;
1346 WIN_ReleasePtr( pWnd );
1350 return TRUE;
1354 /***********************************************************************
1355 * SetWindowPlacement (USER32.@)
1357 * Win95:
1358 * Fails if wndpl->length of Win95 (!) apps is invalid.
1360 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
1362 if (!wpl) return FALSE;
1363 return WINPOS_SetPlacement( hwnd, wpl, PLACE_MIN | PLACE_MAX | PLACE_RECT );
1367 /***********************************************************************
1368 * AnimateWindow (USER32.@)
1369 * Shows/Hides a window with an animation
1370 * NO ANIMATION YET
1372 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
1374 FIXME("partial stub\n");
1376 /* If trying to show/hide and it's already *
1377 * shown/hidden or invalid window, fail with *
1378 * invalid parameter */
1379 if(!IsWindow(hwnd) ||
1380 (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
1381 (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
1383 SetLastError(ERROR_INVALID_PARAMETER);
1384 return FALSE;
1387 ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1389 return TRUE;
1392 /***********************************************************************
1393 * SetInternalWindowPos (USER32.@)
1395 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1396 LPRECT rect, LPPOINT pt )
1398 if( IsWindow(hwnd) )
1400 WINDOWPLACEMENT wndpl;
1401 UINT flags;
1403 wndpl.length = sizeof(wndpl);
1404 wndpl.showCmd = showCmd;
1405 wndpl.flags = flags = 0;
1407 if( pt )
1409 flags |= PLACE_MIN;
1410 wndpl.flags |= WPF_SETMINPOSITION;
1411 wndpl.ptMinPosition = *pt;
1413 if( rect )
1415 flags |= PLACE_RECT;
1416 wndpl.rcNormalPosition = *rect;
1418 WINPOS_SetPlacement( hwnd, &wndpl, flags );
1423 /*******************************************************************
1424 * can_activate_window
1426 * Check if we can activate the specified window.
1428 static BOOL can_activate_window( HWND hwnd )
1430 LONG style;
1432 if (!hwnd) return FALSE;
1433 style = GetWindowLongW( hwnd, GWL_STYLE );
1434 if (!(style & WS_VISIBLE)) return FALSE;
1435 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1436 return !(style & WS_DISABLED);
1440 /*******************************************************************
1441 * WINPOS_ActivateOtherWindow
1443 * Activates window other than pWnd.
1445 void WINPOS_ActivateOtherWindow(HWND hwnd)
1447 HWND hwndTo, fg;
1449 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1451 hwndTo = GetAncestor( hwndTo, GA_ROOT );
1452 if (can_activate_window( hwndTo )) goto done;
1455 hwndTo = hwnd;
1456 for (;;)
1458 if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1459 if (can_activate_window( hwndTo )) break;
1462 done:
1463 fg = GetForegroundWindow();
1464 TRACE("win = %p fg = %p\n", hwndTo, fg);
1465 if (!fg || (hwnd == fg))
1467 if (SetForegroundWindow( hwndTo )) return;
1469 if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1473 /***********************************************************************
1474 * WINPOS_HandleWindowPosChanging
1476 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1478 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1480 POINT minTrack, maxTrack;
1481 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1483 if (winpos->flags & SWP_NOSIZE) return 0;
1484 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1486 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1487 if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1488 if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1489 if (!(style & WS_MINIMIZE))
1491 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1492 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1495 return 0;
1499 /***********************************************************************
1500 * dump_winpos_flags
1502 static void dump_winpos_flags(UINT flags)
1504 TRACE("flags:");
1505 if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1506 if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1507 if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1508 if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1509 if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1510 if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1511 if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1512 if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1513 if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1514 if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1515 if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1516 if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1517 if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1519 #define DUMPED_FLAGS \
1520 (SWP_NOSIZE | \
1521 SWP_NOMOVE | \
1522 SWP_NOZORDER | \
1523 SWP_NOREDRAW | \
1524 SWP_NOACTIVATE | \
1525 SWP_FRAMECHANGED | \
1526 SWP_SHOWWINDOW | \
1527 SWP_HIDEWINDOW | \
1528 SWP_NOCOPYBITS | \
1529 SWP_NOOWNERZORDER | \
1530 SWP_NOSENDCHANGING | \
1531 SWP_DEFERERASE | \
1532 SWP_ASYNCWINDOWPOS)
1534 if(flags & ~DUMPED_FLAGS) TRACE(" %08x", flags & ~DUMPED_FLAGS);
1535 TRACE("\n");
1536 #undef DUMPED_FLAGS
1539 /***********************************************************************
1540 * SWP_DoWinPosChanging
1542 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
1544 WND *wndPtr;
1546 /* Send WM_WINDOWPOSCHANGING message */
1548 if (!(pWinpos->flags & SWP_NOSENDCHANGING))
1549 SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
1551 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) ||
1552 wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
1554 /* Calculate new position and size */
1556 *pNewWindowRect = wndPtr->rectWindow;
1557 *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
1558 : wndPtr->rectClient;
1560 if (!(pWinpos->flags & SWP_NOSIZE))
1562 pNewWindowRect->right = pNewWindowRect->left + pWinpos->cx;
1563 pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
1565 if (!(pWinpos->flags & SWP_NOMOVE))
1567 pNewWindowRect->left = pWinpos->x;
1568 pNewWindowRect->top = pWinpos->y;
1569 pNewWindowRect->right += pWinpos->x - wndPtr->rectWindow.left;
1570 pNewWindowRect->bottom += pWinpos->y - wndPtr->rectWindow.top;
1572 OffsetRect( pNewClientRect, pWinpos->x - wndPtr->rectWindow.left,
1573 pWinpos->y - wndPtr->rectWindow.top );
1575 pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
1577 TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
1578 pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
1579 pWinpos->cx, pWinpos->cy, pWinpos->flags );
1580 TRACE( "current %s style %08x new %s\n",
1581 wine_dbgstr_rect( &wndPtr->rectWindow ), wndPtr->dwStyle,
1582 wine_dbgstr_rect( pNewWindowRect ));
1584 WIN_ReleasePtr( wndPtr );
1585 return TRUE;
1588 /***********************************************************************
1589 * get_valid_rects
1591 * Compute the valid rects from the old and new client rect and WVR_* flags.
1592 * Helper for WM_NCCALCSIZE handling.
1594 static inline void get_valid_rects( const RECT *old_client, const RECT *new_client, UINT flags,
1595 RECT *valid )
1597 int cx, cy;
1599 if (flags & WVR_REDRAW)
1601 SetRectEmpty( &valid[0] );
1602 SetRectEmpty( &valid[1] );
1603 return;
1606 if (flags & WVR_VALIDRECTS)
1608 if (!IntersectRect( &valid[0], &valid[0], new_client ) ||
1609 !IntersectRect( &valid[1], &valid[1], old_client ))
1611 SetRectEmpty( &valid[0] );
1612 SetRectEmpty( &valid[1] );
1613 return;
1615 flags = WVR_ALIGNLEFT | WVR_ALIGNTOP;
1617 else
1619 valid[0] = *new_client;
1620 valid[1] = *old_client;
1623 /* make sure the rectangles have the same size */
1624 cx = min( valid[0].right - valid[0].left, valid[1].right - valid[1].left );
1625 cy = min( valid[0].bottom - valid[0].top, valid[1].bottom - valid[1].top );
1627 if (flags & WVR_ALIGNBOTTOM)
1629 valid[0].top = valid[0].bottom - cy;
1630 valid[1].top = valid[1].bottom - cy;
1632 else
1634 valid[0].bottom = valid[0].top + cy;
1635 valid[1].bottom = valid[1].top + cy;
1637 if (flags & WVR_ALIGNRIGHT)
1639 valid[0].left = valid[0].right - cx;
1640 valid[1].left = valid[1].right - cx;
1642 else
1644 valid[0].right = valid[0].left + cx;
1645 valid[1].right = valid[1].left + cx;
1650 /***********************************************************************
1651 * SWP_DoOwnedPopups
1653 * fix Z order taking into account owned popups -
1654 * basically we need to maintain them above the window that owns them
1656 * FIXME: hide/show owned popups when owner visibility changes.
1658 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
1660 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1661 HWND owner, *list = NULL;
1662 unsigned int i;
1664 TRACE("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
1666 if ((style & WS_POPUP) && (owner = GetWindow( hwnd, GW_OWNER )))
1668 /* make sure this popup stays above the owner */
1670 if (hwndInsertAfter != HWND_TOP && hwndInsertAfter != HWND_TOPMOST)
1672 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return hwndInsertAfter;
1674 for (i = 0; list[i]; i++)
1676 if (list[i] == owner)
1678 if (i > 0) hwndInsertAfter = list[i-1];
1679 else hwndInsertAfter = HWND_TOP;
1680 break;
1683 if (hwndInsertAfter == HWND_NOTOPMOST)
1685 if (!(GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST)) break;
1687 else if (list[i] == hwndInsertAfter) break;
1691 else if (style & WS_CHILD) return hwndInsertAfter;
1693 if (hwndInsertAfter == HWND_BOTTOM) goto done;
1694 if (!list && !(list = WIN_ListChildren( GetDesktopWindow() ))) goto done;
1696 i = 0;
1697 if (hwndInsertAfter == HWND_TOP || hwndInsertAfter == HWND_NOTOPMOST)
1699 if (hwndInsertAfter == HWND_NOTOPMOST || !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_TOPMOST))
1701 /* skip all the topmost windows */
1702 while (list[i] && (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST)) i++;
1705 else if (hwndInsertAfter != HWND_TOPMOST)
1707 /* skip windows that are already placed correctly */
1708 for (i = 0; list[i]; i++)
1710 if (list[i] == hwndInsertAfter) break;
1711 if (list[i] == hwnd) goto done; /* nothing to do if window is moving backwards in z-order */
1715 for ( ; list[i]; i++)
1717 if (list[i] == hwnd) break;
1718 if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_POPUP)) continue;
1719 if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
1720 TRACE( "moving %p owned by %p after %p\n", list[i], hwnd, hwndInsertAfter );
1721 SetWindowPos( list[i], hwndInsertAfter, 0, 0, 0, 0,
1722 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING | SWP_DEFERERASE );
1723 hwndInsertAfter = list[i];
1726 done:
1727 HeapFree( GetProcessHeap(), 0, list );
1728 return hwndInsertAfter;
1731 /***********************************************************************
1732 * SWP_DoNCCalcSize
1734 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect,
1735 RECT *validRects )
1737 UINT wvrFlags = 0;
1738 WND *wndPtr;
1740 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
1742 /* Send WM_NCCALCSIZE message to get new client area */
1743 if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
1745 NCCALCSIZE_PARAMS params;
1746 WINDOWPOS winposCopy;
1748 params.rgrc[0] = *pNewWindowRect;
1749 params.rgrc[1] = wndPtr->rectWindow;
1750 params.rgrc[2] = wndPtr->rectClient;
1751 params.lppos = &winposCopy;
1752 winposCopy = *pWinpos;
1753 WIN_ReleasePtr( wndPtr );
1755 wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
1757 *pNewClientRect = params.rgrc[0];
1759 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
1761 TRACE( "hwnd %p old win %s old client %s new win %s new client %s\n", pWinpos->hwnd,
1762 wine_dbgstr_rect(&wndPtr->rectWindow), wine_dbgstr_rect(&wndPtr->rectClient),
1763 wine_dbgstr_rect(pNewWindowRect), wine_dbgstr_rect(pNewClientRect) );
1765 if( pNewClientRect->left != wndPtr->rectClient.left ||
1766 pNewClientRect->top != wndPtr->rectClient.top )
1767 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1769 if( (pNewClientRect->right - pNewClientRect->left !=
1770 wndPtr->rectClient.right - wndPtr->rectClient.left))
1771 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1772 else
1773 wvrFlags &= ~WVR_HREDRAW;
1775 if (pNewClientRect->bottom - pNewClientRect->top !=
1776 wndPtr->rectClient.bottom - wndPtr->rectClient.top)
1777 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1778 else
1779 wvrFlags &= ~WVR_VREDRAW;
1781 validRects[0] = params.rgrc[1];
1782 validRects[1] = params.rgrc[2];
1784 else
1786 if (!(pWinpos->flags & SWP_NOMOVE) &&
1787 (pNewClientRect->left != wndPtr->rectClient.left ||
1788 pNewClientRect->top != wndPtr->rectClient.top))
1789 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1792 if (pWinpos->flags & (SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
1794 SetRectEmpty( &validRects[0] );
1795 SetRectEmpty( &validRects[1] );
1797 else get_valid_rects( &wndPtr->rectClient, pNewClientRect, wvrFlags, validRects );
1799 WIN_ReleasePtr( wndPtr );
1800 return wvrFlags;
1803 /* fix redundant flags and values in the WINDOWPOS structure */
1804 static BOOL fixup_flags( WINDOWPOS *winpos )
1806 HWND parent;
1807 WND *wndPtr = WIN_GetPtr( winpos->hwnd );
1808 BOOL ret = TRUE;
1810 if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
1812 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1813 return FALSE;
1815 winpos->hwnd = wndPtr->hwndSelf; /* make it a full handle */
1817 /* Finally make sure that all coordinates are valid */
1818 if (winpos->x < -32768) winpos->x = -32768;
1819 else if (winpos->x > 32767) winpos->x = 32767;
1820 if (winpos->y < -32768) winpos->y = -32768;
1821 else if (winpos->y > 32767) winpos->y = 32767;
1823 if (winpos->cx < 0) winpos->cx = 0;
1824 else if (winpos->cx > 32767) winpos->cx = 32767;
1825 if (winpos->cy < 0) winpos->cy = 0;
1826 else if (winpos->cy > 32767) winpos->cy = 32767;
1828 parent = GetAncestor( winpos->hwnd, GA_PARENT );
1829 if (!IsWindowVisible( parent )) winpos->flags |= SWP_NOREDRAW;
1831 if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
1832 else
1834 winpos->flags &= ~SWP_HIDEWINDOW;
1835 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
1838 if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
1839 (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
1840 winpos->flags |= SWP_NOSIZE; /* Already the right size */
1842 if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
1843 winpos->flags |= SWP_NOMOVE; /* Already the right position */
1845 if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
1847 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)) && /* Bring to the top when activating */
1848 (winpos->flags & SWP_NOZORDER ||
1849 (winpos->hwndInsertAfter != HWND_TOPMOST && winpos->hwndInsertAfter != HWND_NOTOPMOST)))
1851 winpos->flags &= ~SWP_NOZORDER;
1852 winpos->hwndInsertAfter = HWND_TOP;
1856 /* Check hwndInsertAfter */
1857 if (winpos->flags & SWP_NOZORDER) goto done;
1859 /* fix sign extension */
1860 if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
1861 else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
1863 /* hwndInsertAfter must be a sibling of the window */
1864 if (winpos->hwndInsertAfter == HWND_TOP)
1866 if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1867 winpos->flags |= SWP_NOZORDER;
1869 else if (winpos->hwndInsertAfter == HWND_BOTTOM)
1871 if (!(wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
1872 winpos->flags |= SWP_NOZORDER;
1874 else if (winpos->hwndInsertAfter == HWND_TOPMOST)
1876 if ((wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1877 winpos->flags |= SWP_NOZORDER;
1879 else if (winpos->hwndInsertAfter == HWND_NOTOPMOST)
1881 if (!(wndPtr->dwExStyle & WS_EX_TOPMOST))
1882 winpos->flags |= SWP_NOZORDER;
1884 else
1886 if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != parent) ret = FALSE;
1887 else
1889 /* don't need to change the Zorder of hwnd if it's already inserted
1890 * after hwndInsertAfter or when inserting hwnd after itself.
1892 if ((winpos->hwnd == winpos->hwndInsertAfter) ||
1893 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
1894 winpos->flags |= SWP_NOZORDER;
1897 done:
1898 WIN_ReleasePtr( wndPtr );
1899 return ret;
1903 /***********************************************************************
1904 * set_window_pos
1906 * Backend implementation of SetWindowPos.
1908 BOOL set_window_pos( HWND hwnd, HWND insert_after, UINT swp_flags,
1909 const RECT *window_rect, const RECT *client_rect, const RECT *valid_rects )
1911 WND *win;
1912 BOOL ret;
1913 RECT visible_rect, old_window_rect;
1914 DWORD new_style;
1916 if (!(win = WIN_GetPtr( hwnd ))) return FALSE;
1917 if (win == WND_DESKTOP || win == WND_OTHER_PROCESS) return FALSE;
1919 old_window_rect = win->rectWindow;
1920 SERVER_START_REQ( set_window_pos )
1922 req->handle = hwnd;
1923 req->previous = insert_after;
1924 req->flags = swp_flags;
1925 req->window.left = window_rect->left;
1926 req->window.top = window_rect->top;
1927 req->window.right = window_rect->right;
1928 req->window.bottom = window_rect->bottom;
1929 req->client.left = client_rect->left;
1930 req->client.top = client_rect->top;
1931 req->client.right = client_rect->right;
1932 req->client.bottom = client_rect->bottom;
1933 if (!IsRectEmpty( &valid_rects[0] ))
1934 wine_server_add_data( req, valid_rects, 2 * sizeof(*valid_rects) );
1935 if ((ret = !wine_server_call( req )))
1937 win->dwStyle = reply->new_style;
1938 win->dwExStyle = reply->new_ex_style;
1939 win->rectWindow = *window_rect;
1940 win->rectClient = *client_rect;
1941 visible_rect.left = reply->visible.left;
1942 visible_rect.top = reply->visible.top;
1943 visible_rect.right = reply->visible.right;
1944 visible_rect.bottom = reply->visible.bottom;
1947 SERVER_END_REQ;
1948 new_style = win->dwStyle;
1949 WIN_ReleasePtr( win );
1951 if (ret)
1953 USER_Driver->pSetWindowPos( hwnd, insert_after, swp_flags, window_rect,
1954 client_rect, &visible_rect, valid_rects );
1956 if ((((swp_flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) && (new_style & WS_VISIBLE)) ||
1957 (swp_flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW)))
1958 invalidate_dce( hwnd, &old_window_rect );
1960 return ret;
1964 /***********************************************************************
1965 * USER_SetWindowPos
1967 * User32 internal function
1969 BOOL USER_SetWindowPos( WINDOWPOS * winpos )
1971 RECT newWindowRect, newClientRect, valid_rects[2];
1972 UINT orig_flags;
1974 orig_flags = winpos->flags;
1976 /* First make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
1977 if (!(winpos->flags & SWP_NOMOVE))
1979 if (winpos->x < -32768) winpos->x = -32768;
1980 else if (winpos->x > 32767) winpos->x = 32767;
1981 if (winpos->y < -32768) winpos->y = -32768;
1982 else if (winpos->y > 32767) winpos->y = 32767;
1984 if (!(winpos->flags & SWP_NOSIZE))
1986 if (winpos->cx < 0) winpos->cx = 0;
1987 else if (winpos->cx > 32767) winpos->cx = 32767;
1988 if (winpos->cy < 0) winpos->cy = 0;
1989 else if (winpos->cy > 32767) winpos->cy = 32767;
1992 if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
1994 /* Fix redundant flags */
1995 if (!fixup_flags( winpos )) return FALSE;
1997 if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
1999 if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
2000 winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
2003 /* Common operations */
2005 SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect, valid_rects );
2007 if (!set_window_pos( winpos->hwnd, winpos->hwndInsertAfter, winpos->flags,
2008 &newWindowRect, &newClientRect, valid_rects ))
2009 return FALSE;
2011 /* erase parent when hiding or resizing child */
2012 if (!(orig_flags & SWP_DEFERERASE) &&
2013 ((orig_flags & SWP_HIDEWINDOW) ||
2014 (!(orig_flags & SWP_SHOWWINDOW) &&
2015 (winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOGEOMETRYCHANGE)))
2017 HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
2018 if (!parent || parent == GetDesktopWindow()) parent = winpos->hwnd;
2019 erase_now( parent, 0 );
2022 if( winpos->flags & SWP_HIDEWINDOW )
2023 HideCaret(winpos->hwnd);
2024 else if (winpos->flags & SWP_SHOWWINDOW)
2025 ShowCaret(winpos->hwnd);
2027 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)))
2029 /* child windows get WM_CHILDACTIVATE message */
2030 if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
2031 SendMessageW( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
2032 else
2033 SetForegroundWindow( winpos->hwnd );
2036 /* And last, send the WM_WINDOWPOSCHANGED message */
2038 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
2040 if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
2042 /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
2043 and always contains final window position.
2045 winpos->x = newWindowRect.left;
2046 winpos->y = newWindowRect.top;
2047 winpos->cx = newWindowRect.right - newWindowRect.left;
2048 winpos->cy = newWindowRect.bottom - newWindowRect.top;
2049 SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
2051 return TRUE;
2054 /***********************************************************************
2055 * SetWindowPos (USER32.@)
2057 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
2058 INT x, INT y, INT cx, INT cy, UINT flags )
2060 WINDOWPOS winpos;
2062 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2063 hwnd, hwndInsertAfter, x, y, cx, cy, flags);
2064 if(TRACE_ON(win)) dump_winpos_flags(flags);
2066 if (is_broadcast(hwnd))
2068 SetLastError( ERROR_INVALID_PARAMETER );
2069 return FALSE;
2072 winpos.hwnd = WIN_GetFullHandle(hwnd);
2073 winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
2074 winpos.x = x;
2075 winpos.y = y;
2076 winpos.cx = cx;
2077 winpos.cy = cy;
2078 winpos.flags = flags;
2080 if (WIN_IsCurrentThread( hwnd ))
2081 return USER_SetWindowPos(&winpos);
2083 return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
2087 /***********************************************************************
2088 * BeginDeferWindowPos (USER32.@)
2090 HDWP WINAPI BeginDeferWindowPos( INT count )
2092 HDWP handle;
2093 DWP *pDWP;
2095 TRACE("%d\n", count);
2097 if (count < 0)
2099 SetLastError(ERROR_INVALID_PARAMETER);
2100 return 0;
2102 /* Windows allows zero count, in which case it allocates context for 8 moves */
2103 if (count == 0) count = 8;
2105 handle = USER_HEAP_ALLOC( sizeof(DWP) + (count-1)*sizeof(WINDOWPOS) );
2106 if (!handle) return 0;
2107 pDWP = (DWP *) USER_HEAP_LIN_ADDR( handle );
2108 pDWP->actualCount = 0;
2109 pDWP->suggestedCount = count;
2110 pDWP->valid = TRUE;
2111 pDWP->wMagic = DWP_MAGIC;
2112 pDWP->hwndParent = 0;
2114 TRACE("returning hdwp %p\n", handle);
2115 return handle;
2119 /***********************************************************************
2120 * DeferWindowPos (USER32.@)
2122 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
2123 INT x, INT y, INT cx, INT cy,
2124 UINT flags )
2126 DWP *pDWP;
2127 int i;
2128 HDWP newhdwp = hdwp,retvalue;
2130 TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2131 hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
2133 hwnd = WIN_GetFullHandle( hwnd );
2134 if (hwnd == GetDesktopWindow()) return 0;
2136 if (!(pDWP = USER_HEAP_LIN_ADDR( hdwp ))) return 0;
2138 USER_Lock();
2140 for (i = 0; i < pDWP->actualCount; i++)
2142 if (pDWP->winPos[i].hwnd == hwnd)
2144 /* Merge with the other changes */
2145 if (!(flags & SWP_NOZORDER))
2147 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
2149 if (!(flags & SWP_NOMOVE))
2151 pDWP->winPos[i].x = x;
2152 pDWP->winPos[i].y = y;
2154 if (!(flags & SWP_NOSIZE))
2156 pDWP->winPos[i].cx = cx;
2157 pDWP->winPos[i].cy = cy;
2159 pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
2160 SWP_NOZORDER | SWP_NOREDRAW |
2161 SWP_NOACTIVATE | SWP_NOCOPYBITS|
2162 SWP_NOOWNERZORDER);
2163 pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
2164 SWP_FRAMECHANGED);
2165 retvalue = hdwp;
2166 goto END;
2169 if (pDWP->actualCount >= pDWP->suggestedCount)
2171 newhdwp = USER_HEAP_REALLOC( hdwp,
2172 sizeof(DWP) + pDWP->suggestedCount*sizeof(WINDOWPOS) );
2173 if (!newhdwp)
2175 retvalue = 0;
2176 goto END;
2178 pDWP = (DWP *) USER_HEAP_LIN_ADDR( newhdwp );
2179 pDWP->suggestedCount++;
2181 pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
2182 pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
2183 pDWP->winPos[pDWP->actualCount].x = x;
2184 pDWP->winPos[pDWP->actualCount].y = y;
2185 pDWP->winPos[pDWP->actualCount].cx = cx;
2186 pDWP->winPos[pDWP->actualCount].cy = cy;
2187 pDWP->winPos[pDWP->actualCount].flags = flags;
2188 pDWP->actualCount++;
2189 retvalue = newhdwp;
2190 END:
2191 USER_Unlock();
2192 return retvalue;
2196 /***********************************************************************
2197 * EndDeferWindowPos (USER32.@)
2199 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
2201 DWP *pDWP;
2202 WINDOWPOS *winpos;
2203 BOOL res = TRUE;
2204 int i;
2206 TRACE("%p\n", hdwp);
2208 pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
2209 if (!pDWP) return FALSE;
2210 for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
2212 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2213 winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
2214 winpos->cx, winpos->cy, winpos->flags);
2216 if (!(res = USER_SetWindowPos( winpos ))) break;
2218 USER_HEAP_FREE( hdwp );
2219 return res;
2223 /***********************************************************************
2224 * TileChildWindows (USER.199)
2226 void WINAPI TileChildWindows16( HWND16 parent, WORD action )
2228 FIXME("(%04x, %d): stub\n", parent, action);
2231 /***********************************************************************
2232 * CascadeChildWindows (USER.198)
2234 void WINAPI CascadeChildWindows16( HWND16 parent, WORD action )
2236 FIXME("(%04x, %d): stub\n", parent, action);