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