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 >= (int)screen_width || rect->top >= (int)screen_height) return FALSE;
114 if (rect->right < 0 || rect->bottom < 0) return FALSE;
120 /***********************************************************************
121 * get_window_attributes
123 * Fill the window attributes structure for an X window.
125 static int get_window_attributes( Display *display, struct x11drv_win_data *data,
126 XSetWindowAttributes *attr )
128 if (!data->managed &&
129 root_window == DefaultRootWindow( display ) &&
130 data->whole_window != root_window &&
131 is_window_managed( data->hwnd ))
133 data->managed = TRUE;
134 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
136 attr->override_redirect = !data->managed;
137 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
138 attr->save_under = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
139 attr->cursor = x11drv_thread_data()->cursor;
140 return (CWOverrideRedirect | CWSaveUnder | CWColormap | CWCursor);
144 /***********************************************************************
145 * X11DRV_sync_window_style
147 * Change the X window attributes when the window style has changed.
149 void X11DRV_sync_window_style( Display *display, struct x11drv_win_data *data )
151 XSetWindowAttributes attr;
152 int mask = get_window_attributes( display, data, &attr );
155 if (data->whole_window != DefaultRootWindow(display))
156 XChangeWindowAttributes( display, data->whole_window, mask, &attr );
161 /***********************************************************************
164 * fill the window changes structure
166 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
170 if (old->right - old->left != new->right - new->left )
172 if (!(changes->width = new->right - new->left)) changes->width = 1;
175 if (old->bottom - old->top != new->bottom - new->top)
177 if (!(changes->height = new->bottom - new->top)) changes->height = 1;
180 if (old->left != new->left)
182 changes->x = new->left;
185 if (old->top != new->top)
187 changes->y = new->top;
194 /***********************************************************************
197 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
199 XSetWindowAttributes attr;
201 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
202 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
203 attr.bit_gravity = NorthWestGravity;
204 attr.backing_store = NotUseful/*WhenMapped*/;
205 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
208 data->icon_window = XCreateWindow( display, root_window, 0, 0,
209 GetSystemMetrics( SM_CXICON ),
210 GetSystemMetrics( SM_CYICON ),
213 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
214 XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
217 TRACE( "created %lx\n", data->icon_window );
218 SetPropA( data->hwnd, icon_window_prop, (HANDLE)data->icon_window );
219 return data->icon_window;
224 /***********************************************************************
225 * destroy_icon_window
227 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
229 if (!data->icon_window) return;
230 if (x11drv_thread_data()->cursor_window == data->icon_window)
231 x11drv_thread_data()->cursor_window = None;
233 XDeleteContext( display, data->icon_window, winContext );
234 XDestroyWindow( display, data->icon_window );
235 data->icon_window = 0;
237 RemovePropA( data->hwnd, icon_window_prop );
241 /***********************************************************************
244 * Set the icon wm hints
246 static void set_icon_hints( Display *display, struct x11drv_win_data *data,
247 XWMHints *hints, HICON hIcon )
249 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
250 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
251 data->hWMIconBitmap = 0;
252 data->hWMIconMask = 0;
256 destroy_icon_window( display, data );
257 hints->flags &= ~(IconPixmapHint | IconMaskHint | IconWindowHint);
261 if (!data->icon_window) create_icon_window( display, data );
262 hints->icon_window = data->icon_window;
263 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
273 GetIconInfo(hIcon, &ii);
275 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
278 rcMask.right = bmMask.bmWidth;
279 rcMask.bottom = bmMask.bmHeight;
281 hDC = CreateCompatibleDC(0);
282 hbmOrig = SelectObject(hDC, ii.hbmMask);
283 InvertRect(hDC, &rcMask);
284 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
285 SelectObject(hDC, hbmOrig);
288 data->hWMIconBitmap = ii.hbmColor;
289 data->hWMIconMask = ii.hbmMask;
291 hints->icon_pixmap = X11DRV_get_pixmap(data->hWMIconBitmap);
292 hints->icon_mask = X11DRV_get_pixmap(data->hWMIconMask);
293 destroy_icon_window( display, data );
294 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
298 /***********************************************************************
299 * systray_dock_window
301 * Docks the given X window with the NETWM system tray.
303 static void systray_dock_window( Display *display, struct x11drv_win_data *data )
305 static Atom systray_atom;
306 Window systray_window;
311 if (DefaultScreen( display ) == 0)
312 systray_atom = x11drv_atom(_NET_SYSTEM_TRAY_S0);
315 char systray_buffer[29]; /* strlen(_NET_SYSTEM_TRAY_S4294967295)+1 */
316 sprintf( systray_buffer, "_NET_SYSTEM_TRAY_S%u", DefaultScreen( display ) );
317 systray_atom = XInternAtom( display, systray_buffer, False );
320 systray_window = XGetSelectionOwner( display, systray_atom );
323 TRACE("Docking tray icon %p\n", data->hwnd);
325 if (systray_window != None)
328 unsigned long info[2];
330 /* Put the window offscreen so it isn't mapped. The window _cannot_ be
331 * mapped if we intend to dock with an XEMBED tray. If the window is
332 * mapped when we dock, it may become visible as a child of the root
333 * window after it docks, which isn't the proper behavior.
335 * For more information on this problem, see
336 * http://standards.freedesktop.org/xembed-spec/latest/ar01s04.html */
338 SetWindowPos( data->hwnd, NULL, screen_width + 1, screen_height + 1,
339 0, 0, SWP_NOZORDER | SWP_NOSIZE );
341 /* set XEMBED protocol data on the window */
342 info[0] = 0; /* protocol version */
343 info[1] = 1; /* mapped = true */
346 XChangeProperty( display, data->whole_window,
347 x11drv_atom(_XEMBED_INFO),
348 x11drv_atom(_XEMBED_INFO), 32, PropModeReplace,
349 (unsigned char*)info, 2 );
352 /* send the docking request message */
353 ZeroMemory( &ev, sizeof(ev) );
354 ev.xclient.type = ClientMessage;
355 ev.xclient.window = systray_window;
356 ev.xclient.message_type = x11drv_atom( _NET_SYSTEM_TRAY_OPCODE );
357 ev.xclient.format = 32;
358 ev.xclient.data.l[0] = CurrentTime;
359 ev.xclient.data.l[1] = SYSTEM_TRAY_REQUEST_DOCK;
360 ev.xclient.data.l[2] = data->whole_window;
361 ev.xclient.data.l[3] = 0;
362 ev.xclient.data.l[4] = 0;
365 XSendEvent( display, systray_window, False, NoEventMask, &ev );
373 /* fall back to he KDE hints if the WM doesn't support XEMBED'ed
377 XChangeProperty( display, data->whole_window,
378 x11drv_atom(KWM_DOCKWINDOW),
379 x11drv_atom(KWM_DOCKWINDOW), 32, PropModeReplace,
380 (unsigned char*)&val, 1 );
381 XChangeProperty( display, data->whole_window,
382 x11drv_atom(_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR),
383 XA_WINDOW, 32, PropModeReplace,
384 (unsigned char*)&data->whole_window, 1 );
390 /***********************************************************************
393 * set the window size hints
395 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
397 XSizeHints* size_hints;
399 if ((size_hints = XAllocSizeHints()))
401 size_hints->flags = 0;
403 if (data->hwnd != GetDesktopWindow()) /* don't force position of desktop */
405 size_hints->win_gravity = StaticGravity;
406 size_hints->x = data->whole_rect.left;
407 size_hints->y = data->whole_rect.top;
408 size_hints->flags |= PWinGravity | PPosition;
411 if ( !(style & WS_THICKFRAME) )
413 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
414 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
415 size_hints->min_width = size_hints->max_width;
416 size_hints->min_height = size_hints->max_height;
417 size_hints->flags |= PMinSize | PMaxSize;
419 XSetWMNormalHints( display, data->whole_window, size_hints );
425 /***********************************************************************
428 * get the name of the current process for setting class hints
430 static char *get_process_name(void)
436 WCHAR module[MAX_PATH];
437 DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
438 if (len && len < MAX_PATH)
441 WCHAR *p, *appname = module;
443 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
444 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
445 len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
446 if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
448 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
457 /***********************************************************************
458 * X11DRV_set_wm_hints
460 * Set the window manager hints for a newly-created window
462 void X11DRV_set_wm_hints( Display *display, struct x11drv_win_data *data )
465 XClassHint *class_hints;
471 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
472 DWORD ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
473 HWND owner = GetWindow( data->hwnd, GW_OWNER );
474 char *process_name = get_process_name();
476 if (data->hwnd == GetDesktopWindow())
478 if (data->whole_window == DefaultRootWindow(display)) return;
479 /* force some styles for the desktop to get the correct decorations */
480 style |= WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
484 /* transient for hint */
487 Window owner_win = X11DRV_get_whole_window( owner );
489 XSetTransientForHint( display, data->whole_window, owner_win );
491 group_leader = owner_win;
493 else group_leader = data->whole_window;
499 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
500 protocols[i++] = x11drv_atom(_NET_WM_PING);
501 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
502 XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
503 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
506 if ((class_hints = XAllocClassHint()))
508 static char wine[] = "Wine";
510 class_hints->res_name = process_name;
511 class_hints->res_class = wine;
512 XSetClassHint( display, data->whole_window, class_hints );
513 XFree( class_hints );
517 set_size_hints( display, data, style );
519 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
520 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
521 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
523 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
524 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
526 /* map WS_EX_TOOLWINDOW to _NET_WM_WINDOW_TYPE_UTILITY */
527 if (ex_style & WS_EX_TOOLWINDOW)
529 Atom a = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
530 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
531 XA_ATOM, 32, PropModeReplace, (unsigned char*)&a, 1);
534 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
535 mwm_hints.functions = 0;
536 if ((style & WS_CAPTION) == WS_CAPTION) mwm_hints.functions |= MWM_FUNC_MOVE;
537 if (style & WS_THICKFRAME) mwm_hints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
538 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
539 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
540 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
541 if (ex_style & WS_EX_APPWINDOW) mwm_hints.functions |= MWM_FUNC_MOVE;
542 mwm_hints.decorations = 0;
543 if ((style & WS_CAPTION) == WS_CAPTION)
545 mwm_hints.decorations |= MWM_DECOR_TITLE;
546 if (style & WS_SYSMENU) mwm_hints.decorations |= MWM_DECOR_MENU;
547 if (style & WS_MINIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MINIMIZE;
548 if (style & WS_MAXIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MAXIMIZE;
550 if (ex_style & WS_EX_DLGMODALFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
551 else if (style & WS_THICKFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
552 else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
553 else if (style & WS_BORDER) mwm_hints.decorations |= MWM_DECOR_BORDER;
554 else if (!(style & (WS_CHILD|WS_POPUP))) mwm_hints.decorations |= MWM_DECOR_BORDER;
556 XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
557 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
558 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
560 XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
561 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
563 wm_hints = XAllocWMHints();
569 wm_hints->flags = InputHint | StateHint | WindowGroupHint;
570 wm_hints->input = !(style & WS_DISABLED);
572 set_icon_hints( display, data, wm_hints,
573 (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON ) );
575 wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
576 wm_hints->window_group = group_leader;
579 XSetWMHints( display, data->whole_window, wm_hints );
586 /***********************************************************************
587 * X11DRV_set_iconic_state
589 * Set the X11 iconic state according to the window style.
591 void X11DRV_set_iconic_state( HWND hwnd )
593 Display *display = thread_display();
594 struct x11drv_win_data *data;
597 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
598 BOOL iconic = (style & WS_MINIMIZE) != 0;
600 if (!(data = X11DRV_get_win_data( hwnd ))) return;
601 if (!data->whole_window || data->whole_window == DefaultRootWindow(display)) return;
603 GetWindowRect( hwnd, &rect );
607 if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
608 wm_hints->flags |= StateHint | IconPositionHint;
609 wm_hints->initial_state = iconic ? IconicState : NormalState;
610 wm_hints->icon_x = rect.left;
611 wm_hints->icon_y = rect.top;
612 XSetWMHints( display, data->whole_window, wm_hints );
614 if (style & WS_VISIBLE)
617 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
619 if (X11DRV_is_window_rect_mapped( &rect ))
620 XMapWindow( display, data->whole_window );
628 /***********************************************************************
629 * X11DRV_window_to_X_rect
631 * Convert a rect from client to X window coordinates
633 void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
637 if (!data->managed) return;
638 if (IsRectEmpty( rect )) return;
640 rc.top = rc.bottom = rc.left = rc.right = 0;
642 AdjustWindowRectEx( &rc, GetWindowLongW( data->hwnd, GWL_STYLE ) & ~(WS_HSCROLL|WS_VSCROLL),
643 FALSE, GetWindowLongW( data->hwnd, GWL_EXSTYLE ) );
645 rect->left -= rc.left;
646 rect->right -= rc.right;
648 rect->bottom -= rc.bottom;
649 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
650 if (rect->left >= rect->right) rect->right = rect->left + 1;
654 /***********************************************************************
655 * X11DRV_X_to_window_rect
657 * Opposite of X11DRV_window_to_X_rect
659 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
661 if (!data->managed) return;
662 if (IsRectEmpty( rect )) return;
664 AdjustWindowRectEx( rect, GetWindowLongW( data->hwnd, GWL_STYLE ) & ~(WS_HSCROLL|WS_VSCROLL),
665 FALSE, GetWindowLongW( data->hwnd, GWL_EXSTYLE ));
667 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
668 if (rect->left >= rect->right) rect->right = rect->left + 1;
672 /***********************************************************************
673 * X11DRV_sync_window_position
675 * Synchronize the X window position with the Windows one
677 void X11DRV_sync_window_position( Display *display, struct x11drv_win_data *data,
678 UINT swp_flags, const RECT *new_client_rect,
679 const RECT *new_whole_rect )
681 XWindowChanges changes;
685 old_whole_rect = data->whole_rect;
686 data->whole_rect = *new_whole_rect;
688 data->client_rect = *new_client_rect;
689 OffsetRect( &data->client_rect, -data->whole_rect.left, -data->whole_rect.top );
691 if (!data->whole_window || data->lock_changes) return;
693 mask = get_window_changes( &changes, &old_whole_rect, &data->whole_rect );
695 if (!(swp_flags & SWP_NOZORDER))
697 /* find window that this one must be after */
698 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
699 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
700 prev = GetWindow( prev, GW_HWNDPREV );
701 if (!prev) /* top child */
703 changes.stack_mode = Above;
708 /* should use stack_mode Below but most window managers don't get it right */
709 /* so move it above the next one in Z order */
710 HWND next = GetWindow( data->hwnd, GW_HWNDNEXT );
711 while (next && !(GetWindowLongW( next, GWL_STYLE ) & WS_VISIBLE))
712 next = GetWindow( next, GW_HWNDNEXT );
715 changes.stack_mode = Above;
716 changes.sibling = X11DRV_get_whole_window(next);
717 mask |= CWStackMode | CWSibling;
724 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
726 TRACE( "setting win %lx pos %ld,%ld,%ldx%ld after %lx changes=%x\n",
727 data->whole_window, data->whole_rect.left, data->whole_rect.top,
728 data->whole_rect.right - data->whole_rect.left,
729 data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
732 if (mask & (CWWidth|CWHeight)) set_size_hints( display, data, style );
733 XReconfigureWMWindow( display, data->whole_window,
734 DefaultScreen(display), mask, &changes );
740 /**********************************************************************
741 * create_whole_window
743 * Create the whole X window for a given window
745 static Window create_whole_window( Display *display, struct x11drv_win_data *data, DWORD style )
748 XSetWindowAttributes attr;
752 rect = data->window_rect;
753 X11DRV_window_to_X_rect( data, &rect );
755 if (!(cx = rect.right - rect.left)) cx = 1;
756 if (!(cy = rect.bottom - rect.top)) cy = 1;
758 mask = get_window_attributes( display, data, &attr );
760 /* set the attributes that don't change over the lifetime of the window */
761 attr.bit_gravity = NorthWestGravity;
762 attr.backing_store = NotUseful;
763 attr.event_mask = (ExposureMask | PointerMotionMask |
764 ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
765 KeyPressMask | KeyReleaseMask | StructureNotifyMask |
766 FocusChangeMask | KeymapStateMask);
767 mask |= CWBitGravity | CWBackingStore | CWEventMask;
771 data->whole_rect = rect;
772 data->whole_window = XCreateWindow( display, root_window, rect.left, rect.top, cx, cy,
773 0, screen_depth, InputOutput, visual,
776 if (!data->whole_window)
781 XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
783 /* non-maximized child must be at bottom of Z order */
784 if ((style & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
786 XWindowChanges changes;
787 changes.stack_mode = Below;
788 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
792 xim = x11drv_thread_data()->xim;
793 if (xim) data->xic = X11DRV_CreateIC( xim, display, data->whole_window );
795 X11DRV_set_wm_hints( display, data );
797 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
798 return data->whole_window;
802 /**********************************************************************
803 * destroy_whole_window
805 * Destroy the whole X window for a given window.
807 static void destroy_whole_window( Display *display, struct x11drv_win_data *data )
809 struct x11drv_thread_data *thread_data = x11drv_thread_data();
811 if (!data->whole_window) return;
813 TRACE( "win %p xwin %lx\n", data->hwnd, data->whole_window );
814 if (thread_data->cursor_window == data->whole_window) thread_data->cursor_window = None;
816 XDeleteContext( display, data->whole_window, winContext );
817 if (data->whole_window != DefaultRootWindow(display))
818 XDestroyWindow( display, data->whole_window );
819 data->whole_window = 0;
822 XUnsetICFocus( data->xic );
823 XDestroyIC( data->xic );
825 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
828 RemovePropA( data->hwnd, whole_window_prop );
832 /*****************************************************************
833 * SetWindowText (X11DRV.@)
835 void X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
837 Display *display = thread_display();
844 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(display))
846 /* allocate new buffer for window text */
847 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
848 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count )))
850 ERR("Not enough memory for window text\n");
853 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
855 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
856 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
858 ERR("Not enough memory for window text in UTF-8\n");
859 HeapFree( GetProcessHeap(), 0, buffer );
862 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
865 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
867 XSetWMName( display, win, &prop );
868 XSetWMIconName( display, win, &prop );
872 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
873 according to the standard
874 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
876 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
877 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
880 HeapFree( GetProcessHeap(), 0, utf8_buffer );
881 HeapFree( GetProcessHeap(), 0, buffer );
886 /***********************************************************************
887 * DestroyWindow (X11DRV.@)
889 void X11DRV_DestroyWindow( HWND hwnd )
891 struct x11drv_thread_data *thread_data = x11drv_thread_data();
892 Display *display = thread_data->display;
893 struct x11drv_win_data *data;
895 if (!(data = X11DRV_get_win_data( hwnd ))) return;
897 free_window_dce( data );
898 destroy_whole_window( display, data );
899 destroy_icon_window( display, data );
901 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
902 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
903 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
905 XDeleteContext( display, (XID)hwnd, win_data_context );
907 HeapFree( GetProcessHeap(), 0, data );
911 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
913 struct x11drv_win_data *data;
915 if ((data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data))))
918 data->whole_window = 0;
919 data->icon_window = 0;
921 data->managed = FALSE;
923 data->lock_changes = 0;
924 data->hWMIconBitmap = 0;
925 data->hWMIconMask = 0;
928 if (!winContext) winContext = XUniqueContext();
929 if (!win_data_context) win_data_context = XUniqueContext();
930 XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
937 /* fill in the desktop X window id in the x11drv_win_data structure */
938 static void get_desktop_xwin( Display *display, struct x11drv_win_data *data )
941 Window win = (Window)GetPropA( data->hwnd, whole_window_prop );
945 unsigned int width, height;
947 /* retrieve the real size of the desktop */
948 SERVER_START_REQ( get_window_rectangles )
950 req->handle = data->hwnd;
951 wine_server_call( req );
952 width = reply->window.right - reply->window.left;
953 height = reply->window.bottom - reply->window.top;
956 data->whole_window = win;
957 if (win != root_window) X11DRV_init_desktop( win, width, height );
964 visualid = XVisualIDFromVisual(visual);
966 SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
967 SetPropA( data->hwnd, visual_id_prop, (HANDLE)visualid );
968 data->whole_window = root_window;
969 SetRect( &rect, 0, 0, screen_width, screen_height );
970 X11DRV_set_window_pos( data->hwnd, 0, &rect, &rect, SWP_NOZORDER, NULL );
971 if (root_window != DefaultRootWindow( display ))
973 data->managed = TRUE;
974 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
979 /**********************************************************************
980 * CreateDesktopWindow (X11DRV.@)
982 BOOL X11DRV_CreateDesktopWindow( HWND hwnd )
984 Display *display = thread_display();
985 struct x11drv_win_data *data;
987 if (!(data = alloc_win_data( display, hwnd ))) return FALSE;
989 get_desktop_xwin( display, data );
995 /**********************************************************************
996 * CreateWindow (X11DRV.@)
998 BOOL X11DRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
1000 Display *display = thread_display();
1002 struct x11drv_win_data *data;
1006 CBT_CREATEWNDA cbtc;
1010 if (!(data = alloc_win_data( display, hwnd ))) return FALSE;
1014 ERR( "invalid window width %d\n", cs->cx );
1019 ERR( "invalid window height %d\n", cs->cy );
1024 ERR( "invalid window width %d\n", cs->cx );
1029 ERR( "invalid window height %d\n", cs->cy );
1033 /* initialize the dimensions before sending WM_GETMINMAXINFO */
1034 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
1035 X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER, NULL );
1037 /* create an X window if it's a top level window */
1038 if (GetAncestor( hwnd, GA_PARENT ) == GetDesktopWindow())
1040 if (!create_whole_window( display, data, cs->style )) goto failed;
1042 else if (hwnd == GetDesktopWindow())
1044 get_desktop_xwin( display, data );
1047 /* get class or window DC if needed */
1048 alloc_window_dce( data );
1050 /* Call the WH_CBT hook */
1052 /* the window style passed to the hook must be the real window style,
1053 * rather than just the window style that the caller to CreateWindowEx
1054 * passed in, so we have to copy the original CREATESTRUCT and get the
1055 * the real style. */
1057 cbcs.style = GetWindowLongW(hwnd, GWL_STYLE);
1060 cbtc.hwndInsertAfter = HWND_TOP;
1061 if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode ))
1063 TRACE("CBT-hook returned !0\n");
1067 /* Send the WM_GETMINMAXINFO message and fix the size if needed */
1068 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
1070 POINT maxSize, maxPos, minTrack, maxTrack;
1072 WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
1073 if (maxSize.x < cs->cx) cs->cx = maxSize.x;
1074 if (maxSize.y < cs->cy) cs->cy = maxSize.y;
1075 if (cs->cx < 0) cs->cx = 0;
1076 if (cs->cy < 0) cs->cy = 0;
1078 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
1079 if (!X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER, NULL )) return FALSE;
1082 /* send WM_NCCREATE */
1083 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cs->cx, cs->cy );
1085 ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1087 ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1090 WARN("aborted by WM_xxCREATE!\n");
1094 /* make sure the window is still valid */
1095 if (!(data = X11DRV_get_win_data( hwnd ))) return FALSE;
1096 if (data->whole_window) X11DRV_sync_window_style( display, data );
1098 /* send WM_NCCALCSIZE */
1099 rect = data->window_rect;
1100 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&rect );
1102 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
1104 /* yes, even if the CBT hook was called with HWND_TOP */
1105 insert_after = ((wndPtr->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
1107 X11DRV_set_window_pos( hwnd, insert_after, &wndPtr->rectWindow, &rect, 0, NULL );
1109 TRACE( "win %p window %ld,%ld,%ld,%ld client %ld,%ld,%ld,%ld whole %ld,%ld,%ld,%ld X client %ld,%ld,%ld,%ld xwin %x\n",
1110 hwnd, wndPtr->rectWindow.left, wndPtr->rectWindow.top,
1111 wndPtr->rectWindow.right, wndPtr->rectWindow.bottom,
1112 wndPtr->rectClient.left, wndPtr->rectClient.top,
1113 wndPtr->rectClient.right, wndPtr->rectClient.bottom,
1114 data->whole_rect.left, data->whole_rect.top,
1115 data->whole_rect.right, data->whole_rect.bottom,
1116 data->client_rect.left, data->client_rect.top,
1117 data->client_rect.right, data->client_rect.bottom,
1118 (unsigned int)data->whole_window );
1120 WIN_ReleasePtr( wndPtr );
1123 ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1125 ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1127 if (!ret) return FALSE;
1129 NotifyWinEvent(EVENT_OBJECT_CREATE, hwnd, OBJID_WINDOW, 0);
1131 /* Send the size messages */
1133 if (!(wndPtr = WIN_GetPtr(hwnd)) || wndPtr == WND_OTHER_PROCESS) return FALSE;
1134 if (!(wndPtr->flags & WIN_NEED_SIZE))
1136 RECT rect = wndPtr->rectClient;
1137 WIN_ReleasePtr( wndPtr );
1138 /* send it anyway */
1139 if (((rect.right-rect.left) <0) ||((rect.bottom-rect.top)<0))
1140 WARN("sending bogus WM_SIZE message 0x%08lx\n",
1141 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1142 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1143 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1144 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( rect.left, rect.top ) );
1146 else WIN_ReleasePtr( wndPtr );
1148 /* Show the window, maximizing or minimizing if needed */
1150 style = GetWindowLongW( hwnd, GWL_STYLE );
1151 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1153 extern UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect ); /*FIXME*/
1156 UINT swFlag = (style & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1157 WIN_SetStyle( hwnd, 0, WS_MAXIMIZE | WS_MINIMIZE );
1158 WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1160 swFlag = SWP_FRAMECHANGED | SWP_NOZORDER; /* Frame always gets changed */
1161 if (!(style & WS_VISIBLE) || (style & WS_CHILD) || GetActiveWindow()) swFlag |= SWP_NOACTIVATE;
1163 SetWindowPos( hwnd, 0, newPos.left, newPos.top,
1164 newPos.right, newPos.bottom, swFlag );
1167 /* Dock system tray windows. */
1168 /* Dock after the window is created so we don't have problems calling
1170 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_TRAYWINDOW)
1171 systray_dock_window( display, data );
1176 X11DRV_DestroyWindow( hwnd );
1181 /***********************************************************************
1182 * X11DRV_get_win_data
1184 * Return the X11 data structure associated with a window.
1186 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1190 if (!hwnd || XFindContext( thread_display(), (XID)hwnd, win_data_context, &data )) data = NULL;
1191 return (struct x11drv_win_data *)data;
1195 /***********************************************************************
1196 * X11DRV_get_whole_window
1198 * Return the X window associated with the full area of a window
1200 Window X11DRV_get_whole_window( HWND hwnd )
1202 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1204 if (!data) return (Window)GetPropA( hwnd, whole_window_prop );
1205 return data->whole_window;
1209 /***********************************************************************
1212 * Return the X input context associated with a window
1214 XIC X11DRV_get_ic( HWND hwnd )
1216 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1218 if (!data) return 0;
1223 /*****************************************************************
1224 * SetParent (X11DRV.@)
1226 HWND X11DRV_SetParent( HWND hwnd, HWND parent )
1228 Display *display = thread_display();
1231 HWND old_parent = 0;
1233 /* Windows hides the window first, then shows it again
1234 * including the WM_SHOWWINDOW messages and all */
1235 BOOL was_visible = ShowWindow( hwnd, SW_HIDE );
1237 wndPtr = WIN_GetPtr( hwnd );
1238 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return 0;
1240 SERVER_START_REQ( set_parent )
1243 req->parent = parent;
1244 if ((ret = !wine_server_call( req )))
1246 old_parent = reply->old_parent;
1247 wndPtr->parent = parent = reply->full_parent;
1252 WIN_ReleasePtr( wndPtr );
1255 if (parent != old_parent)
1257 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1259 if (!data) return 0;
1261 if (parent != GetDesktopWindow()) /* a child window */
1263 if (old_parent == GetDesktopWindow())
1265 /* destroy the old X windows */
1266 destroy_whole_window( display, data );
1267 destroy_icon_window( display, data );
1270 data->managed = FALSE;
1271 RemovePropA( data->hwnd, managed_prop );
1275 else /* new top level window */
1277 /* FIXME: we ignore errors since we can't really recover anyway */
1278 create_whole_window( display, data, GetWindowLongW( hwnd, GWL_STYLE ) );
1282 /* SetParent additionally needs to make hwnd the topmost window
1283 in the x-order and send the expected WM_WINDOWPOSCHANGING and
1284 WM_WINDOWPOSCHANGED notification messages.
1286 SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0,
1287 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | (was_visible ? SWP_SHOWWINDOW : 0) );
1288 /* FIXME: a WM_MOVE is also generated (in the DefWindowProc handler
1289 * for WM_WINDOWPOSCHANGED) in Windows, should probably remove SWP_NOMOVE */
1295 /*****************************************************************
1296 * SetFocus (X11DRV.@)
1299 * Explicit colormap management seems to work only with OLVWM.
1301 void X11DRV_SetFocus( HWND hwnd )
1303 Display *display = thread_display();
1304 struct x11drv_win_data *data;
1305 XWindowAttributes win_attr;
1307 /* Only mess with the X focus if there's */
1308 /* no desktop window and if the window is not managed by the WM. */
1309 if (root_window != DefaultRootWindow(display)) return;
1311 if (!hwnd) /* If setting the focus to 0, uninstall the colormap */
1314 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1315 XUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1316 wine_tsx11_unlock();
1320 hwnd = GetAncestor( hwnd, GA_ROOT );
1322 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1323 if (data->managed || !data->whole_window) return;
1325 /* Set X focus and install colormap */
1327 if (XGetWindowAttributes( display, data->whole_window, &win_attr ) &&
1328 (win_attr.map_state == IsViewable))
1330 /* If window is not viewable, don't change anything */
1332 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1333 /* FIXME: this is not entirely correct */
1334 XSetInputFocus( display, data->whole_window, RevertToParent,
1336 GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1337 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1338 XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1340 wine_tsx11_unlock();
1344 /**********************************************************************
1345 * SetWindowIcon (X11DRV.@)
1347 * hIcon or hIconSm has changed (or is being initialised for the
1348 * first time). Complete the X11 driver-specific initialisation
1349 * and set the window hints.
1351 * This is not entirely correct, may need to create
1352 * an icon window and set the pixmap as a background
1354 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
1356 Display *display = thread_display();
1357 struct x11drv_win_data *data;
1360 if (type != ICON_BIG) return; /* nothing to do here */
1362 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1363 if (!data->whole_window) return;
1364 if (!data->managed) return;
1367 if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
1368 wine_tsx11_unlock();
1371 set_icon_hints( display, data, wm_hints, icon );
1373 XSetWMHints( display, data->whole_window, wm_hints );
1375 wine_tsx11_unlock();