window.c 84.4 KB
Newer Older
1 2 3 4 5 6
/*
 * Window related functions
 *
 * Copyright 1993, 1994, 1995, 1996, 2001 Alexandre Julliard
 * Copyright 1993 David Metcalfe
 * Copyright 1995, 1996 Alex Korobka
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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
20
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 22 23 24
 */

#include "config.h"

25
#include <stdarg.h>
26
#include <stdlib.h>
27
#include <stdio.h>
28 29 30
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
31

32
#include <X11/Xlib.h>
33 34
#include <X11/Xresource.h>
#include <X11/Xutil.h>
35 36 37
#ifdef HAVE_LIBXSHAPE
#include <X11/extensions/shape.h>
#endif /* HAVE_LIBXSHAPE */
38

39
#include "windef.h"
40 41 42
#include "winbase.h"
#include "wingdi.h"
#include "winuser.h"
43
#include "wine/unicode.h"
44 45

#include "x11drv.h"
46 47
#include "wine/debug.h"
#include "wine/server.h"
48
#include "mwm.h"
49

50
WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
51

52 53 54 55 56 57 58 59 60 61 62 63
#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT      0
#define _NET_WM_MOVERESIZE_SIZE_TOP          1
#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT     2
#define _NET_WM_MOVERESIZE_SIZE_RIGHT        3
#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT  4
#define _NET_WM_MOVERESIZE_SIZE_BOTTOM       5
#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT   6
#define _NET_WM_MOVERESIZE_SIZE_LEFT         7
#define _NET_WM_MOVERESIZE_MOVE              8
#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD     9
#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD    10

64 65 66 67 68 69
#define _NET_WM_STATE_REMOVE  0
#define _NET_WM_STATE_ADD     1
#define _NET_WM_STATE_TOGGLE  2

#define SWP_AGG_NOPOSCHANGE (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)

70
/* is cursor clipping active? */
71
int clipping_cursor = 0;
72

73 74 75
/* X context to associate a hwnd to an X window */
XContext winContext = 0;

76
/* X context to associate a struct x11drv_win_data to an hwnd */
77
XContext win_data_context = 0;
78

79 80 81 82
/* time of last user event and window where it's stored */
static Time last_user_time;
static Window user_time_window;

83
static const char foreign_window_prop[] = "__wine_x11_foreign_window";
84
static const char whole_window_prop[] = "__wine_x11_whole_window";
85
static const char clip_window_prop[]  = "__wine_x11_clip_window";
86
static const char managed_prop[]      = "__wine_x11_managed";
87

88 89 90 91 92 93 94 95 96
static CRITICAL_SECTION win_data_section;
static CRITICAL_SECTION_DEBUG critsect_debug =
{
    0, 0, &win_data_section,
    { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
      0, 0, { (DWORD_PTR)(__FILE__ ": win_data_section") }
};
static CRITICAL_SECTION win_data_section = { &critsect_debug, -1, 0, 0, 0, 0 };

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

/***********************************************************************
 * http://standards.freedesktop.org/startup-notification-spec
 */
static void remove_startup_notification(Display *display, Window window)
{
    static LONG startup_notification_removed = 0;
    char id[1024];
    char message[1024];
    int i;
    int pos;
    XEvent xevent;
    const char *src;
    int srclen;

    if (InterlockedCompareExchange(&startup_notification_removed, 1, 0) != 0)
        return;

    if (GetEnvironmentVariableA("DESKTOP_STARTUP_ID", id, sizeof(id)) == 0)
        return;
    SetEnvironmentVariableA("DESKTOP_STARTUP_ID", NULL);

119 120
    if ((src = strstr( id, "_TIME" ))) update_user_time( atol( src + 5 ));

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 149 150 151 152 153 154 155 156
    pos = snprintf(message, sizeof(message), "remove: ID=");
    message[pos++] = '"';
    for (i = 0; id[i] && pos < sizeof(message) - 2; i++)
    {
        if (id[i] == '"' || id[i] == '\\')
            message[pos++] = '\\';
        message[pos++] = id[i];
    }
    message[pos++] = '"';
    message[pos++] = '\0';

    xevent.xclient.type = ClientMessage;
    xevent.xclient.message_type = x11drv_atom(_NET_STARTUP_INFO_BEGIN);
    xevent.xclient.display = display;
    xevent.xclient.window = window;
    xevent.xclient.format = 8;

    src = message;
    srclen = strlen(src) + 1;

    while (srclen > 0)
    {
        int msglen = srclen;
        if (msglen > 20)
            msglen = 20;
        memset(&xevent.xclient.data.b[0], 0, 20);
        memcpy(&xevent.xclient.data.b[0], src, msglen);
        src += msglen;
        srclen -= msglen;

        XSendEvent( display, DefaultRootWindow( display ), False, PropertyChangeMask, &xevent );
        xevent.xclient.message_type = x11drv_atom(_NET_STARTUP_INFO);
    }
}


157 158 159 160 161 162
struct has_popup_result
{
    HWND hwnd;
    BOOL found;
};

163 164 165 166 167 168 169 170
static BOOL is_managed( HWND hwnd )
{
    struct x11drv_win_data *data = get_win_data( hwnd );
    BOOL ret = data && data->managed;
    release_win_data( data );
    return ret;
}

171
static BOOL CALLBACK has_managed_popup( HWND hwnd, LPARAM lparam )
172 173 174 175
{
    struct has_popup_result *result = (struct has_popup_result *)lparam;

    if (hwnd == result->hwnd) return FALSE;  /* popups are always above owner */
176
    if (GetWindow( hwnd, GW_OWNER ) != result->hwnd) return TRUE;
177
    result->found = is_managed( hwnd );
178 179 180 181 182 183 184 185 186
    return !result->found;
}

static BOOL has_owned_popups( HWND hwnd )
{
    struct has_popup_result result;

    result.hwnd = hwnd;
    result.found = FALSE;
187
    EnumWindows( has_managed_popup, (LPARAM)&result );
188 189 190
    return result.found;
}

191
/***********************************************************************
192
 *		is_window_managed
193
 *
194
 * Check if a given window should be managed
195
 */
196
static BOOL is_window_managed( HWND hwnd, UINT swp_flags, const RECT *window_rect )
197
{
198 199
    DWORD style, ex_style;

200 201
    if (!managed_mode) return FALSE;

202
    /* child windows are not managed */
203
    style = GetWindowLongW( hwnd, GWL_STYLE );
204
    if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD) return FALSE;
205 206 207
    /* activated windows are managed */
    if (!(swp_flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) return TRUE;
    if (hwnd == GetActiveWindow()) return TRUE;
208
    /* windows with caption are managed */
209
    if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
210
    /* windows with thick frame are managed */
211 212
    if (style & WS_THICKFRAME) return TRUE;
    if (style & WS_POPUP)
213
    {
214 215 216
        HMONITOR hmon;
        MONITORINFO mi;

217 218 219
        /* popup with sysmenu == caption are managed */
        if (style & WS_SYSMENU) return TRUE;
        /* full-screen popup windows are managed */
220 221 222 223 224
        hmon = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY );
        mi.cbSize = sizeof( mi );
        GetMonitorInfoW( hmon, &mi );
        if (window_rect->left <= mi.rcWork.left && window_rect->right >= mi.rcWork.right &&
            window_rect->top <= mi.rcWork.top && window_rect->bottom >= mi.rcWork.bottom)
225
            return TRUE;
226
    }
227 228 229
    /* application windows are managed */
    ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
    if (ex_style & WS_EX_APPWINDOW) return TRUE;
230 231
    /* windows that own popups are managed */
    if (has_owned_popups( hwnd )) return TRUE;
232 233
    /* default: not managed */
    return FALSE;
234 235 236
}


237 238 239 240 241 242 243 244 245
/***********************************************************************
 *		is_window_resizable
 *
 * Check if window should be made resizable by the window manager
 */
static inline BOOL is_window_resizable( struct x11drv_win_data *data, DWORD style )
{
    if (style & WS_THICKFRAME) return TRUE;
    /* Metacity needs the window to be resizable to make it fullscreen */
246
    return is_window_rect_fullscreen( &data->whole_rect );
247 248 249
}


250 251 252
/***********************************************************************
 *              get_mwm_decorations
 */
253 254
static unsigned long get_mwm_decorations( struct x11drv_win_data *data,
                                          DWORD style, DWORD ex_style )
255 256 257
{
    unsigned long ret = 0;

258 259 260
    if (!decorated_mode) return 0;

    if (IsRectEmpty( &data->window_rect )) return 0;
261
    if (data->shaped) return 0;
262

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
    if (ex_style & WS_EX_TOOLWINDOW) return 0;

    if ((style & WS_CAPTION) == WS_CAPTION)
    {
        ret |= MWM_DECOR_TITLE | MWM_DECOR_BORDER;
        if (style & WS_SYSMENU) ret |= MWM_DECOR_MENU;
        if (style & WS_MINIMIZEBOX) ret |= MWM_DECOR_MINIMIZE;
        if (style & WS_MAXIMIZEBOX) ret |= MWM_DECOR_MAXIMIZE;
    }
    if (ex_style & WS_EX_DLGMODALFRAME) ret |= MWM_DECOR_BORDER;
    else if (style & WS_THICKFRAME) ret |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
    else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) ret |= MWM_DECOR_BORDER;
    return ret;
}


/***********************************************************************
 *		get_x11_rect_offset
 *
 * Helper for X11DRV_window_to_X_rect and X11DRV_X_to_window_rect.
 */
static void get_x11_rect_offset( struct x11drv_win_data *data, RECT *rect )
{
    DWORD style, ex_style, style_mask = 0, ex_style_mask = 0;
    unsigned long decor;

    rect->top = rect->bottom = rect->left = rect->right = 0;

    style = GetWindowLongW( data->hwnd, GWL_STYLE );
    ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
293
    decor = get_mwm_decorations( data, style, ex_style );
294 295 296 297 298 299 300 301 302 303 304 305

    if (decor & MWM_DECOR_TITLE) style_mask |= WS_CAPTION;
    if (decor & MWM_DECOR_BORDER)
    {
        style_mask |= WS_DLGFRAME | WS_THICKFRAME;
        ex_style_mask |= WS_EX_DLGMODALFRAME;
    }

    AdjustWindowRectEx( rect, style & style_mask, FALSE, ex_style & ex_style_mask );
}


306 307 308
/***********************************************************************
 *              get_window_attributes
 *
309
 * Fill the window attributes structure for an X window.
310
 */
311
static int get_window_attributes( struct x11drv_win_data *data, XSetWindowAttributes *attr )
312
{
313
    attr->override_redirect = !data->managed;
314
    attr->colormap          = default_colormap;
315
    attr->save_under        = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
316
    attr->bit_gravity       = NorthWestGravity;
317
    attr->win_gravity       = StaticGravity;
318
    attr->backing_store     = NotUseful;
319
    attr->border_pixel      = 0;
320 321
    attr->event_mask        = (ExposureMask | PointerMotionMask |
                               ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
322 323 324
                               KeyPressMask | KeyReleaseMask | FocusChangeMask |
                               KeymapStateMask | StructureNotifyMask);
    if (data->managed) attr->event_mask |= PropertyChangeMask;
325

326
    return (CWOverrideRedirect | CWSaveUnder | CWColormap | CWBorderPixel |
327
            CWEventMask | CWBitGravity | CWBackingStore);
328 329 330 331
}


/***********************************************************************
332
 *              sync_window_style
333 334 335
 *
 * Change the X window attributes when the window style has changed.
 */
