Added tracing to all functions.
[wine/multimedia.git] / windows / winpos.c
blobd64a269bd65d0ce87dd9479865ccbc86740bab19
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 "wine/winuser16.h"
33 #include "wine/server.h"
34 #include "controls.h"
35 #include "user.h"
36 #include "win.h"
37 #include "message.h"
38 #include "winpos.h"
39 #include "nonclient.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 /* ----- internal variables ----- */
73 static LPCSTR atomInternalPos;
76 /***********************************************************************
77 * WINPOS_CreateInternalPosAtom
79 BOOL WINPOS_CreateInternalPosAtom()
81 LPCSTR str = "SysIP";
82 atomInternalPos = (LPCSTR)(DWORD)GlobalAddAtomA(str);
83 return (atomInternalPos) ? TRUE : FALSE;
86 /***********************************************************************
87 * WINPOS_CheckInternalPos
89 * Called when a window is destroyed.
91 void WINPOS_CheckInternalPos( HWND hwnd )
93 LPINTERNALPOS lpPos = (LPINTERNALPOS) GetPropA( hwnd, atomInternalPos );
95 if( lpPos )
97 if( IsWindow(lpPos->hwndIconTitle) )
98 DestroyWindow( lpPos->hwndIconTitle );
99 HeapFree( GetProcessHeap(), 0, lpPos );
103 /***********************************************************************
104 * ArrangeIconicWindows (USER32.@)
106 UINT WINAPI ArrangeIconicWindows( HWND parent )
108 RECT rectParent;
109 HWND hwndChild;
110 INT x, y, xspacing, yspacing;
112 GetClientRect( parent, &rectParent );
113 x = rectParent.left;
114 y = rectParent.bottom;
115 xspacing = GetSystemMetrics(SM_CXICONSPACING);
116 yspacing = GetSystemMetrics(SM_CYICONSPACING);
118 hwndChild = GetWindow( parent, GW_CHILD );
119 while (hwndChild)
121 if( IsIconic( hwndChild ) )
123 WINPOS_ShowIconTitle( hwndChild, FALSE );
125 SetWindowPos( hwndChild, 0, x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
126 y - yspacing - GetSystemMetrics(SM_CYICON)/2, 0, 0,
127 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
128 if( IsWindow(hwndChild) )
129 WINPOS_ShowIconTitle(hwndChild , TRUE );
131 if (x <= rectParent.right - xspacing) x += xspacing;
132 else
134 x = rectParent.left;
135 y -= yspacing;
138 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
140 return yspacing;
144 /***********************************************************************
145 * SwitchToThisWindow (USER32.@)
147 void WINAPI SwitchToThisWindow( HWND hwnd, BOOL restore )
149 ShowWindow( hwnd, restore ? SW_RESTORE : SW_SHOWMINIMIZED );
153 /***********************************************************************
154 * GetWindowRect (USER32.@)
156 BOOL WINAPI GetWindowRect( HWND hwnd, LPRECT rect )
158 BOOL ret = WIN_GetRectangles( hwnd, rect, NULL );
159 if (ret)
161 MapWindowPoints( GetAncestor( hwnd, GA_PARENT ), 0, (POINT *)rect, 2 );
162 TRACE( "hwnd %p (%ld,%ld)-(%ld,%ld)\n",
163 hwnd, rect->left, rect->top, rect->right, rect->bottom);
165 return ret;
169 /***********************************************************************
170 * GetWindowRgn (USER32.@)
172 int WINAPI GetWindowRgn ( HWND hwnd, HRGN hrgn )
174 int nRet = ERROR;
175 WND *wndPtr = WIN_GetPtr( hwnd );
177 if (wndPtr == WND_OTHER_PROCESS)
179 if (IsWindow( hwnd ))
180 FIXME( "not supported on other process window %p\n", hwnd );
181 wndPtr = NULL;
183 if (!wndPtr)
185 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
186 return ERROR;
188 if (wndPtr->hrgnWnd) nRet = CombineRgn( hrgn, wndPtr->hrgnWnd, 0, RGN_COPY );
189 WIN_ReleasePtr( wndPtr );
190 return nRet;
194 /***********************************************************************
195 * SetWindowRgn (USER32.@)
197 int WINAPI SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL bRedraw )
199 RECT rect;
200 WND *wndPtr;
202 if (hrgn) /* verify that region really exists */
204 if (GetRgnBox( hrgn, &rect ) == ERROR) return FALSE;
207 if (USER_Driver.pSetWindowRgn)
208 return USER_Driver.pSetWindowRgn( hwnd, hrgn, bRedraw );
210 if ((wndPtr = WIN_GetPtr( hwnd )) == WND_OTHER_PROCESS)
212 if (IsWindow( hwnd ))
213 FIXME( "not supported on other process window %p\n", hwnd );
214 wndPtr = NULL;
216 if (!wndPtr)
218 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
219 return FALSE;
222 if (wndPtr->hrgnWnd == hrgn)
224 WIN_ReleasePtr( wndPtr );
225 return TRUE;
228 if (wndPtr->hrgnWnd)
230 /* delete previous region */
231 DeleteObject(wndPtr->hrgnWnd);
232 wndPtr->hrgnWnd = 0;
234 wndPtr->hrgnWnd = hrgn;
235 WIN_ReleasePtr( wndPtr );
237 /* Size the window to the rectangle of the new region (if it isn't NULL) */
238 if (hrgn) SetWindowPos( hwnd, 0, rect.left, rect.top,
239 rect.right - rect.left, rect.bottom - rect.top,
240 SWP_NOSIZE | SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOACTIVATE |
241 SWP_NOZORDER | (bRedraw ? 0 : SWP_NOREDRAW) );
242 return TRUE;
246 /***********************************************************************
247 * GetClientRect (USER32.@)
249 BOOL WINAPI GetClientRect( HWND hwnd, LPRECT rect )
251 BOOL ret;
253 rect->right = rect->bottom = 0;
254 if ((ret = WIN_GetRectangles( hwnd, NULL, rect )))
256 rect->right -= rect->left;
257 rect->bottom -= rect->top;
259 rect->left = rect->top = 0;
260 return ret;
264 /*******************************************************************
265 * ClientToScreen (USER32.@)
267 BOOL WINAPI ClientToScreen( HWND hwnd, LPPOINT lppnt )
269 MapWindowPoints( hwnd, 0, lppnt, 1 );
270 return TRUE;
274 /*******************************************************************
275 * ScreenToClient (USER32.@)
277 BOOL WINAPI ScreenToClient( HWND hwnd, LPPOINT lppnt )
279 MapWindowPoints( 0, hwnd, lppnt, 1 );
280 return TRUE;
284 /***********************************************************************
285 * find_child_from_point
287 * Find the child that contains pt. Helper for WindowFromPoint.
288 * pt is in parent client coordinates.
289 * lparam is the param to pass in the WM_NCHITTEST message.
291 static HWND find_child_from_point( HWND parent, POINT pt, INT *hittest, LPARAM lparam )
293 int i, res;
294 LONG style, exstyle;
295 RECT rectWindow, rectClient;
296 WND *wndPtr;
297 HWND *list = WIN_ListChildren( parent );
298 HWND retvalue = 0;
300 if (!list) return 0;
301 for (i = 0; list[i]; i++)
303 /* If point is in window, and window is visible, and it */
304 /* is enabled (or it's a top-level window), then explore */
305 /* its children. Otherwise, go to the next window. */
307 style = GetWindowLongW( list[i], GWL_STYLE );
308 if (!(style & WS_VISIBLE)) continue; /* not visible -> skip */
309 if ((style & (WS_POPUP | WS_CHILD | WS_DISABLED)) == (WS_CHILD | WS_DISABLED))
310 continue; /* disabled child -> skip */
311 exstyle = GetWindowLongW( list[i], GWL_EXSTYLE );
312 if ((exstyle & (WS_EX_LAYERED | WS_EX_TRANSPARENT)) == (WS_EX_LAYERED | WS_EX_TRANSPARENT))
313 continue; /* transparent -> skip */
315 if (!WIN_GetRectangles( list[i], &rectWindow, &rectClient )) continue;
316 if (!PtInRect( &rectWindow, pt )) continue; /* not in window -> skip */
318 /* FIXME: check window region for other processes too */
319 if ((wndPtr = WIN_GetPtr( list[i] )) && wndPtr != WND_OTHER_PROCESS)
321 if (wndPtr->hrgnWnd && !PtInRegion( wndPtr->hrgnWnd,
322 pt.x - rectWindow.left, pt.y - rectWindow.top ))
324 WIN_ReleasePtr( wndPtr );
325 continue; /* point outside window region -> skip */
327 WIN_ReleasePtr( wndPtr );
330 /* If window is minimized or disabled, return at once */
331 if (style & WS_MINIMIZE)
333 *hittest = HTCAPTION;
334 retvalue = list[i];
335 break;
337 if (style & WS_DISABLED)
339 *hittest = HTERROR;
340 retvalue = list[i];
341 break;
344 /* If point is in client area, explore children */
345 if (PtInRect( &rectClient, pt ))
347 POINT new_pt;
349 new_pt.x = pt.x - rectClient.left;
350 new_pt.y = pt.y - rectClient.top;
351 if ((retvalue = find_child_from_point( list[i], new_pt, hittest, lparam ))) break;
354 /* Now it's inside window, send WM_NCCHITTEST (if same thread) */
355 if (!WIN_IsCurrentThread( list[i] ))
357 *hittest = HTCLIENT;
358 retvalue = list[i];
359 break;
361 if ((res = SendMessageA( list[i], WM_NCHITTEST, 0, lparam )) != HTTRANSPARENT)
363 *hittest = res; /* Found the window */
364 retvalue = list[i];
365 break;
367 /* continue search with next sibling */
369 HeapFree( GetProcessHeap(), 0, list );
370 return retvalue;
374 /***********************************************************************
375 * WINPOS_WindowFromPoint
377 * Find the window and hittest for a given point.
379 HWND WINPOS_WindowFromPoint( HWND hwndScope, POINT pt, INT *hittest )
381 POINT xy = pt;
382 int res;
383 LONG style;
385 TRACE("scope %p %ld,%ld\n", hwndScope, pt.x, pt.y);
387 if (!hwndScope) hwndScope = GetDesktopWindow();
388 style = GetWindowLongW( hwndScope, GWL_STYLE );
390 *hittest = HTERROR;
391 if (style & WS_DISABLED) return 0;
393 MapWindowPoints( GetDesktopWindow(), GetAncestor( hwndScope, GA_PARENT ), &xy, 1 );
395 if (!(style & WS_MINIMIZE))
397 RECT rectClient;
398 if (WIN_GetRectangles( hwndScope, NULL, &rectClient ) && PtInRect( &rectClient, xy ))
400 HWND ret;
402 xy.x -= rectClient.left;
403 xy.y -= rectClient.top;
404 if ((ret = find_child_from_point( hwndScope, xy, hittest, MAKELONG( pt.x, pt.y ) )))
406 TRACE( "found child %p\n", ret );
407 return ret;
412 /* If nothing found, try the scope window */
413 if (!WIN_IsCurrentThread( hwndScope ))
415 *hittest = HTCLIENT;
416 TRACE( "returning %p\n", hwndScope );
417 return hwndScope;
419 res = SendMessageA( hwndScope, WM_NCHITTEST, 0, MAKELONG( pt.x, pt.y ) );
420 if (res != HTTRANSPARENT)
422 *hittest = res; /* Found the window */
423 TRACE( "returning %p\n", hwndScope );
424 return hwndScope;
426 *hittest = HTNOWHERE;
427 TRACE( "nothing found\n" );
428 return 0;
432 /*******************************************************************
433 * WindowFromPoint (USER32.@)
435 HWND WINAPI WindowFromPoint( POINT pt )
437 INT hittest;
438 return WINPOS_WindowFromPoint( 0, pt, &hittest );
442 /*******************************************************************
443 * ChildWindowFromPoint (USER32.@)
445 HWND WINAPI ChildWindowFromPoint( HWND hwndParent, POINT pt )
447 return ChildWindowFromPointEx( hwndParent, pt, CWP_ALL );
450 /*******************************************************************
451 * ChildWindowFromPointEx (USER32.@)
453 HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
455 /* pt is in the client coordinates */
456 HWND *list;
457 int i;
458 RECT rect;
459 HWND retvalue;
461 GetClientRect( hwndParent, &rect );
462 if (!PtInRect( &rect, pt )) return 0;
463 if (!(list = WIN_ListChildren( hwndParent ))) return 0;
465 for (i = 0; list[i]; i++)
467 if (!WIN_GetRectangles( list[i], &rect, NULL )) continue;
468 if (!PtInRect( &rect, pt )) continue;
469 if (uFlags & (CWP_SKIPINVISIBLE|CWP_SKIPDISABLED))
471 LONG style = GetWindowLongW( list[i], GWL_STYLE );
472 if ((uFlags & CWP_SKIPINVISIBLE) && !(style & WS_VISIBLE)) continue;
473 if ((uFlags & CWP_SKIPDISABLED) && (style & WS_DISABLED)) continue;
475 if (uFlags & CWP_SKIPTRANSPARENT)
477 if (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TRANSPARENT) continue;
479 break;
481 retvalue = list[i];
482 HeapFree( GetProcessHeap(), 0, list );
483 if (!retvalue) retvalue = hwndParent;
484 return retvalue;
488 /*******************************************************************
489 * WINPOS_GetWinOffset
491 * Calculate the offset between the origin of the two windows. Used
492 * to implement MapWindowPoints.
494 static void WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo, POINT *offset )
496 WND * wndPtr;
498 offset->x = offset->y = 0;
500 /* Translate source window origin to screen coords */
501 if (hwndFrom)
503 HWND hwnd = hwndFrom;
505 while (hwnd)
507 if (hwnd == hwndTo) return;
508 if (!(wndPtr = WIN_GetPtr( hwnd )))
510 ERR( "bad hwndFrom = %p\n", hwnd );
511 return;
513 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
514 offset->x += wndPtr->rectClient.left;
515 offset->y += wndPtr->rectClient.top;
516 hwnd = wndPtr->parent;
517 WIN_ReleasePtr( wndPtr );
521 /* Translate origin to destination window coords */
522 if (hwndTo)
524 HWND hwnd = hwndTo;
526 while (hwnd)
528 if (!(wndPtr = WIN_GetPtr( hwnd )))
530 ERR( "bad hwndTo = %p\n", hwnd );
531 return;
533 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
534 offset->x -= wndPtr->rectClient.left;
535 offset->y -= wndPtr->rectClient.top;
536 hwnd = wndPtr->parent;
537 WIN_ReleasePtr( wndPtr );
540 return;
542 other_process: /* one of the parents may belong to another process, do it the hard way */
543 offset->x = offset->y = 0;
544 SERVER_START_REQ( get_windows_offset )
546 req->from = hwndFrom;
547 req->to = hwndTo;
548 if (!wine_server_call( req ))
550 offset->x = reply->x;
551 offset->y = reply->y;
554 SERVER_END_REQ;
558 /*******************************************************************
559 * MapWindowPoints (USER.258)
561 void WINAPI MapWindowPoints16( HWND16 hwndFrom, HWND16 hwndTo,
562 LPPOINT16 lppt, UINT16 count )
564 POINT offset;
566 WINPOS_GetWinOffset( WIN_Handle32(hwndFrom), WIN_Handle32(hwndTo), &offset );
567 while (count--)
569 lppt->x += offset.x;
570 lppt->y += offset.y;
571 lppt++;
576 /*******************************************************************
577 * MapWindowPoints (USER32.@)
579 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
581 POINT offset;
583 WINPOS_GetWinOffset( hwndFrom, hwndTo, &offset );
584 while (count--)
586 lppt->x += offset.x;
587 lppt->y += offset.y;
588 lppt++;
590 return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
594 /***********************************************************************
595 * IsIconic (USER32.@)
597 BOOL WINAPI IsIconic(HWND hWnd)
599 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
603 /***********************************************************************
604 * IsZoomed (USER32.@)
606 BOOL WINAPI IsZoomed(HWND hWnd)
608 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
612 /*******************************************************************
613 * AllowSetForegroundWindow (USER32.@)
615 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
617 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
618 * implemented, then fix this function. */
619 return TRUE;
623 /*******************************************************************
624 * LockSetForegroundWindow (USER32.@)
626 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
628 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
629 * implemented, then fix this function. */
630 return TRUE;
634 /***********************************************************************
635 * BringWindowToTop (USER32.@)
637 BOOL WINAPI BringWindowToTop( HWND hwnd )
639 return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
643 /***********************************************************************
644 * MoveWindow (USER32.@)
646 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
647 BOOL repaint )
649 int flags = SWP_NOZORDER | SWP_NOACTIVATE;
650 if (!repaint) flags |= SWP_NOREDRAW;
651 TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
652 return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
655 /***********************************************************************
656 * WINPOS_InitInternalPos
658 static LPINTERNALPOS WINPOS_InitInternalPos( WND* wnd, POINT pt, const RECT *restoreRect )
660 LPINTERNALPOS lpPos = (LPINTERNALPOS) GetPropA( wnd->hwndSelf,
661 atomInternalPos );
662 if( !lpPos )
664 /* this happens when the window is minimized/maximized
665 * for the first time (rectWindow is not adjusted yet) */
667 lpPos = HeapAlloc( GetProcessHeap(), 0, sizeof(INTERNALPOS) );
668 if( !lpPos ) return NULL;
670 SetPropA( wnd->hwndSelf, atomInternalPos, (HANDLE)lpPos );
671 lpPos->hwndIconTitle = 0; /* defer until needs to be shown */
672 CONV_RECT32TO16( &wnd->rectWindow, &lpPos->rectNormal );
673 *(UINT*)&lpPos->ptIconPos = *(UINT*)&lpPos->ptMaxPos = 0xFFFFFFFF;
676 if( wnd->dwStyle & WS_MINIMIZE )
677 CONV_POINT32TO16( &pt, &lpPos->ptIconPos );
678 else if( wnd->dwStyle & WS_MAXIMIZE )
679 CONV_POINT32TO16( &pt, &lpPos->ptMaxPos );
680 else if( restoreRect )
681 CONV_RECT32TO16( restoreRect, &lpPos->rectNormal );
683 return lpPos;
686 /***********************************************************************
687 * WINPOS_RedrawIconTitle
689 BOOL WINPOS_RedrawIconTitle( HWND hWnd )
691 LPINTERNALPOS lpPos = (LPINTERNALPOS)GetPropA( hWnd, atomInternalPos );
692 if( lpPos )
694 if( lpPos->hwndIconTitle )
696 SendMessageA( lpPos->hwndIconTitle, WM_SHOWWINDOW, TRUE, 0);
697 InvalidateRect( lpPos->hwndIconTitle, NULL, TRUE );
698 return TRUE;
701 return FALSE;
704 /***********************************************************************
705 * WINPOS_ShowIconTitle
707 BOOL WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
709 LPINTERNALPOS lpPos = (LPINTERNALPOS)GetPropA( hwnd, atomInternalPos );
711 if( lpPos && !(GetWindowLongA( hwnd, GWL_EXSTYLE) & WS_EX_MANAGED))
713 HWND title = lpPos->hwndIconTitle;
715 TRACE("%p %i\n", hwnd, (bShow != 0) );
717 if( !title )
718 lpPos->hwndIconTitle = title = ICONTITLE_Create( hwnd );
719 if( bShow )
721 if (!IsWindowVisible(title))
723 SendMessageA( title, WM_SHOWWINDOW, TRUE, 0 );
724 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
725 SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
728 else ShowWindow( title, SW_HIDE );
730 return FALSE;
733 /*******************************************************************
734 * WINPOS_GetMinMaxInfo
736 * Get the minimized and maximized information for a window.
738 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
739 POINT *minTrack, POINT *maxTrack )
741 LPINTERNALPOS lpPos;
742 MINMAXINFO MinMax;
743 INT xinc, yinc;
744 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
745 LONG exstyle = GetWindowLongA( hwnd, GWL_EXSTYLE );
746 RECT rc;
748 /* Compute default values */
750 GetWindowRect(hwnd, &rc);
751 MinMax.ptReserved.x = rc.left;
752 MinMax.ptReserved.y = rc.top;
754 if (style & WS_CHILD)
756 if ((style & WS_CAPTION) == WS_CAPTION)
757 style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
759 GetClientRect(GetParent(hwnd), &rc);
760 AdjustWindowRectEx(&rc, style, 0, exstyle);
762 /* avoid calculating this twice */
763 style &= ~(WS_DLGFRAME | WS_BORDER | WS_THICKFRAME);
765 MinMax.ptMaxSize.x = rc.right - rc.left;
766 MinMax.ptMaxSize.y = rc.bottom - rc.top;
768 else
770 MinMax.ptMaxSize.x = GetSystemMetrics(SM_CXSCREEN);
771 MinMax.ptMaxSize.y = GetSystemMetrics(SM_CYSCREEN);
773 MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
774 MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
775 MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXSCREEN);
776 MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYSCREEN);
778 if (HAS_DLGFRAME( style, exstyle ))
780 xinc = GetSystemMetrics(SM_CXDLGFRAME);
781 yinc = GetSystemMetrics(SM_CYDLGFRAME);
783 else
785 xinc = yinc = 0;
786 if (HAS_THICKFRAME(style))
788 xinc += GetSystemMetrics(SM_CXFRAME);
789 yinc += GetSystemMetrics(SM_CYFRAME);
791 if (style & WS_BORDER)
793 xinc += GetSystemMetrics(SM_CXBORDER);
794 yinc += GetSystemMetrics(SM_CYBORDER);
797 MinMax.ptMaxSize.x += 2 * xinc;
798 MinMax.ptMaxSize.y += 2 * yinc;
800 lpPos = (LPINTERNALPOS)GetPropA( hwnd, atomInternalPos );
801 if( lpPos && !EMPTYPOINT(lpPos->ptMaxPos) )
802 CONV_POINT16TO32( &lpPos->ptMaxPos, &MinMax.ptMaxPosition );
803 else
805 MinMax.ptMaxPosition.x = -xinc;
806 MinMax.ptMaxPosition.y = -yinc;
809 SendMessageA( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
811 /* Some sanity checks */
813 TRACE("%ld %ld / %ld %ld / %ld %ld / %ld %ld\n",
814 MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
815 MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
816 MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
817 MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
818 MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
819 MinMax.ptMinTrackSize.x );
820 MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
821 MinMax.ptMinTrackSize.y );
823 if (maxSize) *maxSize = MinMax.ptMaxSize;
824 if (maxPos) *maxPos = MinMax.ptMaxPosition;
825 if (minTrack) *minTrack = MinMax.ptMinTrackSize;
826 if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
829 /***********************************************************************
830 * ShowWindowAsync (USER32.@)
832 * doesn't wait; returns immediately.
833 * used by threads to toggle windows in other (possibly hanging) threads
835 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
837 HWND full_handle;
839 if (is_broadcast(hwnd))
841 SetLastError( ERROR_INVALID_PARAMETER );
842 return FALSE;
845 if ((full_handle = WIN_IsCurrentThread( hwnd )))
846 return USER_Driver.pShowWindow( full_handle, cmd );
847 return SendNotifyMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
851 /***********************************************************************
852 * ShowWindow (USER32.@)
854 BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
856 HWND full_handle;
858 if (is_broadcast(hwnd))
860 SetLastError( ERROR_INVALID_PARAMETER );
861 return FALSE;
863 if ((full_handle = WIN_IsCurrentThread( hwnd )))
865 if (USER_Driver.pShowWindow)
866 return USER_Driver.pShowWindow( full_handle, cmd );
867 return FALSE;
869 return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
873 /***********************************************************************
874 * GetInternalWindowPos (USER32.@)
876 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
877 LPPOINT ptIcon )
879 WINDOWPLACEMENT wndpl;
880 if (GetWindowPlacement( hwnd, &wndpl ))
882 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
883 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
884 return wndpl.showCmd;
886 return 0;
890 /***********************************************************************
891 * GetWindowPlacement (USER32.@)
893 * Win95:
894 * Fails if wndpl->length of Win95 (!) apps is invalid.
896 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
898 WND *pWnd = WIN_FindWndPtr( hwnd );
899 LPINTERNALPOS lpPos;
901 if(!pWnd ) return FALSE;
903 lpPos = WINPOS_InitInternalPos( pWnd, *(LPPOINT)&pWnd->rectWindow.left, &pWnd->rectWindow );
904 wndpl->length = sizeof(*wndpl);
905 if( pWnd->dwStyle & WS_MINIMIZE )
906 wndpl->showCmd = SW_SHOWMINIMIZED;
907 else
908 wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
909 if( pWnd->flags & WIN_RESTORE_MAX )
910 wndpl->flags = WPF_RESTORETOMAXIMIZED;
911 else
912 wndpl->flags = 0;
913 CONV_POINT16TO32( &lpPos->ptIconPos, &wndpl->ptMinPosition );
914 CONV_POINT16TO32( &lpPos->ptMaxPos, &wndpl->ptMaxPosition );
915 CONV_RECT16TO32( &lpPos->rectNormal, &wndpl->rcNormalPosition );
916 WIN_ReleaseWndPtr(pWnd);
917 return TRUE;
921 /***********************************************************************
922 * WINPOS_SetPlacement
924 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
926 WND *pWnd = WIN_FindWndPtr( hwnd );
927 if( pWnd )
929 LPINTERNALPOS lpPos = (LPINTERNALPOS)WINPOS_InitInternalPos( pWnd,
930 *(LPPOINT)&pWnd->rectWindow.left, &pWnd->rectWindow );
932 if( flags & PLACE_MIN ) CONV_POINT32TO16( &wndpl->ptMinPosition, &lpPos->ptIconPos );
933 if( flags & PLACE_MAX ) CONV_POINT32TO16( &wndpl->ptMaxPosition, &lpPos->ptMaxPos );
934 if( flags & PLACE_RECT) CONV_RECT32TO16( &wndpl->rcNormalPosition, &lpPos->rectNormal );
936 if( pWnd->dwStyle & WS_MINIMIZE )
938 WINPOS_ShowIconTitle( pWnd->hwndSelf, FALSE );
939 if( wndpl->flags & WPF_SETMINPOSITION && !EMPTYPOINT(lpPos->ptIconPos))
940 SetWindowPos( hwnd, 0, lpPos->ptIconPos.x, lpPos->ptIconPos.y,
941 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
943 else if( pWnd->dwStyle & WS_MAXIMIZE )
945 if( !EMPTYPOINT(lpPos->ptMaxPos) )
946 SetWindowPos( hwnd, 0, lpPos->ptMaxPos.x, lpPos->ptMaxPos.y,
947 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
949 else if( flags & PLACE_RECT )
950 SetWindowPos( hwnd, 0, lpPos->rectNormal.left, lpPos->rectNormal.top,
951 lpPos->rectNormal.right - lpPos->rectNormal.left,
952 lpPos->rectNormal.bottom - lpPos->rectNormal.top,
953 SWP_NOZORDER | SWP_NOACTIVATE );
955 ShowWindow( hwnd, wndpl->showCmd );
956 if( IsWindow(hwnd) && pWnd->dwStyle & WS_MINIMIZE )
958 if( pWnd->dwStyle & WS_VISIBLE ) WINPOS_ShowIconTitle( pWnd->hwndSelf, TRUE );
960 /* SDK: ...valid only the next time... */
961 if( wndpl->flags & WPF_RESTORETOMAXIMIZED ) pWnd->flags |= WIN_RESTORE_MAX;
963 WIN_ReleaseWndPtr(pWnd);
964 return TRUE;
966 return FALSE;
970 /***********************************************************************
971 * SetWindowPlacement (USER32.@)
973 * Win95:
974 * Fails if wndpl->length of Win95 (!) apps is invalid.
976 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
978 if (!wpl) return FALSE;
979 return WINPOS_SetPlacement( hwnd, wpl, PLACE_MIN | PLACE_MAX | PLACE_RECT );
983 /***********************************************************************
984 * AnimateWindow (USER32.@)
985 * Shows/Hides a window with an animation
986 * NO ANIMATION YET
988 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
990 FIXME("partial stub\n");
992 /* If trying to show/hide and it's already *
993 * shown/hidden or invalid window, fail with *
994 * invalid parameter */
995 if(!IsWindow(hwnd) ||
996 (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
997 (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
999 SetLastError(ERROR_INVALID_PARAMETER);
1000 return FALSE;
1003 ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1005 return TRUE;
1008 /***********************************************************************
1009 * SetInternalWindowPos (USER32.@)
1011 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1012 LPRECT rect, LPPOINT pt )
1014 if( IsWindow(hwnd) )
1016 WINDOWPLACEMENT wndpl;
1017 UINT flags;
1019 wndpl.length = sizeof(wndpl);
1020 wndpl.showCmd = showCmd;
1021 wndpl.flags = flags = 0;
1023 if( pt )
1025 flags |= PLACE_MIN;
1026 wndpl.flags |= WPF_SETMINPOSITION;
1027 wndpl.ptMinPosition = *pt;
1029 if( rect )
1031 flags |= PLACE_RECT;
1032 wndpl.rcNormalPosition = *rect;
1034 WINPOS_SetPlacement( hwnd, &wndpl, flags );
1039 /*******************************************************************
1040 * can_activate_window
1042 * Check if we can activate the specified window.
1044 static BOOL can_activate_window( HWND hwnd )
1046 LONG style;
1048 if (!hwnd) return FALSE;
1049 style = GetWindowLongW( hwnd, GWL_STYLE );
1050 if (!(style & WS_VISIBLE)) return FALSE;
1051 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1052 return !(style & WS_DISABLED);
1056 /*******************************************************************
1057 * WINPOS_ActivateOtherWindow
1059 * Activates window other than pWnd.
1061 void WINPOS_ActivateOtherWindow(HWND hwnd)
1063 HWND hwndTo, fg;
1065 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1067 hwndTo = GetAncestor( hwndTo, GA_ROOT );
1068 if (can_activate_window( hwndTo )) goto done;
1071 hwndTo = hwnd;
1072 for (;;)
1074 if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1075 if (can_activate_window( hwndTo )) break;
1078 done:
1079 fg = GetForegroundWindow();
1080 TRACE("win = %p fg = %p\n", hwndTo, fg);
1081 if (!fg || (hwnd == fg))
1083 if (SetForegroundWindow( hwndTo )) return;
1085 if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1089 /***********************************************************************
1090 * WINPOS_HandleWindowPosChanging16
1092 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1094 LONG WINPOS_HandleWindowPosChanging16( HWND hwnd, WINDOWPOS16 *winpos )
1096 POINT minTrack, maxTrack;
1097 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1099 if (winpos->flags & SWP_NOSIZE) return 0;
1100 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1102 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1103 if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1104 if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1105 if (!(style & WS_MINIMIZE))
1107 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1108 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1111 return 0;
1115 /***********************************************************************
1116 * WINPOS_HandleWindowPosChanging
1118 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1120 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1122 POINT minTrack, maxTrack;
1123 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1125 if (winpos->flags & SWP_NOSIZE) return 0;
1126 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1128 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1129 if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1130 if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1131 if (!(style & WS_MINIMIZE))
1133 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1134 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1137 return 0;
1141 /***********************************************************************
1142 * dump_winpos_flags
1144 static void dump_winpos_flags(UINT flags)
1146 TRACE("flags:");
1147 if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1148 if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1149 if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1150 if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1151 if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1152 if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1153 if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1154 if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1155 if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1156 if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1157 if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1158 if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1159 if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1161 #define DUMPED_FLAGS \
1162 (SWP_NOSIZE | \
1163 SWP_NOMOVE | \
1164 SWP_NOZORDER | \
1165 SWP_NOREDRAW | \
1166 SWP_NOACTIVATE | \
1167 SWP_FRAMECHANGED | \
1168 SWP_SHOWWINDOW | \
1169 SWP_HIDEWINDOW | \
1170 SWP_NOCOPYBITS | \
1171 SWP_NOOWNERZORDER | \
1172 SWP_NOSENDCHANGING | \
1173 SWP_DEFERERASE | \
1174 SWP_ASYNCWINDOWPOS)
1176 if(flags & ~DUMPED_FLAGS) TRACE(" %08x", flags & ~DUMPED_FLAGS);
1177 TRACE("\n");
1178 #undef DUMPED_FLAGS
1181 /***********************************************************************
1182 * SetWindowPos (USER32.@)
1184 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
1185 INT x, INT y, INT cx, INT cy, UINT flags )
1187 WINDOWPOS winpos;
1189 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1190 hwnd, hwndInsertAfter, x, y, cx, cy, flags);
1191 if(TRACE_ON(win)) dump_winpos_flags(flags);
1193 if (is_broadcast(hwnd))
1195 SetLastError( ERROR_INVALID_PARAMETER );
1196 return FALSE;
1199 winpos.hwnd = WIN_GetFullHandle(hwnd);
1200 winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
1201 winpos.x = x;
1202 winpos.y = y;
1203 winpos.cx = cx;
1204 winpos.cy = cy;
1205 winpos.flags = flags;
1206 if (WIN_IsCurrentThread( hwnd )) return USER_Driver.pSetWindowPos( &winpos );
1207 return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
1211 /***********************************************************************
1212 * BeginDeferWindowPos (USER32.@)
1214 HDWP WINAPI BeginDeferWindowPos( INT count )
1216 HDWP handle;
1217 DWP *pDWP;
1219 TRACE("%d\n", count);
1221 if (count < 0)
1223 SetLastError(ERROR_INVALID_PARAMETER);
1224 return 0;
1226 /* Windows allows zero count, in which case it allocates context for 8 moves */
1227 if (count == 0) count = 8;
1229 handle = USER_HEAP_ALLOC( sizeof(DWP) + (count-1)*sizeof(WINDOWPOS) );
1230 if (!handle) return 0;
1231 pDWP = (DWP *) USER_HEAP_LIN_ADDR( handle );
1232 pDWP->actualCount = 0;
1233 pDWP->suggestedCount = count;
1234 pDWP->valid = TRUE;
1235 pDWP->wMagic = DWP_MAGIC;
1236 pDWP->hwndParent = 0;
1238 TRACE("returning hdwp %p\n", handle);
1239 return handle;
1243 /***********************************************************************
1244 * DeferWindowPos (USER32.@)
1246 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
1247 INT x, INT y, INT cx, INT cy,
1248 UINT flags )
1250 DWP *pDWP;
1251 int i;
1252 HDWP newhdwp = hdwp,retvalue;
1254 TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1255 hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
1257 hwnd = WIN_GetFullHandle( hwnd );
1258 if (hwnd == GetDesktopWindow()) return 0;
1260 if (!(pDWP = USER_HEAP_LIN_ADDR( hdwp ))) return 0;
1262 USER_Lock();
1264 for (i = 0; i < pDWP->actualCount; i++)
1266 if (pDWP->winPos[i].hwnd == hwnd)
1268 /* Merge with the other changes */
1269 if (!(flags & SWP_NOZORDER))
1271 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
1273 if (!(flags & SWP_NOMOVE))
1275 pDWP->winPos[i].x = x;
1276 pDWP->winPos[i].y = y;
1278 if (!(flags & SWP_NOSIZE))
1280 pDWP->winPos[i].cx = cx;
1281 pDWP->winPos[i].cy = cy;
1283 pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
1284 SWP_NOZORDER | SWP_NOREDRAW |
1285 SWP_NOACTIVATE | SWP_NOCOPYBITS|
1286 SWP_NOOWNERZORDER);
1287 pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
1288 SWP_FRAMECHANGED);
1289 retvalue = hdwp;
1290 goto END;
1293 if (pDWP->actualCount >= pDWP->suggestedCount)
1295 newhdwp = USER_HEAP_REALLOC( hdwp,
1296 sizeof(DWP) + pDWP->suggestedCount*sizeof(WINDOWPOS) );
1297 if (!newhdwp)
1299 retvalue = 0;
1300 goto END;
1302 pDWP = (DWP *) USER_HEAP_LIN_ADDR( newhdwp );
1303 pDWP->suggestedCount++;
1305 pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
1306 pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
1307 pDWP->winPos[pDWP->actualCount].x = x;
1308 pDWP->winPos[pDWP->actualCount].y = y;
1309 pDWP->winPos[pDWP->actualCount].cx = cx;
1310 pDWP->winPos[pDWP->actualCount].cy = cy;
1311 pDWP->winPos[pDWP->actualCount].flags = flags;
1312 pDWP->actualCount++;
1313 retvalue = newhdwp;
1314 END:
1315 USER_Unlock();
1316 return retvalue;
1320 /***********************************************************************
1321 * EndDeferWindowPos (USER32.@)
1323 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
1325 DWP *pDWP;
1326 WINDOWPOS *winpos;
1327 BOOL res = TRUE;
1328 int i;
1330 TRACE("%p\n", hdwp);
1332 pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
1333 if (!pDWP) return FALSE;
1334 for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
1336 if (!(res = USER_Driver.pSetWindowPos( winpos ))) break;
1338 USER_HEAP_FREE( hdwp );
1339 return res;
1343 /***********************************************************************
1344 * TileChildWindows (USER.199)
1346 void WINAPI TileChildWindows16( HWND16 parent, WORD action )
1348 FIXME("(%04x, %d): stub\n", parent, action);
1351 /***********************************************************************
1352 * CascadeChildWindows (USER.198)
1354 void WINAPI CascadeChildWindows16( HWND16 parent, WORD action )
1356 FIXME("(%04x, %d): stub\n", parent, action);