push b0b97fcd59eff07f047585692fa36859b459324f
[wine/hacks.git] / dlls / winex11.drv / window.c
blobd3dc16b0e6ccd177947c8effe386ae0f48321d02
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 hrgn )
357 #ifdef HAVE_LIBXSHAPE
358 if (!data->whole_window) return;
359 data->shaped = FALSE;
361 if (!hrgn)
363 wine_tsx11_lock();
364 XShapeCombineMask( display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
365 wine_tsx11_unlock();
367 else
369 RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
370 if (pRegionData)
372 wine_tsx11_lock();
373 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
374 data->window_rect.left - data->whole_rect.left,
375 data->window_rect.top - data->whole_rect.top,
376 (XRectangle *)pRegionData->Buffer,
377 pRegionData->rdh.nCount, ShapeSet, YXBanded );
378 wine_tsx11_unlock();
379 HeapFree(GetProcessHeap(), 0, pRegionData);
380 data->shaped = TRUE;
383 #endif /* HAVE_LIBXSHAPE */
387 /***********************************************************************
388 * sync_window_text
390 static void sync_window_text( Display *display, Window win, const WCHAR *text )
392 UINT count;
393 char *buffer, *utf8_buffer;
394 XTextProperty prop;
396 /* allocate new buffer for window text */
397 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
398 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count ))) return;
399 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
401 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
402 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
404 HeapFree( GetProcessHeap(), 0, buffer );
405 return;
407 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
409 wine_tsx11_lock();
410 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
412 XSetWMName( display, win, &prop );
413 XSetWMIconName( display, win, &prop );
414 XFree( prop.value );
417 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
418 according to the standard
419 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
421 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
422 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
423 wine_tsx11_unlock();
425 HeapFree( GetProcessHeap(), 0, utf8_buffer );
426 HeapFree( GetProcessHeap(), 0, buffer );
430 /***********************************************************************
431 * X11DRV_set_win_format
433 BOOL X11DRV_set_win_format( HWND hwnd, XID fbconfig_id )
435 struct x11drv_win_data *data;
436 XVisualInfo *vis;
437 int w, h;
439 if (!(data = X11DRV_get_win_data(hwnd)) &&
440 !(data = X11DRV_create_win_data(hwnd))) return FALSE;
442 if (!(vis = visual_from_fbconfig_id(fbconfig_id))) return FALSE;
444 if (data->whole_window)
446 Display *display = thread_display();
447 Window client = data->client_window;
449 if (vis->visualid != data->visualid)
451 client = create_client_window( display, data, vis );
452 TRACE( "re-created client window %lx for %p fbconfig %lx\n", client, data->hwnd, fbconfig_id );
454 wine_tsx11_lock();
455 XFree(vis);
456 XFlush( display );
457 wine_tsx11_unlock();
458 if (client) goto done;
459 return FALSE;
462 w = data->client_rect.right - data->client_rect.left;
463 h = data->client_rect.bottom - data->client_rect.top;
465 if(w <= 0) w = 1;
466 if(h <= 0) h = 1;
468 #ifdef SONAME_LIBXCOMPOSITE
469 if(usexcomposite)
471 XSetWindowAttributes attrib;
472 static Window dummy_parent;
474 wine_tsx11_lock();
475 attrib.override_redirect = True;
476 if (!dummy_parent)
478 dummy_parent = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, screen_depth,
479 InputOutput, visual, CWOverrideRedirect, &attrib );
480 XMapWindow( gdi_display, dummy_parent );
482 data->colormap = XCreateColormap(gdi_display, dummy_parent, vis->visual,
483 (vis->class == PseudoColor ||
484 vis->class == GrayScale ||
485 vis->class == DirectColor) ?
486 AllocAll : AllocNone);
487 attrib.colormap = data->colormap;
488 XInstallColormap(gdi_display, attrib.colormap);
490 if(data->gl_drawable) XDestroyWindow(gdi_display, data->gl_drawable);
491 data->gl_drawable = XCreateWindow(gdi_display, dummy_parent, -w, 0, w, h, 0,
492 vis->depth, InputOutput, vis->visual,
493 CWColormap | CWOverrideRedirect,
494 &attrib);
495 if(data->gl_drawable)
497 pXCompositeRedirectWindow(gdi_display, data->gl_drawable,
498 CompositeRedirectManual);
499 XMapWindow(gdi_display, data->gl_drawable);
501 XFree(vis);
502 XFlush( gdi_display );
503 wine_tsx11_unlock();
505 else
506 #endif
508 WARN("XComposite is not available, using GLXPixmap hack\n");
510 wine_tsx11_lock();
512 if(data->pixmap) XFreePixmap(gdi_display, data->pixmap);
513 data->pixmap = XCreatePixmap(gdi_display, root_window, w, h, vis->depth);
514 if(!data->pixmap)
516 XFree(vis);
517 wine_tsx11_unlock();
518 return FALSE;
521 if(data->gl_drawable) destroy_glxpixmap(gdi_display, data->gl_drawable);
522 data->gl_drawable = create_glxpixmap(gdi_display, vis, data->pixmap);
523 if(!data->gl_drawable)
525 XFreePixmap(gdi_display, data->pixmap);
526 data->pixmap = 0;
528 XFree(vis);
529 XFlush( gdi_display );
530 wine_tsx11_unlock();
531 if (data->pixmap) SetPropA(hwnd, pixmap_prop, (HANDLE)data->pixmap);
534 if (!data->gl_drawable) return FALSE;
536 TRACE("Created GL drawable 0x%lx, using FBConfigID 0x%lx\n",
537 data->gl_drawable, fbconfig_id);
538 SetPropA(hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
540 done:
541 data->fbconfig_id = fbconfig_id;
542 SetPropA(hwnd, fbconfig_id_prop, (HANDLE)data->fbconfig_id);
543 /* force DCE invalidation */
544 SetWindowPos( hwnd, 0, 0, 0, 0, 0,
545 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE |
546 SWP_NOREDRAW | SWP_DEFERERASE | SWP_NOSENDCHANGING | SWP_STATECHANGED);
547 return TRUE;
550 /***********************************************************************
551 * sync_gl_drawable
553 static void sync_gl_drawable(struct x11drv_win_data *data)
555 int w = data->client_rect.right - data->client_rect.left;
556 int h = data->client_rect.bottom - data->client_rect.top;
557 XVisualInfo *vis;
558 Drawable glxp;
559 Pixmap pix;
561 if (w <= 0) w = 1;
562 if (h <= 0) h = 1;
564 TRACE("Resizing GL drawable 0x%lx to %dx%d\n", data->gl_drawable, w, h);
565 #ifdef SONAME_LIBXCOMPOSITE
566 if(usexcomposite)
568 wine_tsx11_lock();
569 XMoveResizeWindow(gdi_display, data->gl_drawable, -w, 0, w, h);
570 wine_tsx11_unlock();
571 return;
573 #endif
575 if (!(vis = visual_from_fbconfig_id(data->fbconfig_id))) return;
577 wine_tsx11_lock();
578 pix = XCreatePixmap(gdi_display, root_window, w, h, vis->depth);
579 if(!pix)
581 ERR("Failed to create pixmap for offscreen rendering\n");
582 XFree(vis);
583 wine_tsx11_unlock();
584 return;
587 glxp = create_glxpixmap(gdi_display, vis, pix);
588 if(!glxp)
590 ERR("Failed to create drawable for offscreen rendering\n");
591 XFreePixmap(gdi_display, pix);
592 XFree(vis);
593 wine_tsx11_unlock();
594 return;
597 XFree(vis);
599 mark_drawable_dirty(data->gl_drawable, glxp);
601 XFreePixmap(gdi_display, data->pixmap);
602 destroy_glxpixmap(gdi_display, data->gl_drawable);
603 TRACE( "Recreated GL drawable %lx to replace %lx\n", glxp, data->gl_drawable );
605 data->pixmap = pix;
606 data->gl_drawable = glxp;
608 XFlush( gdi_display );
609 wine_tsx11_unlock();
611 SetPropA(data->hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
612 SetPropA(data->hwnd, pixmap_prop, (HANDLE)data->pixmap);
616 /***********************************************************************
617 * get_window_changes
619 * fill the window changes structure
621 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
623 int mask = 0;
625 if (old->right - old->left != new->right - new->left )
627 if ((changes->width = new->right - new->left) <= 0) changes->width = 1;
628 mask |= CWWidth;
630 if (old->bottom - old->top != new->bottom - new->top)
632 if ((changes->height = new->bottom - new->top) <= 0) changes->height = 1;
633 mask |= CWHeight;
635 if (old->left != new->left)
637 changes->x = new->left;
638 mask |= CWX;
640 if (old->top != new->top)
642 changes->y = new->top;
643 mask |= CWY;
645 return mask;
649 /***********************************************************************
650 * create_icon_window
652 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
654 XSetWindowAttributes attr;
656 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
657 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
658 attr.bit_gravity = NorthWestGravity;
659 attr.backing_store = NotUseful/*WhenMapped*/;
660 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
662 wine_tsx11_lock();
663 data->icon_window = XCreateWindow( display, root_window, 0, 0,
664 GetSystemMetrics( SM_CXICON ),
665 GetSystemMetrics( SM_CYICON ),
666 0, screen_depth,
667 InputOutput, visual,
668 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
669 XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
670 XFlush( display ); /* make sure the window exists before we start painting to it */
671 wine_tsx11_unlock();
673 TRACE( "created %lx\n", data->icon_window );
674 SetPropA( data->hwnd, icon_window_prop, (HANDLE)data->icon_window );
675 return data->icon_window;
680 /***********************************************************************
681 * destroy_icon_window
683 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
685 if (!data->icon_window) return;
686 if (x11drv_thread_data()->cursor_window == data->icon_window)
687 x11drv_thread_data()->cursor_window = None;
688 wine_tsx11_lock();
689 XDeleteContext( display, data->icon_window, winContext );
690 XDestroyWindow( display, data->icon_window );
691 data->icon_window = 0;
692 wine_tsx11_unlock();
693 RemovePropA( data->hwnd, icon_window_prop );
697 /***********************************************************************
698 * set_icon_hints
700 * Set the icon wm hints
702 static void set_icon_hints( Display *display, struct x11drv_win_data *data, HICON hIcon )
704 XWMHints *hints = data->wm_hints;
706 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
707 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
708 data->hWMIconBitmap = 0;
709 data->hWMIconMask = 0;
711 if (!hIcon)
713 if (!data->icon_window) create_icon_window( display, data );
714 hints->icon_window = data->icon_window;
715 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
717 else
719 HBITMAP hbmOrig;
720 RECT rcMask;
721 BITMAP bmMask;
722 ICONINFO ii;
723 HDC hDC;
725 GetIconInfo(hIcon, &ii);
727 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
728 rcMask.top = 0;
729 rcMask.left = 0;
730 rcMask.right = bmMask.bmWidth;
731 rcMask.bottom = bmMask.bmHeight;
733 hDC = CreateCompatibleDC(0);
734 hbmOrig = SelectObject(hDC, ii.hbmMask);
735 InvertRect(hDC, &rcMask);
736 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
737 SelectObject(hDC, hbmOrig);
738 DeleteDC(hDC);
740 data->hWMIconBitmap = ii.hbmColor;
741 data->hWMIconMask = ii.hbmMask;
743 hints->icon_pixmap = X11DRV_get_pixmap(data->hWMIconBitmap);
744 hints->icon_mask = X11DRV_get_pixmap(data->hWMIconMask);
745 destroy_icon_window( display, data );
746 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
751 /***********************************************************************
752 * set_size_hints
754 * set the window size hints
756 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
758 XSizeHints* size_hints;
760 if (!(size_hints = XAllocSizeHints())) return;
762 size_hints->win_gravity = StaticGravity;
763 size_hints->flags |= PWinGravity;
765 /* don't update size hints if window is not in normal state */
766 if (!(style & (WS_MINIMIZE | WS_MAXIMIZE)))
768 if (data->hwnd != GetDesktopWindow()) /* don't force position of desktop */
770 size_hints->x = data->whole_rect.left;
771 size_hints->y = data->whole_rect.top;
772 size_hints->flags |= PPosition;
775 if (!is_window_resizable( data, style ))
777 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
778 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
779 size_hints->min_width = size_hints->max_width;
780 size_hints->min_height = size_hints->max_height;
781 size_hints->flags |= PMinSize | PMaxSize;
784 XSetWMNormalHints( display, data->whole_window, size_hints );
785 XFree( size_hints );
789 /***********************************************************************
790 * get_process_name
792 * get the name of the current process for setting class hints
794 static char *get_process_name(void)
796 static char *name;
798 if (!name)
800 WCHAR module[MAX_PATH];
801 DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
802 if (len && len < MAX_PATH)
804 char *ptr;
805 WCHAR *p, *appname = module;
807 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
808 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
809 len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
810 if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
812 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
813 name = ptr;
817 return name;
821 /***********************************************************************
822 * set_initial_wm_hints
824 * Set the window manager hints that don't change over the lifetime of a window.
826 static void set_initial_wm_hints( Display *display, struct x11drv_win_data *data )
828 long i;
829 Atom protocols[3];
830 Atom dndVersion = 4;
831 XClassHint *class_hints;
832 char *process_name = get_process_name();
834 wine_tsx11_lock();
836 /* wm protocols */
837 i = 0;
838 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
839 protocols[i++] = x11drv_atom(_NET_WM_PING);
840 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
841 XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
842 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
844 /* class hints */
845 if ((class_hints = XAllocClassHint()))
847 static char wine[] = "Wine";
849 class_hints->res_name = process_name;
850 class_hints->res_class = wine;
851 XSetClassHint( display, data->whole_window, class_hints );
852 XFree( class_hints );
855 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
856 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
857 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
858 i = getpid();
859 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
860 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
862 XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
863 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
865 data->wm_hints = XAllocWMHints();
866 wine_tsx11_unlock();
868 if (data->wm_hints)
870 HICON icon = (HICON)SendMessageW( data->hwnd, WM_GETICON, ICON_BIG, 0 );
871 if (!icon) icon = (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON );
872 data->wm_hints->flags = 0;
873 set_icon_hints( display, data, icon );
878 /***********************************************************************
879 * set_wm_hints
881 * Set the window manager hints for a newly-created window
883 static void set_wm_hints( Display *display, struct x11drv_win_data *data )
885 Window group_leader;
886 Atom window_type;
887 MwmHints mwm_hints;
888 DWORD style, ex_style;
889 HWND owner;
891 if (data->hwnd == GetDesktopWindow())
893 /* force some styles for the desktop to get the correct decorations */
894 style = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
895 ex_style = WS_EX_APPWINDOW;
896 owner = 0;
898 else
900 style = GetWindowLongW( data->hwnd, GWL_STYLE );
901 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
902 owner = get_window_owner( data->hwnd );
905 /* transient for hint */
906 if (owner)
908 Window owner_win = X11DRV_get_whole_window( owner );
909 wine_tsx11_lock();
910 XSetTransientForHint( display, data->whole_window, owner_win );
911 wine_tsx11_unlock();
912 group_leader = owner_win;
914 else group_leader = data->whole_window;
916 wine_tsx11_lock();
918 /* size hints */
919 set_size_hints( display, data, style );
921 /* set the WM_WINDOW_TYPE */
922 if (style & WS_THICKFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
923 else if (ex_style & WS_EX_APPWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
924 else if (style & WS_DLGFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
925 else if (ex_style & WS_EX_DLGMODALFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
926 else if ((style & WS_POPUP) && owner) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
927 #if 0 /* many window managers don't handle utility windows very well */
928 else if (ex_style & WS_EX_TOOLWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
929 #endif
930 else window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
932 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
933 XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
935 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
936 mwm_hints.decorations = get_mwm_decorations( data, style, ex_style );
937 mwm_hints.functions = MWM_FUNC_MOVE;
938 if (is_window_resizable( data, style )) mwm_hints.functions |= MWM_FUNC_RESIZE;
939 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
940 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
941 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
943 XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
944 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
945 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
947 /* wm hints */
948 if (data->wm_hints)
950 data->wm_hints->flags |= InputHint | StateHint | WindowGroupHint;
951 data->wm_hints->input = !(style & WS_DISABLED);
952 data->wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
953 data->wm_hints->window_group = group_leader;
954 XSetWMHints( display, data->whole_window, data->wm_hints );
957 wine_tsx11_unlock();
961 /***********************************************************************
962 * update_net_wm_states
964 void update_net_wm_states( Display *display, struct x11drv_win_data *data )
966 static const unsigned int state_atoms[NB_NET_WM_STATES] =
968 XATOM__NET_WM_STATE_FULLSCREEN,
969 XATOM__NET_WM_STATE_ABOVE,
970 XATOM__NET_WM_STATE_MAXIMIZED_VERT,
971 XATOM__NET_WM_STATE_SKIP_PAGER,
972 XATOM__NET_WM_STATE_SKIP_TASKBAR
975 DWORD i, style, ex_style, new_state = 0;
977 if (!data->managed) return;
978 if (data->whole_window == root_window) return;
980 style = GetWindowLongW( data->hwnd, GWL_STYLE );
981 if (data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
982 data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height)
984 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION)
985 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
986 else if (!(style & WS_MINIMIZE))
987 new_state |= (1 << NET_WM_STATE_FULLSCREEN);
989 else if (style & WS_MAXIMIZE)
990 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
992 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
993 if (ex_style & WS_EX_TOPMOST)
994 new_state |= (1 << NET_WM_STATE_ABOVE);
995 if (ex_style & WS_EX_TOOLWINDOW)
996 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR) | (1 << NET_WM_STATE_SKIP_PAGER);
997 if (!(ex_style & WS_EX_APPWINDOW) && get_window_owner( data->hwnd ))
998 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR);
1000 if (!data->mapped) /* set the _NET_WM_STATE atom directly */
1002 Atom atoms[NB_NET_WM_STATES+1];
1003 DWORD count;
1005 for (i = count = 0; i < NB_NET_WM_STATES; i++)
1007 if (!(new_state & (1 << i))) continue;
1008 TRACE( "setting wm state %u for unmapped window %p/%lx\n",
1009 i, data->hwnd, data->whole_window );
1010 atoms[count++] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1011 if (state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT)
1012 atoms[count++] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
1014 wine_tsx11_lock();
1015 XChangeProperty( display, data->whole_window, x11drv_atom(_NET_WM_STATE), XA_ATOM,
1016 32, PropModeReplace, (unsigned char *)atoms, count );
1017 wine_tsx11_unlock();
1019 else /* ask the window manager to do it for us */
1021 XEvent xev;
1023 xev.xclient.type = ClientMessage;
1024 xev.xclient.window = data->whole_window;
1025 xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
1026 xev.xclient.serial = 0;
1027 xev.xclient.display = display;
1028 xev.xclient.send_event = True;
1029 xev.xclient.format = 32;
1030 xev.xclient.data.l[3] = 1;
1032 for (i = 0; i < NB_NET_WM_STATES; i++)
1034 if (!((data->net_wm_state ^ new_state) & (1 << i))) continue; /* unchanged */
1036 TRACE( "setting wm state %u for window %p/%lx to %u prev %u\n",
1037 i, data->hwnd, data->whole_window,
1038 (new_state & (1 << i)) != 0, (data->net_wm_state & (1 << i)) != 0 );
1040 xev.xclient.data.l[0] = (new_state & (1 << i)) ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1041 xev.xclient.data.l[1] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1042 xev.xclient.data.l[2] = ((state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT) ?
1043 x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ) : 0);
1044 wine_tsx11_lock();
1045 XSendEvent( display, root_window, False,
1046 SubstructureRedirectMask | SubstructureNotifyMask, &xev );
1047 wine_tsx11_unlock();
1050 data->net_wm_state = new_state;
1054 /***********************************************************************
1055 * set_xembed_flags
1057 static void set_xembed_flags( Display *display, struct x11drv_win_data *data, unsigned long flags )
1059 unsigned long info[2];
1061 info[0] = 0; /* protocol version */
1062 info[1] = flags;
1063 wine_tsx11_lock();
1064 XChangeProperty( display, data->whole_window, x11drv_atom(_XEMBED_INFO),
1065 x11drv_atom(_XEMBED_INFO), 32, PropModeReplace, (unsigned char*)info, 2 );
1066 wine_tsx11_unlock();
1070 /***********************************************************************
1071 * map_window
1073 static void map_window( Display *display, struct x11drv_win_data *data, DWORD new_style )
1075 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1077 wait_for_withdrawn_state( display, data, TRUE );
1079 if (!data->embedded)
1081 update_net_wm_states( display, data );
1082 sync_window_style( display, data );
1083 wine_tsx11_lock();
1084 XMapWindow( display, data->whole_window );
1085 wine_tsx11_unlock();
1087 else set_xembed_flags( display, data, XEMBED_MAPPED );
1089 data->mapped = TRUE;
1090 data->iconic = (new_style & WS_MINIMIZE) != 0;
1094 /***********************************************************************
1095 * unmap_window
1097 static void unmap_window( Display *display, struct x11drv_win_data *data )
1099 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1101 if (!data->embedded)
1103 wait_for_withdrawn_state( display, data, FALSE );
1104 wine_tsx11_lock();
1105 if (data->managed) XWithdrawWindow( display, data->whole_window, DefaultScreen(display) );
1106 else XUnmapWindow( display, data->whole_window );
1107 wine_tsx11_unlock();
1109 else set_xembed_flags( display, data, 0 );
1111 data->mapped = FALSE;
1112 data->net_wm_state = 0;
1116 /***********************************************************************
1117 * make_window_embedded
1119 void make_window_embedded( Display *display, struct x11drv_win_data *data )
1121 if (data->mapped)
1123 /* the window cannot be mapped before being embedded */
1124 unmap_window( display, data );
1125 data->embedded = TRUE;
1126 map_window( display, data, 0 );
1128 else
1130 data->embedded = TRUE;
1131 set_xembed_flags( display, data, 0 );
1136 /***********************************************************************
1137 * X11DRV_window_to_X_rect
1139 * Convert a rect from client to X window coordinates
1141 void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
1143 RECT rc;
1145 if (!data->managed) return;
1146 if (IsRectEmpty( rect )) return;
1148 get_x11_rect_offset( data, &rc );
1150 rect->left -= rc.left;
1151 rect->right -= rc.right;
1152 rect->top -= rc.top;
1153 rect->bottom -= rc.bottom;
1154 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1155 if (rect->left >= rect->right) rect->right = rect->left + 1;
1159 /***********************************************************************
1160 * X11DRV_X_to_window_rect
1162 * Opposite of X11DRV_window_to_X_rect
1164 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
1166 RECT rc;
1168 if (!data->managed) return;
1169 if (IsRectEmpty( rect )) return;
1171 get_x11_rect_offset( data, &rc );
1173 rect->left += rc.left;
1174 rect->right += rc.right;
1175 rect->top += rc.top;
1176 rect->bottom += rc.bottom;
1177 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1178 if (rect->left >= rect->right) rect->right = rect->left + 1;
1182 /***********************************************************************
1183 * sync_window_position
1185 * Synchronize the X window position with the Windows one
1187 static void sync_window_position( Display *display, struct x11drv_win_data *data,
1188 UINT swp_flags, const RECT *old_client_rect,
1189 const RECT *old_whole_rect )
1191 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
1192 XWindowChanges changes;
1193 unsigned int mask = 0;
1195 if (data->managed && data->iconic) return;
1197 /* resizing a managed maximized window is not allowed */
1198 if (!(style & WS_MAXIMIZE) || !data->managed)
1200 if ((changes.width = data->whole_rect.right - data->whole_rect.left) <= 0) changes.width = 1;
1201 if ((changes.height = data->whole_rect.bottom - data->whole_rect.top) <= 0) changes.height = 1;
1202 mask |= CWWidth | CWHeight;
1205 /* only the size is allowed to change for the desktop window */
1206 if (data->whole_window != root_window)
1208 changes.x = data->whole_rect.left - virtual_screen_rect.left;
1209 changes.y = data->whole_rect.top - virtual_screen_rect.top;
1210 mask |= CWX | CWY;
1213 if (!(swp_flags & SWP_NOZORDER) || (swp_flags & SWP_SHOWWINDOW))
1215 /* find window that this one must be after */
1216 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
1217 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
1218 prev = GetWindow( prev, GW_HWNDPREV );
1219 if (!prev) /* top child */
1221 changes.stack_mode = Above;
1222 mask |= CWStackMode;
1224 /* should use stack_mode Below but most window managers don't get it right */
1225 /* and Above with a sibling doesn't work so well either, so we ignore it */
1228 TRACE( "setting win %p/%lx pos %d,%d,%dx%d after %lx changes=%x\n",
1229 data->hwnd, data->whole_window, data->whole_rect.left, data->whole_rect.top,
1230 data->whole_rect.right - data->whole_rect.left,
1231 data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
1233 wine_tsx11_lock();
1234 set_size_hints( display, data, style );
1235 XReconfigureWMWindow( display, data->whole_window,
1236 DefaultScreen(display), mask, &changes );
1237 wine_tsx11_unlock();
1241 /***********************************************************************
1242 * sync_client_position
1244 * Synchronize the X client window position with the Windows one
1246 static void sync_client_position( Display *display, struct x11drv_win_data *data,
1247 UINT swp_flags, const RECT *old_client_rect,
1248 const RECT *old_whole_rect )
1250 int mask;
1251 XWindowChanges changes;
1252 RECT old = *old_client_rect;
1253 RECT new = data->client_rect;
1255 OffsetRect( &old, -old_whole_rect->left, -old_whole_rect->top );
1256 OffsetRect( &new, -data->whole_rect.left, -data->whole_rect.top );
1257 if (!(mask = get_window_changes( &changes, &old, &new ))) return;
1259 if (data->client_window)
1261 TRACE( "setting client win %lx pos %d,%d,%dx%d changes=%x\n",
1262 data->client_window, new.left, new.top,
1263 new.right - new.left, new.bottom - new.top, mask );
1264 wine_tsx11_lock();
1265 XConfigureWindow( display, data->client_window, mask, &changes );
1266 wine_tsx11_unlock();
1269 if (data->gl_drawable && (mask & (CWWidth|CWHeight))) sync_gl_drawable( data );
1273 /***********************************************************************
1274 * move_window_bits
1276 * Move the window bits when a window is moved.
1278 static void move_window_bits( struct x11drv_win_data *data, const RECT *old_rect, const RECT *new_rect,
1279 const RECT *old_client_rect )
1281 RECT src_rect = *old_rect;
1282 RECT dst_rect = *new_rect;
1283 HDC hdc_src, hdc_dst;
1284 INT code;
1285 HRGN rgn = 0;
1286 HWND parent = 0;
1288 if (!data->whole_window)
1290 OffsetRect( &dst_rect, -data->window_rect.left, -data->window_rect.top );
1291 parent = GetAncestor( data->hwnd, GA_PARENT );
1292 hdc_src = GetDCEx( parent, 0, DCX_CACHE );
1293 hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE | DCX_WINDOW );
1295 else
1297 OffsetRect( &dst_rect, -data->client_rect.left, -data->client_rect.top );
1298 /* make src rect relative to the old position of the window */
1299 OffsetRect( &src_rect, -old_client_rect->left, -old_client_rect->top );
1300 if (dst_rect.left == src_rect.left && dst_rect.top == src_rect.top) return;
1301 hdc_src = hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE );
1304 code = X11DRV_START_EXPOSURES;
1305 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
1307 TRACE( "copying bits for win %p/%lx/%lx %s -> %s\n",
1308 data->hwnd, data->whole_window, data->client_window,
1309 wine_dbgstr_rect(&src_rect), wine_dbgstr_rect(&dst_rect) );
1310 BitBlt( hdc_dst, dst_rect.left, dst_rect.top,
1311 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top,
1312 hdc_src, src_rect.left, src_rect.top, SRCCOPY );
1314 code = X11DRV_END_EXPOSURES;
1315 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, sizeof(rgn), (LPSTR)&rgn );
1317 ReleaseDC( data->hwnd, hdc_dst );
1318 if (hdc_src != hdc_dst) ReleaseDC( parent, hdc_src );
1320 if (rgn)
1322 if (!data->whole_window)
1324 /* map region to client rect since we are using DCX_WINDOW */
1325 OffsetRgn( rgn, data->window_rect.left - data->client_rect.left,
1326 data->window_rect.top - data->client_rect.top );
1327 RedrawWindow( data->hwnd, NULL, rgn,
1328 RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ALLCHILDREN );
1330 else RedrawWindow( data->hwnd, NULL, rgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1331 DeleteObject( rgn );
1336 /**********************************************************************
1337 * create_whole_window
1339 * Create the whole X window for a given window
1341 static Window create_whole_window( Display *display, struct x11drv_win_data *data )
1343 int cx, cy, mask;
1344 XSetWindowAttributes attr;
1345 WCHAR text[1024];
1346 HRGN hrgn;
1348 if (!(cx = data->window_rect.right - data->window_rect.left)) cx = 1;
1349 if (!(cy = data->window_rect.bottom - data->window_rect.top)) cy = 1;
1351 if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1353 TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1354 data->managed = TRUE;
1355 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1358 mask = get_window_attributes( display, data, &attr );
1360 wine_tsx11_lock();
1362 data->whole_rect = data->window_rect;
1363 data->whole_window = XCreateWindow( display, root_window,
1364 data->window_rect.left - virtual_screen_rect.left,
1365 data->window_rect.top - virtual_screen_rect.top,
1366 cx, cy, 0, screen_depth, InputOutput,
1367 visual, mask, &attr );
1369 if (data->whole_window) XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
1370 wine_tsx11_unlock();
1372 if (!data->whole_window) return 0;
1374 if (!create_client_window( display, data, NULL ))
1376 wine_tsx11_lock();
1377 XDeleteContext( display, data->whole_window, winContext );
1378 XDestroyWindow( display, data->whole_window );
1379 data->whole_window = 0;
1380 wine_tsx11_unlock();
1381 return 0;
1384 set_initial_wm_hints( display, data );
1385 set_wm_hints( display, data );
1387 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1389 /* set the window text */
1390 if (!InternalGetWindowText( data->hwnd, text, sizeof(text)/sizeof(WCHAR) )) text[0] = 0;
1391 sync_window_text( display, data->whole_window, text );
1393 /* set the window region */
1394 if ((hrgn = CreateRectRgn( 0, 0, 0, 0 )))
1396 if (GetWindowRgn( data->hwnd, hrgn ) != ERROR) sync_window_region( display, data, hrgn );
1397 DeleteObject( hrgn );
1399 wine_tsx11_lock();
1400 XFlush( display ); /* make sure the window exists before we start painting to it */
1401 wine_tsx11_unlock();
1402 return data->whole_window;
1406 /**********************************************************************
1407 * destroy_whole_window
1409 * Destroy the whole X window for a given window.
1411 static void destroy_whole_window( Display *display, struct x11drv_win_data *data, BOOL already_destroyed )
1413 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1415 if (!data->whole_window) return;
1417 TRACE( "win %p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1418 if (thread_data->cursor_window == data->whole_window ||
1419 thread_data->cursor_window == data->client_window)
1420 thread_data->cursor_window = None;
1421 wine_tsx11_lock();
1422 XDeleteContext( display, data->whole_window, winContext );
1423 XDeleteContext( display, data->client_window, winContext );
1424 if (!already_destroyed) XDestroyWindow( display, data->whole_window );
1425 data->whole_window = data->client_window = 0;
1426 data->wm_state = WithdrawnState;
1427 data->net_wm_state = 0;
1428 data->mapped = FALSE;
1429 if (data->xic)
1431 XUnsetICFocus( data->xic );
1432 XDestroyIC( data->xic );
1433 data->xic = 0;
1435 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1436 XFlush( display );
1437 XFree( data->wm_hints );
1438 data->wm_hints = NULL;
1439 wine_tsx11_unlock();
1440 RemovePropA( data->hwnd, whole_window_prop );
1441 RemovePropA( data->hwnd, client_window_prop );
1445 /*****************************************************************
1446 * SetWindowText (X11DRV.@)
1448 void X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1450 Window win;
1452 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(gdi_display))
1454 Display *display = thread_init_display();
1455 sync_window_text( display, win, text );
1460 /***********************************************************************
1461 * SetWindowStyle (X11DRV.@)
1463 * Update the X state of a window to reflect a style change
1465 void X11DRV_SetWindowStyle( HWND hwnd, DWORD old_style )
1467 struct x11drv_win_data *data;
1468 DWORD new_style, changed;
1470 if (hwnd == GetDesktopWindow()) return;
1471 new_style = GetWindowLongW( hwnd, GWL_STYLE );
1472 changed = new_style ^ old_style;
1474 if ((changed & WS_VISIBLE) && (new_style & WS_VISIBLE))
1476 /* we don't unmap windows, that causes trouble with the window manager */
1477 if (!(data = X11DRV_get_win_data( hwnd )) &&
1478 !(data = X11DRV_create_win_data( hwnd ))) return;
1480 if (data->whole_window && is_window_rect_mapped( &data->window_rect ))
1482 Display *display = thread_display();
1483 set_wm_hints( display, data );
1484 if (!data->mapped) map_window( display, data, new_style );
1488 if (changed & WS_DISABLED)
1490 data = X11DRV_get_win_data( hwnd );
1491 if (data && data->wm_hints)
1493 wine_tsx11_lock();
1494 data->wm_hints->input = !(new_style & WS_DISABLED);
1495 XSetWMHints( thread_display(), data->whole_window, data->wm_hints );
1496 wine_tsx11_unlock();
1502 /***********************************************************************
1503 * DestroyWindow (X11DRV.@)
1505 void X11DRV_DestroyWindow( HWND hwnd )
1507 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1508 struct x11drv_win_data *data;
1510 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1512 if (data->pixmap)
1514 wine_tsx11_lock();
1515 destroy_glxpixmap(gdi_display, data->gl_drawable);
1516 XFreePixmap(gdi_display, data->pixmap);
1517 wine_tsx11_unlock();
1519 else if (data->gl_drawable)
1521 wine_tsx11_lock();
1522 XDestroyWindow(gdi_display, data->gl_drawable);
1523 wine_tsx11_unlock();
1526 destroy_whole_window( thread_data->display, data, FALSE );
1527 destroy_icon_window( thread_data->display, data );
1529 if (data->colormap)
1531 wine_tsx11_lock();
1532 XFreeColormap( thread_data->display, data->colormap );
1533 wine_tsx11_unlock();
1536 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1537 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
1538 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
1539 wine_tsx11_lock();
1540 XDeleteContext( thread_data->display, (XID)hwnd, win_data_context );
1541 wine_tsx11_unlock();
1542 HeapFree( GetProcessHeap(), 0, data );
1546 /***********************************************************************
1547 * X11DRV_DestroyNotify
1549 void X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1551 Display *display = event->xdestroywindow.display;
1552 struct x11drv_win_data *data;
1554 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1556 FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1557 destroy_whole_window( display, data, TRUE );
1561 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
1563 struct x11drv_win_data *data;
1565 if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
1567 data->hwnd = hwnd;
1568 wine_tsx11_lock();
1569 if (!winContext) winContext = XUniqueContext();
1570 if (!win_data_context) win_data_context = XUniqueContext();
1571 XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
1572 wine_tsx11_unlock();
1574 return data;
1578 /* initialize the desktop window id in the desktop manager process */
1579 static struct x11drv_win_data *create_desktop_win_data( Display *display, HWND hwnd )
1581 struct x11drv_win_data *data;
1583 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1584 data->whole_window = data->client_window = root_window;
1585 data->managed = TRUE;
1586 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1587 SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
1588 SetPropA( data->hwnd, client_window_prop, (HANDLE)root_window );
1589 set_initial_wm_hints( display, data );
1590 return data;
1593 /**********************************************************************
1594 * CreateDesktopWindow (X11DRV.@)
1596 BOOL X11DRV_CreateDesktopWindow( HWND hwnd )
1598 unsigned int width, height;
1600 /* retrieve the real size of the desktop */
1601 SERVER_START_REQ( get_window_rectangles )
1603 req->handle = hwnd;
1604 wine_server_call( req );
1605 width = reply->window.right - reply->window.left;
1606 height = reply->window.bottom - reply->window.top;
1608 SERVER_END_REQ;
1610 if (!width && !height) /* not initialized yet */
1612 SERVER_START_REQ( set_window_pos )
1614 req->handle = hwnd;
1615 req->previous = 0;
1616 req->flags = SWP_NOZORDER;
1617 req->window.left = virtual_screen_rect.left;
1618 req->window.top = virtual_screen_rect.top;
1619 req->window.right = virtual_screen_rect.right;
1620 req->window.bottom = virtual_screen_rect.bottom;
1621 req->client = req->window;
1622 wine_server_call( req );
1624 SERVER_END_REQ;
1626 else
1628 Window win = (Window)GetPropA( hwnd, whole_window_prop );
1629 if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1631 return TRUE;
1635 /**********************************************************************
1636 * CreateWindow (X11DRV.@)
1638 BOOL X11DRV_CreateWindow( HWND hwnd )
1640 if (hwnd == GetDesktopWindow() && root_window != DefaultRootWindow( gdi_display ))
1642 Display *display = thread_init_display();
1644 /* the desktop win data can't be created lazily */
1645 if (!create_desktop_win_data( display, hwnd )) return FALSE;
1647 return TRUE;
1651 /***********************************************************************
1652 * X11DRV_get_win_data
1654 * Return the X11 data structure associated with a window.
1656 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1658 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1659 char *data;
1661 if (!thread_data) return NULL;
1662 if (!hwnd) return NULL;
1663 if (XFindContext( thread_data->display, (XID)hwnd, win_data_context, &data )) data = NULL;
1664 return (struct x11drv_win_data *)data;
1668 /***********************************************************************
1669 * X11DRV_create_win_data
1671 * Create an X11 data window structure for an existing window.
1673 struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd )
1675 Display *display;
1676 struct x11drv_win_data *data;
1677 HWND parent;
1679 if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL; /* desktop */
1681 /* don't create win data for HWND_MESSAGE windows */
1682 if (parent != GetDesktopWindow() && !GetAncestor( parent, GA_PARENT )) return NULL;
1684 display = thread_init_display();
1685 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1687 GetWindowRect( hwnd, &data->window_rect );
1688 MapWindowPoints( 0, parent, (POINT *)&data->window_rect, 2 );
1689 data->whole_rect = data->window_rect;
1690 GetClientRect( hwnd, &data->client_rect );
1691 MapWindowPoints( hwnd, parent, (POINT *)&data->client_rect, 2 );
1693 if (parent == GetDesktopWindow())
1695 if (!create_whole_window( display, data ))
1697 HeapFree( GetProcessHeap(), 0, data );
1698 return NULL;
1700 TRACE( "win %p/%lx/%lx window %s whole %s client %s\n",
1701 hwnd, data->whole_window, data->client_window, wine_dbgstr_rect( &data->window_rect ),
1702 wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
1704 return data;
1708 /***********************************************************************
1709 * X11DRV_get_whole_window
1711 * Return the X window associated with the full area of a window
1713 Window X11DRV_get_whole_window( HWND hwnd )
1715 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1717 if (!data)
1719 if (hwnd == GetDesktopWindow()) return root_window;
1720 return (Window)GetPropA( hwnd, whole_window_prop );
1722 return data->whole_window;
1726 /***********************************************************************
1727 * X11DRV_get_client_window
1729 * Return the X window associated with the client area of a window
1731 Window X11DRV_get_client_window( HWND hwnd )
1733 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1735 if (!data)
1737 if (hwnd == GetDesktopWindow()) return root_window;
1738 return (Window)GetPropA( hwnd, client_window_prop );
1740 return data->client_window;
1744 /***********************************************************************
1745 * X11DRV_get_ic
1747 * Return the X input context associated with a window
1749 XIC X11DRV_get_ic( HWND hwnd )
1751 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1752 XIM xim;
1754 if (!data) return 0;
1755 if (data->xic) return data->xic;
1756 if (!(xim = x11drv_thread_data()->xim)) return 0;
1757 return X11DRV_CreateIC( xim, data );
1761 /***********************************************************************
1762 * X11DRV_GetDC (X11DRV.@)
1764 void X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
1765 const RECT *top_rect, DWORD flags )
1767 struct x11drv_escape_set_drawable escape;
1768 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1770 escape.code = X11DRV_SET_DRAWABLE;
1771 escape.mode = IncludeInferiors;
1772 escape.fbconfig_id = 0;
1773 escape.gl_drawable = 0;
1774 escape.pixmap = 0;
1775 escape.gl_copy = FALSE;
1777 if (top == hwnd && data && IsIconic( hwnd ) && data->icon_window)
1779 escape.drawable = data->icon_window;
1781 else if (top == hwnd)
1783 escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
1784 /* GL draws to the client area even for window DCs */
1785 escape.gl_drawable = data ? data->client_window : X11DRV_get_client_window( hwnd );
1786 if (flags & DCX_WINDOW)
1787 escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
1788 else
1789 escape.drawable = escape.gl_drawable;
1791 else
1793 escape.drawable = X11DRV_get_client_window( top );
1794 escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
1795 escape.gl_drawable = data ? data->gl_drawable : (Drawable)GetPropA( hwnd, gl_drawable_prop );
1796 escape.pixmap = data ? data->pixmap : (Pixmap)GetPropA( hwnd, pixmap_prop );
1797 escape.gl_copy = (escape.gl_drawable != 0);
1798 if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
1801 escape.dc_rect.left = win_rect->left - top_rect->left;
1802 escape.dc_rect.top = win_rect->top - top_rect->top;
1803 escape.dc_rect.right = win_rect->right - top_rect->left;
1804 escape.dc_rect.bottom = win_rect->bottom - top_rect->top;
1805 escape.drawable_rect.left = top_rect->left;
1806 escape.drawable_rect.top = top_rect->top;
1807 escape.drawable_rect.right = top_rect->right;
1808 escape.drawable_rect.bottom = top_rect->bottom;
1810 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1814 /***********************************************************************
1815 * X11DRV_ReleaseDC (X11DRV.@)
1817 void X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
1819 struct x11drv_escape_set_drawable escape;
1821 escape.code = X11DRV_SET_DRAWABLE;
1822 escape.drawable = root_window;
1823 escape.mode = IncludeInferiors;
1824 escape.drawable_rect = virtual_screen_rect;
1825 SetRect( &escape.dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
1826 virtual_screen_rect.bottom - virtual_screen_rect.top );
1827 escape.fbconfig_id = 0;
1828 escape.gl_drawable = 0;
1829 escape.pixmap = 0;
1830 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1834 /***********************************************************************
1835 * SetCapture (X11DRV.@)
1837 void X11DRV_SetCapture( HWND hwnd, UINT flags )
1839 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1841 if (!thread_data) return;
1842 if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
1844 if (hwnd)
1846 Window grab_win = X11DRV_get_client_window( GetAncestor( hwnd, GA_ROOT ) );
1848 if (!grab_win) return;
1849 wine_tsx11_lock();
1850 XFlush( gdi_display );
1851 XGrabPointer( thread_data->display, grab_win, False,
1852 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1853 GrabModeAsync, GrabModeAsync, None, None, CurrentTime );
1854 wine_tsx11_unlock();
1855 thread_data->grab_window = grab_win;
1857 else /* release capture */
1859 wine_tsx11_lock();
1860 XFlush( gdi_display );
1861 XUngrabPointer( thread_data->display, CurrentTime );
1862 wine_tsx11_unlock();
1863 thread_data->grab_window = None;
1868 /*****************************************************************
1869 * SetParent (X11DRV.@)
1871 void X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
1873 Display *display = thread_display();
1874 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1876 if (!data) return;
1877 if (parent == old_parent) return;
1879 if (parent != GetDesktopWindow()) /* a child window */
1881 if (old_parent == GetDesktopWindow())
1883 /* destroy the old X windows */
1884 destroy_whole_window( display, data, FALSE );
1885 destroy_icon_window( display, data );
1886 if (data->managed)
1888 data->managed = FALSE;
1889 RemovePropA( data->hwnd, managed_prop );
1893 else /* new top level window */
1895 /* FIXME: we ignore errors since we can't really recover anyway */
1896 create_whole_window( display, data );
1901 /*****************************************************************
1902 * SetFocus (X11DRV.@)
1904 * Set the X focus.
1906 void X11DRV_SetFocus( HWND hwnd )
1908 Display *display = thread_display();
1909 struct x11drv_win_data *data;
1910 XWindowChanges changes;
1912 if (!(hwnd = GetAncestor( hwnd, GA_ROOT ))) return;
1913 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1914 if (data->managed || !data->whole_window) return;
1916 /* Set X focus and install colormap */
1917 wine_tsx11_lock();
1918 changes.stack_mode = Above;
1919 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
1920 if (root_window == DefaultRootWindow(display))
1922 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1923 /* FIXME: this is not entirely correct */
1924 XSetInputFocus( display, data->whole_window, RevertToParent,
1925 /* CurrentTime */
1926 GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1928 wine_tsx11_unlock();
1932 /***********************************************************************
1933 * WindowPosChanging (X11DRV.@)
1935 void X11DRV_WindowPosChanging( HWND hwnd, HWND insert_after, UINT swp_flags,
1936 const RECT *window_rect, const RECT *client_rect, RECT *visible_rect )
1938 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1939 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
1941 if (!data)
1943 /* create the win data if the window is being made visible */
1944 if (!(style & WS_VISIBLE) && !(swp_flags & SWP_SHOWWINDOW)) return;
1945 if (!(data = X11DRV_create_win_data( hwnd ))) return;
1948 /* check if we need to switch the window to managed */
1949 if (!data->managed && data->whole_window && is_window_managed( hwnd, swp_flags, window_rect ))
1951 TRACE( "making win %p/%lx managed\n", hwnd, data->whole_window );
1952 if (data->mapped) unmap_window( thread_display(), data );
1953 data->managed = TRUE;
1954 SetPropA( hwnd, managed_prop, (HANDLE)1 );
1957 *visible_rect = *window_rect;
1958 X11DRV_window_to_X_rect( data, visible_rect );
1962 /***********************************************************************
1963 * WindowPosChanged (X11DRV.@)
1965 void X11DRV_WindowPosChanged( HWND hwnd, HWND insert_after, UINT swp_flags,
1966 const RECT *rectWindow, const RECT *rectClient,
1967 const RECT *visible_rect, const RECT *valid_rects )
1969 struct x11drv_thread_data *thread_data;
1970 Display *display;
1971 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1972 DWORD new_style = GetWindowLongW( hwnd, GWL_STYLE );
1973 RECT old_whole_rect, old_client_rect;
1974 int event_type;
1976 if (!data) return;
1978 thread_data = x11drv_thread_data();
1979 display = thread_data->display;
1981 old_whole_rect = data->whole_rect;
1982 old_client_rect = data->client_rect;
1983 data->window_rect = *rectWindow;
1984 data->whole_rect = *visible_rect;
1985 data->client_rect = *rectClient;
1987 TRACE( "win %p window %s client %s style %08x flags %08x\n",
1988 hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style, swp_flags );
1990 if (!IsRectEmpty( &valid_rects[0] ))
1992 int x_offset = old_whole_rect.left - data->whole_rect.left;
1993 int y_offset = old_whole_rect.top - data->whole_rect.top;
1995 /* if all that happened is that the whole window moved, copy everything */
1996 if (!(swp_flags & SWP_FRAMECHANGED) &&
1997 old_whole_rect.right - data->whole_rect.right == x_offset &&
1998 old_whole_rect.bottom - data->whole_rect.bottom == y_offset &&
1999 old_client_rect.left - data->client_rect.left == x_offset &&
2000 old_client_rect.right - data->client_rect.right == x_offset &&
2001 old_client_rect.top - data->client_rect.top == y_offset &&
2002 old_client_rect.bottom - data->client_rect.bottom == y_offset &&
2003 !memcmp( &valid_rects[0], &data->client_rect, sizeof(RECT) ))
2005 /* if we have an X window the bits will be moved by the X server */
2006 if (!data->whole_window)
2007 move_window_bits( data, &old_whole_rect, &data->whole_rect, &old_client_rect );
2009 else
2010 move_window_bits( data, &valid_rects[1], &valid_rects[0], &old_client_rect );
2013 wine_tsx11_lock();
2014 XFlush( gdi_display ); /* make sure painting is done before we move the window */
2015 wine_tsx11_unlock();
2017 sync_client_position( display, data, swp_flags, &old_client_rect, &old_whole_rect );
2019 if (!data->whole_window) return;
2021 /* check if we are currently processing an event relevant to this window */
2022 event_type = 0;
2023 if (thread_data->current_event && thread_data->current_event->xany.window == data->whole_window)
2024 event_type = thread_data->current_event->type;
2026 if (event_type != ConfigureNotify && event_type != PropertyNotify)
2027 event_type = 0; /* ignore other events */
2029 if (data->mapped)
2031 if (((swp_flags & SWP_HIDEWINDOW) && !(new_style & WS_VISIBLE)) ||
2032 (!event_type && !is_window_rect_mapped( rectWindow )))
2033 unmap_window( display, data );
2036 /* don't change position if we are about to minimize or maximize a managed window */
2037 if (!event_type &&
2038 !(data->managed && (swp_flags & SWP_STATECHANGED) && (new_style & (WS_MINIMIZE|WS_MAXIMIZE))))
2039 sync_window_position( display, data, swp_flags, &old_client_rect, &old_whole_rect );
2041 if ((new_style & WS_VISIBLE) &&
2042 ((new_style & WS_MINIMIZE) || is_window_rect_mapped( rectWindow )))
2044 if (!data->mapped || (swp_flags & (SWP_FRAMECHANGED|SWP_STATECHANGED)))
2045 set_wm_hints( display, data );
2047 if (!data->mapped)
2049 map_window( display, data, new_style );
2051 else if ((swp_flags & SWP_STATECHANGED) && (!data->iconic != !(new_style & WS_MINIMIZE)))
2053 data->iconic = (new_style & WS_MINIMIZE) != 0;
2054 TRACE( "changing win %p iconic state to %u\n", data->hwnd, data->iconic );
2055 wine_tsx11_lock();
2056 if (data->iconic)
2057 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
2058 else if (is_window_rect_mapped( rectWindow ))
2059 XMapWindow( display, data->whole_window );
2060 wine_tsx11_unlock();
2061 update_net_wm_states( display, data );
2063 else if (!event_type)
2065 update_net_wm_states( display, data );
2069 wine_tsx11_lock();
2070 XFlush( display ); /* make sure changes are done before we start painting again */
2071 wine_tsx11_unlock();
2075 /***********************************************************************
2076 * ShowWindow (X11DRV.@)
2078 UINT X11DRV_ShowWindow( HWND hwnd, INT cmd, RECT *rect, UINT swp )
2080 int x, y;
2081 unsigned int width, height, border, depth;
2082 Window root, top;
2083 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
2084 struct x11drv_thread_data *thread_data = x11drv_thread_data();
2085 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2087 if (!data || !data->whole_window || !data->managed || !data->mapped || data->iconic) return swp;
2088 if (style & WS_MINIMIZE) return swp;
2090 /* only fetch the new rectangle if the ShowWindow was a result of a window manager event */
2092 if (!thread_data->current_event || thread_data->current_event->xany.window != data->whole_window)
2093 return swp;
2095 if (thread_data->current_event->type != ConfigureNotify &&
2096 thread_data->current_event->type != PropertyNotify)
2097 return swp;
2099 TRACE( "win %p/%lx cmd %d at %s flags %08x\n",
2100 hwnd, data->whole_window, cmd, wine_dbgstr_rect(rect), swp );
2102 wine_tsx11_lock();
2103 XGetGeometry( thread_data->display, data->whole_window,
2104 &root, &x, &y, &width, &height, &border, &depth );
2105 XTranslateCoordinates( thread_data->display, data->whole_window, root, 0, 0, &x, &y, &top );
2106 wine_tsx11_unlock();
2107 rect->left = x;
2108 rect->top = y;
2109 rect->right = x + width;
2110 rect->bottom = y + height;
2111 OffsetRect( rect, virtual_screen_rect.left, virtual_screen_rect.top );
2112 X11DRV_X_to_window_rect( data, rect );
2113 return swp & ~(SWP_NOMOVE | SWP_NOCLIENTMOVE | SWP_NOSIZE | SWP_NOCLIENTSIZE);
2117 /**********************************************************************
2118 * SetWindowIcon (X11DRV.@)
2120 * hIcon or hIconSm has changed (or is being initialised for the
2121 * first time). Complete the X11 driver-specific initialisation
2122 * and set the window hints.
2124 * This is not entirely correct, may need to create
2125 * an icon window and set the pixmap as a background
2127 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
2129 Display *display = thread_display();
2130 struct x11drv_win_data *data;
2132 if (type != ICON_BIG) return; /* nothing to do here */
2134 if (!(data = X11DRV_get_win_data( hwnd ))) return;
2135 if (!data->whole_window) return;
2136 if (!data->managed) return;
2138 if (data->wm_hints)
2140 set_icon_hints( display, data, icon );
2141 wine_tsx11_lock();
2142 XSetWMHints( display, data->whole_window, data->wm_hints );
2143 wine_tsx11_unlock();
2148 /***********************************************************************
2149 * SetWindowRgn (X11DRV.@)
2151 * Assign specified region to window (for non-rectangular windows)
2153 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
2155 struct x11drv_win_data *data;
2157 if ((data = X11DRV_get_win_data( hwnd )))
2159 sync_window_region( thread_display(), data, hrgn );
2161 else if (GetWindowThreadProcessId( hwnd, NULL ) != GetCurrentThreadId())
2163 FIXME( "not supported on other thread window %p\n", hwnd );
2164 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2165 return FALSE;
2168 return TRUE;
2172 /***********************************************************************
2173 * is_netwm_supported
2175 static BOOL is_netwm_supported( Display *display, Atom atom )
2177 static Atom *net_supported;
2178 static int net_supported_count = -1;
2179 int i;
2181 wine_tsx11_lock();
2182 if (net_supported_count == -1)
2184 Atom type;
2185 int format;
2186 unsigned long count, remaining;
2188 if (!XGetWindowProperty( display, DefaultRootWindow(display), x11drv_atom(_NET_SUPPORTED), 0,
2189 ~0UL, False, XA_ATOM, &type, &format, &count,
2190 &remaining, (unsigned char **)&net_supported ))
2191 net_supported_count = get_property_size( format, count ) / sizeof(Atom);
2192 else
2193 net_supported_count = 0;
2195 wine_tsx11_unlock();
2197 for (i = 0; i < net_supported_count; i++)
2198 if (net_supported[i] == atom) return TRUE;
2199 return FALSE;
2203 /***********************************************************************
2204 * SysCommand (X11DRV.@)
2206 * Perform WM_SYSCOMMAND handling.
2208 LRESULT X11DRV_SysCommand( HWND hwnd, WPARAM wparam, LPARAM lparam )
2210 WPARAM hittest = wparam & 0x0f;
2211 DWORD dwPoint;
2212 int x, y, dir;
2213 XEvent xev;
2214 Display *display = thread_display();
2215 struct x11drv_win_data *data;
2217 if (!(data = X11DRV_get_win_data( hwnd ))) return -1;
2218 if (!data->whole_window || !data->managed || !data->mapped) return -1;
2220 switch (wparam & 0xfff0)
2222 case SC_MOVE:
2223 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
2224 else dir = _NET_WM_MOVERESIZE_MOVE;
2225 break;
2226 case SC_SIZE:
2227 /* windows without WS_THICKFRAME are not resizable through the window manager */
2228 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_THICKFRAME)) return -1;
2230 switch (hittest)
2232 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
2233 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
2234 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
2235 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
2236 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
2237 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
2238 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
2239 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
2240 default: dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD; break;
2242 break;
2244 case SC_KEYMENU:
2245 /* prevent a simple ALT press+release from activating the system menu,
2246 * as that can get confusing on managed windows */
2247 if ((WCHAR)lparam) return -1; /* got an explicit char */
2248 if (GetMenu( hwnd )) return -1; /* window has a real menu */
2249 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) return -1; /* no system menu */
2250 TRACE( "ignoring SC_KEYMENU wp %lx lp %lx\n", wparam, lparam );
2251 return 0;
2253 default:
2254 return -1;
2257 if (IsZoomed(hwnd)) return -1;
2259 if (!is_netwm_supported( display, x11drv_atom(_NET_WM_MOVERESIZE) ))
2261 TRACE( "_NET_WM_MOVERESIZE not supported\n" );
2262 return -1;
2265 dwPoint = GetMessagePos();
2266 x = (short)LOWORD(dwPoint);
2267 y = (short)HIWORD(dwPoint);
2269 TRACE("hwnd %p, x %d, y %d, dir %d\n", hwnd, x, y, dir);
2271 xev.xclient.type = ClientMessage;
2272 xev.xclient.window = X11DRV_get_whole_window(hwnd);
2273 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
2274 xev.xclient.serial = 0;
2275 xev.xclient.display = display;
2276 xev.xclient.send_event = True;
2277 xev.xclient.format = 32;
2278 xev.xclient.data.l[0] = x; /* x coord */
2279 xev.xclient.data.l[1] = y; /* y coord */
2280 xev.xclient.data.l[2] = dir; /* direction */
2281 xev.xclient.data.l[3] = 1; /* button */
2282 xev.xclient.data.l[4] = 0; /* unused */
2284 /* need to ungrab the pointer that may have been automatically grabbed
2285 * with a ButtonPress event */
2286 wine_tsx11_lock();
2287 XUngrabPointer( display, CurrentTime );
2288 XSendEvent(display, root_window, False, SubstructureNotifyMask, &xev);
2289 wine_tsx11_unlock();
2290 return 0;