336
static void sync_window_style( struct x11drv_win_data *data )
337
{
338 339 340
    if (data->whole_window != root_window)
    {
        XSetWindowAttributes attr;
341
        int mask = get_window_attributes( data, &attr );
342

343
        XChangeWindowAttributes( data->display, data->whole_window, mask, &attr );
344
    }
345
}
346

347

348 349 350 351 352
/***********************************************************************
 *              sync_window_region
 *
 * Update the X11 window region.
 */
353
static void sync_window_region( struct x11drv_win_data *data, HRGN win_region )
354 355
{
#ifdef HAVE_LIBXSHAPE
356 357
    HRGN hrgn = win_region;

358
    if (!data->whole_window) return;
359
    data->shaped = FALSE;
360

361 362 363
    if (IsRectEmpty( &data->window_rect ))  /* set an empty shape */
    {
        static XRectangle empty_rect;
364
        XShapeCombineRectangles( data->display, data->whole_window, ShapeBounding, 0, 0,
365 366 367 368
                                 &empty_rect, 1, ShapeSet, YXBanded );
        return;
    }

369 370 371 372 373 374 375 376 377 378
    if (hrgn == (HRGN)1)  /* hack: win_region == 1 means retrieve region from server */
    {
        if (!(hrgn = CreateRectRgn( 0, 0, 0, 0 ))) return;
        if (GetWindowRgn( data->hwnd, hrgn ) == ERROR)
        {
            DeleteObject( hrgn );
            hrgn = 0;
        }
    }

379 380
    if (!hrgn)
    {
381
        XShapeCombineMask( data->display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
382 383 384
    }
    else
    {
385 386 387 388
        RGNDATA *pRegionData;

        if (GetWindowLongW( data->hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) MirrorRgn( data->hwnd, hrgn );
        if ((pRegionData = X11DRV_GetRegionData( hrgn, 0 )))
389
        {
390
            XShapeCombineRectangles( data->display, data->whole_window, ShapeBounding,
391 392 393 394 395
                                     data->window_rect.left - data->whole_rect.left,
                                     data->window_rect.top - data->whole_rect.top,
                                     (XRectangle *)pRegionData->Buffer,
                                     pRegionData->rdh.nCount, ShapeSet, YXBanded );
            HeapFree(GetProcessHeap(), 0, pRegionData);
396
            data->shaped = TRUE;
397 398
        }
    }
399
    if (hrgn && hrgn != win_region) DeleteObject( hrgn );
400 401 402 403
#endif  /* HAVE_LIBXSHAPE */
}


404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
/***********************************************************************
 *              sync_window_opacity
 */
static void sync_window_opacity( Display *display, Window win,
                                 COLORREF key, BYTE alpha, DWORD flags )
{
    unsigned long opacity = 0xffffffff;

    if (flags & LWA_ALPHA) opacity = (0xffffffff / 0xff) * alpha;

    if (opacity == 0xffffffff)
        XDeleteProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY) );
    else
        XChangeProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY),
                         XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&opacity, 1 );
}


422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
/***********************************************************************
 *              sync_window_text
 */
static void sync_window_text( Display *display, Window win, const WCHAR *text )
{
    UINT count;
    char *buffer, *utf8_buffer;
    XTextProperty prop;

    /* allocate new buffer for window text */
    count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
    if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count ))) return;
    WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);

    count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
    if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
    {
        HeapFree( GetProcessHeap(), 0, buffer );
        return;
    }
    WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);

    if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
    {
        XSetWMName( display, win, &prop );
        XSetWMIconName( display, win, &prop );
        XFree( prop.value );
    }
    /*
      Implements a NET_WM UTF-8 title. It should be without a trailing \0,
      according to the standard
      ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
    */
    XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
                     8, PropModeReplace, (unsigned char *) utf8_buffer, count);

    HeapFree( GetProcessHeap(), 0, utf8_buffer );
    HeapFree( GetProcessHeap(), 0, buffer );
}


463 464 465 466 467 468 469
/***********************************************************************
 *              get_bitmap_argb
 *
 * Return the bitmap bits in ARGB format. Helper for setting icon hints.
 */
static unsigned long *get_bitmap_argb( HDC hdc, HBITMAP color, HBITMAP mask, unsigned int *size )
{
470 471
    char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
    BITMAPINFO *info = (BITMAPINFO *)buffer;
472
    BITMAP bm;
473 474 475
    unsigned int *ptr, *bits = NULL;
    unsigned char *mask_bits = NULL;
    int i, j, has_alpha = 0;
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495

    if (!GetObjectW( color, sizeof(bm), &bm )) return NULL;
    info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    info->bmiHeader.biWidth = bm.bmWidth;
    info->bmiHeader.biHeight = -bm.bmHeight;
    info->bmiHeader.biPlanes = 1;
    info->bmiHeader.biBitCount = 32;
    info->bmiHeader.biCompression = BI_RGB;
    info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
    info->bmiHeader.biXPelsPerMeter = 0;
    info->bmiHeader.biYPelsPerMeter = 0;
    info->bmiHeader.biClrUsed = 0;
    info->bmiHeader.biClrImportant = 0;
    *size = bm.bmWidth * bm.bmHeight + 2;
    if (!(bits = HeapAlloc( GetProcessHeap(), 0, *size * sizeof(long) ))) goto failed;
    if (!GetDIBits( hdc, color, 0, bm.bmHeight, bits + 2, info, DIB_RGB_COLORS )) goto failed;

    bits[0] = bm.bmWidth;
    bits[1] = bm.bmHeight;

496 497 498 499 500 501 502 503 504 505 506 507 508 509
    for (i = 0; i < bm.bmWidth * bm.bmHeight; i++)
        if ((has_alpha = (bits[i + 2] & 0xff000000) != 0)) break;

    if (!has_alpha)
    {
        unsigned int width_bytes = (bm.bmWidth + 31) / 32 * 4;
        /* generate alpha channel from the mask */
        info->bmiHeader.biBitCount = 1;
        info->bmiHeader.biSizeImage = width_bytes * bm.bmHeight;
        if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto failed;
        if (!GetDIBits( hdc, mask, 0, bm.bmHeight, mask_bits, info, DIB_RGB_COLORS )) goto failed;
        ptr = bits + 2;
        for (i = 0; i < bm.bmHeight; i++)
            for (j = 0; j < bm.bmWidth; j++, ptr++)
510
                if (!((mask_bits[i * width_bytes + j / 8] << (j % 8)) & 0x80)) *ptr |= 0xff000000;
511
        HeapFree( GetProcessHeap(), 0, mask_bits );
512 513
    }

514 515 516 517 518 519 520 521
    /* convert to array of longs */
    if (bits && sizeof(long) > sizeof(int))
        for (i = *size - 1; i >= 0; i--) ((unsigned long *)bits)[i] = bits[i];

    return (unsigned long *)bits;

failed:
    HeapFree( GetProcessHeap(), 0, bits );
522
    HeapFree( GetProcessHeap(), 0, mask_bits );
523 524 525 526
    return NULL;
}


527 528 529
/***********************************************************************
 *              create_icon_pixmaps
 */
