gdi32: Reimplement Ellipse in paths to avoid calling imprecise arc helper functions.
[wine.git] / dlls / user32 / win.c
blob9051f7a344b94779924526f10055fc5cbc100674
1 /*
2 * Window related functions
4 * Copyright 1993, 1994 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winver.h"
32 #include "wine/server.h"
33 #include "wine/unicode.h"
34 #include "win.h"
35 #include "user_private.h"
36 #include "controls.h"
37 #include "winerror.h"
38 #include "wine/gdi_driver.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(win);
43 #define NB_USER_HANDLES ((LAST_USER_HANDLE - FIRST_USER_HANDLE + 1) >> 1)
44 #define USER_HANDLE_TO_INDEX(hwnd) ((LOWORD(hwnd) - FIRST_USER_HANDLE) >> 1)
46 static DWORD process_layout = ~0u;
48 static struct list window_surfaces = LIST_INIT( window_surfaces );
50 static CRITICAL_SECTION surfaces_section;
51 static CRITICAL_SECTION_DEBUG critsect_debug =
53 0, 0, &surfaces_section,
54 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
55 0, 0, { (DWORD_PTR)(__FILE__ ": surfaces_section") }
57 static CRITICAL_SECTION surfaces_section = { &critsect_debug, -1, 0, 0, 0, 0 };
59 /**********************************************************************/
61 /* helper for Get/SetWindowLong */
62 static inline LONG_PTR get_win_data( const void *ptr, UINT size )
64 if (size == sizeof(WORD))
66 WORD ret;
67 memcpy( &ret, ptr, sizeof(ret) );
68 return ret;
70 else if (size == sizeof(DWORD))
72 DWORD ret;
73 memcpy( &ret, ptr, sizeof(ret) );
74 return ret;
76 else
78 LONG_PTR ret;
79 memcpy( &ret, ptr, sizeof(ret) );
80 return ret;
84 /* helper for Get/SetWindowLong */
85 static inline void set_win_data( void *ptr, LONG_PTR val, UINT size )
87 if (size == sizeof(WORD))
89 WORD newval = val;
90 memcpy( ptr, &newval, sizeof(newval) );
92 else if (size == sizeof(DWORD))
94 DWORD newval = val;
95 memcpy( ptr, &newval, sizeof(newval) );
97 else
99 memcpy( ptr, &val, sizeof(val) );
104 static void *user_handles[NB_USER_HANDLES];
106 /***********************************************************************
107 * alloc_user_handle
109 HANDLE alloc_user_handle( struct user_object *ptr, enum user_obj_type type )
111 HANDLE handle = 0;
113 SERVER_START_REQ( alloc_user_handle )
115 if (!wine_server_call_err( req )) handle = wine_server_ptr_handle( reply->handle );
117 SERVER_END_REQ;
119 if (handle)
121 UINT index = USER_HANDLE_TO_INDEX( handle );
123 assert( index < NB_USER_HANDLES );
124 ptr->handle = handle;
125 ptr->type = type;
126 InterlockedExchangePointer( &user_handles[index], ptr );
128 return handle;
132 /***********************************************************************
133 * get_user_handle_ptr
135 void *get_user_handle_ptr( HANDLE handle, enum user_obj_type type )
137 struct user_object *ptr;
138 WORD index = USER_HANDLE_TO_INDEX( handle );
140 if (index >= NB_USER_HANDLES) return NULL;
142 USER_Lock();
143 if ((ptr = user_handles[index]))
145 if (ptr->type == type &&
146 ((UINT)(UINT_PTR)ptr->handle == (UINT)(UINT_PTR)handle ||
147 !HIWORD(handle) || HIWORD(handle) == 0xffff))
148 return ptr;
149 ptr = NULL;
151 else ptr = OBJ_OTHER_PROCESS;
152 USER_Unlock();
153 return ptr;
157 /***********************************************************************
158 * release_user_handle_ptr
160 void release_user_handle_ptr( void *ptr )
162 assert( ptr && ptr != OBJ_OTHER_PROCESS );
163 USER_Unlock();
167 /***********************************************************************
168 * free_user_handle
170 void *free_user_handle( HANDLE handle, enum user_obj_type type )
172 struct user_object *ptr;
173 WORD index = USER_HANDLE_TO_INDEX( handle );
175 if ((ptr = get_user_handle_ptr( handle, type )) && ptr != OBJ_OTHER_PROCESS)
177 SERVER_START_REQ( free_user_handle )
179 req->handle = wine_server_user_handle( handle );
180 if (wine_server_call( req )) ptr = NULL;
181 else InterlockedCompareExchangePointer( &user_handles[index], NULL, ptr );
183 SERVER_END_REQ;
184 USER_Unlock();
186 return ptr;
190 /***********************************************************************
191 * create_window_handle
193 * Create a window handle with the server.
195 static WND *create_window_handle( HWND parent, HWND owner, LPCWSTR name,
196 HINSTANCE instance, BOOL unicode )
198 WORD index;
199 WND *win;
200 HWND handle = 0, full_parent = 0, full_owner = 0;
201 struct tagCLASS *class = NULL;
202 int extra_bytes = 0;
204 SERVER_START_REQ( create_window )
206 req->parent = wine_server_user_handle( parent );
207 req->owner = wine_server_user_handle( owner );
208 req->instance = wine_server_client_ptr( instance );
209 if (!(req->atom = get_int_atom_value( name )) && name)
210 wine_server_add_data( req, name, strlenW(name)*sizeof(WCHAR) );
211 if (!wine_server_call_err( req ))
213 handle = wine_server_ptr_handle( reply->handle );
214 full_parent = wine_server_ptr_handle( reply->parent );
215 full_owner = wine_server_ptr_handle( reply->owner );
216 extra_bytes = reply->extra;
217 class = wine_server_get_ptr( reply->class_ptr );
220 SERVER_END_REQ;
222 if (!handle)
224 WARN( "error %d creating window\n", GetLastError() );
225 return NULL;
228 if (!(win = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
229 sizeof(WND) + extra_bytes - sizeof(win->wExtra) )))
231 SERVER_START_REQ( destroy_window )
233 req->handle = wine_server_user_handle( handle );
234 wine_server_call( req );
236 SERVER_END_REQ;
237 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
238 return NULL;
241 if (!parent) /* if parent is 0 we don't have a desktop window yet */
243 struct user_thread_info *thread_info = get_user_thread_info();
245 if (name == (LPCWSTR)DESKTOP_CLASS_ATOM)
247 if (!thread_info->top_window) thread_info->top_window = full_parent ? full_parent : handle;
248 else assert( full_parent == thread_info->top_window );
249 if (full_parent && !USER_Driver->pCreateDesktopWindow( thread_info->top_window ))
250 ERR( "failed to create desktop window\n" );
252 else /* HWND_MESSAGE parent */
254 if (!thread_info->msg_window && !full_parent) thread_info->msg_window = handle;
258 USER_Lock();
260 index = USER_HANDLE_TO_INDEX(handle);
261 assert( index < NB_USER_HANDLES );
262 win->obj.handle = handle;
263 win->obj.type = USER_WINDOW;
264 win->parent = full_parent;
265 win->owner = full_owner;
266 win->class = class;
267 win->winproc = get_class_winproc( class );
268 win->cbWndExtra = extra_bytes;
269 InterlockedExchangePointer( &user_handles[index], win );
270 if (WINPROC_IsUnicode( win->winproc, unicode )) win->flags |= WIN_ISUNICODE;
271 return win;
275 /***********************************************************************
276 * free_window_handle
278 * Free a window handle.
280 static void free_window_handle( HWND hwnd )
282 struct user_object *ptr;
283 WORD index = USER_HANDLE_TO_INDEX(hwnd);
285 if ((ptr = get_user_handle_ptr( hwnd, USER_WINDOW )) && ptr != OBJ_OTHER_PROCESS)
287 SERVER_START_REQ( destroy_window )
289 req->handle = wine_server_user_handle( hwnd );
290 if (wine_server_call_err( req )) ptr = NULL;
291 else InterlockedCompareExchangePointer( &user_handles[index], NULL, ptr );
293 SERVER_END_REQ;
294 USER_Unlock();
295 HeapFree( GetProcessHeap(), 0, ptr );
300 /*******************************************************************
301 * list_window_children
303 * Build an array of the children of a given window. The array must be
304 * freed with HeapFree. Returns NULL when no windows are found.
306 static HWND *list_window_children( HDESK desktop, HWND hwnd, LPCWSTR class, DWORD tid )
308 HWND *list;
309 int i, size = 128;
310 ATOM atom = get_int_atom_value( class );
312 /* empty class is not the same as NULL class */
313 if (!atom && class && !class[0]) return NULL;
315 for (;;)
317 int count = 0;
319 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
321 SERVER_START_REQ( get_window_children )
323 req->desktop = wine_server_obj_handle( desktop );
324 req->parent = wine_server_user_handle( hwnd );
325 req->tid = tid;
326 req->atom = atom;
327 if (!atom && class) wine_server_add_data( req, class, strlenW(class)*sizeof(WCHAR) );
328 wine_server_set_reply( req, list, (size-1) * sizeof(user_handle_t) );
329 if (!wine_server_call( req )) count = reply->count;
331 SERVER_END_REQ;
332 if (count && count < size)
334 /* start from the end since HWND is potentially larger than user_handle_t */
335 for (i = count - 1; i >= 0; i--)
336 list[i] = wine_server_ptr_handle( ((user_handle_t *)list)[i] );
337 list[count] = 0;
338 return list;
340 HeapFree( GetProcessHeap(), 0, list );
341 if (!count) break;
342 size = count + 1; /* restart with a large enough buffer */
344 return NULL;
348 /*******************************************************************
349 * list_window_parents
351 * Build an array of all parents of a given window, starting with
352 * the immediate parent. The array must be freed with HeapFree.
354 static HWND *list_window_parents( HWND hwnd )
356 WND *win;
357 HWND current, *list;
358 int i, pos = 0, size = 16, count;
360 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) return NULL;
362 current = hwnd;
363 for (;;)
365 if (!(win = WIN_GetPtr( current ))) goto empty;
366 if (win == WND_OTHER_PROCESS) break; /* need to do it the hard way */
367 if (win == WND_DESKTOP)
369 if (!pos) goto empty;
370 list[pos] = 0;
371 return list;
373 list[pos] = current = win->parent;
374 WIN_ReleasePtr( win );
375 if (!current) return list;
376 if (++pos == size - 1)
378 /* need to grow the list */
379 HWND *new_list = HeapReAlloc( GetProcessHeap(), 0, list, (size+16) * sizeof(HWND) );
380 if (!new_list) goto empty;
381 list = new_list;
382 size += 16;
386 /* at least one parent belongs to another process, have to query the server */
388 for (;;)
390 count = 0;
391 SERVER_START_REQ( get_window_parents )
393 req->handle = wine_server_user_handle( hwnd );
394 wine_server_set_reply( req, list, (size-1) * sizeof(user_handle_t) );
395 if (!wine_server_call( req )) count = reply->count;
397 SERVER_END_REQ;
398 if (!count) goto empty;
399 if (size > count)
401 /* start from the end since HWND is potentially larger than user_handle_t */
402 for (i = count - 1; i >= 0; i--)
403 list[i] = wine_server_ptr_handle( ((user_handle_t *)list)[i] );
404 list[count] = 0;
405 return list;
407 HeapFree( GetProcessHeap(), 0, list );
408 size = count + 1;
409 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) return NULL;
412 empty:
413 HeapFree( GetProcessHeap(), 0, list );
414 return NULL;
418 /*******************************************************************
419 * send_parent_notify
421 static void send_parent_notify( HWND hwnd, UINT msg )
423 if ((GetWindowLongW( hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD &&
424 !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY))
426 HWND parent = GetParent(hwnd);
427 if (parent && parent != GetDesktopWindow())
428 SendMessageW( parent, WM_PARENTNOTIFY,
429 MAKEWPARAM( msg, GetWindowLongPtrW( hwnd, GWLP_ID )), (LPARAM)hwnd );
434 /*******************************************************************
435 * update_window_state
437 * Trigger an update of the window's driver state and surface.
439 static void update_window_state( HWND hwnd )
441 RECT window_rect, client_rect, valid_rects[2];
443 WIN_GetRectangles( hwnd, COORDS_PARENT, &window_rect, &client_rect );
444 valid_rects[0] = valid_rects[1] = client_rect;
445 set_window_pos( hwnd, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE |
446 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW,
447 &window_rect, &client_rect, valid_rects );
451 /*******************************************************************
452 * get_server_window_text
454 * Retrieve the window text from the server.
456 static void get_server_window_text( HWND hwnd, LPWSTR text, INT count )
458 size_t len = 0;
460 SERVER_START_REQ( get_window_text )
462 req->handle = wine_server_user_handle( hwnd );
463 wine_server_set_reply( req, text, (count - 1) * sizeof(WCHAR) );
464 if (!wine_server_call_err( req )) len = wine_server_reply_size(reply);
466 SERVER_END_REQ;
467 text[len / sizeof(WCHAR)] = 0;
471 /*******************************************************************
472 * get_hwnd_message_parent
474 * Return the parent for HWND_MESSAGE windows.
476 HWND get_hwnd_message_parent(void)
478 struct user_thread_info *thread_info = get_user_thread_info();
480 if (!thread_info->msg_window) GetDesktopWindow(); /* trigger creation */
481 return thread_info->msg_window;
485 /*******************************************************************
486 * is_desktop_window
488 * Check if window is the desktop or the HWND_MESSAGE top parent.
490 BOOL is_desktop_window( HWND hwnd )
492 struct user_thread_info *thread_info = get_user_thread_info();
494 if (!hwnd) return FALSE;
495 if (hwnd == thread_info->top_window) return TRUE;
496 if (hwnd == thread_info->msg_window) return TRUE;
498 if (!HIWORD(hwnd) || HIWORD(hwnd) == 0xffff)
500 if (LOWORD(thread_info->top_window) == LOWORD(hwnd)) return TRUE;
501 if (LOWORD(thread_info->msg_window) == LOWORD(hwnd)) return TRUE;
503 return FALSE;
507 /*******************************************************************
508 * Dummy window surface for windows that shouldn't get painted.
511 static void dummy_surface_lock( struct window_surface *window_surface )
513 /* nothing to do */
516 static void dummy_surface_unlock( struct window_surface *window_surface )
518 /* nothing to do */
521 static void *dummy_surface_get_bitmap_info( struct window_surface *window_surface, BITMAPINFO *info )
523 static DWORD dummy_data;
525 info->bmiHeader.biSize = sizeof( info->bmiHeader );
526 info->bmiHeader.biWidth = dummy_surface.rect.right;
527 info->bmiHeader.biHeight = dummy_surface.rect.bottom;
528 info->bmiHeader.biPlanes = 1;
529 info->bmiHeader.biBitCount = 32;
530 info->bmiHeader.biCompression = BI_RGB;
531 info->bmiHeader.biSizeImage = 0;
532 info->bmiHeader.biXPelsPerMeter = 0;
533 info->bmiHeader.biYPelsPerMeter = 0;
534 info->bmiHeader.biClrUsed = 0;
535 info->bmiHeader.biClrImportant = 0;
536 return &dummy_data;
539 static RECT *dummy_surface_get_bounds( struct window_surface *window_surface )
541 static RECT dummy_bounds;
542 return &dummy_bounds;
545 static void dummy_surface_set_region( struct window_surface *window_surface, HRGN region )
547 /* nothing to do */
550 static void dummy_surface_flush( struct window_surface *window_surface )
552 /* nothing to do */
555 static void dummy_surface_destroy( struct window_surface *window_surface )
557 /* nothing to do */
560 static const struct window_surface_funcs dummy_surface_funcs =
562 dummy_surface_lock,
563 dummy_surface_unlock,
564 dummy_surface_get_bitmap_info,
565 dummy_surface_get_bounds,
566 dummy_surface_set_region,
567 dummy_surface_flush,
568 dummy_surface_destroy
571 struct window_surface dummy_surface = { &dummy_surface_funcs, { NULL, NULL }, 1, { 0, 0, 1, 1 } };
574 /*******************************************************************
575 * register_window_surface
577 * Register a window surface in the global list, possibly replacing another one.
579 void register_window_surface( struct window_surface *old, struct window_surface *new )
581 if (old == new) return;
582 EnterCriticalSection( &surfaces_section );
583 if (old && old != &dummy_surface) list_remove( &old->entry );
584 if (new && new != &dummy_surface) list_add_tail( &window_surfaces, &new->entry );
585 LeaveCriticalSection( &surfaces_section );
589 /*******************************************************************
590 * flush_window_surfaces
592 * Flush pending output from all window surfaces.
594 void flush_window_surfaces( BOOL idle )
596 static DWORD last_idle;
597 DWORD now;
598 struct window_surface *surface;
600 EnterCriticalSection( &surfaces_section );
601 now = GetTickCount();
602 if (idle) last_idle = now;
603 /* if not idle, we only flush if there's evidence that the app never goes idle */
604 else if ((int)(now - last_idle) < 50) goto done;
606 LIST_FOR_EACH_ENTRY( surface, &window_surfaces, struct window_surface, entry )
607 surface->funcs->flush( surface );
608 done:
609 LeaveCriticalSection( &surfaces_section );
613 /***********************************************************************
614 * WIN_GetPtr
616 * Return a pointer to the WND structure if local to the process,
617 * or WND_OTHER_PROCESS if handle may be valid in other process.
618 * If ret value is a valid pointer, it must be released with WIN_ReleasePtr.
620 WND *WIN_GetPtr( HWND hwnd )
622 WND *ptr;
624 if ((ptr = get_user_handle_ptr( hwnd, USER_WINDOW )) == WND_OTHER_PROCESS)
626 if (is_desktop_window( hwnd )) ptr = WND_DESKTOP;
628 return ptr;
632 /***********************************************************************
633 * WIN_IsCurrentProcess
635 * Check whether a given window belongs to the current process (and return the full handle).
637 HWND WIN_IsCurrentProcess( HWND hwnd )
639 WND *ptr;
640 HWND ret;
642 if (!(ptr = WIN_GetPtr( hwnd )) || ptr == WND_OTHER_PROCESS || ptr == WND_DESKTOP) return 0;
643 ret = ptr->obj.handle;
644 WIN_ReleasePtr( ptr );
645 return ret;
649 /***********************************************************************
650 * WIN_IsCurrentThread
652 * Check whether a given window belongs to the current thread (and return the full handle).
654 HWND WIN_IsCurrentThread( HWND hwnd )
656 WND *ptr;
657 HWND ret = 0;
659 if (!(ptr = WIN_GetPtr( hwnd )) || ptr == WND_OTHER_PROCESS || ptr == WND_DESKTOP) return 0;
660 if (ptr->tid == GetCurrentThreadId()) ret = ptr->obj.handle;
661 WIN_ReleasePtr( ptr );
662 return ret;
666 /***********************************************************************
667 * win_set_flags
669 * Set the flags of a window and return the previous value.
671 UINT win_set_flags( HWND hwnd, UINT set_mask, UINT clear_mask )
673 UINT ret;
674 WND *ptr = WIN_GetPtr( hwnd );
676 if (!ptr || ptr == WND_OTHER_PROCESS || ptr == WND_DESKTOP) return 0;
677 ret = ptr->flags;
678 ptr->flags = (ret & ~clear_mask) | set_mask;
679 WIN_ReleasePtr( ptr );
680 return ret;
684 /***********************************************************************
685 * WIN_GetFullHandle
687 * Convert a possibly truncated window handle to a full 32-bit handle.
689 HWND WIN_GetFullHandle( HWND hwnd )
691 WND *ptr;
693 if (!hwnd || (ULONG_PTR)hwnd >> 16) return hwnd;
694 if (LOWORD(hwnd) <= 1 || LOWORD(hwnd) == 0xffff) return hwnd;
695 /* do sign extension for -2 and -3 */
696 if (LOWORD(hwnd) >= (WORD)-3) return (HWND)(LONG_PTR)(INT16)LOWORD(hwnd);
698 if (!(ptr = WIN_GetPtr( hwnd ))) return hwnd;
700 if (ptr == WND_DESKTOP)
702 if (LOWORD(hwnd) == LOWORD(GetDesktopWindow())) return GetDesktopWindow();
703 else return get_hwnd_message_parent();
706 if (ptr != WND_OTHER_PROCESS)
708 hwnd = ptr->obj.handle;
709 WIN_ReleasePtr( ptr );
711 else /* may belong to another process */
713 SERVER_START_REQ( get_window_info )
715 req->handle = wine_server_user_handle( hwnd );
716 if (!wine_server_call_err( req )) hwnd = wine_server_ptr_handle( reply->full_handle );
718 SERVER_END_REQ;
720 return hwnd;
724 /***********************************************************************
725 * WIN_SetOwner
727 * Change the owner of a window.
729 HWND WIN_SetOwner( HWND hwnd, HWND owner )
731 WND *win = WIN_GetPtr( hwnd );
732 HWND ret = 0;
734 if (!win || win == WND_DESKTOP) return 0;
735 if (win == WND_OTHER_PROCESS)
737 if (IsWindow(hwnd)) ERR( "cannot set owner %p on other process window %p\n", owner, hwnd );
738 return 0;
740 SERVER_START_REQ( set_window_owner )
742 req->handle = wine_server_user_handle( hwnd );
743 req->owner = wine_server_user_handle( owner );
744 if (!wine_server_call( req ))
746 win->owner = wine_server_ptr_handle( reply->full_owner );
747 ret = wine_server_ptr_handle( reply->prev_owner );
750 SERVER_END_REQ;
751 WIN_ReleasePtr( win );
752 return ret;
756 /***********************************************************************
757 * WIN_SetStyle
759 * Change the style of a window.
761 ULONG WIN_SetStyle( HWND hwnd, ULONG set_bits, ULONG clear_bits )
763 BOOL ok, made_visible = FALSE;
764 STYLESTRUCT style;
765 WND *win = WIN_GetPtr( hwnd );
767 if (!win || win == WND_DESKTOP) return 0;
768 if (win == WND_OTHER_PROCESS)
770 if (IsWindow(hwnd))
771 ERR( "cannot set style %x/%x on other process window %p\n",
772 set_bits, clear_bits, hwnd );
773 return 0;
775 style.styleOld = win->dwStyle;
776 style.styleNew = (win->dwStyle | set_bits) & ~clear_bits;
777 if (style.styleNew == style.styleOld)
779 WIN_ReleasePtr( win );
780 return style.styleNew;
782 SERVER_START_REQ( set_window_info )
784 req->handle = wine_server_user_handle( hwnd );
785 req->flags = SET_WIN_STYLE;
786 req->style = style.styleNew;
787 req->extra_offset = -1;
788 if ((ok = !wine_server_call( req )))
790 style.styleOld = reply->old_style;
791 win->dwStyle = style.styleNew;
794 SERVER_END_REQ;
796 if (ok && ((style.styleOld ^ style.styleNew) & WS_VISIBLE))
798 made_visible = (style.styleNew & WS_VISIBLE) != 0;
799 invalidate_dce( win, NULL );
801 WIN_ReleasePtr( win );
803 if (!ok) return 0;
805 USER_Driver->pSetWindowStyle( hwnd, GWL_STYLE, &style );
806 if (made_visible) update_window_state( hwnd );
808 return style.styleOld;
812 /***********************************************************************
813 * WIN_GetRectangles
815 * Get the window and client rectangles.
817 BOOL WIN_GetRectangles( HWND hwnd, enum coords_relative relative, RECT *rectWindow, RECT *rectClient )
819 WND *win = WIN_GetPtr( hwnd );
820 BOOL ret = TRUE;
822 if (!win)
824 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
825 return FALSE;
827 if (win == WND_DESKTOP)
829 RECT rect;
830 rect.left = rect.top = 0;
831 if (hwnd == get_hwnd_message_parent())
833 rect.right = 100;
834 rect.bottom = 100;
836 else
838 rect.right = GetSystemMetrics(SM_CXSCREEN);
839 rect.bottom = GetSystemMetrics(SM_CYSCREEN);
841 if (rectWindow) *rectWindow = rect;
842 if (rectClient) *rectClient = rect;
843 return TRUE;
845 if (win != WND_OTHER_PROCESS)
847 RECT window_rect = win->rectWindow, client_rect = win->rectClient;
849 switch (relative)
851 case COORDS_CLIENT:
852 OffsetRect( &window_rect, -win->rectClient.left, -win->rectClient.top );
853 OffsetRect( &client_rect, -win->rectClient.left, -win->rectClient.top );
854 if (win->dwExStyle & WS_EX_LAYOUTRTL)
855 mirror_rect( &win->rectClient, &window_rect );
856 break;
857 case COORDS_WINDOW:
858 OffsetRect( &window_rect, -win->rectWindow.left, -win->rectWindow.top );
859 OffsetRect( &client_rect, -win->rectWindow.left, -win->rectWindow.top );
860 if (win->dwExStyle & WS_EX_LAYOUTRTL)
861 mirror_rect( &win->rectWindow, &client_rect );
862 break;
863 case COORDS_PARENT:
864 if (win->parent)
866 WND *parent = WIN_GetPtr( win->parent );
867 if (parent == WND_DESKTOP) break;
868 if (!parent || parent == WND_OTHER_PROCESS)
870 WIN_ReleasePtr( win );
871 goto other_process;
873 if (parent->flags & WIN_CHILDREN_MOVED)
875 WIN_ReleasePtr( parent );
876 WIN_ReleasePtr( win );
877 goto other_process;
879 if (parent->dwExStyle & WS_EX_LAYOUTRTL)
881 mirror_rect( &parent->rectClient, &window_rect );
882 mirror_rect( &parent->rectClient, &client_rect );
884 WIN_ReleasePtr( parent );
886 break;
887 case COORDS_SCREEN:
888 while (win->parent)
890 WND *parent = WIN_GetPtr( win->parent );
891 if (parent == WND_DESKTOP) break;
892 if (!parent || parent == WND_OTHER_PROCESS)
894 WIN_ReleasePtr( win );
895 goto other_process;
897 WIN_ReleasePtr( win );
898 if (parent->flags & WIN_CHILDREN_MOVED)
900 WIN_ReleasePtr( parent );
901 goto other_process;
903 win = parent;
904 if (win->parent)
906 OffsetRect( &window_rect, win->rectClient.left, win->rectClient.top );
907 OffsetRect( &client_rect, win->rectClient.left, win->rectClient.top );
910 break;
912 if (rectWindow) *rectWindow = window_rect;
913 if (rectClient) *rectClient = client_rect;
914 WIN_ReleasePtr( win );
915 return TRUE;
918 other_process:
919 SERVER_START_REQ( get_window_rectangles )
921 req->handle = wine_server_user_handle( hwnd );
922 req->relative = relative;
923 if ((ret = !wine_server_call_err( req )))
925 if (rectWindow)
927 rectWindow->left = reply->window.left;
928 rectWindow->top = reply->window.top;
929 rectWindow->right = reply->window.right;
930 rectWindow->bottom = reply->window.bottom;
932 if (rectClient)
934 rectClient->left = reply->client.left;
935 rectClient->top = reply->client.top;
936 rectClient->right = reply->client.right;
937 rectClient->bottom = reply->client.bottom;
941 SERVER_END_REQ;
942 return ret;
946 /***********************************************************************
947 * WIN_DestroyWindow
949 * Destroy storage associated to a window. "Internals" p.358
951 LRESULT WIN_DestroyWindow( HWND hwnd )
953 WND *wndPtr;
954 HWND *list;
955 HMENU menu = 0, sys_menu;
956 HWND icon_title;
957 struct window_surface *surface;
959 TRACE("%p\n", hwnd );
961 /* free child windows */
962 if ((list = WIN_ListChildren( hwnd )))
964 int i;
965 for (i = 0; list[i]; i++)
967 if (WIN_IsCurrentThread( list[i] )) WIN_DestroyWindow( list[i] );
968 else SendMessageW( list[i], WM_WINE_DESTROYWINDOW, 0, 0 );
970 HeapFree( GetProcessHeap(), 0, list );
973 /* Unlink now so we won't bother with the children later on */
974 SERVER_START_REQ( set_parent )
976 req->handle = wine_server_user_handle( hwnd );
977 req->parent = 0;
978 wine_server_call( req );
980 SERVER_END_REQ;
983 * Send the WM_NCDESTROY to the window being destroyed.
985 SendMessageW( hwnd, WM_NCDESTROY, 0, 0 );
987 /* FIXME: do we need to fake QS_MOUSEMOVE wakebit? */
989 /* free resources associated with the window */
991 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
992 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
993 menu = (HMENU)wndPtr->wIDmenu;
994 sys_menu = wndPtr->hSysMenu;
995 free_dce( wndPtr->dce, hwnd );
996 wndPtr->dce = NULL;
997 icon_title = wndPtr->icon_title;
998 HeapFree( GetProcessHeap(), 0, wndPtr->text );
999 wndPtr->text = NULL;
1000 HeapFree( GetProcessHeap(), 0, wndPtr->pScroll );
1001 wndPtr->pScroll = NULL;
1002 DestroyIcon( wndPtr->hIconSmall2 );
1003 surface = wndPtr->surface;
1004 wndPtr->surface = NULL;
1005 WIN_ReleasePtr( wndPtr );
1007 if (icon_title) DestroyWindow( icon_title );
1008 if (menu) DestroyMenu( menu );
1009 if (sys_menu) DestroyMenu( sys_menu );
1010 if (surface)
1012 register_window_surface( surface, NULL );
1013 window_surface_release( surface );
1016 USER_Driver->pDestroyWindow( hwnd );
1018 free_window_handle( hwnd );
1019 return 0;
1023 /***********************************************************************
1024 * destroy_thread_window
1026 * Destroy a window upon exit of its thread.
1028 static void destroy_thread_window( HWND hwnd )
1030 WND *wndPtr;
1031 HWND *list;
1032 HMENU menu = 0, sys_menu = 0;
1033 struct window_surface *surface = NULL;
1034 WORD index;
1036 /* free child windows */
1038 if ((list = WIN_ListChildren( hwnd )))
1040 int i;
1041 for (i = 0; list[i]; i++)
1043 if (WIN_IsCurrentThread( list[i] )) destroy_thread_window( list[i] );
1044 else SendMessageW( list[i], WM_WINE_DESTROYWINDOW, 0, 0 );
1046 HeapFree( GetProcessHeap(), 0, list );
1049 /* destroy the client-side storage */
1051 index = USER_HANDLE_TO_INDEX(hwnd);
1052 if (index >= NB_USER_HANDLES) return;
1053 USER_Lock();
1054 if ((wndPtr = user_handles[index]))
1056 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD) menu = (HMENU)wndPtr->wIDmenu;
1057 sys_menu = wndPtr->hSysMenu;
1058 free_dce( wndPtr->dce, hwnd );
1059 surface = wndPtr->surface;
1060 wndPtr->surface = NULL;
1061 InterlockedCompareExchangePointer( &user_handles[index], NULL, wndPtr );
1063 USER_Unlock();
1065 HeapFree( GetProcessHeap(), 0, wndPtr );
1066 if (menu) DestroyMenu( menu );
1067 if (sys_menu) DestroyMenu( sys_menu );
1068 if (surface)
1070 register_window_surface( surface, NULL );
1071 window_surface_release( surface );
1076 /***********************************************************************
1077 * destroy_thread_child_windows
1079 * Destroy child windows upon exit of its thread.
1081 static void destroy_thread_child_windows( HWND hwnd )
1083 HWND *list;
1084 int i;
1086 if (WIN_IsCurrentThread( hwnd ))
1088 destroy_thread_window( hwnd );
1090 else if ((list = WIN_ListChildren( hwnd )))
1092 for (i = 0; list[i]; i++) destroy_thread_child_windows( list[i] );
1093 HeapFree( GetProcessHeap(), 0, list );
1098 /***********************************************************************
1099 * WIN_DestroyThreadWindows
1101 * Destroy all children of 'wnd' owned by the current thread.
1103 void WIN_DestroyThreadWindows( HWND hwnd )
1105 HWND *list;
1106 int i;
1108 if (!(list = WIN_ListChildren( hwnd ))) return;
1110 /* reset owners of top-level windows */
1111 for (i = 0; list[i]; i++)
1113 if (!WIN_IsCurrentThread( list[i] ))
1115 HWND owner = GetWindow( list[i], GW_OWNER );
1116 if (owner && WIN_IsCurrentThread( owner )) WIN_SetOwner( list[i], 0 );
1120 for (i = 0; list[i]; i++) destroy_thread_child_windows( list[i] );
1121 HeapFree( GetProcessHeap(), 0, list );
1125 /***********************************************************************
1126 * WIN_FixCoordinates
1128 * Fix the coordinates - Helper for WIN_CreateWindowEx.
1129 * returns default show mode in sw.
1131 static void WIN_FixCoordinates( CREATESTRUCTW *cs, INT *sw)
1133 #define IS_DEFAULT(x) ((x) == CW_USEDEFAULT || (x) == (SHORT)0x8000)
1134 POINT pos[2];
1136 if (cs->dwExStyle & WS_EX_MDICHILD)
1138 UINT id = 0;
1140 MDI_CalcDefaultChildPos(cs->hwndParent, -1, pos, 0, &id);
1141 if (!(cs->style & WS_POPUP)) cs->hMenu = ULongToHandle(id);
1143 TRACE("MDI child id %04x\n", id);
1146 if (cs->style & (WS_CHILD | WS_POPUP))
1148 if (cs->dwExStyle & WS_EX_MDICHILD)
1150 if (IS_DEFAULT(cs->x))
1152 cs->x = pos[0].x;
1153 cs->y = pos[0].y;
1155 if (IS_DEFAULT(cs->cx) || !cs->cx) cs->cx = pos[1].x;
1156 if (IS_DEFAULT(cs->cy) || !cs->cy) cs->cy = pos[1].y;
1158 else
1160 if (IS_DEFAULT(cs->x)) cs->x = cs->y = 0;
1161 if (IS_DEFAULT(cs->cx)) cs->cx = cs->cy = 0;
1164 else /* overlapped window */
1166 HMONITOR monitor;
1167 MONITORINFO mon_info;
1168 STARTUPINFOW info;
1170 if (!IS_DEFAULT(cs->x) && !IS_DEFAULT(cs->cx) && !IS_DEFAULT(cs->cy)) return;
1172 monitor = MonitorFromWindow( cs->hwndParent, MONITOR_DEFAULTTOPRIMARY );
1173 mon_info.cbSize = sizeof(mon_info);
1174 GetMonitorInfoW( monitor, &mon_info );
1175 GetStartupInfoW( &info );
1177 if (IS_DEFAULT(cs->x))
1179 if (!IS_DEFAULT(cs->y)) *sw = cs->y;
1180 cs->x = (info.dwFlags & STARTF_USEPOSITION) ? info.dwX : mon_info.rcWork.left;
1181 cs->y = (info.dwFlags & STARTF_USEPOSITION) ? info.dwY : mon_info.rcWork.top;
1184 if (IS_DEFAULT(cs->cx))
1186 if (info.dwFlags & STARTF_USESIZE)
1188 cs->cx = info.dwXSize;
1189 cs->cy = info.dwYSize;
1191 else
1193 cs->cx = (mon_info.rcWork.right - mon_info.rcWork.left) * 3 / 4 - cs->x;
1194 cs->cy = (mon_info.rcWork.bottom - mon_info.rcWork.top) * 3 / 4 - cs->y;
1197 /* neither x nor cx are default. Check the y values .
1198 * In the trace we see Outlook and Outlook Express using
1199 * cy set to CW_USEDEFAULT when opening the address book.
1201 else if (IS_DEFAULT(cs->cy))
1203 FIXME("Strange use of CW_USEDEFAULT in nHeight\n");
1204 cs->cy = (mon_info.rcWork.bottom - mon_info.rcWork.top) * 3 / 4 - cs->y;
1207 #undef IS_DEFAULT
1210 /***********************************************************************
1211 * dump_window_styles
1213 static void dump_window_styles( DWORD style, DWORD exstyle )
1215 TRACE( "style:" );
1216 if(style & WS_POPUP) TRACE(" WS_POPUP");
1217 if(style & WS_CHILD) TRACE(" WS_CHILD");
1218 if(style & WS_MINIMIZE) TRACE(" WS_MINIMIZE");
1219 if(style & WS_VISIBLE) TRACE(" WS_VISIBLE");
1220 if(style & WS_DISABLED) TRACE(" WS_DISABLED");
1221 if(style & WS_CLIPSIBLINGS) TRACE(" WS_CLIPSIBLINGS");
1222 if(style & WS_CLIPCHILDREN) TRACE(" WS_CLIPCHILDREN");
1223 if(style & WS_MAXIMIZE) TRACE(" WS_MAXIMIZE");
1224 if((style & WS_CAPTION) == WS_CAPTION) TRACE(" WS_CAPTION");
1225 else
1227 if(style & WS_BORDER) TRACE(" WS_BORDER");
1228 if(style & WS_DLGFRAME) TRACE(" WS_DLGFRAME");
1230 if(style & WS_VSCROLL) TRACE(" WS_VSCROLL");
1231 if(style & WS_HSCROLL) TRACE(" WS_HSCROLL");
1232 if(style & WS_SYSMENU) TRACE(" WS_SYSMENU");
1233 if(style & WS_THICKFRAME) TRACE(" WS_THICKFRAME");
1234 if (style & WS_CHILD)
1236 if(style & WS_GROUP) TRACE(" WS_GROUP");
1237 if(style & WS_TABSTOP) TRACE(" WS_TABSTOP");
1239 else
1241 if(style & WS_MINIMIZEBOX) TRACE(" WS_MINIMIZEBOX");
1242 if(style & WS_MAXIMIZEBOX) TRACE(" WS_MAXIMIZEBOX");
1245 /* FIXME: Add dumping of BS_/ES_/SBS_/LBS_/CBS_/DS_/etc. styles */
1246 #define DUMPED_STYLES \
1247 ((DWORD)(WS_POPUP | \
1248 WS_CHILD | \
1249 WS_MINIMIZE | \
1250 WS_VISIBLE | \
1251 WS_DISABLED | \
1252 WS_CLIPSIBLINGS | \
1253 WS_CLIPCHILDREN | \
1254 WS_MAXIMIZE | \
1255 WS_BORDER | \
1256 WS_DLGFRAME | \
1257 WS_VSCROLL | \
1258 WS_HSCROLL | \
1259 WS_SYSMENU | \
1260 WS_THICKFRAME | \
1261 WS_GROUP | \
1262 WS_TABSTOP | \
1263 WS_MINIMIZEBOX | \
1264 WS_MAXIMIZEBOX))
1266 if(style & ~DUMPED_STYLES) TRACE(" %08x", style & ~DUMPED_STYLES);
1267 TRACE("\n");
1268 #undef DUMPED_STYLES
1270 TRACE( "exstyle:" );
1271 if(exstyle & WS_EX_DLGMODALFRAME) TRACE(" WS_EX_DLGMODALFRAME");
1272 if(exstyle & WS_EX_DRAGDETECT) TRACE(" WS_EX_DRAGDETECT");
1273 if(exstyle & WS_EX_NOPARENTNOTIFY) TRACE(" WS_EX_NOPARENTNOTIFY");
1274 if(exstyle & WS_EX_TOPMOST) TRACE(" WS_EX_TOPMOST");
1275 if(exstyle & WS_EX_ACCEPTFILES) TRACE(" WS_EX_ACCEPTFILES");
1276 if(exstyle & WS_EX_TRANSPARENT) TRACE(" WS_EX_TRANSPARENT");
1277 if(exstyle & WS_EX_MDICHILD) TRACE(" WS_EX_MDICHILD");
1278 if(exstyle & WS_EX_TOOLWINDOW) TRACE(" WS_EX_TOOLWINDOW");
1279 if(exstyle & WS_EX_WINDOWEDGE) TRACE(" WS_EX_WINDOWEDGE");
1280 if(exstyle & WS_EX_CLIENTEDGE) TRACE(" WS_EX_CLIENTEDGE");
1281 if(exstyle & WS_EX_CONTEXTHELP) TRACE(" WS_EX_CONTEXTHELP");
1282 if(exstyle & WS_EX_RIGHT) TRACE(" WS_EX_RIGHT");
1283 if(exstyle & WS_EX_RTLREADING) TRACE(" WS_EX_RTLREADING");
1284 if(exstyle & WS_EX_LEFTSCROLLBAR) TRACE(" WS_EX_LEFTSCROLLBAR");
1285 if(exstyle & WS_EX_CONTROLPARENT) TRACE(" WS_EX_CONTROLPARENT");
1286 if(exstyle & WS_EX_STATICEDGE) TRACE(" WS_EX_STATICEDGE");
1287 if(exstyle & WS_EX_APPWINDOW) TRACE(" WS_EX_APPWINDOW");
1288 if(exstyle & WS_EX_LAYERED) TRACE(" WS_EX_LAYERED");
1289 if(exstyle & WS_EX_LAYOUTRTL) TRACE(" WS_EX_LAYOUTRTL");
1291 #define DUMPED_EX_STYLES \
1292 ((DWORD)(WS_EX_DLGMODALFRAME | \
1293 WS_EX_DRAGDETECT | \
1294 WS_EX_NOPARENTNOTIFY | \
1295 WS_EX_TOPMOST | \
1296 WS_EX_ACCEPTFILES | \
1297 WS_EX_TRANSPARENT | \
1298 WS_EX_MDICHILD | \
1299 WS_EX_TOOLWINDOW | \
1300 WS_EX_WINDOWEDGE | \
1301 WS_EX_CLIENTEDGE | \
1302 WS_EX_CONTEXTHELP | \
1303 WS_EX_RIGHT | \
1304 WS_EX_RTLREADING | \
1305 WS_EX_LEFTSCROLLBAR | \
1306 WS_EX_CONTROLPARENT | \
1307 WS_EX_STATICEDGE | \
1308 WS_EX_APPWINDOW | \
1309 WS_EX_LAYERED | \
1310 WS_EX_LAYOUTRTL))
1312 if(exstyle & ~DUMPED_EX_STYLES) TRACE(" %08x", exstyle & ~DUMPED_EX_STYLES);
1313 TRACE("\n");
1314 #undef DUMPED_EX_STYLES
1318 /***********************************************************************
1319 * WIN_CreateWindowEx
1321 * Implementation of CreateWindowEx().
1323 HWND WIN_CreateWindowEx( CREATESTRUCTW *cs, LPCWSTR className, HINSTANCE module, BOOL unicode )
1325 INT cx, cy, style, sw = SW_SHOW;
1326 LRESULT result;
1327 RECT rect;
1328 WND *wndPtr;
1329 HWND hwnd, parent, owner, top_child = 0;
1330 MDICREATESTRUCTW mdi_cs;
1331 CBT_CREATEWNDW cbtc;
1332 CREATESTRUCTW cbcs;
1334 TRACE("%s %s ex=%08x style=%08x %d,%d %dx%d parent=%p menu=%p inst=%p params=%p\n",
1335 unicode ? debugstr_w(cs->lpszName) : debugstr_a((LPCSTR)cs->lpszName),
1336 debugstr_w(className),
1337 cs->dwExStyle, cs->style, cs->x, cs->y, cs->cx, cs->cy,
1338 cs->hwndParent, cs->hMenu, cs->hInstance, cs->lpCreateParams );
1339 if(TRACE_ON(win)) dump_window_styles( cs->style, cs->dwExStyle );
1341 /* Fix the styles for MDI children */
1342 if (cs->dwExStyle & WS_EX_MDICHILD)
1344 if (!(win_get_flags( cs->hwndParent ) & WIN_ISMDICLIENT))
1346 WARN("WS_EX_MDICHILD, but parent %p is not MDIClient\n", cs->hwndParent);
1347 return 0;
1350 /* cs->lpCreateParams of WM_[NC]CREATE is different for MDI children.
1351 * MDICREATESTRUCT members have the originally passed values.
1353 * Note: we rely on the fact that MDICREATESTRUCTA and MDICREATESTRUCTW
1354 * have the same layout.
1356 mdi_cs.szClass = cs->lpszClass;
1357 mdi_cs.szTitle = cs->lpszName;
1358 mdi_cs.hOwner = cs->hInstance;
1359 mdi_cs.x = cs->x;
1360 mdi_cs.y = cs->y;
1361 mdi_cs.cx = cs->cx;
1362 mdi_cs.cy = cs->cy;
1363 mdi_cs.style = cs->style;
1364 mdi_cs.lParam = (LPARAM)cs->lpCreateParams;
1366 cs->lpCreateParams = &mdi_cs;
1368 if (GetWindowLongW(cs->hwndParent, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1370 if (cs->style & WS_POPUP)
1372 TRACE("WS_POPUP with MDIS_ALLCHILDSTYLES is not allowed\n");
1373 return 0;
1375 cs->style |= WS_CHILD | WS_CLIPSIBLINGS;
1377 else
1379 cs->style &= ~WS_POPUP;
1380 cs->style |= WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CAPTION |
1381 WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
1384 top_child = GetWindow(cs->hwndParent, GW_CHILD);
1386 if (top_child)
1388 /* Restore current maximized child */
1389 if((cs->style & WS_VISIBLE) && IsZoomed(top_child))
1391 TRACE("Restoring current maximized child %p\n", top_child);
1392 if (cs->style & WS_MAXIMIZE)
1394 /* if the new window is maximized don't bother repainting */
1395 SendMessageW( top_child, WM_SETREDRAW, FALSE, 0 );
1396 ShowWindow( top_child, SW_SHOWNORMAL );
1397 SendMessageW( top_child, WM_SETREDRAW, TRUE, 0 );
1399 else ShowWindow( top_child, SW_SHOWNORMAL );
1404 /* Find the parent window */
1406 parent = cs->hwndParent;
1407 owner = 0;
1409 if (cs->hwndParent == HWND_MESSAGE)
1411 cs->hwndParent = parent = get_hwnd_message_parent();
1413 else if (cs->hwndParent)
1415 if ((cs->style & (WS_CHILD|WS_POPUP)) != WS_CHILD)
1417 parent = GetDesktopWindow();
1418 owner = cs->hwndParent;
1420 else
1422 DWORD parent_style = GetWindowLongW( parent, GWL_EXSTYLE );
1423 if ((parent_style & WS_EX_LAYOUTRTL) && !(parent_style & WS_EX_NOINHERITLAYOUT))
1424 cs->dwExStyle |= WS_EX_LAYOUTRTL;
1427 else
1429 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
1431 if ((cs->style & (WS_CHILD|WS_POPUP)) == WS_CHILD)
1433 WARN("No parent for child window\n" );
1434 SetLastError(ERROR_TLW_WITH_WSCHILD);
1435 return 0; /* WS_CHILD needs a parent, but WS_POPUP doesn't */
1438 /* are we creating the desktop or HWND_MESSAGE parent itself? */
1439 if (className != (LPCWSTR)DESKTOP_CLASS_ATOM &&
1440 (IS_INTRESOURCE(className) || strcmpiW( className, messageW )))
1442 DWORD layout;
1443 GetProcessDefaultLayout( &layout );
1444 if (layout & LAYOUT_RTL) cs->dwExStyle |= WS_EX_LAYOUTRTL;
1445 parent = GetDesktopWindow();
1449 WIN_FixCoordinates(cs, &sw); /* fix default coordinates */
1451 if ((cs->dwExStyle & WS_EX_DLGMODALFRAME) ||
1452 ((!(cs->dwExStyle & WS_EX_STATICEDGE)) &&
1453 (cs->style & (WS_DLGFRAME | WS_THICKFRAME))))
1454 cs->dwExStyle |= WS_EX_WINDOWEDGE;
1455 else
1456 cs->dwExStyle &= ~WS_EX_WINDOWEDGE;
1458 /* Create the window structure */
1460 if (!(wndPtr = create_window_handle( parent, owner, className, module, unicode )))
1462 WNDCLASSW wc;
1463 /* if it's a comctl32 class, GetClassInfo will load it, then we can retry */
1464 if (GetLastError() != ERROR_INVALID_HANDLE ||
1465 !GetClassInfoW( 0, className, &wc ) ||
1466 !(wndPtr = create_window_handle( parent, owner, className, module, unicode )))
1467 return 0;
1469 hwnd = wndPtr->obj.handle;
1471 /* Fill the window structure */
1473 wndPtr->tid = GetCurrentThreadId();
1474 wndPtr->hInstance = cs->hInstance;
1475 wndPtr->text = NULL;
1476 wndPtr->dwStyle = cs->style & ~WS_VISIBLE;
1477 wndPtr->dwExStyle = cs->dwExStyle;
1478 wndPtr->wIDmenu = 0;
1479 wndPtr->helpContext = 0;
1480 wndPtr->pScroll = NULL;
1481 wndPtr->userdata = 0;
1482 wndPtr->hIcon = 0;
1483 wndPtr->hIconSmall = 0;
1484 wndPtr->hIconSmall2 = 0;
1485 wndPtr->hSysMenu = 0;
1487 wndPtr->min_pos.x = wndPtr->min_pos.y = -1;
1488 wndPtr->max_pos.x = wndPtr->max_pos.y = -1;
1489 SetRect( &wndPtr->normal_rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
1491 if (wndPtr->dwStyle & WS_SYSMENU) SetSystemMenu( hwnd, 0 );
1494 * Correct the window styles.
1496 * It affects only the style loaded into the WIN structure.
1499 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
1501 wndPtr->dwStyle |= WS_CLIPSIBLINGS;
1502 if (!(wndPtr->dwStyle & WS_POPUP))
1503 wndPtr->dwStyle |= WS_CAPTION;
1506 /* WS_EX_WINDOWEDGE depends on some other styles */
1507 if (wndPtr->dwExStyle & WS_EX_DLGMODALFRAME)
1508 wndPtr->dwExStyle |= WS_EX_WINDOWEDGE;
1509 else if (wndPtr->dwStyle & (WS_DLGFRAME | WS_THICKFRAME))
1511 if (!((wndPtr->dwExStyle & WS_EX_STATICEDGE) &&
1512 (wndPtr->dwStyle & (WS_CHILD | WS_POPUP))))
1513 wndPtr->dwExStyle |= WS_EX_WINDOWEDGE;
1515 else
1516 wndPtr->dwExStyle &= ~WS_EX_WINDOWEDGE;
1518 if (!(wndPtr->dwStyle & (WS_CHILD | WS_POPUP)))
1519 wndPtr->flags |= WIN_NEED_SIZE;
1521 SERVER_START_REQ( set_window_info )
1523 req->handle = wine_server_user_handle( hwnd );
1524 req->flags = SET_WIN_STYLE | SET_WIN_EXSTYLE | SET_WIN_INSTANCE | SET_WIN_UNICODE;
1525 req->style = wndPtr->dwStyle;
1526 req->ex_style = wndPtr->dwExStyle;
1527 req->instance = wine_server_client_ptr( wndPtr->hInstance );
1528 req->is_unicode = (wndPtr->flags & WIN_ISUNICODE) != 0;
1529 req->extra_offset = -1;
1530 wine_server_call( req );
1532 SERVER_END_REQ;
1534 /* Set the window menu */
1536 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
1538 if (cs->hMenu)
1540 if (!MENU_SetMenu(hwnd, cs->hMenu))
1542 WIN_ReleasePtr( wndPtr );
1543 free_window_handle( hwnd );
1544 return 0;
1547 else
1549 LPCWSTR menuName = (LPCWSTR)GetClassLongPtrW( hwnd, GCLP_MENUNAME );
1550 if (menuName)
1552 cs->hMenu = LoadMenuW( cs->hInstance, menuName );
1553 if (cs->hMenu) MENU_SetMenu( hwnd, cs->hMenu );
1557 else SetWindowLongPtrW( hwnd, GWLP_ID, (ULONG_PTR)cs->hMenu );
1559 /* call the WH_CBT hook */
1561 /* the window style passed to the hook must be the real window style,
1562 * rather than just the window style that the caller to CreateWindowEx
1563 * passed in, so we have to copy the original CREATESTRUCT and get the
1564 * the real style. */
1565 cbcs = *cs;
1566 cbcs.style = wndPtr->dwStyle;
1567 cbtc.lpcs = &cbcs;
1568 cbtc.hwndInsertAfter = HWND_TOP;
1569 WIN_ReleasePtr( wndPtr );
1570 if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode )) goto failed;
1572 /* send the WM_GETMINMAXINFO message and fix the size if needed */
1574 cx = cs->cx;
1575 cy = cs->cy;
1576 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
1578 POINT maxSize, maxPos, minTrack, maxTrack;
1579 WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
1580 if (maxTrack.x < cx) cx = maxTrack.x;
1581 if (maxTrack.y < cy) cy = maxTrack.y;
1582 if (minTrack.x > cx) cx = minTrack.x;
1583 if (minTrack.y > cy) cy = minTrack.y;
1586 if (cx < 0) cx = 0;
1587 if (cy < 0) cy = 0;
1588 SetRect( &rect, cs->x, cs->y, cs->x + cx, cs->y + cy );
1589 /* check for wraparound */
1590 if (cs->x + cx < cs->x) rect.right = 0x7fffffff;
1591 if (cs->y + cy < cs->y) rect.bottom = 0x7fffffff;
1592 if (!set_window_pos( hwnd, 0, SWP_NOZORDER | SWP_NOACTIVATE, &rect, &rect, NULL )) goto failed;
1594 /* send WM_NCCREATE */
1596 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cx, cy );
1597 if (unicode)
1598 result = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1599 else
1600 result = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1601 if (!result)
1603 WARN( "%p: aborted by WM_NCCREATE\n", hwnd );
1604 goto failed;
1607 /* send WM_NCCALCSIZE */
1609 if (WIN_GetRectangles( hwnd, COORDS_PARENT, &rect, NULL ))
1611 /* yes, even if the CBT hook was called with HWND_TOP */
1612 HWND insert_after = (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
1613 RECT client_rect = rect;
1615 /* the rectangle is in screen coords for WM_NCCALCSIZE when wparam is FALSE */
1616 MapWindowPoints( parent, 0, (POINT *)&client_rect, 2 );
1617 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&client_rect );
1618 MapWindowPoints( 0, parent, (POINT *)&client_rect, 2 );
1619 set_window_pos( hwnd, insert_after, SWP_NOACTIVATE, &rect, &client_rect, NULL );
1621 else return 0;
1623 /* send WM_CREATE */
1625 if (unicode)
1626 result = SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs );
1627 else
1628 result = SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs );
1629 if (result == -1) goto failed;
1631 /* call the driver */
1633 if (!USER_Driver->pCreateWindow( hwnd )) goto failed;
1635 NotifyWinEvent(EVENT_OBJECT_CREATE, hwnd, OBJID_WINDOW, 0);
1637 /* send the size messages */
1639 if (!(win_get_flags( hwnd ) & WIN_NEED_SIZE))
1641 WIN_GetRectangles( hwnd, COORDS_PARENT, NULL, &rect );
1642 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1643 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1644 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( rect.left, rect.top ) );
1647 /* Show the window, maximizing or minimizing if needed */
1649 style = WIN_SetStyle( hwnd, 0, WS_MAXIMIZE | WS_MINIMIZE );
1650 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1652 RECT newPos;
1653 UINT swFlag = (style & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1655 swFlag = WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1656 swFlag |= SWP_FRAMECHANGED; /* Frame always gets changed */
1657 if (!(style & WS_VISIBLE) || (style & WS_CHILD) || GetActiveWindow()) swFlag |= SWP_NOACTIVATE;
1658 SetWindowPos( hwnd, 0, newPos.left, newPos.top, newPos.right - newPos.left,
1659 newPos.bottom - newPos.top, swFlag );
1662 /* Notify the parent window only */
1664 send_parent_notify( hwnd, WM_CREATE );
1665 if (!IsWindow( hwnd )) return 0;
1667 if (parent == GetDesktopWindow())
1668 PostMessageW( parent, WM_PARENTNOTIFY, WM_CREATE, (LPARAM)hwnd );
1670 if (cs->style & WS_VISIBLE)
1672 if (cs->style & WS_MAXIMIZE)
1673 sw = SW_SHOW;
1674 else if (cs->style & WS_MINIMIZE)
1675 sw = SW_SHOWMINIMIZED;
1677 ShowWindow( hwnd, sw );
1678 if (cs->dwExStyle & WS_EX_MDICHILD)
1680 SendMessageW(cs->hwndParent, WM_MDIREFRESHMENU, 0, 0);
1681 /* ShowWindow won't activate child windows */
1682 SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE );
1686 /* Call WH_SHELL hook */
1688 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) && !GetWindow( hwnd, GW_OWNER ))
1689 HOOK_CallHooks( WH_SHELL, HSHELL_WINDOWCREATED, (WPARAM)hwnd, 0, TRUE );
1691 TRACE("created window %p\n", hwnd);
1692 return hwnd;
1694 failed:
1695 WIN_DestroyWindow( hwnd );
1696 return 0;
1700 /***********************************************************************
1701 * CreateWindowExA (USER32.@)
1703 HWND WINAPI DECLSPEC_HOTPATCH CreateWindowExA( DWORD exStyle, LPCSTR className,
1704 LPCSTR windowName, DWORD style, INT x,
1705 INT y, INT width, INT height,
1706 HWND parent, HMENU menu,
1707 HINSTANCE instance, LPVOID data )
1709 CREATESTRUCTA cs;
1711 cs.lpCreateParams = data;
1712 cs.hInstance = instance;
1713 cs.hMenu = menu;
1714 cs.hwndParent = parent;
1715 cs.x = x;
1716 cs.y = y;
1717 cs.cx = width;
1718 cs.cy = height;
1719 cs.style = style;
1720 cs.lpszName = windowName;
1721 cs.lpszClass = className;
1722 cs.dwExStyle = exStyle;
1724 if (!IS_INTRESOURCE(className))
1726 WCHAR bufferW[256];
1727 if (!MultiByteToWideChar( CP_ACP, 0, className, -1, bufferW, sizeof(bufferW)/sizeof(WCHAR) ))
1728 return 0;
1729 return wow_handlers.create_window( (CREATESTRUCTW *)&cs, bufferW, instance, FALSE );
1731 /* Note: we rely on the fact that CREATESTRUCTA and */
1732 /* CREATESTRUCTW have the same layout. */
1733 return wow_handlers.create_window( (CREATESTRUCTW *)&cs, (LPCWSTR)className, instance, FALSE );
1737 /***********************************************************************
1738 * CreateWindowExW (USER32.@)
1740 HWND WINAPI DECLSPEC_HOTPATCH CreateWindowExW( DWORD exStyle, LPCWSTR className,
1741 LPCWSTR windowName, DWORD style, INT x,
1742 INT y, INT width, INT height,
1743 HWND parent, HMENU menu,
1744 HINSTANCE instance, LPVOID data )
1746 CREATESTRUCTW cs;
1748 cs.lpCreateParams = data;
1749 cs.hInstance = instance;
1750 cs.hMenu = menu;
1751 cs.hwndParent = parent;
1752 cs.x = x;
1753 cs.y = y;
1754 cs.cx = width;
1755 cs.cy = height;
1756 cs.style = style;
1757 cs.lpszName = windowName;
1758 cs.lpszClass = className;
1759 cs.dwExStyle = exStyle;
1761 return wow_handlers.create_window( &cs, className, instance, TRUE );
1765 /***********************************************************************
1766 * WIN_SendDestroyMsg
1768 static void WIN_SendDestroyMsg( HWND hwnd )
1770 GUITHREADINFO info;
1772 info.cbSize = sizeof(info);
1773 if (GetGUIThreadInfo( GetCurrentThreadId(), &info ))
1775 if (hwnd == info.hwndCaret) DestroyCaret();
1776 if (hwnd == info.hwndActive) WINPOS_ActivateOtherWindow( hwnd );
1780 * Send the WM_DESTROY to the window.
1782 SendMessageW( hwnd, WM_DESTROY, 0, 0);
1785 * This WM_DESTROY message can trigger re-entrant calls to DestroyWindow
1786 * make sure that the window still exists when we come back.
1788 if (IsWindow(hwnd))
1790 HWND* pWndArray;
1791 int i;
1793 if (!(pWndArray = WIN_ListChildren( hwnd ))) return;
1795 for (i = 0; pWndArray[i]; i++)
1797 if (IsWindow( pWndArray[i] )) WIN_SendDestroyMsg( pWndArray[i] );
1799 HeapFree( GetProcessHeap(), 0, pWndArray );
1801 else
1802 WARN("\tdestroyed itself while in WM_DESTROY!\n");
1806 /***********************************************************************
1807 * DestroyWindow (USER32.@)
1809 BOOL WINAPI DestroyWindow( HWND hwnd )
1811 BOOL is_child;
1813 if (!(hwnd = WIN_IsCurrentThread( hwnd )) || is_desktop_window( hwnd ))
1815 SetLastError( ERROR_ACCESS_DENIED );
1816 return FALSE;
1819 TRACE("(%p)\n", hwnd);
1821 /* Call hooks */
1823 if (HOOK_CallHooks( WH_CBT, HCBT_DESTROYWND, (WPARAM)hwnd, 0, TRUE )) return FALSE;
1825 if (MENU_IsMenuActive() == hwnd)
1826 EndMenu();
1828 is_child = (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) != 0;
1830 if (is_child)
1832 if (!USER_IsExitingThread( GetCurrentThreadId() ))
1833 send_parent_notify( hwnd, WM_DESTROY );
1835 else if (!GetWindow( hwnd, GW_OWNER ))
1837 HOOK_CallHooks( WH_SHELL, HSHELL_WINDOWDESTROYED, (WPARAM)hwnd, 0L, TRUE );
1838 /* FIXME: clean up palette - see "Internals" p.352 */
1841 if (!IsWindow(hwnd)) return TRUE;
1843 /* Hide the window */
1844 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)
1846 /* Only child windows receive WM_SHOWWINDOW in DestroyWindow() */
1847 if (is_child)
1848 ShowWindow( hwnd, SW_HIDE );
1849 else
1850 SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE |
1851 SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW );
1854 if (!IsWindow(hwnd)) return TRUE;
1856 /* Recursively destroy owned windows */
1858 if (!is_child)
1860 for (;;)
1862 int i;
1863 BOOL got_one = FALSE;
1864 HWND *list = WIN_ListChildren( GetDesktopWindow() );
1865 if (list)
1867 for (i = 0; list[i]; i++)
1869 if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
1870 if (WIN_IsCurrentThread( list[i] ))
1872 DestroyWindow( list[i] );
1873 got_one = TRUE;
1874 continue;
1876 WIN_SetOwner( list[i], 0 );
1878 HeapFree( GetProcessHeap(), 0, list );
1880 if (!got_one) break;
1884 /* Send destroy messages */
1886 WIN_SendDestroyMsg( hwnd );
1887 if (!IsWindow( hwnd )) return TRUE;
1889 CLIPBOARD_ReleaseOwner( hwnd );
1891 /* Destroy the window storage */
1893 WIN_DestroyWindow( hwnd );
1894 return TRUE;
1898 /***********************************************************************
1899 * CloseWindow (USER32.@)
1901 BOOL WINAPI CloseWindow( HWND hwnd )
1903 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) return FALSE;
1904 ShowWindow( hwnd, SW_MINIMIZE );
1905 return TRUE;
1909 /***********************************************************************
1910 * OpenIcon (USER32.@)
1912 BOOL WINAPI OpenIcon( HWND hwnd )
1914 if (!IsIconic( hwnd )) return FALSE;
1915 ShowWindow( hwnd, SW_SHOWNORMAL );
1916 return TRUE;
1920 /***********************************************************************
1921 * FindWindowExW (USER32.@)
1923 HWND WINAPI FindWindowExW( HWND parent, HWND child, LPCWSTR className, LPCWSTR title )
1925 HWND *list;
1926 HWND retvalue = 0;
1927 int i = 0, len = 0;
1928 WCHAR *buffer = NULL;
1930 if (!parent && child) parent = GetDesktopWindow();
1931 else if (parent == HWND_MESSAGE) parent = get_hwnd_message_parent();
1933 if (title)
1935 len = strlenW(title) + 1; /* one extra char to check for chars beyond the end */
1936 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return 0;
1939 if (!(list = list_window_children( 0, parent, className, 0 ))) goto done;
1941 if (child)
1943 child = WIN_GetFullHandle( child );
1944 while (list[i] && list[i] != child) i++;
1945 if (!list[i]) goto done;
1946 i++; /* start from next window */
1949 if (title)
1951 while (list[i])
1953 if (InternalGetWindowText( list[i], buffer, len + 1 ))
1955 if (!strcmpiW( buffer, title )) break;
1957 else
1959 if (!title[0]) break;
1961 i++;
1964 retvalue = list[i];
1966 done:
1967 HeapFree( GetProcessHeap(), 0, list );
1968 HeapFree( GetProcessHeap(), 0, buffer );
1969 return retvalue;
1974 /***********************************************************************
1975 * FindWindowA (USER32.@)
1977 HWND WINAPI FindWindowA( LPCSTR className, LPCSTR title )
1979 HWND ret = FindWindowExA( 0, 0, className, title );
1980 if (!ret) SetLastError (ERROR_CANNOT_FIND_WND_CLASS);
1981 return ret;
1985 /***********************************************************************
1986 * FindWindowExA (USER32.@)
1988 HWND WINAPI FindWindowExA( HWND parent, HWND child, LPCSTR className, LPCSTR title )
1990 LPWSTR titleW = NULL;
1991 HWND hwnd = 0;
1993 if (title)
1995 DWORD len = MultiByteToWideChar( CP_ACP, 0, title, -1, NULL, 0 );
1996 if (!(titleW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return 0;
1997 MultiByteToWideChar( CP_ACP, 0, title, -1, titleW, len );
2000 if (!IS_INTRESOURCE(className))
2002 WCHAR classW[256];
2003 if (MultiByteToWideChar( CP_ACP, 0, className, -1, classW, sizeof(classW)/sizeof(WCHAR) ))
2004 hwnd = FindWindowExW( parent, child, classW, titleW );
2006 else
2008 hwnd = FindWindowExW( parent, child, (LPCWSTR)className, titleW );
2011 HeapFree( GetProcessHeap(), 0, titleW );
2012 return hwnd;
2016 /***********************************************************************
2017 * FindWindowW (USER32.@)
2019 HWND WINAPI FindWindowW( LPCWSTR className, LPCWSTR title )
2021 return FindWindowExW( 0, 0, className, title );
2025 /**********************************************************************
2026 * GetDesktopWindow (USER32.@)
2028 HWND WINAPI GetDesktopWindow(void)
2030 struct user_thread_info *thread_info = get_user_thread_info();
2032 if (thread_info->top_window) return thread_info->top_window;
2034 SERVER_START_REQ( get_desktop_window )
2036 req->force = 0;
2037 if (!wine_server_call( req ))
2039 thread_info->top_window = wine_server_ptr_handle( reply->top_window );
2040 thread_info->msg_window = wine_server_ptr_handle( reply->msg_window );
2043 SERVER_END_REQ;
2045 if (!thread_info->top_window)
2047 static const WCHAR explorer[] = {'\\','e','x','p','l','o','r','e','r','.','e','x','e',0};
2048 static const WCHAR args[] = {' ','/','d','e','s','k','t','o','p',0};
2049 STARTUPINFOW si;
2050 PROCESS_INFORMATION pi;
2051 WCHAR windir[MAX_PATH];
2052 WCHAR app[MAX_PATH + sizeof(explorer)/sizeof(WCHAR)];
2053 WCHAR cmdline[MAX_PATH + (sizeof(explorer) + sizeof(args))/sizeof(WCHAR)];
2054 WCHAR desktop[MAX_PATH];
2055 void *redir;
2057 SERVER_START_REQ( set_user_object_info )
2059 req->handle = wine_server_obj_handle( GetThreadDesktop(GetCurrentThreadId()) );
2060 req->flags = SET_USER_OBJECT_GET_FULL_NAME;
2061 wine_server_set_reply( req, desktop, sizeof(desktop) - sizeof(WCHAR) );
2062 if (!wine_server_call( req ))
2064 size_t size = wine_server_reply_size( reply );
2065 desktop[size / sizeof(WCHAR)] = 0;
2066 TRACE( "starting explorer for desktop %s\n", debugstr_w(desktop) );
2068 else
2069 desktop[0] = 0;
2071 SERVER_END_REQ;
2073 memset( &si, 0, sizeof(si) );
2074 si.cb = sizeof(si);
2075 si.lpDesktop = *desktop ? desktop : NULL;
2076 si.dwFlags = STARTF_USESTDHANDLES;
2077 si.hStdInput = 0;
2078 si.hStdOutput = 0;
2079 si.hStdError = GetStdHandle( STD_ERROR_HANDLE );
2081 GetSystemDirectoryW( windir, MAX_PATH );
2082 strcpyW( app, windir );
2083 strcatW( app, explorer );
2084 strcpyW( cmdline, app );
2085 strcatW( cmdline, args );
2087 Wow64DisableWow64FsRedirection( &redir );
2088 if (CreateProcessW( app, cmdline, NULL, NULL, FALSE, DETACHED_PROCESS,
2089 NULL, windir, &si, &pi ))
2091 TRACE( "started explorer pid %04x tid %04x\n", pi.dwProcessId, pi.dwThreadId );
2092 WaitForInputIdle( pi.hProcess, 10000 );
2093 CloseHandle( pi.hThread );
2094 CloseHandle( pi.hProcess );
2096 else WARN( "failed to start explorer, err %d\n", GetLastError() );
2097 Wow64RevertWow64FsRedirection( redir );
2099 SERVER_START_REQ( get_desktop_window )
2101 req->force = 1;
2102 if (!wine_server_call( req ))
2104 thread_info->top_window = wine_server_ptr_handle( reply->top_window );
2105 thread_info->msg_window = wine_server_ptr_handle( reply->msg_window );
2108 SERVER_END_REQ;
2111 if (!thread_info->top_window || !USER_Driver->pCreateDesktopWindow( thread_info->top_window ))
2112 ERR( "failed to create desktop window\n" );
2114 return thread_info->top_window;
2118 /*******************************************************************
2119 * EnableWindow (USER32.@)
2121 BOOL WINAPI EnableWindow( HWND hwnd, BOOL enable )
2123 BOOL retvalue;
2124 HWND full_handle;
2126 if (is_broadcast(hwnd))
2128 SetLastError( ERROR_INVALID_PARAMETER );
2129 return FALSE;
2132 if (!(full_handle = WIN_IsCurrentThread( hwnd )))
2133 return SendMessageW( hwnd, WM_WINE_ENABLEWINDOW, enable, 0 );
2135 hwnd = full_handle;
2137 TRACE("( %p, %d )\n", hwnd, enable);
2139 retvalue = !IsWindowEnabled( hwnd );
2141 if (enable && retvalue)
2143 WIN_SetStyle( hwnd, 0, WS_DISABLED );
2144 SendMessageW( hwnd, WM_ENABLE, TRUE, 0 );
2146 else if (!enable && !retvalue)
2148 HWND capture_wnd;
2150 SendMessageW( hwnd, WM_CANCELMODE, 0, 0);
2152 WIN_SetStyle( hwnd, WS_DISABLED, 0 );
2154 if (hwnd == GetFocus())
2155 SetFocus( 0 ); /* A disabled window can't have the focus */
2157 capture_wnd = GetCapture();
2158 if (hwnd == capture_wnd || IsChild(hwnd, capture_wnd))
2159 ReleaseCapture(); /* A disabled window can't capture the mouse */
2161 SendMessageW( hwnd, WM_ENABLE, FALSE, 0 );
2163 return retvalue;
2167 /***********************************************************************
2168 * IsWindowEnabled (USER32.@)
2170 BOOL WINAPI IsWindowEnabled(HWND hWnd)
2172 return !(GetWindowLongW( hWnd, GWL_STYLE ) & WS_DISABLED);
2176 /***********************************************************************
2177 * IsWindowUnicode (USER32.@)
2179 BOOL WINAPI IsWindowUnicode( HWND hwnd )
2181 WND * wndPtr;
2182 BOOL retvalue = FALSE;
2184 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
2186 if (wndPtr == WND_DESKTOP) return TRUE;
2188 if (wndPtr != WND_OTHER_PROCESS)
2190 retvalue = (wndPtr->flags & WIN_ISUNICODE) != 0;
2191 WIN_ReleasePtr( wndPtr );
2193 else
2195 SERVER_START_REQ( get_window_info )
2197 req->handle = wine_server_user_handle( hwnd );
2198 if (!wine_server_call_err( req )) retvalue = reply->is_unicode;
2200 SERVER_END_REQ;
2202 return retvalue;
2206 /**********************************************************************
2207 * WIN_GetWindowLong
2209 * Helper function for GetWindowLong().
2211 static LONG_PTR WIN_GetWindowLong( HWND hwnd, INT offset, UINT size, BOOL unicode )
2213 LONG_PTR retvalue = 0;
2214 WND *wndPtr;
2216 if (offset == GWLP_HWNDPARENT)
2218 HWND parent = GetAncestor( hwnd, GA_PARENT );
2219 if (parent == GetDesktopWindow()) parent = GetWindow( hwnd, GW_OWNER );
2220 return (ULONG_PTR)parent;
2223 if (!(wndPtr = WIN_GetPtr( hwnd )))
2225 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2226 return 0;
2229 if (wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP)
2231 if (offset == GWLP_WNDPROC)
2233 SetLastError( ERROR_ACCESS_DENIED );
2234 return 0;
2236 SERVER_START_REQ( set_window_info )
2238 req->handle = wine_server_user_handle( hwnd );
2239 req->flags = 0; /* don't set anything, just retrieve */
2240 req->extra_offset = (offset >= 0) ? offset : -1;
2241 req->extra_size = (offset >= 0) ? size : 0;
2242 if (!wine_server_call_err( req ))
2244 switch(offset)
2246 case GWL_STYLE: retvalue = reply->old_style; break;
2247 case GWL_EXSTYLE: retvalue = reply->old_ex_style; break;
2248 case GWLP_ID: retvalue = reply->old_id; break;
2249 case GWLP_HINSTANCE: retvalue = (ULONG_PTR)wine_server_get_ptr( reply->old_instance ); break;
2250 case GWLP_USERDATA: retvalue = reply->old_user_data; break;
2251 default:
2252 if (offset >= 0) retvalue = get_win_data( &reply->old_extra_value, size );
2253 else SetLastError( ERROR_INVALID_INDEX );
2254 break;
2258 SERVER_END_REQ;
2259 return retvalue;
2262 /* now we have a valid wndPtr */
2264 if (offset >= 0)
2266 if (offset > (int)(wndPtr->cbWndExtra - size))
2268 WARN("Invalid offset %d\n", offset );
2269 WIN_ReleasePtr( wndPtr );
2270 SetLastError( ERROR_INVALID_INDEX );
2271 return 0;
2273 retvalue = get_win_data( (char *)wndPtr->wExtra + offset, size );
2275 /* Special case for dialog window procedure */
2276 if ((offset == DWLP_DLGPROC) && (size == sizeof(LONG_PTR)) && wndPtr->dlgInfo)
2277 retvalue = (LONG_PTR)WINPROC_GetProc( (WNDPROC)retvalue, unicode );
2278 WIN_ReleasePtr( wndPtr );
2279 return retvalue;
2282 switch(offset)
2284 case GWLP_USERDATA: retvalue = wndPtr->userdata; break;
2285 case GWL_STYLE: retvalue = wndPtr->dwStyle; break;
2286 case GWL_EXSTYLE: retvalue = wndPtr->dwExStyle; break;
2287 case GWLP_ID: retvalue = wndPtr->wIDmenu; break;
2288 case GWLP_HINSTANCE: retvalue = (ULONG_PTR)wndPtr->hInstance; break;
2289 case GWLP_WNDPROC:
2290 /* This looks like a hack only for the edit control (see tests). This makes these controls
2291 * more tolerant to A/W mismatches. The lack of W->A->W conversion for such a mismatch suggests
2292 * that the hack is in GetWindowLongPtr[AW], not in winprocs.
2294 if (wndPtr->winproc == BUILTIN_WINPROC(WINPROC_EDIT) && (!unicode != !(wndPtr->flags & WIN_ISUNICODE)))
2295 retvalue = (ULONG_PTR)wndPtr->winproc;
2296 else
2297 retvalue = (ULONG_PTR)WINPROC_GetProc( wndPtr->winproc, unicode );
2298 break;
2299 default:
2300 WARN("Unknown offset %d\n", offset );
2301 SetLastError( ERROR_INVALID_INDEX );
2302 break;
2304 WIN_ReleasePtr(wndPtr);
2305 return retvalue;
2309 /**********************************************************************
2310 * WIN_SetWindowLong
2312 * Helper function for SetWindowLong().
2314 * 0 is the failure code. However, in the case of failure SetLastError
2315 * must be set to distinguish between a 0 return value and a failure.
2317 LONG_PTR WIN_SetWindowLong( HWND hwnd, INT offset, UINT size, LONG_PTR newval, BOOL unicode )
2319 STYLESTRUCT style;
2320 BOOL ok, made_visible = FALSE;
2321 LONG_PTR retval = 0;
2322 WND *wndPtr;
2324 TRACE( "%p %d %lx %c\n", hwnd, offset, newval, unicode ? 'W' : 'A' );
2326 if (is_broadcast(hwnd))
2328 SetLastError( ERROR_INVALID_PARAMETER );
2329 return FALSE;
2332 if (!(wndPtr = WIN_GetPtr( hwnd )))
2334 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2335 return 0;
2337 if (wndPtr == WND_DESKTOP)
2339 /* can't change anything on the desktop window */
2340 SetLastError( ERROR_ACCESS_DENIED );
2341 return 0;
2343 if (wndPtr == WND_OTHER_PROCESS)
2345 if (offset == GWLP_WNDPROC)
2347 SetLastError( ERROR_ACCESS_DENIED );
2348 return 0;
2350 if (offset > 32767 || offset < -32767)
2352 SetLastError( ERROR_INVALID_INDEX );
2353 return 0;
2355 return SendMessageW( hwnd, WM_WINE_SETWINDOWLONG, MAKEWPARAM( offset, size ), newval );
2358 /* first some special cases */
2359 switch( offset )
2361 case GWL_STYLE:
2362 style.styleOld = wndPtr->dwStyle;
2363 style.styleNew = newval;
2364 WIN_ReleasePtr( wndPtr );
2365 SendMessageW( hwnd, WM_STYLECHANGING, GWL_STYLE, (LPARAM)&style );
2366 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
2367 newval = style.styleNew;
2368 /* WS_CLIPSIBLINGS can't be reset on top-level windows */
2369 if (wndPtr->parent == GetDesktopWindow()) newval |= WS_CLIPSIBLINGS;
2370 /* FIXME: changing WS_DLGFRAME | WS_THICKFRAME is supposed to change
2371 WS_EX_WINDOWEDGE too */
2372 break;
2373 case GWL_EXSTYLE:
2374 style.styleOld = wndPtr->dwExStyle;
2375 style.styleNew = newval;
2376 WIN_ReleasePtr( wndPtr );
2377 SendMessageW( hwnd, WM_STYLECHANGING, GWL_EXSTYLE, (LPARAM)&style );
2378 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
2379 /* WS_EX_TOPMOST can only be changed through SetWindowPos */
2380 newval = (style.styleNew & ~WS_EX_TOPMOST) | (wndPtr->dwExStyle & WS_EX_TOPMOST);
2381 /* WS_EX_WINDOWEDGE depends on some other styles */
2382 if (newval & WS_EX_DLGMODALFRAME)
2383 newval |= WS_EX_WINDOWEDGE;
2384 else if (!(newval & WS_EX_STATICEDGE) && (wndPtr->dwStyle & (WS_DLGFRAME | WS_THICKFRAME)))
2385 newval |= WS_EX_WINDOWEDGE;
2386 else
2387 newval &= ~WS_EX_WINDOWEDGE;
2388 break;
2389 case GWLP_HWNDPARENT:
2390 if (wndPtr->parent == GetDesktopWindow())
2392 WIN_ReleasePtr( wndPtr );
2393 return (ULONG_PTR)WIN_SetOwner( hwnd, (HWND)newval );
2395 else
2397 WIN_ReleasePtr( wndPtr );
2398 return (ULONG_PTR)SetParent( hwnd, (HWND)newval );
2400 case GWLP_WNDPROC:
2402 WNDPROC proc;
2403 UINT old_flags = wndPtr->flags;
2404 retval = WIN_GetWindowLong( hwnd, offset, size, unicode );
2405 proc = WINPROC_AllocProc( (WNDPROC)newval, unicode );
2406 if (proc) wndPtr->winproc = proc;
2407 if (WINPROC_IsUnicode( proc, unicode )) wndPtr->flags |= WIN_ISUNICODE;
2408 else wndPtr->flags &= ~WIN_ISUNICODE;
2409 if (!((old_flags ^ wndPtr->flags) & WIN_ISUNICODE))
2411 WIN_ReleasePtr( wndPtr );
2412 return retval;
2414 /* update is_unicode flag on the server side */
2415 break;
2417 case GWLP_ID:
2418 case GWLP_HINSTANCE:
2419 case GWLP_USERDATA:
2420 break;
2421 case DWLP_DLGPROC:
2422 if ((wndPtr->cbWndExtra - sizeof(LONG_PTR) >= DWLP_DLGPROC) &&
2423 (size == sizeof(LONG_PTR)) && wndPtr->dlgInfo)
2425 WNDPROC *ptr = (WNDPROC *)((char *)wndPtr->wExtra + DWLP_DLGPROC);
2426 retval = (ULONG_PTR)WINPROC_GetProc( *ptr, unicode );
2427 *ptr = WINPROC_AllocProc( (WNDPROC)newval, unicode );
2428 WIN_ReleasePtr( wndPtr );
2429 return retval;
2431 /* fall through */
2432 default:
2433 if (offset < 0 || offset > (int)(wndPtr->cbWndExtra - size))
2435 WARN("Invalid offset %d\n", offset );
2436 WIN_ReleasePtr( wndPtr );
2437 SetLastError( ERROR_INVALID_INDEX );
2438 return 0;
2440 else if (get_win_data( (char *)wndPtr->wExtra + offset, size ) == newval)
2442 /* already set to the same value */
2443 WIN_ReleasePtr( wndPtr );
2444 return newval;
2446 break;
2449 SERVER_START_REQ( set_window_info )
2451 req->handle = wine_server_user_handle( hwnd );
2452 req->extra_offset = -1;
2453 switch(offset)
2455 case GWL_STYLE:
2456 req->flags = SET_WIN_STYLE;
2457 req->style = newval;
2458 break;
2459 case GWL_EXSTYLE:
2460 req->flags = SET_WIN_EXSTYLE;
2461 req->ex_style = newval;
2462 break;
2463 case GWLP_ID:
2464 req->flags = SET_WIN_ID;
2465 req->id = newval;
2466 break;
2467 case GWLP_HINSTANCE:
2468 req->flags = SET_WIN_INSTANCE;
2469 req->instance = wine_server_client_ptr( (void *)newval );
2470 break;
2471 case GWLP_WNDPROC:
2472 req->flags = SET_WIN_UNICODE;
2473 req->is_unicode = (wndPtr->flags & WIN_ISUNICODE) != 0;
2474 break;
2475 case GWLP_USERDATA:
2476 req->flags = SET_WIN_USERDATA;
2477 req->user_data = newval;
2478 break;
2479 default:
2480 req->flags = SET_WIN_EXTRA;
2481 req->extra_offset = offset;
2482 req->extra_size = size;
2483 set_win_data( &req->extra_value, newval, size );
2485 if ((ok = !wine_server_call_err( req )))
2487 switch(offset)
2489 case GWL_STYLE:
2490 wndPtr->dwStyle = newval;
2491 retval = reply->old_style;
2492 break;
2493 case GWL_EXSTYLE:
2494 wndPtr->dwExStyle = newval;
2495 retval = reply->old_ex_style;
2496 break;
2497 case GWLP_ID:
2498 wndPtr->wIDmenu = newval;
2499 retval = reply->old_id;
2500 break;
2501 case GWLP_HINSTANCE:
2502 wndPtr->hInstance = (HINSTANCE)newval;
2503 retval = (ULONG_PTR)wine_server_get_ptr( reply->old_instance );
2504 break;
2505 case GWLP_WNDPROC:
2506 break;
2507 case GWLP_USERDATA:
2508 wndPtr->userdata = newval;
2509 retval = reply->old_user_data;
2510 break;
2511 default:
2512 retval = get_win_data( (char *)wndPtr->wExtra + offset, size );
2513 set_win_data( (char *)wndPtr->wExtra + offset, newval, size );
2514 break;
2518 SERVER_END_REQ;
2520 if ((offset == GWL_STYLE && ((style.styleOld ^ style.styleNew) & WS_VISIBLE)) ||
2521 (offset == GWL_EXSTYLE && ((style.styleOld ^ style.styleNew) & WS_EX_LAYERED)))
2523 made_visible = (wndPtr->dwStyle & WS_VISIBLE) != 0;
2524 invalidate_dce( wndPtr, NULL );
2526 WIN_ReleasePtr( wndPtr );
2528 if (!ok) return 0;
2530 if (offset == GWL_STYLE || offset == GWL_EXSTYLE)
2532 style.styleOld = retval;
2533 style.styleNew = newval;
2534 USER_Driver->pSetWindowStyle( hwnd, offset, &style );
2535 if (made_visible) update_window_state( hwnd );
2536 SendMessageW( hwnd, WM_STYLECHANGED, offset, (LPARAM)&style );
2539 return retval;
2543 /**********************************************************************
2544 * GetWindowWord (USER32.@)
2546 WORD WINAPI GetWindowWord( HWND hwnd, INT offset )
2548 switch(offset)
2550 case GWLP_ID:
2551 case GWLP_HINSTANCE:
2552 case GWLP_HWNDPARENT:
2553 break;
2554 default:
2555 if (offset < 0)
2557 WARN("Invalid offset %d\n", offset );
2558 SetLastError( ERROR_INVALID_INDEX );
2559 return 0;
2561 break;
2563 return WIN_GetWindowLong( hwnd, offset, sizeof(WORD), FALSE );
2567 /**********************************************************************
2568 * GetWindowLongA (USER32.@)
2570 LONG WINAPI GetWindowLongA( HWND hwnd, INT offset )
2572 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG), FALSE );
2576 /**********************************************************************
2577 * GetWindowLongW (USER32.@)
2579 LONG WINAPI GetWindowLongW( HWND hwnd, INT offset )
2581 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG), TRUE );
2585 /**********************************************************************
2586 * SetWindowWord (USER32.@)
2588 WORD WINAPI SetWindowWord( HWND hwnd, INT offset, WORD newval )
2590 switch(offset)
2592 case GWLP_ID:
2593 case GWLP_HINSTANCE:
2594 case GWLP_HWNDPARENT:
2595 break;
2596 default:
2597 if (offset < 0)
2599 WARN("Invalid offset %d\n", offset );
2600 SetLastError( ERROR_INVALID_INDEX );
2601 return 0;
2603 break;
2605 return WIN_SetWindowLong( hwnd, offset, sizeof(WORD), newval, FALSE );
2609 /**********************************************************************
2610 * SetWindowLongA (USER32.@)
2612 * See SetWindowLongW.
2614 LONG WINAPI DECLSPEC_HOTPATCH SetWindowLongA( HWND hwnd, INT offset, LONG newval )
2616 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG), newval, FALSE );
2620 /**********************************************************************
2621 * SetWindowLongW (USER32.@) Set window attribute
2623 * SetWindowLong() alters one of a window's attributes or sets a 32-bit (long)
2624 * value in a window's extra memory.
2626 * The _hwnd_ parameter specifies the handle to a window that
2627 * has extra memory. The _newval_ parameter contains the new
2628 * attribute or extra memory value. If positive, the _offset_
2629 * parameter is the byte-addressed location in the window's extra
2630 * memory to set. If negative, _offset_ specifies the window
2631 * attribute to set, and should be one of the following values:
2633 * GWL_EXSTYLE The window's extended window style
2635 * GWL_STYLE The window's window style.
2637 * GWLP_WNDPROC Pointer to the window's window procedure.
2639 * GWLP_HINSTANCE The window's application instance handle.
2641 * GWLP_ID The window's identifier.
2643 * GWLP_USERDATA The window's user-specified data.
2645 * If the window is a dialog box, the _offset_ parameter can be one of
2646 * the following values:
2648 * DWLP_DLGPROC The address of the window's dialog box procedure.
2650 * DWLP_MSGRESULT The return value of a message
2651 * that the dialog box procedure processed.
2653 * DWLP_USER Application specific information.
2655 * RETURNS
2657 * If successful, returns the previous value located at _offset_. Otherwise,
2658 * returns 0.
2660 * NOTES
2662 * Extra memory for a window class is specified by a nonzero cbWndExtra
2663 * parameter of the WNDCLASS structure passed to RegisterClass() at the
2664 * time of class creation.
2666 * Using GWL_WNDPROC to set a new window procedure effectively creates
2667 * a window subclass. Use CallWindowProc() in the new windows procedure
2668 * to pass messages to the superclass's window procedure.
2670 * The user data is reserved for use by the application which created
2671 * the window.
2673 * Do not use GWL_STYLE to change the window's WS_DISABLED style;
2674 * instead, call the EnableWindow() function to change the window's
2675 * disabled state.
2677 * Do not use GWL_HWNDPARENT to reset the window's parent, use
2678 * SetParent() instead.
2680 * Win95:
2681 * When offset is GWL_STYLE and the calling app's ver is 4.0,
2682 * it sends WM_STYLECHANGING before changing the settings
2683 * and WM_STYLECHANGED afterwards.
2684 * App ver 4.0 can't use SetWindowLong to change WS_EX_TOPMOST.
2686 LONG WINAPI DECLSPEC_HOTPATCH SetWindowLongW(
2687 HWND hwnd, /* [in] window to alter */
2688 INT offset, /* [in] offset, in bytes, of location to alter */
2689 LONG newval /* [in] new value of location */
2691 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG), newval, TRUE );
2695 /*******************************************************************
2696 * GetWindowTextA (USER32.@)
2698 INT WINAPI GetWindowTextA( HWND hwnd, LPSTR lpString, INT nMaxCount )
2700 WCHAR *buffer;
2702 if (!lpString || nMaxCount <= 0) return 0;
2704 if (WIN_IsCurrentProcess( hwnd ))
2706 lpString[0] = 0;
2707 return (INT)SendMessageA( hwnd, WM_GETTEXT, nMaxCount, (LPARAM)lpString );
2710 /* when window belongs to other process, don't send a message */
2711 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, nMaxCount * sizeof(WCHAR) ))) return 0;
2712 get_server_window_text( hwnd, buffer, nMaxCount );
2713 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, lpString, nMaxCount, NULL, NULL ))
2714 lpString[nMaxCount-1] = 0;
2715 HeapFree( GetProcessHeap(), 0, buffer );
2716 return strlen(lpString);
2720 /*******************************************************************
2721 * InternalGetWindowText (USER32.@)
2723 INT WINAPI InternalGetWindowText(HWND hwnd,LPWSTR lpString,INT nMaxCount )
2725 WND *win;
2727 if (nMaxCount <= 0) return 0;
2728 if (!(win = WIN_GetPtr( hwnd ))) return 0;
2729 if (win == WND_DESKTOP) lpString[0] = 0;
2730 else if (win != WND_OTHER_PROCESS)
2732 if (win->text) lstrcpynW( lpString, win->text, nMaxCount );
2733 else lpString[0] = 0;
2734 WIN_ReleasePtr( win );
2736 else
2738 get_server_window_text( hwnd, lpString, nMaxCount );
2740 return strlenW(lpString);
2744 /*******************************************************************
2745 * GetWindowTextW (USER32.@)
2747 INT WINAPI GetWindowTextW( HWND hwnd, LPWSTR lpString, INT nMaxCount )
2749 if (!lpString || nMaxCount <= 0) return 0;
2751 if (WIN_IsCurrentProcess( hwnd ))
2753 lpString[0] = 0;
2754 return (INT)SendMessageW( hwnd, WM_GETTEXT, nMaxCount, (LPARAM)lpString );
2757 /* when window belongs to other process, don't send a message */
2758 get_server_window_text( hwnd, lpString, nMaxCount );
2759 return strlenW(lpString);
2763 /*******************************************************************
2764 * SetWindowTextA (USER32.@)
2765 * SetWindowText (USER32.@)
2767 BOOL WINAPI DECLSPEC_HOTPATCH SetWindowTextA( HWND hwnd, LPCSTR lpString )
2769 if (is_broadcast(hwnd))
2771 SetLastError( ERROR_INVALID_PARAMETER );
2772 return FALSE;
2774 if (!WIN_IsCurrentProcess( hwnd ))
2775 WARN( "setting text %s of other process window %p should not use SendMessage\n",
2776 debugstr_a(lpString), hwnd );
2777 return (BOOL)SendMessageA( hwnd, WM_SETTEXT, 0, (LPARAM)lpString );
2781 /*******************************************************************
2782 * SetWindowTextW (USER32.@)
2784 BOOL WINAPI DECLSPEC_HOTPATCH SetWindowTextW( HWND hwnd, LPCWSTR lpString )
2786 if (is_broadcast(hwnd))
2788 SetLastError( ERROR_INVALID_PARAMETER );
2789 return FALSE;
2791 if (!WIN_IsCurrentProcess( hwnd ))
2792 WARN( "setting text %s of other process window %p should not use SendMessage\n",
2793 debugstr_w(lpString), hwnd );
2794 return (BOOL)SendMessageW( hwnd, WM_SETTEXT, 0, (LPARAM)lpString );
2798 /*******************************************************************
2799 * GetWindowTextLengthA (USER32.@)
2801 INT WINAPI GetWindowTextLengthA( HWND hwnd )
2803 return SendMessageA( hwnd, WM_GETTEXTLENGTH, 0, 0 );
2806 /*******************************************************************
2807 * GetWindowTextLengthW (USER32.@)
2809 INT WINAPI GetWindowTextLengthW( HWND hwnd )
2811 return SendMessageW( hwnd, WM_GETTEXTLENGTH, 0, 0 );
2815 /*******************************************************************
2816 * IsWindow (USER32.@)
2818 BOOL WINAPI IsWindow( HWND hwnd )
2820 WND *ptr;
2821 BOOL ret;
2823 if (!(ptr = WIN_GetPtr( hwnd ))) return FALSE;
2824 if (ptr == WND_DESKTOP) return TRUE;
2826 if (ptr != WND_OTHER_PROCESS)
2828 WIN_ReleasePtr( ptr );
2829 return TRUE;
2832 /* check other processes */
2833 SERVER_START_REQ( get_window_info )
2835 req->handle = wine_server_user_handle( hwnd );
2836 ret = !wine_server_call_err( req );
2838 SERVER_END_REQ;
2839 return ret;
2843 /***********************************************************************
2844 * GetWindowThreadProcessId (USER32.@)
2846 DWORD WINAPI GetWindowThreadProcessId( HWND hwnd, LPDWORD process )
2848 WND *ptr;
2849 DWORD tid = 0;
2851 if (!(ptr = WIN_GetPtr( hwnd )))
2853 SetLastError( ERROR_INVALID_WINDOW_HANDLE);
2854 return 0;
2857 if (ptr != WND_OTHER_PROCESS && ptr != WND_DESKTOP)
2859 /* got a valid window */
2860 tid = ptr->tid;
2861 if (process) *process = GetCurrentProcessId();
2862 WIN_ReleasePtr( ptr );
2863 return tid;
2866 /* check other processes */
2867 SERVER_START_REQ( get_window_info )
2869 req->handle = wine_server_user_handle( hwnd );
2870 if (!wine_server_call_err( req ))
2872 tid = (DWORD)reply->tid;
2873 if (process) *process = (DWORD)reply->pid;
2876 SERVER_END_REQ;
2877 return tid;
2881 /*****************************************************************
2882 * GetParent (USER32.@)
2884 HWND WINAPI GetParent( HWND hwnd )
2886 WND *wndPtr;
2887 HWND retvalue = 0;
2889 if (!(wndPtr = WIN_GetPtr( hwnd )))
2891 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2892 return 0;
2894 if (wndPtr == WND_DESKTOP) return 0;
2895 if (wndPtr == WND_OTHER_PROCESS)
2897 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
2898 if (style & (WS_POPUP | WS_CHILD))
2900 SERVER_START_REQ( get_window_tree )
2902 req->handle = wine_server_user_handle( hwnd );
2903 if (!wine_server_call_err( req ))
2905 if (style & WS_POPUP) retvalue = wine_server_ptr_handle( reply->owner );
2906 else if (style & WS_CHILD) retvalue = wine_server_ptr_handle( reply->parent );
2909 SERVER_END_REQ;
2912 else
2914 if (wndPtr->dwStyle & WS_POPUP) retvalue = wndPtr->owner;
2915 else if (wndPtr->dwStyle & WS_CHILD) retvalue = wndPtr->parent;
2916 WIN_ReleasePtr( wndPtr );
2918 return retvalue;
2922 /*****************************************************************
2923 * GetAncestor (USER32.@)
2925 HWND WINAPI GetAncestor( HWND hwnd, UINT type )
2927 WND *win;
2928 HWND *list, ret = 0;
2930 switch(type)
2932 case GA_PARENT:
2933 if (!(win = WIN_GetPtr( hwnd )))
2935 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2936 return 0;
2938 if (win == WND_DESKTOP) return 0;
2939 if (win != WND_OTHER_PROCESS)
2941 ret = win->parent;
2942 WIN_ReleasePtr( win );
2944 else /* need to query the server */
2946 SERVER_START_REQ( get_window_tree )
2948 req->handle = wine_server_user_handle( hwnd );
2949 if (!wine_server_call_err( req )) ret = wine_server_ptr_handle( reply->parent );
2951 SERVER_END_REQ;
2953 break;
2955 case GA_ROOT:
2956 if (!(list = list_window_parents( hwnd ))) return 0;
2958 if (!list[0] || !list[1]) ret = WIN_GetFullHandle( hwnd ); /* top-level window */
2959 else
2961 int count = 2;
2962 while (list[count]) count++;
2963 ret = list[count - 2]; /* get the one before the desktop */
2965 HeapFree( GetProcessHeap(), 0, list );
2966 break;
2968 case GA_ROOTOWNER:
2969 if (is_desktop_window( hwnd )) return 0;
2970 ret = WIN_GetFullHandle( hwnd );
2971 for (;;)
2973 HWND parent = GetParent( ret );
2974 if (!parent) break;
2975 ret = parent;
2977 break;
2979 return ret;
2983 /*****************************************************************
2984 * SetParent (USER32.@)
2986 HWND WINAPI SetParent( HWND hwnd, HWND parent )
2988 HWND full_handle;
2989 HWND old_parent = 0;
2990 BOOL was_visible;
2991 WND *wndPtr;
2992 POINT pt;
2993 BOOL ret;
2995 TRACE("(%p %p)\n", hwnd, parent);
2997 if (is_broadcast(hwnd) || is_broadcast(parent))
2999 SetLastError(ERROR_INVALID_PARAMETER);
3000 return 0;
3003 if (!parent) parent = GetDesktopWindow();
3004 else if (parent == HWND_MESSAGE) parent = get_hwnd_message_parent();
3005 else parent = WIN_GetFullHandle( parent );
3007 if (!IsWindow( parent ))
3009 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3010 return 0;
3013 /* Some applications try to set a child as a parent */
3014 if (IsChild(hwnd, parent))
3016 SetLastError( ERROR_INVALID_PARAMETER );
3017 return 0;
3020 if (!(full_handle = WIN_IsCurrentThread( hwnd )))
3021 return (HWND)SendMessageW( hwnd, WM_WINE_SETPARENT, (WPARAM)parent, 0 );
3023 if (full_handle == parent)
3025 SetLastError( ERROR_INVALID_PARAMETER );
3026 return 0;
3029 /* Windows hides the window first, then shows it again
3030 * including the WM_SHOWWINDOW messages and all */
3031 was_visible = ShowWindow( hwnd, SW_HIDE );
3033 wndPtr = WIN_GetPtr( hwnd );
3034 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return 0;
3036 pt.x = wndPtr->rectWindow.left;
3037 pt.y = wndPtr->rectWindow.top;
3039 SERVER_START_REQ( set_parent )
3041 req->handle = wine_server_user_handle( hwnd );
3042 req->parent = wine_server_user_handle( parent );
3043 if ((ret = !wine_server_call( req )))
3045 old_parent = wine_server_ptr_handle( reply->old_parent );
3046 wndPtr->parent = parent = wine_server_ptr_handle( reply->full_parent );
3050 SERVER_END_REQ;
3051 WIN_ReleasePtr( wndPtr );
3052 if (!ret) return 0;
3054 USER_Driver->pSetParent( full_handle, parent, old_parent );
3056 /* SetParent additionally needs to make hwnd the topmost window
3057 in the x-order and send the expected WM_WINDOWPOSCHANGING and
3058 WM_WINDOWPOSCHANGED notification messages.
3060 SetWindowPos( hwnd, HWND_TOP, pt.x, pt.y, 0, 0, SWP_NOSIZE );
3062 if (was_visible) ShowWindow( hwnd, SW_SHOW );
3064 return old_parent;
3068 /*******************************************************************
3069 * IsChild (USER32.@)
3071 BOOL WINAPI IsChild( HWND parent, HWND child )
3073 HWND *list;
3074 int i;
3075 BOOL ret = FALSE;
3077 if (!(GetWindowLongW( child, GWL_STYLE ) & WS_CHILD)) return FALSE;
3078 if (!(list = list_window_parents( child ))) return FALSE;
3079 parent = WIN_GetFullHandle( parent );
3080 for (i = 0; list[i]; i++)
3082 if (list[i] == parent)
3084 ret = list[i] && list[i+1];
3085 break;
3087 if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_CHILD)) break;
3089 HeapFree( GetProcessHeap(), 0, list );
3090 return ret;
3094 /***********************************************************************
3095 * IsWindowVisible (USER32.@)
3097 BOOL WINAPI IsWindowVisible( HWND hwnd )
3099 HWND *list;
3100 BOOL retval = TRUE;
3101 int i;
3103 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)) return FALSE;
3104 if (!(list = list_window_parents( hwnd ))) return TRUE;
3105 if (list[0])
3107 for (i = 0; list[i+1]; i++)
3108 if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_VISIBLE)) break;
3109 retval = !list[i+1] && (list[i] == GetDesktopWindow()); /* top message window isn't visible */
3111 HeapFree( GetProcessHeap(), 0, list );
3112 return retval;
3116 /***********************************************************************
3117 * WIN_IsWindowDrawable
3119 * hwnd is drawable when it is visible, all parents are not
3120 * minimized, and it is itself not minimized unless we are
3121 * trying to draw its default class icon.
3123 BOOL WIN_IsWindowDrawable( HWND hwnd, BOOL icon )
3125 HWND *list;
3126 BOOL retval = TRUE;
3127 int i;
3128 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
3130 if (!(style & WS_VISIBLE)) return FALSE;
3131 if ((style & WS_MINIMIZE) && icon && GetClassLongPtrW( hwnd, GCLP_HICON )) return FALSE;
3133 if (!(list = list_window_parents( hwnd ))) return TRUE;
3134 if (list[0])
3136 for (i = 0; list[i+1]; i++)
3137 if ((GetWindowLongW( list[i], GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != WS_VISIBLE)
3138 break;
3139 retval = !list[i+1] && (list[i] == GetDesktopWindow()); /* top message window isn't visible */
3141 HeapFree( GetProcessHeap(), 0, list );
3142 return retval;
3146 /*******************************************************************
3147 * GetTopWindow (USER32.@)
3149 HWND WINAPI GetTopWindow( HWND hwnd )
3151 if (!hwnd) hwnd = GetDesktopWindow();
3152 return GetWindow( hwnd, GW_CHILD );
3156 /*******************************************************************
3157 * GetWindow (USER32.@)
3159 HWND WINAPI GetWindow( HWND hwnd, UINT rel )
3161 HWND retval = 0;
3163 if (rel == GW_OWNER) /* this one may be available locally */
3165 WND *wndPtr = WIN_GetPtr( hwnd );
3166 if (!wndPtr)
3168 SetLastError( ERROR_INVALID_HANDLE );
3169 return 0;
3171 if (wndPtr == WND_DESKTOP) return 0;
3172 if (wndPtr != WND_OTHER_PROCESS)
3174 retval = wndPtr->owner;
3175 WIN_ReleasePtr( wndPtr );
3176 return retval;
3178 /* else fall through to server call */
3181 SERVER_START_REQ( get_window_tree )
3183 req->handle = wine_server_user_handle( hwnd );
3184 if (!wine_server_call_err( req ))
3186 switch(rel)
3188 case GW_HWNDFIRST:
3189 retval = wine_server_ptr_handle( reply->first_sibling );
3190 break;
3191 case GW_HWNDLAST:
3192 retval = wine_server_ptr_handle( reply->last_sibling );
3193 break;
3194 case GW_HWNDNEXT:
3195 retval = wine_server_ptr_handle( reply->next_sibling );
3196 break;
3197 case GW_HWNDPREV:
3198 retval = wine_server_ptr_handle( reply->prev_sibling );
3199 break;
3200 case GW_OWNER:
3201 retval = wine_server_ptr_handle( reply->owner );
3202 break;
3203 case GW_CHILD:
3204 retval = wine_server_ptr_handle( reply->first_child );
3205 break;
3209 SERVER_END_REQ;
3210 return retval;
3214 /*******************************************************************
3215 * ShowOwnedPopups (USER32.@)
3217 BOOL WINAPI ShowOwnedPopups( HWND owner, BOOL fShow )
3219 int count = 0;
3220 HWND *win_array = WIN_ListChildren( GetDesktopWindow() );
3222 if (!win_array) return TRUE;
3224 while (win_array[count]) count++;
3225 while (--count >= 0)
3227 if (GetWindow( win_array[count], GW_OWNER ) != owner) continue;
3228 if (fShow)
3230 if (win_get_flags( win_array[count] ) & WIN_NEEDS_SHOW_OWNEDPOPUP)
3231 /* In Windows, ShowOwnedPopups(TRUE) generates
3232 * WM_SHOWWINDOW messages with SW_PARENTOPENING,
3233 * regardless of the state of the owner
3235 SendMessageW(win_array[count], WM_SHOWWINDOW, SW_SHOWNORMAL, SW_PARENTOPENING);
3237 else
3239 if (GetWindowLongW( win_array[count], GWL_STYLE ) & WS_VISIBLE)
3240 /* In Windows, ShowOwnedPopups(FALSE) generates
3241 * WM_SHOWWINDOW messages with SW_PARENTCLOSING,
3242 * regardless of the state of the owner
3244 SendMessageW(win_array[count], WM_SHOWWINDOW, SW_HIDE, SW_PARENTCLOSING);
3247 HeapFree( GetProcessHeap(), 0, win_array );
3248 return TRUE;
3252 /*******************************************************************
3253 * GetLastActivePopup (USER32.@)
3255 HWND WINAPI GetLastActivePopup( HWND hwnd )
3257 HWND retval = hwnd;
3259 SERVER_START_REQ( get_window_info )
3261 req->handle = wine_server_user_handle( hwnd );
3262 if (!wine_server_call_err( req )) retval = wine_server_ptr_handle( reply->last_active );
3264 SERVER_END_REQ;
3265 return retval;
3269 /*******************************************************************
3270 * WIN_ListChildren
3272 * Build an array of the children of a given window. The array must be
3273 * freed with HeapFree. Returns NULL when no windows are found.
3275 HWND *WIN_ListChildren( HWND hwnd )
3277 if (!hwnd)
3279 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3280 return NULL;
3282 return list_window_children( 0, hwnd, NULL, 0 );
3286 /*******************************************************************
3287 * EnumWindows (USER32.@)
3289 BOOL WINAPI EnumWindows( WNDENUMPROC lpEnumFunc, LPARAM lParam )
3291 HWND *list;
3292 BOOL ret = TRUE;
3293 int i;
3295 USER_CheckNotLock();
3297 /* We have to build a list of all windows first, to avoid */
3298 /* unpleasant side-effects, for instance if the callback */
3299 /* function changes the Z-order of the windows. */
3301 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return TRUE;
3303 /* Now call the callback function for every window */
3305 for (i = 0; list[i]; i++)
3307 /* Make sure that the window still exists */
3308 if (!IsWindow( list[i] )) continue;
3309 if (!(ret = lpEnumFunc( list[i], lParam ))) break;
3311 HeapFree( GetProcessHeap(), 0, list );
3312 return ret;
3316 /**********************************************************************
3317 * EnumThreadWindows (USER32.@)
3319 BOOL WINAPI EnumThreadWindows( DWORD id, WNDENUMPROC func, LPARAM lParam )
3321 HWND *list;
3322 int i;
3323 BOOL ret = TRUE;
3325 USER_CheckNotLock();
3327 if (!(list = list_window_children( 0, GetDesktopWindow(), NULL, id ))) return TRUE;
3329 /* Now call the callback function for every window */
3331 for (i = 0; list[i]; i++)
3332 if (!(ret = func( list[i], lParam ))) break;
3333 HeapFree( GetProcessHeap(), 0, list );
3334 return ret;
3338 /***********************************************************************
3339 * EnumDesktopWindows (USER32.@)
3341 BOOL WINAPI EnumDesktopWindows( HDESK desktop, WNDENUMPROC func, LPARAM lparam )
3343 HWND *list;
3344 int i;
3346 USER_CheckNotLock();
3348 if (!(list = list_window_children( desktop, 0, NULL, 0 ))) return TRUE;
3350 for (i = 0; list[i]; i++)
3351 if (!func( list[i], lparam )) break;
3352 HeapFree( GetProcessHeap(), 0, list );
3353 return TRUE;
3357 #ifdef __i386__
3358 /* Some apps pass a non-stdcall proc to EnumChildWindows,
3359 * so we need a small assembly wrapper to call the proc.
3361 extern LRESULT enum_callback_wrapper( WNDENUMPROC proc, HWND hwnd, LPARAM lparam );
3362 __ASM_GLOBAL_FUNC( enum_callback_wrapper,
3363 "pushl %ebp\n\t"
3364 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
3365 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
3366 "movl %esp,%ebp\n\t"
3367 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
3368 "pushl 16(%ebp)\n\t"
3369 "pushl 12(%ebp)\n\t"
3370 "call *8(%ebp)\n\t"
3371 "leave\n\t"
3372 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
3373 __ASM_CFI(".cfi_same_value %ebp\n\t")
3374 "ret" )
3375 #else
3376 static inline LRESULT enum_callback_wrapper( WNDENUMPROC proc, HWND hwnd, LPARAM lparam )
3378 return proc( hwnd, lparam );
3380 #endif /* __i386__ */
3382 /**********************************************************************
3383 * WIN_EnumChildWindows
3385 * Helper function for EnumChildWindows().
3387 static BOOL WIN_EnumChildWindows( HWND *list, WNDENUMPROC func, LPARAM lParam )
3389 HWND *childList;
3390 BOOL ret = FALSE;
3392 for ( ; *list; list++)
3394 /* Make sure that the window still exists */
3395 if (!IsWindow( *list )) continue;
3396 /* Build children list first */
3397 childList = WIN_ListChildren( *list );
3399 ret = enum_callback_wrapper( func, *list, lParam );
3401 if (childList)
3403 if (ret) ret = WIN_EnumChildWindows( childList, func, lParam );
3404 HeapFree( GetProcessHeap(), 0, childList );
3406 if (!ret) return FALSE;
3408 return TRUE;
3412 /**********************************************************************
3413 * EnumChildWindows (USER32.@)
3415 BOOL WINAPI EnumChildWindows( HWND parent, WNDENUMPROC func, LPARAM lParam )
3417 HWND *list;
3418 BOOL ret;
3420 USER_CheckNotLock();
3422 if (!(list = WIN_ListChildren( parent ))) return FALSE;
3423 ret = WIN_EnumChildWindows( list, func, lParam );
3424 HeapFree( GetProcessHeap(), 0, list );
3425 return ret;
3429 /*******************************************************************
3430 * AnyPopup (USER32.@)
3432 BOOL WINAPI AnyPopup(void)
3434 int i;
3435 BOOL retvalue;
3436 HWND *list = WIN_ListChildren( GetDesktopWindow() );
3438 if (!list) return FALSE;
3439 for (i = 0; list[i]; i++)
3441 if (IsWindowVisible( list[i] ) && GetWindow( list[i], GW_OWNER )) break;
3443 retvalue = (list[i] != 0);
3444 HeapFree( GetProcessHeap(), 0, list );
3445 return retvalue;
3449 /*******************************************************************
3450 * FlashWindow (USER32.@)
3452 BOOL WINAPI FlashWindow( HWND hWnd, BOOL bInvert )
3454 FLASHWINFO finfo;
3456 finfo.cbSize = sizeof(FLASHWINFO);
3457 finfo.dwFlags = bInvert ? FLASHW_ALL : FLASHW_STOP;
3458 finfo.uCount = 1;
3459 finfo.dwTimeout = 0;
3460 finfo.hwnd = hWnd;
3461 return FlashWindowEx( &finfo );
3464 /*******************************************************************
3465 * FlashWindowEx (USER32.@)
3467 BOOL WINAPI FlashWindowEx( PFLASHWINFO pfinfo )
3469 WND *wndPtr;
3471 TRACE( "%p\n", pfinfo );
3473 if (!pfinfo)
3475 SetLastError( ERROR_NOACCESS );
3476 return FALSE;
3479 if (!pfinfo->hwnd || pfinfo->cbSize != sizeof(FLASHWINFO) || !IsWindow( pfinfo->hwnd ))
3481 SetLastError( ERROR_INVALID_PARAMETER );
3482 return FALSE;
3484 FIXME( "%p - semi-stub\n", pfinfo );
3486 if (IsIconic( pfinfo->hwnd ))
3488 RedrawWindow( pfinfo->hwnd, 0, 0, RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW | RDW_FRAME );
3490 wndPtr = WIN_GetPtr( pfinfo->hwnd );
3491 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
3492 if (pfinfo->dwFlags && !(wndPtr->flags & WIN_NCACTIVATED))
3494 wndPtr->flags |= WIN_NCACTIVATED;
3496 else
3498 wndPtr->flags &= ~WIN_NCACTIVATED;
3500 WIN_ReleasePtr( wndPtr );
3501 USER_Driver->pFlashWindowEx( pfinfo );
3502 return TRUE;
3504 else
3506 WPARAM wparam;
3507 HWND hwnd = pfinfo->hwnd;
3509 wndPtr = WIN_GetPtr( hwnd );
3510 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
3511 hwnd = wndPtr->obj.handle; /* make it a full handle */
3513 if (pfinfo->dwFlags) wparam = !(wndPtr->flags & WIN_NCACTIVATED);
3514 else wparam = (hwnd == GetForegroundWindow());
3516 WIN_ReleasePtr( wndPtr );
3517 SendMessageW( hwnd, WM_NCACTIVATE, wparam, 0 );
3518 USER_Driver->pFlashWindowEx( pfinfo );
3519 return wparam;
3523 /*******************************************************************
3524 * GetWindowContextHelpId (USER32.@)
3526 DWORD WINAPI GetWindowContextHelpId( HWND hwnd )
3528 DWORD retval;
3529 WND *wnd = WIN_GetPtr( hwnd );
3530 if (!wnd || wnd == WND_DESKTOP) return 0;
3531 if (wnd == WND_OTHER_PROCESS)
3533 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
3534 return 0;
3536 retval = wnd->helpContext;
3537 WIN_ReleasePtr( wnd );
3538 return retval;
3542 /*******************************************************************
3543 * SetWindowContextHelpId (USER32.@)
3545 BOOL WINAPI SetWindowContextHelpId( HWND hwnd, DWORD id )
3547 WND *wnd = WIN_GetPtr( hwnd );
3548 if (!wnd || wnd == WND_DESKTOP) return FALSE;
3549 if (wnd == WND_OTHER_PROCESS)
3551 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
3552 return FALSE;
3554 wnd->helpContext = id;
3555 WIN_ReleasePtr( wnd );
3556 return TRUE;
3560 /*******************************************************************
3561 * DragDetect (USER32.@)
3563 BOOL WINAPI DragDetect( HWND hWnd, POINT pt )
3565 MSG msg;
3566 RECT rect;
3567 WORD wDragWidth = GetSystemMetrics(SM_CXDRAG);
3568 WORD wDragHeight= GetSystemMetrics(SM_CYDRAG);
3570 rect.left = pt.x - wDragWidth;
3571 rect.right = pt.x + wDragWidth;
3573 rect.top = pt.y - wDragHeight;
3574 rect.bottom = pt.y + wDragHeight;
3576 SetCapture(hWnd);
3578 while(1)
3580 while (PeekMessageW( &msg, 0, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE ))
3582 if( msg.message == WM_LBUTTONUP )
3584 ReleaseCapture();
3585 return FALSE;
3587 if( msg.message == WM_MOUSEMOVE )
3589 POINT tmp;
3590 tmp.x = (short)LOWORD(msg.lParam);
3591 tmp.y = (short)HIWORD(msg.lParam);
3592 if( !PtInRect( &rect, tmp ))
3594 ReleaseCapture();
3595 return TRUE;
3599 WaitMessage();
3601 return FALSE;
3604 /******************************************************************************
3605 * GetWindowModuleFileNameA (USER32.@)
3607 UINT WINAPI GetWindowModuleFileNameA( HWND hwnd, LPSTR module, UINT size )
3609 WND *win;
3610 HINSTANCE hinst;
3612 TRACE( "%p, %p, %u\n", hwnd, module, size );
3614 win = WIN_GetPtr( hwnd );
3615 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP)
3617 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3618 return 0;
3620 hinst = win->hInstance;
3621 WIN_ReleasePtr( win );
3623 return GetModuleFileNameA( hinst, module, size );
3626 /******************************************************************************
3627 * GetWindowModuleFileNameW (USER32.@)
3629 UINT WINAPI GetWindowModuleFileNameW( HWND hwnd, LPWSTR module, UINT size )
3631 WND *win;
3632 HINSTANCE hinst;
3634 TRACE( "%p, %p, %u\n", hwnd, module, size );
3636 win = WIN_GetPtr( hwnd );
3637 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP)
3639 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3640 return 0;
3642 hinst = win->hInstance;
3643 WIN_ReleasePtr( win );
3645 return GetModuleFileNameW( hinst, module, size );
3648 /******************************************************************************
3649 * GetWindowInfo (USER32.@)
3651 * Note: tests show that Windows doesn't check cbSize of the structure.
3653 BOOL WINAPI DECLSPEC_HOTPATCH GetWindowInfo( HWND hwnd, PWINDOWINFO pwi)
3655 if (!pwi) return FALSE;
3656 if (!WIN_GetRectangles( hwnd, COORDS_SCREEN, &pwi->rcWindow, &pwi->rcClient )) return FALSE;
3658 pwi->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
3659 pwi->dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
3660 pwi->dwWindowStatus = ((GetActiveWindow() == hwnd) ? WS_ACTIVECAPTION : 0);
3662 pwi->cxWindowBorders = pwi->rcClient.left - pwi->rcWindow.left;
3663 pwi->cyWindowBorders = pwi->rcWindow.bottom - pwi->rcClient.bottom;
3665 pwi->atomWindowType = GetClassLongW( hwnd, GCW_ATOM );
3666 pwi->wCreatorVersion = 0x0400;
3668 return TRUE;
3671 /******************************************************************************
3672 * SwitchDesktop (USER32.@)
3674 * NOTES: Sets the current input or interactive desktop.
3676 BOOL WINAPI SwitchDesktop( HDESK hDesktop)
3678 FIXME("(hwnd %p) stub!\n", hDesktop);
3679 return TRUE;
3683 /***********************************************************************
3684 * __wine_set_pixel_format
3686 BOOL CDECL __wine_set_pixel_format( HWND hwnd, int format )
3688 WND *win = WIN_GetPtr( hwnd );
3690 if (!win || win == WND_DESKTOP || win == WND_OTHER_PROCESS)
3692 WARN( "setting format %d on win %p not supported\n", format, hwnd );
3693 return FALSE;
3695 win->pixel_format = format;
3696 WIN_ReleasePtr( win );
3698 update_window_state( hwnd );
3699 return TRUE;
3703 /*****************************************************************************
3704 * SetLayeredWindowAttributes (USER32.@)
3706 BOOL WINAPI SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
3708 BOOL ret;
3710 TRACE("(%p,%08x,%d,%x)\n", hwnd, key, alpha, flags);
3712 SERVER_START_REQ( set_window_layered_info )
3714 req->handle = wine_server_user_handle( hwnd );
3715 req->color_key = key;
3716 req->alpha = alpha;
3717 req->flags = flags;
3718 ret = !wine_server_call_err( req );
3720 SERVER_END_REQ;
3722 if (ret) USER_Driver->pSetLayeredWindowAttributes( hwnd, key, alpha, flags );
3724 return ret;
3728 /*****************************************************************************
3729 * GetLayeredWindowAttributes (USER32.@)
3731 BOOL WINAPI GetLayeredWindowAttributes( HWND hwnd, COLORREF *key, BYTE *alpha, DWORD *flags )
3733 BOOL ret;
3735 SERVER_START_REQ( get_window_layered_info )
3737 req->handle = wine_server_user_handle( hwnd );
3738 if ((ret = !wine_server_call_err( req )))
3740 if (key) *key = reply->color_key;
3741 if (alpha) *alpha = reply->alpha;
3742 if (flags) *flags = reply->flags;
3745 SERVER_END_REQ;
3747 return ret;
3751 /*****************************************************************************
3752 * UpdateLayeredWindowIndirect (USER32.@)
3754 BOOL WINAPI UpdateLayeredWindowIndirect( HWND hwnd, const UPDATELAYEREDWINDOWINFO *info )
3756 DWORD flags = SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW;
3757 RECT window_rect, client_rect;
3758 SIZE offset;
3760 if (!info ||
3761 info->cbSize != sizeof(*info) ||
3762 info->dwFlags & ~(ULW_COLORKEY | ULW_ALPHA | ULW_OPAQUE | ULW_EX_NORESIZE) ||
3763 !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED) ||
3764 GetLayeredWindowAttributes( hwnd, NULL, NULL, NULL ))
3766 SetLastError( ERROR_INVALID_PARAMETER );
3767 return FALSE;
3770 WIN_GetRectangles( hwnd, COORDS_PARENT, &window_rect, &client_rect );
3772 if (info->pptDst)
3774 offset.cx = info->pptDst->x - window_rect.left;
3775 offset.cy = info->pptDst->y - window_rect.top;
3776 OffsetRect( &client_rect, offset.cx, offset.cy );
3777 OffsetRect( &window_rect, offset.cx, offset.cy );
3778 flags &= ~SWP_NOMOVE;
3780 if (info->psize)
3782 offset.cx = info->psize->cx - (window_rect.right - window_rect.left);
3783 offset.cy = info->psize->cy - (window_rect.bottom - window_rect.top);
3784 if (info->psize->cx <= 0 || info->psize->cy <= 0)
3786 SetLastError( ERROR_INVALID_PARAMETER );
3787 return FALSE;
3789 if ((info->dwFlags & ULW_EX_NORESIZE) && (offset.cx || offset.cy))
3791 SetLastError( ERROR_INCORRECT_SIZE );
3792 return FALSE;
3794 client_rect.right += offset.cx;
3795 client_rect.bottom += offset.cy;
3796 window_rect.right += offset.cx;
3797 window_rect.bottom += offset.cy;
3798 flags &= ~SWP_NOSIZE;
3801 TRACE( "window %p win %s client %s\n", hwnd,
3802 wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(&client_rect) );
3804 if (!USER_Driver->pUpdateLayeredWindow( hwnd, info, &window_rect )) return FALSE;
3806 set_window_pos( hwnd, 0, flags, &window_rect, &client_rect, NULL );
3807 return TRUE;
3811 /*****************************************************************************
3812 * UpdateLayeredWindow (USER32.@)
3814 BOOL WINAPI UpdateLayeredWindow( HWND hwnd, HDC hdcDst, POINT *pptDst, SIZE *psize,
3815 HDC hdcSrc, POINT *pptSrc, COLORREF crKey, BLENDFUNCTION *pblend,
3816 DWORD flags)
3818 UPDATELAYEREDWINDOWINFO info;
3820 if (flags & ULW_EX_NORESIZE) /* only valid for UpdateLayeredWindowIndirect */
3822 SetLastError( ERROR_INVALID_PARAMETER );
3823 return FALSE;
3825 info.cbSize = sizeof(info);
3826 info.hdcDst = hdcDst;
3827 info.pptDst = pptDst;
3828 info.psize = psize;
3829 info.hdcSrc = hdcSrc;
3830 info.pptSrc = pptSrc;
3831 info.crKey = crKey;
3832 info.pblend = pblend;
3833 info.dwFlags = flags;
3834 info.prcDirty = NULL;
3835 return UpdateLayeredWindowIndirect( hwnd, &info );
3839 /******************************************************************************
3840 * GetProcessDefaultLayout [USER32.@]
3842 * Gets the default layout for parentless windows.
3844 BOOL WINAPI GetProcessDefaultLayout( DWORD *layout )
3846 if (!layout)
3848 SetLastError( ERROR_NOACCESS );
3849 return FALSE;
3851 if (process_layout == ~0u)
3853 static const WCHAR translationW[] = { '\\','V','a','r','F','i','l','e','I','n','f','o',
3854 '\\','T','r','a','n','s','l','a','t','i','o','n', 0 };
3855 static const WCHAR filedescW[] = { '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
3856 '\\','%','0','4','x','%','0','4','x',
3857 '\\','F','i','l','e','D','e','s','c','r','i','p','t','i','o','n',0 };
3858 WCHAR *str, buffer[MAX_PATH];
3859 DWORD i, len, version_layout = 0;
3860 DWORD user_lang = GetUserDefaultLangID();
3861 DWORD *languages;
3862 void *data = NULL;
3864 GetModuleFileNameW( 0, buffer, MAX_PATH );
3865 if (!(len = GetFileVersionInfoSizeW( buffer, NULL ))) goto done;
3866 if (!(data = HeapAlloc( GetProcessHeap(), 0, len ))) goto done;
3867 if (!GetFileVersionInfoW( buffer, 0, len, data )) goto done;
3868 if (!VerQueryValueW( data, translationW, (void **)&languages, &len ) || !len) goto done;
3870 len /= sizeof(DWORD);
3871 for (i = 0; i < len; i++) if (LOWORD(languages[i]) == user_lang) break;
3872 if (i == len) /* try neutral language */
3873 for (i = 0; i < len; i++)
3874 if (LOWORD(languages[i]) == MAKELANGID( PRIMARYLANGID(user_lang), SUBLANG_NEUTRAL )) break;
3875 if (i == len) i = 0; /* default to the first one */
3877 sprintfW( buffer, filedescW, LOWORD(languages[i]), HIWORD(languages[i]) );
3878 if (!VerQueryValueW( data, buffer, (void **)&str, &len )) goto done;
3879 TRACE( "found description %s\n", debugstr_w( str ));
3880 if (str[0] == 0x200e && str[1] == 0x200e) version_layout = LAYOUT_RTL;
3882 done:
3883 HeapFree( GetProcessHeap(), 0, data );
3884 process_layout = version_layout;
3886 *layout = process_layout;
3887 return TRUE;
3891 /******************************************************************************
3892 * SetProcessDefaultLayout [USER32.@]
3894 * Sets the default layout for parentless windows.
3896 BOOL WINAPI SetProcessDefaultLayout( DWORD layout )
3898 process_layout = layout;
3899 return TRUE;
3903 /* 64bit versions */
3905 #ifdef GetWindowLongPtrW
3906 #undef GetWindowLongPtrW
3907 #endif
3909 #ifdef GetWindowLongPtrA
3910 #undef GetWindowLongPtrA
3911 #endif
3913 #ifdef SetWindowLongPtrW
3914 #undef SetWindowLongPtrW
3915 #endif
3917 #ifdef SetWindowLongPtrA
3918 #undef SetWindowLongPtrA
3919 #endif
3921 /*****************************************************************************
3922 * GetWindowLongPtrW (USER32.@)
3924 LONG_PTR WINAPI GetWindowLongPtrW( HWND hwnd, INT offset )
3926 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG_PTR), TRUE );
3929 /*****************************************************************************
3930 * GetWindowLongPtrA (USER32.@)
3932 LONG_PTR WINAPI GetWindowLongPtrA( HWND hwnd, INT offset )
3934 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG_PTR), FALSE );
3937 /*****************************************************************************
3938 * SetWindowLongPtrW (USER32.@)
3940 LONG_PTR WINAPI SetWindowLongPtrW( HWND hwnd, INT offset, LONG_PTR newval )
3942 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG_PTR), newval, TRUE );
3945 /*****************************************************************************
3946 * SetWindowLongPtrA (USER32.@)
3948 LONG_PTR WINAPI SetWindowLongPtrA( HWND hwnd, INT offset, LONG_PTR newval )
3950 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG_PTR), newval, FALSE );
3953 /*****************************************************************************
3954 * RegisterTouchWindow (USER32.@)
3956 BOOL WINAPI RegisterTouchWindow(HWND hwnd, ULONG flags)
3958 FIXME("(%p %08x): stub\n", hwnd, flags);
3959 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
3960 return FALSE;