user32: Add a helper function for copying bits from a window surface.
[wine.git] / dlls / user32 / winpos.c
bloba37ab7c2eb25e2a9be88d50c696252927ea091e8
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)
48 #define SWP_AGG_NOCLIENTCHANGE \
49 (SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE)
51 #define HAS_DLGFRAME(style,exStyle) \
52 (((exStyle) & WS_EX_DLGMODALFRAME) || \
53 (((style) & WS_DLGFRAME) && !((style) & WS_BORDER)))
55 #define HAS_THICKFRAME(style) \
56 (((style) & WS_THICKFRAME) && \
57 !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
59 #define EMPTYPOINT(pt) ((pt).x == -1 && (pt).y == -1)
61 #define ON_LEFT_BORDER(hit) \
62 (((hit) == HTLEFT) || ((hit) == HTTOPLEFT) || ((hit) == HTBOTTOMLEFT))
63 #define ON_RIGHT_BORDER(hit) \
64 (((hit) == HTRIGHT) || ((hit) == HTTOPRIGHT) || ((hit) == HTBOTTOMRIGHT))
65 #define ON_TOP_BORDER(hit) \
66 (((hit) == HTTOP) || ((hit) == HTTOPLEFT) || ((hit) == HTTOPRIGHT))
67 #define ON_BOTTOM_BORDER(hit) \
68 (((hit) == HTBOTTOM) || ((hit) == HTBOTTOMLEFT) || ((hit) == HTBOTTOMRIGHT))
70 #define PLACE_MIN 0x0001
71 #define PLACE_MAX 0x0002
72 #define PLACE_RECT 0x0004
74 typedef struct
76 struct user_object obj;
77 INT actualCount;
78 INT suggestedCount;
79 HWND hwndParent;
80 WINDOWPOS *winPos;
81 } DWP;
84 /***********************************************************************
85 * SwitchToThisWindow (USER32.@)
87 void WINAPI SwitchToThisWindow( HWND hwnd, BOOL alt_tab )
89 if (IsIconic( hwnd )) ShowWindow( hwnd, SW_RESTORE );
90 else BringWindowToTop( hwnd );
94 /***********************************************************************
95 * GetWindowRect (USER32.@)
97 BOOL WINAPI GetWindowRect( HWND hwnd, LPRECT rect )
99 BOOL ret = WIN_GetRectangles( hwnd, COORDS_SCREEN, rect, NULL );
100 if (ret) TRACE( "hwnd %p %s\n", hwnd, wine_dbgstr_rect(rect) );
101 return ret;
105 /***********************************************************************
106 * GetWindowRgn (USER32.@)
108 int WINAPI GetWindowRgn ( HWND hwnd, HRGN hrgn )
110 int nRet = ERROR;
111 NTSTATUS status;
112 HRGN win_rgn = 0;
113 RGNDATA *data;
114 size_t size = 256;
118 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
120 SetLastError( ERROR_OUTOFMEMORY );
121 return ERROR;
123 SERVER_START_REQ( get_window_region )
125 req->window = wine_server_user_handle( hwnd );
126 wine_server_set_reply( req, data->Buffer, size );
127 if (!(status = wine_server_call( req )))
129 size_t reply_size = wine_server_reply_size( reply );
130 if (reply_size)
132 data->rdh.dwSize = sizeof(data->rdh);
133 data->rdh.iType = RDH_RECTANGLES;
134 data->rdh.nCount = reply_size / sizeof(RECT);
135 data->rdh.nRgnSize = reply_size;
136 win_rgn = ExtCreateRegion( NULL, data->rdh.dwSize + data->rdh.nRgnSize, data );
139 else size = reply->total_size;
141 SERVER_END_REQ;
142 HeapFree( GetProcessHeap(), 0, data );
143 } while (status == STATUS_BUFFER_OVERFLOW);
145 if (status) SetLastError( RtlNtStatusToDosError(status) );
146 else if (win_rgn)
148 nRet = CombineRgn( hrgn, win_rgn, 0, RGN_COPY );
149 DeleteObject( win_rgn );
151 return nRet;
154 /***********************************************************************
155 * GetWindowRgnBox (USER32.@)
157 int WINAPI GetWindowRgnBox( HWND hwnd, LPRECT prect )
159 int ret = ERROR;
160 HRGN hrgn;
162 if (!prect)
163 return ERROR;
165 if ((hrgn = CreateRectRgn(0, 0, 0, 0)))
167 if ((ret = GetWindowRgn( hwnd, hrgn )) != ERROR )
168 ret = GetRgnBox( hrgn, prect );
169 DeleteObject(hrgn);
172 return ret;
175 /***********************************************************************
176 * SetWindowRgn (USER32.@)
178 int WINAPI SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL bRedraw )
180 static const RECT empty_rect;
181 BOOL ret;
183 if (hrgn)
185 RGNDATA *data;
186 DWORD size;
188 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
189 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
190 if (!GetRegionData( hrgn, size, data ))
192 HeapFree( GetProcessHeap(), 0, data );
193 return FALSE;
195 SERVER_START_REQ( set_window_region )
197 req->window = wine_server_user_handle( hwnd );
198 req->redraw = (bRedraw != 0);
199 if (data->rdh.nCount)
200 wine_server_add_data( req, data->Buffer, data->rdh.nCount * sizeof(RECT) );
201 else
202 wine_server_add_data( req, &empty_rect, sizeof(empty_rect) );
203 ret = !wine_server_call_err( req );
205 SERVER_END_REQ;
206 HeapFree( GetProcessHeap(), 0, data );
208 else /* clear existing region */
210 SERVER_START_REQ( set_window_region )
212 req->window = wine_server_user_handle( hwnd );
213 req->redraw = (bRedraw != 0);
214 ret = !wine_server_call_err( req );
216 SERVER_END_REQ;
219 if (ret)
221 UINT swp_flags = SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE;
222 if (!bRedraw) swp_flags |= SWP_NOREDRAW;
223 SetWindowPos( hwnd, 0, 0, 0, 0, 0, swp_flags );
224 USER_Driver->pSetWindowRgn( hwnd, hrgn, bRedraw );
225 if (hrgn) DeleteObject( hrgn );
227 return ret;
231 /***********************************************************************
232 * GetClientRect (USER32.@)
234 BOOL WINAPI GetClientRect( HWND hwnd, LPRECT rect )
236 return WIN_GetRectangles( hwnd, COORDS_CLIENT, NULL, rect );
240 /***********************************************************************
241 * list_children_from_point
243 * Get the list of children that can contain point from the server.
244 * Point is in screen coordinates.
245 * Returned list must be freed by caller.
247 static HWND *list_children_from_point( HWND hwnd, POINT pt )
249 HWND *list;
250 int i, size = 128;
252 for (;;)
254 int count = 0;
256 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
258 SERVER_START_REQ( get_window_children_from_point )
260 req->parent = wine_server_user_handle( hwnd );
261 req->x = pt.x;
262 req->y = pt.y;
263 wine_server_set_reply( req, list, (size-1) * sizeof(user_handle_t) );
264 if (!wine_server_call( req )) count = reply->count;
266 SERVER_END_REQ;
267 if (count && count < size)
269 /* start from the end since HWND is potentially larger than user_handle_t */
270 for (i = count - 1; i >= 0; i--)
271 list[i] = wine_server_ptr_handle( ((user_handle_t *)list)[i] );
272 list[count] = 0;
273 return list;
275 HeapFree( GetProcessHeap(), 0, list );
276 if (!count) break;
277 size = count + 1; /* restart with a large enough buffer */
279 return NULL;
283 /***********************************************************************
284 * WINPOS_WindowFromPoint
286 * Find the window and hittest for a given point.
288 HWND WINPOS_WindowFromPoint( HWND hwndScope, POINT pt, INT *hittest )
290 int i, res;
291 HWND ret, *list;
293 if (!hwndScope) hwndScope = GetDesktopWindow();
295 *hittest = HTNOWHERE;
297 if (!(list = list_children_from_point( hwndScope, pt ))) return 0;
299 /* now determine the hittest */
301 for (i = 0; list[i]; i++)
303 LONG style = GetWindowLongW( list[i], GWL_STYLE );
305 /* If window is minimized or disabled, return at once */
306 if (style & WS_MINIMIZE)
308 *hittest = HTCAPTION;
309 break;
311 if (style & WS_DISABLED)
313 *hittest = HTERROR;
314 break;
316 /* Send WM_NCCHITTEST (if same thread) */
317 if (!WIN_IsCurrentThread( list[i] ))
319 *hittest = HTCLIENT;
320 break;
322 res = SendMessageW( list[i], WM_NCHITTEST, 0, MAKELONG(pt.x,pt.y) );
323 if (res != HTTRANSPARENT)
325 *hittest = res; /* Found the window */
326 break;
328 /* continue search with next window in z-order */
330 ret = list[i];
331 HeapFree( GetProcessHeap(), 0, list );
332 TRACE( "scope %p (%d,%d) returning %p\n", hwndScope, pt.x, pt.y, ret );
333 return ret;
337 /*******************************************************************
338 * WindowFromPoint (USER32.@)
340 HWND WINAPI WindowFromPoint( POINT pt )
342 INT hittest;
343 return WINPOS_WindowFromPoint( 0, pt, &hittest );
347 /*******************************************************************
348 * ChildWindowFromPoint (USER32.@)
350 HWND WINAPI ChildWindowFromPoint( HWND hwndParent, POINT pt )
352 return ChildWindowFromPointEx( hwndParent, pt, CWP_ALL );
355 /*******************************************************************
356 * RealChildWindowFromPoint (USER32.@)
358 HWND WINAPI RealChildWindowFromPoint( HWND hwndParent, POINT pt )
360 return ChildWindowFromPointEx( hwndParent, pt, CWP_SKIPTRANSPARENT | CWP_SKIPINVISIBLE );
363 /*******************************************************************
364 * ChildWindowFromPointEx (USER32.@)
366 HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
368 /* pt is in the client coordinates */
369 HWND *list;
370 int i;
371 RECT rect;
372 HWND retvalue;
374 GetClientRect( hwndParent, &rect );
375 if (!PtInRect( &rect, pt )) return 0;
376 if (!(list = WIN_ListChildren( hwndParent ))) return hwndParent;
378 for (i = 0; list[i]; i++)
380 if (!WIN_GetRectangles( list[i], COORDS_PARENT, &rect, NULL )) continue;
381 if (!PtInRect( &rect, pt )) continue;
382 if (uFlags & (CWP_SKIPINVISIBLE|CWP_SKIPDISABLED))
384 LONG style = GetWindowLongW( list[i], GWL_STYLE );
385 if ((uFlags & CWP_SKIPINVISIBLE) && !(style & WS_VISIBLE)) continue;
386 if ((uFlags & CWP_SKIPDISABLED) && (style & WS_DISABLED)) continue;
388 if (uFlags & CWP_SKIPTRANSPARENT)
390 if (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TRANSPARENT) continue;
392 break;
394 retvalue = list[i];
395 HeapFree( GetProcessHeap(), 0, list );
396 if (!retvalue) retvalue = hwndParent;
397 return retvalue;
401 /*******************************************************************
402 * WINPOS_GetWinOffset
404 * Calculate the offset between the origin of the two windows. Used
405 * to implement MapWindowPoints.
407 static BOOL WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo, BOOL *mirrored, POINT *ret_offset )
409 WND * wndPtr;
410 POINT offset;
411 BOOL mirror_from, mirror_to, ret;
412 HWND hwnd;
414 offset.x = offset.y = 0;
415 *mirrored = mirror_from = mirror_to = FALSE;
417 /* Translate source window origin to screen coords */
418 if (hwndFrom)
420 if (!(wndPtr = WIN_GetPtr( hwndFrom )))
422 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
423 return FALSE;
425 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
426 if (wndPtr != WND_DESKTOP)
428 if (wndPtr->dwExStyle & WS_EX_LAYOUTRTL)
430 mirror_from = TRUE;
431 offset.x += wndPtr->rectClient.right - wndPtr->rectClient.left;
433 while (wndPtr->parent)
435 offset.x += wndPtr->rectClient.left;
436 offset.y += wndPtr->rectClient.top;
437 hwnd = wndPtr->parent;
438 WIN_ReleasePtr( wndPtr );
439 if (!(wndPtr = WIN_GetPtr( hwnd ))) break;
440 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
441 if (wndPtr == WND_DESKTOP) break;
442 if (wndPtr->flags & WIN_CHILDREN_MOVED)
444 WIN_ReleasePtr( wndPtr );
445 goto other_process;
448 if (wndPtr && wndPtr != WND_DESKTOP) WIN_ReleasePtr( wndPtr );
452 /* Translate origin to destination window coords */
453 if (hwndTo)
455 if (!(wndPtr = WIN_GetPtr( hwndTo )))
457 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
458 return FALSE;
460 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
461 if (wndPtr != WND_DESKTOP)
463 if (wndPtr->dwExStyle & WS_EX_LAYOUTRTL)
465 mirror_to = TRUE;
466 offset.x -= wndPtr->rectClient.right - wndPtr->rectClient.left;
468 while (wndPtr->parent)
470 offset.x -= wndPtr->rectClient.left;
471 offset.y -= wndPtr->rectClient.top;
472 hwnd = wndPtr->parent;
473 WIN_ReleasePtr( wndPtr );
474 if (!(wndPtr = WIN_GetPtr( hwnd ))) break;
475 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
476 if (wndPtr == WND_DESKTOP) break;
477 if (wndPtr->flags & WIN_CHILDREN_MOVED)
479 WIN_ReleasePtr( wndPtr );
480 goto other_process;
483 if (wndPtr && wndPtr != WND_DESKTOP) WIN_ReleasePtr( wndPtr );
487 *mirrored = mirror_from ^ mirror_to;
488 if (mirror_from) offset.x = -offset.x;
489 *ret_offset = offset;
490 return TRUE;
492 other_process: /* one of the parents may belong to another process, do it the hard way */
493 SERVER_START_REQ( get_windows_offset )
495 req->from = wine_server_user_handle( hwndFrom );
496 req->to = wine_server_user_handle( hwndTo );
497 if ((ret = !wine_server_call_err( req )))
499 ret_offset->x = reply->x;
500 ret_offset->y = reply->y;
501 *mirrored = reply->mirror;
504 SERVER_END_REQ;
505 return ret;
508 /* map coordinates of a window region */
509 void map_window_region( HWND from, HWND to, HRGN hrgn )
511 BOOL mirrored;
512 POINT offset;
513 UINT i, size;
514 RGNDATA *data;
515 HRGN new_rgn;
516 RECT *rect;
518 if (!WINPOS_GetWinOffset( from, to, &mirrored, &offset )) return;
520 if (!mirrored)
522 OffsetRgn( hrgn, offset.x, offset.y );
523 return;
525 if (!(size = GetRegionData( hrgn, 0, NULL ))) return;
526 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return;
527 GetRegionData( hrgn, size, data );
528 rect = (RECT *)data->Buffer;
529 for (i = 0; i < data->rdh.nCount; i++)
531 int tmp = -(rect[i].left + offset.x);
532 rect[i].left = -(rect[i].right + offset.x);
533 rect[i].right = tmp;
534 rect[i].top += offset.y;
535 rect[i].bottom += offset.y;
537 if ((new_rgn = ExtCreateRegion( NULL, data->rdh.dwSize + data->rdh.nRgnSize, data )))
539 CombineRgn( hrgn, new_rgn, 0, RGN_COPY );
540 DeleteObject( new_rgn );
542 HeapFree( GetProcessHeap(), 0, data );
546 /*******************************************************************
547 * MapWindowPoints (USER32.@)
549 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
551 BOOL mirrored;
552 POINT offset;
553 UINT i;
555 if (!WINPOS_GetWinOffset( hwndFrom, hwndTo, &mirrored, &offset )) return 0;
557 for (i = 0; i < count; i++)
559 lppt[i].x += offset.x;
560 lppt[i].y += offset.y;
561 if (mirrored) lppt[i].x = -lppt[i].x;
563 if (mirrored && count == 2) /* special case for rectangle */
565 int tmp = lppt[0].x;
566 lppt[0].x = lppt[1].x;
567 lppt[1].x = tmp;
569 return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
573 /*******************************************************************
574 * ClientToScreen (USER32.@)
576 BOOL WINAPI ClientToScreen( HWND hwnd, LPPOINT lppnt )
578 POINT offset;
579 BOOL mirrored;
581 if (!hwnd)
583 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
584 return FALSE;
586 if (!WINPOS_GetWinOffset( hwnd, 0, &mirrored, &offset )) return FALSE;
587 lppnt->x += offset.x;
588 lppnt->y += offset.y;
589 if (mirrored) lppnt->x = -lppnt->x;
590 return TRUE;
594 /*******************************************************************
595 * ScreenToClient (USER32.@)
597 BOOL WINAPI ScreenToClient( HWND hwnd, LPPOINT lppnt )
599 POINT offset;
600 BOOL mirrored;
602 if (!hwnd)
604 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
605 return FALSE;
607 if (!WINPOS_GetWinOffset( 0, hwnd, &mirrored, &offset )) return FALSE;
608 lppnt->x += offset.x;
609 lppnt->y += offset.y;
610 if (mirrored) lppnt->x = -lppnt->x;
611 return TRUE;
615 /***********************************************************************
616 * IsIconic (USER32.@)
618 BOOL WINAPI IsIconic(HWND hWnd)
620 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
624 /***********************************************************************
625 * IsZoomed (USER32.@)
627 BOOL WINAPI IsZoomed(HWND hWnd)
629 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
633 /*******************************************************************
634 * AllowSetForegroundWindow (USER32.@)
636 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
638 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
639 * implemented, then fix this function. */
640 return TRUE;
644 /*******************************************************************
645 * LockSetForegroundWindow (USER32.@)
647 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
649 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
650 * implemented, then fix this function. */
651 return TRUE;
655 /***********************************************************************
656 * BringWindowToTop (USER32.@)
658 BOOL WINAPI BringWindowToTop( HWND hwnd )
660 return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
664 /***********************************************************************
665 * MoveWindow (USER32.@)
667 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
668 BOOL repaint )
670 int flags = SWP_NOZORDER | SWP_NOACTIVATE;
671 if (!repaint) flags |= SWP_NOREDRAW;
672 TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
673 return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
677 /***********************************************************************
678 * WINPOS_RedrawIconTitle
680 BOOL WINPOS_RedrawIconTitle( HWND hWnd )
682 HWND icon_title = 0;
683 WND *win = WIN_GetPtr( hWnd );
685 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
687 icon_title = win->icon_title;
688 WIN_ReleasePtr( win );
690 if (!icon_title) return FALSE;
691 SendMessageW( icon_title, WM_SHOWWINDOW, TRUE, 0 );
692 InvalidateRect( icon_title, NULL, TRUE );
693 return TRUE;
696 /***********************************************************************
697 * WINPOS_ShowIconTitle
699 static void WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
701 WND *win = WIN_GetPtr( hwnd );
702 HWND title = 0;
704 TRACE("%p %i\n", hwnd, (bShow != 0) );
706 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP) return;
707 if (win->rectWindow.left == -32000 || win->rectWindow.top == -32000)
709 TRACE( "not showing title for hidden icon %p\n", hwnd );
710 bShow = FALSE;
712 else title = win->icon_title;
713 WIN_ReleasePtr( win );
715 if (bShow)
717 if (!title)
719 title = ICONTITLE_Create( hwnd );
720 if (!(win = WIN_GetPtr( hwnd )) || win == WND_OTHER_PROCESS)
722 DestroyWindow( title );
723 return;
725 win->icon_title = title;
726 WIN_ReleasePtr( win );
728 if (!IsWindowVisible(title))
730 SendMessageW( title, WM_SHOWWINDOW, TRUE, 0 );
731 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
732 SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
735 else if (title) ShowWindow( title, SW_HIDE );
738 /*******************************************************************
739 * WINPOS_GetMinMaxInfo
741 * Get the minimized and maximized information for a window.
743 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
744 POINT *minTrack, POINT *maxTrack )
746 MINMAXINFO MinMax;
747 HMONITOR monitor;
748 INT xinc, yinc;
749 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
750 LONG adjustedStyle;
751 LONG exstyle = GetWindowLongW( hwnd, GWL_EXSTYLE );
752 RECT rc;
753 WND *win;
755 /* Compute default values */
757 GetWindowRect(hwnd, &rc);
758 MinMax.ptReserved.x = rc.left;
759 MinMax.ptReserved.y = rc.top;
761 if ((style & WS_CAPTION) == WS_CAPTION)
762 adjustedStyle = style & ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
763 else
764 adjustedStyle = style;
766 GetClientRect(GetAncestor(hwnd,GA_PARENT), &rc);
767 AdjustWindowRectEx(&rc, adjustedStyle, ((style & WS_POPUP) && GetMenu(hwnd)), exstyle);
769 xinc = -rc.left;
770 yinc = -rc.top;
772 MinMax.ptMaxSize.x = rc.right - rc.left;
773 MinMax.ptMaxSize.y = rc.bottom - rc.top;
774 if (style & (WS_DLGFRAME | WS_BORDER))
776 MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
777 MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
779 else
781 MinMax.ptMinTrackSize.x = 2 * xinc;
782 MinMax.ptMinTrackSize.y = 2 * yinc;
784 MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXMAXTRACK);
785 MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYMAXTRACK);
786 MinMax.ptMaxPosition.x = -xinc;
787 MinMax.ptMaxPosition.y = -yinc;
789 if ((win = WIN_GetPtr( hwnd )) && win != WND_DESKTOP && win != WND_OTHER_PROCESS)
791 if (!EMPTYPOINT(win->max_pos)) MinMax.ptMaxPosition = win->max_pos;
792 WIN_ReleasePtr( win );
795 SendMessageW( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
797 /* if the app didn't change the values, adapt them for the current monitor */
799 if ((monitor = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY )))
801 RECT rc_work;
802 MONITORINFO mon_info;
804 mon_info.cbSize = sizeof(mon_info);
805 GetMonitorInfoW( monitor, &mon_info );
807 rc_work = mon_info.rcMonitor;
809 if (style & WS_MAXIMIZEBOX)
811 if ((style & WS_CAPTION) == WS_CAPTION || !(style & (WS_CHILD | WS_POPUP)))
812 rc_work = mon_info.rcWork;
815 if (MinMax.ptMaxSize.x == GetSystemMetrics(SM_CXSCREEN) + 2 * xinc &&
816 MinMax.ptMaxSize.y == GetSystemMetrics(SM_CYSCREEN) + 2 * yinc)
818 MinMax.ptMaxSize.x = (rc_work.right - rc_work.left) + 2 * xinc;
819 MinMax.ptMaxSize.y = (rc_work.bottom - rc_work.top) + 2 * yinc;
821 if (MinMax.ptMaxPosition.x == -xinc && MinMax.ptMaxPosition.y == -yinc)
823 MinMax.ptMaxPosition.x = rc_work.left - xinc;
824 MinMax.ptMaxPosition.y = rc_work.top - yinc;
828 /* Some sanity checks */
830 TRACE("%d %d / %d %d / %d %d / %d %d\n",
831 MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
832 MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
833 MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
834 MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
835 MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
836 MinMax.ptMinTrackSize.x );
837 MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
838 MinMax.ptMinTrackSize.y );
840 if (maxSize) *maxSize = MinMax.ptMaxSize;
841 if (maxPos) *maxPos = MinMax.ptMaxPosition;
842 if (minTrack) *minTrack = MinMax.ptMinTrackSize;
843 if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
847 /***********************************************************************
848 * WINPOS_FindIconPos
850 * Find a suitable place for an iconic window.
852 static POINT WINPOS_FindIconPos( HWND hwnd, POINT pt )
854 RECT rect, rectParent;
855 HWND parent, child;
856 HRGN hrgn, tmp;
857 int x, y, xspacing, yspacing;
858 MINIMIZEDMETRICS metrics;
860 metrics.cbSize = sizeof(metrics);
861 SystemParametersInfoW( SPI_GETMINIMIZEDMETRICS, sizeof(metrics), &metrics, 0 );
863 parent = GetAncestor( hwnd, GA_PARENT );
864 if (parent == GetDesktopWindow())
866 MONITORINFO mon_info;
867 HMONITOR monitor = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY );
869 mon_info.cbSize = sizeof( mon_info );
870 GetMonitorInfoW( monitor, &mon_info );
871 rectParent = mon_info.rcWork;
873 else GetClientRect( parent, &rectParent );
875 if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
876 (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
877 return pt; /* The icon already has a suitable position */
879 xspacing = GetSystemMetrics(SM_CXICONSPACING);
880 yspacing = GetSystemMetrics(SM_CYICONSPACING);
882 /* Check if another icon already occupies this spot */
883 /* FIXME: this is completely inefficient */
885 hrgn = CreateRectRgn( 0, 0, 0, 0 );
886 tmp = CreateRectRgn( 0, 0, 0, 0 );
887 for (child = GetWindow( parent, GW_CHILD ); child; child = GetWindow( child, GW_HWNDNEXT ))
889 if (child == hwnd) continue;
890 if ((GetWindowLongW( child, GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != (WS_VISIBLE|WS_MINIMIZE))
891 continue;
892 if (WIN_GetRectangles( child, COORDS_PARENT, &rect, NULL ))
894 SetRectRgn( tmp, rect.left, rect.top, rect.right, rect.bottom );
895 CombineRgn( hrgn, hrgn, tmp, RGN_OR );
898 DeleteObject( tmp );
900 for (y = 0; y < (rectParent.bottom - rectParent.top) / yspacing; y++)
902 if (metrics.iArrange & ARW_STARTTOP)
904 rect.top = rectParent.top + y * yspacing;
905 rect.bottom = rect.top + yspacing;
907 else
909 rect.bottom = rectParent.bottom - y * yspacing;
910 rect.top = rect.bottom - yspacing;
912 for (x = 0; x < (rectParent.right - rectParent.left) / xspacing; x++)
914 if (metrics.iArrange & ARW_STARTRIGHT)
916 rect.right = rectParent.right - x * xspacing;
917 rect.left = rect.right - xspacing;
919 else
921 rect.left = rectParent.left + x * xspacing;
922 rect.right = rect.left + xspacing;
924 if (!RectInRegion( hrgn, &rect ))
926 /* No window was found, so it's OK for us */
927 pt.x = rect.left + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
928 pt.y = rect.top + (yspacing - GetSystemMetrics(SM_CYICON)) / 2;
929 DeleteObject( hrgn );
930 return pt;
934 DeleteObject( hrgn );
935 pt.x = pt.y = 0;
936 return pt;
940 /***********************************************************************
941 * WINPOS_MinMaximize
943 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
945 UINT swpFlags = 0;
946 POINT size;
947 LONG old_style;
948 WINDOWPLACEMENT wpl;
950 TRACE("%p %u\n", hwnd, cmd );
952 wpl.length = sizeof(wpl);
953 GetWindowPlacement( hwnd, &wpl );
955 if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
956 return SWP_NOSIZE | SWP_NOMOVE;
958 if (IsIconic( hwnd ))
960 switch (cmd)
962 case SW_SHOWMINNOACTIVE:
963 case SW_SHOWMINIMIZED:
964 case SW_FORCEMINIMIZE:
965 case SW_MINIMIZE:
966 return SWP_NOSIZE | SWP_NOMOVE;
968 if (!SendMessageW( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
969 swpFlags |= SWP_NOCOPYBITS;
972 switch( cmd )
974 case SW_SHOWMINNOACTIVE:
975 case SW_SHOWMINIMIZED:
976 case SW_FORCEMINIMIZE:
977 case SW_MINIMIZE:
978 if (IsZoomed( hwnd )) win_set_flags( hwnd, WIN_RESTORE_MAX, 0 );
979 else win_set_flags( hwnd, 0, WIN_RESTORE_MAX );
981 old_style = WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
983 wpl.ptMinPosition = WINPOS_FindIconPos( hwnd, wpl.ptMinPosition );
985 if (!(old_style & WS_MINIMIZE)) swpFlags |= SWP_STATECHANGED;
986 SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
987 wpl.ptMinPosition.x + GetSystemMetrics(SM_CXICON),
988 wpl.ptMinPosition.y + GetSystemMetrics(SM_CYICON) );
989 swpFlags |= SWP_NOCOPYBITS;
990 break;
992 case SW_MAXIMIZE:
993 old_style = GetWindowLongW( hwnd, GWL_STYLE );
994 if ((old_style & WS_MAXIMIZE) && (old_style & WS_VISIBLE)) return SWP_NOSIZE | SWP_NOMOVE;
996 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
998 old_style = WIN_SetStyle( hwnd, WS_MAXIMIZE, WS_MINIMIZE );
999 if (old_style & WS_MINIMIZE)
1001 win_set_flags( hwnd, WIN_RESTORE_MAX, 0 );
1002 WINPOS_ShowIconTitle( hwnd, FALSE );
1005 if (!(old_style & WS_MAXIMIZE)) swpFlags |= SWP_STATECHANGED;
1006 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y,
1007 wpl.ptMaxPosition.x + size.x, wpl.ptMaxPosition.y + size.y );
1008 break;
1010 case SW_SHOWNOACTIVATE:
1011 win_set_flags( hwnd, 0, WIN_RESTORE_MAX );
1012 /* fall through */
1013 case SW_SHOWNORMAL:
1014 case SW_RESTORE:
1015 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
1016 old_style = WIN_SetStyle( hwnd, 0, WS_MINIMIZE | WS_MAXIMIZE );
1017 if (old_style & WS_MINIMIZE)
1019 WINPOS_ShowIconTitle( hwnd, FALSE );
1020 if (win_get_flags( hwnd ) & WIN_RESTORE_MAX)
1022 /* Restore to maximized position */
1023 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
1024 WIN_SetStyle( hwnd, WS_MAXIMIZE, 0 );
1025 swpFlags |= SWP_STATECHANGED;
1026 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y,
1027 wpl.ptMaxPosition.x + size.x, wpl.ptMaxPosition.y + size.y );
1028 break;
1031 else if (!(old_style & WS_MAXIMIZE)) break;
1033 swpFlags |= SWP_STATECHANGED;
1035 /* Restore to normal position */
1037 *rect = wpl.rcNormalPosition;
1038 break;
1041 return swpFlags;
1045 /***********************************************************************
1046 * show_window
1048 * Implementation of ShowWindow and ShowWindowAsync.
1050 static BOOL show_window( HWND hwnd, INT cmd )
1052 WND *wndPtr;
1053 HWND parent;
1054 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1055 BOOL wasVisible = (style & WS_VISIBLE) != 0;
1056 BOOL showFlag = TRUE;
1057 RECT newPos = {0, 0, 0, 0};
1058 UINT swp = 0;
1060 TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);
1062 switch(cmd)
1064 case SW_HIDE:
1065 if (!wasVisible) return FALSE;
1066 showFlag = FALSE;
1067 swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1068 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1069 break;
1071 case SW_SHOWMINNOACTIVE:
1072 case SW_MINIMIZE:
1073 case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
1074 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1075 /* fall through */
1076 case SW_SHOWMINIMIZED:
1077 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1078 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
1079 if ((style & WS_MINIMIZE) && wasVisible) return TRUE;
1080 break;
1082 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
1083 if (!wasVisible) swp |= SWP_SHOWWINDOW;
1084 swp |= SWP_FRAMECHANGED;
1085 swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
1086 if ((style & WS_MAXIMIZE) && wasVisible) return TRUE;
1087 break;
1089 case SW_SHOWNA:
1090 swp |= SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1091 if (style & WS_CHILD) swp |= SWP_NOZORDER;
1092 break;
1093 case SW_SHOW:
1094 if (wasVisible) return TRUE;
1095 swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1096 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1097 break;
1099 case SW_SHOWNOACTIVATE:
1100 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1101 /* fall through */
1102 case SW_RESTORE:
1103 /* fall through */
1104 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
1105 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
1106 if (!wasVisible) swp |= SWP_SHOWWINDOW;
1107 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1109 swp |= SWP_FRAMECHANGED;
1110 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
1112 else
1114 if (wasVisible) return TRUE;
1115 swp |= SWP_NOSIZE | SWP_NOMOVE;
1117 if (style & WS_CHILD && !(swp & SWP_STATECHANGED)) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1118 break;
1119 default:
1120 return wasVisible;
1123 if ((showFlag != wasVisible || cmd == SW_SHOWNA) && cmd != SW_SHOWMAXIMIZED && !(swp & SWP_STATECHANGED))
1125 SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
1126 if (!IsWindow( hwnd )) return wasVisible;
1129 swp = USER_Driver->pShowWindow( hwnd, cmd, &newPos, swp );
1131 parent = GetAncestor( hwnd, GA_PARENT );
1132 if (parent && !IsWindowVisible( parent ) && !(swp & SWP_STATECHANGED))
1134 /* if parent is not visible simply toggle WS_VISIBLE and return */
1135 if (showFlag) WIN_SetStyle( hwnd, WS_VISIBLE, 0 );
1136 else WIN_SetStyle( hwnd, 0, WS_VISIBLE );
1138 else
1139 SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1140 newPos.right - newPos.left, newPos.bottom - newPos.top, swp );
1142 if (cmd == SW_HIDE)
1144 HWND hFocus;
1146 WINPOS_ShowIconTitle( hwnd, FALSE );
1148 /* FIXME: This will cause the window to be activated irrespective
1149 * of whether it is owned by the same thread. Has to be done
1150 * asynchronously.
1153 if (hwnd == GetActiveWindow())
1154 WINPOS_ActivateOtherWindow(hwnd);
1156 /* Revert focus to parent */
1157 hFocus = GetFocus();
1158 if (hwnd == hFocus)
1160 HWND parent = GetAncestor(hwnd, GA_PARENT);
1161 if (parent == GetDesktopWindow()) parent = 0;
1162 SetFocus(parent);
1164 return wasVisible;
1167 if (IsIconic(hwnd)) WINPOS_ShowIconTitle( hwnd, TRUE );
1169 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return wasVisible;
1171 if (wndPtr->flags & WIN_NEED_SIZE)
1173 /* should happen only in CreateWindowEx() */
1174 int wParam = SIZE_RESTORED;
1175 RECT client;
1176 LPARAM lparam;
1178 WIN_GetRectangles( hwnd, COORDS_PARENT, NULL, &client );
1179 lparam = MAKELONG( client.right - client.left, client.bottom - client.top );
1180 wndPtr->flags &= ~WIN_NEED_SIZE;
1181 if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1182 else if (wndPtr->dwStyle & WS_MINIMIZE)
1184 wParam = SIZE_MINIMIZED;
1185 lparam = 0;
1187 WIN_ReleasePtr( wndPtr );
1189 SendMessageW( hwnd, WM_SIZE, wParam, lparam );
1190 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( client.left, client.top ));
1192 else WIN_ReleasePtr( wndPtr );
1194 /* if previous state was minimized Windows sets focus to the window */
1195 if (style & WS_MINIMIZE) SetFocus( hwnd );
1197 return wasVisible;
1201 /***********************************************************************
1202 * ShowWindowAsync (USER32.@)
1204 * doesn't wait; returns immediately.
1205 * used by threads to toggle windows in other (possibly hanging) threads
1207 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
1209 HWND full_handle;
1211 if (is_broadcast(hwnd))
1213 SetLastError( ERROR_INVALID_PARAMETER );
1214 return FALSE;
1217 if ((full_handle = WIN_IsCurrentThread( hwnd )))
1218 return show_window( full_handle, cmd );
1220 return SendNotifyMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
1224 /***********************************************************************
1225 * ShowWindow (USER32.@)
1227 BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
1229 HWND full_handle;
1231 if (is_broadcast(hwnd))
1233 SetLastError( ERROR_INVALID_PARAMETER );
1234 return FALSE;
1236 if ((full_handle = WIN_IsCurrentThread( hwnd )))
1237 return show_window( full_handle, cmd );
1239 if ((cmd == SW_HIDE) && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
1240 return FALSE;
1242 return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
1246 /***********************************************************************
1247 * GetInternalWindowPos (USER32.@)
1249 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
1250 LPPOINT ptIcon )
1252 WINDOWPLACEMENT wndpl;
1253 if (GetWindowPlacement( hwnd, &wndpl ))
1255 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
1256 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
1257 return wndpl.showCmd;
1259 return 0;
1263 /***********************************************************************
1264 * GetWindowPlacement (USER32.@)
1266 * Win95:
1267 * Fails if wndpl->length of Win95 (!) apps is invalid.
1269 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
1271 WND *pWnd = WIN_GetPtr( hwnd );
1273 if (!pWnd) return FALSE;
1275 if (pWnd == WND_DESKTOP)
1277 wndpl->length = sizeof(*wndpl);
1278 wndpl->showCmd = SW_SHOWNORMAL;
1279 wndpl->flags = 0;
1280 wndpl->ptMinPosition.x = -1;
1281 wndpl->ptMinPosition.y = -1;
1282 wndpl->ptMaxPosition.x = -1;
1283 wndpl->ptMaxPosition.y = -1;
1284 GetWindowRect( hwnd, &wndpl->rcNormalPosition );
1285 return TRUE;
1287 if (pWnd == WND_OTHER_PROCESS)
1289 if (!IsWindow( hwnd )) return FALSE;
1290 FIXME( "not supported on other process window %p\n", hwnd );
1291 /* provide some dummy information */
1292 wndpl->length = sizeof(*wndpl);
1293 wndpl->showCmd = SW_SHOWNORMAL;
1294 wndpl->flags = 0;
1295 wndpl->ptMinPosition.x = -1;
1296 wndpl->ptMinPosition.y = -1;
1297 wndpl->ptMaxPosition.x = -1;
1298 wndpl->ptMaxPosition.y = -1;
1299 GetWindowRect( hwnd, &wndpl->rcNormalPosition );
1300 return TRUE;
1303 /* update the placement according to the current style */
1304 if (pWnd->dwStyle & WS_MINIMIZE)
1306 pWnd->min_pos.x = pWnd->rectWindow.left;
1307 pWnd->min_pos.y = pWnd->rectWindow.top;
1309 else if (pWnd->dwStyle & WS_MAXIMIZE)
1311 pWnd->max_pos.x = pWnd->rectWindow.left;
1312 pWnd->max_pos.y = pWnd->rectWindow.top;
1314 else
1316 pWnd->normal_rect = pWnd->rectWindow;
1319 wndpl->length = sizeof(*wndpl);
1320 if( pWnd->dwStyle & WS_MINIMIZE )
1321 wndpl->showCmd = SW_SHOWMINIMIZED;
1322 else
1323 wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
1324 if( pWnd->flags & WIN_RESTORE_MAX )
1325 wndpl->flags = WPF_RESTORETOMAXIMIZED;
1326 else
1327 wndpl->flags = 0;
1328 wndpl->ptMinPosition = pWnd->min_pos;
1329 wndpl->ptMaxPosition = pWnd->max_pos;
1330 wndpl->rcNormalPosition = pWnd->normal_rect;
1331 WIN_ReleasePtr( pWnd );
1333 TRACE( "%p: returning min %d,%d max %d,%d normal %s\n",
1334 hwnd, wndpl->ptMinPosition.x, wndpl->ptMinPosition.y,
1335 wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y,
1336 wine_dbgstr_rect(&wndpl->rcNormalPosition) );
1337 return TRUE;
1340 /* make sure the specified rect is visible on screen */
1341 static void make_rect_onscreen( RECT *rect )
1343 MONITORINFO info;
1344 HMONITOR monitor = MonitorFromRect( rect, MONITOR_DEFAULTTONEAREST );
1346 info.cbSize = sizeof(info);
1347 if (!monitor || !GetMonitorInfoW( monitor, &info )) return;
1348 /* FIXME: map coordinates from rcWork to rcMonitor */
1349 if (rect->right <= info.rcWork.left)
1351 rect->right += info.rcWork.left - rect->left;
1352 rect->left = info.rcWork.left;
1354 else if (rect->left >= info.rcWork.right)
1356 rect->left += info.rcWork.right - rect->right;
1357 rect->right = info.rcWork.right;
1359 if (rect->bottom <= info.rcWork.top)
1361 rect->bottom += info.rcWork.top - rect->top;
1362 rect->top = info.rcWork.top;
1364 else if (rect->top >= info.rcWork.bottom)
1366 rect->top += info.rcWork.bottom - rect->bottom;
1367 rect->bottom = info.rcWork.bottom;
1371 /* make sure the specified point is visible on screen */
1372 static void make_point_onscreen( POINT *pt )
1374 RECT rect;
1376 SetRect( &rect, pt->x, pt->y, pt->x + 1, pt->y + 1 );
1377 make_rect_onscreen( &rect );
1378 pt->x = rect.left;
1379 pt->y = rect.top;
1383 /***********************************************************************
1384 * WINPOS_SetPlacement
1386 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
1388 DWORD style;
1389 WND *pWnd = WIN_GetPtr( hwnd );
1390 WINDOWPLACEMENT wp = *wndpl;
1392 if (flags & PLACE_MIN) make_point_onscreen( &wp.ptMinPosition );
1393 if (flags & PLACE_MAX) make_point_onscreen( &wp.ptMaxPosition );
1394 if (flags & PLACE_RECT) make_rect_onscreen( &wp.rcNormalPosition );
1396 TRACE( "%p: setting min %d,%d max %d,%d normal %s flags %x ajusted to min %d,%d max %d,%d normal %s\n",
1397 hwnd, wndpl->ptMinPosition.x, wndpl->ptMinPosition.y,
1398 wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y,
1399 wine_dbgstr_rect(&wndpl->rcNormalPosition), flags,
1400 wp.ptMinPosition.x, wp.ptMinPosition.y, wp.ptMaxPosition.x, wp.ptMaxPosition.y,
1401 wine_dbgstr_rect(&wp.rcNormalPosition) );
1403 if (!pWnd || pWnd == WND_OTHER_PROCESS || pWnd == WND_DESKTOP) return FALSE;
1405 if( flags & PLACE_MIN ) pWnd->min_pos = wp.ptMinPosition;
1406 if( flags & PLACE_MAX ) pWnd->max_pos = wp.ptMaxPosition;
1407 if( flags & PLACE_RECT) pWnd->normal_rect = wp.rcNormalPosition;
1409 style = pWnd->dwStyle;
1411 WIN_ReleasePtr( pWnd );
1413 if( style & WS_MINIMIZE )
1415 if (flags & PLACE_MIN)
1417 WINPOS_ShowIconTitle( hwnd, FALSE );
1418 SetWindowPos( hwnd, 0, wp.ptMinPosition.x, wp.ptMinPosition.y, 0, 0,
1419 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1422 else if( style & WS_MAXIMIZE )
1424 if (flags & PLACE_MAX)
1425 SetWindowPos( hwnd, 0, wp.ptMaxPosition.x, wp.ptMaxPosition.y, 0, 0,
1426 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1428 else if( flags & PLACE_RECT )
1429 SetWindowPos( hwnd, 0, wp.rcNormalPosition.left, wp.rcNormalPosition.top,
1430 wp.rcNormalPosition.right - wp.rcNormalPosition.left,
1431 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top,
1432 SWP_NOZORDER | SWP_NOACTIVATE );
1434 ShowWindow( hwnd, wndpl->showCmd );
1436 if (IsIconic( hwnd ))
1438 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE) WINPOS_ShowIconTitle( hwnd, TRUE );
1440 /* SDK: ...valid only the next time... */
1441 if( wndpl->flags & WPF_RESTORETOMAXIMIZED )
1442 win_set_flags( hwnd, WIN_RESTORE_MAX, 0 );
1444 return TRUE;
1448 /***********************************************************************
1449 * SetWindowPlacement (USER32.@)
1451 * Win95:
1452 * Fails if wndpl->length of Win95 (!) apps is invalid.
1454 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
1456 UINT flags = PLACE_MAX | PLACE_RECT;
1457 if (!wpl) return FALSE;
1458 if (wpl->flags & WPF_SETMINPOSITION) flags |= PLACE_MIN;
1459 return WINPOS_SetPlacement( hwnd, wpl, flags );
1463 /***********************************************************************
1464 * AnimateWindow (USER32.@)
1465 * Shows/Hides a window with an animation
1466 * NO ANIMATION YET
1468 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
1470 FIXME("partial stub\n");
1472 /* If trying to show/hide and it's already *
1473 * shown/hidden or invalid window, fail with *
1474 * invalid parameter */
1475 if(!IsWindow(hwnd) ||
1476 (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
1477 (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
1479 SetLastError(ERROR_INVALID_PARAMETER);
1480 return FALSE;
1483 ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1485 return TRUE;
1488 /***********************************************************************
1489 * SetInternalWindowPos (USER32.@)
1491 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1492 LPRECT rect, LPPOINT pt )
1494 WINDOWPLACEMENT wndpl;
1495 UINT flags;
1497 wndpl.length = sizeof(wndpl);
1498 wndpl.showCmd = showCmd;
1499 wndpl.flags = flags = 0;
1501 if( pt )
1503 flags |= PLACE_MIN;
1504 wndpl.flags |= WPF_SETMINPOSITION;
1505 wndpl.ptMinPosition = *pt;
1507 if( rect )
1509 flags |= PLACE_RECT;
1510 wndpl.rcNormalPosition = *rect;
1512 WINPOS_SetPlacement( hwnd, &wndpl, flags );
1516 /*******************************************************************
1517 * can_activate_window
1519 * Check if we can activate the specified window.
1521 static BOOL can_activate_window( HWND hwnd )
1523 LONG style;
1525 if (!hwnd) return FALSE;
1526 style = GetWindowLongW( hwnd, GWL_STYLE );
1527 if (!(style & WS_VISIBLE)) return FALSE;
1528 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1529 return !(style & WS_DISABLED);
1533 /*******************************************************************
1534 * WINPOS_ActivateOtherWindow
1536 * Activates window other than pWnd.
1538 void WINPOS_ActivateOtherWindow(HWND hwnd)
1540 HWND hwndTo, fg;
1542 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1544 hwndTo = GetAncestor( hwndTo, GA_ROOT );
1545 if (can_activate_window( hwndTo )) goto done;
1548 hwndTo = hwnd;
1549 for (;;)
1551 if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1552 if (can_activate_window( hwndTo )) goto done;
1555 hwndTo = GetTopWindow( 0 );
1556 for (;;)
1558 if (hwndTo == hwnd)
1560 hwndTo = 0;
1561 break;
1563 if (can_activate_window( hwndTo )) goto done;
1564 if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1567 done:
1568 fg = GetForegroundWindow();
1569 TRACE("win = %p fg = %p\n", hwndTo, fg);
1570 if (!fg || (hwnd == fg))
1572 if (SetForegroundWindow( hwndTo )) return;
1574 if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1578 /***********************************************************************
1579 * WINPOS_HandleWindowPosChanging
1581 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1583 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1585 POINT minTrack, maxTrack;
1586 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1588 if (winpos->flags & SWP_NOSIZE) return 0;
1589 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1591 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1592 if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1593 if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1594 if (!(style & WS_MINIMIZE))
1596 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1597 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1600 return 0;
1604 /***********************************************************************
1605 * dump_winpos_flags
1607 static void dump_winpos_flags(UINT flags)
1609 static const DWORD dumped_flags = (SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOREDRAW |
1610 SWP_NOACTIVATE | SWP_FRAMECHANGED | SWP_SHOWWINDOW |
1611 SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOOWNERZORDER |
1612 SWP_NOSENDCHANGING | SWP_DEFERERASE | SWP_ASYNCWINDOWPOS |
1613 SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_STATECHANGED);
1614 TRACE("flags:");
1615 if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1616 if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1617 if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1618 if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1619 if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1620 if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1621 if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1622 if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1623 if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1624 if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1625 if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1626 if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1627 if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1628 if(flags & SWP_NOCLIENTSIZE) TRACE(" SWP_NOCLIENTSIZE");
1629 if(flags & SWP_NOCLIENTMOVE) TRACE(" SWP_NOCLIENTMOVE");
1630 if(flags & SWP_STATECHANGED) TRACE(" SWP_STATECHANGED");
1632 if(flags & ~dumped_flags) TRACE(" %08x", flags & ~dumped_flags);
1633 TRACE("\n");
1636 /***********************************************************************
1637 * SWP_DoWinPosChanging
1639 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
1641 WND *wndPtr;
1642 RECT window_rect, client_rect;
1644 /* Send WM_WINDOWPOSCHANGING message */
1646 if (!(pWinpos->flags & SWP_NOSENDCHANGING)
1647 && !((pWinpos->flags & SWP_AGG_NOCLIENTCHANGE) && (pWinpos->flags & SWP_SHOWWINDOW)))
1648 SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
1650 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) ||
1651 wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
1653 /* Calculate new position and size */
1655 WIN_GetRectangles( pWinpos->hwnd, COORDS_PARENT, &window_rect, &client_rect );
1656 *pNewWindowRect = window_rect;
1657 *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? window_rect : client_rect;
1659 if (!(pWinpos->flags & SWP_NOSIZE))
1661 if (wndPtr->dwStyle & WS_MINIMIZE)
1663 pNewWindowRect->right = pNewWindowRect->left + GetSystemMetrics(SM_CXICON);
1664 pNewWindowRect->bottom = pNewWindowRect->top + GetSystemMetrics(SM_CYICON);
1666 else
1668 pNewWindowRect->right = pNewWindowRect->left + pWinpos->cx;
1669 pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
1672 if (!(pWinpos->flags & SWP_NOMOVE))
1674 pNewWindowRect->left = pWinpos->x;
1675 pNewWindowRect->top = pWinpos->y;
1676 pNewWindowRect->right += pWinpos->x - window_rect.left;
1677 pNewWindowRect->bottom += pWinpos->y - window_rect.top;
1679 OffsetRect( pNewClientRect, pWinpos->x - window_rect.left,
1680 pWinpos->y - window_rect.top );
1682 pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
1684 TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
1685 pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
1686 pWinpos->cx, pWinpos->cy, pWinpos->flags );
1687 TRACE( "current %s style %08x new %s\n",
1688 wine_dbgstr_rect( &window_rect ), wndPtr->dwStyle,
1689 wine_dbgstr_rect( pNewWindowRect ));
1691 WIN_ReleasePtr( wndPtr );
1692 return TRUE;
1695 /***********************************************************************
1696 * get_valid_rects
1698 * Compute the valid rects from the old and new client rect and WVR_* flags.
1699 * Helper for WM_NCCALCSIZE handling.
1701 static inline void get_valid_rects( const RECT *old_client, const RECT *new_client, UINT flags,
1702 RECT *valid )
1704 int cx, cy;
1706 if (flags & WVR_REDRAW)
1708 SetRectEmpty( &valid[0] );
1709 SetRectEmpty( &valid[1] );
1710 return;
1713 if (flags & WVR_VALIDRECTS)
1715 if (!IntersectRect( &valid[0], &valid[0], new_client ) ||
1716 !IntersectRect( &valid[1], &valid[1], old_client ))
1718 SetRectEmpty( &valid[0] );
1719 SetRectEmpty( &valid[1] );
1720 return;
1722 flags = WVR_ALIGNLEFT | WVR_ALIGNTOP;
1724 else
1726 valid[0] = *new_client;
1727 valid[1] = *old_client;
1730 /* make sure the rectangles have the same size */
1731 cx = min( valid[0].right - valid[0].left, valid[1].right - valid[1].left );
1732 cy = min( valid[0].bottom - valid[0].top, valid[1].bottom - valid[1].top );
1734 if (flags & WVR_ALIGNBOTTOM)
1736 valid[0].top = valid[0].bottom - cy;
1737 valid[1].top = valid[1].bottom - cy;
1739 else
1741 valid[0].bottom = valid[0].top + cy;
1742 valid[1].bottom = valid[1].top + cy;
1744 if (flags & WVR_ALIGNRIGHT)
1746 valid[0].left = valid[0].right - cx;
1747 valid[1].left = valid[1].right - cx;
1749 else
1751 valid[0].right = valid[0].left + cx;
1752 valid[1].right = valid[1].left + cx;
1757 /***********************************************************************
1758 * SWP_DoOwnedPopups
1760 * fix Z order taking into account owned popups -
1761 * basically we need to maintain them above the window that owns them
1763 * FIXME: hide/show owned popups when owner visibility changes.
1765 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
1767 HWND owner, *list = NULL;
1768 unsigned int i;
1770 TRACE("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
1772 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) return hwndInsertAfter;
1774 if ((owner = GetWindow( hwnd, GW_OWNER )))
1776 /* make sure this popup stays above the owner */
1778 if (hwndInsertAfter != HWND_TOPMOST)
1780 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return hwndInsertAfter;
1782 for (i = 0; list[i]; i++)
1784 BOOL topmost = (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST) != 0;
1786 if (list[i] == owner)
1788 if (i > 0) hwndInsertAfter = list[i-1];
1789 else hwndInsertAfter = topmost ? HWND_TOPMOST : HWND_TOP;
1790 break;
1793 if (hwndInsertAfter == HWND_TOP || hwndInsertAfter == HWND_NOTOPMOST)
1795 if (!topmost) break;
1797 else if (list[i] == hwndInsertAfter) break;
1802 if (hwndInsertAfter == HWND_BOTTOM) goto done;
1803 if (!list && !(list = WIN_ListChildren( GetDesktopWindow() ))) goto done;
1805 i = 0;
1806 if (hwndInsertAfter == HWND_TOP || hwndInsertAfter == HWND_NOTOPMOST)
1808 if (hwndInsertAfter == HWND_NOTOPMOST || !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_TOPMOST))
1810 /* skip all the topmost windows */
1811 while (list[i] && (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST)) i++;
1814 else if (hwndInsertAfter != HWND_TOPMOST)
1816 /* skip windows that are already placed correctly */
1817 for (i = 0; list[i]; i++)
1819 if (list[i] == hwndInsertAfter) break;
1820 if (list[i] == hwnd) goto done; /* nothing to do if window is moving backwards in z-order */
1824 for ( ; list[i]; i++)
1826 if (list[i] == hwnd) break;
1827 if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
1828 TRACE( "moving %p owned by %p after %p\n", list[i], hwnd, hwndInsertAfter );
1829 SetWindowPos( list[i], hwndInsertAfter, 0, 0, 0, 0,
1830 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING | SWP_DEFERERASE );
1831 hwndInsertAfter = list[i];
1834 done:
1835 HeapFree( GetProcessHeap(), 0, list );
1836 return hwndInsertAfter;
1839 /***********************************************************************
1840 * SWP_DoNCCalcSize
1842 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect,
1843 RECT *validRects )
1845 UINT wvrFlags = 0;
1846 RECT window_rect, client_rect;
1848 WIN_GetRectangles( pWinpos->hwnd, COORDS_PARENT, &window_rect, &client_rect );
1850 /* Send WM_NCCALCSIZE message to get new client area */
1851 if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
1853 NCCALCSIZE_PARAMS params;
1854 WINDOWPOS winposCopy;
1856 params.rgrc[0] = *pNewWindowRect;
1857 params.rgrc[1] = window_rect;
1858 params.rgrc[2] = client_rect;
1859 params.lppos = &winposCopy;
1860 winposCopy = *pWinpos;
1862 wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
1864 *pNewClientRect = params.rgrc[0];
1866 TRACE( "hwnd %p old win %s old client %s new win %s new client %s\n", pWinpos->hwnd,
1867 wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(&client_rect),
1868 wine_dbgstr_rect(pNewWindowRect), wine_dbgstr_rect(pNewClientRect) );
1870 if( pNewClientRect->left != client_rect.left ||
1871 pNewClientRect->top != client_rect.top )
1872 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1874 if( (pNewClientRect->right - pNewClientRect->left !=
1875 client_rect.right - client_rect.left))
1876 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1877 else
1878 wvrFlags &= ~WVR_HREDRAW;
1880 if (pNewClientRect->bottom - pNewClientRect->top !=
1881 client_rect.bottom - client_rect.top)
1882 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
1883 else
1884 wvrFlags &= ~WVR_VREDRAW;
1886 validRects[0] = params.rgrc[1];
1887 validRects[1] = params.rgrc[2];
1889 else
1891 if (!(pWinpos->flags & SWP_NOMOVE) &&
1892 (pNewClientRect->left != client_rect.left ||
1893 pNewClientRect->top != client_rect.top))
1894 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
1897 if (pWinpos->flags & (SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
1899 SetRectEmpty( &validRects[0] );
1900 SetRectEmpty( &validRects[1] );
1902 else get_valid_rects( &client_rect, pNewClientRect, wvrFlags, validRects );
1904 return wvrFlags;
1907 /* fix redundant flags and values in the WINDOWPOS structure */
1908 static BOOL fixup_flags( WINDOWPOS *winpos )
1910 HWND parent;
1911 RECT window_rect;
1912 POINT pt;
1913 WND *wndPtr = WIN_GetPtr( winpos->hwnd );
1914 BOOL ret = TRUE;
1916 if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
1918 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1919 return FALSE;
1921 winpos->hwnd = wndPtr->obj.handle; /* make it a full handle */
1923 /* Finally make sure that all coordinates are valid */
1924 if (winpos->x < -32768) winpos->x = -32768;
1925 else if (winpos->x > 32767) winpos->x = 32767;
1926 if (winpos->y < -32768) winpos->y = -32768;
1927 else if (winpos->y > 32767) winpos->y = 32767;
1929 if (winpos->cx < 0) winpos->cx = 0;
1930 else if (winpos->cx > 32767) winpos->cx = 32767;
1931 if (winpos->cy < 0) winpos->cy = 0;
1932 else if (winpos->cy > 32767) winpos->cy = 32767;
1934 parent = GetAncestor( winpos->hwnd, GA_PARENT );
1935 if (!IsWindowVisible( parent )) winpos->flags |= SWP_NOREDRAW;
1937 if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
1938 else
1940 winpos->flags &= ~SWP_HIDEWINDOW;
1941 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
1944 WIN_GetRectangles( winpos->hwnd, COORDS_SCREEN, &window_rect, NULL );
1945 if ((window_rect.right - window_rect.left == winpos->cx) &&
1946 (window_rect.bottom - window_rect.top == winpos->cy))
1947 winpos->flags |= SWP_NOSIZE; /* Already the right size */
1949 pt.x = winpos->x;
1950 pt.y = winpos->y;
1951 ClientToScreen( parent, &pt );
1952 if ((window_rect.left == pt.x) && (window_rect.top == pt.y))
1953 winpos->flags |= SWP_NOMOVE; /* Already the right position */
1955 if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
1957 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)) && /* Bring to the top when activating */
1958 (winpos->flags & SWP_NOZORDER ||
1959 (winpos->hwndInsertAfter != HWND_TOPMOST && winpos->hwndInsertAfter != HWND_NOTOPMOST)))
1961 winpos->flags &= ~SWP_NOZORDER;
1962 winpos->hwndInsertAfter = HWND_TOP;
1966 /* Check hwndInsertAfter */
1967 if (winpos->flags & SWP_NOZORDER) goto done;
1969 if (winpos->hwndInsertAfter == HWND_TOP)
1971 if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1972 winpos->flags |= SWP_NOZORDER;
1974 else if (winpos->hwndInsertAfter == HWND_BOTTOM)
1976 if (!(wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
1977 winpos->flags |= SWP_NOZORDER;
1979 else if (winpos->hwndInsertAfter == HWND_TOPMOST)
1981 if ((wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
1982 winpos->flags |= SWP_NOZORDER;
1984 else if (winpos->hwndInsertAfter == HWND_NOTOPMOST)
1986 if (!(wndPtr->dwExStyle & WS_EX_TOPMOST))
1987 winpos->flags |= SWP_NOZORDER;
1989 else
1991 if ((winpos->hwnd == winpos->hwndInsertAfter) ||
1992 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
1993 winpos->flags |= SWP_NOZORDER;
1995 done:
1996 WIN_ReleasePtr( wndPtr );
1997 return ret;
2001 /***********************************************************************
2002 * update_surface_region
2004 static void update_surface_region( HWND hwnd )
2006 NTSTATUS status;
2007 HRGN region = 0;
2008 RGNDATA *data;
2009 size_t size = 256;
2010 WND *win = WIN_GetPtr( hwnd );
2012 if (!win || win == WND_DESKTOP || win == WND_OTHER_PROCESS) return;
2013 if (!win->surface) goto done;
2017 if (!(data = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( RGNDATA, Buffer[size] )))) goto done;
2019 SERVER_START_REQ( get_surface_region )
2021 req->window = wine_server_user_handle( hwnd );
2022 wine_server_set_reply( req, data->Buffer, size );
2023 if (!(status = wine_server_call( req )))
2025 size_t reply_size = wine_server_reply_size( reply );
2026 if (reply_size)
2028 data->rdh.dwSize = sizeof(data->rdh);
2029 data->rdh.iType = RDH_RECTANGLES;
2030 data->rdh.nCount = reply_size / sizeof(RECT);
2031 data->rdh.nRgnSize = reply_size;
2032 region = ExtCreateRegion( NULL, data->rdh.dwSize + data->rdh.nRgnSize, data );
2033 OffsetRgn( region, -reply->visible_rect.left, -reply->visible_rect.top );
2036 else size = reply->total_size;
2038 SERVER_END_REQ;
2039 HeapFree( GetProcessHeap(), 0, data );
2040 } while (status == STATUS_BUFFER_OVERFLOW);
2042 if (status) goto done;
2044 win->surface->funcs->set_region( win->surface, region );
2045 if (region) DeleteObject( region );
2047 done:
2048 WIN_ReleasePtr( win );
2052 /***********************************************************************
2053 * set_window_pos
2055 * Backend implementation of SetWindowPos.
2057 BOOL set_window_pos( HWND hwnd, HWND insert_after, UINT swp_flags,
2058 const RECT *window_rect, const RECT *client_rect, const RECT *valid_rects )
2060 WND *win;
2061 HWND surface_win = 0, parent = GetAncestor( hwnd, GA_PARENT );
2062 BOOL ret, needs_update = FALSE;
2063 int old_width;
2064 RECT visible_rect, old_visible_rect, old_window_rect;
2065 struct window_surface *old_surface, *new_surface = NULL;
2067 if (!parent || parent == GetDesktopWindow())
2069 new_surface = &dummy_surface; /* provide a default surface for top-level windows */
2070 window_surface_add_ref( new_surface );
2072 visible_rect = *window_rect;
2073 USER_Driver->pWindowPosChanging( hwnd, insert_after, swp_flags,
2074 window_rect, client_rect, &visible_rect, &new_surface );
2076 WIN_GetRectangles( hwnd, COORDS_SCREEN, &old_window_rect, NULL );
2078 if (!(win = WIN_GetPtr( hwnd )) || win == WND_DESKTOP || win == WND_OTHER_PROCESS)
2080 if (new_surface) window_surface_release( new_surface );
2081 return FALSE;
2083 old_width = win->rectClient.right - win->rectClient.left;
2084 old_visible_rect = win->visible_rect;
2085 old_surface = win->surface;
2086 if (old_surface != new_surface) swp_flags |= SWP_FRAMECHANGED; /* force refreshing non-client area */
2088 SERVER_START_REQ( set_window_pos )
2090 req->handle = wine_server_user_handle( hwnd );
2091 req->previous = wine_server_user_handle( insert_after );
2092 req->swp_flags = swp_flags;
2093 req->window.left = window_rect->left;
2094 req->window.top = window_rect->top;
2095 req->window.right = window_rect->right;
2096 req->window.bottom = window_rect->bottom;
2097 req->client.left = client_rect->left;
2098 req->client.top = client_rect->top;
2099 req->client.right = client_rect->right;
2100 req->client.bottom = client_rect->bottom;
2101 if (!EqualRect( window_rect, &visible_rect ) || !IsRectEmpty( &valid_rects[0] ))
2103 wine_server_add_data( req, &visible_rect, sizeof(visible_rect) );
2104 if (!IsRectEmpty( &valid_rects[0] ))
2105 wine_server_add_data( req, valid_rects, 2 * sizeof(*valid_rects) );
2107 if (new_surface) req->paint_flags |= SET_WINPOS_PAINT_SURFACE;
2108 if (win->pixel_format) req->paint_flags |= SET_WINPOS_PIXEL_FORMAT;
2110 if ((ret = !wine_server_call( req )))
2112 win->dwStyle = reply->new_style;
2113 win->dwExStyle = reply->new_ex_style;
2114 win->rectWindow = *window_rect;
2115 win->rectClient = *client_rect;
2116 win->visible_rect = visible_rect;
2117 win->surface = new_surface;
2118 surface_win = wine_server_ptr_handle( reply->surface_win );
2119 needs_update = reply->needs_update;
2120 if (GetWindowLongW( win->parent, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
2122 RECT client;
2123 GetClientRect( win->parent, &client );
2124 mirror_rect( &client, &win->rectWindow );
2125 mirror_rect( &client, &win->rectClient );
2126 mirror_rect( &client, &win->visible_rect );
2128 /* if an RTL window is resized the children have moved */
2129 if (win->dwExStyle & WS_EX_LAYOUTRTL && client_rect->right - client_rect->left != old_width)
2130 win->flags |= WIN_CHILDREN_MOVED;
2133 SERVER_END_REQ;
2135 if (ret)
2137 if (needs_update) update_surface_region( surface_win );
2138 if (((swp_flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) ||
2139 (swp_flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW | SWP_STATECHANGED | SWP_FRAMECHANGED)))
2140 invalidate_dce( win, &old_window_rect );
2143 WIN_ReleasePtr( win );
2145 if (ret)
2147 TRACE( "win %p surface %p -> %p\n", hwnd, old_surface, new_surface );
2148 register_window_surface( old_surface, new_surface );
2149 if (old_surface)
2151 if (!IsRectEmpty( valid_rects ))
2153 move_window_bits( hwnd, old_surface, new_surface, &visible_rect,
2154 &old_visible_rect, window_rect, valid_rects );
2155 valid_rects = NULL; /* prevent the driver from trying to also move the bits */
2157 window_surface_release( old_surface );
2159 USER_Driver->pWindowPosChanged( hwnd, insert_after, swp_flags, window_rect,
2160 client_rect, &visible_rect, valid_rects, new_surface );
2162 else if (new_surface) window_surface_release( new_surface );
2164 return ret;
2168 /***********************************************************************
2169 * USER_SetWindowPos
2171 * User32 internal function
2173 BOOL USER_SetWindowPos( WINDOWPOS * winpos )
2175 RECT newWindowRect, newClientRect, valid_rects[2];
2176 UINT orig_flags;
2178 orig_flags = winpos->flags;
2180 /* First, check z-order arguments. */
2181 if (!(winpos->flags & SWP_NOZORDER))
2183 /* fix sign extension */
2184 if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
2185 else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
2187 if (!(winpos->hwndInsertAfter == HWND_TOP ||
2188 winpos->hwndInsertAfter == HWND_BOTTOM ||
2189 winpos->hwndInsertAfter == HWND_TOPMOST ||
2190 winpos->hwndInsertAfter == HWND_NOTOPMOST))
2192 HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
2193 HWND insertafter_parent = GetAncestor( winpos->hwndInsertAfter, GA_PARENT );
2195 /* hwndInsertAfter must be a sibling of the window */
2196 if (!insertafter_parent) return FALSE;
2197 if (insertafter_parent != parent) return TRUE;
2201 /* Make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
2202 if (!(winpos->flags & SWP_NOMOVE))
2204 if (winpos->x < -32768) winpos->x = -32768;
2205 else if (winpos->x > 32767) winpos->x = 32767;
2206 if (winpos->y < -32768) winpos->y = -32768;
2207 else if (winpos->y > 32767) winpos->y = 32767;
2209 if (!(winpos->flags & SWP_NOSIZE))
2211 if (winpos->cx < 0) winpos->cx = 0;
2212 else if (winpos->cx > 32767) winpos->cx = 32767;
2213 if (winpos->cy < 0) winpos->cy = 0;
2214 else if (winpos->cy > 32767) winpos->cy = 32767;
2217 if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
2219 /* Fix redundant flags */
2220 if (!fixup_flags( winpos )) return FALSE;
2222 if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
2224 if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
2225 winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
2228 /* Common operations */
2230 SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect, valid_rects );
2232 if (!set_window_pos( winpos->hwnd, winpos->hwndInsertAfter, winpos->flags,
2233 &newWindowRect, &newClientRect, valid_rects ))
2234 return FALSE;
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 if(!(orig_flags & SWP_DEFERERASE))
2254 /* erase parent when hiding or resizing child */
2255 if ((orig_flags & SWP_HIDEWINDOW) ||
2256 (!(orig_flags & SWP_SHOWWINDOW) &&
2257 (winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOGEOMETRYCHANGE))
2259 HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
2260 if (!parent || parent == GetDesktopWindow()) parent = winpos->hwnd;
2261 erase_now( parent, 0 );
2264 /* Give newly shown windows a chance to redraw */
2265 if(((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE)
2266 && !(orig_flags & SWP_AGG_NOCLIENTCHANGE) && (orig_flags & SWP_SHOWWINDOW))
2268 erase_now(winpos->hwnd, 0);
2272 /* And last, send the WM_WINDOWPOSCHANGED message */
2274 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
2276 if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE)
2277 && !((orig_flags & SWP_AGG_NOCLIENTCHANGE) && (orig_flags & SWP_SHOWWINDOW)))
2279 /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
2280 and always contains final window position.
2282 winpos->x = newWindowRect.left;
2283 winpos->y = newWindowRect.top;
2284 winpos->cx = newWindowRect.right - newWindowRect.left;
2285 winpos->cy = newWindowRect.bottom - newWindowRect.top;
2286 SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
2288 return TRUE;
2291 /***********************************************************************
2292 * SetWindowPos (USER32.@)
2294 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
2295 INT x, INT y, INT cx, INT cy, UINT flags )
2297 WINDOWPOS winpos;
2299 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2300 hwnd, hwndInsertAfter, x, y, cx, cy, flags);
2301 if(TRACE_ON(win)) dump_winpos_flags(flags);
2303 if (is_broadcast(hwnd))
2305 SetLastError( ERROR_INVALID_PARAMETER );
2306 return FALSE;
2309 winpos.hwnd = WIN_GetFullHandle(hwnd);
2310 winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
2311 winpos.x = x;
2312 winpos.y = y;
2313 winpos.cx = cx;
2314 winpos.cy = cy;
2315 winpos.flags = flags;
2317 if (WIN_IsCurrentThread( hwnd ))
2318 return USER_SetWindowPos(&winpos);
2320 return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
2324 /***********************************************************************
2325 * BeginDeferWindowPos (USER32.@)
2327 HDWP WINAPI BeginDeferWindowPos( INT count )
2329 HDWP handle = 0;
2330 DWP *pDWP;
2332 TRACE("%d\n", count);
2334 if (count < 0)
2336 SetLastError(ERROR_INVALID_PARAMETER);
2337 return 0;
2339 /* Windows allows zero count, in which case it allocates context for 8 moves */
2340 if (count == 0) count = 8;
2342 if (!(pDWP = HeapAlloc( GetProcessHeap(), 0, sizeof(DWP)))) return 0;
2344 pDWP->actualCount = 0;
2345 pDWP->suggestedCount = count;
2346 pDWP->hwndParent = 0;
2348 if (!(pDWP->winPos = HeapAlloc( GetProcessHeap(), 0, count * sizeof(WINDOWPOS) )) ||
2349 !(handle = alloc_user_handle( &pDWP->obj, USER_DWP )))
2351 HeapFree( GetProcessHeap(), 0, pDWP->winPos );
2352 HeapFree( GetProcessHeap(), 0, pDWP );
2355 TRACE("returning hdwp %p\n", handle);
2356 return handle;
2360 /***********************************************************************
2361 * DeferWindowPos (USER32.@)
2363 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
2364 INT x, INT y, INT cx, INT cy,
2365 UINT flags )
2367 DWP *pDWP;
2368 int i;
2369 HDWP retvalue = hdwp;
2371 TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2372 hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
2374 hwnd = WIN_GetFullHandle( hwnd );
2375 if (is_desktop_window( hwnd ) || !IsWindow( hwnd ))
2377 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2378 return 0;
2381 if (!(pDWP = get_user_handle_ptr( hdwp, USER_DWP ))) return 0;
2382 if (pDWP == OBJ_OTHER_PROCESS)
2384 FIXME( "other process handle %p?\n", hdwp );
2385 return 0;
2388 for (i = 0; i < pDWP->actualCount; i++)
2390 if (pDWP->winPos[i].hwnd == hwnd)
2392 /* Merge with the other changes */
2393 if (!(flags & SWP_NOZORDER))
2395 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
2397 if (!(flags & SWP_NOMOVE))
2399 pDWP->winPos[i].x = x;
2400 pDWP->winPos[i].y = y;
2402 if (!(flags & SWP_NOSIZE))
2404 pDWP->winPos[i].cx = cx;
2405 pDWP->winPos[i].cy = cy;
2407 pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
2408 SWP_NOZORDER | SWP_NOREDRAW |
2409 SWP_NOACTIVATE | SWP_NOCOPYBITS|
2410 SWP_NOOWNERZORDER);
2411 pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
2412 SWP_FRAMECHANGED);
2413 goto END;
2416 if (pDWP->actualCount >= pDWP->suggestedCount)
2418 WINDOWPOS *newpos = HeapReAlloc( GetProcessHeap(), 0, pDWP->winPos,
2419 pDWP->suggestedCount * 2 * sizeof(WINDOWPOS) );
2420 if (!newpos)
2422 retvalue = 0;
2423 goto END;
2425 pDWP->suggestedCount *= 2;
2426 pDWP->winPos = newpos;
2428 pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
2429 pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
2430 pDWP->winPos[pDWP->actualCount].x = x;
2431 pDWP->winPos[pDWP->actualCount].y = y;
2432 pDWP->winPos[pDWP->actualCount].cx = cx;
2433 pDWP->winPos[pDWP->actualCount].cy = cy;
2434 pDWP->winPos[pDWP->actualCount].flags = flags;
2435 pDWP->actualCount++;
2436 END:
2437 release_user_handle_ptr( pDWP );
2438 return retvalue;
2442 /***********************************************************************
2443 * EndDeferWindowPos (USER32.@)
2445 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
2447 DWP *pDWP;
2448 WINDOWPOS *winpos;
2449 int i;
2451 TRACE("%p\n", hdwp);
2453 if (!(pDWP = free_user_handle( hdwp, USER_DWP ))) return FALSE;
2454 if (pDWP == OBJ_OTHER_PROCESS)
2456 FIXME( "other process handle %p?\n", hdwp );
2457 return FALSE;
2460 for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
2462 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
2463 winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
2464 winpos->cx, winpos->cy, winpos->flags);
2466 if (WIN_IsCurrentThread( winpos->hwnd ))
2467 USER_SetWindowPos( winpos );
2468 else
2469 SendMessageW( winpos->hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)winpos );
2471 HeapFree( GetProcessHeap(), 0, pDWP->winPos );
2472 HeapFree( GetProcessHeap(), 0, pDWP );
2473 return TRUE;
2477 /***********************************************************************
2478 * ArrangeIconicWindows (USER32.@)
2480 UINT WINAPI ArrangeIconicWindows( HWND parent )
2482 RECT rectParent;
2483 HWND hwndChild;
2484 INT x, y, xspacing, yspacing;
2485 POINT pt;
2486 MINIMIZEDMETRICS metrics;
2488 metrics.cbSize = sizeof(metrics);
2489 SystemParametersInfoW( SPI_GETMINIMIZEDMETRICS, sizeof(metrics), &metrics, 0 );
2491 if (parent == GetDesktopWindow())
2493 MONITORINFO mon_info;
2494 HMONITOR monitor = MonitorFromWindow( 0, MONITOR_DEFAULTTOPRIMARY );
2496 mon_info.cbSize = sizeof( mon_info );
2497 GetMonitorInfoW( monitor, &mon_info );
2498 rectParent = mon_info.rcWork;
2500 else GetClientRect( parent, &rectParent );
2502 x = y = 0;
2503 xspacing = GetSystemMetrics(SM_CXICONSPACING);
2504 yspacing = GetSystemMetrics(SM_CYICONSPACING);
2506 hwndChild = GetWindow( parent, GW_CHILD );
2507 while (hwndChild)
2509 if( IsIconic( hwndChild ) )
2511 WINPOS_ShowIconTitle( hwndChild, FALSE );
2513 if (metrics.iArrange & ARW_STARTRIGHT)
2514 pt.x = rectParent.right - (x + 1) * xspacing;
2515 else
2516 pt.x = rectParent.left + x * xspacing;
2517 if (metrics.iArrange & ARW_STARTTOP)
2518 pt.y = rectParent.top + y * yspacing;
2519 else
2520 pt.y = rectParent.bottom - (y + 1) * yspacing;
2522 SetWindowPos( hwndChild, 0, pt.x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
2523 pt.y + (yspacing - GetSystemMetrics(SM_CYICON)) / 2, 0, 0,
2524 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
2525 if( IsWindow(hwndChild) )
2526 WINPOS_ShowIconTitle(hwndChild , TRUE );
2528 if (++x >= (rectParent.right - rectParent.left) / xspacing)
2530 x = 0;
2531 y++;
2534 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
2536 return yspacing;
2540 /***********************************************************************
2541 * draw_moving_frame
2543 * Draw the frame used when moving or resizing window.
2545 static void draw_moving_frame( HWND parent, HDC hdc, RECT *screen_rect, BOOL thickframe )
2547 RECT rect = *screen_rect;
2549 if (parent) MapWindowPoints( 0, parent, (POINT *)&rect, 2 );
2550 if (thickframe)
2552 const int width = GetSystemMetrics(SM_CXFRAME);
2553 const int height = GetSystemMetrics(SM_CYFRAME);
2555 HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
2556 PatBlt( hdc, rect.left, rect.top,
2557 rect.right - rect.left - width, height, PATINVERT );
2558 PatBlt( hdc, rect.left, rect.top + height, width,
2559 rect.bottom - rect.top - height, PATINVERT );
2560 PatBlt( hdc, rect.left + width, rect.bottom - 1,
2561 rect.right - rect.left - width, -height, PATINVERT );
2562 PatBlt( hdc, rect.right - 1, rect.top, -width,
2563 rect.bottom - rect.top - height, PATINVERT );
2564 SelectObject( hdc, hbrush );
2566 else DrawFocusRect( hdc, &rect );
2570 /***********************************************************************
2571 * start_size_move
2573 * Initialization of a move or resize, when initiated from a menu choice.
2574 * Return hit test code for caption or sizing border.
2576 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
2578 LONG hittest = 0;
2579 POINT pt;
2580 MSG msg;
2581 RECT rectWindow;
2583 GetWindowRect( hwnd, &rectWindow );
2585 if ((wParam & 0xfff0) == SC_MOVE)
2587 /* Move pointer at the center of the caption */
2588 RECT rect = rectWindow;
2589 /* Note: to be exactly centered we should take the different types
2590 * of border into account, but it shouldn't make more than a few pixels
2591 * of difference so let's not bother with that */
2592 rect.top += GetSystemMetrics(SM_CYBORDER);
2593 if (style & WS_SYSMENU)
2594 rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
2595 if (style & WS_MINIMIZEBOX)
2596 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
2597 if (style & WS_MAXIMIZEBOX)
2598 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
2599 pt.x = (rect.right + rect.left) / 2;
2600 pt.y = rect.top + GetSystemMetrics(SM_CYSIZE)/2;
2601 hittest = HTCAPTION;
2602 *capturePoint = pt;
2604 else /* SC_SIZE */
2606 SetCursor( LoadCursorW( 0, (LPWSTR)IDC_SIZEALL ) );
2607 pt.x = pt.y = 0;
2608 while(!hittest)
2610 if (!GetMessageW( &msg, 0, 0, 0 )) return 0;
2611 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2613 switch(msg.message)
2615 case WM_MOUSEMOVE:
2616 pt.x = min( max( msg.pt.x, rectWindow.left ), rectWindow.right - 1 );
2617 pt.y = min( max( msg.pt.y, rectWindow.top ), rectWindow.bottom - 1 );
2618 hittest = SendMessageW( hwnd, WM_NCHITTEST, 0, MAKELONG( pt.x, pt.y ) );
2619 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT)) hittest = 0;
2620 break;
2622 case WM_LBUTTONUP:
2623 return 0;
2625 case WM_KEYDOWN:
2626 switch(msg.wParam)
2628 case VK_UP:
2629 hittest = HTTOP;
2630 pt.x =(rectWindow.left+rectWindow.right)/2;
2631 pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
2632 break;
2633 case VK_DOWN:
2634 hittest = HTBOTTOM;
2635 pt.x =(rectWindow.left+rectWindow.right)/2;
2636 pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
2637 break;
2638 case VK_LEFT:
2639 hittest = HTLEFT;
2640 pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
2641 pt.y =(rectWindow.top+rectWindow.bottom)/2;
2642 break;
2643 case VK_RIGHT:
2644 hittest = HTRIGHT;
2645 pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
2646 pt.y =(rectWindow.top+rectWindow.bottom)/2;
2647 break;
2648 case VK_RETURN:
2649 case VK_ESCAPE:
2650 return 0;
2652 break;
2653 default:
2654 TranslateMessage( &msg );
2655 DispatchMessageW( &msg );
2656 break;
2659 *capturePoint = pt;
2661 SetCursorPos( pt.x, pt.y );
2662 SendMessageW( hwnd, WM_SETCURSOR, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
2663 return hittest;
2667 /***********************************************************************
2668 * WINPOS_SysCommandSizeMove
2670 * Perform SC_MOVE and SC_SIZE commands.
2672 void WINPOS_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
2674 MSG msg;
2675 RECT sizingRect, mouseRect, origRect;
2676 HDC hdc;
2677 HWND parent;
2678 LONG hittest = (LONG)(wParam & 0x0f);
2679 WPARAM syscommand = wParam & 0xfff0;
2680 HCURSOR hDragCursor = 0, hOldCursor = 0;
2681 POINT minTrack, maxTrack;
2682 POINT capturePoint, pt;
2683 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
2684 BOOL thickframe = HAS_THICKFRAME( style );
2685 BOOL iconic = style & WS_MINIMIZE;
2686 BOOL moved = FALSE;
2687 DWORD dwPoint = GetMessagePos ();
2688 BOOL DragFullWindows = TRUE;
2689 HMONITOR mon = 0;
2691 if (IsZoomed(hwnd) || !IsWindowVisible(hwnd)) return;
2693 pt.x = (short)LOWORD(dwPoint);
2694 pt.y = (short)HIWORD(dwPoint);
2695 capturePoint = pt;
2696 ClipCursor( NULL );
2698 TRACE("hwnd %p command %04lx, hittest %d, pos %d,%d\n",
2699 hwnd, syscommand, hittest, pt.x, pt.y);
2701 if (syscommand == SC_MOVE)
2703 if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
2704 if (!hittest) return;
2706 else /* SC_SIZE */
2708 if ( hittest && (syscommand != SC_MOUSEMENU) )
2709 hittest += (HTLEFT - WMSZ_LEFT);
2710 else
2712 set_capture_window( hwnd, GUI_INMOVESIZE, NULL );
2713 hittest = start_size_move( hwnd, wParam, &capturePoint, style );
2714 if (!hittest)
2716 set_capture_window( 0, GUI_INMOVESIZE, NULL );
2717 return;
2722 /* Get min/max info */
2724 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
2725 WIN_GetRectangles( hwnd, COORDS_PARENT, &sizingRect, NULL );
2726 origRect = sizingRect;
2727 if (style & WS_CHILD)
2729 parent = GetParent(hwnd);
2730 GetClientRect( parent, &mouseRect );
2731 MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
2732 MapWindowPoints( parent, 0, (LPPOINT)&sizingRect, 2 );
2734 else
2736 parent = 0;
2737 mouseRect = get_virtual_screen_rect();
2738 mon = MonitorFromPoint( pt, MONITOR_DEFAULTTONEAREST );
2741 if (ON_LEFT_BORDER(hittest))
2743 mouseRect.left = max( mouseRect.left, sizingRect.right-maxTrack.x+capturePoint.x-sizingRect.left );
2744 mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x+capturePoint.x-sizingRect.left );
2746 else if (ON_RIGHT_BORDER(hittest))
2748 mouseRect.left = max( mouseRect.left, sizingRect.left+minTrack.x+capturePoint.x-sizingRect.right );
2749 mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x+capturePoint.x-sizingRect.right );
2751 if (ON_TOP_BORDER(hittest))
2753 mouseRect.top = max( mouseRect.top, sizingRect.bottom-maxTrack.y+capturePoint.y-sizingRect.top );
2754 mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y+capturePoint.y-sizingRect.top);
2756 else if (ON_BOTTOM_BORDER(hittest))
2758 mouseRect.top = max( mouseRect.top, sizingRect.top+minTrack.y+capturePoint.y-sizingRect.bottom );
2759 mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y+capturePoint.y-sizingRect.bottom );
2762 /* Retrieve a default cache DC (without using the window style) */
2763 hdc = GetDCEx( parent, 0, DCX_CACHE );
2765 if( iconic ) /* create a cursor for dragging */
2767 hDragCursor = (HCURSOR)GetClassLongPtrW( hwnd, GCLP_HICON);
2768 if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageW( hwnd, WM_QUERYDRAGICON, 0, 0L);
2769 if( !hDragCursor ) iconic = FALSE;
2772 /* we only allow disabling the full window drag for child windows */
2773 if (parent) SystemParametersInfoW( SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0 );
2775 /* repaint the window before moving it around */
2776 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
2778 SendMessageW( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
2779 set_capture_window( hwnd, GUI_INMOVESIZE, NULL );
2781 while(1)
2783 int dx = 0, dy = 0;
2785 if (!GetMessageW( &msg, 0, 0, 0 )) break;
2786 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2788 /* Exit on button-up, Return, or Esc */
2789 if ((msg.message == WM_LBUTTONUP) ||
2790 ((msg.message == WM_KEYDOWN) &&
2791 ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
2793 if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
2795 TranslateMessage( &msg );
2796 DispatchMessageW( &msg );
2797 continue; /* We are not interested in other messages */
2800 pt = msg.pt;
2802 if (msg.message == WM_KEYDOWN) switch(msg.wParam)
2804 case VK_UP: pt.y -= 8; break;
2805 case VK_DOWN: pt.y += 8; break;
2806 case VK_LEFT: pt.x -= 8; break;
2807 case VK_RIGHT: pt.x += 8; break;
2810 pt.x = max( pt.x, mouseRect.left );
2811 pt.x = min( pt.x, mouseRect.right - 1 );
2812 pt.y = max( pt.y, mouseRect.top );
2813 pt.y = min( pt.y, mouseRect.bottom - 1 );
2815 if (!parent)
2817 HMONITOR newmon;
2818 MONITORINFO info;
2820 if ((newmon = MonitorFromPoint( pt, MONITOR_DEFAULTTONULL )))
2821 mon = newmon;
2823 info.cbSize = sizeof(info);
2824 if (mon && GetMonitorInfoW( mon, &info ))
2826 pt.x = max( pt.x, info.rcWork.left );
2827 pt.x = min( pt.x, info.rcWork.right - 1 );
2828 pt.y = max( pt.y, info.rcWork.top );
2829 pt.y = min( pt.y, info.rcWork.bottom - 1 );
2833 dx = pt.x - capturePoint.x;
2834 dy = pt.y - capturePoint.y;
2836 if (dx || dy)
2838 if( !moved )
2840 moved = TRUE;
2842 if( iconic ) /* ok, no system popup tracking */
2844 hOldCursor = SetCursor(hDragCursor);
2845 ShowCursor( TRUE );
2846 WINPOS_ShowIconTitle( hwnd, FALSE );
2848 else if(!DragFullWindows)
2849 draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2852 if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
2853 else
2855 if(!iconic && !DragFullWindows) draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2856 if (hittest == HTCAPTION) OffsetRect( &sizingRect, dx, dy );
2857 if (ON_LEFT_BORDER(hittest)) sizingRect.left += dx;
2858 else if (ON_RIGHT_BORDER(hittest)) sizingRect.right += dx;
2859 if (ON_TOP_BORDER(hittest)) sizingRect.top += dy;
2860 else if (ON_BOTTOM_BORDER(hittest)) sizingRect.bottom += dy;
2861 capturePoint = pt;
2863 /* determine the hit location */
2864 if (syscommand == SC_SIZE)
2866 WPARAM wpSizingHit = 0;
2868 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
2869 wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
2870 SendMessageW( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&sizingRect );
2872 else
2873 SendMessageW( hwnd, WM_MOVING, 0, (LPARAM)&sizingRect );
2875 if (!iconic)
2877 if(!DragFullWindows)
2878 draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2879 else
2881 RECT rect = sizingRect;
2882 MapWindowPoints( 0, parent, (POINT *)&rect, 2 );
2883 SetWindowPos( hwnd, 0, rect.left, rect.top,
2884 rect.right - rect.left, rect.bottom - rect.top,
2885 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2892 if( iconic )
2894 if( moved ) /* restore cursors, show icon title later on */
2896 ShowCursor( FALSE );
2897 SetCursor( hOldCursor );
2900 else if (moved && !DragFullWindows)
2902 draw_moving_frame( parent, hdc, &sizingRect, thickframe );
2905 set_capture_window( 0, GUI_INMOVESIZE, NULL );
2906 ReleaseDC( parent, hdc );
2907 if (parent) MapWindowPoints( 0, parent, (POINT *)&sizingRect, 2 );
2909 if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
2910 moved = FALSE;
2912 SendMessageW( hwnd, WM_EXITSIZEMOVE, 0, 0 );
2913 SendMessageW( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
2915 /* window moved or resized */
2916 if (moved)
2918 /* if the moving/resizing isn't canceled call SetWindowPos
2919 * with the new position or the new size of the window
2921 if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
2923 /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
2924 if(!DragFullWindows || iconic)
2925 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
2926 sizingRect.right - sizingRect.left,
2927 sizingRect.bottom - sizingRect.top,
2928 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2930 else
2931 { /* restore previous size/position */
2932 if(DragFullWindows)
2933 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
2934 origRect.right - origRect.left,
2935 origRect.bottom - origRect.top,
2936 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2940 if (IsIconic(hwnd))
2942 /* Single click brings up the system menu when iconized */
2944 if( !moved )
2946 if(style & WS_SYSMENU )
2947 SendMessageW( hwnd, WM_SYSCOMMAND,
2948 SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
2950 else WINPOS_ShowIconTitle( hwnd, TRUE );
2954 /***********************************************************************
2955 * LogicalToPhysicalPoint (USER32.@)
2957 BOOL WINAPI LogicalToPhysicalPoint(HWND hwnd, POINT *point)
2959 static int once;
2961 if (!once++)
2962 FIXME("(%p %p) stub\n", hwnd, point);
2963 return TRUE;
2966 /***********************************************************************
2967 * PhysicalToLogicalPoint (USER32.@)
2969 BOOL WINAPI PhysicalToLogicalPoint(HWND hwnd, POINT *point)
2971 static int once;
2973 if (!once++)
2974 FIXME("(%p %p) stub\n", hwnd, point);
2975 return TRUE;