winex11: Driver-side implementation of SetLayeredWindowAttributes.
[wine.git] / dlls / winex11.drv / window.c
blobbd370f68827d49096412cc55b82ed5350ae8ba6f
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 win_region )
341 #ifdef HAVE_LIBXSHAPE
342 HRGN hrgn = win_region;
344 if (!data->whole_window) return;
345 data->shaped = FALSE;
347 if (hrgn == (HRGN)1) /* hack: win_region == 1 means retrieve region from server */
349 if (!(hrgn = CreateRectRgn( 0, 0, 0, 0 ))) return;
350 if (GetWindowRgn( data->hwnd, hrgn ) == ERROR)
352 DeleteObject( hrgn );
353 hrgn = 0;
357 if (!hrgn)
359 wine_tsx11_lock();
360 XShapeCombineMask( display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
361 wine_tsx11_unlock();
363 else
365 RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
366 if (pRegionData)
368 wine_tsx11_lock();
369 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
370 data->window_rect.left - data->whole_rect.left,
371 data->window_rect.top - data->whole_rect.top,
372 (XRectangle *)pRegionData->Buffer,
373 pRegionData->rdh.nCount, ShapeSet, YXBanded );
374 wine_tsx11_unlock();
375 HeapFree(GetProcessHeap(), 0, pRegionData);
376 data->shaped = TRUE;
379 if (hrgn && hrgn != win_region) DeleteObject( hrgn );
380 #endif /* HAVE_LIBXSHAPE */
384 /***********************************************************************
385 * sync_window_opacity
387 static void sync_window_opacity( Display *display, Window win,
388 COLORREF key, BYTE alpha, DWORD flags )
390 unsigned long opacity = 0xffffffff;
392 if (flags & LWA_ALPHA) opacity = (0xffffffff / 0xff) * alpha;
394 if (flags & LWA_COLORKEY) FIXME("LWA_COLORKEY not supported\n");
396 wine_tsx11_lock();
397 if (opacity == 0xffffffff)
398 XDeleteProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY) );
399 else
400 XChangeProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY),
401 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&opacity, 1 );
402 wine_tsx11_unlock();
406 /***********************************************************************
407 * sync_window_text
409 static void sync_window_text( Display *display, Window win, const WCHAR *text )
411 UINT count;
412 char *buffer, *utf8_buffer;
413 XTextProperty prop;
415 /* allocate new buffer for window text */
416 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
417 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count ))) return;
418 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
420 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
421 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
423 HeapFree( GetProcessHeap(), 0, buffer );
424 return;
426 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
428 wine_tsx11_lock();
429 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
431 XSetWMName( display, win, &prop );
432 XSetWMIconName( display, win, &prop );
433 XFree( prop.value );
436 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
437 according to the standard
438 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
440 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
441 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
442 wine_tsx11_unlock();
444 HeapFree( GetProcessHeap(), 0, utf8_buffer );
445 HeapFree( GetProcessHeap(), 0, buffer );
449 /***********************************************************************
450 * set_win_format
452 static BOOL set_win_format( HWND hwnd, XID fbconfig_id )
454 struct x11drv_win_data *data;
455 XVisualInfo *vis;
456 int w, h;
458 if (!(data = X11DRV_get_win_data(hwnd)) &&
459 !(data = X11DRV_create_win_data(hwnd))) return FALSE;
461 if (!(vis = visual_from_fbconfig_id(fbconfig_id))) return FALSE;
463 if (data->whole_window)
465 Display *display = thread_display();
466 Window client = data->client_window;
468 if (vis->visualid != data->visualid)
470 client = create_client_window( display, data, vis );
471 TRACE( "re-created client window %lx for %p fbconfig %lx\n", client, data->hwnd, fbconfig_id );
473 wine_tsx11_lock();
474 XFree(vis);
475 XFlush( display );
476 wine_tsx11_unlock();
477 if (client) goto done;
478 return FALSE;
481 w = data->client_rect.right - data->client_rect.left;
482 h = data->client_rect.bottom - data->client_rect.top;
484 if(w <= 0) w = 1;
485 if(h <= 0) h = 1;
487 #ifdef SONAME_LIBXCOMPOSITE
488 if(usexcomposite)
490 XSetWindowAttributes attrib;
491 static Window dummy_parent;
493 wine_tsx11_lock();
494 attrib.override_redirect = True;
495 if (!dummy_parent)
497 dummy_parent = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, screen_depth,
498 InputOutput, visual, CWOverrideRedirect, &attrib );
499 XMapWindow( gdi_display, dummy_parent );
501 data->colormap = XCreateColormap(gdi_display, dummy_parent, vis->visual,
502 (vis->class == PseudoColor ||
503 vis->class == GrayScale ||
504 vis->class == DirectColor) ?
505 AllocAll : AllocNone);
506 attrib.colormap = data->colormap;
507 XInstallColormap(gdi_display, attrib.colormap);
509 if(data->gl_drawable) XDestroyWindow(gdi_display, data->gl_drawable);
510 data->gl_drawable = XCreateWindow(gdi_display, dummy_parent, -w, 0, w, h, 0,
511 vis->depth, InputOutput, vis->visual,
512 CWColormap | CWOverrideRedirect,
513 &attrib);
514 if(data->gl_drawable)
516 pXCompositeRedirectWindow(gdi_display, data->gl_drawable,
517 CompositeRedirectManual);
518 XMapWindow(gdi_display, data->gl_drawable);
520 XFree(vis);
521 XFlush( gdi_display );
522 wine_tsx11_unlock();
524 else
525 #endif
527 WARN("XComposite is not available, using GLXPixmap hack\n");
529 wine_tsx11_lock();
531 if(data->pixmap) XFreePixmap(gdi_display, data->pixmap);
532 data->pixmap = XCreatePixmap(gdi_display, root_window, w, h, vis->depth);
533 if(!data->pixmap)
535 XFree(vis);
536 wine_tsx11_unlock();
537 return FALSE;
540 if(data->gl_drawable) destroy_glxpixmap(gdi_display, data->gl_drawable);
541 data->gl_drawable = create_glxpixmap(gdi_display, vis, data->pixmap);
542 if(!data->gl_drawable)
544 XFreePixmap(gdi_display, data->pixmap);
545 data->pixmap = 0;
547 XFree(vis);
548 XFlush( gdi_display );
549 wine_tsx11_unlock();
550 if (data->pixmap) SetPropA(hwnd, pixmap_prop, (HANDLE)data->pixmap);
553 if (!data->gl_drawable) return FALSE;
555 TRACE("Created GL drawable 0x%lx, using FBConfigID 0x%lx\n",
556 data->gl_drawable, fbconfig_id);
557 SetPropA(hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
559 done:
560 data->fbconfig_id = fbconfig_id;
561 SetPropA(hwnd, fbconfig_id_prop, (HANDLE)data->fbconfig_id);
562 /* force DCE invalidation */
563 SetWindowPos( hwnd, 0, 0, 0, 0, 0,
564 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE |
565 SWP_NOREDRAW | SWP_DEFERERASE | SWP_NOSENDCHANGING | SWP_STATECHANGED);
566 return TRUE;
569 /***********************************************************************
570 * sync_gl_drawable
572 static void sync_gl_drawable(struct x11drv_win_data *data)
574 int w = data->client_rect.right - data->client_rect.left;
575 int h = data->client_rect.bottom - data->client_rect.top;
576 XVisualInfo *vis;
577 Drawable glxp;
578 Pixmap pix;
580 if (w <= 0) w = 1;
581 if (h <= 0) h = 1;
583 TRACE("Resizing GL drawable 0x%lx to %dx%d\n", data->gl_drawable, w, h);
584 #ifdef SONAME_LIBXCOMPOSITE
585 if(usexcomposite)
587 wine_tsx11_lock();
588 XMoveResizeWindow(gdi_display, data->gl_drawable, -w, 0, w, h);
589 wine_tsx11_unlock();
590 return;
592 #endif
594 if (!(vis = visual_from_fbconfig_id(data->fbconfig_id))) return;
596 wine_tsx11_lock();
597 pix = XCreatePixmap(gdi_display, root_window, w, h, vis->depth);
598 if(!pix)
600 ERR("Failed to create pixmap for offscreen rendering\n");
601 XFree(vis);
602 wine_tsx11_unlock();
603 return;
606 glxp = create_glxpixmap(gdi_display, vis, pix);
607 if(!glxp)
609 ERR("Failed to create drawable for offscreen rendering\n");
610 XFreePixmap(gdi_display, pix);
611 XFree(vis);
612 wine_tsx11_unlock();
613 return;
616 XFree(vis);
618 mark_drawable_dirty(data->gl_drawable, glxp);
620 XFreePixmap(gdi_display, data->pixmap);
621 destroy_glxpixmap(gdi_display, data->gl_drawable);
622 TRACE( "Recreated GL drawable %lx to replace %lx\n", glxp, data->gl_drawable );
624 data->pixmap = pix;
625 data->gl_drawable = glxp;
627 XFlush( gdi_display );
628 wine_tsx11_unlock();
630 SetPropA(data->hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
631 SetPropA(data->hwnd, pixmap_prop, (HANDLE)data->pixmap);
635 /***********************************************************************
636 * get_window_changes
638 * fill the window changes structure
640 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
642 int mask = 0;
644 if (old->right - old->left != new->right - new->left )
646 if ((changes->width = new->right - new->left) <= 0) changes->width = 1;
647 mask |= CWWidth;
649 if (old->bottom - old->top != new->bottom - new->top)
651 if ((changes->height = new->bottom - new->top) <= 0) changes->height = 1;
652 mask |= CWHeight;
654 if (old->left != new->left)
656 changes->x = new->left;
657 mask |= CWX;
659 if (old->top != new->top)
661 changes->y = new->top;
662 mask |= CWY;
664 return mask;
668 /***********************************************************************
669 * create_icon_window
671 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
673 XSetWindowAttributes attr;
675 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
676 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
677 attr.bit_gravity = NorthWestGravity;
678 attr.backing_store = NotUseful/*WhenMapped*/;
679 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
681 wine_tsx11_lock();
682 data->icon_window = XCreateWindow( display, root_window, 0, 0,
683 GetSystemMetrics( SM_CXICON ),
684 GetSystemMetrics( SM_CYICON ),
685 0, screen_depth,
686 InputOutput, visual,
687 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
688 XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
689 XFlush( display ); /* make sure the window exists before we start painting to it */
690 wine_tsx11_unlock();
692 TRACE( "created %lx\n", data->icon_window );
693 SetPropA( data->hwnd, icon_window_prop, (HANDLE)data->icon_window );
694 return data->icon_window;
699 /***********************************************************************
700 * destroy_icon_window
702 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
704 if (!data->icon_window) return;
705 if (x11drv_thread_data()->cursor_window == data->icon_window)
706 x11drv_thread_data()->cursor_window = None;
707 wine_tsx11_lock();
708 XDeleteContext( display, data->icon_window, winContext );
709 XDestroyWindow( display, data->icon_window );
710 data->icon_window = 0;
711 wine_tsx11_unlock();
712 RemovePropA( data->hwnd, icon_window_prop );
716 /***********************************************************************
717 * set_icon_hints
719 * Set the icon wm hints
721 static void set_icon_hints( Display *display, struct x11drv_win_data *data, HICON hIcon )
723 XWMHints *hints = data->wm_hints;
725 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
726 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
727 data->hWMIconBitmap = 0;
728 data->hWMIconMask = 0;
730 if (!hIcon)
732 if (!data->icon_window) create_icon_window( display, data );
733 hints->icon_window = data->icon_window;
734 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
736 else
738 HBITMAP hbmOrig;
739 RECT rcMask;
740 BITMAP bmMask;
741 ICONINFO ii;
742 HDC hDC;
744 GetIconInfo(hIcon, &ii);
746 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
747 rcMask.top = 0;
748 rcMask.left = 0;
749 rcMask.right = bmMask.bmWidth;
750 rcMask.bottom = bmMask.bmHeight;
752 hDC = CreateCompatibleDC(0);
753 hbmOrig = SelectObject(hDC, ii.hbmMask);
754 InvertRect(hDC, &rcMask);
755 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
756 SelectObject(hDC, hbmOrig);
757 DeleteDC(hDC);
759 data->hWMIconBitmap = ii.hbmColor;
760 data->hWMIconMask = ii.hbmMask;
762 hints->icon_pixmap = X11DRV_get_pixmap(data->hWMIconBitmap);
763 hints->icon_mask = X11DRV_get_pixmap(data->hWMIconMask);
764 destroy_icon_window( display, data );
765 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
770 /***********************************************************************
771 * set_size_hints
773 * set the window size hints
775 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
777 XSizeHints* size_hints;
779 if (!(size_hints = XAllocSizeHints())) return;
781 size_hints->win_gravity = StaticGravity;
782 size_hints->flags |= PWinGravity;
784 /* don't update size hints if window is not in normal state */
785 if (!(style & (WS_MINIMIZE | WS_MAXIMIZE)))
787 if (data->hwnd != GetDesktopWindow()) /* don't force position of desktop */
789 size_hints->x = data->whole_rect.left;
790 size_hints->y = data->whole_rect.top;
791 size_hints->flags |= PPosition;
794 if (!is_window_resizable( data, style ))
796 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
797 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
798 size_hints->min_width = size_hints->max_width;
799 size_hints->min_height = size_hints->max_height;
800 size_hints->flags |= PMinSize | PMaxSize;
803 XSetWMNormalHints( display, data->whole_window, size_hints );
804 XFree( size_hints );
808 /***********************************************************************
809 * get_process_name
811 * get the name of the current process for setting class hints
813 static char *get_process_name(void)
815 static char *name;
817 if (!name)
819 WCHAR module[MAX_PATH];
820 DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
821 if (len && len < MAX_PATH)
823 char *ptr;
824 WCHAR *p, *appname = module;
826 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
827 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
828 len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
829 if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
831 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
832 name = ptr;
836 return name;
840 /***********************************************************************
841 * set_initial_wm_hints
843 * Set the window manager hints that don't change over the lifetime of a window.
845 static void set_initial_wm_hints( Display *display, struct x11drv_win_data *data )
847 long i;
848 Atom protocols[3];
849 Atom dndVersion = 4;
850 XClassHint *class_hints;
851 char *process_name = get_process_name();
853 wine_tsx11_lock();
855 /* wm protocols */
856 i = 0;
857 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
858 protocols[i++] = x11drv_atom(_NET_WM_PING);
859 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
860 XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
861 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
863 /* class hints */
864 if ((class_hints = XAllocClassHint()))
866 static char wine[] = "Wine";
868 class_hints->res_name = process_name;
869 class_hints->res_class = wine;
870 XSetClassHint( display, data->whole_window, class_hints );
871 XFree( class_hints );
874 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
875 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
876 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
877 i = getpid();
878 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
879 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
881 XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
882 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
884 data->wm_hints = XAllocWMHints();
885 wine_tsx11_unlock();
887 if (data->wm_hints)
889 HICON icon = (HICON)SendMessageW( data->hwnd, WM_GETICON, ICON_BIG, 0 );
890 if (!icon) icon = (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON );
891 data->wm_hints->flags = 0;
892 set_icon_hints( display, data, icon );
897 /***********************************************************************
898 * set_wm_hints
900 * Set the window manager hints for a newly-created window
902 static void set_wm_hints( Display *display, struct x11drv_win_data *data )
904 Window group_leader;
905 Atom window_type;
906 MwmHints mwm_hints;
907 DWORD style, ex_style;
908 HWND owner;
910 if (data->hwnd == GetDesktopWindow())
912 /* force some styles for the desktop to get the correct decorations */
913 style = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
914 ex_style = WS_EX_APPWINDOW;
915 owner = 0;
917 else
919 style = GetWindowLongW( data->hwnd, GWL_STYLE );
920 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
921 owner = get_window_owner( data->hwnd );
924 /* transient for hint */
925 if (owner)
927 Window owner_win = X11DRV_get_whole_window( owner );
928 wine_tsx11_lock();
929 XSetTransientForHint( display, data->whole_window, owner_win );
930 wine_tsx11_unlock();
931 group_leader = owner_win;
933 else group_leader = data->whole_window;
935 wine_tsx11_lock();
937 /* size hints */
938 set_size_hints( display, data, style );
940 /* set the WM_WINDOW_TYPE */
941 if (style & WS_THICKFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
942 else if (ex_style & WS_EX_APPWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
943 else if (style & WS_DLGFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
944 else if (ex_style & WS_EX_DLGMODALFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
945 else if ((style & WS_POPUP) && owner) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
946 #if 0 /* many window managers don't handle utility windows very well */
947 else if (ex_style & WS_EX_TOOLWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
948 #endif
949 else window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
951 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
952 XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
954 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
955 mwm_hints.decorations = get_mwm_decorations( data, style, ex_style );
956 mwm_hints.functions = MWM_FUNC_MOVE;
957 if (is_window_resizable( data, style )) mwm_hints.functions |= MWM_FUNC_RESIZE;
958 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
959 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
960 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
962 XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
963 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
964 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
966 /* wm hints */
967 if (data->wm_hints)
969 data->wm_hints->flags |= InputHint | StateHint | WindowGroupHint;
970 data->wm_hints->input = !(style & WS_DISABLED);
971 data->wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
972 data->wm_hints->window_group = group_leader;
973 XSetWMHints( display, data->whole_window, data->wm_hints );
976 wine_tsx11_unlock();
980 /***********************************************************************
981 * update_net_wm_states
983 void update_net_wm_states( Display *display, struct x11drv_win_data *data )
985 static const unsigned int state_atoms[NB_NET_WM_STATES] =
987 XATOM__NET_WM_STATE_FULLSCREEN,
988 XATOM__NET_WM_STATE_ABOVE,
989 XATOM__NET_WM_STATE_MAXIMIZED_VERT,
990 XATOM__NET_WM_STATE_SKIP_PAGER,
991 XATOM__NET_WM_STATE_SKIP_TASKBAR
994 DWORD i, style, ex_style, new_state = 0;
996 if (!data->managed) return;
997 if (data->whole_window == root_window) return;
999 style = GetWindowLongW( data->hwnd, GWL_STYLE );
1000 if (data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
1001 data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height)
1003 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION)
1004 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
1005 else if (!(style & WS_MINIMIZE))
1006 new_state |= (1 << NET_WM_STATE_FULLSCREEN);
1008 else if (style & WS_MAXIMIZE)
1009 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
1011 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
1012 if (ex_style & WS_EX_TOPMOST)
1013 new_state |= (1 << NET_WM_STATE_ABOVE);
1014 if (ex_style & WS_EX_TOOLWINDOW)
1015 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR) | (1 << NET_WM_STATE_SKIP_PAGER);
1016 if (!(ex_style & WS_EX_APPWINDOW) && get_window_owner( data->hwnd ))
1017 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR);
1019 if (!data->mapped) /* set the _NET_WM_STATE atom directly */
1021 Atom atoms[NB_NET_WM_STATES+1];
1022 DWORD count;
1024 for (i = count = 0; i < NB_NET_WM_STATES; i++)
1026 if (!(new_state & (1 << i))) continue;
1027 TRACE( "setting wm state %u for unmapped window %p/%lx\n",
1028 i, data->hwnd, data->whole_window );
1029 atoms[count++] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1030 if (state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT)
1031 atoms[count++] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
1033 wine_tsx11_lock();
1034 XChangeProperty( display, data->whole_window, x11drv_atom(_NET_WM_STATE), XA_ATOM,
1035 32, PropModeReplace, (unsigned char *)atoms, count );
1036 wine_tsx11_unlock();
1038 else /* ask the window manager to do it for us */
1040 XEvent xev;
1042 xev.xclient.type = ClientMessage;
1043 xev.xclient.window = data->whole_window;
1044 xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
1045 xev.xclient.serial = 0;
1046 xev.xclient.display = display;
1047 xev.xclient.send_event = True;
1048 xev.xclient.format = 32;
1049 xev.xclient.data.l[3] = 1;
1051 for (i = 0; i < NB_NET_WM_STATES; i++)
1053 if (!((data->net_wm_state ^ new_state) & (1 << i))) continue; /* unchanged */
1055 TRACE( "setting wm state %u for window %p/%lx to %u prev %u\n",
1056 i, data->hwnd, data->whole_window,
1057 (new_state & (1 << i)) != 0, (data->net_wm_state & (1 << i)) != 0 );
1059 xev.xclient.data.l[0] = (new_state & (1 << i)) ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1060 xev.xclient.data.l[1] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1061 xev.xclient.data.l[2] = ((state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT) ?
1062 x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ) : 0);
1063 wine_tsx11_lock();
1064 XSendEvent( display, root_window, False,
1065 SubstructureRedirectMask | SubstructureNotifyMask, &xev );
1066 wine_tsx11_unlock();
1069 data->net_wm_state = new_state;
1073 /***********************************************************************
1074 * set_xembed_flags
1076 static void set_xembed_flags( Display *display, struct x11drv_win_data *data, unsigned long flags )
1078 unsigned long info[2];
1080 info[0] = 0; /* protocol version */
1081 info[1] = flags;
1082 wine_tsx11_lock();
1083 XChangeProperty( display, data->whole_window, x11drv_atom(_XEMBED_INFO),
1084 x11drv_atom(_XEMBED_INFO), 32, PropModeReplace, (unsigned char*)info, 2 );
1085 wine_tsx11_unlock();
1089 /***********************************************************************
1090 * map_window
1092 static void map_window( Display *display, struct x11drv_win_data *data, DWORD new_style )
1094 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1096 wait_for_withdrawn_state( display, data, TRUE );
1098 if (!data->embedded)
1100 update_net_wm_states( display, data );
1101 sync_window_style( display, data );
1102 wine_tsx11_lock();
1103 XMapWindow( display, data->whole_window );
1104 wine_tsx11_unlock();
1106 else set_xembed_flags( display, data, XEMBED_MAPPED );
1108 data->mapped = TRUE;
1109 data->iconic = (new_style & WS_MINIMIZE) != 0;
1113 /***********************************************************************
1114 * unmap_window
1116 static void unmap_window( Display *display, struct x11drv_win_data *data )
1118 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1120 if (!data->embedded)
1122 wait_for_withdrawn_state( display, data, FALSE );
1123 wine_tsx11_lock();
1124 if (data->managed) XWithdrawWindow( display, data->whole_window, DefaultScreen(display) );
1125 else XUnmapWindow( display, data->whole_window );
1126 wine_tsx11_unlock();
1128 else set_xembed_flags( display, data, 0 );
1130 data->mapped = FALSE;
1131 data->net_wm_state = 0;
1135 /***********************************************************************
1136 * make_window_embedded
1138 void make_window_embedded( Display *display, struct x11drv_win_data *data )
1140 if (data->mapped)
1142 /* the window cannot be mapped before being embedded */
1143 unmap_window( display, data );
1144 data->embedded = TRUE;
1145 map_window( display, data, 0 );
1147 else
1149 data->embedded = TRUE;
1150 set_xembed_flags( display, data, 0 );
1155 /***********************************************************************
1156 * X11DRV_window_to_X_rect
1158 * Convert a rect from client to X window coordinates
1160 void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
1162 RECT rc;
1164 if (!data->managed) return;
1165 if (IsRectEmpty( rect )) return;
1167 get_x11_rect_offset( data, &rc );
1169 rect->left -= rc.left;
1170 rect->right -= rc.right;
1171 rect->top -= rc.top;
1172 rect->bottom -= rc.bottom;
1173 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1174 if (rect->left >= rect->right) rect->right = rect->left + 1;
1178 /***********************************************************************
1179 * X11DRV_X_to_window_rect
1181 * Opposite of X11DRV_window_to_X_rect
1183 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
1185 RECT rc;
1187 if (!data->managed) return;
1188 if (IsRectEmpty( rect )) return;
1190 get_x11_rect_offset( data, &rc );
1192 rect->left += rc.left;
1193 rect->right += rc.right;
1194 rect->top += rc.top;
1195 rect->bottom += rc.bottom;
1196 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1197 if (rect->left >= rect->right) rect->right = rect->left + 1;
1201 /***********************************************************************
1202 * sync_window_position
1204 * Synchronize the X window position with the Windows one
1206 static void sync_window_position( Display *display, struct x11drv_win_data *data,
1207 UINT swp_flags, const RECT *old_client_rect,
1208 const RECT *old_whole_rect )
1210 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
1211 XWindowChanges changes;
1212 unsigned int mask = 0;
1214 if (data->managed && data->iconic) return;
1216 /* resizing a managed maximized window is not allowed */
1217 if (!(style & WS_MAXIMIZE) || !data->managed)
1219 if ((changes.width = data->whole_rect.right - data->whole_rect.left) <= 0) changes.width = 1;
1220 if ((changes.height = data->whole_rect.bottom - data->whole_rect.top) <= 0) changes.height = 1;
1221 mask |= CWWidth | CWHeight;
1224 /* only the size is allowed to change for the desktop window */
1225 if (data->whole_window != root_window)
1227 changes.x = data->whole_rect.left - virtual_screen_rect.left;
1228 changes.y = data->whole_rect.top - virtual_screen_rect.top;
1229 mask |= CWX | CWY;
1232 if (!(swp_flags & SWP_NOZORDER) || (swp_flags & SWP_SHOWWINDOW))
1234 /* find window that this one must be after */
1235 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
1236 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
1237 prev = GetWindow( prev, GW_HWNDPREV );
1238 if (!prev) /* top child */
1240 changes.stack_mode = Above;
1241 mask |= CWStackMode;
1243 /* should use stack_mode Below but most window managers don't get it right */
1244 /* and Above with a sibling doesn't work so well either, so we ignore it */
1247 TRACE( "setting win %p/%lx pos %d,%d,%dx%d after %lx changes=%x\n",
1248 data->hwnd, data->whole_window, data->whole_rect.left, data->whole_rect.top,
1249 data->whole_rect.right - data->whole_rect.left,
1250 data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
1252 wine_tsx11_lock();
1253 set_size_hints( display, data, style );
1254 XReconfigureWMWindow( display, data->whole_window,
1255 DefaultScreen(display), mask, &changes );
1256 wine_tsx11_unlock();
1260 /***********************************************************************
1261 * sync_client_position
1263 * Synchronize the X client window position with the Windows one
1265 static void sync_client_position( Display *display, struct x11drv_win_data *data,
1266 UINT swp_flags, const RECT *old_client_rect,
1267 const RECT *old_whole_rect )
1269 int mask;
1270 XWindowChanges changes;
1271 RECT old = *old_client_rect;
1272 RECT new = data->client_rect;
1274 OffsetRect( &old, -old_whole_rect->left, -old_whole_rect->top );
1275 OffsetRect( &new, -data->whole_rect.left, -data->whole_rect.top );
1276 if (!(mask = get_window_changes( &changes, &old, &new ))) return;
1278 if (data->client_window)
1280 TRACE( "setting client win %lx pos %d,%d,%dx%d changes=%x\n",
1281 data->client_window, new.left, new.top,
1282 new.right - new.left, new.bottom - new.top, mask );
1283 wine_tsx11_lock();
1284 XConfigureWindow( display, data->client_window, mask, &changes );
1285 wine_tsx11_unlock();
1288 if (data->gl_drawable && (mask & (CWWidth|CWHeight))) sync_gl_drawable( data );
1292 /***********************************************************************
1293 * move_window_bits
1295 * Move the window bits when a window is moved.
1297 static void move_window_bits( struct x11drv_win_data *data, const RECT *old_rect, const RECT *new_rect,
1298 const RECT *old_client_rect )
1300 RECT src_rect = *old_rect;
1301 RECT dst_rect = *new_rect;
1302 HDC hdc_src, hdc_dst;
1303 INT code;
1304 HRGN rgn = 0;
1305 HWND parent = 0;
1307 if (!data->whole_window)
1309 OffsetRect( &dst_rect, -data->window_rect.left, -data->window_rect.top );
1310 parent = GetAncestor( data->hwnd, GA_PARENT );
1311 hdc_src = GetDCEx( parent, 0, DCX_CACHE );
1312 hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE | DCX_WINDOW );
1314 else
1316 OffsetRect( &dst_rect, -data->client_rect.left, -data->client_rect.top );
1317 /* make src rect relative to the old position of the window */
1318 OffsetRect( &src_rect, -old_client_rect->left, -old_client_rect->top );
1319 if (dst_rect.left == src_rect.left && dst_rect.top == src_rect.top) return;
1320 hdc_src = hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE );
1323 code = X11DRV_START_EXPOSURES;
1324 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
1326 TRACE( "copying bits for win %p/%lx/%lx %s -> %s\n",
1327 data->hwnd, data->whole_window, data->client_window,
1328 wine_dbgstr_rect(&src_rect), wine_dbgstr_rect(&dst_rect) );
1329 BitBlt( hdc_dst, dst_rect.left, dst_rect.top,
1330 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top,
1331 hdc_src, src_rect.left, src_rect.top, SRCCOPY );
1333 code = X11DRV_END_EXPOSURES;
1334 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, sizeof(rgn), (LPSTR)&rgn );
1336 ReleaseDC( data->hwnd, hdc_dst );
1337 if (hdc_src != hdc_dst) ReleaseDC( parent, hdc_src );
1339 if (rgn)
1341 if (!data->whole_window)
1343 /* map region to client rect since we are using DCX_WINDOW */
1344 OffsetRgn( rgn, data->window_rect.left - data->client_rect.left,
1345 data->window_rect.top - data->client_rect.top );
1346 RedrawWindow( data->hwnd, NULL, rgn,
1347 RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ALLCHILDREN );
1349 else RedrawWindow( data->hwnd, NULL, rgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1350 DeleteObject( rgn );
1355 /**********************************************************************
1356 * create_whole_window
1358 * Create the whole X window for a given window
1360 static Window create_whole_window( Display *display, struct x11drv_win_data *data )
1362 int cx, cy, mask;
1363 XSetWindowAttributes attr;
1364 WCHAR text[1024];
1365 COLORREF key;
1366 BYTE alpha;
1367 DWORD layered_flags;
1369 if (!(cx = data->window_rect.right - data->window_rect.left)) cx = 1;
1370 if (!(cy = data->window_rect.bottom - data->window_rect.top)) cy = 1;
1372 if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1374 TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1375 data->managed = TRUE;
1376 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1379 mask = get_window_attributes( display, data, &attr );
1381 wine_tsx11_lock();
1383 data->whole_rect = data->window_rect;
1384 data->whole_window = XCreateWindow( display, root_window,
1385 data->window_rect.left - virtual_screen_rect.left,
1386 data->window_rect.top - virtual_screen_rect.top,
1387 cx, cy, 0, screen_depth, InputOutput,
1388 visual, mask, &attr );
1390 if (data->whole_window) XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
1391 wine_tsx11_unlock();
1393 if (!data->whole_window) return 0;
1395 if (!create_client_window( display, data, NULL ))
1397 wine_tsx11_lock();
1398 XDeleteContext( display, data->whole_window, winContext );
1399 XDestroyWindow( display, data->whole_window );
1400 data->whole_window = 0;
1401 wine_tsx11_unlock();
1402 return 0;
1405 set_initial_wm_hints( display, data );
1406 set_wm_hints( display, data );
1408 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1410 /* set the window text */
1411 if (!InternalGetWindowText( data->hwnd, text, sizeof(text)/sizeof(WCHAR) )) text[0] = 0;
1412 sync_window_text( display, data->whole_window, text );
1414 /* set the window region */
1415 sync_window_region( display, data, (HRGN)1 );
1417 /* set the window opacity */
1418 if (!GetLayeredWindowAttributes( data->hwnd, &key, &alpha, &layered_flags )) layered_flags = 0;
1419 sync_window_opacity( display, data->whole_window, key, alpha, layered_flags );
1421 wine_tsx11_lock();
1422 XFlush( display ); /* make sure the window exists before we start painting to it */
1423 wine_tsx11_unlock();
1424 return data->whole_window;
1428 /**********************************************************************
1429 * destroy_whole_window
1431 * Destroy the whole X window for a given window.
1433 static void destroy_whole_window( Display *display, struct x11drv_win_data *data, BOOL already_destroyed )
1435 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1437 if (!data->whole_window) return;
1439 TRACE( "win %p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1440 if (thread_data->cursor_window == data->whole_window ||
1441 thread_data->cursor_window == data->client_window)
1442 thread_data->cursor_window = None;
1443 wine_tsx11_lock();
1444 XDeleteContext( display, data->whole_window, winContext );
1445 XDeleteContext( display, data->client_window, winContext );
1446 if (!already_destroyed) XDestroyWindow( display, data->whole_window );
1447 data->whole_window = data->client_window = 0;
1448 data->wm_state = WithdrawnState;
1449 data->net_wm_state = 0;
1450 data->mapped = FALSE;
1451 if (data->xic)
1453 XUnsetICFocus( data->xic );
1454 XDestroyIC( data->xic );
1455 data->xic = 0;
1457 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1458 XFlush( display );
1459 XFree( data->wm_hints );
1460 data->wm_hints = NULL;
1461 wine_tsx11_unlock();
1462 RemovePropA( data->hwnd, whole_window_prop );
1463 RemovePropA( data->hwnd, client_window_prop );
1467 /*****************************************************************
1468 * SetWindowText (X11DRV.@)
1470 void X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1472 Window win;
1474 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(gdi_display))
1476 Display *display = thread_init_display();
1477 sync_window_text( display, win, text );
1482 /***********************************************************************
1483 * SetWindowStyle (X11DRV.@)
1485 * Update the X state of a window to reflect a style change
1487 void X11DRV_SetWindowStyle( HWND hwnd, INT offset, STYLESTRUCT *style )
1489 struct x11drv_win_data *data;
1490 DWORD changed;
1492 if (hwnd == GetDesktopWindow()) return;
1493 changed = style->styleNew ^ style->styleOld;
1495 if (offset == GWL_STYLE && (changed & WS_VISIBLE) && (style->styleNew & WS_VISIBLE))
1497 /* we don't unmap windows, that causes trouble with the window manager */
1498 if (!(data = X11DRV_get_win_data( hwnd )) &&
1499 !(data = X11DRV_create_win_data( hwnd ))) return;
1501 if (data->whole_window && is_window_rect_mapped( &data->window_rect ))
1503 Display *display = thread_display();
1504 set_wm_hints( display, data );
1505 if (!data->mapped) map_window( display, data, style->styleNew );
1509 if (offset == GWL_STYLE && (changed & WS_DISABLED))
1511 data = X11DRV_get_win_data( hwnd );
1512 if (data && data->wm_hints)
1514 wine_tsx11_lock();
1515 data->wm_hints->input = !(style->styleNew & WS_DISABLED);
1516 XSetWMHints( thread_display(), data->whole_window, data->wm_hints );
1517 wine_tsx11_unlock();
1521 if (offset == GWL_EXSTYLE && (changed & WS_EX_LAYERED))
1523 /* changing WS_EX_LAYERED resets attributes */
1524 if ((data = X11DRV_get_win_data( hwnd )) && data->whole_window)
1525 sync_window_opacity( thread_display(), data->whole_window, 0, 0, 0 );
1530 /***********************************************************************
1531 * DestroyWindow (X11DRV.@)
1533 void X11DRV_DestroyWindow( HWND hwnd )
1535 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1536 struct x11drv_win_data *data;
1538 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1540 if (data->pixmap)
1542 wine_tsx11_lock();
1543 destroy_glxpixmap(gdi_display, data->gl_drawable);
1544 XFreePixmap(gdi_display, data->pixmap);
1545 wine_tsx11_unlock();
1547 else if (data->gl_drawable)
1549 wine_tsx11_lock();
1550 XDestroyWindow(gdi_display, data->gl_drawable);
1551 wine_tsx11_unlock();
1554 destroy_whole_window( thread_data->display, data, FALSE );
1555 destroy_icon_window( thread_data->display, data );
1557 if (data->colormap)
1559 wine_tsx11_lock();
1560 XFreeColormap( thread_data->display, data->colormap );
1561 wine_tsx11_unlock();
1564 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1565 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
1566 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
1567 wine_tsx11_lock();
1568 XDeleteContext( thread_data->display, (XID)hwnd, win_data_context );
1569 wine_tsx11_unlock();
1570 HeapFree( GetProcessHeap(), 0, data );
1574 /***********************************************************************
1575 * X11DRV_DestroyNotify
1577 void X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1579 Display *display = event->xdestroywindow.display;
1580 struct x11drv_win_data *data;
1582 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1584 FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1585 destroy_whole_window( display, data, TRUE );
1589 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
1591 struct x11drv_win_data *data;
1593 if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
1595 data->hwnd = hwnd;
1596 wine_tsx11_lock();
1597 if (!winContext) winContext = XUniqueContext();
1598 if (!win_data_context) win_data_context = XUniqueContext();
1599 XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
1600 wine_tsx11_unlock();
1602 return data;
1606 /* initialize the desktop window id in the desktop manager process */
1607 static struct x11drv_win_data *create_desktop_win_data( Display *display, HWND hwnd )
1609 struct x11drv_win_data *data;
1611 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1612 data->whole_window = data->client_window = root_window;
1613 data->managed = TRUE;
1614 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1615 SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
1616 SetPropA( data->hwnd, client_window_prop, (HANDLE)root_window );
1617 set_initial_wm_hints( display, data );
1618 return data;
1621 /**********************************************************************
1622 * CreateDesktopWindow (X11DRV.@)
1624 BOOL X11DRV_CreateDesktopWindow( HWND hwnd )
1626 unsigned int width, height;
1628 /* retrieve the real size of the desktop */
1629 SERVER_START_REQ( get_window_rectangles )
1631 req->handle = hwnd;
1632 wine_server_call( req );
1633 width = reply->window.right - reply->window.left;
1634 height = reply->window.bottom - reply->window.top;
1636 SERVER_END_REQ;
1638 if (!width && !height) /* not initialized yet */
1640 SERVER_START_REQ( set_window_pos )
1642 req->handle = hwnd;
1643 req->previous = 0;
1644 req->flags = SWP_NOZORDER;
1645 req->window.left = virtual_screen_rect.left;
1646 req->window.top = virtual_screen_rect.top;
1647 req->window.right = virtual_screen_rect.right;
1648 req->window.bottom = virtual_screen_rect.bottom;
1649 req->client = req->window;
1650 wine_server_call( req );
1652 SERVER_END_REQ;
1654 else
1656 Window win = (Window)GetPropA( hwnd, whole_window_prop );
1657 if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1659 return TRUE;
1663 /**********************************************************************
1664 * CreateWindow (X11DRV.@)
1666 BOOL X11DRV_CreateWindow( HWND hwnd )
1668 if (hwnd == GetDesktopWindow() && root_window != DefaultRootWindow( gdi_display ))
1670 Display *display = thread_init_display();
1672 /* the desktop win data can't be created lazily */
1673 if (!create_desktop_win_data( display, hwnd )) return FALSE;
1675 return TRUE;
1679 /***********************************************************************
1680 * X11DRV_get_win_data
1682 * Return the X11 data structure associated with a window.
1684 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1686 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1687 char *data;
1689 if (!thread_data) return NULL;
1690 if (!hwnd) return NULL;
1691 if (XFindContext( thread_data->display, (XID)hwnd, win_data_context, &data )) data = NULL;
1692 return (struct x11drv_win_data *)data;
1696 /***********************************************************************
1697 * X11DRV_create_win_data
1699 * Create an X11 data window structure for an existing window.
1701 struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd )
1703 Display *display;
1704 struct x11drv_win_data *data;
1705 HWND parent;
1707 if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL; /* desktop */
1709 /* don't create win data for HWND_MESSAGE windows */
1710 if (parent != GetDesktopWindow() && !GetAncestor( parent, GA_PARENT )) return NULL;
1712 display = thread_init_display();
1713 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1715 GetWindowRect( hwnd, &data->window_rect );
1716 MapWindowPoints( 0, parent, (POINT *)&data->window_rect, 2 );
1717 data->whole_rect = data->window_rect;
1718 GetClientRect( hwnd, &data->client_rect );
1719 MapWindowPoints( hwnd, parent, (POINT *)&data->client_rect, 2 );
1721 if (parent == GetDesktopWindow())
1723 if (!create_whole_window( display, data ))
1725 HeapFree( GetProcessHeap(), 0, data );
1726 return NULL;
1728 TRACE( "win %p/%lx/%lx window %s whole %s client %s\n",
1729 hwnd, data->whole_window, data->client_window, wine_dbgstr_rect( &data->window_rect ),
1730 wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
1732 return data;
1736 /***********************************************************************
1737 * X11DRV_get_whole_window
1739 * Return the X window associated with the full area of a window
1741 Window X11DRV_get_whole_window( HWND hwnd )
1743 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1745 if (!data)
1747 if (hwnd == GetDesktopWindow()) return root_window;
1748 return (Window)GetPropA( hwnd, whole_window_prop );
1750 return data->whole_window;
1754 /***********************************************************************
1755 * X11DRV_get_client_window
1757 * Return the X window associated with the client area of a window
1759 Window X11DRV_get_client_window( HWND hwnd )
1761 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1763 if (!data)
1765 if (hwnd == GetDesktopWindow()) return root_window;
1766 return (Window)GetPropA( hwnd, client_window_prop );
1768 return data->client_window;
1772 /***********************************************************************
1773 * X11DRV_get_ic
1775 * Return the X input context associated with a window
1777 XIC X11DRV_get_ic( HWND hwnd )
1779 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1780 XIM xim;
1782 if (!data) return 0;
1783 if (data->xic) return data->xic;
1784 if (!(xim = x11drv_thread_data()->xim)) return 0;
1785 return X11DRV_CreateIC( xim, data );
1789 /***********************************************************************
1790 * X11DRV_GetDC (X11DRV.@)
1792 void X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
1793 const RECT *top_rect, DWORD flags )
1795 struct x11drv_escape_set_drawable escape;
1796 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1798 escape.code = X11DRV_SET_DRAWABLE;
1799 escape.mode = IncludeInferiors;
1800 escape.fbconfig_id = 0;
1801 escape.gl_drawable = 0;
1802 escape.pixmap = 0;
1803 escape.gl_copy = FALSE;
1805 if (top == hwnd && data && IsIconic( hwnd ) && data->icon_window)
1807 escape.drawable = data->icon_window;
1809 else if (top == hwnd)
1811 escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
1812 /* GL draws to the client area even for window DCs */
1813 escape.gl_drawable = data ? data->client_window : X11DRV_get_client_window( hwnd );
1814 if (flags & DCX_WINDOW)
1815 escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
1816 else
1817 escape.drawable = escape.gl_drawable;
1819 else
1821 escape.drawable = X11DRV_get_client_window( top );
1822 escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
1823 escape.gl_drawable = data ? data->gl_drawable : (Drawable)GetPropA( hwnd, gl_drawable_prop );
1824 escape.pixmap = data ? data->pixmap : (Pixmap)GetPropA( hwnd, pixmap_prop );
1825 escape.gl_copy = (escape.gl_drawable != 0);
1826 if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
1829 escape.dc_rect.left = win_rect->left - top_rect->left;
1830 escape.dc_rect.top = win_rect->top - top_rect->top;
1831 escape.dc_rect.right = win_rect->right - top_rect->left;
1832 escape.dc_rect.bottom = win_rect->bottom - top_rect->top;
1833 escape.drawable_rect.left = top_rect->left;
1834 escape.drawable_rect.top = top_rect->top;
1835 escape.drawable_rect.right = top_rect->right;
1836 escape.drawable_rect.bottom = top_rect->bottom;
1838 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1842 /***********************************************************************
1843 * X11DRV_ReleaseDC (X11DRV.@)
1845 void X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
1847 struct x11drv_escape_set_drawable escape;
1849 escape.code = X11DRV_SET_DRAWABLE;
1850 escape.drawable = root_window;
1851 escape.mode = IncludeInferiors;
1852 escape.drawable_rect = virtual_screen_rect;
1853 SetRect( &escape.dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
1854 virtual_screen_rect.bottom - virtual_screen_rect.top );
1855 escape.fbconfig_id = 0;
1856 escape.gl_drawable = 0;
1857 escape.pixmap = 0;
1858 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1862 /***********************************************************************
1863 * SetCapture (X11DRV.@)
1865 void X11DRV_SetCapture( HWND hwnd, UINT flags )
1867 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1869 if (!thread_data) return;
1870 if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
1872 if (hwnd)
1874 Window grab_win = X11DRV_get_client_window( GetAncestor( hwnd, GA_ROOT ) );
1876 if (!grab_win) return;
1877 wine_tsx11_lock();
1878 XFlush( gdi_display );
1879 XGrabPointer( thread_data->display, grab_win, False,
1880 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1881 GrabModeAsync, GrabModeAsync, None, None, CurrentTime );
1882 wine_tsx11_unlock();
1883 thread_data->grab_window = grab_win;
1885 else /* release capture */
1887 wine_tsx11_lock();
1888 XFlush( gdi_display );
1889 XUngrabPointer( thread_data->display, CurrentTime );
1890 wine_tsx11_unlock();
1891 thread_data->grab_window = None;
1896 /*****************************************************************
1897 * SetParent (X11DRV.@)
1899 void X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
1901 Display *display = thread_display();
1902 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1904 if (!data) return;
1905 if (parent == old_parent) return;
1907 if (parent != GetDesktopWindow()) /* a child window */
1909 if (old_parent == GetDesktopWindow())
1911 /* destroy the old X windows */
1912 destroy_whole_window( display, data, FALSE );
1913 destroy_icon_window( display, data );
1914 if (data->managed)
1916 data->managed = FALSE;
1917 RemovePropA( data->hwnd, managed_prop );
1921 else /* new top level window */
1923 /* FIXME: we ignore errors since we can't really recover anyway */
1924 create_whole_window( display, data );
1929 /*****************************************************************
1930 * SetFocus (X11DRV.@)
1932 * Set the X focus.
1934 void X11DRV_SetFocus( HWND hwnd )
1936 Display *display = thread_display();
1937 struct x11drv_win_data *data;
1938 XWindowChanges changes;
1940 if (!(hwnd = GetAncestor( hwnd, GA_ROOT ))) return;
1941 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1942 if (data->managed || !data->whole_window) return;
1944 /* Set X focus and install colormap */
1945 wine_tsx11_lock();
1946 changes.stack_mode = Above;
1947 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
1948 if (root_window == DefaultRootWindow(display))
1950 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1951 /* FIXME: this is not entirely correct */
1952 XSetInputFocus( display, data->whole_window, RevertToParent,
1953 /* CurrentTime */
1954 GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1956 wine_tsx11_unlock();
1960 /***********************************************************************
1961 * WindowPosChanging (X11DRV.@)
1963 void X11DRV_WindowPosChanging( HWND hwnd, HWND insert_after, UINT swp_flags,
1964 const RECT *window_rect, const RECT *client_rect, RECT *visible_rect )
1966 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1967 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
1969 if (!data)
1971 /* create the win data if the window is being made visible */
1972 if (!(style & WS_VISIBLE) && !(swp_flags & SWP_SHOWWINDOW)) return;
1973 if (!(data = X11DRV_create_win_data( hwnd ))) return;
1976 /* check if we need to switch the window to managed */
1977 if (!data->managed && data->whole_window && is_window_managed( hwnd, swp_flags, window_rect ))
1979 TRACE( "making win %p/%lx managed\n", hwnd, data->whole_window );
1980 if (data->mapped) unmap_window( thread_display(), data );
1981 data->managed = TRUE;
1982 SetPropA( hwnd, managed_prop, (HANDLE)1 );
1985 *visible_rect = *window_rect;
1986 X11DRV_window_to_X_rect( data, visible_rect );
1990 /***********************************************************************
1991 * WindowPosChanged (X11DRV.@)
1993 void X11DRV_WindowPosChanged( HWND hwnd, HWND insert_after, UINT swp_flags,
1994 const RECT *rectWindow, const RECT *rectClient,
1995 const RECT *visible_rect, const RECT *valid_rects )
1997 struct x11drv_thread_data *thread_data;
1998 Display *display;
1999 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2000 DWORD new_style = GetWindowLongW( hwnd, GWL_STYLE );
2001 RECT old_whole_rect, old_client_rect;
2002 int event_type;
2004 if (!data) return;
2006 thread_data = x11drv_thread_data();
2007 display = thread_data->display;
2009 old_whole_rect = data->whole_rect;
2010 old_client_rect = data->client_rect;
2011 data->window_rect = *rectWindow;
2012 data->whole_rect = *visible_rect;
2013 data->client_rect = *rectClient;
2015 TRACE( "win %p window %s client %s style %08x flags %08x\n",
2016 hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style, swp_flags );
2018 if (!IsRectEmpty( &valid_rects[0] ))
2020 int x_offset = old_whole_rect.left - data->whole_rect.left;
2021 int y_offset = old_whole_rect.top - data->whole_rect.top;
2023 /* if all that happened is that the whole window moved, copy everything */
2024 if (!(swp_flags & SWP_FRAMECHANGED) &&
2025 old_whole_rect.right - data->whole_rect.right == x_offset &&
2026 old_whole_rect.bottom - data->whole_rect.bottom == y_offset &&
2027 old_client_rect.left - data->client_rect.left == x_offset &&
2028 old_client_rect.right - data->client_rect.right == x_offset &&
2029 old_client_rect.top - data->client_rect.top == y_offset &&
2030 old_client_rect.bottom - data->client_rect.bottom == y_offset &&
2031 !memcmp( &valid_rects[0], &data->client_rect, sizeof(RECT) ))
2033 /* if we have an X window the bits will be moved by the X server */
2034 if (!data->whole_window)
2035 move_window_bits( data, &old_whole_rect, &data->whole_rect, &old_client_rect );
2037 else
2038 move_window_bits( data, &valid_rects[1], &valid_rects[0], &old_client_rect );
2041 wine_tsx11_lock();
2042 XFlush( gdi_display ); /* make sure painting is done before we move the window */
2043 wine_tsx11_unlock();
2045 sync_client_position( display, data, swp_flags, &old_client_rect, &old_whole_rect );
2047 if (!data->whole_window) return;
2049 /* check if we are currently processing an event relevant to this window */
2050 event_type = 0;
2051 if (thread_data->current_event && thread_data->current_event->xany.window == data->whole_window)
2052 event_type = thread_data->current_event->type;
2054 if (event_type != ConfigureNotify && event_type != PropertyNotify)
2055 event_type = 0; /* ignore other events */
2057 if (data->mapped)
2059 if (((swp_flags & SWP_HIDEWINDOW) && !(new_style & WS_VISIBLE)) ||
2060 (!event_type && !is_window_rect_mapped( rectWindow )))
2061 unmap_window( display, data );
2064 /* don't change position if we are about to minimize or maximize a managed window */
2065 if (!event_type &&
2066 !(data->managed && (swp_flags & SWP_STATECHANGED) && (new_style & (WS_MINIMIZE|WS_MAXIMIZE))))
2067 sync_window_position( display, data, swp_flags, &old_client_rect, &old_whole_rect );
2069 if ((new_style & WS_VISIBLE) &&
2070 ((new_style & WS_MINIMIZE) || is_window_rect_mapped( rectWindow )))
2072 if (!data->mapped || (swp_flags & (SWP_FRAMECHANGED|SWP_STATECHANGED)))
2073 set_wm_hints( display, data );
2075 if (!data->mapped)
2077 map_window( display, data, new_style );
2079 else if ((swp_flags & SWP_STATECHANGED) && (!data->iconic != !(new_style & WS_MINIMIZE)))
2081 data->iconic = (new_style & WS_MINIMIZE) != 0;
2082 TRACE( "changing win %p iconic state to %u\n", data->hwnd, data->iconic );
2083 wine_tsx11_lock();
2084 if (data->iconic)
2085 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
2086 else if (is_window_rect_mapped( rectWindow ))
2087 XMapWindow( display, data->whole_window );
2088 wine_tsx11_unlock();
2089 update_net_wm_states( display, data );
2091 else if (!event_type)
2093 update_net_wm_states( display, data );
2097 wine_tsx11_lock();
2098 XFlush( display ); /* make sure changes are done before we start painting again */
2099 wine_tsx11_unlock();
2103 /***********************************************************************
2104 * ShowWindow (X11DRV.@)
2106 UINT X11DRV_ShowWindow( HWND hwnd, INT cmd, RECT *rect, UINT swp )
2108 int x, y;
2109 unsigned int width, height, border, depth;
2110 Window root, top;
2111 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
2112 struct x11drv_thread_data *thread_data = x11drv_thread_data();
2113 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2115 if (!data || !data->whole_window || !data->managed || !data->mapped || data->iconic) return swp;
2116 if (style & WS_MINIMIZE) return swp;
2118 /* only fetch the new rectangle if the ShowWindow was a result of a window manager event */
2120 if (!thread_data->current_event || thread_data->current_event->xany.window != data->whole_window)
2121 return swp;
2123 if (thread_data->current_event->type != ConfigureNotify &&
2124 thread_data->current_event->type != PropertyNotify)
2125 return swp;
2127 TRACE( "win %p/%lx cmd %d at %s flags %08x\n",
2128 hwnd, data->whole_window, cmd, wine_dbgstr_rect(rect), swp );
2130 wine_tsx11_lock();
2131 XGetGeometry( thread_data->display, data->whole_window,
2132 &root, &x, &y, &width, &height, &border, &depth );
2133 XTranslateCoordinates( thread_data->display, data->whole_window, root, 0, 0, &x, &y, &top );
2134 wine_tsx11_unlock();
2135 rect->left = x;
2136 rect->top = y;
2137 rect->right = x + width;
2138 rect->bottom = y + height;
2139 OffsetRect( rect, virtual_screen_rect.left, virtual_screen_rect.top );
2140 X11DRV_X_to_window_rect( data, rect );
2141 return swp & ~(SWP_NOMOVE | SWP_NOCLIENTMOVE | SWP_NOSIZE | SWP_NOCLIENTSIZE);
2145 /**********************************************************************
2146 * SetWindowIcon (X11DRV.@)
2148 * hIcon or hIconSm has changed (or is being initialised for the
2149 * first time). Complete the X11 driver-specific initialisation
2150 * and set the window hints.
2152 * This is not entirely correct, may need to create
2153 * an icon window and set the pixmap as a background
2155 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
2157 Display *display = thread_display();
2158 struct x11drv_win_data *data;
2160 if (type != ICON_BIG) return; /* nothing to do here */
2162 if (!(data = X11DRV_get_win_data( hwnd ))) return;
2163 if (!data->whole_window) return;
2164 if (!data->managed) return;
2166 if (data->wm_hints)
2168 set_icon_hints( display, data, icon );
2169 wine_tsx11_lock();
2170 XSetWMHints( display, data->whole_window, data->wm_hints );
2171 wine_tsx11_unlock();
2176 /***********************************************************************
2177 * SetWindowRgn (X11DRV.@)
2179 * Assign specified region to window (for non-rectangular windows)
2181 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
2183 struct x11drv_win_data *data;
2185 if ((data = X11DRV_get_win_data( hwnd )))
2187 sync_window_region( thread_display(), data, hrgn );
2189 else if (X11DRV_get_whole_window( hwnd ))
2191 SendMessageW( hwnd, WM_X11DRV_SET_WIN_REGION, 0, 0 );
2193 return TRUE;
2197 /***********************************************************************
2198 * SetLayeredWindowAttributes (X11DRV.@)
2200 * Set transparency attributes for a layered window.
2202 void X11DRV_SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
2204 Window win = X11DRV_get_whole_window( hwnd );
2206 if (win) sync_window_opacity( thread_display(), win, key, alpha, flags );
2210 /**********************************************************************
2211 * X11DRV_WindowMessage (X11DRV.@)
2213 LRESULT X11DRV_WindowMessage( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
2215 struct x11drv_win_data *data;
2217 switch(msg)
2219 case WM_X11DRV_ACQUIRE_SELECTION:
2220 return X11DRV_AcquireClipboard( hwnd );
2221 case WM_X11DRV_DELETE_WINDOW:
2222 return SendMessageW( hwnd, WM_SYSCOMMAND, SC_CLOSE, 0 );
2223 case WM_X11DRV_SET_WIN_FORMAT:
2224 return set_win_format( hwnd, (XID)wp );
2225 case WM_X11DRV_SET_WIN_REGION:
2226 if ((data = X11DRV_get_win_data( hwnd ))) sync_window_region( thread_display(), data, (HRGN)1 );
2227 return 0;
2228 case WM_X11DRV_RESIZE_DESKTOP:
2229 X11DRV_resize_desktop( LOWORD(lp), HIWORD(lp) );
2230 return 0;
2231 default:
2232 FIXME( "got window msg %x hwnd %p wp %lx lp %lx\n", msg, hwnd, wp, lp );
2233 return 0;
2238 /***********************************************************************
2239 * is_netwm_supported
2241 static BOOL is_netwm_supported( Display *display, Atom atom )
2243 static Atom *net_supported;
2244 static int net_supported_count = -1;
2245 int i;
2247 wine_tsx11_lock();
2248 if (net_supported_count == -1)
2250 Atom type;
2251 int format;
2252 unsigned long count, remaining;
2254 if (!XGetWindowProperty( display, DefaultRootWindow(display), x11drv_atom(_NET_SUPPORTED), 0,
2255 ~0UL, False, XA_ATOM, &type, &format, &count,
2256 &remaining, (unsigned char **)&net_supported ))
2257 net_supported_count = get_property_size( format, count ) / sizeof(Atom);
2258 else
2259 net_supported_count = 0;
2261 wine_tsx11_unlock();
2263 for (i = 0; i < net_supported_count; i++)
2264 if (net_supported[i] == atom) return TRUE;
2265 return FALSE;
2269 /***********************************************************************
2270 * SysCommand (X11DRV.@)
2272 * Perform WM_SYSCOMMAND handling.
2274 LRESULT X11DRV_SysCommand( HWND hwnd, WPARAM wparam, LPARAM lparam )
2276 WPARAM hittest = wparam & 0x0f;
2277 DWORD dwPoint;
2278 int x, y, dir;
2279 XEvent xev;
2280 Display *display = thread_display();
2281 struct x11drv_win_data *data;
2283 if (!(data = X11DRV_get_win_data( hwnd ))) return -1;
2284 if (!data->whole_window || !data->managed || !data->mapped) return -1;
2286 switch (wparam & 0xfff0)
2288 case SC_MOVE:
2289 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
2290 else dir = _NET_WM_MOVERESIZE_MOVE;
2291 break;
2292 case SC_SIZE:
2293 /* windows without WS_THICKFRAME are not resizable through the window manager */
2294 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_THICKFRAME)) return -1;
2296 switch (hittest)
2298 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
2299 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
2300 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
2301 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
2302 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
2303 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
2304 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
2305 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
2306 default: dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD; break;
2308 break;
2310 case SC_KEYMENU:
2311 /* prevent a simple ALT press+release from activating the system menu,
2312 * as that can get confusing on managed windows */
2313 if ((WCHAR)lparam) return -1; /* got an explicit char */
2314 if (GetMenu( hwnd )) return -1; /* window has a real menu */
2315 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) return -1; /* no system menu */
2316 TRACE( "ignoring SC_KEYMENU wp %lx lp %lx\n", wparam, lparam );
2317 return 0;
2319 default:
2320 return -1;
2323 if (IsZoomed(hwnd)) return -1;
2325 if (!is_netwm_supported( display, x11drv_atom(_NET_WM_MOVERESIZE) ))
2327 TRACE( "_NET_WM_MOVERESIZE not supported\n" );
2328 return -1;
2331 dwPoint = GetMessagePos();
2332 x = (short)LOWORD(dwPoint);
2333 y = (short)HIWORD(dwPoint);
2335 TRACE("hwnd %p, x %d, y %d, dir %d\n", hwnd, x, y, dir);
2337 xev.xclient.type = ClientMessage;
2338 xev.xclient.window = X11DRV_get_whole_window(hwnd);
2339 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
2340 xev.xclient.serial = 0;
2341 xev.xclient.display = display;
2342 xev.xclient.send_event = True;
2343 xev.xclient.format = 32;
2344 xev.xclient.data.l[0] = x - virtual_screen_rect.left; /* x coord */
2345 xev.xclient.data.l[1] = y - virtual_screen_rect.top; /* y coord */
2346 xev.xclient.data.l[2] = dir; /* direction */
2347 xev.xclient.data.l[3] = 1; /* button */
2348 xev.xclient.data.l[4] = 0; /* unused */
2350 /* need to ungrab the pointer that may have been automatically grabbed
2351 * with a ButtonPress event */
2352 wine_tsx11_lock();
2353 XUngrabPointer( display, CurrentTime );
2354 XSendEvent(display, root_window, False, SubstructureNotifyMask, &xev);
2355 wine_tsx11_unlock();
2356 return 0;