ntdll: Added RtlDosPathNameToNtPathName_U_WithStatus().
[wine.git] / dlls / user32 / winpos.c
blob6b6b26a7c8958ce29029e6f4fdce4a1b92dc8785
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;
1258 if (GetWindowPlacement( hwnd, &wndpl ))
1260 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
1261 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
1262 return wndpl.showCmd;
1264 return 0;
1268 /***********************************************************************
1269 * GetWindowPlacement (USER32.@)
1271 * Win95:
1272 * Fails if wndpl->length of Win95 (!) apps is invalid.
1274 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
1276 WND *pWnd = WIN_GetPtr( hwnd );
1278 if (!pWnd) return FALSE;
1280 if (pWnd == WND_DESKTOP)
1282 wndpl->length = sizeof(*wndpl);
1283 wndpl->showCmd = SW_SHOWNORMAL;
1284 wndpl->flags = 0;
1285 wndpl->ptMinPosition.x = -1;
1286 wndpl->ptMinPosition.y = -1;
1287 wndpl->ptMaxPosition.x = -1;
1288 wndpl->ptMaxPosition.y = -1;
1289 GetWindowRect( hwnd, &wndpl->rcNormalPosition );
1290 return TRUE;
1292 if (pWnd == WND_OTHER_PROCESS)
1294 if (!IsWindow( hwnd )) return FALSE;
1295 FIXME( "not supported on other process window %p\n", hwnd );
1296 /* provide some dummy information */
1297 wndpl->length = sizeof(*wndpl);
1298 wndpl->showCmd = SW_SHOWNORMAL;
1299 wndpl->flags = 0;
1300 wndpl->ptMinPosition.x = -1;
1301 wndpl->ptMinPosition.y = -1;
1302 wndpl->ptMaxPosition.x = -1;
1303 wndpl->ptMaxPosition.y = -1;
1304 GetWindowRect( hwnd, &wndpl->rcNormalPosition );
1305 return TRUE;
1308 /* update the placement according to the current style */
1309 if (pWnd->dwStyle & WS_MINIMIZE)
1311 pWnd->min_pos.x = pWnd->rectWindow.left;
1312 pWnd->min_pos.y = pWnd->rectWindow.top;
1314 else if (pWnd->dwStyle & WS_MAXIMIZE)
1316 pWnd->max_pos.x = pWnd->rectWindow.left;
1317 pWnd->max_pos.y = pWnd->rectWindow.top;
1319 else
1321 pWnd->normal_rect = pWnd->rectWindow;
1324 wndpl->length = sizeof(*wndpl);
1325 if( pWnd->dwStyle & WS_MINIMIZE )
1326 wndpl->showCmd = SW_SHOWMINIMIZED;
1327 else
1328 wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
1329 if( pWnd->flags & WIN_RESTORE_MAX )
1330 wndpl->flags = WPF_RESTORETOMAXIMIZED;
1331 else
1332 wndpl->flags = 0;
1333 wndpl->ptMinPosition = pWnd->min_pos;
1334 wndpl->ptMaxPosition = pWnd->max_pos;
1335 wndpl->rcNormalPosition = pWnd->normal_rect;
1336 WIN_ReleasePtr( pWnd );
1338 TRACE( "%p: returning min %d,%d max %d,%d normal %s\n",
1339 hwnd, wndpl->ptMinPosition.x, wndpl->ptMinPosition.y,
1340 wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y,
1341 wine_dbgstr_rect(&wndpl->rcNormalPosition) );
1342 return TRUE;
1345 /* make sure the specified rect is visible on screen */
1346 static void make_rect_onscreen( RECT *rect )
1348 MONITORINFO info;
1349 HMONITOR monitor = MonitorFromRect( rect, MONITOR_DEFAULTTONEAREST );
1351 info.cbSize = sizeof(info);
1352 if (!monitor || !GetMonitorInfoW( monitor, &info )) return;
1353 /* FIXME: map coordinates from rcWork to rcMonitor */
1354 if (rect->right <= info.rcWork.left)
1356 rect->right += info.rcWork.left - rect->left;
1357 rect->left = info.rcWork.left;
1359 else if (rect->left >= info.rcWork.right)
1361 rect->left += info.rcWork.right - rect->right;
1362 rect->right = info.rcWork.right;
1364 if (rect->bottom <= info.rcWork.top)
1366 rect->bottom += info.rcWork.top - rect->top;
1367 rect->top = info.rcWork.top;
1369 else if (rect->top >= info.rcWork.bottom)
1371 rect->top += info.rcWork.bottom - rect->bottom;
1372 rect->bottom = info.rcWork.bottom;
1376 /* make sure the specified point is visible on screen */
1377 static void make_point_onscreen( POINT *pt )
1379 RECT rect;
1381 SetRect( &rect, pt->x, pt->y, pt->x + 1, pt->y + 1 );
1382 make_rect_onscreen( &rect );
1383 pt->x = rect.left;
1384 pt->y = rect.top;
1388 /***********************************************************************
1389 * WINPOS_SetPlacement
1391 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
1393 DWORD style;
1394 WND *pWnd = WIN_GetPtr( hwnd );
1395 WINDOWPLACEMENT wp = *wndpl;
1397 if (flags & PLACE_MIN) make_point_onscreen( &wp.ptMinPosition );
1398 if (flags & PLACE_MAX) make_point_onscreen( &wp.ptMaxPosition );
1399 if (flags & PLACE_RECT) make_rect_onscreen( &wp.rcNormalPosition );
1401 TRACE( "%p: setting min %d,%d max %d,%d normal %s flags %x ajusted to min %d,%d max %d,%d normal %s\n",
1402 hwnd, wndpl->ptMinPosition.x, wndpl->ptMinPosition.y,
1403 wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y,
1404 wine_dbgstr_rect(&wndpl->rcNormalPosition), flags,
1405 wp.ptMinPosition.x, wp.ptMinPosition.y, wp.ptMaxPosition.x, wp.ptMaxPosition.y,
1406 wine_dbgstr_rect(&wp.rcNormalPosition) );
1408 if (!pWnd || pWnd == WND_OTHER_PROCESS || pWnd == WND_DESKTOP) return FALSE;
1410 if( flags & PLACE_MIN ) pWnd->min_pos = wp.ptMinPosition;
1411 if( flags & PLACE_MAX ) pWnd->max_pos = wp.ptMaxPosition;
1412 if( flags & PLACE_RECT) pWnd->normal_rect = wp.rcNormalPosition;
1414 style = pWnd->dwStyle;
1416 WIN_ReleasePtr( pWnd );
1418 if( style & WS_MINIMIZE )
1420 if (flags & PLACE_MIN)
1422 WINPOS_ShowIconTitle( hwnd, FALSE );
1423 SetWindowPos( hwnd, 0, wp.ptMinPosition.x, wp.ptMinPosition.y, 0, 0,
1424 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1427 else if( style & WS_MAXIMIZE )
1429 if (flags & PLACE_MAX)
1430 SetWindowPos( hwnd, 0, wp.ptMaxPosition.x, wp.ptMaxPosition.y, 0, 0,
1431 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1433 else if( flags & PLACE_RECT )
1434 SetWindowPos( hwnd, 0, wp.rcNormalPosition.left, wp.rcNormalPosition.top,
1435 wp.rcNormalPosition.right - wp.rcNormalPosition.left,
1436 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top,
1437 SWP_NOZORDER | SWP_NOACTIVATE );
1439 ShowWindow( hwnd, wndpl->showCmd );
1441 if (IsIconic( hwnd ))
1443 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE) WINPOS_ShowIconTitle( hwnd, TRUE );
1445 /* SDK: ...valid only the next time... */
1446 if( wndpl->flags & WPF_RESTORETOMAXIMIZED )
1447 win_set_flags( hwnd, WIN_RESTORE_MAX, 0 );
1449 return TRUE;
1453 /***********************************************************************
1454 * SetWindowPlacement (USER32.@)
1456 * Win95:
1457 * Fails if wndpl->length of Win95 (!) apps is invalid.
1459 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
1461 UINT flags = PLACE_MAX | PLACE_RECT;
1462 if (!wpl) return FALSE;
1463 if (wpl->flags & WPF_SETMINPOSITION) flags |= PLACE_MIN;
1464 return WINPOS_SetPlacement( hwnd, wpl, flags );
1468 /***********************************************************************
1469 * AnimateWindow (USER32.@)
1470 * Shows/Hides a window with an animation
1471 * NO ANIMATION YET
1473 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
1475 FIXME("partial stub\n");
1477 /* If trying to show/hide and it's already *
1478 * shown/hidden or invalid window, fail with *
1479 * invalid parameter */
1480 if(!IsWindow(hwnd) ||
1481 (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
1482 (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
1484 SetLastError(ERROR_INVALID_PARAMETER);
1485 return FALSE;
1488 ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1490 return TRUE;
1493 /***********************************************************************
1494 * SetInternalWindowPos (USER32.@)
1496 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1497 LPRECT rect, LPPOINT pt )
1499 WINDOWPLACEMENT wndpl;
1500 UINT flags;
1502 wndpl.length = sizeof(wndpl);
1503 wndpl.showCmd = showCmd;
1504 wndpl.flags = flags = 0;
1506 if( pt )
1508 flags |= PLACE_MIN;
1509 wndpl.flags |= WPF_SETMINPOSITION;
1510 wndpl.ptMinPosition = *pt;
1512 if( rect )
1514 flags |= PLACE_RECT;
1515 wndpl.rcNormalPosition = *rect;
1517 WINPOS_SetPlacement( hwnd, &wndpl, flags );
1521 /*******************************************************************
1522 * can_activate_window
1524 * Check if we can activate the specified window.
1526 static BOOL can_activate_window( HWND hwnd )
1528 LONG style;
1530 if (!hwnd) return FALSE;
1531 style = GetWindowLongW( hwnd, GWL_STYLE );
1532 if (!(style & WS_VISIBLE)) return FALSE;
1533 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1534 return !(style & WS_DISABLED);
1538 /*******************************************************************
1539 * WINPOS_ActivateOtherWindow
1541 * Activates window other than pWnd.
1543 void WINPOS_ActivateOtherWindow(HWND hwnd)
1545 HWND hwndTo, fg;
1547 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1549 hwndTo = GetAncestor( hwndTo, GA_ROOT );
1550 if (can_activate_window( hwndTo )) goto done;
1553 hwndTo = hwnd;
1554 for (;;)
1556 if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1557 if (can_activate_window( hwndTo )) goto done;
1560 hwndTo = GetTopWindow( 0 );
1561 for (;;)
1563 if (hwndTo == hwnd)
1565 hwndTo = 0;
1566 break;
1568 if (can_activate_window( hwndTo )) goto done;
1569 if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1572 done:
1573 fg = GetForegroundWindow();
1574 TRACE("win = %p fg = %p\n", hwndTo, fg);
1575 if (!fg || (hwnd == fg))
1577 if (SetForegroundWindow( hwndTo )) return;
1579 if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1583 /***********************************************************************
1584 * WINPOS_HandleWindowPosChanging
1586 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1588 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1590 POINT minTrack, maxTrack;
1591 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1593 if (winpos->flags & SWP_NOSIZE) return 0;
1594 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1596 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1597 if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1598 if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1599 if (!(style & WS_MINIMIZE))
1601 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1602 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1605 return 0;
1609 /***********************************************************************
1610 * dump_winpos_flags
1612 static void dump_winpos_flags(UINT flags)
1614 static const DWORD dumped_flags = (SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOREDRAW |
1615 SWP_NOACTIVATE | SWP_FRAMECHANGED | SWP_SHOWWINDOW |
1616 SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOOWNERZORDER |
1617 SWP_NOSENDCHANGING | SWP_DEFERERASE | SWP_ASYNCWINDOWPOS |
1618 SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_STATECHANGED);
1619 TRACE("flags:");
1620 if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1621 if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1622 if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1623 if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1624 if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1625 if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1626 if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1627 if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1628 if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1629 if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1630 if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1631 if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1632 if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1633 if(flags & SWP_NOCLIENTSIZE) TRACE(" SWP_NOCLIENTSIZE");
1634 if(flags & SWP_NOCLIENTMOVE) TRACE(" SWP_NOCLIENTMOVE");
1635 if(flags & SWP_STATECHANGED) TRACE(" SWP_STATECHANGED");
1637 if(flags & ~dumped_flags) TRACE(" %08x", flags & ~dumped_flags);
1638 TRACE("\n");
1641 /***********************************************************************
1642 * SWP_DoWinPosChanging
1644 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
1646 WND *wndPtr;
1647 RECT window_rect, client_rect;
1649 /* Send WM_WINDOWPOSCHANGING message */
1651 if (!(pWinpos->flags & SWP_NOSENDCHANGING)
1652 && !((pWinpos->flags & SWP_AGG_NOCLIENTCHANGE) && (pWinpos->flags & SWP_SHOWWINDOW)))
1653 SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
1655 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) ||
1656 wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
1658 /* Calculate new position and size */
1660 WIN_GetRectangles( pWinpos->hwnd, COORDS_PARENT, &window_rect, &client_rect );
1661 *pNewWindowRect = window_rect;
1662 *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? window_rect : client_rect;
1664 if (!(pWinpos->flags & SWP_NOSIZE))
1666 if (wndPtr->dwStyle & WS_MINIMIZE)
1668 pNewWindowRect->right = pNewWindowRect->left + GetSystemMetrics(SM_CXICON);
1669 pNewWindowRect->bottom = pNewWindowRect->top + GetSystemMetrics(SM_CYICON);
1671 else
1673 pNewWindowRect->right = pNewWindowRect->left + pWinpos->cx;
1674 pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
1677 if (!(pWinpos->flags & SWP_NOMOVE))
1679 pNewWindowRect->left = pWinpos->x;
1680 pNewWindowRect->top = pWinpos->y;
1681 pNewWindowRect->right += pWinpos->x - window_rect.left;
1682 pNewWindowRect->bottom += pWinpos->y - window_rect.top;
1684 OffsetRect( pNewClientRect, pWinpos->x - window_rect.left,
1685 pWinpos->y - window_rect.top );
1687 pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
1689 TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
1690 pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
1691 pWinpos->cx, pWinpos->cy, pWinpos->flags );
1692 TRACE( "current %s style %08x new %s\n",
1693 wine_dbgstr_rect( &window_rect ), wndPtr->dwStyle,
1694 wine_dbgstr_rect( pNewWindowRect ));
1696 WIN_ReleasePtr( wndPtr );
1697 return TRUE;
1700 /***********************************************************************
1701 * get_valid_rects
1703 * Compute the valid rects from the old and new client rect and WVR_* flags.
1704 * Helper for WM_NCCALCSIZE handling.
1706 static inline void get_valid_rects( const RECT *old_client, const RECT *new_client, UINT flags,
1707 RECT *valid )
1709 int cx, cy;
1711 if (flags & WVR_REDRAW)
1713 SetRectEmpty( &valid[0] );
1714 SetRectEmpty( &valid[1] );
1715 return;
1718 if (flags & WVR_VALIDRECTS)
1720 if (!IntersectRect( &valid[0], &valid[0], new_client ) ||
1721 !IntersectRect( &valid[1], &valid[1], old_client ))
1723 SetRectEmpty( &valid[0] );
1724 SetRectEmpty( &valid[1] );
1725 return;
1727 flags = WVR_ALIGNLEFT | WVR_ALIGNTOP;
1729 else
1731 valid[0] = *new_client;
1732 valid[1] = *old_client;
1735 /* make sure the rectangles have the same size */
1736 cx = min( valid[0].right - valid[0].left, valid[1].right - valid[1].left );
1737 cy = min( valid[0].bottom - valid[0].top, valid[1].bottom - valid[1].top );
1739 if (flags & WVR_ALIGNBOTTOM)
1741 valid[0].top = valid[0].bottom - cy;
1742 valid[1].top = valid[1].bottom - cy;
1744 else
1746 valid[0].bottom = valid[0].top + cy;
1747 valid[1].bottom = valid[1].top + cy;
1749 if (flags & WVR_ALIGNRIGHT)
1751 valid[0].left = valid[0].right - cx;
1752 valid[1].left = valid[1].right - cx;
1754 else
1756 valid[0].right = valid[0].left + cx;
1757 valid[1].right = valid[1].left + cx;
1762 /***********************************************************************
1763 * SWP_DoOwnedPopups
1765 * fix Z order taking into account owned popups -
1766 * basically we need to maintain them above the window that owns them
1768 * FIXME: hide/show owned popups when owner visibility changes.
1770 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
1772 HWND owner, *list = NULL;
1773 unsigned int i;
1775 TRACE("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
1777 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) return hwndInsertAfter;
1779 if ((owner = GetWindow( hwnd, GW_OWNER )))
1781 /* make sure this popup stays above the owner */
1783 if (hwndInsertAfter != HWND_TOPMOST)
1785 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return hwndInsertAfter;
1787 for (i = 0; list[i]; i++)
1789 BOOL topmost = (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST) != 0;
1791 if (list[i] == owner)
1793 if (i > 0) hwndInsertAfter = list[i-1];
1794 else hwndInsertAfter = topmost ? HWND_TOPMOST : HWND_TOP;
1795 break;
1798 if (hwndInsertAfter == HWND_TOP || hwndInsertAfter == HWND_NOTOPMOST)
1800 if (!topmost) break;
1802 else if (list[i] == hwndInsertAfter) break;
1807 if (hwndInsertAfter == HWND_BOTTOM) goto done;
1808 if (!list && !(list = WIN_ListChildren( GetDesktopWindow() ))) goto done;
1810 i = 0;
1811 if (hwndInsertAfter == HWND_TOP || hwndInsertAfter == HWND_NOTOPMOST)
1813 if (hwndInsertAfter == HWND_NOTOPMOST || !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_TOPMOST))
1815 /* skip all the topmost windows */
1816 while (list[i] && (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST)) i++;
1819 else if (hwndInsertAfter != HWND_TOPMOST)
1821 /* skip windows that are already placed correctly */
1822 for (i = 0; list[i]; i++)
1824 if (list[i] == hwndInsertAfter) break;
1825 if (list[i] == hwnd) goto done; /* nothing to do if window is moving backwards in z-order */
1829 for ( ; list[i]; i++)
1831 if (list[i] == hwnd) break;
1832 if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
1833 TRACE( "moving %p owned by %p after %p\n", list[i], hwnd, hwndInsertAfter );
1834 SetWindowPos( list[i], hwndInsertAfter, 0, 0, 0, 0,
1835 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING | SWP_DEFERERASE );
1836 hwndInsertAfter = list[i];
1839 done:
1840 HeapFree( GetProcessHeap(), 0, list );
1841 return hwndInsertAfter;
1844 /***********************************************************************
1845 * SWP_DoNCCalcSize
1847 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect,
1848 RECT *validRects )
1850 UINT wvrFlags = 0;
1851 RECT window_rect, client_rect;
1853 WIN_GetRectangles( pWinpos->hwnd, COORDS_PARENT, &window_rect, &client_rect );
1855 /* Send WM_NCCALCSIZE message to get new client area */
1856 if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
1858 NCCALCSIZE_PARAMS params;
1859 WINDOWPOS winposCopy;
1861 params.rgrc[0] = *pNewWindowRect;
1862 params.rgrc[1] = window_rect;
1863 params.rgrc[2] = client_rect;
1864 params.lppos = &winposCopy;
1865 winposCopy = *pWinpos;
1867 wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
1869 *pNewClientRect = params.rgrc[0];
1871 TRACE( "hwnd %p old win %s old client %s new win %s new client %s\n", pWinpos->hwnd,
1872 wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(&client_rect),
1873 wine_dbgstr_rect(pNewWindowRect), wine_dbgstr_rect(pNewClientRect) );
1875 if( pNewClientRect->left != client_rect.left ||
1876 pNewClientRect->top != client_rect.top )
1877 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1879 if( (pNewClientRect->right - pNewClientRect->left !=
1880 client_rect.right - client_rect.left))
1881 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1882 else
1883 wvrFlags &= ~WVR_HREDRAW;
1885 if (pNewClientRect->bottom - pNewClientRect->top !=
1886 client_rect.bottom - client_rect.top)
1887 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1888 else
1889 wvrFlags &= ~WVR_VREDRAW;
1891 validRects[0] = params.rgrc[1];
1892 validRects[1] = params.rgrc[2];
1894 else
1896 if (!(pWinpos->flags & SWP_NOMOVE) &&
1897 (pNewClientRect->left != client_rect.left ||
1898 pNewClientRect->top != client_rect.top))
1899 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1902 if (pWinpos->flags & (SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
1904 SetRectEmpty( &validRects[0] );
1905 SetRectEmpty( &validRects[1] );
1907 else get_valid_rects( &client_rect, pNewClientRect, wvrFlags, validRects );
1909 return wvrFlags;
1912 /* fix redundant flags and values in the WINDOWPOS structure */
1913 static BOOL fixup_flags( WINDOWPOS *winpos )
1915 HWND parent;
1916 RECT window_rect;
1917 POINT pt;
1918 WND *wndPtr = WIN_GetPtr( winpos->hwnd );
1919 BOOL ret = TRUE;
1921 if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
1923 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1924 return FALSE;
1926 winpos->hwnd = wndPtr->obj.handle; /* make it a full handle */
1928 /* Finally make sure that all coordinates are valid */
1929 if (winpos->x < -32768) winpos->x = -32768;
1930 else if (winpos->x > 32767) winpos->x = 32767;
1931 if (winpos->y < -32768) winpos->y = -32768;
1932 else if (winpos->y > 32767) winpos->y = 32767;
1934 if (winpos->cx < 0) winpos->cx = 0;
1935 else if (winpos->cx > 32767) winpos->cx = 32767;
1936 if (winpos->cy < 0) winpos->cy = 0;
1937 else if (winpos->cy > 32767) winpos->cy = 32767;
1939 parent = GetAncestor( winpos->hwnd, GA_PARENT );
1940 if (!IsWindowVisible( parent )) winpos->flags |= SWP_NOREDRAW;
1942 if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
1943 else
1945 winpos->flags &= ~SWP_HIDEWINDOW;
1946 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
1949 WIN_GetRectangles( winpos->hwnd, COORDS_SCREEN, &window_rect, NULL );
1950 if ((window_rect.right - window_rect.left == winpos->cx) &&
1951 (window_rect.bottom - window_rect.top == winpos->cy))
1952 winpos->flags |= SWP_NOSIZE; /* Already the right size */
1954 pt.x = winpos->x;
1955 pt.y = winpos->y;
1956 ClientToScreen( parent, &pt );
1957 if ((window_rect.left == pt.x) && (window_rect.top == pt.y))
1958 winpos->flags |= SWP_NOMOVE; /* Already the right position */
1960 if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
1962 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)) && /* Bring to the top when activating */
1963 (winpos->flags & SWP_NOZORDER ||
1964 (winpos->hwndInsertAfter != HWND_TOPMOST && winpos->hwndInsertAfter != HWND_NOTOPMOST)))
1966 winpos->flags &= ~SWP_NOZORDER;
1967 winpos->hwndInsertAfter = HWND_TOP;
1971 /* Check hwndInsertAfter */
1972 if (winpos->flags & SWP_NOZORDER) goto done;
1974 if (winpos->hwndInsertAfter == HWND_TOP)
1976 if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1977 winpos->flags |= SWP_NOZORDER;
1979 else if (winpos->hwndInsertAfter == HWND_BOTTOM)
1981 if (!(wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
1982 winpos->flags |= SWP_NOZORDER;
1984 else if (winpos->hwndInsertAfter == HWND_TOPMOST)
1986 if ((wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1987 winpos->flags |= SWP_NOZORDER;
1989 else if (winpos->hwndInsertAfter == HWND_NOTOPMOST)
1991 if (!(wndPtr->dwExStyle & WS_EX_TOPMOST))
1992 winpos->flags |= SWP_NOZORDER;
1994 else
1996 if ((winpos->hwnd == winpos->hwndInsertAfter) ||
1997 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
1998 winpos->flags |= SWP_NOZORDER;
2000 done:
2001 WIN_ReleasePtr( wndPtr );
2002 return ret;
2006 /***********************************************************************
2007 * update_surface_region
2009 static void update_surface_region( HWND hwnd )
2011 NTSTATUS status;
2012 HRGN region = 0;
2013 RGNDATA *data;
2014 size_t size = 256;
2015 WND *win = WIN_GetPtr( hwnd );
2017 if (!win || win == WND_DESKTOP || win == WND_OTHER_PROCESS) return;
2018 if (!win->surface) goto done;
2022 if (!(data = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( RGNDATA, Buffer[size] )))) goto done;
2024 SERVER_START_REQ( get_surface_region )
2026 req->window = wine_server_user_handle( hwnd );
2027 wine_server_set_reply( req, data->Buffer, size );
2028 if (!(status = wine_server_call( req )))
2030 size_t reply_size = wine_server_reply_size( reply );
2031 if (reply_size)
2033 data->rdh.dwSize = sizeof(data->rdh);
2034 data->rdh.iType = RDH_RECTANGLES;
2035 data->rdh.nCount = reply_size / sizeof(RECT);
2036 data->rdh.nRgnSize = reply_size;
2037 region = ExtCreateRegion( NULL, data->rdh.dwSize + data->rdh.nRgnSize, data );
2038 OffsetRgn( region, -reply->visible_rect.left, -reply->visible_rect.top );
2041 else size = reply->total_size;
2043 SERVER_END_REQ;
2044 HeapFree( GetProcessHeap(), 0, data );
2045 } while (status == STATUS_BUFFER_OVERFLOW);
2047 if (status) goto done;
2049 win->surface->funcs->set_region( win->surface, region );
2050 if (region) DeleteObject( region );
2052 done:
2053 WIN_ReleasePtr( win );
2057 /***********************************************************************
2058 * set_window_pos
2060 * Backend implementation of SetWindowPos.
2062 BOOL set_window_pos( HWND hwnd, HWND insert_after, UINT swp_flags,
2063 const RECT *window_rect, const RECT *client_rect, const RECT *valid_rects )
2065 WND *win;
2066 HWND surface_win = 0, parent = GetAncestor( hwnd, GA_PARENT );
2067 BOOL ret, needs_update = FALSE;
2068 RECT visible_rect, old_visible_rect, old_window_rect, old_client_rect;
2069 struct window_surface *old_surface, *new_surface = NULL;
2071 if (!parent || parent == GetDesktopWindow())
2073 new_surface = &dummy_surface; /* provide a default surface for top-level windows */
2074 window_surface_add_ref( new_surface );
2076 visible_rect = *window_rect;
2077 USER_Driver->pWindowPosChanging( hwnd, insert_after, swp_flags,
2078 window_rect, client_rect, &visible_rect, &new_surface );
2080 WIN_GetRectangles( hwnd, COORDS_SCREEN, &old_window_rect, NULL );
2082 if (!(win = WIN_GetPtr( hwnd )) || win == WND_DESKTOP || win == WND_OTHER_PROCESS)
2084 if (new_surface) window_surface_release( new_surface );
2085 return FALSE;
2087 old_visible_rect = win->visible_rect;
2088 old_client_rect = win->rectClient;
2089 old_surface = win->surface;
2090 if (old_surface != new_surface) swp_flags |= SWP_FRAMECHANGED; /* force refreshing non-client area */
2092 SERVER_START_REQ( set_window_pos )
2094 req->handle = wine_server_user_handle( hwnd );
2095 req->previous = wine_server_user_handle( insert_after );
2096 req->swp_flags = swp_flags;
2097 req->window.left = window_rect->left;
2098 req->window.top = window_rect->top;
2099 req->window.right = window_rect->right;
2100 req->window.bottom = window_rect->bottom;
2101 req->client.left = client_rect->left;
2102 req->client.top = client_rect->top;
2103 req->client.right = client_rect->right;
2104 req->client.bottom = client_rect->bottom;
2105 if (!EqualRect( window_rect, &visible_rect ) || !IsRectEmpty( &valid_rects[0] ))
2107 wine_server_add_data( req, &visible_rect, sizeof(visible_rect) );
2108 if (!IsRectEmpty( &valid_rects[0] ))
2109 wine_server_add_data( req, valid_rects, 2 * sizeof(*valid_rects) );
2111 if (new_surface) req->paint_flags |= SET_WINPOS_PAINT_SURFACE;
2112 if (win->pixel_format) req->paint_flags |= SET_WINPOS_PIXEL_FORMAT;
2114 if ((ret = !wine_server_call( req )))
2116 win->dwStyle = reply->new_style;
2117 win->dwExStyle = reply->new_ex_style;
2118 win->rectWindow = *window_rect;
2119 win->rectClient = *client_rect;
2120 win->visible_rect = visible_rect;
2121 win->surface = new_surface;
2122 surface_win = wine_server_ptr_handle( reply->surface_win );
2123 needs_update = reply->needs_update;
2124 if (GetWindowLongW( win->parent, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
2126 RECT client;
2127 GetClientRect( win->parent, &client );
2128 mirror_rect( &client, &win->rectWindow );
2129 mirror_rect( &client, &win->rectClient );
2130 mirror_rect( &client, &win->visible_rect );
2132 /* if an RTL window is resized the children have moved */
2133 if (win->dwExStyle & WS_EX_LAYOUTRTL &&
2134 client_rect->right - client_rect->left != old_client_rect.right - old_client_rect.left)
2135 win->flags |= WIN_CHILDREN_MOVED;
2138 SERVER_END_REQ;
2140 if (ret)
2142 if (needs_update) update_surface_region( surface_win );
2143 if (((swp_flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) ||
2144 (swp_flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW | SWP_STATECHANGED | SWP_FRAMECHANGED)))
2145 invalidate_dce( win, &old_window_rect );
2148 WIN_ReleasePtr( win );
2150 if (ret)
2152 TRACE( "win %p surface %p -> %p\n", hwnd, old_surface, new_surface );
2153 register_window_surface( old_surface, new_surface );
2154 if (old_surface)
2156 if (!IsRectEmpty( valid_rects ))
2158 move_window_bits( hwnd, old_surface, new_surface, &visible_rect,
2159 &old_visible_rect, window_rect, valid_rects );
2160 valid_rects = NULL; /* prevent the driver from trying to also move the bits */
2162 window_surface_release( old_surface );
2164 else if (surface_win && surface_win != hwnd)
2166 if (!IsRectEmpty( valid_rects ))
2168 RECT rects[2];
2169 int x_offset = old_visible_rect.left - visible_rect.left;
2170 int y_offset = old_visible_rect.top - visible_rect.top;
2172 /* if all that happened is that the whole window moved, copy everything */
2173 if (!(swp_flags & SWP_FRAMECHANGED) &&
2174 old_visible_rect.right - visible_rect.right == x_offset &&
2175 old_visible_rect.bottom - visible_rect.bottom == y_offset &&
2176 old_client_rect.left - client_rect->left == x_offset &&
2177 old_client_rect.right - client_rect->right == x_offset &&
2178 old_client_rect.top - client_rect->top == y_offset &&
2179 old_client_rect.bottom - client_rect->bottom == y_offset &&
2180 EqualRect( &valid_rects[0], client_rect ))
2182 rects[0] = visible_rect;
2183 rects[1] = old_visible_rect;
2184 valid_rects = rects;
2186 move_window_bits_parent( hwnd, surface_win, window_rect, valid_rects );
2187 valid_rects = NULL; /* prevent the driver from trying to also move the bits */
2191 USER_Driver->pWindowPosChanged( hwnd, insert_after, swp_flags, window_rect,
2192 client_rect, &visible_rect, valid_rects, new_surface );
2194 else if (new_surface) window_surface_release( new_surface );
2196 return ret;
2200 /***********************************************************************
2201 * USER_SetWindowPos
2203 * User32 internal function
2205 BOOL USER_SetWindowPos( WINDOWPOS * winpos )
2207 RECT newWindowRect, newClientRect, valid_rects[2];
2208 UINT orig_flags;
2210 orig_flags = winpos->flags;
2212 /* First, check z-order arguments. */
2213 if (!(winpos->flags & SWP_NOZORDER))
2215 /* fix sign extension */
2216 if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
2217 else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
2219 if (!(winpos->hwndInsertAfter == HWND_TOP ||
2220 winpos->hwndInsertAfter == HWND_BOTTOM ||
2221 winpos->hwndInsertAfter == HWND_TOPMOST ||
2222 winpos->hwndInsertAfter == HWND_NOTOPMOST))
2224 HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
2225 HWND insertafter_parent = GetAncestor( winpos->hwndInsertAfter, GA_PARENT );
2227 /* hwndInsertAfter must be a sibling of the window */
2228 if (!insertafter_parent) return FALSE;
2229 if (insertafter_parent != parent) return TRUE;
2233 /* Make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
2234 if (!(winpos->flags & SWP_NOMOVE))
2236 if (winpos->x < -32768) winpos->x = -32768;
2237 else if (winpos->x > 32767) winpos->x = 32767;
2238 if (winpos->y < -32768) winpos->y = -32768;
2239 else if (winpos->y > 32767) winpos->y = 32767;
2241 if (!(winpos->flags & SWP_NOSIZE))
2243 if (winpos->cx < 0) winpos->cx = 0;
2244 else if (winpos->cx > 32767) winpos->cx = 32767;
2245 if (winpos->cy < 0) winpos->cy = 0;
2246 else if (winpos->cy > 32767) winpos->cy = 32767;
2249 if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
2251 /* Fix redundant flags */
2252 if (!fixup_flags( winpos )) return FALSE;
2254 if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
2256 if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
2257 winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
2260 /* Common operations */
2262 SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect, valid_rects );
2264 if (!set_window_pos( winpos->hwnd, winpos->hwndInsertAfter, winpos->flags,
2265 &newWindowRect, &newClientRect, valid_rects ))
2266 return FALSE;
2270 if( winpos->flags & SWP_HIDEWINDOW )
2271 HideCaret(winpos->hwnd);
2272 else if (winpos->flags & SWP_SHOWWINDOW)
2273 ShowCaret(winpos->hwnd);
2275 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)))
2277 /* child windows get WM_CHILDACTIVATE message */
2278 if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
2279 SendMessageW( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
2280 else
2281 SetForegroundWindow( winpos->hwnd );
2284 if(!(orig_flags & SWP_DEFERERASE))
2286 /* erase parent when hiding or resizing child */
2287 if ((orig_flags & SWP_HIDEWINDOW) ||
2288 (!(orig_flags & SWP_SHOWWINDOW) &&
2289 (winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOGEOMETRYCHANGE))
2291 HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
2292 if (!parent || parent == GetDesktopWindow()) parent = winpos->hwnd;
2293 erase_now( parent, 0 );
2296 /* Give newly shown windows a chance to redraw */
2297 if(((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE)
2298 && !(orig_flags & SWP_AGG_NOCLIENTCHANGE) && (orig_flags & SWP_SHOWWINDOW))
2300 erase_now(winpos->hwnd, 0);
2304 /* And last, send the WM_WINDOWPOSCHANGED message */
2306 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
2308 if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE)
2309 && !((orig_flags & SWP_AGG_NOCLIENTCHANGE) && (orig_flags & SWP_SHOWWINDOW)))
2311 /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
2312 and always contains final window position.
2314 winpos->x = newWindowRect.left;
2315 winpos->y = newWindowRect.top;
2316 winpos->cx = newWindowRect.right - newWindowRect.left;
2317 winpos->cy = newWindowRect.bottom - newWindowRect.top;
2318 SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
2320 return TRUE;
2323 /***********************************************************************
2324 * SetWindowPos (USER32.@)
2326 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
2327 INT x, INT y, INT cx, INT cy, UINT flags )
2329 WINDOWPOS winpos;
2331 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2332 hwnd, hwndInsertAfter, x, y, cx, cy, flags);
2333 if(TRACE_ON(win)) dump_winpos_flags(flags);
2335 if (is_broadcast(hwnd))
2337 SetLastError( ERROR_INVALID_PARAMETER );
2338 return FALSE;
2341 winpos.hwnd = WIN_GetFullHandle(hwnd);
2342 winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
2343 winpos.x = x;
2344 winpos.y = y;
2345 winpos.cx = cx;
2346 winpos.cy = cy;
2347 winpos.flags = flags;
2349 if (WIN_IsCurrentThread( hwnd ))
2350 return USER_SetWindowPos(&winpos);
2352 return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
2356 /***********************************************************************
2357 * BeginDeferWindowPos (USER32.@)
2359 HDWP WINAPI BeginDeferWindowPos( INT count )
2361 HDWP handle = 0;
2362 DWP *pDWP;
2364 TRACE("%d\n", count);
2366 if (count < 0)
2368 SetLastError(ERROR_INVALID_PARAMETER);
2369 return 0;
2371 /* Windows allows zero count, in which case it allocates context for 8 moves */
2372 if (count == 0) count = 8;
2374 if (!(pDWP = HeapAlloc( GetProcessHeap(), 0, sizeof(DWP)))) return 0;
2376 pDWP->actualCount = 0;
2377 pDWP->suggestedCount = count;
2378 pDWP->hwndParent = 0;
2380 if (!(pDWP->winPos = HeapAlloc( GetProcessHeap(), 0, count * sizeof(WINDOWPOS) )) ||
2381 !(handle = alloc_user_handle( &pDWP->obj, USER_DWP )))
2383 HeapFree( GetProcessHeap(), 0, pDWP->winPos );
2384 HeapFree( GetProcessHeap(), 0, pDWP );
2387 TRACE("returning hdwp %p\n", handle);
2388 return handle;
2392 /***********************************************************************
2393 * DeferWindowPos (USER32.@)
2395 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
2396 INT x, INT y, INT cx, INT cy,
2397 UINT flags )
2399 DWP *pDWP;
2400 int i;
2401 HDWP retvalue = hdwp;
2403 TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2404 hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
2406 hwnd = WIN_GetFullHandle( hwnd );
2407 if (is_desktop_window( hwnd ) || !IsWindow( hwnd ))
2409 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2410 return 0;
2413 if (!(pDWP = get_user_handle_ptr( hdwp, USER_DWP ))) return 0;
2414 if (pDWP == OBJ_OTHER_PROCESS)
2416 FIXME( "other process handle %p?\n", hdwp );
2417 return 0;
2420 for (i = 0; i < pDWP->actualCount; i++)
2422 if (pDWP->winPos[i].hwnd == hwnd)
2424 /* Merge with the other changes */
2425 if (!(flags & SWP_NOZORDER))
2427 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
2429 if (!(flags & SWP_NOMOVE))
2431 pDWP->winPos[i].x = x;
2432 pDWP->winPos[i].y = y;
2434 if (!(flags & SWP_NOSIZE))
2436 pDWP->winPos[i].cx = cx;
2437 pDWP->winPos[i].cy = cy;
2439 pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
2440 SWP_NOZORDER | SWP_NOREDRAW |
2441 SWP_NOACTIVATE | SWP_NOCOPYBITS|
2442 SWP_NOOWNERZORDER);
2443 pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
2444 SWP_FRAMECHANGED);
2445 goto END;
2448 if (pDWP->actualCount >= pDWP->suggestedCount)
2450 WINDOWPOS *newpos = HeapReAlloc( GetProcessHeap(), 0, pDWP->winPos,
2451 pDWP->suggestedCount * 2 * sizeof(WINDOWPOS) );
2452 if (!newpos)
2454 retvalue = 0;
2455 goto END;
2457 pDWP->suggestedCount *= 2;
2458 pDWP->winPos = newpos;
2460 pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
2461 pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
2462 pDWP->winPos[pDWP->actualCount].x = x;
2463 pDWP->winPos[pDWP->actualCount].y = y;
2464 pDWP->winPos[pDWP->actualCount].cx = cx;
2465 pDWP->winPos[pDWP->actualCount].cy = cy;
2466 pDWP->winPos[pDWP->actualCount].flags = flags;
2467 pDWP->actualCount++;
2468 END:
2469 release_user_handle_ptr( pDWP );
2470 return retvalue;
2474 /***********************************************************************
2475 * EndDeferWindowPos (USER32.@)
2477 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
2479 DWP *pDWP;
2480 WINDOWPOS *winpos;
2481 int i;
2483 TRACE("%p\n", hdwp);
2485 if (!(pDWP = free_user_handle( hdwp, USER_DWP ))) return FALSE;
2486 if (pDWP == OBJ_OTHER_PROCESS)
2488 FIXME( "other process handle %p?\n", hdwp );
2489 return FALSE;
2492 for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
2494 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2495 winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
2496 winpos->cx, winpos->cy, winpos->flags);
2498 if (WIN_IsCurrentThread( winpos->hwnd ))
2499 USER_SetWindowPos( winpos );
2500 else
2501 SendMessageW( winpos->hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)winpos );
2503 HeapFree( GetProcessHeap(), 0, pDWP->winPos );
2504 HeapFree( GetProcessHeap(), 0, pDWP );
2505 return TRUE;
2509 /***********************************************************************
2510 * ArrangeIconicWindows (USER32.@)
2512 UINT WINAPI ArrangeIconicWindows( HWND parent )
2514 RECT rectParent;
2515 HWND hwndChild;
2516 INT x, y, xspacing, yspacing;
2517 POINT pt;
2518 MINIMIZEDMETRICS metrics;
2520 metrics.cbSize = sizeof(metrics);
2521 SystemParametersInfoW( SPI_GETMINIMIZEDMETRICS, sizeof(metrics), &metrics, 0 );
2523 if (parent == GetDesktopWindow())
2525 MONITORINFO mon_info;
2526 HMONITOR monitor = MonitorFromWindow( 0, MONITOR_DEFAULTTOPRIMARY );
2528 mon_info.cbSize = sizeof( mon_info );
2529 GetMonitorInfoW( monitor, &mon_info );
2530 rectParent = mon_info.rcWork;
2532 else GetClientRect( parent, &rectParent );
2534 x = y = 0;
2535 xspacing = GetSystemMetrics(SM_CXICONSPACING);
2536 yspacing = GetSystemMetrics(SM_CYICONSPACING);
2538 hwndChild = GetWindow( parent, GW_CHILD );
2539 while (hwndChild)
2541 if( IsIconic( hwndChild ) )
2543 WINPOS_ShowIconTitle( hwndChild, FALSE );
2545 if (metrics.iArrange & ARW_STARTRIGHT)
2546 pt.x = rectParent.right - (x + 1) * xspacing;
2547 else
2548 pt.x = rectParent.left + x * xspacing;
2549 if (metrics.iArrange & ARW_STARTTOP)
2550 pt.y = rectParent.top + y * yspacing;
2551 else
2552 pt.y = rectParent.bottom - (y + 1) * yspacing;
2554 SetWindowPos( hwndChild, 0, pt.x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
2555 pt.y + (yspacing - GetSystemMetrics(SM_CYICON)) / 2, 0, 0,
2556 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
2557 if( IsWindow(hwndChild) )
2558 WINPOS_ShowIconTitle(hwndChild , TRUE );
2560 if (++x >= (rectParent.right - rectParent.left) / xspacing)
2562 x = 0;
2563 y++;
2566 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
2568 return yspacing;
2572 /***********************************************************************
2573 * draw_moving_frame
2575 * Draw the frame used when moving or resizing window.
2577 static void draw_moving_frame( HWND parent, HDC hdc, RECT *screen_rect, BOOL thickframe )
2579 RECT rect = *screen_rect;
2581 if (parent) MapWindowPoints( 0, parent, (POINT *)&rect, 2 );
2582 if (thickframe)
2584 const int width = GetSystemMetrics(SM_CXFRAME);
2585 const int height = GetSystemMetrics(SM_CYFRAME);
2587 HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
2588 PatBlt( hdc, rect.left, rect.top,
2589 rect.right - rect.left - width, height, PATINVERT );
2590 PatBlt( hdc, rect.left, rect.top + height, width,
2591 rect.bottom - rect.top - height, PATINVERT );
2592 PatBlt( hdc, rect.left + width, rect.bottom - 1,
2593 rect.right - rect.left - width, -height, PATINVERT );
2594 PatBlt( hdc, rect.right - 1, rect.top, -width,
2595 rect.bottom - rect.top - height, PATINVERT );
2596 SelectObject( hdc, hbrush );
2598 else DrawFocusRect( hdc, &rect );
2602 /***********************************************************************
2603 * start_size_move
2605 * Initialization of a move or resize, when initiated from a menu choice.
2606 * Return hit test code for caption or sizing border.
2608 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
2610 LONG hittest = 0;
2611 POINT pt;
2612 MSG msg;
2613 RECT rectWindow;
2615 GetWindowRect( hwnd, &rectWindow );
2617 if ((wParam & 0xfff0) == SC_MOVE)
2619 /* Move pointer at the center of the caption */
2620 RECT rect = rectWindow;
2621 /* Note: to be exactly centered we should take the different types
2622 * of border into account, but it shouldn't make more than a few pixels
2623 * of difference so let's not bother with that */
2624 rect.top += GetSystemMetrics(SM_CYBORDER);
2625 if (style & WS_SYSMENU)
2626 rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
2627 if (style & WS_MINIMIZEBOX)
2628 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
2629 if (style & WS_MAXIMIZEBOX)
2630 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
2631 pt.x = (rect.right + rect.left) / 2;
2632 pt.y = rect.top + GetSystemMetrics(SM_CYSIZE)/2;
2633 hittest = HTCAPTION;
2634 *capturePoint = pt;
2636 else /* SC_SIZE */
2638 SetCursor( LoadCursorW( 0, (LPWSTR)IDC_SIZEALL ) );
2639 pt.x = pt.y = 0;
2640 while(!hittest)
2642 if (!GetMessageW( &msg, 0, 0, 0 )) return 0;
2643 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2645 switch(msg.message)
2647 case WM_MOUSEMOVE:
2648 pt.x = min( max( msg.pt.x, rectWindow.left ), rectWindow.right - 1 );
2649 pt.y = min( max( msg.pt.y, rectWindow.top ), rectWindow.bottom - 1 );
2650 hittest = SendMessageW( hwnd, WM_NCHITTEST, 0, MAKELONG( pt.x, pt.y ) );
2651 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT)) hittest = 0;
2652 break;
2654 case WM_LBUTTONUP:
2655 return 0;
2657 case WM_KEYDOWN:
2658 switch(msg.wParam)
2660 case VK_UP:
2661 hittest = HTTOP;
2662 pt.x =(rectWindow.left+rectWindow.right)/2;
2663 pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
2664 break;
2665 case VK_DOWN:
2666 hittest = HTBOTTOM;
2667 pt.x =(rectWindow.left+rectWindow.right)/2;
2668 pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
2669 break;
2670 case VK_LEFT:
2671 hittest = HTLEFT;
2672 pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
2673 pt.y =(rectWindow.top+rectWindow.bottom)/2;
2674 break;
2675 case VK_RIGHT:
2676 hittest = HTRIGHT;
2677 pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
2678 pt.y =(rectWindow.top+rectWindow.bottom)/2;
2679 break;
2680 case VK_RETURN:
2681 case VK_ESCAPE:
2682 return 0;
2684 break;
2685 default:
2686 TranslateMessage( &msg );
2687 DispatchMessageW( &msg );
2688 break;
2691 *capturePoint = pt;
2693 SetCursorPos( pt.x, pt.y );
2694 SendMessageW( hwnd, WM_SETCURSOR, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
2695 return hittest;
2699 /***********************************************************************
2700 * WINPOS_SysCommandSizeMove
2702 * Perform SC_MOVE and SC_SIZE commands.
2704 void WINPOS_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
2706 MSG msg;
2707 RECT sizingRect, mouseRect, origRect;
2708 HDC hdc;
2709 HWND parent;
2710 LONG hittest = (LONG)(wParam & 0x0f);
2711 WPARAM syscommand = wParam & 0xfff0;
2712 HCURSOR hDragCursor = 0, hOldCursor = 0;
2713 POINT minTrack, maxTrack;
2714 POINT capturePoint, pt;
2715 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
2716 BOOL thickframe = HAS_THICKFRAME( style );
2717 BOOL iconic = style & WS_MINIMIZE;
2718 BOOL moved = FALSE;
2719 DWORD dwPoint = GetMessagePos ();
2720 BOOL DragFullWindows = TRUE;
2721 HMONITOR mon = 0;
2723 if (IsZoomed(hwnd) || !IsWindowVisible(hwnd)) return;
2725 pt.x = (short)LOWORD(dwPoint);
2726 pt.y = (short)HIWORD(dwPoint);
2727 capturePoint = pt;
2728 ClipCursor( NULL );
2730 TRACE("hwnd %p command %04lx, hittest %d, pos %d,%d\n",
2731 hwnd, syscommand, hittest, pt.x, pt.y);
2733 if (syscommand == SC_MOVE)
2735 if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
2736 if (!hittest) return;
2738 else /* SC_SIZE */
2740 if ( hittest && (syscommand != SC_MOUSEMENU) )
2741 hittest += (HTLEFT - WMSZ_LEFT);
2742 else
2744 set_capture_window( hwnd, GUI_INMOVESIZE, NULL );
2745 hittest = start_size_move( hwnd, wParam, &capturePoint, style );
2746 if (!hittest)
2748 set_capture_window( 0, GUI_INMOVESIZE, NULL );
2749 return;
2754 /* Get min/max info */
2756 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
2757 WIN_GetRectangles( hwnd, COORDS_PARENT, &sizingRect, NULL );
2758 origRect = sizingRect;
2759 if (style & WS_CHILD)
2761 parent = GetParent(hwnd);
2762 GetClientRect( parent, &mouseRect );
2763 MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
2764 MapWindowPoints( parent, 0, (LPPOINT)&sizingRect, 2 );
2766 else
2768 parent = 0;
2769 mouseRect = get_virtual_screen_rect();
2770 mon = MonitorFromPoint( pt, MONITOR_DEFAULTTONEAREST );
2773 if (ON_LEFT_BORDER(hittest))
2775 mouseRect.left = max( mouseRect.left, sizingRect.right-maxTrack.x+capturePoint.x-sizingRect.left );
2776 mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x+capturePoint.x-sizingRect.left );
2778 else if (ON_RIGHT_BORDER(hittest))
2780 mouseRect.left = max( mouseRect.left, sizingRect.left+minTrack.x+capturePoint.x-sizingRect.right );
2781 mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x+capturePoint.x-sizingRect.right );
2783 if (ON_TOP_BORDER(hittest))
2785 mouseRect.top = max( mouseRect.top, sizingRect.bottom-maxTrack.y+capturePoint.y-sizingRect.top );
2786 mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y+capturePoint.y-sizingRect.top);
2788 else if (ON_BOTTOM_BORDER(hittest))
2790 mouseRect.top = max( mouseRect.top, sizingRect.top+minTrack.y+capturePoint.y-sizingRect.bottom );
2791 mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y+capturePoint.y-sizingRect.bottom );
2794 /* Retrieve a default cache DC (without using the window style) */
2795 hdc = GetDCEx( parent, 0, DCX_CACHE );
2797 if( iconic ) /* create a cursor for dragging */
2799 hDragCursor = (HCURSOR)GetClassLongPtrW( hwnd, GCLP_HICON);
2800 if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageW( hwnd, WM_QUERYDRAGICON, 0, 0L);
2801 if( !hDragCursor ) iconic = FALSE;
2804 /* we only allow disabling the full window drag for child windows */
2805 if (parent) SystemParametersInfoW( SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0 );
2807 /* repaint the window before moving it around */
2808 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
2810 SendMessageW( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
2811 set_capture_window( hwnd, GUI_INMOVESIZE, NULL );
2813 while(1)
2815 int dx = 0, dy = 0;
2817 if (!GetMessageW( &msg, 0, 0, 0 )) break;
2818 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2820 /* Exit on button-up, Return, or Esc */
2821 if ((msg.message == WM_LBUTTONUP) ||
2822 ((msg.message == WM_KEYDOWN) &&
2823 ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
2825 if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
2827 TranslateMessage( &msg );
2828 DispatchMessageW( &msg );
2829 continue; /* We are not interested in other messages */
2832 pt = msg.pt;
2834 if (msg.message == WM_KEYDOWN) switch(msg.wParam)
2836 case VK_UP: pt.y -= 8; break;
2837 case VK_DOWN: pt.y += 8; break;
2838 case VK_LEFT: pt.x -= 8; break;
2839 case VK_RIGHT: pt.x += 8; break;
2842 pt.x = max( pt.x, mouseRect.left );
2843 pt.x = min( pt.x, mouseRect.right - 1 );
2844 pt.y = max( pt.y, mouseRect.top );
2845 pt.y = min( pt.y, mouseRect.bottom - 1 );
2847 if (!parent)
2849 HMONITOR newmon;
2850 MONITORINFO info;
2852 if ((newmon = MonitorFromPoint( pt, MONITOR_DEFAULTTONULL )))
2853 mon = newmon;
2855 info.cbSize = sizeof(info);
2856 if (mon && GetMonitorInfoW( mon, &info ))
2858 pt.x = max( pt.x, info.rcWork.left );
2859 pt.x = min( pt.x, info.rcWork.right - 1 );
2860 pt.y = max( pt.y, info.rcWork.top );
2861 pt.y = min( pt.y, info.rcWork.bottom - 1 );
2865 dx = pt.x - capturePoint.x;
2866 dy = pt.y - capturePoint.y;
2868 if (dx || dy)
2870 if( !moved )
2872 moved = TRUE;
2874 if( iconic ) /* ok, no system popup tracking */
2876 hOldCursor = SetCursor(hDragCursor);
2877 ShowCursor( TRUE );
2878 WINPOS_ShowIconTitle( hwnd, FALSE );
2880 else if(!DragFullWindows)
2881 draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2884 if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
2885 else
2887 if(!iconic && !DragFullWindows) draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2888 if (hittest == HTCAPTION) OffsetRect( &sizingRect, dx, dy );
2889 if (ON_LEFT_BORDER(hittest)) sizingRect.left += dx;
2890 else if (ON_RIGHT_BORDER(hittest)) sizingRect.right += dx;
2891 if (ON_TOP_BORDER(hittest)) sizingRect.top += dy;
2892 else if (ON_BOTTOM_BORDER(hittest)) sizingRect.bottom += dy;
2893 capturePoint = pt;
2895 /* determine the hit location */
2896 if (syscommand == SC_SIZE)
2898 WPARAM wpSizingHit = 0;
2900 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
2901 wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
2902 SendMessageW( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&sizingRect );
2904 else
2905 SendMessageW( hwnd, WM_MOVING, 0, (LPARAM)&sizingRect );
2907 if (!iconic)
2909 if(!DragFullWindows)
2910 draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2911 else
2913 RECT rect = sizingRect;
2914 MapWindowPoints( 0, parent, (POINT *)&rect, 2 );
2915 SetWindowPos( hwnd, 0, rect.left, rect.top,
2916 rect.right - rect.left, rect.bottom - rect.top,
2917 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2924 if( iconic )
2926 if( moved ) /* restore cursors, show icon title later on */
2928 ShowCursor( FALSE );
2929 SetCursor( hOldCursor );
2932 else if (moved && !DragFullWindows)
2934 draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2937 set_capture_window( 0, GUI_INMOVESIZE, NULL );
2938 ReleaseDC( parent, hdc );
2939 if (parent) MapWindowPoints( 0, parent, (POINT *)&sizingRect, 2 );
2941 if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
2942 moved = FALSE;
2944 SendMessageW( hwnd, WM_EXITSIZEMOVE, 0, 0 );
2945 SendMessageW( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
2947 /* window moved or resized */
2948 if (moved)
2950 /* if the moving/resizing isn't canceled call SetWindowPos
2951 * with the new position or the new size of the window
2953 if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
2955 /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
2956 if(!DragFullWindows || iconic)
2957 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
2958 sizingRect.right - sizingRect.left,
2959 sizingRect.bottom - sizingRect.top,
2960 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2962 else
2963 { /* restore previous size/position */
2964 if(DragFullWindows)
2965 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
2966 origRect.right - origRect.left,
2967 origRect.bottom - origRect.top,
2968 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2972 if (IsIconic(hwnd))
2974 /* Single click brings up the system menu when iconized */
2976 if( !moved )
2978 if(style & WS_SYSMENU )
2979 SendMessageW( hwnd, WM_SYSCOMMAND,
2980 SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
2982 else WINPOS_ShowIconTitle( hwnd, TRUE );
2986 /***********************************************************************
2987 * LogicalToPhysicalPoint (USER32.@)
2989 BOOL WINAPI LogicalToPhysicalPoint(HWND hwnd, POINT *point)
2991 static int once;
2993 if (!once++)
2994 FIXME("(%p %p) stub\n", hwnd, point);
2995 return TRUE;
2998 /***********************************************************************
2999 * PhysicalToLogicalPoint (USER32.@)
3001 BOOL WINAPI PhysicalToLogicalPoint(HWND hwnd, POINT *point)
3003 static int once;
3005 if (!once++)
3006 FIXME("(%p %p) stub\n", hwnd, point);
3007 return TRUE;