user32: Moved the ShowWindow implementation from winex11 back to user32.
[wine/wine64.git] / dlls / winex11.drv / window.c
blob2f6e8a1ad873b01f2013ee3660b0428791e05304
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 "win.h"
50 #include "mwm.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
54 /* X context to associate a hwnd to an X window */
55 XContext winContext = 0;
57 /* X context to associate a struct x11drv_win_data to an hwnd */
58 static XContext win_data_context;
60 static const char whole_window_prop[] = "__wine_x11_whole_window";
61 static const char client_window_prop[]= "__wine_x11_client_window";
62 static const char icon_window_prop[] = "__wine_x11_icon_window";
63 static const char fbconfig_id_prop[] = "__wine_x11_fbconfig_id";
64 static const char gl_drawable_prop[] = "__wine_x11_gl_drawable";
65 static const char pixmap_prop[] = "__wine_x11_pixmap";
66 static const char managed_prop[] = "__wine_x11_managed";
68 /* for XDG systray icons */
69 #define SYSTEM_TRAY_REQUEST_DOCK 0
71 extern int usexcomposite;
73 extern void WIN_invalidate_dce( HWND hwnd, const RECT *rect ); /* FIXME: to be removed */
75 /***********************************************************************
76 * is_window_managed
78 * Check if a given window should be managed
80 BOOL is_window_managed( HWND hwnd, UINT swp_flags, const RECT *window_rect )
82 DWORD style, ex_style;
84 if (!managed_mode) return FALSE;
86 /* child windows are not managed */
87 style = GetWindowLongW( hwnd, GWL_STYLE );
88 if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD) return FALSE;
89 /* activated windows are managed */
90 if (!(swp_flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) return TRUE;
91 if (hwnd == GetActiveWindow()) return TRUE;
92 /* windows with caption are managed */
93 if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
94 /* tool windows are not managed */
95 ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
96 if (ex_style & WS_EX_TOOLWINDOW) return FALSE;
97 /* windows with thick frame are managed */
98 if (style & WS_THICKFRAME) return TRUE;
99 /* application windows are managed */
100 if (ex_style & WS_EX_APPWINDOW) return TRUE;
101 if (style & WS_POPUP)
103 /* popup with sysmenu == caption are managed */
104 if (style & WS_SYSMENU) return TRUE;
105 /* full-screen popup windows are managed */
106 if (window_rect->left <= 0 && window_rect->right >= screen_width &&
107 window_rect->top <= 0 && window_rect->bottom >= screen_height)
108 return TRUE;
110 /* default: not managed */
111 return FALSE;
115 /***********************************************************************
116 * X11DRV_is_window_rect_mapped
118 * Check if the X whole window should be mapped based on its rectangle
120 BOOL X11DRV_is_window_rect_mapped( const RECT *rect )
122 /* don't map if rect is empty */
123 if (IsRectEmpty( rect )) return FALSE;
125 /* don't map if rect is off-screen */
126 if (rect->left >= virtual_screen_rect.right ||
127 rect->top >= virtual_screen_rect.bottom ||
128 rect->right <= virtual_screen_rect.left ||
129 rect->bottom <= virtual_screen_rect.top)
130 return FALSE;
132 return TRUE;
136 /***********************************************************************
137 * get_mwm_decorations
139 static unsigned long get_mwm_decorations( DWORD style, DWORD ex_style )
141 unsigned long ret = 0;
143 if (ex_style & WS_EX_TOOLWINDOW) return 0;
145 if ((style & WS_CAPTION) == WS_CAPTION)
147 ret |= MWM_DECOR_TITLE | MWM_DECOR_BORDER;
148 if (style & WS_SYSMENU) ret |= MWM_DECOR_MENU;
149 if (style & WS_MINIMIZEBOX) ret |= MWM_DECOR_MINIMIZE;
150 if (style & WS_MAXIMIZEBOX) ret |= MWM_DECOR_MAXIMIZE;
152 if (ex_style & WS_EX_DLGMODALFRAME) ret |= MWM_DECOR_BORDER;
153 else if (style & WS_THICKFRAME) ret |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
154 else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) ret |= MWM_DECOR_BORDER;
155 return ret;
159 /***********************************************************************
160 * get_x11_rect_offset
162 * Helper for X11DRV_window_to_X_rect and X11DRV_X_to_window_rect.
164 static void get_x11_rect_offset( struct x11drv_win_data *data, RECT *rect )
166 DWORD style, ex_style, style_mask = 0, ex_style_mask = 0;
167 unsigned long decor;
169 rect->top = rect->bottom = rect->left = rect->right = 0;
171 style = GetWindowLongW( data->hwnd, GWL_STYLE );
172 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
173 decor = get_mwm_decorations( style, ex_style );
175 if (decor & MWM_DECOR_TITLE) style_mask |= WS_CAPTION;
176 if (decor & MWM_DECOR_BORDER)
178 style_mask |= WS_DLGFRAME | WS_THICKFRAME;
179 ex_style_mask |= WS_EX_DLGMODALFRAME;
182 AdjustWindowRectEx( rect, style & style_mask, FALSE, ex_style & ex_style_mask );
186 /***********************************************************************
187 * get_window_attributes
189 * Fill the window attributes structure for an X window.
191 static int get_window_attributes( Display *display, struct x11drv_win_data *data,
192 XSetWindowAttributes *attr )
194 attr->override_redirect = !data->managed;
195 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
196 attr->save_under = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
197 attr->cursor = x11drv_thread_data()->cursor;
198 attr->bit_gravity = NorthWestGravity;
199 attr->backing_store = NotUseful;
200 attr->event_mask = (ExposureMask | PointerMotionMask |
201 ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
202 KeyPressMask | KeyReleaseMask | FocusChangeMask | KeymapStateMask);
203 if (data->managed) attr->event_mask |= StructureNotifyMask | PropertyChangeMask;
205 return (CWOverrideRedirect | CWSaveUnder | CWColormap | CWCursor |
206 CWEventMask | CWBitGravity | CWBackingStore);
210 /***********************************************************************
211 * create_client_window
213 static Window create_client_window( Display *display, struct x11drv_win_data *data, XVisualInfo *vis )
215 int cx, cy, mask;
216 XSetWindowAttributes attr;
217 Window client;
219 attr.bit_gravity = NorthWestGravity;
220 attr.win_gravity = NorthWestGravity;
221 attr.backing_store = NotUseful;
222 attr.event_mask = (ExposureMask | PointerMotionMask |
223 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
224 mask = CWEventMask | CWBitGravity | CWWinGravity | CWBackingStore;
226 if ((cx = data->client_rect.right - data->client_rect.left) <= 0) cx = 1;
227 if ((cy = data->client_rect.bottom - data->client_rect.top) <= 0) cy = 1;
229 wine_tsx11_lock();
231 if (vis)
233 attr.colormap = XCreateColormap( display, root_window, vis->visual,
234 (vis->class == PseudoColor || vis->class == GrayScale ||
235 vis->class == DirectColor) ? AllocAll : AllocNone );
236 mask |= CWColormap;
239 client = XCreateWindow( display, data->whole_window,
240 data->client_rect.left - data->whole_rect.left,
241 data->client_rect.top - data->whole_rect.top,
242 cx, cy, 0, screen_depth, InputOutput,
243 vis ? vis->visual : visual, mask, &attr );
244 if (!client)
246 wine_tsx11_unlock();
247 return 0;
250 if (data->client_window)
252 XDeleteContext( display, data->client_window, winContext );
253 XDestroyWindow( display, data->client_window );
255 data->client_window = client;
257 if (data->colormap) XFreeColormap( display, data->colormap );
258 data->colormap = vis ? attr.colormap : 0;
260 XMapWindow( display, data->client_window );
261 XSaveContext( display, data->client_window, winContext, (char *)data->hwnd );
262 wine_tsx11_unlock();
264 SetPropA( data->hwnd, client_window_prop, (HANDLE)data->client_window );
265 return data->client_window;
269 /***********************************************************************
270 * X11DRV_sync_window_style
272 * Change the X window attributes when the window style has changed.
274 void X11DRV_sync_window_style( Display *display, struct x11drv_win_data *data )
276 if (data->whole_window != root_window)
278 XSetWindowAttributes attr;
279 int mask = get_window_attributes( display, data, &attr );
281 wine_tsx11_lock();
282 XChangeWindowAttributes( display, data->whole_window, mask, &attr );
283 wine_tsx11_unlock();
288 /***********************************************************************
289 * sync_window_region
291 * Update the X11 window region.
293 static void sync_window_region( Display *display, struct x11drv_win_data *data, HRGN hrgn )
295 #ifdef HAVE_LIBXSHAPE
296 if (!data->whole_window) return;
298 if (!hrgn)
300 wine_tsx11_lock();
301 XShapeCombineMask( display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
302 wine_tsx11_unlock();
304 else
306 RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
307 if (pRegionData)
309 wine_tsx11_lock();
310 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
311 data->window_rect.left - data->whole_rect.left,
312 data->window_rect.top - data->whole_rect.top,
313 (XRectangle *)pRegionData->Buffer,
314 pRegionData->rdh.nCount, ShapeSet, YXBanded );
315 wine_tsx11_unlock();
316 HeapFree(GetProcessHeap(), 0, pRegionData);
319 #endif /* HAVE_LIBXSHAPE */
323 /***********************************************************************
324 * sync_window_text
326 static void sync_window_text( Display *display, Window win, const WCHAR *text )
328 UINT count;
329 char *buffer, *utf8_buffer;
330 XTextProperty prop;
332 /* allocate new buffer for window text */
333 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
334 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count ))) return;
335 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
337 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
338 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
340 HeapFree( GetProcessHeap(), 0, buffer );
341 return;
343 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
345 wine_tsx11_lock();
346 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
348 XSetWMName( display, win, &prop );
349 XSetWMIconName( display, win, &prop );
350 XFree( prop.value );
353 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
354 according to the standard
355 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
357 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
358 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
359 wine_tsx11_unlock();
361 HeapFree( GetProcessHeap(), 0, utf8_buffer );
362 HeapFree( GetProcessHeap(), 0, buffer );
366 /***********************************************************************
367 * X11DRV_set_win_format
369 BOOL X11DRV_set_win_format( HWND hwnd, XID fbconfig_id )
371 Display *display = thread_display();
372 struct x11drv_win_data *data;
373 XVisualInfo *vis;
374 int w, h;
376 if (!(data = X11DRV_get_win_data(hwnd)) &&
377 !(data = X11DRV_create_win_data(hwnd))) return FALSE;
379 if (data->fbconfig_id) return FALSE; /* can't change it twice */
381 wine_tsx11_lock();
382 vis = visual_from_fbconfig_id(fbconfig_id);
383 wine_tsx11_unlock();
384 if (!vis) return FALSE;
386 if (data->whole_window)
388 Window client = data->client_window;
390 if (vis->visualid != XVisualIDFromVisual(visual))
392 client = create_client_window( display, data, vis );
393 TRACE( "re-created client window %lx for %p fbconfig %lx\n", client, data->hwnd, fbconfig_id );
395 wine_tsx11_lock();
396 XFree(vis);
397 wine_tsx11_unlock();
398 if (client) goto done;
399 return FALSE;
402 w = data->client_rect.right - data->client_rect.left;
403 h = data->client_rect.bottom - data->client_rect.top;
405 if(w <= 0) w = 1;
406 if(h <= 0) h = 1;
408 #ifdef SONAME_LIBXCOMPOSITE
409 if(usexcomposite)
411 XSetWindowAttributes attrib;
412 Window parent = X11DRV_get_whole_window( GetAncestor( hwnd, GA_ROOT ));
414 if (!parent) parent = root_window;
415 wine_tsx11_lock();
416 data->colormap = XCreateColormap(display, parent, vis->visual,
417 (vis->class == PseudoColor ||
418 vis->class == GrayScale ||
419 vis->class == DirectColor) ?
420 AllocAll : AllocNone);
421 attrib.override_redirect = True;
422 attrib.colormap = data->colormap;
423 XInstallColormap(gdi_display, attrib.colormap);
425 data->gl_drawable = XCreateWindow(display, parent, -w, 0, w, h, 0,
426 vis->depth, InputOutput, vis->visual,
427 CWColormap | CWOverrideRedirect,
428 &attrib);
429 if(data->gl_drawable)
431 pXCompositeRedirectWindow(display, data->gl_drawable,
432 CompositeRedirectManual);
433 XMapWindow(display, data->gl_drawable);
435 XFree(vis);
436 wine_tsx11_unlock();
438 else
439 #endif
441 WARN("XComposite is not available, using GLXPixmap hack\n");
443 wine_tsx11_lock();
444 data->pixmap = XCreatePixmap(display, root_window, w, h, vis->depth);
445 if(!data->pixmap)
447 XFree(vis);
448 wine_tsx11_unlock();
449 return FALSE;
452 data->gl_drawable = create_glxpixmap(display, vis, data->pixmap);
453 if(!data->gl_drawable)
455 XFreePixmap(display, data->pixmap);
456 data->pixmap = 0;
458 XFree(vis);
459 wine_tsx11_unlock();
460 if (data->pixmap) SetPropA(hwnd, pixmap_prop, (HANDLE)data->pixmap);
463 if (!data->gl_drawable) return FALSE;
465 TRACE("Created GL drawable 0x%lx, using FBConfigID 0x%lx\n",
466 data->gl_drawable, fbconfig_id);
467 SetPropA(hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
469 done:
470 data->fbconfig_id = fbconfig_id;
471 SetPropA(hwnd, fbconfig_id_prop, (HANDLE)data->fbconfig_id);
472 wine_tsx11_lock();
473 XFlush( display );
474 wine_tsx11_unlock();
475 WIN_invalidate_dce( hwnd, NULL );
476 return TRUE;
479 /***********************************************************************
480 * sync_gl_drawable
482 static void sync_gl_drawable(Display *display, struct x11drv_win_data *data)
484 int w = data->client_rect.right - data->client_rect.left;
485 int h = data->client_rect.bottom - data->client_rect.top;
486 XVisualInfo *vis;
487 Drawable glxp;
488 Pixmap pix;
490 if (w <= 0) w = 1;
491 if (h <= 0) h = 1;
493 TRACE("Resizing GL drawable 0x%lx to %dx%d\n", data->gl_drawable, w, h);
494 #ifdef SONAME_LIBXCOMPOSITE
495 if(usexcomposite)
497 wine_tsx11_lock();
498 XMoveResizeWindow(display, data->gl_drawable, -w, 0, w, h);
499 wine_tsx11_unlock();
500 return;
502 #endif
504 wine_tsx11_lock();
506 vis = visual_from_fbconfig_id(data->fbconfig_id);
507 if(!vis)
509 wine_tsx11_unlock();
510 return;
513 pix = XCreatePixmap(display, root_window, w, h, vis->depth);
514 if(!pix)
516 ERR("Failed to create pixmap for offscreen rendering\n");
517 XFree(vis);
518 wine_tsx11_unlock();
519 return;
522 glxp = create_glxpixmap(display, vis, pix);
523 if(!glxp)
525 ERR("Failed to create drawable for offscreen rendering\n");
526 XFreePixmap(display, pix);
527 XFree(vis);
528 wine_tsx11_unlock();
529 return;
532 XFree(vis);
534 mark_drawable_dirty(data->gl_drawable, glxp);
536 XFreePixmap(display, data->pixmap);
537 destroy_glxpixmap(display, data->gl_drawable);
539 data->pixmap = pix;
540 data->gl_drawable = glxp;
542 wine_tsx11_unlock();
544 SetPropA(data->hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
545 SetPropA(data->hwnd, pixmap_prop, (HANDLE)data->pixmap);
549 /***********************************************************************
550 * get_window_changes
552 * fill the window changes structure
554 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
556 int mask = 0;
558 if (old->right - old->left != new->right - new->left )
560 if ((changes->width = new->right - new->left) <= 0) changes->width = 1;
561 mask |= CWWidth;
563 if (old->bottom - old->top != new->bottom - new->top)
565 if ((changes->height = new->bottom - new->top) <= 0) changes->height = 1;
566 mask |= CWHeight;
568 if (old->left != new->left)
570 changes->x = new->left;
571 mask |= CWX;
573 if (old->top != new->top)
575 changes->y = new->top;
576 mask |= CWY;
578 return mask;
582 /***********************************************************************
583 * create_icon_window
585 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
587 XSetWindowAttributes attr;
589 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
590 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
591 attr.bit_gravity = NorthWestGravity;
592 attr.backing_store = NotUseful/*WhenMapped*/;
593 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
595 wine_tsx11_lock();
596 data->icon_window = XCreateWindow( display, root_window, 0, 0,
597 GetSystemMetrics( SM_CXICON ),
598 GetSystemMetrics( SM_CYICON ),
599 0, screen_depth,
600 InputOutput, visual,
601 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
602 XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
603 wine_tsx11_unlock();
605 TRACE( "created %lx\n", data->icon_window );
606 SetPropA( data->hwnd, icon_window_prop, (HANDLE)data->icon_window );
607 return data->icon_window;
612 /***********************************************************************
613 * destroy_icon_window
615 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
617 if (!data->icon_window) return;
618 if (x11drv_thread_data()->cursor_window == data->icon_window)
619 x11drv_thread_data()->cursor_window = None;
620 wine_tsx11_lock();
621 XDeleteContext( display, data->icon_window, winContext );
622 XDestroyWindow( display, data->icon_window );
623 data->icon_window = 0;
624 wine_tsx11_unlock();
625 RemovePropA( data->hwnd, icon_window_prop );
629 /***********************************************************************
630 * set_icon_hints
632 * Set the icon wm hints
634 static void set_icon_hints( Display *display, struct x11drv_win_data *data, HICON hIcon )
636 XWMHints *hints = data->wm_hints;
638 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
639 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
640 data->hWMIconBitmap = 0;
641 data->hWMIconMask = 0;
643 if (!hIcon)
645 if (!data->icon_window) create_icon_window( display, data );
646 hints->icon_window = data->icon_window;
647 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
649 else
651 HBITMAP hbmOrig;
652 RECT rcMask;
653 BITMAP bmMask;
654 ICONINFO ii;
655 HDC hDC;
657 GetIconInfo(hIcon, &ii);
659 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
660 rcMask.top = 0;
661 rcMask.left = 0;
662 rcMask.right = bmMask.bmWidth;
663 rcMask.bottom = bmMask.bmHeight;
665 hDC = CreateCompatibleDC(0);
666 hbmOrig = SelectObject(hDC, ii.hbmMask);
667 InvertRect(hDC, &rcMask);
668 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
669 SelectObject(hDC, hbmOrig);
670 DeleteDC(hDC);
672 data->hWMIconBitmap = ii.hbmColor;
673 data->hWMIconMask = ii.hbmMask;
675 hints->icon_pixmap = X11DRV_get_pixmap(data->hWMIconBitmap);
676 hints->icon_mask = X11DRV_get_pixmap(data->hWMIconMask);
677 destroy_icon_window( display, data );
678 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
682 /***********************************************************************
683 * wine_make_systray_window (X11DRV.@)
685 * Docks the given X window with the NETWM system tray.
687 void X11DRV_make_systray_window( HWND hwnd )
689 static Atom systray_atom;
690 Display *display = thread_display();
691 struct x11drv_win_data *data;
692 Window systray_window;
694 if (!(data = X11DRV_get_win_data( hwnd )) &&
695 !(data = X11DRV_create_win_data( hwnd ))) return;
697 wine_tsx11_lock();
698 if (!systray_atom)
700 if (DefaultScreen( display ) == 0)
701 systray_atom = x11drv_atom(_NET_SYSTEM_TRAY_S0);
702 else
704 char systray_buffer[29]; /* strlen(_NET_SYSTEM_TRAY_S4294967295)+1 */
705 sprintf( systray_buffer, "_NET_SYSTEM_TRAY_S%u", DefaultScreen( display ) );
706 systray_atom = XInternAtom( display, systray_buffer, False );
709 systray_window = XGetSelectionOwner( display, systray_atom );
710 wine_tsx11_unlock();
712 TRACE("Docking tray icon %p\n", data->hwnd);
714 if (systray_window != None)
716 XEvent ev;
717 unsigned long info[2];
719 /* Put the window offscreen so it isn't mapped. The window _cannot_ be
720 * mapped if we intend to dock with an XEMBED tray. If the window is
721 * mapped when we dock, it may become visible as a child of the root
722 * window after it docks, which isn't the proper behavior.
724 * For more information on this problem, see
725 * http://standards.freedesktop.org/xembed-spec/latest/ar01s04.html */
727 SetWindowPos( data->hwnd, NULL, virtual_screen_rect.right + 1, virtual_screen_rect.bottom + 1,
728 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE );
730 /* set XEMBED protocol data on the window */
731 info[0] = 0; /* protocol version */
732 info[1] = 1; /* mapped = true */
734 wine_tsx11_lock();
735 XChangeProperty( display, data->whole_window,
736 x11drv_atom(_XEMBED_INFO),
737 x11drv_atom(_XEMBED_INFO), 32, PropModeReplace,
738 (unsigned char*)info, 2 );
739 wine_tsx11_unlock();
741 /* send the docking request message */
742 ZeroMemory( &ev, sizeof(ev) );
743 ev.xclient.type = ClientMessage;
744 ev.xclient.window = systray_window;
745 ev.xclient.message_type = x11drv_atom( _NET_SYSTEM_TRAY_OPCODE );
746 ev.xclient.format = 32;
747 ev.xclient.data.l[0] = CurrentTime;
748 ev.xclient.data.l[1] = SYSTEM_TRAY_REQUEST_DOCK;
749 ev.xclient.data.l[2] = data->whole_window;
750 ev.xclient.data.l[3] = 0;
751 ev.xclient.data.l[4] = 0;
753 wine_tsx11_lock();
754 XSendEvent( display, systray_window, False, NoEventMask, &ev );
755 wine_tsx11_unlock();
758 else
760 int val = 1;
762 /* fall back to he KDE hints if the WM doesn't support XEMBED'ed
763 * systrays */
765 wine_tsx11_lock();
766 XChangeProperty( display, data->whole_window,
767 x11drv_atom(KWM_DOCKWINDOW),
768 x11drv_atom(KWM_DOCKWINDOW), 32, PropModeReplace,
769 (unsigned char*)&val, 1 );
770 XChangeProperty( display, data->whole_window,
771 x11drv_atom(_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR),
772 XA_WINDOW, 32, PropModeReplace,
773 (unsigned char*)&data->whole_window, 1 );
774 wine_tsx11_unlock();
779 /***********************************************************************
780 * set_size_hints
782 * set the window size hints
784 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
786 XSizeHints* size_hints;
788 if ((size_hints = XAllocSizeHints()))
790 size_hints->flags = 0;
792 if (data->hwnd != GetDesktopWindow()) /* don't force position of desktop */
794 size_hints->win_gravity = StaticGravity;
795 size_hints->x = data->whole_rect.left;
796 size_hints->y = data->whole_rect.top;
797 size_hints->flags |= PWinGravity | PPosition;
800 if ( !(style & WS_THICKFRAME) )
802 /* If we restrict window resizing Metacity decides that it should
803 * disable fullscreen support for this window as well.
805 if (!(data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
806 data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height))
808 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
809 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
810 size_hints->min_width = size_hints->max_width;
811 size_hints->min_height = size_hints->max_height;
812 size_hints->flags |= PMinSize | PMaxSize;
815 XSetWMNormalHints( display, data->whole_window, size_hints );
816 XFree( size_hints );
821 /***********************************************************************
822 * get_process_name
824 * get the name of the current process for setting class hints
826 static char *get_process_name(void)
828 static char *name;
830 if (!name)
832 WCHAR module[MAX_PATH];
833 DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
834 if (len && len < MAX_PATH)
836 char *ptr;
837 WCHAR *p, *appname = module;
839 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
840 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
841 len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
842 if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
844 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
845 name = ptr;
849 return name;
853 /***********************************************************************
854 * set_initial_wm_hints
856 * Set the window manager hints that don't change over the lifetime of a window.
858 static void set_initial_wm_hints( Display *display, struct x11drv_win_data *data )
860 int i;
861 Atom protocols[3];
862 Atom dndVersion = 4;
863 XClassHint *class_hints;
864 char *process_name = get_process_name();
866 wine_tsx11_lock();
868 /* wm protocols */
869 i = 0;
870 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
871 protocols[i++] = x11drv_atom(_NET_WM_PING);
872 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
873 XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
874 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
876 /* class hints */
877 if ((class_hints = XAllocClassHint()))
879 static char wine[] = "Wine";
881 class_hints->res_name = process_name;
882 class_hints->res_class = wine;
883 XSetClassHint( display, data->whole_window, class_hints );
884 XFree( class_hints );
887 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
888 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
889 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
890 i = getpid();
891 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
892 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
894 XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
895 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
897 data->wm_hints = XAllocWMHints();
898 wine_tsx11_unlock();
900 if (data->wm_hints)
902 HICON icon = (HICON)SendMessageW( data->hwnd, WM_GETICON, ICON_BIG, 0 );
903 if (!icon) icon = (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON );
904 data->wm_hints->flags = 0;
905 set_icon_hints( display, data, icon );
910 /***********************************************************************
911 * X11DRV_set_wm_hints
913 * Set the window manager hints for a newly-created window
915 void X11DRV_set_wm_hints( Display *display, struct x11drv_win_data *data )
917 Window group_leader;
918 Atom window_type;
919 MwmHints mwm_hints;
920 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
921 DWORD ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
922 HWND owner = GetWindow( data->hwnd, GW_OWNER );
924 if (data->hwnd == GetDesktopWindow())
926 /* force some styles for the desktop to get the correct decorations */
927 style |= WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
928 owner = 0;
931 /* transient for hint */
932 if (owner)
934 Window owner_win = X11DRV_get_whole_window( owner );
935 wine_tsx11_lock();
936 XSetTransientForHint( display, data->whole_window, owner_win );
937 wine_tsx11_unlock();
938 group_leader = owner_win;
940 else group_leader = data->whole_window;
942 wine_tsx11_lock();
944 /* size hints */
945 set_size_hints( display, data, style );
947 /* set the WM_WINDOW_TYPE */
948 window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
949 if (ex_style & WS_EX_TOOLWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
950 else if (style & WS_THICKFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
951 else if (style & WS_DLGFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
952 else if (ex_style & WS_EX_DLGMODALFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
954 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
955 XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
957 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
958 mwm_hints.decorations = get_mwm_decorations( style, ex_style );
959 mwm_hints.functions = MWM_FUNC_MOVE;
960 if (style & WS_THICKFRAME) mwm_hints.functions |= MWM_FUNC_RESIZE;
961 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
962 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
963 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
965 XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
966 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
967 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
969 /* wm hints */
970 if (data->wm_hints)
972 data->wm_hints->flags |= InputHint | StateHint | WindowGroupHint;
973 data->wm_hints->input = !(style & WS_DISABLED);
974 data->wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
975 data->wm_hints->window_group = group_leader;
976 XSetWMHints( display, data->whole_window, data->wm_hints );
979 wine_tsx11_unlock();
983 /***********************************************************************
984 * X11DRV_window_to_X_rect
986 * Convert a rect from client to X window coordinates
988 void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
990 RECT rc;
992 if (!data->managed) return;
993 if (IsRectEmpty( rect )) return;
995 get_x11_rect_offset( data, &rc );
997 rect->left -= rc.left;
998 rect->right -= rc.right;
999 rect->top -= rc.top;
1000 rect->bottom -= rc.bottom;
1001 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1002 if (rect->left >= rect->right) rect->right = rect->left + 1;
1006 /***********************************************************************
1007 * X11DRV_X_to_window_rect
1009 * Opposite of X11DRV_window_to_X_rect
1011 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
1013 RECT rc;
1015 if (!data->managed) return;
1016 if (IsRectEmpty( rect )) return;
1018 get_x11_rect_offset( data, &rc );
1020 rect->left += rc.left;
1021 rect->right += rc.right;
1022 rect->top += rc.top;
1023 rect->bottom += rc.bottom;
1024 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1025 if (rect->left >= rect->right) rect->right = rect->left + 1;
1029 /***********************************************************************
1030 * X11DRV_sync_window_position
1032 * Synchronize the X window position with the Windows one
1034 void X11DRV_sync_window_position( Display *display, struct x11drv_win_data *data,
1035 UINT swp_flags, const RECT *old_client_rect,
1036 const RECT *old_whole_rect )
1038 XWindowChanges changes;
1039 int mask = get_window_changes( &changes, old_whole_rect, &data->whole_rect );
1041 if (!(swp_flags & SWP_NOZORDER))
1043 /* find window that this one must be after */
1044 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
1045 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
1046 prev = GetWindow( prev, GW_HWNDPREV );
1047 if (!prev) /* top child */
1049 changes.stack_mode = Above;
1050 mask |= CWStackMode;
1052 else
1054 /* should use stack_mode Below but most window managers don't get it right */
1055 /* so move it above the next one in Z order */
1056 HWND next = GetWindow( data->hwnd, GW_HWNDNEXT );
1057 while (next && !(GetWindowLongW( next, GWL_STYLE ) & WS_VISIBLE))
1058 next = GetWindow( next, GW_HWNDNEXT );
1059 if (next)
1061 changes.stack_mode = Above;
1062 changes.sibling = X11DRV_get_whole_window(next);
1063 mask |= CWStackMode | CWSibling;
1068 /* only the size is allowed to change for the desktop window */
1069 if (data->whole_window == root_window) mask &= CWWidth | CWHeight;
1071 if (mask)
1073 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
1075 TRACE( "setting win %lx pos %d,%d,%dx%d after %lx changes=%x\n",
1076 data->whole_window, data->whole_rect.left, data->whole_rect.top,
1077 data->whole_rect.right - data->whole_rect.left,
1078 data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
1080 wine_tsx11_lock();
1081 if (mask & (CWWidth|CWHeight)) set_size_hints( display, data, style );
1082 if (mask & CWX) changes.x -= virtual_screen_rect.left;
1083 if (mask & CWY) changes.y -= virtual_screen_rect.top;
1084 XReconfigureWMWindow( display, data->whole_window,
1085 DefaultScreen(display), mask, &changes );
1086 wine_tsx11_unlock();
1091 /***********************************************************************
1092 * X11DRV_sync_client_position
1094 * Synchronize the X client window position with the Windows one
1096 void X11DRV_sync_client_position( Display *display, struct x11drv_win_data *data,
1097 UINT swp_flags, const RECT *old_client_rect,
1098 const RECT *old_whole_rect )
1100 int mask;
1101 XWindowChanges changes;
1102 RECT old = *old_client_rect;
1103 RECT new = data->client_rect;
1105 OffsetRect( &old, -old_whole_rect->left, -old_whole_rect->top );
1106 OffsetRect( &new, -data->whole_rect.left, -data->whole_rect.top );
1107 if (!(mask = get_window_changes( &changes, &old, &new ))) return;
1109 if (data->client_window)
1111 TRACE( "setting client win %lx pos %d,%d,%dx%d changes=%x\n",
1112 data->client_window, new.left, new.top,
1113 new.right - new.left, new.bottom - new.top, mask );
1114 wine_tsx11_lock();
1115 XConfigureWindow( display, data->client_window, mask, &changes );
1116 wine_tsx11_unlock();
1119 if (data->gl_drawable && (mask & (CWWidth|CWHeight))) sync_gl_drawable( display, data );
1121 /* make sure the changes get to the server before we start painting */
1122 if (data->client_window || data->gl_drawable)
1124 wine_tsx11_lock();
1125 XFlush(display);
1126 wine_tsx11_unlock();
1131 /**********************************************************************
1132 * create_whole_window
1134 * Create the whole X window for a given window
1136 static Window create_whole_window( Display *display, struct x11drv_win_data *data )
1138 int cx, cy, mask;
1139 XSetWindowAttributes attr;
1140 XIM xim;
1141 WCHAR text[1024];
1142 HRGN hrgn;
1144 if (!(cx = data->window_rect.right - data->window_rect.left)) cx = 1;
1145 if (!(cy = data->window_rect.bottom - data->window_rect.top)) cy = 1;
1147 if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1149 TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1150 data->managed = TRUE;
1151 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1154 mask = get_window_attributes( display, data, &attr );
1156 wine_tsx11_lock();
1158 data->whole_rect = data->window_rect;
1159 data->whole_window = XCreateWindow( display, root_window,
1160 data->window_rect.left - virtual_screen_rect.left,
1161 data->window_rect.top - virtual_screen_rect.top,
1162 cx, cy, 0, screen_depth, InputOutput,
1163 visual, mask, &attr );
1165 if (data->whole_window) XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
1166 wine_tsx11_unlock();
1168 if (!data->whole_window) return 0;
1170 if (!create_client_window( display, data, NULL ))
1172 wine_tsx11_lock();
1173 XDeleteContext( display, data->whole_window, winContext );
1174 XDestroyWindow( display, data->whole_window );
1175 data->whole_window = 0;
1176 wine_tsx11_unlock();
1177 return 0;
1180 xim = x11drv_thread_data()->xim;
1181 if (xim) data->xic = X11DRV_CreateIC( xim, display, data->whole_window );
1183 set_initial_wm_hints( display, data );
1184 X11DRV_set_wm_hints( display, data );
1186 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1188 /* set the window text */
1189 if (!InternalGetWindowText( data->hwnd, text, sizeof(text)/sizeof(WCHAR) )) text[0] = 0;
1190 sync_window_text( display, data->whole_window, text );
1192 /* set the window region */
1193 if ((hrgn = CreateRectRgn( 0, 0, 0, 0 )))
1195 if (GetWindowRgn( data->hwnd, hrgn ) != ERROR) sync_window_region( display, data, hrgn );
1196 DeleteObject( hrgn );
1198 return data->whole_window;
1202 /**********************************************************************
1203 * destroy_whole_window
1205 * Destroy the whole X window for a given window.
1207 static void destroy_whole_window( Display *display, struct x11drv_win_data *data )
1209 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1211 if (!data->whole_window) return;
1213 TRACE( "win %p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1214 if (thread_data->cursor_window == data->whole_window ||
1215 thread_data->cursor_window == data->client_window)
1216 thread_data->cursor_window = None;
1217 wine_tsx11_lock();
1218 XDeleteContext( display, data->whole_window, winContext );
1219 XDeleteContext( display, data->client_window, winContext );
1220 XDestroyWindow( display, data->whole_window );
1221 data->whole_window = data->client_window = 0;
1222 if (data->xic)
1224 XUnsetICFocus( data->xic );
1225 XDestroyIC( data->xic );
1227 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1228 XFlush( display );
1229 XFree( data->wm_hints );
1230 data->wm_hints = NULL;
1231 wine_tsx11_unlock();
1232 RemovePropA( data->hwnd, whole_window_prop );
1233 RemovePropA( data->hwnd, client_window_prop );
1237 /*****************************************************************
1238 * SetWindowText (X11DRV.@)
1240 void X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1242 Display *display = thread_display();
1243 Window win;
1245 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(display))
1246 sync_window_text( display, win, text );
1250 /***********************************************************************
1251 * DestroyWindow (X11DRV.@)
1253 void X11DRV_DestroyWindow( HWND hwnd )
1255 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1256 Display *display = thread_data->display;
1257 struct x11drv_win_data *data;
1259 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1261 if (data->pixmap)
1263 destroy_glxpixmap(display, data->gl_drawable);
1264 wine_tsx11_lock();
1265 XFreePixmap(display, data->pixmap);
1266 wine_tsx11_unlock();
1268 else if (data->gl_drawable)
1270 wine_tsx11_lock();
1271 XDestroyWindow(display, data->gl_drawable);
1272 wine_tsx11_unlock();
1275 destroy_whole_window( display, data );
1276 destroy_icon_window( display, data );
1278 if (data->colormap)
1280 wine_tsx11_lock();
1281 XFreeColormap( display, data->colormap );
1282 wine_tsx11_unlock();
1285 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1286 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
1287 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
1288 wine_tsx11_lock();
1289 XDeleteContext( display, (XID)hwnd, win_data_context );
1290 wine_tsx11_unlock();
1291 HeapFree( GetProcessHeap(), 0, data );
1295 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
1297 struct x11drv_win_data *data;
1299 if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
1301 data->hwnd = hwnd;
1302 wine_tsx11_lock();
1303 if (!winContext) winContext = XUniqueContext();
1304 if (!win_data_context) win_data_context = XUniqueContext();
1305 XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
1306 wine_tsx11_unlock();
1308 return data;
1312 /* initialize the desktop window id in the desktop manager process */
1313 static struct x11drv_win_data *create_desktop_win_data( Display *display, HWND hwnd )
1315 struct x11drv_win_data *data;
1316 VisualID visualid;
1318 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1319 wine_tsx11_lock();
1320 visualid = XVisualIDFromVisual(visual);
1321 wine_tsx11_unlock();
1322 data->whole_window = data->client_window = root_window;
1323 data->managed = TRUE;
1324 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1325 SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
1326 SetPropA( data->hwnd, client_window_prop, (HANDLE)root_window );
1327 set_initial_wm_hints( display, data );
1328 return data;
1331 /**********************************************************************
1332 * CreateDesktopWindow (X11DRV.@)
1334 BOOL X11DRV_CreateDesktopWindow( HWND hwnd )
1336 unsigned int width, height;
1338 /* retrieve the real size of the desktop */
1339 SERVER_START_REQ( get_window_rectangles )
1341 req->handle = hwnd;
1342 wine_server_call( req );
1343 width = reply->window.right - reply->window.left;
1344 height = reply->window.bottom - reply->window.top;
1346 SERVER_END_REQ;
1348 if (!width && !height) /* not initialized yet */
1350 SERVER_START_REQ( set_window_pos )
1352 req->handle = hwnd;
1353 req->previous = 0;
1354 req->flags = SWP_NOZORDER;
1355 req->window.left = virtual_screen_rect.left;
1356 req->window.top = virtual_screen_rect.top;
1357 req->window.right = virtual_screen_rect.right;
1358 req->window.bottom = virtual_screen_rect.bottom;
1359 req->client = req->window;
1360 wine_server_call( req );
1362 SERVER_END_REQ;
1364 else
1366 Window win = (Window)GetPropA( hwnd, whole_window_prop );
1367 if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1369 return TRUE;
1373 /**********************************************************************
1374 * CreateWindow (X11DRV.@)
1376 BOOL X11DRV_CreateWindow( HWND hwnd )
1378 Display *display = thread_display();
1380 if (hwnd == GetDesktopWindow() && root_window != DefaultRootWindow( display ))
1382 /* the desktop win data can't be created lazily */
1383 if (!create_desktop_win_data( display, hwnd )) return FALSE;
1385 return TRUE;
1389 /***********************************************************************
1390 * X11DRV_get_win_data
1392 * Return the X11 data structure associated with a window.
1394 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1396 char *data;
1398 if (!hwnd || XFindContext( thread_display(), (XID)hwnd, win_data_context, &data )) data = NULL;
1399 return (struct x11drv_win_data *)data;
1403 /***********************************************************************
1404 * X11DRV_create_win_data
1406 * Create an X11 data window structure for an existing window.
1408 struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd )
1410 Display *display = thread_display();
1411 struct x11drv_win_data *data;
1412 HWND parent;
1414 if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL; /* desktop */
1415 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1417 GetWindowRect( hwnd, &data->window_rect );
1418 MapWindowPoints( 0, parent, (POINT *)&data->window_rect, 2 );
1419 data->whole_rect = data->window_rect;
1420 GetClientRect( hwnd, &data->client_rect );
1421 MapWindowPoints( hwnd, parent, (POINT *)&data->client_rect, 2 );
1423 if (parent == GetDesktopWindow())
1425 if (!create_whole_window( display, data ))
1427 HeapFree( GetProcessHeap(), 0, data );
1428 return NULL;
1430 TRACE( "win %p/%lx/%lx window %s whole %s client %s\n",
1431 hwnd, data->whole_window, data->client_window, wine_dbgstr_rect( &data->window_rect ),
1432 wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
1434 return data;
1438 /***********************************************************************
1439 * X11DRV_get_whole_window
1441 * Return the X window associated with the full area of a window
1443 Window X11DRV_get_whole_window( HWND hwnd )
1445 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1447 if (!data)
1449 if (hwnd == GetDesktopWindow()) return root_window;
1450 return (Window)GetPropA( hwnd, whole_window_prop );
1452 return data->whole_window;
1456 /***********************************************************************
1457 * X11DRV_get_client_window
1459 * Return the X window associated with the client area of a window
1461 Window X11DRV_get_client_window( HWND hwnd )
1463 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1465 if (!data)
1467 if (hwnd == GetDesktopWindow()) return root_window;
1468 return (Window)GetPropA( hwnd, client_window_prop );
1470 return data->client_window;
1474 /***********************************************************************
1475 * X11DRV_get_ic
1477 * Return the X input context associated with a window
1479 XIC X11DRV_get_ic( HWND hwnd )
1481 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1483 if (!data) return 0;
1484 return data->xic;
1488 /***********************************************************************
1489 * X11DRV_GetDC (X11DRV.@)
1491 void X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
1492 const RECT *top_rect, DWORD flags )
1494 struct x11drv_escape_set_drawable escape;
1495 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1497 escape.code = X11DRV_SET_DRAWABLE;
1498 escape.mode = IncludeInferiors;
1499 escape.fbconfig_id = 0;
1500 escape.gl_drawable = 0;
1501 escape.pixmap = 0;
1503 if (top == hwnd && data && IsIconic( hwnd ) && data->icon_window)
1505 escape.drawable = data->icon_window;
1507 else if (top == hwnd && (flags & DCX_WINDOW))
1509 escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
1511 else
1513 escape.drawable = X11DRV_get_client_window( top );
1514 escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
1515 escape.gl_drawable = data ? data->gl_drawable : (Drawable)GetPropA( hwnd, gl_drawable_prop );
1516 escape.pixmap = data ? data->pixmap : (Pixmap)GetPropA( hwnd, pixmap_prop );
1519 escape.dc_rect.left = win_rect->left - top_rect->left;
1520 escape.dc_rect.top = win_rect->top - top_rect->top;
1521 escape.dc_rect.right = win_rect->right - top_rect->left;
1522 escape.dc_rect.bottom = win_rect->bottom - top_rect->top;
1523 escape.drawable_rect.left = top_rect->left;
1524 escape.drawable_rect.top = top_rect->top;
1525 escape.drawable_rect.right = top_rect->right;
1526 escape.drawable_rect.bottom = top_rect->bottom;
1528 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1532 /***********************************************************************
1533 * X11DRV_ReleaseDC (X11DRV.@)
1535 void X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
1537 struct x11drv_escape_set_drawable escape;
1539 escape.code = X11DRV_SET_DRAWABLE;
1540 escape.drawable = root_window;
1541 escape.mode = IncludeInferiors;
1542 escape.drawable_rect = virtual_screen_rect;
1543 SetRect( &escape.dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
1544 virtual_screen_rect.bottom - virtual_screen_rect.top );
1545 escape.fbconfig_id = 0;
1546 escape.gl_drawable = 0;
1547 escape.pixmap = 0;
1548 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1552 /*****************************************************************
1553 * SetParent (X11DRV.@)
1555 void X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
1557 Display *display = thread_display();
1558 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1560 if (!data) return;
1561 if (parent == old_parent) return;
1563 if (parent != GetDesktopWindow()) /* a child window */
1565 if (old_parent == GetDesktopWindow())
1567 /* destroy the old X windows */
1568 destroy_whole_window( display, data );
1569 destroy_icon_window( display, data );
1570 if (data->managed)
1572 data->managed = FALSE;
1573 RemovePropA( data->hwnd, managed_prop );
1577 else /* new top level window */
1579 /* FIXME: we ignore errors since we can't really recover anyway */
1580 create_whole_window( display, data );
1585 /*****************************************************************
1586 * SetFocus (X11DRV.@)
1588 * Set the X focus.
1589 * Explicit colormap management seems to work only with OLVWM.
1591 void X11DRV_SetFocus( HWND hwnd )
1593 Display *display = thread_display();
1594 struct x11drv_win_data *data;
1595 XWindowChanges changes;
1597 /* If setting the focus to 0, uninstall the colormap */
1598 if (!hwnd && root_window == DefaultRootWindow(display))
1600 wine_tsx11_lock();
1601 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1602 XUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1603 wine_tsx11_unlock();
1604 return;
1607 hwnd = GetAncestor( hwnd, GA_ROOT );
1609 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1610 if (data->managed || !data->whole_window) return;
1612 /* Set X focus and install colormap */
1613 wine_tsx11_lock();
1614 changes.stack_mode = Above;
1615 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
1616 if (root_window == DefaultRootWindow(display))
1618 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1619 /* FIXME: this is not entirely correct */
1620 XSetInputFocus( display, data->whole_window, RevertToParent,
1621 /* CurrentTime */
1622 GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1623 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1624 XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1626 wine_tsx11_unlock();
1630 /**********************************************************************
1631 * SetWindowIcon (X11DRV.@)
1633 * hIcon or hIconSm has changed (or is being initialised for the
1634 * first time). Complete the X11 driver-specific initialisation
1635 * and set the window hints.
1637 * This is not entirely correct, may need to create
1638 * an icon window and set the pixmap as a background
1640 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
1642 Display *display = thread_display();
1643 struct x11drv_win_data *data;
1645 if (type != ICON_BIG) return; /* nothing to do here */
1647 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1648 if (!data->whole_window) return;
1649 if (!data->managed) return;
1651 if (data->wm_hints)
1653 set_icon_hints( display, data, icon );
1654 wine_tsx11_lock();
1655 XSetWMHints( display, data->whole_window, data->wm_hints );
1656 wine_tsx11_unlock();
1661 /***********************************************************************
1662 * SetWindowRgn (X11DRV.@)
1664 * Assign specified region to window (for non-rectangular windows)
1666 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
1668 struct x11drv_win_data *data;
1670 if ((data = X11DRV_get_win_data( hwnd )))
1672 sync_window_region( thread_display(), data, hrgn );
1674 else if (GetWindowThreadProcessId( hwnd, NULL ) != GetCurrentThreadId())
1676 FIXME( "not supported on other thread window %p\n", hwnd );
1677 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1678 return FALSE;
1681 return TRUE;