530
static BOOL create_icon_pixmaps( HDC hdc, const ICONINFO *icon, Pixmap *icon_ret, Pixmap *mask_ret )
531 532 533
{
    char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
    BITMAPINFO *info = (BITMAPINFO *)buffer;
534
    XVisualInfo vis = default_visual;
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
    struct gdi_image_bits bits;
    Pixmap color_pixmap = 0, mask_pixmap = 0;
    int i, lines;

    bits.ptr = NULL;
    bits.free = NULL;
    bits.is_copy = TRUE;

    info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    info->bmiHeader.biBitCount = 0;
    if (!(lines = GetDIBits( hdc, icon->hbmColor, 0, 0, NULL, info, DIB_RGB_COLORS ))) goto failed;
    if (!(bits.ptr = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto failed;
    if (!GetDIBits( hdc, icon->hbmColor, 0, lines, bits.ptr, info, DIB_RGB_COLORS )) goto failed;

    color_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
    HeapFree( GetProcessHeap(), 0, bits.ptr );
    bits.ptr = NULL;
    if (!color_pixmap) goto failed;

    info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    info->bmiHeader.biBitCount = 0;
    if (!(lines = GetDIBits( hdc, icon->hbmMask, 0, 0, NULL, info, DIB_RGB_COLORS ))) goto failed;
    if (!(bits.ptr = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto failed;
    if (!GetDIBits( hdc, icon->hbmMask, 0, lines, bits.ptr, info, DIB_RGB_COLORS )) goto failed;

    /* invert the mask */
    for (i = 0; i < info->bmiHeader.biSizeImage / sizeof(DWORD); i++) ((DWORD *)bits.ptr)[i] ^= ~0u;

    vis.depth = 1;
    mask_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
    HeapFree( GetProcessHeap(), 0, bits.ptr );
    bits.ptr = NULL;
    if (!mask_pixmap) goto failed;

569 570
    *icon_ret = color_pixmap;
    *mask_ret = mask_pixmap;
571 572 573 574 575 576 577 578 579 580
    return TRUE;

failed:
    if (color_pixmap) XFreePixmap( gdi_display, color_pixmap );
    if (mask_pixmap) XFreePixmap( gdi_display, mask_pixmap );
    HeapFree( GetProcessHeap(), 0, bits.ptr );
    return FALSE;
}


581
/***********************************************************************
582
 *              fetch_icon_data
583
 */
584
static void fetch_icon_data( HWND hwnd, HICON icon_big, HICON icon_small )
585
{
586
    struct x11drv_win_data *data;
587 588 589 590
    ICONINFO ii, ii_small;
    HDC hDC;
    unsigned int size;
    unsigned long *bits;
591
    Pixmap icon_pixmap, mask_pixmap;
592

593 594
    if (!icon_big)
    {
595 596
        icon_big = (HICON)SendMessageW( hwnd, WM_GETICON, ICON_BIG, 0 );
        if (!icon_big) icon_big = (HICON)GetClassLongPtrW( hwnd, GCLP_HICON );
597
        if (!icon_big) icon_big = LoadIconW( 0, (LPWSTR)IDI_WINLOGO );
598 599 600
    }
    if (!icon_small)
    {
601 602
        icon_small = (HICON)SendMessageW( hwnd, WM_GETICON, ICON_SMALL, 0 );
        if (!icon_small) icon_small = (HICON)GetClassLongPtrW( hwnd, GCLP_HICONSM );
603 604
    }

605
    if (!GetIconInfo(icon_big, &ii)) return;
606

607 608 609 610 611 612
    hDC = CreateCompatibleDC(0);
    bits = get_bitmap_argb( hDC, ii.hbmColor, ii.hbmMask, &size );
    if (bits && GetIconInfo( icon_small, &ii_small ))
    {
        unsigned int size_small;
        unsigned long *bits_small, *new;
613

614 615
        if ((bits_small = get_bitmap_argb( hDC, ii_small.hbmColor, ii_small.hbmMask, &size_small )) &&
            (bits_small[0] != bits[0] || bits_small[1] != bits[1]))  /* size must be different */
616
        {
617 618
            if ((new = HeapReAlloc( GetProcessHeap(), 0, bits,
                                    (size + size_small) * sizeof(unsigned long) )))
619
            {
620 621 622
                bits = new;
                memcpy( bits + size, bits_small, size_small * sizeof(unsigned long) );
                size += size_small;
623 624
            }
        }
625 626 627 628
        HeapFree( GetProcessHeap(), 0, bits_small );
        DeleteObject( ii_small.hbmColor );
        DeleteObject( ii_small.hbmMask );
    }
629

630 631
    if (!create_icon_pixmaps( hDC, &ii, &icon_pixmap, &mask_pixmap )) icon_pixmap = mask_pixmap = 0;

632 633 634
    DeleteObject( ii.hbmColor );
    DeleteObject( ii.hbmMask );
    DeleteDC(hDC);
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652

    if ((data = get_win_data( hwnd )))
    {
        if (data->icon_pixmap) XFreePixmap( gdi_display, data->icon_pixmap );
        if (data->icon_mask) XFreePixmap( gdi_display, data->icon_mask );
        HeapFree( GetProcessHeap(), 0, data->icon_bits );
        data->icon_pixmap = icon_pixmap;
        data->icon_mask = mask_pixmap;
        data->icon_bits = bits;
        data->icon_size = size;
        release_win_data( data );
    }
    else
    {
        if (icon_pixmap) XFreePixmap( gdi_display, icon_pixmap );
        if (mask_pixmap) XFreePixmap( gdi_display, mask_pixmap );
        HeapFree( GetProcessHeap(), 0, bits );
    }
653 654 655 656
}


/***********************************************************************
657
 *              set_size_hints
658
 *
659
 * set the window size hints
660
 */
661
static void set_size_hints( struct x11drv_win_data *data, DWORD style )
662
{
663 664
    XSizeHints* size_hints;

665
    if (!(size_hints = XAllocSizeHints())) return;
666

667 668 669
    size_hints->win_gravity = StaticGravity;
    size_hints->flags |= PWinGravity;

670 671 672
    /* don't update size hints if window is not in normal state */
    if (!(style & (WS_MINIMIZE | WS_MAXIMIZE)))
    {
673 674 675 676
        if (data->hwnd != GetDesktopWindow())  /* don't force position of desktop */
        {
            size_hints->x = data->whole_rect.left;
            size_hints->y = data->whole_rect.top;
677
            size_hints->flags |= PPosition;
678
        }
679
        else size_hints->win_gravity = NorthWestGravity;
680

681
        if (!is_window_resizable( data, style ))
682
        {
683 684
            size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
            size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
685 686
            if (size_hints->max_width <= 0 ||size_hints->max_height <= 0)
                size_hints->max_width = size_hints->max_height = 1;
687 688 689
            size_hints->min_width = size_hints->max_width;
            size_hints->min_height = size_hints->max_height;
            size_hints->flags |= PMinSize | PMaxSize;
690 691
        }
    }
692
    XSetWMNormalHints( data->display, data->whole_window, size_hints );
693
    XFree( size_hints );
694 695 696
}


697 698 699
/***********************************************************************
 *              set_mwm_hints
 */
700
static void set_mwm_hints( struct x11drv_win_data *data, DWORD style, DWORD ex_style )
701 702 703
{
    MwmHints mwm_hints;

704 705
    if (data->hwnd == GetDesktopWindow())
    {
706 707 708
        if (is_desktop_fullscreen()) mwm_hints.decorations = 0;
        else mwm_hints.decorations = MWM_DECOR_TITLE | MWM_DECOR_BORDER | MWM_DECOR_MENU | MWM_DECOR_MINIMIZE;
        mwm_hints.functions        = MWM_FUNC_MOVE | MWM_FUNC_MINIMIZE | MWM_FUNC_CLOSE;
709 710
    }
    else
711
    {
712 713 714 715 716 717 718 719 720
        mwm_hints.decorations = get_mwm_decorations( data, style, ex_style );
        mwm_hints.functions = MWM_FUNC_MOVE;
        if (is_window_resizable( data, style )) mwm_hints.functions |= MWM_FUNC_RESIZE;
        if (!(style & WS_DISABLED))
        {
            if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
            if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
            if (style & WS_SYSMENU)     mwm_hints.functions |= MWM_FUNC_CLOSE;
        }
721
    }
722

723 724 725
    TRACE( "%p setting mwm hints to %lx,%lx (style %x exstyle %x)\n",
           data->hwnd, mwm_hints.decorations, mwm_hints.functions, style, ex_style );

726
    mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
727
    XChangeProperty( data->display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
728 729 730 731 732
                     x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
                     (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
}


733 734 735
/***********************************************************************
 *              set_style_hints
 */
736
static void set_style_hints( struct x11drv_win_data *data, DWORD style, DWORD ex_style )
737 738 739 740
{
    Window group_leader = data->whole_window;
    HWND owner = GetWindow( data->hwnd, GW_OWNER );
    Window owner_win = X11DRV_get_whole_window( owner );
741
    XWMHints *wm_hints;
742 743 744 745
    Atom window_type;

    if (owner_win)
    {
746
        XSetTransientForHint( data->display, data->whole_window, owner_win );
747 748 749 750 751 752 753 754 755 756 757 758
        group_leader = owner_win;
    }

    /* Only use dialog type for owned popups. Metacity allows making fullscreen
     * only normal windows, and doesn't handle correctly TRANSIENT_FOR hint for
     * dialogs owned by fullscreen windows.
     */
    if (((style & WS_POPUP) || (ex_style & WS_EX_DLGMODALFRAME)) && owner)
        window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
    else
        window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);

759
    XChangeProperty(data->display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
760 761
		    XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);

762
    if ((wm_hints = XAllocWMHints()))
763
    {
764 765 766 767 768 769 770 771 772 773
        wm_hints->flags = InputHint | StateHint | WindowGroupHint;
        wm_hints->input = !use_take_focus && !(style & WS_DISABLED);
        wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
        wm_hints->window_group = group_leader;
        if (data->icon_pixmap)
        {
            wm_hints->icon_pixmap = data->icon_pixmap;
            wm_hints->icon_mask = data->icon_mask;
            wm_hints->flags |= IconPixmapHint | IconMaskHint;
        }
774
        XSetWMHints( data->display, data->whole_window, wm_hints );
775
        XFree( wm_hints );
776
    }
777 778

    if (data->icon_bits)
779
        XChangeProperty( data->display, data->whole_window, x11drv_atom(_NET_WM_ICON),
780 781 782
                         XA_CARDINAL, 32, PropModeReplace,
                         (unsigned char *)data->icon_bits, data->icon_size );
    else
783
        XDeleteProperty( data->display, data->whole_window, x11drv_atom(_NET_WM_ICON) );
784

785 786 787
}


788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
/***********************************************************************
 *              get_process_name
 *
 * get the name of the current process for setting class hints
 */
static char *get_process_name(void)
{
    static char *name;

    if (!name)
    {
        WCHAR module[MAX_PATH];
        DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
        if (len && len < MAX_PATH)
        {
            char *ptr;
            WCHAR *p, *appname = module;

            if ((p = strrchrW( appname, '/' ))) appname = p + 1;
            if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
            len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
            if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
            {
                WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
                name = ptr;
            }
        }
    }
    return name;
}


820 821 822 823 824
/***********************************************************************
 *              set_initial_wm_hints
 *
 * Set the window manager hints that don't change over the lifetime of a window.
 */
825
static void set_initial_wm_hints( Display *display, Window window )
826
{
827
    long i;
828
    Atom protocols[3];
829
    Atom dndVersion = WINE_XDND_VERSION;
830 831 832 833 834 835 836 837
    XClassHint *class_hints;
    char *process_name = get_process_name();

    /* wm protocols */
    i = 0;
    protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
    protocols[i++] = x11drv_atom(_NET_WM_PING);
    if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
838
    XChangeProperty( display, window, x11drv_atom(WM_PROTOCOLS),
839 840 841 842 843 844 845 846 847
                     XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );

    /* class hints */
    if ((class_hints = XAllocClassHint()))
    {
        static char wine[] = "Wine";

        class_hints->res_name = process_name;
        class_hints->res_class = wine;
848
        XSetClassHint( display, window, class_hints );
849 850 851 852
        XFree( class_hints );
    }

    /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
853
    XSetWMProperties(display, window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
854 855
    /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
    i = getpid();
856
    XChangeProperty(display, window, x11drv_atom(_NET_WM_PID),
857 858
                    XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);

859
    XChangeProperty( display, window, x11drv_atom(XdndAware),
860 861
                     XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );

862 863
    update_user_time( 0 );  /* make sure that the user time window exists */
    if (user_time_window)
864
        XChangeProperty( display, window, x11drv_atom(_NET_WM_USER_TIME_WINDOW),
865
                         XA_WINDOW, 32, PropModeReplace, (unsigned char *)&user_time_window, 1 );
866 867 868
}


869
/***********************************************************************
870
 *              make_owner_managed
871
 *
872
 * If the window is managed, make sure its owner window is too.
873
 */
874
static void make_owner_managed( HWND hwnd )
875
{
876
    HWND owner;
877

878 879 880
    if (!(owner = GetWindow( hwnd, GW_OWNER ))) return;
    if (is_managed( owner )) return;
    if (!is_managed( hwnd )) return;
881

882 883 884
    SetWindowPos( owner, 0, 0, 0, 0, 0,
                  SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE |
                  SWP_NOREDRAW | SWP_DEFERERASE | SWP_NOSENDCHANGING | SWP_STATECHANGED );
885 886 887
}


888
/***********************************************************************
889
 *              set_wm_hints
890
 *
891
 * Set all the window manager hints for a window.
892
 */
893
static void set_wm_hints( struct x11drv_win_data *data )
894
{
895
    DWORD style, ex_style;
896

897
    if (data->hwnd == GetDesktopWindow())
898 899
    {
        /* force some styles for the desktop to get the correct decorations */
900 901
        style = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
        ex_style = WS_EX_APPWINDOW;
902
    }
903 904
    else
    {
905 906
        style = GetWindowLongW( data->hwnd, GWL_STYLE );
        ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
907
    }
908

909 910 911
    set_size_hints( data, style );
    set_mwm_hints( data, style, ex_style );
    set_style_hints( data, style, ex_style );
912 913 914
}


915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
/***********************************************************************
 *     init_clip_window
 */
Window init_clip_window(void)
{
    struct x11drv_thread_data *data = x11drv_init_thread_data();

    if (!data->clip_window &&
        (data->clip_window = (Window)GetPropA( GetDesktopWindow(), clip_window_prop )))
    {
        XSelectInput( data->display, data->clip_window, StructureNotifyMask );
    }
    return data->clip_window;
}


931 932 933 934 935 936 937
/***********************************************************************
 *     update_user_time
 */
void update_user_time( Time time )
{
    if (!user_time_window)
    {
938 939
        Window win = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, CopyFromParent,
                                    InputOnly, CopyFromParent, 0, NULL );
940 941
        if (InterlockedCompareExchangePointer( (void **)&user_time_window, (void *)win, 0 ))
            XDestroyWindow( gdi_display, win );
942 943
        TRACE( "user time window %lx\n", user_time_window );
    }
944 945 946 947

    if (!time) return;
    XLockDisplay( gdi_display );
    if (!last_user_time || (long)(time - last_user_time) > 0)
948 949 950 951 952
    {
        last_user_time = time;
        XChangeProperty( gdi_display, user_time_window, x11drv_atom(_NET_WM_USER_TIME),
                         XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&time, 1 );
    }
953
    XUnlockDisplay( gdi_display );
954 955
}

956 957 958
/***********************************************************************
 *     update_net_wm_states
 */
959
void update_net_wm_states( struct x11drv_win_data *data )
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
{
    static const unsigned int state_atoms[NB_NET_WM_STATES] =
    {
        XATOM__NET_WM_STATE_FULLSCREEN,
        XATOM__NET_WM_STATE_ABOVE,
        XATOM__NET_WM_STATE_MAXIMIZED_VERT,
        XATOM__NET_WM_STATE_SKIP_PAGER,
        XATOM__NET_WM_STATE_SKIP_TASKBAR
    };

    DWORD i, style, ex_style, new_state = 0;

    if (!data->managed) return;
    if (data->whole_window == root_window) return;

    style = GetWindowLongW( data->hwnd, GWL_STYLE );
976
    if (is_window_rect_fullscreen( &data->whole_rect ))
977 978 979 980 981 982 983 984 985 986 987 988
    {
        if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION)
            new_state |= (1 << NET_WM_STATE_MAXIMIZED);
        else if (!(style & WS_MINIMIZE))
            new_state |= (1 << NET_WM_STATE_FULLSCREEN);
    }
    else if (style & WS_MAXIMIZE)
        new_state |= (1 << NET_WM_STATE_MAXIMIZED);

    ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
    if (ex_style & WS_EX_TOPMOST)
        new_state |= (1 << NET_WM_STATE_ABOVE);
989
    if (ex_style & (WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE))
990
        new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR) | (1 << NET_WM_STATE_SKIP_PAGER);
991
    if (!(ex_style & WS_EX_APPWINDOW) && GetWindow( data->hwnd, GW_OWNER ))
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
        new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR);

    if (!data->mapped)  /* set the _NET_WM_STATE atom directly */
    {
        Atom atoms[NB_NET_WM_STATES+1];
        DWORD count;

        for (i = count = 0; i < NB_NET_WM_STATES; i++)
        {
            if (!(new_state & (1 << i))) continue;
            TRACE( "setting wm state %u for unmapped window %p/%lx\n",
                   i, data->hwnd, data->whole_window );
            atoms[count++] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
            if (state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT)
                atoms[count++] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
        }
1008
        XChangeProperty( data->display, data->whole_window, x11drv_atom(_NET_WM_STATE), XA_ATOM,
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
                         32, PropModeReplace, (unsigned char *)atoms, count );
    }
    else  /* ask the window manager to do it for us */
    {
        XEvent xev;

        xev.xclient.type = ClientMessage;
        xev.xclient.window = data->whole_window;
        xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
        xev.xclient.serial = 0;
1019
        xev.xclient.display = data->display;
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
        xev.xclient.send_event = True;
        xev.xclient.format = 32;
        xev.xclient.data.l[3] = 1;

        for (i = 0; i < NB_NET_WM_STATES; i++)
        {
            if (!((data->net_wm_state ^ new_state) & (1 << i))) continue;  /* unchanged */

            TRACE( "setting wm state %u for window %p/%lx to %u prev %u\n",
                   i, data->hwnd, data->whole_window,
                   (new_state & (1 << i)) != 0, (data->net_wm_state & (1 << i)) != 0 );

            xev.xclient.data.l[0] = (new_state & (1 << i)) ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
            xev.xclient.data.l[1] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
            xev.xclient.data.l[2] = ((state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT) ?
                                     x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ) : 0);
1036
            XSendEvent( data->display, root_window, False,
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
                        SubstructureRedirectMask | SubstructureNotifyMask, &xev );
        }
    }
    data->net_wm_state = new_state;
}


/***********************************************************************
 *     set_xembed_flags
 */
1047
static void set_xembed_flags( struct x11drv_win_data *data, unsigned long flags )
1048 1049 1050
{
    unsigned long info[2];

1051 1052
    if (!data->whole_window) return;

1053 1054
    info[0] = 0; /* protocol version */
    info[1] = flags;
1055
    XChangeProperty( data->display, data->whole_window, x11drv_atom(_XEMBED_INFO),
1056 1057 1058 1059 1060 1061 1062
                     x11drv_atom(_XEMBED_INFO), 32, PropModeReplace, (unsigned char*)info, 2 );
}


/***********************************************************************
 *     map_window
 */
1063
static void map_window( HWND hwnd, DWORD new_style )
1064
{
1065
    struct x11drv_win_data *data;
1066

1067
    make_owner_managed( hwnd );
1068
    wait_for_withdrawn_state( hwnd, TRUE );
1069

1070 1071 1072
    if (!(data = get_win_data( hwnd ))) return;

    if (data->whole_window && !data->mapped)
1073
    {
1074
        TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1075

1076 1077 1078 1079 1080 1081 1082 1083
        remove_startup_notification( data->display, data->whole_window );
        set_wm_hints( data );

        if (!data->embedded)
        {
            update_net_wm_states( data );
            sync_window_style( data );
            XMapWindow( data->display, data->whole_window );
1084
            XFlush( data->display );
1085 1086 1087 1088 1089 1090 1091
        }
        else set_xembed_flags( data, XEMBED_MAPPED );

        data->mapped = TRUE;
        data->iconic = (new_style & WS_MINIMIZE) != 0;
    }
    release_win_data( data );
1092 1093 1094 1095 1096 1097
}


/***********************************************************************
 *     unmap_window
 */
1098
static void unmap_window( HWND hwnd )
1099
{
1100
    struct x11drv_win_data *data;
1101

1102 1103 1104
    wait_for_withdrawn_state( hwnd, FALSE );

    if (!(data = get_win_data( hwnd ))) return;
1105

1106
    if (data->mapped)
1107
    {
1108 1109 1110 1111
        TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );

        if (data->embedded) set_xembed_flags( data, 0 );
        else if (!data->managed) XUnmapWindow( data->display, data->whole_window );
1112
        else XWithdrawWindow( data->display, data->whole_window, DefaultScreen(data->display) );
1113

1114 1115 1116 1117
        data->mapped = FALSE;
        data->net_wm_state = 0;
    }
    release_win_data( data );
1118 1119 1120 1121 1122 1123
}


