Release 20030408.
[wine/gsoc-2012-control.git] / windows / winpos.c
blob98871797ed583b49f6bef1ddbeac2a136e3b7fff
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 <string.h>
23 #include "winerror.h"
24 #include "windef.h"
25 #include "wingdi.h"
26 #include "winerror.h"
27 #include "wine/winuser16.h"
28 #include "wine/server.h"
29 #include "controls.h"
30 #include "user.h"
31 #include "win.h"
32 #include "message.h"
33 #include "winpos.h"
34 #include "nonclient.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(win);
39 #define HAS_DLGFRAME(style,exStyle) \
40 (((exStyle) & WS_EX_DLGMODALFRAME) || \
41 (((style) & WS_DLGFRAME) && !((style) & WS_BORDER)))
43 #define HAS_THICKFRAME(style) \
44 (((style) & WS_THICKFRAME) && \
45 !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
47 #define EMPTYPOINT(pt) ((*(LONG*)&(pt)) == -1)
49 #define PLACE_MIN 0x0001
50 #define PLACE_MAX 0x0002
51 #define PLACE_RECT 0x0004
54 #define DWP_MAGIC ((INT)('W' | ('P' << 8) | ('O' << 16) | ('S' << 24)))
56 typedef struct
58 INT actualCount;
59 INT suggestedCount;
60 BOOL valid;
61 INT wMagic;
62 HWND hwndParent;
63 WINDOWPOS winPos[1];
64 } DWP;
66 /* ----- internal variables ----- */
68 static HWND hGlobalShellWindow=0; /*the shell*/
69 static HWND hGlobalTaskmanWindow=0;
70 static HWND hGlobalProgmanWindow=0;
72 static LPCSTR atomInternalPos;
75 /***********************************************************************
76 * WINPOS_CreateInternalPosAtom
78 BOOL WINPOS_CreateInternalPosAtom()
80 LPSTR str = "SysIP";
81 atomInternalPos = (LPCSTR)(DWORD)GlobalAddAtomA(str);
82 return (atomInternalPos) ? TRUE : FALSE;
85 /***********************************************************************
86 * WINPOS_CheckInternalPos
88 * Called when a window is destroyed.
90 void WINPOS_CheckInternalPos( HWND hwnd )
92 LPINTERNALPOS lpPos = (LPINTERNALPOS) GetPropA( hwnd, atomInternalPos );
94 if( lpPos )
96 if( IsWindow(lpPos->hwndIconTitle) )
97 DestroyWindow( lpPos->hwndIconTitle );
98 HeapFree( GetProcessHeap(), 0, lpPos );
102 /***********************************************************************
103 * ArrangeIconicWindows (USER32.@)
105 UINT WINAPI ArrangeIconicWindows( HWND parent )
107 RECT rectParent;
108 HWND hwndChild;
109 INT x, y, xspacing, yspacing;
111 GetClientRect( parent, &rectParent );
112 x = rectParent.left;
113 y = rectParent.bottom;
114 xspacing = GetSystemMetrics(SM_CXICONSPACING);
115 yspacing = GetSystemMetrics(SM_CYICONSPACING);
117 hwndChild = GetWindow( parent, GW_CHILD );
118 while (hwndChild)
120 if( IsIconic( hwndChild ) )
122 WINPOS_ShowIconTitle( hwndChild, FALSE );
124 SetWindowPos( hwndChild, 0, x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
125 y - yspacing - GetSystemMetrics(SM_CYICON)/2, 0, 0,
126 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
127 if( IsWindow(hwndChild) )
128 WINPOS_ShowIconTitle(hwndChild , TRUE );
130 if (x <= rectParent.right - xspacing) x += xspacing;
131 else
133 x = rectParent.left;
134 y -= yspacing;
137 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
139 return yspacing;
143 /***********************************************************************
144 * SwitchToThisWindow (USER32.@)
146 void WINAPI SwitchToThisWindow( HWND hwnd, BOOL restore )
148 ShowWindow( hwnd, restore ? SW_RESTORE : SW_SHOWMINIMIZED );
152 /***********************************************************************
153 * GetWindowRect (USER32.@)
155 BOOL WINAPI GetWindowRect( HWND hwnd, LPRECT rect )
157 BOOL ret = WIN_GetRectangles( hwnd, rect, NULL );
158 if (ret)
160 MapWindowPoints( GetAncestor( hwnd, GA_PARENT ), 0, (POINT *)rect, 2 );
161 TRACE( "hwnd %p (%ld,%ld)-(%ld,%ld)\n",
162 hwnd, rect->left, rect->top, rect->right, rect->bottom);
164 return ret;
168 /***********************************************************************
169 * GetWindowRgn (USER32.@)
171 int WINAPI GetWindowRgn ( HWND hwnd, HRGN hrgn )
173 int nRet = ERROR;
174 WND *wndPtr = WIN_GetPtr( hwnd );
176 if (wndPtr == WND_OTHER_PROCESS)
178 if (IsWindow( hwnd ))
179 FIXME( "not supported on other process window %p\n", hwnd );
180 wndPtr = NULL;
182 if (!wndPtr)
184 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
185 return ERROR;
187 if (wndPtr->hrgnWnd) nRet = CombineRgn( hrgn, wndPtr->hrgnWnd, 0, RGN_COPY );
188 WIN_ReleasePtr( wndPtr );
189 return nRet;
193 /***********************************************************************
194 * SetWindowRgn (USER32.@)
196 int WINAPI SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL bRedraw )
198 RECT rect;
199 WND *wndPtr;
201 if (hrgn) /* verify that region really exists */
203 if (GetRgnBox( hrgn, &rect ) == ERROR) return FALSE;
206 if (USER_Driver.pSetWindowRgn)
207 return USER_Driver.pSetWindowRgn( hwnd, hrgn, bRedraw );
209 if ((wndPtr = WIN_GetPtr( hwnd )) == WND_OTHER_PROCESS)
211 if (IsWindow( hwnd ))
212 FIXME( "not supported on other process window %p\n", hwnd );
213 wndPtr = NULL;
215 if (!wndPtr)
217 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
218 return FALSE;
221 if (wndPtr->hrgnWnd == hrgn)
223 WIN_ReleasePtr( wndPtr );
224 return TRUE;
227 if (wndPtr->hrgnWnd)
229 /* delete previous region */
230 DeleteObject(wndPtr->hrgnWnd);
231 wndPtr->hrgnWnd = 0;
233 wndPtr->hrgnWnd = hrgn;
234 WIN_ReleasePtr( wndPtr );
236 /* Size the window to the rectangle of the new region (if it isn't NULL) */
237 if (hrgn) SetWindowPos( hwnd, 0, rect.left, rect.top,
238 rect.right - rect.left, rect.bottom - rect.top,
239 SWP_NOSIZE | SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOACTIVATE |
240 SWP_NOZORDER | (bRedraw ? 0 : SWP_NOREDRAW) );
241 return TRUE;
245 /***********************************************************************
246 * GetClientRect (USER32.@)
248 BOOL WINAPI GetClientRect( HWND hwnd, LPRECT rect )
250 BOOL ret;
252 rect->right = rect->bottom = 0;
253 if ((ret = WIN_GetRectangles( hwnd, NULL, rect )))
255 rect->right -= rect->left;
256 rect->bottom -= rect->top;
258 rect->left = rect->top = 0;
259 return ret;
263 /*******************************************************************
264 * ClientToScreen (USER32.@)
266 BOOL WINAPI ClientToScreen( HWND hwnd, LPPOINT lppnt )
268 MapWindowPoints( hwnd, 0, lppnt, 1 );
269 return TRUE;
273 /*******************************************************************
274 * ScreenToClient (USER32.@)
276 BOOL WINAPI ScreenToClient( HWND hwnd, LPPOINT lppnt )
278 MapWindowPoints( 0, hwnd, lppnt, 1 );
279 return TRUE;
283 /***********************************************************************
284 * find_child_from_point
286 * Find the child that contains pt. Helper for WindowFromPoint.
287 * pt is in parent client coordinates.
288 * lparam is the param to pass in the WM_NCHITTEST message.
290 static HWND find_child_from_point( HWND parent, POINT pt, INT *hittest, LPARAM lparam )
292 int i, res;
293 LONG style, exstyle;
294 RECT rectWindow, rectClient;
295 WND *wndPtr;
296 HWND *list = WIN_ListChildren( parent );
297 HWND retvalue = 0;
299 if (!list) return 0;
300 for (i = 0; list[i]; i++)
302 /* If point is in window, and window is visible, and it */
303 /* is enabled (or it's a top-level window), then explore */
304 /* its children. Otherwise, go to the next window. */
306 style = GetWindowLongW( list[i], GWL_STYLE );
307 if (!(style & WS_VISIBLE)) continue; /* not visible -> skip */
308 if ((style & (WS_POPUP | WS_CHILD | WS_DISABLED)) == (WS_CHILD | WS_DISABLED))
309 continue; /* disabled child -> skip */
310 exstyle = GetWindowLongW( list[i], GWL_EXSTYLE );
311 if ((exstyle & (WS_EX_LAYERED | WS_EX_TRANSPARENT)) == (WS_EX_LAYERED | WS_EX_TRANSPARENT))
312 continue; /* transparent -> skip */
314 if (!WIN_GetRectangles( list[i], &rectWindow, &rectClient )) continue;
315 if (!PtInRect( &rectWindow, pt )) continue; /* not in window -> skip */
317 /* FIXME: check window region for other processes too */
318 if ((wndPtr = WIN_GetPtr( list[i] )) && wndPtr != WND_OTHER_PROCESS)
320 if (wndPtr->hrgnWnd && !PtInRegion( wndPtr->hrgnWnd,
321 pt.x - rectWindow.left, pt.y - rectWindow.top ))
323 WIN_ReleasePtr( wndPtr );
324 continue; /* point outside window region -> skip */
326 WIN_ReleasePtr( wndPtr );
329 /* If window is minimized or disabled, return at once */
330 if (style & WS_MINIMIZE)
332 *hittest = HTCAPTION;
333 retvalue = list[i];
334 break;
336 if (style & WS_DISABLED)
338 *hittest = HTERROR;
339 retvalue = list[i];
340 break;
343 /* If point is in client area, explore children */
344 if (PtInRect( &rectClient, pt ))
346 POINT new_pt;
348 new_pt.x = pt.x - rectClient.left;
349 new_pt.y = pt.y - rectClient.top;
350 if ((retvalue = find_child_from_point( list[i], new_pt, hittest, lparam ))) break;
353 /* Now it's inside window, send WM_NCCHITTEST (if same thread) */
354 if (!WIN_IsCurrentThread( list[i] ))
356 *hittest = HTCLIENT;
357 retvalue = list[i];
358 break;
360 if ((res = SendMessageA( list[i], WM_NCHITTEST, 0, lparam )) != HTTRANSPARENT)
362 *hittest = res; /* Found the window */
363 retvalue = list[i];
364 break;
366 /* continue search with next sibling */
368 HeapFree( GetProcessHeap(), 0, list );
369 return retvalue;
373 /***********************************************************************
374 * WINPOS_WindowFromPoint
376 * Find the window and hittest for a given point.
378 HWND WINPOS_WindowFromPoint( HWND hwndScope, POINT pt, INT *hittest )
380 POINT xy = pt;
381 int res;
382 LONG style;
384 TRACE("scope %p %ld,%ld\n", hwndScope, pt.x, pt.y);
386 if (!hwndScope) hwndScope = GetDesktopWindow();
387 style = GetWindowLongW( hwndScope, GWL_STYLE );
389 *hittest = HTERROR;
390 if (style & WS_DISABLED) return 0;
392 MapWindowPoints( GetDesktopWindow(), GetAncestor( hwndScope, GA_PARENT ), &xy, 1 );
394 if (!(style & WS_MINIMIZE))
396 RECT rectClient;
397 if (WIN_GetRectangles( hwndScope, NULL, &rectClient ) && PtInRect( &rectClient, xy ))
399 HWND ret;
401 xy.x -= rectClient.left;
402 xy.y -= rectClient.top;
403 if ((ret = find_child_from_point( hwndScope, xy, hittest, MAKELONG( pt.x, pt.y ) )))
405 TRACE( "found child %p\n", ret );
406 return ret;
411 /* If nothing found, try the scope window */
412 if (!WIN_IsCurrentThread( hwndScope ))
414 *hittest = HTCLIENT;
415 TRACE( "returning %p\n", hwndScope );
416 return hwndScope;
418 res = SendMessageA( hwndScope, WM_NCHITTEST, 0, MAKELONG( pt.x, pt.y ) );
419 if (res != HTTRANSPARENT)
421 *hittest = res; /* Found the window */
422 TRACE( "returning %p\n", hwndScope );
423 return hwndScope;
425 *hittest = HTNOWHERE;
426 TRACE( "nothing found\n" );
427 return 0;
431 /*******************************************************************
432 * WindowFromPoint (USER32.@)
434 HWND WINAPI WindowFromPoint( POINT pt )
436 INT hittest;
437 return WINPOS_WindowFromPoint( 0, pt, &hittest );
441 /*******************************************************************
442 * ChildWindowFromPoint (USER32.@)
444 HWND WINAPI ChildWindowFromPoint( HWND hwndParent, POINT pt )
446 return ChildWindowFromPointEx( hwndParent, pt, CWP_ALL );
449 /*******************************************************************
450 * ChildWindowFromPointEx (USER32.@)
452 HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
454 /* pt is in the client coordinates */
455 HWND *list;
456 int i;
457 RECT rect;
458 HWND retvalue;
460 GetClientRect( hwndParent, &rect );
461 if (!PtInRect( &rect, pt )) return 0;
462 if (!(list = WIN_ListChildren( hwndParent ))) return 0;
464 for (i = 0; list[i]; i++)
466 if (!WIN_GetRectangles( list[i], &rect, NULL )) continue;
467 if (!PtInRect( &rect, pt )) continue;
468 if (uFlags & (CWP_SKIPINVISIBLE|CWP_SKIPDISABLED))
470 LONG style = GetWindowLongW( list[i], GWL_STYLE );
471 if ((uFlags & CWP_SKIPINVISIBLE) && !(style & WS_VISIBLE)) continue;
472 if ((uFlags & CWP_SKIPDISABLED) && (style & WS_DISABLED)) continue;
474 if (uFlags & CWP_SKIPTRANSPARENT)
476 if (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TRANSPARENT) continue;
478 break;
480 retvalue = list[i];
481 HeapFree( GetProcessHeap(), 0, list );
482 if (!retvalue) retvalue = hwndParent;
483 return retvalue;
487 /*******************************************************************
488 * WINPOS_GetWinOffset
490 * Calculate the offset between the origin of the two windows. Used
491 * to implement MapWindowPoints.
493 static void WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo, POINT *offset )
495 WND * wndPtr;
497 offset->x = offset->y = 0;
499 /* Translate source window origin to screen coords */
500 if (hwndFrom)
502 HWND hwnd = hwndFrom;
504 while (hwnd)
506 if (hwnd == hwndTo) return;
507 if (!(wndPtr = WIN_GetPtr( hwnd )))
509 ERR( "bad hwndFrom = %p\n", hwnd );
510 return;
512 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
513 offset->x += wndPtr->rectClient.left;
514 offset->y += wndPtr->rectClient.top;
515 hwnd = wndPtr->parent;
516 WIN_ReleasePtr( wndPtr );
520 /* Translate origin to destination window coords */
521 if (hwndTo)
523 HWND hwnd = hwndTo;
525 while (hwnd)
527 if (!(wndPtr = WIN_GetPtr( hwnd )))
529 ERR( "bad hwndTo = %p\n", hwnd );
530 return;
532 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
533 offset->x -= wndPtr->rectClient.left;
534 offset->y -= wndPtr->rectClient.top;
535 hwnd = wndPtr->parent;
536 WIN_ReleasePtr( wndPtr );
539 return;
541 other_process: /* one of the parents may belong to another process, do it the hard way */
542 offset->x = offset->y = 0;
543 SERVER_START_REQ( get_windows_offset )
545 req->from = hwndFrom;
546 req->to = hwndTo;
547 if (!wine_server_call( req ))
549 offset->x = reply->x;
550 offset->y = reply->y;
553 SERVER_END_REQ;
557 /*******************************************************************
558 * MapWindowPoints (USER.258)
560 void WINAPI MapWindowPoints16( HWND16 hwndFrom, HWND16 hwndTo,
561 LPPOINT16 lppt, UINT16 count )
563 POINT offset;
565 WINPOS_GetWinOffset( WIN_Handle32(hwndFrom), WIN_Handle32(hwndTo), &offset );
566 while (count--)
568 lppt->x += offset.x;
569 lppt->y += offset.y;
570 lppt++;
575 /*******************************************************************
576 * MapWindowPoints (USER32.@)
578 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
580 POINT offset;
582 WINPOS_GetWinOffset( hwndFrom, hwndTo, &offset );
583 while (count--)
585 lppt->x += offset.x;
586 lppt->y += offset.y;
587 lppt++;
589 return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
593 /***********************************************************************
594 * IsIconic (USER32.@)
596 BOOL WINAPI IsIconic(HWND hWnd)
598 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
602 /***********************************************************************
603 * IsZoomed (USER32.@)
605 BOOL WINAPI IsZoomed(HWND hWnd)
607 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
611 /*******************************************************************
612 * AllowSetForegroundWindow (USER32.@)
614 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
616 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
617 * implemented, then fix this function. */
618 return TRUE;
622 /*******************************************************************
623 * LockSetForegroundWindow (USER32.@)
625 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
627 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
628 * implemented, then fix this function. */
629 return TRUE;
633 /*******************************************************************
634 * SetShellWindow (USER32.@)
636 HWND WINAPI SetShellWindow(HWND hwndshell)
637 { WARN("(hWnd=%p) semi stub\n",hwndshell );
639 hGlobalShellWindow = WIN_GetFullHandle( hwndshell );
640 return hGlobalShellWindow;
644 /*******************************************************************
645 * GetShellWindow (USER32.@)
647 HWND WINAPI GetShellWindow(void)
648 { WARN("(hWnd=%p) semi stub\n",hGlobalShellWindow );
650 return hGlobalShellWindow;
654 /***********************************************************************
655 * BringWindowToTop (USER32.@)
657 BOOL WINAPI BringWindowToTop( HWND hwnd )
659 return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
663 /***********************************************************************
664 * MoveWindow (USER32.@)
666 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
667 BOOL repaint )
669 int flags = SWP_NOZORDER | SWP_NOACTIVATE;
670 if (!repaint) flags |= SWP_NOREDRAW;
671 TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
672 return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
675 /***********************************************************************
676 * WINPOS_InitInternalPos
678 static LPINTERNALPOS WINPOS_InitInternalPos( WND* wnd, POINT pt, const RECT *restoreRect )
680 LPINTERNALPOS lpPos = (LPINTERNALPOS) GetPropA( wnd->hwndSelf,
681 atomInternalPos );
682 if( !lpPos )
684 /* this happens when the window is minimized/maximized
685 * for the first time (rectWindow is not adjusted yet) */
687 lpPos = HeapAlloc( GetProcessHeap(), 0, sizeof(INTERNALPOS) );
688 if( !lpPos ) return NULL;
690 SetPropA( wnd->hwndSelf, atomInternalPos, (HANDLE)lpPos );
691 lpPos->hwndIconTitle = 0; /* defer until needs to be shown */
692 CONV_RECT32TO16( &wnd->rectWindow, &lpPos->rectNormal );
693 *(UINT*)&lpPos->ptIconPos = *(UINT*)&lpPos->ptMaxPos = 0xFFFFFFFF;
696 if( wnd->dwStyle & WS_MINIMIZE )
697 CONV_POINT32TO16( &pt, &lpPos->ptIconPos );
698 else if( wnd->dwStyle & WS_MAXIMIZE )
699 CONV_POINT32TO16( &pt, &lpPos->ptMaxPos );
700 else if( restoreRect )
701 CONV_RECT32TO16( restoreRect, &lpPos->rectNormal );
703 return lpPos;
706 /***********************************************************************
707 * WINPOS_RedrawIconTitle
709 BOOL WINPOS_RedrawIconTitle( HWND hWnd )
711 LPINTERNALPOS lpPos = (LPINTERNALPOS)GetPropA( hWnd, atomInternalPos );
712 if( lpPos )
714 if( lpPos->hwndIconTitle )
716 SendMessageA( lpPos->hwndIconTitle, WM_SHOWWINDOW, TRUE, 0);
717 InvalidateRect( lpPos->hwndIconTitle, NULL, TRUE );
718 return TRUE;
721 return FALSE;
724 /***********************************************************************
725 * WINPOS_ShowIconTitle
727 BOOL WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
729 LPINTERNALPOS lpPos = (LPINTERNALPOS)GetPropA( hwnd, atomInternalPos );
731 if( lpPos && !(GetWindowLongA( hwnd, GWL_EXSTYLE) & WS_EX_MANAGED))
733 HWND title = lpPos->hwndIconTitle;
735 TRACE("%p %i\n", hwnd, (bShow != 0) );
737 if( !title )
738 lpPos->hwndIconTitle = title = ICONTITLE_Create( hwnd );
739 if( bShow )
741 if (!IsWindowVisible(title))
743 SendMessageA( title, WM_SHOWWINDOW, TRUE, 0 );
744 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
745 SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
748 else ShowWindow( title, SW_HIDE );
750 return FALSE;
753 /*******************************************************************
754 * WINPOS_GetMinMaxInfo
756 * Get the minimized and maximized information for a window.
758 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
759 POINT *minTrack, POINT *maxTrack )
761 LPINTERNALPOS lpPos;
762 MINMAXINFO MinMax;
763 INT xinc, yinc;
764 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
765 LONG exstyle = GetWindowLongA( hwnd, GWL_EXSTYLE );
767 /* Compute default values */
769 MinMax.ptMaxSize.x = GetSystemMetrics(SM_CXSCREEN);
770 MinMax.ptMaxSize.y = GetSystemMetrics(SM_CYSCREEN);
771 MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
772 MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
773 MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXSCREEN);
774 MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYSCREEN);
776 if (HAS_DLGFRAME( style, exstyle ))
778 xinc = GetSystemMetrics(SM_CXDLGFRAME);
779 yinc = GetSystemMetrics(SM_CYDLGFRAME);
781 else
783 xinc = yinc = 0;
784 if (HAS_THICKFRAME(style))
786 xinc += GetSystemMetrics(SM_CXFRAME);
787 yinc += GetSystemMetrics(SM_CYFRAME);
789 if (style & WS_BORDER)
791 xinc += GetSystemMetrics(SM_CXBORDER);
792 yinc += GetSystemMetrics(SM_CYBORDER);
795 MinMax.ptMaxSize.x += 2 * xinc;
796 MinMax.ptMaxSize.y += 2 * yinc;
798 lpPos = (LPINTERNALPOS)GetPropA( hwnd, atomInternalPos );
799 if( lpPos && !EMPTYPOINT(lpPos->ptMaxPos) )
800 CONV_POINT16TO32( &lpPos->ptMaxPos, &MinMax.ptMaxPosition );
801 else
803 MinMax.ptMaxPosition.x = -xinc;
804 MinMax.ptMaxPosition.y = -yinc;
807 SendMessageA( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
809 /* Some sanity checks */
811 TRACE("%ld %ld / %ld %ld / %ld %ld / %ld %ld\n",
812 MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
813 MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
814 MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
815 MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
816 MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
817 MinMax.ptMinTrackSize.x );
818 MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
819 MinMax.ptMinTrackSize.y );
821 if (maxSize) *maxSize = MinMax.ptMaxSize;
822 if (maxPos) *maxPos = MinMax.ptMaxPosition;
823 if (minTrack) *minTrack = MinMax.ptMinTrackSize;
824 if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
827 /***********************************************************************
828 * ShowWindowAsync (USER32.@)
830 * doesn't wait; returns immediately.
831 * used by threads to toggle windows in other (possibly hanging) threads
833 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
835 HWND full_handle;
837 if ((full_handle = WIN_IsCurrentThread( hwnd )))
838 return USER_Driver.pShowWindow( full_handle, cmd );
839 return SendNotifyMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
843 /***********************************************************************
844 * ShowWindow (USER32.@)
846 BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
848 HWND full_handle;
850 if ((full_handle = WIN_IsCurrentThread( hwnd )))
851 return USER_Driver.pShowWindow( full_handle, cmd );
852 return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
856 /***********************************************************************
857 * GetInternalWindowPos (USER32.@)
859 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
860 LPPOINT ptIcon )
862 WINDOWPLACEMENT wndpl;
863 if (GetWindowPlacement( hwnd, &wndpl ))
865 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
866 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
867 return wndpl.showCmd;
869 return 0;
873 /***********************************************************************
874 * GetWindowPlacement (USER32.@)
876 * Win95:
877 * Fails if wndpl->length of Win95 (!) apps is invalid.
879 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
881 WND *pWnd = WIN_FindWndPtr( hwnd );
882 LPINTERNALPOS lpPos;
884 if(!pWnd ) return FALSE;
886 lpPos = WINPOS_InitInternalPos( pWnd, *(LPPOINT)&pWnd->rectWindow.left, &pWnd->rectWindow );
887 wndpl->length = sizeof(*wndpl);
888 if( pWnd->dwStyle & WS_MINIMIZE )
889 wndpl->showCmd = SW_SHOWMINIMIZED;
890 else
891 wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
892 if( pWnd->flags & WIN_RESTORE_MAX )
893 wndpl->flags = WPF_RESTORETOMAXIMIZED;
894 else
895 wndpl->flags = 0;
896 CONV_POINT16TO32( &lpPos->ptIconPos, &wndpl->ptMinPosition );
897 CONV_POINT16TO32( &lpPos->ptMaxPos, &wndpl->ptMaxPosition );
898 CONV_RECT16TO32( &lpPos->rectNormal, &wndpl->rcNormalPosition );
899 WIN_ReleaseWndPtr(pWnd);
900 return TRUE;
904 /***********************************************************************
905 * WINPOS_SetPlacement
907 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
909 WND *pWnd = WIN_FindWndPtr( hwnd );
910 if( pWnd )
912 LPINTERNALPOS lpPos = (LPINTERNALPOS)WINPOS_InitInternalPos( pWnd,
913 *(LPPOINT)&pWnd->rectWindow.left, &pWnd->rectWindow );
915 if( flags & PLACE_MIN ) CONV_POINT32TO16( &wndpl->ptMinPosition, &lpPos->ptIconPos );
916 if( flags & PLACE_MAX ) CONV_POINT32TO16( &wndpl->ptMaxPosition, &lpPos->ptMaxPos );
917 if( flags & PLACE_RECT) CONV_RECT32TO16( &wndpl->rcNormalPosition, &lpPos->rectNormal );
919 if( pWnd->dwStyle & WS_MINIMIZE )
921 WINPOS_ShowIconTitle( pWnd->hwndSelf, FALSE );
922 if( wndpl->flags & WPF_SETMINPOSITION && !EMPTYPOINT(lpPos->ptIconPos))
923 SetWindowPos( hwnd, 0, lpPos->ptIconPos.x, lpPos->ptIconPos.y,
924 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
926 else if( pWnd->dwStyle & WS_MAXIMIZE )
928 if( !EMPTYPOINT(lpPos->ptMaxPos) )
929 SetWindowPos( hwnd, 0, lpPos->ptMaxPos.x, lpPos->ptMaxPos.y,
930 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
932 else if( flags & PLACE_RECT )
933 SetWindowPos( hwnd, 0, lpPos->rectNormal.left, lpPos->rectNormal.top,
934 lpPos->rectNormal.right - lpPos->rectNormal.left,
935 lpPos->rectNormal.bottom - lpPos->rectNormal.top,
936 SWP_NOZORDER | SWP_NOACTIVATE );
938 ShowWindow( hwnd, wndpl->showCmd );
939 if( IsWindow(hwnd) && pWnd->dwStyle & WS_MINIMIZE )
941 if( pWnd->dwStyle & WS_VISIBLE ) WINPOS_ShowIconTitle( pWnd->hwndSelf, TRUE );
943 /* SDK: ...valid only the next time... */
944 if( wndpl->flags & WPF_RESTORETOMAXIMIZED ) pWnd->flags |= WIN_RESTORE_MAX;
946 WIN_ReleaseWndPtr(pWnd);
947 return TRUE;
949 return FALSE;
953 /***********************************************************************
954 * SetWindowPlacement (USER32.@)
956 * Win95:
957 * Fails if wndpl->length of Win95 (!) apps is invalid.
959 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
961 if (!wpl) return FALSE;
962 return WINPOS_SetPlacement( hwnd, wpl, PLACE_MIN | PLACE_MAX | PLACE_RECT );
966 /***********************************************************************
967 * AnimateWindow (USER32.@)
968 * Shows/Hides a window with an animation
969 * NO ANIMATION YET
971 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
973 FIXME("partial stub\n");
975 /* If trying to show/hide and it's already *
976 * shown/hidden or invalid window, fail with *
977 * invalid parameter */
978 if(!IsWindow(hwnd) ||
979 (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
980 (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
982 SetLastError(ERROR_INVALID_PARAMETER);
983 return FALSE;
986 ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
988 return TRUE;
991 /***********************************************************************
992 * SetInternalWindowPos (USER32.@)
994 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
995 LPRECT rect, LPPOINT pt )
997 if( IsWindow(hwnd) )
999 WINDOWPLACEMENT wndpl;
1000 UINT flags;
1002 wndpl.length = sizeof(wndpl);
1003 wndpl.showCmd = showCmd;
1004 wndpl.flags = flags = 0;
1006 if( pt )
1008 flags |= PLACE_MIN;
1009 wndpl.flags |= WPF_SETMINPOSITION;
1010 wndpl.ptMinPosition = *pt;
1012 if( rect )
1014 flags |= PLACE_RECT;
1015 wndpl.rcNormalPosition = *rect;
1017 WINPOS_SetPlacement( hwnd, &wndpl, flags );
1022 /*******************************************************************
1023 * can_activate_window
1025 * Check if we can activate the specified window.
1027 static BOOL can_activate_window( HWND hwnd )
1029 LONG style;
1031 if (!hwnd) return FALSE;
1032 style = GetWindowLongW( hwnd, GWL_STYLE );
1033 if (!(style & WS_VISIBLE)) return FALSE;
1034 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1035 return !(style & WS_DISABLED);
1039 /*******************************************************************
1040 * WINPOS_ActivateOtherWindow
1042 * Activates window other than pWnd.
1044 void WINPOS_ActivateOtherWindow(HWND hwnd)
1046 HWND hwndTo, fg;
1048 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1050 hwndTo = GetAncestor( hwndTo, GA_ROOT );
1051 if (can_activate_window( hwndTo )) goto done;
1054 hwndTo = hwnd;
1055 for (;;)
1057 if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1058 if (can_activate_window( hwndTo )) break;
1061 done:
1062 fg = GetForegroundWindow();
1063 TRACE("win = %p fg = %p\n", hwndTo, fg);
1064 if (!fg || (hwnd == fg))
1066 if (SetForegroundWindow( hwndTo )) return;
1068 if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1072 /***********************************************************************
1073 * WINPOS_HandleWindowPosChanging16
1075 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1077 LONG WINPOS_HandleWindowPosChanging16( HWND hwnd, WINDOWPOS16 *winpos )
1079 POINT maxSize, minTrack;
1080 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
1082 if (winpos->flags & SWP_NOSIZE) return 0;
1083 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1085 WINPOS_GetMinMaxInfo( hwnd, &maxSize, NULL, &minTrack, NULL );
1086 if (maxSize.x < winpos->cx) winpos->cx = maxSize.x;
1087 if (maxSize.y < winpos->cy) winpos->cy = maxSize.y;
1088 if (!(style & WS_MINIMIZE))
1090 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1091 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1094 return 0;
1098 /***********************************************************************
1099 * WINPOS_HandleWindowPosChanging
1101 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1103 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1105 POINT maxSize, minTrack;
1106 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
1108 if (winpos->flags & SWP_NOSIZE) return 0;
1109 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1111 WINPOS_GetMinMaxInfo( hwnd, &maxSize, NULL, &minTrack, NULL );
1112 winpos->cx = min( winpos->cx, maxSize.x );
1113 winpos->cy = min( winpos->cy, maxSize.y );
1114 if (!(style & WS_MINIMIZE))
1116 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1117 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1120 return 0;
1124 /***********************************************************************
1125 * dump_winpos_flags
1127 static void dump_winpos_flags(UINT flags)
1129 TRACE("flags:");
1130 if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1131 if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1132 if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1133 if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1134 if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1135 if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1136 if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1137 if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1138 if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1139 if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1140 if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1141 if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1142 if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1144 #define DUMPED_FLAGS \
1145 (SWP_NOSIZE | \
1146 SWP_NOMOVE | \
1147 SWP_NOZORDER | \
1148 SWP_NOREDRAW | \
1149 SWP_NOACTIVATE | \
1150 SWP_FRAMECHANGED | \
1151 SWP_SHOWWINDOW | \
1152 SWP_HIDEWINDOW | \
1153 SWP_NOCOPYBITS | \
1154 SWP_NOOWNERZORDER | \
1155 SWP_NOSENDCHANGING | \
1156 SWP_DEFERERASE | \
1157 SWP_ASYNCWINDOWPOS)
1159 if(flags & ~DUMPED_FLAGS) TRACE(" %08x", flags & ~DUMPED_FLAGS);
1160 TRACE("\n");
1161 #undef DUMPED_FLAGS
1164 /***********************************************************************
1165 * SetWindowPos (USER32.@)
1167 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
1168 INT x, INT y, INT cx, INT cy, UINT flags )
1170 WINDOWPOS winpos;
1172 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1173 hwnd, hwndInsertAfter, x, y, cx, cy, flags);
1174 if(TRACE_ON(win)) dump_winpos_flags(flags);
1176 winpos.hwnd = WIN_GetFullHandle(hwnd);
1177 winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
1178 winpos.x = x;
1179 winpos.y = y;
1180 winpos.cx = cx;
1181 winpos.cy = cy;
1182 winpos.flags = flags;
1183 if (WIN_IsCurrentThread( hwnd )) return USER_Driver.pSetWindowPos( &winpos );
1184 return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
1188 /***********************************************************************
1189 * BeginDeferWindowPos (USER32.@)
1191 HDWP WINAPI BeginDeferWindowPos( INT count )
1193 HDWP handle;
1194 DWP *pDWP;
1196 if (count < 0)
1198 SetLastError(ERROR_INVALID_PARAMETER);
1199 return 0;
1201 /* Windows allows zero count, in which case it allocates context for 8 moves */
1202 if (count == 0) count = 8;
1204 handle = USER_HEAP_ALLOC( sizeof(DWP) + (count-1)*sizeof(WINDOWPOS) );
1205 if (!handle) return 0;
1206 pDWP = (DWP *) USER_HEAP_LIN_ADDR( handle );
1207 pDWP->actualCount = 0;
1208 pDWP->suggestedCount = count;
1209 pDWP->valid = TRUE;
1210 pDWP->wMagic = DWP_MAGIC;
1211 pDWP->hwndParent = 0;
1212 return handle;
1216 /***********************************************************************
1217 * DeferWindowPos (USER32.@)
1219 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
1220 INT x, INT y, INT cx, INT cy,
1221 UINT flags )
1223 DWP *pDWP;
1224 int i;
1225 HDWP newhdwp = hdwp,retvalue;
1227 hwnd = WIN_GetFullHandle( hwnd );
1228 if (hwnd == GetDesktopWindow()) return 0;
1230 if (!(pDWP = USER_HEAP_LIN_ADDR( hdwp ))) return 0;
1232 USER_Lock();
1234 for (i = 0; i < pDWP->actualCount; i++)
1236 if (pDWP->winPos[i].hwnd == hwnd)
1238 /* Merge with the other changes */
1239 if (!(flags & SWP_NOZORDER))
1241 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
1243 if (!(flags & SWP_NOMOVE))
1245 pDWP->winPos[i].x = x;
1246 pDWP->winPos[i].y = y;
1248 if (!(flags & SWP_NOSIZE))
1250 pDWP->winPos[i].cx = cx;
1251 pDWP->winPos[i].cy = cy;
1253 pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
1254 SWP_NOZORDER | SWP_NOREDRAW |
1255 SWP_NOACTIVATE | SWP_NOCOPYBITS|
1256 SWP_NOOWNERZORDER);
1257 pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
1258 SWP_FRAMECHANGED);
1259 retvalue = hdwp;
1260 goto END;
1263 if (pDWP->actualCount >= pDWP->suggestedCount)
1265 newhdwp = USER_HEAP_REALLOC( hdwp,
1266 sizeof(DWP) + pDWP->suggestedCount*sizeof(WINDOWPOS) );
1267 if (!newhdwp)
1269 retvalue = 0;
1270 goto END;
1272 pDWP = (DWP *) USER_HEAP_LIN_ADDR( newhdwp );
1273 pDWP->suggestedCount++;
1275 pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
1276 pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
1277 pDWP->winPos[pDWP->actualCount].x = x;
1278 pDWP->winPos[pDWP->actualCount].y = y;
1279 pDWP->winPos[pDWP->actualCount].cx = cx;
1280 pDWP->winPos[pDWP->actualCount].cy = cy;
1281 pDWP->winPos[pDWP->actualCount].flags = flags;
1282 pDWP->actualCount++;
1283 retvalue = newhdwp;
1284 END:
1285 USER_Unlock();
1286 return retvalue;
1290 /***********************************************************************
1291 * EndDeferWindowPos (USER32.@)
1293 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
1295 DWP *pDWP;
1296 WINDOWPOS *winpos;
1297 BOOL res = TRUE;
1298 int i;
1300 pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
1301 if (!pDWP) return FALSE;
1302 for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
1304 if (!(res = USER_Driver.pSetWindowPos( winpos ))) break;
1306 USER_HEAP_FREE( hdwp );
1307 return res;
1311 /***********************************************************************
1312 * TileChildWindows (USER.199)
1314 void WINAPI TileChildWindows16( HWND16 parent, WORD action )
1316 FIXME("(%04x, %d): stub\n", parent, action);
1319 /***********************************************************************
1320 * CascadeChildWindows (USER.198)
1322 void WINAPI CascadeChildWindows16( HWND16 parent, WORD action )
1324 FIXME("(%04x, %d): stub\n", parent, action);
1327 /***********************************************************************
1328 * SetProgmanWindow (USER32.@)
1330 HWND WINAPI SetProgmanWindow ( HWND hwnd )
1332 hGlobalProgmanWindow = hwnd;
1333 return hGlobalProgmanWindow;
1336 /***********************************************************************
1337 * GetProgmanWindow (USER32.@)
1339 HWND WINAPI GetProgmanWindow(void)
1341 return hGlobalProgmanWindow;
1344 /***********************************************************************
1345 * SetShellWindowEx (USER32.@)
1346 * hwndProgman = Progman[Program Manager]
1347 * |-> SHELLDLL_DefView
1348 * hwndListView = | |-> SysListView32
1349 * | | |-> tooltips_class32
1350 * | |
1351 * | |-> SysHeader32
1353 * |-> ProxyTarget
1355 HWND WINAPI SetShellWindowEx ( HWND hwndProgman, HWND hwndListView )
1357 FIXME("%p %p stub\n",hwndProgman ,hwndListView );
1358 hGlobalShellWindow = hwndProgman;
1359 return hGlobalShellWindow;
1363 /***********************************************************************
1364 * SetTaskmanWindow (USER32.@)
1365 * NOTES
1366 * hwnd = MSTaskSwWClass
1367 * |-> SysTabControl32
1369 HWND WINAPI SetTaskmanWindow ( HWND hwnd )
1371 hGlobalTaskmanWindow = hwnd;
1372 return hGlobalTaskmanWindow;
1375 /***********************************************************************
1376 * GetTaskmanWindow (USER32.@)
1378 HWND WINAPI GetTaskmanWindow(void)
1380 return hGlobalTaskmanWindow;