comctl32/tests: Initialize item mask on item insert (Valgrind).
[wine.git] / dlls / user32 / winpos.c
blob03c16c909ec2232b2e94c7c78bf5d179856b82c7
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 while (wndPtr->parent)
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;
463 if (wndPtr && wndPtr != WND_DESKTOP) WIN_ReleasePtr( wndPtr );
467 /* Translate origin to destination window coords */
468 if (hwndTo)
470 if (!(wndPtr = WIN_GetPtr( hwndTo ))) return offset;
471 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
472 if (wndPtr != WND_DESKTOP)
474 if (wndPtr->dwExStyle & WS_EX_LAYOUTRTL)
476 mirror_to = TRUE;
477 offset.x -= wndPtr->rectClient.right - wndPtr->rectClient.left;
479 while (wndPtr->parent)
481 offset.x -= wndPtr->rectClient.left;
482 offset.y -= wndPtr->rectClient.top;
483 hwnd = wndPtr->parent;
484 WIN_ReleasePtr( wndPtr );
485 if (!(wndPtr = WIN_GetPtr( hwnd ))) break;
486 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
487 if (wndPtr == WND_DESKTOP) break;
488 if (wndPtr->flags & WIN_CHILDREN_MOVED)
490 WIN_ReleasePtr( wndPtr );
491 goto other_process;
494 if (wndPtr && wndPtr != WND_DESKTOP) WIN_ReleasePtr( wndPtr );
498 *mirrored = mirror_from ^ mirror_to;
499 if (mirror_from) offset.x = -offset.x;
500 return offset;
502 other_process: /* one of the parents may belong to another process, do it the hard way */
503 offset.x = offset.y = 0;
504 SERVER_START_REQ( get_windows_offset )
506 req->from = wine_server_user_handle( hwndFrom );
507 req->to = wine_server_user_handle( hwndTo );
508 if (!wine_server_call( req ))
510 offset.x = reply->x;
511 offset.y = reply->y;
512 *mirrored = reply->mirror;
515 SERVER_END_REQ;
516 return offset;
519 /* map coordinates of a window region */
520 void map_window_region( HWND from, HWND to, HRGN hrgn )
522 BOOL mirrored;
523 POINT offset = WINPOS_GetWinOffset( from, to, &mirrored );
524 UINT i, size;
525 RGNDATA *data;
526 HRGN new_rgn;
527 RECT *rect;
529 if (!mirrored)
531 OffsetRgn( hrgn, offset.x, offset.y );
532 return;
534 if (!(size = GetRegionData( hrgn, 0, NULL ))) return;
535 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return;
536 GetRegionData( hrgn, size, data );
537 rect = (RECT *)data->Buffer;
538 for (i = 0; i < data->rdh.nCount; i++)
540 int tmp = -(rect[i].left + offset.x);
541 rect[i].left = -(rect[i].right + offset.x);
542 rect[i].right = tmp;
543 rect[i].top += offset.y;
544 rect[i].bottom += offset.y;
546 if ((new_rgn = ExtCreateRegion( NULL, data->rdh.nCount, data )))
548 CombineRgn( hrgn, new_rgn, 0, RGN_COPY );
549 DeleteObject( new_rgn );
551 HeapFree( GetProcessHeap(), 0, data );
555 /*******************************************************************
556 * MapWindowPoints (USER32.@)
558 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
560 BOOL mirrored;
561 POINT offset = WINPOS_GetWinOffset( hwndFrom, hwndTo, &mirrored );
562 UINT i;
564 for (i = 0; i < count; i++)
566 lppt[i].x += offset.x;
567 lppt[i].y += offset.y;
568 if (mirrored) lppt[i].x = -lppt[i].x;
570 if (mirrored && count == 2) /* special case for rectangle */
572 int tmp = lppt[0].x;
573 lppt[0].x = lppt[1].x;
574 lppt[1].x = tmp;
576 return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
580 /***********************************************************************
581 * IsIconic (USER32.@)
583 BOOL WINAPI IsIconic(HWND hWnd)
585 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
589 /***********************************************************************
590 * IsZoomed (USER32.@)
592 BOOL WINAPI IsZoomed(HWND hWnd)
594 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
598 /*******************************************************************
599 * AllowSetForegroundWindow (USER32.@)
601 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
603 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
604 * implemented, then fix this function. */
605 return TRUE;
609 /*******************************************************************
610 * LockSetForegroundWindow (USER32.@)
612 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
614 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
615 * implemented, then fix this function. */
616 return TRUE;
620 /***********************************************************************
621 * BringWindowToTop (USER32.@)
623 BOOL WINAPI BringWindowToTop( HWND hwnd )
625 return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
629 /***********************************************************************
630 * MoveWindow (USER32.@)
632 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
633 BOOL repaint )
635 int flags = SWP_NOZORDER | SWP_NOACTIVATE;
636 if (!repaint) flags |= SWP_NOREDRAW;
637 TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
638 return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
642 /***********************************************************************
643 * WINPOS_RedrawIconTitle
645 BOOL WINPOS_RedrawIconTitle( HWND hWnd )
647 HWND icon_title = 0;
648 WND *win = WIN_GetPtr( hWnd );
650 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
652 icon_title = win->icon_title;
653 WIN_ReleasePtr( win );
655 if (!icon_title) return FALSE;
656 SendMessageW( icon_title, WM_SHOWWINDOW, TRUE, 0 );
657 InvalidateRect( icon_title, NULL, TRUE );
658 return TRUE;
661 /***********************************************************************
662 * WINPOS_ShowIconTitle
664 static BOOL WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
666 if (!GetPropA( hwnd, "__wine_x11_managed" ))
668 WND *win = WIN_GetPtr( hwnd );
669 HWND title = 0;
671 TRACE("%p %i\n", hwnd, (bShow != 0) );
673 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP) return FALSE;
674 title = win->icon_title;
675 WIN_ReleasePtr( win );
677 if( bShow )
679 if (!title)
681 title = ICONTITLE_Create( hwnd );
682 if (!(win = WIN_GetPtr( hwnd )) || win == WND_OTHER_PROCESS)
684 DestroyWindow( title );
685 return FALSE;
687 win->icon_title = title;
688 WIN_ReleasePtr( win );
690 if (!IsWindowVisible(title))
692 SendMessageW( title, WM_SHOWWINDOW, TRUE, 0 );
693 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
694 SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
697 else if (title) ShowWindow( title, SW_HIDE );
699 return FALSE;
702 /*******************************************************************
703 * WINPOS_GetMinMaxInfo
705 * Get the minimized and maximized information for a window.
707 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
708 POINT *minTrack, POINT *maxTrack )
710 MINMAXINFO MinMax;
711 HMONITOR monitor;
712 INT xinc, yinc;
713 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
714 LONG adjustedStyle;
715 LONG exstyle = GetWindowLongW( hwnd, GWL_EXSTYLE );
716 RECT rc;
717 WND *win;
719 /* Compute default values */
721 GetWindowRect(hwnd, &rc);
722 MinMax.ptReserved.x = rc.left;
723 MinMax.ptReserved.y = rc.top;
725 if ((style & WS_CAPTION) == WS_CAPTION)
726 adjustedStyle = style & ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
727 else
728 adjustedStyle = style;
730 GetClientRect(GetAncestor(hwnd,GA_PARENT), &rc);
731 AdjustWindowRectEx(&rc, adjustedStyle, ((style & WS_POPUP) && GetMenu(hwnd)), exstyle);
733 xinc = -rc.left;
734 yinc = -rc.top;
736 MinMax.ptMaxSize.x = rc.right - rc.left;
737 MinMax.ptMaxSize.y = rc.bottom - rc.top;
738 if (style & (WS_DLGFRAME | WS_BORDER))
740 MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
741 MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
743 else
745 MinMax.ptMinTrackSize.x = 2 * xinc;
746 MinMax.ptMinTrackSize.y = 2 * yinc;
748 MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXMAXTRACK);
749 MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYMAXTRACK);
750 MinMax.ptMaxPosition.x = -xinc;
751 MinMax.ptMaxPosition.y = -yinc;
753 if ((win = WIN_GetPtr( hwnd )) && win != WND_DESKTOP && win != WND_OTHER_PROCESS)
755 if (!EMPTYPOINT(win->max_pos)) MinMax.ptMaxPosition = win->max_pos;
756 WIN_ReleasePtr( win );
759 SendMessageW( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
761 /* if the app didn't change the values, adapt them for the current monitor */
763 if ((monitor = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY )))
765 RECT rc_work;
766 MONITORINFO mon_info;
768 mon_info.cbSize = sizeof(mon_info);
769 GetMonitorInfoW( monitor, &mon_info );
771 rc_work = mon_info.rcMonitor;
773 if (style & WS_MAXIMIZEBOX)
775 if ((style & WS_CAPTION) == WS_CAPTION || !(style & (WS_CHILD | WS_POPUP)))
776 rc_work = mon_info.rcWork;
779 if (MinMax.ptMaxSize.x == GetSystemMetrics(SM_CXSCREEN) + 2 * xinc &&
780 MinMax.ptMaxSize.y == GetSystemMetrics(SM_CYSCREEN) + 2 * yinc)
782 MinMax.ptMaxSize.x = (rc_work.right - rc_work.left) + 2 * xinc;
783 MinMax.ptMaxSize.y = (rc_work.bottom - rc_work.top) + 2 * yinc;
785 if (MinMax.ptMaxPosition.x == -xinc && MinMax.ptMaxPosition.y == -yinc)
787 MinMax.ptMaxPosition.x = rc_work.left - xinc;
788 MinMax.ptMaxPosition.y = rc_work.top - yinc;
792 /* Some sanity checks */
794 TRACE("%d %d / %d %d / %d %d / %d %d\n",
795 MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
796 MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
797 MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
798 MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
799 MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
800 MinMax.ptMinTrackSize.x );
801 MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
802 MinMax.ptMinTrackSize.y );
804 if (maxSize) *maxSize = MinMax.ptMaxSize;
805 if (maxPos) *maxPos = MinMax.ptMaxPosition;
806 if (minTrack) *minTrack = MinMax.ptMinTrackSize;
807 if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
811 /***********************************************************************
812 * WINPOS_FindIconPos
814 * Find a suitable place for an iconic window.
816 static POINT WINPOS_FindIconPos( HWND hwnd, POINT pt )
818 RECT rect, rectParent;
819 HWND parent, child;
820 HRGN hrgn, tmp;
821 int xspacing, yspacing;
823 parent = GetAncestor( hwnd, GA_PARENT );
824 GetClientRect( parent, &rectParent );
825 if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
826 (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
827 return pt; /* The icon already has a suitable position */
829 xspacing = GetSystemMetrics(SM_CXICONSPACING);
830 yspacing = GetSystemMetrics(SM_CYICONSPACING);
832 /* Check if another icon already occupies this spot */
833 /* FIXME: this is completely inefficient */
835 hrgn = CreateRectRgn( 0, 0, 0, 0 );
836 tmp = CreateRectRgn( 0, 0, 0, 0 );
837 for (child = GetWindow( parent, GW_HWNDFIRST ); child; child = GetWindow( child, GW_HWNDNEXT ))
839 if (child == hwnd) continue;
840 if ((GetWindowLongW( child, GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != (WS_VISIBLE|WS_MINIMIZE))
841 continue;
842 if (WIN_GetRectangles( child, COORDS_PARENT, &rect, NULL ))
844 SetRectRgn( tmp, rect.left, rect.top, rect.right, rect.bottom );
845 CombineRgn( hrgn, hrgn, tmp, RGN_OR );
848 DeleteObject( tmp );
850 for (rect.bottom = rectParent.bottom; rect.bottom >= yspacing; rect.bottom -= yspacing)
852 for (rect.left = rectParent.left; rect.left <= rectParent.right - xspacing; rect.left += xspacing)
854 rect.right = rect.left + xspacing;
855 rect.top = rect.bottom - yspacing;
856 if (!RectInRegion( hrgn, &rect ))
858 /* No window was found, so it's OK for us */
859 pt.x = rect.left + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
860 pt.y = rect.top + (yspacing - GetSystemMetrics(SM_CYICON)) / 2;
861 DeleteObject( hrgn );
862 return pt;
866 DeleteObject( hrgn );
867 pt.x = pt.y = 0;
868 return pt;
872 /***********************************************************************
873 * WINPOS_MinMaximize
875 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
877 WND *wndPtr;
878 UINT swpFlags = 0;
879 POINT size;
880 LONG old_style;
881 WINDOWPLACEMENT wpl;
883 TRACE("%p %u\n", hwnd, cmd );
885 wpl.length = sizeof(wpl);
886 GetWindowPlacement( hwnd, &wpl );
888 if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
889 return SWP_NOSIZE | SWP_NOMOVE;
891 if (IsIconic( hwnd ))
893 switch (cmd)
895 case SW_SHOWMINNOACTIVE:
896 case SW_SHOWMINIMIZED:
897 case SW_FORCEMINIMIZE:
898 case SW_MINIMIZE:
899 return SWP_NOSIZE | SWP_NOMOVE;
901 if (!SendMessageW( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
902 swpFlags |= SWP_NOCOPYBITS;
905 switch( cmd )
907 case SW_SHOWMINNOACTIVE:
908 case SW_SHOWMINIMIZED:
909 case SW_FORCEMINIMIZE:
910 case SW_MINIMIZE:
911 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
912 if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
913 else wndPtr->flags &= ~WIN_RESTORE_MAX;
914 WIN_ReleasePtr( wndPtr );
916 old_style = WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
918 wpl.ptMinPosition = WINPOS_FindIconPos( hwnd, wpl.ptMinPosition );
920 if (!(old_style & WS_MINIMIZE)) swpFlags |= SWP_STATECHANGED;
921 SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
922 wpl.ptMinPosition.x + GetSystemMetrics(SM_CXICON),
923 wpl.ptMinPosition.y + GetSystemMetrics(SM_CYICON) );
924 swpFlags |= SWP_NOCOPYBITS;
925 break;
927 case SW_MAXIMIZE:
928 old_style = GetWindowLongW( hwnd, GWL_STYLE );
929 if ((old_style & WS_MAXIMIZE) && (old_style & WS_VISIBLE)) return SWP_NOSIZE | SWP_NOMOVE;
931 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
933 old_style = WIN_SetStyle( hwnd, WS_MAXIMIZE, WS_MINIMIZE );
934 if (old_style & WS_MINIMIZE)
936 if ((wndPtr = WIN_GetPtr( hwnd )) && wndPtr != WND_OTHER_PROCESS)
938 wndPtr->flags |= WIN_RESTORE_MAX;
939 WIN_ReleasePtr( wndPtr );
941 WINPOS_ShowIconTitle( hwnd, FALSE );
944 if (!(old_style & WS_MAXIMIZE)) swpFlags |= SWP_STATECHANGED;
945 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y,
946 wpl.ptMaxPosition.x + size.x, wpl.ptMaxPosition.y + size.y );
947 break;
949 case SW_SHOWNOACTIVATE:
950 if ((wndPtr = WIN_GetPtr( hwnd )) && wndPtr != WND_OTHER_PROCESS)
952 wndPtr->flags &= ~WIN_RESTORE_MAX;
953 WIN_ReleasePtr( wndPtr );
955 /* fall through */
956 case SW_SHOWNORMAL:
957 case SW_RESTORE:
958 old_style = WIN_SetStyle( hwnd, 0, WS_MINIMIZE | WS_MAXIMIZE );
959 if (old_style & WS_MINIMIZE)
961 BOOL restore_max;
963 WINPOS_ShowIconTitle( hwnd, FALSE );
965 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
966 restore_max = (wndPtr->flags & WIN_RESTORE_MAX) != 0;
967 WIN_ReleasePtr( wndPtr );
968 if (restore_max)
970 /* Restore to maximized position */
971 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
972 WIN_SetStyle( hwnd, WS_MAXIMIZE, 0 );
973 swpFlags |= SWP_STATECHANGED;
974 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y,
975 wpl.ptMaxPosition.x + size.x, wpl.ptMaxPosition.y + size.y );
976 break;
979 else if (!(old_style & WS_MAXIMIZE)) break;
981 swpFlags |= SWP_STATECHANGED;
983 /* Restore to normal position */
985 *rect = wpl.rcNormalPosition;
986 break;
989 return swpFlags;
993 /***********************************************************************
994 * show_window
996 * Implementation of ShowWindow and ShowWindowAsync.
998 static BOOL show_window( HWND hwnd, INT cmd )
1000 WND *wndPtr;
1001 HWND parent;
1002 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1003 BOOL wasVisible = (style & WS_VISIBLE) != 0;
1004 BOOL showFlag = TRUE;
1005 RECT newPos = {0, 0, 0, 0};
1006 UINT swp = 0;
1008 TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);
1010 switch(cmd)
1012 case SW_HIDE:
1013 if (!wasVisible) return FALSE;
1014 showFlag = FALSE;
1015 swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1016 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1017 break;
1019 case SW_SHOWMINNOACTIVE:
1020 case SW_MINIMIZE:
1021 case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
1022 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1023 /* fall through */
1024 case SW_SHOWMINIMIZED:
1025 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1026 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
1027 if ((style & WS_MINIMIZE) && wasVisible) return TRUE;
1028 break;
1030 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
1031 if (!wasVisible) swp |= SWP_SHOWWINDOW;
1032 swp |= SWP_FRAMECHANGED;
1033 swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
1034 if ((style & WS_MAXIMIZE) && wasVisible) return TRUE;
1035 break;
1037 case SW_SHOWNA:
1038 swp |= SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1039 if (style & WS_CHILD) swp |= SWP_NOZORDER;
1040 break;
1041 case SW_SHOW:
1042 if (wasVisible) return TRUE;
1043 swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1044 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1045 break;
1047 case SW_SHOWNOACTIVATE:
1048 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1049 /* fall through */
1050 case SW_RESTORE:
1051 /* fall through */
1052 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
1053 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
1054 if (!wasVisible) swp |= SWP_SHOWWINDOW;
1055 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1057 swp |= SWP_FRAMECHANGED;
1058 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
1060 else
1062 if (wasVisible) return TRUE;
1063 swp |= SWP_NOSIZE | SWP_NOMOVE;
1065 if (style & WS_CHILD && !(swp & SWP_STATECHANGED)) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1066 break;
1067 default:
1068 return wasVisible;
1071 if ((showFlag != wasVisible || cmd == SW_SHOWNA) && cmd != SW_SHOWMAXIMIZED && !(swp & SWP_STATECHANGED))
1073 SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
1074 if (!IsWindow( hwnd )) return wasVisible;
1077 swp = USER_Driver->pShowWindow( hwnd, cmd, &newPos, swp );
1079 parent = GetAncestor( hwnd, GA_PARENT );
1080 if (parent && !IsWindowVisible( parent ) && !(swp & SWP_STATECHANGED))
1082 /* if parent is not visible simply toggle WS_VISIBLE and return */
1083 if (showFlag) WIN_SetStyle( hwnd, WS_VISIBLE, 0 );
1084 else WIN_SetStyle( hwnd, 0, WS_VISIBLE );
1086 else
1087 SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1088 newPos.right - newPos.left, newPos.bottom - newPos.top, swp );
1090 if (cmd == SW_HIDE)
1092 HWND hFocus;
1094 WINPOS_ShowIconTitle( hwnd, FALSE );
1096 /* FIXME: This will cause the window to be activated irrespective
1097 * of whether it is owned by the same thread. Has to be done
1098 * asynchronously.
1101 if (hwnd == GetActiveWindow())
1102 WINPOS_ActivateOtherWindow(hwnd);
1104 /* Revert focus to parent */
1105 hFocus = GetFocus();
1106 if (hwnd == hFocus)
1108 HWND parent = GetAncestor(hwnd, GA_PARENT);
1109 if (parent == GetDesktopWindow()) parent = 0;
1110 SetFocus(parent);
1112 return wasVisible;
1115 if (IsIconic(hwnd)) WINPOS_ShowIconTitle( hwnd, TRUE );
1117 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return wasVisible;
1119 if (wndPtr->flags & WIN_NEED_SIZE)
1121 /* should happen only in CreateWindowEx() */
1122 int wParam = SIZE_RESTORED;
1123 RECT client;
1124 LPARAM lparam;
1126 WIN_GetRectangles( hwnd, COORDS_PARENT, NULL, &client );
1127 lparam = MAKELONG( client.right - client.left, client.bottom - client.top );
1128 wndPtr->flags &= ~WIN_NEED_SIZE;
1129 if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1130 else if (wndPtr->dwStyle & WS_MINIMIZE)
1132 wParam = SIZE_MINIMIZED;
1133 lparam = 0;
1135 WIN_ReleasePtr( wndPtr );
1137 SendMessageW( hwnd, WM_SIZE, wParam, lparam );
1138 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( client.left, client.top ));
1140 else WIN_ReleasePtr( wndPtr );
1142 /* if previous state was minimized Windows sets focus to the window */
1143 if (style & WS_MINIMIZE) SetFocus( hwnd );
1145 return wasVisible;
1149 /***********************************************************************
1150 * ShowWindowAsync (USER32.@)
1152 * doesn't wait; returns immediately.
1153 * used by threads to toggle windows in other (possibly hanging) threads
1155 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
1157 HWND full_handle;
1159 if (is_broadcast(hwnd))
1161 SetLastError( ERROR_INVALID_PARAMETER );
1162 return FALSE;
1165 if ((full_handle = WIN_IsCurrentThread( hwnd )))
1166 return show_window( full_handle, cmd );
1168 return SendNotifyMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
1172 /***********************************************************************
1173 * ShowWindow (USER32.@)
1175 BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
1177 HWND full_handle;
1179 if (is_broadcast(hwnd))
1181 SetLastError( ERROR_INVALID_PARAMETER );
1182 return FALSE;
1184 if ((full_handle = WIN_IsCurrentThread( hwnd )))
1185 return show_window( full_handle, cmd );
1187 return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
1191 /***********************************************************************
1192 * GetInternalWindowPos (USER32.@)
1194 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
1195 LPPOINT ptIcon )
1197 WINDOWPLACEMENT wndpl;
1198 if (GetWindowPlacement( hwnd, &wndpl ))
1200 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
1201 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
1202 return wndpl.showCmd;
1204 return 0;
1208 /***********************************************************************
1209 * GetWindowPlacement (USER32.@)
1211 * Win95:
1212 * Fails if wndpl->length of Win95 (!) apps is invalid.
1214 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
1216 WND *pWnd = WIN_GetPtr( hwnd );
1218 if (!pWnd) return FALSE;
1220 if (pWnd == WND_DESKTOP)
1222 wndpl->length = sizeof(*wndpl);
1223 wndpl->showCmd = SW_SHOWNORMAL;
1224 wndpl->flags = 0;
1225 wndpl->ptMinPosition.x = -1;
1226 wndpl->ptMinPosition.y = -1;
1227 wndpl->ptMaxPosition.x = -1;
1228 wndpl->ptMaxPosition.y = -1;
1229 GetWindowRect( hwnd, &wndpl->rcNormalPosition );
1230 return TRUE;
1232 if (pWnd == WND_OTHER_PROCESS)
1234 if (!IsWindow( hwnd )) return FALSE;
1235 FIXME( "not supported on other process window %p\n", hwnd );
1236 /* provide some dummy information */
1237 wndpl->length = sizeof(*wndpl);
1238 wndpl->showCmd = SW_SHOWNORMAL;
1239 wndpl->flags = 0;
1240 wndpl->ptMinPosition.x = -1;
1241 wndpl->ptMinPosition.y = -1;
1242 wndpl->ptMaxPosition.x = -1;
1243 wndpl->ptMaxPosition.y = -1;
1244 GetWindowRect( hwnd, &wndpl->rcNormalPosition );
1245 return TRUE;
1248 /* update the placement according to the current style */
1249 if (pWnd->dwStyle & WS_MINIMIZE)
1251 pWnd->min_pos.x = pWnd->rectWindow.left;
1252 pWnd->min_pos.y = pWnd->rectWindow.top;
1254 else if (pWnd->dwStyle & WS_MAXIMIZE)
1256 pWnd->max_pos.x = pWnd->rectWindow.left;
1257 pWnd->max_pos.y = pWnd->rectWindow.top;
1259 else
1261 pWnd->normal_rect = pWnd->rectWindow;
1264 wndpl->length = sizeof(*wndpl);
1265 if( pWnd->dwStyle & WS_MINIMIZE )
1266 wndpl->showCmd = SW_SHOWMINIMIZED;
1267 else
1268 wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
1269 if( pWnd->flags & WIN_RESTORE_MAX )
1270 wndpl->flags = WPF_RESTORETOMAXIMIZED;
1271 else
1272 wndpl->flags = 0;
1273 wndpl->ptMinPosition = pWnd->min_pos;
1274 wndpl->ptMaxPosition = pWnd->max_pos;
1275 wndpl->rcNormalPosition = pWnd->normal_rect;
1276 WIN_ReleasePtr( pWnd );
1278 TRACE( "%p: returning min %d,%d max %d,%d normal %s\n",
1279 hwnd, wndpl->ptMinPosition.x, wndpl->ptMinPosition.y,
1280 wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y,
1281 wine_dbgstr_rect(&wndpl->rcNormalPosition) );
1282 return TRUE;
1285 /* make sure the specified rect is visible on screen */
1286 static void make_rect_onscreen( RECT *rect )
1288 MONITORINFO info;
1289 HMONITOR monitor = MonitorFromRect( rect, MONITOR_DEFAULTTONEAREST );
1291 info.cbSize = sizeof(info);
1292 if (!monitor || !GetMonitorInfoW( monitor, &info )) return;
1293 /* FIXME: map coordinates from rcWork to rcMonitor */
1294 if (rect->right <= info.rcWork.left)
1296 rect->right += info.rcWork.left - rect->left;
1297 rect->left = info.rcWork.left;
1299 else if (rect->left >= info.rcWork.right)
1301 rect->left += info.rcWork.right - rect->right;
1302 rect->right = info.rcWork.right;
1304 if (rect->bottom <= info.rcWork.top)
1306 rect->bottom += info.rcWork.top - rect->top;
1307 rect->top = info.rcWork.top;
1309 else if (rect->top >= info.rcWork.bottom)
1311 rect->top += info.rcWork.bottom - rect->bottom;
1312 rect->bottom = info.rcWork.bottom;
1316 /* make sure the specified point is visible on screen */
1317 static void make_point_onscreen( POINT *pt )
1319 RECT rect;
1321 SetRect( &rect, pt->x, pt->y, pt->x + 1, pt->y + 1 );
1322 make_rect_onscreen( &rect );
1323 pt->x = rect.left;
1324 pt->y = rect.top;
1328 /***********************************************************************
1329 * WINPOS_SetPlacement
1331 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
1333 DWORD style;
1334 WND *pWnd = WIN_GetPtr( hwnd );
1335 WINDOWPLACEMENT wp = *wndpl;
1337 if (flags & PLACE_MIN) make_point_onscreen( &wp.ptMinPosition );
1338 if (flags & PLACE_MAX) make_point_onscreen( &wp.ptMaxPosition );
1339 if (flags & PLACE_RECT) make_rect_onscreen( &wp.rcNormalPosition );
1341 TRACE( "%p: setting min %d,%d max %d,%d normal %s flags %x ajusted to min %d,%d max %d,%d normal %s\n",
1342 hwnd, wndpl->ptMinPosition.x, wndpl->ptMinPosition.y,
1343 wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y,
1344 wine_dbgstr_rect(&wndpl->rcNormalPosition), flags,
1345 wp.ptMinPosition.x, wp.ptMinPosition.y, wp.ptMaxPosition.x, wp.ptMaxPosition.y,
1346 wine_dbgstr_rect(&wp.rcNormalPosition) );
1348 if (!pWnd || pWnd == WND_OTHER_PROCESS || pWnd == WND_DESKTOP) return FALSE;
1350 if( flags & PLACE_MIN ) pWnd->min_pos = wp.ptMinPosition;
1351 if( flags & PLACE_MAX ) pWnd->max_pos = wp.ptMaxPosition;
1352 if( flags & PLACE_RECT) pWnd->normal_rect = wp.rcNormalPosition;
1354 style = pWnd->dwStyle;
1356 WIN_ReleasePtr( pWnd );
1358 if( style & WS_MINIMIZE )
1360 if (flags & PLACE_MIN)
1362 WINPOS_ShowIconTitle( hwnd, FALSE );
1363 SetWindowPos( hwnd, 0, wp.ptMinPosition.x, wp.ptMinPosition.y, 0, 0,
1364 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1367 else if( style & WS_MAXIMIZE )
1369 if (flags & PLACE_MAX)
1370 SetWindowPos( hwnd, 0, wp.ptMaxPosition.x, wp.ptMaxPosition.y, 0, 0,
1371 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1373 else if( flags & PLACE_RECT )
1374 SetWindowPos( hwnd, 0, wp.rcNormalPosition.left, wp.rcNormalPosition.top,
1375 wp.rcNormalPosition.right - wp.rcNormalPosition.left,
1376 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top,
1377 SWP_NOZORDER | SWP_NOACTIVATE );
1379 ShowWindow( hwnd, wndpl->showCmd );
1381 if (IsIconic( hwnd ))
1383 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE) WINPOS_ShowIconTitle( hwnd, TRUE );
1385 /* SDK: ...valid only the next time... */
1386 if( wndpl->flags & WPF_RESTORETOMAXIMIZED )
1388 pWnd = WIN_GetPtr( hwnd );
1389 if (pWnd && pWnd != WND_OTHER_PROCESS)
1391 pWnd->flags |= WIN_RESTORE_MAX;
1392 WIN_ReleasePtr( pWnd );
1396 return TRUE;
1400 /***********************************************************************
1401 * SetWindowPlacement (USER32.@)
1403 * Win95:
1404 * Fails if wndpl->length of Win95 (!) apps is invalid.
1406 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
1408 UINT flags = PLACE_MAX | PLACE_RECT;
1409 if (!wpl) return FALSE;
1410 if (wpl->flags & WPF_SETMINPOSITION) flags |= PLACE_MIN;
1411 return WINPOS_SetPlacement( hwnd, wpl, flags );
1415 /***********************************************************************
1416 * AnimateWindow (USER32.@)
1417 * Shows/Hides a window with an animation
1418 * NO ANIMATION YET
1420 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
1422 FIXME("partial stub\n");
1424 /* If trying to show/hide and it's already *
1425 * shown/hidden or invalid window, fail with *
1426 * invalid parameter */
1427 if(!IsWindow(hwnd) ||
1428 (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
1429 (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
1431 SetLastError(ERROR_INVALID_PARAMETER);
1432 return FALSE;
1435 ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1437 return TRUE;
1440 /***********************************************************************
1441 * SetInternalWindowPos (USER32.@)
1443 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1444 LPRECT rect, LPPOINT pt )
1446 WINDOWPLACEMENT wndpl;
1447 UINT flags;
1449 wndpl.length = sizeof(wndpl);
1450 wndpl.showCmd = showCmd;
1451 wndpl.flags = flags = 0;
1453 if( pt )
1455 flags |= PLACE_MIN;
1456 wndpl.flags |= WPF_SETMINPOSITION;
1457 wndpl.ptMinPosition = *pt;
1459 if( rect )
1461 flags |= PLACE_RECT;
1462 wndpl.rcNormalPosition = *rect;
1464 WINPOS_SetPlacement( hwnd, &wndpl, flags );
1468 /*******************************************************************
1469 * can_activate_window
1471 * Check if we can activate the specified window.
1473 static BOOL can_activate_window( HWND hwnd )
1475 LONG style;
1477 if (!hwnd) return FALSE;
1478 style = GetWindowLongW( hwnd, GWL_STYLE );
1479 if (!(style & WS_VISIBLE)) return FALSE;
1480 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1481 return !(style & WS_DISABLED);
1485 /*******************************************************************
1486 * WINPOS_ActivateOtherWindow
1488 * Activates window other than pWnd.
1490 void WINPOS_ActivateOtherWindow(HWND hwnd)
1492 HWND hwndTo, fg;
1494 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1496 hwndTo = GetAncestor( hwndTo, GA_ROOT );
1497 if (can_activate_window( hwndTo )) goto done;
1500 hwndTo = hwnd;
1501 for (;;)
1503 if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1504 if (can_activate_window( hwndTo )) break;
1507 done:
1508 fg = GetForegroundWindow();
1509 TRACE("win = %p fg = %p\n", hwndTo, fg);
1510 if (!fg || (hwnd == fg))
1512 if (SetForegroundWindow( hwndTo )) return;
1514 if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1518 /***********************************************************************
1519 * WINPOS_HandleWindowPosChanging
1521 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1523 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1525 POINT minTrack, maxTrack;
1526 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1528 if (winpos->flags & SWP_NOSIZE) return 0;
1529 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1531 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1532 if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1533 if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1534 if (!(style & WS_MINIMIZE))
1536 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1537 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1540 return 0;
1544 /***********************************************************************
1545 * dump_winpos_flags
1547 static void dump_winpos_flags(UINT flags)
1549 static const DWORD dumped_flags = (SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOREDRAW |
1550 SWP_NOACTIVATE | SWP_FRAMECHANGED | SWP_SHOWWINDOW |
1551 SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOOWNERZORDER |
1552 SWP_NOSENDCHANGING | SWP_DEFERERASE | SWP_ASYNCWINDOWPOS |
1553 SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_STATECHANGED);
1554 TRACE("flags:");
1555 if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1556 if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1557 if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1558 if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1559 if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1560 if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1561 if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1562 if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1563 if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1564 if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1565 if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1566 if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1567 if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1568 if(flags & SWP_NOCLIENTSIZE) TRACE(" SWP_NOCLIENTSIZE");
1569 if(flags & SWP_NOCLIENTMOVE) TRACE(" SWP_NOCLIENTMOVE");
1570 if(flags & SWP_STATECHANGED) TRACE(" SWP_STATECHANGED");
1572 if(flags & ~dumped_flags) TRACE(" %08x", flags & ~dumped_flags);
1573 TRACE("\n");
1576 /***********************************************************************
1577 * SWP_DoWinPosChanging
1579 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
1581 WND *wndPtr;
1582 RECT window_rect, client_rect;
1584 /* Send WM_WINDOWPOSCHANGING message */
1586 if (!(pWinpos->flags & SWP_NOSENDCHANGING))
1587 SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
1589 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) ||
1590 wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
1592 /* Calculate new position and size */
1594 WIN_GetRectangles( pWinpos->hwnd, COORDS_PARENT, &window_rect, &client_rect );
1595 *pNewWindowRect = window_rect;
1596 *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? window_rect : client_rect;
1598 if (!(pWinpos->flags & SWP_NOSIZE))
1600 if (wndPtr->dwStyle & WS_MINIMIZE)
1602 pNewWindowRect->right = pNewWindowRect->left + GetSystemMetrics(SM_CXICON);
1603 pNewWindowRect->bottom = pNewWindowRect->top + GetSystemMetrics(SM_CYICON);
1605 else
1607 pNewWindowRect->right = pNewWindowRect->left + pWinpos->cx;
1608 pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
1611 if (!(pWinpos->flags & SWP_NOMOVE))
1613 pNewWindowRect->left = pWinpos->x;
1614 pNewWindowRect->top = pWinpos->y;
1615 pNewWindowRect->right += pWinpos->x - window_rect.left;
1616 pNewWindowRect->bottom += pWinpos->y - window_rect.top;
1618 OffsetRect( pNewClientRect, pWinpos->x - window_rect.left,
1619 pWinpos->y - window_rect.top );
1621 pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
1623 TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
1624 pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
1625 pWinpos->cx, pWinpos->cy, pWinpos->flags );
1626 TRACE( "current %s style %08x new %s\n",
1627 wine_dbgstr_rect( &window_rect ), wndPtr->dwStyle,
1628 wine_dbgstr_rect( pNewWindowRect ));
1630 WIN_ReleasePtr( wndPtr );
1631 return TRUE;
1634 /***********************************************************************
1635 * get_valid_rects
1637 * Compute the valid rects from the old and new client rect and WVR_* flags.
1638 * Helper for WM_NCCALCSIZE handling.
1640 static inline void get_valid_rects( const RECT *old_client, const RECT *new_client, UINT flags,
1641 RECT *valid )
1643 int cx, cy;
1645 if (flags & WVR_REDRAW)
1647 SetRectEmpty( &valid[0] );
1648 SetRectEmpty( &valid[1] );
1649 return;
1652 if (flags & WVR_VALIDRECTS)
1654 if (!IntersectRect( &valid[0], &valid[0], new_client ) ||
1655 !IntersectRect( &valid[1], &valid[1], old_client ))
1657 SetRectEmpty( &valid[0] );
1658 SetRectEmpty( &valid[1] );
1659 return;
1661 flags = WVR_ALIGNLEFT | WVR_ALIGNTOP;
1663 else
1665 valid[0] = *new_client;
1666 valid[1] = *old_client;
1669 /* make sure the rectangles have the same size */
1670 cx = min( valid[0].right - valid[0].left, valid[1].right - valid[1].left );
1671 cy = min( valid[0].bottom - valid[0].top, valid[1].bottom - valid[1].top );
1673 if (flags & WVR_ALIGNBOTTOM)
1675 valid[0].top = valid[0].bottom - cy;
1676 valid[1].top = valid[1].bottom - cy;
1678 else
1680 valid[0].bottom = valid[0].top + cy;
1681 valid[1].bottom = valid[1].top + cy;
1683 if (flags & WVR_ALIGNRIGHT)
1685 valid[0].left = valid[0].right - cx;
1686 valid[1].left = valid[1].right - cx;
1688 else
1690 valid[0].right = valid[0].left + cx;
1691 valid[1].right = valid[1].left + cx;
1696 /***********************************************************************
1697 * SWP_DoOwnedPopups
1699 * fix Z order taking into account owned popups -
1700 * basically we need to maintain them above the window that owns them
1702 * FIXME: hide/show owned popups when owner visibility changes.
1704 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
1706 HWND owner, *list = NULL;
1707 unsigned int i;
1709 TRACE("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
1711 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) return hwndInsertAfter;
1713 if ((owner = GetWindow( hwnd, GW_OWNER )))
1715 /* make sure this popup stays above the owner */
1717 if (hwndInsertAfter != HWND_TOPMOST)
1719 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return hwndInsertAfter;
1721 for (i = 0; list[i]; i++)
1723 BOOL topmost = (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST) != 0;
1725 if (list[i] == owner)
1727 if (i > 0) hwndInsertAfter = list[i-1];
1728 else hwndInsertAfter = topmost ? HWND_TOPMOST : HWND_TOP;
1729 break;
1732 if (hwndInsertAfter == HWND_TOP || hwndInsertAfter == HWND_NOTOPMOST)
1734 if (!topmost) break;
1736 else if (list[i] == hwndInsertAfter) break;
1741 if (hwndInsertAfter == HWND_BOTTOM) goto done;
1742 if (!list && !(list = WIN_ListChildren( GetDesktopWindow() ))) goto done;
1744 i = 0;
1745 if (hwndInsertAfter == HWND_TOP || hwndInsertAfter == HWND_NOTOPMOST)
1747 if (hwndInsertAfter == HWND_NOTOPMOST || !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_TOPMOST))
1749 /* skip all the topmost windows */
1750 while (list[i] && (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST)) i++;
1753 else if (hwndInsertAfter != HWND_TOPMOST)
1755 /* skip windows that are already placed correctly */
1756 for (i = 0; list[i]; i++)
1758 if (list[i] == hwndInsertAfter) break;
1759 if (list[i] == hwnd) goto done; /* nothing to do if window is moving backwards in z-order */
1763 for ( ; list[i]; i++)
1765 if (list[i] == hwnd) break;
1766 if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
1767 TRACE( "moving %p owned by %p after %p\n", list[i], hwnd, hwndInsertAfter );
1768 SetWindowPos( list[i], hwndInsertAfter, 0, 0, 0, 0,
1769 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING | SWP_DEFERERASE );
1770 hwndInsertAfter = list[i];
1773 done:
1774 HeapFree( GetProcessHeap(), 0, list );
1775 return hwndInsertAfter;
1778 /***********************************************************************
1779 * SWP_DoNCCalcSize
1781 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect,
1782 RECT *validRects )
1784 UINT wvrFlags = 0;
1785 RECT window_rect, client_rect;
1787 WIN_GetRectangles( pWinpos->hwnd, COORDS_PARENT, &window_rect, &client_rect );
1789 /* Send WM_NCCALCSIZE message to get new client area */
1790 if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
1792 NCCALCSIZE_PARAMS params;
1793 WINDOWPOS winposCopy;
1795 params.rgrc[0] = *pNewWindowRect;
1796 params.rgrc[1] = window_rect;
1797 params.rgrc[2] = client_rect;
1798 params.lppos = &winposCopy;
1799 winposCopy = *pWinpos;
1801 wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
1803 *pNewClientRect = params.rgrc[0];
1805 TRACE( "hwnd %p old win %s old client %s new win %s new client %s\n", pWinpos->hwnd,
1806 wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(&client_rect),
1807 wine_dbgstr_rect(pNewWindowRect), wine_dbgstr_rect(pNewClientRect) );
1809 if( pNewClientRect->left != client_rect.left ||
1810 pNewClientRect->top != client_rect.top )
1811 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1813 if( (pNewClientRect->right - pNewClientRect->left !=
1814 client_rect.right - client_rect.left))
1815 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1816 else
1817 wvrFlags &= ~WVR_HREDRAW;
1819 if (pNewClientRect->bottom - pNewClientRect->top !=
1820 client_rect.bottom - client_rect.top)
1821 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1822 else
1823 wvrFlags &= ~WVR_VREDRAW;
1825 validRects[0] = params.rgrc[1];
1826 validRects[1] = params.rgrc[2];
1828 else
1830 if (!(pWinpos->flags & SWP_NOMOVE) &&
1831 (pNewClientRect->left != client_rect.left ||
1832 pNewClientRect->top != client_rect.top))
1833 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1836 if (pWinpos->flags & (SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
1838 SetRectEmpty( &validRects[0] );
1839 SetRectEmpty( &validRects[1] );
1841 else get_valid_rects( &client_rect, pNewClientRect, wvrFlags, validRects );
1843 return wvrFlags;
1846 /* fix redundant flags and values in the WINDOWPOS structure */
1847 static BOOL fixup_flags( WINDOWPOS *winpos )
1849 HWND parent;
1850 RECT window_rect;
1851 WND *wndPtr = WIN_GetPtr( winpos->hwnd );
1852 BOOL ret = TRUE;
1854 if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
1856 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1857 return FALSE;
1859 winpos->hwnd = wndPtr->obj.handle; /* make it a full handle */
1861 /* Finally make sure that all coordinates are valid */
1862 if (winpos->x < -32768) winpos->x = -32768;
1863 else if (winpos->x > 32767) winpos->x = 32767;
1864 if (winpos->y < -32768) winpos->y = -32768;
1865 else if (winpos->y > 32767) winpos->y = 32767;
1867 if (winpos->cx < 0) winpos->cx = 0;
1868 else if (winpos->cx > 32767) winpos->cx = 32767;
1869 if (winpos->cy < 0) winpos->cy = 0;
1870 else if (winpos->cy > 32767) winpos->cy = 32767;
1872 parent = GetAncestor( winpos->hwnd, GA_PARENT );
1873 if (!IsWindowVisible( parent )) winpos->flags |= SWP_NOREDRAW;
1875 if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
1876 else
1878 winpos->flags &= ~SWP_HIDEWINDOW;
1879 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
1882 WIN_GetRectangles( winpos->hwnd, COORDS_PARENT, &window_rect, NULL );
1883 if ((window_rect.right - window_rect.left == winpos->cx) &&
1884 (window_rect.bottom - window_rect.top == winpos->cy))
1885 winpos->flags |= SWP_NOSIZE; /* Already the right size */
1887 if ((window_rect.left == winpos->x) && (window_rect.top == winpos->y))
1888 winpos->flags |= SWP_NOMOVE; /* Already the right position */
1890 if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
1892 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)) && /* Bring to the top when activating */
1893 (winpos->flags & SWP_NOZORDER ||
1894 (winpos->hwndInsertAfter != HWND_TOPMOST && winpos->hwndInsertAfter != HWND_NOTOPMOST)))
1896 winpos->flags &= ~SWP_NOZORDER;
1897 winpos->hwndInsertAfter = HWND_TOP;
1901 /* Check hwndInsertAfter */
1902 if (winpos->flags & SWP_NOZORDER) goto done;
1904 /* fix sign extension */
1905 if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
1906 else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
1908 /* hwndInsertAfter must be a sibling of the window */
1909 if (winpos->hwndInsertAfter == HWND_TOP)
1911 if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1912 winpos->flags |= SWP_NOZORDER;
1914 else if (winpos->hwndInsertAfter == HWND_BOTTOM)
1916 if (!(wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
1917 winpos->flags |= SWP_NOZORDER;
1919 else if (winpos->hwndInsertAfter == HWND_TOPMOST)
1921 if ((wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1922 winpos->flags |= SWP_NOZORDER;
1924 else if (winpos->hwndInsertAfter == HWND_NOTOPMOST)
1926 if (!(wndPtr->dwExStyle & WS_EX_TOPMOST))
1927 winpos->flags |= SWP_NOZORDER;
1929 else
1931 if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != parent) ret = FALSE;
1932 else
1934 /* don't need to change the Zorder of hwnd if it's already inserted
1935 * after hwndInsertAfter or when inserting hwnd after itself.
1937 if ((winpos->hwnd == winpos->hwndInsertAfter) ||
1938 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
1939 winpos->flags |= SWP_NOZORDER;
1942 done:
1943 WIN_ReleasePtr( wndPtr );
1944 return ret;
1948 /***********************************************************************
1949 * set_window_pos
1951 * Backend implementation of SetWindowPos.
1953 BOOL set_window_pos( HWND hwnd, HWND insert_after, UINT swp_flags,
1954 const RECT *window_rect, const RECT *client_rect, const RECT *valid_rects )
1956 WND *win;
1957 BOOL ret;
1958 RECT visible_rect, old_window_rect;
1959 int old_width;
1961 visible_rect = *window_rect;
1962 USER_Driver->pWindowPosChanging( hwnd, insert_after, swp_flags,
1963 window_rect, client_rect, &visible_rect );
1965 WIN_GetRectangles( hwnd, COORDS_SCREEN, &old_window_rect, NULL );
1967 if (!(win = WIN_GetPtr( hwnd ))) return FALSE;
1968 if (win == WND_DESKTOP || win == WND_OTHER_PROCESS) return FALSE;
1969 old_width = win->rectClient.right - win->rectClient.left;
1971 SERVER_START_REQ( set_window_pos )
1973 req->handle = wine_server_user_handle( hwnd );
1974 req->previous = wine_server_user_handle( insert_after );
1975 req->flags = swp_flags;
1976 req->window.left = window_rect->left;
1977 req->window.top = window_rect->top;
1978 req->window.right = window_rect->right;
1979 req->window.bottom = window_rect->bottom;
1980 req->client.left = client_rect->left;
1981 req->client.top = client_rect->top;
1982 req->client.right = client_rect->right;
1983 req->client.bottom = client_rect->bottom;
1984 if (memcmp( window_rect, &visible_rect, sizeof(RECT) ) || !IsRectEmpty( &valid_rects[0] ))
1986 wine_server_add_data( req, &visible_rect, sizeof(visible_rect) );
1987 if (!IsRectEmpty( &valid_rects[0] ))
1988 wine_server_add_data( req, valid_rects, 2 * sizeof(*valid_rects) );
1990 if ((ret = !wine_server_call( req )))
1992 win->dwStyle = reply->new_style;
1993 win->dwExStyle = reply->new_ex_style;
1994 win->rectWindow = *window_rect;
1995 win->rectClient = *client_rect;
1996 if (GetWindowLongW( win->parent, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
1998 RECT client;
1999 GetClientRect( win->parent, &client );
2000 mirror_rect( &client, &win->rectWindow );
2001 mirror_rect( &client, &win->rectClient );
2003 /* if an RTL window is resized the children have moved */
2004 if (win->dwExStyle & WS_EX_LAYOUTRTL && client_rect->right - client_rect->left != old_width)
2005 win->flags |= WIN_CHILDREN_MOVED;
2008 SERVER_END_REQ;
2009 WIN_ReleasePtr( win );
2011 if (ret)
2013 if (((swp_flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) ||
2014 (swp_flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW | SWP_STATECHANGED)))
2015 invalidate_dce( hwnd, &old_window_rect );
2017 USER_Driver->pWindowPosChanged( hwnd, insert_after, swp_flags, window_rect,
2018 client_rect, &visible_rect, valid_rects );
2020 return ret;
2024 /***********************************************************************
2025 * USER_SetWindowPos
2027 * User32 internal function
2029 BOOL USER_SetWindowPos( WINDOWPOS * winpos )
2031 RECT newWindowRect, newClientRect, valid_rects[2];
2032 UINT orig_flags;
2034 orig_flags = winpos->flags;
2036 /* First make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
2037 if (!(winpos->flags & SWP_NOMOVE))
2039 if (winpos->x < -32768) winpos->x = -32768;
2040 else if (winpos->x > 32767) winpos->x = 32767;
2041 if (winpos->y < -32768) winpos->y = -32768;
2042 else if (winpos->y > 32767) winpos->y = 32767;
2044 if (!(winpos->flags & SWP_NOSIZE))
2046 if (winpos->cx < 0) winpos->cx = 0;
2047 else if (winpos->cx > 32767) winpos->cx = 32767;
2048 if (winpos->cy < 0) winpos->cy = 0;
2049 else if (winpos->cy > 32767) winpos->cy = 32767;
2052 if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
2054 /* Fix redundant flags */
2055 if (!fixup_flags( winpos )) return FALSE;
2057 if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
2059 if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
2060 winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
2063 /* Common operations */
2065 SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect, valid_rects );
2067 if (!set_window_pos( winpos->hwnd, winpos->hwndInsertAfter, winpos->flags,
2068 &newWindowRect, &newClientRect, valid_rects ))
2069 return FALSE;
2071 /* erase parent when hiding or resizing child */
2072 if (!(orig_flags & SWP_DEFERERASE) &&
2073 ((orig_flags & SWP_HIDEWINDOW) ||
2074 (!(orig_flags & SWP_SHOWWINDOW) &&
2075 (winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOGEOMETRYCHANGE)))
2077 HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
2078 if (!parent || parent == GetDesktopWindow()) parent = winpos->hwnd;
2079 erase_now( parent, 0 );
2082 if( winpos->flags & SWP_HIDEWINDOW )
2083 HideCaret(winpos->hwnd);
2084 else if (winpos->flags & SWP_SHOWWINDOW)
2085 ShowCaret(winpos->hwnd);
2087 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)))
2089 /* child windows get WM_CHILDACTIVATE message */
2090 if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
2091 SendMessageW( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
2092 else
2093 SetForegroundWindow( winpos->hwnd );
2096 /* And last, send the WM_WINDOWPOSCHANGED message */
2098 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
2100 if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
2102 /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
2103 and always contains final window position.
2105 winpos->x = newWindowRect.left;
2106 winpos->y = newWindowRect.top;
2107 winpos->cx = newWindowRect.right - newWindowRect.left;
2108 winpos->cy = newWindowRect.bottom - newWindowRect.top;
2109 SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
2111 return TRUE;
2114 /***********************************************************************
2115 * SetWindowPos (USER32.@)
2117 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
2118 INT x, INT y, INT cx, INT cy, UINT flags )
2120 WINDOWPOS winpos;
2122 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2123 hwnd, hwndInsertAfter, x, y, cx, cy, flags);
2124 if(TRACE_ON(win)) dump_winpos_flags(flags);
2126 if (is_broadcast(hwnd))
2128 SetLastError( ERROR_INVALID_PARAMETER );
2129 return FALSE;
2132 winpos.hwnd = WIN_GetFullHandle(hwnd);
2133 winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
2134 winpos.x = x;
2135 winpos.y = y;
2136 winpos.cx = cx;
2137 winpos.cy = cy;
2138 winpos.flags = flags;
2140 if (WIN_IsCurrentThread( hwnd ))
2141 return USER_SetWindowPos(&winpos);
2143 return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
2147 /***********************************************************************
2148 * BeginDeferWindowPos (USER32.@)
2150 HDWP WINAPI BeginDeferWindowPos( INT count )
2152 HDWP handle = 0;
2153 DWP *pDWP;
2155 TRACE("%d\n", count);
2157 if (count < 0)
2159 SetLastError(ERROR_INVALID_PARAMETER);
2160 return 0;
2162 /* Windows allows zero count, in which case it allocates context for 8 moves */
2163 if (count == 0) count = 8;
2165 if (!(pDWP = HeapAlloc( GetProcessHeap(), 0, sizeof(DWP)))) return 0;
2167 pDWP->actualCount = 0;
2168 pDWP->suggestedCount = count;
2169 pDWP->hwndParent = 0;
2171 if (!(pDWP->winPos = HeapAlloc( GetProcessHeap(), 0, count * sizeof(WINDOWPOS) )) ||
2172 !(handle = alloc_user_handle( &pDWP->obj, USER_DWP )))
2174 HeapFree( GetProcessHeap(), 0, pDWP->winPos );
2175 HeapFree( GetProcessHeap(), 0, pDWP );
2178 TRACE("returning hdwp %p\n", handle);
2179 return handle;
2183 /***********************************************************************
2184 * DeferWindowPos (USER32.@)
2186 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
2187 INT x, INT y, INT cx, INT cy,
2188 UINT flags )
2190 DWP *pDWP;
2191 int i;
2192 HDWP retvalue = hdwp;
2194 TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2195 hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
2197 hwnd = WIN_GetFullHandle( hwnd );
2198 if (is_desktop_window( hwnd )) return 0;
2200 if (!(pDWP = get_user_handle_ptr( hdwp, USER_DWP ))) return 0;
2201 if (pDWP == OBJ_OTHER_PROCESS)
2203 FIXME( "other process handle %p?\n", hdwp );
2204 return 0;
2207 for (i = 0; i < pDWP->actualCount; i++)
2209 if (pDWP->winPos[i].hwnd == hwnd)
2211 /* Merge with the other changes */
2212 if (!(flags & SWP_NOZORDER))
2214 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
2216 if (!(flags & SWP_NOMOVE))
2218 pDWP->winPos[i].x = x;
2219 pDWP->winPos[i].y = y;
2221 if (!(flags & SWP_NOSIZE))
2223 pDWP->winPos[i].cx = cx;
2224 pDWP->winPos[i].cy = cy;
2226 pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
2227 SWP_NOZORDER | SWP_NOREDRAW |
2228 SWP_NOACTIVATE | SWP_NOCOPYBITS|
2229 SWP_NOOWNERZORDER);
2230 pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
2231 SWP_FRAMECHANGED);
2232 goto END;
2235 if (pDWP->actualCount >= pDWP->suggestedCount)
2237 WINDOWPOS *newpos = HeapReAlloc( GetProcessHeap(), 0, pDWP->winPos,
2238 pDWP->suggestedCount * 2 * sizeof(WINDOWPOS) );
2239 if (!newpos)
2241 retvalue = 0;
2242 goto END;
2244 pDWP->suggestedCount *= 2;
2245 pDWP->winPos = newpos;
2247 pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
2248 pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
2249 pDWP->winPos[pDWP->actualCount].x = x;
2250 pDWP->winPos[pDWP->actualCount].y = y;
2251 pDWP->winPos[pDWP->actualCount].cx = cx;
2252 pDWP->winPos[pDWP->actualCount].cy = cy;
2253 pDWP->winPos[pDWP->actualCount].flags = flags;
2254 pDWP->actualCount++;
2255 END:
2256 release_user_handle_ptr( pDWP );
2257 return retvalue;
2261 /***********************************************************************
2262 * EndDeferWindowPos (USER32.@)
2264 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
2266 DWP *pDWP;
2267 WINDOWPOS *winpos;
2268 BOOL res = TRUE;
2269 int i;
2271 TRACE("%p\n", hdwp);
2273 if (!(pDWP = free_user_handle( hdwp, USER_DWP ))) return FALSE;
2274 if (pDWP == OBJ_OTHER_PROCESS)
2276 FIXME( "other process handle %p?\n", hdwp );
2277 return FALSE;
2280 for (i = 0, winpos = pDWP->winPos; res && i < pDWP->actualCount; i++, winpos++)
2282 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2283 winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
2284 winpos->cx, winpos->cy, winpos->flags);
2286 if (WIN_IsCurrentThread( winpos->hwnd ))
2287 res = USER_SetWindowPos( winpos );
2288 else
2289 res = SendMessageW( winpos->hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)winpos );
2291 HeapFree( GetProcessHeap(), 0, pDWP->winPos );
2292 HeapFree( GetProcessHeap(), 0, pDWP );
2293 return res;
2297 /***********************************************************************
2298 * ArrangeIconicWindows (USER32.@)
2300 UINT WINAPI ArrangeIconicWindows( HWND parent )
2302 RECT rectParent;
2303 HWND hwndChild;
2304 INT x, y, xspacing, yspacing;
2306 GetClientRect( parent, &rectParent );
2307 x = rectParent.left;
2308 y = rectParent.bottom;
2309 xspacing = GetSystemMetrics(SM_CXICONSPACING);
2310 yspacing = GetSystemMetrics(SM_CYICONSPACING);
2312 hwndChild = GetWindow( parent, GW_CHILD );
2313 while (hwndChild)
2315 if( IsIconic( hwndChild ) )
2317 WINPOS_ShowIconTitle( hwndChild, FALSE );
2319 SetWindowPos( hwndChild, 0, x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
2320 y - yspacing - GetSystemMetrics(SM_CYICON)/2, 0, 0,
2321 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
2322 if( IsWindow(hwndChild) )
2323 WINPOS_ShowIconTitle(hwndChild , TRUE );
2325 if (x <= rectParent.right - xspacing) x += xspacing;
2326 else
2328 x = rectParent.left;
2329 y -= yspacing;
2332 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
2334 return yspacing;
2338 /***********************************************************************
2339 * draw_moving_frame
2341 * Draw the frame used when moving or resizing window.
2343 static void draw_moving_frame( HWND parent, HDC hdc, RECT *screen_rect, BOOL thickframe )
2345 RECT rect = *screen_rect;
2347 if (parent) MapWindowPoints( 0, parent, (POINT *)&rect, 2 );
2348 if (thickframe)
2350 const int width = GetSystemMetrics(SM_CXFRAME);
2351 const int height = GetSystemMetrics(SM_CYFRAME);
2353 HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
2354 PatBlt( hdc, rect.left, rect.top,
2355 rect.right - rect.left - width, height, PATINVERT );
2356 PatBlt( hdc, rect.left, rect.top + height, width,
2357 rect.bottom - rect.top - height, PATINVERT );
2358 PatBlt( hdc, rect.left + width, rect.bottom - 1,
2359 rect.right - rect.left - width, -height, PATINVERT );
2360 PatBlt( hdc, rect.right - 1, rect.top, -width,
2361 rect.bottom - rect.top - height, PATINVERT );
2362 SelectObject( hdc, hbrush );
2364 else DrawFocusRect( hdc, &rect );
2368 /***********************************************************************
2369 * start_size_move
2371 * Initialization of a move or resize, when initiated from a menu choice.
2372 * Return hit test code for caption or sizing border.
2374 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
2376 LONG hittest = 0;
2377 POINT pt;
2378 MSG msg;
2379 RECT rectWindow;
2381 GetWindowRect( hwnd, &rectWindow );
2383 if ((wParam & 0xfff0) == SC_MOVE)
2385 /* Move pointer at the center of the caption */
2386 RECT rect = rectWindow;
2387 /* Note: to be exactly centered we should take the different types
2388 * of border into account, but it shouldn't make more than a few pixels
2389 * of difference so let's not bother with that */
2390 rect.top += GetSystemMetrics(SM_CYBORDER);
2391 if (style & WS_SYSMENU)
2392 rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
2393 if (style & WS_MINIMIZEBOX)
2394 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
2395 if (style & WS_MAXIMIZEBOX)
2396 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
2397 pt.x = (rect.right + rect.left) / 2;
2398 pt.y = rect.top + GetSystemMetrics(SM_CYSIZE)/2;
2399 hittest = HTCAPTION;
2400 *capturePoint = pt;
2402 else /* SC_SIZE */
2404 SetCursor( LoadCursorW( 0, (LPWSTR)IDC_SIZEALL ) );
2405 pt.x = pt.y = 0;
2406 while(!hittest)
2408 if (!GetMessageW( &msg, 0, 0, 0 )) return 0;
2409 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2411 switch(msg.message)
2413 case WM_MOUSEMOVE:
2414 pt.x = min( max( msg.pt.x, rectWindow.left ), rectWindow.right - 1 );
2415 pt.y = min( max( msg.pt.y, rectWindow.top ), rectWindow.bottom - 1 );
2416 hittest = SendMessageW( hwnd, WM_NCHITTEST, 0, MAKELONG( pt.x, pt.y ) );
2417 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT)) hittest = 0;
2418 break;
2420 case WM_LBUTTONUP:
2421 return 0;
2423 case WM_KEYDOWN:
2424 switch(msg.wParam)
2426 case VK_UP:
2427 hittest = HTTOP;
2428 pt.x =(rectWindow.left+rectWindow.right)/2;
2429 pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
2430 break;
2431 case VK_DOWN:
2432 hittest = HTBOTTOM;
2433 pt.x =(rectWindow.left+rectWindow.right)/2;
2434 pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
2435 break;
2436 case VK_LEFT:
2437 hittest = HTLEFT;
2438 pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
2439 pt.y =(rectWindow.top+rectWindow.bottom)/2;
2440 break;
2441 case VK_RIGHT:
2442 hittest = HTRIGHT;
2443 pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
2444 pt.y =(rectWindow.top+rectWindow.bottom)/2;
2445 break;
2446 case VK_RETURN:
2447 case VK_ESCAPE:
2448 return 0;
2450 break;
2451 default:
2452 TranslateMessage( &msg );
2453 DispatchMessageW( &msg );
2454 break;
2457 *capturePoint = pt;
2459 SetCursorPos( pt.x, pt.y );
2460 SendMessageW( hwnd, WM_SETCURSOR, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
2461 return hittest;
2465 /***********************************************************************
2466 * WINPOS_SysCommandSizeMove
2468 * Perform SC_MOVE and SC_SIZE commands.
2470 void WINPOS_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
2472 MSG msg;
2473 RECT sizingRect, mouseRect, origRect;
2474 HDC hdc;
2475 HWND parent;
2476 LONG hittest = (LONG)(wParam & 0x0f);
2477 WPARAM syscommand = wParam & 0xfff0;
2478 HCURSOR hDragCursor = 0, hOldCursor = 0;
2479 POINT minTrack, maxTrack;
2480 POINT capturePoint, pt;
2481 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
2482 BOOL thickframe = HAS_THICKFRAME( style );
2483 BOOL iconic = style & WS_MINIMIZE;
2484 BOOL moved = FALSE;
2485 DWORD dwPoint = GetMessagePos ();
2486 BOOL DragFullWindows = TRUE;
2488 if (IsZoomed(hwnd) || !IsWindowVisible(hwnd)) return;
2490 pt.x = (short)LOWORD(dwPoint);
2491 pt.y = (short)HIWORD(dwPoint);
2492 capturePoint = pt;
2494 TRACE("hwnd %p command %04lx, hittest %d, pos %d,%d\n",
2495 hwnd, syscommand, hittest, pt.x, pt.y);
2497 if (syscommand == SC_MOVE)
2499 if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
2500 if (!hittest) return;
2502 else /* SC_SIZE */
2504 if ( hittest && (syscommand != SC_MOUSEMENU) )
2505 hittest += (HTLEFT - WMSZ_LEFT);
2506 else
2508 set_capture_window( hwnd, GUI_INMOVESIZE, NULL );
2509 hittest = start_size_move( hwnd, wParam, &capturePoint, style );
2510 if (!hittest)
2512 set_capture_window( 0, GUI_INMOVESIZE, NULL );
2513 return;
2518 /* Get min/max info */
2520 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
2521 WIN_GetRectangles( hwnd, COORDS_PARENT, &sizingRect, NULL );
2522 origRect = sizingRect;
2523 if (style & WS_CHILD)
2525 parent = GetParent(hwnd);
2526 GetClientRect( parent, &mouseRect );
2527 MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
2528 MapWindowPoints( parent, 0, (LPPOINT)&sizingRect, 2 );
2530 else
2532 parent = 0;
2533 GetClientRect( GetDesktopWindow(), &mouseRect );
2534 mouseRect.left = GetSystemMetrics( SM_XVIRTUALSCREEN );
2535 mouseRect.top = GetSystemMetrics( SM_YVIRTUALSCREEN );
2536 mouseRect.right = mouseRect.left + GetSystemMetrics( SM_CXVIRTUALSCREEN );
2537 mouseRect.bottom = mouseRect.top + GetSystemMetrics( SM_CYVIRTUALSCREEN );
2540 if (ON_LEFT_BORDER(hittest))
2542 mouseRect.left = max( mouseRect.left, sizingRect.right-maxTrack.x );
2543 mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
2545 else if (ON_RIGHT_BORDER(hittest))
2547 mouseRect.left = max( mouseRect.left, sizingRect.left+minTrack.x );
2548 mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
2550 if (ON_TOP_BORDER(hittest))
2552 mouseRect.top = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
2553 mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
2555 else if (ON_BOTTOM_BORDER(hittest))
2557 mouseRect.top = max( mouseRect.top, sizingRect.top+minTrack.y );
2558 mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
2561 /* Retrieve a default cache DC (without using the window style) */
2562 hdc = GetDCEx( parent, 0, DCX_CACHE );
2564 if( iconic ) /* create a cursor for dragging */
2566 hDragCursor = (HCURSOR)GetClassLongPtrW( hwnd, GCLP_HICON);
2567 if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageW( hwnd, WM_QUERYDRAGICON, 0, 0L);
2568 if( !hDragCursor ) iconic = FALSE;
2571 /* we only allow disabling the full window drag for child windows */
2572 if (parent) SystemParametersInfoW( SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0 );
2574 /* repaint the window before moving it around */
2575 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
2577 SendMessageW( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
2578 set_capture_window( hwnd, GUI_INMOVESIZE, NULL );
2580 while(1)
2582 int dx = 0, dy = 0;
2584 if (!GetMessageW( &msg, 0, 0, 0 )) break;
2585 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2587 /* Exit on button-up, Return, or Esc */
2588 if ((msg.message == WM_LBUTTONUP) ||
2589 ((msg.message == WM_KEYDOWN) &&
2590 ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
2592 if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
2594 TranslateMessage( &msg );
2595 DispatchMessageW( &msg );
2596 continue; /* We are not interested in other messages */
2599 pt = msg.pt;
2601 if (msg.message == WM_KEYDOWN) switch(msg.wParam)
2603 case VK_UP: pt.y -= 8; break;
2604 case VK_DOWN: pt.y += 8; break;
2605 case VK_LEFT: pt.x -= 8; break;
2606 case VK_RIGHT: pt.x += 8; break;
2609 pt.x = max( pt.x, mouseRect.left );
2610 pt.x = min( pt.x, mouseRect.right );
2611 pt.y = max( pt.y, mouseRect.top );
2612 pt.y = min( pt.y, mouseRect.bottom );
2614 dx = pt.x - capturePoint.x;
2615 dy = pt.y - capturePoint.y;
2617 if (dx || dy)
2619 if( !moved )
2621 moved = TRUE;
2623 if( iconic ) /* ok, no system popup tracking */
2625 hOldCursor = SetCursor(hDragCursor);
2626 ShowCursor( TRUE );
2627 WINPOS_ShowIconTitle( hwnd, FALSE );
2629 else if(!DragFullWindows)
2630 draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2633 if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
2634 else
2636 WPARAM wpSizingHit = 0;
2638 if(!iconic && !DragFullWindows) draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2639 if (hittest == HTCAPTION) OffsetRect( &sizingRect, dx, dy );
2640 if (ON_LEFT_BORDER(hittest)) sizingRect.left += dx;
2641 else if (ON_RIGHT_BORDER(hittest)) sizingRect.right += dx;
2642 if (ON_TOP_BORDER(hittest)) sizingRect.top += dy;
2643 else if (ON_BOTTOM_BORDER(hittest)) sizingRect.bottom += dy;
2644 capturePoint = pt;
2646 /* determine the hit location */
2647 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
2648 wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
2649 SendMessageW( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&sizingRect );
2651 if (!iconic)
2653 if(!DragFullWindows)
2654 draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2655 else
2657 RECT rect = sizingRect;
2658 MapWindowPoints( 0, parent, (POINT *)&rect, 2 );
2659 SetWindowPos( hwnd, 0, rect.left, rect.top,
2660 rect.right - rect.left, rect.bottom - rect.top,
2661 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2668 if( iconic )
2670 if( moved ) /* restore cursors, show icon title later on */
2672 ShowCursor( FALSE );
2673 SetCursor( hOldCursor );
2676 else if (moved && !DragFullWindows)
2678 draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2681 set_capture_window( 0, GUI_INMOVESIZE, NULL );
2682 ReleaseDC( parent, hdc );
2683 if (parent) MapWindowPoints( 0, parent, (POINT *)&sizingRect, 2 );
2685 if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
2686 moved = FALSE;
2688 SendMessageW( hwnd, WM_EXITSIZEMOVE, 0, 0 );
2689 SendMessageW( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
2691 /* window moved or resized */
2692 if (moved)
2694 /* if the moving/resizing isn't canceled call SetWindowPos
2695 * with the new position or the new size of the window
2697 if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
2699 /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
2700 if(!DragFullWindows || iconic)
2701 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
2702 sizingRect.right - sizingRect.left,
2703 sizingRect.bottom - sizingRect.top,
2704 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2706 else
2707 { /* restore previous size/position */
2708 if(DragFullWindows)
2709 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
2710 origRect.right - origRect.left,
2711 origRect.bottom - origRect.top,
2712 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2716 if (IsIconic(hwnd))
2718 /* Single click brings up the system menu when iconized */
2720 if( !moved )
2722 if(style & WS_SYSMENU )
2723 SendMessageW( hwnd, WM_SYSCOMMAND,
2724 SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
2726 else WINPOS_ShowIconTitle( hwnd, TRUE );