msxml3: Fix ignorable whitespace detection.
[wine.git] / dlls / user32 / winpos.c
blob308e7eae979618015b4782a3e119cd20f243509a
1 /*
2 * Window position related functions.
4 * Copyright 1993, 1994, 1995 Alexandre Julliard
5 * 1995, 1996, 1999 Alex Korobka
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdarg.h>
26 #include <string.h>
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winerror.h"
33 #include "wine/server.h"
34 #include "controls.h"
35 #include "user_private.h"
36 #include "wine/gdi_driver.h"
37 #include "win.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(win);
42 #define SWP_AGG_NOGEOMETRYCHANGE \
43 (SWP_NOSIZE | SWP_NOCLIENTSIZE | SWP_NOZORDER)
44 #define SWP_AGG_NOPOSCHANGE \
45 (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
46 #define SWP_AGG_STATUSFLAGS \
47 (SWP_AGG_NOPOSCHANGE | SWP_FRAMECHANGED | SWP_HIDEWINDOW | SWP_SHOWWINDOW)
49 #define HAS_DLGFRAME(style,exStyle) \
50 (((exStyle) & WS_EX_DLGMODALFRAME) || \
51 (((style) & WS_DLGFRAME) && !((style) & WS_BORDER)))
53 #define HAS_THICKFRAME(style) \
54 (((style) & WS_THICKFRAME) && \
55 !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
57 #define EMPTYPOINT(pt) ((pt).x == -1 && (pt).y == -1)
59 #define ON_LEFT_BORDER(hit) \
60 (((hit) == HTLEFT) || ((hit) == HTTOPLEFT) || ((hit) == HTBOTTOMLEFT))
61 #define ON_RIGHT_BORDER(hit) \
62 (((hit) == HTRIGHT) || ((hit) == HTTOPRIGHT) || ((hit) == HTBOTTOMRIGHT))
63 #define ON_TOP_BORDER(hit) \
64 (((hit) == HTTOP) || ((hit) == HTTOPLEFT) || ((hit) == HTTOPRIGHT))
65 #define ON_BOTTOM_BORDER(hit) \
66 (((hit) == HTBOTTOM) || ((hit) == HTBOTTOMLEFT) || ((hit) == HTBOTTOMRIGHT))
68 #define PLACE_MIN 0x0001
69 #define PLACE_MAX 0x0002
70 #define PLACE_RECT 0x0004
72 typedef struct
74 struct user_object obj;
75 INT actualCount;
76 INT suggestedCount;
77 HWND hwndParent;
78 WINDOWPOS *winPos;
79 } DWP;
82 /***********************************************************************
83 * SwitchToThisWindow (USER32.@)
85 void WINAPI SwitchToThisWindow( HWND hwnd, BOOL alt_tab )
87 if (IsIconic( hwnd )) ShowWindow( hwnd, SW_RESTORE );
88 else BringWindowToTop( hwnd );
92 /***********************************************************************
93 * GetWindowRect (USER32.@)
95 BOOL WINAPI GetWindowRect( HWND hwnd, LPRECT rect )
97 BOOL ret = WIN_GetRectangles( hwnd, COORDS_SCREEN, rect, NULL );
98 if (ret) TRACE( "hwnd %p %s\n", hwnd, wine_dbgstr_rect(rect) );
99 return ret;
103 /***********************************************************************
104 * GetWindowRgn (USER32.@)
106 int WINAPI GetWindowRgn ( HWND hwnd, HRGN hrgn )
108 int nRet = ERROR;
109 NTSTATUS status;
110 HRGN win_rgn = 0;
111 RGNDATA *data;
112 size_t size = 256;
116 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
118 SetLastError( ERROR_OUTOFMEMORY );
119 return ERROR;
121 SERVER_START_REQ( get_window_region )
123 req->window = wine_server_user_handle( hwnd );
124 wine_server_set_reply( req, data->Buffer, size );
125 if (!(status = wine_server_call( req )))
127 size_t reply_size = wine_server_reply_size( reply );
128 if (reply_size)
130 data->rdh.dwSize = sizeof(data->rdh);
131 data->rdh.iType = RDH_RECTANGLES;
132 data->rdh.nCount = reply_size / sizeof(RECT);
133 data->rdh.nRgnSize = reply_size;
134 win_rgn = ExtCreateRegion( NULL, size, data );
137 else size = reply->total_size;
139 SERVER_END_REQ;
140 HeapFree( GetProcessHeap(), 0, data );
141 } while (status == STATUS_BUFFER_OVERFLOW);
143 if (status) SetLastError( RtlNtStatusToDosError(status) );
144 else if (win_rgn)
146 nRet = CombineRgn( hrgn, win_rgn, 0, RGN_COPY );
147 DeleteObject( win_rgn );
149 return nRet;
152 /***********************************************************************
153 * GetWindowRgnBox (USER32.@)
155 int WINAPI GetWindowRgnBox( HWND hwnd, LPRECT prect )
157 int ret = ERROR;
158 HRGN hrgn;
160 if (!prect)
161 return ERROR;
163 if ((hrgn = CreateRectRgn(0, 0, 0, 0)))
165 if ((ret = GetWindowRgn( hwnd, hrgn )) != ERROR )
166 ret = GetRgnBox( hrgn, prect );
167 DeleteObject(hrgn);
170 return ret;
173 /***********************************************************************
174 * SetWindowRgn (USER32.@)
176 int WINAPI SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL bRedraw )
178 static const RECT empty_rect;
179 BOOL ret;
181 if (hrgn)
183 RGNDATA *data;
184 DWORD size;
186 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
187 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
188 if (!GetRegionData( hrgn, size, data ))
190 HeapFree( GetProcessHeap(), 0, data );
191 return FALSE;
193 SERVER_START_REQ( set_window_region )
195 req->window = wine_server_user_handle( hwnd );
196 req->redraw = (bRedraw != 0);
197 if (data->rdh.nCount)
198 wine_server_add_data( req, data->Buffer, data->rdh.nCount * sizeof(RECT) );
199 else
200 wine_server_add_data( req, &empty_rect, sizeof(empty_rect) );
201 ret = !wine_server_call_err( req );
203 SERVER_END_REQ;
204 HeapFree( GetProcessHeap(), 0, data );
206 else /* clear existing region */
208 SERVER_START_REQ( set_window_region )
210 req->window = wine_server_user_handle( hwnd );
211 req->redraw = (bRedraw != 0);
212 ret = !wine_server_call_err( req );
214 SERVER_END_REQ;
217 if (ret)
219 UINT swp_flags = SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE;
220 if (!bRedraw) swp_flags |= SWP_NOREDRAW;
221 SetWindowPos( hwnd, 0, 0, 0, 0, 0, swp_flags );
222 USER_Driver->pSetWindowRgn( hwnd, hrgn, bRedraw );
223 if (hrgn) DeleteObject( hrgn );
225 return ret;
229 /***********************************************************************
230 * GetClientRect (USER32.@)
232 BOOL WINAPI GetClientRect( HWND hwnd, LPRECT rect )
234 return WIN_GetRectangles( hwnd, COORDS_CLIENT, NULL, rect );
238 /***********************************************************************
239 * list_children_from_point
241 * Get the list of children that can contain point from the server.
242 * Point is in screen coordinates.
243 * Returned list must be freed by caller.
245 static HWND *list_children_from_point( HWND hwnd, POINT pt )
247 HWND *list;
248 int i, size = 128;
250 for (;;)
252 int count = 0;
254 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
256 SERVER_START_REQ( get_window_children_from_point )
258 req->parent = wine_server_user_handle( hwnd );
259 req->x = pt.x;
260 req->y = pt.y;
261 wine_server_set_reply( req, list, (size-1) * sizeof(user_handle_t) );
262 if (!wine_server_call( req )) count = reply->count;
264 SERVER_END_REQ;
265 if (count && count < size)
267 /* start from the end since HWND is potentially larger than user_handle_t */
268 for (i = count - 1; i >= 0; i--)
269 list[i] = wine_server_ptr_handle( ((user_handle_t *)list)[i] );
270 list[count] = 0;
271 return list;
273 HeapFree( GetProcessHeap(), 0, list );
274 if (!count) break;
275 size = count + 1; /* restart with a large enough buffer */
277 return NULL;
281 /***********************************************************************
282 * WINPOS_WindowFromPoint
284 * Find the window and hittest for a given point.
286 HWND WINPOS_WindowFromPoint( HWND hwndScope, POINT pt, INT *hittest )
288 int i, res;
289 HWND ret, *list;
291 if (!hwndScope) hwndScope = GetDesktopWindow();
293 *hittest = HTNOWHERE;
295 if (!(list = list_children_from_point( hwndScope, pt ))) return 0;
297 /* now determine the hittest */
299 for (i = 0; list[i]; i++)
301 LONG style = GetWindowLongW( list[i], GWL_STYLE );
303 /* If window is minimized or disabled, return at once */
304 if (style & WS_MINIMIZE)
306 *hittest = HTCAPTION;
307 break;
309 if (style & WS_DISABLED)
311 *hittest = HTERROR;
312 break;
314 /* Send WM_NCCHITTEST (if same thread) */
315 if (!WIN_IsCurrentThread( list[i] ))
317 *hittest = HTCLIENT;
318 break;
320 res = SendMessageW( list[i], WM_NCHITTEST, 0, MAKELONG(pt.x,pt.y) );
321 if (res != HTTRANSPARENT)
323 *hittest = res; /* Found the window */
324 break;
326 /* continue search with next window in z-order */
328 ret = list[i];
329 HeapFree( GetProcessHeap(), 0, list );
330 TRACE( "scope %p (%d,%d) returning %p\n", hwndScope, pt.x, pt.y, ret );
331 return ret;
335 /*******************************************************************
336 * WindowFromPoint (USER32.@)
338 HWND WINAPI WindowFromPoint( POINT pt )
340 INT hittest;
341 return WINPOS_WindowFromPoint( 0, pt, &hittest );
345 /*******************************************************************
346 * ChildWindowFromPoint (USER32.@)
348 HWND WINAPI ChildWindowFromPoint( HWND hwndParent, POINT pt )
350 return ChildWindowFromPointEx( hwndParent, pt, CWP_ALL );
353 /*******************************************************************
354 * RealChildWindowFromPoint (USER32.@)
356 HWND WINAPI RealChildWindowFromPoint( HWND hwndParent, POINT pt )
358 return ChildWindowFromPointEx( hwndParent, pt, CWP_SKIPTRANSPARENT | CWP_SKIPINVISIBLE );
361 /*******************************************************************
362 * ChildWindowFromPointEx (USER32.@)
364 HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
366 /* pt is in the client coordinates */
367 HWND *list;
368 int i;
369 RECT rect;
370 HWND retvalue;
372 GetClientRect( hwndParent, &rect );
373 if (!PtInRect( &rect, pt )) return 0;
374 if (!(list = WIN_ListChildren( hwndParent ))) return hwndParent;
376 for (i = 0; list[i]; i++)
378 if (!WIN_GetRectangles( list[i], COORDS_PARENT, &rect, NULL )) continue;
379 if (!PtInRect( &rect, pt )) continue;
380 if (uFlags & (CWP_SKIPINVISIBLE|CWP_SKIPDISABLED))
382 LONG style = GetWindowLongW( list[i], GWL_STYLE );
383 if ((uFlags & CWP_SKIPINVISIBLE) && !(style & WS_VISIBLE)) continue;
384 if ((uFlags & CWP_SKIPDISABLED) && (style & WS_DISABLED)) continue;
386 if (uFlags & CWP_SKIPTRANSPARENT)
388 if (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TRANSPARENT) continue;
390 break;
392 retvalue = list[i];
393 HeapFree( GetProcessHeap(), 0, list );
394 if (!retvalue) retvalue = hwndParent;
395 return retvalue;
399 /*******************************************************************
400 * WINPOS_GetWinOffset
402 * Calculate the offset between the origin of the two windows. Used
403 * to implement MapWindowPoints.
405 static BOOL WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo, BOOL *mirrored, POINT *ret_offset )
407 WND * wndPtr;
408 POINT offset;
409 BOOL mirror_from, mirror_to, ret;
410 HWND hwnd;
412 offset.x = offset.y = 0;
413 *mirrored = mirror_from = mirror_to = FALSE;
415 /* Translate source window origin to screen coords */
416 if (hwndFrom)
418 if (!(wndPtr = WIN_GetPtr( hwndFrom )))
420 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
421 return FALSE;
423 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
424 if (wndPtr != WND_DESKTOP)
426 if (wndPtr->dwExStyle & WS_EX_LAYOUTRTL)
428 mirror_from = TRUE;
429 offset.x += wndPtr->rectClient.right - wndPtr->rectClient.left;
431 while (wndPtr->parent)
433 offset.x += wndPtr->rectClient.left;
434 offset.y += wndPtr->rectClient.top;
435 hwnd = wndPtr->parent;
436 WIN_ReleasePtr( wndPtr );
437 if (!(wndPtr = WIN_GetPtr( hwnd ))) break;
438 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
439 if (wndPtr == WND_DESKTOP) break;
440 if (wndPtr->flags & WIN_CHILDREN_MOVED)
442 WIN_ReleasePtr( wndPtr );
443 goto other_process;
446 if (wndPtr && wndPtr != WND_DESKTOP) WIN_ReleasePtr( wndPtr );
450 /* Translate origin to destination window coords */
451 if (hwndTo)
453 if (!(wndPtr = WIN_GetPtr( hwndTo )))
455 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
456 return FALSE;
458 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
459 if (wndPtr != WND_DESKTOP)
461 if (wndPtr->dwExStyle & WS_EX_LAYOUTRTL)
463 mirror_to = TRUE;
464 offset.x -= wndPtr->rectClient.right - wndPtr->rectClient.left;
466 while (wndPtr->parent)
468 offset.x -= wndPtr->rectClient.left;
469 offset.y -= wndPtr->rectClient.top;
470 hwnd = wndPtr->parent;
471 WIN_ReleasePtr( wndPtr );
472 if (!(wndPtr = WIN_GetPtr( hwnd ))) break;
473 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
474 if (wndPtr == WND_DESKTOP) break;
475 if (wndPtr->flags & WIN_CHILDREN_MOVED)
477 WIN_ReleasePtr( wndPtr );
478 goto other_process;
481 if (wndPtr && wndPtr != WND_DESKTOP) WIN_ReleasePtr( wndPtr );
485 *mirrored = mirror_from ^ mirror_to;
486 if (mirror_from) offset.x = -offset.x;
487 *ret_offset = offset;
488 return TRUE;
490 other_process: /* one of the parents may belong to another process, do it the hard way */
491 SERVER_START_REQ( get_windows_offset )
493 req->from = wine_server_user_handle( hwndFrom );
494 req->to = wine_server_user_handle( hwndTo );
495 if ((ret = !wine_server_call_err( req )))
497 ret_offset->x = reply->x;
498 ret_offset->y = reply->y;
499 *mirrored = reply->mirror;
502 SERVER_END_REQ;
503 return ret;
506 /* map coordinates of a window region */
507 void map_window_region( HWND from, HWND to, HRGN hrgn )
509 BOOL mirrored;
510 POINT offset;
511 UINT i, size;
512 RGNDATA *data;
513 HRGN new_rgn;
514 RECT *rect;
516 if (!WINPOS_GetWinOffset( from, to, &mirrored, &offset )) return;
518 if (!mirrored)
520 OffsetRgn( hrgn, offset.x, offset.y );
521 return;
523 if (!(size = GetRegionData( hrgn, 0, NULL ))) return;
524 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return;
525 GetRegionData( hrgn, size, data );
526 rect = (RECT *)data->Buffer;
527 for (i = 0; i < data->rdh.nCount; i++)
529 int tmp = -(rect[i].left + offset.x);
530 rect[i].left = -(rect[i].right + offset.x);
531 rect[i].right = tmp;
532 rect[i].top += offset.y;
533 rect[i].bottom += offset.y;
535 if ((new_rgn = ExtCreateRegion( NULL, data->rdh.nCount, data )))
537 CombineRgn( hrgn, new_rgn, 0, RGN_COPY );
538 DeleteObject( new_rgn );
540 HeapFree( GetProcessHeap(), 0, data );
544 /*******************************************************************
545 * MapWindowPoints (USER32.@)
547 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
549 BOOL mirrored;
550 POINT offset;
551 UINT i;
553 if (!WINPOS_GetWinOffset( hwndFrom, hwndTo, &mirrored, &offset )) return 0;
555 for (i = 0; i < count; i++)
557 lppt[i].x += offset.x;
558 lppt[i].y += offset.y;
559 if (mirrored) lppt[i].x = -lppt[i].x;
561 if (mirrored && count == 2) /* special case for rectangle */
563 int tmp = lppt[0].x;
564 lppt[0].x = lppt[1].x;
565 lppt[1].x = tmp;
567 return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
571 /*******************************************************************
572 * ClientToScreen (USER32.@)
574 BOOL WINAPI ClientToScreen( HWND hwnd, LPPOINT lppnt )
576 POINT offset;
577 BOOL mirrored;
579 if (!hwnd)
581 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
582 return FALSE;
584 if (!WINPOS_GetWinOffset( hwnd, 0, &mirrored, &offset )) return FALSE;
585 lppnt->x += offset.x;
586 lppnt->y += offset.y;
587 if (mirrored) lppnt->x = -lppnt->x;
588 return TRUE;
592 /*******************************************************************
593 * ScreenToClient (USER32.@)
595 BOOL WINAPI ScreenToClient( HWND hwnd, LPPOINT lppnt )
597 POINT offset;
598 BOOL mirrored;
600 if (!hwnd)
602 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
603 return FALSE;
605 if (!WINPOS_GetWinOffset( 0, hwnd, &mirrored, &offset )) return FALSE;
606 lppnt->x += offset.x;
607 lppnt->y += offset.y;
608 if (mirrored) lppnt->x = -lppnt->x;
609 return TRUE;
613 /***********************************************************************
614 * IsIconic (USER32.@)
616 BOOL WINAPI IsIconic(HWND hWnd)
618 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
622 /***********************************************************************
623 * IsZoomed (USER32.@)
625 BOOL WINAPI IsZoomed(HWND hWnd)
627 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
631 /*******************************************************************
632 * AllowSetForegroundWindow (USER32.@)
634 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
636 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
637 * implemented, then fix this function. */
638 return TRUE;
642 /*******************************************************************
643 * LockSetForegroundWindow (USER32.@)
645 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
647 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
648 * implemented, then fix this function. */
649 return TRUE;
653 /***********************************************************************
654 * BringWindowToTop (USER32.@)
656 BOOL WINAPI BringWindowToTop( HWND hwnd )
658 return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
662 /***********************************************************************
663 * MoveWindow (USER32.@)
665 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
666 BOOL repaint )
668 int flags = SWP_NOZORDER | SWP_NOACTIVATE;
669 if (!repaint) flags |= SWP_NOREDRAW;
670 TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
671 return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
675 /***********************************************************************
676 * WINPOS_RedrawIconTitle
678 BOOL WINPOS_RedrawIconTitle( HWND hWnd )
680 HWND icon_title = 0;
681 WND *win = WIN_GetPtr( hWnd );
683 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
685 icon_title = win->icon_title;
686 WIN_ReleasePtr( win );
688 if (!icon_title) return FALSE;
689 SendMessageW( icon_title, WM_SHOWWINDOW, TRUE, 0 );
690 InvalidateRect( icon_title, NULL, TRUE );
691 return TRUE;
694 /***********************************************************************
695 * WINPOS_ShowIconTitle
697 static void WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
699 WND *win = WIN_GetPtr( hwnd );
700 HWND title = 0;
702 TRACE("%p %i\n", hwnd, (bShow != 0) );
704 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP) return;
705 if (win->rectWindow.left == -32000 || win->rectWindow.top == -32000)
707 TRACE( "not showing title for hidden icon %p\n", hwnd );
708 bShow = FALSE;
710 else title = win->icon_title;
711 WIN_ReleasePtr( win );
713 if (bShow)
715 if (!title)
717 title = ICONTITLE_Create( hwnd );
718 if (!(win = WIN_GetPtr( hwnd )) || win == WND_OTHER_PROCESS)
720 DestroyWindow( title );
721 return;
723 win->icon_title = title;
724 WIN_ReleasePtr( win );
726 if (!IsWindowVisible(title))
728 SendMessageW( title, WM_SHOWWINDOW, TRUE, 0 );
729 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
730 SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
733 else if (title) ShowWindow( title, SW_HIDE );
736 /*******************************************************************
737 * WINPOS_GetMinMaxInfo
739 * Get the minimized and maximized information for a window.
741 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
742 POINT *minTrack, POINT *maxTrack )
744 MINMAXINFO MinMax;
745 HMONITOR monitor;
746 INT xinc, yinc;
747 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
748 LONG adjustedStyle;
749 LONG exstyle = GetWindowLongW( hwnd, GWL_EXSTYLE );
750 RECT rc;
751 WND *win;
753 /* Compute default values */
755 GetWindowRect(hwnd, &rc);
756 MinMax.ptReserved.x = rc.left;
757 MinMax.ptReserved.y = rc.top;
759 if ((style & WS_CAPTION) == WS_CAPTION)
760 adjustedStyle = style & ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
761 else
762 adjustedStyle = style;
764 GetClientRect(GetAncestor(hwnd,GA_PARENT), &rc);
765 AdjustWindowRectEx(&rc, adjustedStyle, ((style & WS_POPUP) && GetMenu(hwnd)), exstyle);
767 xinc = -rc.left;
768 yinc = -rc.top;
770 MinMax.ptMaxSize.x = rc.right - rc.left;
771 MinMax.ptMaxSize.y = rc.bottom - rc.top;
772 if (style & (WS_DLGFRAME | WS_BORDER))
774 MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
775 MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
777 else
779 MinMax.ptMinTrackSize.x = 2 * xinc;
780 MinMax.ptMinTrackSize.y = 2 * yinc;
782 MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXMAXTRACK);
783 MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYMAXTRACK);
784 MinMax.ptMaxPosition.x = -xinc;
785 MinMax.ptMaxPosition.y = -yinc;
787 if ((win = WIN_GetPtr( hwnd )) && win != WND_DESKTOP && win != WND_OTHER_PROCESS)
789 if (!EMPTYPOINT(win->max_pos)) MinMax.ptMaxPosition = win->max_pos;
790 WIN_ReleasePtr( win );
793 SendMessageW( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
795 /* if the app didn't change the values, adapt them for the current monitor */
797 if ((monitor = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY )))
799 RECT rc_work;
800 MONITORINFO mon_info;
802 mon_info.cbSize = sizeof(mon_info);
803 GetMonitorInfoW( monitor, &mon_info );
805 rc_work = mon_info.rcMonitor;
807 if (style & WS_MAXIMIZEBOX)
809 if ((style & WS_CAPTION) == WS_CAPTION || !(style & (WS_CHILD | WS_POPUP)))
810 rc_work = mon_info.rcWork;
813 if (MinMax.ptMaxSize.x == GetSystemMetrics(SM_CXSCREEN) + 2 * xinc &&
814 MinMax.ptMaxSize.y == GetSystemMetrics(SM_CYSCREEN) + 2 * yinc)
816 MinMax.ptMaxSize.x = (rc_work.right - rc_work.left) + 2 * xinc;
817 MinMax.ptMaxSize.y = (rc_work.bottom - rc_work.top) + 2 * yinc;
819 if (MinMax.ptMaxPosition.x == -xinc && MinMax.ptMaxPosition.y == -yinc)
821 MinMax.ptMaxPosition.x = rc_work.left - xinc;
822 MinMax.ptMaxPosition.y = rc_work.top - yinc;
826 /* Some sanity checks */
828 TRACE("%d %d / %d %d / %d %d / %d %d\n",
829 MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
830 MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
831 MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
832 MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
833 MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
834 MinMax.ptMinTrackSize.x );
835 MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
836 MinMax.ptMinTrackSize.y );
838 if (maxSize) *maxSize = MinMax.ptMaxSize;
839 if (maxPos) *maxPos = MinMax.ptMaxPosition;
840 if (minTrack) *minTrack = MinMax.ptMinTrackSize;
841 if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
845 /***********************************************************************
846 * WINPOS_FindIconPos
848 * Find a suitable place for an iconic window.
850 static POINT WINPOS_FindIconPos( HWND hwnd, POINT pt )
852 RECT rect, rectParent;
853 HWND parent, child;
854 HRGN hrgn, tmp;
855 int xspacing, yspacing;
857 parent = GetAncestor( hwnd, GA_PARENT );
858 GetClientRect( parent, &rectParent );
859 if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
860 (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
861 return pt; /* The icon already has a suitable position */
863 xspacing = GetSystemMetrics(SM_CXICONSPACING);
864 yspacing = GetSystemMetrics(SM_CYICONSPACING);
866 /* Check if another icon already occupies this spot */
867 /* FIXME: this is completely inefficient */
869 hrgn = CreateRectRgn( 0, 0, 0, 0 );
870 tmp = CreateRectRgn( 0, 0, 0, 0 );
871 for (child = GetWindow( parent, GW_HWNDFIRST ); child; child = GetWindow( child, GW_HWNDNEXT ))
873 if (child == hwnd) continue;
874 if ((GetWindowLongW( child, GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != (WS_VISIBLE|WS_MINIMIZE))
875 continue;
876 if (WIN_GetRectangles( child, COORDS_PARENT, &rect, NULL ))
878 SetRectRgn( tmp, rect.left, rect.top, rect.right, rect.bottom );
879 CombineRgn( hrgn, hrgn, tmp, RGN_OR );
882 DeleteObject( tmp );
884 for (rect.bottom = rectParent.bottom; rect.bottom >= yspacing; rect.bottom -= yspacing)
886 for (rect.left = rectParent.left; rect.left <= rectParent.right - xspacing; rect.left += xspacing)
888 rect.right = rect.left + xspacing;
889 rect.top = rect.bottom - yspacing;
890 if (!RectInRegion( hrgn, &rect ))
892 /* No window was found, so it's OK for us */
893 pt.x = rect.left + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
894 pt.y = rect.top + (yspacing - GetSystemMetrics(SM_CYICON)) / 2;
895 DeleteObject( hrgn );
896 return pt;
900 DeleteObject( hrgn );
901 pt.x = pt.y = 0;
902 return pt;
906 /***********************************************************************
907 * WINPOS_MinMaximize
909 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
911 WND *wndPtr;
912 UINT swpFlags = 0;
913 POINT size;
914 LONG old_style;
915 WINDOWPLACEMENT wpl;
917 TRACE("%p %u\n", hwnd, cmd );
919 wpl.length = sizeof(wpl);
920 GetWindowPlacement( hwnd, &wpl );
922 if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
923 return SWP_NOSIZE | SWP_NOMOVE;
925 if (IsIconic( hwnd ))
927 switch (cmd)
929 case SW_SHOWMINNOACTIVE:
930 case SW_SHOWMINIMIZED:
931 case SW_FORCEMINIMIZE:
932 case SW_MINIMIZE:
933 return SWP_NOSIZE | SWP_NOMOVE;
935 if (!SendMessageW( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
936 swpFlags |= SWP_NOCOPYBITS;
939 switch( cmd )
941 case SW_SHOWMINNOACTIVE:
942 case SW_SHOWMINIMIZED:
943 case SW_FORCEMINIMIZE:
944 case SW_MINIMIZE:
945 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
946 if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
947 else wndPtr->flags &= ~WIN_RESTORE_MAX;
948 WIN_ReleasePtr( wndPtr );
950 old_style = WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
952 wpl.ptMinPosition = WINPOS_FindIconPos( hwnd, wpl.ptMinPosition );
954 if (!(old_style & WS_MINIMIZE)) swpFlags |= SWP_STATECHANGED;
955 SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
956 wpl.ptMinPosition.x + GetSystemMetrics(SM_CXICON),
957 wpl.ptMinPosition.y + GetSystemMetrics(SM_CYICON) );
958 swpFlags |= SWP_NOCOPYBITS;
959 break;
961 case SW_MAXIMIZE:
962 old_style = GetWindowLongW( hwnd, GWL_STYLE );
963 if ((old_style & WS_MAXIMIZE) && (old_style & WS_VISIBLE)) return SWP_NOSIZE | SWP_NOMOVE;
965 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
967 old_style = WIN_SetStyle( hwnd, WS_MAXIMIZE, WS_MINIMIZE );
968 if (old_style & WS_MINIMIZE)
970 if ((wndPtr = WIN_GetPtr( hwnd )) && wndPtr != WND_OTHER_PROCESS)
972 wndPtr->flags |= WIN_RESTORE_MAX;
973 WIN_ReleasePtr( wndPtr );
975 WINPOS_ShowIconTitle( hwnd, FALSE );
978 if (!(old_style & WS_MAXIMIZE)) swpFlags |= SWP_STATECHANGED;
979 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y,
980 wpl.ptMaxPosition.x + size.x, wpl.ptMaxPosition.y + size.y );
981 break;
983 case SW_SHOWNOACTIVATE:
984 if ((wndPtr = WIN_GetPtr( hwnd )) && wndPtr != WND_OTHER_PROCESS)
986 wndPtr->flags &= ~WIN_RESTORE_MAX;
987 WIN_ReleasePtr( wndPtr );
989 /* fall through */
990 case SW_SHOWNORMAL:
991 case SW_RESTORE:
992 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
993 old_style = WIN_SetStyle( hwnd, 0, WS_MINIMIZE | WS_MAXIMIZE );
994 if (old_style & WS_MINIMIZE)
996 BOOL restore_max;
998 WINPOS_ShowIconTitle( hwnd, FALSE );
1000 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
1001 restore_max = (wndPtr->flags & WIN_RESTORE_MAX) != 0;
1002 WIN_ReleasePtr( wndPtr );
1003 if (restore_max)
1005 /* Restore to maximized position */
1006 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
1007 WIN_SetStyle( hwnd, WS_MAXIMIZE, 0 );
1008 swpFlags |= SWP_STATECHANGED;
1009 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y,
1010 wpl.ptMaxPosition.x + size.x, wpl.ptMaxPosition.y + size.y );
1011 break;
1014 else if (!(old_style & WS_MAXIMIZE)) break;
1016 swpFlags |= SWP_STATECHANGED;
1018 /* Restore to normal position */
1020 *rect = wpl.rcNormalPosition;
1021 break;
1024 return swpFlags;
1028 /***********************************************************************
1029 * show_window
1031 * Implementation of ShowWindow and ShowWindowAsync.
1033 static BOOL show_window( HWND hwnd, INT cmd )
1035 WND *wndPtr;
1036 HWND parent;
1037 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1038 BOOL wasVisible = (style & WS_VISIBLE) != 0;
1039 BOOL showFlag = TRUE;
1040 RECT newPos = {0, 0, 0, 0};
1041 UINT swp = 0;
1043 TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);
1045 switch(cmd)
1047 case SW_HIDE:
1048 if (!wasVisible) return FALSE;
1049 showFlag = FALSE;
1050 swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1051 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1052 break;
1054 case SW_SHOWMINNOACTIVE:
1055 case SW_MINIMIZE:
1056 case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
1057 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1058 /* fall through */
1059 case SW_SHOWMINIMIZED:
1060 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1061 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
1062 if ((style & WS_MINIMIZE) && wasVisible) return TRUE;
1063 break;
1065 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
1066 if (!wasVisible) swp |= SWP_SHOWWINDOW;
1067 swp |= SWP_FRAMECHANGED;
1068 swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
1069 if ((style & WS_MAXIMIZE) && wasVisible) return TRUE;
1070 break;
1072 case SW_SHOWNA:
1073 swp |= SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1074 if (style & WS_CHILD) swp |= SWP_NOZORDER;
1075 break;
1076 case SW_SHOW:
1077 if (wasVisible) return TRUE;
1078 swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1079 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1080 break;
1082 case SW_SHOWNOACTIVATE:
1083 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1084 /* fall through */
1085 case SW_RESTORE:
1086 /* fall through */
1087 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
1088 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
1089 if (!wasVisible) swp |= SWP_SHOWWINDOW;
1090 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1092 swp |= SWP_FRAMECHANGED;
1093 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
1095 else
1097 if (wasVisible) return TRUE;
1098 swp |= SWP_NOSIZE | SWP_NOMOVE;
1100 if (style & WS_CHILD && !(swp & SWP_STATECHANGED)) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1101 break;
1102 default:
1103 return wasVisible;
1106 if ((showFlag != wasVisible || cmd == SW_SHOWNA) && cmd != SW_SHOWMAXIMIZED && !(swp & SWP_STATECHANGED))
1108 SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
1109 if (!IsWindow( hwnd )) return wasVisible;
1112 swp = USER_Driver->pShowWindow( hwnd, cmd, &newPos, swp );
1114 parent = GetAncestor( hwnd, GA_PARENT );
1115 if (parent && !IsWindowVisible( parent ) && !(swp & SWP_STATECHANGED))
1117 /* if parent is not visible simply toggle WS_VISIBLE and return */
1118 if (showFlag) WIN_SetStyle( hwnd, WS_VISIBLE, 0 );
1119 else WIN_SetStyle( hwnd, 0, WS_VISIBLE );
1121 else
1122 SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1123 newPos.right - newPos.left, newPos.bottom - newPos.top, swp );
1125 if (cmd == SW_HIDE)
1127 HWND hFocus;
1129 WINPOS_ShowIconTitle( hwnd, FALSE );
1131 /* FIXME: This will cause the window to be activated irrespective
1132 * of whether it is owned by the same thread. Has to be done
1133 * asynchronously.
1136 if (hwnd == GetActiveWindow())
1137 WINPOS_ActivateOtherWindow(hwnd);
1139 /* Revert focus to parent */
1140 hFocus = GetFocus();
1141 if (hwnd == hFocus)
1143 HWND parent = GetAncestor(hwnd, GA_PARENT);
1144 if (parent == GetDesktopWindow()) parent = 0;
1145 SetFocus(parent);
1147 return wasVisible;
1150 if (IsIconic(hwnd)) WINPOS_ShowIconTitle( hwnd, TRUE );
1152 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return wasVisible;
1154 if (wndPtr->flags & WIN_NEED_SIZE)
1156 /* should happen only in CreateWindowEx() */
1157 int wParam = SIZE_RESTORED;
1158 RECT client;
1159 LPARAM lparam;
1161 WIN_GetRectangles( hwnd, COORDS_PARENT, NULL, &client );
1162 lparam = MAKELONG( client.right - client.left, client.bottom - client.top );
1163 wndPtr->flags &= ~WIN_NEED_SIZE;
1164 if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1165 else if (wndPtr->dwStyle & WS_MINIMIZE)
1167 wParam = SIZE_MINIMIZED;
1168 lparam = 0;
1170 WIN_ReleasePtr( wndPtr );
1172 SendMessageW( hwnd, WM_SIZE, wParam, lparam );
1173 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( client.left, client.top ));
1175 else WIN_ReleasePtr( wndPtr );
1177 /* if previous state was minimized Windows sets focus to the window */
1178 if (style & WS_MINIMIZE) SetFocus( hwnd );
1180 return wasVisible;
1184 /***********************************************************************
1185 * ShowWindowAsync (USER32.@)
1187 * doesn't wait; returns immediately.
1188 * used by threads to toggle windows in other (possibly hanging) threads
1190 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
1192 HWND full_handle;
1194 if (is_broadcast(hwnd))
1196 SetLastError( ERROR_INVALID_PARAMETER );
1197 return FALSE;
1200 if ((full_handle = WIN_IsCurrentThread( hwnd )))
1201 return show_window( full_handle, cmd );
1203 return SendNotifyMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
1207 /***********************************************************************
1208 * ShowWindow (USER32.@)
1210 BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
1212 HWND full_handle;
1214 if (is_broadcast(hwnd))
1216 SetLastError( ERROR_INVALID_PARAMETER );
1217 return FALSE;
1219 if ((full_handle = WIN_IsCurrentThread( hwnd )))
1220 return show_window( full_handle, cmd );
1222 return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
1226 /***********************************************************************
1227 * GetInternalWindowPos (USER32.@)
1229 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
1230 LPPOINT ptIcon )
1232 WINDOWPLACEMENT wndpl;
1233 if (GetWindowPlacement( hwnd, &wndpl ))
1235 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
1236 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
1237 return wndpl.showCmd;
1239 return 0;
1243 /***********************************************************************
1244 * GetWindowPlacement (USER32.@)
1246 * Win95:
1247 * Fails if wndpl->length of Win95 (!) apps is invalid.
1249 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
1251 WND *pWnd = WIN_GetPtr( hwnd );
1253 if (!pWnd) return FALSE;
1255 if (pWnd == WND_DESKTOP)
1257 wndpl->length = sizeof(*wndpl);
1258 wndpl->showCmd = SW_SHOWNORMAL;
1259 wndpl->flags = 0;
1260 wndpl->ptMinPosition.x = -1;
1261 wndpl->ptMinPosition.y = -1;
1262 wndpl->ptMaxPosition.x = -1;
1263 wndpl->ptMaxPosition.y = -1;
1264 GetWindowRect( hwnd, &wndpl->rcNormalPosition );
1265 return TRUE;
1267 if (pWnd == WND_OTHER_PROCESS)
1269 if (!IsWindow( hwnd )) return FALSE;
1270 FIXME( "not supported on other process window %p\n", hwnd );
1271 /* provide some dummy information */
1272 wndpl->length = sizeof(*wndpl);
1273 wndpl->showCmd = SW_SHOWNORMAL;
1274 wndpl->flags = 0;
1275 wndpl->ptMinPosition.x = -1;
1276 wndpl->ptMinPosition.y = -1;
1277 wndpl->ptMaxPosition.x = -1;
1278 wndpl->ptMaxPosition.y = -1;
1279 GetWindowRect( hwnd, &wndpl->rcNormalPosition );
1280 return TRUE;
1283 /* update the placement according to the current style */
1284 if (pWnd->dwStyle & WS_MINIMIZE)
1286 pWnd->min_pos.x = pWnd->rectWindow.left;
1287 pWnd->min_pos.y = pWnd->rectWindow.top;
1289 else if (pWnd->dwStyle & WS_MAXIMIZE)
1291 pWnd->max_pos.x = pWnd->rectWindow.left;
1292 pWnd->max_pos.y = pWnd->rectWindow.top;
1294 else
1296 pWnd->normal_rect = pWnd->rectWindow;
1299 wndpl->length = sizeof(*wndpl);
1300 if( pWnd->dwStyle & WS_MINIMIZE )
1301 wndpl->showCmd = SW_SHOWMINIMIZED;
1302 else
1303 wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
1304 if( pWnd->flags & WIN_RESTORE_MAX )
1305 wndpl->flags = WPF_RESTORETOMAXIMIZED;
1306 else
1307 wndpl->flags = 0;
1308 wndpl->ptMinPosition = pWnd->min_pos;
1309 wndpl->ptMaxPosition = pWnd->max_pos;
1310 wndpl->rcNormalPosition = pWnd->normal_rect;
1311 WIN_ReleasePtr( pWnd );
1313 TRACE( "%p: returning min %d,%d max %d,%d normal %s\n",
1314 hwnd, wndpl->ptMinPosition.x, wndpl->ptMinPosition.y,
1315 wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y,
1316 wine_dbgstr_rect(&wndpl->rcNormalPosition) );
1317 return TRUE;
1320 /* make sure the specified rect is visible on screen */
1321 static void make_rect_onscreen( RECT *rect )
1323 MONITORINFO info;
1324 HMONITOR monitor = MonitorFromRect( rect, MONITOR_DEFAULTTONEAREST );
1326 info.cbSize = sizeof(info);
1327 if (!monitor || !GetMonitorInfoW( monitor, &info )) return;
1328 /* FIXME: map coordinates from rcWork to rcMonitor */
1329 if (rect->right <= info.rcWork.left)
1331 rect->right += info.rcWork.left - rect->left;
1332 rect->left = info.rcWork.left;
1334 else if (rect->left >= info.rcWork.right)
1336 rect->left += info.rcWork.right - rect->right;
1337 rect->right = info.rcWork.right;
1339 if (rect->bottom <= info.rcWork.top)
1341 rect->bottom += info.rcWork.top - rect->top;
1342 rect->top = info.rcWork.top;
1344 else if (rect->top >= info.rcWork.bottom)
1346 rect->top += info.rcWork.bottom - rect->bottom;
1347 rect->bottom = info.rcWork.bottom;
1351 /* make sure the specified point is visible on screen */
1352 static void make_point_onscreen( POINT *pt )
1354 RECT rect;
1356 SetRect( &rect, pt->x, pt->y, pt->x + 1, pt->y + 1 );
1357 make_rect_onscreen( &rect );
1358 pt->x = rect.left;
1359 pt->y = rect.top;
1363 /***********************************************************************
1364 * WINPOS_SetPlacement
1366 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
1368 DWORD style;
1369 WND *pWnd = WIN_GetPtr( hwnd );
1370 WINDOWPLACEMENT wp = *wndpl;
1372 if (flags & PLACE_MIN) make_point_onscreen( &wp.ptMinPosition );
1373 if (flags & PLACE_MAX) make_point_onscreen( &wp.ptMaxPosition );
1374 if (flags & PLACE_RECT) make_rect_onscreen( &wp.rcNormalPosition );
1376 TRACE( "%p: setting min %d,%d max %d,%d normal %s flags %x ajusted to min %d,%d max %d,%d normal %s\n",
1377 hwnd, wndpl->ptMinPosition.x, wndpl->ptMinPosition.y,
1378 wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y,
1379 wine_dbgstr_rect(&wndpl->rcNormalPosition), flags,
1380 wp.ptMinPosition.x, wp.ptMinPosition.y, wp.ptMaxPosition.x, wp.ptMaxPosition.y,
1381 wine_dbgstr_rect(&wp.rcNormalPosition) );
1383 if (!pWnd || pWnd == WND_OTHER_PROCESS || pWnd == WND_DESKTOP) return FALSE;
1385 if( flags & PLACE_MIN ) pWnd->min_pos = wp.ptMinPosition;
1386 if( flags & PLACE_MAX ) pWnd->max_pos = wp.ptMaxPosition;
1387 if( flags & PLACE_RECT) pWnd->normal_rect = wp.rcNormalPosition;
1389 style = pWnd->dwStyle;
1391 WIN_ReleasePtr( pWnd );
1393 if( style & WS_MINIMIZE )
1395 if (flags & PLACE_MIN)
1397 WINPOS_ShowIconTitle( hwnd, FALSE );
1398 SetWindowPos( hwnd, 0, wp.ptMinPosition.x, wp.ptMinPosition.y, 0, 0,
1399 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1402 else if( style & WS_MAXIMIZE )
1404 if (flags & PLACE_MAX)
1405 SetWindowPos( hwnd, 0, wp.ptMaxPosition.x, wp.ptMaxPosition.y, 0, 0,
1406 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1408 else if( flags & PLACE_RECT )
1409 SetWindowPos( hwnd, 0, wp.rcNormalPosition.left, wp.rcNormalPosition.top,
1410 wp.rcNormalPosition.right - wp.rcNormalPosition.left,
1411 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top,
1412 SWP_NOZORDER | SWP_NOACTIVATE );
1414 ShowWindow( hwnd, wndpl->showCmd );
1416 if (IsIconic( hwnd ))
1418 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE) WINPOS_ShowIconTitle( hwnd, TRUE );
1420 /* SDK: ...valid only the next time... */
1421 if( wndpl->flags & WPF_RESTORETOMAXIMIZED )
1423 pWnd = WIN_GetPtr( hwnd );
1424 if (pWnd && pWnd != WND_OTHER_PROCESS)
1426 pWnd->flags |= WIN_RESTORE_MAX;
1427 WIN_ReleasePtr( pWnd );
1431 return TRUE;
1435 /***********************************************************************
1436 * SetWindowPlacement (USER32.@)
1438 * Win95:
1439 * Fails if wndpl->length of Win95 (!) apps is invalid.
1441 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
1443 UINT flags = PLACE_MAX | PLACE_RECT;
1444 if (!wpl) return FALSE;
1445 if (wpl->flags & WPF_SETMINPOSITION) flags |= PLACE_MIN;
1446 return WINPOS_SetPlacement( hwnd, wpl, flags );
1450 /***********************************************************************
1451 * AnimateWindow (USER32.@)
1452 * Shows/Hides a window with an animation
1453 * NO ANIMATION YET
1455 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
1457 FIXME("partial stub\n");
1459 /* If trying to show/hide and it's already *
1460 * shown/hidden or invalid window, fail with *
1461 * invalid parameter */
1462 if(!IsWindow(hwnd) ||
1463 (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
1464 (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
1466 SetLastError(ERROR_INVALID_PARAMETER);
1467 return FALSE;
1470 ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1472 return TRUE;
1475 /***********************************************************************
1476 * SetInternalWindowPos (USER32.@)
1478 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1479 LPRECT rect, LPPOINT pt )
1481 WINDOWPLACEMENT wndpl;
1482 UINT flags;
1484 wndpl.length = sizeof(wndpl);
1485 wndpl.showCmd = showCmd;
1486 wndpl.flags = flags = 0;
1488 if( pt )
1490 flags |= PLACE_MIN;
1491 wndpl.flags |= WPF_SETMINPOSITION;
1492 wndpl.ptMinPosition = *pt;
1494 if( rect )
1496 flags |= PLACE_RECT;
1497 wndpl.rcNormalPosition = *rect;
1499 WINPOS_SetPlacement( hwnd, &wndpl, flags );
1503 /*******************************************************************
1504 * can_activate_window
1506 * Check if we can activate the specified window.
1508 static BOOL can_activate_window( HWND hwnd )
1510 LONG style;
1512 if (!hwnd) return FALSE;
1513 style = GetWindowLongW( hwnd, GWL_STYLE );
1514 if (!(style & WS_VISIBLE)) return FALSE;
1515 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1516 return !(style & WS_DISABLED);
1520 /*******************************************************************
1521 * WINPOS_ActivateOtherWindow
1523 * Activates window other than pWnd.
1525 void WINPOS_ActivateOtherWindow(HWND hwnd)
1527 HWND hwndTo, fg;
1529 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1531 hwndTo = GetAncestor( hwndTo, GA_ROOT );
1532 if (can_activate_window( hwndTo )) goto done;
1535 hwndTo = hwnd;
1536 for (;;)
1538 if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1539 if (can_activate_window( hwndTo )) break;
1542 done:
1543 fg = GetForegroundWindow();
1544 TRACE("win = %p fg = %p\n", hwndTo, fg);
1545 if (!fg || (hwnd == fg))
1547 if (SetForegroundWindow( hwndTo )) return;
1549 if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1553 /***********************************************************************
1554 * WINPOS_HandleWindowPosChanging
1556 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1558 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1560 POINT minTrack, maxTrack;
1561 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1563 if (winpos->flags & SWP_NOSIZE) return 0;
1564 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1566 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1567 if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1568 if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1569 if (!(style & WS_MINIMIZE))
1571 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1572 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1575 return 0;
1579 /***********************************************************************
1580 * dump_winpos_flags
1582 static void dump_winpos_flags(UINT flags)
1584 static const DWORD dumped_flags = (SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOREDRAW |
1585 SWP_NOACTIVATE | SWP_FRAMECHANGED | SWP_SHOWWINDOW |
1586 SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOOWNERZORDER |
1587 SWP_NOSENDCHANGING | SWP_DEFERERASE | SWP_ASYNCWINDOWPOS |
1588 SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_STATECHANGED);
1589 TRACE("flags:");
1590 if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1591 if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1592 if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1593 if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1594 if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1595 if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1596 if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1597 if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1598 if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1599 if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1600 if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1601 if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1602 if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1603 if(flags & SWP_NOCLIENTSIZE) TRACE(" SWP_NOCLIENTSIZE");
1604 if(flags & SWP_NOCLIENTMOVE) TRACE(" SWP_NOCLIENTMOVE");
1605 if(flags & SWP_STATECHANGED) TRACE(" SWP_STATECHANGED");
1607 if(flags & ~dumped_flags) TRACE(" %08x", flags & ~dumped_flags);
1608 TRACE("\n");
1611 /***********************************************************************
1612 * SWP_DoWinPosChanging
1614 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
1616 WND *wndPtr;
1617 RECT window_rect, client_rect;
1619 /* Send WM_WINDOWPOSCHANGING message */
1621 if (!(pWinpos->flags & SWP_NOSENDCHANGING))
1622 SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
1624 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) ||
1625 wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
1627 /* Calculate new position and size */
1629 WIN_GetRectangles( pWinpos->hwnd, COORDS_PARENT, &window_rect, &client_rect );
1630 *pNewWindowRect = window_rect;
1631 *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? window_rect : client_rect;
1633 if (!(pWinpos->flags & SWP_NOSIZE))
1635 if (wndPtr->dwStyle & WS_MINIMIZE)
1637 pNewWindowRect->right = pNewWindowRect->left + GetSystemMetrics(SM_CXICON);
1638 pNewWindowRect->bottom = pNewWindowRect->top + GetSystemMetrics(SM_CYICON);
1640 else
1642 pNewWindowRect->right = pNewWindowRect->left + pWinpos->cx;
1643 pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
1646 if (!(pWinpos->flags & SWP_NOMOVE))
1648 pNewWindowRect->left = pWinpos->x;
1649 pNewWindowRect->top = pWinpos->y;
1650 pNewWindowRect->right += pWinpos->x - window_rect.left;
1651 pNewWindowRect->bottom += pWinpos->y - window_rect.top;
1653 OffsetRect( pNewClientRect, pWinpos->x - window_rect.left,
1654 pWinpos->y - window_rect.top );
1656 pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
1658 TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
1659 pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
1660 pWinpos->cx, pWinpos->cy, pWinpos->flags );
1661 TRACE( "current %s style %08x new %s\n",
1662 wine_dbgstr_rect( &window_rect ), wndPtr->dwStyle,
1663 wine_dbgstr_rect( pNewWindowRect ));
1665 WIN_ReleasePtr( wndPtr );
1666 return TRUE;
1669 /***********************************************************************
1670 * get_valid_rects
1672 * Compute the valid rects from the old and new client rect and WVR_* flags.
1673 * Helper for WM_NCCALCSIZE handling.
1675 static inline void get_valid_rects( const RECT *old_client, const RECT *new_client, UINT flags,
1676 RECT *valid )
1678 int cx, cy;
1680 if (flags & WVR_REDRAW)
1682 SetRectEmpty( &valid[0] );
1683 SetRectEmpty( &valid[1] );
1684 return;
1687 if (flags & WVR_VALIDRECTS)
1689 if (!IntersectRect( &valid[0], &valid[0], new_client ) ||
1690 !IntersectRect( &valid[1], &valid[1], old_client ))
1692 SetRectEmpty( &valid[0] );
1693 SetRectEmpty( &valid[1] );
1694 return;
1696 flags = WVR_ALIGNLEFT | WVR_ALIGNTOP;
1698 else
1700 valid[0] = *new_client;
1701 valid[1] = *old_client;
1704 /* make sure the rectangles have the same size */
1705 cx = min( valid[0].right - valid[0].left, valid[1].right - valid[1].left );
1706 cy = min( valid[0].bottom - valid[0].top, valid[1].bottom - valid[1].top );
1708 if (flags & WVR_ALIGNBOTTOM)
1710 valid[0].top = valid[0].bottom - cy;
1711 valid[1].top = valid[1].bottom - cy;
1713 else
1715 valid[0].bottom = valid[0].top + cy;
1716 valid[1].bottom = valid[1].top + cy;
1718 if (flags & WVR_ALIGNRIGHT)
1720 valid[0].left = valid[0].right - cx;
1721 valid[1].left = valid[1].right - cx;
1723 else
1725 valid[0].right = valid[0].left + cx;
1726 valid[1].right = valid[1].left + cx;
1731 /***********************************************************************
1732 * SWP_DoOwnedPopups
1734 * fix Z order taking into account owned popups -
1735 * basically we need to maintain them above the window that owns them
1737 * FIXME: hide/show owned popups when owner visibility changes.
1739 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
1741 HWND owner, *list = NULL;
1742 unsigned int i;
1744 TRACE("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
1746 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) return hwndInsertAfter;
1748 if ((owner = GetWindow( hwnd, GW_OWNER )))
1750 /* make sure this popup stays above the owner */
1752 if (hwndInsertAfter != HWND_TOPMOST)
1754 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return hwndInsertAfter;
1756 for (i = 0; list[i]; i++)
1758 BOOL topmost = (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST) != 0;
1760 if (list[i] == owner)
1762 if (i > 0) hwndInsertAfter = list[i-1];
1763 else hwndInsertAfter = topmost ? HWND_TOPMOST : HWND_TOP;
1764 break;
1767 if (hwndInsertAfter == HWND_TOP || hwndInsertAfter == HWND_NOTOPMOST)
1769 if (!topmost) break;
1771 else if (list[i] == hwndInsertAfter) break;
1776 if (hwndInsertAfter == HWND_BOTTOM) goto done;
1777 if (!list && !(list = WIN_ListChildren( GetDesktopWindow() ))) goto done;
1779 i = 0;
1780 if (hwndInsertAfter == HWND_TOP || hwndInsertAfter == HWND_NOTOPMOST)
1782 if (hwndInsertAfter == HWND_NOTOPMOST || !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_TOPMOST))
1784 /* skip all the topmost windows */
1785 while (list[i] && (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST)) i++;
1788 else if (hwndInsertAfter != HWND_TOPMOST)
1790 /* skip windows that are already placed correctly */
1791 for (i = 0; list[i]; i++)
1793 if (list[i] == hwndInsertAfter) break;
1794 if (list[i] == hwnd) goto done; /* nothing to do if window is moving backwards in z-order */
1798 for ( ; list[i]; i++)
1800 if (list[i] == hwnd) break;
1801 if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
1802 TRACE( "moving %p owned by %p after %p\n", list[i], hwnd, hwndInsertAfter );
1803 SetWindowPos( list[i], hwndInsertAfter, 0, 0, 0, 0,
1804 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING | SWP_DEFERERASE );
1805 hwndInsertAfter = list[i];
1808 done:
1809 HeapFree( GetProcessHeap(), 0, list );
1810 return hwndInsertAfter;
1813 /***********************************************************************
1814 * SWP_DoNCCalcSize
1816 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect,
1817 RECT *validRects )
1819 UINT wvrFlags = 0;
1820 RECT window_rect, client_rect;
1822 WIN_GetRectangles( pWinpos->hwnd, COORDS_PARENT, &window_rect, &client_rect );
1824 /* Send WM_NCCALCSIZE message to get new client area */
1825 if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
1827 NCCALCSIZE_PARAMS params;
1828 WINDOWPOS winposCopy;
1830 params.rgrc[0] = *pNewWindowRect;
1831 params.rgrc[1] = window_rect;
1832 params.rgrc[2] = client_rect;
1833 params.lppos = &winposCopy;
1834 winposCopy = *pWinpos;
1836 wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
1838 *pNewClientRect = params.rgrc[0];
1840 TRACE( "hwnd %p old win %s old client %s new win %s new client %s\n", pWinpos->hwnd,
1841 wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(&client_rect),
1842 wine_dbgstr_rect(pNewWindowRect), wine_dbgstr_rect(pNewClientRect) );
1844 if( pNewClientRect->left != client_rect.left ||
1845 pNewClientRect->top != client_rect.top )
1846 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1848 if( (pNewClientRect->right - pNewClientRect->left !=
1849 client_rect.right - client_rect.left))
1850 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1851 else
1852 wvrFlags &= ~WVR_HREDRAW;
1854 if (pNewClientRect->bottom - pNewClientRect->top !=
1855 client_rect.bottom - client_rect.top)
1856 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1857 else
1858 wvrFlags &= ~WVR_VREDRAW;
1860 validRects[0] = params.rgrc[1];
1861 validRects[1] = params.rgrc[2];
1863 else
1865 if (!(pWinpos->flags & SWP_NOMOVE) &&
1866 (pNewClientRect->left != client_rect.left ||
1867 pNewClientRect->top != client_rect.top))
1868 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1871 if (pWinpos->flags & (SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
1873 SetRectEmpty( &validRects[0] );
1874 SetRectEmpty( &validRects[1] );
1876 else get_valid_rects( &client_rect, pNewClientRect, wvrFlags, validRects );
1878 return wvrFlags;
1881 /* fix redundant flags and values in the WINDOWPOS structure */
1882 static BOOL fixup_flags( WINDOWPOS *winpos )
1884 HWND parent;
1885 RECT window_rect;
1886 POINT pt;
1887 WND *wndPtr = WIN_GetPtr( winpos->hwnd );
1888 BOOL ret = TRUE;
1890 if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
1892 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1893 return FALSE;
1895 winpos->hwnd = wndPtr->obj.handle; /* make it a full handle */
1897 /* Finally make sure that all coordinates are valid */
1898 if (winpos->x < -32768) winpos->x = -32768;
1899 else if (winpos->x > 32767) winpos->x = 32767;
1900 if (winpos->y < -32768) winpos->y = -32768;
1901 else if (winpos->y > 32767) winpos->y = 32767;
1903 if (winpos->cx < 0) winpos->cx = 0;
1904 else if (winpos->cx > 32767) winpos->cx = 32767;
1905 if (winpos->cy < 0) winpos->cy = 0;
1906 else if (winpos->cy > 32767) winpos->cy = 32767;
1908 parent = GetAncestor( winpos->hwnd, GA_PARENT );
1909 if (!IsWindowVisible( parent )) winpos->flags |= SWP_NOREDRAW;
1911 if (winpos->flags & SWP_HIDEWINDOW) wndPtr->flags |= WIN_HIDDEN;
1912 else if (winpos->flags & SWP_SHOWWINDOW) wndPtr->flags &= ~WIN_HIDDEN;
1914 if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
1915 else
1917 winpos->flags &= ~SWP_HIDEWINDOW;
1918 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
1921 WIN_GetRectangles( winpos->hwnd, COORDS_SCREEN, &window_rect, NULL );
1922 if ((window_rect.right - window_rect.left == winpos->cx) &&
1923 (window_rect.bottom - window_rect.top == winpos->cy))
1924 winpos->flags |= SWP_NOSIZE; /* Already the right size */
1926 pt.x = winpos->x;
1927 pt.y = winpos->y;
1928 ClientToScreen( parent, &pt );
1929 if ((window_rect.left == pt.x) && (window_rect.top == pt.y))
1930 winpos->flags |= SWP_NOMOVE; /* Already the right position */
1932 if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
1934 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)) && /* Bring to the top when activating */
1935 (winpos->flags & SWP_NOZORDER ||
1936 (winpos->hwndInsertAfter != HWND_TOPMOST && winpos->hwndInsertAfter != HWND_NOTOPMOST)))
1938 winpos->flags &= ~SWP_NOZORDER;
1939 winpos->hwndInsertAfter = HWND_TOP;
1943 /* Check hwndInsertAfter */
1944 if (winpos->flags & SWP_NOZORDER) goto done;
1946 if (winpos->hwndInsertAfter == HWND_TOP)
1948 if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1949 winpos->flags |= SWP_NOZORDER;
1951 else if (winpos->hwndInsertAfter == HWND_BOTTOM)
1953 if (!(wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
1954 winpos->flags |= SWP_NOZORDER;
1956 else if (winpos->hwndInsertAfter == HWND_TOPMOST)
1958 if ((wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1959 winpos->flags |= SWP_NOZORDER;
1961 else if (winpos->hwndInsertAfter == HWND_NOTOPMOST)
1963 if (!(wndPtr->dwExStyle & WS_EX_TOPMOST))
1964 winpos->flags |= SWP_NOZORDER;
1966 else
1968 if ((winpos->hwnd == winpos->hwndInsertAfter) ||
1969 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
1970 winpos->flags |= SWP_NOZORDER;
1972 done:
1973 WIN_ReleasePtr( wndPtr );
1974 return ret;
1978 /***********************************************************************
1979 * update_surface_region
1981 static void update_surface_region( HWND hwnd )
1983 NTSTATUS status;
1984 HRGN region = 0;
1985 RGNDATA *data;
1986 size_t size = 256;
1987 WND *win = WIN_GetPtr( hwnd );
1989 if (!win || win == WND_DESKTOP || win == WND_OTHER_PROCESS) return;
1990 if (!win->surface) goto done;
1994 if (!(data = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( RGNDATA, Buffer[size] )))) goto done;
1996 SERVER_START_REQ( get_surface_region )
1998 req->window = wine_server_user_handle( hwnd );
1999 wine_server_set_reply( req, data->Buffer, size );
2000 if (!(status = wine_server_call( req )))
2002 size_t reply_size = wine_server_reply_size( reply );
2003 if (reply_size)
2005 data->rdh.dwSize = sizeof(data->rdh);
2006 data->rdh.iType = RDH_RECTANGLES;
2007 data->rdh.nCount = reply_size / sizeof(RECT);
2008 data->rdh.nRgnSize = reply_size;
2009 region = ExtCreateRegion( NULL, size, data );
2010 OffsetRgn( region, -reply->visible_rect.left, -reply->visible_rect.top );
2013 else size = reply->total_size;
2015 SERVER_END_REQ;
2016 HeapFree( GetProcessHeap(), 0, data );
2017 } while (status == STATUS_BUFFER_OVERFLOW);
2019 if (status) goto done;
2021 win->surface->funcs->set_region( win->surface, region );
2022 if (region) DeleteObject( region );
2024 done:
2025 WIN_ReleasePtr( win );
2029 /***********************************************************************
2030 * set_window_pos
2032 * Backend implementation of SetWindowPos.
2034 BOOL set_window_pos( HWND hwnd, HWND insert_after, UINT swp_flags,
2035 const RECT *window_rect, const RECT *client_rect, const RECT *valid_rects )
2037 WND *win;
2038 HWND surface_win = 0, parent = GetAncestor( hwnd, GA_PARENT );
2039 BOOL ret;
2040 int old_width;
2041 RECT visible_rect, old_visible_rect, old_window_rect;
2042 struct window_surface *old_surface, *new_surface = NULL;
2044 if (!parent || parent == GetDesktopWindow())
2046 new_surface = &dummy_surface; /* provide a default surface for top-level windows */
2047 window_surface_add_ref( new_surface );
2049 visible_rect = *window_rect;
2050 USER_Driver->pWindowPosChanging( hwnd, insert_after, swp_flags,
2051 window_rect, client_rect, &visible_rect, &new_surface );
2053 WIN_GetRectangles( hwnd, COORDS_SCREEN, &old_window_rect, NULL );
2055 if (!(win = WIN_GetPtr( hwnd )) || win == WND_DESKTOP || win == WND_OTHER_PROCESS)
2057 if (new_surface) window_surface_release( new_surface );
2058 return FALSE;
2060 old_width = win->rectClient.right - win->rectClient.left;
2061 old_visible_rect = win->visible_rect;
2062 old_surface = win->surface;
2063 if (old_surface != new_surface) swp_flags |= SWP_FRAMECHANGED; /* force refreshing non-client area */
2065 SERVER_START_REQ( set_window_pos )
2067 req->handle = wine_server_user_handle( hwnd );
2068 req->previous = wine_server_user_handle( insert_after );
2069 req->swp_flags = swp_flags;
2070 req->window.left = window_rect->left;
2071 req->window.top = window_rect->top;
2072 req->window.right = window_rect->right;
2073 req->window.bottom = window_rect->bottom;
2074 req->client.left = client_rect->left;
2075 req->client.top = client_rect->top;
2076 req->client.right = client_rect->right;
2077 req->client.bottom = client_rect->bottom;
2078 if (memcmp( window_rect, &visible_rect, sizeof(RECT) ) || !IsRectEmpty( &valid_rects[0] ))
2080 wine_server_add_data( req, &visible_rect, sizeof(visible_rect) );
2081 if (!IsRectEmpty( &valid_rects[0] ))
2082 wine_server_add_data( req, valid_rects, 2 * sizeof(*valid_rects) );
2084 if (new_surface) req->paint_flags |= SET_WINPOS_PAINT_SURFACE;
2085 if (win->pixel_format) req->paint_flags |= SET_WINPOS_PIXEL_FORMAT;
2087 if ((ret = !wine_server_call( req )))
2089 win->dwStyle = reply->new_style;
2090 win->dwExStyle = reply->new_ex_style;
2091 win->rectWindow = *window_rect;
2092 win->rectClient = *client_rect;
2093 win->visible_rect = visible_rect;
2094 win->surface = new_surface;
2095 surface_win = wine_server_ptr_handle( reply->surface_win );
2096 if (GetWindowLongW( win->parent, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
2098 RECT client;
2099 GetClientRect( win->parent, &client );
2100 mirror_rect( &client, &win->rectWindow );
2101 mirror_rect( &client, &win->rectClient );
2102 mirror_rect( &client, &win->visible_rect );
2104 /* if an RTL window is resized the children have moved */
2105 if (win->dwExStyle & WS_EX_LAYOUTRTL && client_rect->right - client_rect->left != old_width)
2106 win->flags |= WIN_CHILDREN_MOVED;
2109 SERVER_END_REQ;
2111 if (ret)
2113 if (surface_win) update_surface_region( surface_win );
2114 if (((swp_flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) ||
2115 (swp_flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW | SWP_STATECHANGED | SWP_FRAMECHANGED)))
2116 invalidate_dce( win, &old_window_rect );
2119 WIN_ReleasePtr( win );
2121 if (ret)
2123 TRACE( "win %p surface %p -> %p\n", hwnd, old_surface, new_surface );
2124 register_window_surface( old_surface, new_surface );
2125 if (old_surface)
2127 if (!IsRectEmpty( valid_rects ))
2129 move_window_bits( hwnd, old_surface, new_surface, &visible_rect,
2130 &old_visible_rect, client_rect, valid_rects );
2131 valid_rects = NULL; /* prevent the driver from trying to also move the bits */
2133 window_surface_release( old_surface );
2135 USER_Driver->pWindowPosChanged( hwnd, insert_after, swp_flags, window_rect,
2136 client_rect, &visible_rect, valid_rects, new_surface );
2138 else if (new_surface) window_surface_release( new_surface );
2140 return ret;
2144 /***********************************************************************
2145 * USER_SetWindowPos
2147 * User32 internal function
2149 BOOL USER_SetWindowPos( WINDOWPOS * winpos )
2151 RECT newWindowRect, newClientRect, valid_rects[2];
2152 UINT orig_flags;
2154 orig_flags = winpos->flags;
2156 /* First, check z-order arguments. */
2157 if (!(winpos->flags & SWP_NOZORDER))
2159 /* fix sign extension */
2160 if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
2161 else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
2163 if (!(winpos->hwndInsertAfter == HWND_TOP ||
2164 winpos->hwndInsertAfter == HWND_BOTTOM ||
2165 winpos->hwndInsertAfter == HWND_TOPMOST ||
2166 winpos->hwndInsertAfter == HWND_NOTOPMOST))
2168 HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
2169 HWND insertafter_parent = GetAncestor( winpos->hwndInsertAfter, GA_PARENT );
2171 /* hwndInsertAfter must be a sibling of the window */
2172 if (!insertafter_parent) return FALSE;
2173 if (insertafter_parent != parent) return TRUE;
2177 /* Make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
2178 if (!(winpos->flags & SWP_NOMOVE))
2180 if (winpos->x < -32768) winpos->x = -32768;
2181 else if (winpos->x > 32767) winpos->x = 32767;
2182 if (winpos->y < -32768) winpos->y = -32768;
2183 else if (winpos->y > 32767) winpos->y = 32767;
2185 if (!(winpos->flags & SWP_NOSIZE))
2187 if (winpos->cx < 0) winpos->cx = 0;
2188 else if (winpos->cx > 32767) winpos->cx = 32767;
2189 if (winpos->cy < 0) winpos->cy = 0;
2190 else if (winpos->cy > 32767) winpos->cy = 32767;
2193 if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
2195 /* Fix redundant flags */
2196 if (!fixup_flags( winpos )) return FALSE;
2198 if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
2200 if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
2201 winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
2204 /* Common operations */
2206 SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect, valid_rects );
2208 if (!set_window_pos( winpos->hwnd, winpos->hwndInsertAfter, winpos->flags,
2209 &newWindowRect, &newClientRect, valid_rects ))
2210 return FALSE;
2212 /* erase parent when hiding or resizing child */
2213 if (!(orig_flags & SWP_DEFERERASE) &&
2214 ((orig_flags & SWP_HIDEWINDOW) ||
2215 (!(orig_flags & SWP_SHOWWINDOW) &&
2216 (winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOGEOMETRYCHANGE)))
2218 HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
2219 if (!parent || parent == GetDesktopWindow()) parent = winpos->hwnd;
2220 erase_now( parent, 0 );
2223 if( winpos->flags & SWP_HIDEWINDOW )
2224 HideCaret(winpos->hwnd);
2225 else if (winpos->flags & SWP_SHOWWINDOW)
2226 ShowCaret(winpos->hwnd);
2228 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)))
2230 /* child windows get WM_CHILDACTIVATE message */
2231 if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
2232 SendMessageW( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
2233 else
2234 SetForegroundWindow( winpos->hwnd );
2237 /* And last, send the WM_WINDOWPOSCHANGED message */
2239 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
2241 if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
2243 /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
2244 and always contains final window position.
2246 winpos->x = newWindowRect.left;
2247 winpos->y = newWindowRect.top;
2248 winpos->cx = newWindowRect.right - newWindowRect.left;
2249 winpos->cy = newWindowRect.bottom - newWindowRect.top;
2250 SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
2252 return TRUE;
2255 /***********************************************************************
2256 * SetWindowPos (USER32.@)
2258 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
2259 INT x, INT y, INT cx, INT cy, UINT flags )
2261 WINDOWPOS winpos;
2263 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2264 hwnd, hwndInsertAfter, x, y, cx, cy, flags);
2265 if(TRACE_ON(win)) dump_winpos_flags(flags);
2267 if (is_broadcast(hwnd))
2269 SetLastError( ERROR_INVALID_PARAMETER );
2270 return FALSE;
2273 winpos.hwnd = WIN_GetFullHandle(hwnd);
2274 winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
2275 winpos.x = x;
2276 winpos.y = y;
2277 winpos.cx = cx;
2278 winpos.cy = cy;
2279 winpos.flags = flags;
2281 if (WIN_IsCurrentThread( hwnd ))
2282 return USER_SetWindowPos(&winpos);
2284 return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
2288 /***********************************************************************
2289 * BeginDeferWindowPos (USER32.@)
2291 HDWP WINAPI BeginDeferWindowPos( INT count )
2293 HDWP handle = 0;
2294 DWP *pDWP;
2296 TRACE("%d\n", count);
2298 if (count < 0)
2300 SetLastError(ERROR_INVALID_PARAMETER);
2301 return 0;
2303 /* Windows allows zero count, in which case it allocates context for 8 moves */
2304 if (count == 0) count = 8;
2306 if (!(pDWP = HeapAlloc( GetProcessHeap(), 0, sizeof(DWP)))) return 0;
2308 pDWP->actualCount = 0;
2309 pDWP->suggestedCount = count;
2310 pDWP->hwndParent = 0;
2312 if (!(pDWP->winPos = HeapAlloc( GetProcessHeap(), 0, count * sizeof(WINDOWPOS) )) ||
2313 !(handle = alloc_user_handle( &pDWP->obj, USER_DWP )))
2315 HeapFree( GetProcessHeap(), 0, pDWP->winPos );
2316 HeapFree( GetProcessHeap(), 0, pDWP );
2319 TRACE("returning hdwp %p\n", handle);
2320 return handle;
2324 /***********************************************************************
2325 * DeferWindowPos (USER32.@)
2327 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
2328 INT x, INT y, INT cx, INT cy,
2329 UINT flags )
2331 DWP *pDWP;
2332 int i;
2333 HDWP retvalue = hdwp;
2335 TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2336 hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
2338 hwnd = WIN_GetFullHandle( hwnd );
2339 if (is_desktop_window( hwnd )) return 0;
2341 if (!(pDWP = get_user_handle_ptr( hdwp, USER_DWP ))) return 0;
2342 if (pDWP == OBJ_OTHER_PROCESS)
2344 FIXME( "other process handle %p?\n", hdwp );
2345 return 0;
2348 for (i = 0; i < pDWP->actualCount; i++)
2350 if (pDWP->winPos[i].hwnd == hwnd)
2352 /* Merge with the other changes */
2353 if (!(flags & SWP_NOZORDER))
2355 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
2357 if (!(flags & SWP_NOMOVE))
2359 pDWP->winPos[i].x = x;
2360 pDWP->winPos[i].y = y;
2362 if (!(flags & SWP_NOSIZE))
2364 pDWP->winPos[i].cx = cx;
2365 pDWP->winPos[i].cy = cy;
2367 pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
2368 SWP_NOZORDER | SWP_NOREDRAW |
2369 SWP_NOACTIVATE | SWP_NOCOPYBITS|
2370 SWP_NOOWNERZORDER);
2371 pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
2372 SWP_FRAMECHANGED);
2373 goto END;
2376 if (pDWP->actualCount >= pDWP->suggestedCount)
2378 WINDOWPOS *newpos = HeapReAlloc( GetProcessHeap(), 0, pDWP->winPos,
2379 pDWP->suggestedCount * 2 * sizeof(WINDOWPOS) );
2380 if (!newpos)
2382 retvalue = 0;
2383 goto END;
2385 pDWP->suggestedCount *= 2;
2386 pDWP->winPos = newpos;
2388 pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
2389 pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
2390 pDWP->winPos[pDWP->actualCount].x = x;
2391 pDWP->winPos[pDWP->actualCount].y = y;
2392 pDWP->winPos[pDWP->actualCount].cx = cx;
2393 pDWP->winPos[pDWP->actualCount].cy = cy;
2394 pDWP->winPos[pDWP->actualCount].flags = flags;
2395 pDWP->actualCount++;
2396 END:
2397 release_user_handle_ptr( pDWP );
2398 return retvalue;
2402 /***********************************************************************
2403 * EndDeferWindowPos (USER32.@)
2405 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
2407 DWP *pDWP;
2408 WINDOWPOS *winpos;
2409 BOOL res = TRUE;
2410 int i;
2412 TRACE("%p\n", hdwp);
2414 if (!(pDWP = free_user_handle( hdwp, USER_DWP ))) return FALSE;
2415 if (pDWP == OBJ_OTHER_PROCESS)
2417 FIXME( "other process handle %p?\n", hdwp );
2418 return FALSE;
2421 for (i = 0, winpos = pDWP->winPos; res && i < pDWP->actualCount; i++, winpos++)
2423 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2424 winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
2425 winpos->cx, winpos->cy, winpos->flags);
2427 if (WIN_IsCurrentThread( winpos->hwnd ))
2428 res = USER_SetWindowPos( winpos );
2429 else
2430 res = SendMessageW( winpos->hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)winpos );
2432 HeapFree( GetProcessHeap(), 0, pDWP->winPos );
2433 HeapFree( GetProcessHeap(), 0, pDWP );
2434 return res;
2438 /***********************************************************************
2439 * ArrangeIconicWindows (USER32.@)
2441 UINT WINAPI ArrangeIconicWindows( HWND parent )
2443 RECT rectParent;
2444 HWND hwndChild;
2445 INT x, y, xspacing, yspacing;
2447 GetClientRect( parent, &rectParent );
2448 x = rectParent.left;
2449 y = rectParent.bottom;
2450 xspacing = GetSystemMetrics(SM_CXICONSPACING);
2451 yspacing = GetSystemMetrics(SM_CYICONSPACING);
2453 hwndChild = GetWindow( parent, GW_CHILD );
2454 while (hwndChild)
2456 if( IsIconic( hwndChild ) )
2458 WINPOS_ShowIconTitle( hwndChild, FALSE );
2460 SetWindowPos( hwndChild, 0, x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
2461 y - yspacing - GetSystemMetrics(SM_CYICON)/2, 0, 0,
2462 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
2463 if( IsWindow(hwndChild) )
2464 WINPOS_ShowIconTitle(hwndChild , TRUE );
2466 if (x <= rectParent.right - xspacing) x += xspacing;
2467 else
2469 x = rectParent.left;
2470 y -= yspacing;
2473 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
2475 return yspacing;
2479 /***********************************************************************
2480 * draw_moving_frame
2482 * Draw the frame used when moving or resizing window.
2484 static void draw_moving_frame( HWND parent, HDC hdc, RECT *screen_rect, BOOL thickframe )
2486 RECT rect = *screen_rect;
2488 if (parent) MapWindowPoints( 0, parent, (POINT *)&rect, 2 );
2489 if (thickframe)
2491 const int width = GetSystemMetrics(SM_CXFRAME);
2492 const int height = GetSystemMetrics(SM_CYFRAME);
2494 HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
2495 PatBlt( hdc, rect.left, rect.top,
2496 rect.right - rect.left - width, height, PATINVERT );
2497 PatBlt( hdc, rect.left, rect.top + height, width,
2498 rect.bottom - rect.top - height, PATINVERT );
2499 PatBlt( hdc, rect.left + width, rect.bottom - 1,
2500 rect.right - rect.left - width, -height, PATINVERT );
2501 PatBlt( hdc, rect.right - 1, rect.top, -width,
2502 rect.bottom - rect.top - height, PATINVERT );
2503 SelectObject( hdc, hbrush );
2505 else DrawFocusRect( hdc, &rect );
2509 /***********************************************************************
2510 * start_size_move
2512 * Initialization of a move or resize, when initiated from a menu choice.
2513 * Return hit test code for caption or sizing border.
2515 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
2517 LONG hittest = 0;
2518 POINT pt;
2519 MSG msg;
2520 RECT rectWindow;
2522 GetWindowRect( hwnd, &rectWindow );
2524 if ((wParam & 0xfff0) == SC_MOVE)
2526 /* Move pointer at the center of the caption */
2527 RECT rect = rectWindow;
2528 /* Note: to be exactly centered we should take the different types
2529 * of border into account, but it shouldn't make more than a few pixels
2530 * of difference so let's not bother with that */
2531 rect.top += GetSystemMetrics(SM_CYBORDER);
2532 if (style & WS_SYSMENU)
2533 rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
2534 if (style & WS_MINIMIZEBOX)
2535 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
2536 if (style & WS_MAXIMIZEBOX)
2537 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
2538 pt.x = (rect.right + rect.left) / 2;
2539 pt.y = rect.top + GetSystemMetrics(SM_CYSIZE)/2;
2540 hittest = HTCAPTION;
2541 *capturePoint = pt;
2543 else /* SC_SIZE */
2545 SetCursor( LoadCursorW( 0, (LPWSTR)IDC_SIZEALL ) );
2546 pt.x = pt.y = 0;
2547 while(!hittest)
2549 if (!GetMessageW( &msg, 0, 0, 0 )) return 0;
2550 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2552 switch(msg.message)
2554 case WM_MOUSEMOVE:
2555 pt.x = min( max( msg.pt.x, rectWindow.left ), rectWindow.right - 1 );
2556 pt.y = min( max( msg.pt.y, rectWindow.top ), rectWindow.bottom - 1 );
2557 hittest = SendMessageW( hwnd, WM_NCHITTEST, 0, MAKELONG( pt.x, pt.y ) );
2558 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT)) hittest = 0;
2559 break;
2561 case WM_LBUTTONUP:
2562 return 0;
2564 case WM_KEYDOWN:
2565 switch(msg.wParam)
2567 case VK_UP:
2568 hittest = HTTOP;
2569 pt.x =(rectWindow.left+rectWindow.right)/2;
2570 pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
2571 break;
2572 case VK_DOWN:
2573 hittest = HTBOTTOM;
2574 pt.x =(rectWindow.left+rectWindow.right)/2;
2575 pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
2576 break;
2577 case VK_LEFT:
2578 hittest = HTLEFT;
2579 pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
2580 pt.y =(rectWindow.top+rectWindow.bottom)/2;
2581 break;
2582 case VK_RIGHT:
2583 hittest = HTRIGHT;
2584 pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
2585 pt.y =(rectWindow.top+rectWindow.bottom)/2;
2586 break;
2587 case VK_RETURN:
2588 case VK_ESCAPE:
2589 return 0;
2591 break;
2592 default:
2593 TranslateMessage( &msg );
2594 DispatchMessageW( &msg );
2595 break;
2598 *capturePoint = pt;
2600 SetCursorPos( pt.x, pt.y );
2601 SendMessageW( hwnd, WM_SETCURSOR, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
2602 return hittest;
2606 /***********************************************************************
2607 * WINPOS_SysCommandSizeMove
2609 * Perform SC_MOVE and SC_SIZE commands.
2611 void WINPOS_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
2613 MSG msg;
2614 RECT sizingRect, mouseRect, origRect;
2615 HDC hdc;
2616 HWND parent;
2617 LONG hittest = (LONG)(wParam & 0x0f);
2618 WPARAM syscommand = wParam & 0xfff0;
2619 HCURSOR hDragCursor = 0, hOldCursor = 0;
2620 POINT minTrack, maxTrack;
2621 POINT capturePoint, pt;
2622 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
2623 BOOL thickframe = HAS_THICKFRAME( style );
2624 BOOL iconic = style & WS_MINIMIZE;
2625 BOOL moved = FALSE;
2626 DWORD dwPoint = GetMessagePos ();
2627 BOOL DragFullWindows = TRUE;
2629 if (IsZoomed(hwnd) || !IsWindowVisible(hwnd)) return;
2631 pt.x = (short)LOWORD(dwPoint);
2632 pt.y = (short)HIWORD(dwPoint);
2633 capturePoint = pt;
2634 ClipCursor( NULL );
2636 TRACE("hwnd %p command %04lx, hittest %d, pos %d,%d\n",
2637 hwnd, syscommand, hittest, pt.x, pt.y);
2639 if (syscommand == SC_MOVE)
2641 if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
2642 if (!hittest) return;
2644 else /* SC_SIZE */
2646 if ( hittest && (syscommand != SC_MOUSEMENU) )
2647 hittest += (HTLEFT - WMSZ_LEFT);
2648 else
2650 set_capture_window( hwnd, GUI_INMOVESIZE, NULL );
2651 hittest = start_size_move( hwnd, wParam, &capturePoint, style );
2652 if (!hittest)
2654 set_capture_window( 0, GUI_INMOVESIZE, NULL );
2655 return;
2660 /* Get min/max info */
2662 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
2663 WIN_GetRectangles( hwnd, COORDS_PARENT, &sizingRect, NULL );
2664 origRect = sizingRect;
2665 if (style & WS_CHILD)
2667 parent = GetParent(hwnd);
2668 GetClientRect( parent, &mouseRect );
2669 MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
2670 MapWindowPoints( parent, 0, (LPPOINT)&sizingRect, 2 );
2672 else
2674 parent = 0;
2675 mouseRect = get_virtual_screen_rect();
2678 if (ON_LEFT_BORDER(hittest))
2680 mouseRect.left = max( mouseRect.left, sizingRect.right-maxTrack.x );
2681 mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
2683 else if (ON_RIGHT_BORDER(hittest))
2685 mouseRect.left = max( mouseRect.left, sizingRect.left+minTrack.x );
2686 mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
2688 if (ON_TOP_BORDER(hittest))
2690 mouseRect.top = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
2691 mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
2693 else if (ON_BOTTOM_BORDER(hittest))
2695 mouseRect.top = max( mouseRect.top, sizingRect.top+minTrack.y );
2696 mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
2699 /* Retrieve a default cache DC (without using the window style) */
2700 hdc = GetDCEx( parent, 0, DCX_CACHE );
2702 if( iconic ) /* create a cursor for dragging */
2704 hDragCursor = (HCURSOR)GetClassLongPtrW( hwnd, GCLP_HICON);
2705 if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageW( hwnd, WM_QUERYDRAGICON, 0, 0L);
2706 if( !hDragCursor ) iconic = FALSE;
2709 /* we only allow disabling the full window drag for child windows */
2710 if (parent) SystemParametersInfoW( SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0 );
2712 /* repaint the window before moving it around */
2713 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
2715 SendMessageW( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
2716 set_capture_window( hwnd, GUI_INMOVESIZE, NULL );
2718 while(1)
2720 int dx = 0, dy = 0;
2722 if (!GetMessageW( &msg, 0, 0, 0 )) break;
2723 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2725 /* Exit on button-up, Return, or Esc */
2726 if ((msg.message == WM_LBUTTONUP) ||
2727 ((msg.message == WM_KEYDOWN) &&
2728 ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
2730 if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
2732 TranslateMessage( &msg );
2733 DispatchMessageW( &msg );
2734 continue; /* We are not interested in other messages */
2737 pt = msg.pt;
2739 if (msg.message == WM_KEYDOWN) switch(msg.wParam)
2741 case VK_UP: pt.y -= 8; break;
2742 case VK_DOWN: pt.y += 8; break;
2743 case VK_LEFT: pt.x -= 8; break;
2744 case VK_RIGHT: pt.x += 8; break;
2747 pt.x = max( pt.x, mouseRect.left );
2748 pt.x = min( pt.x, mouseRect.right );
2749 pt.y = max( pt.y, mouseRect.top );
2750 pt.y = min( pt.y, mouseRect.bottom );
2752 dx = pt.x - capturePoint.x;
2753 dy = pt.y - capturePoint.y;
2755 if (dx || dy)
2757 if( !moved )
2759 moved = TRUE;
2761 if( iconic ) /* ok, no system popup tracking */
2763 hOldCursor = SetCursor(hDragCursor);
2764 ShowCursor( TRUE );
2765 WINPOS_ShowIconTitle( hwnd, FALSE );
2767 else if(!DragFullWindows)
2768 draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2771 if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
2772 else
2774 if(!iconic && !DragFullWindows) draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2775 if (hittest == HTCAPTION) OffsetRect( &sizingRect, dx, dy );
2776 if (ON_LEFT_BORDER(hittest)) sizingRect.left += dx;
2777 else if (ON_RIGHT_BORDER(hittest)) sizingRect.right += dx;
2778 if (ON_TOP_BORDER(hittest)) sizingRect.top += dy;
2779 else if (ON_BOTTOM_BORDER(hittest)) sizingRect.bottom += dy;
2780 capturePoint = pt;
2782 /* determine the hit location */
2783 if (syscommand == SC_SIZE)
2785 WPARAM wpSizingHit = 0;
2787 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
2788 wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
2789 SendMessageW( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&sizingRect );
2791 else
2792 SendMessageW( hwnd, WM_MOVING, 0, (LPARAM)&sizingRect );
2794 if (!iconic)
2796 if(!DragFullWindows)
2797 draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2798 else
2800 RECT rect = sizingRect;
2801 MapWindowPoints( 0, parent, (POINT *)&rect, 2 );
2802 SetWindowPos( hwnd, 0, rect.left, rect.top,
2803 rect.right - rect.left, rect.bottom - rect.top,
2804 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2811 if( iconic )
2813 if( moved ) /* restore cursors, show icon title later on */
2815 ShowCursor( FALSE );
2816 SetCursor( hOldCursor );
2819 else if (moved && !DragFullWindows)
2821 draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2824 set_capture_window( 0, GUI_INMOVESIZE, NULL );
2825 ReleaseDC( parent, hdc );
2826 if (parent) MapWindowPoints( 0, parent, (POINT *)&sizingRect, 2 );
2828 if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
2829 moved = FALSE;
2831 SendMessageW( hwnd, WM_EXITSIZEMOVE, 0, 0 );
2832 SendMessageW( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
2834 /* window moved or resized */
2835 if (moved)
2837 /* if the moving/resizing isn't canceled call SetWindowPos
2838 * with the new position or the new size of the window
2840 if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
2842 /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
2843 if(!DragFullWindows || iconic)
2844 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
2845 sizingRect.right - sizingRect.left,
2846 sizingRect.bottom - sizingRect.top,
2847 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2849 else
2850 { /* restore previous size/position */
2851 if(DragFullWindows)
2852 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
2853 origRect.right - origRect.left,
2854 origRect.bottom - origRect.top,
2855 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2859 if (IsIconic(hwnd))
2861 /* Single click brings up the system menu when iconized */
2863 if( !moved )
2865 if(style & WS_SYSMENU )
2866 SendMessageW( hwnd, WM_SYSCOMMAND,
2867 SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
2869 else WINPOS_ShowIconTitle( hwnd, TRUE );