crypt32/tests: Add tests for decoding enveloped messages.
[wine/multimedia.git] / dlls / user32 / winpos.c
blob716616f19ce18dd29a200123bc51443d344bf261
1 /*
2 * Window position related functions.
4 * Copyright 1993, 1994, 1995 Alexandre Julliard
5 * 1995, 1996, 1999 Alex Korobka
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdarg.h>
26 #include <string.h>
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winerror.h"
33 #include "wine/server.h"
34 #include "controls.h"
35 #include "user_private.h"
36 #include "win.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(win);
41 #define SWP_AGG_NOGEOMETRYCHANGE \
42 (SWP_NOSIZE | SWP_NOCLIENTSIZE | SWP_NOZORDER)
43 #define SWP_AGG_NOPOSCHANGE \
44 (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
45 #define SWP_AGG_STATUSFLAGS \
46 (SWP_AGG_NOPOSCHANGE | SWP_FRAMECHANGED | SWP_HIDEWINDOW | SWP_SHOWWINDOW)
48 #define HAS_DLGFRAME(style,exStyle) \
49 (((exStyle) & WS_EX_DLGMODALFRAME) || \
50 (((style) & WS_DLGFRAME) && !((style) & WS_BORDER)))
52 #define HAS_THICKFRAME(style) \
53 (((style) & WS_THICKFRAME) && \
54 !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
56 #define EMPTYPOINT(pt) ((pt).x == -1 && (pt).y == -1)
58 #define ON_LEFT_BORDER(hit) \
59 (((hit) == HTLEFT) || ((hit) == HTTOPLEFT) || ((hit) == HTBOTTOMLEFT))
60 #define ON_RIGHT_BORDER(hit) \
61 (((hit) == HTRIGHT) || ((hit) == HTTOPRIGHT) || ((hit) == HTBOTTOMRIGHT))
62 #define ON_TOP_BORDER(hit) \
63 (((hit) == HTTOP) || ((hit) == HTTOPLEFT) || ((hit) == HTTOPRIGHT))
64 #define ON_BOTTOM_BORDER(hit) \
65 (((hit) == HTBOTTOM) || ((hit) == HTBOTTOMLEFT) || ((hit) == HTBOTTOMRIGHT))
67 #define PLACE_MIN 0x0001
68 #define PLACE_MAX 0x0002
69 #define PLACE_RECT 0x0004
71 typedef struct
73 struct user_object obj;
74 INT actualCount;
75 INT suggestedCount;
76 HWND hwndParent;
77 WINDOWPOS *winPos;
78 } DWP;
81 /***********************************************************************
82 * SwitchToThisWindow (USER32.@)
84 void WINAPI SwitchToThisWindow( HWND hwnd, BOOL alt_tab )
86 if (IsIconic( hwnd )) ShowWindow( hwnd, SW_RESTORE );
87 else BringWindowToTop( hwnd );
91 /***********************************************************************
92 * GetWindowRect (USER32.@)
94 BOOL WINAPI GetWindowRect( HWND hwnd, LPRECT rect )
96 BOOL ret = WIN_GetRectangles( hwnd, COORDS_SCREEN, rect, NULL );
97 if (ret) TRACE( "hwnd %p %s\n", hwnd, wine_dbgstr_rect(rect) );
98 return ret;
102 /***********************************************************************
103 * GetWindowRgn (USER32.@)
105 int WINAPI GetWindowRgn ( HWND hwnd, HRGN hrgn )
107 int nRet = ERROR;
108 NTSTATUS status;
109 HRGN win_rgn = 0;
110 RGNDATA *data;
111 size_t size = 256;
115 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
117 SetLastError( ERROR_OUTOFMEMORY );
118 return ERROR;
120 SERVER_START_REQ( get_window_region )
122 req->window = wine_server_user_handle( hwnd );
123 wine_server_set_reply( req, data->Buffer, size );
124 if (!(status = wine_server_call( req )))
126 size_t reply_size = wine_server_reply_size( reply );
127 if (reply_size)
129 data->rdh.dwSize = sizeof(data->rdh);
130 data->rdh.iType = RDH_RECTANGLES;
131 data->rdh.nCount = reply_size / sizeof(RECT);
132 data->rdh.nRgnSize = reply_size;
133 win_rgn = ExtCreateRegion( NULL, size, data );
136 else size = reply->total_size;
138 SERVER_END_REQ;
139 HeapFree( GetProcessHeap(), 0, data );
140 } while (status == STATUS_BUFFER_OVERFLOW);
142 if (status) SetLastError( RtlNtStatusToDosError(status) );
143 else if (win_rgn)
145 nRet = CombineRgn( hrgn, win_rgn, 0, RGN_COPY );
146 DeleteObject( win_rgn );
148 return nRet;
151 /***********************************************************************
152 * GetWindowRgnBox (USER32.@)
154 int WINAPI GetWindowRgnBox( HWND hwnd, LPRECT prect )
156 int ret = ERROR;
157 HRGN hrgn;
159 if (!prect)
160 return ERROR;
162 if ((hrgn = CreateRectRgn(0, 0, 0, 0)))
164 if ((ret = GetWindowRgn( hwnd, hrgn )) != ERROR )
165 ret = GetRgnBox( hrgn, prect );
166 DeleteObject(hrgn);
169 return ret;
172 /***********************************************************************
173 * SetWindowRgn (USER32.@)
175 int WINAPI SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL bRedraw )
177 static const RECT empty_rect;
178 BOOL ret;
180 if (hrgn)
182 RGNDATA *data;
183 DWORD size;
185 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
186 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
187 if (!GetRegionData( hrgn, size, data ))
189 HeapFree( GetProcessHeap(), 0, data );
190 return FALSE;
192 SERVER_START_REQ( set_window_region )
194 req->window = wine_server_user_handle( hwnd );
195 req->redraw = (bRedraw != 0);
196 if (data->rdh.nCount)
197 wine_server_add_data( req, data->Buffer, data->rdh.nCount * sizeof(RECT) );
198 else
199 wine_server_add_data( req, &empty_rect, sizeof(empty_rect) );
200 ret = !wine_server_call_err( req );
202 SERVER_END_REQ;
203 HeapFree( GetProcessHeap(), 0, data );
205 else /* clear existing region */
207 SERVER_START_REQ( set_window_region )
209 req->window = wine_server_user_handle( hwnd );
210 req->redraw = (bRedraw != 0);
211 ret = !wine_server_call_err( req );
213 SERVER_END_REQ;
216 if (ret) ret = USER_Driver->pSetWindowRgn( hwnd, hrgn, bRedraw );
218 if (ret)
220 UINT swp_flags = SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE;
221 if (!bRedraw) swp_flags |= SWP_NOREDRAW;
222 SetWindowPos( hwnd, 0, 0, 0, 0, 0, swp_flags );
223 invalidate_dce( hwnd, NULL );
224 if (hrgn) DeleteObject( hrgn );
226 return ret;
230 /***********************************************************************
231 * GetClientRect (USER32.@)
233 BOOL WINAPI GetClientRect( HWND hwnd, LPRECT rect )
235 return WIN_GetRectangles( hwnd, COORDS_CLIENT, NULL, rect );
239 /*******************************************************************
240 * ClientToScreen (USER32.@)
242 BOOL WINAPI ClientToScreen( HWND hwnd, LPPOINT lppnt )
244 MapWindowPoints( hwnd, 0, lppnt, 1 );
245 return TRUE;
249 /*******************************************************************
250 * ScreenToClient (USER32.@)
252 BOOL WINAPI ScreenToClient( HWND hwnd, LPPOINT lppnt )
254 MapWindowPoints( 0, hwnd, lppnt, 1 );
255 return TRUE;
259 /***********************************************************************
260 * list_children_from_point
262 * Get the list of children that can contain point from the server.
263 * Point is in screen coordinates.
264 * Returned list must be freed by caller.
266 static HWND *list_children_from_point( HWND hwnd, POINT pt )
268 HWND *list;
269 int i, size = 128;
271 for (;;)
273 int count = 0;
275 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
277 SERVER_START_REQ( get_window_children_from_point )
279 req->parent = wine_server_user_handle( hwnd );
280 req->x = pt.x;
281 req->y = pt.y;
282 wine_server_set_reply( req, list, (size-1) * sizeof(user_handle_t) );
283 if (!wine_server_call( req )) count = reply->count;
285 SERVER_END_REQ;
286 if (count && count < size)
288 /* start from the end since HWND is potentially larger than user_handle_t */
289 for (i = count - 1; i >= 0; i--)
290 list[i] = wine_server_ptr_handle( ((user_handle_t *)list)[i] );
291 list[count] = 0;
292 return list;
294 HeapFree( GetProcessHeap(), 0, list );
295 if (!count) break;
296 size = count + 1; /* restart with a large enough buffer */
298 return NULL;
302 /***********************************************************************
303 * WINPOS_WindowFromPoint
305 * Find the window and hittest for a given point.
307 HWND WINPOS_WindowFromPoint( HWND hwndScope, POINT pt, INT *hittest )
309 int i, res;
310 HWND ret, *list;
312 if (!hwndScope) hwndScope = GetDesktopWindow();
314 *hittest = HTNOWHERE;
316 if (!(list = list_children_from_point( hwndScope, pt ))) return 0;
318 /* now determine the hittest */
320 for (i = 0; list[i]; i++)
322 LONG style = GetWindowLongW( list[i], GWL_STYLE );
324 /* If window is minimized or disabled, return at once */
325 if (style & WS_MINIMIZE)
327 *hittest = HTCAPTION;
328 break;
330 if (style & WS_DISABLED)
332 *hittest = HTERROR;
333 break;
335 /* Send WM_NCCHITTEST (if same thread) */
336 if (!WIN_IsCurrentThread( list[i] ))
338 *hittest = HTCLIENT;
339 break;
341 res = SendMessageW( list[i], WM_NCHITTEST, 0, MAKELONG(pt.x,pt.y) );
342 if (res != HTTRANSPARENT)
344 *hittest = res; /* Found the window */
345 break;
347 /* continue search with next window in z-order */
349 ret = list[i];
350 HeapFree( GetProcessHeap(), 0, list );
351 TRACE( "scope %p (%d,%d) returning %p\n", hwndScope, pt.x, pt.y, ret );
352 return ret;
356 /*******************************************************************
357 * WindowFromPoint (USER32.@)
359 HWND WINAPI WindowFromPoint( POINT pt )
361 INT hittest;
362 return WINPOS_WindowFromPoint( 0, pt, &hittest );
366 /*******************************************************************
367 * ChildWindowFromPoint (USER32.@)
369 HWND WINAPI ChildWindowFromPoint( HWND hwndParent, POINT pt )
371 return ChildWindowFromPointEx( hwndParent, pt, CWP_ALL );
374 /*******************************************************************
375 * RealChildWindowFromPoint (USER32.@)
377 HWND WINAPI RealChildWindowFromPoint( HWND hwndParent, POINT pt )
379 return ChildWindowFromPointEx( hwndParent, pt, CWP_SKIPTRANSPARENT );
382 /*******************************************************************
383 * ChildWindowFromPointEx (USER32.@)
385 HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
387 /* pt is in the client coordinates */
388 HWND *list;
389 int i;
390 RECT rect;
391 HWND retvalue;
393 GetClientRect( hwndParent, &rect );
394 if (!PtInRect( &rect, pt )) return 0;
395 if (!(list = WIN_ListChildren( hwndParent ))) return hwndParent;
397 for (i = 0; list[i]; i++)
399 if (!WIN_GetRectangles( list[i], COORDS_PARENT, &rect, NULL )) continue;
400 if (!PtInRect( &rect, pt )) continue;
401 if (uFlags & (CWP_SKIPINVISIBLE|CWP_SKIPDISABLED))
403 LONG style = GetWindowLongW( list[i], GWL_STYLE );
404 if ((uFlags & CWP_SKIPINVISIBLE) && !(style & WS_VISIBLE)) continue;
405 if ((uFlags & CWP_SKIPDISABLED) && (style & WS_DISABLED)) continue;
407 if (uFlags & CWP_SKIPTRANSPARENT)
409 if (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TRANSPARENT) continue;
411 break;
413 retvalue = list[i];
414 HeapFree( GetProcessHeap(), 0, list );
415 if (!retvalue) retvalue = hwndParent;
416 return retvalue;
420 /*******************************************************************
421 * WINPOS_GetWinOffset
423 * Calculate the offset between the origin of the two windows. Used
424 * to implement MapWindowPoints.
426 static POINT WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo, BOOL *mirrored )
428 WND * wndPtr;
429 POINT offset;
430 BOOL mirror_from, mirror_to;
431 HWND hwnd;
433 offset.x = offset.y = 0;
434 *mirrored = mirror_from = mirror_to = FALSE;
436 /* Translate source window origin to screen coords */
437 if (hwndFrom)
439 if (!(wndPtr = WIN_GetPtr( hwndFrom ))) return offset;
440 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
441 if (wndPtr != WND_DESKTOP)
443 if (wndPtr->dwExStyle & WS_EX_LAYOUTRTL)
445 mirror_from = TRUE;
446 offset.x += wndPtr->rectClient.right - wndPtr->rectClient.left;
448 for (;;)
450 offset.x += wndPtr->rectClient.left;
451 offset.y += wndPtr->rectClient.top;
452 hwnd = wndPtr->parent;
453 WIN_ReleasePtr( wndPtr );
454 if (!(wndPtr = WIN_GetPtr( hwnd ))) break;
455 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
456 if (wndPtr == WND_DESKTOP) break;
457 if (wndPtr->flags & WIN_CHILDREN_MOVED)
459 WIN_ReleasePtr( wndPtr );
460 goto other_process;
466 /* Translate origin to destination window coords */
467 if (hwndTo)
469 if (!(wndPtr = WIN_GetPtr( hwndTo ))) return offset;
470 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
471 if (wndPtr != WND_DESKTOP)
473 if (wndPtr->dwExStyle & WS_EX_LAYOUTRTL)
475 mirror_to = TRUE;
476 offset.x -= wndPtr->rectClient.right - wndPtr->rectClient.left;
478 for (;;)
480 offset.x -= wndPtr->rectClient.left;
481 offset.y -= wndPtr->rectClient.top;
482 hwnd = wndPtr->parent;
483 WIN_ReleasePtr( wndPtr );
484 if (!(wndPtr = WIN_GetPtr( hwnd ))) break;
485 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
486 if (wndPtr == WND_DESKTOP) break;
487 if (wndPtr->flags & WIN_CHILDREN_MOVED)
489 WIN_ReleasePtr( wndPtr );
490 goto other_process;
496 *mirrored = mirror_from ^ mirror_to;
497 if (mirror_from) offset.x = -offset.x;
498 return offset;
500 other_process: /* one of the parents may belong to another process, do it the hard way */
501 offset.x = offset.y = 0;
502 SERVER_START_REQ( get_windows_offset )
504 req->from = wine_server_user_handle( hwndFrom );
505 req->to = wine_server_user_handle( hwndTo );
506 if (!wine_server_call( req ))
508 offset.x = reply->x;
509 offset.y = reply->y;
510 *mirrored = reply->mirror;
513 SERVER_END_REQ;
514 return offset;
517 /* map coordinates of a window region */
518 void map_window_region( HWND from, HWND to, HRGN hrgn )
520 BOOL mirrored;
521 POINT offset = WINPOS_GetWinOffset( from, to, &mirrored );
522 UINT i, size;
523 RGNDATA *data;
524 HRGN new_rgn;
525 RECT *rect;
527 if (!mirrored)
529 OffsetRgn( hrgn, offset.x, offset.y );
530 return;
532 if (!(size = GetRegionData( hrgn, 0, NULL ))) return;
533 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return;
534 GetRegionData( hrgn, size, data );
535 rect = (RECT *)data->Buffer;
536 for (i = 0; i < data->rdh.nCount; i++)
538 int tmp = -(rect[i].left + offset.x);
539 rect[i].left = -(rect[i].right + offset.x);
540 rect[i].right = tmp;
541 rect[i].top += offset.y;
542 rect[i].bottom += offset.y;
544 if ((new_rgn = ExtCreateRegion( NULL, data->rdh.nCount, data )))
546 CombineRgn( hrgn, new_rgn, 0, RGN_COPY );
547 DeleteObject( new_rgn );
549 HeapFree( GetProcessHeap(), 0, data );
553 /*******************************************************************
554 * MapWindowPoints (USER32.@)
556 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
558 BOOL mirrored;
559 POINT offset = WINPOS_GetWinOffset( hwndFrom, hwndTo, &mirrored );
560 UINT i;
562 for (i = 0; i < count; i++)
564 lppt[i].x += offset.x;
565 lppt[i].y += offset.y;
566 if (mirrored) lppt[i].x = -lppt[i].x;
568 if (mirrored && count == 2) /* special case for rectangle */
570 int tmp = lppt[0].x;
571 lppt[0].x = lppt[1].x;
572 lppt[1].x = tmp;
574 return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
578 /***********************************************************************
579 * IsIconic (USER32.@)
581 BOOL WINAPI IsIconic(HWND hWnd)
583 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
587 /***********************************************************************
588 * IsZoomed (USER32.@)
590 BOOL WINAPI IsZoomed(HWND hWnd)
592 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
596 /*******************************************************************
597 * AllowSetForegroundWindow (USER32.@)
599 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
601 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
602 * implemented, then fix this function. */
603 return TRUE;
607 /*******************************************************************
608 * LockSetForegroundWindow (USER32.@)
610 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
612 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
613 * implemented, then fix this function. */
614 return TRUE;
618 /***********************************************************************
619 * BringWindowToTop (USER32.@)
621 BOOL WINAPI BringWindowToTop( HWND hwnd )
623 return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
627 /***********************************************************************
628 * MoveWindow (USER32.@)
630 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
631 BOOL repaint )
633 int flags = SWP_NOZORDER | SWP_NOACTIVATE;
634 if (!repaint) flags |= SWP_NOREDRAW;
635 TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
636 return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
640 /***********************************************************************
641 * WINPOS_RedrawIconTitle
643 BOOL WINPOS_RedrawIconTitle( HWND hWnd )
645 HWND icon_title = 0;
646 WND *win = WIN_GetPtr( hWnd );
648 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
650 icon_title = win->icon_title;
651 WIN_ReleasePtr( win );
653 if (!icon_title) return FALSE;
654 SendMessageW( icon_title, WM_SHOWWINDOW, TRUE, 0 );
655 InvalidateRect( icon_title, NULL, TRUE );
656 return TRUE;
659 /***********************************************************************
660 * WINPOS_ShowIconTitle
662 static BOOL WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
664 if (!GetPropA( hwnd, "__wine_x11_managed" ))
666 WND *win = WIN_GetPtr( hwnd );
667 HWND title = 0;
669 TRACE("%p %i\n", hwnd, (bShow != 0) );
671 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP) return FALSE;
672 title = win->icon_title;
673 WIN_ReleasePtr( win );
675 if( bShow )
677 if (!title)
679 title = ICONTITLE_Create( hwnd );
680 if (!(win = WIN_GetPtr( hwnd )) || win == WND_OTHER_PROCESS)
682 DestroyWindow( title );
683 return FALSE;
685 win->icon_title = title;
686 WIN_ReleasePtr( win );
688 if (!IsWindowVisible(title))
690 SendMessageW( title, WM_SHOWWINDOW, TRUE, 0 );
691 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
692 SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
695 else if (title) ShowWindow( title, SW_HIDE );
697 return FALSE;
700 /*******************************************************************
701 * WINPOS_GetMinMaxInfo
703 * Get the minimized and maximized information for a window.
705 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
706 POINT *minTrack, POINT *maxTrack )
708 MINMAXINFO MinMax;
709 HMONITOR monitor;
710 INT xinc, yinc;
711 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
712 LONG adjustedStyle;
713 LONG exstyle = GetWindowLongW( hwnd, GWL_EXSTYLE );
714 RECT rc;
715 WND *win;
717 /* Compute default values */
719 GetWindowRect(hwnd, &rc);
720 MinMax.ptReserved.x = rc.left;
721 MinMax.ptReserved.y = rc.top;
723 if ((style & WS_CAPTION) == WS_CAPTION)
724 adjustedStyle = style & ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
725 else
726 adjustedStyle = style;
728 GetClientRect(GetAncestor(hwnd,GA_PARENT), &rc);
729 AdjustWindowRectEx(&rc, adjustedStyle, ((style & WS_POPUP) && GetMenu(hwnd)), exstyle);
731 xinc = -rc.left;
732 yinc = -rc.top;
734 MinMax.ptMaxSize.x = rc.right - rc.left;
735 MinMax.ptMaxSize.y = rc.bottom - rc.top;
736 if (style & (WS_DLGFRAME | WS_BORDER))
738 MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
739 MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
741 else
743 MinMax.ptMinTrackSize.x = 2 * xinc;
744 MinMax.ptMinTrackSize.y = 2 * yinc;
746 MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXMAXTRACK);
747 MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYMAXTRACK);
748 MinMax.ptMaxPosition.x = -xinc;
749 MinMax.ptMaxPosition.y = -yinc;
751 if ((win = WIN_GetPtr( hwnd )) && win != WND_DESKTOP && win != WND_OTHER_PROCESS)
753 if (!EMPTYPOINT(win->max_pos)) MinMax.ptMaxPosition = win->max_pos;
754 WIN_ReleasePtr( win );
757 SendMessageW( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
759 /* if the app didn't change the values, adapt them for the current monitor */
761 if ((monitor = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY )))
763 RECT rc_work;
764 MONITORINFO mon_info;
766 mon_info.cbSize = sizeof(mon_info);
767 GetMonitorInfoW( monitor, &mon_info );
769 rc_work = mon_info.rcMonitor;
771 if (style & WS_MAXIMIZEBOX)
773 if ((style & WS_CAPTION) == WS_CAPTION || !(style & (WS_CHILD | WS_POPUP)))
774 rc_work = mon_info.rcWork;
777 if (MinMax.ptMaxSize.x == GetSystemMetrics(SM_CXSCREEN) + 2 * xinc &&
778 MinMax.ptMaxSize.y == GetSystemMetrics(SM_CYSCREEN) + 2 * yinc)
780 MinMax.ptMaxSize.x = (rc_work.right - rc_work.left) + 2 * xinc;
781 MinMax.ptMaxSize.y = (rc_work.bottom - rc_work.top) + 2 * yinc;
783 if (MinMax.ptMaxPosition.x == -xinc && MinMax.ptMaxPosition.y == -yinc)
785 MinMax.ptMaxPosition.x = rc_work.left - xinc;
786 MinMax.ptMaxPosition.y = rc_work.top - yinc;
790 /* Some sanity checks */
792 TRACE("%d %d / %d %d / %d %d / %d %d\n",
793 MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
794 MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
795 MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
796 MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
797 MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
798 MinMax.ptMinTrackSize.x );
799 MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
800 MinMax.ptMinTrackSize.y );
802 if (maxSize) *maxSize = MinMax.ptMaxSize;
803 if (maxPos) *maxPos = MinMax.ptMaxPosition;
804 if (minTrack) *minTrack = MinMax.ptMinTrackSize;
805 if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
809 /***********************************************************************
810 * WINPOS_FindIconPos
812 * Find a suitable place for an iconic window.
814 static POINT WINPOS_FindIconPos( HWND hwnd, POINT pt )
816 RECT rect, rectParent;
817 HWND parent, child;
818 HRGN hrgn, tmp;
819 int xspacing, yspacing;
821 parent = GetAncestor( hwnd, GA_PARENT );
822 GetClientRect( parent, &rectParent );
823 if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
824 (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
825 return pt; /* The icon already has a suitable position */
827 xspacing = GetSystemMetrics(SM_CXICONSPACING);
828 yspacing = GetSystemMetrics(SM_CYICONSPACING);
830 /* Check if another icon already occupies this spot */
831 /* FIXME: this is completely inefficient */
833 hrgn = CreateRectRgn( 0, 0, 0, 0 );
834 tmp = CreateRectRgn( 0, 0, 0, 0 );
835 for (child = GetWindow( parent, GW_HWNDFIRST ); child; child = GetWindow( child, GW_HWNDNEXT ))
837 if (child == hwnd) continue;
838 if ((GetWindowLongW( child, GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != (WS_VISIBLE|WS_MINIMIZE))
839 continue;
840 if (WIN_GetRectangles( child, COORDS_PARENT, &rect, NULL ))
842 SetRectRgn( tmp, rect.left, rect.top, rect.right, rect.bottom );
843 CombineRgn( hrgn, hrgn, tmp, RGN_OR );
846 DeleteObject( tmp );
848 for (rect.bottom = rectParent.bottom; rect.bottom >= yspacing; rect.bottom -= yspacing)
850 for (rect.left = rectParent.left; rect.left <= rectParent.right - xspacing; rect.left += xspacing)
852 rect.right = rect.left + xspacing;
853 rect.top = rect.bottom - yspacing;
854 if (!RectInRegion( hrgn, &rect ))
856 /* No window was found, so it's OK for us */
857 pt.x = rect.left + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
858 pt.y = rect.top + (yspacing - GetSystemMetrics(SM_CYICON)) / 2;
859 DeleteObject( hrgn );
860 return pt;
864 DeleteObject( hrgn );
865 pt.x = pt.y = 0;
866 return pt;
870 /***********************************************************************
871 * WINPOS_MinMaximize
873 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
875 WND *wndPtr;
876 UINT swpFlags = 0;
877 POINT size;
878 LONG old_style;
879 WINDOWPLACEMENT wpl;
881 TRACE("%p %u\n", hwnd, cmd );
883 wpl.length = sizeof(wpl);
884 GetWindowPlacement( hwnd, &wpl );
886 if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
887 return SWP_NOSIZE | SWP_NOMOVE;
889 if (IsIconic( hwnd ))
891 switch (cmd)
893 case SW_SHOWMINNOACTIVE:
894 case SW_SHOWMINIMIZED:
895 case SW_FORCEMINIMIZE:
896 case SW_MINIMIZE:
897 return SWP_NOSIZE | SWP_NOMOVE;
899 if (!SendMessageW( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
900 swpFlags |= SWP_NOCOPYBITS;
903 switch( cmd )
905 case SW_SHOWMINNOACTIVE:
906 case SW_SHOWMINIMIZED:
907 case SW_FORCEMINIMIZE:
908 case SW_MINIMIZE:
909 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
910 if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
911 else wndPtr->flags &= ~WIN_RESTORE_MAX;
912 WIN_ReleasePtr( wndPtr );
914 old_style = WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
916 wpl.ptMinPosition = WINPOS_FindIconPos( hwnd, wpl.ptMinPosition );
918 if (!(old_style & WS_MINIMIZE)) swpFlags |= SWP_STATECHANGED;
919 SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
920 wpl.ptMinPosition.x + GetSystemMetrics(SM_CXICON),
921 wpl.ptMinPosition.y + GetSystemMetrics(SM_CYICON) );
922 swpFlags |= SWP_NOCOPYBITS;
923 break;
925 case SW_MAXIMIZE:
926 old_style = GetWindowLongW( hwnd, GWL_STYLE );
927 if ((old_style & WS_MAXIMIZE) && (old_style & WS_VISIBLE)) return SWP_NOSIZE | SWP_NOMOVE;
929 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
931 old_style = WIN_SetStyle( hwnd, WS_MAXIMIZE, WS_MINIMIZE );
932 if (old_style & WS_MINIMIZE)
934 if ((wndPtr = WIN_GetPtr( hwnd )) && wndPtr != WND_OTHER_PROCESS)
936 wndPtr->flags |= WIN_RESTORE_MAX;
937 WIN_ReleasePtr( wndPtr );
939 WINPOS_ShowIconTitle( hwnd, FALSE );
942 if (!(old_style & WS_MAXIMIZE)) swpFlags |= SWP_STATECHANGED;
943 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y,
944 wpl.ptMaxPosition.x + size.x, wpl.ptMaxPosition.y + size.y );
945 break;
947 case SW_SHOWNOACTIVATE:
948 if ((wndPtr = WIN_GetPtr( hwnd )) && wndPtr != WND_OTHER_PROCESS)
950 wndPtr->flags &= ~WIN_RESTORE_MAX;
951 WIN_ReleasePtr( wndPtr );
953 /* fall through */
954 case SW_SHOWNORMAL:
955 case SW_RESTORE:
956 old_style = WIN_SetStyle( hwnd, 0, WS_MINIMIZE | WS_MAXIMIZE );
957 if (old_style & WS_MINIMIZE)
959 BOOL restore_max;
961 WINPOS_ShowIconTitle( hwnd, FALSE );
963 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
964 restore_max = (wndPtr->flags & WIN_RESTORE_MAX) != 0;
965 WIN_ReleasePtr( wndPtr );
966 if (restore_max)
968 /* Restore to maximized position */
969 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
970 WIN_SetStyle( hwnd, WS_MAXIMIZE, 0 );
971 swpFlags |= SWP_STATECHANGED;
972 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y,
973 wpl.ptMaxPosition.x + size.x, wpl.ptMaxPosition.y + size.y );
974 break;
977 else if (!(old_style & WS_MAXIMIZE)) break;
979 swpFlags |= SWP_STATECHANGED;
981 /* Restore to normal position */
983 *rect = wpl.rcNormalPosition;
984 break;
987 return swpFlags;
991 /***********************************************************************
992 * show_window
994 * Implementation of ShowWindow and ShowWindowAsync.
996 static BOOL show_window( HWND hwnd, INT cmd )
998 WND *wndPtr;
999 HWND parent;
1000 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1001 BOOL wasVisible = (style & WS_VISIBLE) != 0;
1002 BOOL showFlag = TRUE;
1003 RECT newPos = {0, 0, 0, 0};
1004 UINT swp = 0;
1006 TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);
1008 switch(cmd)
1010 case SW_HIDE:
1011 if (!wasVisible) return FALSE;
1012 showFlag = FALSE;
1013 swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1014 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1015 break;
1017 case SW_SHOWMINNOACTIVE:
1018 case SW_MINIMIZE:
1019 case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
1020 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1021 /* fall through */
1022 case SW_SHOWMINIMIZED:
1023 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1024 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
1025 if ((style & WS_MINIMIZE) && wasVisible) return TRUE;
1026 break;
1028 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
1029 if (!wasVisible) swp |= SWP_SHOWWINDOW;
1030 swp |= SWP_FRAMECHANGED;
1031 swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
1032 if ((style & WS_MAXIMIZE) && wasVisible) return TRUE;
1033 break;
1035 case SW_SHOWNA:
1036 swp |= SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1037 if (style & WS_CHILD) swp |= SWP_NOZORDER;
1038 break;
1039 case SW_SHOW:
1040 if (wasVisible) return TRUE;
1041 swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1042 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1043 break;
1045 case SW_SHOWNOACTIVATE:
1046 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1047 /* fall through */
1048 case SW_RESTORE:
1049 /* fall through */
1050 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
1051 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
1052 if (!wasVisible) swp |= SWP_SHOWWINDOW;
1053 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1055 swp |= SWP_FRAMECHANGED;
1056 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
1058 else
1060 if (wasVisible) return TRUE;
1061 swp |= SWP_NOSIZE | SWP_NOMOVE;
1063 if (style & WS_CHILD && !(swp & SWP_STATECHANGED)) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1064 break;
1065 default:
1066 return wasVisible;
1069 if ((showFlag != wasVisible || cmd == SW_SHOWNA) && cmd != SW_SHOWMAXIMIZED && !(swp & SWP_STATECHANGED))
1071 SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
1072 if (!IsWindow( hwnd )) return wasVisible;
1075 swp = USER_Driver->pShowWindow( hwnd, cmd, &newPos, swp );
1077 parent = GetAncestor( hwnd, GA_PARENT );
1078 if (parent && !IsWindowVisible( parent ) && !(swp & SWP_STATECHANGED))
1080 /* if parent is not visible simply toggle WS_VISIBLE and return */
1081 if (showFlag) WIN_SetStyle( hwnd, WS_VISIBLE, 0 );
1082 else WIN_SetStyle( hwnd, 0, WS_VISIBLE );
1084 else
1085 SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1086 newPos.right - newPos.left, newPos.bottom - newPos.top, swp );
1088 if (cmd == SW_HIDE)
1090 HWND hFocus;
1092 WINPOS_ShowIconTitle( hwnd, FALSE );
1094 /* FIXME: This will cause the window to be activated irrespective
1095 * of whether it is owned by the same thread. Has to be done
1096 * asynchronously.
1099 if (hwnd == GetActiveWindow())
1100 WINPOS_ActivateOtherWindow(hwnd);
1102 /* Revert focus to parent */
1103 hFocus = GetFocus();
1104 if (hwnd == hFocus)
1106 HWND parent = GetAncestor(hwnd, GA_PARENT);
1107 if (parent == GetDesktopWindow()) parent = 0;
1108 SetFocus(parent);
1110 return wasVisible;
1113 if (IsIconic(hwnd)) WINPOS_ShowIconTitle( hwnd, TRUE );
1115 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return wasVisible;
1117 if (wndPtr->flags & WIN_NEED_SIZE)
1119 /* should happen only in CreateWindowEx() */
1120 int wParam = SIZE_RESTORED;
1121 RECT client;
1122 LPARAM lparam;
1124 WIN_GetRectangles( hwnd, COORDS_PARENT, NULL, &client );
1125 lparam = MAKELONG( client.right - client.left, client.bottom - client.top );
1126 wndPtr->flags &= ~WIN_NEED_SIZE;
1127 if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1128 else if (wndPtr->dwStyle & WS_MINIMIZE)
1130 wParam = SIZE_MINIMIZED;
1131 lparam = 0;
1133 WIN_ReleasePtr( wndPtr );
1135 SendMessageW( hwnd, WM_SIZE, wParam, lparam );
1136 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( client.left, client.top ));
1138 else WIN_ReleasePtr( wndPtr );
1140 /* if previous state was minimized Windows sets focus to the window */
1141 if (style & WS_MINIMIZE) SetFocus( hwnd );
1143 return wasVisible;
1147 /***********************************************************************
1148 * ShowWindowAsync (USER32.@)
1150 * doesn't wait; returns immediately.
1151 * used by threads to toggle windows in other (possibly hanging) threads
1153 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
1155 HWND full_handle;
1157 if (is_broadcast(hwnd))
1159 SetLastError( ERROR_INVALID_PARAMETER );
1160 return FALSE;
1163 if ((full_handle = WIN_IsCurrentThread( hwnd )))
1164 return show_window( full_handle, cmd );
1166 return SendNotifyMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
1170 /***********************************************************************
1171 * ShowWindow (USER32.@)
1173 BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
1175 HWND full_handle;
1177 if (is_broadcast(hwnd))
1179 SetLastError( ERROR_INVALID_PARAMETER );
1180 return FALSE;
1182 if ((full_handle = WIN_IsCurrentThread( hwnd )))
1183 return show_window( full_handle, cmd );
1185 return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
1189 /***********************************************************************
1190 * GetInternalWindowPos (USER32.@)
1192 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
1193 LPPOINT ptIcon )
1195 WINDOWPLACEMENT wndpl;
1196 if (GetWindowPlacement( hwnd, &wndpl ))
1198 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
1199 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
1200 return wndpl.showCmd;
1202 return 0;
1206 /***********************************************************************
1207 * GetWindowPlacement (USER32.@)
1209 * Win95:
1210 * Fails if wndpl->length of Win95 (!) apps is invalid.
1212 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
1214 WND *pWnd = WIN_GetPtr( hwnd );
1216 if (!pWnd) return FALSE;
1218 if (pWnd == WND_DESKTOP)
1220 wndpl->length = sizeof(*wndpl);
1221 wndpl->showCmd = SW_SHOWNORMAL;
1222 wndpl->flags = 0;
1223 wndpl->ptMinPosition.x = -1;
1224 wndpl->ptMinPosition.y = -1;
1225 wndpl->ptMaxPosition.x = -1;
1226 wndpl->ptMaxPosition.y = -1;
1227 GetWindowRect( hwnd, &wndpl->rcNormalPosition );
1228 return TRUE;
1230 if (pWnd == WND_OTHER_PROCESS)
1232 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
1233 return FALSE;
1236 /* update the placement according to the current style */
1237 if (pWnd->dwStyle & WS_MINIMIZE)
1239 pWnd->min_pos.x = pWnd->rectWindow.left;
1240 pWnd->min_pos.y = pWnd->rectWindow.top;
1242 else if (pWnd->dwStyle & WS_MAXIMIZE)
1244 pWnd->max_pos.x = pWnd->rectWindow.left;
1245 pWnd->max_pos.y = pWnd->rectWindow.top;
1247 else
1249 pWnd->normal_rect = pWnd->rectWindow;
1252 wndpl->length = sizeof(*wndpl);
1253 if( pWnd->dwStyle & WS_MINIMIZE )
1254 wndpl->showCmd = SW_SHOWMINIMIZED;
1255 else
1256 wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
1257 if( pWnd->flags & WIN_RESTORE_MAX )
1258 wndpl->flags = WPF_RESTORETOMAXIMIZED;
1259 else
1260 wndpl->flags = 0;
1261 wndpl->ptMinPosition = pWnd->min_pos;
1262 wndpl->ptMaxPosition = pWnd->max_pos;
1263 wndpl->rcNormalPosition = pWnd->normal_rect;
1264 WIN_ReleasePtr( pWnd );
1266 TRACE( "%p: returning min %d,%d max %d,%d normal %s\n",
1267 hwnd, wndpl->ptMinPosition.x, wndpl->ptMinPosition.y,
1268 wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y,
1269 wine_dbgstr_rect(&wndpl->rcNormalPosition) );
1270 return TRUE;
1273 /* make sure the specified rect is visible on screen */
1274 static void make_rect_onscreen( RECT *rect )
1276 MONITORINFO info;
1277 HMONITOR monitor = MonitorFromRect( rect, MONITOR_DEFAULTTONEAREST );
1279 info.cbSize = sizeof(info);
1280 if (!monitor || !GetMonitorInfoW( monitor, &info )) return;
1281 /* FIXME: map coordinates from rcWork to rcMonitor */
1282 if (rect->right <= info.rcWork.left)
1284 rect->right += info.rcWork.left - rect->left;
1285 rect->left = info.rcWork.left;
1287 else if (rect->left >= info.rcWork.right)
1289 rect->left += info.rcWork.right - rect->right;
1290 rect->right = info.rcWork.right;
1292 if (rect->bottom <= info.rcWork.top)
1294 rect->bottom += info.rcWork.top - rect->top;
1295 rect->top = info.rcWork.top;
1297 else if (rect->top >= info.rcWork.bottom)
1299 rect->top += info.rcWork.bottom - rect->bottom;
1300 rect->bottom = info.rcWork.bottom;
1304 /* make sure the specified point is visible on screen */
1305 static void make_point_onscreen( POINT *pt )
1307 RECT rect;
1309 SetRect( &rect, pt->x, pt->y, pt->x + 1, pt->y + 1 );
1310 make_rect_onscreen( &rect );
1311 pt->x = rect.left;
1312 pt->y = rect.top;
1316 /***********************************************************************
1317 * WINPOS_SetPlacement
1319 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
1321 DWORD style;
1322 WND *pWnd = WIN_GetPtr( hwnd );
1323 WINDOWPLACEMENT wp = *wndpl;
1325 if (flags & PLACE_MIN) make_point_onscreen( &wp.ptMinPosition );
1326 if (flags & PLACE_MAX) make_point_onscreen( &wp.ptMaxPosition );
1327 if (flags & PLACE_RECT) make_rect_onscreen( &wp.rcNormalPosition );
1329 TRACE( "%p: setting min %d,%d max %d,%d normal %s flags %x ajusted to min %d,%d max %d,%d normal %s\n",
1330 hwnd, wndpl->ptMinPosition.x, wndpl->ptMinPosition.y,
1331 wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y,
1332 wine_dbgstr_rect(&wndpl->rcNormalPosition), flags,
1333 wp.ptMinPosition.x, wp.ptMinPosition.y, wp.ptMaxPosition.x, wp.ptMaxPosition.y,
1334 wine_dbgstr_rect(&wp.rcNormalPosition) );
1336 if (!pWnd || pWnd == WND_OTHER_PROCESS || pWnd == WND_DESKTOP) return FALSE;
1338 if( flags & PLACE_MIN ) pWnd->min_pos = wp.ptMinPosition;
1339 if( flags & PLACE_MAX ) pWnd->max_pos = wp.ptMaxPosition;
1340 if( flags & PLACE_RECT) pWnd->normal_rect = wp.rcNormalPosition;
1342 style = pWnd->dwStyle;
1344 WIN_ReleasePtr( pWnd );
1346 if( style & WS_MINIMIZE )
1348 if (flags & PLACE_MIN)
1350 WINPOS_ShowIconTitle( hwnd, FALSE );
1351 SetWindowPos( hwnd, 0, wp.ptMinPosition.x, wp.ptMinPosition.y, 0, 0,
1352 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1355 else if( style & WS_MAXIMIZE )
1357 if (flags & PLACE_MAX)
1358 SetWindowPos( hwnd, 0, wp.ptMaxPosition.x, wp.ptMaxPosition.y, 0, 0,
1359 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1361 else if( flags & PLACE_RECT )
1362 SetWindowPos( hwnd, 0, wp.rcNormalPosition.left, wp.rcNormalPosition.top,
1363 wp.rcNormalPosition.right - wp.rcNormalPosition.left,
1364 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top,
1365 SWP_NOZORDER | SWP_NOACTIVATE );
1367 ShowWindow( hwnd, wndpl->showCmd );
1369 if (IsIconic( hwnd ))
1371 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE) WINPOS_ShowIconTitle( hwnd, TRUE );
1373 /* SDK: ...valid only the next time... */
1374 if( wndpl->flags & WPF_RESTORETOMAXIMIZED )
1376 pWnd = WIN_GetPtr( hwnd );
1377 if (pWnd && pWnd != WND_OTHER_PROCESS)
1379 pWnd->flags |= WIN_RESTORE_MAX;
1380 WIN_ReleasePtr( pWnd );
1384 return TRUE;
1388 /***********************************************************************
1389 * SetWindowPlacement (USER32.@)
1391 * Win95:
1392 * Fails if wndpl->length of Win95 (!) apps is invalid.
1394 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
1396 UINT flags = PLACE_MAX | PLACE_RECT;
1397 if (!wpl) return FALSE;
1398 if (wpl->flags & WPF_SETMINPOSITION) flags |= PLACE_MIN;
1399 return WINPOS_SetPlacement( hwnd, wpl, flags );
1403 /***********************************************************************
1404 * AnimateWindow (USER32.@)
1405 * Shows/Hides a window with an animation
1406 * NO ANIMATION YET
1408 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
1410 FIXME("partial stub\n");
1412 /* If trying to show/hide and it's already *
1413 * shown/hidden or invalid window, fail with *
1414 * invalid parameter */
1415 if(!IsWindow(hwnd) ||
1416 (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
1417 (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
1419 SetLastError(ERROR_INVALID_PARAMETER);
1420 return FALSE;
1423 ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1425 return TRUE;
1428 /***********************************************************************
1429 * SetInternalWindowPos (USER32.@)
1431 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1432 LPRECT rect, LPPOINT pt )
1434 WINDOWPLACEMENT wndpl;
1435 UINT flags;
1437 wndpl.length = sizeof(wndpl);
1438 wndpl.showCmd = showCmd;
1439 wndpl.flags = flags = 0;
1441 if( pt )
1443 flags |= PLACE_MIN;
1444 wndpl.flags |= WPF_SETMINPOSITION;
1445 wndpl.ptMinPosition = *pt;
1447 if( rect )
1449 flags |= PLACE_RECT;
1450 wndpl.rcNormalPosition = *rect;
1452 WINPOS_SetPlacement( hwnd, &wndpl, flags );
1456 /*******************************************************************
1457 * can_activate_window
1459 * Check if we can activate the specified window.
1461 static BOOL can_activate_window( HWND hwnd )
1463 LONG style;
1465 if (!hwnd) return FALSE;
1466 style = GetWindowLongW( hwnd, GWL_STYLE );
1467 if (!(style & WS_VISIBLE)) return FALSE;
1468 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1469 return !(style & WS_DISABLED);
1473 /*******************************************************************
1474 * WINPOS_ActivateOtherWindow
1476 * Activates window other than pWnd.
1478 void WINPOS_ActivateOtherWindow(HWND hwnd)
1480 HWND hwndTo, fg;
1482 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1484 hwndTo = GetAncestor( hwndTo, GA_ROOT );
1485 if (can_activate_window( hwndTo )) goto done;
1488 hwndTo = hwnd;
1489 for (;;)
1491 if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1492 if (can_activate_window( hwndTo )) break;
1495 done:
1496 fg = GetForegroundWindow();
1497 TRACE("win = %p fg = %p\n", hwndTo, fg);
1498 if (!fg || (hwnd == fg))
1500 if (SetForegroundWindow( hwndTo )) return;
1502 if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1506 /***********************************************************************
1507 * WINPOS_HandleWindowPosChanging
1509 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1511 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1513 POINT minTrack, maxTrack;
1514 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1516 if (winpos->flags & SWP_NOSIZE) return 0;
1517 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1519 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1520 if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1521 if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1522 if (!(style & WS_MINIMIZE))
1524 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1525 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1528 return 0;
1532 /***********************************************************************
1533 * dump_winpos_flags
1535 static void dump_winpos_flags(UINT flags)
1537 static const DWORD dumped_flags = (SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOREDRAW |
1538 SWP_NOACTIVATE | SWP_FRAMECHANGED | SWP_SHOWWINDOW |
1539 SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOOWNERZORDER |
1540 SWP_NOSENDCHANGING | SWP_DEFERERASE | SWP_ASYNCWINDOWPOS |
1541 SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_STATECHANGED);
1542 TRACE("flags:");
1543 if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1544 if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1545 if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1546 if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1547 if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1548 if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1549 if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1550 if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1551 if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1552 if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1553 if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1554 if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1555 if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1556 if(flags & SWP_NOCLIENTSIZE) TRACE(" SWP_NOCLIENTSIZE");
1557 if(flags & SWP_NOCLIENTMOVE) TRACE(" SWP_NOCLIENTMOVE");
1558 if(flags & SWP_STATECHANGED) TRACE(" SWP_STATECHANGED");
1560 if(flags & ~dumped_flags) TRACE(" %08x", flags & ~dumped_flags);
1561 TRACE("\n");
1564 /***********************************************************************
1565 * SWP_DoWinPosChanging
1567 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
1569 WND *wndPtr;
1570 RECT window_rect, client_rect;
1572 /* Send WM_WINDOWPOSCHANGING message */
1574 if (!(pWinpos->flags & SWP_NOSENDCHANGING))
1575 SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
1577 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) ||
1578 wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
1580 /* Calculate new position and size */
1582 WIN_GetRectangles( pWinpos->hwnd, COORDS_PARENT, &window_rect, &client_rect );
1583 *pNewWindowRect = window_rect;
1584 *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? window_rect : client_rect;
1586 if (!(pWinpos->flags & SWP_NOSIZE))
1588 if (wndPtr->dwStyle & WS_MINIMIZE)
1590 pNewWindowRect->right = pNewWindowRect->left + GetSystemMetrics(SM_CXICON);
1591 pNewWindowRect->bottom = pNewWindowRect->top + GetSystemMetrics(SM_CYICON);
1593 else
1595 pNewWindowRect->right = pNewWindowRect->left + pWinpos->cx;
1596 pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
1599 if (!(pWinpos->flags & SWP_NOMOVE))
1601 pNewWindowRect->left = pWinpos->x;
1602 pNewWindowRect->top = pWinpos->y;
1603 pNewWindowRect->right += pWinpos->x - window_rect.left;
1604 pNewWindowRect->bottom += pWinpos->y - window_rect.top;
1606 OffsetRect( pNewClientRect, pWinpos->x - window_rect.left,
1607 pWinpos->y - window_rect.top );
1609 pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
1611 TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
1612 pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
1613 pWinpos->cx, pWinpos->cy, pWinpos->flags );
1614 TRACE( "current %s style %08x new %s\n",
1615 wine_dbgstr_rect( &window_rect ), wndPtr->dwStyle,
1616 wine_dbgstr_rect( pNewWindowRect ));
1618 WIN_ReleasePtr( wndPtr );
1619 return TRUE;
1622 /***********************************************************************
1623 * get_valid_rects
1625 * Compute the valid rects from the old and new client rect and WVR_* flags.
1626 * Helper for WM_NCCALCSIZE handling.
1628 static inline void get_valid_rects( const RECT *old_client, const RECT *new_client, UINT flags,
1629 RECT *valid )
1631 int cx, cy;
1633 if (flags & WVR_REDRAW)
1635 SetRectEmpty( &valid[0] );
1636 SetRectEmpty( &valid[1] );
1637 return;
1640 if (flags & WVR_VALIDRECTS)
1642 if (!IntersectRect( &valid[0], &valid[0], new_client ) ||
1643 !IntersectRect( &valid[1], &valid[1], old_client ))
1645 SetRectEmpty( &valid[0] );
1646 SetRectEmpty( &valid[1] );
1647 return;
1649 flags = WVR_ALIGNLEFT | WVR_ALIGNTOP;
1651 else
1653 valid[0] = *new_client;
1654 valid[1] = *old_client;
1657 /* make sure the rectangles have the same size */
1658 cx = min( valid[0].right - valid[0].left, valid[1].right - valid[1].left );
1659 cy = min( valid[0].bottom - valid[0].top, valid[1].bottom - valid[1].top );
1661 if (flags & WVR_ALIGNBOTTOM)
1663 valid[0].top = valid[0].bottom - cy;
1664 valid[1].top = valid[1].bottom - cy;
1666 else
1668 valid[0].bottom = valid[0].top + cy;
1669 valid[1].bottom = valid[1].top + cy;
1671 if (flags & WVR_ALIGNRIGHT)
1673 valid[0].left = valid[0].right - cx;
1674 valid[1].left = valid[1].right - cx;
1676 else
1678 valid[0].right = valid[0].left + cx;
1679 valid[1].right = valid[1].left + cx;
1684 /***********************************************************************
1685 * SWP_DoOwnedPopups
1687 * fix Z order taking into account owned popups -
1688 * basically we need to maintain them above the window that owns them
1690 * FIXME: hide/show owned popups when owner visibility changes.
1692 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
1694 HWND owner, *list = NULL;
1695 unsigned int i;
1697 TRACE("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
1699 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) return hwndInsertAfter;
1701 if ((owner = GetWindow( hwnd, GW_OWNER )))
1703 /* make sure this popup stays above the owner */
1705 if (hwndInsertAfter != HWND_TOPMOST)
1707 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return hwndInsertAfter;
1709 for (i = 0; list[i]; i++)
1711 BOOL topmost = (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST) != 0;
1713 if (list[i] == owner)
1715 if (i > 0) hwndInsertAfter = list[i-1];
1716 else hwndInsertAfter = topmost ? HWND_TOPMOST : HWND_TOP;
1717 break;
1720 if (hwndInsertAfter == HWND_TOP || hwndInsertAfter == HWND_NOTOPMOST)
1722 if (!topmost) break;
1724 else if (list[i] == hwndInsertAfter) break;
1729 if (hwndInsertAfter == HWND_BOTTOM) goto done;
1730 if (!list && !(list = WIN_ListChildren( GetDesktopWindow() ))) goto done;
1732 i = 0;
1733 if (hwndInsertAfter == HWND_TOP || hwndInsertAfter == HWND_NOTOPMOST)
1735 if (hwndInsertAfter == HWND_NOTOPMOST || !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_TOPMOST))
1737 /* skip all the topmost windows */
1738 while (list[i] && (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST)) i++;
1741 else if (hwndInsertAfter != HWND_TOPMOST)
1743 /* skip windows that are already placed correctly */
1744 for (i = 0; list[i]; i++)
1746 if (list[i] == hwndInsertAfter) break;
1747 if (list[i] == hwnd) goto done; /* nothing to do if window is moving backwards in z-order */
1751 for ( ; list[i]; i++)
1753 if (list[i] == hwnd) break;
1754 if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
1755 TRACE( "moving %p owned by %p after %p\n", list[i], hwnd, hwndInsertAfter );
1756 SetWindowPos( list[i], hwndInsertAfter, 0, 0, 0, 0,
1757 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING | SWP_DEFERERASE );
1758 hwndInsertAfter = list[i];
1761 done:
1762 HeapFree( GetProcessHeap(), 0, list );
1763 return hwndInsertAfter;
1766 /***********************************************************************
1767 * SWP_DoNCCalcSize
1769 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect,
1770 RECT *validRects )
1772 UINT wvrFlags = 0;
1773 RECT window_rect, client_rect;
1775 WIN_GetRectangles( pWinpos->hwnd, COORDS_PARENT, &window_rect, &client_rect );
1777 /* Send WM_NCCALCSIZE message to get new client area */
1778 if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
1780 NCCALCSIZE_PARAMS params;
1781 WINDOWPOS winposCopy;
1783 params.rgrc[0] = *pNewWindowRect;
1784 params.rgrc[1] = window_rect;
1785 params.rgrc[2] = client_rect;
1786 params.lppos = &winposCopy;
1787 winposCopy = *pWinpos;
1789 wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
1791 *pNewClientRect = params.rgrc[0];
1793 TRACE( "hwnd %p old win %s old client %s new win %s new client %s\n", pWinpos->hwnd,
1794 wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(&client_rect),
1795 wine_dbgstr_rect(pNewWindowRect), wine_dbgstr_rect(pNewClientRect) );
1797 if( pNewClientRect->left != client_rect.left ||
1798 pNewClientRect->top != client_rect.top )
1799 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1801 if( (pNewClientRect->right - pNewClientRect->left !=
1802 client_rect.right - client_rect.left))
1803 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1804 else
1805 wvrFlags &= ~WVR_HREDRAW;
1807 if (pNewClientRect->bottom - pNewClientRect->top !=
1808 client_rect.bottom - client_rect.top)
1809 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1810 else
1811 wvrFlags &= ~WVR_VREDRAW;
1813 validRects[0] = params.rgrc[1];
1814 validRects[1] = params.rgrc[2];
1816 else
1818 if (!(pWinpos->flags & SWP_NOMOVE) &&
1819 (pNewClientRect->left != client_rect.left ||
1820 pNewClientRect->top != client_rect.top))
1821 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1824 if (pWinpos->flags & (SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
1826 SetRectEmpty( &validRects[0] );
1827 SetRectEmpty( &validRects[1] );
1829 else get_valid_rects( &client_rect, pNewClientRect, wvrFlags, validRects );
1831 return wvrFlags;
1834 /* fix redundant flags and values in the WINDOWPOS structure */
1835 static BOOL fixup_flags( WINDOWPOS *winpos )
1837 HWND parent;
1838 RECT window_rect;
1839 WND *wndPtr = WIN_GetPtr( winpos->hwnd );
1840 BOOL ret = TRUE;
1842 if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
1844 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1845 return FALSE;
1847 winpos->hwnd = wndPtr->obj.handle; /* make it a full handle */
1849 /* Finally make sure that all coordinates are valid */
1850 if (winpos->x < -32768) winpos->x = -32768;
1851 else if (winpos->x > 32767) winpos->x = 32767;
1852 if (winpos->y < -32768) winpos->y = -32768;
1853 else if (winpos->y > 32767) winpos->y = 32767;
1855 if (winpos->cx < 0) winpos->cx = 0;
1856 else if (winpos->cx > 32767) winpos->cx = 32767;
1857 if (winpos->cy < 0) winpos->cy = 0;
1858 else if (winpos->cy > 32767) winpos->cy = 32767;
1860 parent = GetAncestor( winpos->hwnd, GA_PARENT );
1861 if (!IsWindowVisible( parent )) winpos->flags |= SWP_NOREDRAW;
1863 if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
1864 else
1866 winpos->flags &= ~SWP_HIDEWINDOW;
1867 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
1870 WIN_GetRectangles( winpos->hwnd, COORDS_PARENT, &window_rect, NULL );
1871 if ((window_rect.right - window_rect.left == winpos->cx) &&
1872 (window_rect.bottom - window_rect.top == winpos->cy))
1873 winpos->flags |= SWP_NOSIZE; /* Already the right size */
1875 if ((window_rect.left == winpos->x) && (window_rect.top == winpos->y))
1876 winpos->flags |= SWP_NOMOVE; /* Already the right position */
1878 if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
1880 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)) && /* Bring to the top when activating */
1881 (winpos->flags & SWP_NOZORDER ||
1882 (winpos->hwndInsertAfter != HWND_TOPMOST && winpos->hwndInsertAfter != HWND_NOTOPMOST)))
1884 winpos->flags &= ~SWP_NOZORDER;
1885 winpos->hwndInsertAfter = HWND_TOP;
1889 /* Check hwndInsertAfter */
1890 if (winpos->flags & SWP_NOZORDER) goto done;
1892 /* fix sign extension */
1893 if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
1894 else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
1896 /* hwndInsertAfter must be a sibling of the window */
1897 if (winpos->hwndInsertAfter == HWND_TOP)
1899 if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1900 winpos->flags |= SWP_NOZORDER;
1902 else if (winpos->hwndInsertAfter == HWND_BOTTOM)
1904 if (!(wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
1905 winpos->flags |= SWP_NOZORDER;
1907 else if (winpos->hwndInsertAfter == HWND_TOPMOST)
1909 if ((wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1910 winpos->flags |= SWP_NOZORDER;
1912 else if (winpos->hwndInsertAfter == HWND_NOTOPMOST)
1914 if (!(wndPtr->dwExStyle & WS_EX_TOPMOST))
1915 winpos->flags |= SWP_NOZORDER;
1917 else
1919 if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != parent) ret = FALSE;
1920 else
1922 /* don't need to change the Zorder of hwnd if it's already inserted
1923 * after hwndInsertAfter or when inserting hwnd after itself.
1925 if ((winpos->hwnd == winpos->hwndInsertAfter) ||
1926 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
1927 winpos->flags |= SWP_NOZORDER;
1930 done:
1931 WIN_ReleasePtr( wndPtr );
1932 return ret;
1936 /***********************************************************************
1937 * set_window_pos
1939 * Backend implementation of SetWindowPos.
1941 BOOL set_window_pos( HWND hwnd, HWND insert_after, UINT swp_flags,
1942 const RECT *window_rect, const RECT *client_rect, const RECT *valid_rects )
1944 WND *win;
1945 BOOL ret;
1946 RECT visible_rect, old_window_rect;
1947 int old_width;
1949 visible_rect = *window_rect;
1950 USER_Driver->pWindowPosChanging( hwnd, insert_after, swp_flags,
1951 window_rect, client_rect, &visible_rect );
1953 WIN_GetRectangles( hwnd, COORDS_SCREEN, &old_window_rect, NULL );
1955 if (!(win = WIN_GetPtr( hwnd ))) return FALSE;
1956 if (win == WND_DESKTOP || win == WND_OTHER_PROCESS) return FALSE;
1957 old_width = win->rectClient.right - win->rectClient.left;
1959 SERVER_START_REQ( set_window_pos )
1961 req->handle = wine_server_user_handle( hwnd );
1962 req->previous = wine_server_user_handle( insert_after );
1963 req->flags = swp_flags;
1964 req->window.left = window_rect->left;
1965 req->window.top = window_rect->top;
1966 req->window.right = window_rect->right;
1967 req->window.bottom = window_rect->bottom;
1968 req->client.left = client_rect->left;
1969 req->client.top = client_rect->top;
1970 req->client.right = client_rect->right;
1971 req->client.bottom = client_rect->bottom;
1972 if (memcmp( window_rect, &visible_rect, sizeof(RECT) ) || !IsRectEmpty( &valid_rects[0] ))
1974 wine_server_add_data( req, &visible_rect, sizeof(visible_rect) );
1975 if (!IsRectEmpty( &valid_rects[0] ))
1976 wine_server_add_data( req, valid_rects, 2 * sizeof(*valid_rects) );
1978 if ((ret = !wine_server_call( req )))
1980 win->dwStyle = reply->new_style;
1981 win->dwExStyle = reply->new_ex_style;
1982 win->rectWindow = *window_rect;
1983 win->rectClient = *client_rect;
1984 if (GetWindowLongW( win->parent, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
1986 RECT client;
1987 GetClientRect( win->parent, &client );
1988 mirror_rect( &client, &win->rectWindow );
1989 mirror_rect( &client, &win->rectClient );
1991 /* if an RTL window is resized the children have moved */
1992 if (win->dwExStyle & WS_EX_LAYOUTRTL && client_rect->right - client_rect->left != old_width)
1993 win->flags |= WIN_CHILDREN_MOVED;
1996 SERVER_END_REQ;
1997 WIN_ReleasePtr( win );
1999 if (ret)
2001 if (((swp_flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) ||
2002 (swp_flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW | SWP_STATECHANGED)))
2003 invalidate_dce( hwnd, &old_window_rect );
2005 USER_Driver->pWindowPosChanged( hwnd, insert_after, swp_flags, window_rect,
2006 client_rect, &visible_rect, valid_rects );
2008 return ret;
2012 /***********************************************************************
2013 * USER_SetWindowPos
2015 * User32 internal function
2017 BOOL USER_SetWindowPos( WINDOWPOS * winpos )
2019 RECT newWindowRect, newClientRect, valid_rects[2];
2020 UINT orig_flags;
2022 orig_flags = winpos->flags;
2024 /* First make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
2025 if (!(winpos->flags & SWP_NOMOVE))
2027 if (winpos->x < -32768) winpos->x = -32768;
2028 else if (winpos->x > 32767) winpos->x = 32767;
2029 if (winpos->y < -32768) winpos->y = -32768;
2030 else if (winpos->y > 32767) winpos->y = 32767;
2032 if (!(winpos->flags & SWP_NOSIZE))
2034 if (winpos->cx < 0) winpos->cx = 0;
2035 else if (winpos->cx > 32767) winpos->cx = 32767;
2036 if (winpos->cy < 0) winpos->cy = 0;
2037 else if (winpos->cy > 32767) winpos->cy = 32767;
2040 if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
2042 /* Fix redundant flags */
2043 if (!fixup_flags( winpos )) return FALSE;
2045 if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
2047 if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
2048 winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
2051 /* Common operations */
2053 SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect, valid_rects );
2055 if (!set_window_pos( winpos->hwnd, winpos->hwndInsertAfter, winpos->flags,
2056 &newWindowRect, &newClientRect, valid_rects ))
2057 return FALSE;
2059 /* erase parent when hiding or resizing child */
2060 if (!(orig_flags & SWP_DEFERERASE) &&
2061 ((orig_flags & SWP_HIDEWINDOW) ||
2062 (!(orig_flags & SWP_SHOWWINDOW) &&
2063 (winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOGEOMETRYCHANGE)))
2065 HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
2066 if (!parent || parent == GetDesktopWindow()) parent = winpos->hwnd;
2067 erase_now( parent, 0 );
2070 if( winpos->flags & SWP_HIDEWINDOW )
2071 HideCaret(winpos->hwnd);
2072 else if (winpos->flags & SWP_SHOWWINDOW)
2073 ShowCaret(winpos->hwnd);
2075 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)))
2077 /* child windows get WM_CHILDACTIVATE message */
2078 if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
2079 SendMessageW( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
2080 else
2081 SetForegroundWindow( winpos->hwnd );
2084 /* And last, send the WM_WINDOWPOSCHANGED message */
2086 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
2088 if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
2090 /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
2091 and always contains final window position.
2093 winpos->x = newWindowRect.left;
2094 winpos->y = newWindowRect.top;
2095 winpos->cx = newWindowRect.right - newWindowRect.left;
2096 winpos->cy = newWindowRect.bottom - newWindowRect.top;
2097 SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
2099 return TRUE;
2102 /***********************************************************************
2103 * SetWindowPos (USER32.@)
2105 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
2106 INT x, INT y, INT cx, INT cy, UINT flags )
2108 WINDOWPOS winpos;
2110 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2111 hwnd, hwndInsertAfter, x, y, cx, cy, flags);
2112 if(TRACE_ON(win)) dump_winpos_flags(flags);
2114 if (is_broadcast(hwnd))
2116 SetLastError( ERROR_INVALID_PARAMETER );
2117 return FALSE;
2120 winpos.hwnd = WIN_GetFullHandle(hwnd);
2121 winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
2122 winpos.x = x;
2123 winpos.y = y;
2124 winpos.cx = cx;
2125 winpos.cy = cy;
2126 winpos.flags = flags;
2128 if (WIN_IsCurrentThread( hwnd ))
2129 return USER_SetWindowPos(&winpos);
2131 return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
2135 /***********************************************************************
2136 * BeginDeferWindowPos (USER32.@)
2138 HDWP WINAPI BeginDeferWindowPos( INT count )
2140 HDWP handle = 0;
2141 DWP *pDWP;
2143 TRACE("%d\n", count);
2145 if (count < 0)
2147 SetLastError(ERROR_INVALID_PARAMETER);
2148 return 0;
2150 /* Windows allows zero count, in which case it allocates context for 8 moves */
2151 if (count == 0) count = 8;
2153 if (!(pDWP = HeapAlloc( GetProcessHeap(), 0, sizeof(DWP)))) return 0;
2155 pDWP->actualCount = 0;
2156 pDWP->suggestedCount = count;
2157 pDWP->hwndParent = 0;
2159 if (!(pDWP->winPos = HeapAlloc( GetProcessHeap(), 0, count * sizeof(WINDOWPOS) )) ||
2160 !(handle = alloc_user_handle( &pDWP->obj, USER_DWP )))
2162 HeapFree( GetProcessHeap(), 0, pDWP->winPos );
2163 HeapFree( GetProcessHeap(), 0, pDWP );
2166 TRACE("returning hdwp %p\n", handle);
2167 return handle;
2171 /***********************************************************************
2172 * DeferWindowPos (USER32.@)
2174 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
2175 INT x, INT y, INT cx, INT cy,
2176 UINT flags )
2178 DWP *pDWP;
2179 int i;
2180 HDWP retvalue = hdwp;
2182 TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2183 hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
2185 hwnd = WIN_GetFullHandle( hwnd );
2186 if (is_desktop_window( hwnd )) return 0;
2188 if (!(pDWP = get_user_handle_ptr( hdwp, USER_DWP ))) return 0;
2189 if (pDWP == OBJ_OTHER_PROCESS)
2191 FIXME( "other process handle %p?\n", hdwp );
2192 return 0;
2195 for (i = 0; i < pDWP->actualCount; i++)
2197 if (pDWP->winPos[i].hwnd == hwnd)
2199 /* Merge with the other changes */
2200 if (!(flags & SWP_NOZORDER))
2202 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
2204 if (!(flags & SWP_NOMOVE))
2206 pDWP->winPos[i].x = x;
2207 pDWP->winPos[i].y = y;
2209 if (!(flags & SWP_NOSIZE))
2211 pDWP->winPos[i].cx = cx;
2212 pDWP->winPos[i].cy = cy;
2214 pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
2215 SWP_NOZORDER | SWP_NOREDRAW |
2216 SWP_NOACTIVATE | SWP_NOCOPYBITS|
2217 SWP_NOOWNERZORDER);
2218 pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
2219 SWP_FRAMECHANGED);
2220 goto END;
2223 if (pDWP->actualCount >= pDWP->suggestedCount)
2225 WINDOWPOS *newpos = HeapReAlloc( GetProcessHeap(), 0, pDWP->winPos,
2226 pDWP->suggestedCount * 2 * sizeof(WINDOWPOS) );
2227 if (!newpos)
2229 retvalue = 0;
2230 goto END;
2232 pDWP->suggestedCount *= 2;
2233 pDWP->winPos = newpos;
2235 pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
2236 pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
2237 pDWP->winPos[pDWP->actualCount].x = x;
2238 pDWP->winPos[pDWP->actualCount].y = y;
2239 pDWP->winPos[pDWP->actualCount].cx = cx;
2240 pDWP->winPos[pDWP->actualCount].cy = cy;
2241 pDWP->winPos[pDWP->actualCount].flags = flags;
2242 pDWP->actualCount++;
2243 END:
2244 release_user_handle_ptr( pDWP );
2245 return retvalue;
2249 /***********************************************************************
2250 * EndDeferWindowPos (USER32.@)
2252 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
2254 DWP *pDWP;
2255 WINDOWPOS *winpos;
2256 BOOL res = TRUE;
2257 int i;
2259 TRACE("%p\n", hdwp);
2261 if (!(pDWP = free_user_handle( hdwp, USER_DWP ))) return FALSE;
2262 if (pDWP == OBJ_OTHER_PROCESS)
2264 FIXME( "other process handle %p?\n", hdwp );
2265 return FALSE;
2268 for (i = 0, winpos = pDWP->winPos; res && i < pDWP->actualCount; i++, winpos++)
2270 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2271 winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
2272 winpos->cx, winpos->cy, winpos->flags);
2274 if (WIN_IsCurrentThread( winpos->hwnd ))
2275 res = USER_SetWindowPos( winpos );
2276 else
2277 res = SendMessageW( winpos->hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)winpos );
2279 HeapFree( GetProcessHeap(), 0, pDWP->winPos );
2280 HeapFree( GetProcessHeap(), 0, pDWP );
2281 return res;
2285 /***********************************************************************
2286 * ArrangeIconicWindows (USER32.@)
2288 UINT WINAPI ArrangeIconicWindows( HWND parent )
2290 RECT rectParent;
2291 HWND hwndChild;
2292 INT x, y, xspacing, yspacing;
2294 GetClientRect( parent, &rectParent );
2295 x = rectParent.left;
2296 y = rectParent.bottom;
2297 xspacing = GetSystemMetrics(SM_CXICONSPACING);
2298 yspacing = GetSystemMetrics(SM_CYICONSPACING);
2300 hwndChild = GetWindow( parent, GW_CHILD );
2301 while (hwndChild)
2303 if( IsIconic( hwndChild ) )
2305 WINPOS_ShowIconTitle( hwndChild, FALSE );
2307 SetWindowPos( hwndChild, 0, x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
2308 y - yspacing - GetSystemMetrics(SM_CYICON)/2, 0, 0,
2309 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
2310 if( IsWindow(hwndChild) )
2311 WINPOS_ShowIconTitle(hwndChild , TRUE );
2313 if (x <= rectParent.right - xspacing) x += xspacing;
2314 else
2316 x = rectParent.left;
2317 y -= yspacing;
2320 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
2322 return yspacing;
2326 /***********************************************************************
2327 * draw_moving_frame
2329 * Draw the frame used when moving or resizing window.
2331 static void draw_moving_frame( HWND parent, HDC hdc, RECT *screen_rect, BOOL thickframe )
2333 RECT rect = *screen_rect;
2335 if (parent) MapWindowPoints( 0, parent, (POINT *)&rect, 2 );
2336 if (thickframe)
2338 const int width = GetSystemMetrics(SM_CXFRAME);
2339 const int height = GetSystemMetrics(SM_CYFRAME);
2341 HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
2342 PatBlt( hdc, rect.left, rect.top,
2343 rect.right - rect.left - width, height, PATINVERT );
2344 PatBlt( hdc, rect.left, rect.top + height, width,
2345 rect.bottom - rect.top - height, PATINVERT );
2346 PatBlt( hdc, rect.left + width, rect.bottom - 1,
2347 rect.right - rect.left - width, -height, PATINVERT );
2348 PatBlt( hdc, rect.right - 1, rect.top, -width,
2349 rect.bottom - rect.top - height, PATINVERT );
2350 SelectObject( hdc, hbrush );
2352 else DrawFocusRect( hdc, &rect );
2356 /***********************************************************************
2357 * start_size_move
2359 * Initialization of a move or resize, when initiated from a menu choice.
2360 * Return hit test code for caption or sizing border.
2362 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
2364 LONG hittest = 0;
2365 POINT pt;
2366 MSG msg;
2367 RECT rectWindow;
2369 GetWindowRect( hwnd, &rectWindow );
2371 if ((wParam & 0xfff0) == SC_MOVE)
2373 /* Move pointer at the center of the caption */
2374 RECT rect = rectWindow;
2375 /* Note: to be exactly centered we should take the different types
2376 * of border into account, but it shouldn't make more than a few pixels
2377 * of difference so let's not bother with that */
2378 rect.top += GetSystemMetrics(SM_CYBORDER);
2379 if (style & WS_SYSMENU)
2380 rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
2381 if (style & WS_MINIMIZEBOX)
2382 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
2383 if (style & WS_MAXIMIZEBOX)
2384 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
2385 pt.x = (rect.right + rect.left) / 2;
2386 pt.y = rect.top + GetSystemMetrics(SM_CYSIZE)/2;
2387 hittest = HTCAPTION;
2388 *capturePoint = pt;
2390 else /* SC_SIZE */
2392 SetCursor( LoadCursorW( 0, (LPWSTR)IDC_SIZEALL ) );
2393 pt.x = pt.y = 0;
2394 while(!hittest)
2396 if (!GetMessageW( &msg, 0, 0, 0 )) return 0;
2397 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2399 switch(msg.message)
2401 case WM_MOUSEMOVE:
2402 pt.x = min( max( msg.pt.x, rectWindow.left ), rectWindow.right - 1 );
2403 pt.y = min( max( msg.pt.y, rectWindow.top ), rectWindow.bottom - 1 );
2404 hittest = SendMessageW( hwnd, WM_NCHITTEST, 0, MAKELONG( pt.x, pt.y ) );
2405 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT)) hittest = 0;
2406 break;
2408 case WM_LBUTTONUP:
2409 return 0;
2411 case WM_KEYDOWN:
2412 switch(msg.wParam)
2414 case VK_UP:
2415 hittest = HTTOP;
2416 pt.x =(rectWindow.left+rectWindow.right)/2;
2417 pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
2418 break;
2419 case VK_DOWN:
2420 hittest = HTBOTTOM;
2421 pt.x =(rectWindow.left+rectWindow.right)/2;
2422 pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
2423 break;
2424 case VK_LEFT:
2425 hittest = HTLEFT;
2426 pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
2427 pt.y =(rectWindow.top+rectWindow.bottom)/2;
2428 break;
2429 case VK_RIGHT:
2430 hittest = HTRIGHT;
2431 pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
2432 pt.y =(rectWindow.top+rectWindow.bottom)/2;
2433 break;
2434 case VK_RETURN:
2435 case VK_ESCAPE:
2436 return 0;
2438 break;
2439 default:
2440 TranslateMessage( &msg );
2441 DispatchMessageW( &msg );
2442 break;
2445 *capturePoint = pt;
2447 SetCursorPos( pt.x, pt.y );
2448 SendMessageW( hwnd, WM_SETCURSOR, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
2449 return hittest;
2453 /***********************************************************************
2454 * WINPOS_SysCommandSizeMove
2456 * Perform SC_MOVE and SC_SIZE commands.
2458 void WINPOS_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
2460 MSG msg;
2461 RECT sizingRect, mouseRect, origRect;
2462 HDC hdc;
2463 HWND parent;
2464 LONG hittest = (LONG)(wParam & 0x0f);
2465 WPARAM syscommand = wParam & 0xfff0;
2466 HCURSOR hDragCursor = 0, hOldCursor = 0;
2467 POINT minTrack, maxTrack;
2468 POINT capturePoint, pt;
2469 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
2470 BOOL thickframe = HAS_THICKFRAME( style );
2471 BOOL iconic = style & WS_MINIMIZE;
2472 BOOL moved = FALSE;
2473 DWORD dwPoint = GetMessagePos ();
2474 BOOL DragFullWindows = TRUE;
2476 if (IsZoomed(hwnd) || !IsWindowVisible(hwnd)) return;
2478 pt.x = (short)LOWORD(dwPoint);
2479 pt.y = (short)HIWORD(dwPoint);
2480 capturePoint = pt;
2482 TRACE("hwnd %p command %04lx, hittest %d, pos %d,%d\n",
2483 hwnd, syscommand, hittest, pt.x, pt.y);
2485 if (syscommand == SC_MOVE)
2487 if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
2488 if (!hittest) return;
2490 else /* SC_SIZE */
2492 if ( hittest && (syscommand != SC_MOUSEMENU) )
2493 hittest += (HTLEFT - WMSZ_LEFT);
2494 else
2496 set_capture_window( hwnd, GUI_INMOVESIZE, NULL );
2497 hittest = start_size_move( hwnd, wParam, &capturePoint, style );
2498 if (!hittest)
2500 set_capture_window( 0, GUI_INMOVESIZE, NULL );
2501 return;
2506 /* Get min/max info */
2508 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
2509 WIN_GetRectangles( hwnd, COORDS_PARENT, &sizingRect, NULL );
2510 origRect = sizingRect;
2511 if (style & WS_CHILD)
2513 parent = GetParent(hwnd);
2514 GetClientRect( parent, &mouseRect );
2515 MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
2516 MapWindowPoints( parent, 0, (LPPOINT)&sizingRect, 2 );
2518 else
2520 parent = 0;
2521 GetClientRect( GetDesktopWindow(), &mouseRect );
2522 mouseRect.left = GetSystemMetrics( SM_XVIRTUALSCREEN );
2523 mouseRect.top = GetSystemMetrics( SM_YVIRTUALSCREEN );
2524 mouseRect.right = mouseRect.left + GetSystemMetrics( SM_CXVIRTUALSCREEN );
2525 mouseRect.bottom = mouseRect.top + GetSystemMetrics( SM_CYVIRTUALSCREEN );
2528 if (ON_LEFT_BORDER(hittest))
2530 mouseRect.left = max( mouseRect.left, sizingRect.right-maxTrack.x );
2531 mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
2533 else if (ON_RIGHT_BORDER(hittest))
2535 mouseRect.left = max( mouseRect.left, sizingRect.left+minTrack.x );
2536 mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
2538 if (ON_TOP_BORDER(hittest))
2540 mouseRect.top = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
2541 mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
2543 else if (ON_BOTTOM_BORDER(hittest))
2545 mouseRect.top = max( mouseRect.top, sizingRect.top+minTrack.y );
2546 mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
2549 /* Retrieve a default cache DC (without using the window style) */
2550 hdc = GetDCEx( parent, 0, DCX_CACHE );
2552 if( iconic ) /* create a cursor for dragging */
2554 hDragCursor = (HCURSOR)GetClassLongPtrW( hwnd, GCLP_HICON);
2555 if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageW( hwnd, WM_QUERYDRAGICON, 0, 0L);
2556 if( !hDragCursor ) iconic = FALSE;
2559 /* we only allow disabling the full window drag for child windows */
2560 if (parent) SystemParametersInfoW( SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0 );
2562 /* repaint the window before moving it around */
2563 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
2565 SendMessageW( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
2566 set_capture_window( hwnd, GUI_INMOVESIZE, NULL );
2568 while(1)
2570 int dx = 0, dy = 0;
2572 if (!GetMessageW( &msg, 0, 0, 0 )) break;
2573 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2575 /* Exit on button-up, Return, or Esc */
2576 if ((msg.message == WM_LBUTTONUP) ||
2577 ((msg.message == WM_KEYDOWN) &&
2578 ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
2580 if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
2582 TranslateMessage( &msg );
2583 DispatchMessageW( &msg );
2584 continue; /* We are not interested in other messages */
2587 pt = msg.pt;
2589 if (msg.message == WM_KEYDOWN) switch(msg.wParam)
2591 case VK_UP: pt.y -= 8; break;
2592 case VK_DOWN: pt.y += 8; break;
2593 case VK_LEFT: pt.x -= 8; break;
2594 case VK_RIGHT: pt.x += 8; break;
2597 pt.x = max( pt.x, mouseRect.left );
2598 pt.x = min( pt.x, mouseRect.right );
2599 pt.y = max( pt.y, mouseRect.top );
2600 pt.y = min( pt.y, mouseRect.bottom );
2602 dx = pt.x - capturePoint.x;
2603 dy = pt.y - capturePoint.y;
2605 if (dx || dy)
2607 if( !moved )
2609 moved = TRUE;
2611 if( iconic ) /* ok, no system popup tracking */
2613 hOldCursor = SetCursor(hDragCursor);
2614 ShowCursor( TRUE );
2615 WINPOS_ShowIconTitle( hwnd, FALSE );
2617 else if(!DragFullWindows)
2618 draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2621 if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
2622 else
2624 WPARAM wpSizingHit = 0;
2626 if(!iconic && !DragFullWindows) draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2627 if (hittest == HTCAPTION) OffsetRect( &sizingRect, dx, dy );
2628 if (ON_LEFT_BORDER(hittest)) sizingRect.left += dx;
2629 else if (ON_RIGHT_BORDER(hittest)) sizingRect.right += dx;
2630 if (ON_TOP_BORDER(hittest)) sizingRect.top += dy;
2631 else if (ON_BOTTOM_BORDER(hittest)) sizingRect.bottom += dy;
2632 capturePoint = pt;
2634 /* determine the hit location */
2635 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
2636 wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
2637 SendMessageW( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&sizingRect );
2639 if (!iconic)
2641 if(!DragFullWindows)
2642 draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2643 else
2645 RECT rect = sizingRect;
2646 MapWindowPoints( 0, parent, (POINT *)&rect, 2 );
2647 SetWindowPos( hwnd, 0, rect.left, rect.top,
2648 rect.right - rect.left, rect.bottom - rect.top,
2649 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2656 if( iconic )
2658 if( moved ) /* restore cursors, show icon title later on */
2660 ShowCursor( FALSE );
2661 SetCursor( hOldCursor );
2664 else if (moved && !DragFullWindows)
2666 draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2669 set_capture_window( 0, GUI_INMOVESIZE, NULL );
2670 ReleaseDC( parent, hdc );
2671 if (parent) MapWindowPoints( 0, parent, (POINT *)&sizingRect, 2 );
2673 if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
2674 moved = FALSE;
2676 SendMessageW( hwnd, WM_EXITSIZEMOVE, 0, 0 );
2677 SendMessageW( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
2679 /* window moved or resized */
2680 if (moved)
2682 /* if the moving/resizing isn't canceled call SetWindowPos
2683 * with the new position or the new size of the window
2685 if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
2687 /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
2688 if(!DragFullWindows || iconic)
2689 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
2690 sizingRect.right - sizingRect.left,
2691 sizingRect.bottom - sizingRect.top,
2692 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2694 else
2695 { /* restore previous size/position */
2696 if(DragFullWindows)
2697 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
2698 origRect.right - origRect.left,
2699 origRect.bottom - origRect.top,
2700 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2704 if (IsIconic(hwnd))
2706 /* Single click brings up the system menu when iconized */
2708 if( !moved )
2710 if(style & WS_SYSMENU )
2711 SendMessageW( hwnd, WM_SYSCOMMAND,
2712 SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
2714 else WINPOS_ShowIconTitle( hwnd, TRUE );