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