window.c 43.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 20
 *
 * 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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 22 23 24
 */

#include "config.h"

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

30
#include "ts_xlib.h"
31 32
#include <X11/Xresource.h>
#include <X11/Xutil.h>
33 34 35

#include "winbase.h"
#include "wingdi.h"
36
#include "winreg.h"
37
#include "winuser.h"
38
#include "wine/unicode.h"
39

40
#include "wine/debug.h"
41 42
#include "x11drv.h"
#include "win.h"
43
#include "winpos.h"
44
#include "mwm.h"
45

46
WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
47 48 49 50

extern Pixmap X11DRV_BITMAP_Pixmap( HBITMAP );

#define HAS_DLGFRAME(style,exStyle) \
51 52 53
    (((exStyle) & WS_EX_DLGMODALFRAME) || \
     (((style) & WS_DLGFRAME) && !((style) & WS_THICKFRAME)))

54 55 56 57 58
/* X context to associate a hwnd to an X window */
XContext winContext = 0;

Atom wmProtocols = None;
Atom wmDeleteWindow = None;
59
Atom wmTakeFocus = None;
60 61 62
Atom dndProtocol = None;
Atom dndSelection = None;
Atom wmChangeState = None;
63
Atom mwmHints = None;
64
Atom kwmDockWindow = None;
65 66
Atom netwmPid = None;
Atom netwmPing = None;
67 68
Atom _kde_net_wm_system_tray_window_for = None; /* KDE 2 Final */

69 70 71
static LPCSTR whole_window_atom;
static LPCSTR client_window_atom;
static LPCSTR icon_window_atom;
72 73

/***********************************************************************
74
 *		is_window_managed
75
 *
76
 * Check if a given window should be managed
77
 */
78
inline static BOOL is_window_managed( WND *win )
79
{
80
    if (!managed_mode) return FALSE;
81 82 83 84 85 86 87 88 89 90 91
    /* tray window is always managed */
    if (win->dwExStyle & WS_EX_TRAYWINDOW) return TRUE;
    /* child windows are not managed */
    if (win->dwStyle & WS_CHILD) return FALSE;
    /* tool windows are not managed */
    if (win->dwExStyle & WS_EX_TOOLWINDOW) return FALSE;
    /* windows with caption or thick frame are managed */
    if ((win->dwStyle & WS_CAPTION) == WS_CAPTION) return TRUE;
    if (win->dwStyle & WS_THICKFRAME) return TRUE;
    /* default: not managed */
    return FALSE;
92 93 94
}


95 96 97 98 99 100 101 102
/***********************************************************************
 *		is_client_window_mapped
 *
 * Check if the X client window should be mapped
 */
inline static BOOL is_client_window_mapped( WND *win )
{
    struct x11drv_win_data *data = win->pDriverData;
103
    return !(win->dwStyle & WS_MINIMIZE) && !IsRectEmpty( &data->client_rect );
104 105 106
}


107 108 109
/***********************************************************************
 *              get_window_attributes
 *
110
 * Fill the window attributes structure for an X window.
111
 */
112
static int get_window_attributes( Display *display, WND *win, XSetWindowAttributes *attr )
113 114 115 116
{
    BOOL is_top_level = is_window_top_level( win );
    BOOL managed = is_top_level && is_window_managed( win );

117 118
    if (managed) WIN_SetExStyle( win->hwndSelf, win->dwExStyle | WS_EX_MANAGED );
    else WIN_SetExStyle( win->hwndSelf, win->dwExStyle & ~WS_EX_MANAGED );
119 120 121 122

    attr->override_redirect = !managed;
    attr->colormap          = X11DRV_PALETTE_PaletteXColormap;
    attr->save_under        = ((win->clsStyle & CS_SAVEBITS) != 0);
123
    attr->cursor            = x11drv_thread_data()->cursor;
124
    attr->event_mask        = (ExposureMask | PointerMotionMask |
125 126
                               ButtonPressMask | ButtonReleaseMask | EnterWindowMask);

127
    if (is_window_top_level( win ))
128 129
        attr->event_mask |= (KeyPressMask | KeyReleaseMask | StructureNotifyMask |
                             FocusChangeMask | KeymapStateMask);
130

131 132 133 134 135
    return (CWOverrideRedirect | CWSaveUnder | CWEventMask | CWColormap | CWCursor);
}


/***********************************************************************
136
 *              X11DRV_sync_window_style
137 138 139
 *
 * Change the X window attributes when the window style has changed.
 */
140
void X11DRV_sync_window_style( Display *display, WND *win )
141 142 143
{
    XSetWindowAttributes attr;
    int mask;
144

145 146 147 148
    wine_tsx11_lock();
    mask = get_window_attributes( display, win, &attr );
    XChangeWindowAttributes( display, get_whole_window(win), mask, &attr );
    wine_tsx11_unlock();
149
}
150

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

/***********************************************************************
 *              get_window_changes
 *
 * fill the window changes structure
 */
static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
{
    int mask = 0;

    if (old->right - old->left != new->right - new->left )
    {
        if (!(changes->width = new->right - new->left)) changes->width = 1;
        mask |= CWWidth;
    }
    if (old->bottom - old->top != new->bottom - new->top)
    {
        if (!(changes->height = new->bottom - new->top)) changes->height = 1;
        mask |= CWHeight;
    }
    if (old->left != new->left)
    {
        changes->x = new->left;
        mask |= CWX;
    }
    if (old->top != new->top)
    {
        changes->y = new->top;
        mask |= CWY;
180
    }
181 182 183 184 185 186 187 188 189 190 191 192 193
    return mask;
}


/***********************************************************************
 *              create_icon_window
 */
