user32: Delay registration of the builtin classes until the first window is created.
[wine.git] / dlls / user32 / win.c
blobd506fee7c1f6a8427723def735e89d2e687237f1
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>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winver.h"
31 #include "wine/server.h"
32 #include "wine/unicode.h"
33 #include "win.h"
34 #include "user_private.h"
35 #include "controls.h"
36 #include "winerror.h"
37 #include "wine/gdi_driver.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(win);
42 #define NB_USER_HANDLES ((LAST_USER_HANDLE - FIRST_USER_HANDLE + 1) >> 1)
43 #define USER_HANDLE_TO_INDEX(hwnd) ((LOWORD(hwnd) - FIRST_USER_HANDLE) >> 1)
45 static DWORD process_layout = ~0u;
47 static struct list window_surfaces = LIST_INIT( window_surfaces );
49 static CRITICAL_SECTION surfaces_section;
50 static CRITICAL_SECTION_DEBUG critsect_debug =
52 0, 0, &surfaces_section,
53 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
54 0, 0, { (DWORD_PTR)(__FILE__ ": surfaces_section") }
56 static CRITICAL_SECTION surfaces_section = { &critsect_debug, -1, 0, 0, 0, 0 };
58 /**********************************************************************/
60 /* helper for Get/SetWindowLong */
61 static inline LONG_PTR get_win_data( const void *ptr, UINT size )
63 if (size == sizeof(WORD))
65 WORD ret;
66 memcpy( &ret, ptr, sizeof(ret) );
67 return ret;
69 else if (size == sizeof(DWORD))
71 DWORD ret;
72 memcpy( &ret, ptr, sizeof(ret) );
73 return ret;
75 else
77 LONG_PTR ret;
78 memcpy( &ret, ptr, sizeof(ret) );
79 return ret;
83 /* helper for Get/SetWindowLong */
84 static inline void set_win_data( void *ptr, LONG_PTR val, UINT size )
86 if (size == sizeof(WORD))
88 WORD newval = val;
89 memcpy( ptr, &newval, sizeof(newval) );
91 else if (size == sizeof(DWORD))
93 DWORD newval = val;
94 memcpy( ptr, &newval, sizeof(newval) );
96 else
98 memcpy( ptr, &val, sizeof(val) );
103 static void *user_handles[NB_USER_HANDLES];
105 /***********************************************************************
106 * alloc_user_handle
108 HANDLE alloc_user_handle( struct user_object *ptr, enum user_obj_type type )
110 HANDLE handle = 0;
112 SERVER_START_REQ( alloc_user_handle )
114 if (!wine_server_call_err( req )) handle = wine_server_ptr_handle( reply->handle );
116 SERVER_END_REQ;
118 if (handle)
120 UINT index = USER_HANDLE_TO_INDEX( handle );
122 assert( index < NB_USER_HANDLES );
123 ptr->handle = handle;
124 ptr->type = type;
125 InterlockedExchangePointer( &user_handles[index], ptr );
127 return handle;
131 /***********************************************************************
132 * get_user_handle_ptr
134 void *get_user_handle_ptr( HANDLE handle, enum user_obj_type type )
136 struct user_object *ptr;
137 WORD index = USER_HANDLE_TO_INDEX( handle );
139 if (index >= NB_USER_HANDLES) return NULL;
141 USER_Lock();
142 if ((ptr = user_handles[index]))
144 if (ptr->type == type &&
145 ((UINT)(UINT_PTR)ptr->handle == (UINT)(UINT_PTR)handle ||
146 !HIWORD(handle) || HIWORD(handle) == 0xffff))
147 return ptr;
148 ptr = NULL;
150 else ptr = OBJ_OTHER_PROCESS;
151 USER_Unlock();
152 return ptr;
156 /***********************************************************************
157 * release_user_handle_ptr
159 void release_user_handle_ptr( void *ptr )
161 assert( ptr && ptr != OBJ_OTHER_PROCESS );
162 USER_Unlock();
166 /***********************************************************************
167 * free_user_handle
169 void *free_user_handle( HANDLE handle, enum user_obj_type type )
171 struct user_object *ptr;
172 WORD index = USER_HANDLE_TO_INDEX( handle );
174 if ((ptr = get_user_handle_ptr( handle, type )) && ptr != OBJ_OTHER_PROCESS)
176 SERVER_START_REQ( free_user_handle )
178 req->handle = wine_server_user_handle( handle );
179 if (wine_server_call( req )) ptr = NULL;
180 else InterlockedCompareExchangePointer( &user_handles[index], NULL, ptr );
182 SERVER_END_REQ;
183 USER_Unlock();
185 return ptr;
189 /***********************************************************************
190 * create_window_handle
192 * Create a window handle with the server.
194 static WND *create_window_handle( HWND parent, HWND owner, LPCWSTR name,
195 HINSTANCE instance, BOOL unicode )
197 WORD index;
198 WND *win;
199 HWND handle = 0, full_parent = 0, full_owner = 0;
200 struct tagCLASS *class = NULL;
201 int extra_bytes = 0;
203 SERVER_START_REQ( create_window )
205 req->parent = wine_server_user_handle( parent );
206 req->owner = wine_server_user_handle( owner );
207 req->instance = wine_server_client_ptr( instance );
208 if (!(req->atom = get_int_atom_value( name )) && name)
209 wine_server_add_data( req, name, strlenW(name)*sizeof(WCHAR) );
210 if (!wine_server_call_err( req ))
212 handle = wine_server_ptr_handle( reply->handle );
213 full_parent = wine_server_ptr_handle( reply->parent );
214 full_owner = wine_server_ptr_handle( reply->owner );
215 extra_bytes = reply->extra;
216 class = wine_server_get_ptr( reply->class_ptr );
219 SERVER_END_REQ;
221 if (!handle)
223 WARN( "error %d creating window\n", GetLastError() );
224 return NULL;
227 if (!(win = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
228 sizeof(WND) + extra_bytes - sizeof(win->wExtra) )))
230 SERVER_START_REQ( destroy_window )
232 req->handle = wine_server_user_handle( handle );
233 wine_server_call( req );
235 SERVER_END_REQ;
236 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
237 return NULL;
240 if (!parent) /* if parent is 0 we don't have a desktop window yet */
242 struct user_thread_info *thread_info = get_user_thread_info();
244 if (name == (LPCWSTR)DESKTOP_CLASS_ATOM)
246 if (!thread_info->top_window) thread_info->top_window = full_parent ? full_parent : handle;
247 else assert( full_parent == thread_info->top_window );
248 if (full_parent && !USER_Driver->pCreateDesktopWindow( thread_info->top_window ))
249 ERR( "failed to create desktop window\n" );
251 else /* HWND_MESSAGE parent */
253 if (!thread_info->msg_window && !full_parent) thread_info->msg_window = handle;
257 USER_Lock();
259 index = USER_HANDLE_TO_INDEX(handle);
260 assert( index < NB_USER_HANDLES );
261 win->obj.handle = handle;
262 win->obj.type = USER_WINDOW;
263 win->parent = full_parent;
264 win->owner = full_owner;
265 win->class = class;
266 win->winproc = get_class_winproc( class );
267 win->cbWndExtra = extra_bytes;
268 InterlockedExchangePointer( &user_handles[index], win );
269 if (WINPROC_IsUnicode( win->winproc, unicode )) win->flags |= WIN_ISUNICODE;
270 return win;
274 /***********************************************************************
275 * free_window_handle
277 * Free a window handle.
279 static void free_window_handle( HWND hwnd )
281 struct user_object *ptr;
282 WORD index = USER_HANDLE_TO_INDEX(hwnd);
284 if ((ptr = get_user_handle_ptr( hwnd, USER_WINDOW )) && ptr != OBJ_OTHER_PROCESS)
286 SERVER_START_REQ( destroy_window )
288 req->handle = wine_server_user_handle( hwnd );
289 if (wine_server_call_err( req )) ptr = NULL;
290 else InterlockedCompareExchangePointer( &user_handles[index], NULL, ptr );
292 SERVER_END_REQ;
293 USER_Unlock();
294 HeapFree( GetProcessHeap(), 0, ptr );
299 /*******************************************************************
300 * list_window_children
302 * Build an array of the children of a given window. The array must be
303 * freed with HeapFree. Returns NULL when no windows are found.
305 static HWND *list_window_children( HDESK desktop, HWND hwnd, LPCWSTR class, DWORD tid )
307 HWND *list;
308 int i, size = 128;
309 ATOM atom = get_int_atom_value( class );
311 /* empty class is not the same as NULL class */
312 if (!atom && class && !class[0]) return NULL;
314 for (;;)
316 int count = 0;
318 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
320 SERVER_START_REQ( get_window_children )
322 req->desktop = wine_server_obj_handle( desktop );
323 req->parent = wine_server_user_handle( hwnd );
324 req->tid = tid;
325 req->atom = atom;
326 if (!atom && class) wine_server_add_data( req, class, strlenW(class)*sizeof(WCHAR) );
327 wine_server_set_reply( req, list, (size-1) * sizeof(user_handle_t) );
328 if (!wine_server_call( req )) count = reply->count;
330 SERVER_END_REQ;
331 if (count && count < size)
333 /* start from the end since HWND is potentially larger than user_handle_t */
334 for (i = count - 1; i >= 0; i--)
335 list[i] = wine_server_ptr_handle( ((user_handle_t *)list)[i] );
336 list[count] = 0;
337 return list;
339 HeapFree( GetProcessHeap(), 0, list );
340 if (!count) break;
341 size = count + 1; /* restart with a large enough buffer */
343 return NULL;
347 /*******************************************************************
348 * list_window_parents
350 * Build an array of all parents of a given window, starting with
351 * the immediate parent. The array must be freed with HeapFree.
353 static HWND *list_window_parents( HWND hwnd )
355 WND *win;
356 HWND current, *list;
357 int i, pos = 0, size = 16, count = 0;
359 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) return NULL;
361 current = hwnd;
362 for (;;)
364 if (!(win = WIN_GetPtr( current ))) goto empty;
365 if (win == WND_OTHER_PROCESS) break; /* need to do it the hard way */
366 if (win == WND_DESKTOP)
368 if (!pos) goto empty;
369 list[pos] = 0;
370 return list;
372 list[pos] = current = win->parent;
373 WIN_ReleasePtr( win );
374 if (!current) return list;
375 if (++pos == size - 1)
377 /* need to grow the list */
378 HWND *new_list = HeapReAlloc( GetProcessHeap(), 0, list, (size+16) * sizeof(HWND) );
379 if (!new_list) goto empty;
380 list = new_list;
381 size += 16;
385 /* at least one parent belongs to another process, have to query the server */
387 for (;;)
389 count = 0;
390 SERVER_START_REQ( get_window_parents )
392 req->handle = wine_server_user_handle( hwnd );
393 wine_server_set_reply( req, list, (size-1) * sizeof(user_handle_t) );
394 if (!wine_server_call( req )) count = reply->count;
396 SERVER_END_REQ;
397 if (!count) goto empty;
398 if (size > count)
400 /* start from the end since HWND is potentially larger than user_handle_t */
401 for (i = count - 1; i >= 0; i--)
402 list[i] = wine_server_ptr_handle( ((user_handle_t *)list)[i] );
403 list[count] = 0;
404 return list;
406 HeapFree( GetProcessHeap(), 0, list );
407 size = count + 1;
408 if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) return NULL;
411 empty:
412 HeapFree( GetProcessHeap(), 0, list );
413 return NULL;
417 /*******************************************************************
418 * send_parent_notify
420 static void send_parent_notify( HWND hwnd, UINT msg )
422 if ((GetWindowLongW( hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD &&
423 !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY))
425 HWND parent = GetParent(hwnd);
426 if (parent && parent != GetDesktopWindow())
427 SendMessageW( parent, WM_PARENTNOTIFY,
428 MAKEWPARAM( msg, GetWindowLongPtrW( hwnd, GWLP_ID )), (LPARAM)hwnd );
433 /*******************************************************************
434 * update_window_state
436 * Trigger an update of the window's driver state and surface.
438 static void update_window_state( HWND hwnd )
440 RECT window_rect, client_rect;
442 WIN_GetRectangles( hwnd, COORDS_PARENT, &window_rect, &client_rect );
443 set_window_pos( hwnd, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE |
444 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW, &window_rect, &client_rect, NULL );
448 /*******************************************************************
449 * get_server_window_text
451 * Retrieve the window text from the server.
453 static void get_server_window_text( HWND hwnd, LPWSTR text, INT count )
455 size_t len = 0;
457 SERVER_START_REQ( get_window_text )
459 req->handle = wine_server_user_handle( hwnd );
460 wine_server_set_reply( req, text, (count - 1) * sizeof(WCHAR) );
461 if (!wine_server_call_err( req )) len = wine_server_reply_size(reply);
463 SERVER_END_REQ;
464 text[len / sizeof(WCHAR)] = 0;
468 /*******************************************************************
469 * get_hwnd_message_parent
471 * Return the parent for HWND_MESSAGE windows.
473 HWND get_hwnd_message_parent(void)
475 struct user_thread_info *thread_info = get_user_thread_info();
477 if (!thread_info->msg_window) GetDesktopWindow(); /* trigger creation */
478 return thread_info->msg_window;
482 /*******************************************************************
483 * is_desktop_window
485 * Check if window is the desktop or the HWND_MESSAGE top parent.
487 BOOL is_desktop_window( HWND hwnd )
489 struct user_thread_info *thread_info = get_user_thread_info();
491 if (!hwnd) return FALSE;
492 if (hwnd == thread_info->top_window) return TRUE;
493 if (hwnd == thread_info->msg_window) return TRUE;
495 if (!HIWORD(hwnd) || HIWORD(hwnd) == 0xffff)
497 if (LOWORD(thread_info->top_window) == LOWORD(hwnd)) return TRUE;
498 if (LOWORD(thread_info->msg_window) == LOWORD(hwnd)) return TRUE;
500 return FALSE;
504 /*******************************************************************
505 * Dummy window surface for windows that shouldn't get painted.
508 static void dummy_surface_lock( struct window_surface *window_surface )
510 /* nothing to do */
513 static void dummy_surface_unlock( struct window_surface *window_surface )
515 /* nothing to do */
518 static void *dummy_surface_get_bitmap_info( struct window_surface *window_surface, BITMAPINFO *info )
520 static DWORD dummy_data;
522 info->bmiHeader.biSize = sizeof( info->bmiHeader );
523 info->bmiHeader.biWidth = dummy_surface.rect.right;
524 info->bmiHeader.biHeight = dummy_surface.rect.bottom;
525 info->bmiHeader.biPlanes = 1;
526 info->bmiHeader.biBitCount = 32;
527 info->bmiHeader.biCompression = BI_RGB;
528 info->bmiHeader.biSizeImage = 0;
529 info->bmiHeader.biXPelsPerMeter = 0;
530 info->bmiHeader.biYPelsPerMeter = 0;
531 info->bmiHeader.biClrUsed = 0;
532 info->bmiHeader.biClrImportant = 0;
533 return &dummy_data;
536 static RECT *dummy_surface_get_bounds( struct window_surface *window_surface )
538 static RECT dummy_bounds;
539 return &dummy_bounds;
542 static void dummy_surface_set_region( struct window_surface *window_surface, HRGN region )
544 /* nothing to do */
547 static void dummy_surface_flush( struct window_surface *window_surface )
549 /* nothing to do */
552 static void dummy_surface_destroy( struct window_surface *window_surface )
554 /* nothing to do */
557 static const struct window_surface_funcs dummy_surface_funcs =
559 dummy_surface_lock,
560 dummy_surface_unlock,
561 dummy_surface_get_bitmap_info,
562 dummy_surface_get_bounds,
563 dummy_surface_set_region,
564 dummy_surface_flush,
565 dummy_surface_destroy
568 struct window_surface dummy_surface = { &dummy_surface_funcs, { NULL, NULL }, 1, { 0, 0, 1, 1 } };
571 /*******************************************************************
572 * register_window_surface
574 * Register a window surface in the global list, possibly replacing another one.
576 void register_window_surface( struct window_surface *old, struct window_surface *new )
578 if (old == new) return;
579 EnterCriticalSection( &surfaces_section );
580 if (old && old != &dummy_surface) list_remove( &old->entry );
581 if (new && new != &dummy_surface) list_add_tail( &window_surfaces, &new->entry );
582 LeaveCriticalSection( &surfaces_section );
586 /*******************************************************************
587 * flush_window_surfaces
589 * Flush pending output from all window surfaces.
591 void flush_window_surfaces( BOOL idle )
593 static DWORD last_idle;
594 DWORD now;
595 struct window_surface *surface;
597 EnterCriticalSection( &surfaces_section );
598 now = GetTickCount();
599 if (idle) last_idle = now;
600 /* if not idle, we only flush if there's evidence that the app never goes idle */
601 else if ((int)(now - last_idle) < 1000) goto done;
603 LIST_FOR_EACH_ENTRY( surface, &window_surfaces, struct window_surface, entry )
604 surface->funcs->flush( surface );
605 done:
606 LeaveCriticalSection( &surfaces_section );
610 /***********************************************************************
611 * WIN_GetPtr
613 * Return a pointer to the WND structure if local to the process,
614 * or WND_OTHER_PROCESS if handle may be valid in other process.
615 * If ret value is a valid pointer, it must be released with WIN_ReleasePtr.
617 WND *WIN_GetPtr( HWND hwnd )
619 WND *ptr;
621 if ((ptr = get_user_handle_ptr( hwnd, USER_WINDOW )) == WND_OTHER_PROCESS)
623 if (is_desktop_window( hwnd )) ptr = WND_DESKTOP;
625 return ptr;
629 /***********************************************************************
630 * WIN_IsCurrentProcess
632 * Check whether a given window belongs to the current process (and return the full handle).
634 HWND WIN_IsCurrentProcess( HWND hwnd )
636 WND *ptr;
637 HWND ret;
639 if (!(ptr = WIN_GetPtr( hwnd )) || ptr == WND_OTHER_PROCESS || ptr == WND_DESKTOP) return 0;
640 ret = ptr->obj.handle;
641 WIN_ReleasePtr( ptr );
642 return ret;
646 /***********************************************************************
647 * WIN_IsCurrentThread
649 * Check whether a given window belongs to the current thread (and return the full handle).
651 HWND WIN_IsCurrentThread( HWND hwnd )
653 WND *ptr;
654 HWND ret = 0;
656 if (!(ptr = WIN_GetPtr( hwnd )) || ptr == WND_OTHER_PROCESS || ptr == WND_DESKTOP) return 0;
657 if (ptr->tid == GetCurrentThreadId()) ret = ptr->obj.handle;
658 WIN_ReleasePtr( ptr );
659 return ret;
663 /***********************************************************************
664 * WIN_GetFullHandle
666 * Convert a possibly truncated window handle to a full 32-bit handle.
668 HWND WIN_GetFullHandle( HWND hwnd )
670 WND *ptr;
672 if (!hwnd || (ULONG_PTR)hwnd >> 16) return hwnd;
673 if (LOWORD(hwnd) <= 1 || LOWORD(hwnd) == 0xffff) return hwnd;
674 /* do sign extension for -2 and -3 */
675 if (LOWORD(hwnd) >= (WORD)-3) return (HWND)(LONG_PTR)(INT16)LOWORD(hwnd);
677 if (!(ptr = WIN_GetPtr( hwnd ))) return hwnd;
679 if (ptr == WND_DESKTOP)
681 if (LOWORD(hwnd) == LOWORD(GetDesktopWindow())) return GetDesktopWindow();
682 else return get_hwnd_message_parent();
685 if (ptr != WND_OTHER_PROCESS)
687 hwnd = ptr->obj.handle;
688 WIN_ReleasePtr( ptr );
690 else /* may belong to another process */
692 SERVER_START_REQ( get_window_info )
694 req->handle = wine_server_user_handle( hwnd );
695 if (!wine_server_call_err( req )) hwnd = wine_server_ptr_handle( reply->full_handle );
697 SERVER_END_REQ;
699 return hwnd;
703 /***********************************************************************
704 * WIN_SetOwner
706 * Change the owner of a window.
708 HWND WIN_SetOwner( HWND hwnd, HWND owner )
710 WND *win = WIN_GetPtr( hwnd );
711 HWND ret = 0;
713 if (!win || win == WND_DESKTOP) return 0;
714 if (win == WND_OTHER_PROCESS)
716 if (IsWindow(hwnd)) ERR( "cannot set owner %p on other process window %p\n", owner, hwnd );
717 return 0;
719 SERVER_START_REQ( set_window_owner )
721 req->handle = wine_server_user_handle( hwnd );
722 req->owner = wine_server_user_handle( owner );
723 if (!wine_server_call( req ))
725 win->owner = wine_server_ptr_handle( reply->full_owner );
726 ret = wine_server_ptr_handle( reply->prev_owner );
729 SERVER_END_REQ;
730 WIN_ReleasePtr( win );
731 return ret;
735 /***********************************************************************
736 * WIN_SetStyle
738 * Change the style of a window.
740 ULONG WIN_SetStyle( HWND hwnd, ULONG set_bits, ULONG clear_bits )
742 BOOL ok, made_visible = FALSE;
743 STYLESTRUCT style;
744 WND *win = WIN_GetPtr( hwnd );
746 if (!win || win == WND_DESKTOP) return 0;
747 if (win == WND_OTHER_PROCESS)
749 if (IsWindow(hwnd))
750 ERR( "cannot set style %x/%x on other process window %p\n",
751 set_bits, clear_bits, hwnd );
752 return 0;
754 style.styleOld = win->dwStyle;
755 style.styleNew = (win->dwStyle | set_bits) & ~clear_bits;
756 if (style.styleNew == style.styleOld)
758 WIN_ReleasePtr( win );
759 return style.styleNew;
761 SERVER_START_REQ( set_window_info )
763 req->handle = wine_server_user_handle( hwnd );
764 req->flags = SET_WIN_STYLE;
765 req->style = style.styleNew;
766 req->extra_offset = -1;
767 if ((ok = !wine_server_call( req )))
769 style.styleOld = reply->old_style;
770 win->dwStyle = style.styleNew;
773 SERVER_END_REQ;
775 if (ok && ((style.styleOld ^ style.styleNew) & WS_VISIBLE))
777 /* Some apps try to make their window visible through WM_SETREDRAW.
778 * Only do that if the window was never explicitly hidden,
779 * because Steam messes with WM_SETREDRAW after hiding its windows. */
780 made_visible = !(win->flags & WIN_HIDDEN) && (style.styleNew & WS_VISIBLE);
781 invalidate_dce( win, NULL );
783 WIN_ReleasePtr( win );
785 if (!ok) return 0;
787 USER_Driver->pSetWindowStyle( hwnd, GWL_STYLE, &style );
788 if (made_visible) update_window_state( hwnd );
790 return style.styleOld;
794 /***********************************************************************
795 * WIN_GetRectangles
797 * Get the window and client rectangles.
799 BOOL WIN_GetRectangles( HWND hwnd, enum coords_relative relative, RECT *rectWindow, RECT *rectClient )
801 WND *win = WIN_GetPtr( hwnd );
802 BOOL ret = TRUE;
804 if (!win)
806 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
807 return FALSE;
809 if (win == WND_DESKTOP)
811 RECT rect;
812 rect.left = rect.top = 0;
813 if (hwnd == get_hwnd_message_parent())
815 rect.right = 100;
816 rect.bottom = 100;
818 else
820 rect.right = GetSystemMetrics(SM_CXSCREEN);
821 rect.bottom = GetSystemMetrics(SM_CYSCREEN);
823 if (rectWindow) *rectWindow = rect;
824 if (rectClient) *rectClient = rect;
825 return TRUE;
827 if (win != WND_OTHER_PROCESS)
829 RECT window_rect = win->rectWindow, client_rect = win->rectClient;
831 switch (relative)
833 case COORDS_CLIENT:
834 OffsetRect( &window_rect, -win->rectClient.left, -win->rectClient.top );
835 OffsetRect( &client_rect, -win->rectClient.left, -win->rectClient.top );
836 if (win->dwExStyle & WS_EX_LAYOUTRTL)
837 mirror_rect( &win->rectClient, &window_rect );
838 break;
839 case COORDS_WINDOW:
840 OffsetRect( &window_rect, -win->rectWindow.left, -win->rectWindow.top );
841 OffsetRect( &client_rect, -win->rectWindow.left, -win->rectWindow.top );
842 if (win->dwExStyle & WS_EX_LAYOUTRTL)
843 mirror_rect( &win->rectWindow, &client_rect );
844 break;
845 case COORDS_PARENT:
846 if (win->parent)
848 WND *parent = WIN_GetPtr( win->parent );
849 if (parent == WND_DESKTOP) break;
850 if (!parent || parent == WND_OTHER_PROCESS)
852 WIN_ReleasePtr( win );
853 goto other_process;
855 if (parent->flags & WIN_CHILDREN_MOVED)
857 WIN_ReleasePtr( parent );
858 WIN_ReleasePtr( win );
859 goto other_process;
861 if (parent->dwExStyle & WS_EX_LAYOUTRTL)
863 mirror_rect( &parent->rectClient, &window_rect );
864 mirror_rect( &parent->rectClient, &client_rect );
866 WIN_ReleasePtr( parent );
868 break;
869 case COORDS_SCREEN:
870 while (win->parent)
872 WND *parent = WIN_GetPtr( win->parent );
873 if (parent == WND_DESKTOP) break;
874 if (!parent || parent == WND_OTHER_PROCESS)
876 WIN_ReleasePtr( win );
877 goto other_process;
879 WIN_ReleasePtr( win );
880 if (parent->flags & WIN_CHILDREN_MOVED)
882 WIN_ReleasePtr( parent );
883 goto other_process;
885 win = parent;
886 if (win->parent)
888 OffsetRect( &window_rect, win->rectClient.left, win->rectClient.top );
889 OffsetRect( &client_rect, win->rectClient.left, win->rectClient.top );
892 break;
894 if (rectWindow) *rectWindow = window_rect;
895 if (rectClient) *rectClient = client_rect;
896 WIN_ReleasePtr( win );
897 return TRUE;
900 other_process:
901 SERVER_START_REQ( get_window_rectangles )
903 req->handle = wine_server_user_handle( hwnd );
904 req->relative = relative;
905 if ((ret = !wine_server_call_err( req )))
907 if (rectWindow)
909 rectWindow->left = reply->window.left;
910 rectWindow->top = reply->window.top;
911 rectWindow->right = reply->window.right;
912 rectWindow->bottom = reply->window.bottom;
914 if (rectClient)
916 rectClient->left = reply->client.left;
917 rectClient->top = reply->client.top;
918 rectClient->right = reply->client.right;
919 rectClient->bottom = reply->client.bottom;
923 SERVER_END_REQ;
924 return ret;
928 /***********************************************************************
929 * WIN_DestroyWindow
931 * Destroy storage associated to a window. "Internals" p.358
933 LRESULT WIN_DestroyWindow( HWND hwnd )
935 WND *wndPtr;
936 HWND *list;
937 HMENU menu = 0, sys_menu;
938 HWND icon_title;
939 struct window_surface *surface;
941 TRACE("%p\n", hwnd );
943 /* free child windows */
944 if ((list = WIN_ListChildren( hwnd )))
946 int i;
947 for (i = 0; list[i]; i++)
949 if (WIN_IsCurrentThread( list[i] )) WIN_DestroyWindow( list[i] );
950 else SendMessageW( list[i], WM_WINE_DESTROYWINDOW, 0, 0 );
952 HeapFree( GetProcessHeap(), 0, list );
955 /* Unlink now so we won't bother with the children later on */
956 SERVER_START_REQ( set_parent )
958 req->handle = wine_server_user_handle( hwnd );
959 req->parent = 0;
960 wine_server_call( req );
962 SERVER_END_REQ;
965 * Send the WM_NCDESTROY to the window being destroyed.
967 SendMessageW( hwnd, WM_NCDESTROY, 0, 0 );
969 /* FIXME: do we need to fake QS_MOUSEMOVE wakebit? */
971 /* free resources associated with the window */
973 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
974 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
975 menu = (HMENU)wndPtr->wIDmenu;
976 sys_menu = wndPtr->hSysMenu;
977 free_dce( wndPtr->dce, hwnd );
978 wndPtr->dce = NULL;
979 icon_title = wndPtr->icon_title;
980 HeapFree( GetProcessHeap(), 0, wndPtr->text );
981 wndPtr->text = NULL;
982 HeapFree( GetProcessHeap(), 0, wndPtr->pScroll );
983 wndPtr->pScroll = NULL;
984 surface = wndPtr->surface;
985 wndPtr->surface = NULL;
986 WIN_ReleasePtr( wndPtr );
988 if (icon_title) DestroyWindow( icon_title );
989 if (menu) DestroyMenu( menu );
990 if (sys_menu) DestroyMenu( sys_menu );
991 if (surface)
993 register_window_surface( surface, NULL );
994 window_surface_release( surface );
997 USER_Driver->pDestroyWindow( hwnd );
999 free_window_handle( hwnd );
1000 return 0;
1004 /***********************************************************************
1005 * destroy_thread_window
1007 * Destroy a window upon exit of its thread.
1009 static void destroy_thread_window( HWND hwnd )
1011 WND *wndPtr;
1012 HWND *list;
1013 HMENU menu = 0, sys_menu = 0;
1014 struct window_surface *surface = NULL;
1015 WORD index;
1017 /* free child windows */
1019 if ((list = WIN_ListChildren( hwnd )))
1021 int i;
1022 for (i = 0; list[i]; i++)
1024 if (WIN_IsCurrentThread( list[i] )) destroy_thread_window( list[i] );
1025 else SendMessageW( list[i], WM_WINE_DESTROYWINDOW, 0, 0 );
1027 HeapFree( GetProcessHeap(), 0, list );
1030 /* destroy the client-side storage */
1032 index = USER_HANDLE_TO_INDEX(hwnd);
1033 if (index >= NB_USER_HANDLES) return;
1034 USER_Lock();
1035 if ((wndPtr = user_handles[index]))
1037 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD) menu = (HMENU)wndPtr->wIDmenu;
1038 sys_menu = wndPtr->hSysMenu;
1039 free_dce( wndPtr->dce, hwnd );
1040 surface = wndPtr->surface;
1041 wndPtr->surface = NULL;
1042 InterlockedCompareExchangePointer( &user_handles[index], NULL, wndPtr );
1044 USER_Unlock();
1046 HeapFree( GetProcessHeap(), 0, wndPtr );
1047 if (menu) DestroyMenu( menu );
1048 if (sys_menu) DestroyMenu( sys_menu );
1049 if (surface)
1051 register_window_surface( surface, NULL );
1052 window_surface_release( surface );
1057 /***********************************************************************
1058 * destroy_thread_child_windows
1060 * Destroy child windows upon exit of its thread.
1062 static void destroy_thread_child_windows( HWND hwnd )
1064 HWND *list;
1065 int i;
1067 if (WIN_IsCurrentThread( hwnd ))
1069 destroy_thread_window( hwnd );
1071 else if ((list = WIN_ListChildren( hwnd )))
1073 for (i = 0; list[i]; i++) destroy_thread_child_windows( list[i] );
1074 HeapFree( GetProcessHeap(), 0, list );
1079 /***********************************************************************
1080 * WIN_DestroyThreadWindows
1082 * Destroy all children of 'wnd' owned by the current thread.
1084 void WIN_DestroyThreadWindows( HWND hwnd )
1086 HWND *list;
1087 int i;
1089 if (!(list = WIN_ListChildren( hwnd ))) return;
1091 /* reset owners of top-level windows */
1092 for (i = 0; list[i]; i++)
1094 if (!WIN_IsCurrentThread( list[i] ))
1096 HWND owner = GetWindow( list[i], GW_OWNER );
1097 if (owner && WIN_IsCurrentThread( owner )) WIN_SetOwner( list[i], 0 );
1101 for (i = 0; list[i]; i++) destroy_thread_child_windows( list[i] );
1102 HeapFree( GetProcessHeap(), 0, list );
1106 /***********************************************************************
1107 * WIN_FixCoordinates
1109 * Fix the coordinates - Helper for WIN_CreateWindowEx.
1110 * returns default show mode in sw.
1112 static void WIN_FixCoordinates( CREATESTRUCTW *cs, INT *sw)
1114 #define IS_DEFAULT(x) ((x) == CW_USEDEFAULT || (x) == (SHORT)0x8000)
1115 POINT pos[2];
1117 if (cs->dwExStyle & WS_EX_MDICHILD)
1119 UINT id = 0;
1121 MDI_CalcDefaultChildPos(cs->hwndParent, -1, pos, 0, &id);
1122 if (!(cs->style & WS_POPUP)) cs->hMenu = ULongToHandle(id);
1124 TRACE("MDI child id %04x\n", id);
1127 if (cs->style & (WS_CHILD | WS_POPUP))
1129 if (cs->dwExStyle & WS_EX_MDICHILD)
1131 if (IS_DEFAULT(cs->x))
1133 cs->x = pos[0].x;
1134 cs->y = pos[0].y;
1136 if (IS_DEFAULT(cs->cx) || !cs->cx) cs->cx = pos[1].x;
1137 if (IS_DEFAULT(cs->cy) || !cs->cy) cs->cy = pos[1].y;
1139 else
1141 if (IS_DEFAULT(cs->x)) cs->x = cs->y = 0;
1142 if (IS_DEFAULT(cs->cx)) cs->cx = cs->cy = 0;
1145 else /* overlapped window */
1147 HMONITOR monitor;
1148 MONITORINFO mon_info;
1149 STARTUPINFOW info;
1151 if (!IS_DEFAULT(cs->x) && !IS_DEFAULT(cs->cx) && !IS_DEFAULT(cs->cy)) return;
1153 monitor = MonitorFromWindow( cs->hwndParent, MONITOR_DEFAULTTOPRIMARY );
1154 mon_info.cbSize = sizeof(mon_info);
1155 GetMonitorInfoW( monitor, &mon_info );
1156 GetStartupInfoW( &info );
1158 if (IS_DEFAULT(cs->x))
1160 if (!IS_DEFAULT(cs->y)) *sw = cs->y;
1161 cs->x = (info.dwFlags & STARTF_USEPOSITION) ? info.dwX : mon_info.rcWork.left;
1162 cs->y = (info.dwFlags & STARTF_USEPOSITION) ? info.dwY : mon_info.rcWork.top;
1165 if (IS_DEFAULT(cs->cx))
1167 if (info.dwFlags & STARTF_USESIZE)
1169 cs->cx = info.dwXSize;
1170 cs->cy = info.dwYSize;
1172 else
1174 cs->cx = (mon_info.rcWork.right - mon_info.rcWork.left) * 3 / 4 - cs->x;
1175 cs->cy = (mon_info.rcWork.bottom - mon_info.rcWork.top) * 3 / 4 - cs->y;
1178 /* neither x nor cx are default. Check the y values .
1179 * In the trace we see Outlook and Outlook Express using
1180 * cy set to CW_USEDEFAULT when opening the address book.
1182 else if (IS_DEFAULT(cs->cy))
1184 FIXME("Strange use of CW_USEDEFAULT in nHeight\n");
1185 cs->cy = (mon_info.rcWork.bottom - mon_info.rcWork.top) * 3 / 4 - cs->y;
1188 #undef IS_DEFAULT
1191 /***********************************************************************
1192 * dump_window_styles
1194 static void dump_window_styles( DWORD style, DWORD exstyle )
1196 TRACE( "style:" );
1197 if(style & WS_POPUP) TRACE(" WS_POPUP");
1198 if(style & WS_CHILD) TRACE(" WS_CHILD");
1199 if(style & WS_MINIMIZE) TRACE(" WS_MINIMIZE");
1200 if(style & WS_VISIBLE) TRACE(" WS_VISIBLE");
1201 if(style & WS_DISABLED) TRACE(" WS_DISABLED");
1202 if(style & WS_CLIPSIBLINGS) TRACE(" WS_CLIPSIBLINGS");
1203 if(style & WS_CLIPCHILDREN) TRACE(" WS_CLIPCHILDREN");
1204 if(style & WS_MAXIMIZE) TRACE(" WS_MAXIMIZE");
1205 if((style & WS_CAPTION) == WS_CAPTION) TRACE(" WS_CAPTION");
1206 else
1208 if(style & WS_BORDER) TRACE(" WS_BORDER");
1209 if(style & WS_DLGFRAME) TRACE(" WS_DLGFRAME");
1211 if(style & WS_VSCROLL) TRACE(" WS_VSCROLL");
1212 if(style & WS_HSCROLL) TRACE(" WS_HSCROLL");
1213 if(style & WS_SYSMENU) TRACE(" WS_SYSMENU");
1214 if(style & WS_THICKFRAME) TRACE(" WS_THICKFRAME");
1215 if (style & WS_CHILD)
1217 if(style & WS_GROUP) TRACE(" WS_GROUP");
1218 if(style & WS_TABSTOP) TRACE(" WS_TABSTOP");
1220 else
1222 if(style & WS_MINIMIZEBOX) TRACE(" WS_MINIMIZEBOX");
1223 if(style & WS_MAXIMIZEBOX) TRACE(" WS_MAXIMIZEBOX");
1226 /* FIXME: Add dumping of BS_/ES_/SBS_/LBS_/CBS_/DS_/etc. styles */
1227 #define DUMPED_STYLES \
1228 ((DWORD)(WS_POPUP | \
1229 WS_CHILD | \
1230 WS_MINIMIZE | \
1231 WS_VISIBLE | \
1232 WS_DISABLED | \
1233 WS_CLIPSIBLINGS | \
1234 WS_CLIPCHILDREN | \
1235 WS_MAXIMIZE | \
1236 WS_BORDER | \
1237 WS_DLGFRAME | \
1238 WS_VSCROLL | \
1239 WS_HSCROLL | \
1240 WS_SYSMENU | \
1241 WS_THICKFRAME | \
1242 WS_GROUP | \
1243 WS_TABSTOP | \
1244 WS_MINIMIZEBOX | \
1245 WS_MAXIMIZEBOX))
1247 if(style & ~DUMPED_STYLES) TRACE(" %08x", style & ~DUMPED_STYLES);
1248 TRACE("\n");
1249 #undef DUMPED_STYLES
1251 TRACE( "exstyle:" );
1252 if(exstyle & WS_EX_DLGMODALFRAME) TRACE(" WS_EX_DLGMODALFRAME");
1253 if(exstyle & WS_EX_DRAGDETECT) TRACE(" WS_EX_DRAGDETECT");
1254 if(exstyle & WS_EX_NOPARENTNOTIFY) TRACE(" WS_EX_NOPARENTNOTIFY");
1255 if(exstyle & WS_EX_TOPMOST) TRACE(" WS_EX_TOPMOST");
1256 if(exstyle & WS_EX_ACCEPTFILES) TRACE(" WS_EX_ACCEPTFILES");
1257 if(exstyle & WS_EX_TRANSPARENT) TRACE(" WS_EX_TRANSPARENT");
1258 if(exstyle & WS_EX_MDICHILD) TRACE(" WS_EX_MDICHILD");
1259 if(exstyle & WS_EX_TOOLWINDOW) TRACE(" WS_EX_TOOLWINDOW");
1260 if(exstyle & WS_EX_WINDOWEDGE) TRACE(" WS_EX_WINDOWEDGE");
1261 if(exstyle & WS_EX_CLIENTEDGE) TRACE(" WS_EX_CLIENTEDGE");
1262 if(exstyle & WS_EX_CONTEXTHELP) TRACE(" WS_EX_CONTEXTHELP");
1263 if(exstyle & WS_EX_RIGHT) TRACE(" WS_EX_RIGHT");
1264 if(exstyle & WS_EX_RTLREADING) TRACE(" WS_EX_RTLREADING");
1265 if(exstyle & WS_EX_LEFTSCROLLBAR) TRACE(" WS_EX_LEFTSCROLLBAR");
1266 if(exstyle & WS_EX_CONTROLPARENT) TRACE(" WS_EX_CONTROLPARENT");
1267 if(exstyle & WS_EX_STATICEDGE) TRACE(" WS_EX_STATICEDGE");
1268 if(exstyle & WS_EX_APPWINDOW) TRACE(" WS_EX_APPWINDOW");
1269 if(exstyle & WS_EX_LAYERED) TRACE(" WS_EX_LAYERED");
1270 if(exstyle & WS_EX_LAYOUTRTL) TRACE(" WS_EX_LAYOUTRTL");
1272 #define DUMPED_EX_STYLES \
1273 ((DWORD)(WS_EX_DLGMODALFRAME | \
1274 WS_EX_DRAGDETECT | \
1275 WS_EX_NOPARENTNOTIFY | \
1276 WS_EX_TOPMOST | \
1277 WS_EX_ACCEPTFILES | \
1278 WS_EX_TRANSPARENT | \
1279 WS_EX_MDICHILD | \
1280 WS_EX_TOOLWINDOW | \
1281 WS_EX_WINDOWEDGE | \
1282 WS_EX_CLIENTEDGE | \
1283 WS_EX_CONTEXTHELP | \
1284 WS_EX_RIGHT | \
1285 WS_EX_RTLREADING | \
1286 WS_EX_LEFTSCROLLBAR | \
1287 WS_EX_CONTROLPARENT | \
1288 WS_EX_STATICEDGE | \
1289 WS_EX_APPWINDOW | \
1290 WS_EX_LAYERED | \
1291 WS_EX_LAYOUTRTL))
1293 if(exstyle & ~DUMPED_EX_STYLES) TRACE(" %08x", exstyle & ~DUMPED_EX_STYLES);
1294 TRACE("\n");
1295 #undef DUMPED_EX_STYLES
1299 /***********************************************************************
1300 * WIN_CreateWindowEx
1302 * Implementation of CreateWindowEx().
1304 HWND WIN_CreateWindowEx( CREATESTRUCTW *cs, LPCWSTR className, HINSTANCE module, BOOL unicode )
1306 INT cx, cy, style, sw = SW_SHOW;
1307 LRESULT result;
1308 RECT rect;
1309 WND *wndPtr;
1310 HWND hwnd, parent, owner, top_child = 0;
1311 MDICREATESTRUCTW mdi_cs;
1312 CBT_CREATEWNDW cbtc;
1313 CREATESTRUCTW cbcs;
1315 TRACE("%s %s ex=%08x style=%08x %d,%d %dx%d parent=%p menu=%p inst=%p params=%p\n",
1316 unicode ? debugstr_w(cs->lpszName) : debugstr_a((LPCSTR)cs->lpszName),
1317 debugstr_w(className),
1318 cs->dwExStyle, cs->style, cs->x, cs->y, cs->cx, cs->cy,
1319 cs->hwndParent, cs->hMenu, cs->hInstance, cs->lpCreateParams );
1320 if(TRACE_ON(win)) dump_window_styles( cs->style, cs->dwExStyle );
1322 /* Fix the styles for MDI children */
1323 if (cs->dwExStyle & WS_EX_MDICHILD)
1325 UINT flags = 0;
1327 wndPtr = WIN_GetPtr(cs->hwndParent);
1328 if (wndPtr && wndPtr != WND_OTHER_PROCESS && wndPtr != WND_DESKTOP)
1330 flags = wndPtr->flags;
1331 WIN_ReleasePtr(wndPtr);
1334 if (!(flags & WIN_ISMDICLIENT))
1336 WARN("WS_EX_MDICHILD, but parent %p is not MDIClient\n", cs->hwndParent);
1337 return 0;
1340 /* cs->lpCreateParams of WM_[NC]CREATE is different for MDI children.
1341 * MDICREATESTRUCT members have the originally passed values.
1343 * Note: we rely on the fact that MDICREATESTRUCTA and MDICREATESTRUCTW
1344 * have the same layout.
1346 mdi_cs.szClass = cs->lpszClass;
1347 mdi_cs.szTitle = cs->lpszName;
1348 mdi_cs.hOwner = cs->hInstance;
1349 mdi_cs.x = cs->x;
1350 mdi_cs.y = cs->y;
1351 mdi_cs.cx = cs->cx;
1352 mdi_cs.cy = cs->cy;
1353 mdi_cs.style = cs->style;
1354 mdi_cs.lParam = (LPARAM)cs->lpCreateParams;
1356 cs->lpCreateParams = &mdi_cs;
1358 if (GetWindowLongW(cs->hwndParent, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1360 if (cs->style & WS_POPUP)
1362 TRACE("WS_POPUP with MDIS_ALLCHILDSTYLES is not allowed\n");
1363 return 0;
1365 cs->style |= WS_CHILD | WS_CLIPSIBLINGS;
1367 else
1369 cs->style &= ~WS_POPUP;
1370 cs->style |= WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CAPTION |
1371 WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
1374 top_child = GetWindow(cs->hwndParent, GW_CHILD);
1376 if (top_child)
1378 /* Restore current maximized child */
1379 if((cs->style & WS_VISIBLE) && IsZoomed(top_child))
1381 TRACE("Restoring current maximized child %p\n", top_child);
1382 if (cs->style & WS_MAXIMIZE)
1384 /* if the new window is maximized don't bother repainting */
1385 SendMessageW( top_child, WM_SETREDRAW, FALSE, 0 );
1386 ShowWindow( top_child, SW_SHOWNORMAL );
1387 SendMessageW( top_child, WM_SETREDRAW, TRUE, 0 );
1389 else ShowWindow( top_child, SW_SHOWNORMAL );
1394 CLASS_RegisterBuiltinClasses();
1396 /* Find the parent window */
1398 parent = cs->hwndParent;
1399 owner = 0;
1401 if (cs->hwndParent == HWND_MESSAGE)
1403 cs->hwndParent = parent = get_hwnd_message_parent();
1405 else if (cs->hwndParent)
1407 if ((cs->style & (WS_CHILD|WS_POPUP)) != WS_CHILD)
1409 parent = GetDesktopWindow();
1410 owner = cs->hwndParent;
1412 else
1414 DWORD parent_style = GetWindowLongW( parent, GWL_EXSTYLE );
1415 if ((parent_style & WS_EX_LAYOUTRTL) && !(parent_style & WS_EX_NOINHERITLAYOUT))
1416 cs->dwExStyle |= WS_EX_LAYOUTRTL;
1419 else
1421 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
1423 if ((cs->style & (WS_CHILD|WS_POPUP)) == WS_CHILD)
1425 WARN("No parent for child window\n" );
1426 SetLastError(ERROR_TLW_WITH_WSCHILD);
1427 return 0; /* WS_CHILD needs a parent, but WS_POPUP doesn't */
1430 /* are we creating the desktop or HWND_MESSAGE parent itself? */
1431 if (className != (LPCWSTR)DESKTOP_CLASS_ATOM &&
1432 (IS_INTRESOURCE(className) || strcmpiW( className, messageW )))
1434 DWORD layout;
1435 GetProcessDefaultLayout( &layout );
1436 if (layout & LAYOUT_RTL) cs->dwExStyle |= WS_EX_LAYOUTRTL;
1437 parent = GetDesktopWindow();
1441 WIN_FixCoordinates(cs, &sw); /* fix default coordinates */
1443 if ((cs->dwExStyle & WS_EX_DLGMODALFRAME) ||
1444 ((!(cs->dwExStyle & WS_EX_STATICEDGE)) &&
1445 (cs->style & (WS_DLGFRAME | WS_THICKFRAME))))
1446 cs->dwExStyle |= WS_EX_WINDOWEDGE;
1447 else
1448 cs->dwExStyle &= ~WS_EX_WINDOWEDGE;
1450 /* Create the window structure */
1452 if (!(wndPtr = create_window_handle( parent, owner, className, module, unicode )))
1453 return 0;
1454 hwnd = wndPtr->obj.handle;
1456 /* Fill the window structure */
1458 wndPtr->tid = GetCurrentThreadId();
1459 wndPtr->hInstance = cs->hInstance;
1460 wndPtr->text = NULL;
1461 wndPtr->dwStyle = cs->style & ~WS_VISIBLE;
1462 wndPtr->dwExStyle = cs->dwExStyle;
1463 wndPtr->wIDmenu = 0;
1464 wndPtr->helpContext = 0;
1465 wndPtr->pScroll = NULL;
1466 wndPtr->userdata = 0;
1467 wndPtr->hIcon = 0;
1468 wndPtr->hIconSmall = 0;
1469 wndPtr->hSysMenu = 0;
1471 wndPtr->min_pos.x = wndPtr->min_pos.y = -1;
1472 wndPtr->max_pos.x = wndPtr->max_pos.y = -1;
1473 SetRect( &wndPtr->normal_rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
1475 if (wndPtr->dwStyle & WS_SYSMENU) SetSystemMenu( hwnd, 0 );
1478 * Correct the window styles.
1480 * It affects only the style loaded into the WIN structure.
1483 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
1485 wndPtr->dwStyle |= WS_CLIPSIBLINGS;
1486 if (!(wndPtr->dwStyle & WS_POPUP))
1487 wndPtr->dwStyle |= WS_CAPTION;
1490 /* WS_EX_WINDOWEDGE depends on some other styles */
1491 if (wndPtr->dwExStyle & WS_EX_DLGMODALFRAME)
1492 wndPtr->dwExStyle |= WS_EX_WINDOWEDGE;
1493 else if (wndPtr->dwStyle & (WS_DLGFRAME | WS_THICKFRAME))
1495 if (!((wndPtr->dwExStyle & WS_EX_STATICEDGE) &&
1496 (wndPtr->dwStyle & (WS_CHILD | WS_POPUP))))
1497 wndPtr->dwExStyle |= WS_EX_WINDOWEDGE;
1499 else
1500 wndPtr->dwExStyle &= ~WS_EX_WINDOWEDGE;
1502 if (!(wndPtr->dwStyle & (WS_CHILD | WS_POPUP)))
1503 wndPtr->flags |= WIN_NEED_SIZE;
1505 SERVER_START_REQ( set_window_info )
1507 req->handle = wine_server_user_handle( hwnd );
1508 req->flags = SET_WIN_STYLE | SET_WIN_EXSTYLE | SET_WIN_INSTANCE | SET_WIN_UNICODE;
1509 req->style = wndPtr->dwStyle;
1510 req->ex_style = wndPtr->dwExStyle;
1511 req->instance = wine_server_client_ptr( wndPtr->hInstance );
1512 req->is_unicode = (wndPtr->flags & WIN_ISUNICODE) != 0;
1513 req->extra_offset = -1;
1514 wine_server_call( req );
1516 SERVER_END_REQ;
1518 /* Set the window menu */
1520 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
1522 if (cs->hMenu)
1524 if (!MENU_SetMenu(hwnd, cs->hMenu))
1526 WIN_ReleasePtr( wndPtr );
1527 free_window_handle( hwnd );
1528 return 0;
1531 else
1533 LPCWSTR menuName = (LPCWSTR)GetClassLongPtrW( hwnd, GCLP_MENUNAME );
1534 if (menuName)
1536 cs->hMenu = LoadMenuW( cs->hInstance, menuName );
1537 if (cs->hMenu) MENU_SetMenu( hwnd, cs->hMenu );
1541 else SetWindowLongPtrW( hwnd, GWLP_ID, (ULONG_PTR)cs->hMenu );
1543 /* call the WH_CBT hook */
1545 /* the window style passed to the hook must be the real window style,
1546 * rather than just the window style that the caller to CreateWindowEx
1547 * passed in, so we have to copy the original CREATESTRUCT and get the
1548 * the real style. */
1549 cbcs = *cs;
1550 cbcs.style = wndPtr->dwStyle;
1551 cbtc.lpcs = &cbcs;
1552 cbtc.hwndInsertAfter = HWND_TOP;
1553 WIN_ReleasePtr( wndPtr );
1554 if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode )) goto failed;
1556 /* send the WM_GETMINMAXINFO message and fix the size if needed */
1558 cx = cs->cx;
1559 cy = cs->cy;
1560 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
1562 POINT maxSize, maxPos, minTrack, maxTrack;
1563 WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
1564 if (maxTrack.x < cx) cx = maxTrack.x;
1565 if (maxTrack.y < cy) cy = maxTrack.y;
1566 if (minTrack.x > cx) cx = minTrack.x;
1567 if (minTrack.y > cy) cy = minTrack.y;
1570 if (cx < 0) cx = 0;
1571 if (cy < 0) cy = 0;
1572 SetRect( &rect, cs->x, cs->y, cs->x + cx, cs->y + cy );
1573 /* check for wraparound */
1574 if (cs->x + cx < cs->x) rect.right = 0x7fffffff;
1575 if (cs->y + cy < cs->y) rect.bottom = 0x7fffffff;
1576 if (!set_window_pos( hwnd, 0, SWP_NOZORDER | SWP_NOACTIVATE, &rect, &rect, NULL )) goto failed;
1578 /* send WM_NCCREATE */
1580 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cx, cy );
1581 if (unicode)
1582 result = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1583 else
1584 result = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1585 if (!result)
1587 WARN( "%p: aborted by WM_NCCREATE\n", hwnd );
1588 goto failed;
1591 /* send WM_NCCALCSIZE */
1593 if (WIN_GetRectangles( hwnd, COORDS_PARENT, &rect, NULL ))
1595 /* yes, even if the CBT hook was called with HWND_TOP */
1596 HWND insert_after = (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
1597 RECT client_rect = rect;
1599 /* the rectangle is in screen coords for WM_NCCALCSIZE when wparam is FALSE */
1600 MapWindowPoints( parent, 0, (POINT *)&client_rect, 2 );
1601 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&client_rect );
1602 MapWindowPoints( 0, parent, (POINT *)&client_rect, 2 );
1603 set_window_pos( hwnd, insert_after, SWP_NOACTIVATE, &rect, &client_rect, NULL );
1605 else return 0;
1607 /* send WM_CREATE */
1609 if (unicode)
1610 result = SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs );
1611 else
1612 result = SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs );
1613 if (result == -1) goto failed;
1615 /* call the driver */
1617 if (!USER_Driver->pCreateWindow( hwnd )) goto failed;
1619 NotifyWinEvent(EVENT_OBJECT_CREATE, hwnd, OBJID_WINDOW, 0);
1621 /* send the size messages */
1623 if (!(wndPtr = WIN_GetPtr( hwnd )) ||
1624 wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return 0;
1625 if (!(wndPtr->flags & WIN_NEED_SIZE))
1627 WIN_ReleasePtr( wndPtr );
1628 WIN_GetRectangles( hwnd, COORDS_PARENT, NULL, &rect );
1629 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1630 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1631 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( rect.left, rect.top ) );
1633 else WIN_ReleasePtr( wndPtr );
1635 /* Show the window, maximizing or minimizing if needed */
1637 style = WIN_SetStyle( hwnd, 0, WS_MAXIMIZE | WS_MINIMIZE );
1638 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1640 RECT newPos;
1641 UINT swFlag = (style & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1643 swFlag = WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1644 swFlag |= SWP_FRAMECHANGED; /* Frame always gets changed */
1645 if (!(style & WS_VISIBLE) || (style & WS_CHILD) || GetActiveWindow()) swFlag |= SWP_NOACTIVATE;
1646 SetWindowPos( hwnd, 0, newPos.left, newPos.top, newPos.right - newPos.left,
1647 newPos.bottom - newPos.top, swFlag );
1650 /* Notify the parent window only */
1652 send_parent_notify( hwnd, WM_CREATE );
1653 if (!IsWindow( hwnd )) return 0;
1655 if (cs->style & WS_VISIBLE)
1657 if (cs->style & WS_MAXIMIZE)
1658 sw = SW_SHOW;
1659 else if (cs->style & WS_MINIMIZE)
1660 sw = SW_SHOWMINIMIZED;
1662 ShowWindow( hwnd, sw );
1663 if (cs->dwExStyle & WS_EX_MDICHILD)
1665 SendMessageW(cs->hwndParent, WM_MDIREFRESHMENU, 0, 0);
1666 /* ShowWindow won't activate child windows */
1667 SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE );
1671 /* Call WH_SHELL hook */
1673 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) && !GetWindow( hwnd, GW_OWNER ))
1674 HOOK_CallHooks( WH_SHELL, HSHELL_WINDOWCREATED, (WPARAM)hwnd, 0, TRUE );
1676 TRACE("created window %p\n", hwnd);
1677 return hwnd;
1679 failed:
1680 WIN_DestroyWindow( hwnd );
1681 return 0;
1685 /***********************************************************************
1686 * CreateWindowExA (USER32.@)
1688 HWND WINAPI DECLSPEC_HOTPATCH CreateWindowExA( DWORD exStyle, LPCSTR className,
1689 LPCSTR windowName, DWORD style, INT x,
1690 INT y, INT width, INT height,
1691 HWND parent, HMENU menu,
1692 HINSTANCE instance, LPVOID data )
1694 CREATESTRUCTA cs;
1696 cs.lpCreateParams = data;
1697 cs.hInstance = instance;
1698 cs.hMenu = menu;
1699 cs.hwndParent = parent;
1700 cs.x = x;
1701 cs.y = y;
1702 cs.cx = width;
1703 cs.cy = height;
1704 cs.style = style;
1705 cs.lpszName = windowName;
1706 cs.lpszClass = className;
1707 cs.dwExStyle = exStyle;
1709 if (!IS_INTRESOURCE(className))
1711 WCHAR bufferW[256];
1712 if (!MultiByteToWideChar( CP_ACP, 0, className, -1, bufferW, sizeof(bufferW)/sizeof(WCHAR) ))
1713 return 0;
1714 return wow_handlers.create_window( (CREATESTRUCTW *)&cs, bufferW, instance, FALSE );
1716 /* Note: we rely on the fact that CREATESTRUCTA and */
1717 /* CREATESTRUCTW have the same layout. */
1718 return wow_handlers.create_window( (CREATESTRUCTW *)&cs, (LPCWSTR)className, instance, FALSE );
1722 /***********************************************************************
1723 * CreateWindowExW (USER32.@)
1725 HWND WINAPI DECLSPEC_HOTPATCH CreateWindowExW( DWORD exStyle, LPCWSTR className,
1726 LPCWSTR windowName, DWORD style, INT x,
1727 INT y, INT width, INT height,
1728 HWND parent, HMENU menu,
1729 HINSTANCE instance, LPVOID data )
1731 CREATESTRUCTW cs;
1733 cs.lpCreateParams = data;
1734 cs.hInstance = instance;
1735 cs.hMenu = menu;
1736 cs.hwndParent = parent;
1737 cs.x = x;
1738 cs.y = y;
1739 cs.cx = width;
1740 cs.cy = height;
1741 cs.style = style;
1742 cs.lpszName = windowName;
1743 cs.lpszClass = className;
1744 cs.dwExStyle = exStyle;
1746 return wow_handlers.create_window( &cs, className, instance, TRUE );
1750 /***********************************************************************
1751 * WIN_SendDestroyMsg
1753 static void WIN_SendDestroyMsg( HWND hwnd )
1755 GUITHREADINFO info;
1757 info.cbSize = sizeof(info);
1758 if (GetGUIThreadInfo( GetCurrentThreadId(), &info ))
1760 if (hwnd == info.hwndCaret) DestroyCaret();
1761 if (hwnd == info.hwndActive) WINPOS_ActivateOtherWindow( hwnd );
1765 * Send the WM_DESTROY to the window.
1767 SendMessageW( hwnd, WM_DESTROY, 0, 0);
1770 * This WM_DESTROY message can trigger re-entrant calls to DestroyWindow
1771 * make sure that the window still exists when we come back.
1773 if (IsWindow(hwnd))
1775 HWND* pWndArray;
1776 int i;
1778 if (!(pWndArray = WIN_ListChildren( hwnd ))) return;
1780 for (i = 0; pWndArray[i]; i++)
1782 if (IsWindow( pWndArray[i] )) WIN_SendDestroyMsg( pWndArray[i] );
1784 HeapFree( GetProcessHeap(), 0, pWndArray );
1786 else
1787 WARN("\tdestroyed itself while in WM_DESTROY!\n");
1791 /***********************************************************************
1792 * DestroyWindow (USER32.@)
1794 BOOL WINAPI DestroyWindow( HWND hwnd )
1796 BOOL is_child;
1798 if (!(hwnd = WIN_IsCurrentThread( hwnd )) || is_desktop_window( hwnd ))
1800 SetLastError( ERROR_ACCESS_DENIED );
1801 return FALSE;
1804 TRACE("(%p)\n", hwnd);
1806 /* Call hooks */
1808 if (HOOK_CallHooks( WH_CBT, HCBT_DESTROYWND, (WPARAM)hwnd, 0, TRUE )) return FALSE;
1810 if (MENU_IsMenuActive() == hwnd)
1811 EndMenu();
1813 is_child = (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) != 0;
1815 if (is_child)
1817 if (!USER_IsExitingThread( GetCurrentThreadId() ))
1818 send_parent_notify( hwnd, WM_DESTROY );
1820 else if (!GetWindow( hwnd, GW_OWNER ))
1822 HOOK_CallHooks( WH_SHELL, HSHELL_WINDOWDESTROYED, (WPARAM)hwnd, 0L, TRUE );
1823 /* FIXME: clean up palette - see "Internals" p.352 */
1826 if (!IsWindow(hwnd)) return TRUE;
1828 /* Hide the window */
1829 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)
1831 /* Only child windows receive WM_SHOWWINDOW in DestroyWindow() */
1832 if (is_child)
1833 ShowWindow( hwnd, SW_HIDE );
1834 else
1835 SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE |
1836 SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW );
1839 if (!IsWindow(hwnd)) return TRUE;
1841 /* Recursively destroy owned windows */
1843 if (!is_child)
1845 for (;;)
1847 int i, got_one = 0;
1848 HWND *list = WIN_ListChildren( GetDesktopWindow() );
1849 if (list)
1851 for (i = 0; list[i]; i++)
1853 if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
1854 if (WIN_IsCurrentThread( list[i] ))
1856 DestroyWindow( list[i] );
1857 got_one = 1;
1858 continue;
1860 WIN_SetOwner( list[i], 0 );
1862 HeapFree( GetProcessHeap(), 0, list );
1864 if (!got_one) break;
1868 /* Send destroy messages */
1870 WIN_SendDestroyMsg( hwnd );
1871 if (!IsWindow( hwnd )) return TRUE;
1873 if (GetClipboardOwner() == hwnd)
1874 CLIPBOARD_ReleaseOwner();
1876 /* Destroy the window storage */
1878 WIN_DestroyWindow( hwnd );
1879 return TRUE;
1883 /***********************************************************************
1884 * CloseWindow (USER32.@)
1886 BOOL WINAPI CloseWindow( HWND hwnd )
1888 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) return FALSE;
1889 ShowWindow( hwnd, SW_MINIMIZE );
1890 return TRUE;
1894 /***********************************************************************
1895 * OpenIcon (USER32.@)
1897 BOOL WINAPI OpenIcon( HWND hwnd )
1899 if (!IsIconic( hwnd )) return FALSE;
1900 ShowWindow( hwnd, SW_SHOWNORMAL );
1901 return TRUE;
1905 /***********************************************************************
1906 * FindWindowExW (USER32.@)
1908 HWND WINAPI FindWindowExW( HWND parent, HWND child, LPCWSTR className, LPCWSTR title )
1910 HWND *list = NULL;
1911 HWND retvalue = 0;
1912 int i = 0, len = 0;
1913 WCHAR *buffer = NULL;
1915 if (!parent && child) parent = GetDesktopWindow();
1916 else if (parent == HWND_MESSAGE) parent = get_hwnd_message_parent();
1918 if (title)
1920 len = strlenW(title) + 1; /* one extra char to check for chars beyond the end */
1921 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return 0;
1924 if (!(list = list_window_children( 0, parent, className, 0 ))) goto done;
1926 if (child)
1928 child = WIN_GetFullHandle( child );
1929 while (list[i] && list[i] != child) i++;
1930 if (!list[i]) goto done;
1931 i++; /* start from next window */
1934 if (title)
1936 while (list[i])
1938 if (GetWindowTextW( list[i], buffer, len + 1 ))
1940 if (!strcmpiW( buffer, title )) break;
1942 else
1944 if (!title[0]) break;
1946 i++;
1949 retvalue = list[i];
1951 done:
1952 HeapFree( GetProcessHeap(), 0, list );
1953 HeapFree( GetProcessHeap(), 0, buffer );
1954 return retvalue;
1959 /***********************************************************************
1960 * FindWindowA (USER32.@)
1962 HWND WINAPI FindWindowA( LPCSTR className, LPCSTR title )
1964 HWND ret = FindWindowExA( 0, 0, className, title );
1965 if (!ret) SetLastError (ERROR_CANNOT_FIND_WND_CLASS);
1966 return ret;
1970 /***********************************************************************
1971 * FindWindowExA (USER32.@)
1973 HWND WINAPI FindWindowExA( HWND parent, HWND child, LPCSTR className, LPCSTR title )
1975 LPWSTR titleW = NULL;
1976 HWND hwnd = 0;
1978 if (title)
1980 DWORD len = MultiByteToWideChar( CP_ACP, 0, title, -1, NULL, 0 );
1981 if (!(titleW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return 0;
1982 MultiByteToWideChar( CP_ACP, 0, title, -1, titleW, len );
1985 if (!IS_INTRESOURCE(className))
1987 WCHAR classW[256];
1988 if (MultiByteToWideChar( CP_ACP, 0, className, -1, classW, sizeof(classW)/sizeof(WCHAR) ))
1989 hwnd = FindWindowExW( parent, child, classW, titleW );
1991 else
1993 hwnd = FindWindowExW( parent, child, (LPCWSTR)className, titleW );
1996 HeapFree( GetProcessHeap(), 0, titleW );
1997 return hwnd;
2001 /***********************************************************************
2002 * FindWindowW (USER32.@)
2004 HWND WINAPI FindWindowW( LPCWSTR className, LPCWSTR title )
2006 return FindWindowExW( 0, 0, className, title );
2010 /**********************************************************************
2011 * GetDesktopWindow (USER32.@)
2013 HWND WINAPI GetDesktopWindow(void)
2015 struct user_thread_info *thread_info = get_user_thread_info();
2017 if (thread_info->top_window) return thread_info->top_window;
2019 SERVER_START_REQ( get_desktop_window )
2021 req->force = 0;
2022 if (!wine_server_call( req ))
2024 thread_info->top_window = wine_server_ptr_handle( reply->top_window );
2025 thread_info->msg_window = wine_server_ptr_handle( reply->msg_window );
2028 SERVER_END_REQ;
2030 if (!thread_info->top_window)
2032 USEROBJECTFLAGS flags;
2033 if (!GetUserObjectInformationW( GetProcessWindowStation(), UOI_FLAGS, &flags,
2034 sizeof(flags), NULL ) || (flags.dwFlags & WSF_VISIBLE))
2036 static const WCHAR explorer[] = {'\\','e','x','p','l','o','r','e','r','.','e','x','e',0};
2037 static const WCHAR args[] = {' ','/','d','e','s','k','t','o','p',0};
2038 STARTUPINFOW si;
2039 PROCESS_INFORMATION pi;
2040 WCHAR windir[MAX_PATH];
2041 WCHAR app[MAX_PATH + sizeof(explorer)/sizeof(WCHAR)];
2042 WCHAR cmdline[MAX_PATH + (sizeof(explorer) + sizeof(args))/sizeof(WCHAR)];
2043 void *redir;
2045 memset( &si, 0, sizeof(si) );
2046 si.cb = sizeof(si);
2047 si.dwFlags = STARTF_USESTDHANDLES;
2048 si.hStdInput = 0;
2049 si.hStdOutput = 0;
2050 si.hStdError = GetStdHandle( STD_ERROR_HANDLE );
2052 GetSystemDirectoryW( windir, MAX_PATH );
2053 strcpyW( app, windir );
2054 strcatW( app, explorer );
2055 strcpyW( cmdline, app );
2056 strcatW( cmdline, args );
2058 Wow64DisableWow64FsRedirection( &redir );
2059 if (CreateProcessW( app, cmdline, NULL, NULL, FALSE, DETACHED_PROCESS,
2060 NULL, windir, &si, &pi ))
2062 TRACE( "started explorer pid %04x tid %04x\n", pi.dwProcessId, pi.dwThreadId );
2063 WaitForInputIdle( pi.hProcess, 10000 );
2064 CloseHandle( pi.hThread );
2065 CloseHandle( pi.hProcess );
2067 else WARN( "failed to start explorer, err %d\n", GetLastError() );
2068 Wow64RevertWow64FsRedirection( redir );
2070 else TRACE( "not starting explorer since winstation is not visible\n" );
2072 SERVER_START_REQ( get_desktop_window )
2074 req->force = 1;
2075 if (!wine_server_call( req ))
2077 thread_info->top_window = wine_server_ptr_handle( reply->top_window );
2078 thread_info->msg_window = wine_server_ptr_handle( reply->msg_window );
2081 SERVER_END_REQ;
2084 if (!thread_info->top_window || !USER_Driver->pCreateDesktopWindow( thread_info->top_window ))
2085 ERR( "failed to create desktop window\n" );
2087 return thread_info->top_window;
2091 /*******************************************************************
2092 * EnableWindow (USER32.@)
2094 BOOL WINAPI EnableWindow( HWND hwnd, BOOL enable )
2096 BOOL retvalue;
2097 HWND full_handle;
2099 if (is_broadcast(hwnd))
2101 SetLastError( ERROR_INVALID_PARAMETER );
2102 return FALSE;
2105 if (!(full_handle = WIN_IsCurrentThread( hwnd )))
2106 return SendMessageW( hwnd, WM_WINE_ENABLEWINDOW, enable, 0 );
2108 hwnd = full_handle;
2110 TRACE("( %p, %d )\n", hwnd, enable);
2112 retvalue = !IsWindowEnabled( hwnd );
2114 if (enable && retvalue)
2116 WIN_SetStyle( hwnd, 0, WS_DISABLED );
2117 SendMessageW( hwnd, WM_ENABLE, TRUE, 0 );
2119 else if (!enable && !retvalue)
2121 HWND capture_wnd;
2123 SendMessageW( hwnd, WM_CANCELMODE, 0, 0);
2125 WIN_SetStyle( hwnd, WS_DISABLED, 0 );
2127 if (hwnd == GetFocus())
2128 SetFocus( 0 ); /* A disabled window can't have the focus */
2130 capture_wnd = GetCapture();
2131 if (hwnd == capture_wnd || IsChild(hwnd, capture_wnd))
2132 ReleaseCapture(); /* A disabled window can't capture the mouse */
2134 SendMessageW( hwnd, WM_ENABLE, FALSE, 0 );
2136 return retvalue;
2140 /***********************************************************************
2141 * IsWindowEnabled (USER32.@)
2143 BOOL WINAPI IsWindowEnabled(HWND hWnd)
2145 return !(GetWindowLongW( hWnd, GWL_STYLE ) & WS_DISABLED);
2149 /***********************************************************************
2150 * IsWindowUnicode (USER32.@)
2152 BOOL WINAPI IsWindowUnicode( HWND hwnd )
2154 WND * wndPtr;
2155 BOOL retvalue = FALSE;
2157 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
2159 if (wndPtr == WND_DESKTOP) return TRUE;
2161 if (wndPtr != WND_OTHER_PROCESS)
2163 retvalue = (wndPtr->flags & WIN_ISUNICODE) != 0;
2164 WIN_ReleasePtr( wndPtr );
2166 else
2168 SERVER_START_REQ( get_window_info )
2170 req->handle = wine_server_user_handle( hwnd );
2171 if (!wine_server_call_err( req )) retvalue = reply->is_unicode;
2173 SERVER_END_REQ;
2175 return retvalue;
2179 /**********************************************************************
2180 * WIN_GetWindowLong
2182 * Helper function for GetWindowLong().
2184 static LONG_PTR WIN_GetWindowLong( HWND hwnd, INT offset, UINT size, BOOL unicode )
2186 LONG_PTR retvalue = 0;
2187 WND *wndPtr;
2189 if (offset == GWLP_HWNDPARENT)
2191 HWND parent = GetAncestor( hwnd, GA_PARENT );
2192 if (parent == GetDesktopWindow()) parent = GetWindow( hwnd, GW_OWNER );
2193 return (ULONG_PTR)parent;
2196 if (!(wndPtr = WIN_GetPtr( hwnd )))
2198 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2199 return 0;
2202 if (wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP)
2204 if (offset == GWLP_WNDPROC)
2206 SetLastError( ERROR_ACCESS_DENIED );
2207 return 0;
2209 SERVER_START_REQ( set_window_info )
2211 req->handle = wine_server_user_handle( hwnd );
2212 req->flags = 0; /* don't set anything, just retrieve */
2213 req->extra_offset = (offset >= 0) ? offset : -1;
2214 req->extra_size = (offset >= 0) ? size : 0;
2215 if (!wine_server_call_err( req ))
2217 switch(offset)
2219 case GWL_STYLE: retvalue = reply->old_style; break;
2220 case GWL_EXSTYLE: retvalue = reply->old_ex_style; break;
2221 case GWLP_ID: retvalue = reply->old_id; break;
2222 case GWLP_HINSTANCE: retvalue = (ULONG_PTR)wine_server_get_ptr( reply->old_instance ); break;
2223 case GWLP_USERDATA: retvalue = reply->old_user_data; break;
2224 default:
2225 if (offset >= 0) retvalue = get_win_data( &reply->old_extra_value, size );
2226 else SetLastError( ERROR_INVALID_INDEX );
2227 break;
2231 SERVER_END_REQ;
2232 return retvalue;
2235 /* now we have a valid wndPtr */
2237 if (offset >= 0)
2239 if (offset > (int)(wndPtr->cbWndExtra - size))
2241 WARN("Invalid offset %d\n", offset );
2242 WIN_ReleasePtr( wndPtr );
2243 SetLastError( ERROR_INVALID_INDEX );
2244 return 0;
2246 retvalue = get_win_data( (char *)wndPtr->wExtra + offset, size );
2248 /* Special case for dialog window procedure */
2249 if ((offset == DWLP_DLGPROC) && (size == sizeof(LONG_PTR)) && wndPtr->dlgInfo)
2250 retvalue = (LONG_PTR)WINPROC_GetProc( (WNDPROC)retvalue, unicode );
2251 WIN_ReleasePtr( wndPtr );
2252 return retvalue;
2255 switch(offset)
2257 case GWLP_USERDATA: retvalue = wndPtr->userdata; break;
2258 case GWL_STYLE: retvalue = wndPtr->dwStyle; break;
2259 case GWL_EXSTYLE: retvalue = wndPtr->dwExStyle; break;
2260 case GWLP_ID: retvalue = wndPtr->wIDmenu; break;
2261 case GWLP_HINSTANCE: retvalue = (ULONG_PTR)wndPtr->hInstance; break;
2262 case GWLP_WNDPROC:
2263 /* This looks like a hack only for the edit control (see tests). This makes these controls
2264 * more tolerant to A/W mismatches. The lack of W->A->W conversion for such a mismatch suggests
2265 * that the hack is in GetWindowLongPtr[AW], not in winprocs.
2267 if (wndPtr->winproc == BUILTIN_WINPROC(WINPROC_EDIT) && (!unicode != !(wndPtr->flags & WIN_ISUNICODE)))
2268 retvalue = (ULONG_PTR)wndPtr->winproc;
2269 else
2270 retvalue = (ULONG_PTR)WINPROC_GetProc( wndPtr->winproc, unicode );
2271 break;
2272 default:
2273 WARN("Unknown offset %d\n", offset );
2274 SetLastError( ERROR_INVALID_INDEX );
2275 break;
2277 WIN_ReleasePtr(wndPtr);
2278 return retvalue;
2282 /**********************************************************************
2283 * WIN_SetWindowLong
2285 * Helper function for SetWindowLong().
2287 * 0 is the failure code. However, in the case of failure SetLastError
2288 * must be set to distinguish between a 0 return value and a failure.
2290 LONG_PTR WIN_SetWindowLong( HWND hwnd, INT offset, UINT size, LONG_PTR newval, BOOL unicode )
2292 STYLESTRUCT style;
2293 BOOL ok, made_visible = FALSE;
2294 LONG_PTR retval = 0;
2295 WND *wndPtr;
2297 TRACE( "%p %d %lx %c\n", hwnd, offset, newval, unicode ? 'W' : 'A' );
2299 if (is_broadcast(hwnd))
2301 SetLastError( ERROR_INVALID_PARAMETER );
2302 return FALSE;
2305 if (!(wndPtr = WIN_GetPtr( hwnd )))
2307 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2308 return 0;
2310 if (wndPtr == WND_DESKTOP)
2312 /* can't change anything on the desktop window */
2313 SetLastError( ERROR_ACCESS_DENIED );
2314 return 0;
2316 if (wndPtr == WND_OTHER_PROCESS)
2318 if (offset == GWLP_WNDPROC)
2320 SetLastError( ERROR_ACCESS_DENIED );
2321 return 0;
2323 if (offset > 32767 || offset < -32767)
2325 SetLastError( ERROR_INVALID_INDEX );
2326 return 0;
2328 return SendMessageW( hwnd, WM_WINE_SETWINDOWLONG, MAKEWPARAM( offset, size ), newval );
2331 /* first some special cases */
2332 switch( offset )
2334 case GWL_STYLE:
2335 style.styleOld = wndPtr->dwStyle;
2336 style.styleNew = newval;
2337 WIN_ReleasePtr( wndPtr );
2338 SendMessageW( hwnd, WM_STYLECHANGING, GWL_STYLE, (LPARAM)&style );
2339 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
2340 newval = style.styleNew;
2341 /* WS_CLIPSIBLINGS can't be reset on top-level windows */
2342 if (wndPtr->parent == GetDesktopWindow()) newval |= WS_CLIPSIBLINGS;
2343 /* FIXME: changing WS_DLGFRAME | WS_THICKFRAME is supposed to change
2344 WS_EX_WINDOWEDGE too */
2345 break;
2346 case GWL_EXSTYLE:
2347 style.styleOld = wndPtr->dwExStyle;
2348 style.styleNew = newval;
2349 WIN_ReleasePtr( wndPtr );
2350 SendMessageW( hwnd, WM_STYLECHANGING, GWL_EXSTYLE, (LPARAM)&style );
2351 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
2352 /* WS_EX_TOPMOST can only be changed through SetWindowPos */
2353 newval = (style.styleNew & ~WS_EX_TOPMOST) | (wndPtr->dwExStyle & WS_EX_TOPMOST);
2354 /* WS_EX_WINDOWEDGE depends on some other styles */
2355 if (newval & WS_EX_DLGMODALFRAME)
2356 newval |= WS_EX_WINDOWEDGE;
2357 else if (!(newval & WS_EX_STATICEDGE) && (wndPtr->dwStyle & (WS_DLGFRAME | WS_THICKFRAME)))
2358 newval |= WS_EX_WINDOWEDGE;
2359 else
2360 newval &= ~WS_EX_WINDOWEDGE;
2361 break;
2362 case GWLP_HWNDPARENT:
2363 if (wndPtr->parent == GetDesktopWindow())
2365 WIN_ReleasePtr( wndPtr );
2366 return (ULONG_PTR)WIN_SetOwner( hwnd, (HWND)newval );
2368 else
2370 WIN_ReleasePtr( wndPtr );
2371 return (ULONG_PTR)SetParent( hwnd, (HWND)newval );
2373 case GWLP_WNDPROC:
2375 WNDPROC proc;
2376 UINT old_flags = wndPtr->flags;
2377 retval = WIN_GetWindowLong( hwnd, offset, size, unicode );
2378 proc = WINPROC_AllocProc( (WNDPROC)newval, unicode );
2379 if (proc) wndPtr->winproc = proc;
2380 if (WINPROC_IsUnicode( proc, unicode )) wndPtr->flags |= WIN_ISUNICODE;
2381 else wndPtr->flags &= ~WIN_ISUNICODE;
2382 if (!((old_flags ^ wndPtr->flags) & WIN_ISUNICODE))
2384 WIN_ReleasePtr( wndPtr );
2385 return retval;
2387 /* update is_unicode flag on the server side */
2388 break;
2390 case GWLP_ID:
2391 case GWLP_HINSTANCE:
2392 case GWLP_USERDATA:
2393 break;
2394 case DWLP_DLGPROC:
2395 if ((wndPtr->cbWndExtra - sizeof(LONG_PTR) >= DWLP_DLGPROC) &&
2396 (size == sizeof(LONG_PTR)) && wndPtr->dlgInfo)
2398 WNDPROC *ptr = (WNDPROC *)((char *)wndPtr->wExtra + DWLP_DLGPROC);
2399 retval = (ULONG_PTR)WINPROC_GetProc( *ptr, unicode );
2400 *ptr = WINPROC_AllocProc( (WNDPROC)newval, unicode );
2401 WIN_ReleasePtr( wndPtr );
2402 return retval;
2404 /* fall through */
2405 default:
2406 if (offset < 0 || offset > (int)(wndPtr->cbWndExtra - size))
2408 WARN("Invalid offset %d\n", offset );
2409 WIN_ReleasePtr( wndPtr );
2410 SetLastError( ERROR_INVALID_INDEX );
2411 return 0;
2413 else if (get_win_data( (char *)wndPtr->wExtra + offset, size ) == newval)
2415 /* already set to the same value */
2416 WIN_ReleasePtr( wndPtr );
2417 return newval;
2419 break;
2422 SERVER_START_REQ( set_window_info )
2424 req->handle = wine_server_user_handle( hwnd );
2425 req->extra_offset = -1;
2426 switch(offset)
2428 case GWL_STYLE:
2429 req->flags = SET_WIN_STYLE;
2430 req->style = newval;
2431 break;
2432 case GWL_EXSTYLE:
2433 req->flags = SET_WIN_EXSTYLE;
2434 req->ex_style = newval;
2435 break;
2436 case GWLP_ID:
2437 req->flags = SET_WIN_ID;
2438 req->id = newval;
2439 break;
2440 case GWLP_HINSTANCE:
2441 req->flags = SET_WIN_INSTANCE;
2442 req->instance = wine_server_client_ptr( (void *)newval );
2443 break;
2444 case GWLP_WNDPROC:
2445 req->flags = SET_WIN_UNICODE;
2446 req->is_unicode = (wndPtr->flags & WIN_ISUNICODE) != 0;
2447 break;
2448 case GWLP_USERDATA:
2449 req->flags = SET_WIN_USERDATA;
2450 req->user_data = newval;
2451 break;
2452 default:
2453 req->flags = SET_WIN_EXTRA;
2454 req->extra_offset = offset;
2455 req->extra_size = size;
2456 set_win_data( &req->extra_value, newval, size );
2458 if ((ok = !wine_server_call_err( req )))
2460 switch(offset)
2462 case GWL_STYLE:
2463 wndPtr->dwStyle = newval;
2464 retval = reply->old_style;
2465 break;
2466 case GWL_EXSTYLE:
2467 wndPtr->dwExStyle = newval;
2468 retval = reply->old_ex_style;
2469 break;
2470 case GWLP_ID:
2471 wndPtr->wIDmenu = newval;
2472 retval = reply->old_id;
2473 break;
2474 case GWLP_HINSTANCE:
2475 wndPtr->hInstance = (HINSTANCE)newval;
2476 retval = (ULONG_PTR)wine_server_get_ptr( reply->old_instance );
2477 break;
2478 case GWLP_WNDPROC:
2479 break;
2480 case GWLP_USERDATA:
2481 wndPtr->userdata = newval;
2482 retval = reply->old_user_data;
2483 break;
2484 default:
2485 retval = get_win_data( (char *)wndPtr->wExtra + offset, size );
2486 set_win_data( (char *)wndPtr->wExtra + offset, newval, size );
2487 break;
2491 SERVER_END_REQ;
2493 if ((offset == GWL_STYLE && ((style.styleOld ^ style.styleNew) & WS_VISIBLE)) ||
2494 (offset == GWL_EXSTYLE && ((style.styleOld ^ style.styleNew) & WS_EX_LAYERED)))
2496 made_visible = !(wndPtr->flags & WIN_HIDDEN) && (wndPtr->dwStyle & WS_VISIBLE);
2497 invalidate_dce( wndPtr, NULL );
2499 WIN_ReleasePtr( wndPtr );
2501 if (!ok) return 0;
2503 if (offset == GWL_STYLE || offset == GWL_EXSTYLE)
2505 style.styleOld = retval;
2506 style.styleNew = newval;
2507 USER_Driver->pSetWindowStyle( hwnd, offset, &style );
2508 if (made_visible) update_window_state( hwnd );
2509 SendMessageW( hwnd, WM_STYLECHANGED, offset, (LPARAM)&style );
2512 return retval;
2516 /**********************************************************************
2517 * GetWindowWord (USER32.@)
2519 WORD WINAPI GetWindowWord( HWND hwnd, INT offset )
2521 switch(offset)
2523 case GWLP_ID:
2524 case GWLP_HINSTANCE:
2525 case GWLP_HWNDPARENT:
2526 break;
2527 default:
2528 if (offset < 0)
2530 WARN("Invalid offset %d\n", offset );
2531 SetLastError( ERROR_INVALID_INDEX );
2532 return 0;
2534 break;
2536 return WIN_GetWindowLong( hwnd, offset, sizeof(WORD), FALSE );
2540 /**********************************************************************
2541 * GetWindowLongA (USER32.@)
2543 LONG WINAPI GetWindowLongA( HWND hwnd, INT offset )
2545 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG), FALSE );
2549 /**********************************************************************
2550 * GetWindowLongW (USER32.@)
2552 LONG WINAPI GetWindowLongW( HWND hwnd, INT offset )
2554 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG), TRUE );
2558 /**********************************************************************
2559 * SetWindowWord (USER32.@)
2561 WORD WINAPI SetWindowWord( HWND hwnd, INT offset, WORD newval )
2563 switch(offset)
2565 case GWLP_ID:
2566 case GWLP_HINSTANCE:
2567 case GWLP_HWNDPARENT:
2568 break;
2569 default:
2570 if (offset < 0)
2572 WARN("Invalid offset %d\n", offset );
2573 SetLastError( ERROR_INVALID_INDEX );
2574 return 0;
2576 break;
2578 return WIN_SetWindowLong( hwnd, offset, sizeof(WORD), newval, FALSE );
2582 /**********************************************************************
2583 * SetWindowLongA (USER32.@)
2585 * See SetWindowLongW.
2587 LONG WINAPI DECLSPEC_HOTPATCH SetWindowLongA( HWND hwnd, INT offset, LONG newval )
2589 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG), newval, FALSE );
2593 /**********************************************************************
2594 * SetWindowLongW (USER32.@) Set window attribute
2596 * SetWindowLong() alters one of a window's attributes or sets a 32-bit (long)
2597 * value in a window's extra memory.
2599 * The _hwnd_ parameter specifies the window. is the handle to a
2600 * window that has extra memory. The _newval_ parameter contains the
2601 * new attribute or extra memory value. If positive, the _offset_
2602 * parameter is the byte-addressed location in the window's extra
2603 * memory to set. If negative, _offset_ specifies the window
2604 * attribute to set, and should be one of the following values:
2606 * GWL_EXSTYLE The window's extended window style
2608 * GWL_STYLE The window's window style.
2610 * GWLP_WNDPROC Pointer to the window's window procedure.
2612 * GWLP_HINSTANCE The window's pplication instance handle.
2614 * GWLP_ID The window's identifier.
2616 * GWLP_USERDATA The window's user-specified data.
2618 * If the window is a dialog box, the _offset_ parameter can be one of
2619 * the following values:
2621 * DWLP_DLGPROC The address of the window's dialog box procedure.
2623 * DWLP_MSGRESULT The return value of a message
2624 * that the dialog box procedure processed.
2626 * DWLP_USER Application specific information.
2628 * RETURNS
2630 * If successful, returns the previous value located at _offset_. Otherwise,
2631 * returns 0.
2633 * NOTES
2635 * Extra memory for a window class is specified by a nonzero cbWndExtra
2636 * parameter of the WNDCLASS structure passed to RegisterClass() at the
2637 * time of class creation.
2639 * Using GWL_WNDPROC to set a new window procedure effectively creates
2640 * a window subclass. Use CallWindowProc() in the new windows procedure
2641 * to pass messages to the superclass's window procedure.
2643 * The user data is reserved for use by the application which created
2644 * the window.
2646 * Do not use GWL_STYLE to change the window's WS_DISABLED style;
2647 * instead, call the EnableWindow() function to change the window's
2648 * disabled state.
2650 * Do not use GWL_HWNDPARENT to reset the window's parent, use
2651 * SetParent() instead.
2653 * Win95:
2654 * When offset is GWL_STYLE and the calling app's ver is 4.0,
2655 * it sends WM_STYLECHANGING before changing the settings
2656 * and WM_STYLECHANGED afterwards.
2657 * App ver 4.0 can't use SetWindowLong to change WS_EX_TOPMOST.
2659 LONG WINAPI SetWindowLongW(
2660 HWND hwnd, /* [in] window to alter */
2661 INT offset, /* [in] offset, in bytes, of location to alter */
2662 LONG newval /* [in] new value of location */
2664 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG), newval, TRUE );
2668 /*******************************************************************
2669 * GetWindowTextA (USER32.@)
2671 INT WINAPI GetWindowTextA( HWND hwnd, LPSTR lpString, INT nMaxCount )
2673 WCHAR *buffer;
2675 if (!lpString) return 0;
2677 if (WIN_IsCurrentProcess( hwnd ))
2678 return (INT)SendMessageA( hwnd, WM_GETTEXT, nMaxCount, (LPARAM)lpString );
2680 /* when window belongs to other process, don't send a message */
2681 if (nMaxCount <= 0) return 0;
2682 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, nMaxCount * sizeof(WCHAR) ))) return 0;
2683 get_server_window_text( hwnd, buffer, nMaxCount );
2684 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, lpString, nMaxCount, NULL, NULL ))
2685 lpString[nMaxCount-1] = 0;
2686 HeapFree( GetProcessHeap(), 0, buffer );
2687 return strlen(lpString);
2691 /*******************************************************************
2692 * InternalGetWindowText (USER32.@)
2694 INT WINAPI InternalGetWindowText(HWND hwnd,LPWSTR lpString,INT nMaxCount )
2696 WND *win;
2698 if (nMaxCount <= 0) return 0;
2699 if (!(win = WIN_GetPtr( hwnd ))) return 0;
2700 if (win == WND_DESKTOP) lpString[0] = 0;
2701 else if (win != WND_OTHER_PROCESS)
2703 if (win->text) lstrcpynW( lpString, win->text, nMaxCount );
2704 else lpString[0] = 0;
2705 WIN_ReleasePtr( win );
2707 else
2709 get_server_window_text( hwnd, lpString, nMaxCount );
2711 return strlenW(lpString);
2715 /*******************************************************************
2716 * GetWindowTextW (USER32.@)
2718 INT WINAPI GetWindowTextW( HWND hwnd, LPWSTR lpString, INT nMaxCount )
2720 if (!lpString) return 0;
2722 if (WIN_IsCurrentProcess( hwnd ))
2723 return (INT)SendMessageW( hwnd, WM_GETTEXT, nMaxCount, (LPARAM)lpString );
2725 /* when window belongs to other process, don't send a message */
2726 if (nMaxCount <= 0) return 0;
2727 get_server_window_text( hwnd, lpString, nMaxCount );
2728 return strlenW(lpString);
2732 /*******************************************************************
2733 * SetWindowTextA (USER32.@)
2734 * SetWindowText (USER32.@)
2736 BOOL WINAPI SetWindowTextA( HWND hwnd, LPCSTR lpString )
2738 if (is_broadcast(hwnd))
2740 SetLastError( ERROR_INVALID_PARAMETER );
2741 return FALSE;
2743 if (!WIN_IsCurrentProcess( hwnd ))
2744 WARN( "setting text %s of other process window %p should not use SendMessage\n",
2745 debugstr_a(lpString), hwnd );
2746 return (BOOL)SendMessageA( hwnd, WM_SETTEXT, 0, (LPARAM)lpString );
2750 /*******************************************************************
2751 * SetWindowTextW (USER32.@)
2753 BOOL WINAPI SetWindowTextW( HWND hwnd, LPCWSTR lpString )
2755 if (is_broadcast(hwnd))
2757 SetLastError( ERROR_INVALID_PARAMETER );
2758 return FALSE;
2760 if (!WIN_IsCurrentProcess( hwnd ))
2761 WARN( "setting text %s of other process window %p should not use SendMessage\n",
2762 debugstr_w(lpString), hwnd );
2763 return (BOOL)SendMessageW( hwnd, WM_SETTEXT, 0, (LPARAM)lpString );
2767 /*******************************************************************
2768 * GetWindowTextLengthA (USER32.@)
2770 INT WINAPI GetWindowTextLengthA( HWND hwnd )
2772 return SendMessageA( hwnd, WM_GETTEXTLENGTH, 0, 0 );
2775 /*******************************************************************
2776 * GetWindowTextLengthW (USER32.@)
2778 INT WINAPI GetWindowTextLengthW( HWND hwnd )
2780 return SendMessageW( hwnd, WM_GETTEXTLENGTH, 0, 0 );
2784 /*******************************************************************
2785 * IsWindow (USER32.@)
2787 BOOL WINAPI IsWindow( HWND hwnd )
2789 WND *ptr;
2790 BOOL ret;
2792 if (!(ptr = WIN_GetPtr( hwnd ))) return FALSE;
2793 if (ptr == WND_DESKTOP) return TRUE;
2795 if (ptr != WND_OTHER_PROCESS)
2797 WIN_ReleasePtr( ptr );
2798 return TRUE;
2801 /* check other processes */
2802 SERVER_START_REQ( get_window_info )
2804 req->handle = wine_server_user_handle( hwnd );
2805 ret = !wine_server_call_err( req );
2807 SERVER_END_REQ;
2808 return ret;
2812 /***********************************************************************
2813 * GetWindowThreadProcessId (USER32.@)
2815 DWORD WINAPI GetWindowThreadProcessId( HWND hwnd, LPDWORD process )
2817 WND *ptr;
2818 DWORD tid = 0;
2820 if (!(ptr = WIN_GetPtr( hwnd )))
2822 SetLastError( ERROR_INVALID_WINDOW_HANDLE);
2823 return 0;
2826 if (ptr != WND_OTHER_PROCESS && ptr != WND_DESKTOP)
2828 /* got a valid window */
2829 tid = ptr->tid;
2830 if (process) *process = GetCurrentProcessId();
2831 WIN_ReleasePtr( ptr );
2832 return tid;
2835 /* check other processes */
2836 SERVER_START_REQ( get_window_info )
2838 req->handle = wine_server_user_handle( hwnd );
2839 if (!wine_server_call_err( req ))
2841 tid = (DWORD)reply->tid;
2842 if (process) *process = (DWORD)reply->pid;
2845 SERVER_END_REQ;
2846 return tid;
2850 /*****************************************************************
2851 * GetParent (USER32.@)
2853 HWND WINAPI GetParent( HWND hwnd )
2855 WND *wndPtr;
2856 HWND retvalue = 0;
2858 if (!(wndPtr = WIN_GetPtr( hwnd )))
2860 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2861 return 0;
2863 if (wndPtr == WND_DESKTOP) return 0;
2864 if (wndPtr == WND_OTHER_PROCESS)
2866 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
2867 if (style & (WS_POPUP | WS_CHILD))
2869 SERVER_START_REQ( get_window_tree )
2871 req->handle = wine_server_user_handle( hwnd );
2872 if (!wine_server_call_err( req ))
2874 if (style & WS_POPUP) retvalue = wine_server_ptr_handle( reply->owner );
2875 else if (style & WS_CHILD) retvalue = wine_server_ptr_handle( reply->parent );
2878 SERVER_END_REQ;
2881 else
2883 if (wndPtr->dwStyle & WS_POPUP) retvalue = wndPtr->owner;
2884 else if (wndPtr->dwStyle & WS_CHILD) retvalue = wndPtr->parent;
2885 WIN_ReleasePtr( wndPtr );
2887 return retvalue;
2891 /*****************************************************************
2892 * GetAncestor (USER32.@)
2894 HWND WINAPI GetAncestor( HWND hwnd, UINT type )
2896 WND *win;
2897 HWND *list, ret = 0;
2899 switch(type)
2901 case GA_PARENT:
2902 if (!(win = WIN_GetPtr( hwnd )))
2904 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2905 return 0;
2907 if (win == WND_DESKTOP) return 0;
2908 if (win != WND_OTHER_PROCESS)
2910 ret = win->parent;
2911 WIN_ReleasePtr( win );
2913 else /* need to query the server */
2915 SERVER_START_REQ( get_window_tree )
2917 req->handle = wine_server_user_handle( hwnd );
2918 if (!wine_server_call_err( req )) ret = wine_server_ptr_handle( reply->parent );
2920 SERVER_END_REQ;
2922 break;
2924 case GA_ROOT:
2925 if (!(list = list_window_parents( hwnd ))) return 0;
2927 if (!list[0] || !list[1]) ret = WIN_GetFullHandle( hwnd ); /* top-level window */
2928 else
2930 int count = 2;
2931 while (list[count]) count++;
2932 ret = list[count - 2]; /* get the one before the desktop */
2934 HeapFree( GetProcessHeap(), 0, list );
2935 break;
2937 case GA_ROOTOWNER:
2938 if (is_desktop_window( hwnd )) return 0;
2939 ret = WIN_GetFullHandle( hwnd );
2940 for (;;)
2942 HWND parent = GetParent( ret );
2943 if (!parent) break;
2944 ret = parent;
2946 break;
2948 return ret;
2952 /*****************************************************************
2953 * SetParent (USER32.@)
2955 HWND WINAPI SetParent( HWND hwnd, HWND parent )
2957 HWND full_handle;
2958 HWND old_parent = 0;
2959 BOOL was_visible;
2960 WND *wndPtr;
2961 POINT pt;
2962 BOOL ret;
2964 if (is_broadcast(hwnd) || is_broadcast(parent))
2966 SetLastError(ERROR_INVALID_PARAMETER);
2967 return 0;
2970 if (!parent) parent = GetDesktopWindow();
2971 else if (parent == HWND_MESSAGE) parent = get_hwnd_message_parent();
2972 else parent = WIN_GetFullHandle( parent );
2974 if (!IsWindow( parent ))
2976 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2977 return 0;
2980 /* Some applications try to set a child as a parent */
2981 if (IsChild(hwnd, parent))
2983 SetLastError( ERROR_INVALID_PARAMETER );
2984 return 0;
2987 if (!(full_handle = WIN_IsCurrentThread( hwnd )))
2988 return (HWND)SendMessageW( hwnd, WM_WINE_SETPARENT, (WPARAM)parent, 0 );
2990 if (full_handle == parent)
2992 SetLastError( ERROR_INVALID_PARAMETER );
2993 return 0;
2996 /* Windows hides the window first, then shows it again
2997 * including the WM_SHOWWINDOW messages and all */
2998 was_visible = ShowWindow( hwnd, SW_HIDE );
3000 wndPtr = WIN_GetPtr( hwnd );
3001 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return 0;
3003 pt.x = wndPtr->rectWindow.left;
3004 pt.y = wndPtr->rectWindow.top;
3006 SERVER_START_REQ( set_parent )
3008 req->handle = wine_server_user_handle( hwnd );
3009 req->parent = wine_server_user_handle( parent );
3010 if ((ret = !wine_server_call( req )))
3012 old_parent = wine_server_ptr_handle( reply->old_parent );
3013 wndPtr->parent = parent = wine_server_ptr_handle( reply->full_parent );
3017 SERVER_END_REQ;
3018 WIN_ReleasePtr( wndPtr );
3019 if (!ret) return 0;
3021 USER_Driver->pSetParent( full_handle, parent, old_parent );
3023 /* SetParent additionally needs to make hwnd the topmost window
3024 in the x-order and send the expected WM_WINDOWPOSCHANGING and
3025 WM_WINDOWPOSCHANGED notification messages.
3027 SetWindowPos( hwnd, HWND_TOP, pt.x, pt.y, 0, 0, SWP_NOSIZE );
3029 if (was_visible) ShowWindow( hwnd, SW_SHOW );
3031 return old_parent;
3035 /*******************************************************************
3036 * IsChild (USER32.@)
3038 BOOL WINAPI IsChild( HWND parent, HWND child )
3040 HWND *list = list_window_parents( child );
3041 int i;
3042 BOOL ret;
3044 if (!list) return FALSE;
3045 parent = WIN_GetFullHandle( parent );
3046 for (i = 0; list[i]; i++) if (list[i] == parent) break;
3047 ret = list[i] && list[i+1];
3048 HeapFree( GetProcessHeap(), 0, list );
3049 return ret;
3053 /***********************************************************************
3054 * IsWindowVisible (USER32.@)
3056 BOOL WINAPI IsWindowVisible( HWND hwnd )
3058 HWND *list;
3059 BOOL retval = TRUE;
3060 int i;
3062 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)) return FALSE;
3063 if (!(list = list_window_parents( hwnd ))) return TRUE;
3064 if (list[0])
3066 for (i = 0; list[i+1]; i++)
3067 if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_VISIBLE)) break;
3068 retval = !list[i+1] && (list[i] == GetDesktopWindow()); /* top message window isn't visible */
3070 HeapFree( GetProcessHeap(), 0, list );
3071 return retval;
3075 /***********************************************************************
3076 * WIN_IsWindowDrawable
3078 * hwnd is drawable when it is visible, all parents are not
3079 * minimized, and it is itself not minimized unless we are
3080 * trying to draw its default class icon.
3082 BOOL WIN_IsWindowDrawable( HWND hwnd, BOOL icon )
3084 HWND *list;
3085 BOOL retval = TRUE;
3086 int i;
3087 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
3089 if (!(style & WS_VISIBLE)) return FALSE;
3090 if ((style & WS_MINIMIZE) && icon && GetClassLongPtrW( hwnd, GCLP_HICON )) return FALSE;
3092 if (!(list = list_window_parents( hwnd ))) return TRUE;
3093 if (list[0])
3095 for (i = 0; list[i+1]; i++)
3096 if ((GetWindowLongW( list[i], GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != WS_VISIBLE)
3097 break;
3098 retval = !list[i+1] && (list[i] == GetDesktopWindow()); /* top message window isn't visible */
3100 HeapFree( GetProcessHeap(), 0, list );
3101 return retval;
3105 /*******************************************************************
3106 * GetTopWindow (USER32.@)
3108 HWND WINAPI GetTopWindow( HWND hwnd )
3110 if (!hwnd) hwnd = GetDesktopWindow();
3111 return GetWindow( hwnd, GW_CHILD );
3115 /*******************************************************************
3116 * GetWindow (USER32.@)
3118 HWND WINAPI GetWindow( HWND hwnd, UINT rel )
3120 HWND retval = 0;
3122 if (rel == GW_OWNER) /* this one may be available locally */
3124 WND *wndPtr = WIN_GetPtr( hwnd );
3125 if (!wndPtr)
3127 SetLastError( ERROR_INVALID_HANDLE );
3128 return 0;
3130 if (wndPtr == WND_DESKTOP) return 0;
3131 if (wndPtr != WND_OTHER_PROCESS)
3133 retval = wndPtr->owner;
3134 WIN_ReleasePtr( wndPtr );
3135 return retval;
3137 /* else fall through to server call */
3140 SERVER_START_REQ( get_window_tree )
3142 req->handle = wine_server_user_handle( hwnd );
3143 if (!wine_server_call_err( req ))
3145 switch(rel)
3147 case GW_HWNDFIRST:
3148 retval = wine_server_ptr_handle( reply->first_sibling );
3149 break;
3150 case GW_HWNDLAST:
3151 retval = wine_server_ptr_handle( reply->last_sibling );
3152 break;
3153 case GW_HWNDNEXT:
3154 retval = wine_server_ptr_handle( reply->next_sibling );
3155 break;
3156 case GW_HWNDPREV:
3157 retval = wine_server_ptr_handle( reply->prev_sibling );
3158 break;
3159 case GW_OWNER:
3160 retval = wine_server_ptr_handle( reply->owner );
3161 break;
3162 case GW_CHILD:
3163 retval = wine_server_ptr_handle( reply->first_child );
3164 break;
3168 SERVER_END_REQ;
3169 return retval;
3173 /*******************************************************************
3174 * ShowOwnedPopups (USER32.@)
3176 BOOL WINAPI ShowOwnedPopups( HWND owner, BOOL fShow )
3178 int count = 0;
3179 WND *pWnd;
3180 HWND *win_array = WIN_ListChildren( GetDesktopWindow() );
3182 if (!win_array) return TRUE;
3184 while (win_array[count]) count++;
3185 while (--count >= 0)
3187 if (GetWindow( win_array[count], GW_OWNER ) != owner) continue;
3188 if (!(pWnd = WIN_GetPtr( win_array[count] ))) continue;
3189 if (pWnd == WND_OTHER_PROCESS) continue;
3190 if (fShow)
3192 if (pWnd->flags & WIN_NEEDS_SHOW_OWNEDPOPUP)
3194 WIN_ReleasePtr( pWnd );
3195 /* In Windows, ShowOwnedPopups(TRUE) generates
3196 * WM_SHOWWINDOW messages with SW_PARENTOPENING,
3197 * regardless of the state of the owner
3199 SendMessageW(win_array[count], WM_SHOWWINDOW, SW_SHOWNORMAL, SW_PARENTOPENING);
3200 continue;
3203 else
3205 if (pWnd->dwStyle & WS_VISIBLE)
3207 WIN_ReleasePtr( pWnd );
3208 /* In Windows, ShowOwnedPopups(FALSE) generates
3209 * WM_SHOWWINDOW messages with SW_PARENTCLOSING,
3210 * regardless of the state of the owner
3212 SendMessageW(win_array[count], WM_SHOWWINDOW, SW_HIDE, SW_PARENTCLOSING);
3213 continue;
3216 WIN_ReleasePtr( pWnd );
3218 HeapFree( GetProcessHeap(), 0, win_array );
3219 return TRUE;
3223 /*******************************************************************
3224 * GetLastActivePopup (USER32.@)
3226 HWND WINAPI GetLastActivePopup( HWND hwnd )
3228 HWND retval = hwnd;
3230 SERVER_START_REQ( get_window_info )
3232 req->handle = wine_server_user_handle( hwnd );
3233 if (!wine_server_call_err( req )) retval = wine_server_ptr_handle( reply->last_active );
3235 SERVER_END_REQ;
3236 return retval;
3240 /*******************************************************************
3241 * WIN_ListChildren
3243 * Build an array of the children of a given window. The array must be
3244 * freed with HeapFree. Returns NULL when no windows are found.
3246 HWND *WIN_ListChildren( HWND hwnd )
3248 if (!hwnd)
3250 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3251 return NULL;
3253 return list_window_children( 0, hwnd, NULL, 0 );
3257 /*******************************************************************
3258 * EnumWindows (USER32.@)
3260 BOOL WINAPI EnumWindows( WNDENUMPROC lpEnumFunc, LPARAM lParam )
3262 HWND *list;
3263 BOOL ret = TRUE;
3264 int i;
3266 USER_CheckNotLock();
3268 /* We have to build a list of all windows first, to avoid */
3269 /* unpleasant side-effects, for instance if the callback */
3270 /* function changes the Z-order of the windows. */
3272 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return TRUE;
3274 /* Now call the callback function for every window */
3276 for (i = 0; list[i]; i++)
3278 /* Make sure that the window still exists */
3279 if (!IsWindow( list[i] )) continue;
3280 if (!(ret = lpEnumFunc( list[i], lParam ))) break;
3282 HeapFree( GetProcessHeap(), 0, list );
3283 return ret;
3287 /**********************************************************************
3288 * EnumThreadWindows (USER32.@)
3290 BOOL WINAPI EnumThreadWindows( DWORD id, WNDENUMPROC func, LPARAM lParam )
3292 HWND *list;
3293 int i;
3294 BOOL ret = TRUE;
3296 USER_CheckNotLock();
3298 if (!(list = list_window_children( 0, GetDesktopWindow(), NULL, id ))) return TRUE;
3300 /* Now call the callback function for every window */
3302 for (i = 0; list[i]; i++)
3303 if (!(ret = func( list[i], lParam ))) break;
3304 HeapFree( GetProcessHeap(), 0, list );
3305 return ret;
3309 /***********************************************************************
3310 * EnumDesktopWindows (USER32.@)
3312 BOOL WINAPI EnumDesktopWindows( HDESK desktop, WNDENUMPROC func, LPARAM lparam )
3314 HWND *list;
3315 int i;
3317 USER_CheckNotLock();
3319 if (!(list = list_window_children( desktop, 0, NULL, 0 ))) return TRUE;
3321 for (i = 0; list[i]; i++)
3322 if (!func( list[i], lparam )) break;
3323 HeapFree( GetProcessHeap(), 0, list );
3324 return TRUE;
3328 /**********************************************************************
3329 * WIN_EnumChildWindows
3331 * Helper function for EnumChildWindows().
3333 static BOOL WIN_EnumChildWindows( HWND *list, WNDENUMPROC func, LPARAM lParam )
3335 HWND *childList;
3336 BOOL ret = FALSE;
3338 for ( ; *list; list++)
3340 /* Make sure that the window still exists */
3341 if (!IsWindow( *list )) continue;
3342 /* Build children list first */
3343 childList = WIN_ListChildren( *list );
3345 ret = func( *list, lParam );
3347 if (childList)
3349 if (ret) ret = WIN_EnumChildWindows( childList, func, lParam );
3350 HeapFree( GetProcessHeap(), 0, childList );
3352 if (!ret) return FALSE;
3354 return TRUE;
3358 /**********************************************************************
3359 * EnumChildWindows (USER32.@)
3361 BOOL WINAPI EnumChildWindows( HWND parent, WNDENUMPROC func, LPARAM lParam )
3363 HWND *list;
3364 BOOL ret;
3366 USER_CheckNotLock();
3368 if (!(list = WIN_ListChildren( parent ))) return FALSE;
3369 ret = WIN_EnumChildWindows( list, func, lParam );
3370 HeapFree( GetProcessHeap(), 0, list );
3371 return ret;
3375 /*******************************************************************
3376 * AnyPopup (USER32.@)
3378 BOOL WINAPI AnyPopup(void)
3380 int i;
3381 BOOL retvalue;
3382 HWND *list = WIN_ListChildren( GetDesktopWindow() );
3384 if (!list) return FALSE;
3385 for (i = 0; list[i]; i++)
3387 if (IsWindowVisible( list[i] ) && GetWindow( list[i], GW_OWNER )) break;
3389 retvalue = (list[i] != 0);
3390 HeapFree( GetProcessHeap(), 0, list );
3391 return retvalue;
3395 /*******************************************************************
3396 * FlashWindow (USER32.@)
3398 BOOL WINAPI FlashWindow( HWND hWnd, BOOL bInvert )
3400 WND *wndPtr;
3402 TRACE("%p\n", hWnd);
3404 if (IsIconic( hWnd ))
3406 RedrawWindow( hWnd, 0, 0, RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW | RDW_FRAME );
3408 wndPtr = WIN_GetPtr(hWnd);
3409 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
3410 if (bInvert && !(wndPtr->flags & WIN_NCACTIVATED))
3412 wndPtr->flags |= WIN_NCACTIVATED;
3414 else
3416 wndPtr->flags &= ~WIN_NCACTIVATED;
3418 WIN_ReleasePtr( wndPtr );
3419 return TRUE;
3421 else
3423 WPARAM wparam;
3425 wndPtr = WIN_GetPtr(hWnd);
3426 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
3427 hWnd = wndPtr->obj.handle; /* make it a full handle */
3429 if (bInvert) wparam = !(wndPtr->flags & WIN_NCACTIVATED);
3430 else wparam = (hWnd == GetForegroundWindow());
3432 WIN_ReleasePtr( wndPtr );
3433 SendMessageW( hWnd, WM_NCACTIVATE, wparam, 0 );
3434 return wparam;
3438 /*******************************************************************
3439 * FlashWindowEx (USER32.@)
3441 BOOL WINAPI FlashWindowEx( PFLASHWINFO pfwi )
3443 FIXME("%p\n", pfwi);
3444 return TRUE;
3447 /*******************************************************************
3448 * GetWindowContextHelpId (USER32.@)
3450 DWORD WINAPI GetWindowContextHelpId( HWND hwnd )
3452 DWORD retval;
3453 WND *wnd = WIN_GetPtr( hwnd );
3454 if (!wnd || wnd == WND_DESKTOP) return 0;
3455 if (wnd == WND_OTHER_PROCESS)
3457 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
3458 return 0;
3460 retval = wnd->helpContext;
3461 WIN_ReleasePtr( wnd );
3462 return retval;
3466 /*******************************************************************
3467 * SetWindowContextHelpId (USER32.@)
3469 BOOL WINAPI SetWindowContextHelpId( HWND hwnd, DWORD id )
3471 WND *wnd = WIN_GetPtr( hwnd );
3472 if (!wnd || wnd == WND_DESKTOP) return FALSE;
3473 if (wnd == WND_OTHER_PROCESS)
3475 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
3476 return 0;
3478 wnd->helpContext = id;
3479 WIN_ReleasePtr( wnd );
3480 return TRUE;
3484 /*******************************************************************
3485 * DragDetect (USER32.@)
3487 BOOL WINAPI DragDetect( HWND hWnd, POINT pt )
3489 MSG msg;
3490 RECT rect;
3491 WORD wDragWidth = GetSystemMetrics(SM_CXDRAG);
3492 WORD wDragHeight= GetSystemMetrics(SM_CYDRAG);
3494 rect.left = pt.x - wDragWidth;
3495 rect.right = pt.x + wDragWidth;
3497 rect.top = pt.y - wDragHeight;
3498 rect.bottom = pt.y + wDragHeight;
3500 SetCapture(hWnd);
3502 while(1)
3504 while (PeekMessageW( &msg, 0, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE ))
3506 if( msg.message == WM_LBUTTONUP )
3508 ReleaseCapture();
3509 return 0;
3511 if( msg.message == WM_MOUSEMOVE )
3513 POINT tmp;
3514 tmp.x = (short)LOWORD(msg.lParam);
3515 tmp.y = (short)HIWORD(msg.lParam);
3516 if( !PtInRect( &rect, tmp ))
3518 ReleaseCapture();
3519 return 1;
3523 WaitMessage();
3525 return 0;
3528 /******************************************************************************
3529 * GetWindowModuleFileNameA (USER32.@)
3531 UINT WINAPI GetWindowModuleFileNameA( HWND hwnd, LPSTR module, UINT size )
3533 WND *win;
3534 HINSTANCE hinst;
3536 TRACE( "%p, %p, %u\n", hwnd, module, size );
3538 win = WIN_GetPtr( hwnd );
3539 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP)
3541 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3542 return 0;
3544 hinst = win->hInstance;
3545 WIN_ReleasePtr( win );
3547 return GetModuleFileNameA( hinst, module, size );
3550 /******************************************************************************
3551 * GetWindowModuleFileNameW (USER32.@)
3553 UINT WINAPI GetWindowModuleFileNameW( HWND hwnd, LPWSTR module, UINT size )
3555 WND *win;
3556 HINSTANCE hinst;
3558 TRACE( "%p, %p, %u\n", hwnd, module, size );
3560 win = WIN_GetPtr( hwnd );
3561 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP)
3563 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3564 return 0;
3566 hinst = win->hInstance;
3567 WIN_ReleasePtr( win );
3569 return GetModuleFileNameW( hinst, module, size );
3572 /******************************************************************************
3573 * GetWindowInfo (USER32.@)
3575 * Note: tests show that Windows doesn't check cbSize of the structure.
3577 BOOL WINAPI DECLSPEC_HOTPATCH GetWindowInfo( HWND hwnd, PWINDOWINFO pwi)
3579 if (!pwi) return FALSE;
3580 if (!WIN_GetRectangles( hwnd, COORDS_SCREEN, &pwi->rcWindow, &pwi->rcClient )) return FALSE;
3582 pwi->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
3583 pwi->dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
3584 pwi->dwWindowStatus = ((GetActiveWindow() == hwnd) ? WS_ACTIVECAPTION : 0);
3586 pwi->cxWindowBorders = pwi->rcClient.left - pwi->rcWindow.left;
3587 pwi->cyWindowBorders = pwi->rcWindow.bottom - pwi->rcClient.bottom;
3589 pwi->atomWindowType = GetClassLongW( hwnd, GCW_ATOM );
3590 pwi->wCreatorVersion = 0x0400;
3592 return TRUE;
3595 /******************************************************************************
3596 * SwitchDesktop (USER32.@)
3598 * NOTES: Sets the current input or interactive desktop.
3600 BOOL WINAPI SwitchDesktop( HDESK hDesktop)
3602 FIXME("(hwnd %p) stub!\n", hDesktop);
3603 return TRUE;
3607 /***********************************************************************
3608 * __wine_set_pixel_format
3610 BOOL CDECL __wine_set_pixel_format( HWND hwnd, int format )
3612 WND *win = WIN_GetPtr( hwnd );
3614 if (!win || win == WND_DESKTOP || win == WND_OTHER_PROCESS)
3616 WARN( "setting format %d on win %p not supported\n", format, hwnd );
3617 return FALSE;
3619 win->pixel_format = format;
3620 WIN_ReleasePtr( win );
3622 update_window_state( hwnd );
3623 return TRUE;
3627 /*****************************************************************************
3628 * SetLayeredWindowAttributes (USER32.@)
3630 BOOL WINAPI SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
3632 BOOL ret;
3634 TRACE("(%p,%08x,%d,%x): stub!\n", hwnd, key, alpha, flags);
3636 SERVER_START_REQ( set_window_layered_info )
3638 req->handle = wine_server_user_handle( hwnd );
3639 req->color_key = key;
3640 req->alpha = alpha;
3641 req->flags = flags;
3642 ret = !wine_server_call_err( req );
3644 SERVER_END_REQ;
3646 if (ret) USER_Driver->pSetLayeredWindowAttributes( hwnd, key, alpha, flags );
3648 return ret;
3652 /*****************************************************************************
3653 * GetLayeredWindowAttributes (USER32.@)
3655 BOOL WINAPI GetLayeredWindowAttributes( HWND hwnd, COLORREF *key, BYTE *alpha, DWORD *flags )
3657 BOOL ret;
3659 SERVER_START_REQ( get_window_layered_info )
3661 req->handle = wine_server_user_handle( hwnd );
3662 if ((ret = !wine_server_call_err( req )))
3664 if (key) *key = reply->color_key;
3665 if (alpha) *alpha = reply->alpha;
3666 if (flags) *flags = reply->flags;
3669 SERVER_END_REQ;
3671 return ret;
3675 /*****************************************************************************
3676 * UpdateLayeredWindowIndirect (USER32.@)
3678 BOOL WINAPI UpdateLayeredWindowIndirect( HWND hwnd, const UPDATELAYEREDWINDOWINFO *info )
3680 DWORD flags = SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW;
3681 RECT window_rect, client_rect;
3682 SIZE offset;
3684 if (!info ||
3685 info->cbSize != sizeof(*info) ||
3686 info->dwFlags & ~(ULW_COLORKEY | ULW_ALPHA | ULW_OPAQUE | ULW_EX_NORESIZE) ||
3687 !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED) ||
3688 GetLayeredWindowAttributes( hwnd, NULL, NULL, NULL ))
3690 SetLastError( ERROR_INVALID_PARAMETER );
3691 return FALSE;
3694 WIN_GetRectangles( hwnd, COORDS_PARENT, &window_rect, &client_rect );
3696 if (info->pptDst)
3698 offset.cx = info->pptDst->x - window_rect.left;
3699 offset.cy = info->pptDst->y - window_rect.top;
3700 OffsetRect( &client_rect, offset.cx, offset.cy );
3701 OffsetRect( &window_rect, offset.cx, offset.cy );
3702 flags &= ~SWP_NOMOVE;
3704 if (info->psize)
3706 offset.cx = info->psize->cx - (window_rect.right - window_rect.left);
3707 offset.cy = info->psize->cy - (window_rect.bottom - window_rect.top);
3708 if (info->psize->cx <= 0 || info->psize->cy <= 0)
3710 SetLastError( ERROR_INVALID_PARAMETER );
3711 return FALSE;
3713 if ((info->dwFlags & ULW_EX_NORESIZE) && (offset.cx || offset.cy))
3715 SetLastError( ERROR_INCORRECT_SIZE );
3716 return FALSE;
3718 client_rect.right += offset.cx;
3719 client_rect.bottom += offset.cy;
3720 window_rect.right += offset.cx;
3721 window_rect.bottom += offset.cy;
3722 flags &= ~SWP_NOSIZE;
3725 TRACE( "window %p win %s client %s\n", hwnd,
3726 wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(&client_rect) );
3728 if (!USER_Driver->pUpdateLayeredWindow( hwnd, info, &window_rect )) return FALSE;
3730 set_window_pos( hwnd, 0, flags, &window_rect, &client_rect, NULL );
3731 return TRUE;
3735 /*****************************************************************************
3736 * UpdateLayeredWindow (USER32.@)
3738 BOOL WINAPI UpdateLayeredWindow( HWND hwnd, HDC hdcDst, POINT *pptDst, SIZE *psize,
3739 HDC hdcSrc, POINT *pptSrc, COLORREF crKey, BLENDFUNCTION *pblend,
3740 DWORD flags)
3742 UPDATELAYEREDWINDOWINFO info;
3744 if (flags & ULW_EX_NORESIZE) /* only valid for UpdateLayeredWindowIndirect */
3746 SetLastError( ERROR_INVALID_PARAMETER );
3747 return FALSE;
3749 info.cbSize = sizeof(info);
3750 info.hdcDst = hdcDst;
3751 info.pptDst = pptDst;
3752 info.psize = psize;
3753 info.hdcSrc = hdcSrc;
3754 info.pptSrc = pptSrc;
3755 info.crKey = crKey;
3756 info.pblend = pblend;
3757 info.dwFlags = flags;
3758 info.prcDirty = NULL;
3759 return UpdateLayeredWindowIndirect( hwnd, &info );
3763 /******************************************************************************
3764 * GetProcessDefaultLayout [USER32.@]
3766 * Gets the default layout for parentless windows.
3768 BOOL WINAPI GetProcessDefaultLayout( DWORD *layout )
3770 if (!layout)
3772 SetLastError( ERROR_NOACCESS );
3773 return FALSE;
3775 if (process_layout == ~0u)
3777 static const WCHAR translationW[] = { '\\','V','a','r','F','i','l','e','I','n','f','o',
3778 '\\','T','r','a','n','s','l','a','t','i','o','n', 0 };
3779 static const WCHAR filedescW[] = { '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
3780 '\\','%','0','4','x','%','0','4','x',
3781 '\\','F','i','l','e','D','e','s','c','r','i','p','t','i','o','n',0 };
3782 WCHAR *str, buffer[MAX_PATH];
3783 DWORD i, len, version_layout = 0;
3784 DWORD user_lang = GetUserDefaultLangID();
3785 DWORD *languages;
3786 void *data = NULL;
3788 GetModuleFileNameW( 0, buffer, MAX_PATH );
3789 if (!(len = GetFileVersionInfoSizeW( buffer, NULL ))) goto done;
3790 if (!(data = HeapAlloc( GetProcessHeap(), 0, len ))) goto done;
3791 if (!GetFileVersionInfoW( buffer, 0, len, data )) goto done;
3792 if (!VerQueryValueW( data, translationW, (void **)&languages, &len ) || !len) goto done;
3794 len /= sizeof(DWORD);
3795 for (i = 0; i < len; i++) if (LOWORD(languages[i]) == user_lang) break;
3796 if (i == len) /* try neutral language */
3797 for (i = 0; i < len; i++)
3798 if (LOWORD(languages[i]) == MAKELANGID( PRIMARYLANGID(user_lang), SUBLANG_NEUTRAL )) break;
3799 if (i == len) i = 0; /* default to the first one */
3801 sprintfW( buffer, filedescW, LOWORD(languages[i]), HIWORD(languages[i]) );
3802 if (!VerQueryValueW( data, buffer, (void **)&str, &len )) goto done;
3803 TRACE( "found description %s\n", debugstr_w( str ));
3804 if (str[0] == 0x200e && str[1] == 0x200e) version_layout = LAYOUT_RTL;
3806 done:
3807 HeapFree( GetProcessHeap(), 0, data );
3808 process_layout = version_layout;
3810 *layout = process_layout;
3811 return TRUE;
3815 /******************************************************************************
3816 * SetProcessDefaultLayout [USER32.@]
3818 * Sets the default layout for parentless windows.
3820 BOOL WINAPI SetProcessDefaultLayout( DWORD layout )
3822 process_layout = layout;
3823 return TRUE;
3827 /* 64bit versions */
3829 #ifdef GetWindowLongPtrW
3830 #undef GetWindowLongPtrW
3831 #endif
3833 #ifdef GetWindowLongPtrA
3834 #undef GetWindowLongPtrA
3835 #endif
3837 #ifdef SetWindowLongPtrW
3838 #undef SetWindowLongPtrW
3839 #endif
3841 #ifdef SetWindowLongPtrA
3842 #undef SetWindowLongPtrA
3843 #endif
3845 /*****************************************************************************
3846 * GetWindowLongPtrW (USER32.@)
3848 LONG_PTR WINAPI GetWindowLongPtrW( HWND hwnd, INT offset )
3850 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG_PTR), TRUE );
3853 /*****************************************************************************
3854 * GetWindowLongPtrA (USER32.@)
3856 LONG_PTR WINAPI GetWindowLongPtrA( HWND hwnd, INT offset )
3858 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG_PTR), FALSE );
3861 /*****************************************************************************
3862 * SetWindowLongPtrW (USER32.@)
3864 LONG_PTR WINAPI SetWindowLongPtrW( HWND hwnd, INT offset, LONG_PTR newval )
3866 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG_PTR), newval, TRUE );
3869 /*****************************************************************************
3870 * SetWindowLongPtrA (USER32.@)
3872 LONG_PTR WINAPI SetWindowLongPtrA( HWND hwnd, INT offset, LONG_PTR newval )
3874 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG_PTR), newval, FALSE );