mstask: Improve stubs for ITask::GetTaskFlags and ITask::GetFlags.
[wine.git] / dlls / user32 / win.c
blob3c497f878e93ebf18a48fb7646de22d3d2eb12b3
1 /*
2 * Window related functions
4 * Copyright 1993, 1994 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winver.h"
32 #include "wine/server.h"
33 #include "wine/unicode.h"
34 #include "win.h"
35 #include "user_private.h"
36 #include "controls.h"
37 #include "winerror.h"
38 #include "wine/gdi_driver.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(win);
43 #define NB_USER_HANDLES ((LAST_USER_HANDLE - FIRST_USER_HANDLE + 1) >> 1)
44 #define USER_HANDLE_TO_INDEX(hwnd) ((LOWORD(hwnd) - FIRST_USER_HANDLE) >> 1)
46 static DWORD process_layout = ~0u;
48 static struct list window_surfaces = LIST_INIT( window_surfaces );
50 static CRITICAL_SECTION surfaces_section;
51 static CRITICAL_SECTION_DEBUG critsect_debug =
53 0, 0, &surfaces_section,
54 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
55 0, 0, { (DWORD_PTR)(__FILE__ ": surfaces_section") }
57 static CRITICAL_SECTION surfaces_section = { &critsect_debug, -1, 0, 0, 0, 0 };
59 /**********************************************************************/
61 /* helper for Get/SetWindowLong */
62 static inline LONG_PTR get_win_data( const void *ptr, UINT size )
64 if (size == sizeof(WORD))
66 WORD ret;
67 memcpy( &ret, ptr, sizeof(ret) );
68 return ret;
70 else if (size == sizeof(DWORD))
72 DWORD ret;
73 memcpy( &ret, ptr, sizeof(ret) );
74 return ret;
76 else
78 LONG_PTR ret;
79 memcpy( &ret, ptr, sizeof(ret) );
80 return ret;
84 /* helper for Get/SetWindowLong */
85 static inline void set_win_data( void *ptr, LONG_PTR val, UINT size )
87 if (size == sizeof(WORD))
89 WORD newval = val;
90 memcpy( ptr, &newval, sizeof(newval) );
92 else if (size == sizeof(DWORD))
94 DWORD newval = val;
95 memcpy( ptr, &newval, sizeof(newval) );
97 else
99 memcpy( ptr, &val, sizeof(val) );
104 static void *user_handles[NB_USER_HANDLES];
106 /***********************************************************************
107 * alloc_user_handle
109 HANDLE alloc_user_handle( struct user_object *ptr, enum user_obj_type type )
111 HANDLE handle = 0;
113 SERVER_START_REQ( alloc_user_handle )
115 if (!wine_server_call_err( req )) handle = wine_server_ptr_handle( reply->handle );
117 SERVER_END_REQ;
119 if (handle)
121 UINT index = USER_HANDLE_TO_INDEX( handle );
123 assert( index < NB_USER_HANDLES );
124 ptr->handle = handle;
125 ptr->type = type;
126 InterlockedExchangePointer( &user_handles[index], ptr );
128 return handle;
132 /***********************************************************************
133 * get_user_handle_ptr
135 void *get_user_handle_ptr( HANDLE handle, enum user_obj_type type )
137 struct user_object *ptr;
138 WORD index = USER_HANDLE_TO_INDEX( handle );
140 if (index >= NB_USER_HANDLES) return NULL;
142 USER_Lock();
143 if ((ptr = user_handles[index]))
145 if (ptr->type == type &&
146 ((UINT)(UINT_PTR)ptr->handle == (UINT)(UINT_PTR)handle ||
147 !HIWORD(handle) || HIWORD(handle) == 0xffff))
148 return ptr;
149 ptr = NULL;
151 else ptr = OBJ_OTHER_PROCESS;
152 USER_Unlock();
153 return ptr;
157 /***********************************************************************
158 * release_user_handle_ptr
160 void release_user_handle_ptr( void *ptr )
162 assert( ptr && ptr != OBJ_OTHER_PROCESS );
163 USER_Unlock();
167 /***********************************************************************
168 * free_user_handle
170 void *free_user_handle( HANDLE handle, enum user_obj_type type )
172 struct user_object *ptr;
173 WORD index = USER_HANDLE_TO_INDEX( handle );
175 if ((ptr = get_user_handle_ptr( handle, type )) && ptr != OBJ_OTHER_PROCESS)
177 SERVER_START_REQ( free_user_handle )
179 req->handle = wine_server_user_handle( handle );
180 if (wine_server_call( req )) ptr = NULL;
181 else InterlockedCompareExchangePointer( &user_handles[index], NULL, ptr );
183 SERVER_END_REQ;
184 USER_Unlock();
186 return ptr;
190 /***********************************************************************
191 * create_window_handle
193 * Create a window handle with the server.
195 static WND *create_window_handle( HWND parent, HWND owner, LPCWSTR name,
196 HINSTANCE instance, BOOL unicode )
198 WORD index;
199 WND *win;
200 HWND handle = 0, full_parent = 0, full_owner = 0;
201 struct tagCLASS *class = NULL;
202 int extra_bytes = 0;
204 SERVER_START_REQ( create_window )
206 req->parent = wine_server_user_handle( parent );
207 req->owner = wine_server_user_handle( owner );
208 req->instance = wine_server_client_ptr( instance );
209 if (!(req->atom = get_int_atom_value( name )) && name)
210 wine_server_add_data( req, name, strlenW(name)*sizeof(WCHAR) );
211 if (!wine_server_call_err( req ))
213 handle = wine_server_ptr_handle( reply->handle );
214 full_parent = wine_server_ptr_handle( reply->parent );
215 full_owner = wine_server_ptr_handle( reply->owner );
216 extra_bytes = reply->extra;
217 class = wine_server_get_ptr( reply->class_ptr );
220 SERVER_END_REQ;
222 if (!handle)
224 WARN( "error %d creating window\n", GetLastError() );
225 return NULL;
228 if (!(win = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
229 sizeof(WND) + extra_bytes - sizeof(win->wExtra) )))
231 SERVER_START_REQ( destroy_window )
233 req->handle = wine_server_user_handle( handle );
234 wine_server_call( req );
236 SERVER_END_REQ;
237 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
238 return NULL;
241 if (!parent) /* if parent is 0 we don't have a desktop window yet */
243 struct user_thread_info *thread_info = get_user_thread_info();
245 if (name == (LPCWSTR)DESKTOP_CLASS_ATOM)
247 if (!thread_info->top_window) thread_info->top_window = full_parent ? full_parent : handle;
248 else assert( full_parent == thread_info->top_window );
249 if (full_parent && !USER_Driver->pCreateDesktopWindow( thread_info->top_window ))
250 ERR( "failed to create desktop window\n" );
252 else /* HWND_MESSAGE parent */
254 if (!thread_info->msg_window && !full_parent) thread_info->msg_window = handle;
258 USER_Lock();
260 index = USER_HANDLE_TO_INDEX(handle);
261 assert( index < NB_USER_HANDLES );
262 win->obj.handle = handle;
263 win->obj.type = USER_WINDOW;
264 win->parent = full_parent;
265 win->owner = full_owner;
266 win->class = class;
267 win->winproc = get_class_winproc( class );
268 win->cbWndExtra = extra_bytes;
269 InterlockedExchangePointer( &user_handles[index], win );
270 if (WINPROC_IsUnicode( win->winproc, unicode )) win->flags |= WIN_ISUNICODE;
271 return win;
275 /***********************************************************************
276 * free_window_handle
278 * Free a window handle.
280 static void free_window_handle( HWND hwnd )
282 struct user_object *ptr;
283 WORD index = USER_HANDLE_TO_INDEX(hwnd);
285 if ((ptr = get_user_handle_ptr( hwnd, USER_WINDOW )) && ptr != OBJ_OTHER_PROCESS)
287 SERVER_START_REQ( destroy_window )
289 req->handle = wine_server_user_handle( hwnd );
290 if (wine_server_call_err( req )) ptr = NULL;
291 else InterlockedCompareExchangePointer( &user_handles[index], NULL, ptr );
293 SERVER_END_REQ;
294 USER_Unlock();
295 HeapFree( GetProcessHeap(), 0, ptr );
300 /*******************************************************************
301 * list_window_children
303 * Build an array of the children of a given window. The array must be
304 * freed with HeapFree. Returns NULL when no windows are found.
306 static HWND *list_window_children( HDESK desktop, HWND hwnd, LPCWSTR class, DWORD tid )
308 HWND *list;
309 int i, size = 128;
310 ATOM atom = get_int_atom_value( class );
312 /* empty class is not the same as NULL class */
313 if (!atom && class && !class[0]) return NULL;
315 for (;;)
317 int count = 0;
319 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
321 SERVER_START_REQ( get_window_children )
323 req->desktop = wine_server_obj_handle( desktop );
324 req->parent = wine_server_user_handle( hwnd );
325 req->tid = tid;
326 req->atom = atom;
327 if (!atom && class) wine_server_add_data( req, class, strlenW(class)*sizeof(WCHAR) );
328 wine_server_set_reply( req, list, (size-1) * sizeof(user_handle_t) );
329 if (!wine_server_call( req )) count = reply->count;
331 SERVER_END_REQ;
332 if (count && count < size)
334 /* start from the end since HWND is potentially larger than user_handle_t */
335 for (i = count - 1; i >= 0; i--)
336 list[i] = wine_server_ptr_handle( ((user_handle_t *)list)[i] );
337 list[count] = 0;
338 return list;
340 HeapFree( GetProcessHeap(), 0, list );
341 if (!count) break;
342 size = count + 1; /* restart with a large enough buffer */
344 return NULL;
348 /*******************************************************************
349 * list_window_parents
351 * Build an array of all parents of a given window, starting with
352 * the immediate parent. The array must be freed with HeapFree.
354 static HWND *list_window_parents( HWND hwnd )
356 WND *win;
357 HWND current, *list;
358 int i, pos = 0, size = 16, count;
360 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) return NULL;
362 current = hwnd;
363 for (;;)
365 if (!(win = WIN_GetPtr( current ))) goto empty;
366 if (win == WND_OTHER_PROCESS) break; /* need to do it the hard way */
367 if (win == WND_DESKTOP)
369 if (!pos) goto empty;
370 list[pos] = 0;
371 return list;
373 list[pos] = current = win->parent;
374 WIN_ReleasePtr( win );
375 if (!current) return list;
376 if (++pos == size - 1)
378 /* need to grow the list */
379 HWND *new_list = HeapReAlloc( GetProcessHeap(), 0, list, (size+16) * sizeof(HWND) );
380 if (!new_list) goto empty;
381 list = new_list;
382 size += 16;
386 /* at least one parent belongs to another process, have to query the server */
388 for (;;)
390 count = 0;
391 SERVER_START_REQ( get_window_parents )
393 req->handle = wine_server_user_handle( hwnd );
394 wine_server_set_reply( req, list, (size-1) * sizeof(user_handle_t) );
395 if (!wine_server_call( req )) count = reply->count;
397 SERVER_END_REQ;
398 if (!count) goto empty;
399 if (size > count)
401 /* start from the end since HWND is potentially larger than user_handle_t */
402 for (i = count - 1; i >= 0; i--)
403 list[i] = wine_server_ptr_handle( ((user_handle_t *)list)[i] );
404 list[count] = 0;
405 return list;
407 HeapFree( GetProcessHeap(), 0, list );
408 size = count + 1;
409 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) return NULL;
412 empty:
413 HeapFree( GetProcessHeap(), 0, list );
414 return NULL;
418 /*******************************************************************
419 * send_parent_notify
421 static void send_parent_notify( HWND hwnd, UINT msg )
423 if ((GetWindowLongW( hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD &&
424 !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY))
426 HWND parent = GetParent(hwnd);
427 if (parent && parent != GetDesktopWindow())
428 SendMessageW( parent, WM_PARENTNOTIFY,
429 MAKEWPARAM( msg, GetWindowLongPtrW( hwnd, GWLP_ID )), (LPARAM)hwnd );
434 /*******************************************************************
435 * update_window_state
437 * Trigger an update of the window's driver state and surface.
439 static void update_window_state( HWND hwnd )
441 RECT window_rect, client_rect, valid_rects[2];
443 WIN_GetRectangles( hwnd, COORDS_PARENT, &window_rect, &client_rect );
444 valid_rects[0] = valid_rects[1] = client_rect;
445 set_window_pos( hwnd, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE |
446 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW,
447 &window_rect, &client_rect, valid_rects );
451 /*******************************************************************
452 * get_server_window_text
454 * Retrieve the window text from the server.
456 static void get_server_window_text( HWND hwnd, LPWSTR text, INT count )
458 size_t len = 0;
460 SERVER_START_REQ( get_window_text )
462 req->handle = wine_server_user_handle( hwnd );
463 wine_server_set_reply( req, text, (count - 1) * sizeof(WCHAR) );
464 if (!wine_server_call_err( req )) len = wine_server_reply_size(reply);
466 SERVER_END_REQ;
467 text[len / sizeof(WCHAR)] = 0;
471 /*******************************************************************
472 * get_hwnd_message_parent
474 * Return the parent for HWND_MESSAGE windows.
476 HWND get_hwnd_message_parent(void)
478 struct user_thread_info *thread_info = get_user_thread_info();
480 if (!thread_info->msg_window) GetDesktopWindow(); /* trigger creation */
481 return thread_info->msg_window;
485 /*******************************************************************
486 * is_desktop_window
488 * Check if window is the desktop or the HWND_MESSAGE top parent.
490 BOOL is_desktop_window( HWND hwnd )
492 struct user_thread_info *thread_info = get_user_thread_info();
494 if (!hwnd) return FALSE;
495 if (hwnd == thread_info->top_window) return TRUE;
496 if (hwnd == thread_info->msg_window) return TRUE;
498 if (!HIWORD(hwnd) || HIWORD(hwnd) == 0xffff)
500 if (LOWORD(thread_info->top_window) == LOWORD(hwnd)) return TRUE;
501 if (LOWORD(thread_info->msg_window) == LOWORD(hwnd)) return TRUE;
503 return FALSE;
507 /*******************************************************************
508 * Dummy window surface for windows that shouldn't get painted.
511 static void dummy_surface_lock( struct window_surface *window_surface )
513 /* nothing to do */
516 static void dummy_surface_unlock( struct window_surface *window_surface )
518 /* nothing to do */
521 static void *dummy_surface_get_bitmap_info( struct window_surface *window_surface, BITMAPINFO *info )
523 static DWORD dummy_data;
525 info->bmiHeader.biSize = sizeof( info->bmiHeader );
526 info->bmiHeader.biWidth = dummy_surface.rect.right;
527 info->bmiHeader.biHeight = dummy_surface.rect.bottom;
528 info->bmiHeader.biPlanes = 1;
529 info->bmiHeader.biBitCount = 32;
530 info->bmiHeader.biCompression = BI_RGB;
531 info->bmiHeader.biSizeImage = 0;
532 info->bmiHeader.biXPelsPerMeter = 0;
533 info->bmiHeader.biYPelsPerMeter = 0;
534 info->bmiHeader.biClrUsed = 0;
535 info->bmiHeader.biClrImportant = 0;
536 return &dummy_data;
539 static RECT *dummy_surface_get_bounds( struct window_surface *window_surface )
541 static RECT dummy_bounds;
542 return &dummy_bounds;
545 static void dummy_surface_set_region( struct window_surface *window_surface, HRGN region )
547 /* nothing to do */
550 static void dummy_surface_flush( struct window_surface *window_surface )
552 /* nothing to do */
555 static void dummy_surface_destroy( struct window_surface *window_surface )
557 /* nothing to do */
560 static const struct window_surface_funcs dummy_surface_funcs =
562 dummy_surface_lock,
563 dummy_surface_unlock,
564 dummy_surface_get_bitmap_info,
565 dummy_surface_get_bounds,
566 dummy_surface_set_region,
567 dummy_surface_flush,
568 dummy_surface_destroy
571 struct window_surface dummy_surface = { &dummy_surface_funcs, { NULL, NULL }, 1, { 0, 0, 1, 1 } };
574 /*******************************************************************
575 * register_window_surface
577 * Register a window surface in the global list, possibly replacing another one.
579 void register_window_surface( struct window_surface *old, struct window_surface *new )
581 if (old == new) return;
582 EnterCriticalSection( &surfaces_section );
583 if (old && old != &dummy_surface) list_remove( &old->entry );
584 if (new && new != &dummy_surface) list_add_tail( &window_surfaces, &new->entry );
585 LeaveCriticalSection( &surfaces_section );
589 /*******************************************************************
590 * flush_window_surfaces
592 * Flush pending output from all window surfaces.
594 void flush_window_surfaces( BOOL idle )
596 static DWORD last_idle;
597 DWORD now;
598 struct window_surface *surface;
600 EnterCriticalSection( &surfaces_section );
601 now = GetTickCount();
602 if (idle) last_idle = now;
603 /* if not idle, we only flush if there's evidence that the app never goes idle */
604 else if ((int)(now - last_idle) < 50) goto done;
606 LIST_FOR_EACH_ENTRY( surface, &window_surfaces, struct window_surface, entry )
607 surface->funcs->flush( surface );
608 done:
609 LeaveCriticalSection( &surfaces_section );
613 /***********************************************************************
614 * WIN_GetPtr
616 * Return a pointer to the WND structure if local to the process,
617 * or WND_OTHER_PROCESS if handle may be valid in other process.
618 * If ret value is a valid pointer, it must be released with WIN_ReleasePtr.
620 WND *WIN_GetPtr( HWND hwnd )
622 WND *ptr;
624 if ((ptr = get_user_handle_ptr( hwnd, USER_WINDOW )) == WND_OTHER_PROCESS)
626 if (is_desktop_window( hwnd )) ptr = WND_DESKTOP;
628 return ptr;
632 /***********************************************************************
633 * WIN_IsCurrentProcess
635 * Check whether a given window belongs to the current process (and return the full handle).
637 HWND WIN_IsCurrentProcess( HWND hwnd )
639 WND *ptr;
640 HWND ret;
642 if (!(ptr = WIN_GetPtr( hwnd )) || ptr == WND_OTHER_PROCESS || ptr == WND_DESKTOP) return 0;
643 ret = ptr->obj.handle;
644 WIN_ReleasePtr( ptr );
645 return ret;
649 /***********************************************************************
650 * WIN_IsCurrentThread
652 * Check whether a given window belongs to the current thread (and return the full handle).
654 HWND WIN_IsCurrentThread( HWND hwnd )
656 WND *ptr;
657 HWND ret = 0;
659 if (!(ptr = WIN_GetPtr( hwnd )) || ptr == WND_OTHER_PROCESS || ptr == WND_DESKTOP) return 0;
660 if (ptr->tid == GetCurrentThreadId()) ret = ptr->obj.handle;
661 WIN_ReleasePtr( ptr );
662 return ret;
666 /***********************************************************************
667 * win_set_flags
669 * Set the flags of a window and return the previous value.
671 UINT win_set_flags( HWND hwnd, UINT set_mask, UINT clear_mask )
673 UINT ret;
674 WND *ptr = WIN_GetPtr( hwnd );
676 if (!ptr || ptr == WND_OTHER_PROCESS || ptr == WND_DESKTOP) return 0;
677 ret = ptr->flags;
678 ptr->flags = (ret & ~clear_mask) | set_mask;
679 WIN_ReleasePtr( ptr );
680 return ret;
684 /***********************************************************************
685 * WIN_GetFullHandle
687 * Convert a possibly truncated window handle to a full 32-bit handle.
689 HWND WIN_GetFullHandle( HWND hwnd )
691 WND *ptr;
693 if (!hwnd || (ULONG_PTR)hwnd >> 16) return hwnd;
694 if (LOWORD(hwnd) <= 1 || LOWORD(hwnd) == 0xffff) return hwnd;
695 /* do sign extension for -2 and -3 */
696 if (LOWORD(hwnd) >= (WORD)-3) return (HWND)(LONG_PTR)(INT16)LOWORD(hwnd);
698 if (!(ptr = WIN_GetPtr( hwnd ))) return hwnd;
700 if (ptr == WND_DESKTOP)
702 if (LOWORD(hwnd) == LOWORD(GetDesktopWindow())) return GetDesktopWindow();
703 else return get_hwnd_message_parent();
706 if (ptr != WND_OTHER_PROCESS)
708 hwnd = ptr->obj.handle;
709 WIN_ReleasePtr( ptr );
711 else /* may belong to another process */
713 SERVER_START_REQ( get_window_info )
715 req->handle = wine_server_user_handle( hwnd );
716 if (!wine_server_call_err( req )) hwnd = wine_server_ptr_handle( reply->full_handle );
718 SERVER_END_REQ;
720 return hwnd;
724 /***********************************************************************
725 * WIN_SetOwner
727 * Change the owner of a window.
729 HWND WIN_SetOwner( HWND hwnd, HWND owner )
731 WND *win = WIN_GetPtr( hwnd );
732 HWND ret = 0;
734 if (!win || win == WND_DESKTOP) return 0;
735 if (win == WND_OTHER_PROCESS)
737 if (IsWindow(hwnd)) ERR( "cannot set owner %p on other process window %p\n", owner, hwnd );
738 return 0;
740 SERVER_START_REQ( set_window_owner )
742 req->handle = wine_server_user_handle( hwnd );
743 req->owner = wine_server_user_handle( owner );
744 if (!wine_server_call( req ))
746 win->owner = wine_server_ptr_handle( reply->full_owner );
747 ret = wine_server_ptr_handle( reply->prev_owner );
750 SERVER_END_REQ;
751 WIN_ReleasePtr( win );
752 return ret;
756 /***********************************************************************
757 * WIN_SetStyle
759 * Change the style of a window.
761 ULONG WIN_SetStyle( HWND hwnd, ULONG set_bits, ULONG clear_bits )
763 BOOL ok, made_visible = FALSE;
764 STYLESTRUCT style;
765 WND *win = WIN_GetPtr( hwnd );
767 if (!win || win == WND_DESKTOP) return 0;
768 if (win == WND_OTHER_PROCESS)
770 if (IsWindow(hwnd))
771 return SendMessageW(hwnd, WM_WINE_SETSTYLE, set_bits, clear_bits);
772 return 0;
774 style.styleOld = win->dwStyle;
775 style.styleNew = (win->dwStyle | set_bits) & ~clear_bits;
776 if (style.styleNew == style.styleOld)
778 WIN_ReleasePtr( win );
779 return style.styleNew;
781 SERVER_START_REQ( set_window_info )
783 req->handle = wine_server_user_handle( hwnd );
784 req->flags = SET_WIN_STYLE;
785 req->style = style.styleNew;
786 req->extra_offset = -1;
787 if ((ok = !wine_server_call( req )))
789 style.styleOld = reply->old_style;
790 win->dwStyle = style.styleNew;
793 SERVER_END_REQ;
795 if (ok && ((style.styleOld ^ style.styleNew) & WS_VISIBLE))
797 made_visible = (style.styleNew & WS_VISIBLE) != 0;
798 invalidate_dce( win, NULL );
800 WIN_ReleasePtr( win );
802 if (!ok) return 0;
804 USER_Driver->pSetWindowStyle( hwnd, GWL_STYLE, &style );
805 if (made_visible) update_window_state( hwnd );
807 return style.styleOld;
811 /***********************************************************************
812 * WIN_GetRectangles
814 * Get the window and client rectangles.
816 BOOL WIN_GetRectangles( HWND hwnd, enum coords_relative relative, RECT *rectWindow, RECT *rectClient )
818 WND *win = WIN_GetPtr( hwnd );
819 BOOL ret = TRUE;
821 if (!win)
823 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
824 return FALSE;
826 if (win == WND_DESKTOP)
828 RECT rect;
829 rect.left = rect.top = 0;
830 if (hwnd == get_hwnd_message_parent())
832 rect.right = 100;
833 rect.bottom = 100;
835 else
837 rect.right = GetSystemMetrics(SM_CXSCREEN);
838 rect.bottom = GetSystemMetrics(SM_CYSCREEN);
840 if (rectWindow) *rectWindow = rect;
841 if (rectClient) *rectClient = rect;
842 return TRUE;
844 if (win != WND_OTHER_PROCESS)
846 RECT window_rect = win->rectWindow, client_rect = win->rectClient;
848 switch (relative)
850 case COORDS_CLIENT:
851 OffsetRect( &window_rect, -win->rectClient.left, -win->rectClient.top );
852 OffsetRect( &client_rect, -win->rectClient.left, -win->rectClient.top );
853 if (win->dwExStyle & WS_EX_LAYOUTRTL)
854 mirror_rect( &win->rectClient, &window_rect );
855 break;
856 case COORDS_WINDOW:
857 OffsetRect( &window_rect, -win->rectWindow.left, -win->rectWindow.top );
858 OffsetRect( &client_rect, -win->rectWindow.left, -win->rectWindow.top );
859 if (win->dwExStyle & WS_EX_LAYOUTRTL)
860 mirror_rect( &win->rectWindow, &client_rect );
861 break;
862 case COORDS_PARENT:
863 if (win->parent)
865 WND *parent = WIN_GetPtr( win->parent );
866 if (parent == WND_DESKTOP) break;
867 if (!parent || parent == WND_OTHER_PROCESS)
869 WIN_ReleasePtr( win );
870 goto other_process;
872 if (parent->flags & WIN_CHILDREN_MOVED)
874 WIN_ReleasePtr( parent );
875 WIN_ReleasePtr( win );
876 goto other_process;
878 if (parent->dwExStyle & WS_EX_LAYOUTRTL)
880 mirror_rect( &parent->rectClient, &window_rect );
881 mirror_rect( &parent->rectClient, &client_rect );
883 WIN_ReleasePtr( parent );
885 break;
886 case COORDS_SCREEN:
887 while (win->parent)
889 WND *parent = WIN_GetPtr( win->parent );
890 if (parent == WND_DESKTOP) break;
891 if (!parent || parent == WND_OTHER_PROCESS)
893 WIN_ReleasePtr( win );
894 goto other_process;
896 WIN_ReleasePtr( win );
897 if (parent->flags & WIN_CHILDREN_MOVED)
899 WIN_ReleasePtr( parent );
900 goto other_process;
902 win = parent;
903 if (win->parent)
905 OffsetRect( &window_rect, win->rectClient.left, win->rectClient.top );
906 OffsetRect( &client_rect, win->rectClient.left, win->rectClient.top );
909 break;
911 if (rectWindow) *rectWindow = window_rect;
912 if (rectClient) *rectClient = client_rect;
913 WIN_ReleasePtr( win );
914 return TRUE;
917 other_process:
918 SERVER_START_REQ( get_window_rectangles )
920 req->handle = wine_server_user_handle( hwnd );
921 req->relative = relative;
922 if ((ret = !wine_server_call_err( req )))
924 if (rectWindow)
926 rectWindow->left = reply->window.left;
927 rectWindow->top = reply->window.top;
928 rectWindow->right = reply->window.right;
929 rectWindow->bottom = reply->window.bottom;
931 if (rectClient)
933 rectClient->left = reply->client.left;
934 rectClient->top = reply->client.top;
935 rectClient->right = reply->client.right;
936 rectClient->bottom = reply->client.bottom;
940 SERVER_END_REQ;
941 return ret;
945 /***********************************************************************
946 * WIN_DestroyWindow
948 * Destroy storage associated to a window. "Internals" p.358
950 LRESULT WIN_DestroyWindow( HWND hwnd )
952 WND *wndPtr;
953 HWND *list;
954 HMENU menu = 0, sys_menu;
955 HWND icon_title;
956 struct window_surface *surface;
958 TRACE("%p\n", hwnd );
960 /* destroy default IME window */
961 if (win_set_flags( hwnd, 0, WIN_HAS_IME_WIN ) & WIN_HAS_IME_WIN)
963 TRACE("unregister IME window for %p\n", hwnd);
964 imm_unregister_window( hwnd );
967 /* free child windows */
968 if ((list = WIN_ListChildren( hwnd )))
970 int i;
971 for (i = 0; list[i]; i++)
973 if (WIN_IsCurrentThread( list[i] )) WIN_DestroyWindow( list[i] );
974 else SendMessageW( list[i], WM_WINE_DESTROYWINDOW, 0, 0 );
976 HeapFree( GetProcessHeap(), 0, list );
979 /* Unlink now so we won't bother with the children later on */
980 SERVER_START_REQ( set_parent )
982 req->handle = wine_server_user_handle( hwnd );
983 req->parent = 0;
984 wine_server_call( req );
986 SERVER_END_REQ;
989 * Send the WM_NCDESTROY to the window being destroyed.
991 SendMessageW( hwnd, WM_NCDESTROY, 0, 0 );
993 /* FIXME: do we need to fake QS_MOUSEMOVE wakebit? */
995 /* free resources associated with the window */
997 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
998 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
999 menu = (HMENU)wndPtr->wIDmenu;
1000 sys_menu = wndPtr->hSysMenu;
1001 free_dce( wndPtr->dce, hwnd );
1002 wndPtr->dce = NULL;
1003 icon_title = wndPtr->icon_title;
1004 HeapFree( GetProcessHeap(), 0, wndPtr->text );
1005 wndPtr->text = NULL;
1006 HeapFree( GetProcessHeap(), 0, wndPtr->pScroll );
1007 wndPtr->pScroll = NULL;
1008 DestroyIcon( wndPtr->hIconSmall2 );
1009 surface = wndPtr->surface;
1010 wndPtr->surface = NULL;
1011 WIN_ReleasePtr( wndPtr );
1013 if (icon_title) DestroyWindow( icon_title );
1014 if (menu) DestroyMenu( menu );
1015 if (sys_menu) DestroyMenu( sys_menu );
1016 if (surface)
1018 register_window_surface( surface, NULL );
1019 window_surface_release( surface );
1022 USER_Driver->pDestroyWindow( hwnd );
1024 free_window_handle( hwnd );
1025 return 0;
1029 /***********************************************************************
1030 * destroy_thread_window
1032 * Destroy a window upon exit of its thread.
1034 static void destroy_thread_window( HWND hwnd )
1036 WND *wndPtr;
1037 HWND *list;
1038 HMENU menu = 0, sys_menu = 0;
1039 struct window_surface *surface = NULL;
1040 WORD index;
1042 /* free child windows */
1044 if ((list = WIN_ListChildren( hwnd )))
1046 int i;
1047 for (i = 0; list[i]; i++)
1049 if (WIN_IsCurrentThread( list[i] )) destroy_thread_window( list[i] );
1050 else SendMessageW( list[i], WM_WINE_DESTROYWINDOW, 0, 0 );
1052 HeapFree( GetProcessHeap(), 0, list );
1055 /* destroy the client-side storage */
1057 index = USER_HANDLE_TO_INDEX(hwnd);
1058 if (index >= NB_USER_HANDLES) return;
1059 USER_Lock();
1060 if ((wndPtr = user_handles[index]))
1062 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD) menu = (HMENU)wndPtr->wIDmenu;
1063 sys_menu = wndPtr->hSysMenu;
1064 free_dce( wndPtr->dce, hwnd );
1065 surface = wndPtr->surface;
1066 wndPtr->surface = NULL;
1067 InterlockedCompareExchangePointer( &user_handles[index], NULL, wndPtr );
1069 USER_Unlock();
1071 HeapFree( GetProcessHeap(), 0, wndPtr );
1072 if (menu) DestroyMenu( menu );
1073 if (sys_menu) DestroyMenu( sys_menu );
1074 if (surface)
1076 register_window_surface( surface, NULL );
1077 window_surface_release( surface );
1082 /***********************************************************************
1083 * destroy_thread_child_windows
1085 * Destroy child windows upon exit of its thread.
1087 static void destroy_thread_child_windows( HWND hwnd )
1089 HWND *list;
1090 int i;
1092 if (WIN_IsCurrentThread( hwnd ))
1094 destroy_thread_window( hwnd );
1096 else if ((list = WIN_ListChildren( hwnd )))
1098 for (i = 0; list[i]; i++) destroy_thread_child_windows( list[i] );
1099 HeapFree( GetProcessHeap(), 0, list );
1104 /***********************************************************************
1105 * WIN_DestroyThreadWindows
1107 * Destroy all children of 'wnd' owned by the current thread.
1109 void WIN_DestroyThreadWindows( HWND hwnd )
1111 HWND *list;
1112 int i;
1114 if (!(list = WIN_ListChildren( hwnd ))) return;
1116 /* reset owners of top-level windows */
1117 for (i = 0; list[i]; i++)
1119 if (!WIN_IsCurrentThread( list[i] ))
1121 HWND owner = GetWindow( list[i], GW_OWNER );
1122 if (owner && WIN_IsCurrentThread( owner )) WIN_SetOwner( list[i], 0 );
1126 for (i = 0; list[i]; i++) destroy_thread_child_windows( list[i] );
1127 HeapFree( GetProcessHeap(), 0, list );
1131 /***********************************************************************
1132 * WIN_FixCoordinates
1134 * Fix the coordinates - Helper for WIN_CreateWindowEx.
1135 * returns default show mode in sw.
1137 static void WIN_FixCoordinates( CREATESTRUCTW *cs, INT *sw)
1139 #define IS_DEFAULT(x) ((x) == CW_USEDEFAULT || (x) == (SHORT)0x8000)
1140 POINT pos[2];
1142 if (cs->dwExStyle & WS_EX_MDICHILD)
1144 UINT id = 0;
1146 MDI_CalcDefaultChildPos(cs->hwndParent, -1, pos, 0, &id);
1147 if (!(cs->style & WS_POPUP)) cs->hMenu = ULongToHandle(id);
1149 TRACE("MDI child id %04x\n", id);
1152 if (cs->style & (WS_CHILD | WS_POPUP))
1154 if (cs->dwExStyle & WS_EX_MDICHILD)
1156 if (IS_DEFAULT(cs->x))
1158 cs->x = pos[0].x;
1159 cs->y = pos[0].y;
1161 if (IS_DEFAULT(cs->cx) || !cs->cx) cs->cx = pos[1].x;
1162 if (IS_DEFAULT(cs->cy) || !cs->cy) cs->cy = pos[1].y;
1164 else
1166 if (IS_DEFAULT(cs->x)) cs->x = cs->y = 0;
1167 if (IS_DEFAULT(cs->cx)) cs->cx = cs->cy = 0;
1170 else /* overlapped window */
1172 HMONITOR monitor;
1173 MONITORINFO mon_info;
1174 STARTUPINFOW info;
1176 if (!IS_DEFAULT(cs->x) && !IS_DEFAULT(cs->cx) && !IS_DEFAULT(cs->cy)) return;
1178 monitor = MonitorFromWindow( cs->hwndParent, MONITOR_DEFAULTTOPRIMARY );
1179 mon_info.cbSize = sizeof(mon_info);
1180 GetMonitorInfoW( monitor, &mon_info );
1181 GetStartupInfoW( &info );
1183 if (IS_DEFAULT(cs->x))
1185 if (!IS_DEFAULT(cs->y)) *sw = cs->y;
1186 cs->x = (info.dwFlags & STARTF_USEPOSITION) ? info.dwX : mon_info.rcWork.left;
1187 cs->y = (info.dwFlags & STARTF_USEPOSITION) ? info.dwY : mon_info.rcWork.top;
1190 if (IS_DEFAULT(cs->cx))
1192 if (info.dwFlags & STARTF_USESIZE)
1194 cs->cx = info.dwXSize;
1195 cs->cy = info.dwYSize;
1197 else
1199 cs->cx = (mon_info.rcWork.right - mon_info.rcWork.left) * 3 / 4 - cs->x;
1200 cs->cy = (mon_info.rcWork.bottom - mon_info.rcWork.top) * 3 / 4 - cs->y;
1203 /* neither x nor cx are default. Check the y values .
1204 * In the trace we see Outlook and Outlook Express using
1205 * cy set to CW_USEDEFAULT when opening the address book.
1207 else if (IS_DEFAULT(cs->cy))
1209 FIXME("Strange use of CW_USEDEFAULT in nHeight\n");
1210 cs->cy = (mon_info.rcWork.bottom - mon_info.rcWork.top) * 3 / 4 - cs->y;
1213 #undef IS_DEFAULT
1216 /***********************************************************************
1217 * dump_window_styles
1219 static void dump_window_styles( DWORD style, DWORD exstyle )
1221 TRACE( "style:" );
1222 if(style & WS_POPUP) TRACE(" WS_POPUP");
1223 if(style & WS_CHILD) TRACE(" WS_CHILD");
1224 if(style & WS_MINIMIZE) TRACE(" WS_MINIMIZE");
1225 if(style & WS_VISIBLE) TRACE(" WS_VISIBLE");
1226 if(style & WS_DISABLED) TRACE(" WS_DISABLED");
1227 if(style & WS_CLIPSIBLINGS) TRACE(" WS_CLIPSIBLINGS");
1228 if(style & WS_CLIPCHILDREN) TRACE(" WS_CLIPCHILDREN");
1229 if(style & WS_MAXIMIZE) TRACE(" WS_MAXIMIZE");
1230 if((style & WS_CAPTION) == WS_CAPTION) TRACE(" WS_CAPTION");
1231 else
1233 if(style & WS_BORDER) TRACE(" WS_BORDER");
1234 if(style & WS_DLGFRAME) TRACE(" WS_DLGFRAME");
1236 if(style & WS_VSCROLL) TRACE(" WS_VSCROLL");
1237 if(style & WS_HSCROLL) TRACE(" WS_HSCROLL");
1238 if(style & WS_SYSMENU) TRACE(" WS_SYSMENU");
1239 if(style & WS_THICKFRAME) TRACE(" WS_THICKFRAME");
1240 if (style & WS_CHILD)
1242 if(style & WS_GROUP) TRACE(" WS_GROUP");
1243 if(style & WS_TABSTOP) TRACE(" WS_TABSTOP");
1245 else
1247 if(style & WS_MINIMIZEBOX) TRACE(" WS_MINIMIZEBOX");
1248 if(style & WS_MAXIMIZEBOX) TRACE(" WS_MAXIMIZEBOX");
1251 /* FIXME: Add dumping of BS_/ES_/SBS_/LBS_/CBS_/DS_/etc. styles */
1252 #define DUMPED_STYLES \
1253 ((DWORD)(WS_POPUP | \
1254 WS_CHILD | \
1255 WS_MINIMIZE | \
1256 WS_VISIBLE | \
1257 WS_DISABLED | \
1258 WS_CLIPSIBLINGS | \
1259 WS_CLIPCHILDREN | \
1260 WS_MAXIMIZE | \
1261 WS_BORDER | \
1262 WS_DLGFRAME | \
1263 WS_VSCROLL | \
1264 WS_HSCROLL | \
1265 WS_SYSMENU | \
1266 WS_THICKFRAME | \
1267 WS_GROUP | \
1268 WS_TABSTOP | \
1269 WS_MINIMIZEBOX | \
1270 WS_MAXIMIZEBOX))
1272 if(style & ~DUMPED_STYLES) TRACE(" %08x", style & ~DUMPED_STYLES);
1273 TRACE("\n");
1274 #undef DUMPED_STYLES
1276 TRACE( "exstyle:" );
1277 if(exstyle & WS_EX_DLGMODALFRAME) TRACE(" WS_EX_DLGMODALFRAME");
1278 if(exstyle & WS_EX_DRAGDETECT) TRACE(" WS_EX_DRAGDETECT");
1279 if(exstyle & WS_EX_NOPARENTNOTIFY) TRACE(" WS_EX_NOPARENTNOTIFY");
1280 if(exstyle & WS_EX_TOPMOST) TRACE(" WS_EX_TOPMOST");
1281 if(exstyle & WS_EX_ACCEPTFILES) TRACE(" WS_EX_ACCEPTFILES");
1282 if(exstyle & WS_EX_TRANSPARENT) TRACE(" WS_EX_TRANSPARENT");
1283 if(exstyle & WS_EX_MDICHILD) TRACE(" WS_EX_MDICHILD");
1284 if(exstyle & WS_EX_TOOLWINDOW) TRACE(" WS_EX_TOOLWINDOW");
1285 if(exstyle & WS_EX_WINDOWEDGE) TRACE(" WS_EX_WINDOWEDGE");
1286 if(exstyle & WS_EX_CLIENTEDGE) TRACE(" WS_EX_CLIENTEDGE");
1287 if(exstyle & WS_EX_CONTEXTHELP) TRACE(" WS_EX_CONTEXTHELP");
1288 if(exstyle & WS_EX_RIGHT) TRACE(" WS_EX_RIGHT");
1289 if(exstyle & WS_EX_RTLREADING) TRACE(" WS_EX_RTLREADING");
1290 if(exstyle & WS_EX_LEFTSCROLLBAR) TRACE(" WS_EX_LEFTSCROLLBAR");
1291 if(exstyle & WS_EX_CONTROLPARENT) TRACE(" WS_EX_CONTROLPARENT");
1292 if(exstyle & WS_EX_STATICEDGE) TRACE(" WS_EX_STATICEDGE");
1293 if(exstyle & WS_EX_APPWINDOW) TRACE(" WS_EX_APPWINDOW");
1294 if(exstyle & WS_EX_LAYERED) TRACE(" WS_EX_LAYERED");
1295 if(exstyle & WS_EX_NOINHERITLAYOUT) TRACE(" WS_EX_NOINHERITLAYOUT");
1296 if(exstyle & WS_EX_LAYOUTRTL) TRACE(" WS_EX_LAYOUTRTL");
1297 if(exstyle & WS_EX_COMPOSITED) TRACE(" WS_EX_COMPOSITED");
1298 if(exstyle & WS_EX_NOACTIVATE) TRACE(" WS_EX_NOACTIVATE");
1300 #define DUMPED_EX_STYLES \
1301 ((DWORD)(WS_EX_DLGMODALFRAME | \
1302 WS_EX_DRAGDETECT | \
1303 WS_EX_NOPARENTNOTIFY | \
1304 WS_EX_TOPMOST | \
1305 WS_EX_ACCEPTFILES | \
1306 WS_EX_TRANSPARENT | \
1307 WS_EX_MDICHILD | \
1308 WS_EX_TOOLWINDOW | \
1309 WS_EX_WINDOWEDGE | \
1310 WS_EX_CLIENTEDGE | \
1311 WS_EX_CONTEXTHELP | \
1312 WS_EX_RIGHT | \
1313 WS_EX_RTLREADING | \
1314 WS_EX_LEFTSCROLLBAR | \
1315 WS_EX_CONTROLPARENT | \
1316 WS_EX_STATICEDGE | \
1317 WS_EX_APPWINDOW | \
1318 WS_EX_LAYERED | \
1319 WS_EX_NOINHERITLAYOUT | \
1320 WS_EX_LAYOUTRTL | \
1321 WS_EX_COMPOSITED |\
1322 WS_EX_NOACTIVATE))
1324 if(exstyle & ~DUMPED_EX_STYLES) TRACE(" %08x", exstyle & ~DUMPED_EX_STYLES);
1325 TRACE("\n");
1326 #undef DUMPED_EX_STYLES
1330 /***********************************************************************
1331 * WIN_CreateWindowEx
1333 * Implementation of CreateWindowEx().
1335 HWND WIN_CreateWindowEx( CREATESTRUCTW *cs, LPCWSTR className, HINSTANCE module, BOOL unicode )
1337 INT cx, cy, style, sw = SW_SHOW;
1338 LRESULT result;
1339 RECT rect;
1340 WND *wndPtr;
1341 HWND hwnd, parent, owner, top_child = 0;
1342 const WCHAR *p = className;
1343 MDICREATESTRUCTW mdi_cs;
1344 CBT_CREATEWNDW cbtc;
1345 CREATESTRUCTW cbcs;
1347 className = CLASS_GetVersionedName(className, NULL, TRUE);
1349 TRACE("%s %s%s%s ex=%08x style=%08x %d,%d %dx%d parent=%p menu=%p inst=%p params=%p\n",
1350 unicode ? debugstr_w(cs->lpszName) : debugstr_a((LPCSTR)cs->lpszName),
1351 debugstr_w(p), p != className ? "->" : "", p != className ? debugstr_w(className) : "",
1352 cs->dwExStyle, cs->style, cs->x, cs->y, cs->cx, cs->cy,
1353 cs->hwndParent, cs->hMenu, cs->hInstance, cs->lpCreateParams );
1354 if(TRACE_ON(win)) dump_window_styles( cs->style, cs->dwExStyle );
1356 /* Fix the styles for MDI children */
1357 if (cs->dwExStyle & WS_EX_MDICHILD)
1359 if (!(win_get_flags( cs->hwndParent ) & WIN_ISMDICLIENT))
1361 WARN("WS_EX_MDICHILD, but parent %p is not MDIClient\n", cs->hwndParent);
1362 return 0;
1365 /* cs->lpCreateParams of WM_[NC]CREATE is different for MDI children.
1366 * MDICREATESTRUCT members have the originally passed values.
1368 * Note: we rely on the fact that MDICREATESTRUCTA and MDICREATESTRUCTW
1369 * have the same layout.
1371 mdi_cs.szClass = cs->lpszClass;
1372 mdi_cs.szTitle = cs->lpszName;
1373 mdi_cs.hOwner = cs->hInstance;
1374 mdi_cs.x = cs->x;
1375 mdi_cs.y = cs->y;
1376 mdi_cs.cx = cs->cx;
1377 mdi_cs.cy = cs->cy;
1378 mdi_cs.style = cs->style;
1379 mdi_cs.lParam = (LPARAM)cs->lpCreateParams;
1381 cs->lpCreateParams = &mdi_cs;
1383 if (GetWindowLongW(cs->hwndParent, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1385 if (cs->style & WS_POPUP)
1387 TRACE("WS_POPUP with MDIS_ALLCHILDSTYLES is not allowed\n");
1388 return 0;
1390 cs->style |= WS_CHILD | WS_CLIPSIBLINGS;
1392 else
1394 cs->style &= ~WS_POPUP;
1395 cs->style |= WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CAPTION |
1396 WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
1399 top_child = GetWindow(cs->hwndParent, GW_CHILD);
1401 if (top_child)
1403 /* Restore current maximized child */
1404 if((cs->style & WS_VISIBLE) && IsZoomed(top_child))
1406 TRACE("Restoring current maximized child %p\n", top_child);
1407 if (cs->style & WS_MAXIMIZE)
1409 /* if the new window is maximized don't bother repainting */
1410 SendMessageW( top_child, WM_SETREDRAW, FALSE, 0 );
1411 ShowWindow( top_child, SW_SHOWNORMAL );
1412 SendMessageW( top_child, WM_SETREDRAW, TRUE, 0 );
1414 else ShowWindow( top_child, SW_SHOWNORMAL );
1419 /* Find the parent window */
1421 parent = cs->hwndParent;
1422 owner = 0;
1424 if (cs->hwndParent == HWND_MESSAGE)
1426 cs->hwndParent = parent = get_hwnd_message_parent();
1428 else if (cs->hwndParent)
1430 if ((cs->style & (WS_CHILD|WS_POPUP)) != WS_CHILD)
1432 parent = GetDesktopWindow();
1433 owner = cs->hwndParent;
1435 else
1437 DWORD parent_style = GetWindowLongW( parent, GWL_EXSTYLE );
1438 if ((parent_style & WS_EX_LAYOUTRTL) && !(parent_style & WS_EX_NOINHERITLAYOUT))
1439 cs->dwExStyle |= WS_EX_LAYOUTRTL;
1442 else
1444 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
1446 if ((cs->style & (WS_CHILD|WS_POPUP)) == WS_CHILD)
1448 WARN("No parent for child window\n" );
1449 SetLastError(ERROR_TLW_WITH_WSCHILD);
1450 return 0; /* WS_CHILD needs a parent, but WS_POPUP doesn't */
1453 /* are we creating the desktop or HWND_MESSAGE parent itself? */
1454 if (className != (LPCWSTR)DESKTOP_CLASS_ATOM &&
1455 (IS_INTRESOURCE(className) || strcmpiW( className, messageW )))
1457 DWORD layout;
1458 GetProcessDefaultLayout( &layout );
1459 if (layout & LAYOUT_RTL) cs->dwExStyle |= WS_EX_LAYOUTRTL;
1460 parent = GetDesktopWindow();
1464 WIN_FixCoordinates(cs, &sw); /* fix default coordinates */
1466 if ((cs->dwExStyle & WS_EX_DLGMODALFRAME) ||
1467 ((!(cs->dwExStyle & WS_EX_STATICEDGE)) &&
1468 (cs->style & (WS_DLGFRAME | WS_THICKFRAME))))
1469 cs->dwExStyle |= WS_EX_WINDOWEDGE;
1470 else
1471 cs->dwExStyle &= ~WS_EX_WINDOWEDGE;
1473 /* Create the window structure */
1475 if (!(wndPtr = create_window_handle( parent, owner, className, module, unicode )))
1477 WNDCLASSW wc;
1478 /* if it's a comctl32 class, GetClassInfo will load it, then we can retry */
1479 if (GetLastError() != ERROR_INVALID_HANDLE ||
1480 !GetClassInfoW( 0, className, &wc ) ||
1481 !(wndPtr = create_window_handle( parent, owner, className, module, unicode )))
1482 return 0;
1484 hwnd = wndPtr->obj.handle;
1486 /* Fill the window structure */
1488 wndPtr->tid = GetCurrentThreadId();
1489 wndPtr->hInstance = cs->hInstance;
1490 wndPtr->text = NULL;
1491 wndPtr->dwStyle = cs->style & ~WS_VISIBLE;
1492 wndPtr->dwExStyle = cs->dwExStyle;
1493 wndPtr->wIDmenu = 0;
1494 wndPtr->helpContext = 0;
1495 wndPtr->pScroll = NULL;
1496 wndPtr->userdata = 0;
1497 wndPtr->hIcon = 0;
1498 wndPtr->hIconSmall = 0;
1499 wndPtr->hIconSmall2 = 0;
1500 wndPtr->hSysMenu = 0;
1502 wndPtr->min_pos.x = wndPtr->min_pos.y = -1;
1503 wndPtr->max_pos.x = wndPtr->max_pos.y = -1;
1504 SetRect( &wndPtr->normal_rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
1506 if (wndPtr->dwStyle & WS_SYSMENU) SetSystemMenu( hwnd, 0 );
1509 * Correct the window styles.
1511 * It affects only the style loaded into the WIN structure.
1514 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
1516 wndPtr->dwStyle |= WS_CLIPSIBLINGS;
1517 if (!(wndPtr->dwStyle & WS_POPUP))
1518 wndPtr->dwStyle |= WS_CAPTION;
1521 /* WS_EX_WINDOWEDGE depends on some other styles */
1522 if (wndPtr->dwExStyle & WS_EX_DLGMODALFRAME)
1523 wndPtr->dwExStyle |= WS_EX_WINDOWEDGE;
1524 else if (wndPtr->dwStyle & (WS_DLGFRAME | WS_THICKFRAME))
1526 if (!((wndPtr->dwExStyle & WS_EX_STATICEDGE) &&
1527 (wndPtr->dwStyle & (WS_CHILD | WS_POPUP))))
1528 wndPtr->dwExStyle |= WS_EX_WINDOWEDGE;
1530 else
1531 wndPtr->dwExStyle &= ~WS_EX_WINDOWEDGE;
1533 if (!(wndPtr->dwStyle & (WS_CHILD | WS_POPUP)))
1534 wndPtr->flags |= WIN_NEED_SIZE;
1536 SERVER_START_REQ( set_window_info )
1538 req->handle = wine_server_user_handle( hwnd );
1539 req->flags = SET_WIN_STYLE | SET_WIN_EXSTYLE | SET_WIN_INSTANCE | SET_WIN_UNICODE;
1540 req->style = wndPtr->dwStyle;
1541 req->ex_style = wndPtr->dwExStyle;
1542 req->instance = wine_server_client_ptr( wndPtr->hInstance );
1543 req->is_unicode = (wndPtr->flags & WIN_ISUNICODE) != 0;
1544 req->extra_offset = -1;
1545 wine_server_call( req );
1547 SERVER_END_REQ;
1549 /* Set the window menu */
1551 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
1553 if (cs->hMenu)
1555 if (!MENU_SetMenu(hwnd, cs->hMenu))
1557 WIN_ReleasePtr( wndPtr );
1558 free_window_handle( hwnd );
1559 return 0;
1562 else
1564 LPCWSTR menuName = (LPCWSTR)GetClassLongPtrW( hwnd, GCLP_MENUNAME );
1565 if (menuName)
1567 cs->hMenu = LoadMenuW( cs->hInstance, menuName );
1568 if (cs->hMenu) MENU_SetMenu( hwnd, cs->hMenu );
1572 else SetWindowLongPtrW( hwnd, GWLP_ID, (ULONG_PTR)cs->hMenu );
1574 /* call the WH_CBT hook */
1576 /* the window style passed to the hook must be the real window style,
1577 * rather than just the window style that the caller to CreateWindowEx
1578 * passed in, so we have to copy the original CREATESTRUCT and get the
1579 * the real style. */
1580 cbcs = *cs;
1581 cbcs.style = wndPtr->dwStyle;
1582 cbtc.lpcs = &cbcs;
1583 cbtc.hwndInsertAfter = HWND_TOP;
1584 WIN_ReleasePtr( wndPtr );
1585 if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode )) goto failed;
1587 /* send the WM_GETMINMAXINFO message and fix the size if needed */
1589 cx = cs->cx;
1590 cy = cs->cy;
1591 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
1593 POINT maxSize, maxPos, minTrack, maxTrack;
1594 WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
1595 if (maxTrack.x < cx) cx = maxTrack.x;
1596 if (maxTrack.y < cy) cy = maxTrack.y;
1597 if (minTrack.x > cx) cx = minTrack.x;
1598 if (minTrack.y > cy) cy = minTrack.y;
1601 if (cx < 0) cx = 0;
1602 if (cy < 0) cy = 0;
1603 SetRect( &rect, cs->x, cs->y, cs->x + cx, cs->y + cy );
1604 /* check for wraparound */
1605 if (cs->x + cx < cs->x) rect.right = 0x7fffffff;
1606 if (cs->y + cy < cs->y) rect.bottom = 0x7fffffff;
1607 if (!set_window_pos( hwnd, 0, SWP_NOZORDER | SWP_NOACTIVATE, &rect, &rect, NULL )) goto failed;
1609 /* send WM_NCCREATE */
1611 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cx, cy );
1612 if (unicode)
1613 result = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1614 else
1615 result = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1616 if (!result)
1618 WARN( "%p: aborted by WM_NCCREATE\n", hwnd );
1619 goto failed;
1622 /* create default IME window */
1624 if (imm_register_window && !is_desktop_window( hwnd ) &&
1625 parent != get_hwnd_message_parent() && imm_register_window( hwnd ))
1627 TRACE("register IME window for %p\n", hwnd);
1628 win_set_flags( hwnd, WIN_HAS_IME_WIN, 0 );
1631 /* send WM_NCCALCSIZE */
1633 if (WIN_GetRectangles( hwnd, COORDS_PARENT, &rect, NULL ))
1635 /* yes, even if the CBT hook was called with HWND_TOP */
1636 HWND insert_after = (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
1637 RECT client_rect = rect;
1639 /* the rectangle is in screen coords for WM_NCCALCSIZE when wparam is FALSE */
1640 MapWindowPoints( parent, 0, (POINT *)&client_rect, 2 );
1641 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&client_rect );
1642 MapWindowPoints( 0, parent, (POINT *)&client_rect, 2 );
1643 set_window_pos( hwnd, insert_after, SWP_NOACTIVATE, &rect, &client_rect, NULL );
1645 else return 0;
1647 /* send WM_CREATE */
1649 if (unicode)
1650 result = SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs );
1651 else
1652 result = SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs );
1653 if (result == -1) goto failed;
1655 /* call the driver */
1657 if (!USER_Driver->pCreateWindow( hwnd )) goto failed;
1659 NotifyWinEvent(EVENT_OBJECT_CREATE, hwnd, OBJID_WINDOW, 0);
1661 /* send the size messages */
1663 if (!(win_get_flags( hwnd ) & WIN_NEED_SIZE))
1665 WIN_GetRectangles( hwnd, COORDS_PARENT, NULL, &rect );
1666 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1667 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1668 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( rect.left, rect.top ) );
1671 /* Show the window, maximizing or minimizing if needed */
1673 style = WIN_SetStyle( hwnd, 0, WS_MAXIMIZE | WS_MINIMIZE );
1674 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1676 RECT newPos;
1677 UINT swFlag = (style & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1679 swFlag = WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1680 swFlag |= SWP_FRAMECHANGED; /* Frame always gets changed */
1681 if (!(style & WS_VISIBLE) || (style & WS_CHILD) || GetActiveWindow()) swFlag |= SWP_NOACTIVATE;
1682 SetWindowPos( hwnd, 0, newPos.left, newPos.top, newPos.right - newPos.left,
1683 newPos.bottom - newPos.top, swFlag );
1686 /* Notify the parent window only */
1688 send_parent_notify( hwnd, WM_CREATE );
1689 if (!IsWindow( hwnd )) return 0;
1691 if (parent == GetDesktopWindow())
1692 PostMessageW( parent, WM_PARENTNOTIFY, WM_CREATE, (LPARAM)hwnd );
1694 if (cs->style & WS_VISIBLE)
1696 if (cs->style & WS_MAXIMIZE)
1697 sw = SW_SHOW;
1698 else if (cs->style & WS_MINIMIZE)
1699 sw = SW_SHOWMINIMIZED;
1701 ShowWindow( hwnd, sw );
1702 if (cs->dwExStyle & WS_EX_MDICHILD)
1704 SendMessageW(cs->hwndParent, WM_MDIREFRESHMENU, 0, 0);
1705 /* ShowWindow won't activate child windows */
1706 SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE );
1710 /* Call WH_SHELL hook */
1712 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) && !GetWindow( hwnd, GW_OWNER ))
1713 HOOK_CallHooks( WH_SHELL, HSHELL_WINDOWCREATED, (WPARAM)hwnd, 0, TRUE );
1715 TRACE("created window %p\n", hwnd);
1716 return hwnd;
1718 failed:
1719 WIN_DestroyWindow( hwnd );
1720 return 0;
1724 /***********************************************************************
1725 * CreateWindowExA (USER32.@)
1727 HWND WINAPI DECLSPEC_HOTPATCH CreateWindowExA( DWORD exStyle, LPCSTR className,
1728 LPCSTR windowName, DWORD style, INT x,
1729 INT y, INT width, INT height,
1730 HWND parent, HMENU menu,
1731 HINSTANCE instance, LPVOID data )
1733 CREATESTRUCTA cs;
1735 cs.lpCreateParams = data;
1736 cs.hInstance = instance;
1737 cs.hMenu = menu;
1738 cs.hwndParent = parent;
1739 cs.x = x;
1740 cs.y = y;
1741 cs.cx = width;
1742 cs.cy = height;
1743 cs.style = style;
1744 cs.lpszName = windowName;
1745 cs.lpszClass = className;
1746 cs.dwExStyle = exStyle;
1748 if (!IS_INTRESOURCE(className))
1750 WCHAR bufferW[256];
1751 if (!MultiByteToWideChar( CP_ACP, 0, className, -1, bufferW, sizeof(bufferW)/sizeof(WCHAR) ))
1752 return 0;
1753 return wow_handlers.create_window( (CREATESTRUCTW *)&cs, bufferW, instance, FALSE );
1755 /* Note: we rely on the fact that CREATESTRUCTA and */
1756 /* CREATESTRUCTW have the same layout. */
1757 return wow_handlers.create_window( (CREATESTRUCTW *)&cs, (LPCWSTR)className, instance, FALSE );
1761 /***********************************************************************
1762 * CreateWindowExW (USER32.@)
1764 HWND WINAPI DECLSPEC_HOTPATCH CreateWindowExW( DWORD exStyle, LPCWSTR className,
1765 LPCWSTR windowName, DWORD style, INT x,
1766 INT y, INT width, INT height,
1767 HWND parent, HMENU menu,
1768 HINSTANCE instance, LPVOID data )
1770 CREATESTRUCTW cs;
1772 cs.lpCreateParams = data;
1773 cs.hInstance = instance;
1774 cs.hMenu = menu;
1775 cs.hwndParent = parent;
1776 cs.x = x;
1777 cs.y = y;
1778 cs.cx = width;
1779 cs.cy = height;
1780 cs.style = style;
1781 cs.lpszName = windowName;
1782 cs.lpszClass = className;
1783 cs.dwExStyle = exStyle;
1785 return wow_handlers.create_window( &cs, className, instance, TRUE );
1789 /***********************************************************************
1790 * WIN_SendDestroyMsg
1792 static void WIN_SendDestroyMsg( HWND hwnd )
1794 GUITHREADINFO info;
1796 info.cbSize = sizeof(info);
1797 if (GetGUIThreadInfo( GetCurrentThreadId(), &info ))
1799 if (hwnd == info.hwndCaret) DestroyCaret();
1800 if (hwnd == info.hwndActive) WINPOS_ActivateOtherWindow( hwnd );
1803 if (hwnd == GetClipboardOwner()) CLIPBOARD_ReleaseOwner( hwnd );
1806 * Send the WM_DESTROY to the window.
1808 SendMessageW( hwnd, WM_DESTROY, 0, 0);
1811 * This WM_DESTROY message can trigger re-entrant calls to DestroyWindow
1812 * make sure that the window still exists when we come back.
1814 if (IsWindow(hwnd))
1816 HWND* pWndArray;
1817 int i;
1819 if (!(pWndArray = WIN_ListChildren( hwnd ))) return;
1821 for (i = 0; pWndArray[i]; i++)
1823 if (IsWindow( pWndArray[i] )) WIN_SendDestroyMsg( pWndArray[i] );
1825 HeapFree( GetProcessHeap(), 0, pWndArray );
1827 else
1828 WARN("\tdestroyed itself while in WM_DESTROY!\n");
1832 /***********************************************************************
1833 * DestroyWindow (USER32.@)
1835 BOOL WINAPI DestroyWindow( HWND hwnd )
1837 BOOL is_child;
1839 if (!(hwnd = WIN_IsCurrentThread( hwnd )) || is_desktop_window( hwnd ))
1841 SetLastError( ERROR_ACCESS_DENIED );
1842 return FALSE;
1845 TRACE("(%p)\n", hwnd);
1847 /* Call hooks */
1849 if (HOOK_CallHooks( WH_CBT, HCBT_DESTROYWND, (WPARAM)hwnd, 0, TRUE )) return FALSE;
1851 if (MENU_IsMenuActive() == hwnd)
1852 EndMenu();
1854 is_child = (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) != 0;
1856 if (is_child)
1858 if (!USER_IsExitingThread( GetCurrentThreadId() ))
1859 send_parent_notify( hwnd, WM_DESTROY );
1861 else if (!GetWindow( hwnd, GW_OWNER ))
1863 HOOK_CallHooks( WH_SHELL, HSHELL_WINDOWDESTROYED, (WPARAM)hwnd, 0L, TRUE );
1864 /* FIXME: clean up palette - see "Internals" p.352 */
1867 if (!IsWindow(hwnd)) return TRUE;
1869 /* Hide the window */
1870 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)
1872 /* Only child windows receive WM_SHOWWINDOW in DestroyWindow() */
1873 if (is_child)
1874 ShowWindow( hwnd, SW_HIDE );
1875 else
1876 SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE |
1877 SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW );
1880 if (!IsWindow(hwnd)) return TRUE;
1882 /* Recursively destroy owned windows */
1884 if (!is_child)
1886 for (;;)
1888 int i;
1889 BOOL got_one = FALSE;
1890 HWND *list = WIN_ListChildren( GetDesktopWindow() );
1891 if (list)
1893 for (i = 0; list[i]; i++)
1895 if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
1896 if (WIN_IsCurrentThread( list[i] ))
1898 DestroyWindow( list[i] );
1899 got_one = TRUE;
1900 continue;
1902 WIN_SetOwner( list[i], 0 );
1904 HeapFree( GetProcessHeap(), 0, list );
1906 if (!got_one) break;
1910 /* Send destroy messages */
1912 WIN_SendDestroyMsg( hwnd );
1913 if (!IsWindow( hwnd )) return TRUE;
1915 /* Destroy the window storage */
1917 WIN_DestroyWindow( hwnd );
1918 return TRUE;
1922 /***********************************************************************
1923 * CloseWindow (USER32.@)
1925 BOOL WINAPI CloseWindow( HWND hwnd )
1927 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) return FALSE;
1928 ShowWindow( hwnd, SW_MINIMIZE );
1929 return TRUE;
1933 /***********************************************************************
1934 * OpenIcon (USER32.@)
1936 BOOL WINAPI OpenIcon( HWND hwnd )
1938 if (!IsIconic( hwnd )) return FALSE;
1939 ShowWindow( hwnd, SW_SHOWNORMAL );
1940 return TRUE;
1944 /***********************************************************************
1945 * FindWindowExW (USER32.@)
1947 HWND WINAPI FindWindowExW( HWND parent, HWND child, LPCWSTR className, LPCWSTR title )
1949 HWND *list;
1950 HWND retvalue = 0;
1951 int i = 0, len = 0;
1952 WCHAR *buffer = NULL;
1954 if (!parent && child) parent = GetDesktopWindow();
1955 else if (parent == HWND_MESSAGE) parent = get_hwnd_message_parent();
1957 if (title)
1959 len = strlenW(title) + 1; /* one extra char to check for chars beyond the end */
1960 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return 0;
1963 if (!(list = list_window_children( 0, parent, className, 0 ))) goto done;
1965 if (child)
1967 child = WIN_GetFullHandle( child );
1968 while (list[i] && list[i] != child) i++;
1969 if (!list[i]) goto done;
1970 i++; /* start from next window */
1973 if (title)
1975 while (list[i])
1977 if (InternalGetWindowText( list[i], buffer, len + 1 ))
1979 if (!strcmpiW( buffer, title )) break;
1981 else
1983 if (!title[0]) break;
1985 i++;
1988 retvalue = list[i];
1990 done:
1991 HeapFree( GetProcessHeap(), 0, list );
1992 HeapFree( GetProcessHeap(), 0, buffer );
1993 return retvalue;
1998 /***********************************************************************
1999 * FindWindowA (USER32.@)
2001 HWND WINAPI FindWindowA( LPCSTR className, LPCSTR title )
2003 HWND ret = FindWindowExA( 0, 0, className, title );
2004 if (!ret) SetLastError (ERROR_CANNOT_FIND_WND_CLASS);
2005 return ret;
2009 /***********************************************************************
2010 * FindWindowExA (USER32.@)
2012 HWND WINAPI FindWindowExA( HWND parent, HWND child, LPCSTR className, LPCSTR title )
2014 LPWSTR titleW = NULL;
2015 HWND hwnd = 0;
2017 if (title)
2019 DWORD len = MultiByteToWideChar( CP_ACP, 0, title, -1, NULL, 0 );
2020 if (!(titleW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return 0;
2021 MultiByteToWideChar( CP_ACP, 0, title, -1, titleW, len );
2024 if (!IS_INTRESOURCE(className))
2026 WCHAR classW[256];
2027 if (MultiByteToWideChar( CP_ACP, 0, className, -1, classW, sizeof(classW)/sizeof(WCHAR) ))
2028 hwnd = FindWindowExW( parent, child, classW, titleW );
2030 else
2032 hwnd = FindWindowExW( parent, child, (LPCWSTR)className, titleW );
2035 HeapFree( GetProcessHeap(), 0, titleW );
2036 return hwnd;
2040 /***********************************************************************
2041 * FindWindowW (USER32.@)
2043 HWND WINAPI FindWindowW( LPCWSTR className, LPCWSTR title )
2045 return FindWindowExW( 0, 0, className, title );
2049 /**********************************************************************
2050 * GetDesktopWindow (USER32.@)
2052 HWND WINAPI GetDesktopWindow(void)
2054 struct user_thread_info *thread_info = get_user_thread_info();
2056 if (thread_info->top_window) return thread_info->top_window;
2058 SERVER_START_REQ( get_desktop_window )
2060 req->force = 0;
2061 if (!wine_server_call( req ))
2063 thread_info->top_window = wine_server_ptr_handle( reply->top_window );
2064 thread_info->msg_window = wine_server_ptr_handle( reply->msg_window );
2067 SERVER_END_REQ;
2069 if (!thread_info->top_window)
2071 static const WCHAR explorer[] = {'\\','e','x','p','l','o','r','e','r','.','e','x','e',0};
2072 static const WCHAR args[] = {' ','/','d','e','s','k','t','o','p',0};
2073 STARTUPINFOW si;
2074 PROCESS_INFORMATION pi;
2075 WCHAR windir[MAX_PATH];
2076 WCHAR app[MAX_PATH + sizeof(explorer)/sizeof(WCHAR)];
2077 WCHAR cmdline[MAX_PATH + (sizeof(explorer) + sizeof(args))/sizeof(WCHAR)];
2078 WCHAR desktop[MAX_PATH];
2079 void *redir;
2081 SERVER_START_REQ( set_user_object_info )
2083 req->handle = wine_server_obj_handle( GetThreadDesktop(GetCurrentThreadId()) );
2084 req->flags = SET_USER_OBJECT_GET_FULL_NAME;
2085 wine_server_set_reply( req, desktop, sizeof(desktop) - sizeof(WCHAR) );
2086 if (!wine_server_call( req ))
2088 size_t size = wine_server_reply_size( reply );
2089 desktop[size / sizeof(WCHAR)] = 0;
2090 TRACE( "starting explorer for desktop %s\n", debugstr_w(desktop) );
2092 else
2093 desktop[0] = 0;
2095 SERVER_END_REQ;
2097 memset( &si, 0, sizeof(si) );
2098 si.cb = sizeof(si);
2099 si.lpDesktop = *desktop ? desktop : NULL;
2100 si.dwFlags = STARTF_USESTDHANDLES;
2101 si.hStdInput = 0;
2102 si.hStdOutput = 0;
2103 si.hStdError = GetStdHandle( STD_ERROR_HANDLE );
2105 GetSystemDirectoryW( windir, MAX_PATH );
2106 strcpyW( app, windir );
2107 strcatW( app, explorer );
2108 strcpyW( cmdline, app );
2109 strcatW( cmdline, args );
2111 Wow64DisableWow64FsRedirection( &redir );
2112 if (CreateProcessW( app, cmdline, NULL, NULL, FALSE, DETACHED_PROCESS,
2113 NULL, windir, &si, &pi ))
2115 TRACE( "started explorer pid %04x tid %04x\n", pi.dwProcessId, pi.dwThreadId );
2116 WaitForInputIdle( pi.hProcess, 10000 );
2117 CloseHandle( pi.hThread );
2118 CloseHandle( pi.hProcess );
2120 else WARN( "failed to start explorer, err %d\n", GetLastError() );
2121 Wow64RevertWow64FsRedirection( redir );
2123 SERVER_START_REQ( get_desktop_window )
2125 req->force = 1;
2126 if (!wine_server_call( req ))
2128 thread_info->top_window = wine_server_ptr_handle( reply->top_window );
2129 thread_info->msg_window = wine_server_ptr_handle( reply->msg_window );
2132 SERVER_END_REQ;
2135 if (!thread_info->top_window || !USER_Driver->pCreateDesktopWindow( thread_info->top_window ))
2136 ERR( "failed to create desktop window\n" );
2138 return thread_info->top_window;
2142 /*******************************************************************
2143 * EnableWindow (USER32.@)
2145 BOOL WINAPI EnableWindow( HWND hwnd, BOOL enable )
2147 BOOL retvalue;
2149 if (is_broadcast(hwnd))
2151 SetLastError( ERROR_INVALID_PARAMETER );
2152 return FALSE;
2155 TRACE("( %p, %d )\n", hwnd, enable);
2157 if (enable)
2159 retvalue = (WIN_SetStyle( hwnd, 0, WS_DISABLED ) & WS_DISABLED) != 0;
2160 if (retvalue) SendMessageW( hwnd, WM_ENABLE, TRUE, 0 );
2162 else
2164 SendMessageW( hwnd, WM_CANCELMODE, 0, 0 );
2166 retvalue = (WIN_SetStyle( hwnd, WS_DISABLED, 0 ) & WS_DISABLED) != 0;
2167 if (!retvalue)
2169 if (hwnd == GetFocus())
2170 SetFocus( 0 ); /* A disabled window can't have the focus */
2172 SendMessageW( hwnd, WM_ENABLE, FALSE, 0 );
2175 return retvalue;
2179 /***********************************************************************
2180 * IsWindowEnabled (USER32.@)
2182 BOOL WINAPI IsWindowEnabled(HWND hWnd)
2184 return !(GetWindowLongW( hWnd, GWL_STYLE ) & WS_DISABLED);
2188 /***********************************************************************
2189 * IsWindowUnicode (USER32.@)
2191 BOOL WINAPI IsWindowUnicode( HWND hwnd )
2193 WND * wndPtr;
2194 BOOL retvalue = FALSE;
2196 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
2198 if (wndPtr == WND_DESKTOP) return TRUE;
2200 if (wndPtr != WND_OTHER_PROCESS)
2202 retvalue = (wndPtr->flags & WIN_ISUNICODE) != 0;
2203 WIN_ReleasePtr( wndPtr );
2205 else
2207 SERVER_START_REQ( get_window_info )
2209 req->handle = wine_server_user_handle( hwnd );
2210 if (!wine_server_call_err( req )) retvalue = reply->is_unicode;
2212 SERVER_END_REQ;
2214 return retvalue;
2218 /**********************************************************************
2219 * WIN_GetWindowLong
2221 * Helper function for GetWindowLong().
2223 static LONG_PTR WIN_GetWindowLong( HWND hwnd, INT offset, UINT size, BOOL unicode )
2225 LONG_PTR retvalue = 0;
2226 WND *wndPtr;
2228 if (offset == GWLP_HWNDPARENT)
2230 HWND parent = GetAncestor( hwnd, GA_PARENT );
2231 if (parent == GetDesktopWindow()) parent = GetWindow( hwnd, GW_OWNER );
2232 return (ULONG_PTR)parent;
2235 if (!(wndPtr = WIN_GetPtr( hwnd )))
2237 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2238 return 0;
2241 if (wndPtr == WND_DESKTOP)
2243 switch (offset)
2245 case GWL_STYLE:
2246 retvalue = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN; /* message parent is not visible */
2247 if (WIN_GetFullHandle( hwnd ) == GetDesktopWindow())
2248 retvalue |= WS_VISIBLE;
2249 return retvalue;
2250 case GWL_EXSTYLE:
2251 case GWLP_USERDATA:
2252 case GWLP_ID:
2253 case GWLP_HINSTANCE:
2254 return 0;
2255 case GWLP_WNDPROC:
2256 SetLastError( ERROR_ACCESS_DENIED );
2257 return 0;
2259 SetLastError( ERROR_INVALID_INDEX );
2260 return 0;
2263 if (wndPtr == WND_OTHER_PROCESS)
2265 if (offset == GWLP_WNDPROC)
2267 SetLastError( ERROR_ACCESS_DENIED );
2268 return 0;
2270 SERVER_START_REQ( set_window_info )
2272 req->handle = wine_server_user_handle( hwnd );
2273 req->flags = 0; /* don't set anything, just retrieve */
2274 req->extra_offset = (offset >= 0) ? offset : -1;
2275 req->extra_size = (offset >= 0) ? size : 0;
2276 if (!wine_server_call_err( req ))
2278 switch(offset)
2280 case GWL_STYLE: retvalue = reply->old_style; break;
2281 case GWL_EXSTYLE: retvalue = reply->old_ex_style; break;
2282 case GWLP_ID: retvalue = reply->old_id; break;
2283 case GWLP_HINSTANCE: retvalue = (ULONG_PTR)wine_server_get_ptr( reply->old_instance ); break;
2284 case GWLP_USERDATA: retvalue = reply->old_user_data; break;
2285 default:
2286 if (offset >= 0) retvalue = get_win_data( &reply->old_extra_value, size );
2287 else SetLastError( ERROR_INVALID_INDEX );
2288 break;
2292 SERVER_END_REQ;
2293 return retvalue;
2296 /* now we have a valid wndPtr */
2298 if (offset >= 0)
2300 if (offset > (int)(wndPtr->cbWndExtra - size))
2302 WARN("Invalid offset %d\n", offset );
2303 WIN_ReleasePtr( wndPtr );
2304 SetLastError( ERROR_INVALID_INDEX );
2305 return 0;
2307 retvalue = get_win_data( (char *)wndPtr->wExtra + offset, size );
2309 /* Special case for dialog window procedure */
2310 if ((offset == DWLP_DLGPROC) && (size == sizeof(LONG_PTR)) && wndPtr->dlgInfo)
2311 retvalue = (LONG_PTR)WINPROC_GetProc( (WNDPROC)retvalue, unicode );
2312 WIN_ReleasePtr( wndPtr );
2313 return retvalue;
2316 switch(offset)
2318 case GWLP_USERDATA: retvalue = wndPtr->userdata; break;
2319 case GWL_STYLE: retvalue = wndPtr->dwStyle; break;
2320 case GWL_EXSTYLE: retvalue = wndPtr->dwExStyle; break;
2321 case GWLP_ID: retvalue = wndPtr->wIDmenu; break;
2322 case GWLP_HINSTANCE: retvalue = (ULONG_PTR)wndPtr->hInstance; break;
2323 case GWLP_WNDPROC:
2324 /* This looks like a hack only for the edit control (see tests). This makes these controls
2325 * more tolerant to A/W mismatches. The lack of W->A->W conversion for such a mismatch suggests
2326 * that the hack is in GetWindowLongPtr[AW], not in winprocs.
2328 if (wndPtr->winproc == BUILTIN_WINPROC(WINPROC_EDIT) && (!unicode != !(wndPtr->flags & WIN_ISUNICODE)))
2329 retvalue = (ULONG_PTR)wndPtr->winproc;
2330 else
2331 retvalue = (ULONG_PTR)WINPROC_GetProc( wndPtr->winproc, unicode );
2332 break;
2333 default:
2334 WARN("Unknown offset %d\n", offset );
2335 SetLastError( ERROR_INVALID_INDEX );
2336 break;
2338 WIN_ReleasePtr(wndPtr);
2339 return retvalue;
2343 /**********************************************************************
2344 * WIN_SetWindowLong
2346 * Helper function for SetWindowLong().
2348 * 0 is the failure code. However, in the case of failure SetLastError
2349 * must be set to distinguish between a 0 return value and a failure.
2351 LONG_PTR WIN_SetWindowLong( HWND hwnd, INT offset, UINT size, LONG_PTR newval, BOOL unicode )
2353 STYLESTRUCT style;
2354 BOOL ok, made_visible = FALSE;
2355 LONG_PTR retval = 0;
2356 WND *wndPtr;
2358 TRACE( "%p %d %lx %c\n", hwnd, offset, newval, unicode ? 'W' : 'A' );
2360 if (is_broadcast(hwnd))
2362 SetLastError( ERROR_INVALID_PARAMETER );
2363 return FALSE;
2366 if (!(wndPtr = WIN_GetPtr( hwnd )))
2368 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2369 return 0;
2371 if (wndPtr == WND_DESKTOP)
2373 /* can't change anything on the desktop window */
2374 SetLastError( ERROR_ACCESS_DENIED );
2375 return 0;
2377 if (wndPtr == WND_OTHER_PROCESS)
2379 if (offset == GWLP_WNDPROC)
2381 SetLastError( ERROR_ACCESS_DENIED );
2382 return 0;
2384 if (offset > 32767 || offset < -32767)
2386 SetLastError( ERROR_INVALID_INDEX );
2387 return 0;
2389 return SendMessageW( hwnd, WM_WINE_SETWINDOWLONG, MAKEWPARAM( offset, size ), newval );
2392 /* first some special cases */
2393 switch( offset )
2395 case GWL_STYLE:
2396 style.styleOld = wndPtr->dwStyle;
2397 style.styleNew = newval;
2398 WIN_ReleasePtr( wndPtr );
2399 SendMessageW( hwnd, WM_STYLECHANGING, GWL_STYLE, (LPARAM)&style );
2400 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
2401 newval = style.styleNew;
2402 /* WS_CLIPSIBLINGS can't be reset on top-level windows */
2403 if (wndPtr->parent == GetDesktopWindow()) newval |= WS_CLIPSIBLINGS;
2404 /* WS_MINIMIZE can't be reset */
2405 if (wndPtr->dwStyle & WS_MINIMIZE) newval |= WS_MINIMIZE;
2406 /* FIXME: changing WS_DLGFRAME | WS_THICKFRAME is supposed to change
2407 WS_EX_WINDOWEDGE too */
2408 break;
2409 case GWL_EXSTYLE:
2410 style.styleOld = wndPtr->dwExStyle;
2411 style.styleNew = newval;
2412 WIN_ReleasePtr( wndPtr );
2413 SendMessageW( hwnd, WM_STYLECHANGING, GWL_EXSTYLE, (LPARAM)&style );
2414 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
2415 /* WS_EX_TOPMOST can only be changed through SetWindowPos */
2416 newval = (style.styleNew & ~WS_EX_TOPMOST) | (wndPtr->dwExStyle & WS_EX_TOPMOST);
2417 /* WS_EX_WINDOWEDGE depends on some other styles */
2418 if (newval & WS_EX_DLGMODALFRAME)
2419 newval |= WS_EX_WINDOWEDGE;
2420 else if (!(newval & WS_EX_STATICEDGE) && (wndPtr->dwStyle & (WS_DLGFRAME | WS_THICKFRAME)))
2421 newval |= WS_EX_WINDOWEDGE;
2422 else
2423 newval &= ~WS_EX_WINDOWEDGE;
2424 break;
2425 case GWLP_HWNDPARENT:
2426 if (wndPtr->parent == GetDesktopWindow())
2428 WIN_ReleasePtr( wndPtr );
2429 return (ULONG_PTR)WIN_SetOwner( hwnd, (HWND)newval );
2431 else
2433 WIN_ReleasePtr( wndPtr );
2434 return (ULONG_PTR)SetParent( hwnd, (HWND)newval );
2436 case GWLP_WNDPROC:
2438 WNDPROC proc;
2439 UINT old_flags = wndPtr->flags;
2440 retval = WIN_GetWindowLong( hwnd, offset, size, unicode );
2441 proc = WINPROC_AllocProc( (WNDPROC)newval, unicode );
2442 if (proc) wndPtr->winproc = proc;
2443 if (WINPROC_IsUnicode( proc, unicode )) wndPtr->flags |= WIN_ISUNICODE;
2444 else wndPtr->flags &= ~WIN_ISUNICODE;
2445 if (!((old_flags ^ wndPtr->flags) & WIN_ISUNICODE))
2447 WIN_ReleasePtr( wndPtr );
2448 return retval;
2450 /* update is_unicode flag on the server side */
2451 break;
2453 case GWLP_ID:
2454 case GWLP_HINSTANCE:
2455 case GWLP_USERDATA:
2456 break;
2457 case DWLP_DLGPROC:
2458 if ((wndPtr->cbWndExtra - sizeof(LONG_PTR) >= DWLP_DLGPROC) &&
2459 (size == sizeof(LONG_PTR)) && wndPtr->dlgInfo)
2461 WNDPROC *ptr = (WNDPROC *)((char *)wndPtr->wExtra + DWLP_DLGPROC);
2462 retval = (ULONG_PTR)WINPROC_GetProc( *ptr, unicode );
2463 *ptr = WINPROC_AllocProc( (WNDPROC)newval, unicode );
2464 WIN_ReleasePtr( wndPtr );
2465 return retval;
2467 /* fall through */
2468 default:
2469 if (offset < 0 || offset > (int)(wndPtr->cbWndExtra - size))
2471 WARN("Invalid offset %d\n", offset );
2472 WIN_ReleasePtr( wndPtr );
2473 SetLastError( ERROR_INVALID_INDEX );
2474 return 0;
2476 else if (get_win_data( (char *)wndPtr->wExtra + offset, size ) == newval)
2478 /* already set to the same value */
2479 WIN_ReleasePtr( wndPtr );
2480 return newval;
2482 break;
2485 SERVER_START_REQ( set_window_info )
2487 req->handle = wine_server_user_handle( hwnd );
2488 req->extra_offset = -1;
2489 switch(offset)
2491 case GWL_STYLE:
2492 req->flags = SET_WIN_STYLE;
2493 req->style = newval;
2494 break;
2495 case GWL_EXSTYLE:
2496 req->flags = SET_WIN_EXSTYLE;
2497 req->ex_style = newval;
2498 break;
2499 case GWLP_ID:
2500 req->flags = SET_WIN_ID;
2501 req->id = newval;
2502 break;
2503 case GWLP_HINSTANCE:
2504 req->flags = SET_WIN_INSTANCE;
2505 req->instance = wine_server_client_ptr( (void *)newval );
2506 break;
2507 case GWLP_WNDPROC:
2508 req->flags = SET_WIN_UNICODE;
2509 req->is_unicode = (wndPtr->flags & WIN_ISUNICODE) != 0;
2510 break;
2511 case GWLP_USERDATA:
2512 req->flags = SET_WIN_USERDATA;
2513 req->user_data = newval;
2514 break;
2515 default:
2516 req->flags = SET_WIN_EXTRA;
2517 req->extra_offset = offset;
2518 req->extra_size = size;
2519 set_win_data( &req->extra_value, newval, size );
2521 if ((ok = !wine_server_call_err( req )))
2523 switch(offset)
2525 case GWL_STYLE:
2526 wndPtr->dwStyle = newval;
2527 retval = reply->old_style;
2528 break;
2529 case GWL_EXSTYLE:
2530 wndPtr->dwExStyle = newval;
2531 retval = reply->old_ex_style;
2532 break;
2533 case GWLP_ID:
2534 wndPtr->wIDmenu = newval;
2535 retval = reply->old_id;
2536 break;
2537 case GWLP_HINSTANCE:
2538 wndPtr->hInstance = (HINSTANCE)newval;
2539 retval = (ULONG_PTR)wine_server_get_ptr( reply->old_instance );
2540 break;
2541 case GWLP_WNDPROC:
2542 break;
2543 case GWLP_USERDATA:
2544 wndPtr->userdata = newval;
2545 retval = reply->old_user_data;
2546 break;
2547 default:
2548 retval = get_win_data( (char *)wndPtr->wExtra + offset, size );
2549 set_win_data( (char *)wndPtr->wExtra + offset, newval, size );
2550 break;
2554 SERVER_END_REQ;
2556 if ((offset == GWL_STYLE && ((style.styleOld ^ style.styleNew) & WS_VISIBLE)) ||
2557 (offset == GWL_EXSTYLE && ((style.styleOld ^ style.styleNew) & WS_EX_LAYERED)))
2559 made_visible = (wndPtr->dwStyle & WS_VISIBLE) != 0;
2560 invalidate_dce( wndPtr, NULL );
2562 WIN_ReleasePtr( wndPtr );
2564 if (!ok) return 0;
2566 if (offset == GWL_STYLE || offset == GWL_EXSTYLE)
2568 style.styleOld = retval;
2569 style.styleNew = newval;
2570 USER_Driver->pSetWindowStyle( hwnd, offset, &style );
2571 if (made_visible) update_window_state( hwnd );
2572 SendMessageW( hwnd, WM_STYLECHANGED, offset, (LPARAM)&style );
2575 return retval;
2579 /**********************************************************************
2580 * GetWindowWord (USER32.@)
2582 WORD WINAPI GetWindowWord( HWND hwnd, INT offset )
2584 switch(offset)
2586 case GWLP_ID:
2587 case GWLP_HINSTANCE:
2588 case GWLP_HWNDPARENT:
2589 break;
2590 default:
2591 if (offset < 0)
2593 WARN("Invalid offset %d\n", offset );
2594 SetLastError( ERROR_INVALID_INDEX );
2595 return 0;
2597 break;
2599 return WIN_GetWindowLong( hwnd, offset, sizeof(WORD), FALSE );
2603 /**********************************************************************
2604 * GetWindowLongA (USER32.@)
2606 LONG WINAPI GetWindowLongA( HWND hwnd, INT offset )
2608 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG), FALSE );
2612 /**********************************************************************
2613 * GetWindowLongW (USER32.@)
2615 LONG WINAPI GetWindowLongW( HWND hwnd, INT offset )
2617 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG), TRUE );
2621 /**********************************************************************
2622 * SetWindowWord (USER32.@)
2624 WORD WINAPI SetWindowWord( HWND hwnd, INT offset, WORD newval )
2626 switch(offset)
2628 case GWLP_ID:
2629 case GWLP_HINSTANCE:
2630 case GWLP_HWNDPARENT:
2631 break;
2632 default:
2633 if (offset < 0)
2635 WARN("Invalid offset %d\n", offset );
2636 SetLastError( ERROR_INVALID_INDEX );
2637 return 0;
2639 break;
2641 return WIN_SetWindowLong( hwnd, offset, sizeof(WORD), newval, FALSE );
2645 /**********************************************************************
2646 * SetWindowLongA (USER32.@)
2648 * See SetWindowLongW.
2650 LONG WINAPI DECLSPEC_HOTPATCH SetWindowLongA( HWND hwnd, INT offset, LONG newval )
2652 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG), newval, FALSE );
2656 /**********************************************************************
2657 * SetWindowLongW (USER32.@) Set window attribute
2659 * SetWindowLong() alters one of a window's attributes or sets a 32-bit (long)
2660 * value in a window's extra memory.
2662 * The _hwnd_ parameter specifies the handle to a window that
2663 * has extra memory. The _newval_ parameter contains the new
2664 * attribute or extra memory value. If positive, the _offset_
2665 * parameter is the byte-addressed location in the window's extra
2666 * memory to set. If negative, _offset_ specifies the window
2667 * attribute to set, and should be one of the following values:
2669 * GWL_EXSTYLE The window's extended window style
2671 * GWL_STYLE The window's window style.
2673 * GWLP_WNDPROC Pointer to the window's window procedure.
2675 * GWLP_HINSTANCE The window's application instance handle.
2677 * GWLP_ID The window's identifier.
2679 * GWLP_USERDATA The window's user-specified data.
2681 * If the window is a dialog box, the _offset_ parameter can be one of
2682 * the following values:
2684 * DWLP_DLGPROC The address of the window's dialog box procedure.
2686 * DWLP_MSGRESULT The return value of a message
2687 * that the dialog box procedure processed.
2689 * DWLP_USER Application specific information.
2691 * RETURNS
2693 * If successful, returns the previous value located at _offset_. Otherwise,
2694 * returns 0.
2696 * NOTES
2698 * Extra memory for a window class is specified by a nonzero cbWndExtra
2699 * parameter of the WNDCLASS structure passed to RegisterClass() at the
2700 * time of class creation.
2702 * Using GWL_WNDPROC to set a new window procedure effectively creates
2703 * a window subclass. Use CallWindowProc() in the new windows procedure
2704 * to pass messages to the superclass's window procedure.
2706 * The user data is reserved for use by the application which created
2707 * the window.
2709 * Do not use GWL_STYLE to change the window's WS_DISABLED style;
2710 * instead, call the EnableWindow() function to change the window's
2711 * disabled state.
2713 * Do not use GWL_HWNDPARENT to reset the window's parent, use
2714 * SetParent() instead.
2716 * Win95:
2717 * When offset is GWL_STYLE and the calling app's ver is 4.0,
2718 * it sends WM_STYLECHANGING before changing the settings
2719 * and WM_STYLECHANGED afterwards.
2720 * App ver 4.0 can't use SetWindowLong to change WS_EX_TOPMOST.
2722 LONG WINAPI DECLSPEC_HOTPATCH SetWindowLongW(
2723 HWND hwnd, /* [in] window to alter */
2724 INT offset, /* [in] offset, in bytes, of location to alter */
2725 LONG newval /* [in] new value of location */
2727 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG), newval, TRUE );
2731 /*******************************************************************
2732 * GetWindowTextA (USER32.@)
2734 INT WINAPI GetWindowTextA( HWND hwnd, LPSTR lpString, INT nMaxCount )
2736 WCHAR *buffer;
2738 if (!lpString || nMaxCount <= 0) return 0;
2740 if (WIN_IsCurrentProcess( hwnd ))
2742 lpString[0] = 0;
2743 return (INT)SendMessageA( hwnd, WM_GETTEXT, nMaxCount, (LPARAM)lpString );
2746 /* when window belongs to other process, don't send a message */
2747 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, nMaxCount * sizeof(WCHAR) ))) return 0;
2748 get_server_window_text( hwnd, buffer, nMaxCount );
2749 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, lpString, nMaxCount, NULL, NULL ))
2750 lpString[nMaxCount-1] = 0;
2751 HeapFree( GetProcessHeap(), 0, buffer );
2752 return strlen(lpString);
2756 /*******************************************************************
2757 * InternalGetWindowText (USER32.@)
2759 INT WINAPI InternalGetWindowText(HWND hwnd,LPWSTR lpString,INT nMaxCount )
2761 WND *win;
2763 if (nMaxCount <= 0) return 0;
2764 if (!(win = WIN_GetPtr( hwnd ))) return 0;
2765 if (win == WND_DESKTOP) lpString[0] = 0;
2766 else if (win != WND_OTHER_PROCESS)
2768 if (win->text) lstrcpynW( lpString, win->text, nMaxCount );
2769 else lpString[0] = 0;
2770 WIN_ReleasePtr( win );
2772 else
2774 get_server_window_text( hwnd, lpString, nMaxCount );
2776 return strlenW(lpString);
2780 /*******************************************************************
2781 * GetWindowTextW (USER32.@)
2783 INT WINAPI GetWindowTextW( HWND hwnd, LPWSTR lpString, INT nMaxCount )
2785 if (!lpString || nMaxCount <= 0) return 0;
2787 if (WIN_IsCurrentProcess( hwnd ))
2789 lpString[0] = 0;
2790 return (INT)SendMessageW( hwnd, WM_GETTEXT, nMaxCount, (LPARAM)lpString );
2793 /* when window belongs to other process, don't send a message */
2794 get_server_window_text( hwnd, lpString, nMaxCount );
2795 return strlenW(lpString);
2799 /*******************************************************************
2800 * SetWindowTextA (USER32.@)
2801 * SetWindowText (USER32.@)
2803 BOOL WINAPI DECLSPEC_HOTPATCH SetWindowTextA( HWND hwnd, LPCSTR lpString )
2805 if (is_broadcast(hwnd))
2807 SetLastError( ERROR_INVALID_PARAMETER );
2808 return FALSE;
2810 if (!WIN_IsCurrentProcess( hwnd ))
2811 WARN( "setting text %s of other process window %p should not use SendMessage\n",
2812 debugstr_a(lpString), hwnd );
2813 return (BOOL)SendMessageA( hwnd, WM_SETTEXT, 0, (LPARAM)lpString );
2817 /*******************************************************************
2818 * SetWindowTextW (USER32.@)
2820 BOOL WINAPI DECLSPEC_HOTPATCH SetWindowTextW( HWND hwnd, LPCWSTR lpString )
2822 if (is_broadcast(hwnd))
2824 SetLastError( ERROR_INVALID_PARAMETER );
2825 return FALSE;
2827 if (!WIN_IsCurrentProcess( hwnd ))
2828 WARN( "setting text %s of other process window %p should not use SendMessage\n",
2829 debugstr_w(lpString), hwnd );
2830 return (BOOL)SendMessageW( hwnd, WM_SETTEXT, 0, (LPARAM)lpString );
2834 /*******************************************************************
2835 * GetWindowTextLengthA (USER32.@)
2837 INT WINAPI GetWindowTextLengthA( HWND hwnd )
2839 return SendMessageA( hwnd, WM_GETTEXTLENGTH, 0, 0 );
2842 /*******************************************************************
2843 * GetWindowTextLengthW (USER32.@)
2845 INT WINAPI GetWindowTextLengthW( HWND hwnd )
2847 return SendMessageW( hwnd, WM_GETTEXTLENGTH, 0, 0 );
2851 /*******************************************************************
2852 * IsWindow (USER32.@)
2854 BOOL WINAPI IsWindow( HWND hwnd )
2856 WND *ptr;
2857 BOOL ret;
2859 if (!(ptr = WIN_GetPtr( hwnd ))) return FALSE;
2860 if (ptr == WND_DESKTOP) return TRUE;
2862 if (ptr != WND_OTHER_PROCESS)
2864 WIN_ReleasePtr( ptr );
2865 return TRUE;
2868 /* check other processes */
2869 SERVER_START_REQ( get_window_info )
2871 req->handle = wine_server_user_handle( hwnd );
2872 ret = !wine_server_call_err( req );
2874 SERVER_END_REQ;
2875 return ret;
2879 /***********************************************************************
2880 * GetWindowThreadProcessId (USER32.@)
2882 DWORD WINAPI GetWindowThreadProcessId( HWND hwnd, LPDWORD process )
2884 WND *ptr;
2885 DWORD tid = 0;
2887 if (!(ptr = WIN_GetPtr( hwnd )))
2889 SetLastError( ERROR_INVALID_WINDOW_HANDLE);
2890 return 0;
2893 if (ptr != WND_OTHER_PROCESS && ptr != WND_DESKTOP)
2895 /* got a valid window */
2896 tid = ptr->tid;
2897 if (process) *process = GetCurrentProcessId();
2898 WIN_ReleasePtr( ptr );
2899 return tid;
2902 /* check other processes */
2903 SERVER_START_REQ( get_window_info )
2905 req->handle = wine_server_user_handle( hwnd );
2906 if (!wine_server_call_err( req ))
2908 tid = (DWORD)reply->tid;
2909 if (process) *process = (DWORD)reply->pid;
2912 SERVER_END_REQ;
2913 return tid;
2917 /*****************************************************************
2918 * GetParent (USER32.@)
2920 HWND WINAPI GetParent( HWND hwnd )
2922 WND *wndPtr;
2923 HWND retvalue = 0;
2925 if (!(wndPtr = WIN_GetPtr( hwnd )))
2927 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2928 return 0;
2930 if (wndPtr == WND_DESKTOP) return 0;
2931 if (wndPtr == WND_OTHER_PROCESS)
2933 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
2934 if (style & (WS_POPUP | WS_CHILD))
2936 SERVER_START_REQ( get_window_tree )
2938 req->handle = wine_server_user_handle( hwnd );
2939 if (!wine_server_call_err( req ))
2941 if (style & WS_POPUP) retvalue = wine_server_ptr_handle( reply->owner );
2942 else if (style & WS_CHILD) retvalue = wine_server_ptr_handle( reply->parent );
2945 SERVER_END_REQ;
2948 else
2950 if (wndPtr->dwStyle & WS_POPUP) retvalue = wndPtr->owner;
2951 else if (wndPtr->dwStyle & WS_CHILD) retvalue = wndPtr->parent;
2952 WIN_ReleasePtr( wndPtr );
2954 return retvalue;
2958 /*****************************************************************
2959 * GetAncestor (USER32.@)
2961 HWND WINAPI GetAncestor( HWND hwnd, UINT type )
2963 WND *win;
2964 HWND *list, ret = 0;
2966 switch(type)
2968 case GA_PARENT:
2969 if (!(win = WIN_GetPtr( hwnd )))
2971 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2972 return 0;
2974 if (win == WND_DESKTOP) return 0;
2975 if (win != WND_OTHER_PROCESS)
2977 ret = win->parent;
2978 WIN_ReleasePtr( win );
2980 else /* need to query the server */
2982 SERVER_START_REQ( get_window_tree )
2984 req->handle = wine_server_user_handle( hwnd );
2985 if (!wine_server_call_err( req )) ret = wine_server_ptr_handle( reply->parent );
2987 SERVER_END_REQ;
2989 break;
2991 case GA_ROOT:
2992 if (!(list = list_window_parents( hwnd ))) return 0;
2994 if (!list[0] || !list[1]) ret = WIN_GetFullHandle( hwnd ); /* top-level window */
2995 else
2997 int count = 2;
2998 while (list[count]) count++;
2999 ret = list[count - 2]; /* get the one before the desktop */
3001 HeapFree( GetProcessHeap(), 0, list );
3002 break;
3004 case GA_ROOTOWNER:
3005 if (is_desktop_window( hwnd )) return 0;
3006 ret = WIN_GetFullHandle( hwnd );
3007 for (;;)
3009 HWND parent = GetParent( ret );
3010 if (!parent) break;
3011 ret = parent;
3013 break;
3015 return ret;
3019 /*****************************************************************
3020 * SetParent (USER32.@)
3022 HWND WINAPI SetParent( HWND hwnd, HWND parent )
3024 HWND full_handle;
3025 HWND old_parent = 0;
3026 BOOL was_visible;
3027 WND *wndPtr;
3028 POINT pt;
3029 BOOL ret;
3031 TRACE("(%p %p)\n", hwnd, parent);
3033 if (is_broadcast(hwnd) || is_broadcast(parent))
3035 SetLastError(ERROR_INVALID_PARAMETER);
3036 return 0;
3039 if (!parent) parent = GetDesktopWindow();
3040 else if (parent == HWND_MESSAGE) parent = get_hwnd_message_parent();
3041 else parent = WIN_GetFullHandle( parent );
3043 if (!IsWindow( parent ))
3045 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3046 return 0;
3049 /* Some applications try to set a child as a parent */
3050 if (IsChild(hwnd, parent))
3052 SetLastError( ERROR_INVALID_PARAMETER );
3053 return 0;
3056 if (!(full_handle = WIN_IsCurrentThread( hwnd )))
3057 return (HWND)SendMessageW( hwnd, WM_WINE_SETPARENT, (WPARAM)parent, 0 );
3059 if (full_handle == parent)
3061 SetLastError( ERROR_INVALID_PARAMETER );
3062 return 0;
3065 /* Windows hides the window first, then shows it again
3066 * including the WM_SHOWWINDOW messages and all */
3067 was_visible = ShowWindow( hwnd, SW_HIDE );
3069 wndPtr = WIN_GetPtr( hwnd );
3070 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return 0;
3072 pt.x = wndPtr->rectWindow.left;
3073 pt.y = wndPtr->rectWindow.top;
3075 SERVER_START_REQ( set_parent )
3077 req->handle = wine_server_user_handle( hwnd );
3078 req->parent = wine_server_user_handle( parent );
3079 if ((ret = !wine_server_call( req )))
3081 old_parent = wine_server_ptr_handle( reply->old_parent );
3082 wndPtr->parent = parent = wine_server_ptr_handle( reply->full_parent );
3086 SERVER_END_REQ;
3087 WIN_ReleasePtr( wndPtr );
3088 if (!ret) return 0;
3090 USER_Driver->pSetParent( full_handle, parent, old_parent );
3092 /* SetParent additionally needs to make hwnd the topmost window
3093 in the x-order and send the expected WM_WINDOWPOSCHANGING and
3094 WM_WINDOWPOSCHANGED notification messages.
3096 SetWindowPos( hwnd, HWND_TOP, pt.x, pt.y, 0, 0, SWP_NOSIZE );
3098 if (was_visible) ShowWindow( hwnd, SW_SHOW );
3100 return old_parent;
3104 /*******************************************************************
3105 * IsChild (USER32.@)
3107 BOOL WINAPI IsChild( HWND parent, HWND child )
3109 HWND *list;
3110 int i;
3111 BOOL ret = FALSE;
3113 if (!(GetWindowLongW( child, GWL_STYLE ) & WS_CHILD)) return FALSE;
3114 if (!(list = list_window_parents( child ))) return FALSE;
3115 parent = WIN_GetFullHandle( parent );
3116 for (i = 0; list[i]; i++)
3118 if (list[i] == parent)
3120 ret = list[i] && list[i+1];
3121 break;
3123 if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_CHILD)) break;
3125 HeapFree( GetProcessHeap(), 0, list );
3126 return ret;
3130 /***********************************************************************
3131 * IsWindowVisible (USER32.@)
3133 BOOL WINAPI IsWindowVisible( HWND hwnd )
3135 HWND *list;
3136 BOOL retval = TRUE;
3137 int i;
3139 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)) return FALSE;
3140 if (!(list = list_window_parents( hwnd ))) return TRUE;
3141 if (list[0])
3143 for (i = 0; list[i+1]; i++)
3144 if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_VISIBLE)) break;
3145 retval = !list[i+1] && (list[i] == GetDesktopWindow()); /* top message window isn't visible */
3147 HeapFree( GetProcessHeap(), 0, list );
3148 return retval;
3152 /***********************************************************************
3153 * WIN_IsWindowDrawable
3155 * hwnd is drawable when it is visible, all parents are not
3156 * minimized, and it is itself not minimized unless we are
3157 * trying to draw its default class icon.
3159 BOOL WIN_IsWindowDrawable( HWND hwnd, BOOL icon )
3161 HWND *list;
3162 BOOL retval = TRUE;
3163 int i;
3164 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
3166 if (!(style & WS_VISIBLE)) return FALSE;
3167 if ((style & WS_MINIMIZE) && icon && GetClassLongPtrW( hwnd, GCLP_HICON )) return FALSE;
3169 if (!(list = list_window_parents( hwnd ))) return TRUE;
3170 if (list[0])
3172 for (i = 0; list[i+1]; i++)
3173 if ((GetWindowLongW( list[i], GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != WS_VISIBLE)
3174 break;
3175 retval = !list[i+1] && (list[i] == GetDesktopWindow()); /* top message window isn't visible */
3177 HeapFree( GetProcessHeap(), 0, list );
3178 return retval;
3182 /*******************************************************************
3183 * GetTopWindow (USER32.@)
3185 HWND WINAPI GetTopWindow( HWND hwnd )
3187 if (!hwnd) hwnd = GetDesktopWindow();
3188 return GetWindow( hwnd, GW_CHILD );
3192 /*******************************************************************
3193 * GetWindow (USER32.@)
3195 HWND WINAPI GetWindow( HWND hwnd, UINT rel )
3197 HWND retval = 0;
3199 if (rel == GW_OWNER) /* this one may be available locally */
3201 WND *wndPtr = WIN_GetPtr( hwnd );
3202 if (!wndPtr)
3204 SetLastError( ERROR_INVALID_HANDLE );
3205 return 0;
3207 if (wndPtr == WND_DESKTOP) return 0;
3208 if (wndPtr != WND_OTHER_PROCESS)
3210 retval = wndPtr->owner;
3211 WIN_ReleasePtr( wndPtr );
3212 return retval;
3214 /* else fall through to server call */
3217 SERVER_START_REQ( get_window_tree )
3219 req->handle = wine_server_user_handle( hwnd );
3220 if (!wine_server_call_err( req ))
3222 switch(rel)
3224 case GW_HWNDFIRST:
3225 retval = wine_server_ptr_handle( reply->first_sibling );
3226 break;
3227 case GW_HWNDLAST:
3228 retval = wine_server_ptr_handle( reply->last_sibling );
3229 break;
3230 case GW_HWNDNEXT:
3231 retval = wine_server_ptr_handle( reply->next_sibling );
3232 break;
3233 case GW_HWNDPREV:
3234 retval = wine_server_ptr_handle( reply->prev_sibling );
3235 break;
3236 case GW_OWNER:
3237 retval = wine_server_ptr_handle( reply->owner );
3238 break;
3239 case GW_CHILD:
3240 retval = wine_server_ptr_handle( reply->first_child );
3241 break;
3245 SERVER_END_REQ;
3246 return retval;
3250 /*******************************************************************
3251 * ShowOwnedPopups (USER32.@)
3253 BOOL WINAPI ShowOwnedPopups( HWND owner, BOOL fShow )
3255 int count = 0;
3256 HWND *win_array = WIN_ListChildren( GetDesktopWindow() );
3258 if (!win_array) return TRUE;
3260 while (win_array[count]) count++;
3261 while (--count >= 0)
3263 if (GetWindow( win_array[count], GW_OWNER ) != owner) continue;
3264 if (fShow)
3266 if (win_get_flags( win_array[count] ) & WIN_NEEDS_SHOW_OWNEDPOPUP)
3267 /* In Windows, ShowOwnedPopups(TRUE) generates
3268 * WM_SHOWWINDOW messages with SW_PARENTOPENING,
3269 * regardless of the state of the owner
3271 SendMessageW(win_array[count], WM_SHOWWINDOW, SW_SHOWNORMAL, SW_PARENTOPENING);
3273 else
3275 if (GetWindowLongW( win_array[count], GWL_STYLE ) & WS_VISIBLE)
3276 /* In Windows, ShowOwnedPopups(FALSE) generates
3277 * WM_SHOWWINDOW messages with SW_PARENTCLOSING,
3278 * regardless of the state of the owner
3280 SendMessageW(win_array[count], WM_SHOWWINDOW, SW_HIDE, SW_PARENTCLOSING);
3283 HeapFree( GetProcessHeap(), 0, win_array );
3284 return TRUE;
3288 /*******************************************************************
3289 * GetLastActivePopup (USER32.@)
3291 HWND WINAPI GetLastActivePopup( HWND hwnd )
3293 HWND retval = hwnd;
3295 SERVER_START_REQ( get_window_info )
3297 req->handle = wine_server_user_handle( hwnd );
3298 if (!wine_server_call_err( req )) retval = wine_server_ptr_handle( reply->last_active );
3300 SERVER_END_REQ;
3301 return retval;
3305 /*******************************************************************
3306 * WIN_ListChildren
3308 * Build an array of the children of a given window. The array must be
3309 * freed with HeapFree. Returns NULL when no windows are found.
3311 HWND *WIN_ListChildren( HWND hwnd )
3313 if (!hwnd)
3315 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3316 return NULL;
3318 return list_window_children( 0, hwnd, NULL, 0 );
3322 /*******************************************************************
3323 * EnumWindows (USER32.@)
3325 BOOL WINAPI EnumWindows( WNDENUMPROC lpEnumFunc, LPARAM lParam )
3327 HWND *list;
3328 BOOL ret = TRUE;
3329 int i;
3331 USER_CheckNotLock();
3333 /* We have to build a list of all windows first, to avoid */
3334 /* unpleasant side-effects, for instance if the callback */
3335 /* function changes the Z-order of the windows. */
3337 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return TRUE;
3339 /* Now call the callback function for every window */
3341 for (i = 0; list[i]; i++)
3343 /* Make sure that the window still exists */
3344 if (!IsWindow( list[i] )) continue;
3345 if (!(ret = lpEnumFunc( list[i], lParam ))) break;
3347 HeapFree( GetProcessHeap(), 0, list );
3348 return ret;
3352 /**********************************************************************
3353 * EnumThreadWindows (USER32.@)
3355 BOOL WINAPI EnumThreadWindows( DWORD id, WNDENUMPROC func, LPARAM lParam )
3357 HWND *list;
3358 int i;
3359 BOOL ret = TRUE;
3361 USER_CheckNotLock();
3363 if (!(list = list_window_children( 0, GetDesktopWindow(), NULL, id ))) return TRUE;
3365 /* Now call the callback function for every window */
3367 for (i = 0; list[i]; i++)
3368 if (!(ret = func( list[i], lParam ))) break;
3369 HeapFree( GetProcessHeap(), 0, list );
3370 return ret;
3374 /***********************************************************************
3375 * EnumDesktopWindows (USER32.@)
3377 BOOL WINAPI EnumDesktopWindows( HDESK desktop, WNDENUMPROC func, LPARAM lparam )
3379 HWND *list;
3380 int i;
3382 USER_CheckNotLock();
3384 if (!(list = list_window_children( desktop, 0, NULL, 0 ))) return TRUE;
3386 for (i = 0; list[i]; i++)
3387 if (!func( list[i], lparam )) break;
3388 HeapFree( GetProcessHeap(), 0, list );
3389 return TRUE;
3393 #ifdef __i386__
3394 /* Some apps pass a non-stdcall proc to EnumChildWindows,
3395 * so we need a small assembly wrapper to call the proc.
3397 extern LRESULT enum_callback_wrapper( WNDENUMPROC proc, HWND hwnd, LPARAM lparam );
3398 __ASM_GLOBAL_FUNC( enum_callback_wrapper,
3399 "pushl %ebp\n\t"
3400 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
3401 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
3402 "movl %esp,%ebp\n\t"
3403 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
3404 "pushl 16(%ebp)\n\t"
3405 "pushl 12(%ebp)\n\t"
3406 "call *8(%ebp)\n\t"
3407 "leave\n\t"
3408 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
3409 __ASM_CFI(".cfi_same_value %ebp\n\t")
3410 "ret" )
3411 #else
3412 static inline LRESULT enum_callback_wrapper( WNDENUMPROC proc, HWND hwnd, LPARAM lparam )
3414 return proc( hwnd, lparam );
3416 #endif /* __i386__ */
3418 /**********************************************************************
3419 * WIN_EnumChildWindows
3421 * Helper function for EnumChildWindows().
3423 static BOOL WIN_EnumChildWindows( HWND *list, WNDENUMPROC func, LPARAM lParam )
3425 HWND *childList;
3426 BOOL ret = FALSE;
3428 for ( ; *list; list++)
3430 /* Make sure that the window still exists */
3431 if (!IsWindow( *list )) continue;
3432 /* Build children list first */
3433 childList = WIN_ListChildren( *list );
3435 ret = enum_callback_wrapper( func, *list, lParam );
3437 if (childList)
3439 if (ret) ret = WIN_EnumChildWindows( childList, func, lParam );
3440 HeapFree( GetProcessHeap(), 0, childList );
3442 if (!ret) return FALSE;
3444 return TRUE;
3448 /**********************************************************************
3449 * EnumChildWindows (USER32.@)
3451 BOOL WINAPI EnumChildWindows( HWND parent, WNDENUMPROC func, LPARAM lParam )
3453 HWND *list;
3454 BOOL ret;
3456 USER_CheckNotLock();
3458 if (!(list = WIN_ListChildren( parent ))) return FALSE;
3459 ret = WIN_EnumChildWindows( list, func, lParam );
3460 HeapFree( GetProcessHeap(), 0, list );
3461 return ret;
3465 /*******************************************************************
3466 * AnyPopup (USER32.@)
3468 BOOL WINAPI AnyPopup(void)
3470 int i;
3471 BOOL retvalue;
3472 HWND *list = WIN_ListChildren( GetDesktopWindow() );
3474 if (!list) return FALSE;
3475 for (i = 0; list[i]; i++)
3477 if (IsWindowVisible( list[i] ) && GetWindow( list[i], GW_OWNER )) break;
3479 retvalue = (list[i] != 0);
3480 HeapFree( GetProcessHeap(), 0, list );
3481 return retvalue;
3485 /*******************************************************************
3486 * FlashWindow (USER32.@)
3488 BOOL WINAPI FlashWindow( HWND hWnd, BOOL bInvert )
3490 FLASHWINFO finfo;
3492 finfo.cbSize = sizeof(FLASHWINFO);
3493 finfo.dwFlags = bInvert ? FLASHW_ALL : FLASHW_STOP;
3494 finfo.uCount = 1;
3495 finfo.dwTimeout = 0;
3496 finfo.hwnd = hWnd;
3497 return FlashWindowEx( &finfo );
3500 /*******************************************************************
3501 * FlashWindowEx (USER32.@)
3503 BOOL WINAPI FlashWindowEx( PFLASHWINFO pfinfo )
3505 WND *wndPtr;
3507 TRACE( "%p\n", pfinfo );
3509 if (!pfinfo)
3511 SetLastError( ERROR_NOACCESS );
3512 return FALSE;
3515 if (!pfinfo->hwnd || pfinfo->cbSize != sizeof(FLASHWINFO) || !IsWindow( pfinfo->hwnd ))
3517 SetLastError( ERROR_INVALID_PARAMETER );
3518 return FALSE;
3520 FIXME( "%p - semi-stub\n", pfinfo );
3522 if (IsIconic( pfinfo->hwnd ))
3524 RedrawWindow( pfinfo->hwnd, 0, 0, RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW | RDW_FRAME );
3526 wndPtr = WIN_GetPtr( pfinfo->hwnd );
3527 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
3528 if (pfinfo->dwFlags && !(wndPtr->flags & WIN_NCACTIVATED))
3530 wndPtr->flags |= WIN_NCACTIVATED;
3532 else
3534 wndPtr->flags &= ~WIN_NCACTIVATED;
3536 WIN_ReleasePtr( wndPtr );
3537 USER_Driver->pFlashWindowEx( pfinfo );
3538 return TRUE;
3540 else
3542 WPARAM wparam;
3543 HWND hwnd = pfinfo->hwnd;
3545 wndPtr = WIN_GetPtr( hwnd );
3546 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
3547 hwnd = wndPtr->obj.handle; /* make it a full handle */
3549 if (pfinfo->dwFlags) wparam = !(wndPtr->flags & WIN_NCACTIVATED);
3550 else wparam = (hwnd == GetForegroundWindow());
3552 WIN_ReleasePtr( wndPtr );
3553 SendMessageW( hwnd, WM_NCACTIVATE, wparam, 0 );
3554 USER_Driver->pFlashWindowEx( pfinfo );
3555 return wparam;
3559 /*******************************************************************
3560 * GetWindowContextHelpId (USER32.@)
3562 DWORD WINAPI GetWindowContextHelpId( HWND hwnd )
3564 DWORD retval;
3565 WND *wnd = WIN_GetPtr( hwnd );
3566 if (!wnd || wnd == WND_DESKTOP) return 0;
3567 if (wnd == WND_OTHER_PROCESS)
3569 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
3570 return 0;
3572 retval = wnd->helpContext;
3573 WIN_ReleasePtr( wnd );
3574 return retval;
3578 /*******************************************************************
3579 * SetWindowContextHelpId (USER32.@)
3581 BOOL WINAPI SetWindowContextHelpId( HWND hwnd, DWORD id )
3583 WND *wnd = WIN_GetPtr( hwnd );
3584 if (!wnd || wnd == WND_DESKTOP) return FALSE;
3585 if (wnd == WND_OTHER_PROCESS)
3587 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
3588 return FALSE;
3590 wnd->helpContext = id;
3591 WIN_ReleasePtr( wnd );
3592 return TRUE;
3596 /*******************************************************************
3597 * DragDetect (USER32.@)
3599 BOOL WINAPI DragDetect( HWND hWnd, POINT pt )
3601 MSG msg;
3602 RECT rect;
3603 WORD wDragWidth = GetSystemMetrics(SM_CXDRAG);
3604 WORD wDragHeight= GetSystemMetrics(SM_CYDRAG);
3606 SetRect(&rect, pt.x - wDragWidth, pt.y - wDragHeight, pt.x + wDragWidth, pt.y + wDragHeight);
3608 SetCapture(hWnd);
3610 while(1)
3612 while (PeekMessageW( &msg, 0, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE ))
3614 if( msg.message == WM_LBUTTONUP )
3616 ReleaseCapture();
3617 return FALSE;
3619 if( msg.message == WM_MOUSEMOVE )
3621 POINT tmp;
3622 tmp.x = (short)LOWORD(msg.lParam);
3623 tmp.y = (short)HIWORD(msg.lParam);
3624 if( !PtInRect( &rect, tmp ))
3626 ReleaseCapture();
3627 return TRUE;
3631 WaitMessage();
3633 return FALSE;
3636 /******************************************************************************
3637 * GetWindowModuleFileNameA (USER32.@)
3639 UINT WINAPI GetWindowModuleFileNameA( HWND hwnd, LPSTR module, UINT size )
3641 WND *win;
3642 HINSTANCE hinst;
3644 TRACE( "%p, %p, %u\n", hwnd, module, size );
3646 win = WIN_GetPtr( hwnd );
3647 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP)
3649 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3650 return 0;
3652 hinst = win->hInstance;
3653 WIN_ReleasePtr( win );
3655 return GetModuleFileNameA( hinst, module, size );
3658 /******************************************************************************
3659 * GetWindowModuleFileNameW (USER32.@)
3661 UINT WINAPI GetWindowModuleFileNameW( HWND hwnd, LPWSTR module, UINT size )
3663 WND *win;
3664 HINSTANCE hinst;
3666 TRACE( "%p, %p, %u\n", hwnd, module, size );
3668 win = WIN_GetPtr( hwnd );
3669 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP)
3671 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3672 return 0;
3674 hinst = win->hInstance;
3675 WIN_ReleasePtr( win );
3677 return GetModuleFileNameW( hinst, module, size );
3680 /******************************************************************************
3681 * GetWindowInfo (USER32.@)
3683 * Note: tests show that Windows doesn't check cbSize of the structure.
3685 BOOL WINAPI DECLSPEC_HOTPATCH GetWindowInfo( HWND hwnd, PWINDOWINFO pwi)
3687 if (!pwi) return FALSE;
3688 if (!WIN_GetRectangles( hwnd, COORDS_SCREEN, &pwi->rcWindow, &pwi->rcClient )) return FALSE;
3690 pwi->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
3691 pwi->dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
3692 pwi->dwWindowStatus = ((GetActiveWindow() == hwnd) ? WS_ACTIVECAPTION : 0);
3694 pwi->cxWindowBorders = pwi->rcClient.left - pwi->rcWindow.left;
3695 pwi->cyWindowBorders = pwi->rcWindow.bottom - pwi->rcClient.bottom;
3697 pwi->atomWindowType = GetClassLongW( hwnd, GCW_ATOM );
3698 pwi->wCreatorVersion = 0x0400;
3700 return TRUE;
3703 /******************************************************************************
3704 * SwitchDesktop (USER32.@)
3706 * NOTES: Sets the current input or interactive desktop.
3708 BOOL WINAPI SwitchDesktop( HDESK hDesktop)
3710 FIXME("(hwnd %p) stub!\n", hDesktop);
3711 return TRUE;
3715 /***********************************************************************
3716 * __wine_set_pixel_format
3718 BOOL CDECL __wine_set_pixel_format( HWND hwnd, int format )
3720 WND *win = WIN_GetPtr( hwnd );
3722 if (!win || win == WND_DESKTOP || win == WND_OTHER_PROCESS)
3724 WARN( "setting format %d on win %p not supported\n", format, hwnd );
3725 return FALSE;
3727 win->pixel_format = format;
3728 WIN_ReleasePtr( win );
3730 update_window_state( hwnd );
3731 return TRUE;
3735 /*****************************************************************************
3736 * SetLayeredWindowAttributes (USER32.@)
3738 BOOL WINAPI SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
3740 BOOL ret;
3742 TRACE("(%p,%08x,%d,%x)\n", hwnd, key, alpha, flags);
3744 SERVER_START_REQ( set_window_layered_info )
3746 req->handle = wine_server_user_handle( hwnd );
3747 req->color_key = key;
3748 req->alpha = alpha;
3749 req->flags = flags;
3750 ret = !wine_server_call_err( req );
3752 SERVER_END_REQ;
3754 if (ret)
3756 USER_Driver->pSetLayeredWindowAttributes( hwnd, key, alpha, flags );
3757 update_window_state( hwnd );
3760 return ret;
3764 /*****************************************************************************
3765 * GetLayeredWindowAttributes (USER32.@)
3767 BOOL WINAPI GetLayeredWindowAttributes( HWND hwnd, COLORREF *key, BYTE *alpha, DWORD *flags )
3769 BOOL ret;
3771 SERVER_START_REQ( get_window_layered_info )
3773 req->handle = wine_server_user_handle( hwnd );
3774 if ((ret = !wine_server_call_err( req )))
3776 if (key) *key = reply->color_key;
3777 if (alpha) *alpha = reply->alpha;
3778 if (flags) *flags = reply->flags;
3781 SERVER_END_REQ;
3783 return ret;
3787 /*****************************************************************************
3788 * UpdateLayeredWindowIndirect (USER32.@)
3790 BOOL WINAPI UpdateLayeredWindowIndirect( HWND hwnd, const UPDATELAYEREDWINDOWINFO *info )
3792 DWORD flags = SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW;
3793 RECT window_rect, client_rect;
3794 SIZE offset;
3796 if (!info ||
3797 info->cbSize != sizeof(*info) ||
3798 info->dwFlags & ~(ULW_COLORKEY | ULW_ALPHA | ULW_OPAQUE | ULW_EX_NORESIZE) ||
3799 !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED) ||
3800 GetLayeredWindowAttributes( hwnd, NULL, NULL, NULL ))
3802 SetLastError( ERROR_INVALID_PARAMETER );
3803 return FALSE;
3806 WIN_GetRectangles( hwnd, COORDS_PARENT, &window_rect, &client_rect );
3808 if (info->pptDst)
3810 offset.cx = info->pptDst->x - window_rect.left;
3811 offset.cy = info->pptDst->y - window_rect.top;
3812 OffsetRect( &client_rect, offset.cx, offset.cy );
3813 OffsetRect( &window_rect, offset.cx, offset.cy );
3814 flags &= ~SWP_NOMOVE;
3816 if (info->psize)
3818 offset.cx = info->psize->cx - (window_rect.right - window_rect.left);
3819 offset.cy = info->psize->cy - (window_rect.bottom - window_rect.top);
3820 if (info->psize->cx <= 0 || info->psize->cy <= 0)
3822 SetLastError( ERROR_INVALID_PARAMETER );
3823 return FALSE;
3825 if ((info->dwFlags & ULW_EX_NORESIZE) && (offset.cx || offset.cy))
3827 SetLastError( ERROR_INCORRECT_SIZE );
3828 return FALSE;
3830 client_rect.right += offset.cx;
3831 client_rect.bottom += offset.cy;
3832 window_rect.right += offset.cx;
3833 window_rect.bottom += offset.cy;
3834 flags &= ~SWP_NOSIZE;
3837 TRACE( "window %p win %s client %s\n", hwnd,
3838 wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(&client_rect) );
3840 if (!USER_Driver->pUpdateLayeredWindow( hwnd, info, &window_rect )) return FALSE;
3842 set_window_pos( hwnd, 0, flags, &window_rect, &client_rect, NULL );
3843 return TRUE;
3847 /*****************************************************************************
3848 * UpdateLayeredWindow (USER32.@)
3850 BOOL WINAPI UpdateLayeredWindow( HWND hwnd, HDC hdcDst, POINT *pptDst, SIZE *psize,
3851 HDC hdcSrc, POINT *pptSrc, COLORREF crKey, BLENDFUNCTION *pblend,
3852 DWORD flags)
3854 UPDATELAYEREDWINDOWINFO info;
3856 if (flags & ULW_EX_NORESIZE) /* only valid for UpdateLayeredWindowIndirect */
3858 SetLastError( ERROR_INVALID_PARAMETER );
3859 return FALSE;
3861 info.cbSize = sizeof(info);
3862 info.hdcDst = hdcDst;
3863 info.pptDst = pptDst;
3864 info.psize = psize;
3865 info.hdcSrc = hdcSrc;
3866 info.pptSrc = pptSrc;
3867 info.crKey = crKey;
3868 info.pblend = pblend;
3869 info.dwFlags = flags;
3870 info.prcDirty = NULL;
3871 return UpdateLayeredWindowIndirect( hwnd, &info );
3875 /******************************************************************************
3876 * GetProcessDefaultLayout [USER32.@]
3878 * Gets the default layout for parentless windows.
3880 BOOL WINAPI GetProcessDefaultLayout( DWORD *layout )
3882 if (!layout)
3884 SetLastError( ERROR_NOACCESS );
3885 return FALSE;
3887 if (process_layout == ~0u)
3889 static const WCHAR translationW[] = { '\\','V','a','r','F','i','l','e','I','n','f','o',
3890 '\\','T','r','a','n','s','l','a','t','i','o','n', 0 };
3891 static const WCHAR filedescW[] = { '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
3892 '\\','%','0','4','x','%','0','4','x',
3893 '\\','F','i','l','e','D','e','s','c','r','i','p','t','i','o','n',0 };
3894 WCHAR *str, buffer[MAX_PATH];
3895 DWORD i, len, version_layout = 0;
3896 DWORD user_lang = GetUserDefaultLangID();
3897 DWORD *languages;
3898 void *data = NULL;
3900 GetModuleFileNameW( 0, buffer, MAX_PATH );
3901 if (!(len = GetFileVersionInfoSizeW( buffer, NULL ))) goto done;
3902 if (!(data = HeapAlloc( GetProcessHeap(), 0, len ))) goto done;
3903 if (!GetFileVersionInfoW( buffer, 0, len, data )) goto done;
3904 if (!VerQueryValueW( data, translationW, (void **)&languages, &len ) || !len) goto done;
3906 len /= sizeof(DWORD);
3907 for (i = 0; i < len; i++) if (LOWORD(languages[i]) == user_lang) break;
3908 if (i == len) /* try neutral language */
3909 for (i = 0; i < len; i++)
3910 if (LOWORD(languages[i]) == MAKELANGID( PRIMARYLANGID(user_lang), SUBLANG_NEUTRAL )) break;
3911 if (i == len) i = 0; /* default to the first one */
3913 sprintfW( buffer, filedescW, LOWORD(languages[i]), HIWORD(languages[i]) );
3914 if (!VerQueryValueW( data, buffer, (void **)&str, &len )) goto done;
3915 TRACE( "found description %s\n", debugstr_w( str ));
3916 if (str[0] == 0x200e && str[1] == 0x200e) version_layout = LAYOUT_RTL;
3918 done:
3919 HeapFree( GetProcessHeap(), 0, data );
3920 process_layout = version_layout;
3922 *layout = process_layout;
3923 return TRUE;
3927 /******************************************************************************
3928 * SetProcessDefaultLayout [USER32.@]
3930 * Sets the default layout for parentless windows.
3932 BOOL WINAPI SetProcessDefaultLayout( DWORD layout )
3934 process_layout = layout;
3935 return TRUE;
3939 /* 64bit versions */
3941 #ifdef GetWindowLongPtrW
3942 #undef GetWindowLongPtrW
3943 #endif
3945 #ifdef GetWindowLongPtrA
3946 #undef GetWindowLongPtrA
3947 #endif
3949 #ifdef SetWindowLongPtrW
3950 #undef SetWindowLongPtrW
3951 #endif
3953 #ifdef SetWindowLongPtrA
3954 #undef SetWindowLongPtrA
3955 #endif
3957 /*****************************************************************************
3958 * GetWindowLongPtrW (USER32.@)
3960 LONG_PTR WINAPI GetWindowLongPtrW( HWND hwnd, INT offset )
3962 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG_PTR), TRUE );
3965 /*****************************************************************************
3966 * GetWindowLongPtrA (USER32.@)
3968 LONG_PTR WINAPI GetWindowLongPtrA( HWND hwnd, INT offset )
3970 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG_PTR), FALSE );
3973 /*****************************************************************************
3974 * SetWindowLongPtrW (USER32.@)
3976 LONG_PTR WINAPI SetWindowLongPtrW( HWND hwnd, INT offset, LONG_PTR newval )
3978 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG_PTR), newval, TRUE );
3981 /*****************************************************************************
3982 * SetWindowLongPtrA (USER32.@)
3984 LONG_PTR WINAPI SetWindowLongPtrA( HWND hwnd, INT offset, LONG_PTR newval )
3986 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG_PTR), newval, FALSE );
3989 /*****************************************************************************
3990 * RegisterTouchWindow (USER32.@)
3992 BOOL WINAPI RegisterTouchWindow(HWND hwnd, ULONG flags)
3994 FIXME("(%p %08x): stub\n", hwnd, flags);
3995 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
3996 return FALSE;
3999 /*****************************************************************************
4000 * UnregisterTouchWindow (USER32.@)
4002 BOOL WINAPI UnregisterTouchWindow(HWND hwnd)
4004 FIXME("(%p): stub\n", hwnd);
4005 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
4006 return FALSE;
4009 /*****************************************************************************
4010 * CloseTouchInputHandle (USER32.@)
4012 BOOL WINAPI CloseTouchInputHandle(HTOUCHINPUT handle)
4014 FIXME("(%p): stub\n", handle);
4015 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
4016 return FALSE;
4019 /*****************************************************************************
4020 * GetTouchInputInfo (USER32.@)
4022 BOOL WINAPI GetTouchInputInfo(HTOUCHINPUT handle, UINT count, TOUCHINPUT *ptr, int size)
4024 FIXME("(%p %u %p %u): stub\n", handle, count, ptr, size);
4025 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
4026 return FALSE;
4029 /*****************************************************************************
4030 * GetGestureInfo (USER32.@)
4032 BOOL WINAPI GetGestureInfo(HGESTUREINFO handle, PGESTUREINFO ptr)
4034 FIXME("(%p %p): stub\n", handle, ptr);
4035 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
4036 return FALSE;
4039 /*****************************************************************************
4040 * GetWindowDisplayAffinity (USER32.@)
4042 BOOL WINAPI GetWindowDisplayAffinity(HWND hwnd, DWORD *affinity)
4044 FIXME("(%p, %p): stub\n", hwnd, affinity);
4046 if (!hwnd || !affinity)
4048 SetLastError(hwnd ? ERROR_NOACCESS : ERROR_INVALID_WINDOW_HANDLE);
4049 return FALSE;
4052 *affinity = WDA_NONE;
4053 return TRUE;
4056 /*****************************************************************************
4057 * SetWindowDisplayAffinity (USER32.@)
4059 BOOL WINAPI SetWindowDisplayAffinity(HWND hwnd, DWORD affinity)
4061 FIXME("(%p, %u): stub\n", hwnd, affinity);
4063 if (!hwnd)
4065 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
4066 return FALSE;
4069 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
4070 return FALSE;