msi: Make msi_dialog_dup_property return a copy of the property if the property is...
[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     MwmHints mwm_hints;
469     Atom dndVersion = 4;
470     int i;
471     DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
472     DWORD ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
473     HWND owner = GetWindow( data->hwnd, GW_OWNER );
474     char *process_name = get_process_name();
475
476     if (data->hwnd == GetDesktopWindow())
477     {
478         if (data->whole_window == DefaultRootWindow(display)) return;
479         /* force some styles for the desktop to get the correct decorations */
480         style |= WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
481         owner = 0;
482     }
483
484     /* transient for hint */
485     if (owner)
486     {
487         Window owner_win = X11DRV_get_whole_window( owner );
488         wine_tsx11_lock();
489         XSetTransientForHint( display, data->whole_window, owner_win );
490         wine_tsx11_unlock();
491         group_leader = owner_win;
492     }
493     else group_leader = data->whole_window;
494
495     wine_tsx11_lock();
496
497     /* wm protocols */
498     i = 0;
499     protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
500     protocols[i++] = x11drv_atom(_NET_WM_PING);
501     if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
502     XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
503                      XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
504
505     /* class hints */
506     if ((class_hints = XAllocClassHint()))
507     {
508         static char wine[] = "Wine";
509
510         class_hints->res_name = process_name;
511         class_hints->res_class = wine;
512         XSetClassHint( display, data->whole_window, class_hints );
513         XFree( class_hints );
514     }
515
516     /* size hints */
517     set_size_hints( display, data, style );
518
519     /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
520     XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
521     /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
522     i = getpid();
523     XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
524                     XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
525
526    /* map WS_EX_TOOLWINDOW to _NET_WM_WINDOW_TYPE_UTILITY */
527    if (ex_style & WS_EX_TOOLWINDOW)
528    {
529       Atom a = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
530       XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
531                       XA_ATOM, 32, PropModeReplace, (unsigned char*)&a, 1);
532    }
533
534     mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
535     mwm_hints.functions = MWM_FUNC_MOVE;
536     if (style & WS_THICKFRAME) mwm_hints.functions |= MWM_FUNC_RESIZE;
537     if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
538     if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
539     if (style & WS_SYSMENU)    mwm_hints.functions |= MWM_FUNC_CLOSE;
540     mwm_hints.decorations = 0;
541     if ((style & WS_CAPTION) == WS_CAPTION) 
542     {
543         mwm_hints.decorations |= MWM_DECOR_TITLE;
544         if (style & WS_SYSMENU) mwm_hints.decorations |= MWM_DECOR_MENU;
545         if (style & WS_MINIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MINIMIZE;
546         if (style & WS_MAXIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MAXIMIZE;
547     }
548     if (ex_style & WS_EX_DLGMODALFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
549     else if (style & WS_THICKFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
550     else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
551     else if (style & WS_BORDER) mwm_hints.decorations |= MWM_DECOR_BORDER;
552     else if (!(style & (WS_CHILD|WS_POPUP))) mwm_hints.decorations |= MWM_DECOR_BORDER;
553
554     XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
555                      x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
556                      (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
557
558     XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
559                      XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
560
561     wm_hints = XAllocWMHints();
562     wine_tsx11_unlock();
563
564     /* wm hints */
565     if (wm_hints)
566     {
567         wm_hints->flags = InputHint | StateHint | WindowGroupHint;
568         wm_hints->input = !(style & WS_DISABLED);
569
570         set_icon_hints( display, data, wm_hints,
571                         (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON ) );
572
573         wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
574         wm_hints->window_group = group_leader;
575
576         wine_tsx11_lock();
577         XSetWMHints( display, data->whole_window, wm_hints );
578         XFree(wm_hints);
579         wine_tsx11_unlock();
580     }
581 }
582
583
584 /***********************************************************************
585  *              X11DRV_set_iconic_state
586  *
587  * Set the X11 iconic state according to the window style.
588  */
589 void X11DRV_set_iconic_state( HWND hwnd )
590 {
591     Display *display = thread_display();
592     struct x11drv_win_data *data;
593     RECT rect;
594     XWMHints* wm_hints;
595     DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
596     BOOL iconic = (style & WS_MINIMIZE) != 0;
597
598     if (!(data = X11DRV_get_win_data( hwnd ))) return;
599     if (!data->whole_window || data->whole_window == DefaultRootWindow(display)) return;
600
601     GetWindowRect( hwnd, &rect );
602
603     wine_tsx11_lock();
604
605     if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
606     wm_hints->flags |= StateHint | IconPositionHint;
607     wm_hints->initial_state = iconic ? IconicState : NormalState;
608     wm_hints->icon_x = rect.left;
609     wm_hints->icon_y = rect.top;
610     XSetWMHints( display, data->whole_window, wm_hints );
611
612     if (style & WS_VISIBLE)
613     {
614         if (iconic)
615             XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
616         else
617             if (X11DRV_is_window_rect_mapped( &rect ))
618                 XMapWindow( display, data->whole_window );
619     }
620
621     XFree(wm_hints);
622     wine_tsx11_unlock();
623 }
624
625
626 /***********************************************************************
627  *              X11DRV_window_to_X_rect
628  *
629  * Convert a rect from client to X window coordinates
630  */
631 void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
632 {
633     RECT rc;
634
635     if (!data->managed) return;
636     if (IsRectEmpty( rect )) return;
637
638     rc.top = rc.bottom = rc.left = rc.right = 0;
639
640     AdjustWindowRectEx( &rc, GetWindowLongW( data->hwnd, GWL_STYLE ) & ~(WS_HSCROLL|WS_VSCROLL),
641                         FALSE, GetWindowLongW( data->hwnd, GWL_EXSTYLE ) );
642
643     rect->left   -= rc.left;
644     rect->right  -= rc.right;
645     rect->top    -= rc.top;
646     rect->bottom -= rc.bottom;
647     if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
648     if (rect->left >= rect->right) rect->right = rect->left + 1;
649 }
650
651
652 /***********************************************************************
653  *              X11DRV_X_to_window_rect
654  *
655  * Opposite of X11DRV_window_to_X_rect
656  */
657 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
658 {
659     if (!data->managed) return;
660     if (IsRectEmpty( rect )) return;
661
662     AdjustWindowRectEx( rect, GetWindowLongW( data->hwnd, GWL_STYLE ) & ~(WS_HSCROLL|WS_VSCROLL),
663                         FALSE, GetWindowLongW( data->hwnd, GWL_EXSTYLE ));
664
665     if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
666     if (rect->left >= rect->right) rect->right = rect->left + 1;
667 }
668
669
670 /***********************************************************************
671  *              X11DRV_sync_window_position
672  *
673  * Synchronize the X window position with the Windows one
674  */
675 void X11DRV_sync_window_position( Display *display, struct x11drv_win_data *data,
676                                   UINT swp_flags, const RECT *new_client_rect,
677                                   const RECT *new_whole_rect )
678 {
679     XWindowChanges changes;
680     int mask;
681     RECT old_whole_rect;
682
683     old_whole_rect = data->whole_rect;
684     data->whole_rect = *new_whole_rect;
685
686     data->client_rect = *new_client_rect;
687     OffsetRect( &data->client_rect, -data->whole_rect.left, -data->whole_rect.top );
688
689     if (!data->whole_window || data->lock_changes) return;
690
691     mask = get_window_changes( &changes, &old_whole_rect, &data->whole_rect );
692
693     if (!(swp_flags & SWP_NOZORDER))
694     {
695         /* find window that this one must be after */
696         HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
697         while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
698             prev = GetWindow( prev, GW_HWNDPREV );
699         if (!prev)  /* top child */
700         {
701             changes.stack_mode = Above;
702             mask |= CWStackMode;
703         }
704         else
705         {
706             /* should use stack_mode Below but most window managers don't get it right */
707             /* so move it above the next one in Z order */
708             HWND next = GetWindow( data->hwnd, GW_HWNDNEXT );
709             while (next && !(GetWindowLongW( next, GWL_STYLE ) & WS_VISIBLE))
710                 next = GetWindow( next, GW_HWNDNEXT );
711             if (next)
712             {
713                 changes.stack_mode = Above;
714                 changes.sibling = X11DRV_get_whole_window(next);
715                 mask |= CWStackMode | CWSibling;
716             }
717         }
718     }
719
720     if (mask)
721     {
722         DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
723
724         TRACE( "setting win %lx pos %ld,%ld,%ldx%ld after %lx changes=%x\n",
725                data->whole_window, data->whole_rect.left, data->whole_rect.top,
726                data->whole_rect.right - data->whole_rect.left,
727                data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
728
729         wine_tsx11_lock();
730         if (mask & (CWWidth|CWHeight)) set_size_hints( display, data, style );
731         XReconfigureWMWindow( display, data->whole_window,
732                               DefaultScreen(display), mask, &changes );
733         wine_tsx11_unlock();
734     }
735 }
736
737
738 /**********************************************************************
739  *              create_whole_window
740  *
741  * Create the whole X window for a given window
742  */
743 static Window create_whole_window( Display *display, struct x11drv_win_data *data, DWORD style )
744 {
745     int cx, cy, mask;
746     XSetWindowAttributes attr;
747     XIM xim;
748     RECT rect;
749
750     rect = data->window_rect;
751     X11DRV_window_to_X_rect( data, &rect );
752
753     if (!(cx = rect.right - rect.left)) cx = 1;
754     if (!(cy = rect.bottom - rect.top)) cy = 1;
755
756     mask = get_window_attributes( display, data, &attr );
757
758     /* set the attributes that don't change over the lifetime of the window */
759     attr.bit_gravity   = NorthWestGravity;
760     attr.backing_store = NotUseful;
761     attr.event_mask    = (ExposureMask | PointerMotionMask |
762                           ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
763                           KeyPressMask | KeyReleaseMask | StructureNotifyMask |
764                           FocusChangeMask | KeymapStateMask);
765     mask |= CWBitGravity | CWBackingStore | CWEventMask;
766
767     wine_tsx11_lock();
768
769     data->whole_rect = rect;
770     data->whole_window = XCreateWindow( display, root_window, rect.left, rect.top, cx, cy,
771                                         0, screen_depth, InputOutput, visual,
772                                         mask, &attr );
773
774     if (!data->whole_window)
775     {
776         wine_tsx11_unlock();
777         return 0;
778     }
779     XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
780
781     /* non-maximized child must be at bottom of Z order */
782     if ((style & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
783     {
784         XWindowChanges changes;
785         changes.stack_mode = Below;
786         XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
787     }
788     wine_tsx11_unlock();
789
790     xim = x11drv_thread_data()->xim;
791     if (xim) data->xic = X11DRV_CreateIC( xim, display, data->whole_window );
792
793     X11DRV_set_wm_hints( display, data );
794
795     SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
796     return data->whole_window;
797 }
798
799
800 /**********************************************************************
801  *              destroy_whole_window
802  *
803  * Destroy the whole X window for a given window.
804  */
805 static void destroy_whole_window( Display *display, struct x11drv_win_data *data )
806 {
807     struct x11drv_thread_data *thread_data = x11drv_thread_data();
808
809     if (!data->whole_window) return;
810
811     TRACE( "win %p xwin %lx\n", data->hwnd, data->whole_window );
812     if (thread_data->cursor_window == data->whole_window) thread_data->cursor_window = None;
813     wine_tsx11_lock();
814     XDeleteContext( display, data->whole_window, winContext );
815     if (data->whole_window != DefaultRootWindow(display))
816         XDestroyWindow( display, data->whole_window );
817     data->whole_window = 0;
818     if (data->xic)
819     {
820         XUnsetICFocus( data->xic );
821         XDestroyIC( data->xic );
822     }
823     /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
824     XFlush( display );
825     wine_tsx11_unlock();
826     RemovePropA( data->hwnd, whole_window_prop );
827 }
828
829
830 /*****************************************************************
831  *              SetWindowText   (X11DRV.@)
832  */
833 void X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
834 {
835     Display *display = thread_display();
836     UINT count;
837     char *buffer;
838     char *utf8_buffer;
839     Window win;
840     XTextProperty prop;
841
842     if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(display))
843     {
844         /* allocate new buffer for window text */
845         count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
846         if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count )))
847         {
848             ERR("Not enough memory for window text\n");
849             return;
850         }
851         WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
852
853         count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
854         if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
855         {
856             ERR("Not enough memory for window text in UTF-8\n");
857             HeapFree( GetProcessHeap(), 0, buffer );
858             return;
859         }
860         WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
861
862         wine_tsx11_lock();
863         if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
864         {
865             XSetWMName( display, win, &prop );
866             XSetWMIconName( display, win, &prop );
867             XFree( prop.value );
868         }
869         /*
870         Implements a NET_WM UTF-8 title. It should be without a trailing \0,
871         according to the standard
872         ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
873         */
874         XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
875                          8, PropModeReplace, (unsigned char *) utf8_buffer, count);
876         wine_tsx11_unlock();
877
878         HeapFree( GetProcessHeap(), 0, utf8_buffer );
879         HeapFree( GetProcessHeap(), 0, buffer );
880     }
881 }
882
883
884 /***********************************************************************
885  *              DestroyWindow   (X11DRV.@)
886  */
887 void X11DRV_DestroyWindow( HWND hwnd )
888 {
889     struct x11drv_thread_data *thread_data = x11drv_thread_data();
890     Display *display = thread_data->display;
891     struct x11drv_win_data *data;
892
893     if (!(data = X11DRV_get_win_data( hwnd ))) return;
894
895     free_window_dce( data );
896     destroy_whole_window( display, data );
897     destroy_icon_window( display, data );
898
899     if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
900     if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
901     if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
902     wine_tsx11_lock();
903     XDeleteContext( display, (XID)hwnd, win_data_context );
904     wine_tsx11_unlock();
905     HeapFree( GetProcessHeap(), 0, data );
906 }
907
908
909 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
910 {
911     struct x11drv_win_data *data;
912
913     if ((data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data))))
914     {
915         data->hwnd          = hwnd;
916         data->whole_window  = 0;
917         data->icon_window   = 0;
918         data->xic           = 0;
919         data->managed       = FALSE;
920         data->dce           = NULL;
921         data->lock_changes  = 0;
922         data->hWMIconBitmap = 0;
923         data->hWMIconMask   = 0;
924
925         wine_tsx11_lock();
926         if (!winContext) winContext = XUniqueContext();
927         if (!win_data_context) win_data_context = XUniqueContext();
928         XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
929         wine_tsx11_unlock();
930     }
931     return data;
932 }
933
934
935 /* fill in the desktop X window id in the x11drv_win_data structure */
936 static void get_desktop_xwin( Display *display, struct x11drv_win_data *data )
937 {
938     RECT rect;
939     Window win = (Window)GetPropA( data->hwnd, whole_window_prop );
940
941     if (win)
942     {
943         unsigned int width, height;
944
945         /* retrieve the real size of the desktop */
946         SERVER_START_REQ( get_window_rectangles )
947         {
948             req->handle = data->hwnd;
949             wine_server_call( req );
950             width  = reply->window.right - reply->window.left;
951             height = reply->window.bottom - reply->window.top;
952         }
953         SERVER_END_REQ;
954         data->whole_window = win;
955         if (win != root_window) X11DRV_init_desktop( win, width, height );
956     }
957     else
958     {
959         VisualID visualid;
960
961         wine_tsx11_lock();
962         visualid = XVisualIDFromVisual(visual);
963         wine_tsx11_unlock();
964         SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
965         SetPropA( data->hwnd, visual_id_prop, (HANDLE)visualid );
966         data->whole_window = root_window;
967         SetRect( &rect, 0, 0, screen_width, screen_height );
968         X11DRV_set_window_pos( data->hwnd, 0, &rect, &rect, SWP_NOZORDER, NULL );
969         if (root_window != DefaultRootWindow( display ))
970         {
971             data->managed = TRUE;
972             SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
973         }
974     }
975 }
976
977 /**********************************************************************
978  *              CreateDesktopWindow   (X11DRV.@)
979  */
980 BOOL X11DRV_CreateDesktopWindow( HWND hwnd )
981 {
982     Display *display = thread_display();
983     struct x11drv_win_data *data;
984
985     if (!(data = alloc_win_data( display, hwnd ))) return FALSE;
986
987     get_desktop_xwin( display, data );
988
989     return TRUE;
990 }
991
992
993 /**********************************************************************
994  *              CreateWindow   (X11DRV.@)
995  */
996 BOOL X11DRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
997 {
998     Display *display = thread_display();
999     WND *wndPtr;
1000     struct x11drv_win_data *data;
1001     HWND insert_after;
1002     RECT rect;
1003     DWORD style;
1004     CBT_CREATEWNDA cbtc;
1005     CREATESTRUCTA cbcs;
1006     BOOL ret = FALSE;
1007
1008     if (!(data = alloc_win_data( display, hwnd ))) return FALSE;
1009
1010     if (cs->cx > 65535)
1011     {
1012         ERR( "invalid window width %d\n", cs->cx );
1013         cs->cx = 65535;
1014     }
1015     if (cs->cy > 65535)
1016     {
1017         ERR( "invalid window height %d\n", cs->cy );
1018         cs->cy = 65535;
1019     }
1020     if (cs->cx < 0)
1021     {
1022         ERR( "invalid window width %d\n", cs->cx );
1023         cs->cx = 0;
1024     }
1025     if (cs->cy < 0)
1026     {
1027         ERR( "invalid window height %d\n", cs->cy );
1028         cs->cy = 0;
1029     }
1030
1031     /* initialize the dimensions before sending WM_GETMINMAXINFO */
1032     SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
1033     X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER, NULL );
1034
1035     /* create an X window if it's a top level window */
1036     if (GetAncestor( hwnd, GA_PARENT ) == GetDesktopWindow())
1037     {
1038         if (!create_whole_window( display, data, cs->style )) goto failed;
1039     }
1040     else if (hwnd == GetDesktopWindow())
1041     {
1042         get_desktop_xwin( display, data );
1043     }
1044
1045     /* get class or window DC if needed */
1046     alloc_window_dce( data );
1047
1048     /* Call the WH_CBT hook */
1049
1050     /* the window style passed to the hook must be the real window style,
1051      * rather than just the window style that the caller to CreateWindowEx
1052      * passed in, so we have to copy the original CREATESTRUCT and get the
1053      * the real style. */
1054     cbcs = *cs;
1055     cbcs.style = GetWindowLongW(hwnd, GWL_STYLE);
1056
1057     cbtc.lpcs = &cbcs;
1058     cbtc.hwndInsertAfter = HWND_TOP;
1059     if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode ))
1060     {
1061         TRACE("CBT-hook returned !0\n");
1062         goto failed;
1063     }
1064
1065     /* Send the WM_GETMINMAXINFO message and fix the size if needed */
1066     if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
1067     {
1068         POINT maxSize, maxPos, minTrack, maxTrack;
1069
1070         WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
1071         if (maxSize.x < cs->cx) cs->cx = maxSize.x;
1072         if (maxSize.y < cs->cy) cs->cy = maxSize.y;
1073         if (cs->cx < 0) cs->cx = 0;
1074         if (cs->cy < 0) cs->cy = 0;
1075
1076         SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
1077         if (!X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER, NULL )) return FALSE;
1078     }
1079
1080     /* send WM_NCCREATE */
1081     TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cs->cx, cs->cy );
1082     if (unicode)
1083         ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1084     else
1085         ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1086     if (!ret)
1087     {
1088         WARN("aborted by WM_xxCREATE!\n");
1089         return FALSE;
1090     }
1091
1092     /* make sure the window is still valid */
1093     if (!(data = X11DRV_get_win_data( hwnd ))) return FALSE;
1094     if (data->whole_window) X11DRV_sync_window_style( display, data );
1095
1096     /* send WM_NCCALCSIZE */
1097     rect = data->window_rect;
1098     SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&rect );
1099
1100     if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
1101
1102     /* yes, even if the CBT hook was called with HWND_TOP */
1103     insert_after = ((wndPtr->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
1104
1105     X11DRV_set_window_pos( hwnd, insert_after, &wndPtr->rectWindow, &rect, 0, NULL );
1106
1107     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\n",
1108            hwnd, wndPtr->rectWindow.left, wndPtr->rectWindow.top,
1109            wndPtr->rectWindow.right, wndPtr->rectWindow.bottom,
1110            wndPtr->rectClient.left, wndPtr->rectClient.top,
1111            wndPtr->rectClient.right, wndPtr->rectClient.bottom,
1112            data->whole_rect.left, data->whole_rect.top,
1113            data->whole_rect.right, data->whole_rect.bottom,
1114            data->client_rect.left, data->client_rect.top,
1115            data->client_rect.right, data->client_rect.bottom,
1116            (unsigned int)data->whole_window );
1117
1118     WIN_ReleasePtr( wndPtr );
1119
1120     if (unicode)
1121         ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1122     else
1123         ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1124
1125     if (!ret) return FALSE;
1126
1127     NotifyWinEvent(EVENT_OBJECT_CREATE, hwnd, OBJID_WINDOW, 0);
1128
1129     /* Send the size messages */
1130
1131     if (!(wndPtr = WIN_GetPtr(hwnd)) || wndPtr == WND_OTHER_PROCESS) return FALSE;
1132     if (!(wndPtr->flags & WIN_NEED_SIZE))
1133     {
1134         RECT rect = wndPtr->rectClient;
1135         WIN_ReleasePtr( wndPtr );
1136         /* send it anyway */
1137         if (((rect.right-rect.left) <0) ||((rect.bottom-rect.top)<0))
1138             WARN("sending bogus WM_SIZE message 0x%08lx\n",
1139                  MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1140         SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1141                       MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1142         SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( rect.left, rect.top ) );
1143     }
1144     else WIN_ReleasePtr( wndPtr );
1145
1146     /* Show the window, maximizing or minimizing if needed */
1147
1148     style = GetWindowLongW( hwnd, GWL_STYLE );
1149     if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1150     {
1151         extern UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect ); /*FIXME*/
1152
1153         RECT newPos;
1154         UINT swFlag = (style & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1155         WIN_SetStyle( hwnd, 0, WS_MAXIMIZE | WS_MINIMIZE );
1156         WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1157
1158         swFlag = SWP_FRAMECHANGED | SWP_NOZORDER; /* Frame always gets changed */
1159         if (!(style & WS_VISIBLE) || (style & WS_CHILD) || GetActiveWindow()) swFlag |= SWP_NOACTIVATE;
1160
1161         SetWindowPos( hwnd, 0, newPos.left, newPos.top,
1162                       newPos.right, newPos.bottom, swFlag );
1163     }
1164
1165     /* Dock system tray windows. */
1166     /* Dock after the window is created so we don't have problems calling
1167      * SetWindowPos. */
1168     if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_TRAYWINDOW)
1169         systray_dock_window( display, data );
1170
1171     return TRUE;
1172
1173  failed:
1174     X11DRV_DestroyWindow( hwnd );
1175     return FALSE;
1176 }
1177
1178
1179 /***********************************************************************
1180  *              X11DRV_get_win_data
1181  *
1182  * Return the X11 data structure associated with a window.
1183  */
1184 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1185 {
1186     char *data;
1187
1188     if (!hwnd || XFindContext( thread_display(), (XID)hwnd, win_data_context, &data )) data = NULL;
1189     return (struct x11drv_win_data *)data;
1190 }
1191
1192
1193 /***********************************************************************
1194  *              X11DRV_get_whole_window
1195  *
1196  * Return the X window associated with the full area of a window
1197  */
1198 Window X11DRV_get_whole_window( HWND hwnd )
1199 {
1200     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1201
1202     if (!data) return (Window)GetPropA( hwnd, whole_window_prop );
1203     return data->whole_window;
1204 }
1205
1206
1207 /***********************************************************************
1208  *              X11DRV_get_ic
1209  *
1210  * Return the X input context associated with a window
1211  */
1212 XIC X11DRV_get_ic( HWND hwnd )
1213 {
1214     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1215
1216     if (!data) return 0;
1217     return data->xic;
1218 }
1219
1220
1221 /*****************************************************************
1222  *              SetParent   (X11DRV.@)
1223  */
1224 HWND X11DRV_SetParent( HWND hwnd, HWND parent )
1225 {
1226     Display *display = thread_display();
1227     WND *wndPtr;
1228     BOOL ret;
1229     HWND old_parent = 0;
1230
1231     /* Windows hides the window first, then shows it again
1232      * including the WM_SHOWWINDOW messages and all */
1233     BOOL was_visible = ShowWindow( hwnd, SW_HIDE );
1234
1235     wndPtr = WIN_GetPtr( hwnd );
1236     if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return 0;
1237
1238     SERVER_START_REQ( set_parent )
1239     {
1240         req->handle = hwnd;
1241         req->parent = parent;
1242         if ((ret = !wine_server_call( req )))
1243         {
1244             old_parent = reply->old_parent;
1245             wndPtr->parent = parent = reply->full_parent;
1246         }
1247
1248     }
1249     SERVER_END_REQ;
1250     WIN_ReleasePtr( wndPtr );
1251     if (!ret) return 0;
1252
1253     if (parent != old_parent)
1254     {
1255         struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1256
1257         if (!data) return 0;
1258
1259         if (parent != GetDesktopWindow()) /* a child window */
1260         {
1261             if (old_parent == GetDesktopWindow())
1262             {
1263                 /* destroy the old X windows */
1264                 destroy_whole_window( display, data );
1265                 destroy_icon_window( display, data );
1266                 if (data->managed)
1267                 {
1268                     data->managed = FALSE;
1269                     RemovePropA( data->hwnd, managed_prop );
1270                 }
1271             }
1272         }
1273         else  /* new top level window */
1274         {
1275             /* FIXME: we ignore errors since we can't really recover anyway */
1276             create_whole_window( display, data, GetWindowLongW( hwnd, GWL_STYLE ) );
1277         }
1278     }
1279
1280     /* SetParent additionally needs to make hwnd the topmost window
1281        in the x-order and send the expected WM_WINDOWPOSCHANGING and
1282        WM_WINDOWPOSCHANGED notification messages.
1283     */
1284     SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0,
1285                   SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | (was_visible ? SWP_SHOWWINDOW : 0) );
1286     /* FIXME: a WM_MOVE is also generated (in the DefWindowProc handler
1287      * for WM_WINDOWPOSCHANGED) in Windows, should probably remove SWP_NOMOVE */
1288
1289     return old_parent;
1290 }
1291
1292
1293 /*****************************************************************
1294  *              SetFocus   (X11DRV.@)
1295  *
1296  * Set the X focus.
1297  * Explicit colormap management seems to work only with OLVWM.
1298  */
1299 void X11DRV_SetFocus( HWND hwnd )
1300 {
1301     Display *display = thread_display();
1302     struct x11drv_win_data *data;
1303     XWindowAttributes win_attr;
1304
1305     /* Only mess with the X focus if there's */
1306     /* no desktop window and if the window is not managed by the WM. */
1307     if (root_window != DefaultRootWindow(display)) return;
1308
1309     if (!hwnd)  /* If setting the focus to 0, uninstall the colormap */
1310     {
1311         wine_tsx11_lock();
1312         if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1313             XUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1314         wine_tsx11_unlock();
1315         return;
1316     }
1317
1318     hwnd = GetAncestor( hwnd, GA_ROOT );
1319
1320     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1321     if (data->managed || !data->whole_window) return;
1322
1323     /* Set X focus and install colormap */
1324     wine_tsx11_lock();
1325     if (XGetWindowAttributes( display, data->whole_window, &win_attr ) &&
1326         (win_attr.map_state == IsViewable))
1327     {
1328         /* If window is not viewable, don't change anything */
1329
1330         /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1331         /* FIXME: this is not entirely correct */
1332         XSetInputFocus( display, data->whole_window, RevertToParent,
1333                         /* CurrentTime */
1334                         GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1335         if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1336             XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1337     }
1338     wine_tsx11_unlock();
1339 }
1340
1341
1342 /**********************************************************************
1343  *              SetWindowIcon (X11DRV.@)
1344  *
1345  * hIcon or hIconSm has changed (or is being initialised for the
1346  * first time). Complete the X11 driver-specific initialisation
1347  * and set the window hints.
1348  *
1349  * This is not entirely correct, may need to create
1350  * an icon window and set the pixmap as a background
1351  */
1352 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
1353 {
1354     Display *display = thread_display();
1355     struct x11drv_win_data *data;
1356     XWMHints* wm_hints;
1357
1358     if (type != ICON_BIG) return;  /* nothing to do here */
1359
1360     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1361     if (!data->whole_window) return;
1362     if (!data->managed) return;
1363
1364     wine_tsx11_lock();
1365     if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
1366     wine_tsx11_unlock();
1367     if (wm_hints)
1368     {
1369         set_icon_hints( display, data, wm_hints, icon );
1370         wine_tsx11_lock();
1371         XSetWMHints( display, data->whole_window, wm_hints );
1372         XFree( wm_hints );
1373         wine_tsx11_unlock();
1374     }
1375 }