winex11.drv: Declare some functions static.
[wine.git] / dlls / winex11.drv / window.c
blob8eccfe1b73675f39729f5b62d45d626be2c657a4
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 |
311 KeymapStateMask | StructureNotifyMask);
312 if (data->managed) attr->event_mask |= PropertyChangeMask;
314 return (CWOverrideRedirect | CWSaveUnder | CWColormap | CWCursor |
315 CWEventMask | CWBitGravity | CWBackingStore);
319 /***********************************************************************
320 * create_client_window
322 static Window create_client_window( Display *display, struct x11drv_win_data *data, XVisualInfo *vis )
324 int cx, cy, mask;
325 XSetWindowAttributes attr;
326 Window client;
327 Visual *client_visual = vis ? vis->visual : visual;
329 attr.bit_gravity = NorthWestGravity;
330 attr.win_gravity = NorthWestGravity;
331 attr.backing_store = NotUseful;
332 attr.event_mask = (ExposureMask | PointerMotionMask |
333 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
334 mask = CWEventMask | CWBitGravity | CWWinGravity | CWBackingStore;
336 if ((cx = data->client_rect.right - data->client_rect.left) <= 0) cx = 1;
337 if ((cy = data->client_rect.bottom - data->client_rect.top) <= 0) cy = 1;
339 wine_tsx11_lock();
341 if (vis)
343 attr.colormap = XCreateColormap( display, root_window, vis->visual,
344 (vis->class == PseudoColor || vis->class == GrayScale ||
345 vis->class == DirectColor) ? AllocAll : AllocNone );
346 mask |= CWColormap;
349 client = XCreateWindow( display, data->whole_window,
350 data->client_rect.left - data->whole_rect.left,
351 data->client_rect.top - data->whole_rect.top,
352 cx, cy, 0, screen_depth, InputOutput,
353 client_visual, mask, &attr );
354 if (!client)
356 wine_tsx11_unlock();
357 return 0;
360 if (data->client_window)
362 struct x11drv_thread_data *thread_data = x11drv_thread_data();
363 if (thread_data->cursor_window == data->client_window) thread_data->cursor_window = None;
364 XDeleteContext( display, data->client_window, winContext );
365 XDestroyWindow( display, data->client_window );
367 data->client_window = client;
368 data->visualid = XVisualIDFromVisual( client_visual );
370 if (data->colormap) XFreeColormap( display, data->colormap );
371 data->colormap = vis ? attr.colormap : 0;
373 XMapWindow( display, data->client_window );
374 XSaveContext( display, data->client_window, winContext, (char *)data->hwnd );
375 wine_tsx11_unlock();
377 SetPropA( data->hwnd, client_window_prop, (HANDLE)data->client_window );
378 return data->client_window;
382 /***********************************************************************
383 * sync_window_style
385 * Change the X window attributes when the window style has changed.
387 static void sync_window_style( Display *display, struct x11drv_win_data *data )
389 if (data->whole_window != root_window)
391 XSetWindowAttributes attr;
392 int mask = get_window_attributes( display, data, &attr );
394 wine_tsx11_lock();
395 XChangeWindowAttributes( display, data->whole_window, mask, &attr );
396 wine_tsx11_unlock();
401 /***********************************************************************
402 * sync_window_region
404 * Update the X11 window region.
406 static void sync_window_region( Display *display, struct x11drv_win_data *data, HRGN win_region )
408 #ifdef HAVE_LIBXSHAPE
409 HRGN hrgn = win_region;
411 if (!data->whole_window) return;
412 data->shaped = FALSE;
414 if (hrgn == (HRGN)1) /* hack: win_region == 1 means retrieve region from server */
416 if (!(hrgn = CreateRectRgn( 0, 0, 0, 0 ))) return;
417 if (GetWindowRgn( data->hwnd, hrgn ) == ERROR)
419 DeleteObject( hrgn );
420 hrgn = 0;
424 if (!hrgn)
426 wine_tsx11_lock();
427 XShapeCombineMask( display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
428 wine_tsx11_unlock();
430 else
432 RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
433 if (pRegionData)
435 wine_tsx11_lock();
436 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
437 data->window_rect.left - data->whole_rect.left,
438 data->window_rect.top - data->whole_rect.top,
439 (XRectangle *)pRegionData->Buffer,
440 pRegionData->rdh.nCount, ShapeSet, YXBanded );
441 wine_tsx11_unlock();
442 HeapFree(GetProcessHeap(), 0, pRegionData);
443 data->shaped = TRUE;
446 if (hrgn && hrgn != win_region) DeleteObject( hrgn );
447 #endif /* HAVE_LIBXSHAPE */
451 /***********************************************************************
452 * sync_window_opacity
454 static void sync_window_opacity( Display *display, Window win,
455 COLORREF key, BYTE alpha, DWORD flags )
457 unsigned long opacity = 0xffffffff;
459 if (flags & LWA_ALPHA) opacity = (0xffffffff / 0xff) * alpha;
461 if (flags & LWA_COLORKEY) FIXME("LWA_COLORKEY not supported\n");
463 wine_tsx11_lock();
464 if (opacity == 0xffffffff)
465 XDeleteProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY) );
466 else
467 XChangeProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY),
468 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&opacity, 1 );
469 wine_tsx11_unlock();
473 /***********************************************************************
474 * sync_window_text
476 static void sync_window_text( Display *display, Window win, const WCHAR *text )
478 UINT count;
479 char *buffer, *utf8_buffer;
480 XTextProperty prop;
482 /* allocate new buffer for window text */
483 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
484 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count ))) return;
485 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
487 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
488 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
490 HeapFree( GetProcessHeap(), 0, buffer );
491 return;
493 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
495 wine_tsx11_lock();
496 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
498 XSetWMName( display, win, &prop );
499 XSetWMIconName( display, win, &prop );
500 XFree( prop.value );
503 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
504 according to the standard
505 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
507 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
508 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
509 wine_tsx11_unlock();
511 HeapFree( GetProcessHeap(), 0, utf8_buffer );
512 HeapFree( GetProcessHeap(), 0, buffer );
516 /***********************************************************************
517 * set_win_format
519 static BOOL set_win_format( HWND hwnd, XID fbconfig_id )
521 struct x11drv_win_data *data;
522 XVisualInfo *vis;
523 int w, h;
525 if (!(data = X11DRV_get_win_data(hwnd)) &&
526 !(data = X11DRV_create_win_data(hwnd))) return FALSE;
528 if (!(vis = visual_from_fbconfig_id(fbconfig_id))) return FALSE;
530 if (data->whole_window)
532 Display *display = thread_display();
533 Window client = data->client_window;
535 if (vis->visualid != data->visualid)
537 client = create_client_window( display, data, vis );
538 TRACE( "re-created client window %lx for %p fbconfig %lx\n", client, data->hwnd, fbconfig_id );
540 wine_tsx11_lock();
541 XFree(vis);
542 XFlush( display );
543 wine_tsx11_unlock();
544 if (client) goto done;
545 return FALSE;
548 w = data->client_rect.right - data->client_rect.left;
549 h = data->client_rect.bottom - data->client_rect.top;
551 if(w <= 0) w = 1;
552 if(h <= 0) h = 1;
554 #ifdef SONAME_LIBXCOMPOSITE
555 if(usexcomposite)
557 XSetWindowAttributes attrib;
558 static Window dummy_parent;
560 wine_tsx11_lock();
561 attrib.override_redirect = True;
562 if (!dummy_parent)
564 dummy_parent = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, screen_depth,
565 InputOutput, visual, CWOverrideRedirect, &attrib );
566 XMapWindow( gdi_display, dummy_parent );
568 data->colormap = XCreateColormap(gdi_display, dummy_parent, vis->visual,
569 (vis->class == PseudoColor ||
570 vis->class == GrayScale ||
571 vis->class == DirectColor) ?
572 AllocAll : AllocNone);
573 attrib.colormap = data->colormap;
574 XInstallColormap(gdi_display, attrib.colormap);
576 if(data->gl_drawable) XDestroyWindow(gdi_display, data->gl_drawable);
577 data->gl_drawable = XCreateWindow(gdi_display, dummy_parent, -w, 0, w, h, 0,
578 vis->depth, InputOutput, vis->visual,
579 CWColormap | CWOverrideRedirect,
580 &attrib);
581 if(data->gl_drawable)
583 pXCompositeRedirectWindow(gdi_display, data->gl_drawable,
584 CompositeRedirectManual);
585 XMapWindow(gdi_display, data->gl_drawable);
587 XFree(vis);
588 XFlush( gdi_display );
589 wine_tsx11_unlock();
591 else
592 #endif
594 WARN("XComposite is not available, using GLXPixmap hack\n");
596 wine_tsx11_lock();
598 if(data->pixmap) XFreePixmap(gdi_display, data->pixmap);
599 data->pixmap = XCreatePixmap(gdi_display, root_window, w, h, vis->depth);
600 if(!data->pixmap)
602 XFree(vis);
603 wine_tsx11_unlock();
604 return FALSE;
607 if(data->gl_drawable) destroy_glxpixmap(gdi_display, data->gl_drawable);
608 data->gl_drawable = create_glxpixmap(gdi_display, vis, data->pixmap);
609 if(!data->gl_drawable)
611 XFreePixmap(gdi_display, data->pixmap);
612 data->pixmap = 0;
614 XFree(vis);
615 XFlush( gdi_display );
616 wine_tsx11_unlock();
617 if (data->pixmap) SetPropA(hwnd, pixmap_prop, (HANDLE)data->pixmap);
620 if (!data->gl_drawable) return FALSE;
622 TRACE("Created GL drawable 0x%lx, using FBConfigID 0x%lx\n",
623 data->gl_drawable, fbconfig_id);
624 SetPropA(hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
626 done:
627 data->fbconfig_id = fbconfig_id;
628 SetPropA(hwnd, fbconfig_id_prop, (HANDLE)data->fbconfig_id);
629 /* force DCE invalidation */
630 SetWindowPos( hwnd, 0, 0, 0, 0, 0,
631 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE |
632 SWP_NOREDRAW | SWP_DEFERERASE | SWP_NOSENDCHANGING | SWP_STATECHANGED);
633 return TRUE;
636 /***********************************************************************
637 * sync_gl_drawable
639 static void sync_gl_drawable(struct x11drv_win_data *data)
641 int w = data->client_rect.right - data->client_rect.left;
642 int h = data->client_rect.bottom - data->client_rect.top;
643 XVisualInfo *vis;
644 Drawable glxp;
645 Pixmap pix;
647 if (w <= 0) w = 1;
648 if (h <= 0) h = 1;
650 TRACE("Resizing GL drawable 0x%lx to %dx%d\n", data->gl_drawable, w, h);
651 #ifdef SONAME_LIBXCOMPOSITE
652 if(usexcomposite)
654 wine_tsx11_lock();
655 XMoveResizeWindow(gdi_display, data->gl_drawable, -w, 0, w, h);
656 wine_tsx11_unlock();
657 return;
659 #endif
661 if (!(vis = visual_from_fbconfig_id(data->fbconfig_id))) return;
663 wine_tsx11_lock();
664 pix = XCreatePixmap(gdi_display, root_window, w, h, vis->depth);
665 if(!pix)
667 ERR("Failed to create pixmap for offscreen rendering\n");
668 XFree(vis);
669 wine_tsx11_unlock();
670 return;
673 glxp = create_glxpixmap(gdi_display, vis, pix);
674 if(!glxp)
676 ERR("Failed to create drawable for offscreen rendering\n");
677 XFreePixmap(gdi_display, pix);
678 XFree(vis);
679 wine_tsx11_unlock();
680 return;
683 XFree(vis);
685 mark_drawable_dirty(data->gl_drawable, glxp);
687 XFreePixmap(gdi_display, data->pixmap);
688 destroy_glxpixmap(gdi_display, data->gl_drawable);
689 TRACE( "Recreated GL drawable %lx to replace %lx\n", glxp, data->gl_drawable );
691 data->pixmap = pix;
692 data->gl_drawable = glxp;
694 XFlush( gdi_display );
695 wine_tsx11_unlock();
697 SetPropA(data->hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
698 SetPropA(data->hwnd, pixmap_prop, (HANDLE)data->pixmap);
702 /***********************************************************************
703 * get_window_changes
705 * fill the window changes structure
707 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
709 int mask = 0;
711 if (old->right - old->left != new->right - new->left )
713 if ((changes->width = new->right - new->left) <= 0) changes->width = 1;
714 mask |= CWWidth;
716 if (old->bottom - old->top != new->bottom - new->top)
718 if ((changes->height = new->bottom - new->top) <= 0) changes->height = 1;
719 mask |= CWHeight;
721 if (old->left != new->left)
723 changes->x = new->left;
724 mask |= CWX;
726 if (old->top != new->top)
728 changes->y = new->top;
729 mask |= CWY;
731 return mask;
735 /***********************************************************************
736 * create_icon_window
738 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
740 XSetWindowAttributes attr;
742 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
743 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
744 attr.bit_gravity = NorthWestGravity;
745 attr.backing_store = NotUseful/*WhenMapped*/;
746 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
748 wine_tsx11_lock();
749 data->icon_window = XCreateWindow( display, root_window, 0, 0,
750 GetSystemMetrics( SM_CXICON ),
751 GetSystemMetrics( SM_CYICON ),
752 0, screen_depth,
753 InputOutput, visual,
754 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
755 XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
756 XFlush( display ); /* make sure the window exists before we start painting to it */
757 wine_tsx11_unlock();
759 TRACE( "created %lx\n", data->icon_window );
760 SetPropA( data->hwnd, icon_window_prop, (HANDLE)data->icon_window );
761 return data->icon_window;
766 /***********************************************************************
767 * destroy_icon_window
769 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
771 if (!data->icon_window) return;
772 if (x11drv_thread_data()->cursor_window == data->icon_window)
773 x11drv_thread_data()->cursor_window = None;
774 wine_tsx11_lock();
775 XDeleteContext( display, data->icon_window, winContext );
776 XDestroyWindow( display, data->icon_window );
777 data->icon_window = 0;
778 wine_tsx11_unlock();
779 RemovePropA( data->hwnd, icon_window_prop );
783 /***********************************************************************
784 * set_icon_hints
786 * Set the icon wm hints
788 static void set_icon_hints( Display *display, struct x11drv_win_data *data, HICON hIcon )
790 XWMHints *hints = data->wm_hints;
792 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
793 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
794 data->hWMIconBitmap = 0;
795 data->hWMIconMask = 0;
797 if (!hIcon)
799 if (!data->icon_window) create_icon_window( display, data );
800 hints->icon_window = data->icon_window;
801 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
803 else
805 HBITMAP hbmOrig;
806 RECT rcMask;
807 BITMAP bmMask;
808 ICONINFO ii;
809 HDC hDC;
811 GetIconInfo(hIcon, &ii);
813 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
814 rcMask.top = 0;
815 rcMask.left = 0;
816 rcMask.right = bmMask.bmWidth;
817 rcMask.bottom = bmMask.bmHeight;
819 hDC = CreateCompatibleDC(0);
820 hbmOrig = SelectObject(hDC, ii.hbmMask);
821 InvertRect(hDC, &rcMask);
822 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
823 SelectObject(hDC, hbmOrig);
824 DeleteDC(hDC);
826 data->hWMIconBitmap = ii.hbmColor;
827 data->hWMIconMask = ii.hbmMask;
829 hints->icon_pixmap = X11DRV_get_pixmap(data->hWMIconBitmap);
830 hints->icon_mask = X11DRV_get_pixmap(data->hWMIconMask);
831 destroy_icon_window( display, data );
832 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
837 /***********************************************************************
838 * set_size_hints
840 * set the window size hints
842 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
844 XSizeHints* size_hints;
846 if (!(size_hints = XAllocSizeHints())) return;
848 size_hints->win_gravity = StaticGravity;
849 size_hints->flags |= PWinGravity;
851 /* don't update size hints if window is not in normal state */
852 if (!(style & (WS_MINIMIZE | WS_MAXIMIZE)))
854 if (data->hwnd != GetDesktopWindow()) /* don't force position of desktop */
856 size_hints->x = data->whole_rect.left;
857 size_hints->y = data->whole_rect.top;
858 size_hints->flags |= PPosition;
861 if (!is_window_resizable( data, style ))
863 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
864 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
865 size_hints->min_width = size_hints->max_width;
866 size_hints->min_height = size_hints->max_height;
867 size_hints->flags |= PMinSize | PMaxSize;
870 XSetWMNormalHints( display, data->whole_window, size_hints );
871 XFree( size_hints );
875 /***********************************************************************
876 * get_process_name
878 * get the name of the current process for setting class hints
880 static char *get_process_name(void)
882 static char *name;
884 if (!name)
886 WCHAR module[MAX_PATH];
887 DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
888 if (len && len < MAX_PATH)
890 char *ptr;
891 WCHAR *p, *appname = module;
893 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
894 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
895 len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
896 if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
898 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
899 name = ptr;
903 return name;
907 /***********************************************************************
908 * set_initial_wm_hints
910 * Set the window manager hints that don't change over the lifetime of a window.
912 static void set_initial_wm_hints( Display *display, struct x11drv_win_data *data )
914 long i;
915 Atom protocols[3];
916 Atom dndVersion = 4;
917 XClassHint *class_hints;
918 char *process_name = get_process_name();
920 wine_tsx11_lock();
922 /* wm protocols */
923 i = 0;
924 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
925 protocols[i++] = x11drv_atom(_NET_WM_PING);
926 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
927 XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
928 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
930 /* class hints */
931 if ((class_hints = XAllocClassHint()))
933 static char wine[] = "Wine";
935 class_hints->res_name = process_name;
936 class_hints->res_class = wine;
937 XSetClassHint( display, data->whole_window, class_hints );
938 XFree( class_hints );
941 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
942 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
943 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
944 i = getpid();
945 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
946 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
948 XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
949 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
951 data->wm_hints = XAllocWMHints();
952 wine_tsx11_unlock();
954 if (data->wm_hints)
956 HICON icon = (HICON)SendMessageW( data->hwnd, WM_GETICON, ICON_BIG, 0 );
957 if (!icon) icon = (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON );
958 data->wm_hints->flags = 0;
959 set_icon_hints( display, data, icon );
964 /***********************************************************************
965 * set_wm_hints
967 * Set the window manager hints for a newly-created window
969 static void set_wm_hints( Display *display, struct x11drv_win_data *data )
971 Window group_leader;
972 Atom window_type;
973 MwmHints mwm_hints;
974 DWORD style, ex_style;
975 HWND owner;
977 if (data->hwnd == GetDesktopWindow())
979 /* force some styles for the desktop to get the correct decorations */
980 style = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
981 ex_style = WS_EX_APPWINDOW;
982 owner = 0;
984 else
986 style = GetWindowLongW( data->hwnd, GWL_STYLE );
987 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
988 owner = get_window_owner( data->hwnd );
991 /* transient for hint */
992 if (owner)
994 Window owner_win = X11DRV_get_whole_window( owner );
995 wine_tsx11_lock();
996 XSetTransientForHint( display, data->whole_window, owner_win );
997 wine_tsx11_unlock();
998 group_leader = owner_win;
1000 else group_leader = data->whole_window;
1002 wine_tsx11_lock();
1004 /* size hints */
1005 set_size_hints( display, data, style );
1007 /* set the WM_WINDOW_TYPE */
1008 if (style & WS_THICKFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
1009 else if (ex_style & WS_EX_APPWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
1010 else if (style & WS_DLGFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
1011 else if (ex_style & WS_EX_DLGMODALFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
1012 else if ((style & WS_POPUP) && owner) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
1013 #if 0 /* many window managers don't handle utility windows very well */
1014 else if (ex_style & WS_EX_TOOLWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
1015 #endif
1016 else window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
1018 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
1019 XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
1021 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
1022 mwm_hints.decorations = get_mwm_decorations( data, style, ex_style );
1023 mwm_hints.functions = MWM_FUNC_MOVE;
1024 if (is_window_resizable( data, style )) mwm_hints.functions |= MWM_FUNC_RESIZE;
1025 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
1026 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
1027 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
1029 XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
1030 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
1031 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
1033 /* wm hints */
1034 if (data->wm_hints)
1036 data->wm_hints->flags |= InputHint | StateHint | WindowGroupHint;
1037 data->wm_hints->input = !(style & WS_DISABLED);
1038 data->wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
1039 data->wm_hints->window_group = group_leader;
1040 XSetWMHints( display, data->whole_window, data->wm_hints );
1043 wine_tsx11_unlock();
1047 /***********************************************************************
1048 * update_net_wm_states
1050 void update_net_wm_states( Display *display, struct x11drv_win_data *data )
1052 static const unsigned int state_atoms[NB_NET_WM_STATES] =
1054 XATOM__NET_WM_STATE_FULLSCREEN,
1055 XATOM__NET_WM_STATE_ABOVE,
1056 XATOM__NET_WM_STATE_MAXIMIZED_VERT,
1057 XATOM__NET_WM_STATE_SKIP_PAGER,
1058 XATOM__NET_WM_STATE_SKIP_TASKBAR
1061 DWORD i, style, ex_style, new_state = 0;
1063 if (!data->managed) return;
1064 if (data->whole_window == root_window) return;
1066 style = GetWindowLongW( data->hwnd, GWL_STYLE );
1067 if (data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
1068 data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height)
1070 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION)
1071 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
1072 else if (!(style & WS_MINIMIZE))
1073 new_state |= (1 << NET_WM_STATE_FULLSCREEN);
1075 else if (style & WS_MAXIMIZE)
1076 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
1078 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
1079 if (ex_style & WS_EX_TOPMOST)
1080 new_state |= (1 << NET_WM_STATE_ABOVE);
1081 if (ex_style & WS_EX_TOOLWINDOW)
1082 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR) | (1 << NET_WM_STATE_SKIP_PAGER);
1083 if (!(ex_style & WS_EX_APPWINDOW) && get_window_owner( data->hwnd ))
1084 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR);
1086 if (!data->mapped) /* set the _NET_WM_STATE atom directly */
1088 Atom atoms[NB_NET_WM_STATES+1];
1089 DWORD count;
1091 for (i = count = 0; i < NB_NET_WM_STATES; i++)
1093 if (!(new_state & (1 << i))) continue;
1094 TRACE( "setting wm state %u for unmapped window %p/%lx\n",
1095 i, data->hwnd, data->whole_window );
1096 atoms[count++] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1097 if (state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT)
1098 atoms[count++] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
1100 wine_tsx11_lock();
1101 XChangeProperty( display, data->whole_window, x11drv_atom(_NET_WM_STATE), XA_ATOM,
1102 32, PropModeReplace, (unsigned char *)atoms, count );
1103 wine_tsx11_unlock();
1105 else /* ask the window manager to do it for us */
1107 XEvent xev;
1109 xev.xclient.type = ClientMessage;
1110 xev.xclient.window = data->whole_window;
1111 xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
1112 xev.xclient.serial = 0;
1113 xev.xclient.display = display;
1114 xev.xclient.send_event = True;
1115 xev.xclient.format = 32;
1116 xev.xclient.data.l[3] = 1;
1118 for (i = 0; i < NB_NET_WM_STATES; i++)
1120 if (!((data->net_wm_state ^ new_state) & (1 << i))) continue; /* unchanged */
1122 TRACE( "setting wm state %u for window %p/%lx to %u prev %u\n",
1123 i, data->hwnd, data->whole_window,
1124 (new_state & (1 << i)) != 0, (data->net_wm_state & (1 << i)) != 0 );
1126 xev.xclient.data.l[0] = (new_state & (1 << i)) ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1127 xev.xclient.data.l[1] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1128 xev.xclient.data.l[2] = ((state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT) ?
1129 x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ) : 0);
1130 wine_tsx11_lock();
1131 XSendEvent( display, root_window, False,
1132 SubstructureRedirectMask | SubstructureNotifyMask, &xev );
1133 wine_tsx11_unlock();
1136 data->net_wm_state = new_state;
1140 /***********************************************************************
1141 * set_xembed_flags
1143 static void set_xembed_flags( Display *display, struct x11drv_win_data *data, unsigned long flags )
1145 unsigned long info[2];
1147 info[0] = 0; /* protocol version */
1148 info[1] = flags;
1149 wine_tsx11_lock();
1150 XChangeProperty( display, data->whole_window, x11drv_atom(_XEMBED_INFO),
1151 x11drv_atom(_XEMBED_INFO), 32, PropModeReplace, (unsigned char*)info, 2 );
1152 wine_tsx11_unlock();
1156 /***********************************************************************
1157 * map_window
1159 static void map_window( Display *display, struct x11drv_win_data *data, DWORD new_style )
1161 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1163 remove_startup_notification( display, data->whole_window );
1165 wait_for_withdrawn_state( display, data, TRUE );
1167 if (!data->embedded)
1169 update_net_wm_states( display, data );
1170 sync_window_style( display, data );
1171 wine_tsx11_lock();
1172 XMapWindow( display, data->whole_window );
1173 wine_tsx11_unlock();
1175 else set_xembed_flags( display, data, XEMBED_MAPPED );
1177 data->mapped = TRUE;
1178 data->iconic = (new_style & WS_MINIMIZE) != 0;
1182 /***********************************************************************
1183 * unmap_window
1185 static void unmap_window( Display *display, struct x11drv_win_data *data )
1187 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1189 if (!data->embedded)
1191 wait_for_withdrawn_state( display, data, FALSE );
1192 wine_tsx11_lock();
1193 if (data->managed) XWithdrawWindow( display, data->whole_window, DefaultScreen(display) );
1194 else XUnmapWindow( display, data->whole_window );
1195 wine_tsx11_unlock();
1197 else set_xembed_flags( display, data, 0 );
1199 data->mapped = FALSE;
1200 data->net_wm_state = 0;
1204 /***********************************************************************
1205 * make_window_embedded
1207 void make_window_embedded( Display *display, struct x11drv_win_data *data )
1209 if (data->mapped)
1211 /* the window cannot be mapped before being embedded */
1212 unmap_window( display, data );
1213 data->embedded = TRUE;
1214 map_window( display, data, 0 );
1216 else
1218 data->embedded = TRUE;
1219 set_xembed_flags( display, data, 0 );
1224 /***********************************************************************
1225 * X11DRV_window_to_X_rect
1227 * Convert a rect from client to X window coordinates
1229 static void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
1231 RECT rc;
1233 if (!data->managed) return;
1234 if (IsRectEmpty( rect )) return;
1236 get_x11_rect_offset( data, &rc );
1238 rect->left -= rc.left;
1239 rect->right -= rc.right;
1240 rect->top -= rc.top;
1241 rect->bottom -= rc.bottom;
1242 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1243 if (rect->left >= rect->right) rect->right = rect->left + 1;
1247 /***********************************************************************
1248 * X11DRV_X_to_window_rect
1250 * Opposite of X11DRV_window_to_X_rect
1252 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
1254 RECT rc;
1256 if (!data->managed) return;
1257 if (IsRectEmpty( rect )) return;
1259 get_x11_rect_offset( data, &rc );
1261 rect->left += rc.left;
1262 rect->right += rc.right;
1263 rect->top += rc.top;
1264 rect->bottom += rc.bottom;
1265 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1266 if (rect->left >= rect->right) rect->right = rect->left + 1;
1270 /***********************************************************************
1271 * sync_window_position
1273 * Synchronize the X window position with the Windows one
1275 static void sync_window_position( Display *display, struct x11drv_win_data *data,
1276 UINT swp_flags, const RECT *old_client_rect,
1277 const RECT *old_whole_rect )
1279 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
1280 XWindowChanges changes;
1281 unsigned int mask = 0;
1283 if (data->managed && data->iconic) return;
1285 /* resizing a managed maximized window is not allowed */
1286 if (!(style & WS_MAXIMIZE) || !data->managed)
1288 changes.width = data->whole_rect.right - data->whole_rect.left;
1289 changes.height = data->whole_rect.bottom - data->whole_rect.top;
1290 /* if window rect is empty force size to 1x1 */
1291 if (changes.width <= 0 || changes.height <= 0) changes.width = changes.height = 1;
1292 mask |= CWWidth | CWHeight;
1295 /* only the size is allowed to change for the desktop window */
1296 if (data->whole_window != root_window)
1298 changes.x = data->whole_rect.left - virtual_screen_rect.left;
1299 changes.y = data->whole_rect.top - virtual_screen_rect.top;
1300 mask |= CWX | CWY;
1303 if (!(swp_flags & SWP_NOZORDER) || (swp_flags & SWP_SHOWWINDOW))
1305 /* find window that this one must be after */
1306 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
1307 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
1308 prev = GetWindow( prev, GW_HWNDPREV );
1309 if (!prev) /* top child */
1311 changes.stack_mode = Above;
1312 mask |= CWStackMode;
1314 /* should use stack_mode Below but most window managers don't get it right */
1315 /* and Above with a sibling doesn't work so well either, so we ignore it */
1318 TRACE( "setting win %p/%lx pos %d,%d,%dx%d after %lx changes=%x\n",
1319 data->hwnd, data->whole_window, data->whole_rect.left, data->whole_rect.top,
1320 data->whole_rect.right - data->whole_rect.left,
1321 data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
1323 wine_tsx11_lock();
1324 set_size_hints( display, data, style );
1325 XReconfigureWMWindow( display, data->whole_window,
1326 DefaultScreen(display), mask, &changes );
1327 wine_tsx11_unlock();
1331 /***********************************************************************
1332 * sync_client_position
1334 * Synchronize the X client window position with the Windows one
1336 static void sync_client_position( Display *display, struct x11drv_win_data *data,
1337 UINT swp_flags, const RECT *old_client_rect,
1338 const RECT *old_whole_rect )
1340 int mask;
1341 XWindowChanges changes;
1342 RECT old = *old_client_rect;
1343 RECT new = data->client_rect;
1345 OffsetRect( &old, -old_whole_rect->left, -old_whole_rect->top );
1346 OffsetRect( &new, -data->whole_rect.left, -data->whole_rect.top );
1347 if (!(mask = get_window_changes( &changes, &old, &new ))) return;
1349 if (data->client_window)
1351 TRACE( "setting client win %lx pos %d,%d,%dx%d changes=%x\n",
1352 data->client_window, new.left, new.top,
1353 new.right - new.left, new.bottom - new.top, mask );
1354 wine_tsx11_lock();
1355 XConfigureWindow( display, data->client_window, mask, &changes );
1356 wine_tsx11_unlock();
1359 if (data->gl_drawable && (mask & (CWWidth|CWHeight))) sync_gl_drawable( data );
1363 /***********************************************************************
1364 * move_window_bits
1366 * Move the window bits when a window is moved.
1368 static void move_window_bits( struct x11drv_win_data *data, const RECT *old_rect, const RECT *new_rect,
1369 const RECT *old_client_rect )
1371 RECT src_rect = *old_rect;
1372 RECT dst_rect = *new_rect;
1373 HDC hdc_src, hdc_dst;
1374 INT code;
1375 HRGN rgn = 0;
1376 HWND parent = 0;
1378 if (!data->whole_window)
1380 OffsetRect( &dst_rect, -data->window_rect.left, -data->window_rect.top );
1381 parent = GetAncestor( data->hwnd, GA_PARENT );
1382 hdc_src = GetDCEx( parent, 0, DCX_CACHE );
1383 hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE | DCX_WINDOW );
1385 else
1387 OffsetRect( &dst_rect, -data->client_rect.left, -data->client_rect.top );
1388 /* make src rect relative to the old position of the window */
1389 OffsetRect( &src_rect, -old_client_rect->left, -old_client_rect->top );
1390 if (dst_rect.left == src_rect.left && dst_rect.top == src_rect.top) return;
1391 hdc_src = hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE );
1394 code = X11DRV_START_EXPOSURES;
1395 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
1397 TRACE( "copying bits for win %p/%lx/%lx %s -> %s\n",
1398 data->hwnd, data->whole_window, data->client_window,
1399 wine_dbgstr_rect(&src_rect), wine_dbgstr_rect(&dst_rect) );
1400 BitBlt( hdc_dst, dst_rect.left, dst_rect.top,
1401 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top,
1402 hdc_src, src_rect.left, src_rect.top, SRCCOPY );
1404 code = X11DRV_END_EXPOSURES;
1405 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, sizeof(rgn), (LPSTR)&rgn );
1407 ReleaseDC( data->hwnd, hdc_dst );
1408 if (hdc_src != hdc_dst) ReleaseDC( parent, hdc_src );
1410 if (rgn)
1412 if (!data->whole_window)
1414 /* map region to client rect since we are using DCX_WINDOW */
1415 OffsetRgn( rgn, data->window_rect.left - data->client_rect.left,
1416 data->window_rect.top - data->client_rect.top );
1417 RedrawWindow( data->hwnd, NULL, rgn,
1418 RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ALLCHILDREN );
1420 else RedrawWindow( data->hwnd, NULL, rgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1421 DeleteObject( rgn );
1426 /**********************************************************************
1427 * create_whole_window
1429 * Create the whole X window for a given window
1431 static Window create_whole_window( Display *display, struct x11drv_win_data *data )
1433 int cx, cy, mask;
1434 XSetWindowAttributes attr;
1435 WCHAR text[1024];
1436 COLORREF key;
1437 BYTE alpha;
1438 DWORD layered_flags;
1440 if (!(cx = data->window_rect.right - data->window_rect.left)) cx = 1;
1441 if (!(cy = data->window_rect.bottom - data->window_rect.top)) cy = 1;
1443 if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1445 TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1446 data->managed = TRUE;
1447 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1450 mask = get_window_attributes( display, data, &attr );
1452 wine_tsx11_lock();
1454 data->whole_rect = data->window_rect;
1455 data->whole_window = XCreateWindow( display, root_window,
1456 data->window_rect.left - virtual_screen_rect.left,
1457 data->window_rect.top - virtual_screen_rect.top,
1458 cx, cy, 0, screen_depth, InputOutput,
1459 visual, mask, &attr );
1461 if (data->whole_window) XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
1462 wine_tsx11_unlock();
1464 if (!data->whole_window) return 0;
1466 if (!create_client_window( display, data, NULL ))
1468 wine_tsx11_lock();
1469 XDeleteContext( display, data->whole_window, winContext );
1470 XDestroyWindow( display, data->whole_window );
1471 data->whole_window = 0;
1472 wine_tsx11_unlock();
1473 return 0;
1476 set_initial_wm_hints( display, data );
1477 set_wm_hints( display, data );
1479 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1481 /* set the window text */
1482 if (!InternalGetWindowText( data->hwnd, text, sizeof(text)/sizeof(WCHAR) )) text[0] = 0;
1483 sync_window_text( display, data->whole_window, text );
1485 /* set the window region */
1486 sync_window_region( display, data, (HRGN)1 );
1488 /* set the window opacity */
1489 if (!GetLayeredWindowAttributes( data->hwnd, &key, &alpha, &layered_flags )) layered_flags = 0;
1490 sync_window_opacity( display, data->whole_window, key, alpha, layered_flags );
1492 wine_tsx11_lock();
1493 XFlush( display ); /* make sure the window exists before we start painting to it */
1494 wine_tsx11_unlock();
1495 return data->whole_window;
1499 /**********************************************************************
1500 * destroy_whole_window
1502 * Destroy the whole X window for a given window.
1504 static void destroy_whole_window( Display *display, struct x11drv_win_data *data, BOOL already_destroyed )
1506 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1508 if (!data->whole_window) return;
1510 TRACE( "win %p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1511 if (thread_data->cursor_window == data->whole_window ||
1512 thread_data->cursor_window == data->client_window)
1513 thread_data->cursor_window = None;
1514 wine_tsx11_lock();
1515 XDeleteContext( display, data->whole_window, winContext );
1516 XDeleteContext( display, data->client_window, winContext );
1517 if (!already_destroyed) XDestroyWindow( display, data->whole_window );
1518 data->whole_window = data->client_window = 0;
1519 data->wm_state = WithdrawnState;
1520 data->net_wm_state = 0;
1521 data->mapped = FALSE;
1522 if (data->xic)
1524 XUnsetICFocus( data->xic );
1525 XDestroyIC( data->xic );
1526 data->xic = 0;
1528 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1529 XFlush( display );
1530 XFree( data->wm_hints );
1531 data->wm_hints = NULL;
1532 wine_tsx11_unlock();
1533 RemovePropA( data->hwnd, whole_window_prop );
1534 RemovePropA( data->hwnd, client_window_prop );
1538 /*****************************************************************
1539 * SetWindowText (X11DRV.@)
1541 void CDECL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1543 Window win;
1545 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(gdi_display))
1547 Display *display = thread_init_display();
1548 sync_window_text( display, win, text );
1553 /***********************************************************************
1554 * SetWindowStyle (X11DRV.@)
1556 * Update the X state of a window to reflect a style change
1558 void CDECL X11DRV_SetWindowStyle( HWND hwnd, INT offset, STYLESTRUCT *style )
1560 struct x11drv_win_data *data;
1561 DWORD changed;
1563 if (hwnd == GetDesktopWindow()) return;
1564 changed = style->styleNew ^ style->styleOld;
1566 if (offset == GWL_STYLE && (changed & WS_VISIBLE) && (style->styleNew & WS_VISIBLE))
1568 /* we don't unmap windows, that causes trouble with the window manager */
1569 if (!(data = X11DRV_get_win_data( hwnd )) &&
1570 !(data = X11DRV_create_win_data( hwnd ))) return;
1572 if (data->whole_window && is_window_rect_mapped( &data->window_rect ))
1574 Display *display = thread_display();
1575 set_wm_hints( display, data );
1576 if (!data->mapped) map_window( display, data, style->styleNew );
1580 if (offset == GWL_STYLE && (changed & WS_DISABLED))
1582 data = X11DRV_get_win_data( hwnd );
1583 if (data && data->wm_hints)
1585 wine_tsx11_lock();
1586 data->wm_hints->input = !(style->styleNew & WS_DISABLED);
1587 XSetWMHints( thread_display(), data->whole_window, data->wm_hints );
1588 wine_tsx11_unlock();
1592 if (offset == GWL_EXSTYLE && (changed & WS_EX_LAYERED))
1594 /* changing WS_EX_LAYERED resets attributes */
1595 if ((data = X11DRV_get_win_data( hwnd )) && data->whole_window)
1596 sync_window_opacity( thread_display(), data->whole_window, 0, 0, 0 );
1601 /***********************************************************************
1602 * DestroyWindow (X11DRV.@)
1604 void CDECL X11DRV_DestroyWindow( HWND hwnd )
1606 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1607 struct x11drv_win_data *data;
1609 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1611 if (data->pixmap)
1613 wine_tsx11_lock();
1614 destroy_glxpixmap(gdi_display, data->gl_drawable);
1615 XFreePixmap(gdi_display, data->pixmap);
1616 wine_tsx11_unlock();
1618 else if (data->gl_drawable)
1620 wine_tsx11_lock();
1621 XDestroyWindow(gdi_display, data->gl_drawable);
1622 wine_tsx11_unlock();
1625 destroy_whole_window( thread_data->display, data, FALSE );
1626 destroy_icon_window( thread_data->display, data );
1628 if (data->colormap)
1630 wine_tsx11_lock();
1631 XFreeColormap( thread_data->display, data->colormap );
1632 wine_tsx11_unlock();
1635 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1636 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
1637 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
1638 wine_tsx11_lock();
1639 XDeleteContext( thread_data->display, (XID)hwnd, win_data_context );
1640 wine_tsx11_unlock();
1641 HeapFree( GetProcessHeap(), 0, data );
1645 /***********************************************************************
1646 * X11DRV_DestroyNotify
1648 void X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1650 Display *display = event->xdestroywindow.display;
1651 struct x11drv_win_data *data;
1653 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1655 FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1656 destroy_whole_window( display, data, TRUE );
1660 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
1662 struct x11drv_win_data *data;
1664 if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
1666 data->hwnd = hwnd;
1667 wine_tsx11_lock();
1668 if (!winContext) winContext = XUniqueContext();
1669 if (!win_data_context) win_data_context = XUniqueContext();
1670 XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
1671 wine_tsx11_unlock();
1673 return data;
1677 /* initialize the desktop window id in the desktop manager process */
1678 static struct x11drv_win_data *create_desktop_win_data( Display *display, HWND hwnd )
1680 struct x11drv_win_data *data;
1682 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1683 data->whole_window = data->client_window = root_window;
1684 data->managed = TRUE;
1685 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1686 SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
1687 SetPropA( data->hwnd, client_window_prop, (HANDLE)root_window );
1688 set_initial_wm_hints( display, data );
1689 return data;
1692 /**********************************************************************
1693 * CreateDesktopWindow (X11DRV.@)
1695 BOOL CDECL X11DRV_CreateDesktopWindow( HWND hwnd )
1697 unsigned int width, height;
1699 /* retrieve the real size of the desktop */
1700 SERVER_START_REQ( get_window_rectangles )
1702 req->handle = wine_server_user_handle( hwnd );
1703 wine_server_call( req );
1704 width = reply->window.right - reply->window.left;
1705 height = reply->window.bottom - reply->window.top;
1707 SERVER_END_REQ;
1709 if (!width && !height) /* not initialized yet */
1711 SERVER_START_REQ( set_window_pos )
1713 req->handle = wine_server_user_handle( hwnd );
1714 req->previous = 0;
1715 req->flags = SWP_NOZORDER;
1716 req->window.left = virtual_screen_rect.left;
1717 req->window.top = virtual_screen_rect.top;
1718 req->window.right = virtual_screen_rect.right;
1719 req->window.bottom = virtual_screen_rect.bottom;
1720 req->client = req->window;
1721 wine_server_call( req );
1723 SERVER_END_REQ;
1725 else
1727 Window win = (Window)GetPropA( hwnd, whole_window_prop );
1728 if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1730 return TRUE;
1734 /**********************************************************************
1735 * CreateWindow (X11DRV.@)
1737 BOOL CDECL X11DRV_CreateWindow( HWND hwnd )
1739 if (hwnd == GetDesktopWindow() && root_window != DefaultRootWindow( gdi_display ))
1741 Display *display = thread_init_display();
1743 /* the desktop win data can't be created lazily */
1744 if (!create_desktop_win_data( display, hwnd )) return FALSE;
1746 return TRUE;
1750 /***********************************************************************
1751 * X11DRV_get_win_data
1753 * Return the X11 data structure associated with a window.
1755 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1757 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1758 char *data;
1760 if (!thread_data) return NULL;
1761 if (!hwnd) return NULL;
1762 if (XFindContext( thread_data->display, (XID)hwnd, win_data_context, &data )) data = NULL;
1763 return (struct x11drv_win_data *)data;
1767 /***********************************************************************
1768 * X11DRV_create_win_data
1770 * Create an X11 data window structure for an existing window.
1772 struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd )
1774 Display *display;
1775 struct x11drv_win_data *data;
1776 HWND parent;
1778 if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL; /* desktop */
1780 /* don't create win data for HWND_MESSAGE windows */
1781 if (parent != GetDesktopWindow() && !GetAncestor( parent, GA_PARENT )) return NULL;
1783 display = thread_init_display();
1784 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1786 GetWindowRect( hwnd, &data->window_rect );
1787 MapWindowPoints( 0, parent, (POINT *)&data->window_rect, 2 );
1788 data->whole_rect = data->window_rect;
1789 GetClientRect( hwnd, &data->client_rect );
1790 MapWindowPoints( hwnd, parent, (POINT *)&data->client_rect, 2 );
1792 if (parent == GetDesktopWindow())
1794 if (!create_whole_window( display, data ))
1796 HeapFree( GetProcessHeap(), 0, data );
1797 return NULL;
1799 TRACE( "win %p/%lx/%lx window %s whole %s client %s\n",
1800 hwnd, data->whole_window, data->client_window, wine_dbgstr_rect( &data->window_rect ),
1801 wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
1803 return data;
1807 /***********************************************************************
1808 * X11DRV_get_whole_window
1810 * Return the X window associated with the full area of a window
1812 Window X11DRV_get_whole_window( HWND hwnd )
1814 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1816 if (!data)
1818 if (hwnd == GetDesktopWindow()) return root_window;
1819 return (Window)GetPropA( hwnd, whole_window_prop );
1821 return data->whole_window;
1825 /***********************************************************************
1826 * X11DRV_get_client_window
1828 * Return the X window associated with the client area of a window
1830 static Window X11DRV_get_client_window( HWND hwnd )
1832 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1834 if (!data)
1836 if (hwnd == GetDesktopWindow()) return root_window;
1837 return (Window)GetPropA( hwnd, client_window_prop );
1839 return data->client_window;
1843 /***********************************************************************
1844 * X11DRV_get_ic
1846 * Return the X input context associated with a window
1848 XIC X11DRV_get_ic( HWND hwnd )
1850 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1851 XIM xim;
1853 if (!data) return 0;
1854 if (data->xic) return data->xic;
1855 if (!(xim = x11drv_thread_data()->xim)) return 0;
1856 return X11DRV_CreateIC( xim, data );
1860 /***********************************************************************
1861 * X11DRV_GetDC (X11DRV.@)
1863 void CDECL X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
1864 const RECT *top_rect, DWORD flags )
1866 struct x11drv_escape_set_drawable escape;
1867 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1869 escape.code = X11DRV_SET_DRAWABLE;
1870 escape.mode = IncludeInferiors;
1871 escape.fbconfig_id = 0;
1872 escape.gl_drawable = 0;
1873 escape.pixmap = 0;
1874 escape.gl_copy = FALSE;
1876 if (top == hwnd && data && IsIconic( hwnd ) && data->icon_window)
1878 escape.drawable = data->icon_window;
1880 else if (top == hwnd)
1882 escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
1883 /* GL draws to the client area even for window DCs */
1884 escape.gl_drawable = data ? data->client_window : X11DRV_get_client_window( hwnd );
1885 if (flags & DCX_WINDOW)
1886 escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
1887 else
1888 escape.drawable = escape.gl_drawable;
1890 else
1892 escape.drawable = X11DRV_get_client_window( top );
1893 escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
1894 escape.gl_drawable = data ? data->gl_drawable : (Drawable)GetPropA( hwnd, gl_drawable_prop );
1895 escape.pixmap = data ? data->pixmap : (Pixmap)GetPropA( hwnd, pixmap_prop );
1896 escape.gl_copy = (escape.gl_drawable != 0);
1897 if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
1900 escape.dc_rect.left = win_rect->left - top_rect->left;
1901 escape.dc_rect.top = win_rect->top - top_rect->top;
1902 escape.dc_rect.right = win_rect->right - top_rect->left;
1903 escape.dc_rect.bottom = win_rect->bottom - top_rect->top;
1904 escape.drawable_rect.left = top_rect->left;
1905 escape.drawable_rect.top = top_rect->top;
1906 escape.drawable_rect.right = top_rect->right;
1907 escape.drawable_rect.bottom = top_rect->bottom;
1909 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1913 /***********************************************************************
1914 * X11DRV_ReleaseDC (X11DRV.@)
1916 void CDECL X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
1918 struct x11drv_escape_set_drawable escape;
1920 escape.code = X11DRV_SET_DRAWABLE;
1921 escape.drawable = root_window;
1922 escape.mode = IncludeInferiors;
1923 escape.drawable_rect = virtual_screen_rect;
1924 SetRect( &escape.dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
1925 virtual_screen_rect.bottom - virtual_screen_rect.top );
1926 escape.fbconfig_id = 0;
1927 escape.gl_drawable = 0;
1928 escape.pixmap = 0;
1929 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1933 /***********************************************************************
1934 * SetCapture (X11DRV.@)
1936 void CDECL X11DRV_SetCapture( HWND hwnd, UINT flags )
1938 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1940 if (!thread_data) return;
1941 if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
1943 if (hwnd)
1945 Window grab_win = X11DRV_get_client_window( GetAncestor( hwnd, GA_ROOT ) );
1947 if (!grab_win) return;
1948 wine_tsx11_lock();
1949 XFlush( gdi_display );
1950 XGrabPointer( thread_data->display, grab_win, False,
1951 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1952 GrabModeAsync, GrabModeAsync, None, None, CurrentTime );
1953 wine_tsx11_unlock();
1954 thread_data->grab_window = grab_win;
1956 else /* release capture */
1958 wine_tsx11_lock();
1959 XFlush( gdi_display );
1960 XUngrabPointer( thread_data->display, CurrentTime );
1961 XFlush( thread_data->display );
1962 wine_tsx11_unlock();
1963 thread_data->grab_window = None;
1968 /*****************************************************************
1969 * SetParent (X11DRV.@)
1971 void CDECL X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
1973 Display *display = thread_display();
1974 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1976 if (!data) return;
1977 if (parent == old_parent) return;
1979 if (parent != GetDesktopWindow()) /* a child window */
1981 if (old_parent == GetDesktopWindow())
1983 /* destroy the old X windows */
1984 destroy_whole_window( display, data, FALSE );
1985 destroy_icon_window( display, data );
1986 if (data->managed)
1988 data->managed = FALSE;
1989 RemovePropA( data->hwnd, managed_prop );
1993 else /* new top level window */
1995 /* FIXME: we ignore errors since we can't really recover anyway */
1996 create_whole_window( display, data );
2001 /*****************************************************************
2002 * SetFocus (X11DRV.@)
2004 * Set the X focus.
2006 void CDECL X11DRV_SetFocus( HWND hwnd )
2008 Display *display = thread_display();
2009 struct x11drv_win_data *data;
2010 XWindowChanges changes;
2012 if (!(hwnd = GetAncestor( hwnd, GA_ROOT ))) return;
2013 if (!(data = X11DRV_get_win_data( hwnd ))) return;
2014 if (data->managed || !data->whole_window) return;
2016 /* Set X focus and install colormap */
2017 wine_tsx11_lock();
2018 changes.stack_mode = Above;
2019 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
2020 if (root_window == DefaultRootWindow(display))
2022 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
2023 /* FIXME: this is not entirely correct */
2024 XSetInputFocus( display, data->whole_window, RevertToParent,
2025 /* CurrentTime */
2026 GetMessageTime() - EVENT_x11_time_to_win32_time(0));
2028 wine_tsx11_unlock();
2032 /***********************************************************************
2033 * WindowPosChanging (X11DRV.@)
2035 void CDECL X11DRV_WindowPosChanging( HWND hwnd, HWND insert_after, UINT swp_flags,
2036 const RECT *window_rect, const RECT *client_rect, RECT *visible_rect )
2038 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2039 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
2041 if (!data)
2043 /* create the win data if the window is being made visible */
2044 if (!(style & WS_VISIBLE) && !(swp_flags & SWP_SHOWWINDOW)) return;
2045 if (!(data = X11DRV_create_win_data( hwnd ))) return;
2048 /* check if we need to switch the window to managed */
2049 if (!data->managed && data->whole_window && is_window_managed( hwnd, swp_flags, window_rect ))
2051 TRACE( "making win %p/%lx managed\n", hwnd, data->whole_window );
2052 if (data->mapped) unmap_window( thread_display(), data );
2053 data->managed = TRUE;
2054 SetPropA( hwnd, managed_prop, (HANDLE)1 );
2057 *visible_rect = *window_rect;
2058 X11DRV_window_to_X_rect( data, visible_rect );
2062 /***********************************************************************
2063 * WindowPosChanged (X11DRV.@)
2065 void CDECL X11DRV_WindowPosChanged( HWND hwnd, HWND insert_after, UINT swp_flags,
2066 const RECT *rectWindow, const RECT *rectClient,
2067 const RECT *visible_rect, const RECT *valid_rects )
2069 struct x11drv_thread_data *thread_data;
2070 Display *display;
2071 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2072 DWORD new_style = GetWindowLongW( hwnd, GWL_STYLE );
2073 RECT old_whole_rect, old_client_rect;
2074 int event_type;
2076 if (!data) return;
2078 thread_data = x11drv_thread_data();
2079 display = thread_data->display;
2081 old_whole_rect = data->whole_rect;
2082 old_client_rect = data->client_rect;
2083 data->window_rect = *rectWindow;
2084 data->whole_rect = *visible_rect;
2085 data->client_rect = *rectClient;
2087 TRACE( "win %p window %s client %s style %08x flags %08x\n",
2088 hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style, swp_flags );
2090 if (!IsRectEmpty( &valid_rects[0] ))
2092 int x_offset = old_whole_rect.left - data->whole_rect.left;
2093 int y_offset = old_whole_rect.top - data->whole_rect.top;
2095 /* if all that happened is that the whole window moved, copy everything */
2096 if (!(swp_flags & SWP_FRAMECHANGED) &&
2097 old_whole_rect.right - data->whole_rect.right == x_offset &&
2098 old_whole_rect.bottom - data->whole_rect.bottom == y_offset &&
2099 old_client_rect.left - data->client_rect.left == x_offset &&
2100 old_client_rect.right - data->client_rect.right == x_offset &&
2101 old_client_rect.top - data->client_rect.top == y_offset &&
2102 old_client_rect.bottom - data->client_rect.bottom == y_offset &&
2103 !memcmp( &valid_rects[0], &data->client_rect, sizeof(RECT) ))
2105 /* if we have an X window the bits will be moved by the X server */
2106 if (!data->whole_window)
2107 move_window_bits( data, &old_whole_rect, &data->whole_rect, &old_client_rect );
2109 else
2110 move_window_bits( data, &valid_rects[1], &valid_rects[0], &old_client_rect );
2113 wine_tsx11_lock();
2114 XFlush( gdi_display ); /* make sure painting is done before we move the window */
2115 wine_tsx11_unlock();
2117 sync_client_position( display, data, swp_flags, &old_client_rect, &old_whole_rect );
2119 if (!data->whole_window) return;
2121 /* check if we are currently processing an event relevant to this window */
2122 event_type = 0;
2123 if (thread_data->current_event && thread_data->current_event->xany.window == data->whole_window)
2124 event_type = thread_data->current_event->type;
2126 if (event_type != ConfigureNotify && event_type != PropertyNotify)
2127 event_type = 0; /* ignore other events */
2129 if (data->mapped)
2131 if (((swp_flags & SWP_HIDEWINDOW) && !(new_style & WS_VISIBLE)) ||
2132 (!event_type && !is_window_rect_mapped( rectWindow )))
2133 unmap_window( display, data );
2136 /* don't change position if we are about to minimize or maximize a managed window */
2137 if (!event_type &&
2138 !(data->managed && (swp_flags & SWP_STATECHANGED) && (new_style & (WS_MINIMIZE|WS_MAXIMIZE))))
2139 sync_window_position( display, data, swp_flags, &old_client_rect, &old_whole_rect );
2141 if ((new_style & WS_VISIBLE) &&
2142 ((new_style & WS_MINIMIZE) || is_window_rect_mapped( rectWindow )))
2144 if (!data->mapped || (swp_flags & (SWP_FRAMECHANGED|SWP_STATECHANGED)))
2145 set_wm_hints( display, data );
2147 if (!data->mapped)
2149 map_window( display, data, new_style );
2151 else if ((swp_flags & SWP_STATECHANGED) && (!data->iconic != !(new_style & WS_MINIMIZE)))
2153 data->iconic = (new_style & WS_MINIMIZE) != 0;
2154 TRACE( "changing win %p iconic state to %u\n", data->hwnd, data->iconic );
2155 wine_tsx11_lock();
2156 if (data->iconic)
2157 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
2158 else if (is_window_rect_mapped( rectWindow ))
2159 XMapWindow( display, data->whole_window );
2160 wine_tsx11_unlock();
2161 update_net_wm_states( display, data );
2163 else if (!event_type)
2165 update_net_wm_states( display, data );
2169 wine_tsx11_lock();
2170 XFlush( display ); /* make sure changes are done before we start painting again */
2171 wine_tsx11_unlock();
2175 /***********************************************************************
2176 * ShowWindow (X11DRV.@)
2178 UINT CDECL X11DRV_ShowWindow( HWND hwnd, INT cmd, RECT *rect, UINT swp )
2180 int x, y;
2181 unsigned int width, height, border, depth;
2182 Window root, top;
2183 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
2184 struct x11drv_thread_data *thread_data = x11drv_thread_data();
2185 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2187 if (!data || !data->whole_window || !data->managed || !data->mapped || data->iconic) return swp;
2188 if (style & WS_MINIMIZE) return swp;
2189 if (IsRectEmpty( rect )) return swp;
2191 /* only fetch the new rectangle if the ShowWindow was a result of a window manager event */
2193 if (!thread_data->current_event || thread_data->current_event->xany.window != data->whole_window)
2194 return swp;
2196 if (thread_data->current_event->type != ConfigureNotify &&
2197 thread_data->current_event->type != PropertyNotify)
2198 return swp;
2200 TRACE( "win %p/%lx cmd %d at %s flags %08x\n",
2201 hwnd, data->whole_window, cmd, wine_dbgstr_rect(rect), swp );
2203 wine_tsx11_lock();
2204 XGetGeometry( thread_data->display, data->whole_window,
2205 &root, &x, &y, &width, &height, &border, &depth );
2206 XTranslateCoordinates( thread_data->display, data->whole_window, root, 0, 0, &x, &y, &top );
2207 wine_tsx11_unlock();
2208 rect->left = x;
2209 rect->top = y;
2210 rect->right = x + width;
2211 rect->bottom = y + height;
2212 OffsetRect( rect, virtual_screen_rect.left, virtual_screen_rect.top );
2213 X11DRV_X_to_window_rect( data, rect );
2214 return swp & ~(SWP_NOMOVE | SWP_NOCLIENTMOVE | SWP_NOSIZE | SWP_NOCLIENTSIZE);
2218 /**********************************************************************
2219 * SetWindowIcon (X11DRV.@)
2221 * hIcon or hIconSm has changed (or is being initialised for the
2222 * first time). Complete the X11 driver-specific initialisation
2223 * and set the window hints.
2225 * This is not entirely correct, may need to create
2226 * an icon window and set the pixmap as a background
2228 void CDECL X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
2230 Display *display = thread_display();
2231 struct x11drv_win_data *data;
2233 if (type != ICON_BIG) return; /* nothing to do here */
2235 if (!(data = X11DRV_get_win_data( hwnd ))) return;
2236 if (!data->whole_window) return;
2237 if (!data->managed) return;
2239 if (data->wm_hints)
2241 set_icon_hints( display, data, icon );
2242 wine_tsx11_lock();
2243 XSetWMHints( display, data->whole_window, data->wm_hints );
2244 wine_tsx11_unlock();
2249 /***********************************************************************
2250 * SetWindowRgn (X11DRV.@)
2252 * Assign specified region to window (for non-rectangular windows)
2254 int CDECL X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
2256 struct x11drv_win_data *data;
2258 if ((data = X11DRV_get_win_data( hwnd )))
2260 sync_window_region( thread_display(), data, hrgn );
2262 else if (X11DRV_get_whole_window( hwnd ))
2264 SendMessageW( hwnd, WM_X11DRV_SET_WIN_REGION, 0, 0 );
2266 return TRUE;
2270 /***********************************************************************
2271 * SetLayeredWindowAttributes (X11DRV.@)
2273 * Set transparency attributes for a layered window.
2275 void CDECL X11DRV_SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
2277 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2279 if (data)
2281 if (data->whole_window)
2282 sync_window_opacity( thread_display(), data->whole_window, key, alpha, flags );
2284 else
2286 Window win = X11DRV_get_whole_window( hwnd );
2287 if (win) sync_window_opacity( gdi_display, win, key, alpha, flags );
2292 /**********************************************************************
2293 * X11DRV_WindowMessage (X11DRV.@)
2295 LRESULT CDECL X11DRV_WindowMessage( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
2297 struct x11drv_win_data *data;
2299 switch(msg)
2301 case WM_X11DRV_ACQUIRE_SELECTION:
2302 return X11DRV_AcquireClipboard( hwnd );
2303 case WM_X11DRV_DELETE_WINDOW:
2304 return SendMessageW( hwnd, WM_SYSCOMMAND, SC_CLOSE, 0 );
2305 case WM_X11DRV_SET_WIN_FORMAT:
2306 return set_win_format( hwnd, (XID)wp );
2307 case WM_X11DRV_SET_WIN_REGION:
2308 if ((data = X11DRV_get_win_data( hwnd ))) sync_window_region( thread_display(), data, (HRGN)1 );
2309 return 0;
2310 case WM_X11DRV_RESIZE_DESKTOP:
2311 X11DRV_resize_desktop( LOWORD(lp), HIWORD(lp) );
2312 return 0;
2313 default:
2314 FIXME( "got window msg %x hwnd %p wp %lx lp %lx\n", msg, hwnd, wp, lp );
2315 return 0;
2320 /***********************************************************************
2321 * is_netwm_supported
2323 static BOOL is_netwm_supported( Display *display, Atom atom )
2325 static Atom *net_supported;
2326 static int net_supported_count = -1;
2327 int i;
2329 wine_tsx11_lock();
2330 if (net_supported_count == -1)
2332 Atom type;
2333 int format;
2334 unsigned long count, remaining;
2336 if (!XGetWindowProperty( display, DefaultRootWindow(display), x11drv_atom(_NET_SUPPORTED), 0,
2337 ~0UL, False, XA_ATOM, &type, &format, &count,
2338 &remaining, (unsigned char **)&net_supported ))
2339 net_supported_count = get_property_size( format, count ) / sizeof(Atom);
2340 else
2341 net_supported_count = 0;
2343 wine_tsx11_unlock();
2345 for (i = 0; i < net_supported_count; i++)
2346 if (net_supported[i] == atom) return TRUE;
2347 return FALSE;
2351 /***********************************************************************
2352 * SysCommand (X11DRV.@)
2354 * Perform WM_SYSCOMMAND handling.
2356 LRESULT CDECL X11DRV_SysCommand( HWND hwnd, WPARAM wparam, LPARAM lparam )
2358 WPARAM hittest = wparam & 0x0f;
2359 DWORD dwPoint;
2360 int x, y, dir;
2361 XEvent xev;
2362 Display *display = thread_display();
2363 struct x11drv_win_data *data;
2365 if (!(data = X11DRV_get_win_data( hwnd ))) return -1;
2366 if (!data->whole_window || !data->managed || !data->mapped) return -1;
2368 switch (wparam & 0xfff0)
2370 case SC_MOVE:
2371 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
2372 else dir = _NET_WM_MOVERESIZE_MOVE;
2373 break;
2374 case SC_SIZE:
2375 /* windows without WS_THICKFRAME are not resizable through the window manager */
2376 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_THICKFRAME)) return -1;
2378 switch (hittest)
2380 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
2381 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
2382 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
2383 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
2384 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
2385 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
2386 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
2387 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
2388 default: dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD; break;
2390 break;
2392 case SC_KEYMENU:
2393 /* prevent a simple ALT press+release from activating the system menu,
2394 * as that can get confusing on managed windows */
2395 if ((WCHAR)lparam) return -1; /* got an explicit char */
2396 if (GetMenu( hwnd )) return -1; /* window has a real menu */
2397 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) return -1; /* no system menu */
2398 TRACE( "ignoring SC_KEYMENU wp %lx lp %lx\n", wparam, lparam );
2399 return 0;
2401 default:
2402 return -1;
2405 if (IsZoomed(hwnd)) return -1;
2407 if (!is_netwm_supported( display, x11drv_atom(_NET_WM_MOVERESIZE) ))
2409 TRACE( "_NET_WM_MOVERESIZE not supported\n" );
2410 return -1;
2413 dwPoint = GetMessagePos();
2414 x = (short)LOWORD(dwPoint);
2415 y = (short)HIWORD(dwPoint);
2417 TRACE("hwnd %p, x %d, y %d, dir %d\n", hwnd, x, y, dir);
2419 xev.xclient.type = ClientMessage;
2420 xev.xclient.window = X11DRV_get_whole_window(hwnd);
2421 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
2422 xev.xclient.serial = 0;
2423 xev.xclient.display = display;
2424 xev.xclient.send_event = True;
2425 xev.xclient.format = 32;
2426 xev.xclient.data.l[0] = x - virtual_screen_rect.left; /* x coord */
2427 xev.xclient.data.l[1] = y - virtual_screen_rect.top; /* y coord */
2428 xev.xclient.data.l[2] = dir; /* direction */
2429 xev.xclient.data.l[3] = 1; /* button */
2430 xev.xclient.data.l[4] = 0; /* unused */
2432 /* need to ungrab the pointer that may have been automatically grabbed
2433 * with a ButtonPress event */
2434 wine_tsx11_lock();
2435 XUngrabPointer( display, CurrentTime );
2436 XSendEvent(display, root_window, False, SubstructureNotifyMask, &xev);
2437 wine_tsx11_unlock();
2438 return 0;