windowscodecs: Implement TiffDecoder_GetResolution.
[wine/multimedia.git] / dlls / winex11.drv / window.c
blobe35547a260825d537296ca67a5d9a2387894d3f6
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 /* cursor clipping window */
72 Window clip_window = 0;
73 int clipping_cursor = 0;
75 /* X context to associate a hwnd to an X window */
76 XContext winContext = 0;
78 /* X context to associate a struct x11drv_win_data to an hwnd */
79 static XContext win_data_context;
81 /* time of last user event and window where it's stored */
82 static Time last_user_time;
83 static Window user_time_window;
85 static const char foreign_window_prop[] = "__wine_x11_foreign_window";
86 static const char whole_window_prop[] = "__wine_x11_whole_window";
87 static const char client_window_prop[]= "__wine_x11_client_window";
88 static const char icon_window_prop[] = "__wine_x11_icon_window";
89 static const char clip_window_prop[] = "__wine_x11_clip_window";
90 static const char fbconfig_id_prop[] = "__wine_x11_fbconfig_id";
91 static const char gl_drawable_prop[] = "__wine_x11_gl_drawable";
92 static const char pixmap_prop[] = "__wine_x11_pixmap";
93 static const char managed_prop[] = "__wine_x11_managed";
96 /***********************************************************************
97 * http://standards.freedesktop.org/startup-notification-spec
99 static void remove_startup_notification(Display *display, Window window)
101 static LONG startup_notification_removed = 0;
102 char id[1024];
103 char message[1024];
104 int i;
105 int pos;
106 XEvent xevent;
107 const char *src;
108 int srclen;
110 if (InterlockedCompareExchange(&startup_notification_removed, 1, 0) != 0)
111 return;
113 if (GetEnvironmentVariableA("DESKTOP_STARTUP_ID", id, sizeof(id)) == 0)
114 return;
115 SetEnvironmentVariableA("DESKTOP_STARTUP_ID", NULL);
117 if ((src = strstr( id, "_TIME" ))) update_user_time( atol( src + 5 ));
119 pos = snprintf(message, sizeof(message), "remove: ID=");
120 message[pos++] = '"';
121 for (i = 0; id[i] && pos < sizeof(message) - 2; i++)
123 if (id[i] == '"' || id[i] == '\\')
124 message[pos++] = '\\';
125 message[pos++] = id[i];
127 message[pos++] = '"';
128 message[pos++] = '\0';
130 xevent.xclient.type = ClientMessage;
131 xevent.xclient.message_type = x11drv_atom(_NET_STARTUP_INFO_BEGIN);
132 xevent.xclient.display = display;
133 xevent.xclient.window = window;
134 xevent.xclient.format = 8;
136 src = message;
137 srclen = strlen(src) + 1;
139 wine_tsx11_lock();
140 while (srclen > 0)
142 int msglen = srclen;
143 if (msglen > 20)
144 msglen = 20;
145 memset(&xevent.xclient.data.b[0], 0, 20);
146 memcpy(&xevent.xclient.data.b[0], src, msglen);
147 src += msglen;
148 srclen -= msglen;
150 XSendEvent( display, DefaultRootWindow( display ), False, PropertyChangeMask, &xevent );
151 xevent.xclient.message_type = x11drv_atom(_NET_STARTUP_INFO);
153 wine_tsx11_unlock();
157 struct has_popup_result
159 HWND hwnd;
160 BOOL found;
163 static BOOL CALLBACK has_popup( HWND hwnd, LPARAM lparam )
165 struct has_popup_result *result = (struct has_popup_result *)lparam;
167 if (hwnd == result->hwnd) return FALSE; /* popups are always above owner */
168 result->found = (GetWindow( hwnd, GW_OWNER ) == result->hwnd);
169 return !result->found;
172 static BOOL has_owned_popups( HWND hwnd )
174 struct has_popup_result result;
176 result.hwnd = hwnd;
177 result.found = FALSE;
178 EnumWindows( has_popup, (LPARAM)&result );
179 return result.found;
182 /***********************************************************************
183 * is_window_managed
185 * Check if a given window should be managed
187 static BOOL is_window_managed( HWND hwnd, UINT swp_flags, const RECT *window_rect )
189 DWORD style, ex_style;
191 if (!managed_mode) return FALSE;
193 /* child windows are not managed */
194 style = GetWindowLongW( hwnd, GWL_STYLE );
195 if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD) return FALSE;
196 /* activated windows are managed */
197 if (!(swp_flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) return TRUE;
198 if (hwnd == GetActiveWindow()) return TRUE;
199 /* windows with caption are managed */
200 if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
201 /* windows with thick frame are managed */
202 if (style & WS_THICKFRAME) return TRUE;
203 if (style & WS_POPUP)
205 HMONITOR hmon;
206 MONITORINFO mi;
208 /* popup with sysmenu == caption are managed */
209 if (style & WS_SYSMENU) return TRUE;
210 /* full-screen popup windows are managed */
211 hmon = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY );
212 mi.cbSize = sizeof( mi );
213 GetMonitorInfoW( hmon, &mi );
214 if (window_rect->left <= mi.rcWork.left && window_rect->right >= mi.rcWork.right &&
215 window_rect->top <= mi.rcWork.top && window_rect->bottom >= mi.rcWork.bottom)
216 return TRUE;
218 /* application windows are managed */
219 ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
220 if (ex_style & WS_EX_APPWINDOW) return TRUE;
221 /* windows that own popups are managed */
222 if (has_owned_popups( hwnd )) return TRUE;
223 /* default: not managed */
224 return FALSE;
228 /***********************************************************************
229 * is_window_rect_mapped
231 * Check if the X whole window should be mapped based on its rectangle
233 static BOOL is_window_rect_mapped( const RECT *rect )
235 /* don't map if rect is off-screen */
236 if (rect->left >= virtual_screen_rect.right ||
237 rect->top >= virtual_screen_rect.bottom ||
238 rect->right <= virtual_screen_rect.left ||
239 rect->bottom <= virtual_screen_rect.top)
240 return FALSE;
242 return TRUE;
246 /***********************************************************************
247 * is_window_resizable
249 * Check if window should be made resizable by the window manager
251 static inline BOOL is_window_resizable( struct x11drv_win_data *data, DWORD style )
253 if (style & WS_THICKFRAME) return TRUE;
254 /* Metacity needs the window to be resizable to make it fullscreen */
255 return (data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
256 data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height);
260 /***********************************************************************
261 * get_mwm_decorations
263 static unsigned long get_mwm_decorations( struct x11drv_win_data *data,
264 DWORD style, DWORD ex_style )
266 unsigned long ret = 0;
268 if (!decorated_mode) return 0;
270 if (IsRectEmpty( &data->window_rect )) return 0;
271 if (data->shaped) return 0;
273 if (ex_style & WS_EX_TOOLWINDOW) return 0;
275 if ((style & WS_CAPTION) == WS_CAPTION)
277 ret |= MWM_DECOR_TITLE | MWM_DECOR_BORDER;
278 if (style & WS_SYSMENU) ret |= MWM_DECOR_MENU;
279 if (style & WS_MINIMIZEBOX) ret |= MWM_DECOR_MINIMIZE;
280 if (style & WS_MAXIMIZEBOX) ret |= MWM_DECOR_MAXIMIZE;
282 if (ex_style & WS_EX_DLGMODALFRAME) ret |= MWM_DECOR_BORDER;
283 else if (style & WS_THICKFRAME) ret |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
284 else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) ret |= MWM_DECOR_BORDER;
285 return ret;
289 /***********************************************************************
290 * get_x11_rect_offset
292 * Helper for X11DRV_window_to_X_rect and X11DRV_X_to_window_rect.
294 static void get_x11_rect_offset( struct x11drv_win_data *data, RECT *rect )
296 DWORD style, ex_style, style_mask = 0, ex_style_mask = 0;
297 unsigned long decor;
299 rect->top = rect->bottom = rect->left = rect->right = 0;
301 style = GetWindowLongW( data->hwnd, GWL_STYLE );
302 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
303 decor = get_mwm_decorations( data, style, ex_style );
305 if (decor & MWM_DECOR_TITLE) style_mask |= WS_CAPTION;
306 if (decor & MWM_DECOR_BORDER)
308 style_mask |= WS_DLGFRAME | WS_THICKFRAME;
309 ex_style_mask |= WS_EX_DLGMODALFRAME;
312 AdjustWindowRectEx( rect, style & style_mask, FALSE, ex_style & ex_style_mask );
316 /***********************************************************************
317 * get_window_attributes
319 * Fill the window attributes structure for an X window.
321 static int get_window_attributes( Display *display, struct x11drv_win_data *data,
322 XSetWindowAttributes *attr )
324 attr->override_redirect = !data->managed;
325 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
326 attr->save_under = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
327 attr->bit_gravity = NorthWestGravity;
328 attr->win_gravity = StaticGravity;
329 attr->backing_store = NotUseful;
330 attr->event_mask = (ExposureMask | PointerMotionMask |
331 ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
332 KeyPressMask | KeyReleaseMask | FocusChangeMask |
333 KeymapStateMask | StructureNotifyMask);
334 if (data->managed) attr->event_mask |= PropertyChangeMask;
336 return (CWOverrideRedirect | CWSaveUnder | CWColormap |
337 CWEventMask | CWBitGravity | CWBackingStore);
341 /***********************************************************************
342 * create_client_window
344 static Window create_client_window( Display *display, struct x11drv_win_data *data, XVisualInfo *vis )
346 int cx, cy, mask;
347 XSetWindowAttributes attr;
348 Window client;
349 Visual *client_visual = vis ? vis->visual : visual;
351 attr.bit_gravity = NorthWestGravity;
352 attr.win_gravity = NorthWestGravity;
353 attr.backing_store = NotUseful;
354 attr.event_mask = (ExposureMask | PointerMotionMask |
355 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
356 mask = CWEventMask | CWBitGravity | CWWinGravity | CWBackingStore;
358 if ((cx = data->client_rect.right - data->client_rect.left) <= 0) cx = 1;
359 else if (cx > 65535) cx = 65535;
360 if ((cy = data->client_rect.bottom - data->client_rect.top) <= 0) cy = 1;
361 else if (cy > 65535) cy = 65535;
363 wine_tsx11_lock();
365 if (vis)
367 attr.colormap = XCreateColormap( display, root_window, vis->visual,
368 (vis->class == PseudoColor || vis->class == GrayScale ||
369 vis->class == DirectColor) ? AllocAll : AllocNone );
370 mask |= CWColormap;
373 client = XCreateWindow( display, data->whole_window,
374 data->client_rect.left - data->whole_rect.left,
375 data->client_rect.top - data->whole_rect.top,
376 cx, cy, 0, screen_depth, InputOutput,
377 client_visual, mask, &attr );
378 if (!client)
380 wine_tsx11_unlock();
381 return 0;
384 if (data->client_window)
386 XDeleteContext( display, data->client_window, winContext );
387 XDestroyWindow( display, data->client_window );
389 data->client_window = client;
390 data->visualid = XVisualIDFromVisual( client_visual );
392 if (data->colormap) XFreeColormap( display, data->colormap );
393 data->colormap = vis ? attr.colormap : 0;
395 XMapWindow( display, data->client_window );
396 XSaveContext( display, data->client_window, winContext, (char *)data->hwnd );
397 wine_tsx11_unlock();
399 SetPropA( data->hwnd, client_window_prop, (HANDLE)data->client_window );
400 return data->client_window;
404 /***********************************************************************
405 * sync_window_style
407 * Change the X window attributes when the window style has changed.
409 static void sync_window_style( Display *display, struct x11drv_win_data *data )
411 if (data->whole_window != root_window)
413 XSetWindowAttributes attr;
414 int mask = get_window_attributes( display, data, &attr );
416 wine_tsx11_lock();
417 XChangeWindowAttributes( display, data->whole_window, mask, &attr );
418 wine_tsx11_unlock();
423 /***********************************************************************
424 * sync_window_region
426 * Update the X11 window region.
428 static void sync_window_region( Display *display, struct x11drv_win_data *data, HRGN win_region )
430 #ifdef HAVE_LIBXSHAPE
431 HRGN hrgn = win_region;
433 if (!data->whole_window) return;
434 data->shaped = FALSE;
436 if (IsRectEmpty( &data->window_rect )) /* set an empty shape */
438 static XRectangle empty_rect;
439 wine_tsx11_lock();
440 XShapeCombineRectangles( display, data->whole_window, ShapeBounding, 0, 0,
441 &empty_rect, 1, ShapeSet, YXBanded );
442 wine_tsx11_unlock();
443 return;
446 if (hrgn == (HRGN)1) /* hack: win_region == 1 means retrieve region from server */
448 if (!(hrgn = CreateRectRgn( 0, 0, 0, 0 ))) return;
449 if (GetWindowRgn( data->hwnd, hrgn ) == ERROR)
451 DeleteObject( hrgn );
452 hrgn = 0;
456 if (!hrgn)
458 wine_tsx11_lock();
459 XShapeCombineMask( display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
460 wine_tsx11_unlock();
462 else
464 RGNDATA *pRegionData;
466 if (GetWindowLongW( data->hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) MirrorRgn( data->hwnd, hrgn );
467 if ((pRegionData = X11DRV_GetRegionData( hrgn, 0 )))
469 wine_tsx11_lock();
470 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
471 data->window_rect.left - data->whole_rect.left,
472 data->window_rect.top - data->whole_rect.top,
473 (XRectangle *)pRegionData->Buffer,
474 pRegionData->rdh.nCount, ShapeSet, YXBanded );
475 wine_tsx11_unlock();
476 HeapFree(GetProcessHeap(), 0, pRegionData);
477 data->shaped = TRUE;
480 if (hrgn && hrgn != win_region) DeleteObject( hrgn );
481 #endif /* HAVE_LIBXSHAPE */
485 /***********************************************************************
486 * sync_window_opacity
488 static void sync_window_opacity( Display *display, Window win,
489 COLORREF key, BYTE alpha, DWORD flags )
491 unsigned long opacity = 0xffffffff;
493 if (flags & LWA_ALPHA) opacity = (0xffffffff / 0xff) * alpha;
495 if (flags & LWA_COLORKEY) FIXME("LWA_COLORKEY not supported\n");
497 wine_tsx11_lock();
498 if (opacity == 0xffffffff)
499 XDeleteProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY) );
500 else
501 XChangeProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY),
502 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&opacity, 1 );
503 wine_tsx11_unlock();
507 /***********************************************************************
508 * sync_window_text
510 static void sync_window_text( Display *display, Window win, const WCHAR *text )
512 UINT count;
513 char *buffer, *utf8_buffer;
514 XTextProperty prop;
516 /* allocate new buffer for window text */
517 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
518 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count ))) return;
519 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
521 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
522 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
524 HeapFree( GetProcessHeap(), 0, buffer );
525 return;
527 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
529 wine_tsx11_lock();
530 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
532 XSetWMName( display, win, &prop );
533 XSetWMIconName( display, win, &prop );
534 XFree( prop.value );
537 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
538 according to the standard
539 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
541 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
542 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
543 wine_tsx11_unlock();
545 HeapFree( GetProcessHeap(), 0, utf8_buffer );
546 HeapFree( GetProcessHeap(), 0, buffer );
550 /***********************************************************************
551 * set_win_format
553 static BOOL set_win_format( HWND hwnd, XID fbconfig_id )
555 struct x11drv_win_data *data;
556 XVisualInfo *vis;
557 int w, h;
559 if (!(data = X11DRV_get_win_data(hwnd)) &&
560 !(data = X11DRV_create_win_data(hwnd))) return FALSE;
562 if (!(vis = visual_from_fbconfig_id(fbconfig_id))) return FALSE;
564 if (data->whole_window)
566 Display *display = thread_display();
567 Window client = data->client_window;
569 if (vis->visualid != data->visualid)
571 client = create_client_window( display, data, vis );
572 TRACE( "re-created client window %lx for %p fbconfig %lx\n", client, data->hwnd, fbconfig_id );
574 wine_tsx11_lock();
575 XFree(vis);
576 XFlush( display );
577 wine_tsx11_unlock();
578 if (client) goto done;
579 return FALSE;
582 w = data->client_rect.right - data->client_rect.left;
583 h = data->client_rect.bottom - data->client_rect.top;
585 if(w <= 0) w = 1;
586 if(h <= 0) h = 1;
588 #ifdef SONAME_LIBXCOMPOSITE
589 if(usexcomposite)
591 XSetWindowAttributes attrib;
592 static Window dummy_parent;
594 wine_tsx11_lock();
595 attrib.override_redirect = True;
596 if (!dummy_parent)
598 dummy_parent = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, screen_depth,
599 InputOutput, visual, CWOverrideRedirect, &attrib );
600 XMapWindow( gdi_display, dummy_parent );
602 data->colormap = XCreateColormap(gdi_display, dummy_parent, vis->visual,
603 (vis->class == PseudoColor ||
604 vis->class == GrayScale ||
605 vis->class == DirectColor) ?
606 AllocAll : AllocNone);
607 attrib.colormap = data->colormap;
608 XInstallColormap(gdi_display, attrib.colormap);
610 if(data->gl_drawable) XDestroyWindow(gdi_display, data->gl_drawable);
611 data->gl_drawable = XCreateWindow(gdi_display, dummy_parent, -w, 0, w, h, 0,
612 vis->depth, InputOutput, vis->visual,
613 CWColormap | CWOverrideRedirect,
614 &attrib);
615 if(data->gl_drawable)
617 pXCompositeRedirectWindow(gdi_display, data->gl_drawable,
618 CompositeRedirectManual);
619 XMapWindow(gdi_display, data->gl_drawable);
621 XFree(vis);
622 XFlush( gdi_display );
623 wine_tsx11_unlock();
625 else
626 #endif
628 WARN("XComposite is not available, using GLXPixmap hack\n");
630 wine_tsx11_lock();
632 if(data->pixmap) XFreePixmap(gdi_display, data->pixmap);
633 data->pixmap = XCreatePixmap(gdi_display, root_window, w, h, vis->depth);
634 if(!data->pixmap)
636 XFree(vis);
637 wine_tsx11_unlock();
638 return FALSE;
641 if(data->gl_drawable) destroy_glxpixmap(gdi_display, data->gl_drawable);
642 data->gl_drawable = create_glxpixmap(gdi_display, vis, data->pixmap);
643 if(!data->gl_drawable)
645 XFreePixmap(gdi_display, data->pixmap);
646 data->pixmap = 0;
648 XFree(vis);
649 XFlush( gdi_display );
650 wine_tsx11_unlock();
651 if (data->pixmap) SetPropA(hwnd, pixmap_prop, (HANDLE)data->pixmap);
654 if (!data->gl_drawable) return FALSE;
656 TRACE("Created GL drawable 0x%lx, using FBConfigID 0x%lx\n",
657 data->gl_drawable, fbconfig_id);
658 SetPropA(hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
660 done:
661 data->fbconfig_id = fbconfig_id;
662 SetPropA(hwnd, fbconfig_id_prop, (HANDLE)data->fbconfig_id);
663 /* force DCE invalidation */
664 SetWindowPos( hwnd, 0, 0, 0, 0, 0,
665 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE |
666 SWP_NOREDRAW | SWP_DEFERERASE | SWP_NOSENDCHANGING | SWP_STATECHANGED);
667 return TRUE;
670 /***********************************************************************
671 * sync_gl_drawable
673 static void sync_gl_drawable(struct x11drv_win_data *data)
675 int w = data->client_rect.right - data->client_rect.left;
676 int h = data->client_rect.bottom - data->client_rect.top;
677 XVisualInfo *vis;
678 Drawable glxp;
679 Pixmap pix;
681 if (w <= 0) w = 1;
682 if (h <= 0) h = 1;
684 TRACE("Resizing GL drawable 0x%lx to %dx%d\n", data->gl_drawable, w, h);
685 #ifdef SONAME_LIBXCOMPOSITE
686 if(usexcomposite)
688 wine_tsx11_lock();
689 XMoveResizeWindow(gdi_display, data->gl_drawable, -w, 0, w, h);
690 wine_tsx11_unlock();
691 return;
693 #endif
695 if (!(vis = visual_from_fbconfig_id(data->fbconfig_id))) return;
697 wine_tsx11_lock();
698 pix = XCreatePixmap(gdi_display, root_window, w, h, vis->depth);
699 if(!pix)
701 ERR("Failed to create pixmap for offscreen rendering\n");
702 XFree(vis);
703 wine_tsx11_unlock();
704 return;
707 glxp = create_glxpixmap(gdi_display, vis, pix);
708 if(!glxp)
710 ERR("Failed to create drawable for offscreen rendering\n");
711 XFreePixmap(gdi_display, pix);
712 XFree(vis);
713 wine_tsx11_unlock();
714 return;
717 XFree(vis);
719 mark_drawable_dirty(data->gl_drawable, glxp);
721 XFreePixmap(gdi_display, data->pixmap);
722 destroy_glxpixmap(gdi_display, data->gl_drawable);
723 TRACE( "Recreated GL drawable %lx to replace %lx\n", glxp, data->gl_drawable );
725 data->pixmap = pix;
726 data->gl_drawable = glxp;
728 XFlush( gdi_display );
729 wine_tsx11_unlock();
731 SetPropA(data->hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
732 SetPropA(data->hwnd, pixmap_prop, (HANDLE)data->pixmap);
736 /***********************************************************************
737 * get_window_changes
739 * fill the window changes structure
741 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
743 int mask = 0;
745 if (old->right - old->left != new->right - new->left )
747 if ((changes->width = new->right - new->left) <= 0) changes->width = 1;
748 else if (changes->width > 65535) changes->width = 65535;
749 mask |= CWWidth;
751 if (old->bottom - old->top != new->bottom - new->top)
753 if ((changes->height = new->bottom - new->top) <= 0) changes->height = 1;
754 else if (changes->height > 65535) changes->height = 65535;
755 mask |= CWHeight;
757 if (old->left != new->left)
759 changes->x = new->left;
760 mask |= CWX;
762 if (old->top != new->top)
764 changes->y = new->top;
765 mask |= CWY;
767 return mask;
771 /***********************************************************************
772 * create_icon_window
774 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
776 XSetWindowAttributes attr;
778 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
779 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
780 attr.bit_gravity = NorthWestGravity;
781 attr.backing_store = NotUseful/*WhenMapped*/;
782 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
784 wine_tsx11_lock();
785 data->icon_window = XCreateWindow( display, root_window, 0, 0,
786 GetSystemMetrics( SM_CXICON ),
787 GetSystemMetrics( SM_CYICON ),
788 0, screen_depth,
789 InputOutput, visual,
790 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
791 XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
792 XFlush( display ); /* make sure the window exists before we start painting to it */
793 wine_tsx11_unlock();
795 TRACE( "created %lx\n", data->icon_window );
796 SetPropA( data->hwnd, icon_window_prop, (HANDLE)data->icon_window );
797 return data->icon_window;
802 /***********************************************************************
803 * destroy_icon_window
805 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
807 if (!data->icon_window) return;
808 wine_tsx11_lock();
809 XDeleteContext( display, data->icon_window, winContext );
810 XDestroyWindow( display, data->icon_window );
811 data->icon_window = 0;
812 wine_tsx11_unlock();
813 RemovePropA( data->hwnd, icon_window_prop );
817 /***********************************************************************
818 * get_bitmap_argb
820 * Return the bitmap bits in ARGB format. Helper for setting icon hints.
822 static unsigned long *get_bitmap_argb( HDC hdc, HBITMAP color, HBITMAP mask, unsigned int *size )
824 BITMAP bm;
825 BITMAPINFO *info;
826 unsigned int *ptr, *bits = NULL;
827 unsigned char *mask_bits = NULL;
828 int i, j, has_alpha = 0;
830 if (!GetObjectW( color, sizeof(bm), &bm )) return NULL;
831 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) return NULL;
832 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
833 info->bmiHeader.biWidth = bm.bmWidth;
834 info->bmiHeader.biHeight = -bm.bmHeight;
835 info->bmiHeader.biPlanes = 1;
836 info->bmiHeader.biBitCount = 32;
837 info->bmiHeader.biCompression = BI_RGB;
838 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
839 info->bmiHeader.biXPelsPerMeter = 0;
840 info->bmiHeader.biYPelsPerMeter = 0;
841 info->bmiHeader.biClrUsed = 0;
842 info->bmiHeader.biClrImportant = 0;
843 *size = bm.bmWidth * bm.bmHeight + 2;
844 if (!(bits = HeapAlloc( GetProcessHeap(), 0, *size * sizeof(long) ))) goto failed;
845 if (!GetDIBits( hdc, color, 0, bm.bmHeight, bits + 2, info, DIB_RGB_COLORS )) goto failed;
847 bits[0] = bm.bmWidth;
848 bits[1] = bm.bmHeight;
850 for (i = 0; i < bm.bmWidth * bm.bmHeight; i++)
851 if ((has_alpha = (bits[i + 2] & 0xff000000) != 0)) break;
853 if (!has_alpha)
855 unsigned int width_bytes = (bm.bmWidth + 31) / 32 * 4;
856 /* generate alpha channel from the mask */
857 info->bmiHeader.biBitCount = 1;
858 info->bmiHeader.biSizeImage = width_bytes * bm.bmHeight;
859 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto failed;
860 if (!GetDIBits( hdc, mask, 0, bm.bmHeight, mask_bits, info, DIB_RGB_COLORS )) goto failed;
861 ptr = bits + 2;
862 for (i = 0; i < bm.bmHeight; i++)
863 for (j = 0; j < bm.bmWidth; j++, ptr++)
864 if (!((mask_bits[i * width_bytes + j / 8] << (j % 8)) & 0x80)) *ptr |= 0xff000000;
865 HeapFree( GetProcessHeap(), 0, mask_bits );
867 HeapFree( GetProcessHeap(), 0, info );
869 /* convert to array of longs */
870 if (bits && sizeof(long) > sizeof(int))
871 for (i = *size - 1; i >= 0; i--) ((unsigned long *)bits)[i] = bits[i];
873 return (unsigned long *)bits;
875 failed:
876 HeapFree( GetProcessHeap(), 0, info );
877 HeapFree( GetProcessHeap(), 0, bits );
878 HeapFree( GetProcessHeap(), 0, mask_bits );
879 return NULL;
883 /***********************************************************************
884 * set_icon_hints
886 * Set the icon wm hints
888 static void set_icon_hints( Display *display, struct x11drv_win_data *data,
889 HICON icon_big, HICON icon_small )
891 XWMHints *hints = data->wm_hints;
893 if (!icon_big)
895 icon_big = (HICON)SendMessageW( data->hwnd, WM_GETICON, ICON_BIG, 0 );
896 if (!icon_big) icon_big = (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON );
898 if (!icon_small)
900 icon_small = (HICON)SendMessageW( data->hwnd, WM_GETICON, ICON_SMALL, 0 );
901 if (!icon_small) icon_small = (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICONSM );
904 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
905 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
906 data->hWMIconBitmap = 0;
907 data->hWMIconMask = 0;
909 if (!icon_big)
911 if (!data->icon_window) create_icon_window( display, data );
912 hints->icon_window = data->icon_window;
913 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
915 else
917 HBITMAP hbmOrig;
918 RECT rcMask;
919 BITMAP bm;
920 ICONINFO ii, ii_small;
921 HDC hDC;
922 unsigned int size;
923 unsigned long *bits;
925 if (!GetIconInfo(icon_big, &ii)) return;
927 GetObjectW(ii.hbmMask, sizeof(bm), &bm);
928 rcMask.top = 0;
929 rcMask.left = 0;
930 rcMask.right = bm.bmWidth;
931 rcMask.bottom = bm.bmHeight;
933 hDC = CreateCompatibleDC(0);
934 bits = get_bitmap_argb( hDC, ii.hbmColor, ii.hbmMask, &size );
935 if (bits && GetIconInfo( icon_small, &ii_small ))
937 unsigned int size_small;
938 unsigned long *bits_small, *new;
940 if ((bits_small = get_bitmap_argb( hDC, ii_small.hbmColor, ii_small.hbmMask, &size_small )) &&
941 (bits_small[0] != bits[0] || bits_small[1] != bits[1])) /* size must be different */
943 if ((new = HeapReAlloc( GetProcessHeap(), 0, bits,
944 (size + size_small) * sizeof(unsigned long) )))
946 bits = new;
947 memcpy( bits + size, bits_small, size_small * sizeof(unsigned long) );
948 size += size_small;
951 HeapFree( GetProcessHeap(), 0, bits_small );
952 DeleteObject( ii_small.hbmColor );
953 DeleteObject( ii_small.hbmMask );
955 wine_tsx11_lock();
956 if (bits)
957 XChangeProperty( display, data->whole_window, x11drv_atom(_NET_WM_ICON),
958 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)bits, size );
959 else
960 XDeleteProperty( display, data->whole_window, x11drv_atom(_NET_WM_ICON) );
961 wine_tsx11_unlock();
962 HeapFree( GetProcessHeap(), 0, bits );
964 hbmOrig = SelectObject(hDC, ii.hbmMask);
965 InvertRect(hDC, &rcMask);
966 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
967 SelectObject(hDC, hbmOrig);
969 data->hWMIconBitmap = ii.hbmColor;
970 data->hWMIconMask = ii.hbmMask;
972 hints->icon_pixmap = X11DRV_get_pixmap(data->hWMIconBitmap);
973 hints->icon_mask = X11DRV_get_pixmap(data->hWMIconMask);
974 destroy_icon_window( display, data );
975 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
977 DeleteDC(hDC);
982 /***********************************************************************
983 * set_size_hints
985 * set the window size hints
987 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
989 XSizeHints* size_hints;
991 if (!(size_hints = XAllocSizeHints())) return;
993 size_hints->win_gravity = StaticGravity;
994 size_hints->flags |= PWinGravity;
996 /* don't update size hints if window is not in normal state */
997 if (!(style & (WS_MINIMIZE | WS_MAXIMIZE)))
999 if (data->hwnd != GetDesktopWindow()) /* don't force position of desktop */
1001 size_hints->x = data->whole_rect.left;
1002 size_hints->y = data->whole_rect.top;
1003 size_hints->flags |= PPosition;
1006 if (!is_window_resizable( data, style ))
1008 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
1009 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
1010 if (size_hints->max_width <= 0 ||size_hints->max_height <= 0)
1011 size_hints->max_width = size_hints->max_height = 1;
1012 size_hints->min_width = size_hints->max_width;
1013 size_hints->min_height = size_hints->max_height;
1014 size_hints->flags |= PMinSize | PMaxSize;
1017 XSetWMNormalHints( display, data->whole_window, size_hints );
1018 XFree( size_hints );
1022 /***********************************************************************
1023 * get_process_name
1025 * get the name of the current process for setting class hints
1027 static char *get_process_name(void)
1029 static char *name;
1031 if (!name)
1033 WCHAR module[MAX_PATH];
1034 DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
1035 if (len && len < MAX_PATH)
1037 char *ptr;
1038 WCHAR *p, *appname = module;
1040 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
1041 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
1042 len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
1043 if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
1045 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
1046 name = ptr;
1050 return name;
1054 /***********************************************************************
1055 * set_initial_wm_hints
1057 * Set the window manager hints that don't change over the lifetime of a window.
1059 static void set_initial_wm_hints( Display *display, struct x11drv_win_data *data )
1061 long i;
1062 Atom protocols[3];
1063 Atom dndVersion = WINE_XDND_VERSION;
1064 XClassHint *class_hints;
1065 char *process_name = get_process_name();
1067 wine_tsx11_lock();
1069 /* wm protocols */
1070 i = 0;
1071 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
1072 protocols[i++] = x11drv_atom(_NET_WM_PING);
1073 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
1074 XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
1075 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
1077 /* class hints */
1078 if ((class_hints = XAllocClassHint()))
1080 static char wine[] = "Wine";
1082 class_hints->res_name = process_name;
1083 class_hints->res_class = wine;
1084 XSetClassHint( display, data->whole_window, class_hints );
1085 XFree( class_hints );
1088 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
1089 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
1090 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
1091 i = getpid();
1092 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
1093 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
1095 XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
1096 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
1098 update_user_time( 0 ); /* make sure that the user time window exists */
1099 if (user_time_window)
1100 XChangeProperty( display, data->whole_window, x11drv_atom(_NET_WM_USER_TIME_WINDOW),
1101 XA_WINDOW, 32, PropModeReplace, (unsigned char *)&user_time_window, 1 );
1103 data->wm_hints = XAllocWMHints();
1104 wine_tsx11_unlock();
1106 if (data->wm_hints)
1108 data->wm_hints->flags = 0;
1109 set_icon_hints( display, data, 0, 0 );
1114 /***********************************************************************
1115 * get_owner_whole_window
1117 * Retrieve an owner's window, creating it if necessary.
1119 static Window get_owner_whole_window( HWND owner, BOOL force_managed )
1121 struct x11drv_win_data *data;
1123 if (!owner) return 0;
1125 if (!(data = X11DRV_get_win_data( owner )))
1127 if (GetWindowThreadProcessId( owner, NULL ) != GetCurrentThreadId() ||
1128 !(data = X11DRV_create_win_data( owner )))
1129 return (Window)GetPropA( owner, whole_window_prop );
1131 else if (!data->managed && force_managed) /* make it managed */
1133 SetWindowPos( owner, 0, 0, 0, 0, 0,
1134 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE |
1135 SWP_NOREDRAW | SWP_DEFERERASE | SWP_NOSENDCHANGING | SWP_STATECHANGED );
1137 return data->whole_window;
1141 /***********************************************************************
1142 * set_wm_hints
1144 * Set the window manager hints for a newly-created window
1146 static void set_wm_hints( Display *display, struct x11drv_win_data *data )
1148 Window group_leader = data->whole_window;
1149 Window owner_win = 0;
1150 Atom window_type;
1151 MwmHints mwm_hints;
1152 DWORD style, ex_style;
1153 HWND owner;
1155 if (data->hwnd == GetDesktopWindow())
1157 /* force some styles for the desktop to get the correct decorations */
1158 style = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
1159 ex_style = WS_EX_APPWINDOW;
1160 owner = 0;
1162 else
1164 style = GetWindowLongW( data->hwnd, GWL_STYLE );
1165 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
1166 owner = GetWindow( data->hwnd, GW_OWNER );
1167 if ((owner_win = get_owner_whole_window( owner, data->managed ))) group_leader = owner_win;
1170 wine_tsx11_lock();
1172 if (owner_win) XSetTransientForHint( display, data->whole_window, owner_win );
1174 /* size hints */
1175 set_size_hints( display, data, style );
1177 window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
1178 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
1179 XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
1181 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
1182 mwm_hints.decorations = get_mwm_decorations( data, style, ex_style );
1183 mwm_hints.functions = MWM_FUNC_MOVE;
1184 if (is_window_resizable( data, style )) mwm_hints.functions |= MWM_FUNC_RESIZE;
1185 if (!(style & WS_DISABLED))
1187 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
1188 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
1189 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
1192 XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
1193 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
1194 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
1196 /* wm hints */
1197 if (data->wm_hints)
1199 data->wm_hints->flags |= InputHint | StateHint | WindowGroupHint;
1200 data->wm_hints->input = !use_take_focus && !(style & WS_DISABLED);
1201 data->wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
1202 data->wm_hints->window_group = group_leader;
1203 XSetWMHints( display, data->whole_window, data->wm_hints );
1206 wine_tsx11_unlock();
1210 /***********************************************************************
1211 * update_user_time
1213 void update_user_time( Time time )
1215 wine_tsx11_lock();
1216 if (!user_time_window)
1218 user_time_window = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, 0, InputOnly,
1219 DefaultVisual(gdi_display,DefaultScreen(gdi_display)), 0, NULL );
1220 TRACE( "user time window %lx\n", user_time_window );
1222 if (time && (!last_user_time || (long)(time - last_user_time) > 0))
1224 last_user_time = time;
1225 XChangeProperty( gdi_display, user_time_window, x11drv_atom(_NET_WM_USER_TIME),
1226 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&time, 1 );
1228 wine_tsx11_unlock();
1231 /***********************************************************************
1232 * update_net_wm_states
1234 void update_net_wm_states( Display *display, struct x11drv_win_data *data )
1236 static const unsigned int state_atoms[NB_NET_WM_STATES] =
1238 XATOM__NET_WM_STATE_FULLSCREEN,
1239 XATOM__NET_WM_STATE_ABOVE,
1240 XATOM__NET_WM_STATE_MAXIMIZED_VERT,
1241 XATOM__NET_WM_STATE_SKIP_PAGER,
1242 XATOM__NET_WM_STATE_SKIP_TASKBAR
1245 DWORD i, style, ex_style, new_state = 0;
1247 if (!data->managed) return;
1248 if (data->whole_window == root_window) return;
1250 style = GetWindowLongW( data->hwnd, GWL_STYLE );
1251 if (data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
1252 data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height)
1254 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION)
1255 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
1256 else if (!(style & WS_MINIMIZE))
1257 new_state |= (1 << NET_WM_STATE_FULLSCREEN);
1259 else if (style & WS_MAXIMIZE)
1260 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
1262 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
1263 if (ex_style & WS_EX_TOPMOST)
1264 new_state |= (1 << NET_WM_STATE_ABOVE);
1265 if (ex_style & (WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE))
1266 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR) | (1 << NET_WM_STATE_SKIP_PAGER);
1267 if (!(ex_style & WS_EX_APPWINDOW) && GetWindow( data->hwnd, GW_OWNER ))
1268 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR);
1270 if (!data->mapped) /* set the _NET_WM_STATE atom directly */
1272 Atom atoms[NB_NET_WM_STATES+1];
1273 DWORD count;
1275 for (i = count = 0; i < NB_NET_WM_STATES; i++)
1277 if (!(new_state & (1 << i))) continue;
1278 TRACE( "setting wm state %u for unmapped window %p/%lx\n",
1279 i, data->hwnd, data->whole_window );
1280 atoms[count++] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1281 if (state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT)
1282 atoms[count++] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
1284 wine_tsx11_lock();
1285 XChangeProperty( display, data->whole_window, x11drv_atom(_NET_WM_STATE), XA_ATOM,
1286 32, PropModeReplace, (unsigned char *)atoms, count );
1287 wine_tsx11_unlock();
1289 else /* ask the window manager to do it for us */
1291 XEvent xev;
1293 xev.xclient.type = ClientMessage;
1294 xev.xclient.window = data->whole_window;
1295 xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
1296 xev.xclient.serial = 0;
1297 xev.xclient.display = display;
1298 xev.xclient.send_event = True;
1299 xev.xclient.format = 32;
1300 xev.xclient.data.l[3] = 1;
1302 for (i = 0; i < NB_NET_WM_STATES; i++)
1304 if (!((data->net_wm_state ^ new_state) & (1 << i))) continue; /* unchanged */
1306 TRACE( "setting wm state %u for window %p/%lx to %u prev %u\n",
1307 i, data->hwnd, data->whole_window,
1308 (new_state & (1 << i)) != 0, (data->net_wm_state & (1 << i)) != 0 );
1310 xev.xclient.data.l[0] = (new_state & (1 << i)) ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1311 xev.xclient.data.l[1] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1312 xev.xclient.data.l[2] = ((state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT) ?
1313 x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ) : 0);
1314 wine_tsx11_lock();
1315 XSendEvent( display, root_window, False,
1316 SubstructureRedirectMask | SubstructureNotifyMask, &xev );
1317 wine_tsx11_unlock();
1320 data->net_wm_state = new_state;
1324 /***********************************************************************
1325 * set_xembed_flags
1327 static void set_xembed_flags( Display *display, struct x11drv_win_data *data, unsigned long flags )
1329 unsigned long info[2];
1331 if (!data->whole_window) return;
1333 info[0] = 0; /* protocol version */
1334 info[1] = flags;
1335 wine_tsx11_lock();
1336 XChangeProperty( display, data->whole_window, x11drv_atom(_XEMBED_INFO),
1337 x11drv_atom(_XEMBED_INFO), 32, PropModeReplace, (unsigned char*)info, 2 );
1338 wine_tsx11_unlock();
1342 /***********************************************************************
1343 * map_window
1345 static void map_window( Display *display, struct x11drv_win_data *data, DWORD new_style )
1347 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1349 remove_startup_notification( display, data->whole_window );
1351 wait_for_withdrawn_state( display, data, TRUE );
1353 if (!data->embedded)
1355 update_net_wm_states( display, data );
1356 sync_window_style( display, data );
1357 wine_tsx11_lock();
1358 XMapWindow( display, data->whole_window );
1359 wine_tsx11_unlock();
1361 else set_xembed_flags( display, data, XEMBED_MAPPED );
1363 data->mapped = TRUE;
1364 data->iconic = (new_style & WS_MINIMIZE) != 0;
1368 /***********************************************************************
1369 * unmap_window
1371 static void unmap_window( Display *display, struct x11drv_win_data *data )
1373 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1375 if (!data->embedded)
1377 wait_for_withdrawn_state( display, data, FALSE );
1378 wine_tsx11_lock();
1379 if (data->managed) XWithdrawWindow( display, data->whole_window, DefaultScreen(display) );
1380 else XUnmapWindow( display, data->whole_window );
1381 wine_tsx11_unlock();
1383 else set_xembed_flags( display, data, 0 );
1385 data->mapped = FALSE;
1386 data->net_wm_state = 0;
1390 /***********************************************************************
1391 * make_window_embedded
1393 void make_window_embedded( Display *display, struct x11drv_win_data *data )
1395 BOOL was_mapped = data->mapped;
1396 /* the window cannot be mapped before being embedded */
1397 if (data->mapped) unmap_window( display, data );
1399 data->embedded = TRUE;
1400 data->managed = TRUE;
1401 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1402 sync_window_style( display, data );
1404 if (was_mapped)
1405 map_window( display, data, 0 );
1406 else
1407 set_xembed_flags( display, data, 0 );
1411 /***********************************************************************
1412 * X11DRV_window_to_X_rect
1414 * Convert a rect from client to X window coordinates
1416 static void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
1418 RECT rc;
1420 if (!data->managed) return;
1421 if (IsRectEmpty( rect )) return;
1423 get_x11_rect_offset( data, &rc );
1425 rect->left -= rc.left;
1426 rect->right -= rc.right;
1427 rect->top -= rc.top;
1428 rect->bottom -= rc.bottom;
1429 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1430 if (rect->left >= rect->right) rect->right = rect->left + 1;
1434 /***********************************************************************
1435 * X11DRV_X_to_window_rect
1437 * Opposite of X11DRV_window_to_X_rect
1439 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
1441 RECT rc;
1443 if (!data->managed) return;
1444 if (IsRectEmpty( rect )) return;
1446 get_x11_rect_offset( data, &rc );
1448 rect->left += rc.left;
1449 rect->right += rc.right;
1450 rect->top += rc.top;
1451 rect->bottom += rc.bottom;
1452 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1453 if (rect->left >= rect->right) rect->right = rect->left + 1;
1457 /***********************************************************************
1458 * sync_window_position
1460 * Synchronize the X window position with the Windows one
1462 static void sync_window_position( Display *display, struct x11drv_win_data *data,
1463 UINT swp_flags, const RECT *old_window_rect,
1464 const RECT *old_whole_rect, const RECT *old_client_rect )
1466 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
1467 XWindowChanges changes;
1468 unsigned int mask = 0;
1470 if (data->managed && data->iconic) return;
1472 /* resizing a managed maximized window is not allowed */
1473 if (!(style & WS_MAXIMIZE) || !data->managed)
1475 changes.width = data->whole_rect.right - data->whole_rect.left;
1476 changes.height = data->whole_rect.bottom - data->whole_rect.top;
1477 /* if window rect is empty force size to 1x1 */
1478 if (changes.width <= 0 || changes.height <= 0) changes.width = changes.height = 1;
1479 if (changes.width > 65535) changes.width = 65535;
1480 if (changes.height > 65535) changes.height = 65535;
1481 mask |= CWWidth | CWHeight;
1484 /* only the size is allowed to change for the desktop window */
1485 if (data->whole_window != root_window)
1487 changes.x = data->whole_rect.left - virtual_screen_rect.left;
1488 changes.y = data->whole_rect.top - virtual_screen_rect.top;
1489 mask |= CWX | CWY;
1492 if (!(swp_flags & SWP_NOZORDER) || (swp_flags & SWP_SHOWWINDOW))
1494 /* find window that this one must be after */
1495 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
1496 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
1497 prev = GetWindow( prev, GW_HWNDPREV );
1498 if (!prev) /* top child */
1500 changes.stack_mode = Above;
1501 mask |= CWStackMode;
1503 /* should use stack_mode Below but most window managers don't get it right */
1504 /* and Above with a sibling doesn't work so well either, so we ignore it */
1507 wine_tsx11_lock();
1508 set_size_hints( display, data, style );
1509 data->configure_serial = NextRequest( display );
1510 XReconfigureWMWindow( display, data->whole_window,
1511 DefaultScreen(display), mask, &changes );
1512 #ifdef HAVE_LIBXSHAPE
1513 if (IsRectEmpty( old_window_rect ) != IsRectEmpty( &data->window_rect ))
1514 sync_window_region( display, data, (HRGN)1 );
1515 if (data->shaped)
1517 int old_x_offset = old_window_rect->left - old_whole_rect->left;
1518 int old_y_offset = old_window_rect->top - old_whole_rect->top;
1519 int new_x_offset = data->window_rect.left - data->whole_rect.left;
1520 int new_y_offset = data->window_rect.top - data->whole_rect.top;
1521 if (old_x_offset != new_x_offset || old_y_offset != new_y_offset)
1522 XShapeOffsetShape( display, data->whole_window, ShapeBounding,
1523 new_x_offset - old_x_offset, new_y_offset - old_y_offset );
1525 #endif
1526 wine_tsx11_unlock();
1528 TRACE( "win %p/%lx pos %d,%d,%dx%d after %lx changes=%x serial=%lu\n",
1529 data->hwnd, data->whole_window, data->whole_rect.left, data->whole_rect.top,
1530 data->whole_rect.right - data->whole_rect.left,
1531 data->whole_rect.bottom - data->whole_rect.top,
1532 changes.sibling, mask, data->configure_serial );
1536 /***********************************************************************
1537 * sync_client_position
1539 * Synchronize the X client window position with the Windows one
1541 static void sync_client_position( Display *display, struct x11drv_win_data *data,
1542 UINT swp_flags, const RECT *old_client_rect,
1543 const RECT *old_whole_rect )
1545 int mask;
1546 XWindowChanges changes;
1547 RECT old = *old_client_rect;
1548 RECT new = data->client_rect;
1550 OffsetRect( &old, -old_whole_rect->left, -old_whole_rect->top );
1551 OffsetRect( &new, -data->whole_rect.left, -data->whole_rect.top );
1552 if (!(mask = get_window_changes( &changes, &old, &new ))) return;
1554 if (data->client_window)
1556 TRACE( "setting client win %lx pos %d,%d,%dx%d changes=%x\n",
1557 data->client_window, new.left, new.top,
1558 new.right - new.left, new.bottom - new.top, mask );
1559 wine_tsx11_lock();
1560 XConfigureWindow( display, data->client_window, mask, &changes );
1561 wine_tsx11_unlock();
1564 if (data->gl_drawable && (mask & (CWWidth|CWHeight))) sync_gl_drawable( data );
1568 /***********************************************************************
1569 * move_window_bits
1571 * Move the window bits when a window is moved.
1573 static void move_window_bits( struct x11drv_win_data *data, const RECT *old_rect, const RECT *new_rect,
1574 const RECT *old_client_rect )
1576 RECT src_rect = *old_rect;
1577 RECT dst_rect = *new_rect;
1578 HDC hdc_src, hdc_dst;
1579 INT code;
1580 HRGN rgn;
1581 HWND parent = 0;
1583 if (!data->whole_window)
1585 OffsetRect( &dst_rect, -data->window_rect.left, -data->window_rect.top );
1586 parent = GetAncestor( data->hwnd, GA_PARENT );
1587 hdc_src = GetDCEx( parent, 0, DCX_CACHE );
1588 hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE | DCX_WINDOW );
1590 else
1592 OffsetRect( &dst_rect, -data->client_rect.left, -data->client_rect.top );
1593 /* make src rect relative to the old position of the window */
1594 OffsetRect( &src_rect, -old_client_rect->left, -old_client_rect->top );
1595 if (dst_rect.left == src_rect.left && dst_rect.top == src_rect.top) return;
1596 hdc_src = hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE );
1599 rgn = CreateRectRgnIndirect( &dst_rect );
1600 SelectClipRgn( hdc_dst, rgn );
1601 DeleteObject( rgn );
1602 ExcludeUpdateRgn( hdc_dst, data->hwnd );
1604 code = X11DRV_START_EXPOSURES;
1605 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
1607 TRACE( "copying bits for win %p/%lx/%lx %s -> %s\n",
1608 data->hwnd, data->whole_window, data->client_window,
1609 wine_dbgstr_rect(&src_rect), wine_dbgstr_rect(&dst_rect) );
1610 BitBlt( hdc_dst, dst_rect.left, dst_rect.top,
1611 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top,
1612 hdc_src, src_rect.left, src_rect.top, SRCCOPY );
1614 rgn = 0;
1615 code = X11DRV_END_EXPOSURES;
1616 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, sizeof(rgn), (LPSTR)&rgn );
1618 ReleaseDC( data->hwnd, hdc_dst );
1619 if (hdc_src != hdc_dst) ReleaseDC( parent, hdc_src );
1621 if (rgn)
1623 if (!data->whole_window)
1625 /* map region to client rect since we are using DCX_WINDOW */
1626 OffsetRgn( rgn, data->window_rect.left - data->client_rect.left,
1627 data->window_rect.top - data->client_rect.top );
1628 RedrawWindow( data->hwnd, NULL, rgn,
1629 RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ALLCHILDREN );
1631 else RedrawWindow( data->hwnd, NULL, rgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1632 DeleteObject( rgn );
1637 /**********************************************************************
1638 * create_whole_window
1640 * Create the whole X window for a given window
1642 static Window create_whole_window( Display *display, struct x11drv_win_data *data )
1644 int cx, cy, mask;
1645 XSetWindowAttributes attr;
1646 WCHAR text[1024];
1647 COLORREF key;
1648 BYTE alpha;
1649 DWORD layered_flags;
1650 HRGN win_rgn;
1652 if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1654 TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1655 data->managed = TRUE;
1656 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1659 if ((win_rgn = CreateRectRgn( 0, 0, 0, 0 )) &&
1660 GetWindowRgn( data->hwnd, win_rgn ) == ERROR)
1662 DeleteObject( win_rgn );
1663 win_rgn = 0;
1665 data->shaped = (win_rgn != 0);
1667 mask = get_window_attributes( display, data, &attr );
1669 data->whole_rect = data->window_rect;
1670 X11DRV_window_to_X_rect( data, &data->whole_rect );
1671 if (!(cx = data->whole_rect.right - data->whole_rect.left)) cx = 1;
1672 else if (cx > 65535) cx = 65535;
1673 if (!(cy = data->whole_rect.bottom - data->whole_rect.top)) cy = 1;
1674 else if (cy > 65535) cy = 65535;
1676 wine_tsx11_lock();
1677 data->whole_window = XCreateWindow( display, root_window,
1678 data->whole_rect.left - virtual_screen_rect.left,
1679 data->whole_rect.top - virtual_screen_rect.top,
1680 cx, cy, 0, screen_depth, InputOutput,
1681 visual, mask, &attr );
1683 if (data->whole_window) XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
1684 wine_tsx11_unlock();
1686 if (!data->whole_window) goto done;
1688 if (!create_client_window( display, data, NULL ))
1690 wine_tsx11_lock();
1691 XDeleteContext( display, data->whole_window, winContext );
1692 XDestroyWindow( display, data->whole_window );
1693 data->whole_window = 0;
1694 wine_tsx11_unlock();
1695 goto done;
1698 set_initial_wm_hints( display, data );
1699 set_wm_hints( display, data );
1701 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1703 /* set the window text */
1704 if (!InternalGetWindowText( data->hwnd, text, sizeof(text)/sizeof(WCHAR) )) text[0] = 0;
1705 sync_window_text( display, data->whole_window, text );
1707 /* set the window region */
1708 if (win_rgn || IsRectEmpty( &data->window_rect )) sync_window_region( display, data, win_rgn );
1710 /* set the window opacity */
1711 if (!GetLayeredWindowAttributes( data->hwnd, &key, &alpha, &layered_flags )) layered_flags = 0;
1712 sync_window_opacity( display, data->whole_window, key, alpha, layered_flags );
1714 wine_tsx11_lock();
1715 XFlush( display ); /* make sure the window exists before we start painting to it */
1716 wine_tsx11_unlock();
1718 sync_window_cursor( data );
1719 /* setting the cursor can fail if the window isn't created yet */
1720 /* so make sure that we try again once we receive a mouse event */
1721 data->cursor = (HANDLE)~0u;
1723 done:
1724 if (win_rgn) DeleteObject( win_rgn );
1725 return data->whole_window;
1729 /**********************************************************************
1730 * destroy_whole_window
1732 * Destroy the whole X window for a given window.
1734 static void destroy_whole_window( Display *display, struct x11drv_win_data *data, BOOL already_destroyed )
1736 if (!data->whole_window)
1738 if (data->embedded)
1740 Window xwin = (Window)GetPropA( data->hwnd, foreign_window_prop );
1741 if (xwin)
1743 wine_tsx11_lock();
1744 if (!already_destroyed) XSelectInput( display, xwin, 0 );
1745 XDeleteContext( display, xwin, winContext );
1746 wine_tsx11_unlock();
1747 RemovePropA( data->hwnd, foreign_window_prop );
1750 return;
1754 TRACE( "win %p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1755 wine_tsx11_lock();
1756 XDeleteContext( display, data->whole_window, winContext );
1757 XDeleteContext( display, data->client_window, winContext );
1758 if (!already_destroyed) XDestroyWindow( display, data->whole_window );
1759 data->whole_window = data->client_window = 0;
1760 data->wm_state = WithdrawnState;
1761 data->net_wm_state = 0;
1762 data->mapped = FALSE;
1763 if (data->xic)
1765 XUnsetICFocus( data->xic );
1766 XDestroyIC( data->xic );
1767 data->xic = 0;
1769 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1770 XFlush( display );
1771 XFree( data->wm_hints );
1772 data->wm_hints = NULL;
1773 wine_tsx11_unlock();
1774 RemovePropA( data->hwnd, whole_window_prop );
1775 RemovePropA( data->hwnd, client_window_prop );
1779 /*****************************************************************
1780 * SetWindowText (X11DRV.@)
1782 void CDECL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1784 Window win;
1786 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(gdi_display))
1788 Display *display = thread_init_display();
1789 sync_window_text( display, win, text );
1794 /***********************************************************************
1795 * SetWindowStyle (X11DRV.@)
1797 * Update the X state of a window to reflect a style change
1799 void CDECL X11DRV_SetWindowStyle( HWND hwnd, INT offset, STYLESTRUCT *style )
1801 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1802 DWORD changed;
1804 if (hwnd == GetDesktopWindow()) return;
1805 changed = style->styleNew ^ style->styleOld;
1807 /* if WS_VISIBLE was set through WM_SETREDRAW, map the window if it's the first time */
1808 if (offset == GWL_STYLE && (changed & WS_VISIBLE) && (style->styleNew & WS_VISIBLE) && !data)
1810 if (!(data = X11DRV_create_win_data( hwnd ))) return;
1812 if (data->whole_window && is_window_rect_mapped( &data->window_rect ))
1814 Display *display = thread_display();
1815 set_wm_hints( display, data );
1816 if (!data->mapped) map_window( display, data, style->styleNew );
1819 if (!data || !data->whole_window) return;
1821 if (offset == GWL_STYLE && (changed & WS_DISABLED))
1822 set_wm_hints( thread_display(), data );
1824 if (offset == GWL_EXSTYLE && (changed & WS_EX_LAYERED)) /* changing WS_EX_LAYERED resets attributes */
1825 sync_window_opacity( thread_display(), data->whole_window, 0, 0, 0 );
1829 /***********************************************************************
1830 * DestroyWindow (X11DRV.@)
1832 void CDECL X11DRV_DestroyWindow( HWND hwnd )
1834 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1835 struct x11drv_win_data *data;
1837 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1839 if (data->pixmap)
1841 wine_tsx11_lock();
1842 destroy_glxpixmap(gdi_display, data->gl_drawable);
1843 XFreePixmap(gdi_display, data->pixmap);
1844 wine_tsx11_unlock();
1846 else if (data->gl_drawable)
1848 wine_tsx11_lock();
1849 XDestroyWindow(gdi_display, data->gl_drawable);
1850 wine_tsx11_unlock();
1853 destroy_whole_window( thread_data->display, data, FALSE );
1854 destroy_icon_window( thread_data->display, data );
1856 if (data->colormap)
1858 wine_tsx11_lock();
1859 XFreeColormap( thread_data->display, data->colormap );
1860 wine_tsx11_unlock();
1863 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1864 if (thread_data->last_xic_hwnd == hwnd) thread_data->last_xic_hwnd = 0;
1865 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
1866 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
1867 wine_tsx11_lock();
1868 XDeleteContext( thread_data->display, (XID)hwnd, win_data_context );
1869 wine_tsx11_unlock();
1870 HeapFree( GetProcessHeap(), 0, data );
1874 /***********************************************************************
1875 * X11DRV_DestroyNotify
1877 void X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1879 Display *display = event->xdestroywindow.display;
1880 struct x11drv_win_data *data;
1882 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1883 if (!data->embedded) FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1885 destroy_whole_window( display, data, TRUE );
1886 if (data->embedded) SendMessageW( hwnd, WM_CLOSE, 0, 0 );
1890 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
1892 struct x11drv_win_data *data;
1894 if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
1896 data->hwnd = hwnd;
1897 wine_tsx11_lock();
1898 if (!winContext) winContext = XUniqueContext();
1899 if (!win_data_context) win_data_context = XUniqueContext();
1900 XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
1901 wine_tsx11_unlock();
1903 return data;
1907 /* initialize the desktop window id in the desktop manager process */
1908 static struct x11drv_win_data *create_desktop_win_data( Display *display, HWND hwnd )
1910 struct x11drv_win_data *data;
1912 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1913 data->whole_window = data->client_window = root_window;
1914 data->managed = TRUE;
1915 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1916 SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
1917 SetPropA( data->hwnd, client_window_prop, (HANDLE)root_window );
1918 set_initial_wm_hints( display, data );
1919 return data;
1922 /**********************************************************************
1923 * CreateDesktopWindow (X11DRV.@)
1925 BOOL CDECL X11DRV_CreateDesktopWindow( HWND hwnd )
1927 unsigned int width, height;
1929 /* retrieve the real size of the desktop */
1930 SERVER_START_REQ( get_window_rectangles )
1932 req->handle = wine_server_user_handle( hwnd );
1933 req->relative = COORDS_CLIENT;
1934 wine_server_call( req );
1935 width = reply->window.right;
1936 height = reply->window.bottom;
1938 SERVER_END_REQ;
1940 if (!width && !height) /* not initialized yet */
1942 SERVER_START_REQ( set_window_pos )
1944 req->handle = wine_server_user_handle( hwnd );
1945 req->previous = 0;
1946 req->flags = SWP_NOZORDER;
1947 req->window.left = virtual_screen_rect.left;
1948 req->window.top = virtual_screen_rect.top;
1949 req->window.right = virtual_screen_rect.right;
1950 req->window.bottom = virtual_screen_rect.bottom;
1951 req->client = req->window;
1952 wine_server_call( req );
1954 SERVER_END_REQ;
1956 else
1958 Window win = (Window)GetPropA( hwnd, whole_window_prop );
1959 if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1961 return TRUE;
1965 /**********************************************************************
1966 * CreateWindow (X11DRV.@)
1968 BOOL CDECL X11DRV_CreateWindow( HWND hwnd )
1970 Display *display = thread_init_display();
1972 if (hwnd == GetDesktopWindow())
1974 XSetWindowAttributes attr;
1976 if (root_window != DefaultRootWindow( gdi_display ))
1978 /* the desktop win data can't be created lazily */
1979 if (!create_desktop_win_data( display, hwnd )) return FALSE;
1982 /* create the cursor clipping window */
1983 attr.override_redirect = TRUE;
1984 attr.event_mask = StructureNotifyMask;
1985 wine_tsx11_lock();
1986 clip_window = XCreateWindow( display, root_window, 0, 0, 1, 1, 0, 0,
1987 InputOnly, visual, CWOverrideRedirect | CWEventMask, &attr );
1988 wine_tsx11_unlock();
1989 SetPropA( hwnd, clip_window_prop, (HANDLE)clip_window );
1991 else if (!clip_window)
1993 clip_window = (Window)GetPropA( GetDesktopWindow(), clip_window_prop );
1994 wine_tsx11_lock();
1995 XSelectInput( display, clip_window, StructureNotifyMask );
1996 wine_tsx11_unlock();
1999 return TRUE;
2003 /***********************************************************************
2004 * X11DRV_get_win_data
2006 * Return the X11 data structure associated with a window.
2008 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
2010 struct x11drv_thread_data *thread_data = x11drv_thread_data();
2011 char *data;
2013 if (!thread_data) return NULL;
2014 if (!hwnd) return NULL;
2015 if (XFindContext( thread_data->display, (XID)hwnd, win_data_context, &data )) data = NULL;
2016 return (struct x11drv_win_data *)data;
2020 /***********************************************************************
2021 * X11DRV_create_win_data
2023 * Create an X11 data window structure for an existing window.
2025 struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd )
2027 Display *display;
2028 struct x11drv_win_data *data;
2029 HWND parent;
2031 if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL; /* desktop */
2033 /* don't create win data for HWND_MESSAGE windows */
2034 if (parent != GetDesktopWindow() && !GetAncestor( parent, GA_PARENT )) return NULL;
2036 display = thread_init_display();
2037 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
2039 GetWindowRect( hwnd, &data->window_rect );
2040 MapWindowPoints( 0, parent, (POINT *)&data->window_rect, 2 );
2041 data->whole_rect = data->window_rect;
2042 GetClientRect( hwnd, &data->client_rect );
2043 MapWindowPoints( hwnd, parent, (POINT *)&data->client_rect, 2 );
2045 if (parent == GetDesktopWindow())
2047 if (!create_whole_window( display, data ))
2049 HeapFree( GetProcessHeap(), 0, data );
2050 return NULL;
2052 TRACE( "win %p/%lx/%lx window %s whole %s client %s\n",
2053 hwnd, data->whole_window, data->client_window, wine_dbgstr_rect( &data->window_rect ),
2054 wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
2056 return data;
2060 /* window procedure for foreign windows */
2061 static LRESULT WINAPI foreign_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2063 switch(msg)
2065 case WM_WINDOWPOSCHANGED:
2066 update_systray_balloon_position();
2067 break;
2068 case WM_PARENTNOTIFY:
2069 if (LOWORD(wparam) == WM_DESTROY)
2071 TRACE( "%p: got parent notify destroy for win %lx\n", hwnd, lparam );
2072 PostMessageW( hwnd, WM_CLOSE, 0, 0 ); /* so that we come back here once the child is gone */
2074 return 0;
2075 case WM_CLOSE:
2076 if (GetWindow( hwnd, GW_CHILD )) return 0; /* refuse to die if we still have children */
2077 break;
2079 return DefWindowProcW( hwnd, msg, wparam, lparam );
2083 /***********************************************************************
2084 * create_foreign_window
2086 * Create a foreign window for the specified X window and its ancestors
2088 HWND create_foreign_window( Display *display, Window xwin )
2090 static const WCHAR classW[] = {'_','_','w','i','n','e','_','x','1','1','_','f','o','r','e','i','g','n','_','w','i','n','d','o','w',0};
2091 static BOOL class_registered;
2092 struct x11drv_win_data *data;
2093 HWND hwnd, parent;
2094 Window xparent, xroot;
2095 Window *xchildren;
2096 unsigned int nchildren;
2097 XWindowAttributes attr;
2098 DWORD style = WS_CLIPCHILDREN;
2100 if (!class_registered)
2102 WNDCLASSEXW class;
2104 memset( &class, 0, sizeof(class) );
2105 class.cbSize = sizeof(class);
2106 class.lpfnWndProc = foreign_window_proc;
2107 class.lpszClassName = classW;
2108 if (!RegisterClassExW( &class ) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
2110 ERR( "Could not register foreign window class\n" );
2111 return FALSE;
2113 class_registered = TRUE;
2116 wine_tsx11_lock();
2117 if (XFindContext( display, xwin, winContext, (char **)&hwnd )) hwnd = 0;
2118 if (hwnd) /* already created */
2120 wine_tsx11_unlock();
2121 return hwnd;
2124 XSelectInput( display, xwin, StructureNotifyMask );
2125 if (!XGetWindowAttributes( display, xwin, &attr ) ||
2126 !XQueryTree( display, xwin, &xroot, &xparent, &xchildren, &nchildren ))
2128 XSelectInput( display, xwin, 0 );
2129 wine_tsx11_unlock();
2130 return 0;
2132 XFree( xchildren );
2133 wine_tsx11_unlock();
2135 if (xparent == xroot)
2137 parent = GetDesktopWindow();
2138 style |= WS_POPUP;
2139 attr.x += virtual_screen_rect.left;
2140 attr.y += virtual_screen_rect.top;
2142 else
2144 parent = create_foreign_window( display, xparent );
2145 style |= WS_CHILD;
2148 hwnd = CreateWindowW( classW, NULL, style, attr.x, attr.y, attr.width, attr.height,
2149 parent, 0, 0, NULL );
2151 if (!(data = alloc_win_data( display, hwnd )))
2153 DestroyWindow( hwnd );
2154 return 0;
2156 SetRect( &data->window_rect, attr.x, attr.y, attr.x + attr.width, attr.y + attr.height );
2157 data->whole_rect = data->client_rect = data->window_rect;
2158 data->whole_window = data->client_window = 0;
2159 data->embedded = TRUE;
2160 data->mapped = TRUE;
2162 SetPropA( hwnd, foreign_window_prop, (HANDLE)xwin );
2163 wine_tsx11_lock();
2164 XSaveContext( display, xwin, winContext, (char *)data->hwnd );
2165 wine_tsx11_unlock();
2167 ShowWindow( hwnd, SW_SHOW );
2169 TRACE( "win %lx parent %p style %08x %s -> hwnd %p\n",
2170 xwin, parent, style, wine_dbgstr_rect(&data->window_rect), hwnd );
2172 return hwnd;
2176 /***********************************************************************
2177 * X11DRV_get_whole_window
2179 * Return the X window associated with the full area of a window
2181 Window X11DRV_get_whole_window( HWND hwnd )
2183 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2185 if (!data)
2187 if (hwnd == GetDesktopWindow()) return root_window;
2188 return (Window)GetPropA( hwnd, whole_window_prop );
2190 return data->whole_window;
2194 /***********************************************************************
2195 * X11DRV_get_client_window
2197 * Return the X window associated with the client area of a window
2199 static Window X11DRV_get_client_window( HWND hwnd )
2201 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2203 if (!data)
2205 if (hwnd == GetDesktopWindow()) return root_window;
2206 return (Window)GetPropA( hwnd, client_window_prop );
2208 return data->client_window;
2212 /***********************************************************************
2213 * X11DRV_get_ic
2215 * Return the X input context associated with a window
2217 XIC X11DRV_get_ic( HWND hwnd )
2219 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2220 XIM xim;
2222 if (!data) return 0;
2224 x11drv_thread_data()->last_xic_hwnd = hwnd;
2225 if (data->xic) return data->xic;
2226 if (!(xim = x11drv_thread_data()->xim)) return 0;
2227 return X11DRV_CreateIC( xim, data );
2231 /***********************************************************************
2232 * X11DRV_GetDC (X11DRV.@)
2234 void CDECL X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
2235 const RECT *top_rect, DWORD flags )
2237 struct x11drv_escape_set_drawable escape;
2238 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2239 HWND parent;
2241 escape.code = X11DRV_SET_DRAWABLE;
2242 escape.mode = IncludeInferiors;
2243 escape.fbconfig_id = 0;
2244 escape.gl_drawable = 0;
2245 escape.pixmap = 0;
2246 escape.gl_copy = FALSE;
2248 escape.dc_rect.left = win_rect->left - top_rect->left;
2249 escape.dc_rect.top = win_rect->top - top_rect->top;
2250 escape.dc_rect.right = win_rect->right - top_rect->left;
2251 escape.dc_rect.bottom = win_rect->bottom - top_rect->top;
2252 escape.drawable_rect.left = top_rect->left;
2253 escape.drawable_rect.top = top_rect->top;
2254 escape.drawable_rect.right = top_rect->right;
2255 escape.drawable_rect.bottom = top_rect->bottom;
2257 if (top == hwnd)
2259 escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
2260 /* GL draws to the client area even for window DCs */
2261 escape.gl_drawable = data ? data->client_window : X11DRV_get_client_window( hwnd );
2262 if (data && IsIconic( hwnd ) && data->icon_window)
2264 escape.drawable = data->icon_window;
2266 else if (flags & DCX_WINDOW)
2267 escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
2268 else
2269 escape.drawable = escape.gl_drawable;
2271 else
2273 /* find the first ancestor that has a drawable */
2274 for (parent = hwnd; parent && parent != top; parent = GetAncestor( parent, GA_PARENT ))
2275 if ((escape.drawable = X11DRV_get_client_window( parent ))) break;
2277 if (escape.drawable)
2279 POINT pt = { 0, 0 };
2280 MapWindowPoints( top, parent, &pt, 1 );
2281 OffsetRect( &escape.dc_rect, pt.x, pt.y );
2282 OffsetRect( &escape.drawable_rect, -pt.x, -pt.y );
2284 else escape.drawable = X11DRV_get_client_window( top );
2286 escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
2287 escape.gl_drawable = data ? data->gl_drawable : (Drawable)GetPropA( hwnd, gl_drawable_prop );
2288 escape.pixmap = data ? data->pixmap : (Pixmap)GetPropA( hwnd, pixmap_prop );
2289 escape.gl_copy = (escape.gl_drawable != 0);
2290 if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
2293 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2297 /***********************************************************************
2298 * X11DRV_ReleaseDC (X11DRV.@)
2300 void CDECL X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
2302 struct x11drv_escape_set_drawable escape;
2304 escape.code = X11DRV_SET_DRAWABLE;
2305 escape.drawable = root_window;
2306 escape.mode = IncludeInferiors;
2307 escape.drawable_rect = virtual_screen_rect;
2308 SetRect( &escape.dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
2309 virtual_screen_rect.bottom - virtual_screen_rect.top );
2310 OffsetRect( &escape.dc_rect, -escape.drawable_rect.left, -escape.drawable_rect.top );
2311 escape.fbconfig_id = 0;
2312 escape.gl_drawable = 0;
2313 escape.pixmap = 0;
2314 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2318 /***********************************************************************
2319 * SetCapture (X11DRV.@)
2321 void CDECL X11DRV_SetCapture( HWND hwnd, UINT flags )
2323 struct x11drv_thread_data *thread_data = x11drv_thread_data();
2325 if (!thread_data) return;
2326 if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
2328 if (hwnd)
2330 Window grab_win = X11DRV_get_client_window( GetAncestor( hwnd, GA_ROOT ) );
2332 if (!grab_win) return;
2333 wine_tsx11_lock();
2334 XFlush( gdi_display );
2335 XGrabPointer( thread_data->display, grab_win, False,
2336 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
2337 GrabModeAsync, GrabModeAsync, None, None, CurrentTime );
2338 wine_tsx11_unlock();
2339 thread_data->grab_window = grab_win;
2341 else /* release capture */
2343 wine_tsx11_lock();
2344 XFlush( gdi_display );
2345 XUngrabPointer( thread_data->display, CurrentTime );
2346 XFlush( thread_data->display );
2347 wine_tsx11_unlock();
2348 thread_data->grab_window = None;
2353 /*****************************************************************
2354 * SetParent (X11DRV.@)
2356 void CDECL X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
2358 Display *display = thread_display();
2359 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2361 if (!data) return;
2362 if (parent == old_parent) return;
2363 if (data->embedded) return;
2365 if (parent != GetDesktopWindow()) /* a child window */
2367 if (old_parent == GetDesktopWindow())
2369 /* destroy the old X windows */
2370 destroy_whole_window( display, data, FALSE );
2371 destroy_icon_window( display, data );
2372 if (data->managed)
2374 data->managed = FALSE;
2375 RemovePropA( data->hwnd, managed_prop );
2379 else /* new top level window */
2381 /* FIXME: we ignore errors since we can't really recover anyway */
2382 create_whole_window( display, data );
2387 /*****************************************************************
2388 * SetFocus (X11DRV.@)
2390 * Set the X focus.
2392 void CDECL X11DRV_SetFocus( HWND hwnd )
2394 Display *display = thread_display();
2395 struct x11drv_win_data *data;
2396 XWindowChanges changes;
2397 DWORD timestamp;
2399 if (!(hwnd = GetAncestor( hwnd, GA_ROOT ))) return;
2400 if (!(data = X11DRV_get_win_data( hwnd ))) return;
2401 if (data->managed || !data->whole_window) return;
2403 if (EVENT_x11_time_to_win32_time(0))
2404 /* ICCCM says don't use CurrentTime, so try to use last message time if possible */
2405 /* FIXME: this is not entirely correct */
2406 timestamp = GetMessageTime() - EVENT_x11_time_to_win32_time(0);
2407 else
2408 timestamp = CurrentTime;
2410 /* Set X focus and install colormap */
2411 wine_tsx11_lock();
2412 changes.stack_mode = Above;
2413 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
2414 XSetInputFocus( display, data->whole_window, RevertToParent, timestamp );
2415 wine_tsx11_unlock();
2419 /***********************************************************************
2420 * WindowPosChanging (X11DRV.@)
2422 void CDECL X11DRV_WindowPosChanging( HWND hwnd, HWND insert_after, UINT swp_flags,
2423 const RECT *window_rect, const RECT *client_rect, RECT *visible_rect )
2425 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2426 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
2428 if (!data)
2430 /* create the win data if the window is being made visible */
2431 if (!(style & WS_VISIBLE) && !(swp_flags & SWP_SHOWWINDOW)) return;
2432 if (!(data = X11DRV_create_win_data( hwnd ))) return;
2435 /* check if we need to switch the window to managed */
2436 if (!data->managed && data->whole_window && is_window_managed( hwnd, swp_flags, window_rect ))
2438 TRACE( "making win %p/%lx managed\n", hwnd, data->whole_window );
2439 if (data->mapped) unmap_window( thread_display(), data );
2440 data->managed = TRUE;
2441 SetPropA( hwnd, managed_prop, (HANDLE)1 );
2444 *visible_rect = *window_rect;
2445 X11DRV_window_to_X_rect( data, visible_rect );
2449 /***********************************************************************
2450 * WindowPosChanged (X11DRV.@)
2452 void CDECL X11DRV_WindowPosChanged( HWND hwnd, HWND insert_after, UINT swp_flags,
2453 const RECT *rectWindow, const RECT *rectClient,
2454 const RECT *visible_rect, const RECT *valid_rects )
2456 struct x11drv_thread_data *thread_data;
2457 Display *display;
2458 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2459 DWORD new_style = GetWindowLongW( hwnd, GWL_STYLE );
2460 RECT old_window_rect, old_whole_rect, old_client_rect;
2461 int event_type;
2463 if (!data) return;
2465 thread_data = x11drv_thread_data();
2466 display = thread_data->display;
2468 old_window_rect = data->window_rect;
2469 old_whole_rect = data->whole_rect;
2470 old_client_rect = data->client_rect;
2471 data->window_rect = *rectWindow;
2472 data->whole_rect = *visible_rect;
2473 data->client_rect = *rectClient;
2475 TRACE( "win %p window %s client %s style %08x flags %08x\n",
2476 hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style, swp_flags );
2478 if (!IsRectEmpty( &valid_rects[0] ))
2480 int x_offset = old_whole_rect.left - data->whole_rect.left;
2481 int y_offset = old_whole_rect.top - data->whole_rect.top;
2483 /* if all that happened is that the whole window moved, copy everything */
2484 if (!(swp_flags & SWP_FRAMECHANGED) &&
2485 old_whole_rect.right - data->whole_rect.right == x_offset &&
2486 old_whole_rect.bottom - data->whole_rect.bottom == y_offset &&
2487 old_client_rect.left - data->client_rect.left == x_offset &&
2488 old_client_rect.right - data->client_rect.right == x_offset &&
2489 old_client_rect.top - data->client_rect.top == y_offset &&
2490 old_client_rect.bottom - data->client_rect.bottom == y_offset &&
2491 !memcmp( &valid_rects[0], &data->client_rect, sizeof(RECT) ))
2493 /* if we have an X window the bits will be moved by the X server */
2494 if (!data->whole_window && (x_offset != 0 || y_offset != 0))
2495 move_window_bits( data, &old_whole_rect, &data->whole_rect, &old_client_rect );
2497 else
2498 move_window_bits( data, &valid_rects[1], &valid_rects[0], &old_client_rect );
2501 wine_tsx11_lock();
2502 XFlush( gdi_display ); /* make sure painting is done before we move the window */
2503 wine_tsx11_unlock();
2505 sync_client_position( display, data, swp_flags, &old_client_rect, &old_whole_rect );
2507 if (!data->whole_window) return;
2509 /* check if we are currently processing an event relevant to this window */
2510 event_type = 0;
2511 if (thread_data->current_event && thread_data->current_event->xany.window == data->whole_window)
2512 event_type = thread_data->current_event->type;
2514 if (event_type != ConfigureNotify && event_type != PropertyNotify &&
2515 event_type != GravityNotify && event_type != ReparentNotify)
2516 event_type = 0; /* ignore other events */
2518 if (data->mapped && event_type != ReparentNotify)
2520 if (((swp_flags & SWP_HIDEWINDOW) && !(new_style & WS_VISIBLE)) ||
2521 (!event_type &&
2522 !is_window_rect_mapped( rectWindow ) && is_window_rect_mapped( &old_window_rect )))
2523 unmap_window( display, data );
2526 /* don't change position if we are about to minimize or maximize a managed window */
2527 if (!event_type &&
2528 !(data->managed && (swp_flags & SWP_STATECHANGED) && (new_style & (WS_MINIMIZE|WS_MAXIMIZE))))
2529 sync_window_position( display, data, swp_flags,
2530 &old_window_rect, &old_whole_rect, &old_client_rect );
2532 if ((new_style & WS_VISIBLE) &&
2533 ((new_style & WS_MINIMIZE) || is_window_rect_mapped( rectWindow )))
2535 if (!data->mapped || (swp_flags & (SWP_FRAMECHANGED|SWP_STATECHANGED)))
2536 set_wm_hints( display, data );
2538 if (!data->mapped)
2540 map_window( display, data, new_style );
2542 else if ((swp_flags & SWP_STATECHANGED) && (!data->iconic != !(new_style & WS_MINIMIZE)))
2544 data->iconic = (new_style & WS_MINIMIZE) != 0;
2545 TRACE( "changing win %p iconic state to %u\n", data->hwnd, data->iconic );
2546 wine_tsx11_lock();
2547 if (data->iconic)
2548 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
2549 else if (is_window_rect_mapped( rectWindow ))
2550 XMapWindow( display, data->whole_window );
2551 wine_tsx11_unlock();
2552 update_net_wm_states( display, data );
2554 else if (!event_type)
2556 update_net_wm_states( display, data );
2560 wine_tsx11_lock();
2561 XFlush( display ); /* make sure changes are done before we start painting again */
2562 wine_tsx11_unlock();
2566 /***********************************************************************
2567 * ShowWindow (X11DRV.@)
2569 UINT CDECL X11DRV_ShowWindow( HWND hwnd, INT cmd, RECT *rect, UINT swp )
2571 int x, y;
2572 unsigned int width, height, border, depth;
2573 Window root, top;
2574 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
2575 struct x11drv_thread_data *thread_data = x11drv_thread_data();
2576 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2578 if (!data || !data->whole_window || !data->managed || !data->mapped || data->iconic) return swp;
2579 if (style & WS_MINIMIZE) return swp;
2580 if (IsRectEmpty( rect )) return swp;
2582 /* only fetch the new rectangle if the ShowWindow was a result of a window manager event */
2584 if (!thread_data->current_event || thread_data->current_event->xany.window != data->whole_window)
2585 return swp;
2587 if (thread_data->current_event->type != ConfigureNotify &&
2588 thread_data->current_event->type != PropertyNotify)
2589 return swp;
2591 TRACE( "win %p/%lx cmd %d at %s flags %08x\n",
2592 hwnd, data->whole_window, cmd, wine_dbgstr_rect(rect), swp );
2594 wine_tsx11_lock();
2595 XGetGeometry( thread_data->display, data->whole_window,
2596 &root, &x, &y, &width, &height, &border, &depth );
2597 XTranslateCoordinates( thread_data->display, data->whole_window, root, 0, 0, &x, &y, &top );
2598 wine_tsx11_unlock();
2599 rect->left = x;
2600 rect->top = y;
2601 rect->right = x + width;
2602 rect->bottom = y + height;
2603 OffsetRect( rect, virtual_screen_rect.left, virtual_screen_rect.top );
2604 X11DRV_X_to_window_rect( data, rect );
2605 return swp & ~(SWP_NOMOVE | SWP_NOCLIENTMOVE | SWP_NOSIZE | SWP_NOCLIENTSIZE);
2609 /**********************************************************************
2610 * SetWindowIcon (X11DRV.@)
2612 * hIcon or hIconSm has changed (or is being initialised for the
2613 * first time). Complete the X11 driver-specific initialisation
2614 * and set the window hints.
2616 * This is not entirely correct, may need to create
2617 * an icon window and set the pixmap as a background
2619 void CDECL X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
2621 Display *display = thread_display();
2622 struct x11drv_win_data *data;
2625 if (!(data = X11DRV_get_win_data( hwnd ))) return;
2626 if (!data->whole_window) return;
2627 if (!data->managed) return;
2629 if (data->wm_hints)
2631 if (type == ICON_BIG) set_icon_hints( display, data, icon, 0 );
2632 else set_icon_hints( display, data, 0, icon );
2633 wine_tsx11_lock();
2634 XSetWMHints( display, data->whole_window, data->wm_hints );
2635 wine_tsx11_unlock();
2640 /***********************************************************************
2641 * SetWindowRgn (X11DRV.@)
2643 * Assign specified region to window (for non-rectangular windows)
2645 int CDECL X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
2647 struct x11drv_win_data *data;
2649 if ((data = X11DRV_get_win_data( hwnd )))
2651 sync_window_region( thread_display(), data, hrgn );
2653 else if (X11DRV_get_whole_window( hwnd ))
2655 SendMessageW( hwnd, WM_X11DRV_SET_WIN_REGION, 0, 0 );
2657 return TRUE;
2661 /***********************************************************************
2662 * SetLayeredWindowAttributes (X11DRV.@)
2664 * Set transparency attributes for a layered window.
2666 void CDECL X11DRV_SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
2668 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2670 if (data)
2672 if (data->whole_window)
2673 sync_window_opacity( thread_display(), data->whole_window, key, alpha, flags );
2675 else
2677 Window win = X11DRV_get_whole_window( hwnd );
2678 if (win) sync_window_opacity( gdi_display, win, key, alpha, flags );
2683 /**********************************************************************
2684 * X11DRV_WindowMessage (X11DRV.@)
2686 LRESULT CDECL X11DRV_WindowMessage( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
2688 struct x11drv_win_data *data;
2690 switch(msg)
2692 case WM_X11DRV_ACQUIRE_SELECTION:
2693 return X11DRV_AcquireClipboard( hwnd );
2694 case WM_X11DRV_SET_WIN_FORMAT:
2695 return set_win_format( hwnd, (XID)wp );
2696 case WM_X11DRV_SET_WIN_REGION:
2697 if ((data = X11DRV_get_win_data( hwnd ))) sync_window_region( thread_display(), data, (HRGN)1 );
2698 return 0;
2699 case WM_X11DRV_RESIZE_DESKTOP:
2700 X11DRV_resize_desktop( LOWORD(lp), HIWORD(lp) );
2701 return 0;
2702 case WM_X11DRV_SET_CURSOR:
2703 if ((data = X11DRV_get_win_data( hwnd )) && data->whole_window)
2705 data->cursor = (HCURSOR)lp;
2706 set_window_cursor( data->whole_window, data->cursor );
2708 return 0;
2709 default:
2710 FIXME( "got window msg %x hwnd %p wp %lx lp %lx\n", msg, hwnd, wp, lp );
2711 return 0;
2716 /***********************************************************************
2717 * is_netwm_supported
2719 static BOOL is_netwm_supported( Display *display, Atom atom )
2721 static Atom *net_supported;
2722 static int net_supported_count = -1;
2723 int i;
2725 wine_tsx11_lock();
2726 if (net_supported_count == -1)
2728 Atom type;
2729 int format;
2730 unsigned long count, remaining;
2732 if (!XGetWindowProperty( display, DefaultRootWindow(display), x11drv_atom(_NET_SUPPORTED), 0,
2733 ~0UL, False, XA_ATOM, &type, &format, &count,
2734 &remaining, (unsigned char **)&net_supported ))
2735 net_supported_count = get_property_size( format, count ) / sizeof(Atom);
2736 else
2737 net_supported_count = 0;
2739 wine_tsx11_unlock();
2741 for (i = 0; i < net_supported_count; i++)
2742 if (net_supported[i] == atom) return TRUE;
2743 return FALSE;
2747 /***********************************************************************
2748 * SysCommand (X11DRV.@)
2750 * Perform WM_SYSCOMMAND handling.
2752 LRESULT CDECL X11DRV_SysCommand( HWND hwnd, WPARAM wparam, LPARAM lparam )
2754 WPARAM hittest = wparam & 0x0f;
2755 DWORD dwPoint;
2756 int x, y, dir;
2757 XEvent xev;
2758 Display *display = thread_display();
2759 struct x11drv_win_data *data;
2761 if (!(data = X11DRV_get_win_data( hwnd ))) return -1;
2762 if (!data->whole_window || !data->managed || !data->mapped) return -1;
2764 switch (wparam & 0xfff0)
2766 case SC_MOVE:
2767 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
2768 else dir = _NET_WM_MOVERESIZE_MOVE;
2769 break;
2770 case SC_SIZE:
2771 /* windows without WS_THICKFRAME are not resizable through the window manager */
2772 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_THICKFRAME)) return -1;
2774 switch (hittest)
2776 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
2777 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
2778 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
2779 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
2780 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
2781 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
2782 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
2783 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
2784 default: dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD; break;
2786 break;
2788 case SC_KEYMENU:
2789 /* prevent a simple ALT press+release from activating the system menu,
2790 * as that can get confusing on managed windows */
2791 if ((WCHAR)lparam) return -1; /* got an explicit char */
2792 if (GetMenu( hwnd )) return -1; /* window has a real menu */
2793 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) return -1; /* no system menu */
2794 TRACE( "ignoring SC_KEYMENU wp %lx lp %lx\n", wparam, lparam );
2795 return 0;
2797 default:
2798 return -1;
2801 if (IsZoomed(hwnd)) return -1;
2803 if (!is_netwm_supported( display, x11drv_atom(_NET_WM_MOVERESIZE) ))
2805 TRACE( "_NET_WM_MOVERESIZE not supported\n" );
2806 return -1;
2809 dwPoint = GetMessagePos();
2810 x = (short)LOWORD(dwPoint);
2811 y = (short)HIWORD(dwPoint);
2813 TRACE("hwnd %p, x %d, y %d, dir %d\n", hwnd, x, y, dir);
2815 xev.xclient.type = ClientMessage;
2816 xev.xclient.window = X11DRV_get_whole_window(hwnd);
2817 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
2818 xev.xclient.serial = 0;
2819 xev.xclient.display = display;
2820 xev.xclient.send_event = True;
2821 xev.xclient.format = 32;
2822 xev.xclient.data.l[0] = x - virtual_screen_rect.left; /* x coord */
2823 xev.xclient.data.l[1] = y - virtual_screen_rect.top; /* y coord */
2824 xev.xclient.data.l[2] = dir; /* direction */
2825 xev.xclient.data.l[3] = 1; /* button */
2826 xev.xclient.data.l[4] = 0; /* unused */
2828 /* need to ungrab the pointer that may have been automatically grabbed
2829 * with a ButtonPress event */
2830 wine_tsx11_lock();
2831 XUngrabPointer( display, CurrentTime );
2832 XSendEvent(display, root_window, False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
2833 wine_tsx11_unlock();
2834 return 0;