usp10: Avoid leaking "open_stack" and "stack_index" in case of 0 "pair_count" in...
[wine.git] / dlls / user32 / win.c
blobb13032bd6ef3a3ef8df30d122bc4afb45187f4b8
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 return SendMessageW(hwnd, WM_WINE_SETSTYLE, set_bits, clear_bits);
772 return 0;
774 style.styleOld = win->dwStyle;
775 style.styleNew = (win->dwStyle | set_bits) & ~clear_bits;
776 if (style.styleNew == style.styleOld)
778 WIN_ReleasePtr( win );
779 return style.styleNew;
781 SERVER_START_REQ( set_window_info )
783 req->handle = wine_server_user_handle( hwnd );
784 req->flags = SET_WIN_STYLE;
785 req->style = style.styleNew;
786 req->extra_offset = -1;
787 if ((ok = !wine_server_call( req )))
789 style.styleOld = reply->old_style;
790 win->dwStyle = style.styleNew;
793 SERVER_END_REQ;
795 if (ok && ((style.styleOld ^ style.styleNew) & WS_VISIBLE))
797 made_visible = (style.styleNew & WS_VISIBLE) != 0;
798 invalidate_dce( win, NULL );
800 WIN_ReleasePtr( win );
802 if (!ok) return 0;
804 USER_Driver->pSetWindowStyle( hwnd, GWL_STYLE, &style );
805 if (made_visible) update_window_state( hwnd );
807 return style.styleOld;
811 /***********************************************************************
812 * WIN_GetRectangles
814 * Get the window and client rectangles.
816 BOOL WIN_GetRectangles( HWND hwnd, enum coords_relative relative, RECT *rectWindow, RECT *rectClient )
818 WND *win = WIN_GetPtr( hwnd );
819 BOOL ret = TRUE;
821 if (!win)
823 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
824 return FALSE;
826 if (win == WND_DESKTOP)
828 RECT rect;
829 rect.left = rect.top = 0;
830 if (hwnd == get_hwnd_message_parent())
832 rect.right = 100;
833 rect.bottom = 100;
835 else
837 rect.right = GetSystemMetrics(SM_CXSCREEN);
838 rect.bottom = GetSystemMetrics(SM_CYSCREEN);
840 if (rectWindow) *rectWindow = rect;
841 if (rectClient) *rectClient = rect;
842 return TRUE;
844 if (win != WND_OTHER_PROCESS)
846 RECT window_rect = win->rectWindow, client_rect = win->rectClient;
848 switch (relative)
850 case COORDS_CLIENT:
851 OffsetRect( &window_rect, -win->rectClient.left, -win->rectClient.top );
852 OffsetRect( &client_rect, -win->rectClient.left, -win->rectClient.top );
853 if (win->dwExStyle & WS_EX_LAYOUTRTL)
854 mirror_rect( &win->rectClient, &window_rect );
855 break;
856 case COORDS_WINDOW:
857 OffsetRect( &window_rect, -win->rectWindow.left, -win->rectWindow.top );
858 OffsetRect( &client_rect, -win->rectWindow.left, -win->rectWindow.top );
859 if (win->dwExStyle & WS_EX_LAYOUTRTL)
860 mirror_rect( &win->rectWindow, &client_rect );
861 break;
862 case COORDS_PARENT:
863 if (win->parent)
865 WND *parent = WIN_GetPtr( win->parent );
866 if (parent == WND_DESKTOP) break;
867 if (!parent || parent == WND_OTHER_PROCESS)
869 WIN_ReleasePtr( win );
870 goto other_process;
872 if (parent->flags & WIN_CHILDREN_MOVED)
874 WIN_ReleasePtr( parent );
875 WIN_ReleasePtr( win );
876 goto other_process;
878 if (parent->dwExStyle & WS_EX_LAYOUTRTL)
880 mirror_rect( &parent->rectClient, &window_rect );
881 mirror_rect( &parent->rectClient, &client_rect );
883 WIN_ReleasePtr( parent );
885 break;
886 case COORDS_SCREEN:
887 while (win->parent)
889 WND *parent = WIN_GetPtr( win->parent );
890 if (parent == WND_DESKTOP) break;
891 if (!parent || parent == WND_OTHER_PROCESS)
893 WIN_ReleasePtr( win );
894 goto other_process;
896 WIN_ReleasePtr( win );
897 if (parent->flags & WIN_CHILDREN_MOVED)
899 WIN_ReleasePtr( parent );
900 goto other_process;
902 win = parent;
903 if (win->parent)
905 OffsetRect( &window_rect, win->rectClient.left, win->rectClient.top );
906 OffsetRect( &client_rect, win->rectClient.left, win->rectClient.top );
909 break;
911 if (rectWindow) *rectWindow = window_rect;
912 if (rectClient) *rectClient = client_rect;
913 WIN_ReleasePtr( win );
914 return TRUE;
917 other_process:
918 SERVER_START_REQ( get_window_rectangles )
920 req->handle = wine_server_user_handle( hwnd );
921 req->relative = relative;
922 if ((ret = !wine_server_call_err( req )))
924 if (rectWindow)
926 rectWindow->left = reply->window.left;
927 rectWindow->top = reply->window.top;
928 rectWindow->right = reply->window.right;
929 rectWindow->bottom = reply->window.bottom;
931 if (rectClient)
933 rectClient->left = reply->client.left;
934 rectClient->top = reply->client.top;
935 rectClient->right = reply->client.right;
936 rectClient->bottom = reply->client.bottom;
940 SERVER_END_REQ;
941 return ret;
945 /***********************************************************************
946 * WIN_DestroyWindow
948 * Destroy storage associated to a window. "Internals" p.358
950 LRESULT WIN_DestroyWindow( HWND hwnd )
952 WND *wndPtr;
953 HWND *list;
954 HMENU menu = 0, sys_menu;
955 HWND icon_title;
956 struct window_surface *surface;
958 TRACE("%p\n", hwnd );
960 /* destroy default IME window */
961 if (win_set_flags( hwnd, 0, WIN_HAS_IME_WIN ) & WIN_HAS_IME_WIN)
963 TRACE("unregister IME window for %p\n", hwnd);
964 imm_unregister_window( hwnd );
967 /* free child windows */
968 if ((list = WIN_ListChildren( hwnd )))
970 int i;
971 for (i = 0; list[i]; i++)
973 if (WIN_IsCurrentThread( list[i] )) WIN_DestroyWindow( list[i] );
974 else SendMessageW( list[i], WM_WINE_DESTROYWINDOW, 0, 0 );
976 HeapFree( GetProcessHeap(), 0, list );
979 /* Unlink now so we won't bother with the children later on */
980 SERVER_START_REQ( set_parent )
982 req->handle = wine_server_user_handle( hwnd );
983 req->parent = 0;
984 wine_server_call( req );
986 SERVER_END_REQ;
989 * Send the WM_NCDESTROY to the window being destroyed.
991 SendMessageW( hwnd, WM_NCDESTROY, 0, 0 );
993 /* FIXME: do we need to fake QS_MOUSEMOVE wakebit? */
995 /* free resources associated with the window */
997 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
998 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
999 menu = (HMENU)wndPtr->wIDmenu;
1000 sys_menu = wndPtr->hSysMenu;
1001 free_dce( wndPtr->dce, hwnd );
1002 wndPtr->dce = NULL;
1003 icon_title = wndPtr->icon_title;
1004 HeapFree( GetProcessHeap(), 0, wndPtr->text );
1005 wndPtr->text = NULL;
1006 HeapFree( GetProcessHeap(), 0, wndPtr->pScroll );
1007 wndPtr->pScroll = NULL;
1008 DestroyIcon( wndPtr->hIconSmall2 );
1009 surface = wndPtr->surface;
1010 wndPtr->surface = NULL;
1011 WIN_ReleasePtr( wndPtr );
1013 if (icon_title) DestroyWindow( icon_title );
1014 if (menu) DestroyMenu( menu );
1015 if (sys_menu) DestroyMenu( sys_menu );
1016 if (surface)
1018 register_window_surface( surface, NULL );
1019 window_surface_release( surface );
1022 USER_Driver->pDestroyWindow( hwnd );
1024 free_window_handle( hwnd );
1025 return 0;
1029 /***********************************************************************
1030 * destroy_thread_window
1032 * Destroy a window upon exit of its thread.
1034 static void destroy_thread_window( HWND hwnd )
1036 WND *wndPtr;
1037 HWND *list;
1038 HMENU menu = 0, sys_menu = 0;
1039 struct window_surface *surface = NULL;
1040 WORD index;
1042 /* free child windows */
1044 if ((list = WIN_ListChildren( hwnd )))
1046 int i;
1047 for (i = 0; list[i]; i++)
1049 if (WIN_IsCurrentThread( list[i] )) destroy_thread_window( list[i] );
1050 else SendMessageW( list[i], WM_WINE_DESTROYWINDOW, 0, 0 );
1052 HeapFree( GetProcessHeap(), 0, list );
1055 /* destroy the client-side storage */
1057 index = USER_HANDLE_TO_INDEX(hwnd);
1058 if (index >= NB_USER_HANDLES) return;
1059 USER_Lock();
1060 if ((wndPtr = user_handles[index]))
1062 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD) menu = (HMENU)wndPtr->wIDmenu;
1063 sys_menu = wndPtr->hSysMenu;
1064 free_dce( wndPtr->dce, hwnd );
1065 surface = wndPtr->surface;
1066 wndPtr->surface = NULL;
1067 InterlockedCompareExchangePointer( &user_handles[index], NULL, wndPtr );
1069 USER_Unlock();
1071 HeapFree( GetProcessHeap(), 0, wndPtr );
1072 if (menu) DestroyMenu( menu );
1073 if (sys_menu) DestroyMenu( sys_menu );
1074 if (surface)
1076 register_window_surface( surface, NULL );
1077 window_surface_release( surface );
1082 /***********************************************************************
1083 * destroy_thread_child_windows
1085 * Destroy child windows upon exit of its thread.
1087 static void destroy_thread_child_windows( HWND hwnd )
1089 HWND *list;
1090 int i;
1092 if (WIN_IsCurrentThread( hwnd ))
1094 destroy_thread_window( hwnd );
1096 else if ((list = WIN_ListChildren( hwnd )))
1098 for (i = 0; list[i]; i++) destroy_thread_child_windows( list[i] );
1099 HeapFree( GetProcessHeap(), 0, list );
1104 /***********************************************************************
1105 * WIN_DestroyThreadWindows
1107 * Destroy all children of 'wnd' owned by the current thread.
1109 void WIN_DestroyThreadWindows( HWND hwnd )
1111 HWND *list;
1112 int i;
1114 if (!(list = WIN_ListChildren( hwnd ))) return;
1116 /* reset owners of top-level windows */
1117 for (i = 0; list[i]; i++)
1119 if (!WIN_IsCurrentThread( list[i] ))
1121 HWND owner = GetWindow( list[i], GW_OWNER );
1122 if (owner && WIN_IsCurrentThread( owner )) WIN_SetOwner( list[i], 0 );
1126 for (i = 0; list[i]; i++) destroy_thread_child_windows( list[i] );
1127 HeapFree( GetProcessHeap(), 0, list );
1131 /***********************************************************************
1132 * WIN_FixCoordinates
1134 * Fix the coordinates - Helper for WIN_CreateWindowEx.
1135 * returns default show mode in sw.
1137 static void WIN_FixCoordinates( CREATESTRUCTW *cs, INT *sw)
1139 #define IS_DEFAULT(x) ((x) == CW_USEDEFAULT || (x) == (SHORT)0x8000)
1140 POINT pos[2];
1142 if (cs->dwExStyle & WS_EX_MDICHILD)
1144 UINT id = 0;
1146 MDI_CalcDefaultChildPos(cs->hwndParent, -1, pos, 0, &id);
1147 if (!(cs->style & WS_POPUP)) cs->hMenu = ULongToHandle(id);
1149 TRACE("MDI child id %04x\n", id);
1152 if (cs->style & (WS_CHILD | WS_POPUP))
1154 if (cs->dwExStyle & WS_EX_MDICHILD)
1156 if (IS_DEFAULT(cs->x))
1158 cs->x = pos[0].x;
1159 cs->y = pos[0].y;
1161 if (IS_DEFAULT(cs->cx) || !cs->cx) cs->cx = pos[1].x;
1162 if (IS_DEFAULT(cs->cy) || !cs->cy) cs->cy = pos[1].y;
1164 else
1166 if (IS_DEFAULT(cs->x)) cs->x = cs->y = 0;
1167 if (IS_DEFAULT(cs->cx)) cs->cx = cs->cy = 0;
1170 else /* overlapped window */
1172 HMONITOR monitor;
1173 MONITORINFO mon_info;
1174 STARTUPINFOW info;
1176 if (!IS_DEFAULT(cs->x) && !IS_DEFAULT(cs->cx) && !IS_DEFAULT(cs->cy)) return;
1178 monitor = MonitorFromWindow( cs->hwndParent, MONITOR_DEFAULTTOPRIMARY );
1179 mon_info.cbSize = sizeof(mon_info);
1180 GetMonitorInfoW( monitor, &mon_info );
1181 GetStartupInfoW( &info );
1183 if (IS_DEFAULT(cs->x))
1185 if (!IS_DEFAULT(cs->y)) *sw = cs->y;
1186 cs->x = (info.dwFlags & STARTF_USEPOSITION) ? info.dwX : mon_info.rcWork.left;
1187 cs->y = (info.dwFlags & STARTF_USEPOSITION) ? info.dwY : mon_info.rcWork.top;
1190 if (IS_DEFAULT(cs->cx))
1192 if (info.dwFlags & STARTF_USESIZE)
1194 cs->cx = info.dwXSize;
1195 cs->cy = info.dwYSize;
1197 else
1199 cs->cx = (mon_info.rcWork.right - mon_info.rcWork.left) * 3 / 4 - cs->x;
1200 cs->cy = (mon_info.rcWork.bottom - mon_info.rcWork.top) * 3 / 4 - cs->y;
1203 /* neither x nor cx are default. Check the y values .
1204 * In the trace we see Outlook and Outlook Express using
1205 * cy set to CW_USEDEFAULT when opening the address book.
1207 else if (IS_DEFAULT(cs->cy))
1209 FIXME("Strange use of CW_USEDEFAULT in nHeight\n");
1210 cs->cy = (mon_info.rcWork.bottom - mon_info.rcWork.top) * 3 / 4 - cs->y;
1213 #undef IS_DEFAULT
1216 /***********************************************************************
1217 * dump_window_styles
1219 static void dump_window_styles( DWORD style, DWORD exstyle )
1221 TRACE( "style:" );
1222 if(style & WS_POPUP) TRACE(" WS_POPUP");
1223 if(style & WS_CHILD) TRACE(" WS_CHILD");
1224 if(style & WS_MINIMIZE) TRACE(" WS_MINIMIZE");
1225 if(style & WS_VISIBLE) TRACE(" WS_VISIBLE");
1226 if(style & WS_DISABLED) TRACE(" WS_DISABLED");
1227 if(style & WS_CLIPSIBLINGS) TRACE(" WS_CLIPSIBLINGS");
1228 if(style & WS_CLIPCHILDREN) TRACE(" WS_CLIPCHILDREN");
1229 if(style & WS_MAXIMIZE) TRACE(" WS_MAXIMIZE");
1230 if((style & WS_CAPTION) == WS_CAPTION) TRACE(" WS_CAPTION");
1231 else
1233 if(style & WS_BORDER) TRACE(" WS_BORDER");
1234 if(style & WS_DLGFRAME) TRACE(" WS_DLGFRAME");
1236 if(style & WS_VSCROLL) TRACE(" WS_VSCROLL");
1237 if(style & WS_HSCROLL) TRACE(" WS_HSCROLL");
1238 if(style & WS_SYSMENU) TRACE(" WS_SYSMENU");
1239 if(style & WS_THICKFRAME) TRACE(" WS_THICKFRAME");
1240 if (style & WS_CHILD)
1242 if(style & WS_GROUP) TRACE(" WS_GROUP");
1243 if(style & WS_TABSTOP) TRACE(" WS_TABSTOP");
1245 else
1247 if(style & WS_MINIMIZEBOX) TRACE(" WS_MINIMIZEBOX");
1248 if(style & WS_MAXIMIZEBOX) TRACE(" WS_MAXIMIZEBOX");
1251 /* FIXME: Add dumping of BS_/ES_/SBS_/LBS_/CBS_/DS_/etc. styles */
1252 #define DUMPED_STYLES \
1253 ((DWORD)(WS_POPUP | \
1254 WS_CHILD | \
1255 WS_MINIMIZE | \
1256 WS_VISIBLE | \
1257 WS_DISABLED | \
1258 WS_CLIPSIBLINGS | \
1259 WS_CLIPCHILDREN | \
1260 WS_MAXIMIZE | \
1261 WS_BORDER | \
1262 WS_DLGFRAME | \
1263 WS_VSCROLL | \
1264 WS_HSCROLL | \
1265 WS_SYSMENU | \
1266 WS_THICKFRAME | \
1267 WS_GROUP | \
1268 WS_TABSTOP | \
1269 WS_MINIMIZEBOX | \
1270 WS_MAXIMIZEBOX))
1272 if(style & ~DUMPED_STYLES) TRACE(" %08x", style & ~DUMPED_STYLES);
1273 TRACE("\n");
1274 #undef DUMPED_STYLES
1276 TRACE( "exstyle:" );
1277 if(exstyle & WS_EX_DLGMODALFRAME) TRACE(" WS_EX_DLGMODALFRAME");
1278 if(exstyle & WS_EX_DRAGDETECT) TRACE(" WS_EX_DRAGDETECT");
1279 if(exstyle & WS_EX_NOPARENTNOTIFY) TRACE(" WS_EX_NOPARENTNOTIFY");
1280 if(exstyle & WS_EX_TOPMOST) TRACE(" WS_EX_TOPMOST");
1281 if(exstyle & WS_EX_ACCEPTFILES) TRACE(" WS_EX_ACCEPTFILES");
1282 if(exstyle & WS_EX_TRANSPARENT) TRACE(" WS_EX_TRANSPARENT");
1283 if(exstyle & WS_EX_MDICHILD) TRACE(" WS_EX_MDICHILD");
1284 if(exstyle & WS_EX_TOOLWINDOW) TRACE(" WS_EX_TOOLWINDOW");
1285 if(exstyle & WS_EX_WINDOWEDGE) TRACE(" WS_EX_WINDOWEDGE");
1286 if(exstyle & WS_EX_CLIENTEDGE) TRACE(" WS_EX_CLIENTEDGE");
1287 if(exstyle & WS_EX_CONTEXTHELP) TRACE(" WS_EX_CONTEXTHELP");
1288 if(exstyle & WS_EX_RIGHT) TRACE(" WS_EX_RIGHT");
1289 if(exstyle & WS_EX_RTLREADING) TRACE(" WS_EX_RTLREADING");
1290 if(exstyle & WS_EX_LEFTSCROLLBAR) TRACE(" WS_EX_LEFTSCROLLBAR");
1291 if(exstyle & WS_EX_CONTROLPARENT) TRACE(" WS_EX_CONTROLPARENT");
1292 if(exstyle & WS_EX_STATICEDGE) TRACE(" WS_EX_STATICEDGE");
1293 if(exstyle & WS_EX_APPWINDOW) TRACE(" WS_EX_APPWINDOW");
1294 if(exstyle & WS_EX_LAYERED) TRACE(" WS_EX_LAYERED");
1295 if(exstyle & WS_EX_LAYOUTRTL) TRACE(" WS_EX_LAYOUTRTL");
1297 #define DUMPED_EX_STYLES \
1298 ((DWORD)(WS_EX_DLGMODALFRAME | \
1299 WS_EX_DRAGDETECT | \
1300 WS_EX_NOPARENTNOTIFY | \
1301 WS_EX_TOPMOST | \
1302 WS_EX_ACCEPTFILES | \
1303 WS_EX_TRANSPARENT | \
1304 WS_EX_MDICHILD | \
1305 WS_EX_TOOLWINDOW | \
1306 WS_EX_WINDOWEDGE | \
1307 WS_EX_CLIENTEDGE | \
1308 WS_EX_CONTEXTHELP | \
1309 WS_EX_RIGHT | \
1310 WS_EX_RTLREADING | \
1311 WS_EX_LEFTSCROLLBAR | \
1312 WS_EX_CONTROLPARENT | \
1313 WS_EX_STATICEDGE | \
1314 WS_EX_APPWINDOW | \
1315 WS_EX_LAYERED | \
1316 WS_EX_LAYOUTRTL))
1318 if(exstyle & ~DUMPED_EX_STYLES) TRACE(" %08x", exstyle & ~DUMPED_EX_STYLES);
1319 TRACE("\n");
1320 #undef DUMPED_EX_STYLES
1324 /***********************************************************************
1325 * WIN_CreateWindowEx
1327 * Implementation of CreateWindowEx().
1329 HWND WIN_CreateWindowEx( CREATESTRUCTW *cs, LPCWSTR className, HINSTANCE module, BOOL unicode )
1331 INT cx, cy, style, sw = SW_SHOW;
1332 LRESULT result;
1333 RECT rect;
1334 WND *wndPtr;
1335 HWND hwnd, parent, owner, top_child = 0;
1336 const WCHAR *p = className;
1337 MDICREATESTRUCTW mdi_cs;
1338 CBT_CREATEWNDW cbtc;
1339 CREATESTRUCTW cbcs;
1341 className = CLASS_GetVersionedName(className, NULL);
1343 TRACE("%s %s%s%s ex=%08x style=%08x %d,%d %dx%d parent=%p menu=%p inst=%p params=%p\n",
1344 unicode ? debugstr_w(cs->lpszName) : debugstr_a((LPCSTR)cs->lpszName),
1345 debugstr_w(p), p != className ? "->" : "", p != className ? debugstr_w(className) : "",
1346 cs->dwExStyle, cs->style, cs->x, cs->y, cs->cx, cs->cy,
1347 cs->hwndParent, cs->hMenu, cs->hInstance, cs->lpCreateParams );
1348 if(TRACE_ON(win)) dump_window_styles( cs->style, cs->dwExStyle );
1350 /* Fix the styles for MDI children */
1351 if (cs->dwExStyle & WS_EX_MDICHILD)
1353 if (!(win_get_flags( cs->hwndParent ) & WIN_ISMDICLIENT))
1355 WARN("WS_EX_MDICHILD, but parent %p is not MDIClient\n", cs->hwndParent);
1356 return 0;
1359 /* cs->lpCreateParams of WM_[NC]CREATE is different for MDI children.
1360 * MDICREATESTRUCT members have the originally passed values.
1362 * Note: we rely on the fact that MDICREATESTRUCTA and MDICREATESTRUCTW
1363 * have the same layout.
1365 mdi_cs.szClass = cs->lpszClass;
1366 mdi_cs.szTitle = cs->lpszName;
1367 mdi_cs.hOwner = cs->hInstance;
1368 mdi_cs.x = cs->x;
1369 mdi_cs.y = cs->y;
1370 mdi_cs.cx = cs->cx;
1371 mdi_cs.cy = cs->cy;
1372 mdi_cs.style = cs->style;
1373 mdi_cs.lParam = (LPARAM)cs->lpCreateParams;
1375 cs->lpCreateParams = &mdi_cs;
1377 if (GetWindowLongW(cs->hwndParent, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1379 if (cs->style & WS_POPUP)
1381 TRACE("WS_POPUP with MDIS_ALLCHILDSTYLES is not allowed\n");
1382 return 0;
1384 cs->style |= WS_CHILD | WS_CLIPSIBLINGS;
1386 else
1388 cs->style &= ~WS_POPUP;
1389 cs->style |= WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CAPTION |
1390 WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
1393 top_child = GetWindow(cs->hwndParent, GW_CHILD);
1395 if (top_child)
1397 /* Restore current maximized child */
1398 if((cs->style & WS_VISIBLE) && IsZoomed(top_child))
1400 TRACE("Restoring current maximized child %p\n", top_child);
1401 if (cs->style & WS_MAXIMIZE)
1403 /* if the new window is maximized don't bother repainting */
1404 SendMessageW( top_child, WM_SETREDRAW, FALSE, 0 );
1405 ShowWindow( top_child, SW_SHOWNORMAL );
1406 SendMessageW( top_child, WM_SETREDRAW, TRUE, 0 );
1408 else ShowWindow( top_child, SW_SHOWNORMAL );
1413 /* Find the parent window */
1415 parent = cs->hwndParent;
1416 owner = 0;
1418 if (cs->hwndParent == HWND_MESSAGE)
1420 cs->hwndParent = parent = get_hwnd_message_parent();
1422 else if (cs->hwndParent)
1424 if ((cs->style & (WS_CHILD|WS_POPUP)) != WS_CHILD)
1426 parent = GetDesktopWindow();
1427 owner = cs->hwndParent;
1429 else
1431 DWORD parent_style = GetWindowLongW( parent, GWL_EXSTYLE );
1432 if ((parent_style & WS_EX_LAYOUTRTL) && !(parent_style & WS_EX_NOINHERITLAYOUT))
1433 cs->dwExStyle |= WS_EX_LAYOUTRTL;
1436 else
1438 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
1440 if ((cs->style & (WS_CHILD|WS_POPUP)) == WS_CHILD)
1442 WARN("No parent for child window\n" );
1443 SetLastError(ERROR_TLW_WITH_WSCHILD);
1444 return 0; /* WS_CHILD needs a parent, but WS_POPUP doesn't */
1447 /* are we creating the desktop or HWND_MESSAGE parent itself? */
1448 if (className != (LPCWSTR)DESKTOP_CLASS_ATOM &&
1449 (IS_INTRESOURCE(className) || strcmpiW( className, messageW )))
1451 DWORD layout;
1452 GetProcessDefaultLayout( &layout );
1453 if (layout & LAYOUT_RTL) cs->dwExStyle |= WS_EX_LAYOUTRTL;
1454 parent = GetDesktopWindow();
1458 WIN_FixCoordinates(cs, &sw); /* fix default coordinates */
1460 if ((cs->dwExStyle & WS_EX_DLGMODALFRAME) ||
1461 ((!(cs->dwExStyle & WS_EX_STATICEDGE)) &&
1462 (cs->style & (WS_DLGFRAME | WS_THICKFRAME))))
1463 cs->dwExStyle |= WS_EX_WINDOWEDGE;
1464 else
1465 cs->dwExStyle &= ~WS_EX_WINDOWEDGE;
1467 /* Create the window structure */
1469 if (!(wndPtr = create_window_handle( parent, owner, className, module, unicode )))
1471 WNDCLASSW wc;
1472 /* if it's a comctl32 class, GetClassInfo will load it, then we can retry */
1473 if (GetLastError() != ERROR_INVALID_HANDLE ||
1474 !GetClassInfoW( 0, className, &wc ) ||
1475 !(wndPtr = create_window_handle( parent, owner, className, module, unicode )))
1476 return 0;
1478 hwnd = wndPtr->obj.handle;
1480 /* Fill the window structure */
1482 wndPtr->tid = GetCurrentThreadId();
1483 wndPtr->hInstance = cs->hInstance;
1484 wndPtr->text = NULL;
1485 wndPtr->dwStyle = cs->style & ~WS_VISIBLE;
1486 wndPtr->dwExStyle = cs->dwExStyle;
1487 wndPtr->wIDmenu = 0;
1488 wndPtr->helpContext = 0;
1489 wndPtr->pScroll = NULL;
1490 wndPtr->userdata = 0;
1491 wndPtr->hIcon = 0;
1492 wndPtr->hIconSmall = 0;
1493 wndPtr->hIconSmall2 = 0;
1494 wndPtr->hSysMenu = 0;
1496 wndPtr->min_pos.x = wndPtr->min_pos.y = -1;
1497 wndPtr->max_pos.x = wndPtr->max_pos.y = -1;
1498 SetRect( &wndPtr->normal_rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
1500 if (wndPtr->dwStyle & WS_SYSMENU) SetSystemMenu( hwnd, 0 );
1503 * Correct the window styles.
1505 * It affects only the style loaded into the WIN structure.
1508 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
1510 wndPtr->dwStyle |= WS_CLIPSIBLINGS;
1511 if (!(wndPtr->dwStyle & WS_POPUP))
1512 wndPtr->dwStyle |= WS_CAPTION;
1515 /* WS_EX_WINDOWEDGE depends on some other styles */
1516 if (wndPtr->dwExStyle & WS_EX_DLGMODALFRAME)
1517 wndPtr->dwExStyle |= WS_EX_WINDOWEDGE;
1518 else if (wndPtr->dwStyle & (WS_DLGFRAME | WS_THICKFRAME))
1520 if (!((wndPtr->dwExStyle & WS_EX_STATICEDGE) &&
1521 (wndPtr->dwStyle & (WS_CHILD | WS_POPUP))))
1522 wndPtr->dwExStyle |= WS_EX_WINDOWEDGE;
1524 else
1525 wndPtr->dwExStyle &= ~WS_EX_WINDOWEDGE;
1527 if (!(wndPtr->dwStyle & (WS_CHILD | WS_POPUP)))
1528 wndPtr->flags |= WIN_NEED_SIZE;
1530 SERVER_START_REQ( set_window_info )
1532 req->handle = wine_server_user_handle( hwnd );
1533 req->flags = SET_WIN_STYLE | SET_WIN_EXSTYLE | SET_WIN_INSTANCE | SET_WIN_UNICODE;
1534 req->style = wndPtr->dwStyle;
1535 req->ex_style = wndPtr->dwExStyle;
1536 req->instance = wine_server_client_ptr( wndPtr->hInstance );
1537 req->is_unicode = (wndPtr->flags & WIN_ISUNICODE) != 0;
1538 req->extra_offset = -1;
1539 wine_server_call( req );
1541 SERVER_END_REQ;
1543 /* Set the window menu */
1545 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
1547 if (cs->hMenu)
1549 if (!MENU_SetMenu(hwnd, cs->hMenu))
1551 WIN_ReleasePtr( wndPtr );
1552 free_window_handle( hwnd );
1553 return 0;
1556 else
1558 LPCWSTR menuName = (LPCWSTR)GetClassLongPtrW( hwnd, GCLP_MENUNAME );
1559 if (menuName)
1561 cs->hMenu = LoadMenuW( cs->hInstance, menuName );
1562 if (cs->hMenu) MENU_SetMenu( hwnd, cs->hMenu );
1566 else SetWindowLongPtrW( hwnd, GWLP_ID, (ULONG_PTR)cs->hMenu );
1568 /* call the WH_CBT hook */
1570 /* the window style passed to the hook must be the real window style,
1571 * rather than just the window style that the caller to CreateWindowEx
1572 * passed in, so we have to copy the original CREATESTRUCT and get the
1573 * the real style. */
1574 cbcs = *cs;
1575 cbcs.style = wndPtr->dwStyle;
1576 cbtc.lpcs = &cbcs;
1577 cbtc.hwndInsertAfter = HWND_TOP;
1578 WIN_ReleasePtr( wndPtr );
1579 if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode )) goto failed;
1581 /* send the WM_GETMINMAXINFO message and fix the size if needed */
1583 cx = cs->cx;
1584 cy = cs->cy;
1585 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
1587 POINT maxSize, maxPos, minTrack, maxTrack;
1588 WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
1589 if (maxTrack.x < cx) cx = maxTrack.x;
1590 if (maxTrack.y < cy) cy = maxTrack.y;
1591 if (minTrack.x > cx) cx = minTrack.x;
1592 if (minTrack.y > cy) cy = minTrack.y;
1595 if (cx < 0) cx = 0;
1596 if (cy < 0) cy = 0;
1597 SetRect( &rect, cs->x, cs->y, cs->x + cx, cs->y + cy );
1598 /* check for wraparound */
1599 if (cs->x + cx < cs->x) rect.right = 0x7fffffff;
1600 if (cs->y + cy < cs->y) rect.bottom = 0x7fffffff;
1601 if (!set_window_pos( hwnd, 0, SWP_NOZORDER | SWP_NOACTIVATE, &rect, &rect, NULL )) goto failed;
1603 /* send WM_NCCREATE */
1605 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cx, cy );
1606 if (unicode)
1607 result = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1608 else
1609 result = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1610 if (!result)
1612 WARN( "%p: aborted by WM_NCCREATE\n", hwnd );
1613 goto failed;
1616 /* create default IME window */
1618 if (imm_register_window && !is_desktop_window( hwnd ) &&
1619 parent != get_hwnd_message_parent() && imm_register_window( hwnd ))
1621 TRACE("register IME window for %p\n", hwnd);
1622 win_set_flags( hwnd, WIN_HAS_IME_WIN, 0 );
1625 /* send WM_NCCALCSIZE */
1627 if (WIN_GetRectangles( hwnd, COORDS_PARENT, &rect, NULL ))
1629 /* yes, even if the CBT hook was called with HWND_TOP */
1630 HWND insert_after = (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
1631 RECT client_rect = rect;
1633 /* the rectangle is in screen coords for WM_NCCALCSIZE when wparam is FALSE */
1634 MapWindowPoints( parent, 0, (POINT *)&client_rect, 2 );
1635 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&client_rect );
1636 MapWindowPoints( 0, parent, (POINT *)&client_rect, 2 );
1637 set_window_pos( hwnd, insert_after, SWP_NOACTIVATE, &rect, &client_rect, NULL );
1639 else return 0;
1641 /* send WM_CREATE */
1643 if (unicode)
1644 result = SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs );
1645 else
1646 result = SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs );
1647 if (result == -1) goto failed;
1649 /* call the driver */
1651 if (!USER_Driver->pCreateWindow( hwnd )) goto failed;
1653 NotifyWinEvent(EVENT_OBJECT_CREATE, hwnd, OBJID_WINDOW, 0);
1655 /* send the size messages */
1657 if (!(win_get_flags( hwnd ) & WIN_NEED_SIZE))
1659 WIN_GetRectangles( hwnd, COORDS_PARENT, NULL, &rect );
1660 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1661 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1662 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( rect.left, rect.top ) );
1665 /* Show the window, maximizing or minimizing if needed */
1667 style = WIN_SetStyle( hwnd, 0, WS_MAXIMIZE | WS_MINIMIZE );
1668 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1670 RECT newPos;
1671 UINT swFlag = (style & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1673 swFlag = WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1674 swFlag |= SWP_FRAMECHANGED; /* Frame always gets changed */
1675 if (!(style & WS_VISIBLE) || (style & WS_CHILD) || GetActiveWindow()) swFlag |= SWP_NOACTIVATE;
1676 SetWindowPos( hwnd, 0, newPos.left, newPos.top, newPos.right - newPos.left,
1677 newPos.bottom - newPos.top, swFlag );
1680 /* Notify the parent window only */
1682 send_parent_notify( hwnd, WM_CREATE );
1683 if (!IsWindow( hwnd )) return 0;
1685 if (parent == GetDesktopWindow())
1686 PostMessageW( parent, WM_PARENTNOTIFY, WM_CREATE, (LPARAM)hwnd );
1688 if (cs->style & WS_VISIBLE)
1690 if (cs->style & WS_MAXIMIZE)
1691 sw = SW_SHOW;
1692 else if (cs->style & WS_MINIMIZE)
1693 sw = SW_SHOWMINIMIZED;
1695 ShowWindow( hwnd, sw );
1696 if (cs->dwExStyle & WS_EX_MDICHILD)
1698 SendMessageW(cs->hwndParent, WM_MDIREFRESHMENU, 0, 0);
1699 /* ShowWindow won't activate child windows */
1700 SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE );
1704 /* Call WH_SHELL hook */
1706 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) && !GetWindow( hwnd, GW_OWNER ))
1707 HOOK_CallHooks( WH_SHELL, HSHELL_WINDOWCREATED, (WPARAM)hwnd, 0, TRUE );
1709 TRACE("created window %p\n", hwnd);
1710 return hwnd;
1712 failed:
1713 WIN_DestroyWindow( hwnd );
1714 return 0;
1718 /***********************************************************************
1719 * CreateWindowExA (USER32.@)
1721 HWND WINAPI DECLSPEC_HOTPATCH CreateWindowExA( DWORD exStyle, LPCSTR className,
1722 LPCSTR windowName, DWORD style, INT x,
1723 INT y, INT width, INT height,
1724 HWND parent, HMENU menu,
1725 HINSTANCE instance, LPVOID data )
1727 CREATESTRUCTA cs;
1729 cs.lpCreateParams = data;
1730 cs.hInstance = instance;
1731 cs.hMenu = menu;
1732 cs.hwndParent = parent;
1733 cs.x = x;
1734 cs.y = y;
1735 cs.cx = width;
1736 cs.cy = height;
1737 cs.style = style;
1738 cs.lpszName = windowName;
1739 cs.lpszClass = className;
1740 cs.dwExStyle = exStyle;
1742 if (!IS_INTRESOURCE(className))
1744 WCHAR bufferW[256];
1745 if (!MultiByteToWideChar( CP_ACP, 0, className, -1, bufferW, sizeof(bufferW)/sizeof(WCHAR) ))
1746 return 0;
1747 return wow_handlers.create_window( (CREATESTRUCTW *)&cs, bufferW, instance, FALSE );
1749 /* Note: we rely on the fact that CREATESTRUCTA and */
1750 /* CREATESTRUCTW have the same layout. */
1751 return wow_handlers.create_window( (CREATESTRUCTW *)&cs, (LPCWSTR)className, instance, FALSE );
1755 /***********************************************************************
1756 * CreateWindowExW (USER32.@)
1758 HWND WINAPI DECLSPEC_HOTPATCH CreateWindowExW( DWORD exStyle, LPCWSTR className,
1759 LPCWSTR windowName, DWORD style, INT x,
1760 INT y, INT width, INT height,
1761 HWND parent, HMENU menu,
1762 HINSTANCE instance, LPVOID data )
1764 CREATESTRUCTW cs;
1766 cs.lpCreateParams = data;
1767 cs.hInstance = instance;
1768 cs.hMenu = menu;
1769 cs.hwndParent = parent;
1770 cs.x = x;
1771 cs.y = y;
1772 cs.cx = width;
1773 cs.cy = height;
1774 cs.style = style;
1775 cs.lpszName = windowName;
1776 cs.lpszClass = className;
1777 cs.dwExStyle = exStyle;
1779 return wow_handlers.create_window( &cs, className, instance, TRUE );
1783 /***********************************************************************
1784 * WIN_SendDestroyMsg
1786 static void WIN_SendDestroyMsg( HWND hwnd )
1788 GUITHREADINFO info;
1790 info.cbSize = sizeof(info);
1791 if (GetGUIThreadInfo( GetCurrentThreadId(), &info ))
1793 if (hwnd == info.hwndCaret) DestroyCaret();
1794 if (hwnd == info.hwndActive) WINPOS_ActivateOtherWindow( hwnd );
1797 if (hwnd == GetClipboardOwner()) CLIPBOARD_ReleaseOwner( hwnd );
1800 * Send the WM_DESTROY to the window.
1802 SendMessageW( hwnd, WM_DESTROY, 0, 0);
1805 * This WM_DESTROY message can trigger re-entrant calls to DestroyWindow
1806 * make sure that the window still exists when we come back.
1808 if (IsWindow(hwnd))
1810 HWND* pWndArray;
1811 int i;
1813 if (!(pWndArray = WIN_ListChildren( hwnd ))) return;
1815 for (i = 0; pWndArray[i]; i++)
1817 if (IsWindow( pWndArray[i] )) WIN_SendDestroyMsg( pWndArray[i] );
1819 HeapFree( GetProcessHeap(), 0, pWndArray );
1821 else
1822 WARN("\tdestroyed itself while in WM_DESTROY!\n");
1826 /***********************************************************************
1827 * DestroyWindow (USER32.@)
1829 BOOL WINAPI DestroyWindow( HWND hwnd )
1831 BOOL is_child;
1833 if (!(hwnd = WIN_IsCurrentThread( hwnd )) || is_desktop_window( hwnd ))
1835 SetLastError( ERROR_ACCESS_DENIED );
1836 return FALSE;
1839 TRACE("(%p)\n", hwnd);
1841 /* Call hooks */
1843 if (HOOK_CallHooks( WH_CBT, HCBT_DESTROYWND, (WPARAM)hwnd, 0, TRUE )) return FALSE;
1845 if (MENU_IsMenuActive() == hwnd)
1846 EndMenu();
1848 is_child = (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) != 0;
1850 if (is_child)
1852 if (!USER_IsExitingThread( GetCurrentThreadId() ))
1853 send_parent_notify( hwnd, WM_DESTROY );
1855 else if (!GetWindow( hwnd, GW_OWNER ))
1857 HOOK_CallHooks( WH_SHELL, HSHELL_WINDOWDESTROYED, (WPARAM)hwnd, 0L, TRUE );
1858 /* FIXME: clean up palette - see "Internals" p.352 */
1861 if (!IsWindow(hwnd)) return TRUE;
1863 /* Hide the window */
1864 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)
1866 /* Only child windows receive WM_SHOWWINDOW in DestroyWindow() */
1867 if (is_child)
1868 ShowWindow( hwnd, SW_HIDE );
1869 else
1870 SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE |
1871 SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW );
1874 if (!IsWindow(hwnd)) return TRUE;
1876 /* Recursively destroy owned windows */
1878 if (!is_child)
1880 for (;;)
1882 int i;
1883 BOOL got_one = FALSE;
1884 HWND *list = WIN_ListChildren( GetDesktopWindow() );
1885 if (list)
1887 for (i = 0; list[i]; i++)
1889 if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
1890 if (WIN_IsCurrentThread( list[i] ))
1892 DestroyWindow( list[i] );
1893 got_one = TRUE;
1894 continue;
1896 WIN_SetOwner( list[i], 0 );
1898 HeapFree( GetProcessHeap(), 0, list );
1900 if (!got_one) break;
1904 /* Send destroy messages */
1906 WIN_SendDestroyMsg( hwnd );
1907 if (!IsWindow( hwnd )) return TRUE;
1909 /* Destroy the window storage */
1911 WIN_DestroyWindow( hwnd );
1912 return TRUE;
1916 /***********************************************************************
1917 * CloseWindow (USER32.@)
1919 BOOL WINAPI CloseWindow( HWND hwnd )
1921 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) return FALSE;
1922 ShowWindow( hwnd, SW_MINIMIZE );
1923 return TRUE;
1927 /***********************************************************************
1928 * OpenIcon (USER32.@)
1930 BOOL WINAPI OpenIcon( HWND hwnd )
1932 if (!IsIconic( hwnd )) return FALSE;
1933 ShowWindow( hwnd, SW_SHOWNORMAL );
1934 return TRUE;
1938 /***********************************************************************
1939 * FindWindowExW (USER32.@)
1941 HWND WINAPI FindWindowExW( HWND parent, HWND child, LPCWSTR className, LPCWSTR title )
1943 HWND *list;
1944 HWND retvalue = 0;
1945 int i = 0, len = 0;
1946 WCHAR *buffer = NULL;
1948 if (!parent && child) parent = GetDesktopWindow();
1949 else if (parent == HWND_MESSAGE) parent = get_hwnd_message_parent();
1951 if (title)
1953 len = strlenW(title) + 1; /* one extra char to check for chars beyond the end */
1954 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return 0;
1957 if (!(list = list_window_children( 0, parent, className, 0 ))) goto done;
1959 if (child)
1961 child = WIN_GetFullHandle( child );
1962 while (list[i] && list[i] != child) i++;
1963 if (!list[i]) goto done;
1964 i++; /* start from next window */
1967 if (title)
1969 while (list[i])
1971 if (InternalGetWindowText( list[i], buffer, len + 1 ))
1973 if (!strcmpiW( buffer, title )) break;
1975 else
1977 if (!title[0]) break;
1979 i++;
1982 retvalue = list[i];
1984 done:
1985 HeapFree( GetProcessHeap(), 0, list );
1986 HeapFree( GetProcessHeap(), 0, buffer );
1987 return retvalue;
1992 /***********************************************************************
1993 * FindWindowA (USER32.@)
1995 HWND WINAPI FindWindowA( LPCSTR className, LPCSTR title )
1997 HWND ret = FindWindowExA( 0, 0, className, title );
1998 if (!ret) SetLastError (ERROR_CANNOT_FIND_WND_CLASS);
1999 return ret;
2003 /***********************************************************************
2004 * FindWindowExA (USER32.@)
2006 HWND WINAPI FindWindowExA( HWND parent, HWND child, LPCSTR className, LPCSTR title )
2008 LPWSTR titleW = NULL;
2009 HWND hwnd = 0;
2011 if (title)
2013 DWORD len = MultiByteToWideChar( CP_ACP, 0, title, -1, NULL, 0 );
2014 if (!(titleW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return 0;
2015 MultiByteToWideChar( CP_ACP, 0, title, -1, titleW, len );
2018 if (!IS_INTRESOURCE(className))
2020 WCHAR classW[256];
2021 if (MultiByteToWideChar( CP_ACP, 0, className, -1, classW, sizeof(classW)/sizeof(WCHAR) ))
2022 hwnd = FindWindowExW( parent, child, classW, titleW );
2024 else
2026 hwnd = FindWindowExW( parent, child, (LPCWSTR)className, titleW );
2029 HeapFree( GetProcessHeap(), 0, titleW );
2030 return hwnd;
2034 /***********************************************************************
2035 * FindWindowW (USER32.@)
2037 HWND WINAPI FindWindowW( LPCWSTR className, LPCWSTR title )
2039 return FindWindowExW( 0, 0, className, title );
2043 /**********************************************************************
2044 * GetDesktopWindow (USER32.@)
2046 HWND WINAPI GetDesktopWindow(void)
2048 struct user_thread_info *thread_info = get_user_thread_info();
2050 if (thread_info->top_window) return thread_info->top_window;
2052 SERVER_START_REQ( get_desktop_window )
2054 req->force = 0;
2055 if (!wine_server_call( req ))
2057 thread_info->top_window = wine_server_ptr_handle( reply->top_window );
2058 thread_info->msg_window = wine_server_ptr_handle( reply->msg_window );
2061 SERVER_END_REQ;
2063 if (!thread_info->top_window)
2065 static const WCHAR explorer[] = {'\\','e','x','p','l','o','r','e','r','.','e','x','e',0};
2066 static const WCHAR args[] = {' ','/','d','e','s','k','t','o','p',0};
2067 STARTUPINFOW si;
2068 PROCESS_INFORMATION pi;
2069 WCHAR windir[MAX_PATH];
2070 WCHAR app[MAX_PATH + sizeof(explorer)/sizeof(WCHAR)];
2071 WCHAR cmdline[MAX_PATH + (sizeof(explorer) + sizeof(args))/sizeof(WCHAR)];
2072 WCHAR desktop[MAX_PATH];
2073 void *redir;
2075 SERVER_START_REQ( set_user_object_info )
2077 req->handle = wine_server_obj_handle( GetThreadDesktop(GetCurrentThreadId()) );
2078 req->flags = SET_USER_OBJECT_GET_FULL_NAME;
2079 wine_server_set_reply( req, desktop, sizeof(desktop) - sizeof(WCHAR) );
2080 if (!wine_server_call( req ))
2082 size_t size = wine_server_reply_size( reply );
2083 desktop[size / sizeof(WCHAR)] = 0;
2084 TRACE( "starting explorer for desktop %s\n", debugstr_w(desktop) );
2086 else
2087 desktop[0] = 0;
2089 SERVER_END_REQ;
2091 memset( &si, 0, sizeof(si) );
2092 si.cb = sizeof(si);
2093 si.lpDesktop = *desktop ? desktop : NULL;
2094 si.dwFlags = STARTF_USESTDHANDLES;
2095 si.hStdInput = 0;
2096 si.hStdOutput = 0;
2097 si.hStdError = GetStdHandle( STD_ERROR_HANDLE );
2099 GetSystemDirectoryW( windir, MAX_PATH );
2100 strcpyW( app, windir );
2101 strcatW( app, explorer );
2102 strcpyW( cmdline, app );
2103 strcatW( cmdline, args );
2105 Wow64DisableWow64FsRedirection( &redir );
2106 if (CreateProcessW( app, cmdline, NULL, NULL, FALSE, DETACHED_PROCESS,
2107 NULL, windir, &si, &pi ))
2109 TRACE( "started explorer pid %04x tid %04x\n", pi.dwProcessId, pi.dwThreadId );
2110 WaitForInputIdle( pi.hProcess, 10000 );
2111 CloseHandle( pi.hThread );
2112 CloseHandle( pi.hProcess );
2114 else WARN( "failed to start explorer, err %d\n", GetLastError() );
2115 Wow64RevertWow64FsRedirection( redir );
2117 SERVER_START_REQ( get_desktop_window )
2119 req->force = 1;
2120 if (!wine_server_call( req ))
2122 thread_info->top_window = wine_server_ptr_handle( reply->top_window );
2123 thread_info->msg_window = wine_server_ptr_handle( reply->msg_window );
2126 SERVER_END_REQ;
2129 if (!thread_info->top_window || !USER_Driver->pCreateDesktopWindow( thread_info->top_window ))
2130 ERR( "failed to create desktop window\n" );
2132 return thread_info->top_window;
2136 /*******************************************************************
2137 * EnableWindow (USER32.@)
2139 BOOL WINAPI EnableWindow( HWND hwnd, BOOL enable )
2141 BOOL retvalue;
2143 if (is_broadcast(hwnd))
2145 SetLastError( ERROR_INVALID_PARAMETER );
2146 return FALSE;
2149 TRACE("( %p, %d )\n", hwnd, enable);
2151 if (enable)
2153 retvalue = (WIN_SetStyle( hwnd, 0, WS_DISABLED ) & WS_DISABLED) != 0;
2154 if (retvalue) SendMessageW( hwnd, WM_ENABLE, TRUE, 0 );
2156 else
2158 SendMessageW( hwnd, WM_CANCELMODE, 0, 0 );
2160 retvalue = (WIN_SetStyle( hwnd, WS_DISABLED, 0 ) & WS_DISABLED) != 0;
2161 if (!retvalue)
2163 if (hwnd == GetFocus())
2164 SetFocus( 0 ); /* A disabled window can't have the focus */
2166 SendMessageW( hwnd, WM_ENABLE, FALSE, 0 );
2169 return retvalue;
2173 /***********************************************************************
2174 * IsWindowEnabled (USER32.@)
2176 BOOL WINAPI IsWindowEnabled(HWND hWnd)
2178 return !(GetWindowLongW( hWnd, GWL_STYLE ) & WS_DISABLED);
2182 /***********************************************************************
2183 * IsWindowUnicode (USER32.@)
2185 BOOL WINAPI IsWindowUnicode( HWND hwnd )
2187 WND * wndPtr;
2188 BOOL retvalue = FALSE;
2190 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
2192 if (wndPtr == WND_DESKTOP) return TRUE;
2194 if (wndPtr != WND_OTHER_PROCESS)
2196 retvalue = (wndPtr->flags & WIN_ISUNICODE) != 0;
2197 WIN_ReleasePtr( wndPtr );
2199 else
2201 SERVER_START_REQ( get_window_info )
2203 req->handle = wine_server_user_handle( hwnd );
2204 if (!wine_server_call_err( req )) retvalue = reply->is_unicode;
2206 SERVER_END_REQ;
2208 return retvalue;
2212 /**********************************************************************
2213 * WIN_GetWindowLong
2215 * Helper function for GetWindowLong().
2217 static LONG_PTR WIN_GetWindowLong( HWND hwnd, INT offset, UINT size, BOOL unicode )
2219 LONG_PTR retvalue = 0;
2220 WND *wndPtr;
2222 if (offset == GWLP_HWNDPARENT)
2224 HWND parent = GetAncestor( hwnd, GA_PARENT );
2225 if (parent == GetDesktopWindow()) parent = GetWindow( hwnd, GW_OWNER );
2226 return (ULONG_PTR)parent;
2229 if (!(wndPtr = WIN_GetPtr( hwnd )))
2231 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2232 return 0;
2235 if (wndPtr == WND_DESKTOP)
2237 switch (offset)
2239 case GWL_STYLE:
2240 retvalue = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN; /* message parent is not visible */
2241 if (WIN_GetFullHandle( hwnd ) == GetDesktopWindow())
2242 retvalue |= WS_VISIBLE;
2243 return retvalue;
2244 case GWL_EXSTYLE:
2245 case GWLP_USERDATA:
2246 case GWLP_ID:
2247 case GWLP_HINSTANCE:
2248 return 0;
2249 case GWLP_WNDPROC:
2250 SetLastError( ERROR_ACCESS_DENIED );
2251 return 0;
2253 SetLastError( ERROR_INVALID_INDEX );
2254 return 0;
2257 if (wndPtr == WND_OTHER_PROCESS)
2259 if (offset == GWLP_WNDPROC)
2261 SetLastError( ERROR_ACCESS_DENIED );
2262 return 0;
2264 SERVER_START_REQ( set_window_info )
2266 req->handle = wine_server_user_handle( hwnd );
2267 req->flags = 0; /* don't set anything, just retrieve */
2268 req->extra_offset = (offset >= 0) ? offset : -1;
2269 req->extra_size = (offset >= 0) ? size : 0;
2270 if (!wine_server_call_err( req ))
2272 switch(offset)
2274 case GWL_STYLE: retvalue = reply->old_style; break;
2275 case GWL_EXSTYLE: retvalue = reply->old_ex_style; break;
2276 case GWLP_ID: retvalue = reply->old_id; break;
2277 case GWLP_HINSTANCE: retvalue = (ULONG_PTR)wine_server_get_ptr( reply->old_instance ); break;
2278 case GWLP_USERDATA: retvalue = reply->old_user_data; break;
2279 default:
2280 if (offset >= 0) retvalue = get_win_data( &reply->old_extra_value, size );
2281 else SetLastError( ERROR_INVALID_INDEX );
2282 break;
2286 SERVER_END_REQ;
2287 return retvalue;
2290 /* now we have a valid wndPtr */
2292 if (offset >= 0)
2294 if (offset > (int)(wndPtr->cbWndExtra - size))
2296 WARN("Invalid offset %d\n", offset );
2297 WIN_ReleasePtr( wndPtr );
2298 SetLastError( ERROR_INVALID_INDEX );
2299 return 0;
2301 retvalue = get_win_data( (char *)wndPtr->wExtra + offset, size );
2303 /* Special case for dialog window procedure */
2304 if ((offset == DWLP_DLGPROC) && (size == sizeof(LONG_PTR)) && wndPtr->dlgInfo)
2305 retvalue = (LONG_PTR)WINPROC_GetProc( (WNDPROC)retvalue, unicode );
2306 WIN_ReleasePtr( wndPtr );
2307 return retvalue;
2310 switch(offset)
2312 case GWLP_USERDATA: retvalue = wndPtr->userdata; break;
2313 case GWL_STYLE: retvalue = wndPtr->dwStyle; break;
2314 case GWL_EXSTYLE: retvalue = wndPtr->dwExStyle; break;
2315 case GWLP_ID: retvalue = wndPtr->wIDmenu; break;
2316 case GWLP_HINSTANCE: retvalue = (ULONG_PTR)wndPtr->hInstance; break;
2317 case GWLP_WNDPROC:
2318 /* This looks like a hack only for the edit control (see tests). This makes these controls
2319 * more tolerant to A/W mismatches. The lack of W->A->W conversion for such a mismatch suggests
2320 * that the hack is in GetWindowLongPtr[AW], not in winprocs.
2322 if (wndPtr->winproc == BUILTIN_WINPROC(WINPROC_EDIT) && (!unicode != !(wndPtr->flags & WIN_ISUNICODE)))
2323 retvalue = (ULONG_PTR)wndPtr->winproc;
2324 else
2325 retvalue = (ULONG_PTR)WINPROC_GetProc( wndPtr->winproc, unicode );
2326 break;
2327 default:
2328 WARN("Unknown offset %d\n", offset );
2329 SetLastError( ERROR_INVALID_INDEX );
2330 break;
2332 WIN_ReleasePtr(wndPtr);
2333 return retvalue;
2337 /**********************************************************************
2338 * WIN_SetWindowLong
2340 * Helper function for SetWindowLong().
2342 * 0 is the failure code. However, in the case of failure SetLastError
2343 * must be set to distinguish between a 0 return value and a failure.
2345 LONG_PTR WIN_SetWindowLong( HWND hwnd, INT offset, UINT size, LONG_PTR newval, BOOL unicode )
2347 STYLESTRUCT style;
2348 BOOL ok, made_visible = FALSE;
2349 LONG_PTR retval = 0;
2350 WND *wndPtr;
2352 TRACE( "%p %d %lx %c\n", hwnd, offset, newval, unicode ? 'W' : 'A' );
2354 if (is_broadcast(hwnd))
2356 SetLastError( ERROR_INVALID_PARAMETER );
2357 return FALSE;
2360 if (!(wndPtr = WIN_GetPtr( hwnd )))
2362 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2363 return 0;
2365 if (wndPtr == WND_DESKTOP)
2367 /* can't change anything on the desktop window */
2368 SetLastError( ERROR_ACCESS_DENIED );
2369 return 0;
2371 if (wndPtr == WND_OTHER_PROCESS)
2373 if (offset == GWLP_WNDPROC)
2375 SetLastError( ERROR_ACCESS_DENIED );
2376 return 0;
2378 if (offset > 32767 || offset < -32767)
2380 SetLastError( ERROR_INVALID_INDEX );
2381 return 0;
2383 return SendMessageW( hwnd, WM_WINE_SETWINDOWLONG, MAKEWPARAM( offset, size ), newval );
2386 /* first some special cases */
2387 switch( offset )
2389 case GWL_STYLE:
2390 style.styleOld = wndPtr->dwStyle;
2391 style.styleNew = newval;
2392 WIN_ReleasePtr( wndPtr );
2393 SendMessageW( hwnd, WM_STYLECHANGING, GWL_STYLE, (LPARAM)&style );
2394 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
2395 newval = style.styleNew;
2396 /* WS_CLIPSIBLINGS can't be reset on top-level windows */
2397 if (wndPtr->parent == GetDesktopWindow()) newval |= WS_CLIPSIBLINGS;
2398 /* WS_MINIMIZE can't be reset */
2399 if (wndPtr->dwStyle & WS_MINIMIZE) newval |= WS_MINIMIZE;
2400 /* FIXME: changing WS_DLGFRAME | WS_THICKFRAME is supposed to change
2401 WS_EX_WINDOWEDGE too */
2402 break;
2403 case GWL_EXSTYLE:
2404 style.styleOld = wndPtr->dwExStyle;
2405 style.styleNew = newval;
2406 WIN_ReleasePtr( wndPtr );
2407 SendMessageW( hwnd, WM_STYLECHANGING, GWL_EXSTYLE, (LPARAM)&style );
2408 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
2409 /* WS_EX_TOPMOST can only be changed through SetWindowPos */
2410 newval = (style.styleNew & ~WS_EX_TOPMOST) | (wndPtr->dwExStyle & WS_EX_TOPMOST);
2411 /* WS_EX_WINDOWEDGE depends on some other styles */
2412 if (newval & WS_EX_DLGMODALFRAME)
2413 newval |= WS_EX_WINDOWEDGE;
2414 else if (!(newval & WS_EX_STATICEDGE) && (wndPtr->dwStyle & (WS_DLGFRAME | WS_THICKFRAME)))
2415 newval |= WS_EX_WINDOWEDGE;
2416 else
2417 newval &= ~WS_EX_WINDOWEDGE;
2418 break;
2419 case GWLP_HWNDPARENT:
2420 if (wndPtr->parent == GetDesktopWindow())
2422 WIN_ReleasePtr( wndPtr );
2423 return (ULONG_PTR)WIN_SetOwner( hwnd, (HWND)newval );
2425 else
2427 WIN_ReleasePtr( wndPtr );
2428 return (ULONG_PTR)SetParent( hwnd, (HWND)newval );
2430 case GWLP_WNDPROC:
2432 WNDPROC proc;
2433 UINT old_flags = wndPtr->flags;
2434 retval = WIN_GetWindowLong( hwnd, offset, size, unicode );
2435 proc = WINPROC_AllocProc( (WNDPROC)newval, unicode );
2436 if (proc) wndPtr->winproc = proc;
2437 if (WINPROC_IsUnicode( proc, unicode )) wndPtr->flags |= WIN_ISUNICODE;
2438 else wndPtr->flags &= ~WIN_ISUNICODE;
2439 if (!((old_flags ^ wndPtr->flags) & WIN_ISUNICODE))
2441 WIN_ReleasePtr( wndPtr );
2442 return retval;
2444 /* update is_unicode flag on the server side */
2445 break;
2447 case GWLP_ID:
2448 case GWLP_HINSTANCE:
2449 case GWLP_USERDATA:
2450 break;
2451 case DWLP_DLGPROC:
2452 if ((wndPtr->cbWndExtra - sizeof(LONG_PTR) >= DWLP_DLGPROC) &&
2453 (size == sizeof(LONG_PTR)) && wndPtr->dlgInfo)
2455 WNDPROC *ptr = (WNDPROC *)((char *)wndPtr->wExtra + DWLP_DLGPROC);
2456 retval = (ULONG_PTR)WINPROC_GetProc( *ptr, unicode );
2457 *ptr = WINPROC_AllocProc( (WNDPROC)newval, unicode );
2458 WIN_ReleasePtr( wndPtr );
2459 return retval;
2461 /* fall through */
2462 default:
2463 if (offset < 0 || offset > (int)(wndPtr->cbWndExtra - size))
2465 WARN("Invalid offset %d\n", offset );
2466 WIN_ReleasePtr( wndPtr );
2467 SetLastError( ERROR_INVALID_INDEX );
2468 return 0;
2470 else if (get_win_data( (char *)wndPtr->wExtra + offset, size ) == newval)
2472 /* already set to the same value */
2473 WIN_ReleasePtr( wndPtr );
2474 return newval;
2476 break;
2479 SERVER_START_REQ( set_window_info )
2481 req->handle = wine_server_user_handle( hwnd );
2482 req->extra_offset = -1;
2483 switch(offset)
2485 case GWL_STYLE:
2486 req->flags = SET_WIN_STYLE;
2487 req->style = newval;
2488 break;
2489 case GWL_EXSTYLE:
2490 req->flags = SET_WIN_EXSTYLE;
2491 req->ex_style = newval;
2492 break;
2493 case GWLP_ID:
2494 req->flags = SET_WIN_ID;
2495 req->id = newval;
2496 break;
2497 case GWLP_HINSTANCE:
2498 req->flags = SET_WIN_INSTANCE;
2499 req->instance = wine_server_client_ptr( (void *)newval );
2500 break;
2501 case GWLP_WNDPROC:
2502 req->flags = SET_WIN_UNICODE;
2503 req->is_unicode = (wndPtr->flags & WIN_ISUNICODE) != 0;
2504 break;
2505 case GWLP_USERDATA:
2506 req->flags = SET_WIN_USERDATA;
2507 req->user_data = newval;
2508 break;
2509 default:
2510 req->flags = SET_WIN_EXTRA;
2511 req->extra_offset = offset;
2512 req->extra_size = size;
2513 set_win_data( &req->extra_value, newval, size );
2515 if ((ok = !wine_server_call_err( req )))
2517 switch(offset)
2519 case GWL_STYLE:
2520 wndPtr->dwStyle = newval;
2521 retval = reply->old_style;
2522 break;
2523 case GWL_EXSTYLE:
2524 wndPtr->dwExStyle = newval;
2525 retval = reply->old_ex_style;
2526 break;
2527 case GWLP_ID:
2528 wndPtr->wIDmenu = newval;
2529 retval = reply->old_id;
2530 break;
2531 case GWLP_HINSTANCE:
2532 wndPtr->hInstance = (HINSTANCE)newval;
2533 retval = (ULONG_PTR)wine_server_get_ptr( reply->old_instance );
2534 break;
2535 case GWLP_WNDPROC:
2536 break;
2537 case GWLP_USERDATA:
2538 wndPtr->userdata = newval;
2539 retval = reply->old_user_data;
2540 break;
2541 default:
2542 retval = get_win_data( (char *)wndPtr->wExtra + offset, size );
2543 set_win_data( (char *)wndPtr->wExtra + offset, newval, size );
2544 break;
2548 SERVER_END_REQ;
2550 if ((offset == GWL_STYLE && ((style.styleOld ^ style.styleNew) & WS_VISIBLE)) ||
2551 (offset == GWL_EXSTYLE && ((style.styleOld ^ style.styleNew) & WS_EX_LAYERED)))
2553 made_visible = (wndPtr->dwStyle & WS_VISIBLE) != 0;
2554 invalidate_dce( wndPtr, NULL );
2556 WIN_ReleasePtr( wndPtr );
2558 if (!ok) return 0;
2560 if (offset == GWL_STYLE || offset == GWL_EXSTYLE)
2562 style.styleOld = retval;
2563 style.styleNew = newval;
2564 USER_Driver->pSetWindowStyle( hwnd, offset, &style );
2565 if (made_visible) update_window_state( hwnd );
2566 SendMessageW( hwnd, WM_STYLECHANGED, offset, (LPARAM)&style );
2569 return retval;
2573 /**********************************************************************
2574 * GetWindowWord (USER32.@)
2576 WORD WINAPI GetWindowWord( HWND hwnd, INT offset )
2578 switch(offset)
2580 case GWLP_ID:
2581 case GWLP_HINSTANCE:
2582 case GWLP_HWNDPARENT:
2583 break;
2584 default:
2585 if (offset < 0)
2587 WARN("Invalid offset %d\n", offset );
2588 SetLastError( ERROR_INVALID_INDEX );
2589 return 0;
2591 break;
2593 return WIN_GetWindowLong( hwnd, offset, sizeof(WORD), FALSE );
2597 /**********************************************************************
2598 * GetWindowLongA (USER32.@)
2600 LONG WINAPI GetWindowLongA( HWND hwnd, INT offset )
2602 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG), FALSE );
2606 /**********************************************************************
2607 * GetWindowLongW (USER32.@)
2609 LONG WINAPI GetWindowLongW( HWND hwnd, INT offset )
2611 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG), TRUE );
2615 /**********************************************************************
2616 * SetWindowWord (USER32.@)
2618 WORD WINAPI SetWindowWord( HWND hwnd, INT offset, WORD newval )
2620 switch(offset)
2622 case GWLP_ID:
2623 case GWLP_HINSTANCE:
2624 case GWLP_HWNDPARENT:
2625 break;
2626 default:
2627 if (offset < 0)
2629 WARN("Invalid offset %d\n", offset );
2630 SetLastError( ERROR_INVALID_INDEX );
2631 return 0;
2633 break;
2635 return WIN_SetWindowLong( hwnd, offset, sizeof(WORD), newval, FALSE );
2639 /**********************************************************************
2640 * SetWindowLongA (USER32.@)
2642 * See SetWindowLongW.
2644 LONG WINAPI DECLSPEC_HOTPATCH SetWindowLongA( HWND hwnd, INT offset, LONG newval )
2646 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG), newval, FALSE );
2650 /**********************************************************************
2651 * SetWindowLongW (USER32.@) Set window attribute
2653 * SetWindowLong() alters one of a window's attributes or sets a 32-bit (long)
2654 * value in a window's extra memory.
2656 * The _hwnd_ parameter specifies the handle to a window that
2657 * has extra memory. The _newval_ parameter contains the new
2658 * attribute or extra memory value. If positive, the _offset_
2659 * parameter is the byte-addressed location in the window's extra
2660 * memory to set. If negative, _offset_ specifies the window
2661 * attribute to set, and should be one of the following values:
2663 * GWL_EXSTYLE The window's extended window style
2665 * GWL_STYLE The window's window style.
2667 * GWLP_WNDPROC Pointer to the window's window procedure.
2669 * GWLP_HINSTANCE The window's application instance handle.
2671 * GWLP_ID The window's identifier.
2673 * GWLP_USERDATA The window's user-specified data.
2675 * If the window is a dialog box, the _offset_ parameter can be one of
2676 * the following values:
2678 * DWLP_DLGPROC The address of the window's dialog box procedure.
2680 * DWLP_MSGRESULT The return value of a message
2681 * that the dialog box procedure processed.
2683 * DWLP_USER Application specific information.
2685 * RETURNS
2687 * If successful, returns the previous value located at _offset_. Otherwise,
2688 * returns 0.
2690 * NOTES
2692 * Extra memory for a window class is specified by a nonzero cbWndExtra
2693 * parameter of the WNDCLASS structure passed to RegisterClass() at the
2694 * time of class creation.
2696 * Using GWL_WNDPROC to set a new window procedure effectively creates
2697 * a window subclass. Use CallWindowProc() in the new windows procedure
2698 * to pass messages to the superclass's window procedure.
2700 * The user data is reserved for use by the application which created
2701 * the window.
2703 * Do not use GWL_STYLE to change the window's WS_DISABLED style;
2704 * instead, call the EnableWindow() function to change the window's
2705 * disabled state.
2707 * Do not use GWL_HWNDPARENT to reset the window's parent, use
2708 * SetParent() instead.
2710 * Win95:
2711 * When offset is GWL_STYLE and the calling app's ver is 4.0,
2712 * it sends WM_STYLECHANGING before changing the settings
2713 * and WM_STYLECHANGED afterwards.
2714 * App ver 4.0 can't use SetWindowLong to change WS_EX_TOPMOST.
2716 LONG WINAPI DECLSPEC_HOTPATCH SetWindowLongW(
2717 HWND hwnd, /* [in] window to alter */
2718 INT offset, /* [in] offset, in bytes, of location to alter */
2719 LONG newval /* [in] new value of location */
2721 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG), newval, TRUE );
2725 /*******************************************************************
2726 * GetWindowTextA (USER32.@)
2728 INT WINAPI GetWindowTextA( HWND hwnd, LPSTR lpString, INT nMaxCount )
2730 WCHAR *buffer;
2732 if (!lpString || nMaxCount <= 0) return 0;
2734 if (WIN_IsCurrentProcess( hwnd ))
2736 lpString[0] = 0;
2737 return (INT)SendMessageA( hwnd, WM_GETTEXT, nMaxCount, (LPARAM)lpString );
2740 /* when window belongs to other process, don't send a message */
2741 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, nMaxCount * sizeof(WCHAR) ))) return 0;
2742 get_server_window_text( hwnd, buffer, nMaxCount );
2743 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, lpString, nMaxCount, NULL, NULL ))
2744 lpString[nMaxCount-1] = 0;
2745 HeapFree( GetProcessHeap(), 0, buffer );
2746 return strlen(lpString);
2750 /*******************************************************************
2751 * InternalGetWindowText (USER32.@)
2753 INT WINAPI InternalGetWindowText(HWND hwnd,LPWSTR lpString,INT nMaxCount )
2755 WND *win;
2757 if (nMaxCount <= 0) return 0;
2758 if (!(win = WIN_GetPtr( hwnd ))) return 0;
2759 if (win == WND_DESKTOP) lpString[0] = 0;
2760 else if (win != WND_OTHER_PROCESS)
2762 if (win->text) lstrcpynW( lpString, win->text, nMaxCount );
2763 else lpString[0] = 0;
2764 WIN_ReleasePtr( win );
2766 else
2768 get_server_window_text( hwnd, lpString, nMaxCount );
2770 return strlenW(lpString);
2774 /*******************************************************************
2775 * GetWindowTextW (USER32.@)
2777 INT WINAPI GetWindowTextW( HWND hwnd, LPWSTR lpString, INT nMaxCount )
2779 if (!lpString || nMaxCount <= 0) return 0;
2781 if (WIN_IsCurrentProcess( hwnd ))
2783 lpString[0] = 0;
2784 return (INT)SendMessageW( hwnd, WM_GETTEXT, nMaxCount, (LPARAM)lpString );
2787 /* when window belongs to other process, don't send a message */
2788 get_server_window_text( hwnd, lpString, nMaxCount );
2789 return strlenW(lpString);
2793 /*******************************************************************
2794 * SetWindowTextA (USER32.@)
2795 * SetWindowText (USER32.@)
2797 BOOL WINAPI DECLSPEC_HOTPATCH SetWindowTextA( HWND hwnd, LPCSTR lpString )
2799 if (is_broadcast(hwnd))
2801 SetLastError( ERROR_INVALID_PARAMETER );
2802 return FALSE;
2804 if (!WIN_IsCurrentProcess( hwnd ))
2805 WARN( "setting text %s of other process window %p should not use SendMessage\n",
2806 debugstr_a(lpString), hwnd );
2807 return (BOOL)SendMessageA( hwnd, WM_SETTEXT, 0, (LPARAM)lpString );
2811 /*******************************************************************
2812 * SetWindowTextW (USER32.@)
2814 BOOL WINAPI DECLSPEC_HOTPATCH SetWindowTextW( HWND hwnd, LPCWSTR lpString )
2816 if (is_broadcast(hwnd))
2818 SetLastError( ERROR_INVALID_PARAMETER );
2819 return FALSE;
2821 if (!WIN_IsCurrentProcess( hwnd ))
2822 WARN( "setting text %s of other process window %p should not use SendMessage\n",
2823 debugstr_w(lpString), hwnd );
2824 return (BOOL)SendMessageW( hwnd, WM_SETTEXT, 0, (LPARAM)lpString );
2828 /*******************************************************************
2829 * GetWindowTextLengthA (USER32.@)
2831 INT WINAPI GetWindowTextLengthA( HWND hwnd )
2833 return SendMessageA( hwnd, WM_GETTEXTLENGTH, 0, 0 );
2836 /*******************************************************************
2837 * GetWindowTextLengthW (USER32.@)
2839 INT WINAPI GetWindowTextLengthW( HWND hwnd )
2841 return SendMessageW( hwnd, WM_GETTEXTLENGTH, 0, 0 );
2845 /*******************************************************************
2846 * IsWindow (USER32.@)
2848 BOOL WINAPI IsWindow( HWND hwnd )
2850 WND *ptr;
2851 BOOL ret;
2853 if (!(ptr = WIN_GetPtr( hwnd ))) return FALSE;
2854 if (ptr == WND_DESKTOP) return TRUE;
2856 if (ptr != WND_OTHER_PROCESS)
2858 WIN_ReleasePtr( ptr );
2859 return TRUE;
2862 /* check other processes */
2863 SERVER_START_REQ( get_window_info )
2865 req->handle = wine_server_user_handle( hwnd );
2866 ret = !wine_server_call_err( req );
2868 SERVER_END_REQ;
2869 return ret;
2873 /***********************************************************************
2874 * GetWindowThreadProcessId (USER32.@)
2876 DWORD WINAPI GetWindowThreadProcessId( HWND hwnd, LPDWORD process )
2878 WND *ptr;
2879 DWORD tid = 0;
2881 if (!(ptr = WIN_GetPtr( hwnd )))
2883 SetLastError( ERROR_INVALID_WINDOW_HANDLE);
2884 return 0;
2887 if (ptr != WND_OTHER_PROCESS && ptr != WND_DESKTOP)
2889 /* got a valid window */
2890 tid = ptr->tid;
2891 if (process) *process = GetCurrentProcessId();
2892 WIN_ReleasePtr( ptr );
2893 return tid;
2896 /* check other processes */
2897 SERVER_START_REQ( get_window_info )
2899 req->handle = wine_server_user_handle( hwnd );
2900 if (!wine_server_call_err( req ))
2902 tid = (DWORD)reply->tid;
2903 if (process) *process = (DWORD)reply->pid;
2906 SERVER_END_REQ;
2907 return tid;
2911 /*****************************************************************
2912 * GetParent (USER32.@)
2914 HWND WINAPI GetParent( HWND hwnd )
2916 WND *wndPtr;
2917 HWND retvalue = 0;
2919 if (!(wndPtr = WIN_GetPtr( hwnd )))
2921 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2922 return 0;
2924 if (wndPtr == WND_DESKTOP) return 0;
2925 if (wndPtr == WND_OTHER_PROCESS)
2927 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
2928 if (style & (WS_POPUP | WS_CHILD))
2930 SERVER_START_REQ( get_window_tree )
2932 req->handle = wine_server_user_handle( hwnd );
2933 if (!wine_server_call_err( req ))
2935 if (style & WS_POPUP) retvalue = wine_server_ptr_handle( reply->owner );
2936 else if (style & WS_CHILD) retvalue = wine_server_ptr_handle( reply->parent );
2939 SERVER_END_REQ;
2942 else
2944 if (wndPtr->dwStyle & WS_POPUP) retvalue = wndPtr->owner;
2945 else if (wndPtr->dwStyle & WS_CHILD) retvalue = wndPtr->parent;
2946 WIN_ReleasePtr( wndPtr );
2948 return retvalue;
2952 /*****************************************************************
2953 * GetAncestor (USER32.@)
2955 HWND WINAPI GetAncestor( HWND hwnd, UINT type )
2957 WND *win;
2958 HWND *list, ret = 0;
2960 switch(type)
2962 case GA_PARENT:
2963 if (!(win = WIN_GetPtr( hwnd )))
2965 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2966 return 0;
2968 if (win == WND_DESKTOP) return 0;
2969 if (win != WND_OTHER_PROCESS)
2971 ret = win->parent;
2972 WIN_ReleasePtr( win );
2974 else /* need to query the server */
2976 SERVER_START_REQ( get_window_tree )
2978 req->handle = wine_server_user_handle( hwnd );
2979 if (!wine_server_call_err( req )) ret = wine_server_ptr_handle( reply->parent );
2981 SERVER_END_REQ;
2983 break;
2985 case GA_ROOT:
2986 if (!(list = list_window_parents( hwnd ))) return 0;
2988 if (!list[0] || !list[1]) ret = WIN_GetFullHandle( hwnd ); /* top-level window */
2989 else
2991 int count = 2;
2992 while (list[count]) count++;
2993 ret = list[count - 2]; /* get the one before the desktop */
2995 HeapFree( GetProcessHeap(), 0, list );
2996 break;
2998 case GA_ROOTOWNER:
2999 if (is_desktop_window( hwnd )) return 0;
3000 ret = WIN_GetFullHandle( hwnd );
3001 for (;;)
3003 HWND parent = GetParent( ret );
3004 if (!parent) break;
3005 ret = parent;
3007 break;
3009 return ret;
3013 /*****************************************************************
3014 * SetParent (USER32.@)
3016 HWND WINAPI SetParent( HWND hwnd, HWND parent )
3018 HWND full_handle;
3019 HWND old_parent = 0;
3020 BOOL was_visible;
3021 WND *wndPtr;
3022 POINT pt;
3023 BOOL ret;
3025 TRACE("(%p %p)\n", hwnd, parent);
3027 if (is_broadcast(hwnd) || is_broadcast(parent))
3029 SetLastError(ERROR_INVALID_PARAMETER);
3030 return 0;
3033 if (!parent) parent = GetDesktopWindow();
3034 else if (parent == HWND_MESSAGE) parent = get_hwnd_message_parent();
3035 else parent = WIN_GetFullHandle( parent );
3037 if (!IsWindow( parent ))
3039 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3040 return 0;
3043 /* Some applications try to set a child as a parent */
3044 if (IsChild(hwnd, parent))
3046 SetLastError( ERROR_INVALID_PARAMETER );
3047 return 0;
3050 if (!(full_handle = WIN_IsCurrentThread( hwnd )))
3051 return (HWND)SendMessageW( hwnd, WM_WINE_SETPARENT, (WPARAM)parent, 0 );
3053 if (full_handle == parent)
3055 SetLastError( ERROR_INVALID_PARAMETER );
3056 return 0;
3059 /* Windows hides the window first, then shows it again
3060 * including the WM_SHOWWINDOW messages and all */
3061 was_visible = ShowWindow( hwnd, SW_HIDE );
3063 wndPtr = WIN_GetPtr( hwnd );
3064 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return 0;
3066 pt.x = wndPtr->rectWindow.left;
3067 pt.y = wndPtr->rectWindow.top;
3069 SERVER_START_REQ( set_parent )
3071 req->handle = wine_server_user_handle( hwnd );
3072 req->parent = wine_server_user_handle( parent );
3073 if ((ret = !wine_server_call( req )))
3075 old_parent = wine_server_ptr_handle( reply->old_parent );
3076 wndPtr->parent = parent = wine_server_ptr_handle( reply->full_parent );
3080 SERVER_END_REQ;
3081 WIN_ReleasePtr( wndPtr );
3082 if (!ret) return 0;
3084 USER_Driver->pSetParent( full_handle, parent, old_parent );
3086 /* SetParent additionally needs to make hwnd the topmost window
3087 in the x-order and send the expected WM_WINDOWPOSCHANGING and
3088 WM_WINDOWPOSCHANGED notification messages.
3090 SetWindowPos( hwnd, HWND_TOP, pt.x, pt.y, 0, 0, SWP_NOSIZE );
3092 if (was_visible) ShowWindow( hwnd, SW_SHOW );
3094 return old_parent;
3098 /*******************************************************************
3099 * IsChild (USER32.@)
3101 BOOL WINAPI IsChild( HWND parent, HWND child )
3103 HWND *list;
3104 int i;
3105 BOOL ret = FALSE;
3107 if (!(GetWindowLongW( child, GWL_STYLE ) & WS_CHILD)) return FALSE;
3108 if (!(list = list_window_parents( child ))) return FALSE;
3109 parent = WIN_GetFullHandle( parent );
3110 for (i = 0; list[i]; i++)
3112 if (list[i] == parent)
3114 ret = list[i] && list[i+1];
3115 break;
3117 if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_CHILD)) break;
3119 HeapFree( GetProcessHeap(), 0, list );
3120 return ret;
3124 /***********************************************************************
3125 * IsWindowVisible (USER32.@)
3127 BOOL WINAPI IsWindowVisible( HWND hwnd )
3129 HWND *list;
3130 BOOL retval = TRUE;
3131 int i;
3133 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)) return FALSE;
3134 if (!(list = list_window_parents( hwnd ))) return TRUE;
3135 if (list[0])
3137 for (i = 0; list[i+1]; i++)
3138 if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_VISIBLE)) break;
3139 retval = !list[i+1] && (list[i] == GetDesktopWindow()); /* top message window isn't visible */
3141 HeapFree( GetProcessHeap(), 0, list );
3142 return retval;
3146 /***********************************************************************
3147 * WIN_IsWindowDrawable
3149 * hwnd is drawable when it is visible, all parents are not
3150 * minimized, and it is itself not minimized unless we are
3151 * trying to draw its default class icon.
3153 BOOL WIN_IsWindowDrawable( HWND hwnd, BOOL icon )
3155 HWND *list;
3156 BOOL retval = TRUE;
3157 int i;
3158 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
3160 if (!(style & WS_VISIBLE)) return FALSE;
3161 if ((style & WS_MINIMIZE) && icon && GetClassLongPtrW( hwnd, GCLP_HICON )) return FALSE;
3163 if (!(list = list_window_parents( hwnd ))) return TRUE;
3164 if (list[0])
3166 for (i = 0; list[i+1]; i++)
3167 if ((GetWindowLongW( list[i], GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != WS_VISIBLE)
3168 break;
3169 retval = !list[i+1] && (list[i] == GetDesktopWindow()); /* top message window isn't visible */
3171 HeapFree( GetProcessHeap(), 0, list );
3172 return retval;
3176 /*******************************************************************
3177 * GetTopWindow (USER32.@)
3179 HWND WINAPI GetTopWindow( HWND hwnd )
3181 if (!hwnd) hwnd = GetDesktopWindow();
3182 return GetWindow( hwnd, GW_CHILD );
3186 /*******************************************************************
3187 * GetWindow (USER32.@)
3189 HWND WINAPI GetWindow( HWND hwnd, UINT rel )
3191 HWND retval = 0;
3193 if (rel == GW_OWNER) /* this one may be available locally */
3195 WND *wndPtr = WIN_GetPtr( hwnd );
3196 if (!wndPtr)
3198 SetLastError( ERROR_INVALID_HANDLE );
3199 return 0;
3201 if (wndPtr == WND_DESKTOP) return 0;
3202 if (wndPtr != WND_OTHER_PROCESS)
3204 retval = wndPtr->owner;
3205 WIN_ReleasePtr( wndPtr );
3206 return retval;
3208 /* else fall through to server call */
3211 SERVER_START_REQ( get_window_tree )
3213 req->handle = wine_server_user_handle( hwnd );
3214 if (!wine_server_call_err( req ))
3216 switch(rel)
3218 case GW_HWNDFIRST:
3219 retval = wine_server_ptr_handle( reply->first_sibling );
3220 break;
3221 case GW_HWNDLAST:
3222 retval = wine_server_ptr_handle( reply->last_sibling );
3223 break;
3224 case GW_HWNDNEXT:
3225 retval = wine_server_ptr_handle( reply->next_sibling );
3226 break;
3227 case GW_HWNDPREV:
3228 retval = wine_server_ptr_handle( reply->prev_sibling );
3229 break;
3230 case GW_OWNER:
3231 retval = wine_server_ptr_handle( reply->owner );
3232 break;
3233 case GW_CHILD:
3234 retval = wine_server_ptr_handle( reply->first_child );
3235 break;
3239 SERVER_END_REQ;
3240 return retval;
3244 /*******************************************************************
3245 * ShowOwnedPopups (USER32.@)
3247 BOOL WINAPI ShowOwnedPopups( HWND owner, BOOL fShow )
3249 int count = 0;
3250 HWND *win_array = WIN_ListChildren( GetDesktopWindow() );
3252 if (!win_array) return TRUE;
3254 while (win_array[count]) count++;
3255 while (--count >= 0)
3257 if (GetWindow( win_array[count], GW_OWNER ) != owner) continue;
3258 if (fShow)
3260 if (win_get_flags( win_array[count] ) & WIN_NEEDS_SHOW_OWNEDPOPUP)
3261 /* In Windows, ShowOwnedPopups(TRUE) generates
3262 * WM_SHOWWINDOW messages with SW_PARENTOPENING,
3263 * regardless of the state of the owner
3265 SendMessageW(win_array[count], WM_SHOWWINDOW, SW_SHOWNORMAL, SW_PARENTOPENING);
3267 else
3269 if (GetWindowLongW( win_array[count], GWL_STYLE ) & WS_VISIBLE)
3270 /* In Windows, ShowOwnedPopups(FALSE) generates
3271 * WM_SHOWWINDOW messages with SW_PARENTCLOSING,
3272 * regardless of the state of the owner
3274 SendMessageW(win_array[count], WM_SHOWWINDOW, SW_HIDE, SW_PARENTCLOSING);
3277 HeapFree( GetProcessHeap(), 0, win_array );
3278 return TRUE;
3282 /*******************************************************************
3283 * GetLastActivePopup (USER32.@)
3285 HWND WINAPI GetLastActivePopup( HWND hwnd )
3287 HWND retval = hwnd;
3289 SERVER_START_REQ( get_window_info )
3291 req->handle = wine_server_user_handle( hwnd );
3292 if (!wine_server_call_err( req )) retval = wine_server_ptr_handle( reply->last_active );
3294 SERVER_END_REQ;
3295 return retval;
3299 /*******************************************************************
3300 * WIN_ListChildren
3302 * Build an array of the children of a given window. The array must be
3303 * freed with HeapFree. Returns NULL when no windows are found.
3305 HWND *WIN_ListChildren( HWND hwnd )
3307 if (!hwnd)
3309 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3310 return NULL;
3312 return list_window_children( 0, hwnd, NULL, 0 );
3316 /*******************************************************************
3317 * EnumWindows (USER32.@)
3319 BOOL WINAPI EnumWindows( WNDENUMPROC lpEnumFunc, LPARAM lParam )
3321 HWND *list;
3322 BOOL ret = TRUE;
3323 int i;
3325 USER_CheckNotLock();
3327 /* We have to build a list of all windows first, to avoid */
3328 /* unpleasant side-effects, for instance if the callback */
3329 /* function changes the Z-order of the windows. */
3331 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return TRUE;
3333 /* Now call the callback function for every window */
3335 for (i = 0; list[i]; i++)
3337 /* Make sure that the window still exists */
3338 if (!IsWindow( list[i] )) continue;
3339 if (!(ret = lpEnumFunc( list[i], lParam ))) break;
3341 HeapFree( GetProcessHeap(), 0, list );
3342 return ret;
3346 /**********************************************************************
3347 * EnumThreadWindows (USER32.@)
3349 BOOL WINAPI EnumThreadWindows( DWORD id, WNDENUMPROC func, LPARAM lParam )
3351 HWND *list;
3352 int i;
3353 BOOL ret = TRUE;
3355 USER_CheckNotLock();
3357 if (!(list = list_window_children( 0, GetDesktopWindow(), NULL, id ))) return TRUE;
3359 /* Now call the callback function for every window */
3361 for (i = 0; list[i]; i++)
3362 if (!(ret = func( list[i], lParam ))) break;
3363 HeapFree( GetProcessHeap(), 0, list );
3364 return ret;
3368 /***********************************************************************
3369 * EnumDesktopWindows (USER32.@)
3371 BOOL WINAPI EnumDesktopWindows( HDESK desktop, WNDENUMPROC func, LPARAM lparam )
3373 HWND *list;
3374 int i;
3376 USER_CheckNotLock();
3378 if (!(list = list_window_children( desktop, 0, NULL, 0 ))) return TRUE;
3380 for (i = 0; list[i]; i++)
3381 if (!func( list[i], lparam )) break;
3382 HeapFree( GetProcessHeap(), 0, list );
3383 return TRUE;
3387 #ifdef __i386__
3388 /* Some apps pass a non-stdcall proc to EnumChildWindows,
3389 * so we need a small assembly wrapper to call the proc.
3391 extern LRESULT enum_callback_wrapper( WNDENUMPROC proc, HWND hwnd, LPARAM lparam );
3392 __ASM_GLOBAL_FUNC( enum_callback_wrapper,
3393 "pushl %ebp\n\t"
3394 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
3395 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
3396 "movl %esp,%ebp\n\t"
3397 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
3398 "pushl 16(%ebp)\n\t"
3399 "pushl 12(%ebp)\n\t"
3400 "call *8(%ebp)\n\t"
3401 "leave\n\t"
3402 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
3403 __ASM_CFI(".cfi_same_value %ebp\n\t")
3404 "ret" )
3405 #else
3406 static inline LRESULT enum_callback_wrapper( WNDENUMPROC proc, HWND hwnd, LPARAM lparam )
3408 return proc( hwnd, lparam );
3410 #endif /* __i386__ */
3412 /**********************************************************************
3413 * WIN_EnumChildWindows
3415 * Helper function for EnumChildWindows().
3417 static BOOL WIN_EnumChildWindows( HWND *list, WNDENUMPROC func, LPARAM lParam )
3419 HWND *childList;
3420 BOOL ret = FALSE;
3422 for ( ; *list; list++)
3424 /* Make sure that the window still exists */
3425 if (!IsWindow( *list )) continue;
3426 /* Build children list first */
3427 childList = WIN_ListChildren( *list );
3429 ret = enum_callback_wrapper( func, *list, lParam );
3431 if (childList)
3433 if (ret) ret = WIN_EnumChildWindows( childList, func, lParam );
3434 HeapFree( GetProcessHeap(), 0, childList );
3436 if (!ret) return FALSE;
3438 return TRUE;
3442 /**********************************************************************
3443 * EnumChildWindows (USER32.@)
3445 BOOL WINAPI EnumChildWindows( HWND parent, WNDENUMPROC func, LPARAM lParam )
3447 HWND *list;
3448 BOOL ret;
3450 USER_CheckNotLock();
3452 if (!(list = WIN_ListChildren( parent ))) return FALSE;
3453 ret = WIN_EnumChildWindows( list, func, lParam );
3454 HeapFree( GetProcessHeap(), 0, list );
3455 return ret;
3459 /*******************************************************************
3460 * AnyPopup (USER32.@)
3462 BOOL WINAPI AnyPopup(void)
3464 int i;
3465 BOOL retvalue;
3466 HWND *list = WIN_ListChildren( GetDesktopWindow() );
3468 if (!list) return FALSE;
3469 for (i = 0; list[i]; i++)
3471 if (IsWindowVisible( list[i] ) && GetWindow( list[i], GW_OWNER )) break;
3473 retvalue = (list[i] != 0);
3474 HeapFree( GetProcessHeap(), 0, list );
3475 return retvalue;
3479 /*******************************************************************
3480 * FlashWindow (USER32.@)
3482 BOOL WINAPI FlashWindow( HWND hWnd, BOOL bInvert )
3484 FLASHWINFO finfo;
3486 finfo.cbSize = sizeof(FLASHWINFO);
3487 finfo.dwFlags = bInvert ? FLASHW_ALL : FLASHW_STOP;
3488 finfo.uCount = 1;
3489 finfo.dwTimeout = 0;
3490 finfo.hwnd = hWnd;
3491 return FlashWindowEx( &finfo );
3494 /*******************************************************************
3495 * FlashWindowEx (USER32.@)
3497 BOOL WINAPI FlashWindowEx( PFLASHWINFO pfinfo )
3499 WND *wndPtr;
3501 TRACE( "%p\n", pfinfo );
3503 if (!pfinfo)
3505 SetLastError( ERROR_NOACCESS );
3506 return FALSE;
3509 if (!pfinfo->hwnd || pfinfo->cbSize != sizeof(FLASHWINFO) || !IsWindow( pfinfo->hwnd ))
3511 SetLastError( ERROR_INVALID_PARAMETER );
3512 return FALSE;
3514 FIXME( "%p - semi-stub\n", pfinfo );
3516 if (IsIconic( pfinfo->hwnd ))
3518 RedrawWindow( pfinfo->hwnd, 0, 0, RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW | RDW_FRAME );
3520 wndPtr = WIN_GetPtr( pfinfo->hwnd );
3521 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
3522 if (pfinfo->dwFlags && !(wndPtr->flags & WIN_NCACTIVATED))
3524 wndPtr->flags |= WIN_NCACTIVATED;
3526 else
3528 wndPtr->flags &= ~WIN_NCACTIVATED;
3530 WIN_ReleasePtr( wndPtr );
3531 USER_Driver->pFlashWindowEx( pfinfo );
3532 return TRUE;
3534 else
3536 WPARAM wparam;
3537 HWND hwnd = pfinfo->hwnd;
3539 wndPtr = WIN_GetPtr( hwnd );
3540 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
3541 hwnd = wndPtr->obj.handle; /* make it a full handle */
3543 if (pfinfo->dwFlags) wparam = !(wndPtr->flags & WIN_NCACTIVATED);
3544 else wparam = (hwnd == GetForegroundWindow());
3546 WIN_ReleasePtr( wndPtr );
3547 SendMessageW( hwnd, WM_NCACTIVATE, wparam, 0 );
3548 USER_Driver->pFlashWindowEx( pfinfo );
3549 return wparam;
3553 /*******************************************************************
3554 * GetWindowContextHelpId (USER32.@)
3556 DWORD WINAPI GetWindowContextHelpId( HWND hwnd )
3558 DWORD retval;
3559 WND *wnd = WIN_GetPtr( hwnd );
3560 if (!wnd || wnd == WND_DESKTOP) return 0;
3561 if (wnd == WND_OTHER_PROCESS)
3563 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
3564 return 0;
3566 retval = wnd->helpContext;
3567 WIN_ReleasePtr( wnd );
3568 return retval;
3572 /*******************************************************************
3573 * SetWindowContextHelpId (USER32.@)
3575 BOOL WINAPI SetWindowContextHelpId( HWND hwnd, DWORD id )
3577 WND *wnd = WIN_GetPtr( hwnd );
3578 if (!wnd || wnd == WND_DESKTOP) return FALSE;
3579 if (wnd == WND_OTHER_PROCESS)
3581 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
3582 return FALSE;
3584 wnd->helpContext = id;
3585 WIN_ReleasePtr( wnd );
3586 return TRUE;
3590 /*******************************************************************
3591 * DragDetect (USER32.@)
3593 BOOL WINAPI DragDetect( HWND hWnd, POINT pt )
3595 MSG msg;
3596 RECT rect;
3597 WORD wDragWidth = GetSystemMetrics(SM_CXDRAG);
3598 WORD wDragHeight= GetSystemMetrics(SM_CYDRAG);
3600 SetRect(&rect, pt.x - wDragWidth, pt.y - wDragHeight, pt.x + wDragWidth, pt.y + wDragHeight);
3602 SetCapture(hWnd);
3604 while(1)
3606 while (PeekMessageW( &msg, 0, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE ))
3608 if( msg.message == WM_LBUTTONUP )
3610 ReleaseCapture();
3611 return FALSE;
3613 if( msg.message == WM_MOUSEMOVE )
3615 POINT tmp;
3616 tmp.x = (short)LOWORD(msg.lParam);
3617 tmp.y = (short)HIWORD(msg.lParam);
3618 if( !PtInRect( &rect, tmp ))
3620 ReleaseCapture();
3621 return TRUE;
3625 WaitMessage();
3627 return FALSE;
3630 /******************************************************************************
3631 * GetWindowModuleFileNameA (USER32.@)
3633 UINT WINAPI GetWindowModuleFileNameA( HWND hwnd, LPSTR module, UINT size )
3635 WND *win;
3636 HINSTANCE hinst;
3638 TRACE( "%p, %p, %u\n", hwnd, module, size );
3640 win = WIN_GetPtr( hwnd );
3641 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP)
3643 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3644 return 0;
3646 hinst = win->hInstance;
3647 WIN_ReleasePtr( win );
3649 return GetModuleFileNameA( hinst, module, size );
3652 /******************************************************************************
3653 * GetWindowModuleFileNameW (USER32.@)
3655 UINT WINAPI GetWindowModuleFileNameW( HWND hwnd, LPWSTR module, UINT size )
3657 WND *win;
3658 HINSTANCE hinst;
3660 TRACE( "%p, %p, %u\n", hwnd, module, size );
3662 win = WIN_GetPtr( hwnd );
3663 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP)
3665 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3666 return 0;
3668 hinst = win->hInstance;
3669 WIN_ReleasePtr( win );
3671 return GetModuleFileNameW( hinst, module, size );
3674 /******************************************************************************
3675 * GetWindowInfo (USER32.@)
3677 * Note: tests show that Windows doesn't check cbSize of the structure.
3679 BOOL WINAPI DECLSPEC_HOTPATCH GetWindowInfo( HWND hwnd, PWINDOWINFO pwi)
3681 if (!pwi) return FALSE;
3682 if (!WIN_GetRectangles( hwnd, COORDS_SCREEN, &pwi->rcWindow, &pwi->rcClient )) return FALSE;
3684 pwi->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
3685 pwi->dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
3686 pwi->dwWindowStatus = ((GetActiveWindow() == hwnd) ? WS_ACTIVECAPTION : 0);
3688 pwi->cxWindowBorders = pwi->rcClient.left - pwi->rcWindow.left;
3689 pwi->cyWindowBorders = pwi->rcWindow.bottom - pwi->rcClient.bottom;
3691 pwi->atomWindowType = GetClassLongW( hwnd, GCW_ATOM );
3692 pwi->wCreatorVersion = 0x0400;
3694 return TRUE;
3697 /******************************************************************************
3698 * SwitchDesktop (USER32.@)
3700 * NOTES: Sets the current input or interactive desktop.
3702 BOOL WINAPI SwitchDesktop( HDESK hDesktop)
3704 FIXME("(hwnd %p) stub!\n", hDesktop);
3705 return TRUE;
3709 /***********************************************************************
3710 * __wine_set_pixel_format
3712 BOOL CDECL __wine_set_pixel_format( HWND hwnd, int format )
3714 WND *win = WIN_GetPtr( hwnd );
3716 if (!win || win == WND_DESKTOP || win == WND_OTHER_PROCESS)
3718 WARN( "setting format %d on win %p not supported\n", format, hwnd );
3719 return FALSE;
3721 win->pixel_format = format;
3722 WIN_ReleasePtr( win );
3724 update_window_state( hwnd );
3725 return TRUE;
3729 /*****************************************************************************
3730 * SetLayeredWindowAttributes (USER32.@)
3732 BOOL WINAPI SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
3734 BOOL ret;
3736 TRACE("(%p,%08x,%d,%x)\n", hwnd, key, alpha, flags);
3738 SERVER_START_REQ( set_window_layered_info )
3740 req->handle = wine_server_user_handle( hwnd );
3741 req->color_key = key;
3742 req->alpha = alpha;
3743 req->flags = flags;
3744 ret = !wine_server_call_err( req );
3746 SERVER_END_REQ;
3748 if (ret)
3750 USER_Driver->pSetLayeredWindowAttributes( hwnd, key, alpha, flags );
3751 update_window_state( hwnd );
3754 return ret;
3758 /*****************************************************************************
3759 * GetLayeredWindowAttributes (USER32.@)
3761 BOOL WINAPI GetLayeredWindowAttributes( HWND hwnd, COLORREF *key, BYTE *alpha, DWORD *flags )
3763 BOOL ret;
3765 SERVER_START_REQ( get_window_layered_info )
3767 req->handle = wine_server_user_handle( hwnd );
3768 if ((ret = !wine_server_call_err( req )))
3770 if (key) *key = reply->color_key;
3771 if (alpha) *alpha = reply->alpha;
3772 if (flags) *flags = reply->flags;
3775 SERVER_END_REQ;
3777 return ret;
3781 /*****************************************************************************
3782 * UpdateLayeredWindowIndirect (USER32.@)
3784 BOOL WINAPI UpdateLayeredWindowIndirect( HWND hwnd, const UPDATELAYEREDWINDOWINFO *info )
3786 DWORD flags = SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW;
3787 RECT window_rect, client_rect;
3788 SIZE offset;
3790 if (!info ||
3791 info->cbSize != sizeof(*info) ||
3792 info->dwFlags & ~(ULW_COLORKEY | ULW_ALPHA | ULW_OPAQUE | ULW_EX_NORESIZE) ||
3793 !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED) ||
3794 GetLayeredWindowAttributes( hwnd, NULL, NULL, NULL ))
3796 SetLastError( ERROR_INVALID_PARAMETER );
3797 return FALSE;
3800 WIN_GetRectangles( hwnd, COORDS_PARENT, &window_rect, &client_rect );
3802 if (info->pptDst)
3804 offset.cx = info->pptDst->x - window_rect.left;
3805 offset.cy = info->pptDst->y - window_rect.top;
3806 OffsetRect( &client_rect, offset.cx, offset.cy );
3807 OffsetRect( &window_rect, offset.cx, offset.cy );
3808 flags &= ~SWP_NOMOVE;
3810 if (info->psize)
3812 offset.cx = info->psize->cx - (window_rect.right - window_rect.left);
3813 offset.cy = info->psize->cy - (window_rect.bottom - window_rect.top);
3814 if (info->psize->cx <= 0 || info->psize->cy <= 0)
3816 SetLastError( ERROR_INVALID_PARAMETER );
3817 return FALSE;
3819 if ((info->dwFlags & ULW_EX_NORESIZE) && (offset.cx || offset.cy))
3821 SetLastError( ERROR_INCORRECT_SIZE );
3822 return FALSE;
3824 client_rect.right += offset.cx;
3825 client_rect.bottom += offset.cy;
3826 window_rect.right += offset.cx;
3827 window_rect.bottom += offset.cy;
3828 flags &= ~SWP_NOSIZE;
3831 TRACE( "window %p win %s client %s\n", hwnd,
3832 wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(&client_rect) );
3834 if (!USER_Driver->pUpdateLayeredWindow( hwnd, info, &window_rect )) return FALSE;
3836 set_window_pos( hwnd, 0, flags, &window_rect, &client_rect, NULL );
3837 return TRUE;
3841 /*****************************************************************************
3842 * UpdateLayeredWindow (USER32.@)
3844 BOOL WINAPI UpdateLayeredWindow( HWND hwnd, HDC hdcDst, POINT *pptDst, SIZE *psize,
3845 HDC hdcSrc, POINT *pptSrc, COLORREF crKey, BLENDFUNCTION *pblend,
3846 DWORD flags)
3848 UPDATELAYEREDWINDOWINFO info;
3850 if (flags & ULW_EX_NORESIZE) /* only valid for UpdateLayeredWindowIndirect */
3852 SetLastError( ERROR_INVALID_PARAMETER );
3853 return FALSE;
3855 info.cbSize = sizeof(info);
3856 info.hdcDst = hdcDst;
3857 info.pptDst = pptDst;
3858 info.psize = psize;
3859 info.hdcSrc = hdcSrc;
3860 info.pptSrc = pptSrc;
3861 info.crKey = crKey;
3862 info.pblend = pblend;
3863 info.dwFlags = flags;
3864 info.prcDirty = NULL;
3865 return UpdateLayeredWindowIndirect( hwnd, &info );
3869 /******************************************************************************
3870 * GetProcessDefaultLayout [USER32.@]
3872 * Gets the default layout for parentless windows.
3874 BOOL WINAPI GetProcessDefaultLayout( DWORD *layout )
3876 if (!layout)
3878 SetLastError( ERROR_NOACCESS );
3879 return FALSE;
3881 if (process_layout == ~0u)
3883 static const WCHAR translationW[] = { '\\','V','a','r','F','i','l','e','I','n','f','o',
3884 '\\','T','r','a','n','s','l','a','t','i','o','n', 0 };
3885 static const WCHAR filedescW[] = { '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
3886 '\\','%','0','4','x','%','0','4','x',
3887 '\\','F','i','l','e','D','e','s','c','r','i','p','t','i','o','n',0 };
3888 WCHAR *str, buffer[MAX_PATH];
3889 DWORD i, len, version_layout = 0;
3890 DWORD user_lang = GetUserDefaultLangID();
3891 DWORD *languages;
3892 void *data = NULL;
3894 GetModuleFileNameW( 0, buffer, MAX_PATH );
3895 if (!(len = GetFileVersionInfoSizeW( buffer, NULL ))) goto done;
3896 if (!(data = HeapAlloc( GetProcessHeap(), 0, len ))) goto done;
3897 if (!GetFileVersionInfoW( buffer, 0, len, data )) goto done;
3898 if (!VerQueryValueW( data, translationW, (void **)&languages, &len ) || !len) goto done;
3900 len /= sizeof(DWORD);
3901 for (i = 0; i < len; i++) if (LOWORD(languages[i]) == user_lang) break;
3902 if (i == len) /* try neutral language */
3903 for (i = 0; i < len; i++)
3904 if (LOWORD(languages[i]) == MAKELANGID( PRIMARYLANGID(user_lang), SUBLANG_NEUTRAL )) break;
3905 if (i == len) i = 0; /* default to the first one */
3907 sprintfW( buffer, filedescW, LOWORD(languages[i]), HIWORD(languages[i]) );
3908 if (!VerQueryValueW( data, buffer, (void **)&str, &len )) goto done;
3909 TRACE( "found description %s\n", debugstr_w( str ));
3910 if (str[0] == 0x200e && str[1] == 0x200e) version_layout = LAYOUT_RTL;
3912 done:
3913 HeapFree( GetProcessHeap(), 0, data );
3914 process_layout = version_layout;
3916 *layout = process_layout;
3917 return TRUE;
3921 /******************************************************************************
3922 * SetProcessDefaultLayout [USER32.@]
3924 * Sets the default layout for parentless windows.
3926 BOOL WINAPI SetProcessDefaultLayout( DWORD layout )
3928 process_layout = layout;
3929 return TRUE;
3933 /* 64bit versions */
3935 #ifdef GetWindowLongPtrW
3936 #undef GetWindowLongPtrW
3937 #endif
3939 #ifdef GetWindowLongPtrA
3940 #undef GetWindowLongPtrA
3941 #endif
3943 #ifdef SetWindowLongPtrW
3944 #undef SetWindowLongPtrW
3945 #endif
3947 #ifdef SetWindowLongPtrA
3948 #undef SetWindowLongPtrA
3949 #endif
3951 /*****************************************************************************
3952 * GetWindowLongPtrW (USER32.@)
3954 LONG_PTR WINAPI GetWindowLongPtrW( HWND hwnd, INT offset )
3956 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG_PTR), TRUE );
3959 /*****************************************************************************
3960 * GetWindowLongPtrA (USER32.@)
3962 LONG_PTR WINAPI GetWindowLongPtrA( HWND hwnd, INT offset )
3964 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG_PTR), FALSE );
3967 /*****************************************************************************
3968 * SetWindowLongPtrW (USER32.@)
3970 LONG_PTR WINAPI SetWindowLongPtrW( HWND hwnd, INT offset, LONG_PTR newval )
3972 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG_PTR), newval, TRUE );
3975 /*****************************************************************************
3976 * SetWindowLongPtrA (USER32.@)
3978 LONG_PTR WINAPI SetWindowLongPtrA( HWND hwnd, INT offset, LONG_PTR newval )
3980 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG_PTR), newval, FALSE );
3983 /*****************************************************************************
3984 * RegisterTouchWindow (USER32.@)
3986 BOOL WINAPI RegisterTouchWindow(HWND hwnd, ULONG flags)
3988 FIXME("(%p %08x): stub\n", hwnd, flags);
3989 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
3990 return FALSE;
3993 /*****************************************************************************
3994 * UnregisterTouchWindow (USER32.@)
3996 BOOL WINAPI UnregisterTouchWindow(HWND hwnd)
3998 FIXME("(%p): stub\n", hwnd);
3999 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
4000 return FALSE;
4003 /*****************************************************************************
4004 * CloseTouchInputHandle (USER32.@)
4006 BOOL WINAPI CloseTouchInputHandle(HTOUCHINPUT handle)
4008 FIXME("(%p): stub\n", handle);
4009 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
4010 return FALSE;
4013 /*****************************************************************************
4014 * GetTouchInputInfo (USER32.@)
4016 BOOL WINAPI GetTouchInputInfo(HTOUCHINPUT handle, UINT count, TOUCHINPUT *ptr, int size)
4018 FIXME("(%p %u %p %u): stub\n", handle, count, ptr, size);
4019 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
4020 return FALSE;
4023 /*****************************************************************************
4024 * GetGestureInfo (USER32.@)
4026 BOOL WINAPI GetGestureInfo(HGESTUREINFO handle, PGESTUREINFO ptr)
4028 FIXME("(%p %p): stub\n", handle, ptr);
4029 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
4030 return FALSE;