ddraw/tests: Rewrite LimitTest().
[wine.git] / dlls / user32 / winpos.c
blob10f0fd06990568e65c21ee5387ad3182f2dffea5
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 "wine/gdi_driver.h"
37 #include "win.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(win);
42 #define SWP_AGG_NOGEOMETRYCHANGE \
43 (SWP_NOSIZE | SWP_NOCLIENTSIZE | SWP_NOZORDER)
44 #define SWP_AGG_NOPOSCHANGE \
45 (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
46 #define SWP_AGG_STATUSFLAGS \
47 (SWP_AGG_NOPOSCHANGE | SWP_FRAMECHANGED | SWP_HIDEWINDOW | SWP_SHOWWINDOW)
48 #define SWP_AGG_NOCLIENTCHANGE \
49 (SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE)
51 #define HAS_DLGFRAME(style,exStyle) \
52 (((exStyle) & WS_EX_DLGMODALFRAME) || \
53 (((style) & WS_DLGFRAME) && !((style) & WS_BORDER)))
55 #define HAS_THICKFRAME(style) \
56 (((style) & WS_THICKFRAME) && \
57 !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
59 #define EMPTYPOINT(pt) ((pt).x == -1 && (pt).y == -1)
61 #define ON_LEFT_BORDER(hit) \
62 (((hit) == HTLEFT) || ((hit) == HTTOPLEFT) || ((hit) == HTBOTTOMLEFT))
63 #define ON_RIGHT_BORDER(hit) \
64 (((hit) == HTRIGHT) || ((hit) == HTTOPRIGHT) || ((hit) == HTBOTTOMRIGHT))
65 #define ON_TOP_BORDER(hit) \
66 (((hit) == HTTOP) || ((hit) == HTTOPLEFT) || ((hit) == HTTOPRIGHT))
67 #define ON_BOTTOM_BORDER(hit) \
68 (((hit) == HTBOTTOM) || ((hit) == HTBOTTOMLEFT) || ((hit) == HTBOTTOMRIGHT))
70 #define PLACE_MIN 0x0001
71 #define PLACE_MAX 0x0002
72 #define PLACE_RECT 0x0004
74 typedef struct
76 struct user_object obj;
77 INT actualCount;
78 INT suggestedCount;
79 HWND hwndParent;
80 WINDOWPOS *winPos;
81 } DWP;
84 /***********************************************************************
85 * SwitchToThisWindow (USER32.@)
87 void WINAPI SwitchToThisWindow( HWND hwnd, BOOL alt_tab )
89 if (IsIconic( hwnd )) ShowWindow( hwnd, SW_RESTORE );
90 else BringWindowToTop( hwnd );
94 /***********************************************************************
95 * GetWindowRect (USER32.@)
97 BOOL WINAPI GetWindowRect( HWND hwnd, LPRECT rect )
99 BOOL ret = WIN_GetRectangles( hwnd, COORDS_SCREEN, rect, NULL );
100 if (ret) TRACE( "hwnd %p %s\n", hwnd, wine_dbgstr_rect(rect) );
101 return ret;
105 /***********************************************************************
106 * GetWindowRgn (USER32.@)
108 int WINAPI GetWindowRgn ( HWND hwnd, HRGN hrgn )
110 int nRet = ERROR;
111 NTSTATUS status;
112 HRGN win_rgn = 0;
113 RGNDATA *data;
114 size_t size = 256;
118 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
120 SetLastError( ERROR_OUTOFMEMORY );
121 return ERROR;
123 SERVER_START_REQ( get_window_region )
125 req->window = wine_server_user_handle( hwnd );
126 wine_server_set_reply( req, data->Buffer, size );
127 if (!(status = wine_server_call( req )))
129 size_t reply_size = wine_server_reply_size( reply );
130 if (reply_size)
132 data->rdh.dwSize = sizeof(data->rdh);
133 data->rdh.iType = RDH_RECTANGLES;
134 data->rdh.nCount = reply_size / sizeof(RECT);
135 data->rdh.nRgnSize = reply_size;
136 win_rgn = ExtCreateRegion( NULL, data->rdh.dwSize + data->rdh.nRgnSize, data );
139 else size = reply->total_size;
141 SERVER_END_REQ;
142 HeapFree( GetProcessHeap(), 0, data );
143 } while (status == STATUS_BUFFER_OVERFLOW);
145 if (status) SetLastError( RtlNtStatusToDosError(status) );
146 else if (win_rgn)
148 nRet = CombineRgn( hrgn, win_rgn, 0, RGN_COPY );
149 DeleteObject( win_rgn );
151 return nRet;
154 /***********************************************************************
155 * GetWindowRgnBox (USER32.@)
157 int WINAPI GetWindowRgnBox( HWND hwnd, LPRECT prect )
159 int ret = ERROR;
160 HRGN hrgn;
162 if (!prect)
163 return ERROR;
165 if ((hrgn = CreateRectRgn(0, 0, 0, 0)))
167 if ((ret = GetWindowRgn( hwnd, hrgn )) != ERROR )
168 ret = GetRgnBox( hrgn, prect );
169 DeleteObject(hrgn);
172 return ret;
175 /***********************************************************************
176 * SetWindowRgn (USER32.@)
178 int WINAPI SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL bRedraw )
180 static const RECT empty_rect;
181 BOOL ret;
183 if (hrgn)
185 RGNDATA *data;
186 DWORD size;
188 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
189 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
190 if (!GetRegionData( hrgn, size, data ))
192 HeapFree( GetProcessHeap(), 0, data );
193 return FALSE;
195 SERVER_START_REQ( set_window_region )
197 req->window = wine_server_user_handle( hwnd );
198 req->redraw = (bRedraw != 0);
199 if (data->rdh.nCount)
200 wine_server_add_data( req, data->Buffer, data->rdh.nCount * sizeof(RECT) );
201 else
202 wine_server_add_data( req, &empty_rect, sizeof(empty_rect) );
203 ret = !wine_server_call_err( req );
205 SERVER_END_REQ;
206 HeapFree( GetProcessHeap(), 0, data );
208 else /* clear existing region */
210 SERVER_START_REQ( set_window_region )
212 req->window = wine_server_user_handle( hwnd );
213 req->redraw = (bRedraw != 0);
214 ret = !wine_server_call_err( req );
216 SERVER_END_REQ;
219 if (ret)
221 UINT swp_flags = SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE;
222 if (!bRedraw) swp_flags |= SWP_NOREDRAW;
223 SetWindowPos( hwnd, 0, 0, 0, 0, 0, swp_flags );
224 USER_Driver->pSetWindowRgn( hwnd, hrgn, bRedraw );
225 if (hrgn) DeleteObject( hrgn );
227 return ret;
231 /***********************************************************************
232 * GetClientRect (USER32.@)
234 BOOL WINAPI GetClientRect( HWND hwnd, LPRECT rect )
236 return WIN_GetRectangles( hwnd, COORDS_CLIENT, NULL, rect );
240 /***********************************************************************
241 * list_children_from_point
243 * Get the list of children that can contain point from the server.
244 * Point is in screen coordinates.
245 * Returned list must be freed by caller.
247 static HWND *list_children_from_point( HWND hwnd, POINT pt )
249 HWND *list;
250 int i, size = 128;
252 for (;;)
254 int count = 0;
256 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
258 SERVER_START_REQ( get_window_children_from_point )
260 req->parent = wine_server_user_handle( hwnd );
261 req->x = pt.x;
262 req->y = pt.y;
263 wine_server_set_reply( req, list, (size-1) * sizeof(user_handle_t) );
264 if (!wine_server_call( req )) count = reply->count;
266 SERVER_END_REQ;
267 if (count && count < size)
269 /* start from the end since HWND is potentially larger than user_handle_t */
270 for (i = count - 1; i >= 0; i--)
271 list[i] = wine_server_ptr_handle( ((user_handle_t *)list)[i] );
272 list[count] = 0;
273 return list;
275 HeapFree( GetProcessHeap(), 0, list );
276 if (!count) break;
277 size = count + 1; /* restart with a large enough buffer */
279 return NULL;
283 /***********************************************************************
284 * WINPOS_WindowFromPoint
286 * Find the window and hittest for a given point.
288 HWND WINPOS_WindowFromPoint( HWND hwndScope, POINT pt, INT *hittest )
290 int i, res;
291 HWND ret, *list;
293 if (!hwndScope) hwndScope = GetDesktopWindow();
295 *hittest = HTNOWHERE;
297 if (!(list = list_children_from_point( hwndScope, pt ))) return 0;
299 /* now determine the hittest */
301 for (i = 0; list[i]; i++)
303 LONG style = GetWindowLongW( list[i], GWL_STYLE );
305 /* If window is minimized or disabled, return at once */
306 if (style & WS_MINIMIZE)
308 *hittest = HTCAPTION;
309 break;
311 if (style & WS_DISABLED)
313 *hittest = HTERROR;
314 break;
316 /* Send WM_NCCHITTEST (if same thread) */
317 if (!WIN_IsCurrentThread( list[i] ))
319 *hittest = HTCLIENT;
320 break;
322 res = SendMessageW( list[i], WM_NCHITTEST, 0, MAKELONG(pt.x,pt.y) );
323 if (res != HTTRANSPARENT)
325 *hittest = res; /* Found the window */
326 break;
328 /* continue search with next window in z-order */
330 ret = list[i];
331 HeapFree( GetProcessHeap(), 0, list );
332 TRACE( "scope %p (%d,%d) returning %p\n", hwndScope, pt.x, pt.y, ret );
333 return ret;
337 /*******************************************************************
338 * WindowFromPoint (USER32.@)
340 HWND WINAPI WindowFromPoint( POINT pt )
342 INT hittest;
343 return WINPOS_WindowFromPoint( 0, pt, &hittest );
347 /*******************************************************************
348 * ChildWindowFromPoint (USER32.@)
350 HWND WINAPI ChildWindowFromPoint( HWND hwndParent, POINT pt )
352 return ChildWindowFromPointEx( hwndParent, pt, CWP_ALL );
355 /*******************************************************************
356 * RealChildWindowFromPoint (USER32.@)
358 HWND WINAPI RealChildWindowFromPoint( HWND hwndParent, POINT pt )
360 return ChildWindowFromPointEx( hwndParent, pt, CWP_SKIPTRANSPARENT | CWP_SKIPINVISIBLE );
363 /*******************************************************************
364 * ChildWindowFromPointEx (USER32.@)
366 HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
368 /* pt is in the client coordinates */
369 HWND *list;
370 int i;
371 RECT rect;
372 HWND retvalue;
374 GetClientRect( hwndParent, &rect );
375 if (!PtInRect( &rect, pt )) return 0;
376 if (!(list = WIN_ListChildren( hwndParent ))) return hwndParent;
378 for (i = 0; list[i]; i++)
380 if (!WIN_GetRectangles( list[i], COORDS_PARENT, &rect, NULL )) continue;
381 if (!PtInRect( &rect, pt )) continue;
382 if (uFlags & (CWP_SKIPINVISIBLE|CWP_SKIPDISABLED))
384 LONG style = GetWindowLongW( list[i], GWL_STYLE );
385 if ((uFlags & CWP_SKIPINVISIBLE) && !(style & WS_VISIBLE)) continue;
386 if ((uFlags & CWP_SKIPDISABLED) && (style & WS_DISABLED)) continue;
388 if (uFlags & CWP_SKIPTRANSPARENT)
390 if (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TRANSPARENT) continue;
392 break;
394 retvalue = list[i];
395 HeapFree( GetProcessHeap(), 0, list );
396 if (!retvalue) retvalue = hwndParent;
397 return retvalue;
401 /*******************************************************************
402 * WINPOS_GetWinOffset
404 * Calculate the offset between the origin of the two windows. Used
405 * to implement MapWindowPoints.
407 static BOOL WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo, BOOL *mirrored, POINT *ret_offset )
409 WND * wndPtr;
410 POINT offset;
411 BOOL mirror_from, mirror_to, ret;
412 HWND hwnd;
414 offset.x = offset.y = 0;
415 *mirrored = mirror_from = mirror_to = FALSE;
417 /* Translate source window origin to screen coords */
418 if (hwndFrom)
420 if (!(wndPtr = WIN_GetPtr( hwndFrom )))
422 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
423 return FALSE;
425 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
426 if (wndPtr != WND_DESKTOP)
428 if (wndPtr->dwExStyle & WS_EX_LAYOUTRTL)
430 mirror_from = TRUE;
431 offset.x += wndPtr->rectClient.right - wndPtr->rectClient.left;
433 while (wndPtr->parent)
435 offset.x += wndPtr->rectClient.left;
436 offset.y += wndPtr->rectClient.top;
437 hwnd = wndPtr->parent;
438 WIN_ReleasePtr( wndPtr );
439 if (!(wndPtr = WIN_GetPtr( hwnd ))) break;
440 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
441 if (wndPtr == WND_DESKTOP) break;
442 if (wndPtr->flags & WIN_CHILDREN_MOVED)
444 WIN_ReleasePtr( wndPtr );
445 goto other_process;
448 if (wndPtr && wndPtr != WND_DESKTOP) WIN_ReleasePtr( wndPtr );
452 /* Translate origin to destination window coords */
453 if (hwndTo)
455 if (!(wndPtr = WIN_GetPtr( hwndTo )))
457 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
458 return FALSE;
460 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
461 if (wndPtr != WND_DESKTOP)
463 if (wndPtr->dwExStyle & WS_EX_LAYOUTRTL)
465 mirror_to = TRUE;
466 offset.x -= wndPtr->rectClient.right - wndPtr->rectClient.left;
468 while (wndPtr->parent)
470 offset.x -= wndPtr->rectClient.left;
471 offset.y -= wndPtr->rectClient.top;
472 hwnd = wndPtr->parent;
473 WIN_ReleasePtr( wndPtr );
474 if (!(wndPtr = WIN_GetPtr( hwnd ))) break;
475 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
476 if (wndPtr == WND_DESKTOP) break;
477 if (wndPtr->flags & WIN_CHILDREN_MOVED)
479 WIN_ReleasePtr( wndPtr );
480 goto other_process;
483 if (wndPtr && wndPtr != WND_DESKTOP) WIN_ReleasePtr( wndPtr );
487 *mirrored = mirror_from ^ mirror_to;
488 if (mirror_from) offset.x = -offset.x;
489 *ret_offset = offset;
490 return TRUE;
492 other_process: /* one of the parents may belong to another process, do it the hard way */
493 SERVER_START_REQ( get_windows_offset )
495 req->from = wine_server_user_handle( hwndFrom );
496 req->to = wine_server_user_handle( hwndTo );
497 if ((ret = !wine_server_call_err( req )))
499 ret_offset->x = reply->x;
500 ret_offset->y = reply->y;
501 *mirrored = reply->mirror;
504 SERVER_END_REQ;
505 return ret;
508 /* map coordinates of a window region */
509 void map_window_region( HWND from, HWND to, HRGN hrgn )
511 BOOL mirrored;
512 POINT offset;
513 UINT i, size;
514 RGNDATA *data;
515 HRGN new_rgn;
516 RECT *rect;
518 if (!WINPOS_GetWinOffset( from, to, &mirrored, &offset )) return;
520 if (!mirrored)
522 OffsetRgn( hrgn, offset.x, offset.y );
523 return;
525 if (!(size = GetRegionData( hrgn, 0, NULL ))) return;
526 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return;
527 GetRegionData( hrgn, size, data );
528 rect = (RECT *)data->Buffer;
529 for (i = 0; i < data->rdh.nCount; i++)
531 int tmp = -(rect[i].left + offset.x);
532 rect[i].left = -(rect[i].right + offset.x);
533 rect[i].right = tmp;
534 rect[i].top += offset.y;
535 rect[i].bottom += offset.y;
537 if ((new_rgn = ExtCreateRegion( NULL, data->rdh.dwSize + data->rdh.nRgnSize, data )))
539 CombineRgn( hrgn, new_rgn, 0, RGN_COPY );
540 DeleteObject( new_rgn );
542 HeapFree( GetProcessHeap(), 0, data );
546 /*******************************************************************
547 * MapWindowPoints (USER32.@)
549 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
551 BOOL mirrored;
552 POINT offset;
553 UINT i;
555 if (!WINPOS_GetWinOffset( hwndFrom, hwndTo, &mirrored, &offset )) return 0;
557 for (i = 0; i < count; i++)
559 lppt[i].x += offset.x;
560 lppt[i].y += offset.y;
561 if (mirrored) lppt[i].x = -lppt[i].x;
563 if (mirrored && count == 2) /* special case for rectangle */
565 int tmp = lppt[0].x;
566 lppt[0].x = lppt[1].x;
567 lppt[1].x = tmp;
569 return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
573 /*******************************************************************
574 * ClientToScreen (USER32.@)
576 BOOL WINAPI ClientToScreen( HWND hwnd, LPPOINT lppnt )
578 POINT offset;
579 BOOL mirrored;
581 if (!hwnd)
583 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
584 return FALSE;
586 if (!WINPOS_GetWinOffset( hwnd, 0, &mirrored, &offset )) return FALSE;
587 lppnt->x += offset.x;
588 lppnt->y += offset.y;
589 if (mirrored) lppnt->x = -lppnt->x;
590 return TRUE;
594 /*******************************************************************
595 * ScreenToClient (USER32.@)
597 BOOL WINAPI ScreenToClient( HWND hwnd, LPPOINT lppnt )
599 POINT offset;
600 BOOL mirrored;
602 if (!hwnd)
604 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
605 return FALSE;
607 if (!WINPOS_GetWinOffset( 0, hwnd, &mirrored, &offset )) return FALSE;
608 lppnt->x += offset.x;
609 lppnt->y += offset.y;
610 if (mirrored) lppnt->x = -lppnt->x;
611 return TRUE;
615 /***********************************************************************
616 * IsIconic (USER32.@)
618 BOOL WINAPI IsIconic(HWND hWnd)
620 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
624 /***********************************************************************
625 * IsZoomed (USER32.@)
627 BOOL WINAPI IsZoomed(HWND hWnd)
629 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
633 /*******************************************************************
634 * AllowSetForegroundWindow (USER32.@)
636 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
638 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
639 * implemented, then fix this function. */
640 return TRUE;
644 /*******************************************************************
645 * LockSetForegroundWindow (USER32.@)
647 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
649 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
650 * implemented, then fix this function. */
651 return TRUE;
655 /***********************************************************************
656 * BringWindowToTop (USER32.@)
658 BOOL WINAPI BringWindowToTop( HWND hwnd )
660 return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
664 /***********************************************************************
665 * MoveWindow (USER32.@)
667 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
668 BOOL repaint )
670 int flags = SWP_NOZORDER | SWP_NOACTIVATE;
671 if (!repaint) flags |= SWP_NOREDRAW;
672 TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
673 return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
677 /***********************************************************************
678 * WINPOS_RedrawIconTitle
680 BOOL WINPOS_RedrawIconTitle( HWND hWnd )
682 HWND icon_title = 0;
683 WND *win = WIN_GetPtr( hWnd );
685 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
687 icon_title = win->icon_title;
688 WIN_ReleasePtr( win );
690 if (!icon_title) return FALSE;
691 SendMessageW( icon_title, WM_SHOWWINDOW, TRUE, 0 );
692 InvalidateRect( icon_title, NULL, TRUE );
693 return TRUE;
696 /***********************************************************************
697 * WINPOS_ShowIconTitle
699 static void WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
701 WND *win = WIN_GetPtr( hwnd );
702 HWND title = 0;
704 TRACE("%p %i\n", hwnd, (bShow != 0) );
706 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP) return;
707 if (win->rectWindow.left == -32000 || win->rectWindow.top == -32000)
709 TRACE( "not showing title for hidden icon %p\n", hwnd );
710 bShow = FALSE;
712 else title = win->icon_title;
713 WIN_ReleasePtr( win );
715 if (bShow)
717 if (!title)
719 title = ICONTITLE_Create( hwnd );
720 if (!(win = WIN_GetPtr( hwnd )) || win == WND_OTHER_PROCESS)
722 DestroyWindow( title );
723 return;
725 win->icon_title = title;
726 WIN_ReleasePtr( win );
728 if (!IsWindowVisible(title))
730 SendMessageW( title, WM_SHOWWINDOW, TRUE, 0 );
731 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
732 SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
735 else if (title) ShowWindow( title, SW_HIDE );
738 /*******************************************************************
739 * WINPOS_GetMinMaxInfo
741 * Get the minimized and maximized information for a window.
743 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
744 POINT *minTrack, POINT *maxTrack )
746 MINMAXINFO MinMax;
747 HMONITOR monitor;
748 INT xinc, yinc;
749 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
750 LONG adjustedStyle;
751 LONG exstyle = GetWindowLongW( hwnd, GWL_EXSTYLE );
752 RECT rc;
753 WND *win;
755 /* Compute default values */
757 GetWindowRect(hwnd, &rc);
758 MinMax.ptReserved.x = rc.left;
759 MinMax.ptReserved.y = rc.top;
761 if ((style & WS_CAPTION) == WS_CAPTION)
762 adjustedStyle = style & ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
763 else
764 adjustedStyle = style;
766 GetClientRect(GetAncestor(hwnd,GA_PARENT), &rc);
767 AdjustWindowRectEx(&rc, adjustedStyle, ((style & WS_POPUP) && GetMenu(hwnd)), exstyle);
769 xinc = -rc.left;
770 yinc = -rc.top;
772 MinMax.ptMaxSize.x = rc.right - rc.left;
773 MinMax.ptMaxSize.y = rc.bottom - rc.top;
774 if (style & (WS_DLGFRAME | WS_BORDER))
776 MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
777 MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
779 else
781 MinMax.ptMinTrackSize.x = 2 * xinc;
782 MinMax.ptMinTrackSize.y = 2 * yinc;
784 MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXMAXTRACK);
785 MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYMAXTRACK);
786 MinMax.ptMaxPosition.x = -xinc;
787 MinMax.ptMaxPosition.y = -yinc;
789 if ((win = WIN_GetPtr( hwnd )) && win != WND_DESKTOP && win != WND_OTHER_PROCESS)
791 if (!EMPTYPOINT(win->max_pos)) MinMax.ptMaxPosition = win->max_pos;
792 WIN_ReleasePtr( win );
795 SendMessageW( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
797 /* if the app didn't change the values, adapt them for the current monitor */
799 if ((monitor = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY )))
801 RECT rc_work;
802 MONITORINFO mon_info;
804 mon_info.cbSize = sizeof(mon_info);
805 GetMonitorInfoW( monitor, &mon_info );
807 rc_work = mon_info.rcMonitor;
809 if (style & WS_MAXIMIZEBOX)
811 if ((style & WS_CAPTION) == WS_CAPTION || !(style & (WS_CHILD | WS_POPUP)))
812 rc_work = mon_info.rcWork;
815 if (MinMax.ptMaxSize.x == GetSystemMetrics(SM_CXSCREEN) + 2 * xinc &&
816 MinMax.ptMaxSize.y == GetSystemMetrics(SM_CYSCREEN) + 2 * yinc)
818 MinMax.ptMaxSize.x = (rc_work.right - rc_work.left) + 2 * xinc;
819 MinMax.ptMaxSize.y = (rc_work.bottom - rc_work.top) + 2 * yinc;
821 if (MinMax.ptMaxPosition.x == -xinc && MinMax.ptMaxPosition.y == -yinc)
823 MinMax.ptMaxPosition.x = rc_work.left - xinc;
824 MinMax.ptMaxPosition.y = rc_work.top - yinc;
828 /* Some sanity checks */
830 TRACE("%d %d / %d %d / %d %d / %d %d\n",
831 MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
832 MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
833 MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
834 MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
835 MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
836 MinMax.ptMinTrackSize.x );
837 MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
838 MinMax.ptMinTrackSize.y );
840 if (maxSize) *maxSize = MinMax.ptMaxSize;
841 if (maxPos) *maxPos = MinMax.ptMaxPosition;
842 if (minTrack) *minTrack = MinMax.ptMinTrackSize;
843 if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
847 /***********************************************************************
848 * WINPOS_FindIconPos
850 * Find a suitable place for an iconic window.
852 static POINT WINPOS_FindIconPos( HWND hwnd, POINT pt )
854 RECT rect, rectParent;
855 HWND parent, child;
856 HRGN hrgn, tmp;
857 int x, y, xspacing, yspacing;
858 MINIMIZEDMETRICS metrics;
860 metrics.cbSize = sizeof(metrics);
861 SystemParametersInfoW( SPI_GETMINIMIZEDMETRICS, sizeof(metrics), &metrics, 0 );
863 parent = GetAncestor( hwnd, GA_PARENT );
864 if (parent == GetDesktopWindow())
866 MONITORINFO mon_info;
867 HMONITOR monitor = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY );
869 mon_info.cbSize = sizeof( mon_info );
870 GetMonitorInfoW( monitor, &mon_info );
871 rectParent = mon_info.rcWork;
873 else GetClientRect( parent, &rectParent );
875 if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
876 (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
877 return pt; /* The icon already has a suitable position */
879 xspacing = GetSystemMetrics(SM_CXICONSPACING);
880 yspacing = GetSystemMetrics(SM_CYICONSPACING);
882 /* Check if another icon already occupies this spot */
883 /* FIXME: this is completely inefficient */
885 hrgn = CreateRectRgn( 0, 0, 0, 0 );
886 tmp = CreateRectRgn( 0, 0, 0, 0 );
887 for (child = GetWindow( parent, GW_CHILD ); child; child = GetWindow( child, GW_HWNDNEXT ))
889 if (child == hwnd) continue;
890 if ((GetWindowLongW( child, GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != (WS_VISIBLE|WS_MINIMIZE))
891 continue;
892 if (WIN_GetRectangles( child, COORDS_PARENT, &rect, NULL ))
894 SetRectRgn( tmp, rect.left, rect.top, rect.right, rect.bottom );
895 CombineRgn( hrgn, hrgn, tmp, RGN_OR );
898 DeleteObject( tmp );
900 for (y = 0; y < (rectParent.bottom - rectParent.top) / yspacing; y++)
902 if (metrics.iArrange & ARW_STARTTOP)
904 rect.top = rectParent.top + y * yspacing;
905 rect.bottom = rect.top + yspacing;
907 else
909 rect.bottom = rectParent.bottom - y * yspacing;
910 rect.top = rect.bottom - yspacing;
912 for (x = 0; x < (rectParent.right - rectParent.left) / xspacing; x++)
914 if (metrics.iArrange & ARW_STARTRIGHT)
916 rect.right = rectParent.right - x * xspacing;
917 rect.left = rect.right - xspacing;
919 else
921 rect.left = rectParent.left + x * xspacing;
922 rect.right = rect.left + xspacing;
924 if (!RectInRegion( hrgn, &rect ))
926 /* No window was found, so it's OK for us */
927 pt.x = rect.left + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
928 pt.y = rect.top + (yspacing - GetSystemMetrics(SM_CYICON)) / 2;
929 DeleteObject( hrgn );
930 return pt;
934 DeleteObject( hrgn );
935 pt.x = pt.y = 0;
936 return pt;
940 /***********************************************************************
941 * WINPOS_MinMaximize
943 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
945 UINT swpFlags = 0;
946 POINT size;
947 LONG old_style;
948 WINDOWPLACEMENT wpl;
950 TRACE("%p %u\n", hwnd, cmd );
952 wpl.length = sizeof(wpl);
953 GetWindowPlacement( hwnd, &wpl );
955 if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
956 return SWP_NOSIZE | SWP_NOMOVE;
958 if (IsIconic( hwnd ))
960 switch (cmd)
962 case SW_SHOWMINNOACTIVE:
963 case SW_SHOWMINIMIZED:
964 case SW_FORCEMINIMIZE:
965 case SW_MINIMIZE:
966 wpl.ptMinPosition = WINPOS_FindIconPos( hwnd, wpl.ptMinPosition );
968 SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
969 wpl.ptMinPosition.x + GetSystemMetrics(SM_CXICON),
970 wpl.ptMinPosition.y + GetSystemMetrics(SM_CYICON) );
971 return SWP_NOSIZE | SWP_NOMOVE;
973 if (!SendMessageW( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
974 swpFlags |= SWP_NOCOPYBITS;
977 switch( cmd )
979 case SW_SHOWMINNOACTIVE:
980 case SW_SHOWMINIMIZED:
981 case SW_FORCEMINIMIZE:
982 case SW_MINIMIZE:
983 if (IsZoomed( hwnd )) win_set_flags( hwnd, WIN_RESTORE_MAX, 0 );
984 else win_set_flags( hwnd, 0, WIN_RESTORE_MAX );
986 old_style = WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
988 wpl.ptMinPosition = WINPOS_FindIconPos( hwnd, wpl.ptMinPosition );
990 if (!(old_style & WS_MINIMIZE)) swpFlags |= SWP_STATECHANGED;
991 SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
992 wpl.ptMinPosition.x + GetSystemMetrics(SM_CXICON),
993 wpl.ptMinPosition.y + GetSystemMetrics(SM_CYICON) );
994 swpFlags |= SWP_NOCOPYBITS;
995 break;
997 case SW_MAXIMIZE:
998 old_style = GetWindowLongW( hwnd, GWL_STYLE );
999 if ((old_style & WS_MAXIMIZE) && (old_style & WS_VISIBLE)) return SWP_NOSIZE | SWP_NOMOVE;
1001 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
1003 old_style = WIN_SetStyle( hwnd, WS_MAXIMIZE, WS_MINIMIZE );
1004 if (old_style & WS_MINIMIZE)
1006 win_set_flags( hwnd, WIN_RESTORE_MAX, 0 );
1007 WINPOS_ShowIconTitle( hwnd, FALSE );
1010 if (!(old_style & WS_MAXIMIZE)) swpFlags |= SWP_STATECHANGED;
1011 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y,
1012 wpl.ptMaxPosition.x + size.x, wpl.ptMaxPosition.y + size.y );
1013 break;
1015 case SW_SHOWNOACTIVATE:
1016 win_set_flags( hwnd, 0, WIN_RESTORE_MAX );
1017 /* fall through */
1018 case SW_SHOWNORMAL:
1019 case SW_RESTORE:
1020 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
1021 old_style = WIN_SetStyle( hwnd, 0, WS_MINIMIZE | WS_MAXIMIZE );
1022 if (old_style & WS_MINIMIZE)
1024 WINPOS_ShowIconTitle( hwnd, FALSE );
1025 if (win_get_flags( hwnd ) & WIN_RESTORE_MAX)
1027 /* Restore to maximized position */
1028 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
1029 WIN_SetStyle( hwnd, WS_MAXIMIZE, 0 );
1030 swpFlags |= SWP_STATECHANGED;
1031 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y,
1032 wpl.ptMaxPosition.x + size.x, wpl.ptMaxPosition.y + size.y );
1033 break;
1036 else if (!(old_style & WS_MAXIMIZE)) break;
1038 swpFlags |= SWP_STATECHANGED;
1040 /* Restore to normal position */
1042 *rect = wpl.rcNormalPosition;
1043 break;
1046 return swpFlags;
1050 /***********************************************************************
1051 * show_window
1053 * Implementation of ShowWindow and ShowWindowAsync.
1055 static BOOL show_window( HWND hwnd, INT cmd )
1057 WND *wndPtr;
1058 HWND parent;
1059 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1060 BOOL wasVisible = (style & WS_VISIBLE) != 0;
1061 BOOL showFlag = TRUE;
1062 RECT newPos = {0, 0, 0, 0};
1063 UINT swp = 0;
1065 TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);
1067 switch(cmd)
1069 case SW_HIDE:
1070 if (!wasVisible) return FALSE;
1071 showFlag = FALSE;
1072 swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1073 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1074 break;
1076 case SW_SHOWMINNOACTIVE:
1077 case SW_MINIMIZE:
1078 case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
1079 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1080 /* fall through */
1081 case SW_SHOWMINIMIZED:
1082 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1083 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
1084 if ((style & WS_MINIMIZE) && wasVisible) return TRUE;
1085 break;
1087 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
1088 if (!wasVisible) swp |= SWP_SHOWWINDOW;
1089 swp |= SWP_FRAMECHANGED;
1090 swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
1091 if ((style & WS_MAXIMIZE) && wasVisible) return TRUE;
1092 break;
1094 case SW_SHOWNA:
1095 swp |= SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1096 if (style & WS_CHILD) swp |= SWP_NOZORDER;
1097 break;
1098 case SW_SHOW:
1099 if (wasVisible) return TRUE;
1100 swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1101 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1102 break;
1104 case SW_SHOWNOACTIVATE:
1105 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1106 /* fall through */
1107 case SW_RESTORE:
1108 /* fall through */
1109 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
1110 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
1111 if (!wasVisible) swp |= SWP_SHOWWINDOW;
1112 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1114 swp |= SWP_FRAMECHANGED;
1115 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
1117 else
1119 if (wasVisible) return TRUE;
1120 swp |= SWP_NOSIZE | SWP_NOMOVE;
1122 if (style & WS_CHILD && !(swp & SWP_STATECHANGED)) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1123 break;
1124 default:
1125 return wasVisible;
1128 if ((showFlag != wasVisible || cmd == SW_SHOWNA) && cmd != SW_SHOWMAXIMIZED && !(swp & SWP_STATECHANGED))
1130 SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
1131 if (!IsWindow( hwnd )) return wasVisible;
1134 swp = USER_Driver->pShowWindow( hwnd, cmd, &newPos, swp );
1136 parent = GetAncestor( hwnd, GA_PARENT );
1137 if (parent && !IsWindowVisible( parent ) && !(swp & SWP_STATECHANGED))
1139 /* if parent is not visible simply toggle WS_VISIBLE and return */
1140 if (showFlag) WIN_SetStyle( hwnd, WS_VISIBLE, 0 );
1141 else WIN_SetStyle( hwnd, 0, WS_VISIBLE );
1143 else
1144 SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1145 newPos.right - newPos.left, newPos.bottom - newPos.top, swp );
1147 if (cmd == SW_HIDE)
1149 HWND hFocus;
1151 WINPOS_ShowIconTitle( hwnd, FALSE );
1153 /* FIXME: This will cause the window to be activated irrespective
1154 * of whether it is owned by the same thread. Has to be done
1155 * asynchronously.
1158 if (hwnd == GetActiveWindow())
1159 WINPOS_ActivateOtherWindow(hwnd);
1161 /* Revert focus to parent */
1162 hFocus = GetFocus();
1163 if (hwnd == hFocus)
1165 HWND parent = GetAncestor(hwnd, GA_PARENT);
1166 if (parent == GetDesktopWindow()) parent = 0;
1167 SetFocus(parent);
1169 return wasVisible;
1172 if (IsIconic(hwnd)) WINPOS_ShowIconTitle( hwnd, TRUE );
1174 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return wasVisible;
1176 if (wndPtr->flags & WIN_NEED_SIZE)
1178 /* should happen only in CreateWindowEx() */
1179 int wParam = SIZE_RESTORED;
1180 RECT client;
1181 LPARAM lparam;
1183 WIN_GetRectangles( hwnd, COORDS_PARENT, NULL, &client );
1184 lparam = MAKELONG( client.right - client.left, client.bottom - client.top );
1185 wndPtr->flags &= ~WIN_NEED_SIZE;
1186 if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1187 else if (wndPtr->dwStyle & WS_MINIMIZE)
1189 wParam = SIZE_MINIMIZED;
1190 lparam = 0;
1192 WIN_ReleasePtr( wndPtr );
1194 SendMessageW( hwnd, WM_SIZE, wParam, lparam );
1195 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( client.left, client.top ));
1197 else WIN_ReleasePtr( wndPtr );
1199 /* if previous state was minimized Windows sets focus to the window */
1200 if (style & WS_MINIMIZE) SetFocus( hwnd );
1202 return wasVisible;
1206 /***********************************************************************
1207 * ShowWindowAsync (USER32.@)
1209 * doesn't wait; returns immediately.
1210 * used by threads to toggle windows in other (possibly hanging) threads
1212 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
1214 HWND full_handle;
1216 if (is_broadcast(hwnd))
1218 SetLastError( ERROR_INVALID_PARAMETER );
1219 return FALSE;
1222 if ((full_handle = WIN_IsCurrentThread( hwnd )))
1223 return show_window( full_handle, cmd );
1225 return SendNotifyMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
1229 /***********************************************************************
1230 * ShowWindow (USER32.@)
1232 BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
1234 HWND full_handle;
1236 if (is_broadcast(hwnd))
1238 SetLastError( ERROR_INVALID_PARAMETER );
1239 return FALSE;
1241 if ((full_handle = WIN_IsCurrentThread( hwnd )))
1242 return show_window( full_handle, cmd );
1244 if ((cmd == SW_HIDE) && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
1245 return FALSE;
1247 return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
1251 /***********************************************************************
1252 * GetInternalWindowPos (USER32.@)
1254 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
1255 LPPOINT ptIcon )
1257 WINDOWPLACEMENT wndpl;
1259 wndpl.length = sizeof(wndpl);
1260 if (GetWindowPlacement( hwnd, &wndpl ))
1262 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
1263 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
1264 return wndpl.showCmd;
1266 return 0;
1270 /***********************************************************************
1271 * GetWindowPlacement (USER32.@)
1273 * Win95:
1274 * Fails if wndpl->length of Win95 (!) apps is invalid.
1276 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
1278 WND *pWnd = WIN_GetPtr( hwnd );
1280 if (!pWnd) return FALSE;
1282 if (pWnd == WND_DESKTOP)
1284 wndpl->length = sizeof(*wndpl);
1285 wndpl->showCmd = SW_SHOWNORMAL;
1286 wndpl->flags = 0;
1287 wndpl->ptMinPosition.x = -1;
1288 wndpl->ptMinPosition.y = -1;
1289 wndpl->ptMaxPosition.x = -1;
1290 wndpl->ptMaxPosition.y = -1;
1291 GetWindowRect( hwnd, &wndpl->rcNormalPosition );
1292 return TRUE;
1294 if (pWnd == WND_OTHER_PROCESS)
1296 if (!IsWindow( hwnd )) return FALSE;
1297 FIXME( "not supported on other process window %p\n", hwnd );
1298 /* provide some dummy information */
1299 wndpl->length = sizeof(*wndpl);
1300 wndpl->showCmd = SW_SHOWNORMAL;
1301 wndpl->flags = 0;
1302 wndpl->ptMinPosition.x = -1;
1303 wndpl->ptMinPosition.y = -1;
1304 wndpl->ptMaxPosition.x = -1;
1305 wndpl->ptMaxPosition.y = -1;
1306 GetWindowRect( hwnd, &wndpl->rcNormalPosition );
1307 return TRUE;
1310 /* update the placement according to the current style */
1311 if (pWnd->dwStyle & WS_MINIMIZE)
1313 pWnd->min_pos.x = pWnd->rectWindow.left;
1314 pWnd->min_pos.y = pWnd->rectWindow.top;
1316 else if (pWnd->dwStyle & WS_MAXIMIZE)
1318 pWnd->max_pos.x = pWnd->rectWindow.left;
1319 pWnd->max_pos.y = pWnd->rectWindow.top;
1321 else
1323 pWnd->normal_rect = pWnd->rectWindow;
1326 wndpl->length = sizeof(*wndpl);
1327 if( pWnd->dwStyle & WS_MINIMIZE )
1328 wndpl->showCmd = SW_SHOWMINIMIZED;
1329 else
1330 wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
1331 if( pWnd->flags & WIN_RESTORE_MAX )
1332 wndpl->flags = WPF_RESTORETOMAXIMIZED;
1333 else
1334 wndpl->flags = 0;
1335 wndpl->ptMinPosition = pWnd->min_pos;
1336 wndpl->ptMaxPosition = pWnd->max_pos;
1337 wndpl->rcNormalPosition = pWnd->normal_rect;
1338 WIN_ReleasePtr( pWnd );
1340 TRACE( "%p: returning min %d,%d max %d,%d normal %s\n",
1341 hwnd, wndpl->ptMinPosition.x, wndpl->ptMinPosition.y,
1342 wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y,
1343 wine_dbgstr_rect(&wndpl->rcNormalPosition) );
1344 return TRUE;
1347 /* make sure the specified rect is visible on screen */
1348 static void make_rect_onscreen( RECT *rect )
1350 MONITORINFO info;
1351 HMONITOR monitor = MonitorFromRect( rect, MONITOR_DEFAULTTONEAREST );
1353 info.cbSize = sizeof(info);
1354 if (!monitor || !GetMonitorInfoW( monitor, &info )) return;
1355 /* FIXME: map coordinates from rcWork to rcMonitor */
1356 if (rect->right <= info.rcWork.left)
1358 rect->right += info.rcWork.left - rect->left;
1359 rect->left = info.rcWork.left;
1361 else if (rect->left >= info.rcWork.right)
1363 rect->left += info.rcWork.right - rect->right;
1364 rect->right = info.rcWork.right;
1366 if (rect->bottom <= info.rcWork.top)
1368 rect->bottom += info.rcWork.top - rect->top;
1369 rect->top = info.rcWork.top;
1371 else if (rect->top >= info.rcWork.bottom)
1373 rect->top += info.rcWork.bottom - rect->bottom;
1374 rect->bottom = info.rcWork.bottom;
1378 /* make sure the specified point is visible on screen */
1379 static void make_point_onscreen( POINT *pt )
1381 RECT rect;
1383 SetRect( &rect, pt->x, pt->y, pt->x + 1, pt->y + 1 );
1384 make_rect_onscreen( &rect );
1385 pt->x = rect.left;
1386 pt->y = rect.top;
1390 /***********************************************************************
1391 * WINPOS_SetPlacement
1393 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
1395 DWORD style;
1396 WND *pWnd = WIN_GetPtr( hwnd );
1397 WINDOWPLACEMENT wp = *wndpl;
1399 if (flags & PLACE_MIN) make_point_onscreen( &wp.ptMinPosition );
1400 if (flags & PLACE_MAX) make_point_onscreen( &wp.ptMaxPosition );
1401 if (flags & PLACE_RECT) make_rect_onscreen( &wp.rcNormalPosition );
1403 TRACE( "%p: setting min %d,%d max %d,%d normal %s flags %x ajusted to min %d,%d max %d,%d normal %s\n",
1404 hwnd, wndpl->ptMinPosition.x, wndpl->ptMinPosition.y,
1405 wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y,
1406 wine_dbgstr_rect(&wndpl->rcNormalPosition), flags,
1407 wp.ptMinPosition.x, wp.ptMinPosition.y, wp.ptMaxPosition.x, wp.ptMaxPosition.y,
1408 wine_dbgstr_rect(&wp.rcNormalPosition) );
1410 if (!pWnd || pWnd == WND_OTHER_PROCESS || pWnd == WND_DESKTOP) return FALSE;
1412 if( flags & PLACE_MIN ) pWnd->min_pos = wp.ptMinPosition;
1413 if( flags & PLACE_MAX ) pWnd->max_pos = wp.ptMaxPosition;
1414 if( flags & PLACE_RECT) pWnd->normal_rect = wp.rcNormalPosition;
1416 style = pWnd->dwStyle;
1418 WIN_ReleasePtr( pWnd );
1420 if( style & WS_MINIMIZE )
1422 if (flags & PLACE_MIN)
1424 WINPOS_ShowIconTitle( hwnd, FALSE );
1425 SetWindowPos( hwnd, 0, wp.ptMinPosition.x, wp.ptMinPosition.y, 0, 0,
1426 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1429 else if( style & WS_MAXIMIZE )
1431 if (flags & PLACE_MAX)
1432 SetWindowPos( hwnd, 0, wp.ptMaxPosition.x, wp.ptMaxPosition.y, 0, 0,
1433 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1435 else if( flags & PLACE_RECT )
1436 SetWindowPos( hwnd, 0, wp.rcNormalPosition.left, wp.rcNormalPosition.top,
1437 wp.rcNormalPosition.right - wp.rcNormalPosition.left,
1438 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top,
1439 SWP_NOZORDER | SWP_NOACTIVATE );
1441 ShowWindow( hwnd, wndpl->showCmd );
1443 if (IsIconic( hwnd ))
1445 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE) WINPOS_ShowIconTitle( hwnd, TRUE );
1447 /* SDK: ...valid only the next time... */
1448 if( wndpl->flags & WPF_RESTORETOMAXIMIZED )
1449 win_set_flags( hwnd, WIN_RESTORE_MAX, 0 );
1451 return TRUE;
1455 /***********************************************************************
1456 * SetWindowPlacement (USER32.@)
1458 * Win95:
1459 * Fails if wndpl->length of Win95 (!) apps is invalid.
1461 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
1463 UINT flags = PLACE_MAX | PLACE_RECT;
1464 if (!wpl) return FALSE;
1465 if (wpl->flags & WPF_SETMINPOSITION) flags |= PLACE_MIN;
1466 return WINPOS_SetPlacement( hwnd, wpl, flags );
1470 /***********************************************************************
1471 * AnimateWindow (USER32.@)
1472 * Shows/Hides a window with an animation
1473 * NO ANIMATION YET
1475 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
1477 FIXME("partial stub\n");
1479 /* If trying to show/hide and it's already *
1480 * shown/hidden or invalid window, fail with *
1481 * invalid parameter */
1482 if(!IsWindow(hwnd) ||
1483 (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
1484 (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
1486 SetLastError(ERROR_INVALID_PARAMETER);
1487 return FALSE;
1490 ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1492 return TRUE;
1495 /***********************************************************************
1496 * SetInternalWindowPos (USER32.@)
1498 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1499 LPRECT rect, LPPOINT pt )
1501 WINDOWPLACEMENT wndpl;
1502 UINT flags;
1504 wndpl.length = sizeof(wndpl);
1505 wndpl.showCmd = showCmd;
1506 wndpl.flags = flags = 0;
1508 if( pt )
1510 flags |= PLACE_MIN;
1511 wndpl.flags |= WPF_SETMINPOSITION;
1512 wndpl.ptMinPosition = *pt;
1514 if( rect )
1516 flags |= PLACE_RECT;
1517 wndpl.rcNormalPosition = *rect;
1519 WINPOS_SetPlacement( hwnd, &wndpl, flags );
1523 /*******************************************************************
1524 * can_activate_window
1526 * Check if we can activate the specified window.
1528 static BOOL can_activate_window( HWND hwnd )
1530 LONG style;
1532 if (!hwnd) return FALSE;
1533 style = GetWindowLongW( hwnd, GWL_STYLE );
1534 if (!(style & WS_VISIBLE)) return FALSE;
1535 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1536 return !(style & WS_DISABLED);
1540 /*******************************************************************
1541 * WINPOS_ActivateOtherWindow
1543 * Activates window other than pWnd.
1545 void WINPOS_ActivateOtherWindow(HWND hwnd)
1547 HWND hwndTo, fg;
1549 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1551 hwndTo = GetAncestor( hwndTo, GA_ROOT );
1552 if (can_activate_window( hwndTo )) goto done;
1555 hwndTo = hwnd;
1556 for (;;)
1558 if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1559 if (can_activate_window( hwndTo )) goto done;
1562 hwndTo = GetTopWindow( 0 );
1563 for (;;)
1565 if (hwndTo == hwnd)
1567 hwndTo = 0;
1568 break;
1570 if (can_activate_window( hwndTo )) goto done;
1571 if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1574 done:
1575 fg = GetForegroundWindow();
1576 TRACE("win = %p fg = %p\n", hwndTo, fg);
1577 if (!fg || (hwnd == fg))
1579 if (SetForegroundWindow( hwndTo )) return;
1581 if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1585 /***********************************************************************
1586 * WINPOS_HandleWindowPosChanging
1588 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1590 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1592 POINT minTrack, maxTrack;
1593 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1595 if (winpos->flags & SWP_NOSIZE) return 0;
1596 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1598 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1599 if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1600 if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1601 if (!(style & WS_MINIMIZE))
1603 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1604 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1607 return 0;
1611 /***********************************************************************
1612 * dump_winpos_flags
1614 static void dump_winpos_flags(UINT flags)
1616 static const DWORD dumped_flags = (SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOREDRAW |
1617 SWP_NOACTIVATE | SWP_FRAMECHANGED | SWP_SHOWWINDOW |
1618 SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOOWNERZORDER |
1619 SWP_NOSENDCHANGING | SWP_DEFERERASE | SWP_ASYNCWINDOWPOS |
1620 SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_STATECHANGED);
1621 TRACE("flags:");
1622 if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1623 if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1624 if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1625 if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1626 if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1627 if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1628 if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1629 if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1630 if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1631 if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1632 if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1633 if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1634 if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1635 if(flags & SWP_NOCLIENTSIZE) TRACE(" SWP_NOCLIENTSIZE");
1636 if(flags & SWP_NOCLIENTMOVE) TRACE(" SWP_NOCLIENTMOVE");
1637 if(flags & SWP_STATECHANGED) TRACE(" SWP_STATECHANGED");
1639 if(flags & ~dumped_flags) TRACE(" %08x", flags & ~dumped_flags);
1640 TRACE("\n");
1643 /***********************************************************************
1644 * SWP_DoWinPosChanging
1646 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
1648 WND *wndPtr;
1649 RECT window_rect, client_rect;
1651 /* Send WM_WINDOWPOSCHANGING message */
1653 if (!(pWinpos->flags & SWP_NOSENDCHANGING)
1654 && !((pWinpos->flags & SWP_AGG_NOCLIENTCHANGE) && (pWinpos->flags & SWP_SHOWWINDOW)))
1655 SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
1657 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) ||
1658 wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
1660 /* Calculate new position and size */
1662 WIN_GetRectangles( pWinpos->hwnd, COORDS_PARENT, &window_rect, &client_rect );
1663 *pNewWindowRect = window_rect;
1664 *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? window_rect : client_rect;
1666 if (!(pWinpos->flags & SWP_NOSIZE))
1668 if (wndPtr->dwStyle & WS_MINIMIZE)
1670 pNewWindowRect->right = pNewWindowRect->left + GetSystemMetrics(SM_CXICON);
1671 pNewWindowRect->bottom = pNewWindowRect->top + GetSystemMetrics(SM_CYICON);
1673 else
1675 pNewWindowRect->right = pNewWindowRect->left + pWinpos->cx;
1676 pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
1679 if (!(pWinpos->flags & SWP_NOMOVE))
1681 pNewWindowRect->left = pWinpos->x;
1682 pNewWindowRect->top = pWinpos->y;
1683 pNewWindowRect->right += pWinpos->x - window_rect.left;
1684 pNewWindowRect->bottom += pWinpos->y - window_rect.top;
1686 OffsetRect( pNewClientRect, pWinpos->x - window_rect.left,
1687 pWinpos->y - window_rect.top );
1689 pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
1691 TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
1692 pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
1693 pWinpos->cx, pWinpos->cy, pWinpos->flags );
1694 TRACE( "current %s style %08x new %s\n",
1695 wine_dbgstr_rect( &window_rect ), wndPtr->dwStyle,
1696 wine_dbgstr_rect( pNewWindowRect ));
1698 WIN_ReleasePtr( wndPtr );
1699 return TRUE;
1702 /***********************************************************************
1703 * get_valid_rects
1705 * Compute the valid rects from the old and new client rect and WVR_* flags.
1706 * Helper for WM_NCCALCSIZE handling.
1708 static inline void get_valid_rects( const RECT *old_client, const RECT *new_client, UINT flags,
1709 RECT *valid )
1711 int cx, cy;
1713 if (flags & WVR_REDRAW)
1715 SetRectEmpty( &valid[0] );
1716 SetRectEmpty( &valid[1] );
1717 return;
1720 if (flags & WVR_VALIDRECTS)
1722 if (!IntersectRect( &valid[0], &valid[0], new_client ) ||
1723 !IntersectRect( &valid[1], &valid[1], old_client ))
1725 SetRectEmpty( &valid[0] );
1726 SetRectEmpty( &valid[1] );
1727 return;
1729 flags = WVR_ALIGNLEFT | WVR_ALIGNTOP;
1731 else
1733 valid[0] = *new_client;
1734 valid[1] = *old_client;
1737 /* make sure the rectangles have the same size */
1738 cx = min( valid[0].right - valid[0].left, valid[1].right - valid[1].left );
1739 cy = min( valid[0].bottom - valid[0].top, valid[1].bottom - valid[1].top );
1741 if (flags & WVR_ALIGNBOTTOM)
1743 valid[0].top = valid[0].bottom - cy;
1744 valid[1].top = valid[1].bottom - cy;
1746 else
1748 valid[0].bottom = valid[0].top + cy;
1749 valid[1].bottom = valid[1].top + cy;
1751 if (flags & WVR_ALIGNRIGHT)
1753 valid[0].left = valid[0].right - cx;
1754 valid[1].left = valid[1].right - cx;
1756 else
1758 valid[0].right = valid[0].left + cx;
1759 valid[1].right = valid[1].left + cx;
1764 /***********************************************************************
1765 * SWP_DoOwnedPopups
1767 * fix Z order taking into account owned popups -
1768 * basically we need to maintain them above the window that owns them
1770 * FIXME: hide/show owned popups when owner visibility changes.
1772 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
1774 HWND owner, *list = NULL;
1775 unsigned int i;
1777 TRACE("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
1779 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) return hwndInsertAfter;
1781 if ((owner = GetWindow( hwnd, GW_OWNER )))
1783 /* make sure this popup stays above the owner */
1785 if (hwndInsertAfter != HWND_TOPMOST)
1787 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return hwndInsertAfter;
1789 for (i = 0; list[i]; i++)
1791 BOOL topmost = (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST) != 0;
1793 if (list[i] == owner)
1795 if (i > 0) hwndInsertAfter = list[i-1];
1796 else hwndInsertAfter = topmost ? HWND_TOPMOST : HWND_TOP;
1797 break;
1800 if (hwndInsertAfter == HWND_TOP || hwndInsertAfter == HWND_NOTOPMOST)
1802 if (!topmost) break;
1804 else if (list[i] == hwndInsertAfter) break;
1809 if (hwndInsertAfter == HWND_BOTTOM) goto done;
1810 if (!list && !(list = WIN_ListChildren( GetDesktopWindow() ))) goto done;
1812 i = 0;
1813 if (hwndInsertAfter == HWND_TOP || hwndInsertAfter == HWND_NOTOPMOST)
1815 if (hwndInsertAfter == HWND_NOTOPMOST || !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_TOPMOST))
1817 /* skip all the topmost windows */
1818 while (list[i] && (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST)) i++;
1821 else if (hwndInsertAfter != HWND_TOPMOST)
1823 /* skip windows that are already placed correctly */
1824 for (i = 0; list[i]; i++)
1826 if (list[i] == hwndInsertAfter) break;
1827 if (list[i] == hwnd) goto done; /* nothing to do if window is moving backwards in z-order */
1831 for ( ; list[i]; i++)
1833 if (list[i] == hwnd) break;
1834 if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
1835 TRACE( "moving %p owned by %p after %p\n", list[i], hwnd, hwndInsertAfter );
1836 SetWindowPos( list[i], hwndInsertAfter, 0, 0, 0, 0,
1837 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING | SWP_DEFERERASE );
1838 hwndInsertAfter = list[i];
1841 done:
1842 HeapFree( GetProcessHeap(), 0, list );
1843 return hwndInsertAfter;
1846 /***********************************************************************
1847 * SWP_DoNCCalcSize
1849 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect,
1850 RECT *validRects )
1852 UINT wvrFlags = 0;
1853 RECT window_rect, client_rect;
1855 WIN_GetRectangles( pWinpos->hwnd, COORDS_PARENT, &window_rect, &client_rect );
1857 /* Send WM_NCCALCSIZE message to get new client area */
1858 if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
1860 NCCALCSIZE_PARAMS params;
1861 WINDOWPOS winposCopy;
1863 params.rgrc[0] = *pNewWindowRect;
1864 params.rgrc[1] = window_rect;
1865 params.rgrc[2] = client_rect;
1866 params.lppos = &winposCopy;
1867 winposCopy = *pWinpos;
1869 wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
1871 *pNewClientRect = params.rgrc[0];
1873 TRACE( "hwnd %p old win %s old client %s new win %s new client %s\n", pWinpos->hwnd,
1874 wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(&client_rect),
1875 wine_dbgstr_rect(pNewWindowRect), wine_dbgstr_rect(pNewClientRect) );
1877 if( pNewClientRect->left != client_rect.left ||
1878 pNewClientRect->top != client_rect.top )
1879 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1881 if( (pNewClientRect->right - pNewClientRect->left !=
1882 client_rect.right - client_rect.left))
1883 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1884 else
1885 wvrFlags &= ~WVR_HREDRAW;
1887 if (pNewClientRect->bottom - pNewClientRect->top !=
1888 client_rect.bottom - client_rect.top)
1889 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1890 else
1891 wvrFlags &= ~WVR_VREDRAW;
1893 validRects[0] = params.rgrc[1];
1894 validRects[1] = params.rgrc[2];
1896 else
1898 if (!(pWinpos->flags & SWP_NOMOVE) &&
1899 (pNewClientRect->left != client_rect.left ||
1900 pNewClientRect->top != client_rect.top))
1901 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1904 if (pWinpos->flags & (SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
1906 SetRectEmpty( &validRects[0] );
1907 SetRectEmpty( &validRects[1] );
1909 else get_valid_rects( &client_rect, pNewClientRect, wvrFlags, validRects );
1911 return wvrFlags;
1914 /* fix redundant flags and values in the WINDOWPOS structure */
1915 static BOOL fixup_flags( WINDOWPOS *winpos )
1917 HWND parent;
1918 RECT window_rect;
1919 POINT pt;
1920 WND *wndPtr = WIN_GetPtr( winpos->hwnd );
1921 BOOL ret = TRUE;
1923 if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
1925 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1926 return FALSE;
1928 winpos->hwnd = wndPtr->obj.handle; /* make it a full handle */
1930 /* Finally make sure that all coordinates are valid */
1931 if (winpos->x < -32768) winpos->x = -32768;
1932 else if (winpos->x > 32767) winpos->x = 32767;
1933 if (winpos->y < -32768) winpos->y = -32768;
1934 else if (winpos->y > 32767) winpos->y = 32767;
1936 if (winpos->cx < 0) winpos->cx = 0;
1937 else if (winpos->cx > 32767) winpos->cx = 32767;
1938 if (winpos->cy < 0) winpos->cy = 0;
1939 else if (winpos->cy > 32767) winpos->cy = 32767;
1941 parent = GetAncestor( winpos->hwnd, GA_PARENT );
1942 if (!IsWindowVisible( parent )) winpos->flags |= SWP_NOREDRAW;
1944 if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
1945 else
1947 winpos->flags &= ~SWP_HIDEWINDOW;
1948 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
1951 WIN_GetRectangles( winpos->hwnd, COORDS_SCREEN, &window_rect, NULL );
1952 if ((window_rect.right - window_rect.left == winpos->cx) &&
1953 (window_rect.bottom - window_rect.top == winpos->cy))
1954 winpos->flags |= SWP_NOSIZE; /* Already the right size */
1956 pt.x = winpos->x;
1957 pt.y = winpos->y;
1958 ClientToScreen( parent, &pt );
1959 if ((window_rect.left == pt.x) && (window_rect.top == pt.y))
1960 winpos->flags |= SWP_NOMOVE; /* Already the right position */
1962 if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
1964 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)) && /* Bring to the top when activating */
1965 (winpos->flags & SWP_NOZORDER ||
1966 (winpos->hwndInsertAfter != HWND_TOPMOST && winpos->hwndInsertAfter != HWND_NOTOPMOST)))
1968 winpos->flags &= ~SWP_NOZORDER;
1969 winpos->hwndInsertAfter = HWND_TOP;
1973 /* Check hwndInsertAfter */
1974 if (winpos->flags & SWP_NOZORDER) goto done;
1976 if (winpos->hwndInsertAfter == HWND_TOP)
1978 if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1979 winpos->flags |= SWP_NOZORDER;
1981 else if (winpos->hwndInsertAfter == HWND_BOTTOM)
1983 if (!(wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
1984 winpos->flags |= SWP_NOZORDER;
1986 else if (winpos->hwndInsertAfter == HWND_TOPMOST)
1988 if ((wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1989 winpos->flags |= SWP_NOZORDER;
1991 else if (winpos->hwndInsertAfter == HWND_NOTOPMOST)
1993 if (!(wndPtr->dwExStyle & WS_EX_TOPMOST))
1994 winpos->flags |= SWP_NOZORDER;
1996 else
1998 if ((winpos->hwnd == winpos->hwndInsertAfter) ||
1999 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
2000 winpos->flags |= SWP_NOZORDER;
2002 done:
2003 WIN_ReleasePtr( wndPtr );
2004 return ret;
2008 /***********************************************************************
2009 * update_surface_region
2011 static void update_surface_region( HWND hwnd )
2013 NTSTATUS status;
2014 HRGN region = 0;
2015 RGNDATA *data;
2016 size_t size = 256;
2017 WND *win = WIN_GetPtr( hwnd );
2019 if (!win || win == WND_DESKTOP || win == WND_OTHER_PROCESS) return;
2020 if (!win->surface) goto done;
2024 if (!(data = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( RGNDATA, Buffer[size] )))) goto done;
2026 SERVER_START_REQ( get_surface_region )
2028 req->window = wine_server_user_handle( hwnd );
2029 wine_server_set_reply( req, data->Buffer, size );
2030 if (!(status = wine_server_call( req )))
2032 size_t reply_size = wine_server_reply_size( reply );
2033 if (reply_size)
2035 data->rdh.dwSize = sizeof(data->rdh);
2036 data->rdh.iType = RDH_RECTANGLES;
2037 data->rdh.nCount = reply_size / sizeof(RECT);
2038 data->rdh.nRgnSize = reply_size;
2039 region = ExtCreateRegion( NULL, data->rdh.dwSize + data->rdh.nRgnSize, data );
2040 OffsetRgn( region, -reply->visible_rect.left, -reply->visible_rect.top );
2043 else size = reply->total_size;
2045 SERVER_END_REQ;
2046 HeapFree( GetProcessHeap(), 0, data );
2047 } while (status == STATUS_BUFFER_OVERFLOW);
2049 if (status) goto done;
2051 win->surface->funcs->set_region( win->surface, region );
2052 if (region) DeleteObject( region );
2054 done:
2055 WIN_ReleasePtr( win );
2059 /***********************************************************************
2060 * set_window_pos
2062 * Backend implementation of SetWindowPos.
2064 BOOL set_window_pos( HWND hwnd, HWND insert_after, UINT swp_flags,
2065 const RECT *window_rect, const RECT *client_rect, const RECT *valid_rects )
2067 WND *win;
2068 HWND surface_win = 0, parent = GetAncestor( hwnd, GA_PARENT );
2069 BOOL ret, needs_update = FALSE;
2070 RECT visible_rect, old_visible_rect, old_window_rect, old_client_rect;
2071 struct window_surface *old_surface, *new_surface = NULL;
2073 if (!parent || parent == GetDesktopWindow())
2075 new_surface = &dummy_surface; /* provide a default surface for top-level windows */
2076 window_surface_add_ref( new_surface );
2078 visible_rect = *window_rect;
2079 USER_Driver->pWindowPosChanging( hwnd, insert_after, swp_flags,
2080 window_rect, client_rect, &visible_rect, &new_surface );
2082 WIN_GetRectangles( hwnd, COORDS_SCREEN, &old_window_rect, NULL );
2084 if (!(win = WIN_GetPtr( hwnd )) || win == WND_DESKTOP || win == WND_OTHER_PROCESS)
2086 if (new_surface) window_surface_release( new_surface );
2087 return FALSE;
2089 old_visible_rect = win->visible_rect;
2090 old_client_rect = win->rectClient;
2091 old_surface = win->surface;
2092 if (old_surface != new_surface) swp_flags |= SWP_FRAMECHANGED; /* force refreshing non-client area */
2094 SERVER_START_REQ( set_window_pos )
2096 req->handle = wine_server_user_handle( hwnd );
2097 req->previous = wine_server_user_handle( insert_after );
2098 req->swp_flags = swp_flags;
2099 req->window.left = window_rect->left;
2100 req->window.top = window_rect->top;
2101 req->window.right = window_rect->right;
2102 req->window.bottom = window_rect->bottom;
2103 req->client.left = client_rect->left;
2104 req->client.top = client_rect->top;
2105 req->client.right = client_rect->right;
2106 req->client.bottom = client_rect->bottom;
2107 if (!EqualRect( window_rect, &visible_rect ) || !IsRectEmpty( &valid_rects[0] ))
2109 wine_server_add_data( req, &visible_rect, sizeof(visible_rect) );
2110 if (!IsRectEmpty( &valid_rects[0] ))
2111 wine_server_add_data( req, valid_rects, 2 * sizeof(*valid_rects) );
2113 if (new_surface) req->paint_flags |= SET_WINPOS_PAINT_SURFACE;
2114 if (win->pixel_format) req->paint_flags |= SET_WINPOS_PIXEL_FORMAT;
2116 if ((ret = !wine_server_call( req )))
2118 win->dwStyle = reply->new_style;
2119 win->dwExStyle = reply->new_ex_style;
2120 win->rectWindow = *window_rect;
2121 win->rectClient = *client_rect;
2122 win->visible_rect = visible_rect;
2123 win->surface = new_surface;
2124 surface_win = wine_server_ptr_handle( reply->surface_win );
2125 needs_update = reply->needs_update;
2126 if (GetWindowLongW( win->parent, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
2128 RECT client;
2129 GetClientRect( win->parent, &client );
2130 mirror_rect( &client, &win->rectWindow );
2131 mirror_rect( &client, &win->rectClient );
2132 mirror_rect( &client, &win->visible_rect );
2134 /* if an RTL window is resized the children have moved */
2135 if (win->dwExStyle & WS_EX_LAYOUTRTL &&
2136 client_rect->right - client_rect->left != old_client_rect.right - old_client_rect.left)
2137 win->flags |= WIN_CHILDREN_MOVED;
2140 SERVER_END_REQ;
2142 if (ret)
2144 if (needs_update) update_surface_region( surface_win );
2145 if (((swp_flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) ||
2146 (swp_flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW | SWP_STATECHANGED | SWP_FRAMECHANGED)))
2147 invalidate_dce( win, &old_window_rect );
2150 WIN_ReleasePtr( win );
2152 if (ret)
2154 TRACE( "win %p surface %p -> %p\n", hwnd, old_surface, new_surface );
2155 register_window_surface( old_surface, new_surface );
2156 if (old_surface)
2158 if (!IsRectEmpty( valid_rects ))
2160 move_window_bits( hwnd, old_surface, new_surface, &visible_rect,
2161 &old_visible_rect, window_rect, valid_rects );
2162 valid_rects = NULL; /* prevent the driver from trying to also move the bits */
2164 window_surface_release( old_surface );
2166 else if (surface_win && surface_win != hwnd)
2168 if (!IsRectEmpty( valid_rects ))
2170 RECT rects[2];
2171 int x_offset = old_visible_rect.left - visible_rect.left;
2172 int y_offset = old_visible_rect.top - visible_rect.top;
2174 /* if all that happened is that the whole window moved, copy everything */
2175 if (!(swp_flags & SWP_FRAMECHANGED) &&
2176 old_visible_rect.right - visible_rect.right == x_offset &&
2177 old_visible_rect.bottom - visible_rect.bottom == y_offset &&
2178 old_client_rect.left - client_rect->left == x_offset &&
2179 old_client_rect.right - client_rect->right == x_offset &&
2180 old_client_rect.top - client_rect->top == y_offset &&
2181 old_client_rect.bottom - client_rect->bottom == y_offset &&
2182 EqualRect( &valid_rects[0], client_rect ))
2184 rects[0] = visible_rect;
2185 rects[1] = old_visible_rect;
2186 valid_rects = rects;
2188 move_window_bits_parent( hwnd, surface_win, window_rect, valid_rects );
2189 valid_rects = NULL; /* prevent the driver from trying to also move the bits */
2193 USER_Driver->pWindowPosChanged( hwnd, insert_after, swp_flags, window_rect,
2194 client_rect, &visible_rect, valid_rects, new_surface );
2196 else if (new_surface) window_surface_release( new_surface );
2198 return ret;
2202 /***********************************************************************
2203 * USER_SetWindowPos
2205 * User32 internal function
2207 BOOL USER_SetWindowPos( WINDOWPOS * winpos )
2209 RECT newWindowRect, newClientRect, valid_rects[2];
2210 UINT orig_flags;
2212 orig_flags = winpos->flags;
2214 /* First, check z-order arguments. */
2215 if (!(winpos->flags & SWP_NOZORDER))
2217 /* fix sign extension */
2218 if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
2219 else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
2221 if (!(winpos->hwndInsertAfter == HWND_TOP ||
2222 winpos->hwndInsertAfter == HWND_BOTTOM ||
2223 winpos->hwndInsertAfter == HWND_TOPMOST ||
2224 winpos->hwndInsertAfter == HWND_NOTOPMOST))
2226 HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
2227 HWND insertafter_parent = GetAncestor( winpos->hwndInsertAfter, GA_PARENT );
2229 /* hwndInsertAfter must be a sibling of the window */
2230 if (!insertafter_parent) return FALSE;
2231 if (insertafter_parent != parent) return TRUE;
2235 /* Make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
2236 if (!(winpos->flags & SWP_NOMOVE))
2238 if (winpos->x < -32768) winpos->x = -32768;
2239 else if (winpos->x > 32767) winpos->x = 32767;
2240 if (winpos->y < -32768) winpos->y = -32768;
2241 else if (winpos->y > 32767) winpos->y = 32767;
2243 if (!(winpos->flags & SWP_NOSIZE))
2245 if (winpos->cx < 0) winpos->cx = 0;
2246 else if (winpos->cx > 32767) winpos->cx = 32767;
2247 if (winpos->cy < 0) winpos->cy = 0;
2248 else if (winpos->cy > 32767) winpos->cy = 32767;
2251 if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
2253 /* Fix redundant flags */
2254 if (!fixup_flags( winpos )) return FALSE;
2256 if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
2258 if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
2259 winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
2262 /* Common operations */
2264 SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect, valid_rects );
2266 if (!set_window_pos( winpos->hwnd, winpos->hwndInsertAfter, winpos->flags,
2267 &newWindowRect, &newClientRect, valid_rects ))
2268 return FALSE;
2272 if( winpos->flags & SWP_HIDEWINDOW )
2273 HideCaret(winpos->hwnd);
2274 else if (winpos->flags & SWP_SHOWWINDOW)
2275 ShowCaret(winpos->hwnd);
2277 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)))
2279 /* child windows get WM_CHILDACTIVATE message */
2280 if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
2281 SendMessageW( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
2282 else
2283 SetForegroundWindow( winpos->hwnd );
2286 if(!(orig_flags & SWP_DEFERERASE))
2288 /* erase parent when hiding or resizing child */
2289 if ((orig_flags & SWP_HIDEWINDOW) ||
2290 (!(orig_flags & SWP_SHOWWINDOW) &&
2291 (winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOGEOMETRYCHANGE))
2293 HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
2294 if (!parent || parent == GetDesktopWindow()) parent = winpos->hwnd;
2295 erase_now( parent, 0 );
2298 /* Give newly shown windows a chance to redraw */
2299 if(((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE)
2300 && !(orig_flags & SWP_AGG_NOCLIENTCHANGE) && (orig_flags & SWP_SHOWWINDOW))
2302 erase_now(winpos->hwnd, 0);
2306 /* And last, send the WM_WINDOWPOSCHANGED message */
2308 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
2310 if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE)
2311 && !((orig_flags & SWP_AGG_NOCLIENTCHANGE) && (orig_flags & SWP_SHOWWINDOW)))
2313 /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
2314 and always contains final window position.
2316 winpos->x = newWindowRect.left;
2317 winpos->y = newWindowRect.top;
2318 winpos->cx = newWindowRect.right - newWindowRect.left;
2319 winpos->cy = newWindowRect.bottom - newWindowRect.top;
2320 SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
2322 return TRUE;
2325 /***********************************************************************
2326 * SetWindowPos (USER32.@)
2328 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
2329 INT x, INT y, INT cx, INT cy, UINT flags )
2331 WINDOWPOS winpos;
2333 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2334 hwnd, hwndInsertAfter, x, y, cx, cy, flags);
2335 if(TRACE_ON(win)) dump_winpos_flags(flags);
2337 if (is_broadcast(hwnd))
2339 SetLastError( ERROR_INVALID_PARAMETER );
2340 return FALSE;
2343 winpos.hwnd = WIN_GetFullHandle(hwnd);
2344 winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
2345 winpos.x = x;
2346 winpos.y = y;
2347 winpos.cx = cx;
2348 winpos.cy = cy;
2349 winpos.flags = flags;
2351 if (WIN_IsCurrentThread( hwnd ))
2352 return USER_SetWindowPos(&winpos);
2354 return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
2358 /***********************************************************************
2359 * BeginDeferWindowPos (USER32.@)
2361 HDWP WINAPI BeginDeferWindowPos( INT count )
2363 HDWP handle = 0;
2364 DWP *pDWP;
2366 TRACE("%d\n", count);
2368 if (count < 0)
2370 SetLastError(ERROR_INVALID_PARAMETER);
2371 return 0;
2373 /* Windows allows zero count, in which case it allocates context for 8 moves */
2374 if (count == 0) count = 8;
2376 if (!(pDWP = HeapAlloc( GetProcessHeap(), 0, sizeof(DWP)))) return 0;
2378 pDWP->actualCount = 0;
2379 pDWP->suggestedCount = count;
2380 pDWP->hwndParent = 0;
2382 if (!(pDWP->winPos = HeapAlloc( GetProcessHeap(), 0, count * sizeof(WINDOWPOS) )) ||
2383 !(handle = alloc_user_handle( &pDWP->obj, USER_DWP )))
2385 HeapFree( GetProcessHeap(), 0, pDWP->winPos );
2386 HeapFree( GetProcessHeap(), 0, pDWP );
2389 TRACE("returning hdwp %p\n", handle);
2390 return handle;
2394 /***********************************************************************
2395 * DeferWindowPos (USER32.@)
2397 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
2398 INT x, INT y, INT cx, INT cy,
2399 UINT flags )
2401 DWP *pDWP;
2402 int i;
2403 HDWP retvalue = hdwp;
2405 TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2406 hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
2408 hwnd = WIN_GetFullHandle( hwnd );
2409 if (is_desktop_window( hwnd ) || !IsWindow( hwnd ))
2411 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2412 return 0;
2415 if (!(pDWP = get_user_handle_ptr( hdwp, USER_DWP ))) return 0;
2416 if (pDWP == OBJ_OTHER_PROCESS)
2418 FIXME( "other process handle %p?\n", hdwp );
2419 return 0;
2422 for (i = 0; i < pDWP->actualCount; i++)
2424 if (pDWP->winPos[i].hwnd == hwnd)
2426 /* Merge with the other changes */
2427 if (!(flags & SWP_NOZORDER))
2429 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
2431 if (!(flags & SWP_NOMOVE))
2433 pDWP->winPos[i].x = x;
2434 pDWP->winPos[i].y = y;
2436 if (!(flags & SWP_NOSIZE))
2438 pDWP->winPos[i].cx = cx;
2439 pDWP->winPos[i].cy = cy;
2441 pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
2442 SWP_NOZORDER | SWP_NOREDRAW |
2443 SWP_NOACTIVATE | SWP_NOCOPYBITS|
2444 SWP_NOOWNERZORDER);
2445 pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
2446 SWP_FRAMECHANGED);
2447 goto END;
2450 if (pDWP->actualCount >= pDWP->suggestedCount)
2452 WINDOWPOS *newpos = HeapReAlloc( GetProcessHeap(), 0, pDWP->winPos,
2453 pDWP->suggestedCount * 2 * sizeof(WINDOWPOS) );
2454 if (!newpos)
2456 retvalue = 0;
2457 goto END;
2459 pDWP->suggestedCount *= 2;
2460 pDWP->winPos = newpos;
2462 pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
2463 pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
2464 pDWP->winPos[pDWP->actualCount].x = x;
2465 pDWP->winPos[pDWP->actualCount].y = y;
2466 pDWP->winPos[pDWP->actualCount].cx = cx;
2467 pDWP->winPos[pDWP->actualCount].cy = cy;
2468 pDWP->winPos[pDWP->actualCount].flags = flags;
2469 pDWP->actualCount++;
2470 END:
2471 release_user_handle_ptr( pDWP );
2472 return retvalue;
2476 /***********************************************************************
2477 * EndDeferWindowPos (USER32.@)
2479 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
2481 DWP *pDWP;
2482 WINDOWPOS *winpos;
2483 int i;
2485 TRACE("%p\n", hdwp);
2487 if (!(pDWP = free_user_handle( hdwp, USER_DWP ))) return FALSE;
2488 if (pDWP == OBJ_OTHER_PROCESS)
2490 FIXME( "other process handle %p?\n", hdwp );
2491 return FALSE;
2494 for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
2496 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2497 winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
2498 winpos->cx, winpos->cy, winpos->flags);
2500 if (WIN_IsCurrentThread( winpos->hwnd ))
2501 USER_SetWindowPos( winpos );
2502 else
2503 SendMessageW( winpos->hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)winpos );
2505 HeapFree( GetProcessHeap(), 0, pDWP->winPos );
2506 HeapFree( GetProcessHeap(), 0, pDWP );
2507 return TRUE;
2511 /***********************************************************************
2512 * ArrangeIconicWindows (USER32.@)
2514 UINT WINAPI ArrangeIconicWindows( HWND parent )
2516 RECT rectParent;
2517 HWND hwndChild;
2518 INT x, y, xspacing, yspacing;
2519 POINT pt;
2520 MINIMIZEDMETRICS metrics;
2522 metrics.cbSize = sizeof(metrics);
2523 SystemParametersInfoW( SPI_GETMINIMIZEDMETRICS, sizeof(metrics), &metrics, 0 );
2525 if (parent == GetDesktopWindow())
2527 MONITORINFO mon_info;
2528 HMONITOR monitor = MonitorFromWindow( 0, MONITOR_DEFAULTTOPRIMARY );
2530 mon_info.cbSize = sizeof( mon_info );
2531 GetMonitorInfoW( monitor, &mon_info );
2532 rectParent = mon_info.rcWork;
2534 else GetClientRect( parent, &rectParent );
2536 x = y = 0;
2537 xspacing = GetSystemMetrics(SM_CXICONSPACING);
2538 yspacing = GetSystemMetrics(SM_CYICONSPACING);
2540 hwndChild = GetWindow( parent, GW_CHILD );
2541 while (hwndChild)
2543 if( IsIconic( hwndChild ) )
2545 WINPOS_ShowIconTitle( hwndChild, FALSE );
2547 if (metrics.iArrange & ARW_STARTRIGHT)
2548 pt.x = rectParent.right - (x + 1) * xspacing;
2549 else
2550 pt.x = rectParent.left + x * xspacing;
2551 if (metrics.iArrange & ARW_STARTTOP)
2552 pt.y = rectParent.top + y * yspacing;
2553 else
2554 pt.y = rectParent.bottom - (y + 1) * yspacing;
2556 SetWindowPos( hwndChild, 0, pt.x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
2557 pt.y + (yspacing - GetSystemMetrics(SM_CYICON)) / 2, 0, 0,
2558 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
2559 if( IsWindow(hwndChild) )
2560 WINPOS_ShowIconTitle(hwndChild , TRUE );
2562 if (++x >= (rectParent.right - rectParent.left) / xspacing)
2564 x = 0;
2565 y++;
2568 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
2570 return yspacing;
2574 /***********************************************************************
2575 * draw_moving_frame
2577 * Draw the frame used when moving or resizing window.
2579 static void draw_moving_frame( HWND parent, HDC hdc, RECT *screen_rect, BOOL thickframe )
2581 RECT rect = *screen_rect;
2583 if (parent) MapWindowPoints( 0, parent, (POINT *)&rect, 2 );
2584 if (thickframe)
2586 const int width = GetSystemMetrics(SM_CXFRAME);
2587 const int height = GetSystemMetrics(SM_CYFRAME);
2589 HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
2590 PatBlt( hdc, rect.left, rect.top,
2591 rect.right - rect.left - width, height, PATINVERT );
2592 PatBlt( hdc, rect.left, rect.top + height, width,
2593 rect.bottom - rect.top - height, PATINVERT );
2594 PatBlt( hdc, rect.left + width, rect.bottom - 1,
2595 rect.right - rect.left - width, -height, PATINVERT );
2596 PatBlt( hdc, rect.right - 1, rect.top, -width,
2597 rect.bottom - rect.top - height, PATINVERT );
2598 SelectObject( hdc, hbrush );
2600 else DrawFocusRect( hdc, &rect );
2604 /***********************************************************************
2605 * start_size_move
2607 * Initialization of a move or resize, when initiated from a menu choice.
2608 * Return hit test code for caption or sizing border.
2610 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
2612 LONG hittest = 0;
2613 POINT pt;
2614 MSG msg;
2615 RECT rectWindow;
2617 GetWindowRect( hwnd, &rectWindow );
2619 if ((wParam & 0xfff0) == SC_MOVE)
2621 /* Move pointer at the center of the caption */
2622 RECT rect = rectWindow;
2623 /* Note: to be exactly centered we should take the different types
2624 * of border into account, but it shouldn't make more than a few pixels
2625 * of difference so let's not bother with that */
2626 rect.top += GetSystemMetrics(SM_CYBORDER);
2627 if (style & WS_SYSMENU)
2628 rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
2629 if (style & WS_MINIMIZEBOX)
2630 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
2631 if (style & WS_MAXIMIZEBOX)
2632 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
2633 pt.x = (rect.right + rect.left) / 2;
2634 pt.y = rect.top + GetSystemMetrics(SM_CYSIZE)/2;
2635 hittest = HTCAPTION;
2636 *capturePoint = pt;
2638 else /* SC_SIZE */
2640 SetCursor( LoadCursorW( 0, (LPWSTR)IDC_SIZEALL ) );
2641 pt.x = pt.y = 0;
2642 while(!hittest)
2644 if (!GetMessageW( &msg, 0, 0, 0 )) return 0;
2645 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2647 switch(msg.message)
2649 case WM_MOUSEMOVE:
2650 pt.x = min( max( msg.pt.x, rectWindow.left ), rectWindow.right - 1 );
2651 pt.y = min( max( msg.pt.y, rectWindow.top ), rectWindow.bottom - 1 );
2652 hittest = SendMessageW( hwnd, WM_NCHITTEST, 0, MAKELONG( pt.x, pt.y ) );
2653 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT)) hittest = 0;
2654 break;
2656 case WM_LBUTTONUP:
2657 return 0;
2659 case WM_KEYDOWN:
2660 switch(msg.wParam)
2662 case VK_UP:
2663 hittest = HTTOP;
2664 pt.x =(rectWindow.left+rectWindow.right)/2;
2665 pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
2666 break;
2667 case VK_DOWN:
2668 hittest = HTBOTTOM;
2669 pt.x =(rectWindow.left+rectWindow.right)/2;
2670 pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
2671 break;
2672 case VK_LEFT:
2673 hittest = HTLEFT;
2674 pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
2675 pt.y =(rectWindow.top+rectWindow.bottom)/2;
2676 break;
2677 case VK_RIGHT:
2678 hittest = HTRIGHT;
2679 pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
2680 pt.y =(rectWindow.top+rectWindow.bottom)/2;
2681 break;
2682 case VK_RETURN:
2683 case VK_ESCAPE:
2684 return 0;
2686 break;
2687 default:
2688 TranslateMessage( &msg );
2689 DispatchMessageW( &msg );
2690 break;
2693 *capturePoint = pt;
2695 SetCursorPos( pt.x, pt.y );
2696 SendMessageW( hwnd, WM_SETCURSOR, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
2697 return hittest;
2701 /***********************************************************************
2702 * WINPOS_SysCommandSizeMove
2704 * Perform SC_MOVE and SC_SIZE commands.
2706 void WINPOS_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
2708 MSG msg;
2709 RECT sizingRect, mouseRect, origRect;
2710 HDC hdc;
2711 HWND parent;
2712 LONG hittest = (LONG)(wParam & 0x0f);
2713 WPARAM syscommand = wParam & 0xfff0;
2714 HCURSOR hDragCursor = 0, hOldCursor = 0;
2715 POINT minTrack, maxTrack;
2716 POINT capturePoint, pt;
2717 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
2718 BOOL thickframe = HAS_THICKFRAME( style );
2719 BOOL iconic = style & WS_MINIMIZE;
2720 BOOL moved = FALSE;
2721 DWORD dwPoint = GetMessagePos ();
2722 BOOL DragFullWindows = TRUE;
2723 HMONITOR mon = 0;
2725 if (IsZoomed(hwnd) || !IsWindowVisible(hwnd)) return;
2727 pt.x = (short)LOWORD(dwPoint);
2728 pt.y = (short)HIWORD(dwPoint);
2729 capturePoint = pt;
2730 ClipCursor( NULL );
2732 TRACE("hwnd %p command %04lx, hittest %d, pos %d,%d\n",
2733 hwnd, syscommand, hittest, pt.x, pt.y);
2735 if (syscommand == SC_MOVE)
2737 if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
2738 if (!hittest) return;
2740 else /* SC_SIZE */
2742 if ( hittest && (syscommand != SC_MOUSEMENU) )
2743 hittest += (HTLEFT - WMSZ_LEFT);
2744 else
2746 set_capture_window( hwnd, GUI_INMOVESIZE, NULL );
2747 hittest = start_size_move( hwnd, wParam, &capturePoint, style );
2748 if (!hittest)
2750 set_capture_window( 0, GUI_INMOVESIZE, NULL );
2751 return;
2756 /* Get min/max info */
2758 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
2759 WIN_GetRectangles( hwnd, COORDS_PARENT, &sizingRect, NULL );
2760 origRect = sizingRect;
2761 if (style & WS_CHILD)
2763 parent = GetParent(hwnd);
2764 GetClientRect( parent, &mouseRect );
2765 MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
2766 MapWindowPoints( parent, 0, (LPPOINT)&sizingRect, 2 );
2768 else
2770 parent = 0;
2771 mouseRect = get_virtual_screen_rect();
2772 mon = MonitorFromPoint( pt, MONITOR_DEFAULTTONEAREST );
2775 if (ON_LEFT_BORDER(hittest))
2777 mouseRect.left = max( mouseRect.left, sizingRect.right-maxTrack.x+capturePoint.x-sizingRect.left );
2778 mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x+capturePoint.x-sizingRect.left );
2780 else if (ON_RIGHT_BORDER(hittest))
2782 mouseRect.left = max( mouseRect.left, sizingRect.left+minTrack.x+capturePoint.x-sizingRect.right );
2783 mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x+capturePoint.x-sizingRect.right );
2785 if (ON_TOP_BORDER(hittest))
2787 mouseRect.top = max( mouseRect.top, sizingRect.bottom-maxTrack.y+capturePoint.y-sizingRect.top );
2788 mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y+capturePoint.y-sizingRect.top);
2790 else if (ON_BOTTOM_BORDER(hittest))
2792 mouseRect.top = max( mouseRect.top, sizingRect.top+minTrack.y+capturePoint.y-sizingRect.bottom );
2793 mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y+capturePoint.y-sizingRect.bottom );
2796 /* Retrieve a default cache DC (without using the window style) */
2797 hdc = GetDCEx( parent, 0, DCX_CACHE );
2799 if( iconic ) /* create a cursor for dragging */
2801 hDragCursor = (HCURSOR)GetClassLongPtrW( hwnd, GCLP_HICON);
2802 if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageW( hwnd, WM_QUERYDRAGICON, 0, 0L);
2803 if( !hDragCursor ) iconic = FALSE;
2806 /* we only allow disabling the full window drag for child windows */
2807 if (parent) SystemParametersInfoW( SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0 );
2809 /* repaint the window before moving it around */
2810 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
2812 SendMessageW( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
2813 set_capture_window( hwnd, GUI_INMOVESIZE, NULL );
2815 while(1)
2817 int dx = 0, dy = 0;
2819 if (!GetMessageW( &msg, 0, 0, 0 )) break;
2820 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2822 /* Exit on button-up, Return, or Esc */
2823 if ((msg.message == WM_LBUTTONUP) ||
2824 ((msg.message == WM_KEYDOWN) &&
2825 ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
2827 if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
2829 TranslateMessage( &msg );
2830 DispatchMessageW( &msg );
2831 continue; /* We are not interested in other messages */
2834 pt = msg.pt;
2836 if (msg.message == WM_KEYDOWN) switch(msg.wParam)
2838 case VK_UP: pt.y -= 8; break;
2839 case VK_DOWN: pt.y += 8; break;
2840 case VK_LEFT: pt.x -= 8; break;
2841 case VK_RIGHT: pt.x += 8; break;
2844 pt.x = max( pt.x, mouseRect.left );
2845 pt.x = min( pt.x, mouseRect.right - 1 );
2846 pt.y = max( pt.y, mouseRect.top );
2847 pt.y = min( pt.y, mouseRect.bottom - 1 );
2849 if (!parent)
2851 HMONITOR newmon;
2852 MONITORINFO info;
2854 if ((newmon = MonitorFromPoint( pt, MONITOR_DEFAULTTONULL )))
2855 mon = newmon;
2857 info.cbSize = sizeof(info);
2858 if (mon && GetMonitorInfoW( mon, &info ))
2860 pt.x = max( pt.x, info.rcWork.left );
2861 pt.x = min( pt.x, info.rcWork.right - 1 );
2862 pt.y = max( pt.y, info.rcWork.top );
2863 pt.y = min( pt.y, info.rcWork.bottom - 1 );
2867 dx = pt.x - capturePoint.x;
2868 dy = pt.y - capturePoint.y;
2870 if (dx || dy)
2872 if( !moved )
2874 moved = TRUE;
2876 if( iconic ) /* ok, no system popup tracking */
2878 hOldCursor = SetCursor(hDragCursor);
2879 ShowCursor( TRUE );
2880 WINPOS_ShowIconTitle( hwnd, FALSE );
2882 else if(!DragFullWindows)
2883 draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2886 if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
2887 else
2889 if(!iconic && !DragFullWindows) draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2890 if (hittest == HTCAPTION) OffsetRect( &sizingRect, dx, dy );
2891 if (ON_LEFT_BORDER(hittest)) sizingRect.left += dx;
2892 else if (ON_RIGHT_BORDER(hittest)) sizingRect.right += dx;
2893 if (ON_TOP_BORDER(hittest)) sizingRect.top += dy;
2894 else if (ON_BOTTOM_BORDER(hittest)) sizingRect.bottom += dy;
2895 capturePoint = pt;
2897 /* determine the hit location */
2898 if (syscommand == SC_SIZE)
2900 WPARAM wpSizingHit = 0;
2902 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
2903 wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
2904 SendMessageW( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&sizingRect );
2906 else
2907 SendMessageW( hwnd, WM_MOVING, 0, (LPARAM)&sizingRect );
2909 if (!iconic)
2911 if(!DragFullWindows)
2912 draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2913 else
2915 RECT rect = sizingRect;
2916 MapWindowPoints( 0, parent, (POINT *)&rect, 2 );
2917 SetWindowPos( hwnd, 0, rect.left, rect.top,
2918 rect.right - rect.left, rect.bottom - rect.top,
2919 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2926 if( iconic )
2928 if( moved ) /* restore cursors, show icon title later on */
2930 ShowCursor( FALSE );
2931 SetCursor( hOldCursor );
2934 else if (moved && !DragFullWindows)
2936 draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2939 set_capture_window( 0, GUI_INMOVESIZE, NULL );
2940 ReleaseDC( parent, hdc );
2941 if (parent) MapWindowPoints( 0, parent, (POINT *)&sizingRect, 2 );
2943 if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
2944 moved = FALSE;
2946 SendMessageW( hwnd, WM_EXITSIZEMOVE, 0, 0 );
2947 SendMessageW( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
2949 /* window moved or resized */
2950 if (moved)
2952 /* if the moving/resizing isn't canceled call SetWindowPos
2953 * with the new position or the new size of the window
2955 if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
2957 /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
2958 if(!DragFullWindows || iconic)
2959 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
2960 sizingRect.right - sizingRect.left,
2961 sizingRect.bottom - sizingRect.top,
2962 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2964 else
2965 { /* restore previous size/position */
2966 if(DragFullWindows)
2967 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
2968 origRect.right - origRect.left,
2969 origRect.bottom - origRect.top,
2970 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2974 if (IsIconic(hwnd))
2976 /* Single click brings up the system menu when iconized */
2978 if( !moved )
2980 if(style & WS_SYSMENU )
2981 SendMessageW( hwnd, WM_SYSCOMMAND,
2982 SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
2984 else WINPOS_ShowIconTitle( hwnd, TRUE );
2988 /***********************************************************************
2989 * LogicalToPhysicalPoint (USER32.@)
2991 BOOL WINAPI LogicalToPhysicalPoint(HWND hwnd, POINT *point)
2993 static int once;
2995 if (!once++)
2996 FIXME("(%p %p) stub\n", hwnd, point);
2997 return TRUE;
3000 /***********************************************************************
3001 * PhysicalToLogicalPoint (USER32.@)
3003 BOOL WINAPI PhysicalToLogicalPoint(HWND hwnd, POINT *point)
3005 static int once;
3007 if (!once++)
3008 FIXME("(%p %p) stub\n", hwnd, point);
3009 return TRUE;