wgl: Put the core functionality of X11DRV_SetPixelFormat in a separate function.
[wine/wine-kai.git] / dlls / winex11.drv / window.c
blob89b71500547f54fc0da606db8b4e0e2bb3b03216
1 /*
2 * Window related functions
4 * Copyright 1993, 1994, 1995, 1996, 2001 Alexandre Julliard
5 * Copyright 1993 David Metcalfe
6 * Copyright 1995, 1996 Alex Korobka
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
32 #include <X11/Xlib.h>
33 #include <X11/Xresource.h>
34 #include <X11/Xutil.h>
35 #ifdef HAVE_LIBXSHAPE
36 #include <X11/extensions/shape.h>
37 #endif /* HAVE_LIBXSHAPE */
39 #include "windef.h"
40 #include "winbase.h"
41 #include "wingdi.h"
42 #include "winuser.h"
43 #include "wine/unicode.h"
45 #include "x11drv.h"
46 #include "xcomposite.h"
47 #include "wine/debug.h"
48 #include "wine/server.h"
49 #include "mwm.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
53 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
54 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
55 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
56 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
57 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
58 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
59 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
60 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
61 #define _NET_WM_MOVERESIZE_MOVE 8
62 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9
63 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10
65 #define _NET_WM_STATE_REMOVE 0
66 #define _NET_WM_STATE_ADD 1
67 #define _NET_WM_STATE_TOGGLE 2
69 #define SWP_AGG_NOPOSCHANGE (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
71 /* X context to associate a hwnd to an X window */
72 XContext winContext = 0;
74 /* X context to associate a struct x11drv_win_data to an hwnd */
75 static XContext win_data_context;
77 static const char whole_window_prop[] = "__wine_x11_whole_window";
78 static const char client_window_prop[]= "__wine_x11_client_window";
79 static const char icon_window_prop[] = "__wine_x11_icon_window";
80 static const char fbconfig_id_prop[] = "__wine_x11_fbconfig_id";
81 static const char gl_drawable_prop[] = "__wine_x11_gl_drawable";
82 static const char pixmap_prop[] = "__wine_x11_pixmap";
83 static const char managed_prop[] = "__wine_x11_managed";
85 /***********************************************************************
86 * is_window_managed
88 * Check if a given window should be managed
90 static BOOL is_window_managed( HWND hwnd, UINT swp_flags, const RECT *window_rect )
92 DWORD style, ex_style;
94 if (!managed_mode) return FALSE;
96 /* child windows are not managed */
97 style = GetWindowLongW( hwnd, GWL_STYLE );
98 if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD) return FALSE;
99 /* activated windows are managed */
100 if (!(swp_flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) return TRUE;
101 if (hwnd == GetActiveWindow()) return TRUE;
102 /* windows with caption are managed */
103 if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
104 /* tool windows are not managed */
105 ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
106 if (ex_style & WS_EX_TOOLWINDOW) return FALSE;
107 /* windows with thick frame are managed */
108 if (style & WS_THICKFRAME) return TRUE;
109 /* application windows are managed */
110 if (ex_style & WS_EX_APPWINDOW) return TRUE;
111 if (style & WS_POPUP)
113 /* popup with sysmenu == caption are managed */
114 if (style & WS_SYSMENU) return TRUE;
115 /* full-screen popup windows are managed */
116 if (window_rect->left <= 0 && window_rect->right >= screen_width &&
117 window_rect->top <= 0 && window_rect->bottom >= screen_height)
118 return TRUE;
120 /* default: not managed */
121 return FALSE;
125 /***********************************************************************
126 * X11DRV_is_window_rect_mapped
128 * Check if the X whole window should be mapped based on its rectangle
130 static BOOL is_window_rect_mapped( const RECT *rect )
132 /* don't map if rect is off-screen */
133 if (rect->left >= virtual_screen_rect.right ||
134 rect->top >= virtual_screen_rect.bottom ||
135 rect->right <= virtual_screen_rect.left ||
136 rect->bottom <= virtual_screen_rect.top)
137 return FALSE;
139 return TRUE;
143 /***********************************************************************
144 * is_window_resizable
146 * Check if window should be made resizable by the window manager
148 static inline BOOL is_window_resizable( struct x11drv_win_data *data, DWORD style )
150 if (style & WS_THICKFRAME) return TRUE;
151 /* Metacity needs the window to be resizable to make it fullscreen */
152 return (data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
153 data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height);
157 /***********************************************************************
158 * get_mwm_decorations
160 static unsigned long get_mwm_decorations( struct x11drv_win_data *data,
161 DWORD style, DWORD ex_style )
163 unsigned long ret = 0;
165 if (!decorated_mode) return 0;
167 if (IsRectEmpty( &data->window_rect )) return 0;
169 if (ex_style & WS_EX_TOOLWINDOW) return 0;
171 if ((style & WS_CAPTION) == WS_CAPTION)
173 ret |= MWM_DECOR_TITLE | MWM_DECOR_BORDER;
174 if (style & WS_SYSMENU) ret |= MWM_DECOR_MENU;
175 if (style & WS_MINIMIZEBOX) ret |= MWM_DECOR_MINIMIZE;
176 if (style & WS_MAXIMIZEBOX) ret |= MWM_DECOR_MAXIMIZE;
178 if (ex_style & WS_EX_DLGMODALFRAME) ret |= MWM_DECOR_BORDER;
179 else if (style & WS_THICKFRAME) ret |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
180 else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) ret |= MWM_DECOR_BORDER;
181 return ret;
185 /***********************************************************************
186 * get_x11_rect_offset
188 * Helper for X11DRV_window_to_X_rect and X11DRV_X_to_window_rect.
190 static void get_x11_rect_offset( struct x11drv_win_data *data, RECT *rect )
192 DWORD style, ex_style, style_mask = 0, ex_style_mask = 0;
193 unsigned long decor;
195 rect->top = rect->bottom = rect->left = rect->right = 0;
197 style = GetWindowLongW( data->hwnd, GWL_STYLE );
198 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
199 decor = get_mwm_decorations( data, style, ex_style );
201 if (decor & MWM_DECOR_TITLE) style_mask |= WS_CAPTION;
202 if (decor & MWM_DECOR_BORDER)
204 style_mask |= WS_DLGFRAME | WS_THICKFRAME;
205 ex_style_mask |= WS_EX_DLGMODALFRAME;
208 AdjustWindowRectEx( rect, style & style_mask, FALSE, ex_style & ex_style_mask );
212 /***********************************************************************
213 * get_window_attributes
215 * Fill the window attributes structure for an X window.
217 static int get_window_attributes( Display *display, struct x11drv_win_data *data,
218 XSetWindowAttributes *attr )
220 attr->override_redirect = !data->managed;
221 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
222 attr->save_under = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
223 attr->cursor = x11drv_thread_data()->cursor;
224 attr->bit_gravity = NorthWestGravity;
225 attr->win_gravity = StaticGravity;
226 attr->backing_store = NotUseful;
227 attr->event_mask = (ExposureMask | PointerMotionMask |
228 ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
229 KeyPressMask | KeyReleaseMask | FocusChangeMask | KeymapStateMask);
230 if (data->managed) attr->event_mask |= StructureNotifyMask | PropertyChangeMask;
232 return (CWOverrideRedirect | CWSaveUnder | CWColormap | CWCursor |
233 CWEventMask | CWBitGravity | CWBackingStore);
237 /***********************************************************************
238 * create_client_window
240 static Window create_client_window( Display *display, struct x11drv_win_data *data, XVisualInfo *vis )
242 int cx, cy, mask;
243 XSetWindowAttributes attr;
244 Window client;
246 attr.bit_gravity = NorthWestGravity;
247 attr.win_gravity = NorthWestGravity;
248 attr.backing_store = NotUseful;
249 attr.event_mask = (ExposureMask | PointerMotionMask |
250 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
251 mask = CWEventMask | CWBitGravity | CWWinGravity | CWBackingStore;
253 if ((cx = data->client_rect.right - data->client_rect.left) <= 0) cx = 1;
254 if ((cy = data->client_rect.bottom - data->client_rect.top) <= 0) cy = 1;
256 wine_tsx11_lock();
258 if (vis)
260 attr.colormap = XCreateColormap( display, root_window, vis->visual,
261 (vis->class == PseudoColor || vis->class == GrayScale ||
262 vis->class == DirectColor) ? AllocAll : AllocNone );
263 mask |= CWColormap;
266 client = XCreateWindow( display, data->whole_window,
267 data->client_rect.left - data->whole_rect.left,
268 data->client_rect.top - data->whole_rect.top,
269 cx, cy, 0, screen_depth, InputOutput,
270 vis ? vis->visual : visual, mask, &attr );
271 if (!client)
273 wine_tsx11_unlock();
274 return 0;
277 if (data->client_window)
279 struct x11drv_thread_data *thread_data = x11drv_thread_data();
280 if (thread_data->cursor_window == data->client_window) thread_data->cursor_window = None;
281 XDeleteContext( display, data->client_window, winContext );
282 XDestroyWindow( display, data->client_window );
284 data->client_window = client;
286 if (data->colormap) XFreeColormap( display, data->colormap );
287 data->colormap = vis ? attr.colormap : 0;
289 XMapWindow( display, data->client_window );
290 XSaveContext( display, data->client_window, winContext, (char *)data->hwnd );
291 wine_tsx11_unlock();
293 SetPropA( data->hwnd, client_window_prop, (HANDLE)data->client_window );
294 return data->client_window;
298 /***********************************************************************
299 * sync_window_style
301 * Change the X window attributes when the window style has changed.
303 static void sync_window_style( Display *display, struct x11drv_win_data *data )
305 if (data->whole_window != root_window)
307 XSetWindowAttributes attr;
308 int mask = get_window_attributes( display, data, &attr );
310 wine_tsx11_lock();
311 XChangeWindowAttributes( display, data->whole_window, mask, &attr );
312 wine_tsx11_unlock();
317 /***********************************************************************
318 * sync_window_region
320 * Update the X11 window region.
322 static void sync_window_region( Display *display, struct x11drv_win_data *data, HRGN hrgn )
324 #ifdef HAVE_LIBXSHAPE
325 if (!data->whole_window) return;
327 if (!hrgn)
329 wine_tsx11_lock();
330 XShapeCombineMask( display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
331 wine_tsx11_unlock();
333 else
335 RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
336 if (pRegionData)
338 wine_tsx11_lock();
339 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
340 data->window_rect.left - data->whole_rect.left,
341 data->window_rect.top - data->whole_rect.top,
342 (XRectangle *)pRegionData->Buffer,
343 pRegionData->rdh.nCount, ShapeSet, YXBanded );
344 wine_tsx11_unlock();
345 HeapFree(GetProcessHeap(), 0, pRegionData);
348 #endif /* HAVE_LIBXSHAPE */
352 /***********************************************************************
353 * sync_window_text
355 static void sync_window_text( Display *display, Window win, const WCHAR *text )
357 UINT count;
358 char *buffer, *utf8_buffer;
359 XTextProperty prop;
361 /* allocate new buffer for window text */
362 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
363 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count ))) return;
364 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
366 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
367 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
369 HeapFree( GetProcessHeap(), 0, buffer );
370 return;
372 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
374 wine_tsx11_lock();
375 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
377 XSetWMName( display, win, &prop );
378 XSetWMIconName( display, win, &prop );
379 XFree( prop.value );
382 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
383 according to the standard
384 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
386 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
387 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
388 wine_tsx11_unlock();
390 HeapFree( GetProcessHeap(), 0, utf8_buffer );
391 HeapFree( GetProcessHeap(), 0, buffer );
395 /***********************************************************************
396 * X11DRV_set_win_format
398 BOOL X11DRV_set_win_format( HWND hwnd, XID fbconfig_id )
400 Display *display = thread_display();
401 struct x11drv_win_data *data;
402 XVisualInfo *vis;
403 int w, h;
405 if (!(data = X11DRV_get_win_data(hwnd)) &&
406 !(data = X11DRV_create_win_data(hwnd))) return FALSE;
408 if (data->fbconfig_id) return FALSE; /* can't change it twice */
410 wine_tsx11_lock();
411 vis = visual_from_fbconfig_id(fbconfig_id);
412 wine_tsx11_unlock();
413 if (!vis) return FALSE;
415 if (data->whole_window)
417 Window client = data->client_window;
419 if (vis->visualid != XVisualIDFromVisual(visual))
421 client = create_client_window( display, data, vis );
422 TRACE( "re-created client window %lx for %p fbconfig %lx\n", client, data->hwnd, fbconfig_id );
424 wine_tsx11_lock();
425 XFree(vis);
426 wine_tsx11_unlock();
427 if (client) goto done;
428 return FALSE;
431 w = data->client_rect.right - data->client_rect.left;
432 h = data->client_rect.bottom - data->client_rect.top;
434 if(w <= 0) w = 1;
435 if(h <= 0) h = 1;
437 #ifdef SONAME_LIBXCOMPOSITE
438 if(usexcomposite)
440 XSetWindowAttributes attrib;
441 Window parent = X11DRV_get_whole_window( GetAncestor( hwnd, GA_ROOT ));
443 if (!parent) parent = root_window;
444 wine_tsx11_lock();
445 data->colormap = XCreateColormap(display, parent, vis->visual,
446 (vis->class == PseudoColor ||
447 vis->class == GrayScale ||
448 vis->class == DirectColor) ?
449 AllocAll : AllocNone);
450 attrib.override_redirect = True;
451 attrib.colormap = data->colormap;
452 XInstallColormap(gdi_display, attrib.colormap);
454 data->gl_drawable = XCreateWindow(display, parent, -w, 0, w, h, 0,
455 vis->depth, InputOutput, vis->visual,
456 CWColormap | CWOverrideRedirect,
457 &attrib);
458 if(data->gl_drawable)
460 pXCompositeRedirectWindow(display, data->gl_drawable,
461 CompositeRedirectManual);
462 XMapWindow(display, data->gl_drawable);
464 XFree(vis);
465 wine_tsx11_unlock();
467 else
468 #endif
470 WARN("XComposite is not available, using GLXPixmap hack\n");
472 wine_tsx11_lock();
473 data->pixmap = XCreatePixmap(display, root_window, w, h, vis->depth);
474 if(!data->pixmap)
476 XFree(vis);
477 wine_tsx11_unlock();
478 return FALSE;
481 data->gl_drawable = create_glxpixmap(display, vis, data->pixmap);
482 if(!data->gl_drawable)
484 XFreePixmap(display, data->pixmap);
485 data->pixmap = 0;
487 XFree(vis);
488 wine_tsx11_unlock();
489 if (data->pixmap) SetPropA(hwnd, pixmap_prop, (HANDLE)data->pixmap);
492 if (!data->gl_drawable) return FALSE;
494 TRACE("Created GL drawable 0x%lx, using FBConfigID 0x%lx\n",
495 data->gl_drawable, fbconfig_id);
496 SetPropA(hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
498 done:
499 data->fbconfig_id = fbconfig_id;
500 SetPropA(hwnd, fbconfig_id_prop, (HANDLE)data->fbconfig_id);
501 wine_tsx11_lock();
502 XFlush( display );
503 wine_tsx11_unlock();
504 /* force DCE invalidation */
505 SetWindowPos( hwnd, 0, 0, 0, 0, 0,
506 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE |
507 SWP_NOREDRAW | SWP_NOSENDCHANGING | SWP_STATECHANGED);
508 return TRUE;
511 /***********************************************************************
512 * sync_gl_drawable
514 static void sync_gl_drawable(Display *display, struct x11drv_win_data *data)
516 int w = data->client_rect.right - data->client_rect.left;
517 int h = data->client_rect.bottom - data->client_rect.top;
518 XVisualInfo *vis;
519 Drawable glxp;
520 Pixmap pix;
522 if (w <= 0) w = 1;
523 if (h <= 0) h = 1;
525 TRACE("Resizing GL drawable 0x%lx to %dx%d\n", data->gl_drawable, w, h);
526 #ifdef SONAME_LIBXCOMPOSITE
527 if(usexcomposite)
529 wine_tsx11_lock();
530 XMoveResizeWindow(display, data->gl_drawable, -w, 0, w, h);
531 wine_tsx11_unlock();
532 return;
534 #endif
536 wine_tsx11_lock();
538 vis = visual_from_fbconfig_id(data->fbconfig_id);
539 if(!vis)
541 wine_tsx11_unlock();
542 return;
545 pix = XCreatePixmap(display, root_window, w, h, vis->depth);
546 if(!pix)
548 ERR("Failed to create pixmap for offscreen rendering\n");
549 XFree(vis);
550 wine_tsx11_unlock();
551 return;
554 glxp = create_glxpixmap(display, vis, pix);
555 if(!glxp)
557 ERR("Failed to create drawable for offscreen rendering\n");
558 XFreePixmap(display, pix);
559 XFree(vis);
560 wine_tsx11_unlock();
561 return;
564 XFree(vis);
566 mark_drawable_dirty(data->gl_drawable, glxp);
568 XFreePixmap(display, data->pixmap);
569 destroy_glxpixmap(display, data->gl_drawable);
570 TRACE( "Recreated GL drawable %lx to replace %lx\n", glxp, data->gl_drawable );
572 data->pixmap = pix;
573 data->gl_drawable = glxp;
575 XFlush( display );
576 wine_tsx11_unlock();
578 SetPropA(data->hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
579 SetPropA(data->hwnd, pixmap_prop, (HANDLE)data->pixmap);
583 /***********************************************************************
584 * get_window_changes
586 * fill the window changes structure
588 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
590 int mask = 0;
592 if (old->right - old->left != new->right - new->left )
594 if ((changes->width = new->right - new->left) <= 0) changes->width = 1;
595 mask |= CWWidth;
597 if (old->bottom - old->top != new->bottom - new->top)
599 if ((changes->height = new->bottom - new->top) <= 0) changes->height = 1;
600 mask |= CWHeight;
602 if (old->left != new->left)
604 changes->x = new->left;
605 mask |= CWX;
607 if (old->top != new->top)
609 changes->y = new->top;
610 mask |= CWY;
612 return mask;
616 /***********************************************************************
617 * create_icon_window
619 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
621 XSetWindowAttributes attr;
623 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
624 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
625 attr.bit_gravity = NorthWestGravity;
626 attr.backing_store = NotUseful/*WhenMapped*/;
627 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
629 wine_tsx11_lock();
630 data->icon_window = XCreateWindow( display, root_window, 0, 0,
631 GetSystemMetrics( SM_CXICON ),
632 GetSystemMetrics( SM_CYICON ),
633 0, screen_depth,
634 InputOutput, visual,
635 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
636 XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
637 XFlush( display ); /* make sure the window exists before we start painting to it */
638 wine_tsx11_unlock();
640 TRACE( "created %lx\n", data->icon_window );
641 SetPropA( data->hwnd, icon_window_prop, (HANDLE)data->icon_window );
642 return data->icon_window;
647 /***********************************************************************
648 * destroy_icon_window
650 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
652 if (!data->icon_window) return;
653 if (x11drv_thread_data()->cursor_window == data->icon_window)
654 x11drv_thread_data()->cursor_window = None;
655 wine_tsx11_lock();
656 XDeleteContext( display, data->icon_window, winContext );
657 XDestroyWindow( display, data->icon_window );
658 data->icon_window = 0;
659 wine_tsx11_unlock();
660 RemovePropA( data->hwnd, icon_window_prop );
664 /***********************************************************************
665 * set_icon_hints
667 * Set the icon wm hints
669 static void set_icon_hints( Display *display, struct x11drv_win_data *data, HICON hIcon )
671 XWMHints *hints = data->wm_hints;
673 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
674 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
675 data->hWMIconBitmap = 0;
676 data->hWMIconMask = 0;
678 if (!hIcon)
680 if (!data->icon_window) create_icon_window( display, data );
681 hints->icon_window = data->icon_window;
682 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
684 else
686 HBITMAP hbmOrig;
687 RECT rcMask;
688 BITMAP bmMask;
689 ICONINFO ii;
690 HDC hDC;
692 GetIconInfo(hIcon, &ii);
694 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
695 rcMask.top = 0;
696 rcMask.left = 0;
697 rcMask.right = bmMask.bmWidth;
698 rcMask.bottom = bmMask.bmHeight;
700 hDC = CreateCompatibleDC(0);
701 hbmOrig = SelectObject(hDC, ii.hbmMask);
702 InvertRect(hDC, &rcMask);
703 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
704 SelectObject(hDC, hbmOrig);
705 DeleteDC(hDC);
707 data->hWMIconBitmap = ii.hbmColor;
708 data->hWMIconMask = ii.hbmMask;
710 hints->icon_pixmap = X11DRV_get_pixmap(data->hWMIconBitmap);
711 hints->icon_mask = X11DRV_get_pixmap(data->hWMIconMask);
712 destroy_icon_window( display, data );
713 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
718 /***********************************************************************
719 * set_size_hints
721 * set the window size hints
723 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
725 XSizeHints* size_hints;
727 if (!(size_hints = XAllocSizeHints())) return;
729 size_hints->win_gravity = StaticGravity;
730 size_hints->flags |= PWinGravity;
732 /* don't update size hints if window is not in normal state */
733 if (!(style & (WS_MINIMIZE | WS_MAXIMIZE)))
735 if (data->hwnd != GetDesktopWindow()) /* don't force position of desktop */
737 size_hints->x = data->whole_rect.left;
738 size_hints->y = data->whole_rect.top;
739 size_hints->flags |= PPosition;
742 if (!is_window_resizable( data, style ))
744 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
745 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
746 size_hints->min_width = size_hints->max_width;
747 size_hints->min_height = size_hints->max_height;
748 size_hints->flags |= PMinSize | PMaxSize;
751 XSetWMNormalHints( display, data->whole_window, size_hints );
752 XFree( size_hints );
756 /***********************************************************************
757 * get_process_name
759 * get the name of the current process for setting class hints
761 static char *get_process_name(void)
763 static char *name;
765 if (!name)
767 WCHAR module[MAX_PATH];
768 DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
769 if (len && len < MAX_PATH)
771 char *ptr;
772 WCHAR *p, *appname = module;
774 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
775 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
776 len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
777 if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
779 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
780 name = ptr;
784 return name;
788 /***********************************************************************
789 * set_initial_wm_hints
791 * Set the window manager hints that don't change over the lifetime of a window.
793 static void set_initial_wm_hints( Display *display, struct x11drv_win_data *data )
795 long i;
796 Atom protocols[3];
797 Atom dndVersion = 4;
798 XClassHint *class_hints;
799 char *process_name = get_process_name();
801 wine_tsx11_lock();
803 /* wm protocols */
804 i = 0;
805 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
806 protocols[i++] = x11drv_atom(_NET_WM_PING);
807 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
808 XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
809 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
811 /* class hints */
812 if ((class_hints = XAllocClassHint()))
814 static char wine[] = "Wine";
816 class_hints->res_name = process_name;
817 class_hints->res_class = wine;
818 XSetClassHint( display, data->whole_window, class_hints );
819 XFree( class_hints );
822 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
823 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
824 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
825 i = getpid();
826 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
827 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
829 XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
830 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
832 data->wm_hints = XAllocWMHints();
833 wine_tsx11_unlock();
835 if (data->wm_hints)
837 HICON icon = (HICON)SendMessageW( data->hwnd, WM_GETICON, ICON_BIG, 0 );
838 if (!icon) icon = (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON );
839 data->wm_hints->flags = 0;
840 set_icon_hints( display, data, icon );
845 /***********************************************************************
846 * set_wm_hints
848 * Set the window manager hints for a newly-created window
850 static void set_wm_hints( Display *display, struct x11drv_win_data *data )
852 Window group_leader;
853 Atom window_type;
854 MwmHints mwm_hints;
855 DWORD style, ex_style;
856 HWND owner;
858 if (data->hwnd == GetDesktopWindow())
860 /* force some styles for the desktop to get the correct decorations */
861 style = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
862 ex_style = WS_EX_APPWINDOW;
863 owner = 0;
865 else
867 style = GetWindowLongW( data->hwnd, GWL_STYLE );
868 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
869 owner = GetWindow( data->hwnd, GW_OWNER );
872 /* transient for hint */
873 if (owner)
875 Window owner_win = X11DRV_get_whole_window( owner );
876 wine_tsx11_lock();
877 XSetTransientForHint( display, data->whole_window, owner_win );
878 wine_tsx11_unlock();
879 group_leader = owner_win;
881 else group_leader = data->whole_window;
883 wine_tsx11_lock();
885 /* size hints */
886 set_size_hints( display, data, style );
888 /* set the WM_WINDOW_TYPE */
889 if (style & WS_THICKFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
890 else if (ex_style & WS_EX_APPWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
891 else if (style & WS_DLGFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
892 else if (ex_style & WS_EX_DLGMODALFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
893 else if ((style & WS_POPUP) && owner) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
894 #if 0 /* many window managers don't handle utility windows very well */
895 else if (ex_style & WS_EX_TOOLWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
896 #endif
897 else window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
899 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
900 XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
902 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
903 mwm_hints.decorations = get_mwm_decorations( data, style, ex_style );
904 mwm_hints.functions = MWM_FUNC_MOVE;
905 if (is_window_resizable( data, style )) mwm_hints.functions |= MWM_FUNC_RESIZE;
906 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
907 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
908 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
910 XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
911 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
912 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
914 /* wm hints */
915 if (data->wm_hints)
917 data->wm_hints->flags |= InputHint | StateHint | WindowGroupHint;
918 data->wm_hints->input = !(style & WS_DISABLED);
919 data->wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
920 data->wm_hints->window_group = group_leader;
921 XSetWMHints( display, data->whole_window, data->wm_hints );
924 wine_tsx11_unlock();
928 /***********************************************************************
929 * update_net_wm_states
931 void update_net_wm_states( Display *display, struct x11drv_win_data *data )
933 static const unsigned int state_atoms[NB_NET_WM_STATES] =
935 XATOM__NET_WM_STATE_FULLSCREEN,
936 XATOM__NET_WM_STATE_ABOVE,
937 XATOM__NET_WM_STATE_MAXIMIZED_VERT,
938 XATOM__NET_WM_STATE_SKIP_PAGER,
939 XATOM__NET_WM_STATE_SKIP_TASKBAR
942 DWORD i, style, ex_style, new_state = 0;
944 if (!data->managed) return;
945 if (data->whole_window == root_window) return;
947 style = GetWindowLongW( data->hwnd, GWL_STYLE );
948 if (data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
949 data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height)
951 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION)
952 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
953 else if (!(style & WS_MINIMIZE))
954 new_state |= (1 << NET_WM_STATE_FULLSCREEN);
956 else if (style & WS_MAXIMIZE)
957 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
959 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
960 if (ex_style & WS_EX_TOPMOST)
961 new_state |= (1 << NET_WM_STATE_ABOVE);
962 if (ex_style & WS_EX_TOOLWINDOW)
963 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR) | (1 << NET_WM_STATE_SKIP_PAGER);
964 if (!(ex_style & WS_EX_APPWINDOW) && GetWindow( data->hwnd, GW_OWNER ))
965 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR);
967 if (!data->mapped) /* set the _NET_WM_STATE atom directly */
969 Atom atoms[NB_NET_WM_STATES+1];
970 DWORD count;
972 for (i = count = 0; i < NB_NET_WM_STATES; i++)
974 if (!(new_state & (1 << i))) continue;
975 TRACE( "setting wm state %u for unmapped window %p/%lx\n",
976 i, data->hwnd, data->whole_window );
977 atoms[count++] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
978 if (state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT)
979 atoms[count++] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
981 wine_tsx11_lock();
982 XChangeProperty( display, data->whole_window, x11drv_atom(_NET_WM_STATE), XA_ATOM,
983 32, PropModeReplace, (unsigned char *)atoms, count );
984 wine_tsx11_unlock();
986 else /* ask the window manager to do it for us */
988 XEvent xev;
990 xev.xclient.type = ClientMessage;
991 xev.xclient.window = data->whole_window;
992 xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
993 xev.xclient.serial = 0;
994 xev.xclient.display = display;
995 xev.xclient.send_event = True;
996 xev.xclient.format = 32;
997 xev.xclient.data.l[3] = 1;
999 for (i = 0; i < NB_NET_WM_STATES; i++)
1001 if (!((data->net_wm_state ^ new_state) & (1 << i))) continue; /* unchanged */
1003 TRACE( "setting wm state %u for window %p/%lx to %u prev %u\n",
1004 i, data->hwnd, data->whole_window,
1005 (new_state & (1 << i)) != 0, (data->net_wm_state & (1 << i)) != 0 );
1007 xev.xclient.data.l[0] = (new_state & (1 << i)) ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1008 xev.xclient.data.l[1] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1009 xev.xclient.data.l[2] = ((state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT) ?
1010 x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ) : 0);
1011 wine_tsx11_lock();
1012 XSendEvent( display, root_window, False,
1013 SubstructureRedirectMask | SubstructureNotifyMask, &xev );
1014 wine_tsx11_unlock();
1017 data->net_wm_state = new_state;
1021 /***********************************************************************
1022 * set_xembed_flags
1024 static void set_xembed_flags( Display *display, struct x11drv_win_data *data, unsigned long flags )
1026 unsigned long info[2];
1028 info[0] = 0; /* protocol version */
1029 info[1] = flags;
1030 wine_tsx11_lock();
1031 XChangeProperty( display, data->whole_window, x11drv_atom(_XEMBED_INFO),
1032 x11drv_atom(_XEMBED_INFO), 32, PropModeReplace, (unsigned char*)info, 2 );
1033 wine_tsx11_unlock();
1037 /***********************************************************************
1038 * map_window
1040 static void map_window( Display *display, struct x11drv_win_data *data, DWORD new_style )
1042 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1044 wait_for_withdrawn_state( display, data, TRUE );
1046 if (!data->embedded)
1048 update_net_wm_states( display, data );
1049 sync_window_style( display, data );
1050 wine_tsx11_lock();
1051 XMapWindow( display, data->whole_window );
1052 wine_tsx11_unlock();
1054 else set_xembed_flags( display, data, XEMBED_MAPPED );
1056 data->mapped = TRUE;
1057 data->iconic = (new_style & WS_MINIMIZE) != 0;
1061 /***********************************************************************
1062 * unmap_window
1064 static void unmap_window( Display *display, struct x11drv_win_data *data )
1066 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1068 if (!data->embedded)
1070 wait_for_withdrawn_state( display, data, FALSE );
1071 wine_tsx11_lock();
1072 if (data->managed) XWithdrawWindow( display, data->whole_window, DefaultScreen(display) );
1073 else XUnmapWindow( display, data->whole_window );
1074 wine_tsx11_unlock();
1076 else set_xembed_flags( display, data, 0 );
1078 data->mapped = FALSE;
1079 data->net_wm_state = 0;
1083 /***********************************************************************
1084 * make_window_embedded
1086 void make_window_embedded( Display *display, struct x11drv_win_data *data )
1088 if (data->mapped)
1090 /* the window cannot be mapped before being embedded */
1091 unmap_window( display, data );
1092 data->embedded = TRUE;
1093 map_window( display, data, 0 );
1095 else
1097 data->embedded = TRUE;
1098 set_xembed_flags( display, data, 0 );
1103 /***********************************************************************
1104 * X11DRV_window_to_X_rect
1106 * Convert a rect from client to X window coordinates
1108 void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
1110 RECT rc;
1112 if (!data->managed) return;
1113 if (IsRectEmpty( rect )) return;
1115 get_x11_rect_offset( data, &rc );
1117 rect->left -= rc.left;
1118 rect->right -= rc.right;
1119 rect->top -= rc.top;
1120 rect->bottom -= rc.bottom;
1121 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1122 if (rect->left >= rect->right) rect->right = rect->left + 1;
1126 /***********************************************************************
1127 * X11DRV_X_to_window_rect
1129 * Opposite of X11DRV_window_to_X_rect
1131 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
1133 RECT rc;
1135 if (!data->managed) return;
1136 if (IsRectEmpty( rect )) return;
1138 get_x11_rect_offset( data, &rc );
1140 rect->left += rc.left;
1141 rect->right += rc.right;
1142 rect->top += rc.top;
1143 rect->bottom += rc.bottom;
1144 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1145 if (rect->left >= rect->right) rect->right = rect->left + 1;
1149 /***********************************************************************
1150 * sync_window_position
1152 * Synchronize the X window position with the Windows one
1154 static void sync_window_position( Display *display, struct x11drv_win_data *data,
1155 UINT swp_flags, const RECT *old_client_rect,
1156 const RECT *old_whole_rect )
1158 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
1159 XWindowChanges changes;
1160 unsigned int mask = 0;
1162 if (data->managed && data->iconic) return;
1164 /* resizing a managed maximized window is not allowed */
1165 if (!(style & WS_MAXIMIZE) || !data->managed)
1167 if ((changes.width = data->whole_rect.right - data->whole_rect.left) <= 0) changes.width = 1;
1168 if ((changes.height = data->whole_rect.bottom - data->whole_rect.top) <= 0) changes.height = 1;
1169 mask |= CWWidth | CWHeight;
1172 /* only the size is allowed to change for the desktop window */
1173 if (data->whole_window != root_window)
1175 changes.x = data->whole_rect.left - virtual_screen_rect.left;
1176 changes.y = data->whole_rect.top - virtual_screen_rect.top;
1177 mask |= CWX | CWY;
1180 if (!(swp_flags & SWP_NOZORDER))
1182 /* find window that this one must be after */
1183 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
1184 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
1185 prev = GetWindow( prev, GW_HWNDPREV );
1186 if (!prev) /* top child */
1188 changes.stack_mode = Above;
1189 mask |= CWStackMode;
1191 /* should use stack_mode Below but most window managers don't get it right */
1192 /* and Above with a sibling doesn't work so well either, so we ignore it */
1195 TRACE( "setting win %p/%lx pos %d,%d,%dx%d after %lx changes=%x\n",
1196 data->hwnd, data->whole_window, data->whole_rect.left, data->whole_rect.top,
1197 data->whole_rect.right - data->whole_rect.left,
1198 data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
1200 wine_tsx11_lock();
1201 set_size_hints( display, data, style );
1202 XReconfigureWMWindow( display, data->whole_window,
1203 DefaultScreen(display), mask, &changes );
1204 wine_tsx11_unlock();
1208 /***********************************************************************
1209 * sync_client_position
1211 * Synchronize the X client window position with the Windows one
1213 static void sync_client_position( Display *display, struct x11drv_win_data *data,
1214 UINT swp_flags, const RECT *old_client_rect,
1215 const RECT *old_whole_rect )
1217 int mask;
1218 XWindowChanges changes;
1219 RECT old = *old_client_rect;
1220 RECT new = data->client_rect;
1222 OffsetRect( &old, -old_whole_rect->left, -old_whole_rect->top );
1223 OffsetRect( &new, -data->whole_rect.left, -data->whole_rect.top );
1224 if (!(mask = get_window_changes( &changes, &old, &new ))) return;
1226 if (data->client_window)
1228 TRACE( "setting client win %lx pos %d,%d,%dx%d changes=%x\n",
1229 data->client_window, new.left, new.top,
1230 new.right - new.left, new.bottom - new.top, mask );
1231 wine_tsx11_lock();
1232 XConfigureWindow( display, data->client_window, mask, &changes );
1233 wine_tsx11_unlock();
1236 if (data->gl_drawable && (mask & (CWWidth|CWHeight))) sync_gl_drawable( display, data );
1240 /***********************************************************************
1241 * move_window_bits
1243 * Move the window bits when a window is moved.
1245 static void move_window_bits( struct x11drv_win_data *data, const RECT *old_rect, const RECT *new_rect,
1246 const RECT *old_client_rect )
1248 RECT src_rect = *old_rect;
1249 RECT dst_rect = *new_rect;
1250 HDC hdc_src, hdc_dst;
1251 INT code;
1252 HRGN rgn = 0;
1253 HWND parent = 0;
1255 if (!data->whole_window)
1257 OffsetRect( &dst_rect, -data->window_rect.left, -data->window_rect.top );
1258 parent = GetAncestor( data->hwnd, GA_PARENT );
1259 hdc_src = GetDCEx( parent, 0, DCX_CACHE );
1260 hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE | DCX_WINDOW );
1262 else
1264 OffsetRect( &dst_rect, -data->client_rect.left, -data->client_rect.top );
1265 /* make src rect relative to the old position of the window */
1266 OffsetRect( &src_rect, -old_client_rect->left, -old_client_rect->top );
1267 if (dst_rect.left == src_rect.left && dst_rect.top == src_rect.top) return;
1268 hdc_src = hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE );
1271 code = X11DRV_START_EXPOSURES;
1272 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
1274 TRACE( "copying bits for win %p/%lx/%lx %s -> %s\n",
1275 data->hwnd, data->whole_window, data->client_window,
1276 wine_dbgstr_rect(&src_rect), wine_dbgstr_rect(&dst_rect) );
1277 BitBlt( hdc_dst, dst_rect.left, dst_rect.top,
1278 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top,
1279 hdc_src, src_rect.left, src_rect.top, SRCCOPY );
1281 code = X11DRV_END_EXPOSURES;
1282 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, sizeof(rgn), (LPSTR)&rgn );
1284 ReleaseDC( data->hwnd, hdc_dst );
1285 if (hdc_src != hdc_dst) ReleaseDC( parent, hdc_src );
1287 if (rgn)
1289 if (!data->whole_window)
1291 /* map region to client rect since we are using DCX_WINDOW */
1292 OffsetRgn( rgn, data->window_rect.left - data->client_rect.left,
1293 data->window_rect.top - data->client_rect.top );
1294 RedrawWindow( data->hwnd, NULL, rgn,
1295 RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ALLCHILDREN );
1297 else RedrawWindow( data->hwnd, NULL, rgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1298 DeleteObject( rgn );
1303 /**********************************************************************
1304 * create_whole_window
1306 * Create the whole X window for a given window
1308 static Window create_whole_window( Display *display, struct x11drv_win_data *data )
1310 int cx, cy, mask;
1311 XSetWindowAttributes attr;
1312 WCHAR text[1024];
1313 HRGN hrgn;
1315 if (!(cx = data->window_rect.right - data->window_rect.left)) cx = 1;
1316 if (!(cy = data->window_rect.bottom - data->window_rect.top)) cy = 1;
1318 if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1320 TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1321 data->managed = TRUE;
1322 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1325 mask = get_window_attributes( display, data, &attr );
1327 wine_tsx11_lock();
1329 data->whole_rect = data->window_rect;
1330 data->whole_window = XCreateWindow( display, root_window,
1331 data->window_rect.left - virtual_screen_rect.left,
1332 data->window_rect.top - virtual_screen_rect.top,
1333 cx, cy, 0, screen_depth, InputOutput,
1334 visual, mask, &attr );
1336 if (data->whole_window) XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
1337 wine_tsx11_unlock();
1339 if (!data->whole_window) return 0;
1341 if (!create_client_window( display, data, NULL ))
1343 wine_tsx11_lock();
1344 XDeleteContext( display, data->whole_window, winContext );
1345 XDestroyWindow( display, data->whole_window );
1346 data->whole_window = 0;
1347 wine_tsx11_unlock();
1348 return 0;
1351 set_initial_wm_hints( display, data );
1352 set_wm_hints( display, data );
1354 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1356 /* set the window text */
1357 if (!InternalGetWindowText( data->hwnd, text, sizeof(text)/sizeof(WCHAR) )) text[0] = 0;
1358 sync_window_text( display, data->whole_window, text );
1360 /* set the window region */
1361 if ((hrgn = CreateRectRgn( 0, 0, 0, 0 )))
1363 if (GetWindowRgn( data->hwnd, hrgn ) != ERROR) sync_window_region( display, data, hrgn );
1364 DeleteObject( hrgn );
1366 wine_tsx11_lock();
1367 XFlush( display ); /* make sure the window exists before we start painting to it */
1368 wine_tsx11_unlock();
1369 return data->whole_window;
1373 /**********************************************************************
1374 * destroy_whole_window
1376 * Destroy the whole X window for a given window.
1378 static void destroy_whole_window( Display *display, struct x11drv_win_data *data, BOOL already_destroyed )
1380 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1382 if (!data->whole_window) return;
1384 TRACE( "win %p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1385 if (thread_data->cursor_window == data->whole_window ||
1386 thread_data->cursor_window == data->client_window)
1387 thread_data->cursor_window = None;
1388 wine_tsx11_lock();
1389 XDeleteContext( display, data->whole_window, winContext );
1390 XDeleteContext( display, data->client_window, winContext );
1391 if (!already_destroyed) XDestroyWindow( display, data->whole_window );
1392 data->whole_window = data->client_window = 0;
1393 data->wm_state = WithdrawnState;
1394 data->net_wm_state = 0;
1395 data->mapped = FALSE;
1396 if (data->xic)
1398 XUnsetICFocus( data->xic );
1399 XDestroyIC( data->xic );
1400 data->xic = 0;
1402 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1403 XFlush( display );
1404 XFree( data->wm_hints );
1405 data->wm_hints = NULL;
1406 wine_tsx11_unlock();
1407 RemovePropA( data->hwnd, whole_window_prop );
1408 RemovePropA( data->hwnd, client_window_prop );
1412 /*****************************************************************
1413 * SetWindowText (X11DRV.@)
1415 void X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1417 Display *display = thread_display();
1418 Window win;
1420 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(display))
1421 sync_window_text( display, win, text );
1425 /***********************************************************************
1426 * SetWindowStyle (X11DRV.@)
1428 * Update the X state of a window to reflect a style change
1430 void X11DRV_SetWindowStyle( HWND hwnd, DWORD old_style )
1432 Display *display = thread_display();
1433 struct x11drv_win_data *data;
1434 DWORD new_style, changed;
1436 if (hwnd == GetDesktopWindow()) return;
1437 new_style = GetWindowLongW( hwnd, GWL_STYLE );
1438 changed = new_style ^ old_style;
1440 if ((changed & WS_VISIBLE) && (new_style & WS_VISIBLE))
1442 /* we don't unmap windows, that causes trouble with the window manager */
1443 if (!(data = X11DRV_get_win_data( hwnd )) &&
1444 !(data = X11DRV_create_win_data( hwnd ))) return;
1446 if (data->whole_window && is_window_rect_mapped( &data->window_rect ))
1448 set_wm_hints( display, data );
1449 if (!data->mapped) map_window( display, data, new_style );
1453 if (changed & WS_DISABLED)
1455 data = X11DRV_get_win_data( hwnd );
1456 if (data && data->wm_hints)
1458 wine_tsx11_lock();
1459 data->wm_hints->input = !(new_style & WS_DISABLED);
1460 XSetWMHints( display, data->whole_window, data->wm_hints );
1461 wine_tsx11_unlock();
1467 /***********************************************************************
1468 * DestroyWindow (X11DRV.@)
1470 void X11DRV_DestroyWindow( HWND hwnd )
1472 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1473 Display *display = thread_data->display;
1474 struct x11drv_win_data *data;
1476 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1478 if (data->pixmap)
1480 destroy_glxpixmap(display, data->gl_drawable);
1481 wine_tsx11_lock();
1482 XFreePixmap(display, data->pixmap);
1483 wine_tsx11_unlock();
1485 else if (data->gl_drawable)
1487 wine_tsx11_lock();
1488 XDestroyWindow(display, data->gl_drawable);
1489 wine_tsx11_unlock();
1492 destroy_whole_window( display, data, FALSE );
1493 destroy_icon_window( display, data );
1495 if (data->colormap)
1497 wine_tsx11_lock();
1498 XFreeColormap( display, data->colormap );
1499 wine_tsx11_unlock();
1502 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1503 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
1504 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
1505 wine_tsx11_lock();
1506 XDeleteContext( display, (XID)hwnd, win_data_context );
1507 wine_tsx11_unlock();
1508 HeapFree( GetProcessHeap(), 0, data );
1512 /***********************************************************************
1513 * X11DRV_DestroyNotify
1515 void X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1517 Display *display = event->xdestroywindow.display;
1518 struct x11drv_win_data *data;
1520 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1522 FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1523 destroy_whole_window( display, data, TRUE );
1527 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
1529 struct x11drv_win_data *data;
1531 if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
1533 data->hwnd = hwnd;
1534 wine_tsx11_lock();
1535 if (!winContext) winContext = XUniqueContext();
1536 if (!win_data_context) win_data_context = XUniqueContext();
1537 XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
1538 wine_tsx11_unlock();
1540 return data;
1544 /* initialize the desktop window id in the desktop manager process */
1545 static struct x11drv_win_data *create_desktop_win_data( Display *display, HWND hwnd )
1547 struct x11drv_win_data *data;
1548 VisualID visualid;
1550 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1551 wine_tsx11_lock();
1552 visualid = XVisualIDFromVisual(visual);
1553 wine_tsx11_unlock();
1554 data->whole_window = data->client_window = root_window;
1555 data->managed = TRUE;
1556 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1557 SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
1558 SetPropA( data->hwnd, client_window_prop, (HANDLE)root_window );
1559 set_initial_wm_hints( display, data );
1560 return data;
1563 /**********************************************************************
1564 * CreateDesktopWindow (X11DRV.@)
1566 BOOL X11DRV_CreateDesktopWindow( HWND hwnd )
1568 unsigned int width, height;
1570 /* retrieve the real size of the desktop */
1571 SERVER_START_REQ( get_window_rectangles )
1573 req->handle = hwnd;
1574 wine_server_call( req );
1575 width = reply->window.right - reply->window.left;
1576 height = reply->window.bottom - reply->window.top;
1578 SERVER_END_REQ;
1580 if (!width && !height) /* not initialized yet */
1582 SERVER_START_REQ( set_window_pos )
1584 req->handle = hwnd;
1585 req->previous = 0;
1586 req->flags = SWP_NOZORDER;
1587 req->window.left = virtual_screen_rect.left;
1588 req->window.top = virtual_screen_rect.top;
1589 req->window.right = virtual_screen_rect.right;
1590 req->window.bottom = virtual_screen_rect.bottom;
1591 req->client = req->window;
1592 wine_server_call( req );
1594 SERVER_END_REQ;
1596 else
1598 Window win = (Window)GetPropA( hwnd, whole_window_prop );
1599 if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1601 return TRUE;
1605 /**********************************************************************
1606 * CreateWindow (X11DRV.@)
1608 BOOL X11DRV_CreateWindow( HWND hwnd )
1610 Display *display = thread_display();
1612 if (hwnd == GetDesktopWindow() && root_window != DefaultRootWindow( display ))
1614 /* the desktop win data can't be created lazily */
1615 if (!create_desktop_win_data( display, hwnd )) return FALSE;
1617 return TRUE;
1621 /***********************************************************************
1622 * X11DRV_get_win_data
1624 * Return the X11 data structure associated with a window.
1626 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1628 char *data;
1630 if (!hwnd || XFindContext( thread_display(), (XID)hwnd, win_data_context, &data )) data = NULL;
1631 return (struct x11drv_win_data *)data;
1635 /***********************************************************************
1636 * X11DRV_create_win_data
1638 * Create an X11 data window structure for an existing window.
1640 struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd )
1642 Display *display = thread_display();
1643 struct x11drv_win_data *data;
1644 HWND parent;
1646 if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL; /* desktop */
1647 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1649 GetWindowRect( hwnd, &data->window_rect );
1650 MapWindowPoints( 0, parent, (POINT *)&data->window_rect, 2 );
1651 data->whole_rect = data->window_rect;
1652 GetClientRect( hwnd, &data->client_rect );
1653 MapWindowPoints( hwnd, parent, (POINT *)&data->client_rect, 2 );
1655 if (parent == GetDesktopWindow())
1657 if (!create_whole_window( display, data ))
1659 HeapFree( GetProcessHeap(), 0, data );
1660 return NULL;
1662 TRACE( "win %p/%lx/%lx window %s whole %s client %s\n",
1663 hwnd, data->whole_window, data->client_window, wine_dbgstr_rect( &data->window_rect ),
1664 wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
1666 return data;
1670 /***********************************************************************
1671 * X11DRV_get_whole_window
1673 * Return the X window associated with the full area of a window
1675 Window X11DRV_get_whole_window( HWND hwnd )
1677 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1679 if (!data)
1681 if (hwnd == GetDesktopWindow()) return root_window;
1682 return (Window)GetPropA( hwnd, whole_window_prop );
1684 return data->whole_window;
1688 /***********************************************************************
1689 * X11DRV_get_client_window
1691 * Return the X window associated with the client area of a window
1693 Window X11DRV_get_client_window( HWND hwnd )
1695 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1697 if (!data)
1699 if (hwnd == GetDesktopWindow()) return root_window;
1700 return (Window)GetPropA( hwnd, client_window_prop );
1702 return data->client_window;
1706 /***********************************************************************
1707 * X11DRV_get_ic
1709 * Return the X input context associated with a window
1711 XIC X11DRV_get_ic( HWND hwnd )
1713 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1714 XIM xim;
1716 if (!data) return 0;
1717 if (data->xic) return data->xic;
1718 if (!(xim = x11drv_thread_data()->xim)) return 0;
1719 return X11DRV_CreateIC( xim, data );
1723 /***********************************************************************
1724 * X11DRV_GetDC (X11DRV.@)
1726 void X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
1727 const RECT *top_rect, DWORD flags )
1729 struct x11drv_escape_set_drawable escape;
1730 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1732 escape.code = X11DRV_SET_DRAWABLE;
1733 escape.mode = IncludeInferiors;
1734 escape.fbconfig_id = 0;
1735 escape.gl_drawable = 0;
1736 escape.pixmap = 0;
1738 if (top == hwnd && data && IsIconic( hwnd ) && data->icon_window)
1740 escape.drawable = data->icon_window;
1742 else if (top == hwnd && (flags & DCX_WINDOW))
1744 escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
1746 else
1748 escape.drawable = X11DRV_get_client_window( top );
1749 escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
1750 escape.gl_drawable = data ? data->gl_drawable : (Drawable)GetPropA( hwnd, gl_drawable_prop );
1751 escape.pixmap = data ? data->pixmap : (Pixmap)GetPropA( hwnd, pixmap_prop );
1752 if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
1755 escape.dc_rect.left = win_rect->left - top_rect->left;
1756 escape.dc_rect.top = win_rect->top - top_rect->top;
1757 escape.dc_rect.right = win_rect->right - top_rect->left;
1758 escape.dc_rect.bottom = win_rect->bottom - top_rect->top;
1759 escape.drawable_rect.left = top_rect->left;
1760 escape.drawable_rect.top = top_rect->top;
1761 escape.drawable_rect.right = top_rect->right;
1762 escape.drawable_rect.bottom = top_rect->bottom;
1764 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1768 /***********************************************************************
1769 * X11DRV_ReleaseDC (X11DRV.@)
1771 void X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
1773 struct x11drv_escape_set_drawable escape;
1775 escape.code = X11DRV_SET_DRAWABLE;
1776 escape.drawable = root_window;
1777 escape.mode = IncludeInferiors;
1778 escape.drawable_rect = virtual_screen_rect;
1779 SetRect( &escape.dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
1780 virtual_screen_rect.bottom - virtual_screen_rect.top );
1781 escape.fbconfig_id = 0;
1782 escape.gl_drawable = 0;
1783 escape.pixmap = 0;
1784 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1788 /***********************************************************************
1789 * SetCapture (X11DRV.@)
1791 void X11DRV_SetCapture( HWND hwnd, UINT flags )
1793 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1795 if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
1797 if (hwnd)
1799 Window grab_win = X11DRV_get_client_window( GetAncestor( hwnd, GA_ROOT ) );
1801 if (!grab_win) return;
1802 wine_tsx11_lock();
1803 XFlush( gdi_display );
1804 XGrabPointer( thread_data->display, grab_win, False,
1805 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1806 GrabModeAsync, GrabModeAsync, None, None, CurrentTime );
1807 wine_tsx11_unlock();
1808 thread_data->grab_window = grab_win;
1810 else /* release capture */
1812 wine_tsx11_lock();
1813 XFlush( gdi_display );
1814 XUngrabPointer( thread_data->display, CurrentTime );
1815 wine_tsx11_unlock();
1816 thread_data->grab_window = None;
1821 /*****************************************************************
1822 * SetParent (X11DRV.@)
1824 void X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
1826 Display *display = thread_display();
1827 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1829 if (!data) return;
1830 if (parent == old_parent) return;
1832 if (parent != GetDesktopWindow()) /* a child window */
1834 if (old_parent == GetDesktopWindow())
1836 /* destroy the old X windows */
1837 destroy_whole_window( display, data, FALSE );
1838 destroy_icon_window( display, data );
1839 if (data->managed)
1841 data->managed = FALSE;
1842 RemovePropA( data->hwnd, managed_prop );
1846 else /* new top level window */
1848 /* FIXME: we ignore errors since we can't really recover anyway */
1849 create_whole_window( display, data );
1854 /*****************************************************************
1855 * SetFocus (X11DRV.@)
1857 * Set the X focus.
1858 * Explicit colormap management seems to work only with OLVWM.
1860 void X11DRV_SetFocus( HWND hwnd )
1862 Display *display = thread_display();
1863 struct x11drv_win_data *data;
1864 XWindowChanges changes;
1866 /* If setting the focus to 0, uninstall the colormap */
1867 if (!hwnd && root_window == DefaultRootWindow(display))
1869 wine_tsx11_lock();
1870 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1871 XUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1872 wine_tsx11_unlock();
1873 return;
1876 hwnd = GetAncestor( hwnd, GA_ROOT );
1878 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1879 if (data->managed || !data->whole_window) return;
1881 /* Set X focus and install colormap */
1882 wine_tsx11_lock();
1883 changes.stack_mode = Above;
1884 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
1885 if (root_window == DefaultRootWindow(display))
1887 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1888 /* FIXME: this is not entirely correct */
1889 XSetInputFocus( display, data->whole_window, RevertToParent,
1890 /* CurrentTime */
1891 GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1892 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1893 XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1895 wine_tsx11_unlock();
1899 /***********************************************************************
1900 * SetWindowPos (X11DRV.@)
1902 void X11DRV_SetWindowPos( HWND hwnd, HWND insert_after, UINT swp_flags,
1903 const RECT *rectWindow, const RECT *rectClient,
1904 const RECT *visible_rect, const RECT *valid_rects )
1906 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1907 Display *display = thread_data->display;
1908 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1909 DWORD new_style = GetWindowLongW( hwnd, GWL_STYLE );
1910 RECT old_window_rect, old_whole_rect, old_client_rect;
1911 int event_type;
1913 if (!data)
1915 /* create the win data if the window is being made visible */
1916 if (!(new_style & WS_VISIBLE)) return;
1917 if (!(data = X11DRV_create_win_data( hwnd ))) return;
1920 /* check if we need to switch the window to managed */
1921 if (!data->managed && data->whole_window && is_window_managed( hwnd, swp_flags, rectWindow ))
1923 TRACE( "making win %p/%lx managed\n", hwnd, data->whole_window );
1924 if (data->mapped) unmap_window( display, data );
1925 data->managed = TRUE;
1926 SetPropA( hwnd, managed_prop, (HANDLE)1 );
1929 old_window_rect = data->window_rect;
1930 old_whole_rect = data->whole_rect;
1931 old_client_rect = data->client_rect;
1932 data->window_rect = *rectWindow;
1933 data->whole_rect = *rectWindow;
1934 data->client_rect = *rectClient;
1935 X11DRV_window_to_X_rect( data, &data->whole_rect );
1936 if (memcmp( visible_rect, &data->whole_rect, sizeof(RECT) ))
1938 TRACE( "%p: need to update visible rect %s -> %s\n", hwnd,
1939 wine_dbgstr_rect(visible_rect), wine_dbgstr_rect(&data->whole_rect) );
1940 SERVER_START_REQ( set_window_visible_rect )
1942 req->handle = hwnd;
1943 req->flags = swp_flags;
1944 req->visible.left = data->whole_rect.left;
1945 req->visible.top = data->whole_rect.top;
1946 req->visible.right = data->whole_rect.right;
1947 req->visible.bottom = data->whole_rect.bottom;
1948 wine_server_call( req );
1950 SERVER_END_REQ;
1953 TRACE( "win %p window %s client %s style %08x flags %08x\n",
1954 hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style, swp_flags );
1956 if (!IsRectEmpty( &valid_rects[0] ))
1958 int x_offset = old_whole_rect.left - data->whole_rect.left;
1959 int y_offset = old_whole_rect.top - data->whole_rect.top;
1961 /* if all that happened is that the whole window moved, copy everything */
1962 if (!(swp_flags & SWP_FRAMECHANGED) &&
1963 old_whole_rect.right - data->whole_rect.right == x_offset &&
1964 old_whole_rect.bottom - data->whole_rect.bottom == y_offset &&
1965 old_client_rect.left - data->client_rect.left == x_offset &&
1966 old_client_rect.right - data->client_rect.right == x_offset &&
1967 old_client_rect.top - data->client_rect.top == y_offset &&
1968 old_client_rect.bottom - data->client_rect.bottom == y_offset &&
1969 !memcmp( &valid_rects[0], &data->client_rect, sizeof(RECT) ))
1971 /* if we have an X window the bits will be moved by the X server */
1972 if (!data->whole_window)
1973 move_window_bits( data, &old_whole_rect, &data->whole_rect, &old_client_rect );
1975 else
1976 move_window_bits( data, &valid_rects[1], &valid_rects[0], &old_client_rect );
1979 wine_tsx11_lock();
1980 XFlush( gdi_display ); /* make sure painting is done before we move the window */
1981 wine_tsx11_unlock();
1983 sync_client_position( display, data, swp_flags, &old_client_rect, &old_whole_rect );
1985 if (!data->whole_window) return;
1987 /* check if we are currently processing an event relevant to this window */
1988 event_type = 0;
1989 if (thread_data->current_event && thread_data->current_event->xany.window == data->whole_window)
1990 event_type = thread_data->current_event->type;
1992 if (event_type != ConfigureNotify && event_type != PropertyNotify)
1993 event_type = 0; /* ignore other events */
1995 if (data->mapped && (!(new_style & WS_VISIBLE) ||
1996 (!event_type && !is_window_rect_mapped( rectWindow ))))
1997 unmap_window( display, data );
1999 /* don't change position if we are about to minimize or maximize a managed window */
2000 if (!event_type &&
2001 !(data->managed && (swp_flags & SWP_STATECHANGED) && (new_style & (WS_MINIMIZE|WS_MAXIMIZE))))
2002 sync_window_position( display, data, swp_flags, &old_client_rect, &old_whole_rect );
2004 if ((new_style & WS_VISIBLE) &&
2005 ((new_style & WS_MINIMIZE) || is_window_rect_mapped( rectWindow )))
2007 if (!data->mapped || (swp_flags & (SWP_FRAMECHANGED|SWP_STATECHANGED)))
2008 set_wm_hints( display, data );
2010 if (!data->mapped)
2012 map_window( display, data, new_style );
2014 else if ((swp_flags & SWP_STATECHANGED) && (!data->iconic != !(new_style & WS_MINIMIZE)))
2016 data->iconic = (new_style & WS_MINIMIZE) != 0;
2017 TRACE( "changing win %p iconic state to %u\n", data->hwnd, data->iconic );
2018 wine_tsx11_lock();
2019 if (data->iconic)
2020 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
2021 else if (is_window_rect_mapped( rectWindow ))
2022 XMapWindow( display, data->whole_window );
2023 wine_tsx11_unlock();
2024 update_net_wm_states( display, data );
2026 else if (!event_type)
2028 update_net_wm_states( display, data );
2032 wine_tsx11_lock();
2033 XFlush( display ); /* make sure changes are done before we start painting again */
2034 wine_tsx11_unlock();
2038 /**********************************************************************
2039 * SetWindowIcon (X11DRV.@)
2041 * hIcon or hIconSm has changed (or is being initialised for the
2042 * first time). Complete the X11 driver-specific initialisation
2043 * and set the window hints.
2045 * This is not entirely correct, may need to create
2046 * an icon window and set the pixmap as a background
2048 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
2050 Display *display = thread_display();
2051 struct x11drv_win_data *data;
2053 if (type != ICON_BIG) return; /* nothing to do here */
2055 if (!(data = X11DRV_get_win_data( hwnd ))) return;
2056 if (!data->whole_window) return;
2057 if (!data->managed) return;
2059 if (data->wm_hints)
2061 set_icon_hints( display, data, icon );
2062 wine_tsx11_lock();
2063 XSetWMHints( display, data->whole_window, data->wm_hints );
2064 wine_tsx11_unlock();
2069 /***********************************************************************
2070 * SetWindowRgn (X11DRV.@)
2072 * Assign specified region to window (for non-rectangular windows)
2074 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
2076 struct x11drv_win_data *data;
2078 if ((data = X11DRV_get_win_data( hwnd )))
2080 sync_window_region( thread_display(), data, hrgn );
2082 else if (GetWindowThreadProcessId( hwnd, NULL ) != GetCurrentThreadId())
2084 FIXME( "not supported on other thread window %p\n", hwnd );
2085 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2086 return FALSE;
2089 return TRUE;
2093 /***********************************************************************
2094 * is_netwm_supported
2096 static BOOL is_netwm_supported( Display *display, Atom atom )
2098 static Atom *net_supported;
2099 static int net_supported_count = -1;
2100 int i;
2102 wine_tsx11_lock();
2103 if (net_supported_count == -1)
2105 Atom type;
2106 int format;
2107 unsigned long count, remaining;
2109 if (!XGetWindowProperty( display, DefaultRootWindow(display), x11drv_atom(_NET_SUPPORTED), 0,
2110 ~0UL, False, XA_ATOM, &type, &format, &count,
2111 &remaining, (unsigned char **)&net_supported ))
2112 net_supported_count = get_property_size( format, count ) / sizeof(Atom);
2113 else
2114 net_supported_count = 0;
2116 wine_tsx11_unlock();
2118 for (i = 0; i < net_supported_count; i++)
2119 if (net_supported[i] == atom) return TRUE;
2120 return FALSE;
2124 /***********************************************************************
2125 * SysCommand (X11DRV.@)
2127 * Perform WM_SYSCOMMAND handling.
2129 LRESULT X11DRV_SysCommand( HWND hwnd, WPARAM wparam, LPARAM lparam )
2131 WPARAM hittest = wparam & 0x0f;
2132 DWORD dwPoint;
2133 int x, y, dir;
2134 XEvent xev;
2135 Display *display = thread_display();
2136 struct x11drv_win_data *data;
2138 if (!(data = X11DRV_get_win_data( hwnd ))) return -1;
2139 if (!data->whole_window || !data->managed || !data->mapped) return -1;
2141 switch (wparam & 0xfff0)
2143 case SC_MOVE:
2144 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
2145 else dir = _NET_WM_MOVERESIZE_MOVE;
2146 break;
2147 case SC_SIZE:
2148 /* windows without WS_THICKFRAME are not resizable through the window manager */
2149 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_THICKFRAME)) return -1;
2151 switch (hittest)
2153 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
2154 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
2155 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
2156 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
2157 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
2158 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
2159 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
2160 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
2161 default: dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD; break;
2163 break;
2165 case SC_KEYMENU:
2166 /* prevent a simple ALT press+release from activating the system menu,
2167 * as that can get confusing on managed windows */
2168 if ((WCHAR)lparam) return -1; /* got an explicit char */
2169 if (GetMenu( hwnd )) return -1; /* window has a real menu */
2170 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) return -1; /* no system menu */
2171 TRACE( "ignoring SC_KEYMENU wp %lx lp %lx\n", wparam, lparam );
2172 return 0;
2174 default:
2175 return -1;
2178 if (IsZoomed(hwnd)) return -1;
2180 if (!is_netwm_supported( display, x11drv_atom(_NET_WM_MOVERESIZE) ))
2182 TRACE( "_NET_WM_MOVERESIZE not supported\n" );
2183 return -1;
2186 dwPoint = GetMessagePos();
2187 x = (short)LOWORD(dwPoint);
2188 y = (short)HIWORD(dwPoint);
2190 TRACE("hwnd %p, x %d, y %d, dir %d\n", hwnd, x, y, dir);
2192 xev.xclient.type = ClientMessage;
2193 xev.xclient.window = X11DRV_get_whole_window(hwnd);
2194 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
2195 xev.xclient.serial = 0;
2196 xev.xclient.display = display;
2197 xev.xclient.send_event = True;
2198 xev.xclient.format = 32;
2199 xev.xclient.data.l[0] = x; /* x coord */
2200 xev.xclient.data.l[1] = y; /* y coord */
2201 xev.xclient.data.l[2] = dir; /* direction */
2202 xev.xclient.data.l[3] = 1; /* button */
2203 xev.xclient.data.l[4] = 0; /* unused */
2205 /* need to ungrab the pointer that may have been automatically grabbed
2206 * with a ButtonPress event */
2207 wine_tsx11_lock();
2208 XUngrabPointer( display, CurrentTime );
2209 XSendEvent(display, root_window, False, SubstructureNotifyMask, &xev);
2210 wine_tsx11_unlock();
2211 return 0;