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
33 #include <X11/Xresource.h>
34 #include <X11/Xutil.h>
41 #include "wine/unicode.h"
44 #include "wine/debug.h"
45 #include "wine/server.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
51 /* X context to associate a hwnd to an X window */
52 XContext winContext = 0;
54 /* X context to associate a struct x11drv_win_data to an hwnd */
55 static XContext win_data_context;
57 static const char whole_window_prop[] = "__wine_x11_whole_window";
58 static const char icon_window_prop[] = "__wine_x11_icon_window";
59 static const char managed_prop[] = "__wine_x11_managed";
60 static const char visual_id_prop[] = "__wine_x11_visual_id";
62 /* for XDG systray icons */
63 #define SYSTEM_TRAY_REQUEST_DOCK 0
65 /***********************************************************************
68 * Check if a given window should be managed
70 inline static BOOL is_window_managed( HWND hwnd )
72 DWORD style, ex_style;
74 if (!managed_mode) return FALSE;
75 /* tray window is always managed */
76 ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
77 if (ex_style & WS_EX_TRAYWINDOW) return TRUE;
78 /* child windows are not managed */
79 style = GetWindowLongW( hwnd, GWL_STYLE );
80 if (style & WS_CHILD) return FALSE;
81 /* windows with caption are managed */
82 if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
83 /* tool windows are not managed */
84 if (ex_style & WS_EX_TOOLWINDOW) return FALSE;
85 /* windows with thick frame are managed */
86 if (style & WS_THICKFRAME) return TRUE;
87 /* application windows are managed */
88 if (ex_style & WS_EX_APPWINDOW) return TRUE;
89 /* full-screen popup windows are managed */
93 GetWindowRect( hwnd, &rect );
94 if ((rect.right - rect.left) == screen_width && (rect.bottom - rect.top) == screen_height)
97 /* default: not managed */
102 /***********************************************************************
103 * X11DRV_is_window_rect_mapped
105 * Check if the X whole window should be mapped based on its rectangle
107 BOOL X11DRV_is_window_rect_mapped( const RECT *rect )
109 /* don't map if rect is empty */
110 if (IsRectEmpty( rect )) return FALSE;
112 /* don't map if rect is off-screen */
113 if (rect->left >= virtual_screen_rect.right ||
114 rect->top >= virtual_screen_rect.bottom ||
115 rect->right <= virtual_screen_rect.left ||
116 rect->bottom <= virtual_screen_rect.top)
123 /***********************************************************************
124 * get_window_attributes
126 * Fill the window attributes structure for an X window.
128 static int get_window_attributes( Display *display, struct x11drv_win_data *data,
129 XSetWindowAttributes *attr )
131 if (!data->managed &&
132 root_window == DefaultRootWindow( display ) &&
133 data->whole_window != root_window &&
134 is_window_managed( data->hwnd ))
136 data->managed = TRUE;
137 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
139 attr->override_redirect = !data->managed;
140 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
141 attr->save_under = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
142 attr->cursor = x11drv_thread_data()->cursor;
143 return (CWOverrideRedirect | CWSaveUnder | CWColormap | CWCursor);
147 /***********************************************************************
148 * X11DRV_sync_window_style
150 * Change the X window attributes when the window style has changed.
152 void X11DRV_sync_window_style( Display *display, struct x11drv_win_data *data )
154 XSetWindowAttributes attr;
155 int mask = get_window_attributes( display, data, &attr );
158 if (data->whole_window != DefaultRootWindow(display))
159 XChangeWindowAttributes( display, data->whole_window, mask, &attr );
164 /***********************************************************************
167 * fill the window changes structure
169 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
173 if (old->right - old->left != new->right - new->left )
175 if (!(changes->width = new->right - new->left)) changes->width = 1;
178 if (old->bottom - old->top != new->bottom - new->top)
180 if (!(changes->height = new->bottom - new->top)) changes->height = 1;
183 if (old->left != new->left)
185 changes->x = new->left;
188 if (old->top != new->top)
190 changes->y = new->top;
197 /***********************************************************************
200 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
202 XSetWindowAttributes attr;
204 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
205 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
206 attr.bit_gravity = NorthWestGravity;
207 attr.backing_store = NotUseful/*WhenMapped*/;
208 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
211 data->icon_window = XCreateWindow( display, root_window, 0, 0,
212 GetSystemMetrics( SM_CXICON ),
213 GetSystemMetrics( SM_CYICON ),
216 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
217 XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
220 TRACE( "created %lx\n", data->icon_window );
221 SetPropA( data->hwnd, icon_window_prop, (HANDLE)data->icon_window );
222 return data->icon_window;
227 /***********************************************************************
228 * destroy_icon_window
230 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
232 if (!data->icon_window) return;
233 if (x11drv_thread_data()->cursor_window == data->icon_window)
234 x11drv_thread_data()->cursor_window = None;
236 XDeleteContext( display, data->icon_window, winContext );
237 XDestroyWindow( display, data->icon_window );
238 data->icon_window = 0;
240 RemovePropA( data->hwnd, icon_window_prop );
244 /***********************************************************************
247 * Set the icon wm hints
249 static void set_icon_hints( Display *display, struct x11drv_win_data *data,
250 XWMHints *hints, HICON hIcon )
252 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
253 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
254 data->hWMIconBitmap = 0;
255 data->hWMIconMask = 0;
259 destroy_icon_window( display, data );
260 hints->flags &= ~(IconPixmapHint | IconMaskHint | IconWindowHint);
264 if (!data->icon_window) create_icon_window( display, data );
265 hints->icon_window = data->icon_window;
266 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
276 GetIconInfo(hIcon, &ii);
278 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
281 rcMask.right = bmMask.bmWidth;
282 rcMask.bottom = bmMask.bmHeight;
284 hDC = CreateCompatibleDC(0);
285 hbmOrig = SelectObject(hDC, ii.hbmMask);
286 InvertRect(hDC, &rcMask);
287 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
288 SelectObject(hDC, hbmOrig);
291 data->hWMIconBitmap = ii.hbmColor;
292 data->hWMIconMask = ii.hbmMask;
294 hints->icon_pixmap = X11DRV_get_pixmap(data->hWMIconBitmap);
295 hints->icon_mask = X11DRV_get_pixmap(data->hWMIconMask);
296 destroy_icon_window( display, data );
297 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
301 /***********************************************************************
302 * systray_dock_window
304 * Docks the given X window with the NETWM system tray.
306 static void systray_dock_window( Display *display, struct x11drv_win_data *data )
308 static Atom systray_atom;
309 Window systray_window;
314 if (DefaultScreen( display ) == 0)
315 systray_atom = x11drv_atom(_NET_SYSTEM_TRAY_S0);
318 char systray_buffer[29]; /* strlen(_NET_SYSTEM_TRAY_S4294967295)+1 */
319 sprintf( systray_buffer, "_NET_SYSTEM_TRAY_S%u", DefaultScreen( display ) );
320 systray_atom = XInternAtom( display, systray_buffer, False );
323 systray_window = XGetSelectionOwner( display, systray_atom );
326 TRACE("Docking tray icon %p\n", data->hwnd);
328 if (systray_window != None)
331 unsigned long info[2];
333 /* Put the window offscreen so it isn't mapped. The window _cannot_ be
334 * mapped if we intend to dock with an XEMBED tray. If the window is
335 * mapped when we dock, it may become visible as a child of the root
336 * window after it docks, which isn't the proper behavior.
338 * For more information on this problem, see
339 * http://standards.freedesktop.org/xembed-spec/latest/ar01s04.html */
341 SetWindowPos( data->hwnd, NULL, virtual_screen_rect.right + 1, virtual_screen_rect.bottom + 1,
342 0, 0, SWP_NOZORDER | SWP_NOSIZE );
344 /* set XEMBED protocol data on the window */
345 info[0] = 0; /* protocol version */
346 info[1] = 1; /* mapped = true */
349 XChangeProperty( display, data->whole_window,
350 x11drv_atom(_XEMBED_INFO),
351 x11drv_atom(_XEMBED_INFO), 32, PropModeReplace,
352 (unsigned char*)info, 2 );
355 /* send the docking request message */
356 ZeroMemory( &ev, sizeof(ev) );
357 ev.xclient.type = ClientMessage;
358 ev.xclient.window = systray_window;
359 ev.xclient.message_type = x11drv_atom( _NET_SYSTEM_TRAY_OPCODE );
360 ev.xclient.format = 32;
361 ev.xclient.data.l[0] = CurrentTime;
362 ev.xclient.data.l[1] = SYSTEM_TRAY_REQUEST_DOCK;
363 ev.xclient.data.l[2] = data->whole_window;
364 ev.xclient.data.l[3] = 0;
365 ev.xclient.data.l[4] = 0;
368 XSendEvent( display, systray_window, False, NoEventMask, &ev );
376 /* fall back to he KDE hints if the WM doesn't support XEMBED'ed
380 XChangeProperty( display, data->whole_window,
381 x11drv_atom(KWM_DOCKWINDOW),
382 x11drv_atom(KWM_DOCKWINDOW), 32, PropModeReplace,
383 (unsigned char*)&val, 1 );
384 XChangeProperty( display, data->whole_window,
385 x11drv_atom(_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR),
386 XA_WINDOW, 32, PropModeReplace,
387 (unsigned char*)&data->whole_window, 1 );
393 /***********************************************************************
396 * set the window size hints
398 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
400 XSizeHints* size_hints;
402 if ((size_hints = XAllocSizeHints()))
404 size_hints->flags = 0;
406 if (data->hwnd != GetDesktopWindow()) /* don't force position of desktop */
408 size_hints->win_gravity = StaticGravity;
409 size_hints->x = data->whole_rect.left;
410 size_hints->y = data->whole_rect.top;
411 size_hints->flags |= PWinGravity | PPosition;
414 if ( !(style & WS_THICKFRAME) )
416 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
417 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
418 size_hints->min_width = size_hints->max_width;
419 size_hints->min_height = size_hints->max_height;
420 size_hints->flags |= PMinSize | PMaxSize;
422 XSetWMNormalHints( display, data->whole_window, size_hints );
428 /***********************************************************************
431 * get the name of the current process for setting class hints
433 static char *get_process_name(void)
439 WCHAR module[MAX_PATH];
440 DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
441 if (len && len < MAX_PATH)
444 WCHAR *p, *appname = module;
446 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
447 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
448 len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
449 if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
451 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
460 /***********************************************************************
461 * X11DRV_set_wm_hints
463 * Set the window manager hints for a newly-created window
465 void X11DRV_set_wm_hints( Display *display, struct x11drv_win_data *data )
468 XClassHint *class_hints;
475 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
476 DWORD ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
477 HWND owner = GetWindow( data->hwnd, GW_OWNER );
478 char *process_name = get_process_name();
480 if (data->hwnd == GetDesktopWindow())
482 if (data->whole_window == DefaultRootWindow(display)) return;
483 /* force some styles for the desktop to get the correct decorations */
484 style |= WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
488 /* transient for hint */
491 Window owner_win = X11DRV_get_whole_window( owner );
493 XSetTransientForHint( display, data->whole_window, owner_win );
495 group_leader = owner_win;
497 else group_leader = data->whole_window;
503 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
504 protocols[i++] = x11drv_atom(_NET_WM_PING);
505 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
506 XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
507 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
510 if ((class_hints = XAllocClassHint()))
512 static char wine[] = "Wine";
514 class_hints->res_name = process_name;
515 class_hints->res_class = wine;
516 XSetClassHint( display, data->whole_window, class_hints );
517 XFree( class_hints );
521 set_size_hints( display, data, style );
523 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
524 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
525 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
527 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
528 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
530 /* set the WM_WINDOW_TYPE */
531 window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
532 if (ex_style & WS_EX_TOOLWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
533 else if (style & WS_THICKFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
534 else if (style & WS_DLGFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
535 else if (ex_style & WS_EX_DLGMODALFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
537 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
538 XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
540 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
541 mwm_hints.functions = MWM_FUNC_MOVE;
542 if (style & WS_THICKFRAME) mwm_hints.functions |= MWM_FUNC_RESIZE;
543 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
544 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
545 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
546 mwm_hints.decorations = 0;
547 if ((style & WS_CAPTION) == WS_CAPTION)
549 mwm_hints.decorations |= MWM_DECOR_TITLE;
550 if (style & WS_SYSMENU) mwm_hints.decorations |= MWM_DECOR_MENU;
551 if (style & WS_MINIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MINIMIZE;
552 if (style & WS_MAXIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MAXIMIZE;
554 if (ex_style & WS_EX_DLGMODALFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
555 else if (style & WS_THICKFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
556 else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
557 else if (style & WS_BORDER) mwm_hints.decorations |= MWM_DECOR_BORDER;
558 else if (!(style & (WS_CHILD|WS_POPUP))) mwm_hints.decorations |= MWM_DECOR_BORDER;
560 XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
561 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
562 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
564 XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
565 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
567 wm_hints = XAllocWMHints();
573 wm_hints->flags = InputHint | StateHint | WindowGroupHint;
574 wm_hints->input = !(style & WS_DISABLED);
576 set_icon_hints( display, data, wm_hints,
577 (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON ) );
579 wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
580 wm_hints->window_group = group_leader;
583 XSetWMHints( display, data->whole_window, wm_hints );
590 /***********************************************************************
591 * X11DRV_set_iconic_state
593 * Set the X11 iconic state according to the window style.
595 void X11DRV_set_iconic_state( HWND hwnd )
597 Display *display = thread_display();
598 struct x11drv_win_data *data;
601 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
602 BOOL iconic = (style & WS_MINIMIZE) != 0;
604 if (!(data = X11DRV_get_win_data( hwnd ))) return;
605 if (!data->whole_window || data->whole_window == DefaultRootWindow(display)) return;
607 GetWindowRect( hwnd, &rect );
611 if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
612 wm_hints->flags |= StateHint | IconPositionHint;
613 wm_hints->initial_state = iconic ? IconicState : NormalState;
614 wm_hints->icon_x = rect.left - virtual_screen_rect.left;
615 wm_hints->icon_y = rect.top - virtual_screen_rect.top;
616 XSetWMHints( display, data->whole_window, wm_hints );
618 if (style & WS_VISIBLE)
621 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
623 if (X11DRV_is_window_rect_mapped( &rect ))
624 XMapWindow( display, data->whole_window );
632 /***********************************************************************
633 * X11DRV_window_to_X_rect
635 * Convert a rect from client to X window coordinates
637 void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
641 if (!data->managed) return;
642 if (IsRectEmpty( rect )) return;
644 rc.top = rc.bottom = rc.left = rc.right = 0;
646 AdjustWindowRectEx( &rc, GetWindowLongW( data->hwnd, GWL_STYLE ) & ~(WS_HSCROLL|WS_VSCROLL),
647 FALSE, GetWindowLongW( data->hwnd, GWL_EXSTYLE ) );
649 rect->left -= rc.left;
650 rect->right -= rc.right;
652 rect->bottom -= rc.bottom;
653 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
654 if (rect->left >= rect->right) rect->right = rect->left + 1;
658 /***********************************************************************
659 * X11DRV_X_to_window_rect
661 * Opposite of X11DRV_window_to_X_rect
663 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
665 if (!data->managed) return;
666 if (IsRectEmpty( rect )) return;
668 AdjustWindowRectEx( rect, GetWindowLongW( data->hwnd, GWL_STYLE ) & ~(WS_HSCROLL|WS_VSCROLL),
669 FALSE, GetWindowLongW( data->hwnd, GWL_EXSTYLE ));
671 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
672 if (rect->left >= rect->right) rect->right = rect->left + 1;
676 /***********************************************************************
677 * X11DRV_sync_window_position
679 * Synchronize the X window position with the Windows one
681 void X11DRV_sync_window_position( Display *display, struct x11drv_win_data *data,
682 UINT swp_flags, const RECT *new_client_rect,
683 const RECT *new_whole_rect )
685 XWindowChanges changes;
689 old_whole_rect = data->whole_rect;
690 data->whole_rect = *new_whole_rect;
692 data->client_rect = *new_client_rect;
693 OffsetRect( &data->client_rect, -data->whole_rect.left, -data->whole_rect.top );
695 if (!data->whole_window || data->lock_changes) return;
697 mask = get_window_changes( &changes, &old_whole_rect, &data->whole_rect );
699 if (!(swp_flags & SWP_NOZORDER))
701 /* find window that this one must be after */
702 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
703 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
704 prev = GetWindow( prev, GW_HWNDPREV );
705 if (!prev) /* top child */
707 changes.stack_mode = Above;
712 /* should use stack_mode Below but most window managers don't get it right */
713 /* so move it above the next one in Z order */
714 HWND next = GetWindow( data->hwnd, GW_HWNDNEXT );
715 while (next && !(GetWindowLongW( next, GWL_STYLE ) & WS_VISIBLE))
716 next = GetWindow( next, GW_HWNDNEXT );
719 changes.stack_mode = Above;
720 changes.sibling = X11DRV_get_whole_window(next);
721 mask |= CWStackMode | CWSibling;
728 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
730 TRACE( "setting win %lx pos %d,%d,%dx%d after %lx changes=%x\n",
731 data->whole_window, data->whole_rect.left, data->whole_rect.top,
732 data->whole_rect.right - data->whole_rect.left,
733 data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
736 if (mask & (CWWidth|CWHeight)) set_size_hints( display, data, style );
737 if (mask & CWX) changes.x -= virtual_screen_rect.left;
738 if (mask & CWY) changes.y -= virtual_screen_rect.top;
739 XReconfigureWMWindow( display, data->whole_window,
740 DefaultScreen(display), mask, &changes );
746 /**********************************************************************
747 * create_whole_window
749 * Create the whole X window for a given window
751 static Window create_whole_window( Display *display, struct x11drv_win_data *data, DWORD style )
754 XSetWindowAttributes attr;
758 rect = data->window_rect;
759 X11DRV_window_to_X_rect( data, &rect );
761 if (!(cx = rect.right - rect.left)) cx = 1;
762 if (!(cy = rect.bottom - rect.top)) cy = 1;
764 mask = get_window_attributes( display, data, &attr );
766 /* set the attributes that don't change over the lifetime of the window */
767 attr.bit_gravity = NorthWestGravity;
768 attr.backing_store = NotUseful;
769 attr.event_mask = (ExposureMask | PointerMotionMask |
770 ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
771 KeyPressMask | KeyReleaseMask | StructureNotifyMask |
772 FocusChangeMask | KeymapStateMask);
773 mask |= CWBitGravity | CWBackingStore | CWEventMask;
777 data->whole_rect = rect;
778 data->whole_window = XCreateWindow( display, root_window,
779 rect.left - virtual_screen_rect.left,
780 rect.top - virtual_screen_rect.top,
781 cx, cy, 0, screen_depth, InputOutput,
782 visual, mask, &attr );
784 if (!data->whole_window)
789 XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
791 /* non-maximized child must be at bottom of Z order */
792 if ((style & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
794 XWindowChanges changes;
795 changes.stack_mode = Below;
796 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
800 xim = x11drv_thread_data()->xim;
801 if (xim) data->xic = X11DRV_CreateIC( xim, display, data->whole_window );
803 X11DRV_set_wm_hints( display, data );
805 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
806 return data->whole_window;
810 /**********************************************************************
811 * destroy_whole_window
813 * Destroy the whole X window for a given window.
815 static void destroy_whole_window( Display *display, struct x11drv_win_data *data )
817 struct x11drv_thread_data *thread_data = x11drv_thread_data();
819 if (!data->whole_window) return;
821 TRACE( "win %p xwin %lx\n", data->hwnd, data->whole_window );
822 if (thread_data->cursor_window == data->whole_window) thread_data->cursor_window = None;
824 XDeleteContext( display, data->whole_window, winContext );
825 if (data->whole_window != DefaultRootWindow(display))
826 XDestroyWindow( display, data->whole_window );
827 data->whole_window = 0;
830 XUnsetICFocus( data->xic );
831 XDestroyIC( data->xic );
833 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
836 RemovePropA( data->hwnd, whole_window_prop );
840 /*****************************************************************
841 * SetWindowText (X11DRV.@)
843 void X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
845 Display *display = thread_display();
852 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(display))
854 /* allocate new buffer for window text */
855 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
856 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count )))
858 ERR("Not enough memory for window text\n");
861 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
863 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
864 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
866 ERR("Not enough memory for window text in UTF-8\n");
867 HeapFree( GetProcessHeap(), 0, buffer );
870 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
873 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
875 XSetWMName( display, win, &prop );
876 XSetWMIconName( display, win, &prop );
880 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
881 according to the standard
882 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
884 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
885 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
888 HeapFree( GetProcessHeap(), 0, utf8_buffer );
889 HeapFree( GetProcessHeap(), 0, buffer );
894 /***********************************************************************
895 * DestroyWindow (X11DRV.@)
897 void X11DRV_DestroyWindow( HWND hwnd )
899 struct x11drv_thread_data *thread_data = x11drv_thread_data();
900 Display *display = thread_data->display;
901 struct x11drv_win_data *data;
903 if (!(data = X11DRV_get_win_data( hwnd ))) return;
905 free_window_dce( data );
906 destroy_whole_window( display, data );
907 destroy_icon_window( display, data );
909 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
910 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
911 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
913 XDeleteContext( display, (XID)hwnd, win_data_context );
915 HeapFree( GetProcessHeap(), 0, data );
919 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
921 struct x11drv_win_data *data;
923 if ((data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data))))
926 data->whole_window = 0;
927 data->icon_window = 0;
929 data->managed = FALSE;
931 data->lock_changes = 0;
932 data->hWMIconBitmap = 0;
933 data->hWMIconMask = 0;
936 if (!winContext) winContext = XUniqueContext();
937 if (!win_data_context) win_data_context = XUniqueContext();
938 XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
945 /* fill in the desktop X window id in the x11drv_win_data structure */
946 static void get_desktop_xwin( Display *display, struct x11drv_win_data *data )
948 Window win = (Window)GetPropA( data->hwnd, whole_window_prop );
952 unsigned int width, height;
954 /* retrieve the real size of the desktop */
955 SERVER_START_REQ( get_window_rectangles )
957 req->handle = data->hwnd;
958 wine_server_call( req );
959 width = reply->window.right - reply->window.left;
960 height = reply->window.bottom - reply->window.top;
963 data->whole_window = win;
964 if (win != root_window) X11DRV_init_desktop( win, width, height );
971 visualid = XVisualIDFromVisual(visual);
973 SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
974 SetPropA( data->hwnd, visual_id_prop, (HANDLE)visualid );
975 data->whole_window = root_window;
976 X11DRV_set_window_pos( data->hwnd, 0, &virtual_screen_rect, &virtual_screen_rect,
977 SWP_NOZORDER, NULL );
978 if (root_window != DefaultRootWindow( display ))
980 data->managed = TRUE;
981 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
986 /**********************************************************************
987 * CreateDesktopWindow (X11DRV.@)
989 BOOL X11DRV_CreateDesktopWindow( HWND hwnd )
991 Display *display = thread_display();
992 struct x11drv_win_data *data;
994 if (!(data = alloc_win_data( display, hwnd ))) return FALSE;
996 get_desktop_xwin( display, data );
1002 /**********************************************************************
1003 * CreateWindow (X11DRV.@)
1005 BOOL X11DRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
1007 Display *display = thread_display();
1009 struct x11drv_win_data *data;
1013 CBT_CREATEWNDA cbtc;
1017 if (!(data = alloc_win_data( display, hwnd ))) return FALSE;
1021 ERR( "invalid window width %d\n", cs->cx );
1026 ERR( "invalid window height %d\n", cs->cy );
1031 ERR( "invalid window width %d\n", cs->cx );
1036 ERR( "invalid window height %d\n", cs->cy );
1040 /* initialize the dimensions before sending WM_GETMINMAXINFO */
1041 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
1042 X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER, NULL );
1044 /* create an X window if it's a top level window */
1045 if (GetAncestor( hwnd, GA_PARENT ) == GetDesktopWindow())
1047 if (!create_whole_window( display, data, cs->style )) goto failed;
1049 else if (hwnd == GetDesktopWindow())
1051 get_desktop_xwin( display, data );
1054 /* get class or window DC if needed */
1055 alloc_window_dce( data );
1057 /* Call the WH_CBT hook */
1059 /* the window style passed to the hook must be the real window style,
1060 * rather than just the window style that the caller to CreateWindowEx
1061 * passed in, so we have to copy the original CREATESTRUCT and get the
1062 * the real style. */
1064 cbcs.style = GetWindowLongW(hwnd, GWL_STYLE);
1067 cbtc.hwndInsertAfter = HWND_TOP;
1068 if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode ))
1070 TRACE("CBT-hook returned !0\n");
1074 /* Send the WM_GETMINMAXINFO message and fix the size if needed */
1075 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
1077 POINT maxSize, maxPos, minTrack, maxTrack;
1079 WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
1080 if (maxSize.x < cs->cx) cs->cx = maxSize.x;
1081 if (maxSize.y < cs->cy) cs->cy = maxSize.y;
1082 if (cs->cx < 0) cs->cx = 0;
1083 if (cs->cy < 0) cs->cy = 0;
1085 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
1086 if (!X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER, NULL )) return FALSE;
1089 /* send WM_NCCREATE */
1090 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cs->cx, cs->cy );
1092 ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1094 ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1097 WARN("aborted by WM_xxCREATE!\n");
1101 /* make sure the window is still valid */
1102 if (!(data = X11DRV_get_win_data( hwnd ))) return FALSE;
1103 if (data->whole_window) X11DRV_sync_window_style( display, data );
1105 /* send WM_NCCALCSIZE */
1106 rect = data->window_rect;
1107 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&rect );
1109 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
1111 /* yes, even if the CBT hook was called with HWND_TOP */
1112 insert_after = ((wndPtr->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
1114 X11DRV_set_window_pos( hwnd, insert_after, &wndPtr->rectWindow, &rect, 0, NULL );
1116 TRACE( "win %p window %d,%d,%d,%d client %d,%d,%d,%d whole %d,%d,%d,%d X client %d,%d,%d,%d xwin %x\n",
1117 hwnd, wndPtr->rectWindow.left, wndPtr->rectWindow.top,
1118 wndPtr->rectWindow.right, wndPtr->rectWindow.bottom,
1119 wndPtr->rectClient.left, wndPtr->rectClient.top,
1120 wndPtr->rectClient.right, wndPtr->rectClient.bottom,
1121 data->whole_rect.left, data->whole_rect.top,
1122 data->whole_rect.right, data->whole_rect.bottom,
1123 data->client_rect.left, data->client_rect.top,
1124 data->client_rect.right, data->client_rect.bottom,
1125 (unsigned int)data->whole_window );
1127 WIN_ReleasePtr( wndPtr );
1130 ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1132 ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1134 if (!ret) return FALSE;
1136 NotifyWinEvent(EVENT_OBJECT_CREATE, hwnd, OBJID_WINDOW, 0);
1138 /* Send the size messages */
1140 if (!(wndPtr = WIN_GetPtr(hwnd)) || wndPtr == WND_OTHER_PROCESS) return FALSE;
1141 if (!(wndPtr->flags & WIN_NEED_SIZE))
1143 RECT rect = wndPtr->rectClient;
1144 WIN_ReleasePtr( wndPtr );
1145 /* send it anyway */
1146 if (((rect.right-rect.left) <0) ||((rect.bottom-rect.top)<0))
1147 WARN("sending bogus WM_SIZE message 0x%08x\n",
1148 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1149 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1150 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1151 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( rect.left, rect.top ) );
1153 else WIN_ReleasePtr( wndPtr );
1155 /* Show the window, maximizing or minimizing if needed */
1157 style = GetWindowLongW( hwnd, GWL_STYLE );
1158 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1160 extern UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect ); /*FIXME*/
1163 UINT swFlag = (style & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1164 WIN_SetStyle( hwnd, 0, WS_MAXIMIZE | WS_MINIMIZE );
1165 WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1167 swFlag = SWP_FRAMECHANGED | SWP_NOZORDER; /* Frame always gets changed */
1168 if (!(style & WS_VISIBLE) || (style & WS_CHILD) || GetActiveWindow()) swFlag |= SWP_NOACTIVATE;
1170 SetWindowPos( hwnd, 0, newPos.left, newPos.top,
1171 newPos.right, newPos.bottom, swFlag );
1174 /* Dock system tray windows. */
1175 /* Dock after the window is created so we don't have problems calling
1177 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_TRAYWINDOW)
1178 systray_dock_window( display, data );
1183 X11DRV_DestroyWindow( hwnd );
1188 /***********************************************************************
1189 * X11DRV_get_win_data
1191 * Return the X11 data structure associated with a window.
1193 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1197 if (!hwnd || XFindContext( thread_display(), (XID)hwnd, win_data_context, &data )) data = NULL;
1198 return (struct x11drv_win_data *)data;
1202 /***********************************************************************
1203 * X11DRV_get_whole_window
1205 * Return the X window associated with the full area of a window
1207 Window X11DRV_get_whole_window( HWND hwnd )
1209 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1211 if (!data) return (Window)GetPropA( hwnd, whole_window_prop );
1212 return data->whole_window;
1216 /***********************************************************************
1219 * Return the X input context associated with a window
1221 XIC X11DRV_get_ic( HWND hwnd )
1223 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1225 if (!data) return 0;
1230 /*****************************************************************
1231 * SetParent (X11DRV.@)
1233 HWND X11DRV_SetParent( HWND hwnd, HWND parent )
1235 Display *display = thread_display();
1238 HWND old_parent = 0;
1240 /* Windows hides the window first, then shows it again
1241 * including the WM_SHOWWINDOW messages and all */
1242 BOOL was_visible = ShowWindow( hwnd, SW_HIDE );
1244 wndPtr = WIN_GetPtr( hwnd );
1245 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return 0;
1247 SERVER_START_REQ( set_parent )
1250 req->parent = parent;
1251 if ((ret = !wine_server_call( req )))
1253 old_parent = reply->old_parent;
1254 wndPtr->parent = parent = reply->full_parent;
1259 WIN_ReleasePtr( wndPtr );
1262 if (parent != old_parent)
1264 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1266 if (!data) return 0;
1268 if (parent != GetDesktopWindow()) /* a child window */
1270 if (old_parent == GetDesktopWindow())
1272 /* destroy the old X windows */
1273 destroy_whole_window( display, data );
1274 destroy_icon_window( display, data );
1277 data->managed = FALSE;
1278 RemovePropA( data->hwnd, managed_prop );
1282 else /* new top level window */
1284 /* FIXME: we ignore errors since we can't really recover anyway */
1285 create_whole_window( display, data, GetWindowLongW( hwnd, GWL_STYLE ) );
1289 /* SetParent additionally needs to make hwnd the topmost window
1290 in the x-order and send the expected WM_WINDOWPOSCHANGING and
1291 WM_WINDOWPOSCHANGED notification messages.
1293 SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0,
1294 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | (was_visible ? SWP_SHOWWINDOW : 0) );
1295 /* FIXME: a WM_MOVE is also generated (in the DefWindowProc handler
1296 * for WM_WINDOWPOSCHANGED) in Windows, should probably remove SWP_NOMOVE */
1302 /*****************************************************************
1303 * SetFocus (X11DRV.@)
1306 * Explicit colormap management seems to work only with OLVWM.
1308 void X11DRV_SetFocus( HWND hwnd )
1310 Display *display = thread_display();
1311 struct x11drv_win_data *data;
1312 XWindowAttributes win_attr;
1314 /* Only mess with the X focus if there's */
1315 /* no desktop window and if the window is not managed by the WM. */
1316 if (root_window != DefaultRootWindow(display)) return;
1318 if (!hwnd) /* If setting the focus to 0, uninstall the colormap */
1321 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1322 XUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1323 wine_tsx11_unlock();
1327 hwnd = GetAncestor( hwnd, GA_ROOT );
1329 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1330 if (data->managed || !data->whole_window) return;
1332 /* Set X focus and install colormap */
1334 if (XGetWindowAttributes( display, data->whole_window, &win_attr ) &&
1335 (win_attr.map_state == IsViewable))
1337 /* If window is not viewable, don't change anything */
1339 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1340 /* FIXME: this is not entirely correct */
1341 XSetInputFocus( display, data->whole_window, RevertToParent,
1343 GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1344 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1345 XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1347 wine_tsx11_unlock();
1351 /**********************************************************************
1352 * SetWindowIcon (X11DRV.@)
1354 * hIcon or hIconSm has changed (or is being initialised for the
1355 * first time). Complete the X11 driver-specific initialisation
1356 * and set the window hints.
1358 * This is not entirely correct, may need to create
1359 * an icon window and set the pixmap as a background
1361 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
1363 Display *display = thread_display();
1364 struct x11drv_win_data *data;
1367 if (type != ICON_BIG) return; /* nothing to do here */
1369 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1370 if (!data->whole_window) return;
1371 if (!data->managed) return;
1374 if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
1375 wine_tsx11_unlock();
1378 set_icon_hints( display, data, wm_hints, icon );
1380 XSetWMHints( display, data->whole_window, wm_hints );
1382 wine_tsx11_unlock();