winex11.drv: Remove unused variables.
[wine/gsoc-2012-control.git] / dlls / winex11.drv / window.c
blob0b20338910bff39d64b66eaa52451fdee1151152
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 /* windows with thick frame are managed */
105 if (style & WS_THICKFRAME) return TRUE;
106 if (style & WS_POPUP)
108 /* popup with sysmenu == caption are managed */
109 if (style & WS_SYSMENU) return TRUE;
110 /* full-screen popup windows are managed */
111 if (window_rect->left <= 0 && window_rect->right >= screen_width &&
112 window_rect->top <= 0 && window_rect->bottom >= screen_height)
113 return TRUE;
115 /* application windows are managed */
116 ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
117 if (ex_style & WS_EX_APPWINDOW) return TRUE;
118 /* default: not managed */
119 return FALSE;
123 /***********************************************************************
124 * X11DRV_is_window_rect_mapped
126 * Check if the X whole window should be mapped based on its rectangle
128 static BOOL is_window_rect_mapped( const RECT *rect )
130 /* don't map if rect is empty */
131 if (IsRectEmpty( rect )) return FALSE;
133 /* don't map if rect is off-screen */
134 if (rect->left >= virtual_screen_rect.right ||
135 rect->top >= virtual_screen_rect.bottom ||
136 rect->right <= virtual_screen_rect.left ||
137 rect->bottom <= virtual_screen_rect.top)
138 return FALSE;
140 return TRUE;
144 /***********************************************************************
145 * is_window_resizable
147 * Check if window should be made resizable by the window manager
149 static inline BOOL is_window_resizable( struct x11drv_win_data *data, DWORD style )
151 if (style & WS_THICKFRAME) return TRUE;
152 /* Metacity needs the window to be resizable to make it fullscreen */
153 return (data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
154 data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height);
158 /***********************************************************************
159 * get_window_owner
161 static HWND get_window_owner( HWND hwnd )
163 RECT rect;
164 HWND owner = GetWindow( hwnd, GW_OWNER );
165 /* ignore the zero-size owners used by Delphi apps */
166 if (owner && GetWindowRect( owner, &rect ) && IsRectEmpty( &rect )) owner = 0;
167 return owner;
171 /***********************************************************************
172 * get_mwm_decorations
174 static unsigned long get_mwm_decorations( struct x11drv_win_data *data,
175 DWORD style, DWORD ex_style )
177 unsigned long ret = 0;
179 if (!decorated_mode) return 0;
181 if (IsRectEmpty( &data->window_rect )) return 0;
182 if (data->shaped) return 0;
184 if (ex_style & WS_EX_TOOLWINDOW) return 0;
186 if ((style & WS_CAPTION) == WS_CAPTION)
188 ret |= MWM_DECOR_TITLE | MWM_DECOR_BORDER;
189 if (style & WS_SYSMENU) ret |= MWM_DECOR_MENU;
190 if (style & WS_MINIMIZEBOX) ret |= MWM_DECOR_MINIMIZE;
191 if (style & WS_MAXIMIZEBOX) ret |= MWM_DECOR_MAXIMIZE;
193 if (ex_style & WS_EX_DLGMODALFRAME) ret |= MWM_DECOR_BORDER;
194 else if (style & WS_THICKFRAME) ret |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
195 else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) ret |= MWM_DECOR_BORDER;
196 return ret;
200 /***********************************************************************
201 * get_x11_rect_offset
203 * Helper for X11DRV_window_to_X_rect and X11DRV_X_to_window_rect.
205 static void get_x11_rect_offset( struct x11drv_win_data *data, RECT *rect )
207 DWORD style, ex_style, style_mask = 0, ex_style_mask = 0;
208 unsigned long decor;
210 rect->top = rect->bottom = rect->left = rect->right = 0;
212 style = GetWindowLongW( data->hwnd, GWL_STYLE );
213 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
214 decor = get_mwm_decorations( data, style, ex_style );
216 if (decor & MWM_DECOR_TITLE) style_mask |= WS_CAPTION;
217 if (decor & MWM_DECOR_BORDER)
219 style_mask |= WS_DLGFRAME | WS_THICKFRAME;
220 ex_style_mask |= WS_EX_DLGMODALFRAME;
223 AdjustWindowRectEx( rect, style & style_mask, FALSE, ex_style & ex_style_mask );
227 /***********************************************************************
228 * get_window_attributes
230 * Fill the window attributes structure for an X window.
232 static int get_window_attributes( Display *display, struct x11drv_win_data *data,
233 XSetWindowAttributes *attr )
235 attr->override_redirect = !data->managed;
236 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
237 attr->save_under = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
238 attr->cursor = x11drv_thread_data()->cursor;
239 attr->bit_gravity = NorthWestGravity;
240 attr->win_gravity = StaticGravity;
241 attr->backing_store = NotUseful;
242 attr->event_mask = (ExposureMask | PointerMotionMask |
243 ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
244 KeyPressMask | KeyReleaseMask | FocusChangeMask | KeymapStateMask);
245 if (data->managed) attr->event_mask |= StructureNotifyMask | PropertyChangeMask;
247 return (CWOverrideRedirect | CWSaveUnder | CWColormap | CWCursor |
248 CWEventMask | CWBitGravity | CWBackingStore);
252 /***********************************************************************
253 * create_client_window
255 static Window create_client_window( Display *display, struct x11drv_win_data *data, XVisualInfo *vis )
257 int cx, cy, mask;
258 XSetWindowAttributes attr;
259 Window client;
261 attr.bit_gravity = NorthWestGravity;
262 attr.win_gravity = NorthWestGravity;
263 attr.backing_store = NotUseful;
264 attr.event_mask = (ExposureMask | PointerMotionMask |
265 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
266 mask = CWEventMask | CWBitGravity | CWWinGravity | CWBackingStore;
268 if ((cx = data->client_rect.right - data->client_rect.left) <= 0) cx = 1;
269 if ((cy = data->client_rect.bottom - data->client_rect.top) <= 0) cy = 1;
271 wine_tsx11_lock();
273 if (vis)
275 attr.colormap = XCreateColormap( display, root_window, vis->visual,
276 (vis->class == PseudoColor || vis->class == GrayScale ||
277 vis->class == DirectColor) ? AllocAll : AllocNone );
278 mask |= CWColormap;
281 client = XCreateWindow( display, data->whole_window,
282 data->client_rect.left - data->whole_rect.left,
283 data->client_rect.top - data->whole_rect.top,
284 cx, cy, 0, screen_depth, InputOutput,
285 vis ? vis->visual : visual, mask, &attr );
286 if (!client)
288 wine_tsx11_unlock();
289 return 0;
292 if (data->client_window)
294 struct x11drv_thread_data *thread_data = x11drv_thread_data();
295 if (thread_data->cursor_window == data->client_window) thread_data->cursor_window = None;
296 XDeleteContext( display, data->client_window, winContext );
297 XDestroyWindow( display, data->client_window );
299 data->client_window = client;
301 if (data->colormap) XFreeColormap( display, data->colormap );
302 data->colormap = vis ? attr.colormap : 0;
304 XMapWindow( display, data->client_window );
305 XSaveContext( display, data->client_window, winContext, (char *)data->hwnd );
306 wine_tsx11_unlock();
308 SetPropA( data->hwnd, client_window_prop, (HANDLE)data->client_window );
309 return data->client_window;
313 /***********************************************************************
314 * sync_window_style
316 * Change the X window attributes when the window style has changed.
318 static void sync_window_style( Display *display, struct x11drv_win_data *data )
320 if (data->whole_window != root_window)
322 XSetWindowAttributes attr;
323 int mask = get_window_attributes( display, data, &attr );
325 wine_tsx11_lock();
326 XChangeWindowAttributes( display, data->whole_window, mask, &attr );
327 wine_tsx11_unlock();
332 /***********************************************************************
333 * sync_window_region
335 * Update the X11 window region.
337 static void sync_window_region( Display *display, struct x11drv_win_data *data, HRGN hrgn )
339 #ifdef HAVE_LIBXSHAPE
340 if (!data->whole_window) return;
341 data->shaped = FALSE;
343 if (!hrgn)
345 wine_tsx11_lock();
346 XShapeCombineMask( display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
347 wine_tsx11_unlock();
349 else
351 RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
352 if (pRegionData)
354 wine_tsx11_lock();
355 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
356 data->window_rect.left - data->whole_rect.left,
357 data->window_rect.top - data->whole_rect.top,
358 (XRectangle *)pRegionData->Buffer,
359 pRegionData->rdh.nCount, ShapeSet, YXBanded );
360 wine_tsx11_unlock();
361 HeapFree(GetProcessHeap(), 0, pRegionData);
362 data->shaped = TRUE;
365 #endif /* HAVE_LIBXSHAPE */
369 /***********************************************************************
370 * sync_window_text
372 static void sync_window_text( Display *display, Window win, const WCHAR *text )
374 UINT count;
375 char *buffer, *utf8_buffer;
376 XTextProperty prop;
378 /* allocate new buffer for window text */
379 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
380 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count ))) return;
381 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
383 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
384 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
386 HeapFree( GetProcessHeap(), 0, buffer );
387 return;
389 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
391 wine_tsx11_lock();
392 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
394 XSetWMName( display, win, &prop );
395 XSetWMIconName( display, win, &prop );
396 XFree( prop.value );
399 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
400 according to the standard
401 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
403 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
404 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
405 wine_tsx11_unlock();
407 HeapFree( GetProcessHeap(), 0, utf8_buffer );
408 HeapFree( GetProcessHeap(), 0, buffer );
412 /***********************************************************************
413 * X11DRV_set_win_format
415 BOOL X11DRV_set_win_format( HWND hwnd, XID fbconfig_id )
417 Display *display = thread_display();
418 struct x11drv_win_data *data;
419 XVisualInfo *vis;
420 int w, h;
422 if (!(data = X11DRV_get_win_data(hwnd)) &&
423 !(data = X11DRV_create_win_data(hwnd))) return FALSE;
425 wine_tsx11_lock();
426 vis = visual_from_fbconfig_id(fbconfig_id);
427 wine_tsx11_unlock();
428 if (!vis) return FALSE;
430 if (data->whole_window)
432 Window client = data->client_window;
434 if (vis->visualid != XVisualIDFromVisual(visual))
436 client = create_client_window( display, data, vis );
437 TRACE( "re-created client window %lx for %p fbconfig %lx\n", client, data->hwnd, fbconfig_id );
439 wine_tsx11_lock();
440 XFree(vis);
441 wine_tsx11_unlock();
442 if (client) goto done;
443 return FALSE;
446 w = data->client_rect.right - data->client_rect.left;
447 h = data->client_rect.bottom - data->client_rect.top;
449 if(w <= 0) w = 1;
450 if(h <= 0) h = 1;
452 #ifdef SONAME_LIBXCOMPOSITE
453 if(usexcomposite)
455 XSetWindowAttributes attrib;
456 Window parent = X11DRV_get_whole_window( GetAncestor( hwnd, GA_ROOT ));
458 if (!parent) parent = root_window;
459 wine_tsx11_lock();
460 data->colormap = XCreateColormap(display, parent, vis->visual,
461 (vis->class == PseudoColor ||
462 vis->class == GrayScale ||
463 vis->class == DirectColor) ?
464 AllocAll : AllocNone);
465 attrib.override_redirect = True;
466 attrib.colormap = data->colormap;
467 XInstallColormap(gdi_display, attrib.colormap);
469 if(data->gl_drawable) XDestroyWindow(gdi_display, data->gl_drawable);
470 data->gl_drawable = XCreateWindow(display, parent, -w, 0, w, h, 0,
471 vis->depth, InputOutput, vis->visual,
472 CWColormap | CWOverrideRedirect,
473 &attrib);
474 if(data->gl_drawable)
476 pXCompositeRedirectWindow(display, data->gl_drawable,
477 CompositeRedirectManual);
478 XMapWindow(display, data->gl_drawable);
480 XFree(vis);
481 wine_tsx11_unlock();
483 else
484 #endif
486 WARN("XComposite is not available, using GLXPixmap hack\n");
488 wine_tsx11_lock();
490 if(data->pixmap) XFreePixmap(display, data->pixmap);
491 data->pixmap = XCreatePixmap(display, root_window, w, h, vis->depth);
492 if(!data->pixmap)
494 XFree(vis);
495 wine_tsx11_unlock();
496 return FALSE;
499 if(data->gl_drawable) destroy_glxpixmap(display, data->gl_drawable);
500 data->gl_drawable = create_glxpixmap(display, vis, data->pixmap);
501 if(!data->gl_drawable)
503 XFreePixmap(display, data->pixmap);
504 data->pixmap = 0;
506 XFree(vis);
507 wine_tsx11_unlock();
508 if (data->pixmap) SetPropA(hwnd, pixmap_prop, (HANDLE)data->pixmap);
511 if (!data->gl_drawable) return FALSE;
513 TRACE("Created GL drawable 0x%lx, using FBConfigID 0x%lx\n",
514 data->gl_drawable, fbconfig_id);
515 SetPropA(hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
517 done:
518 data->fbconfig_id = fbconfig_id;
519 SetPropA(hwnd, fbconfig_id_prop, (HANDLE)data->fbconfig_id);
520 wine_tsx11_lock();
521 XFlush( display );
522 wine_tsx11_unlock();
523 /* force DCE invalidation */
524 SetWindowPos( hwnd, 0, 0, 0, 0, 0,
525 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE |
526 SWP_NOREDRAW | SWP_NOSENDCHANGING | SWP_STATECHANGED);
527 return TRUE;
530 /***********************************************************************
531 * sync_gl_drawable
533 static void sync_gl_drawable(Display *display, struct x11drv_win_data *data)
535 int w = data->client_rect.right - data->client_rect.left;
536 int h = data->client_rect.bottom - data->client_rect.top;
537 XVisualInfo *vis;
538 Drawable glxp;
539 Pixmap pix;
541 if (w <= 0) w = 1;
542 if (h <= 0) h = 1;
544 TRACE("Resizing GL drawable 0x%lx to %dx%d\n", data->gl_drawable, w, h);
545 #ifdef SONAME_LIBXCOMPOSITE
546 if(usexcomposite)
548 wine_tsx11_lock();
549 XMoveResizeWindow(display, data->gl_drawable, -w, 0, w, h);
550 wine_tsx11_unlock();
551 return;
553 #endif
555 wine_tsx11_lock();
557 vis = visual_from_fbconfig_id(data->fbconfig_id);
558 if(!vis)
560 wine_tsx11_unlock();
561 return;
564 pix = XCreatePixmap(display, root_window, w, h, vis->depth);
565 if(!pix)
567 ERR("Failed to create pixmap for offscreen rendering\n");
568 XFree(vis);
569 wine_tsx11_unlock();
570 return;
573 glxp = create_glxpixmap(display, vis, pix);
574 if(!glxp)
576 ERR("Failed to create drawable for offscreen rendering\n");
577 XFreePixmap(display, pix);
578 XFree(vis);
579 wine_tsx11_unlock();
580 return;
583 XFree(vis);
585 mark_drawable_dirty(data->gl_drawable, glxp);
587 XFreePixmap(display, data->pixmap);
588 destroy_glxpixmap(display, data->gl_drawable);
589 TRACE( "Recreated GL drawable %lx to replace %lx\n", glxp, data->gl_drawable );
591 data->pixmap = pix;
592 data->gl_drawable = glxp;
594 XFlush( display );
595 wine_tsx11_unlock();
597 SetPropA(data->hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
598 SetPropA(data->hwnd, pixmap_prop, (HANDLE)data->pixmap);
602 /***********************************************************************
603 * get_window_changes
605 * fill the window changes structure
607 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
609 int mask = 0;
611 if (old->right - old->left != new->right - new->left )
613 if ((changes->width = new->right - new->left) <= 0) changes->width = 1;
614 mask |= CWWidth;
616 if (old->bottom - old->top != new->bottom - new->top)
618 if ((changes->height = new->bottom - new->top) <= 0) changes->height = 1;
619 mask |= CWHeight;
621 if (old->left != new->left)
623 changes->x = new->left;
624 mask |= CWX;
626 if (old->top != new->top)
628 changes->y = new->top;
629 mask |= CWY;
631 return mask;
635 /***********************************************************************
636 * create_icon_window
638 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
640 XSetWindowAttributes attr;
642 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
643 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
644 attr.bit_gravity = NorthWestGravity;
645 attr.backing_store = NotUseful/*WhenMapped*/;
646 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
648 wine_tsx11_lock();
649 data->icon_window = XCreateWindow( display, root_window, 0, 0,
650 GetSystemMetrics( SM_CXICON ),
651 GetSystemMetrics( SM_CYICON ),
652 0, screen_depth,
653 InputOutput, visual,
654 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
655 XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
656 XFlush( display ); /* make sure the window exists before we start painting to it */
657 wine_tsx11_unlock();
659 TRACE( "created %lx\n", data->icon_window );
660 SetPropA( data->hwnd, icon_window_prop, (HANDLE)data->icon_window );
661 return data->icon_window;
666 /***********************************************************************
667 * destroy_icon_window
669 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
671 if (!data->icon_window) return;
672 if (x11drv_thread_data()->cursor_window == data->icon_window)
673 x11drv_thread_data()->cursor_window = None;
674 wine_tsx11_lock();
675 XDeleteContext( display, data->icon_window, winContext );
676 XDestroyWindow( display, data->icon_window );
677 data->icon_window = 0;
678 wine_tsx11_unlock();
679 RemovePropA( data->hwnd, icon_window_prop );
683 /***********************************************************************
684 * set_icon_hints
686 * Set the icon wm hints
688 static void set_icon_hints( Display *display, struct x11drv_win_data *data, HICON hIcon )
690 XWMHints *hints = data->wm_hints;
692 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
693 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
694 data->hWMIconBitmap = 0;
695 data->hWMIconMask = 0;
697 if (!hIcon)
699 if (!data->icon_window) create_icon_window( display, data );
700 hints->icon_window = data->icon_window;
701 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
703 else
705 HBITMAP hbmOrig;
706 RECT rcMask;
707 BITMAP bmMask;
708 ICONINFO ii;
709 HDC hDC;
711 GetIconInfo(hIcon, &ii);
713 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
714 rcMask.top = 0;
715 rcMask.left = 0;
716 rcMask.right = bmMask.bmWidth;
717 rcMask.bottom = bmMask.bmHeight;
719 hDC = CreateCompatibleDC(0);
720 hbmOrig = SelectObject(hDC, ii.hbmMask);
721 InvertRect(hDC, &rcMask);
722 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
723 SelectObject(hDC, hbmOrig);
724 DeleteDC(hDC);
726 data->hWMIconBitmap = ii.hbmColor;
727 data->hWMIconMask = ii.hbmMask;
729 hints->icon_pixmap = X11DRV_get_pixmap(data->hWMIconBitmap);
730 hints->icon_mask = X11DRV_get_pixmap(data->hWMIconMask);
731 destroy_icon_window( display, data );
732 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
737 /***********************************************************************
738 * set_size_hints
740 * set the window size hints
742 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
744 XSizeHints* size_hints;
746 if (!(size_hints = XAllocSizeHints())) return;
748 size_hints->win_gravity = StaticGravity;
749 size_hints->flags |= PWinGravity;
751 /* don't update size hints if window is not in normal state */
752 if (!(style & (WS_MINIMIZE | WS_MAXIMIZE)))
754 if (data->hwnd != GetDesktopWindow()) /* don't force position of desktop */
756 size_hints->x = data->whole_rect.left;
757 size_hints->y = data->whole_rect.top;
758 size_hints->flags |= PPosition;
761 if (!is_window_resizable( data, style ))
763 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
764 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
765 size_hints->min_width = size_hints->max_width;
766 size_hints->min_height = size_hints->max_height;
767 size_hints->flags |= PMinSize | PMaxSize;
770 XSetWMNormalHints( display, data->whole_window, size_hints );
771 XFree( size_hints );
775 /***********************************************************************
776 * get_process_name
778 * get the name of the current process for setting class hints
780 static char *get_process_name(void)
782 static char *name;
784 if (!name)
786 WCHAR module[MAX_PATH];
787 DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
788 if (len && len < MAX_PATH)
790 char *ptr;
791 WCHAR *p, *appname = module;
793 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
794 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
795 len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
796 if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
798 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
799 name = ptr;
803 return name;
807 /***********************************************************************
808 * set_initial_wm_hints
810 * Set the window manager hints that don't change over the lifetime of a window.
812 static void set_initial_wm_hints( Display *display, struct x11drv_win_data *data )
814 long i;
815 Atom protocols[3];
816 Atom dndVersion = 4;
817 XClassHint *class_hints;
818 char *process_name = get_process_name();
820 wine_tsx11_lock();
822 /* wm protocols */
823 i = 0;
824 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
825 protocols[i++] = x11drv_atom(_NET_WM_PING);
826 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
827 XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
828 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
830 /* class hints */
831 if ((class_hints = XAllocClassHint()))
833 static char wine[] = "Wine";
835 class_hints->res_name = process_name;
836 class_hints->res_class = wine;
837 XSetClassHint( display, data->whole_window, class_hints );
838 XFree( class_hints );
841 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
842 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
843 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
844 i = getpid();
845 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
846 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
848 XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
849 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
851 data->wm_hints = XAllocWMHints();
852 wine_tsx11_unlock();
854 if (data->wm_hints)
856 HICON icon = (HICON)SendMessageW( data->hwnd, WM_GETICON, ICON_BIG, 0 );
857 if (!icon) icon = (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON );
858 data->wm_hints->flags = 0;
859 set_icon_hints( display, data, icon );
864 /***********************************************************************
865 * set_wm_hints
867 * Set the window manager hints for a newly-created window
869 static void set_wm_hints( Display *display, struct x11drv_win_data *data )
871 Window group_leader;
872 Atom window_type;
873 MwmHints mwm_hints;
874 DWORD style, ex_style;
875 HWND owner;
877 if (data->hwnd == GetDesktopWindow())
879 /* force some styles for the desktop to get the correct decorations */
880 style = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
881 ex_style = WS_EX_APPWINDOW;
882 owner = 0;
884 else
886 style = GetWindowLongW( data->hwnd, GWL_STYLE );
887 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
888 owner = get_window_owner( data->hwnd );
891 /* transient for hint */
892 if (owner)
894 Window owner_win = X11DRV_get_whole_window( owner );
895 wine_tsx11_lock();
896 XSetTransientForHint( display, data->whole_window, owner_win );
897 wine_tsx11_unlock();
898 group_leader = owner_win;
900 else group_leader = data->whole_window;
902 wine_tsx11_lock();
904 /* size hints */
905 set_size_hints( display, data, style );
907 /* set the WM_WINDOW_TYPE */
908 if (style & WS_THICKFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
909 else if (ex_style & WS_EX_APPWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
910 else if (style & WS_DLGFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
911 else if (ex_style & WS_EX_DLGMODALFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
912 else if ((style & WS_POPUP) && owner) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
913 #if 0 /* many window managers don't handle utility windows very well */
914 else if (ex_style & WS_EX_TOOLWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
915 #endif
916 else window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
918 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
919 XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
921 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
922 mwm_hints.decorations = get_mwm_decorations( data, style, ex_style );
923 mwm_hints.functions = MWM_FUNC_MOVE;
924 if (is_window_resizable( data, style )) mwm_hints.functions |= MWM_FUNC_RESIZE;
925 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
926 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
927 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
929 XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
930 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
931 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
933 /* wm hints */
934 if (data->wm_hints)
936 data->wm_hints->flags |= InputHint | StateHint | WindowGroupHint;
937 data->wm_hints->input = !(style & WS_DISABLED);
938 data->wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
939 data->wm_hints->window_group = group_leader;
940 XSetWMHints( display, data->whole_window, data->wm_hints );
943 wine_tsx11_unlock();
947 /***********************************************************************
948 * update_net_wm_states
950 void update_net_wm_states( Display *display, struct x11drv_win_data *data )
952 static const unsigned int state_atoms[NB_NET_WM_STATES] =
954 XATOM__NET_WM_STATE_FULLSCREEN,
955 XATOM__NET_WM_STATE_ABOVE,
956 XATOM__NET_WM_STATE_MAXIMIZED_VERT,
957 XATOM__NET_WM_STATE_SKIP_PAGER,
958 XATOM__NET_WM_STATE_SKIP_TASKBAR
961 DWORD i, style, ex_style, new_state = 0;
963 if (!data->managed) return;
964 if (data->whole_window == root_window) return;
966 style = GetWindowLongW( data->hwnd, GWL_STYLE );
967 if (data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
968 data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height)
970 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION)
971 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
972 else if (!(style & WS_MINIMIZE))
973 new_state |= (1 << NET_WM_STATE_FULLSCREEN);
975 else if (style & WS_MAXIMIZE)
976 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
978 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
979 if (ex_style & WS_EX_TOPMOST)
980 new_state |= (1 << NET_WM_STATE_ABOVE);
981 if (ex_style & WS_EX_TOOLWINDOW)
982 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR) | (1 << NET_WM_STATE_SKIP_PAGER);
983 if (!(ex_style & WS_EX_APPWINDOW) && get_window_owner( data->hwnd ))
984 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR);
986 if (!data->mapped) /* set the _NET_WM_STATE atom directly */
988 Atom atoms[NB_NET_WM_STATES+1];
989 DWORD count;
991 for (i = count = 0; i < NB_NET_WM_STATES; i++)
993 if (!(new_state & (1 << i))) continue;
994 TRACE( "setting wm state %u for unmapped window %p/%lx\n",
995 i, data->hwnd, data->whole_window );
996 atoms[count++] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
997 if (state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT)
998 atoms[count++] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
1000 wine_tsx11_lock();
1001 XChangeProperty( display, data->whole_window, x11drv_atom(_NET_WM_STATE), XA_ATOM,
1002 32, PropModeReplace, (unsigned char *)atoms, count );
1003 wine_tsx11_unlock();
1005 else /* ask the window manager to do it for us */
1007 XEvent xev;
1009 xev.xclient.type = ClientMessage;
1010 xev.xclient.window = data->whole_window;
1011 xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
1012 xev.xclient.serial = 0;
1013 xev.xclient.display = display;
1014 xev.xclient.send_event = True;
1015 xev.xclient.format = 32;
1016 xev.xclient.data.l[3] = 1;
1018 for (i = 0; i < NB_NET_WM_STATES; i++)
1020 if (!((data->net_wm_state ^ new_state) & (1 << i))) continue; /* unchanged */
1022 TRACE( "setting wm state %u for window %p/%lx to %u prev %u\n",
1023 i, data->hwnd, data->whole_window,
1024 (new_state & (1 << i)) != 0, (data->net_wm_state & (1 << i)) != 0 );
1026 xev.xclient.data.l[0] = (new_state & (1 << i)) ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1027 xev.xclient.data.l[1] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1028 xev.xclient.data.l[2] = ((state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT) ?
1029 x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ) : 0);
1030 wine_tsx11_lock();
1031 XSendEvent( display, root_window, False,
1032 SubstructureRedirectMask | SubstructureNotifyMask, &xev );
1033 wine_tsx11_unlock();
1036 data->net_wm_state = new_state;
1040 /***********************************************************************
1041 * set_xembed_flags
1043 static void set_xembed_flags( Display *display, struct x11drv_win_data *data, unsigned long flags )
1045 unsigned long info[2];
1047 info[0] = 0; /* protocol version */
1048 info[1] = flags;
1049 wine_tsx11_lock();
1050 XChangeProperty( display, data->whole_window, x11drv_atom(_XEMBED_INFO),
1051 x11drv_atom(_XEMBED_INFO), 32, PropModeReplace, (unsigned char*)info, 2 );
1052 wine_tsx11_unlock();
1056 /***********************************************************************
1057 * map_window
1059 static void map_window( Display *display, struct x11drv_win_data *data, DWORD new_style )
1061 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1063 wait_for_withdrawn_state( display, data, TRUE );
1065 if (!data->embedded)
1067 update_net_wm_states( display, data );
1068 sync_window_style( display, data );
1069 wine_tsx11_lock();
1070 XMapWindow( display, data->whole_window );
1071 wine_tsx11_unlock();
1073 else set_xembed_flags( display, data, XEMBED_MAPPED );
1075 data->mapped = TRUE;
1076 data->iconic = (new_style & WS_MINIMIZE) != 0;
1080 /***********************************************************************
1081 * unmap_window
1083 static void unmap_window( Display *display, struct x11drv_win_data *data )
1085 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1087 if (!data->embedded)
1089 wait_for_withdrawn_state( display, data, FALSE );
1090 wine_tsx11_lock();
1091 if (data->managed) XWithdrawWindow( display, data->whole_window, DefaultScreen(display) );
1092 else XUnmapWindow( display, data->whole_window );
1093 wine_tsx11_unlock();
1095 else set_xembed_flags( display, data, 0 );
1097 data->mapped = FALSE;
1098 data->net_wm_state = 0;
1102 /***********************************************************************
1103 * make_window_embedded
1105 void make_window_embedded( Display *display, struct x11drv_win_data *data )
1107 if (data->mapped)
1109 /* the window cannot be mapped before being embedded */
1110 unmap_window( display, data );
1111 data->embedded = TRUE;
1112 map_window( display, data, 0 );
1114 else
1116 data->embedded = TRUE;
1117 set_xembed_flags( display, data, 0 );
1122 /***********************************************************************
1123 * X11DRV_window_to_X_rect
1125 * Convert a rect from client to X window coordinates
1127 void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
1129 RECT rc;
1131 if (!data->managed) return;
1132 if (IsRectEmpty( rect )) return;
1134 get_x11_rect_offset( data, &rc );
1136 rect->left -= rc.left;
1137 rect->right -= rc.right;
1138 rect->top -= rc.top;
1139 rect->bottom -= rc.bottom;
1140 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1141 if (rect->left >= rect->right) rect->right = rect->left + 1;
1145 /***********************************************************************
1146 * X11DRV_X_to_window_rect
1148 * Opposite of X11DRV_window_to_X_rect
1150 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
1152 RECT rc;
1154 if (!data->managed) return;
1155 if (IsRectEmpty( rect )) return;
1157 get_x11_rect_offset( data, &rc );
1159 rect->left += rc.left;
1160 rect->right += rc.right;
1161 rect->top += rc.top;
1162 rect->bottom += rc.bottom;
1163 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1164 if (rect->left >= rect->right) rect->right = rect->left + 1;
1168 /***********************************************************************
1169 * sync_window_position
1171 * Synchronize the X window position with the Windows one
1173 static void sync_window_position( Display *display, struct x11drv_win_data *data,
1174 UINT swp_flags, const RECT *old_client_rect,
1175 const RECT *old_whole_rect )
1177 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
1178 XWindowChanges changes;
1179 unsigned int mask = 0;
1181 if (data->managed && data->iconic) return;
1183 /* resizing a managed maximized window is not allowed */
1184 if (!(style & WS_MAXIMIZE) || !data->managed)
1186 if ((changes.width = data->whole_rect.right - data->whole_rect.left) <= 0) changes.width = 1;
1187 if ((changes.height = data->whole_rect.bottom - data->whole_rect.top) <= 0) changes.height = 1;
1188 mask |= CWWidth | CWHeight;
1191 /* only the size is allowed to change for the desktop window */
1192 if (data->whole_window != root_window)
1194 changes.x = data->whole_rect.left - virtual_screen_rect.left;
1195 changes.y = data->whole_rect.top - virtual_screen_rect.top;
1196 mask |= CWX | CWY;
1199 if (!(swp_flags & SWP_NOZORDER))
1201 /* find window that this one must be after */
1202 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
1203 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
1204 prev = GetWindow( prev, GW_HWNDPREV );
1205 if (!prev) /* top child */
1207 changes.stack_mode = Above;
1208 mask |= CWStackMode;
1210 /* should use stack_mode Below but most window managers don't get it right */
1211 /* and Above with a sibling doesn't work so well either, so we ignore it */
1214 TRACE( "setting win %p/%lx pos %d,%d,%dx%d after %lx changes=%x\n",
1215 data->hwnd, data->whole_window, data->whole_rect.left, data->whole_rect.top,
1216 data->whole_rect.right - data->whole_rect.left,
1217 data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
1219 wine_tsx11_lock();
1220 set_size_hints( display, data, style );
1221 XReconfigureWMWindow( display, data->whole_window,
1222 DefaultScreen(display), mask, &changes );
1223 wine_tsx11_unlock();
1227 /***********************************************************************
1228 * sync_client_position
1230 * Synchronize the X client window position with the Windows one
1232 static void sync_client_position( Display *display, struct x11drv_win_data *data,
1233 UINT swp_flags, const RECT *old_client_rect,
1234 const RECT *old_whole_rect )
1236 int mask;
1237 XWindowChanges changes;
1238 RECT old = *old_client_rect;
1239 RECT new = data->client_rect;
1241 OffsetRect( &old, -old_whole_rect->left, -old_whole_rect->top );
1242 OffsetRect( &new, -data->whole_rect.left, -data->whole_rect.top );
1243 if (!(mask = get_window_changes( &changes, &old, &new ))) return;
1245 if (data->client_window)
1247 TRACE( "setting client win %lx pos %d,%d,%dx%d changes=%x\n",
1248 data->client_window, new.left, new.top,
1249 new.right - new.left, new.bottom - new.top, mask );
1250 wine_tsx11_lock();
1251 XConfigureWindow( display, data->client_window, mask, &changes );
1252 wine_tsx11_unlock();
1255 if (data->gl_drawable && (mask & (CWWidth|CWHeight))) sync_gl_drawable( display, data );
1259 /***********************************************************************
1260 * move_window_bits
1262 * Move the window bits when a window is moved.
1264 static void move_window_bits( struct x11drv_win_data *data, const RECT *old_rect, const RECT *new_rect,
1265 const RECT *old_client_rect )
1267 RECT src_rect = *old_rect;
1268 RECT dst_rect = *new_rect;
1269 HDC hdc_src, hdc_dst;
1270 INT code;
1271 HRGN rgn = 0;
1272 HWND parent = 0;
1274 if (!data->whole_window)
1276 OffsetRect( &dst_rect, -data->window_rect.left, -data->window_rect.top );
1277 parent = GetAncestor( data->hwnd, GA_PARENT );
1278 hdc_src = GetDCEx( parent, 0, DCX_CACHE );
1279 hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE | DCX_WINDOW );
1281 else
1283 OffsetRect( &dst_rect, -data->client_rect.left, -data->client_rect.top );
1284 /* make src rect relative to the old position of the window */
1285 OffsetRect( &src_rect, -old_client_rect->left, -old_client_rect->top );
1286 if (dst_rect.left == src_rect.left && dst_rect.top == src_rect.top) return;
1287 hdc_src = hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE );
1290 code = X11DRV_START_EXPOSURES;
1291 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
1293 TRACE( "copying bits for win %p/%lx/%lx %s -> %s\n",
1294 data->hwnd, data->whole_window, data->client_window,
1295 wine_dbgstr_rect(&src_rect), wine_dbgstr_rect(&dst_rect) );
1296 BitBlt( hdc_dst, dst_rect.left, dst_rect.top,
1297 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top,
1298 hdc_src, src_rect.left, src_rect.top, SRCCOPY );
1300 code = X11DRV_END_EXPOSURES;
1301 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, sizeof(rgn), (LPSTR)&rgn );
1303 ReleaseDC( data->hwnd, hdc_dst );
1304 if (hdc_src != hdc_dst) ReleaseDC( parent, hdc_src );
1306 if (rgn)
1308 if (!data->whole_window)
1310 /* map region to client rect since we are using DCX_WINDOW */
1311 OffsetRgn( rgn, data->window_rect.left - data->client_rect.left,
1312 data->window_rect.top - data->client_rect.top );
1313 RedrawWindow( data->hwnd, NULL, rgn,
1314 RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ALLCHILDREN );
1316 else RedrawWindow( data->hwnd, NULL, rgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1317 DeleteObject( rgn );
1322 /**********************************************************************
1323 * create_whole_window
1325 * Create the whole X window for a given window
1327 static Window create_whole_window( Display *display, struct x11drv_win_data *data )
1329 int cx, cy, mask;
1330 XSetWindowAttributes attr;
1331 WCHAR text[1024];
1332 HRGN hrgn;
1334 if (!(cx = data->window_rect.right - data->window_rect.left)) cx = 1;
1335 if (!(cy = data->window_rect.bottom - data->window_rect.top)) cy = 1;
1337 if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1339 TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1340 data->managed = TRUE;
1341 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1344 mask = get_window_attributes( display, data, &attr );
1346 wine_tsx11_lock();
1348 data->whole_rect = data->window_rect;
1349 data->whole_window = XCreateWindow( display, root_window,
1350 data->window_rect.left - virtual_screen_rect.left,
1351 data->window_rect.top - virtual_screen_rect.top,
1352 cx, cy, 0, screen_depth, InputOutput,
1353 visual, mask, &attr );
1355 if (data->whole_window) XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
1356 wine_tsx11_unlock();
1358 if (!data->whole_window) return 0;
1360 if (!create_client_window( display, data, NULL ))
1362 wine_tsx11_lock();
1363 XDeleteContext( display, data->whole_window, winContext );
1364 XDestroyWindow( display, data->whole_window );
1365 data->whole_window = 0;
1366 wine_tsx11_unlock();
1367 return 0;
1370 set_initial_wm_hints( display, data );
1371 set_wm_hints( display, data );
1373 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1375 /* set the window text */
1376 if (!InternalGetWindowText( data->hwnd, text, sizeof(text)/sizeof(WCHAR) )) text[0] = 0;
1377 sync_window_text( display, data->whole_window, text );
1379 /* set the window region */
1380 if ((hrgn = CreateRectRgn( 0, 0, 0, 0 )))
1382 if (GetWindowRgn( data->hwnd, hrgn ) != ERROR) sync_window_region( display, data, hrgn );
1383 DeleteObject( hrgn );
1385 wine_tsx11_lock();
1386 XFlush( display ); /* make sure the window exists before we start painting to it */
1387 wine_tsx11_unlock();
1388 return data->whole_window;
1392 /**********************************************************************
1393 * destroy_whole_window
1395 * Destroy the whole X window for a given window.
1397 static void destroy_whole_window( Display *display, struct x11drv_win_data *data, BOOL already_destroyed )
1399 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1401 if (!data->whole_window) return;
1403 TRACE( "win %p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1404 if (thread_data->cursor_window == data->whole_window ||
1405 thread_data->cursor_window == data->client_window)
1406 thread_data->cursor_window = None;
1407 wine_tsx11_lock();
1408 XDeleteContext( display, data->whole_window, winContext );
1409 XDeleteContext( display, data->client_window, winContext );
1410 if (!already_destroyed) XDestroyWindow( display, data->whole_window );
1411 data->whole_window = data->client_window = 0;
1412 data->wm_state = WithdrawnState;
1413 data->net_wm_state = 0;
1414 data->mapped = FALSE;
1415 if (data->xic)
1417 XUnsetICFocus( data->xic );
1418 XDestroyIC( data->xic );
1419 data->xic = 0;
1421 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1422 XFlush( display );
1423 XFree( data->wm_hints );
1424 data->wm_hints = NULL;
1425 wine_tsx11_unlock();
1426 RemovePropA( data->hwnd, whole_window_prop );
1427 RemovePropA( data->hwnd, client_window_prop );
1431 /*****************************************************************
1432 * SetWindowText (X11DRV.@)
1434 void X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1436 Display *display = thread_display();
1437 Window win;
1439 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(display))
1440 sync_window_text( display, win, text );
1444 /***********************************************************************
1445 * SetWindowStyle (X11DRV.@)
1447 * Update the X state of a window to reflect a style change
1449 void X11DRV_SetWindowStyle( HWND hwnd, DWORD old_style )
1451 Display *display = thread_display();
1452 struct x11drv_win_data *data;
1453 DWORD new_style, changed;
1455 if (hwnd == GetDesktopWindow()) return;
1456 new_style = GetWindowLongW( hwnd, GWL_STYLE );
1457 changed = new_style ^ old_style;
1459 if ((changed & WS_VISIBLE) && (new_style & WS_VISIBLE))
1461 /* we don't unmap windows, that causes trouble with the window manager */
1462 if (!(data = X11DRV_get_win_data( hwnd )) &&
1463 !(data = X11DRV_create_win_data( hwnd ))) return;
1465 if (data->whole_window && is_window_rect_mapped( &data->window_rect ))
1467 set_wm_hints( display, data );
1468 if (!data->mapped) map_window( display, data, new_style );
1472 if (changed & WS_DISABLED)
1474 data = X11DRV_get_win_data( hwnd );
1475 if (data && data->wm_hints)
1477 wine_tsx11_lock();
1478 data->wm_hints->input = !(new_style & WS_DISABLED);
1479 XSetWMHints( display, data->whole_window, data->wm_hints );
1480 wine_tsx11_unlock();
1486 /***********************************************************************
1487 * DestroyWindow (X11DRV.@)
1489 void X11DRV_DestroyWindow( HWND hwnd )
1491 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1492 Display *display = thread_data->display;
1493 struct x11drv_win_data *data;
1495 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1497 if (data->pixmap)
1499 destroy_glxpixmap(display, data->gl_drawable);
1500 wine_tsx11_lock();
1501 XFreePixmap(display, data->pixmap);
1502 wine_tsx11_unlock();
1504 else if (data->gl_drawable)
1506 wine_tsx11_lock();
1507 XDestroyWindow(display, data->gl_drawable);
1508 wine_tsx11_unlock();
1511 destroy_whole_window( display, data, FALSE );
1512 destroy_icon_window( display, data );
1514 if (data->colormap)
1516 wine_tsx11_lock();
1517 XFreeColormap( display, data->colormap );
1518 wine_tsx11_unlock();
1521 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1522 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
1523 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
1524 wine_tsx11_lock();
1525 XDeleteContext( display, (XID)hwnd, win_data_context );
1526 wine_tsx11_unlock();
1527 HeapFree( GetProcessHeap(), 0, data );
1531 /***********************************************************************
1532 * X11DRV_DestroyNotify
1534 void X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1536 Display *display = event->xdestroywindow.display;
1537 struct x11drv_win_data *data;
1539 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1541 FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1542 destroy_whole_window( display, data, TRUE );
1546 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
1548 struct x11drv_win_data *data;
1550 if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
1552 data->hwnd = hwnd;
1553 wine_tsx11_lock();
1554 if (!winContext) winContext = XUniqueContext();
1555 if (!win_data_context) win_data_context = XUniqueContext();
1556 XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
1557 wine_tsx11_unlock();
1559 return data;
1563 /* initialize the desktop window id in the desktop manager process */
1564 static struct x11drv_win_data *create_desktop_win_data( Display *display, HWND hwnd )
1566 struct x11drv_win_data *data;
1568 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1569 data->whole_window = data->client_window = root_window;
1570 data->managed = TRUE;
1571 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1572 SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
1573 SetPropA( data->hwnd, client_window_prop, (HANDLE)root_window );
1574 set_initial_wm_hints( display, data );
1575 return data;
1578 /**********************************************************************
1579 * CreateDesktopWindow (X11DRV.@)
1581 BOOL X11DRV_CreateDesktopWindow( HWND hwnd )
1583 unsigned int width, height;
1585 /* retrieve the real size of the desktop */
1586 SERVER_START_REQ( get_window_rectangles )
1588 req->handle = hwnd;
1589 wine_server_call( req );
1590 width = reply->window.right - reply->window.left;
1591 height = reply->window.bottom - reply->window.top;
1593 SERVER_END_REQ;
1595 if (!width && !height) /* not initialized yet */
1597 SERVER_START_REQ( set_window_pos )
1599 req->handle = hwnd;
1600 req->previous = 0;
1601 req->flags = SWP_NOZORDER;
1602 req->window.left = virtual_screen_rect.left;
1603 req->window.top = virtual_screen_rect.top;
1604 req->window.right = virtual_screen_rect.right;
1605 req->window.bottom = virtual_screen_rect.bottom;
1606 req->client = req->window;
1607 wine_server_call( req );
1609 SERVER_END_REQ;
1611 else
1613 Window win = (Window)GetPropA( hwnd, whole_window_prop );
1614 if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1616 return TRUE;
1620 /**********************************************************************
1621 * CreateWindow (X11DRV.@)
1623 BOOL X11DRV_CreateWindow( HWND hwnd )
1625 Display *display = thread_display();
1627 if (hwnd == GetDesktopWindow() && root_window != DefaultRootWindow( display ))
1629 /* the desktop win data can't be created lazily */
1630 if (!create_desktop_win_data( display, hwnd )) return FALSE;
1632 return TRUE;
1636 /***********************************************************************
1637 * X11DRV_get_win_data
1639 * Return the X11 data structure associated with a window.
1641 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1643 char *data;
1645 if (!hwnd || XFindContext( thread_display(), (XID)hwnd, win_data_context, &data )) data = NULL;
1646 return (struct x11drv_win_data *)data;
1650 /***********************************************************************
1651 * X11DRV_create_win_data
1653 * Create an X11 data window structure for an existing window.
1655 struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd )
1657 Display *display = thread_display();
1658 struct x11drv_win_data *data;
1659 HWND parent;
1661 if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL; /* desktop */
1662 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1664 GetWindowRect( hwnd, &data->window_rect );
1665 MapWindowPoints( 0, parent, (POINT *)&data->window_rect, 2 );
1666 data->whole_rect = data->window_rect;
1667 GetClientRect( hwnd, &data->client_rect );
1668 MapWindowPoints( hwnd, parent, (POINT *)&data->client_rect, 2 );
1670 if (parent == GetDesktopWindow())
1672 if (!create_whole_window( display, data ))
1674 HeapFree( GetProcessHeap(), 0, data );
1675 return NULL;
1677 TRACE( "win %p/%lx/%lx window %s whole %s client %s\n",
1678 hwnd, data->whole_window, data->client_window, wine_dbgstr_rect( &data->window_rect ),
1679 wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
1681 return data;
1685 /***********************************************************************
1686 * X11DRV_get_whole_window
1688 * Return the X window associated with the full area of a window
1690 Window X11DRV_get_whole_window( HWND hwnd )
1692 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1694 if (!data)
1696 if (hwnd == GetDesktopWindow()) return root_window;
1697 return (Window)GetPropA( hwnd, whole_window_prop );
1699 return data->whole_window;
1703 /***********************************************************************
1704 * X11DRV_get_client_window
1706 * Return the X window associated with the client area of a window
1708 Window X11DRV_get_client_window( HWND hwnd )
1710 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1712 if (!data)
1714 if (hwnd == GetDesktopWindow()) return root_window;
1715 return (Window)GetPropA( hwnd, client_window_prop );
1717 return data->client_window;
1721 /***********************************************************************
1722 * X11DRV_get_ic
1724 * Return the X input context associated with a window
1726 XIC X11DRV_get_ic( HWND hwnd )
1728 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1729 XIM xim;
1731 if (!data) return 0;
1732 if (data->xic) return data->xic;
1733 if (!(xim = x11drv_thread_data()->xim)) return 0;
1734 return X11DRV_CreateIC( xim, data );
1738 /***********************************************************************
1739 * X11DRV_GetDC (X11DRV.@)
1741 void X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
1742 const RECT *top_rect, DWORD flags )
1744 struct x11drv_escape_set_drawable escape;
1745 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1747 escape.code = X11DRV_SET_DRAWABLE;
1748 escape.mode = IncludeInferiors;
1749 escape.fbconfig_id = 0;
1750 escape.gl_drawable = 0;
1751 escape.pixmap = 0;
1753 if (top == hwnd && data && IsIconic( hwnd ) && data->icon_window)
1755 escape.drawable = data->icon_window;
1757 else if (top == hwnd && (flags & DCX_WINDOW))
1759 escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
1761 else
1763 escape.drawable = X11DRV_get_client_window( top );
1764 escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
1765 escape.gl_drawable = data ? data->gl_drawable : (Drawable)GetPropA( hwnd, gl_drawable_prop );
1766 escape.pixmap = data ? data->pixmap : (Pixmap)GetPropA( hwnd, pixmap_prop );
1767 if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
1770 escape.dc_rect.left = win_rect->left - top_rect->left;
1771 escape.dc_rect.top = win_rect->top - top_rect->top;
1772 escape.dc_rect.right = win_rect->right - top_rect->left;
1773 escape.dc_rect.bottom = win_rect->bottom - top_rect->top;
1774 escape.drawable_rect.left = top_rect->left;
1775 escape.drawable_rect.top = top_rect->top;
1776 escape.drawable_rect.right = top_rect->right;
1777 escape.drawable_rect.bottom = top_rect->bottom;
1779 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1783 /***********************************************************************
1784 * X11DRV_ReleaseDC (X11DRV.@)
1786 void X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
1788 struct x11drv_escape_set_drawable escape;
1790 escape.code = X11DRV_SET_DRAWABLE;
1791 escape.drawable = root_window;
1792 escape.mode = IncludeInferiors;
1793 escape.drawable_rect = virtual_screen_rect;
1794 SetRect( &escape.dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
1795 virtual_screen_rect.bottom - virtual_screen_rect.top );
1796 escape.fbconfig_id = 0;
1797 escape.gl_drawable = 0;
1798 escape.pixmap = 0;
1799 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1803 /***********************************************************************
1804 * SetCapture (X11DRV.@)
1806 void X11DRV_SetCapture( HWND hwnd, UINT flags )
1808 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1810 if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
1812 if (hwnd)
1814 Window grab_win = X11DRV_get_client_window( GetAncestor( hwnd, GA_ROOT ) );
1816 if (!grab_win) return;
1817 wine_tsx11_lock();
1818 XFlush( gdi_display );
1819 XGrabPointer( thread_data->display, grab_win, False,
1820 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1821 GrabModeAsync, GrabModeAsync, None, None, CurrentTime );
1822 wine_tsx11_unlock();
1823 thread_data->grab_window = grab_win;
1825 else /* release capture */
1827 wine_tsx11_lock();
1828 XFlush( gdi_display );
1829 XUngrabPointer( thread_data->display, CurrentTime );
1830 wine_tsx11_unlock();
1831 thread_data->grab_window = None;
1836 /*****************************************************************
1837 * SetParent (X11DRV.@)
1839 void X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
1841 Display *display = thread_display();
1842 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1844 if (!data) return;
1845 if (parent == old_parent) return;
1847 if (parent != GetDesktopWindow()) /* a child window */
1849 if (old_parent == GetDesktopWindow())
1851 /* destroy the old X windows */
1852 destroy_whole_window( display, data, FALSE );
1853 destroy_icon_window( display, data );
1854 if (data->managed)
1856 data->managed = FALSE;
1857 RemovePropA( data->hwnd, managed_prop );
1861 else /* new top level window */
1863 /* FIXME: we ignore errors since we can't really recover anyway */
1864 create_whole_window( display, data );
1869 /*****************************************************************
1870 * SetFocus (X11DRV.@)
1872 * Set the X focus.
1873 * Explicit colormap management seems to work only with OLVWM.
1875 void X11DRV_SetFocus( HWND hwnd )
1877 Display *display = thread_display();
1878 struct x11drv_win_data *data;
1879 XWindowChanges changes;
1881 /* If setting the focus to 0, uninstall the colormap */
1882 if (!hwnd && root_window == DefaultRootWindow(display))
1884 wine_tsx11_lock();
1885 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1886 XUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1887 wine_tsx11_unlock();
1888 return;
1891 hwnd = GetAncestor( hwnd, GA_ROOT );
1893 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1894 if (data->managed || !data->whole_window) return;
1896 /* Set X focus and install colormap */
1897 wine_tsx11_lock();
1898 changes.stack_mode = Above;
1899 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
1900 if (root_window == DefaultRootWindow(display))
1902 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1903 /* FIXME: this is not entirely correct */
1904 XSetInputFocus( display, data->whole_window, RevertToParent,
1905 /* CurrentTime */
1906 GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1907 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1908 XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1910 wine_tsx11_unlock();
1914 /***********************************************************************
1915 * SetWindowPos (X11DRV.@)
1917 void X11DRV_SetWindowPos( HWND hwnd, HWND insert_after, UINT swp_flags,
1918 const RECT *rectWindow, const RECT *rectClient,
1919 const RECT *visible_rect, const RECT *valid_rects )
1921 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1922 Display *display = thread_data->display;
1923 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1924 DWORD new_style = GetWindowLongW( hwnd, GWL_STYLE );
1925 RECT old_whole_rect, old_client_rect;
1926 int event_type;
1928 if (!data)
1930 /* create the win data if the window is being made visible */
1931 if (!(new_style & WS_VISIBLE)) return;
1932 if (!(data = X11DRV_create_win_data( hwnd ))) return;
1935 /* check if we need to switch the window to managed */
1936 if (!data->managed && data->whole_window && is_window_managed( hwnd, swp_flags, rectWindow ))
1938 TRACE( "making win %p/%lx managed\n", hwnd, data->whole_window );
1939 if (data->mapped) unmap_window( display, data );
1940 data->managed = TRUE;
1941 SetPropA( hwnd, managed_prop, (HANDLE)1 );
1944 old_whole_rect = data->whole_rect;
1945 old_client_rect = data->client_rect;
1946 data->window_rect = *rectWindow;
1947 data->whole_rect = *rectWindow;
1948 data->client_rect = *rectClient;
1949 X11DRV_window_to_X_rect( data, &data->whole_rect );
1950 if (memcmp( visible_rect, &data->whole_rect, sizeof(RECT) ))
1952 TRACE( "%p: need to update visible rect %s -> %s\n", hwnd,
1953 wine_dbgstr_rect(visible_rect), wine_dbgstr_rect(&data->whole_rect) );
1954 SERVER_START_REQ( set_window_visible_rect )
1956 req->handle = hwnd;
1957 req->flags = swp_flags;
1958 req->visible.left = data->whole_rect.left;
1959 req->visible.top = data->whole_rect.top;
1960 req->visible.right = data->whole_rect.right;
1961 req->visible.bottom = data->whole_rect.bottom;
1962 wine_server_call( req );
1964 SERVER_END_REQ;
1967 TRACE( "win %p window %s client %s style %08x flags %08x\n",
1968 hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style, swp_flags );
1970 if (!IsRectEmpty( &valid_rects[0] ))
1972 int x_offset = old_whole_rect.left - data->whole_rect.left;
1973 int y_offset = old_whole_rect.top - data->whole_rect.top;
1975 /* if all that happened is that the whole window moved, copy everything */
1976 if (!(swp_flags & SWP_FRAMECHANGED) &&
1977 old_whole_rect.right - data->whole_rect.right == x_offset &&
1978 old_whole_rect.bottom - data->whole_rect.bottom == y_offset &&
1979 old_client_rect.left - data->client_rect.left == x_offset &&
1980 old_client_rect.right - data->client_rect.right == x_offset &&
1981 old_client_rect.top - data->client_rect.top == y_offset &&
1982 old_client_rect.bottom - data->client_rect.bottom == y_offset &&
1983 !memcmp( &valid_rects[0], &data->client_rect, sizeof(RECT) ))
1985 /* if we have an X window the bits will be moved by the X server */
1986 if (!data->whole_window)
1987 move_window_bits( data, &old_whole_rect, &data->whole_rect, &old_client_rect );
1989 else
1990 move_window_bits( data, &valid_rects[1], &valid_rects[0], &old_client_rect );
1993 wine_tsx11_lock();
1994 XFlush( gdi_display ); /* make sure painting is done before we move the window */
1995 wine_tsx11_unlock();
1997 sync_client_position( display, data, swp_flags, &old_client_rect, &old_whole_rect );
1999 if (!data->whole_window) return;
2001 /* check if we are currently processing an event relevant to this window */
2002 event_type = 0;
2003 if (thread_data->current_event && thread_data->current_event->xany.window == data->whole_window)
2004 event_type = thread_data->current_event->type;
2006 if (event_type != ConfigureNotify && event_type != PropertyNotify)
2007 event_type = 0; /* ignore other events */
2009 if (data->mapped)
2011 if (((swp_flags & SWP_HIDEWINDOW) && !(new_style & WS_VISIBLE)) ||
2012 (!event_type && !is_window_rect_mapped( rectWindow )))
2013 unmap_window( display, data );
2016 /* don't change position if we are about to minimize or maximize a managed window */
2017 if (!event_type &&
2018 !(data->managed && (swp_flags & SWP_STATECHANGED) && (new_style & (WS_MINIMIZE|WS_MAXIMIZE))))
2019 sync_window_position( display, data, swp_flags, &old_client_rect, &old_whole_rect );
2021 if ((new_style & WS_VISIBLE) &&
2022 ((new_style & WS_MINIMIZE) || is_window_rect_mapped( rectWindow )))
2024 if (!data->mapped || (swp_flags & (SWP_FRAMECHANGED|SWP_STATECHANGED)))
2025 set_wm_hints( display, data );
2027 if (!data->mapped)
2029 map_window( display, data, new_style );
2031 else if ((swp_flags & SWP_STATECHANGED) && (!data->iconic != !(new_style & WS_MINIMIZE)))
2033 data->iconic = (new_style & WS_MINIMIZE) != 0;
2034 TRACE( "changing win %p iconic state to %u\n", data->hwnd, data->iconic );
2035 wine_tsx11_lock();
2036 if (data->iconic)
2037 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
2038 else if (is_window_rect_mapped( rectWindow ))
2039 XMapWindow( display, data->whole_window );
2040 wine_tsx11_unlock();
2041 update_net_wm_states( display, data );
2043 else if (!event_type)
2045 update_net_wm_states( display, data );
2049 wine_tsx11_lock();
2050 XFlush( display ); /* make sure changes are done before we start painting again */
2051 wine_tsx11_unlock();
2055 /**********************************************************************
2056 * SetWindowIcon (X11DRV.@)
2058 * hIcon or hIconSm has changed (or is being initialised for the
2059 * first time). Complete the X11 driver-specific initialisation
2060 * and set the window hints.
2062 * This is not entirely correct, may need to create
2063 * an icon window and set the pixmap as a background
2065 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
2067 Display *display = thread_display();
2068 struct x11drv_win_data *data;
2070 if (type != ICON_BIG) return; /* nothing to do here */
2072 if (!(data = X11DRV_get_win_data( hwnd ))) return;
2073 if (!data->whole_window) return;
2074 if (!data->managed) return;
2076 if (data->wm_hints)
2078 set_icon_hints( display, data, icon );
2079 wine_tsx11_lock();
2080 XSetWMHints( display, data->whole_window, data->wm_hints );
2081 wine_tsx11_unlock();
2086 /***********************************************************************
2087 * SetWindowRgn (X11DRV.@)
2089 * Assign specified region to window (for non-rectangular windows)
2091 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
2093 struct x11drv_win_data *data;
2095 if ((data = X11DRV_get_win_data( hwnd )))
2097 sync_window_region( thread_display(), data, hrgn );
2099 else if (GetWindowThreadProcessId( hwnd, NULL ) != GetCurrentThreadId())
2101 FIXME( "not supported on other thread window %p\n", hwnd );
2102 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2103 return FALSE;
2106 return TRUE;
2110 /***********************************************************************
2111 * is_netwm_supported
2113 static BOOL is_netwm_supported( Display *display, Atom atom )
2115 static Atom *net_supported;
2116 static int net_supported_count = -1;
2117 int i;
2119 wine_tsx11_lock();
2120 if (net_supported_count == -1)
2122 Atom type;
2123 int format;
2124 unsigned long count, remaining;
2126 if (!XGetWindowProperty( display, DefaultRootWindow(display), x11drv_atom(_NET_SUPPORTED), 0,
2127 ~0UL, False, XA_ATOM, &type, &format, &count,
2128 &remaining, (unsigned char **)&net_supported ))
2129 net_supported_count = get_property_size( format, count ) / sizeof(Atom);
2130 else
2131 net_supported_count = 0;
2133 wine_tsx11_unlock();
2135 for (i = 0; i < net_supported_count; i++)
2136 if (net_supported[i] == atom) return TRUE;
2137 return FALSE;
2141 /***********************************************************************
2142 * SysCommand (X11DRV.@)
2144 * Perform WM_SYSCOMMAND handling.
2146 LRESULT X11DRV_SysCommand( HWND hwnd, WPARAM wparam, LPARAM lparam )
2148 WPARAM hittest = wparam & 0x0f;
2149 DWORD dwPoint;
2150 int x, y, dir;
2151 XEvent xev;
2152 Display *display = thread_display();
2153 struct x11drv_win_data *data;
2155 if (!(data = X11DRV_get_win_data( hwnd ))) return -1;
2156 if (!data->whole_window || !data->managed || !data->mapped) return -1;
2158 switch (wparam & 0xfff0)
2160 case SC_MOVE:
2161 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
2162 else dir = _NET_WM_MOVERESIZE_MOVE;
2163 break;
2164 case SC_SIZE:
2165 /* windows without WS_THICKFRAME are not resizable through the window manager */
2166 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_THICKFRAME)) return -1;
2168 switch (hittest)
2170 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
2171 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
2172 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
2173 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
2174 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
2175 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
2176 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
2177 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
2178 default: dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD; break;
2180 break;
2182 case SC_KEYMENU:
2183 /* prevent a simple ALT press+release from activating the system menu,
2184 * as that can get confusing on managed windows */
2185 if ((WCHAR)lparam) return -1; /* got an explicit char */
2186 if (GetMenu( hwnd )) return -1; /* window has a real menu */
2187 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) return -1; /* no system menu */
2188 TRACE( "ignoring SC_KEYMENU wp %lx lp %lx\n", wparam, lparam );
2189 return 0;
2191 default:
2192 return -1;
2195 if (IsZoomed(hwnd)) return -1;
2197 if (!is_netwm_supported( display, x11drv_atom(_NET_WM_MOVERESIZE) ))
2199 TRACE( "_NET_WM_MOVERESIZE not supported\n" );
2200 return -1;
2203 dwPoint = GetMessagePos();
2204 x = (short)LOWORD(dwPoint);
2205 y = (short)HIWORD(dwPoint);
2207 TRACE("hwnd %p, x %d, y %d, dir %d\n", hwnd, x, y, dir);
2209 xev.xclient.type = ClientMessage;
2210 xev.xclient.window = X11DRV_get_whole_window(hwnd);
2211 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
2212 xev.xclient.serial = 0;
2213 xev.xclient.display = display;
2214 xev.xclient.send_event = True;
2215 xev.xclient.format = 32;
2216 xev.xclient.data.l[0] = x; /* x coord */
2217 xev.xclient.data.l[1] = y; /* y coord */
2218 xev.xclient.data.l[2] = dir; /* direction */
2219 xev.xclient.data.l[3] = 1; /* button */
2220 xev.xclient.data.l[4] = 0; /* unused */
2222 /* need to ungrab the pointer that may have been automatically grabbed
2223 * with a ButtonPress event */
2224 wine_tsx11_lock();
2225 XUngrabPointer( display, CurrentTime );
2226 XSendEvent(display, root_window, False, SubstructureNotifyMask, &xev);
2227 wine_tsx11_unlock();
2228 return 0;