win.c 108 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3
/*
 * Window related functions
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
4
 * Copyright 1993, 1994 Alexandre Julliard
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
Alexandre Julliard's avatar
Alexandre Julliard committed
19
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
20

21 22 23
#include "config.h"
#include "wine/port.h"

24
#include <assert.h>
25
#include <stdarg.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
26
#include <stdlib.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
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"
Alexandre Julliard's avatar
Alexandre Julliard committed
33
#include "win.h"
34
#include "user_private.h"
35
#include "controls.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
36
#include "winerror.h"
37
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
38

39
WINE_DEFAULT_DEBUG_CHANNEL(win);
40

41 42
#define NB_USER_HANDLES  ((LAST_USER_HANDLE - FIRST_USER_HANDLE + 1) >> 1)
#define USER_HANDLE_TO_INDEX(hwnd) ((LOWORD(hwnd) - FIRST_USER_HANDLE) >> 1)
43

44
static DWORD process_layout = ~0u;
45

46 47
/**********************************************************************/

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
/* helper for Get/SetWindowLong */
static inline LONG_PTR get_win_data( const void *ptr, UINT size )
{
    if (size == sizeof(WORD))
    {
        WORD ret;
        memcpy( &ret, ptr, sizeof(ret) );
        return ret;
    }
    else if (size == sizeof(DWORD))
    {
        DWORD ret;
        memcpy( &ret, ptr, sizeof(ret) );
        return ret;
    }
    else
    {
        LONG_PTR ret;
        memcpy( &ret, ptr, sizeof(ret) );
        return ret;
    }
}

/* helper for Get/SetWindowLong */
static inline void set_win_data( void *ptr, LONG_PTR val, UINT size )
{
    if (size == sizeof(WORD))
    {
        WORD newval = val;
        memcpy( ptr, &newval, sizeof(newval) );
    }
    else if (size == sizeof(DWORD))
    {
        DWORD newval = val;
        memcpy( ptr, &newval, sizeof(newval) );
    }
    else
    {
        memcpy( ptr, &val, sizeof(val) );
    }
}

Alexandre Julliard's avatar
Alexandre Julliard committed
90

91
static void *user_handles[NB_USER_HANDLES];
92

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
/***********************************************************************
 *           alloc_user_handle
 */
HANDLE alloc_user_handle( struct user_object *ptr, enum user_obj_type type )
{
    HANDLE handle = 0;

    SERVER_START_REQ( alloc_user_handle )
    {
        if (!wine_server_call_err( req )) handle = wine_server_ptr_handle( reply->handle );
    }
    SERVER_END_REQ;

    if (handle)
    {
        UINT index = USER_HANDLE_TO_INDEX( handle );

        assert( index < NB_USER_HANDLES );
        ptr->handle = handle;
        ptr->type = type;
113
        InterlockedExchangePointer( &user_handles[index], ptr );
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    }
    return handle;
}


/***********************************************************************
 *           get_user_handle_ptr
 */
void *get_user_handle_ptr( HANDLE handle, enum user_obj_type type )
{
    struct user_object *ptr;
    WORD index = USER_HANDLE_TO_INDEX( handle );

    if (index >= NB_USER_HANDLES) return NULL;

    USER_Lock();
    if ((ptr = user_handles[index]))
    {
        if (ptr->type == type &&
            ((UINT)(UINT_PTR)ptr->handle == (UINT)(UINT_PTR)handle ||
             !HIWORD(handle) || HIWORD(handle) == 0xffff))
            return ptr;
        ptr = NULL;
    }
    else ptr = OBJ_OTHER_PROCESS;
    USER_Unlock();
    return ptr;
}


/***********************************************************************
 *           release_user_handle_ptr
 */
void release_user_handle_ptr( void *ptr )
{
149
    assert( ptr && ptr != OBJ_OTHER_PROCESS );
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    USER_Unlock();
}


/***********************************************************************
 *           free_user_handle
 */
void *free_user_handle( HANDLE handle, enum user_obj_type type )
{
    struct user_object *ptr;
    WORD index = USER_HANDLE_TO_INDEX( handle );

    if ((ptr = get_user_handle_ptr( handle, type )) && ptr != OBJ_OTHER_PROCESS)
    {
        SERVER_START_REQ( free_user_handle )
        {
            req->handle = wine_server_user_handle( handle );
167 168
            if (wine_server_call( req )) ptr = NULL;
            else InterlockedCompareExchangePointer( &user_handles[index], NULL, ptr );
169 170 171 172 173 174 175 176
        }
        SERVER_END_REQ;
        release_user_handle_ptr( ptr );
    }
    return ptr;
}


177 178 179 180 181
/***********************************************************************
 *           create_window_handle
 *
 * Create a window handle with the server.
 */
182
static WND *create_window_handle( HWND parent, HWND owner, LPCWSTR name,
183
                                  HINSTANCE instance, BOOL unicode )
184
{
185
    WORD index;
186
    WND *win;
187
    HWND handle = 0, full_parent = 0, full_owner = 0;
188 189
    struct tagCLASS *class = NULL;
    int extra_bytes = 0;
190

191
    SERVER_START_REQ( create_window )
192
    {
193 194
        req->parent   = wine_server_user_handle( parent );
        req->owner    = wine_server_user_handle( owner );
195
        req->instance = wine_server_client_ptr( instance );
196 197
        if (!(req->atom = get_int_atom_value( name )) && name)
            wine_server_add_data( req, name, strlenW(name)*sizeof(WCHAR) );
198 199
        if (!wine_server_call_err( req ))
        {
200
            handle      = wine_server_ptr_handle( reply->handle );
201 202
            full_parent = wine_server_ptr_handle( reply->parent );
            full_owner  = wine_server_ptr_handle( reply->owner );
203
            extra_bytes = reply->extra;
204
            class       = wine_server_get_ptr( reply->class_ptr );
205
        }
206
    }
207
    SERVER_END_REQ;
208

209 210
    if (!handle)
    {
211
        WARN( "error %d creating window\n", GetLastError() );
212 213 214
        return NULL;
    }

215 216
    if (!(win = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
                           sizeof(WND) + extra_bytes - sizeof(win->wExtra) )))
217
    {
218 219
        SERVER_START_REQ( destroy_window )
        {
220
            req->handle = wine_server_user_handle( handle );
221 222 223 224
            wine_server_call( req );
        }
        SERVER_END_REQ;
        SetLastError( ERROR_NOT_ENOUGH_MEMORY );
225 226
        return NULL;
    }
227

228 229 230 231
    if (!parent)  /* if parent is 0 we don't have a desktop window yet */
    {
        struct user_thread_info *thread_info = get_user_thread_info();

232 233 234 235 236 237 238 239 240 241 242
        if (name == (LPCWSTR)DESKTOP_CLASS_ATOM)
        {
            if (!thread_info->top_window) thread_info->top_window = full_parent ? full_parent : handle;
            else assert( full_parent == thread_info->top_window );
            if (full_parent && !USER_Driver->pCreateDesktopWindow( thread_info->top_window ))
                ERR( "failed to create desktop window\n" );
        }
        else  /* HWND_MESSAGE parent */
        {
            if (!thread_info->msg_window && !full_parent) thread_info->msg_window = handle;
        }
243 244
    }

245 246
    USER_Lock();

247
    index = USER_HANDLE_TO_INDEX(handle);
248
    assert( index < NB_USER_HANDLES );
249 250
    win->obj.handle = handle;
    win->obj.type   = USER_WINDOW;
251 252
    win->parent     = full_parent;
    win->owner      = full_owner;
253 254
    win->class      = class;
    win->winproc    = get_class_winproc( class );
255
    win->cbWndExtra = extra_bytes;
256
    InterlockedExchangePointer( &user_handles[index], win );
257
    if (WINPROC_IsUnicode( win->winproc, unicode )) win->flags |= WIN_ISUNICODE;
258 259 260 261 262 263 264 265 266
    return win;
}


/***********************************************************************
 *           free_window_handle
 *
 * Free a window handle.
 */
267
static void free_window_handle( HWND hwnd )
268
{
269
    struct user_object *ptr;
270
    WORD index = USER_HANDLE_TO_INDEX(hwnd);
271

272
    if ((ptr = get_user_handle_ptr( hwnd, USER_WINDOW )) && ptr != OBJ_OTHER_PROCESS)
273 274 275
    {
        SERVER_START_REQ( destroy_window )
        {
276
            req->handle = wine_server_user_handle( hwnd );
277 278
            if (wine_server_call_err( req )) ptr = NULL;
            else InterlockedCompareExchangePointer( &user_handles[index], NULL, ptr );
279 280
        }
        SERVER_END_REQ;
281 282
        release_user_handle_ptr( ptr );
        HeapFree( GetProcessHeap(), 0, ptr );
283 284 285 286
    }
}


287 288 289 290 291 292
/*******************************************************************
 *           list_window_children
 *
 * Build an array of the children of a given window. The array must be
 * freed with HeapFree. Returns NULL when no windows are found.
 */
293
static HWND *list_window_children( HDESK desktop, HWND hwnd, LPCWSTR class, DWORD tid )
294
{
295
    HWND *list;
296
    int i, size = 128;
297 298 299 300
    ATOM atom = get_int_atom_value( class );

    /* empty class is not the same as NULL class */
    if (!atom && class && !class[0]) return NULL;
301

302
    for (;;)
303
    {
304 305 306 307 308
        int count = 0;

        if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;

        SERVER_START_REQ( get_window_children )
309
        {
310
            req->desktop = wine_server_obj_handle( desktop );
311
            req->parent = wine_server_user_handle( hwnd );
312
            req->tid = tid;
313 314
            req->atom = atom;
            if (!atom && class) wine_server_add_data( req, class, strlenW(class)*sizeof(WCHAR) );
315
            wine_server_set_reply( req, list, (size-1) * sizeof(user_handle_t) );
316
            if (!wine_server_call( req )) count = reply->count;
317
        }
318 319 320
        SERVER_END_REQ;
        if (count && count < size)
        {
321 322 323
            /* start from the end since HWND is potentially larger than user_handle_t */
            for (i = count - 1; i >= 0; i--)
                list[i] = wine_server_ptr_handle( ((user_handle_t *)list)[i] );
324 325 326 327 328 329
            list[count] = 0;
            return list;
        }
        HeapFree( GetProcessHeap(), 0, list );
        if (!count) break;
        size = count + 1;  /* restart with a large enough buffer */
330
    }
331
    return NULL;
332 333 334
}


335 336 337 338 339 340 341 342 343 344
/*******************************************************************
 *           list_window_parents
 *
 * Build an array of all parents of a given window, starting with
 * the immediate parent. The array must be freed with HeapFree.
 */
static HWND *list_window_parents( HWND hwnd )
{
    WND *win;
    HWND current, *list;
345
    int i, pos = 0, size = 16, count = 0;
346 347 348 349 350 351 352 353

    if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) return NULL;

    current = hwnd;
    for (;;)
    {
        if (!(win = WIN_GetPtr( current ))) goto empty;
        if (win == WND_OTHER_PROCESS) break;  /* need to do it the hard way */
354
        if (win == WND_DESKTOP)
355 356
        {
            if (!pos) goto empty;
357
            list[pos] = 0;
358 359
            return list;
        }
360 361
        list[pos] = current = win->parent;
        WIN_ReleasePtr( win );
362
        if (!current) return list;
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
        if (++pos == size - 1)
        {
            /* need to grow the list */
            HWND *new_list = HeapReAlloc( GetProcessHeap(), 0, list, (size+16) * sizeof(HWND) );
            if (!new_list) goto empty;
            list = new_list;
            size += 16;
        }
    }

    /* at least one parent belongs to another process, have to query the server */

    for (;;)
    {
        count = 0;
        SERVER_START_REQ( get_window_parents )
        {
380 381
            req->handle = wine_server_user_handle( hwnd );
            wine_server_set_reply( req, list, (size-1) * sizeof(user_handle_t) );
382 383 384 385 386 387
            if (!wine_server_call( req )) count = reply->count;
        }
        SERVER_END_REQ;
        if (!count) goto empty;
        if (size > count)
        {
388 389 390
            /* start from the end since HWND is potentially larger than user_handle_t */
            for (i = count - 1; i >= 0; i--)
                list[i] = wine_server_ptr_handle( ((user_handle_t *)list)[i] );
391 392 393 394 395 396 397 398 399 400 401 402 403 404
            list[count] = 0;
            return list;
        }
        HeapFree( GetProcessHeap(), 0, list );
        size = count + 1;
        if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) return NULL;
    }

 empty:
    HeapFree( GetProcessHeap(), 0, list );
    return NULL;
}


405 406 407 408 409
/*******************************************************************
 *           send_parent_notify
 */
