Add WAVE_FORMAT_EXTENSIBLE.
[wine/multimedia.git] / windows / winpos.c
blob5908de9b88c5e5f0815c02a9dbaa82f89053a978
1 /*
2 * Window position related functions.
4 * Copyright 1993, 1994, 1995 Alexandre Julliard
5 * 1995, 1996, 1999 Alex Korobka
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdarg.h>
26 #include <string.h>
27 #include "winerror.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "winerror.h"
32 #include "wine/winuser16.h"
33 #include "wine/server.h"
34 #include "controls.h"
35 #include "user.h"
36 #include "win.h"
37 #include "message.h"
38 #include "winpos.h"
39 #include "nonclient.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(win);
44 #define HAS_DLGFRAME(style,exStyle) \
45 (((exStyle) & WS_EX_DLGMODALFRAME) || \
46 (((style) & WS_DLGFRAME) && !((style) & WS_BORDER)))
48 #define HAS_THICKFRAME(style) \
49 (((style) & WS_THICKFRAME) && \
50 !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
52 #define EMPTYPOINT(pt) ((*(LONG*)&(pt)) == -1)
54 #define PLACE_MIN 0x0001
55 #define PLACE_MAX 0x0002
56 #define PLACE_RECT 0x0004
59 #define DWP_MAGIC ((INT)('W' | ('P' << 8) | ('O' << 16) | ('S' << 24)))
61 typedef struct
63 INT actualCount;
64 INT suggestedCount;
65 BOOL valid;
66 INT wMagic;
67 HWND hwndParent;
68 WINDOWPOS winPos[1];
69 } DWP;
71 /* ----- internal variables ----- */
73 static LPCSTR atomInternalPos;
76 /***********************************************************************
77 * WINPOS_CreateInternalPosAtom
79 BOOL WINPOS_CreateInternalPosAtom()
81 LPCSTR str = "SysIP";
82 atomInternalPos = (LPCSTR)(DWORD)GlobalAddAtomA(str);
83 return (atomInternalPos) ? TRUE : FALSE;
86 /***********************************************************************
87 * WINPOS_CheckInternalPos
89 * Called when a window is destroyed.
91 void WINPOS_CheckInternalPos( HWND hwnd )
93 LPINTERNALPOS lpPos = (LPINTERNALPOS) GetPropA( hwnd, atomInternalPos );
95 if( lpPos )
97 if( IsWindow(lpPos->hwndIconTitle) )
98 DestroyWindow( lpPos->hwndIconTitle );
99 HeapFree( GetProcessHeap(), 0, lpPos );
103 /***********************************************************************
104 * ArrangeIconicWindows (USER32.@)
106 UINT WINAPI ArrangeIconicWindows( HWND parent )
108 RECT rectParent;
109 HWND hwndChild;
110 INT x, y, xspacing, yspacing;
112 GetClientRect( parent, &rectParent );
113 x = rectParent.left;
114 y = rectParent.bottom;
115 xspacing = GetSystemMetrics(SM_CXICONSPACING);
116 yspacing = GetSystemMetrics(SM_CYICONSPACING);
118 hwndChild = GetWindow( parent, GW_CHILD );
119 while (hwndChild)
121 if( IsIconic( hwndChild ) )
123 WINPOS_ShowIconTitle( hwndChild, FALSE );
125 SetWindowPos( hwndChild, 0, x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
126 y - yspacing - GetSystemMetrics(SM_CYICON)/2, 0, 0,
127 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
128 if( IsWindow(hwndChild) )
129 WINPOS_ShowIconTitle(hwndChild , TRUE );
131 if (x <= rectParent.right - xspacing) x += xspacing;
132 else
134 x = rectParent.left;
135 y -= yspacing;
138 hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
140 return yspacing;
144 /***********************************************************************
145 * SwitchToThisWindow (USER32.@)
147 void WINAPI SwitchToThisWindow( HWND hwnd, BOOL restore )
149 ShowWindow( hwnd, restore ? SW_RESTORE : SW_SHOWMINIMIZED );
153 /***********************************************************************
154 * GetWindowRect (USER32.@)
156 BOOL WINAPI GetWindowRect( HWND hwnd, LPRECT rect )
158 BOOL ret = WIN_GetRectangles( hwnd, rect, NULL );
159 if (ret)
161 MapWindowPoints( GetAncestor( hwnd, GA_PARENT ), 0, (POINT *)rect, 2 );
162 TRACE( "hwnd %p (%ld,%ld)-(%ld,%ld)\n",
163 hwnd, rect->left, rect->top, rect->right, rect->bottom);
165 return ret;
169 /***********************************************************************
170 * GetWindowRgn (USER32.@)
172 int WINAPI GetWindowRgn ( HWND hwnd, HRGN hrgn )
174 int nRet = ERROR;
175 HRGN win_rgn = 0;
176 RGNDATA *data;
177 size_t size = 256;
178 BOOL retry = FALSE;
182 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
184 SetLastError( ERROR_OUTOFMEMORY );
185 return ERROR;
187 SERVER_START_REQ( get_window_region )
189 req->window = hwnd;
190 wine_server_set_reply( req, data->Buffer, size );
191 if (!wine_server_call_err( req ))
193 if (!reply->total_size) retry = FALSE; /* no region at all */
194 else if (reply->total_size <= size)
196 size_t reply_size = wine_server_reply_size( reply );
197 data->rdh.dwSize = sizeof(data->rdh);
198 data->rdh.iType = RDH_RECTANGLES;
199 data->rdh.nCount = reply_size / sizeof(RECT);
200 data->rdh.nRgnSize = reply_size;
201 win_rgn = ExtCreateRegion( NULL, size, data );
202 retry = FALSE;
204 else
206 size = reply->total_size;
207 retry = TRUE;
211 SERVER_END_REQ;
212 HeapFree( GetProcessHeap(), 0, data );
213 } while (retry);
215 if (win_rgn)
217 nRet = CombineRgn( hrgn, win_rgn, 0, RGN_COPY );
218 DeleteObject( win_rgn );
220 return nRet;
224 /***********************************************************************
225 * SetWindowRgn (USER32.@)
227 int WINAPI SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL bRedraw )
229 static const RECT empty_rect;
230 BOOL ret;
232 if (hrgn)
234 RGNDATA *data;
235 DWORD size;
237 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
238 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
239 if (!GetRegionData( hrgn, size, data ))
241 HeapFree( GetProcessHeap(), 0, data );
242 return FALSE;
244 SERVER_START_REQ( set_window_region )
246 req->window = hwnd;
247 if (data->rdh.nCount)
248 wine_server_add_data( req, data->Buffer, data->rdh.nCount * sizeof(RECT) );
249 else
250 wine_server_add_data( req, &empty_rect, sizeof(empty_rect) );
251 ret = !wine_server_call_err( req );
253 SERVER_END_REQ;
255 else /* clear existing region */
257 SERVER_START_REQ( set_window_region )
259 req->window = hwnd;
260 ret = !wine_server_call_err( req );
262 SERVER_END_REQ;
265 if (ret && USER_Driver.pSetWindowRgn)
266 ret = USER_Driver.pSetWindowRgn( hwnd, hrgn, bRedraw );
268 if (ret && bRedraw) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_INVALIDATE | RDW_ERASE );
269 return ret;
273 /***********************************************************************
274 * GetClientRect (USER32.@)
276 BOOL WINAPI GetClientRect( HWND hwnd, LPRECT rect )
278 BOOL ret;
280 rect->right = rect->bottom = 0;
281 if ((ret = WIN_GetRectangles( hwnd, NULL, rect )))
283 rect->right -= rect->left;
284 rect->bottom -= rect->top;
286 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 = SendMessageA( 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 0;
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_OTHER_PROCESS) goto other_process;
487 offset->x += wndPtr->rectClient.left;
488 offset->y += wndPtr->rectClient.top;
489 hwnd = wndPtr->parent;
490 WIN_ReleasePtr( wndPtr );
494 /* Translate origin to destination window coords */
495 if (hwndTo)
497 HWND hwnd = hwndTo;
499 while (hwnd)
501 if (!(wndPtr = WIN_GetPtr( hwnd )))
503 ERR( "bad hwndTo = %p\n", hwnd );
504 return;
506 if (wndPtr == WND_OTHER_PROCESS) goto other_process;
507 offset->x -= wndPtr->rectClient.left;
508 offset->y -= wndPtr->rectClient.top;
509 hwnd = wndPtr->parent;
510 WIN_ReleasePtr( wndPtr );
513 return;
515 other_process: /* one of the parents may belong to another process, do it the hard way */
516 offset->x = offset->y = 0;
517 SERVER_START_REQ( get_windows_offset )
519 req->from = hwndFrom;
520 req->to = hwndTo;
521 if (!wine_server_call( req ))
523 offset->x = reply->x;
524 offset->y = reply->y;
527 SERVER_END_REQ;
531 /*******************************************************************
532 * MapWindowPoints (USER.258)
534 void WINAPI MapWindowPoints16( HWND16 hwndFrom, HWND16 hwndTo,
535 LPPOINT16 lppt, UINT16 count )
537 POINT offset;
539 WINPOS_GetWinOffset( WIN_Handle32(hwndFrom), WIN_Handle32(hwndTo), &offset );
540 while (count--)
542 lppt->x += offset.x;
543 lppt->y += offset.y;
544 lppt++;
549 /*******************************************************************
550 * MapWindowPoints (USER32.@)
552 INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
554 POINT offset;
556 WINPOS_GetWinOffset( hwndFrom, hwndTo, &offset );
557 while (count--)
559 lppt->x += offset.x;
560 lppt->y += offset.y;
561 lppt++;
563 return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
567 /***********************************************************************
568 * IsIconic (USER32.@)
570 BOOL WINAPI IsIconic(HWND hWnd)
572 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
576 /***********************************************************************
577 * IsZoomed (USER32.@)
579 BOOL WINAPI IsZoomed(HWND hWnd)
581 return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
585 /*******************************************************************
586 * AllowSetForegroundWindow (USER32.@)
588 BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
590 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
591 * implemented, then fix this function. */
592 return TRUE;
596 /*******************************************************************
597 * LockSetForegroundWindow (USER32.@)
599 BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
601 /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
602 * implemented, then fix this function. */
603 return TRUE;
607 /***********************************************************************
608 * BringWindowToTop (USER32.@)
610 BOOL WINAPI BringWindowToTop( HWND hwnd )
612 return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
616 /***********************************************************************
617 * MoveWindow (USER32.@)
619 BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
620 BOOL repaint )
622 int flags = SWP_NOZORDER | SWP_NOACTIVATE;
623 if (!repaint) flags |= SWP_NOREDRAW;
624 TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
625 return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
628 /***********************************************************************
629 * WINPOS_InitInternalPos
631 static LPINTERNALPOS WINPOS_InitInternalPos( WND* wnd, POINT pt, const RECT *restoreRect )
633 LPINTERNALPOS lpPos = (LPINTERNALPOS) GetPropA( wnd->hwndSelf,
634 atomInternalPos );
635 if( !lpPos )
637 /* this happens when the window is minimized/maximized
638 * for the first time (rectWindow is not adjusted yet) */
640 lpPos = HeapAlloc( GetProcessHeap(), 0, sizeof(INTERNALPOS) );
641 if( !lpPos ) return NULL;
643 SetPropA( wnd->hwndSelf, atomInternalPos, (HANDLE)lpPos );
644 lpPos->hwndIconTitle = 0; /* defer until needs to be shown */
645 CONV_RECT32TO16( &wnd->rectWindow, &lpPos->rectNormal );
646 *(UINT*)&lpPos->ptIconPos = *(UINT*)&lpPos->ptMaxPos = 0xFFFFFFFF;
649 if( wnd->dwStyle & WS_MINIMIZE )
650 CONV_POINT32TO16( &pt, &lpPos->ptIconPos );
651 else if( wnd->dwStyle & WS_MAXIMIZE )
652 CONV_POINT32TO16( &pt, &lpPos->ptMaxPos );
653 else if( restoreRect )
654 CONV_RECT32TO16( restoreRect, &lpPos->rectNormal );
656 return lpPos;
659 /***********************************************************************
660 * WINPOS_RedrawIconTitle
662 BOOL WINPOS_RedrawIconTitle( HWND hWnd )
664 LPINTERNALPOS lpPos = (LPINTERNALPOS)GetPropA( hWnd, atomInternalPos );
665 if( lpPos )
667 if( lpPos->hwndIconTitle )
669 SendMessageA( lpPos->hwndIconTitle, WM_SHOWWINDOW, TRUE, 0);
670 InvalidateRect( lpPos->hwndIconTitle, NULL, TRUE );
671 return TRUE;
674 return FALSE;
677 /***********************************************************************
678 * WINPOS_ShowIconTitle
680 BOOL WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
682 LPINTERNALPOS lpPos = (LPINTERNALPOS)GetPropA( hwnd, atomInternalPos );
684 if( lpPos && !(GetWindowLongA( hwnd, GWL_EXSTYLE) & WS_EX_MANAGED))
686 HWND title = lpPos->hwndIconTitle;
688 TRACE("%p %i\n", hwnd, (bShow != 0) );
690 if( !title )
691 lpPos->hwndIconTitle = title = ICONTITLE_Create( hwnd );
692 if( bShow )
694 if (!IsWindowVisible(title))
696 SendMessageA( title, WM_SHOWWINDOW, TRUE, 0 );
697 SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
698 SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
701 else ShowWindow( title, SW_HIDE );
703 return FALSE;
706 /*******************************************************************
707 * WINPOS_GetMinMaxInfo
709 * Get the minimized and maximized information for a window.
711 void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
712 POINT *minTrack, POINT *maxTrack )
714 LPINTERNALPOS lpPos;
715 MINMAXINFO MinMax;
716 INT xinc, yinc;
717 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
718 LONG exstyle = GetWindowLongA( hwnd, GWL_EXSTYLE );
719 RECT rc;
721 /* Compute default values */
723 GetWindowRect(hwnd, &rc);
724 MinMax.ptReserved.x = rc.left;
725 MinMax.ptReserved.y = rc.top;
727 if (style & WS_CHILD)
729 if ((style & WS_CAPTION) == WS_CAPTION)
730 style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
732 GetClientRect(GetParent(hwnd), &rc);
733 AdjustWindowRectEx(&rc, style, 0, exstyle);
735 /* avoid calculating this twice */
736 style &= ~(WS_DLGFRAME | WS_BORDER | WS_THICKFRAME);
738 MinMax.ptMaxSize.x = rc.right - rc.left;
739 MinMax.ptMaxSize.y = rc.bottom - rc.top;
741 else
743 MinMax.ptMaxSize.x = GetSystemMetrics(SM_CXSCREEN);
744 MinMax.ptMaxSize.y = GetSystemMetrics(SM_CYSCREEN);
746 MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
747 MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
748 MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXSCREEN);
749 MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYSCREEN);
751 if (HAS_DLGFRAME( style, exstyle ))
753 xinc = GetSystemMetrics(SM_CXDLGFRAME);
754 yinc = GetSystemMetrics(SM_CYDLGFRAME);
756 else
758 xinc = yinc = 0;
759 if (HAS_THICKFRAME(style))
761 xinc += GetSystemMetrics(SM_CXFRAME);
762 yinc += GetSystemMetrics(SM_CYFRAME);
764 if (style & WS_BORDER)
766 xinc += GetSystemMetrics(SM_CXBORDER);
767 yinc += GetSystemMetrics(SM_CYBORDER);
770 MinMax.ptMaxSize.x += 2 * xinc;
771 MinMax.ptMaxSize.y += 2 * yinc;
773 lpPos = (LPINTERNALPOS)GetPropA( hwnd, atomInternalPos );
774 if( lpPos && !EMPTYPOINT(lpPos->ptMaxPos) )
775 CONV_POINT16TO32( &lpPos->ptMaxPos, &MinMax.ptMaxPosition );
776 else
778 MinMax.ptMaxPosition.x = -xinc;
779 MinMax.ptMaxPosition.y = -yinc;
782 SendMessageA( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
784 /* Some sanity checks */
786 TRACE("%ld %ld / %ld %ld / %ld %ld / %ld %ld\n",
787 MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
788 MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
789 MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
790 MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
791 MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
792 MinMax.ptMinTrackSize.x );
793 MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
794 MinMax.ptMinTrackSize.y );
796 if (maxSize) *maxSize = MinMax.ptMaxSize;
797 if (maxPos) *maxPos = MinMax.ptMaxPosition;
798 if (minTrack) *minTrack = MinMax.ptMinTrackSize;
799 if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
802 /***********************************************************************
803 * ShowWindowAsync (USER32.@)
805 * doesn't wait; returns immediately.
806 * used by threads to toggle windows in other (possibly hanging) threads
808 BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
810 HWND full_handle;
812 if (is_broadcast(hwnd))
814 SetLastError( ERROR_INVALID_PARAMETER );
815 return FALSE;
818 if ((full_handle = WIN_IsCurrentThread( hwnd )))
819 return USER_Driver.pShowWindow( full_handle, cmd );
820 return SendNotifyMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
824 /***********************************************************************
825 * ShowWindow (USER32.@)
827 BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
829 HWND full_handle;
831 if (is_broadcast(hwnd))
833 SetLastError( ERROR_INVALID_PARAMETER );
834 return FALSE;
836 if ((full_handle = WIN_IsCurrentThread( hwnd )))
838 if (USER_Driver.pShowWindow)
839 return USER_Driver.pShowWindow( full_handle, cmd );
840 return FALSE;
842 return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
846 /***********************************************************************
847 * GetInternalWindowPos (USER32.@)
849 UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
850 LPPOINT ptIcon )
852 WINDOWPLACEMENT wndpl;
853 if (GetWindowPlacement( hwnd, &wndpl ))
855 if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
856 if (ptIcon) *ptIcon = wndpl.ptMinPosition;
857 return wndpl.showCmd;
859 return 0;
863 /***********************************************************************
864 * GetWindowPlacement (USER32.@)
866 * Win95:
867 * Fails if wndpl->length of Win95 (!) apps is invalid.
869 BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
871 WND *pWnd = WIN_FindWndPtr( hwnd );
872 LPINTERNALPOS lpPos;
874 if(!pWnd ) return FALSE;
876 lpPos = WINPOS_InitInternalPos( pWnd, *(LPPOINT)&pWnd->rectWindow.left, &pWnd->rectWindow );
877 wndpl->length = sizeof(*wndpl);
878 if( pWnd->dwStyle & WS_MINIMIZE )
879 wndpl->showCmd = SW_SHOWMINIMIZED;
880 else
881 wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
882 if( pWnd->flags & WIN_RESTORE_MAX )
883 wndpl->flags = WPF_RESTORETOMAXIMIZED;
884 else
885 wndpl->flags = 0;
886 CONV_POINT16TO32( &lpPos->ptIconPos, &wndpl->ptMinPosition );
887 CONV_POINT16TO32( &lpPos->ptMaxPos, &wndpl->ptMaxPosition );
888 CONV_RECT16TO32( &lpPos->rectNormal, &wndpl->rcNormalPosition );
889 WIN_ReleaseWndPtr(pWnd);
890 return TRUE;
894 /***********************************************************************
895 * WINPOS_SetPlacement
897 static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
899 WND *pWnd = WIN_FindWndPtr( hwnd );
900 if( pWnd )
902 LPINTERNALPOS lpPos = (LPINTERNALPOS)WINPOS_InitInternalPos( pWnd,
903 *(LPPOINT)&pWnd->rectWindow.left, &pWnd->rectWindow );
905 if( flags & PLACE_MIN ) CONV_POINT32TO16( &wndpl->ptMinPosition, &lpPos->ptIconPos );
906 if( flags & PLACE_MAX ) CONV_POINT32TO16( &wndpl->ptMaxPosition, &lpPos->ptMaxPos );
907 if( flags & PLACE_RECT) CONV_RECT32TO16( &wndpl->rcNormalPosition, &lpPos->rectNormal );
909 if( pWnd->dwStyle & WS_MINIMIZE )
911 WINPOS_ShowIconTitle( pWnd->hwndSelf, FALSE );
912 if( wndpl->flags & WPF_SETMINPOSITION && !EMPTYPOINT(lpPos->ptIconPos))
913 SetWindowPos( hwnd, 0, lpPos->ptIconPos.x, lpPos->ptIconPos.y,
914 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
916 else if( pWnd->dwStyle & WS_MAXIMIZE )
918 if( !EMPTYPOINT(lpPos->ptMaxPos) )
919 SetWindowPos( hwnd, 0, lpPos->ptMaxPos.x, lpPos->ptMaxPos.y,
920 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
922 else if( flags & PLACE_RECT )
923 SetWindowPos( hwnd, 0, lpPos->rectNormal.left, lpPos->rectNormal.top,
924 lpPos->rectNormal.right - lpPos->rectNormal.left,
925 lpPos->rectNormal.bottom - lpPos->rectNormal.top,
926 SWP_NOZORDER | SWP_NOACTIVATE );
928 ShowWindow( hwnd, wndpl->showCmd );
929 if( IsWindow(hwnd) && pWnd->dwStyle & WS_MINIMIZE )
931 if( pWnd->dwStyle & WS_VISIBLE ) WINPOS_ShowIconTitle( pWnd->hwndSelf, TRUE );
933 /* SDK: ...valid only the next time... */
934 if( wndpl->flags & WPF_RESTORETOMAXIMIZED ) pWnd->flags |= WIN_RESTORE_MAX;
936 WIN_ReleaseWndPtr(pWnd);
937 return TRUE;
939 return FALSE;
943 /***********************************************************************
944 * SetWindowPlacement (USER32.@)
946 * Win95:
947 * Fails if wndpl->length of Win95 (!) apps is invalid.
949 BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
951 if (!wpl) return FALSE;
952 return WINPOS_SetPlacement( hwnd, wpl, PLACE_MIN | PLACE_MAX | PLACE_RECT );
956 /***********************************************************************
957 * AnimateWindow (USER32.@)
958 * Shows/Hides a window with an animation
959 * NO ANIMATION YET
961 BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
963 FIXME("partial stub\n");
965 /* If trying to show/hide and it's already *
966 * shown/hidden or invalid window, fail with *
967 * invalid parameter */
968 if(!IsWindow(hwnd) ||
969 (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
970 (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
972 SetLastError(ERROR_INVALID_PARAMETER);
973 return FALSE;
976 ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));
978 return TRUE;
981 /***********************************************************************
982 * SetInternalWindowPos (USER32.@)
984 void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
985 LPRECT rect, LPPOINT pt )
987 if( IsWindow(hwnd) )
989 WINDOWPLACEMENT wndpl;
990 UINT flags;
992 wndpl.length = sizeof(wndpl);
993 wndpl.showCmd = showCmd;
994 wndpl.flags = flags = 0;
996 if( pt )
998 flags |= PLACE_MIN;
999 wndpl.flags |= WPF_SETMINPOSITION;
1000 wndpl.ptMinPosition = *pt;
1002 if( rect )
1004 flags |= PLACE_RECT;
1005 wndpl.rcNormalPosition = *rect;
1007 WINPOS_SetPlacement( hwnd, &wndpl, flags );
1012 /*******************************************************************
1013 * can_activate_window
1015 * Check if we can activate the specified window.
1017 static BOOL can_activate_window( HWND hwnd )
1019 LONG style;
1021 if (!hwnd) return FALSE;
1022 style = GetWindowLongW( hwnd, GWL_STYLE );
1023 if (!(style & WS_VISIBLE)) return FALSE;
1024 if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
1025 return !(style & WS_DISABLED);
1029 /*******************************************************************
1030 * WINPOS_ActivateOtherWindow
1032 * Activates window other than pWnd.
1034 void WINPOS_ActivateOtherWindow(HWND hwnd)
1036 HWND hwndTo, fg;
1038 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1040 hwndTo = GetAncestor( hwndTo, GA_ROOT );
1041 if (can_activate_window( hwndTo )) goto done;
1044 hwndTo = hwnd;
1045 for (;;)
1047 if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
1048 if (can_activate_window( hwndTo )) break;
1051 done:
1052 fg = GetForegroundWindow();
1053 TRACE("win = %p fg = %p\n", hwndTo, fg);
1054 if (!fg || (hwnd == fg))
1056 if (SetForegroundWindow( hwndTo )) return;
1058 if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
1062 /***********************************************************************
1063 * WINPOS_HandleWindowPosChanging16
1065 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1067 LONG WINPOS_HandleWindowPosChanging16( HWND hwnd, WINDOWPOS16 *winpos )
1069 POINT minTrack, maxTrack;
1070 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1072 if (winpos->flags & SWP_NOSIZE) return 0;
1073 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1075 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1076 if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1077 if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1078 if (!(style & WS_MINIMIZE))
1080 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1081 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1084 return 0;
1088 /***********************************************************************
1089 * WINPOS_HandleWindowPosChanging
1091 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
1093 LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
1095 POINT minTrack, maxTrack;
1096 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1098 if (winpos->flags & SWP_NOSIZE) return 0;
1099 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
1101 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1102 if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
1103 if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1104 if (!(style & WS_MINIMIZE))
1106 if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
1107 if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
1110 return 0;
1114 /***********************************************************************
1115 * dump_winpos_flags
1117 static void dump_winpos_flags(UINT flags)
1119 TRACE("flags:");
1120 if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
1121 if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
1122 if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
1123 if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
1124 if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
1125 if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
1126 if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
1127 if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
1128 if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
1129 if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
1130 if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
1131 if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
1132 if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1134 #define DUMPED_FLAGS \
1135 (SWP_NOSIZE | \
1136 SWP_NOMOVE | \
1137 SWP_NOZORDER | \
1138 SWP_NOREDRAW | \
1139 SWP_NOACTIVATE | \
1140 SWP_FRAMECHANGED | \
1141 SWP_SHOWWINDOW | \
1142 SWP_HIDEWINDOW | \
1143 SWP_NOCOPYBITS | \
1144 SWP_NOOWNERZORDER | \
1145 SWP_NOSENDCHANGING | \
1146 SWP_DEFERERASE | \
1147 SWP_ASYNCWINDOWPOS)
1149 if(flags & ~DUMPED_FLAGS) TRACE(" %08x", flags & ~DUMPED_FLAGS);
1150 TRACE("\n");
1151 #undef DUMPED_FLAGS
1154 /***********************************************************************
1155 * SetWindowPos (USER32.@)
1157 BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
1158 INT x, INT y, INT cx, INT cy, UINT flags )
1160 WINDOWPOS winpos;
1162 TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1163 hwnd, hwndInsertAfter, x, y, cx, cy, flags);
1164 if(TRACE_ON(win)) dump_winpos_flags(flags);
1166 if (is_broadcast(hwnd))
1168 SetLastError( ERROR_INVALID_PARAMETER );
1169 return FALSE;
1172 winpos.hwnd = WIN_GetFullHandle(hwnd);
1173 winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
1174 winpos.x = x;
1175 winpos.y = y;
1176 winpos.cx = cx;
1177 winpos.cy = cy;
1178 winpos.flags = flags;
1179 if (WIN_IsCurrentThread( hwnd )) return USER_Driver.pSetWindowPos( &winpos );
1180 return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
1184 /***********************************************************************
1185 * BeginDeferWindowPos (USER32.@)
1187 HDWP WINAPI BeginDeferWindowPos( INT count )
1189 HDWP handle;
1190 DWP *pDWP;
1192 TRACE("%d\n", count);
1194 if (count < 0)
1196 SetLastError(ERROR_INVALID_PARAMETER);
1197 return 0;
1199 /* Windows allows zero count, in which case it allocates context for 8 moves */
1200 if (count == 0) count = 8;
1202 handle = USER_HEAP_ALLOC( sizeof(DWP) + (count-1)*sizeof(WINDOWPOS) );
1203 if (!handle) return 0;
1204 pDWP = (DWP *) USER_HEAP_LIN_ADDR( handle );
1205 pDWP->actualCount = 0;
1206 pDWP->suggestedCount = count;
1207 pDWP->valid = TRUE;
1208 pDWP->wMagic = DWP_MAGIC;
1209 pDWP->hwndParent = 0;
1211 TRACE("returning hdwp %p\n", handle);
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 TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
1228 hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);
1230 hwnd = WIN_GetFullHandle( hwnd );
1231 if (hwnd == GetDesktopWindow()) return 0;
1233 if (!(pDWP = USER_HEAP_LIN_ADDR( hdwp ))) return 0;
1235 USER_Lock();
1237 for (i = 0; i < pDWP->actualCount; i++)
1239 if (pDWP->winPos[i].hwnd == hwnd)
1241 /* Merge with the other changes */
1242 if (!(flags & SWP_NOZORDER))
1244 pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
1246 if (!(flags & SWP_NOMOVE))
1248 pDWP->winPos[i].x = x;
1249 pDWP->winPos[i].y = y;
1251 if (!(flags & SWP_NOSIZE))
1253 pDWP->winPos[i].cx = cx;
1254 pDWP->winPos[i].cy = cy;
1256 pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
1257 SWP_NOZORDER | SWP_NOREDRAW |
1258 SWP_NOACTIVATE | SWP_NOCOPYBITS|
1259 SWP_NOOWNERZORDER);
1260 pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
1261 SWP_FRAMECHANGED);
1262 retvalue = hdwp;
1263 goto END;
1266 if (pDWP->actualCount >= pDWP->suggestedCount)
1268 newhdwp = USER_HEAP_REALLOC( hdwp,
1269 sizeof(DWP) + pDWP->suggestedCount*sizeof(WINDOWPOS) );
1270 if (!newhdwp)
1272 retvalue = 0;
1273 goto END;
1275 pDWP = (DWP *) USER_HEAP_LIN_ADDR( newhdwp );
1276 pDWP->suggestedCount++;
1278 pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
1279 pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
1280 pDWP->winPos[pDWP->actualCount].x = x;
1281 pDWP->winPos[pDWP->actualCount].y = y;
1282 pDWP->winPos[pDWP->actualCount].cx = cx;
1283 pDWP->winPos[pDWP->actualCount].cy = cy;
1284 pDWP->winPos[pDWP->actualCount].flags = flags;
1285 pDWP->actualCount++;
1286 retvalue = newhdwp;
1287 END:
1288 USER_Unlock();
1289 return retvalue;
1293 /***********************************************************************
1294 * EndDeferWindowPos (USER32.@)
1296 BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
1298 DWP *pDWP;
1299 WINDOWPOS *winpos;
1300 BOOL res = TRUE;
1301 int i;
1303 TRACE("%p\n", hdwp);
1305 pDWP = (DWP *) USER_HEAP_LIN_ADDR( hdwp );
1306 if (!pDWP) return FALSE;
1307 for (i = 0, winpos = pDWP->winPos; i < pDWP->actualCount; i++, winpos++)
1309 if (!(res = USER_Driver.pSetWindowPos( winpos ))) break;
1311 USER_HEAP_FREE( hdwp );
1312 return res;
1316 /***********************************************************************
1317 * TileChildWindows (USER.199)
1319 void WINAPI TileChildWindows16( HWND16 parent, WORD action )
1321 FIXME("(%04x, %d): stub\n", parent, action);
1324 /***********************************************************************
1325 * CascadeChildWindows (USER.198)
1327 void WINAPI CascadeChildWindows16( HWND16 parent, WORD action )
1329 FIXME("(%04x, %d): stub\n", parent, action);