d3d11: Make extracting input signature more robust.
[wine.git] / dlls / user32 / win.c
blobf3b4ef547c685a4680f358af2cf878f5b863d925
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winver.h"
32 #include "wine/server.h"
33 #include "wine/unicode.h"
34 #include "win.h"
35 #include "user_private.h"
36 #include "controls.h"
37 #include "winerror.h"
38 #include "wine/gdi_driver.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 static DWORD process_layout = ~0u;
48 static struct list window_surfaces = LIST_INIT( window_surfaces );
50 static CRITICAL_SECTION surfaces_section;
51 static CRITICAL_SECTION_DEBUG critsect_debug =
53 0, 0, &surfaces_section,
54 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
55 0, 0, { (DWORD_PTR)(__FILE__ ": surfaces_section") }
57 static CRITICAL_SECTION surfaces_section = { &critsect_debug, -1, 0, 0, 0, 0 };
59 /**********************************************************************/
61 /* helper for Get/SetWindowLong */
62 static inline LONG_PTR get_win_data( const void *ptr, UINT size )
64 if (size == sizeof(WORD))
66 WORD ret;
67 memcpy( &ret, ptr, sizeof(ret) );
68 return ret;
70 else if (size == sizeof(DWORD))
72 DWORD ret;
73 memcpy( &ret, ptr, sizeof(ret) );
74 return ret;
76 else
78 LONG_PTR ret;
79 memcpy( &ret, ptr, sizeof(ret) );
80 return ret;
84 /* helper for Get/SetWindowLong */
85 static inline void set_win_data( void *ptr, LONG_PTR val, UINT size )
87 if (size == sizeof(WORD))
89 WORD newval = val;
90 memcpy( ptr, &newval, sizeof(newval) );
92 else if (size == sizeof(DWORD))
94 DWORD newval = val;
95 memcpy( ptr, &newval, sizeof(newval) );
97 else
99 memcpy( ptr, &val, sizeof(val) );
104 static void *user_handles[NB_USER_HANDLES];
106 /***********************************************************************
107 * alloc_user_handle
109 HANDLE alloc_user_handle( struct user_object *ptr, enum user_obj_type type )
111 HANDLE handle = 0;
113 SERVER_START_REQ( alloc_user_handle )
115 if (!wine_server_call_err( req )) handle = wine_server_ptr_handle( reply->handle );
117 SERVER_END_REQ;
119 if (handle)
121 UINT index = USER_HANDLE_TO_INDEX( handle );
123 assert( index < NB_USER_HANDLES );
124 ptr->handle = handle;
125 ptr->type = type;
126 InterlockedExchangePointer( &user_handles[index], ptr );
128 return handle;
132 /***********************************************************************
133 * get_user_handle_ptr
135 void *get_user_handle_ptr( HANDLE handle, enum user_obj_type type )
137 struct user_object *ptr;
138 WORD index = USER_HANDLE_TO_INDEX( handle );
140 if (index >= NB_USER_HANDLES) return NULL;
142 USER_Lock();
143 if ((ptr = user_handles[index]))
145 if (ptr->type == type &&
146 ((UINT)(UINT_PTR)ptr->handle == (UINT)(UINT_PTR)handle ||
147 !HIWORD(handle) || HIWORD(handle) == 0xffff))
148 return ptr;
149 ptr = NULL;
151 else ptr = OBJ_OTHER_PROCESS;
152 USER_Unlock();
153 return ptr;
157 /***********************************************************************
158 * release_user_handle_ptr
160 void release_user_handle_ptr( void *ptr )
162 assert( ptr && ptr != OBJ_OTHER_PROCESS );
163 USER_Unlock();
167 /***********************************************************************
168 * free_user_handle
170 void *free_user_handle( HANDLE handle, enum user_obj_type type )
172 struct user_object *ptr;
173 WORD index = USER_HANDLE_TO_INDEX( handle );
175 if ((ptr = get_user_handle_ptr( handle, type )) && ptr != OBJ_OTHER_PROCESS)
177 SERVER_START_REQ( free_user_handle )
179 req->handle = wine_server_user_handle( handle );
180 if (wine_server_call( req )) ptr = NULL;
181 else InterlockedCompareExchangePointer( &user_handles[index], NULL, ptr );
183 SERVER_END_REQ;
184 USER_Unlock();
186 return ptr;
190 /***********************************************************************
191 * create_window_handle
193 * Create a window handle with the server.
195 static WND *create_window_handle( HWND parent, HWND owner, LPCWSTR name,
196 HINSTANCE instance, BOOL unicode )
198 WORD index;
199 WND *win;
200 HWND handle = 0, full_parent = 0, full_owner = 0;
201 struct tagCLASS *class = NULL;
202 int extra_bytes = 0;
204 SERVER_START_REQ( create_window )
206 req->parent = wine_server_user_handle( parent );
207 req->owner = wine_server_user_handle( owner );
208 req->instance = wine_server_client_ptr( instance );
209 if (!(req->atom = get_int_atom_value( name )) && name)
210 wine_server_add_data( req, name, strlenW(name)*sizeof(WCHAR) );
211 if (!wine_server_call_err( req ))
213 handle = wine_server_ptr_handle( reply->handle );
214 full_parent = wine_server_ptr_handle( reply->parent );
215 full_owner = wine_server_ptr_handle( reply->owner );
216 extra_bytes = reply->extra;
217 class = wine_server_get_ptr( reply->class_ptr );
220 SERVER_END_REQ;
222 if (!handle)
224 WARN( "error %d creating window\n", GetLastError() );
225 return NULL;
228 if (!(win = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
229 sizeof(WND) + extra_bytes - sizeof(win->wExtra) )))
231 SERVER_START_REQ( destroy_window )
233 req->handle = wine_server_user_handle( handle );
234 wine_server_call( req );
236 SERVER_END_REQ;
237 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
238 return NULL;
241 if (!parent) /* if parent is 0 we don't have a desktop window yet */
243 struct user_thread_info *thread_info = get_user_thread_info();
245 if (name == (LPCWSTR)DESKTOP_CLASS_ATOM)
247 if (!thread_info->top_window) thread_info->top_window = full_parent ? full_parent : handle;
248 else assert( full_parent == thread_info->top_window );
249 if (full_parent && !USER_Driver->pCreateDesktopWindow( thread_info->top_window ))
250 ERR( "failed to create desktop window\n" );
252 else /* HWND_MESSAGE parent */
254 if (!thread_info->msg_window && !full_parent) thread_info->msg_window = handle;
258 USER_Lock();
260 index = USER_HANDLE_TO_INDEX(handle);
261 assert( index < NB_USER_HANDLES );
262 win->obj.handle = handle;
263 win->obj.type = USER_WINDOW;
264 win->parent = full_parent;
265 win->owner = full_owner;
266 win->class = class;
267 win->winproc = get_class_winproc( class );
268 win->cbWndExtra = extra_bytes;
269 InterlockedExchangePointer( &user_handles[index], win );
270 if (WINPROC_IsUnicode( win->winproc, unicode )) win->flags |= WIN_ISUNICODE;
271 return win;
275 /***********************************************************************
276 * free_window_handle
278 * Free a window handle.
280 static void free_window_handle( HWND hwnd )
282 struct user_object *ptr;
283 WORD index = USER_HANDLE_TO_INDEX(hwnd);
285 if ((ptr = get_user_handle_ptr( hwnd, USER_WINDOW )) && ptr != OBJ_OTHER_PROCESS)
287 SERVER_START_REQ( destroy_window )
289 req->handle = wine_server_user_handle( hwnd );
290 if (wine_server_call_err( req )) ptr = NULL;
291 else InterlockedCompareExchangePointer( &user_handles[index], NULL, ptr );
293 SERVER_END_REQ;
294 USER_Unlock();
295 HeapFree( GetProcessHeap(), 0, ptr );
300 /*******************************************************************
301 * list_window_children
303 * Build an array of the children of a given window. The array must be
304 * freed with HeapFree. Returns NULL when no windows are found.
306 static HWND *list_window_children( HDESK desktop, HWND hwnd, LPCWSTR class, DWORD tid )
308 HWND *list;
309 int i, size = 128;
310 ATOM atom = get_int_atom_value( class );
312 /* empty class is not the same as NULL class */
313 if (!atom && class && !class[0]) return NULL;
315 for (;;)
317 int count = 0;
319 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
321 SERVER_START_REQ( get_window_children )
323 req->desktop = wine_server_obj_handle( desktop );
324 req->parent = wine_server_user_handle( hwnd );
325 req->tid = tid;
326 req->atom = atom;
327 if (!atom && class) wine_server_add_data( req, class, strlenW(class)*sizeof(WCHAR) );
328 wine_server_set_reply( req, list, (size-1) * sizeof(user_handle_t) );
329 if (!wine_server_call( req )) count = reply->count;
331 SERVER_END_REQ;
332 if (count && count < size)
334 /* start from the end since HWND is potentially larger than user_handle_t */
335 for (i = count - 1; i >= 0; i--)
336 list[i] = wine_server_ptr_handle( ((user_handle_t *)list)[i] );
337 list[count] = 0;
338 return list;
340 HeapFree( GetProcessHeap(), 0, list );
341 if (!count) break;
342 size = count + 1; /* restart with a large enough buffer */
344 return NULL;
348 /*******************************************************************
349 * list_window_parents
351 * Build an array of all parents of a given window, starting with
352 * the immediate parent. The array must be freed with HeapFree.
354 static HWND *list_window_parents( HWND hwnd )
356 WND *win;
357 HWND current, *list;
358 int i, pos = 0, size = 16, count;
360 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) return NULL;
362 current = hwnd;
363 for (;;)
365 if (!(win = WIN_GetPtr( current ))) goto empty;
366 if (win == WND_OTHER_PROCESS) break; /* need to do it the hard way */
367 if (win == WND_DESKTOP)
369 if (!pos) goto empty;
370 list[pos] = 0;
371 return list;
373 list[pos] = current = win->parent;
374 WIN_ReleasePtr( win );
375 if (!current) return list;
376 if (++pos == size - 1)
378 /* need to grow the list */
379 HWND *new_list = HeapReAlloc( GetProcessHeap(), 0, list, (size+16) * sizeof(HWND) );
380 if (!new_list) goto empty;
381 list = new_list;
382 size += 16;
386 /* at least one parent belongs to another process, have to query the server */
388 for (;;)
390 count = 0;
391 SERVER_START_REQ( get_window_parents )
393 req->handle = wine_server_user_handle( hwnd );
394 wine_server_set_reply( req, list, (size-1) * sizeof(user_handle_t) );
395 if (!wine_server_call( req )) count = reply->count;
397 SERVER_END_REQ;
398 if (!count) goto empty;
399 if (size > count)
401 /* start from the end since HWND is potentially larger than user_handle_t */
402 for (i = count - 1; i >= 0; i--)
403 list[i] = wine_server_ptr_handle( ((user_handle_t *)list)[i] );
404 list[count] = 0;
405 return list;
407 HeapFree( GetProcessHeap(), 0, list );
408 size = count + 1;
409 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) return NULL;
412 empty:
413 HeapFree( GetProcessHeap(), 0, list );
414 return NULL;
418 /*******************************************************************
419 * send_parent_notify
421 static void send_parent_notify( HWND hwnd, UINT msg )
423 if ((GetWindowLongW( hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD &&
424 !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY))
426 HWND parent = GetParent(hwnd);
427 if (parent && parent != GetDesktopWindow())
428 SendMessageW( parent, WM_PARENTNOTIFY,
429 MAKEWPARAM( msg, GetWindowLongPtrW( hwnd, GWLP_ID )), (LPARAM)hwnd );
434 /*******************************************************************
435 * update_window_state
437 * Trigger an update of the window's driver state and surface.
439 static void update_window_state( HWND hwnd )
441 RECT window_rect, client_rect, valid_rects[2];
443 WIN_GetRectangles( hwnd, COORDS_PARENT, &window_rect, &client_rect );
444 valid_rects[0] = valid_rects[1] = client_rect;
445 set_window_pos( hwnd, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE |
446 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW,
447 &window_rect, &client_rect, valid_rects );
451 /*******************************************************************
452 * get_server_window_text
454 * Retrieve the window text from the server.
456 static void get_server_window_text( HWND hwnd, LPWSTR text, INT count )
458 size_t len = 0;
460 SERVER_START_REQ( get_window_text )
462 req->handle = wine_server_user_handle( hwnd );
463 wine_server_set_reply( req, text, (count - 1) * sizeof(WCHAR) );
464 if (!wine_server_call_err( req )) len = wine_server_reply_size(reply);
466 SERVER_END_REQ;
467 text[len / sizeof(WCHAR)] = 0;
471 /*******************************************************************
472 * get_hwnd_message_parent
474 * Return the parent for HWND_MESSAGE windows.
476 HWND get_hwnd_message_parent(void)
478 struct user_thread_info *thread_info = get_user_thread_info();
480 if (!thread_info->msg_window) GetDesktopWindow(); /* trigger creation */
481 return thread_info->msg_window;
485 /*******************************************************************
486 * is_desktop_window
488 * Check if window is the desktop or the HWND_MESSAGE top parent.
490 BOOL is_desktop_window( HWND hwnd )
492 struct user_thread_info *thread_info = get_user_thread_info();
494 if (!hwnd) return FALSE;
495 if (hwnd == thread_info->top_window) return TRUE;
496 if (hwnd == thread_info->msg_window) return TRUE;
498 if (!HIWORD(hwnd) || HIWORD(hwnd) == 0xffff)
500 if (LOWORD(thread_info->top_window) == LOWORD(hwnd)) return TRUE;
501 if (LOWORD(thread_info->msg_window) == LOWORD(hwnd)) return TRUE;
503 return FALSE;
507 /*******************************************************************
508 * Dummy window surface for windows that shouldn't get painted.
511 static void dummy_surface_lock( struct window_surface *window_surface )
513 /* nothing to do */
516 static void dummy_surface_unlock( struct window_surface *window_surface )
518 /* nothing to do */
521 static void *dummy_surface_get_bitmap_info( struct window_surface *window_surface, BITMAPINFO *info )
523 static DWORD dummy_data;
525 info->bmiHeader.biSize = sizeof( info->bmiHeader );
526 info->bmiHeader.biWidth = dummy_surface.rect.right;
527 info->bmiHeader.biHeight = dummy_surface.rect.bottom;
528 info->bmiHeader.biPlanes = 1;
529 info->bmiHeader.biBitCount = 32;
530 info->bmiHeader.biCompression = BI_RGB;
531 info->bmiHeader.biSizeImage = 0;
532 info->bmiHeader.biXPelsPerMeter = 0;
533 info->bmiHeader.biYPelsPerMeter = 0;
534 info->bmiHeader.biClrUsed = 0;
535 info->bmiHeader.biClrImportant = 0;
536 return &dummy_data;
539 static RECT *dummy_surface_get_bounds( struct window_surface *window_surface )
541 static RECT dummy_bounds;
542 return &dummy_bounds;
545 static void dummy_surface_set_region( struct window_surface *window_surface, HRGN region )
547 /* nothing to do */
550 static void dummy_surface_flush( struct window_surface *window_surface )
552 /* nothing to do */
555 static void dummy_surface_destroy( struct window_surface *window_surface )
557 /* nothing to do */
560 static const struct window_surface_funcs dummy_surface_funcs =
562 dummy_surface_lock,
563 dummy_surface_unlock,
564 dummy_surface_get_bitmap_info,
565 dummy_surface_get_bounds,
566 dummy_surface_set_region,
567 dummy_surface_flush,
568 dummy_surface_destroy
571 struct window_surface dummy_surface = { &dummy_surface_funcs, { NULL, NULL }, 1, { 0, 0, 1, 1 } };
574 /*******************************************************************
575 * register_window_surface
577 * Register a window surface in the global list, possibly replacing another one.
579 void register_window_surface( struct window_surface *old, struct window_surface *new )
581 if (old == new) return;
582 EnterCriticalSection( &surfaces_section );
583 if (old && old != &dummy_surface) list_remove( &old->entry );
584 if (new && new != &dummy_surface) list_add_tail( &window_surfaces, &new->entry );
585 LeaveCriticalSection( &surfaces_section );
589 /*******************************************************************
590 * flush_window_surfaces
592 * Flush pending output from all window surfaces.
594 void flush_window_surfaces( BOOL idle )
596 static DWORD last_idle;
597 DWORD now;
598 struct window_surface *surface;
600 EnterCriticalSection( &surfaces_section );
601 now = GetTickCount();
602 if (idle) last_idle = now;
603 /* if not idle, we only flush if there's evidence that the app never goes idle */
604 else if ((int)(now - last_idle) < 50) goto done;
606 LIST_FOR_EACH_ENTRY( surface, &window_surfaces, struct window_surface, entry )
607 surface->funcs->flush( surface );
608 done:
609 LeaveCriticalSection( &surfaces_section );
613 /***********************************************************************
614 * WIN_GetPtr
616 * Return a pointer to the WND structure if local to the process,
617 * or WND_OTHER_PROCESS if handle may be valid in other process.
618 * If ret value is a valid pointer, it must be released with WIN_ReleasePtr.
620 WND *WIN_GetPtr( HWND hwnd )
622 WND *ptr;
624 if ((ptr = get_user_handle_ptr( hwnd, USER_WINDOW )) == WND_OTHER_PROCESS)
626 if (is_desktop_window( hwnd )) ptr = WND_DESKTOP;
628 return ptr;
632 /***********************************************************************
633 * WIN_IsCurrentProcess
635 * Check whether a given window belongs to the current process (and return the full handle).
637 HWND WIN_IsCurrentProcess( HWND hwnd )
639 WND *ptr;
640 HWND ret;
642 if (!(ptr = WIN_GetPtr( hwnd )) || ptr == WND_OTHER_PROCESS || ptr == WND_DESKTOP) return 0;
643 ret = ptr->obj.handle;
644 WIN_ReleasePtr( ptr );
645 return ret;
649 /***********************************************************************
650 * WIN_IsCurrentThread
652 * Check whether a given window belongs to the current thread (and return the full handle).
654 HWND WIN_IsCurrentThread( HWND hwnd )
656 WND *ptr;
657 HWND ret = 0;
659 if (!(ptr = WIN_GetPtr( hwnd )) || ptr == WND_OTHER_PROCESS || ptr == WND_DESKTOP) return 0;
660 if (ptr->tid == GetCurrentThreadId()) ret = ptr->obj.handle;
661 WIN_ReleasePtr( ptr );
662 return ret;
666 /***********************************************************************
667 * win_set_flags
669 * Set the flags of a window and return the previous value.
671 UINT win_set_flags( HWND hwnd, UINT set_mask, UINT clear_mask )
673 UINT ret;
674 WND *ptr = WIN_GetPtr( hwnd );
676 if (!ptr || ptr == WND_OTHER_PROCESS || ptr == WND_DESKTOP) return 0;
677 ret = ptr->flags;
678 ptr->flags = (ret & ~clear_mask) | set_mask;
679 WIN_ReleasePtr( ptr );
680 return ret;
684 /***********************************************************************
685 * WIN_GetFullHandle
687 * Convert a possibly truncated window handle to a full 32-bit handle.
689 HWND WIN_GetFullHandle( HWND hwnd )
691 WND *ptr;
693 if (!hwnd || (ULONG_PTR)hwnd >> 16) return hwnd;
694 if (LOWORD(hwnd) <= 1 || LOWORD(hwnd) == 0xffff) return hwnd;
695 /* do sign extension for -2 and -3 */
696 if (LOWORD(hwnd) >= (WORD)-3) return (HWND)(LONG_PTR)(INT16)LOWORD(hwnd);
698 if (!(ptr = WIN_GetPtr( hwnd ))) return hwnd;
700 if (ptr == WND_DESKTOP)
702 if (LOWORD(hwnd) == LOWORD(GetDesktopWindow())) return GetDesktopWindow();
703 else return get_hwnd_message_parent();
706 if (ptr != WND_OTHER_PROCESS)
708 hwnd = ptr->obj.handle;
709 WIN_ReleasePtr( ptr );
711 else /* may belong to another process */
713 SERVER_START_REQ( get_window_info )
715 req->handle = wine_server_user_handle( hwnd );
716 if (!wine_server_call_err( req )) hwnd = wine_server_ptr_handle( reply->full_handle );
718 SERVER_END_REQ;
720 return hwnd;
724 /***********************************************************************
725 * WIN_SetOwner
727 * Change the owner of a window.
729 HWND WIN_SetOwner( HWND hwnd, HWND owner )
731 WND *win = WIN_GetPtr( hwnd );
732 HWND ret = 0;
734 if (!win || win == WND_DESKTOP) return 0;
735 if (win == WND_OTHER_PROCESS)
737 if (IsWindow(hwnd)) ERR( "cannot set owner %p on other process window %p\n", owner, hwnd );
738 return 0;
740 SERVER_START_REQ( set_window_owner )
742 req->handle = wine_server_user_handle( hwnd );
743 req->owner = wine_server_user_handle( owner );
744 if (!wine_server_call( req ))
746 win->owner = wine_server_ptr_handle( reply->full_owner );
747 ret = wine_server_ptr_handle( reply->prev_owner );
750 SERVER_END_REQ;
751 WIN_ReleasePtr( win );
752 return ret;
756 /***********************************************************************
757 * WIN_SetStyle
759 * Change the style of a window.
761 ULONG WIN_SetStyle( HWND hwnd, ULONG set_bits, ULONG clear_bits )
763 BOOL ok, made_visible = FALSE;
764 STYLESTRUCT style;
765 WND *win = WIN_GetPtr( hwnd );
767 if (!win || win == WND_DESKTOP) return 0;
768 if (win == WND_OTHER_PROCESS)
770 if (IsWindow(hwnd))
771 ERR( "cannot set style %x/%x on other process window %p\n",
772 set_bits, clear_bits, hwnd );
773 return 0;
775 style.styleOld = win->dwStyle;
776 style.styleNew = (win->dwStyle | set_bits) & ~clear_bits;
777 if (style.styleNew == style.styleOld)
779 WIN_ReleasePtr( win );
780 return style.styleNew;
782 SERVER_START_REQ( set_window_info )
784 req->handle = wine_server_user_handle( hwnd );
785 req->flags = SET_WIN_STYLE;
786 req->style = style.styleNew;
787 req->extra_offset = -1;
788 if ((ok = !wine_server_call( req )))
790 style.styleOld = reply->old_style;
791 win->dwStyle = style.styleNew;
794 SERVER_END_REQ;
796 if (ok && ((style.styleOld ^ style.styleNew) & WS_VISIBLE))
798 made_visible = (style.styleNew & WS_VISIBLE) != 0;
799 invalidate_dce( win, NULL );
801 WIN_ReleasePtr( win );
803 if (!ok) return 0;
805 USER_Driver->pSetWindowStyle( hwnd, GWL_STYLE, &style );
806 if (made_visible) update_window_state( hwnd );
808 return style.styleOld;
812 /***********************************************************************
813 * WIN_GetRectangles
815 * Get the window and client rectangles.
817 BOOL WIN_GetRectangles( HWND hwnd, enum coords_relative relative, RECT *rectWindow, RECT *rectClient )
819 WND *win = WIN_GetPtr( hwnd );
820 BOOL ret = TRUE;
822 if (!win)
824 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
825 return FALSE;
827 if (win == WND_DESKTOP)
829 RECT rect;
830 rect.left = rect.top = 0;
831 if (hwnd == get_hwnd_message_parent())
833 rect.right = 100;
834 rect.bottom = 100;
836 else
838 rect.right = GetSystemMetrics(SM_CXSCREEN);
839 rect.bottom = GetSystemMetrics(SM_CYSCREEN);
841 if (rectWindow) *rectWindow = rect;
842 if (rectClient) *rectClient = rect;
843 return TRUE;
845 if (win != WND_OTHER_PROCESS)
847 RECT window_rect = win->rectWindow, client_rect = win->rectClient;
849 switch (relative)
851 case COORDS_CLIENT:
852 OffsetRect( &window_rect, -win->rectClient.left, -win->rectClient.top );
853 OffsetRect( &client_rect, -win->rectClient.left, -win->rectClient.top );
854 if (win->dwExStyle & WS_EX_LAYOUTRTL)
855 mirror_rect( &win->rectClient, &window_rect );
856 break;
857 case COORDS_WINDOW:
858 OffsetRect( &window_rect, -win->rectWindow.left, -win->rectWindow.top );
859 OffsetRect( &client_rect, -win->rectWindow.left, -win->rectWindow.top );
860 if (win->dwExStyle & WS_EX_LAYOUTRTL)
861 mirror_rect( &win->rectWindow, &client_rect );
862 break;
863 case COORDS_PARENT:
864 if (win->parent)
866 WND *parent = WIN_GetPtr( win->parent );
867 if (parent == WND_DESKTOP) break;
868 if (!parent || parent == WND_OTHER_PROCESS)
870 WIN_ReleasePtr( win );
871 goto other_process;
873 if (parent->flags & WIN_CHILDREN_MOVED)
875 WIN_ReleasePtr( parent );
876 WIN_ReleasePtr( win );
877 goto other_process;
879 if (parent->dwExStyle & WS_EX_LAYOUTRTL)
881 mirror_rect( &parent->rectClient, &window_rect );
882 mirror_rect( &parent->rectClient, &client_rect );
884 WIN_ReleasePtr( parent );
886 break;
887 case COORDS_SCREEN:
888 while (win->parent)
890 WND *parent = WIN_GetPtr( win->parent );
891 if (parent == WND_DESKTOP) break;
892 if (!parent || parent == WND_OTHER_PROCESS)
894 WIN_ReleasePtr( win );
895 goto other_process;
897 WIN_ReleasePtr( win );
898 if (parent->flags & WIN_CHILDREN_MOVED)
900 WIN_ReleasePtr( parent );
901 goto other_process;
903 win = parent;
904 if (win->parent)
906 OffsetRect( &window_rect, win->rectClient.left, win->rectClient.top );
907 OffsetRect( &client_rect, win->rectClient.left, win->rectClient.top );
910 break;
912 if (rectWindow) *rectWindow = window_rect;
913 if (rectClient) *rectClient = client_rect;
914 WIN_ReleasePtr( win );
915 return TRUE;
918 other_process:
919 SERVER_START_REQ( get_window_rectangles )
921 req->handle = wine_server_user_handle( hwnd );
922 req->relative = relative;
923 if ((ret = !wine_server_call_err( req )))
925 if (rectWindow)
927 rectWindow->left = reply->window.left;
928 rectWindow->top = reply->window.top;
929 rectWindow->right = reply->window.right;
930 rectWindow->bottom = reply->window.bottom;
932 if (rectClient)
934 rectClient->left = reply->client.left;
935 rectClient->top = reply->client.top;
936 rectClient->right = reply->client.right;
937 rectClient->bottom = reply->client.bottom;
941 SERVER_END_REQ;
942 return ret;
946 /***********************************************************************
947 * WIN_DestroyWindow
949 * Destroy storage associated to a window. "Internals" p.358
951 LRESULT WIN_DestroyWindow( HWND hwnd )
953 WND *wndPtr;
954 HWND *list;
955 HMENU menu = 0, sys_menu;
956 HWND icon_title;
957 struct window_surface *surface;
959 TRACE("%p\n", hwnd );
961 /* destroy default IME window */
962 if (win_set_flags( hwnd, 0, WIN_HAS_IME_WIN ) & WIN_HAS_IME_WIN)
964 TRACE("unregister IME window for %p\n", hwnd);
965 imm_unregister_window( hwnd );
968 /* free child windows */
969 if ((list = WIN_ListChildren( hwnd )))
971 int i;
972 for (i = 0; list[i]; i++)
974 if (WIN_IsCurrentThread( list[i] )) WIN_DestroyWindow( list[i] );
975 else SendMessageW( list[i], WM_WINE_DESTROYWINDOW, 0, 0 );
977 HeapFree( GetProcessHeap(), 0, list );
980 /* Unlink now so we won't bother with the children later on */
981 SERVER_START_REQ( set_parent )
983 req->handle = wine_server_user_handle( hwnd );
984 req->parent = 0;
985 wine_server_call( req );
987 SERVER_END_REQ;
990 * Send the WM_NCDESTROY to the window being destroyed.
992 SendMessageW( hwnd, WM_NCDESTROY, 0, 0 );
994 /* FIXME: do we need to fake QS_MOUSEMOVE wakebit? */
996 /* free resources associated with the window */
998 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
999 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
1000 menu = (HMENU)wndPtr->wIDmenu;
1001 sys_menu = wndPtr->hSysMenu;
1002 free_dce( wndPtr->dce, hwnd );
1003 wndPtr->dce = NULL;
1004 icon_title = wndPtr->icon_title;
1005 HeapFree( GetProcessHeap(), 0, wndPtr->text );
1006 wndPtr->text = NULL;
1007 HeapFree( GetProcessHeap(), 0, wndPtr->pScroll );
1008 wndPtr->pScroll = NULL;
1009 DestroyIcon( wndPtr->hIconSmall2 );
1010 surface = wndPtr->surface;
1011 wndPtr->surface = NULL;
1012 WIN_ReleasePtr( wndPtr );
1014 if (icon_title) DestroyWindow( icon_title );
1015 if (menu) DestroyMenu( menu );
1016 if (sys_menu) DestroyMenu( sys_menu );
1017 if (surface)
1019 register_window_surface( surface, NULL );
1020 window_surface_release( surface );
1023 USER_Driver->pDestroyWindow( hwnd );
1025 free_window_handle( hwnd );
1026 return 0;
1030 /***********************************************************************
1031 * destroy_thread_window
1033 * Destroy a window upon exit of its thread.
1035 static void destroy_thread_window( HWND hwnd )
1037 WND *wndPtr;
1038 HWND *list;
1039 HMENU menu = 0, sys_menu = 0;
1040 struct window_surface *surface = NULL;
1041 WORD index;
1043 /* free child windows */
1045 if ((list = WIN_ListChildren( hwnd )))
1047 int i;
1048 for (i = 0; list[i]; i++)
1050 if (WIN_IsCurrentThread( list[i] )) destroy_thread_window( list[i] );
1051 else SendMessageW( list[i], WM_WINE_DESTROYWINDOW, 0, 0 );
1053 HeapFree( GetProcessHeap(), 0, list );
1056 /* destroy the client-side storage */
1058 index = USER_HANDLE_TO_INDEX(hwnd);
1059 if (index >= NB_USER_HANDLES) return;
1060 USER_Lock();
1061 if ((wndPtr = user_handles[index]))
1063 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD) menu = (HMENU)wndPtr->wIDmenu;
1064 sys_menu = wndPtr->hSysMenu;
1065 free_dce( wndPtr->dce, hwnd );
1066 surface = wndPtr->surface;
1067 wndPtr->surface = NULL;
1068 InterlockedCompareExchangePointer( &user_handles[index], NULL, wndPtr );
1070 USER_Unlock();
1072 HeapFree( GetProcessHeap(), 0, wndPtr );
1073 if (menu) DestroyMenu( menu );
1074 if (sys_menu) DestroyMenu( sys_menu );
1075 if (surface)
1077 register_window_surface( surface, NULL );
1078 window_surface_release( surface );
1083 /***********************************************************************
1084 * destroy_thread_child_windows
1086 * Destroy child windows upon exit of its thread.
1088 static void destroy_thread_child_windows( HWND hwnd )
1090 HWND *list;
1091 int i;
1093 if (WIN_IsCurrentThread( hwnd ))
1095 destroy_thread_window( hwnd );
1097 else if ((list = WIN_ListChildren( hwnd )))
1099 for (i = 0; list[i]; i++) destroy_thread_child_windows( list[i] );
1100 HeapFree( GetProcessHeap(), 0, list );
1105 /***********************************************************************
1106 * WIN_DestroyThreadWindows
1108 * Destroy all children of 'wnd' owned by the current thread.
1110 void WIN_DestroyThreadWindows( HWND hwnd )
1112 HWND *list;
1113 int i;
1115 if (!(list = WIN_ListChildren( hwnd ))) return;
1117 /* reset owners of top-level windows */
1118 for (i = 0; list[i]; i++)
1120 if (!WIN_IsCurrentThread( list[i] ))
1122 HWND owner = GetWindow( list[i], GW_OWNER );
1123 if (owner && WIN_IsCurrentThread( owner )) WIN_SetOwner( list[i], 0 );
1127 for (i = 0; list[i]; i++) destroy_thread_child_windows( list[i] );
1128 HeapFree( GetProcessHeap(), 0, list );
1132 /***********************************************************************
1133 * WIN_FixCoordinates
1135 * Fix the coordinates - Helper for WIN_CreateWindowEx.
1136 * returns default show mode in sw.
1138 static void WIN_FixCoordinates( CREATESTRUCTW *cs, INT *sw)
1140 #define IS_DEFAULT(x) ((x) == CW_USEDEFAULT || (x) == (SHORT)0x8000)
1141 POINT pos[2];
1143 if (cs->dwExStyle & WS_EX_MDICHILD)
1145 UINT id = 0;
1147 MDI_CalcDefaultChildPos(cs->hwndParent, -1, pos, 0, &id);
1148 if (!(cs->style & WS_POPUP)) cs->hMenu = ULongToHandle(id);
1150 TRACE("MDI child id %04x\n", id);
1153 if (cs->style & (WS_CHILD | WS_POPUP))
1155 if (cs->dwExStyle & WS_EX_MDICHILD)
1157 if (IS_DEFAULT(cs->x))
1159 cs->x = pos[0].x;
1160 cs->y = pos[0].y;
1162 if (IS_DEFAULT(cs->cx) || !cs->cx) cs->cx = pos[1].x;
1163 if (IS_DEFAULT(cs->cy) || !cs->cy) cs->cy = pos[1].y;
1165 else
1167 if (IS_DEFAULT(cs->x)) cs->x = cs->y = 0;
1168 if (IS_DEFAULT(cs->cx)) cs->cx = cs->cy = 0;
1171 else /* overlapped window */
1173 HMONITOR monitor;
1174 MONITORINFO mon_info;
1175 STARTUPINFOW info;
1177 if (!IS_DEFAULT(cs->x) && !IS_DEFAULT(cs->cx) && !IS_DEFAULT(cs->cy)) return;
1179 monitor = MonitorFromWindow( cs->hwndParent, MONITOR_DEFAULTTOPRIMARY );
1180 mon_info.cbSize = sizeof(mon_info);
1181 GetMonitorInfoW( monitor, &mon_info );
1182 GetStartupInfoW( &info );
1184 if (IS_DEFAULT(cs->x))
1186 if (!IS_DEFAULT(cs->y)) *sw = cs->y;
1187 cs->x = (info.dwFlags & STARTF_USEPOSITION) ? info.dwX : mon_info.rcWork.left;
1188 cs->y = (info.dwFlags & STARTF_USEPOSITION) ? info.dwY : mon_info.rcWork.top;
1191 if (IS_DEFAULT(cs->cx))
1193 if (info.dwFlags & STARTF_USESIZE)
1195 cs->cx = info.dwXSize;
1196 cs->cy = info.dwYSize;
1198 else
1200 cs->cx = (mon_info.rcWork.right - mon_info.rcWork.left) * 3 / 4 - cs->x;
1201 cs->cy = (mon_info.rcWork.bottom - mon_info.rcWork.top) * 3 / 4 - cs->y;
1204 /* neither x nor cx are default. Check the y values .
1205 * In the trace we see Outlook and Outlook Express using
1206 * cy set to CW_USEDEFAULT when opening the address book.
1208 else if (IS_DEFAULT(cs->cy))
1210 FIXME("Strange use of CW_USEDEFAULT in nHeight\n");
1211 cs->cy = (mon_info.rcWork.bottom - mon_info.rcWork.top) * 3 / 4 - cs->y;
1214 #undef IS_DEFAULT
1217 /***********************************************************************
1218 * dump_window_styles
1220 static void dump_window_styles( DWORD style, DWORD exstyle )
1222 TRACE( "style:" );
1223 if(style & WS_POPUP) TRACE(" WS_POPUP");
1224 if(style & WS_CHILD) TRACE(" WS_CHILD");
1225 if(style & WS_MINIMIZE) TRACE(" WS_MINIMIZE");
1226 if(style & WS_VISIBLE) TRACE(" WS_VISIBLE");
1227 if(style & WS_DISABLED) TRACE(" WS_DISABLED");
1228 if(style & WS_CLIPSIBLINGS) TRACE(" WS_CLIPSIBLINGS");
1229 if(style & WS_CLIPCHILDREN) TRACE(" WS_CLIPCHILDREN");
1230 if(style & WS_MAXIMIZE) TRACE(" WS_MAXIMIZE");
1231 if((style & WS_CAPTION) == WS_CAPTION) TRACE(" WS_CAPTION");
1232 else
1234 if(style & WS_BORDER) TRACE(" WS_BORDER");
1235 if(style & WS_DLGFRAME) TRACE(" WS_DLGFRAME");
1237 if(style & WS_VSCROLL) TRACE(" WS_VSCROLL");
1238 if(style & WS_HSCROLL) TRACE(" WS_HSCROLL");
1239 if(style & WS_SYSMENU) TRACE(" WS_SYSMENU");
1240 if(style & WS_THICKFRAME) TRACE(" WS_THICKFRAME");
1241 if (style & WS_CHILD)
1243 if(style & WS_GROUP) TRACE(" WS_GROUP");
1244 if(style & WS_TABSTOP) TRACE(" WS_TABSTOP");
1246 else
1248 if(style & WS_MINIMIZEBOX) TRACE(" WS_MINIMIZEBOX");
1249 if(style & WS_MAXIMIZEBOX) TRACE(" WS_MAXIMIZEBOX");
1252 /* FIXME: Add dumping of BS_/ES_/SBS_/LBS_/CBS_/DS_/etc. styles */
1253 #define DUMPED_STYLES \
1254 ((DWORD)(WS_POPUP | \
1255 WS_CHILD | \
1256 WS_MINIMIZE | \
1257 WS_VISIBLE | \
1258 WS_DISABLED | \
1259 WS_CLIPSIBLINGS | \
1260 WS_CLIPCHILDREN | \
1261 WS_MAXIMIZE | \
1262 WS_BORDER | \
1263 WS_DLGFRAME | \
1264 WS_VSCROLL | \
1265 WS_HSCROLL | \
1266 WS_SYSMENU | \
1267 WS_THICKFRAME | \
1268 WS_GROUP | \
1269 WS_TABSTOP | \
1270 WS_MINIMIZEBOX | \
1271 WS_MAXIMIZEBOX))
1273 if(style & ~DUMPED_STYLES) TRACE(" %08x", style & ~DUMPED_STYLES);
1274 TRACE("\n");
1275 #undef DUMPED_STYLES
1277 TRACE( "exstyle:" );
1278 if(exstyle & WS_EX_DLGMODALFRAME) TRACE(" WS_EX_DLGMODALFRAME");
1279 if(exstyle & WS_EX_DRAGDETECT) TRACE(" WS_EX_DRAGDETECT");
1280 if(exstyle & WS_EX_NOPARENTNOTIFY) TRACE(" WS_EX_NOPARENTNOTIFY");
1281 if(exstyle & WS_EX_TOPMOST) TRACE(" WS_EX_TOPMOST");
1282 if(exstyle & WS_EX_ACCEPTFILES) TRACE(" WS_EX_ACCEPTFILES");
1283 if(exstyle & WS_EX_TRANSPARENT) TRACE(" WS_EX_TRANSPARENT");
1284 if(exstyle & WS_EX_MDICHILD) TRACE(" WS_EX_MDICHILD");
1285 if(exstyle & WS_EX_TOOLWINDOW) TRACE(" WS_EX_TOOLWINDOW");
1286 if(exstyle & WS_EX_WINDOWEDGE) TRACE(" WS_EX_WINDOWEDGE");
1287 if(exstyle & WS_EX_CLIENTEDGE) TRACE(" WS_EX_CLIENTEDGE");
1288 if(exstyle & WS_EX_CONTEXTHELP) TRACE(" WS_EX_CONTEXTHELP");
1289 if(exstyle & WS_EX_RIGHT) TRACE(" WS_EX_RIGHT");
1290 if(exstyle & WS_EX_RTLREADING) TRACE(" WS_EX_RTLREADING");
1291 if(exstyle & WS_EX_LEFTSCROLLBAR) TRACE(" WS_EX_LEFTSCROLLBAR");
1292 if(exstyle & WS_EX_CONTROLPARENT) TRACE(" WS_EX_CONTROLPARENT");
1293 if(exstyle & WS_EX_STATICEDGE) TRACE(" WS_EX_STATICEDGE");
1294 if(exstyle & WS_EX_APPWINDOW) TRACE(" WS_EX_APPWINDOW");
1295 if(exstyle & WS_EX_LAYERED) TRACE(" WS_EX_LAYERED");
1296 if(exstyle & WS_EX_LAYOUTRTL) TRACE(" WS_EX_LAYOUTRTL");
1298 #define DUMPED_EX_STYLES \
1299 ((DWORD)(WS_EX_DLGMODALFRAME | \
1300 WS_EX_DRAGDETECT | \
1301 WS_EX_NOPARENTNOTIFY | \
1302 WS_EX_TOPMOST | \
1303 WS_EX_ACCEPTFILES | \
1304 WS_EX_TRANSPARENT | \
1305 WS_EX_MDICHILD | \
1306 WS_EX_TOOLWINDOW | \
1307 WS_EX_WINDOWEDGE | \
1308 WS_EX_CLIENTEDGE | \
1309 WS_EX_CONTEXTHELP | \
1310 WS_EX_RIGHT | \
1311 WS_EX_RTLREADING | \
1312 WS_EX_LEFTSCROLLBAR | \
1313 WS_EX_CONTROLPARENT | \
1314 WS_EX_STATICEDGE | \
1315 WS_EX_APPWINDOW | \
1316 WS_EX_LAYERED | \
1317 WS_EX_LAYOUTRTL))
1319 if(exstyle & ~DUMPED_EX_STYLES) TRACE(" %08x", exstyle & ~DUMPED_EX_STYLES);
1320 TRACE("\n");
1321 #undef DUMPED_EX_STYLES
1325 /***********************************************************************
1326 * WIN_CreateWindowEx
1328 * Implementation of CreateWindowEx().
1330 HWND WIN_CreateWindowEx( CREATESTRUCTW *cs, LPCWSTR className, HINSTANCE module, BOOL unicode )
1332 INT cx, cy, style, sw = SW_SHOW;
1333 LRESULT result;
1334 RECT rect;
1335 WND *wndPtr;
1336 HWND hwnd, parent, owner, top_child = 0;
1337 MDICREATESTRUCTW mdi_cs;
1338 CBT_CREATEWNDW cbtc;
1339 CREATESTRUCTW cbcs;
1341 TRACE("%s %s ex=%08x style=%08x %d,%d %dx%d parent=%p menu=%p inst=%p params=%p\n",
1342 unicode ? debugstr_w(cs->lpszName) : debugstr_a((LPCSTR)cs->lpszName),
1343 debugstr_w(className),
1344 cs->dwExStyle, cs->style, cs->x, cs->y, cs->cx, cs->cy,
1345 cs->hwndParent, cs->hMenu, cs->hInstance, cs->lpCreateParams );
1346 if(TRACE_ON(win)) dump_window_styles( cs->style, cs->dwExStyle );
1348 /* Fix the styles for MDI children */
1349 if (cs->dwExStyle & WS_EX_MDICHILD)
1351 if (!(win_get_flags( cs->hwndParent ) & WIN_ISMDICLIENT))
1353 WARN("WS_EX_MDICHILD, but parent %p is not MDIClient\n", cs->hwndParent);
1354 return 0;
1357 /* cs->lpCreateParams of WM_[NC]CREATE is different for MDI children.
1358 * MDICREATESTRUCT members have the originally passed values.
1360 * Note: we rely on the fact that MDICREATESTRUCTA and MDICREATESTRUCTW
1361 * have the same layout.
1363 mdi_cs.szClass = cs->lpszClass;
1364 mdi_cs.szTitle = cs->lpszName;
1365 mdi_cs.hOwner = cs->hInstance;
1366 mdi_cs.x = cs->x;
1367 mdi_cs.y = cs->y;
1368 mdi_cs.cx = cs->cx;
1369 mdi_cs.cy = cs->cy;
1370 mdi_cs.style = cs->style;
1371 mdi_cs.lParam = (LPARAM)cs->lpCreateParams;
1373 cs->lpCreateParams = &mdi_cs;
1375 if (GetWindowLongW(cs->hwndParent, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1377 if (cs->style & WS_POPUP)
1379 TRACE("WS_POPUP with MDIS_ALLCHILDSTYLES is not allowed\n");
1380 return 0;
1382 cs->style |= WS_CHILD | WS_CLIPSIBLINGS;
1384 else
1386 cs->style &= ~WS_POPUP;
1387 cs->style |= WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CAPTION |
1388 WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
1391 top_child = GetWindow(cs->hwndParent, GW_CHILD);
1393 if (top_child)
1395 /* Restore current maximized child */
1396 if((cs->style & WS_VISIBLE) && IsZoomed(top_child))
1398 TRACE("Restoring current maximized child %p\n", top_child);
1399 if (cs->style & WS_MAXIMIZE)
1401 /* if the new window is maximized don't bother repainting */
1402 SendMessageW( top_child, WM_SETREDRAW, FALSE, 0 );
1403 ShowWindow( top_child, SW_SHOWNORMAL );
1404 SendMessageW( top_child, WM_SETREDRAW, TRUE, 0 );
1406 else ShowWindow( top_child, SW_SHOWNORMAL );
1411 /* Find the parent window */
1413 parent = cs->hwndParent;
1414 owner = 0;
1416 if (cs->hwndParent == HWND_MESSAGE)
1418 cs->hwndParent = parent = get_hwnd_message_parent();
1420 else if (cs->hwndParent)
1422 if ((cs->style & (WS_CHILD|WS_POPUP)) != WS_CHILD)
1424 parent = GetDesktopWindow();
1425 owner = cs->hwndParent;
1427 else
1429 DWORD parent_style = GetWindowLongW( parent, GWL_EXSTYLE );
1430 if ((parent_style & WS_EX_LAYOUTRTL) && !(parent_style & WS_EX_NOINHERITLAYOUT))
1431 cs->dwExStyle |= WS_EX_LAYOUTRTL;
1434 else
1436 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
1438 if ((cs->style & (WS_CHILD|WS_POPUP)) == WS_CHILD)
1440 WARN("No parent for child window\n" );
1441 SetLastError(ERROR_TLW_WITH_WSCHILD);
1442 return 0; /* WS_CHILD needs a parent, but WS_POPUP doesn't */
1445 /* are we creating the desktop or HWND_MESSAGE parent itself? */
1446 if (className != (LPCWSTR)DESKTOP_CLASS_ATOM &&
1447 (IS_INTRESOURCE(className) || strcmpiW( className, messageW )))
1449 DWORD layout;
1450 GetProcessDefaultLayout( &layout );
1451 if (layout & LAYOUT_RTL) cs->dwExStyle |= WS_EX_LAYOUTRTL;
1452 parent = GetDesktopWindow();
1456 WIN_FixCoordinates(cs, &sw); /* fix default coordinates */
1458 if ((cs->dwExStyle & WS_EX_DLGMODALFRAME) ||
1459 ((!(cs->dwExStyle & WS_EX_STATICEDGE)) &&
1460 (cs->style & (WS_DLGFRAME | WS_THICKFRAME))))
1461 cs->dwExStyle |= WS_EX_WINDOWEDGE;
1462 else
1463 cs->dwExStyle &= ~WS_EX_WINDOWEDGE;
1465 /* Create the window structure */
1467 if (!(wndPtr = create_window_handle( parent, owner, className, module, unicode )))
1469 WNDCLASSW wc;
1470 /* if it's a comctl32 class, GetClassInfo will load it, then we can retry */
1471 if (GetLastError() != ERROR_INVALID_HANDLE ||
1472 !GetClassInfoW( 0, className, &wc ) ||
1473 !(wndPtr = create_window_handle( parent, owner, className, module, unicode )))
1474 return 0;
1476 hwnd = wndPtr->obj.handle;
1478 /* Fill the window structure */
1480 wndPtr->tid = GetCurrentThreadId();
1481 wndPtr->hInstance = cs->hInstance;
1482 wndPtr->text = NULL;
1483 wndPtr->dwStyle = cs->style & ~WS_VISIBLE;
1484 wndPtr->dwExStyle = cs->dwExStyle;
1485 wndPtr->wIDmenu = 0;
1486 wndPtr->helpContext = 0;
1487 wndPtr->pScroll = NULL;
1488 wndPtr->userdata = 0;
1489 wndPtr->hIcon = 0;
1490 wndPtr->hIconSmall = 0;
1491 wndPtr->hIconSmall2 = 0;
1492 wndPtr->hSysMenu = 0;
1494 wndPtr->min_pos.x = wndPtr->min_pos.y = -1;
1495 wndPtr->max_pos.x = wndPtr->max_pos.y = -1;
1496 SetRect( &wndPtr->normal_rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
1498 if (wndPtr->dwStyle & WS_SYSMENU) SetSystemMenu( hwnd, 0 );
1501 * Correct the window styles.
1503 * It affects only the style loaded into the WIN structure.
1506 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
1508 wndPtr->dwStyle |= WS_CLIPSIBLINGS;
1509 if (!(wndPtr->dwStyle & WS_POPUP))
1510 wndPtr->dwStyle |= WS_CAPTION;
1513 /* WS_EX_WINDOWEDGE depends on some other styles */
1514 if (wndPtr->dwExStyle & WS_EX_DLGMODALFRAME)
1515 wndPtr->dwExStyle |= WS_EX_WINDOWEDGE;
1516 else if (wndPtr->dwStyle & (WS_DLGFRAME | WS_THICKFRAME))
1518 if (!((wndPtr->dwExStyle & WS_EX_STATICEDGE) &&
1519 (wndPtr->dwStyle & (WS_CHILD | WS_POPUP))))
1520 wndPtr->dwExStyle |= WS_EX_WINDOWEDGE;
1522 else
1523 wndPtr->dwExStyle &= ~WS_EX_WINDOWEDGE;
1525 if (!(wndPtr->dwStyle & (WS_CHILD | WS_POPUP)))
1526 wndPtr->flags |= WIN_NEED_SIZE;
1528 SERVER_START_REQ( set_window_info )
1530 req->handle = wine_server_user_handle( hwnd );
1531 req->flags = SET_WIN_STYLE | SET_WIN_EXSTYLE | SET_WIN_INSTANCE | SET_WIN_UNICODE;
1532 req->style = wndPtr->dwStyle;
1533 req->ex_style = wndPtr->dwExStyle;
1534 req->instance = wine_server_client_ptr( wndPtr->hInstance );
1535 req->is_unicode = (wndPtr->flags & WIN_ISUNICODE) != 0;
1536 req->extra_offset = -1;
1537 wine_server_call( req );
1539 SERVER_END_REQ;
1541 /* Set the window menu */
1543 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
1545 if (cs->hMenu)
1547 if (!MENU_SetMenu(hwnd, cs->hMenu))
1549 WIN_ReleasePtr( wndPtr );
1550 free_window_handle( hwnd );
1551 return 0;
1554 else
1556 LPCWSTR menuName = (LPCWSTR)GetClassLongPtrW( hwnd, GCLP_MENUNAME );
1557 if (menuName)
1559 cs->hMenu = LoadMenuW( cs->hInstance, menuName );
1560 if (cs->hMenu) MENU_SetMenu( hwnd, cs->hMenu );
1564 else SetWindowLongPtrW( hwnd, GWLP_ID, (ULONG_PTR)cs->hMenu );
1566 /* call the WH_CBT hook */
1568 /* the window style passed to the hook must be the real window style,
1569 * rather than just the window style that the caller to CreateWindowEx
1570 * passed in, so we have to copy the original CREATESTRUCT and get the
1571 * the real style. */
1572 cbcs = *cs;
1573 cbcs.style = wndPtr->dwStyle;
1574 cbtc.lpcs = &cbcs;
1575 cbtc.hwndInsertAfter = HWND_TOP;
1576 WIN_ReleasePtr( wndPtr );
1577 if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode )) goto failed;
1579 /* send the WM_GETMINMAXINFO message and fix the size if needed */
1581 cx = cs->cx;
1582 cy = cs->cy;
1583 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
1585 POINT maxSize, maxPos, minTrack, maxTrack;
1586 WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
1587 if (maxTrack.x < cx) cx = maxTrack.x;
1588 if (maxTrack.y < cy) cy = maxTrack.y;
1589 if (minTrack.x > cx) cx = minTrack.x;
1590 if (minTrack.y > cy) cy = minTrack.y;
1593 if (cx < 0) cx = 0;
1594 if (cy < 0) cy = 0;
1595 SetRect( &rect, cs->x, cs->y, cs->x + cx, cs->y + cy );
1596 /* check for wraparound */
1597 if (cs->x + cx < cs->x) rect.right = 0x7fffffff;
1598 if (cs->y + cy < cs->y) rect.bottom = 0x7fffffff;
1599 if (!set_window_pos( hwnd, 0, SWP_NOZORDER | SWP_NOACTIVATE, &rect, &rect, NULL )) goto failed;
1601 /* send WM_NCCREATE */
1603 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cx, cy );
1604 if (unicode)
1605 result = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1606 else
1607 result = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1608 if (!result)
1610 WARN( "%p: aborted by WM_NCCREATE\n", hwnd );
1611 goto failed;
1614 /* create default IME window */
1616 if (imm_register_window && !is_desktop_window( hwnd ) &&
1617 parent != get_hwnd_message_parent() && imm_register_window( hwnd ))
1619 TRACE("register IME window for %p\n", hwnd);
1620 win_set_flags( hwnd, WIN_HAS_IME_WIN, 0 );
1623 /* send WM_NCCALCSIZE */
1625 if (WIN_GetRectangles( hwnd, COORDS_PARENT, &rect, NULL ))
1627 /* yes, even if the CBT hook was called with HWND_TOP */
1628 HWND insert_after = (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
1629 RECT client_rect = rect;
1631 /* the rectangle is in screen coords for WM_NCCALCSIZE when wparam is FALSE */
1632 MapWindowPoints( parent, 0, (POINT *)&client_rect, 2 );
1633 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&client_rect );
1634 MapWindowPoints( 0, parent, (POINT *)&client_rect, 2 );
1635 set_window_pos( hwnd, insert_after, SWP_NOACTIVATE, &rect, &client_rect, NULL );
1637 else return 0;
1639 /* send WM_CREATE */
1641 if (unicode)
1642 result = SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs );
1643 else
1644 result = SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs );
1645 if (result == -1) goto failed;
1647 /* call the driver */
1649 if (!USER_Driver->pCreateWindow( hwnd )) goto failed;
1651 NotifyWinEvent(EVENT_OBJECT_CREATE, hwnd, OBJID_WINDOW, 0);
1653 /* send the size messages */
1655 if (!(win_get_flags( hwnd ) & WIN_NEED_SIZE))
1657 WIN_GetRectangles( hwnd, COORDS_PARENT, NULL, &rect );
1658 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1659 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1660 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( rect.left, rect.top ) );
1663 /* Show the window, maximizing or minimizing if needed */
1665 style = WIN_SetStyle( hwnd, 0, WS_MAXIMIZE | WS_MINIMIZE );
1666 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1668 RECT newPos;
1669 UINT swFlag = (style & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1671 swFlag = WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1672 swFlag |= SWP_FRAMECHANGED; /* Frame always gets changed */
1673 if (!(style & WS_VISIBLE) || (style & WS_CHILD) || GetActiveWindow()) swFlag |= SWP_NOACTIVATE;
1674 SetWindowPos( hwnd, 0, newPos.left, newPos.top, newPos.right - newPos.left,
1675 newPos.bottom - newPos.top, swFlag );
1678 /* Notify the parent window only */
1680 send_parent_notify( hwnd, WM_CREATE );
1681 if (!IsWindow( hwnd )) return 0;
1683 if (parent == GetDesktopWindow())
1684 PostMessageW( parent, WM_PARENTNOTIFY, WM_CREATE, (LPARAM)hwnd );
1686 if (cs->style & WS_VISIBLE)
1688 if (cs->style & WS_MAXIMIZE)
1689 sw = SW_SHOW;
1690 else if (cs->style & WS_MINIMIZE)
1691 sw = SW_SHOWMINIMIZED;
1693 ShowWindow( hwnd, sw );
1694 if (cs->dwExStyle & WS_EX_MDICHILD)
1696 SendMessageW(cs->hwndParent, WM_MDIREFRESHMENU, 0, 0);
1697 /* ShowWindow won't activate child windows */
1698 SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE );
1702 /* Call WH_SHELL hook */
1704 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) && !GetWindow( hwnd, GW_OWNER ))
1705 HOOK_CallHooks( WH_SHELL, HSHELL_WINDOWCREATED, (WPARAM)hwnd, 0, TRUE );
1707 TRACE("created window %p\n", hwnd);
1708 return hwnd;
1710 failed:
1711 WIN_DestroyWindow( hwnd );
1712 return 0;
1716 /***********************************************************************
1717 * CreateWindowExA (USER32.@)
1719 HWND WINAPI DECLSPEC_HOTPATCH CreateWindowExA( DWORD exStyle, LPCSTR className,
1720 LPCSTR windowName, DWORD style, INT x,
1721 INT y, INT width, INT height,
1722 HWND parent, HMENU menu,
1723 HINSTANCE instance, LPVOID data )
1725 CREATESTRUCTA cs;
1727 cs.lpCreateParams = data;
1728 cs.hInstance = instance;
1729 cs.hMenu = menu;
1730 cs.hwndParent = parent;
1731 cs.x = x;
1732 cs.y = y;
1733 cs.cx = width;
1734 cs.cy = height;
1735 cs.style = style;
1736 cs.lpszName = windowName;
1737 cs.lpszClass = className;
1738 cs.dwExStyle = exStyle;
1740 if (!IS_INTRESOURCE(className))
1742 WCHAR bufferW[256];
1743 if (!MultiByteToWideChar( CP_ACP, 0, className, -1, bufferW, sizeof(bufferW)/sizeof(WCHAR) ))
1744 return 0;
1745 return wow_handlers.create_window( (CREATESTRUCTW *)&cs, bufferW, instance, FALSE );
1747 /* Note: we rely on the fact that CREATESTRUCTA and */
1748 /* CREATESTRUCTW have the same layout. */
1749 return wow_handlers.create_window( (CREATESTRUCTW *)&cs, (LPCWSTR)className, instance, FALSE );
1753 /***********************************************************************
1754 * CreateWindowExW (USER32.@)
1756 HWND WINAPI DECLSPEC_HOTPATCH CreateWindowExW( DWORD exStyle, LPCWSTR className,
1757 LPCWSTR windowName, DWORD style, INT x,
1758 INT y, INT width, INT height,
1759 HWND parent, HMENU menu,
1760 HINSTANCE instance, LPVOID data )
1762 CREATESTRUCTW cs;
1764 cs.lpCreateParams = data;
1765 cs.hInstance = instance;
1766 cs.hMenu = menu;
1767 cs.hwndParent = parent;
1768 cs.x = x;
1769 cs.y = y;
1770 cs.cx = width;
1771 cs.cy = height;
1772 cs.style = style;
1773 cs.lpszName = windowName;
1774 cs.lpszClass = className;
1775 cs.dwExStyle = exStyle;
1777 return wow_handlers.create_window( &cs, className, instance, TRUE );
1781 /***********************************************************************
1782 * WIN_SendDestroyMsg
1784 static void WIN_SendDestroyMsg( HWND hwnd )
1786 GUITHREADINFO info;
1788 info.cbSize = sizeof(info);
1789 if (GetGUIThreadInfo( GetCurrentThreadId(), &info ))
1791 if (hwnd == info.hwndCaret) DestroyCaret();
1792 if (hwnd == info.hwndActive) WINPOS_ActivateOtherWindow( hwnd );
1795 if (hwnd == GetClipboardOwner()) CLIPBOARD_ReleaseOwner( hwnd );
1798 * Send the WM_DESTROY to the window.
1800 SendMessageW( hwnd, WM_DESTROY, 0, 0);
1803 * This WM_DESTROY message can trigger re-entrant calls to DestroyWindow
1804 * make sure that the window still exists when we come back.
1806 if (IsWindow(hwnd))
1808 HWND* pWndArray;
1809 int i;
1811 if (!(pWndArray = WIN_ListChildren( hwnd ))) return;
1813 for (i = 0; pWndArray[i]; i++)
1815 if (IsWindow( pWndArray[i] )) WIN_SendDestroyMsg( pWndArray[i] );
1817 HeapFree( GetProcessHeap(), 0, pWndArray );
1819 else
1820 WARN("\tdestroyed itself while in WM_DESTROY!\n");
1824 /***********************************************************************
1825 * DestroyWindow (USER32.@)
1827 BOOL WINAPI DestroyWindow( HWND hwnd )
1829 BOOL is_child;
1831 if (!(hwnd = WIN_IsCurrentThread( hwnd )) || is_desktop_window( hwnd ))
1833 SetLastError( ERROR_ACCESS_DENIED );
1834 return FALSE;
1837 TRACE("(%p)\n", hwnd);
1839 /* Call hooks */
1841 if (HOOK_CallHooks( WH_CBT, HCBT_DESTROYWND, (WPARAM)hwnd, 0, TRUE )) return FALSE;
1843 if (MENU_IsMenuActive() == hwnd)
1844 EndMenu();
1846 is_child = (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) != 0;
1848 if (is_child)
1850 if (!USER_IsExitingThread( GetCurrentThreadId() ))
1851 send_parent_notify( hwnd, WM_DESTROY );
1853 else if (!GetWindow( hwnd, GW_OWNER ))
1855 HOOK_CallHooks( WH_SHELL, HSHELL_WINDOWDESTROYED, (WPARAM)hwnd, 0L, TRUE );
1856 /* FIXME: clean up palette - see "Internals" p.352 */
1859 if (!IsWindow(hwnd)) return TRUE;
1861 /* Hide the window */
1862 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)
1864 /* Only child windows receive WM_SHOWWINDOW in DestroyWindow() */
1865 if (is_child)
1866 ShowWindow( hwnd, SW_HIDE );
1867 else
1868 SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE |
1869 SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW );
1872 if (!IsWindow(hwnd)) return TRUE;
1874 /* Recursively destroy owned windows */
1876 if (!is_child)
1878 for (;;)
1880 int i;
1881 BOOL got_one = FALSE;
1882 HWND *list = WIN_ListChildren( GetDesktopWindow() );
1883 if (list)
1885 for (i = 0; list[i]; i++)
1887 if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
1888 if (WIN_IsCurrentThread( list[i] ))
1890 DestroyWindow( list[i] );
1891 got_one = TRUE;
1892 continue;
1894 WIN_SetOwner( list[i], 0 );
1896 HeapFree( GetProcessHeap(), 0, list );
1898 if (!got_one) break;
1902 /* Send destroy messages */
1904 WIN_SendDestroyMsg( hwnd );
1905 if (!IsWindow( hwnd )) return TRUE;
1907 /* Destroy the window storage */
1909 WIN_DestroyWindow( hwnd );
1910 return TRUE;
1914 /***********************************************************************
1915 * CloseWindow (USER32.@)
1917 BOOL WINAPI CloseWindow( HWND hwnd )
1919 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) return FALSE;
1920 ShowWindow( hwnd, SW_MINIMIZE );
1921 return TRUE;
1925 /***********************************************************************
1926 * OpenIcon (USER32.@)
1928 BOOL WINAPI OpenIcon( HWND hwnd )
1930 if (!IsIconic( hwnd )) return FALSE;
1931 ShowWindow( hwnd, SW_SHOWNORMAL );
1932 return TRUE;
1936 /***********************************************************************
1937 * FindWindowExW (USER32.@)
1939 HWND WINAPI FindWindowExW( HWND parent, HWND child, LPCWSTR className, LPCWSTR title )
1941 HWND *list;
1942 HWND retvalue = 0;
1943 int i = 0, len = 0;
1944 WCHAR *buffer = NULL;
1946 if (!parent && child) parent = GetDesktopWindow();
1947 else if (parent == HWND_MESSAGE) parent = get_hwnd_message_parent();
1949 if (title)
1951 len = strlenW(title) + 1; /* one extra char to check for chars beyond the end */
1952 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return 0;
1955 if (!(list = list_window_children( 0, parent, className, 0 ))) goto done;
1957 if (child)
1959 child = WIN_GetFullHandle( child );
1960 while (list[i] && list[i] != child) i++;
1961 if (!list[i]) goto done;
1962 i++; /* start from next window */
1965 if (title)
1967 while (list[i])
1969 if (InternalGetWindowText( list[i], buffer, len + 1 ))
1971 if (!strcmpiW( buffer, title )) break;
1973 else
1975 if (!title[0]) break;
1977 i++;
1980 retvalue = list[i];
1982 done:
1983 HeapFree( GetProcessHeap(), 0, list );
1984 HeapFree( GetProcessHeap(), 0, buffer );
1985 return retvalue;
1990 /***********************************************************************
1991 * FindWindowA (USER32.@)
1993 HWND WINAPI FindWindowA( LPCSTR className, LPCSTR title )
1995 HWND ret = FindWindowExA( 0, 0, className, title );
1996 if (!ret) SetLastError (ERROR_CANNOT_FIND_WND_CLASS);
1997 return ret;
2001 /***********************************************************************
2002 * FindWindowExA (USER32.@)
2004 HWND WINAPI FindWindowExA( HWND parent, HWND child, LPCSTR className, LPCSTR title )
2006 LPWSTR titleW = NULL;
2007 HWND hwnd = 0;
2009 if (title)
2011 DWORD len = MultiByteToWideChar( CP_ACP, 0, title, -1, NULL, 0 );
2012 if (!(titleW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return 0;
2013 MultiByteToWideChar( CP_ACP, 0, title, -1, titleW, len );
2016 if (!IS_INTRESOURCE(className))
2018 WCHAR classW[256];
2019 if (MultiByteToWideChar( CP_ACP, 0, className, -1, classW, sizeof(classW)/sizeof(WCHAR) ))
2020 hwnd = FindWindowExW( parent, child, classW, titleW );
2022 else
2024 hwnd = FindWindowExW( parent, child, (LPCWSTR)className, titleW );
2027 HeapFree( GetProcessHeap(), 0, titleW );
2028 return hwnd;
2032 /***********************************************************************
2033 * FindWindowW (USER32.@)
2035 HWND WINAPI FindWindowW( LPCWSTR className, LPCWSTR title )
2037 return FindWindowExW( 0, 0, className, title );
2041 /**********************************************************************
2042 * GetDesktopWindow (USER32.@)
2044 HWND WINAPI GetDesktopWindow(void)
2046 struct user_thread_info *thread_info = get_user_thread_info();
2048 if (thread_info->top_window) return thread_info->top_window;
2050 SERVER_START_REQ( get_desktop_window )
2052 req->force = 0;
2053 if (!wine_server_call( req ))
2055 thread_info->top_window = wine_server_ptr_handle( reply->top_window );
2056 thread_info->msg_window = wine_server_ptr_handle( reply->msg_window );
2059 SERVER_END_REQ;
2061 if (!thread_info->top_window)
2063 static const WCHAR explorer[] = {'\\','e','x','p','l','o','r','e','r','.','e','x','e',0};
2064 static const WCHAR args[] = {' ','/','d','e','s','k','t','o','p',0};
2065 STARTUPINFOW si;
2066 PROCESS_INFORMATION pi;
2067 WCHAR windir[MAX_PATH];
2068 WCHAR app[MAX_PATH + sizeof(explorer)/sizeof(WCHAR)];
2069 WCHAR cmdline[MAX_PATH + (sizeof(explorer) + sizeof(args))/sizeof(WCHAR)];
2070 WCHAR desktop[MAX_PATH];
2071 void *redir;
2073 SERVER_START_REQ( set_user_object_info )
2075 req->handle = wine_server_obj_handle( GetThreadDesktop(GetCurrentThreadId()) );
2076 req->flags = SET_USER_OBJECT_GET_FULL_NAME;
2077 wine_server_set_reply( req, desktop, sizeof(desktop) - sizeof(WCHAR) );
2078 if (!wine_server_call( req ))
2080 size_t size = wine_server_reply_size( reply );
2081 desktop[size / sizeof(WCHAR)] = 0;
2082 TRACE( "starting explorer for desktop %s\n", debugstr_w(desktop) );
2084 else
2085 desktop[0] = 0;
2087 SERVER_END_REQ;
2089 memset( &si, 0, sizeof(si) );
2090 si.cb = sizeof(si);
2091 si.lpDesktop = *desktop ? desktop : NULL;
2092 si.dwFlags = STARTF_USESTDHANDLES;
2093 si.hStdInput = 0;
2094 si.hStdOutput = 0;
2095 si.hStdError = GetStdHandle( STD_ERROR_HANDLE );
2097 GetSystemDirectoryW( windir, MAX_PATH );
2098 strcpyW( app, windir );
2099 strcatW( app, explorer );
2100 strcpyW( cmdline, app );
2101 strcatW( cmdline, args );
2103 Wow64DisableWow64FsRedirection( &redir );
2104 if (CreateProcessW( app, cmdline, NULL, NULL, FALSE, DETACHED_PROCESS,
2105 NULL, windir, &si, &pi ))
2107 TRACE( "started explorer pid %04x tid %04x\n", pi.dwProcessId, pi.dwThreadId );
2108 WaitForInputIdle( pi.hProcess, 10000 );
2109 CloseHandle( pi.hThread );
2110 CloseHandle( pi.hProcess );
2112 else WARN( "failed to start explorer, err %d\n", GetLastError() );
2113 Wow64RevertWow64FsRedirection( redir );
2115 SERVER_START_REQ( get_desktop_window )
2117 req->force = 1;
2118 if (!wine_server_call( req ))
2120 thread_info->top_window = wine_server_ptr_handle( reply->top_window );
2121 thread_info->msg_window = wine_server_ptr_handle( reply->msg_window );
2124 SERVER_END_REQ;
2127 if (!thread_info->top_window || !USER_Driver->pCreateDesktopWindow( thread_info->top_window ))
2128 ERR( "failed to create desktop window\n" );
2130 return thread_info->top_window;
2134 /*******************************************************************
2135 * EnableWindow (USER32.@)
2137 BOOL WINAPI EnableWindow( HWND hwnd, BOOL enable )
2139 BOOL retvalue;
2140 HWND full_handle;
2142 if (is_broadcast(hwnd))
2144 SetLastError( ERROR_INVALID_PARAMETER );
2145 return FALSE;
2148 if (!(full_handle = WIN_IsCurrentThread( hwnd )))
2149 return SendMessageW( hwnd, WM_WINE_ENABLEWINDOW, enable, 0 );
2151 hwnd = full_handle;
2153 TRACE("( %p, %d )\n", hwnd, enable);
2155 retvalue = !IsWindowEnabled( hwnd );
2157 if (enable && retvalue)
2159 WIN_SetStyle( hwnd, 0, WS_DISABLED );
2160 SendMessageW( hwnd, WM_ENABLE, TRUE, 0 );
2162 else if (!enable && !retvalue)
2164 HWND capture_wnd;
2166 SendMessageW( hwnd, WM_CANCELMODE, 0, 0);
2168 WIN_SetStyle( hwnd, WS_DISABLED, 0 );
2170 if (hwnd == GetFocus())
2171 SetFocus( 0 ); /* A disabled window can't have the focus */
2173 capture_wnd = GetCapture();
2174 if (capture_wnd && (hwnd == capture_wnd || IsChild(hwnd, capture_wnd)))
2175 ReleaseCapture(); /* A disabled window can't capture the mouse */
2177 SendMessageW( hwnd, WM_ENABLE, FALSE, 0 );
2179 return retvalue;
2183 /***********************************************************************
2184 * IsWindowEnabled (USER32.@)
2186 BOOL WINAPI IsWindowEnabled(HWND hWnd)
2188 return !(GetWindowLongW( hWnd, GWL_STYLE ) & WS_DISABLED);
2192 /***********************************************************************
2193 * IsWindowUnicode (USER32.@)
2195 BOOL WINAPI IsWindowUnicode( HWND hwnd )
2197 WND * wndPtr;
2198 BOOL retvalue = FALSE;
2200 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
2202 if (wndPtr == WND_DESKTOP) return TRUE;
2204 if (wndPtr != WND_OTHER_PROCESS)
2206 retvalue = (wndPtr->flags & WIN_ISUNICODE) != 0;
2207 WIN_ReleasePtr( wndPtr );
2209 else
2211 SERVER_START_REQ( get_window_info )
2213 req->handle = wine_server_user_handle( hwnd );
2214 if (!wine_server_call_err( req )) retvalue = reply->is_unicode;
2216 SERVER_END_REQ;
2218 return retvalue;
2222 /**********************************************************************
2223 * WIN_GetWindowLong
2225 * Helper function for GetWindowLong().
2227 static LONG_PTR WIN_GetWindowLong( HWND hwnd, INT offset, UINT size, BOOL unicode )
2229 LONG_PTR retvalue = 0;
2230 WND *wndPtr;
2232 if (offset == GWLP_HWNDPARENT)
2234 HWND parent = GetAncestor( hwnd, GA_PARENT );
2235 if (parent == GetDesktopWindow()) parent = GetWindow( hwnd, GW_OWNER );
2236 return (ULONG_PTR)parent;
2239 if (!(wndPtr = WIN_GetPtr( hwnd )))
2241 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2242 return 0;
2245 if (wndPtr == WND_DESKTOP)
2247 switch (offset)
2249 case GWL_STYLE:
2250 retvalue = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN; /* message parent is not visible */
2251 if (WIN_GetFullHandle( hwnd ) == GetDesktopWindow())
2252 retvalue |= WS_VISIBLE;
2253 return retvalue;
2254 case GWL_EXSTYLE:
2255 case GWLP_USERDATA:
2256 case GWLP_ID:
2257 case GWLP_HINSTANCE:
2258 return 0;
2259 case GWLP_WNDPROC:
2260 SetLastError( ERROR_ACCESS_DENIED );
2261 return 0;
2263 SetLastError( ERROR_INVALID_INDEX );
2264 return 0;
2267 if (wndPtr == WND_OTHER_PROCESS)
2269 if (offset == GWLP_WNDPROC)
2271 SetLastError( ERROR_ACCESS_DENIED );
2272 return 0;
2274 SERVER_START_REQ( set_window_info )
2276 req->handle = wine_server_user_handle( hwnd );
2277 req->flags = 0; /* don't set anything, just retrieve */
2278 req->extra_offset = (offset >= 0) ? offset : -1;
2279 req->extra_size = (offset >= 0) ? size : 0;
2280 if (!wine_server_call_err( req ))
2282 switch(offset)
2284 case GWL_STYLE: retvalue = reply->old_style; break;
2285 case GWL_EXSTYLE: retvalue = reply->old_ex_style; break;
2286 case GWLP_ID: retvalue = reply->old_id; break;
2287 case GWLP_HINSTANCE: retvalue = (ULONG_PTR)wine_server_get_ptr( reply->old_instance ); break;
2288 case GWLP_USERDATA: retvalue = reply->old_user_data; break;
2289 default:
2290 if (offset >= 0) retvalue = get_win_data( &reply->old_extra_value, size );
2291 else SetLastError( ERROR_INVALID_INDEX );
2292 break;
2296 SERVER_END_REQ;
2297 return retvalue;
2300 /* now we have a valid wndPtr */
2302 if (offset >= 0)
2304 if (offset > (int)(wndPtr->cbWndExtra - size))
2306 WARN("Invalid offset %d\n", offset );
2307 WIN_ReleasePtr( wndPtr );
2308 SetLastError( ERROR_INVALID_INDEX );
2309 return 0;
2311 retvalue = get_win_data( (char *)wndPtr->wExtra + offset, size );
2313 /* Special case for dialog window procedure */
2314 if ((offset == DWLP_DLGPROC) && (size == sizeof(LONG_PTR)) && wndPtr->dlgInfo)
2315 retvalue = (LONG_PTR)WINPROC_GetProc( (WNDPROC)retvalue, unicode );
2316 WIN_ReleasePtr( wndPtr );
2317 return retvalue;
2320 switch(offset)
2322 case GWLP_USERDATA: retvalue = wndPtr->userdata; break;
2323 case GWL_STYLE: retvalue = wndPtr->dwStyle; break;
2324 case GWL_EXSTYLE: retvalue = wndPtr->dwExStyle; break;
2325 case GWLP_ID: retvalue = wndPtr->wIDmenu; break;
2326 case GWLP_HINSTANCE: retvalue = (ULONG_PTR)wndPtr->hInstance; break;
2327 case GWLP_WNDPROC:
2328 /* This looks like a hack only for the edit control (see tests). This makes these controls
2329 * more tolerant to A/W mismatches. The lack of W->A->W conversion for such a mismatch suggests
2330 * that the hack is in GetWindowLongPtr[AW], not in winprocs.
2332 if (wndPtr->winproc == BUILTIN_WINPROC(WINPROC_EDIT) && (!unicode != !(wndPtr->flags & WIN_ISUNICODE)))
2333 retvalue = (ULONG_PTR)wndPtr->winproc;
2334 else
2335 retvalue = (ULONG_PTR)WINPROC_GetProc( wndPtr->winproc, unicode );
2336 break;
2337 default:
2338 WARN("Unknown offset %d\n", offset );
2339 SetLastError( ERROR_INVALID_INDEX );
2340 break;
2342 WIN_ReleasePtr(wndPtr);
2343 return retvalue;
2347 /**********************************************************************
2348 * WIN_SetWindowLong
2350 * Helper function for SetWindowLong().
2352 * 0 is the failure code. However, in the case of failure SetLastError
2353 * must be set to distinguish between a 0 return value and a failure.
2355 LONG_PTR WIN_SetWindowLong( HWND hwnd, INT offset, UINT size, LONG_PTR newval, BOOL unicode )
2357 STYLESTRUCT style;
2358 BOOL ok, made_visible = FALSE;
2359 LONG_PTR retval = 0;
2360 WND *wndPtr;
2362 TRACE( "%p %d %lx %c\n", hwnd, offset, newval, unicode ? 'W' : 'A' );
2364 if (is_broadcast(hwnd))
2366 SetLastError( ERROR_INVALID_PARAMETER );
2367 return FALSE;
2370 if (!(wndPtr = WIN_GetPtr( hwnd )))
2372 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2373 return 0;
2375 if (wndPtr == WND_DESKTOP)
2377 /* can't change anything on the desktop window */
2378 SetLastError( ERROR_ACCESS_DENIED );
2379 return 0;
2381 if (wndPtr == WND_OTHER_PROCESS)
2383 if (offset == GWLP_WNDPROC)
2385 SetLastError( ERROR_ACCESS_DENIED );
2386 return 0;
2388 if (offset > 32767 || offset < -32767)
2390 SetLastError( ERROR_INVALID_INDEX );
2391 return 0;
2393 return SendMessageW( hwnd, WM_WINE_SETWINDOWLONG, MAKEWPARAM( offset, size ), newval );
2396 /* first some special cases */
2397 switch( offset )
2399 case GWL_STYLE:
2400 style.styleOld = wndPtr->dwStyle;
2401 style.styleNew = newval;
2402 WIN_ReleasePtr( wndPtr );
2403 SendMessageW( hwnd, WM_STYLECHANGING, GWL_STYLE, (LPARAM)&style );
2404 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
2405 newval = style.styleNew;
2406 /* WS_CLIPSIBLINGS can't be reset on top-level windows */
2407 if (wndPtr->parent == GetDesktopWindow()) newval |= WS_CLIPSIBLINGS;
2408 /* WS_MINIMIZE can't be reset */
2409 if (wndPtr->dwStyle & WS_MINIMIZE) newval |= WS_MINIMIZE;
2410 /* FIXME: changing WS_DLGFRAME | WS_THICKFRAME is supposed to change
2411 WS_EX_WINDOWEDGE too */
2412 break;
2413 case GWL_EXSTYLE:
2414 style.styleOld = wndPtr->dwExStyle;
2415 style.styleNew = newval;
2416 WIN_ReleasePtr( wndPtr );
2417 SendMessageW( hwnd, WM_STYLECHANGING, GWL_EXSTYLE, (LPARAM)&style );
2418 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
2419 /* WS_EX_TOPMOST can only be changed through SetWindowPos */
2420 newval = (style.styleNew & ~WS_EX_TOPMOST) | (wndPtr->dwExStyle & WS_EX_TOPMOST);
2421 /* WS_EX_WINDOWEDGE depends on some other styles */
2422 if (newval & WS_EX_DLGMODALFRAME)
2423 newval |= WS_EX_WINDOWEDGE;
2424 else if (!(newval & WS_EX_STATICEDGE) && (wndPtr->dwStyle & (WS_DLGFRAME | WS_THICKFRAME)))
2425 newval |= WS_EX_WINDOWEDGE;
2426 else
2427 newval &= ~WS_EX_WINDOWEDGE;
2428 break;
2429 case GWLP_HWNDPARENT:
2430 if (wndPtr->parent == GetDesktopWindow())
2432 WIN_ReleasePtr( wndPtr );
2433 return (ULONG_PTR)WIN_SetOwner( hwnd, (HWND)newval );
2435 else
2437 WIN_ReleasePtr( wndPtr );
2438 return (ULONG_PTR)SetParent( hwnd, (HWND)newval );
2440 case GWLP_WNDPROC:
2442 WNDPROC proc;
2443 UINT old_flags = wndPtr->flags;
2444 retval = WIN_GetWindowLong( hwnd, offset, size, unicode );
2445 proc = WINPROC_AllocProc( (WNDPROC)newval, unicode );
2446 if (proc) wndPtr->winproc = proc;
2447 if (WINPROC_IsUnicode( proc, unicode )) wndPtr->flags |= WIN_ISUNICODE;
2448 else wndPtr->flags &= ~WIN_ISUNICODE;
2449 if (!((old_flags ^ wndPtr->flags) & WIN_ISUNICODE))
2451 WIN_ReleasePtr( wndPtr );
2452 return retval;
2454 /* update is_unicode flag on the server side */
2455 break;
2457 case GWLP_ID:
2458 case GWLP_HINSTANCE:
2459 case GWLP_USERDATA:
2460 break;
2461 case DWLP_DLGPROC:
2462 if ((wndPtr->cbWndExtra - sizeof(LONG_PTR) >= DWLP_DLGPROC) &&
2463 (size == sizeof(LONG_PTR)) && wndPtr->dlgInfo)
2465 WNDPROC *ptr = (WNDPROC *)((char *)wndPtr->wExtra + DWLP_DLGPROC);
2466 retval = (ULONG_PTR)WINPROC_GetProc( *ptr, unicode );
2467 *ptr = WINPROC_AllocProc( (WNDPROC)newval, unicode );
2468 WIN_ReleasePtr( wndPtr );
2469 return retval;
2471 /* fall through */
2472 default:
2473 if (offset < 0 || offset > (int)(wndPtr->cbWndExtra - size))
2475 WARN("Invalid offset %d\n", offset );
2476 WIN_ReleasePtr( wndPtr );
2477 SetLastError( ERROR_INVALID_INDEX );
2478 return 0;
2480 else if (get_win_data( (char *)wndPtr->wExtra + offset, size ) == newval)
2482 /* already set to the same value */
2483 WIN_ReleasePtr( wndPtr );
2484 return newval;
2486 break;
2489 SERVER_START_REQ( set_window_info )
2491 req->handle = wine_server_user_handle( hwnd );
2492 req->extra_offset = -1;
2493 switch(offset)
2495 case GWL_STYLE:
2496 req->flags = SET_WIN_STYLE;
2497 req->style = newval;
2498 break;
2499 case GWL_EXSTYLE:
2500 req->flags = SET_WIN_EXSTYLE;
2501 req->ex_style = newval;
2502 break;
2503 case GWLP_ID:
2504 req->flags = SET_WIN_ID;
2505 req->id = newval;
2506 break;
2507 case GWLP_HINSTANCE:
2508 req->flags = SET_WIN_INSTANCE;
2509 req->instance = wine_server_client_ptr( (void *)newval );
2510 break;
2511 case GWLP_WNDPROC:
2512 req->flags = SET_WIN_UNICODE;
2513 req->is_unicode = (wndPtr->flags & WIN_ISUNICODE) != 0;
2514 break;
2515 case GWLP_USERDATA:
2516 req->flags = SET_WIN_USERDATA;
2517 req->user_data = newval;
2518 break;
2519 default:
2520 req->flags = SET_WIN_EXTRA;
2521 req->extra_offset = offset;
2522 req->extra_size = size;
2523 set_win_data( &req->extra_value, newval, size );
2525 if ((ok = !wine_server_call_err( req )))
2527 switch(offset)
2529 case GWL_STYLE:
2530 wndPtr->dwStyle = newval;
2531 retval = reply->old_style;
2532 break;
2533 case GWL_EXSTYLE:
2534 wndPtr->dwExStyle = newval;
2535 retval = reply->old_ex_style;
2536 break;
2537 case GWLP_ID:
2538 wndPtr->wIDmenu = newval;
2539 retval = reply->old_id;
2540 break;
2541 case GWLP_HINSTANCE:
2542 wndPtr->hInstance = (HINSTANCE)newval;
2543 retval = (ULONG_PTR)wine_server_get_ptr( reply->old_instance );
2544 break;
2545 case GWLP_WNDPROC:
2546 break;
2547 case GWLP_USERDATA:
2548 wndPtr->userdata = newval;
2549 retval = reply->old_user_data;
2550 break;
2551 default:
2552 retval = get_win_data( (char *)wndPtr->wExtra + offset, size );
2553 set_win_data( (char *)wndPtr->wExtra + offset, newval, size );
2554 break;
2558 SERVER_END_REQ;
2560 if ((offset == GWL_STYLE && ((style.styleOld ^ style.styleNew) & WS_VISIBLE)) ||
2561 (offset == GWL_EXSTYLE && ((style.styleOld ^ style.styleNew) & WS_EX_LAYERED)))
2563 made_visible = (wndPtr->dwStyle & WS_VISIBLE) != 0;
2564 invalidate_dce( wndPtr, NULL );
2566 WIN_ReleasePtr( wndPtr );
2568 if (!ok) return 0;
2570 if (offset == GWL_STYLE || offset == GWL_EXSTYLE)
2572 style.styleOld = retval;
2573 style.styleNew = newval;
2574 USER_Driver->pSetWindowStyle( hwnd, offset, &style );
2575 if (made_visible) update_window_state( hwnd );
2576 SendMessageW( hwnd, WM_STYLECHANGED, offset, (LPARAM)&style );
2579 return retval;
2583 /**********************************************************************
2584 * GetWindowWord (USER32.@)
2586 WORD WINAPI GetWindowWord( HWND hwnd, INT offset )
2588 switch(offset)
2590 case GWLP_ID:
2591 case GWLP_HINSTANCE:
2592 case GWLP_HWNDPARENT:
2593 break;
2594 default:
2595 if (offset < 0)
2597 WARN("Invalid offset %d\n", offset );
2598 SetLastError( ERROR_INVALID_INDEX );
2599 return 0;
2601 break;
2603 return WIN_GetWindowLong( hwnd, offset, sizeof(WORD), FALSE );
2607 /**********************************************************************
2608 * GetWindowLongA (USER32.@)
2610 LONG WINAPI GetWindowLongA( HWND hwnd, INT offset )
2612 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG), FALSE );
2616 /**********************************************************************
2617 * GetWindowLongW (USER32.@)
2619 LONG WINAPI GetWindowLongW( HWND hwnd, INT offset )
2621 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG), TRUE );
2625 /**********************************************************************
2626 * SetWindowWord (USER32.@)
2628 WORD WINAPI SetWindowWord( HWND hwnd, INT offset, WORD newval )
2630 switch(offset)
2632 case GWLP_ID:
2633 case GWLP_HINSTANCE:
2634 case GWLP_HWNDPARENT:
2635 break;
2636 default:
2637 if (offset < 0)
2639 WARN("Invalid offset %d\n", offset );
2640 SetLastError( ERROR_INVALID_INDEX );
2641 return 0;
2643 break;
2645 return WIN_SetWindowLong( hwnd, offset, sizeof(WORD), newval, FALSE );
2649 /**********************************************************************
2650 * SetWindowLongA (USER32.@)
2652 * See SetWindowLongW.
2654 LONG WINAPI DECLSPEC_HOTPATCH SetWindowLongA( HWND hwnd, INT offset, LONG newval )
2656 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG), newval, FALSE );
2660 /**********************************************************************
2661 * SetWindowLongW (USER32.@) Set window attribute
2663 * SetWindowLong() alters one of a window's attributes or sets a 32-bit (long)
2664 * value in a window's extra memory.
2666 * The _hwnd_ parameter specifies the handle to a window that
2667 * has extra memory. The _newval_ parameter contains the new
2668 * attribute or extra memory value. If positive, the _offset_
2669 * parameter is the byte-addressed location in the window's extra
2670 * memory to set. If negative, _offset_ specifies the window
2671 * attribute to set, and should be one of the following values:
2673 * GWL_EXSTYLE The window's extended window style
2675 * GWL_STYLE The window's window style.
2677 * GWLP_WNDPROC Pointer to the window's window procedure.
2679 * GWLP_HINSTANCE The window's application instance handle.
2681 * GWLP_ID The window's identifier.
2683 * GWLP_USERDATA The window's user-specified data.
2685 * If the window is a dialog box, the _offset_ parameter can be one of
2686 * the following values:
2688 * DWLP_DLGPROC The address of the window's dialog box procedure.
2690 * DWLP_MSGRESULT The return value of a message
2691 * that the dialog box procedure processed.
2693 * DWLP_USER Application specific information.
2695 * RETURNS
2697 * If successful, returns the previous value located at _offset_. Otherwise,
2698 * returns 0.
2700 * NOTES
2702 * Extra memory for a window class is specified by a nonzero cbWndExtra
2703 * parameter of the WNDCLASS structure passed to RegisterClass() at the
2704 * time of class creation.
2706 * Using GWL_WNDPROC to set a new window procedure effectively creates
2707 * a window subclass. Use CallWindowProc() in the new windows procedure
2708 * to pass messages to the superclass's window procedure.
2710 * The user data is reserved for use by the application which created
2711 * the window.
2713 * Do not use GWL_STYLE to change the window's WS_DISABLED style;
2714 * instead, call the EnableWindow() function to change the window's
2715 * disabled state.
2717 * Do not use GWL_HWNDPARENT to reset the window's parent, use
2718 * SetParent() instead.
2720 * Win95:
2721 * When offset is GWL_STYLE and the calling app's ver is 4.0,
2722 * it sends WM_STYLECHANGING before changing the settings
2723 * and WM_STYLECHANGED afterwards.
2724 * App ver 4.0 can't use SetWindowLong to change WS_EX_TOPMOST.
2726 LONG WINAPI DECLSPEC_HOTPATCH SetWindowLongW(
2727 HWND hwnd, /* [in] window to alter */
2728 INT offset, /* [in] offset, in bytes, of location to alter */
2729 LONG newval /* [in] new value of location */
2731 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG), newval, TRUE );
2735 /*******************************************************************
2736 * GetWindowTextA (USER32.@)
2738 INT WINAPI GetWindowTextA( HWND hwnd, LPSTR lpString, INT nMaxCount )
2740 WCHAR *buffer;
2742 if (!lpString || nMaxCount <= 0) return 0;
2744 if (WIN_IsCurrentProcess( hwnd ))
2746 lpString[0] = 0;
2747 return (INT)SendMessageA( hwnd, WM_GETTEXT, nMaxCount, (LPARAM)lpString );
2750 /* when window belongs to other process, don't send a message */
2751 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, nMaxCount * sizeof(WCHAR) ))) return 0;
2752 get_server_window_text( hwnd, buffer, nMaxCount );
2753 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, lpString, nMaxCount, NULL, NULL ))
2754 lpString[nMaxCount-1] = 0;
2755 HeapFree( GetProcessHeap(), 0, buffer );
2756 return strlen(lpString);
2760 /*******************************************************************
2761 * InternalGetWindowText (USER32.@)
2763 INT WINAPI InternalGetWindowText(HWND hwnd,LPWSTR lpString,INT nMaxCount )
2765 WND *win;
2767 if (nMaxCount <= 0) return 0;
2768 if (!(win = WIN_GetPtr( hwnd ))) return 0;
2769 if (win == WND_DESKTOP) lpString[0] = 0;
2770 else if (win != WND_OTHER_PROCESS)
2772 if (win->text) lstrcpynW( lpString, win->text, nMaxCount );
2773 else lpString[0] = 0;
2774 WIN_ReleasePtr( win );
2776 else
2778 get_server_window_text( hwnd, lpString, nMaxCount );
2780 return strlenW(lpString);
2784 /*******************************************************************
2785 * GetWindowTextW (USER32.@)
2787 INT WINAPI GetWindowTextW( HWND hwnd, LPWSTR lpString, INT nMaxCount )
2789 if (!lpString || nMaxCount <= 0) return 0;
2791 if (WIN_IsCurrentProcess( hwnd ))
2793 lpString[0] = 0;
2794 return (INT)SendMessageW( hwnd, WM_GETTEXT, nMaxCount, (LPARAM)lpString );
2797 /* when window belongs to other process, don't send a message */
2798 get_server_window_text( hwnd, lpString, nMaxCount );
2799 return strlenW(lpString);
2803 /*******************************************************************
2804 * SetWindowTextA (USER32.@)
2805 * SetWindowText (USER32.@)
2807 BOOL WINAPI DECLSPEC_HOTPATCH SetWindowTextA( HWND hwnd, LPCSTR lpString )
2809 if (is_broadcast(hwnd))
2811 SetLastError( ERROR_INVALID_PARAMETER );
2812 return FALSE;
2814 if (!WIN_IsCurrentProcess( hwnd ))
2815 WARN( "setting text %s of other process window %p should not use SendMessage\n",
2816 debugstr_a(lpString), hwnd );
2817 return (BOOL)SendMessageA( hwnd, WM_SETTEXT, 0, (LPARAM)lpString );
2821 /*******************************************************************
2822 * SetWindowTextW (USER32.@)
2824 BOOL WINAPI DECLSPEC_HOTPATCH SetWindowTextW( HWND hwnd, LPCWSTR lpString )
2826 if (is_broadcast(hwnd))
2828 SetLastError( ERROR_INVALID_PARAMETER );
2829 return FALSE;
2831 if (!WIN_IsCurrentProcess( hwnd ))
2832 WARN( "setting text %s of other process window %p should not use SendMessage\n",
2833 debugstr_w(lpString), hwnd );
2834 return (BOOL)SendMessageW( hwnd, WM_SETTEXT, 0, (LPARAM)lpString );
2838 /*******************************************************************
2839 * GetWindowTextLengthA (USER32.@)
2841 INT WINAPI GetWindowTextLengthA( HWND hwnd )
2843 return SendMessageA( hwnd, WM_GETTEXTLENGTH, 0, 0 );
2846 /*******************************************************************
2847 * GetWindowTextLengthW (USER32.@)
2849 INT WINAPI GetWindowTextLengthW( HWND hwnd )
2851 return SendMessageW( hwnd, WM_GETTEXTLENGTH, 0, 0 );
2855 /*******************************************************************
2856 * IsWindow (USER32.@)
2858 BOOL WINAPI IsWindow( HWND hwnd )
2860 WND *ptr;
2861 BOOL ret;
2863 if (!(ptr = WIN_GetPtr( hwnd ))) return FALSE;
2864 if (ptr == WND_DESKTOP) return TRUE;
2866 if (ptr != WND_OTHER_PROCESS)
2868 WIN_ReleasePtr( ptr );
2869 return TRUE;
2872 /* check other processes */
2873 SERVER_START_REQ( get_window_info )
2875 req->handle = wine_server_user_handle( hwnd );
2876 ret = !wine_server_call_err( req );
2878 SERVER_END_REQ;
2879 return ret;
2883 /***********************************************************************
2884 * GetWindowThreadProcessId (USER32.@)
2886 DWORD WINAPI GetWindowThreadProcessId( HWND hwnd, LPDWORD process )
2888 WND *ptr;
2889 DWORD tid = 0;
2891 if (!(ptr = WIN_GetPtr( hwnd )))
2893 SetLastError( ERROR_INVALID_WINDOW_HANDLE);
2894 return 0;
2897 if (ptr != WND_OTHER_PROCESS && ptr != WND_DESKTOP)
2899 /* got a valid window */
2900 tid = ptr->tid;
2901 if (process) *process = GetCurrentProcessId();
2902 WIN_ReleasePtr( ptr );
2903 return tid;
2906 /* check other processes */
2907 SERVER_START_REQ( get_window_info )
2909 req->handle = wine_server_user_handle( hwnd );
2910 if (!wine_server_call_err( req ))
2912 tid = (DWORD)reply->tid;
2913 if (process) *process = (DWORD)reply->pid;
2916 SERVER_END_REQ;
2917 return tid;
2921 /*****************************************************************
2922 * GetParent (USER32.@)
2924 HWND WINAPI GetParent( HWND hwnd )
2926 WND *wndPtr;
2927 HWND retvalue = 0;
2929 if (!(wndPtr = WIN_GetPtr( hwnd )))
2931 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2932 return 0;
2934 if (wndPtr == WND_DESKTOP) return 0;
2935 if (wndPtr == WND_OTHER_PROCESS)
2937 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
2938 if (style & (WS_POPUP | WS_CHILD))
2940 SERVER_START_REQ( get_window_tree )
2942 req->handle = wine_server_user_handle( hwnd );
2943 if (!wine_server_call_err( req ))
2945 if (style & WS_POPUP) retvalue = wine_server_ptr_handle( reply->owner );
2946 else if (style & WS_CHILD) retvalue = wine_server_ptr_handle( reply->parent );
2949 SERVER_END_REQ;
2952 else
2954 if (wndPtr->dwStyle & WS_POPUP) retvalue = wndPtr->owner;
2955 else if (wndPtr->dwStyle & WS_CHILD) retvalue = wndPtr->parent;
2956 WIN_ReleasePtr( wndPtr );
2958 return retvalue;
2962 /*****************************************************************
2963 * GetAncestor (USER32.@)
2965 HWND WINAPI GetAncestor( HWND hwnd, UINT type )
2967 WND *win;
2968 HWND *list, ret = 0;
2970 switch(type)
2972 case GA_PARENT:
2973 if (!(win = WIN_GetPtr( hwnd )))
2975 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2976 return 0;
2978 if (win == WND_DESKTOP) return 0;
2979 if (win != WND_OTHER_PROCESS)
2981 ret = win->parent;
2982 WIN_ReleasePtr( win );
2984 else /* need to query the server */
2986 SERVER_START_REQ( get_window_tree )
2988 req->handle = wine_server_user_handle( hwnd );
2989 if (!wine_server_call_err( req )) ret = wine_server_ptr_handle( reply->parent );
2991 SERVER_END_REQ;
2993 break;
2995 case GA_ROOT:
2996 if (!(list = list_window_parents( hwnd ))) return 0;
2998 if (!list[0] || !list[1]) ret = WIN_GetFullHandle( hwnd ); /* top-level window */
2999 else
3001 int count = 2;
3002 while (list[count]) count++;
3003 ret = list[count - 2]; /* get the one before the desktop */
3005 HeapFree( GetProcessHeap(), 0, list );
3006 break;
3008 case GA_ROOTOWNER:
3009 if (is_desktop_window( hwnd )) return 0;
3010 ret = WIN_GetFullHandle( hwnd );
3011 for (;;)
3013 HWND parent = GetParent( ret );
3014 if (!parent) break;
3015 ret = parent;
3017 break;
3019 return ret;
3023 /*****************************************************************
3024 * SetParent (USER32.@)
3026 HWND WINAPI SetParent( HWND hwnd, HWND parent )
3028 HWND full_handle;
3029 HWND old_parent = 0;
3030 BOOL was_visible;
3031 WND *wndPtr;
3032 POINT pt;
3033 BOOL ret;
3035 TRACE("(%p %p)\n", hwnd, parent);
3037 if (is_broadcast(hwnd) || is_broadcast(parent))
3039 SetLastError(ERROR_INVALID_PARAMETER);
3040 return 0;
3043 if (!parent) parent = GetDesktopWindow();
3044 else if (parent == HWND_MESSAGE) parent = get_hwnd_message_parent();
3045 else parent = WIN_GetFullHandle( parent );
3047 if (!IsWindow( parent ))
3049 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3050 return 0;
3053 /* Some applications try to set a child as a parent */
3054 if (IsChild(hwnd, parent))
3056 SetLastError( ERROR_INVALID_PARAMETER );
3057 return 0;
3060 if (!(full_handle = WIN_IsCurrentThread( hwnd )))
3061 return (HWND)SendMessageW( hwnd, WM_WINE_SETPARENT, (WPARAM)parent, 0 );
3063 if (full_handle == parent)
3065 SetLastError( ERROR_INVALID_PARAMETER );
3066 return 0;
3069 /* Windows hides the window first, then shows it again
3070 * including the WM_SHOWWINDOW messages and all */
3071 was_visible = ShowWindow( hwnd, SW_HIDE );
3073 wndPtr = WIN_GetPtr( hwnd );
3074 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return 0;
3076 pt.x = wndPtr->rectWindow.left;
3077 pt.y = wndPtr->rectWindow.top;
3079 SERVER_START_REQ( set_parent )
3081 req->handle = wine_server_user_handle( hwnd );
3082 req->parent = wine_server_user_handle( parent );
3083 if ((ret = !wine_server_call( req )))
3085 old_parent = wine_server_ptr_handle( reply->old_parent );
3086 wndPtr->parent = parent = wine_server_ptr_handle( reply->full_parent );
3090 SERVER_END_REQ;
3091 WIN_ReleasePtr( wndPtr );
3092 if (!ret) return 0;
3094 USER_Driver->pSetParent( full_handle, parent, old_parent );
3096 /* SetParent additionally needs to make hwnd the topmost window
3097 in the x-order and send the expected WM_WINDOWPOSCHANGING and
3098 WM_WINDOWPOSCHANGED notification messages.
3100 SetWindowPos( hwnd, HWND_TOP, pt.x, pt.y, 0, 0, SWP_NOSIZE );
3102 if (was_visible) ShowWindow( hwnd, SW_SHOW );
3104 return old_parent;
3108 /*******************************************************************
3109 * IsChild (USER32.@)
3111 BOOL WINAPI IsChild( HWND parent, HWND child )
3113 HWND *list;
3114 int i;
3115 BOOL ret = FALSE;
3117 if (!(GetWindowLongW( child, GWL_STYLE ) & WS_CHILD)) return FALSE;
3118 if (!(list = list_window_parents( child ))) return FALSE;
3119 parent = WIN_GetFullHandle( parent );
3120 for (i = 0; list[i]; i++)
3122 if (list[i] == parent)
3124 ret = list[i] && list[i+1];
3125 break;
3127 if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_CHILD)) break;
3129 HeapFree( GetProcessHeap(), 0, list );
3130 return ret;
3134 /***********************************************************************
3135 * IsWindowVisible (USER32.@)
3137 BOOL WINAPI IsWindowVisible( HWND hwnd )
3139 HWND *list;
3140 BOOL retval = TRUE;
3141 int i;
3143 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)) return FALSE;
3144 if (!(list = list_window_parents( hwnd ))) return TRUE;
3145 if (list[0])
3147 for (i = 0; list[i+1]; i++)
3148 if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_VISIBLE)) break;
3149 retval = !list[i+1] && (list[i] == GetDesktopWindow()); /* top message window isn't visible */
3151 HeapFree( GetProcessHeap(), 0, list );
3152 return retval;
3156 /***********************************************************************
3157 * WIN_IsWindowDrawable
3159 * hwnd is drawable when it is visible, all parents are not
3160 * minimized, and it is itself not minimized unless we are
3161 * trying to draw its default class icon.
3163 BOOL WIN_IsWindowDrawable( HWND hwnd, BOOL icon )
3165 HWND *list;
3166 BOOL retval = TRUE;
3167 int i;
3168 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
3170 if (!(style & WS_VISIBLE)) return FALSE;
3171 if ((style & WS_MINIMIZE) && icon && GetClassLongPtrW( hwnd, GCLP_HICON )) return FALSE;
3173 if (!(list = list_window_parents( hwnd ))) return TRUE;
3174 if (list[0])
3176 for (i = 0; list[i+1]; i++)
3177 if ((GetWindowLongW( list[i], GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != WS_VISIBLE)
3178 break;
3179 retval = !list[i+1] && (list[i] == GetDesktopWindow()); /* top message window isn't visible */
3181 HeapFree( GetProcessHeap(), 0, list );
3182 return retval;
3186 /*******************************************************************
3187 * GetTopWindow (USER32.@)
3189 HWND WINAPI GetTopWindow( HWND hwnd )
3191 if (!hwnd) hwnd = GetDesktopWindow();
3192 return GetWindow( hwnd, GW_CHILD );
3196 /*******************************************************************
3197 * GetWindow (USER32.@)
3199 HWND WINAPI GetWindow( HWND hwnd, UINT rel )
3201 HWND retval = 0;
3203 if (rel == GW_OWNER) /* this one may be available locally */
3205 WND *wndPtr = WIN_GetPtr( hwnd );
3206 if (!wndPtr)
3208 SetLastError( ERROR_INVALID_HANDLE );
3209 return 0;
3211 if (wndPtr == WND_DESKTOP) return 0;
3212 if (wndPtr != WND_OTHER_PROCESS)
3214 retval = wndPtr->owner;
3215 WIN_ReleasePtr( wndPtr );
3216 return retval;
3218 /* else fall through to server call */
3221 SERVER_START_REQ( get_window_tree )
3223 req->handle = wine_server_user_handle( hwnd );
3224 if (!wine_server_call_err( req ))
3226 switch(rel)
3228 case GW_HWNDFIRST:
3229 retval = wine_server_ptr_handle( reply->first_sibling );
3230 break;
3231 case GW_HWNDLAST:
3232 retval = wine_server_ptr_handle( reply->last_sibling );
3233 break;
3234 case GW_HWNDNEXT:
3235 retval = wine_server_ptr_handle( reply->next_sibling );
3236 break;
3237 case GW_HWNDPREV:
3238 retval = wine_server_ptr_handle( reply->prev_sibling );
3239 break;
3240 case GW_OWNER:
3241 retval = wine_server_ptr_handle( reply->owner );
3242 break;
3243 case GW_CHILD:
3244 retval = wine_server_ptr_handle( reply->first_child );
3245 break;
3249 SERVER_END_REQ;
3250 return retval;
3254 /*******************************************************************
3255 * ShowOwnedPopups (USER32.@)
3257 BOOL WINAPI ShowOwnedPopups( HWND owner, BOOL fShow )
3259 int count = 0;
3260 HWND *win_array = WIN_ListChildren( GetDesktopWindow() );
3262 if (!win_array) return TRUE;
3264 while (win_array[count]) count++;
3265 while (--count >= 0)
3267 if (GetWindow( win_array[count], GW_OWNER ) != owner) continue;
3268 if (fShow)
3270 if (win_get_flags( win_array[count] ) & WIN_NEEDS_SHOW_OWNEDPOPUP)
3271 /* In Windows, ShowOwnedPopups(TRUE) generates
3272 * WM_SHOWWINDOW messages with SW_PARENTOPENING,
3273 * regardless of the state of the owner
3275 SendMessageW(win_array[count], WM_SHOWWINDOW, SW_SHOWNORMAL, SW_PARENTOPENING);
3277 else
3279 if (GetWindowLongW( win_array[count], GWL_STYLE ) & WS_VISIBLE)
3280 /* In Windows, ShowOwnedPopups(FALSE) generates
3281 * WM_SHOWWINDOW messages with SW_PARENTCLOSING,
3282 * regardless of the state of the owner
3284 SendMessageW(win_array[count], WM_SHOWWINDOW, SW_HIDE, SW_PARENTCLOSING);
3287 HeapFree( GetProcessHeap(), 0, win_array );
3288 return TRUE;
3292 /*******************************************************************
3293 * GetLastActivePopup (USER32.@)
3295 HWND WINAPI GetLastActivePopup( HWND hwnd )
3297 HWND retval = hwnd;
3299 SERVER_START_REQ( get_window_info )
3301 req->handle = wine_server_user_handle( hwnd );
3302 if (!wine_server_call_err( req )) retval = wine_server_ptr_handle( reply->last_active );
3304 SERVER_END_REQ;
3305 return retval;
3309 /*******************************************************************
3310 * WIN_ListChildren
3312 * Build an array of the children of a given window. The array must be
3313 * freed with HeapFree. Returns NULL when no windows are found.
3315 HWND *WIN_ListChildren( HWND hwnd )
3317 if (!hwnd)
3319 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3320 return NULL;
3322 return list_window_children( 0, hwnd, NULL, 0 );
3326 /*******************************************************************
3327 * EnumWindows (USER32.@)
3329 BOOL WINAPI EnumWindows( WNDENUMPROC lpEnumFunc, LPARAM lParam )
3331 HWND *list;
3332 BOOL ret = TRUE;
3333 int i;
3335 USER_CheckNotLock();
3337 /* We have to build a list of all windows first, to avoid */
3338 /* unpleasant side-effects, for instance if the callback */
3339 /* function changes the Z-order of the windows. */
3341 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return TRUE;
3343 /* Now call the callback function for every window */
3345 for (i = 0; list[i]; i++)
3347 /* Make sure that the window still exists */
3348 if (!IsWindow( list[i] )) continue;
3349 if (!(ret = lpEnumFunc( list[i], lParam ))) break;
3351 HeapFree( GetProcessHeap(), 0, list );
3352 return ret;
3356 /**********************************************************************
3357 * EnumThreadWindows (USER32.@)
3359 BOOL WINAPI EnumThreadWindows( DWORD id, WNDENUMPROC func, LPARAM lParam )
3361 HWND *list;
3362 int i;
3363 BOOL ret = TRUE;
3365 USER_CheckNotLock();
3367 if (!(list = list_window_children( 0, GetDesktopWindow(), NULL, id ))) return TRUE;
3369 /* Now call the callback function for every window */
3371 for (i = 0; list[i]; i++)
3372 if (!(ret = func( list[i], lParam ))) break;
3373 HeapFree( GetProcessHeap(), 0, list );
3374 return ret;
3378 /***********************************************************************
3379 * EnumDesktopWindows (USER32.@)
3381 BOOL WINAPI EnumDesktopWindows( HDESK desktop, WNDENUMPROC func, LPARAM lparam )
3383 HWND *list;
3384 int i;
3386 USER_CheckNotLock();
3388 if (!(list = list_window_children( desktop, 0, NULL, 0 ))) return TRUE;
3390 for (i = 0; list[i]; i++)
3391 if (!func( list[i], lparam )) break;
3392 HeapFree( GetProcessHeap(), 0, list );
3393 return TRUE;
3397 #ifdef __i386__
3398 /* Some apps pass a non-stdcall proc to EnumChildWindows,
3399 * so we need a small assembly wrapper to call the proc.
3401 extern LRESULT enum_callback_wrapper( WNDENUMPROC proc, HWND hwnd, LPARAM lparam );
3402 __ASM_GLOBAL_FUNC( enum_callback_wrapper,
3403 "pushl %ebp\n\t"
3404 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
3405 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
3406 "movl %esp,%ebp\n\t"
3407 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
3408 "pushl 16(%ebp)\n\t"
3409 "pushl 12(%ebp)\n\t"
3410 "call *8(%ebp)\n\t"
3411 "leave\n\t"
3412 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
3413 __ASM_CFI(".cfi_same_value %ebp\n\t")
3414 "ret" )
3415 #else
3416 static inline LRESULT enum_callback_wrapper( WNDENUMPROC proc, HWND hwnd, LPARAM lparam )
3418 return proc( hwnd, lparam );
3420 #endif /* __i386__ */
3422 /**********************************************************************
3423 * WIN_EnumChildWindows
3425 * Helper function for EnumChildWindows().
3427 static BOOL WIN_EnumChildWindows( HWND *list, WNDENUMPROC func, LPARAM lParam )
3429 HWND *childList;
3430 BOOL ret = FALSE;
3432 for ( ; *list; list++)
3434 /* Make sure that the window still exists */
3435 if (!IsWindow( *list )) continue;
3436 /* Build children list first */
3437 childList = WIN_ListChildren( *list );
3439 ret = enum_callback_wrapper( func, *list, lParam );
3441 if (childList)
3443 if (ret) ret = WIN_EnumChildWindows( childList, func, lParam );
3444 HeapFree( GetProcessHeap(), 0, childList );
3446 if (!ret) return FALSE;
3448 return TRUE;
3452 /**********************************************************************
3453 * EnumChildWindows (USER32.@)
3455 BOOL WINAPI EnumChildWindows( HWND parent, WNDENUMPROC func, LPARAM lParam )
3457 HWND *list;
3458 BOOL ret;
3460 USER_CheckNotLock();
3462 if (!(list = WIN_ListChildren( parent ))) return FALSE;
3463 ret = WIN_EnumChildWindows( list, func, lParam );
3464 HeapFree( GetProcessHeap(), 0, list );
3465 return ret;
3469 /*******************************************************************
3470 * AnyPopup (USER32.@)
3472 BOOL WINAPI AnyPopup(void)
3474 int i;
3475 BOOL retvalue;
3476 HWND *list = WIN_ListChildren( GetDesktopWindow() );
3478 if (!list) return FALSE;
3479 for (i = 0; list[i]; i++)
3481 if (IsWindowVisible( list[i] ) && GetWindow( list[i], GW_OWNER )) break;
3483 retvalue = (list[i] != 0);
3484 HeapFree( GetProcessHeap(), 0, list );
3485 return retvalue;
3489 /*******************************************************************
3490 * FlashWindow (USER32.@)
3492 BOOL WINAPI FlashWindow( HWND hWnd, BOOL bInvert )
3494 FLASHWINFO finfo;
3496 finfo.cbSize = sizeof(FLASHWINFO);
3497 finfo.dwFlags = bInvert ? FLASHW_ALL : FLASHW_STOP;
3498 finfo.uCount = 1;
3499 finfo.dwTimeout = 0;
3500 finfo.hwnd = hWnd;
3501 return FlashWindowEx( &finfo );
3504 /*******************************************************************
3505 * FlashWindowEx (USER32.@)
3507 BOOL WINAPI FlashWindowEx( PFLASHWINFO pfinfo )
3509 WND *wndPtr;
3511 TRACE( "%p\n", pfinfo );
3513 if (!pfinfo)
3515 SetLastError( ERROR_NOACCESS );
3516 return FALSE;
3519 if (!pfinfo->hwnd || pfinfo->cbSize != sizeof(FLASHWINFO) || !IsWindow( pfinfo->hwnd ))
3521 SetLastError( ERROR_INVALID_PARAMETER );
3522 return FALSE;
3524 FIXME( "%p - semi-stub\n", pfinfo );
3526 if (IsIconic( pfinfo->hwnd ))
3528 RedrawWindow( pfinfo->hwnd, 0, 0, RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW | RDW_FRAME );
3530 wndPtr = WIN_GetPtr( pfinfo->hwnd );
3531 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
3532 if (pfinfo->dwFlags && !(wndPtr->flags & WIN_NCACTIVATED))
3534 wndPtr->flags |= WIN_NCACTIVATED;
3536 else
3538 wndPtr->flags &= ~WIN_NCACTIVATED;
3540 WIN_ReleasePtr( wndPtr );
3541 USER_Driver->pFlashWindowEx( pfinfo );
3542 return TRUE;
3544 else
3546 WPARAM wparam;
3547 HWND hwnd = pfinfo->hwnd;
3549 wndPtr = WIN_GetPtr( hwnd );
3550 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
3551 hwnd = wndPtr->obj.handle; /* make it a full handle */
3553 if (pfinfo->dwFlags) wparam = !(wndPtr->flags & WIN_NCACTIVATED);
3554 else wparam = (hwnd == GetForegroundWindow());
3556 WIN_ReleasePtr( wndPtr );
3557 SendMessageW( hwnd, WM_NCACTIVATE, wparam, 0 );
3558 USER_Driver->pFlashWindowEx( pfinfo );
3559 return wparam;
3563 /*******************************************************************
3564 * GetWindowContextHelpId (USER32.@)
3566 DWORD WINAPI GetWindowContextHelpId( HWND hwnd )
3568 DWORD retval;
3569 WND *wnd = WIN_GetPtr( hwnd );
3570 if (!wnd || wnd == WND_DESKTOP) return 0;
3571 if (wnd == WND_OTHER_PROCESS)
3573 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
3574 return 0;
3576 retval = wnd->helpContext;
3577 WIN_ReleasePtr( wnd );
3578 return retval;
3582 /*******************************************************************
3583 * SetWindowContextHelpId (USER32.@)
3585 BOOL WINAPI SetWindowContextHelpId( HWND hwnd, DWORD id )
3587 WND *wnd = WIN_GetPtr( hwnd );
3588 if (!wnd || wnd == WND_DESKTOP) return FALSE;
3589 if (wnd == WND_OTHER_PROCESS)
3591 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
3592 return FALSE;
3594 wnd->helpContext = id;
3595 WIN_ReleasePtr( wnd );
3596 return TRUE;
3600 /*******************************************************************
3601 * DragDetect (USER32.@)
3603 BOOL WINAPI DragDetect( HWND hWnd, POINT pt )
3605 MSG msg;
3606 RECT rect;
3607 WORD wDragWidth = GetSystemMetrics(SM_CXDRAG);
3608 WORD wDragHeight= GetSystemMetrics(SM_CYDRAG);
3610 SetRect(&rect, pt.x - wDragWidth, pt.y - wDragHeight, pt.x + wDragWidth, pt.y + wDragHeight);
3612 SetCapture(hWnd);
3614 while(1)
3616 while (PeekMessageW( &msg, 0, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE ))
3618 if( msg.message == WM_LBUTTONUP )
3620 ReleaseCapture();
3621 return FALSE;
3623 if( msg.message == WM_MOUSEMOVE )
3625 POINT tmp;
3626 tmp.x = (short)LOWORD(msg.lParam);
3627 tmp.y = (short)HIWORD(msg.lParam);
3628 if( !PtInRect( &rect, tmp ))
3630 ReleaseCapture();
3631 return TRUE;
3635 WaitMessage();
3637 return FALSE;
3640 /******************************************************************************
3641 * GetWindowModuleFileNameA (USER32.@)
3643 UINT WINAPI GetWindowModuleFileNameA( HWND hwnd, LPSTR module, UINT size )
3645 WND *win;
3646 HINSTANCE hinst;
3648 TRACE( "%p, %p, %u\n", hwnd, module, size );
3650 win = WIN_GetPtr( hwnd );
3651 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP)
3653 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3654 return 0;
3656 hinst = win->hInstance;
3657 WIN_ReleasePtr( win );
3659 return GetModuleFileNameA( hinst, module, size );
3662 /******************************************************************************
3663 * GetWindowModuleFileNameW (USER32.@)
3665 UINT WINAPI GetWindowModuleFileNameW( HWND hwnd, LPWSTR module, UINT size )
3667 WND *win;
3668 HINSTANCE hinst;
3670 TRACE( "%p, %p, %u\n", hwnd, module, size );
3672 win = WIN_GetPtr( hwnd );
3673 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP)
3675 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3676 return 0;
3678 hinst = win->hInstance;
3679 WIN_ReleasePtr( win );
3681 return GetModuleFileNameW( hinst, module, size );
3684 /******************************************************************************
3685 * GetWindowInfo (USER32.@)
3687 * Note: tests show that Windows doesn't check cbSize of the structure.
3689 BOOL WINAPI DECLSPEC_HOTPATCH GetWindowInfo( HWND hwnd, PWINDOWINFO pwi)
3691 if (!pwi) return FALSE;
3692 if (!WIN_GetRectangles( hwnd, COORDS_SCREEN, &pwi->rcWindow, &pwi->rcClient )) return FALSE;
3694 pwi->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
3695 pwi->dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
3696 pwi->dwWindowStatus = ((GetActiveWindow() == hwnd) ? WS_ACTIVECAPTION : 0);
3698 pwi->cxWindowBorders = pwi->rcClient.left - pwi->rcWindow.left;
3699 pwi->cyWindowBorders = pwi->rcWindow.bottom - pwi->rcClient.bottom;
3701 pwi->atomWindowType = GetClassLongW( hwnd, GCW_ATOM );
3702 pwi->wCreatorVersion = 0x0400;
3704 return TRUE;
3707 /******************************************************************************
3708 * SwitchDesktop (USER32.@)
3710 * NOTES: Sets the current input or interactive desktop.
3712 BOOL WINAPI SwitchDesktop( HDESK hDesktop)
3714 FIXME("(hwnd %p) stub!\n", hDesktop);
3715 return TRUE;
3719 /***********************************************************************
3720 * __wine_set_pixel_format
3722 BOOL CDECL __wine_set_pixel_format( HWND hwnd, int format )
3724 WND *win = WIN_GetPtr( hwnd );
3726 if (!win || win == WND_DESKTOP || win == WND_OTHER_PROCESS)
3728 WARN( "setting format %d on win %p not supported\n", format, hwnd );
3729 return FALSE;
3731 win->pixel_format = format;
3732 WIN_ReleasePtr( win );
3734 update_window_state( hwnd );
3735 return TRUE;
3739 /*****************************************************************************
3740 * SetLayeredWindowAttributes (USER32.@)
3742 BOOL WINAPI SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
3744 BOOL ret;
3746 TRACE("(%p,%08x,%d,%x)\n", hwnd, key, alpha, flags);
3748 SERVER_START_REQ( set_window_layered_info )
3750 req->handle = wine_server_user_handle( hwnd );
3751 req->color_key = key;
3752 req->alpha = alpha;
3753 req->flags = flags;
3754 ret = !wine_server_call_err( req );
3756 SERVER_END_REQ;
3758 if (ret) USER_Driver->pSetLayeredWindowAttributes( hwnd, key, alpha, flags );
3760 return ret;
3764 /*****************************************************************************
3765 * GetLayeredWindowAttributes (USER32.@)
3767 BOOL WINAPI GetLayeredWindowAttributes( HWND hwnd, COLORREF *key, BYTE *alpha, DWORD *flags )
3769 BOOL ret;
3771 SERVER_START_REQ( get_window_layered_info )
3773 req->handle = wine_server_user_handle( hwnd );
3774 if ((ret = !wine_server_call_err( req )))
3776 if (key) *key = reply->color_key;
3777 if (alpha) *alpha = reply->alpha;
3778 if (flags) *flags = reply->flags;
3781 SERVER_END_REQ;
3783 return ret;
3787 /*****************************************************************************
3788 * UpdateLayeredWindowIndirect (USER32.@)
3790 BOOL WINAPI UpdateLayeredWindowIndirect( HWND hwnd, const UPDATELAYEREDWINDOWINFO *info )
3792 DWORD flags = SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW;
3793 RECT window_rect, client_rect;
3794 SIZE offset;
3796 if (!info ||
3797 info->cbSize != sizeof(*info) ||
3798 info->dwFlags & ~(ULW_COLORKEY | ULW_ALPHA | ULW_OPAQUE | ULW_EX_NORESIZE) ||
3799 !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED) ||
3800 GetLayeredWindowAttributes( hwnd, NULL, NULL, NULL ))
3802 SetLastError( ERROR_INVALID_PARAMETER );
3803 return FALSE;
3806 WIN_GetRectangles( hwnd, COORDS_PARENT, &window_rect, &client_rect );
3808 if (info->pptDst)
3810 offset.cx = info->pptDst->x - window_rect.left;
3811 offset.cy = info->pptDst->y - window_rect.top;
3812 OffsetRect( &client_rect, offset.cx, offset.cy );
3813 OffsetRect( &window_rect, offset.cx, offset.cy );
3814 flags &= ~SWP_NOMOVE;
3816 if (info->psize)
3818 offset.cx = info->psize->cx - (window_rect.right - window_rect.left);
3819 offset.cy = info->psize->cy - (window_rect.bottom - window_rect.top);
3820 if (info->psize->cx <= 0 || info->psize->cy <= 0)
3822 SetLastError( ERROR_INVALID_PARAMETER );
3823 return FALSE;
3825 if ((info->dwFlags & ULW_EX_NORESIZE) && (offset.cx || offset.cy))
3827 SetLastError( ERROR_INCORRECT_SIZE );
3828 return FALSE;
3830 client_rect.right += offset.cx;
3831 client_rect.bottom += offset.cy;
3832 window_rect.right += offset.cx;
3833 window_rect.bottom += offset.cy;
3834 flags &= ~SWP_NOSIZE;
3837 TRACE( "window %p win %s client %s\n", hwnd,
3838 wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(&client_rect) );
3840 if (!USER_Driver->pUpdateLayeredWindow( hwnd, info, &window_rect )) return FALSE;
3842 set_window_pos( hwnd, 0, flags, &window_rect, &client_rect, NULL );
3843 return TRUE;
3847 /*****************************************************************************
3848 * UpdateLayeredWindow (USER32.@)
3850 BOOL WINAPI UpdateLayeredWindow( HWND hwnd, HDC hdcDst, POINT *pptDst, SIZE *psize,
3851 HDC hdcSrc, POINT *pptSrc, COLORREF crKey, BLENDFUNCTION *pblend,
3852 DWORD flags)
3854 UPDATELAYEREDWINDOWINFO info;
3856 if (flags & ULW_EX_NORESIZE) /* only valid for UpdateLayeredWindowIndirect */
3858 SetLastError( ERROR_INVALID_PARAMETER );
3859 return FALSE;
3861 info.cbSize = sizeof(info);
3862 info.hdcDst = hdcDst;
3863 info.pptDst = pptDst;
3864 info.psize = psize;
3865 info.hdcSrc = hdcSrc;
3866 info.pptSrc = pptSrc;
3867 info.crKey = crKey;
3868 info.pblend = pblend;
3869 info.dwFlags = flags;
3870 info.prcDirty = NULL;
3871 return UpdateLayeredWindowIndirect( hwnd, &info );
3875 /******************************************************************************
3876 * GetProcessDefaultLayout [USER32.@]
3878 * Gets the default layout for parentless windows.
3880 BOOL WINAPI GetProcessDefaultLayout( DWORD *layout )
3882 if (!layout)
3884 SetLastError( ERROR_NOACCESS );
3885 return FALSE;
3887 if (process_layout == ~0u)
3889 static const WCHAR translationW[] = { '\\','V','a','r','F','i','l','e','I','n','f','o',
3890 '\\','T','r','a','n','s','l','a','t','i','o','n', 0 };
3891 static const WCHAR filedescW[] = { '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
3892 '\\','%','0','4','x','%','0','4','x',
3893 '\\','F','i','l','e','D','e','s','c','r','i','p','t','i','o','n',0 };
3894 WCHAR *str, buffer[MAX_PATH];
3895 DWORD i, len, version_layout = 0;
3896 DWORD user_lang = GetUserDefaultLangID();
3897 DWORD *languages;
3898 void *data = NULL;
3900 GetModuleFileNameW( 0, buffer, MAX_PATH );
3901 if (!(len = GetFileVersionInfoSizeW( buffer, NULL ))) goto done;
3902 if (!(data = HeapAlloc( GetProcessHeap(), 0, len ))) goto done;
3903 if (!GetFileVersionInfoW( buffer, 0, len, data )) goto done;
3904 if (!VerQueryValueW( data, translationW, (void **)&languages, &len ) || !len) goto done;
3906 len /= sizeof(DWORD);
3907 for (i = 0; i < len; i++) if (LOWORD(languages[i]) == user_lang) break;
3908 if (i == len) /* try neutral language */
3909 for (i = 0; i < len; i++)
3910 if (LOWORD(languages[i]) == MAKELANGID( PRIMARYLANGID(user_lang), SUBLANG_NEUTRAL )) break;
3911 if (i == len) i = 0; /* default to the first one */
3913 sprintfW( buffer, filedescW, LOWORD(languages[i]), HIWORD(languages[i]) );
3914 if (!VerQueryValueW( data, buffer, (void **)&str, &len )) goto done;
3915 TRACE( "found description %s\n", debugstr_w( str ));
3916 if (str[0] == 0x200e && str[1] == 0x200e) version_layout = LAYOUT_RTL;
3918 done:
3919 HeapFree( GetProcessHeap(), 0, data );
3920 process_layout = version_layout;
3922 *layout = process_layout;
3923 return TRUE;
3927 /******************************************************************************
3928 * SetProcessDefaultLayout [USER32.@]
3930 * Sets the default layout for parentless windows.
3932 BOOL WINAPI SetProcessDefaultLayout( DWORD layout )
3934 process_layout = layout;
3935 return TRUE;
3939 /* 64bit versions */
3941 #ifdef GetWindowLongPtrW
3942 #undef GetWindowLongPtrW
3943 #endif
3945 #ifdef GetWindowLongPtrA
3946 #undef GetWindowLongPtrA
3947 #endif
3949 #ifdef SetWindowLongPtrW
3950 #undef SetWindowLongPtrW
3951 #endif
3953 #ifdef SetWindowLongPtrA
3954 #undef SetWindowLongPtrA
3955 #endif
3957 /*****************************************************************************
3958 * GetWindowLongPtrW (USER32.@)
3960 LONG_PTR WINAPI GetWindowLongPtrW( HWND hwnd, INT offset )
3962 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG_PTR), TRUE );
3965 /*****************************************************************************
3966 * GetWindowLongPtrA (USER32.@)
3968 LONG_PTR WINAPI GetWindowLongPtrA( HWND hwnd, INT offset )
3970 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG_PTR), FALSE );
3973 /*****************************************************************************
3974 * SetWindowLongPtrW (USER32.@)
3976 LONG_PTR WINAPI SetWindowLongPtrW( HWND hwnd, INT offset, LONG_PTR newval )
3978 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG_PTR), newval, TRUE );
3981 /*****************************************************************************
3982 * SetWindowLongPtrA (USER32.@)
3984 LONG_PTR WINAPI SetWindowLongPtrA( HWND hwnd, INT offset, LONG_PTR newval )
3986 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG_PTR), newval, FALSE );
3989 /*****************************************************************************
3990 * RegisterTouchWindow (USER32.@)
3992 BOOL WINAPI RegisterTouchWindow(HWND hwnd, ULONG flags)
3994 FIXME("(%p %08x): stub\n", hwnd, flags);
3995 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
3996 return FALSE;