/***********************************************************************
 *     make_window_embedded
 */
1124
void make_window_embedded( HWND hwnd )
1125
{
1126
    struct x11drv_win_data *data = get_win_data( hwnd );
1127

1128
    if (!data) return;
1129

1130 1131 1132 1133 1134 1135 1136
    /* the window cannot be mapped before being embedded */
    if (data->mapped)
    {
        if (data->managed) XUnmapWindow( data->display, data->whole_window );
        else XWithdrawWindow( data->display, data->whole_window, DefaultScreen(data->display) );
        data->net_wm_state = 0;
    }
1137 1138
    data->embedded = TRUE;
    data->managed = TRUE;
1139
    SetPropA( hwnd, managed_prop, (HANDLE)1 );
1140
    sync_window_style( data );
1141 1142
    set_xembed_flags( data, data->mapped ? XEMBED_MAPPED : 0 );
    release_win_data( data );
1143 1144 1145
}


1146 1147 1148 1149 1150
/***********************************************************************
 *		X11DRV_window_to_X_rect
 *
 * Convert a rect from client to X window coordinates
 */
1151
static void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
1152
{
1153 1154
    RECT rc;

1155
    if (!data->managed) return;
1156 1157
    if (IsRectEmpty( rect )) return;

1158
    get_x11_rect_offset( data, &rc );
1159

1160 1161 1162 1163
    rect->left   -= rc.left;
    rect->right  -= rc.right;
    rect->top    -= rc.top;
    rect->bottom -= rc.bottom;
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
    if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
    if (rect->left >= rect->right) rect->right = rect->left + 1;
}


/***********************************************************************
 *		X11DRV_X_to_window_rect
 *
 * Opposite of X11DRV_window_to_X_rect
 */
1174
void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
1175
{
1176
    RECT rc;
1177

1178
    if (!data->managed) return;
1179 1180
    if (IsRectEmpty( rect )) return;

1181
    get_x11_rect_offset( data, &rc );
1182

1183 1184 1185 1186
    rect->left   += rc.left;
    rect->right  += rc.right;
    rect->top    += rc.top;
    rect->bottom += rc.bottom;
1187 1188 1189 1190 1191 1192
    if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
    if (rect->left >= rect->right) rect->right = rect->left + 1;
}


/***********************************************************************
1193
 *		sync_window_position
1194
 *
1195
 * Synchronize the X window position with the Windows one
1196
 */
1197
static void sync_window_position( struct x11drv_win_data *data,
1198 1199
                                  UINT swp_flags, const RECT *old_window_rect,
                                  const RECT *old_whole_rect, const RECT *old_client_rect )
1200
{
1201
    DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
1202
    DWORD ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
1203
    XWindowChanges changes;
1204
    unsigned int mask = 0;
1205 1206 1207

    if (data->managed && data->iconic) return;

1208 1209 1210
    /* resizing a managed maximized window is not allowed */
    if (!(style & WS_MAXIMIZE) || !data->managed)
    {
1211 1212 1213 1214
        changes.width = data->whole_rect.right - data->whole_rect.left;
        changes.height = data->whole_rect.bottom - data->whole_rect.top;
        /* if window rect is empty force size to 1x1 */
        if (changes.width <= 0 || changes.height <= 0) changes.width = changes.height = 1;
1215 1216
        if (changes.width > 65535) changes.width = 65535;
        if (changes.height > 65535) changes.height = 65535;
1217 1218
        mask |= CWWidth | CWHeight;
    }
1219 1220 1221 1222 1223 1224 1225 1226

    /* only the size is allowed to change for the desktop window */
    if (data->whole_window != root_window)
    {
        changes.x = data->whole_rect.left - virtual_screen_rect.left;
        changes.y = data->whole_rect.top - virtual_screen_rect.top;
        mask |= CWX | CWY;
    }
1227

1228
    if (!(swp_flags & SWP_NOZORDER) || (swp_flags & SWP_SHOWWINDOW))
1229
    {
1230 1231 1232 1233 1234
        /* find window that this one must be after */
        HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
        while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
            prev = GetWindow( prev, GW_HWNDPREV );
        if (!prev)  /* top child */
1235
        {
1236 1237
            changes.stack_mode = Above;
            mask |= CWStackMode;
1238
        }
1239 1240
        /* should use stack_mode Below but most window managers don't get it right */
        /* and Above with a sibling doesn't work so well either, so we ignore it */
1241 1242
    }

1243 1244 1245 1246 1247
    set_size_hints( data, style );
    set_mwm_hints( data, style, ex_style );
    data->configure_serial = NextRequest( data->display );
    XReconfigureWMWindow( data->display, data->whole_window,
                          DefaultScreen(data->display), mask, &changes );
1248
#ifdef HAVE_LIBXSHAPE
1249
    if (IsRectEmpty( old_window_rect ) != IsRectEmpty( &data->window_rect ))
1250
        sync_window_region( data, (HRGN)1 );
1251 1252
    if (data->shaped)
    {
1253 1254 1255 1256 1257
        int old_x_offset = old_window_rect->left - old_whole_rect->left;
        int old_y_offset = old_window_rect->top - old_whole_rect->top;
        int new_x_offset = data->window_rect.left - data->whole_rect.left;
        int new_y_offset = data->window_rect.top - data->whole_rect.top;
        if (old_x_offset != new_x_offset || old_y_offset != new_y_offset)
1258
            XShapeOffsetShape( data->display, data->whole_window, ShapeBounding,
1259
                               new_x_offset - old_x_offset, new_y_offset - old_y_offset );
1260 1261
    }
#endif
1262 1263 1264 1265 1266 1267

    TRACE( "win %p/%lx pos %d,%d,%dx%d after %lx changes=%x serial=%lu\n",
           data->hwnd, data->whole_window, data->whole_rect.left, data->whole_rect.top,
           data->whole_rect.right - data->whole_rect.left,
           data->whole_rect.bottom - data->whole_rect.top,
           changes.sibling, mask, data->configure_serial );
1268
}
1269

1270

1271 1272 1273 1274 1275
/***********************************************************************
 *		move_window_bits
 *
 * Move the window bits when a window is moved.
 */
