push bf834a7eef2241618d351018da1587a7ae2466d1
[wine/hacks.git] / dlls / winex11.drv / window.c
blobdb34f20c72a4fb39c2972a0291f167d7ce1046b7
1 /*
2 * Window related functions
4 * Copyright 1993, 1994, 1995, 1996, 2001 Alexandre Julliard
5 * Copyright 1993 David Metcalfe
6 * Copyright 1995, 1996 Alex Korobka
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.
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.
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
23 #include "config.h"
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
32 #include <X11/Xlib.h>
33 #include <X11/Xresource.h>
34 #include <X11/Xutil.h>
35 #ifdef HAVE_LIBXSHAPE
36 #include <X11/extensions/shape.h>
37 #endif /* HAVE_LIBXSHAPE */
39 #include "windef.h"
40 #include "winbase.h"
41 #include "wingdi.h"
42 #include "winuser.h"
43 #include "wine/unicode.h"
45 #include "x11drv.h"
46 #include "xcomposite.h"
47 #include "wine/debug.h"
48 #include "wine/server.h"
49 #include "mwm.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
53 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
54 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
55 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
56 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
57 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
58 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
59 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
60 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
61 #define _NET_WM_MOVERESIZE_MOVE 8
62 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9
63 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10
65 #define _NET_WM_STATE_REMOVE 0
66 #define _NET_WM_STATE_ADD 1
67 #define _NET_WM_STATE_TOGGLE 2
69 #define SWP_AGG_NOPOSCHANGE (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
71 /* X context to associate a hwnd to an X window */
72 XContext winContext = 0;
74 /* X context to associate a struct x11drv_win_data to an hwnd */
75 static XContext win_data_context;
77 static const char whole_window_prop[] = "__wine_x11_whole_window";
78 static const char client_window_prop[]= "__wine_x11_client_window";
79 static const char icon_window_prop[] = "__wine_x11_icon_window";
80 static const char fbconfig_id_prop[] = "__wine_x11_fbconfig_id";
81 static const char gl_drawable_prop[] = "__wine_x11_gl_drawable";
82 static const char pixmap_prop[] = "__wine_x11_pixmap";
83 static const char managed_prop[] = "__wine_x11_managed";
86 /***********************************************************************
87 * http://standards.freedesktop.org/startup-notification-spec
89 static void remove_startup_notification(Display *display, Window window)
91 static LONG startup_notification_removed = 0;
92 char id[1024];
93 char message[1024];
94 int i;
95 int pos;
96 XEvent xevent;
97 const char *src;
98 int srclen;
100 if (InterlockedCompareExchange(&startup_notification_removed, 1, 0) != 0)
101 return;
103 if (GetEnvironmentVariableA("DESKTOP_STARTUP_ID", id, sizeof(id)) == 0)
104 return;
105 SetEnvironmentVariableA("DESKTOP_STARTUP_ID", NULL);
107 pos = snprintf(message, sizeof(message), "remove: ID=");
108 message[pos++] = '"';
109 for (i = 0; id[i] && pos < sizeof(message) - 2; i++)
111 if (id[i] == '"' || id[i] == '\\')
112 message[pos++] = '\\';
113 message[pos++] = id[i];
115 message[pos++] = '"';
116 message[pos++] = '\0';
118 xevent.xclient.type = ClientMessage;
119 xevent.xclient.message_type = x11drv_atom(_NET_STARTUP_INFO_BEGIN);
120 xevent.xclient.display = display;
121 xevent.xclient.window = window;
122 xevent.xclient.format = 8;
124 src = message;
125 srclen = strlen(src) + 1;
127 wine_tsx11_lock();
128 while (srclen > 0)
130 int msglen = srclen;
131 if (msglen > 20)
132 msglen = 20;
133 memset(&xevent.xclient.data.b[0], 0, 20);
134 memcpy(&xevent.xclient.data.b[0], src, msglen);
135 src += msglen;
136 srclen -= msglen;
138 XSendEvent( display, DefaultRootWindow( display ), False, PropertyChangeMask, &xevent );
139 xevent.xclient.message_type = x11drv_atom(_NET_STARTUP_INFO);
141 wine_tsx11_unlock();
145 /***********************************************************************
146 * is_window_managed
148 * Check if a given window should be managed
150 static BOOL is_window_managed( HWND hwnd, UINT swp_flags, const RECT *window_rect )
152 DWORD style, ex_style;
154 if (!managed_mode) return FALSE;
156 /* child windows are not managed */
157 style = GetWindowLongW( hwnd, GWL_STYLE );
158 if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD) return FALSE;
159 /* activated windows are managed */
160 if (!(swp_flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) return TRUE;
161 if (hwnd == GetActiveWindow()) return TRUE;
162 /* windows with caption are managed */
163 if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
164 /* windows with thick frame are managed */
165 if (style & WS_THICKFRAME) return TRUE;
166 if (style & WS_POPUP)
168 HMONITOR hmon;
169 MONITORINFO mi;
171 /* popup with sysmenu == caption are managed */
172 if (style & WS_SYSMENU) return TRUE;
173 /* full-screen popup windows are managed */
174 hmon = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY );
175 mi.cbSize = sizeof( mi );
176 GetMonitorInfoW( hmon, &mi );
177 if (window_rect->left <= mi.rcWork.left && window_rect->right >= mi.rcWork.right &&
178 window_rect->top <= mi.rcWork.top && window_rect->bottom >= mi.rcWork.bottom)
179 return TRUE;
181 /* application windows are managed */
182 ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
183 if (ex_style & WS_EX_APPWINDOW) return TRUE;
184 /* default: not managed */
185 return FALSE;
189 /***********************************************************************
190 * X11DRV_is_window_rect_mapped
192 * Check if the X whole window should be mapped based on its rectangle
194 static BOOL is_window_rect_mapped( const RECT *rect )
196 /* don't map if rect is empty */
197 if (IsRectEmpty( rect )) return FALSE;
199 /* don't map if rect is off-screen */
200 if (rect->left >= virtual_screen_rect.right ||
201 rect->top >= virtual_screen_rect.bottom ||
202 rect->right <= virtual_screen_rect.left ||
203 rect->bottom <= virtual_screen_rect.top)
204 return FALSE;
206 return TRUE;
210 /***********************************************************************
211 * is_window_resizable
213 * Check if window should be made resizable by the window manager
215 static inline BOOL is_window_resizable( struct x11drv_win_data *data, DWORD style )
217 if (style & WS_THICKFRAME) return TRUE;
218 /* Metacity needs the window to be resizable to make it fullscreen */
219 return (data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
220 data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height);
224 /***********************************************************************
225 * get_window_owner
227 static HWND get_window_owner( HWND hwnd )
229 RECT rect;
230 HWND owner = GetWindow( hwnd, GW_OWNER );
231 /* ignore the zero-size owners used by Delphi apps */
232 if (owner && GetWindowRect( owner, &rect ) && IsRectEmpty( &rect )) owner = 0;
233 return owner;
237 /***********************************************************************
238 * get_mwm_decorations
240 static unsigned long get_mwm_decorations( struct x11drv_win_data *data,
241 DWORD style, DWORD ex_style )
243 unsigned long ret = 0;
245 if (!decorated_mode) return 0;
247 if (IsRectEmpty( &data->window_rect )) return 0;
248 if (data->shaped) return 0;
250 if (ex_style & WS_EX_TOOLWINDOW) return 0;
252 if ((style & WS_CAPTION) == WS_CAPTION)
254 ret |= MWM_DECOR_TITLE | MWM_DECOR_BORDER;
255 if (style & WS_SYSMENU) ret |= MWM_DECOR_MENU;
256 if (style & WS_MINIMIZEBOX) ret |= MWM_DECOR_MINIMIZE;
257 if (style & WS_MAXIMIZEBOX) ret |= MWM_DECOR_MAXIMIZE;
259 if (ex_style & WS_EX_DLGMODALFRAME) ret |= MWM_DECOR_BORDER;
260 else if (style & WS_THICKFRAME) ret |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
261 else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) ret |= MWM_DECOR_BORDER;
262 return ret;
266 /***********************************************************************
267 * get_x11_rect_offset
269 * Helper for X11DRV_window_to_X_rect and X11DRV_X_to_window_rect.
271 static void get_x11_rect_offset( struct x11drv_win_data *data, RECT *rect )
273 DWORD style, ex_style, style_mask = 0, ex_style_mask = 0;
274 unsigned long decor;
276 rect->top = rect->bottom = rect->left = rect->right = 0;
278 style = GetWindowLongW( data->hwnd, GWL_STYLE );
279 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
280 decor = get_mwm_decorations( data, style, ex_style );
282 if (decor & MWM_DECOR_TITLE) style_mask |= WS_CAPTION;
283 if (decor & MWM_DECOR_BORDER)
285 style_mask |= WS_DLGFRAME | WS_THICKFRAME;
286 ex_style_mask |= WS_EX_DLGMODALFRAME;
289 AdjustWindowRectEx( rect, style & style_mask, FALSE, ex_style & ex_style_mask );
293 /***********************************************************************
294 * get_window_attributes
296 * Fill the window attributes structure for an X window.
298 static int get_window_attributes( Display *display, struct x11drv_win_data *data,
299 XSetWindowAttributes *attr )
301 attr->override_redirect = !data->managed;
302 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
303 attr->save_under = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
304 attr->cursor = x11drv_thread_data()->cursor;
305 attr->bit_gravity = NorthWestGravity;
306 attr->win_gravity = StaticGravity;
307 attr->backing_store = NotUseful;
308 attr->event_mask = (ExposureMask | PointerMotionMask |
309 ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
310 KeyPressMask | KeyReleaseMask | FocusChangeMask | KeymapStateMask);
311 if (data->managed) attr->event_mask |= StructureNotifyMask | PropertyChangeMask;
313 return (CWOverrideRedirect | CWSaveUnder | CWColormap | CWCursor |
314 CWEventMask | CWBitGravity | CWBackingStore);
318 /***********************************************************************
319 * create_client_window
321 static Window create_client_window( Display *display, struct x11drv_win_data *data, XVisualInfo *vis )
323 int cx, cy, mask;
324 XSetWindowAttributes attr;
325 Window client;
326 Visual *client_visual = vis ? vis->visual : visual;
328 attr.bit_gravity = NorthWestGravity;
329 attr.win_gravity = NorthWestGravity;
330 attr.backing_store = NotUseful;
331 attr.event_mask = (ExposureMask | PointerMotionMask |
332 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
333 mask = CWEventMask | CWBitGravity | CWWinGravity | CWBackingStore;
335 if ((cx = data->client_rect.right - data->client_rect.left) <= 0) cx = 1;
336 if ((cy = data->client_rect.bottom - data->client_rect.top) <= 0) cy = 1;
338 wine_tsx11_lock();
340 if (vis)
342 attr.colormap = XCreateColormap( display, root_window, vis->visual,
343 (vis->class == PseudoColor || vis->class == GrayScale ||
344 vis->class == DirectColor) ? AllocAll : AllocNone );
345 mask |= CWColormap;
348 client = XCreateWindow( display, data->whole_window,
349 data->client_rect.left - data->whole_rect.left,
350 data->client_rect.top - data->whole_rect.top,
351 cx, cy, 0, screen_depth, InputOutput,
352 client_visual, mask, &attr );
353 if (!client)
355 wine_tsx11_unlock();
356 return 0;
359 if (data->client_window)
361 struct x11drv_thread_data *thread_data = x11drv_thread_data();
362 if (thread_data->cursor_window == data->client_window) thread_data->cursor_window = None;
363 XDeleteContext( display, data->client_window, winContext );
364 XDestroyWindow( display, data->client_window );
366 data->client_window = client;
367 data->visualid = XVisualIDFromVisual( client_visual );
369 if (data->colormap) XFreeColormap( display, data->colormap );
370 data->colormap = vis ? attr.colormap : 0;
372 XMapWindow( display, data->client_window );
373 XSaveContext( display, data->client_window, winContext, (char *)data->hwnd );
374 wine_tsx11_unlock();
376 SetPropA( data->hwnd, client_window_prop, (HANDLE)data->client_window );
377 return data->client_window;
381 /***********************************************************************
382 * sync_window_style
384 * Change the X window attributes when the window style has changed.
386 static void sync_window_style( Display *display, struct x11drv_win_data *data )
388 if (data->whole_window != root_window)
390 XSetWindowAttributes attr;
391 int mask = get_window_attributes( display, data, &attr );
393 wine_tsx11_lock();
394 XChangeWindowAttributes( display, data->whole_window, mask, &attr );
395 wine_tsx11_unlock();
400 /***********************************************************************
401 * sync_window_region
403 * Update the X11 window region.
405 static void sync_window_region( Display *display, struct x11drv_win_data *data, HRGN win_region )
407 #ifdef HAVE_LIBXSHAPE
408 HRGN hrgn = win_region;
410 if (!data->whole_window) return;
411 data->shaped = FALSE;
413 if (hrgn == (HRGN)1) /* hack: win_region == 1 means retrieve region from server */
415 if (!(hrgn = CreateRectRgn( 0, 0, 0, 0 ))) return;
416 if (GetWindowRgn( data->hwnd, hrgn ) == ERROR)
418 DeleteObject( hrgn );
419 hrgn = 0;
423 if (!hrgn)
425 wine_tsx11_lock();
426 XShapeCombineMask( display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
427 wine_tsx11_unlock();
429 else
431 RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
432 if (pRegionData)
434 wine_tsx11_lock();
435 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
436 data->window_rect.left - data->whole_rect.left,
437 data->window_rect.top - data->whole_rect.top,
438 (XRectangle *)pRegionData->Buffer,
439 pRegionData->rdh.nCount, ShapeSet, YXBanded );
440 wine_tsx11_unlock();
441 HeapFree(GetProcessHeap(), 0, pRegionData);
442 data->shaped = TRUE;
445 if (hrgn && hrgn != win_region) DeleteObject( hrgn );
446 #endif /* HAVE_LIBXSHAPE */
450 /***********************************************************************
451 * sync_window_opacity
453 static void sync_window_opacity( Display *display, Window win,
454 COLORREF key, BYTE alpha, DWORD flags )
456 unsigned long opacity = 0xffffffff;
458 if (flags & LWA_ALPHA) opacity = (0xffffffff / 0xff) * alpha;
460 if (flags & LWA_COLORKEY) FIXME("LWA_COLORKEY not supported\n");
462 wine_tsx11_lock();
463 if (opacity == 0xffffffff)
464 XDeleteProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY) );
465 else
466 XChangeProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY),
467 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&opacity, 1 );
468 wine_tsx11_unlock();
472 /***********************************************************************
473 * sync_window_text
475 static void sync_window_text( Display *display, Window win, const WCHAR *text )
477 UINT count;
478 char *buffer, *utf8_buffer;
479 XTextProperty prop;
481 /* allocate new buffer for window text */
482 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
483 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count ))) return;
484 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
486 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
487 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
489 HeapFree( GetProcessHeap(), 0, buffer );
490 return;
492 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
494 wine_tsx11_lock();
495 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
497 XSetWMName( display, win, &prop );
498 XSetWMIconName( display, win, &prop );
499 XFree( prop.value );
502 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
503 according to the standard
504 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
506 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
507 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
508 wine_tsx11_unlock();
510 HeapFree( GetProcessHeap(), 0, utf8_buffer );
511 HeapFree( GetProcessHeap(), 0, buffer );
515 /***********************************************************************
516 * set_win_format
518 static BOOL set_win_format( HWND hwnd, XID fbconfig_id )
520 struct x11drv_win_data *data;
521 XVisualInfo *vis;
522 int w, h;
524 if (!(data = X11DRV_get_win_data(hwnd)) &&
525 !(data = X11DRV_create_win_data(hwnd))) return FALSE;
527 if (!(vis = visual_from_fbconfig_id(fbconfig_id))) return FALSE;
529 if (data->whole_window)
531 Display *display = thread_display();
532 Window client = data->client_window;
534 if (vis->visualid != data->visualid)
536 client = create_client_window( display, data, vis );
537 TRACE( "re-created client window %lx for %p fbconfig %lx\n", client, data->hwnd, fbconfig_id );
539 wine_tsx11_lock();
540 XFree(vis);
541 XFlush( display );
542 wine_tsx11_unlock();
543 if (client) goto done;
544 return FALSE;
547 w = data->client_rect.right - data->client_rect.left;
548 h = data->client_rect.bottom - data->client_rect.top;
550 if(w <= 0) w = 1;
551 if(h <= 0) h = 1;
553 #ifdef SONAME_LIBXCOMPOSITE
554 if(usexcomposite)
556 XSetWindowAttributes attrib;
557 static Window dummy_parent;
559 wine_tsx11_lock();
560 attrib.override_redirect = True;
561 if (!dummy_parent)
563 dummy_parent = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, screen_depth,
564 InputOutput, visual, CWOverrideRedirect, &attrib );
565 XMapWindow( gdi_display, dummy_parent );
567 data->colormap = XCreateColormap(gdi_display, dummy_parent, vis->visual,
568 (vis->class == PseudoColor ||
569 vis->class == GrayScale ||
570 vis->class == DirectColor) ?
571 AllocAll : AllocNone);
572 attrib.colormap = data->colormap;
573 XInstallColormap(gdi_display, attrib.colormap);
575 if(data->gl_drawable) XDestroyWindow(gdi_display, data->gl_drawable);
576 data->gl_drawable = XCreateWindow(gdi_display, dummy_parent, -w, 0, w, h, 0,
577 vis->depth, InputOutput, vis->visual,
578 CWColormap | CWOverrideRedirect,
579 &attrib);
580 if(data->gl_drawable)
582 pXCompositeRedirectWindow(gdi_display, data->gl_drawable,
583 CompositeRedirectManual);
584 XMapWindow(gdi_display, data->gl_drawable);
586 XFree(vis);
587 XFlush( gdi_display );
588 wine_tsx11_unlock();
590 else
591 #endif
593 WARN("XComposite is not available, using GLXPixmap hack\n");
595 wine_tsx11_lock();
597 if(data->pixmap) XFreePixmap(gdi_display, data->pixmap);
598 data->pixmap = XCreatePixmap(gdi_display, root_window, w, h, vis->depth);
599 if(!data->pixmap)
601 XFree(vis);
602 wine_tsx11_unlock();
603 return FALSE;
606 if(data->gl_drawable) destroy_glxpixmap(gdi_display, data->gl_drawable);
607 data->gl_drawable = create_glxpixmap(gdi_display, vis, data->pixmap);
608 if(!data->gl_drawable)
610 XFreePixmap(gdi_display, data->pixmap);
611 data->pixmap = 0;
613 XFree(vis);
614 XFlush( gdi_display );
615 wine_tsx11_unlock();
616 if (data->pixmap) SetPropA(hwnd, pixmap_prop, (HANDLE)data->pixmap);
619 if (!data->gl_drawable) return FALSE;
621 TRACE("Created GL drawable 0x%lx, using FBConfigID 0x%lx\n",
622 data->gl_drawable, fbconfig_id);
623 SetPropA(hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
625 done:
626 data->fbconfig_id = fbconfig_id;
627 SetPropA(hwnd, fbconfig_id_prop, (HANDLE)data->fbconfig_id);
628 /* force DCE invalidation */
629 SetWindowPos( hwnd, 0, 0, 0, 0, 0,
630 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE |
631 SWP_NOREDRAW | SWP_DEFERERASE | SWP_NOSENDCHANGING | SWP_STATECHANGED);
632 return TRUE;
635 /***********************************************************************
636 * sync_gl_drawable
638 static void sync_gl_drawable(struct x11drv_win_data *data)
640 int w = data->client_rect.right - data->client_rect.left;
641 int h = data->client_rect.bottom - data->client_rect.top;
642 XVisualInfo *vis;
643 Drawable glxp;
644 Pixmap pix;
646 if (w <= 0) w = 1;
647 if (h <= 0) h = 1;
649 TRACE("Resizing GL drawable 0x%lx to %dx%d\n", data->gl_drawable, w, h);
650 #ifdef SONAME_LIBXCOMPOSITE
651 if(usexcomposite)
653 wine_tsx11_lock();
654 XMoveResizeWindow(gdi_display, data->gl_drawable, -w, 0, w, h);
655 wine_tsx11_unlock();
656 return;
658 #endif
660 if (!(vis = visual_from_fbconfig_id(data->fbconfig_id))) return;
662 wine_tsx11_lock();
663 pix = XCreatePixmap(gdi_display, root_window, w, h, vis->depth);
664 if(!pix)
666 ERR("Failed to create pixmap for offscreen rendering\n");
667 XFree(vis);
668 wine_tsx11_unlock();
669 return;
672 glxp = create_glxpixmap(gdi_display, vis, pix);
673 if(!glxp)
675 ERR("Failed to create drawable for offscreen rendering\n");
676 XFreePixmap(gdi_display, pix);
677 XFree(vis);
678 wine_tsx11_unlock();
679 return;
682 XFree(vis);
684 mark_drawable_dirty(data->gl_drawable, glxp);
686 XFreePixmap(gdi_display, data->pixmap);
687 destroy_glxpixmap(gdi_display, data->gl_drawable);
688 TRACE( "Recreated GL drawable %lx to replace %lx\n", glxp, data->gl_drawable );
690 data->pixmap = pix;
691 data->gl_drawable = glxp;
693 XFlush( gdi_display );
694 wine_tsx11_unlock();
696 SetPropA(data->hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
697 SetPropA(data->hwnd, pixmap_prop, (HANDLE)data->pixmap);
701 /***********************************************************************
702 * get_window_changes
704 * fill the window changes structure
706 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
708 int mask = 0;
710 if (old->right - old->left != new->right - new->left )
712 if ((changes->width = new->right - new->left) <= 0) changes->width = 1;
713 mask |= CWWidth;
715 if (old->bottom - old->top != new->bottom - new->top)
717 if ((changes->height = new->bottom - new->top) <= 0) changes->height = 1;
718 mask |= CWHeight;
720 if (old->left != new->left)
722 changes->x = new->left;
723 mask |= CWX;
725 if (old->top != new->top)
727 changes->y = new->top;
728 mask |= CWY;
730 return mask;
734 /***********************************************************************
735 * create_icon_window
737 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
739 XSetWindowAttributes attr;
741 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
742 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
743 attr.bit_gravity = NorthWestGravity;
744 attr.backing_store = NotUseful/*WhenMapped*/;
745 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
747 wine_tsx11_lock();
748 data->icon_window = XCreateWindow( display, root_window, 0, 0,
749 GetSystemMetrics( SM_CXICON ),
750 GetSystemMetrics( SM_CYICON ),
751 0, screen_depth,
752 InputOutput, visual,
753 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
754 XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
755 XFlush( display ); /* make sure the window exists before we start painting to it */
756 wine_tsx11_unlock();
758 TRACE( "created %lx\n", data->icon_window );
759 SetPropA( data->hwnd, icon_window_prop, (HANDLE)data->icon_window );
760 return data->icon_window;
765 /***********************************************************************
766 * destroy_icon_window
768 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
770 if (!data->icon_window) return;
771 if (x11drv_thread_data()->cursor_window == data->icon_window)
772 x11drv_thread_data()->cursor_window = None;
773 wine_tsx11_lock();
774 XDeleteContext( display, data->icon_window, winContext );
775 XDestroyWindow( display, data->icon_window );
776 data->icon_window = 0;
777 wine_tsx11_unlock();
778 RemovePropA( data->hwnd, icon_window_prop );
782 /***********************************************************************
783 * set_icon_hints
785 * Set the icon wm hints
787 static void set_icon_hints( Display *display, struct x11drv_win_data *data, HICON hIcon )
789 XWMHints *hints = data->wm_hints;
791 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
792 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
793 data->hWMIconBitmap = 0;
794 data->hWMIconMask = 0;
796 if (!hIcon)
798 if (!data->icon_window) create_icon_window( display, data );
799 hints->icon_window = data->icon_window;
800 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
802 else
804 HBITMAP hbmOrig;
805 RECT rcMask;
806 BITMAP bmMask;
807 ICONINFO ii;
808 HDC hDC;
810 GetIconInfo(hIcon, &ii);
812 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
813 rcMask.top = 0;
814 rcMask.left = 0;
815 rcMask.right = bmMask.bmWidth;
816 rcMask.bottom = bmMask.bmHeight;
818 hDC = CreateCompatibleDC(0);
819 hbmOrig = SelectObject(hDC, ii.hbmMask);
820 InvertRect(hDC, &rcMask);
821 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
822 SelectObject(hDC, hbmOrig);
823 DeleteDC(hDC);
825 data->hWMIconBitmap = ii.hbmColor;
826 data->hWMIconMask = ii.hbmMask;
828 hints->icon_pixmap = X11DRV_get_pixmap(data->hWMIconBitmap);
829 hints->icon_mask = X11DRV_get_pixmap(data->hWMIconMask);
830 destroy_icon_window( display, data );
831 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
836 /***********************************************************************
837 * set_size_hints
839 * set the window size hints
841 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
843 XSizeHints* size_hints;
845 if (!(size_hints = XAllocSizeHints())) return;
847 size_hints->win_gravity = StaticGravity;
848 size_hints->flags |= PWinGravity;
850 /* don't update size hints if window is not in normal state */
851 if (!(style & (WS_MINIMIZE | WS_MAXIMIZE)))
853 if (data->hwnd != GetDesktopWindow()) /* don't force position of desktop */
855 size_hints->x = data->whole_rect.left;
856 size_hints->y = data->whole_rect.top;
857 size_hints->flags |= PPosition;
860 if (!is_window_resizable( data, style ))
862 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
863 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
864 size_hints->min_width = size_hints->max_width;
865 size_hints->min_height = size_hints->max_height;
866 size_hints->flags |= PMinSize | PMaxSize;
869 XSetWMNormalHints( display, data->whole_window, size_hints );
870 XFree( size_hints );
874 /***********************************************************************
875 * get_process_name
877 * get the name of the current process for setting class hints
879 static char *get_process_name(void)
881 static char *name;
883 if (!name)
885 WCHAR module[MAX_PATH];
886 DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
887 if (len && len < MAX_PATH)
889 char *ptr;
890 WCHAR *p, *appname = module;
892 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
893 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
894 len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
895 if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
897 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
898 name = ptr;
902 return name;
906 /***********************************************************************
907 * set_initial_wm_hints
909 * Set the window manager hints that don't change over the lifetime of a window.
911 static void set_initial_wm_hints( Display *display, struct x11drv_win_data *data )
913 long i;
914 Atom protocols[3];
915 Atom dndVersion = 4;
916 XClassHint *class_hints;
917 char *process_name = get_process_name();
919 wine_tsx11_lock();
921 /* wm protocols */
922 i = 0;
923 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
924 protocols[i++] = x11drv_atom(_NET_WM_PING);
925 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
926 XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
927 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
929 /* class hints */
930 if ((class_hints = XAllocClassHint()))
932 static char wine[] = "Wine";
934 class_hints->res_name = process_name;
935 class_hints->res_class = wine;
936 XSetClassHint( display, data->whole_window, class_hints );
937 XFree( class_hints );
940 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
941 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
942 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
943 i = getpid();
944 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
945 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
947 XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
948 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
950 data->wm_hints = XAllocWMHints();
951 wine_tsx11_unlock();
953 if (data->wm_hints)
955 HICON icon = (HICON)SendMessageW( data->hwnd, WM_GETICON, ICON_BIG, 0 );
956 if (!icon) icon = (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON );
957 data->wm_hints->flags = 0;
958 set_icon_hints( display, data, icon );
963 /***********************************************************************
964 * set_wm_hints
966 * Set the window manager hints for a newly-created window
968 static void set_wm_hints( Display *display, struct x11drv_win_data *data )
970 Window group_leader;
971 Atom window_type;
972 MwmHints mwm_hints;
973 DWORD style, ex_style;
974 HWND owner;
976 if (data->hwnd == GetDesktopWindow())
978 /* force some styles for the desktop to get the correct decorations */
979 style = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
980 ex_style = WS_EX_APPWINDOW;
981 owner = 0;
983 else
985 style = GetWindowLongW( data->hwnd, GWL_STYLE );
986 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
987 owner = get_window_owner( data->hwnd );
990 /* transient for hint */
991 if (owner)
993 Window owner_win = X11DRV_get_whole_window( owner );
994 wine_tsx11_lock();
995 XSetTransientForHint( display, data->whole_window, owner_win );
996 wine_tsx11_unlock();
997 group_leader = owner_win;
999 else group_leader = data->whole_window;
1001 wine_tsx11_lock();
1003 /* size hints */
1004 set_size_hints( display, data, style );
1006 /* set the WM_WINDOW_TYPE */
1007 if (style & WS_THICKFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
1008 else if (ex_style & WS_EX_APPWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
1009 else if (style & WS_DLGFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
1010 else if (ex_style & WS_EX_DLGMODALFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
1011 else if ((style & WS_POPUP) && owner) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
1012 #if 0 /* many window managers don't handle utility windows very well */
1013 else if (ex_style & WS_EX_TOOLWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
1014 #endif
1015 else window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
1017 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
1018 XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
1020 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
1021 mwm_hints.decorations = get_mwm_decorations( data, style, ex_style );
1022 mwm_hints.functions = MWM_FUNC_MOVE;
1023 if (is_window_resizable( data, style )) mwm_hints.functions |= MWM_FUNC_RESIZE;
1024 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
1025 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
1026 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
1028 XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
1029 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
1030 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
1032 /* wm hints */
1033 if (data->wm_hints)
1035 data->wm_hints->flags |= InputHint | StateHint | WindowGroupHint;
1036 data->wm_hints->input = !(style & WS_DISABLED);
1037 data->wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
1038 data->wm_hints->window_group = group_leader;
1039 XSetWMHints( display, data->whole_window, data->wm_hints );
1042 wine_tsx11_unlock();
1046 /***********************************************************************
1047 * update_net_wm_states
1049 void update_net_wm_states( Display *display, struct x11drv_win_data *data )
1051 static const unsigned int state_atoms[NB_NET_WM_STATES] =
1053 XATOM__NET_WM_STATE_FULLSCREEN,
1054 XATOM__NET_WM_STATE_ABOVE,
1055 XATOM__NET_WM_STATE_MAXIMIZED_VERT,
1056 XATOM__NET_WM_STATE_SKIP_PAGER,
1057 XATOM__NET_WM_STATE_SKIP_TASKBAR
1060 DWORD i, style, ex_style, new_state = 0;
1062 if (!data->managed) return;
1063 if (data->whole_window == root_window) return;
1065 style = GetWindowLongW( data->hwnd, GWL_STYLE );
1066 if (data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
1067 data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height)
1069 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION)
1070 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
1071 else if (!(style & WS_MINIMIZE))
1072 new_state |= (1 << NET_WM_STATE_FULLSCREEN);
1074 else if (style & WS_MAXIMIZE)
1075 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
1077 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
1078 if (ex_style & WS_EX_TOPMOST)
1079 new_state |= (1 << NET_WM_STATE_ABOVE);
1080 if (ex_style & WS_EX_TOOLWINDOW)
1081 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR) | (1 << NET_WM_STATE_SKIP_PAGER);
1082 if (!(ex_style & WS_EX_APPWINDOW) && get_window_owner( data->hwnd ))
1083 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR);
1085 if (!data->mapped) /* set the _NET_WM_STATE atom directly */
1087 Atom atoms[NB_NET_WM_STATES+1];
1088 DWORD count;
1090 for (i = count = 0; i < NB_NET_WM_STATES; i++)
1092 if (!(new_state & (1 << i))) continue;
1093 TRACE( "setting wm state %u for unmapped window %p/%lx\n",
1094 i, data->hwnd, data->whole_window );
1095 atoms[count++] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1096 if (state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT)
1097 atoms[count++] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
1099 wine_tsx11_lock();
1100 XChangeProperty( display, data->whole_window, x11drv_atom(_NET_WM_STATE), XA_ATOM,
1101 32, PropModeReplace, (unsigned char *)atoms, count );
1102 wine_tsx11_unlock();
1104 else /* ask the window manager to do it for us */
1106 XEvent xev;
1108 xev.xclient.type = ClientMessage;
1109 xev.xclient.window = data->whole_window;
1110 xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
1111 xev.xclient.serial = 0;
1112 xev.xclient.display = display;
1113 xev.xclient.send_event = True;
1114 xev.xclient.format = 32;
1115 xev.xclient.data.l[3] = 1;
1117 for (i = 0; i < NB_NET_WM_STATES; i++)
1119 if (!((data->net_wm_state ^ new_state) & (1 << i))) continue; /* unchanged */
1121 TRACE( "setting wm state %u for window %p/%lx to %u prev %u\n",
1122 i, data->hwnd, data->whole_window,
1123 (new_state & (1 << i)) != 0, (data->net_wm_state & (1 << i)) != 0 );
1125 xev.xclient.data.l[0] = (new_state & (1 << i)) ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1126 xev.xclient.data.l[1] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1127 xev.xclient.data.l[2] = ((state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT) ?
1128 x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ) : 0);
1129 wine_tsx11_lock();
1130 XSendEvent( display, root_window, False,
1131 SubstructureRedirectMask | SubstructureNotifyMask, &xev );
1132 wine_tsx11_unlock();
1135 data->net_wm_state = new_state;
1139 /***********************************************************************
1140 * set_xembed_flags
1142 static void set_xembed_flags( Display *display, struct x11drv_win_data *data, unsigned long flags )
1144 unsigned long info[2];
1146 info[0] = 0; /* protocol version */
1147 info[1] = flags;
1148 wine_tsx11_lock();
1149 XChangeProperty( display, data->whole_window, x11drv_atom(_XEMBED_INFO),
1150 x11drv_atom(_XEMBED_INFO), 32, PropModeReplace, (unsigned char*)info, 2 );
1151 wine_tsx11_unlock();
1155 /***********************************************************************
1156 * map_window
1158 static void map_window( Display *display, struct x11drv_win_data *data, DWORD new_style )
1160 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1162 remove_startup_notification( display, data->whole_window );
1164 wait_for_withdrawn_state( display, data, TRUE );
1166 if (!data->embedded)
1168 update_net_wm_states( display, data );
1169 sync_window_style( display, data );
1170 wine_tsx11_lock();
1171 XMapWindow( display, data->whole_window );
1172 wine_tsx11_unlock();
1174 else set_xembed_flags( display, data, XEMBED_MAPPED );
1176 data->mapped = TRUE;
1177 data->iconic = (new_style & WS_MINIMIZE) != 0;
1181 /***********************************************************************
1182 * unmap_window
1184 static void unmap_window( Display *display, struct x11drv_win_data *data )
1186 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1188 if (!data->embedded)
1190 wait_for_withdrawn_state( display, data, FALSE );
1191 wine_tsx11_lock();
1192 if (data->managed) XWithdrawWindow( display, data->whole_window, DefaultScreen(display) );
1193 else XUnmapWindow( display, data->whole_window );
1194 wine_tsx11_unlock();
1196 else set_xembed_flags( display, data, 0 );
1198 data->mapped = FALSE;
1199 data->net_wm_state = 0;
1203 /***********************************************************************
1204 * make_window_embedded
1206 void make_window_embedded( Display *display, struct x11drv_win_data *data )
1208 if (data->mapped)
1210 /* the window cannot be mapped before being embedded */
1211 unmap_window( display, data );
1212 data->embedded = TRUE;
1213 map_window( display, data, 0 );
1215 else
1217 data->embedded = TRUE;
1218 set_xembed_flags( display, data, 0 );
1223 /***********************************************************************
1224 * X11DRV_window_to_X_rect
1226 * Convert a rect from client to X window coordinates
1228 void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
1230 RECT rc;
1232 if (!data->managed) return;
1233 if (IsRectEmpty( rect )) return;
1235 get_x11_rect_offset( data, &rc );
1237 rect->left -= rc.left;
1238 rect->right -= rc.right;
1239 rect->top -= rc.top;
1240 rect->bottom -= rc.bottom;
1241 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1242 if (rect->left >= rect->right) rect->right = rect->left + 1;
1246 /***********************************************************************
1247 * X11DRV_X_to_window_rect
1249 * Opposite of X11DRV_window_to_X_rect
1251 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
1253 RECT rc;
1255 if (!data->managed) return;
1256 if (IsRectEmpty( rect )) return;
1258 get_x11_rect_offset( data, &rc );
1260 rect->left += rc.left;
1261 rect->right += rc.right;
1262 rect->top += rc.top;
1263 rect->bottom += rc.bottom;
1264 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1265 if (rect->left >= rect->right) rect->right = rect->left + 1;
1269 /***********************************************************************
1270 * sync_window_position
1272 * Synchronize the X window position with the Windows one
1274 static void sync_window_position( Display *display, struct x11drv_win_data *data,
1275 UINT swp_flags, const RECT *old_client_rect,
1276 const RECT *old_whole_rect )
1278 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
1279 XWindowChanges changes;
1280 unsigned int mask = 0;
1282 if (data->managed && data->iconic) return;
1284 /* resizing a managed maximized window is not allowed */
1285 if (!(style & WS_MAXIMIZE) || !data->managed)
1287 changes.width = data->whole_rect.right - data->whole_rect.left;
1288 changes.height = data->whole_rect.bottom - data->whole_rect.top;
1289 /* if window rect is empty force size to 1x1 */
1290 if (changes.width <= 0 || changes.height <= 0) changes.width = changes.height = 1;
1291 mask |= CWWidth | CWHeight;
1294 /* only the size is allowed to change for the desktop window */
1295 if (data->whole_window != root_window)
1297 changes.x = data->whole_rect.left - virtual_screen_rect.left;
1298 changes.y = data->whole_rect.top - virtual_screen_rect.top;
1299 mask |= CWX | CWY;
1302 if (!(swp_flags & SWP_NOZORDER) || (swp_flags & SWP_SHOWWINDOW))
1304 /* find window that this one must be after */
1305 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
1306 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
1307 prev = GetWindow( prev, GW_HWNDPREV );
1308 if (!prev) /* top child */
1310 changes.stack_mode = Above;
1311 mask |= CWStackMode;
1313 /* should use stack_mode Below but most window managers don't get it right */
1314 /* and Above with a sibling doesn't work so well either, so we ignore it */
1317 TRACE( "setting win %p/%lx pos %d,%d,%dx%d after %lx changes=%x\n",
1318 data->hwnd, data->whole_window, data->whole_rect.left, data->whole_rect.top,
1319 data->whole_rect.right - data->whole_rect.left,
1320 data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
1322 wine_tsx11_lock();
1323 set_size_hints( display, data, style );
1324 XReconfigureWMWindow( display, data->whole_window,
1325 DefaultScreen(display), mask, &changes );
1326 wine_tsx11_unlock();
1330 /***********************************************************************
1331 * sync_client_position
1333 * Synchronize the X client window position with the Windows one
1335 static void sync_client_position( Display *display, struct x11drv_win_data *data,
1336 UINT swp_flags, const RECT *old_client_rect,
1337 const RECT *old_whole_rect )
1339 int mask;
1340 XWindowChanges changes;
1341 RECT old = *old_client_rect;
1342 RECT new = data->client_rect;
1344 OffsetRect( &old, -old_whole_rect->left, -old_whole_rect->top );
1345 OffsetRect( &new, -data->whole_rect.left, -data->whole_rect.top );
1346 if (!(mask = get_window_changes( &changes, &old, &new ))) return;
1348 if (data->client_window)
1350 TRACE( "setting client win %lx pos %d,%d,%dx%d changes=%x\n",
1351 data->client_window, new.left, new.top,
1352 new.right - new.left, new.bottom - new.top, mask );
1353 wine_tsx11_lock();
1354 XConfigureWindow( display, data->client_window, mask, &changes );
1355 wine_tsx11_unlock();
1358 if (data->gl_drawable && (mask & (CWWidth|CWHeight))) sync_gl_drawable( data );
1362 /***********************************************************************
1363 * move_window_bits
1365 * Move the window bits when a window is moved.
1367 static void move_window_bits( struct x11drv_win_data *data, const RECT *old_rect, const RECT *new_rect,
1368 const RECT *old_client_rect )
1370 RECT src_rect = *old_rect;
1371 RECT dst_rect = *new_rect;
1372 HDC hdc_src, hdc_dst;
1373 INT code;
1374 HRGN rgn = 0;
1375 HWND parent = 0;
1377 if (!data->whole_window)
1379 OffsetRect( &dst_rect, -data->window_rect.left, -data->window_rect.top );
1380 parent = GetAncestor( data->hwnd, GA_PARENT );
1381 hdc_src = GetDCEx( parent, 0, DCX_CACHE );
1382 hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE | DCX_WINDOW );
1384 else
1386 OffsetRect( &dst_rect, -data->client_rect.left, -data->client_rect.top );
1387 /* make src rect relative to the old position of the window */
1388 OffsetRect( &src_rect, -old_client_rect->left, -old_client_rect->top );
1389 if (dst_rect.left == src_rect.left && dst_rect.top == src_rect.top) return;
1390 hdc_src = hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE );
1393 code = X11DRV_START_EXPOSURES;
1394 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
1396 TRACE( "copying bits for win %p/%lx/%lx %s -> %s\n",
1397 data->hwnd, data->whole_window, data->client_window,
1398 wine_dbgstr_rect(&src_rect), wine_dbgstr_rect(&dst_rect) );
1399 BitBlt( hdc_dst, dst_rect.left, dst_rect.top,
1400 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top,
1401 hdc_src, src_rect.left, src_rect.top, SRCCOPY );
1403 code = X11DRV_END_EXPOSURES;
1404 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, sizeof(rgn), (LPSTR)&rgn );
1406 ReleaseDC( data->hwnd, hdc_dst );
1407 if (hdc_src != hdc_dst) ReleaseDC( parent, hdc_src );
1409 if (rgn)
1411 if (!data->whole_window)
1413 /* map region to client rect since we are using DCX_WINDOW */
1414 OffsetRgn( rgn, data->window_rect.left - data->client_rect.left,
1415 data->window_rect.top - data->client_rect.top );
1416 RedrawWindow( data->hwnd, NULL, rgn,
1417 RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ALLCHILDREN );
1419 else RedrawWindow( data->hwnd, NULL, rgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1420 DeleteObject( rgn );
1425 /**********************************************************************
1426 * create_whole_window
1428 * Create the whole X window for a given window
1430 static Window create_whole_window( Display *display, struct x11drv_win_data *data )
1432 int cx, cy, mask;
1433 XSetWindowAttributes attr;
1434 WCHAR text[1024];
1435 COLORREF key;
1436 BYTE alpha;
1437 DWORD layered_flags;
1439 if (!(cx = data->window_rect.right - data->window_rect.left)) cx = 1;
1440 if (!(cy = data->window_rect.bottom - data->window_rect.top)) cy = 1;
1442 if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1444 TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1445 data->managed = TRUE;
1446 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1449 mask = get_window_attributes( display, data, &attr );
1451 wine_tsx11_lock();
1453 data->whole_rect = data->window_rect;
1454 data->whole_window = XCreateWindow( display, root_window,
1455 data->window_rect.left - virtual_screen_rect.left,
1456 data->window_rect.top - virtual_screen_rect.top,
1457 cx, cy, 0, screen_depth, InputOutput,
1458 visual, mask, &attr );
1460 if (data->whole_window) XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
1461 wine_tsx11_unlock();
1463 if (!data->whole_window) return 0;
1465 if (!create_client_window( display, data, NULL ))
1467 wine_tsx11_lock();
1468 XDeleteContext( display, data->whole_window, winContext );
1469 XDestroyWindow( display, data->whole_window );
1470 data->whole_window = 0;
1471 wine_tsx11_unlock();
1472 return 0;
1475 set_initial_wm_hints( display, data );
1476 set_wm_hints( display, data );
1478 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1480 /* set the window text */
1481 if (!InternalGetWindowText( data->hwnd, text, sizeof(text)/sizeof(WCHAR) )) text[0] = 0;
1482 sync_window_text( display, data->whole_window, text );
1484 /* set the window region */
1485 sync_window_region( display, data, (HRGN)1 );
1487 /* set the window opacity */
1488 if (!GetLayeredWindowAttributes( data->hwnd, &key, &alpha, &layered_flags )) layered_flags = 0;
1489 sync_window_opacity( display, data->whole_window, key, alpha, layered_flags );
1491 wine_tsx11_lock();
1492 XFlush( display ); /* make sure the window exists before we start painting to it */
1493 wine_tsx11_unlock();
1494 return data->whole_window;
1498 /**********************************************************************
1499 * destroy_whole_window
1501 * Destroy the whole X window for a given window.
1503 static void destroy_whole_window( Display *display, struct x11drv_win_data *data, BOOL already_destroyed )
1505 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1507 if (!data->whole_window) return;
1509 TRACE( "win %p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1510 if (thread_data->cursor_window == data->whole_window ||
1511 thread_data->cursor_window == data->client_window)
1512 thread_data->cursor_window = None;
1513 wine_tsx11_lock();
1514 XDeleteContext( display, data->whole_window, winContext );
1515 XDeleteContext( display, data->client_window, winContext );
1516 if (!already_destroyed) XDestroyWindow( display, data->whole_window );
1517 data->whole_window = data->client_window = 0;
1518 data->wm_state = WithdrawnState;
1519 data->net_wm_state = 0;
1520 data->mapped = FALSE;
1521 if (data->xic)
1523 XUnsetICFocus( data->xic );
1524 XDestroyIC( data->xic );
1525 data->xic = 0;
1527 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1528 XFlush( display );
1529 XFree( data->wm_hints );
1530 data->wm_hints = NULL;
1531 wine_tsx11_unlock();
1532 RemovePropA( data->hwnd, whole_window_prop );
1533 RemovePropA( data->hwnd, client_window_prop );
1537 /*****************************************************************
1538 * SetWindowText (X11DRV.@)
1540 void CDECL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1542 Window win;
1544 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(gdi_display))
1546 Display *display = thread_init_display();
1547 sync_window_text( display, win, text );
1552 /***********************************************************************
1553 * SetWindowStyle (X11DRV.@)
1555 * Update the X state of a window to reflect a style change
1557 void CDECL X11DRV_SetWindowStyle( HWND hwnd, INT offset, STYLESTRUCT *style )
1559 struct x11drv_win_data *data;
1560 DWORD changed;
1562 if (hwnd == GetDesktopWindow()) return;
1563 changed = style->styleNew ^ style->styleOld;
1565 if (offset == GWL_STYLE && (changed & WS_VISIBLE) && (style->styleNew & WS_VISIBLE))
1567 /* we don't unmap windows, that causes trouble with the window manager */
1568 if (!(data = X11DRV_get_win_data( hwnd )) &&
1569 !(data = X11DRV_create_win_data( hwnd ))) return;
1571 if (data->whole_window && is_window_rect_mapped( &data->window_rect ))
1573 Display *display = thread_display();
1574 set_wm_hints( display, data );
1575 if (!data->mapped) map_window( display, data, style->styleNew );
1579 if (offset == GWL_STYLE && (changed & WS_DISABLED))
1581 data = X11DRV_get_win_data( hwnd );
1582 if (data && data->wm_hints)
1584 wine_tsx11_lock();
1585 data->wm_hints->input = !(style->styleNew & WS_DISABLED);
1586 XSetWMHints( thread_display(), data->whole_window, data->wm_hints );
1587 wine_tsx11_unlock();
1591 if (offset == GWL_EXSTYLE && (changed & WS_EX_LAYERED))
1593 /* changing WS_EX_LAYERED resets attributes */
1594 if ((data = X11DRV_get_win_data( hwnd )) && data->whole_window)
1595 sync_window_opacity( thread_display(), data->whole_window, 0, 0, 0 );
1600 /***********************************************************************
1601 * DestroyWindow (X11DRV.@)
1603 void CDECL X11DRV_DestroyWindow( HWND hwnd )
1605 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1606 struct x11drv_win_data *data;
1608 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1610 if (data->pixmap)
1612 wine_tsx11_lock();
1613 destroy_glxpixmap(gdi_display, data->gl_drawable);
1614 XFreePixmap(gdi_display, data->pixmap);
1615 wine_tsx11_unlock();
1617 else if (data->gl_drawable)
1619 wine_tsx11_lock();
1620 XDestroyWindow(gdi_display, data->gl_drawable);
1621 wine_tsx11_unlock();
1624 destroy_whole_window( thread_data->display, data, FALSE );
1625 destroy_icon_window( thread_data->display, data );
1627 if (data->colormap)
1629 wine_tsx11_lock();
1630 XFreeColormap( thread_data->display, data->colormap );
1631 wine_tsx11_unlock();
1634 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1635 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
1636 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
1637 wine_tsx11_lock();
1638 XDeleteContext( thread_data->display, (XID)hwnd, win_data_context );
1639 wine_tsx11_unlock();
1640 HeapFree( GetProcessHeap(), 0, data );
1644 /***********************************************************************
1645 * X11DRV_DestroyNotify
1647 void X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1649 Display *display = event->xdestroywindow.display;
1650 struct x11drv_win_data *data;
1652 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1654 FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1655 destroy_whole_window( display, data, TRUE );
1659 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
1661 struct x11drv_win_data *data;
1663 if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
1665 data->hwnd = hwnd;
1666 wine_tsx11_lock();
1667 if (!winContext) winContext = XUniqueContext();
1668 if (!win_data_context) win_data_context = XUniqueContext();
1669 XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
1670 wine_tsx11_unlock();
1672 return data;
1676 /* initialize the desktop window id in the desktop manager process */
1677 static struct x11drv_win_data *create_desktop_win_data( Display *display, HWND hwnd )
1679 struct x11drv_win_data *data;
1681 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1682 data->whole_window = data->client_window = root_window;
1683 data->managed = TRUE;
1684 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1685 SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
1686 SetPropA( data->hwnd, client_window_prop, (HANDLE)root_window );
1687 set_initial_wm_hints( display, data );
1688 return data;
1691 /**********************************************************************
1692 * CreateDesktopWindow (X11DRV.@)
1694 BOOL CDECL X11DRV_CreateDesktopWindow( HWND hwnd )
1696 unsigned int width, height;
1698 /* retrieve the real size of the desktop */
1699 SERVER_START_REQ( get_window_rectangles )
1701 req->handle = wine_server_user_handle( hwnd );
1702 wine_server_call( req );
1703 width = reply->window.right - reply->window.left;
1704 height = reply->window.bottom - reply->window.top;
1706 SERVER_END_REQ;
1708 if (!width && !height) /* not initialized yet */
1710 SERVER_START_REQ( set_window_pos )
1712 req->handle = wine_server_user_handle( hwnd );
1713 req->previous = 0;
1714 req->flags = SWP_NOZORDER;
1715 req->window.left = virtual_screen_rect.left;
1716 req->window.top = virtual_screen_rect.top;
1717 req->window.right = virtual_screen_rect.right;
1718 req->window.bottom = virtual_screen_rect.bottom;
1719 req->client = req->window;
1720 wine_server_call( req );
1722 SERVER_END_REQ;
1724 else
1726 Window win = (Window)GetPropA( hwnd, whole_window_prop );
1727 if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1729 return TRUE;
1733 /**********************************************************************
1734 * CreateWindow (X11DRV.@)
1736 BOOL CDECL X11DRV_CreateWindow( HWND hwnd )
1738 if (hwnd == GetDesktopWindow() && root_window != DefaultRootWindow( gdi_display ))
1740 Display *display = thread_init_display();
1742 /* the desktop win data can't be created lazily */
1743 if (!create_desktop_win_data( display, hwnd )) return FALSE;
1745 return TRUE;
1749 /***********************************************************************
1750 * X11DRV_get_win_data
1752 * Return the X11 data structure associated with a window.
1754 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1756 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1757 char *data;
1759 if (!thread_data) return NULL;
1760 if (!hwnd) return NULL;
1761 if (XFindContext( thread_data->display, (XID)hwnd, win_data_context, &data )) data = NULL;
1762 return (struct x11drv_win_data *)data;
1766 /***********************************************************************
1767 * X11DRV_create_win_data
1769 * Create an X11 data window structure for an existing window.
1771 struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd )
1773 Display *display;
1774 struct x11drv_win_data *data;
1775 HWND parent;
1777 if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL; /* desktop */
1779 /* don't create win data for HWND_MESSAGE windows */
1780 if (parent != GetDesktopWindow() && !GetAncestor( parent, GA_PARENT )) return NULL;
1782 display = thread_init_display();
1783 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1785 GetWindowRect( hwnd, &data->window_rect );
1786 MapWindowPoints( 0, parent, (POINT *)&data->window_rect, 2 );
1787 data->whole_rect = data->window_rect;
1788 GetClientRect( hwnd, &data->client_rect );
1789 MapWindowPoints( hwnd, parent, (POINT *)&data->client_rect, 2 );
1791 if (parent == GetDesktopWindow())
1793 if (!create_whole_window( display, data ))
1795 HeapFree( GetProcessHeap(), 0, data );
1796 return NULL;
1798 TRACE( "win %p/%lx/%lx window %s whole %s client %s\n",
1799 hwnd, data->whole_window, data->client_window, wine_dbgstr_rect( &data->window_rect ),
1800 wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
1802 return data;
1806 /***********************************************************************
1807 * X11DRV_get_whole_window
1809 * Return the X window associated with the full area of a window
1811 Window X11DRV_get_whole_window( HWND hwnd )
1813 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1815 if (!data)
1817 if (hwnd == GetDesktopWindow()) return root_window;
1818 return (Window)GetPropA( hwnd, whole_window_prop );
1820 return data->whole_window;
1824 /***********************************************************************
1825 * X11DRV_get_client_window
1827 * Return the X window associated with the client area of a window
1829 Window X11DRV_get_client_window( HWND hwnd )
1831 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1833 if (!data)
1835 if (hwnd == GetDesktopWindow()) return root_window;
1836 return (Window)GetPropA( hwnd, client_window_prop );
1838 return data->client_window;
1842 /***********************************************************************
1843 * X11DRV_get_ic
1845 * Return the X input context associated with a window
1847 XIC X11DRV_get_ic( HWND hwnd )
1849 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1850 XIM xim;
1852 if (!data) return 0;
1853 if (data->xic) return data->xic;
1854 if (!(xim = x11drv_thread_data()->xim)) return 0;
1855 return X11DRV_CreateIC( xim, data );
1859 /***********************************************************************
1860 * X11DRV_GetDC (X11DRV.@)
1862 void CDECL X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
1863 const RECT *top_rect, DWORD flags )
1865 struct x11drv_escape_set_drawable escape;
1866 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1868 escape.code = X11DRV_SET_DRAWABLE;
1869 escape.mode = IncludeInferiors;
1870 escape.fbconfig_id = 0;
1871 escape.gl_drawable = 0;
1872 escape.pixmap = 0;
1873 escape.gl_copy = FALSE;
1875 if (top == hwnd && data && IsIconic( hwnd ) && data->icon_window)
1877 escape.drawable = data->icon_window;
1879 else if (top == hwnd)
1881 escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
1882 /* GL draws to the client area even for window DCs */
1883 escape.gl_drawable = data ? data->client_window : X11DRV_get_client_window( hwnd );
1884 if (flags & DCX_WINDOW)
1885 escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
1886 else
1887 escape.drawable = escape.gl_drawable;
1889 else
1891 escape.drawable = X11DRV_get_client_window( top );
1892 escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
1893 escape.gl_drawable = data ? data->gl_drawable : (Drawable)GetPropA( hwnd, gl_drawable_prop );
1894 escape.pixmap = data ? data->pixmap : (Pixmap)GetPropA( hwnd, pixmap_prop );
1895 escape.gl_copy = (escape.gl_drawable != 0);
1896 if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
1899 escape.dc_rect.left = win_rect->left - top_rect->left;
1900 escape.dc_rect.top = win_rect->top - top_rect->top;
1901 escape.dc_rect.right = win_rect->right - top_rect->left;
1902 escape.dc_rect.bottom = win_rect->bottom - top_rect->top;
1903 escape.drawable_rect.left = top_rect->left;
1904 escape.drawable_rect.top = top_rect->top;
1905 escape.drawable_rect.right = top_rect->right;
1906 escape.drawable_rect.bottom = top_rect->bottom;
1908 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1912 /***********************************************************************
1913 * X11DRV_ReleaseDC (X11DRV.@)
1915 void CDECL X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
1917 struct x11drv_escape_set_drawable escape;
1919 escape.code = X11DRV_SET_DRAWABLE;
1920 escape.drawable = root_window;
1921 escape.mode = IncludeInferiors;
1922 escape.drawable_rect = virtual_screen_rect;
1923 SetRect( &escape.dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
1924 virtual_screen_rect.bottom - virtual_screen_rect.top );
1925 escape.fbconfig_id = 0;
1926 escape.gl_drawable = 0;
1927 escape.pixmap = 0;
1928 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1932 /***********************************************************************
1933 * SetCapture (X11DRV.@)
1935 void CDECL X11DRV_SetCapture( HWND hwnd, UINT flags )
1937 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1939 if (!thread_data) return;
1940 if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
1942 if (hwnd)
1944 Window grab_win = X11DRV_get_client_window( GetAncestor( hwnd, GA_ROOT ) );
1946 if (!grab_win) return;
1947 wine_tsx11_lock();
1948 XFlush( gdi_display );
1949 XGrabPointer( thread_data->display, grab_win, False,
1950 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1951 GrabModeAsync, GrabModeAsync, None, None, CurrentTime );
1952 wine_tsx11_unlock();
1953 thread_data->grab_window = grab_win;
1955 else /* release capture */
1957 wine_tsx11_lock();
1958 XFlush( gdi_display );
1959 XUngrabPointer( thread_data->display, CurrentTime );
1960 wine_tsx11_unlock();
1961 thread_data->grab_window = None;
1966 /*****************************************************************
1967 * SetParent (X11DRV.@)
1969 void CDECL X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
1971 Display *display = thread_display();
1972 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1974 if (!data) return;
1975 if (parent == old_parent) return;
1977 if (parent != GetDesktopWindow()) /* a child window */
1979 if (old_parent == GetDesktopWindow())
1981 /* destroy the old X windows */
1982 destroy_whole_window( display, data, FALSE );
1983 destroy_icon_window( display, data );
1984 if (data->managed)
1986 data->managed = FALSE;
1987 RemovePropA( data->hwnd, managed_prop );
1991 else /* new top level window */
1993 /* FIXME: we ignore errors since we can't really recover anyway */
1994 create_whole_window( display, data );
1999 /*****************************************************************
2000 * SetFocus (X11DRV.@)
2002 * Set the X focus.
2004 void CDECL X11DRV_SetFocus( HWND hwnd )
2006 Display *display = thread_display();
2007 struct x11drv_win_data *data;
2008 XWindowChanges changes;
2010 if (!(hwnd = GetAncestor( hwnd, GA_ROOT ))) return;
2011 if (!(data = X11DRV_get_win_data( hwnd ))) return;
2012 if (data->managed || !data->whole_window) return;
2014 /* Set X focus and install colormap */
2015 wine_tsx11_lock();
2016 changes.stack_mode = Above;
2017 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
2018 if (root_window == DefaultRootWindow(display))
2020 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
2021 /* FIXME: this is not entirely correct */
2022 XSetInputFocus( display, data->whole_window, RevertToParent,
2023 /* CurrentTime */
2024 GetMessageTime() - EVENT_x11_time_to_win32_time(0));
2026 wine_tsx11_unlock();
2030 /***********************************************************************
2031 * WindowPosChanging (X11DRV.@)
2033 void CDECL X11DRV_WindowPosChanging( HWND hwnd, HWND insert_after, UINT swp_flags,
2034 const RECT *window_rect, const RECT *client_rect, RECT *visible_rect )
2036 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2037 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
2039 if (!data)
2041 /* create the win data if the window is being made visible */
2042 if (!(style & WS_VISIBLE) && !(swp_flags & SWP_SHOWWINDOW)) return;
2043 if (!(data = X11DRV_create_win_data( hwnd ))) return;
2046 /* check if we need to switch the window to managed */
2047 if (!data->managed && data->whole_window && is_window_managed( hwnd, swp_flags, window_rect ))
2049 TRACE( "making win %p/%lx managed\n", hwnd, data->whole_window );
2050 if (data->mapped) unmap_window( thread_display(), data );
2051 data->managed = TRUE;
2052 SetPropA( hwnd, managed_prop, (HANDLE)1 );
2055 *visible_rect = *window_rect;
2056 X11DRV_window_to_X_rect( data, visible_rect );
2060 /***********************************************************************
2061 * WindowPosChanged (X11DRV.@)
2063 void CDECL X11DRV_WindowPosChanged( HWND hwnd, HWND insert_after, UINT swp_flags,
2064 const RECT *rectWindow, const RECT *rectClient,
2065 const RECT *visible_rect, const RECT *valid_rects )
2067 struct x11drv_thread_data *thread_data;
2068 Display *display;
2069 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2070 DWORD new_style = GetWindowLongW( hwnd, GWL_STYLE );
2071 RECT old_whole_rect, old_client_rect;
2072 int event_type;
2074 if (!data) return;
2076 thread_data = x11drv_thread_data();
2077 display = thread_data->display;
2079 old_whole_rect = data->whole_rect;
2080 old_client_rect = data->client_rect;
2081 data->window_rect = *rectWindow;
2082 data->whole_rect = *visible_rect;
2083 data->client_rect = *rectClient;
2085 TRACE( "win %p window %s client %s style %08x flags %08x\n",
2086 hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style, swp_flags );
2088 if (!IsRectEmpty( &valid_rects[0] ))
2090 int x_offset = old_whole_rect.left - data->whole_rect.left;
2091 int y_offset = old_whole_rect.top - data->whole_rect.top;
2093 /* if all that happened is that the whole window moved, copy everything */
2094 if (!(swp_flags & SWP_FRAMECHANGED) &&
2095 old_whole_rect.right - data->whole_rect.right == x_offset &&
2096 old_whole_rect.bottom - data->whole_rect.bottom == y_offset &&
2097 old_client_rect.left - data->client_rect.left == x_offset &&
2098 old_client_rect.right - data->client_rect.right == x_offset &&
2099 old_client_rect.top - data->client_rect.top == y_offset &&
2100 old_client_rect.bottom - data->client_rect.bottom == y_offset &&
2101 !memcmp( &valid_rects[0], &data->client_rect, sizeof(RECT) ))
2103 /* if we have an X window the bits will be moved by the X server */
2104 if (!data->whole_window)
2105 move_window_bits( data, &old_whole_rect, &data->whole_rect, &old_client_rect );
2107 else
2108 move_window_bits( data, &valid_rects[1], &valid_rects[0], &old_client_rect );
2111 wine_tsx11_lock();
2112 XFlush( gdi_display ); /* make sure painting is done before we move the window */
2113 wine_tsx11_unlock();
2115 sync_client_position( display, data, swp_flags, &old_client_rect, &old_whole_rect );
2117 if (!data->whole_window) return;
2119 /* check if we are currently processing an event relevant to this window */
2120 event_type = 0;
2121 if (thread_data->current_event && thread_data->current_event->xany.window == data->whole_window)
2122 event_type = thread_data->current_event->type;
2124 if (event_type != ConfigureNotify && event_type != PropertyNotify)
2125 event_type = 0; /* ignore other events */
2127 if (data->mapped)
2129 if (((swp_flags & SWP_HIDEWINDOW) && !(new_style & WS_VISIBLE)) ||
2130 (!event_type && !is_window_rect_mapped( rectWindow )))
2131 unmap_window( display, data );
2134 /* don't change position if we are about to minimize or maximize a managed window */
2135 if (!event_type &&
2136 !(data->managed && (swp_flags & SWP_STATECHANGED) && (new_style & (WS_MINIMIZE|WS_MAXIMIZE))))
2137 sync_window_position( display, data, swp_flags, &old_client_rect, &old_whole_rect );
2139 if ((new_style & WS_VISIBLE) &&
2140 ((new_style & WS_MINIMIZE) || is_window_rect_mapped( rectWindow )))
2142 if (!data->mapped || (swp_flags & (SWP_FRAMECHANGED|SWP_STATECHANGED)))
2143 set_wm_hints( display, data );
2145 if (!data->mapped)
2147 map_window( display, data, new_style );
2149 else if ((swp_flags & SWP_STATECHANGED) && (!data->iconic != !(new_style & WS_MINIMIZE)))
2151 data->iconic = (new_style & WS_MINIMIZE) != 0;
2152 TRACE( "changing win %p iconic state to %u\n", data->hwnd, data->iconic );
2153 wine_tsx11_lock();
2154 if (data->iconic)
2155 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
2156 else if (is_window_rect_mapped( rectWindow ))
2157 XMapWindow( display, data->whole_window );
2158 wine_tsx11_unlock();
2159 update_net_wm_states( display, data );
2161 else if (!event_type)
2163 update_net_wm_states( display, data );
2167 wine_tsx11_lock();
2168 XFlush( display ); /* make sure changes are done before we start painting again */
2169 wine_tsx11_unlock();
2173 /***********************************************************************
2174 * ShowWindow (X11DRV.@)
2176 UINT CDECL X11DRV_ShowWindow( HWND hwnd, INT cmd, RECT *rect, UINT swp )
2178 int x, y;
2179 unsigned int width, height, border, depth;
2180 Window root, top;
2181 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
2182 struct x11drv_thread_data *thread_data = x11drv_thread_data();
2183 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2185 if (!data || !data->whole_window || !data->managed || !data->mapped || data->iconic) return swp;
2186 if (style & WS_MINIMIZE) return swp;
2187 if (IsRectEmpty( rect )) return swp;
2189 /* only fetch the new rectangle if the ShowWindow was a result of a window manager event */
2191 if (!thread_data->current_event || thread_data->current_event->xany.window != data->whole_window)
2192 return swp;
2194 if (thread_data->current_event->type != ConfigureNotify &&
2195 thread_data->current_event->type != PropertyNotify)
2196 return swp;
2198 TRACE( "win %p/%lx cmd %d at %s flags %08x\n",
2199 hwnd, data->whole_window, cmd, wine_dbgstr_rect(rect), swp );
2201 wine_tsx11_lock();
2202 XGetGeometry( thread_data->display, data->whole_window,
2203 &root, &x, &y, &width, &height, &border, &depth );
2204 XTranslateCoordinates( thread_data->display, data->whole_window, root, 0, 0, &x, &y, &top );
2205 wine_tsx11_unlock();
2206 rect->left = x;
2207 rect->top = y;
2208 rect->right = x + width;
2209 rect->bottom = y + height;
2210 OffsetRect( rect, virtual_screen_rect.left, virtual_screen_rect.top );
2211 X11DRV_X_to_window_rect( data, rect );
2212 return swp & ~(SWP_NOMOVE | SWP_NOCLIENTMOVE | SWP_NOSIZE | SWP_NOCLIENTSIZE);
2216 /**********************************************************************
2217 * SetWindowIcon (X11DRV.@)
2219 * hIcon or hIconSm has changed (or is being initialised for the
2220 * first time). Complete the X11 driver-specific initialisation
2221 * and set the window hints.
2223 * This is not entirely correct, may need to create
2224 * an icon window and set the pixmap as a background
2226 void CDECL X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
2228 Display *display = thread_display();
2229 struct x11drv_win_data *data;
2231 if (type != ICON_BIG) return; /* nothing to do here */
2233 if (!(data = X11DRV_get_win_data( hwnd ))) return;
2234 if (!data->whole_window) return;
2235 if (!data->managed) return;
2237 if (data->wm_hints)
2239 set_icon_hints( display, data, icon );
2240 wine_tsx11_lock();
2241 XSetWMHints( display, data->whole_window, data->wm_hints );
2242 wine_tsx11_unlock();
2247 /***********************************************************************
2248 * SetWindowRgn (X11DRV.@)
2250 * Assign specified region to window (for non-rectangular windows)
2252 int CDECL X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
2254 struct x11drv_win_data *data;
2256 if ((data = X11DRV_get_win_data( hwnd )))
2258 sync_window_region( thread_display(), data, hrgn );
2260 else if (X11DRV_get_whole_window( hwnd ))
2262 SendMessageW( hwnd, WM_X11DRV_SET_WIN_REGION, 0, 0 );
2264 return TRUE;
2268 /***********************************************************************
2269 * SetLayeredWindowAttributes (X11DRV.@)
2271 * Set transparency attributes for a layered window.
2273 void CDECL X11DRV_SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
2275 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2277 if (data)
2279 if (data->whole_window)
2280 sync_window_opacity( thread_display(), data->whole_window, key, alpha, flags );
2282 else
2284 Window win = X11DRV_get_whole_window( hwnd );
2285 if (win) sync_window_opacity( gdi_display, win, key, alpha, flags );
2290 /**********************************************************************
2291 * X11DRV_WindowMessage (X11DRV.@)
2293 LRESULT CDECL X11DRV_WindowMessage( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
2295 struct x11drv_win_data *data;
2297 switch(msg)
2299 case WM_X11DRV_ACQUIRE_SELECTION:
2300 return X11DRV_AcquireClipboard( hwnd );
2301 case WM_X11DRV_DELETE_WINDOW:
2302 return SendMessageW( hwnd, WM_SYSCOMMAND, SC_CLOSE, 0 );
2303 case WM_X11DRV_SET_WIN_FORMAT:
2304 return set_win_format( hwnd, (XID)wp );
2305 case WM_X11DRV_SET_WIN_REGION:
2306 if ((data = X11DRV_get_win_data( hwnd ))) sync_window_region( thread_display(), data, (HRGN)1 );
2307 return 0;
2308 case WM_X11DRV_RESIZE_DESKTOP:
2309 X11DRV_resize_desktop( LOWORD(lp), HIWORD(lp) );
2310 return 0;
2311 default:
2312 FIXME( "got window msg %x hwnd %p wp %lx lp %lx\n", msg, hwnd, wp, lp );
2313 return 0;
2318 /***********************************************************************
2319 * is_netwm_supported
2321 static BOOL is_netwm_supported( Display *display, Atom atom )
2323 static Atom *net_supported;
2324 static int net_supported_count = -1;
2325 int i;
2327 wine_tsx11_lock();
2328 if (net_supported_count == -1)
2330 Atom type;
2331 int format;
2332 unsigned long count, remaining;
2334 if (!XGetWindowProperty( display, DefaultRootWindow(display), x11drv_atom(_NET_SUPPORTED), 0,
2335 ~0UL, False, XA_ATOM, &type, &format, &count,
2336 &remaining, (unsigned char **)&net_supported ))
2337 net_supported_count = get_property_size( format, count ) / sizeof(Atom);
2338 else
2339 net_supported_count = 0;
2341 wine_tsx11_unlock();
2343 for (i = 0; i < net_supported_count; i++)
2344 if (net_supported[i] == atom) return TRUE;
2345 return FALSE;
2349 /***********************************************************************
2350 * SysCommand (X11DRV.@)
2352 * Perform WM_SYSCOMMAND handling.
2354 LRESULT CDECL X11DRV_SysCommand( HWND hwnd, WPARAM wparam, LPARAM lparam )
2356 WPARAM hittest = wparam & 0x0f;
2357 DWORD dwPoint;
2358 int x, y, dir;
2359 XEvent xev;
2360 Display *display = thread_display();
2361 struct x11drv_win_data *data;
2363 if (!(data = X11DRV_get_win_data( hwnd ))) return -1;
2364 if (!data->whole_window || !data->managed || !data->mapped) return -1;
2366 switch (wparam & 0xfff0)
2368 case SC_MOVE:
2369 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
2370 else dir = _NET_WM_MOVERESIZE_MOVE;
2371 break;
2372 case SC_SIZE:
2373 /* windows without WS_THICKFRAME are not resizable through the window manager */
2374 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_THICKFRAME)) return -1;
2376 switch (hittest)
2378 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
2379 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
2380 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
2381 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
2382 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
2383 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
2384 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
2385 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
2386 default: dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD; break;
2388 break;
2390 case SC_KEYMENU:
2391 /* prevent a simple ALT press+release from activating the system menu,
2392 * as that can get confusing on managed windows */
2393 if ((WCHAR)lparam) return -1; /* got an explicit char */
2394 if (GetMenu( hwnd )) return -1; /* window has a real menu */
2395 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) return -1; /* no system menu */
2396 TRACE( "ignoring SC_KEYMENU wp %lx lp %lx\n", wparam, lparam );
2397 return 0;
2399 default:
2400 return -1;
2403 if (IsZoomed(hwnd)) return -1;
2405 if (!is_netwm_supported( display, x11drv_atom(_NET_WM_MOVERESIZE) ))
2407 TRACE( "_NET_WM_MOVERESIZE not supported\n" );
2408 return -1;
2411 dwPoint = GetMessagePos();
2412 x = (short)LOWORD(dwPoint);
2413 y = (short)HIWORD(dwPoint);
2415 TRACE("hwnd %p, x %d, y %d, dir %d\n", hwnd, x, y, dir);
2417 xev.xclient.type = ClientMessage;
2418 xev.xclient.window = X11DRV_get_whole_window(hwnd);
2419 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
2420 xev.xclient.serial = 0;
2421 xev.xclient.display = display;
2422 xev.xclient.send_event = True;
2423 xev.xclient.format = 32;
2424 xev.xclient.data.l[0] = x - virtual_screen_rect.left; /* x coord */
2425 xev.xclient.data.l[1] = y - virtual_screen_rect.top; /* y coord */
2426 xev.xclient.data.l[2] = dir; /* direction */
2427 xev.xclient.data.l[3] = 1; /* button */
2428 xev.xclient.data.l[4] = 0; /* unused */
2430 /* need to ungrab the pointer that may have been automatically grabbed
2431 * with a ButtonPress event */
2432 wine_tsx11_lock();
2433 XUngrabPointer( display, CurrentTime );
2434 XSendEvent(display, root_window, False, SubstructureNotifyMask, &xev);
2435 wine_tsx11_unlock();
2436 return 0;