2 * Window related functions
4 * Copyright 1993, 1994 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
30 #include "wine/winbase16.h"
31 #include "wine/winuser16.h"
33 #include "wine/server.h"
34 #include "wine/unicode.h"
36 #include "user_private.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(win);
43 #define NB_USER_HANDLES ((LAST_USER_HANDLE - FIRST_USER_HANDLE + 1) >> 1)
44 #define USER_HANDLE_TO_INDEX(hwnd) ((LOWORD(hwnd) - FIRST_USER_HANDLE) >> 1)
46 /**********************************************************************/
48 /* helper for Get/SetWindowLong */
49 static inline LONG_PTR get_win_data( const void *ptr, UINT size )
51 if (size == sizeof(WORD))
54 memcpy( &ret, ptr, sizeof(ret) );
57 else if (size == sizeof(DWORD))
60 memcpy( &ret, ptr, sizeof(ret) );
66 memcpy( &ret, ptr, sizeof(ret) );
71 /* helper for Get/SetWindowLong */
72 static inline void set_win_data( void *ptr, LONG_PTR val, UINT size )
74 if (size == sizeof(WORD))
77 memcpy( ptr, &newval, sizeof(newval) );
79 else if (size == sizeof(DWORD))
82 memcpy( ptr, &newval, sizeof(newval) );
86 memcpy( ptr, &val, sizeof(val) );
91 static void *user_handles[NB_USER_HANDLES];
93 /***********************************************************************
94 * create_window_handle
96 * Create a window handle with the server.
98 static WND *create_window_handle( HWND parent, HWND owner, LPCWSTR name,
99 HINSTANCE instance, BOOL unicode )
103 HWND full_parent = 0, full_owner = 0;
104 struct tagCLASS *class = NULL;
105 user_handle_t handle = 0;
108 /* if 16-bit instance, map to module handle */
109 if (instance && !HIWORD(instance))
110 instance = HINSTANCE_32(GetExePtr(HINSTANCE_16(instance)));
112 SERVER_START_REQ( create_window )
114 req->parent = parent;
116 req->instance = instance;
117 if (!(req->atom = get_int_atom_value( name )) && name)
118 wine_server_add_data( req, name, strlenW(name)*sizeof(WCHAR) );
119 if (!wine_server_call_err( req ))
121 handle = reply->handle;
122 full_parent = reply->parent;
123 full_owner = reply->owner;
124 extra_bytes = reply->extra;
125 class = reply->class_ptr;
132 WARN( "error %d creating window\n", GetLastError() );
136 if (!(win = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
137 sizeof(WND) + extra_bytes - sizeof(win->wExtra) )))
139 SERVER_START_REQ( destroy_window )
141 req->handle = handle;
142 wine_server_call( req );
145 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
149 if (!parent) /* if parent is 0 we don't have a desktop window yet */
151 struct user_thread_info *thread_info = get_user_thread_info();
153 if (!thread_info->desktop) thread_info->desktop = full_parent ? full_parent : handle;
154 else assert( full_parent == thread_info->desktop );
155 if (full_parent && !USER_Driver->pCreateDesktopWindow( thread_info->desktop ))
156 ERR( "failed to create desktop window\n" );
161 index = USER_HANDLE_TO_INDEX(handle);
162 assert( index < NB_USER_HANDLES );
163 user_handles[index] = win;
164 win->hwndSelf = handle;
165 win->parent = full_parent;
166 win->owner = full_owner;
168 win->winproc = get_class_winproc( class );
169 win->dwMagic = WND_MAGIC;
170 win->cbWndExtra = extra_bytes;
171 if (WINPROC_IsUnicode( win->winproc, unicode )) win->flags |= WIN_ISUNICODE;
176 /***********************************************************************
179 * Free a window handle.
181 static WND *free_window_handle( HWND hwnd )
184 WORD index = USER_HANDLE_TO_INDEX(hwnd);
186 if (index >= NB_USER_HANDLES) return NULL;
188 if ((ptr = user_handles[index]))
190 SERVER_START_REQ( destroy_window )
193 if (!wine_server_call_err( req ))
195 user_handles[index] = NULL;
204 HeapFree( GetProcessHeap(), 0, ptr );
209 /*******************************************************************
210 * list_window_children
212 * Build an array of the children of a given window. The array must be
213 * freed with HeapFree. Returns NULL when no windows are found.
215 static HWND *list_window_children( HDESK desktop, HWND hwnd, LPCWSTR class, DWORD tid )
224 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
226 SERVER_START_REQ( get_window_children )
228 req->desktop = desktop;
231 if (!(req->atom = get_int_atom_value( class )) && class)
232 wine_server_add_data( req, class, strlenW(class)*sizeof(WCHAR) );
233 wine_server_set_reply( req, list, (size-1) * sizeof(HWND) );
234 if (!wine_server_call( req )) count = reply->count;
237 if (count && count < size)
242 HeapFree( GetProcessHeap(), 0, list );
244 size = count + 1; /* restart with a large enough buffer */
250 /*******************************************************************
251 * list_window_parents
253 * Build an array of all parents of a given window, starting with
254 * the immediate parent. The array must be freed with HeapFree.
256 static HWND *list_window_parents( HWND hwnd )
260 int pos = 0, size = 16, count = 0;
262 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) return NULL;
267 if (!(win = WIN_GetPtr( current ))) goto empty;
268 if (win == WND_OTHER_PROCESS) break; /* need to do it the hard way */
269 if (win == WND_DESKTOP)
271 if (!pos) goto empty;
275 list[pos] = current = win->parent;
276 WIN_ReleasePtr( win );
277 if (!current) return list;
278 if (++pos == size - 1)
280 /* need to grow the list */
281 HWND *new_list = HeapReAlloc( GetProcessHeap(), 0, list, (size+16) * sizeof(HWND) );
282 if (!new_list) goto empty;
288 /* at least one parent belongs to another process, have to query the server */
293 SERVER_START_REQ( get_window_parents )
296 wine_server_set_reply( req, list, (size-1) * sizeof(HWND) );
297 if (!wine_server_call( req )) count = reply->count;
300 if (!count) goto empty;
306 HeapFree( GetProcessHeap(), 0, list );
308 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) return NULL;
312 HeapFree( GetProcessHeap(), 0, list );
317 /*******************************************************************
320 static void send_parent_notify( HWND hwnd, UINT msg )
322 if ((GetWindowLongW( hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD &&
323 !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY))
325 HWND parent = GetParent(hwnd);
326 if (parent && parent != GetDesktopWindow())
327 SendMessageW( parent, WM_PARENTNOTIFY,
328 MAKEWPARAM( msg, GetWindowLongPtrW( hwnd, GWLP_ID )), (LPARAM)hwnd );
333 /*******************************************************************
334 * get_server_window_text
336 * Retrieve the window text from the server.
338 static void get_server_window_text( HWND hwnd, LPWSTR text, INT count )
342 SERVER_START_REQ( get_window_text )
345 wine_server_set_reply( req, text, (count - 1) * sizeof(WCHAR) );
346 if (!wine_server_call_err( req )) len = wine_server_reply_size(reply);
349 text[len / sizeof(WCHAR)] = 0;
353 /***********************************************************************
356 * Return a pointer to the WND structure if local to the process,
357 * or WND_OTHER_PROCESS if handle may be valid in other process.
358 * If ret value is a valid pointer, it must be released with WIN_ReleasePtr.
360 WND *WIN_GetPtr( HWND hwnd )
363 WORD index = USER_HANDLE_TO_INDEX(hwnd);
365 if (index >= NB_USER_HANDLES) return NULL;
368 if ((ptr = user_handles[index]))
370 if (ptr->dwMagic == WND_MAGIC &&
371 (hwnd == ptr->hwndSelf || !HIWORD(hwnd) || HIWORD(hwnd) == 0xffff))
375 else if (index == USER_HANDLE_TO_INDEX(GetDesktopWindow()))
377 if (hwnd == GetDesktopWindow() || !HIWORD(hwnd) || HIWORD(hwnd) == 0xffff) ptr = WND_DESKTOP;
380 else ptr = WND_OTHER_PROCESS;
386 /***********************************************************************
387 * WIN_IsCurrentProcess
389 * Check whether a given window belongs to the current process (and return the full handle).
391 HWND WIN_IsCurrentProcess( HWND hwnd )
396 if (!(ptr = WIN_GetPtr( hwnd )) || ptr == WND_OTHER_PROCESS || ptr == WND_DESKTOP) return 0;
398 WIN_ReleasePtr( ptr );
403 /***********************************************************************
404 * WIN_IsCurrentThread
406 * Check whether a given window belongs to the current thread (and return the full handle).
408 HWND WIN_IsCurrentThread( HWND hwnd )
413 if (!(ptr = WIN_GetPtr( hwnd )) || ptr == WND_OTHER_PROCESS || ptr == WND_DESKTOP) return 0;
414 if (ptr->tid == GetCurrentThreadId()) ret = ptr->hwndSelf;
415 WIN_ReleasePtr( ptr );
420 /***********************************************************************
423 * Convert a 16-bit window handle to a full 32-bit handle.
425 HWND WIN_Handle32( HWND16 hwnd16 )
428 HWND hwnd = (HWND)(ULONG_PTR)hwnd16;
430 if (hwnd16 <= 1 || hwnd16 == 0xffff) return hwnd;
431 /* do sign extension for -2 and -3 */
432 if (hwnd16 >= (HWND16)-3) return (HWND)(LONG_PTR)(INT16)hwnd16;
434 if (!(ptr = WIN_GetPtr( hwnd ))) return hwnd;
436 if (ptr == WND_DESKTOP) return GetDesktopWindow();
438 if (ptr != WND_OTHER_PROCESS)
440 hwnd = ptr->hwndSelf;
441 WIN_ReleasePtr( ptr );
443 else /* may belong to another process */
445 SERVER_START_REQ( get_window_info )
448 if (!wine_server_call_err( req )) hwnd = reply->full_handle;
456 /***********************************************************************
459 * Change the owner of a window.
461 HWND WIN_SetOwner( HWND hwnd, HWND owner )
463 WND *win = WIN_GetPtr( hwnd );
466 if (!win || win == WND_DESKTOP) return 0;
467 if (win == WND_OTHER_PROCESS)
469 if (IsWindow(hwnd)) ERR( "cannot set owner %p on other process window %p\n", owner, hwnd );
472 SERVER_START_REQ( set_window_owner )
476 if (!wine_server_call( req ))
478 win->owner = reply->full_owner;
479 ret = reply->prev_owner;
483 WIN_ReleasePtr( win );
488 /***********************************************************************
491 * Change the style of a window.
493 ULONG WIN_SetStyle( HWND hwnd, ULONG set_bits, ULONG clear_bits )
496 ULONG new_style, old_style = 0;
497 WND *win = WIN_GetPtr( hwnd );
499 if (!win || win == WND_DESKTOP) return 0;
500 if (win == WND_OTHER_PROCESS)
503 ERR( "cannot set style %x/%x on other process window %p\n",
504 set_bits, clear_bits, hwnd );
507 new_style = (win->dwStyle | set_bits) & ~clear_bits;
508 if (new_style == win->dwStyle)
510 WIN_ReleasePtr( win );
513 SERVER_START_REQ( set_window_info )
516 req->flags = SET_WIN_STYLE;
517 req->style = new_style;
518 req->extra_offset = -1;
519 if ((ok = !wine_server_call( req )))
521 old_style = reply->old_style;
522 win->dwStyle = new_style;
526 WIN_ReleasePtr( win );
529 USER_Driver->pSetWindowStyle( hwnd, old_style );
530 if ((old_style ^ new_style) & WS_VISIBLE) invalidate_dce( hwnd, NULL );
536 /***********************************************************************
539 * Get the window and client rectangles.
541 BOOL WIN_GetRectangles( HWND hwnd, RECT *rectWindow, RECT *rectClient )
543 WND *win = WIN_GetPtr( hwnd );
546 if (!win) return FALSE;
547 if (win == WND_DESKTOP)
550 rect.left = rect.top = 0;
551 rect.right = GetSystemMetrics(SM_CXSCREEN);
552 rect.bottom = GetSystemMetrics(SM_CYSCREEN);
553 if (rectWindow) *rectWindow = rect;
554 if (rectClient) *rectClient = rect;
556 else if (win == WND_OTHER_PROCESS)
558 SERVER_START_REQ( get_window_rectangles )
561 if ((ret = !wine_server_call( req )))
565 rectWindow->left = reply->window.left;
566 rectWindow->top = reply->window.top;
567 rectWindow->right = reply->window.right;
568 rectWindow->bottom = reply->window.bottom;
572 rectClient->left = reply->client.left;
573 rectClient->top = reply->client.top;
574 rectClient->right = reply->client.right;
575 rectClient->bottom = reply->client.bottom;
583 if (rectWindow) *rectWindow = win->rectWindow;
584 if (rectClient) *rectClient = win->rectClient;
585 WIN_ReleasePtr( win );
591 /***********************************************************************
594 * Destroy storage associated to a window. "Internals" p.358
596 LRESULT WIN_DestroyWindow( HWND hwnd )
600 HMENU menu = 0, sys_menu;
603 TRACE("%p\n", hwnd );
605 /* free child windows */
606 if ((list = WIN_ListChildren( hwnd )))
609 for (i = 0; list[i]; i++)
611 if (WIN_IsCurrentThread( list[i] )) WIN_DestroyWindow( list[i] );
612 else SendMessageW( list[i], WM_WINE_DESTROYWINDOW, 0, 0 );
614 HeapFree( GetProcessHeap(), 0, list );
617 /* Unlink now so we won't bother with the children later on */
618 SERVER_START_REQ( set_parent )
622 wine_server_call( req );
627 * Send the WM_NCDESTROY to the window being destroyed.
629 SendMessageW( hwnd, WM_NCDESTROY, 0, 0 );
631 /* FIXME: do we need to fake QS_MOUSEMOVE wakebit? */
633 /* free resources associated with the window */
635 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
636 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
637 menu = (HMENU)wndPtr->wIDmenu;
638 sys_menu = wndPtr->hSysMenu;
639 free_dce( wndPtr->dce, hwnd );
641 icon_title = wndPtr->icon_title;
642 WIN_ReleasePtr( wndPtr );
644 if (icon_title) DestroyWindow( icon_title );
645 if (menu) DestroyMenu( menu );
646 if (sys_menu) DestroyMenu( sys_menu );
648 USER_Driver->pDestroyWindow( hwnd );
650 free_window_handle( hwnd );
654 /***********************************************************************
655 * WIN_DestroyThreadWindows
657 * Destroy all children of 'wnd' owned by the current thread.
659 void WIN_DestroyThreadWindows( HWND hwnd )
664 if (!(list = WIN_ListChildren( hwnd ))) return;
665 for (i = 0; list[i]; i++)
667 if (WIN_IsCurrentThread( list[i] ))
668 DestroyWindow( list[i] );
670 WIN_DestroyThreadWindows( list[i] );
672 HeapFree( GetProcessHeap(), 0, list );
676 /***********************************************************************
679 * Fix the coordinates - Helper for WIN_CreateWindowEx.
680 * returns default show mode in sw.
682 static void WIN_FixCoordinates( CREATESTRUCTA *cs, INT *sw)
684 #define IS_DEFAULT(x) ((x) == CW_USEDEFAULT || (x) == CW_USEDEFAULT16)
687 if (cs->dwExStyle & WS_EX_MDICHILD)
691 MDI_CalcDefaultChildPos(cs->hwndParent, -1, pos, 0, &id);
692 if (!(cs->style & WS_POPUP)) cs->hMenu = ULongToHandle(id);
694 TRACE("MDI child id %04x\n", id);
697 if (cs->style & (WS_CHILD | WS_POPUP))
699 if (cs->dwExStyle & WS_EX_MDICHILD)
701 if (IS_DEFAULT(cs->x))
706 if (IS_DEFAULT(cs->cx) || !cs->cx) cs->cx = pos[1].x;
707 if (IS_DEFAULT(cs->cy) || !cs->cy) cs->cy = pos[1].y;
711 if (IS_DEFAULT(cs->x)) cs->x = cs->y = 0;
712 if (IS_DEFAULT(cs->cx)) cs->cx = cs->cy = 0;
715 else /* overlapped window */
718 MONITORINFO mon_info;
721 if (!IS_DEFAULT(cs->x) && !IS_DEFAULT(cs->cx) && !IS_DEFAULT(cs->cy)) return;
723 monitor = MonitorFromWindow( cs->hwndParent, MONITOR_DEFAULTTOPRIMARY );
724 mon_info.cbSize = sizeof(mon_info);
725 GetMonitorInfoW( monitor, &mon_info );
726 GetStartupInfoW( &info );
728 if (IS_DEFAULT(cs->x))
730 if (!IS_DEFAULT(cs->y)) *sw = cs->y;
731 cs->x = (info.dwFlags & STARTF_USEPOSITION) ? info.dwX : mon_info.rcWork.left;
732 cs->y = (info.dwFlags & STARTF_USEPOSITION) ? info.dwY : mon_info.rcWork.top;
735 if (IS_DEFAULT(cs->cx))
737 if (info.dwFlags & STARTF_USESIZE)
739 cs->cx = info.dwXSize;
740 cs->cy = info.dwYSize;
744 cs->cx = (mon_info.rcWork.right - mon_info.rcWork.left) * 3 / 4 - cs->x;
745 cs->cy = (mon_info.rcWork.bottom - mon_info.rcWork.top) * 3 / 4 - cs->y;
748 /* neither x nor cx are default. Check the y values .
749 * In the trace we see Outlook and Outlook Express using
750 * cy set to CW_USEDEFAULT when opening the address book.
752 else if (IS_DEFAULT(cs->cy))
754 FIXME("Strange use of CW_USEDEFAULT in nHeight\n");
755 cs->cy = (mon_info.rcWork.bottom - mon_info.rcWork.top) * 3 / 4 - cs->y;
761 /***********************************************************************
764 static void dump_window_styles( DWORD style, DWORD exstyle )
767 if(style & WS_POPUP) TRACE(" WS_POPUP");
768 if(style & WS_CHILD) TRACE(" WS_CHILD");
769 if(style & WS_MINIMIZE) TRACE(" WS_MINIMIZE");
770 if(style & WS_VISIBLE) TRACE(" WS_VISIBLE");
771 if(style & WS_DISABLED) TRACE(" WS_DISABLED");
772 if(style & WS_CLIPSIBLINGS) TRACE(" WS_CLIPSIBLINGS");
773 if(style & WS_CLIPCHILDREN) TRACE(" WS_CLIPCHILDREN");
774 if(style & WS_MAXIMIZE) TRACE(" WS_MAXIMIZE");
775 if((style & WS_CAPTION) == WS_CAPTION) TRACE(" WS_CAPTION");
778 if(style & WS_BORDER) TRACE(" WS_BORDER");
779 if(style & WS_DLGFRAME) TRACE(" WS_DLGFRAME");
781 if(style & WS_VSCROLL) TRACE(" WS_VSCROLL");
782 if(style & WS_HSCROLL) TRACE(" WS_HSCROLL");
783 if(style & WS_SYSMENU) TRACE(" WS_SYSMENU");
784 if(style & WS_THICKFRAME) TRACE(" WS_THICKFRAME");
785 if (style & WS_CHILD)
787 if(style & WS_GROUP) TRACE(" WS_GROUP");
788 if(style & WS_TABSTOP) TRACE(" WS_TABSTOP");
792 if(style & WS_MINIMIZEBOX) TRACE(" WS_MINIMIZEBOX");
793 if(style & WS_MAXIMIZEBOX) TRACE(" WS_MAXIMIZEBOX");
796 /* FIXME: Add dumping of BS_/ES_/SBS_/LBS_/CBS_/DS_/etc. styles */
797 #define DUMPED_STYLES \
817 if(style & ~DUMPED_STYLES) TRACE(" %08lx", style & ~DUMPED_STYLES);
822 if(exstyle & WS_EX_DLGMODALFRAME) TRACE(" WS_EX_DLGMODALFRAME");
823 if(exstyle & WS_EX_DRAGDETECT) TRACE(" WS_EX_DRAGDETECT");
824 if(exstyle & WS_EX_NOPARENTNOTIFY) TRACE(" WS_EX_NOPARENTNOTIFY");
825 if(exstyle & WS_EX_TOPMOST) TRACE(" WS_EX_TOPMOST");
826 if(exstyle & WS_EX_ACCEPTFILES) TRACE(" WS_EX_ACCEPTFILES");
827 if(exstyle & WS_EX_TRANSPARENT) TRACE(" WS_EX_TRANSPARENT");
828 if(exstyle & WS_EX_MDICHILD) TRACE(" WS_EX_MDICHILD");
829 if(exstyle & WS_EX_TOOLWINDOW) TRACE(" WS_EX_TOOLWINDOW");
830 if(exstyle & WS_EX_WINDOWEDGE) TRACE(" WS_EX_WINDOWEDGE");
831 if(exstyle & WS_EX_CLIENTEDGE) TRACE(" WS_EX_CLIENTEDGE");
832 if(exstyle & WS_EX_CONTEXTHELP) TRACE(" WS_EX_CONTEXTHELP");
833 if(exstyle & WS_EX_RIGHT) TRACE(" WS_EX_RIGHT");
834 if(exstyle & WS_EX_RTLREADING) TRACE(" WS_EX_RTLREADING");
835 if(exstyle & WS_EX_LEFTSCROLLBAR) TRACE(" WS_EX_LEFTSCROLLBAR");
836 if(exstyle & WS_EX_CONTROLPARENT) TRACE(" WS_EX_CONTROLPARENT");
837 if(exstyle & WS_EX_STATICEDGE) TRACE(" WS_EX_STATICEDGE");
838 if(exstyle & WS_EX_APPWINDOW) TRACE(" WS_EX_APPWINDOW");
839 if(exstyle & WS_EX_LAYERED) TRACE(" WS_EX_LAYERED");
841 #define DUMPED_EX_STYLES \
842 (WS_EX_DLGMODALFRAME | \
844 WS_EX_NOPARENTNOTIFY | \
846 WS_EX_ACCEPTFILES | \
847 WS_EX_TRANSPARENT | \
852 WS_EX_CONTEXTHELP | \
855 WS_EX_LEFTSCROLLBAR | \
856 WS_EX_CONTROLPARENT | \
861 if(exstyle & ~DUMPED_EX_STYLES) TRACE(" %08lx", exstyle & ~DUMPED_EX_STYLES);
863 #undef DUMPED_EX_STYLES
867 /***********************************************************************
870 * Implementation of CreateWindowEx().
872 static HWND WIN_CreateWindowEx( CREATESTRUCTA *cs, LPCWSTR className, UINT flags )
874 INT cx, cy, style, sw = SW_SHOW;
878 HWND hwnd, parent, owner, top_child = 0;
879 BOOL unicode = (flags & WIN_ISUNICODE) != 0;
880 MDICREATESTRUCTA mdi_cs;
884 TRACE("%s %s ex=%08x style=%08x %d,%d %dx%d parent=%p menu=%p inst=%p params=%p\n",
885 unicode ? debugstr_w((LPCWSTR)cs->lpszName) : debugstr_a(cs->lpszName),
886 debugstr_w(className),
887 cs->dwExStyle, cs->style, cs->x, cs->y, cs->cx, cs->cy,
888 cs->hwndParent, cs->hMenu, cs->hInstance, cs->lpCreateParams );
889 if(TRACE_ON(win)) dump_window_styles( cs->style, cs->dwExStyle );
891 /* Fix the styles for MDI children */
892 if (cs->dwExStyle & WS_EX_MDICHILD)
896 wndPtr = WIN_GetPtr(cs->hwndParent);
897 if (wndPtr && wndPtr != WND_OTHER_PROCESS && wndPtr != WND_DESKTOP)
899 flags = wndPtr->flags;
900 WIN_ReleasePtr(wndPtr);
903 if (!(flags & WIN_ISMDICLIENT))
905 WARN("WS_EX_MDICHILD, but parent %p is not MDIClient\n", cs->hwndParent);
909 /* cs->lpCreateParams of WM_[NC]CREATE is different for MDI children.
910 * MDICREATESTRUCT members have the originally passed values.
912 * Note: we rely on the fact that MDICREATESTRUCTA and MDICREATESTRUCTW
913 * have the same layout.
915 mdi_cs.szClass = cs->lpszClass;
916 mdi_cs.szTitle = cs->lpszName;
917 mdi_cs.hOwner = cs->hInstance;
922 mdi_cs.style = cs->style;
923 mdi_cs.lParam = (LPARAM)cs->lpCreateParams;
925 cs->lpCreateParams = (LPVOID)&mdi_cs;
927 if (GetWindowLongW(cs->hwndParent, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
929 if (cs->style & WS_POPUP)
931 TRACE("WS_POPUP with MDIS_ALLCHILDSTYLES is not allowed\n");
934 cs->style |= WS_CHILD | WS_CLIPSIBLINGS;
938 cs->style &= ~WS_POPUP;
939 cs->style |= WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CAPTION |
940 WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
943 top_child = GetWindow(cs->hwndParent, GW_CHILD);
947 /* Restore current maximized child */
948 if((cs->style & WS_VISIBLE) && IsZoomed(top_child))
950 TRACE("Restoring current maximized child %p\n", top_child);
951 SendMessageW( top_child, WM_SETREDRAW, FALSE, 0 );
952 ShowWindow( top_child, SW_SHOWNORMAL );
953 SendMessageW( top_child, WM_SETREDRAW, TRUE, 0 );
958 /* Find the parent window */
960 parent = cs->hwndParent;
963 if (cs->hwndParent == HWND_MESSAGE)
965 /* native ole32.OleInitialize uses HWND_MESSAGE to create the
966 * message window (style: WS_POPUP|WS_DISABLED)
968 FIXME("Parent is HWND_MESSAGE\n");
969 parent = GetDesktopWindow();
971 else if (cs->hwndParent)
973 if ((cs->style & (WS_CHILD|WS_POPUP)) != WS_CHILD)
975 parent = GetDesktopWindow();
976 owner = cs->hwndParent;
981 if ((cs->style & (WS_CHILD|WS_POPUP)) == WS_CHILD)
983 WARN("No parent for child window\n" );
984 SetLastError(ERROR_TLW_WITH_WSCHILD);
985 return 0; /* WS_CHILD needs a parent, but WS_POPUP doesn't */
987 if (className != (LPCWSTR)DESKTOP_CLASS_ATOM) /* are we creating the desktop itself? */
988 parent = GetDesktopWindow();
991 WIN_FixCoordinates(cs, &sw); /* fix default coordinates */
993 if ((cs->dwExStyle & WS_EX_DLGMODALFRAME) ||
994 ((!(cs->dwExStyle & WS_EX_STATICEDGE)) &&
995 (cs->style & (WS_DLGFRAME | WS_THICKFRAME))))
996 cs->dwExStyle |= WS_EX_WINDOWEDGE;
998 cs->dwExStyle &= ~WS_EX_WINDOWEDGE;
1000 /* Create the window structure */
1002 if (!(wndPtr = create_window_handle( parent, owner, className, cs->hInstance, unicode )))
1004 hwnd = wndPtr->hwndSelf;
1006 /* Fill the window structure */
1008 wndPtr->tid = GetCurrentThreadId();
1009 wndPtr->hInstance = cs->hInstance;
1010 wndPtr->text = NULL;
1011 wndPtr->dwStyle = cs->style & ~WS_VISIBLE;
1012 wndPtr->dwExStyle = cs->dwExStyle;
1013 wndPtr->wIDmenu = 0;
1014 wndPtr->helpContext = 0;
1015 wndPtr->pVScroll = NULL;
1016 wndPtr->pHScroll = NULL;
1017 wndPtr->userdata = 0;
1019 wndPtr->hIconSmall = 0;
1020 wndPtr->hSysMenu = 0;
1021 wndPtr->flags |= (flags & WIN_ISWIN32);
1023 if (wndPtr->dwStyle & WS_SYSMENU) SetSystemMenu( hwnd, 0 );
1026 * Correct the window styles.
1028 * It affects only the style loaded into the WIN structure.
1031 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
1033 wndPtr->dwStyle |= WS_CLIPSIBLINGS;
1034 if (!(wndPtr->dwStyle & WS_POPUP))
1035 wndPtr->dwStyle |= WS_CAPTION;
1039 * WS_EX_WINDOWEDGE appears to be enforced based on the other styles, so
1040 * why does the user get to set it?
1043 if ((wndPtr->dwExStyle & WS_EX_DLGMODALFRAME) ||
1044 (wndPtr->dwStyle & (WS_DLGFRAME | WS_THICKFRAME)))
1045 wndPtr->dwExStyle |= WS_EX_WINDOWEDGE;
1047 wndPtr->dwExStyle &= ~WS_EX_WINDOWEDGE;
1049 if (!(wndPtr->dwStyle & (WS_CHILD | WS_POPUP)))
1050 wndPtr->flags |= WIN_NEED_SIZE;
1052 SERVER_START_REQ( set_window_info )
1055 req->flags = SET_WIN_STYLE | SET_WIN_EXSTYLE | SET_WIN_INSTANCE | SET_WIN_UNICODE;
1056 req->style = wndPtr->dwStyle;
1057 req->ex_style = wndPtr->dwExStyle;
1058 req->instance = (void *)wndPtr->hInstance;
1059 req->is_unicode = (wndPtr->flags & WIN_ISUNICODE) != 0;
1060 req->extra_offset = -1;
1061 wine_server_call( req );
1065 /* Set the window menu */
1067 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
1071 if (!MENU_SetMenu(hwnd, cs->hMenu))
1073 WIN_ReleasePtr( wndPtr );
1074 free_window_handle( hwnd );
1080 LPCSTR menuName = (LPCSTR)GetClassLongPtrA( hwnd, GCLP_MENUNAME );
1083 if (!cs->hInstance || HIWORD(cs->hInstance))
1084 cs->hMenu = LoadMenuA(cs->hInstance,menuName);
1086 cs->hMenu = HMENU_32(LoadMenu16(HINSTANCE_16(cs->hInstance),menuName));
1088 if (cs->hMenu) MENU_SetMenu( hwnd, cs->hMenu );
1092 else SetWindowLongPtrW( hwnd, GWLP_ID, (ULONG_PTR)cs->hMenu );
1094 /* call the WH_CBT hook */
1096 /* the window style passed to the hook must be the real window style,
1097 * rather than just the window style that the caller to CreateWindowEx
1098 * passed in, so we have to copy the original CREATESTRUCT and get the
1099 * the real style. */
1101 cbcs.style = wndPtr->dwStyle;
1103 cbtc.hwndInsertAfter = HWND_TOP;
1104 WIN_ReleasePtr( wndPtr );
1105 if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode )) goto failed;
1107 /* send the WM_GETMINMAXINFO message and fix the size if needed */
1111 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
1113 POINT maxSize, maxPos, minTrack, maxTrack;
1114 WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
1115 if (maxTrack.x < cx) cx = maxTrack.x;
1116 if (maxTrack.y < cy) cy = maxTrack.y;
1121 SetRect( &rect, cs->x, cs->y, cs->x + cx, cs->y + cy );
1122 if (!set_window_pos( hwnd, 0, SWP_NOZORDER | SWP_NOACTIVATE, &rect, &rect, NULL )) goto failed;
1124 /* send WM_NCCREATE */
1126 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cx, cy );
1128 result = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1130 result = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1133 WARN( "%p: aborted by WM_NCCREATE\n", hwnd );
1137 /* send WM_NCCALCSIZE */
1139 if ((wndPtr = WIN_GetPtr(hwnd)))
1141 /* yes, even if the CBT hook was called with HWND_TOP */
1142 HWND insert_after = (wndPtr->dwStyle & WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
1143 RECT window_rect = wndPtr->rectWindow;
1144 RECT client_rect = window_rect;
1145 WIN_ReleasePtr( wndPtr );
1146 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&client_rect );
1147 set_window_pos( hwnd, insert_after, SWP_NOACTIVATE, &window_rect, &client_rect, NULL );
1151 /* send WM_CREATE */
1154 result = SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs );
1156 result = SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs );
1157 if (result == -1) goto failed;
1159 /* call the driver */
1161 if (!USER_Driver->pCreateWindow( hwnd )) goto failed;
1163 NotifyWinEvent(EVENT_OBJECT_CREATE, hwnd, OBJID_WINDOW, 0);
1165 /* send the size messages */
1167 if (!(wndPtr = WIN_GetPtr(hwnd))) return 0;
1168 if (!(wndPtr->flags & WIN_NEED_SIZE))
1170 rect = wndPtr->rectClient;
1171 WIN_ReleasePtr( wndPtr );
1172 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1173 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1174 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( rect.left, rect.top ) );
1176 else WIN_ReleasePtr( wndPtr );
1178 /* Show the window, maximizing or minimizing if needed */
1180 style = WIN_SetStyle( hwnd, 0, WS_MAXIMIZE | WS_MINIMIZE );
1181 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1184 UINT swFlag = (style & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1186 swFlag = WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1187 swFlag |= SWP_FRAMECHANGED; /* Frame always gets changed */
1188 if (!(style & WS_VISIBLE) || (style & WS_CHILD) || GetActiveWindow()) swFlag |= SWP_NOACTIVATE;
1189 SetWindowPos( hwnd, 0, newPos.left, newPos.top, newPos.right, newPos.bottom, swFlag );
1192 /* Notify the parent window only */
1194 send_parent_notify( hwnd, WM_CREATE );
1195 if (!IsWindow( hwnd )) return 0;
1197 if (cs->style & WS_VISIBLE)
1199 if (cs->style & WS_MAXIMIZE)
1201 else if (cs->style & WS_MINIMIZE)
1202 sw = SW_SHOWMINIMIZED;
1204 ShowWindow( hwnd, sw );
1205 if (cs->dwExStyle & WS_EX_MDICHILD)
1207 SendMessageW(cs->hwndParent, WM_MDIREFRESHMENU, 0, 0);
1208 /* ShowWindow won't activate child windows */
1209 SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE );
1213 /* Call WH_SHELL hook */
1215 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) && !GetWindow( hwnd, GW_OWNER ))
1216 HOOK_CallHooks( WH_SHELL, HSHELL_WINDOWCREATED, (WPARAM)hwnd, 0, TRUE );
1218 TRACE("created window %p\n", hwnd);
1222 WIN_DestroyWindow( hwnd );
1227 /***********************************************************************
1228 * CreateWindow (USER.41)
1230 HWND16 WINAPI CreateWindow16( LPCSTR className, LPCSTR windowName,
1231 DWORD style, INT16 x, INT16 y, INT16 width,
1232 INT16 height, HWND16 parent, HMENU16 menu,
1233 HINSTANCE16 instance, LPVOID data )
1235 return CreateWindowEx16( 0, className, windowName, style,
1236 x, y, width, height, parent, menu, instance, data );
1240 /***********************************************************************
1241 * CreateWindowEx (USER.452)
1243 HWND16 WINAPI CreateWindowEx16( DWORD exStyle, LPCSTR className,
1244 LPCSTR windowName, DWORD style, INT16 x,
1245 INT16 y, INT16 width, INT16 height,
1246 HWND16 parent, HMENU16 menu,
1247 HINSTANCE16 instance, LPVOID data )
1252 /* Fix the coordinates */
1254 cs.x = (x == CW_USEDEFAULT16) ? CW_USEDEFAULT : (INT)x;
1255 cs.y = (y == CW_USEDEFAULT16) ? CW_USEDEFAULT : (INT)y;
1256 cs.cx = (width == CW_USEDEFAULT16) ? CW_USEDEFAULT : (INT)width;
1257 cs.cy = (height == CW_USEDEFAULT16) ? CW_USEDEFAULT : (INT)height;
1259 /* Create the window */
1261 cs.lpCreateParams = data;
1262 cs.hInstance = HINSTANCE_32(instance);
1263 cs.hMenu = HMENU_32(menu);
1264 cs.hwndParent = WIN_Handle32( parent );
1266 cs.lpszName = windowName;
1267 cs.lpszClass = className;
1268 cs.dwExStyle = exStyle;
1270 if (!IS_INTRESOURCE(className))
1274 if (!MultiByteToWideChar( CP_ACP, 0, className, -1, bufferW, sizeof(bufferW)/sizeof(WCHAR) ))
1276 return HWND_16( WIN_CreateWindowEx( &cs, bufferW, 0 ));
1280 if (!GlobalGetAtomNameA( LOWORD(className), buffer, sizeof(buffer) ))
1282 ERR( "bad atom %x\n", LOWORD(className));
1285 cs.lpszClass = buffer;
1286 return HWND_16( WIN_CreateWindowEx( &cs, (LPCWSTR)className, 0 ));
1291 /***********************************************************************
1292 * CreateWindowExA (USER32.@)
1294 HWND WINAPI CreateWindowExA( DWORD exStyle, LPCSTR className,
1295 LPCSTR windowName, DWORD style, INT x,
1296 INT y, INT width, INT height,
1297 HWND parent, HMENU menu,
1298 HINSTANCE instance, LPVOID data )
1302 cs.lpCreateParams = data;
1303 cs.hInstance = instance;
1305 cs.hwndParent = parent;
1311 cs.lpszName = windowName;
1312 cs.lpszClass = className;
1313 cs.dwExStyle = exStyle;
1315 if (!IS_INTRESOURCE(className))
1318 if (!MultiByteToWideChar( CP_ACP, 0, className, -1, bufferW, sizeof(bufferW)/sizeof(WCHAR) ))
1320 return WIN_CreateWindowEx( &cs, bufferW, WIN_ISWIN32 );
1322 return WIN_CreateWindowEx( &cs, (LPCWSTR)className, WIN_ISWIN32 );
1326 /***********************************************************************
1327 * CreateWindowExW (USER32.@)
1329 HWND WINAPI CreateWindowExW( DWORD exStyle, LPCWSTR className,
1330 LPCWSTR windowName, DWORD style, INT x,
1331 INT y, INT width, INT height,
1332 HWND parent, HMENU menu,
1333 HINSTANCE instance, LPVOID data )
1337 cs.lpCreateParams = data;
1338 cs.hInstance = instance;
1340 cs.hwndParent = parent;
1346 cs.lpszName = windowName;
1347 cs.lpszClass = className;
1348 cs.dwExStyle = exStyle;
1350 /* Note: we rely on the fact that CREATESTRUCTA and */
1351 /* CREATESTRUCTW have the same layout. */
1352 return WIN_CreateWindowEx( (CREATESTRUCTA *)&cs, className, WIN_ISWIN32 | WIN_ISUNICODE );
1356 /***********************************************************************
1357 * WIN_SendDestroyMsg
1359 static void WIN_SendDestroyMsg( HWND hwnd )
1363 if (GetGUIThreadInfo( GetCurrentThreadId(), &info ))
1365 if (hwnd == info.hwndCaret) DestroyCaret();
1366 if (hwnd == info.hwndActive) WINPOS_ActivateOtherWindow( hwnd );
1370 * Send the WM_DESTROY to the window.
1372 SendMessageW( hwnd, WM_DESTROY, 0, 0);
1375 * This WM_DESTROY message can trigger re-entrant calls to DestroyWindow
1376 * make sure that the window still exists when we come back.
1383 if (!(pWndArray = WIN_ListChildren( hwnd ))) return;
1385 for (i = 0; pWndArray[i]; i++)
1387 if (IsWindow( pWndArray[i] )) WIN_SendDestroyMsg( pWndArray[i] );
1389 HeapFree( GetProcessHeap(), 0, pWndArray );
1392 WARN("\tdestroyed itself while in WM_DESTROY!\n");
1396 /***********************************************************************
1397 * DestroyWindow (USER32.@)
1399 BOOL WINAPI DestroyWindow( HWND hwnd )
1403 if (!(hwnd = WIN_IsCurrentThread( hwnd )) || (hwnd == GetDesktopWindow()))
1405 SetLastError( ERROR_ACCESS_DENIED );
1409 TRACE("(%p)\n", hwnd);
1413 if (HOOK_CallHooks( WH_CBT, HCBT_DESTROYWND, (WPARAM)hwnd, 0, TRUE )) return FALSE;
1415 if (MENU_IsMenuActive() == hwnd)
1418 is_child = (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) != 0;
1422 if (!USER_IsExitingThread( GetCurrentThreadId() ))
1423 send_parent_notify( hwnd, WM_DESTROY );
1425 else if (!GetWindow( hwnd, GW_OWNER ))
1427 HOOK_CallHooks( WH_SHELL, HSHELL_WINDOWDESTROYED, (WPARAM)hwnd, 0L, TRUE );
1428 /* FIXME: clean up palette - see "Internals" p.352 */
1431 if (!IsWindow(hwnd)) return TRUE;
1433 /* Hide the window */
1434 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)
1436 /* Only child windows receive WM_SHOWWINDOW in DestroyWindow() */
1438 ShowWindow( hwnd, SW_HIDE );
1440 SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE |
1441 SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW );
1444 if (!IsWindow(hwnd)) return TRUE;
1446 /* Recursively destroy owned windows */
1453 HWND *list = WIN_ListChildren( GetDesktopWindow() );
1456 for (i = 0; list[i]; i++)
1458 if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
1459 if (WIN_IsCurrentThread( list[i] ))
1461 DestroyWindow( list[i] );
1465 WIN_SetOwner( list[i], 0 );
1467 HeapFree( GetProcessHeap(), 0, list );
1469 if (!got_one) break;
1473 /* Send destroy messages */
1475 WIN_SendDestroyMsg( hwnd );
1476 if (!IsWindow( hwnd )) return TRUE;
1478 if (GetClipboardOwner() == hwnd)
1479 CLIPBOARD_ReleaseOwner();
1481 /* Destroy the window storage */
1483 WIN_DestroyWindow( hwnd );
1488 /***********************************************************************
1489 * CloseWindow (USER32.@)
1491 BOOL WINAPI CloseWindow( HWND hwnd )
1493 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) return FALSE;
1494 ShowWindow( hwnd, SW_MINIMIZE );
1499 /***********************************************************************
1500 * OpenIcon (USER32.@)
1502 BOOL WINAPI OpenIcon( HWND hwnd )
1504 if (!IsIconic( hwnd )) return FALSE;
1505 ShowWindow( hwnd, SW_SHOWNORMAL );
1510 /***********************************************************************
1511 * FindWindowExW (USER32.@)
1513 HWND WINAPI FindWindowExW( HWND parent, HWND child, LPCWSTR className, LPCWSTR title )
1518 WCHAR *buffer = NULL;
1520 if (!parent) parent = GetDesktopWindow();
1523 len = strlenW(title) + 1; /* one extra char to check for chars beyond the end */
1524 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return 0;
1527 if (!(list = list_window_children( 0, parent, className, 0 ))) goto done;
1531 child = WIN_GetFullHandle( child );
1532 while (list[i] && list[i] != child) i++;
1533 if (!list[i]) goto done;
1534 i++; /* start from next window */
1541 if (GetWindowTextW( list[i], buffer, len + 1 ) && !strcmpiW( buffer, title )) break;
1548 HeapFree( GetProcessHeap(), 0, list );
1549 HeapFree( GetProcessHeap(), 0, buffer );
1555 /***********************************************************************
1556 * FindWindowA (USER32.@)
1558 HWND WINAPI FindWindowA( LPCSTR className, LPCSTR title )
1560 HWND ret = FindWindowExA( 0, 0, className, title );
1561 if (!ret) SetLastError (ERROR_CANNOT_FIND_WND_CLASS);
1566 /***********************************************************************
1567 * FindWindowExA (USER32.@)
1569 HWND WINAPI FindWindowExA( HWND parent, HWND child, LPCSTR className, LPCSTR title )
1571 LPWSTR titleW = NULL;
1576 DWORD len = MultiByteToWideChar( CP_ACP, 0, title, -1, NULL, 0 );
1577 if (!(titleW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return 0;
1578 MultiByteToWideChar( CP_ACP, 0, title, -1, titleW, len );
1581 if (!IS_INTRESOURCE(className))
1584 if (MultiByteToWideChar( CP_ACP, 0, className, -1, classW, sizeof(classW)/sizeof(WCHAR) ))
1585 hwnd = FindWindowExW( parent, child, classW, titleW );
1589 hwnd = FindWindowExW( parent, child, (LPCWSTR)className, titleW );
1592 HeapFree( GetProcessHeap(), 0, titleW );
1597 /***********************************************************************
1598 * FindWindowW (USER32.@)
1600 HWND WINAPI FindWindowW( LPCWSTR className, LPCWSTR title )
1602 return FindWindowExW( 0, 0, className, title );
1606 /**********************************************************************
1607 * GetDesktopWindow (USER32.@)
1609 HWND WINAPI GetDesktopWindow(void)
1611 struct user_thread_info *thread_info = get_user_thread_info();
1613 if (thread_info->desktop) return thread_info->desktop;
1615 SERVER_START_REQ( get_desktop_window )
1618 if (!wine_server_call( req )) thread_info->desktop = reply->handle;
1622 if (!thread_info->desktop)
1624 USEROBJECTFLAGS flags;
1625 if (!GetUserObjectInformationW( GetProcessWindowStation(), UOI_FLAGS, &flags,
1626 sizeof(flags), NULL ) || (flags.dwFlags & WSF_VISIBLE))
1628 static const WCHAR command_line[] = {'\\','e','x','p','l','o','r','e','r','.','e','x','e',' ','/','d','e','s','k','t','o','p',0};
1630 PROCESS_INFORMATION pi;
1631 WCHAR cmdline[MAX_PATH + sizeof(command_line)/sizeof(WCHAR)];
1633 memset( &si, 0, sizeof(si) );
1635 si.dwFlags = STARTF_USESTDHANDLES;
1638 si.hStdError = GetStdHandle( STD_ERROR_HANDLE );
1640 GetSystemDirectoryW( cmdline, MAX_PATH );
1641 lstrcatW( cmdline, command_line );
1642 if (CreateProcessW( NULL, cmdline, NULL, NULL, FALSE, DETACHED_PROCESS,
1643 NULL, NULL, &si, &pi ))
1645 TRACE( "started explorer pid %04x tid %04x\n", pi.dwProcessId, pi.dwThreadId );
1646 WaitForInputIdle( pi.hProcess, 10000 );
1647 CloseHandle( pi.hThread );
1648 CloseHandle( pi.hProcess );
1650 else WARN( "failed to start explorer, err %d\n", GetLastError() );
1652 else TRACE( "not starting explorer since winstation is not visible\n" );
1654 SERVER_START_REQ( get_desktop_window )
1657 if (!wine_server_call( req )) thread_info->desktop = reply->handle;
1662 if (!thread_info->desktop || !USER_Driver->pCreateDesktopWindow( thread_info->desktop ))
1663 ERR( "failed to create desktop window\n" );
1665 return thread_info->desktop;
1669 /*******************************************************************
1670 * EnableWindow (USER32.@)
1672 BOOL WINAPI EnableWindow( HWND hwnd, BOOL enable )
1677 if (is_broadcast(hwnd))
1679 SetLastError( ERROR_INVALID_PARAMETER );
1683 if (!(full_handle = WIN_IsCurrentThread( hwnd )))
1684 return SendMessageW( hwnd, WM_WINE_ENABLEWINDOW, enable, 0 );
1688 TRACE("( %p, %d )\n", hwnd, enable);
1690 retvalue = !IsWindowEnabled( hwnd );
1692 if (enable && retvalue)
1694 WIN_SetStyle( hwnd, 0, WS_DISABLED );
1695 SendMessageW( hwnd, WM_ENABLE, TRUE, 0 );
1697 else if (!enable && !retvalue)
1701 SendMessageW( hwnd, WM_CANCELMODE, 0, 0);
1703 WIN_SetStyle( hwnd, WS_DISABLED, 0 );
1705 if (hwnd == GetFocus())
1706 SetFocus( 0 ); /* A disabled window can't have the focus */
1708 capture_wnd = GetCapture();
1709 if (hwnd == capture_wnd || IsChild(hwnd, capture_wnd))
1710 ReleaseCapture(); /* A disabled window can't capture the mouse */
1712 SendMessageW( hwnd, WM_ENABLE, FALSE, 0 );
1718 /***********************************************************************
1719 * IsWindowEnabled (USER32.@)
1721 BOOL WINAPI IsWindowEnabled(HWND hWnd)
1723 return !(GetWindowLongW( hWnd, GWL_STYLE ) & WS_DISABLED);
1727 /***********************************************************************
1728 * IsWindowUnicode (USER32.@)
1730 BOOL WINAPI IsWindowUnicode( HWND hwnd )
1733 BOOL retvalue = FALSE;
1735 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
1737 if (wndPtr == WND_DESKTOP) return TRUE;
1739 if (wndPtr != WND_OTHER_PROCESS)
1741 retvalue = (wndPtr->flags & WIN_ISUNICODE) != 0;
1742 WIN_ReleasePtr( wndPtr );
1746 SERVER_START_REQ( get_window_info )
1749 if (!wine_server_call_err( req )) retvalue = reply->is_unicode;
1757 /**********************************************************************
1760 * Helper function for GetWindowLong().
1762 static LONG_PTR WIN_GetWindowLong( HWND hwnd, INT offset, UINT size, BOOL unicode )
1764 LONG_PTR retvalue = 0;
1767 if (offset == GWLP_HWNDPARENT)
1769 HWND parent = GetAncestor( hwnd, GA_PARENT );
1770 if (parent == GetDesktopWindow()) parent = GetWindow( hwnd, GW_OWNER );
1771 return (ULONG_PTR)parent;
1774 if (!(wndPtr = WIN_GetPtr( hwnd )))
1776 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1780 if (wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP)
1782 if (offset == GWLP_WNDPROC)
1784 SetLastError( ERROR_ACCESS_DENIED );
1787 SERVER_START_REQ( set_window_info )
1790 req->flags = 0; /* don't set anything, just retrieve */
1791 req->extra_offset = (offset >= 0) ? offset : -1;
1792 req->extra_size = (offset >= 0) ? size : 0;
1793 if (!wine_server_call_err( req ))
1797 case GWL_STYLE: retvalue = reply->old_style; break;
1798 case GWL_EXSTYLE: retvalue = reply->old_ex_style; break;
1799 case GWLP_ID: retvalue = reply->old_id; break;
1800 case GWLP_HINSTANCE: retvalue = (ULONG_PTR)reply->old_instance; break;
1801 case GWLP_USERDATA: retvalue = reply->old_user_data; break;
1803 if (offset >= 0) retvalue = get_win_data( &reply->old_extra_value, size );
1804 else SetLastError( ERROR_INVALID_INDEX );
1813 /* now we have a valid wndPtr */
1817 if (offset > (int)(wndPtr->cbWndExtra - size))
1819 WARN("Invalid offset %d\n", offset );
1820 WIN_ReleasePtr( wndPtr );
1821 SetLastError( ERROR_INVALID_INDEX );
1824 retvalue = get_win_data( (char *)wndPtr->wExtra + offset, size );
1826 /* Special case for dialog window procedure */
1827 if ((offset == DWLP_DLGPROC) && (size == sizeof(LONG_PTR)) && (wndPtr->flags & WIN_ISDIALOG))
1828 retvalue = (LONG_PTR)WINPROC_GetProc( (WNDPROC)retvalue, unicode );
1829 WIN_ReleasePtr( wndPtr );
1835 case GWLP_USERDATA: retvalue = wndPtr->userdata; break;
1836 case GWL_STYLE: retvalue = wndPtr->dwStyle; break;
1837 case GWL_EXSTYLE: retvalue = wndPtr->dwExStyle; break;
1838 case GWLP_ID: retvalue = wndPtr->wIDmenu; break;
1839 case GWLP_HINSTANCE: retvalue = (ULONG_PTR)wndPtr->hInstance; break;
1841 /* This looks like a hack only for the edit control (see tests). This makes these controls
1842 * more tolerant to A/W mismatches. The lack of W->A->W conversion for such a mismatch suggests
1843 * that the hack is in GetWindowLongPtr[AW], not in winprocs.
1845 if (wndPtr->winproc == EDIT_winproc_handle && (!unicode != !(wndPtr->flags & WIN_ISUNICODE)))
1846 retvalue = (ULONG_PTR)wndPtr->winproc;
1848 retvalue = (ULONG_PTR)WINPROC_GetProc( wndPtr->winproc, unicode );
1851 WARN("Unknown offset %d\n", offset );
1852 SetLastError( ERROR_INVALID_INDEX );
1855 WIN_ReleasePtr(wndPtr);
1860 /**********************************************************************
1863 * Helper function for SetWindowLong().
1865 * 0 is the failure code. However, in the case of failure SetLastError
1866 * must be set to distinguish between a 0 return value and a failure.
1868 LONG_PTR WIN_SetWindowLong( HWND hwnd, INT offset, UINT size, LONG_PTR newval, BOOL unicode )
1872 LONG_PTR retval = 0;
1875 TRACE( "%p %d %lx %c\n", hwnd, offset, newval, unicode ? 'W' : 'A' );
1877 if (is_broadcast(hwnd))
1879 SetLastError( ERROR_INVALID_PARAMETER );
1883 if (!(wndPtr = WIN_GetPtr( hwnd )))
1885 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1888 if (wndPtr == WND_DESKTOP)
1890 /* can't change anything on the desktop window */
1891 SetLastError( ERROR_ACCESS_DENIED );
1894 if (wndPtr == WND_OTHER_PROCESS)
1896 if (offset == GWLP_WNDPROC)
1898 SetLastError( ERROR_ACCESS_DENIED );
1901 if (offset > 32767 || offset < -32767)
1903 SetLastError( ERROR_INVALID_INDEX );
1906 return SendMessageW( hwnd, WM_WINE_SETWINDOWLONG, MAKEWPARAM( offset, size ), newval );
1909 /* first some special cases */
1915 offset == GWL_STYLE ? wndPtr->dwStyle : wndPtr->dwExStyle;
1916 style.styleNew = newval;
1917 WIN_ReleasePtr( wndPtr );
1918 SendMessageW( hwnd, WM_STYLECHANGING, offset, (LPARAM)&style );
1919 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
1920 newval = style.styleNew;
1922 case GWLP_HWNDPARENT:
1923 if (wndPtr->parent == GetDesktopWindow())
1925 WIN_ReleasePtr( wndPtr );
1926 return (ULONG_PTR)WIN_SetOwner( hwnd, (HWND)newval );
1930 WIN_ReleasePtr( wndPtr );
1931 return (ULONG_PTR)SetParent( hwnd, (HWND)newval );
1936 UINT old_flags = wndPtr->flags;
1937 retval = WIN_GetWindowLong( hwnd, offset, size, unicode );
1938 if (unicode) proc = WINPROC_AllocProc( NULL, (WNDPROC)newval );
1939 else proc = WINPROC_AllocProc( (WNDPROC)newval, NULL );
1940 if (proc) wndPtr->winproc = proc;
1941 if (WINPROC_IsUnicode( proc, unicode )) wndPtr->flags |= WIN_ISUNICODE;
1942 else wndPtr->flags &= ~WIN_ISUNICODE;
1943 if (!((old_flags ^ wndPtr->flags) & WIN_ISUNICODE))
1945 WIN_ReleasePtr( wndPtr );
1948 /* update is_unicode flag on the server side */
1952 case GWLP_HINSTANCE:
1956 if ((wndPtr->cbWndExtra - sizeof(LONG_PTR) >= DWLP_DLGPROC) &&
1957 (size == sizeof(LONG_PTR)) && (wndPtr->flags & WIN_ISDIALOG))
1959 WNDPROC *ptr = (WNDPROC *)((char *)wndPtr->wExtra + DWLP_DLGPROC);
1960 retval = (ULONG_PTR)WINPROC_GetProc( *ptr, unicode );
1961 if (unicode) *ptr = WINPROC_AllocProc( NULL, (WNDPROC)newval );
1962 else *ptr = WINPROC_AllocProc( (WNDPROC)newval, NULL );
1963 WIN_ReleasePtr( wndPtr );
1968 if (offset < 0 || offset > (int)(wndPtr->cbWndExtra - size))
1970 WARN("Invalid offset %d\n", offset );
1971 WIN_ReleasePtr( wndPtr );
1972 SetLastError( ERROR_INVALID_INDEX );
1975 else if (get_win_data( (char *)wndPtr->wExtra + offset, size ) == newval)
1977 /* already set to the same value */
1978 WIN_ReleasePtr( wndPtr );
1984 SERVER_START_REQ( set_window_info )
1987 req->extra_offset = -1;
1991 req->flags = SET_WIN_STYLE;
1992 req->style = newval;
1995 req->flags = SET_WIN_EXSTYLE;
1996 /* WS_EX_TOPMOST can only be changed through SetWindowPos */
1997 newval = (newval & ~WS_EX_TOPMOST) | (wndPtr->dwExStyle & WS_EX_TOPMOST);
1998 req->ex_style = newval;
2001 req->flags = SET_WIN_ID;
2004 case GWLP_HINSTANCE:
2005 req->flags = SET_WIN_INSTANCE;
2006 req->instance = (void *)newval;
2009 req->flags = SET_WIN_UNICODE;
2010 req->is_unicode = (wndPtr->flags & WIN_ISUNICODE) != 0;
2013 req->flags = SET_WIN_USERDATA;
2014 req->user_data = newval;
2017 req->flags = SET_WIN_EXTRA;
2018 req->extra_offset = offset;
2019 req->extra_size = size;
2020 set_win_data( &req->extra_value, newval, size );
2022 if ((ok = !wine_server_call_err( req )))
2027 wndPtr->dwStyle = newval;
2028 retval = reply->old_style;
2031 wndPtr->dwExStyle = newval;
2032 retval = reply->old_ex_style;
2035 wndPtr->wIDmenu = newval;
2036 retval = reply->old_id;
2038 case GWLP_HINSTANCE:
2039 wndPtr->hInstance = (HINSTANCE)newval;
2040 retval = (ULONG_PTR)reply->old_instance;
2045 wndPtr->userdata = newval;
2046 retval = reply->old_user_data;
2049 retval = get_win_data( (char *)wndPtr->wExtra + offset, size );
2050 set_win_data( (char *)wndPtr->wExtra + offset, newval, size );
2056 WIN_ReleasePtr( wndPtr );
2060 if (offset == GWL_STYLE) USER_Driver->pSetWindowStyle( hwnd, retval );
2062 if (offset == GWL_STYLE || offset == GWL_EXSTYLE)
2063 SendMessageW( hwnd, WM_STYLECHANGED, offset, (LPARAM)&style );
2069 /**********************************************************************
2070 * GetWindowLong (USER.135)
2072 LONG WINAPI GetWindowLong16( HWND16 hwnd, INT16 offset )
2076 BOOL is_winproc = (offset == GWLP_WNDPROC);
2080 if (!(wndPtr = WIN_GetPtr( WIN_Handle32(hwnd) )))
2082 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2085 if (wndPtr != WND_OTHER_PROCESS && wndPtr != WND_DESKTOP)
2087 if (offset > (int)(wndPtr->cbWndExtra - sizeof(LONG)))
2090 * Some programs try to access last element from 16 bit
2091 * code using illegal offset value. Hopefully this is
2092 * what those programs really expect.
2094 if (wndPtr->cbWndExtra >= 4 && offset == wndPtr->cbWndExtra - sizeof(WORD))
2096 INT offset2 = wndPtr->cbWndExtra - sizeof(LONG);
2097 ERR( "- replaced invalid offset %d with %d\n", offset, offset2 );
2102 WARN("Invalid offset %d\n", offset );
2103 WIN_ReleasePtr( wndPtr );
2104 SetLastError( ERROR_INVALID_INDEX );
2108 is_winproc = ((offset == DWLP_DLGPROC) && (wndPtr->flags & WIN_ISDIALOG));
2109 WIN_ReleasePtr( wndPtr );
2112 retvalue = GetWindowLongA( WIN_Handle32(hwnd), offset );
2113 if (is_winproc) retvalue = (LONG_PTR)WINPROC_GetProc16( (WNDPROC)retvalue, FALSE );
2118 /**********************************************************************
2119 * GetWindowWord (USER32.@)
2121 WORD WINAPI GetWindowWord( HWND hwnd, INT offset )
2126 case GWLP_HINSTANCE:
2127 case GWLP_HWNDPARENT:
2132 WARN("Invalid offset %d\n", offset );
2133 SetLastError( ERROR_INVALID_INDEX );
2138 return WIN_GetWindowLong( hwnd, offset, sizeof(WORD), FALSE );
2142 /**********************************************************************
2143 * GetWindowLongA (USER32.@)
2145 LONG WINAPI GetWindowLongA( HWND hwnd, INT offset )
2147 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG), FALSE );
2151 /**********************************************************************
2152 * GetWindowLongW (USER32.@)
2154 LONG WINAPI GetWindowLongW( HWND hwnd, INT offset )
2156 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG), TRUE );
2160 /**********************************************************************
2161 * SetWindowLong (USER.136)
2163 LONG WINAPI SetWindowLong16( HWND16 hwnd, INT16 offset, LONG newval )
2166 BOOL is_winproc = (offset == GWLP_WNDPROC);
2168 if (offset == DWLP_DLGPROC)
2170 if (!(wndPtr = WIN_GetPtr( WIN_Handle32(hwnd) )))
2172 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2175 if (wndPtr != WND_OTHER_PROCESS && wndPtr != WND_DESKTOP)
2177 is_winproc = ((wndPtr->cbWndExtra - sizeof(LONG_PTR) >= DWLP_DLGPROC) &&
2178 (wndPtr->flags & WIN_ISDIALOG));
2179 WIN_ReleasePtr( wndPtr );
2185 WNDPROC new_proc = WINPROC_AllocProc16( (WNDPROC16)newval );
2186 WNDPROC old_proc = (WNDPROC)SetWindowLongPtrA( WIN_Handle32(hwnd), offset, (LONG_PTR)new_proc );
2187 return (LONG)WINPROC_GetProc16( old_proc, FALSE );
2189 else return SetWindowLongA( WIN_Handle32(hwnd), offset, newval );
2193 /**********************************************************************
2194 * SetWindowWord (USER32.@)
2196 WORD WINAPI SetWindowWord( HWND hwnd, INT offset, WORD newval )
2201 case GWLP_HINSTANCE:
2202 case GWLP_HWNDPARENT:
2207 WARN("Invalid offset %d\n", offset );
2208 SetLastError( ERROR_INVALID_INDEX );
2213 return WIN_SetWindowLong( hwnd, offset, sizeof(WORD), newval, FALSE );
2217 /**********************************************************************
2218 * SetWindowLongA (USER32.@)
2220 * See SetWindowLongW.
2222 LONG WINAPI SetWindowLongA( HWND hwnd, INT offset, LONG newval )
2224 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG), newval, FALSE );
2228 /**********************************************************************
2229 * SetWindowLongW (USER32.@) Set window attribute
2231 * SetWindowLong() alters one of a window's attributes or sets a 32-bit (long)
2232 * value in a window's extra memory.
2234 * The _hwnd_ parameter specifies the window. is the handle to a
2235 * window that has extra memory. The _newval_ parameter contains the
2236 * new attribute or extra memory value. If positive, the _offset_
2237 * parameter is the byte-addressed location in the window's extra
2238 * memory to set. If negative, _offset_ specifies the window
2239 * attribute to set, and should be one of the following values:
2241 * GWL_EXSTYLE The window's extended window style
2243 * GWL_STYLE The window's window style.
2245 * GWLP_WNDPROC Pointer to the window's window procedure.
2247 * GWLP_HINSTANCE The window's pplication instance handle.
2249 * GWLP_ID The window's identifier.
2251 * GWLP_USERDATA The window's user-specified data.
2253 * If the window is a dialog box, the _offset_ parameter can be one of
2254 * the following values:
2256 * DWLP_DLGPROC The address of the window's dialog box procedure.
2258 * DWLP_MSGRESULT The return value of a message
2259 * that the dialog box procedure processed.
2261 * DWLP_USER Application specific information.
2265 * If successful, returns the previous value located at _offset_. Otherwise,
2270 * Extra memory for a window class is specified by a nonzero cbWndExtra
2271 * parameter of the WNDCLASS structure passed to RegisterClass() at the
2272 * time of class creation.
2274 * Using GWL_WNDPROC to set a new window procedure effectively creates
2275 * a window subclass. Use CallWindowProc() in the new windows procedure
2276 * to pass messages to the superclass's window procedure.
2278 * The user data is reserved for use by the application which created
2281 * Do not use GWL_STYLE to change the window's WS_DISABLED style;
2282 * instead, call the EnableWindow() function to change the window's
2285 * Do not use GWL_HWNDPARENT to reset the window's parent, use
2286 * SetParent() instead.
2289 * When offset is GWL_STYLE and the calling app's ver is 4.0,
2290 * it sends WM_STYLECHANGING before changing the settings
2291 * and WM_STYLECHANGED afterwards.
2292 * App ver 4.0 can't use SetWindowLong to change WS_EX_TOPMOST.
2294 LONG WINAPI SetWindowLongW(
2295 HWND hwnd, /* [in] window to alter */
2296 INT offset, /* [in] offset, in bytes, of location to alter */
2297 LONG newval /* [in] new value of location */
2299 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG), newval, TRUE );
2303 /*******************************************************************
2304 * GetWindowTextA (USER32.@)
2306 INT WINAPI GetWindowTextA( HWND hwnd, LPSTR lpString, INT nMaxCount )
2310 if (!lpString) return 0;
2312 if (WIN_IsCurrentProcess( hwnd ))
2313 return (INT)SendMessageA( hwnd, WM_GETTEXT, nMaxCount, (LPARAM)lpString );
2315 /* when window belongs to other process, don't send a message */
2316 if (nMaxCount <= 0) return 0;
2317 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, nMaxCount * sizeof(WCHAR) ))) return 0;
2318 get_server_window_text( hwnd, buffer, nMaxCount );
2319 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, lpString, nMaxCount, NULL, NULL ))
2320 lpString[nMaxCount-1] = 0;
2321 HeapFree( GetProcessHeap(), 0, buffer );
2322 return strlen(lpString);
2326 /*******************************************************************
2327 * InternalGetWindowText (USER32.@)
2329 INT WINAPI InternalGetWindowText(HWND hwnd,LPWSTR lpString,INT nMaxCount )
2333 if (nMaxCount <= 0) return 0;
2334 if (!(win = WIN_GetPtr( hwnd ))) return 0;
2335 if (win == WND_DESKTOP) lpString[0] = 0;
2336 else if (win != WND_OTHER_PROCESS)
2338 if (win->text) lstrcpynW( lpString, win->text, nMaxCount );
2339 else lpString[0] = 0;
2340 WIN_ReleasePtr( win );
2344 get_server_window_text( hwnd, lpString, nMaxCount );
2346 return strlenW(lpString);
2350 /*******************************************************************
2351 * GetWindowTextW (USER32.@)
2353 INT WINAPI GetWindowTextW( HWND hwnd, LPWSTR lpString, INT nMaxCount )
2355 if (!lpString) return 0;
2357 if (WIN_IsCurrentProcess( hwnd ))
2358 return (INT)SendMessageW( hwnd, WM_GETTEXT, nMaxCount, (LPARAM)lpString );
2360 /* when window belongs to other process, don't send a message */
2361 if (nMaxCount <= 0) return 0;
2362 get_server_window_text( hwnd, lpString, nMaxCount );
2363 return strlenW(lpString);
2367 /*******************************************************************
2368 * SetWindowTextA (USER32.@)
2369 * SetWindowText (USER32.@)
2371 BOOL WINAPI SetWindowTextA( HWND hwnd, LPCSTR lpString )
2373 if (is_broadcast(hwnd))
2375 SetLastError( ERROR_INVALID_PARAMETER );
2378 if (!WIN_IsCurrentProcess( hwnd ))
2379 WARN( "setting text %s of other process window %p should not use SendMessage\n",
2380 debugstr_a(lpString), hwnd );
2381 return (BOOL)SendMessageA( hwnd, WM_SETTEXT, 0, (LPARAM)lpString );
2385 /*******************************************************************
2386 * SetWindowTextW (USER32.@)
2388 BOOL WINAPI SetWindowTextW( HWND hwnd, LPCWSTR lpString )
2390 if (is_broadcast(hwnd))
2392 SetLastError( ERROR_INVALID_PARAMETER );
2395 if (!WIN_IsCurrentProcess( hwnd ))
2396 WARN( "setting text %s of other process window %p should not use SendMessage\n",
2397 debugstr_w(lpString), hwnd );
2398 return (BOOL)SendMessageW( hwnd, WM_SETTEXT, 0, (LPARAM)lpString );
2402 /*******************************************************************
2403 * GetWindowTextLengthA (USER32.@)
2405 INT WINAPI GetWindowTextLengthA( HWND hwnd )
2407 return SendMessageA( hwnd, WM_GETTEXTLENGTH, 0, 0 );
2410 /*******************************************************************
2411 * GetWindowTextLengthW (USER32.@)
2413 INT WINAPI GetWindowTextLengthW( HWND hwnd )
2415 return SendMessageW( hwnd, WM_GETTEXTLENGTH, 0, 0 );
2419 /*******************************************************************
2420 * IsWindow (USER32.@)
2422 BOOL WINAPI IsWindow( HWND hwnd )
2427 if (!(ptr = WIN_GetPtr( hwnd ))) return FALSE;
2428 if (ptr == WND_DESKTOP) return TRUE;
2430 if (ptr != WND_OTHER_PROCESS)
2432 WIN_ReleasePtr( ptr );
2436 /* check other processes */
2437 SERVER_START_REQ( get_window_info )
2440 ret = !wine_server_call_err( req );
2447 /***********************************************************************
2448 * GetWindowThreadProcessId (USER32.@)
2450 DWORD WINAPI GetWindowThreadProcessId( HWND hwnd, LPDWORD process )
2455 if (!(ptr = WIN_GetPtr( hwnd )))
2457 SetLastError( ERROR_INVALID_WINDOW_HANDLE);
2461 if (ptr != WND_OTHER_PROCESS && ptr != WND_DESKTOP)
2463 /* got a valid window */
2465 if (process) *process = GetCurrentProcessId();
2466 WIN_ReleasePtr( ptr );
2470 /* check other processes */
2471 SERVER_START_REQ( get_window_info )
2474 if (!wine_server_call_err( req ))
2476 tid = (DWORD)reply->tid;
2477 if (process) *process = (DWORD)reply->pid;
2485 /*****************************************************************
2486 * GetParent (USER32.@)
2488 HWND WINAPI GetParent( HWND hwnd )
2493 if (!(wndPtr = WIN_GetPtr( hwnd )))
2495 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2498 if (wndPtr == WND_DESKTOP) return 0;
2499 if (wndPtr == WND_OTHER_PROCESS)
2501 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
2502 if (style & (WS_POPUP | WS_CHILD))
2504 SERVER_START_REQ( get_window_tree )
2507 if (!wine_server_call_err( req ))
2509 if (style & WS_POPUP) retvalue = reply->owner;
2510 else if (style & WS_CHILD) retvalue = reply->parent;
2518 if (wndPtr->dwStyle & WS_POPUP) retvalue = wndPtr->owner;
2519 else if (wndPtr->dwStyle & WS_CHILD) retvalue = wndPtr->parent;
2520 WIN_ReleasePtr( wndPtr );
2526 /*****************************************************************
2527 * GetAncestor (USER32.@)
2529 HWND WINAPI GetAncestor( HWND hwnd, UINT type )
2532 HWND *list, ret = 0;
2537 if (!(win = WIN_GetPtr( hwnd )))
2539 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2542 if (win == WND_DESKTOP) return 0;
2543 if (win != WND_OTHER_PROCESS)
2546 WIN_ReleasePtr( win );
2548 else /* need to query the server */
2550 SERVER_START_REQ( get_window_tree )
2553 if (!wine_server_call_err( req )) ret = reply->parent;
2560 if (!(list = list_window_parents( hwnd ))) return 0;
2562 if (!list[0] || !list[1]) ret = WIN_GetFullHandle( hwnd ); /* top-level window */
2566 while (list[count]) count++;
2567 ret = list[count - 2]; /* get the one before the desktop */
2569 HeapFree( GetProcessHeap(), 0, list );
2573 if ((ret = WIN_GetFullHandle( hwnd )) == GetDesktopWindow()) return 0;
2576 HWND parent = GetParent( ret );
2586 /*****************************************************************
2587 * SetParent (USER32.@)
2589 HWND WINAPI SetParent( HWND hwnd, HWND parent )
2592 HWND old_parent = 0;
2597 if (is_broadcast(hwnd) || is_broadcast(parent))
2599 SetLastError(ERROR_INVALID_PARAMETER);
2603 if (!parent) parent = GetDesktopWindow();
2604 else parent = WIN_GetFullHandle( parent );
2606 if (!IsWindow( parent ))
2608 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2612 /* Some applications try to set a child as a parent */
2613 if (IsChild(hwnd, parent))
2615 SetLastError( ERROR_INVALID_PARAMETER );
2619 if (!(full_handle = WIN_IsCurrentThread( hwnd )))
2620 return (HWND)SendMessageW( hwnd, WM_WINE_SETPARENT, (WPARAM)parent, 0 );
2622 /* Windows hides the window first, then shows it again
2623 * including the WM_SHOWWINDOW messages and all */
2624 was_visible = ShowWindow( hwnd, SW_HIDE );
2626 wndPtr = WIN_GetPtr( hwnd );
2627 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return 0;
2629 SERVER_START_REQ( set_parent )
2632 req->parent = parent;
2633 if ((ret = !wine_server_call( req )))
2635 old_parent = reply->old_parent;
2636 wndPtr->parent = parent = reply->full_parent;
2641 WIN_ReleasePtr( wndPtr );
2644 USER_Driver->pSetParent( full_handle, parent, old_parent );
2646 /* SetParent additionally needs to make hwnd the topmost window
2647 in the x-order and send the expected WM_WINDOWPOSCHANGING and
2648 WM_WINDOWPOSCHANGED notification messages.
2650 SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0,
2651 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | (was_visible ? SWP_SHOWWINDOW : 0) );
2652 /* FIXME: a WM_MOVE is also generated (in the DefWindowProc handler
2653 * for WM_WINDOWPOSCHANGED) in Windows, should probably remove SWP_NOMOVE */
2659 /*******************************************************************
2660 * IsChild (USER32.@)
2662 BOOL WINAPI IsChild( HWND parent, HWND child )
2664 HWND *list = list_window_parents( child );
2668 if (!list) return FALSE;
2669 parent = WIN_GetFullHandle( parent );
2670 for (i = 0; list[i]; i++) if (list[i] == parent) break;
2671 ret = list[i] && list[i+1];
2672 HeapFree( GetProcessHeap(), 0, list );
2677 /***********************************************************************
2678 * IsWindowVisible (USER32.@)
2680 BOOL WINAPI IsWindowVisible( HWND hwnd )
2686 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)) return FALSE;
2687 if (!(list = list_window_parents( hwnd ))) return TRUE;
2688 if (list[0] && list[1]) /* desktop window is considered always visible so we don't check it */
2690 for (i = 0; list[i+1]; i++)
2691 if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_VISIBLE)) break;
2692 retval = !list[i+1];
2694 HeapFree( GetProcessHeap(), 0, list );
2699 /***********************************************************************
2700 * WIN_IsWindowDrawable
2702 * hwnd is drawable when it is visible, all parents are not
2703 * minimized, and it is itself not minimized unless we are
2704 * trying to draw its default class icon.
2706 BOOL WIN_IsWindowDrawable( HWND hwnd, BOOL icon )
2711 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
2713 if (!(style & WS_VISIBLE)) return FALSE;
2714 if ((style & WS_MINIMIZE) && icon && GetClassLongPtrW( hwnd, GCLP_HICON )) return FALSE;
2716 if (!(list = list_window_parents( hwnd ))) return TRUE;
2717 if (list[0] && list[1]) /* desktop window is considered always visible so we don't check it */
2719 for (i = 0; list[i+1]; i++)
2720 if ((GetWindowLongW( list[i], GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != WS_VISIBLE)
2722 retval = !list[i+1];
2724 HeapFree( GetProcessHeap(), 0, list );
2729 /*******************************************************************
2730 * GetTopWindow (USER32.@)
2732 HWND WINAPI GetTopWindow( HWND hwnd )
2734 if (!hwnd) hwnd = GetDesktopWindow();
2735 return GetWindow( hwnd, GW_CHILD );
2739 /*******************************************************************
2740 * GetWindow (USER32.@)
2742 HWND WINAPI GetWindow( HWND hwnd, UINT rel )
2746 if (rel == GW_OWNER) /* this one may be available locally */
2748 WND *wndPtr = WIN_GetPtr( hwnd );
2751 SetLastError( ERROR_INVALID_HANDLE );
2754 if (wndPtr == WND_DESKTOP) return 0;
2755 if (wndPtr != WND_OTHER_PROCESS)
2757 retval = wndPtr->owner;
2758 WIN_ReleasePtr( wndPtr );
2761 /* else fall through to server call */
2764 SERVER_START_REQ( get_window_tree )
2767 if (!wine_server_call_err( req ))
2772 retval = reply->first_sibling;
2775 retval = reply->last_sibling;
2778 retval = reply->next_sibling;
2781 retval = reply->prev_sibling;
2784 retval = reply->owner;
2787 retval = reply->first_child;
2797 /*******************************************************************
2798 * ShowOwnedPopups (USER32.@)
2800 BOOL WINAPI ShowOwnedPopups( HWND owner, BOOL fShow )
2804 HWND *win_array = WIN_ListChildren( GetDesktopWindow() );
2806 if (!win_array) return TRUE;
2808 while (win_array[count]) count++;
2809 while (--count >= 0)
2811 if (GetWindow( win_array[count], GW_OWNER ) != owner) continue;
2812 if (!(pWnd = WIN_GetPtr( win_array[count] ))) continue;
2813 if (pWnd == WND_OTHER_PROCESS) continue;
2816 if (pWnd->flags & WIN_NEEDS_SHOW_OWNEDPOPUP)
2818 WIN_ReleasePtr( pWnd );
2819 /* In Windows, ShowOwnedPopups(TRUE) generates
2820 * WM_SHOWWINDOW messages with SW_PARENTOPENING,
2821 * regardless of the state of the owner
2823 SendMessageW(win_array[count], WM_SHOWWINDOW, SW_SHOWNORMAL, SW_PARENTOPENING);
2829 if (pWnd->dwStyle & WS_VISIBLE)
2831 WIN_ReleasePtr( pWnd );
2832 /* In Windows, ShowOwnedPopups(FALSE) generates
2833 * WM_SHOWWINDOW messages with SW_PARENTCLOSING,
2834 * regardless of the state of the owner
2836 SendMessageW(win_array[count], WM_SHOWWINDOW, SW_HIDE, SW_PARENTCLOSING);
2840 WIN_ReleasePtr( pWnd );
2842 HeapFree( GetProcessHeap(), 0, win_array );
2847 /*******************************************************************
2848 * GetLastActivePopup (USER32.@)
2850 HWND WINAPI GetLastActivePopup( HWND hwnd )
2854 SERVER_START_REQ( get_window_info )
2857 if (!wine_server_call_err( req )) retval = reply->last_active;
2864 /*******************************************************************
2867 * Build an array of the children of a given window. The array must be
2868 * freed with HeapFree. Returns NULL when no windows are found.
2870 HWND *WIN_ListChildren( HWND hwnd )
2872 return list_window_children( 0, hwnd, NULL, 0 );
2876 /*******************************************************************
2877 * EnumWindows (USER32.@)
2879 BOOL WINAPI EnumWindows( WNDENUMPROC lpEnumFunc, LPARAM lParam )
2885 USER_CheckNotLock();
2887 /* We have to build a list of all windows first, to avoid */
2888 /* unpleasant side-effects, for instance if the callback */
2889 /* function changes the Z-order of the windows. */
2891 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return TRUE;
2893 /* Now call the callback function for every window */
2895 for (i = 0; list[i]; i++)
2897 /* Make sure that the window still exists */
2898 if (!IsWindow( list[i] )) continue;
2899 if (!(ret = lpEnumFunc( list[i], lParam ))) break;
2901 HeapFree( GetProcessHeap(), 0, list );
2906 /**********************************************************************
2907 * EnumThreadWindows (USER32.@)
2909 BOOL WINAPI EnumThreadWindows( DWORD id, WNDENUMPROC func, LPARAM lParam )
2914 USER_CheckNotLock();
2916 if (!(list = list_window_children( 0, GetDesktopWindow(), NULL, id ))) return TRUE;
2918 /* Now call the callback function for every window */
2920 for (i = 0; list[i]; i++)
2921 if (!func( list[i], lParam )) break;
2922 HeapFree( GetProcessHeap(), 0, list );
2927 /***********************************************************************
2928 * EnumDesktopWindows (USER32.@)
2930 BOOL WINAPI EnumDesktopWindows( HDESK desktop, WNDENUMPROC func, LPARAM lparam )
2935 USER_CheckNotLock();
2937 if (!(list = list_window_children( desktop, 0, NULL, 0 ))) return TRUE;
2939 for (i = 0; list[i]; i++)
2940 if (!func( list[i], lparam )) break;
2941 HeapFree( GetProcessHeap(), 0, list );
2946 /**********************************************************************
2947 * WIN_EnumChildWindows
2949 * Helper function for EnumChildWindows().
2951 static BOOL WIN_EnumChildWindows( HWND *list, WNDENUMPROC func, LPARAM lParam )
2956 for ( ; *list; list++)
2958 /* Make sure that the window still exists */
2959 if (!IsWindow( *list )) continue;
2960 /* Build children list first */
2961 childList = WIN_ListChildren( *list );
2963 ret = func( *list, lParam );
2967 if (ret) ret = WIN_EnumChildWindows( childList, func, lParam );
2968 HeapFree( GetProcessHeap(), 0, childList );
2970 if (!ret) return FALSE;
2976 /**********************************************************************
2977 * EnumChildWindows (USER32.@)
2979 BOOL WINAPI EnumChildWindows( HWND parent, WNDENUMPROC func, LPARAM lParam )
2984 USER_CheckNotLock();
2986 if (!(list = WIN_ListChildren( parent ))) return FALSE;
2987 ret = WIN_EnumChildWindows( list, func, lParam );
2988 HeapFree( GetProcessHeap(), 0, list );
2993 /*******************************************************************
2994 * AnyPopup (USER.52)
2996 BOOL16 WINAPI AnyPopup16(void)
3002 /*******************************************************************
3003 * AnyPopup (USER32.@)
3005 BOOL WINAPI AnyPopup(void)
3009 HWND *list = WIN_ListChildren( GetDesktopWindow() );
3011 if (!list) return FALSE;
3012 for (i = 0; list[i]; i++)
3014 if (IsWindowVisible( list[i] ) && GetWindow( list[i], GW_OWNER )) break;
3016 retvalue = (list[i] != 0);
3017 HeapFree( GetProcessHeap(), 0, list );
3022 /*******************************************************************
3023 * FlashWindow (USER32.@)
3025 BOOL WINAPI FlashWindow( HWND hWnd, BOOL bInvert )
3029 TRACE("%p\n", hWnd);
3031 if (IsIconic( hWnd ))
3033 RedrawWindow( hWnd, 0, 0, RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW | RDW_FRAME );
3035 wndPtr = WIN_GetPtr(hWnd);
3036 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
3037 if (bInvert && !(wndPtr->flags & WIN_NCACTIVATED))
3039 wndPtr->flags |= WIN_NCACTIVATED;
3043 wndPtr->flags &= ~WIN_NCACTIVATED;
3045 WIN_ReleasePtr( wndPtr );
3052 wndPtr = WIN_GetPtr(hWnd);
3053 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
3054 hWnd = wndPtr->hwndSelf; /* make it a full handle */
3056 if (bInvert) wparam = !(wndPtr->flags & WIN_NCACTIVATED);
3057 else wparam = (hWnd == GetForegroundWindow());
3059 WIN_ReleasePtr( wndPtr );
3060 SendMessageW( hWnd, WM_NCACTIVATE, wparam, (LPARAM)0 );
3065 /*******************************************************************
3066 * FlashWindowEx (USER32.@)
3068 BOOL WINAPI FlashWindowEx( PFLASHWINFO pfwi )
3070 FIXME("%p\n", pfwi);
3074 /*******************************************************************
3075 * GetWindowContextHelpId (USER32.@)
3077 DWORD WINAPI GetWindowContextHelpId( HWND hwnd )
3080 WND *wnd = WIN_GetPtr( hwnd );
3081 if (!wnd || wnd == WND_DESKTOP) return 0;
3082 if (wnd == WND_OTHER_PROCESS)
3084 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
3087 retval = wnd->helpContext;
3088 WIN_ReleasePtr( wnd );
3093 /*******************************************************************
3094 * SetWindowContextHelpId (USER32.@)
3096 BOOL WINAPI SetWindowContextHelpId( HWND hwnd, DWORD id )
3098 WND *wnd = WIN_GetPtr( hwnd );
3099 if (!wnd || wnd == WND_DESKTOP) return FALSE;
3100 if (wnd == WND_OTHER_PROCESS)
3102 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
3105 wnd->helpContext = id;
3106 WIN_ReleasePtr( wnd );
3111 /*******************************************************************
3112 * DragDetect (USER32.@)
3114 BOOL WINAPI DragDetect( HWND hWnd, POINT pt )
3118 WORD wDragWidth = GetSystemMetrics(SM_CXDRAG);
3119 WORD wDragHeight= GetSystemMetrics(SM_CYDRAG);
3121 rect.left = pt.x - wDragWidth;
3122 rect.right = pt.x + wDragWidth;
3124 rect.top = pt.y - wDragHeight;
3125 rect.bottom = pt.y + wDragHeight;
3131 while (PeekMessageW( &msg, 0, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE ))
3133 if( msg.message == WM_LBUTTONUP )
3138 if( msg.message == WM_MOUSEMOVE )
3141 tmp.x = (short)LOWORD(msg.lParam);
3142 tmp.y = (short)HIWORD(msg.lParam);
3143 if( !PtInRect( &rect, tmp ))
3155 /******************************************************************************
3156 * GetWindowModuleFileNameA (USER32.@)
3158 UINT WINAPI GetWindowModuleFileNameA( HWND hwnd, LPSTR lpszFileName, UINT cchFileNameMax)
3160 FIXME("GetWindowModuleFileNameA(hwnd %p, lpszFileName %p, cchFileNameMax %u) stub!\n",
3161 hwnd, lpszFileName, cchFileNameMax);
3165 /******************************************************************************
3166 * GetWindowModuleFileNameW (USER32.@)
3168 UINT WINAPI GetWindowModuleFileNameW( HWND hwnd, LPWSTR lpszFileName, UINT cchFileNameMax)
3170 FIXME("GetWindowModuleFileNameW(hwnd %p, lpszFileName %p, cchFileNameMax %u) stub!\n",
3171 hwnd, lpszFileName, cchFileNameMax);
3175 /******************************************************************************
3176 * GetWindowInfo (USER32.@)
3178 * Note: tests show that Windows doesn't check cbSize of the structure.
3180 BOOL WINAPI GetWindowInfo( HWND hwnd, PWINDOWINFO pwi)
3182 if (!pwi) return FALSE;
3183 if (!IsWindow(hwnd)) return FALSE;
3185 GetWindowRect(hwnd, &pwi->rcWindow);
3186 GetClientRect(hwnd, &pwi->rcClient);
3187 /* translate to screen coordinates */
3188 MapWindowPoints(hwnd, 0, (LPPOINT)&pwi->rcClient, 2);
3190 pwi->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
3191 pwi->dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
3192 pwi->dwWindowStatus = ((GetActiveWindow() == hwnd) ? WS_ACTIVECAPTION : 0);
3194 pwi->cxWindowBorders = pwi->rcClient.left - pwi->rcWindow.left;
3195 pwi->cyWindowBorders = pwi->rcWindow.bottom - pwi->rcClient.bottom;
3197 pwi->atomWindowType = GetClassLongW( hwnd, GCW_ATOM );
3198 pwi->wCreatorVersion = 0x0400;
3203 /******************************************************************************
3204 * SwitchDesktop (USER32.@)
3206 * NOTES: Sets the current input or interactive desktop.
3208 BOOL WINAPI SwitchDesktop( HDESK hDesktop)
3210 FIXME("SwitchDesktop(hwnd %p) stub!\n", hDesktop);
3214 /*****************************************************************************
3215 * SetLayeredWindowAttributes (USER32.@)
3217 BOOL WINAPI SetLayeredWindowAttributes( HWND hWnd, COLORREF rgbKey,
3218 BYTE bAlpha, DWORD dwFlags )
3220 FIXME("(%p,0x%.8x,%d,%d): stub!\n", hWnd, rgbKey, bAlpha, dwFlags);
3224 /*****************************************************************************
3225 * GetLayeredWindowAttributes (USER32.@)
3227 BOOL WINAPI GetLayeredWindowAttributes( HWND hWnd, COLORREF *prgbKey,
3228 BYTE *pbAlpha, DWORD *pdwFlags )
3230 FIXME("(%p,%p,%p,%p): stub!\n", hWnd, prgbKey, pbAlpha, pdwFlags);
3234 /*****************************************************************************
3235 * UpdateLayeredWindow (USER32.@)
3237 BOOL WINAPI UpdateLayeredWindow( HWND hwnd, HDC hdcDst, POINT *pptDst, SIZE *psize,
3238 HDC hdcSrc, POINT *pptSrc, COLORREF crKey, BLENDFUNCTION *pblend,
3245 FIXME("(%p,%p,%p,%p,%p,%p,0x%08x,%p,%d): stub!\n",
3246 hwnd, hdcDst, pptDst, psize, hdcSrc, pptSrc, crKey, pblend, dwFlags);
3251 /* 64bit versions */
3253 #ifdef GetWindowLongPtrW
3254 #undef GetWindowLongPtrW
3257 #ifdef GetWindowLongPtrA
3258 #undef GetWindowLongPtrA
3261 #ifdef SetWindowLongPtrW
3262 #undef SetWindowLongPtrW
3265 #ifdef SetWindowLongPtrA
3266 #undef SetWindowLongPtrA
3269 /*****************************************************************************
3270 * GetWindowLongPtrW (USER32.@)
3272 LONG_PTR WINAPI GetWindowLongPtrW( HWND hwnd, INT offset )
3274 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG_PTR), TRUE );
3277 /*****************************************************************************
3278 * GetWindowLongPtrA (USER32.@)
3280 LONG_PTR WINAPI GetWindowLongPtrA( HWND hwnd, INT offset )
3282 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG_PTR), FALSE );
3285 /*****************************************************************************
3286 * SetWindowLongPtrW (USER32.@)
3288 LONG_PTR WINAPI SetWindowLongPtrW( HWND hwnd, INT offset, LONG_PTR newval )
3290 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG_PTR), newval, TRUE );
3293 /*****************************************************************************
3294 * SetWindowLongPtrA (USER32.@)
3296 LONG_PTR WINAPI SetWindowLongPtrA( HWND hwnd, INT offset, LONG_PTR newval )
3298 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG_PTR), newval, FALSE );