oleaut32/tests: Fix a few typos.
[wine/multimedia.git] / dlls / user32 / winpos.c
bloba13753c7461d5846299403fafeecd7387f7c0256
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_NOGEOMETRYCHANGE \
44 (SWP_NOSIZE | SWP_NOCLIENTSIZE | SWP_NOZORDER)
45 #define SWP_AGG_NOPOSCHANGE \
46 (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
47 #define SWP_AGG_STATUSFLAGS \
48 (SWP_AGG_NOPOSCHANGE | SWP_FRAMECHANGED | SWP_HIDEWINDOW | SWP_SHOWWINDOW)
50 #define HAS_DLGFRAME(style,exStyle) \
51 (((exStyle) & WS_EX_DLGMODALFRAME) || \
52 (((style) & WS_DLGFRAME) && !((style) & WS_BORDER)))
54 #define HAS_THICKFRAME(style) \
55 (((style) & WS_THICKFRAME) && \
56 !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
58 #define EMPTYPOINT(pt) ((*(LONG*)&(pt)) == -1)
60 #define PLACE_MIN 0x0001
61 #define PLACE_MAX 0x0002
62 #define PLACE_RECT 0x0004
65 #define DWP_MAGIC ((INT)('W' | ('P' << 8) | ('O' << 16) | ('S' << 24)))
67 typedef struct
69 INT actualCount;
70 INT suggestedCount;
71 BOOL valid;
72 INT wMagic;
73 HWND hwndParent;
74 WINDOWPOS winPos[1];
75 } DWP;
77 typedef struct
79 RECT16 rectNormal;
80 POINT16 ptIconPos;
81 POINT16 ptMaxPos;
82 HWND hwndIconTitle;
83 } INTERNALPOS, *LPINTERNALPOS;
85 /* ----- internal functions ----- */
87 static const WCHAR SysIP_W[] = { 'S','y','s','I','P',0 };
89 static inline INTERNALPOS *get_internal_pos( HWND hwnd )
91 return GetPropW( hwnd, SysIP_W );
94 static inline void set_internal_pos( HWND hwnd, INTERNALPOS *pos )
96 SetPropW( hwnd, SysIP_W, pos );
99 /***********************************************************************
100 * WINPOS_CheckInternalPos
102 * Called when a window is destroyed.
104 void WINPOS_CheckInternalPos( HWND hwnd )
106 LPINTERNALPOS lpPos = get_internal_pos( hwnd );
108 if ( lpPos )
110 if( IsWindow(lpPos->hwndIconTitle) )
111 DestroyWindow( lpPos->hwndIconTitle );
112 HeapFree( GetProcessHeap(), 0, lpPos );
116 /***********************************************************************
117 * ArrangeIconicWindows (USER32.@)
119 UINT WINAPI ArrangeIconicWindows( HWND parent )
121 RECT rectParent;
122 HWND hwndChild;
123 INT x, y, xspacing, yspacing;
125 GetClientRect( parent, &rectParent );
126 x = rectParent.left;
127 y = rectParent.bottom;
128 xspacing = GetSystemMetrics(SM_CXICONSPACING);
129 yspacing = GetSystemMetrics(SM_CYICONSPACING);
131 hwndChild = GetWindow( parent, GW_CHILD );
132 while (hwndChild)
134 if( IsIconic( hwndChild ) )
136 WINPOS_ShowIconTitle( hwndChild, FALSE );
138 SetWindowPos( hwndChild, 0, x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
139 y - yspacing - GetSystemMetrics(SM_CYICON)/2, 0, 0,
140 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
141 if( IsWindow(hwndChild) )
142 WINPOS_ShowIconTitle(hwndChild , TRUE );
144 if (x <= rectParent.right - xspacing) x += xspacing;
145 else
147 x = rectParent.left;
148 y -= yspacing;
151 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
153 return yspacing;
157 /***********************************************************************
158 * SwitchToThisWindow (USER32.@)
160 void WINAPI SwitchToThisWindow( HWND hwnd, BOOL restore )
162 ShowWindow( hwnd, restore ? SW_RESTORE : SW_SHOWMINIMIZED );
166 /***********************************************************************
167 * GetWindowRect (USER32.@)
169 BOOL WINAPI GetWindowRect( HWND hwnd, LPRECT rect )
171 BOOL ret = WIN_GetRectangles( hwnd, rect, NULL );
172 if (ret)
174 MapWindowPoints( GetAncestor( hwnd, GA_PARENT ), 0, (POINT *)rect, 2 );
175 TRACE( "hwnd %p (%d,%d)-(%d,%d)\n",
176 hwnd, rect->left, rect->top, rect->right, rect->bottom);
178 return ret;
182 /***********************************************************************
183 * GetWindowRgn (USER32.@)
185 int WINAPI GetWindowRgn ( HWND hwnd, HRGN hrgn )
187 int nRet = ERROR;
188 NTSTATUS status;
189 HRGN win_rgn = 0;
190 RGNDATA *data;
191 size_t size = 256;
195 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
197 SetLastError( ERROR_OUTOFMEMORY );
198 return ERROR;
200 SERVER_START_REQ( get_window_region )
202 req->window = hwnd;
203 wine_server_set_reply( req, data->Buffer, size );
204 if (!(status = wine_server_call( req )))
206 size_t reply_size = wine_server_reply_size( reply );
207 if (reply_size)
209 data->rdh.dwSize = sizeof(data->rdh);
210 data->rdh.iType = RDH_RECTANGLES;
211 data->rdh.nCount = reply_size / sizeof(RECT);
212 data->rdh.nRgnSize = reply_size;
213 win_rgn = ExtCreateRegion( NULL, size, data );
216 else size = reply->total_size;
218 SERVER_END_REQ;
219 HeapFree( GetProcessHeap(), 0, data );
220 } while (status == STATUS_BUFFER_OVERFLOW);
222 if (status) SetLastError( RtlNtStatusToDosError(status) );
223 else if (win_rgn)
225 nRet = CombineRgn( hrgn, win_rgn, 0, RGN_COPY );
226 DeleteObject( win_rgn );
228 return nRet;
232 /***********************************************************************
233 * SetWindowRgn (USER32.@)
235 int WINAPI SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL bRedraw )
237 static const RECT empty_rect;
238 BOOL ret;
240 if (hrgn)
242 RGNDATA *data;
243 DWORD size;
245 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
246 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
247 if (!GetRegionData( hrgn, size, data ))
249 HeapFree( GetProcessHeap(), 0, data );
250 return FALSE;
252 SERVER_START_REQ( set_window_region )
254 req->window = hwnd;
255 req->redraw = (bRedraw != 0);
256 if (data->rdh.nCount)
257 wine_server_add_data( req, data->Buffer, data->rdh.nCount * sizeof(RECT) );
258 else
259 wine_server_add_data( req, &empty_rect, sizeof(empty_rect) );
260 ret = !wine_server_call_err( req );
262 SERVER_END_REQ;
264 else /* clear existing region */
266 SERVER_START_REQ( set_window_region )
268 req->window = hwnd;
269 req->redraw = (bRedraw != 0);
270 ret = !wine_server_call_err( req );
272 SERVER_END_REQ;
275 if (ret) ret = USER_Driver->pSetWindowRgn( hwnd, hrgn, bRedraw );
277 if (ret)
279 UINT swp_flags = SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_FRAMECHANGED;
280 if (hrgn) swp_flags |= SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE;
281 if (!bRedraw) swp_flags |= SWP_NOREDRAW;
282 SetWindowPos( hwnd, 0, 0, 0, 0, 0, swp_flags );
284 return ret;
288 /***********************************************************************
289 * GetClientRect (USER32.@)
291 BOOL WINAPI GetClientRect( HWND hwnd, LPRECT rect )
293 BOOL ret;
295 if ((ret = WIN_GetRectangles( hwnd, NULL, rect )))
297 rect->right -= rect->left;
298 rect->bottom -= rect->top;
299 rect->left = rect->top = 0;
301 return ret;
305 /*******************************************************************
306 * ClientToScreen (USER32.@)
308 BOOL WINAPI ClientToScreen( HWND hwnd, LPPOINT lppnt )
310 MapWindowPoints( hwnd, 0, lppnt, 1 );
311 return TRUE;
315 /*******************************************************************
316 * ScreenToClient (USER32.@)
318 BOOL WINAPI ScreenToClient( HWND hwnd, LPPOINT lppnt )
320 MapWindowPoints( 0, hwnd, lppnt, 1 );
321 return TRUE;
325 /***********************************************************************
326 * list_children_from_point
328 * Get the list of children that can contain point from the server.
329 * Point is in screen coordinates.
330 * Returned list must be freed by caller.
332 static HWND *list_children_from_point( HWND hwnd, POINT pt )
334 HWND *list;
335 int size = 32;
337 for (;;)
339 int count = 0;
341 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
343 SERVER_START_REQ( get_window_children_from_point )
345 req->parent = hwnd;
346 req->x = pt.x;
347 req->y = pt.y;
348 wine_server_set_reply( req, list, (size-1) * sizeof(HWND) );
349 if (!wine_server_call( req )) count = reply->count;
351 SERVER_END_REQ;
352 if (count && count < size)
354 list[count] = 0;
355 return list;
357 HeapFree( GetProcessHeap(), 0, list );
358 if (!count) break;
359 size = count + 1; /* restart with a large enough buffer */
361 return NULL;
365 /***********************************************************************
366 * WINPOS_WindowFromPoint
368 * Find the window and hittest for a given point.
370 HWND WINPOS_WindowFromPoint( HWND hwndScope, POINT pt, INT *hittest )
372 int i, res;
373 HWND ret, *list;
375 if (!hwndScope) hwndScope = GetDesktopWindow();
377 *hittest = HTNOWHERE;
379 if (!(list = list_children_from_point( hwndScope, pt ))) return 0;
381 /* now determine the hittest */
383 for (i = 0; list[i]; i++)
385 LONG style = GetWindowLongW( list[i], GWL_STYLE );
387 /* If window is minimized or disabled, return at once */
388 if (style & WS_MINIMIZE)
390 *hittest = HTCAPTION;
391 break;
393 if (style & WS_DISABLED)
395 *hittest = HTERROR;
396 break;
398 /* Send WM_NCCHITTEST (if same thread) */
399 if (!WIN_IsCurrentThread( list[i] ))
401 *hittest = HTCLIENT;
402 break;
404 res = SendMessageW( list[i], WM_NCHITTEST, 0, MAKELONG(pt.x,pt.y) );
405 if (res != HTTRANSPARENT)
407 *hittest = res; /* Found the window */
408 break;
410 /* continue search with next window in z-order */
412 ret = list[i];
413 HeapFree( GetProcessHeap(), 0, list );
414 TRACE( "scope %p (%d,%d) returning %p\n", hwndScope, pt.x, pt.y, ret );
415 return ret;
419 /*******************************************************************
420 * WindowFromPoint (USER32.@)
422 HWND WINAPI WindowFromPoint( POINT pt )
424 INT hittest;
425 return WINPOS_WindowFromPoint( 0, pt, &hittest );
429 /*******************************************************************
430 * ChildWindowFromPoint (USER32.@)
432 HWND WINAPI ChildWindowFromPoint( HWND hwndParent, POINT pt )
434 return ChildWindowFromPointEx( hwndParent, pt, CWP_ALL );
437 /*******************************************************************
438 * RealChildWindowFromPoint (USER32.@)
440 HWND WINAPI RealChildWindowFromPoint( HWND hwndParent, POINT pt )
442 return ChildWindowFromPointEx( hwndParent, pt, CWP_SKIPTRANSPARENT );
445 /*******************************************************************
446 * ChildWindowFromPointEx (USER32.@)
448 HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
450 /* pt is in the client coordinates */
451 HWND *list;
452 int i;
453 RECT rect;
454 HWND retvalue;
456 GetClientRect( hwndParent, &rect );
457 if (!PtInRect( &rect, pt )) return 0;
458 if (!(list = WIN_ListChildren( hwndParent ))) return hwndParent;
460 for (i = 0; list[i]; i++)
462 if (!WIN_GetRectangles( list[i], &rect, NULL )) continue;
463 if (!PtInRect( &rect, pt )) continue;
464 if (uFlags & (CWP_SKIPINVISIBLE|CWP_SKIPDISABLED))
466 LONG style = GetWindowLongW( list[i], GWL_STYLE );
467 if ((uFlags & CWP_SKIPINVISIBLE) && !(style & WS_VISIBLE)) continue;
468 if ((uFlags & CWP_SKIPDISABLED) && (style & WS_DISABLED)) continue;
470 if (uFlags & CWP_SKIPTRANSPARENT)
472 if (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TRANSPARENT) continue;
474 break;
476 retvalue = list[i];
477 HeapFree( GetProcessHeap(), 0, list );
478 if (!retvalue) retvalue = hwndParent;
479 return retvalue;
483 /*******************************************************************
484 * WINPOS_GetWinOffset
486 * Calculate the offset between the origin of the two windows. Used
487 * to implement MapWindowPoints.
489 static void WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo, POINT *offset )
491 WND * wndPtr;
493 offset->x = offset->y = 0;
495 /* Translate source window origin to screen coords */
496 if (hwndFrom)
498 HWND hwnd = hwndFrom;
500 while (hwnd)
502 if (hwnd == hwndTo) return;
503 if (!(wndPtr = WIN_GetPtr( hwnd )))
505 ERR( "bad hwndFrom = %p\n", hwnd );
506 return;
508 if (wndPtr == WND_DESKTOP) break;
509 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
510 offset->x += wndPtr->rectClient.left;
511 offset->y += wndPtr->rectClient.top;
512 hwnd = wndPtr->parent;
513 WIN_ReleasePtr( wndPtr );
517 /* Translate origin to destination window coords */
518 if (hwndTo)
520 HWND hwnd = hwndTo;
522 while (hwnd)
524 if (!(wndPtr = WIN_GetPtr( hwnd )))
526 ERR( "bad hwndTo = %p\n", hwnd );
527 return;
529 if (wndPtr == WND_DESKTOP) break;
530 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
531 offset->x -= wndPtr->rectClient.left;
532 offset->y -= wndPtr->rectClient.top;
533 hwnd = wndPtr->parent;
534 WIN_ReleasePtr( wndPtr );
537 return;
539 other_process: /* one of the parents may belong to another process, do it the hard way */
540 offset->x = offset->y = 0;
541 SERVER_START_REQ( get_windows_offset )
543 req->from = hwndFrom;
544 req->to = hwndTo;
545 if (!wine_server_call( req ))
547 offset->x = reply->x;
548 offset->y = reply->y;
551 SERVER_END_REQ;
555 /*******************************************************************
556 * MapWindowPoints (USER.258)
558 void WINAPI MapWindowPoints16( HWND16 hwndFrom, HWND16 hwndTo,
559 LPPOINT16 lppt, UINT16 count )
561 POINT offset;
563 WINPOS_GetWinOffset( WIN_Handle32(hwndFrom), WIN_Handle32(hwndTo), &offset );
564 while (count--)
566 lppt->x += offset.x;
567 lppt->y += offset.y;
568 lppt++;
573 /*******************************************************************
574 * MapWindowPoints (USER32.@)
576 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
578 POINT offset;
580 WINPOS_GetWinOffset( hwndFrom, hwndTo, &offset );
581 while (count--)
583 lppt->x += offset.x;
584 lppt->y += offset.y;
585 lppt++;
587 return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
591 /***********************************************************************
592 * IsIconic (USER32.@)
594 BOOL WINAPI IsIconic(HWND hWnd)
596 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
600 /***********************************************************************
601 * IsZoomed (USER32.@)
603 BOOL WINAPI IsZoomed(HWND hWnd)
605 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
609 /*******************************************************************
610 * AllowSetForegroundWindow (USER32.@)
612 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
614 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
615 * implemented, then fix this function. */
616 return TRUE;
620 /*******************************************************************
621 * LockSetForegroundWindow (USER32.@)
623 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
625 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
626 * implemented, then fix this function. */
627 return TRUE;
631 /***********************************************************************
632 * BringWindowToTop (USER32.@)
634 BOOL WINAPI BringWindowToTop( HWND hwnd )
636 return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
640 /***********************************************************************
641 * MoveWindow (USER32.@)
643 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
644 BOOL repaint )
646 int flags = SWP_NOZORDER | SWP_NOACTIVATE;
647 if (!repaint) flags |= SWP_NOREDRAW;
648 TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
649 return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
652 /***********************************************************************
653 * WINPOS_InitInternalPos
655 static LPINTERNALPOS WINPOS_InitInternalPos( WND* wnd )
657 LPINTERNALPOS lpPos = get_internal_pos( wnd->hwndSelf );
658 if( !lpPos )
660 /* this happens when the window is minimized/maximized
661 * for the first time (rectWindow is not adjusted yet) */
663 lpPos = HeapAlloc( GetProcessHeap(), 0, sizeof(INTERNALPOS) );
664 if( !lpPos ) return NULL;
666 set_internal_pos( wnd->hwndSelf, lpPos );
667 lpPos->hwndIconTitle = 0; /* defer until needs to be shown */
668 lpPos->rectNormal.left = wnd->rectWindow.left;
669 lpPos->rectNormal.top = wnd->rectWindow.top;
670 lpPos->rectNormal.right = wnd->rectWindow.right;
671 lpPos->rectNormal.bottom = wnd->rectWindow.bottom;
672 lpPos->ptIconPos.x = lpPos->ptIconPos.y = -1;
673 lpPos->ptMaxPos.x = lpPos->ptMaxPos.y = -1;
676 if( wnd->dwStyle & WS_MINIMIZE )
678 lpPos->ptIconPos.x = wnd->rectWindow.left;
679 lpPos->ptIconPos.y = wnd->rectWindow.top;
681 else if( wnd->dwStyle & WS_MAXIMIZE )
683 lpPos->ptMaxPos.x = wnd->rectWindow.left;
684 lpPos->ptMaxPos.y = wnd->rectWindow.top;
686 else
688 lpPos->rectNormal.left = wnd->rectWindow.left;
689 lpPos->rectNormal.top = wnd->rectWindow.top;
690 lpPos->rectNormal.right = wnd->rectWindow.right;
691 lpPos->rectNormal.bottom = wnd->rectWindow.bottom;
693 return lpPos;
696 /***********************************************************************
697 * WINPOS_RedrawIconTitle
699 BOOL WINPOS_RedrawIconTitle( HWND hWnd )
701 LPINTERNALPOS lpPos = get_internal_pos( hWnd );
702 if( lpPos )
704 if( lpPos->hwndIconTitle )
706 SendMessageW( lpPos->hwndIconTitle, WM_SHOWWINDOW, TRUE, 0);
707 InvalidateRect( lpPos->hwndIconTitle, NULL, TRUE );
708 return TRUE;
711 return FALSE;
714 /***********************************************************************
715 * WINPOS_ShowIconTitle
717 BOOL WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
719 LPINTERNALPOS lpPos = get_internal_pos( hwnd );
721 if (lpPos && !GetPropA( hwnd, "__wine_x11_managed" ))
723 HWND title = lpPos->hwndIconTitle;
725 TRACE("%p %i\n", hwnd, (bShow != 0) );
727 if( !title )
728 lpPos->hwndIconTitle = title = ICONTITLE_Create( hwnd );
729 if( bShow )
731 if (!IsWindowVisible(title))
733 SendMessageW( title, WM_SHOWWINDOW, TRUE, 0 );
734 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
735 SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
738 else ShowWindow( title, SW_HIDE );
740 return FALSE;
743 /*******************************************************************
744 * WINPOS_GetMinMaxInfo
746 * Get the minimized and maximized information for a window.
748 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
749 POINT *minTrack, POINT *maxTrack )
751 LPINTERNALPOS lpPos;
752 MINMAXINFO MinMax;
753 HMONITOR monitor;
754 INT xinc, yinc;
755 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
756 LONG exstyle = GetWindowLongA( hwnd, GWL_EXSTYLE );
757 RECT rc;
759 /* Compute default values */
761 GetWindowRect(hwnd, &rc);
762 MinMax.ptReserved.x = rc.left;
763 MinMax.ptReserved.y = rc.top;
765 if (style & WS_CHILD)
767 if ((style & WS_CAPTION) == WS_CAPTION)
768 style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
770 GetClientRect(GetAncestor(hwnd,GA_PARENT), &rc);
771 AdjustWindowRectEx(&rc, style, ((style & WS_POPUP) && GetMenu(hwnd)), exstyle);
773 /* avoid calculating this twice */
774 style &= ~(WS_DLGFRAME | WS_BORDER | WS_THICKFRAME);
776 MinMax.ptMaxSize.x = rc.right - rc.left;
777 MinMax.ptMaxSize.y = rc.bottom - rc.top;
779 else
781 MinMax.ptMaxSize.x = GetSystemMetrics(SM_CXSCREEN);
782 MinMax.ptMaxSize.y = GetSystemMetrics(SM_CYSCREEN);
784 MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
785 MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
786 MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXMAXTRACK);
787 MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYMAXTRACK);
789 if (HAS_DLGFRAME( style, exstyle ))
791 xinc = GetSystemMetrics(SM_CXDLGFRAME);
792 yinc = GetSystemMetrics(SM_CYDLGFRAME);
794 else
796 xinc = yinc = 0;
797 if (HAS_THICKFRAME(style))
799 xinc += GetSystemMetrics(SM_CXFRAME);
800 yinc += GetSystemMetrics(SM_CYFRAME);
802 if (style & WS_BORDER)
804 xinc += GetSystemMetrics(SM_CXBORDER);
805 yinc += GetSystemMetrics(SM_CYBORDER);
808 MinMax.ptMaxSize.x += 2 * xinc;
809 MinMax.ptMaxSize.y += 2 * yinc;
811 lpPos = get_internal_pos( hwnd );
812 if( lpPos && !EMPTYPOINT(lpPos->ptMaxPos) )
814 MinMax.ptMaxPosition.x = lpPos->ptMaxPos.x;
815 MinMax.ptMaxPosition.y = lpPos->ptMaxPos.y;
817 else
819 MinMax.ptMaxPosition.x = -xinc;
820 MinMax.ptMaxPosition.y = -yinc;
823 SendMessageW( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
825 /* if the app didn't change the values, adapt them for the current monitor */
827 if ((monitor = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY )))
829 MONITORINFO mon_info;
831 mon_info.cbSize = sizeof(mon_info);
832 GetMonitorInfoW( monitor, &mon_info );
834 if (MinMax.ptMaxSize.x == GetSystemMetrics(SM_CXSCREEN) + 2 * xinc &&
835 MinMax.ptMaxSize.y == GetSystemMetrics(SM_CYSCREEN) + 2 * yinc)
837 MinMax.ptMaxSize.x = (mon_info.rcWork.right - mon_info.rcWork.left) + 2 * xinc;
838 MinMax.ptMaxSize.y = (mon_info.rcWork.bottom - mon_info.rcWork.top) + 2 * yinc;
840 if (MinMax.ptMaxPosition.x == -xinc && MinMax.ptMaxPosition.y == -yinc)
842 MinMax.ptMaxPosition.x = mon_info.rcWork.left - xinc;
843 MinMax.ptMaxPosition.y = mon_info.rcWork.top - yinc;
847 /* Some sanity checks */
849 TRACE("%d %d / %d %d / %d %d / %d %d\n",
850 MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
851 MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
852 MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
853 MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
854 MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
855 MinMax.ptMinTrackSize.x );
856 MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
857 MinMax.ptMinTrackSize.y );
859 if (maxSize) *maxSize = MinMax.ptMaxSize;
860 if (maxPos) *maxPos = MinMax.ptMaxPosition;
861 if (minTrack) *minTrack = MinMax.ptMinTrackSize;
862 if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
865 /***********************************************************************
866 * ShowWindowAsync (USER32.@)
868 * doesn't wait; returns immediately.
869 * used by threads to toggle windows in other (possibly hanging) threads
871 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
873 HWND full_handle;
875 if (is_broadcast(hwnd))
877 SetLastError( ERROR_INVALID_PARAMETER );
878 return FALSE;
881 if ((full_handle = WIN_IsCurrentThread( hwnd )))
882 return USER_Driver->pShowWindow( full_handle, cmd );
884 return SendNotifyMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
888 /***********************************************************************
889 * ShowWindow (USER32.@)
891 BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
893 HWND full_handle;
895 if (is_broadcast(hwnd))
897 SetLastError( ERROR_INVALID_PARAMETER );
898 return FALSE;
900 if ((full_handle = WIN_IsCurrentThread( hwnd )))
901 return USER_Driver->pShowWindow( full_handle, cmd );
903 return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
907 /***********************************************************************
908 * GetInternalWindowPos (USER32.@)
910 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
911 LPPOINT ptIcon )
913 WINDOWPLACEMENT wndpl;
914 if (GetWindowPlacement( hwnd, &wndpl ))
916 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
917 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
918 return wndpl.showCmd;
920 return 0;
924 /***********************************************************************
925 * GetWindowPlacement (USER32.@)
927 * Win95:
928 * Fails if wndpl->length of Win95 (!) apps is invalid.
930 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
932 WND *pWnd = WIN_GetPtr( hwnd );
933 LPINTERNALPOS lpPos;
935 if (!pWnd || pWnd == WND_DESKTOP) return FALSE;
936 if (pWnd == WND_OTHER_PROCESS)
938 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
939 return FALSE;
942 lpPos = WINPOS_InitInternalPos( pWnd );
943 wndpl->length = sizeof(*wndpl);
944 if( pWnd->dwStyle & WS_MINIMIZE )
945 wndpl->showCmd = SW_SHOWMINIMIZED;
946 else
947 wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
948 if( pWnd->flags & WIN_RESTORE_MAX )
949 wndpl->flags = WPF_RESTORETOMAXIMIZED;
950 else
951 wndpl->flags = 0;
952 wndpl->ptMinPosition.x = lpPos->ptIconPos.x;
953 wndpl->ptMinPosition.y = lpPos->ptIconPos.y;
954 wndpl->ptMaxPosition.x = lpPos->ptMaxPos.x;
955 wndpl->ptMaxPosition.y = lpPos->ptMaxPos.y;
956 wndpl->rcNormalPosition.left = lpPos->rectNormal.left;
957 wndpl->rcNormalPosition.top = lpPos->rectNormal.top;
958 wndpl->rcNormalPosition.right = lpPos->rectNormal.right;
959 wndpl->rcNormalPosition.bottom = lpPos->rectNormal.bottom;
960 WIN_ReleasePtr( pWnd );
961 return TRUE;
965 /***********************************************************************
966 * WINPOS_SetPlacement
968 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
970 LPINTERNALPOS lpPos;
971 DWORD style;
972 WND *pWnd = WIN_GetPtr( hwnd );
974 if (!pWnd || pWnd == WND_OTHER_PROCESS || pWnd == WND_DESKTOP) return FALSE;
975 lpPos = WINPOS_InitInternalPos( pWnd );
977 if( flags & PLACE_MIN )
979 lpPos->ptIconPos.x = wndpl->ptMinPosition.x;
980 lpPos->ptIconPos.y = wndpl->ptMinPosition.y;
982 if( flags & PLACE_MAX )
984 lpPos->ptMaxPos.x = wndpl->ptMaxPosition.x;
985 lpPos->ptMaxPos.y = wndpl->ptMaxPosition.y;
987 if( flags & PLACE_RECT)
989 lpPos->rectNormal.left = wndpl->rcNormalPosition.left;
990 lpPos->rectNormal.top = wndpl->rcNormalPosition.top;
991 lpPos->rectNormal.right = wndpl->rcNormalPosition.right;
992 lpPos->rectNormal.bottom = wndpl->rcNormalPosition.bottom;
995 style = pWnd->dwStyle;
996 WIN_ReleasePtr( pWnd );
998 if( style & WS_MINIMIZE )
1000 WINPOS_ShowIconTitle( hwnd, FALSE );
1001 if( wndpl->flags & WPF_SETMINPOSITION && !EMPTYPOINT(lpPos->ptIconPos))
1002 SetWindowPos( hwnd, 0, lpPos->ptIconPos.x, lpPos->ptIconPos.y,
1003 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1005 else if( style & WS_MAXIMIZE )
1007 if( !EMPTYPOINT(lpPos->ptMaxPos) )
1008 SetWindowPos( hwnd, 0, lpPos->ptMaxPos.x, lpPos->ptMaxPos.y,
1009 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1011 else if( flags & PLACE_RECT )
1012 SetWindowPos( hwnd, 0, lpPos->rectNormal.left, lpPos->rectNormal.top,
1013 lpPos->rectNormal.right - lpPos->rectNormal.left,
1014 lpPos->rectNormal.bottom - lpPos->rectNormal.top,
1015 SWP_NOZORDER | SWP_NOACTIVATE );
1017 ShowWindow( hwnd, wndpl->showCmd );
1019 if (IsIconic( hwnd ))
1021 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE) WINPOS_ShowIconTitle( hwnd, TRUE );
1023 /* SDK: ...valid only the next time... */
1024 if( wndpl->flags & WPF_RESTORETOMAXIMIZED )
1026 pWnd = WIN_GetPtr( hwnd );
1027 if (pWnd && pWnd != WND_OTHER_PROCESS)
1029 pWnd->flags |= WIN_RESTORE_MAX;
1030 WIN_ReleasePtr( pWnd );
1034 return TRUE;
1038 /***********************************************************************
1039 * SetWindowPlacement (USER32.@)
1041 * Win95:
1042 * Fails if wndpl->length of Win95 (!) apps is invalid.
1044 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
1046 if (!wpl) return FALSE;
1047 return WINPOS_SetPlacement( hwnd, wpl, PLACE_MIN | PLACE_MAX | PLACE_RECT );
1051 /***********************************************************************
1052 * AnimateWindow (USER32.@)
1053 * Shows/Hides a window with an animation
1054 * NO ANIMATION YET
1056 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
1058 FIXME("partial stub\n");
1060 /* If trying to show/hide and it's already *
1061 * shown/hidden or invalid window, fail with *
1062 * invalid parameter */
1063 if(!IsWindow(hwnd) ||
1064 (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
1065 (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
1067 SetLastError(ERROR_INVALID_PARAMETER);
1068 return FALSE;
1071 ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1073 return TRUE;
1076 /***********************************************************************
1077 * SetInternalWindowPos (USER32.@)
1079 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1080 LPRECT rect, LPPOINT pt )
1082 if( IsWindow(hwnd) )
1084 WINDOWPLACEMENT wndpl;
1085 UINT flags;
1087 wndpl.length = sizeof(wndpl);
1088 wndpl.showCmd = showCmd;
1089 wndpl.flags = flags = 0;
1091 if( pt )
1093 flags |= PLACE_MIN;
1094 wndpl.flags |= WPF_SETMINPOSITION;
1095 wndpl.ptMinPosition = *pt;
1097 if( rect )
1099 flags |= PLACE_RECT;
1100 wndpl.rcNormalPosition = *rect;
1102 WINPOS_SetPlacement( hwnd, &wndpl, flags );
1107 /*******************************************************************
1108 * can_activate_window
1110 * Check if we can activate the specified window.
1112 static BOOL can_activate_window( HWND hwnd )
1114 LONG style;
1116 if (!hwnd) return FALSE;
1117 style = GetWindowLongW( hwnd, GWL_STYLE );
1118 if (!(style & WS_VISIBLE)) return FALSE;
1119 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1120 return !(style & WS_DISABLED);
1124 /*******************************************************************
1125 * WINPOS_ActivateOtherWindow
1127 * Activates window other than pWnd.
1129 void WINPOS_ActivateOtherWindow(HWND hwnd)
1131 HWND hwndTo, fg;
1133 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1135 hwndTo = GetAncestor( hwndTo, GA_ROOT );
1136 if (can_activate_window( hwndTo )) goto done;
1139 hwndTo = hwnd;
1140 for (;;)
1142 if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1143 if (can_activate_window( hwndTo )) break;
1146 done:
1147 fg = GetForegroundWindow();
1148 TRACE("win = %p fg = %p\n", hwndTo, fg);
1149 if (!fg || (hwnd == fg))
1151 if (SetForegroundWindow( hwndTo )) return;
1153 if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1157 /***********************************************************************
1158 * WINPOS_HandleWindowPosChanging
1160 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1162 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1164 POINT minTrack, maxTrack;
1165 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1167 if (winpos->flags & SWP_NOSIZE) return 0;
1168 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1170 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1171 if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1172 if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1173 if (!(style & WS_MINIMIZE))
1175 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1176 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1179 return 0;
1183 /***********************************************************************
1184 * dump_winpos_flags
1186 static void dump_winpos_flags(UINT flags)
1188 TRACE("flags:");
1189 if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1190 if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1191 if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1192 if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1193 if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1194 if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1195 if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1196 if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1197 if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1198 if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1199 if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1200 if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1201 if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1203 #define DUMPED_FLAGS \
1204 (SWP_NOSIZE | \
1205 SWP_NOMOVE | \
1206 SWP_NOZORDER | \
1207 SWP_NOREDRAW | \
1208 SWP_NOACTIVATE | \
1209 SWP_FRAMECHANGED | \
1210 SWP_SHOWWINDOW | \
1211 SWP_HIDEWINDOW | \
1212 SWP_NOCOPYBITS | \
1213 SWP_NOOWNERZORDER | \
1214 SWP_NOSENDCHANGING | \
1215 SWP_DEFERERASE | \
1216 SWP_ASYNCWINDOWPOS)
1218 if(flags & ~DUMPED_FLAGS) TRACE(" %08x", flags & ~DUMPED_FLAGS);
1219 TRACE("\n");
1220 #undef DUMPED_FLAGS
1223 /***********************************************************************
1224 * SWP_DoWinPosChanging
1226 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
1228 WND *wndPtr;
1230 /* Send WM_WINDOWPOSCHANGING message */
1232 if (!(pWinpos->flags & SWP_NOSENDCHANGING))
1233 SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
1235 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) ||
1236 wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
1238 /* Calculate new position and size */
1240 *pNewWindowRect = wndPtr->rectWindow;
1241 *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
1242 : wndPtr->rectClient;
1244 if (!(pWinpos->flags & SWP_NOSIZE))
1246 pNewWindowRect->right = pNewWindowRect->left + pWinpos->cx;
1247 pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
1249 if (!(pWinpos->flags & SWP_NOMOVE))
1251 pNewWindowRect->left = pWinpos->x;
1252 pNewWindowRect->top = pWinpos->y;
1253 pNewWindowRect->right += pWinpos->x - wndPtr->rectWindow.left;
1254 pNewWindowRect->bottom += pWinpos->y - wndPtr->rectWindow.top;
1256 OffsetRect( pNewClientRect, pWinpos->x - wndPtr->rectWindow.left,
1257 pWinpos->y - wndPtr->rectWindow.top );
1259 pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
1261 TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
1262 pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
1263 pWinpos->cx, pWinpos->cy, pWinpos->flags );
1264 TRACE( "current %s style %08x new %s\n",
1265 wine_dbgstr_rect( &wndPtr->rectWindow ), wndPtr->dwStyle,
1266 wine_dbgstr_rect( pNewWindowRect ));
1268 WIN_ReleasePtr( wndPtr );
1269 return TRUE;
1272 /***********************************************************************
1273 * get_valid_rects
1275 * Compute the valid rects from the old and new client rect and WVR_* flags.
1276 * Helper for WM_NCCALCSIZE handling.
1278 static inline void get_valid_rects( const RECT *old_client, const RECT *new_client, UINT flags,
1279 RECT *valid )
1281 int cx, cy;
1283 if (flags & WVR_REDRAW)
1285 SetRectEmpty( &valid[0] );
1286 SetRectEmpty( &valid[1] );
1287 return;
1290 if (flags & WVR_VALIDRECTS)
1292 if (!IntersectRect( &valid[0], &valid[0], new_client ) ||
1293 !IntersectRect( &valid[1], &valid[1], old_client ))
1295 SetRectEmpty( &valid[0] );
1296 SetRectEmpty( &valid[1] );
1297 return;
1299 flags = WVR_ALIGNLEFT | WVR_ALIGNTOP;
1301 else
1303 valid[0] = *new_client;
1304 valid[1] = *old_client;
1307 /* make sure the rectangles have the same size */
1308 cx = min( valid[0].right - valid[0].left, valid[1].right - valid[1].left );
1309 cy = min( valid[0].bottom - valid[0].top, valid[1].bottom - valid[1].top );
1311 if (flags & WVR_ALIGNBOTTOM)
1313 valid[0].top = valid[0].bottom - cy;
1314 valid[1].top = valid[1].bottom - cy;
1316 else
1318 valid[0].bottom = valid[0].top + cy;
1319 valid[1].bottom = valid[1].top + cy;
1321 if (flags & WVR_ALIGNRIGHT)
1323 valid[0].left = valid[0].right - cx;
1324 valid[1].left = valid[1].right - cx;
1326 else
1328 valid[0].right = valid[0].left + cx;
1329 valid[1].right = valid[1].left + cx;
1333 struct move_owned_info
1335 HWND owner;
1336 HWND insert_after;
1339 static BOOL CALLBACK move_owned_popups( HWND hwnd, LPARAM lparam )
1341 struct move_owned_info *info = (struct move_owned_info *)lparam;
1343 if (hwnd == info->owner) return FALSE;
1344 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) &&
1345 GetWindow( hwnd, GW_OWNER ) == info->owner)
1347 SetWindowPos( hwnd, info->insert_after, 0, 0, 0, 0,
1348 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE |
1349 SWP_NOSENDCHANGING | SWP_DEFERERASE );
1350 info->insert_after = hwnd;
1352 return TRUE;
1355 /***********************************************************************
1356 * SWP_DoOwnedPopups
1358 * fix Z order taking into account owned popups -
1359 * basically we need to maintain them above the window that owns them
1361 * FIXME: hide/show owned popups when owner visibility changes.
1363 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
1365 HWND owner = GetWindow( hwnd, GW_OWNER );
1366 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1367 struct move_owned_info info;
1369 TRACE("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
1371 if ((style & WS_POPUP) && owner)
1373 /* make sure this popup stays above the owner */
1375 if( hwndInsertAfter != HWND_TOP )
1377 HWND hwndLocalPrev = HWND_TOP;
1378 HWND prev = GetWindow( owner, GW_HWNDPREV );
1380 while (prev && prev != hwndInsertAfter)
1382 if (hwndLocalPrev == HWND_TOP && GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE)
1383 hwndLocalPrev = prev;
1384 prev = GetWindow( prev, GW_HWNDPREV );
1386 if (!prev) hwndInsertAfter = hwndLocalPrev;
1389 else if (style & WS_CHILD) return hwndInsertAfter;
1391 info.owner = hwnd;
1392 info.insert_after = hwndInsertAfter;
1393 EnumWindows( move_owned_popups, (LPARAM)&info );
1394 return info.insert_after;
1397 /***********************************************************************
1398 * SWP_DoNCCalcSize
1400 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect,
1401 RECT *validRects )
1403 UINT wvrFlags = 0;
1404 WND *wndPtr;
1406 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
1408 /* Send WM_NCCALCSIZE message to get new client area */
1409 if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
1411 NCCALCSIZE_PARAMS params;
1412 WINDOWPOS winposCopy;
1414 params.rgrc[0] = *pNewWindowRect;
1415 params.rgrc[1] = wndPtr->rectWindow;
1416 params.rgrc[2] = wndPtr->rectClient;
1417 params.lppos = &winposCopy;
1418 winposCopy = *pWinpos;
1419 WIN_ReleasePtr( wndPtr );
1421 wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
1423 *pNewClientRect = params.rgrc[0];
1425 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
1427 TRACE( "hwnd %p old win %s old client %s new win %s new client %s\n", pWinpos->hwnd,
1428 wine_dbgstr_rect(&wndPtr->rectWindow), wine_dbgstr_rect(&wndPtr->rectClient),
1429 wine_dbgstr_rect(pNewWindowRect), wine_dbgstr_rect(pNewClientRect) );
1431 if( pNewClientRect->left != wndPtr->rectClient.left ||
1432 pNewClientRect->top != wndPtr->rectClient.top )
1433 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1435 if( (pNewClientRect->right - pNewClientRect->left !=
1436 wndPtr->rectClient.right - wndPtr->rectClient.left))
1437 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1438 else
1439 wvrFlags &= ~WVR_HREDRAW;
1441 if (pNewClientRect->bottom - pNewClientRect->top !=
1442 wndPtr->rectClient.bottom - wndPtr->rectClient.top)
1443 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1444 else
1445 wvrFlags &= ~WVR_VREDRAW;
1447 validRects[0] = params.rgrc[1];
1448 validRects[1] = params.rgrc[2];
1450 else
1452 if (!(pWinpos->flags & SWP_NOMOVE) &&
1453 (pNewClientRect->left != wndPtr->rectClient.left ||
1454 pNewClientRect->top != wndPtr->rectClient.top))
1455 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1458 if (pWinpos->flags & (SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
1460 SetRectEmpty( &validRects[0] );
1461 SetRectEmpty( &validRects[1] );
1463 else get_valid_rects( &wndPtr->rectClient, pNewClientRect, wvrFlags, validRects );
1465 WIN_ReleasePtr( wndPtr );
1466 return wvrFlags;
1469 /* fix redundant flags and values in the WINDOWPOS structure */
1470 static BOOL fixup_flags( WINDOWPOS *winpos )
1472 HWND parent;
1473 WND *wndPtr = WIN_GetPtr( winpos->hwnd );
1474 BOOL ret = TRUE;
1476 if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
1478 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1479 return FALSE;
1481 winpos->hwnd = wndPtr->hwndSelf; /* make it a full handle */
1483 /* Finally make sure that all coordinates are valid */
1484 if (winpos->x < -32768) winpos->x = -32768;
1485 else if (winpos->x > 32767) winpos->x = 32767;
1486 if (winpos->y < -32768) winpos->y = -32768;
1487 else if (winpos->y > 32767) winpos->y = 32767;
1489 if (winpos->cx < 0) winpos->cx = 0;
1490 else if (winpos->cx > 32767) winpos->cx = 32767;
1491 if (winpos->cy < 0) winpos->cy = 0;
1492 else if (winpos->cy > 32767) winpos->cy = 32767;
1494 parent = GetAncestor( winpos->hwnd, GA_PARENT );
1495 if (!IsWindowVisible( parent )) winpos->flags |= SWP_NOREDRAW;
1497 if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
1498 else
1500 winpos->flags &= ~SWP_HIDEWINDOW;
1501 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
1504 if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
1505 (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
1506 winpos->flags |= SWP_NOSIZE; /* Already the right size */
1508 if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
1509 winpos->flags |= SWP_NOMOVE; /* Already the right position */
1511 if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
1513 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) /* Bring to the top when activating */
1515 winpos->flags &= ~SWP_NOZORDER;
1516 winpos->hwndInsertAfter = HWND_TOP;
1520 /* Check hwndInsertAfter */
1521 if (winpos->flags & SWP_NOZORDER) goto done;
1523 /* fix sign extension */
1524 if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
1525 else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
1527 /* FIXME: TOPMOST not supported yet */
1528 if ((winpos->hwndInsertAfter == HWND_TOPMOST) ||
1529 (winpos->hwndInsertAfter == HWND_NOTOPMOST)) winpos->hwndInsertAfter = HWND_TOP;
1531 /* hwndInsertAfter must be a sibling of the window */
1532 if (winpos->hwndInsertAfter == HWND_TOP)
1534 if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1535 winpos->flags |= SWP_NOZORDER;
1537 else if (winpos->hwndInsertAfter == HWND_BOTTOM)
1539 if (GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
1540 winpos->flags |= SWP_NOZORDER;
1542 else
1544 if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != parent) ret = FALSE;
1545 else
1547 /* don't need to change the Zorder of hwnd if it's already inserted
1548 * after hwndInsertAfter or when inserting hwnd after itself.
1550 if ((winpos->hwnd == winpos->hwndInsertAfter) ||
1551 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
1552 winpos->flags |= SWP_NOZORDER;
1555 done:
1556 WIN_ReleasePtr( wndPtr );
1557 return ret;
1560 /***********************************************************************
1561 * USER_SetWindowPos
1563 * User32 internal function
1565 BOOL USER_SetWindowPos( WINDOWPOS * winpos )
1567 RECT newWindowRect, newClientRect, valid_rects[2];
1568 UINT orig_flags;
1570 orig_flags = winpos->flags;
1572 /* First make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
1573 if (!(winpos->flags & SWP_NOMOVE))
1575 if (winpos->x < -32768) winpos->x = -32768;
1576 else if (winpos->x > 32767) winpos->x = 32767;
1577 if (winpos->y < -32768) winpos->y = -32768;
1578 else if (winpos->y > 32767) winpos->y = 32767;
1580 if (!(winpos->flags & SWP_NOSIZE))
1582 if (winpos->cx < 0) winpos->cx = 0;
1583 else if (winpos->cx > 32767) winpos->cx = 32767;
1584 if (winpos->cy < 0) winpos->cy = 0;
1585 else if (winpos->cy > 32767) winpos->cy = 32767;
1588 if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
1590 /* Fix redundant flags */
1591 if (!fixup_flags( winpos )) return FALSE;
1593 if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
1595 if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
1596 winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
1599 /* Common operations */
1601 SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect, valid_rects );
1603 if(!USER_Driver->pSetWindowPos( winpos->hwnd, winpos->hwndInsertAfter,
1604 &newWindowRect, &newClientRect, orig_flags, valid_rects ))
1605 return FALSE;
1607 /* erase parent if hiding child */
1608 if (!(orig_flags & SWP_DEFERERASE))
1610 if (orig_flags & SWP_HIDEWINDOW)
1612 HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
1613 erase_now( parent, RDW_NOCHILDREN );
1615 else if (!(orig_flags & SWP_SHOWWINDOW) &&
1616 (winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOGEOMETRYCHANGE)
1618 erase_now( winpos->hwnd, 0 );
1622 if( winpos->flags & SWP_HIDEWINDOW )
1623 HideCaret(winpos->hwnd);
1624 else if (winpos->flags & SWP_SHOWWINDOW)
1625 ShowCaret(winpos->hwnd);
1627 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)))
1629 /* child windows get WM_CHILDACTIVATE message */
1630 if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
1631 SendMessageW( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
1632 else
1633 SetForegroundWindow( winpos->hwnd );
1636 /* And last, send the WM_WINDOWPOSCHANGED message */
1638 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
1640 if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
1642 /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
1643 and always contains final window position.
1645 winpos->x = newWindowRect.left;
1646 winpos->y = newWindowRect.top;
1647 winpos->cx = newWindowRect.right - newWindowRect.left;
1648 winpos->cy = newWindowRect.bottom - newWindowRect.top;
1649 SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
1651 return TRUE;
1654 /***********************************************************************
1655 * SetWindowPos (USER32.@)
1657 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
1658 INT x, INT y, INT cx, INT cy, UINT flags )
1660 WINDOWPOS winpos;
1662 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1663 hwnd, hwndInsertAfter, x, y, cx, cy, flags);
1664 if(TRACE_ON(win)) dump_winpos_flags(flags);
1666 if (is_broadcast(hwnd))
1668 SetLastError( ERROR_INVALID_PARAMETER );
1669 return FALSE;
1672 winpos.hwnd = WIN_GetFullHandle(hwnd);
1673 winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
1674 winpos.x = x;
1675 winpos.y = y;
1676 winpos.cx = cx;
1677 winpos.cy = cy;
1678 winpos.flags = flags;
1680 if (WIN_IsCurrentThread( hwnd ))
1681 return USER_SetWindowPos(&winpos);
1683 return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
1687 /***********************************************************************
1688 * BeginDeferWindowPos (USER32.@)
1690 HDWP WINAPI BeginDeferWindowPos( INT count )
1692 HDWP handle;
1693 DWP *pDWP;
1695 TRACE("%d\n", count);
1697 if (count < 0)
1699 SetLastError(ERROR_INVALID_PARAMETER);
1700 return 0;
1702 /* Windows allows zero count, in which case it allocates context for 8 moves */
1703 if (count == 0) count = 8;
1705 handle = USER_HEAP_ALLOC( sizeof(DWP) + (count-1)*sizeof(WINDOWPOS) );
1706 if (!handle) return 0;
1707 pDWP = (DWP *) USER_HEAP_LIN_ADDR( handle );
1708 pDWP->actualCount = 0;
1709 pDWP->suggestedCount = count;
1710 pDWP->valid = TRUE;
1711 pDWP->wMagic = DWP_MAGIC;
1712 pDWP->hwndParent = 0;
1714 TRACE("returning hdwp %p\n", handle);
1715 return handle;
1719 /***********************************************************************
1720 * DeferWindowPos (USER32.@)
1722 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
1723 INT x, INT y, INT cx, INT cy,
1724 UINT flags )
1726 DWP *pDWP;
1727 int i;
1728 HDWP newhdwp = hdwp,retvalue;
1730 TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1731 hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
1733 hwnd = WIN_GetFullHandle( hwnd );
1734 if (hwnd == GetDesktopWindow()) return 0;
1736 if (!(pDWP = USER_HEAP_LIN_ADDR( hdwp ))) return 0;
1738 USER_Lock();
1740 for (i = 0; i < pDWP->actualCount; i++)
1742 if (pDWP->winPos[i].hwnd == hwnd)
1744 /* Merge with the other changes */
1745 if (!(flags & SWP_NOZORDER))
1747 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
1749 if (!(flags & SWP_NOMOVE))
1751 pDWP->winPos[i].x = x;
1752 pDWP->winPos[i].y = y;
1754 if (!(flags & SWP_NOSIZE))
1756 pDWP->winPos[i].cx = cx;
1757 pDWP->winPos[i].cy = cy;
1759 pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
1760 SWP_NOZORDER | SWP_NOREDRAW |
1761 SWP_NOACTIVATE | SWP_NOCOPYBITS|
1762 SWP_NOOWNERZORDER);
1763 pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
1764 SWP_FRAMECHANGED);
1765 retvalue = hdwp;
1766 goto END;
1769 if (pDWP->actualCount >= pDWP->suggestedCount)
1771 newhdwp = USER_HEAP_REALLOC( hdwp,
1772 sizeof(DWP) + pDWP->suggestedCount*sizeof(WINDOWPOS) );
1773 if (!newhdwp)
1775 retvalue = 0;
1776 goto END;
1778 pDWP = (DWP *) USER_HEAP_LIN_ADDR( newhdwp );
1779 pDWP->suggestedCount++;
1781 pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
1782 pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
1783 pDWP->winPos[pDWP->actualCount].x = x;
1784 pDWP->winPos[pDWP->actualCount].y = y;
1785 pDWP->winPos[pDWP->actualCount].cx = cx;
1786 pDWP->winPos[pDWP->actualCount].cy = cy;
1787 pDWP->winPos[pDWP->actualCount].flags = flags;
1788 pDWP->actualCount++;
1789 retvalue = newhdwp;
1790 END:
1791 USER_Unlock();
1792 return retvalue;
1796 /***********************************************************************
1797 * EndDeferWindowPos (USER32.@)
1799 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
1801 DWP *pDWP;
1802 WINDOWPOS *winpos;
1803 BOOL res = TRUE;
1804 int i;
1806 TRACE("%p\n", hdwp);
1808 pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
1809 if (!pDWP) return FALSE;
1810 for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
1812 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1813 winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
1814 winpos->cx, winpos->cy, winpos->flags);
1816 if (!(res = USER_SetWindowPos( winpos ))) break;
1818 USER_HEAP_FREE( hdwp );
1819 return res;
1823 /***********************************************************************
1824 * TileChildWindows (USER.199)
1826 void WINAPI TileChildWindows16( HWND16 parent, WORD action )
1828 FIXME("(%04x, %d): stub\n", parent, action);
1831 /***********************************************************************
1832 * CascadeChildWindows (USER.198)
1834 void WINAPI CascadeChildWindows16( HWND16 parent, WORD action )
1836 FIXME("(%04x, %d): stub\n", parent, action);