push 6fe5edf8439c19d3885814583531c2f2b1495177
[wine/hacks.git] / dlls / winex11.drv / window.c
blobbaede94b17b341d5c3b8535fb7abbdae5d4c601a
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 /* X context to associate a hwnd to an X window */
66 XContext winContext = 0;
68 /* X context to associate a struct x11drv_win_data to an hwnd */
69 static XContext win_data_context;
71 static const char whole_window_prop[] = "__wine_x11_whole_window";
72 static const char client_window_prop[]= "__wine_x11_client_window";
73 static const char icon_window_prop[] = "__wine_x11_icon_window";
74 static const char fbconfig_id_prop[] = "__wine_x11_fbconfig_id";
75 static const char gl_drawable_prop[] = "__wine_x11_gl_drawable";
76 static const char pixmap_prop[] = "__wine_x11_pixmap";
77 static const char managed_prop[] = "__wine_x11_managed";
79 extern int usexcomposite;
81 /***********************************************************************
82 * is_window_managed
84 * Check if a given window should be managed
86 BOOL is_window_managed( HWND hwnd, UINT swp_flags, const RECT *window_rect )
88 DWORD style, ex_style;
90 if (!managed_mode) return FALSE;
92 /* child windows are not managed */
93 style = GetWindowLongW( hwnd, GWL_STYLE );
94 if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD) return FALSE;
95 #ifndef DEFAULTMANAGED
96 /* set to 1 to enable default managed instead of default not managed */
97 #define DEFAULTMANAGED 0
98 #endif
99 if(!DEFAULTMANAGED){
100 /* activated windows are managed */
101 if (!(swp_flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) return TRUE;
102 if (hwnd == GetActiveWindow()) return TRUE;
103 /* windows with caption are managed */
104 if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
105 /* tool windows are not managed */
106 ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
107 if (ex_style & WS_EX_TOOLWINDOW) return FALSE;
108 /* windows with thick frame are managed */
109 if (style & WS_THICKFRAME) return TRUE;
110 /* application windows are managed */
111 if (ex_style & WS_EX_APPWINDOW) return TRUE;
113 if (style & WS_POPUP)
115 /* popup with sysmenu == caption are managed */
116 if (style & WS_SYSMENU) return TRUE;
117 /* full-screen popup windows are managed */
118 if (window_rect->left <= 0 && window_rect->right >= screen_width &&
119 window_rect->top <= 0 && window_rect->bottom >= screen_height)
120 return TRUE;
121 if(DEFAULTMANAGED) {
122 /* all other popups are not managed */
123 return FALSE;
126 if(DEFAULTMANAGED) {
127 /* default: managed */
128 return TRUE;
129 } else {
130 /* default: not managed */
131 return FALSE;
137 /***********************************************************************
138 * X11DRV_is_window_rect_mapped
140 * Check if the X whole window should be mapped based on its rectangle
142 BOOL X11DRV_is_window_rect_mapped( const RECT *rect )
144 /* don't map if rect is off-screen */
145 if (rect->left >= virtual_screen_rect.right ||
146 rect->top >= virtual_screen_rect.bottom ||
147 rect->right <= virtual_screen_rect.left ||
148 rect->bottom <= virtual_screen_rect.top)
149 return FALSE;
151 return TRUE;
155 /***********************************************************************
156 * is_window_resizable
158 * Check if window should be made resizable by the window manager
160 static inline BOOL is_window_resizable( struct x11drv_win_data *data, DWORD style )
162 if (style & WS_THICKFRAME) return TRUE;
163 /* Metacity needs the window to be resizable to make it fullscreen */
164 return (data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
165 data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height);
169 /***********************************************************************
170 * get_mwm_decorations
172 static unsigned long get_mwm_decorations( struct x11drv_win_data *data,
173 DWORD style, DWORD ex_style )
175 unsigned long ret = 0;
177 if (!decorated_mode) return 0;
179 if (IsRectEmpty( &data->window_rect )) return 0;
181 if (ex_style & WS_EX_TOOLWINDOW) return 0;
183 if ((style & WS_CAPTION) == WS_CAPTION)
185 ret |= MWM_DECOR_TITLE | MWM_DECOR_BORDER;
186 if (style & WS_SYSMENU) ret |= MWM_DECOR_MENU;
187 if (style & WS_MINIMIZEBOX) ret |= MWM_DECOR_MINIMIZE;
188 if (style & WS_MAXIMIZEBOX) ret |= MWM_DECOR_MAXIMIZE;
190 if (ex_style & WS_EX_DLGMODALFRAME) ret |= MWM_DECOR_BORDER;
191 else if (style & WS_THICKFRAME) ret |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
192 else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) ret |= MWM_DECOR_BORDER;
193 return ret;
197 /***********************************************************************
198 * get_x11_rect_offset
200 * Helper for X11DRV_window_to_X_rect and X11DRV_X_to_window_rect.
202 static void get_x11_rect_offset( struct x11drv_win_data *data, RECT *rect )
204 DWORD style, ex_style, style_mask = 0, ex_style_mask = 0;
205 unsigned long decor;
207 rect->top = rect->bottom = rect->left = rect->right = 0;
209 style = GetWindowLongW( data->hwnd, GWL_STYLE );
210 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
211 decor = get_mwm_decorations( data, style, ex_style );
213 if (decor & MWM_DECOR_TITLE) style_mask |= WS_CAPTION;
214 if (decor & MWM_DECOR_BORDER)
216 style_mask |= WS_DLGFRAME | WS_THICKFRAME;
217 ex_style_mask |= WS_EX_DLGMODALFRAME;
220 AdjustWindowRectEx( rect, style & style_mask, FALSE, ex_style & ex_style_mask );
224 /***********************************************************************
225 * get_window_attributes
227 * Fill the window attributes structure for an X window.
229 static int get_window_attributes( Display *display, struct x11drv_win_data *data,
230 XSetWindowAttributes *attr )
232 attr->override_redirect = !data->managed;
233 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
234 attr->save_under = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
235 attr->cursor = x11drv_thread_data()->cursor;
236 attr->bit_gravity = NorthWestGravity;
237 attr->backing_store = NotUseful;
238 attr->event_mask = (ExposureMask | PointerMotionMask |
239 ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
240 KeyPressMask | KeyReleaseMask | FocusChangeMask | KeymapStateMask);
241 if (data->managed) attr->event_mask |= StructureNotifyMask | PropertyChangeMask;
243 return (CWOverrideRedirect | CWSaveUnder | CWColormap | CWCursor |
244 CWEventMask | CWBitGravity | CWBackingStore);
248 /***********************************************************************
249 * create_client_window
251 static Window create_client_window( Display *display, struct x11drv_win_data *data, XVisualInfo *vis )
253 int cx, cy, mask;
254 XSetWindowAttributes attr;
255 Window client;
257 attr.bit_gravity = NorthWestGravity;
258 attr.win_gravity = NorthWestGravity;
259 attr.backing_store = NotUseful;
260 attr.event_mask = (ExposureMask | PointerMotionMask |
261 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
262 mask = CWEventMask | CWBitGravity | CWWinGravity | CWBackingStore;
264 if ((cx = data->client_rect.right - data->client_rect.left) <= 0) cx = 1;
265 if ((cy = data->client_rect.bottom - data->client_rect.top) <= 0) cy = 1;
267 wine_tsx11_lock();
269 if (vis)
271 attr.colormap = XCreateColormap( display, root_window, vis->visual,
272 (vis->class == PseudoColor || vis->class == GrayScale ||
273 vis->class == DirectColor) ? AllocAll : AllocNone );
274 mask |= CWColormap;
277 client = XCreateWindow( display, data->whole_window,
278 data->client_rect.left - data->whole_rect.left,
279 data->client_rect.top - data->whole_rect.top,
280 cx, cy, 0, screen_depth, InputOutput,
281 vis ? vis->visual : visual, mask, &attr );
282 if (!client)
284 wine_tsx11_unlock();
285 return 0;
288 if (data->client_window)
290 struct x11drv_thread_data *thread_data = x11drv_thread_data();
291 if (thread_data->cursor_window == data->client_window) thread_data->cursor_window = None;
292 XDeleteContext( display, data->client_window, winContext );
293 XDestroyWindow( display, data->client_window );
295 data->client_window = client;
297 if (data->colormap) XFreeColormap( display, data->colormap );
298 data->colormap = vis ? attr.colormap : 0;
300 XMapWindow( display, data->client_window );
301 XSaveContext( display, data->client_window, winContext, (char *)data->hwnd );
302 wine_tsx11_unlock();
304 SetPropA( data->hwnd, client_window_prop, (HANDLE)data->client_window );
305 return data->client_window;
309 /***********************************************************************
310 * X11DRV_sync_window_style
312 * Change the X window attributes when the window style has changed.
314 void X11DRV_sync_window_style( Display *display, struct x11drv_win_data *data )
316 if (data->whole_window != root_window)
318 XSetWindowAttributes attr;
319 int mask = get_window_attributes( display, data, &attr );
321 wine_tsx11_lock();
322 XChangeWindowAttributes( display, data->whole_window, mask, &attr );
323 wine_tsx11_unlock();
328 /***********************************************************************
329 * sync_window_region
331 * Update the X11 window region.
333 static void sync_window_region( Display *display, struct x11drv_win_data *data, HRGN hrgn )
335 #ifdef HAVE_LIBXSHAPE
336 if (!data->whole_window) return;
338 if (!hrgn)
340 wine_tsx11_lock();
341 XShapeCombineMask( display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
342 wine_tsx11_unlock();
344 else
346 RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
347 if (pRegionData)
349 wine_tsx11_lock();
350 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
351 data->window_rect.left - data->whole_rect.left,
352 data->window_rect.top - data->whole_rect.top,
353 (XRectangle *)pRegionData->Buffer,
354 pRegionData->rdh.nCount, ShapeSet, YXBanded );
355 wine_tsx11_unlock();
356 HeapFree(GetProcessHeap(), 0, pRegionData);
359 #endif /* HAVE_LIBXSHAPE */
363 /***********************************************************************
364 * sync_window_text
366 static void sync_window_text( Display *display, Window win, const WCHAR *text )
368 UINT count;
369 char *buffer, *utf8_buffer;
370 XTextProperty prop;
372 /* allocate new buffer for window text */
373 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
374 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count ))) return;
375 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
377 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
378 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
380 HeapFree( GetProcessHeap(), 0, buffer );
381 return;
383 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
385 wine_tsx11_lock();
386 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
388 XSetWMName( display, win, &prop );
389 XSetWMIconName( display, win, &prop );
390 XFree( prop.value );
393 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
394 according to the standard
395 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
397 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
398 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
399 wine_tsx11_unlock();
401 HeapFree( GetProcessHeap(), 0, utf8_buffer );
402 HeapFree( GetProcessHeap(), 0, buffer );
406 /***********************************************************************
407 * X11DRV_set_win_format
409 BOOL X11DRV_set_win_format( HWND hwnd, XID fbconfig_id )
411 Display *display = thread_display();
412 struct x11drv_win_data *data;
413 XVisualInfo *vis;
414 int w, h;
416 if (!(data = X11DRV_get_win_data(hwnd)) &&
417 !(data = X11DRV_create_win_data(hwnd))) return FALSE;
419 if (data->fbconfig_id) return FALSE; /* can't change it twice */
421 wine_tsx11_lock();
422 vis = visual_from_fbconfig_id(fbconfig_id);
423 wine_tsx11_unlock();
424 if (!vis) return FALSE;
426 if (data->whole_window)
428 Window client = data->client_window;
430 if (vis->visualid != XVisualIDFromVisual(visual))
432 client = create_client_window( display, data, vis );
433 TRACE( "re-created client window %lx for %p fbconfig %lx\n", client, data->hwnd, fbconfig_id );
435 wine_tsx11_lock();
436 XFree(vis);
437 wine_tsx11_unlock();
438 if (client) goto done;
439 return FALSE;
442 w = data->client_rect.right - data->client_rect.left;
443 h = data->client_rect.bottom - data->client_rect.top;
445 if(w <= 0) w = 1;
446 if(h <= 0) h = 1;
448 #ifdef SONAME_LIBXCOMPOSITE
449 if(usexcomposite)
451 XSetWindowAttributes attrib;
452 Window parent = X11DRV_get_whole_window( GetAncestor( hwnd, GA_ROOT ));
454 if (!parent) parent = root_window;
455 wine_tsx11_lock();
456 data->colormap = XCreateColormap(display, parent, vis->visual,
457 (vis->class == PseudoColor ||
458 vis->class == GrayScale ||
459 vis->class == DirectColor) ?
460 AllocAll : AllocNone);
461 attrib.override_redirect = True;
462 attrib.colormap = data->colormap;
463 XInstallColormap(gdi_display, attrib.colormap);
465 data->gl_drawable = XCreateWindow(display, parent, -w, 0, w, h, 0,
466 vis->depth, InputOutput, vis->visual,
467 CWColormap | CWOverrideRedirect,
468 &attrib);
469 if(data->gl_drawable)
471 pXCompositeRedirectWindow(display, data->gl_drawable,
472 CompositeRedirectManual);
473 XMapWindow(display, data->gl_drawable);
475 XFree(vis);
476 wine_tsx11_unlock();
478 else
479 #endif
481 WARN("XComposite is not available, using GLXPixmap hack\n");
483 wine_tsx11_lock();
484 data->pixmap = XCreatePixmap(display, root_window, w, h, vis->depth);
485 if(!data->pixmap)
487 XFree(vis);
488 wine_tsx11_unlock();
489 return FALSE;
492 data->gl_drawable = create_glxpixmap(display, vis, data->pixmap);
493 if(!data->gl_drawable)
495 XFreePixmap(display, data->pixmap);
496 data->pixmap = 0;
498 XFree(vis);
499 wine_tsx11_unlock();
500 if (data->pixmap) SetPropA(hwnd, pixmap_prop, (HANDLE)data->pixmap);
503 if (!data->gl_drawable) return FALSE;
505 TRACE("Created GL drawable 0x%lx, using FBConfigID 0x%lx\n",
506 data->gl_drawable, fbconfig_id);
507 SetPropA(hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
509 done:
510 data->fbconfig_id = fbconfig_id;
511 SetPropA(hwnd, fbconfig_id_prop, (HANDLE)data->fbconfig_id);
512 wine_tsx11_lock();
513 XFlush( display );
514 wine_tsx11_unlock();
515 WIN_invalidate_dce( hwnd, NULL );
516 return TRUE;
519 /***********************************************************************
520 * sync_gl_drawable
522 static void sync_gl_drawable(Display *display, struct x11drv_win_data *data)
524 int w = data->client_rect.right - data->client_rect.left;
525 int h = data->client_rect.bottom - data->client_rect.top;
526 XVisualInfo *vis;
527 Drawable glxp;
528 Pixmap pix;
530 if (w <= 0) w = 1;
531 if (h <= 0) h = 1;
533 TRACE("Resizing GL drawable 0x%lx to %dx%d\n", data->gl_drawable, w, h);
534 #ifdef SONAME_LIBXCOMPOSITE
535 if(usexcomposite)
537 wine_tsx11_lock();
538 XMoveResizeWindow(display, data->gl_drawable, -w, 0, w, h);
539 wine_tsx11_unlock();
540 return;
542 #endif
544 wine_tsx11_lock();
546 vis = visual_from_fbconfig_id(data->fbconfig_id);
547 if(!vis)
549 wine_tsx11_unlock();
550 return;
553 pix = XCreatePixmap(display, root_window, w, h, vis->depth);
554 if(!pix)
556 ERR("Failed to create pixmap for offscreen rendering\n");
557 XFree(vis);
558 wine_tsx11_unlock();
559 return;
562 glxp = create_glxpixmap(display, vis, pix);
563 if(!glxp)
565 ERR("Failed to create drawable for offscreen rendering\n");
566 XFreePixmap(display, pix);
567 XFree(vis);
568 wine_tsx11_unlock();
569 return;
572 XFree(vis);
574 mark_drawable_dirty(data->gl_drawable, glxp);
576 XFreePixmap(display, data->pixmap);
577 destroy_glxpixmap(display, data->gl_drawable);
579 data->pixmap = pix;
580 data->gl_drawable = glxp;
582 XFlush( display );
583 wine_tsx11_unlock();
585 SetPropA(data->hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
586 SetPropA(data->hwnd, pixmap_prop, (HANDLE)data->pixmap);
590 /***********************************************************************
591 * get_window_changes
593 * fill the window changes structure
595 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
597 int mask = 0;
599 if (old->right - old->left != new->right - new->left )
601 if ((changes->width = new->right - new->left) <= 0) changes->width = 1;
602 mask |= CWWidth;
604 if (old->bottom - old->top != new->bottom - new->top)
606 if ((changes->height = new->bottom - new->top) <= 0) changes->height = 1;
607 mask |= CWHeight;
609 if (old->left != new->left)
611 changes->x = new->left;
612 mask |= CWX;
614 if (old->top != new->top)
616 changes->y = new->top;
617 mask |= CWY;
619 return mask;
623 /***********************************************************************
624 * create_icon_window
626 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
628 XSetWindowAttributes attr;
630 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
631 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
632 attr.bit_gravity = NorthWestGravity;
633 attr.backing_store = NotUseful/*WhenMapped*/;
634 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
636 wine_tsx11_lock();
637 data->icon_window = XCreateWindow( display, root_window, 0, 0,
638 GetSystemMetrics( SM_CXICON ),
639 GetSystemMetrics( SM_CYICON ),
640 0, screen_depth,
641 InputOutput, visual,
642 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
643 XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
644 XFlush( display ); /* make sure the window exists before we start painting to it */
645 wine_tsx11_unlock();
647 TRACE( "created %lx\n", data->icon_window );
648 SetPropA( data->hwnd, icon_window_prop, (HANDLE)data->icon_window );
649 return data->icon_window;
654 /***********************************************************************
655 * destroy_icon_window
657 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
659 if (!data->icon_window) return;
660 if (x11drv_thread_data()->cursor_window == data->icon_window)
661 x11drv_thread_data()->cursor_window = None;
662 wine_tsx11_lock();
663 XDeleteContext( display, data->icon_window, winContext );
664 XDestroyWindow( display, data->icon_window );
665 data->icon_window = 0;
666 wine_tsx11_unlock();
667 RemovePropA( data->hwnd, icon_window_prop );
671 /***********************************************************************
672 * set_icon_hints
674 * Set the icon wm hints
676 static void set_icon_hints( Display *display, struct x11drv_win_data *data, HICON hIcon )
678 XWMHints *hints = data->wm_hints;
680 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
681 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
682 data->hWMIconBitmap = 0;
683 data->hWMIconMask = 0;
685 if (!hIcon)
687 if (!data->icon_window) create_icon_window( display, data );
688 hints->icon_window = data->icon_window;
689 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
691 else
693 HBITMAP hbmOrig;
694 RECT rcMask;
695 BITMAP bmMask;
696 ICONINFO ii;
697 HDC hDC;
699 GetIconInfo(hIcon, &ii);
701 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
702 rcMask.top = 0;
703 rcMask.left = 0;
704 rcMask.right = bmMask.bmWidth;
705 rcMask.bottom = bmMask.bmHeight;
707 hDC = CreateCompatibleDC(0);
708 hbmOrig = SelectObject(hDC, ii.hbmMask);
709 InvertRect(hDC, &rcMask);
710 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
711 SelectObject(hDC, hbmOrig);
712 DeleteDC(hDC);
714 data->hWMIconBitmap = ii.hbmColor;
715 data->hWMIconMask = ii.hbmMask;
717 hints->icon_pixmap = X11DRV_get_pixmap(data->hWMIconBitmap);
718 hints->icon_mask = X11DRV_get_pixmap(data->hWMIconMask);
719 destroy_icon_window( display, data );
720 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
725 /***********************************************************************
726 * set_size_hints
728 * set the window size hints
730 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
732 XSizeHints* size_hints;
734 if (!(size_hints = XAllocSizeHints())) return;
736 /* don't update size hints if window is not in normal state */
737 if (!(style & (WS_MINIMIZE | WS_MAXIMIZE)))
739 if (data->hwnd != GetDesktopWindow()) /* don't force position of desktop */
741 size_hints->win_gravity = StaticGravity;
742 size_hints->x = data->whole_rect.left;
743 size_hints->y = data->whole_rect.top;
744 size_hints->flags |= PWinGravity | PPosition;
747 if (!is_window_resizable( data, style ))
749 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
750 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
751 size_hints->min_width = size_hints->max_width;
752 size_hints->min_height = size_hints->max_height;
753 size_hints->flags |= PMinSize | PMaxSize;
756 XSetWMNormalHints( display, data->whole_window, size_hints );
757 XFree( size_hints );
761 /***********************************************************************
762 * get_process_name
764 * get the name of the current process for setting class hints
766 static char *get_process_name(void)
768 static char *name;
770 if (!name)
772 WCHAR module[MAX_PATH];
773 DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
774 if (len && len < MAX_PATH)
776 char *ptr;
777 WCHAR *p, *appname = module;
779 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
780 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
781 len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
782 if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
784 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
785 name = ptr;
789 return name;
793 /***********************************************************************
794 * set_initial_wm_hints
796 * Set the window manager hints that don't change over the lifetime of a window.
798 static void set_initial_wm_hints( Display *display, struct x11drv_win_data *data )
800 long i;
801 Atom protocols[3];
802 Atom dndVersion = 4;
803 XClassHint *class_hints;
804 char *process_name = get_process_name();
806 wine_tsx11_lock();
808 /* wm protocols */
809 i = 0;
810 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
811 protocols[i++] = x11drv_atom(_NET_WM_PING);
812 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
813 XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
814 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
816 /* class hints */
817 if ((class_hints = XAllocClassHint()))
819 static char wine[] = "Wine";
821 class_hints->res_name = process_name;
822 class_hints->res_class = wine;
823 XSetClassHint( display, data->whole_window, class_hints );
824 XFree( class_hints );
827 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
828 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
829 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
830 i = getpid();
831 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
832 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
834 XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
835 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
837 data->wm_hints = XAllocWMHints();
838 wine_tsx11_unlock();
840 if (data->wm_hints)
842 HICON icon = (HICON)SendMessageW( data->hwnd, WM_GETICON, ICON_BIG, 0 );
843 if (!icon) icon = (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON );
844 data->wm_hints->flags = 0;
845 set_icon_hints( display, data, icon );
850 /***********************************************************************
851 * X11DRV_set_wm_hints
853 * Set the window manager hints for a newly-created window
855 void X11DRV_set_wm_hints( Display *display, struct x11drv_win_data *data )
857 Window group_leader;
858 Atom window_type;
859 MwmHints mwm_hints;
860 DWORD style, ex_style;
861 HWND owner;
863 if (data->hwnd == GetDesktopWindow())
865 /* force some styles for the desktop to get the correct decorations */
866 style = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
867 ex_style = WS_EX_APPWINDOW;
868 owner = 0;
870 else
872 style = GetWindowLongW( data->hwnd, GWL_STYLE );
873 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
874 owner = GetWindow( data->hwnd, GW_OWNER );
877 /* transient for hint */
878 if (owner)
880 Window owner_win = X11DRV_get_whole_window( owner );
881 wine_tsx11_lock();
882 XSetTransientForHint( display, data->whole_window, owner_win );
883 wine_tsx11_unlock();
884 group_leader = owner_win;
886 else group_leader = data->whole_window;
888 wine_tsx11_lock();
890 /* size hints */
891 set_size_hints( display, data, style );
893 /* set the WM_WINDOW_TYPE */
894 if (style & WS_THICKFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
895 else if (ex_style & WS_EX_APPWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
896 else if (style & WS_DLGFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
897 else if (ex_style & WS_EX_DLGMODALFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
898 #if 0 /* many window managers don't handle utility windows very well */
899 else if (ex_style & WS_EX_TOOLWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
900 #endif
901 else window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
903 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
904 XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
906 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
907 mwm_hints.decorations = get_mwm_decorations( data, style, ex_style );
908 mwm_hints.functions = MWM_FUNC_MOVE;
909 if (is_window_resizable( data, style )) mwm_hints.functions |= MWM_FUNC_RESIZE;
910 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
911 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
912 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
914 XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
915 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
916 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
918 /* wm hints */
919 if (data->wm_hints)
921 data->wm_hints->flags |= InputHint | StateHint | WindowGroupHint;
922 data->wm_hints->input = !(style & WS_DISABLED);
923 data->wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
924 data->wm_hints->window_group = group_leader;
925 XSetWMHints( display, data->whole_window, data->wm_hints );
928 wine_tsx11_unlock();
932 /***********************************************************************
933 * X11DRV_window_to_X_rect
935 * Convert a rect from client to X window coordinates
937 void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
939 RECT rc;
941 if (!data->managed) return;
942 if (IsRectEmpty( rect )) return;
944 get_x11_rect_offset( data, &rc );
946 rect->left -= rc.left;
947 rect->right -= rc.right;
948 rect->top -= rc.top;
949 rect->bottom -= rc.bottom;
950 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
951 if (rect->left >= rect->right) rect->right = rect->left + 1;
955 /***********************************************************************
956 * X11DRV_X_to_window_rect
958 * Opposite of X11DRV_window_to_X_rect
960 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
962 RECT rc;
964 if (!data->managed) return;
965 if (IsRectEmpty( rect )) return;
967 get_x11_rect_offset( data, &rc );
969 rect->left += rc.left;
970 rect->right += rc.right;
971 rect->top += rc.top;
972 rect->bottom += rc.bottom;
973 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
974 if (rect->left >= rect->right) rect->right = rect->left + 1;
978 /***********************************************************************
979 * X11DRV_sync_window_position
981 * Synchronize the X window position with the Windows one
983 void X11DRV_sync_window_position( Display *display, struct x11drv_win_data *data,
984 UINT swp_flags, const RECT *old_client_rect,
985 const RECT *old_whole_rect )
987 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
988 XWindowChanges changes;
989 int mask = CWWidth | CWHeight;
991 if (data->managed && data->iconic) return;
993 if ((changes.width = data->whole_rect.right - data->whole_rect.left) <= 0) changes.width = 1;
994 if ((changes.height = data->whole_rect.bottom - data->whole_rect.top) <= 0) changes.height = 1;
996 /* only the size is allowed to change for the desktop window */
997 if (data->whole_window != root_window)
999 changes.x = data->whole_rect.left - virtual_screen_rect.left;
1000 changes.y = data->whole_rect.top - virtual_screen_rect.top;
1001 mask |= CWX | CWY;
1004 if (!(swp_flags & SWP_NOZORDER))
1006 /* find window that this one must be after */
1007 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
1008 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
1009 prev = GetWindow( prev, GW_HWNDPREV );
1010 if (!prev) /* top child */
1012 changes.stack_mode = Above;
1013 mask |= CWStackMode;
1015 /* should use stack_mode Below but most window managers don't get it right */
1016 /* and Above with a sibling doesn't work so well either, so we ignore it */
1019 TRACE( "setting win %p/%lx pos %d,%d,%dx%d after %lx changes=%x\n",
1020 data->hwnd, data->whole_window, data->whole_rect.left, data->whole_rect.top,
1021 data->whole_rect.right - data->whole_rect.left,
1022 data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
1024 wine_tsx11_lock();
1025 set_size_hints( display, data, style );
1026 XReconfigureWMWindow( display, data->whole_window,
1027 DefaultScreen(display), mask, &changes );
1028 wine_tsx11_unlock();
1032 /***********************************************************************
1033 * X11DRV_sync_client_position
1035 * Synchronize the X client window position with the Windows one
1037 void X11DRV_sync_client_position( Display *display, struct x11drv_win_data *data,
1038 UINT swp_flags, const RECT *old_client_rect,
1039 const RECT *old_whole_rect )
1041 int mask;
1042 XWindowChanges changes;
1043 RECT old = *old_client_rect;
1044 RECT new = data->client_rect;
1046 OffsetRect( &old, -old_whole_rect->left, -old_whole_rect->top );
1047 OffsetRect( &new, -data->whole_rect.left, -data->whole_rect.top );
1048 if (!(mask = get_window_changes( &changes, &old, &new ))) return;
1050 if (data->client_window)
1052 TRACE( "setting client win %lx pos %d,%d,%dx%d changes=%x\n",
1053 data->client_window, new.left, new.top,
1054 new.right - new.left, new.bottom - new.top, mask );
1055 wine_tsx11_lock();
1056 XConfigureWindow( display, data->client_window, mask, &changes );
1057 wine_tsx11_unlock();
1060 if (data->gl_drawable && (mask & (CWWidth|CWHeight))) sync_gl_drawable( display, data );
1064 /**********************************************************************
1065 * create_whole_window
1067 * Create the whole X window for a given window
1069 static Window create_whole_window( Display *display, struct x11drv_win_data *data )
1071 int cx, cy, mask;
1072 XSetWindowAttributes attr;
1073 WCHAR text[1024];
1074 HRGN hrgn;
1076 if (!(cx = data->window_rect.right - data->window_rect.left)) cx = 1;
1077 if (!(cy = data->window_rect.bottom - data->window_rect.top)) cy = 1;
1079 if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1081 TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1082 data->managed = TRUE;
1083 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1086 mask = get_window_attributes( display, data, &attr );
1088 wine_tsx11_lock();
1090 data->whole_rect = data->window_rect;
1091 data->whole_window = XCreateWindow( display, root_window,
1092 data->window_rect.left - virtual_screen_rect.left,
1093 data->window_rect.top - virtual_screen_rect.top,
1094 cx, cy, 0, screen_depth, InputOutput,
1095 visual, mask, &attr );
1097 if (data->whole_window) XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
1098 wine_tsx11_unlock();
1100 if (!data->whole_window) return 0;
1102 if (!create_client_window( display, data, NULL ))
1104 wine_tsx11_lock();
1105 XDeleteContext( display, data->whole_window, winContext );
1106 XDestroyWindow( display, data->whole_window );
1107 data->whole_window = 0;
1108 wine_tsx11_unlock();
1109 return 0;
1112 set_initial_wm_hints( display, data );
1113 X11DRV_set_wm_hints( display, data );
1115 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1117 /* set the window text */
1118 if (!InternalGetWindowText( data->hwnd, text, sizeof(text)/sizeof(WCHAR) )) text[0] = 0;
1119 sync_window_text( display, data->whole_window, text );
1121 /* set the window region */
1122 if ((hrgn = CreateRectRgn( 0, 0, 0, 0 )))
1124 if (GetWindowRgn( data->hwnd, hrgn ) != ERROR) sync_window_region( display, data, hrgn );
1125 DeleteObject( hrgn );
1127 wine_tsx11_lock();
1128 XFlush( display ); /* make sure the window exists before we start painting to it */
1129 wine_tsx11_unlock();
1130 return data->whole_window;
1134 /**********************************************************************
1135 * destroy_whole_window
1137 * Destroy the whole X window for a given window.
1139 static void destroy_whole_window( Display *display, struct x11drv_win_data *data, BOOL already_destroyed )
1141 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1143 if (!data->whole_window) return;
1145 TRACE( "win %p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1146 if (thread_data->cursor_window == data->whole_window ||
1147 thread_data->cursor_window == data->client_window)
1148 thread_data->cursor_window = None;
1149 wine_tsx11_lock();
1150 XDeleteContext( display, data->whole_window, winContext );
1151 XDeleteContext( display, data->client_window, winContext );
1152 if (!already_destroyed) XDestroyWindow( display, data->whole_window );
1153 data->whole_window = data->client_window = 0;
1154 data->wm_state = WithdrawnState;
1155 data->net_wm_state = 0;
1156 data->mapped = FALSE;
1157 if (data->xic)
1159 XUnsetICFocus( data->xic );
1160 XDestroyIC( data->xic );
1161 data->xic = 0;
1163 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1164 XFlush( display );
1165 XFree( data->wm_hints );
1166 data->wm_hints = NULL;
1167 wine_tsx11_unlock();
1168 RemovePropA( data->hwnd, whole_window_prop );
1169 RemovePropA( data->hwnd, client_window_prop );
1173 /*****************************************************************
1174 * SetWindowText (X11DRV.@)
1176 void X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1178 Display *display = thread_display();
1179 Window win;
1181 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(display))
1182 sync_window_text( display, win, text );
1186 /***********************************************************************
1187 * DestroyWindow (X11DRV.@)
1189 void X11DRV_DestroyWindow( HWND hwnd )
1191 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1192 Display *display = thread_data->display;
1193 struct x11drv_win_data *data;
1195 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1197 if (data->pixmap)
1199 destroy_glxpixmap(display, data->gl_drawable);
1200 wine_tsx11_lock();
1201 XFreePixmap(display, data->pixmap);
1202 wine_tsx11_unlock();
1204 else if (data->gl_drawable)
1206 wine_tsx11_lock();
1207 XDestroyWindow(display, data->gl_drawable);
1208 wine_tsx11_unlock();
1211 destroy_whole_window( display, data, FALSE );
1212 destroy_icon_window( display, data );
1214 if (data->colormap)
1216 wine_tsx11_lock();
1217 XFreeColormap( display, data->colormap );
1218 wine_tsx11_unlock();
1221 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1222 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
1223 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
1224 wine_tsx11_lock();
1225 XDeleteContext( display, (XID)hwnd, win_data_context );
1226 wine_tsx11_unlock();
1227 HeapFree( GetProcessHeap(), 0, data );
1231 /***********************************************************************
1232 * X11DRV_DestroyNotify
1234 void X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1236 Display *display = event->xdestroywindow.display;
1237 struct x11drv_win_data *data;
1239 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1241 FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1242 destroy_whole_window( display, data, TRUE );
1246 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
1248 struct x11drv_win_data *data;
1250 if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
1252 data->hwnd = hwnd;
1253 wine_tsx11_lock();
1254 if (!winContext) winContext = XUniqueContext();
1255 if (!win_data_context) win_data_context = XUniqueContext();
1256 XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
1257 wine_tsx11_unlock();
1259 return data;
1263 /* initialize the desktop window id in the desktop manager process */
1264 static struct x11drv_win_data *create_desktop_win_data( Display *display, HWND hwnd )
1266 struct x11drv_win_data *data;
1267 VisualID visualid;
1269 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1270 wine_tsx11_lock();
1271 visualid = XVisualIDFromVisual(visual);
1272 wine_tsx11_unlock();
1273 data->whole_window = data->client_window = root_window;
1274 data->managed = TRUE;
1275 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1276 SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
1277 SetPropA( data->hwnd, client_window_prop, (HANDLE)root_window );
1278 set_initial_wm_hints( display, data );
1279 return data;
1282 /**********************************************************************
1283 * CreateDesktopWindow (X11DRV.@)
1285 BOOL X11DRV_CreateDesktopWindow( HWND hwnd )
1287 unsigned int width, height;
1289 /* retrieve the real size of the desktop */
1290 SERVER_START_REQ( get_window_rectangles )
1292 req->handle = hwnd;
1293 wine_server_call( req );
1294 width = reply->window.right - reply->window.left;
1295 height = reply->window.bottom - reply->window.top;
1297 SERVER_END_REQ;
1299 if (!width && !height) /* not initialized yet */
1301 SERVER_START_REQ( set_window_pos )
1303 req->handle = hwnd;
1304 req->previous = 0;
1305 req->flags = SWP_NOZORDER;
1306 req->window.left = virtual_screen_rect.left;
1307 req->window.top = virtual_screen_rect.top;
1308 req->window.right = virtual_screen_rect.right;
1309 req->window.bottom = virtual_screen_rect.bottom;
1310 req->client = req->window;
1311 wine_server_call( req );
1313 SERVER_END_REQ;
1315 else
1317 Window win = (Window)GetPropA( hwnd, whole_window_prop );
1318 if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1320 return TRUE;
1324 /**********************************************************************
1325 * CreateWindow (X11DRV.@)
1327 BOOL X11DRV_CreateWindow( HWND hwnd )
1329 Display *display = thread_display();
1331 if (hwnd == GetDesktopWindow() && root_window != DefaultRootWindow( display ))
1333 /* the desktop win data can't be created lazily */
1334 if (!create_desktop_win_data( display, hwnd )) return FALSE;
1336 return TRUE;
1340 /***********************************************************************
1341 * X11DRV_get_win_data
1343 * Return the X11 data structure associated with a window.
1345 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1347 char *data;
1349 if (!hwnd || XFindContext( thread_display(), (XID)hwnd, win_data_context, &data )) data = NULL;
1350 return (struct x11drv_win_data *)data;
1354 /***********************************************************************
1355 * X11DRV_create_win_data
1357 * Create an X11 data window structure for an existing window.
1359 struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd )
1361 Display *display = thread_display();
1362 struct x11drv_win_data *data;
1363 HWND parent;
1365 if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL; /* desktop */
1366 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1368 GetWindowRect( hwnd, &data->window_rect );
1369 MapWindowPoints( 0, parent, (POINT *)&data->window_rect, 2 );
1370 data->whole_rect = data->window_rect;
1371 GetClientRect( hwnd, &data->client_rect );
1372 MapWindowPoints( hwnd, parent, (POINT *)&data->client_rect, 2 );
1374 if (parent == GetDesktopWindow())
1376 if (!create_whole_window( display, data ))
1378 HeapFree( GetProcessHeap(), 0, data );
1379 return NULL;
1381 TRACE( "win %p/%lx/%lx window %s whole %s client %s\n",
1382 hwnd, data->whole_window, data->client_window, wine_dbgstr_rect( &data->window_rect ),
1383 wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
1385 return data;
1389 /***********************************************************************
1390 * X11DRV_get_whole_window
1392 * Return the X window associated with the full area of a window
1394 Window X11DRV_get_whole_window( HWND hwnd )
1396 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1398 if (!data)
1400 if (hwnd == GetDesktopWindow()) return root_window;
1401 return (Window)GetPropA( hwnd, whole_window_prop );
1403 return data->whole_window;
1407 /***********************************************************************
1408 * X11DRV_get_client_window
1410 * Return the X window associated with the client area of a window
1412 Window X11DRV_get_client_window( HWND hwnd )
1414 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1416 if (!data)
1418 if (hwnd == GetDesktopWindow()) return root_window;
1419 return (Window)GetPropA( hwnd, client_window_prop );
1421 return data->client_window;
1425 /***********************************************************************
1426 * X11DRV_get_ic
1428 * Return the X input context associated with a window
1430 XIC X11DRV_get_ic( HWND hwnd )
1432 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1433 XIM xim;
1435 if (!data) return 0;
1436 if (data->xic) return data->xic;
1437 if (!(xim = x11drv_thread_data()->xim)) return 0;
1438 return X11DRV_CreateIC( xim, data );
1442 /***********************************************************************
1443 * X11DRV_GetDC (X11DRV.@)
1445 void X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
1446 const RECT *top_rect, DWORD flags )
1448 struct x11drv_escape_set_drawable escape;
1449 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1451 escape.code = X11DRV_SET_DRAWABLE;
1452 escape.mode = IncludeInferiors;
1453 escape.fbconfig_id = 0;
1454 escape.gl_drawable = 0;
1455 escape.pixmap = 0;
1457 if (top == hwnd && data && IsIconic( hwnd ) && data->icon_window)
1459 escape.drawable = data->icon_window;
1461 else if (top == hwnd && (flags & DCX_WINDOW))
1463 escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
1465 else
1467 escape.drawable = X11DRV_get_client_window( top );
1468 escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
1469 escape.gl_drawable = data ? data->gl_drawable : (Drawable)GetPropA( hwnd, gl_drawable_prop );
1470 escape.pixmap = data ? data->pixmap : (Pixmap)GetPropA( hwnd, pixmap_prop );
1471 if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
1474 escape.dc_rect.left = win_rect->left - top_rect->left;
1475 escape.dc_rect.top = win_rect->top - top_rect->top;
1476 escape.dc_rect.right = win_rect->right - top_rect->left;
1477 escape.dc_rect.bottom = win_rect->bottom - top_rect->top;
1478 escape.drawable_rect.left = top_rect->left;
1479 escape.drawable_rect.top = top_rect->top;
1480 escape.drawable_rect.right = top_rect->right;
1481 escape.drawable_rect.bottom = top_rect->bottom;
1483 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1487 /***********************************************************************
1488 * X11DRV_ReleaseDC (X11DRV.@)
1490 void X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
1492 struct x11drv_escape_set_drawable escape;
1494 escape.code = X11DRV_SET_DRAWABLE;
1495 escape.drawable = root_window;
1496 escape.mode = IncludeInferiors;
1497 escape.drawable_rect = virtual_screen_rect;
1498 SetRect( &escape.dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
1499 virtual_screen_rect.bottom - virtual_screen_rect.top );
1500 escape.fbconfig_id = 0;
1501 escape.gl_drawable = 0;
1502 escape.pixmap = 0;
1503 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1507 /***********************************************************************
1508 * SetCapture (X11DRV.@)
1510 void X11DRV_SetCapture( HWND hwnd, UINT flags )
1512 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1514 if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
1516 if (hwnd)
1518 Window grab_win = X11DRV_get_client_window( GetAncestor( hwnd, GA_ROOT ) );
1520 if (!grab_win) return;
1521 wine_tsx11_lock();
1522 XFlush( gdi_display );
1523 XGrabPointer( thread_data->display, grab_win, False,
1524 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1525 GrabModeAsync, GrabModeAsync, None, None, CurrentTime );
1526 wine_tsx11_unlock();
1527 thread_data->grab_window = grab_win;
1529 else /* release capture */
1531 wine_tsx11_lock();
1532 XFlush( gdi_display );
1533 XUngrabPointer( thread_data->display, CurrentTime );
1534 wine_tsx11_unlock();
1535 thread_data->grab_window = None;
1540 /*****************************************************************
1541 * SetParent (X11DRV.@)
1543 void X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
1545 Display *display = thread_display();
1546 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1548 if (!data) return;
1549 if (parent == old_parent) return;
1551 if (parent != GetDesktopWindow()) /* a child window */
1553 if (old_parent == GetDesktopWindow())
1555 /* destroy the old X windows */
1556 destroy_whole_window( display, data, FALSE );
1557 destroy_icon_window( display, data );
1558 if (data->managed)
1560 data->managed = FALSE;
1561 RemovePropA( data->hwnd, managed_prop );
1565 else /* new top level window */
1567 /* FIXME: we ignore errors since we can't really recover anyway */
1568 create_whole_window( display, data );
1573 /*****************************************************************
1574 * SetFocus (X11DRV.@)
1576 * Set the X focus.
1577 * Explicit colormap management seems to work only with OLVWM.
1579 void X11DRV_SetFocus( HWND hwnd )
1581 Display *display = thread_display();
1582 struct x11drv_win_data *data;
1583 XWindowChanges changes;
1585 /* If setting the focus to 0, uninstall the colormap */
1586 if (!hwnd && root_window == DefaultRootWindow(display))
1588 wine_tsx11_lock();
1589 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1590 XUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1591 wine_tsx11_unlock();
1592 return;
1595 hwnd = GetAncestor( hwnd, GA_ROOT );
1597 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1598 if (data->managed || !data->whole_window) return;
1600 /* Set X focus and install colormap */
1601 wine_tsx11_lock();
1602 changes.stack_mode = Above;
1603 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
1604 if (root_window == DefaultRootWindow(display))
1606 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1607 /* FIXME: this is not entirely correct */
1608 XSetInputFocus( display, data->whole_window, RevertToParent,
1609 /* CurrentTime */
1610 GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1611 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1612 XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1614 wine_tsx11_unlock();
1618 /**********************************************************************
1619 * SetWindowIcon (X11DRV.@)
1621 * hIcon or hIconSm has changed (or is being initialised for the
1622 * first time). Complete the X11 driver-specific initialisation
1623 * and set the window hints.
1625 * This is not entirely correct, may need to create
1626 * an icon window and set the pixmap as a background
1628 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
1630 Display *display = thread_display();
1631 struct x11drv_win_data *data;
1633 if (type != ICON_BIG) return; /* nothing to do here */
1635 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1636 if (!data->whole_window) return;
1637 if (!data->managed) return;
1639 if (data->wm_hints)
1641 set_icon_hints( display, data, icon );
1642 wine_tsx11_lock();
1643 XSetWMHints( display, data->whole_window, data->wm_hints );
1644 wine_tsx11_unlock();
1649 /***********************************************************************
1650 * SetWindowRgn (X11DRV.@)
1652 * Assign specified region to window (for non-rectangular windows)
1654 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
1656 struct x11drv_win_data *data;
1658 if ((data = X11DRV_get_win_data( hwnd )))
1660 sync_window_region( thread_display(), data, hrgn );
1662 else if (GetWindowThreadProcessId( hwnd, NULL ) != GetCurrentThreadId())
1664 FIXME( "not supported on other thread window %p\n", hwnd );
1665 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1666 return FALSE;
1669 return TRUE;
1673 /***********************************************************************
1674 * is_netwm_supported
1676 static BOOL is_netwm_supported( Display *display, Atom atom )
1678 static Atom *net_supported;
1679 static int net_supported_count = -1;
1680 int i;
1682 wine_tsx11_lock();
1683 if (net_supported_count == -1)
1685 Atom type;
1686 int format;
1687 unsigned long count, remaining;
1689 if (!XGetWindowProperty( display, DefaultRootWindow(display), x11drv_atom(_NET_SUPPORTED), 0,
1690 ~0UL, False, XA_ATOM, &type, &format, &count,
1691 &remaining, (unsigned char **)&net_supported ))
1692 net_supported_count = get_property_size( format, count ) / sizeof(Atom);
1693 else
1694 net_supported_count = 0;
1696 wine_tsx11_unlock();
1698 for (i = 0; i < net_supported_count; i++)
1699 if (net_supported[i] == atom) return TRUE;
1700 return FALSE;
1704 /***********************************************************************
1705 * SysCommand (X11DRV.@)
1707 * Perform WM_SYSCOMMAND handling.
1709 LRESULT X11DRV_SysCommand( HWND hwnd, WPARAM wparam, LPARAM lparam )
1711 WPARAM hittest = wparam & 0x0f;
1712 DWORD dwPoint;
1713 int x, y, dir;
1714 XEvent xev;
1715 Display *display = thread_display();
1716 struct x11drv_win_data *data;
1718 if (!(data = X11DRV_get_win_data( hwnd ))) return -1;
1719 if (!data->whole_window || !data->managed || !data->mapped) return -1;
1721 switch (wparam & 0xfff0)
1723 case SC_MOVE:
1724 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
1725 else dir = _NET_WM_MOVERESIZE_MOVE;
1726 break;
1727 case SC_SIZE:
1728 /* windows without WS_THICKFRAME are not resizable through the window manager */
1729 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_THICKFRAME)) return -1;
1731 switch (hittest)
1733 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
1734 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
1735 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
1736 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
1737 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
1738 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
1739 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
1740 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
1741 default: dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD; break;
1743 break;
1745 case SC_KEYMENU:
1746 /* prevent a simple ALT press+release from activating the system menu,
1747 * as that can get confusing on managed windows */
1748 if ((WCHAR)lparam) return -1; /* got an explicit char */
1749 if (GetMenu( hwnd )) return -1; /* window has a real menu */
1750 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) return -1; /* no system menu */
1751 TRACE( "ignoring SC_KEYMENU wp %lx lp %lx\n", wparam, lparam );
1752 return 0;
1754 default:
1755 return -1;
1758 if (IsZoomed(hwnd)) return -1;
1760 if (!is_netwm_supported( display, x11drv_atom(_NET_WM_MOVERESIZE) ))
1762 TRACE( "_NET_WM_MOVERESIZE not supported\n" );
1763 return -1;
1766 dwPoint = GetMessagePos();
1767 x = (short)LOWORD(dwPoint);
1768 y = (short)HIWORD(dwPoint);
1770 TRACE("hwnd %p, x %d, y %d, dir %d\n", hwnd, x, y, dir);
1772 xev.xclient.type = ClientMessage;
1773 xev.xclient.window = X11DRV_get_whole_window(hwnd);
1774 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
1775 xev.xclient.serial = 0;
1776 xev.xclient.display = display;
1777 xev.xclient.send_event = True;
1778 xev.xclient.format = 32;
1779 xev.xclient.data.l[0] = x; /* x coord */
1780 xev.xclient.data.l[1] = y; /* y coord */
1781 xev.xclient.data.l[2] = dir; /* direction */
1782 xev.xclient.data.l[3] = 1; /* button */
1783 xev.xclient.data.l[4] = 0; /* unused */
1785 /* need to ungrab the pointer that may have been automatically grabbed
1786 * with a ButtonPress event */
1787 wine_tsx11_lock();
1788 XUngrabPointer( display, CurrentTime );
1789 XSendEvent(display, root_window, False, SubstructureNotifyMask, &xev);
1790 wine_tsx11_unlock();
1791 return 0;