po: Fix a mistake in Dutch translation.
[wine.git] / dlls / user32 / winpos.c
blob12d3ffa0325842f4887afaee0ff100ef7f18d925
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, data->rdh.dwSize + data->rdh.nRgnSize, 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.dwSize + data->rdh.nRgnSize, 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 if ((cmd == SW_HIDE) && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
1223 return FALSE;
1225 return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
1229 /***********************************************************************
1230 * GetInternalWindowPos (USER32.@)
1232 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
1233 LPPOINT ptIcon )
1235 WINDOWPLACEMENT wndpl;
1236 if (GetWindowPlacement( hwnd, &wndpl ))
1238 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
1239 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
1240 return wndpl.showCmd;
1242 return 0;
1246 /***********************************************************************
1247 * GetWindowPlacement (USER32.@)
1249 * Win95:
1250 * Fails if wndpl->length of Win95 (!) apps is invalid.
1252 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
1254 WND *pWnd = WIN_GetPtr( hwnd );
1256 if (!pWnd) return FALSE;
1258 if (pWnd == WND_DESKTOP)
1260 wndpl->length = sizeof(*wndpl);
1261 wndpl->showCmd = SW_SHOWNORMAL;
1262 wndpl->flags = 0;
1263 wndpl->ptMinPosition.x = -1;
1264 wndpl->ptMinPosition.y = -1;
1265 wndpl->ptMaxPosition.x = -1;
1266 wndpl->ptMaxPosition.y = -1;
1267 GetWindowRect( hwnd, &wndpl->rcNormalPosition );
1268 return TRUE;
1270 if (pWnd == WND_OTHER_PROCESS)
1272 if (!IsWindow( hwnd )) return FALSE;
1273 FIXME( "not supported on other process window %p\n", hwnd );
1274 /* provide some dummy information */
1275 wndpl->length = sizeof(*wndpl);
1276 wndpl->showCmd = SW_SHOWNORMAL;
1277 wndpl->flags = 0;
1278 wndpl->ptMinPosition.x = -1;
1279 wndpl->ptMinPosition.y = -1;
1280 wndpl->ptMaxPosition.x = -1;
1281 wndpl->ptMaxPosition.y = -1;
1282 GetWindowRect( hwnd, &wndpl->rcNormalPosition );
1283 return TRUE;
1286 /* update the placement according to the current style */
1287 if (pWnd->dwStyle & WS_MINIMIZE)
1289 pWnd->min_pos.x = pWnd->rectWindow.left;
1290 pWnd->min_pos.y = pWnd->rectWindow.top;
1292 else if (pWnd->dwStyle & WS_MAXIMIZE)
1294 pWnd->max_pos.x = pWnd->rectWindow.left;
1295 pWnd->max_pos.y = pWnd->rectWindow.top;
1297 else
1299 pWnd->normal_rect = pWnd->rectWindow;
1302 wndpl->length = sizeof(*wndpl);
1303 if( pWnd->dwStyle & WS_MINIMIZE )
1304 wndpl->showCmd = SW_SHOWMINIMIZED;
1305 else
1306 wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
1307 if( pWnd->flags & WIN_RESTORE_MAX )
1308 wndpl->flags = WPF_RESTORETOMAXIMIZED;
1309 else
1310 wndpl->flags = 0;
1311 wndpl->ptMinPosition = pWnd->min_pos;
1312 wndpl->ptMaxPosition = pWnd->max_pos;
1313 wndpl->rcNormalPosition = pWnd->normal_rect;
1314 WIN_ReleasePtr( pWnd );
1316 TRACE( "%p: returning min %d,%d max %d,%d normal %s\n",
1317 hwnd, wndpl->ptMinPosition.x, wndpl->ptMinPosition.y,
1318 wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y,
1319 wine_dbgstr_rect(&wndpl->rcNormalPosition) );
1320 return TRUE;
1323 /* make sure the specified rect is visible on screen */
1324 static void make_rect_onscreen( RECT *rect )
1326 MONITORINFO info;
1327 HMONITOR monitor = MonitorFromRect( rect, MONITOR_DEFAULTTONEAREST );
1329 info.cbSize = sizeof(info);
1330 if (!monitor || !GetMonitorInfoW( monitor, &info )) return;
1331 /* FIXME: map coordinates from rcWork to rcMonitor */
1332 if (rect->right <= info.rcWork.left)
1334 rect->right += info.rcWork.left - rect->left;
1335 rect->left = info.rcWork.left;
1337 else if (rect->left >= info.rcWork.right)
1339 rect->left += info.rcWork.right - rect->right;
1340 rect->right = info.rcWork.right;
1342 if (rect->bottom <= info.rcWork.top)
1344 rect->bottom += info.rcWork.top - rect->top;
1345 rect->top = info.rcWork.top;
1347 else if (rect->top >= info.rcWork.bottom)
1349 rect->top += info.rcWork.bottom - rect->bottom;
1350 rect->bottom = info.rcWork.bottom;
1354 /* make sure the specified point is visible on screen */
1355 static void make_point_onscreen( POINT *pt )
1357 RECT rect;
1359 SetRect( &rect, pt->x, pt->y, pt->x + 1, pt->y + 1 );
1360 make_rect_onscreen( &rect );
1361 pt->x = rect.left;
1362 pt->y = rect.top;
1366 /***********************************************************************
1367 * WINPOS_SetPlacement
1369 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
1371 DWORD style;
1372 WND *pWnd = WIN_GetPtr( hwnd );
1373 WINDOWPLACEMENT wp = *wndpl;
1375 if (flags & PLACE_MIN) make_point_onscreen( &wp.ptMinPosition );
1376 if (flags & PLACE_MAX) make_point_onscreen( &wp.ptMaxPosition );
1377 if (flags & PLACE_RECT) make_rect_onscreen( &wp.rcNormalPosition );
1379 TRACE( "%p: setting min %d,%d max %d,%d normal %s flags %x ajusted to min %d,%d max %d,%d normal %s\n",
1380 hwnd, wndpl->ptMinPosition.x, wndpl->ptMinPosition.y,
1381 wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y,
1382 wine_dbgstr_rect(&wndpl->rcNormalPosition), flags,
1383 wp.ptMinPosition.x, wp.ptMinPosition.y, wp.ptMaxPosition.x, wp.ptMaxPosition.y,
1384 wine_dbgstr_rect(&wp.rcNormalPosition) );
1386 if (!pWnd || pWnd == WND_OTHER_PROCESS || pWnd == WND_DESKTOP) return FALSE;
1388 if( flags & PLACE_MIN ) pWnd->min_pos = wp.ptMinPosition;
1389 if( flags & PLACE_MAX ) pWnd->max_pos = wp.ptMaxPosition;
1390 if( flags & PLACE_RECT) pWnd->normal_rect = wp.rcNormalPosition;
1392 style = pWnd->dwStyle;
1394 WIN_ReleasePtr( pWnd );
1396 if( style & WS_MINIMIZE )
1398 if (flags & PLACE_MIN)
1400 WINPOS_ShowIconTitle( hwnd, FALSE );
1401 SetWindowPos( hwnd, 0, wp.ptMinPosition.x, wp.ptMinPosition.y, 0, 0,
1402 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1405 else if( style & WS_MAXIMIZE )
1407 if (flags & PLACE_MAX)
1408 SetWindowPos( hwnd, 0, wp.ptMaxPosition.x, wp.ptMaxPosition.y, 0, 0,
1409 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1411 else if( flags & PLACE_RECT )
1412 SetWindowPos( hwnd, 0, wp.rcNormalPosition.left, wp.rcNormalPosition.top,
1413 wp.rcNormalPosition.right - wp.rcNormalPosition.left,
1414 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top,
1415 SWP_NOZORDER | SWP_NOACTIVATE );
1417 ShowWindow( hwnd, wndpl->showCmd );
1419 if (IsIconic( hwnd ))
1421 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE) WINPOS_ShowIconTitle( hwnd, TRUE );
1423 /* SDK: ...valid only the next time... */
1424 if( wndpl->flags & WPF_RESTORETOMAXIMIZED )
1426 pWnd = WIN_GetPtr( hwnd );
1427 if (pWnd && pWnd != WND_OTHER_PROCESS)
1429 pWnd->flags |= WIN_RESTORE_MAX;
1430 WIN_ReleasePtr( pWnd );
1434 return TRUE;
1438 /***********************************************************************
1439 * SetWindowPlacement (USER32.@)
1441 * Win95:
1442 * Fails if wndpl->length of Win95 (!) apps is invalid.
1444 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
1446 UINT flags = PLACE_MAX | PLACE_RECT;
1447 if (!wpl) return FALSE;
1448 if (wpl->flags & WPF_SETMINPOSITION) flags |= PLACE_MIN;
1449 return WINPOS_SetPlacement( hwnd, wpl, flags );
1453 /***********************************************************************
1454 * AnimateWindow (USER32.@)
1455 * Shows/Hides a window with an animation
1456 * NO ANIMATION YET
1458 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
1460 FIXME("partial stub\n");
1462 /* If trying to show/hide and it's already *
1463 * shown/hidden or invalid window, fail with *
1464 * invalid parameter */
1465 if(!IsWindow(hwnd) ||
1466 (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
1467 (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
1469 SetLastError(ERROR_INVALID_PARAMETER);
1470 return FALSE;
1473 ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1475 return TRUE;
1478 /***********************************************************************
1479 * SetInternalWindowPos (USER32.@)
1481 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1482 LPRECT rect, LPPOINT pt )
1484 WINDOWPLACEMENT wndpl;
1485 UINT flags;
1487 wndpl.length = sizeof(wndpl);
1488 wndpl.showCmd = showCmd;
1489 wndpl.flags = flags = 0;
1491 if( pt )
1493 flags |= PLACE_MIN;
1494 wndpl.flags |= WPF_SETMINPOSITION;
1495 wndpl.ptMinPosition = *pt;
1497 if( rect )
1499 flags |= PLACE_RECT;
1500 wndpl.rcNormalPosition = *rect;
1502 WINPOS_SetPlacement( hwnd, &wndpl, flags );
1506 /*******************************************************************
1507 * can_activate_window
1509 * Check if we can activate the specified window.
1511 static BOOL can_activate_window( HWND hwnd )
1513 LONG style;
1515 if (!hwnd) return FALSE;
1516 style = GetWindowLongW( hwnd, GWL_STYLE );
1517 if (!(style & WS_VISIBLE)) return FALSE;
1518 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1519 return !(style & WS_DISABLED);
1523 /*******************************************************************
1524 * WINPOS_ActivateOtherWindow
1526 * Activates window other than pWnd.
1528 void WINPOS_ActivateOtherWindow(HWND hwnd)
1530 HWND hwndTo, fg;
1532 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1534 hwndTo = GetAncestor( hwndTo, GA_ROOT );
1535 if (can_activate_window( hwndTo )) goto done;
1538 hwndTo = hwnd;
1539 for (;;)
1541 if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1542 if (can_activate_window( hwndTo )) goto done;
1545 hwndTo = GetTopWindow( 0 );
1546 for (;;)
1548 if (hwndTo == hwnd)
1550 hwndTo = 0;
1551 break;
1553 if (can_activate_window( hwndTo )) goto done;
1554 if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1557 done:
1558 fg = GetForegroundWindow();
1559 TRACE("win = %p fg = %p\n", hwndTo, fg);
1560 if (!fg || (hwnd == fg))
1562 if (SetForegroundWindow( hwndTo )) return;
1564 if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1568 /***********************************************************************
1569 * WINPOS_HandleWindowPosChanging
1571 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1573 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1575 POINT minTrack, maxTrack;
1576 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1578 if (winpos->flags & SWP_NOSIZE) return 0;
1579 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1581 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1582 if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1583 if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1584 if (!(style & WS_MINIMIZE))
1586 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1587 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1590 return 0;
1594 /***********************************************************************
1595 * dump_winpos_flags
1597 static void dump_winpos_flags(UINT flags)
1599 static const DWORD dumped_flags = (SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOREDRAW |
1600 SWP_NOACTIVATE | SWP_FRAMECHANGED | SWP_SHOWWINDOW |
1601 SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOOWNERZORDER |
1602 SWP_NOSENDCHANGING | SWP_DEFERERASE | SWP_ASYNCWINDOWPOS |
1603 SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_STATECHANGED);
1604 TRACE("flags:");
1605 if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1606 if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1607 if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1608 if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1609 if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1610 if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1611 if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1612 if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1613 if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1614 if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1615 if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1616 if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1617 if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1618 if(flags & SWP_NOCLIENTSIZE) TRACE(" SWP_NOCLIENTSIZE");
1619 if(flags & SWP_NOCLIENTMOVE) TRACE(" SWP_NOCLIENTMOVE");
1620 if(flags & SWP_STATECHANGED) TRACE(" SWP_STATECHANGED");
1622 if(flags & ~dumped_flags) TRACE(" %08x", flags & ~dumped_flags);
1623 TRACE("\n");
1626 /***********************************************************************
1627 * SWP_DoWinPosChanging
1629 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
1631 WND *wndPtr;
1632 RECT window_rect, client_rect;
1634 /* Send WM_WINDOWPOSCHANGING message */
1636 if (!(pWinpos->flags & SWP_NOSENDCHANGING))
1637 SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
1639 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) ||
1640 wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
1642 /* Calculate new position and size */
1644 WIN_GetRectangles( pWinpos->hwnd, COORDS_PARENT, &window_rect, &client_rect );
1645 *pNewWindowRect = window_rect;
1646 *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? window_rect : client_rect;
1648 if (!(pWinpos->flags & SWP_NOSIZE))
1650 if (wndPtr->dwStyle & WS_MINIMIZE)
1652 pNewWindowRect->right = pNewWindowRect->left + GetSystemMetrics(SM_CXICON);
1653 pNewWindowRect->bottom = pNewWindowRect->top + GetSystemMetrics(SM_CYICON);
1655 else
1657 pNewWindowRect->right = pNewWindowRect->left + pWinpos->cx;
1658 pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
1661 if (!(pWinpos->flags & SWP_NOMOVE))
1663 pNewWindowRect->left = pWinpos->x;
1664 pNewWindowRect->top = pWinpos->y;
1665 pNewWindowRect->right += pWinpos->x - window_rect.left;
1666 pNewWindowRect->bottom += pWinpos->y - window_rect.top;
1668 OffsetRect( pNewClientRect, pWinpos->x - window_rect.left,
1669 pWinpos->y - window_rect.top );
1671 pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
1673 TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
1674 pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
1675 pWinpos->cx, pWinpos->cy, pWinpos->flags );
1676 TRACE( "current %s style %08x new %s\n",
1677 wine_dbgstr_rect( &window_rect ), wndPtr->dwStyle,
1678 wine_dbgstr_rect( pNewWindowRect ));
1680 WIN_ReleasePtr( wndPtr );
1681 return TRUE;
1684 /***********************************************************************
1685 * get_valid_rects
1687 * Compute the valid rects from the old and new client rect and WVR_* flags.
1688 * Helper for WM_NCCALCSIZE handling.
1690 static inline void get_valid_rects( const RECT *old_client, const RECT *new_client, UINT flags,
1691 RECT *valid )
1693 int cx, cy;
1695 if (flags & WVR_REDRAW)
1697 SetRectEmpty( &valid[0] );
1698 SetRectEmpty( &valid[1] );
1699 return;
1702 if (flags & WVR_VALIDRECTS)
1704 if (!IntersectRect( &valid[0], &valid[0], new_client ) ||
1705 !IntersectRect( &valid[1], &valid[1], old_client ))
1707 SetRectEmpty( &valid[0] );
1708 SetRectEmpty( &valid[1] );
1709 return;
1711 flags = WVR_ALIGNLEFT | WVR_ALIGNTOP;
1713 else
1715 valid[0] = *new_client;
1716 valid[1] = *old_client;
1719 /* make sure the rectangles have the same size */
1720 cx = min( valid[0].right - valid[0].left, valid[1].right - valid[1].left );
1721 cy = min( valid[0].bottom - valid[0].top, valid[1].bottom - valid[1].top );
1723 if (flags & WVR_ALIGNBOTTOM)
1725 valid[0].top = valid[0].bottom - cy;
1726 valid[1].top = valid[1].bottom - cy;
1728 else
1730 valid[0].bottom = valid[0].top + cy;
1731 valid[1].bottom = valid[1].top + cy;
1733 if (flags & WVR_ALIGNRIGHT)
1735 valid[0].left = valid[0].right - cx;
1736 valid[1].left = valid[1].right - cx;
1738 else
1740 valid[0].right = valid[0].left + cx;
1741 valid[1].right = valid[1].left + cx;
1746 /***********************************************************************
1747 * SWP_DoOwnedPopups
1749 * fix Z order taking into account owned popups -
1750 * basically we need to maintain them above the window that owns them
1752 * FIXME: hide/show owned popups when owner visibility changes.
1754 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
1756 HWND owner, *list = NULL;
1757 unsigned int i;
1759 TRACE("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
1761 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) return hwndInsertAfter;
1763 if ((owner = GetWindow( hwnd, GW_OWNER )))
1765 /* make sure this popup stays above the owner */
1767 if (hwndInsertAfter != HWND_TOPMOST)
1769 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return hwndInsertAfter;
1771 for (i = 0; list[i]; i++)
1773 BOOL topmost = (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST) != 0;
1775 if (list[i] == owner)
1777 if (i > 0) hwndInsertAfter = list[i-1];
1778 else hwndInsertAfter = topmost ? HWND_TOPMOST : HWND_TOP;
1779 break;
1782 if (hwndInsertAfter == HWND_TOP || hwndInsertAfter == HWND_NOTOPMOST)
1784 if (!topmost) break;
1786 else if (list[i] == hwndInsertAfter) break;
1791 if (hwndInsertAfter == HWND_BOTTOM) goto done;
1792 if (!list && !(list = WIN_ListChildren( GetDesktopWindow() ))) goto done;
1794 i = 0;
1795 if (hwndInsertAfter == HWND_TOP || hwndInsertAfter == HWND_NOTOPMOST)
1797 if (hwndInsertAfter == HWND_NOTOPMOST || !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_TOPMOST))
1799 /* skip all the topmost windows */
1800 while (list[i] && (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST)) i++;
1803 else if (hwndInsertAfter != HWND_TOPMOST)
1805 /* skip windows that are already placed correctly */
1806 for (i = 0; list[i]; i++)
1808 if (list[i] == hwndInsertAfter) break;
1809 if (list[i] == hwnd) goto done; /* nothing to do if window is moving backwards in z-order */
1813 for ( ; list[i]; i++)
1815 if (list[i] == hwnd) break;
1816 if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
1817 TRACE( "moving %p owned by %p after %p\n", list[i], hwnd, hwndInsertAfter );
1818 SetWindowPos( list[i], hwndInsertAfter, 0, 0, 0, 0,
1819 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING | SWP_DEFERERASE );
1820 hwndInsertAfter = list[i];
1823 done:
1824 HeapFree( GetProcessHeap(), 0, list );
1825 return hwndInsertAfter;
1828 /***********************************************************************
1829 * SWP_DoNCCalcSize
1831 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect,
1832 RECT *validRects )
1834 UINT wvrFlags = 0;
1835 RECT window_rect, client_rect;
1837 WIN_GetRectangles( pWinpos->hwnd, COORDS_PARENT, &window_rect, &client_rect );
1839 /* Send WM_NCCALCSIZE message to get new client area */
1840 if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
1842 NCCALCSIZE_PARAMS params;
1843 WINDOWPOS winposCopy;
1845 params.rgrc[0] = *pNewWindowRect;
1846 params.rgrc[1] = window_rect;
1847 params.rgrc[2] = client_rect;
1848 params.lppos = &winposCopy;
1849 winposCopy = *pWinpos;
1851 wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
1853 *pNewClientRect = params.rgrc[0];
1855 TRACE( "hwnd %p old win %s old client %s new win %s new client %s\n", pWinpos->hwnd,
1856 wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(&client_rect),
1857 wine_dbgstr_rect(pNewWindowRect), wine_dbgstr_rect(pNewClientRect) );
1859 if( pNewClientRect->left != client_rect.left ||
1860 pNewClientRect->top != client_rect.top )
1861 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1863 if( (pNewClientRect->right - pNewClientRect->left !=
1864 client_rect.right - client_rect.left))
1865 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1866 else
1867 wvrFlags &= ~WVR_HREDRAW;
1869 if (pNewClientRect->bottom - pNewClientRect->top !=
1870 client_rect.bottom - client_rect.top)
1871 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1872 else
1873 wvrFlags &= ~WVR_VREDRAW;
1875 validRects[0] = params.rgrc[1];
1876 validRects[1] = params.rgrc[2];
1878 else
1880 if (!(pWinpos->flags & SWP_NOMOVE) &&
1881 (pNewClientRect->left != client_rect.left ||
1882 pNewClientRect->top != client_rect.top))
1883 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1886 if (pWinpos->flags & (SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
1888 SetRectEmpty( &validRects[0] );
1889 SetRectEmpty( &validRects[1] );
1891 else get_valid_rects( &client_rect, pNewClientRect, wvrFlags, validRects );
1893 return wvrFlags;
1896 /* fix redundant flags and values in the WINDOWPOS structure */
1897 static BOOL fixup_flags( WINDOWPOS *winpos )
1899 HWND parent;
1900 RECT window_rect;
1901 POINT pt;
1902 WND *wndPtr = WIN_GetPtr( winpos->hwnd );
1903 BOOL ret = TRUE;
1905 if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
1907 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1908 return FALSE;
1910 winpos->hwnd = wndPtr->obj.handle; /* make it a full handle */
1912 /* Finally make sure that all coordinates are valid */
1913 if (winpos->x < -32768) winpos->x = -32768;
1914 else if (winpos->x > 32767) winpos->x = 32767;
1915 if (winpos->y < -32768) winpos->y = -32768;
1916 else if (winpos->y > 32767) winpos->y = 32767;
1918 if (winpos->cx < 0) winpos->cx = 0;
1919 else if (winpos->cx > 32767) winpos->cx = 32767;
1920 if (winpos->cy < 0) winpos->cy = 0;
1921 else if (winpos->cy > 32767) winpos->cy = 32767;
1923 parent = GetAncestor( winpos->hwnd, GA_PARENT );
1924 if (!IsWindowVisible( parent )) winpos->flags |= SWP_NOREDRAW;
1926 if (winpos->flags & SWP_HIDEWINDOW) wndPtr->flags |= WIN_HIDDEN;
1927 else if (winpos->flags & SWP_SHOWWINDOW) wndPtr->flags &= ~WIN_HIDDEN;
1929 if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
1930 else
1932 winpos->flags &= ~SWP_HIDEWINDOW;
1933 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
1936 WIN_GetRectangles( winpos->hwnd, COORDS_SCREEN, &window_rect, NULL );
1937 if ((window_rect.right - window_rect.left == winpos->cx) &&
1938 (window_rect.bottom - window_rect.top == winpos->cy))
1939 winpos->flags |= SWP_NOSIZE; /* Already the right size */
1941 pt.x = winpos->x;
1942 pt.y = winpos->y;
1943 ClientToScreen( parent, &pt );
1944 if ((window_rect.left == pt.x) && (window_rect.top == pt.y))
1945 winpos->flags |= SWP_NOMOVE; /* Already the right position */
1947 if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
1949 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)) && /* Bring to the top when activating */
1950 (winpos->flags & SWP_NOZORDER ||
1951 (winpos->hwndInsertAfter != HWND_TOPMOST && winpos->hwndInsertAfter != HWND_NOTOPMOST)))
1953 winpos->flags &= ~SWP_NOZORDER;
1954 winpos->hwndInsertAfter = HWND_TOP;
1958 /* Check hwndInsertAfter */
1959 if (winpos->flags & SWP_NOZORDER) goto done;
1961 if (winpos->hwndInsertAfter == HWND_TOP)
1963 if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1964 winpos->flags |= SWP_NOZORDER;
1966 else if (winpos->hwndInsertAfter == HWND_BOTTOM)
1968 if (!(wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
1969 winpos->flags |= SWP_NOZORDER;
1971 else if (winpos->hwndInsertAfter == HWND_TOPMOST)
1973 if ((wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1974 winpos->flags |= SWP_NOZORDER;
1976 else if (winpos->hwndInsertAfter == HWND_NOTOPMOST)
1978 if (!(wndPtr->dwExStyle & WS_EX_TOPMOST))
1979 winpos->flags |= SWP_NOZORDER;
1981 else
1983 if ((winpos->hwnd == winpos->hwndInsertAfter) ||
1984 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
1985 winpos->flags |= SWP_NOZORDER;
1987 done:
1988 WIN_ReleasePtr( wndPtr );
1989 return ret;
1993 /***********************************************************************
1994 * update_surface_region
1996 static void update_surface_region( HWND hwnd )
1998 NTSTATUS status;
1999 HRGN region = 0;
2000 RGNDATA *data;
2001 size_t size = 256;
2002 WND *win = WIN_GetPtr( hwnd );
2004 if (!win || win == WND_DESKTOP || win == WND_OTHER_PROCESS) return;
2005 if (!win->surface) goto done;
2009 if (!(data = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( RGNDATA, Buffer[size] )))) goto done;
2011 SERVER_START_REQ( get_surface_region )
2013 req->window = wine_server_user_handle( hwnd );
2014 wine_server_set_reply( req, data->Buffer, size );
2015 if (!(status = wine_server_call( req )))
2017 size_t reply_size = wine_server_reply_size( reply );
2018 if (reply_size)
2020 data->rdh.dwSize = sizeof(data->rdh);
2021 data->rdh.iType = RDH_RECTANGLES;
2022 data->rdh.nCount = reply_size / sizeof(RECT);
2023 data->rdh.nRgnSize = reply_size;
2024 region = ExtCreateRegion( NULL, data->rdh.dwSize + data->rdh.nRgnSize, data );
2025 OffsetRgn( region, -reply->visible_rect.left, -reply->visible_rect.top );
2028 else size = reply->total_size;
2030 SERVER_END_REQ;
2031 HeapFree( GetProcessHeap(), 0, data );
2032 } while (status == STATUS_BUFFER_OVERFLOW);
2034 if (status) goto done;
2036 win->surface->funcs->set_region( win->surface, region );
2037 if (region) DeleteObject( region );
2039 done:
2040 WIN_ReleasePtr( win );
2044 /***********************************************************************
2045 * set_window_pos
2047 * Backend implementation of SetWindowPos.
2049 BOOL set_window_pos( HWND hwnd, HWND insert_after, UINT swp_flags,
2050 const RECT *window_rect, const RECT *client_rect, const RECT *valid_rects )
2052 WND *win;
2053 HWND surface_win = 0, parent = GetAncestor( hwnd, GA_PARENT );
2054 BOOL ret;
2055 int old_width;
2056 RECT visible_rect, old_visible_rect, old_window_rect;
2057 struct window_surface *old_surface, *new_surface = NULL;
2059 if (!parent || parent == GetDesktopWindow())
2061 new_surface = &dummy_surface; /* provide a default surface for top-level windows */
2062 window_surface_add_ref( new_surface );
2064 visible_rect = *window_rect;
2065 USER_Driver->pWindowPosChanging( hwnd, insert_after, swp_flags,
2066 window_rect, client_rect, &visible_rect, &new_surface );
2068 WIN_GetRectangles( hwnd, COORDS_SCREEN, &old_window_rect, NULL );
2070 if (!(win = WIN_GetPtr( hwnd )) || win == WND_DESKTOP || win == WND_OTHER_PROCESS)
2072 if (new_surface) window_surface_release( new_surface );
2073 return FALSE;
2075 old_width = win->rectClient.right - win->rectClient.left;
2076 old_visible_rect = win->visible_rect;
2077 old_surface = win->surface;
2078 if (old_surface != new_surface) swp_flags |= SWP_FRAMECHANGED; /* force refreshing non-client area */
2080 SERVER_START_REQ( set_window_pos )
2082 req->handle = wine_server_user_handle( hwnd );
2083 req->previous = wine_server_user_handle( insert_after );
2084 req->swp_flags = swp_flags;
2085 req->window.left = window_rect->left;
2086 req->window.top = window_rect->top;
2087 req->window.right = window_rect->right;
2088 req->window.bottom = window_rect->bottom;
2089 req->client.left = client_rect->left;
2090 req->client.top = client_rect->top;
2091 req->client.right = client_rect->right;
2092 req->client.bottom = client_rect->bottom;
2093 if (memcmp( window_rect, &visible_rect, sizeof(RECT) ) || !IsRectEmpty( &valid_rects[0] ))
2095 wine_server_add_data( req, &visible_rect, sizeof(visible_rect) );
2096 if (!IsRectEmpty( &valid_rects[0] ))
2097 wine_server_add_data( req, valid_rects, 2 * sizeof(*valid_rects) );
2099 if (new_surface) req->paint_flags |= SET_WINPOS_PAINT_SURFACE;
2100 if (win->pixel_format) req->paint_flags |= SET_WINPOS_PIXEL_FORMAT;
2102 if ((ret = !wine_server_call( req )))
2104 win->dwStyle = reply->new_style;
2105 win->dwExStyle = reply->new_ex_style;
2106 win->rectWindow = *window_rect;
2107 win->rectClient = *client_rect;
2108 win->visible_rect = visible_rect;
2109 win->surface = new_surface;
2110 surface_win = wine_server_ptr_handle( reply->surface_win );
2111 if (GetWindowLongW( win->parent, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
2113 RECT client;
2114 GetClientRect( win->parent, &client );
2115 mirror_rect( &client, &win->rectWindow );
2116 mirror_rect( &client, &win->rectClient );
2117 mirror_rect( &client, &win->visible_rect );
2119 /* if an RTL window is resized the children have moved */
2120 if (win->dwExStyle & WS_EX_LAYOUTRTL && client_rect->right - client_rect->left != old_width)
2121 win->flags |= WIN_CHILDREN_MOVED;
2124 SERVER_END_REQ;
2126 if (ret)
2128 if (surface_win) update_surface_region( surface_win );
2129 if (((swp_flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) ||
2130 (swp_flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW | SWP_STATECHANGED | SWP_FRAMECHANGED)))
2131 invalidate_dce( win, &old_window_rect );
2134 WIN_ReleasePtr( win );
2136 if (ret)
2138 TRACE( "win %p surface %p -> %p\n", hwnd, old_surface, new_surface );
2139 register_window_surface( old_surface, new_surface );
2140 if (old_surface)
2142 if (!IsRectEmpty( valid_rects ))
2144 move_window_bits( hwnd, old_surface, new_surface, &visible_rect,
2145 &old_visible_rect, client_rect, valid_rects );
2146 valid_rects = NULL; /* prevent the driver from trying to also move the bits */
2148 window_surface_release( old_surface );
2150 USER_Driver->pWindowPosChanged( hwnd, insert_after, swp_flags, window_rect,
2151 client_rect, &visible_rect, valid_rects, new_surface );
2153 else if (new_surface) window_surface_release( new_surface );
2155 return ret;
2159 /***********************************************************************
2160 * USER_SetWindowPos
2162 * User32 internal function
2164 BOOL USER_SetWindowPos( WINDOWPOS * winpos )
2166 RECT newWindowRect, newClientRect, valid_rects[2];
2167 UINT orig_flags;
2169 orig_flags = winpos->flags;
2171 /* First, check z-order arguments. */
2172 if (!(winpos->flags & SWP_NOZORDER))
2174 /* fix sign extension */
2175 if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
2176 else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
2178 if (!(winpos->hwndInsertAfter == HWND_TOP ||
2179 winpos->hwndInsertAfter == HWND_BOTTOM ||
2180 winpos->hwndInsertAfter == HWND_TOPMOST ||
2181 winpos->hwndInsertAfter == HWND_NOTOPMOST))
2183 HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
2184 HWND insertafter_parent = GetAncestor( winpos->hwndInsertAfter, GA_PARENT );
2186 /* hwndInsertAfter must be a sibling of the window */
2187 if (!insertafter_parent) return FALSE;
2188 if (insertafter_parent != parent) return TRUE;
2192 /* Make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
2193 if (!(winpos->flags & SWP_NOMOVE))
2195 if (winpos->x < -32768) winpos->x = -32768;
2196 else if (winpos->x > 32767) winpos->x = 32767;
2197 if (winpos->y < -32768) winpos->y = -32768;
2198 else if (winpos->y > 32767) winpos->y = 32767;
2200 if (!(winpos->flags & SWP_NOSIZE))
2202 if (winpos->cx < 0) winpos->cx = 0;
2203 else if (winpos->cx > 32767) winpos->cx = 32767;
2204 if (winpos->cy < 0) winpos->cy = 0;
2205 else if (winpos->cy > 32767) winpos->cy = 32767;
2208 if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
2210 /* Fix redundant flags */
2211 if (!fixup_flags( winpos )) return FALSE;
2213 if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
2215 if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
2216 winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
2219 /* Common operations */
2221 SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect, valid_rects );
2223 if (!set_window_pos( winpos->hwnd, winpos->hwndInsertAfter, winpos->flags,
2224 &newWindowRect, &newClientRect, valid_rects ))
2225 return FALSE;
2227 /* erase parent when hiding or resizing child */
2228 if (!(orig_flags & SWP_DEFERERASE) &&
2229 ((orig_flags & SWP_HIDEWINDOW) ||
2230 (!(orig_flags & SWP_SHOWWINDOW) &&
2231 (winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOGEOMETRYCHANGE)))
2233 HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
2234 if (!parent || parent == GetDesktopWindow()) parent = winpos->hwnd;
2235 erase_now( parent, 0 );
2238 if( winpos->flags & SWP_HIDEWINDOW )
2239 HideCaret(winpos->hwnd);
2240 else if (winpos->flags & SWP_SHOWWINDOW)
2241 ShowCaret(winpos->hwnd);
2243 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)))
2245 /* child windows get WM_CHILDACTIVATE message */
2246 if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
2247 SendMessageW( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
2248 else
2249 SetForegroundWindow( winpos->hwnd );
2252 /* And last, send the WM_WINDOWPOSCHANGED message */
2254 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
2256 if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
2258 /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
2259 and always contains final window position.
2261 winpos->x = newWindowRect.left;
2262 winpos->y = newWindowRect.top;
2263 winpos->cx = newWindowRect.right - newWindowRect.left;
2264 winpos->cy = newWindowRect.bottom - newWindowRect.top;
2265 SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
2267 return TRUE;
2270 /***********************************************************************
2271 * SetWindowPos (USER32.@)
2273 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
2274 INT x, INT y, INT cx, INT cy, UINT flags )
2276 WINDOWPOS winpos;
2278 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2279 hwnd, hwndInsertAfter, x, y, cx, cy, flags);
2280 if(TRACE_ON(win)) dump_winpos_flags(flags);
2282 if (is_broadcast(hwnd))
2284 SetLastError( ERROR_INVALID_PARAMETER );
2285 return FALSE;
2288 winpos.hwnd = WIN_GetFullHandle(hwnd);
2289 winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
2290 winpos.x = x;
2291 winpos.y = y;
2292 winpos.cx = cx;
2293 winpos.cy = cy;
2294 winpos.flags = flags;
2296 if (WIN_IsCurrentThread( hwnd ))
2297 return USER_SetWindowPos(&winpos);
2299 return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
2303 /***********************************************************************
2304 * BeginDeferWindowPos (USER32.@)
2306 HDWP WINAPI BeginDeferWindowPos( INT count )
2308 HDWP handle = 0;
2309 DWP *pDWP;
2311 TRACE("%d\n", count);
2313 if (count < 0)
2315 SetLastError(ERROR_INVALID_PARAMETER);
2316 return 0;
2318 /* Windows allows zero count, in which case it allocates context for 8 moves */
2319 if (count == 0) count = 8;
2321 if (!(pDWP = HeapAlloc( GetProcessHeap(), 0, sizeof(DWP)))) return 0;
2323 pDWP->actualCount = 0;
2324 pDWP->suggestedCount = count;
2325 pDWP->hwndParent = 0;
2327 if (!(pDWP->winPos = HeapAlloc( GetProcessHeap(), 0, count * sizeof(WINDOWPOS) )) ||
2328 !(handle = alloc_user_handle( &pDWP->obj, USER_DWP )))
2330 HeapFree( GetProcessHeap(), 0, pDWP->winPos );
2331 HeapFree( GetProcessHeap(), 0, pDWP );
2334 TRACE("returning hdwp %p\n", handle);
2335 return handle;
2339 /***********************************************************************
2340 * DeferWindowPos (USER32.@)
2342 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
2343 INT x, INT y, INT cx, INT cy,
2344 UINT flags )
2346 DWP *pDWP;
2347 int i;
2348 HDWP retvalue = hdwp;
2350 TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2351 hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
2353 hwnd = WIN_GetFullHandle( hwnd );
2354 if (is_desktop_window( hwnd )) return 0;
2356 if (!(pDWP = get_user_handle_ptr( hdwp, USER_DWP ))) return 0;
2357 if (pDWP == OBJ_OTHER_PROCESS)
2359 FIXME( "other process handle %p?\n", hdwp );
2360 return 0;
2363 for (i = 0; i < pDWP->actualCount; i++)
2365 if (pDWP->winPos[i].hwnd == hwnd)
2367 /* Merge with the other changes */
2368 if (!(flags & SWP_NOZORDER))
2370 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
2372 if (!(flags & SWP_NOMOVE))
2374 pDWP->winPos[i].x = x;
2375 pDWP->winPos[i].y = y;
2377 if (!(flags & SWP_NOSIZE))
2379 pDWP->winPos[i].cx = cx;
2380 pDWP->winPos[i].cy = cy;
2382 pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
2383 SWP_NOZORDER | SWP_NOREDRAW |
2384 SWP_NOACTIVATE | SWP_NOCOPYBITS|
2385 SWP_NOOWNERZORDER);
2386 pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
2387 SWP_FRAMECHANGED);
2388 goto END;
2391 if (pDWP->actualCount >= pDWP->suggestedCount)
2393 WINDOWPOS *newpos = HeapReAlloc( GetProcessHeap(), 0, pDWP->winPos,
2394 pDWP->suggestedCount * 2 * sizeof(WINDOWPOS) );
2395 if (!newpos)
2397 retvalue = 0;
2398 goto END;
2400 pDWP->suggestedCount *= 2;
2401 pDWP->winPos = newpos;
2403 pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
2404 pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
2405 pDWP->winPos[pDWP->actualCount].x = x;
2406 pDWP->winPos[pDWP->actualCount].y = y;
2407 pDWP->winPos[pDWP->actualCount].cx = cx;
2408 pDWP->winPos[pDWP->actualCount].cy = cy;
2409 pDWP->winPos[pDWP->actualCount].flags = flags;
2410 pDWP->actualCount++;
2411 END:
2412 release_user_handle_ptr( pDWP );
2413 return retvalue;
2417 /***********************************************************************
2418 * EndDeferWindowPos (USER32.@)
2420 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
2422 DWP *pDWP;
2423 WINDOWPOS *winpos;
2424 BOOL res = TRUE;
2425 int i;
2427 TRACE("%p\n", hdwp);
2429 if (!(pDWP = free_user_handle( hdwp, USER_DWP ))) return FALSE;
2430 if (pDWP == OBJ_OTHER_PROCESS)
2432 FIXME( "other process handle %p?\n", hdwp );
2433 return FALSE;
2436 for (i = 0, winpos = pDWP->winPos; res && i < pDWP->actualCount; i++, winpos++)
2438 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2439 winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
2440 winpos->cx, winpos->cy, winpos->flags);
2442 if (WIN_IsCurrentThread( winpos->hwnd ))
2443 res = USER_SetWindowPos( winpos );
2444 else
2445 res = SendMessageW( winpos->hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)winpos );
2447 HeapFree( GetProcessHeap(), 0, pDWP->winPos );
2448 HeapFree( GetProcessHeap(), 0, pDWP );
2449 return res;
2453 /***********************************************************************
2454 * ArrangeIconicWindows (USER32.@)
2456 UINT WINAPI ArrangeIconicWindows( HWND parent )
2458 RECT rectParent;
2459 HWND hwndChild;
2460 INT x, y, xspacing, yspacing;
2462 GetClientRect( parent, &rectParent );
2463 x = rectParent.left;
2464 y = rectParent.bottom;
2465 xspacing = GetSystemMetrics(SM_CXICONSPACING);
2466 yspacing = GetSystemMetrics(SM_CYICONSPACING);
2468 hwndChild = GetWindow( parent, GW_CHILD );
2469 while (hwndChild)
2471 if( IsIconic( hwndChild ) )
2473 WINPOS_ShowIconTitle( hwndChild, FALSE );
2475 SetWindowPos( hwndChild, 0, x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
2476 y - yspacing - GetSystemMetrics(SM_CYICON)/2, 0, 0,
2477 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
2478 if( IsWindow(hwndChild) )
2479 WINPOS_ShowIconTitle(hwndChild , TRUE );
2481 if (x <= rectParent.right - xspacing) x += xspacing;
2482 else
2484 x = rectParent.left;
2485 y -= yspacing;
2488 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
2490 return yspacing;
2494 /***********************************************************************
2495 * draw_moving_frame
2497 * Draw the frame used when moving or resizing window.
2499 static void draw_moving_frame( HWND parent, HDC hdc, RECT *screen_rect, BOOL thickframe )
2501 RECT rect = *screen_rect;
2503 if (parent) MapWindowPoints( 0, parent, (POINT *)&rect, 2 );
2504 if (thickframe)
2506 const int width = GetSystemMetrics(SM_CXFRAME);
2507 const int height = GetSystemMetrics(SM_CYFRAME);
2509 HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
2510 PatBlt( hdc, rect.left, rect.top,
2511 rect.right - rect.left - width, height, PATINVERT );
2512 PatBlt( hdc, rect.left, rect.top + height, width,
2513 rect.bottom - rect.top - height, PATINVERT );
2514 PatBlt( hdc, rect.left + width, rect.bottom - 1,
2515 rect.right - rect.left - width, -height, PATINVERT );
2516 PatBlt( hdc, rect.right - 1, rect.top, -width,
2517 rect.bottom - rect.top - height, PATINVERT );
2518 SelectObject( hdc, hbrush );
2520 else DrawFocusRect( hdc, &rect );
2524 /***********************************************************************
2525 * start_size_move
2527 * Initialization of a move or resize, when initiated from a menu choice.
2528 * Return hit test code for caption or sizing border.
2530 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
2532 LONG hittest = 0;
2533 POINT pt;
2534 MSG msg;
2535 RECT rectWindow;
2537 GetWindowRect( hwnd, &rectWindow );
2539 if ((wParam & 0xfff0) == SC_MOVE)
2541 /* Move pointer at the center of the caption */
2542 RECT rect = rectWindow;
2543 /* Note: to be exactly centered we should take the different types
2544 * of border into account, but it shouldn't make more than a few pixels
2545 * of difference so let's not bother with that */
2546 rect.top += GetSystemMetrics(SM_CYBORDER);
2547 if (style & WS_SYSMENU)
2548 rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
2549 if (style & WS_MINIMIZEBOX)
2550 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
2551 if (style & WS_MAXIMIZEBOX)
2552 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
2553 pt.x = (rect.right + rect.left) / 2;
2554 pt.y = rect.top + GetSystemMetrics(SM_CYSIZE)/2;
2555 hittest = HTCAPTION;
2556 *capturePoint = pt;
2558 else /* SC_SIZE */
2560 SetCursor( LoadCursorW( 0, (LPWSTR)IDC_SIZEALL ) );
2561 pt.x = pt.y = 0;
2562 while(!hittest)
2564 if (!GetMessageW( &msg, 0, 0, 0 )) return 0;
2565 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2567 switch(msg.message)
2569 case WM_MOUSEMOVE:
2570 pt.x = min( max( msg.pt.x, rectWindow.left ), rectWindow.right - 1 );
2571 pt.y = min( max( msg.pt.y, rectWindow.top ), rectWindow.bottom - 1 );
2572 hittest = SendMessageW( hwnd, WM_NCHITTEST, 0, MAKELONG( pt.x, pt.y ) );
2573 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT)) hittest = 0;
2574 break;
2576 case WM_LBUTTONUP:
2577 return 0;
2579 case WM_KEYDOWN:
2580 switch(msg.wParam)
2582 case VK_UP:
2583 hittest = HTTOP;
2584 pt.x =(rectWindow.left+rectWindow.right)/2;
2585 pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
2586 break;
2587 case VK_DOWN:
2588 hittest = HTBOTTOM;
2589 pt.x =(rectWindow.left+rectWindow.right)/2;
2590 pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
2591 break;
2592 case VK_LEFT:
2593 hittest = HTLEFT;
2594 pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
2595 pt.y =(rectWindow.top+rectWindow.bottom)/2;
2596 break;
2597 case VK_RIGHT:
2598 hittest = HTRIGHT;
2599 pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
2600 pt.y =(rectWindow.top+rectWindow.bottom)/2;
2601 break;
2602 case VK_RETURN:
2603 case VK_ESCAPE:
2604 return 0;
2606 break;
2607 default:
2608 TranslateMessage( &msg );
2609 DispatchMessageW( &msg );
2610 break;
2613 *capturePoint = pt;
2615 SetCursorPos( pt.x, pt.y );
2616 SendMessageW( hwnd, WM_SETCURSOR, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
2617 return hittest;
2621 /***********************************************************************
2622 * WINPOS_SysCommandSizeMove
2624 * Perform SC_MOVE and SC_SIZE commands.
2626 void WINPOS_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
2628 MSG msg;
2629 RECT sizingRect, mouseRect, origRect;
2630 HDC hdc;
2631 HWND parent;
2632 LONG hittest = (LONG)(wParam & 0x0f);
2633 WPARAM syscommand = wParam & 0xfff0;
2634 HCURSOR hDragCursor = 0, hOldCursor = 0;
2635 POINT minTrack, maxTrack;
2636 POINT capturePoint, pt;
2637 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
2638 BOOL thickframe = HAS_THICKFRAME( style );
2639 BOOL iconic = style & WS_MINIMIZE;
2640 BOOL moved = FALSE;
2641 DWORD dwPoint = GetMessagePos ();
2642 BOOL DragFullWindows = TRUE;
2643 HMONITOR mon = 0;
2645 if (IsZoomed(hwnd) || !IsWindowVisible(hwnd)) return;
2647 pt.x = (short)LOWORD(dwPoint);
2648 pt.y = (short)HIWORD(dwPoint);
2649 capturePoint = pt;
2650 ClipCursor( NULL );
2652 TRACE("hwnd %p command %04lx, hittest %d, pos %d,%d\n",
2653 hwnd, syscommand, hittest, pt.x, pt.y);
2655 if (syscommand == SC_MOVE)
2657 if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
2658 if (!hittest) return;
2660 else /* SC_SIZE */
2662 if ( hittest && (syscommand != SC_MOUSEMENU) )
2663 hittest += (HTLEFT - WMSZ_LEFT);
2664 else
2666 set_capture_window( hwnd, GUI_INMOVESIZE, NULL );
2667 hittest = start_size_move( hwnd, wParam, &capturePoint, style );
2668 if (!hittest)
2670 set_capture_window( 0, GUI_INMOVESIZE, NULL );
2671 return;
2676 /* Get min/max info */
2678 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
2679 WIN_GetRectangles( hwnd, COORDS_PARENT, &sizingRect, NULL );
2680 origRect = sizingRect;
2681 if (style & WS_CHILD)
2683 parent = GetParent(hwnd);
2684 GetClientRect( parent, &mouseRect );
2685 MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
2686 MapWindowPoints( parent, 0, (LPPOINT)&sizingRect, 2 );
2688 else
2690 parent = 0;
2691 mouseRect = get_virtual_screen_rect();
2692 mon = MonitorFromPoint( pt, MONITOR_DEFAULTTONEAREST );
2695 if (ON_LEFT_BORDER(hittest))
2697 mouseRect.left = max( mouseRect.left, sizingRect.right-maxTrack.x+capturePoint.x-sizingRect.left );
2698 mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x+capturePoint.x-sizingRect.left );
2700 else if (ON_RIGHT_BORDER(hittest))
2702 mouseRect.left = max( mouseRect.left, sizingRect.left+minTrack.x+capturePoint.x-sizingRect.right );
2703 mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x+capturePoint.x-sizingRect.right );
2705 if (ON_TOP_BORDER(hittest))
2707 mouseRect.top = max( mouseRect.top, sizingRect.bottom-maxTrack.y+capturePoint.y-sizingRect.top );
2708 mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y+capturePoint.y-sizingRect.top);
2710 else if (ON_BOTTOM_BORDER(hittest))
2712 mouseRect.top = max( mouseRect.top, sizingRect.top+minTrack.y+capturePoint.y-sizingRect.bottom );
2713 mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y+capturePoint.y-sizingRect.bottom );
2716 /* Retrieve a default cache DC (without using the window style) */
2717 hdc = GetDCEx( parent, 0, DCX_CACHE );
2719 if( iconic ) /* create a cursor for dragging */
2721 hDragCursor = (HCURSOR)GetClassLongPtrW( hwnd, GCLP_HICON);
2722 if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageW( hwnd, WM_QUERYDRAGICON, 0, 0L);
2723 if( !hDragCursor ) iconic = FALSE;
2726 /* we only allow disabling the full window drag for child windows */
2727 if (parent) SystemParametersInfoW( SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0 );
2729 /* repaint the window before moving it around */
2730 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
2732 SendMessageW( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
2733 set_capture_window( hwnd, GUI_INMOVESIZE, NULL );
2735 while(1)
2737 int dx = 0, dy = 0;
2739 if (!GetMessageW( &msg, 0, 0, 0 )) break;
2740 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2742 /* Exit on button-up, Return, or Esc */
2743 if ((msg.message == WM_LBUTTONUP) ||
2744 ((msg.message == WM_KEYDOWN) &&
2745 ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
2747 if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
2749 TranslateMessage( &msg );
2750 DispatchMessageW( &msg );
2751 continue; /* We are not interested in other messages */
2754 pt = msg.pt;
2756 if (msg.message == WM_KEYDOWN) switch(msg.wParam)
2758 case VK_UP: pt.y -= 8; break;
2759 case VK_DOWN: pt.y += 8; break;
2760 case VK_LEFT: pt.x -= 8; break;
2761 case VK_RIGHT: pt.x += 8; break;
2764 pt.x = max( pt.x, mouseRect.left );
2765 pt.x = min( pt.x, mouseRect.right - 1 );
2766 pt.y = max( pt.y, mouseRect.top );
2767 pt.y = min( pt.y, mouseRect.bottom - 1 );
2769 if (!parent)
2771 HMONITOR newmon;
2772 MONITORINFO info;
2774 if ((newmon = MonitorFromPoint( pt, MONITOR_DEFAULTTONULL )))
2775 mon = newmon;
2777 info.cbSize = sizeof(info);
2778 if (mon && GetMonitorInfoW( mon, &info ))
2780 pt.x = max( pt.x, info.rcWork.left );
2781 pt.x = min( pt.x, info.rcWork.right - 1 );
2782 pt.y = max( pt.y, info.rcWork.top );
2783 pt.y = min( pt.y, info.rcWork.bottom - 1 );
2787 dx = pt.x - capturePoint.x;
2788 dy = pt.y - capturePoint.y;
2790 if (dx || dy)
2792 if( !moved )
2794 moved = TRUE;
2796 if( iconic ) /* ok, no system popup tracking */
2798 hOldCursor = SetCursor(hDragCursor);
2799 ShowCursor( TRUE );
2800 WINPOS_ShowIconTitle( hwnd, FALSE );
2802 else if(!DragFullWindows)
2803 draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2806 if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
2807 else
2809 if(!iconic && !DragFullWindows) draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2810 if (hittest == HTCAPTION) OffsetRect( &sizingRect, dx, dy );
2811 if (ON_LEFT_BORDER(hittest)) sizingRect.left += dx;
2812 else if (ON_RIGHT_BORDER(hittest)) sizingRect.right += dx;
2813 if (ON_TOP_BORDER(hittest)) sizingRect.top += dy;
2814 else if (ON_BOTTOM_BORDER(hittest)) sizingRect.bottom += dy;
2815 capturePoint = pt;
2817 /* determine the hit location */
2818 if (syscommand == SC_SIZE)
2820 WPARAM wpSizingHit = 0;
2822 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
2823 wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
2824 SendMessageW( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&sizingRect );
2826 else
2827 SendMessageW( hwnd, WM_MOVING, 0, (LPARAM)&sizingRect );
2829 if (!iconic)
2831 if(!DragFullWindows)
2832 draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2833 else
2835 RECT rect = sizingRect;
2836 MapWindowPoints( 0, parent, (POINT *)&rect, 2 );
2837 SetWindowPos( hwnd, 0, rect.left, rect.top,
2838 rect.right - rect.left, rect.bottom - rect.top,
2839 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2846 if( iconic )
2848 if( moved ) /* restore cursors, show icon title later on */
2850 ShowCursor( FALSE );
2851 SetCursor( hOldCursor );
2854 else if (moved && !DragFullWindows)
2856 draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2859 set_capture_window( 0, GUI_INMOVESIZE, NULL );
2860 ReleaseDC( parent, hdc );
2861 if (parent) MapWindowPoints( 0, parent, (POINT *)&sizingRect, 2 );
2863 if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
2864 moved = FALSE;
2866 SendMessageW( hwnd, WM_EXITSIZEMOVE, 0, 0 );
2867 SendMessageW( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
2869 /* window moved or resized */
2870 if (moved)
2872 /* if the moving/resizing isn't canceled call SetWindowPos
2873 * with the new position or the new size of the window
2875 if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
2877 /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
2878 if(!DragFullWindows || iconic)
2879 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
2880 sizingRect.right - sizingRect.left,
2881 sizingRect.bottom - sizingRect.top,
2882 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2884 else
2885 { /* restore previous size/position */
2886 if(DragFullWindows)
2887 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
2888 origRect.right - origRect.left,
2889 origRect.bottom - origRect.top,
2890 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2894 if (IsIconic(hwnd))
2896 /* Single click brings up the system menu when iconized */
2898 if( !moved )
2900 if(style & WS_SYSMENU )
2901 SendMessageW( hwnd, WM_SYSCOMMAND,
2902 SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
2904 else WINPOS_ShowIconTitle( hwnd, TRUE );