winex11: Don't create a win data structure for HWND_MESSAGE windows.
[wine/multimedia.git] / dlls / winex11.drv / window.c
blob0b416d9cdff0126a7c8531e0fa7e5859fa644075
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";
85 /***********************************************************************
86 * is_window_managed
88 * Check if a given window should be managed
90 static BOOL is_window_managed( HWND hwnd, UINT swp_flags, const RECT *window_rect )
92 DWORD style, ex_style;
94 if (!managed_mode) return FALSE;
96 /* child windows are not managed */
97 style = GetWindowLongW( hwnd, GWL_STYLE );
98 if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD) return FALSE;
99 /* activated windows are managed */
100 if (!(swp_flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) return TRUE;
101 if (hwnd == GetActiveWindow()) return TRUE;
102 /* windows with caption are managed */
103 if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
104 /* windows with thick frame are managed */
105 if (style & WS_THICKFRAME) return TRUE;
106 if (style & WS_POPUP)
108 /* popup with sysmenu == caption are managed */
109 if (style & WS_SYSMENU) return TRUE;
110 /* full-screen popup windows are managed */
111 if (window_rect->left <= 0 && window_rect->right >= screen_width &&
112 window_rect->top <= 0 && window_rect->bottom >= screen_height)
113 return TRUE;
115 /* application windows are managed */
116 ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
117 if (ex_style & WS_EX_APPWINDOW) return TRUE;
118 /* default: not managed */
119 return FALSE;
123 /***********************************************************************
124 * X11DRV_is_window_rect_mapped
126 * Check if the X whole window should be mapped based on its rectangle
128 static BOOL is_window_rect_mapped( const RECT *rect )
130 /* don't map if rect is empty */
131 if (IsRectEmpty( rect )) return FALSE;
133 /* don't map if rect is off-screen */
134 if (rect->left >= virtual_screen_rect.right ||
135 rect->top >= virtual_screen_rect.bottom ||
136 rect->right <= virtual_screen_rect.left ||
137 rect->bottom <= virtual_screen_rect.top)
138 return FALSE;
140 return TRUE;
144 /***********************************************************************
145 * is_window_resizable
147 * Check if window should be made resizable by the window manager
149 static inline BOOL is_window_resizable( struct x11drv_win_data *data, DWORD style )
151 if (style & WS_THICKFRAME) return TRUE;
152 /* Metacity needs the window to be resizable to make it fullscreen */
153 return (data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
154 data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height);
158 /***********************************************************************
159 * get_window_owner
161 static HWND get_window_owner( HWND hwnd )
163 RECT rect;
164 HWND owner = GetWindow( hwnd, GW_OWNER );
165 /* ignore the zero-size owners used by Delphi apps */
166 if (owner && GetWindowRect( owner, &rect ) && IsRectEmpty( &rect )) owner = 0;
167 return owner;
171 /***********************************************************************
172 * get_mwm_decorations
174 static unsigned long get_mwm_decorations( struct x11drv_win_data *data,
175 DWORD style, DWORD ex_style )
177 unsigned long ret = 0;
179 if (!decorated_mode) return 0;
181 if (IsRectEmpty( &data->window_rect )) return 0;
182 if (data->shaped) return 0;
184 if (ex_style & WS_EX_TOOLWINDOW) return 0;
186 if ((style & WS_CAPTION) == WS_CAPTION)
188 ret |= MWM_DECOR_TITLE | MWM_DECOR_BORDER;
189 if (style & WS_SYSMENU) ret |= MWM_DECOR_MENU;
190 if (style & WS_MINIMIZEBOX) ret |= MWM_DECOR_MINIMIZE;
191 if (style & WS_MAXIMIZEBOX) ret |= MWM_DECOR_MAXIMIZE;
193 if (ex_style & WS_EX_DLGMODALFRAME) ret |= MWM_DECOR_BORDER;
194 else if (style & WS_THICKFRAME) ret |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
195 else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) ret |= MWM_DECOR_BORDER;
196 return ret;
200 /***********************************************************************
201 * get_x11_rect_offset
203 * Helper for X11DRV_window_to_X_rect and X11DRV_X_to_window_rect.
205 static void get_x11_rect_offset( struct x11drv_win_data *data, RECT *rect )
207 DWORD style, ex_style, style_mask = 0, ex_style_mask = 0;
208 unsigned long decor;
210 rect->top = rect->bottom = rect->left = rect->right = 0;
212 style = GetWindowLongW( data->hwnd, GWL_STYLE );
213 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
214 decor = get_mwm_decorations( data, style, ex_style );
216 if (decor & MWM_DECOR_TITLE) style_mask |= WS_CAPTION;
217 if (decor & MWM_DECOR_BORDER)
219 style_mask |= WS_DLGFRAME | WS_THICKFRAME;
220 ex_style_mask |= WS_EX_DLGMODALFRAME;
223 AdjustWindowRectEx( rect, style & style_mask, FALSE, ex_style & ex_style_mask );
227 /***********************************************************************
228 * get_window_attributes
230 * Fill the window attributes structure for an X window.
232 static int get_window_attributes( Display *display, struct x11drv_win_data *data,
233 XSetWindowAttributes *attr )
235 attr->override_redirect = !data->managed;
236 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
237 attr->save_under = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
238 attr->cursor = x11drv_thread_data()->cursor;
239 attr->bit_gravity = NorthWestGravity;
240 attr->win_gravity = StaticGravity;
241 attr->backing_store = NotUseful;
242 attr->event_mask = (ExposureMask | PointerMotionMask |
243 ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
244 KeyPressMask | KeyReleaseMask | FocusChangeMask | KeymapStateMask);
245 if (data->managed) attr->event_mask |= StructureNotifyMask | PropertyChangeMask;
247 return (CWOverrideRedirect | CWSaveUnder | CWColormap | CWCursor |
248 CWEventMask | CWBitGravity | CWBackingStore);
252 /***********************************************************************
253 * create_client_window
255 static Window create_client_window( Display *display, struct x11drv_win_data *data, XVisualInfo *vis )
257 int cx, cy, mask;
258 XSetWindowAttributes attr;
259 Window client;
260 Visual *client_visual = vis ? vis->visual : visual;
262 attr.bit_gravity = NorthWestGravity;
263 attr.win_gravity = NorthWestGravity;
264 attr.backing_store = NotUseful;
265 attr.event_mask = (ExposureMask | PointerMotionMask |
266 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
267 mask = CWEventMask | CWBitGravity | CWWinGravity | CWBackingStore;
269 if ((cx = data->client_rect.right - data->client_rect.left) <= 0) cx = 1;
270 if ((cy = data->client_rect.bottom - data->client_rect.top) <= 0) cy = 1;
272 wine_tsx11_lock();
274 if (vis)
276 attr.colormap = XCreateColormap( display, root_window, vis->visual,
277 (vis->class == PseudoColor || vis->class == GrayScale ||
278 vis->class == DirectColor) ? AllocAll : AllocNone );
279 mask |= CWColormap;
282 client = XCreateWindow( display, data->whole_window,
283 data->client_rect.left - data->whole_rect.left,
284 data->client_rect.top - data->whole_rect.top,
285 cx, cy, 0, screen_depth, InputOutput,
286 client_visual, mask, &attr );
287 if (!client)
289 wine_tsx11_unlock();
290 return 0;
293 if (data->client_window)
295 struct x11drv_thread_data *thread_data = x11drv_thread_data();
296 if (thread_data->cursor_window == data->client_window) thread_data->cursor_window = None;
297 XDeleteContext( display, data->client_window, winContext );
298 XDestroyWindow( display, data->client_window );
300 data->client_window = client;
301 data->visualid = XVisualIDFromVisual( client_visual );
303 if (data->colormap) XFreeColormap( display, data->colormap );
304 data->colormap = vis ? attr.colormap : 0;
306 XMapWindow( display, data->client_window );
307 XSaveContext( display, data->client_window, winContext, (char *)data->hwnd );
308 wine_tsx11_unlock();
310 SetPropA( data->hwnd, client_window_prop, (HANDLE)data->client_window );
311 return data->client_window;
315 /***********************************************************************
316 * sync_window_style
318 * Change the X window attributes when the window style has changed.
320 static void sync_window_style( Display *display, struct x11drv_win_data *data )
322 if (data->whole_window != root_window)
324 XSetWindowAttributes attr;
325 int mask = get_window_attributes( display, data, &attr );
327 wine_tsx11_lock();
328 XChangeWindowAttributes( display, data->whole_window, mask, &attr );
329 wine_tsx11_unlock();
334 /***********************************************************************
335 * sync_window_region
337 * Update the X11 window region.
339 static void sync_window_region( Display *display, struct x11drv_win_data *data, HRGN hrgn )
341 #ifdef HAVE_LIBXSHAPE
342 if (!data->whole_window) return;
343 data->shaped = FALSE;
345 if (!hrgn)
347 wine_tsx11_lock();
348 XShapeCombineMask( display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
349 wine_tsx11_unlock();
351 else
353 RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
354 if (pRegionData)
356 wine_tsx11_lock();
357 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
358 data->window_rect.left - data->whole_rect.left,
359 data->window_rect.top - data->whole_rect.top,
360 (XRectangle *)pRegionData->Buffer,
361 pRegionData->rdh.nCount, ShapeSet, YXBanded );
362 wine_tsx11_unlock();
363 HeapFree(GetProcessHeap(), 0, pRegionData);
364 data->shaped = TRUE;
367 #endif /* HAVE_LIBXSHAPE */
371 /***********************************************************************
372 * sync_window_text
374 static void sync_window_text( Display *display, Window win, const WCHAR *text )
376 UINT count;
377 char *buffer, *utf8_buffer;
378 XTextProperty prop;
380 /* allocate new buffer for window text */
381 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
382 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count ))) return;
383 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
385 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
386 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
388 HeapFree( GetProcessHeap(), 0, buffer );
389 return;
391 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
393 wine_tsx11_lock();
394 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
396 XSetWMName( display, win, &prop );
397 XSetWMIconName( display, win, &prop );
398 XFree( prop.value );
401 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
402 according to the standard
403 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
405 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
406 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
407 wine_tsx11_unlock();
409 HeapFree( GetProcessHeap(), 0, utf8_buffer );
410 HeapFree( GetProcessHeap(), 0, buffer );
414 /***********************************************************************
415 * X11DRV_set_win_format
417 BOOL X11DRV_set_win_format( HWND hwnd, XID fbconfig_id )
419 struct x11drv_win_data *data;
420 XVisualInfo *vis;
421 int w, h;
423 if (!(data = X11DRV_get_win_data(hwnd)) &&
424 !(data = X11DRV_create_win_data(hwnd))) return FALSE;
426 if (!(vis = visual_from_fbconfig_id(fbconfig_id))) return FALSE;
428 if (data->whole_window)
430 Display *display = thread_display();
431 Window client = data->client_window;
433 if (vis->visualid != data->visualid)
435 client = create_client_window( display, data, vis );
436 TRACE( "re-created client window %lx for %p fbconfig %lx\n", client, data->hwnd, fbconfig_id );
438 wine_tsx11_lock();
439 XFree(vis);
440 XFlush( display );
441 wine_tsx11_unlock();
442 if (client) goto done;
443 return FALSE;
446 w = data->client_rect.right - data->client_rect.left;
447 h = data->client_rect.bottom - data->client_rect.top;
449 if(w <= 0) w = 1;
450 if(h <= 0) h = 1;
452 #ifdef SONAME_LIBXCOMPOSITE
453 if(usexcomposite)
455 XSetWindowAttributes attrib;
456 static Window dummy_parent;
458 wine_tsx11_lock();
459 attrib.override_redirect = True;
460 if (!dummy_parent)
462 dummy_parent = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, screen_depth,
463 InputOutput, visual, CWOverrideRedirect, &attrib );
464 XMapWindow( gdi_display, dummy_parent );
466 data->colormap = XCreateColormap(gdi_display, dummy_parent, vis->visual,
467 (vis->class == PseudoColor ||
468 vis->class == GrayScale ||
469 vis->class == DirectColor) ?
470 AllocAll : AllocNone);
471 attrib.colormap = data->colormap;
472 XInstallColormap(gdi_display, attrib.colormap);
474 if(data->gl_drawable) XDestroyWindow(gdi_display, data->gl_drawable);
475 data->gl_drawable = XCreateWindow(gdi_display, dummy_parent, -w, 0, w, h, 0,
476 vis->depth, InputOutput, vis->visual,
477 CWColormap | CWOverrideRedirect,
478 &attrib);
479 if(data->gl_drawable)
481 pXCompositeRedirectWindow(gdi_display, data->gl_drawable,
482 CompositeRedirectManual);
483 XMapWindow(gdi_display, data->gl_drawable);
485 XFree(vis);
486 XFlush( gdi_display );
487 wine_tsx11_unlock();
489 else
490 #endif
492 WARN("XComposite is not available, using GLXPixmap hack\n");
494 wine_tsx11_lock();
496 if(data->pixmap) XFreePixmap(gdi_display, data->pixmap);
497 data->pixmap = XCreatePixmap(gdi_display, root_window, w, h, vis->depth);
498 if(!data->pixmap)
500 XFree(vis);
501 wine_tsx11_unlock();
502 return FALSE;
505 if(data->gl_drawable) destroy_glxpixmap(gdi_display, data->gl_drawable);
506 data->gl_drawable = create_glxpixmap(gdi_display, vis, data->pixmap);
507 if(!data->gl_drawable)
509 XFreePixmap(gdi_display, data->pixmap);
510 data->pixmap = 0;
512 XFree(vis);
513 XFlush( gdi_display );
514 wine_tsx11_unlock();
515 if (data->pixmap) SetPropA(hwnd, pixmap_prop, (HANDLE)data->pixmap);
518 if (!data->gl_drawable) return FALSE;
520 TRACE("Created GL drawable 0x%lx, using FBConfigID 0x%lx\n",
521 data->gl_drawable, fbconfig_id);
522 SetPropA(hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
524 done:
525 data->fbconfig_id = fbconfig_id;
526 SetPropA(hwnd, fbconfig_id_prop, (HANDLE)data->fbconfig_id);
527 /* force DCE invalidation */
528 SetWindowPos( hwnd, 0, 0, 0, 0, 0,
529 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE |
530 SWP_NOREDRAW | SWP_DEFERERASE | SWP_NOSENDCHANGING | SWP_STATECHANGED);
531 return TRUE;
534 /***********************************************************************
535 * sync_gl_drawable
537 static void sync_gl_drawable(struct x11drv_win_data *data)
539 int w = data->client_rect.right - data->client_rect.left;
540 int h = data->client_rect.bottom - data->client_rect.top;
541 XVisualInfo *vis;
542 Drawable glxp;
543 Pixmap pix;
545 if (w <= 0) w = 1;
546 if (h <= 0) h = 1;
548 TRACE("Resizing GL drawable 0x%lx to %dx%d\n", data->gl_drawable, w, h);
549 #ifdef SONAME_LIBXCOMPOSITE
550 if(usexcomposite)
552 wine_tsx11_lock();
553 XMoveResizeWindow(gdi_display, data->gl_drawable, -w, 0, w, h);
554 wine_tsx11_unlock();
555 return;
557 #endif
559 if (!(vis = visual_from_fbconfig_id(data->fbconfig_id))) return;
561 wine_tsx11_lock();
562 pix = XCreatePixmap(gdi_display, root_window, w, h, vis->depth);
563 if(!pix)
565 ERR("Failed to create pixmap for offscreen rendering\n");
566 XFree(vis);
567 wine_tsx11_unlock();
568 return;
571 glxp = create_glxpixmap(gdi_display, vis, pix);
572 if(!glxp)
574 ERR("Failed to create drawable for offscreen rendering\n");
575 XFreePixmap(gdi_display, pix);
576 XFree(vis);
577 wine_tsx11_unlock();
578 return;
581 XFree(vis);
583 mark_drawable_dirty(data->gl_drawable, glxp);
585 XFreePixmap(gdi_display, data->pixmap);
586 destroy_glxpixmap(gdi_display, data->gl_drawable);
587 TRACE( "Recreated GL drawable %lx to replace %lx\n", glxp, data->gl_drawable );
589 data->pixmap = pix;
590 data->gl_drawable = glxp;
592 XFlush( gdi_display );
593 wine_tsx11_unlock();
595 SetPropA(data->hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
596 SetPropA(data->hwnd, pixmap_prop, (HANDLE)data->pixmap);
600 /***********************************************************************
601 * get_window_changes
603 * fill the window changes structure
605 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
607 int mask = 0;
609 if (old->right - old->left != new->right - new->left )
611 if ((changes->width = new->right - new->left) <= 0) changes->width = 1;
612 mask |= CWWidth;
614 if (old->bottom - old->top != new->bottom - new->top)
616 if ((changes->height = new->bottom - new->top) <= 0) changes->height = 1;
617 mask |= CWHeight;
619 if (old->left != new->left)
621 changes->x = new->left;
622 mask |= CWX;
624 if (old->top != new->top)
626 changes->y = new->top;
627 mask |= CWY;
629 return mask;
633 /***********************************************************************
634 * create_icon_window
636 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
638 XSetWindowAttributes attr;
640 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
641 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
642 attr.bit_gravity = NorthWestGravity;
643 attr.backing_store = NotUseful/*WhenMapped*/;
644 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
646 wine_tsx11_lock();
647 data->icon_window = XCreateWindow( display, root_window, 0, 0,
648 GetSystemMetrics( SM_CXICON ),
649 GetSystemMetrics( SM_CYICON ),
650 0, screen_depth,
651 InputOutput, visual,
652 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
653 XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
654 XFlush( display ); /* make sure the window exists before we start painting to it */
655 wine_tsx11_unlock();
657 TRACE( "created %lx\n", data->icon_window );
658 SetPropA( data->hwnd, icon_window_prop, (HANDLE)data->icon_window );
659 return data->icon_window;
664 /***********************************************************************
665 * destroy_icon_window
667 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
669 if (!data->icon_window) return;
670 if (x11drv_thread_data()->cursor_window == data->icon_window)
671 x11drv_thread_data()->cursor_window = None;
672 wine_tsx11_lock();
673 XDeleteContext( display, data->icon_window, winContext );
674 XDestroyWindow( display, data->icon_window );
675 data->icon_window = 0;
676 wine_tsx11_unlock();
677 RemovePropA( data->hwnd, icon_window_prop );
681 /***********************************************************************
682 * set_icon_hints
684 * Set the icon wm hints
686 static void set_icon_hints( Display *display, struct x11drv_win_data *data, HICON hIcon )
688 XWMHints *hints = data->wm_hints;
690 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
691 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
692 data->hWMIconBitmap = 0;
693 data->hWMIconMask = 0;
695 if (!hIcon)
697 if (!data->icon_window) create_icon_window( display, data );
698 hints->icon_window = data->icon_window;
699 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
701 else
703 HBITMAP hbmOrig;
704 RECT rcMask;
705 BITMAP bmMask;
706 ICONINFO ii;
707 HDC hDC;
709 GetIconInfo(hIcon, &ii);
711 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
712 rcMask.top = 0;
713 rcMask.left = 0;
714 rcMask.right = bmMask.bmWidth;
715 rcMask.bottom = bmMask.bmHeight;
717 hDC = CreateCompatibleDC(0);
718 hbmOrig = SelectObject(hDC, ii.hbmMask);
719 InvertRect(hDC, &rcMask);
720 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
721 SelectObject(hDC, hbmOrig);
722 DeleteDC(hDC);
724 data->hWMIconBitmap = ii.hbmColor;
725 data->hWMIconMask = ii.hbmMask;
727 hints->icon_pixmap = X11DRV_get_pixmap(data->hWMIconBitmap);
728 hints->icon_mask = X11DRV_get_pixmap(data->hWMIconMask);
729 destroy_icon_window( display, data );
730 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
735 /***********************************************************************
736 * set_size_hints
738 * set the window size hints
740 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
742 XSizeHints* size_hints;
744 if (!(size_hints = XAllocSizeHints())) return;
746 size_hints->win_gravity = StaticGravity;
747 size_hints->flags |= PWinGravity;
749 /* don't update size hints if window is not in normal state */
750 if (!(style & (WS_MINIMIZE | WS_MAXIMIZE)))
752 if (data->hwnd != GetDesktopWindow()) /* don't force position of desktop */
754 size_hints->x = data->whole_rect.left;
755 size_hints->y = data->whole_rect.top;
756 size_hints->flags |= PPosition;
759 if (!is_window_resizable( data, style ))
761 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
762 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
763 size_hints->min_width = size_hints->max_width;
764 size_hints->min_height = size_hints->max_height;
765 size_hints->flags |= PMinSize | PMaxSize;
768 XSetWMNormalHints( display, data->whole_window, size_hints );
769 XFree( size_hints );
773 /***********************************************************************
774 * get_process_name
776 * get the name of the current process for setting class hints
778 static char *get_process_name(void)
780 static char *name;
782 if (!name)
784 WCHAR module[MAX_PATH];
785 DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
786 if (len && len < MAX_PATH)
788 char *ptr;
789 WCHAR *p, *appname = module;
791 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
792 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
793 len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
794 if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
796 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
797 name = ptr;
801 return name;
805 /***********************************************************************
806 * set_initial_wm_hints
808 * Set the window manager hints that don't change over the lifetime of a window.
810 static void set_initial_wm_hints( Display *display, struct x11drv_win_data *data )
812 long i;
813 Atom protocols[3];
814 Atom dndVersion = 4;
815 XClassHint *class_hints;
816 char *process_name = get_process_name();
818 wine_tsx11_lock();
820 /* wm protocols */
821 i = 0;
822 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
823 protocols[i++] = x11drv_atom(_NET_WM_PING);
824 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
825 XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
826 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
828 /* class hints */
829 if ((class_hints = XAllocClassHint()))
831 static char wine[] = "Wine";
833 class_hints->res_name = process_name;
834 class_hints->res_class = wine;
835 XSetClassHint( display, data->whole_window, class_hints );
836 XFree( class_hints );
839 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
840 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
841 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
842 i = getpid();
843 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
844 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
846 XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
847 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
849 data->wm_hints = XAllocWMHints();
850 wine_tsx11_unlock();
852 if (data->wm_hints)
854 HICON icon = (HICON)SendMessageW( data->hwnd, WM_GETICON, ICON_BIG, 0 );
855 if (!icon) icon = (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON );
856 data->wm_hints->flags = 0;
857 set_icon_hints( display, data, icon );
862 /***********************************************************************
863 * set_wm_hints
865 * Set the window manager hints for a newly-created window
867 static void set_wm_hints( Display *display, struct x11drv_win_data *data )
869 Window group_leader;
870 Atom window_type;
871 MwmHints mwm_hints;
872 DWORD style, ex_style;
873 HWND owner;
875 if (data->hwnd == GetDesktopWindow())
877 /* force some styles for the desktop to get the correct decorations */
878 style = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
879 ex_style = WS_EX_APPWINDOW;
880 owner = 0;
882 else
884 style = GetWindowLongW( data->hwnd, GWL_STYLE );
885 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
886 owner = get_window_owner( data->hwnd );
889 /* transient for hint */
890 if (owner)
892 Window owner_win = X11DRV_get_whole_window( owner );
893 wine_tsx11_lock();
894 XSetTransientForHint( display, data->whole_window, owner_win );
895 wine_tsx11_unlock();
896 group_leader = owner_win;
898 else group_leader = data->whole_window;
900 wine_tsx11_lock();
902 /* size hints */
903 set_size_hints( display, data, style );
905 /* set the WM_WINDOW_TYPE */
906 if (style & WS_THICKFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
907 else if (ex_style & WS_EX_APPWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
908 else if (style & WS_DLGFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
909 else if (ex_style & WS_EX_DLGMODALFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
910 else if ((style & WS_POPUP) && owner) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
911 #if 0 /* many window managers don't handle utility windows very well */
912 else if (ex_style & WS_EX_TOOLWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
913 #endif
914 else window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
916 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
917 XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
919 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
920 mwm_hints.decorations = get_mwm_decorations( data, style, ex_style );
921 mwm_hints.functions = MWM_FUNC_MOVE;
922 if (is_window_resizable( data, style )) mwm_hints.functions |= MWM_FUNC_RESIZE;
923 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
924 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
925 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
927 XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
928 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
929 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
931 /* wm hints */
932 if (data->wm_hints)
934 data->wm_hints->flags |= InputHint | StateHint | WindowGroupHint;
935 data->wm_hints->input = !(style & WS_DISABLED);
936 data->wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
937 data->wm_hints->window_group = group_leader;
938 XSetWMHints( display, data->whole_window, data->wm_hints );
941 wine_tsx11_unlock();
945 /***********************************************************************
946 * update_net_wm_states
948 void update_net_wm_states( Display *display, struct x11drv_win_data *data )
950 static const unsigned int state_atoms[NB_NET_WM_STATES] =
952 XATOM__NET_WM_STATE_FULLSCREEN,
953 XATOM__NET_WM_STATE_ABOVE,
954 XATOM__NET_WM_STATE_MAXIMIZED_VERT,
955 XATOM__NET_WM_STATE_SKIP_PAGER,
956 XATOM__NET_WM_STATE_SKIP_TASKBAR
959 DWORD i, style, ex_style, new_state = 0;
961 if (!data->managed) return;
962 if (data->whole_window == root_window) return;
964 style = GetWindowLongW( data->hwnd, GWL_STYLE );
965 if (data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
966 data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height)
968 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION)
969 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
970 else if (!(style & WS_MINIMIZE))
971 new_state |= (1 << NET_WM_STATE_FULLSCREEN);
973 else if (style & WS_MAXIMIZE)
974 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
976 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
977 if (ex_style & WS_EX_TOPMOST)
978 new_state |= (1 << NET_WM_STATE_ABOVE);
979 if (ex_style & WS_EX_TOOLWINDOW)
980 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR) | (1 << NET_WM_STATE_SKIP_PAGER);
981 if (!(ex_style & WS_EX_APPWINDOW) && get_window_owner( data->hwnd ))
982 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR);
984 if (!data->mapped) /* set the _NET_WM_STATE atom directly */
986 Atom atoms[NB_NET_WM_STATES+1];
987 DWORD count;
989 for (i = count = 0; i < NB_NET_WM_STATES; i++)
991 if (!(new_state & (1 << i))) continue;
992 TRACE( "setting wm state %u for unmapped window %p/%lx\n",
993 i, data->hwnd, data->whole_window );
994 atoms[count++] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
995 if (state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT)
996 atoms[count++] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
998 wine_tsx11_lock();
999 XChangeProperty( display, data->whole_window, x11drv_atom(_NET_WM_STATE), XA_ATOM,
1000 32, PropModeReplace, (unsigned char *)atoms, count );
1001 wine_tsx11_unlock();
1003 else /* ask the window manager to do it for us */
1005 XEvent xev;
1007 xev.xclient.type = ClientMessage;
1008 xev.xclient.window = data->whole_window;
1009 xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
1010 xev.xclient.serial = 0;
1011 xev.xclient.display = display;
1012 xev.xclient.send_event = True;
1013 xev.xclient.format = 32;
1014 xev.xclient.data.l[3] = 1;
1016 for (i = 0; i < NB_NET_WM_STATES; i++)
1018 if (!((data->net_wm_state ^ new_state) & (1 << i))) continue; /* unchanged */
1020 TRACE( "setting wm state %u for window %p/%lx to %u prev %u\n",
1021 i, data->hwnd, data->whole_window,
1022 (new_state & (1 << i)) != 0, (data->net_wm_state & (1 << i)) != 0 );
1024 xev.xclient.data.l[0] = (new_state & (1 << i)) ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1025 xev.xclient.data.l[1] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1026 xev.xclient.data.l[2] = ((state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT) ?
1027 x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ) : 0);
1028 wine_tsx11_lock();
1029 XSendEvent( display, root_window, False,
1030 SubstructureRedirectMask | SubstructureNotifyMask, &xev );
1031 wine_tsx11_unlock();
1034 data->net_wm_state = new_state;
1038 /***********************************************************************
1039 * set_xembed_flags
1041 static void set_xembed_flags( Display *display, struct x11drv_win_data *data, unsigned long flags )
1043 unsigned long info[2];
1045 info[0] = 0; /* protocol version */
1046 info[1] = flags;
1047 wine_tsx11_lock();
1048 XChangeProperty( display, data->whole_window, x11drv_atom(_XEMBED_INFO),
1049 x11drv_atom(_XEMBED_INFO), 32, PropModeReplace, (unsigned char*)info, 2 );
1050 wine_tsx11_unlock();
1054 /***********************************************************************
1055 * map_window
1057 static void map_window( Display *display, struct x11drv_win_data *data, DWORD new_style )
1059 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1061 wait_for_withdrawn_state( display, data, TRUE );
1063 if (!data->embedded)
1065 update_net_wm_states( display, data );
1066 sync_window_style( display, data );
1067 wine_tsx11_lock();
1068 XMapWindow( display, data->whole_window );
1069 wine_tsx11_unlock();
1071 else set_xembed_flags( display, data, XEMBED_MAPPED );
1073 data->mapped = TRUE;
1074 data->iconic = (new_style & WS_MINIMIZE) != 0;
1078 /***********************************************************************
1079 * unmap_window
1081 static void unmap_window( Display *display, struct x11drv_win_data *data )
1083 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1085 if (!data->embedded)
1087 wait_for_withdrawn_state( display, data, FALSE );
1088 wine_tsx11_lock();
1089 if (data->managed) XWithdrawWindow( display, data->whole_window, DefaultScreen(display) );
1090 else XUnmapWindow( display, data->whole_window );
1091 wine_tsx11_unlock();
1093 else set_xembed_flags( display, data, 0 );
1095 data->mapped = FALSE;
1096 data->net_wm_state = 0;
1100 /***********************************************************************
1101 * make_window_embedded
1103 void make_window_embedded( Display *display, struct x11drv_win_data *data )
1105 if (data->mapped)
1107 /* the window cannot be mapped before being embedded */
1108 unmap_window( display, data );
1109 data->embedded = TRUE;
1110 map_window( display, data, 0 );
1112 else
1114 data->embedded = TRUE;
1115 set_xembed_flags( display, data, 0 );
1120 /***********************************************************************
1121 * X11DRV_window_to_X_rect
1123 * Convert a rect from client to X window coordinates
1125 void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
1127 RECT rc;
1129 if (!data->managed) return;
1130 if (IsRectEmpty( rect )) return;
1132 get_x11_rect_offset( data, &rc );
1134 rect->left -= rc.left;
1135 rect->right -= rc.right;
1136 rect->top -= rc.top;
1137 rect->bottom -= rc.bottom;
1138 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1139 if (rect->left >= rect->right) rect->right = rect->left + 1;
1143 /***********************************************************************
1144 * X11DRV_X_to_window_rect
1146 * Opposite of X11DRV_window_to_X_rect
1148 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
1150 RECT rc;
1152 if (!data->managed) return;
1153 if (IsRectEmpty( rect )) return;
1155 get_x11_rect_offset( data, &rc );
1157 rect->left += rc.left;
1158 rect->right += rc.right;
1159 rect->top += rc.top;
1160 rect->bottom += rc.bottom;
1161 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1162 if (rect->left >= rect->right) rect->right = rect->left + 1;
1166 /***********************************************************************
1167 * sync_window_position
1169 * Synchronize the X window position with the Windows one
1171 static void sync_window_position( Display *display, struct x11drv_win_data *data,
1172 UINT swp_flags, const RECT *old_client_rect,
1173 const RECT *old_whole_rect )
1175 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
1176 XWindowChanges changes;
1177 unsigned int mask = 0;
1179 if (data->managed && data->iconic) return;
1181 /* resizing a managed maximized window is not allowed */
1182 if (!(style & WS_MAXIMIZE) || !data->managed)
1184 if ((changes.width = data->whole_rect.right - data->whole_rect.left) <= 0) changes.width = 1;
1185 if ((changes.height = data->whole_rect.bottom - data->whole_rect.top) <= 0) changes.height = 1;
1186 mask |= CWWidth | CWHeight;
1189 /* only the size is allowed to change for the desktop window */
1190 if (data->whole_window != root_window)
1192 changes.x = data->whole_rect.left - virtual_screen_rect.left;
1193 changes.y = data->whole_rect.top - virtual_screen_rect.top;
1194 mask |= CWX | CWY;
1197 if (!(swp_flags & SWP_NOZORDER))
1199 /* find window that this one must be after */
1200 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
1201 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
1202 prev = GetWindow( prev, GW_HWNDPREV );
1203 if (!prev) /* top child */
1205 changes.stack_mode = Above;
1206 mask |= CWStackMode;
1208 /* should use stack_mode Below but most window managers don't get it right */
1209 /* and Above with a sibling doesn't work so well either, so we ignore it */
1212 TRACE( "setting win %p/%lx pos %d,%d,%dx%d after %lx changes=%x\n",
1213 data->hwnd, data->whole_window, data->whole_rect.left, data->whole_rect.top,
1214 data->whole_rect.right - data->whole_rect.left,
1215 data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
1217 wine_tsx11_lock();
1218 set_size_hints( display, data, style );
1219 XReconfigureWMWindow( display, data->whole_window,
1220 DefaultScreen(display), mask, &changes );
1221 wine_tsx11_unlock();
1225 /***********************************************************************
1226 * sync_client_position
1228 * Synchronize the X client window position with the Windows one
1230 static void sync_client_position( Display *display, struct x11drv_win_data *data,
1231 UINT swp_flags, const RECT *old_client_rect,
1232 const RECT *old_whole_rect )
1234 int mask;
1235 XWindowChanges changes;
1236 RECT old = *old_client_rect;
1237 RECT new = data->client_rect;
1239 OffsetRect( &old, -old_whole_rect->left, -old_whole_rect->top );
1240 OffsetRect( &new, -data->whole_rect.left, -data->whole_rect.top );
1241 if (!(mask = get_window_changes( &changes, &old, &new ))) return;
1243 if (data->client_window)
1245 TRACE( "setting client win %lx pos %d,%d,%dx%d changes=%x\n",
1246 data->client_window, new.left, new.top,
1247 new.right - new.left, new.bottom - new.top, mask );
1248 wine_tsx11_lock();
1249 XConfigureWindow( display, data->client_window, mask, &changes );
1250 wine_tsx11_unlock();
1253 if (data->gl_drawable && (mask & (CWWidth|CWHeight))) sync_gl_drawable( data );
1257 /***********************************************************************
1258 * move_window_bits
1260 * Move the window bits when a window is moved.
1262 static void move_window_bits( struct x11drv_win_data *data, const RECT *old_rect, const RECT *new_rect,
1263 const RECT *old_client_rect )
1265 RECT src_rect = *old_rect;
1266 RECT dst_rect = *new_rect;
1267 HDC hdc_src, hdc_dst;
1268 INT code;
1269 HRGN rgn = 0;
1270 HWND parent = 0;
1272 if (!data->whole_window)
1274 OffsetRect( &dst_rect, -data->window_rect.left, -data->window_rect.top );
1275 parent = GetAncestor( data->hwnd, GA_PARENT );
1276 hdc_src = GetDCEx( parent, 0, DCX_CACHE );
1277 hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE | DCX_WINDOW );
1279 else
1281 OffsetRect( &dst_rect, -data->client_rect.left, -data->client_rect.top );
1282 /* make src rect relative to the old position of the window */
1283 OffsetRect( &src_rect, -old_client_rect->left, -old_client_rect->top );
1284 if (dst_rect.left == src_rect.left && dst_rect.top == src_rect.top) return;
1285 hdc_src = hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE );
1288 code = X11DRV_START_EXPOSURES;
1289 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
1291 TRACE( "copying bits for win %p/%lx/%lx %s -> %s\n",
1292 data->hwnd, data->whole_window, data->client_window,
1293 wine_dbgstr_rect(&src_rect), wine_dbgstr_rect(&dst_rect) );
1294 BitBlt( hdc_dst, dst_rect.left, dst_rect.top,
1295 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top,
1296 hdc_src, src_rect.left, src_rect.top, SRCCOPY );
1298 code = X11DRV_END_EXPOSURES;
1299 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, sizeof(rgn), (LPSTR)&rgn );
1301 ReleaseDC( data->hwnd, hdc_dst );
1302 if (hdc_src != hdc_dst) ReleaseDC( parent, hdc_src );
1304 if (rgn)
1306 if (!data->whole_window)
1308 /* map region to client rect since we are using DCX_WINDOW */
1309 OffsetRgn( rgn, data->window_rect.left - data->client_rect.left,
1310 data->window_rect.top - data->client_rect.top );
1311 RedrawWindow( data->hwnd, NULL, rgn,
1312 RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ALLCHILDREN );
1314 else RedrawWindow( data->hwnd, NULL, rgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1315 DeleteObject( rgn );
1320 /**********************************************************************
1321 * create_whole_window
1323 * Create the whole X window for a given window
1325 static Window create_whole_window( Display *display, struct x11drv_win_data *data )
1327 int cx, cy, mask;
1328 XSetWindowAttributes attr;
1329 WCHAR text[1024];
1330 HRGN hrgn;
1332 if (!(cx = data->window_rect.right - data->window_rect.left)) cx = 1;
1333 if (!(cy = data->window_rect.bottom - data->window_rect.top)) cy = 1;
1335 if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1337 TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1338 data->managed = TRUE;
1339 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1342 mask = get_window_attributes( display, data, &attr );
1344 wine_tsx11_lock();
1346 data->whole_rect = data->window_rect;
1347 data->whole_window = XCreateWindow( display, root_window,
1348 data->window_rect.left - virtual_screen_rect.left,
1349 data->window_rect.top - virtual_screen_rect.top,
1350 cx, cy, 0, screen_depth, InputOutput,
1351 visual, mask, &attr );
1353 if (data->whole_window) XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
1354 wine_tsx11_unlock();
1356 if (!data->whole_window) return 0;
1358 if (!create_client_window( display, data, NULL ))
1360 wine_tsx11_lock();
1361 XDeleteContext( display, data->whole_window, winContext );
1362 XDestroyWindow( display, data->whole_window );
1363 data->whole_window = 0;
1364 wine_tsx11_unlock();
1365 return 0;
1368 set_initial_wm_hints( display, data );
1369 set_wm_hints( display, data );
1371 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1373 /* set the window text */
1374 if (!InternalGetWindowText( data->hwnd, text, sizeof(text)/sizeof(WCHAR) )) text[0] = 0;
1375 sync_window_text( display, data->whole_window, text );
1377 /* set the window region */
1378 if ((hrgn = CreateRectRgn( 0, 0, 0, 0 )))
1380 if (GetWindowRgn( data->hwnd, hrgn ) != ERROR) sync_window_region( display, data, hrgn );
1381 DeleteObject( hrgn );
1383 wine_tsx11_lock();
1384 XFlush( display ); /* make sure the window exists before we start painting to it */
1385 wine_tsx11_unlock();
1386 return data->whole_window;
1390 /**********************************************************************
1391 * destroy_whole_window
1393 * Destroy the whole X window for a given window.
1395 static void destroy_whole_window( Display *display, struct x11drv_win_data *data, BOOL already_destroyed )
1397 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1399 if (!data->whole_window) return;
1401 TRACE( "win %p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1402 if (thread_data->cursor_window == data->whole_window ||
1403 thread_data->cursor_window == data->client_window)
1404 thread_data->cursor_window = None;
1405 wine_tsx11_lock();
1406 XDeleteContext( display, data->whole_window, winContext );
1407 XDeleteContext( display, data->client_window, winContext );
1408 if (!already_destroyed) XDestroyWindow( display, data->whole_window );
1409 data->whole_window = data->client_window = 0;
1410 data->wm_state = WithdrawnState;
1411 data->net_wm_state = 0;
1412 data->mapped = FALSE;
1413 if (data->xic)
1415 XUnsetICFocus( data->xic );
1416 XDestroyIC( data->xic );
1417 data->xic = 0;
1419 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1420 XFlush( display );
1421 XFree( data->wm_hints );
1422 data->wm_hints = NULL;
1423 wine_tsx11_unlock();
1424 RemovePropA( data->hwnd, whole_window_prop );
1425 RemovePropA( data->hwnd, client_window_prop );
1429 /*****************************************************************
1430 * SetWindowText (X11DRV.@)
1432 void X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1434 Window win;
1436 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(gdi_display))
1438 Display *display = thread_init_display();
1439 sync_window_text( display, win, text );
1444 /***********************************************************************
1445 * SetWindowStyle (X11DRV.@)
1447 * Update the X state of a window to reflect a style change
1449 void X11DRV_SetWindowStyle( HWND hwnd, DWORD old_style )
1451 struct x11drv_win_data *data;
1452 DWORD new_style, changed;
1454 if (hwnd == GetDesktopWindow()) return;
1455 new_style = GetWindowLongW( hwnd, GWL_STYLE );
1456 changed = new_style ^ old_style;
1458 if ((changed & WS_VISIBLE) && (new_style & WS_VISIBLE))
1460 /* we don't unmap windows, that causes trouble with the window manager */
1461 if (!(data = X11DRV_get_win_data( hwnd )) &&
1462 !(data = X11DRV_create_win_data( hwnd ))) return;
1464 if (data->whole_window && is_window_rect_mapped( &data->window_rect ))
1466 Display *display = thread_display();
1467 set_wm_hints( display, data );
1468 if (!data->mapped) map_window( display, data, new_style );
1472 if (changed & WS_DISABLED)
1474 data = X11DRV_get_win_data( hwnd );
1475 if (data && data->wm_hints)
1477 wine_tsx11_lock();
1478 data->wm_hints->input = !(new_style & WS_DISABLED);
1479 XSetWMHints( thread_display(), data->whole_window, data->wm_hints );
1480 wine_tsx11_unlock();
1486 /***********************************************************************
1487 * DestroyWindow (X11DRV.@)
1489 void X11DRV_DestroyWindow( HWND hwnd )
1491 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1492 struct x11drv_win_data *data;
1494 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1496 if (data->pixmap)
1498 wine_tsx11_lock();
1499 destroy_glxpixmap(gdi_display, data->gl_drawable);
1500 XFreePixmap(gdi_display, data->pixmap);
1501 wine_tsx11_unlock();
1503 else if (data->gl_drawable)
1505 wine_tsx11_lock();
1506 XDestroyWindow(gdi_display, data->gl_drawable);
1507 wine_tsx11_unlock();
1510 destroy_whole_window( thread_data->display, data, FALSE );
1511 destroy_icon_window( thread_data->display, data );
1513 if (data->colormap)
1515 wine_tsx11_lock();
1516 XFreeColormap( thread_data->display, data->colormap );
1517 wine_tsx11_unlock();
1520 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1521 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
1522 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
1523 wine_tsx11_lock();
1524 XDeleteContext( thread_data->display, (XID)hwnd, win_data_context );
1525 wine_tsx11_unlock();
1526 HeapFree( GetProcessHeap(), 0, data );
1530 /***********************************************************************
1531 * X11DRV_DestroyNotify
1533 void X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1535 Display *display = event->xdestroywindow.display;
1536 struct x11drv_win_data *data;
1538 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1540 FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1541 destroy_whole_window( display, data, TRUE );
1545 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
1547 struct x11drv_win_data *data;
1549 if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
1551 data->hwnd = hwnd;
1552 wine_tsx11_lock();
1553 if (!winContext) winContext = XUniqueContext();
1554 if (!win_data_context) win_data_context = XUniqueContext();
1555 XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
1556 wine_tsx11_unlock();
1558 return data;
1562 /* initialize the desktop window id in the desktop manager process */
1563 static struct x11drv_win_data *create_desktop_win_data( Display *display, HWND hwnd )
1565 struct x11drv_win_data *data;
1567 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1568 data->whole_window = data->client_window = root_window;
1569 data->managed = TRUE;
1570 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1571 SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
1572 SetPropA( data->hwnd, client_window_prop, (HANDLE)root_window );
1573 set_initial_wm_hints( display, data );
1574 return data;
1577 /**********************************************************************
1578 * CreateDesktopWindow (X11DRV.@)
1580 BOOL X11DRV_CreateDesktopWindow( HWND hwnd )
1582 unsigned int width, height;
1584 /* retrieve the real size of the desktop */
1585 SERVER_START_REQ( get_window_rectangles )
1587 req->handle = hwnd;
1588 wine_server_call( req );
1589 width = reply->window.right - reply->window.left;
1590 height = reply->window.bottom - reply->window.top;
1592 SERVER_END_REQ;
1594 if (!width && !height) /* not initialized yet */
1596 SERVER_START_REQ( set_window_pos )
1598 req->handle = hwnd;
1599 req->previous = 0;
1600 req->flags = SWP_NOZORDER;
1601 req->window.left = virtual_screen_rect.left;
1602 req->window.top = virtual_screen_rect.top;
1603 req->window.right = virtual_screen_rect.right;
1604 req->window.bottom = virtual_screen_rect.bottom;
1605 req->client = req->window;
1606 wine_server_call( req );
1608 SERVER_END_REQ;
1610 else
1612 Window win = (Window)GetPropA( hwnd, whole_window_prop );
1613 if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1615 return TRUE;
1619 /**********************************************************************
1620 * CreateWindow (X11DRV.@)
1622 BOOL X11DRV_CreateWindow( HWND hwnd )
1624 if (hwnd == GetDesktopWindow() && root_window != DefaultRootWindow( gdi_display ))
1626 Display *display = thread_init_display();
1628 /* the desktop win data can't be created lazily */
1629 if (!create_desktop_win_data( display, hwnd )) return FALSE;
1631 return TRUE;
1635 /***********************************************************************
1636 * X11DRV_get_win_data
1638 * Return the X11 data structure associated with a window.
1640 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1642 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1643 char *data;
1645 if (!thread_data) return NULL;
1646 if (!hwnd) return NULL;
1647 if (XFindContext( thread_data->display, (XID)hwnd, win_data_context, &data )) data = NULL;
1648 return (struct x11drv_win_data *)data;
1652 /***********************************************************************
1653 * X11DRV_create_win_data
1655 * Create an X11 data window structure for an existing window.
1657 struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd )
1659 Display *display;
1660 struct x11drv_win_data *data;
1661 HWND parent;
1663 if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL; /* desktop */
1665 /* don't create win data for HWND_MESSAGE windows */
1666 if (parent != GetDesktopWindow() && !GetAncestor( parent, GA_PARENT )) return NULL;
1668 display = thread_init_display();
1669 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1671 GetWindowRect( hwnd, &data->window_rect );
1672 MapWindowPoints( 0, parent, (POINT *)&data->window_rect, 2 );
1673 data->whole_rect = data->window_rect;
1674 GetClientRect( hwnd, &data->client_rect );
1675 MapWindowPoints( hwnd, parent, (POINT *)&data->client_rect, 2 );
1677 if (parent == GetDesktopWindow())
1679 if (!create_whole_window( display, data ))
1681 HeapFree( GetProcessHeap(), 0, data );
1682 return NULL;
1684 TRACE( "win %p/%lx/%lx window %s whole %s client %s\n",
1685 hwnd, data->whole_window, data->client_window, wine_dbgstr_rect( &data->window_rect ),
1686 wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
1688 return data;
1692 /***********************************************************************
1693 * X11DRV_get_whole_window
1695 * Return the X window associated with the full area of a window
1697 Window X11DRV_get_whole_window( HWND hwnd )
1699 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1701 if (!data)
1703 if (hwnd == GetDesktopWindow()) return root_window;
1704 return (Window)GetPropA( hwnd, whole_window_prop );
1706 return data->whole_window;
1710 /***********************************************************************
1711 * X11DRV_get_client_window
1713 * Return the X window associated with the client area of a window
1715 Window X11DRV_get_client_window( HWND hwnd )
1717 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1719 if (!data)
1721 if (hwnd == GetDesktopWindow()) return root_window;
1722 return (Window)GetPropA( hwnd, client_window_prop );
1724 return data->client_window;
1728 /***********************************************************************
1729 * X11DRV_get_ic
1731 * Return the X input context associated with a window
1733 XIC X11DRV_get_ic( HWND hwnd )
1735 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1736 XIM xim;
1738 if (!data) return 0;
1739 if (data->xic) return data->xic;
1740 if (!(xim = x11drv_thread_data()->xim)) return 0;
1741 return X11DRV_CreateIC( xim, data );
1745 /***********************************************************************
1746 * X11DRV_GetDC (X11DRV.@)
1748 void X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
1749 const RECT *top_rect, DWORD flags )
1751 struct x11drv_escape_set_drawable escape;
1752 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1754 escape.code = X11DRV_SET_DRAWABLE;
1755 escape.mode = IncludeInferiors;
1756 escape.fbconfig_id = 0;
1757 escape.gl_drawable = 0;
1758 escape.pixmap = 0;
1759 escape.gl_copy = FALSE;
1761 if (top == hwnd && data && IsIconic( hwnd ) && data->icon_window)
1763 escape.drawable = data->icon_window;
1765 else if (top == hwnd)
1767 escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
1768 /* GL draws to the client area even for window DCs */
1769 escape.gl_drawable = data ? data->client_window : X11DRV_get_client_window( hwnd );
1770 if (flags & DCX_WINDOW)
1771 escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
1772 else
1773 escape.drawable = escape.gl_drawable;
1775 else
1777 escape.drawable = X11DRV_get_client_window( top );
1778 escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
1779 escape.gl_drawable = data ? data->gl_drawable : (Drawable)GetPropA( hwnd, gl_drawable_prop );
1780 escape.pixmap = data ? data->pixmap : (Pixmap)GetPropA( hwnd, pixmap_prop );
1781 escape.gl_copy = (escape.gl_drawable != 0);
1782 if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
1785 escape.dc_rect.left = win_rect->left - top_rect->left;
1786 escape.dc_rect.top = win_rect->top - top_rect->top;
1787 escape.dc_rect.right = win_rect->right - top_rect->left;
1788 escape.dc_rect.bottom = win_rect->bottom - top_rect->top;
1789 escape.drawable_rect.left = top_rect->left;
1790 escape.drawable_rect.top = top_rect->top;
1791 escape.drawable_rect.right = top_rect->right;
1792 escape.drawable_rect.bottom = top_rect->bottom;
1794 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1798 /***********************************************************************
1799 * X11DRV_ReleaseDC (X11DRV.@)
1801 void X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
1803 struct x11drv_escape_set_drawable escape;
1805 escape.code = X11DRV_SET_DRAWABLE;
1806 escape.drawable = root_window;
1807 escape.mode = IncludeInferiors;
1808 escape.drawable_rect = virtual_screen_rect;
1809 SetRect( &escape.dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
1810 virtual_screen_rect.bottom - virtual_screen_rect.top );
1811 escape.fbconfig_id = 0;
1812 escape.gl_drawable = 0;
1813 escape.pixmap = 0;
1814 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1818 /***********************************************************************
1819 * SetCapture (X11DRV.@)
1821 void X11DRV_SetCapture( HWND hwnd, UINT flags )
1823 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1825 if (!thread_data) return;
1826 if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
1828 if (hwnd)
1830 Window grab_win = X11DRV_get_client_window( GetAncestor( hwnd, GA_ROOT ) );
1832 if (!grab_win) return;
1833 wine_tsx11_lock();
1834 XFlush( gdi_display );
1835 XGrabPointer( thread_data->display, grab_win, False,
1836 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1837 GrabModeAsync, GrabModeAsync, None, None, CurrentTime );
1838 wine_tsx11_unlock();
1839 thread_data->grab_window = grab_win;
1841 else /* release capture */
1843 wine_tsx11_lock();
1844 XFlush( gdi_display );
1845 XUngrabPointer( thread_data->display, CurrentTime );
1846 wine_tsx11_unlock();
1847 thread_data->grab_window = None;
1852 /*****************************************************************
1853 * SetParent (X11DRV.@)
1855 void X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
1857 Display *display = thread_display();
1858 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1860 if (!data) return;
1861 if (parent == old_parent) return;
1863 if (parent != GetDesktopWindow()) /* a child window */
1865 if (old_parent == GetDesktopWindow())
1867 /* destroy the old X windows */
1868 destroy_whole_window( display, data, FALSE );
1869 destroy_icon_window( display, data );
1870 if (data->managed)
1872 data->managed = FALSE;
1873 RemovePropA( data->hwnd, managed_prop );
1877 else /* new top level window */
1879 /* FIXME: we ignore errors since we can't really recover anyway */
1880 create_whole_window( display, data );
1885 /*****************************************************************
1886 * SetFocus (X11DRV.@)
1888 * Set the X focus.
1890 void X11DRV_SetFocus( HWND hwnd )
1892 Display *display = thread_display();
1893 struct x11drv_win_data *data;
1894 XWindowChanges changes;
1896 if (!(hwnd = GetAncestor( hwnd, GA_ROOT ))) return;
1897 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1898 if (data->managed || !data->whole_window) return;
1900 /* Set X focus and install colormap */
1901 wine_tsx11_lock();
1902 changes.stack_mode = Above;
1903 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
1904 if (root_window == DefaultRootWindow(display))
1906 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1907 /* FIXME: this is not entirely correct */
1908 XSetInputFocus( display, data->whole_window, RevertToParent,
1909 /* CurrentTime */
1910 GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1912 wine_tsx11_unlock();
1916 /***********************************************************************
1917 * SetWindowPos (X11DRV.@)
1919 void X11DRV_SetWindowPos( HWND hwnd, HWND insert_after, UINT swp_flags,
1920 const RECT *rectWindow, const RECT *rectClient,
1921 const RECT *visible_rect, const RECT *valid_rects )
1923 struct x11drv_thread_data *thread_data;
1924 Display *display;
1925 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1926 DWORD new_style = GetWindowLongW( hwnd, GWL_STYLE );
1927 RECT old_whole_rect, old_client_rect;
1928 int event_type;
1930 if (!data)
1932 /* create the win data if the window is being made visible */
1933 if (!(new_style & WS_VISIBLE)) return;
1934 if (!(data = X11DRV_create_win_data( hwnd ))) return;
1937 thread_data = x11drv_thread_data();
1938 display = thread_data->display;
1940 /* check if we need to switch the window to managed */
1941 if (!data->managed && data->whole_window && is_window_managed( hwnd, swp_flags, rectWindow ))
1943 TRACE( "making win %p/%lx managed\n", hwnd, data->whole_window );
1944 if (data->mapped) unmap_window( display, data );
1945 data->managed = TRUE;
1946 SetPropA( hwnd, managed_prop, (HANDLE)1 );
1949 old_whole_rect = data->whole_rect;
1950 old_client_rect = data->client_rect;
1951 data->window_rect = *rectWindow;
1952 data->whole_rect = *rectWindow;
1953 data->client_rect = *rectClient;
1954 X11DRV_window_to_X_rect( data, &data->whole_rect );
1955 if (memcmp( visible_rect, &data->whole_rect, sizeof(RECT) ))
1957 TRACE( "%p: need to update visible rect %s -> %s\n", hwnd,
1958 wine_dbgstr_rect(visible_rect), wine_dbgstr_rect(&data->whole_rect) );
1959 SERVER_START_REQ( set_window_visible_rect )
1961 req->handle = hwnd;
1962 req->flags = swp_flags;
1963 req->visible.left = data->whole_rect.left;
1964 req->visible.top = data->whole_rect.top;
1965 req->visible.right = data->whole_rect.right;
1966 req->visible.bottom = data->whole_rect.bottom;
1967 wine_server_call( req );
1969 SERVER_END_REQ;
1972 TRACE( "win %p window %s client %s style %08x flags %08x\n",
1973 hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style, swp_flags );
1975 if (!IsRectEmpty( &valid_rects[0] ))
1977 int x_offset = old_whole_rect.left - data->whole_rect.left;
1978 int y_offset = old_whole_rect.top - data->whole_rect.top;
1980 /* if all that happened is that the whole window moved, copy everything */
1981 if (!(swp_flags & SWP_FRAMECHANGED) &&
1982 old_whole_rect.right - data->whole_rect.right == x_offset &&
1983 old_whole_rect.bottom - data->whole_rect.bottom == y_offset &&
1984 old_client_rect.left - data->client_rect.left == x_offset &&
1985 old_client_rect.right - data->client_rect.right == x_offset &&
1986 old_client_rect.top - data->client_rect.top == y_offset &&
1987 old_client_rect.bottom - data->client_rect.bottom == y_offset &&
1988 !memcmp( &valid_rects[0], &data->client_rect, sizeof(RECT) ))
1990 /* if we have an X window the bits will be moved by the X server */
1991 if (!data->whole_window)
1992 move_window_bits( data, &old_whole_rect, &data->whole_rect, &old_client_rect );
1994 else
1995 move_window_bits( data, &valid_rects[1], &valid_rects[0], &old_client_rect );
1998 wine_tsx11_lock();
1999 XFlush( gdi_display ); /* make sure painting is done before we move the window */
2000 wine_tsx11_unlock();
2002 sync_client_position( display, data, swp_flags, &old_client_rect, &old_whole_rect );
2004 if (!data->whole_window) return;
2006 /* check if we are currently processing an event relevant to this window */
2007 event_type = 0;
2008 if (thread_data->current_event && thread_data->current_event->xany.window == data->whole_window)
2009 event_type = thread_data->current_event->type;
2011 if (event_type != ConfigureNotify && event_type != PropertyNotify)
2012 event_type = 0; /* ignore other events */
2014 if (data->mapped)
2016 if (((swp_flags & SWP_HIDEWINDOW) && !(new_style & WS_VISIBLE)) ||
2017 (!event_type && !is_window_rect_mapped( rectWindow )))
2018 unmap_window( display, data );
2021 /* don't change position if we are about to minimize or maximize a managed window */
2022 if (!event_type &&
2023 !(data->managed && (swp_flags & SWP_STATECHANGED) && (new_style & (WS_MINIMIZE|WS_MAXIMIZE))))
2024 sync_window_position( display, data, swp_flags, &old_client_rect, &old_whole_rect );
2026 if ((new_style & WS_VISIBLE) &&
2027 ((new_style & WS_MINIMIZE) || is_window_rect_mapped( rectWindow )))
2029 if (!data->mapped || (swp_flags & (SWP_FRAMECHANGED|SWP_STATECHANGED)))
2030 set_wm_hints( display, data );
2032 if (!data->mapped)
2034 map_window( display, data, new_style );
2036 else if ((swp_flags & SWP_STATECHANGED) && (!data->iconic != !(new_style & WS_MINIMIZE)))
2038 data->iconic = (new_style & WS_MINIMIZE) != 0;
2039 TRACE( "changing win %p iconic state to %u\n", data->hwnd, data->iconic );
2040 wine_tsx11_lock();
2041 if (data->iconic)
2042 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
2043 else if (is_window_rect_mapped( rectWindow ))
2044 XMapWindow( display, data->whole_window );
2045 wine_tsx11_unlock();
2046 update_net_wm_states( display, data );
2048 else if (!event_type)
2050 update_net_wm_states( display, data );
2054 wine_tsx11_lock();
2055 XFlush( display ); /* make sure changes are done before we start painting again */
2056 wine_tsx11_unlock();
2060 /**********************************************************************
2061 * SetWindowIcon (X11DRV.@)
2063 * hIcon or hIconSm has changed (or is being initialised for the
2064 * first time). Complete the X11 driver-specific initialisation
2065 * and set the window hints.
2067 * This is not entirely correct, may need to create
2068 * an icon window and set the pixmap as a background
2070 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
2072 Display *display = thread_display();
2073 struct x11drv_win_data *data;
2075 if (type != ICON_BIG) return; /* nothing to do here */
2077 if (!(data = X11DRV_get_win_data( hwnd ))) return;
2078 if (!data->whole_window) return;
2079 if (!data->managed) return;
2081 if (data->wm_hints)
2083 set_icon_hints( display, data, icon );
2084 wine_tsx11_lock();
2085 XSetWMHints( display, data->whole_window, data->wm_hints );
2086 wine_tsx11_unlock();
2091 /***********************************************************************
2092 * SetWindowRgn (X11DRV.@)
2094 * Assign specified region to window (for non-rectangular windows)
2096 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
2098 struct x11drv_win_data *data;
2100 if ((data = X11DRV_get_win_data( hwnd )))
2102 sync_window_region( thread_display(), data, hrgn );
2104 else if (GetWindowThreadProcessId( hwnd, NULL ) != GetCurrentThreadId())
2106 FIXME( "not supported on other thread window %p\n", hwnd );
2107 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2108 return FALSE;
2111 return TRUE;
2115 /***********************************************************************
2116 * is_netwm_supported
2118 static BOOL is_netwm_supported( Display *display, Atom atom )
2120 static Atom *net_supported;
2121 static int net_supported_count = -1;
2122 int i;
2124 wine_tsx11_lock();
2125 if (net_supported_count == -1)
2127 Atom type;
2128 int format;
2129 unsigned long count, remaining;
2131 if (!XGetWindowProperty( display, DefaultRootWindow(display), x11drv_atom(_NET_SUPPORTED), 0,
2132 ~0UL, False, XA_ATOM, &type, &format, &count,
2133 &remaining, (unsigned char **)&net_supported ))
2134 net_supported_count = get_property_size( format, count ) / sizeof(Atom);
2135 else
2136 net_supported_count = 0;
2138 wine_tsx11_unlock();
2140 for (i = 0; i < net_supported_count; i++)
2141 if (net_supported[i] == atom) return TRUE;
2142 return FALSE;
2146 /***********************************************************************
2147 * SysCommand (X11DRV.@)
2149 * Perform WM_SYSCOMMAND handling.
2151 LRESULT X11DRV_SysCommand( HWND hwnd, WPARAM wparam, LPARAM lparam )
2153 WPARAM hittest = wparam & 0x0f;
2154 DWORD dwPoint;
2155 int x, y, dir;
2156 XEvent xev;
2157 Display *display = thread_display();
2158 struct x11drv_win_data *data;
2160 if (!(data = X11DRV_get_win_data( hwnd ))) return -1;
2161 if (!data->whole_window || !data->managed || !data->mapped) return -1;
2163 switch (wparam & 0xfff0)
2165 case SC_MOVE:
2166 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
2167 else dir = _NET_WM_MOVERESIZE_MOVE;
2168 break;
2169 case SC_SIZE:
2170 /* windows without WS_THICKFRAME are not resizable through the window manager */
2171 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_THICKFRAME)) return -1;
2173 switch (hittest)
2175 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
2176 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
2177 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
2178 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
2179 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
2180 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
2181 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
2182 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
2183 default: dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD; break;
2185 break;
2187 case SC_KEYMENU:
2188 /* prevent a simple ALT press+release from activating the system menu,
2189 * as that can get confusing on managed windows */
2190 if ((WCHAR)lparam) return -1; /* got an explicit char */
2191 if (GetMenu( hwnd )) return -1; /* window has a real menu */
2192 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) return -1; /* no system menu */
2193 TRACE( "ignoring SC_KEYMENU wp %lx lp %lx\n", wparam, lparam );
2194 return 0;
2196 default:
2197 return -1;
2200 if (IsZoomed(hwnd)) return -1;
2202 if (!is_netwm_supported( display, x11drv_atom(_NET_WM_MOVERESIZE) ))
2204 TRACE( "_NET_WM_MOVERESIZE not supported\n" );
2205 return -1;
2208 dwPoint = GetMessagePos();
2209 x = (short)LOWORD(dwPoint);
2210 y = (short)HIWORD(dwPoint);
2212 TRACE("hwnd %p, x %d, y %d, dir %d\n", hwnd, x, y, dir);
2214 xev.xclient.type = ClientMessage;
2215 xev.xclient.window = X11DRV_get_whole_window(hwnd);
2216 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
2217 xev.xclient.serial = 0;
2218 xev.xclient.display = display;
2219 xev.xclient.send_event = True;
2220 xev.xclient.format = 32;
2221 xev.xclient.data.l[0] = x; /* x coord */
2222 xev.xclient.data.l[1] = y; /* y coord */
2223 xev.xclient.data.l[2] = dir; /* direction */
2224 xev.xclient.data.l[3] = 1; /* button */
2225 xev.xclient.data.l[4] = 0; /* unused */
2227 /* need to ungrab the pointer that may have been automatically grabbed
2228 * with a ButtonPress event */
2229 wine_tsx11_lock();
2230 XUngrabPointer( display, CurrentTime );
2231 XSendEvent(display, root_window, False, SubstructureNotifyMask, &xev);
2232 wine_tsx11_unlock();
2233 return 0;