Get rid of nonclient.h and of the corresponding exported functions in
[wine.git] / windows / winpos.c
blob5373e90b8b9c2c917e7f14be79d89285629f05df
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdarg.h>
26 #include <string.h>
27 #include "winerror.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "winerror.h"
32 #include "ntstatus.h"
33 #include "wine/winuser16.h"
34 #include "wine/server.h"
35 #include "controls.h"
36 #include "user_private.h"
37 #include "win.h"
38 #include "message.h"
39 #include "winpos.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(win);
44 #define HAS_DLGFRAME(style,exStyle) \
45 (((exStyle) & WS_EX_DLGMODALFRAME) || \
46 (((style) & WS_DLGFRAME) && !((style) & WS_BORDER)))
48 #define HAS_THICKFRAME(style) \
49 (((style) & WS_THICKFRAME) && \
50 !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
52 #define EMPTYPOINT(pt) ((*(LONG*)&(pt)) == -1)
54 #define PLACE_MIN 0x0001
55 #define PLACE_MAX 0x0002
56 #define PLACE_RECT 0x0004
59 #define DWP_MAGIC ((INT)('W' | ('P' << 8) | ('O' << 16) | ('S' << 24)))
61 typedef struct
63 INT actualCount;
64 INT suggestedCount;
65 BOOL valid;
66 INT wMagic;
67 HWND hwndParent;
68 WINDOWPOS winPos[1];
69 } DWP;
71 typedef struct
73 RECT16 rectNormal;
74 POINT16 ptIconPos;
75 POINT16 ptMaxPos;
76 HWND hwndIconTitle;
77 } INTERNALPOS, *LPINTERNALPOS;
79 /* ----- internal variables ----- */
81 static LPCSTR atomInternalPos;
84 /***********************************************************************
85 * WINPOS_CreateInternalPosAtom
87 BOOL WINPOS_CreateInternalPosAtom()
89 LPCSTR str = "SysIP";
90 atomInternalPos = (LPCSTR)(DWORD)GlobalAddAtomA(str);
91 return (atomInternalPos) ? TRUE : FALSE;
94 /***********************************************************************
95 * WINPOS_CheckInternalPos
97 * Called when a window is destroyed.
99 void WINPOS_CheckInternalPos( HWND hwnd )
101 LPINTERNALPOS lpPos = (LPINTERNALPOS) GetPropA( hwnd, atomInternalPos );
103 if( lpPos )
105 if( IsWindow(lpPos->hwndIconTitle) )
106 DestroyWindow( lpPos->hwndIconTitle );
107 HeapFree( GetProcessHeap(), 0, lpPos );
111 /***********************************************************************
112 * ArrangeIconicWindows (USER32.@)
114 UINT WINAPI ArrangeIconicWindows( HWND parent )
116 RECT rectParent;
117 HWND hwndChild;
118 INT x, y, xspacing, yspacing;
120 GetClientRect( parent, &rectParent );
121 x = rectParent.left;
122 y = rectParent.bottom;
123 xspacing = GetSystemMetrics(SM_CXICONSPACING);
124 yspacing = GetSystemMetrics(SM_CYICONSPACING);
126 hwndChild = GetWindow( parent, GW_CHILD );
127 while (hwndChild)
129 if( IsIconic( hwndChild ) )
131 WINPOS_ShowIconTitle( hwndChild, FALSE );
133 SetWindowPos( hwndChild, 0, x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
134 y - yspacing - GetSystemMetrics(SM_CYICON)/2, 0, 0,
135 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
136 if( IsWindow(hwndChild) )
137 WINPOS_ShowIconTitle(hwndChild , TRUE );
139 if (x <= rectParent.right - xspacing) x += xspacing;
140 else
142 x = rectParent.left;
143 y -= yspacing;
146 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
148 return yspacing;
152 /***********************************************************************
153 * SwitchToThisWindow (USER32.@)
155 void WINAPI SwitchToThisWindow( HWND hwnd, BOOL restore )
157 ShowWindow( hwnd, restore ? SW_RESTORE : SW_SHOWMINIMIZED );
161 /***********************************************************************
162 * GetWindowRect (USER32.@)
164 BOOL WINAPI GetWindowRect( HWND hwnd, LPRECT rect )
166 BOOL ret = WIN_GetRectangles( hwnd, rect, NULL );
167 if (ret)
169 MapWindowPoints( GetAncestor( hwnd, GA_PARENT ), 0, (POINT *)rect, 2 );
170 TRACE( "hwnd %p (%ld,%ld)-(%ld,%ld)\n",
171 hwnd, rect->left, rect->top, rect->right, rect->bottom);
173 return ret;
177 /***********************************************************************
178 * GetWindowRgn (USER32.@)
180 int WINAPI GetWindowRgn ( HWND hwnd, HRGN hrgn )
182 int nRet = ERROR;
183 NTSTATUS status;
184 HRGN win_rgn = 0;
185 RGNDATA *data;
186 size_t size = 256;
190 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
192 SetLastError( ERROR_OUTOFMEMORY );
193 return ERROR;
195 SERVER_START_REQ( get_window_region )
197 req->window = hwnd;
198 wine_server_set_reply( req, data->Buffer, size );
199 if (!(status = wine_server_call( req )))
201 size_t reply_size = wine_server_reply_size( reply );
202 if (reply_size)
204 data->rdh.dwSize = sizeof(data->rdh);
205 data->rdh.iType = RDH_RECTANGLES;
206 data->rdh.nCount = reply_size / sizeof(RECT);
207 data->rdh.nRgnSize = reply_size;
208 win_rgn = ExtCreateRegion( NULL, size, data );
211 else size = reply->total_size;
213 SERVER_END_REQ;
214 HeapFree( GetProcessHeap(), 0, data );
215 } while (status == STATUS_BUFFER_OVERFLOW);
217 if (status) SetLastError( RtlNtStatusToDosError(status) );
218 else if (win_rgn)
220 nRet = CombineRgn( hrgn, win_rgn, 0, RGN_COPY );
221 DeleteObject( win_rgn );
223 return nRet;
227 /***********************************************************************
228 * SetWindowRgn (USER32.@)
230 int WINAPI SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL bRedraw )
232 static const RECT empty_rect;
233 BOOL ret;
235 if (hrgn)
237 RGNDATA *data;
238 DWORD size;
240 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
241 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
242 if (!GetRegionData( hrgn, size, data ))
244 HeapFree( GetProcessHeap(), 0, data );
245 return FALSE;
247 SERVER_START_REQ( set_window_region )
249 req->window = hwnd;
250 if (data->rdh.nCount)
251 wine_server_add_data( req, data->Buffer, data->rdh.nCount * sizeof(RECT) );
252 else
253 wine_server_add_data( req, &empty_rect, sizeof(empty_rect) );
254 ret = !wine_server_call_err( req );
256 SERVER_END_REQ;
258 else /* clear existing region */
260 SERVER_START_REQ( set_window_region )
262 req->window = hwnd;
263 ret = !wine_server_call_err( req );
265 SERVER_END_REQ;
268 if (ret && USER_Driver.pSetWindowRgn)
269 ret = USER_Driver.pSetWindowRgn( hwnd, hrgn, bRedraw );
271 if (ret && bRedraw) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_INVALIDATE | RDW_ERASE );
272 return ret;
276 /***********************************************************************
277 * GetClientRect (USER32.@)
279 BOOL WINAPI GetClientRect( HWND hwnd, LPRECT rect )
281 BOOL ret;
283 if ((ret = WIN_GetRectangles( hwnd, NULL, rect )))
285 rect->right -= rect->left;
286 rect->bottom -= rect->top;
287 rect->left = rect->top = 0;
289 return ret;
293 /*******************************************************************
294 * ClientToScreen (USER32.@)
296 BOOL WINAPI ClientToScreen( HWND hwnd, LPPOINT lppnt )
298 MapWindowPoints( hwnd, 0, lppnt, 1 );
299 return TRUE;
303 /*******************************************************************
304 * ScreenToClient (USER32.@)
306 BOOL WINAPI ScreenToClient( HWND hwnd, LPPOINT lppnt )
308 MapWindowPoints( 0, hwnd, lppnt, 1 );
309 return TRUE;
313 /***********************************************************************
314 * list_children_from_point
316 * Get the list of children that can contain point from the server.
317 * Point is in screen coordinates.
318 * Returned list must be freed by caller.
320 static HWND *list_children_from_point( HWND hwnd, POINT pt )
322 HWND *list;
323 int size = 32;
325 for (;;)
327 int count = 0;
329 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
331 SERVER_START_REQ( get_window_children_from_point )
333 req->parent = hwnd;
334 req->x = pt.x;
335 req->y = pt.y;
336 wine_server_set_reply( req, list, (size-1) * sizeof(HWND) );
337 if (!wine_server_call( req )) count = reply->count;
339 SERVER_END_REQ;
340 if (count && count < size)
342 list[count] = 0;
343 return list;
345 HeapFree( GetProcessHeap(), 0, list );
346 if (!count) break;
347 size = count + 1; /* restart with a large enough buffer */
349 return NULL;
353 /***********************************************************************
354 * WINPOS_WindowFromPoint
356 * Find the window and hittest for a given point.
358 HWND WINPOS_WindowFromPoint( HWND hwndScope, POINT pt, INT *hittest )
360 int i, res;
361 HWND ret, *list;
363 if (!hwndScope) hwndScope = GetDesktopWindow();
365 *hittest = HTNOWHERE;
367 if (!(list = list_children_from_point( hwndScope, pt ))) return 0;
369 /* now determine the hittest */
371 for (i = 0; list[i]; i++)
373 LONG style = GetWindowLongW( list[i], GWL_STYLE );
375 /* If window is minimized or disabled, return at once */
376 if (style & WS_MINIMIZE)
378 *hittest = HTCAPTION;
379 break;
381 if (style & WS_DISABLED)
383 *hittest = HTERROR;
384 break;
386 /* Send WM_NCCHITTEST (if same thread) */
387 if (!WIN_IsCurrentThread( list[i] ))
389 *hittest = HTCLIENT;
390 break;
392 res = SendMessageA( list[i], WM_NCHITTEST, 0, MAKELONG(pt.x,pt.y) );
393 if (res != HTTRANSPARENT)
395 *hittest = res; /* Found the window */
396 break;
398 /* continue search with next window in z-order */
400 ret = list[i];
401 HeapFree( GetProcessHeap(), 0, list );
402 TRACE( "scope %p (%ld,%ld) returning %p\n", hwndScope, pt.x, pt.y, ret );
403 return ret;
407 /*******************************************************************
408 * WindowFromPoint (USER32.@)
410 HWND WINAPI WindowFromPoint( POINT pt )
412 INT hittest;
413 return WINPOS_WindowFromPoint( 0, pt, &hittest );
417 /*******************************************************************
418 * ChildWindowFromPoint (USER32.@)
420 HWND WINAPI ChildWindowFromPoint( HWND hwndParent, POINT pt )
422 return ChildWindowFromPointEx( hwndParent, pt, CWP_ALL );
425 /*******************************************************************
426 * ChildWindowFromPointEx (USER32.@)
428 HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
430 /* pt is in the client coordinates */
431 HWND *list;
432 int i;
433 RECT rect;
434 HWND retvalue;
436 GetClientRect( hwndParent, &rect );
437 if (!PtInRect( &rect, pt )) return 0;
438 if (!(list = WIN_ListChildren( hwndParent ))) return 0;
440 for (i = 0; list[i]; i++)
442 if (!WIN_GetRectangles( list[i], &rect, NULL )) continue;
443 if (!PtInRect( &rect, pt )) continue;
444 if (uFlags & (CWP_SKIPINVISIBLE|CWP_SKIPDISABLED))
446 LONG style = GetWindowLongW( list[i], GWL_STYLE );
447 if ((uFlags & CWP_SKIPINVISIBLE) && !(style & WS_VISIBLE)) continue;
448 if ((uFlags & CWP_SKIPDISABLED) && (style & WS_DISABLED)) continue;
450 if (uFlags & CWP_SKIPTRANSPARENT)
452 if (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TRANSPARENT) continue;
454 break;
456 retvalue = list[i];
457 HeapFree( GetProcessHeap(), 0, list );
458 if (!retvalue) retvalue = hwndParent;
459 return retvalue;
463 /*******************************************************************
464 * WINPOS_GetWinOffset
466 * Calculate the offset between the origin of the two windows. Used
467 * to implement MapWindowPoints.
469 static void WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo, POINT *offset )
471 WND * wndPtr;
473 offset->x = offset->y = 0;
475 /* Translate source window origin to screen coords */
476 if (hwndFrom)
478 HWND hwnd = hwndFrom;
480 while (hwnd && hwnd != GetDesktopWindow())
482 if (hwnd == hwndTo) return;
483 if (!(wndPtr = WIN_GetPtr( hwnd )))
485 ERR( "bad hwndFrom = %p\n", hwnd );
486 return;
488 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
489 offset->x += wndPtr->rectClient.left;
490 offset->y += wndPtr->rectClient.top;
491 hwnd = wndPtr->parent;
492 WIN_ReleasePtr( wndPtr );
496 /* Translate origin to destination window coords */
497 if (hwndTo)
499 HWND hwnd = hwndTo;
501 while (hwnd && hwnd != GetDesktopWindow())
503 if (!(wndPtr = WIN_GetPtr( hwnd )))
505 ERR( "bad hwndTo = %p\n", hwnd );
506 return;
508 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
509 offset->x -= wndPtr->rectClient.left;
510 offset->y -= wndPtr->rectClient.top;
511 hwnd = wndPtr->parent;
512 WIN_ReleasePtr( wndPtr );
515 return;
517 other_process: /* one of the parents may belong to another process, do it the hard way */
518 offset->x = offset->y = 0;
519 SERVER_START_REQ( get_windows_offset )
521 req->from = hwndFrom;
522 req->to = hwndTo;
523 if (!wine_server_call( req ))
525 offset->x = reply->x;
526 offset->y = reply->y;
529 SERVER_END_REQ;
533 /*******************************************************************
534 * MapWindowPoints (USER.258)
536 void WINAPI MapWindowPoints16( HWND16 hwndFrom, HWND16 hwndTo,
537 LPPOINT16 lppt, UINT16 count )
539 POINT offset;
541 WINPOS_GetWinOffset( WIN_Handle32(hwndFrom), WIN_Handle32(hwndTo), &offset );
542 while (count--)
544 lppt->x += offset.x;
545 lppt->y += offset.y;
546 lppt++;
551 /*******************************************************************
552 * MapWindowPoints (USER32.@)
554 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
556 POINT offset;
558 WINPOS_GetWinOffset( hwndFrom, hwndTo, &offset );
559 while (count--)
561 lppt->x += offset.x;
562 lppt->y += offset.y;
563 lppt++;
565 return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
569 /***********************************************************************
570 * IsIconic (USER32.@)
572 BOOL WINAPI IsIconic(HWND hWnd)
574 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
578 /***********************************************************************
579 * IsZoomed (USER32.@)
581 BOOL WINAPI IsZoomed(HWND hWnd)
583 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
587 /*******************************************************************
588 * AllowSetForegroundWindow (USER32.@)
590 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
592 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
593 * implemented, then fix this function. */
594 return TRUE;
598 /*******************************************************************
599 * LockSetForegroundWindow (USER32.@)
601 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
603 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
604 * implemented, then fix this function. */
605 return TRUE;
609 /***********************************************************************
610 * BringWindowToTop (USER32.@)
612 BOOL WINAPI BringWindowToTop( HWND hwnd )
614 return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
618 /***********************************************************************
619 * MoveWindow (USER32.@)
621 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
622 BOOL repaint )
624 int flags = SWP_NOZORDER | SWP_NOACTIVATE;
625 if (!repaint) flags |= SWP_NOREDRAW;
626 TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
627 return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
630 /***********************************************************************
631 * WINPOS_InitInternalPos
633 static LPINTERNALPOS WINPOS_InitInternalPos( WND* wnd, POINT pt, const RECT *restoreRect )
635 LPINTERNALPOS lpPos = (LPINTERNALPOS) GetPropA( wnd->hwndSelf,
636 atomInternalPos );
637 if( !lpPos )
639 /* this happens when the window is minimized/maximized
640 * for the first time (rectWindow is not adjusted yet) */
642 lpPos = HeapAlloc( GetProcessHeap(), 0, sizeof(INTERNALPOS) );
643 if( !lpPos ) return NULL;
645 SetPropA( wnd->hwndSelf, atomInternalPos, (HANDLE)lpPos );
646 lpPos->hwndIconTitle = 0; /* defer until needs to be shown */
647 lpPos->rectNormal.left = wnd->rectWindow.left;
648 lpPos->rectNormal.top = wnd->rectWindow.top;
649 lpPos->rectNormal.right = wnd->rectWindow.right;
650 lpPos->rectNormal.bottom = wnd->rectWindow.bottom;
651 lpPos->ptIconPos.x = lpPos->ptIconPos.y = -1;
652 lpPos->ptMaxPos.x = lpPos->ptMaxPos.y = -1;
655 if( wnd->dwStyle & WS_MINIMIZE )
657 lpPos->ptIconPos.x = pt.x;
658 lpPos->ptIconPos.y = pt.y;
660 else if( wnd->dwStyle & WS_MAXIMIZE )
662 lpPos->ptMaxPos.x = pt.x;
663 lpPos->ptMaxPos.y = pt.y;
665 else if( restoreRect )
667 lpPos->rectNormal.left = restoreRect->left;
668 lpPos->rectNormal.top = restoreRect->top;
669 lpPos->rectNormal.right = restoreRect->right;
670 lpPos->rectNormal.bottom = restoreRect->bottom;
672 return lpPos;
675 /***********************************************************************
676 * WINPOS_RedrawIconTitle
678 BOOL WINPOS_RedrawIconTitle( HWND hWnd )
680 LPINTERNALPOS lpPos = (LPINTERNALPOS)GetPropA( hWnd, atomInternalPos );
681 if( lpPos )
683 if( lpPos->hwndIconTitle )
685 SendMessageA( lpPos->hwndIconTitle, WM_SHOWWINDOW, TRUE, 0);
686 InvalidateRect( lpPos->hwndIconTitle, NULL, TRUE );
687 return TRUE;
690 return FALSE;
693 /***********************************************************************
694 * WINPOS_ShowIconTitle
696 BOOL WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
698 LPINTERNALPOS lpPos = (LPINTERNALPOS)GetPropA( hwnd, atomInternalPos );
700 if( lpPos && !(GetWindowLongA( hwnd, GWL_EXSTYLE) & WS_EX_MANAGED))
702 HWND title = lpPos->hwndIconTitle;
704 TRACE("%p %i\n", hwnd, (bShow != 0) );
706 if( !title )
707 lpPos->hwndIconTitle = title = ICONTITLE_Create( hwnd );
708 if( bShow )
710 if (!IsWindowVisible(title))
712 SendMessageA( title, WM_SHOWWINDOW, TRUE, 0 );
713 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
714 SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
717 else ShowWindow( title, SW_HIDE );
719 return FALSE;
722 /*******************************************************************
723 * WINPOS_GetMinMaxInfo
725 * Get the minimized and maximized information for a window.
727 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
728 POINT *minTrack, POINT *maxTrack )
730 LPINTERNALPOS lpPos;
731 MINMAXINFO MinMax;
732 INT xinc, yinc;
733 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
734 LONG exstyle = GetWindowLongA( hwnd, GWL_EXSTYLE );
735 RECT rc;
737 /* Compute default values */
739 GetWindowRect(hwnd, &rc);
740 MinMax.ptReserved.x = rc.left;
741 MinMax.ptReserved.y = rc.top;
743 if (style & WS_CHILD)
745 if ((style & WS_CAPTION) == WS_CAPTION)
746 style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
748 GetClientRect(GetParent(hwnd), &rc);
749 AdjustWindowRectEx(&rc, style, 0, exstyle);
751 /* avoid calculating this twice */
752 style &= ~(WS_DLGFRAME | WS_BORDER | WS_THICKFRAME);
754 MinMax.ptMaxSize.x = rc.right - rc.left;
755 MinMax.ptMaxSize.y = rc.bottom - rc.top;
757 else
759 MinMax.ptMaxSize.x = GetSystemMetrics(SM_CXSCREEN);
760 MinMax.ptMaxSize.y = GetSystemMetrics(SM_CYSCREEN);
762 MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
763 MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
764 MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXSCREEN);
765 MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYSCREEN);
767 if (HAS_DLGFRAME( style, exstyle ))
769 xinc = GetSystemMetrics(SM_CXDLGFRAME);
770 yinc = GetSystemMetrics(SM_CYDLGFRAME);
772 else
774 xinc = yinc = 0;
775 if (HAS_THICKFRAME(style))
777 xinc += GetSystemMetrics(SM_CXFRAME);
778 yinc += GetSystemMetrics(SM_CYFRAME);
780 if (style & WS_BORDER)
782 xinc += GetSystemMetrics(SM_CXBORDER);
783 yinc += GetSystemMetrics(SM_CYBORDER);
786 MinMax.ptMaxSize.x += 2 * xinc;
787 MinMax.ptMaxSize.y += 2 * yinc;
789 lpPos = (LPINTERNALPOS)GetPropA( hwnd, atomInternalPos );
790 if( lpPos && !EMPTYPOINT(lpPos->ptMaxPos) )
792 MinMax.ptMaxPosition.x = lpPos->ptMaxPos.x;
793 MinMax.ptMaxPosition.y = lpPos->ptMaxPos.y;
795 else
797 MinMax.ptMaxPosition.x = -xinc;
798 MinMax.ptMaxPosition.y = -yinc;
801 SendMessageA( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
803 /* Some sanity checks */
805 TRACE("%ld %ld / %ld %ld / %ld %ld / %ld %ld\n",
806 MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
807 MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
808 MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
809 MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
810 MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
811 MinMax.ptMinTrackSize.x );
812 MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
813 MinMax.ptMinTrackSize.y );
815 if (maxSize) *maxSize = MinMax.ptMaxSize;
816 if (maxPos) *maxPos = MinMax.ptMaxPosition;
817 if (minTrack) *minTrack = MinMax.ptMinTrackSize;
818 if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
821 /***********************************************************************
822 * ShowWindowAsync (USER32.@)
824 * doesn't wait; returns immediately.
825 * used by threads to toggle windows in other (possibly hanging) threads
827 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
829 HWND full_handle;
831 if (is_broadcast(hwnd))
833 SetLastError( ERROR_INVALID_PARAMETER );
834 return FALSE;
837 if ((full_handle = WIN_IsCurrentThread( hwnd )))
839 if (USER_Driver.pShowWindow)
840 return USER_Driver.pShowWindow( full_handle, cmd );
841 return FALSE;
843 return SendNotifyMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
847 /***********************************************************************
848 * ShowWindow (USER32.@)
850 BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
852 HWND full_handle;
854 if (is_broadcast(hwnd))
856 SetLastError( ERROR_INVALID_PARAMETER );
857 return FALSE;
859 if ((full_handle = WIN_IsCurrentThread( hwnd )))
861 if (USER_Driver.pShowWindow)
862 return USER_Driver.pShowWindow( full_handle, cmd );
863 return FALSE;
865 return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
869 /***********************************************************************
870 * GetInternalWindowPos (USER32.@)
872 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
873 LPPOINT ptIcon )
875 WINDOWPLACEMENT wndpl;
876 if (GetWindowPlacement( hwnd, &wndpl ))
878 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
879 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
880 return wndpl.showCmd;
882 return 0;
886 /***********************************************************************
887 * GetWindowPlacement (USER32.@)
889 * Win95:
890 * Fails if wndpl->length of Win95 (!) apps is invalid.
892 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
894 WND *pWnd = WIN_FindWndPtr( hwnd );
895 LPINTERNALPOS lpPos;
897 if(!pWnd ) return FALSE;
899 lpPos = WINPOS_InitInternalPos( pWnd, *(LPPOINT)&pWnd->rectWindow.left, &pWnd->rectWindow );
900 wndpl->length = sizeof(*wndpl);
901 if( pWnd->dwStyle & WS_MINIMIZE )
902 wndpl->showCmd = SW_SHOWMINIMIZED;
903 else
904 wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
905 if( pWnd->flags & WIN_RESTORE_MAX )
906 wndpl->flags = WPF_RESTORETOMAXIMIZED;
907 else
908 wndpl->flags = 0;
909 wndpl->ptMinPosition.x = lpPos->ptIconPos.x;
910 wndpl->ptMinPosition.y = lpPos->ptIconPos.y;
911 wndpl->ptMaxPosition.x = lpPos->ptMaxPos.x;
912 wndpl->ptMaxPosition.y = lpPos->ptMaxPos.y;
913 wndpl->rcNormalPosition.left = lpPos->rectNormal.left;
914 wndpl->rcNormalPosition.top = lpPos->rectNormal.top;
915 wndpl->rcNormalPosition.right = lpPos->rectNormal.right;
916 wndpl->rcNormalPosition.bottom = lpPos->rectNormal.bottom;
917 WIN_ReleaseWndPtr(pWnd);
918 return TRUE;
922 /***********************************************************************
923 * WINPOS_SetPlacement
925 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
927 WND *pWnd = WIN_FindWndPtr( hwnd );
928 if( pWnd )
930 LPINTERNALPOS lpPos = (LPINTERNALPOS)WINPOS_InitInternalPos( pWnd,
931 *(LPPOINT)&pWnd->rectWindow.left, &pWnd->rectWindow );
933 if( flags & PLACE_MIN )
935 lpPos->ptIconPos.x = wndpl->ptMinPosition.x;
936 lpPos->ptIconPos.y = wndpl->ptMinPosition.y;
938 if( flags & PLACE_MAX )
940 lpPos->ptMaxPos.x = wndpl->ptMaxPosition.x;
941 lpPos->ptMaxPos.y = wndpl->ptMaxPosition.y;
943 if( flags & PLACE_RECT)
945 lpPos->rectNormal.left = wndpl->rcNormalPosition.left;
946 lpPos->rectNormal.top = wndpl->rcNormalPosition.top;
947 lpPos->rectNormal.right = wndpl->rcNormalPosition.right;
948 lpPos->rectNormal.bottom = wndpl->rcNormalPosition.bottom;
950 if( pWnd->dwStyle & WS_MINIMIZE )
952 WINPOS_ShowIconTitle( pWnd->hwndSelf, FALSE );
953 if( wndpl->flags & WPF_SETMINPOSITION && !EMPTYPOINT(lpPos->ptIconPos))
954 SetWindowPos( hwnd, 0, lpPos->ptIconPos.x, lpPos->ptIconPos.y,
955 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
957 else if( pWnd->dwStyle & WS_MAXIMIZE )
959 if( !EMPTYPOINT(lpPos->ptMaxPos) )
960 SetWindowPos( hwnd, 0, lpPos->ptMaxPos.x, lpPos->ptMaxPos.y,
961 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
963 else if( flags & PLACE_RECT )
964 SetWindowPos( hwnd, 0, lpPos->rectNormal.left, lpPos->rectNormal.top,
965 lpPos->rectNormal.right - lpPos->rectNormal.left,
966 lpPos->rectNormal.bottom - lpPos->rectNormal.top,
967 SWP_NOZORDER | SWP_NOACTIVATE );
969 ShowWindow( hwnd, wndpl->showCmd );
970 if( IsWindow(hwnd) && pWnd->dwStyle & WS_MINIMIZE )
972 if( pWnd->dwStyle & WS_VISIBLE ) WINPOS_ShowIconTitle( pWnd->hwndSelf, TRUE );
974 /* SDK: ...valid only the next time... */
975 if( wndpl->flags & WPF_RESTORETOMAXIMIZED ) pWnd->flags |= WIN_RESTORE_MAX;
977 WIN_ReleaseWndPtr(pWnd);
978 return TRUE;
980 return FALSE;
984 /***********************************************************************
985 * SetWindowPlacement (USER32.@)
987 * Win95:
988 * Fails if wndpl->length of Win95 (!) apps is invalid.
990 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
992 if (!wpl) return FALSE;
993 return WINPOS_SetPlacement( hwnd, wpl, PLACE_MIN | PLACE_MAX | PLACE_RECT );
997 /***********************************************************************
998 * AnimateWindow (USER32.@)
999 * Shows/Hides a window with an animation
1000 * NO ANIMATION YET
1002 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
1004 FIXME("partial stub\n");
1006 /* If trying to show/hide and it's already *
1007 * shown/hidden or invalid window, fail with *
1008 * invalid parameter */
1009 if(!IsWindow(hwnd) ||
1010 (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
1011 (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
1013 SetLastError(ERROR_INVALID_PARAMETER);
1014 return FALSE;
1017 ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1019 return TRUE;
1022 /***********************************************************************
1023 * SetInternalWindowPos (USER32.@)
1025 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1026 LPRECT rect, LPPOINT pt )
1028 if( IsWindow(hwnd) )
1030 WINDOWPLACEMENT wndpl;
1031 UINT flags;
1033 wndpl.length = sizeof(wndpl);
1034 wndpl.showCmd = showCmd;
1035 wndpl.flags = flags = 0;
1037 if( pt )
1039 flags |= PLACE_MIN;
1040 wndpl.flags |= WPF_SETMINPOSITION;
1041 wndpl.ptMinPosition = *pt;
1043 if( rect )
1045 flags |= PLACE_RECT;
1046 wndpl.rcNormalPosition = *rect;
1048 WINPOS_SetPlacement( hwnd, &wndpl, flags );
1053 /*******************************************************************
1054 * can_activate_window
1056 * Check if we can activate the specified window.
1058 static BOOL can_activate_window( HWND hwnd )
1060 LONG style;
1062 if (!hwnd) return FALSE;
1063 style = GetWindowLongW( hwnd, GWL_STYLE );
1064 if (!(style & WS_VISIBLE)) return FALSE;
1065 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1066 return !(style & WS_DISABLED);
1070 /*******************************************************************
1071 * WINPOS_ActivateOtherWindow
1073 * Activates window other than pWnd.
1075 void WINPOS_ActivateOtherWindow(HWND hwnd)
1077 HWND hwndTo, fg;
1079 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1081 hwndTo = GetAncestor( hwndTo, GA_ROOT );
1082 if (can_activate_window( hwndTo )) goto done;
1085 hwndTo = hwnd;
1086 for (;;)
1088 if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1089 if (can_activate_window( hwndTo )) break;
1092 done:
1093 fg = GetForegroundWindow();
1094 TRACE("win = %p fg = %p\n", hwndTo, fg);
1095 if (!fg || (hwnd == fg))
1097 if (SetForegroundWindow( hwndTo )) return;
1099 if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1103 /***********************************************************************
1104 * WINPOS_HandleWindowPosChanging16
1106 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1108 LONG WINPOS_HandleWindowPosChanging16( HWND hwnd, WINDOWPOS16 *winpos )
1110 POINT minTrack, maxTrack;
1111 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1113 if (winpos->flags & SWP_NOSIZE) return 0;
1114 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1116 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1117 if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1118 if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1119 if (!(style & WS_MINIMIZE))
1121 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1122 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1125 return 0;
1129 /***********************************************************************
1130 * WINPOS_HandleWindowPosChanging
1132 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1134 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1136 POINT minTrack, maxTrack;
1137 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1139 if (winpos->flags & SWP_NOSIZE) return 0;
1140 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1142 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1143 if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1144 if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1145 if (!(style & WS_MINIMIZE))
1147 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1148 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1151 return 0;
1155 /***********************************************************************
1156 * dump_winpos_flags
1158 static void dump_winpos_flags(UINT flags)
1160 TRACE("flags:");
1161 if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1162 if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1163 if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1164 if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1165 if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1166 if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1167 if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1168 if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1169 if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1170 if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1171 if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1172 if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1173 if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1175 #define DUMPED_FLAGS \
1176 (SWP_NOSIZE | \
1177 SWP_NOMOVE | \
1178 SWP_NOZORDER | \
1179 SWP_NOREDRAW | \
1180 SWP_NOACTIVATE | \
1181 SWP_FRAMECHANGED | \
1182 SWP_SHOWWINDOW | \
1183 SWP_HIDEWINDOW | \
1184 SWP_NOCOPYBITS | \
1185 SWP_NOOWNERZORDER | \
1186 SWP_NOSENDCHANGING | \
1187 SWP_DEFERERASE | \
1188 SWP_ASYNCWINDOWPOS)
1190 if(flags & ~DUMPED_FLAGS) TRACE(" %08x", flags & ~DUMPED_FLAGS);
1191 TRACE("\n");
1192 #undef DUMPED_FLAGS
1195 /***********************************************************************
1196 * SetWindowPos (USER32.@)
1198 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
1199 INT x, INT y, INT cx, INT cy, UINT flags )
1201 WINDOWPOS winpos;
1203 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1204 hwnd, hwndInsertAfter, x, y, cx, cy, flags);
1205 if(TRACE_ON(win)) dump_winpos_flags(flags);
1207 if (is_broadcast(hwnd))
1209 SetLastError( ERROR_INVALID_PARAMETER );
1210 return FALSE;
1213 winpos.hwnd = WIN_GetFullHandle(hwnd);
1214 winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
1215 winpos.x = x;
1216 winpos.y = y;
1217 winpos.cx = cx;
1218 winpos.cy = cy;
1219 winpos.flags = flags;
1220 if (WIN_IsCurrentThread( hwnd ))
1222 if (USER_Driver.pSetWindowPos)
1223 return USER_Driver.pSetWindowPos( &winpos );
1224 return FALSE;
1226 return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
1230 /***********************************************************************
1231 * BeginDeferWindowPos (USER32.@)
1233 HDWP WINAPI BeginDeferWindowPos( INT count )
1235 HDWP handle;
1236 DWP *pDWP;
1238 TRACE("%d\n", count);
1240 if (count < 0)
1242 SetLastError(ERROR_INVALID_PARAMETER);
1243 return 0;
1245 /* Windows allows zero count, in which case it allocates context for 8 moves */
1246 if (count == 0) count = 8;
1248 handle = USER_HEAP_ALLOC( sizeof(DWP) + (count-1)*sizeof(WINDOWPOS) );
1249 if (!handle) return 0;
1250 pDWP = (DWP *) USER_HEAP_LIN_ADDR( handle );
1251 pDWP->actualCount = 0;
1252 pDWP->suggestedCount = count;
1253 pDWP->valid = TRUE;
1254 pDWP->wMagic = DWP_MAGIC;
1255 pDWP->hwndParent = 0;
1257 TRACE("returning hdwp %p\n", handle);
1258 return handle;
1262 /***********************************************************************
1263 * DeferWindowPos (USER32.@)
1265 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
1266 INT x, INT y, INT cx, INT cy,
1267 UINT flags )
1269 DWP *pDWP;
1270 int i;
1271 HDWP newhdwp = hdwp,retvalue;
1273 TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1274 hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
1276 hwnd = WIN_GetFullHandle( hwnd );
1277 if (hwnd == GetDesktopWindow()) return 0;
1279 if (!(pDWP = USER_HEAP_LIN_ADDR( hdwp ))) return 0;
1281 USER_Lock();
1283 for (i = 0; i < pDWP->actualCount; i++)
1285 if (pDWP->winPos[i].hwnd == hwnd)
1287 /* Merge with the other changes */
1288 if (!(flags & SWP_NOZORDER))
1290 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
1292 if (!(flags & SWP_NOMOVE))
1294 pDWP->winPos[i].x = x;
1295 pDWP->winPos[i].y = y;
1297 if (!(flags & SWP_NOSIZE))
1299 pDWP->winPos[i].cx = cx;
1300 pDWP->winPos[i].cy = cy;
1302 pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
1303 SWP_NOZORDER | SWP_NOREDRAW |
1304 SWP_NOACTIVATE | SWP_NOCOPYBITS|
1305 SWP_NOOWNERZORDER);
1306 pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
1307 SWP_FRAMECHANGED);
1308 retvalue = hdwp;
1309 goto END;
1312 if (pDWP->actualCount >= pDWP->suggestedCount)
1314 newhdwp = USER_HEAP_REALLOC( hdwp,
1315 sizeof(DWP) + pDWP->suggestedCount*sizeof(WINDOWPOS) );
1316 if (!newhdwp)
1318 retvalue = 0;
1319 goto END;
1321 pDWP = (DWP *) USER_HEAP_LIN_ADDR( newhdwp );
1322 pDWP->suggestedCount++;
1324 pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
1325 pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
1326 pDWP->winPos[pDWP->actualCount].x = x;
1327 pDWP->winPos[pDWP->actualCount].y = y;
1328 pDWP->winPos[pDWP->actualCount].cx = cx;
1329 pDWP->winPos[pDWP->actualCount].cy = cy;
1330 pDWP->winPos[pDWP->actualCount].flags = flags;
1331 pDWP->actualCount++;
1332 retvalue = newhdwp;
1333 END:
1334 USER_Unlock();
1335 return retvalue;
1339 /***********************************************************************
1340 * EndDeferWindowPos (USER32.@)
1342 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
1344 DWP *pDWP;
1345 WINDOWPOS *winpos;
1346 BOOL res = TRUE;
1347 int i;
1349 TRACE("%p\n", hdwp);
1351 pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
1352 if (!pDWP) return FALSE;
1353 for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
1355 if (!USER_Driver.pSetWindowPos || !(res = USER_Driver.pSetWindowPos( winpos ))) break;
1357 USER_HEAP_FREE( hdwp );
1358 return res;
1362 /***********************************************************************
1363 * TileChildWindows (USER.199)
1365 void WINAPI TileChildWindows16( HWND16 parent, WORD action )
1367 FIXME("(%04x, %d): stub\n", parent, action);
1370 /***********************************************************************
1371 * CascadeChildWindows (USER.198)
1373 void WINAPI CascadeChildWindows16( HWND16 parent, WORD action )
1375 FIXME("(%04x, %d): stub\n", parent, action);