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
32 #include <X11/Xresource.h>
33 #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 /***********************************************************************
64 * Check if a given window should be managed
66 inline static BOOL is_window_managed( HWND hwnd )
68 DWORD style, ex_style;
70 if (!managed_mode) return FALSE;
71 /* tray window is always managed */
72 ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
73 if (ex_style & WS_EX_TRAYWINDOW) return TRUE;
74 /* child windows are not managed */
75 style = GetWindowLongW( hwnd, GWL_STYLE );
76 if (style & WS_CHILD) return FALSE;
77 /* windows with caption are managed */
78 if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
79 /* tool windows are not managed */
80 if (ex_style & WS_EX_TOOLWINDOW) return FALSE;
81 /* windows with thick frame are managed */
82 if (style & WS_THICKFRAME) return TRUE;
83 /* application windows are managed */
84 if (ex_style & WS_EX_APPWINDOW) return TRUE;
85 /* full-screen popup windows are managed */
89 GetWindowRect( hwnd, &rect );
90 if ((rect.right - rect.left) == screen_width && (rect.bottom - rect.top) == screen_height)
93 /* default: not managed */
98 /***********************************************************************
99 * X11DRV_is_window_rect_mapped
101 * Check if the X whole window should be mapped based on its rectangle
103 BOOL X11DRV_is_window_rect_mapped( const RECT *rect )
105 /* don't map if rect is empty */
106 if (IsRectEmpty( rect )) return FALSE;
108 /* don't map if rect is off-screen */
109 if (rect->left >= (int)screen_width || rect->top >= (int)screen_height) return FALSE;
110 if (rect->right < 0 || rect->bottom < 0) return FALSE;
116 /***********************************************************************
117 * get_window_attributes
119 * Fill the window attributes structure for an X window.
121 static int get_window_attributes( Display *display, struct x11drv_win_data *data,
122 XSetWindowAttributes *attr )
124 if (!data->managed &&
125 root_window == DefaultRootWindow( display ) &&
126 data->whole_window != root_window &&
127 is_window_managed( data->hwnd ))
129 data->managed = TRUE;
130 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
132 attr->override_redirect = !data->managed;
133 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
134 attr->save_under = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
135 attr->cursor = x11drv_thread_data()->cursor;
136 return (CWOverrideRedirect | CWSaveUnder | CWColormap | CWCursor);
140 /***********************************************************************
141 * X11DRV_sync_window_style
143 * Change the X window attributes when the window style has changed.
145 void X11DRV_sync_window_style( Display *display, struct x11drv_win_data *data )
147 XSetWindowAttributes attr;
148 int mask = get_window_attributes( display, data, &attr );
151 if (data->whole_window != DefaultRootWindow(display))
152 XChangeWindowAttributes( display, data->whole_window, mask, &attr );
157 /***********************************************************************
160 * fill the window changes structure
162 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
166 if (old->right - old->left != new->right - new->left )
168 if (!(changes->width = new->right - new->left)) changes->width = 1;
171 if (old->bottom - old->top != new->bottom - new->top)
173 if (!(changes->height = new->bottom - new->top)) changes->height = 1;
176 if (old->left != new->left)
178 changes->x = new->left;
181 if (old->top != new->top)
183 changes->y = new->top;
190 /***********************************************************************
193 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
195 XSetWindowAttributes attr;
197 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
198 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
199 attr.bit_gravity = NorthWestGravity;
200 attr.backing_store = NotUseful/*WhenMapped*/;
201 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
204 data->icon_window = XCreateWindow( display, root_window, 0, 0,
205 GetSystemMetrics( SM_CXICON ),
206 GetSystemMetrics( SM_CYICON ),
209 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
210 XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
213 TRACE( "created %lx\n", data->icon_window );
214 SetPropA( data->hwnd, icon_window_prop, (HANDLE)data->icon_window );
215 return data->icon_window;
220 /***********************************************************************
221 * destroy_icon_window
223 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
225 if (!data->icon_window) return;
226 if (x11drv_thread_data()->cursor_window == data->icon_window)
227 x11drv_thread_data()->cursor_window = None;
229 XDeleteContext( display, data->icon_window, winContext );
230 XDestroyWindow( display, data->icon_window );
231 data->icon_window = 0;
233 RemovePropA( data->hwnd, icon_window_prop );
237 /***********************************************************************
240 * Set the icon wm hints
242 static void set_icon_hints( Display *display, struct x11drv_win_data *data,
243 XWMHints *hints, HICON hIcon )
245 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
246 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
247 data->hWMIconBitmap = 0;
248 data->hWMIconMask = 0;
252 destroy_icon_window( display, data );
253 hints->flags &= ~(IconPixmapHint | IconMaskHint | IconWindowHint);
257 if (!data->icon_window) create_icon_window( display, data );
258 hints->icon_window = data->icon_window;
259 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
269 GetIconInfo(hIcon, &ii);
271 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
274 rcMask.right = bmMask.bmWidth;
275 rcMask.bottom = bmMask.bmHeight;
277 hDC = CreateCompatibleDC(0);
278 hbmOrig = SelectObject(hDC, ii.hbmMask);
279 InvertRect(hDC, &rcMask);
280 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
281 SelectObject(hDC, hbmOrig);
284 data->hWMIconBitmap = ii.hbmColor;
285 data->hWMIconMask = ii.hbmMask;
287 hints->icon_pixmap = X11DRV_get_pixmap(data->hWMIconBitmap);
288 hints->icon_mask = X11DRV_get_pixmap(data->hWMIconMask);
289 destroy_icon_window( display, data );
290 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
295 /***********************************************************************
298 * set the window size hints
300 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
302 XSizeHints* size_hints;
304 if ((size_hints = XAllocSizeHints()))
306 size_hints->flags = 0;
308 if (data->hwnd != GetDesktopWindow()) /* don't force position of desktop */
310 size_hints->win_gravity = StaticGravity;
311 size_hints->x = data->whole_rect.left;
312 size_hints->y = data->whole_rect.top;
313 size_hints->flags |= PWinGravity | PPosition;
316 if ( !(style & WS_THICKFRAME) )
318 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
319 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
320 size_hints->min_width = size_hints->max_width;
321 size_hints->min_height = size_hints->max_height;
322 size_hints->flags |= PMinSize | PMaxSize;
324 XSetWMNormalHints( display, data->whole_window, size_hints );
330 /***********************************************************************
333 * get the name of the current process for setting class hints
335 static char *get_process_name(void)
341 WCHAR module[MAX_PATH];
342 DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
343 if (len && len < MAX_PATH)
346 WCHAR *p, *appname = module;
348 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
349 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
350 len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
351 if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
353 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
362 /***********************************************************************
363 * X11DRV_set_wm_hints
365 * Set the window manager hints for a newly-created window
367 void X11DRV_set_wm_hints( Display *display, struct x11drv_win_data *data )
370 XClassHint *class_hints;
376 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
377 DWORD ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
378 HWND owner = GetWindow( data->hwnd, GW_OWNER );
379 char *process_name = get_process_name();
381 if (data->hwnd == GetDesktopWindow())
383 if (data->whole_window == DefaultRootWindow(display)) return;
384 /* force some styles for the desktop to get the correct decorations */
385 style |= WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
389 /* transient for hint */
392 Window owner_win = X11DRV_get_whole_window( owner );
394 XSetTransientForHint( display, data->whole_window, owner_win );
396 group_leader = owner_win;
398 else group_leader = data->whole_window;
404 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
405 protocols[i++] = x11drv_atom(_NET_WM_PING);
406 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
407 XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
408 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
411 if ((class_hints = XAllocClassHint()))
413 static char wine[] = "Wine";
415 class_hints->res_name = process_name;
416 class_hints->res_class = wine;
417 XSetClassHint( display, data->whole_window, class_hints );
418 XFree( class_hints );
422 set_size_hints( display, data, style );
424 /* systray properties (KDE only for now) */
425 if (ex_style & WS_EX_TRAYWINDOW)
428 XChangeProperty( display, data->whole_window, x11drv_atom(KWM_DOCKWINDOW),
429 x11drv_atom(KWM_DOCKWINDOW), 32, PropModeReplace, (unsigned char*)&val, 1 );
430 XChangeProperty( display, data->whole_window, x11drv_atom(_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR),
431 XA_WINDOW, 32, PropModeReplace, (unsigned char*)&data->whole_window, 1 );
434 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
435 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
436 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
438 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
439 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
441 /* map WS_EX_TOOLWINDOW to _NET_WM_WINDOW_TYPE_UTILITY */
442 if (ex_style & WS_EX_TOOLWINDOW)
444 Atom a = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
445 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
446 XA_ATOM, 32, PropModeReplace, (unsigned char*)&a, 1);
449 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
450 mwm_hints.functions = 0;
451 if ((style & WS_CAPTION) == WS_CAPTION) mwm_hints.functions |= MWM_FUNC_MOVE;
452 if (style & WS_THICKFRAME) mwm_hints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
453 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
454 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
455 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
456 if (ex_style & WS_EX_APPWINDOW) mwm_hints.functions |= MWM_FUNC_MOVE;
457 mwm_hints.decorations = 0;
458 if ((style & WS_CAPTION) == WS_CAPTION)
460 mwm_hints.decorations |= MWM_DECOR_TITLE;
461 if (style & WS_SYSMENU) mwm_hints.decorations |= MWM_DECOR_MENU;
462 if (style & WS_MINIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MINIMIZE;
463 if (style & WS_MAXIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MAXIMIZE;
465 if (ex_style & WS_EX_DLGMODALFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
466 else if (style & WS_THICKFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
467 else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
468 else if (style & WS_BORDER) mwm_hints.decorations |= MWM_DECOR_BORDER;
469 else if (!(style & (WS_CHILD|WS_POPUP))) mwm_hints.decorations |= MWM_DECOR_BORDER;
471 XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
472 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
473 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
475 XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
476 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
478 wm_hints = XAllocWMHints();
484 wm_hints->flags = InputHint | StateHint | WindowGroupHint;
485 wm_hints->input = !(style & WS_DISABLED);
487 set_icon_hints( display, data, wm_hints,
488 (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON ) );
490 wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
491 wm_hints->window_group = group_leader;
494 XSetWMHints( display, data->whole_window, wm_hints );
501 /***********************************************************************
502 * X11DRV_set_iconic_state
504 * Set the X11 iconic state according to the window style.
506 void X11DRV_set_iconic_state( HWND hwnd )
508 Display *display = thread_display();
509 struct x11drv_win_data *data;
512 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
513 BOOL iconic = (style & WS_MINIMIZE) != 0;
515 if (!(data = X11DRV_get_win_data( hwnd ))) return;
516 if (!data->whole_window || data->whole_window == DefaultRootWindow(display)) return;
518 GetWindowRect( hwnd, &rect );
522 if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
523 wm_hints->flags |= StateHint | IconPositionHint;
524 wm_hints->initial_state = iconic ? IconicState : NormalState;
525 wm_hints->icon_x = rect.left;
526 wm_hints->icon_y = rect.top;
527 XSetWMHints( display, data->whole_window, wm_hints );
529 if (style & WS_VISIBLE)
532 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
534 if (X11DRV_is_window_rect_mapped( &rect ))
535 XMapWindow( display, data->whole_window );
543 /***********************************************************************
544 * X11DRV_window_to_X_rect
546 * Convert a rect from client to X window coordinates
548 void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
552 if (!data->managed) return;
553 if (IsRectEmpty( rect )) return;
555 rc.top = rc.bottom = rc.left = rc.right = 0;
557 AdjustWindowRectEx( &rc, GetWindowLongW( data->hwnd, GWL_STYLE ) & ~(WS_HSCROLL|WS_VSCROLL),
558 FALSE, GetWindowLongW( data->hwnd, GWL_EXSTYLE ) );
560 rect->left -= rc.left;
561 rect->right -= rc.right;
563 rect->bottom -= rc.bottom;
564 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
565 if (rect->left >= rect->right) rect->right = rect->left + 1;
569 /***********************************************************************
570 * X11DRV_X_to_window_rect
572 * Opposite of X11DRV_window_to_X_rect
574 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
576 if (!data->managed) return;
577 if (IsRectEmpty( rect )) return;
579 AdjustWindowRectEx( rect, GetWindowLongW( data->hwnd, GWL_STYLE ) & ~(WS_HSCROLL|WS_VSCROLL),
580 FALSE, GetWindowLongW( data->hwnd, GWL_EXSTYLE ));
582 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
583 if (rect->left >= rect->right) rect->right = rect->left + 1;
587 /***********************************************************************
588 * X11DRV_sync_window_position
590 * Synchronize the X window position with the Windows one
592 void X11DRV_sync_window_position( Display *display, struct x11drv_win_data *data,
593 UINT swp_flags, const RECT *new_client_rect,
594 const RECT *new_whole_rect )
596 XWindowChanges changes;
600 old_whole_rect = data->whole_rect;
601 data->whole_rect = *new_whole_rect;
603 data->client_rect = *new_client_rect;
604 OffsetRect( &data->client_rect, -data->whole_rect.left, -data->whole_rect.top );
606 if (!data->whole_window || data->lock_changes) return;
608 mask = get_window_changes( &changes, &old_whole_rect, &data->whole_rect );
610 if (!(swp_flags & SWP_NOZORDER))
612 /* find window that this one must be after */
613 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
614 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
615 prev = GetWindow( prev, GW_HWNDPREV );
616 if (!prev) /* top child */
618 changes.stack_mode = Above;
623 /* should use stack_mode Below but most window managers don't get it right */
624 /* so move it above the next one in Z order */
625 HWND next = GetWindow( data->hwnd, GW_HWNDNEXT );
626 while (next && !(GetWindowLongW( next, GWL_STYLE ) & WS_VISIBLE))
627 next = GetWindow( next, GW_HWNDNEXT );
630 changes.stack_mode = Above;
631 changes.sibling = X11DRV_get_whole_window(next);
632 mask |= CWStackMode | CWSibling;
639 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
641 TRACE( "setting win %lx pos %ld,%ld,%ldx%ld after %lx changes=%x\n",
642 data->whole_window, data->whole_rect.left, data->whole_rect.top,
643 data->whole_rect.right - data->whole_rect.left,
644 data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
647 if (mask & (CWWidth|CWHeight)) set_size_hints( display, data, style );
648 XReconfigureWMWindow( display, data->whole_window,
649 DefaultScreen(display), mask, &changes );
655 /**********************************************************************
656 * create_whole_window
658 * Create the whole X window for a given window
660 static Window create_whole_window( Display *display, struct x11drv_win_data *data, DWORD style )
663 XSetWindowAttributes attr;
667 rect = data->window_rect;
668 X11DRV_window_to_X_rect( data, &rect );
670 if (!(cx = rect.right - rect.left)) cx = 1;
671 if (!(cy = rect.bottom - rect.top)) cy = 1;
673 mask = get_window_attributes( display, data, &attr );
675 /* set the attributes that don't change over the lifetime of the window */
676 attr.bit_gravity = NorthWestGravity;
677 attr.backing_store = NotUseful;
678 attr.event_mask = (ExposureMask | PointerMotionMask |
679 ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
680 KeyPressMask | KeyReleaseMask | StructureNotifyMask |
681 FocusChangeMask | KeymapStateMask);
682 mask |= CWBitGravity | CWBackingStore | CWEventMask;
686 data->whole_rect = rect;
687 data->whole_window = XCreateWindow( display, root_window, rect.left, rect.top, cx, cy,
688 0, screen_depth, InputOutput, visual,
691 if (!data->whole_window)
696 XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
698 /* non-maximized child must be at bottom of Z order */
699 if ((style & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
701 XWindowChanges changes;
702 changes.stack_mode = Below;
703 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
707 xim = x11drv_thread_data()->xim;
708 if (xim) data->xic = X11DRV_CreateIC( xim, display, data->whole_window );
710 X11DRV_set_wm_hints( display, data );
712 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
713 return data->whole_window;
717 /**********************************************************************
718 * destroy_whole_window
720 * Destroy the whole X window for a given window.
722 static void destroy_whole_window( Display *display, struct x11drv_win_data *data )
724 struct x11drv_thread_data *thread_data = x11drv_thread_data();
726 if (!data->whole_window) return;
728 TRACE( "win %p xwin %lx\n", data->hwnd, data->whole_window );
729 if (thread_data->cursor_window == data->whole_window) thread_data->cursor_window = None;
731 XDeleteContext( display, data->whole_window, winContext );
732 if (data->whole_window != DefaultRootWindow(display))
733 XDestroyWindow( display, data->whole_window );
734 data->whole_window = 0;
737 XUnsetICFocus( data->xic );
738 XDestroyIC( data->xic );
740 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
743 RemovePropA( data->hwnd, whole_window_prop );
747 /*****************************************************************
748 * SetWindowText (X11DRV.@)
750 void X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
752 Display *display = thread_display();
759 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(display))
761 /* allocate new buffer for window text */
762 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
763 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count )))
765 ERR("Not enough memory for window text\n");
768 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
770 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
771 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
773 ERR("Not enough memory for window text in UTF-8\n");
774 HeapFree( GetProcessHeap(), 0, buffer );
777 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
780 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
782 XSetWMName( display, win, &prop );
783 XSetWMIconName( display, win, &prop );
787 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
788 according to the standard
789 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
791 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
792 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
795 HeapFree( GetProcessHeap(), 0, utf8_buffer );
796 HeapFree( GetProcessHeap(), 0, buffer );
801 /***********************************************************************
802 * DestroyWindow (X11DRV.@)
804 void X11DRV_DestroyWindow( HWND hwnd )
806 struct x11drv_thread_data *thread_data = x11drv_thread_data();
807 Display *display = thread_data->display;
808 struct x11drv_win_data *data;
810 if (!(data = X11DRV_get_win_data( hwnd ))) return;
812 free_window_dce( data );
813 destroy_whole_window( display, data );
814 destroy_icon_window( display, data );
816 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
817 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
818 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
820 XDeleteContext( display, (XID)hwnd, win_data_context );
822 HeapFree( GetProcessHeap(), 0, data );
826 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
828 struct x11drv_win_data *data;
830 if ((data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data))))
833 data->whole_window = 0;
834 data->icon_window = 0;
836 data->managed = FALSE;
838 data->lock_changes = 0;
839 data->hWMIconBitmap = 0;
840 data->hWMIconMask = 0;
843 if (!winContext) winContext = XUniqueContext();
844 if (!win_data_context) win_data_context = XUniqueContext();
845 XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
852 /* fill in the desktop X window id in the x11drv_win_data structure */
853 static void get_desktop_xwin( Display *display, struct x11drv_win_data *data )
856 Window win = (Window)GetPropA( data->hwnd, whole_window_prop );
860 unsigned int width, height;
862 /* retrieve the real size of the desktop */
863 SERVER_START_REQ( get_window_rectangles )
865 req->handle = data->hwnd;
866 wine_server_call( req );
867 width = reply->window.right - reply->window.left;
868 height = reply->window.bottom - reply->window.top;
871 data->whole_window = win;
872 if (win != root_window) X11DRV_init_desktop( win, width, height );
879 visualid = XVisualIDFromVisual(visual);
881 SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
882 SetPropA( data->hwnd, visual_id_prop, (HANDLE)visualid );
883 data->whole_window = root_window;
884 SetRect( &rect, 0, 0, screen_width, screen_height );
885 X11DRV_set_window_pos( data->hwnd, 0, &rect, &rect, SWP_NOZORDER, NULL );
886 if (root_window != DefaultRootWindow( display ))
888 data->managed = TRUE;
889 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
894 /**********************************************************************
895 * CreateDesktopWindow (X11DRV.@)
897 BOOL X11DRV_CreateDesktopWindow( HWND hwnd )
899 Display *display = thread_display();
900 struct x11drv_win_data *data;
902 if (!(data = alloc_win_data( display, hwnd ))) return FALSE;
904 get_desktop_xwin( display, data );
910 /**********************************************************************
911 * CreateWindow (X11DRV.@)
913 BOOL X11DRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
915 Display *display = thread_display();
917 struct x11drv_win_data *data;
925 if (!(data = alloc_win_data( display, hwnd ))) return FALSE;
929 ERR( "invalid window width %d\n", cs->cx );
934 ERR( "invalid window height %d\n", cs->cy );
939 ERR( "invalid window width %d\n", cs->cx );
944 ERR( "invalid window height %d\n", cs->cy );
948 /* initialize the dimensions before sending WM_GETMINMAXINFO */
949 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
950 X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER, NULL );
952 /* create an X window if it's a top level window */
953 if (GetAncestor( hwnd, GA_PARENT ) == GetDesktopWindow())
955 if (!create_whole_window( display, data, cs->style )) goto failed;
957 else if (hwnd == GetDesktopWindow())
959 get_desktop_xwin( display, data );
962 /* get class or window DC if needed */
963 alloc_window_dce( data );
965 /* Call the WH_CBT hook */
967 /* the window style passed to the hook must be the real window style,
968 * rather than just the window style that the caller to CreateWindowEx
969 * passed in, so we have to copy the original CREATESTRUCT and get the
972 cbcs.style = GetWindowLongW(hwnd, GWL_STYLE);
975 cbtc.hwndInsertAfter = HWND_TOP;
976 if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode ))
978 TRACE("CBT-hook returned !0\n");
982 /* Send the WM_GETMINMAXINFO message and fix the size if needed */
983 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
985 POINT maxSize, maxPos, minTrack, maxTrack;
987 WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
988 if (maxSize.x < cs->cx) cs->cx = maxSize.x;
989 if (maxSize.y < cs->cy) cs->cy = maxSize.y;
990 if (cs->cx < 0) cs->cx = 0;
991 if (cs->cy < 0) cs->cy = 0;
993 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
994 if (!X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER, NULL )) return FALSE;
997 /* send WM_NCCREATE */
998 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cs->cx, cs->cy );
1000 ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1002 ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1005 WARN("aborted by WM_xxCREATE!\n");
1009 /* make sure the window is still valid */
1010 if (!(data = X11DRV_get_win_data( hwnd ))) return FALSE;
1011 if (data->whole_window) X11DRV_sync_window_style( display, data );
1013 /* send WM_NCCALCSIZE */
1014 rect = data->window_rect;
1015 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&rect );
1017 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
1019 /* yes, even if the CBT hook was called with HWND_TOP */
1020 insert_after = ((wndPtr->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
1022 X11DRV_set_window_pos( hwnd, insert_after, &wndPtr->rectWindow, &rect, 0, NULL );
1024 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",
1025 hwnd, wndPtr->rectWindow.left, wndPtr->rectWindow.top,
1026 wndPtr->rectWindow.right, wndPtr->rectWindow.bottom,
1027 wndPtr->rectClient.left, wndPtr->rectClient.top,
1028 wndPtr->rectClient.right, wndPtr->rectClient.bottom,
1029 data->whole_rect.left, data->whole_rect.top,
1030 data->whole_rect.right, data->whole_rect.bottom,
1031 data->client_rect.left, data->client_rect.top,
1032 data->client_rect.right, data->client_rect.bottom,
1033 (unsigned int)data->whole_window );
1035 WIN_ReleasePtr( wndPtr );
1038 ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1040 ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1042 if (!ret) return FALSE;
1044 NotifyWinEvent(EVENT_OBJECT_CREATE, hwnd, OBJID_WINDOW, 0);
1046 /* Send the size messages */
1048 if (!(wndPtr = WIN_GetPtr(hwnd)) || wndPtr == WND_OTHER_PROCESS) return FALSE;
1049 if (!(wndPtr->flags & WIN_NEED_SIZE))
1051 RECT rect = wndPtr->rectClient;
1052 WIN_ReleasePtr( wndPtr );
1053 /* send it anyway */
1054 if (((rect.right-rect.left) <0) ||((rect.bottom-rect.top)<0))
1055 WARN("sending bogus WM_SIZE message 0x%08lx\n",
1056 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1057 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1058 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1059 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( rect.left, rect.top ) );
1061 else WIN_ReleasePtr( wndPtr );
1063 /* Show the window, maximizing or minimizing if needed */
1065 style = GetWindowLongW( hwnd, GWL_STYLE );
1066 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1068 extern UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect ); /*FIXME*/
1071 UINT swFlag = (style & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1072 WIN_SetStyle( hwnd, 0, WS_MAXIMIZE | WS_MINIMIZE );
1073 WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1075 swFlag = SWP_FRAMECHANGED | SWP_NOZORDER; /* Frame always gets changed */
1076 if (!(style & WS_VISIBLE) || (style & WS_CHILD) || GetActiveWindow()) swFlag |= SWP_NOACTIVATE;
1078 SetWindowPos( hwnd, 0, newPos.left, newPos.top,
1079 newPos.right, newPos.bottom, swFlag );
1085 X11DRV_DestroyWindow( hwnd );
1090 /***********************************************************************
1091 * X11DRV_get_win_data
1093 * Return the X11 data structure associated with a window.
1095 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1099 if (!hwnd || XFindContext( thread_display(), (XID)hwnd, win_data_context, &data )) data = NULL;
1100 return (struct x11drv_win_data *)data;
1104 /***********************************************************************
1105 * X11DRV_get_whole_window
1107 * Return the X window associated with the full area of a window
1109 Window X11DRV_get_whole_window( HWND hwnd )
1111 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1113 if (!data) return (Window)GetPropA( hwnd, whole_window_prop );
1114 return data->whole_window;
1118 /***********************************************************************
1121 * Return the X input context associated with a window
1123 XIC X11DRV_get_ic( HWND hwnd )
1125 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1127 if (!data) return 0;
1132 /*****************************************************************
1133 * SetParent (X11DRV.@)
1135 HWND X11DRV_SetParent( HWND hwnd, HWND parent )
1137 Display *display = thread_display();
1140 HWND old_parent = 0;
1142 /* Windows hides the window first, then shows it again
1143 * including the WM_SHOWWINDOW messages and all */
1144 BOOL was_visible = ShowWindow( hwnd, SW_HIDE );
1146 wndPtr = WIN_GetPtr( hwnd );
1147 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return 0;
1149 SERVER_START_REQ( set_parent )
1152 req->parent = parent;
1153 if ((ret = !wine_server_call( req )))
1155 old_parent = reply->old_parent;
1156 wndPtr->parent = parent = reply->full_parent;
1161 WIN_ReleasePtr( wndPtr );
1164 if (parent != old_parent)
1166 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1168 if (!data) return 0;
1170 if (parent != GetDesktopWindow()) /* a child window */
1172 if (old_parent == GetDesktopWindow())
1174 /* destroy the old X windows */
1175 destroy_whole_window( display, data );
1176 destroy_icon_window( display, data );
1179 data->managed = FALSE;
1180 RemovePropA( data->hwnd, managed_prop );
1184 else /* new top level window */
1186 /* FIXME: we ignore errors since we can't really recover anyway */
1187 create_whole_window( display, data, GetWindowLongW( hwnd, GWL_STYLE ) );
1191 /* SetParent additionally needs to make hwnd the topmost window
1192 in the x-order and send the expected WM_WINDOWPOSCHANGING and
1193 WM_WINDOWPOSCHANGED notification messages.
1195 SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0,
1196 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | (was_visible ? SWP_SHOWWINDOW : 0) );
1197 /* FIXME: a WM_MOVE is also generated (in the DefWindowProc handler
1198 * for WM_WINDOWPOSCHANGED) in Windows, should probably remove SWP_NOMOVE */
1204 /*****************************************************************
1205 * SetFocus (X11DRV.@)
1208 * Explicit colormap management seems to work only with OLVWM.
1210 void X11DRV_SetFocus( HWND hwnd )
1212 Display *display = thread_display();
1213 struct x11drv_win_data *data;
1214 XWindowAttributes win_attr;
1216 /* Only mess with the X focus if there's */
1217 /* no desktop window and if the window is not managed by the WM. */
1218 if (root_window != DefaultRootWindow(display)) return;
1220 if (!hwnd) /* If setting the focus to 0, uninstall the colormap */
1223 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1224 XUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1225 wine_tsx11_unlock();
1229 hwnd = GetAncestor( hwnd, GA_ROOT );
1231 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1232 if (data->managed || !data->whole_window) return;
1234 /* Set X focus and install colormap */
1236 if (XGetWindowAttributes( display, data->whole_window, &win_attr ) &&
1237 (win_attr.map_state == IsViewable))
1239 /* If window is not viewable, don't change anything */
1241 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1242 /* FIXME: this is not entirely correct */
1243 XSetInputFocus( display, data->whole_window, RevertToParent,
1245 GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1246 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1247 XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1249 wine_tsx11_unlock();
1253 /**********************************************************************
1254 * SetWindowIcon (X11DRV.@)
1256 * hIcon or hIconSm has changed (or is being initialised for the
1257 * first time). Complete the X11 driver-specific initialisation
1258 * and set the window hints.
1260 * This is not entirely correct, may need to create
1261 * an icon window and set the pixmap as a background
1263 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
1265 Display *display = thread_display();
1266 struct x11drv_win_data *data;
1269 if (type != ICON_BIG) return; /* nothing to do here */
1271 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1272 if (!data->whole_window) return;
1273 if (!data->managed) return;
1276 if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
1277 wine_tsx11_unlock();
1280 set_icon_hints( display, data, wm_hints, icon );
1282 XSetWMHints( display, data->whole_window, wm_hints );
1284 wine_tsx11_unlock();