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