gphoto2.ds: Set supported groups.
[wine.git] / dlls / user32 / win.c
blob382ca1086eca0aec5e08e724189eb9baccc7f221
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 MDICREATESTRUCTW mdi_cs;
1337 CBT_CREATEWNDW cbtc;
1338 CREATESTRUCTW cbcs;
1340 TRACE("%s %s ex=%08x style=%08x %d,%d %dx%d parent=%p menu=%p inst=%p params=%p\n",
1341 unicode ? debugstr_w(cs->lpszName) : debugstr_a((LPCSTR)cs->lpszName),
1342 debugstr_w(className),
1343 cs->dwExStyle, cs->style, cs->x, cs->y, cs->cx, cs->cy,
1344 cs->hwndParent, cs->hMenu, cs->hInstance, cs->lpCreateParams );
1345 if(TRACE_ON(win)) dump_window_styles( cs->style, cs->dwExStyle );
1347 /* Fix the styles for MDI children */
1348 if (cs->dwExStyle & WS_EX_MDICHILD)
1350 if (!(win_get_flags( cs->hwndParent ) & WIN_ISMDICLIENT))
1352 WARN("WS_EX_MDICHILD, but parent %p is not MDIClient\n", cs->hwndParent);
1353 return 0;
1356 /* cs->lpCreateParams of WM_[NC]CREATE is different for MDI children.
1357 * MDICREATESTRUCT members have the originally passed values.
1359 * Note: we rely on the fact that MDICREATESTRUCTA and MDICREATESTRUCTW
1360 * have the same layout.
1362 mdi_cs.szClass = cs->lpszClass;
1363 mdi_cs.szTitle = cs->lpszName;
1364 mdi_cs.hOwner = cs->hInstance;
1365 mdi_cs.x = cs->x;
1366 mdi_cs.y = cs->y;
1367 mdi_cs.cx = cs->cx;
1368 mdi_cs.cy = cs->cy;
1369 mdi_cs.style = cs->style;
1370 mdi_cs.lParam = (LPARAM)cs->lpCreateParams;
1372 cs->lpCreateParams = &mdi_cs;
1374 if (GetWindowLongW(cs->hwndParent, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1376 if (cs->style & WS_POPUP)
1378 TRACE("WS_POPUP with MDIS_ALLCHILDSTYLES is not allowed\n");
1379 return 0;
1381 cs->style |= WS_CHILD | WS_CLIPSIBLINGS;
1383 else
1385 cs->style &= ~WS_POPUP;
1386 cs->style |= WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CAPTION |
1387 WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
1390 top_child = GetWindow(cs->hwndParent, GW_CHILD);
1392 if (top_child)
1394 /* Restore current maximized child */
1395 if((cs->style & WS_VISIBLE) && IsZoomed(top_child))
1397 TRACE("Restoring current maximized child %p\n", top_child);
1398 if (cs->style & WS_MAXIMIZE)
1400 /* if the new window is maximized don't bother repainting */
1401 SendMessageW( top_child, WM_SETREDRAW, FALSE, 0 );
1402 ShowWindow( top_child, SW_SHOWNORMAL );
1403 SendMessageW( top_child, WM_SETREDRAW, TRUE, 0 );
1405 else ShowWindow( top_child, SW_SHOWNORMAL );
1410 /* Find the parent window */
1412 parent = cs->hwndParent;
1413 owner = 0;
1415 if (cs->hwndParent == HWND_MESSAGE)
1417 cs->hwndParent = parent = get_hwnd_message_parent();
1419 else if (cs->hwndParent)
1421 if ((cs->style & (WS_CHILD|WS_POPUP)) != WS_CHILD)
1423 parent = GetDesktopWindow();
1424 owner = cs->hwndParent;
1426 else
1428 DWORD parent_style = GetWindowLongW( parent, GWL_EXSTYLE );
1429 if ((parent_style & WS_EX_LAYOUTRTL) && !(parent_style & WS_EX_NOINHERITLAYOUT))
1430 cs->dwExStyle |= WS_EX_LAYOUTRTL;
1433 else
1435 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
1437 if ((cs->style & (WS_CHILD|WS_POPUP)) == WS_CHILD)
1439 WARN("No parent for child window\n" );
1440 SetLastError(ERROR_TLW_WITH_WSCHILD);
1441 return 0; /* WS_CHILD needs a parent, but WS_POPUP doesn't */
1444 /* are we creating the desktop or HWND_MESSAGE parent itself? */
1445 if (className != (LPCWSTR)DESKTOP_CLASS_ATOM &&
1446 (IS_INTRESOURCE(className) || strcmpiW( className, messageW )))
1448 DWORD layout;
1449 GetProcessDefaultLayout( &layout );
1450 if (layout & LAYOUT_RTL) cs->dwExStyle |= WS_EX_LAYOUTRTL;
1451 parent = GetDesktopWindow();
1455 WIN_FixCoordinates(cs, &sw); /* fix default coordinates */
1457 if ((cs->dwExStyle & WS_EX_DLGMODALFRAME) ||
1458 ((!(cs->dwExStyle & WS_EX_STATICEDGE)) &&
1459 (cs->style & (WS_DLGFRAME | WS_THICKFRAME))))
1460 cs->dwExStyle |= WS_EX_WINDOWEDGE;
1461 else
1462 cs->dwExStyle &= ~WS_EX_WINDOWEDGE;
1464 /* Create the window structure */
1466 if (!(wndPtr = create_window_handle( parent, owner, className, module, unicode )))
1468 WNDCLASSW wc;
1469 /* if it's a comctl32 class, GetClassInfo will load it, then we can retry */
1470 if (GetLastError() != ERROR_INVALID_HANDLE ||
1471 !GetClassInfoW( 0, className, &wc ) ||
1472 !(wndPtr = create_window_handle( parent, owner, className, module, unicode )))
1473 return 0;
1475 hwnd = wndPtr->obj.handle;
1477 /* Fill the window structure */
1479 wndPtr->tid = GetCurrentThreadId();
1480 wndPtr->hInstance = cs->hInstance;
1481 wndPtr->text = NULL;
1482 wndPtr->dwStyle = cs->style & ~WS_VISIBLE;
1483 wndPtr->dwExStyle = cs->dwExStyle;
1484 wndPtr->wIDmenu = 0;
1485 wndPtr->helpContext = 0;
1486 wndPtr->pScroll = NULL;
1487 wndPtr->userdata = 0;
1488 wndPtr->hIcon = 0;
1489 wndPtr->hIconSmall = 0;
1490 wndPtr->hIconSmall2 = 0;
1491 wndPtr->hSysMenu = 0;
1493 wndPtr->min_pos.x = wndPtr->min_pos.y = -1;
1494 wndPtr->max_pos.x = wndPtr->max_pos.y = -1;
1495 SetRect( &wndPtr->normal_rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
1497 if (wndPtr->dwStyle & WS_SYSMENU) SetSystemMenu( hwnd, 0 );
1500 * Correct the window styles.
1502 * It affects only the style loaded into the WIN structure.
1505 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
1507 wndPtr->dwStyle |= WS_CLIPSIBLINGS;
1508 if (!(wndPtr->dwStyle & WS_POPUP))
1509 wndPtr->dwStyle |= WS_CAPTION;
1512 /* WS_EX_WINDOWEDGE depends on some other styles */
1513 if (wndPtr->dwExStyle & WS_EX_DLGMODALFRAME)
1514 wndPtr->dwExStyle |= WS_EX_WINDOWEDGE;
1515 else if (wndPtr->dwStyle & (WS_DLGFRAME | WS_THICKFRAME))
1517 if (!((wndPtr->dwExStyle & WS_EX_STATICEDGE) &&
1518 (wndPtr->dwStyle & (WS_CHILD | WS_POPUP))))
1519 wndPtr->dwExStyle |= WS_EX_WINDOWEDGE;
1521 else
1522 wndPtr->dwExStyle &= ~WS_EX_WINDOWEDGE;
1524 if (!(wndPtr->dwStyle & (WS_CHILD | WS_POPUP)))
1525 wndPtr->flags |= WIN_NEED_SIZE;
1527 SERVER_START_REQ( set_window_info )
1529 req->handle = wine_server_user_handle( hwnd );
1530 req->flags = SET_WIN_STYLE | SET_WIN_EXSTYLE | SET_WIN_INSTANCE | SET_WIN_UNICODE;
1531 req->style = wndPtr->dwStyle;
1532 req->ex_style = wndPtr->dwExStyle;
1533 req->instance = wine_server_client_ptr( wndPtr->hInstance );
1534 req->is_unicode = (wndPtr->flags & WIN_ISUNICODE) != 0;
1535 req->extra_offset = -1;
1536 wine_server_call( req );
1538 SERVER_END_REQ;
1540 /* Set the window menu */
1542 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
1544 if (cs->hMenu)
1546 if (!MENU_SetMenu(hwnd, cs->hMenu))
1548 WIN_ReleasePtr( wndPtr );
1549 free_window_handle( hwnd );
1550 return 0;
1553 else
1555 LPCWSTR menuName = (LPCWSTR)GetClassLongPtrW( hwnd, GCLP_MENUNAME );
1556 if (menuName)
1558 cs->hMenu = LoadMenuW( cs->hInstance, menuName );
1559 if (cs->hMenu) MENU_SetMenu( hwnd, cs->hMenu );
1563 else SetWindowLongPtrW( hwnd, GWLP_ID, (ULONG_PTR)cs->hMenu );
1565 /* call the WH_CBT hook */
1567 /* the window style passed to the hook must be the real window style,
1568 * rather than just the window style that the caller to CreateWindowEx
1569 * passed in, so we have to copy the original CREATESTRUCT and get the
1570 * the real style. */
1571 cbcs = *cs;
1572 cbcs.style = wndPtr->dwStyle;
1573 cbtc.lpcs = &cbcs;
1574 cbtc.hwndInsertAfter = HWND_TOP;
1575 WIN_ReleasePtr( wndPtr );
1576 if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode )) goto failed;
1578 /* send the WM_GETMINMAXINFO message and fix the size if needed */
1580 cx = cs->cx;
1581 cy = cs->cy;
1582 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
1584 POINT maxSize, maxPos, minTrack, maxTrack;
1585 WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
1586 if (maxTrack.x < cx) cx = maxTrack.x;
1587 if (maxTrack.y < cy) cy = maxTrack.y;
1588 if (minTrack.x > cx) cx = minTrack.x;
1589 if (minTrack.y > cy) cy = minTrack.y;
1592 if (cx < 0) cx = 0;
1593 if (cy < 0) cy = 0;
1594 SetRect( &rect, cs->x, cs->y, cs->x + cx, cs->y + cy );
1595 /* check for wraparound */
1596 if (cs->x + cx < cs->x) rect.right = 0x7fffffff;
1597 if (cs->y + cy < cs->y) rect.bottom = 0x7fffffff;
1598 if (!set_window_pos( hwnd, 0, SWP_NOZORDER | SWP_NOACTIVATE, &rect, &rect, NULL )) goto failed;
1600 /* send WM_NCCREATE */
1602 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cx, cy );
1603 if (unicode)
1604 result = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1605 else
1606 result = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1607 if (!result)
1609 WARN( "%p: aborted by WM_NCCREATE\n", hwnd );
1610 goto failed;
1613 /* create default IME window */
1615 if (imm_register_window && !is_desktop_window( hwnd ) &&
1616 parent != get_hwnd_message_parent() && imm_register_window( hwnd ))
1618 TRACE("register IME window for %p\n", hwnd);
1619 win_set_flags( hwnd, WIN_HAS_IME_WIN, 0 );
1622 /* send WM_NCCALCSIZE */
1624 if (WIN_GetRectangles( hwnd, COORDS_PARENT, &rect, NULL ))
1626 /* yes, even if the CBT hook was called with HWND_TOP */
1627 HWND insert_after = (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
1628 RECT client_rect = rect;
1630 /* the rectangle is in screen coords for WM_NCCALCSIZE when wparam is FALSE */
1631 MapWindowPoints( parent, 0, (POINT *)&client_rect, 2 );
1632 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&client_rect );
1633 MapWindowPoints( 0, parent, (POINT *)&client_rect, 2 );
1634 set_window_pos( hwnd, insert_after, SWP_NOACTIVATE, &rect, &client_rect, NULL );
1636 else return 0;
1638 /* send WM_CREATE */
1640 if (unicode)
1641 result = SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs );
1642 else
1643 result = SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs );
1644 if (result == -1) goto failed;
1646 /* call the driver */
1648 if (!USER_Driver->pCreateWindow( hwnd )) goto failed;
1650 NotifyWinEvent(EVENT_OBJECT_CREATE, hwnd, OBJID_WINDOW, 0);
1652 /* send the size messages */
1654 if (!(win_get_flags( hwnd ) & WIN_NEED_SIZE))
1656 WIN_GetRectangles( hwnd, COORDS_PARENT, NULL, &rect );
1657 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1658 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1659 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( rect.left, rect.top ) );
1662 /* Show the window, maximizing or minimizing if needed */
1664 style = WIN_SetStyle( hwnd, 0, WS_MAXIMIZE | WS_MINIMIZE );
1665 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1667 RECT newPos;
1668 UINT swFlag = (style & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1670 swFlag = WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1671 swFlag |= SWP_FRAMECHANGED; /* Frame always gets changed */
1672 if (!(style & WS_VISIBLE) || (style & WS_CHILD) || GetActiveWindow()) swFlag |= SWP_NOACTIVATE;
1673 SetWindowPos( hwnd, 0, newPos.left, newPos.top, newPos.right - newPos.left,
1674 newPos.bottom - newPos.top, swFlag );
1677 /* Notify the parent window only */
1679 send_parent_notify( hwnd, WM_CREATE );
1680 if (!IsWindow( hwnd )) return 0;
1682 if (parent == GetDesktopWindow())
1683 PostMessageW( parent, WM_PARENTNOTIFY, WM_CREATE, (LPARAM)hwnd );
1685 if (cs->style & WS_VISIBLE)
1687 if (cs->style & WS_MAXIMIZE)
1688 sw = SW_SHOW;
1689 else if (cs->style & WS_MINIMIZE)
1690 sw = SW_SHOWMINIMIZED;
1692 ShowWindow( hwnd, sw );
1693 if (cs->dwExStyle & WS_EX_MDICHILD)
1695 SendMessageW(cs->hwndParent, WM_MDIREFRESHMENU, 0, 0);
1696 /* ShowWindow won't activate child windows */
1697 SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE );
1701 /* Call WH_SHELL hook */
1703 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) && !GetWindow( hwnd, GW_OWNER ))
1704 HOOK_CallHooks( WH_SHELL, HSHELL_WINDOWCREATED, (WPARAM)hwnd, 0, TRUE );
1706 TRACE("created window %p\n", hwnd);
1707 return hwnd;
1709 failed:
1710 WIN_DestroyWindow( hwnd );
1711 return 0;
1715 /***********************************************************************
1716 * CreateWindowExA (USER32.@)
1718 HWND WINAPI DECLSPEC_HOTPATCH CreateWindowExA( DWORD exStyle, LPCSTR className,
1719 LPCSTR windowName, DWORD style, INT x,
1720 INT y, INT width, INT height,
1721 HWND parent, HMENU menu,
1722 HINSTANCE instance, LPVOID data )
1724 CREATESTRUCTA cs;
1726 cs.lpCreateParams = data;
1727 cs.hInstance = instance;
1728 cs.hMenu = menu;
1729 cs.hwndParent = parent;
1730 cs.x = x;
1731 cs.y = y;
1732 cs.cx = width;
1733 cs.cy = height;
1734 cs.style = style;
1735 cs.lpszName = windowName;
1736 cs.lpszClass = className;
1737 cs.dwExStyle = exStyle;
1739 if (!IS_INTRESOURCE(className))
1741 WCHAR bufferW[256];
1742 if (!MultiByteToWideChar( CP_ACP, 0, className, -1, bufferW, sizeof(bufferW)/sizeof(WCHAR) ))
1743 return 0;
1744 return wow_handlers.create_window( (CREATESTRUCTW *)&cs, bufferW, instance, FALSE );
1746 /* Note: we rely on the fact that CREATESTRUCTA and */
1747 /* CREATESTRUCTW have the same layout. */
1748 return wow_handlers.create_window( (CREATESTRUCTW *)&cs, (LPCWSTR)className, instance, FALSE );
1752 /***********************************************************************
1753 * CreateWindowExW (USER32.@)
1755 HWND WINAPI DECLSPEC_HOTPATCH CreateWindowExW( DWORD exStyle, LPCWSTR className,
1756 LPCWSTR windowName, DWORD style, INT x,
1757 INT y, INT width, INT height,
1758 HWND parent, HMENU menu,
1759 HINSTANCE instance, LPVOID data )
1761 CREATESTRUCTW cs;
1763 cs.lpCreateParams = data;
1764 cs.hInstance = instance;
1765 cs.hMenu = menu;
1766 cs.hwndParent = parent;
1767 cs.x = x;
1768 cs.y = y;
1769 cs.cx = width;
1770 cs.cy = height;
1771 cs.style = style;
1772 cs.lpszName = windowName;
1773 cs.lpszClass = className;
1774 cs.dwExStyle = exStyle;
1776 return wow_handlers.create_window( &cs, className, instance, TRUE );
1780 /***********************************************************************
1781 * WIN_SendDestroyMsg
1783 static void WIN_SendDestroyMsg( HWND hwnd )
1785 GUITHREADINFO info;
1787 info.cbSize = sizeof(info);
1788 if (GetGUIThreadInfo( GetCurrentThreadId(), &info ))
1790 if (hwnd == info.hwndCaret) DestroyCaret();
1791 if (hwnd == info.hwndActive) WINPOS_ActivateOtherWindow( hwnd );
1794 if (hwnd == GetClipboardOwner()) CLIPBOARD_ReleaseOwner( hwnd );
1797 * Send the WM_DESTROY to the window.
1799 SendMessageW( hwnd, WM_DESTROY, 0, 0);
1802 * This WM_DESTROY message can trigger re-entrant calls to DestroyWindow
1803 * make sure that the window still exists when we come back.
1805 if (IsWindow(hwnd))
1807 HWND* pWndArray;
1808 int i;
1810 if (!(pWndArray = WIN_ListChildren( hwnd ))) return;
1812 for (i = 0; pWndArray[i]; i++)
1814 if (IsWindow( pWndArray[i] )) WIN_SendDestroyMsg( pWndArray[i] );
1816 HeapFree( GetProcessHeap(), 0, pWndArray );
1818 else
1819 WARN("\tdestroyed itself while in WM_DESTROY!\n");
1823 /***********************************************************************
1824 * DestroyWindow (USER32.@)
1826 BOOL WINAPI DestroyWindow( HWND hwnd )
1828 BOOL is_child;
1830 if (!(hwnd = WIN_IsCurrentThread( hwnd )) || is_desktop_window( hwnd ))
1832 SetLastError( ERROR_ACCESS_DENIED );
1833 return FALSE;
1836 TRACE("(%p)\n", hwnd);
1838 /* Call hooks */
1840 if (HOOK_CallHooks( WH_CBT, HCBT_DESTROYWND, (WPARAM)hwnd, 0, TRUE )) return FALSE;
1842 if (MENU_IsMenuActive() == hwnd)
1843 EndMenu();
1845 is_child = (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) != 0;
1847 if (is_child)
1849 if (!USER_IsExitingThread( GetCurrentThreadId() ))
1850 send_parent_notify( hwnd, WM_DESTROY );
1852 else if (!GetWindow( hwnd, GW_OWNER ))
1854 HOOK_CallHooks( WH_SHELL, HSHELL_WINDOWDESTROYED, (WPARAM)hwnd, 0L, TRUE );
1855 /* FIXME: clean up palette - see "Internals" p.352 */
1858 if (!IsWindow(hwnd)) return TRUE;
1860 /* Hide the window */
1861 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)
1863 /* Only child windows receive WM_SHOWWINDOW in DestroyWindow() */
1864 if (is_child)
1865 ShowWindow( hwnd, SW_HIDE );
1866 else
1867 SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE |
1868 SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW );
1871 if (!IsWindow(hwnd)) return TRUE;
1873 /* Recursively destroy owned windows */
1875 if (!is_child)
1877 for (;;)
1879 int i;
1880 BOOL got_one = FALSE;
1881 HWND *list = WIN_ListChildren( GetDesktopWindow() );
1882 if (list)
1884 for (i = 0; list[i]; i++)
1886 if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
1887 if (WIN_IsCurrentThread( list[i] ))
1889 DestroyWindow( list[i] );
1890 got_one = TRUE;
1891 continue;
1893 WIN_SetOwner( list[i], 0 );
1895 HeapFree( GetProcessHeap(), 0, list );
1897 if (!got_one) break;
1901 /* Send destroy messages */
1903 WIN_SendDestroyMsg( hwnd );
1904 if (!IsWindow( hwnd )) return TRUE;
1906 /* Destroy the window storage */
1908 WIN_DestroyWindow( hwnd );
1909 return TRUE;
1913 /***********************************************************************
1914 * CloseWindow (USER32.@)
1916 BOOL WINAPI CloseWindow( HWND hwnd )
1918 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) return FALSE;
1919 ShowWindow( hwnd, SW_MINIMIZE );
1920 return TRUE;
1924 /***********************************************************************
1925 * OpenIcon (USER32.@)
1927 BOOL WINAPI OpenIcon( HWND hwnd )
1929 if (!IsIconic( hwnd )) return FALSE;
1930 ShowWindow( hwnd, SW_SHOWNORMAL );
1931 return TRUE;
1935 /***********************************************************************
1936 * FindWindowExW (USER32.@)
1938 HWND WINAPI FindWindowExW( HWND parent, HWND child, LPCWSTR className, LPCWSTR title )
1940 HWND *list;
1941 HWND retvalue = 0;
1942 int i = 0, len = 0;
1943 WCHAR *buffer = NULL;
1945 if (!parent && child) parent = GetDesktopWindow();
1946 else if (parent == HWND_MESSAGE) parent = get_hwnd_message_parent();
1948 if (title)
1950 len = strlenW(title) + 1; /* one extra char to check for chars beyond the end */
1951 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return 0;
1954 if (!(list = list_window_children( 0, parent, className, 0 ))) goto done;
1956 if (child)
1958 child = WIN_GetFullHandle( child );
1959 while (list[i] && list[i] != child) i++;
1960 if (!list[i]) goto done;
1961 i++; /* start from next window */
1964 if (title)
1966 while (list[i])
1968 if (InternalGetWindowText( list[i], buffer, len + 1 ))
1970 if (!strcmpiW( buffer, title )) break;
1972 else
1974 if (!title[0]) break;
1976 i++;
1979 retvalue = list[i];
1981 done:
1982 HeapFree( GetProcessHeap(), 0, list );
1983 HeapFree( GetProcessHeap(), 0, buffer );
1984 return retvalue;
1989 /***********************************************************************
1990 * FindWindowA (USER32.@)
1992 HWND WINAPI FindWindowA( LPCSTR className, LPCSTR title )
1994 HWND ret = FindWindowExA( 0, 0, className, title );
1995 if (!ret) SetLastError (ERROR_CANNOT_FIND_WND_CLASS);
1996 return ret;
2000 /***********************************************************************
2001 * FindWindowExA (USER32.@)
2003 HWND WINAPI FindWindowExA( HWND parent, HWND child, LPCSTR className, LPCSTR title )
2005 LPWSTR titleW = NULL;
2006 HWND hwnd = 0;
2008 if (title)
2010 DWORD len = MultiByteToWideChar( CP_ACP, 0, title, -1, NULL, 0 );
2011 if (!(titleW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return 0;
2012 MultiByteToWideChar( CP_ACP, 0, title, -1, titleW, len );
2015 if (!IS_INTRESOURCE(className))
2017 WCHAR classW[256];
2018 if (MultiByteToWideChar( CP_ACP, 0, className, -1, classW, sizeof(classW)/sizeof(WCHAR) ))
2019 hwnd = FindWindowExW( parent, child, classW, titleW );
2021 else
2023 hwnd = FindWindowExW( parent, child, (LPCWSTR)className, titleW );
2026 HeapFree( GetProcessHeap(), 0, titleW );
2027 return hwnd;
2031 /***********************************************************************
2032 * FindWindowW (USER32.@)
2034 HWND WINAPI FindWindowW( LPCWSTR className, LPCWSTR title )
2036 return FindWindowExW( 0, 0, className, title );
2040 /**********************************************************************
2041 * GetDesktopWindow (USER32.@)
2043 HWND WINAPI GetDesktopWindow(void)
2045 struct user_thread_info *thread_info = get_user_thread_info();
2047 if (thread_info->top_window) return thread_info->top_window;
2049 SERVER_START_REQ( get_desktop_window )
2051 req->force = 0;
2052 if (!wine_server_call( req ))
2054 thread_info->top_window = wine_server_ptr_handle( reply->top_window );
2055 thread_info->msg_window = wine_server_ptr_handle( reply->msg_window );
2058 SERVER_END_REQ;
2060 if (!thread_info->top_window)
2062 static const WCHAR explorer[] = {'\\','e','x','p','l','o','r','e','r','.','e','x','e',0};
2063 static const WCHAR args[] = {' ','/','d','e','s','k','t','o','p',0};
2064 STARTUPINFOW si;
2065 PROCESS_INFORMATION pi;
2066 WCHAR windir[MAX_PATH];
2067 WCHAR app[MAX_PATH + sizeof(explorer)/sizeof(WCHAR)];
2068 WCHAR cmdline[MAX_PATH + (sizeof(explorer) + sizeof(args))/sizeof(WCHAR)];
2069 WCHAR desktop[MAX_PATH];
2070 void *redir;
2072 SERVER_START_REQ( set_user_object_info )
2074 req->handle = wine_server_obj_handle( GetThreadDesktop(GetCurrentThreadId()) );
2075 req->flags = SET_USER_OBJECT_GET_FULL_NAME;
2076 wine_server_set_reply( req, desktop, sizeof(desktop) - sizeof(WCHAR) );
2077 if (!wine_server_call( req ))
2079 size_t size = wine_server_reply_size( reply );
2080 desktop[size / sizeof(WCHAR)] = 0;
2081 TRACE( "starting explorer for desktop %s\n", debugstr_w(desktop) );
2083 else
2084 desktop[0] = 0;
2086 SERVER_END_REQ;
2088 memset( &si, 0, sizeof(si) );
2089 si.cb = sizeof(si);
2090 si.lpDesktop = *desktop ? desktop : NULL;
2091 si.dwFlags = STARTF_USESTDHANDLES;
2092 si.hStdInput = 0;
2093 si.hStdOutput = 0;
2094 si.hStdError = GetStdHandle( STD_ERROR_HANDLE );
2096 GetSystemDirectoryW( windir, MAX_PATH );
2097 strcpyW( app, windir );
2098 strcatW( app, explorer );
2099 strcpyW( cmdline, app );
2100 strcatW( cmdline, args );
2102 Wow64DisableWow64FsRedirection( &redir );
2103 if (CreateProcessW( app, cmdline, NULL, NULL, FALSE, DETACHED_PROCESS,
2104 NULL, windir, &si, &pi ))
2106 TRACE( "started explorer pid %04x tid %04x\n", pi.dwProcessId, pi.dwThreadId );
2107 WaitForInputIdle( pi.hProcess, 10000 );
2108 CloseHandle( pi.hThread );
2109 CloseHandle( pi.hProcess );
2111 else WARN( "failed to start explorer, err %d\n", GetLastError() );
2112 Wow64RevertWow64FsRedirection( redir );
2114 SERVER_START_REQ( get_desktop_window )
2116 req->force = 1;
2117 if (!wine_server_call( req ))
2119 thread_info->top_window = wine_server_ptr_handle( reply->top_window );
2120 thread_info->msg_window = wine_server_ptr_handle( reply->msg_window );
2123 SERVER_END_REQ;
2126 if (!thread_info->top_window || !USER_Driver->pCreateDesktopWindow( thread_info->top_window ))
2127 ERR( "failed to create desktop window\n" );
2129 return thread_info->top_window;
2133 /*******************************************************************
2134 * EnableWindow (USER32.@)
2136 BOOL WINAPI EnableWindow( HWND hwnd, BOOL enable )
2138 BOOL retvalue;
2140 if (is_broadcast(hwnd))
2142 SetLastError( ERROR_INVALID_PARAMETER );
2143 return FALSE;
2146 TRACE("( %p, %d )\n", hwnd, enable);
2148 if (enable)
2150 retvalue = (WIN_SetStyle( hwnd, 0, WS_DISABLED ) & WS_DISABLED) != 0;
2151 if (retvalue) SendMessageW( hwnd, WM_ENABLE, TRUE, 0 );
2153 else
2155 SendMessageW( hwnd, WM_CANCELMODE, 0, 0 );
2157 retvalue = (WIN_SetStyle( hwnd, WS_DISABLED, 0 ) & WS_DISABLED) != 0;
2158 if (!retvalue)
2160 if (hwnd == GetFocus())
2161 SetFocus( 0 ); /* A disabled window can't have the focus */
2163 SendMessageW( hwnd, WM_ENABLE, FALSE, 0 );
2166 return retvalue;
2170 /***********************************************************************
2171 * IsWindowEnabled (USER32.@)
2173 BOOL WINAPI IsWindowEnabled(HWND hWnd)
2175 return !(GetWindowLongW( hWnd, GWL_STYLE ) & WS_DISABLED);
2179 /***********************************************************************
2180 * IsWindowUnicode (USER32.@)
2182 BOOL WINAPI IsWindowUnicode( HWND hwnd )
2184 WND * wndPtr;
2185 BOOL retvalue = FALSE;
2187 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
2189 if (wndPtr == WND_DESKTOP) return TRUE;
2191 if (wndPtr != WND_OTHER_PROCESS)
2193 retvalue = (wndPtr->flags & WIN_ISUNICODE) != 0;
2194 WIN_ReleasePtr( wndPtr );
2196 else
2198 SERVER_START_REQ( get_window_info )
2200 req->handle = wine_server_user_handle( hwnd );
2201 if (!wine_server_call_err( req )) retvalue = reply->is_unicode;
2203 SERVER_END_REQ;
2205 return retvalue;
2209 /**********************************************************************
2210 * WIN_GetWindowLong
2212 * Helper function for GetWindowLong().
2214 static LONG_PTR WIN_GetWindowLong( HWND hwnd, INT offset, UINT size, BOOL unicode )
2216 LONG_PTR retvalue = 0;
2217 WND *wndPtr;
2219 if (offset == GWLP_HWNDPARENT)
2221 HWND parent = GetAncestor( hwnd, GA_PARENT );
2222 if (parent == GetDesktopWindow()) parent = GetWindow( hwnd, GW_OWNER );
2223 return (ULONG_PTR)parent;
2226 if (!(wndPtr = WIN_GetPtr( hwnd )))
2228 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2229 return 0;
2232 if (wndPtr == WND_DESKTOP)
2234 switch (offset)
2236 case GWL_STYLE:
2237 retvalue = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN; /* message parent is not visible */
2238 if (WIN_GetFullHandle( hwnd ) == GetDesktopWindow())
2239 retvalue |= WS_VISIBLE;
2240 return retvalue;
2241 case GWL_EXSTYLE:
2242 case GWLP_USERDATA:
2243 case GWLP_ID:
2244 case GWLP_HINSTANCE:
2245 return 0;
2246 case GWLP_WNDPROC:
2247 SetLastError( ERROR_ACCESS_DENIED );
2248 return 0;
2250 SetLastError( ERROR_INVALID_INDEX );
2251 return 0;
2254 if (wndPtr == WND_OTHER_PROCESS)
2256 if (offset == GWLP_WNDPROC)
2258 SetLastError( ERROR_ACCESS_DENIED );
2259 return 0;
2261 SERVER_START_REQ( set_window_info )
2263 req->handle = wine_server_user_handle( hwnd );
2264 req->flags = 0; /* don't set anything, just retrieve */
2265 req->extra_offset = (offset >= 0) ? offset : -1;
2266 req->extra_size = (offset >= 0) ? size : 0;
2267 if (!wine_server_call_err( req ))
2269 switch(offset)
2271 case GWL_STYLE: retvalue = reply->old_style; break;
2272 case GWL_EXSTYLE: retvalue = reply->old_ex_style; break;
2273 case GWLP_ID: retvalue = reply->old_id; break;
2274 case GWLP_HINSTANCE: retvalue = (ULONG_PTR)wine_server_get_ptr( reply->old_instance ); break;
2275 case GWLP_USERDATA: retvalue = reply->old_user_data; break;
2276 default:
2277 if (offset >= 0) retvalue = get_win_data( &reply->old_extra_value, size );
2278 else SetLastError( ERROR_INVALID_INDEX );
2279 break;
2283 SERVER_END_REQ;
2284 return retvalue;
2287 /* now we have a valid wndPtr */
2289 if (offset >= 0)
2291 if (offset > (int)(wndPtr->cbWndExtra - size))
2293 WARN("Invalid offset %d\n", offset );
2294 WIN_ReleasePtr( wndPtr );
2295 SetLastError( ERROR_INVALID_INDEX );
2296 return 0;
2298 retvalue = get_win_data( (char *)wndPtr->wExtra + offset, size );
2300 /* Special case for dialog window procedure */
2301 if ((offset == DWLP_DLGPROC) && (size == sizeof(LONG_PTR)) && wndPtr->dlgInfo)
2302 retvalue = (LONG_PTR)WINPROC_GetProc( (WNDPROC)retvalue, unicode );
2303 WIN_ReleasePtr( wndPtr );
2304 return retvalue;
2307 switch(offset)
2309 case GWLP_USERDATA: retvalue = wndPtr->userdata; break;
2310 case GWL_STYLE: retvalue = wndPtr->dwStyle; break;
2311 case GWL_EXSTYLE: retvalue = wndPtr->dwExStyle; break;
2312 case GWLP_ID: retvalue = wndPtr->wIDmenu; break;
2313 case GWLP_HINSTANCE: retvalue = (ULONG_PTR)wndPtr->hInstance; break;
2314 case GWLP_WNDPROC:
2315 /* This looks like a hack only for the edit control (see tests). This makes these controls
2316 * more tolerant to A/W mismatches. The lack of W->A->W conversion for such a mismatch suggests
2317 * that the hack is in GetWindowLongPtr[AW], not in winprocs.
2319 if (wndPtr->winproc == BUILTIN_WINPROC(WINPROC_EDIT) && (!unicode != !(wndPtr->flags & WIN_ISUNICODE)))
2320 retvalue = (ULONG_PTR)wndPtr->winproc;
2321 else
2322 retvalue = (ULONG_PTR)WINPROC_GetProc( wndPtr->winproc, unicode );
2323 break;
2324 default:
2325 WARN("Unknown offset %d\n", offset );
2326 SetLastError( ERROR_INVALID_INDEX );
2327 break;
2329 WIN_ReleasePtr(wndPtr);
2330 return retvalue;
2334 /**********************************************************************
2335 * WIN_SetWindowLong
2337 * Helper function for SetWindowLong().
2339 * 0 is the failure code. However, in the case of failure SetLastError
2340 * must be set to distinguish between a 0 return value and a failure.
2342 LONG_PTR WIN_SetWindowLong( HWND hwnd, INT offset, UINT size, LONG_PTR newval, BOOL unicode )
2344 STYLESTRUCT style;
2345 BOOL ok, made_visible = FALSE;
2346 LONG_PTR retval = 0;
2347 WND *wndPtr;
2349 TRACE( "%p %d %lx %c\n", hwnd, offset, newval, unicode ? 'W' : 'A' );
2351 if (is_broadcast(hwnd))
2353 SetLastError( ERROR_INVALID_PARAMETER );
2354 return FALSE;
2357 if (!(wndPtr = WIN_GetPtr( hwnd )))
2359 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2360 return 0;
2362 if (wndPtr == WND_DESKTOP)
2364 /* can't change anything on the desktop window */
2365 SetLastError( ERROR_ACCESS_DENIED );
2366 return 0;
2368 if (wndPtr == WND_OTHER_PROCESS)
2370 if (offset == GWLP_WNDPROC)
2372 SetLastError( ERROR_ACCESS_DENIED );
2373 return 0;
2375 if (offset > 32767 || offset < -32767)
2377 SetLastError( ERROR_INVALID_INDEX );
2378 return 0;
2380 return SendMessageW( hwnd, WM_WINE_SETWINDOWLONG, MAKEWPARAM( offset, size ), newval );
2383 /* first some special cases */
2384 switch( offset )
2386 case GWL_STYLE:
2387 style.styleOld = wndPtr->dwStyle;
2388 style.styleNew = newval;
2389 WIN_ReleasePtr( wndPtr );
2390 SendMessageW( hwnd, WM_STYLECHANGING, GWL_STYLE, (LPARAM)&style );
2391 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
2392 newval = style.styleNew;
2393 /* WS_CLIPSIBLINGS can't be reset on top-level windows */
2394 if (wndPtr->parent == GetDesktopWindow()) newval |= WS_CLIPSIBLINGS;
2395 /* WS_MINIMIZE can't be reset */
2396 if (wndPtr->dwStyle & WS_MINIMIZE) newval |= WS_MINIMIZE;
2397 /* FIXME: changing WS_DLGFRAME | WS_THICKFRAME is supposed to change
2398 WS_EX_WINDOWEDGE too */
2399 break;
2400 case GWL_EXSTYLE:
2401 style.styleOld = wndPtr->dwExStyle;
2402 style.styleNew = newval;
2403 WIN_ReleasePtr( wndPtr );
2404 SendMessageW( hwnd, WM_STYLECHANGING, GWL_EXSTYLE, (LPARAM)&style );
2405 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
2406 /* WS_EX_TOPMOST can only be changed through SetWindowPos */
2407 newval = (style.styleNew & ~WS_EX_TOPMOST) | (wndPtr->dwExStyle & WS_EX_TOPMOST);
2408 /* WS_EX_WINDOWEDGE depends on some other styles */
2409 if (newval & WS_EX_DLGMODALFRAME)
2410 newval |= WS_EX_WINDOWEDGE;
2411 else if (!(newval & WS_EX_STATICEDGE) && (wndPtr->dwStyle & (WS_DLGFRAME | WS_THICKFRAME)))
2412 newval |= WS_EX_WINDOWEDGE;
2413 else
2414 newval &= ~WS_EX_WINDOWEDGE;
2415 break;
2416 case GWLP_HWNDPARENT:
2417 if (wndPtr->parent == GetDesktopWindow())
2419 WIN_ReleasePtr( wndPtr );
2420 return (ULONG_PTR)WIN_SetOwner( hwnd, (HWND)newval );
2422 else
2424 WIN_ReleasePtr( wndPtr );
2425 return (ULONG_PTR)SetParent( hwnd, (HWND)newval );
2427 case GWLP_WNDPROC:
2429 WNDPROC proc;
2430 UINT old_flags = wndPtr->flags;
2431 retval = WIN_GetWindowLong( hwnd, offset, size, unicode );
2432 proc = WINPROC_AllocProc( (WNDPROC)newval, unicode );
2433 if (proc) wndPtr->winproc = proc;
2434 if (WINPROC_IsUnicode( proc, unicode )) wndPtr->flags |= WIN_ISUNICODE;
2435 else wndPtr->flags &= ~WIN_ISUNICODE;
2436 if (!((old_flags ^ wndPtr->flags) & WIN_ISUNICODE))
2438 WIN_ReleasePtr( wndPtr );
2439 return retval;
2441 /* update is_unicode flag on the server side */
2442 break;
2444 case GWLP_ID:
2445 case GWLP_HINSTANCE:
2446 case GWLP_USERDATA:
2447 break;
2448 case DWLP_DLGPROC:
2449 if ((wndPtr->cbWndExtra - sizeof(LONG_PTR) >= DWLP_DLGPROC) &&
2450 (size == sizeof(LONG_PTR)) && wndPtr->dlgInfo)
2452 WNDPROC *ptr = (WNDPROC *)((char *)wndPtr->wExtra + DWLP_DLGPROC);
2453 retval = (ULONG_PTR)WINPROC_GetProc( *ptr, unicode );
2454 *ptr = WINPROC_AllocProc( (WNDPROC)newval, unicode );
2455 WIN_ReleasePtr( wndPtr );
2456 return retval;
2458 /* fall through */
2459 default:
2460 if (offset < 0 || offset > (int)(wndPtr->cbWndExtra - size))
2462 WARN("Invalid offset %d\n", offset );
2463 WIN_ReleasePtr( wndPtr );
2464 SetLastError( ERROR_INVALID_INDEX );
2465 return 0;
2467 else if (get_win_data( (char *)wndPtr->wExtra + offset, size ) == newval)
2469 /* already set to the same value */
2470 WIN_ReleasePtr( wndPtr );
2471 return newval;
2473 break;
2476 SERVER_START_REQ( set_window_info )
2478 req->handle = wine_server_user_handle( hwnd );
2479 req->extra_offset = -1;
2480 switch(offset)
2482 case GWL_STYLE:
2483 req->flags = SET_WIN_STYLE;
2484 req->style = newval;
2485 break;
2486 case GWL_EXSTYLE:
2487 req->flags = SET_WIN_EXSTYLE;
2488 req->ex_style = newval;
2489 break;
2490 case GWLP_ID:
2491 req->flags = SET_WIN_ID;
2492 req->id = newval;
2493 break;
2494 case GWLP_HINSTANCE:
2495 req->flags = SET_WIN_INSTANCE;
2496 req->instance = wine_server_client_ptr( (void *)newval );
2497 break;
2498 case GWLP_WNDPROC:
2499 req->flags = SET_WIN_UNICODE;
2500 req->is_unicode = (wndPtr->flags & WIN_ISUNICODE) != 0;
2501 break;
2502 case GWLP_USERDATA:
2503 req->flags = SET_WIN_USERDATA;
2504 req->user_data = newval;
2505 break;
2506 default:
2507 req->flags = SET_WIN_EXTRA;
2508 req->extra_offset = offset;
2509 req->extra_size = size;
2510 set_win_data( &req->extra_value, newval, size );
2512 if ((ok = !wine_server_call_err( req )))
2514 switch(offset)
2516 case GWL_STYLE:
2517 wndPtr->dwStyle = newval;
2518 retval = reply->old_style;
2519 break;
2520 case GWL_EXSTYLE:
2521 wndPtr->dwExStyle = newval;
2522 retval = reply->old_ex_style;
2523 break;
2524 case GWLP_ID:
2525 wndPtr->wIDmenu = newval;
2526 retval = reply->old_id;
2527 break;
2528 case GWLP_HINSTANCE:
2529 wndPtr->hInstance = (HINSTANCE)newval;
2530 retval = (ULONG_PTR)wine_server_get_ptr( reply->old_instance );
2531 break;
2532 case GWLP_WNDPROC:
2533 break;
2534 case GWLP_USERDATA:
2535 wndPtr->userdata = newval;
2536 retval = reply->old_user_data;
2537 break;
2538 default:
2539 retval = get_win_data( (char *)wndPtr->wExtra + offset, size );
2540 set_win_data( (char *)wndPtr->wExtra + offset, newval, size );
2541 break;
2545 SERVER_END_REQ;
2547 if ((offset == GWL_STYLE && ((style.styleOld ^ style.styleNew) & WS_VISIBLE)) ||
2548 (offset == GWL_EXSTYLE && ((style.styleOld ^ style.styleNew) & WS_EX_LAYERED)))
2550 made_visible = (wndPtr->dwStyle & WS_VISIBLE) != 0;
2551 invalidate_dce( wndPtr, NULL );
2553 WIN_ReleasePtr( wndPtr );
2555 if (!ok) return 0;
2557 if (offset == GWL_STYLE || offset == GWL_EXSTYLE)
2559 style.styleOld = retval;
2560 style.styleNew = newval;
2561 USER_Driver->pSetWindowStyle( hwnd, offset, &style );
2562 if (made_visible) update_window_state( hwnd );
2563 SendMessageW( hwnd, WM_STYLECHANGED, offset, (LPARAM)&style );
2566 return retval;
2570 /**********************************************************************
2571 * GetWindowWord (USER32.@)
2573 WORD WINAPI GetWindowWord( HWND hwnd, INT offset )
2575 switch(offset)
2577 case GWLP_ID:
2578 case GWLP_HINSTANCE:
2579 case GWLP_HWNDPARENT:
2580 break;
2581 default:
2582 if (offset < 0)
2584 WARN("Invalid offset %d\n", offset );
2585 SetLastError( ERROR_INVALID_INDEX );
2586 return 0;
2588 break;
2590 return WIN_GetWindowLong( hwnd, offset, sizeof(WORD), FALSE );
2594 /**********************************************************************
2595 * GetWindowLongA (USER32.@)
2597 LONG WINAPI GetWindowLongA( HWND hwnd, INT offset )
2599 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG), FALSE );
2603 /**********************************************************************
2604 * GetWindowLongW (USER32.@)
2606 LONG WINAPI GetWindowLongW( HWND hwnd, INT offset )
2608 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG), TRUE );
2612 /**********************************************************************
2613 * SetWindowWord (USER32.@)
2615 WORD WINAPI SetWindowWord( HWND hwnd, INT offset, WORD newval )
2617 switch(offset)
2619 case GWLP_ID:
2620 case GWLP_HINSTANCE:
2621 case GWLP_HWNDPARENT:
2622 break;
2623 default:
2624 if (offset < 0)
2626 WARN("Invalid offset %d\n", offset );
2627 SetLastError( ERROR_INVALID_INDEX );
2628 return 0;
2630 break;
2632 return WIN_SetWindowLong( hwnd, offset, sizeof(WORD), newval, FALSE );
2636 /**********************************************************************
2637 * SetWindowLongA (USER32.@)
2639 * See SetWindowLongW.
2641 LONG WINAPI DECLSPEC_HOTPATCH SetWindowLongA( HWND hwnd, INT offset, LONG newval )
2643 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG), newval, FALSE );
2647 /**********************************************************************
2648 * SetWindowLongW (USER32.@) Set window attribute
2650 * SetWindowLong() alters one of a window's attributes or sets a 32-bit (long)
2651 * value in a window's extra memory.
2653 * The _hwnd_ parameter specifies the handle to a window that
2654 * has extra memory. The _newval_ parameter contains the new
2655 * attribute or extra memory value. If positive, the _offset_
2656 * parameter is the byte-addressed location in the window's extra
2657 * memory to set. If negative, _offset_ specifies the window
2658 * attribute to set, and should be one of the following values:
2660 * GWL_EXSTYLE The window's extended window style
2662 * GWL_STYLE The window's window style.
2664 * GWLP_WNDPROC Pointer to the window's window procedure.
2666 * GWLP_HINSTANCE The window's application instance handle.
2668 * GWLP_ID The window's identifier.
2670 * GWLP_USERDATA The window's user-specified data.
2672 * If the window is a dialog box, the _offset_ parameter can be one of
2673 * the following values:
2675 * DWLP_DLGPROC The address of the window's dialog box procedure.
2677 * DWLP_MSGRESULT The return value of a message
2678 * that the dialog box procedure processed.
2680 * DWLP_USER Application specific information.
2682 * RETURNS
2684 * If successful, returns the previous value located at _offset_. Otherwise,
2685 * returns 0.
2687 * NOTES
2689 * Extra memory for a window class is specified by a nonzero cbWndExtra
2690 * parameter of the WNDCLASS structure passed to RegisterClass() at the
2691 * time of class creation.
2693 * Using GWL_WNDPROC to set a new window procedure effectively creates
2694 * a window subclass. Use CallWindowProc() in the new windows procedure
2695 * to pass messages to the superclass's window procedure.
2697 * The user data is reserved for use by the application which created
2698 * the window.
2700 * Do not use GWL_STYLE to change the window's WS_DISABLED style;
2701 * instead, call the EnableWindow() function to change the window's
2702 * disabled state.
2704 * Do not use GWL_HWNDPARENT to reset the window's parent, use
2705 * SetParent() instead.
2707 * Win95:
2708 * When offset is GWL_STYLE and the calling app's ver is 4.0,
2709 * it sends WM_STYLECHANGING before changing the settings
2710 * and WM_STYLECHANGED afterwards.
2711 * App ver 4.0 can't use SetWindowLong to change WS_EX_TOPMOST.
2713 LONG WINAPI DECLSPEC_HOTPATCH SetWindowLongW(
2714 HWND hwnd, /* [in] window to alter */
2715 INT offset, /* [in] offset, in bytes, of location to alter */
2716 LONG newval /* [in] new value of location */
2718 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG), newval, TRUE );
2722 /*******************************************************************
2723 * GetWindowTextA (USER32.@)
2725 INT WINAPI GetWindowTextA( HWND hwnd, LPSTR lpString, INT nMaxCount )
2727 WCHAR *buffer;
2729 if (!lpString || nMaxCount <= 0) return 0;
2731 if (WIN_IsCurrentProcess( hwnd ))
2733 lpString[0] = 0;
2734 return (INT)SendMessageA( hwnd, WM_GETTEXT, nMaxCount, (LPARAM)lpString );
2737 /* when window belongs to other process, don't send a message */
2738 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, nMaxCount * sizeof(WCHAR) ))) return 0;
2739 get_server_window_text( hwnd, buffer, nMaxCount );
2740 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, lpString, nMaxCount, NULL, NULL ))
2741 lpString[nMaxCount-1] = 0;
2742 HeapFree( GetProcessHeap(), 0, buffer );
2743 return strlen(lpString);
2747 /*******************************************************************
2748 * InternalGetWindowText (USER32.@)
2750 INT WINAPI InternalGetWindowText(HWND hwnd,LPWSTR lpString,INT nMaxCount )
2752 WND *win;
2754 if (nMaxCount <= 0) return 0;
2755 if (!(win = WIN_GetPtr( hwnd ))) return 0;
2756 if (win == WND_DESKTOP) lpString[0] = 0;
2757 else if (win != WND_OTHER_PROCESS)
2759 if (win->text) lstrcpynW( lpString, win->text, nMaxCount );
2760 else lpString[0] = 0;
2761 WIN_ReleasePtr( win );
2763 else
2765 get_server_window_text( hwnd, lpString, nMaxCount );
2767 return strlenW(lpString);
2771 /*******************************************************************
2772 * GetWindowTextW (USER32.@)
2774 INT WINAPI GetWindowTextW( HWND hwnd, LPWSTR lpString, INT nMaxCount )
2776 if (!lpString || nMaxCount <= 0) return 0;
2778 if (WIN_IsCurrentProcess( hwnd ))
2780 lpString[0] = 0;
2781 return (INT)SendMessageW( hwnd, WM_GETTEXT, nMaxCount, (LPARAM)lpString );
2784 /* when window belongs to other process, don't send a message */
2785 get_server_window_text( hwnd, lpString, nMaxCount );
2786 return strlenW(lpString);
2790 /*******************************************************************
2791 * SetWindowTextA (USER32.@)
2792 * SetWindowText (USER32.@)
2794 BOOL WINAPI DECLSPEC_HOTPATCH SetWindowTextA( HWND hwnd, LPCSTR lpString )
2796 if (is_broadcast(hwnd))
2798 SetLastError( ERROR_INVALID_PARAMETER );
2799 return FALSE;
2801 if (!WIN_IsCurrentProcess( hwnd ))
2802 WARN( "setting text %s of other process window %p should not use SendMessage\n",
2803 debugstr_a(lpString), hwnd );
2804 return (BOOL)SendMessageA( hwnd, WM_SETTEXT, 0, (LPARAM)lpString );
2808 /*******************************************************************
2809 * SetWindowTextW (USER32.@)
2811 BOOL WINAPI DECLSPEC_HOTPATCH SetWindowTextW( HWND hwnd, LPCWSTR lpString )
2813 if (is_broadcast(hwnd))
2815 SetLastError( ERROR_INVALID_PARAMETER );
2816 return FALSE;
2818 if (!WIN_IsCurrentProcess( hwnd ))
2819 WARN( "setting text %s of other process window %p should not use SendMessage\n",
2820 debugstr_w(lpString), hwnd );
2821 return (BOOL)SendMessageW( hwnd, WM_SETTEXT, 0, (LPARAM)lpString );
2825 /*******************************************************************
2826 * GetWindowTextLengthA (USER32.@)
2828 INT WINAPI GetWindowTextLengthA( HWND hwnd )
2830 return SendMessageA( hwnd, WM_GETTEXTLENGTH, 0, 0 );
2833 /*******************************************************************
2834 * GetWindowTextLengthW (USER32.@)
2836 INT WINAPI GetWindowTextLengthW( HWND hwnd )
2838 return SendMessageW( hwnd, WM_GETTEXTLENGTH, 0, 0 );
2842 /*******************************************************************
2843 * IsWindow (USER32.@)
2845 BOOL WINAPI IsWindow( HWND hwnd )
2847 WND *ptr;
2848 BOOL ret;
2850 if (!(ptr = WIN_GetPtr( hwnd ))) return FALSE;
2851 if (ptr == WND_DESKTOP) return TRUE;
2853 if (ptr != WND_OTHER_PROCESS)
2855 WIN_ReleasePtr( ptr );
2856 return TRUE;
2859 /* check other processes */
2860 SERVER_START_REQ( get_window_info )
2862 req->handle = wine_server_user_handle( hwnd );
2863 ret = !wine_server_call_err( req );
2865 SERVER_END_REQ;
2866 return ret;
2870 /***********************************************************************
2871 * GetWindowThreadProcessId (USER32.@)
2873 DWORD WINAPI GetWindowThreadProcessId( HWND hwnd, LPDWORD process )
2875 WND *ptr;
2876 DWORD tid = 0;
2878 if (!(ptr = WIN_GetPtr( hwnd )))
2880 SetLastError( ERROR_INVALID_WINDOW_HANDLE);
2881 return 0;
2884 if (ptr != WND_OTHER_PROCESS && ptr != WND_DESKTOP)
2886 /* got a valid window */
2887 tid = ptr->tid;
2888 if (process) *process = GetCurrentProcessId();
2889 WIN_ReleasePtr( ptr );
2890 return tid;
2893 /* check other processes */
2894 SERVER_START_REQ( get_window_info )
2896 req->handle = wine_server_user_handle( hwnd );
2897 if (!wine_server_call_err( req ))
2899 tid = (DWORD)reply->tid;
2900 if (process) *process = (DWORD)reply->pid;
2903 SERVER_END_REQ;
2904 return tid;
2908 /*****************************************************************
2909 * GetParent (USER32.@)
2911 HWND WINAPI GetParent( HWND hwnd )
2913 WND *wndPtr;
2914 HWND retvalue = 0;
2916 if (!(wndPtr = WIN_GetPtr( hwnd )))
2918 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2919 return 0;
2921 if (wndPtr == WND_DESKTOP) return 0;
2922 if (wndPtr == WND_OTHER_PROCESS)
2924 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
2925 if (style & (WS_POPUP | WS_CHILD))
2927 SERVER_START_REQ( get_window_tree )
2929 req->handle = wine_server_user_handle( hwnd );
2930 if (!wine_server_call_err( req ))
2932 if (style & WS_POPUP) retvalue = wine_server_ptr_handle( reply->owner );
2933 else if (style & WS_CHILD) retvalue = wine_server_ptr_handle( reply->parent );
2936 SERVER_END_REQ;
2939 else
2941 if (wndPtr->dwStyle & WS_POPUP) retvalue = wndPtr->owner;
2942 else if (wndPtr->dwStyle & WS_CHILD) retvalue = wndPtr->parent;
2943 WIN_ReleasePtr( wndPtr );
2945 return retvalue;
2949 /*****************************************************************
2950 * GetAncestor (USER32.@)
2952 HWND WINAPI GetAncestor( HWND hwnd, UINT type )
2954 WND *win;
2955 HWND *list, ret = 0;
2957 switch(type)
2959 case GA_PARENT:
2960 if (!(win = WIN_GetPtr( hwnd )))
2962 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2963 return 0;
2965 if (win == WND_DESKTOP) return 0;
2966 if (win != WND_OTHER_PROCESS)
2968 ret = win->parent;
2969 WIN_ReleasePtr( win );
2971 else /* need to query the server */
2973 SERVER_START_REQ( get_window_tree )
2975 req->handle = wine_server_user_handle( hwnd );
2976 if (!wine_server_call_err( req )) ret = wine_server_ptr_handle( reply->parent );
2978 SERVER_END_REQ;
2980 break;
2982 case GA_ROOT:
2983 if (!(list = list_window_parents( hwnd ))) return 0;
2985 if (!list[0] || !list[1]) ret = WIN_GetFullHandle( hwnd ); /* top-level window */
2986 else
2988 int count = 2;
2989 while (list[count]) count++;
2990 ret = list[count - 2]; /* get the one before the desktop */
2992 HeapFree( GetProcessHeap(), 0, list );
2993 break;
2995 case GA_ROOTOWNER:
2996 if (is_desktop_window( hwnd )) return 0;
2997 ret = WIN_GetFullHandle( hwnd );
2998 for (;;)
3000 HWND parent = GetParent( ret );
3001 if (!parent) break;
3002 ret = parent;
3004 break;
3006 return ret;
3010 /*****************************************************************
3011 * SetParent (USER32.@)
3013 HWND WINAPI SetParent( HWND hwnd, HWND parent )
3015 HWND full_handle;
3016 HWND old_parent = 0;
3017 BOOL was_visible;
3018 WND *wndPtr;
3019 POINT pt;
3020 BOOL ret;
3022 TRACE("(%p %p)\n", hwnd, parent);
3024 if (is_broadcast(hwnd) || is_broadcast(parent))
3026 SetLastError(ERROR_INVALID_PARAMETER);
3027 return 0;
3030 if (!parent) parent = GetDesktopWindow();
3031 else if (parent == HWND_MESSAGE) parent = get_hwnd_message_parent();
3032 else parent = WIN_GetFullHandle( parent );
3034 if (!IsWindow( parent ))
3036 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3037 return 0;
3040 /* Some applications try to set a child as a parent */
3041 if (IsChild(hwnd, parent))
3043 SetLastError( ERROR_INVALID_PARAMETER );
3044 return 0;
3047 if (!(full_handle = WIN_IsCurrentThread( hwnd )))
3048 return (HWND)SendMessageW( hwnd, WM_WINE_SETPARENT, (WPARAM)parent, 0 );
3050 if (full_handle == parent)
3052 SetLastError( ERROR_INVALID_PARAMETER );
3053 return 0;
3056 /* Windows hides the window first, then shows it again
3057 * including the WM_SHOWWINDOW messages and all */
3058 was_visible = ShowWindow( hwnd, SW_HIDE );
3060 wndPtr = WIN_GetPtr( hwnd );
3061 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return 0;
3063 pt.x = wndPtr->rectWindow.left;
3064 pt.y = wndPtr->rectWindow.top;
3066 SERVER_START_REQ( set_parent )
3068 req->handle = wine_server_user_handle( hwnd );
3069 req->parent = wine_server_user_handle( parent );
3070 if ((ret = !wine_server_call( req )))
3072 old_parent = wine_server_ptr_handle( reply->old_parent );
3073 wndPtr->parent = parent = wine_server_ptr_handle( reply->full_parent );
3077 SERVER_END_REQ;
3078 WIN_ReleasePtr( wndPtr );
3079 if (!ret) return 0;
3081 USER_Driver->pSetParent( full_handle, parent, old_parent );
3083 /* SetParent additionally needs to make hwnd the topmost window
3084 in the x-order and send the expected WM_WINDOWPOSCHANGING and
3085 WM_WINDOWPOSCHANGED notification messages.
3087 SetWindowPos( hwnd, HWND_TOP, pt.x, pt.y, 0, 0, SWP_NOSIZE );
3089 if (was_visible) ShowWindow( hwnd, SW_SHOW );
3091 return old_parent;
3095 /*******************************************************************
3096 * IsChild (USER32.@)
3098 BOOL WINAPI IsChild( HWND parent, HWND child )
3100 HWND *list;
3101 int i;
3102 BOOL ret = FALSE;
3104 if (!(GetWindowLongW( child, GWL_STYLE ) & WS_CHILD)) return FALSE;
3105 if (!(list = list_window_parents( child ))) return FALSE;
3106 parent = WIN_GetFullHandle( parent );
3107 for (i = 0; list[i]; i++)
3109 if (list[i] == parent)
3111 ret = list[i] && list[i+1];
3112 break;
3114 if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_CHILD)) break;
3116 HeapFree( GetProcessHeap(), 0, list );
3117 return ret;
3121 /***********************************************************************
3122 * IsWindowVisible (USER32.@)
3124 BOOL WINAPI IsWindowVisible( HWND hwnd )
3126 HWND *list;
3127 BOOL retval = TRUE;
3128 int i;
3130 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)) return FALSE;
3131 if (!(list = list_window_parents( hwnd ))) return TRUE;
3132 if (list[0])
3134 for (i = 0; list[i+1]; i++)
3135 if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_VISIBLE)) break;
3136 retval = !list[i+1] && (list[i] == GetDesktopWindow()); /* top message window isn't visible */
3138 HeapFree( GetProcessHeap(), 0, list );
3139 return retval;
3143 /***********************************************************************
3144 * WIN_IsWindowDrawable
3146 * hwnd is drawable when it is visible, all parents are not
3147 * minimized, and it is itself not minimized unless we are
3148 * trying to draw its default class icon.
3150 BOOL WIN_IsWindowDrawable( HWND hwnd, BOOL icon )
3152 HWND *list;
3153 BOOL retval = TRUE;
3154 int i;
3155 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
3157 if (!(style & WS_VISIBLE)) return FALSE;
3158 if ((style & WS_MINIMIZE) && icon && GetClassLongPtrW( hwnd, GCLP_HICON )) return FALSE;
3160 if (!(list = list_window_parents( hwnd ))) return TRUE;
3161 if (list[0])
3163 for (i = 0; list[i+1]; i++)
3164 if ((GetWindowLongW( list[i], GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != WS_VISIBLE)
3165 break;
3166 retval = !list[i+1] && (list[i] == GetDesktopWindow()); /* top message window isn't visible */
3168 HeapFree( GetProcessHeap(), 0, list );
3169 return retval;
3173 /*******************************************************************
3174 * GetTopWindow (USER32.@)
3176 HWND WINAPI GetTopWindow( HWND hwnd )
3178 if (!hwnd) hwnd = GetDesktopWindow();
3179 return GetWindow( hwnd, GW_CHILD );
3183 /*******************************************************************
3184 * GetWindow (USER32.@)
3186 HWND WINAPI GetWindow( HWND hwnd, UINT rel )
3188 HWND retval = 0;
3190 if (rel == GW_OWNER) /* this one may be available locally */
3192 WND *wndPtr = WIN_GetPtr( hwnd );
3193 if (!wndPtr)
3195 SetLastError( ERROR_INVALID_HANDLE );
3196 return 0;
3198 if (wndPtr == WND_DESKTOP) return 0;
3199 if (wndPtr != WND_OTHER_PROCESS)
3201 retval = wndPtr->owner;
3202 WIN_ReleasePtr( wndPtr );
3203 return retval;
3205 /* else fall through to server call */
3208 SERVER_START_REQ( get_window_tree )
3210 req->handle = wine_server_user_handle( hwnd );
3211 if (!wine_server_call_err( req ))
3213 switch(rel)
3215 case GW_HWNDFIRST:
3216 retval = wine_server_ptr_handle( reply->first_sibling );
3217 break;
3218 case GW_HWNDLAST:
3219 retval = wine_server_ptr_handle( reply->last_sibling );
3220 break;
3221 case GW_HWNDNEXT:
3222 retval = wine_server_ptr_handle( reply->next_sibling );
3223 break;
3224 case GW_HWNDPREV:
3225 retval = wine_server_ptr_handle( reply->prev_sibling );
3226 break;
3227 case GW_OWNER:
3228 retval = wine_server_ptr_handle( reply->owner );
3229 break;
3230 case GW_CHILD:
3231 retval = wine_server_ptr_handle( reply->first_child );
3232 break;
3236 SERVER_END_REQ;
3237 return retval;
3241 /*******************************************************************
3242 * ShowOwnedPopups (USER32.@)
3244 BOOL WINAPI ShowOwnedPopups( HWND owner, BOOL fShow )
3246 int count = 0;
3247 HWND *win_array = WIN_ListChildren( GetDesktopWindow() );
3249 if (!win_array) return TRUE;
3251 while (win_array[count]) count++;
3252 while (--count >= 0)
3254 if (GetWindow( win_array[count], GW_OWNER ) != owner) continue;
3255 if (fShow)
3257 if (win_get_flags( win_array[count] ) & WIN_NEEDS_SHOW_OWNEDPOPUP)
3258 /* In Windows, ShowOwnedPopups(TRUE) generates
3259 * WM_SHOWWINDOW messages with SW_PARENTOPENING,
3260 * regardless of the state of the owner
3262 SendMessageW(win_array[count], WM_SHOWWINDOW, SW_SHOWNORMAL, SW_PARENTOPENING);
3264 else
3266 if (GetWindowLongW( win_array[count], GWL_STYLE ) & WS_VISIBLE)
3267 /* In Windows, ShowOwnedPopups(FALSE) generates
3268 * WM_SHOWWINDOW messages with SW_PARENTCLOSING,
3269 * regardless of the state of the owner
3271 SendMessageW(win_array[count], WM_SHOWWINDOW, SW_HIDE, SW_PARENTCLOSING);
3274 HeapFree( GetProcessHeap(), 0, win_array );
3275 return TRUE;
3279 /*******************************************************************
3280 * GetLastActivePopup (USER32.@)
3282 HWND WINAPI GetLastActivePopup( HWND hwnd )
3284 HWND retval = hwnd;
3286 SERVER_START_REQ( get_window_info )
3288 req->handle = wine_server_user_handle( hwnd );
3289 if (!wine_server_call_err( req )) retval = wine_server_ptr_handle( reply->last_active );
3291 SERVER_END_REQ;
3292 return retval;
3296 /*******************************************************************
3297 * WIN_ListChildren
3299 * Build an array of the children of a given window. The array must be
3300 * freed with HeapFree. Returns NULL when no windows are found.
3302 HWND *WIN_ListChildren( HWND hwnd )
3304 if (!hwnd)
3306 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3307 return NULL;
3309 return list_window_children( 0, hwnd, NULL, 0 );
3313 /*******************************************************************
3314 * EnumWindows (USER32.@)
3316 BOOL WINAPI EnumWindows( WNDENUMPROC lpEnumFunc, LPARAM lParam )
3318 HWND *list;
3319 BOOL ret = TRUE;
3320 int i;
3322 USER_CheckNotLock();
3324 /* We have to build a list of all windows first, to avoid */
3325 /* unpleasant side-effects, for instance if the callback */
3326 /* function changes the Z-order of the windows. */
3328 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return TRUE;
3330 /* Now call the callback function for every window */
3332 for (i = 0; list[i]; i++)
3334 /* Make sure that the window still exists */
3335 if (!IsWindow( list[i] )) continue;
3336 if (!(ret = lpEnumFunc( list[i], lParam ))) break;
3338 HeapFree( GetProcessHeap(), 0, list );
3339 return ret;
3343 /**********************************************************************
3344 * EnumThreadWindows (USER32.@)
3346 BOOL WINAPI EnumThreadWindows( DWORD id, WNDENUMPROC func, LPARAM lParam )
3348 HWND *list;
3349 int i;
3350 BOOL ret = TRUE;
3352 USER_CheckNotLock();
3354 if (!(list = list_window_children( 0, GetDesktopWindow(), NULL, id ))) return TRUE;
3356 /* Now call the callback function for every window */
3358 for (i = 0; list[i]; i++)
3359 if (!(ret = func( list[i], lParam ))) break;
3360 HeapFree( GetProcessHeap(), 0, list );
3361 return ret;
3365 /***********************************************************************
3366 * EnumDesktopWindows (USER32.@)
3368 BOOL WINAPI EnumDesktopWindows( HDESK desktop, WNDENUMPROC func, LPARAM lparam )
3370 HWND *list;
3371 int i;
3373 USER_CheckNotLock();
3375 if (!(list = list_window_children( desktop, 0, NULL, 0 ))) return TRUE;
3377 for (i = 0; list[i]; i++)
3378 if (!func( list[i], lparam )) break;
3379 HeapFree( GetProcessHeap(), 0, list );
3380 return TRUE;
3384 #ifdef __i386__
3385 /* Some apps pass a non-stdcall proc to EnumChildWindows,
3386 * so we need a small assembly wrapper to call the proc.
3388 extern LRESULT enum_callback_wrapper( WNDENUMPROC proc, HWND hwnd, LPARAM lparam );
3389 __ASM_GLOBAL_FUNC( enum_callback_wrapper,
3390 "pushl %ebp\n\t"
3391 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
3392 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
3393 "movl %esp,%ebp\n\t"
3394 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
3395 "pushl 16(%ebp)\n\t"
3396 "pushl 12(%ebp)\n\t"
3397 "call *8(%ebp)\n\t"
3398 "leave\n\t"
3399 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
3400 __ASM_CFI(".cfi_same_value %ebp\n\t")
3401 "ret" )
3402 #else
3403 static inline LRESULT enum_callback_wrapper( WNDENUMPROC proc, HWND hwnd, LPARAM lparam )
3405 return proc( hwnd, lparam );
3407 #endif /* __i386__ */
3409 /**********************************************************************
3410 * WIN_EnumChildWindows
3412 * Helper function for EnumChildWindows().
3414 static BOOL WIN_EnumChildWindows( HWND *list, WNDENUMPROC func, LPARAM lParam )
3416 HWND *childList;
3417 BOOL ret = FALSE;
3419 for ( ; *list; list++)
3421 /* Make sure that the window still exists */
3422 if (!IsWindow( *list )) continue;
3423 /* Build children list first */
3424 childList = WIN_ListChildren( *list );
3426 ret = enum_callback_wrapper( func, *list, lParam );
3428 if (childList)
3430 if (ret) ret = WIN_EnumChildWindows( childList, func, lParam );
3431 HeapFree( GetProcessHeap(), 0, childList );
3433 if (!ret) return FALSE;
3435 return TRUE;
3439 /**********************************************************************
3440 * EnumChildWindows (USER32.@)
3442 BOOL WINAPI EnumChildWindows( HWND parent, WNDENUMPROC func, LPARAM lParam )
3444 HWND *list;
3445 BOOL ret;
3447 USER_CheckNotLock();
3449 if (!(list = WIN_ListChildren( parent ))) return FALSE;
3450 ret = WIN_EnumChildWindows( list, func, lParam );
3451 HeapFree( GetProcessHeap(), 0, list );
3452 return ret;
3456 /*******************************************************************
3457 * AnyPopup (USER32.@)
3459 BOOL WINAPI AnyPopup(void)
3461 int i;
3462 BOOL retvalue;
3463 HWND *list = WIN_ListChildren( GetDesktopWindow() );
3465 if (!list) return FALSE;
3466 for (i = 0; list[i]; i++)
3468 if (IsWindowVisible( list[i] ) && GetWindow( list[i], GW_OWNER )) break;
3470 retvalue = (list[i] != 0);
3471 HeapFree( GetProcessHeap(), 0, list );
3472 return retvalue;
3476 /*******************************************************************
3477 * FlashWindow (USER32.@)
3479 BOOL WINAPI FlashWindow( HWND hWnd, BOOL bInvert )
3481 FLASHWINFO finfo;
3483 finfo.cbSize = sizeof(FLASHWINFO);
3484 finfo.dwFlags = bInvert ? FLASHW_ALL : FLASHW_STOP;
3485 finfo.uCount = 1;
3486 finfo.dwTimeout = 0;
3487 finfo.hwnd = hWnd;
3488 return FlashWindowEx( &finfo );
3491 /*******************************************************************
3492 * FlashWindowEx (USER32.@)
3494 BOOL WINAPI FlashWindowEx( PFLASHWINFO pfinfo )
3496 WND *wndPtr;
3498 TRACE( "%p\n", pfinfo );
3500 if (!pfinfo)
3502 SetLastError( ERROR_NOACCESS );
3503 return FALSE;
3506 if (!pfinfo->hwnd || pfinfo->cbSize != sizeof(FLASHWINFO) || !IsWindow( pfinfo->hwnd ))
3508 SetLastError( ERROR_INVALID_PARAMETER );
3509 return FALSE;
3511 FIXME( "%p - semi-stub\n", pfinfo );
3513 if (IsIconic( pfinfo->hwnd ))
3515 RedrawWindow( pfinfo->hwnd, 0, 0, RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW | RDW_FRAME );
3517 wndPtr = WIN_GetPtr( pfinfo->hwnd );
3518 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
3519 if (pfinfo->dwFlags && !(wndPtr->flags & WIN_NCACTIVATED))
3521 wndPtr->flags |= WIN_NCACTIVATED;
3523 else
3525 wndPtr->flags &= ~WIN_NCACTIVATED;
3527 WIN_ReleasePtr( wndPtr );
3528 USER_Driver->pFlashWindowEx( pfinfo );
3529 return TRUE;
3531 else
3533 WPARAM wparam;
3534 HWND hwnd = pfinfo->hwnd;
3536 wndPtr = WIN_GetPtr( hwnd );
3537 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
3538 hwnd = wndPtr->obj.handle; /* make it a full handle */
3540 if (pfinfo->dwFlags) wparam = !(wndPtr->flags & WIN_NCACTIVATED);
3541 else wparam = (hwnd == GetForegroundWindow());
3543 WIN_ReleasePtr( wndPtr );
3544 SendMessageW( hwnd, WM_NCACTIVATE, wparam, 0 );
3545 USER_Driver->pFlashWindowEx( pfinfo );
3546 return wparam;
3550 /*******************************************************************
3551 * GetWindowContextHelpId (USER32.@)
3553 DWORD WINAPI GetWindowContextHelpId( HWND hwnd )
3555 DWORD retval;
3556 WND *wnd = WIN_GetPtr( hwnd );
3557 if (!wnd || wnd == WND_DESKTOP) return 0;
3558 if (wnd == WND_OTHER_PROCESS)
3560 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
3561 return 0;
3563 retval = wnd->helpContext;
3564 WIN_ReleasePtr( wnd );
3565 return retval;
3569 /*******************************************************************
3570 * SetWindowContextHelpId (USER32.@)
3572 BOOL WINAPI SetWindowContextHelpId( HWND hwnd, DWORD id )
3574 WND *wnd = WIN_GetPtr( hwnd );
3575 if (!wnd || wnd == WND_DESKTOP) return FALSE;
3576 if (wnd == WND_OTHER_PROCESS)
3578 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
3579 return FALSE;
3581 wnd->helpContext = id;
3582 WIN_ReleasePtr( wnd );
3583 return TRUE;
3587 /*******************************************************************
3588 * DragDetect (USER32.@)
3590 BOOL WINAPI DragDetect( HWND hWnd, POINT pt )
3592 MSG msg;
3593 RECT rect;
3594 WORD wDragWidth = GetSystemMetrics(SM_CXDRAG);
3595 WORD wDragHeight= GetSystemMetrics(SM_CYDRAG);
3597 SetRect(&rect, pt.x - wDragWidth, pt.y - wDragHeight, pt.x + wDragWidth, pt.y + wDragHeight);
3599 SetCapture(hWnd);
3601 while(1)
3603 while (PeekMessageW( &msg, 0, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE ))
3605 if( msg.message == WM_LBUTTONUP )
3607 ReleaseCapture();
3608 return FALSE;
3610 if( msg.message == WM_MOUSEMOVE )
3612 POINT tmp;
3613 tmp.x = (short)LOWORD(msg.lParam);
3614 tmp.y = (short)HIWORD(msg.lParam);
3615 if( !PtInRect( &rect, tmp ))
3617 ReleaseCapture();
3618 return TRUE;
3622 WaitMessage();
3624 return FALSE;
3627 /******************************************************************************
3628 * GetWindowModuleFileNameA (USER32.@)
3630 UINT WINAPI GetWindowModuleFileNameA( HWND hwnd, LPSTR module, UINT size )
3632 WND *win;
3633 HINSTANCE hinst;
3635 TRACE( "%p, %p, %u\n", hwnd, module, size );
3637 win = WIN_GetPtr( hwnd );
3638 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP)
3640 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3641 return 0;
3643 hinst = win->hInstance;
3644 WIN_ReleasePtr( win );
3646 return GetModuleFileNameA( hinst, module, size );
3649 /******************************************************************************
3650 * GetWindowModuleFileNameW (USER32.@)
3652 UINT WINAPI GetWindowModuleFileNameW( HWND hwnd, LPWSTR module, UINT size )
3654 WND *win;
3655 HINSTANCE hinst;
3657 TRACE( "%p, %p, %u\n", hwnd, module, size );
3659 win = WIN_GetPtr( hwnd );
3660 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP)
3662 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3663 return 0;
3665 hinst = win->hInstance;
3666 WIN_ReleasePtr( win );
3668 return GetModuleFileNameW( hinst, module, size );
3671 /******************************************************************************
3672 * GetWindowInfo (USER32.@)
3674 * Note: tests show that Windows doesn't check cbSize of the structure.
3676 BOOL WINAPI DECLSPEC_HOTPATCH GetWindowInfo( HWND hwnd, PWINDOWINFO pwi)
3678 if (!pwi) return FALSE;
3679 if (!WIN_GetRectangles( hwnd, COORDS_SCREEN, &pwi->rcWindow, &pwi->rcClient )) return FALSE;
3681 pwi->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
3682 pwi->dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
3683 pwi->dwWindowStatus = ((GetActiveWindow() == hwnd) ? WS_ACTIVECAPTION : 0);
3685 pwi->cxWindowBorders = pwi->rcClient.left - pwi->rcWindow.left;
3686 pwi->cyWindowBorders = pwi->rcWindow.bottom - pwi->rcClient.bottom;
3688 pwi->atomWindowType = GetClassLongW( hwnd, GCW_ATOM );
3689 pwi->wCreatorVersion = 0x0400;
3691 return TRUE;
3694 /******************************************************************************
3695 * SwitchDesktop (USER32.@)
3697 * NOTES: Sets the current input or interactive desktop.
3699 BOOL WINAPI SwitchDesktop( HDESK hDesktop)
3701 FIXME("(hwnd %p) stub!\n", hDesktop);
3702 return TRUE;
3706 /***********************************************************************
3707 * __wine_set_pixel_format
3709 BOOL CDECL __wine_set_pixel_format( HWND hwnd, int format )
3711 WND *win = WIN_GetPtr( hwnd );
3713 if (!win || win == WND_DESKTOP || win == WND_OTHER_PROCESS)
3715 WARN( "setting format %d on win %p not supported\n", format, hwnd );
3716 return FALSE;
3718 win->pixel_format = format;
3719 WIN_ReleasePtr( win );
3721 update_window_state( hwnd );
3722 return TRUE;
3726 /*****************************************************************************
3727 * SetLayeredWindowAttributes (USER32.@)
3729 BOOL WINAPI SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
3731 BOOL ret;
3733 TRACE("(%p,%08x,%d,%x)\n", hwnd, key, alpha, flags);
3735 SERVER_START_REQ( set_window_layered_info )
3737 req->handle = wine_server_user_handle( hwnd );
3738 req->color_key = key;
3739 req->alpha = alpha;
3740 req->flags = flags;
3741 ret = !wine_server_call_err( req );
3743 SERVER_END_REQ;
3745 if (ret)
3747 USER_Driver->pSetLayeredWindowAttributes( hwnd, key, alpha, flags );
3748 update_window_state( hwnd );
3751 return ret;
3755 /*****************************************************************************
3756 * GetLayeredWindowAttributes (USER32.@)
3758 BOOL WINAPI GetLayeredWindowAttributes( HWND hwnd, COLORREF *key, BYTE *alpha, DWORD *flags )
3760 BOOL ret;
3762 SERVER_START_REQ( get_window_layered_info )
3764 req->handle = wine_server_user_handle( hwnd );
3765 if ((ret = !wine_server_call_err( req )))
3767 if (key) *key = reply->color_key;
3768 if (alpha) *alpha = reply->alpha;
3769 if (flags) *flags = reply->flags;
3772 SERVER_END_REQ;
3774 return ret;
3778 /*****************************************************************************
3779 * UpdateLayeredWindowIndirect (USER32.@)
3781 BOOL WINAPI UpdateLayeredWindowIndirect( HWND hwnd, const UPDATELAYEREDWINDOWINFO *info )
3783 DWORD flags = SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW;
3784 RECT window_rect, client_rect;
3785 SIZE offset;
3787 if (!info ||
3788 info->cbSize != sizeof(*info) ||
3789 info->dwFlags & ~(ULW_COLORKEY | ULW_ALPHA | ULW_OPAQUE | ULW_EX_NORESIZE) ||
3790 !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED) ||
3791 GetLayeredWindowAttributes( hwnd, NULL, NULL, NULL ))
3793 SetLastError( ERROR_INVALID_PARAMETER );
3794 return FALSE;
3797 WIN_GetRectangles( hwnd, COORDS_PARENT, &window_rect, &client_rect );
3799 if (info->pptDst)
3801 offset.cx = info->pptDst->x - window_rect.left;
3802 offset.cy = info->pptDst->y - window_rect.top;
3803 OffsetRect( &client_rect, offset.cx, offset.cy );
3804 OffsetRect( &window_rect, offset.cx, offset.cy );
3805 flags &= ~SWP_NOMOVE;
3807 if (info->psize)
3809 offset.cx = info->psize->cx - (window_rect.right - window_rect.left);
3810 offset.cy = info->psize->cy - (window_rect.bottom - window_rect.top);
3811 if (info->psize->cx <= 0 || info->psize->cy <= 0)
3813 SetLastError( ERROR_INVALID_PARAMETER );
3814 return FALSE;
3816 if ((info->dwFlags & ULW_EX_NORESIZE) && (offset.cx || offset.cy))
3818 SetLastError( ERROR_INCORRECT_SIZE );
3819 return FALSE;
3821 client_rect.right += offset.cx;
3822 client_rect.bottom += offset.cy;
3823 window_rect.right += offset.cx;
3824 window_rect.bottom += offset.cy;
3825 flags &= ~SWP_NOSIZE;
3828 TRACE( "window %p win %s client %s\n", hwnd,
3829 wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(&client_rect) );
3831 if (!USER_Driver->pUpdateLayeredWindow( hwnd, info, &window_rect )) return FALSE;
3833 set_window_pos( hwnd, 0, flags, &window_rect, &client_rect, NULL );
3834 return TRUE;
3838 /*****************************************************************************
3839 * UpdateLayeredWindow (USER32.@)
3841 BOOL WINAPI UpdateLayeredWindow( HWND hwnd, HDC hdcDst, POINT *pptDst, SIZE *psize,
3842 HDC hdcSrc, POINT *pptSrc, COLORREF crKey, BLENDFUNCTION *pblend,
3843 DWORD flags)
3845 UPDATELAYEREDWINDOWINFO info;
3847 if (flags & ULW_EX_NORESIZE) /* only valid for UpdateLayeredWindowIndirect */
3849 SetLastError( ERROR_INVALID_PARAMETER );
3850 return FALSE;
3852 info.cbSize = sizeof(info);
3853 info.hdcDst = hdcDst;
3854 info.pptDst = pptDst;
3855 info.psize = psize;
3856 info.hdcSrc = hdcSrc;
3857 info.pptSrc = pptSrc;
3858 info.crKey = crKey;
3859 info.pblend = pblend;
3860 info.dwFlags = flags;
3861 info.prcDirty = NULL;
3862 return UpdateLayeredWindowIndirect( hwnd, &info );
3866 /******************************************************************************
3867 * GetProcessDefaultLayout [USER32.@]
3869 * Gets the default layout for parentless windows.
3871 BOOL WINAPI GetProcessDefaultLayout( DWORD *layout )
3873 if (!layout)
3875 SetLastError( ERROR_NOACCESS );
3876 return FALSE;
3878 if (process_layout == ~0u)
3880 static const WCHAR translationW[] = { '\\','V','a','r','F','i','l','e','I','n','f','o',
3881 '\\','T','r','a','n','s','l','a','t','i','o','n', 0 };
3882 static const WCHAR filedescW[] = { '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
3883 '\\','%','0','4','x','%','0','4','x',
3884 '\\','F','i','l','e','D','e','s','c','r','i','p','t','i','o','n',0 };
3885 WCHAR *str, buffer[MAX_PATH];
3886 DWORD i, len, version_layout = 0;
3887 DWORD user_lang = GetUserDefaultLangID();
3888 DWORD *languages;
3889 void *data = NULL;
3891 GetModuleFileNameW( 0, buffer, MAX_PATH );
3892 if (!(len = GetFileVersionInfoSizeW( buffer, NULL ))) goto done;
3893 if (!(data = HeapAlloc( GetProcessHeap(), 0, len ))) goto done;
3894 if (!GetFileVersionInfoW( buffer, 0, len, data )) goto done;
3895 if (!VerQueryValueW( data, translationW, (void **)&languages, &len ) || !len) goto done;
3897 len /= sizeof(DWORD);
3898 for (i = 0; i < len; i++) if (LOWORD(languages[i]) == user_lang) break;
3899 if (i == len) /* try neutral language */
3900 for (i = 0; i < len; i++)
3901 if (LOWORD(languages[i]) == MAKELANGID( PRIMARYLANGID(user_lang), SUBLANG_NEUTRAL )) break;
3902 if (i == len) i = 0; /* default to the first one */
3904 sprintfW( buffer, filedescW, LOWORD(languages[i]), HIWORD(languages[i]) );
3905 if (!VerQueryValueW( data, buffer, (void **)&str, &len )) goto done;
3906 TRACE( "found description %s\n", debugstr_w( str ));
3907 if (str[0] == 0x200e && str[1] == 0x200e) version_layout = LAYOUT_RTL;
3909 done:
3910 HeapFree( GetProcessHeap(), 0, data );
3911 process_layout = version_layout;
3913 *layout = process_layout;
3914 return TRUE;
3918 /******************************************************************************
3919 * SetProcessDefaultLayout [USER32.@]
3921 * Sets the default layout for parentless windows.
3923 BOOL WINAPI SetProcessDefaultLayout( DWORD layout )
3925 process_layout = layout;
3926 return TRUE;
3930 /* 64bit versions */
3932 #ifdef GetWindowLongPtrW
3933 #undef GetWindowLongPtrW
3934 #endif
3936 #ifdef GetWindowLongPtrA
3937 #undef GetWindowLongPtrA
3938 #endif
3940 #ifdef SetWindowLongPtrW
3941 #undef SetWindowLongPtrW
3942 #endif
3944 #ifdef SetWindowLongPtrA
3945 #undef SetWindowLongPtrA
3946 #endif
3948 /*****************************************************************************
3949 * GetWindowLongPtrW (USER32.@)
3951 LONG_PTR WINAPI GetWindowLongPtrW( HWND hwnd, INT offset )
3953 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG_PTR), TRUE );
3956 /*****************************************************************************
3957 * GetWindowLongPtrA (USER32.@)
3959 LONG_PTR WINAPI GetWindowLongPtrA( HWND hwnd, INT offset )
3961 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG_PTR), FALSE );
3964 /*****************************************************************************
3965 * SetWindowLongPtrW (USER32.@)
3967 LONG_PTR WINAPI SetWindowLongPtrW( HWND hwnd, INT offset, LONG_PTR newval )
3969 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG_PTR), newval, TRUE );
3972 /*****************************************************************************
3973 * SetWindowLongPtrA (USER32.@)
3975 LONG_PTR WINAPI SetWindowLongPtrA( HWND hwnd, INT offset, LONG_PTR newval )
3977 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG_PTR), newval, FALSE );
3980 /*****************************************************************************
3981 * RegisterTouchWindow (USER32.@)
3983 BOOL WINAPI RegisterTouchWindow(HWND hwnd, ULONG flags)
3985 FIXME("(%p %08x): stub\n", hwnd, flags);
3986 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
3987 return FALSE;
3990 /*****************************************************************************
3991 * UnregisterTouchWindow (USER32.@)
3993 BOOL WINAPI UnregisterTouchWindow(HWND hwnd)
3995 FIXME("(%p): stub\n", hwnd);
3996 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
3997 return FALSE;
4000 /*****************************************************************************
4001 * CloseTouchInputHandle (USER32.@)
4003 BOOL WINAPI CloseTouchInputHandle(HTOUCHINPUT handle)
4005 FIXME("(%p): stub\n", handle);
4006 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
4007 return FALSE;
4010 /*****************************************************************************
4011 * GetTouchInputInfo (USER32.@)
4013 BOOL WINAPI GetTouchInputInfo(HTOUCHINPUT handle, UINT count, TOUCHINPUT *ptr, int size)
4015 FIXME("(%p %u %p %u): stub\n", handle, count, ptr, size);
4016 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
4017 return FALSE;
4020 /*****************************************************************************
4021 * GetGestureInfo (USER32.@)
4023 BOOL WINAPI GetGestureInfo(HGESTUREINFO handle, PGESTUREINFO ptr)
4025 FIXME("(%p %p): stub\n", handle, ptr);
4026 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
4027 return FALSE;