1276 1277 1278
static void move_window_bits( HWND hwnd, Window window, const RECT *old_rect, const RECT *new_rect,
                              const RECT *old_client_rect, const RECT *new_client_rect,
                              const RECT *new_window_rect )
1279 1280 1281 1282 1283
{
    RECT src_rect = *old_rect;
    RECT dst_rect = *new_rect;
    HDC hdc_src, hdc_dst;
    INT code;
1284
    HRGN rgn;
1285 1286
    HWND parent = 0;

1287
    if (!window)
1288
    {
1289 1290
        OffsetRect( &dst_rect, -new_window_rect->left, -new_window_rect->top );
        parent = GetAncestor( hwnd, GA_PARENT );
1291
        hdc_src = GetDCEx( parent, 0, DCX_CACHE );
1292
        hdc_dst = GetDCEx( hwnd, 0, DCX_CACHE | DCX_WINDOW );
1293 1294 1295
    }
    else
    {
1296
        OffsetRect( &dst_rect, -new_client_rect->left, -new_client_rect->top );
1297 1298 1299
        /* make src rect relative to the old position of the window */
        OffsetRect( &src_rect, -old_client_rect->left, -old_client_rect->top );
        if (dst_rect.left == src_rect.left && dst_rect.top == src_rect.top) return;
1300
        hdc_src = hdc_dst = GetDCEx( hwnd, 0, DCX_CACHE );
1301 1302
    }

1303 1304 1305
    rgn = CreateRectRgnIndirect( &dst_rect );
    SelectClipRgn( hdc_dst, rgn );
    DeleteObject( rgn );
1306
    ExcludeUpdateRgn( hdc_dst, hwnd );
1307

1308 1309 1310
    code = X11DRV_START_EXPOSURES;
    ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );

1311
    TRACE( "copying bits for win %p/%lx %s -> %s\n",
1312
           hwnd, window, wine_dbgstr_rect(&src_rect), wine_dbgstr_rect(&dst_rect) );
1313 1314 1315 1316
    BitBlt( hdc_dst, dst_rect.left, dst_rect.top,
            dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top,
            hdc_src, src_rect.left, src_rect.top, SRCCOPY );

1317
    rgn = 0;
1318 1319 1320
    code = X11DRV_END_EXPOSURES;
    ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, sizeof(rgn), (LPSTR)&rgn );

1321
    ReleaseDC( hwnd, hdc_dst );
1322 1323 1324 1325
    if (hdc_src != hdc_dst) ReleaseDC( parent, hdc_src );

    if (rgn)
    {
1326
        if (!window)
1327 1328
        {
            /* map region to client rect since we are using DCX_WINDOW */
1329 1330 1331
            OffsetRgn( rgn, new_window_rect->left - new_client_rect->left,
                       new_window_rect->top - new_client_rect->top );
            RedrawWindow( hwnd, NULL, rgn,
1332 1333
                          RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ALLCHILDREN );
        }
1334
        else RedrawWindow( hwnd, NULL, rgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1335 1336 1337 1338 1339
        DeleteObject( rgn );
    }
}


1340 1341 1342 1343 1344
/**********************************************************************
 *		create_whole_window
 *
 * Create the whole X window for a given window
 */
1345
static void create_whole_window( struct x11drv_win_data *data )
1346 1347 1348
{
    int cx, cy, mask;
    XSetWindowAttributes attr;
1349
    WCHAR text[1024];
1350 1351 1352
    COLORREF key;
    BYTE alpha;
    DWORD layered_flags;
1353
    HRGN win_rgn;
1354

1355 1356 1357 1358 1359 1360 1361
    if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
    {
        TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
        data->managed = TRUE;
        SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
    }

1362
    if ((win_rgn = CreateRectRgn( 0, 0, 0, 0 )) && GetWindowRgn( data->hwnd, win_rgn ) == ERROR)
1363 1364 1365 1366 1367 1368
    {
        DeleteObject( win_rgn );
        win_rgn = 0;
    }
    data->shaped = (win_rgn != 0);

1369
    mask = get_window_attributes( data, &attr );
1370

1371
    data->whole_rect = data->window_rect;
1372 1373 1374 1375 1376 1377
    X11DRV_window_to_X_rect( data, &data->whole_rect );
    if (!(cx = data->whole_rect.right - data->whole_rect.left)) cx = 1;
    else if (cx > 65535) cx = 65535;
    if (!(cy = data->whole_rect.bottom - data->whole_rect.top)) cy = 1;
    else if (cy > 65535) cy = 65535;

1378
    data->whole_window = XCreateWindow( data->display, root_window,
1379 1380
                                        data->whole_rect.left - virtual_screen_rect.left,
                                        data->whole_rect.top - virtual_screen_rect.top,
1381 1382
                                        cx, cy, 0, default_visual.depth, InputOutput,
                                        default_visual.visual, mask, &attr );
1383
    if (!data->whole_window) goto done;
1384

1385 1386
    set_initial_wm_hints( data->display, data->whole_window );
    set_wm_hints( data );
1387

1388
    XSaveContext( data->display, data->whole_window, winContext, (char *)data->hwnd );
1389
    SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1390

1391 1392
    /* set the window text */
    if (!InternalGetWindowText( data->hwnd, text, sizeof(text)/sizeof(WCHAR) )) text[0] = 0;
1393
    sync_window_text( data->display, data->whole_window, text );
1394

1395
    /* set the window region */
1396
    if (win_rgn || IsRectEmpty( &data->window_rect )) sync_window_region( data, win_rgn );
1397

1398 1399
    /* set the window opacity */
    if (!GetLayeredWindowAttributes( data->hwnd, &key, &alpha, &layered_flags )) layered_flags = 0;
1400
    sync_window_opacity( data->display, data->whole_window, key, alpha, layered_flags );
1401

1402
    XFlush( data->display );  /* make sure the window exists before we start painting to it */
1403

1404
    sync_window_cursor( data->whole_window );
1405

1406 1407
done:
    if (win_rgn) DeleteObject( win_rgn );
1408 1409 1410 1411
}


/**********************************************************************
1412
 *		destroy_whole_window
1413
 *
1414
 * Destroy the whole X window for a given window.
1415
 */
1416
static void destroy_whole_window( struct x11drv_win_data *data, BOOL already_destroyed )
1417
{
1418 1419 1420 1421 1422 1423 1424
    if (!data->whole_window)
    {
        if (data->embedded)
        {
            Window xwin = (Window)GetPropA( data->hwnd, foreign_window_prop );
            if (xwin)
            {
1425 1426
                if (!already_destroyed) XSelectInput( data->display, xwin, 0 );
                XDeleteContext( data->display, xwin, winContext );
1427 1428 1429 1430 1431 1432
                RemovePropA( data->hwnd, foreign_window_prop );
            }
        }
        return;
    }

1433

1434
    TRACE( "win %p xwin %lx\n", data->hwnd, data->whole_window );
1435 1436
    XDeleteContext( data->display, data->whole_window, winContext );
    if (!already_destroyed) XDestroyWindow( data->display, data->whole_window );
1437
    data->whole_window = 0;
1438 1439 1440
    data->wm_state = WithdrawnState;
    data->net_wm_state = 0;
    data->mapped = FALSE;
1441 1442 1443 1444
    if (data->xic)
    {
        XUnsetICFocus( data->xic );
        XDestroyIC( data->xic );
1445
        data->xic = 0;
1446
    }
1447
    /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1448
    XFlush( data->display );
1449 1450
    if (data->surface) window_surface_release( data->surface );
    data->surface = NULL;
1451
    RemovePropA( data->hwnd, whole_window_prop );
1452 1453 1454 1455 1456 1457
}


/*****************************************************************
 *		SetWindowText   (X11DRV.@)
 */
1458
void CDECL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1459 1460
{
    Window win;
1461

1462 1463 1464
    if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(gdi_display))
    {
        Display *display = thread_init_display();
1465
        sync_window_text( display, win, text );
1466
    }
1467 1468 1469
}


1470 1471 1472 1473 1474
/***********************************************************************
 *		SetWindowStyle   (X11DRV.@)
 *
 * Update the X state of a window to reflect a style change
 */
1475
void CDECL X11DRV_SetWindowStyle( HWND hwnd, INT offset, STYLESTRUCT *style )
1476
{
1477 1478
    struct x11drv_win_data *data;
    DWORD changed = style->styleNew ^ style->styleOld;
1479 1480

    if (hwnd == GetDesktopWindow()) return;
1481 1482
    if (!(data = get_win_data( hwnd ))) return;
    if (!data->whole_window) goto done;
1483

1484
    if (offset == GWL_STYLE && (changed & WS_DISABLED)) set_wm_hints( data );
1485

1486
    if (offset == GWL_EXSTYLE && (changed & WS_EX_LAYERED)) /* changing WS_EX_LAYERED resets attributes */
1487
    {
1488
        sync_window_opacity( data->display, data->whole_window, 0, 0, 0 );
1489 1490
        if (data->surface) set_surface_color_key( data->surface, CLR_INVALID );
    }
1491 1492
done:
    release_win_data( data );
1493 1494 1495
}


1496 1497 1498
/***********************************************************************
 *		DestroyWindow   (X11DRV.@)
 */
1499
void CDECL X11DRV_DestroyWindow( HWND hwnd )
1500
{
1501
    struct x11drv_thread_data *thread_data = x11drv_thread_data();
1502
    struct x11drv_win_data *data;
1503

1504
    if (!(data = get_win_data( hwnd ))) return;
1505

1506
    destroy_whole_window( data, FALSE );
1507
    if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1508
    if (thread_data->last_xic_hwnd == hwnd) thread_data->last_xic_hwnd = 0;
1509 1510
    if (data->icon_pixmap) XFreePixmap( gdi_display, data->icon_pixmap );
    if (data->icon_mask) XFreePixmap( gdi_display, data->icon_mask );
1511
    HeapFree( GetProcessHeap(), 0, data->icon_bits );
1512 1513
    XDeleteContext( gdi_display, (XID)hwnd, win_data_context );
    release_win_data( data );
1514
    HeapFree( GetProcessHeap(), 0, data );
1515
    destroy_gl_drawable( hwnd );
1516 1517 1518
}


1519 1520 1521 1522 1523 1524
/***********************************************************************
 *		X11DRV_DestroyNotify
 */
void X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
{
    struct x11drv_win_data *data;
1525
    BOOL embedded;
1526

1527 1528 1529
    if (!(data = get_win_data( hwnd ))) return;
    embedded = data->embedded;
    if (!embedded) FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1530

1531
    destroy_whole_window( data, TRUE );
1532 1533
    release_win_data( data );
    if (embedded) SendMessageW( hwnd, WM_CLOSE, 0, 0 );
1534 1535 1536
}


1537 1538 1539 1540
static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
{
    struct x11drv_win_data *data;

1541
    if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
1542
    {
1543
        data->display = display;
1544
        data->hwnd = hwnd;
1545 1546
        EnterCriticalSection( &win_data_section );
        XSaveContext( gdi_display, (XID)hwnd, win_data_context, (char *)data );
1547 1548 1549 1550 1551
    }
    return data;
}


1552
/* initialize the desktop window id in the desktop manager process */
1553
static BOOL create_desktop_win_data( Display *display, HWND hwnd )
1554
{
1555
    struct x11drv_win_data *data;
1556

1557
    if (!(data = alloc_win_data( display, hwnd ))) return FALSE;
1558
    data->whole_window = root_window;
1559 1560 1561
    data->managed = TRUE;
    SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
    SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
1562
    set_initial_wm_hints( display, root_window );
1563 1564
    release_win_data( data );
    return TRUE;
1565 1566
}

