Only create an X window for top-level windows, not for child windows.
[wine] / dlls / x11drv / window.c
1 /*
2  * Window related functions
3  *
4  * Copyright 1993, 1994, 1995, 1996, 2001 Alexandre Julliard
5  * Copyright 1993 David Metcalfe
6  * Copyright 1995, 1996 Alex Korobka
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "config.h"
24
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30
31 #include <X11/Xlib.h>
32 #include <X11/Xresource.h>
33 #include <X11/Xutil.h>
34
35 #include "windef.h"
36 #include "winbase.h"
37 #include "wingdi.h"
38 #include "winreg.h"
39 #include "winuser.h"
40 #include "wine/unicode.h"
41
42 #include "wine/debug.h"
43 #include "x11drv.h"
44 #include "win.h"
45 #include "winpos.h"
46 #include "mwm.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
49
50 /* X context to associate a hwnd to an X window */
51 XContext winContext = 0;
52
53 /* X context to associate a struct x11drv_win_data to an hwnd */
54 static XContext win_data_context;
55
56 Atom X11DRV_Atoms[NB_XATOMS - FIRST_XATOM];
57
58 static const char * const atom_names[NB_XATOMS - FIRST_XATOM] =
59 {
60     "CLIPBOARD",
61     "COMPOUND_TEXT",
62     "MULTIPLE",
63     "SELECTION_DATA",
64     "TARGETS",
65     "TEXT",
66     "UTF8_STRING",
67     "RAW_ASCENT",
68     "RAW_DESCENT",
69     "RAW_CAP_HEIGHT",
70     "WM_PROTOCOLS",
71     "WM_DELETE_WINDOW",
72     "WM_TAKE_FOCUS",
73     "KWM_DOCKWINDOW",
74     "DndProtocol",
75     "DndSelection",
76     "_MOTIF_WM_HINTS",
77     "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR",
78     "_NET_WM_MOVERESIZE",
79     "_NET_WM_PID",
80     "_NET_WM_PING",
81     "_NET_WM_NAME",
82     "_NET_WM_WINDOW_TYPE",
83     "_NET_WM_WINDOW_TYPE_UTILITY",
84     "XdndAware",
85     "XdndEnter",
86     "XdndPosition",
87     "XdndStatus",
88     "XdndLeave",
89     "XdndFinished",
90     "XdndDrop",
91     "XdndActionCopy",
92     "XdndActionMove",
93     "XdndActionLink",
94     "XdndActionAsk",
95     "XdndActionPrivate",
96     "XdndSelection",
97     "XdndTarget",
98     "XdndTypeList",
99     "WCF_DIB",
100     "image/gif",
101     "text/html",
102     "text/plain",
103     "text/rtf",
104     "text/richtext"
105 };
106
107 static LPCSTR whole_window_atom;
108 static LPCSTR icon_window_atom;
109 static LPCSTR client_offset_atom;
110
111 /***********************************************************************
112  *              is_window_managed
113  *
114  * Check if a given window should be managed
115  */
116 inline static BOOL is_window_managed( HWND hwnd )
117 {
118     DWORD style, ex_style;
119
120     if (!managed_mode) return FALSE;
121     /* tray window is always managed */
122     ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
123     if (ex_style & WS_EX_TRAYWINDOW) return TRUE;
124     /* child windows are not managed */
125     style = GetWindowLongW( hwnd, GWL_STYLE );
126     if (style & WS_CHILD) return FALSE;
127     /* windows with caption are managed */
128     if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
129     /* tool windows are not managed  */
130     if (ex_style & WS_EX_TOOLWINDOW) return FALSE;
131     /* windows with thick frame are managed */
132     if (style & WS_THICKFRAME) return TRUE;
133     /* application windows are managed */
134     if (ex_style & WS_EX_APPWINDOW) return TRUE;
135     /* full-screen popup windows are managed */
136     if (style & WS_POPUP)
137     {
138         RECT rect;
139         GetWindowRect( hwnd, &rect );
140         if ((rect.right - rect.left) == screen_width && (rect.bottom - rect.top) == screen_height)
141             return TRUE;
142     }
143     /* default: not managed */
144     return FALSE;
145 }
146
147
148 /***********************************************************************
149  *              X11DRV_is_window_rect_mapped
150  *
151  * Check if the X whole window should be mapped based on its rectangle
152  */
153 BOOL X11DRV_is_window_rect_mapped( const RECT *rect )
154 {
155     /* don't map if rect is empty */
156     if (IsRectEmpty( rect )) return FALSE;
157
158     /* don't map if rect is off-screen */
159     if (rect->left >= (int)screen_width || rect->top >= (int)screen_height) return FALSE;
160     if (rect->right < 0 || rect->bottom < 0) return FALSE;
161
162     return TRUE;
163 }
164
165
166 /***********************************************************************
167  *              get_window_attributes
168  *
169  * Fill the window attributes structure for an X window.
170  */
171 static int get_window_attributes( struct x11drv_win_data *data, XSetWindowAttributes *attr )
172 {
173     BOOL managed = !using_wine_desktop && is_window_managed( data->hwnd );
174     DWORD ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
175
176     if (managed) WIN_SetExStyle( data->hwnd, ex_style | WS_EX_MANAGED );
177     else WIN_SetExStyle( data->hwnd, ex_style & ~WS_EX_MANAGED );
178
179     attr->override_redirect = !managed;
180     attr->colormap          = X11DRV_PALETTE_PaletteXColormap;
181     attr->save_under        = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
182     attr->cursor            = x11drv_thread_data()->cursor;
183     attr->event_mask        = (ExposureMask | PointerMotionMask |
184                                ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
185                                KeyPressMask | KeyReleaseMask | StructureNotifyMask |
186                                FocusChangeMask | KeymapStateMask);
187
188     return (CWOverrideRedirect | CWSaveUnder | CWEventMask | CWColormap | CWCursor);
189 }
190
191
192 /***********************************************************************
193  *              X11DRV_sync_window_style
194  *
195  * Change the X window attributes when the window style has changed.
196  */
197 void X11DRV_sync_window_style( Display *display, struct x11drv_win_data *data )
198 {
199     XSetWindowAttributes attr;
200     int mask = get_window_attributes( data, &attr );
201
202     wine_tsx11_lock();
203     XChangeWindowAttributes( display, data->whole_window, mask, &attr );
204     wine_tsx11_unlock();
205 }
206
207
208 /***********************************************************************
209  *              get_window_changes
210  *
211  * fill the window changes structure
212  */
213 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
214 {
215     int mask = 0;
216
217     if (old->right - old->left != new->right - new->left )
218     {
219         if (!(changes->width = new->right - new->left)) changes->width = 1;
220         mask |= CWWidth;
221     }
222     if (old->bottom - old->top != new->bottom - new->top)
223     {
224         if (!(changes->height = new->bottom - new->top)) changes->height = 1;
225         mask |= CWHeight;
226     }
227     if (old->left != new->left)
228     {
229         changes->x = new->left;
230         mask |= CWX;
231     }
232     if (old->top != new->top)
233     {
234         changes->y = new->top;
235         mask |= CWY;
236     }
237     return mask;
238 }
239
240
241 /***********************************************************************
242  *              create_icon_window
243  */
244 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
245 {
246     XSetWindowAttributes attr;
247
248     attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
249                        ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
250     attr.bit_gravity = NorthWestGravity;
251     attr.backing_store = NotUseful/*WhenMapped*/;
252     attr.colormap      = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
253
254     wine_tsx11_lock();
255     data->icon_window = XCreateWindow( display, root_window, 0, 0,
256                                        GetSystemMetrics( SM_CXICON ),
257                                        GetSystemMetrics( SM_CYICON ),
258                                        0, screen_depth,
259                                        InputOutput, visual,
260                                        CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
261     XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
262     wine_tsx11_unlock();
263
264     TRACE( "created %lx\n", data->icon_window );
265     SetPropA( data->hwnd, icon_window_atom, (HANDLE)data->icon_window );
266     return data->icon_window;
267 }
268
269
270
271 /***********************************************************************
272  *              destroy_icon_window
273  */
274 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
275 {
276     if (!data->icon_window) return;
277     if (x11drv_thread_data()->cursor_window == data->icon_window)
278         x11drv_thread_data()->cursor_window = None;
279     wine_tsx11_lock();
280     XDeleteContext( display, data->icon_window, winContext );
281     XDestroyWindow( display, data->icon_window );
282     data->icon_window = 0;
283     wine_tsx11_unlock();
284     RemovePropA( data->hwnd, icon_window_atom );
285 }
286
287
288 /***********************************************************************
289  *              set_icon_hints
290  *
291  * Set the icon wm hints
292  */
293 static void set_icon_hints( Display *display, struct x11drv_win_data *data,
294                             XWMHints *hints, HICON hIcon, DWORD ex_style )
295 {
296     if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
297     if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
298     data->hWMIconBitmap = 0;
299     data->hWMIconMask = 0;
300
301     if (!(ex_style & WS_EX_MANAGED))
302     {
303         destroy_icon_window( display, data );
304         hints->flags &= ~(IconPixmapHint | IconMaskHint | IconWindowHint);
305     }
306     else if (!hIcon)
307     {
308         if (!data->icon_window) create_icon_window( display, data );
309         hints->icon_window = data->icon_window;
310         hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
311     }
312     else
313     {
314         HBITMAP hbmOrig;
315         RECT rcMask;
316         BITMAP bmMask;
317         ICONINFO ii;
318         HDC hDC;
319
320         GetIconInfo(hIcon, &ii);
321
322         GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
323         rcMask.top    = 0;
324         rcMask.left   = 0;
325         rcMask.right  = bmMask.bmWidth;
326         rcMask.bottom = bmMask.bmHeight;
327
328         hDC = CreateCompatibleDC(0);
329         hbmOrig = SelectObject(hDC, ii.hbmMask);
330         InvertRect(hDC, &rcMask);
331         SelectObject(hDC, ii.hbmColor);  /* force the color bitmap to x11drv mode too */
332         SelectObject(hDC, hbmOrig);
333         DeleteDC(hDC);
334
335         data->hWMIconBitmap = ii.hbmColor;
336         data->hWMIconMask = ii.hbmMask;
337
338         hints->icon_pixmap = X11DRV_BITMAP_Pixmap(data->hWMIconBitmap);
339         hints->icon_mask = X11DRV_BITMAP_Pixmap(data->hWMIconMask);
340         destroy_icon_window( display, data );
341         hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
342     }
343 }
344
345
346 /***********************************************************************
347  *              set_size_hints
348  *
349  * set the window size hints
350  */
351 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
352 {
353     XSizeHints* size_hints;
354
355     if ((size_hints = XAllocSizeHints()))
356     {
357         size_hints->win_gravity = StaticGravity;
358         size_hints->x = data->whole_rect.left;
359         size_hints->y = data->whole_rect.top;
360         size_hints->flags = PWinGravity | PPosition;
361
362         if ( !(style & WS_THICKFRAME) )
363         {
364             size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
365             size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
366             size_hints->min_width = size_hints->max_width;
367             size_hints->min_height = size_hints->max_height;
368             size_hints->flags |= PMinSize | PMaxSize;
369         }
370         XSetWMNormalHints( display, data->whole_window, size_hints );
371         XFree( size_hints );
372     }
373 }
374
375
376 /***********************************************************************
377  *              X11DRV_set_wm_hints
378  *
379  * Set the window manager hints for a newly-created window
380  */
381 void X11DRV_set_wm_hints( Display *display, struct x11drv_win_data *data )
382 {
383     Window group_leader;
384     XClassHint *class_hints;
385     XWMHints* wm_hints;
386     Atom protocols[3];
387     MwmHints mwm_hints;
388     Atom dndVersion = 4;
389     int i;
390     DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
391     DWORD ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
392     HWND owner = GetWindow( data->hwnd, GW_OWNER );
393
394     /* transient for hint */
395     if (owner)
396     {
397         Window owner_win = X11DRV_get_whole_window( owner );
398         wine_tsx11_lock();
399         XSetTransientForHint( display, data->whole_window, owner_win );
400         wine_tsx11_unlock();
401         group_leader = owner_win;
402     }
403     else group_leader = data->whole_window;
404
405     wine_tsx11_lock();
406
407     /* wm protocols */
408     i = 0;
409     protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
410     protocols[i++] = x11drv_atom(_NET_WM_PING);
411     if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
412     XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
413                      XA_ATOM, 32, PropModeReplace, (char *)protocols, i );
414
415     /* class hints */
416     if ((class_hints = XAllocClassHint()))
417     {
418         class_hints->res_name = "wine";
419         class_hints->res_class = "Wine";
420         XSetClassHint( display, data->whole_window, class_hints );
421         XFree( class_hints );
422     }
423
424     /* size hints */
425     set_size_hints( display, data, style );
426
427     /* systray properties (KDE only for now) */
428     if (ex_style & WS_EX_TRAYWINDOW)
429     {
430         int val = 1;
431         XChangeProperty( display, data->whole_window, x11drv_atom(KWM_DOCKWINDOW),
432                          x11drv_atom(KWM_DOCKWINDOW), 32, PropModeReplace, (char*)&val, 1 );
433         XChangeProperty( display, data->whole_window, x11drv_atom(_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR),
434                          XA_WINDOW, 32, PropModeReplace, (char*)&data->whole_window, 1 );
435     }
436
437     /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
438     XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
439     /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
440     i = getpid();
441     XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
442                     XA_CARDINAL, 32, PropModeReplace, (char *)&i, 1);
443
444    /* map WS_EX_TOOLWINDOW to _NET_WM_WINDOW_TYPE_UTILITY */
445    if (ex_style & WS_EX_TOOLWINDOW)
446    {
447       Atom a = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
448       XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
449                       XA_ATOM, 32, PropModeReplace, (char*)&a, 1);
450    }
451
452     mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
453     mwm_hints.functions = 0;
454     if ((style & WS_CAPTION) == WS_CAPTION) mwm_hints.functions |= MWM_FUNC_MOVE;
455     if (style & WS_THICKFRAME) mwm_hints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
456     if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
457     if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
458     if (style & WS_SYSMENU)    mwm_hints.functions |= MWM_FUNC_CLOSE;
459     mwm_hints.decorations = 0;
460     if ((style & WS_CAPTION) == WS_CAPTION) mwm_hints.decorations |= MWM_DECOR_TITLE;
461     if (ex_style & WS_EX_DLGMODALFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
462     else if (style & WS_THICKFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
463     else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
464     else if (style & WS_BORDER) mwm_hints.decorations |= MWM_DECOR_BORDER;
465     else if (!(style & (WS_CHILD|WS_POPUP))) mwm_hints.decorations |= MWM_DECOR_BORDER;
466     if (style & WS_SYSMENU)  mwm_hints.decorations |= MWM_DECOR_MENU;
467     if (style & WS_MINIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MINIMIZE;
468     if (style & WS_MAXIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MAXIMIZE;
469
470     XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
471                      x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
472                      (char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
473
474     XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
475                      XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
476
477     wm_hints = XAllocWMHints();
478     wine_tsx11_unlock();
479
480     /* wm hints */
481     if (wm_hints)
482     {
483         wm_hints->flags = InputHint | StateHint | WindowGroupHint;
484         wm_hints->input = !(style & WS_DISABLED);
485
486         set_icon_hints( display, data, wm_hints,
487                         (HICON)GetClassLongW( data->hwnd, GCL_HICON ), ex_style );
488
489         wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
490         wm_hints->window_group = group_leader;
491
492         wine_tsx11_lock();
493         XSetWMHints( display, data->whole_window, wm_hints );
494         XFree(wm_hints);
495         wine_tsx11_unlock();
496     }
497 }
498
499
500 /***********************************************************************
501  *              X11DRV_set_iconic_state
502  *
503  * Set the X11 iconic state according to the window style.
504  */
505 void X11DRV_set_iconic_state( HWND hwnd )
506 {
507     Display *display = thread_display();
508     struct x11drv_win_data *data;
509     RECT rect;
510     XWMHints* wm_hints;
511     DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
512     BOOL iconic = (style & WS_MINIMIZE) != 0;
513
514     if (!(data = X11DRV_get_win_data( hwnd ))) return;
515     if (!data->whole_window) return;
516
517     GetWindowRect( hwnd, &rect );
518
519     wine_tsx11_lock();
520
521     if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
522     wm_hints->flags |= StateHint | IconPositionHint;
523     wm_hints->initial_state = iconic ? IconicState : NormalState;
524     wm_hints->icon_x = rect.left;
525     wm_hints->icon_y = rect.top;
526     XSetWMHints( display, data->whole_window, wm_hints );
527
528     if (style & WS_VISIBLE)
529     {
530         if (iconic)
531             XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
532         else
533             if (X11DRV_is_window_rect_mapped( &rect ))
534                 XMapWindow( display, data->whole_window );
535     }
536
537     XFree(wm_hints);
538     wine_tsx11_unlock();
539 }
540
541
542 /***********************************************************************
543  *              X11DRV_window_to_X_rect
544  *
545  * Convert a rect from client to X window coordinates
546  */
547 void X11DRV_window_to_X_rect( HWND hwnd, RECT *rect )
548 {
549     RECT rc;
550     DWORD ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
551
552     if (!(ex_style & WS_EX_MANAGED)) return;
553     if (IsRectEmpty( rect )) return;
554
555     rc.top = rc.bottom = rc.left = rc.right = 0;
556
557     AdjustWindowRectEx( &rc, GetWindowLongW(hwnd, GWL_STYLE) & ~(WS_HSCROLL|WS_VSCROLL),
558                         FALSE, ex_style );
559
560     rect->left   -= rc.left;
561     rect->right  -= rc.right;
562     rect->top    -= rc.top;
563     rect->bottom -= rc.bottom;
564     if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
565     if (rect->left >= rect->right) rect->right = rect->left + 1;
566 }
567
568
569 /***********************************************************************
570  *              X11DRV_X_to_window_rect
571  *
572  * Opposite of X11DRV_window_to_X_rect
573  */
574 void X11DRV_X_to_window_rect( HWND hwnd, RECT *rect )
575 {
576     DWORD ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
577
578     if (!(ex_style & WS_EX_MANAGED)) return;
579     if (IsRectEmpty( rect )) return;
580
581     AdjustWindowRectEx( rect, GetWindowLongW(hwnd, GWL_STYLE) & ~(WS_HSCROLL|WS_VSCROLL),
582                         FALSE, ex_style );
583
584     if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
585     if (rect->left >= rect->right) rect->right = rect->left + 1;
586 }
587
588
589 /***********************************************************************
590  *              X11DRV_sync_window_position
591  *
592  * Synchronize the X window position with the Windows one
593  */
594 void X11DRV_sync_window_position( Display *display, struct x11drv_win_data *data,
595                                   UINT swp_flags, const RECT *new_client_rect )
596 {
597     XWindowChanges changes;
598     int mask;
599     RECT old_whole_rect;
600
601     old_whole_rect = data->whole_rect;
602     data->whole_rect = data->window_rect;
603     X11DRV_window_to_X_rect( data->hwnd, &data->whole_rect );
604
605     data->client_rect = *new_client_rect;
606     OffsetRect( &data->client_rect, -data->whole_rect.left, -data->whole_rect.top );
607     SetPropA( data->hwnd, client_offset_atom,
608               (HANDLE)MAKELONG( data->client_rect.left, data->client_rect.top ));
609
610     if (!data->whole_window) return;
611     if (swp_flags & SWP_WINE_NOHOSTMOVE) return;
612
613     mask = get_window_changes( &changes, &old_whole_rect, &data->whole_rect );
614
615     if (!(swp_flags & SWP_NOZORDER))
616     {
617         /* find window that this one must be after */
618         HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
619         while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
620             prev = GetWindow( prev, GW_HWNDPREV );
621         if (!prev)  /* top child */
622         {
623             changes.stack_mode = Above;
624             mask |= CWStackMode;
625         }
626         else
627         {
628             /* should use stack_mode Below but most window managers don't get it right */
629             /* so move it above the next one in Z order */
630             HWND next = GetWindow( data->hwnd, GW_HWNDNEXT );
631             while (next && !(GetWindowLongW( next, GWL_STYLE ) & WS_VISIBLE))
632                 next = GetWindow( next, GW_HWNDNEXT );
633             if (next)
634             {
635                 changes.stack_mode = Above;
636                 changes.sibling = X11DRV_get_whole_window(next);
637                 mask |= CWStackMode | CWSibling;
638             }
639         }
640     }
641
642     if (mask)
643     {
644         DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
645
646         TRACE( "setting win %lx pos %ld,%ld,%ldx%ld after %lx changes=%x\n",
647                data->whole_window, data->whole_rect.left, data->whole_rect.top,
648                data->whole_rect.right - data->whole_rect.left,
649                data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
650
651         wine_tsx11_lock();
652         XSync( gdi_display, False );  /* flush graphics operations before moving the window */
653         if (mask & (CWWidth|CWHeight)) set_size_hints( display, data, style );
654         XReconfigureWMWindow( display, data->whole_window,
655                               DefaultScreen(display), mask, &changes );
656         wine_tsx11_unlock();
657     }
658 }
659
660
661 /**********************************************************************
662  *              create_desktop
663  */
664 static void create_desktop( Display *display, struct x11drv_win_data *data )
665 {
666     VisualID visualid;
667
668     wine_tsx11_lock();
669     winContext = XUniqueContext();
670     XInternAtoms( display, (char **)atom_names, NB_XATOMS - FIRST_XATOM, False, X11DRV_Atoms );
671     visualid = XVisualIDFromVisual(visual);
672     wine_tsx11_unlock();
673
674     whole_window_atom  = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_whole_window" ));
675     icon_window_atom   = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_icon_window" ));
676     client_offset_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_client_area_offset" ));
677
678     data->whole_window = root_window;
679     data->whole_rect = data->client_rect = data->window_rect;
680
681     SetPropA( data->hwnd, whole_window_atom, (HANDLE)root_window );
682     SetPropA( data->hwnd, "__wine_x11_visual_id", (HANDLE)visualid );
683
684     X11DRV_InitClipboard();
685
686     if (root_window != DefaultRootWindow(display)) X11DRV_create_desktop_thread();
687 }
688
689
690 /**********************************************************************
691  *              create_whole_window
692  *
693  * Create the whole X window for a given window
694  */
695 static Window create_whole_window( Display *display, struct x11drv_win_data *data, DWORD style )
696 {
697     int cx, cy, mask;
698     XSetWindowAttributes attr;
699     XIM xim;
700     RECT rect;
701
702     rect = data->window_rect;
703     X11DRV_window_to_X_rect( data->hwnd, &rect );
704
705     if (!(cx = rect.right - rect.left)) cx = 1;
706     if (!(cy = rect.bottom - rect.top)) cy = 1;
707
708     mask = get_window_attributes( data, &attr );
709
710     /* set the attributes that don't change over the lifetime of the window */
711     attr.bit_gravity       = ForgetGravity;
712     attr.win_gravity       = NorthWestGravity;
713     attr.backing_store     = NotUseful/*WhenMapped*/;
714     mask |= CWBitGravity | CWWinGravity | CWBackingStore;
715
716     wine_tsx11_lock();
717
718     data->whole_rect = rect;
719     data->whole_window = XCreateWindow( display, root_window, rect.left, rect.top, cx, cy,
720                                         0, screen_depth, InputOutput, visual,
721                                         mask, &attr );
722
723     if (!data->whole_window)
724     {
725         wine_tsx11_unlock();
726         return 0;
727     }
728     XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
729
730     /* non-maximized child must be at bottom of Z order */
731     if ((style & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
732     {
733         XWindowChanges changes;
734         changes.stack_mode = Below;
735         XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
736     }
737     XSync( display, False );  /* FIXME: should not be needed */
738
739     wine_tsx11_unlock();
740
741     xim = x11drv_thread_data()->xim;
742     if (xim) data->xic = X11DRV_CreateIC( xim, display, data->whole_window );
743
744     X11DRV_set_wm_hints( display, data );
745
746     SetPropA( data->hwnd, whole_window_atom, (HANDLE)data->whole_window );
747     return data->whole_window;
748 }
749
750
751 /**********************************************************************
752  *              destroy_whole_window
753  *
754  * Destroy the whole X window for a given window.
755  */
756 static void destroy_whole_window( Display *display, struct x11drv_win_data *data )
757 {
758     struct x11drv_thread_data *thread_data = x11drv_thread_data();
759
760     if (!data->whole_window) return;
761
762     TRACE( "win %p xwin %lx\n", data->hwnd, data->whole_window );
763     if (thread_data->cursor_window == data->whole_window) thread_data->cursor_window = None;
764     wine_tsx11_lock();
765     XSync( gdi_display, False );  /* flush any reference to this drawable in GDI queue */
766     XDeleteContext( display, data->whole_window, winContext );
767     XDestroyWindow( display, data->whole_window );  /* this destroys client too */
768     data->whole_window = 0;
769     if (data->xic)
770     {
771         XUnsetICFocus( data->xic );
772         XDestroyIC( data->xic );
773     }
774     wine_tsx11_unlock();
775     RemovePropA( data->hwnd, whole_window_atom );
776 }
777
778
779 /*****************************************************************
780  *              SetWindowText   (X11DRV.@)
781  */
782 BOOL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
783 {
784     Display *display = thread_display();
785     UINT count;
786     char *buffer;
787     char *utf8_buffer;
788     Window win;
789     XTextProperty prop;
790
791     if ((win = X11DRV_get_whole_window( hwnd )))
792     {
793         /* allocate new buffer for window text */
794         count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
795         if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count )))
796         {
797             ERR("Not enough memory for window text\n");
798             return FALSE;
799         }
800         WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
801
802         count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
803         if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
804         {
805             ERR("Not enough memory for window text in UTF-8\n");
806             HeapFree( GetProcessHeap(), 0, buffer );
807             return FALSE;
808         }
809         WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
810
811         wine_tsx11_lock();
812         if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
813         {
814             XSetWMName( display, win, &prop );
815             XSetWMIconName( display, win, &prop );
816             XFree( prop.value );
817         }
818         /*
819         Implements a NET_WM UTF-8 title. It should be without a trailing \0,
820         according to the standard
821         ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
822         */
823         XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
824                          8, PropModeReplace, (unsigned char *) utf8_buffer, count);
825         wine_tsx11_unlock();
826
827         HeapFree( GetProcessHeap(), 0, utf8_buffer );
828         HeapFree( GetProcessHeap(), 0, buffer );
829     }
830     return TRUE;
831 }
832
833
834 /***********************************************************************
835  *              DestroyWindow   (X11DRV.@)
836  */
837 BOOL X11DRV_DestroyWindow( HWND hwnd )
838 {
839     struct x11drv_thread_data *thread_data = x11drv_thread_data();
840     Display *display = thread_data->display;
841     WND *wndPtr = WIN_GetPtr( hwnd );
842     struct x11drv_win_data *data;
843
844     if (!(data = X11DRV_get_win_data( hwnd ))) goto done;
845
846     destroy_whole_window( display, data );
847     destroy_icon_window( display, data );
848
849     if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
850     if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
851     if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
852     wine_tsx11_lock();
853     XDeleteContext( gdi_display, (XID)hwnd, win_data_context );
854     wine_tsx11_unlock();
855     HeapFree( GetProcessHeap(), 0, data );
856
857  done:
858     WIN_ReleasePtr( wndPtr );
859     return TRUE;
860 }
861
862
863 /**********************************************************************
864  *              CreateWindow   (X11DRV.@)
865  */
866 BOOL X11DRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
867 {
868     Display *display = thread_display();
869     WND *wndPtr;
870     struct x11drv_win_data *data;
871     HWND parent, insert_after;
872     RECT rect;
873     DWORD style;
874     CBT_CREATEWNDA cbtc;
875     BOOL ret = FALSE;
876
877     if (cs->cx > 65535)
878     {
879         ERR( "invalid window width %d\n", cs->cx );
880         cs->cx = 65535;
881     }
882     if (cs->cy > 65535)
883     {
884         ERR( "invalid window height %d\n", cs->cy );
885         cs->cy = 65535;
886     }
887     if (cs->cx < 0)
888     {
889         ERR( "invalid window width %d\n", cs->cx );
890         cs->cx = 0;
891     }
892     if (cs->cy < 0)
893     {
894         ERR( "invalid window height %d\n", cs->cy );
895         cs->cy = 0;
896     }
897
898     if (!(data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data)))) return FALSE;
899     data->hwnd          = hwnd;
900     data->whole_window  = 0;
901     data->icon_window   = 0;
902     data->xic           = 0;
903     data->hWMIconBitmap = 0;
904     data->hWMIconMask   = 0;
905
906     /* use gdi_display so that it's available from all threads (FIXME) */
907     wine_tsx11_lock();
908     if (!win_data_context) win_data_context = XUniqueContext();
909     XSaveContext( gdi_display, (XID)hwnd, win_data_context, (char *)data );
910     wine_tsx11_unlock();
911
912     /* initialize the dimensions before sending WM_GETMINMAXINFO */
913     SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
914     X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER, 0 );
915
916     parent = GetAncestor( hwnd, GA_PARENT );
917     if (!parent)
918     {
919         create_desktop( display, data );
920         return TRUE;
921     }
922
923     /* create an X window if it's a top level window */
924     if (parent == GetDesktopWindow())
925     {
926         if (!create_whole_window( display, data, cs->style )) goto failed;
927     }
928
929     /* Call the WH_CBT hook */
930     cbtc.lpcs = cs;
931     cbtc.hwndInsertAfter = HWND_TOP;
932     if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode ))
933     {
934         TRACE("CBT-hook returned !0\n");
935         goto failed;
936     }
937
938     /* Send the WM_GETMINMAXINFO message and fix the size if needed */
939     if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
940     {
941         POINT maxSize, maxPos, minTrack, maxTrack;
942
943         WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
944         if (maxSize.x < cs->cx) cs->cx = maxSize.x;
945         if (maxSize.y < cs->cy) cs->cy = maxSize.y;
946         if (cs->cx < 0) cs->cx = 0;
947         if (cs->cy < 0) cs->cy = 0;
948
949         SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
950         if (!X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER, 0 )) return FALSE;
951     }
952
953     /* send WM_NCCREATE */
954     TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cs->cx, cs->cy );
955     if (unicode)
956         ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
957     else
958         ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
959     if (!ret)
960     {
961         WARN("aborted by WM_xxCREATE!\n");
962         return FALSE;
963     }
964
965     /* make sure the window is still valid */
966     if (!(data = X11DRV_get_win_data( hwnd ))) return FALSE;
967     if (data->whole_window) X11DRV_sync_window_style( display, data );
968
969     /* send WM_NCCALCSIZE */
970     rect = data->window_rect;
971     SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&rect );
972
973     if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
974     if (rect.left < wndPtr->rectWindow.left) rect.left = wndPtr->rectWindow.left;
975     if (rect.right > wndPtr->rectWindow.right) rect.right = wndPtr->rectWindow.right;
976     if (rect.top < wndPtr->rectWindow.top) rect.top = wndPtr->rectWindow.top;
977     if (rect.bottom > wndPtr->rectWindow.bottom) rect.bottom = wndPtr->rectWindow.bottom;
978     if (rect.left > rect.right || rect.top > rect.bottom) rect = wndPtr->rectWindow;
979
980     /* yes, even if the CBT hook was called with HWND_TOP */
981     insert_after = ((wndPtr->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
982
983     X11DRV_set_window_pos( hwnd, insert_after, &wndPtr->rectWindow, &rect, 0, 0 );
984
985     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\n",
986            hwnd, wndPtr->rectWindow.left, wndPtr->rectWindow.top,
987            wndPtr->rectWindow.right, wndPtr->rectWindow.bottom,
988            wndPtr->rectClient.left, wndPtr->rectClient.top,
989            wndPtr->rectClient.right, wndPtr->rectClient.bottom,
990            data->whole_rect.left, data->whole_rect.top,
991            data->whole_rect.right, data->whole_rect.bottom,
992            data->client_rect.left, data->client_rect.top,
993            data->client_rect.right, data->client_rect.bottom,
994            (unsigned int)data->whole_window );
995
996     WIN_ReleasePtr( wndPtr );
997
998     if (unicode)
999         ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1000     else
1001         ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1002
1003     if (!ret)
1004     {
1005         WIN_UnlinkWindow( hwnd );
1006         return FALSE;
1007     }
1008
1009     /* Send the size messages */
1010
1011     if (!(wndPtr = WIN_GetPtr(hwnd)) || wndPtr == WND_OTHER_PROCESS) return FALSE;
1012     if (!(wndPtr->flags & WIN_NEED_SIZE))
1013     {
1014         RECT rect = wndPtr->rectClient;
1015         WIN_ReleasePtr( wndPtr );
1016         /* send it anyway */
1017         if (((rect.right-rect.left) <0) ||((rect.bottom-rect.top)<0))
1018             WARN("sending bogus WM_SIZE message 0x%08lx\n",
1019                  MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1020         SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1021                       MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1022         SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( rect.left, rect.top ) );
1023     }
1024     else WIN_ReleasePtr( wndPtr );
1025
1026     /* Show the window, maximizing or minimizing if needed */
1027
1028     style = GetWindowLongW( hwnd, GWL_STYLE );
1029     if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1030     {
1031         extern UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect ); /*FIXME*/
1032
1033         RECT newPos;
1034         UINT swFlag = (style & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1035         WIN_SetStyle( hwnd, 0, WS_MAXIMIZE | WS_MINIMIZE );
1036         WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1037         swFlag = ((style & WS_CHILD) || GetActiveWindow())
1038             ? SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED
1039             : SWP_NOZORDER | SWP_FRAMECHANGED;
1040         SetWindowPos( hwnd, 0, newPos.left, newPos.top,
1041                       newPos.right, newPos.bottom, swFlag );
1042     }
1043
1044     return TRUE;
1045
1046  failed:
1047     X11DRV_DestroyWindow( hwnd );
1048     return FALSE;
1049 }
1050
1051
1052 /***********************************************************************
1053  *              X11DRV_get_win_data
1054  *
1055  * Return the X11 data structure associated with a window.
1056  */
1057 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1058 {
1059     char *data;
1060     if (XFindContext( gdi_display, (XID)hwnd, win_data_context, &data )) data = NULL;
1061     return (struct x11drv_win_data *)data;
1062 }
1063
1064
1065 /***********************************************************************
1066  *              X11DRV_get_client_area_offset
1067  *
1068  * Return the offset of the client area relative to the x11 window.
1069  */
1070 POINT X11DRV_get_client_area_offset( HWND hwnd )
1071 {
1072     POINT pt;
1073     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1074
1075     if (data)
1076     {
1077         pt.x = data->client_rect.left;
1078         pt.y = data->client_rect.top;
1079     }
1080     else
1081     {
1082         ULONG_PTR offset = (ULONG_PTR)GetPropA( hwnd, client_offset_atom );
1083         pt.x = (SHORT)LOWORD(offset);
1084         pt.y = (SHORT)HIWORD(offset);
1085     }
1086     return pt;
1087 }
1088
1089
1090 /***********************************************************************
1091  *              X11DRV_get_whole_window
1092  *
1093  * Return the X window associated with the full area of a window
1094  */
1095 Window X11DRV_get_whole_window( HWND hwnd )
1096 {
1097     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1098
1099     if (!data) return (Window)GetPropA( hwnd, whole_window_atom );
1100     return data->whole_window;
1101 }
1102
1103
1104 /***********************************************************************
1105  *              X11DRV_get_ic
1106  *
1107  * Return the X input context associated with a window
1108  */
1109 XIC X11DRV_get_ic( HWND hwnd )
1110 {
1111     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1112
1113     if (!data) return 0;
1114     return data->xic;
1115 }
1116
1117
1118 /*****************************************************************
1119  *              SetParent   (X11DRV.@)
1120  */
1121 HWND X11DRV_SetParent( HWND hwnd, HWND parent )
1122 {
1123     Display *display = thread_display();
1124     HWND old_parent;
1125
1126     /* Windows hides the window first, then shows it again
1127      * including the WM_SHOWWINDOW messages and all */
1128     BOOL was_visible = ShowWindow( hwnd, SW_HIDE );
1129
1130     if (!IsWindow( parent )) return 0;
1131
1132     old_parent = GetAncestor( hwnd, GA_PARENT );
1133     if (parent != old_parent)
1134     {
1135         struct x11drv_win_data *data;
1136
1137         if (!(data = X11DRV_get_win_data( hwnd ))) return 0;
1138
1139         WIN_LinkWindow( hwnd, parent, HWND_TOP );
1140
1141         if (parent != GetDesktopWindow()) /* a child window */
1142         {
1143             if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD))
1144             {
1145                 HMENU menu = (HMENU)SetWindowLongPtrW( hwnd, GWLP_ID, 0 );
1146                 if (menu) DestroyMenu( menu );
1147             }
1148             if (old_parent == GetDesktopWindow())
1149             {
1150                 /* destroy the old X windows */
1151                 destroy_whole_window( display, data );
1152                 destroy_icon_window( display, data );
1153             }
1154         }
1155         else  /* new top level window */
1156         {
1157             /* FIXME: we ignore errors since we can't really recover anyway */
1158             create_whole_window( display, data, GetWindowLongW( hwnd, GWL_STYLE ) );
1159         }
1160     }
1161
1162     /* SetParent additionally needs to make hwnd the topmost window
1163        in the x-order and send the expected WM_WINDOWPOSCHANGING and
1164        WM_WINDOWPOSCHANGED notification messages.
1165     */
1166     SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0,
1167                   SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | (was_visible ? SWP_SHOWWINDOW : 0) );
1168     /* FIXME: a WM_MOVE is also generated (in the DefWindowProc handler
1169      * for WM_WINDOWPOSCHANGED) in Windows, should probably remove SWP_NOMOVE */
1170
1171     return old_parent;
1172 }
1173
1174
1175 /*****************************************************************
1176  *              SetFocus   (X11DRV.@)
1177  *
1178  * Set the X focus.
1179  * Explicit colormap management seems to work only with OLVWM.
1180  */
1181 void X11DRV_SetFocus( HWND hwnd )
1182 {
1183     Display *display = thread_display();
1184     XWindowAttributes win_attr;
1185     Window win;
1186
1187     /* Only mess with the X focus if there's */
1188     /* no desktop window and if the window is not managed by the WM. */
1189     if (root_window != DefaultRootWindow(display)) return;
1190
1191     if (!hwnd)  /* If setting the focus to 0, uninstall the colormap */
1192     {
1193         wine_tsx11_lock();
1194         if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1195             XUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1196         wine_tsx11_unlock();
1197         return;
1198     }
1199
1200     hwnd = GetAncestor( hwnd, GA_ROOT );
1201     if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_MANAGED) return;
1202     if (!(win = X11DRV_get_whole_window( hwnd ))) return;
1203
1204     /* Set X focus and install colormap */
1205     wine_tsx11_lock();
1206     if (XGetWindowAttributes( display, win, &win_attr ) &&
1207         (win_attr.map_state == IsViewable))
1208     {
1209         /* If window is not viewable, don't change anything */
1210
1211         /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1212         /* FIXME: this is not entirely correct */
1213         XSetInputFocus( display, win, RevertToParent,
1214                         /* CurrentTime */
1215                         GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1216         if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1217             XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1218     }
1219     wine_tsx11_unlock();
1220 }
1221
1222
1223 /**********************************************************************
1224  *              SetWindowIcon (X11DRV.@)
1225  *
1226  * hIcon or hIconSm has changed (or is being initialised for the
1227  * first time). Complete the X11 driver-specific initialisation
1228  * and set the window hints.
1229  *
1230  * This is not entirely correct, may need to create
1231  * an icon window and set the pixmap as a background
1232  */
1233 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
1234 {
1235     Display *display = thread_display();
1236     struct x11drv_win_data *data;
1237     DWORD ex_style;
1238
1239     if (type != ICON_BIG) return;  /* nothing to do here */
1240
1241     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1242     if (!data->whole_window) return;
1243
1244     ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
1245     if (ex_style & WS_EX_MANAGED)
1246     {
1247         XWMHints* wm_hints;
1248
1249         wine_tsx11_lock();
1250         if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
1251         wine_tsx11_unlock();
1252         if (wm_hints)
1253         {
1254             set_icon_hints( display, data, wm_hints, icon, ex_style );
1255             wine_tsx11_lock();
1256             XSetWMHints( display, data->whole_window, wm_hints );
1257             XFree( wm_hints );
1258             wine_tsx11_unlock();
1259         }
1260     }
1261 }