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