user32: Also ignore dynamic DPI awareness changes when DPI scaling is disabled.
[wine.git] / dlls / user32 / win.c
blobdd6142d16c03a654cad0996c0632c25f8bbad75d
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 DPI_AWARENESS_CONTEXT context;
450 RECT window_rect, client_rect, valid_rects[2];
452 context = SetThreadDpiAwarenessContext( GetWindowDpiAwarenessContext( hwnd ));
453 WIN_GetRectangles( hwnd, COORDS_PARENT, &window_rect, &client_rect );
454 valid_rects[0] = valid_rects[1] = client_rect;
455 set_window_pos( hwnd, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE |
456 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW,
457 &window_rect, &client_rect, valid_rects );
458 SetThreadDpiAwarenessContext( context );
462 /*******************************************************************
463 * get_server_window_text
465 * Retrieve the window text from the server.
467 static data_size_t get_server_window_text( HWND hwnd, WCHAR *text, data_size_t count )
469 data_size_t len = 0, needed = 0;
471 SERVER_START_REQ( get_window_text )
473 req->handle = wine_server_user_handle( hwnd );
474 if (count) wine_server_set_reply( req, text, (count - 1) * sizeof(WCHAR) );
475 if (!wine_server_call_err( req ))
477 needed = reply->length;
478 len = wine_server_reply_size(reply);
481 SERVER_END_REQ;
482 if (text) text[len / sizeof(WCHAR)] = 0;
483 return needed;
487 /*******************************************************************
488 * get_hwnd_message_parent
490 * Return the parent for HWND_MESSAGE windows.
492 HWND get_hwnd_message_parent(void)
494 struct user_thread_info *thread_info = get_user_thread_info();
496 if (!thread_info->msg_window) GetDesktopWindow(); /* trigger creation */
497 return thread_info->msg_window;
501 /*******************************************************************
502 * is_desktop_window
504 * Check if window is the desktop or the HWND_MESSAGE top parent.
506 BOOL is_desktop_window( HWND hwnd )
508 struct user_thread_info *thread_info = get_user_thread_info();
510 if (!hwnd) return FALSE;
511 if (hwnd == thread_info->top_window) return TRUE;
512 if (hwnd == thread_info->msg_window) return TRUE;
514 if (!HIWORD(hwnd) || HIWORD(hwnd) == 0xffff)
516 if (LOWORD(thread_info->top_window) == LOWORD(hwnd)) return TRUE;
517 if (LOWORD(thread_info->msg_window) == LOWORD(hwnd)) return TRUE;
519 return FALSE;
523 /*******************************************************************
524 * Dummy window surface for windows that shouldn't get painted.
527 static void dummy_surface_lock( struct window_surface *window_surface )
529 /* nothing to do */
532 static void dummy_surface_unlock( struct window_surface *window_surface )
534 /* nothing to do */
537 static void *dummy_surface_get_bitmap_info( struct window_surface *window_surface, BITMAPINFO *info )
539 static DWORD dummy_data;
541 info->bmiHeader.biSize = sizeof( info->bmiHeader );
542 info->bmiHeader.biWidth = dummy_surface.rect.right;
543 info->bmiHeader.biHeight = dummy_surface.rect.bottom;
544 info->bmiHeader.biPlanes = 1;
545 info->bmiHeader.biBitCount = 32;
546 info->bmiHeader.biCompression = BI_RGB;
547 info->bmiHeader.biSizeImage = 0;
548 info->bmiHeader.biXPelsPerMeter = 0;
549 info->bmiHeader.biYPelsPerMeter = 0;
550 info->bmiHeader.biClrUsed = 0;
551 info->bmiHeader.biClrImportant = 0;
552 return &dummy_data;
555 static RECT *dummy_surface_get_bounds( struct window_surface *window_surface )
557 static RECT dummy_bounds;
558 return &dummy_bounds;
561 static void dummy_surface_set_region( struct window_surface *window_surface, HRGN region )
563 /* nothing to do */
566 static void dummy_surface_flush( struct window_surface *window_surface )
568 /* nothing to do */
571 static void dummy_surface_destroy( struct window_surface *window_surface )
573 /* nothing to do */
576 static const struct window_surface_funcs dummy_surface_funcs =
578 dummy_surface_lock,
579 dummy_surface_unlock,
580 dummy_surface_get_bitmap_info,
581 dummy_surface_get_bounds,
582 dummy_surface_set_region,
583 dummy_surface_flush,
584 dummy_surface_destroy
587 struct window_surface dummy_surface = { &dummy_surface_funcs, { NULL, NULL }, 1, { 0, 0, 1, 1 } };
590 /*******************************************************************
591 * register_window_surface
593 * Register a window surface in the global list, possibly replacing another one.
595 void register_window_surface( struct window_surface *old, struct window_surface *new )
597 if (old == new) return;
598 EnterCriticalSection( &surfaces_section );
599 if (old && old != &dummy_surface) list_remove( &old->entry );
600 if (new && new != &dummy_surface) list_add_tail( &window_surfaces, &new->entry );
601 LeaveCriticalSection( &surfaces_section );
605 /*******************************************************************
606 * flush_window_surfaces
608 * Flush pending output from all window surfaces.
610 void flush_window_surfaces( BOOL idle )
612 static DWORD last_idle;
613 DWORD now;
614 struct window_surface *surface;
616 EnterCriticalSection( &surfaces_section );
617 now = GetTickCount();
618 if (idle) last_idle = now;
619 /* if not idle, we only flush if there's evidence that the app never goes idle */
620 else if ((int)(now - last_idle) < 50) goto done;
622 LIST_FOR_EACH_ENTRY( surface, &window_surfaces, struct window_surface, entry )
623 surface->funcs->flush( surface );
624 done:
625 LeaveCriticalSection( &surfaces_section );
629 /***********************************************************************
630 * WIN_GetPtr
632 * Return a pointer to the WND structure if local to the process,
633 * or WND_OTHER_PROCESS if handle may be valid in other process.
634 * If ret value is a valid pointer, it must be released with WIN_ReleasePtr.
636 WND *WIN_GetPtr( HWND hwnd )
638 WND *ptr;
640 if ((ptr = get_user_handle_ptr( hwnd, USER_WINDOW )) == WND_OTHER_PROCESS)
642 if (is_desktop_window( hwnd )) ptr = WND_DESKTOP;
644 return ptr;
648 /***********************************************************************
649 * WIN_IsCurrentProcess
651 * Check whether a given window belongs to the current process (and return the full handle).
653 HWND WIN_IsCurrentProcess( HWND hwnd )
655 WND *ptr;
656 HWND ret;
658 if (!(ptr = WIN_GetPtr( hwnd )) || ptr == WND_OTHER_PROCESS || ptr == WND_DESKTOP) return 0;
659 ret = ptr->obj.handle;
660 WIN_ReleasePtr( ptr );
661 return ret;
665 /***********************************************************************
666 * WIN_IsCurrentThread
668 * Check whether a given window belongs to the current thread (and return the full handle).
670 HWND WIN_IsCurrentThread( HWND hwnd )
672 WND *ptr;
673 HWND ret = 0;
675 if (!(ptr = WIN_GetPtr( hwnd )) || ptr == WND_OTHER_PROCESS || ptr == WND_DESKTOP) return 0;
676 if (ptr->tid == GetCurrentThreadId()) ret = ptr->obj.handle;
677 WIN_ReleasePtr( ptr );
678 return ret;
682 /***********************************************************************
683 * win_set_flags
685 * Set the flags of a window and return the previous value.
687 UINT win_set_flags( HWND hwnd, UINT set_mask, UINT clear_mask )
689 UINT ret;
690 WND *ptr = WIN_GetPtr( hwnd );
692 if (!ptr || ptr == WND_OTHER_PROCESS || ptr == WND_DESKTOP) return 0;
693 ret = ptr->flags;
694 ptr->flags = (ret & ~clear_mask) | set_mask;
695 WIN_ReleasePtr( ptr );
696 return ret;
700 /***********************************************************************
701 * WIN_GetFullHandle
703 * Convert a possibly truncated window handle to a full 32-bit handle.
705 HWND WIN_GetFullHandle( HWND hwnd )
707 WND *ptr;
709 if (!hwnd || (ULONG_PTR)hwnd >> 16) return hwnd;
710 if (LOWORD(hwnd) <= 1 || LOWORD(hwnd) == 0xffff) return hwnd;
711 /* do sign extension for -2 and -3 */
712 if (LOWORD(hwnd) >= (WORD)-3) return (HWND)(LONG_PTR)(INT16)LOWORD(hwnd);
714 if (!(ptr = WIN_GetPtr( hwnd ))) return hwnd;
716 if (ptr == WND_DESKTOP)
718 if (LOWORD(hwnd) == LOWORD(GetDesktopWindow())) return GetDesktopWindow();
719 else return get_hwnd_message_parent();
722 if (ptr != WND_OTHER_PROCESS)
724 hwnd = ptr->obj.handle;
725 WIN_ReleasePtr( ptr );
727 else /* may belong to another process */
729 SERVER_START_REQ( get_window_info )
731 req->handle = wine_server_user_handle( hwnd );
732 if (!wine_server_call_err( req )) hwnd = wine_server_ptr_handle( reply->full_handle );
734 SERVER_END_REQ;
736 return hwnd;
740 /***********************************************************************
741 * WIN_SetOwner
743 * Change the owner of a window.
745 HWND WIN_SetOwner( HWND hwnd, HWND owner )
747 WND *win = WIN_GetPtr( hwnd );
748 HWND ret = 0;
750 if (!win || win == WND_DESKTOP) return 0;
751 if (win == WND_OTHER_PROCESS)
753 if (IsWindow(hwnd)) ERR( "cannot set owner %p on other process window %p\n", owner, hwnd );
754 return 0;
756 SERVER_START_REQ( set_window_owner )
758 req->handle = wine_server_user_handle( hwnd );
759 req->owner = wine_server_user_handle( owner );
760 if (!wine_server_call( req ))
762 win->owner = wine_server_ptr_handle( reply->full_owner );
763 ret = wine_server_ptr_handle( reply->prev_owner );
766 SERVER_END_REQ;
767 WIN_ReleasePtr( win );
768 return ret;
772 /***********************************************************************
773 * WIN_SetStyle
775 * Change the style of a window.
777 ULONG WIN_SetStyle( HWND hwnd, ULONG set_bits, ULONG clear_bits )
779 BOOL ok, made_visible = FALSE;
780 STYLESTRUCT style;
781 WND *win = WIN_GetPtr( hwnd );
783 if (!win || win == WND_DESKTOP) return 0;
784 if (win == WND_OTHER_PROCESS)
786 if (IsWindow(hwnd))
787 return SendMessageW(hwnd, WM_WINE_SETSTYLE, set_bits, clear_bits);
788 return 0;
790 style.styleOld = win->dwStyle;
791 style.styleNew = (win->dwStyle | set_bits) & ~clear_bits;
792 if (style.styleNew == style.styleOld)
794 WIN_ReleasePtr( win );
795 return style.styleNew;
797 SERVER_START_REQ( set_window_info )
799 req->handle = wine_server_user_handle( hwnd );
800 req->flags = SET_WIN_STYLE;
801 req->style = style.styleNew;
802 req->extra_offset = -1;
803 if ((ok = !wine_server_call( req )))
805 style.styleOld = reply->old_style;
806 win->dwStyle = style.styleNew;
809 SERVER_END_REQ;
811 if (ok && ((style.styleOld ^ style.styleNew) & WS_VISIBLE))
813 made_visible = (style.styleNew & WS_VISIBLE) != 0;
814 invalidate_dce( win, NULL );
816 WIN_ReleasePtr( win );
818 if (!ok) return 0;
820 USER_Driver->pSetWindowStyle( hwnd, GWL_STYLE, &style );
821 if (made_visible) update_window_state( hwnd );
823 return style.styleOld;
827 /***********************************************************************
828 * WIN_GetRectangles
830 * Get the window and client rectangles.
832 BOOL WIN_GetRectangles( HWND hwnd, enum coords_relative relative, RECT *rectWindow, RECT *rectClient )
834 WND *win = WIN_GetPtr( hwnd );
835 BOOL ret = TRUE;
837 if (!win)
839 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
840 return FALSE;
842 if (win == WND_DESKTOP)
844 RECT rect;
845 rect.left = rect.top = 0;
846 if (hwnd == get_hwnd_message_parent())
848 rect.right = 100;
849 rect.bottom = 100;
850 rect = rect_win_to_thread_dpi( hwnd, rect );
852 else
854 rect.right = GetSystemMetrics(SM_CXSCREEN);
855 rect.bottom = GetSystemMetrics(SM_CYSCREEN);
857 if (rectWindow) *rectWindow = rect;
858 if (rectClient) *rectClient = rect;
859 return TRUE;
861 if (win != WND_OTHER_PROCESS)
863 RECT window_rect = win->window_rect, client_rect = win->client_rect;
865 switch (relative)
867 case COORDS_CLIENT:
868 OffsetRect( &window_rect, -win->client_rect.left, -win->client_rect.top );
869 OffsetRect( &client_rect, -win->client_rect.left, -win->client_rect.top );
870 if (win->dwExStyle & WS_EX_LAYOUTRTL)
871 mirror_rect( &win->client_rect, &window_rect );
872 break;
873 case COORDS_WINDOW:
874 OffsetRect( &window_rect, -win->window_rect.left, -win->window_rect.top );
875 OffsetRect( &client_rect, -win->window_rect.left, -win->window_rect.top );
876 if (win->dwExStyle & WS_EX_LAYOUTRTL)
877 mirror_rect( &win->window_rect, &client_rect );
878 break;
879 case COORDS_PARENT:
880 if (win->parent)
882 WND *parent = WIN_GetPtr( win->parent );
883 if (parent == WND_DESKTOP) break;
884 if (!parent || parent == WND_OTHER_PROCESS)
886 WIN_ReleasePtr( win );
887 goto other_process;
889 if (parent->flags & WIN_CHILDREN_MOVED)
891 WIN_ReleasePtr( parent );
892 WIN_ReleasePtr( win );
893 goto other_process;
895 if (parent->dwExStyle & WS_EX_LAYOUTRTL)
897 mirror_rect( &parent->client_rect, &window_rect );
898 mirror_rect( &parent->client_rect, &client_rect );
900 WIN_ReleasePtr( parent );
902 break;
903 case COORDS_SCREEN:
904 while (win->parent)
906 WND *parent = WIN_GetPtr( win->parent );
907 if (parent == WND_DESKTOP) break;
908 if (!parent || parent == WND_OTHER_PROCESS)
910 WIN_ReleasePtr( win );
911 goto other_process;
913 WIN_ReleasePtr( win );
914 if (parent->flags & WIN_CHILDREN_MOVED)
916 WIN_ReleasePtr( parent );
917 goto other_process;
919 win = parent;
920 if (win->parent)
922 OffsetRect( &window_rect, win->client_rect.left, win->client_rect.top );
923 OffsetRect( &client_rect, win->client_rect.left, win->client_rect.top );
926 break;
928 if (rectWindow) *rectWindow = rect_win_to_thread_dpi( hwnd, window_rect );
929 if (rectClient) *rectClient = rect_win_to_thread_dpi( hwnd, client_rect );
930 WIN_ReleasePtr( win );
931 return TRUE;
934 other_process:
935 SERVER_START_REQ( get_window_rectangles )
937 req->handle = wine_server_user_handle( hwnd );
938 req->relative = relative;
939 req->dpi = get_thread_dpi();
940 if ((ret = !wine_server_call_err( req )))
942 if (rectWindow)
944 rectWindow->left = reply->window.left;
945 rectWindow->top = reply->window.top;
946 rectWindow->right = reply->window.right;
947 rectWindow->bottom = reply->window.bottom;
949 if (rectClient)
951 rectClient->left = reply->client.left;
952 rectClient->top = reply->client.top;
953 rectClient->right = reply->client.right;
954 rectClient->bottom = reply->client.bottom;
958 SERVER_END_REQ;
959 return ret;
963 /***********************************************************************
964 * WIN_DestroyWindow
966 * Destroy storage associated to a window. "Internals" p.358
968 LRESULT WIN_DestroyWindow( HWND hwnd )
970 WND *wndPtr;
971 HWND *list;
972 HMENU menu = 0, sys_menu;
973 HWND icon_title;
974 struct window_surface *surface;
976 TRACE("%p\n", hwnd );
978 /* destroy default IME window */
979 if (win_set_flags( hwnd, 0, WIN_HAS_IME_WIN ) & WIN_HAS_IME_WIN)
981 TRACE("unregister IME window for %p\n", hwnd);
982 imm_unregister_window( hwnd );
985 /* free child windows */
986 if ((list = WIN_ListChildren( hwnd )))
988 int i;
989 for (i = 0; list[i]; i++)
991 if (WIN_IsCurrentThread( list[i] )) WIN_DestroyWindow( list[i] );
992 else SendNotifyMessageW( list[i], WM_WINE_DESTROYWINDOW, 0, 0 );
994 HeapFree( GetProcessHeap(), 0, list );
997 /* Unlink now so we won't bother with the children later on */
998 SERVER_START_REQ( set_parent )
1000 req->handle = wine_server_user_handle( hwnd );
1001 req->parent = 0;
1002 wine_server_call( req );
1004 SERVER_END_REQ;
1007 * Send the WM_NCDESTROY to the window being destroyed.
1009 SendMessageW( hwnd, WM_NCDESTROY, 0, 0 );
1011 /* FIXME: do we need to fake QS_MOUSEMOVE wakebit? */
1013 /* free resources associated with the window */
1015 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
1016 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
1017 menu = (HMENU)wndPtr->wIDmenu;
1018 sys_menu = wndPtr->hSysMenu;
1019 free_dce( wndPtr->dce, hwnd );
1020 wndPtr->dce = NULL;
1021 icon_title = wndPtr->icon_title;
1022 HeapFree( GetProcessHeap(), 0, wndPtr->text );
1023 wndPtr->text = NULL;
1024 HeapFree( GetProcessHeap(), 0, wndPtr->pScroll );
1025 wndPtr->pScroll = NULL;
1026 DestroyIcon( wndPtr->hIconSmall2 );
1027 surface = wndPtr->surface;
1028 wndPtr->surface = NULL;
1029 WIN_ReleasePtr( wndPtr );
1031 if (icon_title) DestroyWindow( icon_title );
1032 if (menu) DestroyMenu( menu );
1033 if (sys_menu) DestroyMenu( sys_menu );
1034 if (surface)
1036 register_window_surface( surface, NULL );
1037 window_surface_release( surface );
1040 USER_Driver->pDestroyWindow( hwnd );
1042 free_window_handle( hwnd );
1043 return 0;
1047 /***********************************************************************
1048 * next_thread_window
1050 static WND *next_thread_window( HWND *hwnd )
1052 struct user_object *ptr;
1053 WND *win;
1054 WORD index = *hwnd ? USER_HANDLE_TO_INDEX( *hwnd ) + 1 : 0;
1056 USER_Lock();
1057 while (index < NB_USER_HANDLES)
1059 if (!(ptr = user_handles[index++])) continue;
1060 if (ptr->type != USER_WINDOW) continue;
1061 win = (WND *)ptr;
1062 if (win->tid != GetCurrentThreadId()) continue;
1063 *hwnd = ptr->handle;
1064 return win;
1066 USER_Unlock();
1067 return NULL;
1071 /***********************************************************************
1072 * destroy_thread_windows
1074 * Destroy all window owned by the current thread.
1076 void destroy_thread_windows(void)
1078 WND *wndPtr;
1079 HWND hwnd = 0, *list;
1080 HMENU menu, sys_menu;
1081 struct window_surface *surface;
1082 int i;
1084 while ((wndPtr = next_thread_window( &hwnd )))
1086 /* destroy the client-side storage */
1088 list = WIN_ListChildren( hwnd );
1089 menu = ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD) ? (HMENU)wndPtr->wIDmenu : 0;
1090 sys_menu = wndPtr->hSysMenu;
1091 free_dce( wndPtr->dce, hwnd );
1092 surface = wndPtr->surface;
1093 InterlockedCompareExchangePointer( &user_handles[USER_HANDLE_TO_INDEX(hwnd)], NULL, wndPtr );
1094 WIN_ReleasePtr( wndPtr );
1095 HeapFree( GetProcessHeap(), 0, wndPtr );
1096 if (menu) DestroyMenu( menu );
1097 if (sys_menu) DestroyMenu( sys_menu );
1098 if (surface)
1100 register_window_surface( surface, NULL );
1101 window_surface_release( surface );
1104 /* free child windows */
1106 if (!list) continue;
1107 for (i = 0; list[i]; i++)
1108 if (!WIN_IsCurrentThread( list[i] ))
1109 SendNotifyMessageW( list[i], WM_WINE_DESTROYWINDOW, 0, 0 );
1110 HeapFree( GetProcessHeap(), 0, list );
1115 /***********************************************************************
1116 * WIN_FixCoordinates
1118 * Fix the coordinates - Helper for WIN_CreateWindowEx.
1119 * returns default show mode in sw.
1121 static void WIN_FixCoordinates( CREATESTRUCTW *cs, INT *sw)
1123 #define IS_DEFAULT(x) ((x) == CW_USEDEFAULT || (x) == (SHORT)0x8000)
1124 POINT pos[2];
1126 if (cs->dwExStyle & WS_EX_MDICHILD)
1128 UINT id = 0;
1130 MDI_CalcDefaultChildPos(cs->hwndParent, -1, pos, 0, &id);
1131 if (!(cs->style & WS_POPUP)) cs->hMenu = ULongToHandle(id);
1133 TRACE("MDI child id %04x\n", id);
1136 if (cs->style & (WS_CHILD | WS_POPUP))
1138 if (cs->dwExStyle & WS_EX_MDICHILD)
1140 if (IS_DEFAULT(cs->x))
1142 cs->x = pos[0].x;
1143 cs->y = pos[0].y;
1145 if (IS_DEFAULT(cs->cx) || !cs->cx) cs->cx = pos[1].x;
1146 if (IS_DEFAULT(cs->cy) || !cs->cy) cs->cy = pos[1].y;
1148 else
1150 if (IS_DEFAULT(cs->x)) cs->x = cs->y = 0;
1151 if (IS_DEFAULT(cs->cx)) cs->cx = cs->cy = 0;
1154 else /* overlapped window */
1156 HMONITOR monitor;
1157 MONITORINFO mon_info;
1158 STARTUPINFOW info;
1160 if (!IS_DEFAULT(cs->x) && !IS_DEFAULT(cs->cx) && !IS_DEFAULT(cs->cy)) return;
1162 monitor = MonitorFromWindow( cs->hwndParent, MONITOR_DEFAULTTOPRIMARY );
1163 mon_info.cbSize = sizeof(mon_info);
1164 GetMonitorInfoW( monitor, &mon_info );
1165 GetStartupInfoW( &info );
1167 if (IS_DEFAULT(cs->x))
1169 if (!IS_DEFAULT(cs->y)) *sw = cs->y;
1170 cs->x = (info.dwFlags & STARTF_USEPOSITION) ? info.dwX : mon_info.rcWork.left;
1171 cs->y = (info.dwFlags & STARTF_USEPOSITION) ? info.dwY : mon_info.rcWork.top;
1174 if (IS_DEFAULT(cs->cx))
1176 if (info.dwFlags & STARTF_USESIZE)
1178 cs->cx = info.dwXSize;
1179 cs->cy = info.dwYSize;
1181 else
1183 cs->cx = (mon_info.rcWork.right - mon_info.rcWork.left) * 3 / 4 - cs->x;
1184 cs->cy = (mon_info.rcWork.bottom - mon_info.rcWork.top) * 3 / 4 - cs->y;
1187 /* neither x nor cx are default. Check the y values .
1188 * In the trace we see Outlook and Outlook Express using
1189 * cy set to CW_USEDEFAULT when opening the address book.
1191 else if (IS_DEFAULT(cs->cy))
1193 FIXME("Strange use of CW_USEDEFAULT in nHeight\n");
1194 cs->cy = (mon_info.rcWork.bottom - mon_info.rcWork.top) * 3 / 4 - cs->y;
1197 #undef IS_DEFAULT
1200 /***********************************************************************
1201 * dump_window_styles
1203 static void dump_window_styles( DWORD style, DWORD exstyle )
1205 TRACE( "style:" );
1206 if(style & WS_POPUP) TRACE(" WS_POPUP");
1207 if(style & WS_CHILD) TRACE(" WS_CHILD");
1208 if(style & WS_MINIMIZE) TRACE(" WS_MINIMIZE");
1209 if(style & WS_VISIBLE) TRACE(" WS_VISIBLE");
1210 if(style & WS_DISABLED) TRACE(" WS_DISABLED");
1211 if(style & WS_CLIPSIBLINGS) TRACE(" WS_CLIPSIBLINGS");
1212 if(style & WS_CLIPCHILDREN) TRACE(" WS_CLIPCHILDREN");
1213 if(style & WS_MAXIMIZE) TRACE(" WS_MAXIMIZE");
1214 if((style & WS_CAPTION) == WS_CAPTION) TRACE(" WS_CAPTION");
1215 else
1217 if(style & WS_BORDER) TRACE(" WS_BORDER");
1218 if(style & WS_DLGFRAME) TRACE(" WS_DLGFRAME");
1220 if(style & WS_VSCROLL) TRACE(" WS_VSCROLL");
1221 if(style & WS_HSCROLL) TRACE(" WS_HSCROLL");
1222 if(style & WS_SYSMENU) TRACE(" WS_SYSMENU");
1223 if(style & WS_THICKFRAME) TRACE(" WS_THICKFRAME");
1224 if (style & WS_CHILD)
1226 if(style & WS_GROUP) TRACE(" WS_GROUP");
1227 if(style & WS_TABSTOP) TRACE(" WS_TABSTOP");
1229 else
1231 if(style & WS_MINIMIZEBOX) TRACE(" WS_MINIMIZEBOX");
1232 if(style & WS_MAXIMIZEBOX) TRACE(" WS_MAXIMIZEBOX");
1235 /* FIXME: Add dumping of BS_/ES_/SBS_/LBS_/CBS_/DS_/etc. styles */
1236 #define DUMPED_STYLES \
1237 ((DWORD)(WS_POPUP | \
1238 WS_CHILD | \
1239 WS_MINIMIZE | \
1240 WS_VISIBLE | \
1241 WS_DISABLED | \
1242 WS_CLIPSIBLINGS | \
1243 WS_CLIPCHILDREN | \
1244 WS_MAXIMIZE | \
1245 WS_BORDER | \
1246 WS_DLGFRAME | \
1247 WS_VSCROLL | \
1248 WS_HSCROLL | \
1249 WS_SYSMENU | \
1250 WS_THICKFRAME | \
1251 WS_GROUP | \
1252 WS_TABSTOP | \
1253 WS_MINIMIZEBOX | \
1254 WS_MAXIMIZEBOX))
1256 if(style & ~DUMPED_STYLES) TRACE(" %08x", style & ~DUMPED_STYLES);
1257 TRACE("\n");
1258 #undef DUMPED_STYLES
1260 TRACE( "exstyle:" );
1261 if(exstyle & WS_EX_DLGMODALFRAME) TRACE(" WS_EX_DLGMODALFRAME");
1262 if(exstyle & WS_EX_DRAGDETECT) TRACE(" WS_EX_DRAGDETECT");
1263 if(exstyle & WS_EX_NOPARENTNOTIFY) TRACE(" WS_EX_NOPARENTNOTIFY");
1264 if(exstyle & WS_EX_TOPMOST) TRACE(" WS_EX_TOPMOST");
1265 if(exstyle & WS_EX_ACCEPTFILES) TRACE(" WS_EX_ACCEPTFILES");
1266 if(exstyle & WS_EX_TRANSPARENT) TRACE(" WS_EX_TRANSPARENT");
1267 if(exstyle & WS_EX_MDICHILD) TRACE(" WS_EX_MDICHILD");
1268 if(exstyle & WS_EX_TOOLWINDOW) TRACE(" WS_EX_TOOLWINDOW");
1269 if(exstyle & WS_EX_WINDOWEDGE) TRACE(" WS_EX_WINDOWEDGE");
1270 if(exstyle & WS_EX_CLIENTEDGE) TRACE(" WS_EX_CLIENTEDGE");
1271 if(exstyle & WS_EX_CONTEXTHELP) TRACE(" WS_EX_CONTEXTHELP");
1272 if(exstyle & WS_EX_RIGHT) TRACE(" WS_EX_RIGHT");
1273 if(exstyle & WS_EX_RTLREADING) TRACE(" WS_EX_RTLREADING");
1274 if(exstyle & WS_EX_LEFTSCROLLBAR) TRACE(" WS_EX_LEFTSCROLLBAR");
1275 if(exstyle & WS_EX_CONTROLPARENT) TRACE(" WS_EX_CONTROLPARENT");
1276 if(exstyle & WS_EX_STATICEDGE) TRACE(" WS_EX_STATICEDGE");
1277 if(exstyle & WS_EX_APPWINDOW) TRACE(" WS_EX_APPWINDOW");
1278 if(exstyle & WS_EX_LAYERED) TRACE(" WS_EX_LAYERED");
1279 if(exstyle & WS_EX_NOINHERITLAYOUT) TRACE(" WS_EX_NOINHERITLAYOUT");
1280 if(exstyle & WS_EX_LAYOUTRTL) TRACE(" WS_EX_LAYOUTRTL");
1281 if(exstyle & WS_EX_COMPOSITED) TRACE(" WS_EX_COMPOSITED");
1282 if(exstyle & WS_EX_NOACTIVATE) TRACE(" WS_EX_NOACTIVATE");
1284 #define DUMPED_EX_STYLES \
1285 ((DWORD)(WS_EX_DLGMODALFRAME | \
1286 WS_EX_DRAGDETECT | \
1287 WS_EX_NOPARENTNOTIFY | \
1288 WS_EX_TOPMOST | \
1289 WS_EX_ACCEPTFILES | \
1290 WS_EX_TRANSPARENT | \
1291 WS_EX_MDICHILD | \
1292 WS_EX_TOOLWINDOW | \
1293 WS_EX_WINDOWEDGE | \
1294 WS_EX_CLIENTEDGE | \
1295 WS_EX_CONTEXTHELP | \
1296 WS_EX_RIGHT | \
1297 WS_EX_RTLREADING | \
1298 WS_EX_LEFTSCROLLBAR | \
1299 WS_EX_CONTROLPARENT | \
1300 WS_EX_STATICEDGE | \
1301 WS_EX_APPWINDOW | \
1302 WS_EX_LAYERED | \
1303 WS_EX_NOINHERITLAYOUT | \
1304 WS_EX_LAYOUTRTL | \
1305 WS_EX_COMPOSITED |\
1306 WS_EX_NOACTIVATE))
1308 if(exstyle & ~DUMPED_EX_STYLES) TRACE(" %08x", exstyle & ~DUMPED_EX_STYLES);
1309 TRACE("\n");
1310 #undef DUMPED_EX_STYLES
1313 /***********************************************************************
1314 * map_dpi_create_struct
1316 static void map_dpi_create_struct( CREATESTRUCTW *cs, UINT dpi_from, UINT dpi_to )
1318 if (!dpi_from && !dpi_to) return;
1319 if (!dpi_from || !dpi_to)
1321 POINT pt = { cs->x, cs->y };
1322 UINT mon_dpi = get_monitor_dpi( MonitorFromPoint( pt, MONITOR_DEFAULTTONEAREST ));
1323 if (!dpi_from) dpi_from = mon_dpi;
1324 else dpi_to = mon_dpi;
1326 if (dpi_from == dpi_to) return;
1327 cs->x = MulDiv( cs->x, dpi_to, dpi_from );
1328 cs->y = MulDiv( cs->y, dpi_to, dpi_from );
1329 cs->cx = MulDiv( cs->cx, dpi_to, dpi_from );
1330 cs->cy = MulDiv( cs->cy, dpi_to, dpi_from );
1333 /***********************************************************************
1334 * WIN_CreateWindowEx
1336 * Implementation of CreateWindowEx().
1338 HWND WIN_CreateWindowEx( CREATESTRUCTW *cs, LPCWSTR className, HINSTANCE module, BOOL unicode )
1340 INT cx, cy, style, sw = SW_SHOW;
1341 LRESULT result;
1342 RECT rect;
1343 WND *wndPtr;
1344 HWND hwnd, parent, owner, top_child = 0;
1345 const WCHAR *p = className;
1346 UINT win_dpi, thread_dpi = get_thread_dpi();
1347 DPI_AWARENESS_CONTEXT context;
1348 MDICREATESTRUCTW mdi_cs;
1349 CBT_CREATEWNDW cbtc;
1350 CREATESTRUCTW cbcs;
1352 className = CLASS_GetVersionedName(className, NULL, NULL, TRUE);
1354 TRACE("%s %s%s%s ex=%08x style=%08x %d,%d %dx%d parent=%p menu=%p inst=%p params=%p\n",
1355 unicode ? debugstr_w(cs->lpszName) : debugstr_a((LPCSTR)cs->lpszName),
1356 debugstr_w(p), p != className ? "->" : "", p != className ? debugstr_w(className) : "",
1357 cs->dwExStyle, cs->style, cs->x, cs->y, cs->cx, cs->cy,
1358 cs->hwndParent, cs->hMenu, cs->hInstance, cs->lpCreateParams );
1359 if(TRACE_ON(win)) dump_window_styles( cs->style, cs->dwExStyle );
1361 /* Fix the styles for MDI children */
1362 if (cs->dwExStyle & WS_EX_MDICHILD)
1364 if (!(win_get_flags( cs->hwndParent ) & WIN_ISMDICLIENT))
1366 WARN("WS_EX_MDICHILD, but parent %p is not MDIClient\n", cs->hwndParent);
1367 return 0;
1370 /* cs->lpCreateParams of WM_[NC]CREATE is different for MDI children.
1371 * MDICREATESTRUCT members have the originally passed values.
1373 * Note: we rely on the fact that MDICREATESTRUCTA and MDICREATESTRUCTW
1374 * have the same layout.
1376 mdi_cs.szClass = cs->lpszClass;
1377 mdi_cs.szTitle = cs->lpszName;
1378 mdi_cs.hOwner = cs->hInstance;
1379 mdi_cs.x = cs->x;
1380 mdi_cs.y = cs->y;
1381 mdi_cs.cx = cs->cx;
1382 mdi_cs.cy = cs->cy;
1383 mdi_cs.style = cs->style;
1384 mdi_cs.lParam = (LPARAM)cs->lpCreateParams;
1386 cs->lpCreateParams = &mdi_cs;
1388 if (GetWindowLongW(cs->hwndParent, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1390 if (cs->style & WS_POPUP)
1392 TRACE("WS_POPUP with MDIS_ALLCHILDSTYLES is not allowed\n");
1393 return 0;
1395 cs->style |= WS_CHILD | WS_CLIPSIBLINGS;
1397 else
1399 cs->style &= ~WS_POPUP;
1400 cs->style |= WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CAPTION |
1401 WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
1404 top_child = GetWindow(cs->hwndParent, GW_CHILD);
1406 if (top_child)
1408 /* Restore current maximized child */
1409 if((cs->style & WS_VISIBLE) && IsZoomed(top_child))
1411 TRACE("Restoring current maximized child %p\n", top_child);
1412 if (cs->style & WS_MAXIMIZE)
1414 /* if the new window is maximized don't bother repainting */
1415 SendMessageW( top_child, WM_SETREDRAW, FALSE, 0 );
1416 ShowWindow( top_child, SW_SHOWNORMAL );
1417 SendMessageW( top_child, WM_SETREDRAW, TRUE, 0 );
1419 else ShowWindow( top_child, SW_SHOWNORMAL );
1424 /* Find the parent window */
1426 parent = cs->hwndParent;
1427 owner = 0;
1429 if (cs->hwndParent == HWND_MESSAGE)
1431 cs->hwndParent = parent = get_hwnd_message_parent();
1433 else if (cs->hwndParent)
1435 if ((cs->style & (WS_CHILD|WS_POPUP)) != WS_CHILD)
1437 parent = GetDesktopWindow();
1438 owner = cs->hwndParent;
1440 else
1442 DWORD parent_style = GetWindowLongW( parent, GWL_EXSTYLE );
1443 if ((parent_style & WS_EX_LAYOUTRTL) && !(parent_style & WS_EX_NOINHERITLAYOUT))
1444 cs->dwExStyle |= WS_EX_LAYOUTRTL;
1447 else
1449 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
1451 if ((cs->style & (WS_CHILD|WS_POPUP)) == WS_CHILD)
1453 WARN("No parent for child window\n" );
1454 SetLastError(ERROR_TLW_WITH_WSCHILD);
1455 return 0; /* WS_CHILD needs a parent, but WS_POPUP doesn't */
1458 /* are we creating the desktop or HWND_MESSAGE parent itself? */
1459 if (className != (LPCWSTR)DESKTOP_CLASS_ATOM &&
1460 (IS_INTRESOURCE(className) || strcmpiW( className, messageW )))
1462 DWORD layout;
1463 GetProcessDefaultLayout( &layout );
1464 if (layout & LAYOUT_RTL) cs->dwExStyle |= WS_EX_LAYOUTRTL;
1465 parent = GetDesktopWindow();
1469 WIN_FixCoordinates(cs, &sw); /* fix default coordinates */
1471 if ((cs->dwExStyle & WS_EX_DLGMODALFRAME) ||
1472 ((!(cs->dwExStyle & WS_EX_STATICEDGE)) &&
1473 (cs->style & (WS_DLGFRAME | WS_THICKFRAME))))
1474 cs->dwExStyle |= WS_EX_WINDOWEDGE;
1475 else
1476 cs->dwExStyle &= ~WS_EX_WINDOWEDGE;
1478 /* Create the window structure */
1480 if (!(wndPtr = create_window_handle( parent, owner, className, module, unicode )))
1482 WNDCLASSW wc;
1483 /* if it's a comctl32 class, GetClassInfo will load it, then we can retry */
1484 if (GetLastError() != ERROR_INVALID_HANDLE ||
1485 !GetClassInfoW( 0, className, &wc ) ||
1486 !(wndPtr = create_window_handle( parent, owner, className, module, unicode )))
1487 return 0;
1489 hwnd = wndPtr->obj.handle;
1491 /* Fill the window structure */
1493 wndPtr->tid = GetCurrentThreadId();
1494 wndPtr->hInstance = cs->hInstance;
1495 wndPtr->text = NULL;
1496 wndPtr->dwStyle = cs->style & ~WS_VISIBLE;
1497 wndPtr->dwExStyle = cs->dwExStyle;
1498 wndPtr->wIDmenu = 0;
1499 wndPtr->helpContext = 0;
1500 wndPtr->pScroll = NULL;
1501 wndPtr->userdata = 0;
1502 wndPtr->hIcon = 0;
1503 wndPtr->hIconSmall = 0;
1504 wndPtr->hIconSmall2 = 0;
1505 wndPtr->hSysMenu = 0;
1507 wndPtr->min_pos.x = wndPtr->min_pos.y = -1;
1508 wndPtr->max_pos.x = wndPtr->max_pos.y = -1;
1509 SetRect( &wndPtr->normal_rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
1511 if (wndPtr->dwStyle & WS_SYSMENU) SetSystemMenu( hwnd, 0 );
1514 * Correct the window styles.
1516 * It affects only the style loaded into the WIN structure.
1519 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
1521 wndPtr->dwStyle |= WS_CLIPSIBLINGS;
1522 if (!(wndPtr->dwStyle & WS_POPUP))
1523 wndPtr->dwStyle |= WS_CAPTION;
1526 /* WS_EX_WINDOWEDGE depends on some other styles */
1527 if (wndPtr->dwExStyle & WS_EX_DLGMODALFRAME)
1528 wndPtr->dwExStyle |= WS_EX_WINDOWEDGE;
1529 else if (wndPtr->dwStyle & (WS_DLGFRAME | WS_THICKFRAME))
1531 if (!((wndPtr->dwExStyle & WS_EX_STATICEDGE) &&
1532 (wndPtr->dwStyle & (WS_CHILD | WS_POPUP))))
1533 wndPtr->dwExStyle |= WS_EX_WINDOWEDGE;
1535 else
1536 wndPtr->dwExStyle &= ~WS_EX_WINDOWEDGE;
1538 if (!(wndPtr->dwStyle & (WS_CHILD | WS_POPUP)))
1539 wndPtr->flags |= WIN_NEED_SIZE;
1541 SERVER_START_REQ( set_window_info )
1543 req->handle = wine_server_user_handle( hwnd );
1544 req->flags = SET_WIN_STYLE | SET_WIN_EXSTYLE | SET_WIN_INSTANCE | SET_WIN_UNICODE;
1545 req->style = wndPtr->dwStyle;
1546 req->ex_style = wndPtr->dwExStyle;
1547 req->instance = wine_server_client_ptr( wndPtr->hInstance );
1548 req->is_unicode = (wndPtr->flags & WIN_ISUNICODE) != 0;
1549 req->extra_offset = -1;
1550 wine_server_call( req );
1552 SERVER_END_REQ;
1554 /* Set the window menu */
1556 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
1558 if (cs->hMenu)
1560 if (!MENU_SetMenu(hwnd, cs->hMenu))
1562 WIN_ReleasePtr( wndPtr );
1563 free_window_handle( hwnd );
1564 return 0;
1567 else
1569 LPCWSTR menuName = (LPCWSTR)GetClassLongPtrW( hwnd, GCLP_MENUNAME );
1570 if (menuName)
1572 cs->hMenu = LoadMenuW( cs->hInstance, menuName );
1573 if (cs->hMenu) MENU_SetMenu( hwnd, cs->hMenu );
1577 else SetWindowLongPtrW( hwnd, GWLP_ID, (ULONG_PTR)cs->hMenu );
1579 style = wndPtr->dwStyle;
1580 win_dpi = wndPtr->dpi;
1581 WIN_ReleasePtr( wndPtr );
1583 if (parent) map_dpi_create_struct( cs, thread_dpi, win_dpi );
1585 context = SetThreadDpiAwarenessContext( GetWindowDpiAwarenessContext( hwnd ));
1587 /* call the WH_CBT hook */
1589 /* the window style passed to the hook must be the real window style,
1590 * rather than just the window style that the caller to CreateWindowEx
1591 * passed in, so we have to copy the original CREATESTRUCT and get the
1592 * the real style. */
1593 cbcs = *cs;
1594 cbcs.style = style;
1595 cbtc.lpcs = &cbcs;
1596 cbtc.hwndInsertAfter = HWND_TOP;
1597 if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode )) goto failed;
1599 /* send the WM_GETMINMAXINFO message and fix the size if needed */
1601 cx = cs->cx;
1602 cy = cs->cy;
1603 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
1605 MINMAXINFO info = WINPOS_GetMinMaxInfo( hwnd );
1606 cx = max( min( cx, info.ptMaxTrackSize.x ), info.ptMinTrackSize.x );
1607 cy = max( min( cy, info.ptMaxTrackSize.y ), info.ptMinTrackSize.y );
1610 if (cx < 0) cx = 0;
1611 if (cy < 0) cy = 0;
1612 SetRect( &rect, cs->x, cs->y, cs->x + cx, cs->y + cy );
1613 /* check for wraparound */
1614 if (cs->x > 0x7fffffff - cx) rect.right = 0x7fffffff;
1615 if (cs->y > 0x7fffffff - cy) rect.bottom = 0x7fffffff;
1616 if (!set_window_pos( hwnd, 0, SWP_NOZORDER | SWP_NOACTIVATE, &rect, &rect, NULL )) goto failed;
1618 /* send WM_NCCREATE */
1620 TRACE( "hwnd %p cs %d,%d %dx%d %s\n", hwnd, cs->x, cs->y, cs->cx, cs->cy, wine_dbgstr_rect(&rect) );
1621 if (unicode)
1622 result = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1623 else
1624 result = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1625 if (!result)
1627 WARN( "%p: aborted by WM_NCCREATE\n", hwnd );
1628 goto failed;
1631 /* create default IME window */
1633 if (imm_register_window && !is_desktop_window( hwnd ) &&
1634 parent != get_hwnd_message_parent() && imm_register_window( hwnd ))
1636 TRACE("register IME window for %p\n", hwnd);
1637 win_set_flags( hwnd, WIN_HAS_IME_WIN, 0 );
1640 /* send WM_NCCALCSIZE */
1642 if (WIN_GetRectangles( hwnd, COORDS_PARENT, &rect, NULL ))
1644 /* yes, even if the CBT hook was called with HWND_TOP */
1645 HWND insert_after = (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
1646 RECT client_rect = rect;
1648 /* the rectangle is in screen coords for WM_NCCALCSIZE when wparam is FALSE */
1649 MapWindowPoints( parent, 0, (POINT *)&client_rect, 2 );
1650 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&client_rect );
1651 MapWindowPoints( 0, parent, (POINT *)&client_rect, 2 );
1652 set_window_pos( hwnd, insert_after, SWP_NOACTIVATE, &rect, &client_rect, NULL );
1654 else goto failed;
1656 /* send WM_CREATE */
1658 if (unicode)
1659 result = SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs );
1660 else
1661 result = SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs );
1662 if (result == -1) goto failed;
1664 /* call the driver */
1666 if (!USER_Driver->pCreateWindow( hwnd )) goto failed;
1668 NotifyWinEvent(EVENT_OBJECT_CREATE, hwnd, OBJID_WINDOW, 0);
1670 /* send the size messages */
1672 if (!(win_get_flags( hwnd ) & WIN_NEED_SIZE))
1674 WIN_GetRectangles( hwnd, COORDS_PARENT, NULL, &rect );
1675 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1676 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1677 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( rect.left, rect.top ) );
1680 /* Show the window, maximizing or minimizing if needed */
1682 style = WIN_SetStyle( hwnd, 0, WS_MAXIMIZE | WS_MINIMIZE );
1683 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1685 RECT newPos;
1686 UINT swFlag = (style & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1688 swFlag = WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1689 swFlag |= SWP_FRAMECHANGED; /* Frame always gets changed */
1690 if (!(style & WS_VISIBLE) || (style & WS_CHILD) || GetActiveWindow()) swFlag |= SWP_NOACTIVATE;
1691 SetWindowPos( hwnd, 0, newPos.left, newPos.top, newPos.right - newPos.left,
1692 newPos.bottom - newPos.top, swFlag );
1695 /* Notify the parent window only */
1697 send_parent_notify( hwnd, WM_CREATE );
1698 if (!IsWindow( hwnd ))
1700 SetThreadDpiAwarenessContext( context );
1701 return 0;
1704 if (parent == GetDesktopWindow())
1705 PostMessageW( parent, WM_PARENTNOTIFY, WM_CREATE, (LPARAM)hwnd );
1707 if (cs->style & WS_VISIBLE)
1709 if (cs->style & WS_MAXIMIZE)
1710 sw = SW_SHOW;
1711 else if (cs->style & WS_MINIMIZE)
1712 sw = SW_SHOWMINIMIZED;
1714 ShowWindow( hwnd, sw );
1715 if (cs->dwExStyle & WS_EX_MDICHILD)
1717 SendMessageW(cs->hwndParent, WM_MDIREFRESHMENU, 0, 0);
1718 /* ShowWindow won't activate child windows */
1719 SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE );
1723 /* Call WH_SHELL hook */
1725 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) && !GetWindow( hwnd, GW_OWNER ))
1726 HOOK_CallHooks( WH_SHELL, HSHELL_WINDOWCREATED, (WPARAM)hwnd, 0, TRUE );
1728 TRACE("created window %p\n", hwnd);
1729 SetThreadDpiAwarenessContext( context );
1730 return hwnd;
1732 failed:
1733 WIN_DestroyWindow( hwnd );
1734 SetThreadDpiAwarenessContext( context );
1735 return 0;
1739 /***********************************************************************
1740 * CreateWindowExA (USER32.@)
1742 HWND WINAPI DECLSPEC_HOTPATCH CreateWindowExA( DWORD exStyle, LPCSTR className,
1743 LPCSTR windowName, DWORD style, INT x,
1744 INT y, INT width, INT height,
1745 HWND parent, HMENU menu,
1746 HINSTANCE instance, LPVOID data )
1748 CREATESTRUCTA cs;
1750 cs.lpCreateParams = data;
1751 cs.hInstance = instance;
1752 cs.hMenu = menu;
1753 cs.hwndParent = parent;
1754 cs.x = x;
1755 cs.y = y;
1756 cs.cx = width;
1757 cs.cy = height;
1758 cs.style = style;
1759 cs.lpszName = windowName;
1760 cs.lpszClass = className;
1761 cs.dwExStyle = exStyle;
1763 if (!IS_INTRESOURCE(className))
1765 WCHAR bufferW[256];
1766 if (!MultiByteToWideChar( CP_ACP, 0, className, -1, bufferW, ARRAY_SIZE( bufferW )))
1767 return 0;
1768 return wow_handlers.create_window( (CREATESTRUCTW *)&cs, bufferW, instance, FALSE );
1770 /* Note: we rely on the fact that CREATESTRUCTA and */
1771 /* CREATESTRUCTW have the same layout. */
1772 return wow_handlers.create_window( (CREATESTRUCTW *)&cs, (LPCWSTR)className, instance, FALSE );
1776 /***********************************************************************
1777 * CreateWindowExW (USER32.@)
1779 HWND WINAPI DECLSPEC_HOTPATCH CreateWindowExW( DWORD exStyle, LPCWSTR className,
1780 LPCWSTR windowName, DWORD style, INT x,
1781 INT y, INT width, INT height,
1782 HWND parent, HMENU menu,
1783 HINSTANCE instance, LPVOID data )
1785 CREATESTRUCTW cs;
1787 cs.lpCreateParams = data;
1788 cs.hInstance = instance;
1789 cs.hMenu = menu;
1790 cs.hwndParent = parent;
1791 cs.x = x;
1792 cs.y = y;
1793 cs.cx = width;
1794 cs.cy = height;
1795 cs.style = style;
1796 cs.lpszName = windowName;
1797 cs.lpszClass = className;
1798 cs.dwExStyle = exStyle;
1800 return wow_handlers.create_window( &cs, className, instance, TRUE );
1804 /***********************************************************************
1805 * WIN_SendDestroyMsg
1807 static void WIN_SendDestroyMsg( HWND hwnd )
1809 GUITHREADINFO info;
1811 info.cbSize = sizeof(info);
1812 if (GetGUIThreadInfo( GetCurrentThreadId(), &info ))
1814 if (hwnd == info.hwndCaret) DestroyCaret();
1815 if (hwnd == info.hwndActive) WINPOS_ActivateOtherWindow( hwnd );
1818 if (hwnd == GetClipboardOwner()) CLIPBOARD_ReleaseOwner( hwnd );
1821 * Send the WM_DESTROY to the window.
1823 SendMessageW( hwnd, WM_DESTROY, 0, 0);
1826 * This WM_DESTROY message can trigger re-entrant calls to DestroyWindow
1827 * make sure that the window still exists when we come back.
1829 if (IsWindow(hwnd))
1831 HWND* pWndArray;
1832 int i;
1834 if (!(pWndArray = WIN_ListChildren( hwnd ))) return;
1836 for (i = 0; pWndArray[i]; i++)
1838 if (IsWindow( pWndArray[i] )) WIN_SendDestroyMsg( pWndArray[i] );
1840 HeapFree( GetProcessHeap(), 0, pWndArray );
1842 else
1843 WARN("\tdestroyed itself while in WM_DESTROY!\n");
1847 /***********************************************************************
1848 * DestroyWindow (USER32.@)
1850 BOOL WINAPI DestroyWindow( HWND hwnd )
1852 BOOL is_child;
1854 if (!(hwnd = WIN_IsCurrentThread( hwnd )) || is_desktop_window( hwnd ))
1856 SetLastError( ERROR_ACCESS_DENIED );
1857 return FALSE;
1860 TRACE("(%p)\n", hwnd);
1862 /* Call hooks */
1864 if (HOOK_CallHooks( WH_CBT, HCBT_DESTROYWND, (WPARAM)hwnd, 0, TRUE )) return FALSE;
1866 if (MENU_IsMenuActive() == hwnd)
1867 EndMenu();
1869 is_child = (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) != 0;
1871 if (is_child)
1873 if (!USER_IsExitingThread( GetCurrentThreadId() ))
1874 send_parent_notify( hwnd, WM_DESTROY );
1876 else if (!GetWindow( hwnd, GW_OWNER ))
1878 HOOK_CallHooks( WH_SHELL, HSHELL_WINDOWDESTROYED, (WPARAM)hwnd, 0L, TRUE );
1879 /* FIXME: clean up palette - see "Internals" p.352 */
1882 if (!IsWindow(hwnd)) return TRUE;
1884 /* Hide the window */
1885 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)
1887 /* Only child windows receive WM_SHOWWINDOW in DestroyWindow() */
1888 if (is_child)
1889 ShowWindow( hwnd, SW_HIDE );
1890 else
1891 SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE |
1892 SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW );
1895 if (!IsWindow(hwnd)) return TRUE;
1897 /* Recursively destroy owned windows */
1899 if (!is_child)
1901 for (;;)
1903 int i;
1904 BOOL got_one = FALSE;
1905 HWND *list = WIN_ListChildren( GetDesktopWindow() );
1906 if (list)
1908 for (i = 0; list[i]; i++)
1910 if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
1911 if (WIN_IsCurrentThread( list[i] ))
1913 DestroyWindow( list[i] );
1914 got_one = TRUE;
1915 continue;
1917 WIN_SetOwner( list[i], 0 );
1919 HeapFree( GetProcessHeap(), 0, list );
1921 if (!got_one) break;
1925 /* Send destroy messages */
1927 WIN_SendDestroyMsg( hwnd );
1928 if (!IsWindow( hwnd )) return TRUE;
1930 /* Destroy the window storage */
1932 WIN_DestroyWindow( hwnd );
1933 return TRUE;
1937 /***********************************************************************
1938 * CloseWindow (USER32.@)
1940 BOOL WINAPI CloseWindow( HWND hwnd )
1942 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) return FALSE;
1943 ShowWindow( hwnd, SW_MINIMIZE );
1944 return TRUE;
1948 /***********************************************************************
1949 * OpenIcon (USER32.@)
1951 BOOL WINAPI OpenIcon( HWND hwnd )
1953 if (!IsIconic( hwnd )) return FALSE;
1954 ShowWindow( hwnd, SW_SHOWNORMAL );
1955 return TRUE;
1959 /***********************************************************************
1960 * FindWindowExW (USER32.@)
1962 HWND WINAPI FindWindowExW( HWND parent, HWND child, LPCWSTR className, LPCWSTR title )
1964 HWND *list;
1965 HWND retvalue = 0;
1966 int i = 0, len = 0;
1967 WCHAR *buffer = NULL;
1969 if (!parent && child) parent = GetDesktopWindow();
1970 else if (parent == HWND_MESSAGE) parent = get_hwnd_message_parent();
1972 if (title)
1974 len = strlenW(title) + 1; /* one extra char to check for chars beyond the end */
1975 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return 0;
1978 if (!(list = list_window_children( 0, parent, className, 0 ))) goto done;
1980 if (child)
1982 child = WIN_GetFullHandle( child );
1983 while (list[i] && list[i] != child) i++;
1984 if (!list[i]) goto done;
1985 i++; /* start from next window */
1988 if (title)
1990 while (list[i])
1992 if (InternalGetWindowText( list[i], buffer, len + 1 ))
1994 if (!strcmpiW( buffer, title )) break;
1996 else
1998 if (!title[0]) break;
2000 i++;
2003 retvalue = list[i];
2005 done:
2006 HeapFree( GetProcessHeap(), 0, list );
2007 HeapFree( GetProcessHeap(), 0, buffer );
2008 return retvalue;
2013 /***********************************************************************
2014 * FindWindowA (USER32.@)
2016 HWND WINAPI FindWindowA( LPCSTR className, LPCSTR title )
2018 HWND ret = FindWindowExA( 0, 0, className, title );
2019 if (!ret) SetLastError (ERROR_CANNOT_FIND_WND_CLASS);
2020 return ret;
2024 /***********************************************************************
2025 * FindWindowExA (USER32.@)
2027 HWND WINAPI FindWindowExA( HWND parent, HWND child, LPCSTR className, LPCSTR title )
2029 LPWSTR titleW = NULL;
2030 HWND hwnd = 0;
2032 if (title)
2034 DWORD len = MultiByteToWideChar( CP_ACP, 0, title, -1, NULL, 0 );
2035 if (!(titleW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return 0;
2036 MultiByteToWideChar( CP_ACP, 0, title, -1, titleW, len );
2039 if (!IS_INTRESOURCE(className))
2041 WCHAR classW[256];
2042 if (MultiByteToWideChar( CP_ACP, 0, className, -1, classW, ARRAY_SIZE( classW )))
2043 hwnd = FindWindowExW( parent, child, classW, titleW );
2045 else
2047 hwnd = FindWindowExW( parent, child, (LPCWSTR)className, titleW );
2050 HeapFree( GetProcessHeap(), 0, titleW );
2051 return hwnd;
2055 /***********************************************************************
2056 * FindWindowW (USER32.@)
2058 HWND WINAPI FindWindowW( LPCWSTR className, LPCWSTR title )
2060 return FindWindowExW( 0, 0, className, title );
2064 /**********************************************************************
2065 * GetDesktopWindow (USER32.@)
2067 HWND WINAPI GetDesktopWindow(void)
2069 struct user_thread_info *thread_info = get_user_thread_info();
2071 if (thread_info->top_window) return thread_info->top_window;
2073 SERVER_START_REQ( get_desktop_window )
2075 req->force = 0;
2076 if (!wine_server_call( req ))
2078 thread_info->top_window = wine_server_ptr_handle( reply->top_window );
2079 thread_info->msg_window = wine_server_ptr_handle( reply->msg_window );
2082 SERVER_END_REQ;
2084 if (!thread_info->top_window)
2086 static const WCHAR explorer[] = {'\\','e','x','p','l','o','r','e','r','.','e','x','e',0};
2087 static const WCHAR args[] = {' ','/','d','e','s','k','t','o','p',0};
2088 STARTUPINFOW si;
2089 PROCESS_INFORMATION pi;
2090 WCHAR windir[MAX_PATH];
2091 WCHAR app[MAX_PATH + ARRAY_SIZE( explorer )];
2092 WCHAR cmdline[MAX_PATH + ARRAY_SIZE( explorer ) + ARRAY_SIZE( args )];
2093 WCHAR desktop[MAX_PATH];
2094 void *redir;
2096 SERVER_START_REQ( set_user_object_info )
2098 req->handle = wine_server_obj_handle( GetThreadDesktop(GetCurrentThreadId()) );
2099 req->flags = SET_USER_OBJECT_GET_FULL_NAME;
2100 wine_server_set_reply( req, desktop, sizeof(desktop) - sizeof(WCHAR) );
2101 if (!wine_server_call( req ))
2103 size_t size = wine_server_reply_size( reply );
2104 desktop[size / sizeof(WCHAR)] = 0;
2105 TRACE( "starting explorer for desktop %s\n", debugstr_w(desktop) );
2107 else
2108 desktop[0] = 0;
2110 SERVER_END_REQ;
2112 memset( &si, 0, sizeof(si) );
2113 si.cb = sizeof(si);
2114 si.lpDesktop = *desktop ? desktop : NULL;
2115 si.dwFlags = STARTF_USESTDHANDLES;
2116 si.hStdInput = 0;
2117 si.hStdOutput = 0;
2118 si.hStdError = GetStdHandle( STD_ERROR_HANDLE );
2120 GetSystemDirectoryW( windir, MAX_PATH );
2121 strcpyW( app, windir );
2122 strcatW( app, explorer );
2123 strcpyW( cmdline, app );
2124 strcatW( cmdline, args );
2126 Wow64DisableWow64FsRedirection( &redir );
2127 if (CreateProcessW( app, cmdline, NULL, NULL, FALSE, DETACHED_PROCESS,
2128 NULL, windir, &si, &pi ))
2130 TRACE( "started explorer pid %04x tid %04x\n", pi.dwProcessId, pi.dwThreadId );
2131 WaitForInputIdle( pi.hProcess, 10000 );
2132 CloseHandle( pi.hThread );
2133 CloseHandle( pi.hProcess );
2135 else WARN( "failed to start explorer, err %d\n", GetLastError() );
2136 Wow64RevertWow64FsRedirection( redir );
2138 SERVER_START_REQ( get_desktop_window )
2140 req->force = 1;
2141 if (!wine_server_call( req ))
2143 thread_info->top_window = wine_server_ptr_handle( reply->top_window );
2144 thread_info->msg_window = wine_server_ptr_handle( reply->msg_window );
2147 SERVER_END_REQ;
2150 if (!thread_info->top_window || !USER_Driver->pCreateDesktopWindow( thread_info->top_window ))
2151 ERR( "failed to create desktop window\n" );
2153 return thread_info->top_window;
2157 /*******************************************************************
2158 * EnableWindow (USER32.@)
2160 BOOL WINAPI EnableWindow( HWND hwnd, BOOL enable )
2162 BOOL retvalue;
2164 if (is_broadcast(hwnd))
2166 SetLastError( ERROR_INVALID_PARAMETER );
2167 return FALSE;
2170 TRACE("( %p, %d )\n", hwnd, enable);
2172 if (enable)
2174 retvalue = (WIN_SetStyle( hwnd, 0, WS_DISABLED ) & WS_DISABLED) != 0;
2175 if (retvalue) SendMessageW( hwnd, WM_ENABLE, TRUE, 0 );
2177 else
2179 SendMessageW( hwnd, WM_CANCELMODE, 0, 0 );
2181 retvalue = (WIN_SetStyle( hwnd, WS_DISABLED, 0 ) & WS_DISABLED) != 0;
2182 if (!retvalue)
2184 if (hwnd == GetFocus())
2185 SetFocus( 0 ); /* A disabled window can't have the focus */
2187 SendMessageW( hwnd, WM_ENABLE, FALSE, 0 );
2190 return retvalue;
2194 /***********************************************************************
2195 * IsWindowEnabled (USER32.@)
2197 BOOL WINAPI IsWindowEnabled(HWND hWnd)
2199 LONG ret;
2201 SetLastError(NO_ERROR);
2202 ret = GetWindowLongW( hWnd, GWL_STYLE );
2203 if (!ret && GetLastError() != NO_ERROR) return FALSE;
2204 return !(ret & WS_DISABLED);
2207 /***********************************************************************
2208 * IsWindowUnicode (USER32.@)
2210 BOOL WINAPI IsWindowUnicode( HWND hwnd )
2212 WND * wndPtr;
2213 BOOL retvalue = FALSE;
2215 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
2217 if (wndPtr == WND_DESKTOP) return TRUE;
2219 if (wndPtr != WND_OTHER_PROCESS)
2221 retvalue = (wndPtr->flags & WIN_ISUNICODE) != 0;
2222 WIN_ReleasePtr( wndPtr );
2224 else
2226 SERVER_START_REQ( get_window_info )
2228 req->handle = wine_server_user_handle( hwnd );
2229 if (!wine_server_call_err( req )) retvalue = reply->is_unicode;
2231 SERVER_END_REQ;
2233 return retvalue;
2237 /***********************************************************************
2238 * GetWindowDpiAwarenessContext (USER32.@)
2240 DPI_AWARENESS_CONTEXT WINAPI GetWindowDpiAwarenessContext( HWND hwnd )
2242 WND *win;
2243 DPI_AWARENESS_CONTEXT ret = 0;
2245 if (!(win = WIN_GetPtr( hwnd )))
2247 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2248 return 0;
2250 if (win == WND_DESKTOP) return DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE;
2251 if (win != WND_OTHER_PROCESS)
2253 ret = ULongToHandle( win->dpi_awareness | 0x10 );
2254 WIN_ReleasePtr( win );
2256 else
2258 SERVER_START_REQ( get_window_info )
2260 req->handle = wine_server_user_handle( hwnd );
2261 if (!wine_server_call_err( req )) ret = ULongToHandle( reply->awareness | 0x10 );
2263 SERVER_END_REQ;
2265 return ret;
2269 /***********************************************************************
2270 * GetDpiForWindow (USER32.@)
2272 UINT WINAPI GetDpiForWindow( HWND hwnd )
2274 WND *win;
2275 UINT ret = 0;
2277 if (!(win = WIN_GetPtr( hwnd )))
2279 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2280 return 0;
2282 if (win == WND_DESKTOP)
2284 POINT pt = { 0, 0 };
2285 return get_monitor_dpi( MonitorFromPoint( pt, MONITOR_DEFAULTTOPRIMARY ));
2287 if (win != WND_OTHER_PROCESS)
2289 ret = win->dpi;
2290 if (!ret) ret = get_win_monitor_dpi( hwnd );
2291 WIN_ReleasePtr( win );
2293 else
2295 SERVER_START_REQ( get_window_info )
2297 req->handle = wine_server_user_handle( hwnd );
2298 if (!wine_server_call_err( req )) ret = reply->dpi;
2300 SERVER_END_REQ;
2302 return ret;
2306 /**********************************************************************
2307 * WIN_GetWindowLong
2309 * Helper function for GetWindowLong().
2311 static LONG_PTR WIN_GetWindowLong( HWND hwnd, INT offset, UINT size, BOOL unicode )
2313 LONG_PTR retvalue = 0;
2314 WND *wndPtr;
2316 if (offset == GWLP_HWNDPARENT)
2318 HWND parent = GetAncestor( hwnd, GA_PARENT );
2319 if (parent == GetDesktopWindow()) parent = GetWindow( hwnd, GW_OWNER );
2320 return (ULONG_PTR)parent;
2323 if (!(wndPtr = WIN_GetPtr( hwnd )))
2325 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2326 return 0;
2329 if (wndPtr == WND_DESKTOP)
2331 switch (offset)
2333 case GWL_STYLE:
2334 retvalue = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN; /* message parent is not visible */
2335 if (WIN_GetFullHandle( hwnd ) == GetDesktopWindow())
2336 retvalue |= WS_VISIBLE;
2337 return retvalue;
2338 case GWL_EXSTYLE:
2339 case GWLP_USERDATA:
2340 case GWLP_ID:
2341 case GWLP_HINSTANCE:
2342 return 0;
2343 case GWLP_WNDPROC:
2344 SetLastError( ERROR_ACCESS_DENIED );
2345 return 0;
2347 SetLastError( ERROR_INVALID_INDEX );
2348 return 0;
2351 if (wndPtr == WND_OTHER_PROCESS)
2353 if (offset == GWLP_WNDPROC)
2355 SetLastError( ERROR_ACCESS_DENIED );
2356 return 0;
2358 SERVER_START_REQ( set_window_info )
2360 req->handle = wine_server_user_handle( hwnd );
2361 req->flags = 0; /* don't set anything, just retrieve */
2362 req->extra_offset = (offset >= 0) ? offset : -1;
2363 req->extra_size = (offset >= 0) ? size : 0;
2364 if (!wine_server_call_err( req ))
2366 switch(offset)
2368 case GWL_STYLE: retvalue = reply->old_style; break;
2369 case GWL_EXSTYLE: retvalue = reply->old_ex_style; break;
2370 case GWLP_ID: retvalue = reply->old_id; break;
2371 case GWLP_HINSTANCE: retvalue = (ULONG_PTR)wine_server_get_ptr( reply->old_instance ); break;
2372 case GWLP_USERDATA: retvalue = reply->old_user_data; break;
2373 default:
2374 if (offset >= 0) retvalue = get_win_data( &reply->old_extra_value, size );
2375 else SetLastError( ERROR_INVALID_INDEX );
2376 break;
2380 SERVER_END_REQ;
2381 return retvalue;
2384 /* now we have a valid wndPtr */
2386 if (offset >= 0)
2388 if (offset > (int)(wndPtr->cbWndExtra - size))
2390 WARN("Invalid offset %d\n", offset );
2391 WIN_ReleasePtr( wndPtr );
2392 SetLastError( ERROR_INVALID_INDEX );
2393 return 0;
2395 retvalue = get_win_data( (char *)wndPtr->wExtra + offset, size );
2397 /* Special case for dialog window procedure */
2398 if ((offset == DWLP_DLGPROC) && (size == sizeof(LONG_PTR)) && wndPtr->dlgInfo)
2399 retvalue = (LONG_PTR)WINPROC_GetProc( (WNDPROC)retvalue, unicode );
2400 WIN_ReleasePtr( wndPtr );
2401 return retvalue;
2404 switch(offset)
2406 case GWLP_USERDATA: retvalue = wndPtr->userdata; break;
2407 case GWL_STYLE: retvalue = wndPtr->dwStyle; break;
2408 case GWL_EXSTYLE: retvalue = wndPtr->dwExStyle; break;
2409 case GWLP_ID: retvalue = wndPtr->wIDmenu; break;
2410 case GWLP_HINSTANCE: retvalue = (ULONG_PTR)wndPtr->hInstance; break;
2411 case GWLP_WNDPROC:
2412 /* This looks like a hack only for the edit control (see tests). This makes these controls
2413 * more tolerant to A/W mismatches. The lack of W->A->W conversion for such a mismatch suggests
2414 * that the hack is in GetWindowLongPtr[AW], not in winprocs.
2416 if (wndPtr->winproc == BUILTIN_WINPROC(WINPROC_EDIT) && (!unicode != !(wndPtr->flags & WIN_ISUNICODE)))
2417 retvalue = (ULONG_PTR)wndPtr->winproc;
2418 else
2419 retvalue = (ULONG_PTR)WINPROC_GetProc( wndPtr->winproc, unicode );
2420 break;
2421 default:
2422 WARN("Unknown offset %d\n", offset );
2423 SetLastError( ERROR_INVALID_INDEX );
2424 break;
2426 WIN_ReleasePtr(wndPtr);
2427 return retvalue;
2431 /**********************************************************************
2432 * WIN_SetWindowLong
2434 * Helper function for SetWindowLong().
2436 * 0 is the failure code. However, in the case of failure SetLastError
2437 * must be set to distinguish between a 0 return value and a failure.
2439 LONG_PTR WIN_SetWindowLong( HWND hwnd, INT offset, UINT size, LONG_PTR newval, BOOL unicode )
2441 STYLESTRUCT style;
2442 BOOL ok, made_visible = FALSE;
2443 LONG_PTR retval = 0;
2444 WND *wndPtr;
2446 TRACE( "%p %d %lx %c\n", hwnd, offset, newval, unicode ? 'W' : 'A' );
2448 if (is_broadcast(hwnd))
2450 SetLastError( ERROR_INVALID_PARAMETER );
2451 return FALSE;
2454 if (!(wndPtr = WIN_GetPtr( hwnd )))
2456 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2457 return 0;
2459 if (wndPtr == WND_DESKTOP)
2461 /* can't change anything on the desktop window */
2462 SetLastError( ERROR_ACCESS_DENIED );
2463 return 0;
2465 if (wndPtr == WND_OTHER_PROCESS)
2467 if (offset == GWLP_WNDPROC)
2469 SetLastError( ERROR_ACCESS_DENIED );
2470 return 0;
2472 if (offset > 32767 || offset < -32767)
2474 SetLastError( ERROR_INVALID_INDEX );
2475 return 0;
2477 return SendMessageW( hwnd, WM_WINE_SETWINDOWLONG, MAKEWPARAM( offset, size ), newval );
2480 /* first some special cases */
2481 switch( offset )
2483 case GWL_STYLE:
2484 style.styleOld = wndPtr->dwStyle;
2485 style.styleNew = newval;
2486 WIN_ReleasePtr( wndPtr );
2487 SendMessageW( hwnd, WM_STYLECHANGING, GWL_STYLE, (LPARAM)&style );
2488 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
2489 newval = style.styleNew;
2490 /* WS_CLIPSIBLINGS can't be reset on top-level windows */
2491 if (wndPtr->parent == GetDesktopWindow()) newval |= WS_CLIPSIBLINGS;
2492 /* WS_MINIMIZE can't be reset */
2493 if (wndPtr->dwStyle & WS_MINIMIZE) newval |= WS_MINIMIZE;
2494 /* FIXME: changing WS_DLGFRAME | WS_THICKFRAME is supposed to change
2495 WS_EX_WINDOWEDGE too */
2496 break;
2497 case GWL_EXSTYLE:
2498 style.styleOld = wndPtr->dwExStyle;
2499 style.styleNew = newval;
2500 WIN_ReleasePtr( wndPtr );
2501 SendMessageW( hwnd, WM_STYLECHANGING, GWL_EXSTYLE, (LPARAM)&style );
2502 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
2503 /* WS_EX_TOPMOST can only be changed through SetWindowPos */
2504 newval = (style.styleNew & ~WS_EX_TOPMOST) | (wndPtr->dwExStyle & WS_EX_TOPMOST);
2505 /* WS_EX_WINDOWEDGE depends on some other styles */
2506 if (newval & WS_EX_DLGMODALFRAME)
2507 newval |= WS_EX_WINDOWEDGE;
2508 else if (!(newval & WS_EX_STATICEDGE) && (wndPtr->dwStyle & (WS_DLGFRAME | WS_THICKFRAME)))
2509 newval |= WS_EX_WINDOWEDGE;
2510 else
2511 newval &= ~WS_EX_WINDOWEDGE;
2512 break;
2513 case GWLP_HWNDPARENT:
2514 if (wndPtr->parent == GetDesktopWindow())
2516 WIN_ReleasePtr( wndPtr );
2517 return (ULONG_PTR)WIN_SetOwner( hwnd, (HWND)newval );
2519 else
2521 WIN_ReleasePtr( wndPtr );
2522 return (ULONG_PTR)SetParent( hwnd, (HWND)newval );
2524 case GWLP_WNDPROC:
2526 WNDPROC proc;
2527 UINT old_flags = wndPtr->flags;
2528 retval = WIN_GetWindowLong( hwnd, offset, size, unicode );
2529 proc = WINPROC_AllocProc( (WNDPROC)newval, unicode );
2530 if (proc) wndPtr->winproc = proc;
2531 if (WINPROC_IsUnicode( proc, unicode )) wndPtr->flags |= WIN_ISUNICODE;
2532 else wndPtr->flags &= ~WIN_ISUNICODE;
2533 if (!((old_flags ^ wndPtr->flags) & WIN_ISUNICODE))
2535 WIN_ReleasePtr( wndPtr );
2536 return retval;
2538 /* update is_unicode flag on the server side */
2539 break;
2541 case GWLP_ID:
2542 case GWLP_HINSTANCE:
2543 case GWLP_USERDATA:
2544 break;
2545 case DWLP_DLGPROC:
2546 if ((wndPtr->cbWndExtra - sizeof(LONG_PTR) >= DWLP_DLGPROC) &&
2547 (size == sizeof(LONG_PTR)) && wndPtr->dlgInfo)
2549 WNDPROC *ptr = (WNDPROC *)((char *)wndPtr->wExtra + DWLP_DLGPROC);
2550 retval = (ULONG_PTR)WINPROC_GetProc( *ptr, unicode );
2551 *ptr = WINPROC_AllocProc( (WNDPROC)newval, unicode );
2552 WIN_ReleasePtr( wndPtr );
2553 return retval;
2555 /* fall through */
2556 default:
2557 if (offset < 0 || offset > (int)(wndPtr->cbWndExtra - size))
2559 WARN("Invalid offset %d\n", offset );
2560 WIN_ReleasePtr( wndPtr );
2561 SetLastError( ERROR_INVALID_INDEX );
2562 return 0;
2564 else if (get_win_data( (char *)wndPtr->wExtra + offset, size ) == newval)
2566 /* already set to the same value */
2567 WIN_ReleasePtr( wndPtr );
2568 return newval;
2570 break;
2573 SERVER_START_REQ( set_window_info )
2575 req->handle = wine_server_user_handle( hwnd );
2576 req->extra_offset = -1;
2577 switch(offset)
2579 case GWL_STYLE:
2580 req->flags = SET_WIN_STYLE;
2581 req->style = newval;
2582 break;
2583 case GWL_EXSTYLE:
2584 req->flags = SET_WIN_EXSTYLE;
2585 req->ex_style = newval;
2586 break;
2587 case GWLP_ID:
2588 req->flags = SET_WIN_ID;
2589 req->id = newval;
2590 break;
2591 case GWLP_HINSTANCE:
2592 req->flags = SET_WIN_INSTANCE;
2593 req->instance = wine_server_client_ptr( (void *)newval );
2594 break;
2595 case GWLP_WNDPROC:
2596 req->flags = SET_WIN_UNICODE;
2597 req->is_unicode = (wndPtr->flags & WIN_ISUNICODE) != 0;
2598 break;
2599 case GWLP_USERDATA:
2600 req->flags = SET_WIN_USERDATA;
2601 req->user_data = newval;
2602 break;
2603 default:
2604 req->flags = SET_WIN_EXTRA;
2605 req->extra_offset = offset;
2606 req->extra_size = size;
2607 set_win_data( &req->extra_value, newval, size );
2609 if ((ok = !wine_server_call_err( req )))
2611 switch(offset)
2613 case GWL_STYLE:
2614 wndPtr->dwStyle = newval;
2615 retval = reply->old_style;
2616 break;
2617 case GWL_EXSTYLE:
2618 wndPtr->dwExStyle = newval;
2619 retval = reply->old_ex_style;
2620 break;
2621 case GWLP_ID:
2622 wndPtr->wIDmenu = newval;
2623 retval = reply->old_id;
2624 break;
2625 case GWLP_HINSTANCE:
2626 wndPtr->hInstance = (HINSTANCE)newval;
2627 retval = (ULONG_PTR)wine_server_get_ptr( reply->old_instance );
2628 break;
2629 case GWLP_WNDPROC:
2630 break;
2631 case GWLP_USERDATA:
2632 wndPtr->userdata = newval;
2633 retval = reply->old_user_data;
2634 break;
2635 default:
2636 retval = get_win_data( (char *)wndPtr->wExtra + offset, size );
2637 set_win_data( (char *)wndPtr->wExtra + offset, newval, size );
2638 break;
2642 SERVER_END_REQ;
2644 if ((offset == GWL_STYLE && ((style.styleOld ^ style.styleNew) & WS_VISIBLE)) ||
2645 (offset == GWL_EXSTYLE && ((style.styleOld ^ style.styleNew) & WS_EX_LAYERED)))
2647 made_visible = (wndPtr->dwStyle & WS_VISIBLE) != 0;
2648 invalidate_dce( wndPtr, NULL );
2650 WIN_ReleasePtr( wndPtr );
2652 if (!ok) return 0;
2654 if (offset == GWL_STYLE || offset == GWL_EXSTYLE)
2656 style.styleOld = retval;
2657 style.styleNew = newval;
2658 USER_Driver->pSetWindowStyle( hwnd, offset, &style );
2659 if (made_visible) update_window_state( hwnd );
2660 SendMessageW( hwnd, WM_STYLECHANGED, offset, (LPARAM)&style );
2663 return retval;
2667 /**********************************************************************
2668 * GetWindowWord (USER32.@)
2670 WORD WINAPI GetWindowWord( HWND hwnd, INT offset )
2672 switch(offset)
2674 case GWLP_ID:
2675 case GWLP_HINSTANCE:
2676 case GWLP_HWNDPARENT:
2677 break;
2678 default:
2679 if (offset < 0)
2681 WARN("Invalid offset %d\n", offset );
2682 SetLastError( ERROR_INVALID_INDEX );
2683 return 0;
2685 break;
2687 return WIN_GetWindowLong( hwnd, offset, sizeof(WORD), FALSE );
2691 /**********************************************************************
2692 * GetWindowLongA (USER32.@)
2694 LONG WINAPI GetWindowLongA( HWND hwnd, INT offset )
2696 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG), FALSE );
2700 /**********************************************************************
2701 * GetWindowLongW (USER32.@)
2703 LONG WINAPI GetWindowLongW( HWND hwnd, INT offset )
2705 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG), TRUE );
2709 /**********************************************************************
2710 * SetWindowWord (USER32.@)
2712 WORD WINAPI SetWindowWord( HWND hwnd, INT offset, WORD newval )
2714 switch(offset)
2716 case GWLP_ID:
2717 case GWLP_HINSTANCE:
2718 case GWLP_HWNDPARENT:
2719 break;
2720 default:
2721 if (offset < 0)
2723 WARN("Invalid offset %d\n", offset );
2724 SetLastError( ERROR_INVALID_INDEX );
2725 return 0;
2727 break;
2729 return WIN_SetWindowLong( hwnd, offset, sizeof(WORD), newval, FALSE );
2733 /**********************************************************************
2734 * SetWindowLongA (USER32.@)
2736 * See SetWindowLongW.
2738 LONG WINAPI DECLSPEC_HOTPATCH SetWindowLongA( HWND hwnd, INT offset, LONG newval )
2740 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG), newval, FALSE );
2744 /**********************************************************************
2745 * SetWindowLongW (USER32.@) Set window attribute
2747 * SetWindowLong() alters one of a window's attributes or sets a 32-bit (long)
2748 * value in a window's extra memory.
2750 * The _hwnd_ parameter specifies the handle to a window that
2751 * has extra memory. The _newval_ parameter contains the new
2752 * attribute or extra memory value. If positive, the _offset_
2753 * parameter is the byte-addressed location in the window's extra
2754 * memory to set. If negative, _offset_ specifies the window
2755 * attribute to set, and should be one of the following values:
2757 * GWL_EXSTYLE The window's extended window style
2759 * GWL_STYLE The window's window style.
2761 * GWLP_WNDPROC Pointer to the window's window procedure.
2763 * GWLP_HINSTANCE The window's application instance handle.
2765 * GWLP_ID The window's identifier.
2767 * GWLP_USERDATA The window's user-specified data.
2769 * If the window is a dialog box, the _offset_ parameter can be one of
2770 * the following values:
2772 * DWLP_DLGPROC The address of the window's dialog box procedure.
2774 * DWLP_MSGRESULT The return value of a message
2775 * that the dialog box procedure processed.
2777 * DWLP_USER Application specific information.
2779 * RETURNS
2781 * If successful, returns the previous value located at _offset_. Otherwise,
2782 * returns 0.
2784 * NOTES
2786 * Extra memory for a window class is specified by a nonzero cbWndExtra
2787 * parameter of the WNDCLASS structure passed to RegisterClass() at the
2788 * time of class creation.
2790 * Using GWL_WNDPROC to set a new window procedure effectively creates
2791 * a window subclass. Use CallWindowProc() in the new windows procedure
2792 * to pass messages to the superclass's window procedure.
2794 * The user data is reserved for use by the application which created
2795 * the window.
2797 * Do not use GWL_STYLE to change the window's WS_DISABLED style;
2798 * instead, call the EnableWindow() function to change the window's
2799 * disabled state.
2801 * Do not use GWL_HWNDPARENT to reset the window's parent, use
2802 * SetParent() instead.
2804 * Win95:
2805 * When offset is GWL_STYLE and the calling app's ver is 4.0,
2806 * it sends WM_STYLECHANGING before changing the settings
2807 * and WM_STYLECHANGED afterwards.
2808 * App ver 4.0 can't use SetWindowLong to change WS_EX_TOPMOST.
2810 LONG WINAPI DECLSPEC_HOTPATCH SetWindowLongW(
2811 HWND hwnd, /* [in] window to alter */
2812 INT offset, /* [in] offset, in bytes, of location to alter */
2813 LONG newval /* [in] new value of location */
2815 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG), newval, TRUE );
2819 /*******************************************************************
2820 * GetWindowTextA (USER32.@)
2822 INT WINAPI GetWindowTextA( HWND hwnd, LPSTR lpString, INT nMaxCount )
2824 WCHAR *buffer;
2826 if (!lpString || nMaxCount <= 0) return 0;
2828 if (WIN_IsCurrentProcess( hwnd ))
2830 lpString[0] = 0;
2831 return (INT)SendMessageA( hwnd, WM_GETTEXT, nMaxCount, (LPARAM)lpString );
2834 /* when window belongs to other process, don't send a message */
2835 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, nMaxCount * sizeof(WCHAR) ))) return 0;
2836 get_server_window_text( hwnd, buffer, nMaxCount );
2837 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, lpString, nMaxCount, NULL, NULL ))
2838 lpString[nMaxCount-1] = 0;
2839 HeapFree( GetProcessHeap(), 0, buffer );
2840 return strlen(lpString);
2844 /*******************************************************************
2845 * InternalGetWindowText (USER32.@)
2847 INT WINAPI InternalGetWindowText(HWND hwnd,LPWSTR lpString,INT nMaxCount )
2849 WND *win;
2851 if (nMaxCount <= 0) return 0;
2852 if (!(win = WIN_GetPtr( hwnd ))) return 0;
2853 if (win == WND_DESKTOP) lpString[0] = 0;
2854 else if (win != WND_OTHER_PROCESS)
2856 if (win->text) lstrcpynW( lpString, win->text, nMaxCount );
2857 else lpString[0] = 0;
2858 WIN_ReleasePtr( win );
2860 else
2862 get_server_window_text( hwnd, lpString, nMaxCount );
2864 return strlenW(lpString);
2868 /*******************************************************************
2869 * GetWindowTextW (USER32.@)
2871 INT WINAPI GetWindowTextW( HWND hwnd, LPWSTR lpString, INT nMaxCount )
2873 if (!lpString || nMaxCount <= 0) return 0;
2875 if (WIN_IsCurrentProcess( hwnd ))
2877 lpString[0] = 0;
2878 return (INT)SendMessageW( hwnd, WM_GETTEXT, nMaxCount, (LPARAM)lpString );
2881 /* when window belongs to other process, don't send a message */
2882 get_server_window_text( hwnd, lpString, nMaxCount );
2883 return strlenW(lpString);
2887 /*******************************************************************
2888 * SetWindowTextA (USER32.@)
2889 * SetWindowText (USER32.@)
2891 BOOL WINAPI DECLSPEC_HOTPATCH SetWindowTextA( HWND hwnd, LPCSTR lpString )
2893 if (is_broadcast(hwnd))
2895 SetLastError( ERROR_INVALID_PARAMETER );
2896 return FALSE;
2898 if (!WIN_IsCurrentProcess( hwnd ))
2899 WARN( "setting text %s of other process window %p should not use SendMessage\n",
2900 debugstr_a(lpString), hwnd );
2901 return (BOOL)SendMessageA( hwnd, WM_SETTEXT, 0, (LPARAM)lpString );
2905 /*******************************************************************
2906 * SetWindowTextW (USER32.@)
2908 BOOL WINAPI DECLSPEC_HOTPATCH SetWindowTextW( HWND hwnd, LPCWSTR lpString )
2910 if (is_broadcast(hwnd))
2912 SetLastError( ERROR_INVALID_PARAMETER );
2913 return FALSE;
2915 if (!WIN_IsCurrentProcess( hwnd ))
2916 WARN( "setting text %s of other process window %p should not use SendMessage\n",
2917 debugstr_w(lpString), hwnd );
2918 return (BOOL)SendMessageW( hwnd, WM_SETTEXT, 0, (LPARAM)lpString );
2922 /*******************************************************************
2923 * GetWindowTextLengthA (USER32.@)
2925 INT WINAPI GetWindowTextLengthA( HWND hwnd )
2927 CPINFO info;
2929 if (WIN_IsCurrentProcess( hwnd )) return SendMessageA( hwnd, WM_GETTEXTLENGTH, 0, 0 );
2931 /* when window belongs to other process, don't send a message */
2932 GetCPInfo( CP_ACP, &info );
2933 return get_server_window_text( hwnd, NULL, 0 ) * info.MaxCharSize;
2936 /*******************************************************************
2937 * GetWindowTextLengthW (USER32.@)
2939 INT WINAPI GetWindowTextLengthW( HWND hwnd )
2941 if (WIN_IsCurrentProcess( hwnd )) return SendMessageW( hwnd, WM_GETTEXTLENGTH, 0, 0 );
2943 /* when window belongs to other process, don't send a message */
2944 return get_server_window_text( hwnd, NULL, 0 );
2948 /*******************************************************************
2949 * IsWindow (USER32.@)
2951 BOOL WINAPI IsWindow( HWND hwnd )
2953 WND *ptr;
2954 BOOL ret;
2956 if (!(ptr = WIN_GetPtr( hwnd ))) return FALSE;
2957 if (ptr == WND_DESKTOP) return TRUE;
2959 if (ptr != WND_OTHER_PROCESS)
2961 WIN_ReleasePtr( ptr );
2962 return TRUE;
2965 /* check other processes */
2966 SERVER_START_REQ( get_window_info )
2968 req->handle = wine_server_user_handle( hwnd );
2969 ret = !wine_server_call_err( req );
2971 SERVER_END_REQ;
2972 return ret;
2976 /***********************************************************************
2977 * GetWindowThreadProcessId (USER32.@)
2979 DWORD WINAPI GetWindowThreadProcessId( HWND hwnd, LPDWORD process )
2981 WND *ptr;
2982 DWORD tid = 0;
2984 if (!(ptr = WIN_GetPtr( hwnd )))
2986 SetLastError( ERROR_INVALID_WINDOW_HANDLE);
2987 return 0;
2990 if (ptr != WND_OTHER_PROCESS && ptr != WND_DESKTOP)
2992 /* got a valid window */
2993 tid = ptr->tid;
2994 if (process) *process = GetCurrentProcessId();
2995 WIN_ReleasePtr( ptr );
2996 return tid;
2999 /* check other processes */
3000 SERVER_START_REQ( get_window_info )
3002 req->handle = wine_server_user_handle( hwnd );
3003 if (!wine_server_call_err( req ))
3005 tid = (DWORD)reply->tid;
3006 if (process) *process = (DWORD)reply->pid;
3009 SERVER_END_REQ;
3010 return tid;
3014 /*****************************************************************
3015 * GetParent (USER32.@)
3017 HWND WINAPI GetParent( HWND hwnd )
3019 WND *wndPtr;
3020 HWND retvalue = 0;
3022 if (!(wndPtr = WIN_GetPtr( hwnd )))
3024 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3025 return 0;
3027 if (wndPtr == WND_DESKTOP) return 0;
3028 if (wndPtr == WND_OTHER_PROCESS)
3030 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
3031 if (style & (WS_POPUP | WS_CHILD))
3033 SERVER_START_REQ( get_window_tree )
3035 req->handle = wine_server_user_handle( hwnd );
3036 if (!wine_server_call_err( req ))
3038 if (style & WS_POPUP) retvalue = wine_server_ptr_handle( reply->owner );
3039 else if (style & WS_CHILD) retvalue = wine_server_ptr_handle( reply->parent );
3042 SERVER_END_REQ;
3045 else
3047 if (wndPtr->dwStyle & WS_POPUP) retvalue = wndPtr->owner;
3048 else if (wndPtr->dwStyle & WS_CHILD) retvalue = wndPtr->parent;
3049 WIN_ReleasePtr( wndPtr );
3051 return retvalue;
3055 /*****************************************************************
3056 * GetAncestor (USER32.@)
3058 HWND WINAPI GetAncestor( HWND hwnd, UINT type )
3060 WND *win;
3061 HWND *list, ret = 0;
3063 switch(type)
3065 case GA_PARENT:
3066 if (!(win = WIN_GetPtr( hwnd )))
3068 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3069 return 0;
3071 if (win == WND_DESKTOP) return 0;
3072 if (win != WND_OTHER_PROCESS)
3074 ret = win->parent;
3075 WIN_ReleasePtr( win );
3077 else /* need to query the server */
3079 SERVER_START_REQ( get_window_tree )
3081 req->handle = wine_server_user_handle( hwnd );
3082 if (!wine_server_call_err( req )) ret = wine_server_ptr_handle( reply->parent );
3084 SERVER_END_REQ;
3086 break;
3088 case GA_ROOT:
3089 if (!(list = list_window_parents( hwnd ))) return 0;
3091 if (!list[0] || !list[1]) ret = WIN_GetFullHandle( hwnd ); /* top-level window */
3092 else
3094 int count = 2;
3095 while (list[count]) count++;
3096 ret = list[count - 2]; /* get the one before the desktop */
3098 HeapFree( GetProcessHeap(), 0, list );
3099 break;
3101 case GA_ROOTOWNER:
3102 if (is_desktop_window( hwnd )) return 0;
3103 ret = WIN_GetFullHandle( hwnd );
3104 for (;;)
3106 HWND parent = GetParent( ret );
3107 if (!parent) break;
3108 ret = parent;
3110 break;
3112 return ret;
3116 /*****************************************************************
3117 * SetParent (USER32.@)
3119 HWND WINAPI SetParent( HWND hwnd, HWND parent )
3121 WINDOWPOS winpos;
3122 HWND full_handle;
3123 HWND old_parent = 0;
3124 BOOL was_visible;
3125 WND *wndPtr;
3126 BOOL ret;
3127 DPI_AWARENESS_CONTEXT context;
3128 RECT window_rect, old_screen_rect, new_screen_rect;
3130 TRACE("(%p %p)\n", hwnd, parent);
3132 if (is_broadcast(hwnd) || is_broadcast(parent))
3134 SetLastError(ERROR_INVALID_PARAMETER);
3135 return 0;
3138 if (!parent) parent = GetDesktopWindow();
3139 else if (parent == HWND_MESSAGE) parent = get_hwnd_message_parent();
3140 else parent = WIN_GetFullHandle( parent );
3142 if (!IsWindow( parent ))
3144 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3145 return 0;
3148 /* Some applications try to set a child as a parent */
3149 if (IsChild(hwnd, parent))
3151 SetLastError( ERROR_INVALID_PARAMETER );
3152 return 0;
3155 if (!(full_handle = WIN_IsCurrentThread( hwnd )))
3156 return (HWND)SendMessageW( hwnd, WM_WINE_SETPARENT, (WPARAM)parent, 0 );
3158 if (full_handle == parent)
3160 SetLastError( ERROR_INVALID_PARAMETER );
3161 return 0;
3164 /* Windows hides the window first, then shows it again
3165 * including the WM_SHOWWINDOW messages and all */
3166 was_visible = ShowWindow( hwnd, SW_HIDE );
3168 wndPtr = WIN_GetPtr( hwnd );
3169 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return 0;
3171 context = SetThreadDpiAwarenessContext( GetWindowDpiAwarenessContext( hwnd ));
3172 WIN_GetRectangles( hwnd, COORDS_PARENT, &window_rect, NULL );
3173 SetThreadDpiAwarenessContext( DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE );
3174 WIN_GetRectangles( hwnd, COORDS_SCREEN, &old_screen_rect, NULL );
3175 SetThreadDpiAwarenessContext( context );
3177 SERVER_START_REQ( set_parent )
3179 req->handle = wine_server_user_handle( hwnd );
3180 req->parent = wine_server_user_handle( parent );
3181 if ((ret = !wine_server_call( req )))
3183 old_parent = wine_server_ptr_handle( reply->old_parent );
3184 wndPtr->parent = parent = wine_server_ptr_handle( reply->full_parent );
3185 wndPtr->dpi = reply->dpi;
3186 wndPtr->dpi_awareness = reply->awareness;
3190 SERVER_END_REQ;
3191 WIN_ReleasePtr( wndPtr );
3192 if (!ret) return 0;
3194 context = SetThreadDpiAwarenessContext( DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE );
3195 WIN_GetRectangles( hwnd, COORDS_SCREEN, &new_screen_rect, NULL );
3196 SetThreadDpiAwarenessContext( GetWindowDpiAwarenessContext( hwnd ));
3198 USER_Driver->pSetParent( full_handle, parent, old_parent );
3200 winpos.hwnd = hwnd;
3201 winpos.hwndInsertAfter = HWND_TOP;
3202 winpos.x = window_rect.left;
3203 winpos.y = window_rect.top;
3204 winpos.cx = 0;
3205 winpos.cy = 0;
3206 winpos.flags = SWP_NOSIZE;
3208 USER_SetWindowPos( &winpos, new_screen_rect.left - old_screen_rect.left,
3209 new_screen_rect.top - old_screen_rect.top );
3211 if (was_visible) ShowWindow( hwnd, SW_SHOW );
3213 SetThreadDpiAwarenessContext( context );
3214 return old_parent;
3218 /*******************************************************************
3219 * IsChild (USER32.@)
3221 BOOL WINAPI IsChild( HWND parent, HWND child )
3223 HWND *list;
3224 int i;
3225 BOOL ret = FALSE;
3227 if (!(GetWindowLongW( child, GWL_STYLE ) & WS_CHILD)) return FALSE;
3228 if (!(list = list_window_parents( child ))) return FALSE;
3229 parent = WIN_GetFullHandle( parent );
3230 for (i = 0; list[i]; i++)
3232 if (list[i] == parent)
3234 ret = list[i] && list[i+1];
3235 break;
3237 if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_CHILD)) break;
3239 HeapFree( GetProcessHeap(), 0, list );
3240 return ret;
3244 /***********************************************************************
3245 * IsWindowVisible (USER32.@)
3247 BOOL WINAPI IsWindowVisible( HWND hwnd )
3249 HWND *list;
3250 BOOL retval = TRUE;
3251 int i;
3253 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)) return FALSE;
3254 if (!(list = list_window_parents( hwnd ))) return TRUE;
3255 if (list[0])
3257 for (i = 0; list[i+1]; i++)
3258 if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_VISIBLE)) break;
3259 retval = !list[i+1] && (list[i] == GetDesktopWindow()); /* top message window isn't visible */
3261 HeapFree( GetProcessHeap(), 0, list );
3262 return retval;
3266 /***********************************************************************
3267 * WIN_IsWindowDrawable
3269 * hwnd is drawable when it is visible, all parents are not
3270 * minimized, and it is itself not minimized unless we are
3271 * trying to draw its default class icon.
3273 BOOL WIN_IsWindowDrawable( HWND hwnd, BOOL icon )
3275 HWND *list;
3276 BOOL retval = TRUE;
3277 int i;
3278 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
3280 if (!(style & WS_VISIBLE)) return FALSE;
3281 if ((style & WS_MINIMIZE) && icon && GetClassLongPtrW( hwnd, GCLP_HICON )) return FALSE;
3283 if (!(list = list_window_parents( hwnd ))) return TRUE;
3284 if (list[0])
3286 for (i = 0; list[i+1]; i++)
3287 if ((GetWindowLongW( list[i], GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != WS_VISIBLE)
3288 break;
3289 retval = !list[i+1] && (list[i] == GetDesktopWindow()); /* top message window isn't visible */
3291 HeapFree( GetProcessHeap(), 0, list );
3292 return retval;
3296 /*******************************************************************
3297 * GetTopWindow (USER32.@)
3299 HWND WINAPI GetTopWindow( HWND hwnd )
3301 if (!hwnd) hwnd = GetDesktopWindow();
3302 return GetWindow( hwnd, GW_CHILD );
3306 /*******************************************************************
3307 * GetWindow (USER32.@)
3309 HWND WINAPI GetWindow( HWND hwnd, UINT rel )
3311 HWND retval = 0;
3313 if (rel == GW_OWNER) /* this one may be available locally */
3315 WND *wndPtr = WIN_GetPtr( hwnd );
3316 if (!wndPtr)
3318 SetLastError( ERROR_INVALID_HANDLE );
3319 return 0;
3321 if (wndPtr == WND_DESKTOP) return 0;
3322 if (wndPtr != WND_OTHER_PROCESS)
3324 retval = wndPtr->owner;
3325 WIN_ReleasePtr( wndPtr );
3326 return retval;
3328 /* else fall through to server call */
3331 SERVER_START_REQ( get_window_tree )
3333 req->handle = wine_server_user_handle( hwnd );
3334 if (!wine_server_call_err( req ))
3336 switch(rel)
3338 case GW_HWNDFIRST:
3339 retval = wine_server_ptr_handle( reply->first_sibling );
3340 break;
3341 case GW_HWNDLAST:
3342 retval = wine_server_ptr_handle( reply->last_sibling );
3343 break;
3344 case GW_HWNDNEXT:
3345 retval = wine_server_ptr_handle( reply->next_sibling );
3346 break;
3347 case GW_HWNDPREV:
3348 retval = wine_server_ptr_handle( reply->prev_sibling );
3349 break;
3350 case GW_OWNER:
3351 retval = wine_server_ptr_handle( reply->owner );
3352 break;
3353 case GW_CHILD:
3354 retval = wine_server_ptr_handle( reply->first_child );
3355 break;
3359 SERVER_END_REQ;
3360 return retval;
3364 /*******************************************************************
3365 * ShowOwnedPopups (USER32.@)
3367 BOOL WINAPI ShowOwnedPopups( HWND owner, BOOL fShow )
3369 int count = 0;
3370 HWND *win_array = WIN_ListChildren( GetDesktopWindow() );
3372 if (!win_array) return TRUE;
3374 while (win_array[count]) count++;
3375 while (--count >= 0)
3377 if (GetWindow( win_array[count], GW_OWNER ) != owner) continue;
3378 if (fShow)
3380 if (win_get_flags( win_array[count] ) & WIN_NEEDS_SHOW_OWNEDPOPUP)
3381 /* In Windows, ShowOwnedPopups(TRUE) generates
3382 * WM_SHOWWINDOW messages with SW_PARENTOPENING,
3383 * regardless of the state of the owner
3385 SendMessageW(win_array[count], WM_SHOWWINDOW, SW_SHOWNORMAL, SW_PARENTOPENING);
3387 else
3389 if (GetWindowLongW( win_array[count], GWL_STYLE ) & WS_VISIBLE)
3390 /* In Windows, ShowOwnedPopups(FALSE) generates
3391 * WM_SHOWWINDOW messages with SW_PARENTCLOSING,
3392 * regardless of the state of the owner
3394 SendMessageW(win_array[count], WM_SHOWWINDOW, SW_HIDE, SW_PARENTCLOSING);
3397 HeapFree( GetProcessHeap(), 0, win_array );
3398 return TRUE;
3402 /*******************************************************************
3403 * GetLastActivePopup (USER32.@)
3405 HWND WINAPI GetLastActivePopup( HWND hwnd )
3407 HWND retval = hwnd;
3409 SERVER_START_REQ( get_window_info )
3411 req->handle = wine_server_user_handle( hwnd );
3412 if (!wine_server_call_err( req )) retval = wine_server_ptr_handle( reply->last_active );
3414 SERVER_END_REQ;
3415 return retval;
3419 /*******************************************************************
3420 * WIN_ListChildren
3422 * Build an array of the children of a given window. The array must be
3423 * freed with HeapFree. Returns NULL when no windows are found.
3425 HWND *WIN_ListChildren( HWND hwnd )
3427 if (!hwnd)
3429 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3430 return NULL;
3432 return list_window_children( 0, hwnd, NULL, 0 );
3436 /*******************************************************************
3437 * EnumWindows (USER32.@)
3439 BOOL WINAPI EnumWindows( WNDENUMPROC lpEnumFunc, LPARAM lParam )
3441 HWND *list;
3442 BOOL ret = TRUE;
3443 int i;
3445 USER_CheckNotLock();
3447 /* We have to build a list of all windows first, to avoid */
3448 /* unpleasant side-effects, for instance if the callback */
3449 /* function changes the Z-order of the windows. */
3451 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return TRUE;
3453 /* Now call the callback function for every window */
3455 for (i = 0; list[i]; i++)
3457 /* Make sure that the window still exists */
3458 if (!IsWindow( list[i] )) continue;
3459 if (!(ret = lpEnumFunc( list[i], lParam ))) break;
3461 HeapFree( GetProcessHeap(), 0, list );
3462 return ret;
3466 /**********************************************************************
3467 * EnumThreadWindows (USER32.@)
3469 BOOL WINAPI EnumThreadWindows( DWORD id, WNDENUMPROC func, LPARAM lParam )
3471 HWND *list;
3472 int i;
3473 BOOL ret = TRUE;
3475 USER_CheckNotLock();
3477 if (!(list = list_window_children( 0, GetDesktopWindow(), NULL, id ))) return TRUE;
3479 /* Now call the callback function for every window */
3481 for (i = 0; list[i]; i++)
3482 if (!(ret = func( list[i], lParam ))) break;
3483 HeapFree( GetProcessHeap(), 0, list );
3484 return ret;
3488 /***********************************************************************
3489 * EnumDesktopWindows (USER32.@)
3491 BOOL WINAPI EnumDesktopWindows( HDESK desktop, WNDENUMPROC func, LPARAM lparam )
3493 HWND *list;
3494 int i;
3496 USER_CheckNotLock();
3498 if (!(list = list_window_children( desktop, 0, NULL, 0 ))) return TRUE;
3500 for (i = 0; list[i]; i++)
3501 if (!func( list[i], lparam )) break;
3502 HeapFree( GetProcessHeap(), 0, list );
3503 return TRUE;
3507 #ifdef __i386__
3508 /* Some apps pass a non-stdcall proc to EnumChildWindows,
3509 * so we need a small assembly wrapper to call the proc.
3511 extern LRESULT enum_callback_wrapper( WNDENUMPROC proc, HWND hwnd, LPARAM lparam );
3512 __ASM_GLOBAL_FUNC( enum_callback_wrapper,
3513 "pushl %ebp\n\t"
3514 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
3515 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
3516 "movl %esp,%ebp\n\t"
3517 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
3518 "pushl 16(%ebp)\n\t"
3519 "pushl 12(%ebp)\n\t"
3520 "call *8(%ebp)\n\t"
3521 "leave\n\t"
3522 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
3523 __ASM_CFI(".cfi_same_value %ebp\n\t")
3524 "ret" )
3525 #else
3526 static inline LRESULT enum_callback_wrapper( WNDENUMPROC proc, HWND hwnd, LPARAM lparam )
3528 return proc( hwnd, lparam );
3530 #endif /* __i386__ */
3532 /**********************************************************************
3533 * WIN_EnumChildWindows
3535 * Helper function for EnumChildWindows().
3537 static BOOL WIN_EnumChildWindows( HWND *list, WNDENUMPROC func, LPARAM lParam )
3539 HWND *childList;
3540 BOOL ret = FALSE;
3542 for ( ; *list; list++)
3544 /* Make sure that the window still exists */
3545 if (!IsWindow( *list )) continue;
3546 /* Build children list first */
3547 childList = WIN_ListChildren( *list );
3549 ret = enum_callback_wrapper( func, *list, lParam );
3551 if (childList)
3553 if (ret) ret = WIN_EnumChildWindows( childList, func, lParam );
3554 HeapFree( GetProcessHeap(), 0, childList );
3556 if (!ret) return FALSE;
3558 return TRUE;
3562 /**********************************************************************
3563 * EnumChildWindows (USER32.@)
3565 BOOL WINAPI EnumChildWindows( HWND parent, WNDENUMPROC func, LPARAM lParam )
3567 HWND *list;
3568 BOOL ret;
3570 USER_CheckNotLock();
3572 if (!(list = WIN_ListChildren( parent ))) return FALSE;
3573 ret = WIN_EnumChildWindows( list, func, lParam );
3574 HeapFree( GetProcessHeap(), 0, list );
3575 return ret;
3579 /*******************************************************************
3580 * AnyPopup (USER32.@)
3582 BOOL WINAPI AnyPopup(void)
3584 int i;
3585 BOOL retvalue;
3586 HWND *list = WIN_ListChildren( GetDesktopWindow() );
3588 if (!list) return FALSE;
3589 for (i = 0; list[i]; i++)
3591 if (IsWindowVisible( list[i] ) && GetWindow( list[i], GW_OWNER )) break;
3593 retvalue = (list[i] != 0);
3594 HeapFree( GetProcessHeap(), 0, list );
3595 return retvalue;
3599 /*******************************************************************
3600 * FlashWindow (USER32.@)
3602 BOOL WINAPI FlashWindow( HWND hWnd, BOOL bInvert )
3604 FLASHWINFO finfo;
3606 finfo.cbSize = sizeof(FLASHWINFO);
3607 finfo.dwFlags = bInvert ? FLASHW_ALL : FLASHW_STOP;
3608 finfo.uCount = 1;
3609 finfo.dwTimeout = 0;
3610 finfo.hwnd = hWnd;
3611 return FlashWindowEx( &finfo );
3614 /*******************************************************************
3615 * FlashWindowEx (USER32.@)
3617 BOOL WINAPI FlashWindowEx( PFLASHWINFO pfinfo )
3619 WND *wndPtr;
3621 TRACE( "%p\n", pfinfo );
3623 if (!pfinfo)
3625 SetLastError( ERROR_NOACCESS );
3626 return FALSE;
3629 if (!pfinfo->hwnd || pfinfo->cbSize != sizeof(FLASHWINFO) || !IsWindow( pfinfo->hwnd ))
3631 SetLastError( ERROR_INVALID_PARAMETER );
3632 return FALSE;
3634 FIXME( "%p - semi-stub\n", pfinfo );
3636 if (IsIconic( pfinfo->hwnd ))
3638 RedrawWindow( pfinfo->hwnd, 0, 0, RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW | RDW_FRAME );
3640 wndPtr = WIN_GetPtr( pfinfo->hwnd );
3641 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
3642 if (pfinfo->dwFlags && !(wndPtr->flags & WIN_NCACTIVATED))
3644 wndPtr->flags |= WIN_NCACTIVATED;
3646 else
3648 wndPtr->flags &= ~WIN_NCACTIVATED;
3650 WIN_ReleasePtr( wndPtr );
3651 USER_Driver->pFlashWindowEx( pfinfo );
3652 return TRUE;
3654 else
3656 WPARAM wparam;
3657 HWND hwnd = pfinfo->hwnd;
3659 wndPtr = WIN_GetPtr( hwnd );
3660 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
3661 hwnd = wndPtr->obj.handle; /* make it a full handle */
3663 if (pfinfo->dwFlags) wparam = !(wndPtr->flags & WIN_NCACTIVATED);
3664 else wparam = (hwnd == GetForegroundWindow());
3666 WIN_ReleasePtr( wndPtr );
3667 SendMessageW( hwnd, WM_NCACTIVATE, wparam, 0 );
3668 USER_Driver->pFlashWindowEx( pfinfo );
3669 return wparam;
3673 /*******************************************************************
3674 * GetWindowContextHelpId (USER32.@)
3676 DWORD WINAPI GetWindowContextHelpId( HWND hwnd )
3678 DWORD retval;
3679 WND *wnd = WIN_GetPtr( hwnd );
3680 if (!wnd || wnd == WND_DESKTOP) return 0;
3681 if (wnd == WND_OTHER_PROCESS)
3683 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
3684 return 0;
3686 retval = wnd->helpContext;
3687 WIN_ReleasePtr( wnd );
3688 return retval;
3692 /*******************************************************************
3693 * SetWindowContextHelpId (USER32.@)
3695 BOOL WINAPI SetWindowContextHelpId( HWND hwnd, DWORD id )
3697 WND *wnd = WIN_GetPtr( hwnd );
3698 if (!wnd || wnd == WND_DESKTOP) return FALSE;
3699 if (wnd == WND_OTHER_PROCESS)
3701 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
3702 return FALSE;
3704 wnd->helpContext = id;
3705 WIN_ReleasePtr( wnd );
3706 return TRUE;
3710 /*******************************************************************
3711 * DragDetect (USER32.@)
3713 BOOL WINAPI DragDetect( HWND hWnd, POINT pt )
3715 MSG msg;
3716 RECT rect;
3717 WORD wDragWidth = GetSystemMetrics(SM_CXDRAG);
3718 WORD wDragHeight= GetSystemMetrics(SM_CYDRAG);
3720 SetRect(&rect, pt.x - wDragWidth, pt.y - wDragHeight, pt.x + wDragWidth, pt.y + wDragHeight);
3722 SetCapture(hWnd);
3724 while(1)
3726 while (PeekMessageW( &msg, 0, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE ))
3728 if( msg.message == WM_LBUTTONUP )
3730 ReleaseCapture();
3731 return FALSE;
3733 if( msg.message == WM_MOUSEMOVE )
3735 POINT tmp;
3736 tmp.x = (short)LOWORD(msg.lParam);
3737 tmp.y = (short)HIWORD(msg.lParam);
3738 if( !PtInRect( &rect, tmp ))
3740 ReleaseCapture();
3741 return TRUE;
3745 WaitMessage();
3747 return FALSE;
3750 /******************************************************************************
3751 * GetWindowModuleFileNameA (USER32.@)
3753 UINT WINAPI GetWindowModuleFileNameA( HWND hwnd, LPSTR module, UINT size )
3755 WND *win;
3756 HINSTANCE hinst;
3758 TRACE( "%p, %p, %u\n", hwnd, module, size );
3760 win = WIN_GetPtr( hwnd );
3761 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP)
3763 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3764 return 0;
3766 hinst = win->hInstance;
3767 WIN_ReleasePtr( win );
3769 return GetModuleFileNameA( hinst, module, size );
3772 /******************************************************************************
3773 * GetWindowModuleFileNameW (USER32.@)
3775 UINT WINAPI GetWindowModuleFileNameW( HWND hwnd, LPWSTR module, UINT size )
3777 WND *win;
3778 HINSTANCE hinst;
3780 TRACE( "%p, %p, %u\n", hwnd, module, size );
3782 win = WIN_GetPtr( hwnd );
3783 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP)
3785 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3786 return 0;
3788 hinst = win->hInstance;
3789 WIN_ReleasePtr( win );
3791 return GetModuleFileNameW( hinst, module, size );
3794 /******************************************************************************
3795 * GetWindowInfo (USER32.@)
3797 * Note: tests show that Windows doesn't check cbSize of the structure.
3799 BOOL WINAPI DECLSPEC_HOTPATCH GetWindowInfo( HWND hwnd, PWINDOWINFO pwi)
3801 if (!pwi) return FALSE;
3802 if (!WIN_GetRectangles( hwnd, COORDS_SCREEN, &pwi->rcWindow, &pwi->rcClient )) return FALSE;
3804 pwi->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
3805 pwi->dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
3806 pwi->dwWindowStatus = ((GetActiveWindow() == hwnd) ? WS_ACTIVECAPTION : 0);
3808 pwi->cxWindowBorders = pwi->rcClient.left - pwi->rcWindow.left;
3809 pwi->cyWindowBorders = pwi->rcWindow.bottom - pwi->rcClient.bottom;
3811 pwi->atomWindowType = GetClassLongW( hwnd, GCW_ATOM );
3812 pwi->wCreatorVersion = 0x0400;
3814 return TRUE;
3817 /******************************************************************************
3818 * SwitchDesktop (USER32.@)
3820 * NOTES: Sets the current input or interactive desktop.
3822 BOOL WINAPI SwitchDesktop( HDESK hDesktop)
3824 FIXME("(hwnd %p) stub!\n", hDesktop);
3825 return TRUE;
3829 /***********************************************************************
3830 * __wine_set_pixel_format
3832 BOOL CDECL __wine_set_pixel_format( HWND hwnd, int format )
3834 WND *win = WIN_GetPtr( hwnd );
3836 if (!win || win == WND_DESKTOP || win == WND_OTHER_PROCESS)
3838 WARN( "setting format %d on win %p not supported\n", format, hwnd );
3839 return FALSE;
3841 win->pixel_format = format;
3842 WIN_ReleasePtr( win );
3844 update_window_state( hwnd );
3845 return TRUE;
3849 /*****************************************************************************
3850 * SetLayeredWindowAttributes (USER32.@)
3852 BOOL WINAPI SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
3854 BOOL ret;
3856 TRACE("(%p,%08x,%d,%x)\n", hwnd, key, alpha, flags);
3858 SERVER_START_REQ( set_window_layered_info )
3860 req->handle = wine_server_user_handle( hwnd );
3861 req->color_key = key;
3862 req->alpha = alpha;
3863 req->flags = flags;
3864 ret = !wine_server_call_err( req );
3866 SERVER_END_REQ;
3868 if (ret)
3870 USER_Driver->pSetLayeredWindowAttributes( hwnd, key, alpha, flags );
3871 update_window_state( hwnd );
3874 return ret;
3878 /*****************************************************************************
3879 * GetLayeredWindowAttributes (USER32.@)
3881 BOOL WINAPI GetLayeredWindowAttributes( HWND hwnd, COLORREF *key, BYTE *alpha, DWORD *flags )
3883 BOOL ret;
3885 SERVER_START_REQ( get_window_layered_info )
3887 req->handle = wine_server_user_handle( hwnd );
3888 if ((ret = !wine_server_call_err( req )))
3890 if (key) *key = reply->color_key;
3891 if (alpha) *alpha = reply->alpha;
3892 if (flags) *flags = reply->flags;
3895 SERVER_END_REQ;
3897 return ret;
3901 /*****************************************************************************
3902 * UpdateLayeredWindowIndirect (USER32.@)
3904 BOOL WINAPI UpdateLayeredWindowIndirect( HWND hwnd, const UPDATELAYEREDWINDOWINFO *info )
3906 DWORD flags = SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW;
3907 RECT window_rect, client_rect;
3908 SIZE offset;
3910 if (!info ||
3911 info->cbSize != sizeof(*info) ||
3912 info->dwFlags & ~(ULW_COLORKEY | ULW_ALPHA | ULW_OPAQUE | ULW_EX_NORESIZE) ||
3913 !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED) ||
3914 GetLayeredWindowAttributes( hwnd, NULL, NULL, NULL ))
3916 SetLastError( ERROR_INVALID_PARAMETER );
3917 return FALSE;
3920 WIN_GetRectangles( hwnd, COORDS_PARENT, &window_rect, &client_rect );
3922 if (info->pptDst)
3924 offset.cx = info->pptDst->x - window_rect.left;
3925 offset.cy = info->pptDst->y - window_rect.top;
3926 OffsetRect( &client_rect, offset.cx, offset.cy );
3927 OffsetRect( &window_rect, offset.cx, offset.cy );
3928 flags &= ~SWP_NOMOVE;
3930 if (info->psize)
3932 offset.cx = info->psize->cx - (window_rect.right - window_rect.left);
3933 offset.cy = info->psize->cy - (window_rect.bottom - window_rect.top);
3934 if (info->psize->cx <= 0 || info->psize->cy <= 0)
3936 SetLastError( ERROR_INVALID_PARAMETER );
3937 return FALSE;
3939 if ((info->dwFlags & ULW_EX_NORESIZE) && (offset.cx || offset.cy))
3941 SetLastError( ERROR_INCORRECT_SIZE );
3942 return FALSE;
3944 client_rect.right += offset.cx;
3945 client_rect.bottom += offset.cy;
3946 window_rect.right += offset.cx;
3947 window_rect.bottom += offset.cy;
3948 flags &= ~SWP_NOSIZE;
3951 TRACE( "window %p win %s client %s\n", hwnd,
3952 wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(&client_rect) );
3954 if (!USER_Driver->pUpdateLayeredWindow( hwnd, info, &window_rect )) return FALSE;
3956 set_window_pos( hwnd, 0, flags, &window_rect, &client_rect, NULL );
3957 return TRUE;
3961 /*****************************************************************************
3962 * UpdateLayeredWindow (USER32.@)
3964 BOOL WINAPI UpdateLayeredWindow( HWND hwnd, HDC hdcDst, POINT *pptDst, SIZE *psize,
3965 HDC hdcSrc, POINT *pptSrc, COLORREF crKey, BLENDFUNCTION *pblend,
3966 DWORD flags)
3968 UPDATELAYEREDWINDOWINFO info;
3970 if (flags & ULW_EX_NORESIZE) /* only valid for UpdateLayeredWindowIndirect */
3972 SetLastError( ERROR_INVALID_PARAMETER );
3973 return FALSE;
3975 info.cbSize = sizeof(info);
3976 info.hdcDst = hdcDst;
3977 info.pptDst = pptDst;
3978 info.psize = psize;
3979 info.hdcSrc = hdcSrc;
3980 info.pptSrc = pptSrc;
3981 info.crKey = crKey;
3982 info.pblend = pblend;
3983 info.dwFlags = flags;
3984 info.prcDirty = NULL;
3985 return UpdateLayeredWindowIndirect( hwnd, &info );
3989 /******************************************************************************
3990 * GetProcessDefaultLayout [USER32.@]
3992 * Gets the default layout for parentless windows.
3994 BOOL WINAPI GetProcessDefaultLayout( DWORD *layout )
3996 if (!layout)
3998 SetLastError( ERROR_NOACCESS );
3999 return FALSE;
4001 if (process_layout == ~0u)
4003 static const WCHAR translationW[] = { '\\','V','a','r','F','i','l','e','I','n','f','o',
4004 '\\','T','r','a','n','s','l','a','t','i','o','n', 0 };
4005 static const WCHAR filedescW[] = { '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
4006 '\\','%','0','4','x','%','0','4','x',
4007 '\\','F','i','l','e','D','e','s','c','r','i','p','t','i','o','n',0 };
4008 WCHAR *str, buffer[MAX_PATH];
4009 DWORD i, len, version_layout = 0;
4010 DWORD user_lang = GetUserDefaultLangID();
4011 DWORD *languages;
4012 void *data = NULL;
4014 GetModuleFileNameW( 0, buffer, MAX_PATH );
4015 if (!(len = GetFileVersionInfoSizeW( buffer, NULL ))) goto done;
4016 if (!(data = HeapAlloc( GetProcessHeap(), 0, len ))) goto done;
4017 if (!GetFileVersionInfoW( buffer, 0, len, data )) goto done;
4018 if (!VerQueryValueW( data, translationW, (void **)&languages, &len ) || !len) goto done;
4020 len /= sizeof(DWORD);
4021 for (i = 0; i < len; i++) if (LOWORD(languages[i]) == user_lang) break;
4022 if (i == len) /* try neutral language */
4023 for (i = 0; i < len; i++)
4024 if (LOWORD(languages[i]) == MAKELANGID( PRIMARYLANGID(user_lang), SUBLANG_NEUTRAL )) break;
4025 if (i == len) i = 0; /* default to the first one */
4027 sprintfW( buffer, filedescW, LOWORD(languages[i]), HIWORD(languages[i]) );
4028 if (!VerQueryValueW( data, buffer, (void **)&str, &len )) goto done;
4029 TRACE( "found description %s\n", debugstr_w( str ));
4030 if (str[0] == 0x200e && str[1] == 0x200e) version_layout = LAYOUT_RTL;
4032 done:
4033 HeapFree( GetProcessHeap(), 0, data );
4034 process_layout = version_layout;
4036 *layout = process_layout;
4037 return TRUE;
4041 /******************************************************************************
4042 * SetProcessDefaultLayout [USER32.@]
4044 * Sets the default layout for parentless windows.
4046 BOOL WINAPI SetProcessDefaultLayout( DWORD layout )
4048 process_layout = layout;
4049 return TRUE;
4053 /* 64bit versions */
4055 #ifdef GetWindowLongPtrW
4056 #undef GetWindowLongPtrW
4057 #endif
4059 #ifdef GetWindowLongPtrA
4060 #undef GetWindowLongPtrA
4061 #endif
4063 #ifdef SetWindowLongPtrW
4064 #undef SetWindowLongPtrW
4065 #endif
4067 #ifdef SetWindowLongPtrA
4068 #undef SetWindowLongPtrA
4069 #endif
4071 /*****************************************************************************
4072 * GetWindowLongPtrW (USER32.@)
4074 LONG_PTR WINAPI GetWindowLongPtrW( HWND hwnd, INT offset )
4076 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG_PTR), TRUE );
4079 /*****************************************************************************
4080 * GetWindowLongPtrA (USER32.@)
4082 LONG_PTR WINAPI GetWindowLongPtrA( HWND hwnd, INT offset )
4084 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG_PTR), FALSE );
4087 /*****************************************************************************
4088 * SetWindowLongPtrW (USER32.@)
4090 LONG_PTR WINAPI SetWindowLongPtrW( HWND hwnd, INT offset, LONG_PTR newval )
4092 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG_PTR), newval, TRUE );
4095 /*****************************************************************************
4096 * SetWindowLongPtrA (USER32.@)
4098 LONG_PTR WINAPI SetWindowLongPtrA( HWND hwnd, INT offset, LONG_PTR newval )
4100 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG_PTR), newval, FALSE );
4103 /*****************************************************************************
4104 * RegisterTouchWindow (USER32.@)
4106 BOOL WINAPI RegisterTouchWindow(HWND hwnd, ULONG flags)
4108 FIXME("(%p %08x): stub\n", hwnd, flags);
4109 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
4110 return FALSE;
4113 /*****************************************************************************
4114 * UnregisterTouchWindow (USER32.@)
4116 BOOL WINAPI UnregisterTouchWindow(HWND hwnd)
4118 FIXME("(%p): stub\n", hwnd);
4119 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
4120 return FALSE;
4123 /*****************************************************************************
4124 * CloseTouchInputHandle (USER32.@)
4126 BOOL WINAPI CloseTouchInputHandle(HTOUCHINPUT handle)
4128 FIXME("(%p): stub\n", handle);
4129 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
4130 return FALSE;
4133 /*****************************************************************************
4134 * GetTouchInputInfo (USER32.@)
4136 BOOL WINAPI GetTouchInputInfo(HTOUCHINPUT handle, UINT count, TOUCHINPUT *ptr, int size)
4138 FIXME("(%p %u %p %u): stub\n", handle, count, ptr, size);
4139 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
4140 return FALSE;
4143 /*****************************************************************************
4144 * GetGestureInfo (USER32.@)
4146 BOOL WINAPI GetGestureInfo(HGESTUREINFO handle, PGESTUREINFO ptr)
4148 FIXME("(%p %p): stub\n", handle, ptr);
4149 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
4150 return FALSE;
4153 /*****************************************************************************
4154 * GetWindowDisplayAffinity (USER32.@)
4156 BOOL WINAPI GetWindowDisplayAffinity(HWND hwnd, DWORD *affinity)
4158 FIXME("(%p, %p): stub\n", hwnd, affinity);
4160 if (!hwnd || !affinity)
4162 SetLastError(hwnd ? ERROR_NOACCESS : ERROR_INVALID_WINDOW_HANDLE);
4163 return FALSE;
4166 *affinity = WDA_NONE;
4167 return TRUE;
4170 /*****************************************************************************
4171 * SetWindowDisplayAffinity (USER32.@)
4173 BOOL WINAPI SetWindowDisplayAffinity(HWND hwnd, DWORD affinity)
4175 FIXME("(%p, %u): stub\n", hwnd, affinity);
4177 if (!hwnd)
4179 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
4180 return FALSE;
4183 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
4184 return FALSE;
4187 /**********************************************************************
4188 * SetWindowCompositionAttribute (USER32.@)
4190 BOOL WINAPI SetWindowCompositionAttribute(HWND hwnd, void *data)
4192 FIXME("(%p, %p): stub\n", hwnd, data);
4193 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
4194 return FALSE;