1567 1568 1569
/**********************************************************************
 *		CreateDesktopWindow   (X11DRV.@)
 */
1570
BOOL CDECL X11DRV_CreateDesktopWindow( HWND hwnd )
1571
{
1572
    unsigned int width, height;
1573

1574 1575 1576
    /* retrieve the real size of the desktop */
    SERVER_START_REQ( get_window_rectangles )
    {
1577
        req->handle = wine_server_user_handle( hwnd );
1578
        req->relative = COORDS_CLIENT;
1579
        wine_server_call( req );
1580 1581
        width  = reply->window.right;
        height = reply->window.bottom;
1582 1583
    }
    SERVER_END_REQ;
1584

1585 1586 1587 1588
    if (!width && !height)  /* not initialized yet */
    {
        SERVER_START_REQ( set_window_pos )
        {
1589
            req->handle        = wine_server_user_handle( hwnd );
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
            req->previous      = 0;
            req->flags         = SWP_NOZORDER;
            req->window.left   = virtual_screen_rect.left;
            req->window.top    = virtual_screen_rect.top;
            req->window.right  = virtual_screen_rect.right;
            req->window.bottom = virtual_screen_rect.bottom;
            req->client        = req->window;
            wine_server_call( req );
        }
        SERVER_END_REQ;
    }
    else
    {
        Window win = (Window)GetPropA( hwnd, whole_window_prop );
        if (win && win != root_window) X11DRV_init_desktop( win, width, height );
    }
1606 1607 1608 1609
    return TRUE;
}


1610 1611 1612
/**********************************************************************
 *		CreateWindow   (X11DRV.@)
 */
1613
BOOL CDECL X11DRV_CreateWindow( HWND hwnd )
1614
{
1615
    if (hwnd == GetDesktopWindow())
1616
    {
1617
        struct x11drv_thread_data *data = x11drv_init_thread_data();
1618 1619 1620 1621 1622
        XSetWindowAttributes attr;

        if (root_window != DefaultRootWindow( gdi_display ))
        {
            /* the desktop win data can't be created lazily */
1623
            if (!create_desktop_win_data( data->display, hwnd )) return FALSE;
1624
        }
1625

1626 1627
        /* create the cursor clipping window */
        attr.override_redirect = TRUE;
1628
        attr.event_mask = StructureNotifyMask | FocusChangeMask;
1629
        data->clip_window = XCreateWindow( data->display, root_window, 0, 0, 1, 1, 0, 0,
1630 1631
                                           InputOnly, default_visual.visual,
                                           CWOverrideRedirect | CWEventMask, &attr );
1632
        XFlush( data->display );
1633
        SetPropA( hwnd, clip_window_prop, (HANDLE)data->clip_window );
1634
    }
1635 1636 1637 1638
    return TRUE;
}


1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
/***********************************************************************
 *		get_win_data
 *
 * Lock and return the X11 data structure associated with a window.
 */
struct x11drv_win_data *get_win_data( HWND hwnd )
{
    char *data;

    if (!hwnd) return NULL;
    EnterCriticalSection( &win_data_section );
    if (!XFindContext( gdi_display, (XID)hwnd, win_data_context, &data ))
        return (struct x11drv_win_data *)data;
    LeaveCriticalSection( &win_data_section );
    return NULL;
}


/***********************************************************************
 *		release_win_data
 *
 * Release the data returned by get_win_data.
 */
void release_win_data( struct x11drv_win_data *data )
{
    if (data) LeaveCriticalSection( &win_data_section );
}


1668 1669 1670 1671 1672
/***********************************************************************
 *		X11DRV_create_win_data
 *
 * Create an X11 data window structure for an existing window.
 */
1673 1674
static struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd, const RECT *window_rect,
                                                       const RECT *client_rect )
1675
{
1676
    Display *display;
1677 1678 1679 1680
    struct x11drv_win_data *data;
    HWND parent;

    if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL;  /* desktop */
1681

1682 1683 1684
    /* don't create win data for HWND_MESSAGE windows */
    if (parent != GetDesktopWindow() && !GetAncestor( parent, GA_PARENT )) return NULL;

1685 1686
    if (GetWindowThreadProcessId( hwnd, NULL ) != GetCurrentThreadId()) return NULL;

1687
    display = thread_init_display();
1688 1689
    init_clip_window();  /* make sure the clip window is initialized in this thread */

1690 1691
    if (!(data = alloc_win_data( display, hwnd ))) return NULL;

1692 1693
    data->whole_rect = data->window_rect = *window_rect;
    data->client_rect = *client_rect;
1694 1695
    if (parent == GetDesktopWindow())
    {
1696
        create_whole_window( data );
1697 1698
        TRACE( "win %p/%lx window %s whole %s client %s\n",
               hwnd, data->whole_window, wine_dbgstr_rect( &data->window_rect ),
1699 1700 1701 1702 1703 1704
               wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
    }
    return data;
}


1705 1706 1707 1708 1709
/* window procedure for foreign windows */
static LRESULT WINAPI foreign_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
{
    switch(msg)
    {
1710 1711 1712
    case WM_WINDOWPOSCHANGED:
        update_systray_balloon_position();
        break;
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
    case WM_PARENTNOTIFY:
        if (LOWORD(wparam) == WM_DESTROY)
        {
            TRACE( "%p: got parent notify destroy for win %lx\n", hwnd, lparam );
            PostMessageW( hwnd, WM_CLOSE, 0, 0 );  /* so that we come back here once the child is gone */
        }
        return 0;
    case WM_CLOSE:
        if (GetWindow( hwnd, GW_CHILD )) return 0;  /* refuse to die if we still have children */
        break;
    }
    return DefWindowProcW( hwnd, msg, wparam, lparam );
}


/***********************************************************************
 *		create_foreign_window
 *
 * Create a foreign window for the specified X window and its ancestors
 */
HWND create_foreign_window( Display *display, Window xwin )
{
    static const WCHAR classW[] = {'_','_','w','i','n','e','_','x','1','1','_','f','o','r','e','i','g','n','_','w','i','n','d','o','w',0};
    static BOOL class_registered;
    struct x11drv_win_data *data;
    HWND hwnd, parent;
    Window xparent, xroot;
    Window *xchildren;
    unsigned int nchildren;
    XWindowAttributes attr;
    DWORD style = WS_CLIPCHILDREN;

    if (!class_registered)
    {
        WNDCLASSEXW class;

        memset( &class, 0, sizeof(class) );
        class.cbSize        = sizeof(class);
        class.lpfnWndProc   = foreign_window_proc;
        class.lpszClassName = classW;
        if (!RegisterClassExW( &class ) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
        {
            ERR( "Could not register foreign window class\n" );
            return FALSE;
        }
        class_registered = TRUE;
    }

    if (XFindContext( display, xwin, winContext, (char **)&hwnd )) hwnd = 0;
1762
    if (hwnd) return hwnd;  /* already created */
1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795

    XSelectInput( display, xwin, StructureNotifyMask );
    if (!XGetWindowAttributes( display, xwin, &attr ) ||
        !XQueryTree( display, xwin, &xroot, &xparent, &xchildren, &nchildren ))
    {
        XSelectInput( display, xwin, 0 );
        return 0;
    }
    XFree( xchildren );

    if (xparent == xroot)
    {
        parent = GetDesktopWindow();
        style |= WS_POPUP;
        attr.x += virtual_screen_rect.left;
        attr.y += virtual_screen_rect.top;
    }
    else
    {
        parent = create_foreign_window( display, xparent );
        style |= WS_CHILD;
    }

    hwnd = CreateWindowW( classW, NULL, style, attr.x, attr.y, attr.width, attr.height,
                          parent, 0, 0, NULL );

    if (!(data = alloc_win_data( display, hwnd )))
    {
        DestroyWindow( hwnd );
        return 0;
    }
    SetRect( &data->window_rect, attr.x, attr.y, attr.x + attr.width, attr.y + attr.height );
    data->whole_rect = data->client_rect = data->window_rect;
1796
    data->whole_window = 0;
1797 1798 1799
    data->embedded = TRUE;
    data->mapped = TRUE;

1800
    SetPropA( hwnd, foreign_window_prop, (HANDLE)xwin );
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811
    XSaveContext( display, xwin, winContext, (char *)data->hwnd );

    ShowWindow( hwnd, SW_SHOW );

    TRACE( "win %lx parent %p style %08x %s -> hwnd %p\n",
           xwin, parent, style, wine_dbgstr_rect(&data->window_rect), hwnd );

    return hwnd;
}


1812 1813 1814 1815 1816 1817 1818
/***********************************************************************
 *		X11DRV_get_whole_window
 *
 * Return the X window associated with the full area of a window
 */
Window X11DRV_get_whole_window( HWND hwnd )
{
1819 1820
    struct x11drv_win_data *data = get_win_data( hwnd );
    Window ret;
1821

1822 1823 1824 1825 1826
    if (!data)
    {
        if (hwnd == GetDesktopWindow()) return root_window;
        return (Window)GetPropA( hwnd, whole_window_prop );
    }
1827 1828 1829
    ret = data->whole_window;
    release_win_data( data );
    return ret;
1830 1831 1832
}


1833
/***********************************************************************
1834
 *		X11DRV_get_ic
1835
 *
1836
 * Return the X input context associated with a window
1837
 */
1838
XIC X11DRV_get_ic( HWND hwnd )
1839
{
1840
    struct x11drv_win_data *data = get_win_data( hwnd );
1841
    XIM xim;
1842
    XIC ret = 0;
1843

1844 1845 1846 1847 1848 1849 1850 1851
    if (data)
    {
        x11drv_thread_data()->last_xic_hwnd = hwnd;
        ret = data->xic;
        if (!ret && (xim = x11drv_thread_data()->xim)) ret = X11DRV_CreateIC( xim, data );
        release_win_data( data );
    }
    return ret;
1852 1853
}

1854

1855
/***********************************************************************
1856
 *		X11DRV_GetDC   (X11DRV.@)
1857
 */
1858 1859
void CDECL X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
                         const RECT *top_rect, DWORD flags )
1860
{
1861
    struct x11drv_escape_set_drawable escape;
1862
    HWND parent;
1863

1864
    escape.code        = X11DRV_SET_DRAWABLE;
1865
    escape.hwnd        = hwnd;
1866 1867
    escape.mode        = IncludeInferiors;
    escape.fbconfig_id = 0;
1868

1869 1870 1871 1872 1873
    escape.dc_rect.left         = win_rect->left - top_rect->left;
    escape.dc_rect.top          = win_rect->top - top_rect->top;
    escape.dc_rect.right        = win_rect->right - top_rect->left;
    escape.dc_rect.bottom       = win_rect->bottom - top_rect->top;

1874
    if (top == hwnd)
1875
    {
1876 1877
        struct x11drv_win_data *data = get_win_data( hwnd );

1878
        escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
1879 1880 1881

        /* special case: when repainting the root window, clip out top-level windows */
        if (data && data->whole_window == root_window) escape.mode = ClipByChildren;
1882
        release_win_data( data );
1883 1884 1885
    }
    else
    {
1886 1887
        /* find the first ancestor that has a drawable */
        for (parent = hwnd; parent && parent != top; parent = GetAncestor( parent, GA_PARENT ))
1888
            if ((escape.drawable = X11DRV_get_whole_window( parent ))) break;
1889 1890 1891 1892

        if (escape.drawable)
        {
            POINT pt = { 0, 0 };
1893 1894
            MapWindowPoints( 0, parent, &pt, 1 );
            escape.dc_rect = *win_rect;
1895
            OffsetRect( &escape.dc_rect, pt.x, pt.y );
1896
            if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
1897
        }
1898
        else escape.drawable = X11DRV_get_whole_window( top );
1899
    }
1900

1901
    ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1902 1903
}

1904

1905
/***********************************************************************
1906
 *		X11DRV_ReleaseDC  (X11DRV.@)
1907
 */
1908
void CDECL X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
1909
{
1910 1911 1912
    struct x11drv_escape_set_drawable escape;

    escape.code = X11DRV_SET_DRAWABLE;
1913
    escape.hwnd = GetDesktopWindow();
1914 1915 1916 1917
    escape.drawable = root_window;
    escape.mode = IncludeInferiors;
    SetRect( &escape.dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
             virtual_screen_rect.bottom - virtual_screen_rect.top );
1918
    OffsetRect( &escape.dc_rect, -virtual_screen_rect.left, -virtual_screen_rect.top );
1919 1920
    escape.fbconfig_id = 0;
    ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1921 1922 1923
}


1924 1925 1926
/***********************************************************************
 *		SetCapture  (X11DRV.@)
 */
1927
void CDECL X11DRV_SetCapture( HWND hwnd, UINT flags )
1928 1929 1930
{
    struct x11drv_thread_data *thread_data = x11drv_thread_data();

1931
    if (!thread_data) return;
1932
    if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
1933 1934 1935

    if (hwnd)
    {
1936
        Window grab_win = X11DRV_get_whole_window( GetAncestor( hwnd, GA_ROOT ) );
1937 1938 1939 1940 1941

        if (!grab_win) return;
        XFlush( gdi_display );
        XGrabPointer( thread_data->display, grab_win, False,
                      PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1942
                      GrabModeAsync, GrabModeAsync, None, None, CurrentTime );
1943 1944 1945 1946 1947 1948
        thread_data->grab_window = grab_win;
    }
    else  /* release capture */
    {
        XFlush( gdi_display );
        XUngrabPointer( thread_data->display, CurrentTime );
1949
        XFlush( thread_data->display );
1950 1951 1952 1953 1954
        thread_data->grab_window = None;
    }
}


1955 1956 1957
/*****************************************************************
 *		SetParent   (X11DRV.@)
 */
1958
void CDECL X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
1959
{
1960
    struct x11drv_win_data *data;
1961

1962
    if (parent == old_parent) return;
1963 1964
    if (!(data = get_win_data( hwnd ))) return;
    if (data->embedded) goto done;
1965

1966
    if (parent != GetDesktopWindow()) /* a child window */
1967
    {
1968
        if (old_parent == GetDesktopWindow())
1969
        {
1970
            /* destroy the old X windows */
1971
            destroy_whole_window( data, FALSE );
1972
            if (data->managed)
1973
            {
1974 1975
                data->managed = FALSE;
                RemovePropA( data->hwnd, managed_prop );
1976 1977
            }
        }
1978
    }
1979 1980
    else  /* new top level window */
    {
1981
        create_whole_window( data );
1982
    }
1983 1984 1985
done:
    release_win_data( data );
    fetch_icon_data( hwnd, 0, 0 );
1986 1987 1988
}


1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002
static inline RECT get_surface_rect( const RECT *visible_rect )
{
    RECT rect;

    IntersectRect( &rect, visible_rect, &virtual_screen_rect );
    OffsetRect( &rect, -visible_rect->left, -visible_rect->top );
    rect.left &= ~31;
    rect.top  &= ~31;
    rect.right  = max( rect.left + 32, (rect.right + 31) & ~31 );
    rect.bottom = max( rect.top + 32, (rect.bottom + 31) & ~31 );
    return rect;
}


2003
/***********************************************************************
2004
 *		WindowPosChanging   (X11DRV.@)
2005
 */
2006
void CDECL X11DRV_WindowPosChanging( HWND hwnd, HWND insert_after, UINT swp_flags,
2007 2008
                                     const RECT *window_rect, const RECT *client_rect, RECT *visible_rect,
                                     struct window_surface **surface )
2009
{
2010
    struct x11drv_win_data *data = get_win_data( hwnd );
2011
    RECT surface_rect;
2012 2013 2014
    DWORD flags;
    COLORREF key;
    BOOL layered = GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED;
2015

2016
    if (!data && !(data = X11DRV_create_win_data( hwnd, window_rect, client_rect ))) return;
2017 2018

    /* check if we need to switch the window to managed */
2019
    if (!data->managed && data->whole_window && is_window_managed( hwnd, swp_flags, window_rect ))
2020 2021
    {
        TRACE( "making win %p/%lx managed\n", hwnd, data->whole_window );
2022 2023 2024
        release_win_data( data );
        unmap_window( hwnd );
        if (!(data = get_win_data( hwnd ))) return;
2025 2026 2027 2028
        data->managed = TRUE;
        SetPropA( hwnd, managed_prop, (HANDLE)1 );
    }

2029 2030
    *visible_rect = *window_rect;
    X11DRV_window_to_X_rect( data, visible_rect );
2031 2032

    /* create the window surface if necessary */
2033

2034 2035
    if (!data->whole_window) goto done;
    if (swp_flags & SWP_HIDEWINDOW) goto done;
2036 2037 2038 2039 2040

    if (*surface) window_surface_release( *surface );
    *surface = NULL;  /* indicate that we want to draw directly to the window */

    if (data->embedded) goto done;
2041 2042 2043
    if (data->whole_window == root_window) goto done;
    if (has_gl_drawable( hwnd )) goto done;
    if (!client_side_graphics && !layered) goto done;
2044 2045 2046 2047 2048 2049 2050 2051 2052

    surface_rect = get_surface_rect( visible_rect );
    if (data->surface)
    {
        if (!memcmp( &data->surface->rect, &surface_rect, sizeof(surface_rect) ))
        {
            /* existing surface is good enough */
            window_surface_add_ref( data->surface );
            *surface = data->surface;
2053
            goto done;
2054 2055
        }
    }
2056
    else if (!(swp_flags & SWP_SHOWWINDOW) && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)) goto done;
2057

2058 2059 2060
    if (!layered || !GetLayeredWindowAttributes( hwnd, &key, NULL, &flags ) || !(flags & LWA_COLORKEY))
        key = CLR_INVALID;

2061
    *surface = create_surface( data->whole_window, &default_visual, &surface_rect, key );
2062 2063 2064

done:
    release_win_data( data );
2065 2066 2067 2068 2069 2070
}


