push 4e98c31ec75caed2ea3040ac2e710b58ba1ca0e1
[wine/hacks.git] / dlls / winex11.drv / window.c
blob39183e1cdbfb40dbf81c51b40aec6df355717d7b
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 #ifndef DEFAULTMANAGED
100 /* set to 1 to enable default managed instead of default not managed */
101 #define DEFAULTMANAGED 0
102 #endif
103 if(!DEFAULTMANAGED){
104 /* activated windows are managed */
105 if (!(swp_flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) return TRUE;
106 if (hwnd == GetActiveWindow()) return TRUE;
107 /* windows with caption are managed */
108 if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
109 /* windows with thick frame are managed */
110 if (style & WS_THICKFRAME) return TRUE;
112 if (style & WS_POPUP)
114 /* popup with sysmenu == caption are managed */
115 if (style & WS_SYSMENU) return TRUE;
116 /* full-screen popup windows are managed */
117 if (window_rect->left <= 0 && window_rect->right >= screen_width &&
118 window_rect->top <= 0 && window_rect->bottom >= screen_height)
119 return TRUE;
120 if(DEFAULTMANAGED) {
121 /* all other popups are not managed */
122 return FALSE;
125 /* application windows are managed */
126 ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
127 if (ex_style & WS_EX_APPWINDOW) return TRUE;
128 if(DEFAULTMANAGED) {
129 /* default: managed */
130 return TRUE;
131 } else {
132 /* default: not managed */
133 return FALSE;
139 /***********************************************************************
140 * X11DRV_is_window_rect_mapped
142 * Check if the X whole window should be mapped based on its rectangle
144 static BOOL is_window_rect_mapped( const RECT *rect )
146 /* don't map if rect is empty */
147 if (IsRectEmpty( rect )) return FALSE;
149 /* don't map if rect is off-screen */
150 if (rect->left >= virtual_screen_rect.right ||
151 rect->top >= virtual_screen_rect.bottom ||
152 rect->right <= virtual_screen_rect.left ||
153 rect->bottom <= virtual_screen_rect.top)
154 return FALSE;
156 return TRUE;
160 /***********************************************************************
161 * is_window_resizable
163 * Check if window should be made resizable by the window manager
165 static inline BOOL is_window_resizable( struct x11drv_win_data *data, DWORD style )
167 if (style & WS_THICKFRAME) return TRUE;
168 /* Metacity needs the window to be resizable to make it fullscreen */
169 return (data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
170 data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height);
174 /***********************************************************************
175 * get_window_owner
177 static HWND get_window_owner( HWND hwnd )
179 RECT rect;
180 HWND owner = GetWindow( hwnd, GW_OWNER );
181 /* ignore the zero-size owners used by Delphi apps */
182 if (owner && GetWindowRect( owner, &rect ) && IsRectEmpty( &rect )) owner = 0;
183 return owner;
187 /***********************************************************************
188 * get_mwm_decorations
190 static unsigned long get_mwm_decorations( struct x11drv_win_data *data,
191 DWORD style, DWORD ex_style )
193 unsigned long ret = 0;
195 if (!decorated_mode) return 0;
197 if (IsRectEmpty( &data->window_rect )) return 0;
198 if (data->shaped) return 0;
200 if (ex_style & WS_EX_TOOLWINDOW) return 0;
202 if ((style & WS_CAPTION) == WS_CAPTION)
204 ret |= MWM_DECOR_TITLE | MWM_DECOR_BORDER;
205 if (style & WS_SYSMENU) ret |= MWM_DECOR_MENU;
206 if (style & WS_MINIMIZEBOX) ret |= MWM_DECOR_MINIMIZE;
207 if (style & WS_MAXIMIZEBOX) ret |= MWM_DECOR_MAXIMIZE;
209 if (ex_style & WS_EX_DLGMODALFRAME) ret |= MWM_DECOR_BORDER;
210 else if (style & WS_THICKFRAME) ret |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
211 else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) ret |= MWM_DECOR_BORDER;
212 return ret;
216 /***********************************************************************
217 * get_x11_rect_offset
219 * Helper for X11DRV_window_to_X_rect and X11DRV_X_to_window_rect.
221 static void get_x11_rect_offset( struct x11drv_win_data *data, RECT *rect )
223 DWORD style, ex_style, style_mask = 0, ex_style_mask = 0;
224 unsigned long decor;
226 rect->top = rect->bottom = rect->left = rect->right = 0;
228 style = GetWindowLongW( data->hwnd, GWL_STYLE );
229 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
230 decor = get_mwm_decorations( data, style, ex_style );
232 if (decor & MWM_DECOR_TITLE) style_mask |= WS_CAPTION;
233 if (decor & MWM_DECOR_BORDER)
235 style_mask |= WS_DLGFRAME | WS_THICKFRAME;
236 ex_style_mask |= WS_EX_DLGMODALFRAME;
239 AdjustWindowRectEx( rect, style & style_mask, FALSE, ex_style & ex_style_mask );
243 /***********************************************************************
244 * get_window_attributes
246 * Fill the window attributes structure for an X window.
248 static int get_window_attributes( Display *display, struct x11drv_win_data *data,
249 XSetWindowAttributes *attr )
251 attr->override_redirect = !data->managed;
252 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
253 attr->save_under = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
254 attr->cursor = x11drv_thread_data()->cursor;
255 attr->bit_gravity = NorthWestGravity;
256 attr->win_gravity = StaticGravity;
257 attr->backing_store = NotUseful;
258 attr->event_mask = (ExposureMask | PointerMotionMask |
259 ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
260 KeyPressMask | KeyReleaseMask | FocusChangeMask | KeymapStateMask);
261 if (data->managed) attr->event_mask |= StructureNotifyMask | PropertyChangeMask;
263 return (CWOverrideRedirect | CWSaveUnder | CWColormap | CWCursor |
264 CWEventMask | CWBitGravity | CWBackingStore);
268 /***********************************************************************
269 * create_client_window
271 static Window create_client_window( Display *display, struct x11drv_win_data *data, XVisualInfo *vis )
273 int cx, cy, mask;
274 XSetWindowAttributes attr;
275 Window client;
276 Visual *client_visual = vis ? vis->visual : visual;
278 attr.bit_gravity = NorthWestGravity;
279 attr.win_gravity = NorthWestGravity;
280 attr.backing_store = NotUseful;
281 attr.event_mask = (ExposureMask | PointerMotionMask |
282 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
283 mask = CWEventMask | CWBitGravity | CWWinGravity | CWBackingStore;
285 if ((cx = data->client_rect.right - data->client_rect.left) <= 0) cx = 1;
286 if ((cy = data->client_rect.bottom - data->client_rect.top) <= 0) cy = 1;
288 wine_tsx11_lock();
290 if (vis)
292 attr.colormap = XCreateColormap( display, root_window, vis->visual,
293 (vis->class == PseudoColor || vis->class == GrayScale ||
294 vis->class == DirectColor) ? AllocAll : AllocNone );
295 mask |= CWColormap;
298 client = XCreateWindow( display, data->whole_window,
299 data->client_rect.left - data->whole_rect.left,
300 data->client_rect.top - data->whole_rect.top,
301 cx, cy, 0, screen_depth, InputOutput,
302 client_visual, mask, &attr );
303 if (!client)
305 wine_tsx11_unlock();
306 return 0;
309 if (data->client_window)
311 struct x11drv_thread_data *thread_data = x11drv_thread_data();
312 if (thread_data->cursor_window == data->client_window) thread_data->cursor_window = None;
313 XDeleteContext( display, data->client_window, winContext );
314 XDestroyWindow( display, data->client_window );
316 data->client_window = client;
317 data->visualid = XVisualIDFromVisual( client_visual );
319 if (data->colormap) XFreeColormap( display, data->colormap );
320 data->colormap = vis ? attr.colormap : 0;
322 XMapWindow( display, data->client_window );
323 XSaveContext( display, data->client_window, winContext, (char *)data->hwnd );
324 wine_tsx11_unlock();
326 SetPropA( data->hwnd, client_window_prop, (HANDLE)data->client_window );
327 return data->client_window;
331 /***********************************************************************
332 * sync_window_style
334 * Change the X window attributes when the window style has changed.
336 static void sync_window_style( Display *display, struct x11drv_win_data *data )
338 if (data->whole_window != root_window)
340 XSetWindowAttributes attr;
341 int mask = get_window_attributes( display, data, &attr );
343 wine_tsx11_lock();
344 XChangeWindowAttributes( display, data->whole_window, mask, &attr );
345 wine_tsx11_unlock();
350 /***********************************************************************
351 * sync_window_region
353 * Update the X11 window region.
355 static void sync_window_region( Display *display, struct x11drv_win_data *data, HRGN win_region )
357 #ifdef HAVE_LIBXSHAPE
358 HRGN hrgn = win_region;
360 if (!data->whole_window) return;
361 data->shaped = FALSE;
363 if (hrgn == (HRGN)1) /* hack: win_region == 1 means retrieve region from server */
365 if (!(hrgn = CreateRectRgn( 0, 0, 0, 0 ))) return;
366 if (GetWindowRgn( data->hwnd, hrgn ) == ERROR)
368 DeleteObject( hrgn );
369 hrgn = 0;
373 if (!hrgn)
375 wine_tsx11_lock();
376 XShapeCombineMask( display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
377 wine_tsx11_unlock();
379 else
381 RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
382 if (pRegionData)
384 wine_tsx11_lock();
385 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
386 data->window_rect.left - data->whole_rect.left,
387 data->window_rect.top - data->whole_rect.top,
388 (XRectangle *)pRegionData->Buffer,
389 pRegionData->rdh.nCount, ShapeSet, YXBanded );
390 wine_tsx11_unlock();
391 HeapFree(GetProcessHeap(), 0, pRegionData);
392 data->shaped = TRUE;
395 if (hrgn && hrgn != win_region) DeleteObject( hrgn );
396 #endif /* HAVE_LIBXSHAPE */
400 /***********************************************************************
401 * sync_window_opacity
403 static void sync_window_opacity( Display *display, Window win,
404 COLORREF key, BYTE alpha, DWORD flags )
406 unsigned long opacity = 0xffffffff;
408 if (flags & LWA_ALPHA) opacity = (0xffffffff / 0xff) * alpha;
410 if (flags & LWA_COLORKEY) FIXME("LWA_COLORKEY not supported\n");
412 wine_tsx11_lock();
413 if (opacity == 0xffffffff)
414 XDeleteProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY) );
415 else
416 XChangeProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY),
417 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&opacity, 1 );
418 wine_tsx11_unlock();
422 /***********************************************************************
423 * sync_window_text
425 static void sync_window_text( Display *display, Window win, const WCHAR *text )
427 UINT count;
428 char *buffer, *utf8_buffer;
429 XTextProperty prop;
431 /* allocate new buffer for window text */
432 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
433 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count ))) return;
434 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
436 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
437 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
439 HeapFree( GetProcessHeap(), 0, buffer );
440 return;
442 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
444 wine_tsx11_lock();
445 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
447 XSetWMName( display, win, &prop );
448 XSetWMIconName( display, win, &prop );
449 XFree( prop.value );
452 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
453 according to the standard
454 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
456 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
457 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
458 wine_tsx11_unlock();
460 HeapFree( GetProcessHeap(), 0, utf8_buffer );
461 HeapFree( GetProcessHeap(), 0, buffer );
465 /***********************************************************************
466 * set_win_format
468 static BOOL set_win_format( HWND hwnd, XID fbconfig_id )
470 struct x11drv_win_data *data;
471 XVisualInfo *vis;
472 int w, h;
474 if (!(data = X11DRV_get_win_data(hwnd)) &&
475 !(data = X11DRV_create_win_data(hwnd))) return FALSE;
477 if (!(vis = visual_from_fbconfig_id(fbconfig_id))) return FALSE;
479 if (data->whole_window)
481 Display *display = thread_display();
482 Window client = data->client_window;
484 if (vis->visualid != data->visualid)
486 client = create_client_window( display, data, vis );
487 TRACE( "re-created client window %lx for %p fbconfig %lx\n", client, data->hwnd, fbconfig_id );
489 wine_tsx11_lock();
490 XFree(vis);
491 XFlush( display );
492 wine_tsx11_unlock();
493 if (client) goto done;
494 return FALSE;
497 w = data->client_rect.right - data->client_rect.left;
498 h = data->client_rect.bottom - data->client_rect.top;
500 if(w <= 0) w = 1;
501 if(h <= 0) h = 1;
503 #ifdef SONAME_LIBXCOMPOSITE
504 if(usexcomposite)
506 XSetWindowAttributes attrib;
507 static Window dummy_parent;
509 wine_tsx11_lock();
510 attrib.override_redirect = True;
511 if (!dummy_parent)
513 dummy_parent = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, screen_depth,
514 InputOutput, visual, CWOverrideRedirect, &attrib );
515 XMapWindow( gdi_display, dummy_parent );
517 data->colormap = XCreateColormap(gdi_display, dummy_parent, vis->visual,
518 (vis->class == PseudoColor ||
519 vis->class == GrayScale ||
520 vis->class == DirectColor) ?
521 AllocAll : AllocNone);
522 attrib.colormap = data->colormap;
523 XInstallColormap(gdi_display, attrib.colormap);
525 if(data->gl_drawable) XDestroyWindow(gdi_display, data->gl_drawable);
526 data->gl_drawable = XCreateWindow(gdi_display, dummy_parent, -w, 0, w, h, 0,
527 vis->depth, InputOutput, vis->visual,
528 CWColormap | CWOverrideRedirect,
529 &attrib);
530 if(data->gl_drawable)
532 pXCompositeRedirectWindow(gdi_display, data->gl_drawable,
533 CompositeRedirectManual);
534 XMapWindow(gdi_display, data->gl_drawable);
536 XFree(vis);
537 XFlush( gdi_display );
538 wine_tsx11_unlock();
540 else
541 #endif
543 WARN("XComposite is not available, using GLXPixmap hack\n");
545 wine_tsx11_lock();
547 if(data->pixmap) XFreePixmap(gdi_display, data->pixmap);
548 data->pixmap = XCreatePixmap(gdi_display, root_window, w, h, vis->depth);
549 if(!data->pixmap)
551 XFree(vis);
552 wine_tsx11_unlock();
553 return FALSE;
556 if(data->gl_drawable) destroy_glxpixmap(gdi_display, data->gl_drawable);
557 data->gl_drawable = create_glxpixmap(gdi_display, vis, data->pixmap);
558 if(!data->gl_drawable)
560 XFreePixmap(gdi_display, data->pixmap);
561 data->pixmap = 0;
563 XFree(vis);
564 XFlush( gdi_display );
565 wine_tsx11_unlock();
566 if (data->pixmap) SetPropA(hwnd, pixmap_prop, (HANDLE)data->pixmap);
569 if (!data->gl_drawable) return FALSE;
571 TRACE("Created GL drawable 0x%lx, using FBConfigID 0x%lx\n",
572 data->gl_drawable, fbconfig_id);
573 SetPropA(hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
575 done:
576 data->fbconfig_id = fbconfig_id;
577 SetPropA(hwnd, fbconfig_id_prop, (HANDLE)data->fbconfig_id);
578 /* force DCE invalidation */
579 SetWindowPos( hwnd, 0, 0, 0, 0, 0,
580 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE |
581 SWP_NOREDRAW | SWP_DEFERERASE | SWP_NOSENDCHANGING | SWP_STATECHANGED);
582 return TRUE;
585 /***********************************************************************
586 * sync_gl_drawable
588 static void sync_gl_drawable(struct x11drv_win_data *data)
590 int w = data->client_rect.right - data->client_rect.left;
591 int h = data->client_rect.bottom - data->client_rect.top;
592 XVisualInfo *vis;
593 Drawable glxp;
594 Pixmap pix;
596 if (w <= 0) w = 1;
597 if (h <= 0) h = 1;
599 TRACE("Resizing GL drawable 0x%lx to %dx%d\n", data->gl_drawable, w, h);
600 #ifdef SONAME_LIBXCOMPOSITE
601 if(usexcomposite)
603 wine_tsx11_lock();
604 XMoveResizeWindow(gdi_display, data->gl_drawable, -w, 0, w, h);
605 wine_tsx11_unlock();
606 return;
608 #endif
610 if (!(vis = visual_from_fbconfig_id(data->fbconfig_id))) return;
612 wine_tsx11_lock();
613 pix = XCreatePixmap(gdi_display, root_window, w, h, vis->depth);
614 if(!pix)
616 ERR("Failed to create pixmap for offscreen rendering\n");
617 XFree(vis);
618 wine_tsx11_unlock();
619 return;
622 glxp = create_glxpixmap(gdi_display, vis, pix);
623 if(!glxp)
625 ERR("Failed to create drawable for offscreen rendering\n");
626 XFreePixmap(gdi_display, pix);
627 XFree(vis);
628 wine_tsx11_unlock();
629 return;
632 XFree(vis);
634 mark_drawable_dirty(data->gl_drawable, glxp);
636 XFreePixmap(gdi_display, data->pixmap);
637 destroy_glxpixmap(gdi_display, data->gl_drawable);
638 TRACE( "Recreated GL drawable %lx to replace %lx\n", glxp, data->gl_drawable );
640 data->pixmap = pix;
641 data->gl_drawable = glxp;
643 XFlush( gdi_display );
644 wine_tsx11_unlock();
646 SetPropA(data->hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
647 SetPropA(data->hwnd, pixmap_prop, (HANDLE)data->pixmap);
651 /***********************************************************************
652 * get_window_changes
654 * fill the window changes structure
656 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
658 int mask = 0;
660 if (old->right - old->left != new->right - new->left )
662 if ((changes->width = new->right - new->left) <= 0) changes->width = 1;
663 mask |= CWWidth;
665 if (old->bottom - old->top != new->bottom - new->top)
667 if ((changes->height = new->bottom - new->top) <= 0) changes->height = 1;
668 mask |= CWHeight;
670 if (old->left != new->left)
672 changes->x = new->left;
673 mask |= CWX;
675 if (old->top != new->top)
677 changes->y = new->top;
678 mask |= CWY;
680 return mask;
684 /***********************************************************************
685 * create_icon_window
687 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
689 XSetWindowAttributes attr;
691 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
692 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
693 attr.bit_gravity = NorthWestGravity;
694 attr.backing_store = NotUseful/*WhenMapped*/;
695 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
697 wine_tsx11_lock();
698 data->icon_window = XCreateWindow( display, root_window, 0, 0,
699 GetSystemMetrics( SM_CXICON ),
700 GetSystemMetrics( SM_CYICON ),
701 0, screen_depth,
702 InputOutput, visual,
703 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
704 XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
705 XFlush( display ); /* make sure the window exists before we start painting to it */
706 wine_tsx11_unlock();
708 TRACE( "created %lx\n", data->icon_window );
709 SetPropA( data->hwnd, icon_window_prop, (HANDLE)data->icon_window );
710 return data->icon_window;
715 /***********************************************************************
716 * destroy_icon_window
718 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
720 if (!data->icon_window) return;
721 if (x11drv_thread_data()->cursor_window == data->icon_window)
722 x11drv_thread_data()->cursor_window = None;
723 wine_tsx11_lock();
724 XDeleteContext( display, data->icon_window, winContext );
725 XDestroyWindow( display, data->icon_window );
726 data->icon_window = 0;
727 wine_tsx11_unlock();
728 RemovePropA( data->hwnd, icon_window_prop );
732 /***********************************************************************
733 * set_icon_hints
735 * Set the icon wm hints
737 static void set_icon_hints( Display *display, struct x11drv_win_data *data, HICON hIcon )
739 XWMHints *hints = data->wm_hints;
741 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
742 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
743 data->hWMIconBitmap = 0;
744 data->hWMIconMask = 0;
746 if (!hIcon)
748 if (!data->icon_window) create_icon_window( display, data );
749 hints->icon_window = data->icon_window;
750 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
752 else
754 HBITMAP hbmOrig;
755 RECT rcMask;
756 BITMAP bmMask;
757 ICONINFO ii;
758 HDC hDC;
760 GetIconInfo(hIcon, &ii);
762 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
763 rcMask.top = 0;
764 rcMask.left = 0;
765 rcMask.right = bmMask.bmWidth;
766 rcMask.bottom = bmMask.bmHeight;
768 hDC = CreateCompatibleDC(0);
769 hbmOrig = SelectObject(hDC, ii.hbmMask);
770 InvertRect(hDC, &rcMask);
771 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
772 SelectObject(hDC, hbmOrig);
773 DeleteDC(hDC);
775 data->hWMIconBitmap = ii.hbmColor;
776 data->hWMIconMask = ii.hbmMask;
778 hints->icon_pixmap = X11DRV_get_pixmap(data->hWMIconBitmap);
779 hints->icon_mask = X11DRV_get_pixmap(data->hWMIconMask);
780 destroy_icon_window( display, data );
781 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
786 /***********************************************************************
787 * set_size_hints
789 * set the window size hints
791 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
793 XSizeHints* size_hints;
795 if (!(size_hints = XAllocSizeHints())) return;
797 size_hints->win_gravity = StaticGravity;
798 size_hints->flags |= PWinGravity;
800 /* don't update size hints if window is not in normal state */
801 if (!(style & (WS_MINIMIZE | WS_MAXIMIZE)))
803 if (data->hwnd != GetDesktopWindow()) /* don't force position of desktop */
805 size_hints->x = data->whole_rect.left;
806 size_hints->y = data->whole_rect.top;
807 size_hints->flags |= PPosition;
810 if (!is_window_resizable( data, style ))
812 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
813 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
814 size_hints->min_width = size_hints->max_width;
815 size_hints->min_height = size_hints->max_height;
816 size_hints->flags |= PMinSize | PMaxSize;
819 XSetWMNormalHints( display, data->whole_window, size_hints );
820 XFree( size_hints );
824 /***********************************************************************
825 * get_process_name
827 * get the name of the current process for setting class hints
829 static char *get_process_name(void)
831 static char *name;
833 if (!name)
835 WCHAR module[MAX_PATH];
836 DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
837 if (len && len < MAX_PATH)
839 char *ptr;
840 WCHAR *p, *appname = module;
842 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
843 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
844 len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
845 if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
847 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
848 name = ptr;
852 return name;
856 /***********************************************************************
857 * set_initial_wm_hints
859 * Set the window manager hints that don't change over the lifetime of a window.
861 static void set_initial_wm_hints( Display *display, struct x11drv_win_data *data )
863 long i;
864 Atom protocols[3];
865 Atom dndVersion = 4;
866 XClassHint *class_hints;
867 char *process_name = get_process_name();
869 wine_tsx11_lock();
871 /* wm protocols */
872 i = 0;
873 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
874 protocols[i++] = x11drv_atom(_NET_WM_PING);
875 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
876 XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
877 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
879 /* class hints */
880 if ((class_hints = XAllocClassHint()))
882 static char wine[] = "Wine";
884 class_hints->res_name = process_name;
885 class_hints->res_class = wine;
886 XSetClassHint( display, data->whole_window, class_hints );
887 XFree( class_hints );
890 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
891 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
892 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
893 i = getpid();
894 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
895 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
897 XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
898 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
900 data->wm_hints = XAllocWMHints();
901 wine_tsx11_unlock();
903 if (data->wm_hints)
905 HICON icon = (HICON)SendMessageW( data->hwnd, WM_GETICON, ICON_BIG, 0 );
906 if (!icon) icon = (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON );
907 data->wm_hints->flags = 0;
908 set_icon_hints( display, data, icon );
913 /***********************************************************************
914 * set_wm_hints
916 * Set the window manager hints for a newly-created window
918 static void set_wm_hints( Display *display, struct x11drv_win_data *data )
920 Window group_leader;
921 Atom window_type;
922 MwmHints mwm_hints;
923 DWORD style, ex_style;
924 HWND owner;
926 if (data->hwnd == GetDesktopWindow())
928 /* force some styles for the desktop to get the correct decorations */
929 style = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
930 ex_style = WS_EX_APPWINDOW;
931 owner = 0;
933 else
935 style = GetWindowLongW( data->hwnd, GWL_STYLE );
936 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
937 owner = get_window_owner( data->hwnd );
940 /* transient for hint */
941 if (owner)
943 Window owner_win = X11DRV_get_whole_window( owner );
944 wine_tsx11_lock();
945 XSetTransientForHint( display, data->whole_window, owner_win );
946 wine_tsx11_unlock();
947 group_leader = owner_win;
949 else group_leader = data->whole_window;
951 wine_tsx11_lock();
953 /* size hints */
954 set_size_hints( display, data, style );
956 /* set the WM_WINDOW_TYPE */
957 if (style & WS_THICKFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
958 else if (ex_style & WS_EX_APPWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
959 else if (style & WS_DLGFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
960 else if (ex_style & WS_EX_DLGMODALFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
961 else if ((style & WS_POPUP) && owner) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
962 #if 0 /* many window managers don't handle utility windows very well */
963 else if (ex_style & WS_EX_TOOLWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
964 #endif
965 else window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
967 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
968 XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
970 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
971 mwm_hints.decorations = get_mwm_decorations( data, style, ex_style );
972 mwm_hints.functions = MWM_FUNC_MOVE;
973 if (is_window_resizable( data, style )) mwm_hints.functions |= MWM_FUNC_RESIZE;
974 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
975 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
976 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
978 XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
979 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
980 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
982 /* wm hints */
983 if (data->wm_hints)
985 data->wm_hints->flags |= InputHint | StateHint | WindowGroupHint;
986 data->wm_hints->input = !(style & WS_DISABLED);
987 data->wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
988 data->wm_hints->window_group = group_leader;
989 XSetWMHints( display, data->whole_window, data->wm_hints );
992 wine_tsx11_unlock();
996 /***********************************************************************
997 * update_net_wm_states
999 void update_net_wm_states( Display *display, struct x11drv_win_data *data )
1001 static const unsigned int state_atoms[NB_NET_WM_STATES] =
1003 XATOM__NET_WM_STATE_FULLSCREEN,
1004 XATOM__NET_WM_STATE_ABOVE,
1005 XATOM__NET_WM_STATE_MAXIMIZED_VERT,
1006 XATOM__NET_WM_STATE_SKIP_PAGER,
1007 XATOM__NET_WM_STATE_SKIP_TASKBAR
1010 DWORD i, style, ex_style, new_state = 0;
1012 if (!data->managed) return;
1013 if (data->whole_window == root_window) return;
1015 style = GetWindowLongW( data->hwnd, GWL_STYLE );
1016 if (data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
1017 data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height)
1019 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION)
1020 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
1021 else if (!(style & WS_MINIMIZE))
1022 new_state |= (1 << NET_WM_STATE_FULLSCREEN);
1024 else if (style & WS_MAXIMIZE)
1025 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
1027 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
1028 if (ex_style & WS_EX_TOPMOST)
1029 new_state |= (1 << NET_WM_STATE_ABOVE);
1030 if (ex_style & WS_EX_TOOLWINDOW)
1031 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR) | (1 << NET_WM_STATE_SKIP_PAGER);
1032 if (!(ex_style & WS_EX_APPWINDOW) && get_window_owner( data->hwnd ))
1033 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR);
1035 if (!data->mapped) /* set the _NET_WM_STATE atom directly */
1037 Atom atoms[NB_NET_WM_STATES+1];
1038 DWORD count;
1040 for (i = count = 0; i < NB_NET_WM_STATES; i++)
1042 if (!(new_state & (1 << i))) continue;
1043 TRACE( "setting wm state %u for unmapped window %p/%lx\n",
1044 i, data->hwnd, data->whole_window );
1045 atoms[count++] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1046 if (state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT)
1047 atoms[count++] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
1049 wine_tsx11_lock();
1050 XChangeProperty( display, data->whole_window, x11drv_atom(_NET_WM_STATE), XA_ATOM,
1051 32, PropModeReplace, (unsigned char *)atoms, count );
1052 wine_tsx11_unlock();
1054 else /* ask the window manager to do it for us */
1056 XEvent xev;
1058 xev.xclient.type = ClientMessage;
1059 xev.xclient.window = data->whole_window;
1060 xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
1061 xev.xclient.serial = 0;
1062 xev.xclient.display = display;
1063 xev.xclient.send_event = True;
1064 xev.xclient.format = 32;
1065 xev.xclient.data.l[3] = 1;
1067 for (i = 0; i < NB_NET_WM_STATES; i++)
1069 if (!((data->net_wm_state ^ new_state) & (1 << i))) continue; /* unchanged */
1071 TRACE( "setting wm state %u for window %p/%lx to %u prev %u\n",
1072 i, data->hwnd, data->whole_window,
1073 (new_state & (1 << i)) != 0, (data->net_wm_state & (1 << i)) != 0 );
1075 xev.xclient.data.l[0] = (new_state & (1 << i)) ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1076 xev.xclient.data.l[1] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1077 xev.xclient.data.l[2] = ((state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT) ?
1078 x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ) : 0);
1079 wine_tsx11_lock();
1080 XSendEvent( display, root_window, False,
1081 SubstructureRedirectMask | SubstructureNotifyMask, &xev );
1082 wine_tsx11_unlock();
1085 data->net_wm_state = new_state;
1089 /***********************************************************************
1090 * set_xembed_flags
1092 static void set_xembed_flags( Display *display, struct x11drv_win_data *data, unsigned long flags )
1094 unsigned long info[2];
1096 info[0] = 0; /* protocol version */
1097 info[1] = flags;
1098 wine_tsx11_lock();
1099 XChangeProperty( display, data->whole_window, x11drv_atom(_XEMBED_INFO),
1100 x11drv_atom(_XEMBED_INFO), 32, PropModeReplace, (unsigned char*)info, 2 );
1101 wine_tsx11_unlock();
1105 /***********************************************************************
1106 * map_window
1108 static void map_window( Display *display, struct x11drv_win_data *data, DWORD new_style )
1110 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1112 wait_for_withdrawn_state( display, data, TRUE );
1114 if (!data->embedded)
1116 update_net_wm_states( display, data );
1117 sync_window_style( display, data );
1118 wine_tsx11_lock();
1119 XMapWindow( display, data->whole_window );
1120 wine_tsx11_unlock();
1122 else set_xembed_flags( display, data, XEMBED_MAPPED );
1124 data->mapped = TRUE;
1125 data->iconic = (new_style & WS_MINIMIZE) != 0;
1129 /***********************************************************************
1130 * unmap_window
1132 static void unmap_window( Display *display, struct x11drv_win_data *data )
1134 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1136 if (!data->embedded)
1138 wait_for_withdrawn_state( display, data, FALSE );
1139 wine_tsx11_lock();
1140 if (data->managed) XWithdrawWindow( display, data->whole_window, DefaultScreen(display) );
1141 else XUnmapWindow( display, data->whole_window );
1142 wine_tsx11_unlock();
1144 else set_xembed_flags( display, data, 0 );
1146 data->mapped = FALSE;
1147 data->net_wm_state = 0;
1151 /***********************************************************************
1152 * make_window_embedded
1154 void make_window_embedded( Display *display, struct x11drv_win_data *data )
1156 if (data->mapped)
1158 /* the window cannot be mapped before being embedded */
1159 unmap_window( display, data );
1160 data->embedded = TRUE;
1161 map_window( display, data, 0 );
1163 else
1165 data->embedded = TRUE;
1166 set_xembed_flags( display, data, 0 );
1171 /***********************************************************************
1172 * X11DRV_window_to_X_rect
1174 * Convert a rect from client to X window coordinates
1176 void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
1178 RECT rc;
1180 if (!data->managed) return;
1181 if (IsRectEmpty( rect )) return;
1183 get_x11_rect_offset( data, &rc );
1185 rect->left -= rc.left;
1186 rect->right -= rc.right;
1187 rect->top -= rc.top;
1188 rect->bottom -= rc.bottom;
1189 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1190 if (rect->left >= rect->right) rect->right = rect->left + 1;
1194 /***********************************************************************
1195 * X11DRV_X_to_window_rect
1197 * Opposite of X11DRV_window_to_X_rect
1199 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
1201 RECT rc;
1203 if (!data->managed) return;
1204 if (IsRectEmpty( rect )) return;
1206 get_x11_rect_offset( data, &rc );
1208 rect->left += rc.left;
1209 rect->right += rc.right;
1210 rect->top += rc.top;
1211 rect->bottom += rc.bottom;
1212 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1213 if (rect->left >= rect->right) rect->right = rect->left + 1;
1217 /***********************************************************************
1218 * sync_window_position
1220 * Synchronize the X window position with the Windows one
1222 static void sync_window_position( Display *display, struct x11drv_win_data *data,
1223 UINT swp_flags, const RECT *old_client_rect,
1224 const RECT *old_whole_rect )
1226 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
1227 XWindowChanges changes;
1228 unsigned int mask = 0;
1230 if (data->managed && data->iconic) return;
1232 /* resizing a managed maximized window is not allowed */
1233 if (!(style & WS_MAXIMIZE) || !data->managed)
1235 changes.width = data->whole_rect.right - data->whole_rect.left;
1236 changes.height = data->whole_rect.bottom - data->whole_rect.top;
1237 /* if window rect is empty force size to 1x1 */
1238 if (changes.width <= 0 || changes.height <= 0) changes.width = changes.height = 1;
1239 mask |= CWWidth | CWHeight;
1242 /* only the size is allowed to change for the desktop window */
1243 if (data->whole_window != root_window)
1245 changes.x = data->whole_rect.left - virtual_screen_rect.left;
1246 changes.y = data->whole_rect.top - virtual_screen_rect.top;
1247 mask |= CWX | CWY;
1250 if (!(swp_flags & SWP_NOZORDER) || (swp_flags & SWP_SHOWWINDOW))
1252 /* find window that this one must be after */
1253 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
1254 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
1255 prev = GetWindow( prev, GW_HWNDPREV );
1256 if (!prev) /* top child */
1258 changes.stack_mode = Above;
1259 mask |= CWStackMode;
1261 /* should use stack_mode Below but most window managers don't get it right */
1262 /* and Above with a sibling doesn't work so well either, so we ignore it */
1265 TRACE( "setting win %p/%lx pos %d,%d,%dx%d after %lx changes=%x\n",
1266 data->hwnd, data->whole_window, data->whole_rect.left, data->whole_rect.top,
1267 data->whole_rect.right - data->whole_rect.left,
1268 data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
1270 wine_tsx11_lock();
1271 set_size_hints( display, data, style );
1272 XReconfigureWMWindow( display, data->whole_window,
1273 DefaultScreen(display), mask, &changes );
1274 wine_tsx11_unlock();
1278 /***********************************************************************
1279 * sync_client_position
1281 * Synchronize the X client window position with the Windows one
1283 static void sync_client_position( Display *display, struct x11drv_win_data *data,
1284 UINT swp_flags, const RECT *old_client_rect,
1285 const RECT *old_whole_rect )
1287 int mask;
1288 XWindowChanges changes;
1289 RECT old = *old_client_rect;
1290 RECT new = data->client_rect;
1292 OffsetRect( &old, -old_whole_rect->left, -old_whole_rect->top );
1293 OffsetRect( &new, -data->whole_rect.left, -data->whole_rect.top );
1294 if (!(mask = get_window_changes( &changes, &old, &new ))) return;
1296 if (data->client_window)
1298 TRACE( "setting client win %lx pos %d,%d,%dx%d changes=%x\n",
1299 data->client_window, new.left, new.top,
1300 new.right - new.left, new.bottom - new.top, mask );
1301 wine_tsx11_lock();
1302 XConfigureWindow( display, data->client_window, mask, &changes );
1303 wine_tsx11_unlock();
1306 if (data->gl_drawable && (mask & (CWWidth|CWHeight))) sync_gl_drawable( data );
1310 /***********************************************************************
1311 * move_window_bits
1313 * Move the window bits when a window is moved.
1315 static void move_window_bits( struct x11drv_win_data *data, const RECT *old_rect, const RECT *new_rect,
1316 const RECT *old_client_rect )
1318 RECT src_rect = *old_rect;
1319 RECT dst_rect = *new_rect;
1320 HDC hdc_src, hdc_dst;
1321 INT code;
1322 HRGN rgn = 0;
1323 HWND parent = 0;
1325 if (!data->whole_window)
1327 OffsetRect( &dst_rect, -data->window_rect.left, -data->window_rect.top );
1328 parent = GetAncestor( data->hwnd, GA_PARENT );
1329 hdc_src = GetDCEx( parent, 0, DCX_CACHE );
1330 hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE | DCX_WINDOW );
1332 else
1334 OffsetRect( &dst_rect, -data->client_rect.left, -data->client_rect.top );
1335 /* make src rect relative to the old position of the window */
1336 OffsetRect( &src_rect, -old_client_rect->left, -old_client_rect->top );
1337 if (dst_rect.left == src_rect.left && dst_rect.top == src_rect.top) return;
1338 hdc_src = hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE );
1341 code = X11DRV_START_EXPOSURES;
1342 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
1344 TRACE( "copying bits for win %p/%lx/%lx %s -> %s\n",
1345 data->hwnd, data->whole_window, data->client_window,
1346 wine_dbgstr_rect(&src_rect), wine_dbgstr_rect(&dst_rect) );
1347 BitBlt( hdc_dst, dst_rect.left, dst_rect.top,
1348 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top,
1349 hdc_src, src_rect.left, src_rect.top, SRCCOPY );
1351 code = X11DRV_END_EXPOSURES;
1352 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, sizeof(rgn), (LPSTR)&rgn );
1354 ReleaseDC( data->hwnd, hdc_dst );
1355 if (hdc_src != hdc_dst) ReleaseDC( parent, hdc_src );
1357 if (rgn)
1359 if (!data->whole_window)
1361 /* map region to client rect since we are using DCX_WINDOW */
1362 OffsetRgn( rgn, data->window_rect.left - data->client_rect.left,
1363 data->window_rect.top - data->client_rect.top );
1364 RedrawWindow( data->hwnd, NULL, rgn,
1365 RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ALLCHILDREN );
1367 else RedrawWindow( data->hwnd, NULL, rgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1368 DeleteObject( rgn );
1373 /**********************************************************************
1374 * create_whole_window
1376 * Create the whole X window for a given window
1378 static Window create_whole_window( Display *display, struct x11drv_win_data *data )
1380 int cx, cy, mask;
1381 XSetWindowAttributes attr;
1382 WCHAR text[1024];
1383 COLORREF key;
1384 BYTE alpha;
1385 DWORD layered_flags;
1387 if (!(cx = data->window_rect.right - data->window_rect.left)) cx = 1;
1388 if (!(cy = data->window_rect.bottom - data->window_rect.top)) cy = 1;
1390 if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1392 TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1393 data->managed = TRUE;
1394 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1397 mask = get_window_attributes( display, data, &attr );
1399 wine_tsx11_lock();
1401 data->whole_rect = data->window_rect;
1402 data->whole_window = XCreateWindow( display, root_window,
1403 data->window_rect.left - virtual_screen_rect.left,
1404 data->window_rect.top - virtual_screen_rect.top,
1405 cx, cy, 0, screen_depth, InputOutput,
1406 visual, mask, &attr );
1408 if (data->whole_window) XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
1409 wine_tsx11_unlock();
1411 if (!data->whole_window) return 0;
1413 if (!create_client_window( display, data, NULL ))
1415 wine_tsx11_lock();
1416 XDeleteContext( display, data->whole_window, winContext );
1417 XDestroyWindow( display, data->whole_window );
1418 data->whole_window = 0;
1419 wine_tsx11_unlock();
1420 return 0;
1423 set_initial_wm_hints( display, data );
1424 set_wm_hints( display, data );
1426 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1428 /* set the window text */
1429 if (!InternalGetWindowText( data->hwnd, text, sizeof(text)/sizeof(WCHAR) )) text[0] = 0;
1430 sync_window_text( display, data->whole_window, text );
1432 /* set the window region */
1433 sync_window_region( display, data, (HRGN)1 );
1435 /* set the window opacity */
1436 if (!GetLayeredWindowAttributes( data->hwnd, &key, &alpha, &layered_flags )) layered_flags = 0;
1437 sync_window_opacity( display, data->whole_window, key, alpha, layered_flags );
1439 wine_tsx11_lock();
1440 XFlush( display ); /* make sure the window exists before we start painting to it */
1441 wine_tsx11_unlock();
1442 return data->whole_window;
1446 /**********************************************************************
1447 * destroy_whole_window
1449 * Destroy the whole X window for a given window.
1451 static void destroy_whole_window( Display *display, struct x11drv_win_data *data, BOOL already_destroyed )
1453 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1455 if (!data->whole_window) return;
1457 TRACE( "win %p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1458 if (thread_data->cursor_window == data->whole_window ||
1459 thread_data->cursor_window == data->client_window)
1460 thread_data->cursor_window = None;
1461 wine_tsx11_lock();
1462 XDeleteContext( display, data->whole_window, winContext );
1463 XDeleteContext( display, data->client_window, winContext );
1464 if (!already_destroyed) XDestroyWindow( display, data->whole_window );
1465 data->whole_window = data->client_window = 0;
1466 data->wm_state = WithdrawnState;
1467 data->net_wm_state = 0;
1468 data->mapped = FALSE;
1469 if (data->xic)
1471 XUnsetICFocus( data->xic );
1472 XDestroyIC( data->xic );
1473 data->xic = 0;
1475 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1476 XFlush( display );
1477 XFree( data->wm_hints );
1478 data->wm_hints = NULL;
1479 wine_tsx11_unlock();
1480 RemovePropA( data->hwnd, whole_window_prop );
1481 RemovePropA( data->hwnd, client_window_prop );
1485 /*****************************************************************
1486 * SetWindowText (X11DRV.@)
1488 void X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1490 Window win;
1492 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(gdi_display))
1494 Display *display = thread_init_display();
1495 sync_window_text( display, win, text );
1500 /***********************************************************************
1501 * SetWindowStyle (X11DRV.@)
1503 * Update the X state of a window to reflect a style change
1505 void X11DRV_SetWindowStyle( HWND hwnd, INT offset, STYLESTRUCT *style )
1507 struct x11drv_win_data *data;
1508 DWORD changed;
1510 if (hwnd == GetDesktopWindow()) return;
1511 changed = style->styleNew ^ style->styleOld;
1513 if (offset == GWL_STYLE && (changed & WS_VISIBLE) && (style->styleNew & WS_VISIBLE))
1515 /* we don't unmap windows, that causes trouble with the window manager */
1516 if (!(data = X11DRV_get_win_data( hwnd )) &&
1517 !(data = X11DRV_create_win_data( hwnd ))) return;
1519 if (data->whole_window && is_window_rect_mapped( &data->window_rect ))
1521 Display *display = thread_display();
1522 set_wm_hints( display, data );
1523 if (!data->mapped) map_window( display, data, style->styleNew );
1527 if (offset == GWL_STYLE && (changed & WS_DISABLED))
1529 data = X11DRV_get_win_data( hwnd );
1530 if (data && data->wm_hints)
1532 wine_tsx11_lock();
1533 data->wm_hints->input = !(style->styleNew & WS_DISABLED);
1534 XSetWMHints( thread_display(), data->whole_window, data->wm_hints );
1535 wine_tsx11_unlock();
1539 if (offset == GWL_EXSTYLE && (changed & WS_EX_LAYERED))
1541 /* changing WS_EX_LAYERED resets attributes */
1542 if ((data = X11DRV_get_win_data( hwnd )) && data->whole_window)
1543 sync_window_opacity( thread_display(), data->whole_window, 0, 0, 0 );
1548 /***********************************************************************
1549 * DestroyWindow (X11DRV.@)
1551 void X11DRV_DestroyWindow( HWND hwnd )
1553 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1554 struct x11drv_win_data *data;
1556 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1558 if (data->pixmap)
1560 wine_tsx11_lock();
1561 destroy_glxpixmap(gdi_display, data->gl_drawable);
1562 XFreePixmap(gdi_display, data->pixmap);
1563 wine_tsx11_unlock();
1565 else if (data->gl_drawable)
1567 wine_tsx11_lock();
1568 XDestroyWindow(gdi_display, data->gl_drawable);
1569 wine_tsx11_unlock();
1572 destroy_whole_window( thread_data->display, data, FALSE );
1573 destroy_icon_window( thread_data->display, data );
1575 if (data->colormap)
1577 wine_tsx11_lock();
1578 XFreeColormap( thread_data->display, data->colormap );
1579 wine_tsx11_unlock();
1582 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1583 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
1584 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
1585 wine_tsx11_lock();
1586 XDeleteContext( thread_data->display, (XID)hwnd, win_data_context );
1587 wine_tsx11_unlock();
1588 HeapFree( GetProcessHeap(), 0, data );
1592 /***********************************************************************
1593 * X11DRV_DestroyNotify
1595 void X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1597 Display *display = event->xdestroywindow.display;
1598 struct x11drv_win_data *data;
1600 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1602 FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1603 destroy_whole_window( display, data, TRUE );
1607 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
1609 struct x11drv_win_data *data;
1611 if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
1613 data->hwnd = hwnd;
1614 wine_tsx11_lock();
1615 if (!winContext) winContext = XUniqueContext();
1616 if (!win_data_context) win_data_context = XUniqueContext();
1617 XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
1618 wine_tsx11_unlock();
1620 return data;
1624 /* initialize the desktop window id in the desktop manager process */
1625 static struct x11drv_win_data *create_desktop_win_data( Display *display, HWND hwnd )
1627 struct x11drv_win_data *data;
1629 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1630 data->whole_window = data->client_window = root_window;
1631 data->managed = TRUE;
1632 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1633 SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
1634 SetPropA( data->hwnd, client_window_prop, (HANDLE)root_window );
1635 set_initial_wm_hints( display, data );
1636 return data;
1639 /**********************************************************************
1640 * CreateDesktopWindow (X11DRV.@)
1642 BOOL X11DRV_CreateDesktopWindow( HWND hwnd )
1644 unsigned int width, height;
1646 /* retrieve the real size of the desktop */
1647 SERVER_START_REQ( get_window_rectangles )
1649 req->handle = hwnd;
1650 wine_server_call( req );
1651 width = reply->window.right - reply->window.left;
1652 height = reply->window.bottom - reply->window.top;
1654 SERVER_END_REQ;
1656 if (!width && !height) /* not initialized yet */
1658 SERVER_START_REQ( set_window_pos )
1660 req->handle = hwnd;
1661 req->previous = 0;
1662 req->flags = SWP_NOZORDER;
1663 req->window.left = virtual_screen_rect.left;
1664 req->window.top = virtual_screen_rect.top;
1665 req->window.right = virtual_screen_rect.right;
1666 req->window.bottom = virtual_screen_rect.bottom;
1667 req->client = req->window;
1668 wine_server_call( req );
1670 SERVER_END_REQ;
1672 else
1674 Window win = (Window)GetPropA( hwnd, whole_window_prop );
1675 if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1677 return TRUE;
1681 /**********************************************************************
1682 * CreateWindow (X11DRV.@)
1684 BOOL X11DRV_CreateWindow( HWND hwnd )
1686 if (hwnd == GetDesktopWindow() && root_window != DefaultRootWindow( gdi_display ))
1688 Display *display = thread_init_display();
1690 /* the desktop win data can't be created lazily */
1691 if (!create_desktop_win_data( display, hwnd )) return FALSE;
1693 return TRUE;
1697 /***********************************************************************
1698 * X11DRV_get_win_data
1700 * Return the X11 data structure associated with a window.
1702 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1704 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1705 char *data;
1707 if (!thread_data) return NULL;
1708 if (!hwnd) return NULL;
1709 if (XFindContext( thread_data->display, (XID)hwnd, win_data_context, &data )) data = NULL;
1710 return (struct x11drv_win_data *)data;
1714 /***********************************************************************
1715 * X11DRV_create_win_data
1717 * Create an X11 data window structure for an existing window.
1719 struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd )
1721 Display *display;
1722 struct x11drv_win_data *data;
1723 HWND parent;
1725 if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL; /* desktop */
1727 /* don't create win data for HWND_MESSAGE windows */
1728 if (parent != GetDesktopWindow() && !GetAncestor( parent, GA_PARENT )) return NULL;
1730 display = thread_init_display();
1731 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1733 GetWindowRect( hwnd, &data->window_rect );
1734 MapWindowPoints( 0, parent, (POINT *)&data->window_rect, 2 );
1735 data->whole_rect = data->window_rect;
1736 GetClientRect( hwnd, &data->client_rect );
1737 MapWindowPoints( hwnd, parent, (POINT *)&data->client_rect, 2 );
1739 if (parent == GetDesktopWindow())
1741 if (!create_whole_window( display, data ))
1743 HeapFree( GetProcessHeap(), 0, data );
1744 return NULL;
1746 TRACE( "win %p/%lx/%lx window %s whole %s client %s\n",
1747 hwnd, data->whole_window, data->client_window, wine_dbgstr_rect( &data->window_rect ),
1748 wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
1750 return data;
1754 /***********************************************************************
1755 * X11DRV_get_whole_window
1757 * Return the X window associated with the full area of a window
1759 Window X11DRV_get_whole_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, whole_window_prop );
1768 return data->whole_window;
1772 /***********************************************************************
1773 * X11DRV_get_client_window
1775 * Return the X window associated with the client area of a window
1777 Window X11DRV_get_client_window( HWND hwnd )
1779 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1781 if (!data)
1783 if (hwnd == GetDesktopWindow()) return root_window;
1784 return (Window)GetPropA( hwnd, client_window_prop );
1786 return data->client_window;
1790 /***********************************************************************
1791 * X11DRV_get_ic
1793 * Return the X input context associated with a window
1795 XIC X11DRV_get_ic( HWND hwnd )
1797 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1798 XIM xim;
1800 if (!data) return 0;
1801 if (data->xic) return data->xic;
1802 if (!(xim = x11drv_thread_data()->xim)) return 0;
1803 return X11DRV_CreateIC( xim, data );
1807 /***********************************************************************
1808 * X11DRV_GetDC (X11DRV.@)
1810 void X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
1811 const RECT *top_rect, DWORD flags )
1813 struct x11drv_escape_set_drawable escape;
1814 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1816 escape.code = X11DRV_SET_DRAWABLE;
1817 escape.mode = IncludeInferiors;
1818 escape.fbconfig_id = 0;
1819 escape.gl_drawable = 0;
1820 escape.pixmap = 0;
1821 escape.gl_copy = FALSE;
1823 if (top == hwnd && data && IsIconic( hwnd ) && data->icon_window)
1825 escape.drawable = data->icon_window;
1827 else if (top == hwnd)
1829 escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
1830 /* GL draws to the client area even for window DCs */
1831 escape.gl_drawable = data ? data->client_window : X11DRV_get_client_window( hwnd );
1832 if (flags & DCX_WINDOW)
1833 escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
1834 else
1835 escape.drawable = escape.gl_drawable;
1837 else
1839 escape.drawable = X11DRV_get_client_window( top );
1840 escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
1841 escape.gl_drawable = data ? data->gl_drawable : (Drawable)GetPropA( hwnd, gl_drawable_prop );
1842 escape.pixmap = data ? data->pixmap : (Pixmap)GetPropA( hwnd, pixmap_prop );
1843 escape.gl_copy = (escape.gl_drawable != 0);
1844 if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
1847 escape.dc_rect.left = win_rect->left - top_rect->left;
1848 escape.dc_rect.top = win_rect->top - top_rect->top;
1849 escape.dc_rect.right = win_rect->right - top_rect->left;
1850 escape.dc_rect.bottom = win_rect->bottom - top_rect->top;
1851 escape.drawable_rect.left = top_rect->left;
1852 escape.drawable_rect.top = top_rect->top;
1853 escape.drawable_rect.right = top_rect->right;
1854 escape.drawable_rect.bottom = top_rect->bottom;
1856 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1860 /***********************************************************************
1861 * X11DRV_ReleaseDC (X11DRV.@)
1863 void X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
1865 struct x11drv_escape_set_drawable escape;
1867 escape.code = X11DRV_SET_DRAWABLE;
1868 escape.drawable = root_window;
1869 escape.mode = IncludeInferiors;
1870 escape.drawable_rect = virtual_screen_rect;
1871 SetRect( &escape.dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
1872 virtual_screen_rect.bottom - virtual_screen_rect.top );
1873 escape.fbconfig_id = 0;
1874 escape.gl_drawable = 0;
1875 escape.pixmap = 0;
1876 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1880 /***********************************************************************
1881 * SetCapture (X11DRV.@)
1883 void X11DRV_SetCapture( HWND hwnd, UINT flags )
1885 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1887 if (!thread_data) return;
1888 if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
1890 if (hwnd)
1892 Window grab_win = X11DRV_get_client_window( GetAncestor( hwnd, GA_ROOT ) );
1894 if (!grab_win) return;
1895 wine_tsx11_lock();
1896 XFlush( gdi_display );
1897 XGrabPointer( thread_data->display, grab_win, False,
1898 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1899 GrabModeAsync, GrabModeAsync, None, None, CurrentTime );
1900 wine_tsx11_unlock();
1901 thread_data->grab_window = grab_win;
1903 else /* release capture */
1905 wine_tsx11_lock();
1906 XFlush( gdi_display );
1907 XUngrabPointer( thread_data->display, CurrentTime );
1908 wine_tsx11_unlock();
1909 thread_data->grab_window = None;
1914 /*****************************************************************
1915 * SetParent (X11DRV.@)
1917 void X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
1919 Display *display = thread_display();
1920 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1922 if (!data) return;
1923 if (parent == old_parent) return;
1925 if (parent != GetDesktopWindow()) /* a child window */
1927 if (old_parent == GetDesktopWindow())
1929 /* destroy the old X windows */
1930 destroy_whole_window( display, data, FALSE );
1931 destroy_icon_window( display, data );
1932 if (data->managed)
1934 data->managed = FALSE;
1935 RemovePropA( data->hwnd, managed_prop );
1939 else /* new top level window */
1941 /* FIXME: we ignore errors since we can't really recover anyway */
1942 create_whole_window( display, data );
1947 /*****************************************************************
1948 * SetFocus (X11DRV.@)
1950 * Set the X focus.
1952 void X11DRV_SetFocus( HWND hwnd )
1954 Display *display = thread_display();
1955 struct x11drv_win_data *data;
1956 XWindowChanges changes;
1958 if (!(hwnd = GetAncestor( hwnd, GA_ROOT ))) return;
1959 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1960 if (data->managed || !data->whole_window) return;
1962 /* Set X focus and install colormap */
1963 wine_tsx11_lock();
1964 changes.stack_mode = Above;
1965 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
1966 if (root_window == DefaultRootWindow(display))
1968 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1969 /* FIXME: this is not entirely correct */
1970 XSetInputFocus( display, data->whole_window, RevertToParent,
1971 /* CurrentTime */
1972 GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1974 wine_tsx11_unlock();
1978 /***********************************************************************
1979 * WindowPosChanging (X11DRV.@)
1981 void X11DRV_WindowPosChanging( HWND hwnd, HWND insert_after, UINT swp_flags,
1982 const RECT *window_rect, const RECT *client_rect, RECT *visible_rect )
1984 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1985 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
1987 if (!data)
1989 /* create the win data if the window is being made visible */
1990 if (!(style & WS_VISIBLE) && !(swp_flags & SWP_SHOWWINDOW)) return;
1991 if (!(data = X11DRV_create_win_data( hwnd ))) return;
1994 /* check if we need to switch the window to managed */
1995 if (!data->managed && data->whole_window && is_window_managed( hwnd, swp_flags, window_rect ))
1997 TRACE( "making win %p/%lx managed\n", hwnd, data->whole_window );
1998 if (data->mapped) unmap_window( thread_display(), data );
1999 data->managed = TRUE;
2000 SetPropA( hwnd, managed_prop, (HANDLE)1 );
2003 *visible_rect = *window_rect;
2004 X11DRV_window_to_X_rect( data, visible_rect );
2008 /***********************************************************************
2009 * WindowPosChanged (X11DRV.@)
2011 void X11DRV_WindowPosChanged( HWND hwnd, HWND insert_after, UINT swp_flags,
2012 const RECT *rectWindow, const RECT *rectClient,
2013 const RECT *visible_rect, const RECT *valid_rects )
2015 struct x11drv_thread_data *thread_data;
2016 Display *display;
2017 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2018 DWORD new_style = GetWindowLongW( hwnd, GWL_STYLE );
2019 RECT old_whole_rect, old_client_rect;
2020 int event_type;
2022 if (!data) return;
2024 thread_data = x11drv_thread_data();
2025 display = thread_data->display;
2027 old_whole_rect = data->whole_rect;
2028 old_client_rect = data->client_rect;
2029 data->window_rect = *rectWindow;
2030 data->whole_rect = *visible_rect;
2031 data->client_rect = *rectClient;
2033 TRACE( "win %p window %s client %s style %08x flags %08x\n",
2034 hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style, swp_flags );
2036 if (!IsRectEmpty( &valid_rects[0] ))
2038 int x_offset = old_whole_rect.left - data->whole_rect.left;
2039 int y_offset = old_whole_rect.top - data->whole_rect.top;
2041 /* if all that happened is that the whole window moved, copy everything */
2042 if (!(swp_flags & SWP_FRAMECHANGED) &&
2043 old_whole_rect.right - data->whole_rect.right == x_offset &&
2044 old_whole_rect.bottom - data->whole_rect.bottom == y_offset &&
2045 old_client_rect.left - data->client_rect.left == x_offset &&
2046 old_client_rect.right - data->client_rect.right == x_offset &&
2047 old_client_rect.top - data->client_rect.top == y_offset &&
2048 old_client_rect.bottom - data->client_rect.bottom == y_offset &&
2049 !memcmp( &valid_rects[0], &data->client_rect, sizeof(RECT) ))
2051 /* if we have an X window the bits will be moved by the X server */
2052 if (!data->whole_window)
2053 move_window_bits( data, &old_whole_rect, &data->whole_rect, &old_client_rect );
2055 else
2056 move_window_bits( data, &valid_rects[1], &valid_rects[0], &old_client_rect );
2059 wine_tsx11_lock();
2060 XFlush( gdi_display ); /* make sure painting is done before we move the window */
2061 wine_tsx11_unlock();
2063 sync_client_position( display, data, swp_flags, &old_client_rect, &old_whole_rect );
2065 if (!data->whole_window) return;
2067 /* check if we are currently processing an event relevant to this window */
2068 event_type = 0;
2069 if (thread_data->current_event && thread_data->current_event->xany.window == data->whole_window)
2070 event_type = thread_data->current_event->type;
2072 if (event_type != ConfigureNotify && event_type != PropertyNotify)
2073 event_type = 0; /* ignore other events */
2075 if (data->mapped)
2077 if (((swp_flags & SWP_HIDEWINDOW) && !(new_style & WS_VISIBLE)) ||
2078 (!event_type && !is_window_rect_mapped( rectWindow )))
2079 unmap_window( display, data );
2082 /* don't change position if we are about to minimize or maximize a managed window */
2083 if (!event_type &&
2084 !(data->managed && (swp_flags & SWP_STATECHANGED) && (new_style & (WS_MINIMIZE|WS_MAXIMIZE))))
2085 sync_window_position( display, data, swp_flags, &old_client_rect, &old_whole_rect );
2087 if ((new_style & WS_VISIBLE) &&
2088 ((new_style & WS_MINIMIZE) || is_window_rect_mapped( rectWindow )))
2090 if (!data->mapped || (swp_flags & (SWP_FRAMECHANGED|SWP_STATECHANGED)))
2091 set_wm_hints( display, data );
2093 if (!data->mapped)
2095 map_window( display, data, new_style );
2097 else if ((swp_flags & SWP_STATECHANGED) && (!data->iconic != !(new_style & WS_MINIMIZE)))
2099 data->iconic = (new_style & WS_MINIMIZE) != 0;
2100 TRACE( "changing win %p iconic state to %u\n", data->hwnd, data->iconic );
2101 wine_tsx11_lock();
2102 if (data->iconic)
2103 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
2104 else if (is_window_rect_mapped( rectWindow ))
2105 XMapWindow( display, data->whole_window );
2106 wine_tsx11_unlock();
2107 update_net_wm_states( display, data );
2109 else if (!event_type)
2111 update_net_wm_states( display, data );
2115 wine_tsx11_lock();
2116 XFlush( display ); /* make sure changes are done before we start painting again */
2117 wine_tsx11_unlock();
2121 /***********************************************************************
2122 * ShowWindow (X11DRV.@)
2124 UINT X11DRV_ShowWindow( HWND hwnd, INT cmd, RECT *rect, UINT swp )
2126 int x, y;
2127 unsigned int width, height, border, depth;
2128 Window root, top;
2129 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
2130 struct x11drv_thread_data *thread_data = x11drv_thread_data();
2131 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2133 if (!data || !data->whole_window || !data->managed || !data->mapped || data->iconic) return swp;
2134 if (style & WS_MINIMIZE) return swp;
2135 if (IsRectEmpty( rect )) return swp;
2137 /* only fetch the new rectangle if the ShowWindow was a result of a window manager event */
2139 if (!thread_data->current_event || thread_data->current_event->xany.window != data->whole_window)
2140 return swp;
2142 if (thread_data->current_event->type != ConfigureNotify &&
2143 thread_data->current_event->type != PropertyNotify)
2144 return swp;
2146 TRACE( "win %p/%lx cmd %d at %s flags %08x\n",
2147 hwnd, data->whole_window, cmd, wine_dbgstr_rect(rect), swp );
2149 wine_tsx11_lock();
2150 XGetGeometry( thread_data->display, data->whole_window,
2151 &root, &x, &y, &width, &height, &border, &depth );
2152 XTranslateCoordinates( thread_data->display, data->whole_window, root, 0, 0, &x, &y, &top );
2153 wine_tsx11_unlock();
2154 rect->left = x;
2155 rect->top = y;
2156 rect->right = x + width;
2157 rect->bottom = y + height;
2158 OffsetRect( rect, virtual_screen_rect.left, virtual_screen_rect.top );
2159 X11DRV_X_to_window_rect( data, rect );
2160 return swp & ~(SWP_NOMOVE | SWP_NOCLIENTMOVE | SWP_NOSIZE | SWP_NOCLIENTSIZE);
2164 /**********************************************************************
2165 * SetWindowIcon (X11DRV.@)
2167 * hIcon or hIconSm has changed (or is being initialised for the
2168 * first time). Complete the X11 driver-specific initialisation
2169 * and set the window hints.
2171 * This is not entirely correct, may need to create
2172 * an icon window and set the pixmap as a background
2174 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
2176 Display *display = thread_display();
2177 struct x11drv_win_data *data;
2179 if (type != ICON_BIG) return; /* nothing to do here */
2181 if (!(data = X11DRV_get_win_data( hwnd ))) return;
2182 if (!data->whole_window) return;
2183 if (!data->managed) return;
2185 if (data->wm_hints)
2187 set_icon_hints( display, data, icon );
2188 wine_tsx11_lock();
2189 XSetWMHints( display, data->whole_window, data->wm_hints );
2190 wine_tsx11_unlock();
2195 /***********************************************************************
2196 * SetWindowRgn (X11DRV.@)
2198 * Assign specified region to window (for non-rectangular windows)
2200 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
2202 struct x11drv_win_data *data;
2204 if ((data = X11DRV_get_win_data( hwnd )))
2206 sync_window_region( thread_display(), data, hrgn );
2208 else if (X11DRV_get_whole_window( hwnd ))
2210 SendMessageW( hwnd, WM_X11DRV_SET_WIN_REGION, 0, 0 );
2212 return TRUE;
2216 /***********************************************************************
2217 * SetLayeredWindowAttributes (X11DRV.@)
2219 * Set transparency attributes for a layered window.
2221 void X11DRV_SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
2223 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2225 if (data)
2227 if (data->whole_window)
2228 sync_window_opacity( thread_display(), data->whole_window, key, alpha, flags );
2230 else
2232 Window win = X11DRV_get_whole_window( hwnd );
2233 if (win) sync_window_opacity( gdi_display, win, key, alpha, flags );
2238 /**********************************************************************
2239 * X11DRV_WindowMessage (X11DRV.@)
2241 LRESULT X11DRV_WindowMessage( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
2243 struct x11drv_win_data *data;
2245 switch(msg)
2247 case WM_X11DRV_ACQUIRE_SELECTION:
2248 return X11DRV_AcquireClipboard( hwnd );
2249 case WM_X11DRV_DELETE_WINDOW:
2250 return SendMessageW( hwnd, WM_SYSCOMMAND, SC_CLOSE, 0 );
2251 case WM_X11DRV_SET_WIN_FORMAT:
2252 return set_win_format( hwnd, (XID)wp );
2253 case WM_X11DRV_SET_WIN_REGION:
2254 if ((data = X11DRV_get_win_data( hwnd ))) sync_window_region( thread_display(), data, (HRGN)1 );
2255 return 0;
2256 case WM_X11DRV_RESIZE_DESKTOP:
2257 X11DRV_resize_desktop( LOWORD(lp), HIWORD(lp) );
2258 return 0;
2259 default:
2260 FIXME( "got window msg %x hwnd %p wp %lx lp %lx\n", msg, hwnd, wp, lp );
2261 return 0;
2266 /***********************************************************************
2267 * is_netwm_supported
2269 static BOOL is_netwm_supported( Display *display, Atom atom )
2271 static Atom *net_supported;
2272 static int net_supported_count = -1;
2273 int i;
2275 wine_tsx11_lock();
2276 if (net_supported_count == -1)
2278 Atom type;
2279 int format;
2280 unsigned long count, remaining;
2282 if (!XGetWindowProperty( display, DefaultRootWindow(display), x11drv_atom(_NET_SUPPORTED), 0,
2283 ~0UL, False, XA_ATOM, &type, &format, &count,
2284 &remaining, (unsigned char **)&net_supported ))
2285 net_supported_count = get_property_size( format, count ) / sizeof(Atom);
2286 else
2287 net_supported_count = 0;
2289 wine_tsx11_unlock();
2291 for (i = 0; i < net_supported_count; i++)
2292 if (net_supported[i] == atom) return TRUE;
2293 return FALSE;
2297 /***********************************************************************
2298 * SysCommand (X11DRV.@)
2300 * Perform WM_SYSCOMMAND handling.
2302 LRESULT X11DRV_SysCommand( HWND hwnd, WPARAM wparam, LPARAM lparam )
2304 WPARAM hittest = wparam & 0x0f;
2305 DWORD dwPoint;
2306 int x, y, dir;
2307 XEvent xev;
2308 Display *display = thread_display();
2309 struct x11drv_win_data *data;
2311 if (!(data = X11DRV_get_win_data( hwnd ))) return -1;
2312 if (!data->whole_window || !data->managed || !data->mapped) return -1;
2314 switch (wparam & 0xfff0)
2316 case SC_MOVE:
2317 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
2318 else dir = _NET_WM_MOVERESIZE_MOVE;
2319 break;
2320 case SC_SIZE:
2321 /* windows without WS_THICKFRAME are not resizable through the window manager */
2322 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_THICKFRAME)) return -1;
2324 switch (hittest)
2326 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
2327 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
2328 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
2329 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
2330 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
2331 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
2332 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
2333 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
2334 default: dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD; break;
2336 break;
2338 case SC_KEYMENU:
2339 /* prevent a simple ALT press+release from activating the system menu,
2340 * as that can get confusing on managed windows */
2341 if ((WCHAR)lparam) return -1; /* got an explicit char */
2342 if (GetMenu( hwnd )) return -1; /* window has a real menu */
2343 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) return -1; /* no system menu */
2344 TRACE( "ignoring SC_KEYMENU wp %lx lp %lx\n", wparam, lparam );
2345 return 0;
2347 default:
2348 return -1;
2351 if (IsZoomed(hwnd)) return -1;
2353 if (!is_netwm_supported( display, x11drv_atom(_NET_WM_MOVERESIZE) ))
2355 TRACE( "_NET_WM_MOVERESIZE not supported\n" );
2356 return -1;
2359 dwPoint = GetMessagePos();
2360 x = (short)LOWORD(dwPoint);
2361 y = (short)HIWORD(dwPoint);
2363 TRACE("hwnd %p, x %d, y %d, dir %d\n", hwnd, x, y, dir);
2365 xev.xclient.type = ClientMessage;
2366 xev.xclient.window = X11DRV_get_whole_window(hwnd);
2367 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
2368 xev.xclient.serial = 0;
2369 xev.xclient.display = display;
2370 xev.xclient.send_event = True;
2371 xev.xclient.format = 32;
2372 xev.xclient.data.l[0] = x - virtual_screen_rect.left; /* x coord */
2373 xev.xclient.data.l[1] = y - virtual_screen_rect.top; /* y coord */
2374 xev.xclient.data.l[2] = dir; /* direction */
2375 xev.xclient.data.l[3] = 1; /* button */
2376 xev.xclient.data.l[4] = 0; /* unused */
2378 /* need to ungrab the pointer that may have been automatically grabbed
2379 * with a ButtonPress event */
2380 wine_tsx11_lock();
2381 XUngrabPointer( display, CurrentTime );
2382 XSendEvent(display, root_window, False, SubstructureNotifyMask, &xev);
2383 wine_tsx11_unlock();
2384 return 0;