static Window create_icon_window( Display *display, WND *win )
{
    struct x11drv_win_data *data = win->pDriverData;
    XSetWindowAttributes attr;

    attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
194
                       ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
195 196
    attr.bit_gravity = NorthWestGravity;
    attr.backing_store = NotUseful/*WhenMapped*/;
197
    attr.colormap      = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
198

199 200 201 202 203 204
    wine_tsx11_lock();
    data->icon_window = XCreateWindow( display, root_window, 0, 0,
                                       GetSystemMetrics( SM_CXICON ),
                                       GetSystemMetrics( SM_CYICON ),
                                       0, screen_depth,
                                       InputOutput, visual,
205
                                       CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
206
    XSaveContext( display, data->icon_window, winContext, (char *)win->hwndSelf );
207 208
    wine_tsx11_unlock();

209
    TRACE( "created %lx\n", data->icon_window );
210
    SetPropA( win->hwndSelf, icon_window_atom, (HANDLE)data->icon_window );
211 212 213 214 215 216 217 218
    return data->icon_window;
}



/***********************************************************************
 *              destroy_icon_window
 */
219
inline static void destroy_icon_window( Display *display, WND *win )
220
{
221 222
    struct x11drv_win_data *data = win->pDriverData;

223
    if (!data->icon_window) return;
224 225
    if (x11drv_thread_data()->cursor_window == data->icon_window)
        x11drv_thread_data()->cursor_window = None;
226
    wine_tsx11_lock();
227 228 229
    XDeleteContext( display, data->icon_window, winContext );
    XDestroyWindow( display, data->icon_window );
    data->icon_window = 0;
230
    wine_tsx11_unlock();
231
    RemovePropA( win->hwndSelf, icon_window_atom );
232 233 234 235 236 237 238 239
}


/***********************************************************************
 *              set_icon_hints
 *
 * Set the icon wm hints
 */
240
static void set_icon_hints( Display *display, WND *wndPtr, XWMHints *hints )
241 242
{
    X11DRV_WND_DATA *data = wndPtr->pDriverData;
243
    HICON hIcon = (HICON)GetClassLongA( wndPtr->hwndSelf, GCL_HICON );
244 245 246

    if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
    if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
247 248
    data->hWMIconBitmap = 0;
    data->hWMIconMask = 0;
249

250 251
    if (!(wndPtr->dwExStyle & WS_EX_MANAGED))
    {
252
        destroy_icon_window( display, wndPtr );
253 254 255
        hints->flags &= ~(IconPixmapHint | IconMaskHint | IconWindowHint);
    }
    else if (!hIcon)
256
    {
257 258 259
        if (!data->icon_window) create_icon_window( display, wndPtr );
        hints->icon_window = data->icon_window;
        hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
    }
    else
    {
        HBITMAP hbmOrig;
        RECT rcMask;
        BITMAP bmMask;
        ICONINFO ii;
        HDC hDC;

        GetIconInfo(hIcon, &ii);

        GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
        rcMask.top    = 0;
        rcMask.left   = 0;
        rcMask.right  = bmMask.bmWidth;
        rcMask.bottom = bmMask.bmHeight;

        hDC = CreateCompatibleDC(0);
        hbmOrig = SelectObject(hDC, ii.hbmMask);
        InvertRect(hDC, &rcMask);
280
        SelectObject(hDC, ii.hbmColor);  /* force the color bitmap to x11drv mode too */
281 282 283 284 285 286 287 288
        SelectObject(hDC, hbmOrig);
        DeleteDC(hDC);

        data->hWMIconBitmap = ii.hbmColor;
        data->hWMIconMask = ii.hbmMask;

        hints->icon_pixmap = X11DRV_BITMAP_Pixmap(data->hWMIconBitmap);
        hints->icon_mask = X11DRV_BITMAP_Pixmap(data->hWMIconMask);
289
        destroy_icon_window( display, wndPtr );
290
        hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
291 292 293 294 295
    }
}


/***********************************************************************
296
 *              set_size_hints
297
 *
298
 * set the window size hints
299
 */
300
static void set_size_hints( Display *display, WND *win )
301
{
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
    XSizeHints* size_hints;
    struct x11drv_win_data *data = win->pDriverData;

    if ((size_hints = XAllocSizeHints()))
    {
        size_hints->win_gravity = StaticGravity;
        size_hints->x = data->whole_rect.left;
        size_hints->y = data->whole_rect.top;
        size_hints->flags = PWinGravity | PPosition;

        if (HAS_DLGFRAME( win->dwStyle, win->dwExStyle ))
        {
            size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
            size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
            size_hints->min_width = size_hints->max_width;
            size_hints->min_height = size_hints->max_height;
            size_hints->flags |= PMinSize | PMaxSize;
        }
        XSetWMNormalHints( display, data->whole_window, size_hints );
        XFree( size_hints );
    }
323 324 325
}


326
/***********************************************************************
327
 *              X11DRV_set_wm_hints
328 329
 *
 * Set the window manager hints for a newly-created window
330
 */
331
void X11DRV_set_wm_hints( Display *display, WND *win )
332
{
333 334 335 336 337 338 339 340 341 342 343 344 345
    struct x11drv_win_data *data = win->pDriverData;
    Window group_leader;
    XClassHint *class_hints;
    XWMHints* wm_hints;
    Atom protocols[2];
    int i;

    wine_tsx11_lock();

    /* wm protocols */
    i = 0;
    protocols[i++] = wmDeleteWindow;
    if (wmTakeFocus) protocols[i++] = wmTakeFocus;
346
    if (netwmPing) protocols[i++] = netwmPing;
347 348 349 350 351 352 353 354 355 356
    XSetWMProtocols( display, data->whole_window, protocols, i );

    /* class hints */
    if ((class_hints = XAllocClassHint()))
    {
        class_hints->res_name = "wine";
        class_hints->res_class = "Wine";
        XSetClassHint( display, data->whole_window, class_hints );
        XFree( class_hints );
    }
357

358 359 360
    /* transient for hint */
    if (win->owner)
    {
361 362 363
        Window owner_win = X11DRV_get_whole_window( win->owner );
        XSetTransientForHint( display, data->whole_window, owner_win );
        group_leader = owner_win;
364 365
    }
    else group_leader = data->whole_window;
366

367 368 369 370 371 372 373 374
    /* size hints */
    set_size_hints( display, win );

    /* systray properties (KDE only for now) */
    if (win->dwExStyle & WS_EX_TRAYWINDOW)
    {
        int val = 1;
        if (kwmDockWindow != None)
375 376
            XChangeProperty( display, data->whole_window, kwmDockWindow, kwmDockWindow,
                             32, PropModeReplace, (char*)&val, 1 );
377
        if (_kde_net_wm_system_tray_window_for != None)
378 379 380 381
            XChangeProperty( display, data->whole_window, _kde_net_wm_system_tray_window_for,
                             XA_WINDOW, 32, PropModeReplace, (char*)&data->whole_window, 1 );
    }

382 383 384 385 386 387
    /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
    XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
    /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
    i = getpid();
    XChangeProperty(display, data->whole_window, netwmPid, XA_CARDINAL, 32, PropModeReplace, (char *)&i, 1);
    
388 389 390 391 392 393 394
    if (mwmHints != None)
    {
        MwmHints mwm_hints;
        mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
        mwm_hints.functions = 0;
        if ((win->dwStyle & WS_CAPTION) == WS_CAPTION) mwm_hints.functions |= MWM_FUNC_MOVE;
        if (win->dwStyle & WS_THICKFRAME) mwm_hints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
395 396
        if (win->dwStyle & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
        if (win->dwStyle & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
397 398 399 400 401 402 403 404 405
        if (win->dwStyle & WS_SYSMENU)    mwm_hints.functions |= MWM_FUNC_CLOSE;
        mwm_hints.decorations = 0;
        if ((win->dwStyle & WS_CAPTION) == WS_CAPTION) mwm_hints.decorations |= MWM_DECOR_TITLE;
        if (win->dwExStyle & WS_EX_DLGMODALFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
        else if (win->dwStyle & WS_THICKFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
        else if ((win->dwStyle & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
        else if (win->dwStyle & WS_BORDER) mwm_hints.decorations |= MWM_DECOR_BORDER;
        else if (!(win->dwStyle & (WS_CHILD|WS_POPUP))) mwm_hints.decorations |= MWM_DECOR_BORDER;
        if (win->dwStyle & WS_SYSMENU)  mwm_hints.decorations |= MWM_DECOR_MENU;
406 407
        if (win->dwStyle & WS_MINIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MINIMIZE;
        if (win->dwStyle & WS_MAXIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MAXIMIZE;
408 409 410

        XChangeProperty( display, data->whole_window, mwmHints, mwmHints, 32,
                         PropModeReplace, (char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
411
    }
412

413
    wm_hints = XAllocWMHints();
414
    wine_tsx11_unlock();
415 416

    /* wm hints */
417
    if (wm_hints)
418 419
    {
        wm_hints->flags = InputHint | StateHint | WindowGroupHint;
420
        wm_hints->input = !(win->dwStyle & WS_DISABLED);
421 422 423 424 425 426 427 428 429 430 431

        set_icon_hints( display, win, wm_hints );

        wm_hints->initial_state = (win->dwStyle & WS_MINIMIZE) ? IconicState : NormalState;
        wm_hints->window_group = group_leader;

        wine_tsx11_lock();
        XSetWMHints( display, data->whole_window, wm_hints );
        XFree(wm_hints);
        wine_tsx11_unlock();
    }
432 433 434
}


435 436 437 438
/***********************************************************************
 *              X11DRV_set_iconic_state
 *
 * Set the X11 iconic state according to the window style.
439
 */
440
void X11DRV_set_iconic_state( WND *win )
441
{
442
    Display *display = thread_display();
443 444 445
    struct x11drv_win_data *data = win->pDriverData;
    XWMHints* wm_hints;
    BOOL iconic = IsIconic( win->hwndSelf );
446

447 448
    wine_tsx11_lock();

449 450 451
    if (iconic) XUnmapWindow( display, data->client_window );
    else if (is_client_window_mapped( win )) XMapWindow( display, data->client_window );

452 453 454 455 456 457 458 459
    if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
    wm_hints->flags |= StateHint | IconPositionHint;
    wm_hints->initial_state = iconic ? IconicState : NormalState;
    wm_hints->icon_x = win->rectWindow.left;
    wm_hints->icon_y = win->rectWindow.top;
    XSetWMHints( display, data->whole_window, wm_hints );

    if (win->dwStyle & WS_VISIBLE)
460
    {
461 462 463 464
        if (iconic)
            XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
        else
            if (!IsRectEmpty( &win->rectWindow )) XMapWindow( display, data->whole_window );
465 466
    }

467 468 469 470
    XFree(wm_hints);
    wine_tsx11_unlock();
}

471

472 473 474 475 476 477 478
/***********************************************************************
 *		X11DRV_window_to_X_rect
 *
 * Convert a rect from client to X window coordinates
 */
void X11DRV_window_to_X_rect( WND *win, RECT *rect )
{
479 480
    RECT rc;

481 482 483
    if (!(win->dwExStyle & WS_EX_MANAGED)) return;
    if (IsRectEmpty( rect )) return;

484
    rc.top = rc.bottom = rc.left = rc.right = 0;
485

486
    AdjustWindowRectEx( &rc, win->dwStyle & ~(WS_HSCROLL|WS_VSCROLL), FALSE, win->dwExStyle );
487

488 489 490 491
    rect->left   -= rc.left;
    rect->right  -= rc.right;
    rect->top    -= rc.top;
    rect->bottom -= rc.bottom;
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
    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
 */
void X11DRV_X_to_window_rect( WND *win, RECT *rect )
{
    if (!(win->dwExStyle & WS_EX_MANAGED)) return;
    if (IsRectEmpty( rect )) return;

507
    AdjustWindowRectEx( rect, win->dwStyle & ~(WS_HSCROLL|WS_VSCROLL), FALSE, win->dwExStyle );
508

509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
    if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
    if (rect->left >= rect->right) rect->right = rect->left + 1;
}


/***********************************************************************
 *		X11DRV_sync_whole_window_position
 *
 * Synchronize the X whole window position with the Windows one
 */
int X11DRV_sync_whole_window_position( Display *display, WND *win, int zorder )
{
    XWindowChanges changes;
    int mask;
    struct x11drv_win_data *data = win->pDriverData;
    RECT whole_rect = win->rectWindow;

    X11DRV_window_to_X_rect( win, &whole_rect );
    mask = get_window_changes( &changes, &data->whole_rect, &whole_rect );

    if (zorder)
    {
531
        if (is_window_top_level( win ))
532
        {
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
            /* find window that this one must be after */
            HWND prev = GetWindow( win->hwndSelf, GW_HWNDPREV );
            while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
                prev = GetWindow( prev, GW_HWNDPREV );
            if (!prev)  /* top child */
            {
                changes.stack_mode = Above;
                mask |= CWStackMode;
            }
            else
            {
                /* should use stack_mode Below but most window managers don't get it right */
                /* so move it above the next one in Z order */
                HWND next = GetWindow( win->hwndSelf, GW_HWNDNEXT );
                while (next && !(GetWindowLongW( next, GWL_STYLE ) & WS_VISIBLE))
                    next = GetWindow( next, GW_HWNDNEXT );
                if (next)
                {
                    changes.stack_mode = Above;
                    changes.sibling = X11DRV_get_whole_window(next);
                    mask |= CWStackMode | CWSibling;
                }
            }
556 557 558
        }
        else
        {
559 560 561 562 563 564 565 566 567 568 569 570
            HWND next = GetWindow( win->hwndSelf, GW_HWNDNEXT );
            if (!next)  /* bottom child */
            {
                changes.stack_mode = Below;
                mask |= CWStackMode;
            }
            else
            {
                changes.stack_mode = Above;
                changes.sibling = X11DRV_get_whole_window(next);
                mask |= CWStackMode | CWSibling;
            }
571
        }
572 573 574
    }

    data->whole_rect = whole_rect;
575

576 577
    if (mask)
    {
578
        TRACE( "setting win %lx pos %ld,%ld,%ldx%ld after %lx changes=%x\n",
579 580 581
               data->whole_window, whole_rect.left, whole_rect.top,
               whole_rect.right - whole_rect.left, whole_rect.bottom - whole_rect.top,
               changes.sibling, mask );
582
        wine_tsx11_lock();
583 584 585 586 587 588 589 590 591 592 593 594
        XSync( gdi_display, False );  /* flush graphics operations before moving the window */
        if (is_window_top_level( win ))
        {
            if (mask & (CWWidth|CWHeight)) set_size_hints( display, win );
            XReconfigureWMWindow( display, data->whole_window,
                                  DefaultScreen(display), mask, &changes );
        }
        else XConfigureWindow( display, data->whole_window, mask, &changes );
        wine_tsx11_unlock();
    }
    return mask;
}
595

596

597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
/***********************************************************************
 *		X11DRV_sync_client_window_position
 *
 * Synchronize the X client window position with the Windows one
 */
int X11DRV_sync_client_window_position( Display *display, WND *win )
{
    XWindowChanges changes;
    int mask;
    struct x11drv_win_data *data = win->pDriverData;
    RECT client_rect = win->rectClient;

    OffsetRect( &client_rect, -data->whole_rect.left, -data->whole_rect.top );

    if ((mask = get_window_changes( &changes, &data->client_rect, &client_rect )))
    {
613 614
        BOOL was_mapped = is_client_window_mapped( win );

615
        TRACE( "setting win %lx pos %ld,%ld,%ldx%ld (was %ld,%ld,%ldx%ld) after %lx changes=%x\n",
616 617 618 619 620 621 622 623 624
               data->client_window, client_rect.left, client_rect.top,
               client_rect.right - client_rect.left, client_rect.bottom - client_rect.top,
               data->client_rect.left, data->client_rect.top,
               data->client_rect.right - data->client_rect.left,
               data->client_rect.bottom - data->client_rect.top,
               changes.sibling, mask );
        data->client_rect = client_rect;
        wine_tsx11_lock();
        XSync( gdi_display, False );  /* flush graphics operations before moving the window */
625 626
        if (was_mapped && !is_client_window_mapped( win ))
            XUnmapWindow( display, data->client_window );
627
        XConfigureWindow( display, data->client_window, mask, &changes );
628 629
        if (!was_mapped && is_client_window_mapped( win ))
            XMapWindow( display, data->client_window );
630
        wine_tsx11_unlock();
631 632 633
    }
    return mask;
}
634

635

636 637 638 639 640 641 642 643 644 645 646 647
/***********************************************************************
 *		X11DRV_register_window
 *
 * Associate an X window to a HWND.
 */
void X11DRV_register_window( Display *display, HWND hwnd, struct x11drv_win_data *data )
{
    wine_tsx11_lock();
    XSaveContext( display, data->whole_window, winContext, (char *)hwnd );
    XSaveContext( display, data->client_window, winContext, (char *)hwnd );
    wine_tsx11_unlock();
}
648 649


650 651 652
/**********************************************************************
 *		create_desktop
 */
653
static void create_desktop( Display *display, WND *wndPtr )
654 655
{
    X11DRV_WND_DATA *data = wndPtr->pDriverData;
656

657 658 659 660
    wine_tsx11_lock();
    winContext     = XUniqueContext();
    wmProtocols    = XInternAtom( display, "WM_PROTOCOLS", False );
    wmDeleteWindow = XInternAtom( display, "WM_DELETE_WINDOW", False );
661
    if (use_take_focus) wmTakeFocus = XInternAtom( display, "WM_TAKE_FOCUS", False );
662 663
    dndProtocol = XInternAtom( display, "DndProtocol" , False );
    dndSelection = XInternAtom( display, "DndSelection" , False );
664 665
    wmChangeState = XInternAtom( display, "WM_CHANGE_STATE", False );
    mwmHints = XInternAtom( display, _XA_MWM_HINTS, False );
666 667
    kwmDockWindow = XInternAtom( display, "KWM_DOCKWINDOW", False );
    _kde_net_wm_system_tray_window_for = XInternAtom( display, "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR", False );
668 669
    netwmPid = XInternAtom( display, "_NET_WM_PID", False );
    netwmPing = XInternAtom( display, "_NET_WM_PING", False );
670 671
    wine_tsx11_unlock();

672 673 674 675
    whole_window_atom  = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_whole_window" ));
    client_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_client_window" ));
    icon_window_atom   = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_icon_window" ));

676
    data->whole_window = data->client_window = root_window;
677
    data->whole_rect = data->client_rect = wndPtr->rectWindow;
678

679 680
    SetPropA( wndPtr->hwndSelf, whole_window_atom, (HANDLE)root_window );
    SetPropA( wndPtr->hwndSelf, client_window_atom, (HANDLE)root_window );
681 682
    SetPropA( wndPtr->hwndSelf, "__wine_x11_visual_id", (HANDLE)XVisualIDFromVisual(visual) );

683
    if (root_window != DefaultRootWindow(display)) X11DRV_create_desktop_thread();
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
}


/**********************************************************************
 *		create_whole_window
 *
 * Create the whole X window for a given window
 */
static Window create_whole_window( Display *display, WND *win )
{
    struct x11drv_win_data *data = win->pDriverData;
    int cx, cy, mask;
    XSetWindowAttributes attr;
    Window parent;
    RECT rect;
    BOOL is_top_level = is_window_top_level( win );

    rect = win->rectWindow;
    X11DRV_window_to_X_rect( win, &rect );

    if (!(cx = rect.right - rect.left)) cx = 1;
    if (!(cy = rect.bottom - rect.top)) cy = 1;

707
    parent = X11DRV_get_client_window( win->parent );
708 709 710

    wine_tsx11_lock();

711 712 713 714 715 716 717 718
    mask = get_window_attributes( display, win, &attr );

    /* set the attributes that don't change over the lifetime of the window */
    attr.bit_gravity       = ForgetGravity;
    attr.win_gravity       = NorthWestGravity;
    attr.backing_store     = NotUseful/*WhenMapped*/;
    mask |= CWBitGravity | CWWinGravity | CWBackingStore;

719 720 721 722 723
    data->whole_rect = rect;
    data->whole_window = XCreateWindow( display, parent, rect.left, rect.top, cx, cy,
                                        0, screen_depth, InputOutput, visual,
                                        mask, &attr );

724 725 726 727 728
    if (!data->whole_window)
    {
        wine_tsx11_unlock();
        return 0;
    }
729

730 731 732 733 734 735
    if (is_top_level)
    {
        XIM xim = x11drv_thread_data()->xim;
        if (xim) data->xic = XCreateIC( xim,
                                        XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
                                        XNClientWindow, data->whole_window,
736
                                        XNFocusWindow, data->whole_window,
737 738 739
                                        0 );
    }

740 741 742 743 744 745 746 747
    /* non-maximized child must be at bottom of Z order */
    if ((win->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
    {
        XWindowChanges changes;
        changes.stack_mode = Below;
        XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
    }

748 749
    wine_tsx11_unlock();

750
    if (is_top_level) X11DRV_set_wm_hints( display, win );
751 752 753 754 755 756 757 758 759 760 761 762 763

    return data->whole_window;
}


/**********************************************************************
 *		create_client_window
 *
 * Create the client window for a given window
 */
static Window create_client_window( Display *display, WND *win )
{
    struct x11drv_win_data *data = win->pDriverData;
764
    RECT rect = data->whole_rect;
765 766 767 768 769
    XSetWindowAttributes attr;

    OffsetRect( &rect, -data->whole_rect.left, -data->whole_rect.top );
    data->client_rect = rect;

770
    attr.event_mask = (ExposureMask | PointerMotionMask |
771
                       ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
772 773 774 775
    attr.bit_gravity = (win->clsStyle & (CS_VREDRAW | CS_HREDRAW)) ?
                       ForgetGravity : NorthWestGravity;
    attr.backing_store = NotUseful/*WhenMapped*/;

776 777 778 779 780 781 782 783 784 785
    wine_tsx11_lock();
    data->client_window = XCreateWindow( display, data->whole_window, 0, 0,
                                         max( rect.right - rect.left, 1 ),
                                         max( rect.bottom - rect.top, 1 ),
                                         0, screen_depth,
                                         InputOutput, visual,
                                         CWEventMask | CWBitGravity | CWBackingStore, &attr );
    if (data->client_window && is_client_window_mapped( win ))
        XMapWindow( display, data->client_window );
    wine_tsx11_unlock();
786 787 788 789 790 791 792 793 794 795 796 797
    return data->client_window;
}


/*****************************************************************
 *		SetWindowText   (X11DRV.@)
 */
BOOL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
{
    Display *display = thread_display();
    UINT count;
    char *buffer;
798
    char *utf8_buffer;
799
    Window win;
800
    XTextProperty prop;
801

802
    if ((win = X11DRV_get_whole_window( hwnd )))
803 804
    {
        /* allocate new buffer for window text */
805
        count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
806
        if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count )))
807
        {
808 809 810
            ERR("Not enough memory for window text\n");
            return FALSE;
        }
811
        WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
812

813 814 815 816 817 818 819 820
        count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
        if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
        {
            ERR("Not enough memory for window text in UTF-8\n");
            return FALSE;
        }
        WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);

821
        wine_tsx11_lock();
822 823 824 825 826 827
	if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
	{
	    XSetWMName( display, win, &prop );
	    XSetWMIconName( display, win, &prop );
	    XFree( prop.value );
	}
828 829 830 831 832 833 834 835 836 837
        /*
        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,
            XInternAtom(display, "_NET_WM_NAME", False),
            XInternAtom(display, "UTF8_STRING", False),
            8, PropModeReplace, (unsigned char *) utf8_buffer,
            count);
838
        wine_tsx11_unlock();
839

840
        HeapFree( GetProcessHeap(), 0, utf8_buffer );
841
        HeapFree( GetProcessHeap(), 0, buffer );
842 843 844 845 846 847 848 849 850 851
    }
    return TRUE;
}


/***********************************************************************
 *		DestroyWindow   (X11DRV.@)
 */
BOOL X11DRV_DestroyWindow( HWND hwnd )
{
852 853
    struct x11drv_thread_data *thread_data = x11drv_thread_data();
    Display *display = thread_data->display;
854
    WND *wndPtr = WIN_GetPtr( hwnd );
855 856
    X11DRV_WND_DATA *data = wndPtr->pDriverData;

857 858 859
    if (!data) goto done;

    if (data->whole_window)
860
    {
861
        TRACE( "win %p xwin %lx/%lx\n", hwnd, data->whole_window, data->client_window );
862
        if (thread_data->cursor_window == data->whole_window) thread_data->cursor_window = None;
863
        if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
864 865
        wine_tsx11_lock();
        XSync( gdi_display, False );  /* flush any reference to this drawable in GDI queue */
866 867 868
        XDeleteContext( display, data->whole_window, winContext );
        XDeleteContext( display, data->client_window, winContext );
        XDestroyWindow( display, data->whole_window );  /* this destroys client too */
869 870 871 872 873
        if (data->xic)
        {
            XUnsetICFocus( data->xic );
            XDestroyIC( data->xic );
        }
874
        destroy_icon_window( display, wndPtr );
875
        wine_tsx11_unlock();
876
    }
877 878 879

    if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
    if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
880 881
    HeapFree( GetProcessHeap(), 0, data );
    wndPtr->pDriverData = NULL;
882
 done:
883
    WIN_ReleasePtr( wndPtr );
884 885 886 887
    return TRUE;
}


888 889 890
/**********************************************************************
 *		CreateWindow   (X11DRV.@)
 */
891
BOOL X11DRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
892
{
893
    HWND hwndLinkAfter;
894 895 896
    Display *display = thread_display();
    WND *wndPtr;
    struct x11drv_win_data *data;
897
    RECT rect;
898
    CBT_CREATEWNDA cbtc;
899 900
    BOOL ret = FALSE;

901 902 903 904 905 906 907 908 909 910 911
    if (cs->cx > 65535)
    {
        ERR( "invalid window width %d\n", cs->cx );
        cs->cx = 65535;
    }
    if (cs->cy > 65535)
    {
        ERR( "invalid window height %d\n", cs->cx );
        cs->cy = 65535;
    }

912 913 914 915
    if (!(data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data)))) return FALSE;
    data->whole_window  = 0;
    data->client_window = 0;
    data->icon_window   = 0;
916
    data->xic           = 0;
917 918 919
    data->hWMIconBitmap = 0;
    data->hWMIconMask   = 0;

920
    wndPtr = WIN_GetPtr( hwnd );
921
    wndPtr->pDriverData = data;
922

923 924 925 926
    /* initialize the dimensions before sending WM_GETMINMAXINFO */
    SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
    WIN_SetRectangles( hwnd, &rect, &rect );

927 928
    if (!wndPtr->parent)
    {
929
        create_desktop( display, wndPtr );
930
        WIN_ReleasePtr( wndPtr );
931 932 933 934 935 936 937
        return TRUE;
    }

    if (!create_whole_window( display, wndPtr )) goto failed;
    if (!create_client_window( display, wndPtr )) goto failed;
    TSXSync( display, False );

938 939
    SetPropA( hwnd, whole_window_atom, (HANDLE)data->whole_window );
    SetPropA( hwnd, client_window_atom, (HANDLE)data->client_window );
940

941 942
    /* Call the WH_CBT hook */

943
    hwndLinkAfter = ((cs->style & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
944

945 946 947
    cbtc.lpcs = cs;
    cbtc.hwndInsertAfter = hwndLinkAfter;
    if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode ))
948
    {
949 950
        TRACE("CBT-hook returned !0\n");
        goto failed;
951 952
    }

953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
    /* Send the WM_GETMINMAXINFO message and fix the size if needed */
    if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
    {
        POINT maxSize, maxPos, minTrack, maxTrack;

        WIN_ReleasePtr( wndPtr );
        WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
        if (maxSize.x < cs->cx) cs->cx = maxSize.x;
        if (maxSize.y < cs->cy) cs->cy = maxSize.y;
        if (cs->cx < minTrack.x ) cs->cx = minTrack.x;
        if (cs->cy < minTrack.y ) cs->cy = minTrack.y;
        if (cs->cx < 0) cs->cx = 0;
        if (cs->cy < 0) cs->cy = 0;

        if (!(wndPtr = WIN_GetPtr( hwnd ))) return FALSE;
        SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
        WIN_SetRectangles( hwnd, &rect, &rect );
        X11DRV_sync_whole_window_position( display, wndPtr, 0 );
    }
    WIN_ReleasePtr( wndPtr );

974
    /* send WM_NCCREATE */
975
    TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cs->cx, cs->cy );
976
    if (unicode)
977 978 979
        ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
    else
        ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
980 981
    if (!ret)
    {
982
        WARN("aborted by WM_xxCREATE!\n");
983 984
        return FALSE;
    }
985

986
    if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
987

988
    X11DRV_sync_window_style( display, wndPtr );
989

990 991
    /* send WM_NCCALCSIZE */
    rect = wndPtr->rectWindow;
992
    WIN_ReleasePtr( wndPtr );
993
    SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&rect );
994 995

    if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
996
    if (rect.left > rect.right || rect.top > rect.bottom) rect = wndPtr->rectWindow;
997
    WIN_SetRectangles( hwnd, &wndPtr->rectWindow, &rect );
998 999
    X11DRV_sync_client_window_position( display, wndPtr );
    X11DRV_register_window( display, hwnd, data );
1000

1001
    TRACE( "win %p window %ld,%ld,%ld,%ld client %ld,%ld,%ld,%ld whole %ld,%ld,%ld,%ld X client %ld,%ld,%ld,%ld xwin %x/%x\n",
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
           hwnd, wndPtr->rectWindow.left, wndPtr->rectWindow.top,
           wndPtr->rectWindow.right, wndPtr->rectWindow.bottom,
           wndPtr->rectClient.left, wndPtr->rectClient.top,
           wndPtr->rectClient.right, wndPtr->rectClient.bottom,
           data->whole_rect.left, data->whole_rect.top,
           data->whole_rect.right, data->whole_rect.bottom,
           data->client_rect.left, data->client_rect.top,
           data->client_rect.right, data->client_rect.bottom,
           (unsigned int)data->whole_window, (unsigned int)data->client_window );

    if ((wndPtr->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
1013
        WIN_LinkWindow( hwnd, wndPtr->parent, HWND_BOTTOM );
1014
    else
1015
        WIN_LinkWindow( hwnd, wndPtr->parent, HWND_TOP );
1016

1017
    WIN_ReleasePtr( wndPtr );
1018

1019
    if (unicode)
1020 1021 1022 1023 1024 1025 1026
        ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
    else
        ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);

    if (!ret)
    {
        WIN_UnlinkWindow( hwnd );
1027
        return FALSE;
1028 1029 1030 1031
    }

    /* Send the size messages */

1032
    if (!(wndPtr = WIN_FindWndPtr(hwnd))) return FALSE;
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
    if (!(wndPtr->flags & WIN_NEED_SIZE))
    {
        /* send it anyway */
        if (((wndPtr->rectClient.right-wndPtr->rectClient.left) <0)
            ||((wndPtr->rectClient.bottom-wndPtr->rectClient.top)<0))
            WARN("sending bogus WM_SIZE message 0x%08lx\n",
                 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
                          wndPtr->rectClient.bottom-wndPtr->rectClient.top));
        SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
                      MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
                               wndPtr->rectClient.bottom-wndPtr->rectClient.top));
        SendMessageW( hwnd, WM_MOVE, 0,
                      MAKELONG( wndPtr->rectClient.left, wndPtr->rectClient.top ) );
    }

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

    if (wndPtr->dwStyle & (WS_MINIMIZE | WS_MAXIMIZE))
    {
        extern UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect ); /*FIXME*/

        RECT newPos;
        UINT swFlag = (wndPtr->dwStyle & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1056
        WIN_SetStyle( hwnd, wndPtr->dwStyle & ~(WS_MAXIMIZE | WS_MINIMIZE) );
1057 1058 1059 1060 1061 1062 1063 1064
        WINPOS_MinMaximize( hwnd, swFlag, &newPos );
        swFlag = ((wndPtr->dwStyle & WS_CHILD) || GetActiveWindow())
            ? SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED
            : SWP_NOZORDER | SWP_FRAMECHANGED;
        SetWindowPos( hwnd, 0, newPos.left, newPos.top,
                      newPos.right, newPos.bottom, swFlag );
    }

1065 1066 1067 1068
    /* if the window was made visible set create struct flag so that
     * we do a proper ShowWindow later on */
    if (wndPtr->dwStyle & WS_VISIBLE) cs->style |= WS_VISIBLE;

1069
    WIN_ReleaseWndPtr( wndPtr );
1070 1071 1072 1073
    return TRUE;


 failed:
1074 1075
    X11DRV_DestroyWindow( hwnd );
    if (wndPtr) WIN_ReleasePtr( wndPtr );
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
    return FALSE;
}


/***********************************************************************
 *		X11DRV_get_client_window
 *
 * Return the X window associated with the client area of a window
 */
Window X11DRV_get_client_window( HWND hwnd )
{
    Window ret = 0;
1088 1089 1090
    WND *win = WIN_GetPtr( hwnd );

    if (win == WND_OTHER_PROCESS)
1091
        return (Window)GetPropA( hwnd, client_window_atom );
1092

1093 1094 1095 1096
    if (win)
    {
        struct x11drv_win_data *data = win->pDriverData;
        ret = data->client_window;
1097
        WIN_ReleasePtr( win );
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
    }
    return ret;
}


/***********************************************************************
 *		X11DRV_get_whole_window
 *
 * Return the X window associated with the full area of a window
 */
Window X11DRV_get_whole_window( HWND hwnd )
{
    Window ret = 0;
1111 1112 1113
    WND *win = WIN_GetPtr( hwnd );

    if (win == WND_OTHER_PROCESS)
1114
        return (Window)GetPropA( hwnd, whole_window_atom );
1115

1116 1117 1118 1119
    if (win)
    {
        struct x11drv_win_data *data = win->pDriverData;
        ret = data->whole_window;
1120
        WIN_ReleasePtr( win );
1121 1122 1123 1124 1125
    }
    return ret;
}


1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
/***********************************************************************
 *		X11DRV_get_ic
 *
 * Return the X input context associated with a window
 */
XIC X11DRV_get_ic( HWND hwnd )
{
    XIC ret = 0;
    WND *win = WIN_GetPtr( hwnd );

    if (win && win != WND_OTHER_PROCESS)
    {
        struct x11drv_win_data *data = win->pDriverData;
        ret = data->xic;
        WIN_ReleasePtr( win );
    }
    return ret;
}


1146 1147 1148 1149 1150
/*****************************************************************
 *		SetParent   (X11DRV.@)
 */
HWND X11DRV_SetParent( HWND hwnd, HWND parent )
{
1151
    Display *display = thread_display();
1152 1153 1154 1155 1156
    WND *wndPtr;
    HWND retvalue;

    /* Windows hides the window first, then shows it again
     * including the WM_SHOWWINDOW messages and all */
1157 1158 1159 1160
    BOOL was_visible = ShowWindow( hwnd, SW_HIDE );

    if (!IsWindow( parent )) return 0;
    if (!(wndPtr = WIN_GetPtr(hwnd)) || wndPtr == WND_OTHER_PROCESS) return 0;
1161

1162
    retvalue = wndPtr->parent;  /* old parent */
1163
    if (parent != retvalue)
1164
    {
1165 1166
        struct x11drv_win_data *data = wndPtr->pDriverData;

1167
        WIN_LinkWindow( hwnd, parent, HWND_TOP );
1168

1169
        if (parent != GetDesktopWindow()) /* a child window */
1170
        {
1171
            if (!(wndPtr->dwStyle & WS_CHILD))
1172
            {
1173 1174
                HMENU menu = (HMENU)SetWindowLongW( hwnd, GWL_ID, 0 );
                if (menu) DestroyMenu( menu );
1175 1176
            }
        }
1177

1178
        if (is_window_top_level( wndPtr )) X11DRV_set_wm_hints( display, wndPtr );
1179
        wine_tsx11_lock();
1180
        X11DRV_sync_window_style( display, wndPtr );
1181
        XReparentWindow( display, data->whole_window, X11DRV_get_client_window(parent),
1182 1183
                         data->whole_rect.left, data->whole_rect.top );
        wine_tsx11_unlock();
1184
    }
1185
    WIN_ReleasePtr( wndPtr );
1186 1187 1188

    /* SetParent additionally needs to make hwnd the topmost window
       in the x-order and send the expected WM_WINDOWPOSCHANGING and
1189
       WM_WINDOWPOSCHANGED notification messages.
1190 1191
    */
    SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0,
1192
                  SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | (was_visible ? SWP_SHOWWINDOW : 0) );
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
    /* FIXME: a WM_MOVE is also generated (in the DefWindowProc handler
     * for WM_WINDOWPOSCHANGED) in Windows, should probably remove SWP_NOMOVE */

    return retvalue;
}


/*****************************************************************
 *		SetFocus   (X11DRV.@)
 *
 * Set the X focus.
 * Explicit colormap management seems to work only with OLVWM.
 */
void X11DRV_SetFocus( HWND hwnd )
{
1208
    Display *display = thread_display();
1209 1210 1211 1212 1213
    XWindowAttributes win_attr;
    Window win;

    /* Only mess with the X focus if there's */
    /* no desktop window and if the window is not managed by the WM. */
1214
    if (root_window != DefaultRootWindow(display)) return;
1215 1216 1217 1218 1219

    if (!hwnd)  /* If setting the focus to 0, uninstall the colormap */
    {
        if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
            TSXUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1220
        return;
1221
    }
1222 1223 1224 1225 1226 1227 1228 1229 1230

    hwnd = GetAncestor( hwnd, GA_ROOT );
    if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_MANAGED) return;
    if (!(win = X11DRV_get_whole_window( hwnd ))) return;

    /* Set X focus and install colormap */
    wine_tsx11_lock();
    if (XGetWindowAttributes( display, win, &win_attr ) &&
        (win_attr.map_state == IsViewable))
1231
    {
1232
        /* If window is not viewable, don't change anything */
1233

1234 1235 1236 1237 1238 1239 1240 1241
        /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
        /* FIXME: this is not entirely correct */
        XSetInputFocus( display, win, RevertToParent,
                        /*CurrentTime*/ GetMessageTime() + X11DRV_server_startticks );
        if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
            XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
    }
    wine_tsx11_unlock();
1242 1243 1244 1245
}


/**********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
1246
 *		SetWindowIcon (X11DRV.@)
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
 *
 * hIcon or hIconSm has changed (or is being initialised for the
 * first time). Complete the X11 driver-specific initialisation
 * and set the window hints.
 *
 * This is not entirely correct, may need to create
 * an icon window and set the pixmap as a background
 */
HICON X11DRV_SetWindowIcon( HWND hwnd, HICON icon, BOOL small )
{
1257
    WND *wndPtr;
1258
    Display *display = thread_display();
1259
    HICON old = (HICON)SetClassLongW(hwnd, small ? GCL_HICONSM : GCL_HICON, (LONG)icon );
1260 1261 1262 1263

    SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOSIZE |
                  SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );

1264 1265
    if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return old;

1266 1267
    if (wndPtr->dwExStyle & WS_EX_MANAGED)
    {
1268
        Window win = get_whole_window(wndPtr);
1269
        XWMHints* wm_hints;
1270

1271 1272 1273
        wine_tsx11_lock();
        if (!(wm_hints = XGetWMHints( display, win ))) wm_hints = XAllocWMHints();
        wine_tsx11_unlock();
1274 1275
        if (wm_hints)
        {
1276
            set_icon_hints( display, wndPtr, wm_hints );
1277 1278 1279 1280
            wine_tsx11_lock();
            XSetWMHints( display, win, wm_hints );
            XFree( wm_hints );
            wine_tsx11_unlock();
1281 1282
        }
    }
1283
    WIN_ReleasePtr( wndPtr );
1284 1285
    return old;
}