/***********************************************************************
 *		WindowPosChanged   (X11DRV.@)
 */
2071 2072
void CDECL X11DRV_WindowPosChanged( HWND hwnd, HWND insert_after, UINT swp_flags,
                                    const RECT *rectWindow, const RECT *rectClient,
2073 2074
                                    const RECT *visible_rect, const RECT *valid_rects,
                                    struct window_surface *surface )
2075 2076
{
    struct x11drv_thread_data *thread_data;
2077
    struct x11drv_win_data *data;
2078
    DWORD new_style = GetWindowLongW( hwnd, GWL_STYLE );
2079
    RECT old_window_rect, old_whole_rect, old_client_rect;
2080 2081
    int event_type;

2082
    if (!(data = get_win_data( hwnd ))) return;
2083 2084 2085

    thread_data = x11drv_thread_data();

2086
    old_window_rect = data->window_rect;
2087 2088 2089
    old_whole_rect  = data->whole_rect;
    old_client_rect = data->client_rect;
    data->window_rect = *rectWindow;
2090
    data->whole_rect  = *visible_rect;
2091
    data->client_rect = *rectClient;
2092 2093 2094
    if (surface) window_surface_add_ref( surface );
    if (data->surface) window_surface_release( data->surface );
    data->surface = surface;
2095 2096 2097 2098 2099 2100

    TRACE( "win %p window %s client %s style %08x flags %08x\n",
           hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style, swp_flags );

    if (!IsRectEmpty( &valid_rects[0] ))
    {
2101
        Window window = data->whole_window;
2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115
        int x_offset = old_whole_rect.left - data->whole_rect.left;
        int y_offset = old_whole_rect.top - data->whole_rect.top;

        /* if all that happened is that the whole window moved, copy everything */
        if (!(swp_flags & SWP_FRAMECHANGED) &&
            old_whole_rect.right   - data->whole_rect.right   == x_offset &&
            old_whole_rect.bottom  - data->whole_rect.bottom  == y_offset &&
            old_client_rect.left   - data->client_rect.left   == x_offset &&
            old_client_rect.right  - data->client_rect.right  == x_offset &&
            old_client_rect.top    - data->client_rect.top    == y_offset &&
            old_client_rect.bottom - data->client_rect.bottom == y_offset &&
            !memcmp( &valid_rects[0], &data->client_rect, sizeof(RECT) ))
        {
            /* if we have an X window the bits will be moved by the X server */
2116 2117 2118 2119 2120 2121 2122
            if (!window && (x_offset != 0 || y_offset != 0))
            {
                release_win_data( data );
                move_window_bits( hwnd, window, &old_whole_rect, visible_rect,
                                  &old_client_rect, rectClient, rectWindow );
                if (!(data = get_win_data( hwnd ))) return;
            }
2123 2124
        }
        else
2125 2126 2127 2128 2129 2130
        {
            release_win_data( data );
            move_window_bits( hwnd, window, &valid_rects[1], &valid_rects[0],
                              &old_client_rect, rectClient, rectWindow );
            if (!(data = get_win_data( hwnd ))) return;
        }
2131 2132 2133 2134
    }

    XFlush( gdi_display );  /* make sure painting is done before we move the window */

2135
    sync_gl_drawable( data->hwnd, visible_rect, rectClient );
2136

2137
    if (!data->whole_window) goto done;
2138 2139 2140

    /* check if we are currently processing an event relevant to this window */
    event_type = 0;
2141 2142 2143 2144
    if (thread_data &&
        thread_data->current_event &&
        thread_data->current_event->xany.window == data->whole_window)
    {
2145
        event_type = thread_data->current_event->type;
2146 2147 2148 2149
        if (event_type != ConfigureNotify && event_type != PropertyNotify &&
            event_type != GravityNotify && event_type != ReparentNotify)
            event_type = 0;  /* ignore other events */
    }
2150

2151
    if (data->mapped && event_type != ReparentNotify)
2152 2153
    {
        if (((swp_flags & SWP_HIDEWINDOW) && !(new_style & WS_VISIBLE)) ||
2154
            (!event_type &&
2155
             !is_window_rect_mapped( rectWindow ) && is_window_rect_mapped( &old_window_rect )))
2156
        {
2157
            release_win_data( data );
2158
            unmap_window( hwnd );
2159
            if (is_window_rect_fullscreen( &old_window_rect )) reset_clipping_window();
2160
            if (!(data = get_win_data( hwnd ))) return;
2161
        }
2162
    }
2163 2164 2165 2166

    /* don't change position if we are about to minimize or maximize a managed window */
    if (!event_type &&
        !(data->managed && (swp_flags & SWP_STATECHANGED) && (new_style & (WS_MINIMIZE|WS_MAXIMIZE))))
2167
        sync_window_position( data, swp_flags, &old_window_rect, &old_whole_rect, &old_client_rect );
2168 2169 2170 2171 2172 2173

    if ((new_style & WS_VISIBLE) &&
        ((new_style & WS_MINIMIZE) || is_window_rect_mapped( rectWindow )))
    {
        if (!data->mapped)
        {
2174 2175 2176 2177
            BOOL needs_icon = !data->icon_pixmap;

            release_win_data( data );
            if (needs_icon) fetch_icon_data( hwnd, 0, 0 );
2178
            map_window( hwnd, new_style );
2179
            return;
2180 2181 2182
        }
        else if ((swp_flags & SWP_STATECHANGED) && (!data->iconic != !(new_style & WS_MINIMIZE)))
        {
2183
            set_wm_hints( data );
2184 2185 2186
            data->iconic = (new_style & WS_MINIMIZE) != 0;
            TRACE( "changing win %p iconic state to %u\n", data->hwnd, data->iconic );
            if (data->iconic)
2187
                XIconifyWindow( data->display, data->whole_window, DefaultScreen(data->display) );
2188
            else if (is_window_rect_mapped( rectWindow ))
2189 2190
                XMapWindow( data->display, data->whole_window );
            update_net_wm_states( data );
2191
        }
2192
        else
2193
        {
2194 2195
            if (swp_flags & (SWP_FRAMECHANGED|SWP_STATECHANGED)) set_wm_hints( data );
            if (!event_type) update_net_wm_states( data );
2196 2197 2198
        }
    }

2199
    XFlush( data->display );  /* make sure changes are done before we start painting again */
2200 2201
done:
    release_win_data( data );
2202 2203 2204
}


