dmusic: Assign to structs instead of using memcpy.
[wine.git] / dlls / winex11.drv / window.c
blobcf843897ddc45df700df281582d3143b26988785
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;
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 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
803 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
804 size_hints->min_width = size_hints->max_width;
805 size_hints->min_height = size_hints->max_height;
806 size_hints->flags |= PMinSize | PMaxSize;
808 XSetWMNormalHints( display, data->whole_window, size_hints );
809 XFree( size_hints );
814 /***********************************************************************
815 * get_process_name
817 * get the name of the current process for setting class hints
819 static char *get_process_name(void)
821 static char *name;
823 if (!name)
825 WCHAR module[MAX_PATH];
826 DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
827 if (len && len < MAX_PATH)
829 char *ptr;
830 WCHAR *p, *appname = module;
832 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
833 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
834 len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
835 if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
837 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
838 name = ptr;
842 return name;
846 /***********************************************************************
847 * set_initial_wm_hints
849 * Set the window manager hints that don't change over the lifetime of a window.
851 static void set_initial_wm_hints( Display *display, struct x11drv_win_data *data )
853 int i;
854 Atom protocols[3];
855 Atom dndVersion = 4;
856 XClassHint *class_hints;
857 char *process_name = get_process_name();
859 wine_tsx11_lock();
861 /* wm protocols */
862 i = 0;
863 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
864 protocols[i++] = x11drv_atom(_NET_WM_PING);
865 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
866 XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
867 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
869 /* class hints */
870 if ((class_hints = XAllocClassHint()))
872 static char wine[] = "Wine";
874 class_hints->res_name = process_name;
875 class_hints->res_class = wine;
876 XSetClassHint( display, data->whole_window, class_hints );
877 XFree( class_hints );
880 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
881 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
882 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
883 i = getpid();
884 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
885 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
887 XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
888 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
890 data->wm_hints = XAllocWMHints();
891 wine_tsx11_unlock();
893 if (data->wm_hints)
895 HICON icon = (HICON)SendMessageW( data->hwnd, WM_GETICON, ICON_BIG, 0 );
896 if (!icon) icon = (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON );
897 data->wm_hints->flags = 0;
898 set_icon_hints( display, data, icon );
903 /***********************************************************************
904 * X11DRV_set_wm_hints
906 * Set the window manager hints for a newly-created window
908 void X11DRV_set_wm_hints( Display *display, struct x11drv_win_data *data )
910 Window group_leader;
911 Atom window_type;
912 MwmHints mwm_hints;
913 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
914 DWORD ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
915 HWND owner = GetWindow( data->hwnd, GW_OWNER );
917 if (data->hwnd == GetDesktopWindow())
919 /* force some styles for the desktop to get the correct decorations */
920 style |= WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
921 owner = 0;
924 /* transient for hint */
925 if (owner)
927 Window owner_win = X11DRV_get_whole_window( owner );
928 wine_tsx11_lock();
929 XSetTransientForHint( display, data->whole_window, owner_win );
930 wine_tsx11_unlock();
931 group_leader = owner_win;
933 else group_leader = data->whole_window;
935 wine_tsx11_lock();
937 /* size hints */
938 set_size_hints( display, data, style );
940 /* set the WM_WINDOW_TYPE */
941 window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
942 if (ex_style & WS_EX_TOOLWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
943 else if (style & WS_THICKFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
944 else if (style & WS_DLGFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
945 else if (ex_style & WS_EX_DLGMODALFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
947 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
948 XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
950 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
951 mwm_hints.decorations = get_mwm_decorations( style, ex_style );
952 mwm_hints.functions = MWM_FUNC_MOVE;
953 if (style & WS_THICKFRAME) mwm_hints.functions |= MWM_FUNC_RESIZE;
954 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
955 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
956 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
958 XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
959 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
960 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
962 /* wm hints */
963 if (data->wm_hints)
965 data->wm_hints->flags |= InputHint | StateHint | WindowGroupHint;
966 data->wm_hints->input = !(style & WS_DISABLED);
967 data->wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
968 data->wm_hints->window_group = group_leader;
969 XSetWMHints( display, data->whole_window, data->wm_hints );
972 wine_tsx11_unlock();
976 /***********************************************************************
977 * X11DRV_set_iconic_state
979 * Set the X11 iconic state according to the window style.
981 void X11DRV_set_iconic_state( HWND hwnd )
983 Display *display = thread_display();
984 struct x11drv_win_data *data;
985 RECT rect;
986 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
987 BOOL iconic = (style & WS_MINIMIZE) != 0;
989 if (!(data = X11DRV_get_win_data( hwnd ))) return;
990 if (!data->whole_window) return;
992 GetWindowRect( hwnd, &rect );
994 wine_tsx11_lock();
996 if (data->wm_hints)
998 data->wm_hints->flags |= StateHint | IconPositionHint;
999 data->wm_hints->initial_state = iconic ? IconicState : NormalState;
1000 data->wm_hints->icon_x = rect.left - virtual_screen_rect.left;
1001 data->wm_hints->icon_y = rect.top - virtual_screen_rect.top;
1002 XSetWMHints( display, data->whole_window, data->wm_hints );
1005 if (data->mapped)
1007 if (iconic)
1008 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
1009 else
1010 if (X11DRV_is_window_rect_mapped( &rect ))
1011 XMapWindow( display, data->whole_window );
1014 wine_tsx11_unlock();
1018 /***********************************************************************
1019 * X11DRV_window_to_X_rect
1021 * Convert a rect from client to X window coordinates
1023 void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
1025 RECT rc;
1027 if (!data->managed) return;
1028 if (IsRectEmpty( rect )) return;
1030 get_x11_rect_offset( data, &rc );
1032 rect->left -= rc.left;
1033 rect->right -= rc.right;
1034 rect->top -= rc.top;
1035 rect->bottom -= rc.bottom;
1036 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1037 if (rect->left >= rect->right) rect->right = rect->left + 1;
1041 /***********************************************************************
1042 * X11DRV_X_to_window_rect
1044 * Opposite of X11DRV_window_to_X_rect
1046 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
1048 RECT rc;
1050 if (!data->managed) return;
1051 if (IsRectEmpty( rect )) return;
1053 get_x11_rect_offset( data, &rc );
1055 rect->left += rc.left;
1056 rect->right += rc.right;
1057 rect->top += rc.top;
1058 rect->bottom += rc.bottom;
1059 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1060 if (rect->left >= rect->right) rect->right = rect->left + 1;
1064 /***********************************************************************
1065 * X11DRV_sync_window_position
1067 * Synchronize the X window position with the Windows one
1069 void X11DRV_sync_window_position( Display *display, struct x11drv_win_data *data,
1070 UINT swp_flags, const RECT *old_client_rect,
1071 const RECT *old_whole_rect )
1073 XWindowChanges changes;
1074 int mask = get_window_changes( &changes, old_whole_rect, &data->whole_rect );
1076 if (!(swp_flags & SWP_NOZORDER))
1078 /* find window that this one must be after */
1079 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
1080 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
1081 prev = GetWindow( prev, GW_HWNDPREV );
1082 if (!prev) /* top child */
1084 changes.stack_mode = Above;
1085 mask |= CWStackMode;
1087 else
1089 /* should use stack_mode Below but most window managers don't get it right */
1090 /* so move it above the next one in Z order */
1091 HWND next = GetWindow( data->hwnd, GW_HWNDNEXT );
1092 while (next && !(GetWindowLongW( next, GWL_STYLE ) & WS_VISIBLE))
1093 next = GetWindow( next, GW_HWNDNEXT );
1094 if (next)
1096 changes.stack_mode = Above;
1097 changes.sibling = X11DRV_get_whole_window(next);
1098 mask |= CWStackMode | CWSibling;
1103 /* only the size is allowed to change for the desktop window */
1104 if (data->whole_window == root_window) mask &= CWWidth | CWHeight;
1106 if (mask)
1108 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
1110 TRACE( "setting win %lx pos %d,%d,%dx%d after %lx changes=%x\n",
1111 data->whole_window, data->whole_rect.left, data->whole_rect.top,
1112 data->whole_rect.right - data->whole_rect.left,
1113 data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
1115 wine_tsx11_lock();
1116 if (mask & (CWWidth|CWHeight)) set_size_hints( display, data, style );
1117 if (mask & CWX) changes.x -= virtual_screen_rect.left;
1118 if (mask & CWY) changes.y -= virtual_screen_rect.top;
1119 XReconfigureWMWindow( display, data->whole_window,
1120 DefaultScreen(display), mask, &changes );
1121 wine_tsx11_unlock();
1126 /***********************************************************************
1127 * X11DRV_sync_client_position
1129 * Synchronize the X client window position with the Windows one
1131 void X11DRV_sync_client_position( Display *display, struct x11drv_win_data *data,
1132 UINT swp_flags, const RECT *old_client_rect,
1133 const RECT *old_whole_rect )
1135 int mask;
1136 XWindowChanges changes;
1137 RECT old = *old_client_rect;
1138 RECT new = data->client_rect;
1140 OffsetRect( &old, -old_whole_rect->left, -old_whole_rect->top );
1141 OffsetRect( &new, -data->whole_rect.left, -data->whole_rect.top );
1142 if (!(mask = get_window_changes( &changes, &old, &new ))) return;
1144 if (data->client_window)
1146 TRACE( "setting client win %lx pos %d,%d,%dx%d changes=%x\n",
1147 data->client_window, new.left, new.top,
1148 new.right - new.left, new.bottom - new.top, mask );
1149 wine_tsx11_lock();
1150 XConfigureWindow( display, data->client_window, mask, &changes );
1151 wine_tsx11_unlock();
1154 if (data->gl_drawable && (mask & (CWWidth|CWHeight))) sync_gl_drawable( display, data );
1156 /* make sure the changes get to the server before we start painting */
1157 if (data->client_window || data->gl_drawable)
1159 wine_tsx11_lock();
1160 XFlush(display);
1161 wine_tsx11_unlock();
1166 /**********************************************************************
1167 * create_whole_window
1169 * Create the whole X window for a given window
1171 static Window create_whole_window( Display *display, struct x11drv_win_data *data )
1173 int cx, cy, mask;
1174 XSetWindowAttributes attr;
1175 XIM xim;
1176 WCHAR text[1024];
1177 HRGN hrgn;
1179 if (!(cx = data->window_rect.right - data->window_rect.left)) cx = 1;
1180 if (!(cy = data->window_rect.bottom - data->window_rect.top)) cy = 1;
1182 if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1184 TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1185 data->managed = TRUE;
1186 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1189 mask = get_window_attributes( display, data, &attr );
1191 wine_tsx11_lock();
1193 data->whole_rect = data->window_rect;
1194 data->whole_window = XCreateWindow( display, root_window,
1195 data->window_rect.left - virtual_screen_rect.left,
1196 data->window_rect.top - virtual_screen_rect.top,
1197 cx, cy, 0, screen_depth, InputOutput,
1198 visual, mask, &attr );
1200 if (data->whole_window) XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
1201 wine_tsx11_unlock();
1203 if (!data->whole_window) return 0;
1205 if (!create_client_window( display, data, NULL ))
1207 wine_tsx11_lock();
1208 XDeleteContext( display, data->whole_window, winContext );
1209 XDestroyWindow( display, data->whole_window );
1210 data->whole_window = 0;
1211 wine_tsx11_unlock();
1212 return 0;
1215 xim = x11drv_thread_data()->xim;
1216 if (xim) data->xic = X11DRV_CreateIC( xim, display, data->whole_window );
1218 set_initial_wm_hints( display, data );
1219 X11DRV_set_wm_hints( display, data );
1221 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1223 /* set the window text */
1224 if (!InternalGetWindowText( data->hwnd, text, sizeof(text)/sizeof(WCHAR) )) text[0] = 0;
1225 sync_window_text( display, data->whole_window, text );
1227 /* set the window region */
1228 if ((hrgn = CreateRectRgn( 0, 0, 0, 0 )))
1230 if (GetWindowRgn( data->hwnd, hrgn ) != ERROR) sync_window_region( display, data, hrgn );
1231 DeleteObject( hrgn );
1233 return data->whole_window;
1237 /**********************************************************************
1238 * destroy_whole_window
1240 * Destroy the whole X window for a given window.
1242 static void destroy_whole_window( Display *display, struct x11drv_win_data *data )
1244 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1246 if (!data->whole_window) return;
1248 TRACE( "win %p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1249 if (thread_data->cursor_window == data->whole_window ||
1250 thread_data->cursor_window == data->client_window)
1251 thread_data->cursor_window = None;
1252 wine_tsx11_lock();
1253 XDeleteContext( display, data->whole_window, winContext );
1254 XDeleteContext( display, data->client_window, winContext );
1255 XDestroyWindow( display, data->whole_window );
1256 data->whole_window = data->client_window = 0;
1257 if (data->xic)
1259 XUnsetICFocus( data->xic );
1260 XDestroyIC( data->xic );
1262 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1263 XFlush( display );
1264 XFree( data->wm_hints );
1265 data->wm_hints = NULL;
1266 wine_tsx11_unlock();
1267 RemovePropA( data->hwnd, whole_window_prop );
1268 RemovePropA( data->hwnd, client_window_prop );
1272 /*****************************************************************
1273 * SetWindowText (X11DRV.@)
1275 void X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1277 Display *display = thread_display();
1278 Window win;
1280 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(display))
1281 sync_window_text( display, win, text );
1285 /***********************************************************************
1286 * DestroyWindow (X11DRV.@)
1288 void X11DRV_DestroyWindow( HWND hwnd )
1290 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1291 Display *display = thread_data->display;
1292 struct x11drv_win_data *data;
1294 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1296 if (data->pixmap)
1298 destroy_glxpixmap(display, data->gl_drawable);
1299 wine_tsx11_lock();
1300 XFreePixmap(display, data->pixmap);
1301 wine_tsx11_unlock();
1303 else if (data->gl_drawable)
1305 wine_tsx11_lock();
1306 XDestroyWindow(display, data->gl_drawable);
1307 wine_tsx11_unlock();
1310 destroy_whole_window( display, data );
1311 destroy_icon_window( display, data );
1313 if (data->colormap)
1315 wine_tsx11_lock();
1316 XFreeColormap( display, data->colormap );
1317 wine_tsx11_unlock();
1320 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1321 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
1322 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
1323 wine_tsx11_lock();
1324 XDeleteContext( display, (XID)hwnd, win_data_context );
1325 wine_tsx11_unlock();
1326 HeapFree( GetProcessHeap(), 0, data );
1330 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
1332 struct x11drv_win_data *data;
1334 if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
1336 data->hwnd = hwnd;
1337 wine_tsx11_lock();
1338 if (!winContext) winContext = XUniqueContext();
1339 if (!win_data_context) win_data_context = XUniqueContext();
1340 XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
1341 wine_tsx11_unlock();
1343 return data;
1347 /* initialize the desktop window id in the desktop manager process */
1348 static struct x11drv_win_data *create_desktop_win_data( Display *display, HWND hwnd )
1350 struct x11drv_win_data *data;
1351 VisualID visualid;
1353 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1354 wine_tsx11_lock();
1355 visualid = XVisualIDFromVisual(visual);
1356 wine_tsx11_unlock();
1357 data->whole_window = data->client_window = root_window;
1358 data->managed = TRUE;
1359 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1360 SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
1361 SetPropA( data->hwnd, client_window_prop, (HANDLE)root_window );
1362 set_initial_wm_hints( display, data );
1363 return data;
1366 /**********************************************************************
1367 * CreateDesktopWindow (X11DRV.@)
1369 BOOL X11DRV_CreateDesktopWindow( HWND hwnd )
1371 unsigned int width, height;
1373 /* retrieve the real size of the desktop */
1374 SERVER_START_REQ( get_window_rectangles )
1376 req->handle = hwnd;
1377 wine_server_call( req );
1378 width = reply->window.right - reply->window.left;
1379 height = reply->window.bottom - reply->window.top;
1381 SERVER_END_REQ;
1383 if (!width && !height) /* not initialized yet */
1385 SERVER_START_REQ( set_window_pos )
1387 req->handle = hwnd;
1388 req->previous = 0;
1389 req->flags = SWP_NOZORDER;
1390 req->window.left = virtual_screen_rect.left;
1391 req->window.top = virtual_screen_rect.top;
1392 req->window.right = virtual_screen_rect.right;
1393 req->window.bottom = virtual_screen_rect.bottom;
1394 req->client = req->window;
1395 wine_server_call( req );
1397 SERVER_END_REQ;
1399 else
1401 Window win = (Window)GetPropA( hwnd, whole_window_prop );
1402 if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1404 return TRUE;
1408 /**********************************************************************
1409 * CreateWindow (X11DRV.@)
1411 BOOL X11DRV_CreateWindow( HWND hwnd )
1413 Display *display = thread_display();
1414 DWORD style;
1416 if (hwnd == GetDesktopWindow() && root_window != DefaultRootWindow( display ))
1418 /* the desktop win data can't be created lazily */
1419 if (!create_desktop_win_data( display, hwnd )) return FALSE;
1422 /* Show the window, maximizing or minimizing if needed */
1424 style = GetWindowLongW( hwnd, GWL_STYLE );
1425 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1427 extern UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect ); /*FIXME*/
1429 RECT newPos;
1430 UINT swFlag = (style & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1431 WIN_SetStyle( hwnd, 0, WS_MAXIMIZE | WS_MINIMIZE );
1432 swFlag = WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1434 swFlag |= SWP_FRAMECHANGED; /* Frame always gets changed */
1435 if (!(style & WS_VISIBLE) || (style & WS_CHILD) || GetActiveWindow()) swFlag |= SWP_NOACTIVATE;
1437 SetWindowPos( hwnd, 0, newPos.left, newPos.top,
1438 newPos.right, newPos.bottom, swFlag );
1441 return TRUE;
1445 /***********************************************************************
1446 * X11DRV_get_win_data
1448 * Return the X11 data structure associated with a window.
1450 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1452 char *data;
1454 if (!hwnd || XFindContext( thread_display(), (XID)hwnd, win_data_context, &data )) data = NULL;
1455 return (struct x11drv_win_data *)data;
1459 /***********************************************************************
1460 * X11DRV_create_win_data
1462 * Create an X11 data window structure for an existing window.
1464 struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd )
1466 Display *display = thread_display();
1467 struct x11drv_win_data *data;
1468 HWND parent;
1470 if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL; /* desktop */
1471 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1473 GetWindowRect( hwnd, &data->window_rect );
1474 MapWindowPoints( 0, parent, (POINT *)&data->window_rect, 2 );
1475 data->whole_rect = data->window_rect;
1476 GetClientRect( hwnd, &data->client_rect );
1477 MapWindowPoints( hwnd, parent, (POINT *)&data->client_rect, 2 );
1479 if (parent == GetDesktopWindow())
1481 if (!create_whole_window( display, data ))
1483 HeapFree( GetProcessHeap(), 0, data );
1484 return NULL;
1486 TRACE( "win %p/%lx/%lx window %s whole %s client %s\n",
1487 hwnd, data->whole_window, data->client_window, wine_dbgstr_rect( &data->window_rect ),
1488 wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
1490 return data;
1494 /***********************************************************************
1495 * X11DRV_get_whole_window
1497 * Return the X window associated with the full area of a window
1499 Window X11DRV_get_whole_window( HWND hwnd )
1501 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1503 if (!data)
1505 if (hwnd == GetDesktopWindow()) return root_window;
1506 return (Window)GetPropA( hwnd, whole_window_prop );
1508 return data->whole_window;
1512 /***********************************************************************
1513 * X11DRV_get_client_window
1515 * Return the X window associated with the client area of a window
1517 Window X11DRV_get_client_window( HWND hwnd )
1519 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1521 if (!data)
1523 if (hwnd == GetDesktopWindow()) return root_window;
1524 return (Window)GetPropA( hwnd, client_window_prop );
1526 return data->client_window;
1530 /***********************************************************************
1531 * X11DRV_get_ic
1533 * Return the X input context associated with a window
1535 XIC X11DRV_get_ic( HWND hwnd )
1537 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1539 if (!data) return 0;
1540 return data->xic;
1544 /***********************************************************************
1545 * X11DRV_GetDC (X11DRV.@)
1547 void X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
1548 const RECT *top_rect, DWORD flags )
1550 struct x11drv_escape_set_drawable escape;
1551 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1553 escape.code = X11DRV_SET_DRAWABLE;
1554 escape.mode = IncludeInferiors;
1555 escape.fbconfig_id = 0;
1556 escape.gl_drawable = 0;
1557 escape.pixmap = 0;
1559 if (top == hwnd && data && IsIconic( hwnd ) && data->icon_window)
1561 escape.drawable = data->icon_window;
1563 else if (top == hwnd && (flags & DCX_WINDOW))
1565 escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
1567 else
1569 escape.drawable = X11DRV_get_client_window( top );
1570 escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
1571 escape.gl_drawable = data ? data->gl_drawable : (Drawable)GetPropA( hwnd, gl_drawable_prop );
1572 escape.pixmap = data ? data->pixmap : (Pixmap)GetPropA( hwnd, pixmap_prop );
1575 escape.dc_rect.left = win_rect->left - top_rect->left;
1576 escape.dc_rect.top = win_rect->top - top_rect->top;
1577 escape.dc_rect.right = win_rect->right - top_rect->left;
1578 escape.dc_rect.bottom = win_rect->bottom - top_rect->top;
1579 escape.drawable_rect.left = top_rect->left;
1580 escape.drawable_rect.top = top_rect->top;
1581 escape.drawable_rect.right = top_rect->right;
1582 escape.drawable_rect.bottom = top_rect->bottom;
1584 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1588 /***********************************************************************
1589 * X11DRV_ReleaseDC (X11DRV.@)
1591 void X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
1593 struct x11drv_escape_set_drawable escape;
1595 escape.code = X11DRV_SET_DRAWABLE;
1596 escape.drawable = root_window;
1597 escape.mode = IncludeInferiors;
1598 escape.drawable_rect = virtual_screen_rect;
1599 SetRect( &escape.dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
1600 virtual_screen_rect.bottom - virtual_screen_rect.top );
1601 escape.fbconfig_id = 0;
1602 escape.gl_drawable = 0;
1603 escape.pixmap = 0;
1604 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1608 /*****************************************************************
1609 * SetParent (X11DRV.@)
1611 void X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
1613 Display *display = thread_display();
1614 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1616 if (!data) return;
1617 if (parent == old_parent) return;
1619 if (parent != GetDesktopWindow()) /* a child window */
1621 if (old_parent == GetDesktopWindow())
1623 /* destroy the old X windows */
1624 destroy_whole_window( display, data );
1625 destroy_icon_window( display, data );
1626 if (data->managed)
1628 data->managed = FALSE;
1629 RemovePropA( data->hwnd, managed_prop );
1633 else /* new top level window */
1635 /* FIXME: we ignore errors since we can't really recover anyway */
1636 create_whole_window( display, data );
1641 /*****************************************************************
1642 * SetFocus (X11DRV.@)
1644 * Set the X focus.
1645 * Explicit colormap management seems to work only with OLVWM.
1647 void X11DRV_SetFocus( HWND hwnd )
1649 Display *display = thread_display();
1650 struct x11drv_win_data *data;
1651 XWindowChanges changes;
1653 /* If setting the focus to 0, uninstall the colormap */
1654 if (!hwnd && root_window == DefaultRootWindow(display))
1656 wine_tsx11_lock();
1657 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1658 XUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1659 wine_tsx11_unlock();
1660 return;
1663 hwnd = GetAncestor( hwnd, GA_ROOT );
1665 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1666 if (data->managed || !data->whole_window) return;
1668 /* Set X focus and install colormap */
1669 wine_tsx11_lock();
1670 changes.stack_mode = Above;
1671 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
1672 if (root_window == DefaultRootWindow(display))
1674 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1675 /* FIXME: this is not entirely correct */
1676 XSetInputFocus( display, data->whole_window, RevertToParent,
1677 /* CurrentTime */
1678 GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1679 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1680 XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1682 wine_tsx11_unlock();
1686 /**********************************************************************
1687 * SetWindowIcon (X11DRV.@)
1689 * hIcon or hIconSm has changed (or is being initialised for the
1690 * first time). Complete the X11 driver-specific initialisation
1691 * and set the window hints.
1693 * This is not entirely correct, may need to create
1694 * an icon window and set the pixmap as a background
1696 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
1698 Display *display = thread_display();
1699 struct x11drv_win_data *data;
1701 if (type != ICON_BIG) return; /* nothing to do here */
1703 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1704 if (!data->whole_window) return;
1705 if (!data->managed) return;
1707 if (data->wm_hints)
1709 set_icon_hints( display, data, icon );
1710 wine_tsx11_lock();
1711 XSetWMHints( display, data->whole_window, data->wm_hints );
1712 wine_tsx11_unlock();
1717 /***********************************************************************
1718 * SetWindowRgn (X11DRV.@)
1720 * Assign specified region to window (for non-rectangular windows)
1722 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
1724 struct x11drv_win_data *data;
1726 if ((data = X11DRV_get_win_data( hwnd )))
1728 sync_window_region( thread_display(), data, hrgn );
1730 else if (GetWindowThreadProcessId( hwnd, NULL ) != GetCurrentThreadId())
1732 FIXME( "not supported on other thread window %p\n", hwnd );
1733 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1734 return FALSE;
1737 return TRUE;