push c03b0bb7c67acf502e715114ece376179b90ac7c
[wine/hacks.git] / dlls / winex11.drv / window.c
blob3aa97aa59e33d3311e8e40ad11dd0bb62c98423d
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_text
403 static void sync_window_text( Display *display, Window win, const WCHAR *text )
405 UINT count;
406 char *buffer, *utf8_buffer;
407 XTextProperty prop;
409 /* allocate new buffer for window text */
410 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
411 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count ))) return;
412 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
414 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
415 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
417 HeapFree( GetProcessHeap(), 0, buffer );
418 return;
420 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
422 wine_tsx11_lock();
423 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
425 XSetWMName( display, win, &prop );
426 XSetWMIconName( display, win, &prop );
427 XFree( prop.value );
430 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
431 according to the standard
432 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
434 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
435 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
436 wine_tsx11_unlock();
438 HeapFree( GetProcessHeap(), 0, utf8_buffer );
439 HeapFree( GetProcessHeap(), 0, buffer );
443 /***********************************************************************
444 * set_win_format
446 static BOOL set_win_format( HWND hwnd, XID fbconfig_id )
448 struct x11drv_win_data *data;
449 XVisualInfo *vis;
450 int w, h;
452 if (!(data = X11DRV_get_win_data(hwnd)) &&
453 !(data = X11DRV_create_win_data(hwnd))) return FALSE;
455 if (!(vis = visual_from_fbconfig_id(fbconfig_id))) return FALSE;
457 if (data->whole_window)
459 Display *display = thread_display();
460 Window client = data->client_window;
462 if (vis->visualid != data->visualid)
464 client = create_client_window( display, data, vis );
465 TRACE( "re-created client window %lx for %p fbconfig %lx\n", client, data->hwnd, fbconfig_id );
467 wine_tsx11_lock();
468 XFree(vis);
469 XFlush( display );
470 wine_tsx11_unlock();
471 if (client) goto done;
472 return FALSE;
475 w = data->client_rect.right - data->client_rect.left;
476 h = data->client_rect.bottom - data->client_rect.top;
478 if(w <= 0) w = 1;
479 if(h <= 0) h = 1;
481 #ifdef SONAME_LIBXCOMPOSITE
482 if(usexcomposite)
484 XSetWindowAttributes attrib;
485 static Window dummy_parent;
487 wine_tsx11_lock();
488 attrib.override_redirect = True;
489 if (!dummy_parent)
491 dummy_parent = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, screen_depth,
492 InputOutput, visual, CWOverrideRedirect, &attrib );
493 XMapWindow( gdi_display, dummy_parent );
495 data->colormap = XCreateColormap(gdi_display, dummy_parent, vis->visual,
496 (vis->class == PseudoColor ||
497 vis->class == GrayScale ||
498 vis->class == DirectColor) ?
499 AllocAll : AllocNone);
500 attrib.colormap = data->colormap;
501 XInstallColormap(gdi_display, attrib.colormap);
503 if(data->gl_drawable) XDestroyWindow(gdi_display, data->gl_drawable);
504 data->gl_drawable = XCreateWindow(gdi_display, dummy_parent, -w, 0, w, h, 0,
505 vis->depth, InputOutput, vis->visual,
506 CWColormap | CWOverrideRedirect,
507 &attrib);
508 if(data->gl_drawable)
510 pXCompositeRedirectWindow(gdi_display, data->gl_drawable,
511 CompositeRedirectManual);
512 XMapWindow(gdi_display, data->gl_drawable);
514 XFree(vis);
515 XFlush( gdi_display );
516 wine_tsx11_unlock();
518 else
519 #endif
521 WARN("XComposite is not available, using GLXPixmap hack\n");
523 wine_tsx11_lock();
525 if(data->pixmap) XFreePixmap(gdi_display, data->pixmap);
526 data->pixmap = XCreatePixmap(gdi_display, root_window, w, h, vis->depth);
527 if(!data->pixmap)
529 XFree(vis);
530 wine_tsx11_unlock();
531 return FALSE;
534 if(data->gl_drawable) destroy_glxpixmap(gdi_display, data->gl_drawable);
535 data->gl_drawable = create_glxpixmap(gdi_display, vis, data->pixmap);
536 if(!data->gl_drawable)
538 XFreePixmap(gdi_display, data->pixmap);
539 data->pixmap = 0;
541 XFree(vis);
542 XFlush( gdi_display );
543 wine_tsx11_unlock();
544 if (data->pixmap) SetPropA(hwnd, pixmap_prop, (HANDLE)data->pixmap);
547 if (!data->gl_drawable) return FALSE;
549 TRACE("Created GL drawable 0x%lx, using FBConfigID 0x%lx\n",
550 data->gl_drawable, fbconfig_id);
551 SetPropA(hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
553 done:
554 data->fbconfig_id = fbconfig_id;
555 SetPropA(hwnd, fbconfig_id_prop, (HANDLE)data->fbconfig_id);
556 /* force DCE invalidation */
557 SetWindowPos( hwnd, 0, 0, 0, 0, 0,
558 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE |
559 SWP_NOREDRAW | SWP_DEFERERASE | SWP_NOSENDCHANGING | SWP_STATECHANGED);
560 return TRUE;
563 /***********************************************************************
564 * sync_gl_drawable
566 static void sync_gl_drawable(struct x11drv_win_data *data)
568 int w = data->client_rect.right - data->client_rect.left;
569 int h = data->client_rect.bottom - data->client_rect.top;
570 XVisualInfo *vis;
571 Drawable glxp;
572 Pixmap pix;
574 if (w <= 0) w = 1;
575 if (h <= 0) h = 1;
577 TRACE("Resizing GL drawable 0x%lx to %dx%d\n", data->gl_drawable, w, h);
578 #ifdef SONAME_LIBXCOMPOSITE
579 if(usexcomposite)
581 wine_tsx11_lock();
582 XMoveResizeWindow(gdi_display, data->gl_drawable, -w, 0, w, h);
583 wine_tsx11_unlock();
584 return;
586 #endif
588 if (!(vis = visual_from_fbconfig_id(data->fbconfig_id))) return;
590 wine_tsx11_lock();
591 pix = XCreatePixmap(gdi_display, root_window, w, h, vis->depth);
592 if(!pix)
594 ERR("Failed to create pixmap for offscreen rendering\n");
595 XFree(vis);
596 wine_tsx11_unlock();
597 return;
600 glxp = create_glxpixmap(gdi_display, vis, pix);
601 if(!glxp)
603 ERR("Failed to create drawable for offscreen rendering\n");
604 XFreePixmap(gdi_display, pix);
605 XFree(vis);
606 wine_tsx11_unlock();
607 return;
610 XFree(vis);
612 mark_drawable_dirty(data->gl_drawable, glxp);
614 XFreePixmap(gdi_display, data->pixmap);
615 destroy_glxpixmap(gdi_display, data->gl_drawable);
616 TRACE( "Recreated GL drawable %lx to replace %lx\n", glxp, data->gl_drawable );
618 data->pixmap = pix;
619 data->gl_drawable = glxp;
621 XFlush( gdi_display );
622 wine_tsx11_unlock();
624 SetPropA(data->hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
625 SetPropA(data->hwnd, pixmap_prop, (HANDLE)data->pixmap);
629 /***********************************************************************
630 * get_window_changes
632 * fill the window changes structure
634 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
636 int mask = 0;
638 if (old->right - old->left != new->right - new->left )
640 if ((changes->width = new->right - new->left) <= 0) changes->width = 1;
641 mask |= CWWidth;
643 if (old->bottom - old->top != new->bottom - new->top)
645 if ((changes->height = new->bottom - new->top) <= 0) changes->height = 1;
646 mask |= CWHeight;
648 if (old->left != new->left)
650 changes->x = new->left;
651 mask |= CWX;
653 if (old->top != new->top)
655 changes->y = new->top;
656 mask |= CWY;
658 return mask;
662 /***********************************************************************
663 * create_icon_window
665 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
667 XSetWindowAttributes attr;
669 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
670 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
671 attr.bit_gravity = NorthWestGravity;
672 attr.backing_store = NotUseful/*WhenMapped*/;
673 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
675 wine_tsx11_lock();
676 data->icon_window = XCreateWindow( display, root_window, 0, 0,
677 GetSystemMetrics( SM_CXICON ),
678 GetSystemMetrics( SM_CYICON ),
679 0, screen_depth,
680 InputOutput, visual,
681 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
682 XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
683 XFlush( display ); /* make sure the window exists before we start painting to it */
684 wine_tsx11_unlock();
686 TRACE( "created %lx\n", data->icon_window );
687 SetPropA( data->hwnd, icon_window_prop, (HANDLE)data->icon_window );
688 return data->icon_window;
693 /***********************************************************************
694 * destroy_icon_window
696 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
698 if (!data->icon_window) return;
699 if (x11drv_thread_data()->cursor_window == data->icon_window)
700 x11drv_thread_data()->cursor_window = None;
701 wine_tsx11_lock();
702 XDeleteContext( display, data->icon_window, winContext );
703 XDestroyWindow( display, data->icon_window );
704 data->icon_window = 0;
705 wine_tsx11_unlock();
706 RemovePropA( data->hwnd, icon_window_prop );
710 /***********************************************************************
711 * set_icon_hints
713 * Set the icon wm hints
715 static void set_icon_hints( Display *display, struct x11drv_win_data *data, HICON hIcon )
717 XWMHints *hints = data->wm_hints;
719 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
720 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
721 data->hWMIconBitmap = 0;
722 data->hWMIconMask = 0;
724 if (!hIcon)
726 if (!data->icon_window) create_icon_window( display, data );
727 hints->icon_window = data->icon_window;
728 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
730 else
732 HBITMAP hbmOrig;
733 RECT rcMask;
734 BITMAP bmMask;
735 ICONINFO ii;
736 HDC hDC;
738 GetIconInfo(hIcon, &ii);
740 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
741 rcMask.top = 0;
742 rcMask.left = 0;
743 rcMask.right = bmMask.bmWidth;
744 rcMask.bottom = bmMask.bmHeight;
746 hDC = CreateCompatibleDC(0);
747 hbmOrig = SelectObject(hDC, ii.hbmMask);
748 InvertRect(hDC, &rcMask);
749 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
750 SelectObject(hDC, hbmOrig);
751 DeleteDC(hDC);
753 data->hWMIconBitmap = ii.hbmColor;
754 data->hWMIconMask = ii.hbmMask;
756 hints->icon_pixmap = X11DRV_get_pixmap(data->hWMIconBitmap);
757 hints->icon_mask = X11DRV_get_pixmap(data->hWMIconMask);
758 destroy_icon_window( display, data );
759 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
764 /***********************************************************************
765 * set_size_hints
767 * set the window size hints
769 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
771 XSizeHints* size_hints;
773 if (!(size_hints = XAllocSizeHints())) return;
775 size_hints->win_gravity = StaticGravity;
776 size_hints->flags |= PWinGravity;
778 /* don't update size hints if window is not in normal state */
779 if (!(style & (WS_MINIMIZE | WS_MAXIMIZE)))
781 if (data->hwnd != GetDesktopWindow()) /* don't force position of desktop */
783 size_hints->x = data->whole_rect.left;
784 size_hints->y = data->whole_rect.top;
785 size_hints->flags |= PPosition;
788 if (!is_window_resizable( data, style ))
790 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
791 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
792 size_hints->min_width = size_hints->max_width;
793 size_hints->min_height = size_hints->max_height;
794 size_hints->flags |= PMinSize | PMaxSize;
797 XSetWMNormalHints( display, data->whole_window, size_hints );
798 XFree( size_hints );
802 /***********************************************************************
803 * get_process_name
805 * get the name of the current process for setting class hints
807 static char *get_process_name(void)
809 static char *name;
811 if (!name)
813 WCHAR module[MAX_PATH];
814 DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
815 if (len && len < MAX_PATH)
817 char *ptr;
818 WCHAR *p, *appname = module;
820 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
821 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
822 len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
823 if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
825 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
826 name = ptr;
830 return name;
834 /***********************************************************************
835 * set_initial_wm_hints
837 * Set the window manager hints that don't change over the lifetime of a window.
839 static void set_initial_wm_hints( Display *display, struct x11drv_win_data *data )
841 long i;
842 Atom protocols[3];
843 Atom dndVersion = 4;
844 XClassHint *class_hints;
845 char *process_name = get_process_name();
847 wine_tsx11_lock();
849 /* wm protocols */
850 i = 0;
851 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
852 protocols[i++] = x11drv_atom(_NET_WM_PING);
853 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
854 XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
855 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
857 /* class hints */
858 if ((class_hints = XAllocClassHint()))
860 static char wine[] = "Wine";
862 class_hints->res_name = process_name;
863 class_hints->res_class = wine;
864 XSetClassHint( display, data->whole_window, class_hints );
865 XFree( class_hints );
868 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
869 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
870 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
871 i = getpid();
872 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
873 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
875 XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
876 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
878 data->wm_hints = XAllocWMHints();
879 wine_tsx11_unlock();
881 if (data->wm_hints)
883 HICON icon = (HICON)SendMessageW( data->hwnd, WM_GETICON, ICON_BIG, 0 );
884 if (!icon) icon = (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON );
885 data->wm_hints->flags = 0;
886 set_icon_hints( display, data, icon );
891 /***********************************************************************
892 * set_wm_hints
894 * Set the window manager hints for a newly-created window
896 static void set_wm_hints( Display *display, struct x11drv_win_data *data )
898 Window group_leader;
899 Atom window_type;
900 MwmHints mwm_hints;
901 DWORD style, ex_style;
902 HWND owner;
904 if (data->hwnd == GetDesktopWindow())
906 /* force some styles for the desktop to get the correct decorations */
907 style = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
908 ex_style = WS_EX_APPWINDOW;
909 owner = 0;
911 else
913 style = GetWindowLongW( data->hwnd, GWL_STYLE );
914 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
915 owner = get_window_owner( data->hwnd );
918 /* transient for hint */
919 if (owner)
921 Window owner_win = X11DRV_get_whole_window( owner );
922 wine_tsx11_lock();
923 XSetTransientForHint( display, data->whole_window, owner_win );
924 wine_tsx11_unlock();
925 group_leader = owner_win;
927 else group_leader = data->whole_window;
929 wine_tsx11_lock();
931 /* size hints */
932 set_size_hints( display, data, style );
934 /* set the WM_WINDOW_TYPE */
935 if (style & WS_THICKFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
936 else if (ex_style & WS_EX_APPWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
937 else if (style & WS_DLGFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
938 else if (ex_style & WS_EX_DLGMODALFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
939 else if ((style & WS_POPUP) && owner) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
940 #if 0 /* many window managers don't handle utility windows very well */
941 else if (ex_style & WS_EX_TOOLWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
942 #endif
943 else window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
945 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
946 XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
948 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
949 mwm_hints.decorations = get_mwm_decorations( data, style, ex_style );
950 mwm_hints.functions = MWM_FUNC_MOVE;
951 if (is_window_resizable( data, style )) mwm_hints.functions |= MWM_FUNC_RESIZE;
952 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
953 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
954 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
956 XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
957 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
958 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
960 /* wm hints */
961 if (data->wm_hints)
963 data->wm_hints->flags |= InputHint | StateHint | WindowGroupHint;
964 data->wm_hints->input = !(style & WS_DISABLED);
965 data->wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
966 data->wm_hints->window_group = group_leader;
967 XSetWMHints( display, data->whole_window, data->wm_hints );
970 wine_tsx11_unlock();
974 /***********************************************************************
975 * update_net_wm_states
977 void update_net_wm_states( Display *display, struct x11drv_win_data *data )
979 static const unsigned int state_atoms[NB_NET_WM_STATES] =
981 XATOM__NET_WM_STATE_FULLSCREEN,
982 XATOM__NET_WM_STATE_ABOVE,
983 XATOM__NET_WM_STATE_MAXIMIZED_VERT,
984 XATOM__NET_WM_STATE_SKIP_PAGER,
985 XATOM__NET_WM_STATE_SKIP_TASKBAR
988 DWORD i, style, ex_style, new_state = 0;
990 if (!data->managed) return;
991 if (data->whole_window == root_window) return;
993 style = GetWindowLongW( data->hwnd, GWL_STYLE );
994 if (data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
995 data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height)
997 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION)
998 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
999 else if (!(style & WS_MINIMIZE))
1000 new_state |= (1 << NET_WM_STATE_FULLSCREEN);
1002 else if (style & WS_MAXIMIZE)
1003 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
1005 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
1006 if (ex_style & WS_EX_TOPMOST)
1007 new_state |= (1 << NET_WM_STATE_ABOVE);
1008 if (ex_style & WS_EX_TOOLWINDOW)
1009 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR) | (1 << NET_WM_STATE_SKIP_PAGER);
1010 if (!(ex_style & WS_EX_APPWINDOW) && get_window_owner( data->hwnd ))
1011 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR);
1013 if (!data->mapped) /* set the _NET_WM_STATE atom directly */
1015 Atom atoms[NB_NET_WM_STATES+1];
1016 DWORD count;
1018 for (i = count = 0; i < NB_NET_WM_STATES; i++)
1020 if (!(new_state & (1 << i))) continue;
1021 TRACE( "setting wm state %u for unmapped window %p/%lx\n",
1022 i, data->hwnd, data->whole_window );
1023 atoms[count++] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1024 if (state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT)
1025 atoms[count++] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
1027 wine_tsx11_lock();
1028 XChangeProperty( display, data->whole_window, x11drv_atom(_NET_WM_STATE), XA_ATOM,
1029 32, PropModeReplace, (unsigned char *)atoms, count );
1030 wine_tsx11_unlock();
1032 else /* ask the window manager to do it for us */
1034 XEvent xev;
1036 xev.xclient.type = ClientMessage;
1037 xev.xclient.window = data->whole_window;
1038 xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
1039 xev.xclient.serial = 0;
1040 xev.xclient.display = display;
1041 xev.xclient.send_event = True;
1042 xev.xclient.format = 32;
1043 xev.xclient.data.l[3] = 1;
1045 for (i = 0; i < NB_NET_WM_STATES; i++)
1047 if (!((data->net_wm_state ^ new_state) & (1 << i))) continue; /* unchanged */
1049 TRACE( "setting wm state %u for window %p/%lx to %u prev %u\n",
1050 i, data->hwnd, data->whole_window,
1051 (new_state & (1 << i)) != 0, (data->net_wm_state & (1 << i)) != 0 );
1053 xev.xclient.data.l[0] = (new_state & (1 << i)) ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1054 xev.xclient.data.l[1] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1055 xev.xclient.data.l[2] = ((state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT) ?
1056 x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ) : 0);
1057 wine_tsx11_lock();
1058 XSendEvent( display, root_window, False,
1059 SubstructureRedirectMask | SubstructureNotifyMask, &xev );
1060 wine_tsx11_unlock();
1063 data->net_wm_state = new_state;
1067 /***********************************************************************
1068 * set_xembed_flags
1070 static void set_xembed_flags( Display *display, struct x11drv_win_data *data, unsigned long flags )
1072 unsigned long info[2];
1074 info[0] = 0; /* protocol version */
1075 info[1] = flags;
1076 wine_tsx11_lock();
1077 XChangeProperty( display, data->whole_window, x11drv_atom(_XEMBED_INFO),
1078 x11drv_atom(_XEMBED_INFO), 32, PropModeReplace, (unsigned char*)info, 2 );
1079 wine_tsx11_unlock();
1083 /***********************************************************************
1084 * map_window
1086 static void map_window( Display *display, struct x11drv_win_data *data, DWORD new_style )
1088 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1090 wait_for_withdrawn_state( display, data, TRUE );
1092 if (!data->embedded)
1094 update_net_wm_states( display, data );
1095 sync_window_style( display, data );
1096 wine_tsx11_lock();
1097 XMapWindow( display, data->whole_window );
1098 wine_tsx11_unlock();
1100 else set_xembed_flags( display, data, XEMBED_MAPPED );
1102 data->mapped = TRUE;
1103 data->iconic = (new_style & WS_MINIMIZE) != 0;
1107 /***********************************************************************
1108 * unmap_window
1110 static void unmap_window( Display *display, struct x11drv_win_data *data )
1112 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1114 if (!data->embedded)
1116 wait_for_withdrawn_state( display, data, FALSE );
1117 wine_tsx11_lock();
1118 if (data->managed) XWithdrawWindow( display, data->whole_window, DefaultScreen(display) );
1119 else XUnmapWindow( display, data->whole_window );
1120 wine_tsx11_unlock();
1122 else set_xembed_flags( display, data, 0 );
1124 data->mapped = FALSE;
1125 data->net_wm_state = 0;
1129 /***********************************************************************
1130 * make_window_embedded
1132 void make_window_embedded( Display *display, struct x11drv_win_data *data )
1134 if (data->mapped)
1136 /* the window cannot be mapped before being embedded */
1137 unmap_window( display, data );
1138 data->embedded = TRUE;
1139 map_window( display, data, 0 );
1141 else
1143 data->embedded = TRUE;
1144 set_xembed_flags( display, data, 0 );
1149 /***********************************************************************
1150 * X11DRV_window_to_X_rect
1152 * Convert a rect from client to X window coordinates
1154 void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
1156 RECT rc;
1158 if (!data->managed) return;
1159 if (IsRectEmpty( rect )) return;
1161 get_x11_rect_offset( data, &rc );
1163 rect->left -= rc.left;
1164 rect->right -= rc.right;
1165 rect->top -= rc.top;
1166 rect->bottom -= rc.bottom;
1167 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1168 if (rect->left >= rect->right) rect->right = rect->left + 1;
1172 /***********************************************************************
1173 * X11DRV_X_to_window_rect
1175 * Opposite of X11DRV_window_to_X_rect
1177 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
1179 RECT rc;
1181 if (!data->managed) return;
1182 if (IsRectEmpty( rect )) return;
1184 get_x11_rect_offset( data, &rc );
1186 rect->left += rc.left;
1187 rect->right += rc.right;
1188 rect->top += rc.top;
1189 rect->bottom += rc.bottom;
1190 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1191 if (rect->left >= rect->right) rect->right = rect->left + 1;
1195 /***********************************************************************
1196 * sync_window_position
1198 * Synchronize the X window position with the Windows one
1200 static void sync_window_position( Display *display, struct x11drv_win_data *data,
1201 UINT swp_flags, const RECT *old_client_rect,
1202 const RECT *old_whole_rect )
1204 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
1205 XWindowChanges changes;
1206 unsigned int mask = 0;
1208 if (data->managed && data->iconic) return;
1210 /* resizing a managed maximized window is not allowed */
1211 if (!(style & WS_MAXIMIZE) || !data->managed)
1213 if ((changes.width = data->whole_rect.right - data->whole_rect.left) <= 0) changes.width = 1;
1214 if ((changes.height = data->whole_rect.bottom - data->whole_rect.top) <= 0) changes.height = 1;
1215 mask |= CWWidth | CWHeight;
1218 /* only the size is allowed to change for the desktop window */
1219 if (data->whole_window != root_window)
1221 changes.x = data->whole_rect.left - virtual_screen_rect.left;
1222 changes.y = data->whole_rect.top - virtual_screen_rect.top;
1223 mask |= CWX | CWY;
1226 if (!(swp_flags & SWP_NOZORDER) || (swp_flags & SWP_SHOWWINDOW))
1228 /* find window that this one must be after */
1229 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
1230 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
1231 prev = GetWindow( prev, GW_HWNDPREV );
1232 if (!prev) /* top child */
1234 changes.stack_mode = Above;
1235 mask |= CWStackMode;
1237 /* should use stack_mode Below but most window managers don't get it right */
1238 /* and Above with a sibling doesn't work so well either, so we ignore it */
1241 TRACE( "setting win %p/%lx pos %d,%d,%dx%d after %lx changes=%x\n",
1242 data->hwnd, data->whole_window, data->whole_rect.left, data->whole_rect.top,
1243 data->whole_rect.right - data->whole_rect.left,
1244 data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
1246 wine_tsx11_lock();
1247 set_size_hints( display, data, style );
1248 XReconfigureWMWindow( display, data->whole_window,
1249 DefaultScreen(display), mask, &changes );
1250 wine_tsx11_unlock();
1254 /***********************************************************************
1255 * sync_client_position
1257 * Synchronize the X client window position with the Windows one
1259 static void sync_client_position( Display *display, struct x11drv_win_data *data,
1260 UINT swp_flags, const RECT *old_client_rect,
1261 const RECT *old_whole_rect )
1263 int mask;
1264 XWindowChanges changes;
1265 RECT old = *old_client_rect;
1266 RECT new = data->client_rect;
1268 OffsetRect( &old, -old_whole_rect->left, -old_whole_rect->top );
1269 OffsetRect( &new, -data->whole_rect.left, -data->whole_rect.top );
1270 if (!(mask = get_window_changes( &changes, &old, &new ))) return;
1272 if (data->client_window)
1274 TRACE( "setting client win %lx pos %d,%d,%dx%d changes=%x\n",
1275 data->client_window, new.left, new.top,
1276 new.right - new.left, new.bottom - new.top, mask );
1277 wine_tsx11_lock();
1278 XConfigureWindow( display, data->client_window, mask, &changes );
1279 wine_tsx11_unlock();
1282 if (data->gl_drawable && (mask & (CWWidth|CWHeight))) sync_gl_drawable( data );
1286 /***********************************************************************
1287 * move_window_bits
1289 * Move the window bits when a window is moved.
1291 static void move_window_bits( struct x11drv_win_data *data, const RECT *old_rect, const RECT *new_rect,
1292 const RECT *old_client_rect )
1294 RECT src_rect = *old_rect;
1295 RECT dst_rect = *new_rect;
1296 HDC hdc_src, hdc_dst;
1297 INT code;
1298 HRGN rgn = 0;
1299 HWND parent = 0;
1301 if (!data->whole_window)
1303 OffsetRect( &dst_rect, -data->window_rect.left, -data->window_rect.top );
1304 parent = GetAncestor( data->hwnd, GA_PARENT );
1305 hdc_src = GetDCEx( parent, 0, DCX_CACHE );
1306 hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE | DCX_WINDOW );
1308 else
1310 OffsetRect( &dst_rect, -data->client_rect.left, -data->client_rect.top );
1311 /* make src rect relative to the old position of the window */
1312 OffsetRect( &src_rect, -old_client_rect->left, -old_client_rect->top );
1313 if (dst_rect.left == src_rect.left && dst_rect.top == src_rect.top) return;
1314 hdc_src = hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE );
1317 code = X11DRV_START_EXPOSURES;
1318 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
1320 TRACE( "copying bits for win %p/%lx/%lx %s -> %s\n",
1321 data->hwnd, data->whole_window, data->client_window,
1322 wine_dbgstr_rect(&src_rect), wine_dbgstr_rect(&dst_rect) );
1323 BitBlt( hdc_dst, dst_rect.left, dst_rect.top,
1324 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top,
1325 hdc_src, src_rect.left, src_rect.top, SRCCOPY );
1327 code = X11DRV_END_EXPOSURES;
1328 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, sizeof(rgn), (LPSTR)&rgn );
1330 ReleaseDC( data->hwnd, hdc_dst );
1331 if (hdc_src != hdc_dst) ReleaseDC( parent, hdc_src );
1333 if (rgn)
1335 if (!data->whole_window)
1337 /* map region to client rect since we are using DCX_WINDOW */
1338 OffsetRgn( rgn, data->window_rect.left - data->client_rect.left,
1339 data->window_rect.top - data->client_rect.top );
1340 RedrawWindow( data->hwnd, NULL, rgn,
1341 RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ALLCHILDREN );
1343 else RedrawWindow( data->hwnd, NULL, rgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1344 DeleteObject( rgn );
1349 /**********************************************************************
1350 * create_whole_window
1352 * Create the whole X window for a given window
1354 static Window create_whole_window( Display *display, struct x11drv_win_data *data )
1356 int cx, cy, mask;
1357 XSetWindowAttributes attr;
1358 WCHAR text[1024];
1360 if (!(cx = data->window_rect.right - data->window_rect.left)) cx = 1;
1361 if (!(cy = data->window_rect.bottom - data->window_rect.top)) cy = 1;
1363 if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1365 TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1366 data->managed = TRUE;
1367 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1370 mask = get_window_attributes( display, data, &attr );
1372 wine_tsx11_lock();
1374 data->whole_rect = data->window_rect;
1375 data->whole_window = XCreateWindow( display, root_window,
1376 data->window_rect.left - virtual_screen_rect.left,
1377 data->window_rect.top - virtual_screen_rect.top,
1378 cx, cy, 0, screen_depth, InputOutput,
1379 visual, mask, &attr );
1381 if (data->whole_window) XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
1382 wine_tsx11_unlock();
1384 if (!data->whole_window) return 0;
1386 if (!create_client_window( display, data, NULL ))
1388 wine_tsx11_lock();
1389 XDeleteContext( display, data->whole_window, winContext );
1390 XDestroyWindow( display, data->whole_window );
1391 data->whole_window = 0;
1392 wine_tsx11_unlock();
1393 return 0;
1396 set_initial_wm_hints( display, data );
1397 set_wm_hints( display, data );
1399 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1401 /* set the window text */
1402 if (!InternalGetWindowText( data->hwnd, text, sizeof(text)/sizeof(WCHAR) )) text[0] = 0;
1403 sync_window_text( display, data->whole_window, text );
1405 /* set the window region */
1406 sync_window_region( display, data, (HRGN)1 );
1408 wine_tsx11_lock();
1409 XFlush( display ); /* make sure the window exists before we start painting to it */
1410 wine_tsx11_unlock();
1411 return data->whole_window;
1415 /**********************************************************************
1416 * destroy_whole_window
1418 * Destroy the whole X window for a given window.
1420 static void destroy_whole_window( Display *display, struct x11drv_win_data *data, BOOL already_destroyed )
1422 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1424 if (!data->whole_window) return;
1426 TRACE( "win %p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1427 if (thread_data->cursor_window == data->whole_window ||
1428 thread_data->cursor_window == data->client_window)
1429 thread_data->cursor_window = None;
1430 wine_tsx11_lock();
1431 XDeleteContext( display, data->whole_window, winContext );
1432 XDeleteContext( display, data->client_window, winContext );
1433 if (!already_destroyed) XDestroyWindow( display, data->whole_window );
1434 data->whole_window = data->client_window = 0;
1435 data->wm_state = WithdrawnState;
1436 data->net_wm_state = 0;
1437 data->mapped = FALSE;
1438 if (data->xic)
1440 XUnsetICFocus( data->xic );
1441 XDestroyIC( data->xic );
1442 data->xic = 0;
1444 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1445 XFlush( display );
1446 XFree( data->wm_hints );
1447 data->wm_hints = NULL;
1448 wine_tsx11_unlock();
1449 RemovePropA( data->hwnd, whole_window_prop );
1450 RemovePropA( data->hwnd, client_window_prop );
1454 /*****************************************************************
1455 * SetWindowText (X11DRV.@)
1457 void X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1459 Window win;
1461 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(gdi_display))
1463 Display *display = thread_init_display();
1464 sync_window_text( display, win, text );
1469 /***********************************************************************
1470 * SetWindowStyle (X11DRV.@)
1472 * Update the X state of a window to reflect a style change
1474 void X11DRV_SetWindowStyle( HWND hwnd, DWORD old_style )
1476 struct x11drv_win_data *data;
1477 DWORD new_style, changed;
1479 if (hwnd == GetDesktopWindow()) return;
1480 new_style = GetWindowLongW( hwnd, GWL_STYLE );
1481 changed = new_style ^ old_style;
1483 if ((changed & WS_VISIBLE) && (new_style & WS_VISIBLE))
1485 /* we don't unmap windows, that causes trouble with the window manager */
1486 if (!(data = X11DRV_get_win_data( hwnd )) &&
1487 !(data = X11DRV_create_win_data( hwnd ))) return;
1489 if (data->whole_window && is_window_rect_mapped( &data->window_rect ))
1491 Display *display = thread_display();
1492 set_wm_hints( display, data );
1493 if (!data->mapped) map_window( display, data, new_style );
1497 if (changed & WS_DISABLED)
1499 data = X11DRV_get_win_data( hwnd );
1500 if (data && data->wm_hints)
1502 wine_tsx11_lock();
1503 data->wm_hints->input = !(new_style & WS_DISABLED);
1504 XSetWMHints( thread_display(), data->whole_window, data->wm_hints );
1505 wine_tsx11_unlock();
1511 /***********************************************************************
1512 * DestroyWindow (X11DRV.@)
1514 void X11DRV_DestroyWindow( HWND hwnd )
1516 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1517 struct x11drv_win_data *data;
1519 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1521 if (data->pixmap)
1523 wine_tsx11_lock();
1524 destroy_glxpixmap(gdi_display, data->gl_drawable);
1525 XFreePixmap(gdi_display, data->pixmap);
1526 wine_tsx11_unlock();
1528 else if (data->gl_drawable)
1530 wine_tsx11_lock();
1531 XDestroyWindow(gdi_display, data->gl_drawable);
1532 wine_tsx11_unlock();
1535 destroy_whole_window( thread_data->display, data, FALSE );
1536 destroy_icon_window( thread_data->display, data );
1538 if (data->colormap)
1540 wine_tsx11_lock();
1541 XFreeColormap( thread_data->display, data->colormap );
1542 wine_tsx11_unlock();
1545 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1546 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
1547 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
1548 wine_tsx11_lock();
1549 XDeleteContext( thread_data->display, (XID)hwnd, win_data_context );
1550 wine_tsx11_unlock();
1551 HeapFree( GetProcessHeap(), 0, data );
1555 /***********************************************************************
1556 * X11DRV_DestroyNotify
1558 void X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1560 Display *display = event->xdestroywindow.display;
1561 struct x11drv_win_data *data;
1563 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1565 FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1566 destroy_whole_window( display, data, TRUE );
1570 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
1572 struct x11drv_win_data *data;
1574 if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
1576 data->hwnd = hwnd;
1577 wine_tsx11_lock();
1578 if (!winContext) winContext = XUniqueContext();
1579 if (!win_data_context) win_data_context = XUniqueContext();
1580 XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
1581 wine_tsx11_unlock();
1583 return data;
1587 /* initialize the desktop window id in the desktop manager process */
1588 static struct x11drv_win_data *create_desktop_win_data( Display *display, HWND hwnd )
1590 struct x11drv_win_data *data;
1592 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1593 data->whole_window = data->client_window = root_window;
1594 data->managed = TRUE;
1595 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1596 SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
1597 SetPropA( data->hwnd, client_window_prop, (HANDLE)root_window );
1598 set_initial_wm_hints( display, data );
1599 return data;
1602 /**********************************************************************
1603 * CreateDesktopWindow (X11DRV.@)
1605 BOOL X11DRV_CreateDesktopWindow( HWND hwnd )
1607 unsigned int width, height;
1609 /* retrieve the real size of the desktop */
1610 SERVER_START_REQ( get_window_rectangles )
1612 req->handle = hwnd;
1613 wine_server_call( req );
1614 width = reply->window.right - reply->window.left;
1615 height = reply->window.bottom - reply->window.top;
1617 SERVER_END_REQ;
1619 if (!width && !height) /* not initialized yet */
1621 SERVER_START_REQ( set_window_pos )
1623 req->handle = hwnd;
1624 req->previous = 0;
1625 req->flags = SWP_NOZORDER;
1626 req->window.left = virtual_screen_rect.left;
1627 req->window.top = virtual_screen_rect.top;
1628 req->window.right = virtual_screen_rect.right;
1629 req->window.bottom = virtual_screen_rect.bottom;
1630 req->client = req->window;
1631 wine_server_call( req );
1633 SERVER_END_REQ;
1635 else
1637 Window win = (Window)GetPropA( hwnd, whole_window_prop );
1638 if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1640 return TRUE;
1644 /**********************************************************************
1645 * CreateWindow (X11DRV.@)
1647 BOOL X11DRV_CreateWindow( HWND hwnd )
1649 if (hwnd == GetDesktopWindow() && root_window != DefaultRootWindow( gdi_display ))
1651 Display *display = thread_init_display();
1653 /* the desktop win data can't be created lazily */
1654 if (!create_desktop_win_data( display, hwnd )) return FALSE;
1656 return TRUE;
1660 /***********************************************************************
1661 * X11DRV_get_win_data
1663 * Return the X11 data structure associated with a window.
1665 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1667 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1668 char *data;
1670 if (!thread_data) return NULL;
1671 if (!hwnd) return NULL;
1672 if (XFindContext( thread_data->display, (XID)hwnd, win_data_context, &data )) data = NULL;
1673 return (struct x11drv_win_data *)data;
1677 /***********************************************************************
1678 * X11DRV_create_win_data
1680 * Create an X11 data window structure for an existing window.
1682 struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd )
1684 Display *display;
1685 struct x11drv_win_data *data;
1686 HWND parent;
1688 if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL; /* desktop */
1690 /* don't create win data for HWND_MESSAGE windows */
1691 if (parent != GetDesktopWindow() && !GetAncestor( parent, GA_PARENT )) return NULL;
1693 display = thread_init_display();
1694 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1696 GetWindowRect( hwnd, &data->window_rect );
1697 MapWindowPoints( 0, parent, (POINT *)&data->window_rect, 2 );
1698 data->whole_rect = data->window_rect;
1699 GetClientRect( hwnd, &data->client_rect );
1700 MapWindowPoints( hwnd, parent, (POINT *)&data->client_rect, 2 );
1702 if (parent == GetDesktopWindow())
1704 if (!create_whole_window( display, data ))
1706 HeapFree( GetProcessHeap(), 0, data );
1707 return NULL;
1709 TRACE( "win %p/%lx/%lx window %s whole %s client %s\n",
1710 hwnd, data->whole_window, data->client_window, wine_dbgstr_rect( &data->window_rect ),
1711 wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
1713 return data;
1717 /***********************************************************************
1718 * X11DRV_get_whole_window
1720 * Return the X window associated with the full area of a window
1722 Window X11DRV_get_whole_window( HWND hwnd )
1724 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1726 if (!data)
1728 if (hwnd == GetDesktopWindow()) return root_window;
1729 return (Window)GetPropA( hwnd, whole_window_prop );
1731 return data->whole_window;
1735 /***********************************************************************
1736 * X11DRV_get_client_window
1738 * Return the X window associated with the client area of a window
1740 Window X11DRV_get_client_window( HWND hwnd )
1742 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1744 if (!data)
1746 if (hwnd == GetDesktopWindow()) return root_window;
1747 return (Window)GetPropA( hwnd, client_window_prop );
1749 return data->client_window;
1753 /***********************************************************************
1754 * X11DRV_get_ic
1756 * Return the X input context associated with a window
1758 XIC X11DRV_get_ic( HWND hwnd )
1760 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1761 XIM xim;
1763 if (!data) return 0;
1764 if (data->xic) return data->xic;
1765 if (!(xim = x11drv_thread_data()->xim)) return 0;
1766 return X11DRV_CreateIC( xim, data );
1770 /***********************************************************************
1771 * X11DRV_GetDC (X11DRV.@)
1773 void X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
1774 const RECT *top_rect, DWORD flags )
1776 struct x11drv_escape_set_drawable escape;
1777 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1779 escape.code = X11DRV_SET_DRAWABLE;
1780 escape.mode = IncludeInferiors;
1781 escape.fbconfig_id = 0;
1782 escape.gl_drawable = 0;
1783 escape.pixmap = 0;
1784 escape.gl_copy = FALSE;
1786 if (top == hwnd && data && IsIconic( hwnd ) && data->icon_window)
1788 escape.drawable = data->icon_window;
1790 else if (top == hwnd)
1792 escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
1793 /* GL draws to the client area even for window DCs */
1794 escape.gl_drawable = data ? data->client_window : X11DRV_get_client_window( hwnd );
1795 if (flags & DCX_WINDOW)
1796 escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
1797 else
1798 escape.drawable = escape.gl_drawable;
1800 else
1802 escape.drawable = X11DRV_get_client_window( top );
1803 escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
1804 escape.gl_drawable = data ? data->gl_drawable : (Drawable)GetPropA( hwnd, gl_drawable_prop );
1805 escape.pixmap = data ? data->pixmap : (Pixmap)GetPropA( hwnd, pixmap_prop );
1806 escape.gl_copy = (escape.gl_drawable != 0);
1807 if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
1810 escape.dc_rect.left = win_rect->left - top_rect->left;
1811 escape.dc_rect.top = win_rect->top - top_rect->top;
1812 escape.dc_rect.right = win_rect->right - top_rect->left;
1813 escape.dc_rect.bottom = win_rect->bottom - top_rect->top;
1814 escape.drawable_rect.left = top_rect->left;
1815 escape.drawable_rect.top = top_rect->top;
1816 escape.drawable_rect.right = top_rect->right;
1817 escape.drawable_rect.bottom = top_rect->bottom;
1819 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1823 /***********************************************************************
1824 * X11DRV_ReleaseDC (X11DRV.@)
1826 void X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
1828 struct x11drv_escape_set_drawable escape;
1830 escape.code = X11DRV_SET_DRAWABLE;
1831 escape.drawable = root_window;
1832 escape.mode = IncludeInferiors;
1833 escape.drawable_rect = virtual_screen_rect;
1834 SetRect( &escape.dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
1835 virtual_screen_rect.bottom - virtual_screen_rect.top );
1836 escape.fbconfig_id = 0;
1837 escape.gl_drawable = 0;
1838 escape.pixmap = 0;
1839 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1843 /***********************************************************************
1844 * SetCapture (X11DRV.@)
1846 void X11DRV_SetCapture( HWND hwnd, UINT flags )
1848 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1850 if (!thread_data) return;
1851 if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
1853 if (hwnd)
1855 Window grab_win = X11DRV_get_client_window( GetAncestor( hwnd, GA_ROOT ) );
1857 if (!grab_win) return;
1858 wine_tsx11_lock();
1859 XFlush( gdi_display );
1860 XGrabPointer( thread_data->display, grab_win, False,
1861 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1862 GrabModeAsync, GrabModeAsync, None, None, CurrentTime );
1863 wine_tsx11_unlock();
1864 thread_data->grab_window = grab_win;
1866 else /* release capture */
1868 wine_tsx11_lock();
1869 XFlush( gdi_display );
1870 XUngrabPointer( thread_data->display, CurrentTime );
1871 wine_tsx11_unlock();
1872 thread_data->grab_window = None;
1877 /*****************************************************************
1878 * SetParent (X11DRV.@)
1880 void X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
1882 Display *display = thread_display();
1883 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1885 if (!data) return;
1886 if (parent == old_parent) return;
1888 if (parent != GetDesktopWindow()) /* a child window */
1890 if (old_parent == GetDesktopWindow())
1892 /* destroy the old X windows */
1893 destroy_whole_window( display, data, FALSE );
1894 destroy_icon_window( display, data );
1895 if (data->managed)
1897 data->managed = FALSE;
1898 RemovePropA( data->hwnd, managed_prop );
1902 else /* new top level window */
1904 /* FIXME: we ignore errors since we can't really recover anyway */
1905 create_whole_window( display, data );
1910 /*****************************************************************
1911 * SetFocus (X11DRV.@)
1913 * Set the X focus.
1915 void X11DRV_SetFocus( HWND hwnd )
1917 Display *display = thread_display();
1918 struct x11drv_win_data *data;
1919 XWindowChanges changes;
1921 if (!(hwnd = GetAncestor( hwnd, GA_ROOT ))) return;
1922 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1923 if (data->managed || !data->whole_window) return;
1925 /* Set X focus and install colormap */
1926 wine_tsx11_lock();
1927 changes.stack_mode = Above;
1928 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
1929 if (root_window == DefaultRootWindow(display))
1931 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1932 /* FIXME: this is not entirely correct */
1933 XSetInputFocus( display, data->whole_window, RevertToParent,
1934 /* CurrentTime */
1935 GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1937 wine_tsx11_unlock();
1941 /***********************************************************************
1942 * WindowPosChanging (X11DRV.@)
1944 void X11DRV_WindowPosChanging( HWND hwnd, HWND insert_after, UINT swp_flags,
1945 const RECT *window_rect, const RECT *client_rect, RECT *visible_rect )
1947 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1948 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
1950 if (!data)
1952 /* create the win data if the window is being made visible */
1953 if (!(style & WS_VISIBLE) && !(swp_flags & SWP_SHOWWINDOW)) return;
1954 if (!(data = X11DRV_create_win_data( hwnd ))) return;
1957 /* check if we need to switch the window to managed */
1958 if (!data->managed && data->whole_window && is_window_managed( hwnd, swp_flags, window_rect ))
1960 TRACE( "making win %p/%lx managed\n", hwnd, data->whole_window );
1961 if (data->mapped) unmap_window( thread_display(), data );
1962 data->managed = TRUE;
1963 SetPropA( hwnd, managed_prop, (HANDLE)1 );
1966 *visible_rect = *window_rect;
1967 X11DRV_window_to_X_rect( data, visible_rect );
1971 /***********************************************************************
1972 * WindowPosChanged (X11DRV.@)
1974 void X11DRV_WindowPosChanged( HWND hwnd, HWND insert_after, UINT swp_flags,
1975 const RECT *rectWindow, const RECT *rectClient,
1976 const RECT *visible_rect, const RECT *valid_rects )
1978 struct x11drv_thread_data *thread_data;
1979 Display *display;
1980 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1981 DWORD new_style = GetWindowLongW( hwnd, GWL_STYLE );
1982 RECT old_whole_rect, old_client_rect;
1983 int event_type;
1985 if (!data) return;
1987 thread_data = x11drv_thread_data();
1988 display = thread_data->display;
1990 old_whole_rect = data->whole_rect;
1991 old_client_rect = data->client_rect;
1992 data->window_rect = *rectWindow;
1993 data->whole_rect = *visible_rect;
1994 data->client_rect = *rectClient;
1996 TRACE( "win %p window %s client %s style %08x flags %08x\n",
1997 hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style, swp_flags );
1999 if (!IsRectEmpty( &valid_rects[0] ))
2001 int x_offset = old_whole_rect.left - data->whole_rect.left;
2002 int y_offset = old_whole_rect.top - data->whole_rect.top;
2004 /* if all that happened is that the whole window moved, copy everything */
2005 if (!(swp_flags & SWP_FRAMECHANGED) &&
2006 old_whole_rect.right - data->whole_rect.right == x_offset &&
2007 old_whole_rect.bottom - data->whole_rect.bottom == y_offset &&
2008 old_client_rect.left - data->client_rect.left == x_offset &&
2009 old_client_rect.right - data->client_rect.right == x_offset &&
2010 old_client_rect.top - data->client_rect.top == y_offset &&
2011 old_client_rect.bottom - data->client_rect.bottom == y_offset &&
2012 !memcmp( &valid_rects[0], &data->client_rect, sizeof(RECT) ))
2014 /* if we have an X window the bits will be moved by the X server */
2015 if (!data->whole_window)
2016 move_window_bits( data, &old_whole_rect, &data->whole_rect, &old_client_rect );
2018 else
2019 move_window_bits( data, &valid_rects[1], &valid_rects[0], &old_client_rect );
2022 wine_tsx11_lock();
2023 XFlush( gdi_display ); /* make sure painting is done before we move the window */
2024 wine_tsx11_unlock();
2026 sync_client_position( display, data, swp_flags, &old_client_rect, &old_whole_rect );
2028 if (!data->whole_window) return;
2030 /* check if we are currently processing an event relevant to this window */
2031 event_type = 0;
2032 if (thread_data->current_event && thread_data->current_event->xany.window == data->whole_window)
2033 event_type = thread_data->current_event->type;
2035 if (event_type != ConfigureNotify && event_type != PropertyNotify)
2036 event_type = 0; /* ignore other events */
2038 if (data->mapped)
2040 if (((swp_flags & SWP_HIDEWINDOW) && !(new_style & WS_VISIBLE)) ||
2041 (!event_type && !is_window_rect_mapped( rectWindow )))
2042 unmap_window( display, data );
2045 /* don't change position if we are about to minimize or maximize a managed window */
2046 if (!event_type &&
2047 !(data->managed && (swp_flags & SWP_STATECHANGED) && (new_style & (WS_MINIMIZE|WS_MAXIMIZE))))
2048 sync_window_position( display, data, swp_flags, &old_client_rect, &old_whole_rect );
2050 if ((new_style & WS_VISIBLE) &&
2051 ((new_style & WS_MINIMIZE) || is_window_rect_mapped( rectWindow )))
2053 if (!data->mapped || (swp_flags & (SWP_FRAMECHANGED|SWP_STATECHANGED)))
2054 set_wm_hints( display, data );
2056 if (!data->mapped)
2058 map_window( display, data, new_style );
2060 else if ((swp_flags & SWP_STATECHANGED) && (!data->iconic != !(new_style & WS_MINIMIZE)))
2062 data->iconic = (new_style & WS_MINIMIZE) != 0;
2063 TRACE( "changing win %p iconic state to %u\n", data->hwnd, data->iconic );
2064 wine_tsx11_lock();
2065 if (data->iconic)
2066 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
2067 else if (is_window_rect_mapped( rectWindow ))
2068 XMapWindow( display, data->whole_window );
2069 wine_tsx11_unlock();
2070 update_net_wm_states( display, data );
2072 else if (!event_type)
2074 update_net_wm_states( display, data );
2078 wine_tsx11_lock();
2079 XFlush( display ); /* make sure changes are done before we start painting again */
2080 wine_tsx11_unlock();
2084 /***********************************************************************
2085 * ShowWindow (X11DRV.@)
2087 UINT X11DRV_ShowWindow( HWND hwnd, INT cmd, RECT *rect, UINT swp )
2089 int x, y;
2090 unsigned int width, height, border, depth;
2091 Window root, top;
2092 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
2093 struct x11drv_thread_data *thread_data = x11drv_thread_data();
2094 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2096 if (!data || !data->whole_window || !data->managed || !data->mapped || data->iconic) return swp;
2097 if (style & WS_MINIMIZE) return swp;
2099 /* only fetch the new rectangle if the ShowWindow was a result of a window manager event */
2101 if (!thread_data->current_event || thread_data->current_event->xany.window != data->whole_window)
2102 return swp;
2104 if (thread_data->current_event->type != ConfigureNotify &&
2105 thread_data->current_event->type != PropertyNotify)
2106 return swp;
2108 TRACE( "win %p/%lx cmd %d at %s flags %08x\n",
2109 hwnd, data->whole_window, cmd, wine_dbgstr_rect(rect), swp );
2111 wine_tsx11_lock();
2112 XGetGeometry( thread_data->display, data->whole_window,
2113 &root, &x, &y, &width, &height, &border, &depth );
2114 XTranslateCoordinates( thread_data->display, data->whole_window, root, 0, 0, &x, &y, &top );
2115 wine_tsx11_unlock();
2116 rect->left = x;
2117 rect->top = y;
2118 rect->right = x + width;
2119 rect->bottom = y + height;
2120 OffsetRect( rect, virtual_screen_rect.left, virtual_screen_rect.top );
2121 X11DRV_X_to_window_rect( data, rect );
2122 return swp & ~(SWP_NOMOVE | SWP_NOCLIENTMOVE | SWP_NOSIZE | SWP_NOCLIENTSIZE);
2126 /**********************************************************************
2127 * SetWindowIcon (X11DRV.@)
2129 * hIcon or hIconSm has changed (or is being initialised for the
2130 * first time). Complete the X11 driver-specific initialisation
2131 * and set the window hints.
2133 * This is not entirely correct, may need to create
2134 * an icon window and set the pixmap as a background
2136 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
2138 Display *display = thread_display();
2139 struct x11drv_win_data *data;
2141 if (type != ICON_BIG) return; /* nothing to do here */
2143 if (!(data = X11DRV_get_win_data( hwnd ))) return;
2144 if (!data->whole_window) return;
2145 if (!data->managed) return;
2147 if (data->wm_hints)
2149 set_icon_hints( display, data, icon );
2150 wine_tsx11_lock();
2151 XSetWMHints( display, data->whole_window, data->wm_hints );
2152 wine_tsx11_unlock();
2157 /***********************************************************************
2158 * SetWindowRgn (X11DRV.@)
2160 * Assign specified region to window (for non-rectangular windows)
2162 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
2164 struct x11drv_win_data *data;
2166 if ((data = X11DRV_get_win_data( hwnd )))
2168 sync_window_region( thread_display(), data, hrgn );
2170 else if (X11DRV_get_whole_window( hwnd ))
2172 SendMessageW( hwnd, WM_X11DRV_SET_WIN_REGION, 0, 0 );
2174 return TRUE;
2178 /**********************************************************************
2179 * X11DRV_WindowMessage (X11DRV.@)
2181 LRESULT X11DRV_WindowMessage( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
2183 struct x11drv_win_data *data;
2185 switch(msg)
2187 case WM_X11DRV_ACQUIRE_SELECTION:
2188 return X11DRV_AcquireClipboard( hwnd );
2189 case WM_X11DRV_DELETE_WINDOW:
2190 return SendMessageW( hwnd, WM_SYSCOMMAND, SC_CLOSE, 0 );
2191 case WM_X11DRV_SET_WIN_FORMAT:
2192 return set_win_format( hwnd, (XID)wp );
2193 case WM_X11DRV_SET_WIN_REGION:
2194 if ((data = X11DRV_get_win_data( hwnd ))) sync_window_region( thread_display(), data, (HRGN)1 );
2195 return 0;
2196 case WM_X11DRV_RESIZE_DESKTOP:
2197 X11DRV_resize_desktop( LOWORD(lp), HIWORD(lp) );
2198 return 0;
2199 default:
2200 FIXME( "got window msg %x hwnd %p wp %lx lp %lx\n", msg, hwnd, wp, lp );
2201 return 0;
2206 /***********************************************************************
2207 * is_netwm_supported
2209 static BOOL is_netwm_supported( Display *display, Atom atom )
2211 static Atom *net_supported;
2212 static int net_supported_count = -1;
2213 int i;
2215 wine_tsx11_lock();
2216 if (net_supported_count == -1)
2218 Atom type;
2219 int format;
2220 unsigned long count, remaining;
2222 if (!XGetWindowProperty( display, DefaultRootWindow(display), x11drv_atom(_NET_SUPPORTED), 0,
2223 ~0UL, False, XA_ATOM, &type, &format, &count,
2224 &remaining, (unsigned char **)&net_supported ))
2225 net_supported_count = get_property_size( format, count ) / sizeof(Atom);
2226 else
2227 net_supported_count = 0;
2229 wine_tsx11_unlock();
2231 for (i = 0; i < net_supported_count; i++)
2232 if (net_supported[i] == atom) return TRUE;
2233 return FALSE;
2237 /***********************************************************************
2238 * SysCommand (X11DRV.@)
2240 * Perform WM_SYSCOMMAND handling.
2242 LRESULT X11DRV_SysCommand( HWND hwnd, WPARAM wparam, LPARAM lparam )
2244 WPARAM hittest = wparam & 0x0f;
2245 DWORD dwPoint;
2246 int x, y, dir;
2247 XEvent xev;
2248 Display *display = thread_display();
2249 struct x11drv_win_data *data;
2251 if (!(data = X11DRV_get_win_data( hwnd ))) return -1;
2252 if (!data->whole_window || !data->managed || !data->mapped) return -1;
2254 switch (wparam & 0xfff0)
2256 case SC_MOVE:
2257 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
2258 else dir = _NET_WM_MOVERESIZE_MOVE;
2259 break;
2260 case SC_SIZE:
2261 /* windows without WS_THICKFRAME are not resizable through the window manager */
2262 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_THICKFRAME)) return -1;
2264 switch (hittest)
2266 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
2267 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
2268 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
2269 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
2270 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
2271 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
2272 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
2273 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
2274 default: dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD; break;
2276 break;
2278 case SC_KEYMENU:
2279 /* prevent a simple ALT press+release from activating the system menu,
2280 * as that can get confusing on managed windows */
2281 if ((WCHAR)lparam) return -1; /* got an explicit char */
2282 if (GetMenu( hwnd )) return -1; /* window has a real menu */
2283 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) return -1; /* no system menu */
2284 TRACE( "ignoring SC_KEYMENU wp %lx lp %lx\n", wparam, lparam );
2285 return 0;
2287 default:
2288 return -1;
2291 if (IsZoomed(hwnd)) return -1;
2293 if (!is_netwm_supported( display, x11drv_atom(_NET_WM_MOVERESIZE) ))
2295 TRACE( "_NET_WM_MOVERESIZE not supported\n" );
2296 return -1;
2299 dwPoint = GetMessagePos();
2300 x = (short)LOWORD(dwPoint);
2301 y = (short)HIWORD(dwPoint);
2303 TRACE("hwnd %p, x %d, y %d, dir %d\n", hwnd, x, y, dir);
2305 xev.xclient.type = ClientMessage;
2306 xev.xclient.window = X11DRV_get_whole_window(hwnd);
2307 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
2308 xev.xclient.serial = 0;
2309 xev.xclient.display = display;
2310 xev.xclient.send_event = True;
2311 xev.xclient.format = 32;
2312 xev.xclient.data.l[0] = x; /* x coord */
2313 xev.xclient.data.l[1] = y; /* y coord */
2314 xev.xclient.data.l[2] = dir; /* direction */
2315 xev.xclient.data.l[3] = 1; /* button */
2316 xev.xclient.data.l[4] = 0; /* unused */
2318 /* need to ungrab the pointer that may have been automatically grabbed
2319 * with a ButtonPress event */
2320 wine_tsx11_lock();
2321 XUngrabPointer( display, CurrentTime );
2322 XSendEvent(display, root_window, False, SubstructureNotifyMask, &xev);
2323 wine_tsx11_unlock();
2324 return 0;