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>
40 #include "wine/unicode.h"
43 #include "wine/debug.h"
44 #include "wine/server.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
50 /* X context to associate a hwnd to an X window */
51 XContext winContext = 0;
53 /* X context to associate a struct x11drv_win_data to an hwnd */
54 static XContext win_data_context;
56 static const char whole_window_prop[] = "__wine_x11_whole_window";
57 static const char icon_window_prop[] = "__wine_x11_icon_window";
58 static const char managed_prop[] = "__wine_x11_managed";
59 static const char visual_id_prop[] = "__wine_x11_visual_id";
61 /* for XDG systray icons */
62 #define SYSTEM_TRAY_REQUEST_DOCK 0
64 /***********************************************************************
67 * Check if a given window should be managed
69 static inline BOOL is_window_managed( HWND hwnd )
71 DWORD style, ex_style;
73 if (!managed_mode) return FALSE;
74 /* tray window is always managed */
75 ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
76 if (ex_style & WS_EX_TRAYWINDOW) return TRUE;
77 /* child windows are not managed */
78 style = GetWindowLongW( hwnd, GWL_STYLE );
79 if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD) return FALSE;
80 /* windows with caption are managed */
81 if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
82 /* tool windows are not managed */
83 if (ex_style & WS_EX_TOOLWINDOW) return FALSE;
84 /* windows with thick frame are managed */
85 if (style & WS_THICKFRAME) return TRUE;
86 /* application windows are managed */
87 if (ex_style & WS_EX_APPWINDOW) return TRUE;
92 /* popup with sysmenu == caption are managed */
93 if (style & WS_SYSMENU) return TRUE;
94 /* full-screen popup windows are managed */
95 GetWindowRect( hwnd, &rect );
96 if ((rect.right - rect.left) == screen_width && (rect.bottom - rect.top) == screen_height)
99 /* default: not managed */
104 /***********************************************************************
105 * X11DRV_is_window_rect_mapped
107 * Check if the X whole window should be mapped based on its rectangle
109 BOOL X11DRV_is_window_rect_mapped( const RECT *rect )
111 /* don't map if rect is empty */
112 if (IsRectEmpty( rect )) return FALSE;
114 /* don't map if rect is off-screen */
115 if (rect->left >= virtual_screen_rect.right ||
116 rect->top >= virtual_screen_rect.bottom ||
117 rect->right <= virtual_screen_rect.left ||
118 rect->bottom <= virtual_screen_rect.top)
125 /***********************************************************************
126 * get_window_attributes
128 * Fill the window attributes structure for an X window.
130 static int get_window_attributes( Display *display, struct x11drv_win_data *data,
131 XSetWindowAttributes *attr )
133 if (!data->managed &&
134 root_window == DefaultRootWindow( display ) &&
135 data->whole_window != root_window &&
136 is_window_managed( data->hwnd ))
138 data->managed = TRUE;
139 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
141 attr->override_redirect = !data->managed;
142 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
143 attr->save_under = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
144 attr->cursor = x11drv_thread_data()->cursor;
145 return (CWOverrideRedirect | CWSaveUnder | CWColormap | CWCursor);
149 /***********************************************************************
150 * X11DRV_sync_window_style
152 * Change the X window attributes when the window style has changed.
154 void X11DRV_sync_window_style( Display *display, struct x11drv_win_data *data )
156 XSetWindowAttributes attr;
157 int mask = get_window_attributes( display, data, &attr );
160 if (data->whole_window != DefaultRootWindow(display))
161 XChangeWindowAttributes( display, data->whole_window, mask, &attr );
166 /***********************************************************************
169 * fill the window changes structure
171 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
175 if (old->right - old->left != new->right - new->left )
177 if (!(changes->width = new->right - new->left)) changes->width = 1;
180 if (old->bottom - old->top != new->bottom - new->top)
182 if (!(changes->height = new->bottom - new->top)) changes->height = 1;
185 if (old->left != new->left)
187 changes->x = new->left;
190 if (old->top != new->top)
192 changes->y = new->top;
199 /***********************************************************************
202 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
204 XSetWindowAttributes attr;
206 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
207 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
208 attr.bit_gravity = NorthWestGravity;
209 attr.backing_store = NotUseful/*WhenMapped*/;
210 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
213 data->icon_window = XCreateWindow( display, root_window, 0, 0,
214 GetSystemMetrics( SM_CXICON ),
215 GetSystemMetrics( SM_CYICON ),
218 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
219 XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
222 TRACE( "created %lx\n", data->icon_window );
223 SetPropA( data->hwnd, icon_window_prop, (HANDLE)data->icon_window );
224 return data->icon_window;
229 /***********************************************************************
230 * destroy_icon_window
232 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
234 if (!data->icon_window) return;
235 if (x11drv_thread_data()->cursor_window == data->icon_window)
236 x11drv_thread_data()->cursor_window = None;
238 XDeleteContext( display, data->icon_window, winContext );
239 XDestroyWindow( display, data->icon_window );
240 data->icon_window = 0;
242 RemovePropA( data->hwnd, icon_window_prop );
246 /***********************************************************************
249 * Set the icon wm hints
251 static void set_icon_hints( Display *display, struct x11drv_win_data *data,
252 XWMHints *hints, HICON hIcon )
254 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
255 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
256 data->hWMIconBitmap = 0;
257 data->hWMIconMask = 0;
261 destroy_icon_window( display, data );
262 hints->flags &= ~(IconPixmapHint | IconMaskHint | IconWindowHint);
266 if (!data->icon_window) create_icon_window( display, data );
267 hints->icon_window = data->icon_window;
268 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
278 GetIconInfo(hIcon, &ii);
280 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
283 rcMask.right = bmMask.bmWidth;
284 rcMask.bottom = bmMask.bmHeight;
286 hDC = CreateCompatibleDC(0);
287 hbmOrig = SelectObject(hDC, ii.hbmMask);
288 InvertRect(hDC, &rcMask);
289 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
290 SelectObject(hDC, hbmOrig);
293 data->hWMIconBitmap = ii.hbmColor;
294 data->hWMIconMask = ii.hbmMask;
296 hints->icon_pixmap = X11DRV_get_pixmap(data->hWMIconBitmap);
297 hints->icon_mask = X11DRV_get_pixmap(data->hWMIconMask);
298 destroy_icon_window( display, data );
299 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
303 /***********************************************************************
304 * systray_dock_window
306 * Docks the given X window with the NETWM system tray.
308 static void systray_dock_window( Display *display, struct x11drv_win_data *data )
310 static Atom systray_atom;
311 Window systray_window;
316 if (DefaultScreen( display ) == 0)
317 systray_atom = x11drv_atom(_NET_SYSTEM_TRAY_S0);
320 char systray_buffer[29]; /* strlen(_NET_SYSTEM_TRAY_S4294967295)+1 */
321 sprintf( systray_buffer, "_NET_SYSTEM_TRAY_S%u", DefaultScreen( display ) );
322 systray_atom = XInternAtom( display, systray_buffer, False );
325 systray_window = XGetSelectionOwner( display, systray_atom );
328 TRACE("Docking tray icon %p\n", data->hwnd);
330 if (systray_window != None)
333 unsigned long info[2];
335 /* Put the window offscreen so it isn't mapped. The window _cannot_ be
336 * mapped if we intend to dock with an XEMBED tray. If the window is
337 * mapped when we dock, it may become visible as a child of the root
338 * window after it docks, which isn't the proper behavior.
340 * For more information on this problem, see
341 * http://standards.freedesktop.org/xembed-spec/latest/ar01s04.html */
343 SetWindowPos( data->hwnd, NULL, virtual_screen_rect.right + 1, virtual_screen_rect.bottom + 1,
344 0, 0, SWP_NOZORDER | SWP_NOSIZE );
346 /* set XEMBED protocol data on the window */
347 info[0] = 0; /* protocol version */
348 info[1] = 1; /* mapped = true */
351 XChangeProperty( display, data->whole_window,
352 x11drv_atom(_XEMBED_INFO),
353 x11drv_atom(_XEMBED_INFO), 32, PropModeReplace,
354 (unsigned char*)info, 2 );
357 /* send the docking request message */
358 ZeroMemory( &ev, sizeof(ev) );
359 ev.xclient.type = ClientMessage;
360 ev.xclient.window = systray_window;
361 ev.xclient.message_type = x11drv_atom( _NET_SYSTEM_TRAY_OPCODE );
362 ev.xclient.format = 32;
363 ev.xclient.data.l[0] = CurrentTime;
364 ev.xclient.data.l[1] = SYSTEM_TRAY_REQUEST_DOCK;
365 ev.xclient.data.l[2] = data->whole_window;
366 ev.xclient.data.l[3] = 0;
367 ev.xclient.data.l[4] = 0;
370 XSendEvent( display, systray_window, False, NoEventMask, &ev );
378 /* fall back to he KDE hints if the WM doesn't support XEMBED'ed
382 XChangeProperty( display, data->whole_window,
383 x11drv_atom(KWM_DOCKWINDOW),
384 x11drv_atom(KWM_DOCKWINDOW), 32, PropModeReplace,
385 (unsigned char*)&val, 1 );
386 XChangeProperty( display, data->whole_window,
387 x11drv_atom(_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR),
388 XA_WINDOW, 32, PropModeReplace,
389 (unsigned char*)&data->whole_window, 1 );
395 /***********************************************************************
398 * set the window size hints
400 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
402 XSizeHints* size_hints;
404 if ((size_hints = XAllocSizeHints()))
406 size_hints->flags = 0;
408 if (data->hwnd != GetDesktopWindow()) /* don't force position of desktop */
410 size_hints->win_gravity = StaticGravity;
411 size_hints->x = data->whole_rect.left;
412 size_hints->y = data->whole_rect.top;
413 size_hints->flags |= PWinGravity | PPosition;
416 if ( !(style & WS_THICKFRAME) )
418 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
419 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
420 size_hints->min_width = size_hints->max_width;
421 size_hints->min_height = size_hints->max_height;
422 size_hints->flags |= PMinSize | PMaxSize;
424 XSetWMNormalHints( display, data->whole_window, size_hints );
430 /***********************************************************************
433 * get the name of the current process for setting class hints
435 static char *get_process_name(void)
441 WCHAR module[MAX_PATH];
442 DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
443 if (len && len < MAX_PATH)
446 WCHAR *p, *appname = module;
448 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
449 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
450 len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
451 if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
453 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
462 /***********************************************************************
463 * X11DRV_set_wm_hints
465 * Set the window manager hints for a newly-created window
467 void X11DRV_set_wm_hints( Display *display, struct x11drv_win_data *data )
470 XClassHint *class_hints;
477 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
478 DWORD ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
479 HWND owner = GetWindow( data->hwnd, GW_OWNER );
480 char *process_name = get_process_name();
482 if (data->hwnd == GetDesktopWindow())
484 if (data->whole_window == DefaultRootWindow(display)) return;
485 /* force some styles for the desktop to get the correct decorations */
486 style |= WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
490 /* transient for hint */
493 Window owner_win = X11DRV_get_whole_window( owner );
495 XSetTransientForHint( display, data->whole_window, owner_win );
497 group_leader = owner_win;
499 else group_leader = data->whole_window;
505 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
506 protocols[i++] = x11drv_atom(_NET_WM_PING);
507 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
508 XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
509 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
512 if ((class_hints = XAllocClassHint()))
514 static char wine[] = "Wine";
516 class_hints->res_name = process_name;
517 class_hints->res_class = wine;
518 XSetClassHint( display, data->whole_window, class_hints );
519 XFree( class_hints );
523 set_size_hints( display, data, style );
525 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
526 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
527 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
529 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
530 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
532 /* set the WM_WINDOW_TYPE */
533 window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
534 if (ex_style & WS_EX_TOOLWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
535 else if (style & WS_THICKFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
536 else if (style & WS_DLGFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
537 else if (ex_style & WS_EX_DLGMODALFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
539 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
540 XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
542 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
543 mwm_hints.functions = MWM_FUNC_MOVE;
544 if (style & WS_THICKFRAME) mwm_hints.functions |= MWM_FUNC_RESIZE;
545 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
546 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
547 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
548 mwm_hints.decorations = 0;
549 if ((style & WS_CAPTION) == WS_CAPTION)
551 mwm_hints.decorations |= MWM_DECOR_TITLE;
552 if (style & WS_SYSMENU) mwm_hints.decorations |= MWM_DECOR_MENU;
553 if (style & WS_MINIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MINIMIZE;
554 if (style & WS_MAXIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MAXIMIZE;
556 if (ex_style & WS_EX_DLGMODALFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
557 else if (style & WS_THICKFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
558 else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
559 else if (style & WS_BORDER) mwm_hints.decorations |= MWM_DECOR_BORDER;
560 else if (!(style & (WS_CHILD|WS_POPUP))) mwm_hints.decorations |= MWM_DECOR_BORDER;
562 XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
563 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
564 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
566 XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
567 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
569 wm_hints = XAllocWMHints();
575 wm_hints->flags = InputHint | StateHint | WindowGroupHint;
576 wm_hints->input = !(style & WS_DISABLED);
578 set_icon_hints( display, data, wm_hints,
579 (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON ) );
581 wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
582 wm_hints->window_group = group_leader;
585 XSetWMHints( display, data->whole_window, wm_hints );
592 /***********************************************************************
593 * X11DRV_set_iconic_state
595 * Set the X11 iconic state according to the window style.
597 void X11DRV_set_iconic_state( HWND hwnd )
599 Display *display = thread_display();
600 struct x11drv_win_data *data;
603 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
604 BOOL iconic = (style & WS_MINIMIZE) != 0;
606 if (!(data = X11DRV_get_win_data( hwnd ))) return;
607 if (!data->whole_window || data->whole_window == DefaultRootWindow(display)) return;
609 GetWindowRect( hwnd, &rect );
613 if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
614 wm_hints->flags |= StateHint | IconPositionHint;
615 wm_hints->initial_state = iconic ? IconicState : NormalState;
616 wm_hints->icon_x = rect.left - virtual_screen_rect.left;
617 wm_hints->icon_y = rect.top - virtual_screen_rect.top;
618 XSetWMHints( display, data->whole_window, wm_hints );
620 if (style & WS_VISIBLE)
623 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
625 if (X11DRV_is_window_rect_mapped( &rect ))
626 XMapWindow( display, data->whole_window );
634 /***********************************************************************
635 * X11DRV_window_to_X_rect
637 * Convert a rect from client to X window coordinates
639 void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
643 if (!data->managed) return;
644 if (IsRectEmpty( rect )) return;
646 rc.top = rc.bottom = rc.left = rc.right = 0;
648 AdjustWindowRectEx( &rc, GetWindowLongW( data->hwnd, GWL_STYLE ) & ~(WS_HSCROLL|WS_VSCROLL),
649 FALSE, GetWindowLongW( data->hwnd, GWL_EXSTYLE ) );
651 rect->left -= rc.left;
652 rect->right -= rc.right;
654 rect->bottom -= rc.bottom;
655 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
656 if (rect->left >= rect->right) rect->right = rect->left + 1;
660 /***********************************************************************
661 * X11DRV_X_to_window_rect
663 * Opposite of X11DRV_window_to_X_rect
665 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
667 if (!data->managed) return;
668 if (IsRectEmpty( rect )) return;
670 AdjustWindowRectEx( rect, GetWindowLongW( data->hwnd, GWL_STYLE ) & ~(WS_HSCROLL|WS_VSCROLL),
671 FALSE, GetWindowLongW( data->hwnd, GWL_EXSTYLE ));
673 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
674 if (rect->left >= rect->right) rect->right = rect->left + 1;
678 /***********************************************************************
679 * X11DRV_sync_window_position
681 * Synchronize the X window position with the Windows one
683 void X11DRV_sync_window_position( Display *display, struct x11drv_win_data *data,
684 UINT swp_flags, const RECT *new_client_rect,
685 const RECT *new_whole_rect )
687 XWindowChanges changes;
691 old_whole_rect = data->whole_rect;
692 data->whole_rect = *new_whole_rect;
694 data->client_rect = *new_client_rect;
695 OffsetRect( &data->client_rect, -data->whole_rect.left, -data->whole_rect.top );
697 if (!data->whole_window || data->lock_changes) return;
699 mask = get_window_changes( &changes, &old_whole_rect, &data->whole_rect );
701 if (!(swp_flags & SWP_NOZORDER))
703 /* find window that this one must be after */
704 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
705 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
706 prev = GetWindow( prev, GW_HWNDPREV );
707 if (!prev) /* top child */
709 changes.stack_mode = Above;
714 /* should use stack_mode Below but most window managers don't get it right */
715 /* so move it above the next one in Z order */
716 HWND next = GetWindow( data->hwnd, GW_HWNDNEXT );
717 while (next && !(GetWindowLongW( next, GWL_STYLE ) & WS_VISIBLE))
718 next = GetWindow( next, GW_HWNDNEXT );
721 changes.stack_mode = Above;
722 changes.sibling = X11DRV_get_whole_window(next);
723 mask |= CWStackMode | CWSibling;
730 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
732 TRACE( "setting win %lx pos %d,%d,%dx%d after %lx changes=%x\n",
733 data->whole_window, data->whole_rect.left, data->whole_rect.top,
734 data->whole_rect.right - data->whole_rect.left,
735 data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
738 if (mask & (CWWidth|CWHeight)) set_size_hints( display, data, style );
739 if (mask & CWX) changes.x -= virtual_screen_rect.left;
740 if (mask & CWY) changes.y -= virtual_screen_rect.top;
741 XReconfigureWMWindow( display, data->whole_window,
742 DefaultScreen(display), mask, &changes );
748 /**********************************************************************
749 * create_whole_window
751 * Create the whole X window for a given window
753 static Window create_whole_window( Display *display, struct x11drv_win_data *data, DWORD style )
756 XSetWindowAttributes attr;
760 rect = data->window_rect;
761 X11DRV_window_to_X_rect( data, &rect );
763 if (!(cx = rect.right - rect.left)) cx = 1;
764 if (!(cy = rect.bottom - rect.top)) cy = 1;
766 mask = get_window_attributes( display, data, &attr );
768 /* set the attributes that don't change over the lifetime of the window */
769 attr.bit_gravity = NorthWestGravity;
770 attr.backing_store = NotUseful;
771 attr.event_mask = (ExposureMask | PointerMotionMask |
772 ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
773 KeyPressMask | KeyReleaseMask | StructureNotifyMask |
774 FocusChangeMask | KeymapStateMask);
775 mask |= CWBitGravity | CWBackingStore | CWEventMask;
779 data->whole_rect = rect;
780 data->whole_window = XCreateWindow( display, root_window,
781 rect.left - virtual_screen_rect.left,
782 rect.top - virtual_screen_rect.top,
783 cx, cy, 0, screen_depth, InputOutput,
784 visual, mask, &attr );
786 if (!data->whole_window)
791 XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
793 /* non-maximized child must be at bottom of Z order */
794 if ((style & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
796 XWindowChanges changes;
797 changes.stack_mode = Below;
798 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
802 xim = x11drv_thread_data()->xim;
803 if (xim) data->xic = X11DRV_CreateIC( xim, display, data->whole_window );
805 X11DRV_set_wm_hints( display, data );
807 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
808 return data->whole_window;
812 /**********************************************************************
813 * destroy_whole_window
815 * Destroy the whole X window for a given window.
817 static void destroy_whole_window( Display *display, struct x11drv_win_data *data )
819 struct x11drv_thread_data *thread_data = x11drv_thread_data();
821 if (!data->whole_window) return;
823 TRACE( "win %p xwin %lx\n", data->hwnd, data->whole_window );
824 if (thread_data->cursor_window == data->whole_window) thread_data->cursor_window = None;
826 XDeleteContext( display, data->whole_window, winContext );
827 if (data->whole_window != DefaultRootWindow(display))
828 XDestroyWindow( display, data->whole_window );
829 data->whole_window = 0;
832 XUnsetICFocus( data->xic );
833 XDestroyIC( data->xic );
835 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
838 RemovePropA( data->hwnd, whole_window_prop );
842 /*****************************************************************
843 * SetWindowText (X11DRV.@)
845 void X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
847 Display *display = thread_display();
854 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(display))
856 /* allocate new buffer for window text */
857 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
858 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count )))
860 ERR("Not enough memory for window text\n");
863 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
865 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
866 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
868 ERR("Not enough memory for window text in UTF-8\n");
869 HeapFree( GetProcessHeap(), 0, buffer );
872 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
875 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
877 XSetWMName( display, win, &prop );
878 XSetWMIconName( display, win, &prop );
882 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
883 according to the standard
884 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
886 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
887 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
890 HeapFree( GetProcessHeap(), 0, utf8_buffer );
891 HeapFree( GetProcessHeap(), 0, buffer );
896 /***********************************************************************
897 * DestroyWindow (X11DRV.@)
899 void X11DRV_DestroyWindow( HWND hwnd )
901 struct x11drv_thread_data *thread_data = x11drv_thread_data();
902 Display *display = thread_data->display;
903 struct x11drv_win_data *data;
905 if (!(data = X11DRV_get_win_data( hwnd ))) return;
907 free_window_dce( data );
908 destroy_whole_window( display, data );
909 destroy_icon_window( display, data );
911 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
912 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
913 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
915 XDeleteContext( display, (XID)hwnd, win_data_context );
917 HeapFree( GetProcessHeap(), 0, data );
921 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
923 struct x11drv_win_data *data;
925 if ((data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data))))
928 data->whole_window = 0;
929 data->icon_window = 0;
931 data->managed = FALSE;
933 data->lock_changes = 0;
934 data->hWMIconBitmap = 0;
935 data->hWMIconMask = 0;
938 if (!winContext) winContext = XUniqueContext();
939 if (!win_data_context) win_data_context = XUniqueContext();
940 XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
947 /* fill in the desktop X window id in the x11drv_win_data structure */
948 static void get_desktop_xwin( Display *display, struct x11drv_win_data *data )
950 Window win = (Window)GetPropA( data->hwnd, whole_window_prop );
954 unsigned int width, height;
956 /* retrieve the real size of the desktop */
957 SERVER_START_REQ( get_window_rectangles )
959 req->handle = data->hwnd;
960 wine_server_call( req );
961 width = reply->window.right - reply->window.left;
962 height = reply->window.bottom - reply->window.top;
965 data->whole_window = win;
966 if (win != root_window) X11DRV_init_desktop( win, width, height );
973 visualid = XVisualIDFromVisual(visual);
975 SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
976 SetPropA( data->hwnd, visual_id_prop, (HANDLE)visualid );
977 data->whole_window = root_window;
978 X11DRV_SetWindowPos( data->hwnd, 0, &virtual_screen_rect, &virtual_screen_rect,
979 SWP_NOZORDER, NULL );
980 if (root_window != DefaultRootWindow( display ))
982 data->managed = TRUE;
983 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
988 /**********************************************************************
989 * CreateDesktopWindow (X11DRV.@)
991 BOOL X11DRV_CreateDesktopWindow( HWND hwnd )
993 Display *display = thread_display();
994 struct x11drv_win_data *data;
996 if (!(data = alloc_win_data( display, hwnd ))) return FALSE;
998 get_desktop_xwin( display, data );
1004 /**********************************************************************
1005 * CreateWindow (X11DRV.@)
1007 BOOL X11DRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
1009 Display *display = thread_display();
1011 struct x11drv_win_data *data;
1015 CBT_CREATEWNDA cbtc;
1019 if (!(data = alloc_win_data( display, hwnd ))) return FALSE;
1023 ERR( "invalid window width %d\n", cs->cx );
1028 ERR( "invalid window height %d\n", cs->cy );
1033 ERR( "invalid window width %d\n", cs->cx );
1038 ERR( "invalid window height %d\n", cs->cy );
1042 /* initialize the dimensions before sending WM_GETMINMAXINFO */
1043 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
1044 X11DRV_SetWindowPos( hwnd, 0, &rect, &rect, SWP_NOZORDER, NULL );
1046 /* create an X window if it's a top level window */
1047 if (GetAncestor( hwnd, GA_PARENT ) == GetDesktopWindow())
1049 if (!create_whole_window( display, data, cs->style )) goto failed;
1051 else if (hwnd == GetDesktopWindow())
1053 get_desktop_xwin( display, data );
1056 /* get class or window DC if needed */
1057 alloc_window_dce( data );
1059 /* Call the WH_CBT hook */
1061 /* the window style passed to the hook must be the real window style,
1062 * rather than just the window style that the caller to CreateWindowEx
1063 * passed in, so we have to copy the original CREATESTRUCT and get the
1064 * the real style. */
1066 cbcs.style = GetWindowLongW(hwnd, GWL_STYLE);
1069 cbtc.hwndInsertAfter = HWND_TOP;
1070 if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode ))
1072 TRACE("CBT-hook returned !0\n");
1076 /* Send the WM_GETMINMAXINFO message and fix the size if needed */
1077 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
1079 POINT maxSize, maxPos, minTrack, maxTrack;
1081 WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
1082 if (maxTrack.x < cs->cx) cs->cx = maxTrack.x;
1083 if (maxTrack.y < cs->cy) cs->cy = maxTrack.y;
1084 if (cs->cx < 0) cs->cx = 0;
1085 if (cs->cy < 0) cs->cy = 0;
1087 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
1088 if (!X11DRV_SetWindowPos( hwnd, 0, &rect, &rect, SWP_NOZORDER, NULL )) return FALSE;
1091 /* send WM_NCCREATE */
1092 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cs->cx, cs->cy );
1094 ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1096 ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1099 WARN("aborted by WM_xxCREATE!\n");
1103 /* make sure the window is still valid */
1104 if (!(data = X11DRV_get_win_data( hwnd ))) return FALSE;
1105 if (data->whole_window) X11DRV_sync_window_style( display, data );
1107 /* send WM_NCCALCSIZE */
1108 rect = data->window_rect;
1109 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&rect );
1111 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
1113 /* yes, even if the CBT hook was called with HWND_TOP */
1114 insert_after = (wndPtr->dwStyle & WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
1116 X11DRV_SetWindowPos( hwnd, insert_after, &wndPtr->rectWindow, &rect, 0, NULL );
1118 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",
1119 hwnd, wndPtr->rectWindow.left, wndPtr->rectWindow.top,
1120 wndPtr->rectWindow.right, wndPtr->rectWindow.bottom,
1121 wndPtr->rectClient.left, wndPtr->rectClient.top,
1122 wndPtr->rectClient.right, wndPtr->rectClient.bottom,
1123 data->whole_rect.left, data->whole_rect.top,
1124 data->whole_rect.right, data->whole_rect.bottom,
1125 data->client_rect.left, data->client_rect.top,
1126 data->client_rect.right, data->client_rect.bottom,
1127 (unsigned int)data->whole_window );
1129 WIN_ReleasePtr( wndPtr );
1132 ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1134 ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1136 if (!ret) return FALSE;
1138 NotifyWinEvent(EVENT_OBJECT_CREATE, hwnd, OBJID_WINDOW, 0);
1140 /* Send the size messages */
1142 if (!(wndPtr = WIN_GetPtr(hwnd)) || wndPtr == WND_OTHER_PROCESS) return FALSE;
1143 if (!(wndPtr->flags & WIN_NEED_SIZE))
1145 RECT rect = wndPtr->rectClient;
1146 WIN_ReleasePtr( wndPtr );
1147 /* send it anyway */
1148 if (((rect.right-rect.left) <0) ||((rect.bottom-rect.top)<0))
1149 WARN("sending bogus WM_SIZE message 0x%08x\n",
1150 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1151 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1152 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1153 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( rect.left, rect.top ) );
1155 else WIN_ReleasePtr( wndPtr );
1157 /* Show the window, maximizing or minimizing if needed */
1159 style = GetWindowLongW( hwnd, GWL_STYLE );
1160 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1162 extern UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect ); /*FIXME*/
1165 UINT swFlag = (style & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1166 WIN_SetStyle( hwnd, 0, WS_MAXIMIZE | WS_MINIMIZE );
1167 swFlag = WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1169 swFlag |= SWP_FRAMECHANGED; /* Frame always gets changed */
1170 if (!(style & WS_VISIBLE) || (style & WS_CHILD) || GetActiveWindow()) swFlag |= SWP_NOACTIVATE;
1172 SetWindowPos( hwnd, 0, newPos.left, newPos.top,
1173 newPos.right, newPos.bottom, swFlag );
1176 /* Dock system tray windows. */
1177 /* Dock after the window is created so we don't have problems calling
1179 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_TRAYWINDOW)
1180 systray_dock_window( display, data );
1185 X11DRV_DestroyWindow( hwnd );
1190 /***********************************************************************
1191 * X11DRV_get_win_data
1193 * Return the X11 data structure associated with a window.
1195 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1199 if (!hwnd || XFindContext( thread_display(), (XID)hwnd, win_data_context, &data )) data = NULL;
1200 return (struct x11drv_win_data *)data;
1204 /***********************************************************************
1205 * X11DRV_get_whole_window
1207 * Return the X window associated with the full area of a window
1209 Window X11DRV_get_whole_window( HWND hwnd )
1211 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1213 if (!data) return (Window)GetPropA( hwnd, whole_window_prop );
1214 return data->whole_window;
1218 /***********************************************************************
1221 * Return the X input context associated with a window
1223 XIC X11DRV_get_ic( HWND hwnd )
1225 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1227 if (!data) return 0;
1232 /*****************************************************************
1233 * SetParent (X11DRV.@)
1235 void X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
1237 Display *display = thread_display();
1238 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1241 if (parent == old_parent) return;
1243 if (parent != GetDesktopWindow()) /* a child window */
1245 if (old_parent == GetDesktopWindow())
1247 /* destroy the old X windows */
1248 destroy_whole_window( display, data );
1249 destroy_icon_window( display, data );
1252 data->managed = FALSE;
1253 RemovePropA( data->hwnd, managed_prop );
1257 else /* new top level window */
1259 /* FIXME: we ignore errors since we can't really recover anyway */
1260 create_whole_window( display, data, GetWindowLongW( hwnd, GWL_STYLE ) );
1265 /*****************************************************************
1266 * SetFocus (X11DRV.@)
1269 * Explicit colormap management seems to work only with OLVWM.
1271 void X11DRV_SetFocus( HWND hwnd )
1273 Display *display = thread_display();
1274 struct x11drv_win_data *data;
1275 XWindowAttributes win_attr;
1277 /* Only mess with the X focus if there's */
1278 /* no desktop window and if the window is not managed by the WM. */
1279 if (root_window != DefaultRootWindow(display)) return;
1281 if (!hwnd) /* If setting the focus to 0, uninstall the colormap */
1284 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1285 XUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1286 wine_tsx11_unlock();
1290 hwnd = GetAncestor( hwnd, GA_ROOT );
1292 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1293 if (data->managed || !data->whole_window) return;
1295 /* Set X focus and install colormap */
1297 if (XGetWindowAttributes( display, data->whole_window, &win_attr ) &&
1298 (win_attr.map_state == IsViewable))
1300 /* If window is not viewable, don't change anything */
1302 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1303 /* FIXME: this is not entirely correct */
1304 XSetInputFocus( display, data->whole_window, RevertToParent,
1306 GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1307 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1308 XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1310 wine_tsx11_unlock();
1314 /**********************************************************************
1315 * SetWindowIcon (X11DRV.@)
1317 * hIcon or hIconSm has changed (or is being initialised for the
1318 * first time). Complete the X11 driver-specific initialisation
1319 * and set the window hints.
1321 * This is not entirely correct, may need to create
1322 * an icon window and set the pixmap as a background
1324 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
1326 Display *display = thread_display();
1327 struct x11drv_win_data *data;
1330 if (type != ICON_BIG) return; /* nothing to do here */
1332 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1333 if (!data->whole_window) return;
1334 if (!data->managed) return;
1337 if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
1338 wine_tsx11_unlock();
1341 set_icon_hints( display, data, wm_hints, icon );
1343 XSetWMHints( display, data->whole_window, wm_hints );
1345 wine_tsx11_unlock();