static void send_parent_notify( HWND hwnd, UINT msg )
{
410 411
    if ((GetWindowLongW( hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD &&
        !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY))
412 413 414 415 416 417
    {
        HWND parent = GetParent(hwnd);
        if (parent && parent != GetDesktopWindow())
            SendMessageW( parent, WM_PARENTNOTIFY,
                          MAKEWPARAM( msg, GetWindowLongPtrW( hwnd, GWLP_ID )), (LPARAM)hwnd );
    }
418 419 420
}


421 422 423 424 425 426 427
/*******************************************************************
 *		get_server_window_text
 *
 * Retrieve the window text from the server.
 */
static void get_server_window_text( HWND hwnd, LPWSTR text, INT count )
{
428 429 430
    size_t len = 0;

    SERVER_START_REQ( get_window_text )
431
    {
432
        req->handle = wine_server_user_handle( hwnd );
433 434
        wine_server_set_reply( req, text, (count - 1) * sizeof(WCHAR) );
        if (!wine_server_call_err( req )) len = wine_server_reply_size(reply);
435
    }
436
    SERVER_END_REQ;
437 438 439 440
    text[len / sizeof(WCHAR)] = 0;
}


441 442 443 444 445
/*******************************************************************
 *           get_hwnd_message_parent
 *
 * Return the parent for HWND_MESSAGE windows.
 */
446
HWND get_hwnd_message_parent(void)
447 448 449 450 451 452 453 454
{
    struct user_thread_info *thread_info = get_user_thread_info();

    if (!thread_info->msg_window) GetDesktopWindow();  /* trigger creation */
    return thread_info->msg_window;
}


455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
/*******************************************************************
 *           is_desktop_window
 *
 * Check if window is the desktop or the HWND_MESSAGE top parent.
 */
BOOL is_desktop_window( HWND hwnd )
{
    struct user_thread_info *thread_info = get_user_thread_info();

    if (!hwnd) return FALSE;
    if (hwnd == thread_info->top_window) return TRUE;
    if (hwnd == thread_info->msg_window) return TRUE;

    if (!HIWORD(hwnd) || HIWORD(hwnd) == 0xffff)
    {
        if (LOWORD(thread_info->top_window) == LOWORD(hwnd)) return TRUE;
        if (LOWORD(thread_info->msg_window) == LOWORD(hwnd)) return TRUE;
    }
    return FALSE;
}


477
/***********************************************************************
478
 *           WIN_GetPtr
479
 *
480
 * Return a pointer to the WND structure if local to the process,
Andreas Mohr's avatar
Andreas Mohr committed
481
 * or WND_OTHER_PROCESS if handle may be valid in other process.
482
 * If ret value is a valid pointer, it must be released with WIN_ReleasePtr.
483
 */
484
WND *WIN_GetPtr( HWND hwnd )
485
{
486
    WND *ptr;
487

488
    if ((ptr = get_user_handle_ptr( hwnd, USER_WINDOW )) == WND_OTHER_PROCESS)
489
    {
490
        if (is_desktop_window( hwnd )) ptr = WND_DESKTOP;
491
    }
492 493 494 495 496 497 498
    return ptr;
}


/***********************************************************************
 *           WIN_IsCurrentProcess
 *
499
 * Check whether a given window belongs to the current process (and return the full handle).
500
 */
501
HWND WIN_IsCurrentProcess( HWND hwnd )
502 503
{
    WND *ptr;
504
    HWND ret;
505

506
    if (!(ptr = WIN_GetPtr( hwnd )) || ptr == WND_OTHER_PROCESS || ptr == WND_DESKTOP) return 0;
507
    ret = ptr->obj.handle;
508 509
    WIN_ReleasePtr( ptr );
    return ret;
510 511 512 513 514 515
}


/***********************************************************************
 *           WIN_IsCurrentThread
 *
516
 * Check whether a given window belongs to the current thread (and return the full handle).
517
 */
518
HWND WIN_IsCurrentThread( HWND hwnd )
519 520
{
    WND *ptr;
521
    HWND ret = 0;
522

523
    if (!(ptr = WIN_GetPtr( hwnd )) || ptr == WND_OTHER_PROCESS || ptr == WND_DESKTOP) return 0;
524
    if (ptr->tid == GetCurrentThreadId()) ret = ptr->obj.handle;
525
    WIN_ReleasePtr( ptr );
526
    return ret;
527 528 529 530
}


/***********************************************************************
531
 *           WIN_GetFullHandle
532
 *
533
 * Convert a possibly truncated window handle to a full 32-bit handle.
534
 */
535
HWND WIN_GetFullHandle( HWND hwnd )
536 537 538
{
    WND *ptr;

539 540
    if (!hwnd || (ULONG_PTR)hwnd >> 16) return hwnd;
    if (LOWORD(hwnd) <= 1 || LOWORD(hwnd) == 0xffff) return hwnd;
541
    /* do sign extension for -2 and -3 */
542
    if (LOWORD(hwnd) >= (WORD)-3) return (HWND)(LONG_PTR)(INT16)LOWORD(hwnd);
543

544 545
    if (!(ptr = WIN_GetPtr( hwnd ))) return hwnd;

546 547 548 549 550
    if (ptr == WND_DESKTOP)
    {
        if (LOWORD(hwnd) == LOWORD(GetDesktopWindow())) return GetDesktopWindow();
        else return get_hwnd_message_parent();
    }
551

552
    if (ptr != WND_OTHER_PROCESS)
553
    {
554
        hwnd = ptr->obj.handle;
555
        WIN_ReleasePtr( ptr );
556 557
    }
    else  /* may belong to another process */
558 559 560
    {
        SERVER_START_REQ( get_window_info )
        {
561 562
            req->handle = wine_server_user_handle( hwnd );
            if (!wine_server_call_err( req )) hwnd = wine_server_ptr_handle( reply->full_handle );
563 564 565 566 567 568 569
        }
        SERVER_END_REQ;
    }
    return hwnd;
}


570 571 572 573 574
/***********************************************************************
 *           WIN_SetOwner
 *
 * Change the owner of a window.
 */
575
HWND WIN_SetOwner( HWND hwnd, HWND owner )
576
{
577
    WND *win = WIN_GetPtr( hwnd );
578
    HWND ret = 0;
579

580
    if (!win || win == WND_DESKTOP) return 0;
581
    if (win == WND_OTHER_PROCESS)
Alexandre Julliard's avatar
Alexandre Julliard committed
582
    {
583
        if (IsWindow(hwnd)) ERR( "cannot set owner %p on other process window %p\n", owner, hwnd );
584
        return 0;
585
    }
586 587
    SERVER_START_REQ( set_window_owner )
    {
588 589
        req->handle = wine_server_user_handle( hwnd );
        req->owner  = wine_server_user_handle( owner );
590 591
        if (!wine_server_call( req ))
        {
592 593
            win->owner = wine_server_ptr_handle( reply->full_owner );
            ret = wine_server_ptr_handle( reply->prev_owner );
594
        }
595 596 597
    }
    SERVER_END_REQ;
    WIN_ReleasePtr( win );
598
    return ret;
599 600 601 602 603 604 605 606
}


/***********************************************************************
 *           WIN_SetStyle
 *
 * Change the style of a window.
 */
607
ULONG WIN_SetStyle( HWND hwnd, ULONG set_bits, ULONG clear_bits )
608 609
{
    BOOL ok;
610
    STYLESTRUCT style;
611 612
    WND *win = WIN_GetPtr( hwnd );

613
    if (!win || win == WND_DESKTOP) return 0;
614 615 616
    if (win == WND_OTHER_PROCESS)
    {
        if (IsWindow(hwnd))
617
            ERR( "cannot set style %x/%x on other process window %p\n",
618
                 set_bits, clear_bits, hwnd );
619 620
        return 0;
    }
621 622 623
    style.styleOld = win->dwStyle;
    style.styleNew = (win->dwStyle | set_bits) & ~clear_bits;
    if (style.styleNew == style.styleOld)
624 625
    {
        WIN_ReleasePtr( win );
626
        return style.styleNew;
627 628 629
    }
    SERVER_START_REQ( set_window_info )
    {
630
        req->handle = wine_server_user_handle( hwnd );
631
        req->flags  = SET_WIN_STYLE;
632
        req->style  = style.styleNew;
633
        req->extra_offset = -1;
634
        if ((ok = !wine_server_call( req )))
635
        {
636 637
            style.styleOld = reply->old_style;
            win->dwStyle = style.styleNew;
638 639 640 641
        }
    }
    SERVER_END_REQ;
    WIN_ReleasePtr( win );
642 643
    if (ok)
    {
644 645
        USER_Driver->pSetWindowStyle( hwnd, GWL_STYLE, &style );
        if ((style.styleOld ^ style.styleNew) & WS_VISIBLE) invalidate_dce( hwnd, NULL );
646
    }
647
    return style.styleOld;
648 649 650
}


651 652 653 654 655
/***********************************************************************
 *           WIN_GetRectangles
 *
 * Get the window and client rectangles.
 */
656
BOOL WIN_GetRectangles( HWND hwnd, enum coords_relative relative, RECT *rectWindow, RECT *rectClient )
657 658 659 660
{
    WND *win = WIN_GetPtr( hwnd );
    BOOL ret = TRUE;

661 662 663 664 665
    if (!win)
    {
        SetLastError( ERROR_INVALID_WINDOW_HANDLE );
        return FALSE;
    }
666 667 668 669
    if (win == WND_DESKTOP)
    {
        RECT rect;
        rect.left = rect.top = 0;
670 671 672 673 674 675 676 677 678 679
        if (hwnd == get_hwnd_message_parent())
        {
            rect.right  = 100;
            rect.bottom = 100;
        }
        else
        {
            rect.right  = GetSystemMetrics(SM_CXSCREEN);
            rect.bottom = GetSystemMetrics(SM_CYSCREEN);
        }
680 681
        if (rectWindow) *rectWindow = rect;
        if (rectClient) *rectClient = rect;
682
        return TRUE;
683
    }
684
    if (win != WND_OTHER_PROCESS)
685
    {
686 687 688
        RECT window_rect = win->rectWindow, client_rect = win->rectClient;

        switch (relative)
689
        {
690 691 692
        case COORDS_CLIENT:
            OffsetRect( &window_rect, -win->rectClient.left, -win->rectClient.top );
            OffsetRect( &client_rect, -win->rectClient.left, -win->rectClient.top );
693 694
            if (win->dwExStyle & WS_EX_LAYOUTRTL)
                mirror_rect( &win->rectClient, &window_rect );
695 696 697 698
            break;
        case COORDS_WINDOW:
            OffsetRect( &window_rect, -win->rectWindow.left, -win->rectWindow.top );
            OffsetRect( &client_rect, -win->rectWindow.left, -win->rectWindow.top );
699 700
            if (win->dwExStyle & WS_EX_LAYOUTRTL)
                mirror_rect( &win->rectWindow, &client_rect );
701 702
            break;
        case COORDS_PARENT:
703 704 705 706 707 708 709 710 711
            if (win->parent)
            {
                WND *parent = WIN_GetPtr( win->parent );
                if (parent == WND_DESKTOP) break;
                if (!parent || parent == WND_OTHER_PROCESS)
                {
                    WIN_ReleasePtr( win );
                    goto other_process;
                }
712 713 714 715 716 717
                if (parent->flags & WIN_CHILDREN_MOVED)
                {
                    WIN_ReleasePtr( parent );
                    WIN_ReleasePtr( win );
                    goto other_process;
                }
718 719 720 721 722 723 724
                if (parent->dwExStyle & WS_EX_LAYOUTRTL)
                {
                    mirror_rect( &parent->rectClient, &window_rect );
                    mirror_rect( &parent->rectClient, &client_rect );
                }
                WIN_ReleasePtr( parent );
            }
725 726 727
            break;
        case COORDS_SCREEN:
            while (win->parent)
728
            {
729 730 731
                WND *parent = WIN_GetPtr( win->parent );
                if (parent == WND_DESKTOP) break;
                if (!parent || parent == WND_OTHER_PROCESS)
732
                {
733 734
                    WIN_ReleasePtr( win );
                    goto other_process;
735
                }
736
                WIN_ReleasePtr( win );
737 738 739 740 741
                if (parent->flags & WIN_CHILDREN_MOVED)
                {
                    WIN_ReleasePtr( parent );
                    goto other_process;
                }
742
                win = parent;
743 744 745 746 747
                if (win->parent)
                {
                    OffsetRect( &window_rect, win->rectClient.left, win->rectClient.top );
                    OffsetRect( &client_rect, win->rectClient.left, win->rectClient.top );
                }
748
            }
749
            break;
750
        }
751 752 753
        if (rectWindow) *rectWindow = window_rect;
        if (rectClient) *rectClient = client_rect;
        WIN_ReleasePtr( win );
754
        return TRUE;
755
    }
756 757 758

other_process:
    SERVER_START_REQ( get_window_rectangles )
759
    {
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
        req->handle = wine_server_user_handle( hwnd );
        req->relative = relative;
        if ((ret = !wine_server_call_err( req )))
        {
            if (rectWindow)
            {
                rectWindow->left   = reply->window.left;
                rectWindow->top    = reply->window.top;
                rectWindow->right  = reply->window.right;
                rectWindow->bottom = reply->window.bottom;
            }
            if (rectClient)
            {
                rectClient->left   = reply->client.left;
                rectClient->top    = reply->client.top;
                rectClient->right  = reply->client.right;
                rectClient->bottom = reply->client.bottom;
            }
        }
779
    }
780
    SERVER_END_REQ;
781 782 783 784
    return ret;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
785 786 787
/***********************************************************************
 *           WIN_DestroyWindow
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
788
 * Destroy storage associated to a window. "Internals" p.358
Alexandre Julliard's avatar
Alexandre Julliard committed
789
 */
790
LRESULT WIN_DestroyWindow( HWND hwnd )
Alexandre Julliard's avatar
Alexandre Julliard committed
791
{
792 793
    WND *wndPtr;
    HWND *list;
794
    HMENU menu = 0, sys_menu;
795
    HWND icon_title;
Alexandre Julliard's avatar
Alexandre Julliard committed
796

797
    TRACE("%p\n", hwnd );
Alexandre Julliard's avatar
Alexandre Julliard committed
798

Alexandre Julliard's avatar
Alexandre Julliard committed
799
    /* free child windows */
800
    if ((list = WIN_ListChildren( hwnd )))
801
    {
802
        int i;
803 804 805 806 807
        for (i = 0; list[i]; i++)
        {
            if (WIN_IsCurrentThread( list[i] )) WIN_DestroyWindow( list[i] );
            else SendMessageW( list[i], WM_WINE_DESTROYWINDOW, 0, 0 );
        }
808
        HeapFree( GetProcessHeap(), 0, list );
809
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
810

811
    /* Unlink now so we won't bother with the children later on */
812 813
    SERVER_START_REQ( set_parent )
    {
814
        req->handle = wine_server_user_handle( hwnd );
815 816 817 818
        req->parent = 0;
        wine_server_call( req );
    }
    SERVER_END_REQ;
819

820 821 822
    /*
     * Send the WM_NCDESTROY to the window being destroyed.
     */
823
    SendMessageW( hwnd, WM_NCDESTROY, 0, 0 );
Alexandre Julliard's avatar
Alexandre Julliard committed
824 825 826 827 828

    /* FIXME: do we need to fake QS_MOUSEMOVE wakebit? */

    /* free resources associated with the window */

829
    if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
830 831
    if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
        menu = (HMENU)wndPtr->wIDmenu;
832
    sys_menu = wndPtr->hSysMenu;
833 834
    free_dce( wndPtr->dce, hwnd );
    wndPtr->dce = NULL;
835
    icon_title = wndPtr->icon_title;
836 837 838
    HeapFree( GetProcessHeap(), 0, wndPtr->text );
    wndPtr->text = NULL;
    HeapFree( GetProcessHeap(), 0, wndPtr->pScroll );
839
    wndPtr->pScroll = NULL;
840 841
    WIN_ReleasePtr( wndPtr );

842
    if (icon_title) DestroyWindow( icon_title );
843 844
    if (menu) DestroyMenu( menu );
    if (sys_menu) DestroyMenu( sys_menu );
Alexandre Julliard's avatar
Alexandre Julliard committed
845

846
    USER_Driver->pDestroyWindow( hwnd );
847 848

    free_window_handle( hwnd );
849
    return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
850 851
}

852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887

/***********************************************************************
 *		destroy_thread_window
 *
 * Destroy a window upon exit of its thread.
 */
static void destroy_thread_window( HWND hwnd )
{
    WND *wndPtr;
    HWND *list;
    HMENU menu = 0, sys_menu = 0;
    WORD index;

    /* free child windows */

    if ((list = WIN_ListChildren( hwnd )))
    {
        int i;
        for (i = 0; list[i]; i++)
        {
            if (WIN_IsCurrentThread( list[i] )) destroy_thread_window( list[i] );
            else SendMessageW( list[i], WM_WINE_DESTROYWINDOW, 0, 0 );
        }
        HeapFree( GetProcessHeap(), 0, list );
    }

    /* destroy the client-side storage */

    index = USER_HANDLE_TO_INDEX(hwnd);
    if (index >= NB_USER_HANDLES) return;
    USER_Lock();
    if ((wndPtr = user_handles[index]))
    {
        if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD) menu = (HMENU)wndPtr->wIDmenu;
        sys_menu = wndPtr->hSysMenu;
        free_dce( wndPtr->dce, hwnd );
888
        InterlockedCompareExchangePointer( &user_handles[index], NULL, wndPtr );
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
    }
    USER_Unlock();

    HeapFree( GetProcessHeap(), 0, wndPtr );
    if (menu) DestroyMenu( menu );
    if (sys_menu) DestroyMenu( sys_menu );
}


/***********************************************************************
 *		destroy_thread_child_windows
 *
 * Destroy child windows upon exit of its thread.
 */
static void destroy_thread_child_windows( HWND hwnd )
{
    HWND *list;
    int i;

    if (WIN_IsCurrentThread( hwnd ))
    {
        destroy_thread_window( hwnd );
    }
    else if ((list = WIN_ListChildren( hwnd )))
    {
        for (i = 0; list[i]; i++) destroy_thread_child_windows( list[i] );
        HeapFree( GetProcessHeap(), 0, list );
    }
}


Alexandre Julliard's avatar
Alexandre Julliard committed
920
/***********************************************************************
921
 *           WIN_DestroyThreadWindows
Alexandre Julliard's avatar
Alexandre Julliard committed
922
 *
923
 * Destroy all children of 'wnd' owned by the current thread.
Alexandre Julliard's avatar
Alexandre Julliard committed
924
 */
925
void WIN_DestroyThreadWindows( HWND hwnd )
Alexandre Julliard's avatar
Alexandre Julliard committed
926
{
927 928
    HWND *list;
    int i;
Alexandre Julliard's avatar
Alexandre Julliard committed
929

930
    if (!(list = WIN_ListChildren( hwnd ))) return;
931 932

    /* reset owners of top-level windows */
933
    for (i = 0; list[i]; i++)
Alexandre Julliard's avatar
Alexandre Julliard committed
934
    {
935 936 937 938 939
        if (!WIN_IsCurrentThread( list[i] ))
        {
            HWND owner = GetWindow( list[i], GW_OWNER );
            if (owner && WIN_IsCurrentThread( owner )) WIN_SetOwner( list[i], 0 );
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
940
    }
941 942

    for (i = 0; list[i]; i++) destroy_thread_child_windows( list[i] );
943
    HeapFree( GetProcessHeap(), 0, list );
Alexandre Julliard's avatar
Alexandre Julliard committed
944 945
}

Alexandre Julliard's avatar
Alexandre Julliard committed
946

947
/***********************************************************************
948
 *           WIN_FixCoordinates
949 950 951 952
 *
 * Fix the coordinates - Helper for WIN_CreateWindowEx.
 * returns default show mode in sw.
 */
953
static void WIN_FixCoordinates( CREATESTRUCTW *cs, INT *sw)
954
{
955
#define IS_DEFAULT(x)  ((x) == CW_USEDEFAULT || (x) == (SHORT)0x8000)
956 957 958 959 960 961 962
    POINT pos[2];

    if (cs->dwExStyle & WS_EX_MDICHILD)
    {
        UINT id = 0;

        MDI_CalcDefaultChildPos(cs->hwndParent, -1, pos, 0, &id);
963
        if (!(cs->style & WS_POPUP)) cs->hMenu = ULongToHandle(id);
964 965 966 967

        TRACE("MDI child id %04x\n", id);
    }

968
    if (cs->style & (WS_CHILD | WS_POPUP))
969
    {
970
        if (cs->dwExStyle & WS_EX_MDICHILD)
971
        {
972
            if (IS_DEFAULT(cs->x))
973
            {
974 975
                cs->x = pos[0].x;
                cs->y = pos[0].y;
976
            }
977 978
            if (IS_DEFAULT(cs->cx) || !cs->cx) cs->cx = pos[1].x;
            if (IS_DEFAULT(cs->cy) || !cs->cy) cs->cy = pos[1].y;
979
        }
980
        else
981
        {
982 983 984 985 986 987 988 989 990
            if (IS_DEFAULT(cs->x)) cs->x = cs->y = 0;
            if (IS_DEFAULT(cs->cx)) cs->cx = cs->cy = 0;
        }
    }
    else  /* overlapped window */
    {
        HMONITOR monitor;
        MONITORINFO mon_info;
        STARTUPINFOW info;
991

992
        if (!IS_DEFAULT(cs->x) && !IS_DEFAULT(cs->cx) && !IS_DEFAULT(cs->cy)) return;
993

994
        monitor = MonitorFromWindow( cs->hwndParent, MONITOR_DEFAULTTOPRIMARY );
995 996 997 998 999 1000 1001 1002 1003 1004
        mon_info.cbSize = sizeof(mon_info);
        GetMonitorInfoW( monitor, &mon_info );
        GetStartupInfoW( &info );

        if (IS_DEFAULT(cs->x))
        {
            if (!IS_DEFAULT(cs->y)) *sw = cs->y;
            cs->x = (info.dwFlags & STARTF_USEPOSITION) ? info.dwX : mon_info.rcWork.left;
            cs->y = (info.dwFlags & STARTF_USEPOSITION) ? info.dwY : mon_info.rcWork.top;
        }
1005

1006 1007 1008
        if (IS_DEFAULT(cs->cx))
        {
            if (info.dwFlags & STARTF_USESIZE)
1009
            {
1010 1011
                cs->cx = info.dwXSize;
                cs->cy = info.dwYSize;
1012
            }
1013
            else
1014
            {
1015 1016
                cs->cx = (mon_info.rcWork.right - mon_info.rcWork.left) * 3 / 4 - cs->x;
                cs->cy = (mon_info.rcWork.bottom - mon_info.rcWork.top) * 3 / 4 - cs->y;
1017
            }
1018
        }
1019 1020 1021 1022
        /* neither x nor cx are default. Check the y values .
         * In the trace we see Outlook and Outlook Express using
         * cy set to CW_USEDEFAULT when opening the address book.
         */
1023 1024
        else if (IS_DEFAULT(cs->cy))
        {
1025
            FIXME("Strange use of CW_USEDEFAULT in nHeight\n");
1026
            cs->cy = (mon_info.rcWork.bottom - mon_info.rcWork.top) * 3 / 4 - cs->y;
1027
        }
1028
    }
1029
#undef IS_DEFAULT
1030 1031
}

1032 1033 1034 1035 1036 1037
/***********************************************************************
 *           dump_window_styles
 */
static void dump_window_styles( DWORD style, DWORD exstyle )
{
    TRACE( "style:" );
1038 1039 1040 1041 1042 1043 1044 1045 1046
    if(style & WS_POPUP) TRACE(" WS_POPUP");
    if(style & WS_CHILD) TRACE(" WS_CHILD");
    if(style & WS_MINIMIZE) TRACE(" WS_MINIMIZE");
    if(style & WS_VISIBLE) TRACE(" WS_VISIBLE");
    if(style & WS_DISABLED) TRACE(" WS_DISABLED");
    if(style & WS_CLIPSIBLINGS) TRACE(" WS_CLIPSIBLINGS");
    if(style & WS_CLIPCHILDREN) TRACE(" WS_CLIPCHILDREN");
    if(style & WS_MAXIMIZE) TRACE(" WS_MAXIMIZE");
    if((style & WS_CAPTION) == WS_CAPTION) TRACE(" WS_CAPTION");
1047 1048
    else
    {
1049 1050
        if(style & WS_BORDER) TRACE(" WS_BORDER");
        if(style & WS_DLGFRAME) TRACE(" WS_DLGFRAME");
1051
    }
1052 1053 1054 1055
    if(style & WS_VSCROLL) TRACE(" WS_VSCROLL");
    if(style & WS_HSCROLL) TRACE(" WS_HSCROLL");
    if(style & WS_SYSMENU) TRACE(" WS_SYSMENU");
    if(style & WS_THICKFRAME) TRACE(" WS_THICKFRAME");
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
    if (style & WS_CHILD)
    {
        if(style & WS_GROUP) TRACE(" WS_GROUP");
        if(style & WS_TABSTOP) TRACE(" WS_TABSTOP");
    }
    else
    {
        if(style & WS_MINIMIZEBOX) TRACE(" WS_MINIMIZEBOX");
        if(style & WS_MAXIMIZEBOX) TRACE(" WS_MAXIMIZEBOX");
    }
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087

    /* FIXME: Add dumping of BS_/ES_/SBS_/LBS_/CBS_/DS_/etc. styles */
#define DUMPED_STYLES \
    (WS_POPUP | \
     WS_CHILD | \
     WS_MINIMIZE | \
     WS_VISIBLE | \
     WS_DISABLED | \
     WS_CLIPSIBLINGS | \
     WS_CLIPCHILDREN | \
     WS_MAXIMIZE | \
     WS_BORDER | \
     WS_DLGFRAME | \
     WS_VSCROLL | \
     WS_HSCROLL | \
     WS_SYSMENU | \
     WS_THICKFRAME | \
     WS_GROUP | \
     WS_TABSTOP | \
     WS_MINIMIZEBOX | \
     WS_MAXIMIZEBOX)

1088 1089
    if(style & ~DUMPED_STYLES) TRACE(" %08lx", style & ~DUMPED_STYLES);
    TRACE("\n");
1090 1091 1092
#undef DUMPED_STYLES

    TRACE( "exstyle:" );
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
    if(exstyle & WS_EX_DLGMODALFRAME) TRACE(" WS_EX_DLGMODALFRAME");
    if(exstyle & WS_EX_DRAGDETECT) TRACE(" WS_EX_DRAGDETECT");
    if(exstyle & WS_EX_NOPARENTNOTIFY) TRACE(" WS_EX_NOPARENTNOTIFY");
    if(exstyle & WS_EX_TOPMOST) TRACE(" WS_EX_TOPMOST");
    if(exstyle & WS_EX_ACCEPTFILES) TRACE(" WS_EX_ACCEPTFILES");
    if(exstyle & WS_EX_TRANSPARENT) TRACE(" WS_EX_TRANSPARENT");
    if(exstyle & WS_EX_MDICHILD) TRACE(" WS_EX_MDICHILD");
    if(exstyle & WS_EX_TOOLWINDOW) TRACE(" WS_EX_TOOLWINDOW");
    if(exstyle & WS_EX_WINDOWEDGE) TRACE(" WS_EX_WINDOWEDGE");
    if(exstyle & WS_EX_CLIENTEDGE) TRACE(" WS_EX_CLIENTEDGE");
    if(exstyle & WS_EX_CONTEXTHELP) TRACE(" WS_EX_CONTEXTHELP");
    if(exstyle & WS_EX_RIGHT) TRACE(" WS_EX_RIGHT");
    if(exstyle & WS_EX_RTLREADING) TRACE(" WS_EX_RTLREADING");
    if(exstyle & WS_EX_LEFTSCROLLBAR) TRACE(" WS_EX_LEFTSCROLLBAR");
    if(exstyle & WS_EX_CONTROLPARENT) TRACE(" WS_EX_CONTROLPARENT");
    if(exstyle & WS_EX_STATICEDGE) TRACE(" WS_EX_STATICEDGE");
    if(exstyle & WS_EX_APPWINDOW) TRACE(" WS_EX_APPWINDOW");
    if(exstyle & WS_EX_LAYERED) TRACE(" WS_EX_LAYERED");
1111
    if(exstyle & WS_EX_LAYOUTRTL) TRACE(" WS_EX_LAYOUTRTL");
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130

#define DUMPED_EX_STYLES \
    (WS_EX_DLGMODALFRAME | \
     WS_EX_DRAGDETECT | \
     WS_EX_NOPARENTNOTIFY | \
     WS_EX_TOPMOST | \
     WS_EX_ACCEPTFILES | \
     WS_EX_TRANSPARENT | \
     WS_EX_MDICHILD | \
     WS_EX_TOOLWINDOW | \
     WS_EX_WINDOWEDGE | \
     WS_EX_CLIENTEDGE | \
     WS_EX_CONTEXTHELP | \
     WS_EX_RIGHT | \
     WS_EX_RTLREADING | \
     WS_EX_LEFTSCROLLBAR | \
     WS_EX_CONTROLPARENT | \
     WS_EX_STATICEDGE | \
     WS_EX_APPWINDOW | \
1131 1132
     WS_EX_LAYERED | \
     WS_EX_LAYOUTRTL)
1133

1134 1135
    if(exstyle & ~DUMPED_EX_STYLES) TRACE(" %08lx", exstyle & ~DUMPED_EX_STYLES);
    TRACE("\n");
1136 1137 1138 1139
#undef DUMPED_EX_STYLES
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1140
/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1141 1142 1143
 *           WIN_CreateWindowEx
 *
 * Implementation of CreateWindowEx().
Alexandre Julliard's avatar
Alexandre Julliard committed
1144
 */
1145
HWND WIN_CreateWindowEx( CREATESTRUCTW *cs, LPCWSTR className, HINSTANCE module, BOOL unicode )
Alexandre Julliard's avatar
Alexandre Julliard committed
1146
{
1147
    INT cx, cy, style, sw = SW_SHOW;
1148 1149
    LRESULT result;
    RECT rect;
Alexandre Julliard's avatar
Alexandre Julliard committed
1150
    WND *wndPtr;
1151
    HWND hwnd, parent, owner, top_child = 0;
1152 1153 1154
    MDICREATESTRUCTW mdi_cs;
    CBT_CREATEWNDW cbtc;
    CREATESTRUCTW cbcs;
Alexandre Julliard's avatar
Alexandre Julliard committed
1155

1156
    TRACE("%s %s ex=%08x style=%08x %d,%d %dx%d parent=%p menu=%p inst=%p params=%p\n",
1157
          unicode ? debugstr_w(cs->lpszName) : debugstr_a((LPCSTR)cs->lpszName),
1158
          debugstr_w(className),
Alexandre Julliard's avatar
Alexandre Julliard committed
1159 1160
          cs->dwExStyle, cs->style, cs->x, cs->y, cs->cx, cs->cy,
          cs->hwndParent, cs->hMenu, cs->hInstance, cs->lpCreateParams );
1161 1162
    if(TRACE_ON(win)) dump_window_styles( cs->style, cs->dwExStyle );

1163 1164 1165 1166 1167 1168
    /* Fix the styles for MDI children */
    if (cs->dwExStyle & WS_EX_MDICHILD)
    {
        UINT flags = 0;

        wndPtr = WIN_GetPtr(cs->hwndParent);
1169
        if (wndPtr && wndPtr != WND_OTHER_PROCESS && wndPtr != WND_DESKTOP)
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
        {
            flags = wndPtr->flags;
            WIN_ReleasePtr(wndPtr);
        }

        if (!(flags & WIN_ISMDICLIENT))
        {
            WARN("WS_EX_MDICHILD, but parent %p is not MDIClient\n", cs->hwndParent);
            return 0;
        }

        /* cs->lpCreateParams of WM_[NC]CREATE is different for MDI children.
         * MDICREATESTRUCT members have the originally passed values.
         *
         * Note: we rely on the fact that MDICREATESTRUCTA and MDICREATESTRUCTW
         * have the same layout.
         */
        mdi_cs.szClass = cs->lpszClass;
        mdi_cs.szTitle = cs->lpszName;
        mdi_cs.hOwner = cs->hInstance;
        mdi_cs.x = cs->x;
        mdi_cs.y = cs->y;
        mdi_cs.cx = cs->cx;
        mdi_cs.cy = cs->cy;
        mdi_cs.style = cs->style;
        mdi_cs.lParam = (LPARAM)cs->lpCreateParams;

1197
        cs->lpCreateParams = &mdi_cs;
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215

        if (GetWindowLongW(cs->hwndParent, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
        {
            if (cs->style & WS_POPUP)
            {
                TRACE("WS_POPUP with MDIS_ALLCHILDSTYLES is not allowed\n");
                return 0;
            }
            cs->style |= WS_CHILD | WS_CLIPSIBLINGS;
        }
        else
        {
            cs->style &= ~WS_POPUP;
            cs->style |= WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CAPTION |
                WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
        }

        top_child = GetWindow(cs->hwndParent, GW_CHILD);
1216 1217 1218 1219 1220 1221 1222

        if (top_child)
        {
            /* Restore current maximized child */
            if((cs->style & WS_VISIBLE) && IsZoomed(top_child))
            {
                TRACE("Restoring current maximized child %p\n", top_child);
1223 1224 1225 1226 1227 1228 1229 1230
                if (cs->style & WS_MAXIMIZE)
                {
                    /* if the new window is maximized don't bother repainting */
                    SendMessageW( top_child, WM_SETREDRAW, FALSE, 0 );
                    ShowWindow( top_child, SW_SHOWNORMAL );
                    SendMessageW( top_child, WM_SETREDRAW, TRUE, 0 );
                }
                else ShowWindow( top_child, SW_SHOWNORMAL );
1231 1232
            }
        }
1233 1234
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
1235
    /* Find the parent window */
Alexandre Julliard's avatar
Alexandre Julliard committed
1236

1237
    parent = cs->hwndParent;
1238
    owner = 0;
1239 1240 1241

    if (cs->hwndParent == HWND_MESSAGE)
    {
1242
        cs->hwndParent = parent = get_hwnd_message_parent();
1243 1244
    }
    else if (cs->hwndParent)
Alexandre Julliard's avatar
Alexandre Julliard committed
1245
    {
1246
        if ((cs->style & (WS_CHILD|WS_POPUP)) != WS_CHILD)
Alexandre Julliard's avatar
Alexandre Julliard committed
1247
        {
1248 1249 1250
            parent = GetDesktopWindow();
            owner = cs->hwndParent;
        }
1251 1252 1253 1254 1255 1256
        else
        {
            DWORD parent_style = GetWindowLongW( parent, GWL_EXSTYLE );
            if ((parent_style & WS_EX_LAYOUTRTL) && !(parent_style & WS_EX_NOINHERITLAYOUT))
                cs->dwExStyle |= WS_EX_LAYOUTRTL;
        }
1257
    }
1258
    else
1259
    {
1260 1261
        static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};

1262 1263 1264
        if ((cs->style & (WS_CHILD|WS_POPUP)) == WS_CHILD)
        {
            WARN("No parent for child window\n" );
1265
            SetLastError(ERROR_TLW_WITH_WSCHILD);
1266 1267
            return 0;  /* WS_CHILD needs a parent, but WS_POPUP doesn't */
        }
1268 1269 1270
        /* are we creating the desktop or HWND_MESSAGE parent itself? */
        if (className != (LPCWSTR)DESKTOP_CLASS_ATOM &&
            (IS_INTRESOURCE(className) || strcmpiW( className, messageW )))
1271
        {
1272 1273 1274
            DWORD layout;
            GetProcessDefaultLayout( &layout );
            if (layout & LAYOUT_RTL) cs->dwExStyle |= WS_EX_LAYOUTRTL;
1275
            parent = GetDesktopWindow();
1276
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
1277
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1278

1279
    WIN_FixCoordinates(cs, &sw); /* fix default coordinates */
Alexandre Julliard's avatar
Alexandre Julliard committed
1280

1281 1282 1283 1284 1285 1286 1287
    if ((cs->dwExStyle & WS_EX_DLGMODALFRAME) ||
        ((!(cs->dwExStyle & WS_EX_STATICEDGE)) &&
          (cs->style & (WS_DLGFRAME | WS_THICKFRAME))))
        cs->dwExStyle |= WS_EX_WINDOWEDGE;
    else
        cs->dwExStyle &= ~WS_EX_WINDOWEDGE;

Alexandre Julliard's avatar
Alexandre Julliard committed
1288
    /* Create the window structure */
Alexandre Julliard's avatar
Alexandre Julliard committed
1289

1290
    if (!(wndPtr = create_window_handle( parent, owner, className, module, unicode )))
1291
        return 0;
1292
    hwnd = wndPtr->obj.handle;
Alexandre Julliard's avatar
Alexandre Julliard committed
1293

Alexandre Julliard's avatar
Alexandre Julliard committed
1294
    /* Fill the window structure */
Alexandre Julliard's avatar
Alexandre Julliard committed
1295

1296
    wndPtr->tid            = GetCurrentThreadId();
Alexandre Julliard's avatar
Alexandre Julliard committed
1297
    wndPtr->hInstance      = cs->hInstance;
Alexandre Julliard's avatar
Alexandre Julliard committed
1298
    wndPtr->text           = NULL;
Alexandre Julliard's avatar
Alexandre Julliard committed
1299 1300
    wndPtr->dwStyle        = cs->style & ~WS_VISIBLE;
    wndPtr->dwExStyle      = cs->dwExStyle;
Alexandre Julliard's avatar
Alexandre Julliard committed
1301
    wndPtr->wIDmenu        = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1302
    wndPtr->helpContext    = 0;
1303
    wndPtr->pScroll        = NULL;
Alexandre Julliard's avatar
Alexandre Julliard committed
1304
    wndPtr->userdata       = 0;
1305 1306
    wndPtr->hIcon          = 0;
    wndPtr->hIconSmall     = 0;
1307 1308
    wndPtr->hSysMenu       = 0;

1309 1310 1311
    wndPtr->min_pos.x = wndPtr->min_pos.y = -1;
    wndPtr->max_pos.x = wndPtr->max_pos.y = -1;

1312
    if (wndPtr->dwStyle & WS_SYSMENU) SetSystemMenu( hwnd, 0 );
Alexandre Julliard's avatar
Alexandre Julliard committed
1313

1314 1315 1316 1317 1318 1319
    /*
     * Correct the window styles.
     *
     * It affects only the style loaded into the WIN structure.
     */

1320
    if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
    {
        wndPtr->dwStyle |= WS_CLIPSIBLINGS;
        if (!(wndPtr->dwStyle & WS_POPUP))
            wndPtr->dwStyle |= WS_CAPTION;
    }

    /*
     * WS_EX_WINDOWEDGE appears to be enforced based on the other styles, so
     * why does the user get to set it?
     */

    if ((wndPtr->dwExStyle & WS_EX_DLGMODALFRAME) ||
          (wndPtr->dwStyle & (WS_DLGFRAME | WS_THICKFRAME)))
        wndPtr->dwExStyle |= WS_EX_WINDOWEDGE;
    else
        wndPtr->dwExStyle &= ~WS_EX_WINDOWEDGE;

    if (!(wndPtr->dwStyle & (WS_CHILD | WS_POPUP)))
1339
        wndPtr->flags |= WIN_NEED_SIZE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1340

1341 1342
    SERVER_START_REQ( set_window_info )
    {
1343
        req->handle    = wine_server_user_handle( hwnd );
1344
        req->flags     = SET_WIN_STYLE | SET_WIN_EXSTYLE | SET_WIN_INSTANCE | SET_WIN_UNICODE;
1345 1346
        req->style     = wndPtr->dwStyle;
        req->ex_style  = wndPtr->dwExStyle;
1347
        req->instance  = wine_server_client_ptr( wndPtr->hInstance );
1348
        req->is_unicode = (wndPtr->flags & WIN_ISUNICODE) != 0;
1349
        req->extra_offset = -1;
1350
        wine_server_call( req );
1351 1352
    }
    SERVER_END_REQ;
Alexandre Julliard's avatar
Alexandre Julliard committed
1353

Alexandre Julliard's avatar
Alexandre Julliard committed
1354 1355
    /* Set the window menu */

1356
    if ((wndPtr->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD)
Alexandre Julliard's avatar
Alexandre Julliard committed
1357
    {
1358 1359 1360 1361 1362 1363 1364 1365 1366
        if (cs->hMenu)
        {
            if (!MENU_SetMenu(hwnd, cs->hMenu))
            {
                WIN_ReleasePtr( wndPtr );
                free_window_handle( hwnd );
                return 0;
            }
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
1367 1368
        else
        {
1369
            LPCWSTR menuName = (LPCWSTR)GetClassLongPtrW( hwnd, GCLP_MENUNAME );
Alexandre Julliard's avatar
Alexandre Julliard committed
1370 1371
            if (menuName)
            {
1372
                cs->hMenu = LoadMenuW( cs->hInstance, menuName );
1373
                if (cs->hMenu) MENU_SetMenu( hwnd, cs->hMenu );
Alexandre Julliard's avatar
Alexandre Julliard committed
1374
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
1375
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
1376
    }
1377
    else SetWindowLongPtrW( hwnd, GWLP_ID, (ULONG_PTR)cs->hMenu );
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388

    /* call the WH_CBT hook */

    /* the window style passed to the hook must be the real window style,
     * rather than just the window style that the caller to CreateWindowEx
     * passed in, so we have to copy the original CREATESTRUCT and get the
     * the real style. */
    cbcs = *cs;
    cbcs.style = wndPtr->dwStyle;
    cbtc.lpcs = &cbcs;
    cbtc.hwndInsertAfter = HWND_TOP;
1389
    WIN_ReleasePtr( wndPtr );
1390
    if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode )) goto failed;
Alexandre Julliard's avatar
Alexandre Julliard committed
1391

1392 1393 1394 1395 1396
    /* send the WM_GETMINMAXINFO message and fix the size if needed */

    cx = cs->cx;
    cy = cs->cy;
    if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
Alexandre Julliard's avatar
Alexandre Julliard committed
1397
    {
1398 1399 1400 1401
        POINT maxSize, maxPos, minTrack, maxTrack;
        WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
        if (maxTrack.x < cx) cx = maxTrack.x;
        if (maxTrack.y < cy) cy = maxTrack.y;
1402 1403
        if (minTrack.x > cx) cx = minTrack.x;
        if (minTrack.y > cy) cy = minTrack.y;
1404 1405 1406 1407 1408
    }

    if (cx < 0) cx = 0;
    if (cy < 0) cy = 0;
    SetRect( &rect, cs->x, cs->y, cs->x + cx, cs->y + cy );
1409 1410 1411
    /* check for wraparound */
    if (cs->x + cx < cs->x) rect.right = 0x7fffffff;
    if (cs->y + cy < cs->y) rect.bottom = 0x7fffffff;
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
    if (!set_window_pos( hwnd, 0, SWP_NOZORDER | SWP_NOACTIVATE, &rect, &rect, NULL )) goto failed;

    /* send WM_NCCREATE */

    TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cx, cy );
    if (unicode)
        result = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
    else
        result = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
    if (!result)
    {
        WARN( "%p: aborted by WM_NCCREATE\n", hwnd );
        goto failed;
    }

    /* send WM_NCCALCSIZE */

1429
    if (WIN_GetRectangles( hwnd, COORDS_PARENT, &rect, NULL ))
1430 1431
    {
        /* yes, even if the CBT hook was called with HWND_TOP */
1432 1433
        HWND insert_after = (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
        RECT client_rect = rect;
1434 1435

        /* the rectangle is in screen coords for WM_NCCALCSIZE when wparam is FALSE */
1436
        MapWindowPoints( parent, 0, (POINT *)&client_rect, 2 );
1437
        SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&client_rect );
1438 1439
        MapWindowPoints( 0, parent, (POINT *)&client_rect, 2 );
        set_window_pos( hwnd, insert_after, SWP_NOACTIVATE, &rect, &client_rect, NULL );
1440
    }
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
    else return 0;

    /* send WM_CREATE */

    if (unicode)
        result = SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs );
    else
        result = SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs );
    if (result == -1) goto failed;

1451 1452 1453 1454
    /* call the driver */

    if (!USER_Driver->pCreateWindow( hwnd )) goto failed;

1455 1456 1457 1458
    NotifyWinEvent(EVENT_OBJECT_CREATE, hwnd, OBJID_WINDOW, 0);

    /* send the size messages */

1459 1460
    if (!(wndPtr = WIN_GetPtr( hwnd )) ||
          wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return 0;
1461 1462 1463
    if (!(wndPtr->flags & WIN_NEED_SIZE))
    {
        WIN_ReleasePtr( wndPtr );
1464
        WIN_GetRectangles( hwnd, COORDS_PARENT, NULL, &rect );
1465 1466 1467 1468 1469 1470
        SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
                      MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
        SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( rect.left, rect.top ) );
    }
    else WIN_ReleasePtr( wndPtr );

1471
    /* Show the window, maximizing or minimizing if needed */
1472

1473 1474 1475 1476 1477 1478 1479 1480 1481
    style = WIN_SetStyle( hwnd, 0, WS_MAXIMIZE | WS_MINIMIZE );
    if (style & (WS_MINIMIZE | WS_MAXIMIZE))
    {
        RECT newPos;
        UINT swFlag = (style & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;

        swFlag = WINPOS_MinMaximize( hwnd, swFlag, &newPos );
        swFlag |= SWP_FRAMECHANGED; /* Frame always gets changed */
        if (!(style & WS_VISIBLE) || (style & WS_CHILD) || GetActiveWindow()) swFlag |= SWP_NOACTIVATE;
1482 1483
        SetWindowPos( hwnd, 0, newPos.left, newPos.top, newPos.right - newPos.left,
                      newPos.bottom - newPos.top, swFlag );
1484
    }
1485

1486
    /* Notify the parent window only */
1487

1488
    send_parent_notify( hwnd, WM_CREATE );
1489
    if (!IsWindow( hwnd )) return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1490

1491
    if (cs->style & WS_VISIBLE)
1492
    {
1493
        if (cs->style & WS_MAXIMIZE)
1494
            sw = SW_SHOW;
1495 1496 1497 1498 1499
        else if (cs->style & WS_MINIMIZE)
            sw = SW_SHOWMINIMIZED;

        ShowWindow( hwnd, sw );
        if (cs->dwExStyle & WS_EX_MDICHILD)
1500
        {
1501 1502 1503
            SendMessageW(cs->hwndParent, WM_MDIREFRESHMENU, 0, 0);
            /* ShowWindow won't activate child windows */
            SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE );
1504 1505 1506
        }
    }

1507
    /* Call WH_SHELL hook */
1508

1509
    if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) && !GetWindow( hwnd, GW_OWNER ))
1510
        HOOK_CallHooks( WH_SHELL, HSHELL_WINDOWCREATED, (WPARAM)hwnd, 0, TRUE );
1511

1512
    TRACE("created window %p\n", hwnd);
1513
    return hwnd;
1514 1515 1516 1517

failed:
    WIN_DestroyWindow( hwnd );
    return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1518 1519 1520
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1521
/***********************************************************************
1522
 *		CreateWindowExA (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1523
 */
1524 1525 1526 1527 1528
HWND WINAPI CreateWindowExA( DWORD exStyle, LPCSTR className,
                                 LPCSTR windowName, DWORD style, INT x,
                                 INT y, INT width, INT height,
                                 HWND parent, HMENU menu,
                                 HINSTANCE instance, LPVOID data )
Alexandre Julliard's avatar
Alexandre Julliard committed
1529
{
1530
    CREATESTRUCTA cs;
Alexandre Julliard's avatar
Alexandre Julliard committed
1531 1532

    cs.lpCreateParams = data;
Alexandre Julliard's avatar
Alexandre Julliard committed
1533 1534
    cs.hInstance      = instance;
    cs.hMenu          = menu;
Alexandre Julliard's avatar
Alexandre Julliard committed
1535
    cs.hwndParent     = parent;
Alexandre Julliard's avatar
Alexandre Julliard committed
1536 1537 1538 1539 1540
    cs.x              = x;
    cs.y              = y;
    cs.cx             = width;
    cs.cy             = height;
    cs.style          = style;
Alexandre Julliard's avatar
Alexandre Julliard committed
1541 1542
    cs.lpszName       = windowName;
    cs.lpszClass      = className;
Alexandre Julliard's avatar
Alexandre Julliard committed
1543
    cs.dwExStyle      = exStyle;
1544

1545 1546 1547 1548 1549
    if (!IS_INTRESOURCE(className))
    {
        WCHAR bufferW[256];
        if (!MultiByteToWideChar( CP_ACP, 0, className, -1, bufferW, sizeof(bufferW)/sizeof(WCHAR) ))
            return 0;
1550
        return wow_handlers.create_window( (CREATESTRUCTW *)&cs, bufferW, instance, FALSE );
1551
    }
1552 1553
    /* Note: we rely on the fact that CREATESTRUCTA and */
    /* CREATESTRUCTW have the same layout. */
1554
    return wow_handlers.create_window( (CREATESTRUCTW *)&cs, (LPCWSTR)className, instance, FALSE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1555
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1556 1557


Alexandre Julliard's avatar
Alexandre Julliard committed
1558
/***********************************************************************
1559
 *		CreateWindowExW (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1560
 */
1561 1562 1563 1564 1565
HWND WINAPI CreateWindowExW( DWORD exStyle, LPCWSTR className,
                                 LPCWSTR windowName, DWORD style, INT x,
                                 INT y, INT width, INT height,
                                 HWND parent, HMENU menu,
                                 HINSTANCE instance, LPVOID data )
Alexandre Julliard's avatar
Alexandre Julliard committed
1566
{
1567
    CREATESTRUCTW cs;
Alexandre Julliard's avatar
Alexandre Julliard committed
1568 1569

    cs.lpCreateParams = data;
Alexandre Julliard's avatar
Alexandre Julliard committed
1570 1571
    cs.hInstance      = instance;
    cs.hMenu          = menu;
Alexandre Julliard's avatar
Alexandre Julliard committed
1572
    cs.hwndParent     = parent;
Alexandre Julliard's avatar
Alexandre Julliard committed
1573 1574 1575 1576 1577
    cs.x              = x;
    cs.y              = y;
    cs.cx             = width;
    cs.cy             = height;
    cs.style          = style;
Alexandre Julliard's avatar
Alexandre Julliard committed
1578 1579
    cs.lpszName       = windowName;
    cs.lpszClass      = className;
Alexandre Julliard's avatar
Alexandre Julliard committed
1580
    cs.dwExStyle      = exStyle;
1581

1582
    return wow_handlers.create_window( &cs, className, instance, TRUE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1583 1584
}

1585

Alexandre Julliard's avatar
Alexandre Julliard committed
1586 1587 1588
/***********************************************************************
 *           WIN_SendDestroyMsg
 */
1589
static void WIN_SendDestroyMsg( HWND hwnd )
Alexandre Julliard's avatar
Alexandre Julliard committed
1590
{
1591 1592
    GUITHREADINFO info;

1593
    info.cbSize = sizeof(info);
1594 1595 1596
    if (GetGUIThreadInfo( GetCurrentThreadId(), &info ))
    {
        if (hwnd == info.hwndCaret) DestroyCaret();
1597
        if (hwnd == info.hwndActive) WINPOS_ActivateOtherWindow( hwnd );
1598
    }
1599 1600 1601 1602

    /*
     * Send the WM_DESTROY to the window.
     */
1603
    SendMessageW( hwnd, WM_DESTROY, 0, 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
1604

1605 1606 1607 1608
    /*
     * This WM_DESTROY message can trigger re-entrant calls to DestroyWindow
     * make sure that the window still exists when we come back.
     */
1609 1610 1611 1612
    if (IsWindow(hwnd))
    {
        HWND* pWndArray;
        int i;
1613

1614
        if (!(pWndArray = WIN_ListChildren( hwnd ))) return;
1615

1616
        for (i = 0; pWndArray[i]; i++)
1617 1618 1619
        {
            if (IsWindow( pWndArray[i] )) WIN_SendDestroyMsg( pWndArray[i] );
        }
1620
        HeapFree( GetProcessHeap(), 0, pWndArray );
Alexandre Julliard's avatar
Alexandre Julliard committed
1621 1622
    }
    else
1623
      WARN("\tdestroyed itself while in WM_DESTROY!\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
1624 1625 1626
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1627
/***********************************************************************
1628
 *		DestroyWindow (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1629
 */
1630
BOOL WINAPI DestroyWindow( HWND hwnd )
Alexandre Julliard's avatar
Alexandre Julliard committed
1631
{
1632
    BOOL is_child;
Alexandre Julliard's avatar
Alexandre Julliard committed
1633

1634
    if (!(hwnd = WIN_IsCurrentThread( hwnd )) || is_desktop_window( hwnd ))
1635 1636 1637 1638
    {
        SetLastError( ERROR_ACCESS_DENIED );
        return FALSE;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1639

1640
    TRACE("(%p)\n", hwnd);
Alexandre Julliard's avatar
Alexandre Julliard committed
1641

Alexandre Julliard's avatar
Alexandre Julliard committed
1642 1643
      /* Call hooks */

1644
    if (HOOK_CallHooks( WH_CBT, HCBT_DESTROYWND, (WPARAM)hwnd, 0, TRUE )) return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1645

1646 1647 1648
    if (MENU_IsMenuActive() == hwnd)
        EndMenu();

1649 1650 1651 1652 1653 1654 1655 1656
    is_child = (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) != 0;

    if (is_child)
    {
        if (!USER_IsExitingThread( GetCurrentThreadId() ))
            send_parent_notify( hwnd, WM_DESTROY );
    }
    else if (!GetWindow( hwnd, GW_OWNER ))
Alexandre Julliard's avatar
Alexandre Julliard committed
1657
    {
1658
        HOOK_CallHooks( WH_SHELL, HSHELL_WINDOWDESTROYED, (WPARAM)hwnd, 0L, TRUE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1659 1660 1661
        /* FIXME: clean up palette - see "Internals" p.352 */
    }

1662
    if (!IsWindow(hwnd)) return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1663

Alexandre Julliard's avatar
Alexandre Julliard committed
1664
      /* Hide the window */
1665 1666 1667 1668 1669 1670 1671 1672 1673
    if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)
    {
        /* Only child windows receive WM_SHOWWINDOW in DestroyWindow() */
        if (is_child)
            ShowWindow( hwnd, SW_HIDE );
        else
            SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE |
                          SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW );
    }
1674

1675
    if (!IsWindow(hwnd)) return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1676

Alexandre Julliard's avatar
Alexandre Julliard committed
1677 1678
      /* Recursively destroy owned windows */

1679
    if (!is_child)
Alexandre Julliard's avatar
Alexandre Julliard committed
1680
    {
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
        for (;;)
        {
            int i, got_one = 0;
            HWND *list = WIN_ListChildren( GetDesktopWindow() );
            if (list)
            {
                for (i = 0; list[i]; i++)
                {
                    if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
                    if (WIN_IsCurrentThread( list[i] ))
                    {
                        DestroyWindow( list[i] );
                        got_one = 1;
                        continue;
                    }
                    WIN_SetOwner( list[i], 0 );
                }
                HeapFree( GetProcessHeap(), 0, list );
            }
            if (!got_one) break;
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
1702 1703
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
1704
      /* Send destroy messages */
Alexandre Julliard's avatar
Alexandre Julliard committed
1705

1706
    WIN_SendDestroyMsg( hwnd );
1707
    if (!IsWindow( hwnd )) return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1708

1709 1710 1711
    if (GetClipboardOwner() == hwnd)
        CLIPBOARD_ReleaseOwner();

Alexandre Julliard's avatar
Alexandre Julliard committed
1712 1713
      /* Destroy the window storage */

1714
    WIN_DestroyWindow( hwnd );
1715
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1716 1717 1718
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1719
/***********************************************************************
1720
 *		CloseWindow (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1721
 */
1722
BOOL WINAPI CloseWindow( HWND hwnd )
Alexandre Julliard's avatar
Alexandre Julliard committed
1723
{
1724
    if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD) return FALSE;
1725
    ShowWindow( hwnd, SW_MINIMIZE );
1726
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1727 1728
}

1729

Alexandre Julliard's avatar
Alexandre Julliard committed
1730
/***********************************************************************
1731
 *		OpenIcon (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1732
 */
1733
BOOL WINAPI OpenIcon( HWND hwnd )
Alexandre Julliard's avatar
Alexandre Julliard committed
1734
{
1735 1736
    if (!IsIconic( hwnd )) return FALSE;
    ShowWindow( hwnd, SW_SHOWNORMAL );
Alexandre Julliard's avatar
Alexandre Julliard committed
1737
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1738 1739
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1740

Alexandre Julliard's avatar
Alexandre Julliard committed
1741
/***********************************************************************
1742
 *		FindWindowExW (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1743
 */
1744
HWND WINAPI FindWindowExW( HWND parent, HWND child, LPCWSTR className, LPCWSTR title )
Alexandre Julliard's avatar
Alexandre Julliard committed
1745
{
1746 1747
    HWND *list = NULL;
    HWND retvalue = 0;
1748 1749
    int i = 0, len = 0;
    WCHAR *buffer = NULL;
Alexandre Julliard's avatar
Alexandre Julliard committed
1750

1751 1752 1753
    if (!parent && child) parent = GetDesktopWindow();
    else if (parent == HWND_MESSAGE) parent = get_hwnd_message_parent();

1754
    if (title)
Alexandre Julliard's avatar
Alexandre Julliard committed
1755
    {
1756 1757
        len = strlenW(title) + 1;  /* one extra char to check for chars beyond the end */
        if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1758
    }
1759

1760
    if (!(list = list_window_children( 0, parent, className, 0 ))) goto done;
1761 1762

    if (child)
1763
    {
1764
        child = WIN_GetFullHandle( child );
1765
        while (list[i] && list[i] != child) i++;
1766
        if (!list[i]) goto done;
1767
        i++;  /* start from next window */
Alexandre Julliard's avatar
Alexandre Julliard committed
1768 1769
    }

1770
    if (title)
Alexandre Julliard's avatar
Alexandre Julliard committed
1771
    {
1772 1773
        while (list[i])
        {
1774 1775 1776 1777 1778 1779 1780 1781
            if (GetWindowTextW( list[i], buffer, len + 1 ))
            {
                if (!strcmpiW( buffer, title )) break;
            }
            else
            {
                if (!title[0]) break;
            }
1782 1783
            i++;
        }
1784
    }
1785 1786
    retvalue = list[i];

1787
 done:
1788 1789
    HeapFree( GetProcessHeap(), 0, list );
    HeapFree( GetProcessHeap(), 0, buffer );
1790
    return retvalue;
Alexandre Julliard's avatar
Alexandre Julliard committed
1791
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1792 1793 1794 1795



/***********************************************************************
1796
 *		FindWindowA (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1797
 */
1798
HWND WINAPI FindWindowA( LPCSTR className, LPCSTR title )
Alexandre Julliard's avatar
Alexandre Julliard committed
1799
{
1800
    HWND ret = FindWindowExA( 0, 0, className, title );
Alexandre Julliard's avatar
Alexandre Julliard committed
1801 1802
    if (!ret) SetLastError (ERROR_CANNOT_FIND_WND_CLASS);
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
1803 1804 1805 1806
}


/***********************************************************************
1807
 *		FindWindowExA (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1808
 */
1809
HWND WINAPI FindWindowExA( HWND parent, HWND child, LPCSTR className, LPCSTR title )
Alexandre Julliard's avatar
Alexandre Julliard committed
1810
{
1811 1812
    LPWSTR titleW = NULL;
    HWND hwnd = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1813

1814
    if (title)
Alexandre Julliard's avatar
Alexandre Julliard committed
1815
    {
1816 1817 1818
        DWORD len = MultiByteToWideChar( CP_ACP, 0, title, -1, NULL, 0 );
        if (!(titleW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return 0;
        MultiByteToWideChar( CP_ACP, 0, title, -1, titleW, len );
Alexandre Julliard's avatar
Alexandre Julliard committed
1819
    }
1820

1821
    if (!IS_INTRESOURCE(className))
Alexandre Julliard's avatar
Alexandre Julliard committed
1822
    {
1823 1824 1825 1826 1827 1828 1829
        WCHAR classW[256];
        if (MultiByteToWideChar( CP_ACP, 0, className, -1, classW, sizeof(classW)/sizeof(WCHAR) ))
            hwnd = FindWindowExW( parent, child, classW, titleW );
    }
    else
    {
        hwnd = FindWindowExW( parent, child, (LPCWSTR)className, titleW );
Alexandre Julliard's avatar
Alexandre Julliard committed
1830
    }
1831 1832 1833

    HeapFree( GetProcessHeap(), 0, titleW );
    return hwnd;
Alexandre Julliard's avatar
Alexandre Julliard committed
1834 1835 1836 1837
}


/***********************************************************************
1838
 *		FindWindowW (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1839
 */
1840
HWND WINAPI FindWindowW( LPCWSTR className, LPCWSTR title )
Alexandre Julliard's avatar
Alexandre Julliard committed
1841
{
1842
    return FindWindowExW( 0, 0, className, title );
Alexandre Julliard's avatar
Alexandre Julliard committed
1843 1844 1845
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1846
/**********************************************************************
1847
 *		GetDesktopWindow (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1848
 */
1849
HWND WINAPI GetDesktopWindow(void)
Alexandre Julliard's avatar
Alexandre Julliard committed
1850
{
1851 1852
    struct user_thread_info *thread_info = get_user_thread_info();

1853
    if (thread_info->top_window) return thread_info->top_window;
1854 1855 1856 1857

    SERVER_START_REQ( get_desktop_window )
    {
        req->force = 0;
1858 1859
        if (!wine_server_call( req ))
        {
1860 1861
            thread_info->top_window = wine_server_ptr_handle( reply->top_window );
            thread_info->msg_window = wine_server_ptr_handle( reply->msg_window );
1862
        }
1863 1864 1865
    }
    SERVER_END_REQ;

1866
    if (!thread_info->top_window)
1867
    {
1868 1869 1870
        USEROBJECTFLAGS flags;
        if (!GetUserObjectInformationW( GetProcessWindowStation(), UOI_FLAGS, &flags,
                                        sizeof(flags), NULL ) || (flags.dwFlags & WSF_VISIBLE))
1871
        {
1872 1873
            static const WCHAR explorer[] = {'\\','e','x','p','l','o','r','e','r','.','e','x','e',0};
            static const WCHAR args[] = {' ','/','d','e','s','k','t','o','p',0};
1874 1875
            STARTUPINFOW si;
            PROCESS_INFORMATION pi;
1876 1877 1878
            WCHAR windir[MAX_PATH];
            WCHAR app[MAX_PATH + sizeof(explorer)/sizeof(WCHAR)];
            WCHAR cmdline[MAX_PATH + (sizeof(explorer) + sizeof(args))/sizeof(WCHAR)];
1879
            void *redir;
1880 1881 1882 1883 1884 1885 1886 1887

            memset( &si, 0, sizeof(si) );
            si.cb = sizeof(si);
            si.dwFlags = STARTF_USESTDHANDLES;
            si.hStdInput  = 0;
            si.hStdOutput = 0;
            si.hStdError  = GetStdHandle( STD_ERROR_HANDLE );

1888
            GetSystemDirectoryW( windir, MAX_PATH );
1889 1890 1891 1892
            strcpyW( app, windir );
            strcatW( app, explorer );
            strcpyW( cmdline, app );
            strcatW( cmdline, args );
1893 1894

            Wow64DisableWow64FsRedirection( &redir );
1895 1896
            if (CreateProcessW( app, cmdline, NULL, NULL, FALSE, DETACHED_PROCESS,
                                NULL, windir, &si, &pi ))
1897 1898 1899 1900 1901 1902 1903
            {
                TRACE( "started explorer pid %04x tid %04x\n", pi.dwProcessId, pi.dwThreadId );
                WaitForInputIdle( pi.hProcess, 10000 );
                CloseHandle( pi.hThread );
                CloseHandle( pi.hProcess );
            }
            else WARN( "failed to start explorer, err %d\n", GetLastError() );
1904
            Wow64RevertWow64FsRedirection( redir );
1905
        }
1906
        else TRACE( "not starting explorer since winstation is not visible\n" );
1907

1908 1909
        SERVER_START_REQ( get_desktop_window )
        {
1910
            req->force = 1;
1911 1912
            if (!wine_server_call( req ))
            {
1913 1914
                thread_info->top_window = wine_server_ptr_handle( reply->top_window );
                thread_info->msg_window = wine_server_ptr_handle( reply->msg_window );
1915
            }
1916 1917 1918
        }
        SERVER_END_REQ;
    }
1919

1920
    if (!thread_info->top_window || !USER_Driver->pCreateDesktopWindow( thread_info->top_window ))
1921 1922
        ERR( "failed to create desktop window\n" );

1923
    return thread_info->top_window;
Alexandre Julliard's avatar
Alexandre Julliard committed
1924 1925 1926
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1927
/*******************************************************************
1928
 *		EnableWindow (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1929
 */
1930
BOOL WINAPI EnableWindow( HWND hwnd, BOOL enable )
Alexandre Julliard's avatar
Alexandre Julliard committed
1931
{
1932
    BOOL retvalue;
1933
    HWND full_handle;
Alexandre Julliard's avatar
Alexandre Julliard committed
1934

1935 1936 1937 1938 1939 1940
    if (is_broadcast(hwnd))
    {
        SetLastError( ERROR_INVALID_PARAMETER );
        return FALSE;
    }

1941 1942
    if (!(full_handle = WIN_IsCurrentThread( hwnd )))
        return SendMessageW( hwnd, WM_WINE_ENABLEWINDOW, enable, 0 );
1943

1944
    hwnd = full_handle;
1945

1946
    TRACE("( %p, %d )\n", hwnd, enable);
1947

1948
    retvalue = !IsWindowEnabled( hwnd );
1949

1950
    if (enable && retvalue)
1951
    {
1952
        WIN_SetStyle( hwnd, 0, WS_DISABLED );
1953
        SendMessageW( hwnd, WM_ENABLE, TRUE, 0 );
Alexandre Julliard's avatar
Alexandre Julliard committed
1954
    }
1955
    else if (!enable && !retvalue)
Alexandre Julliard's avatar
Alexandre Julliard committed
1956
    {
1957
        HWND capture_wnd;
1958

1959
        SendMessageW( hwnd, WM_CANCELMODE, 0, 0);
1960

1961
        WIN_SetStyle( hwnd, WS_DISABLED, 0 );
1962

1963
        if (hwnd == GetFocus())
1964
            SetFocus( 0 );  /* A disabled window can't have the focus */
1965

1966 1967
        capture_wnd = GetCapture();
        if (hwnd == capture_wnd || IsChild(hwnd, capture_wnd))
1968 1969
            ReleaseCapture();  /* A disabled window can't capture the mouse */

1970
        SendMessageW( hwnd, WM_ENABLE, FALSE, 0 );
Alexandre Julliard's avatar
Alexandre Julliard committed
1971
    }
1972
    return retvalue;
Alexandre Julliard's avatar
Alexandre Julliard committed
1973 1974 1975
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1976
/***********************************************************************
1977
 *		IsWindowEnabled (USER32.@)
1978
 */
1979
BOOL WINAPI IsWindowEnabled(HWND hWnd)
Alexandre Julliard's avatar
Alexandre Julliard committed
1980
{
1981
    return !(GetWindowLongW( hWnd, GWL_STYLE ) & WS_DISABLED);
Alexandre Julliard's avatar
Alexandre Julliard committed
1982 1983
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1984

Alexandre Julliard's avatar
Alexandre Julliard committed
1985
/***********************************************************************
1986
 *		IsWindowUnicode (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1987
 */
1988
BOOL WINAPI IsWindowUnicode( HWND hwnd )
Alexandre Julliard's avatar
Alexandre Julliard committed
1989
{
1990
    WND * wndPtr;
1991 1992 1993
    BOOL retvalue = FALSE;

    if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1994

1995
    if (wndPtr == WND_DESKTOP) return TRUE;
1996 1997 1998

    if (wndPtr != WND_OTHER_PROCESS)
    {
1999
        retvalue = (wndPtr->flags & WIN_ISUNICODE) != 0;
2000 2001 2002 2003 2004 2005
        WIN_ReleasePtr( wndPtr );
    }
    else
    {
        SERVER_START_REQ( get_window_info )
        {
2006
            req->handle = wine_server_user_handle( hwnd );
2007 2008 2009 2010
            if (!wine_server_call_err( req )) retvalue = reply->is_unicode;
        }
        SERVER_END_REQ;
    }
2011
    return retvalue;
Alexandre Julliard's avatar
Alexandre Julliard committed
2012 2013 2014
}


Alexandre Julliard's avatar
Alexandre Julliard committed
2015
/**********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
2016 2017 2018
 *	     WIN_GetWindowLong
 *
 * Helper function for GetWindowLong().
Alexandre Julliard's avatar
Alexandre Julliard committed
2019
 */
2020
static LONG_PTR WIN_GetWindowLong( HWND hwnd, INT offset, UINT size, BOOL unicode )
Alexandre Julliard's avatar
Alexandre Julliard committed
2021
{
2022
    LONG_PTR retvalue = 0;
2023 2024
    WND *wndPtr;

2025
    if (offset == GWLP_HWNDPARENT)
2026 2027 2028
    {
        HWND parent = GetAncestor( hwnd, GA_PARENT );
        if (parent == GetDesktopWindow()) parent = GetWindow( hwnd, GW_OWNER );
2029
        return (ULONG_PTR)parent;
2030
    }
2031 2032 2033 2034 2035 2036 2037

    if (!(wndPtr = WIN_GetPtr( hwnd )))
    {
        SetLastError( ERROR_INVALID_WINDOW_HANDLE );
        return 0;
    }

2038
    if (wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP)
2039
    {
2040
        if (offset == GWLP_WNDPROC)
2041 2042 2043 2044 2045 2046
        {
            SetLastError( ERROR_ACCESS_DENIED );
            return 0;
        }
        SERVER_START_REQ( set_window_info )
        {
2047
            req->handle = wine_server_user_handle( hwnd );
2048
            req->flags  = 0;  /* don't set anything, just retrieve */
2049
            req->extra_offset = (offset >= 0) ? offset : -1;
2050
            req->extra_size = (offset >= 0) ? size : 0;
2051
            if (!wine_server_call_err( req ))
2052 2053 2054
            {
                switch(offset)
                {
2055 2056 2057
                case GWL_STYLE:      retvalue = reply->old_style; break;
                case GWL_EXSTYLE:    retvalue = reply->old_ex_style; break;
                case GWLP_ID:        retvalue = reply->old_id; break;
2058
                case GWLP_HINSTANCE: retvalue = (ULONG_PTR)wine_server_get_ptr( reply->old_instance ); break;
2059
                case GWLP_USERDATA:  retvalue = reply->old_user_data; break;
2060
                default:
2061
                    if (offset >= 0) retvalue = get_win_data( &reply->old_extra_value, size );
2062
                    else SetLastError( ERROR_INVALID_INDEX );
2063 2064 2065 2066 2067 2068 2069 2070 2071 2072
                    break;
                }
            }
        }
        SERVER_END_REQ;
        return retvalue;
    }

    /* now we have a valid wndPtr */

Alexandre Julliard's avatar
Alexandre Julliard committed
2073 2074
    if (offset >= 0)
    {
2075
        if (offset > (int)(wndPtr->cbWndExtra - size))
Alexandre Julliard's avatar
Alexandre Julliard committed
2076
        {
2077
            WARN("Invalid offset %d\n", offset );
2078 2079 2080
            WIN_ReleasePtr( wndPtr );
            SetLastError( ERROR_INVALID_INDEX );
            return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
2081
        }
2082 2083
        retvalue = get_win_data( (char *)wndPtr->wExtra + offset, size );

Alexandre Julliard's avatar
Alexandre Julliard committed
2084
        /* Special case for dialog window procedure */
2085
        if ((offset == DWLP_DLGPROC) && (size == sizeof(LONG_PTR)) && wndPtr->dlgInfo)
2086
            retvalue = (LONG_PTR)WINPROC_GetProc( (WNDPROC)retvalue, unicode );
2087 2088
        WIN_ReleasePtr( wndPtr );
        return retvalue;
Alexandre Julliard's avatar
Alexandre Julliard committed
2089
    }
2090

Alexandre Julliard's avatar
Alexandre Julliard committed
2091 2092
    switch(offset)
    {
2093
    case GWLP_USERDATA:  retvalue = wndPtr->userdata; break;
2094 2095
    case GWL_STYLE:      retvalue = wndPtr->dwStyle; break;
    case GWL_EXSTYLE:    retvalue = wndPtr->dwExStyle; break;
2096
    case GWLP_ID:        retvalue = wndPtr->wIDmenu; break;
2097
    case GWLP_HINSTANCE: retvalue = (ULONG_PTR)wndPtr->hInstance; break;
2098 2099 2100 2101 2102
    case GWLP_WNDPROC:
        /* This looks like a hack only for the edit control (see tests). This makes these controls
         * more tolerant to A/W mismatches. The lack of W->A->W conversion for such a mismatch suggests
         * that the hack is in GetWindowLongPtr[AW], not in winprocs.
         */
2103
        if (wndPtr->winproc == BUILTIN_WINPROC(WINPROC_EDIT) && (!unicode != !(wndPtr->flags & WIN_ISUNICODE)))
2104 2105 2106 2107
            retvalue = (ULONG_PTR)wndPtr->winproc;
        else
            retvalue = (ULONG_PTR)WINPROC_GetProc( wndPtr->winproc, unicode );
        break;
2108 2109 2110 2111
    default:
        WARN("Unknown offset %d\n", offset );
        SetLastError( ERROR_INVALID_INDEX );
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
2112
    }
2113
    WIN_ReleasePtr(wndPtr);
2114
    return retvalue;
Alexandre Julliard's avatar
Alexandre Julliard committed
2115 2116 2117
}


Alexandre Julliard's avatar
Alexandre Julliard committed
2118
/**********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
2119 2120 2121
 *	     WIN_SetWindowLong
 *
 * Helper function for SetWindowLong().
Alexandre Julliard's avatar
Alexandre Julliard committed
2122 2123 2124
 *
 * 0 is the failure code. However, in the case of failure SetLastError
 * must be set to distinguish between a 0 return value and a failure.
Alexandre Julliard's avatar
Alexandre Julliard committed
2125
 */
2126
LONG_PTR WIN_SetWindowLong( HWND hwnd, INT offset, UINT size, LONG_PTR newval, BOOL unicode )
Alexandre Julliard's avatar
Alexandre Julliard committed
2127
{
2128 2129
    STYLESTRUCT style;
    BOOL ok;
2130
    LONG_PTR retval = 0;
2131
    WND *wndPtr;
2132

2133
    TRACE( "%p %d %lx %c\n", hwnd, offset, newval, unicode ? 'W' : 'A' );
Alexandre Julliard's avatar
Alexandre Julliard committed
2134

2135 2136 2137 2138 2139
    if (is_broadcast(hwnd))
    {
        SetLastError( ERROR_INVALID_PARAMETER );
        return FALSE;
    }
2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152

    if (!(wndPtr = WIN_GetPtr( hwnd )))
    {
        SetLastError( ERROR_INVALID_WINDOW_HANDLE );
        return 0;
    }
    if (wndPtr == WND_DESKTOP)
    {
        /* can't change anything on the desktop window */
        SetLastError( ERROR_ACCESS_DENIED );
        return 0;
    }
    if (wndPtr == WND_OTHER_PROCESS)
Alexandre Julliard's avatar
Alexandre Julliard committed
2153
    {
2154
        if (offset == GWLP_WNDPROC)
2155 2156 2157 2158
        {
            SetLastError( ERROR_ACCESS_DENIED );
            return 0;
        }
2159 2160 2161 2162 2163 2164
        if (offset > 32767 || offset < -32767)
        {
            SetLastError( ERROR_INVALID_INDEX );
            return 0;
        }
        return SendMessageW( hwnd, WM_WINE_SETWINDOWLONG, MAKEWPARAM( offset, size ), newval );
Alexandre Julliard's avatar
Alexandre Julliard committed
2165 2166
    }

2167 2168
    /* first some special cases */
    switch( offset )
Alexandre Julliard's avatar
Alexandre Julliard committed
2169
    {
2170
    case GWL_STYLE:
2171
        style.styleOld = wndPtr->dwStyle;
2172 2173
        style.styleNew = newval;
        WIN_ReleasePtr( wndPtr );
2174
        SendMessageW( hwnd, WM_STYLECHANGING, GWL_STYLE, (LPARAM)&style );
2175 2176
        if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
        newval = style.styleNew;
2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192
        /* WS_CLIPSIBLINGS can't be reset on top-level windows */
        if (wndPtr->parent == GetDesktopWindow()) newval |= WS_CLIPSIBLINGS;
        break;
    case GWL_EXSTYLE:
        style.styleOld = wndPtr->dwExStyle;
        style.styleNew = newval;
        WIN_ReleasePtr( wndPtr );
        SendMessageW( hwnd, WM_STYLECHANGING, GWL_EXSTYLE, (LPARAM)&style );
        if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
        /* WS_EX_TOPMOST can only be changed through SetWindowPos */
        newval = (style.styleNew & ~WS_EX_TOPMOST) | (wndPtr->dwExStyle & WS_EX_TOPMOST);
        /* WS_EX_WINDOWEDGE depends on some other styles */
        if ((newval & WS_EX_DLGMODALFRAME) || (wndPtr->dwStyle & WS_THICKFRAME))
            newval |= WS_EX_WINDOWEDGE;
        else if (wndPtr->dwStyle & (WS_CHILD|WS_POPUP))
            newval &= ~WS_EX_WINDOWEDGE;
2193
        break;
2194
    case GWLP_HWNDPARENT:
2195 2196 2197
        if (wndPtr->parent == GetDesktopWindow())
        {
            WIN_ReleasePtr( wndPtr );
2198
            return (ULONG_PTR)WIN_SetOwner( hwnd, (HWND)newval );
2199 2200 2201 2202
        }
        else
        {
            WIN_ReleasePtr( wndPtr );
2203
            return (ULONG_PTR)SetParent( hwnd, (HWND)newval );
2204
        }
2205
    case GWLP_WNDPROC:
2206
    {
2207
        WNDPROC proc;
2208
        UINT old_flags = wndPtr->flags;
2209
        retval = WIN_GetWindowLong( hwnd, offset, size, unicode );
2210
        proc = WINPROC_AllocProc( (WNDPROC)newval, unicode );
2211 2212
        if (proc) wndPtr->winproc = proc;
        if (WINPROC_IsUnicode( proc, unicode )) wndPtr->flags |= WIN_ISUNICODE;
2213 2214
        else wndPtr->flags &= ~WIN_ISUNICODE;
        if (!((old_flags ^ wndPtr->flags) & WIN_ISUNICODE))
2215 2216 2217 2218 2219 2220 2221
        {
            WIN_ReleasePtr( wndPtr );
            return retval;
        }
        /* update is_unicode flag on the server side */
        break;
    }
2222 2223 2224
    case GWLP_ID:
    case GWLP_HINSTANCE:
    case GWLP_USERDATA:
2225
        break;
2226
    case DWLP_DLGPROC:
2227
        if ((wndPtr->cbWndExtra - sizeof(LONG_PTR) >= DWLP_DLGPROC) &&
2228
            (size == sizeof(LONG_PTR)) && wndPtr->dlgInfo)
2229
        {
2230
            WNDPROC *ptr = (WNDPROC *)((char *)wndPtr->wExtra + DWLP_DLGPROC);
2231
            retval = (ULONG_PTR)WINPROC_GetProc( *ptr, unicode );
2232
            *ptr = WINPROC_AllocProc( (WNDPROC)newval, unicode );
2233 2234 2235 2236 2237
            WIN_ReleasePtr( wndPtr );
            return retval;
        }
        /* fall through */
    default:
2238
        if (offset < 0 || offset > (int)(wndPtr->cbWndExtra - size))
Alexandre Julliard's avatar
Alexandre Julliard committed
2239
        {
2240
            WARN("Invalid offset %d\n", offset );
2241 2242 2243
            WIN_ReleasePtr( wndPtr );
            SetLastError( ERROR_INVALID_INDEX );
            return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
2244
        }
2245
        else if (get_win_data( (char *)wndPtr->wExtra + offset, size ) == newval)
Alexandre Julliard's avatar
Alexandre Julliard committed
2246
        {
2247 2248 2249
            /* already set to the same value */
            WIN_ReleasePtr( wndPtr );
            return newval;
Alexandre Julliard's avatar
Alexandre Julliard committed
2250
        }
2251
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
2252
    }
2253

2254 2255
    SERVER_START_REQ( set_window_info )
    {
2256
        req->handle = wine_server_user_handle( hwnd );
2257 2258
        req->extra_offset = -1;
        switch(offset)
2259 2260
        {
        case GWL_STYLE:
2261 2262 2263
            req->flags = SET_WIN_STYLE;
            req->style = newval;
            break;
2264
        case GWL_EXSTYLE:
2265 2266
            req->flags = SET_WIN_EXSTYLE;
            req->ex_style = newval;
2267
            break;
2268
        case GWLP_ID:
2269 2270 2271
            req->flags = SET_WIN_ID;
            req->id = newval;
            break;
2272
        case GWLP_HINSTANCE:
2273
            req->flags = SET_WIN_INSTANCE;
2274
            req->instance = wine_server_client_ptr( (void *)newval );
2275
            break;
2276 2277
        case GWLP_WNDPROC:
            req->flags = SET_WIN_UNICODE;
2278
            req->is_unicode = (wndPtr->flags & WIN_ISUNICODE) != 0;
2279
            break;
2280
        case GWLP_USERDATA:
2281
            req->flags = SET_WIN_USERDATA;
2282
            req->user_data = newval;
2283 2284
            break;
        default:
2285
            req->flags = SET_WIN_EXTRA;
2286
            req->extra_offset = offset;
2287 2288
            req->extra_size = size;
            set_win_data( &req->extra_value, newval, size );
2289
        }
2290
        if ((ok = !wine_server_call_err( req )))
2291 2292 2293 2294
        {
            switch(offset)
            {
            case GWL_STYLE:
2295 2296
                wndPtr->dwStyle = newval;
                retval = reply->old_style;
2297 2298
                break;
            case GWL_EXSTYLE:
2299 2300
                wndPtr->dwExStyle = newval;
                retval = reply->old_ex_style;
2301
                break;
2302
            case GWLP_ID:
2303 2304
                wndPtr->wIDmenu = newval;
                retval = reply->old_id;
2305
                break;
2306
            case GWLP_HINSTANCE:
2307
                wndPtr->hInstance = (HINSTANCE)newval;
2308
                retval = (ULONG_PTR)wine_server_get_ptr( reply->old_instance );
2309
                break;
2310 2311
            case GWLP_WNDPROC:
                break;
2312
            case GWLP_USERDATA:
2313
                wndPtr->userdata = newval;
2314
                retval = reply->old_user_data;
2315
                break;
2316
            default:
2317 2318
                retval = get_win_data( (char *)wndPtr->wExtra + offset, size );
                set_win_data( (char *)wndPtr->wExtra + offset, newval, size );
2319
                break;
2320 2321
            }
        }
2322 2323 2324
    }
    SERVER_END_REQ;
    WIN_ReleasePtr( wndPtr );
2325

2326
    if (!ok) return 0;
2327

2328
    if (offset == GWL_STYLE || offset == GWL_EXSTYLE)
2329
    {
2330 2331
        style.styleOld = retval;
        style.styleNew = newval;
2332
        USER_Driver->pSetWindowStyle( hwnd, offset, &style );
2333
        SendMessageW( hwnd, WM_STYLECHANGED, offset, (LPARAM)&style );
2334
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
2335

Alexandre Julliard's avatar
Alexandre Julliard committed
2336 2337 2338 2339
    return retval;
}


2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363
/**********************************************************************
 *		GetWindowWord (USER32.@)
 */
WORD WINAPI GetWindowWord( HWND hwnd, INT offset )
{
    switch(offset)
    {
    case GWLP_ID:
    case GWLP_HINSTANCE:
    case GWLP_HWNDPARENT:
        break;
    default:
        if (offset < 0)
        {
            WARN("Invalid offset %d\n", offset );
            SetLastError( ERROR_INVALID_INDEX );
            return 0;
        }
        break;
    }
    return WIN_GetWindowLong( hwnd, offset, sizeof(WORD), FALSE );
}


Alexandre Julliard's avatar
Alexandre Julliard committed
2364
/**********************************************************************
2365
 *		GetWindowLongA (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2366
 */
2367
LONG WINAPI GetWindowLongA( HWND hwnd, INT offset )
Alexandre Julliard's avatar
Alexandre Julliard committed
2368
{
2369
    return WIN_GetWindowLong( hwnd, offset, sizeof(LONG), FALSE );
Alexandre Julliard's avatar
Alexandre Julliard committed
2370 2371 2372 2373
}


/**********************************************************************
2374
 *		GetWindowLongW (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2375
 */
2376
LONG WINAPI GetWindowLongW( HWND hwnd, INT offset )
Alexandre Julliard's avatar
Alexandre Julliard committed
2377
{
2378
    return WIN_GetWindowLong( hwnd, offset, sizeof(LONG), TRUE );
Alexandre Julliard's avatar
Alexandre Julliard committed
2379 2380 2381
}


2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405
/**********************************************************************
 *		SetWindowWord (USER32.@)
 */
WORD WINAPI SetWindowWord( HWND hwnd, INT offset, WORD newval )
{
    switch(offset)
    {
    case GWLP_ID:
    case GWLP_HINSTANCE:
    case GWLP_HWNDPARENT:
        break;
    default:
        if (offset < 0)
        {
            WARN("Invalid offset %d\n", offset );
            SetLastError( ERROR_INVALID_INDEX );
            return 0;
        }
        break;
    }
    return WIN_SetWindowLong( hwnd, offset, sizeof(WORD), newval, FALSE );
}


Alexandre Julliard's avatar
Alexandre Julliard committed
2406
/**********************************************************************
2407
 *		SetWindowLongA (USER32.@)
2408 2409
 *
 * See SetWindowLongW.
Alexandre Julliard's avatar
Alexandre Julliard committed
2410
 */
2411
LONG WINAPI SetWindowLongA( HWND hwnd, INT offset, LONG newval )
Alexandre Julliard's avatar
Alexandre Julliard committed
2412
{
2413
    return WIN_SetWindowLong( hwnd, offset, sizeof(LONG), newval, FALSE );
Alexandre Julliard's avatar
Alexandre Julliard committed
2414 2415 2416
}


Alexandre Julliard's avatar
Alexandre Julliard committed
2417
/**********************************************************************
2418
 *		SetWindowLongW (USER32.@) Set window attribute
Alexandre Julliard's avatar
Alexandre Julliard committed
2419 2420
 *
 * SetWindowLong() alters one of a window's attributes or sets a 32-bit (long)
2421
 * value in a window's extra memory.
Alexandre Julliard's avatar
Alexandre Julliard committed
2422 2423 2424 2425 2426 2427 2428 2429 2430 2431
 *
 * The _hwnd_ parameter specifies the window.  is the handle to a
 * window that has extra memory. The _newval_ parameter contains the
 * new attribute or extra memory value.  If positive, the _offset_
 * parameter is the byte-addressed location in the window's extra
 * memory to set.  If negative, _offset_ specifies the window
 * attribute to set, and should be one of the following values:
 *
 * GWL_EXSTYLE      The window's extended window style
 *
2432
 * GWL_STYLE        The window's window style.
Alexandre Julliard's avatar
Alexandre Julliard committed
2433
 *
2434
 * GWLP_WNDPROC     Pointer to the window's window procedure.
Alexandre Julliard's avatar
Alexandre Julliard committed
2435
 *
2436
 * GWLP_HINSTANCE   The window's pplication instance handle.
Alexandre Julliard's avatar
Alexandre Julliard committed
2437
 *
2438
 * GWLP_ID          The window's identifier.
Alexandre Julliard's avatar
Alexandre Julliard committed
2439
 *
2440
 * GWLP_USERDATA    The window's user-specified data.
Alexandre Julliard's avatar
Alexandre Julliard committed
2441
 *
2442
 * If the window is a dialog box, the _offset_ parameter can be one of
Alexandre Julliard's avatar
Alexandre Julliard committed
2443 2444
 * the following values:
 *
2445
 * DWLP_DLGPROC     The address of the window's dialog box procedure.
Alexandre Julliard's avatar
Alexandre Julliard committed
2446
 *
2447
 * DWLP_MSGRESULT   The return value of a message
Alexandre Julliard's avatar
Alexandre Julliard committed
2448 2449
 *                  that the dialog box procedure processed.
 *
2450
 * DWLP_USER        Application specific information.
Alexandre Julliard's avatar
Alexandre Julliard committed
2451 2452 2453 2454 2455 2456 2457 2458
 *
 * RETURNS
 *
 * If successful, returns the previous value located at _offset_. Otherwise,
 * returns 0.
 *
 * NOTES
 *
2459
 * Extra memory for a window class is specified by a nonzero cbWndExtra
Alexandre Julliard's avatar
Alexandre Julliard committed
2460 2461
 * parameter of the WNDCLASS structure passed to RegisterClass() at the
 * time of class creation.
2462
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
2463 2464 2465 2466 2467 2468 2469
 * Using GWL_WNDPROC to set a new window procedure effectively creates
 * a window subclass. Use CallWindowProc() in the new windows procedure
 * to pass messages to the superclass's window procedure.
 *
 * The user data is reserved for use by the application which created
 * the window.
 *
2470
 * Do not use GWL_STYLE to change the window's WS_DISABLED style;
Alexandre Julliard's avatar
Alexandre Julliard committed
2471 2472 2473 2474 2475 2476
 * instead, call the EnableWindow() function to change the window's
 * disabled state.
 *
 * Do not use GWL_HWNDPARENT to reset the window's parent, use
 * SetParent() instead.
 *
2477 2478 2479 2480 2481
 * Win95:
 * When offset is GWL_STYLE and the calling app's ver is 4.0,
 * it sends WM_STYLECHANGING before changing the settings
 * and WM_STYLECHANGED afterwards.
 * App ver 4.0 can't use SetWindowLong to change WS_EX_TOPMOST.
Alexandre Julliard's avatar
Alexandre Julliard committed
2482
 */
2483
LONG WINAPI SetWindowLongW(
2484 2485 2486
    HWND hwnd,  /* [in] window to alter */
    INT offset, /* [in] offset, in bytes, of location to alter */
    LONG newval /* [in] new value of location */
Alexandre Julliard's avatar
Alexandre Julliard committed
2487
) {
2488
    return WIN_SetWindowLong( hwnd, offset, sizeof(LONG), newval, TRUE );
Alexandre Julliard's avatar
Alexandre Julliard committed
2489 2490 2491 2492
}


/*******************************************************************
2493
 *		GetWindowTextA (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2494
 */
2495
INT WINAPI GetWindowTextA( HWND hwnd, LPSTR lpString, INT nMaxCount )
Alexandre Julliard's avatar
Alexandre Julliard committed
2496
{
2497 2498
    WCHAR *buffer;

2499 2500
    if (!lpString) return 0;

2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511
    if (WIN_IsCurrentProcess( hwnd ))
        return (INT)SendMessageA( hwnd, WM_GETTEXT, nMaxCount, (LPARAM)lpString );

    /* when window belongs to other process, don't send a message */
    if (nMaxCount <= 0) return 0;
    if (!(buffer = HeapAlloc( GetProcessHeap(), 0, nMaxCount * sizeof(WCHAR) ))) return 0;
    get_server_window_text( hwnd, buffer, nMaxCount );
    if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, lpString, nMaxCount, NULL, NULL ))
        lpString[nMaxCount-1] = 0;
    HeapFree( GetProcessHeap(), 0, buffer );
    return strlen(lpString);
Alexandre Julliard's avatar
Alexandre Julliard committed
2512
}
Alexandre Julliard's avatar
Alexandre Julliard committed
2513

2514

Alexandre Julliard's avatar
Alexandre Julliard committed
2515
/*******************************************************************
2516
 *		InternalGetWindowText (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2517
 */
2518
INT WINAPI InternalGetWindowText(HWND hwnd,LPWSTR lpString,INT nMaxCount )
Alexandre Julliard's avatar
Alexandre Julliard committed
2519
{
2520 2521 2522 2523
    WND *win;

    if (nMaxCount <= 0) return 0;
    if (!(win = WIN_GetPtr( hwnd ))) return 0;
2524 2525
    if (win == WND_DESKTOP) lpString[0] = 0;
    else if (win != WND_OTHER_PROCESS)
2526 2527 2528 2529 2530 2531 2532 2533 2534
    {
        if (win->text) lstrcpynW( lpString, win->text, nMaxCount );
        else lpString[0] = 0;
        WIN_ReleasePtr( win );
    }
    else
    {
        get_server_window_text( hwnd, lpString, nMaxCount );
    }
2535
    return strlenW(lpString);
Alexandre Julliard's avatar
Alexandre Julliard committed
2536 2537
}

Alexandre Julliard's avatar
Alexandre Julliard committed
2538 2539

/*******************************************************************
2540
 *		GetWindowTextW (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2541
 */
2542
INT WINAPI GetWindowTextW( HWND hwnd, LPWSTR lpString, INT nMaxCount )
Alexandre Julliard's avatar
Alexandre Julliard committed
2543
{
2544 2545
    if (!lpString) return 0;

2546 2547 2548 2549 2550 2551 2552
    if (WIN_IsCurrentProcess( hwnd ))
        return (INT)SendMessageW( hwnd, WM_GETTEXT, nMaxCount, (LPARAM)lpString );

    /* when window belongs to other process, don't send a message */
    if (nMaxCount <= 0) return 0;
    get_server_window_text( hwnd, lpString, nMaxCount );
    return strlenW(lpString);
Alexandre Julliard's avatar
Alexandre Julliard committed
2553 2554 2555
}


Alexandre Julliard's avatar
Alexandre Julliard committed
2556
/*******************************************************************
2557
 *		SetWindowTextA (USER32.@)
2558
 *		SetWindowText  (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2559
 */
2560
BOOL WINAPI SetWindowTextA( HWND hwnd, LPCSTR lpString )
Alexandre Julliard's avatar
Alexandre Julliard committed
2561
{
2562 2563 2564 2565 2566
    if (is_broadcast(hwnd))
    {
        SetLastError( ERROR_INVALID_PARAMETER );
        return FALSE;
    }
2567
    if (!WIN_IsCurrentProcess( hwnd ))
2568
        WARN( "setting text %s of other process window %p should not use SendMessage\n",
2569
               debugstr_a(lpString), hwnd );
2570
    return (BOOL)SendMessageA( hwnd, WM_SETTEXT, 0, (LPARAM)lpString );
Alexandre Julliard's avatar
Alexandre Julliard committed
2571 2572
}

Alexandre Julliard's avatar
Alexandre Julliard committed
2573

Alexandre Julliard's avatar
Alexandre Julliard committed
2574
/*******************************************************************
2575
 *		SetWindowTextW (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2576
 */
2577
BOOL WINAPI SetWindowTextW( HWND hwnd, LPCWSTR lpString )
Alexandre Julliard's avatar
Alexandre Julliard committed
2578
{
2579 2580 2581 2582 2583
    if (is_broadcast(hwnd))
    {
        SetLastError( ERROR_INVALID_PARAMETER );
        return FALSE;
    }
2584
    if (!WIN_IsCurrentProcess( hwnd ))
2585
        WARN( "setting text %s of other process window %p should not use SendMessage\n",
2586
               debugstr_w(lpString), hwnd );
2587
    return (BOOL)SendMessageW( hwnd, WM_SETTEXT, 0, (LPARAM)lpString );
Alexandre Julliard's avatar
Alexandre Julliard committed
2588 2589 2590
}


Alexandre Julliard's avatar
Alexandre Julliard committed
2591
/*******************************************************************
2592
 *		GetWindowTextLengthA (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2593
 */
2594
INT WINAPI GetWindowTextLengthA( HWND hwnd )
Alexandre Julliard's avatar
Alexandre Julliard committed
2595
{
2596
    return SendMessageA( hwnd, WM_GETTEXTLENGTH, 0, 0 );
Alexandre Julliard's avatar
Alexandre Julliard committed
2597 2598 2599
}

/*******************************************************************
2600
 *		GetWindowTextLengthW (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2601
 */
2602
INT WINAPI GetWindowTextLengthW( HWND hwnd )
Alexandre Julliard's avatar
Alexandre Julliard committed
2603
{
2604
    return SendMessageW( hwnd, WM_GETTEXTLENGTH, 0, 0 );
Alexandre Julliard's avatar
Alexandre Julliard committed
2605 2606
}

Alexandre Julliard's avatar
Alexandre Julliard committed
2607

Alexandre Julliard's avatar
Alexandre Julliard committed
2608
/*******************************************************************
2609
 *		IsWindow (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2610
 */
2611
BOOL WINAPI IsWindow( HWND hwnd )
Alexandre Julliard's avatar
Alexandre Julliard committed
2612
{
2613
    WND *ptr;
2614
    BOOL ret;
2615

2616
    if (!(ptr = WIN_GetPtr( hwnd ))) return FALSE;
2617
    if (ptr == WND_DESKTOP) return TRUE;
2618 2619

    if (ptr != WND_OTHER_PROCESS)
2620
    {
2621
        WIN_ReleasePtr( ptr );
2622
        return TRUE;
2623 2624
    }

2625 2626
    /* check other processes */
    SERVER_START_REQ( get_window_info )
2627
    {
2628
        req->handle = wine_server_user_handle( hwnd );
2629
        ret = !wine_server_call_err( req );
2630
    }
2631
    SERVER_END_REQ;
2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643
    return ret;
}


/***********************************************************************
 *		GetWindowThreadProcessId (USER32.@)
 */
DWORD WINAPI GetWindowThreadProcessId( HWND hwnd, LPDWORD process )
{
    WND *ptr;
    DWORD tid = 0;

2644
    if (!(ptr = WIN_GetPtr( hwnd )))
2645
    {
2646 2647 2648 2649
        SetLastError( ERROR_INVALID_WINDOW_HANDLE);
        return 0;
    }

2650
    if (ptr != WND_OTHER_PROCESS && ptr != WND_DESKTOP)
2651 2652 2653 2654 2655
    {
        /* got a valid window */
        tid = ptr->tid;
        if (process) *process = GetCurrentProcessId();
        WIN_ReleasePtr( ptr );
2656 2657 2658 2659 2660 2661
        return tid;
    }

    /* check other processes */
    SERVER_START_REQ( get_window_info )
    {
2662
        req->handle = wine_server_user_handle( hwnd );
2663
        if (!wine_server_call_err( req ))
2664
        {
2665 2666
            tid = (DWORD)reply->tid;
            if (process) *process = (DWORD)reply->pid;
2667 2668 2669 2670
        }
    }
    SERVER_END_REQ;
    return tid;
Alexandre Julliard's avatar
Alexandre Julliard committed
2671 2672 2673
}


Alexandre Julliard's avatar
Alexandre Julliard committed
2674
/*****************************************************************
2675
 *		GetParent (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2676
 */
2677
HWND WINAPI GetParent( HWND hwnd )
Alexandre Julliard's avatar
Alexandre Julliard committed
2678
{
2679
    WND *wndPtr;
2680 2681
    HWND retvalue = 0;

2682 2683 2684 2685 2686
    if (!(wndPtr = WIN_GetPtr( hwnd )))
    {
        SetLastError( ERROR_INVALID_WINDOW_HANDLE );
        return 0;
    }
2687
    if (wndPtr == WND_DESKTOP) return 0;
2688 2689 2690 2691 2692 2693 2694
    if (wndPtr == WND_OTHER_PROCESS)
    {
        LONG style = GetWindowLongW( hwnd, GWL_STYLE );
        if (style & (WS_POPUP | WS_CHILD))
        {
            SERVER_START_REQ( get_window_tree )
            {
2695
                req->handle = wine_server_user_handle( hwnd );
2696
                if (!wine_server_call_err( req ))
2697
                {
2698 2699
                    if (style & WS_POPUP) retvalue = wine_server_ptr_handle( reply->owner );
                    else if (style & WS_CHILD) retvalue = wine_server_ptr_handle( reply->parent );
2700 2701 2702 2703 2704 2705
                }
            }
            SERVER_END_REQ;
        }
    }
    else
2706
    {
2707 2708
        if (wndPtr->dwStyle & WS_POPUP) retvalue = wndPtr->owner;
        else if (wndPtr->dwStyle & WS_CHILD) retvalue = wndPtr->parent;
2709
        WIN_ReleasePtr( wndPtr );
2710
    }
2711
    return retvalue;
Alexandre Julliard's avatar
Alexandre Julliard committed
2712 2713 2714 2715
}


/*****************************************************************
2716
 *		GetAncestor (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2717
 */
2718
HWND WINAPI GetAncestor( HWND hwnd, UINT type )
Alexandre Julliard's avatar
Alexandre Julliard committed
2719
{
2720
    WND *win;
2721
    HWND *list, ret = 0;
2722

2723
    switch(type)
2724
    {
2725
    case GA_PARENT:
2726 2727 2728 2729 2730
        if (!(win = WIN_GetPtr( hwnd )))
        {
            SetLastError( ERROR_INVALID_WINDOW_HANDLE );
            return 0;
        }
2731
        if (win == WND_DESKTOP) return 0;
2732
        if (win != WND_OTHER_PROCESS)
2733
        {
2734 2735
            ret = win->parent;
            WIN_ReleasePtr( win );
2736
        }
2737
        else /* need to query the server */
2738
        {
2739
            SERVER_START_REQ( get_window_tree )
2740
            {
2741 2742
                req->handle = wine_server_user_handle( hwnd );
                if (!wine_server_call_err( req )) ret = wine_server_ptr_handle( reply->parent );
2743
            }
2744
            SERVER_END_REQ;
2745
        }
2746
        break;
2747

2748
    case GA_ROOT:
2749
        if (!(list = list_window_parents( hwnd ))) return 0;
2750

2751 2752 2753 2754 2755 2756 2757 2758 2759
        if (!list[0] || !list[1]) ret = WIN_GetFullHandle( hwnd );  /* top-level window */
        else
        {
            int count = 2;
            while (list[count]) count++;
            ret = list[count - 2];  /* get the one before the desktop */
        }
        HeapFree( GetProcessHeap(), 0, list );
        break;
2760

2761
    case GA_ROOTOWNER:
2762 2763
        if (is_desktop_window( hwnd )) return 0;
        ret = WIN_GetFullHandle( hwnd );
2764 2765
        for (;;)
        {
2766 2767 2768
            HWND parent = GetParent( ret );
            if (!parent) break;
            ret = parent;
2769
        }
2770
        break;
2771 2772
    }
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
2773 2774
}

Alexandre Julliard's avatar
Alexandre Julliard committed
2775

Alexandre Julliard's avatar
Alexandre Julliard committed
2776
/*****************************************************************
2777
 *		SetParent (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2778
 */
2779
HWND WINAPI SetParent( HWND hwnd, HWND parent )
Alexandre Julliard's avatar
Alexandre Julliard committed
2780
{
2781
    HWND full_handle;
2782 2783 2784 2785
    HWND old_parent = 0;
    BOOL was_visible;
    WND *wndPtr;
    BOOL ret;
2786

2787 2788 2789 2790 2791 2792
    if (is_broadcast(hwnd) || is_broadcast(parent))
    {
        SetLastError(ERROR_INVALID_PARAMETER);
        return 0;
    }

2793
    if (!parent) parent = GetDesktopWindow();
2794
    else if (parent == HWND_MESSAGE) parent = get_hwnd_message_parent();
2795 2796 2797 2798 2799 2800 2801 2802
    else parent = WIN_GetFullHandle( parent );

    if (!IsWindow( parent ))
    {
        SetLastError( ERROR_INVALID_WINDOW_HANDLE );
        return 0;
    }

2803 2804 2805 2806 2807 2808 2809
    /* Some applications try to set a child as a parent */
    if (IsChild(hwnd, parent))
    {
        SetLastError( ERROR_INVALID_PARAMETER );
        return 0;
    }

2810
    if (!(full_handle = WIN_IsCurrentThread( hwnd )))
2811
        return (HWND)SendMessageW( hwnd, WM_WINE_SETPARENT, (WPARAM)parent, 0 );
2812

2813 2814 2815 2816 2817 2818
    if (full_handle == parent)
    {
        SetLastError( ERROR_INVALID_PARAMETER );
        return 0;
    }

2819 2820 2821 2822 2823 2824 2825 2826 2827
    /* Windows hides the window first, then shows it again
     * including the WM_SHOWWINDOW messages and all */
    was_visible = ShowWindow( hwnd, SW_HIDE );

    wndPtr = WIN_GetPtr( hwnd );
    if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return 0;

    SERVER_START_REQ( set_parent )
    {
2828 2829
        req->handle = wine_server_user_handle( hwnd );
        req->parent = wine_server_user_handle( parent );
2830 2831
        if ((ret = !wine_server_call( req )))
        {
2832 2833
            old_parent = wine_server_ptr_handle( reply->old_parent );
            wndPtr->parent = parent = wine_server_ptr_handle( reply->full_parent );
2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846
        }

    }
    SERVER_END_REQ;
    WIN_ReleasePtr( wndPtr );
    if (!ret) return 0;

    USER_Driver->pSetParent( full_handle, parent, old_parent );

    /* SetParent additionally needs to make hwnd the topmost window
       in the x-order and send the expected WM_WINDOWPOSCHANGING and
       WM_WINDOWPOSCHANGED notification messages.
    */
2847
    SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0,
2848 2849 2850 2851 2852
                  SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | (was_visible ? SWP_SHOWWINDOW : 0) );
    /* FIXME: a WM_MOVE is also generated (in the DefWindowProc handler
     * for WM_WINDOWPOSCHANGED) in Windows, should probably remove SWP_NOMOVE */

    return old_parent;
Alexandre Julliard's avatar
Alexandre Julliard committed
2853 2854
}

2855

Alexandre Julliard's avatar
Alexandre Julliard committed
2856
/*******************************************************************
2857
 *		IsChild (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2858
 */
2859
BOOL WINAPI IsChild( HWND parent, HWND child )
Alexandre Julliard's avatar
Alexandre Julliard committed
2860
{
2861
    HWND *list = list_window_parents( child );
2862 2863 2864 2865
    int i;
    BOOL ret;

    if (!list) return FALSE;
2866
    parent = WIN_GetFullHandle( parent );
2867
    for (i = 0; list[i]; i++) if (list[i] == parent) break;
2868
    ret = list[i] && list[i+1];
2869 2870
    HeapFree( GetProcessHeap(), 0, list );
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
2871 2872 2873
}


Alexandre Julliard's avatar
Alexandre Julliard committed
2874
/***********************************************************************
2875
 *		IsWindowVisible (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2876
 */
2877
BOOL WINAPI IsWindowVisible( HWND hwnd )
Alexandre Julliard's avatar
Alexandre Julliard committed
2878
{
2879
    HWND *list;
2880
    BOOL retval = TRUE;
2881 2882 2883
    int i;

    if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)) return FALSE;
2884
    if (!(list = list_window_parents( hwnd ))) return TRUE;
2885
    if (list[0])
2886 2887 2888
    {
        for (i = 0; list[i+1]; i++)
            if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_VISIBLE)) break;
2889
        retval = !list[i+1] && (list[i] == GetDesktopWindow());  /* top message window isn't visible */
2890
    }
2891
    HeapFree( GetProcessHeap(), 0, list );
2892
    return retval;
Alexandre Julliard's avatar
Alexandre Julliard committed
2893 2894
}

Alexandre Julliard's avatar
Alexandre Julliard committed
2895

Alexandre Julliard's avatar
Alexandre Julliard committed
2896 2897
/***********************************************************************
 *           WIN_IsWindowDrawable
Alexandre Julliard's avatar
Alexandre Julliard committed
2898 2899 2900
 *
 * hwnd is drawable when it is visible, all parents are not
 * minimized, and it is itself not minimized unless we are
Alexandre Julliard's avatar
Alexandre Julliard committed
2901
 * trying to draw its default class icon.
Alexandre Julliard's avatar
Alexandre Julliard committed
2902
 */
2903
BOOL WIN_IsWindowDrawable( HWND hwnd, BOOL icon )
Alexandre Julliard's avatar
Alexandre Julliard committed
2904
{
2905
    HWND *list;
2906
    BOOL retval = TRUE;
2907
    int i;
2908
    LONG style = GetWindowLongW( hwnd, GWL_STYLE );
2909

2910
    if (!(style & WS_VISIBLE)) return FALSE;
2911
    if ((style & WS_MINIMIZE) && icon && GetClassLongPtrW( hwnd, GCLP_HICON ))  return FALSE;
2912

2913
    if (!(list = list_window_parents( hwnd ))) return TRUE;
2914
    if (list[0])
2915 2916 2917 2918
    {
        for (i = 0; list[i+1]; i++)
            if ((GetWindowLongW( list[i], GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != WS_VISIBLE)
                break;
2919
        retval = !list[i+1] && (list[i] == GetDesktopWindow());  /* top message window isn't visible */
2920
    }
2921 2922
    HeapFree( GetProcessHeap(), 0, list );
    return retval;
Alexandre Julliard's avatar
Alexandre Julliard committed
2923 2924
}

Alexandre Julliard's avatar
Alexandre Julliard committed
2925

Alexandre Julliard's avatar
Alexandre Julliard committed
2926
/*******************************************************************
2927
 *		GetTopWindow (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2928
 */
2929
HWND WINAPI GetTopWindow( HWND hwnd )
Alexandre Julliard's avatar
Alexandre Julliard committed
2930
{
2931 2932
    if (!hwnd) hwnd = GetDesktopWindow();
    return GetWindow( hwnd, GW_CHILD );
Alexandre Julliard's avatar
Alexandre Julliard committed
2933 2934 2935
}


Alexandre Julliard's avatar
Alexandre Julliard committed
2936
/*******************************************************************
2937
 *		GetWindow (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2938
 */
2939
HWND WINAPI GetWindow( HWND hwnd, UINT rel )
Alexandre Julliard's avatar
Alexandre Julliard committed
2940
{
2941
    HWND retval = 0;
2942

2943
    if (rel == GW_OWNER)  /* this one may be available locally */
Alexandre Julliard's avatar
Alexandre Julliard committed
2944
    {
2945 2946 2947 2948 2949 2950
        WND *wndPtr = WIN_GetPtr( hwnd );
        if (!wndPtr)
        {
            SetLastError( ERROR_INVALID_HANDLE );
            return 0;
        }
2951
        if (wndPtr == WND_DESKTOP) return 0;
2952 2953 2954 2955 2956 2957 2958
        if (wndPtr != WND_OTHER_PROCESS)
        {
            retval = wndPtr->owner;
            WIN_ReleasePtr( wndPtr );
            return retval;
        }
        /* else fall through to server call */
2959
    }
2960

2961 2962
    SERVER_START_REQ( get_window_tree )
    {
2963
        req->handle = wine_server_user_handle( hwnd );
2964
        if (!wine_server_call_err( req ))
Alexandre Julliard's avatar
Alexandre Julliard committed
2965
        {
2966
            switch(rel)
2967
            {
2968
            case GW_HWNDFIRST:
2969
                retval = wine_server_ptr_handle( reply->first_sibling );
2970 2971
                break;
            case GW_HWNDLAST:
2972
                retval = wine_server_ptr_handle( reply->last_sibling );
2973 2974
                break;
            case GW_HWNDNEXT:
2975
                retval = wine_server_ptr_handle( reply->next_sibling );
2976 2977
                break;
            case GW_HWNDPREV:
2978
                retval = wine_server_ptr_handle( reply->prev_sibling );
2979
                break;
2980
            case GW_OWNER:
2981
                retval = wine_server_ptr_handle( reply->owner );
2982
                break;
2983
            case GW_CHILD:
2984
                retval = wine_server_ptr_handle( reply->first_child );
2985
                break;
2986
            }
2987
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
2988
    }
2989
    SERVER_END_REQ;
2990
    return retval;
Alexandre Julliard's avatar
Alexandre Julliard committed
2991 2992 2993
}


Alexandre Julliard's avatar
Alexandre Julliard committed
2994
/*******************************************************************
2995
 *		ShowOwnedPopups (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2996
 */
2997
BOOL WINAPI ShowOwnedPopups( HWND owner, BOOL fShow )
Alexandre Julliard's avatar
Alexandre Julliard committed
2998
{
2999
    int count = 0;
3000
    WND *pWnd;
3001
    HWND *win_array = WIN_ListChildren( GetDesktopWindow() );
3002

3003
    if (!win_array) return TRUE;
3004

3005 3006
    while (win_array[count]) count++;
    while (--count >= 0)
Alexandre Julliard's avatar
Alexandre Julliard committed
3007
    {
3008
        if (GetWindow( win_array[count], GW_OWNER ) != owner) continue;
3009 3010
        if (!(pWnd = WIN_GetPtr( win_array[count] ))) continue;
        if (pWnd == WND_OTHER_PROCESS) continue;
3011
        if (fShow)
3012
        {
3013
            if (pWnd->flags & WIN_NEEDS_SHOW_OWNEDPOPUP)
3014
            {
3015 3016 3017 3018 3019 3020 3021
                WIN_ReleasePtr( pWnd );
                /* In Windows, ShowOwnedPopups(TRUE) generates
                 * WM_SHOWWINDOW messages with SW_PARENTOPENING,
                 * regardless of the state of the owner
                 */
                SendMessageW(win_array[count], WM_SHOWWINDOW, SW_SHOWNORMAL, SW_PARENTOPENING);
                continue;
3022
            }
3023 3024 3025 3026
        }
        else
        {
            if (pWnd->dwStyle & WS_VISIBLE)
3027
            {
3028 3029 3030 3031 3032 3033 3034
                WIN_ReleasePtr( pWnd );
                /* In Windows, ShowOwnedPopups(FALSE) generates
                 * WM_SHOWWINDOW messages with SW_PARENTCLOSING,
                 * regardless of the state of the owner
                 */
                SendMessageW(win_array[count], WM_SHOWWINDOW, SW_HIDE, SW_PARENTCLOSING);
                continue;
3035 3036
            }
        }
3037
        WIN_ReleasePtr( pWnd );
Alexandre Julliard's avatar
Alexandre Julliard committed
3038
    }
3039
    HeapFree( GetProcessHeap(), 0, win_array );
Alexandre Julliard's avatar
Alexandre Julliard committed
3040
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
3041
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3042 3043


Alexandre Julliard's avatar
Alexandre Julliard committed
3044
/*******************************************************************
3045
 *		GetLastActivePopup (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
3046
 */
3047
HWND WINAPI GetLastActivePopup( HWND hwnd )
Alexandre Julliard's avatar
Alexandre Julliard committed
3048
{
3049 3050 3051 3052
    HWND retval = hwnd;

    SERVER_START_REQ( get_window_info )
    {
3053 3054
        req->handle = wine_server_user_handle( hwnd );
        if (!wine_server_call_err( req )) retval = wine_server_ptr_handle( reply->last_active );
3055 3056
    }
    SERVER_END_REQ;
3057
    return retval;
Alexandre Julliard's avatar
Alexandre Julliard committed
3058 3059
}

Alexandre Julliard's avatar
Alexandre Julliard committed
3060

3061 3062 3063 3064 3065 3066 3067
/*******************************************************************
 *           WIN_ListChildren
 *
 * Build an array of the children of a given window. The array must be
 * freed with HeapFree. Returns NULL when no windows are found.
 */
HWND *WIN_ListChildren( HWND hwnd )
Alexandre Julliard's avatar
Alexandre Julliard committed
3068
{
3069 3070 3071 3072 3073
    if (!hwnd)
    {
        SetLastError( ERROR_INVALID_WINDOW_HANDLE );
        return NULL;
    }
3074
    return list_window_children( 0, hwnd, NULL, 0 );
Alexandre Julliard's avatar
Alexandre Julliard committed
3075
}
3076

Alexandre Julliard's avatar
Alexandre Julliard committed
3077 3078

/*******************************************************************
3079
 *		EnumWindows (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
3080
 */
3081
BOOL WINAPI EnumWindows( WNDENUMPROC lpEnumFunc, LPARAM lParam )
Alexandre Julliard's avatar
Alexandre Julliard committed
3082
{
3083 3084
    HWND *list;
    BOOL ret = TRUE;
3085 3086 3087
    int i;

    USER_CheckNotLock();
Alexandre Julliard's avatar
Alexandre Julliard committed
3088

Alexandre Julliard's avatar
Alexandre Julliard committed
3089
    /* We have to build a list of all windows first, to avoid */
3090 3091
    /* unpleasant side-effects, for instance if the callback */
    /* function changes the Z-order of the windows.          */
Alexandre Julliard's avatar
Alexandre Julliard committed
3092

3093
    if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
3094

Alexandre Julliard's avatar
Alexandre Julliard committed
3095
    /* Now call the callback function for every window */
Alexandre Julliard's avatar
Alexandre Julliard committed
3096

3097
    for (i = 0; list[i]; i++)
Alexandre Julliard's avatar
Alexandre Julliard committed
3098 3099
    {
        /* Make sure that the window still exists */
3100 3101
        if (!IsWindow( list[i] )) continue;
        if (!(ret = lpEnumFunc( list[i], lParam ))) break;
Alexandre Julliard's avatar
Alexandre Julliard committed
3102
    }
3103
    HeapFree( GetProcessHeap(), 0, list );
3104
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
3105
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3106 3107


Alexandre Julliard's avatar
Alexandre Julliard committed
3108
/**********************************************************************
3109
 *		EnumThreadWindows (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
3110
 */
3111
BOOL WINAPI EnumThreadWindows( DWORD id, WNDENUMPROC func, LPARAM lParam )
Alexandre Julliard's avatar
Alexandre Julliard committed
3112
{
3113
    HWND *list;
3114
    int i;
3115
    BOOL ret = TRUE;
3116 3117

    USER_CheckNotLock();
3118

3119
    if (!(list = list_window_children( 0, GetDesktopWindow(), NULL, id ))) return TRUE;
3120 3121 3122 3123

    /* Now call the callback function for every window */

    for (i = 0; list[i]; i++)
3124
        if (!(ret = func( list[i], lParam ))) break;
3125
    HeapFree( GetProcessHeap(), 0, list );
3126
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
3127 3128 3129
}


3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148
/***********************************************************************
 *              EnumDesktopWindows   (USER32.@)
 */
BOOL WINAPI EnumDesktopWindows( HDESK desktop, WNDENUMPROC func, LPARAM lparam )
{
    HWND *list;
    int i;

    USER_CheckNotLock();

    if (!(list = list_window_children( desktop, 0, NULL, 0 ))) return TRUE;

    for (i = 0; list[i]; i++)
        if (!func( list[i], lparam )) break;
    HeapFree( GetProcessHeap(), 0, list );
    return TRUE;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
3149
/**********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
3150
 *           WIN_EnumChildWindows
Alexandre Julliard's avatar
Alexandre Julliard committed
3151
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
3152
 * Helper function for EnumChildWindows().
Alexandre Julliard's avatar
Alexandre Julliard committed
3153
 */
3154
static BOOL WIN_EnumChildWindows( HWND *list, WNDENUMPROC func, LPARAM lParam )
Alexandre Julliard's avatar
Alexandre Julliard committed
3155
{
3156 3157
    HWND *childList;
    BOOL ret = FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
3158

3159
    for ( ; *list; list++)
Alexandre Julliard's avatar
Alexandre Julliard committed
3160
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
3161
        /* Make sure that the window still exists */
3162
        if (!IsWindow( *list )) continue;
Alexandre Julliard's avatar
Alexandre Julliard committed
3163
        /* Build children list first */
3164
        childList = WIN_ListChildren( *list );
3165 3166

        ret = func( *list, lParam );
3167

Alexandre Julliard's avatar
Alexandre Julliard committed
3168 3169 3170
        if (childList)
        {
            if (ret) ret = WIN_EnumChildWindows( childList, func, lParam );
3171
            HeapFree( GetProcessHeap(), 0, childList );
Alexandre Julliard's avatar
Alexandre Julliard committed
3172
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
3173 3174 3175 3176 3177 3178 3179
        if (!ret) return FALSE;
    }
    return TRUE;
}


/**********************************************************************
3180
 *		EnumChildWindows (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
3181
 */
3182
BOOL WINAPI EnumChildWindows( HWND parent, WNDENUMPROC func, LPARAM lParam )
Alexandre Julliard's avatar
Alexandre Julliard committed
3183
{
3184
    HWND *list;
3185
    BOOL ret;
3186 3187

    USER_CheckNotLock();
Alexandre Julliard's avatar
Alexandre Julliard committed
3188

3189
    if (!(list = WIN_ListChildren( parent ))) return FALSE;
3190
    ret = WIN_EnumChildWindows( list, func, lParam );
3191
    HeapFree( GetProcessHeap(), 0, list );
3192
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
3193
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3194 3195


Alexandre Julliard's avatar
Alexandre Julliard committed
3196
/*******************************************************************
3197
 *		AnyPopup (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
3198
 */
3199
BOOL WINAPI AnyPopup(void)
Alexandre Julliard's avatar
Alexandre Julliard committed
3200
{
3201
    int i;
3202
    BOOL retvalue;
3203
    HWND *list = WIN_ListChildren( GetDesktopWindow() );
3204 3205 3206

    if (!list) return FALSE;
    for (i = 0; list[i]; i++)
3207
    {
3208
        if (IsWindowVisible( list[i] ) && GetWindow( list[i], GW_OWNER )) break;
3209
    }
3210
    retvalue = (list[i] != 0);
3211
    HeapFree( GetProcessHeap(), 0, list );
3212
    return retvalue;
Alexandre Julliard's avatar
Alexandre Julliard committed
3213 3214
}

Alexandre Julliard's avatar
Alexandre Julliard committed
3215 3216

/*******************************************************************
3217
 *		FlashWindow (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
3218
 */
3219
BOOL WINAPI FlashWindow( HWND hWnd, BOOL bInvert )
Alexandre Julliard's avatar
Alexandre Julliard committed
3220
{
3221
    WND *wndPtr;
Alexandre Julliard's avatar
Alexandre Julliard committed
3222

3223
    TRACE("%p\n", hWnd);
Alexandre Julliard's avatar
Alexandre Julliard committed
3224

3225
    if (IsIconic( hWnd ))
Alexandre Julliard's avatar
Alexandre Julliard committed
3226
    {
3227
        RedrawWindow( hWnd, 0, 0, RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW | RDW_FRAME );
3228 3229

        wndPtr = WIN_GetPtr(hWnd);
3230
        if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
3231 3232 3233 3234 3235 3236 3237 3238
        if (bInvert && !(wndPtr->flags & WIN_NCACTIVATED))
        {
            wndPtr->flags |= WIN_NCACTIVATED;
        }
        else
        {
            wndPtr->flags &= ~WIN_NCACTIVATED;
        }
3239
        WIN_ReleasePtr( wndPtr );
Alexandre Julliard's avatar
Alexandre Julliard committed
3240 3241 3242 3243
        return TRUE;
    }
    else
    {
3244 3245 3246
        WPARAM wparam;

        wndPtr = WIN_GetPtr(hWnd);
3247
        if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
3248
        hWnd = wndPtr->obj.handle;  /* make it a full handle */
3249

Alexandre Julliard's avatar
Alexandre Julliard committed
3250
        if (bInvert) wparam = !(wndPtr->flags & WIN_NCACTIVATED);
3251
        else wparam = (hWnd == GetForegroundWindow());
Alexandre Julliard's avatar
Alexandre Julliard committed
3252

3253
        WIN_ReleasePtr( wndPtr );
3254
        SendMessageW( hWnd, WM_NCACTIVATE, wparam, 0 );
Alexandre Julliard's avatar
Alexandre Julliard committed
3255 3256
        return wparam;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
3257 3258
}

3259 3260 3261 3262 3263 3264 3265 3266
/*******************************************************************
 *		FlashWindowEx (USER32.@)
 */
BOOL WINAPI FlashWindowEx( PFLASHWINFO pfwi )
{
    FIXME("%p\n", pfwi);
    return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
3267

Alexandre Julliard's avatar
Alexandre Julliard committed
3268
/*******************************************************************
3269
 *		GetWindowContextHelpId (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
3270
 */
3271
DWORD WINAPI GetWindowContextHelpId( HWND hwnd )
Alexandre Julliard's avatar
Alexandre Julliard committed
3272
{
3273
    DWORD retval;
3274
    WND *wnd = WIN_GetPtr( hwnd );
3275
    if (!wnd || wnd == WND_DESKTOP) return 0;
3276 3277 3278 3279 3280
    if (wnd == WND_OTHER_PROCESS)
    {
        if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
        return 0;
    }
3281
    retval = wnd->helpContext;
3282
    WIN_ReleasePtr( wnd );
3283
    return retval;
Alexandre Julliard's avatar
Alexandre Julliard committed
3284 3285 3286 3287
}


/*******************************************************************
3288
 *		SetWindowContextHelpId (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
3289
 */
3290
BOOL WINAPI SetWindowContextHelpId( HWND hwnd, DWORD id )
Alexandre Julliard's avatar
Alexandre Julliard committed
3291
{
3292
    WND *wnd = WIN_GetPtr( hwnd );
3293
    if (!wnd || wnd == WND_DESKTOP) return FALSE;
3294 3295 3296 3297 3298
    if (wnd == WND_OTHER_PROCESS)
    {
        if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
        return 0;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
3299
    wnd->helpContext = id;
3300
    WIN_ReleasePtr( wnd );
Alexandre Julliard's avatar
Alexandre Julliard committed
3301 3302 3303 3304
    return TRUE;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
3305
/*******************************************************************
3306
 *		DragDetect (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
3307
 */
3308
BOOL WINAPI DragDetect( HWND hWnd, POINT pt )
Alexandre Julliard's avatar
Alexandre Julliard committed
3309
{
3310 3311
    MSG msg;
    RECT rect;
3312 3313
    WORD wDragWidth = GetSystemMetrics(SM_CXDRAG);
    WORD wDragHeight= GetSystemMetrics(SM_CYDRAG);
Alexandre Julliard's avatar
Alexandre Julliard committed
3314

Alexandre Julliard's avatar
Alexandre Julliard committed
3315 3316
    rect.left = pt.x - wDragWidth;
    rect.right = pt.x + wDragWidth;
Alexandre Julliard's avatar
Alexandre Julliard committed
3317

Alexandre Julliard's avatar
Alexandre Julliard committed
3318 3319
    rect.top = pt.y - wDragHeight;
    rect.bottom = pt.y + wDragHeight;
Alexandre Julliard's avatar
Alexandre Julliard committed
3320

3321
    SetCapture(hWnd);
Alexandre Julliard's avatar
Alexandre Julliard committed
3322

Alexandre Julliard's avatar
Alexandre Julliard committed
3323 3324
    while(1)
    {
3325
        while (PeekMessageW( &msg, 0, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE ))
Alexandre Julliard's avatar
Alexandre Julliard committed
3326 3327
        {
            if( msg.message == WM_LBUTTONUP )
3328 3329 3330
            {
                ReleaseCapture();
                return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
3331 3332
            }
            if( msg.message == WM_MOUSEMOVE )
3333
            {
3334
                POINT tmp;
3335 3336
                tmp.x = (short)LOWORD(msg.lParam);
                tmp.y = (short)HIWORD(msg.lParam);
3337
                if( !PtInRect( &rect, tmp ))
Alexandre Julliard's avatar
Alexandre Julliard committed
3338
                {
3339 3340
                    ReleaseCapture();
                    return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
3341
                }
3342
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
3343
        }
3344
        WaitMessage();
Alexandre Julliard's avatar
Alexandre Julliard committed
3345 3346
    }
    return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
3347 3348
}

3349 3350 3351
/******************************************************************************
 *		GetWindowModuleFileNameA (USER32.@)
 */
3352
UINT WINAPI GetWindowModuleFileNameA( HWND hwnd, LPSTR module, UINT size )
3353
{
3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368
    WND *win;
    HINSTANCE hinst;

    TRACE( "%p, %p, %u\n", hwnd, module, size );

    win = WIN_GetPtr( hwnd );
    if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP)
    {
        SetLastError( ERROR_INVALID_WINDOW_HANDLE );
        return 0;
    }
    hinst = win->hInstance;
    WIN_ReleasePtr( win );

    return GetModuleFileNameA( hinst, module, size );
3369 3370 3371 3372 3373
}

/******************************************************************************
 *		GetWindowModuleFileNameW (USER32.@)
 */
3374
UINT WINAPI GetWindowModuleFileNameW( HWND hwnd, LPWSTR module, UINT size )
3375
{
3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390
    WND *win;
    HINSTANCE hinst;

    TRACE( "%p, %p, %u\n", hwnd, module, size );

    win = WIN_GetPtr( hwnd );
    if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP)
    {
        SetLastError( ERROR_INVALID_WINDOW_HANDLE );
        return 0;
    }
    hinst = win->hInstance;
    WIN_ReleasePtr( win );

    return GetModuleFileNameW( hinst, module, size );
3391
}
3392 3393 3394

/******************************************************************************
 *              GetWindowInfo (USER32.@)
3395 3396
 *
 * Note: tests show that Windows doesn't check cbSize of the structure.
3397 3398 3399 3400
 */
BOOL WINAPI GetWindowInfo( HWND hwnd, PWINDOWINFO pwi)
{
    if (!pwi) return FALSE;
3401
    if (!WIN_GetRectangles( hwnd, COORDS_SCREEN, &pwi->rcWindow, &pwi->rcClient )) return FALSE;
3402

3403 3404
    pwi->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
    pwi->dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
3405
    pwi->dwWindowStatus = ((GetActiveWindow() == hwnd) ? WS_ACTIVECAPTION : 0);
3406 3407 3408 3409 3410 3411 3412

    pwi->cxWindowBorders = pwi->rcClient.left - pwi->rcWindow.left;
    pwi->cyWindowBorders = pwi->rcWindow.bottom - pwi->rcClient.bottom;

    pwi->atomWindowType = GetClassLongW( hwnd, GCW_ATOM );
    pwi->wCreatorVersion = 0x0400;

3413 3414
    return TRUE;
}
3415 3416 3417 3418 3419 3420 3421 3422

/******************************************************************************
 *              SwitchDesktop (USER32.@)
 *
 * NOTES: Sets the current input or interactive desktop.
 */
BOOL WINAPI SwitchDesktop( HDESK hDesktop)
{
3423
    FIXME("(hwnd %p) stub!\n", hDesktop);
3424 3425
    return TRUE;
}
3426 3427 3428 3429

/*****************************************************************************
 *              SetLayeredWindowAttributes (USER32.@)
 */
3430
BOOL WINAPI SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
3431
{
3432 3433
    BOOL ret;

3434
    TRACE("(%p,%08x,%d,%x): stub!\n", hwnd, key, alpha, flags);
3435 3436 3437

    SERVER_START_REQ( set_window_layered_info )
    {
3438
        req->handle = wine_server_user_handle( hwnd );
3439 3440 3441 3442 3443 3444 3445
        req->color_key = key;
        req->alpha = alpha;
        req->flags = flags;
        ret = !wine_server_call_err( req );
    }
    SERVER_END_REQ;

3446 3447
    if (ret) USER_Driver->pSetLayeredWindowAttributes( hwnd, key, alpha, flags );

3448
    return ret;
3449
}
3450

3451

3452 3453 3454
/*****************************************************************************
 *              GetLayeredWindowAttributes (USER32.@)
 */
3455
BOOL WINAPI GetLayeredWindowAttributes( HWND hwnd, COLORREF *key, BYTE *alpha, DWORD *flags )
3456
{
3457 3458 3459 3460
    BOOL ret;

    SERVER_START_REQ( get_window_layered_info )
    {
3461
        req->handle = wine_server_user_handle( hwnd );
3462 3463 3464 3465 3466 3467 3468 3469 3470 3471
        if ((ret = !wine_server_call_err( req )))
        {
            if (key) *key = reply->color_key;
            if (alpha) *alpha = reply->alpha;
            if (flags) *flags = reply->flags;
        }
    }
    SERVER_END_REQ;

    return ret;
3472 3473
}

3474 3475 3476 3477 3478 3479 3480 3481

/*****************************************************************************
 *              UpdateLayeredWindowIndirect  (USER32.@)
 */
BOOL WINAPI UpdateLayeredWindowIndirect( HWND hwnd, const UPDATELAYEREDWINDOWINFO *info )
{
    BYTE alpha = 0xff;

3482 3483 3484 3485 3486 3487 3488
    if (!(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED) ||
        GetLayeredWindowAttributes( hwnd, NULL, NULL, NULL ))
    {
        SetLastError( ERROR_INVALID_PARAMETER );
        return FALSE;
    }

3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505
    if (!(info->dwFlags & ULW_EX_NORESIZE) && (info->pptDst || info->psize))
    {
        int x = 0, y = 0, cx = 0, cy = 0;
        DWORD flags = SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW | SWP_NOSENDCHANGING;

        if (info->pptDst)
        {
            x = info->pptDst->x;
            y = info->pptDst->y;
            flags &= ~SWP_NOMOVE;
        }
        if (info->psize)
        {
            cx = info->psize->cx;
            cy = info->psize->cy;
            flags &= ~SWP_NOSIZE;
        }
3506
        TRACE( "moving window %p pos %d,%d %dx%d\n", hwnd, x, y, cx, cy );
3507 3508 3509 3510 3511
        SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
    }

    if (info->hdcSrc)
    {
3512
        HDC hdc = GetWindowDC( hwnd );
3513 3514 3515 3516

        if (hdc)
        {
            int x = 0, y = 0;
3517
            RECT rect;
3518

3519 3520
            GetWindowRect( hwnd, &rect );
            OffsetRect( &rect, -rect.left, -rect.top);
3521 3522 3523 3524 3525
            if (info->pptSrc)
            {
                x = info->pptSrc->x;
                y = info->pptSrc->y;
            }
3526 3527 3528 3529 3530 3531 3532

            if (!info->prcDirty || (info->prcDirty && IntersectRect(&rect, &rect, info->prcDirty)))
            {
                TRACE( "copying window %p pos %d,%d\n", hwnd, x, y );
                BitBlt( hdc, rect.left, rect.top, rect.right, rect.bottom,
                        info->hdcSrc, rect.left + x, rect.top + y, SRCCOPY );
            }
3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544
            ReleaseDC( hwnd, hdc );
        }
    }

    if (info->pblend && !(info->dwFlags & ULW_OPAQUE)) alpha = info->pblend->SourceConstantAlpha;
    TRACE( "setting window %p alpha %u\n", hwnd, alpha );
    USER_Driver->pSetLayeredWindowAttributes( hwnd, info->crKey, alpha,
                                              info->dwFlags & (LWA_ALPHA | LWA_COLORKEY) );
    return TRUE;
}


3545 3546 3547 3548 3549 3550 3551
/*****************************************************************************
 *              UpdateLayeredWindow (USER32.@)
 */
BOOL WINAPI UpdateLayeredWindow( HWND hwnd, HDC hdcDst, POINT *pptDst, SIZE *psize,
                                 HDC hdcSrc, POINT *pptSrc, COLORREF crKey, BLENDFUNCTION *pblend,
                                 DWORD dwFlags)
{
3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564
    UPDATELAYEREDWINDOWINFO info;

    info.cbSize   = sizeof(info);
    info.hdcDst   = hdcDst;
    info.pptDst   = pptDst;
    info.psize    = psize;
    info.hdcSrc   = hdcSrc;
    info.pptSrc   = pptSrc;
    info.crKey    = crKey;
    info.pblend   = pblend;
    info.dwFlags  = dwFlags;
    info.prcDirty = NULL;
    return UpdateLayeredWindowIndirect( hwnd, &info );
3565
}
3566

3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579

/******************************************************************************
 *                    GetProcessDefaultLayout [USER32.@]
 *
 * Gets the default layout for parentless windows.
 */
BOOL WINAPI GetProcessDefaultLayout( DWORD *layout )
{
    if (!layout)
    {
        SetLastError( ERROR_NOACCESS );
        return FALSE;
    }
3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614
    if (process_layout == ~0u)
    {
        static const WCHAR translationW[] = { '\\','V','a','r','F','i','l','e','I','n','f','o',
                                              '\\','T','r','a','n','s','l','a','t','i','o','n', 0 };
        static const WCHAR filedescW[] = { '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
                                           '\\','%','0','4','x','%','0','4','x',
                                           '\\','F','i','l','e','D','e','s','c','r','i','p','t','i','o','n',0 };
        WCHAR *str, buffer[MAX_PATH];
        DWORD i, len, version_layout = 0;
        DWORD user_lang = GetUserDefaultLangID();
        DWORD *languages;
        void *data = NULL;

        GetModuleFileNameW( 0, buffer, MAX_PATH );
        if (!(len = GetFileVersionInfoSizeW( buffer, NULL ))) goto done;
        if (!(data = HeapAlloc( GetProcessHeap(), 0, len ))) goto done;
        if (!GetFileVersionInfoW( buffer, 0, len, data )) goto done;
        if (!VerQueryValueW( data, translationW, (void **)&languages, &len ) || !len) goto done;

        len /= sizeof(DWORD);
        for (i = 0; i < len; i++) if (LOWORD(languages[i]) == user_lang) break;
        if (i == len)  /* try neutral language */
            for (i = 0; i < len; i++)
                if (LOWORD(languages[i]) == MAKELANGID( PRIMARYLANGID(user_lang), SUBLANG_NEUTRAL )) break;
        if (i == len) i = 0;  /* default to the first one */

        sprintfW( buffer, filedescW, LOWORD(languages[i]), HIWORD(languages[i]) );
        if (!VerQueryValueW( data, buffer, (void **)&str, &len )) goto done;
        TRACE( "found description %s\n", debugstr_w( str ));
        if (str[0] == 0x200e && str[1] == 0x200e) version_layout = LAYOUT_RTL;

    done:
        HeapFree( GetProcessHeap(), 0, data );
        process_layout = version_layout;
    }
3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631
    *layout = process_layout;
    return TRUE;
}


/******************************************************************************
 *                    SetProcessDefaultLayout [USER32.@]
 *
 * Sets the default layout for parentless windows.
 */
BOOL WINAPI SetProcessDefaultLayout( DWORD layout )
{
    process_layout = layout;
    return TRUE;
}


3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654
/* 64bit versions */

#ifdef GetWindowLongPtrW
#undef GetWindowLongPtrW
#endif

#ifdef GetWindowLongPtrA
#undef GetWindowLongPtrA
#endif

#ifdef SetWindowLongPtrW
#undef SetWindowLongPtrW
#endif

#ifdef SetWindowLongPtrA
#undef SetWindowLongPtrA
#endif

/*****************************************************************************
 *              GetWindowLongPtrW (USER32.@)
 */
LONG_PTR WINAPI GetWindowLongPtrW( HWND hwnd, INT offset )
{
3655
    return WIN_GetWindowLong( hwnd, offset, sizeof(LONG_PTR), TRUE );
3656 3657 3658 3659 3660 3661 3662
}

/*****************************************************************************
 *              GetWindowLongPtrA (USER32.@)
 */
LONG_PTR WINAPI GetWindowLongPtrA( HWND hwnd, INT offset )
{
3663
    return WIN_GetWindowLong( hwnd, offset, sizeof(LONG_PTR), FALSE );
3664 3665 3666 3667 3668 3669 3670
}

/*****************************************************************************
 *              SetWindowLongPtrW (USER32.@)
 */
LONG_PTR WINAPI SetWindowLongPtrW( HWND hwnd, INT offset, LONG_PTR newval )
{
3671
    return WIN_SetWindowLong( hwnd, offset, sizeof(LONG_PTR), newval, TRUE );
3672 3673 3674 3675 3676 3677 3678
}

/*****************************************************************************
 *              SetWindowLongPtrA (USER32.@)
 */
LONG_PTR WINAPI SetWindowLongPtrA( HWND hwnd, INT offset, LONG_PTR newval )
{
3679
    return WIN_SetWindowLong( hwnd, offset, sizeof(LONG_PTR), newval, FALSE );
3680
}