combase: Add stub for CleanupTlsOleState.
[wine.git] / dlls / user32 / win.c
blob7bb0088af50b17bdc28b04ff6f4613600d3e752c
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 wine_server_call( req );
291 InterlockedCompareExchangePointer( &user_handles[index], NULL, ptr );
293 SERVER_END_REQ;
294 USER_Unlock();
295 HeapFree( GetProcessHeap(), 0, ptr );
300 /*******************************************************************
301 * list_window_children
303 * Build an array of the children of a given window. The array must be
304 * freed with HeapFree. Returns NULL when no windows are found.
306 static HWND *list_window_children( HDESK desktop, HWND hwnd, LPCWSTR class, DWORD tid )
308 HWND *list;
309 int i, size = 128;
310 ATOM atom = get_int_atom_value( class );
312 /* empty class is not the same as NULL class */
313 if (!atom && class && !class[0]) return NULL;
315 for (;;)
317 int count = 0;
319 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
321 SERVER_START_REQ( get_window_children )
323 req->desktop = wine_server_obj_handle( desktop );
324 req->parent = wine_server_user_handle( hwnd );
325 req->tid = tid;
326 req->atom = atom;
327 if (!atom && class) wine_server_add_data( req, class, strlenW(class)*sizeof(WCHAR) );
328 wine_server_set_reply( req, list, (size-1) * sizeof(user_handle_t) );
329 if (!wine_server_call( req )) count = reply->count;
331 SERVER_END_REQ;
332 if (count && count < size)
334 /* start from the end since HWND is potentially larger than user_handle_t */
335 for (i = count - 1; i >= 0; i--)
336 list[i] = wine_server_ptr_handle( ((user_handle_t *)list)[i] );
337 list[count] = 0;
338 return list;
340 HeapFree( GetProcessHeap(), 0, list );
341 if (!count) break;
342 size = count + 1; /* restart with a large enough buffer */
344 return NULL;
348 /*******************************************************************
349 * list_window_parents
351 * Build an array of all parents of a given window, starting with
352 * the immediate parent. The array must be freed with HeapFree.
354 static HWND *list_window_parents( HWND hwnd )
356 WND *win;
357 HWND current, *list;
358 int i, pos = 0, size = 16, count;
360 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) return NULL;
362 current = hwnd;
363 for (;;)
365 if (!(win = WIN_GetPtr( current ))) goto empty;
366 if (win == WND_OTHER_PROCESS) break; /* need to do it the hard way */
367 if (win == WND_DESKTOP)
369 if (!pos) goto empty;
370 list[pos] = 0;
371 return list;
373 list[pos] = current = win->parent;
374 WIN_ReleasePtr( win );
375 if (!current) return list;
376 if (++pos == size - 1)
378 /* need to grow the list */
379 HWND *new_list = HeapReAlloc( GetProcessHeap(), 0, list, (size+16) * sizeof(HWND) );
380 if (!new_list) goto empty;
381 list = new_list;
382 size += 16;
386 /* at least one parent belongs to another process, have to query the server */
388 for (;;)
390 count = 0;
391 SERVER_START_REQ( get_window_parents )
393 req->handle = wine_server_user_handle( hwnd );
394 wine_server_set_reply( req, list, (size-1) * sizeof(user_handle_t) );
395 if (!wine_server_call( req )) count = reply->count;
397 SERVER_END_REQ;
398 if (!count) goto empty;
399 if (size > count)
401 /* start from the end since HWND is potentially larger than user_handle_t */
402 for (i = count - 1; i >= 0; i--)
403 list[i] = wine_server_ptr_handle( ((user_handle_t *)list)[i] );
404 list[count] = 0;
405 return list;
407 HeapFree( GetProcessHeap(), 0, list );
408 size = count + 1;
409 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) return NULL;
412 empty:
413 HeapFree( GetProcessHeap(), 0, list );
414 return NULL;
418 /*******************************************************************
419 * send_parent_notify
421 static void send_parent_notify( HWND hwnd, UINT msg )
423 if ((GetWindowLongW( hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD &&
424 !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY))
426 HWND parent = GetParent(hwnd);
427 if (parent && parent != GetDesktopWindow())
428 SendMessageW( parent, WM_PARENTNOTIFY,
429 MAKEWPARAM( msg, GetWindowLongPtrW( hwnd, GWLP_ID )), (LPARAM)hwnd );
434 /*******************************************************************
435 * update_window_state
437 * Trigger an update of the window's driver state and surface.
439 static void update_window_state( HWND hwnd )
441 RECT window_rect, client_rect, valid_rects[2];
443 WIN_GetRectangles( hwnd, COORDS_PARENT, &window_rect, &client_rect );
444 valid_rects[0] = valid_rects[1] = client_rect;
445 set_window_pos( hwnd, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE |
446 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW,
447 &window_rect, &client_rect, valid_rects );
451 /*******************************************************************
452 * get_server_window_text
454 * Retrieve the window text from the server.
456 static void get_server_window_text( HWND hwnd, LPWSTR text, INT count )
458 size_t len = 0;
460 SERVER_START_REQ( get_window_text )
462 req->handle = wine_server_user_handle( hwnd );
463 wine_server_set_reply( req, text, (count - 1) * sizeof(WCHAR) );
464 if (!wine_server_call_err( req )) len = wine_server_reply_size(reply);
466 SERVER_END_REQ;
467 text[len / sizeof(WCHAR)] = 0;
471 /*******************************************************************
472 * get_hwnd_message_parent
474 * Return the parent for HWND_MESSAGE windows.
476 HWND get_hwnd_message_parent(void)
478 struct user_thread_info *thread_info = get_user_thread_info();
480 if (!thread_info->msg_window) GetDesktopWindow(); /* trigger creation */
481 return thread_info->msg_window;
485 /*******************************************************************
486 * is_desktop_window
488 * Check if window is the desktop or the HWND_MESSAGE top parent.
490 BOOL is_desktop_window( HWND hwnd )
492 struct user_thread_info *thread_info = get_user_thread_info();
494 if (!hwnd) return FALSE;
495 if (hwnd == thread_info->top_window) return TRUE;
496 if (hwnd == thread_info->msg_window) return TRUE;
498 if (!HIWORD(hwnd) || HIWORD(hwnd) == 0xffff)
500 if (LOWORD(thread_info->top_window) == LOWORD(hwnd)) return TRUE;
501 if (LOWORD(thread_info->msg_window) == LOWORD(hwnd)) return TRUE;
503 return FALSE;
507 /*******************************************************************
508 * Dummy window surface for windows that shouldn't get painted.
511 static void dummy_surface_lock( struct window_surface *window_surface )
513 /* nothing to do */
516 static void dummy_surface_unlock( struct window_surface *window_surface )
518 /* nothing to do */
521 static void *dummy_surface_get_bitmap_info( struct window_surface *window_surface, BITMAPINFO *info )
523 static DWORD dummy_data;
525 info->bmiHeader.biSize = sizeof( info->bmiHeader );
526 info->bmiHeader.biWidth = dummy_surface.rect.right;
527 info->bmiHeader.biHeight = dummy_surface.rect.bottom;
528 info->bmiHeader.biPlanes = 1;
529 info->bmiHeader.biBitCount = 32;
530 info->bmiHeader.biCompression = BI_RGB;
531 info->bmiHeader.biSizeImage = 0;
532 info->bmiHeader.biXPelsPerMeter = 0;
533 info->bmiHeader.biYPelsPerMeter = 0;
534 info->bmiHeader.biClrUsed = 0;
535 info->bmiHeader.biClrImportant = 0;
536 return &dummy_data;
539 static RECT *dummy_surface_get_bounds( struct window_surface *window_surface )
541 static RECT dummy_bounds;
542 return &dummy_bounds;
545 static void dummy_surface_set_region( struct window_surface *window_surface, HRGN region )
547 /* nothing to do */
550 static void dummy_surface_flush( struct window_surface *window_surface )
552 /* nothing to do */
555 static void dummy_surface_destroy( struct window_surface *window_surface )
557 /* nothing to do */
560 static const struct window_surface_funcs dummy_surface_funcs =
562 dummy_surface_lock,
563 dummy_surface_unlock,
564 dummy_surface_get_bitmap_info,
565 dummy_surface_get_bounds,
566 dummy_surface_set_region,
567 dummy_surface_flush,
568 dummy_surface_destroy
571 struct window_surface dummy_surface = { &dummy_surface_funcs, { NULL, NULL }, 1, { 0, 0, 1, 1 } };
574 /*******************************************************************
575 * register_window_surface
577 * Register a window surface in the global list, possibly replacing another one.
579 void register_window_surface( struct window_surface *old, struct window_surface *new )
581 if (old == new) return;
582 EnterCriticalSection( &surfaces_section );
583 if (old && old != &dummy_surface) list_remove( &old->entry );
584 if (new && new != &dummy_surface) list_add_tail( &window_surfaces, &new->entry );
585 LeaveCriticalSection( &surfaces_section );
589 /*******************************************************************
590 * flush_window_surfaces
592 * Flush pending output from all window surfaces.
594 void flush_window_surfaces( BOOL idle )
596 static DWORD last_idle;
597 DWORD now;
598 struct window_surface *surface;
600 EnterCriticalSection( &surfaces_section );
601 now = GetTickCount();
602 if (idle) last_idle = now;
603 /* if not idle, we only flush if there's evidence that the app never goes idle */
604 else if ((int)(now - last_idle) < 50) goto done;
606 LIST_FOR_EACH_ENTRY( surface, &window_surfaces, struct window_surface, entry )
607 surface->funcs->flush( surface );
608 done:
609 LeaveCriticalSection( &surfaces_section );
613 /***********************************************************************
614 * WIN_GetPtr
616 * Return a pointer to the WND structure if local to the process,
617 * or WND_OTHER_PROCESS if handle may be valid in other process.
618 * If ret value is a valid pointer, it must be released with WIN_ReleasePtr.
620 WND *WIN_GetPtr( HWND hwnd )
622 WND *ptr;
624 if ((ptr = get_user_handle_ptr( hwnd, USER_WINDOW )) == WND_OTHER_PROCESS)
626 if (is_desktop_window( hwnd )) ptr = WND_DESKTOP;
628 return ptr;
632 /***********************************************************************
633 * WIN_IsCurrentProcess
635 * Check whether a given window belongs to the current process (and return the full handle).
637 HWND WIN_IsCurrentProcess( HWND hwnd )
639 WND *ptr;
640 HWND ret;
642 if (!(ptr = WIN_GetPtr( hwnd )) || ptr == WND_OTHER_PROCESS || ptr == WND_DESKTOP) return 0;
643 ret = ptr->obj.handle;
644 WIN_ReleasePtr( ptr );
645 return ret;
649 /***********************************************************************
650 * WIN_IsCurrentThread
652 * Check whether a given window belongs to the current thread (and return the full handle).
654 HWND WIN_IsCurrentThread( HWND hwnd )
656 WND *ptr;
657 HWND ret = 0;
659 if (!(ptr = WIN_GetPtr( hwnd )) || ptr == WND_OTHER_PROCESS || ptr == WND_DESKTOP) return 0;
660 if (ptr->tid == GetCurrentThreadId()) ret = ptr->obj.handle;
661 WIN_ReleasePtr( ptr );
662 return ret;
666 /***********************************************************************
667 * win_set_flags
669 * Set the flags of a window and return the previous value.
671 UINT win_set_flags( HWND hwnd, UINT set_mask, UINT clear_mask )
673 UINT ret;
674 WND *ptr = WIN_GetPtr( hwnd );
676 if (!ptr || ptr == WND_OTHER_PROCESS || ptr == WND_DESKTOP) return 0;
677 ret = ptr->flags;
678 ptr->flags = (ret & ~clear_mask) | set_mask;
679 WIN_ReleasePtr( ptr );
680 return ret;
684 /***********************************************************************
685 * WIN_GetFullHandle
687 * Convert a possibly truncated window handle to a full 32-bit handle.
689 HWND WIN_GetFullHandle( HWND hwnd )
691 WND *ptr;
693 if (!hwnd || (ULONG_PTR)hwnd >> 16) return hwnd;
694 if (LOWORD(hwnd) <= 1 || LOWORD(hwnd) == 0xffff) return hwnd;
695 /* do sign extension for -2 and -3 */
696 if (LOWORD(hwnd) >= (WORD)-3) return (HWND)(LONG_PTR)(INT16)LOWORD(hwnd);
698 if (!(ptr = WIN_GetPtr( hwnd ))) return hwnd;
700 if (ptr == WND_DESKTOP)
702 if (LOWORD(hwnd) == LOWORD(GetDesktopWindow())) return GetDesktopWindow();
703 else return get_hwnd_message_parent();
706 if (ptr != WND_OTHER_PROCESS)
708 hwnd = ptr->obj.handle;
709 WIN_ReleasePtr( ptr );
711 else /* may belong to another process */
713 SERVER_START_REQ( get_window_info )
715 req->handle = wine_server_user_handle( hwnd );
716 if (!wine_server_call_err( req )) hwnd = wine_server_ptr_handle( reply->full_handle );
718 SERVER_END_REQ;
720 return hwnd;
724 /***********************************************************************
725 * WIN_SetOwner
727 * Change the owner of a window.
729 HWND WIN_SetOwner( HWND hwnd, HWND owner )
731 WND *win = WIN_GetPtr( hwnd );
732 HWND ret = 0;
734 if (!win || win == WND_DESKTOP) return 0;
735 if (win == WND_OTHER_PROCESS)
737 if (IsWindow(hwnd)) ERR( "cannot set owner %p on other process window %p\n", owner, hwnd );
738 return 0;
740 SERVER_START_REQ( set_window_owner )
742 req->handle = wine_server_user_handle( hwnd );
743 req->owner = wine_server_user_handle( owner );
744 if (!wine_server_call( req ))
746 win->owner = wine_server_ptr_handle( reply->full_owner );
747 ret = wine_server_ptr_handle( reply->prev_owner );
750 SERVER_END_REQ;
751 WIN_ReleasePtr( win );
752 return ret;
756 /***********************************************************************
757 * WIN_SetStyle
759 * Change the style of a window.
761 ULONG WIN_SetStyle( HWND hwnd, ULONG set_bits, ULONG clear_bits )
763 BOOL ok, made_visible = FALSE;
764 STYLESTRUCT style;
765 WND *win = WIN_GetPtr( hwnd );
767 if (!win || win == WND_DESKTOP) return 0;
768 if (win == WND_OTHER_PROCESS)
770 if (IsWindow(hwnd))
771 return SendMessageW(hwnd, WM_WINE_SETSTYLE, set_bits, clear_bits);
772 return 0;
774 style.styleOld = win->dwStyle;
775 style.styleNew = (win->dwStyle | set_bits) & ~clear_bits;
776 if (style.styleNew == style.styleOld)
778 WIN_ReleasePtr( win );
779 return style.styleNew;
781 SERVER_START_REQ( set_window_info )
783 req->handle = wine_server_user_handle( hwnd );
784 req->flags = SET_WIN_STYLE;
785 req->style = style.styleNew;
786 req->extra_offset = -1;
787 if ((ok = !wine_server_call( req )))
789 style.styleOld = reply->old_style;
790 win->dwStyle = style.styleNew;
793 SERVER_END_REQ;
795 if (ok && ((style.styleOld ^ style.styleNew) & WS_VISIBLE))
797 made_visible = (style.styleNew & WS_VISIBLE) != 0;
798 invalidate_dce( win, NULL );
800 WIN_ReleasePtr( win );
802 if (!ok) return 0;
804 USER_Driver->pSetWindowStyle( hwnd, GWL_STYLE, &style );
805 if (made_visible) update_window_state( hwnd );
807 return style.styleOld;
811 /***********************************************************************
812 * WIN_GetRectangles
814 * Get the window and client rectangles.
816 BOOL WIN_GetRectangles( HWND hwnd, enum coords_relative relative, RECT *rectWindow, RECT *rectClient )
818 WND *win = WIN_GetPtr( hwnd );
819 BOOL ret = TRUE;
821 if (!win)
823 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
824 return FALSE;
826 if (win == WND_DESKTOP)
828 RECT rect;
829 rect.left = rect.top = 0;
830 if (hwnd == get_hwnd_message_parent())
832 rect.right = 100;
833 rect.bottom = 100;
835 else
837 rect.right = GetSystemMetrics(SM_CXSCREEN);
838 rect.bottom = GetSystemMetrics(SM_CYSCREEN);
840 if (rectWindow) *rectWindow = rect;
841 if (rectClient) *rectClient = rect;
842 return TRUE;
844 if (win != WND_OTHER_PROCESS)
846 RECT window_rect = win->rectWindow, client_rect = win->rectClient;
848 switch (relative)
850 case COORDS_CLIENT:
851 OffsetRect( &window_rect, -win->rectClient.left, -win->rectClient.top );
852 OffsetRect( &client_rect, -win->rectClient.left, -win->rectClient.top );
853 if (win->dwExStyle & WS_EX_LAYOUTRTL)
854 mirror_rect( &win->rectClient, &window_rect );
855 break;
856 case COORDS_WINDOW:
857 OffsetRect( &window_rect, -win->rectWindow.left, -win->rectWindow.top );
858 OffsetRect( &client_rect, -win->rectWindow.left, -win->rectWindow.top );
859 if (win->dwExStyle & WS_EX_LAYOUTRTL)
860 mirror_rect( &win->rectWindow, &client_rect );
861 break;
862 case COORDS_PARENT:
863 if (win->parent)
865 WND *parent = WIN_GetPtr( win->parent );
866 if (parent == WND_DESKTOP) break;
867 if (!parent || parent == WND_OTHER_PROCESS)
869 WIN_ReleasePtr( win );
870 goto other_process;
872 if (parent->flags & WIN_CHILDREN_MOVED)
874 WIN_ReleasePtr( parent );
875 WIN_ReleasePtr( win );
876 goto other_process;
878 if (parent->dwExStyle & WS_EX_LAYOUTRTL)
880 mirror_rect( &parent->rectClient, &window_rect );
881 mirror_rect( &parent->rectClient, &client_rect );
883 WIN_ReleasePtr( parent );
885 break;
886 case COORDS_SCREEN:
887 while (win->parent)
889 WND *parent = WIN_GetPtr( win->parent );
890 if (parent == WND_DESKTOP) break;
891 if (!parent || parent == WND_OTHER_PROCESS)
893 WIN_ReleasePtr( win );
894 goto other_process;
896 WIN_ReleasePtr( win );
897 if (parent->flags & WIN_CHILDREN_MOVED)
899 WIN_ReleasePtr( parent );
900 goto other_process;
902 win = parent;
903 if (win->parent)
905 OffsetRect( &window_rect, win->rectClient.left, win->rectClient.top );
906 OffsetRect( &client_rect, win->rectClient.left, win->rectClient.top );
909 break;
911 if (rectWindow) *rectWindow = window_rect;
912 if (rectClient) *rectClient = client_rect;
913 WIN_ReleasePtr( win );
914 return TRUE;
917 other_process:
918 SERVER_START_REQ( get_window_rectangles )
920 req->handle = wine_server_user_handle( hwnd );
921 req->relative = relative;
922 if ((ret = !wine_server_call_err( req )))
924 if (rectWindow)
926 rectWindow->left = reply->window.left;
927 rectWindow->top = reply->window.top;
928 rectWindow->right = reply->window.right;
929 rectWindow->bottom = reply->window.bottom;
931 if (rectClient)
933 rectClient->left = reply->client.left;
934 rectClient->top = reply->client.top;
935 rectClient->right = reply->client.right;
936 rectClient->bottom = reply->client.bottom;
940 SERVER_END_REQ;
941 return ret;
945 /***********************************************************************
946 * WIN_DestroyWindow
948 * Destroy storage associated to a window. "Internals" p.358
950 LRESULT WIN_DestroyWindow( HWND hwnd )
952 WND *wndPtr;
953 HWND *list;
954 HMENU menu = 0, sys_menu;
955 HWND icon_title;
956 struct window_surface *surface;
958 TRACE("%p\n", hwnd );
960 /* destroy default IME window */
961 if (win_set_flags( hwnd, 0, WIN_HAS_IME_WIN ) & WIN_HAS_IME_WIN)
963 TRACE("unregister IME window for %p\n", hwnd);
964 imm_unregister_window( hwnd );
967 /* free child windows */
968 if ((list = WIN_ListChildren( hwnd )))
970 int i;
971 for (i = 0; list[i]; i++)
973 if (WIN_IsCurrentThread( list[i] )) WIN_DestroyWindow( list[i] );
974 else SendNotifyMessageW( list[i], WM_WINE_DESTROYWINDOW, 0, 0 );
976 HeapFree( GetProcessHeap(), 0, list );
979 /* Unlink now so we won't bother with the children later on */
980 SERVER_START_REQ( set_parent )
982 req->handle = wine_server_user_handle( hwnd );
983 req->parent = 0;
984 wine_server_call( req );
986 SERVER_END_REQ;
989 * Send the WM_NCDESTROY to the window being destroyed.
991 SendMessageW( hwnd, WM_NCDESTROY, 0, 0 );
993 /* FIXME: do we need to fake QS_MOUSEMOVE wakebit? */
995 /* free resources associated with the window */
997 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
998 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
999 menu = (HMENU)wndPtr->wIDmenu;
1000 sys_menu = wndPtr->hSysMenu;
1001 free_dce( wndPtr->dce, hwnd );
1002 wndPtr->dce = NULL;
1003 icon_title = wndPtr->icon_title;
1004 HeapFree( GetProcessHeap(), 0, wndPtr->text );
1005 wndPtr->text = NULL;
1006 HeapFree( GetProcessHeap(), 0, wndPtr->pScroll );
1007 wndPtr->pScroll = NULL;
1008 DestroyIcon( wndPtr->hIconSmall2 );
1009 surface = wndPtr->surface;
1010 wndPtr->surface = NULL;
1011 WIN_ReleasePtr( wndPtr );
1013 if (icon_title) DestroyWindow( icon_title );
1014 if (menu) DestroyMenu( menu );
1015 if (sys_menu) DestroyMenu( sys_menu );
1016 if (surface)
1018 register_window_surface( surface, NULL );
1019 window_surface_release( surface );
1022 USER_Driver->pDestroyWindow( hwnd );
1024 free_window_handle( hwnd );
1025 return 0;
1029 /***********************************************************************
1030 * next_thread_window
1032 static WND *next_thread_window( HWND *hwnd )
1034 struct user_object *ptr;
1035 WND *win;
1036 WORD index = *hwnd ? USER_HANDLE_TO_INDEX( *hwnd ) + 1 : 0;
1038 USER_Lock();
1039 while (index < NB_USER_HANDLES)
1041 if (!(ptr = user_handles[index++])) continue;
1042 if (ptr->type != USER_WINDOW) continue;
1043 win = (WND *)ptr;
1044 if (win->tid != GetCurrentThreadId()) continue;
1045 *hwnd = ptr->handle;
1046 return win;
1048 USER_Unlock();
1049 return NULL;
1053 /***********************************************************************
1054 * destroy_thread_windows
1056 * Destroy all window owned by the current thread.
1058 void destroy_thread_windows(void)
1060 WND *wndPtr;
1061 HWND hwnd = 0, *list;
1062 HMENU menu, sys_menu;
1063 struct window_surface *surface;
1064 int i;
1066 while ((wndPtr = next_thread_window( &hwnd )))
1068 /* destroy the client-side storage */
1070 list = WIN_ListChildren( hwnd );
1071 menu = ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD) ? (HMENU)wndPtr->wIDmenu : 0;
1072 sys_menu = wndPtr->hSysMenu;
1073 free_dce( wndPtr->dce, hwnd );
1074 surface = wndPtr->surface;
1075 InterlockedCompareExchangePointer( &user_handles[USER_HANDLE_TO_INDEX(hwnd)], NULL, wndPtr );
1076 WIN_ReleasePtr( wndPtr );
1077 HeapFree( GetProcessHeap(), 0, wndPtr );
1078 if (menu) DestroyMenu( menu );
1079 if (sys_menu) DestroyMenu( sys_menu );
1080 if (surface)
1082 register_window_surface( surface, NULL );
1083 window_surface_release( surface );
1086 /* free child windows */
1088 if (!list) continue;
1089 for (i = 0; list[i]; i++)
1090 if (!WIN_IsCurrentThread( list[i] ))
1091 SendNotifyMessageW( list[i], WM_WINE_DESTROYWINDOW, 0, 0 );
1092 HeapFree( GetProcessHeap(), 0, list );
1097 /***********************************************************************
1098 * WIN_FixCoordinates
1100 * Fix the coordinates - Helper for WIN_CreateWindowEx.
1101 * returns default show mode in sw.
1103 static void WIN_FixCoordinates( CREATESTRUCTW *cs, INT *sw)
1105 #define IS_DEFAULT(x) ((x) == CW_USEDEFAULT || (x) == (SHORT)0x8000)
1106 POINT pos[2];
1108 if (cs->dwExStyle & WS_EX_MDICHILD)
1110 UINT id = 0;
1112 MDI_CalcDefaultChildPos(cs->hwndParent, -1, pos, 0, &id);
1113 if (!(cs->style & WS_POPUP)) cs->hMenu = ULongToHandle(id);
1115 TRACE("MDI child id %04x\n", id);
1118 if (cs->style & (WS_CHILD | WS_POPUP))
1120 if (cs->dwExStyle & WS_EX_MDICHILD)
1122 if (IS_DEFAULT(cs->x))
1124 cs->x = pos[0].x;
1125 cs->y = pos[0].y;
1127 if (IS_DEFAULT(cs->cx) || !cs->cx) cs->cx = pos[1].x;
1128 if (IS_DEFAULT(cs->cy) || !cs->cy) cs->cy = pos[1].y;
1130 else
1132 if (IS_DEFAULT(cs->x)) cs->x = cs->y = 0;
1133 if (IS_DEFAULT(cs->cx)) cs->cx = cs->cy = 0;
1136 else /* overlapped window */
1138 HMONITOR monitor;
1139 MONITORINFO mon_info;
1140 STARTUPINFOW info;
1142 if (!IS_DEFAULT(cs->x) && !IS_DEFAULT(cs->cx) && !IS_DEFAULT(cs->cy)) return;
1144 monitor = MonitorFromWindow( cs->hwndParent, MONITOR_DEFAULTTOPRIMARY );
1145 mon_info.cbSize = sizeof(mon_info);
1146 GetMonitorInfoW( monitor, &mon_info );
1147 GetStartupInfoW( &info );
1149 if (IS_DEFAULT(cs->x))
1151 if (!IS_DEFAULT(cs->y)) *sw = cs->y;
1152 cs->x = (info.dwFlags & STARTF_USEPOSITION) ? info.dwX : mon_info.rcWork.left;
1153 cs->y = (info.dwFlags & STARTF_USEPOSITION) ? info.dwY : mon_info.rcWork.top;
1156 if (IS_DEFAULT(cs->cx))
1158 if (info.dwFlags & STARTF_USESIZE)
1160 cs->cx = info.dwXSize;
1161 cs->cy = info.dwYSize;
1163 else
1165 cs->cx = (mon_info.rcWork.right - mon_info.rcWork.left) * 3 / 4 - cs->x;
1166 cs->cy = (mon_info.rcWork.bottom - mon_info.rcWork.top) * 3 / 4 - cs->y;
1169 /* neither x nor cx are default. Check the y values .
1170 * In the trace we see Outlook and Outlook Express using
1171 * cy set to CW_USEDEFAULT when opening the address book.
1173 else if (IS_DEFAULT(cs->cy))
1175 FIXME("Strange use of CW_USEDEFAULT in nHeight\n");
1176 cs->cy = (mon_info.rcWork.bottom - mon_info.rcWork.top) * 3 / 4 - cs->y;
1179 #undef IS_DEFAULT
1182 /***********************************************************************
1183 * dump_window_styles
1185 static void dump_window_styles( DWORD style, DWORD exstyle )
1187 TRACE( "style:" );
1188 if(style & WS_POPUP) TRACE(" WS_POPUP");
1189 if(style & WS_CHILD) TRACE(" WS_CHILD");
1190 if(style & WS_MINIMIZE) TRACE(" WS_MINIMIZE");
1191 if(style & WS_VISIBLE) TRACE(" WS_VISIBLE");
1192 if(style & WS_DISABLED) TRACE(" WS_DISABLED");
1193 if(style & WS_CLIPSIBLINGS) TRACE(" WS_CLIPSIBLINGS");
1194 if(style & WS_CLIPCHILDREN) TRACE(" WS_CLIPCHILDREN");
1195 if(style & WS_MAXIMIZE) TRACE(" WS_MAXIMIZE");
1196 if((style & WS_CAPTION) == WS_CAPTION) TRACE(" WS_CAPTION");
1197 else
1199 if(style & WS_BORDER) TRACE(" WS_BORDER");
1200 if(style & WS_DLGFRAME) TRACE(" WS_DLGFRAME");
1202 if(style & WS_VSCROLL) TRACE(" WS_VSCROLL");
1203 if(style & WS_HSCROLL) TRACE(" WS_HSCROLL");
1204 if(style & WS_SYSMENU) TRACE(" WS_SYSMENU");
1205 if(style & WS_THICKFRAME) TRACE(" WS_THICKFRAME");
1206 if (style & WS_CHILD)
1208 if(style & WS_GROUP) TRACE(" WS_GROUP");
1209 if(style & WS_TABSTOP) TRACE(" WS_TABSTOP");
1211 else
1213 if(style & WS_MINIMIZEBOX) TRACE(" WS_MINIMIZEBOX");
1214 if(style & WS_MAXIMIZEBOX) TRACE(" WS_MAXIMIZEBOX");
1217 /* FIXME: Add dumping of BS_/ES_/SBS_/LBS_/CBS_/DS_/etc. styles */
1218 #define DUMPED_STYLES \
1219 ((DWORD)(WS_POPUP | \
1220 WS_CHILD | \
1221 WS_MINIMIZE | \
1222 WS_VISIBLE | \
1223 WS_DISABLED | \
1224 WS_CLIPSIBLINGS | \
1225 WS_CLIPCHILDREN | \
1226 WS_MAXIMIZE | \
1227 WS_BORDER | \
1228 WS_DLGFRAME | \
1229 WS_VSCROLL | \
1230 WS_HSCROLL | \
1231 WS_SYSMENU | \
1232 WS_THICKFRAME | \
1233 WS_GROUP | \
1234 WS_TABSTOP | \
1235 WS_MINIMIZEBOX | \
1236 WS_MAXIMIZEBOX))
1238 if(style & ~DUMPED_STYLES) TRACE(" %08x", style & ~DUMPED_STYLES);
1239 TRACE("\n");
1240 #undef DUMPED_STYLES
1242 TRACE( "exstyle:" );
1243 if(exstyle & WS_EX_DLGMODALFRAME) TRACE(" WS_EX_DLGMODALFRAME");
1244 if(exstyle & WS_EX_DRAGDETECT) TRACE(" WS_EX_DRAGDETECT");
1245 if(exstyle & WS_EX_NOPARENTNOTIFY) TRACE(" WS_EX_NOPARENTNOTIFY");
1246 if(exstyle & WS_EX_TOPMOST) TRACE(" WS_EX_TOPMOST");
1247 if(exstyle & WS_EX_ACCEPTFILES) TRACE(" WS_EX_ACCEPTFILES");
1248 if(exstyle & WS_EX_TRANSPARENT) TRACE(" WS_EX_TRANSPARENT");
1249 if(exstyle & WS_EX_MDICHILD) TRACE(" WS_EX_MDICHILD");
1250 if(exstyle & WS_EX_TOOLWINDOW) TRACE(" WS_EX_TOOLWINDOW");
1251 if(exstyle & WS_EX_WINDOWEDGE) TRACE(" WS_EX_WINDOWEDGE");
1252 if(exstyle & WS_EX_CLIENTEDGE) TRACE(" WS_EX_CLIENTEDGE");
1253 if(exstyle & WS_EX_CONTEXTHELP) TRACE(" WS_EX_CONTEXTHELP");
1254 if(exstyle & WS_EX_RIGHT) TRACE(" WS_EX_RIGHT");
1255 if(exstyle & WS_EX_RTLREADING) TRACE(" WS_EX_RTLREADING");
1256 if(exstyle & WS_EX_LEFTSCROLLBAR) TRACE(" WS_EX_LEFTSCROLLBAR");
1257 if(exstyle & WS_EX_CONTROLPARENT) TRACE(" WS_EX_CONTROLPARENT");
1258 if(exstyle & WS_EX_STATICEDGE) TRACE(" WS_EX_STATICEDGE");
1259 if(exstyle & WS_EX_APPWINDOW) TRACE(" WS_EX_APPWINDOW");
1260 if(exstyle & WS_EX_LAYERED) TRACE(" WS_EX_LAYERED");
1261 if(exstyle & WS_EX_NOINHERITLAYOUT) TRACE(" WS_EX_NOINHERITLAYOUT");
1262 if(exstyle & WS_EX_LAYOUTRTL) TRACE(" WS_EX_LAYOUTRTL");
1263 if(exstyle & WS_EX_COMPOSITED) TRACE(" WS_EX_COMPOSITED");
1264 if(exstyle & WS_EX_NOACTIVATE) TRACE(" WS_EX_NOACTIVATE");
1266 #define DUMPED_EX_STYLES \
1267 ((DWORD)(WS_EX_DLGMODALFRAME | \
1268 WS_EX_DRAGDETECT | \
1269 WS_EX_NOPARENTNOTIFY | \
1270 WS_EX_TOPMOST | \
1271 WS_EX_ACCEPTFILES | \
1272 WS_EX_TRANSPARENT | \
1273 WS_EX_MDICHILD | \
1274 WS_EX_TOOLWINDOW | \
1275 WS_EX_WINDOWEDGE | \
1276 WS_EX_CLIENTEDGE | \
1277 WS_EX_CONTEXTHELP | \
1278 WS_EX_RIGHT | \
1279 WS_EX_RTLREADING | \
1280 WS_EX_LEFTSCROLLBAR | \
1281 WS_EX_CONTROLPARENT | \
1282 WS_EX_STATICEDGE | \
1283 WS_EX_APPWINDOW | \
1284 WS_EX_LAYERED | \
1285 WS_EX_NOINHERITLAYOUT | \
1286 WS_EX_LAYOUTRTL | \
1287 WS_EX_COMPOSITED |\
1288 WS_EX_NOACTIVATE))
1290 if(exstyle & ~DUMPED_EX_STYLES) TRACE(" %08x", exstyle & ~DUMPED_EX_STYLES);
1291 TRACE("\n");
1292 #undef DUMPED_EX_STYLES
1296 /***********************************************************************
1297 * WIN_CreateWindowEx
1299 * Implementation of CreateWindowEx().
1301 HWND WIN_CreateWindowEx( CREATESTRUCTW *cs, LPCWSTR className, HINSTANCE module, BOOL unicode )
1303 INT cx, cy, style, sw = SW_SHOW;
1304 LRESULT result;
1305 RECT rect;
1306 WND *wndPtr;
1307 HWND hwnd, parent, owner, top_child = 0;
1308 const WCHAR *p = className;
1309 MDICREATESTRUCTW mdi_cs;
1310 CBT_CREATEWNDW cbtc;
1311 CREATESTRUCTW cbcs;
1313 className = CLASS_GetVersionedName(className, NULL, TRUE);
1315 TRACE("%s %s%s%s ex=%08x style=%08x %d,%d %dx%d parent=%p menu=%p inst=%p params=%p\n",
1316 unicode ? debugstr_w(cs->lpszName) : debugstr_a((LPCSTR)cs->lpszName),
1317 debugstr_w(p), p != className ? "->" : "", p != className ? debugstr_w(className) : "",
1318 cs->dwExStyle, cs->style, cs->x, cs->y, cs->cx, cs->cy,
1319 cs->hwndParent, cs->hMenu, cs->hInstance, cs->lpCreateParams );
1320 if(TRACE_ON(win)) dump_window_styles( cs->style, cs->dwExStyle );
1322 /* Fix the styles for MDI children */
1323 if (cs->dwExStyle & WS_EX_MDICHILD)
1325 if (!(win_get_flags( cs->hwndParent ) & WIN_ISMDICLIENT))
1327 WARN("WS_EX_MDICHILD, but parent %p is not MDIClient\n", cs->hwndParent);
1328 return 0;
1331 /* cs->lpCreateParams of WM_[NC]CREATE is different for MDI children.
1332 * MDICREATESTRUCT members have the originally passed values.
1334 * Note: we rely on the fact that MDICREATESTRUCTA and MDICREATESTRUCTW
1335 * have the same layout.
1337 mdi_cs.szClass = cs->lpszClass;
1338 mdi_cs.szTitle = cs->lpszName;
1339 mdi_cs.hOwner = cs->hInstance;
1340 mdi_cs.x = cs->x;
1341 mdi_cs.y = cs->y;
1342 mdi_cs.cx = cs->cx;
1343 mdi_cs.cy = cs->cy;
1344 mdi_cs.style = cs->style;
1345 mdi_cs.lParam = (LPARAM)cs->lpCreateParams;
1347 cs->lpCreateParams = &mdi_cs;
1349 if (GetWindowLongW(cs->hwndParent, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1351 if (cs->style & WS_POPUP)
1353 TRACE("WS_POPUP with MDIS_ALLCHILDSTYLES is not allowed\n");
1354 return 0;
1356 cs->style |= WS_CHILD | WS_CLIPSIBLINGS;
1358 else
1360 cs->style &= ~WS_POPUP;
1361 cs->style |= WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CAPTION |
1362 WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
1365 top_child = GetWindow(cs->hwndParent, GW_CHILD);
1367 if (top_child)
1369 /* Restore current maximized child */
1370 if((cs->style & WS_VISIBLE) && IsZoomed(top_child))
1372 TRACE("Restoring current maximized child %p\n", top_child);
1373 if (cs->style & WS_MAXIMIZE)
1375 /* if the new window is maximized don't bother repainting */
1376 SendMessageW( top_child, WM_SETREDRAW, FALSE, 0 );
1377 ShowWindow( top_child, SW_SHOWNORMAL );
1378 SendMessageW( top_child, WM_SETREDRAW, TRUE, 0 );
1380 else ShowWindow( top_child, SW_SHOWNORMAL );
1385 /* Find the parent window */
1387 parent = cs->hwndParent;
1388 owner = 0;
1390 if (cs->hwndParent == HWND_MESSAGE)
1392 cs->hwndParent = parent = get_hwnd_message_parent();
1394 else if (cs->hwndParent)
1396 if ((cs->style & (WS_CHILD|WS_POPUP)) != WS_CHILD)
1398 parent = GetDesktopWindow();
1399 owner = cs->hwndParent;
1401 else
1403 DWORD parent_style = GetWindowLongW( parent, GWL_EXSTYLE );
1404 if ((parent_style & WS_EX_LAYOUTRTL) && !(parent_style & WS_EX_NOINHERITLAYOUT))
1405 cs->dwExStyle |= WS_EX_LAYOUTRTL;
1408 else
1410 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
1412 if ((cs->style & (WS_CHILD|WS_POPUP)) == WS_CHILD)
1414 WARN("No parent for child window\n" );
1415 SetLastError(ERROR_TLW_WITH_WSCHILD);
1416 return 0; /* WS_CHILD needs a parent, but WS_POPUP doesn't */
1419 /* are we creating the desktop or HWND_MESSAGE parent itself? */
1420 if (className != (LPCWSTR)DESKTOP_CLASS_ATOM &&
1421 (IS_INTRESOURCE(className) || strcmpiW( className, messageW )))
1423 DWORD layout;
1424 GetProcessDefaultLayout( &layout );
1425 if (layout & LAYOUT_RTL) cs->dwExStyle |= WS_EX_LAYOUTRTL;
1426 parent = GetDesktopWindow();
1430 WIN_FixCoordinates(cs, &sw); /* fix default coordinates */
1432 if ((cs->dwExStyle & WS_EX_DLGMODALFRAME) ||
1433 ((!(cs->dwExStyle & WS_EX_STATICEDGE)) &&
1434 (cs->style & (WS_DLGFRAME | WS_THICKFRAME))))
1435 cs->dwExStyle |= WS_EX_WINDOWEDGE;
1436 else
1437 cs->dwExStyle &= ~WS_EX_WINDOWEDGE;
1439 /* Create the window structure */
1441 if (!(wndPtr = create_window_handle( parent, owner, className, module, unicode )))
1443 WNDCLASSW wc;
1444 /* if it's a comctl32 class, GetClassInfo will load it, then we can retry */
1445 if (GetLastError() != ERROR_INVALID_HANDLE ||
1446 !GetClassInfoW( 0, className, &wc ) ||
1447 !(wndPtr = create_window_handle( parent, owner, className, module, unicode )))
1448 return 0;
1450 hwnd = wndPtr->obj.handle;
1452 /* Fill the window structure */
1454 wndPtr->tid = GetCurrentThreadId();
1455 wndPtr->hInstance = cs->hInstance;
1456 wndPtr->text = NULL;
1457 wndPtr->dwStyle = cs->style & ~WS_VISIBLE;
1458 wndPtr->dwExStyle = cs->dwExStyle;
1459 wndPtr->wIDmenu = 0;
1460 wndPtr->helpContext = 0;
1461 wndPtr->pScroll = NULL;
1462 wndPtr->userdata = 0;
1463 wndPtr->hIcon = 0;
1464 wndPtr->hIconSmall = 0;
1465 wndPtr->hIconSmall2 = 0;
1466 wndPtr->hSysMenu = 0;
1467 wndPtr->dpi_awareness = GetThreadDpiAwarenessContext();
1469 wndPtr->min_pos.x = wndPtr->min_pos.y = -1;
1470 wndPtr->max_pos.x = wndPtr->max_pos.y = -1;
1471 SetRect( &wndPtr->normal_rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
1473 if (wndPtr->dwStyle & WS_SYSMENU) SetSystemMenu( hwnd, 0 );
1476 * Correct the window styles.
1478 * It affects only the style loaded into the WIN structure.
1481 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
1483 wndPtr->dwStyle |= WS_CLIPSIBLINGS;
1484 if (!(wndPtr->dwStyle & WS_POPUP))
1485 wndPtr->dwStyle |= WS_CAPTION;
1488 /* WS_EX_WINDOWEDGE depends on some other styles */
1489 if (wndPtr->dwExStyle & WS_EX_DLGMODALFRAME)
1490 wndPtr->dwExStyle |= WS_EX_WINDOWEDGE;
1491 else if (wndPtr->dwStyle & (WS_DLGFRAME | WS_THICKFRAME))
1493 if (!((wndPtr->dwExStyle & WS_EX_STATICEDGE) &&
1494 (wndPtr->dwStyle & (WS_CHILD | WS_POPUP))))
1495 wndPtr->dwExStyle |= WS_EX_WINDOWEDGE;
1497 else
1498 wndPtr->dwExStyle &= ~WS_EX_WINDOWEDGE;
1500 if (!(wndPtr->dwStyle & (WS_CHILD | WS_POPUP)))
1501 wndPtr->flags |= WIN_NEED_SIZE;
1503 SERVER_START_REQ( set_window_info )
1505 req->handle = wine_server_user_handle( hwnd );
1506 req->flags = SET_WIN_STYLE | SET_WIN_EXSTYLE | SET_WIN_INSTANCE | SET_WIN_UNICODE;
1507 req->style = wndPtr->dwStyle;
1508 req->ex_style = wndPtr->dwExStyle;
1509 req->instance = wine_server_client_ptr( wndPtr->hInstance );
1510 req->is_unicode = (wndPtr->flags & WIN_ISUNICODE) != 0;
1511 req->extra_offset = -1;
1512 wine_server_call( req );
1514 SERVER_END_REQ;
1516 /* Set the window menu */
1518 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
1520 if (cs->hMenu)
1522 if (!MENU_SetMenu(hwnd, cs->hMenu))
1524 WIN_ReleasePtr( wndPtr );
1525 free_window_handle( hwnd );
1526 return 0;
1529 else
1531 LPCWSTR menuName = (LPCWSTR)GetClassLongPtrW( hwnd, GCLP_MENUNAME );
1532 if (menuName)
1534 cs->hMenu = LoadMenuW( cs->hInstance, menuName );
1535 if (cs->hMenu) MENU_SetMenu( hwnd, cs->hMenu );
1539 else SetWindowLongPtrW( hwnd, GWLP_ID, (ULONG_PTR)cs->hMenu );
1541 /* call the WH_CBT hook */
1543 /* the window style passed to the hook must be the real window style,
1544 * rather than just the window style that the caller to CreateWindowEx
1545 * passed in, so we have to copy the original CREATESTRUCT and get the
1546 * the real style. */
1547 cbcs = *cs;
1548 cbcs.style = wndPtr->dwStyle;
1549 cbtc.lpcs = &cbcs;
1550 cbtc.hwndInsertAfter = HWND_TOP;
1551 WIN_ReleasePtr( wndPtr );
1552 if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode )) goto failed;
1554 /* send the WM_GETMINMAXINFO message and fix the size if needed */
1556 cx = cs->cx;
1557 cy = cs->cy;
1558 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
1560 POINT maxSize, maxPos, minTrack, maxTrack;
1561 WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
1562 if (maxTrack.x < cx) cx = maxTrack.x;
1563 if (maxTrack.y < cy) cy = maxTrack.y;
1564 if (minTrack.x > cx) cx = minTrack.x;
1565 if (minTrack.y > cy) cy = minTrack.y;
1568 if (cx < 0) cx = 0;
1569 if (cy < 0) cy = 0;
1570 SetRect( &rect, cs->x, cs->y, cs->x + cx, cs->y + cy );
1571 /* check for wraparound */
1572 if (cs->x + cx < cs->x) rect.right = 0x7fffffff;
1573 if (cs->y + cy < cs->y) rect.bottom = 0x7fffffff;
1574 if (!set_window_pos( hwnd, 0, SWP_NOZORDER | SWP_NOACTIVATE, &rect, &rect, NULL )) goto failed;
1576 /* send WM_NCCREATE */
1578 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cx, cy );
1579 if (unicode)
1580 result = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1581 else
1582 result = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1583 if (!result)
1585 WARN( "%p: aborted by WM_NCCREATE\n", hwnd );
1586 goto failed;
1589 /* create default IME window */
1591 if (imm_register_window && !is_desktop_window( hwnd ) &&
1592 parent != get_hwnd_message_parent() && imm_register_window( hwnd ))
1594 TRACE("register IME window for %p\n", hwnd);
1595 win_set_flags( hwnd, WIN_HAS_IME_WIN, 0 );
1598 /* send WM_NCCALCSIZE */
1600 if (WIN_GetRectangles( hwnd, COORDS_PARENT, &rect, NULL ))
1602 /* yes, even if the CBT hook was called with HWND_TOP */
1603 HWND insert_after = (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
1604 RECT client_rect = rect;
1606 /* the rectangle is in screen coords for WM_NCCALCSIZE when wparam is FALSE */
1607 MapWindowPoints( parent, 0, (POINT *)&client_rect, 2 );
1608 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&client_rect );
1609 MapWindowPoints( 0, parent, (POINT *)&client_rect, 2 );
1610 set_window_pos( hwnd, insert_after, SWP_NOACTIVATE, &rect, &client_rect, NULL );
1612 else return 0;
1614 /* send WM_CREATE */
1616 if (unicode)
1617 result = SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs );
1618 else
1619 result = SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs );
1620 if (result == -1) goto failed;
1622 /* call the driver */
1624 if (!USER_Driver->pCreateWindow( hwnd )) goto failed;
1626 NotifyWinEvent(EVENT_OBJECT_CREATE, hwnd, OBJID_WINDOW, 0);
1628 /* send the size messages */
1630 if (!(win_get_flags( hwnd ) & WIN_NEED_SIZE))
1632 WIN_GetRectangles( hwnd, COORDS_PARENT, NULL, &rect );
1633 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1634 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1635 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( rect.left, rect.top ) );
1638 /* Show the window, maximizing or minimizing if needed */
1640 style = WIN_SetStyle( hwnd, 0, WS_MAXIMIZE | WS_MINIMIZE );
1641 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1643 RECT newPos;
1644 UINT swFlag = (style & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1646 swFlag = WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1647 swFlag |= SWP_FRAMECHANGED; /* Frame always gets changed */
1648 if (!(style & WS_VISIBLE) || (style & WS_CHILD) || GetActiveWindow()) swFlag |= SWP_NOACTIVATE;
1649 SetWindowPos( hwnd, 0, newPos.left, newPos.top, newPos.right - newPos.left,
1650 newPos.bottom - newPos.top, swFlag );
1653 /* Notify the parent window only */
1655 send_parent_notify( hwnd, WM_CREATE );
1656 if (!IsWindow( hwnd )) return 0;
1658 if (parent == GetDesktopWindow())
1659 PostMessageW( parent, WM_PARENTNOTIFY, WM_CREATE, (LPARAM)hwnd );
1661 if (cs->style & WS_VISIBLE)
1663 if (cs->style & WS_MAXIMIZE)
1664 sw = SW_SHOW;
1665 else if (cs->style & WS_MINIMIZE)
1666 sw = SW_SHOWMINIMIZED;
1668 ShowWindow( hwnd, sw );
1669 if (cs->dwExStyle & WS_EX_MDICHILD)
1671 SendMessageW(cs->hwndParent, WM_MDIREFRESHMENU, 0, 0);
1672 /* ShowWindow won't activate child windows */
1673 SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE );
1677 /* Call WH_SHELL hook */
1679 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) && !GetWindow( hwnd, GW_OWNER ))
1680 HOOK_CallHooks( WH_SHELL, HSHELL_WINDOWCREATED, (WPARAM)hwnd, 0, TRUE );
1682 TRACE("created window %p\n", hwnd);
1683 return hwnd;
1685 failed:
1686 WIN_DestroyWindow( hwnd );
1687 return 0;
1691 /***********************************************************************
1692 * CreateWindowExA (USER32.@)
1694 HWND WINAPI DECLSPEC_HOTPATCH CreateWindowExA( DWORD exStyle, LPCSTR className,
1695 LPCSTR windowName, DWORD style, INT x,
1696 INT y, INT width, INT height,
1697 HWND parent, HMENU menu,
1698 HINSTANCE instance, LPVOID data )
1700 CREATESTRUCTA cs;
1702 cs.lpCreateParams = data;
1703 cs.hInstance = instance;
1704 cs.hMenu = menu;
1705 cs.hwndParent = parent;
1706 cs.x = x;
1707 cs.y = y;
1708 cs.cx = width;
1709 cs.cy = height;
1710 cs.style = style;
1711 cs.lpszName = windowName;
1712 cs.lpszClass = className;
1713 cs.dwExStyle = exStyle;
1715 if (!IS_INTRESOURCE(className))
1717 WCHAR bufferW[256];
1718 if (!MultiByteToWideChar( CP_ACP, 0, className, -1, bufferW, ARRAY_SIZE( bufferW )))
1719 return 0;
1720 return wow_handlers.create_window( (CREATESTRUCTW *)&cs, bufferW, instance, FALSE );
1722 /* Note: we rely on the fact that CREATESTRUCTA and */
1723 /* CREATESTRUCTW have the same layout. */
1724 return wow_handlers.create_window( (CREATESTRUCTW *)&cs, (LPCWSTR)className, instance, FALSE );
1728 /***********************************************************************
1729 * CreateWindowExW (USER32.@)
1731 HWND WINAPI DECLSPEC_HOTPATCH CreateWindowExW( DWORD exStyle, LPCWSTR className,
1732 LPCWSTR windowName, DWORD style, INT x,
1733 INT y, INT width, INT height,
1734 HWND parent, HMENU menu,
1735 HINSTANCE instance, LPVOID data )
1737 CREATESTRUCTW cs;
1739 cs.lpCreateParams = data;
1740 cs.hInstance = instance;
1741 cs.hMenu = menu;
1742 cs.hwndParent = parent;
1743 cs.x = x;
1744 cs.y = y;
1745 cs.cx = width;
1746 cs.cy = height;
1747 cs.style = style;
1748 cs.lpszName = windowName;
1749 cs.lpszClass = className;
1750 cs.dwExStyle = exStyle;
1752 return wow_handlers.create_window( &cs, className, instance, TRUE );
1756 /***********************************************************************
1757 * WIN_SendDestroyMsg
1759 static void WIN_SendDestroyMsg( HWND hwnd )
1761 GUITHREADINFO info;
1763 info.cbSize = sizeof(info);
1764 if (GetGUIThreadInfo( GetCurrentThreadId(), &info ))
1766 if (hwnd == info.hwndCaret) DestroyCaret();
1767 if (hwnd == info.hwndActive) WINPOS_ActivateOtherWindow( hwnd );
1770 if (hwnd == GetClipboardOwner()) CLIPBOARD_ReleaseOwner( hwnd );
1773 * Send the WM_DESTROY to the window.
1775 SendMessageW( hwnd, WM_DESTROY, 0, 0);
1778 * This WM_DESTROY message can trigger re-entrant calls to DestroyWindow
1779 * make sure that the window still exists when we come back.
1781 if (IsWindow(hwnd))
1783 HWND* pWndArray;
1784 int i;
1786 if (!(pWndArray = WIN_ListChildren( hwnd ))) return;
1788 for (i = 0; pWndArray[i]; i++)
1790 if (IsWindow( pWndArray[i] )) WIN_SendDestroyMsg( pWndArray[i] );
1792 HeapFree( GetProcessHeap(), 0, pWndArray );
1794 else
1795 WARN("\tdestroyed itself while in WM_DESTROY!\n");
1799 /***********************************************************************
1800 * DestroyWindow (USER32.@)
1802 BOOL WINAPI DestroyWindow( HWND hwnd )
1804 BOOL is_child;
1806 if (!(hwnd = WIN_IsCurrentThread( hwnd )) || is_desktop_window( hwnd ))
1808 SetLastError( ERROR_ACCESS_DENIED );
1809 return FALSE;
1812 TRACE("(%p)\n", hwnd);
1814 /* Call hooks */
1816 if (HOOK_CallHooks( WH_CBT, HCBT_DESTROYWND, (WPARAM)hwnd, 0, TRUE )) return FALSE;
1818 if (MENU_IsMenuActive() == hwnd)
1819 EndMenu();
1821 is_child = (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) != 0;
1823 if (is_child)
1825 if (!USER_IsExitingThread( GetCurrentThreadId() ))
1826 send_parent_notify( hwnd, WM_DESTROY );
1828 else if (!GetWindow( hwnd, GW_OWNER ))
1830 HOOK_CallHooks( WH_SHELL, HSHELL_WINDOWDESTROYED, (WPARAM)hwnd, 0L, TRUE );
1831 /* FIXME: clean up palette - see "Internals" p.352 */
1834 if (!IsWindow(hwnd)) return TRUE;
1836 /* Hide the window */
1837 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)
1839 /* Only child windows receive WM_SHOWWINDOW in DestroyWindow() */
1840 if (is_child)
1841 ShowWindow( hwnd, SW_HIDE );
1842 else
1843 SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE |
1844 SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW );
1847 if (!IsWindow(hwnd)) return TRUE;
1849 /* Recursively destroy owned windows */
1851 if (!is_child)
1853 for (;;)
1855 int i;
1856 BOOL got_one = FALSE;
1857 HWND *list = WIN_ListChildren( GetDesktopWindow() );
1858 if (list)
1860 for (i = 0; list[i]; i++)
1862 if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
1863 if (WIN_IsCurrentThread( list[i] ))
1865 DestroyWindow( list[i] );
1866 got_one = TRUE;
1867 continue;
1869 WIN_SetOwner( list[i], 0 );
1871 HeapFree( GetProcessHeap(), 0, list );
1873 if (!got_one) break;
1877 /* Send destroy messages */
1879 WIN_SendDestroyMsg( hwnd );
1880 if (!IsWindow( hwnd )) return TRUE;
1882 /* Destroy the window storage */
1884 WIN_DestroyWindow( hwnd );
1885 return TRUE;
1889 /***********************************************************************
1890 * CloseWindow (USER32.@)
1892 BOOL WINAPI CloseWindow( HWND hwnd )
1894 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) return FALSE;
1895 ShowWindow( hwnd, SW_MINIMIZE );
1896 return TRUE;
1900 /***********************************************************************
1901 * OpenIcon (USER32.@)
1903 BOOL WINAPI OpenIcon( HWND hwnd )
1905 if (!IsIconic( hwnd )) return FALSE;
1906 ShowWindow( hwnd, SW_SHOWNORMAL );
1907 return TRUE;
1911 /***********************************************************************
1912 * FindWindowExW (USER32.@)
1914 HWND WINAPI FindWindowExW( HWND parent, HWND child, LPCWSTR className, LPCWSTR title )
1916 HWND *list;
1917 HWND retvalue = 0;
1918 int i = 0, len = 0;
1919 WCHAR *buffer = NULL;
1921 if (!parent && child) parent = GetDesktopWindow();
1922 else if (parent == HWND_MESSAGE) parent = get_hwnd_message_parent();
1924 if (title)
1926 len = strlenW(title) + 1; /* one extra char to check for chars beyond the end */
1927 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return 0;
1930 if (!(list = list_window_children( 0, parent, className, 0 ))) goto done;
1932 if (child)
1934 child = WIN_GetFullHandle( child );
1935 while (list[i] && list[i] != child) i++;
1936 if (!list[i]) goto done;
1937 i++; /* start from next window */
1940 if (title)
1942 while (list[i])
1944 if (InternalGetWindowText( list[i], buffer, len + 1 ))
1946 if (!strcmpiW( buffer, title )) break;
1948 else
1950 if (!title[0]) break;
1952 i++;
1955 retvalue = list[i];
1957 done:
1958 HeapFree( GetProcessHeap(), 0, list );
1959 HeapFree( GetProcessHeap(), 0, buffer );
1960 return retvalue;
1965 /***********************************************************************
1966 * FindWindowA (USER32.@)
1968 HWND WINAPI FindWindowA( LPCSTR className, LPCSTR title )
1970 HWND ret = FindWindowExA( 0, 0, className, title );
1971 if (!ret) SetLastError (ERROR_CANNOT_FIND_WND_CLASS);
1972 return ret;
1976 /***********************************************************************
1977 * FindWindowExA (USER32.@)
1979 HWND WINAPI FindWindowExA( HWND parent, HWND child, LPCSTR className, LPCSTR title )
1981 LPWSTR titleW = NULL;
1982 HWND hwnd = 0;
1984 if (title)
1986 DWORD len = MultiByteToWideChar( CP_ACP, 0, title, -1, NULL, 0 );
1987 if (!(titleW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return 0;
1988 MultiByteToWideChar( CP_ACP, 0, title, -1, titleW, len );
1991 if (!IS_INTRESOURCE(className))
1993 WCHAR classW[256];
1994 if (MultiByteToWideChar( CP_ACP, 0, className, -1, classW, ARRAY_SIZE( classW )))
1995 hwnd = FindWindowExW( parent, child, classW, titleW );
1997 else
1999 hwnd = FindWindowExW( parent, child, (LPCWSTR)className, titleW );
2002 HeapFree( GetProcessHeap(), 0, titleW );
2003 return hwnd;
2007 /***********************************************************************
2008 * FindWindowW (USER32.@)
2010 HWND WINAPI FindWindowW( LPCWSTR className, LPCWSTR title )
2012 return FindWindowExW( 0, 0, className, title );
2016 /**********************************************************************
2017 * GetDesktopWindow (USER32.@)
2019 HWND WINAPI GetDesktopWindow(void)
2021 struct user_thread_info *thread_info = get_user_thread_info();
2023 if (thread_info->top_window) return thread_info->top_window;
2025 SERVER_START_REQ( get_desktop_window )
2027 req->force = 0;
2028 if (!wine_server_call( req ))
2030 thread_info->top_window = wine_server_ptr_handle( reply->top_window );
2031 thread_info->msg_window = wine_server_ptr_handle( reply->msg_window );
2034 SERVER_END_REQ;
2036 if (!thread_info->top_window)
2038 static const WCHAR explorer[] = {'\\','e','x','p','l','o','r','e','r','.','e','x','e',0};
2039 static const WCHAR args[] = {' ','/','d','e','s','k','t','o','p',0};
2040 STARTUPINFOW si;
2041 PROCESS_INFORMATION pi;
2042 WCHAR windir[MAX_PATH];
2043 WCHAR app[MAX_PATH + sizeof(explorer)/sizeof(WCHAR)];
2044 WCHAR cmdline[MAX_PATH + (sizeof(explorer) + sizeof(args))/sizeof(WCHAR)];
2045 WCHAR desktop[MAX_PATH];
2046 void *redir;
2048 SERVER_START_REQ( set_user_object_info )
2050 req->handle = wine_server_obj_handle( GetThreadDesktop(GetCurrentThreadId()) );
2051 req->flags = SET_USER_OBJECT_GET_FULL_NAME;
2052 wine_server_set_reply( req, desktop, sizeof(desktop) - sizeof(WCHAR) );
2053 if (!wine_server_call( req ))
2055 size_t size = wine_server_reply_size( reply );
2056 desktop[size / sizeof(WCHAR)] = 0;
2057 TRACE( "starting explorer for desktop %s\n", debugstr_w(desktop) );
2059 else
2060 desktop[0] = 0;
2062 SERVER_END_REQ;
2064 memset( &si, 0, sizeof(si) );
2065 si.cb = sizeof(si);
2066 si.lpDesktop = *desktop ? desktop : NULL;
2067 si.dwFlags = STARTF_USESTDHANDLES;
2068 si.hStdInput = 0;
2069 si.hStdOutput = 0;
2070 si.hStdError = GetStdHandle( STD_ERROR_HANDLE );
2072 GetSystemDirectoryW( windir, MAX_PATH );
2073 strcpyW( app, windir );
2074 strcatW( app, explorer );
2075 strcpyW( cmdline, app );
2076 strcatW( cmdline, args );
2078 Wow64DisableWow64FsRedirection( &redir );
2079 if (CreateProcessW( app, cmdline, NULL, NULL, FALSE, DETACHED_PROCESS,
2080 NULL, windir, &si, &pi ))
2082 TRACE( "started explorer pid %04x tid %04x\n", pi.dwProcessId, pi.dwThreadId );
2083 WaitForInputIdle( pi.hProcess, 10000 );
2084 CloseHandle( pi.hThread );
2085 CloseHandle( pi.hProcess );
2087 else WARN( "failed to start explorer, err %d\n", GetLastError() );
2088 Wow64RevertWow64FsRedirection( redir );
2090 SERVER_START_REQ( get_desktop_window )
2092 req->force = 1;
2093 if (!wine_server_call( req ))
2095 thread_info->top_window = wine_server_ptr_handle( reply->top_window );
2096 thread_info->msg_window = wine_server_ptr_handle( reply->msg_window );
2099 SERVER_END_REQ;
2102 if (!thread_info->top_window || !USER_Driver->pCreateDesktopWindow( thread_info->top_window ))
2103 ERR( "failed to create desktop window\n" );
2105 return thread_info->top_window;
2109 /*******************************************************************
2110 * EnableWindow (USER32.@)
2112 BOOL WINAPI EnableWindow( HWND hwnd, BOOL enable )
2114 BOOL retvalue;
2116 if (is_broadcast(hwnd))
2118 SetLastError( ERROR_INVALID_PARAMETER );
2119 return FALSE;
2122 TRACE("( %p, %d )\n", hwnd, enable);
2124 if (enable)
2126 retvalue = (WIN_SetStyle( hwnd, 0, WS_DISABLED ) & WS_DISABLED) != 0;
2127 if (retvalue) SendMessageW( hwnd, WM_ENABLE, TRUE, 0 );
2129 else
2131 SendMessageW( hwnd, WM_CANCELMODE, 0, 0 );
2133 retvalue = (WIN_SetStyle( hwnd, WS_DISABLED, 0 ) & WS_DISABLED) != 0;
2134 if (!retvalue)
2136 if (hwnd == GetFocus())
2137 SetFocus( 0 ); /* A disabled window can't have the focus */
2139 SendMessageW( hwnd, WM_ENABLE, FALSE, 0 );
2142 return retvalue;
2146 /***********************************************************************
2147 * IsWindowEnabled (USER32.@)
2149 BOOL WINAPI IsWindowEnabled(HWND hWnd)
2151 return !(GetWindowLongW( hWnd, GWL_STYLE ) & WS_DISABLED);
2155 /***********************************************************************
2156 * IsWindowUnicode (USER32.@)
2158 BOOL WINAPI IsWindowUnicode( HWND hwnd )
2160 WND * wndPtr;
2161 BOOL retvalue = FALSE;
2163 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
2165 if (wndPtr == WND_DESKTOP) return TRUE;
2167 if (wndPtr != WND_OTHER_PROCESS)
2169 retvalue = (wndPtr->flags & WIN_ISUNICODE) != 0;
2170 WIN_ReleasePtr( wndPtr );
2172 else
2174 SERVER_START_REQ( get_window_info )
2176 req->handle = wine_server_user_handle( hwnd );
2177 if (!wine_server_call_err( req )) retvalue = reply->is_unicode;
2179 SERVER_END_REQ;
2181 return retvalue;
2185 /***********************************************************************
2186 * GetWindowDpiAwarenessContext (USER32.@)
2188 DPI_AWARENESS_CONTEXT WINAPI GetWindowDpiAwarenessContext( HWND hwnd )
2190 WND *win;
2191 DPI_AWARENESS_CONTEXT ret;
2193 if (!(win = WIN_GetPtr( hwnd )))
2195 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2196 return 0;
2198 if (win == WND_DESKTOP) return DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE;
2199 if (win == WND_OTHER_PROCESS)
2201 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
2202 else SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2203 return 0;
2205 ret = win->dpi_awareness;
2206 WIN_ReleasePtr( win );
2207 return ret;
2211 /**********************************************************************
2212 * WIN_GetWindowLong
2214 * Helper function for GetWindowLong().
2216 static LONG_PTR WIN_GetWindowLong( HWND hwnd, INT offset, UINT size, BOOL unicode )
2218 LONG_PTR retvalue = 0;
2219 WND *wndPtr;
2221 if (offset == GWLP_HWNDPARENT)
2223 HWND parent = GetAncestor( hwnd, GA_PARENT );
2224 if (parent == GetDesktopWindow()) parent = GetWindow( hwnd, GW_OWNER );
2225 return (ULONG_PTR)parent;
2228 if (!(wndPtr = WIN_GetPtr( hwnd )))
2230 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2231 return 0;
2234 if (wndPtr == WND_DESKTOP)
2236 switch (offset)
2238 case GWL_STYLE:
2239 retvalue = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN; /* message parent is not visible */
2240 if (WIN_GetFullHandle( hwnd ) == GetDesktopWindow())
2241 retvalue |= WS_VISIBLE;
2242 return retvalue;
2243 case GWL_EXSTYLE:
2244 case GWLP_USERDATA:
2245 case GWLP_ID:
2246 case GWLP_HINSTANCE:
2247 return 0;
2248 case GWLP_WNDPROC:
2249 SetLastError( ERROR_ACCESS_DENIED );
2250 return 0;
2252 SetLastError( ERROR_INVALID_INDEX );
2253 return 0;
2256 if (wndPtr == WND_OTHER_PROCESS)
2258 if (offset == GWLP_WNDPROC)
2260 SetLastError( ERROR_ACCESS_DENIED );
2261 return 0;
2263 SERVER_START_REQ( set_window_info )
2265 req->handle = wine_server_user_handle( hwnd );
2266 req->flags = 0; /* don't set anything, just retrieve */
2267 req->extra_offset = (offset >= 0) ? offset : -1;
2268 req->extra_size = (offset >= 0) ? size : 0;
2269 if (!wine_server_call_err( req ))
2271 switch(offset)
2273 case GWL_STYLE: retvalue = reply->old_style; break;
2274 case GWL_EXSTYLE: retvalue = reply->old_ex_style; break;
2275 case GWLP_ID: retvalue = reply->old_id; break;
2276 case GWLP_HINSTANCE: retvalue = (ULONG_PTR)wine_server_get_ptr( reply->old_instance ); break;
2277 case GWLP_USERDATA: retvalue = reply->old_user_data; break;
2278 default:
2279 if (offset >= 0) retvalue = get_win_data( &reply->old_extra_value, size );
2280 else SetLastError( ERROR_INVALID_INDEX );
2281 break;
2285 SERVER_END_REQ;
2286 return retvalue;
2289 /* now we have a valid wndPtr */
2291 if (offset >= 0)
2293 if (offset > (int)(wndPtr->cbWndExtra - size))
2295 WARN("Invalid offset %d\n", offset );
2296 WIN_ReleasePtr( wndPtr );
2297 SetLastError( ERROR_INVALID_INDEX );
2298 return 0;
2300 retvalue = get_win_data( (char *)wndPtr->wExtra + offset, size );
2302 /* Special case for dialog window procedure */
2303 if ((offset == DWLP_DLGPROC) && (size == sizeof(LONG_PTR)) && wndPtr->dlgInfo)
2304 retvalue = (LONG_PTR)WINPROC_GetProc( (WNDPROC)retvalue, unicode );
2305 WIN_ReleasePtr( wndPtr );
2306 return retvalue;
2309 switch(offset)
2311 case GWLP_USERDATA: retvalue = wndPtr->userdata; break;
2312 case GWL_STYLE: retvalue = wndPtr->dwStyle; break;
2313 case GWL_EXSTYLE: retvalue = wndPtr->dwExStyle; break;
2314 case GWLP_ID: retvalue = wndPtr->wIDmenu; break;
2315 case GWLP_HINSTANCE: retvalue = (ULONG_PTR)wndPtr->hInstance; break;
2316 case GWLP_WNDPROC:
2317 /* This looks like a hack only for the edit control (see tests). This makes these controls
2318 * more tolerant to A/W mismatches. The lack of W->A->W conversion for such a mismatch suggests
2319 * that the hack is in GetWindowLongPtr[AW], not in winprocs.
2321 if (wndPtr->winproc == BUILTIN_WINPROC(WINPROC_EDIT) && (!unicode != !(wndPtr->flags & WIN_ISUNICODE)))
2322 retvalue = (ULONG_PTR)wndPtr->winproc;
2323 else
2324 retvalue = (ULONG_PTR)WINPROC_GetProc( wndPtr->winproc, unicode );
2325 break;
2326 default:
2327 WARN("Unknown offset %d\n", offset );
2328 SetLastError( ERROR_INVALID_INDEX );
2329 break;
2331 WIN_ReleasePtr(wndPtr);
2332 return retvalue;
2336 /**********************************************************************
2337 * WIN_SetWindowLong
2339 * Helper function for SetWindowLong().
2341 * 0 is the failure code. However, in the case of failure SetLastError
2342 * must be set to distinguish between a 0 return value and a failure.
2344 LONG_PTR WIN_SetWindowLong( HWND hwnd, INT offset, UINT size, LONG_PTR newval, BOOL unicode )
2346 STYLESTRUCT style;
2347 BOOL ok, made_visible = FALSE;
2348 LONG_PTR retval = 0;
2349 WND *wndPtr;
2351 TRACE( "%p %d %lx %c\n", hwnd, offset, newval, unicode ? 'W' : 'A' );
2353 if (is_broadcast(hwnd))
2355 SetLastError( ERROR_INVALID_PARAMETER );
2356 return FALSE;
2359 if (!(wndPtr = WIN_GetPtr( hwnd )))
2361 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2362 return 0;
2364 if (wndPtr == WND_DESKTOP)
2366 /* can't change anything on the desktop window */
2367 SetLastError( ERROR_ACCESS_DENIED );
2368 return 0;
2370 if (wndPtr == WND_OTHER_PROCESS)
2372 if (offset == GWLP_WNDPROC)
2374 SetLastError( ERROR_ACCESS_DENIED );
2375 return 0;
2377 if (offset > 32767 || offset < -32767)
2379 SetLastError( ERROR_INVALID_INDEX );
2380 return 0;
2382 return SendMessageW( hwnd, WM_WINE_SETWINDOWLONG, MAKEWPARAM( offset, size ), newval );
2385 /* first some special cases */
2386 switch( offset )
2388 case GWL_STYLE:
2389 style.styleOld = wndPtr->dwStyle;
2390 style.styleNew = newval;
2391 WIN_ReleasePtr( wndPtr );
2392 SendMessageW( hwnd, WM_STYLECHANGING, GWL_STYLE, (LPARAM)&style );
2393 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
2394 newval = style.styleNew;
2395 /* WS_CLIPSIBLINGS can't be reset on top-level windows */
2396 if (wndPtr->parent == GetDesktopWindow()) newval |= WS_CLIPSIBLINGS;
2397 /* WS_MINIMIZE can't be reset */
2398 if (wndPtr->dwStyle & WS_MINIMIZE) newval |= WS_MINIMIZE;
2399 /* FIXME: changing WS_DLGFRAME | WS_THICKFRAME is supposed to change
2400 WS_EX_WINDOWEDGE too */
2401 break;
2402 case GWL_EXSTYLE:
2403 style.styleOld = wndPtr->dwExStyle;
2404 style.styleNew = newval;
2405 WIN_ReleasePtr( wndPtr );
2406 SendMessageW( hwnd, WM_STYLECHANGING, GWL_EXSTYLE, (LPARAM)&style );
2407 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
2408 /* WS_EX_TOPMOST can only be changed through SetWindowPos */
2409 newval = (style.styleNew & ~WS_EX_TOPMOST) | (wndPtr->dwExStyle & WS_EX_TOPMOST);
2410 /* WS_EX_WINDOWEDGE depends on some other styles */
2411 if (newval & WS_EX_DLGMODALFRAME)
2412 newval |= WS_EX_WINDOWEDGE;
2413 else if (!(newval & WS_EX_STATICEDGE) && (wndPtr->dwStyle & (WS_DLGFRAME | WS_THICKFRAME)))
2414 newval |= WS_EX_WINDOWEDGE;
2415 else
2416 newval &= ~WS_EX_WINDOWEDGE;
2417 break;
2418 case GWLP_HWNDPARENT:
2419 if (wndPtr->parent == GetDesktopWindow())
2421 WIN_ReleasePtr( wndPtr );
2422 return (ULONG_PTR)WIN_SetOwner( hwnd, (HWND)newval );
2424 else
2426 WIN_ReleasePtr( wndPtr );
2427 return (ULONG_PTR)SetParent( hwnd, (HWND)newval );
2429 case GWLP_WNDPROC:
2431 WNDPROC proc;
2432 UINT old_flags = wndPtr->flags;
2433 retval = WIN_GetWindowLong( hwnd, offset, size, unicode );
2434 proc = WINPROC_AllocProc( (WNDPROC)newval, unicode );
2435 if (proc) wndPtr->winproc = proc;
2436 if (WINPROC_IsUnicode( proc, unicode )) wndPtr->flags |= WIN_ISUNICODE;
2437 else wndPtr->flags &= ~WIN_ISUNICODE;
2438 if (!((old_flags ^ wndPtr->flags) & WIN_ISUNICODE))
2440 WIN_ReleasePtr( wndPtr );
2441 return retval;
2443 /* update is_unicode flag on the server side */
2444 break;
2446 case GWLP_ID:
2447 case GWLP_HINSTANCE:
2448 case GWLP_USERDATA:
2449 break;
2450 case DWLP_DLGPROC:
2451 if ((wndPtr->cbWndExtra - sizeof(LONG_PTR) >= DWLP_DLGPROC) &&
2452 (size == sizeof(LONG_PTR)) && wndPtr->dlgInfo)
2454 WNDPROC *ptr = (WNDPROC *)((char *)wndPtr->wExtra + DWLP_DLGPROC);
2455 retval = (ULONG_PTR)WINPROC_GetProc( *ptr, unicode );
2456 *ptr = WINPROC_AllocProc( (WNDPROC)newval, unicode );
2457 WIN_ReleasePtr( wndPtr );
2458 return retval;
2460 /* fall through */
2461 default:
2462 if (offset < 0 || offset > (int)(wndPtr->cbWndExtra - size))
2464 WARN("Invalid offset %d\n", offset );
2465 WIN_ReleasePtr( wndPtr );
2466 SetLastError( ERROR_INVALID_INDEX );
2467 return 0;
2469 else if (get_win_data( (char *)wndPtr->wExtra + offset, size ) == newval)
2471 /* already set to the same value */
2472 WIN_ReleasePtr( wndPtr );
2473 return newval;
2475 break;
2478 SERVER_START_REQ( set_window_info )
2480 req->handle = wine_server_user_handle( hwnd );
2481 req->extra_offset = -1;
2482 switch(offset)
2484 case GWL_STYLE:
2485 req->flags = SET_WIN_STYLE;
2486 req->style = newval;
2487 break;
2488 case GWL_EXSTYLE:
2489 req->flags = SET_WIN_EXSTYLE;
2490 req->ex_style = newval;
2491 break;
2492 case GWLP_ID:
2493 req->flags = SET_WIN_ID;
2494 req->id = newval;
2495 break;
2496 case GWLP_HINSTANCE:
2497 req->flags = SET_WIN_INSTANCE;
2498 req->instance = wine_server_client_ptr( (void *)newval );
2499 break;
2500 case GWLP_WNDPROC:
2501 req->flags = SET_WIN_UNICODE;
2502 req->is_unicode = (wndPtr->flags & WIN_ISUNICODE) != 0;
2503 break;
2504 case GWLP_USERDATA:
2505 req->flags = SET_WIN_USERDATA;
2506 req->user_data = newval;
2507 break;
2508 default:
2509 req->flags = SET_WIN_EXTRA;
2510 req->extra_offset = offset;
2511 req->extra_size = size;
2512 set_win_data( &req->extra_value, newval, size );
2514 if ((ok = !wine_server_call_err( req )))
2516 switch(offset)
2518 case GWL_STYLE:
2519 wndPtr->dwStyle = newval;
2520 retval = reply->old_style;
2521 break;
2522 case GWL_EXSTYLE:
2523 wndPtr->dwExStyle = newval;
2524 retval = reply->old_ex_style;
2525 break;
2526 case GWLP_ID:
2527 wndPtr->wIDmenu = newval;
2528 retval = reply->old_id;
2529 break;
2530 case GWLP_HINSTANCE:
2531 wndPtr->hInstance = (HINSTANCE)newval;
2532 retval = (ULONG_PTR)wine_server_get_ptr( reply->old_instance );
2533 break;
2534 case GWLP_WNDPROC:
2535 break;
2536 case GWLP_USERDATA:
2537 wndPtr->userdata = newval;
2538 retval = reply->old_user_data;
2539 break;
2540 default:
2541 retval = get_win_data( (char *)wndPtr->wExtra + offset, size );
2542 set_win_data( (char *)wndPtr->wExtra + offset, newval, size );
2543 break;
2547 SERVER_END_REQ;
2549 if ((offset == GWL_STYLE && ((style.styleOld ^ style.styleNew) & WS_VISIBLE)) ||
2550 (offset == GWL_EXSTYLE && ((style.styleOld ^ style.styleNew) & WS_EX_LAYERED)))
2552 made_visible = (wndPtr->dwStyle & WS_VISIBLE) != 0;
2553 invalidate_dce( wndPtr, NULL );
2555 WIN_ReleasePtr( wndPtr );
2557 if (!ok) return 0;
2559 if (offset == GWL_STYLE || offset == GWL_EXSTYLE)
2561 style.styleOld = retval;
2562 style.styleNew = newval;
2563 USER_Driver->pSetWindowStyle( hwnd, offset, &style );
2564 if (made_visible) update_window_state( hwnd );
2565 SendMessageW( hwnd, WM_STYLECHANGED, offset, (LPARAM)&style );
2568 return retval;
2572 /**********************************************************************
2573 * GetWindowWord (USER32.@)
2575 WORD WINAPI GetWindowWord( HWND hwnd, INT offset )
2577 switch(offset)
2579 case GWLP_ID:
2580 case GWLP_HINSTANCE:
2581 case GWLP_HWNDPARENT:
2582 break;
2583 default:
2584 if (offset < 0)
2586 WARN("Invalid offset %d\n", offset );
2587 SetLastError( ERROR_INVALID_INDEX );
2588 return 0;
2590 break;
2592 return WIN_GetWindowLong( hwnd, offset, sizeof(WORD), FALSE );
2596 /**********************************************************************
2597 * GetWindowLongA (USER32.@)
2599 LONG WINAPI GetWindowLongA( HWND hwnd, INT offset )
2601 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG), FALSE );
2605 /**********************************************************************
2606 * GetWindowLongW (USER32.@)
2608 LONG WINAPI GetWindowLongW( HWND hwnd, INT offset )
2610 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG), TRUE );
2614 /**********************************************************************
2615 * SetWindowWord (USER32.@)
2617 WORD WINAPI SetWindowWord( HWND hwnd, INT offset, WORD newval )
2619 switch(offset)
2621 case GWLP_ID:
2622 case GWLP_HINSTANCE:
2623 case GWLP_HWNDPARENT:
2624 break;
2625 default:
2626 if (offset < 0)
2628 WARN("Invalid offset %d\n", offset );
2629 SetLastError( ERROR_INVALID_INDEX );
2630 return 0;
2632 break;
2634 return WIN_SetWindowLong( hwnd, offset, sizeof(WORD), newval, FALSE );
2638 /**********************************************************************
2639 * SetWindowLongA (USER32.@)
2641 * See SetWindowLongW.
2643 LONG WINAPI DECLSPEC_HOTPATCH SetWindowLongA( HWND hwnd, INT offset, LONG newval )
2645 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG), newval, FALSE );
2649 /**********************************************************************
2650 * SetWindowLongW (USER32.@) Set window attribute
2652 * SetWindowLong() alters one of a window's attributes or sets a 32-bit (long)
2653 * value in a window's extra memory.
2655 * The _hwnd_ parameter specifies the handle to a window that
2656 * has extra memory. The _newval_ parameter contains the new
2657 * attribute or extra memory value. If positive, the _offset_
2658 * parameter is the byte-addressed location in the window's extra
2659 * memory to set. If negative, _offset_ specifies the window
2660 * attribute to set, and should be one of the following values:
2662 * GWL_EXSTYLE The window's extended window style
2664 * GWL_STYLE The window's window style.
2666 * GWLP_WNDPROC Pointer to the window's window procedure.
2668 * GWLP_HINSTANCE The window's application instance handle.
2670 * GWLP_ID The window's identifier.
2672 * GWLP_USERDATA The window's user-specified data.
2674 * If the window is a dialog box, the _offset_ parameter can be one of
2675 * the following values:
2677 * DWLP_DLGPROC The address of the window's dialog box procedure.
2679 * DWLP_MSGRESULT The return value of a message
2680 * that the dialog box procedure processed.
2682 * DWLP_USER Application specific information.
2684 * RETURNS
2686 * If successful, returns the previous value located at _offset_. Otherwise,
2687 * returns 0.
2689 * NOTES
2691 * Extra memory for a window class is specified by a nonzero cbWndExtra
2692 * parameter of the WNDCLASS structure passed to RegisterClass() at the
2693 * time of class creation.
2695 * Using GWL_WNDPROC to set a new window procedure effectively creates
2696 * a window subclass. Use CallWindowProc() in the new windows procedure
2697 * to pass messages to the superclass's window procedure.
2699 * The user data is reserved for use by the application which created
2700 * the window.
2702 * Do not use GWL_STYLE to change the window's WS_DISABLED style;
2703 * instead, call the EnableWindow() function to change the window's
2704 * disabled state.
2706 * Do not use GWL_HWNDPARENT to reset the window's parent, use
2707 * SetParent() instead.
2709 * Win95:
2710 * When offset is GWL_STYLE and the calling app's ver is 4.0,
2711 * it sends WM_STYLECHANGING before changing the settings
2712 * and WM_STYLECHANGED afterwards.
2713 * App ver 4.0 can't use SetWindowLong to change WS_EX_TOPMOST.
2715 LONG WINAPI DECLSPEC_HOTPATCH SetWindowLongW(
2716 HWND hwnd, /* [in] window to alter */
2717 INT offset, /* [in] offset, in bytes, of location to alter */
2718 LONG newval /* [in] new value of location */
2720 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG), newval, TRUE );
2724 /*******************************************************************
2725 * GetWindowTextA (USER32.@)
2727 INT WINAPI GetWindowTextA( HWND hwnd, LPSTR lpString, INT nMaxCount )
2729 WCHAR *buffer;
2731 if (!lpString || nMaxCount <= 0) return 0;
2733 if (WIN_IsCurrentProcess( hwnd ))
2735 lpString[0] = 0;
2736 return (INT)SendMessageA( hwnd, WM_GETTEXT, nMaxCount, (LPARAM)lpString );
2739 /* when window belongs to other process, don't send a message */
2740 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, nMaxCount * sizeof(WCHAR) ))) return 0;
2741 get_server_window_text( hwnd, buffer, nMaxCount );
2742 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, lpString, nMaxCount, NULL, NULL ))
2743 lpString[nMaxCount-1] = 0;
2744 HeapFree( GetProcessHeap(), 0, buffer );
2745 return strlen(lpString);
2749 /*******************************************************************
2750 * InternalGetWindowText (USER32.@)
2752 INT WINAPI InternalGetWindowText(HWND hwnd,LPWSTR lpString,INT nMaxCount )
2754 WND *win;
2756 if (nMaxCount <= 0) return 0;
2757 if (!(win = WIN_GetPtr( hwnd ))) return 0;
2758 if (win == WND_DESKTOP) lpString[0] = 0;
2759 else if (win != WND_OTHER_PROCESS)
2761 if (win->text) lstrcpynW( lpString, win->text, nMaxCount );
2762 else lpString[0] = 0;
2763 WIN_ReleasePtr( win );
2765 else
2767 get_server_window_text( hwnd, lpString, nMaxCount );
2769 return strlenW(lpString);
2773 /*******************************************************************
2774 * GetWindowTextW (USER32.@)
2776 INT WINAPI GetWindowTextW( HWND hwnd, LPWSTR lpString, INT nMaxCount )
2778 if (!lpString || nMaxCount <= 0) return 0;
2780 if (WIN_IsCurrentProcess( hwnd ))
2782 lpString[0] = 0;
2783 return (INT)SendMessageW( hwnd, WM_GETTEXT, nMaxCount, (LPARAM)lpString );
2786 /* when window belongs to other process, don't send a message */
2787 get_server_window_text( hwnd, lpString, nMaxCount );
2788 return strlenW(lpString);
2792 /*******************************************************************
2793 * SetWindowTextA (USER32.@)
2794 * SetWindowText (USER32.@)
2796 BOOL WINAPI DECLSPEC_HOTPATCH SetWindowTextA( HWND hwnd, LPCSTR lpString )
2798 if (is_broadcast(hwnd))
2800 SetLastError( ERROR_INVALID_PARAMETER );
2801 return FALSE;
2803 if (!WIN_IsCurrentProcess( hwnd ))
2804 WARN( "setting text %s of other process window %p should not use SendMessage\n",
2805 debugstr_a(lpString), hwnd );
2806 return (BOOL)SendMessageA( hwnd, WM_SETTEXT, 0, (LPARAM)lpString );
2810 /*******************************************************************
2811 * SetWindowTextW (USER32.@)
2813 BOOL WINAPI DECLSPEC_HOTPATCH SetWindowTextW( HWND hwnd, LPCWSTR lpString )
2815 if (is_broadcast(hwnd))
2817 SetLastError( ERROR_INVALID_PARAMETER );
2818 return FALSE;
2820 if (!WIN_IsCurrentProcess( hwnd ))
2821 WARN( "setting text %s of other process window %p should not use SendMessage\n",
2822 debugstr_w(lpString), hwnd );
2823 return (BOOL)SendMessageW( hwnd, WM_SETTEXT, 0, (LPARAM)lpString );
2827 /*******************************************************************
2828 * GetWindowTextLengthA (USER32.@)
2830 INT WINAPI GetWindowTextLengthA( HWND hwnd )
2832 return SendMessageA( hwnd, WM_GETTEXTLENGTH, 0, 0 );
2835 /*******************************************************************
2836 * GetWindowTextLengthW (USER32.@)
2838 INT WINAPI GetWindowTextLengthW( HWND hwnd )
2840 return SendMessageW( hwnd, WM_GETTEXTLENGTH, 0, 0 );
2844 /*******************************************************************
2845 * IsWindow (USER32.@)
2847 BOOL WINAPI IsWindow( HWND hwnd )
2849 WND *ptr;
2850 BOOL ret;
2852 if (!(ptr = WIN_GetPtr( hwnd ))) return FALSE;
2853 if (ptr == WND_DESKTOP) return TRUE;
2855 if (ptr != WND_OTHER_PROCESS)
2857 WIN_ReleasePtr( ptr );
2858 return TRUE;
2861 /* check other processes */
2862 SERVER_START_REQ( get_window_info )
2864 req->handle = wine_server_user_handle( hwnd );
2865 ret = !wine_server_call_err( req );
2867 SERVER_END_REQ;
2868 return ret;
2872 /***********************************************************************
2873 * GetWindowThreadProcessId (USER32.@)
2875 DWORD WINAPI GetWindowThreadProcessId( HWND hwnd, LPDWORD process )
2877 WND *ptr;
2878 DWORD tid = 0;
2880 if (!(ptr = WIN_GetPtr( hwnd )))
2882 SetLastError( ERROR_INVALID_WINDOW_HANDLE);
2883 return 0;
2886 if (ptr != WND_OTHER_PROCESS && ptr != WND_DESKTOP)
2888 /* got a valid window */
2889 tid = ptr->tid;
2890 if (process) *process = GetCurrentProcessId();
2891 WIN_ReleasePtr( ptr );
2892 return tid;
2895 /* check other processes */
2896 SERVER_START_REQ( get_window_info )
2898 req->handle = wine_server_user_handle( hwnd );
2899 if (!wine_server_call_err( req ))
2901 tid = (DWORD)reply->tid;
2902 if (process) *process = (DWORD)reply->pid;
2905 SERVER_END_REQ;
2906 return tid;
2910 /*****************************************************************
2911 * GetParent (USER32.@)
2913 HWND WINAPI GetParent( HWND hwnd )
2915 WND *wndPtr;
2916 HWND retvalue = 0;
2918 if (!(wndPtr = WIN_GetPtr( hwnd )))
2920 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2921 return 0;
2923 if (wndPtr == WND_DESKTOP) return 0;
2924 if (wndPtr == WND_OTHER_PROCESS)
2926 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
2927 if (style & (WS_POPUP | WS_CHILD))
2929 SERVER_START_REQ( get_window_tree )
2931 req->handle = wine_server_user_handle( hwnd );
2932 if (!wine_server_call_err( req ))
2934 if (style & WS_POPUP) retvalue = wine_server_ptr_handle( reply->owner );
2935 else if (style & WS_CHILD) retvalue = wine_server_ptr_handle( reply->parent );
2938 SERVER_END_REQ;
2941 else
2943 if (wndPtr->dwStyle & WS_POPUP) retvalue = wndPtr->owner;
2944 else if (wndPtr->dwStyle & WS_CHILD) retvalue = wndPtr->parent;
2945 WIN_ReleasePtr( wndPtr );
2947 return retvalue;
2951 /*****************************************************************
2952 * GetAncestor (USER32.@)
2954 HWND WINAPI GetAncestor( HWND hwnd, UINT type )
2956 WND *win;
2957 HWND *list, ret = 0;
2959 switch(type)
2961 case GA_PARENT:
2962 if (!(win = WIN_GetPtr( hwnd )))
2964 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2965 return 0;
2967 if (win == WND_DESKTOP) return 0;
2968 if (win != WND_OTHER_PROCESS)
2970 ret = win->parent;
2971 WIN_ReleasePtr( win );
2973 else /* need to query the server */
2975 SERVER_START_REQ( get_window_tree )
2977 req->handle = wine_server_user_handle( hwnd );
2978 if (!wine_server_call_err( req )) ret = wine_server_ptr_handle( reply->parent );
2980 SERVER_END_REQ;
2982 break;
2984 case GA_ROOT:
2985 if (!(list = list_window_parents( hwnd ))) return 0;
2987 if (!list[0] || !list[1]) ret = WIN_GetFullHandle( hwnd ); /* top-level window */
2988 else
2990 int count = 2;
2991 while (list[count]) count++;
2992 ret = list[count - 2]; /* get the one before the desktop */
2994 HeapFree( GetProcessHeap(), 0, list );
2995 break;
2997 case GA_ROOTOWNER:
2998 if (is_desktop_window( hwnd )) return 0;
2999 ret = WIN_GetFullHandle( hwnd );
3000 for (;;)
3002 HWND parent = GetParent( ret );
3003 if (!parent) break;
3004 ret = parent;
3006 break;
3008 return ret;
3012 /*****************************************************************
3013 * SetParent (USER32.@)
3015 HWND WINAPI SetParent( HWND hwnd, HWND parent )
3017 HWND full_handle;
3018 HWND old_parent = 0;
3019 BOOL was_visible;
3020 WND *wndPtr;
3021 POINT pt;
3022 BOOL ret;
3024 TRACE("(%p %p)\n", hwnd, parent);
3026 if (is_broadcast(hwnd) || is_broadcast(parent))
3028 SetLastError(ERROR_INVALID_PARAMETER);
3029 return 0;
3032 if (!parent) parent = GetDesktopWindow();
3033 else if (parent == HWND_MESSAGE) parent = get_hwnd_message_parent();
3034 else parent = WIN_GetFullHandle( parent );
3036 if (!IsWindow( parent ))
3038 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3039 return 0;
3042 /* Some applications try to set a child as a parent */
3043 if (IsChild(hwnd, parent))
3045 SetLastError( ERROR_INVALID_PARAMETER );
3046 return 0;
3049 if (!(full_handle = WIN_IsCurrentThread( hwnd )))
3050 return (HWND)SendMessageW( hwnd, WM_WINE_SETPARENT, (WPARAM)parent, 0 );
3052 if (full_handle == parent)
3054 SetLastError( ERROR_INVALID_PARAMETER );
3055 return 0;
3058 /* Windows hides the window first, then shows it again
3059 * including the WM_SHOWWINDOW messages and all */
3060 was_visible = ShowWindow( hwnd, SW_HIDE );
3062 wndPtr = WIN_GetPtr( hwnd );
3063 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return 0;
3065 pt.x = wndPtr->rectWindow.left;
3066 pt.y = wndPtr->rectWindow.top;
3068 SERVER_START_REQ( set_parent )
3070 req->handle = wine_server_user_handle( hwnd );
3071 req->parent = wine_server_user_handle( parent );
3072 if ((ret = !wine_server_call( req )))
3074 old_parent = wine_server_ptr_handle( reply->old_parent );
3075 wndPtr->parent = parent = wine_server_ptr_handle( reply->full_parent );
3079 SERVER_END_REQ;
3080 WIN_ReleasePtr( wndPtr );
3081 if (!ret) return 0;
3083 USER_Driver->pSetParent( full_handle, parent, old_parent );
3085 /* SetParent additionally needs to make hwnd the topmost window
3086 in the x-order and send the expected WM_WINDOWPOSCHANGING and
3087 WM_WINDOWPOSCHANGED notification messages.
3089 SetWindowPos( hwnd, HWND_TOP, pt.x, pt.y, 0, 0, SWP_NOSIZE );
3091 if (was_visible) ShowWindow( hwnd, SW_SHOW );
3093 return old_parent;
3097 /*******************************************************************
3098 * IsChild (USER32.@)
3100 BOOL WINAPI IsChild( HWND parent, HWND child )
3102 HWND *list;
3103 int i;
3104 BOOL ret = FALSE;
3106 if (!(GetWindowLongW( child, GWL_STYLE ) & WS_CHILD)) return FALSE;
3107 if (!(list = list_window_parents( child ))) return FALSE;
3108 parent = WIN_GetFullHandle( parent );
3109 for (i = 0; list[i]; i++)
3111 if (list[i] == parent)
3113 ret = list[i] && list[i+1];
3114 break;
3116 if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_CHILD)) break;
3118 HeapFree( GetProcessHeap(), 0, list );
3119 return ret;
3123 /***********************************************************************
3124 * IsWindowVisible (USER32.@)
3126 BOOL WINAPI IsWindowVisible( HWND hwnd )
3128 HWND *list;
3129 BOOL retval = TRUE;
3130 int i;
3132 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)) 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)) break;
3138 retval = !list[i+1] && (list[i] == GetDesktopWindow()); /* top message window isn't visible */
3140 HeapFree( GetProcessHeap(), 0, list );
3141 return retval;
3145 /***********************************************************************
3146 * WIN_IsWindowDrawable
3148 * hwnd is drawable when it is visible, all parents are not
3149 * minimized, and it is itself not minimized unless we are
3150 * trying to draw its default class icon.
3152 BOOL WIN_IsWindowDrawable( HWND hwnd, BOOL icon )
3154 HWND *list;
3155 BOOL retval = TRUE;
3156 int i;
3157 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
3159 if (!(style & WS_VISIBLE)) return FALSE;
3160 if ((style & WS_MINIMIZE) && icon && GetClassLongPtrW( hwnd, GCLP_HICON )) return FALSE;
3162 if (!(list = list_window_parents( hwnd ))) return TRUE;
3163 if (list[0])
3165 for (i = 0; list[i+1]; i++)
3166 if ((GetWindowLongW( list[i], GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != WS_VISIBLE)
3167 break;
3168 retval = !list[i+1] && (list[i] == GetDesktopWindow()); /* top message window isn't visible */
3170 HeapFree( GetProcessHeap(), 0, list );
3171 return retval;
3175 /*******************************************************************
3176 * GetTopWindow (USER32.@)
3178 HWND WINAPI GetTopWindow( HWND hwnd )
3180 if (!hwnd) hwnd = GetDesktopWindow();
3181 return GetWindow( hwnd, GW_CHILD );
3185 /*******************************************************************
3186 * GetWindow (USER32.@)
3188 HWND WINAPI GetWindow( HWND hwnd, UINT rel )
3190 HWND retval = 0;
3192 if (rel == GW_OWNER) /* this one may be available locally */
3194 WND *wndPtr = WIN_GetPtr( hwnd );
3195 if (!wndPtr)
3197 SetLastError( ERROR_INVALID_HANDLE );
3198 return 0;
3200 if (wndPtr == WND_DESKTOP) return 0;
3201 if (wndPtr != WND_OTHER_PROCESS)
3203 retval = wndPtr->owner;
3204 WIN_ReleasePtr( wndPtr );
3205 return retval;
3207 /* else fall through to server call */
3210 SERVER_START_REQ( get_window_tree )
3212 req->handle = wine_server_user_handle( hwnd );
3213 if (!wine_server_call_err( req ))
3215 switch(rel)
3217 case GW_HWNDFIRST:
3218 retval = wine_server_ptr_handle( reply->first_sibling );
3219 break;
3220 case GW_HWNDLAST:
3221 retval = wine_server_ptr_handle( reply->last_sibling );
3222 break;
3223 case GW_HWNDNEXT:
3224 retval = wine_server_ptr_handle( reply->next_sibling );
3225 break;
3226 case GW_HWNDPREV:
3227 retval = wine_server_ptr_handle( reply->prev_sibling );
3228 break;
3229 case GW_OWNER:
3230 retval = wine_server_ptr_handle( reply->owner );
3231 break;
3232 case GW_CHILD:
3233 retval = wine_server_ptr_handle( reply->first_child );
3234 break;
3238 SERVER_END_REQ;
3239 return retval;
3243 /*******************************************************************
3244 * ShowOwnedPopups (USER32.@)
3246 BOOL WINAPI ShowOwnedPopups( HWND owner, BOOL fShow )
3248 int count = 0;
3249 HWND *win_array = WIN_ListChildren( GetDesktopWindow() );
3251 if (!win_array) return TRUE;
3253 while (win_array[count]) count++;
3254 while (--count >= 0)
3256 if (GetWindow( win_array[count], GW_OWNER ) != owner) continue;
3257 if (fShow)
3259 if (win_get_flags( win_array[count] ) & WIN_NEEDS_SHOW_OWNEDPOPUP)
3260 /* In Windows, ShowOwnedPopups(TRUE) generates
3261 * WM_SHOWWINDOW messages with SW_PARENTOPENING,
3262 * regardless of the state of the owner
3264 SendMessageW(win_array[count], WM_SHOWWINDOW, SW_SHOWNORMAL, SW_PARENTOPENING);
3266 else
3268 if (GetWindowLongW( win_array[count], GWL_STYLE ) & WS_VISIBLE)
3269 /* In Windows, ShowOwnedPopups(FALSE) generates
3270 * WM_SHOWWINDOW messages with SW_PARENTCLOSING,
3271 * regardless of the state of the owner
3273 SendMessageW(win_array[count], WM_SHOWWINDOW, SW_HIDE, SW_PARENTCLOSING);
3276 HeapFree( GetProcessHeap(), 0, win_array );
3277 return TRUE;
3281 /*******************************************************************
3282 * GetLastActivePopup (USER32.@)
3284 HWND WINAPI GetLastActivePopup( HWND hwnd )
3286 HWND retval = hwnd;
3288 SERVER_START_REQ( get_window_info )
3290 req->handle = wine_server_user_handle( hwnd );
3291 if (!wine_server_call_err( req )) retval = wine_server_ptr_handle( reply->last_active );
3293 SERVER_END_REQ;
3294 return retval;
3298 /*******************************************************************
3299 * WIN_ListChildren
3301 * Build an array of the children of a given window. The array must be
3302 * freed with HeapFree. Returns NULL when no windows are found.
3304 HWND *WIN_ListChildren( HWND hwnd )
3306 if (!hwnd)
3308 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3309 return NULL;
3311 return list_window_children( 0, hwnd, NULL, 0 );
3315 /*******************************************************************
3316 * EnumWindows (USER32.@)
3318 BOOL WINAPI EnumWindows( WNDENUMPROC lpEnumFunc, LPARAM lParam )
3320 HWND *list;
3321 BOOL ret = TRUE;
3322 int i;
3324 USER_CheckNotLock();
3326 /* We have to build a list of all windows first, to avoid */
3327 /* unpleasant side-effects, for instance if the callback */
3328 /* function changes the Z-order of the windows. */
3330 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return TRUE;
3332 /* Now call the callback function for every window */
3334 for (i = 0; list[i]; i++)
3336 /* Make sure that the window still exists */
3337 if (!IsWindow( list[i] )) continue;
3338 if (!(ret = lpEnumFunc( list[i], lParam ))) break;
3340 HeapFree( GetProcessHeap(), 0, list );
3341 return ret;
3345 /**********************************************************************
3346 * EnumThreadWindows (USER32.@)
3348 BOOL WINAPI EnumThreadWindows( DWORD id, WNDENUMPROC func, LPARAM lParam )
3350 HWND *list;
3351 int i;
3352 BOOL ret = TRUE;
3354 USER_CheckNotLock();
3356 if (!(list = list_window_children( 0, GetDesktopWindow(), NULL, id ))) return TRUE;
3358 /* Now call the callback function for every window */
3360 for (i = 0; list[i]; i++)
3361 if (!(ret = func( list[i], lParam ))) break;
3362 HeapFree( GetProcessHeap(), 0, list );
3363 return ret;
3367 /***********************************************************************
3368 * EnumDesktopWindows (USER32.@)
3370 BOOL WINAPI EnumDesktopWindows( HDESK desktop, WNDENUMPROC func, LPARAM lparam )
3372 HWND *list;
3373 int i;
3375 USER_CheckNotLock();
3377 if (!(list = list_window_children( desktop, 0, NULL, 0 ))) return TRUE;
3379 for (i = 0; list[i]; i++)
3380 if (!func( list[i], lparam )) break;
3381 HeapFree( GetProcessHeap(), 0, list );
3382 return TRUE;
3386 #ifdef __i386__
3387 /* Some apps pass a non-stdcall proc to EnumChildWindows,
3388 * so we need a small assembly wrapper to call the proc.
3390 extern LRESULT enum_callback_wrapper( WNDENUMPROC proc, HWND hwnd, LPARAM lparam );
3391 __ASM_GLOBAL_FUNC( enum_callback_wrapper,
3392 "pushl %ebp\n\t"
3393 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
3394 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
3395 "movl %esp,%ebp\n\t"
3396 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
3397 "pushl 16(%ebp)\n\t"
3398 "pushl 12(%ebp)\n\t"
3399 "call *8(%ebp)\n\t"
3400 "leave\n\t"
3401 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
3402 __ASM_CFI(".cfi_same_value %ebp\n\t")
3403 "ret" )
3404 #else
3405 static inline LRESULT enum_callback_wrapper( WNDENUMPROC proc, HWND hwnd, LPARAM lparam )
3407 return proc( hwnd, lparam );
3409 #endif /* __i386__ */
3411 /**********************************************************************
3412 * WIN_EnumChildWindows
3414 * Helper function for EnumChildWindows().
3416 static BOOL WIN_EnumChildWindows( HWND *list, WNDENUMPROC func, LPARAM lParam )
3418 HWND *childList;
3419 BOOL ret = FALSE;
3421 for ( ; *list; list++)
3423 /* Make sure that the window still exists */
3424 if (!IsWindow( *list )) continue;
3425 /* Build children list first */
3426 childList = WIN_ListChildren( *list );
3428 ret = enum_callback_wrapper( func, *list, lParam );
3430 if (childList)
3432 if (ret) ret = WIN_EnumChildWindows( childList, func, lParam );
3433 HeapFree( GetProcessHeap(), 0, childList );
3435 if (!ret) return FALSE;
3437 return TRUE;
3441 /**********************************************************************
3442 * EnumChildWindows (USER32.@)
3444 BOOL WINAPI EnumChildWindows( HWND parent, WNDENUMPROC func, LPARAM lParam )
3446 HWND *list;
3447 BOOL ret;
3449 USER_CheckNotLock();
3451 if (!(list = WIN_ListChildren( parent ))) return FALSE;
3452 ret = WIN_EnumChildWindows( list, func, lParam );
3453 HeapFree( GetProcessHeap(), 0, list );
3454 return ret;
3458 /*******************************************************************
3459 * AnyPopup (USER32.@)
3461 BOOL WINAPI AnyPopup(void)
3463 int i;
3464 BOOL retvalue;
3465 HWND *list = WIN_ListChildren( GetDesktopWindow() );
3467 if (!list) return FALSE;
3468 for (i = 0; list[i]; i++)
3470 if (IsWindowVisible( list[i] ) && GetWindow( list[i], GW_OWNER )) break;
3472 retvalue = (list[i] != 0);
3473 HeapFree( GetProcessHeap(), 0, list );
3474 return retvalue;
3478 /*******************************************************************
3479 * FlashWindow (USER32.@)
3481 BOOL WINAPI FlashWindow( HWND hWnd, BOOL bInvert )
3483 FLASHWINFO finfo;
3485 finfo.cbSize = sizeof(FLASHWINFO);
3486 finfo.dwFlags = bInvert ? FLASHW_ALL : FLASHW_STOP;
3487 finfo.uCount = 1;
3488 finfo.dwTimeout = 0;
3489 finfo.hwnd = hWnd;
3490 return FlashWindowEx( &finfo );
3493 /*******************************************************************
3494 * FlashWindowEx (USER32.@)
3496 BOOL WINAPI FlashWindowEx( PFLASHWINFO pfinfo )
3498 WND *wndPtr;
3500 TRACE( "%p\n", pfinfo );
3502 if (!pfinfo)
3504 SetLastError( ERROR_NOACCESS );
3505 return FALSE;
3508 if (!pfinfo->hwnd || pfinfo->cbSize != sizeof(FLASHWINFO) || !IsWindow( pfinfo->hwnd ))
3510 SetLastError( ERROR_INVALID_PARAMETER );
3511 return FALSE;
3513 FIXME( "%p - semi-stub\n", pfinfo );
3515 if (IsIconic( pfinfo->hwnd ))
3517 RedrawWindow( pfinfo->hwnd, 0, 0, RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW | RDW_FRAME );
3519 wndPtr = WIN_GetPtr( pfinfo->hwnd );
3520 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
3521 if (pfinfo->dwFlags && !(wndPtr->flags & WIN_NCACTIVATED))
3523 wndPtr->flags |= WIN_NCACTIVATED;
3525 else
3527 wndPtr->flags &= ~WIN_NCACTIVATED;
3529 WIN_ReleasePtr( wndPtr );
3530 USER_Driver->pFlashWindowEx( pfinfo );
3531 return TRUE;
3533 else
3535 WPARAM wparam;
3536 HWND hwnd = pfinfo->hwnd;
3538 wndPtr = WIN_GetPtr( hwnd );
3539 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
3540 hwnd = wndPtr->obj.handle; /* make it a full handle */
3542 if (pfinfo->dwFlags) wparam = !(wndPtr->flags & WIN_NCACTIVATED);
3543 else wparam = (hwnd == GetForegroundWindow());
3545 WIN_ReleasePtr( wndPtr );
3546 SendMessageW( hwnd, WM_NCACTIVATE, wparam, 0 );
3547 USER_Driver->pFlashWindowEx( pfinfo );
3548 return wparam;
3552 /*******************************************************************
3553 * GetWindowContextHelpId (USER32.@)
3555 DWORD WINAPI GetWindowContextHelpId( HWND hwnd )
3557 DWORD retval;
3558 WND *wnd = WIN_GetPtr( hwnd );
3559 if (!wnd || wnd == WND_DESKTOP) return 0;
3560 if (wnd == WND_OTHER_PROCESS)
3562 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
3563 return 0;
3565 retval = wnd->helpContext;
3566 WIN_ReleasePtr( wnd );
3567 return retval;
3571 /*******************************************************************
3572 * SetWindowContextHelpId (USER32.@)
3574 BOOL WINAPI SetWindowContextHelpId( HWND hwnd, DWORD id )
3576 WND *wnd = WIN_GetPtr( hwnd );
3577 if (!wnd || wnd == WND_DESKTOP) return FALSE;
3578 if (wnd == WND_OTHER_PROCESS)
3580 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
3581 return FALSE;
3583 wnd->helpContext = id;
3584 WIN_ReleasePtr( wnd );
3585 return TRUE;
3589 /*******************************************************************
3590 * DragDetect (USER32.@)
3592 BOOL WINAPI DragDetect( HWND hWnd, POINT pt )
3594 MSG msg;
3595 RECT rect;
3596 WORD wDragWidth = GetSystemMetrics(SM_CXDRAG);
3597 WORD wDragHeight= GetSystemMetrics(SM_CYDRAG);
3599 SetRect(&rect, pt.x - wDragWidth, pt.y - wDragHeight, pt.x + wDragWidth, pt.y + wDragHeight);
3601 SetCapture(hWnd);
3603 while(1)
3605 while (PeekMessageW( &msg, 0, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE ))
3607 if( msg.message == WM_LBUTTONUP )
3609 ReleaseCapture();
3610 return FALSE;
3612 if( msg.message == WM_MOUSEMOVE )
3614 POINT tmp;
3615 tmp.x = (short)LOWORD(msg.lParam);
3616 tmp.y = (short)HIWORD(msg.lParam);
3617 if( !PtInRect( &rect, tmp ))
3619 ReleaseCapture();
3620 return TRUE;
3624 WaitMessage();
3626 return FALSE;
3629 /******************************************************************************
3630 * GetWindowModuleFileNameA (USER32.@)
3632 UINT WINAPI GetWindowModuleFileNameA( HWND hwnd, LPSTR module, UINT size )
3634 WND *win;
3635 HINSTANCE hinst;
3637 TRACE( "%p, %p, %u\n", hwnd, module, size );
3639 win = WIN_GetPtr( hwnd );
3640 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP)
3642 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3643 return 0;
3645 hinst = win->hInstance;
3646 WIN_ReleasePtr( win );
3648 return GetModuleFileNameA( hinst, module, size );
3651 /******************************************************************************
3652 * GetWindowModuleFileNameW (USER32.@)
3654 UINT WINAPI GetWindowModuleFileNameW( HWND hwnd, LPWSTR module, UINT size )
3656 WND *win;
3657 HINSTANCE hinst;
3659 TRACE( "%p, %p, %u\n", hwnd, module, size );
3661 win = WIN_GetPtr( hwnd );
3662 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP)
3664 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3665 return 0;
3667 hinst = win->hInstance;
3668 WIN_ReleasePtr( win );
3670 return GetModuleFileNameW( hinst, module, size );
3673 /******************************************************************************
3674 * GetWindowInfo (USER32.@)
3676 * Note: tests show that Windows doesn't check cbSize of the structure.
3678 BOOL WINAPI DECLSPEC_HOTPATCH GetWindowInfo( HWND hwnd, PWINDOWINFO pwi)
3680 if (!pwi) return FALSE;
3681 if (!WIN_GetRectangles( hwnd, COORDS_SCREEN, &pwi->rcWindow, &pwi->rcClient )) return FALSE;
3683 pwi->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
3684 pwi->dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
3685 pwi->dwWindowStatus = ((GetActiveWindow() == hwnd) ? WS_ACTIVECAPTION : 0);
3687 pwi->cxWindowBorders = pwi->rcClient.left - pwi->rcWindow.left;
3688 pwi->cyWindowBorders = pwi->rcWindow.bottom - pwi->rcClient.bottom;
3690 pwi->atomWindowType = GetClassLongW( hwnd, GCW_ATOM );
3691 pwi->wCreatorVersion = 0x0400;
3693 return TRUE;
3696 /******************************************************************************
3697 * SwitchDesktop (USER32.@)
3699 * NOTES: Sets the current input or interactive desktop.
3701 BOOL WINAPI SwitchDesktop( HDESK hDesktop)
3703 FIXME("(hwnd %p) stub!\n", hDesktop);
3704 return TRUE;
3708 /***********************************************************************
3709 * __wine_set_pixel_format
3711 BOOL CDECL __wine_set_pixel_format( HWND hwnd, int format )
3713 WND *win = WIN_GetPtr( hwnd );
3715 if (!win || win == WND_DESKTOP || win == WND_OTHER_PROCESS)
3717 WARN( "setting format %d on win %p not supported\n", format, hwnd );
3718 return FALSE;
3720 win->pixel_format = format;
3721 WIN_ReleasePtr( win );
3723 update_window_state( hwnd );
3724 return TRUE;
3728 /*****************************************************************************
3729 * SetLayeredWindowAttributes (USER32.@)
3731 BOOL WINAPI SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
3733 BOOL ret;
3735 TRACE("(%p,%08x,%d,%x)\n", hwnd, key, alpha, flags);
3737 SERVER_START_REQ( set_window_layered_info )
3739 req->handle = wine_server_user_handle( hwnd );
3740 req->color_key = key;
3741 req->alpha = alpha;
3742 req->flags = flags;
3743 ret = !wine_server_call_err( req );
3745 SERVER_END_REQ;
3747 if (ret)
3749 USER_Driver->pSetLayeredWindowAttributes( hwnd, key, alpha, flags );
3750 update_window_state( hwnd );
3753 return ret;
3757 /*****************************************************************************
3758 * GetLayeredWindowAttributes (USER32.@)
3760 BOOL WINAPI GetLayeredWindowAttributes( HWND hwnd, COLORREF *key, BYTE *alpha, DWORD *flags )
3762 BOOL ret;
3764 SERVER_START_REQ( get_window_layered_info )
3766 req->handle = wine_server_user_handle( hwnd );
3767 if ((ret = !wine_server_call_err( req )))
3769 if (key) *key = reply->color_key;
3770 if (alpha) *alpha = reply->alpha;
3771 if (flags) *flags = reply->flags;
3774 SERVER_END_REQ;
3776 return ret;
3780 /*****************************************************************************
3781 * UpdateLayeredWindowIndirect (USER32.@)
3783 BOOL WINAPI UpdateLayeredWindowIndirect( HWND hwnd, const UPDATELAYEREDWINDOWINFO *info )
3785 DWORD flags = SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW;
3786 RECT window_rect, client_rect;
3787 SIZE offset;
3789 if (!info ||
3790 info->cbSize != sizeof(*info) ||
3791 info->dwFlags & ~(ULW_COLORKEY | ULW_ALPHA | ULW_OPAQUE | ULW_EX_NORESIZE) ||
3792 !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED) ||
3793 GetLayeredWindowAttributes( hwnd, NULL, NULL, NULL ))
3795 SetLastError( ERROR_INVALID_PARAMETER );
3796 return FALSE;
3799 WIN_GetRectangles( hwnd, COORDS_PARENT, &window_rect, &client_rect );
3801 if (info->pptDst)
3803 offset.cx = info->pptDst->x - window_rect.left;
3804 offset.cy = info->pptDst->y - window_rect.top;
3805 OffsetRect( &client_rect, offset.cx, offset.cy );
3806 OffsetRect( &window_rect, offset.cx, offset.cy );
3807 flags &= ~SWP_NOMOVE;
3809 if (info->psize)
3811 offset.cx = info->psize->cx - (window_rect.right - window_rect.left);
3812 offset.cy = info->psize->cy - (window_rect.bottom - window_rect.top);
3813 if (info->psize->cx <= 0 || info->psize->cy <= 0)
3815 SetLastError( ERROR_INVALID_PARAMETER );
3816 return FALSE;
3818 if ((info->dwFlags & ULW_EX_NORESIZE) && (offset.cx || offset.cy))
3820 SetLastError( ERROR_INCORRECT_SIZE );
3821 return FALSE;
3823 client_rect.right += offset.cx;
3824 client_rect.bottom += offset.cy;
3825 window_rect.right += offset.cx;
3826 window_rect.bottom += offset.cy;
3827 flags &= ~SWP_NOSIZE;
3830 TRACE( "window %p win %s client %s\n", hwnd,
3831 wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(&client_rect) );
3833 if (!USER_Driver->pUpdateLayeredWindow( hwnd, info, &window_rect )) return FALSE;
3835 set_window_pos( hwnd, 0, flags, &window_rect, &client_rect, NULL );
3836 return TRUE;
3840 /*****************************************************************************
3841 * UpdateLayeredWindow (USER32.@)
3843 BOOL WINAPI UpdateLayeredWindow( HWND hwnd, HDC hdcDst, POINT *pptDst, SIZE *psize,
3844 HDC hdcSrc, POINT *pptSrc, COLORREF crKey, BLENDFUNCTION *pblend,
3845 DWORD flags)
3847 UPDATELAYEREDWINDOWINFO info;
3849 if (flags & ULW_EX_NORESIZE) /* only valid for UpdateLayeredWindowIndirect */
3851 SetLastError( ERROR_INVALID_PARAMETER );
3852 return FALSE;
3854 info.cbSize = sizeof(info);
3855 info.hdcDst = hdcDst;
3856 info.pptDst = pptDst;
3857 info.psize = psize;
3858 info.hdcSrc = hdcSrc;
3859 info.pptSrc = pptSrc;
3860 info.crKey = crKey;
3861 info.pblend = pblend;
3862 info.dwFlags = flags;
3863 info.prcDirty = NULL;
3864 return UpdateLayeredWindowIndirect( hwnd, &info );
3868 /******************************************************************************
3869 * GetProcessDefaultLayout [USER32.@]
3871 * Gets the default layout for parentless windows.
3873 BOOL WINAPI GetProcessDefaultLayout( DWORD *layout )
3875 if (!layout)
3877 SetLastError( ERROR_NOACCESS );
3878 return FALSE;
3880 if (process_layout == ~0u)
3882 static const WCHAR translationW[] = { '\\','V','a','r','F','i','l','e','I','n','f','o',
3883 '\\','T','r','a','n','s','l','a','t','i','o','n', 0 };
3884 static const WCHAR filedescW[] = { '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
3885 '\\','%','0','4','x','%','0','4','x',
3886 '\\','F','i','l','e','D','e','s','c','r','i','p','t','i','o','n',0 };
3887 WCHAR *str, buffer[MAX_PATH];
3888 DWORD i, len, version_layout = 0;
3889 DWORD user_lang = GetUserDefaultLangID();
3890 DWORD *languages;
3891 void *data = NULL;
3893 GetModuleFileNameW( 0, buffer, MAX_PATH );
3894 if (!(len = GetFileVersionInfoSizeW( buffer, NULL ))) goto done;
3895 if (!(data = HeapAlloc( GetProcessHeap(), 0, len ))) goto done;
3896 if (!GetFileVersionInfoW( buffer, 0, len, data )) goto done;
3897 if (!VerQueryValueW( data, translationW, (void **)&languages, &len ) || !len) goto done;
3899 len /= sizeof(DWORD);
3900 for (i = 0; i < len; i++) if (LOWORD(languages[i]) == user_lang) break;
3901 if (i == len) /* try neutral language */
3902 for (i = 0; i < len; i++)
3903 if (LOWORD(languages[i]) == MAKELANGID( PRIMARYLANGID(user_lang), SUBLANG_NEUTRAL )) break;
3904 if (i == len) i = 0; /* default to the first one */
3906 sprintfW( buffer, filedescW, LOWORD(languages[i]), HIWORD(languages[i]) );
3907 if (!VerQueryValueW( data, buffer, (void **)&str, &len )) goto done;
3908 TRACE( "found description %s\n", debugstr_w( str ));
3909 if (str[0] == 0x200e && str[1] == 0x200e) version_layout = LAYOUT_RTL;
3911 done:
3912 HeapFree( GetProcessHeap(), 0, data );
3913 process_layout = version_layout;
3915 *layout = process_layout;
3916 return TRUE;
3920 /******************************************************************************
3921 * SetProcessDefaultLayout [USER32.@]
3923 * Sets the default layout for parentless windows.
3925 BOOL WINAPI SetProcessDefaultLayout( DWORD layout )
3927 process_layout = layout;
3928 return TRUE;
3932 /* 64bit versions */
3934 #ifdef GetWindowLongPtrW
3935 #undef GetWindowLongPtrW
3936 #endif
3938 #ifdef GetWindowLongPtrA
3939 #undef GetWindowLongPtrA
3940 #endif
3942 #ifdef SetWindowLongPtrW
3943 #undef SetWindowLongPtrW
3944 #endif
3946 #ifdef SetWindowLongPtrA
3947 #undef SetWindowLongPtrA
3948 #endif
3950 /*****************************************************************************
3951 * GetWindowLongPtrW (USER32.@)
3953 LONG_PTR WINAPI GetWindowLongPtrW( HWND hwnd, INT offset )
3955 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG_PTR), TRUE );
3958 /*****************************************************************************
3959 * GetWindowLongPtrA (USER32.@)
3961 LONG_PTR WINAPI GetWindowLongPtrA( HWND hwnd, INT offset )
3963 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG_PTR), FALSE );
3966 /*****************************************************************************
3967 * SetWindowLongPtrW (USER32.@)
3969 LONG_PTR WINAPI SetWindowLongPtrW( HWND hwnd, INT offset, LONG_PTR newval )
3971 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG_PTR), newval, TRUE );
3974 /*****************************************************************************
3975 * SetWindowLongPtrA (USER32.@)
3977 LONG_PTR WINAPI SetWindowLongPtrA( HWND hwnd, INT offset, LONG_PTR newval )
3979 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG_PTR), newval, FALSE );
3982 /*****************************************************************************
3983 * RegisterTouchWindow (USER32.@)
3985 BOOL WINAPI RegisterTouchWindow(HWND hwnd, ULONG flags)
3987 FIXME("(%p %08x): stub\n", hwnd, flags);
3988 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
3989 return FALSE;
3992 /*****************************************************************************
3993 * UnregisterTouchWindow (USER32.@)
3995 BOOL WINAPI UnregisterTouchWindow(HWND hwnd)
3997 FIXME("(%p): stub\n", hwnd);
3998 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
3999 return FALSE;
4002 /*****************************************************************************
4003 * CloseTouchInputHandle (USER32.@)
4005 BOOL WINAPI CloseTouchInputHandle(HTOUCHINPUT handle)
4007 FIXME("(%p): stub\n", handle);
4008 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
4009 return FALSE;
4012 /*****************************************************************************
4013 * GetTouchInputInfo (USER32.@)
4015 BOOL WINAPI GetTouchInputInfo(HTOUCHINPUT handle, UINT count, TOUCHINPUT *ptr, int size)
4017 FIXME("(%p %u %p %u): stub\n", handle, count, ptr, size);
4018 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
4019 return FALSE;
4022 /*****************************************************************************
4023 * GetGestureInfo (USER32.@)
4025 BOOL WINAPI GetGestureInfo(HGESTUREINFO handle, PGESTUREINFO ptr)
4027 FIXME("(%p %p): stub\n", handle, ptr);
4028 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
4029 return FALSE;
4032 /*****************************************************************************
4033 * GetWindowDisplayAffinity (USER32.@)
4035 BOOL WINAPI GetWindowDisplayAffinity(HWND hwnd, DWORD *affinity)
4037 FIXME("(%p, %p): stub\n", hwnd, affinity);
4039 if (!hwnd || !affinity)
4041 SetLastError(hwnd ? ERROR_NOACCESS : ERROR_INVALID_WINDOW_HANDLE);
4042 return FALSE;
4045 *affinity = WDA_NONE;
4046 return TRUE;
4049 /*****************************************************************************
4050 * SetWindowDisplayAffinity (USER32.@)
4052 BOOL WINAPI SetWindowDisplayAffinity(HWND hwnd, DWORD affinity)
4054 FIXME("(%p, %u): stub\n", hwnd, affinity);
4056 if (!hwnd)
4058 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
4059 return FALSE;
4062 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
4063 return FALSE;