windowscodecs/tests: Handle failure to create TIFF decoder.
[wine.git] / dlls / user32 / win.c
blobe3650f5f9e9eec04ca7005cce6d8eeb7dd2960b7
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 made_visible = (style.styleNew & WS_VISIBLE) != 0;
778 invalidate_dce( win, NULL );
780 WIN_ReleasePtr( win );
782 if (!ok) return 0;
784 USER_Driver->pSetWindowStyle( hwnd, GWL_STYLE, &style );
785 if (made_visible) update_window_state( hwnd );
787 return style.styleOld;
791 /***********************************************************************
792 * WIN_GetRectangles
794 * Get the window and client rectangles.
796 BOOL WIN_GetRectangles( HWND hwnd, enum coords_relative relative, RECT *rectWindow, RECT *rectClient )
798 WND *win = WIN_GetPtr( hwnd );
799 BOOL ret = TRUE;
801 if (!win)
803 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
804 return FALSE;
806 if (win == WND_DESKTOP)
808 RECT rect;
809 rect.left = rect.top = 0;
810 if (hwnd == get_hwnd_message_parent())
812 rect.right = 100;
813 rect.bottom = 100;
815 else
817 rect.right = GetSystemMetrics(SM_CXSCREEN);
818 rect.bottom = GetSystemMetrics(SM_CYSCREEN);
820 if (rectWindow) *rectWindow = rect;
821 if (rectClient) *rectClient = rect;
822 return TRUE;
824 if (win != WND_OTHER_PROCESS)
826 RECT window_rect = win->rectWindow, client_rect = win->rectClient;
828 switch (relative)
830 case COORDS_CLIENT:
831 OffsetRect( &window_rect, -win->rectClient.left, -win->rectClient.top );
832 OffsetRect( &client_rect, -win->rectClient.left, -win->rectClient.top );
833 if (win->dwExStyle & WS_EX_LAYOUTRTL)
834 mirror_rect( &win->rectClient, &window_rect );
835 break;
836 case COORDS_WINDOW:
837 OffsetRect( &window_rect, -win->rectWindow.left, -win->rectWindow.top );
838 OffsetRect( &client_rect, -win->rectWindow.left, -win->rectWindow.top );
839 if (win->dwExStyle & WS_EX_LAYOUTRTL)
840 mirror_rect( &win->rectWindow, &client_rect );
841 break;
842 case COORDS_PARENT:
843 if (win->parent)
845 WND *parent = WIN_GetPtr( win->parent );
846 if (parent == WND_DESKTOP) break;
847 if (!parent || parent == WND_OTHER_PROCESS)
849 WIN_ReleasePtr( win );
850 goto other_process;
852 if (parent->flags & WIN_CHILDREN_MOVED)
854 WIN_ReleasePtr( parent );
855 WIN_ReleasePtr( win );
856 goto other_process;
858 if (parent->dwExStyle & WS_EX_LAYOUTRTL)
860 mirror_rect( &parent->rectClient, &window_rect );
861 mirror_rect( &parent->rectClient, &client_rect );
863 WIN_ReleasePtr( parent );
865 break;
866 case COORDS_SCREEN:
867 while (win->parent)
869 WND *parent = WIN_GetPtr( win->parent );
870 if (parent == WND_DESKTOP) break;
871 if (!parent || parent == WND_OTHER_PROCESS)
873 WIN_ReleasePtr( win );
874 goto other_process;
876 WIN_ReleasePtr( win );
877 if (parent->flags & WIN_CHILDREN_MOVED)
879 WIN_ReleasePtr( parent );
880 goto other_process;
882 win = parent;
883 if (win->parent)
885 OffsetRect( &window_rect, win->rectClient.left, win->rectClient.top );
886 OffsetRect( &client_rect, win->rectClient.left, win->rectClient.top );
889 break;
891 if (rectWindow) *rectWindow = window_rect;
892 if (rectClient) *rectClient = client_rect;
893 WIN_ReleasePtr( win );
894 return TRUE;
897 other_process:
898 SERVER_START_REQ( get_window_rectangles )
900 req->handle = wine_server_user_handle( hwnd );
901 req->relative = relative;
902 if ((ret = !wine_server_call_err( req )))
904 if (rectWindow)
906 rectWindow->left = reply->window.left;
907 rectWindow->top = reply->window.top;
908 rectWindow->right = reply->window.right;
909 rectWindow->bottom = reply->window.bottom;
911 if (rectClient)
913 rectClient->left = reply->client.left;
914 rectClient->top = reply->client.top;
915 rectClient->right = reply->client.right;
916 rectClient->bottom = reply->client.bottom;
920 SERVER_END_REQ;
921 return ret;
925 /***********************************************************************
926 * WIN_DestroyWindow
928 * Destroy storage associated to a window. "Internals" p.358
930 LRESULT WIN_DestroyWindow( HWND hwnd )
932 WND *wndPtr;
933 HWND *list;
934 HMENU menu = 0, sys_menu;
935 HWND icon_title;
936 struct window_surface *surface;
938 TRACE("%p\n", hwnd );
940 /* free child windows */
941 if ((list = WIN_ListChildren( hwnd )))
943 int i;
944 for (i = 0; list[i]; i++)
946 if (WIN_IsCurrentThread( list[i] )) WIN_DestroyWindow( list[i] );
947 else SendMessageW( list[i], WM_WINE_DESTROYWINDOW, 0, 0 );
949 HeapFree( GetProcessHeap(), 0, list );
952 /* Unlink now so we won't bother with the children later on */
953 SERVER_START_REQ( set_parent )
955 req->handle = wine_server_user_handle( hwnd );
956 req->parent = 0;
957 wine_server_call( req );
959 SERVER_END_REQ;
962 * Send the WM_NCDESTROY to the window being destroyed.
964 SendMessageW( hwnd, WM_NCDESTROY, 0, 0 );
966 /* FIXME: do we need to fake QS_MOUSEMOVE wakebit? */
968 /* free resources associated with the window */
970 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
971 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
972 menu = (HMENU)wndPtr->wIDmenu;
973 sys_menu = wndPtr->hSysMenu;
974 free_dce( wndPtr->dce, hwnd );
975 wndPtr->dce = NULL;
976 icon_title = wndPtr->icon_title;
977 HeapFree( GetProcessHeap(), 0, wndPtr->text );
978 wndPtr->text = NULL;
979 HeapFree( GetProcessHeap(), 0, wndPtr->pScroll );
980 wndPtr->pScroll = NULL;
981 DestroyIcon( wndPtr->hIconSmall2 );
982 surface = wndPtr->surface;
983 wndPtr->surface = NULL;
984 WIN_ReleasePtr( wndPtr );
986 if (icon_title) DestroyWindow( icon_title );
987 if (menu) DestroyMenu( menu );
988 if (sys_menu) DestroyMenu( sys_menu );
989 if (surface)
991 register_window_surface( surface, NULL );
992 window_surface_release( surface );
995 USER_Driver->pDestroyWindow( hwnd );
997 free_window_handle( hwnd );
998 return 0;
1002 /***********************************************************************
1003 * destroy_thread_window
1005 * Destroy a window upon exit of its thread.
1007 static void destroy_thread_window( HWND hwnd )
1009 WND *wndPtr;
1010 HWND *list;
1011 HMENU menu = 0, sys_menu = 0;
1012 struct window_surface *surface = NULL;
1013 WORD index;
1015 /* free child windows */
1017 if ((list = WIN_ListChildren( hwnd )))
1019 int i;
1020 for (i = 0; list[i]; i++)
1022 if (WIN_IsCurrentThread( list[i] )) destroy_thread_window( list[i] );
1023 else SendMessageW( list[i], WM_WINE_DESTROYWINDOW, 0, 0 );
1025 HeapFree( GetProcessHeap(), 0, list );
1028 /* destroy the client-side storage */
1030 index = USER_HANDLE_TO_INDEX(hwnd);
1031 if (index >= NB_USER_HANDLES) return;
1032 USER_Lock();
1033 if ((wndPtr = user_handles[index]))
1035 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD) menu = (HMENU)wndPtr->wIDmenu;
1036 sys_menu = wndPtr->hSysMenu;
1037 free_dce( wndPtr->dce, hwnd );
1038 surface = wndPtr->surface;
1039 wndPtr->surface = NULL;
1040 InterlockedCompareExchangePointer( &user_handles[index], NULL, wndPtr );
1042 USER_Unlock();
1044 HeapFree( GetProcessHeap(), 0, wndPtr );
1045 if (menu) DestroyMenu( menu );
1046 if (sys_menu) DestroyMenu( sys_menu );
1047 if (surface)
1049 register_window_surface( surface, NULL );
1050 window_surface_release( surface );
1055 /***********************************************************************
1056 * destroy_thread_child_windows
1058 * Destroy child windows upon exit of its thread.
1060 static void destroy_thread_child_windows( HWND hwnd )
1062 HWND *list;
1063 int i;
1065 if (WIN_IsCurrentThread( hwnd ))
1067 destroy_thread_window( hwnd );
1069 else if ((list = WIN_ListChildren( hwnd )))
1071 for (i = 0; list[i]; i++) destroy_thread_child_windows( list[i] );
1072 HeapFree( GetProcessHeap(), 0, list );
1077 /***********************************************************************
1078 * WIN_DestroyThreadWindows
1080 * Destroy all children of 'wnd' owned by the current thread.
1082 void WIN_DestroyThreadWindows( HWND hwnd )
1084 HWND *list;
1085 int i;
1087 if (!(list = WIN_ListChildren( hwnd ))) return;
1089 /* reset owners of top-level windows */
1090 for (i = 0; list[i]; i++)
1092 if (!WIN_IsCurrentThread( list[i] ))
1094 HWND owner = GetWindow( list[i], GW_OWNER );
1095 if (owner && WIN_IsCurrentThread( owner )) WIN_SetOwner( list[i], 0 );
1099 for (i = 0; list[i]; i++) destroy_thread_child_windows( list[i] );
1100 HeapFree( GetProcessHeap(), 0, list );
1104 /***********************************************************************
1105 * WIN_FixCoordinates
1107 * Fix the coordinates - Helper for WIN_CreateWindowEx.
1108 * returns default show mode in sw.
1110 static void WIN_FixCoordinates( CREATESTRUCTW *cs, INT *sw)
1112 #define IS_DEFAULT(x) ((x) == CW_USEDEFAULT || (x) == (SHORT)0x8000)
1113 POINT pos[2];
1115 if (cs->dwExStyle & WS_EX_MDICHILD)
1117 UINT id = 0;
1119 MDI_CalcDefaultChildPos(cs->hwndParent, -1, pos, 0, &id);
1120 if (!(cs->style & WS_POPUP)) cs->hMenu = ULongToHandle(id);
1122 TRACE("MDI child id %04x\n", id);
1125 if (cs->style & (WS_CHILD | WS_POPUP))
1127 if (cs->dwExStyle & WS_EX_MDICHILD)
1129 if (IS_DEFAULT(cs->x))
1131 cs->x = pos[0].x;
1132 cs->y = pos[0].y;
1134 if (IS_DEFAULT(cs->cx) || !cs->cx) cs->cx = pos[1].x;
1135 if (IS_DEFAULT(cs->cy) || !cs->cy) cs->cy = pos[1].y;
1137 else
1139 if (IS_DEFAULT(cs->x)) cs->x = cs->y = 0;
1140 if (IS_DEFAULT(cs->cx)) cs->cx = cs->cy = 0;
1143 else /* overlapped window */
1145 HMONITOR monitor;
1146 MONITORINFO mon_info;
1147 STARTUPINFOW info;
1149 if (!IS_DEFAULT(cs->x) && !IS_DEFAULT(cs->cx) && !IS_DEFAULT(cs->cy)) return;
1151 monitor = MonitorFromWindow( cs->hwndParent, MONITOR_DEFAULTTOPRIMARY );
1152 mon_info.cbSize = sizeof(mon_info);
1153 GetMonitorInfoW( monitor, &mon_info );
1154 GetStartupInfoW( &info );
1156 if (IS_DEFAULT(cs->x))
1158 if (!IS_DEFAULT(cs->y)) *sw = cs->y;
1159 cs->x = (info.dwFlags & STARTF_USEPOSITION) ? info.dwX : mon_info.rcWork.left;
1160 cs->y = (info.dwFlags & STARTF_USEPOSITION) ? info.dwY : mon_info.rcWork.top;
1163 if (IS_DEFAULT(cs->cx))
1165 if (info.dwFlags & STARTF_USESIZE)
1167 cs->cx = info.dwXSize;
1168 cs->cy = info.dwYSize;
1170 else
1172 cs->cx = (mon_info.rcWork.right - mon_info.rcWork.left) * 3 / 4 - cs->x;
1173 cs->cy = (mon_info.rcWork.bottom - mon_info.rcWork.top) * 3 / 4 - cs->y;
1176 /* neither x nor cx are default. Check the y values .
1177 * In the trace we see Outlook and Outlook Express using
1178 * cy set to CW_USEDEFAULT when opening the address book.
1180 else if (IS_DEFAULT(cs->cy))
1182 FIXME("Strange use of CW_USEDEFAULT in nHeight\n");
1183 cs->cy = (mon_info.rcWork.bottom - mon_info.rcWork.top) * 3 / 4 - cs->y;
1186 #undef IS_DEFAULT
1189 /***********************************************************************
1190 * dump_window_styles
1192 static void dump_window_styles( DWORD style, DWORD exstyle )
1194 TRACE( "style:" );
1195 if(style & WS_POPUP) TRACE(" WS_POPUP");
1196 if(style & WS_CHILD) TRACE(" WS_CHILD");
1197 if(style & WS_MINIMIZE) TRACE(" WS_MINIMIZE");
1198 if(style & WS_VISIBLE) TRACE(" WS_VISIBLE");
1199 if(style & WS_DISABLED) TRACE(" WS_DISABLED");
1200 if(style & WS_CLIPSIBLINGS) TRACE(" WS_CLIPSIBLINGS");
1201 if(style & WS_CLIPCHILDREN) TRACE(" WS_CLIPCHILDREN");
1202 if(style & WS_MAXIMIZE) TRACE(" WS_MAXIMIZE");
1203 if((style & WS_CAPTION) == WS_CAPTION) TRACE(" WS_CAPTION");
1204 else
1206 if(style & WS_BORDER) TRACE(" WS_BORDER");
1207 if(style & WS_DLGFRAME) TRACE(" WS_DLGFRAME");
1209 if(style & WS_VSCROLL) TRACE(" WS_VSCROLL");
1210 if(style & WS_HSCROLL) TRACE(" WS_HSCROLL");
1211 if(style & WS_SYSMENU) TRACE(" WS_SYSMENU");
1212 if(style & WS_THICKFRAME) TRACE(" WS_THICKFRAME");
1213 if (style & WS_CHILD)
1215 if(style & WS_GROUP) TRACE(" WS_GROUP");
1216 if(style & WS_TABSTOP) TRACE(" WS_TABSTOP");
1218 else
1220 if(style & WS_MINIMIZEBOX) TRACE(" WS_MINIMIZEBOX");
1221 if(style & WS_MAXIMIZEBOX) TRACE(" WS_MAXIMIZEBOX");
1224 /* FIXME: Add dumping of BS_/ES_/SBS_/LBS_/CBS_/DS_/etc. styles */
1225 #define DUMPED_STYLES \
1226 ((DWORD)(WS_POPUP | \
1227 WS_CHILD | \
1228 WS_MINIMIZE | \
1229 WS_VISIBLE | \
1230 WS_DISABLED | \
1231 WS_CLIPSIBLINGS | \
1232 WS_CLIPCHILDREN | \
1233 WS_MAXIMIZE | \
1234 WS_BORDER | \
1235 WS_DLGFRAME | \
1236 WS_VSCROLL | \
1237 WS_HSCROLL | \
1238 WS_SYSMENU | \
1239 WS_THICKFRAME | \
1240 WS_GROUP | \
1241 WS_TABSTOP | \
1242 WS_MINIMIZEBOX | \
1243 WS_MAXIMIZEBOX))
1245 if(style & ~DUMPED_STYLES) TRACE(" %08x", style & ~DUMPED_STYLES);
1246 TRACE("\n");
1247 #undef DUMPED_STYLES
1249 TRACE( "exstyle:" );
1250 if(exstyle & WS_EX_DLGMODALFRAME) TRACE(" WS_EX_DLGMODALFRAME");
1251 if(exstyle & WS_EX_DRAGDETECT) TRACE(" WS_EX_DRAGDETECT");
1252 if(exstyle & WS_EX_NOPARENTNOTIFY) TRACE(" WS_EX_NOPARENTNOTIFY");
1253 if(exstyle & WS_EX_TOPMOST) TRACE(" WS_EX_TOPMOST");
1254 if(exstyle & WS_EX_ACCEPTFILES) TRACE(" WS_EX_ACCEPTFILES");
1255 if(exstyle & WS_EX_TRANSPARENT) TRACE(" WS_EX_TRANSPARENT");
1256 if(exstyle & WS_EX_MDICHILD) TRACE(" WS_EX_MDICHILD");
1257 if(exstyle & WS_EX_TOOLWINDOW) TRACE(" WS_EX_TOOLWINDOW");
1258 if(exstyle & WS_EX_WINDOWEDGE) TRACE(" WS_EX_WINDOWEDGE");
1259 if(exstyle & WS_EX_CLIENTEDGE) TRACE(" WS_EX_CLIENTEDGE");
1260 if(exstyle & WS_EX_CONTEXTHELP) TRACE(" WS_EX_CONTEXTHELP");
1261 if(exstyle & WS_EX_RIGHT) TRACE(" WS_EX_RIGHT");
1262 if(exstyle & WS_EX_RTLREADING) TRACE(" WS_EX_RTLREADING");
1263 if(exstyle & WS_EX_LEFTSCROLLBAR) TRACE(" WS_EX_LEFTSCROLLBAR");
1264 if(exstyle & WS_EX_CONTROLPARENT) TRACE(" WS_EX_CONTROLPARENT");
1265 if(exstyle & WS_EX_STATICEDGE) TRACE(" WS_EX_STATICEDGE");
1266 if(exstyle & WS_EX_APPWINDOW) TRACE(" WS_EX_APPWINDOW");
1267 if(exstyle & WS_EX_LAYERED) TRACE(" WS_EX_LAYERED");
1268 if(exstyle & WS_EX_LAYOUTRTL) TRACE(" WS_EX_LAYOUTRTL");
1270 #define DUMPED_EX_STYLES \
1271 ((DWORD)(WS_EX_DLGMODALFRAME | \
1272 WS_EX_DRAGDETECT | \
1273 WS_EX_NOPARENTNOTIFY | \
1274 WS_EX_TOPMOST | \
1275 WS_EX_ACCEPTFILES | \
1276 WS_EX_TRANSPARENT | \
1277 WS_EX_MDICHILD | \
1278 WS_EX_TOOLWINDOW | \
1279 WS_EX_WINDOWEDGE | \
1280 WS_EX_CLIENTEDGE | \
1281 WS_EX_CONTEXTHELP | \
1282 WS_EX_RIGHT | \
1283 WS_EX_RTLREADING | \
1284 WS_EX_LEFTSCROLLBAR | \
1285 WS_EX_CONTROLPARENT | \
1286 WS_EX_STATICEDGE | \
1287 WS_EX_APPWINDOW | \
1288 WS_EX_LAYERED | \
1289 WS_EX_LAYOUTRTL))
1291 if(exstyle & ~DUMPED_EX_STYLES) TRACE(" %08x", exstyle & ~DUMPED_EX_STYLES);
1292 TRACE("\n");
1293 #undef DUMPED_EX_STYLES
1297 /***********************************************************************
1298 * WIN_CreateWindowEx
1300 * Implementation of CreateWindowEx().
1302 HWND WIN_CreateWindowEx( CREATESTRUCTW *cs, LPCWSTR className, HINSTANCE module, BOOL unicode )
1304 INT cx, cy, style, sw = SW_SHOW;
1305 LRESULT result;
1306 RECT rect;
1307 WND *wndPtr;
1308 HWND hwnd, parent, owner, top_child = 0;
1309 MDICREATESTRUCTW mdi_cs;
1310 CBT_CREATEWNDW cbtc;
1311 CREATESTRUCTW cbcs;
1313 TRACE("%s %s ex=%08x style=%08x %d,%d %dx%d parent=%p menu=%p inst=%p params=%p\n",
1314 unicode ? debugstr_w(cs->lpszName) : debugstr_a((LPCSTR)cs->lpszName),
1315 debugstr_w(className),
1316 cs->dwExStyle, cs->style, cs->x, cs->y, cs->cx, cs->cy,
1317 cs->hwndParent, cs->hMenu, cs->hInstance, cs->lpCreateParams );
1318 if(TRACE_ON(win)) dump_window_styles( cs->style, cs->dwExStyle );
1320 /* Fix the styles for MDI children */
1321 if (cs->dwExStyle & WS_EX_MDICHILD)
1323 UINT flags = 0;
1325 wndPtr = WIN_GetPtr(cs->hwndParent);
1326 if (wndPtr && wndPtr != WND_OTHER_PROCESS && wndPtr != WND_DESKTOP)
1328 flags = wndPtr->flags;
1329 WIN_ReleasePtr(wndPtr);
1332 if (!(flags & WIN_ISMDICLIENT))
1334 WARN("WS_EX_MDICHILD, but parent %p is not MDIClient\n", cs->hwndParent);
1335 return 0;
1338 /* cs->lpCreateParams of WM_[NC]CREATE is different for MDI children.
1339 * MDICREATESTRUCT members have the originally passed values.
1341 * Note: we rely on the fact that MDICREATESTRUCTA and MDICREATESTRUCTW
1342 * have the same layout.
1344 mdi_cs.szClass = cs->lpszClass;
1345 mdi_cs.szTitle = cs->lpszName;
1346 mdi_cs.hOwner = cs->hInstance;
1347 mdi_cs.x = cs->x;
1348 mdi_cs.y = cs->y;
1349 mdi_cs.cx = cs->cx;
1350 mdi_cs.cy = cs->cy;
1351 mdi_cs.style = cs->style;
1352 mdi_cs.lParam = (LPARAM)cs->lpCreateParams;
1354 cs->lpCreateParams = &mdi_cs;
1356 if (GetWindowLongW(cs->hwndParent, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1358 if (cs->style & WS_POPUP)
1360 TRACE("WS_POPUP with MDIS_ALLCHILDSTYLES is not allowed\n");
1361 return 0;
1363 cs->style |= WS_CHILD | WS_CLIPSIBLINGS;
1365 else
1367 cs->style &= ~WS_POPUP;
1368 cs->style |= WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CAPTION |
1369 WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
1372 top_child = GetWindow(cs->hwndParent, GW_CHILD);
1374 if (top_child)
1376 /* Restore current maximized child */
1377 if((cs->style & WS_VISIBLE) && IsZoomed(top_child))
1379 TRACE("Restoring current maximized child %p\n", top_child);
1380 if (cs->style & WS_MAXIMIZE)
1382 /* if the new window is maximized don't bother repainting */
1383 SendMessageW( top_child, WM_SETREDRAW, FALSE, 0 );
1384 ShowWindow( top_child, SW_SHOWNORMAL );
1385 SendMessageW( top_child, WM_SETREDRAW, TRUE, 0 );
1387 else ShowWindow( top_child, SW_SHOWNORMAL );
1392 /* Find the parent window */
1394 parent = cs->hwndParent;
1395 owner = 0;
1397 if (cs->hwndParent == HWND_MESSAGE)
1399 cs->hwndParent = parent = get_hwnd_message_parent();
1401 else if (cs->hwndParent)
1403 if ((cs->style & (WS_CHILD|WS_POPUP)) != WS_CHILD)
1405 parent = GetDesktopWindow();
1406 owner = cs->hwndParent;
1408 else
1410 DWORD parent_style = GetWindowLongW( parent, GWL_EXSTYLE );
1411 if ((parent_style & WS_EX_LAYOUTRTL) && !(parent_style & WS_EX_NOINHERITLAYOUT))
1412 cs->dwExStyle |= WS_EX_LAYOUTRTL;
1415 else
1417 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
1419 if ((cs->style & (WS_CHILD|WS_POPUP)) == WS_CHILD)
1421 WARN("No parent for child window\n" );
1422 SetLastError(ERROR_TLW_WITH_WSCHILD);
1423 return 0; /* WS_CHILD needs a parent, but WS_POPUP doesn't */
1426 /* are we creating the desktop or HWND_MESSAGE parent itself? */
1427 if (className != (LPCWSTR)DESKTOP_CLASS_ATOM &&
1428 (IS_INTRESOURCE(className) || strcmpiW( className, messageW )))
1430 DWORD layout;
1431 GetProcessDefaultLayout( &layout );
1432 if (layout & LAYOUT_RTL) cs->dwExStyle |= WS_EX_LAYOUTRTL;
1433 parent = GetDesktopWindow();
1437 WIN_FixCoordinates(cs, &sw); /* fix default coordinates */
1439 if ((cs->dwExStyle & WS_EX_DLGMODALFRAME) ||
1440 ((!(cs->dwExStyle & WS_EX_STATICEDGE)) &&
1441 (cs->style & (WS_DLGFRAME | WS_THICKFRAME))))
1442 cs->dwExStyle |= WS_EX_WINDOWEDGE;
1443 else
1444 cs->dwExStyle &= ~WS_EX_WINDOWEDGE;
1446 /* Create the window structure */
1448 if (!(wndPtr = create_window_handle( parent, owner, className, module, unicode )))
1450 WNDCLASSW wc;
1451 /* if it's a comctl32 class, GetClassInfo will load it, then we can retry */
1452 if (GetLastError() != ERROR_INVALID_HANDLE ||
1453 !GetClassInfoW( 0, className, &wc ) ||
1454 !(wndPtr = create_window_handle( parent, owner, className, module, unicode )))
1455 return 0;
1457 hwnd = wndPtr->obj.handle;
1459 /* Fill the window structure */
1461 wndPtr->tid = GetCurrentThreadId();
1462 wndPtr->hInstance = cs->hInstance;
1463 wndPtr->text = NULL;
1464 wndPtr->dwStyle = cs->style & ~WS_VISIBLE;
1465 wndPtr->dwExStyle = cs->dwExStyle;
1466 wndPtr->wIDmenu = 0;
1467 wndPtr->helpContext = 0;
1468 wndPtr->pScroll = NULL;
1469 wndPtr->userdata = 0;
1470 wndPtr->hIcon = 0;
1471 wndPtr->hIconSmall = 0;
1472 wndPtr->hIconSmall2 = 0;
1473 wndPtr->hSysMenu = 0;
1475 wndPtr->min_pos.x = wndPtr->min_pos.y = -1;
1476 wndPtr->max_pos.x = wndPtr->max_pos.y = -1;
1477 SetRect( &wndPtr->normal_rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
1479 if (wndPtr->dwStyle & WS_SYSMENU) SetSystemMenu( hwnd, 0 );
1482 * Correct the window styles.
1484 * It affects only the style loaded into the WIN structure.
1487 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
1489 wndPtr->dwStyle |= WS_CLIPSIBLINGS;
1490 if (!(wndPtr->dwStyle & WS_POPUP))
1491 wndPtr->dwStyle |= WS_CAPTION;
1494 /* WS_EX_WINDOWEDGE depends on some other styles */
1495 if (wndPtr->dwExStyle & WS_EX_DLGMODALFRAME)
1496 wndPtr->dwExStyle |= WS_EX_WINDOWEDGE;
1497 else if (wndPtr->dwStyle & (WS_DLGFRAME | WS_THICKFRAME))
1499 if (!((wndPtr->dwExStyle & WS_EX_STATICEDGE) &&
1500 (wndPtr->dwStyle & (WS_CHILD | WS_POPUP))))
1501 wndPtr->dwExStyle |= WS_EX_WINDOWEDGE;
1503 else
1504 wndPtr->dwExStyle &= ~WS_EX_WINDOWEDGE;
1506 if (!(wndPtr->dwStyle & (WS_CHILD | WS_POPUP)))
1507 wndPtr->flags |= WIN_NEED_SIZE;
1509 SERVER_START_REQ( set_window_info )
1511 req->handle = wine_server_user_handle( hwnd );
1512 req->flags = SET_WIN_STYLE | SET_WIN_EXSTYLE | SET_WIN_INSTANCE | SET_WIN_UNICODE;
1513 req->style = wndPtr->dwStyle;
1514 req->ex_style = wndPtr->dwExStyle;
1515 req->instance = wine_server_client_ptr( wndPtr->hInstance );
1516 req->is_unicode = (wndPtr->flags & WIN_ISUNICODE) != 0;
1517 req->extra_offset = -1;
1518 wine_server_call( req );
1520 SERVER_END_REQ;
1522 /* Set the window menu */
1524 if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
1526 if (cs->hMenu)
1528 if (!MENU_SetMenu(hwnd, cs->hMenu))
1530 WIN_ReleasePtr( wndPtr );
1531 free_window_handle( hwnd );
1532 return 0;
1535 else
1537 LPCWSTR menuName = (LPCWSTR)GetClassLongPtrW( hwnd, GCLP_MENUNAME );
1538 if (menuName)
1540 cs->hMenu = LoadMenuW( cs->hInstance, menuName );
1541 if (cs->hMenu) MENU_SetMenu( hwnd, cs->hMenu );
1545 else SetWindowLongPtrW( hwnd, GWLP_ID, (ULONG_PTR)cs->hMenu );
1547 /* call the WH_CBT hook */
1549 /* the window style passed to the hook must be the real window style,
1550 * rather than just the window style that the caller to CreateWindowEx
1551 * passed in, so we have to copy the original CREATESTRUCT and get the
1552 * the real style. */
1553 cbcs = *cs;
1554 cbcs.style = wndPtr->dwStyle;
1555 cbtc.lpcs = &cbcs;
1556 cbtc.hwndInsertAfter = HWND_TOP;
1557 WIN_ReleasePtr( wndPtr );
1558 if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode )) goto failed;
1560 /* send the WM_GETMINMAXINFO message and fix the size if needed */
1562 cx = cs->cx;
1563 cy = cs->cy;
1564 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
1566 POINT maxSize, maxPos, minTrack, maxTrack;
1567 WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
1568 if (maxTrack.x < cx) cx = maxTrack.x;
1569 if (maxTrack.y < cy) cy = maxTrack.y;
1570 if (minTrack.x > cx) cx = minTrack.x;
1571 if (minTrack.y > cy) cy = minTrack.y;
1574 if (cx < 0) cx = 0;
1575 if (cy < 0) cy = 0;
1576 SetRect( &rect, cs->x, cs->y, cs->x + cx, cs->y + cy );
1577 /* check for wraparound */
1578 if (cs->x + cx < cs->x) rect.right = 0x7fffffff;
1579 if (cs->y + cy < cs->y) rect.bottom = 0x7fffffff;
1580 if (!set_window_pos( hwnd, 0, SWP_NOZORDER | SWP_NOACTIVATE, &rect, &rect, NULL )) goto failed;
1582 /* send WM_NCCREATE */
1584 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cx, cy );
1585 if (unicode)
1586 result = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1587 else
1588 result = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1589 if (!result)
1591 WARN( "%p: aborted by WM_NCCREATE\n", hwnd );
1592 goto failed;
1595 /* send WM_NCCALCSIZE */
1597 if (WIN_GetRectangles( hwnd, COORDS_PARENT, &rect, NULL ))
1599 /* yes, even if the CBT hook was called with HWND_TOP */
1600 HWND insert_after = (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
1601 RECT client_rect = rect;
1603 /* the rectangle is in screen coords for WM_NCCALCSIZE when wparam is FALSE */
1604 MapWindowPoints( parent, 0, (POINT *)&client_rect, 2 );
1605 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&client_rect );
1606 MapWindowPoints( 0, parent, (POINT *)&client_rect, 2 );
1607 set_window_pos( hwnd, insert_after, SWP_NOACTIVATE, &rect, &client_rect, NULL );
1609 else return 0;
1611 /* send WM_CREATE */
1613 if (unicode)
1614 result = SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs );
1615 else
1616 result = SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs );
1617 if (result == -1) goto failed;
1619 /* call the driver */
1621 if (!USER_Driver->pCreateWindow( hwnd )) goto failed;
1623 NotifyWinEvent(EVENT_OBJECT_CREATE, hwnd, OBJID_WINDOW, 0);
1625 /* send the size messages */
1627 if (!(wndPtr = WIN_GetPtr( hwnd )) ||
1628 wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return 0;
1629 if (!(wndPtr->flags & WIN_NEED_SIZE))
1631 WIN_ReleasePtr( wndPtr );
1632 WIN_GetRectangles( hwnd, COORDS_PARENT, NULL, &rect );
1633 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1634 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1635 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( rect.left, rect.top ) );
1637 else WIN_ReleasePtr( wndPtr );
1639 /* Show the window, maximizing or minimizing if needed */
1641 style = WIN_SetStyle( hwnd, 0, WS_MAXIMIZE | WS_MINIMIZE );
1642 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1644 RECT newPos;
1645 UINT swFlag = (style & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1647 swFlag = WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1648 swFlag |= SWP_FRAMECHANGED; /* Frame always gets changed */
1649 if (!(style & WS_VISIBLE) || (style & WS_CHILD) || GetActiveWindow()) swFlag |= SWP_NOACTIVATE;
1650 SetWindowPos( hwnd, 0, newPos.left, newPos.top, newPos.right - newPos.left,
1651 newPos.bottom - newPos.top, swFlag );
1654 /* Notify the parent window only */
1656 send_parent_notify( hwnd, WM_CREATE );
1657 if (!IsWindow( hwnd )) return 0;
1659 if (cs->style & WS_VISIBLE)
1661 if (cs->style & WS_MAXIMIZE)
1662 sw = SW_SHOW;
1663 else if (cs->style & WS_MINIMIZE)
1664 sw = SW_SHOWMINIMIZED;
1666 ShowWindow( hwnd, sw );
1667 if (cs->dwExStyle & WS_EX_MDICHILD)
1669 SendMessageW(cs->hwndParent, WM_MDIREFRESHMENU, 0, 0);
1670 /* ShowWindow won't activate child windows */
1671 SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE );
1675 /* Call WH_SHELL hook */
1677 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) && !GetWindow( hwnd, GW_OWNER ))
1678 HOOK_CallHooks( WH_SHELL, HSHELL_WINDOWCREATED, (WPARAM)hwnd, 0, TRUE );
1680 TRACE("created window %p\n", hwnd);
1681 return hwnd;
1683 failed:
1684 WIN_DestroyWindow( hwnd );
1685 return 0;
1689 /***********************************************************************
1690 * CreateWindowExA (USER32.@)
1692 HWND WINAPI DECLSPEC_HOTPATCH CreateWindowExA( DWORD exStyle, LPCSTR className,
1693 LPCSTR windowName, DWORD style, INT x,
1694 INT y, INT width, INT height,
1695 HWND parent, HMENU menu,
1696 HINSTANCE instance, LPVOID data )
1698 CREATESTRUCTA cs;
1700 cs.lpCreateParams = data;
1701 cs.hInstance = instance;
1702 cs.hMenu = menu;
1703 cs.hwndParent = parent;
1704 cs.x = x;
1705 cs.y = y;
1706 cs.cx = width;
1707 cs.cy = height;
1708 cs.style = style;
1709 cs.lpszName = windowName;
1710 cs.lpszClass = className;
1711 cs.dwExStyle = exStyle;
1713 if (!IS_INTRESOURCE(className))
1715 WCHAR bufferW[256];
1716 if (!MultiByteToWideChar( CP_ACP, 0, className, -1, bufferW, sizeof(bufferW)/sizeof(WCHAR) ))
1717 return 0;
1718 return wow_handlers.create_window( (CREATESTRUCTW *)&cs, bufferW, instance, FALSE );
1720 /* Note: we rely on the fact that CREATESTRUCTA and */
1721 /* CREATESTRUCTW have the same layout. */
1722 return wow_handlers.create_window( (CREATESTRUCTW *)&cs, (LPCWSTR)className, instance, FALSE );
1726 /***********************************************************************
1727 * CreateWindowExW (USER32.@)
1729 HWND WINAPI DECLSPEC_HOTPATCH CreateWindowExW( DWORD exStyle, LPCWSTR className,
1730 LPCWSTR windowName, DWORD style, INT x,
1731 INT y, INT width, INT height,
1732 HWND parent, HMENU menu,
1733 HINSTANCE instance, LPVOID data )
1735 CREATESTRUCTW cs;
1737 cs.lpCreateParams = data;
1738 cs.hInstance = instance;
1739 cs.hMenu = menu;
1740 cs.hwndParent = parent;
1741 cs.x = x;
1742 cs.y = y;
1743 cs.cx = width;
1744 cs.cy = height;
1745 cs.style = style;
1746 cs.lpszName = windowName;
1747 cs.lpszClass = className;
1748 cs.dwExStyle = exStyle;
1750 return wow_handlers.create_window( &cs, className, instance, TRUE );
1754 /***********************************************************************
1755 * WIN_SendDestroyMsg
1757 static void WIN_SendDestroyMsg( HWND hwnd )
1759 GUITHREADINFO info;
1761 info.cbSize = sizeof(info);
1762 if (GetGUIThreadInfo( GetCurrentThreadId(), &info ))
1764 if (hwnd == info.hwndCaret) DestroyCaret();
1765 if (hwnd == info.hwndActive) WINPOS_ActivateOtherWindow( hwnd );
1769 * Send the WM_DESTROY to the window.
1771 SendMessageW( hwnd, WM_DESTROY, 0, 0);
1774 * This WM_DESTROY message can trigger re-entrant calls to DestroyWindow
1775 * make sure that the window still exists when we come back.
1777 if (IsWindow(hwnd))
1779 HWND* pWndArray;
1780 int i;
1782 if (!(pWndArray = WIN_ListChildren( hwnd ))) return;
1784 for (i = 0; pWndArray[i]; i++)
1786 if (IsWindow( pWndArray[i] )) WIN_SendDestroyMsg( pWndArray[i] );
1788 HeapFree( GetProcessHeap(), 0, pWndArray );
1790 else
1791 WARN("\tdestroyed itself while in WM_DESTROY!\n");
1795 /***********************************************************************
1796 * DestroyWindow (USER32.@)
1798 BOOL WINAPI DestroyWindow( HWND hwnd )
1800 BOOL is_child;
1802 if (!(hwnd = WIN_IsCurrentThread( hwnd )) || is_desktop_window( hwnd ))
1804 SetLastError( ERROR_ACCESS_DENIED );
1805 return FALSE;
1808 TRACE("(%p)\n", hwnd);
1810 /* Call hooks */
1812 if (HOOK_CallHooks( WH_CBT, HCBT_DESTROYWND, (WPARAM)hwnd, 0, TRUE )) return FALSE;
1814 if (MENU_IsMenuActive() == hwnd)
1815 EndMenu();
1817 is_child = (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) != 0;
1819 if (is_child)
1821 if (!USER_IsExitingThread( GetCurrentThreadId() ))
1822 send_parent_notify( hwnd, WM_DESTROY );
1824 else if (!GetWindow( hwnd, GW_OWNER ))
1826 HOOK_CallHooks( WH_SHELL, HSHELL_WINDOWDESTROYED, (WPARAM)hwnd, 0L, TRUE );
1827 /* FIXME: clean up palette - see "Internals" p.352 */
1830 if (!IsWindow(hwnd)) return TRUE;
1832 /* Hide the window */
1833 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)
1835 /* Only child windows receive WM_SHOWWINDOW in DestroyWindow() */
1836 if (is_child)
1837 ShowWindow( hwnd, SW_HIDE );
1838 else
1839 SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE |
1840 SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW );
1843 if (!IsWindow(hwnd)) return TRUE;
1845 /* Recursively destroy owned windows */
1847 if (!is_child)
1849 for (;;)
1851 int i;
1852 BOOL got_one = FALSE;
1853 HWND *list = WIN_ListChildren( GetDesktopWindow() );
1854 if (list)
1856 for (i = 0; list[i]; i++)
1858 if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
1859 if (WIN_IsCurrentThread( list[i] ))
1861 DestroyWindow( list[i] );
1862 got_one = TRUE;
1863 continue;
1865 WIN_SetOwner( list[i], 0 );
1867 HeapFree( GetProcessHeap(), 0, list );
1869 if (!got_one) break;
1873 /* Send destroy messages */
1875 WIN_SendDestroyMsg( hwnd );
1876 if (!IsWindow( hwnd )) return TRUE;
1878 CLIPBOARD_ReleaseOwner( hwnd );
1880 /* Destroy the window storage */
1882 WIN_DestroyWindow( hwnd );
1883 return TRUE;
1887 /***********************************************************************
1888 * CloseWindow (USER32.@)
1890 BOOL WINAPI CloseWindow( HWND hwnd )
1892 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) return FALSE;
1893 ShowWindow( hwnd, SW_MINIMIZE );
1894 return TRUE;
1898 /***********************************************************************
1899 * OpenIcon (USER32.@)
1901 BOOL WINAPI OpenIcon( HWND hwnd )
1903 if (!IsIconic( hwnd )) return FALSE;
1904 ShowWindow( hwnd, SW_SHOWNORMAL );
1905 return TRUE;
1909 /***********************************************************************
1910 * FindWindowExW (USER32.@)
1912 HWND WINAPI FindWindowExW( HWND parent, HWND child, LPCWSTR className, LPCWSTR title )
1914 HWND *list = NULL;
1915 HWND retvalue = 0;
1916 int i = 0, len = 0;
1917 WCHAR *buffer = NULL;
1919 if (!parent && child) parent = GetDesktopWindow();
1920 else if (parent == HWND_MESSAGE) parent = get_hwnd_message_parent();
1922 if (title)
1924 len = strlenW(title) + 1; /* one extra char to check for chars beyond the end */
1925 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return 0;
1928 if (!(list = list_window_children( 0, parent, className, 0 ))) goto done;
1930 if (child)
1932 child = WIN_GetFullHandle( child );
1933 while (list[i] && list[i] != child) i++;
1934 if (!list[i]) goto done;
1935 i++; /* start from next window */
1938 if (title)
1940 while (list[i])
1942 if (InternalGetWindowText( list[i], buffer, len + 1 ))
1944 if (!strcmpiW( buffer, title )) break;
1946 else
1948 if (!title[0]) break;
1950 i++;
1953 retvalue = list[i];
1955 done:
1956 HeapFree( GetProcessHeap(), 0, list );
1957 HeapFree( GetProcessHeap(), 0, buffer );
1958 return retvalue;
1963 /***********************************************************************
1964 * FindWindowA (USER32.@)
1966 HWND WINAPI FindWindowA( LPCSTR className, LPCSTR title )
1968 HWND ret = FindWindowExA( 0, 0, className, title );
1969 if (!ret) SetLastError (ERROR_CANNOT_FIND_WND_CLASS);
1970 return ret;
1974 /***********************************************************************
1975 * FindWindowExA (USER32.@)
1977 HWND WINAPI FindWindowExA( HWND parent, HWND child, LPCSTR className, LPCSTR title )
1979 LPWSTR titleW = NULL;
1980 HWND hwnd = 0;
1982 if (title)
1984 DWORD len = MultiByteToWideChar( CP_ACP, 0, title, -1, NULL, 0 );
1985 if (!(titleW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return 0;
1986 MultiByteToWideChar( CP_ACP, 0, title, -1, titleW, len );
1989 if (!IS_INTRESOURCE(className))
1991 WCHAR classW[256];
1992 if (MultiByteToWideChar( CP_ACP, 0, className, -1, classW, sizeof(classW)/sizeof(WCHAR) ))
1993 hwnd = FindWindowExW( parent, child, classW, titleW );
1995 else
1997 hwnd = FindWindowExW( parent, child, (LPCWSTR)className, titleW );
2000 HeapFree( GetProcessHeap(), 0, titleW );
2001 return hwnd;
2005 /***********************************************************************
2006 * FindWindowW (USER32.@)
2008 HWND WINAPI FindWindowW( LPCWSTR className, LPCWSTR title )
2010 return FindWindowExW( 0, 0, className, title );
2014 /**********************************************************************
2015 * GetDesktopWindow (USER32.@)
2017 HWND WINAPI GetDesktopWindow(void)
2019 struct user_thread_info *thread_info = get_user_thread_info();
2021 if (thread_info->top_window) return thread_info->top_window;
2023 SERVER_START_REQ( get_desktop_window )
2025 req->force = 0;
2026 if (!wine_server_call( req ))
2028 thread_info->top_window = wine_server_ptr_handle( reply->top_window );
2029 thread_info->msg_window = wine_server_ptr_handle( reply->msg_window );
2032 SERVER_END_REQ;
2034 if (!thread_info->top_window)
2036 USEROBJECTFLAGS flags;
2037 if (!GetUserObjectInformationW( GetProcessWindowStation(), UOI_FLAGS, &flags,
2038 sizeof(flags), NULL ) || (flags.dwFlags & WSF_VISIBLE))
2040 static const WCHAR explorer[] = {'\\','e','x','p','l','o','r','e','r','.','e','x','e',0};
2041 static const WCHAR args[] = {' ','/','d','e','s','k','t','o','p',0};
2042 STARTUPINFOW si;
2043 PROCESS_INFORMATION pi;
2044 WCHAR windir[MAX_PATH];
2045 WCHAR app[MAX_PATH + sizeof(explorer)/sizeof(WCHAR)];
2046 WCHAR cmdline[MAX_PATH + (sizeof(explorer) + sizeof(args))/sizeof(WCHAR)];
2047 void *redir;
2049 memset( &si, 0, sizeof(si) );
2050 si.cb = sizeof(si);
2051 si.dwFlags = STARTF_USESTDHANDLES;
2052 si.hStdInput = 0;
2053 si.hStdOutput = 0;
2054 si.hStdError = GetStdHandle( STD_ERROR_HANDLE );
2056 GetSystemDirectoryW( windir, MAX_PATH );
2057 strcpyW( app, windir );
2058 strcatW( app, explorer );
2059 strcpyW( cmdline, app );
2060 strcatW( cmdline, args );
2062 Wow64DisableWow64FsRedirection( &redir );
2063 if (CreateProcessW( app, cmdline, NULL, NULL, FALSE, DETACHED_PROCESS,
2064 NULL, windir, &si, &pi ))
2066 TRACE( "started explorer pid %04x tid %04x\n", pi.dwProcessId, pi.dwThreadId );
2067 WaitForInputIdle( pi.hProcess, 10000 );
2068 CloseHandle( pi.hThread );
2069 CloseHandle( pi.hProcess );
2071 else WARN( "failed to start explorer, err %d\n", GetLastError() );
2072 Wow64RevertWow64FsRedirection( redir );
2074 else TRACE( "not starting explorer since winstation is not visible\n" );
2076 SERVER_START_REQ( get_desktop_window )
2078 req->force = 1;
2079 if (!wine_server_call( req ))
2081 thread_info->top_window = wine_server_ptr_handle( reply->top_window );
2082 thread_info->msg_window = wine_server_ptr_handle( reply->msg_window );
2085 SERVER_END_REQ;
2088 if (!thread_info->top_window || !USER_Driver->pCreateDesktopWindow( thread_info->top_window ))
2089 ERR( "failed to create desktop window\n" );
2091 return thread_info->top_window;
2095 /*******************************************************************
2096 * EnableWindow (USER32.@)
2098 BOOL WINAPI EnableWindow( HWND hwnd, BOOL enable )
2100 BOOL retvalue;
2101 HWND full_handle;
2103 if (is_broadcast(hwnd))
2105 SetLastError( ERROR_INVALID_PARAMETER );
2106 return FALSE;
2109 if (!(full_handle = WIN_IsCurrentThread( hwnd )))
2110 return SendMessageW( hwnd, WM_WINE_ENABLEWINDOW, enable, 0 );
2112 hwnd = full_handle;
2114 TRACE("( %p, %d )\n", hwnd, enable);
2116 retvalue = !IsWindowEnabled( hwnd );
2118 if (enable && retvalue)
2120 WIN_SetStyle( hwnd, 0, WS_DISABLED );
2121 SendMessageW( hwnd, WM_ENABLE, TRUE, 0 );
2123 else if (!enable && !retvalue)
2125 HWND capture_wnd;
2127 SendMessageW( hwnd, WM_CANCELMODE, 0, 0);
2129 WIN_SetStyle( hwnd, WS_DISABLED, 0 );
2131 if (hwnd == GetFocus())
2132 SetFocus( 0 ); /* A disabled window can't have the focus */
2134 capture_wnd = GetCapture();
2135 if (hwnd == capture_wnd || IsChild(hwnd, capture_wnd))
2136 ReleaseCapture(); /* A disabled window can't capture the mouse */
2138 SendMessageW( hwnd, WM_ENABLE, FALSE, 0 );
2140 return retvalue;
2144 /***********************************************************************
2145 * IsWindowEnabled (USER32.@)
2147 BOOL WINAPI IsWindowEnabled(HWND hWnd)
2149 return !(GetWindowLongW( hWnd, GWL_STYLE ) & WS_DISABLED);
2153 /***********************************************************************
2154 * IsWindowUnicode (USER32.@)
2156 BOOL WINAPI IsWindowUnicode( HWND hwnd )
2158 WND * wndPtr;
2159 BOOL retvalue = FALSE;
2161 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
2163 if (wndPtr == WND_DESKTOP) return TRUE;
2165 if (wndPtr != WND_OTHER_PROCESS)
2167 retvalue = (wndPtr->flags & WIN_ISUNICODE) != 0;
2168 WIN_ReleasePtr( wndPtr );
2170 else
2172 SERVER_START_REQ( get_window_info )
2174 req->handle = wine_server_user_handle( hwnd );
2175 if (!wine_server_call_err( req )) retvalue = reply->is_unicode;
2177 SERVER_END_REQ;
2179 return retvalue;
2183 /**********************************************************************
2184 * WIN_GetWindowLong
2186 * Helper function for GetWindowLong().
2188 static LONG_PTR WIN_GetWindowLong( HWND hwnd, INT offset, UINT size, BOOL unicode )
2190 LONG_PTR retvalue = 0;
2191 WND *wndPtr;
2193 if (offset == GWLP_HWNDPARENT)
2195 HWND parent = GetAncestor( hwnd, GA_PARENT );
2196 if (parent == GetDesktopWindow()) parent = GetWindow( hwnd, GW_OWNER );
2197 return (ULONG_PTR)parent;
2200 if (!(wndPtr = WIN_GetPtr( hwnd )))
2202 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2203 return 0;
2206 if (wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP)
2208 if (offset == GWLP_WNDPROC)
2210 SetLastError( ERROR_ACCESS_DENIED );
2211 return 0;
2213 SERVER_START_REQ( set_window_info )
2215 req->handle = wine_server_user_handle( hwnd );
2216 req->flags = 0; /* don't set anything, just retrieve */
2217 req->extra_offset = (offset >= 0) ? offset : -1;
2218 req->extra_size = (offset >= 0) ? size : 0;
2219 if (!wine_server_call_err( req ))
2221 switch(offset)
2223 case GWL_STYLE: retvalue = reply->old_style; break;
2224 case GWL_EXSTYLE: retvalue = reply->old_ex_style; break;
2225 case GWLP_ID: retvalue = reply->old_id; break;
2226 case GWLP_HINSTANCE: retvalue = (ULONG_PTR)wine_server_get_ptr( reply->old_instance ); break;
2227 case GWLP_USERDATA: retvalue = reply->old_user_data; break;
2228 default:
2229 if (offset >= 0) retvalue = get_win_data( &reply->old_extra_value, size );
2230 else SetLastError( ERROR_INVALID_INDEX );
2231 break;
2235 SERVER_END_REQ;
2236 return retvalue;
2239 /* now we have a valid wndPtr */
2241 if (offset >= 0)
2243 if (offset > (int)(wndPtr->cbWndExtra - size))
2245 WARN("Invalid offset %d\n", offset );
2246 WIN_ReleasePtr( wndPtr );
2247 SetLastError( ERROR_INVALID_INDEX );
2248 return 0;
2250 retvalue = get_win_data( (char *)wndPtr->wExtra + offset, size );
2252 /* Special case for dialog window procedure */
2253 if ((offset == DWLP_DLGPROC) && (size == sizeof(LONG_PTR)) && wndPtr->dlgInfo)
2254 retvalue = (LONG_PTR)WINPROC_GetProc( (WNDPROC)retvalue, unicode );
2255 WIN_ReleasePtr( wndPtr );
2256 return retvalue;
2259 switch(offset)
2261 case GWLP_USERDATA: retvalue = wndPtr->userdata; break;
2262 case GWL_STYLE: retvalue = wndPtr->dwStyle; break;
2263 case GWL_EXSTYLE: retvalue = wndPtr->dwExStyle; break;
2264 case GWLP_ID: retvalue = wndPtr->wIDmenu; break;
2265 case GWLP_HINSTANCE: retvalue = (ULONG_PTR)wndPtr->hInstance; break;
2266 case GWLP_WNDPROC:
2267 /* This looks like a hack only for the edit control (see tests). This makes these controls
2268 * more tolerant to A/W mismatches. The lack of W->A->W conversion for such a mismatch suggests
2269 * that the hack is in GetWindowLongPtr[AW], not in winprocs.
2271 if (wndPtr->winproc == BUILTIN_WINPROC(WINPROC_EDIT) && (!unicode != !(wndPtr->flags & WIN_ISUNICODE)))
2272 retvalue = (ULONG_PTR)wndPtr->winproc;
2273 else
2274 retvalue = (ULONG_PTR)WINPROC_GetProc( wndPtr->winproc, unicode );
2275 break;
2276 default:
2277 WARN("Unknown offset %d\n", offset );
2278 SetLastError( ERROR_INVALID_INDEX );
2279 break;
2281 WIN_ReleasePtr(wndPtr);
2282 return retvalue;
2286 /**********************************************************************
2287 * WIN_SetWindowLong
2289 * Helper function for SetWindowLong().
2291 * 0 is the failure code. However, in the case of failure SetLastError
2292 * must be set to distinguish between a 0 return value and a failure.
2294 LONG_PTR WIN_SetWindowLong( HWND hwnd, INT offset, UINT size, LONG_PTR newval, BOOL unicode )
2296 STYLESTRUCT style;
2297 BOOL ok, made_visible = FALSE;
2298 LONG_PTR retval = 0;
2299 WND *wndPtr;
2301 TRACE( "%p %d %lx %c\n", hwnd, offset, newval, unicode ? 'W' : 'A' );
2303 if (is_broadcast(hwnd))
2305 SetLastError( ERROR_INVALID_PARAMETER );
2306 return FALSE;
2309 if (!(wndPtr = WIN_GetPtr( hwnd )))
2311 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2312 return 0;
2314 if (wndPtr == WND_DESKTOP)
2316 /* can't change anything on the desktop window */
2317 SetLastError( ERROR_ACCESS_DENIED );
2318 return 0;
2320 if (wndPtr == WND_OTHER_PROCESS)
2322 if (offset == GWLP_WNDPROC)
2324 SetLastError( ERROR_ACCESS_DENIED );
2325 return 0;
2327 if (offset > 32767 || offset < -32767)
2329 SetLastError( ERROR_INVALID_INDEX );
2330 return 0;
2332 return SendMessageW( hwnd, WM_WINE_SETWINDOWLONG, MAKEWPARAM( offset, size ), newval );
2335 /* first some special cases */
2336 switch( offset )
2338 case GWL_STYLE:
2339 style.styleOld = wndPtr->dwStyle;
2340 style.styleNew = newval;
2341 WIN_ReleasePtr( wndPtr );
2342 SendMessageW( hwnd, WM_STYLECHANGING, GWL_STYLE, (LPARAM)&style );
2343 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
2344 newval = style.styleNew;
2345 /* WS_CLIPSIBLINGS can't be reset on top-level windows */
2346 if (wndPtr->parent == GetDesktopWindow()) newval |= WS_CLIPSIBLINGS;
2347 /* FIXME: changing WS_DLGFRAME | WS_THICKFRAME is supposed to change
2348 WS_EX_WINDOWEDGE too */
2349 break;
2350 case GWL_EXSTYLE:
2351 style.styleOld = wndPtr->dwExStyle;
2352 style.styleNew = newval;
2353 WIN_ReleasePtr( wndPtr );
2354 SendMessageW( hwnd, WM_STYLECHANGING, GWL_EXSTYLE, (LPARAM)&style );
2355 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
2356 /* WS_EX_TOPMOST can only be changed through SetWindowPos */
2357 newval = (style.styleNew & ~WS_EX_TOPMOST) | (wndPtr->dwExStyle & WS_EX_TOPMOST);
2358 /* WS_EX_WINDOWEDGE depends on some other styles */
2359 if (newval & WS_EX_DLGMODALFRAME)
2360 newval |= WS_EX_WINDOWEDGE;
2361 else if (!(newval & WS_EX_STATICEDGE) && (wndPtr->dwStyle & (WS_DLGFRAME | WS_THICKFRAME)))
2362 newval |= WS_EX_WINDOWEDGE;
2363 else
2364 newval &= ~WS_EX_WINDOWEDGE;
2365 break;
2366 case GWLP_HWNDPARENT:
2367 if (wndPtr->parent == GetDesktopWindow())
2369 WIN_ReleasePtr( wndPtr );
2370 return (ULONG_PTR)WIN_SetOwner( hwnd, (HWND)newval );
2372 else
2374 WIN_ReleasePtr( wndPtr );
2375 return (ULONG_PTR)SetParent( hwnd, (HWND)newval );
2377 case GWLP_WNDPROC:
2379 WNDPROC proc;
2380 UINT old_flags = wndPtr->flags;
2381 retval = WIN_GetWindowLong( hwnd, offset, size, unicode );
2382 proc = WINPROC_AllocProc( (WNDPROC)newval, unicode );
2383 if (proc) wndPtr->winproc = proc;
2384 if (WINPROC_IsUnicode( proc, unicode )) wndPtr->flags |= WIN_ISUNICODE;
2385 else wndPtr->flags &= ~WIN_ISUNICODE;
2386 if (!((old_flags ^ wndPtr->flags) & WIN_ISUNICODE))
2388 WIN_ReleasePtr( wndPtr );
2389 return retval;
2391 /* update is_unicode flag on the server side */
2392 break;
2394 case GWLP_ID:
2395 case GWLP_HINSTANCE:
2396 case GWLP_USERDATA:
2397 break;
2398 case DWLP_DLGPROC:
2399 if ((wndPtr->cbWndExtra - sizeof(LONG_PTR) >= DWLP_DLGPROC) &&
2400 (size == sizeof(LONG_PTR)) && wndPtr->dlgInfo)
2402 WNDPROC *ptr = (WNDPROC *)((char *)wndPtr->wExtra + DWLP_DLGPROC);
2403 retval = (ULONG_PTR)WINPROC_GetProc( *ptr, unicode );
2404 *ptr = WINPROC_AllocProc( (WNDPROC)newval, unicode );
2405 WIN_ReleasePtr( wndPtr );
2406 return retval;
2408 /* fall through */
2409 default:
2410 if (offset < 0 || offset > (int)(wndPtr->cbWndExtra - size))
2412 WARN("Invalid offset %d\n", offset );
2413 WIN_ReleasePtr( wndPtr );
2414 SetLastError( ERROR_INVALID_INDEX );
2415 return 0;
2417 else if (get_win_data( (char *)wndPtr->wExtra + offset, size ) == newval)
2419 /* already set to the same value */
2420 WIN_ReleasePtr( wndPtr );
2421 return newval;
2423 break;
2426 SERVER_START_REQ( set_window_info )
2428 req->handle = wine_server_user_handle( hwnd );
2429 req->extra_offset = -1;
2430 switch(offset)
2432 case GWL_STYLE:
2433 req->flags = SET_WIN_STYLE;
2434 req->style = newval;
2435 break;
2436 case GWL_EXSTYLE:
2437 req->flags = SET_WIN_EXSTYLE;
2438 req->ex_style = newval;
2439 break;
2440 case GWLP_ID:
2441 req->flags = SET_WIN_ID;
2442 req->id = newval;
2443 break;
2444 case GWLP_HINSTANCE:
2445 req->flags = SET_WIN_INSTANCE;
2446 req->instance = wine_server_client_ptr( (void *)newval );
2447 break;
2448 case GWLP_WNDPROC:
2449 req->flags = SET_WIN_UNICODE;
2450 req->is_unicode = (wndPtr->flags & WIN_ISUNICODE) != 0;
2451 break;
2452 case GWLP_USERDATA:
2453 req->flags = SET_WIN_USERDATA;
2454 req->user_data = newval;
2455 break;
2456 default:
2457 req->flags = SET_WIN_EXTRA;
2458 req->extra_offset = offset;
2459 req->extra_size = size;
2460 set_win_data( &req->extra_value, newval, size );
2462 if ((ok = !wine_server_call_err( req )))
2464 switch(offset)
2466 case GWL_STYLE:
2467 wndPtr->dwStyle = newval;
2468 retval = reply->old_style;
2469 break;
2470 case GWL_EXSTYLE:
2471 wndPtr->dwExStyle = newval;
2472 retval = reply->old_ex_style;
2473 break;
2474 case GWLP_ID:
2475 wndPtr->wIDmenu = newval;
2476 retval = reply->old_id;
2477 break;
2478 case GWLP_HINSTANCE:
2479 wndPtr->hInstance = (HINSTANCE)newval;
2480 retval = (ULONG_PTR)wine_server_get_ptr( reply->old_instance );
2481 break;
2482 case GWLP_WNDPROC:
2483 break;
2484 case GWLP_USERDATA:
2485 wndPtr->userdata = newval;
2486 retval = reply->old_user_data;
2487 break;
2488 default:
2489 retval = get_win_data( (char *)wndPtr->wExtra + offset, size );
2490 set_win_data( (char *)wndPtr->wExtra + offset, newval, size );
2491 break;
2495 SERVER_END_REQ;
2497 if ((offset == GWL_STYLE && ((style.styleOld ^ style.styleNew) & WS_VISIBLE)) ||
2498 (offset == GWL_EXSTYLE && ((style.styleOld ^ style.styleNew) & WS_EX_LAYERED)))
2500 made_visible = (wndPtr->dwStyle & WS_VISIBLE) != 0;
2501 invalidate_dce( wndPtr, NULL );
2503 WIN_ReleasePtr( wndPtr );
2505 if (!ok) return 0;
2507 if (offset == GWL_STYLE || offset == GWL_EXSTYLE)
2509 style.styleOld = retval;
2510 style.styleNew = newval;
2511 USER_Driver->pSetWindowStyle( hwnd, offset, &style );
2512 if (made_visible) update_window_state( hwnd );
2513 SendMessageW( hwnd, WM_STYLECHANGED, offset, (LPARAM)&style );
2516 return retval;
2520 /**********************************************************************
2521 * GetWindowWord (USER32.@)
2523 WORD WINAPI GetWindowWord( HWND hwnd, INT offset )
2525 switch(offset)
2527 case GWLP_ID:
2528 case GWLP_HINSTANCE:
2529 case GWLP_HWNDPARENT:
2530 break;
2531 default:
2532 if (offset < 0)
2534 WARN("Invalid offset %d\n", offset );
2535 SetLastError( ERROR_INVALID_INDEX );
2536 return 0;
2538 break;
2540 return WIN_GetWindowLong( hwnd, offset, sizeof(WORD), FALSE );
2544 /**********************************************************************
2545 * GetWindowLongA (USER32.@)
2547 LONG WINAPI GetWindowLongA( HWND hwnd, INT offset )
2549 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG), FALSE );
2553 /**********************************************************************
2554 * GetWindowLongW (USER32.@)
2556 LONG WINAPI GetWindowLongW( HWND hwnd, INT offset )
2558 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG), TRUE );
2562 /**********************************************************************
2563 * SetWindowWord (USER32.@)
2565 WORD WINAPI SetWindowWord( HWND hwnd, INT offset, WORD newval )
2567 switch(offset)
2569 case GWLP_ID:
2570 case GWLP_HINSTANCE:
2571 case GWLP_HWNDPARENT:
2572 break;
2573 default:
2574 if (offset < 0)
2576 WARN("Invalid offset %d\n", offset );
2577 SetLastError( ERROR_INVALID_INDEX );
2578 return 0;
2580 break;
2582 return WIN_SetWindowLong( hwnd, offset, sizeof(WORD), newval, FALSE );
2586 /**********************************************************************
2587 * SetWindowLongA (USER32.@)
2589 * See SetWindowLongW.
2591 LONG WINAPI DECLSPEC_HOTPATCH SetWindowLongA( HWND hwnd, INT offset, LONG newval )
2593 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG), newval, FALSE );
2597 /**********************************************************************
2598 * SetWindowLongW (USER32.@) Set window attribute
2600 * SetWindowLong() alters one of a window's attributes or sets a 32-bit (long)
2601 * value in a window's extra memory.
2603 * The _hwnd_ parameter specifies the handle to a window that
2604 * has extra memory. The _newval_ parameter contains the new
2605 * attribute or extra memory value. If positive, the _offset_
2606 * parameter is the byte-addressed location in the window's extra
2607 * memory to set. If negative, _offset_ specifies the window
2608 * attribute to set, and should be one of the following values:
2610 * GWL_EXSTYLE The window's extended window style
2612 * GWL_STYLE The window's window style.
2614 * GWLP_WNDPROC Pointer to the window's window procedure.
2616 * GWLP_HINSTANCE The window's application instance handle.
2618 * GWLP_ID The window's identifier.
2620 * GWLP_USERDATA The window's user-specified data.
2622 * If the window is a dialog box, the _offset_ parameter can be one of
2623 * the following values:
2625 * DWLP_DLGPROC The address of the window's dialog box procedure.
2627 * DWLP_MSGRESULT The return value of a message
2628 * that the dialog box procedure processed.
2630 * DWLP_USER Application specific information.
2632 * RETURNS
2634 * If successful, returns the previous value located at _offset_. Otherwise,
2635 * returns 0.
2637 * NOTES
2639 * Extra memory for a window class is specified by a nonzero cbWndExtra
2640 * parameter of the WNDCLASS structure passed to RegisterClass() at the
2641 * time of class creation.
2643 * Using GWL_WNDPROC to set a new window procedure effectively creates
2644 * a window subclass. Use CallWindowProc() in the new windows procedure
2645 * to pass messages to the superclass's window procedure.
2647 * The user data is reserved for use by the application which created
2648 * the window.
2650 * Do not use GWL_STYLE to change the window's WS_DISABLED style;
2651 * instead, call the EnableWindow() function to change the window's
2652 * disabled state.
2654 * Do not use GWL_HWNDPARENT to reset the window's parent, use
2655 * SetParent() instead.
2657 * Win95:
2658 * When offset is GWL_STYLE and the calling app's ver is 4.0,
2659 * it sends WM_STYLECHANGING before changing the settings
2660 * and WM_STYLECHANGED afterwards.
2661 * App ver 4.0 can't use SetWindowLong to change WS_EX_TOPMOST.
2663 LONG WINAPI SetWindowLongW(
2664 HWND hwnd, /* [in] window to alter */
2665 INT offset, /* [in] offset, in bytes, of location to alter */
2666 LONG newval /* [in] new value of location */
2668 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG), newval, TRUE );
2672 /*******************************************************************
2673 * GetWindowTextA (USER32.@)
2675 INT WINAPI GetWindowTextA( HWND hwnd, LPSTR lpString, INT nMaxCount )
2677 WCHAR *buffer;
2679 if (!lpString || nMaxCount <= 0) return 0;
2681 if (WIN_IsCurrentProcess( hwnd ))
2683 lpString[0] = 0;
2684 return (INT)SendMessageA( hwnd, WM_GETTEXT, nMaxCount, (LPARAM)lpString );
2687 /* when window belongs to other process, don't send a message */
2688 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, nMaxCount * sizeof(WCHAR) ))) return 0;
2689 get_server_window_text( hwnd, buffer, nMaxCount );
2690 if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, lpString, nMaxCount, NULL, NULL ))
2691 lpString[nMaxCount-1] = 0;
2692 HeapFree( GetProcessHeap(), 0, buffer );
2693 return strlen(lpString);
2697 /*******************************************************************
2698 * InternalGetWindowText (USER32.@)
2700 INT WINAPI InternalGetWindowText(HWND hwnd,LPWSTR lpString,INT nMaxCount )
2702 WND *win;
2704 if (nMaxCount <= 0) return 0;
2705 if (!(win = WIN_GetPtr( hwnd ))) return 0;
2706 if (win == WND_DESKTOP) lpString[0] = 0;
2707 else if (win != WND_OTHER_PROCESS)
2709 if (win->text) lstrcpynW( lpString, win->text, nMaxCount );
2710 else lpString[0] = 0;
2711 WIN_ReleasePtr( win );
2713 else
2715 get_server_window_text( hwnd, lpString, nMaxCount );
2717 return strlenW(lpString);
2721 /*******************************************************************
2722 * GetWindowTextW (USER32.@)
2724 INT WINAPI GetWindowTextW( HWND hwnd, LPWSTR lpString, INT nMaxCount )
2726 if (!lpString || nMaxCount <= 0) return 0;
2728 if (WIN_IsCurrentProcess( hwnd ))
2730 lpString[0] = 0;
2731 return (INT)SendMessageW( hwnd, WM_GETTEXT, nMaxCount, (LPARAM)lpString );
2734 /* when window belongs to other process, don't send a message */
2735 get_server_window_text( hwnd, lpString, nMaxCount );
2736 return strlenW(lpString);
2740 /*******************************************************************
2741 * SetWindowTextA (USER32.@)
2742 * SetWindowText (USER32.@)
2744 BOOL WINAPI DECLSPEC_HOTPATCH SetWindowTextA( HWND hwnd, LPCSTR lpString )
2746 if (is_broadcast(hwnd))
2748 SetLastError( ERROR_INVALID_PARAMETER );
2749 return FALSE;
2751 if (!WIN_IsCurrentProcess( hwnd ))
2752 WARN( "setting text %s of other process window %p should not use SendMessage\n",
2753 debugstr_a(lpString), hwnd );
2754 return (BOOL)SendMessageA( hwnd, WM_SETTEXT, 0, (LPARAM)lpString );
2758 /*******************************************************************
2759 * SetWindowTextW (USER32.@)
2761 BOOL WINAPI DECLSPEC_HOTPATCH SetWindowTextW( HWND hwnd, LPCWSTR lpString )
2763 if (is_broadcast(hwnd))
2765 SetLastError( ERROR_INVALID_PARAMETER );
2766 return FALSE;
2768 if (!WIN_IsCurrentProcess( hwnd ))
2769 WARN( "setting text %s of other process window %p should not use SendMessage\n",
2770 debugstr_w(lpString), hwnd );
2771 return (BOOL)SendMessageW( hwnd, WM_SETTEXT, 0, (LPARAM)lpString );
2775 /*******************************************************************
2776 * GetWindowTextLengthA (USER32.@)
2778 INT WINAPI GetWindowTextLengthA( HWND hwnd )
2780 return SendMessageA( hwnd, WM_GETTEXTLENGTH, 0, 0 );
2783 /*******************************************************************
2784 * GetWindowTextLengthW (USER32.@)
2786 INT WINAPI GetWindowTextLengthW( HWND hwnd )
2788 return SendMessageW( hwnd, WM_GETTEXTLENGTH, 0, 0 );
2792 /*******************************************************************
2793 * IsWindow (USER32.@)
2795 BOOL WINAPI IsWindow( HWND hwnd )
2797 WND *ptr;
2798 BOOL ret;
2800 if (!(ptr = WIN_GetPtr( hwnd ))) return FALSE;
2801 if (ptr == WND_DESKTOP) return TRUE;
2803 if (ptr != WND_OTHER_PROCESS)
2805 WIN_ReleasePtr( ptr );
2806 return TRUE;
2809 /* check other processes */
2810 SERVER_START_REQ( get_window_info )
2812 req->handle = wine_server_user_handle( hwnd );
2813 ret = !wine_server_call_err( req );
2815 SERVER_END_REQ;
2816 return ret;
2820 /***********************************************************************
2821 * GetWindowThreadProcessId (USER32.@)
2823 DWORD WINAPI GetWindowThreadProcessId( HWND hwnd, LPDWORD process )
2825 WND *ptr;
2826 DWORD tid = 0;
2828 if (!(ptr = WIN_GetPtr( hwnd )))
2830 SetLastError( ERROR_INVALID_WINDOW_HANDLE);
2831 return 0;
2834 if (ptr != WND_OTHER_PROCESS && ptr != WND_DESKTOP)
2836 /* got a valid window */
2837 tid = ptr->tid;
2838 if (process) *process = GetCurrentProcessId();
2839 WIN_ReleasePtr( ptr );
2840 return tid;
2843 /* check other processes */
2844 SERVER_START_REQ( get_window_info )
2846 req->handle = wine_server_user_handle( hwnd );
2847 if (!wine_server_call_err( req ))
2849 tid = (DWORD)reply->tid;
2850 if (process) *process = (DWORD)reply->pid;
2853 SERVER_END_REQ;
2854 return tid;
2858 /*****************************************************************
2859 * GetParent (USER32.@)
2861 HWND WINAPI GetParent( HWND hwnd )
2863 WND *wndPtr;
2864 HWND retvalue = 0;
2866 if (!(wndPtr = WIN_GetPtr( hwnd )))
2868 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2869 return 0;
2871 if (wndPtr == WND_DESKTOP) return 0;
2872 if (wndPtr == WND_OTHER_PROCESS)
2874 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
2875 if (style & (WS_POPUP | WS_CHILD))
2877 SERVER_START_REQ( get_window_tree )
2879 req->handle = wine_server_user_handle( hwnd );
2880 if (!wine_server_call_err( req ))
2882 if (style & WS_POPUP) retvalue = wine_server_ptr_handle( reply->owner );
2883 else if (style & WS_CHILD) retvalue = wine_server_ptr_handle( reply->parent );
2886 SERVER_END_REQ;
2889 else
2891 if (wndPtr->dwStyle & WS_POPUP) retvalue = wndPtr->owner;
2892 else if (wndPtr->dwStyle & WS_CHILD) retvalue = wndPtr->parent;
2893 WIN_ReleasePtr( wndPtr );
2895 return retvalue;
2899 /*****************************************************************
2900 * GetAncestor (USER32.@)
2902 HWND WINAPI GetAncestor( HWND hwnd, UINT type )
2904 WND *win;
2905 HWND *list, ret = 0;
2907 switch(type)
2909 case GA_PARENT:
2910 if (!(win = WIN_GetPtr( hwnd )))
2912 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2913 return 0;
2915 if (win == WND_DESKTOP) return 0;
2916 if (win != WND_OTHER_PROCESS)
2918 ret = win->parent;
2919 WIN_ReleasePtr( win );
2921 else /* need to query the server */
2923 SERVER_START_REQ( get_window_tree )
2925 req->handle = wine_server_user_handle( hwnd );
2926 if (!wine_server_call_err( req )) ret = wine_server_ptr_handle( reply->parent );
2928 SERVER_END_REQ;
2930 break;
2932 case GA_ROOT:
2933 if (!(list = list_window_parents( hwnd ))) return 0;
2935 if (!list[0] || !list[1]) ret = WIN_GetFullHandle( hwnd ); /* top-level window */
2936 else
2938 int count = 2;
2939 while (list[count]) count++;
2940 ret = list[count - 2]; /* get the one before the desktop */
2942 HeapFree( GetProcessHeap(), 0, list );
2943 break;
2945 case GA_ROOTOWNER:
2946 if (is_desktop_window( hwnd )) return 0;
2947 ret = WIN_GetFullHandle( hwnd );
2948 for (;;)
2950 HWND parent = GetParent( ret );
2951 if (!parent) break;
2952 ret = parent;
2954 break;
2956 return ret;
2960 /*****************************************************************
2961 * SetParent (USER32.@)
2963 HWND WINAPI SetParent( HWND hwnd, HWND parent )
2965 HWND full_handle;
2966 HWND old_parent = 0;
2967 BOOL was_visible;
2968 WND *wndPtr;
2969 POINT pt;
2970 BOOL ret;
2972 if (is_broadcast(hwnd) || is_broadcast(parent))
2974 SetLastError(ERROR_INVALID_PARAMETER);
2975 return 0;
2978 if (!parent) parent = GetDesktopWindow();
2979 else if (parent == HWND_MESSAGE) parent = get_hwnd_message_parent();
2980 else parent = WIN_GetFullHandle( parent );
2982 if (!IsWindow( parent ))
2984 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2985 return 0;
2988 /* Some applications try to set a child as a parent */
2989 if (IsChild(hwnd, parent))
2991 SetLastError( ERROR_INVALID_PARAMETER );
2992 return 0;
2995 if (!(full_handle = WIN_IsCurrentThread( hwnd )))
2996 return (HWND)SendMessageW( hwnd, WM_WINE_SETPARENT, (WPARAM)parent, 0 );
2998 if (full_handle == parent)
3000 SetLastError( ERROR_INVALID_PARAMETER );
3001 return 0;
3004 /* Windows hides the window first, then shows it again
3005 * including the WM_SHOWWINDOW messages and all */
3006 was_visible = ShowWindow( hwnd, SW_HIDE );
3008 wndPtr = WIN_GetPtr( hwnd );
3009 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return 0;
3011 pt.x = wndPtr->rectWindow.left;
3012 pt.y = wndPtr->rectWindow.top;
3014 SERVER_START_REQ( set_parent )
3016 req->handle = wine_server_user_handle( hwnd );
3017 req->parent = wine_server_user_handle( parent );
3018 if ((ret = !wine_server_call( req )))
3020 old_parent = wine_server_ptr_handle( reply->old_parent );
3021 wndPtr->parent = parent = wine_server_ptr_handle( reply->full_parent );
3025 SERVER_END_REQ;
3026 WIN_ReleasePtr( wndPtr );
3027 if (!ret) return 0;
3029 USER_Driver->pSetParent( full_handle, parent, old_parent );
3031 /* SetParent additionally needs to make hwnd the topmost window
3032 in the x-order and send the expected WM_WINDOWPOSCHANGING and
3033 WM_WINDOWPOSCHANGED notification messages.
3035 SetWindowPos( hwnd, HWND_TOP, pt.x, pt.y, 0, 0, SWP_NOSIZE );
3037 if (was_visible) ShowWindow( hwnd, SW_SHOW );
3039 return old_parent;
3043 /*******************************************************************
3044 * IsChild (USER32.@)
3046 BOOL WINAPI IsChild( HWND parent, HWND child )
3048 HWND *list;
3049 int i;
3050 BOOL ret = FALSE;
3052 if (!(GetWindowLongW( child, GWL_STYLE ) & WS_CHILD)) return FALSE;
3053 if (!(list = list_window_parents( child ))) return FALSE;
3054 parent = WIN_GetFullHandle( parent );
3055 for (i = 0; list[i]; i++)
3057 if (list[i] == parent)
3059 ret = list[i] && list[i+1];
3060 break;
3062 if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_CHILD)) break;
3064 HeapFree( GetProcessHeap(), 0, list );
3065 return ret;
3069 /***********************************************************************
3070 * IsWindowVisible (USER32.@)
3072 BOOL WINAPI IsWindowVisible( HWND hwnd )
3074 HWND *list;
3075 BOOL retval = TRUE;
3076 int i;
3078 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)) return FALSE;
3079 if (!(list = list_window_parents( hwnd ))) return TRUE;
3080 if (list[0])
3082 for (i = 0; list[i+1]; i++)
3083 if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_VISIBLE)) break;
3084 retval = !list[i+1] && (list[i] == GetDesktopWindow()); /* top message window isn't visible */
3086 HeapFree( GetProcessHeap(), 0, list );
3087 return retval;
3091 /***********************************************************************
3092 * WIN_IsWindowDrawable
3094 * hwnd is drawable when it is visible, all parents are not
3095 * minimized, and it is itself not minimized unless we are
3096 * trying to draw its default class icon.
3098 BOOL WIN_IsWindowDrawable( HWND hwnd, BOOL icon )
3100 HWND *list;
3101 BOOL retval = TRUE;
3102 int i;
3103 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
3105 if (!(style & WS_VISIBLE)) return FALSE;
3106 if ((style & WS_MINIMIZE) && icon && GetClassLongPtrW( hwnd, GCLP_HICON )) return FALSE;
3108 if (!(list = list_window_parents( hwnd ))) return TRUE;
3109 if (list[0])
3111 for (i = 0; list[i+1]; i++)
3112 if ((GetWindowLongW( list[i], GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != WS_VISIBLE)
3113 break;
3114 retval = !list[i+1] && (list[i] == GetDesktopWindow()); /* top message window isn't visible */
3116 HeapFree( GetProcessHeap(), 0, list );
3117 return retval;
3121 /*******************************************************************
3122 * GetTopWindow (USER32.@)
3124 HWND WINAPI GetTopWindow( HWND hwnd )
3126 if (!hwnd) hwnd = GetDesktopWindow();
3127 return GetWindow( hwnd, GW_CHILD );
3131 /*******************************************************************
3132 * GetWindow (USER32.@)
3134 HWND WINAPI GetWindow( HWND hwnd, UINT rel )
3136 HWND retval = 0;
3138 if (rel == GW_OWNER) /* this one may be available locally */
3140 WND *wndPtr = WIN_GetPtr( hwnd );
3141 if (!wndPtr)
3143 SetLastError( ERROR_INVALID_HANDLE );
3144 return 0;
3146 if (wndPtr == WND_DESKTOP) return 0;
3147 if (wndPtr != WND_OTHER_PROCESS)
3149 retval = wndPtr->owner;
3150 WIN_ReleasePtr( wndPtr );
3151 return retval;
3153 /* else fall through to server call */
3156 SERVER_START_REQ( get_window_tree )
3158 req->handle = wine_server_user_handle( hwnd );
3159 if (!wine_server_call_err( req ))
3161 switch(rel)
3163 case GW_HWNDFIRST:
3164 retval = wine_server_ptr_handle( reply->first_sibling );
3165 break;
3166 case GW_HWNDLAST:
3167 retval = wine_server_ptr_handle( reply->last_sibling );
3168 break;
3169 case GW_HWNDNEXT:
3170 retval = wine_server_ptr_handle( reply->next_sibling );
3171 break;
3172 case GW_HWNDPREV:
3173 retval = wine_server_ptr_handle( reply->prev_sibling );
3174 break;
3175 case GW_OWNER:
3176 retval = wine_server_ptr_handle( reply->owner );
3177 break;
3178 case GW_CHILD:
3179 retval = wine_server_ptr_handle( reply->first_child );
3180 break;
3184 SERVER_END_REQ;
3185 return retval;
3189 /*******************************************************************
3190 * ShowOwnedPopups (USER32.@)
3192 BOOL WINAPI ShowOwnedPopups( HWND owner, BOOL fShow )
3194 int count = 0;
3195 WND *pWnd;
3196 HWND *win_array = WIN_ListChildren( GetDesktopWindow() );
3198 if (!win_array) return TRUE;
3200 while (win_array[count]) count++;
3201 while (--count >= 0)
3203 if (GetWindow( win_array[count], GW_OWNER ) != owner) continue;
3204 if (!(pWnd = WIN_GetPtr( win_array[count] ))) continue;
3205 if (pWnd == WND_OTHER_PROCESS) continue;
3206 if (fShow)
3208 if (pWnd->flags & WIN_NEEDS_SHOW_OWNEDPOPUP)
3210 WIN_ReleasePtr( pWnd );
3211 /* In Windows, ShowOwnedPopups(TRUE) generates
3212 * WM_SHOWWINDOW messages with SW_PARENTOPENING,
3213 * regardless of the state of the owner
3215 SendMessageW(win_array[count], WM_SHOWWINDOW, SW_SHOWNORMAL, SW_PARENTOPENING);
3216 continue;
3219 else
3221 if (pWnd->dwStyle & WS_VISIBLE)
3223 WIN_ReleasePtr( pWnd );
3224 /* In Windows, ShowOwnedPopups(FALSE) generates
3225 * WM_SHOWWINDOW messages with SW_PARENTCLOSING,
3226 * regardless of the state of the owner
3228 SendMessageW(win_array[count], WM_SHOWWINDOW, SW_HIDE, SW_PARENTCLOSING);
3229 continue;
3232 WIN_ReleasePtr( pWnd );
3234 HeapFree( GetProcessHeap(), 0, win_array );
3235 return TRUE;
3239 /*******************************************************************
3240 * GetLastActivePopup (USER32.@)
3242 HWND WINAPI GetLastActivePopup( HWND hwnd )
3244 HWND retval = hwnd;
3246 SERVER_START_REQ( get_window_info )
3248 req->handle = wine_server_user_handle( hwnd );
3249 if (!wine_server_call_err( req )) retval = wine_server_ptr_handle( reply->last_active );
3251 SERVER_END_REQ;
3252 return retval;
3256 /*******************************************************************
3257 * WIN_ListChildren
3259 * Build an array of the children of a given window. The array must be
3260 * freed with HeapFree. Returns NULL when no windows are found.
3262 HWND *WIN_ListChildren( HWND hwnd )
3264 if (!hwnd)
3266 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3267 return NULL;
3269 return list_window_children( 0, hwnd, NULL, 0 );
3273 /*******************************************************************
3274 * EnumWindows (USER32.@)
3276 BOOL WINAPI EnumWindows( WNDENUMPROC lpEnumFunc, LPARAM lParam )
3278 HWND *list;
3279 BOOL ret = TRUE;
3280 int i;
3282 USER_CheckNotLock();
3284 /* We have to build a list of all windows first, to avoid */
3285 /* unpleasant side-effects, for instance if the callback */
3286 /* function changes the Z-order of the windows. */
3288 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return TRUE;
3290 /* Now call the callback function for every window */
3292 for (i = 0; list[i]; i++)
3294 /* Make sure that the window still exists */
3295 if (!IsWindow( list[i] )) continue;
3296 if (!(ret = lpEnumFunc( list[i], lParam ))) break;
3298 HeapFree( GetProcessHeap(), 0, list );
3299 return ret;
3303 /**********************************************************************
3304 * EnumThreadWindows (USER32.@)
3306 BOOL WINAPI EnumThreadWindows( DWORD id, WNDENUMPROC func, LPARAM lParam )
3308 HWND *list;
3309 int i;
3310 BOOL ret = TRUE;
3312 USER_CheckNotLock();
3314 if (!(list = list_window_children( 0, GetDesktopWindow(), NULL, id ))) return TRUE;
3316 /* Now call the callback function for every window */
3318 for (i = 0; list[i]; i++)
3319 if (!(ret = func( list[i], lParam ))) break;
3320 HeapFree( GetProcessHeap(), 0, list );
3321 return ret;
3325 /***********************************************************************
3326 * EnumDesktopWindows (USER32.@)
3328 BOOL WINAPI EnumDesktopWindows( HDESK desktop, WNDENUMPROC func, LPARAM lparam )
3330 HWND *list;
3331 int i;
3333 USER_CheckNotLock();
3335 if (!(list = list_window_children( desktop, 0, NULL, 0 ))) return TRUE;
3337 for (i = 0; list[i]; i++)
3338 if (!func( list[i], lparam )) break;
3339 HeapFree( GetProcessHeap(), 0, list );
3340 return TRUE;
3344 /**********************************************************************
3345 * WIN_EnumChildWindows
3347 * Helper function for EnumChildWindows().
3349 static BOOL WIN_EnumChildWindows( HWND *list, WNDENUMPROC func, LPARAM lParam )
3351 HWND *childList;
3352 BOOL ret = FALSE;
3354 for ( ; *list; list++)
3356 /* Make sure that the window still exists */
3357 if (!IsWindow( *list )) continue;
3358 /* Build children list first */
3359 childList = WIN_ListChildren( *list );
3361 ret = func( *list, lParam );
3363 if (childList)
3365 if (ret) ret = WIN_EnumChildWindows( childList, func, lParam );
3366 HeapFree( GetProcessHeap(), 0, childList );
3368 if (!ret) return FALSE;
3370 return TRUE;
3374 /**********************************************************************
3375 * EnumChildWindows (USER32.@)
3377 BOOL WINAPI EnumChildWindows( HWND parent, WNDENUMPROC func, LPARAM lParam )
3379 HWND *list;
3380 BOOL ret;
3382 USER_CheckNotLock();
3384 if (!(list = WIN_ListChildren( parent ))) return FALSE;
3385 ret = WIN_EnumChildWindows( list, func, lParam );
3386 HeapFree( GetProcessHeap(), 0, list );
3387 return ret;
3391 /*******************************************************************
3392 * AnyPopup (USER32.@)
3394 BOOL WINAPI AnyPopup(void)
3396 int i;
3397 BOOL retvalue;
3398 HWND *list = WIN_ListChildren( GetDesktopWindow() );
3400 if (!list) return FALSE;
3401 for (i = 0; list[i]; i++)
3403 if (IsWindowVisible( list[i] ) && GetWindow( list[i], GW_OWNER )) break;
3405 retvalue = (list[i] != 0);
3406 HeapFree( GetProcessHeap(), 0, list );
3407 return retvalue;
3411 /*******************************************************************
3412 * FlashWindow (USER32.@)
3414 BOOL WINAPI FlashWindow( HWND hWnd, BOOL bInvert )
3416 WND *wndPtr;
3418 TRACE("%p\n", hWnd);
3420 if (IsIconic( hWnd ))
3422 RedrawWindow( hWnd, 0, 0, RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW | RDW_FRAME );
3424 wndPtr = WIN_GetPtr(hWnd);
3425 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
3426 if (bInvert && !(wndPtr->flags & WIN_NCACTIVATED))
3428 wndPtr->flags |= WIN_NCACTIVATED;
3430 else
3432 wndPtr->flags &= ~WIN_NCACTIVATED;
3434 WIN_ReleasePtr( wndPtr );
3435 return TRUE;
3437 else
3439 WPARAM wparam;
3441 wndPtr = WIN_GetPtr(hWnd);
3442 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
3443 hWnd = wndPtr->obj.handle; /* make it a full handle */
3445 if (bInvert) wparam = !(wndPtr->flags & WIN_NCACTIVATED);
3446 else wparam = (hWnd == GetForegroundWindow());
3448 WIN_ReleasePtr( wndPtr );
3449 SendMessageW( hWnd, WM_NCACTIVATE, wparam, 0 );
3450 return wparam;
3454 /*******************************************************************
3455 * FlashWindowEx (USER32.@)
3457 BOOL WINAPI FlashWindowEx( PFLASHWINFO pfwi )
3459 FIXME("%p\n", pfwi);
3460 return TRUE;
3463 /*******************************************************************
3464 * GetWindowContextHelpId (USER32.@)
3466 DWORD WINAPI GetWindowContextHelpId( HWND hwnd )
3468 DWORD retval;
3469 WND *wnd = WIN_GetPtr( hwnd );
3470 if (!wnd || wnd == WND_DESKTOP) return 0;
3471 if (wnd == WND_OTHER_PROCESS)
3473 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
3474 return 0;
3476 retval = wnd->helpContext;
3477 WIN_ReleasePtr( wnd );
3478 return retval;
3482 /*******************************************************************
3483 * SetWindowContextHelpId (USER32.@)
3485 BOOL WINAPI SetWindowContextHelpId( HWND hwnd, DWORD id )
3487 WND *wnd = WIN_GetPtr( hwnd );
3488 if (!wnd || wnd == WND_DESKTOP) return FALSE;
3489 if (wnd == WND_OTHER_PROCESS)
3491 if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
3492 return FALSE;
3494 wnd->helpContext = id;
3495 WIN_ReleasePtr( wnd );
3496 return TRUE;
3500 /*******************************************************************
3501 * DragDetect (USER32.@)
3503 BOOL WINAPI DragDetect( HWND hWnd, POINT pt )
3505 MSG msg;
3506 RECT rect;
3507 WORD wDragWidth = GetSystemMetrics(SM_CXDRAG);
3508 WORD wDragHeight= GetSystemMetrics(SM_CYDRAG);
3510 rect.left = pt.x - wDragWidth;
3511 rect.right = pt.x + wDragWidth;
3513 rect.top = pt.y - wDragHeight;
3514 rect.bottom = pt.y + wDragHeight;
3516 SetCapture(hWnd);
3518 while(1)
3520 while (PeekMessageW( &msg, 0, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE ))
3522 if( msg.message == WM_LBUTTONUP )
3524 ReleaseCapture();
3525 return FALSE;
3527 if( msg.message == WM_MOUSEMOVE )
3529 POINT tmp;
3530 tmp.x = (short)LOWORD(msg.lParam);
3531 tmp.y = (short)HIWORD(msg.lParam);
3532 if( !PtInRect( &rect, tmp ))
3534 ReleaseCapture();
3535 return TRUE;
3539 WaitMessage();
3541 return FALSE;
3544 /******************************************************************************
3545 * GetWindowModuleFileNameA (USER32.@)
3547 UINT WINAPI GetWindowModuleFileNameA( HWND hwnd, LPSTR module, UINT size )
3549 WND *win;
3550 HINSTANCE hinst;
3552 TRACE( "%p, %p, %u\n", hwnd, module, size );
3554 win = WIN_GetPtr( hwnd );
3555 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP)
3557 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3558 return 0;
3560 hinst = win->hInstance;
3561 WIN_ReleasePtr( win );
3563 return GetModuleFileNameA( hinst, module, size );
3566 /******************************************************************************
3567 * GetWindowModuleFileNameW (USER32.@)
3569 UINT WINAPI GetWindowModuleFileNameW( HWND hwnd, LPWSTR module, UINT size )
3571 WND *win;
3572 HINSTANCE hinst;
3574 TRACE( "%p, %p, %u\n", hwnd, module, size );
3576 win = WIN_GetPtr( hwnd );
3577 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP)
3579 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3580 return 0;
3582 hinst = win->hInstance;
3583 WIN_ReleasePtr( win );
3585 return GetModuleFileNameW( hinst, module, size );
3588 /******************************************************************************
3589 * GetWindowInfo (USER32.@)
3591 * Note: tests show that Windows doesn't check cbSize of the structure.
3593 BOOL WINAPI DECLSPEC_HOTPATCH GetWindowInfo( HWND hwnd, PWINDOWINFO pwi)
3595 if (!pwi) return FALSE;
3596 if (!WIN_GetRectangles( hwnd, COORDS_SCREEN, &pwi->rcWindow, &pwi->rcClient )) return FALSE;
3598 pwi->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
3599 pwi->dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
3600 pwi->dwWindowStatus = ((GetActiveWindow() == hwnd) ? WS_ACTIVECAPTION : 0);
3602 pwi->cxWindowBorders = pwi->rcClient.left - pwi->rcWindow.left;
3603 pwi->cyWindowBorders = pwi->rcWindow.bottom - pwi->rcClient.bottom;
3605 pwi->atomWindowType = GetClassLongW( hwnd, GCW_ATOM );
3606 pwi->wCreatorVersion = 0x0400;
3608 return TRUE;
3611 /******************************************************************************
3612 * SwitchDesktop (USER32.@)
3614 * NOTES: Sets the current input or interactive desktop.
3616 BOOL WINAPI SwitchDesktop( HDESK hDesktop)
3618 FIXME("(hwnd %p) stub!\n", hDesktop);
3619 return TRUE;
3623 /***********************************************************************
3624 * __wine_set_pixel_format
3626 BOOL CDECL __wine_set_pixel_format( HWND hwnd, int format )
3628 WND *win = WIN_GetPtr( hwnd );
3630 if (!win || win == WND_DESKTOP || win == WND_OTHER_PROCESS)
3632 WARN( "setting format %d on win %p not supported\n", format, hwnd );
3633 return FALSE;
3635 win->pixel_format = format;
3636 WIN_ReleasePtr( win );
3638 update_window_state( hwnd );
3639 return TRUE;
3643 /*****************************************************************************
3644 * SetLayeredWindowAttributes (USER32.@)
3646 BOOL WINAPI SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
3648 BOOL ret;
3650 TRACE("(%p,%08x,%d,%x): stub!\n", hwnd, key, alpha, flags);
3652 SERVER_START_REQ( set_window_layered_info )
3654 req->handle = wine_server_user_handle( hwnd );
3655 req->color_key = key;
3656 req->alpha = alpha;
3657 req->flags = flags;
3658 ret = !wine_server_call_err( req );
3660 SERVER_END_REQ;
3662 if (ret) USER_Driver->pSetLayeredWindowAttributes( hwnd, key, alpha, flags );
3664 return ret;
3668 /*****************************************************************************
3669 * GetLayeredWindowAttributes (USER32.@)
3671 BOOL WINAPI GetLayeredWindowAttributes( HWND hwnd, COLORREF *key, BYTE *alpha, DWORD *flags )
3673 BOOL ret;
3675 SERVER_START_REQ( get_window_layered_info )
3677 req->handle = wine_server_user_handle( hwnd );
3678 if ((ret = !wine_server_call_err( req )))
3680 if (key) *key = reply->color_key;
3681 if (alpha) *alpha = reply->alpha;
3682 if (flags) *flags = reply->flags;
3685 SERVER_END_REQ;
3687 return ret;
3691 /*****************************************************************************
3692 * UpdateLayeredWindowIndirect (USER32.@)
3694 BOOL WINAPI UpdateLayeredWindowIndirect( HWND hwnd, const UPDATELAYEREDWINDOWINFO *info )
3696 DWORD flags = SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW;
3697 RECT window_rect, client_rect;
3698 SIZE offset;
3700 if (!info ||
3701 info->cbSize != sizeof(*info) ||
3702 info->dwFlags & ~(ULW_COLORKEY | ULW_ALPHA | ULW_OPAQUE | ULW_EX_NORESIZE) ||
3703 !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED) ||
3704 GetLayeredWindowAttributes( hwnd, NULL, NULL, NULL ))
3706 SetLastError( ERROR_INVALID_PARAMETER );
3707 return FALSE;
3710 WIN_GetRectangles( hwnd, COORDS_PARENT, &window_rect, &client_rect );
3712 if (info->pptDst)
3714 offset.cx = info->pptDst->x - window_rect.left;
3715 offset.cy = info->pptDst->y - window_rect.top;
3716 OffsetRect( &client_rect, offset.cx, offset.cy );
3717 OffsetRect( &window_rect, offset.cx, offset.cy );
3718 flags &= ~SWP_NOMOVE;
3720 if (info->psize)
3722 offset.cx = info->psize->cx - (window_rect.right - window_rect.left);
3723 offset.cy = info->psize->cy - (window_rect.bottom - window_rect.top);
3724 if (info->psize->cx <= 0 || info->psize->cy <= 0)
3726 SetLastError( ERROR_INVALID_PARAMETER );
3727 return FALSE;
3729 if ((info->dwFlags & ULW_EX_NORESIZE) && (offset.cx || offset.cy))
3731 SetLastError( ERROR_INCORRECT_SIZE );
3732 return FALSE;
3734 client_rect.right += offset.cx;
3735 client_rect.bottom += offset.cy;
3736 window_rect.right += offset.cx;
3737 window_rect.bottom += offset.cy;
3738 flags &= ~SWP_NOSIZE;
3741 TRACE( "window %p win %s client %s\n", hwnd,
3742 wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(&client_rect) );
3744 if (!USER_Driver->pUpdateLayeredWindow( hwnd, info, &window_rect )) return FALSE;
3746 set_window_pos( hwnd, 0, flags, &window_rect, &client_rect, NULL );
3747 return TRUE;
3751 /*****************************************************************************
3752 * UpdateLayeredWindow (USER32.@)
3754 BOOL WINAPI UpdateLayeredWindow( HWND hwnd, HDC hdcDst, POINT *pptDst, SIZE *psize,
3755 HDC hdcSrc, POINT *pptSrc, COLORREF crKey, BLENDFUNCTION *pblend,
3756 DWORD flags)
3758 UPDATELAYEREDWINDOWINFO info;
3760 if (flags & ULW_EX_NORESIZE) /* only valid for UpdateLayeredWindowIndirect */
3762 SetLastError( ERROR_INVALID_PARAMETER );
3763 return FALSE;
3765 info.cbSize = sizeof(info);
3766 info.hdcDst = hdcDst;
3767 info.pptDst = pptDst;
3768 info.psize = psize;
3769 info.hdcSrc = hdcSrc;
3770 info.pptSrc = pptSrc;
3771 info.crKey = crKey;
3772 info.pblend = pblend;
3773 info.dwFlags = flags;
3774 info.prcDirty = NULL;
3775 return UpdateLayeredWindowIndirect( hwnd, &info );
3779 /******************************************************************************
3780 * GetProcessDefaultLayout [USER32.@]
3782 * Gets the default layout for parentless windows.
3784 BOOL WINAPI GetProcessDefaultLayout( DWORD *layout )
3786 if (!layout)
3788 SetLastError( ERROR_NOACCESS );
3789 return FALSE;
3791 if (process_layout == ~0u)
3793 static const WCHAR translationW[] = { '\\','V','a','r','F','i','l','e','I','n','f','o',
3794 '\\','T','r','a','n','s','l','a','t','i','o','n', 0 };
3795 static const WCHAR filedescW[] = { '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
3796 '\\','%','0','4','x','%','0','4','x',
3797 '\\','F','i','l','e','D','e','s','c','r','i','p','t','i','o','n',0 };
3798 WCHAR *str, buffer[MAX_PATH];
3799 DWORD i, len, version_layout = 0;
3800 DWORD user_lang = GetUserDefaultLangID();
3801 DWORD *languages;
3802 void *data = NULL;
3804 GetModuleFileNameW( 0, buffer, MAX_PATH );
3805 if (!(len = GetFileVersionInfoSizeW( buffer, NULL ))) goto done;
3806 if (!(data = HeapAlloc( GetProcessHeap(), 0, len ))) goto done;
3807 if (!GetFileVersionInfoW( buffer, 0, len, data )) goto done;
3808 if (!VerQueryValueW( data, translationW, (void **)&languages, &len ) || !len) goto done;
3810 len /= sizeof(DWORD);
3811 for (i = 0; i < len; i++) if (LOWORD(languages[i]) == user_lang) break;
3812 if (i == len) /* try neutral language */
3813 for (i = 0; i < len; i++)
3814 if (LOWORD(languages[i]) == MAKELANGID( PRIMARYLANGID(user_lang), SUBLANG_NEUTRAL )) break;
3815 if (i == len) i = 0; /* default to the first one */
3817 sprintfW( buffer, filedescW, LOWORD(languages[i]), HIWORD(languages[i]) );
3818 if (!VerQueryValueW( data, buffer, (void **)&str, &len )) goto done;
3819 TRACE( "found description %s\n", debugstr_w( str ));
3820 if (str[0] == 0x200e && str[1] == 0x200e) version_layout = LAYOUT_RTL;
3822 done:
3823 HeapFree( GetProcessHeap(), 0, data );
3824 process_layout = version_layout;
3826 *layout = process_layout;
3827 return TRUE;
3831 /******************************************************************************
3832 * SetProcessDefaultLayout [USER32.@]
3834 * Sets the default layout for parentless windows.
3836 BOOL WINAPI SetProcessDefaultLayout( DWORD layout )
3838 process_layout = layout;
3839 return TRUE;
3843 /* 64bit versions */
3845 #ifdef GetWindowLongPtrW
3846 #undef GetWindowLongPtrW
3847 #endif
3849 #ifdef GetWindowLongPtrA
3850 #undef GetWindowLongPtrA
3851 #endif
3853 #ifdef SetWindowLongPtrW
3854 #undef SetWindowLongPtrW
3855 #endif
3857 #ifdef SetWindowLongPtrA
3858 #undef SetWindowLongPtrA
3859 #endif
3861 /*****************************************************************************
3862 * GetWindowLongPtrW (USER32.@)
3864 LONG_PTR WINAPI GetWindowLongPtrW( HWND hwnd, INT offset )
3866 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG_PTR), TRUE );
3869 /*****************************************************************************
3870 * GetWindowLongPtrA (USER32.@)
3872 LONG_PTR WINAPI GetWindowLongPtrA( HWND hwnd, INT offset )
3874 return WIN_GetWindowLong( hwnd, offset, sizeof(LONG_PTR), FALSE );
3877 /*****************************************************************************
3878 * SetWindowLongPtrW (USER32.@)
3880 LONG_PTR WINAPI SetWindowLongPtrW( HWND hwnd, INT offset, LONG_PTR newval )
3882 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG_PTR), newval, TRUE );
3885 /*****************************************************************************
3886 * SetWindowLongPtrA (USER32.@)
3888 LONG_PTR WINAPI SetWindowLongPtrA( HWND hwnd, INT offset, LONG_PTR newval )
3890 return WIN_SetWindowLong( hwnd, offset, sizeof(LONG_PTR), newval, FALSE );
3893 /*****************************************************************************
3894 * RegisterTouchWindow (USER32.@)
3896 BOOL WINAPI RegisterTouchWindow(HWND hwnd, ULONG flags)
3898 FIXME("(%p %08x): stub\n", hwnd, flags);
3899 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
3900 return FALSE;