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