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