Removed the bitsPerPixel field in the generic DC structure, and leave
[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     if (is_top_level)
793     {
794         XIM xim = x11drv_thread_data()->xim;
795         if (xim) data->xic = XCreateIC( xim,
796                                         XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
797                                         XNClientWindow, data->whole_window,
798                                         XNFocusWindow, data->whole_window,
799                                         0 );
800     }
801
802     /* non-maximized child must be at bottom of Z order */
803     if ((win->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
804     {
805         XWindowChanges changes;
806         changes.stack_mode = Below;
807         XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
808     }
809
810     wine_tsx11_unlock();
811
812     if (is_top_level) X11DRV_set_wm_hints( display, win );
813
814     return data->whole_window;
815 }
816
817
818 /**********************************************************************
819  *              create_client_window
820  *
821  * Create the client window for a given window
822  */
823 static Window create_client_window( Display *display, WND *win )
824 {
825     struct x11drv_win_data *data = win->pDriverData;
826     RECT rect = data->whole_rect;
827     XSetWindowAttributes attr;
828
829     OffsetRect( &rect, -data->whole_rect.left, -data->whole_rect.top );
830     data->client_rect = rect;
831
832     attr.event_mask = (ExposureMask | PointerMotionMask |
833                        ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
834     attr.bit_gravity = (win->clsStyle & (CS_VREDRAW | CS_HREDRAW)) ?
835                        ForgetGravity : NorthWestGravity;
836     attr.backing_store = NotUseful/*WhenMapped*/;
837
838     wine_tsx11_lock();
839     data->client_window = XCreateWindow( display, data->whole_window, 0, 0,
840                                          max( rect.right - rect.left, 1 ),
841                                          max( rect.bottom - rect.top, 1 ),
842                                          0, screen_depth,
843                                          InputOutput, visual,
844                                          CWEventMask | CWBitGravity | CWBackingStore, &attr );
845     if (data->client_window && is_client_window_mapped( win ))
846         XMapWindow( display, data->client_window );
847     wine_tsx11_unlock();
848     return data->client_window;
849 }
850
851
852 /*****************************************************************
853  *              SetWindowText   (X11DRV.@)
854  */
855 BOOL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
856 {
857     Display *display = thread_display();
858     UINT count;
859     char *buffer;
860     char *utf8_buffer;
861     Window win;
862     XTextProperty prop;
863
864     if ((win = X11DRV_get_whole_window( hwnd )))
865     {
866         /* allocate new buffer for window text */
867         count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
868         if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count )))
869         {
870             ERR("Not enough memory for window text\n");
871             return FALSE;
872         }
873         WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
874
875         count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
876         if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
877         {
878             ERR("Not enough memory for window text in UTF-8\n");
879             return FALSE;
880         }
881         WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
882
883         wine_tsx11_lock();
884         if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
885         {
886             XSetWMName( display, win, &prop );
887             XSetWMIconName( display, win, &prop );
888             XFree( prop.value );
889         }
890         /*
891         Implements a NET_WM UTF-8 title. It should be without a trailing \0,
892         according to the standard
893         ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
894         */
895         XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
896                          8, PropModeReplace, (unsigned char *) utf8_buffer, count);
897         wine_tsx11_unlock();
898
899         HeapFree( GetProcessHeap(), 0, utf8_buffer );
900         HeapFree( GetProcessHeap(), 0, buffer );
901     }
902     return TRUE;
903 }
904
905
906 /***********************************************************************
907  *              DestroyWindow   (X11DRV.@)
908  */
909 BOOL X11DRV_DestroyWindow( HWND hwnd )
910 {
911     struct x11drv_thread_data *thread_data = x11drv_thread_data();
912     Display *display = thread_data->display;
913     WND *wndPtr = WIN_GetPtr( hwnd );
914     X11DRV_WND_DATA *data = wndPtr->pDriverData;
915
916     if (!data) goto done;
917
918     if (data->whole_window)
919     {
920         TRACE( "win %p xwin %lx/%lx\n", hwnd, data->whole_window, data->client_window );
921         if (thread_data->cursor_window == data->whole_window) thread_data->cursor_window = None;
922         if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
923         wine_tsx11_lock();
924         XSync( gdi_display, False );  /* flush any reference to this drawable in GDI queue */
925         XDeleteContext( display, data->whole_window, winContext );
926         XDeleteContext( display, data->client_window, winContext );
927         XDestroyWindow( display, data->whole_window );  /* this destroys client too */
928         if (data->xic)
929         {
930             XUnsetICFocus( data->xic );
931             XDestroyIC( data->xic );
932         }
933         destroy_icon_window( display, wndPtr );
934         wine_tsx11_unlock();
935     }
936
937     if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
938     if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
939     HeapFree( GetProcessHeap(), 0, data );
940     wndPtr->pDriverData = NULL;
941  done:
942     WIN_ReleasePtr( wndPtr );
943     return TRUE;
944 }
945
946
947 /**********************************************************************
948  *              CreateWindow   (X11DRV.@)
949  */
950 BOOL X11DRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
951 {
952     Display *display = thread_display();
953     WND *wndPtr;
954     struct x11drv_win_data *data;
955     RECT rect;
956     CBT_CREATEWNDA cbtc;
957     BOOL ret = FALSE;
958
959     if (cs->cx > 65535)
960     {
961         ERR( "invalid window width %d\n", cs->cx );
962         cs->cx = 65535;
963     }
964     if (cs->cy > 65535)
965     {
966         ERR( "invalid window height %d\n", cs->cx );
967         cs->cy = 65535;
968     }
969
970     if (!(data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data)))) return FALSE;
971     data->whole_window  = 0;
972     data->client_window = 0;
973     data->icon_window   = 0;
974     data->xic           = 0;
975     data->hWMIconBitmap = 0;
976     data->hWMIconMask   = 0;
977
978     wndPtr = WIN_GetPtr( hwnd );
979     wndPtr->pDriverData = data;
980
981     /* initialize the dimensions before sending WM_GETMINMAXINFO */
982     SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
983     WIN_SetRectangles( hwnd, &rect, &rect );
984
985     if (!wndPtr->parent)
986     {
987         create_desktop( display, wndPtr );
988         WIN_ReleasePtr( wndPtr );
989         return TRUE;
990     }
991
992     if (!create_whole_window( display, wndPtr )) goto failed;
993     if (!create_client_window( display, wndPtr )) goto failed;
994     wine_tsx11_lock();
995     XSync( display, False );
996     wine_tsx11_unlock();
997
998     SetPropA( hwnd, whole_window_atom, (HANDLE)data->whole_window );
999     SetPropA( hwnd, client_window_atom, (HANDLE)data->client_window );
1000
1001     /* Call the WH_CBT hook */
1002     cbtc.lpcs = cs;
1003     cbtc.hwndInsertAfter = HWND_TOP;
1004     if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode ))
1005     {
1006         TRACE("CBT-hook returned !0\n");
1007         goto failed;
1008     }
1009
1010     /* Send the WM_GETMINMAXINFO message and fix the size if needed */
1011     if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
1012     {
1013         POINT maxSize, maxPos, minTrack, maxTrack;
1014
1015         WIN_ReleasePtr( wndPtr );
1016         WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
1017         if (maxSize.x < cs->cx) cs->cx = maxSize.x;
1018         if (maxSize.y < cs->cy) cs->cy = maxSize.y;
1019         if (cs->cx < minTrack.x ) cs->cx = minTrack.x;
1020         if (cs->cy < minTrack.y ) cs->cy = minTrack.y;
1021         if (cs->cx < 0) cs->cx = 0;
1022         if (cs->cy < 0) cs->cy = 0;
1023
1024         if (!(wndPtr = WIN_GetPtr( hwnd ))) return FALSE;
1025         SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
1026         WIN_SetRectangles( hwnd, &rect, &rect );
1027         X11DRV_sync_whole_window_position( display, wndPtr, 0 );
1028     }
1029     WIN_ReleasePtr( wndPtr );
1030
1031     /* send WM_NCCREATE */
1032     TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cs->cx, cs->cy );
1033     if (unicode)
1034         ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1035     else
1036         ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1037     if (!ret)
1038     {
1039         WARN("aborted by WM_xxCREATE!\n");
1040         return FALSE;
1041     }
1042
1043     if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
1044
1045     X11DRV_sync_window_style( display, wndPtr );
1046
1047     /* send WM_NCCALCSIZE */
1048     rect = wndPtr->rectWindow;
1049     WIN_ReleasePtr( wndPtr );
1050     SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&rect );
1051
1052     if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
1053     if (rect.left > rect.right || rect.top > rect.bottom) rect = wndPtr->rectWindow;
1054     WIN_SetRectangles( hwnd, &wndPtr->rectWindow, &rect );
1055     X11DRV_sync_client_window_position( display, wndPtr );
1056     X11DRV_register_window( display, hwnd, data );
1057
1058     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",
1059            hwnd, wndPtr->rectWindow.left, wndPtr->rectWindow.top,
1060            wndPtr->rectWindow.right, wndPtr->rectWindow.bottom,
1061            wndPtr->rectClient.left, wndPtr->rectClient.top,
1062            wndPtr->rectClient.right, wndPtr->rectClient.bottom,
1063            data->whole_rect.left, data->whole_rect.top,
1064            data->whole_rect.right, data->whole_rect.bottom,
1065            data->client_rect.left, data->client_rect.top,
1066            data->client_rect.right, data->client_rect.bottom,
1067            (unsigned int)data->whole_window, (unsigned int)data->client_window );
1068
1069     if ((wndPtr->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
1070         WIN_LinkWindow( hwnd, wndPtr->parent, HWND_BOTTOM );
1071     else
1072         WIN_LinkWindow( hwnd, wndPtr->parent, HWND_TOP );
1073
1074     WIN_ReleasePtr( wndPtr );
1075
1076     if (unicode)
1077         ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1078     else
1079         ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1080
1081     if (!ret)
1082     {
1083         WIN_UnlinkWindow( hwnd );
1084         return FALSE;
1085     }
1086
1087     /* Send the size messages */
1088
1089     if (!(wndPtr = WIN_FindWndPtr(hwnd))) return FALSE;
1090     if (!(wndPtr->flags & WIN_NEED_SIZE))
1091     {
1092         /* send it anyway */
1093         if (((wndPtr->rectClient.right-wndPtr->rectClient.left) <0)
1094             ||((wndPtr->rectClient.bottom-wndPtr->rectClient.top)<0))
1095             WARN("sending bogus WM_SIZE message 0x%08lx\n",
1096                  MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
1097                           wndPtr->rectClient.bottom-wndPtr->rectClient.top));
1098         SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1099                       MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
1100                                wndPtr->rectClient.bottom-wndPtr->rectClient.top));
1101         SendMessageW( hwnd, WM_MOVE, 0,
1102                       MAKELONG( wndPtr->rectClient.left, wndPtr->rectClient.top ) );
1103     }
1104
1105     /* Show the window, maximizing or minimizing if needed */
1106
1107     if (wndPtr->dwStyle & (WS_MINIMIZE | WS_MAXIMIZE))
1108     {
1109         extern UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect ); /*FIXME*/
1110
1111         RECT newPos;
1112         UINT swFlag = (wndPtr->dwStyle & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1113         WIN_SetStyle( hwnd, wndPtr->dwStyle & ~(WS_MAXIMIZE | WS_MINIMIZE) );
1114         WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1115         swFlag = ((wndPtr->dwStyle & WS_CHILD) || GetActiveWindow())
1116             ? SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED
1117             : SWP_NOZORDER | SWP_FRAMECHANGED;
1118         SetWindowPos( hwnd, 0, newPos.left, newPos.top,
1119                       newPos.right, newPos.bottom, swFlag );
1120     }
1121
1122     /* if the window was made visible set create struct flag so that
1123      * we do a proper ShowWindow later on */
1124     if (wndPtr->dwStyle & WS_VISIBLE) cs->style |= WS_VISIBLE;
1125
1126     WIN_ReleaseWndPtr( wndPtr );
1127     return TRUE;
1128
1129
1130  failed:
1131     X11DRV_DestroyWindow( hwnd );
1132     if (wndPtr) WIN_ReleasePtr( wndPtr );
1133     return FALSE;
1134 }
1135
1136
1137 /***********************************************************************
1138  *              X11DRV_get_client_window
1139  *
1140  * Return the X window associated with the client area of a window
1141  */
1142 Window X11DRV_get_client_window( HWND hwnd )
1143 {
1144     Window ret = 0;
1145     WND *win = WIN_GetPtr( hwnd );
1146
1147     if (win == WND_OTHER_PROCESS)
1148         return (Window)GetPropA( hwnd, client_window_atom );
1149
1150     if (win)
1151     {
1152         struct x11drv_win_data *data = win->pDriverData;
1153         ret = data->client_window;
1154         WIN_ReleasePtr( win );
1155     }
1156     return ret;
1157 }
1158
1159
1160 /***********************************************************************
1161  *              X11DRV_get_whole_window
1162  *
1163  * Return the X window associated with the full area of a window
1164  */
1165 Window X11DRV_get_whole_window( HWND hwnd )
1166 {
1167     Window ret = 0;
1168     WND *win = WIN_GetPtr( hwnd );
1169
1170     if (win == WND_OTHER_PROCESS)
1171         return (Window)GetPropA( hwnd, whole_window_atom );
1172
1173     if (win)
1174     {
1175         struct x11drv_win_data *data = win->pDriverData;
1176         ret = data->whole_window;
1177         WIN_ReleasePtr( win );
1178     }
1179     return ret;
1180 }
1181
1182
1183 /***********************************************************************
1184  *              X11DRV_get_ic
1185  *
1186  * Return the X input context associated with a window
1187  */
1188 XIC X11DRV_get_ic( HWND hwnd )
1189 {
1190     XIC ret = 0;
1191     WND *win = WIN_GetPtr( hwnd );
1192
1193     if (win && win != WND_OTHER_PROCESS)
1194     {
1195         struct x11drv_win_data *data = win->pDriverData;
1196         ret = data->xic;
1197         WIN_ReleasePtr( win );
1198     }
1199     return ret;
1200 }
1201
1202
1203 /*****************************************************************
1204  *              SetParent   (X11DRV.@)
1205  */
1206 HWND X11DRV_SetParent( HWND hwnd, HWND parent )
1207 {
1208     Display *display = thread_display();
1209     WND *wndPtr;
1210     HWND retvalue;
1211
1212     /* Windows hides the window first, then shows it again
1213      * including the WM_SHOWWINDOW messages and all */
1214     BOOL was_visible = ShowWindow( hwnd, SW_HIDE );
1215
1216     if (!IsWindow( parent )) return 0;
1217     if (!(wndPtr = WIN_GetPtr(hwnd)) || wndPtr == WND_OTHER_PROCESS) return 0;
1218
1219     retvalue = wndPtr->parent;  /* old parent */
1220     if (parent != retvalue)
1221     {
1222         struct x11drv_win_data *data = wndPtr->pDriverData;
1223
1224         WIN_LinkWindow( hwnd, parent, HWND_TOP );
1225
1226         if (parent != GetDesktopWindow()) /* a child window */
1227         {
1228             if (!(wndPtr->dwStyle & WS_CHILD))
1229             {
1230                 HMENU menu = (HMENU)SetWindowLongW( hwnd, GWL_ID, 0 );
1231                 if (menu) DestroyMenu( menu );
1232             }
1233         }
1234
1235         if (is_window_top_level( wndPtr )) X11DRV_set_wm_hints( display, wndPtr );
1236         wine_tsx11_lock();
1237         X11DRV_sync_window_style( display, wndPtr );
1238         XReparentWindow( display, data->whole_window, X11DRV_get_client_window(parent),
1239                          data->whole_rect.left, data->whole_rect.top );
1240         wine_tsx11_unlock();
1241     }
1242     WIN_ReleasePtr( wndPtr );
1243
1244     /* SetParent additionally needs to make hwnd the topmost window
1245        in the x-order and send the expected WM_WINDOWPOSCHANGING and
1246        WM_WINDOWPOSCHANGED notification messages.
1247     */
1248     SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0,
1249                   SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | (was_visible ? SWP_SHOWWINDOW : 0) );
1250     /* FIXME: a WM_MOVE is also generated (in the DefWindowProc handler
1251      * for WM_WINDOWPOSCHANGED) in Windows, should probably remove SWP_NOMOVE */
1252
1253     return retvalue;
1254 }
1255
1256
1257 /*****************************************************************
1258  *              SetFocus   (X11DRV.@)
1259  *
1260  * Set the X focus.
1261  * Explicit colormap management seems to work only with OLVWM.
1262  */
1263 void X11DRV_SetFocus( HWND hwnd )
1264 {
1265     Display *display = thread_display();
1266     XWindowAttributes win_attr;
1267     Window win;
1268
1269     /* Only mess with the X focus if there's */
1270     /* no desktop window and if the window is not managed by the WM. */
1271     if (root_window != DefaultRootWindow(display)) return;
1272
1273     if (!hwnd)  /* If setting the focus to 0, uninstall the colormap */
1274     {
1275         wine_tsx11_lock();
1276         if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1277             XUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1278         wine_tsx11_unlock();
1279         return;
1280     }
1281
1282     hwnd = GetAncestor( hwnd, GA_ROOT );
1283     if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_MANAGED) return;
1284     if (!(win = X11DRV_get_whole_window( hwnd ))) return;
1285
1286     /* Set X focus and install colormap */
1287     wine_tsx11_lock();
1288     if (XGetWindowAttributes( display, win, &win_attr ) &&
1289         (win_attr.map_state == IsViewable))
1290     {
1291         /* If window is not viewable, don't change anything */
1292
1293         /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1294         /* FIXME: this is not entirely correct */
1295         XSetInputFocus( display, win, RevertToParent,
1296                         /*CurrentTime*/ GetMessageTime() + X11DRV_server_startticks );
1297         if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1298             XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1299     }
1300     wine_tsx11_unlock();
1301 }
1302
1303
1304 /**********************************************************************
1305  *              SetWindowIcon (X11DRV.@)
1306  *
1307  * hIcon or hIconSm has changed (or is being initialised for the
1308  * first time). Complete the X11 driver-specific initialisation
1309  * and set the window hints.
1310  *
1311  * This is not entirely correct, may need to create
1312  * an icon window and set the pixmap as a background
1313  */
1314 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
1315 {
1316     WND *wndPtr;
1317     Display *display = thread_display();
1318
1319     if (type != ICON_BIG) return;  /* nothing to do here */
1320
1321     if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return;
1322
1323     if (wndPtr->dwExStyle & WS_EX_MANAGED)
1324     {
1325         Window win = get_whole_window(wndPtr);
1326         XWMHints* wm_hints;
1327
1328         wine_tsx11_lock();
1329         if (!(wm_hints = XGetWMHints( display, win ))) wm_hints = XAllocWMHints();
1330         wine_tsx11_unlock();
1331         if (wm_hints)
1332         {
1333             set_icon_hints( display, wndPtr, wm_hints, icon );
1334             wine_tsx11_lock();
1335             XSetWMHints( display, win, wm_hints );
1336             XFree( wm_hints );
1337             wine_tsx11_unlock();
1338         }
1339     }
1340     WIN_ReleasePtr( wndPtr );
1341 }