kernel: Check for an exe which is always present in a system dir.
[wine.git] / dlls / user / winpos.c
blob0c235071c070ad13ac66f4886a0f50cd75a4c543
1 /*
2 * Window position related functions.
4 * Copyright 1993, 1994, 1995 Alexandre Julliard
5 * 1995, 1996, 1999 Alex Korobka
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdarg.h>
26 #include <string.h>
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "winerror.h"
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winerror.h"
34 #include "wine/winuser16.h"
35 #include "wine/server.h"
36 #include "controls.h"
37 #include "user_private.h"
38 #include "win.h"
39 #include "winpos.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(win);
44 #define HAS_DLGFRAME(style,exStyle) \
45 (((exStyle) & WS_EX_DLGMODALFRAME) || \
46 (((style) & WS_DLGFRAME) && !((style) & WS_BORDER)))
48 #define HAS_THICKFRAME(style) \
49 (((style) & WS_THICKFRAME) && \
50 !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
52 #define EMPTYPOINT(pt) ((*(LONG*)&(pt)) == -1)
54 #define PLACE_MIN 0x0001
55 #define PLACE_MAX 0x0002
56 #define PLACE_RECT 0x0004
59 #define DWP_MAGIC ((INT)('W' | ('P' << 8) | ('O' << 16) | ('S' << 24)))
61 typedef struct
63 INT actualCount;
64 INT suggestedCount;
65 BOOL valid;
66 INT wMagic;
67 HWND hwndParent;
68 WINDOWPOS winPos[1];
69 } DWP;
71 typedef struct
73 RECT16 rectNormal;
74 POINT16 ptIconPos;
75 POINT16 ptMaxPos;
76 HWND hwndIconTitle;
77 } INTERNALPOS, *LPINTERNALPOS;
79 /* ----- internal functions ----- */
81 static const WCHAR SysIP_W[] = { 'S','y','s','I','P',0 };
83 static inline INTERNALPOS *get_internal_pos( HWND hwnd )
85 return GetPropW( hwnd, SysIP_W );
88 static inline void set_internal_pos( HWND hwnd, INTERNALPOS *pos )
90 SetPropW( hwnd, SysIP_W, pos );
93 /***********************************************************************
94 * WINPOS_CheckInternalPos
96 * Called when a window is destroyed.
98 void WINPOS_CheckInternalPos( HWND hwnd )
100 LPINTERNALPOS lpPos = get_internal_pos( hwnd );
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) ret = USER_Driver->pSetWindowRgn( hwnd, hrgn, bRedraw );
269 if (ret && bRedraw) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_INVALIDATE | RDW_ERASE );
270 return ret;
274 /***********************************************************************
275 * GetClientRect (USER32.@)
277 BOOL WINAPI GetClientRect( HWND hwnd, LPRECT rect )
279 BOOL ret;
281 if ((ret = WIN_GetRectangles( hwnd, NULL, rect )))
283 rect->right -= rect->left;
284 rect->bottom -= rect->top;
285 rect->left = rect->top = 0;
287 return ret;
291 /*******************************************************************
292 * ClientToScreen (USER32.@)
294 BOOL WINAPI ClientToScreen( HWND hwnd, LPPOINT lppnt )
296 MapWindowPoints( hwnd, 0, lppnt, 1 );
297 return TRUE;
301 /*******************************************************************
302 * ScreenToClient (USER32.@)
304 BOOL WINAPI ScreenToClient( HWND hwnd, LPPOINT lppnt )
306 MapWindowPoints( 0, hwnd, lppnt, 1 );
307 return TRUE;
311 /***********************************************************************
312 * list_children_from_point
314 * Get the list of children that can contain point from the server.
315 * Point is in screen coordinates.
316 * Returned list must be freed by caller.
318 static HWND *list_children_from_point( HWND hwnd, POINT pt )
320 HWND *list;
321 int size = 32;
323 for (;;)
325 int count = 0;
327 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
329 SERVER_START_REQ( get_window_children_from_point )
331 req->parent = hwnd;
332 req->x = pt.x;
333 req->y = pt.y;
334 wine_server_set_reply( req, list, (size-1) * sizeof(HWND) );
335 if (!wine_server_call( req )) count = reply->count;
337 SERVER_END_REQ;
338 if (count && count < size)
340 list[count] = 0;
341 return list;
343 HeapFree( GetProcessHeap(), 0, list );
344 if (!count) break;
345 size = count + 1; /* restart with a large enough buffer */
347 return NULL;
351 /***********************************************************************
352 * WINPOS_WindowFromPoint
354 * Find the window and hittest for a given point.
356 HWND WINPOS_WindowFromPoint( HWND hwndScope, POINT pt, INT *hittest )
358 int i, res;
359 HWND ret, *list;
361 if (!hwndScope) hwndScope = GetDesktopWindow();
363 *hittest = HTNOWHERE;
365 if (!(list = list_children_from_point( hwndScope, pt ))) return 0;
367 /* now determine the hittest */
369 for (i = 0; list[i]; i++)
371 LONG style = GetWindowLongW( list[i], GWL_STYLE );
373 /* If window is minimized or disabled, return at once */
374 if (style & WS_MINIMIZE)
376 *hittest = HTCAPTION;
377 break;
379 if (style & WS_DISABLED)
381 *hittest = HTERROR;
382 break;
384 /* Send WM_NCCHITTEST (if same thread) */
385 if (!WIN_IsCurrentThread( list[i] ))
387 *hittest = HTCLIENT;
388 break;
390 res = SendMessageW( list[i], WM_NCHITTEST, 0, MAKELONG(pt.x,pt.y) );
391 if (res != HTTRANSPARENT)
393 *hittest = res; /* Found the window */
394 break;
396 /* continue search with next window in z-order */
398 ret = list[i];
399 HeapFree( GetProcessHeap(), 0, list );
400 TRACE( "scope %p (%ld,%ld) returning %p\n", hwndScope, pt.x, pt.y, ret );
401 return ret;
405 /*******************************************************************
406 * WindowFromPoint (USER32.@)
408 HWND WINAPI WindowFromPoint( POINT pt )
410 INT hittest;
411 return WINPOS_WindowFromPoint( 0, pt, &hittest );
415 /*******************************************************************
416 * ChildWindowFromPoint (USER32.@)
418 HWND WINAPI ChildWindowFromPoint( HWND hwndParent, POINT pt )
420 return ChildWindowFromPointEx( hwndParent, pt, CWP_ALL );
423 /*******************************************************************
424 * ChildWindowFromPointEx (USER32.@)
426 HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
428 /* pt is in the client coordinates */
429 HWND *list;
430 int i;
431 RECT rect;
432 HWND retvalue;
434 GetClientRect( hwndParent, &rect );
435 if (!PtInRect( &rect, pt )) return 0;
436 if (!(list = WIN_ListChildren( hwndParent ))) return hwndParent;
438 for (i = 0; list[i]; i++)
440 if (!WIN_GetRectangles( list[i], &rect, NULL )) continue;
441 if (!PtInRect( &rect, pt )) continue;
442 if (uFlags & (CWP_SKIPINVISIBLE|CWP_SKIPDISABLED))
444 LONG style = GetWindowLongW( list[i], GWL_STYLE );
445 if ((uFlags & CWP_SKIPINVISIBLE) && !(style & WS_VISIBLE)) continue;
446 if ((uFlags & CWP_SKIPDISABLED) && (style & WS_DISABLED)) continue;
448 if (uFlags & CWP_SKIPTRANSPARENT)
450 if (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TRANSPARENT) continue;
452 break;
454 retvalue = list[i];
455 HeapFree( GetProcessHeap(), 0, list );
456 if (!retvalue) retvalue = hwndParent;
457 return retvalue;
461 /*******************************************************************
462 * WINPOS_GetWinOffset
464 * Calculate the offset between the origin of the two windows. Used
465 * to implement MapWindowPoints.
467 static void WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo, POINT *offset )
469 WND * wndPtr;
471 offset->x = offset->y = 0;
473 /* Translate source window origin to screen coords */
474 if (hwndFrom)
476 HWND hwnd = hwndFrom;
478 while (hwnd)
480 if (hwnd == hwndTo) return;
481 if (!(wndPtr = WIN_GetPtr( hwnd )))
483 ERR( "bad hwndFrom = %p\n", hwnd );
484 return;
486 if (wndPtr == WND_DESKTOP) break;
487 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
488 offset->x += wndPtr->rectClient.left;
489 offset->y += wndPtr->rectClient.top;
490 hwnd = wndPtr->parent;
491 WIN_ReleasePtr( wndPtr );
495 /* Translate origin to destination window coords */
496 if (hwndTo)
498 HWND hwnd = hwndTo;
500 while (hwnd)
502 if (!(wndPtr = WIN_GetPtr( hwnd )))
504 ERR( "bad hwndTo = %p\n", hwnd );
505 return;
507 if (wndPtr == WND_DESKTOP) break;
508 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
509 offset->x -= wndPtr->rectClient.left;
510 offset->y -= wndPtr->rectClient.top;
511 hwnd = wndPtr->parent;
512 WIN_ReleasePtr( wndPtr );
515 return;
517 other_process: /* one of the parents may belong to another process, do it the hard way */
518 offset->x = offset->y = 0;
519 SERVER_START_REQ( get_windows_offset )
521 req->from = hwndFrom;
522 req->to = hwndTo;
523 if (!wine_server_call( req ))
525 offset->x = reply->x;
526 offset->y = reply->y;
529 SERVER_END_REQ;
533 /*******************************************************************
534 * MapWindowPoints (USER.258)
536 void WINAPI MapWindowPoints16( HWND16 hwndFrom, HWND16 hwndTo,
537 LPPOINT16 lppt, UINT16 count )
539 POINT offset;
541 WINPOS_GetWinOffset( WIN_Handle32(hwndFrom), WIN_Handle32(hwndTo), &offset );
542 while (count--)
544 lppt->x += offset.x;
545 lppt->y += offset.y;
546 lppt++;
551 /*******************************************************************
552 * MapWindowPoints (USER32.@)
554 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
556 POINT offset;
558 WINPOS_GetWinOffset( hwndFrom, hwndTo, &offset );
559 while (count--)
561 lppt->x += offset.x;
562 lppt->y += offset.y;
563 lppt++;
565 return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
569 /***********************************************************************
570 * IsIconic (USER32.@)
572 BOOL WINAPI IsIconic(HWND hWnd)
574 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
578 /***********************************************************************
579 * IsZoomed (USER32.@)
581 BOOL WINAPI IsZoomed(HWND hWnd)
583 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
587 /*******************************************************************
588 * AllowSetForegroundWindow (USER32.@)
590 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
592 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
593 * implemented, then fix this function. */
594 return TRUE;
598 /*******************************************************************
599 * LockSetForegroundWindow (USER32.@)
601 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
603 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
604 * implemented, then fix this function. */
605 return TRUE;
609 /***********************************************************************
610 * BringWindowToTop (USER32.@)
612 BOOL WINAPI BringWindowToTop( HWND hwnd )
614 return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
618 /***********************************************************************
619 * MoveWindow (USER32.@)
621 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
622 BOOL repaint )
624 int flags = SWP_NOZORDER | SWP_NOACTIVATE;
625 if (!repaint) flags |= SWP_NOREDRAW;
626 TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
627 return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
630 /***********************************************************************
631 * WINPOS_InitInternalPos
633 static LPINTERNALPOS WINPOS_InitInternalPos( WND* wnd )
635 LPINTERNALPOS lpPos = get_internal_pos( wnd->hwndSelf );
636 if( !lpPos )
638 /* this happens when the window is minimized/maximized
639 * for the first time (rectWindow is not adjusted yet) */
641 lpPos = HeapAlloc( GetProcessHeap(), 0, sizeof(INTERNALPOS) );
642 if( !lpPos ) return NULL;
644 set_internal_pos( wnd->hwndSelf, lpPos );
645 lpPos->hwndIconTitle = 0; /* defer until needs to be shown */
646 lpPos->rectNormal.left = wnd->rectWindow.left;
647 lpPos->rectNormal.top = wnd->rectWindow.top;
648 lpPos->rectNormal.right = wnd->rectWindow.right;
649 lpPos->rectNormal.bottom = wnd->rectWindow.bottom;
650 lpPos->ptIconPos.x = lpPos->ptIconPos.y = -1;
651 lpPos->ptMaxPos.x = lpPos->ptMaxPos.y = -1;
654 if( wnd->dwStyle & WS_MINIMIZE )
656 lpPos->ptIconPos.x = wnd->rectWindow.left;
657 lpPos->ptIconPos.y = wnd->rectWindow.top;
659 else if( wnd->dwStyle & WS_MAXIMIZE )
661 lpPos->ptMaxPos.x = wnd->rectWindow.left;
662 lpPos->ptMaxPos.y = wnd->rectWindow.top;
664 else
666 lpPos->rectNormal.left = wnd->rectWindow.left;
667 lpPos->rectNormal.top = wnd->rectWindow.top;
668 lpPos->rectNormal.right = wnd->rectWindow.right;
669 lpPos->rectNormal.bottom = wnd->rectWindow.bottom;
671 return lpPos;
674 /***********************************************************************
675 * WINPOS_RedrawIconTitle
677 BOOL WINPOS_RedrawIconTitle( HWND hWnd )
679 LPINTERNALPOS lpPos = get_internal_pos( hWnd );
680 if( lpPos )
682 if( lpPos->hwndIconTitle )
684 SendMessageW( lpPos->hwndIconTitle, WM_SHOWWINDOW, TRUE, 0);
685 InvalidateRect( lpPos->hwndIconTitle, NULL, TRUE );
686 return TRUE;
689 return FALSE;
692 /***********************************************************************
693 * WINPOS_ShowIconTitle
695 BOOL WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
697 LPINTERNALPOS lpPos = get_internal_pos( hwnd );
699 if (lpPos && !GetPropA( hwnd, "__wine_x11_managed" ))
701 HWND title = lpPos->hwndIconTitle;
703 TRACE("%p %i\n", hwnd, (bShow != 0) );
705 if( !title )
706 lpPos->hwndIconTitle = title = ICONTITLE_Create( hwnd );
707 if( bShow )
709 if (!IsWindowVisible(title))
711 SendMessageW( title, WM_SHOWWINDOW, TRUE, 0 );
712 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
713 SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
716 else ShowWindow( title, SW_HIDE );
718 return FALSE;
721 /*******************************************************************
722 * WINPOS_GetMinMaxInfo
724 * Get the minimized and maximized information for a window.
726 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
727 POINT *minTrack, POINT *maxTrack )
729 LPINTERNALPOS lpPos;
730 MINMAXINFO MinMax;
731 INT xinc, yinc;
732 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
733 LONG exstyle = GetWindowLongA( hwnd, GWL_EXSTYLE );
734 RECT rc;
736 /* Compute default values */
738 GetWindowRect(hwnd, &rc);
739 MinMax.ptReserved.x = rc.left;
740 MinMax.ptReserved.y = rc.top;
742 if (style & WS_CHILD)
744 if ((style & WS_CAPTION) == WS_CAPTION)
745 style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
747 GetClientRect(GetAncestor(hwnd,GA_PARENT), &rc);
748 AdjustWindowRectEx(&rc, style, ((style & WS_POPUP) && GetMenu(hwnd)), exstyle);
750 /* avoid calculating this twice */
751 style &= ~(WS_DLGFRAME | WS_BORDER | WS_THICKFRAME);
753 MinMax.ptMaxSize.x = rc.right - rc.left;
754 MinMax.ptMaxSize.y = rc.bottom - rc.top;
756 else
758 MinMax.ptMaxSize.x = GetSystemMetrics(SM_CXSCREEN);
759 MinMax.ptMaxSize.y = GetSystemMetrics(SM_CYSCREEN);
761 MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
762 MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
763 MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXSCREEN) + 2*GetSystemMetrics(SM_CXFRAME);
764 MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYSCREEN) + 2*GetSystemMetrics(SM_CYFRAME);
766 if (HAS_DLGFRAME( style, exstyle ))
768 xinc = GetSystemMetrics(SM_CXDLGFRAME);
769 yinc = GetSystemMetrics(SM_CYDLGFRAME);
771 else
773 xinc = yinc = 0;
774 if (HAS_THICKFRAME(style))
776 xinc += GetSystemMetrics(SM_CXFRAME);
777 yinc += GetSystemMetrics(SM_CYFRAME);
779 if (style & WS_BORDER)
781 xinc += GetSystemMetrics(SM_CXBORDER);
782 yinc += GetSystemMetrics(SM_CYBORDER);
785 MinMax.ptMaxSize.x += 2 * xinc;
786 MinMax.ptMaxSize.y += 2 * yinc;
788 lpPos = get_internal_pos( hwnd );
789 if( lpPos && !EMPTYPOINT(lpPos->ptMaxPos) )
791 MinMax.ptMaxPosition.x = lpPos->ptMaxPos.x;
792 MinMax.ptMaxPosition.y = lpPos->ptMaxPos.y;
794 else
796 MinMax.ptMaxPosition.x = -xinc;
797 MinMax.ptMaxPosition.y = -yinc;
800 SendMessageW( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
802 /* Some sanity checks */
804 TRACE("%ld %ld / %ld %ld / %ld %ld / %ld %ld\n",
805 MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
806 MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
807 MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
808 MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
809 MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
810 MinMax.ptMinTrackSize.x );
811 MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
812 MinMax.ptMinTrackSize.y );
814 if (maxSize) *maxSize = MinMax.ptMaxSize;
815 if (maxPos) *maxPos = MinMax.ptMaxPosition;
816 if (minTrack) *minTrack = MinMax.ptMinTrackSize;
817 if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
820 /***********************************************************************
821 * ShowWindowAsync (USER32.@)
823 * doesn't wait; returns immediately.
824 * used by threads to toggle windows in other (possibly hanging) threads
826 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
828 HWND full_handle;
830 if (is_broadcast(hwnd))
832 SetLastError( ERROR_INVALID_PARAMETER );
833 return FALSE;
836 if ((full_handle = WIN_IsCurrentThread( hwnd )))
837 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 (is_broadcast(hwnd))
852 SetLastError( ERROR_INVALID_PARAMETER );
853 return FALSE;
855 if ((full_handle = WIN_IsCurrentThread( hwnd )))
856 return USER_Driver->pShowWindow( full_handle, cmd );
858 return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
862 /***********************************************************************
863 * GetInternalWindowPos (USER32.@)
865 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
866 LPPOINT ptIcon )
868 WINDOWPLACEMENT wndpl;
869 if (GetWindowPlacement( hwnd, &wndpl ))
871 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
872 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
873 return wndpl.showCmd;
875 return 0;
879 /***********************************************************************
880 * GetWindowPlacement (USER32.@)
882 * Win95:
883 * Fails if wndpl->length of Win95 (!) apps is invalid.
885 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
887 WND *pWnd = WIN_GetPtr( hwnd );
888 LPINTERNALPOS lpPos;
890 if (!pWnd || pWnd == WND_DESKTOP) return FALSE;
891 if (pWnd == WND_OTHER_PROCESS)
893 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
894 return FALSE;
897 lpPos = WINPOS_InitInternalPos( pWnd );
898 wndpl->length = sizeof(*wndpl);
899 if( pWnd->dwStyle & WS_MINIMIZE )
900 wndpl->showCmd = SW_SHOWMINIMIZED;
901 else
902 wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
903 if( pWnd->flags & WIN_RESTORE_MAX )
904 wndpl->flags = WPF_RESTORETOMAXIMIZED;
905 else
906 wndpl->flags = 0;
907 wndpl->ptMinPosition.x = lpPos->ptIconPos.x;
908 wndpl->ptMinPosition.y = lpPos->ptIconPos.y;
909 wndpl->ptMaxPosition.x = lpPos->ptMaxPos.x;
910 wndpl->ptMaxPosition.y = lpPos->ptMaxPos.y;
911 wndpl->rcNormalPosition.left = lpPos->rectNormal.left;
912 wndpl->rcNormalPosition.top = lpPos->rectNormal.top;
913 wndpl->rcNormalPosition.right = lpPos->rectNormal.right;
914 wndpl->rcNormalPosition.bottom = lpPos->rectNormal.bottom;
915 WIN_ReleasePtr( pWnd );
916 return TRUE;
920 /***********************************************************************
921 * WINPOS_SetPlacement
923 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
925 LPINTERNALPOS lpPos;
926 DWORD style;
927 WND *pWnd = WIN_GetPtr( hwnd );
929 if (!pWnd || pWnd == WND_OTHER_PROCESS || pWnd == WND_DESKTOP) return FALSE;
930 lpPos = WINPOS_InitInternalPos( pWnd );
932 if( flags & PLACE_MIN )
934 lpPos->ptIconPos.x = wndpl->ptMinPosition.x;
935 lpPos->ptIconPos.y = wndpl->ptMinPosition.y;
937 if( flags & PLACE_MAX )
939 lpPos->ptMaxPos.x = wndpl->ptMaxPosition.x;
940 lpPos->ptMaxPos.y = wndpl->ptMaxPosition.y;
942 if( flags & PLACE_RECT)
944 lpPos->rectNormal.left = wndpl->rcNormalPosition.left;
945 lpPos->rectNormal.top = wndpl->rcNormalPosition.top;
946 lpPos->rectNormal.right = wndpl->rcNormalPosition.right;
947 lpPos->rectNormal.bottom = wndpl->rcNormalPosition.bottom;
950 style = pWnd->dwStyle;
951 WIN_ReleasePtr( pWnd );
953 if( style & WS_MINIMIZE )
955 WINPOS_ShowIconTitle( hwnd, FALSE );
956 if( wndpl->flags & WPF_SETMINPOSITION && !EMPTYPOINT(lpPos->ptIconPos))
957 SetWindowPos( hwnd, 0, lpPos->ptIconPos.x, lpPos->ptIconPos.y,
958 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
960 else if( style & WS_MAXIMIZE )
962 if( !EMPTYPOINT(lpPos->ptMaxPos) )
963 SetWindowPos( hwnd, 0, lpPos->ptMaxPos.x, lpPos->ptMaxPos.y,
964 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
966 else if( flags & PLACE_RECT )
967 SetWindowPos( hwnd, 0, lpPos->rectNormal.left, lpPos->rectNormal.top,
968 lpPos->rectNormal.right - lpPos->rectNormal.left,
969 lpPos->rectNormal.bottom - lpPos->rectNormal.top,
970 SWP_NOZORDER | SWP_NOACTIVATE );
972 ShowWindow( hwnd, wndpl->showCmd );
974 if (IsIconic( hwnd ))
976 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE) WINPOS_ShowIconTitle( hwnd, TRUE );
978 /* SDK: ...valid only the next time... */
979 if( wndpl->flags & WPF_RESTORETOMAXIMIZED )
981 pWnd = WIN_GetPtr( hwnd );
982 if (pWnd && pWnd != WND_OTHER_PROCESS)
984 pWnd->flags |= WIN_RESTORE_MAX;
985 WIN_ReleasePtr( pWnd );
989 return TRUE;
993 /***********************************************************************
994 * SetWindowPlacement (USER32.@)
996 * Win95:
997 * Fails if wndpl->length of Win95 (!) apps is invalid.
999 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
1001 if (!wpl) return FALSE;
1002 return WINPOS_SetPlacement( hwnd, wpl, PLACE_MIN | PLACE_MAX | PLACE_RECT );
1006 /***********************************************************************
1007 * AnimateWindow (USER32.@)
1008 * Shows/Hides a window with an animation
1009 * NO ANIMATION YET
1011 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
1013 FIXME("partial stub\n");
1015 /* If trying to show/hide and it's already *
1016 * shown/hidden or invalid window, fail with *
1017 * invalid parameter */
1018 if(!IsWindow(hwnd) ||
1019 (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
1020 (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
1022 SetLastError(ERROR_INVALID_PARAMETER);
1023 return FALSE;
1026 ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
1028 return TRUE;
1031 /***********************************************************************
1032 * SetInternalWindowPos (USER32.@)
1034 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
1035 LPRECT rect, LPPOINT pt )
1037 if( IsWindow(hwnd) )
1039 WINDOWPLACEMENT wndpl;
1040 UINT flags;
1042 wndpl.length = sizeof(wndpl);
1043 wndpl.showCmd = showCmd;
1044 wndpl.flags = flags = 0;
1046 if( pt )
1048 flags |= PLACE_MIN;
1049 wndpl.flags |= WPF_SETMINPOSITION;
1050 wndpl.ptMinPosition = *pt;
1052 if( rect )
1054 flags |= PLACE_RECT;
1055 wndpl.rcNormalPosition = *rect;
1057 WINPOS_SetPlacement( hwnd, &wndpl, flags );
1062 /*******************************************************************
1063 * can_activate_window
1065 * Check if we can activate the specified window.
1067 static BOOL can_activate_window( HWND hwnd )
1069 LONG style;
1071 if (!hwnd) return FALSE;
1072 style = GetWindowLongW( hwnd, GWL_STYLE );
1073 if (!(style & WS_VISIBLE)) return FALSE;
1074 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1075 return !(style & WS_DISABLED);
1079 /*******************************************************************
1080 * WINPOS_ActivateOtherWindow
1082 * Activates window other than pWnd.
1084 void WINPOS_ActivateOtherWindow(HWND hwnd)
1086 HWND hwndTo, fg;
1088 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1090 hwndTo = GetAncestor( hwndTo, GA_ROOT );
1091 if (can_activate_window( hwndTo )) goto done;
1094 hwndTo = hwnd;
1095 for (;;)
1097 if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1098 if (can_activate_window( hwndTo )) break;
1101 done:
1102 fg = GetForegroundWindow();
1103 TRACE("win = %p fg = %p\n", hwndTo, fg);
1104 if (!fg || (hwnd == fg))
1106 if (SetForegroundWindow( hwndTo )) return;
1108 if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1112 /***********************************************************************
1113 * WINPOS_HandleWindowPosChanging
1115 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1117 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1119 POINT minTrack, maxTrack;
1120 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1122 if (winpos->flags & SWP_NOSIZE) return 0;
1123 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1125 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1126 if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1127 if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1128 if (!(style & WS_MINIMIZE))
1130 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1131 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1134 return 0;
1138 /***********************************************************************
1139 * dump_winpos_flags
1141 static void dump_winpos_flags(UINT flags)
1143 TRACE("flags:");
1144 if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1145 if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1146 if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1147 if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1148 if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1149 if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1150 if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1151 if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1152 if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1153 if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1154 if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1155 if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1156 if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1158 #define DUMPED_FLAGS \
1159 (SWP_NOSIZE | \
1160 SWP_NOMOVE | \
1161 SWP_NOZORDER | \
1162 SWP_NOREDRAW | \
1163 SWP_NOACTIVATE | \
1164 SWP_FRAMECHANGED | \
1165 SWP_SHOWWINDOW | \
1166 SWP_HIDEWINDOW | \
1167 SWP_NOCOPYBITS | \
1168 SWP_NOOWNERZORDER | \
1169 SWP_NOSENDCHANGING | \
1170 SWP_DEFERERASE | \
1171 SWP_ASYNCWINDOWPOS)
1173 if(flags & ~DUMPED_FLAGS) TRACE(" %08x", flags & ~DUMPED_FLAGS);
1174 TRACE("\n");
1175 #undef DUMPED_FLAGS
1178 /***********************************************************************
1179 * SetWindowPos (USER32.@)
1181 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
1182 INT x, INT y, INT cx, INT cy, UINT flags )
1184 WINDOWPOS winpos;
1186 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1187 hwnd, hwndInsertAfter, x, y, cx, cy, flags);
1188 if(TRACE_ON(win)) dump_winpos_flags(flags);
1190 if (is_broadcast(hwnd))
1192 SetLastError( ERROR_INVALID_PARAMETER );
1193 return FALSE;
1196 winpos.hwnd = WIN_GetFullHandle(hwnd);
1197 winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
1198 winpos.x = x;
1199 winpos.y = y;
1200 winpos.cx = cx;
1201 winpos.cy = cy;
1202 winpos.flags = flags;
1203 if (WIN_IsCurrentThread( hwnd ))
1204 return USER_Driver->pSetWindowPos( &winpos );
1206 return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
1210 /***********************************************************************
1211 * BeginDeferWindowPos (USER32.@)
1213 HDWP WINAPI BeginDeferWindowPos( INT count )
1215 HDWP handle;
1216 DWP *pDWP;
1218 TRACE("%d\n", count);
1220 if (count < 0)
1222 SetLastError(ERROR_INVALID_PARAMETER);
1223 return 0;
1225 /* Windows allows zero count, in which case it allocates context for 8 moves */
1226 if (count == 0) count = 8;
1228 handle = USER_HEAP_ALLOC( sizeof(DWP) + (count-1)*sizeof(WINDOWPOS) );
1229 if (!handle) return 0;
1230 pDWP = (DWP *) USER_HEAP_LIN_ADDR( handle );
1231 pDWP->actualCount = 0;
1232 pDWP->suggestedCount = count;
1233 pDWP->valid = TRUE;
1234 pDWP->wMagic = DWP_MAGIC;
1235 pDWP->hwndParent = 0;
1237 TRACE("returning hdwp %p\n", handle);
1238 return handle;
1242 /***********************************************************************
1243 * DeferWindowPos (USER32.@)
1245 HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
1246 INT x, INT y, INT cx, INT cy,
1247 UINT flags )
1249 DWP *pDWP;
1250 int i;
1251 HDWP newhdwp = hdwp,retvalue;
1253 TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1254 hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
1256 hwnd = WIN_GetFullHandle( hwnd );
1257 if (hwnd == GetDesktopWindow()) return 0;
1259 if (!(pDWP = USER_HEAP_LIN_ADDR( hdwp ))) return 0;
1261 USER_Lock();
1263 for (i = 0; i < pDWP->actualCount; i++)
1265 if (pDWP->winPos[i].hwnd == hwnd)
1267 /* Merge with the other changes */
1268 if (!(flags & SWP_NOZORDER))
1270 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
1272 if (!(flags & SWP_NOMOVE))
1274 pDWP->winPos[i].x = x;
1275 pDWP->winPos[i].y = y;
1277 if (!(flags & SWP_NOSIZE))
1279 pDWP->winPos[i].cx = cx;
1280 pDWP->winPos[i].cy = cy;
1282 pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
1283 SWP_NOZORDER | SWP_NOREDRAW |
1284 SWP_NOACTIVATE | SWP_NOCOPYBITS|
1285 SWP_NOOWNERZORDER);
1286 pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
1287 SWP_FRAMECHANGED);
1288 retvalue = hdwp;
1289 goto END;
1292 if (pDWP->actualCount >= pDWP->suggestedCount)
1294 newhdwp = USER_HEAP_REALLOC( hdwp,
1295 sizeof(DWP) + pDWP->suggestedCount*sizeof(WINDOWPOS) );
1296 if (!newhdwp)
1298 retvalue = 0;
1299 goto END;
1301 pDWP = (DWP *) USER_HEAP_LIN_ADDR( newhdwp );
1302 pDWP->suggestedCount++;
1304 pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
1305 pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
1306 pDWP->winPos[pDWP->actualCount].x = x;
1307 pDWP->winPos[pDWP->actualCount].y = y;
1308 pDWP->winPos[pDWP->actualCount].cx = cx;
1309 pDWP->winPos[pDWP->actualCount].cy = cy;
1310 pDWP->winPos[pDWP->actualCount].flags = flags;
1311 pDWP->actualCount++;
1312 retvalue = newhdwp;
1313 END:
1314 USER_Unlock();
1315 return retvalue;
1319 /***********************************************************************
1320 * EndDeferWindowPos (USER32.@)
1322 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
1324 DWP *pDWP;
1325 WINDOWPOS *winpos;
1326 BOOL res = TRUE;
1327 int i;
1329 TRACE("%p\n", hdwp);
1331 pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
1332 if (!pDWP) return FALSE;
1333 for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
1335 if (!(res = USER_Driver->pSetWindowPos( winpos ))) break;
1337 USER_HEAP_FREE( hdwp );
1338 return res;
1342 /***********************************************************************
1343 * TileChildWindows (USER.199)
1345 void WINAPI TileChildWindows16( HWND16 parent, WORD action )
1347 FIXME("(%04x, %d): stub\n", parent, action);
1350 /***********************************************************************
1351 * CascadeChildWindows (USER.198)
1353 void WINAPI CascadeChildWindows16( HWND16 parent, WORD action )
1355 FIXME("(%04x, %d): stub\n", parent, action);