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