shell32: Use Public instead of AllUsersProfile in the registry.
[wine.git] / dlls / user32 / win.c
blobc7a3b06c3a23e9eedc8525c4529f5b0c95fc322c
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;
203 DPI_AWARENESS awareness = GetAwarenessFromDpiAwarenessContext( GetThreadDpiAwarenessContext() );
204 UINT dpi = 0;
206 SERVER_START_REQ( create_window )
208 req->parent = wine_server_user_handle( parent );
209 req->owner = wine_server_user_handle( owner );
210 req->instance = wine_server_client_ptr( instance );
211 req->dpi = GetDpiForSystem();
212 req->awareness = awareness;
213 if (!(req->atom = get_int_atom_value( name )) && name)
214 wine_server_add_data( req, name, strlenW(name)*sizeof(WCHAR) );
215 if (!wine_server_call_err( req ))
217 handle = wine_server_ptr_handle( reply->handle );
218 full_parent = wine_server_ptr_handle( reply->parent );
219 full_owner = wine_server_ptr_handle( reply->owner );
220 extra_bytes = reply->extra;
221 dpi = reply->dpi;
222 awareness = reply->awareness;
223 class = wine_server_get_ptr( reply->class_ptr );
226 SERVER_END_REQ;
228 if (!handle)
230 WARN( "error %d creating window\n", GetLastError() );
231 return NULL;
234 if (!(win = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
235 sizeof(WND) + extra_bytes - sizeof(win->wExtra) )))
237 SERVER_START_REQ( destroy_window )
239 req->handle = wine_server_user_handle( handle );
240 wine_server_call( req );
242 SERVER_END_REQ;
243 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
244 return NULL;
247 if (!parent) /* if parent is 0 we don't have a desktop window yet */
249 struct user_thread_info *thread_info = get_user_thread_info();
251 if (name == (LPCWSTR)DESKTOP_CLASS_ATOM)
253 if (!thread_info->top_window) thread_info->top_window = full_parent ? full_parent : handle;
254 else assert( full_parent == thread_info->top_window );
255 if (full_parent && !USER_Driver->pCreateDesktopWindow( thread_info->top_window ))
256 ERR( "failed to create desktop window\n" );
258 else /* HWND_MESSAGE parent */
260 if (!thread_info->msg_window && !full_parent) thread_info->msg_window = handle;
264 USER_Lock();
266 index = USER_HANDLE_TO_INDEX(handle);
267 assert( index < NB_USER_HANDLES );
268 win->obj.handle = handle;
269 win->obj.type = USER_WINDOW;
270 win->parent = full_parent;
271 win->owner = full_owner;
272 win->class = class;
273 win->winproc = get_class_winproc( class );
274 win->cbWndExtra = extra_bytes;
275 win->dpi = dpi;
276 win->dpi_awareness = awareness;
277 InterlockedExchangePointer( &user_handles[index], win );
278 if (WINPROC_IsUnicode( win->winproc, unicode )) win->flags |= WIN_ISUNICODE;
279 return win;
283 /***********************************************************************
284 * free_window_handle
286 * Free a window handle.
288 static void free_window_handle( HWND hwnd )
290 struct user_object *ptr;
291 WORD index = USER_HANDLE_TO_INDEX(hwnd);
293 if ((ptr = get_user_handle_ptr( hwnd, USER_WINDOW )) && ptr != OBJ_OTHER_PROCESS)
295 SERVER_START_REQ( destroy_window )
297 req->handle = wine_server_user_handle( hwnd );
298 wine_server_call( req );
299 InterlockedCompareExchangePointer( &user_handles[index], NULL, ptr );
301 SERVER_END_REQ;
302 USER_Unlock();
303 HeapFree( GetProcessHeap(), 0, ptr );
308 /*******************************************************************
309 * list_window_children
311 * Build an array of the children of a given window. The array must be
312 * freed with HeapFree. Returns NULL when no windows are found.
314 static HWND *list_window_children( HDESK desktop, HWND hwnd, LPCWSTR class, DWORD tid )
316 HWND *list;
317 int i, size = 128;
318 ATOM atom = get_int_atom_value( class );
320 /* empty class is not the same as NULL class */
321 if (!atom && class && !class[0]) return NULL;
323 for (;;)
325 int count = 0;
327 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
329 SERVER_START_REQ( get_window_children )
331 req->desktop = wine_server_obj_handle( desktop );
332 req->parent = wine_server_user_handle( hwnd );
333 req->tid = tid;
334 req->atom = atom;
335 if (!atom && class) wine_server_add_data( req, class, strlenW(class)*sizeof(WCHAR) );
336 wine_server_set_reply( req, list, (size-1) * sizeof(user_handle_t) );
337 if (!wine_server_call( req )) count = reply->count;
339 SERVER_END_REQ;
340 if (count && count < size)
342 /* start from the end since HWND is potentially larger than user_handle_t */
343 for (i = count - 1; i >= 0; i--)
344 list[i] = wine_server_ptr_handle( ((user_handle_t *)list)[i] );
345 list[count] = 0;
346 return list;
348 HeapFree( GetProcessHeap(), 0, list );
349 if (!count) break;
350 size = count + 1; /* restart with a large enough buffer */
352 return NULL;
356 /*******************************************************************
357 * list_window_parents
359 * Build an array of all parents of a given window, starting with
360 * the immediate parent. The array must be freed with HeapFree.
362 static HWND *list_window_parents( HWND hwnd )
364 WND *win;
365 HWND current, *list;
366 int i, pos = 0, size = 16, count;
368 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) return NULL;
370 current = hwnd;
371 for (;;)
373 if (!(win = WIN_GetPtr( current ))) goto empty;
374 if (win == WND_OTHER_PROCESS) break; /* need to do it the hard way */
375 if (win == WND_DESKTOP)
377 if (!pos) goto empty;
378 list[pos] = 0;
379 return list;
381 list[pos] = current = win->parent;
382 WIN_ReleasePtr( win );
383 if (!current) return list;
384 if (++pos == size - 1)
386 /* need to grow the list */
387 HWND *new_list = HeapReAlloc( GetProcessHeap(), 0, list, (size+16) * sizeof(HWND) );
388 if (!new_list) goto empty;
389 list = new_list;
390 size += 16;
394 /* at least one parent belongs to another process, have to query the server */
396 for (;;)
398 count = 0;
399 SERVER_START_REQ( get_window_parents )
401 req->handle = wine_server_user_handle( hwnd );
402 wine_server_set_reply( req, list, (size-1) * sizeof(user_handle_t) );
403 if (!wine_server_call( req )) count = reply->count;
405 SERVER_END_REQ;
406 if (!count) goto empty;
407 if (size > count)
409 /* start from the end since HWND is potentially larger than user_handle_t */
410 for (i = count - 1; i >= 0; i--)
411 list[i] = wine_server_ptr_handle( ((user_handle_t *)list)[i] );
412 list[count] = 0;
413 return list;
415 HeapFree( GetProcessHeap(), 0, list );
416 size = count + 1;
417 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) return NULL;
420 empty:
421 HeapFree( GetProcessHeap(), 0, list );
422 return NULL;
426 /*******************************************************************
427 * send_parent_notify
429 static void send_parent_notify( HWND hwnd, UINT msg )
431 if ((GetWindowLongW( hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD &&
432 !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY))
434 HWND parent = GetParent(hwnd);
435 if (parent && parent != GetDesktopWindow())
436 SendMessageW( parent, WM_PARENTNOTIFY,
437 MAKEWPARAM( msg, GetWindowLongPtrW( hwnd, GWLP_ID )), (LPARAM)hwnd );
442 /*******************************************************************
443 * update_window_state
445 * Trigger an update of the window's driver state and surface.
447 static void update_window_state( HWND hwnd )
449 RECT window_rect, client_rect, valid_rects[2];
451 WIN_GetRectangles( hwnd, COORDS_PARENT, &window_rect, &client_rect );
452 valid_rects[0] = valid_rects[1] = client_rect;
453 set_window_pos( hwnd, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE |
454 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW,
455 &window_rect, &client_rect, valid_rects );
459 /*******************************************************************
460 * get_server_window_text
462 * Retrieve the window text from the server.
464 static data_size_t get_server_window_text( HWND hwnd, WCHAR *text, data_size_t count )
466 data_size_t len = 0, needed = 0;
468 SERVER_START_REQ( get_window_text )
470 req->handle = wine_server_user_handle( hwnd );
471 if (count) wine_server_set_reply( req, text, (count - 1) * sizeof(WCHAR) );
472 if (!wine_server_call_err( req ))
474 needed = reply->length;
475 len = wine_server_reply_size(reply);
478 SERVER_END_REQ;
479 if (text) text[len / sizeof(WCHAR)] = 0;
480 return needed;
484 /*******************************************************************
485 * get_hwnd_message_parent
487 * Return the parent for HWND_MESSAGE windows.
489 HWND get_hwnd_message_parent(void)
491 struct user_thread_info *thread_info = get_user_thread_info();
493 if (!thread_info->msg_window) GetDesktopWindow(); /* trigger creation */
494 return thread_info->msg_window;
498 /*******************************************************************
499 * is_desktop_window
501 * Check if window is the desktop or the HWND_MESSAGE top parent.
503 BOOL is_desktop_window( HWND hwnd )
505 struct user_thread_info *thread_info = get_user_thread_info();
507 if (!hwnd) return FALSE;
508 if (hwnd == thread_info->top_window) return TRUE;
509 if (hwnd == thread_info->msg_window) return TRUE;
511 if (!HIWORD(hwnd) || HIWORD(hwnd) == 0xffff)
513 if (LOWORD(thread_info->top_window) == LOWORD(hwnd)) return TRUE;
514 if (LOWORD(thread_info->msg_window) == LOWORD(hwnd)) return TRUE;
516 return FALSE;
520 /*******************************************************************
521 * Dummy window surface for windows that shouldn't get painted.
524 static void dummy_surface_lock( struct window_surface *window_surface )
526 /* nothing to do */
529 static void dummy_surface_unlock( struct window_surface *window_surface )
531 /* nothing to do */
534 static void *dummy_surface_get_bitmap_info( struct window_surface *window_surface, BITMAPINFO *info )
536 static DWORD dummy_data;
538 info->bmiHeader.biSize = sizeof( info->bmiHeader );
539 info->bmiHeader.biWidth = dummy_surface.rect.right;
540 info->bmiHeader.biHeight = dummy_surface.rect.bottom;
541 info->bmiHeader.biPlanes = 1;
542 info->bmiHeader.biBitCount = 32;
543 info->bmiHeader.biCompression = BI_RGB;
544 info->bmiHeader.biSizeImage = 0;
545 info->bmiHeader.biXPelsPerMeter = 0;
546 info->bmiHeader.biYPelsPerMeter = 0;
547 info->bmiHeader.biClrUsed = 0;
548 info->bmiHeader.biClrImportant = 0;
549 return &dummy_data;
552 static RECT *dummy_surface_get_bounds( struct window_surface *window_surface )
554 static RECT dummy_bounds;
555 return &dummy_bounds;
558 static void dummy_surface_set_region( struct window_surface *window_surface, HRGN region )
560 /* nothing to do */
563 static void dummy_surface_flush( struct window_surface *window_surface )
565 /* nothing to do */
568 static void dummy_surface_destroy( struct window_surface *window_surface )
570 /* nothing to do */
573 static const struct window_surface_funcs dummy_surface_funcs =
575 dummy_surface_lock,
576 dummy_surface_unlock,
577 dummy_surface_get_bitmap_info,
578 dummy_surface_get_bounds,
579 dummy_surface_set_region,
580 dummy_surface_flush,
581 dummy_surface_destroy
584 struct window_surface dummy_surface = { &dummy_surface_funcs, { NULL, NULL }, 1, { 0, 0, 1, 1 } };
587 /*******************************************************************
588 * register_window_surface
590 * Register a window surface in the global list, possibly replacing another one.
592 void register_window_surface( struct window_surface *old, struct window_surface *new )
594 if (old == new) return;
595 EnterCriticalSection( &surfaces_section );
596 if (old && old != &dummy_surface) list_remove( &old->entry );
597 if (new && new != &dummy_surface) list_add_tail( &window_surfaces, &new->entry );
598 LeaveCriticalSection( &surfaces_section );
602 /*******************************************************************
603 * flush_window_surfaces
605 * Flush pending output from all window surfaces.
607 void flush_window_surfaces( BOOL idle )
609 static DWORD last_idle;
610 DWORD now;
611 struct window_surface *surface;
613 EnterCriticalSection( &surfaces_section );
614 now = GetTickCount();
615 if (idle) last_idle = now;
616 /* if not idle, we only flush if there's evidence that the app never goes idle */
617 else if ((int)(now - last_idle) < 50) goto done;
619 LIST_FOR_EACH_ENTRY( surface, &window_surfaces, struct window_surface, entry )
620 surface->funcs->flush( surface );
621 done:
622 LeaveCriticalSection( &surfaces_section );
626 /***********************************************************************
627 * WIN_GetPtr
629 * Return a pointer to the WND structure if local to the process,
630 * or WND_OTHER_PROCESS if handle may be valid in other process.
631 * If ret value is a valid pointer, it must be released with WIN_ReleasePtr.
633 WND *WIN_GetPtr( HWND hwnd )
635 WND *ptr;
637 if ((ptr = get_user_handle_ptr( hwnd, USER_WINDOW )) == WND_OTHER_PROCESS)
639 if (is_desktop_window( hwnd )) ptr = WND_DESKTOP;
641 return ptr;
645 /***********************************************************************
646 * WIN_IsCurrentProcess
648 * Check whether a given window belongs to the current process (and return the full handle).
650 HWND WIN_IsCurrentProcess( HWND hwnd )
652 WND *ptr;
653 HWND ret;
655 if (!(ptr = WIN_GetPtr( hwnd )) || ptr == WND_OTHER_PROCESS || ptr == WND_DESKTOP) return 0;
656 ret = ptr->obj.handle;
657 WIN_ReleasePtr( ptr );
658 return ret;
662 /***********************************************************************
663 * WIN_IsCurrentThread
665 * Check whether a given window belongs to the current thread (and return the full handle).
667 HWND WIN_IsCurrentThread( HWND hwnd )
669 WND *ptr;
670 HWND ret = 0;
672 if (!(ptr = WIN_GetPtr( hwnd )) || ptr == WND_OTHER_PROCESS || ptr == WND_DESKTOP) return 0;
673 if (ptr->tid == GetCurrentThreadId()) ret = ptr->obj.handle;
674 WIN_ReleasePtr( ptr );
675 return ret;
679 /***********************************************************************
680 * win_set_flags
682 * Set the flags of a window and return the previous value.
684 UINT win_set_flags( HWND hwnd, UINT set_mask, UINT clear_mask )
686 UINT ret;
687 WND *ptr = WIN_GetPtr( hwnd );
689 if (!ptr || ptr == WND_OTHER_PROCESS || ptr == WND_DESKTOP) return 0;
690 ret = ptr->flags;
691 ptr->flags = (ret & ~clear_mask) | set_mask;
692 WIN_ReleasePtr( ptr );
693 return ret;
697 /***********************************************************************
698 * WIN_GetFullHandle
700 * Convert a possibly truncated window handle to a full 32-bit handle.
702 HWND WIN_GetFullHandle( HWND hwnd )
704 WND *ptr;
706 if (!hwnd || (ULONG_PTR)hwnd >> 16) return hwnd;
707 if (LOWORD(hwnd) <= 1 || LOWORD(hwnd) == 0xffff) return hwnd;
708 /* do sign extension for -2 and -3 */
709 if (LOWORD(hwnd) >= (WORD)-3) return (HWND)(LONG_PTR)(INT16)LOWORD(hwnd);
711 if (!(ptr = WIN_GetPtr( hwnd ))) return hwnd;
713 if (ptr == WND_DESKTOP)
715 if (LOWORD(hwnd) == LOWORD(GetDesktopWindow())) return GetDesktopWindow();
716 else return get_hwnd_message_parent();
719 if (ptr != WND_OTHER_PROCESS)
721 hwnd = ptr->obj.handle;
722 WIN_ReleasePtr( ptr );
724 else /* may belong to another process */
726 SERVER_START_REQ( get_window_info )
728 req->handle = wine_server_user_handle( hwnd );
729 if (!wine_server_call_err( req )) hwnd = wine_server_ptr_handle( reply->full_handle );
731 SERVER_END_REQ;
733 return hwnd;
737 /***********************************************************************
738 * WIN_SetOwner
740 * Change the owner of a window.
742 HWND WIN_SetOwner( HWND hwnd, HWND owner )
744 WND *win = WIN_GetPtr( hwnd );
745 HWND ret = 0;
747 if (!win || win == WND_DESKTOP) return 0;
748 if (win == WND_OTHER_PROCESS)
750 if (IsWindow(hwnd)) ERR( "cannot set owner %p on other process window %p\n", owner, hwnd );
751 return 0;
753 SERVER_START_REQ( set_window_owner )
755 req->handle = wine_server_user_handle( hwnd );
756 req->owner = wine_server_user_handle( owner );
757 if (!wine_server_call( req ))
759 win->owner = wine_server_ptr_handle( reply->full_owner );
760 ret = wine_server_ptr_handle( reply->prev_owner );
763 SERVER_END_REQ;
764 WIN_ReleasePtr( win );
765 return ret;
769 /***********************************************************************
770 * WIN_SetStyle
772 * Change the style of a window.
774 ULONG WIN_SetStyle( HWND hwnd, ULONG set_bits, ULONG clear_bits )
776 BOOL ok, made_visible = FALSE;
777 STYLESTRUCT style;
778 WND *win = WIN_GetPtr( hwnd );
780 if (!win || win == WND_DESKTOP) return 0;
781 if (win == WND_OTHER_PROCESS)
783 if (IsWindow(hwnd))
784 return SendMessageW(hwnd, WM_WINE_SETSTYLE, set_bits, clear_bits);
785 return 0;
787 style.styleOld = win->dwStyle;
788 style.styleNew = (win->dwStyle | set_bits) & ~clear_bits;
789 if (style.styleNew == style.styleOld)
791 WIN_ReleasePtr( win );
792 return style.styleNew;
794 SERVER_START_REQ( set_window_info )
796 req->handle = wine_server_user_handle( hwnd );
797 req->flags = SET_WIN_STYLE;
798 req->style = style.styleNew;
799 req->extra_offset = -1;
800 if ((ok = !wine_server_call( req )))
802 style.styleOld = reply->old_style;
803 win->dwStyle = style.styleNew;
806 SERVER_END_REQ;
808 if (ok && ((style.styleOld ^ style.styleNew) & WS_VISIBLE))
810 made_visible = (style.styleNew & WS_VISIBLE) != 0;
811 invalidate_dce( win, NULL );
813 WIN_ReleasePtr( win );
815 if (!ok) return 0;
817 USER_Driver->pSetWindowStyle( hwnd, GWL_STYLE, &style );
818 if (made_visible) update_window_state( hwnd );
820 return style.styleOld;
824 /***********************************************************************
825 * WIN_GetRectangles
827 * Get the window and client rectangles.
829 BOOL WIN_GetRectangles( HWND hwnd, enum coords_relative relative, RECT *rectWindow, RECT *rectClient )
831 WND *win = WIN_GetPtr( hwnd );
832 BOOL ret = TRUE;
834 if (!win)
836 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
837 return FALSE;
839 if (win == WND_DESKTOP)
841 RECT rect;
842 rect.left = rect.top = 0;
843 if (hwnd == get_hwnd_message_parent())
845 rect.right = 100;
846 rect.bottom = 100;
848 else
850 rect.right = GetSystemMetrics(SM_CXSCREEN);
851 rect.bottom = GetSystemMetrics(SM_CYSCREEN);
853 if (rectWindow) *rectWindow = rect;
854 if (rectClient) *rectClient = rect;
855 return TRUE;
857 if (win != WND_OTHER_PROCESS)
859 RECT window_rect = win->window_rect, client_rect = win->client_rect;
861 switch (relative)
863 case COORDS_CLIENT:
864 OffsetRect( &window_rect, -win->client_rect.left, -win->client_rect.top );
865 OffsetRect( &client_rect, -win->client_rect.left, -win->client_rect.top );
866 if (win->dwExStyle & WS_EX_LAYOUTRTL)
867 mirror_rect( &win->client_rect, &window_rect );
868 break;
869 case COORDS_WINDOW:
870 OffsetRect( &window_rect, -win->window_rect.left, -win->window_rect.top );
871 OffsetRect( &client_rect, -win->window_rect.left, -win->window_rect.top );
872 if (win->dwExStyle & WS_EX_LAYOUTRTL)
873 mirror_rect( &win->window_rect, &client_rect );
874 break;
875 case COORDS_PARENT:
876 if (win->parent)
878 WND *parent = WIN_GetPtr( win->parent );
879 if (parent == WND_DESKTOP) break;
880 if (!parent || parent == WND_OTHER_PROCESS)
882 WIN_ReleasePtr( win );
883 goto other_process;
885 if (parent->flags & WIN_CHILDREN_MOVED)
887 WIN_ReleasePtr( parent );
888 WIN_ReleasePtr( win );
889 goto other_process;
891 if (parent->dwExStyle & WS_EX_LAYOUTRTL)
893 mirror_rect( &parent->client_rect, &window_rect );
894 mirror_rect( &parent->client_rect, &client_rect );
896 WIN_ReleasePtr( parent );
898 break;
899 case COORDS_SCREEN:
900 while (win->parent)
902 WND *parent = WIN_GetPtr( win->parent );
903 if (parent == WND_DESKTOP) break;
904 if (!parent || parent == WND_OTHER_PROCESS)
906 WIN_ReleasePtr( win );
907 goto other_process;
909 WIN_ReleasePtr( win );
910 if (parent->flags & WIN_CHILDREN_MOVED)
912 WIN_ReleasePtr( parent );
913 goto other_process;
915 win = parent;
916 if (win->parent)
918 OffsetRect( &window_rect, win->client_rect.left, win->client_rect.top );
919 OffsetRect( &client_rect, win->client_rect.left, win->client_rect.top );
922 break;
924 if (rectWindow) *rectWindow = window_rect;
925 if (rectClient) *rectClient = client_rect;
926 WIN_ReleasePtr( win );
927 return TRUE;
930 other_process:
931 SERVER_START_REQ( get_window_rectangles )
933 req->handle = wine_server_user_handle( hwnd );
934 req->relative = relative;
935 if ((ret = !wine_server_call_err( req )))
937 if (rectWindow)
939 rectWindow->left = reply->window.left;
940 rectWindow->top = reply->window.top;
941 rectWindow->right = reply->window.right;
942 rectWindow->bottom = reply->window.bottom;
944 if (rectClient)
946 rectClient->left = reply->client.left;
947 rectClient->top = reply->client.top;
948 rectClient->right = reply->client.right;
949 rectClient->bottom = reply->client.bottom;
953 SERVER_END_REQ;
954 return ret;
958 /***********************************************************************
959 * WIN_DestroyWindow
961 * Destroy storage associated to a window. "Internals" p.358
963 LRESULT WIN_DestroyWindow( HWND hwnd )
965 WND *wndPtr;
966 HWND *list;
967 HMENU menu = 0, sys_menu;
968 HWND icon_title;
969 struct window_surface *surface;
971 TRACE("%p\n", hwnd );
973 /* destroy default IME window */
974 if (win_set_flags( hwnd, 0, WIN_HAS_IME_WIN ) & WIN_HAS_IME_WIN)
976 TRACE("unregister IME window for %p\n", hwnd);
977 imm_unregister_window( hwnd );
980 /* free child windows */
981 if ((list = WIN_ListChildren( hwnd )))
983 int i;
984 for (i = 0; list[i]; i++)
986 if (WIN_IsCurrentThread( list[i] )) WIN_DestroyWindow( list[i] );
987 else SendNotifyMessageW( list[i], WM_WINE_DESTROYWINDOW, 0, 0 );
989 HeapFree( GetProcessHeap(), 0, list );
992 /* Unlink now so we won't bother with the children later on */
993 SERVER_START_REQ( set_parent )
995 req->handle = wine_server_user_handle( hwnd );
996 req->parent = 0;
997 wine_server_call( req );
999 SERVER_END_REQ;
1002 * Send the WM_NCDESTROY to the window being destroyed.
1004 SendMessageW( hwnd, WM_NCDESTROY, 0, 0 );
1006 /* FIXME: do we need to fake QS_MOUSEMOVE wakebit? */
1008 /* free resources associated with the window */
1010 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
1011 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
1012 menu = (HMENU)wndPtr->wIDmenu;
1013 sys_menu = wndPtr->hSysMenu;
1014 free_dce( wndPtr->dce, hwnd );
1015 wndPtr->dce = NULL;
1016 icon_title = wndPtr->icon_title;
1017 HeapFree( GetProcessHeap(), 0, wndPtr->text );
1018 wndPtr->text = NULL;
1019 HeapFree( GetProcessHeap(), 0, wndPtr->pScroll );
1020 wndPtr->pScroll = NULL;
1021 DestroyIcon( wndPtr->hIconSmall2 );
1022 surface = wndPtr->surface;
1023 wndPtr->surface = NULL;
1024 WIN_ReleasePtr( wndPtr );
1026 if (icon_title) DestroyWindow( icon_title );
1027 if (menu) DestroyMenu( menu );
1028 if (sys_menu) DestroyMenu( sys_menu );
1029 if (surface)
1031 register_window_surface( surface, NULL );
1032 window_surface_release( surface );
1035 USER_Driver->pDestroyWindow( hwnd );
1037 free_window_handle( hwnd );
1038 return 0;
1042 /***********************************************************************
1043 * next_thread_window
1045 static WND *next_thread_window( HWND *hwnd )
1047 struct user_object *ptr;
1048 WND *win;
1049 WORD index = *hwnd ? USER_HANDLE_TO_INDEX( *hwnd ) + 1 : 0;
1051 USER_Lock();
1052 while (index < NB_USER_HANDLES)
1054 if (!(ptr = user_handles[index++])) continue;
1055 if (ptr->type != USER_WINDOW) continue;
1056 win = (WND *)ptr;
1057 if (win->tid != GetCurrentThreadId()) continue;
1058 *hwnd = ptr->handle;
1059 return win;
1061 USER_Unlock();
1062 return NULL;
1066 /***********************************************************************
1067 * destroy_thread_windows
1069 * Destroy all window owned by the current thread.
1071 void destroy_thread_windows(void)
1073 WND *wndPtr;
1074 HWND hwnd = 0, *list;
1075 HMENU menu, sys_menu;
1076 struct window_surface *surface;
1077 int i;
1079 while ((wndPtr = next_thread_window( &hwnd )))
1081 /* destroy the client-side storage */
1083 list = WIN_ListChildren( hwnd );
1084 menu = ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD) ? (HMENU)wndPtr->wIDmenu : 0;
1085 sys_menu = wndPtr->hSysMenu;
1086 free_dce( wndPtr->dce, hwnd );
1087 surface = wndPtr->surface;
1088 InterlockedCompareExchangePointer( &user_handles[USER_HANDLE_TO_INDEX(hwnd)], NULL, wndPtr );
1089 WIN_ReleasePtr( wndPtr );
1090 HeapFree( GetProcessHeap(), 0, wndPtr );
1091 if (menu) DestroyMenu( menu );
1092 if (sys_menu) DestroyMenu( sys_menu );
1093 if (surface)
1095 register_window_surface( surface, NULL );
1096 window_surface_release( surface );
1099 /* free child windows */
1101 if (!list) continue;
1102 for (i = 0; list[i]; i++)
1103 if (!WIN_IsCurrentThread( list[i] ))
1104 SendNotifyMessageW( list[i], WM_WINE_DESTROYWINDOW, 0, 0 );
1105 HeapFree( GetProcessHeap(), 0, list );
1110 /***********************************************************************
1111 * WIN_FixCoordinates
1113 * Fix the coordinates - Helper for WIN_CreateWindowEx.
1114 * returns default show mode in sw.
1116 static void WIN_FixCoordinates( CREATESTRUCTW *cs, INT *sw)
1118 #define IS_DEFAULT(x) ((x) == CW_USEDEFAULT || (x) == (SHORT)0x8000)
1119 POINT pos[2];
1121 if (cs->dwExStyle & WS_EX_MDICHILD)
1123 UINT id = 0;
1125 MDI_CalcDefaultChildPos(cs->hwndParent, -1, pos, 0, &id);
1126 if (!(cs->style & WS_POPUP)) cs->hMenu = ULongToHandle(id);
1128 TRACE("MDI child id %04x\n", id);
1131 if (cs->style & (WS_CHILD | WS_POPUP))
1133 if (cs->dwExStyle & WS_EX_MDICHILD)
1135 if (IS_DEFAULT(cs->x))
1137 cs->x = pos[0].x;
1138 cs->y = pos[0].y;
1140 if (IS_DEFAULT(cs->cx) || !cs->cx) cs->cx = pos[1].x;
1141 if (IS_DEFAULT(cs->cy) || !cs->cy) cs->cy = pos[1].y;
1143 else
1145 if (IS_DEFAULT(cs->x)) cs->x = cs->y = 0;
1146 if (IS_DEFAULT(cs->cx)) cs->cx = cs->cy = 0;
1149 else /* overlapped window */
1151 HMONITOR monitor;
1152 MONITORINFO mon_info;
1153 STARTUPINFOW info;
1155 if (!IS_DEFAULT(cs->x) && !IS_DEFAULT(cs->cx) && !IS_DEFAULT(cs->cy)) return;
1157 monitor = MonitorFromWindow( cs->hwndParent, MONITOR_DEFAULTTOPRIMARY );
1158 mon_info.cbSize = sizeof(mon_info);
1159 GetMonitorInfoW( monitor, &mon_info );
1160 GetStartupInfoW( &info );
1162 if (IS_DEFAULT(cs->x))
1164 if (!IS_DEFAULT(cs->y)) *sw = cs->y;
1165 cs->x = (info.dwFlags & STARTF_USEPOSITION) ? info.dwX : mon_info.rcWork.left;
1166 cs->y = (info.dwFlags & STARTF_USEPOSITION) ? info.dwY : mon_info.rcWork.top;
1169 if (IS_DEFAULT(cs->cx))
1171 if (info.dwFlags & STARTF_USESIZE)
1173 cs->cx = info.dwXSize;
1174 cs->cy = info.dwYSize;
1176 else
1178 cs->cx = (mon_info.rcWork.right - mon_info.rcWork.left) * 3 / 4 - cs->x;
1179 cs->cy = (mon_info.rcWork.bottom - mon_info.rcWork.top) * 3 / 4 - cs->y;
1182 /* neither x nor cx are default. Check the y values .
1183 * In the trace we see Outlook and Outlook Express using
1184 * cy set to CW_USEDEFAULT when opening the address book.
1186 else if (IS_DEFAULT(cs->cy))
1188 FIXME("Strange use of CW_USEDEFAULT in nHeight\n");
1189 cs->cy = (mon_info.rcWork.bottom - mon_info.rcWork.top) * 3 / 4 - cs->y;
1192 #undef IS_DEFAULT
1195 /***********************************************************************
1196 * dump_window_styles
1198 static void dump_window_styles( DWORD style, DWORD exstyle )
1200 TRACE( "style:" );
1201 if(style & WS_POPUP) TRACE(" WS_POPUP");
1202 if(style & WS_CHILD) TRACE(" WS_CHILD");
1203 if(style & WS_MINIMIZE) TRACE(" WS_MINIMIZE");
1204 if(style & WS_VISIBLE) TRACE(" WS_VISIBLE");
1205 if(style & WS_DISABLED) TRACE(" WS_DISABLED");
1206 if(style & WS_CLIPSIBLINGS) TRACE(" WS_CLIPSIBLINGS");
1207 if(style & WS_CLIPCHILDREN) TRACE(" WS_CLIPCHILDREN");
1208 if(style & WS_MAXIMIZE) TRACE(" WS_MAXIMIZE");
1209 if((style & WS_CAPTION) == WS_CAPTION) TRACE(" WS_CAPTION");
1210 else
1212 if(style & WS_BORDER) TRACE(" WS_BORDER");
1213 if(style & WS_DLGFRAME) TRACE(" WS_DLGFRAME");
1215 if(style & WS_VSCROLL) TRACE(" WS_VSCROLL");
1216 if(style & WS_HSCROLL) TRACE(" WS_HSCROLL");
1217 if(style & WS_SYSMENU) TRACE(" WS_SYSMENU");
1218 if(style & WS_THICKFRAME) TRACE(" WS_THICKFRAME");
1219 if (style & WS_CHILD)
1221 if(style & WS_GROUP) TRACE(" WS_GROUP");
1222 if(style & WS_TABSTOP) TRACE(" WS_TABSTOP");
1224 else
1226 if(style & WS_MINIMIZEBOX) TRACE(" WS_MINIMIZEBOX");
1227 if(style & WS_MAXIMIZEBOX) TRACE(" WS_MAXIMIZEBOX");
1230 /* FIXME: Add dumping of BS_/ES_/SBS_/LBS_/CBS_/DS_/etc. styles */
1231 #define DUMPED_STYLES \
1232 ((DWORD)(WS_POPUP | \
1233 WS_CHILD | \
1234 WS_MINIMIZE | \
1235 WS_VISIBLE | \
1236 WS_DISABLED | \
1237 WS_CLIPSIBLINGS | \
1238 WS_CLIPCHILDREN | \
1239 WS_MAXIMIZE | \
1240 WS_BORDER | \
1241 WS_DLGFRAME | \
1242 WS_VSCROLL | \
1243 WS_HSCROLL | \
1244 WS_SYSMENU | \
1245 WS_THICKFRAME | \
1246 WS_GROUP | \
1247 WS_TABSTOP | \
1248 WS_MINIMIZEBOX | \
1249 WS_MAXIMIZEBOX))
1251 if(style & ~DUMPED_STYLES) TRACE(" %08x", style & ~DUMPED_STYLES);
1252 TRACE("\n");
1253 #undef DUMPED_STYLES
1255 TRACE( "exstyle:" );
1256 if(exstyle & WS_EX_DLGMODALFRAME) TRACE(" WS_EX_DLGMODALFRAME");
1257 if(exstyle & WS_EX_DRAGDETECT) TRACE(" WS_EX_DRAGDETECT");
1258 if(exstyle & WS_EX_NOPARENTNOTIFY) TRACE(" WS_EX_NOPARENTNOTIFY");
1259 if(exstyle & WS_EX_TOPMOST) TRACE(" WS_EX_TOPMOST");
1260 if(exstyle & WS_EX_ACCEPTFILES) TRACE(" WS_EX_ACCEPTFILES");
1261 if(exstyle & WS_EX_TRANSPARENT) TRACE(" WS_EX_TRANSPARENT");
1262 if(exstyle & WS_EX_MDICHILD) TRACE(" WS_EX_MDICHILD");
1263 if(exstyle & WS_EX_TOOLWINDOW) TRACE(" WS_EX_TOOLWINDOW");
1264 if(exstyle & WS_EX_WINDOWEDGE) TRACE(" WS_EX_WINDOWEDGE");
1265 if(exstyle & WS_EX_CLIENTEDGE) TRACE(" WS_EX_CLIENTEDGE");
1266 if(exstyle & WS_EX_CONTEXTHELP) TRACE(" WS_EX_CONTEXTHELP");
1267 if(exstyle & WS_EX_RIGHT) TRACE(" WS_EX_RIGHT");
1268 if(exstyle & WS_EX_RTLREADING) TRACE(" WS_EX_RTLREADING");
1269 if(exstyle & WS_EX_LEFTSCROLLBAR) TRACE(" WS_EX_LEFTSCROLLBAR");
1270 if(exstyle & WS_EX_CONTROLPARENT) TRACE(" WS_EX_CONTROLPARENT");
1271 if(exstyle & WS_EX_STATICEDGE) TRACE(" WS_EX_STATICEDGE");
1272 if(exstyle & WS_EX_APPWINDOW) TRACE(" WS_EX_APPWINDOW");
1273 if(exstyle & WS_EX_LAYERED) TRACE(" WS_EX_LAYERED");
1274 if(exstyle & WS_EX_NOINHERITLAYOUT) TRACE(" WS_EX_NOINHERITLAYOUT");
1275 if(exstyle & WS_EX_LAYOUTRTL) TRACE(" WS_EX_LAYOUTRTL");
1276 if(exstyle & WS_EX_COMPOSITED) TRACE(" WS_EX_COMPOSITED");
1277 if(exstyle & WS_EX_NOACTIVATE) TRACE(" WS_EX_NOACTIVATE");
1279 #define DUMPED_EX_STYLES \
1280 ((DWORD)(WS_EX_DLGMODALFRAME | \
1281 WS_EX_DRAGDETECT | \
1282 WS_EX_NOPARENTNOTIFY | \
1283 WS_EX_TOPMOST | \
1284 WS_EX_ACCEPTFILES | \
1285 WS_EX_TRANSPARENT | \
1286 WS_EX_MDICHILD | \
1287 WS_EX_TOOLWINDOW | \
1288 WS_EX_WINDOWEDGE | \
1289 WS_EX_CLIENTEDGE | \
1290 WS_EX_CONTEXTHELP | \
1291 WS_EX_RIGHT | \
1292 WS_EX_RTLREADING | \
1293 WS_EX_LEFTSCROLLBAR | \
1294 WS_EX_CONTROLPARENT | \
1295 WS_EX_STATICEDGE | \
1296 WS_EX_APPWINDOW | \
1297 WS_EX_LAYERED | \
1298 WS_EX_NOINHERITLAYOUT | \
1299 WS_EX_LAYOUTRTL | \
1300 WS_EX_COMPOSITED |\
1301 WS_EX_NOACTIVATE))
1303 if(exstyle & ~DUMPED_EX_STYLES) TRACE(" %08x", exstyle & ~DUMPED_EX_STYLES);
1304 TRACE("\n");
1305 #undef DUMPED_EX_STYLES
1308 /***********************************************************************
1309 * map_dpi_create_struct
1311 static void map_dpi_create_struct( CREATESTRUCTW *cs, UINT dpi_from, UINT dpi_to )
1313 if (!dpi_from && !dpi_to) return;
1314 if (!dpi_from || !dpi_to)
1316 POINT pt = { cs->x, cs->y };
1317 UINT mon_dpi = get_monitor_dpi( MonitorFromPoint( pt, MONITOR_DEFAULTTONEAREST ));
1318 if (!dpi_from) dpi_from = mon_dpi;
1319 else dpi_to = mon_dpi;
1321 if (dpi_from == dpi_to) return;
1322 cs->x = MulDiv( cs->x, dpi_to, dpi_from );
1323 cs->y = MulDiv( cs->y, dpi_to, dpi_from );
1324 cs->cx = MulDiv( cs->cx, dpi_to, dpi_from );
1325 cs->cy = MulDiv( cs->cy, dpi_to, dpi_from );
1328 /***********************************************************************
1329 * WIN_CreateWindowEx
1331 * Implementation of CreateWindowEx().
1333 HWND WIN_CreateWindowEx( CREATESTRUCTW *cs, LPCWSTR className, HINSTANCE module, BOOL unicode )
1335 INT cx, cy, style, sw = SW_SHOW;
1336 LRESULT result;
1337 RECT rect;
1338 WND *wndPtr;
1339 HWND hwnd, parent, owner, top_child = 0;
1340 const WCHAR *p = className;
1341 UINT win_dpi, thread_dpi = get_thread_dpi();
1342 DPI_AWARENESS_CONTEXT context;
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 style = wndPtr->dwStyle;
1575 win_dpi = wndPtr->dpi;
1576 WIN_ReleasePtr( wndPtr );
1578 if (parent) map_dpi_create_struct( cs, thread_dpi, win_dpi );
1580 context = SetThreadDpiAwarenessContext( GetWindowDpiAwarenessContext( hwnd ));
1582 /* call the WH_CBT hook */
1584 /* the window style passed to the hook must be the real window style,
1585 * rather than just the window style that the caller to CreateWindowEx
1586 * passed in, so we have to copy the original CREATESTRUCT and get the
1587 * the real style. */
1588 cbcs = *cs;
1589 cbcs.style = style;
1590 cbtc.lpcs = &cbcs;
1591 cbtc.hwndInsertAfter = HWND_TOP;
1592 if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode )) goto failed;
1594 /* send the WM_GETMINMAXINFO message and fix the size if needed */
1596 cx = cs->cx;
1597 cy = cs->cy;
1598 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
1600 MINMAXINFO info = WINPOS_GetMinMaxInfo( hwnd );
1601 cx = max( min( cx, info.ptMaxTrackSize.x ), info.ptMinTrackSize.x );
1602 cy = max( min( cy, info.ptMaxTrackSize.y ), info.ptMinTrackSize.y );
1605 if (cx < 0) cx = 0;
1606 if (cy < 0) cy = 0;
1607 SetRect( &rect, cs->x, cs->y, cs->x + cx, cs->y + cy );
1608 /* check for wraparound */
1609 if (cs->x + cx < cs->x) rect.right = 0x7fffffff;
1610 if (cs->y + cy < cs->y) rect.bottom = 0x7fffffff;
1611 if (!set_window_pos( hwnd, 0, SWP_NOZORDER | SWP_NOACTIVATE, &rect, &rect, NULL )) goto failed;
1613 /* send WM_NCCREATE */
1615 TRACE( "hwnd %p cs %d,%d %dx%d %s\n", hwnd, cs->x, cs->y, cs->cx, cs->cy, wine_dbgstr_rect(&rect) );
1616 if (unicode)
1617 result = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1618 else
1619 result = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1620 if (!result)
1622 WARN( "%p: aborted by WM_NCCREATE\n", hwnd );
1623 goto failed;
1626 /* create default IME window */
1628 if (imm_register_window && !is_desktop_window( hwnd ) &&
1629 parent != get_hwnd_message_parent() && imm_register_window( hwnd ))
1631 TRACE("register IME window for %p\n", hwnd);
1632 win_set_flags( hwnd, WIN_HAS_IME_WIN, 0 );
1635 /* send WM_NCCALCSIZE */
1637 if (WIN_GetRectangles( hwnd, COORDS_PARENT, &rect, NULL ))
1639 /* yes, even if the CBT hook was called with HWND_TOP */
1640 HWND insert_after = (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
1641 RECT client_rect = rect;
1643 /* the rectangle is in screen coords for WM_NCCALCSIZE when wparam is FALSE */
1644 MapWindowPoints( parent, 0, (POINT *)&client_rect, 2 );
1645 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&client_rect );
1646 MapWindowPoints( 0, parent, (POINT *)&client_rect, 2 );
1647 set_window_pos( hwnd, insert_after, SWP_NOACTIVATE, &rect, &client_rect, NULL );
1649 else goto failed;
1651 /* send WM_CREATE */
1653 if (unicode)
1654 result = SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs );
1655 else
1656 result = SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs );
1657 if (result == -1) goto failed;
1659 /* call the driver */
1661 if (!USER_Driver->pCreateWindow( hwnd )) goto failed;
1663 NotifyWinEvent(EVENT_OBJECT_CREATE, hwnd, OBJID_WINDOW, 0);
1665 /* send the size messages */
1667 if (!(win_get_flags( hwnd ) & WIN_NEED_SIZE))
1669 WIN_GetRectangles( hwnd, COORDS_PARENT, NULL, &rect );
1670 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1671 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1672 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( rect.left, rect.top ) );
1675 /* Show the window, maximizing or minimizing if needed */
1677 style = WIN_SetStyle( hwnd, 0, WS_MAXIMIZE | WS_MINIMIZE );
1678 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1680 RECT newPos;
1681 UINT swFlag = (style & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1683 swFlag = WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1684 swFlag |= SWP_FRAMECHANGED; /* Frame always gets changed */
1685 if (!(style & WS_VISIBLE) || (style & WS_CHILD) || GetActiveWindow()) swFlag |= SWP_NOACTIVATE;
1686 SetWindowPos( hwnd, 0, newPos.left, newPos.top, newPos.right - newPos.left,
1687 newPos.bottom - newPos.top, swFlag );
1690 /* Notify the parent window only */
1692 send_parent_notify( hwnd, WM_CREATE );
1693 if (!IsWindow( hwnd ))
1695 SetThreadDpiAwarenessContext( context );
1696 return 0;
1699 if (parent == GetDesktopWindow())
1700 PostMessageW( parent, WM_PARENTNOTIFY, WM_CREATE, (LPARAM)hwnd );
1702 if (cs->style & WS_VISIBLE)
1704 if (cs->style & WS_MAXIMIZE)
1705 sw = SW_SHOW;
1706 else if (cs->style & WS_MINIMIZE)
1707 sw = SW_SHOWMINIMIZED;
1709 ShowWindow( hwnd, sw );
1710 if (cs->dwExStyle & WS_EX_MDICHILD)
1712 SendMessageW(cs->hwndParent, WM_MDIREFRESHMENU, 0, 0);
1713 /* ShowWindow won't activate child windows */
1714 SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE );
1718 /* Call WH_SHELL hook */
1720 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) && !GetWindow( hwnd, GW_OWNER ))
1721 HOOK_CallHooks( WH_SHELL, HSHELL_WINDOWCREATED, (WPARAM)hwnd, 0, TRUE );
1723 TRACE("created window %p\n", hwnd);
1724 SetThreadDpiAwarenessContext( context );
1725 return hwnd;
1727 failed:
1728 WIN_DestroyWindow( hwnd );
1729 SetThreadDpiAwarenessContext( context );
1730 return 0;
1734 /***********************************************************************
1735 * CreateWindowExA (USER32.@)
1737 HWND WINAPI DECLSPEC_HOTPATCH CreateWindowExA( DWORD exStyle, LPCSTR className,
1738 LPCSTR windowName, DWORD style, INT x,
1739 INT y, INT width, INT height,
1740 HWND parent, HMENU menu,
1741 HINSTANCE instance, LPVOID data )
1743 CREATESTRUCTA cs;
1745 cs.lpCreateParams = data;
1746 cs.hInstance = instance;
1747 cs.hMenu = menu;
1748 cs.hwndParent = parent;
1749 cs.x = x;
1750 cs.y = y;
1751 cs.cx = width;
1752 cs.cy = height;
1753 cs.style = style;
1754 cs.lpszName = windowName;
1755 cs.lpszClass = className;
1756 cs.dwExStyle = exStyle;
1758 if (!IS_INTRESOURCE(className))
1760 WCHAR bufferW[256];
1761 if (!MultiByteToWideChar( CP_ACP, 0, className, -1, bufferW, ARRAY_SIZE( bufferW )))
1762 return 0;
1763 return wow_handlers.create_window( (CREATESTRUCTW *)&cs, bufferW, instance, FALSE );
1765 /* Note: we rely on the fact that CREATESTRUCTA and */
1766 /* CREATESTRUCTW have the same layout. */
1767 return wow_handlers.create_window( (CREATESTRUCTW *)&cs, (LPCWSTR)className, instance, FALSE );
1771 /***********************************************************************
1772 * CreateWindowExW (USER32.@)
1774 HWND WINAPI DECLSPEC_HOTPATCH CreateWindowExW( DWORD exStyle, LPCWSTR className,
1775 LPCWSTR windowName, DWORD style, INT x,
1776 INT y, INT width, INT height,
1777 HWND parent, HMENU menu,
1778 HINSTANCE instance, LPVOID data )
1780 CREATESTRUCTW cs;
1782 cs.lpCreateParams = data;
1783 cs.hInstance = instance;
1784 cs.hMenu = menu;
1785 cs.hwndParent = parent;
1786 cs.x = x;
1787 cs.y = y;
1788 cs.cx = width;
1789 cs.cy = height;
1790 cs.style = style;
1791 cs.lpszName = windowName;
1792 cs.lpszClass = className;
1793 cs.dwExStyle = exStyle;
1795 return wow_handlers.create_window( &cs, className, instance, TRUE );
1799 /***********************************************************************
1800 * WIN_SendDestroyMsg
1802 static void WIN_SendDestroyMsg( HWND hwnd )
1804 GUITHREADINFO info;
1806 info.cbSize = sizeof(info);
1807 if (GetGUIThreadInfo( GetCurrentThreadId(), &info ))
1809 if (hwnd == info.hwndCaret) DestroyCaret();
1810 if (hwnd == info.hwndActive) WINPOS_ActivateOtherWindow( hwnd );
1813 if (hwnd == GetClipboardOwner()) CLIPBOARD_ReleaseOwner( hwnd );
1816 * Send the WM_DESTROY to the window.
1818 SendMessageW( hwnd, WM_DESTROY, 0, 0);
1821 * This WM_DESTROY message can trigger re-entrant calls to DestroyWindow
1822 * make sure that the window still exists when we come back.
1824 if (IsWindow(hwnd))
1826 HWND* pWndArray;
1827 int i;
1829 if (!(pWndArray = WIN_ListChildren( hwnd ))) return;
1831 for (i = 0; pWndArray[i]; i++)
1833 if (IsWindow( pWndArray[i] )) WIN_SendDestroyMsg( pWndArray[i] );
1835 HeapFree( GetProcessHeap(), 0, pWndArray );
1837 else
1838 WARN("\tdestroyed itself while in WM_DESTROY!\n");
1842 /***********************************************************************
1843 * DestroyWindow (USER32.@)
1845 BOOL WINAPI DestroyWindow( HWND hwnd )
1847 BOOL is_child;
1849 if (!(hwnd = WIN_IsCurrentThread( hwnd )) || is_desktop_window( hwnd ))
1851 SetLastError( ERROR_ACCESS_DENIED );
1852 return FALSE;
1855 TRACE("(%p)\n", hwnd);
1857 /* Call hooks */
1859 if (HOOK_CallHooks( WH_CBT, HCBT_DESTROYWND, (WPARAM)hwnd, 0, TRUE )) return FALSE;
1861 if (MENU_IsMenuActive() == hwnd)
1862 EndMenu();
1864 is_child = (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) != 0;
1866 if (is_child)
1868 if (!USER_IsExitingThread( GetCurrentThreadId() ))
1869 send_parent_notify( hwnd, WM_DESTROY );
1871 else if (!GetWindow( hwnd, GW_OWNER ))
1873 HOOK_CallHooks( WH_SHELL, HSHELL_WINDOWDESTROYED, (WPARAM)hwnd, 0L, TRUE );
1874 /* FIXME: clean up palette - see "Internals" p.352 */
1877 if (!IsWindow(hwnd)) return TRUE;
1879 /* Hide the window */
1880 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)
1882 /* Only child windows receive WM_SHOWWINDOW in DestroyWindow() */
1883 if (is_child)
1884 ShowWindow( hwnd, SW_HIDE );
1885 else
1886 SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE |
1887 SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW );
1890 if (!IsWindow(hwnd)) return TRUE;
1892 /* Recursively destroy owned windows */
1894 if (!is_child)
1896 for (;;)
1898 int i;
1899 BOOL got_one = FALSE;
1900 HWND *list = WIN_ListChildren( GetDesktopWindow() );
1901 if (list)
1903 for (i = 0; list[i]; i++)
1905 if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
1906 if (WIN_IsCurrentThread( list[i] ))
1908 DestroyWindow( list[i] );
1909 got_one = TRUE;
1910 continue;
1912 WIN_SetOwner( list[i], 0 );
1914 HeapFree( GetProcessHeap(), 0, list );
1916 if (!got_one) break;
1920 /* Send destroy messages */
1922 WIN_SendDestroyMsg( hwnd );
1923 if (!IsWindow( hwnd )) return TRUE;
1925 /* Destroy the window storage */
1927 WIN_DestroyWindow( hwnd );
1928 return TRUE;
1932 /***********************************************************************
1933 * CloseWindow (USER32.@)
1935 BOOL WINAPI CloseWindow( HWND hwnd )
1937 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) return FALSE;
1938 ShowWindow( hwnd, SW_MINIMIZE );
1939 return TRUE;
1943 /***********************************************************************
1944 * OpenIcon (USER32.@)
1946 BOOL WINAPI OpenIcon( HWND hwnd )
1948 if (!IsIconic( hwnd )) return FALSE;
1949 ShowWindow( hwnd, SW_SHOWNORMAL );
1950 return TRUE;
1954 /***********************************************************************
1955 * FindWindowExW (USER32.@)
1957 HWND WINAPI FindWindowExW( HWND parent, HWND child, LPCWSTR className, LPCWSTR title )
1959 HWND *list;
1960 HWND retvalue = 0;
1961 int i = 0, len = 0;
1962 WCHAR *buffer = NULL;
1964 if (!parent && child) parent = GetDesktopWindow();
1965 else if (parent == HWND_MESSAGE) parent = get_hwnd_message_parent();
1967 if (title)
1969 len = strlenW(title) + 1; /* one extra char to check for chars beyond the end */
1970 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return 0;
1973 if (!(list = list_window_children( 0, parent, className, 0 ))) goto done;
1975 if (child)
1977 child = WIN_GetFullHandle( child );
1978 while (list[i] && list[i] != child) i++;
1979 if (!list[i]) goto done;
1980 i++; /* start from next window */
1983 if (title)
1985 while (list[i])
1987 if (InternalGetWindowText( list[i], buffer, len + 1 ))
1989 if (!strcmpiW( buffer, title )) break;
1991 else
1993 if (!title[0]) break;
1995 i++;
1998 retvalue = list[i];
2000 done:
2001 HeapFree( GetProcessHeap(), 0, list );
2002 HeapFree( GetProcessHeap(), 0, buffer );
2003 return retvalue;
2008 /***********************************************************************
2009 * FindWindowA (USER32.@)
2011 HWND WINAPI FindWindowA( LPCSTR className, LPCSTR title )
2013 HWND ret = FindWindowExA( 0, 0, className, title );
2014 if (!ret) SetLastError (ERROR_CANNOT_FIND_WND_CLASS);
2015 return ret;
2019 /***********************************************************************
2020 * FindWindowExA (USER32.@)
2022 HWND WINAPI FindWindowExA( HWND parent, HWND child, LPCSTR className, LPCSTR title )
2024 LPWSTR titleW = NULL;
2025 HWND hwnd = 0;
2027 if (title)
2029 DWORD len = MultiByteToWideChar( CP_ACP, 0, title, -1, NULL, 0 );
2030 if (!(titleW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return 0;
2031 MultiByteToWideChar( CP_ACP, 0, title, -1, titleW, len );
2034 if (!IS_INTRESOURCE(className))
2036 WCHAR classW[256];
2037 if (MultiByteToWideChar( CP_ACP, 0, className, -1, classW, ARRAY_SIZE( classW )))
2038 hwnd = FindWindowExW( parent, child, classW, titleW );
2040 else
2042 hwnd = FindWindowExW( parent, child, (LPCWSTR)className, titleW );
2045 HeapFree( GetProcessHeap(), 0, titleW );
2046 return hwnd;
2050 /***********************************************************************
2051 * FindWindowW (USER32.@)
2053 HWND WINAPI FindWindowW( LPCWSTR className, LPCWSTR title )
2055 return FindWindowExW( 0, 0, className, title );
2059 /**********************************************************************
2060 * GetDesktopWindow (USER32.@)
2062 HWND WINAPI GetDesktopWindow(void)
2064 struct user_thread_info *thread_info = get_user_thread_info();
2066 if (thread_info->top_window) return thread_info->top_window;
2068 SERVER_START_REQ( get_desktop_window )
2070 req->force = 0;
2071 if (!wine_server_call( req ))
2073 thread_info->top_window = wine_server_ptr_handle( reply->top_window );
2074 thread_info->msg_window = wine_server_ptr_handle( reply->msg_window );
2077 SERVER_END_REQ;
2079 if (!thread_info->top_window)
2081 static const WCHAR explorer[] = {'\\','e','x','p','l','o','r','e','r','.','e','x','e',0};
2082 static const WCHAR args[] = {' ','/','d','e','s','k','t','o','p',0};
2083 STARTUPINFOW si;
2084 PROCESS_INFORMATION pi;
2085 WCHAR windir[MAX_PATH];
2086 WCHAR app[MAX_PATH + ARRAY_SIZE( explorer )];
2087 WCHAR cmdline[MAX_PATH + ARRAY_SIZE( explorer ) + ARRAY_SIZE( args )];
2088 WCHAR desktop[MAX_PATH];
2089 void *redir;
2091 SERVER_START_REQ( set_user_object_info )
2093 req->handle = wine_server_obj_handle( GetThreadDesktop(GetCurrentThreadId()) );
2094 req->flags = SET_USER_OBJECT_GET_FULL_NAME;
2095 wine_server_set_reply( req, desktop, sizeof(desktop) - sizeof(WCHAR) );
2096 if (!wine_server_call( req ))
2098 size_t size = wine_server_reply_size( reply );
2099 desktop[size / sizeof(WCHAR)] = 0;
2100 TRACE( "starting explorer for desktop %s\n", debugstr_w(desktop) );
2102 else
2103 desktop[0] = 0;
2105 SERVER_END_REQ;
2107 memset( &si, 0, sizeof(si) );
2108 si.cb = sizeof(si);
2109 si.lpDesktop = *desktop ? desktop : NULL;
2110 si.dwFlags = STARTF_USESTDHANDLES;
2111 si.hStdInput = 0;
2112 si.hStdOutput = 0;
2113 si.hStdError = GetStdHandle( STD_ERROR_HANDLE );
2115 GetSystemDirectoryW( windir, MAX_PATH );
2116 strcpyW( app, windir );
2117 strcatW( app, explorer );
2118 strcpyW( cmdline, app );
2119 strcatW( cmdline, args );
2121 Wow64DisableWow64FsRedirection( &redir );
2122 if (CreateProcessW( app, cmdline, NULL, NULL, FALSE, DETACHED_PROCESS,
2123 NULL, windir, &si, &pi ))
2125 TRACE( "started explorer pid %04x tid %04x\n", pi.dwProcessId, pi.dwThreadId );
2126 WaitForInputIdle( pi.hProcess, 10000 );
2127 CloseHandle( pi.hThread );
2128 CloseHandle( pi.hProcess );
2130 else WARN( "failed to start explorer, err %d\n", GetLastError() );
2131 Wow64RevertWow64FsRedirection( redir );
2133 SERVER_START_REQ( get_desktop_window )
2135 req->force = 1;
2136 if (!wine_server_call( req ))
2138 thread_info->top_window = wine_server_ptr_handle( reply->top_window );
2139 thread_info->msg_window = wine_server_ptr_handle( reply->msg_window );
2142 SERVER_END_REQ;
2145 if (!thread_info->top_window || !USER_Driver->pCreateDesktopWindow( thread_info->top_window ))
2146 ERR( "failed to create desktop window\n" );
2148 return thread_info->top_window;
2152 /*******************************************************************
2153 * EnableWindow (USER32.@)
2155 BOOL WINAPI EnableWindow( HWND hwnd, BOOL enable )
2157 BOOL retvalue;
2159 if (is_broadcast(hwnd))
2161 SetLastError( ERROR_INVALID_PARAMETER );
2162 return FALSE;
2165 TRACE("( %p, %d )\n", hwnd, enable);
2167 if (enable)
2169 retvalue = (WIN_SetStyle( hwnd, 0, WS_DISABLED ) & WS_DISABLED) != 0;
2170 if (retvalue) SendMessageW( hwnd, WM_ENABLE, TRUE, 0 );
2172 else
2174 SendMessageW( hwnd, WM_CANCELMODE, 0, 0 );
2176 retvalue = (WIN_SetStyle( hwnd, WS_DISABLED, 0 ) & WS_DISABLED) != 0;
2177 if (!retvalue)
2179 if (hwnd == GetFocus())
2180 SetFocus( 0 ); /* A disabled window can't have the focus */
2182 SendMessageW( hwnd, WM_ENABLE, FALSE, 0 );
2185 return retvalue;
2189 /***********************************************************************
2190 * IsWindowEnabled (USER32.@)
2192 BOOL WINAPI IsWindowEnabled(HWND hWnd)
2194 LONG ret;
2196 SetLastError(NO_ERROR);
2197 ret = GetWindowLongW( hWnd, GWL_STYLE );
2198 if (!ret && GetLastError() != NO_ERROR) return FALSE;
2199 return !(ret & WS_DISABLED);
2202 /***********************************************************************
2203 * IsWindowUnicode (USER32.@)
2205 BOOL WINAPI IsWindowUnicode( HWND hwnd )
2207 WND * wndPtr;
2208 BOOL retvalue = FALSE;
2210 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
2212 if (wndPtr == WND_DESKTOP) return TRUE;
2214 if (wndPtr != WND_OTHER_PROCESS)
2216 retvalue = (wndPtr->flags & WIN_ISUNICODE) != 0;
2217 WIN_ReleasePtr( wndPtr );
2219 else
2221 SERVER_START_REQ( get_window_info )
2223 req->handle = wine_server_user_handle( hwnd );
2224 if (!wine_server_call_err( req )) retvalue = reply->is_unicode;
2226 SERVER_END_REQ;
2228 return retvalue;
2232 /***********************************************************************
2233 * GetWindowDpiAwarenessContext (USER32.@)
2235 DPI_AWARENESS_CONTEXT WINAPI GetWindowDpiAwarenessContext( HWND hwnd )
2237 WND *win;
2238 DPI_AWARENESS_CONTEXT ret = 0;
2240 if (!(win = WIN_GetPtr( hwnd )))
2242 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2243 return 0;
2245 if (win == WND_DESKTOP) return DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE;
2246 if (win != WND_OTHER_PROCESS)
2248 ret = ULongToHandle( win->dpi_awareness | 0x10 );
2249 WIN_ReleasePtr( win );
2251 else
2253 SERVER_START_REQ( get_window_info )
2255 req->handle = wine_server_user_handle( hwnd );
2256 if (!wine_server_call_err( req )) ret = ULongToHandle( reply->awareness | 0x10 );
2258 SERVER_END_REQ;
2260 return ret;
2264 /***********************************************************************
2265 * GetDpiForWindow (USER32.@)
2267 UINT WINAPI GetDpiForWindow( HWND hwnd )
2269 WND *win;
2270 UINT ret = 0;
2272 if (!(win = WIN_GetPtr( hwnd )))
2274 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2275 return 0;
2277 if (win == WND_DESKTOP)
2279 POINT pt = { 0, 0 };
2280 return get_monitor_dpi( MonitorFromPoint( pt, MONITOR_DEFAULTTOPRIMARY ));
2282 if (win != WND_OTHER_PROCESS)
2284 ret = win->dpi;
2285 if (!ret) ret = get_win_monitor_dpi( hwnd );
2286 WIN_ReleasePtr( win );
2288 else
2290 SERVER_START_REQ( get_window_info )
2292 req->handle = wine_server_user_handle( hwnd );
2293 if (!wine_server_call_err( req )) ret = reply->dpi;
2295 SERVER_END_REQ;
2297 return ret;
2301 /**********************************************************************
2302 * WIN_GetWindowLong
2304 * Helper function for GetWindowLong().
2306 static LONG_PTR WIN_GetWindowLong( HWND hwnd, INT offset, UINT size, BOOL unicode )
2308 LONG_PTR retvalue = 0;
2309 WND *wndPtr;
2311 if (offset == GWLP_HWNDPARENT)
2313 HWND parent = GetAncestor( hwnd, GA_PARENT );
2314 if (parent == GetDesktopWindow()) parent = GetWindow( hwnd, GW_OWNER );
2315 return (ULONG_PTR)parent;
2318 if (!(wndPtr = WIN_GetPtr( hwnd )))
2320 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2321 return 0;
2324 if (wndPtr == WND_DESKTOP)
2326 switch (offset)
2328 case GWL_STYLE:
2329 retvalue = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN; /* message parent is not visible */
2330 if (WIN_GetFullHandle( hwnd ) == GetDesktopWindow())
2331 retvalue |= WS_VISIBLE;
2332 return retvalue;
2333 case GWL_EXSTYLE:
2334 case GWLP_USERDATA:
2335 case GWLP_ID:
2336 case GWLP_HINSTANCE:
2337 return 0;
2338 case GWLP_WNDPROC:
2339 SetLastError( ERROR_ACCESS_DENIED );
2340 return 0;
2342 SetLastError( ERROR_INVALID_INDEX );
2343 return 0;
2346 if (wndPtr == WND_OTHER_PROCESS)
2348 if (offset == GWLP_WNDPROC)
2350 SetLastError( ERROR_ACCESS_DENIED );
2351 return 0;
2353 SERVER_START_REQ( set_window_info )
2355 req->handle = wine_server_user_handle( hwnd );
2356 req->flags = 0; /* don't set anything, just retrieve */
2357 req->extra_offset = (offset >= 0) ? offset : -1;
2358 req->extra_size = (offset >= 0) ? size : 0;
2359 if (!wine_server_call_err( req ))
2361 switch(offset)
2363 case GWL_STYLE: retvalue = reply->old_style; break;
2364 case GWL_EXSTYLE: retvalue = reply->old_ex_style; break;
2365 case GWLP_ID: retvalue = reply->old_id; break;
2366 case GWLP_HINSTANCE: retvalue = (ULONG_PTR)wine_server_get_ptr( reply->old_instance ); break;
2367 case GWLP_USERDATA: retvalue = reply->old_user_data; break;
2368 default:
2369 if (offset >= 0) retvalue = get_win_data( &reply->old_extra_value, size );
2370 else SetLastError( ERROR_INVALID_INDEX );
2371 break;
2375 SERVER_END_REQ;
2376 return retvalue;
2379 /* now we have a valid wndPtr */
2381 if (offset >= 0)
2383 if (offset > (int)(wndPtr->cbWndExtra - size))
2385 WARN("Invalid offset %d\n", offset );
2386 WIN_ReleasePtr( wndPtr );
2387 SetLastError( ERROR_INVALID_INDEX );
2388 return 0;
2390 retvalue = get_win_data( (char *)wndPtr->wExtra + offset, size );
2392 /* Special case for dialog window procedure */
2393 if ((offset == DWLP_DLGPROC) && (size == sizeof(LONG_PTR)) && wndPtr->dlgInfo)
2394 retvalue = (LONG_PTR)WINPROC_GetProc( (WNDPROC)retvalue, unicode );
2395 WIN_ReleasePtr( wndPtr );
2396 return retvalue;
2399 switch(offset)
2401 case GWLP_USERDATA: retvalue = wndPtr->userdata; break;
2402 case GWL_STYLE: retvalue = wndPtr->dwStyle; break;
2403 case GWL_EXSTYLE: retvalue = wndPtr->dwExStyle; break;
2404 case GWLP_ID: retvalue = wndPtr->wIDmenu; break;
2405 case GWLP_HINSTANCE: retvalue = (ULONG_PTR)wndPtr->hInstance; break;
2406 case GWLP_WNDPROC:
2407 /* This looks like a hack only for the edit control (see tests). This makes these controls
2408 * more tolerant to A/W mismatches. The lack of W->A->W conversion for such a mismatch suggests
2409 * that the hack is in GetWindowLongPtr[AW], not in winprocs.
2411 if (wndPtr->winproc == BUILTIN_WINPROC(WINPROC_EDIT) && (!unicode != !(wndPtr->flags & WIN_ISUNICODE)))
2412 retvalue = (ULONG_PTR)wndPtr->winproc;
2413 else
2414 retvalue = (ULONG_PTR)WINPROC_GetProc( wndPtr->winproc, unicode );
2415 break;
2416 default:
2417 WARN("Unknown offset %d\n", offset );
2418 SetLastError( ERROR_INVALID_INDEX );
2419 break;
2421 WIN_ReleasePtr(wndPtr);
2422 return retvalue;
2426 /**********************************************************************
2427 * WIN_SetWindowLong
2429 * Helper function for SetWindowLong().
2431 * 0 is the failure code. However, in the case of failure SetLastError
2432 * must be set to distinguish between a 0 return value and a failure.
2434 LONG_PTR WIN_SetWindowLong( HWND hwnd, INT offset, UINT size, LONG_PTR newval, BOOL unicode )
2436 STYLESTRUCT style;
2437 BOOL ok, made_visible = FALSE;
2438 LONG_PTR retval = 0;
2439 WND *wndPtr;
2441 TRACE( "%p %d %lx %c\n", hwnd, offset, newval, unicode ? 'W' : 'A' );
2443 if (is_broadcast(hwnd))
2445 SetLastError( ERROR_INVALID_PARAMETER );
2446 return FALSE;
2449 if (!(wndPtr = WIN_GetPtr( hwnd )))
2451 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2452 return 0;
2454 if (wndPtr == WND_DESKTOP)
2456 /* can't change anything on the desktop window */
2457 SetLastError( ERROR_ACCESS_DENIED );
2458 return 0;
2460 if (wndPtr == WND_OTHER_PROCESS)
2462 if (offset == GWLP_WNDPROC)
2464 SetLastError( ERROR_ACCESS_DENIED );
2465 return 0;
2467 if (offset > 32767 || offset < -32767)
2469 SetLastError( ERROR_INVALID_INDEX );
2470 return 0;
2472 return SendMessageW( hwnd, WM_WINE_SETWINDOWLONG, MAKEWPARAM( offset, size ), newval );
2475 /* first some special cases */
2476 switch( offset )
2478 case GWL_STYLE:
2479 style.styleOld = wndPtr->dwStyle;
2480 style.styleNew = newval;
2481 WIN_ReleasePtr( wndPtr );
2482 SendMessageW( hwnd, WM_STYLECHANGING, GWL_STYLE, (LPARAM)&style );
2483 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
2484 newval = style.styleNew;
2485 /* WS_CLIPSIBLINGS can't be reset on top-level windows */
2486 if (wndPtr->parent == GetDesktopWindow()) newval |= WS_CLIPSIBLINGS;
2487 /* WS_MINIMIZE can't be reset */
2488 if (wndPtr->dwStyle & WS_MINIMIZE) newval |= WS_MINIMIZE;
2489 /* FIXME: changing WS_DLGFRAME | WS_THICKFRAME is supposed to change
2490 WS_EX_WINDOWEDGE too */
2491 break;
2492 case GWL_EXSTYLE:
2493 style.styleOld = wndPtr->dwExStyle;
2494 style.styleNew = newval;
2495 WIN_ReleasePtr( wndPtr );
2496 SendMessageW( hwnd, WM_STYLECHANGING, GWL_EXSTYLE, (LPARAM)&style );
2497 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
2498 /* WS_EX_TOPMOST can only be changed through SetWindowPos */
2499 newval = (style.styleNew & ~WS_EX_TOPMOST) | (wndPtr->dwExStyle & WS_EX_TOPMOST);
2500 /* WS_EX_WINDOWEDGE depends on some other styles */
2501 if (newval & WS_EX_DLGMODALFRAME)
2502 newval |= WS_EX_WINDOWEDGE;
2503 else if (!(newval & WS_EX_STATICEDGE) && (wndPtr->dwStyle & (WS_DLGFRAME | WS_THICKFRAME)))
2504 newval |= WS_EX_WINDOWEDGE;
2505 else
2506 newval &= ~WS_EX_WINDOWEDGE;
2507 break;
2508 case GWLP_HWNDPARENT:
2509 if (wndPtr->parent == GetDesktopWindow())
2511 WIN_ReleasePtr( wndPtr );
2512 return (ULONG_PTR)WIN_SetOwner( hwnd, (HWND)newval );
2514 else
2516 WIN_ReleasePtr( wndPtr );
2517 return (ULONG_PTR)SetParent( hwnd, (HWND)newval );
2519 case GWLP_WNDPROC:
2521 WNDPROC proc;
2522 UINT old_flags = wndPtr->flags;
2523 retval = WIN_GetWindowLong( hwnd, offset, size, unicode );
2524 proc = WINPROC_AllocProc( (WNDPROC)newval, unicode );
2525 if (proc) wndPtr->winproc = proc;
2526 if (WINPROC_IsUnicode( proc, unicode )) wndPtr->flags |= WIN_ISUNICODE;
2527 else wndPtr->flags &= ~WIN_ISUNICODE;
2528 if (!((old_flags ^ wndPtr->flags) & WIN_ISUNICODE))
2530 WIN_ReleasePtr( wndPtr );
2531 return retval;
2533 /* update is_unicode flag on the server side */
2534 break;
2536 case GWLP_ID:
2537 case GWLP_HINSTANCE:
2538 case GWLP_USERDATA:
2539 break;
2540 case DWLP_DLGPROC:
2541 if ((wndPtr->cbWndExtra - sizeof(LONG_PTR) >= DWLP_DLGPROC) &&
2542 (size == sizeof(LONG_PTR)) && wndPtr->dlgInfo)
2544 WNDPROC *ptr = (WNDPROC *)((char *)wndPtr->wExtra + DWLP_DLGPROC);
2545 retval = (ULONG_PTR)WINPROC_GetProc( *ptr, unicode );
2546 *ptr = WINPROC_AllocProc( (WNDPROC)newval, unicode );
2547 WIN_ReleasePtr( wndPtr );
2548 return retval;
2550 /* fall through */
2551 default:
2552 if (offset < 0 || offset > (int)(wndPtr->cbWndExtra - size))
2554 WARN("Invalid offset %d\n", offset );
2555 WIN_ReleasePtr( wndPtr );
2556 SetLastError( ERROR_INVALID_INDEX );
2557 return 0;
2559 else if (get_win_data( (char *)wndPtr->wExtra + offset, size ) == newval)
2561 /* already set to the same value */
2562 WIN_ReleasePtr( wndPtr );
2563 return newval;
2565 break;
2568 SERVER_START_REQ( set_window_info )
2570 req->handle = wine_server_user_handle( hwnd );
2571 req->extra_offset = -1;
2572 switch(offset)
2574 case GWL_STYLE:
2575 req->flags = SET_WIN_STYLE;
2576 req->style = newval;
2577 break;
2578 case GWL_EXSTYLE:
2579 req->flags = SET_WIN_EXSTYLE;
2580 req->ex_style = newval;
2581 break;
2582 case GWLP_ID:
2583 req->flags = SET_WIN_ID;
2584 req->id = newval;
2585 break;
2586 case GWLP_HINSTANCE:
2587 req->flags = SET_WIN_INSTANCE;
2588 req->instance = wine_server_client_ptr( (void *)newval );
2589 break;
2590 case GWLP_WNDPROC:
2591 req->flags = SET_WIN_UNICODE;
2592 req->is_unicode = (wndPtr->flags & WIN_ISUNICODE) != 0;
2593 break;
2594 case GWLP_USERDATA:
2595 req->flags = SET_WIN_USERDATA;
2596 req->user_data = newval;
2597 break;
2598 default:
2599 req->flags = SET_WIN_EXTRA;
2600 req->extra_offset = offset;
2601 req->extra_size = size;
2602 set_win_data( &req->extra_value, newval, size );
2604 if ((ok = !wine_server_call_err( req )))
2606 switch(offset)
2608 case GWL_STYLE:
2609 wndPtr->dwStyle = newval;
2610 retval = reply->old_style;
2611 break;
2612 case GWL_EXSTYLE:
2613 wndPtr->dwExStyle = newval;
2614 retval = reply->old_ex_style;
2615 break;
2616 case GWLP_ID:
2617 wndPtr->wIDmenu = newval;
2618 retval = reply->old_id;
2619 break;
2620 case GWLP_HINSTANCE:
2621 wndPtr->hInstance = (HINSTANCE)newval;
2622 retval = (ULONG_PTR)wine_server_get_ptr( reply->old_instance );
2623 break;
2624 case GWLP_WNDPROC:
2625 break;
2626 case GWLP_USERDATA:
2627 wndPtr->userdata = newval;
2628 retval = reply->old_user_data;
2629 break;
2630 default:
2631 retval = get_win_data( (char *)wndPtr->wExtra + offset, size );
2632 set_win_data( (char *)wndPtr->wExtra + offset, newval, size );
2633 break;
2637 SERVER_END_REQ;
2639 if ((offset == GWL_STYLE && ((style.styleOld ^ style.styleNew) & WS_VISIBLE)) ||
2640 (offset == GWL_EXSTYLE && ((style.styleOld ^ style.styleNew) & WS_EX_LAYERED)))
2642 made_visible = (wndPtr->dwStyle & WS_VISIBLE) != 0;
2643 invalidate_dce( wndPtr, NULL );
2645 WIN_ReleasePtr( wndPtr );
2647 if (!ok) return 0;
2649 if (offset == GWL_STYLE || offset == GWL_EXSTYLE)
2651 style.styleOld = retval;
2652 style.styleNew = newval;
2653 USER_Driver->pSetWindowStyle( hwnd, offset, &style );
2654 if (made_visible) update_window_state( hwnd );
2655 SendMessageW( hwnd, WM_STYLECHANGED, offset, (LPARAM)&style );
2658 return retval;
2662 /**********************************************************************
2663 * GetWindowWord (USER32.@)
2665 WORD WINAPI GetWindowWord( HWND hwnd, INT offset )
2667 switch(offset)
2669 case GWLP_ID:
2670 case GWLP_HINSTANCE:
2671 case GWLP_HWNDPARENT:
2672 break;
2673 default:
2674 if (offset < 0)
2676 WARN("Invalid offset %d\n", offset );
2677 SetLastError( ERROR_INVALID_INDEX );
2678 return 0;
2680 break;
2682 return WIN_GetWindowLong( hwnd, offset, sizeof(WORD), FALSE );
2686 /**********************************************************************
2687 * GetWindowLongA (USER32.@)
2689 LONG WINAPI GetWindowLongA( HWND hwnd, INT offset )
2691 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG), FALSE );
2695 /**********************************************************************
2696 * GetWindowLongW (USER32.@)
2698 LONG WINAPI GetWindowLongW( HWND hwnd, INT offset )
2700 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG), TRUE );
2704 /**********************************************************************
2705 * SetWindowWord (USER32.@)
2707 WORD WINAPI SetWindowWord( HWND hwnd, INT offset, WORD newval )
2709 switch(offset)
2711 case GWLP_ID:
2712 case GWLP_HINSTANCE:
2713 case GWLP_HWNDPARENT:
2714 break;
2715 default:
2716 if (offset < 0)
2718 WARN("Invalid offset %d\n", offset );
2719 SetLastError( ERROR_INVALID_INDEX );
2720 return 0;
2722 break;
2724 return WIN_SetWindowLong( hwnd, offset, sizeof(WORD), newval, FALSE );
2728 /**********************************************************************
2729 * SetWindowLongA (USER32.@)
2731 * See SetWindowLongW.
2733 LONG WINAPI DECLSPEC_HOTPATCH SetWindowLongA( HWND hwnd, INT offset, LONG newval )
2735 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG), newval, FALSE );
2739 /**********************************************************************
2740 * SetWindowLongW (USER32.@) Set window attribute
2742 * SetWindowLong() alters one of a window's attributes or sets a 32-bit (long)
2743 * value in a window's extra memory.
2745 * The _hwnd_ parameter specifies the handle to a window that
2746 * has extra memory. The _newval_ parameter contains the new
2747 * attribute or extra memory value. If positive, the _offset_
2748 * parameter is the byte-addressed location in the window's extra
2749 * memory to set. If negative, _offset_ specifies the window
2750 * attribute to set, and should be one of the following values:
2752 * GWL_EXSTYLE The window's extended window style
2754 * GWL_STYLE The window's window style.
2756 * GWLP_WNDPROC Pointer to the window's window procedure.
2758 * GWLP_HINSTANCE The window's application instance handle.
2760 * GWLP_ID The window's identifier.
2762 * GWLP_USERDATA The window's user-specified data.
2764 * If the window is a dialog box, the _offset_ parameter can be one of
2765 * the following values:
2767 * DWLP_DLGPROC The address of the window's dialog box procedure.
2769 * DWLP_MSGRESULT The return value of a message
2770 * that the dialog box procedure processed.
2772 * DWLP_USER Application specific information.
2774 * RETURNS
2776 * If successful, returns the previous value located at _offset_. Otherwise,
2777 * returns 0.
2779 * NOTES
2781 * Extra memory for a window class is specified by a nonzero cbWndExtra
2782 * parameter of the WNDCLASS structure passed to RegisterClass() at the
2783 * time of class creation.
2785 * Using GWL_WNDPROC to set a new window procedure effectively creates
2786 * a window subclass. Use CallWindowProc() in the new windows procedure
2787 * to pass messages to the superclass's window procedure.
2789 * The user data is reserved for use by the application which created
2790 * the window.
2792 * Do not use GWL_STYLE to change the window's WS_DISABLED style;
2793 * instead, call the EnableWindow() function to change the window's
2794 * disabled state.
2796 * Do not use GWL_HWNDPARENT to reset the window's parent, use
2797 * SetParent() instead.
2799 * Win95:
2800 * When offset is GWL_STYLE and the calling app's ver is 4.0,
2801 * it sends WM_STYLECHANGING before changing the settings
2802 * and WM_STYLECHANGED afterwards.
2803 * App ver 4.0 can't use SetWindowLong to change WS_EX_TOPMOST.
2805 LONG WINAPI DECLSPEC_HOTPATCH SetWindowLongW(
2806 HWND hwnd, /* [in] window to alter */
2807 INT offset, /* [in] offset, in bytes, of location to alter */
2808 LONG newval /* [in] new value of location */
2810 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG), newval, TRUE );
2814 /*******************************************************************
2815 * GetWindowTextA (USER32.@)
2817 INT WINAPI GetWindowTextA( HWND hwnd, LPSTR lpString, INT nMaxCount )
2819 WCHAR *buffer;
2821 if (!lpString || nMaxCount <= 0) return 0;
2823 if (WIN_IsCurrentProcess( hwnd ))
2825 lpString[0] = 0;
2826 return (INT)SendMessageA( hwnd, WM_GETTEXT, nMaxCount, (LPARAM)lpString );
2829 /* when window belongs to other process, don't send a message */
2830 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, nMaxCount * sizeof(WCHAR) ))) return 0;
2831 get_server_window_text( hwnd, buffer, nMaxCount );
2832 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, lpString, nMaxCount, NULL, NULL ))
2833 lpString[nMaxCount-1] = 0;
2834 HeapFree( GetProcessHeap(), 0, buffer );
2835 return strlen(lpString);
2839 /*******************************************************************
2840 * InternalGetWindowText (USER32.@)
2842 INT WINAPI InternalGetWindowText(HWND hwnd,LPWSTR lpString,INT nMaxCount )
2844 WND *win;
2846 if (nMaxCount <= 0) return 0;
2847 if (!(win = WIN_GetPtr( hwnd ))) return 0;
2848 if (win == WND_DESKTOP) lpString[0] = 0;
2849 else if (win != WND_OTHER_PROCESS)
2851 if (win->text) lstrcpynW( lpString, win->text, nMaxCount );
2852 else lpString[0] = 0;
2853 WIN_ReleasePtr( win );
2855 else
2857 get_server_window_text( hwnd, lpString, nMaxCount );
2859 return strlenW(lpString);
2863 /*******************************************************************
2864 * GetWindowTextW (USER32.@)
2866 INT WINAPI GetWindowTextW( HWND hwnd, LPWSTR lpString, INT nMaxCount )
2868 if (!lpString || nMaxCount <= 0) return 0;
2870 if (WIN_IsCurrentProcess( hwnd ))
2872 lpString[0] = 0;
2873 return (INT)SendMessageW( hwnd, WM_GETTEXT, nMaxCount, (LPARAM)lpString );
2876 /* when window belongs to other process, don't send a message */
2877 get_server_window_text( hwnd, lpString, nMaxCount );
2878 return strlenW(lpString);
2882 /*******************************************************************
2883 * SetWindowTextA (USER32.@)
2884 * SetWindowText (USER32.@)
2886 BOOL WINAPI DECLSPEC_HOTPATCH SetWindowTextA( HWND hwnd, LPCSTR lpString )
2888 if (is_broadcast(hwnd))
2890 SetLastError( ERROR_INVALID_PARAMETER );
2891 return FALSE;
2893 if (!WIN_IsCurrentProcess( hwnd ))
2894 WARN( "setting text %s of other process window %p should not use SendMessage\n",
2895 debugstr_a(lpString), hwnd );
2896 return (BOOL)SendMessageA( hwnd, WM_SETTEXT, 0, (LPARAM)lpString );
2900 /*******************************************************************
2901 * SetWindowTextW (USER32.@)
2903 BOOL WINAPI DECLSPEC_HOTPATCH SetWindowTextW( HWND hwnd, LPCWSTR lpString )
2905 if (is_broadcast(hwnd))
2907 SetLastError( ERROR_INVALID_PARAMETER );
2908 return FALSE;
2910 if (!WIN_IsCurrentProcess( hwnd ))
2911 WARN( "setting text %s of other process window %p should not use SendMessage\n",
2912 debugstr_w(lpString), hwnd );
2913 return (BOOL)SendMessageW( hwnd, WM_SETTEXT, 0, (LPARAM)lpString );
2917 /*******************************************************************
2918 * GetWindowTextLengthA (USER32.@)
2920 INT WINAPI GetWindowTextLengthA( HWND hwnd )
2922 CPINFO info;
2924 if (WIN_IsCurrentProcess( hwnd )) return SendMessageA( hwnd, WM_GETTEXTLENGTH, 0, 0 );
2926 /* when window belongs to other process, don't send a message */
2927 GetCPInfo( CP_ACP, &info );
2928 return get_server_window_text( hwnd, NULL, 0 ) * info.MaxCharSize;
2931 /*******************************************************************
2932 * GetWindowTextLengthW (USER32.@)
2934 INT WINAPI GetWindowTextLengthW( HWND hwnd )
2936 if (WIN_IsCurrentProcess( hwnd )) return SendMessageW( hwnd, WM_GETTEXTLENGTH, 0, 0 );
2938 /* when window belongs to other process, don't send a message */
2939 return get_server_window_text( hwnd, NULL, 0 );
2943 /*******************************************************************
2944 * IsWindow (USER32.@)
2946 BOOL WINAPI IsWindow( HWND hwnd )
2948 WND *ptr;
2949 BOOL ret;
2951 if (!(ptr = WIN_GetPtr( hwnd ))) return FALSE;
2952 if (ptr == WND_DESKTOP) return TRUE;
2954 if (ptr != WND_OTHER_PROCESS)
2956 WIN_ReleasePtr( ptr );
2957 return TRUE;
2960 /* check other processes */
2961 SERVER_START_REQ( get_window_info )
2963 req->handle = wine_server_user_handle( hwnd );
2964 ret = !wine_server_call_err( req );
2966 SERVER_END_REQ;
2967 return ret;
2971 /***********************************************************************
2972 * GetWindowThreadProcessId (USER32.@)
2974 DWORD WINAPI GetWindowThreadProcessId( HWND hwnd, LPDWORD process )
2976 WND *ptr;
2977 DWORD tid = 0;
2979 if (!(ptr = WIN_GetPtr( hwnd )))
2981 SetLastError( ERROR_INVALID_WINDOW_HANDLE);
2982 return 0;
2985 if (ptr != WND_OTHER_PROCESS && ptr != WND_DESKTOP)
2987 /* got a valid window */
2988 tid = ptr->tid;
2989 if (process) *process = GetCurrentProcessId();
2990 WIN_ReleasePtr( ptr );
2991 return tid;
2994 /* check other processes */
2995 SERVER_START_REQ( get_window_info )
2997 req->handle = wine_server_user_handle( hwnd );
2998 if (!wine_server_call_err( req ))
3000 tid = (DWORD)reply->tid;
3001 if (process) *process = (DWORD)reply->pid;
3004 SERVER_END_REQ;
3005 return tid;
3009 /*****************************************************************
3010 * GetParent (USER32.@)
3012 HWND WINAPI GetParent( HWND hwnd )
3014 WND *wndPtr;
3015 HWND retvalue = 0;
3017 if (!(wndPtr = WIN_GetPtr( hwnd )))
3019 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3020 return 0;
3022 if (wndPtr == WND_DESKTOP) return 0;
3023 if (wndPtr == WND_OTHER_PROCESS)
3025 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
3026 if (style & (WS_POPUP | WS_CHILD))
3028 SERVER_START_REQ( get_window_tree )
3030 req->handle = wine_server_user_handle( hwnd );
3031 if (!wine_server_call_err( req ))
3033 if (style & WS_POPUP) retvalue = wine_server_ptr_handle( reply->owner );
3034 else if (style & WS_CHILD) retvalue = wine_server_ptr_handle( reply->parent );
3037 SERVER_END_REQ;
3040 else
3042 if (wndPtr->dwStyle & WS_POPUP) retvalue = wndPtr->owner;
3043 else if (wndPtr->dwStyle & WS_CHILD) retvalue = wndPtr->parent;
3044 WIN_ReleasePtr( wndPtr );
3046 return retvalue;
3050 /*****************************************************************
3051 * GetAncestor (USER32.@)
3053 HWND WINAPI GetAncestor( HWND hwnd, UINT type )
3055 WND *win;
3056 HWND *list, ret = 0;
3058 switch(type)
3060 case GA_PARENT:
3061 if (!(win = WIN_GetPtr( hwnd )))
3063 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3064 return 0;
3066 if (win == WND_DESKTOP) return 0;
3067 if (win != WND_OTHER_PROCESS)
3069 ret = win->parent;
3070 WIN_ReleasePtr( win );
3072 else /* need to query the server */
3074 SERVER_START_REQ( get_window_tree )
3076 req->handle = wine_server_user_handle( hwnd );
3077 if (!wine_server_call_err( req )) ret = wine_server_ptr_handle( reply->parent );
3079 SERVER_END_REQ;
3081 break;
3083 case GA_ROOT:
3084 if (!(list = list_window_parents( hwnd ))) return 0;
3086 if (!list[0] || !list[1]) ret = WIN_GetFullHandle( hwnd ); /* top-level window */
3087 else
3089 int count = 2;
3090 while (list[count]) count++;
3091 ret = list[count - 2]; /* get the one before the desktop */
3093 HeapFree( GetProcessHeap(), 0, list );
3094 break;
3096 case GA_ROOTOWNER:
3097 if (is_desktop_window( hwnd )) return 0;
3098 ret = WIN_GetFullHandle( hwnd );
3099 for (;;)
3101 HWND parent = GetParent( ret );
3102 if (!parent) break;
3103 ret = parent;
3105 break;
3107 return ret;
3111 /*****************************************************************
3112 * SetParent (USER32.@)
3114 HWND WINAPI SetParent( HWND hwnd, HWND parent )
3116 WINDOWPOS winpos;
3117 HWND full_handle;
3118 HWND old_parent = 0;
3119 BOOL was_visible;
3120 WND *wndPtr;
3121 BOOL ret;
3122 RECT window_rect, old_screen_rect, new_screen_rect;
3124 TRACE("(%p %p)\n", hwnd, parent);
3126 if (is_broadcast(hwnd) || is_broadcast(parent))
3128 SetLastError(ERROR_INVALID_PARAMETER);
3129 return 0;
3132 if (!parent) parent = GetDesktopWindow();
3133 else if (parent == HWND_MESSAGE) parent = get_hwnd_message_parent();
3134 else parent = WIN_GetFullHandle( parent );
3136 if (!IsWindow( parent ))
3138 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3139 return 0;
3142 /* Some applications try to set a child as a parent */
3143 if (IsChild(hwnd, parent))
3145 SetLastError( ERROR_INVALID_PARAMETER );
3146 return 0;
3149 if (!(full_handle = WIN_IsCurrentThread( hwnd )))
3150 return (HWND)SendMessageW( hwnd, WM_WINE_SETPARENT, (WPARAM)parent, 0 );
3152 if (full_handle == parent)
3154 SetLastError( ERROR_INVALID_PARAMETER );
3155 return 0;
3158 /* Windows hides the window first, then shows it again
3159 * including the WM_SHOWWINDOW messages and all */
3160 was_visible = ShowWindow( hwnd, SW_HIDE );
3162 wndPtr = WIN_GetPtr( hwnd );
3163 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return 0;
3165 WIN_GetRectangles( hwnd, COORDS_PARENT, &window_rect, NULL );
3166 WIN_GetRectangles( hwnd, COORDS_SCREEN, &old_screen_rect, NULL );
3168 SERVER_START_REQ( set_parent )
3170 req->handle = wine_server_user_handle( hwnd );
3171 req->parent = wine_server_user_handle( parent );
3172 if ((ret = !wine_server_call( req )))
3174 old_parent = wine_server_ptr_handle( reply->old_parent );
3175 wndPtr->parent = parent = wine_server_ptr_handle( reply->full_parent );
3176 wndPtr->dpi = reply->dpi;
3177 wndPtr->dpi_awareness = reply->awareness;
3181 SERVER_END_REQ;
3182 WIN_ReleasePtr( wndPtr );
3183 if (!ret) return 0;
3185 USER_Driver->pSetParent( full_handle, parent, old_parent );
3187 winpos.hwnd = hwnd;
3188 winpos.hwndInsertAfter = HWND_TOP;
3189 winpos.x = window_rect.left;
3190 winpos.y = window_rect.top;
3191 winpos.cx = 0;
3192 winpos.cy = 0;
3193 winpos.flags = SWP_NOSIZE;
3195 WIN_GetRectangles( hwnd, COORDS_SCREEN, &new_screen_rect, NULL );
3196 USER_SetWindowPos( &winpos, new_screen_rect.left - old_screen_rect.left,
3197 new_screen_rect.top - old_screen_rect.top );
3199 if (was_visible) ShowWindow( hwnd, SW_SHOW );
3201 return old_parent;
3205 /*******************************************************************
3206 * IsChild (USER32.@)
3208 BOOL WINAPI IsChild( HWND parent, HWND child )
3210 HWND *list;
3211 int i;
3212 BOOL ret = FALSE;
3214 if (!(GetWindowLongW( child, GWL_STYLE ) & WS_CHILD)) return FALSE;
3215 if (!(list = list_window_parents( child ))) return FALSE;
3216 parent = WIN_GetFullHandle( parent );
3217 for (i = 0; list[i]; i++)
3219 if (list[i] == parent)
3221 ret = list[i] && list[i+1];
3222 break;
3224 if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_CHILD)) break;
3226 HeapFree( GetProcessHeap(), 0, list );
3227 return ret;
3231 /***********************************************************************
3232 * IsWindowVisible (USER32.@)
3234 BOOL WINAPI IsWindowVisible( HWND hwnd )
3236 HWND *list;
3237 BOOL retval = TRUE;
3238 int i;
3240 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)) return FALSE;
3241 if (!(list = list_window_parents( hwnd ))) return TRUE;
3242 if (list[0])
3244 for (i = 0; list[i+1]; i++)
3245 if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_VISIBLE)) break;
3246 retval = !list[i+1] && (list[i] == GetDesktopWindow()); /* top message window isn't visible */
3248 HeapFree( GetProcessHeap(), 0, list );
3249 return retval;
3253 /***********************************************************************
3254 * WIN_IsWindowDrawable
3256 * hwnd is drawable when it is visible, all parents are not
3257 * minimized, and it is itself not minimized unless we are
3258 * trying to draw its default class icon.
3260 BOOL WIN_IsWindowDrawable( HWND hwnd, BOOL icon )
3262 HWND *list;
3263 BOOL retval = TRUE;
3264 int i;
3265 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
3267 if (!(style & WS_VISIBLE)) return FALSE;
3268 if ((style & WS_MINIMIZE) && icon && GetClassLongPtrW( hwnd, GCLP_HICON )) return FALSE;
3270 if (!(list = list_window_parents( hwnd ))) return TRUE;
3271 if (list[0])
3273 for (i = 0; list[i+1]; i++)
3274 if ((GetWindowLongW( list[i], GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != WS_VISIBLE)
3275 break;
3276 retval = !list[i+1] && (list[i] == GetDesktopWindow()); /* top message window isn't visible */
3278 HeapFree( GetProcessHeap(), 0, list );
3279 return retval;
3283 /*******************************************************************
3284 * GetTopWindow (USER32.@)
3286 HWND WINAPI GetTopWindow( HWND hwnd )
3288 if (!hwnd) hwnd = GetDesktopWindow();
3289 return GetWindow( hwnd, GW_CHILD );
3293 /*******************************************************************
3294 * GetWindow (USER32.@)
3296 HWND WINAPI GetWindow( HWND hwnd, UINT rel )
3298 HWND retval = 0;
3300 if (rel == GW_OWNER) /* this one may be available locally */
3302 WND *wndPtr = WIN_GetPtr( hwnd );
3303 if (!wndPtr)
3305 SetLastError( ERROR_INVALID_HANDLE );
3306 return 0;
3308 if (wndPtr == WND_DESKTOP) return 0;
3309 if (wndPtr != WND_OTHER_PROCESS)
3311 retval = wndPtr->owner;
3312 WIN_ReleasePtr( wndPtr );
3313 return retval;
3315 /* else fall through to server call */
3318 SERVER_START_REQ( get_window_tree )
3320 req->handle = wine_server_user_handle( hwnd );
3321 if (!wine_server_call_err( req ))
3323 switch(rel)
3325 case GW_HWNDFIRST:
3326 retval = wine_server_ptr_handle( reply->first_sibling );
3327 break;
3328 case GW_HWNDLAST:
3329 retval = wine_server_ptr_handle( reply->last_sibling );
3330 break;
3331 case GW_HWNDNEXT:
3332 retval = wine_server_ptr_handle( reply->next_sibling );
3333 break;
3334 case GW_HWNDPREV:
3335 retval = wine_server_ptr_handle( reply->prev_sibling );
3336 break;
3337 case GW_OWNER:
3338 retval = wine_server_ptr_handle( reply->owner );
3339 break;
3340 case GW_CHILD:
3341 retval = wine_server_ptr_handle( reply->first_child );
3342 break;
3346 SERVER_END_REQ;
3347 return retval;
3351 /*******************************************************************
3352 * ShowOwnedPopups (USER32.@)
3354 BOOL WINAPI ShowOwnedPopups( HWND owner, BOOL fShow )
3356 int count = 0;
3357 HWND *win_array = WIN_ListChildren( GetDesktopWindow() );
3359 if (!win_array) return TRUE;
3361 while (win_array[count]) count++;
3362 while (--count >= 0)
3364 if (GetWindow( win_array[count], GW_OWNER ) != owner) continue;
3365 if (fShow)
3367 if (win_get_flags( win_array[count] ) & WIN_NEEDS_SHOW_OWNEDPOPUP)
3368 /* In Windows, ShowOwnedPopups(TRUE) generates
3369 * WM_SHOWWINDOW messages with SW_PARENTOPENING,
3370 * regardless of the state of the owner
3372 SendMessageW(win_array[count], WM_SHOWWINDOW, SW_SHOWNORMAL, SW_PARENTOPENING);
3374 else
3376 if (GetWindowLongW( win_array[count], GWL_STYLE ) & WS_VISIBLE)
3377 /* In Windows, ShowOwnedPopups(FALSE) generates
3378 * WM_SHOWWINDOW messages with SW_PARENTCLOSING,
3379 * regardless of the state of the owner
3381 SendMessageW(win_array[count], WM_SHOWWINDOW, SW_HIDE, SW_PARENTCLOSING);
3384 HeapFree( GetProcessHeap(), 0, win_array );
3385 return TRUE;
3389 /*******************************************************************
3390 * GetLastActivePopup (USER32.@)
3392 HWND WINAPI GetLastActivePopup( HWND hwnd )
3394 HWND retval = hwnd;
3396 SERVER_START_REQ( get_window_info )
3398 req->handle = wine_server_user_handle( hwnd );
3399 if (!wine_server_call_err( req )) retval = wine_server_ptr_handle( reply->last_active );
3401 SERVER_END_REQ;
3402 return retval;
3406 /*******************************************************************
3407 * WIN_ListChildren
3409 * Build an array of the children of a given window. The array must be
3410 * freed with HeapFree. Returns NULL when no windows are found.
3412 HWND *WIN_ListChildren( HWND hwnd )
3414 if (!hwnd)
3416 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3417 return NULL;
3419 return list_window_children( 0, hwnd, NULL, 0 );
3423 /*******************************************************************
3424 * EnumWindows (USER32.@)
3426 BOOL WINAPI EnumWindows( WNDENUMPROC lpEnumFunc, LPARAM lParam )
3428 HWND *list;
3429 BOOL ret = TRUE;
3430 int i;
3432 USER_CheckNotLock();
3434 /* We have to build a list of all windows first, to avoid */
3435 /* unpleasant side-effects, for instance if the callback */
3436 /* function changes the Z-order of the windows. */
3438 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return TRUE;
3440 /* Now call the callback function for every window */
3442 for (i = 0; list[i]; i++)
3444 /* Make sure that the window still exists */
3445 if (!IsWindow( list[i] )) continue;
3446 if (!(ret = lpEnumFunc( list[i], lParam ))) break;
3448 HeapFree( GetProcessHeap(), 0, list );
3449 return ret;
3453 /**********************************************************************
3454 * EnumThreadWindows (USER32.@)
3456 BOOL WINAPI EnumThreadWindows( DWORD id, WNDENUMPROC func, LPARAM lParam )
3458 HWND *list;
3459 int i;
3460 BOOL ret = TRUE;
3462 USER_CheckNotLock();
3464 if (!(list = list_window_children( 0, GetDesktopWindow(), NULL, id ))) return TRUE;
3466 /* Now call the callback function for every window */
3468 for (i = 0; list[i]; i++)
3469 if (!(ret = func( list[i], lParam ))) break;
3470 HeapFree( GetProcessHeap(), 0, list );
3471 return ret;
3475 /***********************************************************************
3476 * EnumDesktopWindows (USER32.@)
3478 BOOL WINAPI EnumDesktopWindows( HDESK desktop, WNDENUMPROC func, LPARAM lparam )
3480 HWND *list;
3481 int i;
3483 USER_CheckNotLock();
3485 if (!(list = list_window_children( desktop, 0, NULL, 0 ))) return TRUE;
3487 for (i = 0; list[i]; i++)
3488 if (!func( list[i], lparam )) break;
3489 HeapFree( GetProcessHeap(), 0, list );
3490 return TRUE;
3494 #ifdef __i386__
3495 /* Some apps pass a non-stdcall proc to EnumChildWindows,
3496 * so we need a small assembly wrapper to call the proc.
3498 extern LRESULT enum_callback_wrapper( WNDENUMPROC proc, HWND hwnd, LPARAM lparam );
3499 __ASM_GLOBAL_FUNC( enum_callback_wrapper,
3500 "pushl %ebp\n\t"
3501 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
3502 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
3503 "movl %esp,%ebp\n\t"
3504 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
3505 "pushl 16(%ebp)\n\t"
3506 "pushl 12(%ebp)\n\t"
3507 "call *8(%ebp)\n\t"
3508 "leave\n\t"
3509 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
3510 __ASM_CFI(".cfi_same_value %ebp\n\t")
3511 "ret" )
3512 #else
3513 static inline LRESULT enum_callback_wrapper( WNDENUMPROC proc, HWND hwnd, LPARAM lparam )
3515 return proc( hwnd, lparam );
3517 #endif /* __i386__ */
3519 /**********************************************************************
3520 * WIN_EnumChildWindows
3522 * Helper function for EnumChildWindows().
3524 static BOOL WIN_EnumChildWindows( HWND *list, WNDENUMPROC func, LPARAM lParam )
3526 HWND *childList;
3527 BOOL ret = FALSE;
3529 for ( ; *list; list++)
3531 /* Make sure that the window still exists */
3532 if (!IsWindow( *list )) continue;
3533 /* Build children list first */
3534 childList = WIN_ListChildren( *list );
3536 ret = enum_callback_wrapper( func, *list, lParam );
3538 if (childList)
3540 if (ret) ret = WIN_EnumChildWindows( childList, func, lParam );
3541 HeapFree( GetProcessHeap(), 0, childList );
3543 if (!ret) return FALSE;
3545 return TRUE;
3549 /**********************************************************************
3550 * EnumChildWindows (USER32.@)
3552 BOOL WINAPI EnumChildWindows( HWND parent, WNDENUMPROC func, LPARAM lParam )
3554 HWND *list;
3555 BOOL ret;
3557 USER_CheckNotLock();
3559 if (!(list = WIN_ListChildren( parent ))) return FALSE;
3560 ret = WIN_EnumChildWindows( list, func, lParam );
3561 HeapFree( GetProcessHeap(), 0, list );
3562 return ret;
3566 /*******************************************************************
3567 * AnyPopup (USER32.@)
3569 BOOL WINAPI AnyPopup(void)
3571 int i;
3572 BOOL retvalue;
3573 HWND *list = WIN_ListChildren( GetDesktopWindow() );
3575 if (!list) return FALSE;
3576 for (i = 0; list[i]; i++)
3578 if (IsWindowVisible( list[i] ) && GetWindow( list[i], GW_OWNER )) break;
3580 retvalue = (list[i] != 0);
3581 HeapFree( GetProcessHeap(), 0, list );
3582 return retvalue;
3586 /*******************************************************************
3587 * FlashWindow (USER32.@)
3589 BOOL WINAPI FlashWindow( HWND hWnd, BOOL bInvert )
3591 FLASHWINFO finfo;
3593 finfo.cbSize = sizeof(FLASHWINFO);
3594 finfo.dwFlags = bInvert ? FLASHW_ALL : FLASHW_STOP;
3595 finfo.uCount = 1;
3596 finfo.dwTimeout = 0;
3597 finfo.hwnd = hWnd;
3598 return FlashWindowEx( &finfo );
3601 /*******************************************************************
3602 * FlashWindowEx (USER32.@)
3604 BOOL WINAPI FlashWindowEx( PFLASHWINFO pfinfo )
3606 WND *wndPtr;
3608 TRACE( "%p\n", pfinfo );
3610 if (!pfinfo)
3612 SetLastError( ERROR_NOACCESS );
3613 return FALSE;
3616 if (!pfinfo->hwnd || pfinfo->cbSize != sizeof(FLASHWINFO) || !IsWindow( pfinfo->hwnd ))
3618 SetLastError( ERROR_INVALID_PARAMETER );
3619 return FALSE;
3621 FIXME( "%p - semi-stub\n", pfinfo );
3623 if (IsIconic( pfinfo->hwnd ))
3625 RedrawWindow( pfinfo->hwnd, 0, 0, RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW | RDW_FRAME );
3627 wndPtr = WIN_GetPtr( pfinfo->hwnd );
3628 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
3629 if (pfinfo->dwFlags && !(wndPtr->flags & WIN_NCACTIVATED))
3631 wndPtr->flags |= WIN_NCACTIVATED;
3633 else
3635 wndPtr->flags &= ~WIN_NCACTIVATED;
3637 WIN_ReleasePtr( wndPtr );
3638 USER_Driver->pFlashWindowEx( pfinfo );
3639 return TRUE;
3641 else
3643 WPARAM wparam;
3644 HWND hwnd = pfinfo->hwnd;
3646 wndPtr = WIN_GetPtr( hwnd );
3647 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
3648 hwnd = wndPtr->obj.handle; /* make it a full handle */
3650 if (pfinfo->dwFlags) wparam = !(wndPtr->flags & WIN_NCACTIVATED);
3651 else wparam = (hwnd == GetForegroundWindow());
3653 WIN_ReleasePtr( wndPtr );
3654 SendMessageW( hwnd, WM_NCACTIVATE, wparam, 0 );
3655 USER_Driver->pFlashWindowEx( pfinfo );
3656 return wparam;
3660 /*******************************************************************
3661 * GetWindowContextHelpId (USER32.@)
3663 DWORD WINAPI GetWindowContextHelpId( HWND hwnd )
3665 DWORD retval;
3666 WND *wnd = WIN_GetPtr( hwnd );
3667 if (!wnd || wnd == WND_DESKTOP) return 0;
3668 if (wnd == WND_OTHER_PROCESS)
3670 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
3671 return 0;
3673 retval = wnd->helpContext;
3674 WIN_ReleasePtr( wnd );
3675 return retval;
3679 /*******************************************************************
3680 * SetWindowContextHelpId (USER32.@)
3682 BOOL WINAPI SetWindowContextHelpId( HWND hwnd, DWORD id )
3684 WND *wnd = WIN_GetPtr( hwnd );
3685 if (!wnd || wnd == WND_DESKTOP) return FALSE;
3686 if (wnd == WND_OTHER_PROCESS)
3688 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
3689 return FALSE;
3691 wnd->helpContext = id;
3692 WIN_ReleasePtr( wnd );
3693 return TRUE;
3697 /*******************************************************************
3698 * DragDetect (USER32.@)
3700 BOOL WINAPI DragDetect( HWND hWnd, POINT pt )
3702 MSG msg;
3703 RECT rect;
3704 WORD wDragWidth = GetSystemMetrics(SM_CXDRAG);
3705 WORD wDragHeight= GetSystemMetrics(SM_CYDRAG);
3707 SetRect(&rect, pt.x - wDragWidth, pt.y - wDragHeight, pt.x + wDragWidth, pt.y + wDragHeight);
3709 SetCapture(hWnd);
3711 while(1)
3713 while (PeekMessageW( &msg, 0, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE ))
3715 if( msg.message == WM_LBUTTONUP )
3717 ReleaseCapture();
3718 return FALSE;
3720 if( msg.message == WM_MOUSEMOVE )
3722 POINT tmp;
3723 tmp.x = (short)LOWORD(msg.lParam);
3724 tmp.y = (short)HIWORD(msg.lParam);
3725 if( !PtInRect( &rect, tmp ))
3727 ReleaseCapture();
3728 return TRUE;
3732 WaitMessage();
3734 return FALSE;
3737 /******************************************************************************
3738 * GetWindowModuleFileNameA (USER32.@)
3740 UINT WINAPI GetWindowModuleFileNameA( HWND hwnd, LPSTR module, UINT size )
3742 WND *win;
3743 HINSTANCE hinst;
3745 TRACE( "%p, %p, %u\n", hwnd, module, size );
3747 win = WIN_GetPtr( hwnd );
3748 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP)
3750 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3751 return 0;
3753 hinst = win->hInstance;
3754 WIN_ReleasePtr( win );
3756 return GetModuleFileNameA( hinst, module, size );
3759 /******************************************************************************
3760 * GetWindowModuleFileNameW (USER32.@)
3762 UINT WINAPI GetWindowModuleFileNameW( HWND hwnd, LPWSTR module, UINT size )
3764 WND *win;
3765 HINSTANCE hinst;
3767 TRACE( "%p, %p, %u\n", hwnd, module, size );
3769 win = WIN_GetPtr( hwnd );
3770 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP)
3772 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3773 return 0;
3775 hinst = win->hInstance;
3776 WIN_ReleasePtr( win );
3778 return GetModuleFileNameW( hinst, module, size );
3781 /******************************************************************************
3782 * GetWindowInfo (USER32.@)
3784 * Note: tests show that Windows doesn't check cbSize of the structure.
3786 BOOL WINAPI DECLSPEC_HOTPATCH GetWindowInfo( HWND hwnd, PWINDOWINFO pwi)
3788 if (!pwi) return FALSE;
3789 if (!WIN_GetRectangles( hwnd, COORDS_SCREEN, &pwi->rcWindow, &pwi->rcClient )) return FALSE;
3791 pwi->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
3792 pwi->dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
3793 pwi->dwWindowStatus = ((GetActiveWindow() == hwnd) ? WS_ACTIVECAPTION : 0);
3795 pwi->cxWindowBorders = pwi->rcClient.left - pwi->rcWindow.left;
3796 pwi->cyWindowBorders = pwi->rcWindow.bottom - pwi->rcClient.bottom;
3798 pwi->atomWindowType = GetClassLongW( hwnd, GCW_ATOM );
3799 pwi->wCreatorVersion = 0x0400;
3801 return TRUE;
3804 /******************************************************************************
3805 * SwitchDesktop (USER32.@)
3807 * NOTES: Sets the current input or interactive desktop.
3809 BOOL WINAPI SwitchDesktop( HDESK hDesktop)
3811 FIXME("(hwnd %p) stub!\n", hDesktop);
3812 return TRUE;
3816 /***********************************************************************
3817 * __wine_set_pixel_format
3819 BOOL CDECL __wine_set_pixel_format( HWND hwnd, int format )
3821 WND *win = WIN_GetPtr( hwnd );
3823 if (!win || win == WND_DESKTOP || win == WND_OTHER_PROCESS)
3825 WARN( "setting format %d on win %p not supported\n", format, hwnd );
3826 return FALSE;
3828 win->pixel_format = format;
3829 WIN_ReleasePtr( win );
3831 update_window_state( hwnd );
3832 return TRUE;
3836 /*****************************************************************************
3837 * SetLayeredWindowAttributes (USER32.@)
3839 BOOL WINAPI SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
3841 BOOL ret;
3843 TRACE("(%p,%08x,%d,%x)\n", hwnd, key, alpha, flags);
3845 SERVER_START_REQ( set_window_layered_info )
3847 req->handle = wine_server_user_handle( hwnd );
3848 req->color_key = key;
3849 req->alpha = alpha;
3850 req->flags = flags;
3851 ret = !wine_server_call_err( req );
3853 SERVER_END_REQ;
3855 if (ret)
3857 USER_Driver->pSetLayeredWindowAttributes( hwnd, key, alpha, flags );
3858 update_window_state( hwnd );
3861 return ret;
3865 /*****************************************************************************
3866 * GetLayeredWindowAttributes (USER32.@)
3868 BOOL WINAPI GetLayeredWindowAttributes( HWND hwnd, COLORREF *key, BYTE *alpha, DWORD *flags )
3870 BOOL ret;
3872 SERVER_START_REQ( get_window_layered_info )
3874 req->handle = wine_server_user_handle( hwnd );
3875 if ((ret = !wine_server_call_err( req )))
3877 if (key) *key = reply->color_key;
3878 if (alpha) *alpha = reply->alpha;
3879 if (flags) *flags = reply->flags;
3882 SERVER_END_REQ;
3884 return ret;
3888 /*****************************************************************************
3889 * UpdateLayeredWindowIndirect (USER32.@)
3891 BOOL WINAPI UpdateLayeredWindowIndirect( HWND hwnd, const UPDATELAYEREDWINDOWINFO *info )
3893 DWORD flags = SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW;
3894 RECT window_rect, client_rect;
3895 SIZE offset;
3897 if (!info ||
3898 info->cbSize != sizeof(*info) ||
3899 info->dwFlags & ~(ULW_COLORKEY | ULW_ALPHA | ULW_OPAQUE | ULW_EX_NORESIZE) ||
3900 !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED) ||
3901 GetLayeredWindowAttributes( hwnd, NULL, NULL, NULL ))
3903 SetLastError( ERROR_INVALID_PARAMETER );
3904 return FALSE;
3907 WIN_GetRectangles( hwnd, COORDS_PARENT, &window_rect, &client_rect );
3909 if (info->pptDst)
3911 offset.cx = info->pptDst->x - window_rect.left;
3912 offset.cy = info->pptDst->y - window_rect.top;
3913 OffsetRect( &client_rect, offset.cx, offset.cy );
3914 OffsetRect( &window_rect, offset.cx, offset.cy );
3915 flags &= ~SWP_NOMOVE;
3917 if (info->psize)
3919 offset.cx = info->psize->cx - (window_rect.right - window_rect.left);
3920 offset.cy = info->psize->cy - (window_rect.bottom - window_rect.top);
3921 if (info->psize->cx <= 0 || info->psize->cy <= 0)
3923 SetLastError( ERROR_INVALID_PARAMETER );
3924 return FALSE;
3926 if ((info->dwFlags & ULW_EX_NORESIZE) && (offset.cx || offset.cy))
3928 SetLastError( ERROR_INCORRECT_SIZE );
3929 return FALSE;
3931 client_rect.right += offset.cx;
3932 client_rect.bottom += offset.cy;
3933 window_rect.right += offset.cx;
3934 window_rect.bottom += offset.cy;
3935 flags &= ~SWP_NOSIZE;
3938 TRACE( "window %p win %s client %s\n", hwnd,
3939 wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(&client_rect) );
3941 if (!USER_Driver->pUpdateLayeredWindow( hwnd, info, &window_rect )) return FALSE;
3943 set_window_pos( hwnd, 0, flags, &window_rect, &client_rect, NULL );
3944 return TRUE;
3948 /*****************************************************************************
3949 * UpdateLayeredWindow (USER32.@)
3951 BOOL WINAPI UpdateLayeredWindow( HWND hwnd, HDC hdcDst, POINT *pptDst, SIZE *psize,
3952 HDC hdcSrc, POINT *pptSrc, COLORREF crKey, BLENDFUNCTION *pblend,
3953 DWORD flags)
3955 UPDATELAYEREDWINDOWINFO info;
3957 if (flags & ULW_EX_NORESIZE) /* only valid for UpdateLayeredWindowIndirect */
3959 SetLastError( ERROR_INVALID_PARAMETER );
3960 return FALSE;
3962 info.cbSize = sizeof(info);
3963 info.hdcDst = hdcDst;
3964 info.pptDst = pptDst;
3965 info.psize = psize;
3966 info.hdcSrc = hdcSrc;
3967 info.pptSrc = pptSrc;
3968 info.crKey = crKey;
3969 info.pblend = pblend;
3970 info.dwFlags = flags;
3971 info.prcDirty = NULL;
3972 return UpdateLayeredWindowIndirect( hwnd, &info );
3976 /******************************************************************************
3977 * GetProcessDefaultLayout [USER32.@]
3979 * Gets the default layout for parentless windows.
3981 BOOL WINAPI GetProcessDefaultLayout( DWORD *layout )
3983 if (!layout)
3985 SetLastError( ERROR_NOACCESS );
3986 return FALSE;
3988 if (process_layout == ~0u)
3990 static const WCHAR translationW[] = { '\\','V','a','r','F','i','l','e','I','n','f','o',
3991 '\\','T','r','a','n','s','l','a','t','i','o','n', 0 };
3992 static const WCHAR filedescW[] = { '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
3993 '\\','%','0','4','x','%','0','4','x',
3994 '\\','F','i','l','e','D','e','s','c','r','i','p','t','i','o','n',0 };
3995 WCHAR *str, buffer[MAX_PATH];
3996 DWORD i, len, version_layout = 0;
3997 DWORD user_lang = GetUserDefaultLangID();
3998 DWORD *languages;
3999 void *data = NULL;
4001 GetModuleFileNameW( 0, buffer, MAX_PATH );
4002 if (!(len = GetFileVersionInfoSizeW( buffer, NULL ))) goto done;
4003 if (!(data = HeapAlloc( GetProcessHeap(), 0, len ))) goto done;
4004 if (!GetFileVersionInfoW( buffer, 0, len, data )) goto done;
4005 if (!VerQueryValueW( data, translationW, (void **)&languages, &len ) || !len) goto done;
4007 len /= sizeof(DWORD);
4008 for (i = 0; i < len; i++) if (LOWORD(languages[i]) == user_lang) break;
4009 if (i == len) /* try neutral language */
4010 for (i = 0; i < len; i++)
4011 if (LOWORD(languages[i]) == MAKELANGID( PRIMARYLANGID(user_lang), SUBLANG_NEUTRAL )) break;
4012 if (i == len) i = 0; /* default to the first one */
4014 sprintfW( buffer, filedescW, LOWORD(languages[i]), HIWORD(languages[i]) );
4015 if (!VerQueryValueW( data, buffer, (void **)&str, &len )) goto done;
4016 TRACE( "found description %s\n", debugstr_w( str ));
4017 if (str[0] == 0x200e && str[1] == 0x200e) version_layout = LAYOUT_RTL;
4019 done:
4020 HeapFree( GetProcessHeap(), 0, data );
4021 process_layout = version_layout;
4023 *layout = process_layout;
4024 return TRUE;
4028 /******************************************************************************
4029 * SetProcessDefaultLayout [USER32.@]
4031 * Sets the default layout for parentless windows.
4033 BOOL WINAPI SetProcessDefaultLayout( DWORD layout )
4035 process_layout = layout;
4036 return TRUE;
4040 /* 64bit versions */
4042 #ifdef GetWindowLongPtrW
4043 #undef GetWindowLongPtrW
4044 #endif
4046 #ifdef GetWindowLongPtrA
4047 #undef GetWindowLongPtrA
4048 #endif
4050 #ifdef SetWindowLongPtrW
4051 #undef SetWindowLongPtrW
4052 #endif
4054 #ifdef SetWindowLongPtrA
4055 #undef SetWindowLongPtrA
4056 #endif
4058 /*****************************************************************************
4059 * GetWindowLongPtrW (USER32.@)
4061 LONG_PTR WINAPI GetWindowLongPtrW( HWND hwnd, INT offset )
4063 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG_PTR), TRUE );
4066 /*****************************************************************************
4067 * GetWindowLongPtrA (USER32.@)
4069 LONG_PTR WINAPI GetWindowLongPtrA( HWND hwnd, INT offset )
4071 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG_PTR), FALSE );
4074 /*****************************************************************************
4075 * SetWindowLongPtrW (USER32.@)
4077 LONG_PTR WINAPI SetWindowLongPtrW( HWND hwnd, INT offset, LONG_PTR newval )
4079 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG_PTR), newval, TRUE );
4082 /*****************************************************************************
4083 * SetWindowLongPtrA (USER32.@)
4085 LONG_PTR WINAPI SetWindowLongPtrA( HWND hwnd, INT offset, LONG_PTR newval )
4087 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG_PTR), newval, FALSE );
4090 /*****************************************************************************
4091 * RegisterTouchWindow (USER32.@)
4093 BOOL WINAPI RegisterTouchWindow(HWND hwnd, ULONG flags)
4095 FIXME("(%p %08x): stub\n", hwnd, flags);
4096 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
4097 return FALSE;
4100 /*****************************************************************************
4101 * UnregisterTouchWindow (USER32.@)
4103 BOOL WINAPI UnregisterTouchWindow(HWND hwnd)
4105 FIXME("(%p): stub\n", hwnd);
4106 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
4107 return FALSE;
4110 /*****************************************************************************
4111 * CloseTouchInputHandle (USER32.@)
4113 BOOL WINAPI CloseTouchInputHandle(HTOUCHINPUT handle)
4115 FIXME("(%p): stub\n", handle);
4116 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
4117 return FALSE;
4120 /*****************************************************************************
4121 * GetTouchInputInfo (USER32.@)
4123 BOOL WINAPI GetTouchInputInfo(HTOUCHINPUT handle, UINT count, TOUCHINPUT *ptr, int size)
4125 FIXME("(%p %u %p %u): stub\n", handle, count, ptr, size);
4126 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
4127 return FALSE;
4130 /*****************************************************************************
4131 * GetGestureInfo (USER32.@)
4133 BOOL WINAPI GetGestureInfo(HGESTUREINFO handle, PGESTUREINFO ptr)
4135 FIXME("(%p %p): stub\n", handle, ptr);
4136 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
4137 return FALSE;
4140 /*****************************************************************************
4141 * GetWindowDisplayAffinity (USER32.@)
4143 BOOL WINAPI GetWindowDisplayAffinity(HWND hwnd, DWORD *affinity)
4145 FIXME("(%p, %p): stub\n", hwnd, affinity);
4147 if (!hwnd || !affinity)
4149 SetLastError(hwnd ? ERROR_NOACCESS : ERROR_INVALID_WINDOW_HANDLE);
4150 return FALSE;
4153 *affinity = WDA_NONE;
4154 return TRUE;
4157 /*****************************************************************************
4158 * SetWindowDisplayAffinity (USER32.@)
4160 BOOL WINAPI SetWindowDisplayAffinity(HWND hwnd, DWORD affinity)
4162 FIXME("(%p, %u): stub\n", hwnd, affinity);
4164 if (!hwnd)
4166 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
4167 return FALSE;
4170 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
4171 return FALSE;