2205 2206 2207
/***********************************************************************
 *           ShowWindow   (X11DRV.@)
 */
2208
UINT CDECL X11DRV_ShowWindow( HWND hwnd, INT cmd, RECT *rect, UINT swp )
2209 2210 2211 2212 2213 2214
{
    int x, y;
    unsigned int width, height, border, depth;
    Window root, top;
    DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
    struct x11drv_thread_data *thread_data = x11drv_thread_data();
2215
    struct x11drv_win_data *data = get_win_data( hwnd );
2216

2217 2218 2219
    if (!data || !data->whole_window || !data->managed || !data->mapped || data->iconic) goto done;
    if (style & WS_MINIMIZE) goto done;
    if (IsRectEmpty( rect )) goto done;
2220 2221 2222 2223

    /* only fetch the new rectangle if the ShowWindow was a result of a window manager event */

    if (!thread_data->current_event || thread_data->current_event->xany.window != data->whole_window)
2224
        goto done;
2225 2226 2227

    if (thread_data->current_event->type != ConfigureNotify &&
        thread_data->current_event->type != PropertyNotify)
2228
        goto done;
2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241

    TRACE( "win %p/%lx cmd %d at %s flags %08x\n",
           hwnd, data->whole_window, cmd, wine_dbgstr_rect(rect), swp );

    XGetGeometry( thread_data->display, data->whole_window,
                  &root, &x, &y, &width, &height, &border, &depth );
    XTranslateCoordinates( thread_data->display, data->whole_window, root, 0, 0, &x, &y, &top );
    rect->left   = x;
    rect->top    = y;
    rect->right  = x + width;
    rect->bottom = y + height;
    OffsetRect( rect, virtual_screen_rect.left, virtual_screen_rect.top );
    X11DRV_X_to_window_rect( data, rect );
2242 2243 2244 2245 2246
    swp &= ~(SWP_NOMOVE | SWP_NOCLIENTMOVE | SWP_NOSIZE | SWP_NOCLIENTSIZE);

done:
    release_win_data( data );
    return swp;
2247 2248 2249
}


2250
/**********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
2251
 *		SetWindowIcon (X11DRV.@)
2252 2253 2254 2255 2256
 *
 * hIcon or hIconSm has changed (or is being initialised for the
 * first time). Complete the X11 driver-specific initialisation
 * and set the window hints.
 */
2257
void CDECL X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
2258
{
2259
    struct x11drv_win_data *data;
2260

2261 2262 2263 2264
    if (!(data = get_win_data( hwnd ))) return;
    if (!data->whole_window) goto done;
    release_win_data( data );  /* release the lock, fetching the icon requires sending messages */

2265 2266
    if (type == ICON_BIG) fetch_icon_data( hwnd, icon, 0 );
    else fetch_icon_data( hwnd, 0, icon );
2267 2268

    if (!(data = get_win_data( hwnd ))) return;
2269
    set_wm_hints( data );
2270 2271
done:
    release_win_data( data );
2272
}
2273 2274 2275 2276 2277 2278 2279


/***********************************************************************
 *		SetWindowRgn  (X11DRV.@)
 *
 * Assign specified region to window (for non-rectangular windows)
 */
2280
int CDECL X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
2281 2282 2283
{
    struct x11drv_win_data *data;

2284
    if ((data = get_win_data( hwnd )))
2285
    {
2286
        sync_window_region( data, hrgn );
2287
        release_win_data( data );
2288
    }
2289
    else if (X11DRV_get_whole_window( hwnd ))
2290
    {
2291
        SendMessageW( hwnd, WM_X11DRV_SET_WIN_REGION, 0, 0 );
2292 2293 2294
    }
    return TRUE;
}
2295 2296


2297 2298 2299 2300 2301
/***********************************************************************
 *		SetLayeredWindowAttributes  (X11DRV.@)
 *
 * Set transparency attributes for a layered window.
 */
2302
void CDECL X11DRV_SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
2303
{
2304
    struct x11drv_win_data *data = get_win_data( hwnd );
2305

2306 2307 2308
    if (data)
    {
        if (data->whole_window)
2309
            sync_window_opacity( data->display, data->whole_window, key, alpha, flags );
2310 2311
        if (data->surface)
            set_surface_color_key( data->surface, (flags & LWA_COLORKEY) ? key : CLR_INVALID );
2312
        release_win_data( data );
2313 2314 2315 2316
    }
    else
    {
        Window win = X11DRV_get_whole_window( hwnd );
2317 2318 2319 2320
        if (win)
        {
            sync_window_opacity( gdi_display, win, key, alpha, flags );
            if (flags & LWA_COLORKEY)
2321
                FIXME( "LWA_COLORKEY not supported on foreign process window %p\n", hwnd );
2322
        }
2323
    }
2324 2325 2326
}


2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369
/*****************************************************************************
 *              UpdateLayeredWindow  (X11DRV.@)
 */
BOOL CDECL X11DRV_UpdateLayeredWindow( HWND hwnd, const UPDATELAYEREDWINDOWINFO *info,
                                       const RECT *window_rect )
{
    BYTE alpha = 0xff;

    if (info->hdcSrc)
    {
        HDC hdc = GetWindowDC( hwnd );

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

            GetWindowRect( hwnd, &rect );
            OffsetRect( &rect, -rect.left, -rect.top);
            if (info->pptSrc)
            {
                x = info->pptSrc->x;
                y = info->pptSrc->y;
            }

            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 );
            }
            ReleaseDC( hwnd, hdc );
        }
    }

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


2370 2371 2372
/**********************************************************************
 *           X11DRV_WindowMessage   (X11DRV.@)
 */
2373
LRESULT CDECL X11DRV_WindowMessage( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
2374
{
2375 2376
    struct x11drv_win_data *data;

2377 2378 2379 2380 2381 2382
    switch(msg)
    {
    case WM_X11DRV_ACQUIRE_SELECTION:
        return X11DRV_AcquireClipboard( hwnd );
    case WM_X11DRV_SET_WIN_FORMAT:
        return set_win_format( hwnd, (XID)wp );
2383
    case WM_X11DRV_SET_WIN_REGION:
2384 2385
        if ((data = get_win_data( hwnd )))
        {
2386
            sync_window_region( data, (HRGN)1 );
2387 2388
            release_win_data( data );
        }
2389
        return 0;
2390 2391 2392
    case WM_X11DRV_RESIZE_DESKTOP:
        X11DRV_resize_desktop( LOWORD(lp), HIWORD(lp) );
        return 0;
2393
    case WM_X11DRV_SET_CURSOR:
2394 2395 2396 2397 2398
        if ((data = get_win_data( hwnd )))
        {
            if (data->whole_window) set_window_cursor( data->whole_window, (HCURSOR)lp );
            release_win_data( data );
        }
2399 2400
        else if (hwnd == x11drv_thread_data()->clip_hwnd)
            set_window_cursor( x11drv_thread_data()->clip_window, (HCURSOR)lp );
2401
        return 0;
2402 2403
    case WM_X11DRV_CLIP_CURSOR:
        return clip_cursor_notify( hwnd, (HWND)lp );
2404 2405 2406 2407 2408 2409 2410
    default:
        FIXME( "got window msg %x hwnd %p wp %lx lp %lx\n", msg, hwnd, wp, lp );
        return 0;
    }
}


2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444
/***********************************************************************
 *              is_netwm_supported
 */
static BOOL is_netwm_supported( Display *display, Atom atom )
{
    static Atom *net_supported;
    static int net_supported_count = -1;
    int i;

    if (net_supported_count == -1)
    {
        Atom type;
        int format;
        unsigned long count, remaining;

        if (!XGetWindowProperty( display, DefaultRootWindow(display), x11drv_atom(_NET_SUPPORTED), 0,
                                 ~0UL, False, XA_ATOM, &type, &format, &count,
                                 &remaining, (unsigned char **)&net_supported ))
            net_supported_count = get_property_size( format, count ) / sizeof(Atom);
        else
            net_supported_count = 0;
    }

    for (i = 0; i < net_supported_count; i++)
        if (net_supported[i] == atom) return TRUE;
    return FALSE;
}


/***********************************************************************
 *           SysCommand   (X11DRV.@)
 *
 * Perform WM_SYSCOMMAND handling.
 */
2445
LRESULT CDECL X11DRV_SysCommand( HWND hwnd, WPARAM wparam, LPARAM lparam )
2446 2447
{
    WPARAM hittest = wparam & 0x0f;
2448
    int dir;
2449 2450
    struct x11drv_win_data *data;

2451 2452
    if (!(data = get_win_data( hwnd ))) return -1;
    if (!data->whole_window || !data->managed || !data->mapped) goto failed;
2453 2454 2455 2456 2457 2458 2459 2460 2461

    switch (wparam & 0xfff0)
    {
    case SC_MOVE:
        if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
        else dir = _NET_WM_MOVERESIZE_MOVE;
        break;
    case SC_SIZE:
        /* windows without WS_THICKFRAME are not resizable through the window manager */
2462
        if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_THICKFRAME)) goto failed;
2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480

        switch (hittest)
        {
        case WMSZ_LEFT:        dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
        case WMSZ_RIGHT:       dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
        case WMSZ_TOP:         dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
        case WMSZ_TOPLEFT:     dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
        case WMSZ_TOPRIGHT:    dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
        case WMSZ_BOTTOM:      dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
        case WMSZ_BOTTOMLEFT:  dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
        case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
        default:               dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD; break;
        }
        break;

    case SC_KEYMENU:
        /* prevent a simple ALT press+release from activating the system menu,
         * as that can get confusing on managed windows */
2481 2482 2483
        if ((WCHAR)lparam) goto failed;  /* got an explicit char */
        if (GetMenu( hwnd )) goto failed;  /* window has a real menu */
        if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) goto failed;  /* no system menu */
2484
        TRACE( "ignoring SC_KEYMENU wp %lx lp %lx\n", wparam, lparam );
2485
        release_win_data( data );
2486 2487 2488
        return 0;

    default:
2489
        goto failed;
2490 2491
    }

2492
    if (IsZoomed(hwnd)) goto failed;
2493

2494
    if (!is_netwm_supported( data->display, x11drv_atom(_NET_WM_MOVERESIZE) ))
2495 2496
    {
        TRACE( "_NET_WM_MOVERESIZE not supported\n" );
2497
        goto failed;
2498 2499
    }

2500
    release_win_data( data );
2501
    move_resize_window( hwnd, dir );
2502
    return 0;
2503 2504 2505 2506

failed:
    release_win_data( data );
    return -1;
2507
}