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