Add support for impersonating a token.
[wine/multimedia.git] / dlls / user / winpos.c
blob3dd63cd828e0f61874fcc3e101b183e45bd636e6
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 "winpos.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(win);
43 #define HAS_DLGFRAME(style,exStyle) \
44 (((exStyle) & WS_EX_DLGMODALFRAME) || \
45 (((style) & WS_DLGFRAME) && !((style) & WS_BORDER)))
47 #define HAS_THICKFRAME(style) \
48 (((style) & WS_THICKFRAME) && \
49 !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
51 #define EMPTYPOINT(pt) ((*(LONG*)&(pt)) == -1)
53 #define PLACE_MIN 0x0001
54 #define PLACE_MAX 0x0002
55 #define PLACE_RECT 0x0004
58 #define DWP_MAGIC ((INT)('W' | ('P' << 8) | ('O' << 16) | ('S' << 24)))
60 typedef struct
62 INT actualCount;
63 INT suggestedCount;
64 BOOL valid;
65 INT wMagic;
66 HWND hwndParent;
67 WINDOWPOS winPos[1];
68 } DWP;
70 typedef struct
72 RECT16 rectNormal;
73 POINT16 ptIconPos;
74 POINT16 ptMaxPos;
75 HWND hwndIconTitle;
76 } INTERNALPOS, *LPINTERNALPOS;
78 /* ----- internal variables ----- */
80 static LPCSTR atomInternalPos;
83 /***********************************************************************
84 * WINPOS_CreateInternalPosAtom
86 BOOL WINPOS_CreateInternalPosAtom()
88 LPCSTR str = "SysIP";
89 atomInternalPos = (LPCSTR)(DWORD)GlobalAddAtomA(str);
90 return (atomInternalPos) ? TRUE : FALSE;
93 /***********************************************************************
94 * WINPOS_CheckInternalPos
96 * Called when a window is destroyed.
98 void WINPOS_CheckInternalPos( HWND hwnd )
100 LPINTERNALPOS lpPos = (LPINTERNALPOS) GetPropA( hwnd, atomInternalPos );
102 if( lpPos )
104 if( IsWindow(lpPos->hwndIconTitle) )
105 DestroyWindow( lpPos->hwndIconTitle );
106 HeapFree( GetProcessHeap(), 0, lpPos );
110 /***********************************************************************
111 * ArrangeIconicWindows (USER32.@)
113 UINT WINAPI ArrangeIconicWindows( HWND parent )
115 RECT rectParent;
116 HWND hwndChild;
117 INT x, y, xspacing, yspacing;
119 GetClientRect( parent, &rectParent );
120 x = rectParent.left;
121 y = rectParent.bottom;
122 xspacing = GetSystemMetrics(SM_CXICONSPACING);
123 yspacing = GetSystemMetrics(SM_CYICONSPACING);
125 hwndChild = GetWindow( parent, GW_CHILD );
126 while (hwndChild)
128 if( IsIconic( hwndChild ) )
130 WINPOS_ShowIconTitle( hwndChild, FALSE );
132 SetWindowPos( hwndChild, 0, x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
133 y - yspacing - GetSystemMetrics(SM_CYICON)/2, 0, 0,
134 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
135 if( IsWindow(hwndChild) )
136 WINPOS_ShowIconTitle(hwndChild , TRUE );
138 if (x <= rectParent.right - xspacing) x += xspacing;
139 else
141 x = rectParent.left;
142 y -= yspacing;
145 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
147 return yspacing;
151 /***********************************************************************
152 * SwitchToThisWindow (USER32.@)
154 void WINAPI SwitchToThisWindow( HWND hwnd, BOOL restore )
156 ShowWindow( hwnd, restore ? SW_RESTORE : SW_SHOWMINIMIZED );
160 /***********************************************************************
161 * GetWindowRect (USER32.@)
163 BOOL WINAPI GetWindowRect( HWND hwnd, LPRECT rect )
165 BOOL ret = WIN_GetRectangles( hwnd, rect, NULL );
166 if (ret)
168 MapWindowPoints( GetAncestor( hwnd, GA_PARENT ), 0, (POINT *)rect, 2 );
169 TRACE( "hwnd %p (%ld,%ld)-(%ld,%ld)\n",
170 hwnd, rect->left, rect->top, rect->right, rect->bottom);
172 return ret;
176 /***********************************************************************
177 * GetWindowRgn (USER32.@)
179 int WINAPI GetWindowRgn ( HWND hwnd, HRGN hrgn )
181 int nRet = ERROR;
182 NTSTATUS status;
183 HRGN win_rgn = 0;
184 RGNDATA *data;
185 size_t size = 256;
189 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
191 SetLastError( ERROR_OUTOFMEMORY );
192 return ERROR;
194 SERVER_START_REQ( get_window_region )
196 req->window = hwnd;
197 wine_server_set_reply( req, data->Buffer, size );
198 if (!(status = wine_server_call( req )))
200 size_t reply_size = wine_server_reply_size( reply );
201 if (reply_size)
203 data->rdh.dwSize = sizeof(data->rdh);
204 data->rdh.iType = RDH_RECTANGLES;
205 data->rdh.nCount = reply_size / sizeof(RECT);
206 data->rdh.nRgnSize = reply_size;
207 win_rgn = ExtCreateRegion( NULL, size, data );
210 else size = reply->total_size;
212 SERVER_END_REQ;
213 HeapFree( GetProcessHeap(), 0, data );
214 } while (status == STATUS_BUFFER_OVERFLOW);
216 if (status) SetLastError( RtlNtStatusToDosError(status) );
217 else if (win_rgn)
219 nRet = CombineRgn( hrgn, win_rgn, 0, RGN_COPY );
220 DeleteObject( win_rgn );
222 return nRet;
226 /***********************************************************************
227 * SetWindowRgn (USER32.@)
229 int WINAPI SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL bRedraw )
231 static const RECT empty_rect;
232 BOOL ret;
234 if (hrgn)
236 RGNDATA *data;
237 DWORD size;
239 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
240 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
241 if (!GetRegionData( hrgn, size, data ))
243 HeapFree( GetProcessHeap(), 0, data );
244 return FALSE;
246 SERVER_START_REQ( set_window_region )
248 req->window = hwnd;
249 if (data->rdh.nCount)
250 wine_server_add_data( req, data->Buffer, data->rdh.nCount * sizeof(RECT) );
251 else
252 wine_server_add_data( req, &empty_rect, sizeof(empty_rect) );
253 ret = !wine_server_call_err( req );
255 SERVER_END_REQ;
257 else /* clear existing region */
259 SERVER_START_REQ( set_window_region )
261 req->window = hwnd;
262 ret = !wine_server_call_err( req );
264 SERVER_END_REQ;
267 if (ret && USER_Driver.pSetWindowRgn)
268 ret = USER_Driver.pSetWindowRgn( hwnd, hrgn, bRedraw );
270 if (ret && bRedraw) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_INVALIDATE | RDW_ERASE );
271 return ret;
275 /***********************************************************************
276 * GetClientRect (USER32.@)
278 BOOL WINAPI GetClientRect( HWND hwnd, LPRECT rect )
280 BOOL ret;
282 if ((ret = WIN_GetRectangles( hwnd, NULL, rect )))
284 rect->right -= rect->left;
285 rect->bottom -= rect->top;
286 rect->left = rect->top = 0;
288 return ret;
292 /*******************************************************************
293 * ClientToScreen (USER32.@)
295 BOOL WINAPI ClientToScreen( HWND hwnd, LPPOINT lppnt )
297 MapWindowPoints( hwnd, 0, lppnt, 1 );
298 return TRUE;
302 /*******************************************************************
303 * ScreenToClient (USER32.@)
305 BOOL WINAPI ScreenToClient( HWND hwnd, LPPOINT lppnt )
307 MapWindowPoints( 0, hwnd, lppnt, 1 );
308 return TRUE;
312 /***********************************************************************
313 * list_children_from_point
315 * Get the list of children that can contain point from the server.
316 * Point is in screen coordinates.
317 * Returned list must be freed by caller.
319 static HWND *list_children_from_point( HWND hwnd, POINT pt )
321 HWND *list;
322 int size = 32;
324 for (;;)
326 int count = 0;
328 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
330 SERVER_START_REQ( get_window_children_from_point )
332 req->parent = hwnd;
333 req->x = pt.x;
334 req->y = pt.y;
335 wine_server_set_reply( req, list, (size-1) * sizeof(HWND) );
336 if (!wine_server_call( req )) count = reply->count;
338 SERVER_END_REQ;
339 if (count && count < size)
341 list[count] = 0;
342 return list;
344 HeapFree( GetProcessHeap(), 0, list );
345 if (!count) break;
346 size = count + 1; /* restart with a large enough buffer */
348 return NULL;
352 /***********************************************************************
353 * WINPOS_WindowFromPoint
355 * Find the window and hittest for a given point.
357 HWND WINPOS_WindowFromPoint( HWND hwndScope, POINT pt, INT *hittest )
359 int i, res;
360 HWND ret, *list;
362 if (!hwndScope) hwndScope = GetDesktopWindow();
364 *hittest = HTNOWHERE;
366 if (!(list = list_children_from_point( hwndScope, pt ))) return 0;
368 /* now determine the hittest */
370 for (i = 0; list[i]; i++)
372 LONG style = GetWindowLongW( list[i], GWL_STYLE );
374 /* If window is minimized or disabled, return at once */
375 if (style & WS_MINIMIZE)
377 *hittest = HTCAPTION;
378 break;
380 if (style & WS_DISABLED)
382 *hittest = HTERROR;
383 break;
385 /* Send WM_NCCHITTEST (if same thread) */
386 if (!WIN_IsCurrentThread( list[i] ))
388 *hittest = HTCLIENT;
389 break;
391 res = SendMessageA( list[i], WM_NCHITTEST, 0, MAKELONG(pt.x,pt.y) );
392 if (res != HTTRANSPARENT)
394 *hittest = res; /* Found the window */
395 break;
397 /* continue search with next window in z-order */
399 ret = list[i];
400 HeapFree( GetProcessHeap(), 0, list );
401 TRACE( "scope %p (%ld,%ld) returning %p\n", hwndScope, pt.x, pt.y, ret );
402 return ret;
406 /*******************************************************************
407 * WindowFromPoint (USER32.@)
409 HWND WINAPI WindowFromPoint( POINT pt )
411 INT hittest;
412 return WINPOS_WindowFromPoint( 0, pt, &hittest );
416 /*******************************************************************
417 * ChildWindowFromPoint (USER32.@)
419 HWND WINAPI ChildWindowFromPoint( HWND hwndParent, POINT pt )
421 return ChildWindowFromPointEx( hwndParent, pt, CWP_ALL );
424 /*******************************************************************
425 * ChildWindowFromPointEx (USER32.@)
427 HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
429 /* pt is in the client coordinates */
430 HWND *list;
431 int i;
432 RECT rect;
433 HWND retvalue;
435 GetClientRect( hwndParent, &rect );
436 if (!PtInRect( &rect, pt )) return 0;
437 if (!(list = WIN_ListChildren( hwndParent ))) return hwndParent;
439 for (i = 0; list[i]; i++)
441 if (!WIN_GetRectangles( list[i], &rect, NULL )) continue;
442 if (!PtInRect( &rect, pt )) continue;
443 if (uFlags & (CWP_SKIPINVISIBLE|CWP_SKIPDISABLED))
445 LONG style = GetWindowLongW( list[i], GWL_STYLE );
446 if ((uFlags & CWP_SKIPINVISIBLE) && !(style & WS_VISIBLE)) continue;
447 if ((uFlags & CWP_SKIPDISABLED) && (style & WS_DISABLED)) continue;
449 if (uFlags & CWP_SKIPTRANSPARENT)
451 if (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TRANSPARENT) continue;
453 break;
455 retvalue = list[i];
456 HeapFree( GetProcessHeap(), 0, list );
457 if (!retvalue) retvalue = hwndParent;
458 return retvalue;
462 /*******************************************************************
463 * WINPOS_GetWinOffset
465 * Calculate the offset between the origin of the two windows. Used
466 * to implement MapWindowPoints.
468 static void WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo, POINT *offset )
470 WND * wndPtr;
472 offset->x = offset->y = 0;
474 /* Translate source window origin to screen coords */
475 if (hwndFrom)
477 HWND hwnd = hwndFrom;
479 while (hwnd)
481 if (hwnd == hwndTo) return;
482 if (!(wndPtr = WIN_GetPtr( hwnd )))
484 ERR( "bad hwndFrom = %p\n", hwnd );
485 return;
487 if (wndPtr == WND_DESKTOP) break;
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)
503 if (!(wndPtr = WIN_GetPtr( hwnd )))
505 ERR( "bad hwndTo = %p\n", hwnd );
506 return;
508 if (wndPtr == WND_DESKTOP) break;
509 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
510 offset->x -= wndPtr->rectClient.left;
511 offset->y -= wndPtr->rectClient.top;
512 hwnd = wndPtr->parent;
513 WIN_ReleasePtr( wndPtr );
516 return;
518 other_process: /* one of the parents may belong to another process, do it the hard way */
519 offset->x = offset->y = 0;
520 SERVER_START_REQ( get_windows_offset )
522 req->from = hwndFrom;
523 req->to = hwndTo;
524 if (!wine_server_call( req ))
526 offset->x = reply->x;
527 offset->y = reply->y;
530 SERVER_END_REQ;
534 /*******************************************************************
535 * MapWindowPoints (USER.258)
537 void WINAPI MapWindowPoints16( HWND16 hwndFrom, HWND16 hwndTo,
538 LPPOINT16 lppt, UINT16 count )
540 POINT offset;
542 WINPOS_GetWinOffset( WIN_Handle32(hwndFrom), WIN_Handle32(hwndTo), &offset );
543 while (count--)
545 lppt->x += offset.x;
546 lppt->y += offset.y;
547 lppt++;
552 /*******************************************************************
553 * MapWindowPoints (USER32.@)
555 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
557 POINT offset;
559 WINPOS_GetWinOffset( hwndFrom, hwndTo, &offset );
560 while (count--)
562 lppt->x += offset.x;
563 lppt->y += offset.y;
564 lppt++;
566 return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
570 /***********************************************************************
571 * IsIconic (USER32.@)
573 BOOL WINAPI IsIconic(HWND hWnd)
575 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
579 /***********************************************************************
580 * IsZoomed (USER32.@)
582 BOOL WINAPI IsZoomed(HWND hWnd)
584 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
588 /*******************************************************************
589 * AllowSetForegroundWindow (USER32.@)
591 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
593 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
594 * implemented, then fix this function. */
595 return TRUE;
599 /*******************************************************************
600 * LockSetForegroundWindow (USER32.@)
602 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
604 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
605 * implemented, then fix this function. */
606 return TRUE;
610 /***********************************************************************
611 * BringWindowToTop (USER32.@)
613 BOOL WINAPI BringWindowToTop( HWND hwnd )
615 return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
619 /***********************************************************************
620 * MoveWindow (USER32.@)
622 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
623 BOOL repaint )
625 int flags = SWP_NOZORDER | SWP_NOACTIVATE;
626 if (!repaint) flags |= SWP_NOREDRAW;
627 TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
628 return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
631 /***********************************************************************
632 * WINPOS_InitInternalPos
634 static LPINTERNALPOS WINPOS_InitInternalPos( WND* wnd )
636 LPINTERNALPOS lpPos = GetPropA( wnd->hwndSelf, 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 = wnd->rectWindow.left;
658 lpPos->ptIconPos.y = wnd->rectWindow.top;
660 else if( wnd->dwStyle & WS_MAXIMIZE )
662 lpPos->ptMaxPos.x = wnd->rectWindow.left;
663 lpPos->ptMaxPos.y = wnd->rectWindow.top;
665 else
667 lpPos->rectNormal.left = wnd->rectWindow.left;
668 lpPos->rectNormal.top = wnd->rectWindow.top;
669 lpPos->rectNormal.right = wnd->rectWindow.right;
670 lpPos->rectNormal.bottom = wnd->rectWindow.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 && !GetPropA( hwnd, "__wine_x11_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(GetAncestor(hwnd,GA_PARENT), &rc);
749 AdjustWindowRectEx(&rc, style, ((style & WS_POPUP) && GetMenu(hwnd)), 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) + 2*GetSystemMetrics(SM_CXFRAME);
765 MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYSCREEN) + 2*GetSystemMetrics(SM_CYFRAME);
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_GetPtr( hwnd );
895 LPINTERNALPOS lpPos;
897 if (!pWnd || pWnd == WND_DESKTOP) return FALSE;
898 if (pWnd == WND_OTHER_PROCESS)
900 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
901 return FALSE;
904 lpPos = WINPOS_InitInternalPos( pWnd );
905 wndpl->length = sizeof(*wndpl);
906 if( pWnd->dwStyle & WS_MINIMIZE )
907 wndpl->showCmd = SW_SHOWMINIMIZED;
908 else
909 wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
910 if( pWnd->flags & WIN_RESTORE_MAX )
911 wndpl->flags = WPF_RESTORETOMAXIMIZED;
912 else
913 wndpl->flags = 0;
914 wndpl->ptMinPosition.x = lpPos->ptIconPos.x;
915 wndpl->ptMinPosition.y = lpPos->ptIconPos.y;
916 wndpl->ptMaxPosition.x = lpPos->ptMaxPos.x;
917 wndpl->ptMaxPosition.y = lpPos->ptMaxPos.y;
918 wndpl->rcNormalPosition.left = lpPos->rectNormal.left;
919 wndpl->rcNormalPosition.top = lpPos->rectNormal.top;
920 wndpl->rcNormalPosition.right = lpPos->rectNormal.right;
921 wndpl->rcNormalPosition.bottom = lpPos->rectNormal.bottom;
922 WIN_ReleasePtr( pWnd );
923 return TRUE;
927 /***********************************************************************
928 * WINPOS_SetPlacement
930 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
932 LPINTERNALPOS lpPos;
933 DWORD style;
934 WND *pWnd = WIN_GetPtr( hwnd );
936 if (!pWnd || pWnd == WND_OTHER_PROCESS || pWnd == WND_DESKTOP) return FALSE;
937 lpPos = WINPOS_InitInternalPos( pWnd );
939 if( flags & PLACE_MIN )
941 lpPos->ptIconPos.x = wndpl->ptMinPosition.x;
942 lpPos->ptIconPos.y = wndpl->ptMinPosition.y;
944 if( flags & PLACE_MAX )
946 lpPos->ptMaxPos.x = wndpl->ptMaxPosition.x;
947 lpPos->ptMaxPos.y = wndpl->ptMaxPosition.y;
949 if( flags & PLACE_RECT)
951 lpPos->rectNormal.left = wndpl->rcNormalPosition.left;
952 lpPos->rectNormal.top = wndpl->rcNormalPosition.top;
953 lpPos->rectNormal.right = wndpl->rcNormalPosition.right;
954 lpPos->rectNormal.bottom = wndpl->rcNormalPosition.bottom;
957 style = pWnd->dwStyle;
958 WIN_ReleasePtr( pWnd );
960 if( style & WS_MINIMIZE )
962 WINPOS_ShowIconTitle( hwnd, FALSE );
963 if( wndpl->flags & WPF_SETMINPOSITION && !EMPTYPOINT(lpPos->ptIconPos))
964 SetWindowPos( hwnd, 0, lpPos->ptIconPos.x, lpPos->ptIconPos.y,
965 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
967 else if( style & WS_MAXIMIZE )
969 if( !EMPTYPOINT(lpPos->ptMaxPos) )
970 SetWindowPos( hwnd, 0, lpPos->ptMaxPos.x, lpPos->ptMaxPos.y,
971 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
973 else if( flags & PLACE_RECT )
974 SetWindowPos( hwnd, 0, lpPos->rectNormal.left, lpPos->rectNormal.top,
975 lpPos->rectNormal.right - lpPos->rectNormal.left,
976 lpPos->rectNormal.bottom - lpPos->rectNormal.top,
977 SWP_NOZORDER | SWP_NOACTIVATE );
979 ShowWindow( hwnd, wndpl->showCmd );
981 if (IsIconic( hwnd ))
983 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE) WINPOS_ShowIconTitle( hwnd, TRUE );
985 /* SDK: ...valid only the next time... */
986 if( wndpl->flags & WPF_RESTORETOMAXIMIZED )
988 pWnd = WIN_GetPtr( hwnd );
989 if (pWnd && pWnd != WND_OTHER_PROCESS)
991 pWnd->flags |= WIN_RESTORE_MAX;
992 WIN_ReleasePtr( pWnd );
996 return TRUE;
1000 /***********************************************************************
1001 * SetWindowPlacement (USER32.@)
1003 * Win95:
1004 * Fails if wndpl->length of Win95 (!) apps is invalid.
1006 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
1008 if (!wpl) return FALSE;
1009 return WINPOS_SetPlacement( hwnd, wpl, PLACE_MIN | PLACE_MAX | PLACE_RECT );
1013 /***********************************************************************
1014 * AnimateWindow (USER32.@)
1015 * Shows/Hides a window with an animation
1016 * NO ANIMATION YET
1018 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
1020 FIXME("partial stub\n");
1022 /* If trying to show/hide and it's already *
1023 * shown/hidden or invalid window, fail with *
1024 * invalid parameter */
1025 if(!IsWindow(hwnd) ||
1026 (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
1027 (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
1029 SetLastError(ERROR_INVALID_PARAMETER);
1030 return FALSE;
1033 ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1035 return TRUE;
1038 /***********************************************************************
1039 * SetInternalWindowPos (USER32.@)
1041 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1042 LPRECT rect, LPPOINT pt )
1044 if( IsWindow(hwnd) )
1046 WINDOWPLACEMENT wndpl;
1047 UINT flags;
1049 wndpl.length = sizeof(wndpl);
1050 wndpl.showCmd = showCmd;
1051 wndpl.flags = flags = 0;
1053 if( pt )
1055 flags |= PLACE_MIN;
1056 wndpl.flags |= WPF_SETMINPOSITION;
1057 wndpl.ptMinPosition = *pt;
1059 if( rect )
1061 flags |= PLACE_RECT;
1062 wndpl.rcNormalPosition = *rect;
1064 WINPOS_SetPlacement( hwnd, &wndpl, flags );
1069 /*******************************************************************
1070 * can_activate_window
1072 * Check if we can activate the specified window.
1074 static BOOL can_activate_window( HWND hwnd )
1076 LONG style;
1078 if (!hwnd) return FALSE;
1079 style = GetWindowLongW( hwnd, GWL_STYLE );
1080 if (!(style & WS_VISIBLE)) return FALSE;
1081 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1082 return !(style & WS_DISABLED);
1086 /*******************************************************************
1087 * WINPOS_ActivateOtherWindow
1089 * Activates window other than pWnd.
1091 void WINPOS_ActivateOtherWindow(HWND hwnd)
1093 HWND hwndTo, fg;
1095 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1097 hwndTo = GetAncestor( hwndTo, GA_ROOT );
1098 if (can_activate_window( hwndTo )) goto done;
1101 hwndTo = hwnd;
1102 for (;;)
1104 if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1105 if (can_activate_window( hwndTo )) break;
1108 done:
1109 fg = GetForegroundWindow();
1110 TRACE("win = %p fg = %p\n", hwndTo, fg);
1111 if (!fg || (hwnd == fg))
1113 if (SetForegroundWindow( hwndTo )) return;
1115 if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1119 /***********************************************************************
1120 * WINPOS_HandleWindowPosChanging
1122 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1124 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1126 POINT minTrack, maxTrack;
1127 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1129 if (winpos->flags & SWP_NOSIZE) return 0;
1130 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1132 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1133 if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1134 if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1135 if (!(style & WS_MINIMIZE))
1137 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1138 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1141 return 0;
1145 /***********************************************************************
1146 * dump_winpos_flags
1148 static void dump_winpos_flags(UINT flags)
1150 TRACE("flags:");
1151 if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1152 if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1153 if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1154 if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1155 if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1156 if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1157 if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1158 if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1159 if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1160 if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1161 if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1162 if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1163 if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1165 #define DUMPED_FLAGS \
1166 (SWP_NOSIZE | \
1167 SWP_NOMOVE | \
1168 SWP_NOZORDER | \
1169 SWP_NOREDRAW | \
1170 SWP_NOACTIVATE | \
1171 SWP_FRAMECHANGED | \
1172 SWP_SHOWWINDOW | \
1173 SWP_HIDEWINDOW | \
1174 SWP_NOCOPYBITS | \
1175 SWP_NOOWNERZORDER | \
1176 SWP_NOSENDCHANGING | \
1177 SWP_DEFERERASE | \
1178 SWP_ASYNCWINDOWPOS)
1180 if(flags & ~DUMPED_FLAGS) TRACE(" %08x", flags & ~DUMPED_FLAGS);
1181 TRACE("\n");
1182 #undef DUMPED_FLAGS
1185 /***********************************************************************
1186 * SetWindowPos (USER32.@)
1188 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
1189 INT x, INT y, INT cx, INT cy, UINT flags )
1191 WINDOWPOS winpos;
1193 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1194 hwnd, hwndInsertAfter, x, y, cx, cy, flags);
1195 if(TRACE_ON(win)) dump_winpos_flags(flags);
1197 if (is_broadcast(hwnd))
1199 SetLastError( ERROR_INVALID_PARAMETER );
1200 return FALSE;
1203 winpos.hwnd = WIN_GetFullHandle(hwnd);
1204 winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
1205 winpos.x = x;
1206 winpos.y = y;
1207 winpos.cx = cx;
1208 winpos.cy = cy;
1209 winpos.flags = flags;
1210 if (WIN_IsCurrentThread( hwnd ))
1212 if (USER_Driver.pSetWindowPos)
1213 return USER_Driver.pSetWindowPos( &winpos );
1214 return FALSE;
1216 return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
1220 /***********************************************************************
1221 * BeginDeferWindowPos (USER32.@)
1223 HDWP WINAPI BeginDeferWindowPos( INT count )
1225 HDWP handle;
1226 DWP *pDWP;
1228 TRACE("%d\n", count);
1230 if (count < 0)
1232 SetLastError(ERROR_INVALID_PARAMETER);
1233 return 0;
1235 /* Windows allows zero count, in which case it allocates context for 8 moves */
1236 if (count == 0) count = 8;
1238 handle = USER_HEAP_ALLOC( sizeof(DWP) + (count-1)*sizeof(WINDOWPOS) );
1239 if (!handle) return 0;
1240 pDWP = (DWP *) USER_HEAP_LIN_ADDR( handle );
1241 pDWP->actualCount = 0;
1242 pDWP->suggestedCount = count;
1243 pDWP->valid = TRUE;
1244 pDWP->wMagic = DWP_MAGIC;
1245 pDWP->hwndParent = 0;
1247 TRACE("returning hdwp %p\n", handle);
1248 return handle;
1252 /***********************************************************************
1253 * DeferWindowPos (USER32.@)
1255 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
1256 INT x, INT y, INT cx, INT cy,
1257 UINT flags )
1259 DWP *pDWP;
1260 int i;
1261 HDWP newhdwp = hdwp,retvalue;
1263 TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1264 hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
1266 hwnd = WIN_GetFullHandle( hwnd );
1267 if (hwnd == GetDesktopWindow()) return 0;
1269 if (!(pDWP = USER_HEAP_LIN_ADDR( hdwp ))) return 0;
1271 USER_Lock();
1273 for (i = 0; i < pDWP->actualCount; i++)
1275 if (pDWP->winPos[i].hwnd == hwnd)
1277 /* Merge with the other changes */
1278 if (!(flags & SWP_NOZORDER))
1280 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
1282 if (!(flags & SWP_NOMOVE))
1284 pDWP->winPos[i].x = x;
1285 pDWP->winPos[i].y = y;
1287 if (!(flags & SWP_NOSIZE))
1289 pDWP->winPos[i].cx = cx;
1290 pDWP->winPos[i].cy = cy;
1292 pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
1293 SWP_NOZORDER | SWP_NOREDRAW |
1294 SWP_NOACTIVATE | SWP_NOCOPYBITS|
1295 SWP_NOOWNERZORDER);
1296 pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
1297 SWP_FRAMECHANGED);
1298 retvalue = hdwp;
1299 goto END;
1302 if (pDWP->actualCount >= pDWP->suggestedCount)
1304 newhdwp = USER_HEAP_REALLOC( hdwp,
1305 sizeof(DWP) + pDWP->suggestedCount*sizeof(WINDOWPOS) );
1306 if (!newhdwp)
1308 retvalue = 0;
1309 goto END;
1311 pDWP = (DWP *) USER_HEAP_LIN_ADDR( newhdwp );
1312 pDWP->suggestedCount++;
1314 pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
1315 pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
1316 pDWP->winPos[pDWP->actualCount].x = x;
1317 pDWP->winPos[pDWP->actualCount].y = y;
1318 pDWP->winPos[pDWP->actualCount].cx = cx;
1319 pDWP->winPos[pDWP->actualCount].cy = cy;
1320 pDWP->winPos[pDWP->actualCount].flags = flags;
1321 pDWP->actualCount++;
1322 retvalue = newhdwp;
1323 END:
1324 USER_Unlock();
1325 return retvalue;
1329 /***********************************************************************
1330 * EndDeferWindowPos (USER32.@)
1332 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
1334 DWP *pDWP;
1335 WINDOWPOS *winpos;
1336 BOOL res = TRUE;
1337 int i;
1339 TRACE("%p\n", hdwp);
1341 pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
1342 if (!pDWP) return FALSE;
1343 for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
1345 if (!USER_Driver.pSetWindowPos || !(res = USER_Driver.pSetWindowPos( winpos ))) break;
1347 USER_HEAP_FREE( hdwp );
1348 return res;
1352 /***********************************************************************
1353 * TileChildWindows (USER.199)
1355 void WINAPI TileChildWindows16( HWND16 parent, WORD action )
1357 FIXME("(%04x, %d): stub\n", parent, action);
1360 /***********************************************************************
1361 * CascadeChildWindows (USER.198)
1363 void WINAPI CascadeChildWindows16( HWND16 parent, WORD action )
1365 FIXME("(%04x, %d): stub\n", parent, action);