comctl32/treeview: Avoid a NULL pointer dereference.
[wine.git] / dlls / user32 / winpos.c
blob915cb5989a2e28e99f34bcf9601880f9d0c16306
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, rect, NULL );
97 if (ret)
99 MapWindowPoints( GetAncestor( hwnd, GA_PARENT ), 0, (POINT *)rect, 2 );
100 TRACE( "hwnd %p (%s)\n", hwnd, wine_dbgstr_rect(rect) );
102 return ret;
106 /***********************************************************************
107 * GetWindowRgn (USER32.@)
109 int WINAPI GetWindowRgn ( HWND hwnd, HRGN hrgn )
111 int nRet = ERROR;
112 NTSTATUS status;
113 HRGN win_rgn = 0;
114 RGNDATA *data;
115 size_t size = 256;
119 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
121 SetLastError( ERROR_OUTOFMEMORY );
122 return ERROR;
124 SERVER_START_REQ( get_window_region )
126 req->window = wine_server_user_handle( hwnd );
127 wine_server_set_reply( req, data->Buffer, size );
128 if (!(status = wine_server_call( req )))
130 size_t reply_size = wine_server_reply_size( reply );
131 if (reply_size)
133 data->rdh.dwSize = sizeof(data->rdh);
134 data->rdh.iType = RDH_RECTANGLES;
135 data->rdh.nCount = reply_size / sizeof(RECT);
136 data->rdh.nRgnSize = reply_size;
137 win_rgn = ExtCreateRegion( NULL, size, data );
140 else size = reply->total_size;
142 SERVER_END_REQ;
143 HeapFree( GetProcessHeap(), 0, data );
144 } while (status == STATUS_BUFFER_OVERFLOW);
146 if (status) SetLastError( RtlNtStatusToDosError(status) );
147 else if (win_rgn)
149 nRet = CombineRgn( hrgn, win_rgn, 0, RGN_COPY );
150 DeleteObject( win_rgn );
152 return nRet;
155 /***********************************************************************
156 * GetWindowRgnBox (USER32.@)
158 int WINAPI GetWindowRgnBox( HWND hwnd, LPRECT prect )
160 int ret = ERROR;
161 HRGN hrgn;
163 if (!prect)
164 return ERROR;
166 if ((hrgn = CreateRectRgn(0, 0, 0, 0)))
168 if ((ret = GetWindowRgn( hwnd, hrgn )) != ERROR )
169 ret = GetRgnBox( hrgn, prect );
170 DeleteObject(hrgn);
173 return ret;
176 /***********************************************************************
177 * SetWindowRgn (USER32.@)
179 int WINAPI SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL bRedraw )
181 static const RECT empty_rect;
182 BOOL ret;
184 if (hrgn)
186 RGNDATA *data;
187 DWORD size;
189 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
190 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
191 if (!GetRegionData( hrgn, size, data ))
193 HeapFree( GetProcessHeap(), 0, data );
194 return FALSE;
196 SERVER_START_REQ( set_window_region )
198 req->window = wine_server_user_handle( hwnd );
199 req->redraw = (bRedraw != 0);
200 if (data->rdh.nCount)
201 wine_server_add_data( req, data->Buffer, data->rdh.nCount * sizeof(RECT) );
202 else
203 wine_server_add_data( req, &empty_rect, sizeof(empty_rect) );
204 ret = !wine_server_call_err( req );
206 SERVER_END_REQ;
207 HeapFree( GetProcessHeap(), 0, data );
209 else /* clear existing region */
211 SERVER_START_REQ( set_window_region )
213 req->window = wine_server_user_handle( hwnd );
214 req->redraw = (bRedraw != 0);
215 ret = !wine_server_call_err( req );
217 SERVER_END_REQ;
220 if (ret) ret = USER_Driver->pSetWindowRgn( hwnd, hrgn, bRedraw );
222 if (ret)
224 UINT swp_flags = SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE;
225 if (!bRedraw) swp_flags |= SWP_NOREDRAW;
226 SetWindowPos( hwnd, 0, 0, 0, 0, 0, swp_flags );
227 invalidate_dce( hwnd, NULL );
229 return ret;
233 /***********************************************************************
234 * GetClientRect (USER32.@)
236 BOOL WINAPI GetClientRect( HWND hwnd, LPRECT rect )
238 BOOL ret;
240 if ((ret = WIN_GetRectangles( hwnd, NULL, rect )))
242 rect->right -= rect->left;
243 rect->bottom -= rect->top;
244 rect->left = rect->top = 0;
246 return ret;
250 /*******************************************************************
251 * ClientToScreen (USER32.@)
253 BOOL WINAPI ClientToScreen( HWND hwnd, LPPOINT lppnt )
255 MapWindowPoints( hwnd, 0, lppnt, 1 );
256 return TRUE;
260 /*******************************************************************
261 * ScreenToClient (USER32.@)
263 BOOL WINAPI ScreenToClient( HWND hwnd, LPPOINT lppnt )
265 MapWindowPoints( 0, hwnd, lppnt, 1 );
266 return TRUE;
270 /***********************************************************************
271 * list_children_from_point
273 * Get the list of children that can contain point from the server.
274 * Point is in screen coordinates.
275 * Returned list must be freed by caller.
277 static HWND *list_children_from_point( HWND hwnd, POINT pt )
279 HWND *list;
280 int i, size = 128;
282 for (;;)
284 int count = 0;
286 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
288 SERVER_START_REQ( get_window_children_from_point )
290 req->parent = wine_server_user_handle( hwnd );
291 req->x = pt.x;
292 req->y = pt.y;
293 wine_server_set_reply( req, list, (size-1) * sizeof(user_handle_t) );
294 if (!wine_server_call( req )) count = reply->count;
296 SERVER_END_REQ;
297 if (count && count < size)
299 /* start from the end since HWND is potentially larger than user_handle_t */
300 for (i = count - 1; i >= 0; i--)
301 list[i] = wine_server_ptr_handle( ((user_handle_t *)list)[i] );
302 list[count] = 0;
303 return list;
305 HeapFree( GetProcessHeap(), 0, list );
306 if (!count) break;
307 size = count + 1; /* restart with a large enough buffer */
309 return NULL;
313 /***********************************************************************
314 * WINPOS_WindowFromPoint
316 * Find the window and hittest for a given point.
318 HWND WINPOS_WindowFromPoint( HWND hwndScope, POINT pt, INT *hittest )
320 int i, res;
321 HWND ret, *list;
323 if (!hwndScope) hwndScope = GetDesktopWindow();
325 *hittest = HTNOWHERE;
327 if (!(list = list_children_from_point( hwndScope, pt ))) return 0;
329 /* now determine the hittest */
331 for (i = 0; list[i]; i++)
333 LONG style = GetWindowLongW( list[i], GWL_STYLE );
335 /* If window is minimized or disabled, return at once */
336 if (style & WS_MINIMIZE)
338 *hittest = HTCAPTION;
339 break;
341 if (style & WS_DISABLED)
343 *hittest = HTERROR;
344 break;
346 /* Send WM_NCCHITTEST (if same thread) */
347 if (!WIN_IsCurrentThread( list[i] ))
349 *hittest = HTCLIENT;
350 break;
352 res = SendMessageW( list[i], WM_NCHITTEST, 0, MAKELONG(pt.x,pt.y) );
353 if (res != HTTRANSPARENT)
355 *hittest = res; /* Found the window */
356 break;
358 /* continue search with next window in z-order */
360 ret = list[i];
361 HeapFree( GetProcessHeap(), 0, list );
362 TRACE( "scope %p (%d,%d) returning %p\n", hwndScope, pt.x, pt.y, ret );
363 return ret;
367 /*******************************************************************
368 * WindowFromPoint (USER32.@)
370 HWND WINAPI WindowFromPoint( POINT pt )
372 INT hittest;
373 return WINPOS_WindowFromPoint( 0, pt, &hittest );
377 /*******************************************************************
378 * ChildWindowFromPoint (USER32.@)
380 HWND WINAPI ChildWindowFromPoint( HWND hwndParent, POINT pt )
382 return ChildWindowFromPointEx( hwndParent, pt, CWP_ALL );
385 /*******************************************************************
386 * RealChildWindowFromPoint (USER32.@)
388 HWND WINAPI RealChildWindowFromPoint( HWND hwndParent, POINT pt )
390 return ChildWindowFromPointEx( hwndParent, pt, CWP_SKIPTRANSPARENT );
393 /*******************************************************************
394 * ChildWindowFromPointEx (USER32.@)
396 HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
398 /* pt is in the client coordinates */
399 HWND *list;
400 int i;
401 RECT rect;
402 HWND retvalue;
404 GetClientRect( hwndParent, &rect );
405 if (!PtInRect( &rect, pt )) return 0;
406 if (!(list = WIN_ListChildren( hwndParent ))) return hwndParent;
408 for (i = 0; list[i]; i++)
410 if (!WIN_GetRectangles( list[i], &rect, NULL )) continue;
411 if (!PtInRect( &rect, pt )) continue;
412 if (uFlags & (CWP_SKIPINVISIBLE|CWP_SKIPDISABLED))
414 LONG style = GetWindowLongW( list[i], GWL_STYLE );
415 if ((uFlags & CWP_SKIPINVISIBLE) && !(style & WS_VISIBLE)) continue;
416 if ((uFlags & CWP_SKIPDISABLED) && (style & WS_DISABLED)) continue;
418 if (uFlags & CWP_SKIPTRANSPARENT)
420 if (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TRANSPARENT) continue;
422 break;
424 retvalue = list[i];
425 HeapFree( GetProcessHeap(), 0, list );
426 if (!retvalue) retvalue = hwndParent;
427 return retvalue;
431 /*******************************************************************
432 * WINPOS_GetWinOffset
434 * Calculate the offset between the origin of the two windows. Used
435 * to implement MapWindowPoints.
437 static void WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo, POINT *offset )
439 WND * wndPtr;
441 offset->x = offset->y = 0;
443 /* Translate source window origin to screen coords */
444 if (hwndFrom)
446 HWND hwnd = hwndFrom;
448 while (hwnd)
450 if (hwnd == hwndTo) return;
451 if (!(wndPtr = WIN_GetPtr( hwnd )))
453 ERR( "bad hwndFrom = %p\n", hwnd );
454 return;
456 if (wndPtr == WND_DESKTOP) break;
457 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
458 if (wndPtr->parent)
460 offset->x += wndPtr->rectClient.left;
461 offset->y += wndPtr->rectClient.top;
463 hwnd = wndPtr->parent;
464 WIN_ReleasePtr( wndPtr );
468 /* Translate origin to destination window coords */
469 if (hwndTo)
471 HWND hwnd = hwndTo;
473 while (hwnd)
475 if (!(wndPtr = WIN_GetPtr( hwnd )))
477 ERR( "bad hwndTo = %p\n", hwnd );
478 return;
480 if (wndPtr == WND_DESKTOP) break;
481 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
482 if (wndPtr->parent)
484 offset->x -= wndPtr->rectClient.left;
485 offset->y -= wndPtr->rectClient.top;
487 hwnd = wndPtr->parent;
488 WIN_ReleasePtr( wndPtr );
491 return;
493 other_process: /* one of the parents may belong to another process, do it the hard way */
494 offset->x = offset->y = 0;
495 SERVER_START_REQ( get_windows_offset )
497 req->from = wine_server_user_handle( hwndFrom );
498 req->to = wine_server_user_handle( hwndTo );
499 if (!wine_server_call( req ))
501 offset->x = reply->x;
502 offset->y = reply->y;
505 SERVER_END_REQ;
509 /*******************************************************************
510 * MapWindowPoints (USER.258)
512 void WINAPI MapWindowPoints16( HWND16 hwndFrom, HWND16 hwndTo,
513 LPPOINT16 lppt, UINT16 count )
515 POINT offset;
517 WINPOS_GetWinOffset( WIN_Handle32(hwndFrom), WIN_Handle32(hwndTo), &offset );
518 while (count--)
520 lppt->x += offset.x;
521 lppt->y += offset.y;
522 lppt++;
527 /*******************************************************************
528 * MapWindowPoints (USER32.@)
530 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
532 POINT offset;
534 WINPOS_GetWinOffset( hwndFrom, hwndTo, &offset );
535 while (count--)
537 lppt->x += offset.x;
538 lppt->y += offset.y;
539 lppt++;
541 return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
545 /***********************************************************************
546 * IsIconic (USER32.@)
548 BOOL WINAPI IsIconic(HWND hWnd)
550 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
554 /***********************************************************************
555 * IsZoomed (USER32.@)
557 BOOL WINAPI IsZoomed(HWND hWnd)
559 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
563 /*******************************************************************
564 * AllowSetForegroundWindow (USER32.@)
566 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
568 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
569 * implemented, then fix this function. */
570 return TRUE;
574 /*******************************************************************
575 * LockSetForegroundWindow (USER32.@)
577 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
579 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
580 * implemented, then fix this function. */
581 return TRUE;
585 /***********************************************************************
586 * BringWindowToTop (USER32.@)
588 BOOL WINAPI BringWindowToTop( HWND hwnd )
590 return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
594 /***********************************************************************
595 * MoveWindow (USER32.@)
597 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
598 BOOL repaint )
600 int flags = SWP_NOZORDER | SWP_NOACTIVATE;
601 if (!repaint) flags |= SWP_NOREDRAW;
602 TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
603 return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
607 /***********************************************************************
608 * WINPOS_RedrawIconTitle
610 BOOL WINPOS_RedrawIconTitle( HWND hWnd )
612 HWND icon_title = 0;
613 WND *win = WIN_GetPtr( hWnd );
615 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
617 icon_title = win->icon_title;
618 WIN_ReleasePtr( win );
620 if (!icon_title) return FALSE;
621 SendMessageW( icon_title, WM_SHOWWINDOW, TRUE, 0 );
622 InvalidateRect( icon_title, NULL, TRUE );
623 return TRUE;
626 /***********************************************************************
627 * WINPOS_ShowIconTitle
629 static BOOL WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
631 if (!GetPropA( hwnd, "__wine_x11_managed" ))
633 WND *win = WIN_GetPtr( hwnd );
634 HWND title = 0;
636 TRACE("%p %i\n", hwnd, (bShow != 0) );
638 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP) return FALSE;
639 title = win->icon_title;
640 WIN_ReleasePtr( win );
642 if( bShow )
644 if (!title)
646 title = ICONTITLE_Create( hwnd );
647 if (!(win = WIN_GetPtr( hwnd )) || win == WND_OTHER_PROCESS)
649 DestroyWindow( title );
650 return FALSE;
652 win->icon_title = title;
653 WIN_ReleasePtr( win );
655 if (!IsWindowVisible(title))
657 SendMessageW( title, WM_SHOWWINDOW, TRUE, 0 );
658 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
659 SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
662 else if (title) ShowWindow( title, SW_HIDE );
664 return FALSE;
667 /*******************************************************************
668 * WINPOS_GetMinMaxInfo
670 * Get the minimized and maximized information for a window.
672 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
673 POINT *minTrack, POINT *maxTrack )
675 MINMAXINFO MinMax;
676 HMONITOR monitor;
677 INT xinc, yinc;
678 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
679 LONG adjustedStyle;
680 LONG exstyle = GetWindowLongW( hwnd, GWL_EXSTYLE );
681 RECT rc;
682 WND *win;
684 /* Compute default values */
686 GetWindowRect(hwnd, &rc);
687 MinMax.ptReserved.x = rc.left;
688 MinMax.ptReserved.y = rc.top;
690 if ((style & WS_CAPTION) == WS_CAPTION)
691 adjustedStyle = style & ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
692 else
693 adjustedStyle = style;
695 GetClientRect(GetAncestor(hwnd,GA_PARENT), &rc);
696 AdjustWindowRectEx(&rc, adjustedStyle, ((style & WS_POPUP) && GetMenu(hwnd)), exstyle);
698 xinc = -rc.left;
699 yinc = -rc.top;
701 MinMax.ptMaxSize.x = rc.right - rc.left;
702 MinMax.ptMaxSize.y = rc.bottom - rc.top;
703 if (style & (WS_DLGFRAME | WS_BORDER))
705 MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
706 MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
708 else
710 MinMax.ptMinTrackSize.x = 2 * xinc;
711 MinMax.ptMinTrackSize.y = 2 * yinc;
713 MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXMAXTRACK);
714 MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYMAXTRACK);
715 MinMax.ptMaxPosition.x = -xinc;
716 MinMax.ptMaxPosition.y = -yinc;
718 if ((win = WIN_GetPtr( hwnd )) && win != WND_DESKTOP && win != WND_OTHER_PROCESS)
720 if (!EMPTYPOINT(win->max_pos)) MinMax.ptMaxPosition = win->max_pos;
721 WIN_ReleasePtr( win );
724 SendMessageW( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
726 /* if the app didn't change the values, adapt them for the current monitor */
728 if ((monitor = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY )))
730 RECT rc_work;
731 MONITORINFO mon_info;
733 mon_info.cbSize = sizeof(mon_info);
734 GetMonitorInfoW( monitor, &mon_info );
736 rc_work = mon_info.rcMonitor;
738 if (style & WS_MAXIMIZEBOX)
740 if ((style & WS_CAPTION) == WS_CAPTION || !(style & (WS_CHILD | WS_POPUP)))
741 rc_work = mon_info.rcWork;
744 if (MinMax.ptMaxSize.x == GetSystemMetrics(SM_CXSCREEN) + 2 * xinc &&
745 MinMax.ptMaxSize.y == GetSystemMetrics(SM_CYSCREEN) + 2 * yinc)
747 MinMax.ptMaxSize.x = (rc_work.right - rc_work.left) + 2 * xinc;
748 MinMax.ptMaxSize.y = (rc_work.bottom - rc_work.top) + 2 * yinc;
750 if (MinMax.ptMaxPosition.x == -xinc && MinMax.ptMaxPosition.y == -yinc)
752 MinMax.ptMaxPosition.x = rc_work.left - xinc;
753 MinMax.ptMaxPosition.y = rc_work.top - yinc;
757 /* Some sanity checks */
759 TRACE("%d %d / %d %d / %d %d / %d %d\n",
760 MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
761 MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
762 MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
763 MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
764 MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
765 MinMax.ptMinTrackSize.x );
766 MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
767 MinMax.ptMinTrackSize.y );
769 if (maxSize) *maxSize = MinMax.ptMaxSize;
770 if (maxPos) *maxPos = MinMax.ptMaxPosition;
771 if (minTrack) *minTrack = MinMax.ptMinTrackSize;
772 if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
776 /***********************************************************************
777 * WINPOS_FindIconPos
779 * Find a suitable place for an iconic window.
781 static POINT WINPOS_FindIconPos( HWND hwnd, POINT pt )
783 RECT rect, rectParent;
784 HWND parent, child;
785 HRGN hrgn, tmp;
786 int xspacing, yspacing;
788 parent = GetAncestor( hwnd, GA_PARENT );
789 GetClientRect( parent, &rectParent );
790 if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
791 (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
792 return pt; /* The icon already has a suitable position */
794 xspacing = GetSystemMetrics(SM_CXICONSPACING);
795 yspacing = GetSystemMetrics(SM_CYICONSPACING);
797 /* Check if another icon already occupies this spot */
798 /* FIXME: this is completely inefficient */
800 hrgn = CreateRectRgn( 0, 0, 0, 0 );
801 tmp = CreateRectRgn( 0, 0, 0, 0 );
802 for (child = GetWindow( parent, GW_HWNDFIRST ); child; child = GetWindow( child, GW_HWNDNEXT ))
804 WND *childPtr;
805 if (child == hwnd) continue;
806 if ((GetWindowLongW( child, GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != (WS_VISIBLE|WS_MINIMIZE))
807 continue;
808 if (!(childPtr = WIN_GetPtr( child )) || childPtr == WND_OTHER_PROCESS)
809 continue;
810 SetRectRgn( tmp, childPtr->rectWindow.left, childPtr->rectWindow.top,
811 childPtr->rectWindow.right, childPtr->rectWindow.bottom );
812 CombineRgn( hrgn, hrgn, tmp, RGN_OR );
813 WIN_ReleasePtr( childPtr );
815 DeleteObject( tmp );
817 for (rect.bottom = rectParent.bottom; rect.bottom >= yspacing; rect.bottom -= yspacing)
819 for (rect.left = rectParent.left; rect.left <= rectParent.right - xspacing; rect.left += xspacing)
821 rect.right = rect.left + xspacing;
822 rect.top = rect.bottom - yspacing;
823 if (!RectInRegion( hrgn, &rect ))
825 /* No window was found, so it's OK for us */
826 pt.x = rect.left + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
827 pt.y = rect.top + (yspacing - GetSystemMetrics(SM_CYICON)) / 2;
828 DeleteObject( hrgn );
829 return pt;
833 DeleteObject( hrgn );
834 pt.x = pt.y = 0;
835 return pt;
839 /***********************************************************************
840 * WINPOS_MinMaximize
842 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
844 WND *wndPtr;
845 UINT swpFlags = 0;
846 POINT size;
847 LONG old_style;
848 WINDOWPLACEMENT wpl;
850 TRACE("%p %u\n", hwnd, cmd );
852 wpl.length = sizeof(wpl);
853 GetWindowPlacement( hwnd, &wpl );
855 if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
856 return SWP_NOSIZE | SWP_NOMOVE;
858 if (IsIconic( hwnd ))
860 switch (cmd)
862 case SW_SHOWMINNOACTIVE:
863 case SW_SHOWMINIMIZED:
864 case SW_FORCEMINIMIZE:
865 case SW_MINIMIZE:
866 return SWP_NOSIZE | SWP_NOMOVE;
868 if (!SendMessageW( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
869 swpFlags |= SWP_NOCOPYBITS;
872 switch( cmd )
874 case SW_SHOWMINNOACTIVE:
875 case SW_SHOWMINIMIZED:
876 case SW_FORCEMINIMIZE:
877 case SW_MINIMIZE:
878 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
879 if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
880 else wndPtr->flags &= ~WIN_RESTORE_MAX;
881 WIN_ReleasePtr( wndPtr );
883 old_style = WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
885 wpl.ptMinPosition = WINPOS_FindIconPos( hwnd, wpl.ptMinPosition );
887 if (!(old_style & WS_MINIMIZE)) swpFlags |= SWP_STATECHANGED;
888 SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
889 wpl.ptMinPosition.x + GetSystemMetrics(SM_CXICON),
890 wpl.ptMinPosition.y + GetSystemMetrics(SM_CYICON) );
891 swpFlags |= SWP_NOCOPYBITS;
892 break;
894 case SW_MAXIMIZE:
895 old_style = GetWindowLongW( hwnd, GWL_STYLE );
896 if ((old_style & WS_MAXIMIZE) && (old_style & WS_VISIBLE)) return SWP_NOSIZE | SWP_NOMOVE;
898 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
900 old_style = WIN_SetStyle( hwnd, WS_MAXIMIZE, WS_MINIMIZE );
901 if (old_style & WS_MINIMIZE) WINPOS_ShowIconTitle( hwnd, FALSE );
903 if (!(old_style & WS_MAXIMIZE)) swpFlags |= SWP_STATECHANGED;
904 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y,
905 wpl.ptMaxPosition.x + size.x, wpl.ptMaxPosition.y + size.y );
906 break;
908 case SW_SHOWNOACTIVATE:
909 case SW_SHOWNORMAL:
910 case SW_RESTORE:
911 old_style = WIN_SetStyle( hwnd, 0, WS_MINIMIZE | WS_MAXIMIZE );
912 if (old_style & WS_MINIMIZE)
914 BOOL restore_max;
916 WINPOS_ShowIconTitle( hwnd, FALSE );
918 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
919 restore_max = (wndPtr->flags & WIN_RESTORE_MAX) != 0;
920 WIN_ReleasePtr( wndPtr );
921 if (restore_max)
923 /* Restore to maximized position */
924 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
925 WIN_SetStyle( hwnd, WS_MAXIMIZE, 0 );
926 swpFlags |= SWP_STATECHANGED;
927 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y,
928 wpl.ptMaxPosition.x + size.x, wpl.ptMaxPosition.y + size.y );
929 break;
932 else if (!(old_style & WS_MAXIMIZE)) break;
934 swpFlags |= SWP_STATECHANGED;
936 /* Restore to normal position */
938 *rect = wpl.rcNormalPosition;
939 break;
942 return swpFlags;
946 /***********************************************************************
947 * show_window
949 * Implementation of ShowWindow and ShowWindowAsync.
951 static BOOL show_window( HWND hwnd, INT cmd )
953 WND *wndPtr;
954 HWND parent;
955 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
956 BOOL wasVisible = (style & WS_VISIBLE) != 0;
957 BOOL showFlag = TRUE;
958 RECT newPos = {0, 0, 0, 0};
959 UINT swp = 0;
961 TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);
963 switch(cmd)
965 case SW_HIDE:
966 if (!wasVisible) return FALSE;
967 showFlag = FALSE;
968 swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE;
969 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
970 break;
972 case SW_SHOWMINNOACTIVE:
973 case SW_MINIMIZE:
974 case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
975 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
976 /* fall through */
977 case SW_SHOWMINIMIZED:
978 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
979 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
980 if ((style & WS_MINIMIZE) && wasVisible) return TRUE;
981 break;
983 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
984 if (!wasVisible) swp |= SWP_SHOWWINDOW;
985 swp |= SWP_FRAMECHANGED;
986 swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
987 if ((style & WS_MAXIMIZE) && wasVisible) return TRUE;
988 break;
990 case SW_SHOWNA:
991 swp |= SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
992 if (style & WS_CHILD) swp |= SWP_NOZORDER;
993 break;
994 case SW_SHOW:
995 if (wasVisible) return TRUE;
996 swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
997 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
998 break;
1000 case SW_SHOWNOACTIVATE:
1001 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1002 /* fall through */
1003 case SW_RESTORE:
1004 /* fall through */
1005 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
1006 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
1007 if (!wasVisible) swp |= SWP_SHOWWINDOW;
1008 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1010 swp |= SWP_FRAMECHANGED;
1011 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
1013 else
1015 if (wasVisible) return TRUE;
1016 swp |= SWP_NOSIZE | SWP_NOMOVE;
1018 if (style & WS_CHILD && !(swp & SWP_STATECHANGED)) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1019 break;
1020 default:
1021 return wasVisible;
1024 if ((showFlag != wasVisible || cmd == SW_SHOWNA) && cmd != SW_SHOWMAXIMIZED && !(swp & SWP_STATECHANGED))
1026 SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
1027 if (!IsWindow( hwnd )) return wasVisible;
1030 swp = USER_Driver->pShowWindow( hwnd, cmd, &newPos, swp );
1032 parent = GetAncestor( hwnd, GA_PARENT );
1033 if (parent && !IsWindowVisible( parent ) && !(swp & SWP_STATECHANGED))
1035 /* if parent is not visible simply toggle WS_VISIBLE and return */
1036 if (showFlag) WIN_SetStyle( hwnd, WS_VISIBLE, 0 );
1037 else WIN_SetStyle( hwnd, 0, WS_VISIBLE );
1039 else
1040 SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1041 newPos.right - newPos.left, newPos.bottom - newPos.top, swp );
1043 if (cmd == SW_HIDE)
1045 HWND hFocus;
1047 WINPOS_ShowIconTitle( hwnd, FALSE );
1049 /* FIXME: This will cause the window to be activated irrespective
1050 * of whether it is owned by the same thread. Has to be done
1051 * asynchronously.
1054 if (hwnd == GetActiveWindow())
1055 WINPOS_ActivateOtherWindow(hwnd);
1057 /* Revert focus to parent */
1058 hFocus = GetFocus();
1059 if (hwnd == hFocus)
1061 HWND parent = GetAncestor(hwnd, GA_PARENT);
1062 if (parent == GetDesktopWindow()) parent = 0;
1063 SetFocus(parent);
1065 return wasVisible;
1068 if (IsIconic(hwnd)) WINPOS_ShowIconTitle( hwnd, TRUE );
1070 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return wasVisible;
1072 if (wndPtr->flags & WIN_NEED_SIZE)
1074 /* should happen only in CreateWindowEx() */
1075 int wParam = SIZE_RESTORED;
1076 RECT client = wndPtr->rectClient;
1077 LPARAM lparam = MAKELONG( client.right - client.left, client.bottom - client.top );
1079 wndPtr->flags &= ~WIN_NEED_SIZE;
1080 if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1081 else if (wndPtr->dwStyle & WS_MINIMIZE)
1083 wParam = SIZE_MINIMIZED;
1084 lparam = 0;
1086 WIN_ReleasePtr( wndPtr );
1088 SendMessageW( hwnd, WM_SIZE, wParam, lparam );
1089 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( client.left, client.top ));
1091 else WIN_ReleasePtr( wndPtr );
1093 /* if previous state was minimized Windows sets focus to the window */
1094 if (style & WS_MINIMIZE) SetFocus( hwnd );
1096 return wasVisible;
1100 /***********************************************************************
1101 * ShowWindowAsync (USER32.@)
1103 * doesn't wait; returns immediately.
1104 * used by threads to toggle windows in other (possibly hanging) threads
1106 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
1108 HWND full_handle;
1110 if (is_broadcast(hwnd))
1112 SetLastError( ERROR_INVALID_PARAMETER );
1113 return FALSE;
1116 if ((full_handle = WIN_IsCurrentThread( hwnd )))
1117 return show_window( full_handle, cmd );
1119 return SendNotifyMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
1123 /***********************************************************************
1124 * ShowWindow (USER32.@)
1126 BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
1128 HWND full_handle;
1130 if (is_broadcast(hwnd))
1132 SetLastError( ERROR_INVALID_PARAMETER );
1133 return FALSE;
1135 if ((full_handle = WIN_IsCurrentThread( hwnd )))
1136 return show_window( full_handle, cmd );
1138 return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
1142 /***********************************************************************
1143 * GetInternalWindowPos (USER32.@)
1145 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
1146 LPPOINT ptIcon )
1148 WINDOWPLACEMENT wndpl;
1149 if (GetWindowPlacement( hwnd, &wndpl ))
1151 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
1152 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
1153 return wndpl.showCmd;
1155 return 0;
1159 /***********************************************************************
1160 * GetWindowPlacement (USER32.@)
1162 * Win95:
1163 * Fails if wndpl->length of Win95 (!) apps is invalid.
1165 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
1167 WND *pWnd = WIN_GetPtr( hwnd );
1169 if (!pWnd) return FALSE;
1171 if (pWnd == WND_DESKTOP)
1173 wndpl->length = sizeof(*wndpl);
1174 wndpl->showCmd = SW_SHOWNORMAL;
1175 wndpl->flags = 0;
1176 wndpl->ptMinPosition.x = -1;
1177 wndpl->ptMinPosition.y = -1;
1178 wndpl->ptMaxPosition.x = -1;
1179 wndpl->ptMaxPosition.y = -1;
1180 GetWindowRect( hwnd, &wndpl->rcNormalPosition );
1181 return TRUE;
1183 if (pWnd == WND_OTHER_PROCESS)
1185 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
1186 return FALSE;
1189 /* update the placement according to the current style */
1190 if (pWnd->dwStyle & WS_MINIMIZE)
1192 pWnd->min_pos.x = pWnd->rectWindow.left;
1193 pWnd->min_pos.y = pWnd->rectWindow.top;
1195 else if (pWnd->dwStyle & WS_MAXIMIZE)
1197 pWnd->max_pos.x = pWnd->rectWindow.left;
1198 pWnd->max_pos.y = pWnd->rectWindow.top;
1200 else
1202 pWnd->normal_rect = pWnd->rectWindow;
1205 wndpl->length = sizeof(*wndpl);
1206 if( pWnd->dwStyle & WS_MINIMIZE )
1207 wndpl->showCmd = SW_SHOWMINIMIZED;
1208 else
1209 wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
1210 if( pWnd->flags & WIN_RESTORE_MAX )
1211 wndpl->flags = WPF_RESTORETOMAXIMIZED;
1212 else
1213 wndpl->flags = 0;
1214 wndpl->ptMinPosition = pWnd->min_pos;
1215 wndpl->ptMaxPosition = pWnd->max_pos;
1216 wndpl->rcNormalPosition = pWnd->normal_rect;
1217 WIN_ReleasePtr( pWnd );
1219 TRACE( "%p: returning min %d,%d max %d,%d normal %s\n",
1220 hwnd, wndpl->ptMinPosition.x, wndpl->ptMinPosition.y,
1221 wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y,
1222 wine_dbgstr_rect(&wndpl->rcNormalPosition) );
1223 return TRUE;
1226 /* make sure the specified rect is visible on screen */
1227 static void make_rect_onscreen( RECT *rect )
1229 MONITORINFO info;
1230 HMONITOR monitor = MonitorFromRect( rect, MONITOR_DEFAULTTONEAREST );
1232 info.cbSize = sizeof(info);
1233 if (!monitor || !GetMonitorInfoW( monitor, &info )) return;
1234 /* FIXME: map coordinates from rcWork to rcMonitor */
1235 if (rect->right <= info.rcWork.left)
1237 rect->right += info.rcWork.left - rect->left;
1238 rect->left = info.rcWork.left;
1240 else if (rect->left >= info.rcWork.right)
1242 rect->left += info.rcWork.right - rect->right;
1243 rect->right = info.rcWork.right;
1245 if (rect->bottom <= info.rcWork.top)
1247 rect->bottom += info.rcWork.top - rect->top;
1248 rect->top = info.rcWork.top;
1250 else if (rect->top >= info.rcWork.bottom)
1252 rect->top += info.rcWork.bottom - rect->bottom;
1253 rect->bottom = info.rcWork.bottom;
1257 /* make sure the specified point is visible on screen */
1258 static void make_point_onscreen( POINT *pt )
1260 RECT rect;
1262 SetRect( &rect, pt->x, pt->y, pt->x + 1, pt->y + 1 );
1263 make_rect_onscreen( &rect );
1264 pt->x = rect.left;
1265 pt->y = rect.top;
1269 /***********************************************************************
1270 * WINPOS_SetPlacement
1272 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
1274 DWORD style;
1275 WND *pWnd = WIN_GetPtr( hwnd );
1276 WINDOWPLACEMENT wp = *wndpl;
1278 if (flags & PLACE_MIN) make_point_onscreen( &wp.ptMinPosition );
1279 if (flags & PLACE_MAX) make_point_onscreen( &wp.ptMaxPosition );
1280 if (flags & PLACE_RECT) make_rect_onscreen( &wp.rcNormalPosition );
1282 TRACE( "%p: setting min %d,%d max %d,%d normal %s flags %x ajusted to min %d,%d max %d,%d normal %s\n",
1283 hwnd, wndpl->ptMinPosition.x, wndpl->ptMinPosition.y,
1284 wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y,
1285 wine_dbgstr_rect(&wndpl->rcNormalPosition), flags,
1286 wp.ptMinPosition.x, wp.ptMinPosition.y, wp.ptMaxPosition.x, wp.ptMaxPosition.y,
1287 wine_dbgstr_rect(&wp.rcNormalPosition) );
1289 if (!pWnd || pWnd == WND_OTHER_PROCESS || pWnd == WND_DESKTOP) return FALSE;
1291 if( flags & PLACE_MIN ) pWnd->min_pos = wp.ptMinPosition;
1292 if( flags & PLACE_MAX ) pWnd->max_pos = wp.ptMaxPosition;
1293 if( flags & PLACE_RECT) pWnd->normal_rect = wp.rcNormalPosition;
1295 style = pWnd->dwStyle;
1297 WIN_ReleasePtr( pWnd );
1299 if( style & WS_MINIMIZE )
1301 if (flags & PLACE_MIN)
1303 WINPOS_ShowIconTitle( hwnd, FALSE );
1304 SetWindowPos( hwnd, 0, wp.ptMinPosition.x, wp.ptMinPosition.y, 0, 0,
1305 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1308 else if( style & WS_MAXIMIZE )
1310 if (flags & PLACE_MAX)
1311 SetWindowPos( hwnd, 0, wp.ptMaxPosition.x, wp.ptMaxPosition.y, 0, 0,
1312 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1314 else if( flags & PLACE_RECT )
1315 SetWindowPos( hwnd, 0, wp.rcNormalPosition.left, wp.rcNormalPosition.top,
1316 wp.rcNormalPosition.right - wp.rcNormalPosition.left,
1317 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top,
1318 SWP_NOZORDER | SWP_NOACTIVATE );
1320 ShowWindow( hwnd, wndpl->showCmd );
1322 if (IsIconic( hwnd ))
1324 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE) WINPOS_ShowIconTitle( hwnd, TRUE );
1326 /* SDK: ...valid only the next time... */
1327 if( wndpl->flags & WPF_RESTORETOMAXIMIZED )
1329 pWnd = WIN_GetPtr( hwnd );
1330 if (pWnd && pWnd != WND_OTHER_PROCESS)
1332 pWnd->flags |= WIN_RESTORE_MAX;
1333 WIN_ReleasePtr( pWnd );
1337 return TRUE;
1341 /***********************************************************************
1342 * SetWindowPlacement (USER32.@)
1344 * Win95:
1345 * Fails if wndpl->length of Win95 (!) apps is invalid.
1347 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
1349 UINT flags = PLACE_MAX | PLACE_RECT;
1350 if (!wpl) return FALSE;
1351 if (wpl->flags & WPF_SETMINPOSITION) flags |= PLACE_MIN;
1352 return WINPOS_SetPlacement( hwnd, wpl, flags );
1356 /***********************************************************************
1357 * AnimateWindow (USER32.@)
1358 * Shows/Hides a window with an animation
1359 * NO ANIMATION YET
1361 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
1363 FIXME("partial stub\n");
1365 /* If trying to show/hide and it's already *
1366 * shown/hidden or invalid window, fail with *
1367 * invalid parameter */
1368 if(!IsWindow(hwnd) ||
1369 (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
1370 (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
1372 SetLastError(ERROR_INVALID_PARAMETER);
1373 return FALSE;
1376 ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1378 return TRUE;
1381 /***********************************************************************
1382 * SetInternalWindowPos (USER32.@)
1384 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1385 LPRECT rect, LPPOINT pt )
1387 WINDOWPLACEMENT wndpl;
1388 UINT flags;
1390 wndpl.length = sizeof(wndpl);
1391 wndpl.showCmd = showCmd;
1392 wndpl.flags = flags = 0;
1394 if( pt )
1396 flags |= PLACE_MIN;
1397 wndpl.flags |= WPF_SETMINPOSITION;
1398 wndpl.ptMinPosition = *pt;
1400 if( rect )
1402 flags |= PLACE_RECT;
1403 wndpl.rcNormalPosition = *rect;
1405 WINPOS_SetPlacement( hwnd, &wndpl, flags );
1409 /*******************************************************************
1410 * can_activate_window
1412 * Check if we can activate the specified window.
1414 static BOOL can_activate_window( HWND hwnd )
1416 LONG style;
1418 if (!hwnd) return FALSE;
1419 style = GetWindowLongW( hwnd, GWL_STYLE );
1420 if (!(style & WS_VISIBLE)) return FALSE;
1421 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1422 return !(style & WS_DISABLED);
1426 /*******************************************************************
1427 * WINPOS_ActivateOtherWindow
1429 * Activates window other than pWnd.
1431 void WINPOS_ActivateOtherWindow(HWND hwnd)
1433 HWND hwndTo, fg;
1435 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1437 hwndTo = GetAncestor( hwndTo, GA_ROOT );
1438 if (can_activate_window( hwndTo )) goto done;
1441 hwndTo = hwnd;
1442 for (;;)
1444 if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1445 if (can_activate_window( hwndTo )) break;
1448 done:
1449 fg = GetForegroundWindow();
1450 TRACE("win = %p fg = %p\n", hwndTo, fg);
1451 if (!fg || (hwnd == fg))
1453 if (SetForegroundWindow( hwndTo )) return;
1455 if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1459 /***********************************************************************
1460 * WINPOS_HandleWindowPosChanging
1462 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1464 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1466 POINT minTrack, maxTrack;
1467 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1469 if (winpos->flags & SWP_NOSIZE) return 0;
1470 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1472 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1473 if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1474 if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1475 if (!(style & WS_MINIMIZE))
1477 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1478 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1481 return 0;
1485 /***********************************************************************
1486 * dump_winpos_flags
1488 static void dump_winpos_flags(UINT flags)
1490 static const DWORD dumped_flags = (SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOREDRAW |
1491 SWP_NOACTIVATE | SWP_FRAMECHANGED | SWP_SHOWWINDOW |
1492 SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOOWNERZORDER |
1493 SWP_NOSENDCHANGING | SWP_DEFERERASE | SWP_ASYNCWINDOWPOS |
1494 SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_STATECHANGED);
1495 TRACE("flags:");
1496 if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1497 if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1498 if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1499 if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1500 if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1501 if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1502 if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1503 if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1504 if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1505 if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1506 if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1507 if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1508 if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1509 if(flags & SWP_NOCLIENTSIZE) TRACE(" SWP_NOCLIENTSIZE");
1510 if(flags & SWP_NOCLIENTMOVE) TRACE(" SWP_NOCLIENTMOVE");
1511 if(flags & SWP_STATECHANGED) TRACE(" SWP_STATECHANGED");
1513 if(flags & ~dumped_flags) TRACE(" %08x", flags & ~dumped_flags);
1514 TRACE("\n");
1517 /***********************************************************************
1518 * SWP_DoWinPosChanging
1520 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
1522 WND *wndPtr;
1524 /* Send WM_WINDOWPOSCHANGING message */
1526 if (!(pWinpos->flags & SWP_NOSENDCHANGING))
1527 SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
1529 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) ||
1530 wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
1532 /* Calculate new position and size */
1534 *pNewWindowRect = wndPtr->rectWindow;
1535 *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
1536 : wndPtr->rectClient;
1538 if (!(pWinpos->flags & SWP_NOSIZE))
1540 pNewWindowRect->right = pNewWindowRect->left + pWinpos->cx;
1541 pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
1543 if (!(pWinpos->flags & SWP_NOMOVE))
1545 pNewWindowRect->left = pWinpos->x;
1546 pNewWindowRect->top = pWinpos->y;
1547 pNewWindowRect->right += pWinpos->x - wndPtr->rectWindow.left;
1548 pNewWindowRect->bottom += pWinpos->y - wndPtr->rectWindow.top;
1550 OffsetRect( pNewClientRect, pWinpos->x - wndPtr->rectWindow.left,
1551 pWinpos->y - wndPtr->rectWindow.top );
1553 pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
1555 TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
1556 pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
1557 pWinpos->cx, pWinpos->cy, pWinpos->flags );
1558 TRACE( "current %s style %08x new %s\n",
1559 wine_dbgstr_rect( &wndPtr->rectWindow ), wndPtr->dwStyle,
1560 wine_dbgstr_rect( pNewWindowRect ));
1562 WIN_ReleasePtr( wndPtr );
1563 return TRUE;
1566 /***********************************************************************
1567 * get_valid_rects
1569 * Compute the valid rects from the old and new client rect and WVR_* flags.
1570 * Helper for WM_NCCALCSIZE handling.
1572 static inline void get_valid_rects( const RECT *old_client, const RECT *new_client, UINT flags,
1573 RECT *valid )
1575 int cx, cy;
1577 if (flags & WVR_REDRAW)
1579 SetRectEmpty( &valid[0] );
1580 SetRectEmpty( &valid[1] );
1581 return;
1584 if (flags & WVR_VALIDRECTS)
1586 if (!IntersectRect( &valid[0], &valid[0], new_client ) ||
1587 !IntersectRect( &valid[1], &valid[1], old_client ))
1589 SetRectEmpty( &valid[0] );
1590 SetRectEmpty( &valid[1] );
1591 return;
1593 flags = WVR_ALIGNLEFT | WVR_ALIGNTOP;
1595 else
1597 valid[0] = *new_client;
1598 valid[1] = *old_client;
1601 /* make sure the rectangles have the same size */
1602 cx = min( valid[0].right - valid[0].left, valid[1].right - valid[1].left );
1603 cy = min( valid[0].bottom - valid[0].top, valid[1].bottom - valid[1].top );
1605 if (flags & WVR_ALIGNBOTTOM)
1607 valid[0].top = valid[0].bottom - cy;
1608 valid[1].top = valid[1].bottom - cy;
1610 else
1612 valid[0].bottom = valid[0].top + cy;
1613 valid[1].bottom = valid[1].top + cy;
1615 if (flags & WVR_ALIGNRIGHT)
1617 valid[0].left = valid[0].right - cx;
1618 valid[1].left = valid[1].right - cx;
1620 else
1622 valid[0].right = valid[0].left + cx;
1623 valid[1].right = valid[1].left + cx;
1628 /***********************************************************************
1629 * SWP_DoOwnedPopups
1631 * fix Z order taking into account owned popups -
1632 * basically we need to maintain them above the window that owns them
1634 * FIXME: hide/show owned popups when owner visibility changes.
1636 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
1638 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1639 HWND owner, *list = NULL;
1640 unsigned int i;
1642 TRACE("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
1644 if ((style & WS_POPUP) && (owner = GetWindow( hwnd, GW_OWNER )))
1646 /* make sure this popup stays above the owner */
1648 if (hwndInsertAfter != HWND_TOP && hwndInsertAfter != HWND_TOPMOST)
1650 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return hwndInsertAfter;
1652 for (i = 0; list[i]; i++)
1654 if (list[i] == owner)
1656 if (i > 0) hwndInsertAfter = list[i-1];
1657 else hwndInsertAfter = HWND_TOP;
1658 break;
1661 if (hwndInsertAfter == HWND_NOTOPMOST)
1663 if (!(GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST)) break;
1665 else if (list[i] == hwndInsertAfter) break;
1669 else if (style & WS_CHILD) return hwndInsertAfter;
1671 if (hwndInsertAfter == HWND_BOTTOM) goto done;
1672 if (!list && !(list = WIN_ListChildren( GetDesktopWindow() ))) goto done;
1674 i = 0;
1675 if (hwndInsertAfter == HWND_TOP || hwndInsertAfter == HWND_NOTOPMOST)
1677 if (hwndInsertAfter == HWND_NOTOPMOST || !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_TOPMOST))
1679 /* skip all the topmost windows */
1680 while (list[i] && (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST)) i++;
1683 else if (hwndInsertAfter != HWND_TOPMOST)
1685 /* skip windows that are already placed correctly */
1686 for (i = 0; list[i]; i++)
1688 if (list[i] == hwndInsertAfter) break;
1689 if (list[i] == hwnd) goto done; /* nothing to do if window is moving backwards in z-order */
1693 for ( ; list[i]; i++)
1695 if (list[i] == hwnd) break;
1696 if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_POPUP)) continue;
1697 if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
1698 TRACE( "moving %p owned by %p after %p\n", list[i], hwnd, hwndInsertAfter );
1699 SetWindowPos( list[i], hwndInsertAfter, 0, 0, 0, 0,
1700 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING | SWP_DEFERERASE );
1701 hwndInsertAfter = list[i];
1704 done:
1705 HeapFree( GetProcessHeap(), 0, list );
1706 return hwndInsertAfter;
1709 /***********************************************************************
1710 * SWP_DoNCCalcSize
1712 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect,
1713 RECT *validRects )
1715 UINT wvrFlags = 0;
1716 WND *wndPtr;
1718 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
1720 /* Send WM_NCCALCSIZE message to get new client area */
1721 if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
1723 NCCALCSIZE_PARAMS params;
1724 WINDOWPOS winposCopy;
1726 params.rgrc[0] = *pNewWindowRect;
1727 params.rgrc[1] = wndPtr->rectWindow;
1728 params.rgrc[2] = wndPtr->rectClient;
1729 params.lppos = &winposCopy;
1730 winposCopy = *pWinpos;
1731 WIN_ReleasePtr( wndPtr );
1733 wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
1735 *pNewClientRect = params.rgrc[0];
1737 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
1739 TRACE( "hwnd %p old win %s old client %s new win %s new client %s\n", pWinpos->hwnd,
1740 wine_dbgstr_rect(&wndPtr->rectWindow), wine_dbgstr_rect(&wndPtr->rectClient),
1741 wine_dbgstr_rect(pNewWindowRect), wine_dbgstr_rect(pNewClientRect) );
1743 if( pNewClientRect->left != wndPtr->rectClient.left ||
1744 pNewClientRect->top != wndPtr->rectClient.top )
1745 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1747 if( (pNewClientRect->right - pNewClientRect->left !=
1748 wndPtr->rectClient.right - wndPtr->rectClient.left))
1749 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1750 else
1751 wvrFlags &= ~WVR_HREDRAW;
1753 if (pNewClientRect->bottom - pNewClientRect->top !=
1754 wndPtr->rectClient.bottom - wndPtr->rectClient.top)
1755 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1756 else
1757 wvrFlags &= ~WVR_VREDRAW;
1759 validRects[0] = params.rgrc[1];
1760 validRects[1] = params.rgrc[2];
1762 else
1764 if (!(pWinpos->flags & SWP_NOMOVE) &&
1765 (pNewClientRect->left != wndPtr->rectClient.left ||
1766 pNewClientRect->top != wndPtr->rectClient.top))
1767 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1770 if (pWinpos->flags & (SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
1772 SetRectEmpty( &validRects[0] );
1773 SetRectEmpty( &validRects[1] );
1775 else get_valid_rects( &wndPtr->rectClient, pNewClientRect, wvrFlags, validRects );
1777 WIN_ReleasePtr( wndPtr );
1778 return wvrFlags;
1781 /* fix redundant flags and values in the WINDOWPOS structure */
1782 static BOOL fixup_flags( WINDOWPOS *winpos )
1784 HWND parent;
1785 WND *wndPtr = WIN_GetPtr( winpos->hwnd );
1786 BOOL ret = TRUE;
1788 if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
1790 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1791 return FALSE;
1793 winpos->hwnd = wndPtr->obj.handle; /* make it a full handle */
1795 /* Finally make sure that all coordinates are valid */
1796 if (winpos->x < -32768) winpos->x = -32768;
1797 else if (winpos->x > 32767) winpos->x = 32767;
1798 if (winpos->y < -32768) winpos->y = -32768;
1799 else if (winpos->y > 32767) winpos->y = 32767;
1801 if (winpos->cx < 0) winpos->cx = 0;
1802 else if (winpos->cx > 32767) winpos->cx = 32767;
1803 if (winpos->cy < 0) winpos->cy = 0;
1804 else if (winpos->cy > 32767) winpos->cy = 32767;
1806 parent = GetAncestor( winpos->hwnd, GA_PARENT );
1807 if (!IsWindowVisible( parent )) winpos->flags |= SWP_NOREDRAW;
1809 if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
1810 else
1812 winpos->flags &= ~SWP_HIDEWINDOW;
1813 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
1816 if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
1817 (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
1818 winpos->flags |= SWP_NOSIZE; /* Already the right size */
1820 if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
1821 winpos->flags |= SWP_NOMOVE; /* Already the right position */
1823 if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
1825 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)) && /* Bring to the top when activating */
1826 (winpos->flags & SWP_NOZORDER ||
1827 (winpos->hwndInsertAfter != HWND_TOPMOST && winpos->hwndInsertAfter != HWND_NOTOPMOST)))
1829 winpos->flags &= ~SWP_NOZORDER;
1830 winpos->hwndInsertAfter = HWND_TOP;
1834 /* Check hwndInsertAfter */
1835 if (winpos->flags & SWP_NOZORDER) goto done;
1837 /* fix sign extension */
1838 if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
1839 else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
1841 /* hwndInsertAfter must be a sibling of the window */
1842 if (winpos->hwndInsertAfter == HWND_TOP)
1844 if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1845 winpos->flags |= SWP_NOZORDER;
1847 else if (winpos->hwndInsertAfter == HWND_BOTTOM)
1849 if (!(wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
1850 winpos->flags |= SWP_NOZORDER;
1852 else if (winpos->hwndInsertAfter == HWND_TOPMOST)
1854 if ((wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1855 winpos->flags |= SWP_NOZORDER;
1857 else if (winpos->hwndInsertAfter == HWND_NOTOPMOST)
1859 if (!(wndPtr->dwExStyle & WS_EX_TOPMOST))
1860 winpos->flags |= SWP_NOZORDER;
1862 else
1864 if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != parent) ret = FALSE;
1865 else
1867 /* don't need to change the Zorder of hwnd if it's already inserted
1868 * after hwndInsertAfter or when inserting hwnd after itself.
1870 if ((winpos->hwnd == winpos->hwndInsertAfter) ||
1871 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
1872 winpos->flags |= SWP_NOZORDER;
1875 done:
1876 WIN_ReleasePtr( wndPtr );
1877 return ret;
1881 /***********************************************************************
1882 * set_window_pos
1884 * Backend implementation of SetWindowPos.
1886 BOOL set_window_pos( HWND hwnd, HWND insert_after, UINT swp_flags,
1887 const RECT *window_rect, const RECT *client_rect, const RECT *valid_rects )
1889 WND *win;
1890 BOOL ret;
1891 RECT visible_rect, old_window_rect;
1893 visible_rect = *window_rect;
1894 USER_Driver->pWindowPosChanging( hwnd, insert_after, swp_flags,
1895 window_rect, client_rect, &visible_rect );
1897 if (!(win = WIN_GetPtr( hwnd ))) return FALSE;
1898 if (win == WND_DESKTOP || win == WND_OTHER_PROCESS) return FALSE;
1900 old_window_rect = win->rectWindow;
1901 SERVER_START_REQ( set_window_pos )
1903 req->handle = wine_server_user_handle( hwnd );
1904 req->previous = wine_server_user_handle( insert_after );
1905 req->flags = swp_flags;
1906 req->window.left = window_rect->left;
1907 req->window.top = window_rect->top;
1908 req->window.right = window_rect->right;
1909 req->window.bottom = window_rect->bottom;
1910 req->client.left = client_rect->left;
1911 req->client.top = client_rect->top;
1912 req->client.right = client_rect->right;
1913 req->client.bottom = client_rect->bottom;
1914 if (memcmp( window_rect, &visible_rect, sizeof(RECT) ) || !IsRectEmpty( &valid_rects[0] ))
1916 wine_server_add_data( req, &visible_rect, sizeof(visible_rect) );
1917 if (!IsRectEmpty( &valid_rects[0] ))
1918 wine_server_add_data( req, valid_rects, 2 * sizeof(*valid_rects) );
1920 if ((ret = !wine_server_call( req )))
1922 win->dwStyle = reply->new_style;
1923 win->dwExStyle = reply->new_ex_style;
1924 win->rectWindow = *window_rect;
1925 win->rectClient = *client_rect;
1928 SERVER_END_REQ;
1929 WIN_ReleasePtr( win );
1931 if (ret)
1933 if (((swp_flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) ||
1934 (swp_flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW | SWP_STATECHANGED)))
1935 invalidate_dce( hwnd, &old_window_rect );
1937 USER_Driver->pWindowPosChanged( hwnd, insert_after, swp_flags, window_rect,
1938 client_rect, &visible_rect, valid_rects );
1940 return ret;
1944 /***********************************************************************
1945 * USER_SetWindowPos
1947 * User32 internal function
1949 BOOL USER_SetWindowPos( WINDOWPOS * winpos )
1951 RECT newWindowRect, newClientRect, valid_rects[2];
1952 UINT orig_flags;
1954 orig_flags = winpos->flags;
1956 /* First make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
1957 if (!(winpos->flags & SWP_NOMOVE))
1959 if (winpos->x < -32768) winpos->x = -32768;
1960 else if (winpos->x > 32767) winpos->x = 32767;
1961 if (winpos->y < -32768) winpos->y = -32768;
1962 else if (winpos->y > 32767) winpos->y = 32767;
1964 if (!(winpos->flags & SWP_NOSIZE))
1966 if (winpos->cx < 0) winpos->cx = 0;
1967 else if (winpos->cx > 32767) winpos->cx = 32767;
1968 if (winpos->cy < 0) winpos->cy = 0;
1969 else if (winpos->cy > 32767) winpos->cy = 32767;
1972 if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
1974 /* Fix redundant flags */
1975 if (!fixup_flags( winpos )) return FALSE;
1977 if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
1979 if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
1980 winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
1983 /* Common operations */
1985 SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect, valid_rects );
1987 if (!set_window_pos( winpos->hwnd, winpos->hwndInsertAfter, winpos->flags,
1988 &newWindowRect, &newClientRect, valid_rects ))
1989 return FALSE;
1991 /* erase parent when hiding or resizing child */
1992 if (!(orig_flags & SWP_DEFERERASE) &&
1993 ((orig_flags & SWP_HIDEWINDOW) ||
1994 (!(orig_flags & SWP_SHOWWINDOW) &&
1995 (winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOGEOMETRYCHANGE)))
1997 HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
1998 if (!parent || parent == GetDesktopWindow()) parent = winpos->hwnd;
1999 erase_now( parent, 0 );
2002 if( winpos->flags & SWP_HIDEWINDOW )
2003 HideCaret(winpos->hwnd);
2004 else if (winpos->flags & SWP_SHOWWINDOW)
2005 ShowCaret(winpos->hwnd);
2007 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)))
2009 /* child windows get WM_CHILDACTIVATE message */
2010 if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
2011 SendMessageW( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
2012 else
2013 SetForegroundWindow( winpos->hwnd );
2016 /* And last, send the WM_WINDOWPOSCHANGED message */
2018 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
2020 if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
2022 /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
2023 and always contains final window position.
2025 winpos->x = newWindowRect.left;
2026 winpos->y = newWindowRect.top;
2027 winpos->cx = newWindowRect.right - newWindowRect.left;
2028 winpos->cy = newWindowRect.bottom - newWindowRect.top;
2029 SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
2031 return TRUE;
2034 /***********************************************************************
2035 * SetWindowPos (USER32.@)
2037 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
2038 INT x, INT y, INT cx, INT cy, UINT flags )
2040 WINDOWPOS winpos;
2042 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2043 hwnd, hwndInsertAfter, x, y, cx, cy, flags);
2044 if(TRACE_ON(win)) dump_winpos_flags(flags);
2046 if (is_broadcast(hwnd))
2048 SetLastError( ERROR_INVALID_PARAMETER );
2049 return FALSE;
2052 winpos.hwnd = WIN_GetFullHandle(hwnd);
2053 winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
2054 winpos.x = x;
2055 winpos.y = y;
2056 winpos.cx = cx;
2057 winpos.cy = cy;
2058 winpos.flags = flags;
2060 if (WIN_IsCurrentThread( hwnd ))
2061 return USER_SetWindowPos(&winpos);
2063 return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
2067 /***********************************************************************
2068 * BeginDeferWindowPos (USER32.@)
2070 HDWP WINAPI BeginDeferWindowPos( INT count )
2072 HDWP handle = 0;
2073 DWP *pDWP;
2075 TRACE("%d\n", count);
2077 if (count < 0)
2079 SetLastError(ERROR_INVALID_PARAMETER);
2080 return 0;
2082 /* Windows allows zero count, in which case it allocates context for 8 moves */
2083 if (count == 0) count = 8;
2085 if (!(pDWP = HeapAlloc( GetProcessHeap(), 0, sizeof(DWP)))) return 0;
2087 pDWP->actualCount = 0;
2088 pDWP->suggestedCount = count;
2089 pDWP->hwndParent = 0;
2091 if (!(pDWP->winPos = HeapAlloc( GetProcessHeap(), 0, count * sizeof(WINDOWPOS) )) ||
2092 !(handle = alloc_user_handle( &pDWP->obj, USER_DWP )))
2094 HeapFree( GetProcessHeap(), 0, pDWP->winPos );
2095 HeapFree( GetProcessHeap(), 0, pDWP );
2098 TRACE("returning hdwp %p\n", handle);
2099 return handle;
2103 /***********************************************************************
2104 * DeferWindowPos (USER32.@)
2106 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
2107 INT x, INT y, INT cx, INT cy,
2108 UINT flags )
2110 DWP *pDWP;
2111 int i;
2112 HDWP retvalue = hdwp;
2114 TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2115 hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
2117 hwnd = WIN_GetFullHandle( hwnd );
2118 if (is_desktop_window( hwnd )) return 0;
2120 if (!(pDWP = get_user_handle_ptr( hdwp, USER_DWP ))) return 0;
2121 if (pDWP == OBJ_OTHER_PROCESS)
2123 FIXME( "other process handle %p?\n", hdwp );
2124 return 0;
2127 for (i = 0; i < pDWP->actualCount; i++)
2129 if (pDWP->winPos[i].hwnd == hwnd)
2131 /* Merge with the other changes */
2132 if (!(flags & SWP_NOZORDER))
2134 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
2136 if (!(flags & SWP_NOMOVE))
2138 pDWP->winPos[i].x = x;
2139 pDWP->winPos[i].y = y;
2141 if (!(flags & SWP_NOSIZE))
2143 pDWP->winPos[i].cx = cx;
2144 pDWP->winPos[i].cy = cy;
2146 pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
2147 SWP_NOZORDER | SWP_NOREDRAW |
2148 SWP_NOACTIVATE | SWP_NOCOPYBITS|
2149 SWP_NOOWNERZORDER);
2150 pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
2151 SWP_FRAMECHANGED);
2152 goto END;
2155 if (pDWP->actualCount >= pDWP->suggestedCount)
2157 WINDOWPOS *newpos = HeapReAlloc( GetProcessHeap(), 0, pDWP->winPos,
2158 pDWP->suggestedCount * 2 * sizeof(WINDOWPOS) );
2159 if (!newpos)
2161 retvalue = 0;
2162 goto END;
2164 pDWP->suggestedCount *= 2;
2165 pDWP->winPos = newpos;
2167 pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
2168 pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
2169 pDWP->winPos[pDWP->actualCount].x = x;
2170 pDWP->winPos[pDWP->actualCount].y = y;
2171 pDWP->winPos[pDWP->actualCount].cx = cx;
2172 pDWP->winPos[pDWP->actualCount].cy = cy;
2173 pDWP->winPos[pDWP->actualCount].flags = flags;
2174 pDWP->actualCount++;
2175 END:
2176 release_user_handle_ptr( pDWP );
2177 return retvalue;
2181 /***********************************************************************
2182 * EndDeferWindowPos (USER32.@)
2184 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
2186 DWP *pDWP;
2187 WINDOWPOS *winpos;
2188 BOOL res = TRUE;
2189 int i;
2191 TRACE("%p\n", hdwp);
2193 if (!(pDWP = free_user_handle( hdwp, USER_DWP ))) return FALSE;
2194 if (pDWP == OBJ_OTHER_PROCESS)
2196 FIXME( "other process handle %p?\n", hdwp );
2197 return FALSE;
2200 for (i = 0, winpos = pDWP->winPos; res && i < pDWP->actualCount; i++, winpos++)
2202 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2203 winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
2204 winpos->cx, winpos->cy, winpos->flags);
2206 if (WIN_IsCurrentThread( winpos->hwnd ))
2207 res = USER_SetWindowPos( winpos );
2208 else
2209 res = SendMessageW( winpos->hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)winpos );
2211 HeapFree( GetProcessHeap(), 0, pDWP->winPos );
2212 HeapFree( GetProcessHeap(), 0, pDWP );
2213 return res;
2217 /***********************************************************************
2218 * ArrangeIconicWindows (USER32.@)
2220 UINT WINAPI ArrangeIconicWindows( HWND parent )
2222 RECT rectParent;
2223 HWND hwndChild;
2224 INT x, y, xspacing, yspacing;
2226 GetClientRect( parent, &rectParent );
2227 x = rectParent.left;
2228 y = rectParent.bottom;
2229 xspacing = GetSystemMetrics(SM_CXICONSPACING);
2230 yspacing = GetSystemMetrics(SM_CYICONSPACING);
2232 hwndChild = GetWindow( parent, GW_CHILD );
2233 while (hwndChild)
2235 if( IsIconic( hwndChild ) )
2237 WINPOS_ShowIconTitle( hwndChild, FALSE );
2239 SetWindowPos( hwndChild, 0, x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
2240 y - yspacing - GetSystemMetrics(SM_CYICON)/2, 0, 0,
2241 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
2242 if( IsWindow(hwndChild) )
2243 WINPOS_ShowIconTitle(hwndChild , TRUE );
2245 if (x <= rectParent.right - xspacing) x += xspacing;
2246 else
2248 x = rectParent.left;
2249 y -= yspacing;
2252 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
2254 return yspacing;
2258 /***********************************************************************
2259 * draw_moving_frame
2261 * Draw the frame used when moving or resizing window.
2263 static void draw_moving_frame( HDC hdc, RECT *rect, BOOL thickframe )
2265 if (thickframe)
2267 const int width = GetSystemMetrics(SM_CXFRAME);
2268 const int height = GetSystemMetrics(SM_CYFRAME);
2270 HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
2271 PatBlt( hdc, rect->left, rect->top,
2272 rect->right - rect->left - width, height, PATINVERT );
2273 PatBlt( hdc, rect->left, rect->top + height, width,
2274 rect->bottom - rect->top - height, PATINVERT );
2275 PatBlt( hdc, rect->left + width, rect->bottom - 1,
2276 rect->right - rect->left - width, -height, PATINVERT );
2277 PatBlt( hdc, rect->right - 1, rect->top, -width,
2278 rect->bottom - rect->top - height, PATINVERT );
2279 SelectObject( hdc, hbrush );
2281 else DrawFocusRect( hdc, rect );
2285 /***********************************************************************
2286 * start_size_move
2288 * Initialization of a move or resize, when initiated from a menu choice.
2289 * Return hit test code for caption or sizing border.
2291 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
2293 LONG hittest = 0;
2294 POINT pt;
2295 MSG msg;
2296 RECT rectWindow;
2298 GetWindowRect( hwnd, &rectWindow );
2300 if ((wParam & 0xfff0) == SC_MOVE)
2302 /* Move pointer at the center of the caption */
2303 RECT rect = rectWindow;
2304 /* Note: to be exactly centered we should take the different types
2305 * of border into account, but it shouldn't make more than a few pixels
2306 * of difference so let's not bother with that */
2307 rect.top += GetSystemMetrics(SM_CYBORDER);
2308 if (style & WS_SYSMENU)
2309 rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
2310 if (style & WS_MINIMIZEBOX)
2311 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
2312 if (style & WS_MAXIMIZEBOX)
2313 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
2314 pt.x = (rect.right + rect.left) / 2;
2315 pt.y = rect.top + GetSystemMetrics(SM_CYSIZE)/2;
2316 hittest = HTCAPTION;
2317 *capturePoint = pt;
2319 else /* SC_SIZE */
2321 SetCursor( LoadCursorW( 0, (LPWSTR)IDC_SIZEALL ) );
2322 pt.x = pt.y = 0;
2323 while(!hittest)
2325 if (!GetMessageW( &msg, 0, 0, 0 )) return 0;
2326 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2328 switch(msg.message)
2330 case WM_MOUSEMOVE:
2331 pt.x = min( max( msg.pt.x, rectWindow.left ), rectWindow.right - 1 );
2332 pt.y = min( max( msg.pt.y, rectWindow.top ), rectWindow.bottom - 1 );
2333 hittest = SendMessageW( hwnd, WM_NCHITTEST, 0, MAKELONG( pt.x, pt.y ) );
2334 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT)) hittest = 0;
2335 break;
2337 case WM_LBUTTONUP:
2338 return 0;
2340 case WM_KEYDOWN:
2341 switch(msg.wParam)
2343 case VK_UP:
2344 hittest = HTTOP;
2345 pt.x =(rectWindow.left+rectWindow.right)/2;
2346 pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
2347 break;
2348 case VK_DOWN:
2349 hittest = HTBOTTOM;
2350 pt.x =(rectWindow.left+rectWindow.right)/2;
2351 pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
2352 break;
2353 case VK_LEFT:
2354 hittest = HTLEFT;
2355 pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
2356 pt.y =(rectWindow.top+rectWindow.bottom)/2;
2357 break;
2358 case VK_RIGHT:
2359 hittest = HTRIGHT;
2360 pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
2361 pt.y =(rectWindow.top+rectWindow.bottom)/2;
2362 break;
2363 case VK_RETURN:
2364 case VK_ESCAPE:
2365 return 0;
2367 break;
2368 default:
2369 TranslateMessage( &msg );
2370 DispatchMessageW( &msg );
2371 break;
2374 *capturePoint = pt;
2376 SetCursorPos( pt.x, pt.y );
2377 SendMessageW( hwnd, WM_SETCURSOR, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
2378 return hittest;
2382 /***********************************************************************
2383 * WINPOS_SysCommandSizeMove
2385 * Perform SC_MOVE and SC_SIZE commands.
2387 void WINPOS_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
2389 MSG msg;
2390 RECT sizingRect, mouseRect, origRect;
2391 HDC hdc;
2392 HWND parent;
2393 LONG hittest = (LONG)(wParam & 0x0f);
2394 WPARAM syscommand = wParam & 0xfff0;
2395 HCURSOR hDragCursor = 0, hOldCursor = 0;
2396 POINT minTrack, maxTrack;
2397 POINT capturePoint, pt;
2398 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
2399 BOOL thickframe = HAS_THICKFRAME( style );
2400 BOOL iconic = style & WS_MINIMIZE;
2401 BOOL moved = FALSE;
2402 DWORD dwPoint = GetMessagePos ();
2403 BOOL DragFullWindows = TRUE;
2405 if (IsZoomed(hwnd) || !IsWindowVisible(hwnd)) return;
2407 pt.x = (short)LOWORD(dwPoint);
2408 pt.y = (short)HIWORD(dwPoint);
2409 capturePoint = pt;
2411 TRACE("hwnd %p command %04lx, hittest %d, pos %d,%d\n",
2412 hwnd, syscommand, hittest, pt.x, pt.y);
2414 if (syscommand == SC_MOVE)
2416 if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
2417 if (!hittest) return;
2419 else /* SC_SIZE */
2421 if ( hittest && (syscommand != SC_MOUSEMENU) )
2422 hittest += (HTLEFT - WMSZ_LEFT);
2423 else
2425 set_capture_window( hwnd, GUI_INMOVESIZE, NULL );
2426 hittest = start_size_move( hwnd, wParam, &capturePoint, style );
2427 if (!hittest)
2429 set_capture_window( 0, GUI_INMOVESIZE, NULL );
2430 return;
2435 /* Get min/max info */
2437 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
2438 GetWindowRect( hwnd, &sizingRect );
2439 if (style & WS_CHILD)
2441 parent = GetParent(hwnd);
2442 /* make sizing rect relative to parent */
2443 MapWindowPoints( 0, parent, (POINT*)&sizingRect, 2 );
2444 GetClientRect( parent, &mouseRect );
2446 else
2448 parent = 0;
2449 GetClientRect( GetDesktopWindow(), &mouseRect );
2450 mouseRect.left = GetSystemMetrics( SM_XVIRTUALSCREEN );
2451 mouseRect.top = GetSystemMetrics( SM_YVIRTUALSCREEN );
2452 mouseRect.right = mouseRect.left + GetSystemMetrics( SM_CXVIRTUALSCREEN );
2453 mouseRect.bottom = mouseRect.top + GetSystemMetrics( SM_CYVIRTUALSCREEN );
2455 origRect = sizingRect;
2457 if (ON_LEFT_BORDER(hittest))
2459 mouseRect.left = max( mouseRect.left, sizingRect.right-maxTrack.x );
2460 mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
2462 else if (ON_RIGHT_BORDER(hittest))
2464 mouseRect.left = max( mouseRect.left, sizingRect.left+minTrack.x );
2465 mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
2467 if (ON_TOP_BORDER(hittest))
2469 mouseRect.top = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
2470 mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
2472 else if (ON_BOTTOM_BORDER(hittest))
2474 mouseRect.top = max( mouseRect.top, sizingRect.top+minTrack.y );
2475 mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
2477 if (parent) MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
2479 /* Retrieve a default cache DC (without using the window style) */
2480 hdc = GetDCEx( parent, 0, DCX_CACHE );
2482 if( iconic ) /* create a cursor for dragging */
2484 hDragCursor = (HCURSOR)GetClassLongPtrW( hwnd, GCLP_HICON);
2485 if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageW( hwnd, WM_QUERYDRAGICON, 0, 0L);
2486 if( !hDragCursor ) iconic = FALSE;
2489 /* we only allow disabling the full window drag for child windows */
2490 if (parent) SystemParametersInfoW( SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0 );
2492 /* repaint the window before moving it around */
2493 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
2495 SendMessageW( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
2496 set_capture_window( hwnd, GUI_INMOVESIZE, NULL );
2498 while(1)
2500 int dx = 0, dy = 0;
2502 if (!GetMessageW( &msg, 0, 0, 0 )) break;
2503 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2505 /* Exit on button-up, Return, or Esc */
2506 if ((msg.message == WM_LBUTTONUP) ||
2507 ((msg.message == WM_KEYDOWN) &&
2508 ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
2510 if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
2512 TranslateMessage( &msg );
2513 DispatchMessageW( &msg );
2514 continue; /* We are not interested in other messages */
2517 pt = msg.pt;
2519 if (msg.message == WM_KEYDOWN) switch(msg.wParam)
2521 case VK_UP: pt.y -= 8; break;
2522 case VK_DOWN: pt.y += 8; break;
2523 case VK_LEFT: pt.x -= 8; break;
2524 case VK_RIGHT: pt.x += 8; break;
2527 pt.x = max( pt.x, mouseRect.left );
2528 pt.x = min( pt.x, mouseRect.right );
2529 pt.y = max( pt.y, mouseRect.top );
2530 pt.y = min( pt.y, mouseRect.bottom );
2532 dx = pt.x - capturePoint.x;
2533 dy = pt.y - capturePoint.y;
2535 if (dx || dy)
2537 if( !moved )
2539 moved = TRUE;
2541 if( iconic ) /* ok, no system popup tracking */
2543 hOldCursor = SetCursor(hDragCursor);
2544 ShowCursor( TRUE );
2545 WINPOS_ShowIconTitle( hwnd, FALSE );
2547 else if(!DragFullWindows)
2548 draw_moving_frame( hdc, &sizingRect, thickframe );
2551 if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
2552 else
2554 RECT newRect = sizingRect;
2555 WPARAM wpSizingHit = 0;
2557 if (hittest == HTCAPTION) OffsetRect( &newRect, dx, dy );
2558 if (ON_LEFT_BORDER(hittest)) newRect.left += dx;
2559 else if (ON_RIGHT_BORDER(hittest)) newRect.right += dx;
2560 if (ON_TOP_BORDER(hittest)) newRect.top += dy;
2561 else if (ON_BOTTOM_BORDER(hittest)) newRect.bottom += dy;
2562 if(!iconic && !DragFullWindows) draw_moving_frame( hdc, &sizingRect, thickframe );
2563 capturePoint = pt;
2565 /* determine the hit location */
2566 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
2567 wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
2568 SendMessageW( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&newRect );
2570 if (!iconic)
2572 if(!DragFullWindows)
2573 draw_moving_frame( hdc, &newRect, thickframe );
2574 else
2575 SetWindowPos( hwnd, 0, newRect.left, newRect.top,
2576 newRect.right - newRect.left,
2577 newRect.bottom - newRect.top,
2578 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2580 sizingRect = newRect;
2585 if( iconic )
2587 if( moved ) /* restore cursors, show icon title later on */
2589 ShowCursor( FALSE );
2590 SetCursor( hOldCursor );
2593 else if (moved && !DragFullWindows)
2595 draw_moving_frame( hdc, &sizingRect, thickframe );
2598 set_capture_window( 0, GUI_INMOVESIZE, NULL );
2599 ReleaseDC( parent, hdc );
2601 if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
2602 moved = FALSE;
2604 SendMessageW( hwnd, WM_EXITSIZEMOVE, 0, 0 );
2605 SendMessageW( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
2607 /* window moved or resized */
2608 if (moved)
2610 /* if the moving/resizing isn't canceled call SetWindowPos
2611 * with the new position or the new size of the window
2613 if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
2615 /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
2616 if(!DragFullWindows || iconic)
2617 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
2618 sizingRect.right - sizingRect.left,
2619 sizingRect.bottom - sizingRect.top,
2620 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2622 else
2623 { /* restore previous size/position */
2624 if(DragFullWindows)
2625 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
2626 origRect.right - origRect.left,
2627 origRect.bottom - origRect.top,
2628 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2632 if (IsIconic(hwnd))
2634 /* Single click brings up the system menu when iconized */
2636 if( !moved )
2638 if(style & WS_SYSMENU )
2639 SendMessageW( hwnd, WM_SYSCOMMAND,
2640 SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
2642 else WINPOS_ShowIconTitle( hwnd, TRUE );