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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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"
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 /***********************************************************************
65 * Check if a given window should be managed
67 inline static BOOL is_window_managed( HWND hwnd )
69 DWORD style, ex_style;
71 if (!managed_mode) return FALSE;
72 /* tray window is always managed */
73 ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
74 if (ex_style & WS_EX_TRAYWINDOW) return TRUE;
75 /* child windows are not managed */
76 style = GetWindowLongW( hwnd, GWL_STYLE );
77 if (style & WS_CHILD) return FALSE;
78 /* windows with caption are managed */
79 if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
80 /* tool windows are not managed */
81 if (ex_style & WS_EX_TOOLWINDOW) return FALSE;
82 /* windows with thick frame are managed */
83 if (style & WS_THICKFRAME) return TRUE;
84 /* application windows are managed */
85 if (ex_style & WS_EX_APPWINDOW) return TRUE;
86 /* full-screen popup windows are managed */
90 GetWindowRect( hwnd, &rect );
91 if ((rect.right - rect.left) == screen_width && (rect.bottom - rect.top) == screen_height)
94 /* default: not managed */
99 /***********************************************************************
100 * X11DRV_is_window_rect_mapped
102 * Check if the X whole window should be mapped based on its rectangle
104 BOOL X11DRV_is_window_rect_mapped( const RECT *rect )
106 /* don't map if rect is empty */
107 if (IsRectEmpty( rect )) return FALSE;
109 /* don't map if rect is off-screen */
110 if (rect->left >= (int)screen_width || rect->top >= (int)screen_height) return FALSE;
111 if (rect->right < 0 || rect->bottom < 0) return FALSE;
117 /***********************************************************************
118 * get_window_attributes
120 * Fill the window attributes structure for an X window.
122 static int get_window_attributes( struct x11drv_win_data *data, XSetWindowAttributes *attr )
124 if (!data->managed && !using_wine_desktop && is_window_managed( data->hwnd ))
126 data->managed = TRUE;
127 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
129 attr->override_redirect = !data->managed;
130 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
131 attr->save_under = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
132 attr->cursor = x11drv_thread_data()->cursor;
133 attr->event_mask = (ExposureMask | PointerMotionMask |
134 ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
135 KeyPressMask | KeyReleaseMask | StructureNotifyMask |
136 FocusChangeMask | KeymapStateMask);
138 return (CWOverrideRedirect | CWSaveUnder | CWEventMask | CWColormap | CWCursor);
142 /***********************************************************************
143 * X11DRV_sync_window_style
145 * Change the X window attributes when the window style has changed.
147 void X11DRV_sync_window_style( Display *display, struct x11drv_win_data *data )
149 XSetWindowAttributes attr;
150 int mask = get_window_attributes( data, &attr );
153 XChangeWindowAttributes( display, data->whole_window, mask, &attr );
158 /***********************************************************************
161 * fill the window changes structure
163 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
167 if (old->right - old->left != new->right - new->left )
169 if (!(changes->width = new->right - new->left)) changes->width = 1;
172 if (old->bottom - old->top != new->bottom - new->top)
174 if (!(changes->height = new->bottom - new->top)) changes->height = 1;
177 if (old->left != new->left)
179 changes->x = new->left;
182 if (old->top != new->top)
184 changes->y = new->top;
191 /***********************************************************************
194 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
196 XSetWindowAttributes attr;
198 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
199 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
200 attr.bit_gravity = NorthWestGravity;
201 attr.backing_store = NotUseful/*WhenMapped*/;
202 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
205 data->icon_window = XCreateWindow( display, root_window, 0, 0,
206 GetSystemMetrics( SM_CXICON ),
207 GetSystemMetrics( SM_CYICON ),
210 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
211 XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
214 TRACE( "created %lx\n", data->icon_window );
215 SetPropA( data->hwnd, icon_window_prop, (HANDLE)data->icon_window );
216 return data->icon_window;
221 /***********************************************************************
222 * destroy_icon_window
224 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
226 if (!data->icon_window) return;
227 if (x11drv_thread_data()->cursor_window == data->icon_window)
228 x11drv_thread_data()->cursor_window = None;
230 XDeleteContext( display, data->icon_window, winContext );
231 XDestroyWindow( display, data->icon_window );
232 data->icon_window = 0;
234 RemovePropA( data->hwnd, icon_window_prop );
238 /***********************************************************************
241 * Set the icon wm hints
243 static void set_icon_hints( Display *display, struct x11drv_win_data *data,
244 XWMHints *hints, HICON hIcon )
246 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
247 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
248 data->hWMIconBitmap = 0;
249 data->hWMIconMask = 0;
253 destroy_icon_window( display, data );
254 hints->flags &= ~(IconPixmapHint | IconMaskHint | IconWindowHint);
258 if (!data->icon_window) create_icon_window( display, data );
259 hints->icon_window = data->icon_window;
260 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
270 GetIconInfo(hIcon, &ii);
272 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
275 rcMask.right = bmMask.bmWidth;
276 rcMask.bottom = bmMask.bmHeight;
278 hDC = CreateCompatibleDC(0);
279 hbmOrig = SelectObject(hDC, ii.hbmMask);
280 InvertRect(hDC, &rcMask);
281 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
282 SelectObject(hDC, hbmOrig);
285 data->hWMIconBitmap = ii.hbmColor;
286 data->hWMIconMask = ii.hbmMask;
288 hints->icon_pixmap = X11DRV_get_pixmap(data->hWMIconBitmap);
289 hints->icon_mask = X11DRV_get_pixmap(data->hWMIconMask);
290 destroy_icon_window( display, data );
291 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
296 /***********************************************************************
299 * set the window size hints
301 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
303 XSizeHints* size_hints;
305 if ((size_hints = XAllocSizeHints()))
307 size_hints->win_gravity = StaticGravity;
308 size_hints->x = data->whole_rect.left;
309 size_hints->y = data->whole_rect.top;
310 size_hints->flags = PWinGravity | PPosition;
312 if ( !(style & WS_THICKFRAME) )
314 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
315 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
316 size_hints->min_width = size_hints->max_width;
317 size_hints->min_height = size_hints->max_height;
318 size_hints->flags |= PMinSize | PMaxSize;
320 XSetWMNormalHints( display, data->whole_window, size_hints );
326 /***********************************************************************
329 * get the name of the current process for setting class hints
331 static char *get_process_name(void)
337 WCHAR module[MAX_PATH];
338 DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
339 if (len && len < MAX_PATH)
342 WCHAR *p, *appname = module;
344 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
345 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
346 len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
347 if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
349 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
358 /***********************************************************************
359 * X11DRV_set_wm_hints
361 * Set the window manager hints for a newly-created window
363 void X11DRV_set_wm_hints( Display *display, struct x11drv_win_data *data )
366 XClassHint *class_hints;
372 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
373 DWORD ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
374 HWND owner = GetWindow( data->hwnd, GW_OWNER );
375 char *process_name = get_process_name();
377 /* transient for hint */
380 Window owner_win = X11DRV_get_whole_window( owner );
382 XSetTransientForHint( display, data->whole_window, owner_win );
384 group_leader = owner_win;
386 else group_leader = data->whole_window;
392 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
393 protocols[i++] = x11drv_atom(_NET_WM_PING);
394 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
395 XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
396 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
399 if ((class_hints = XAllocClassHint()))
401 class_hints->res_name = process_name;
402 class_hints->res_class = "Wine";
403 XSetClassHint( display, data->whole_window, class_hints );
404 XFree( class_hints );
408 set_size_hints( display, data, style );
410 /* systray properties (KDE only for now) */
411 if (ex_style & WS_EX_TRAYWINDOW)
414 XChangeProperty( display, data->whole_window, x11drv_atom(KWM_DOCKWINDOW),
415 x11drv_atom(KWM_DOCKWINDOW), 32, PropModeReplace, (unsigned char*)&val, 1 );
416 XChangeProperty( display, data->whole_window, x11drv_atom(_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR),
417 XA_WINDOW, 32, PropModeReplace, (unsigned char*)&data->whole_window, 1 );
420 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
421 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
422 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
424 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
425 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
427 /* map WS_EX_TOOLWINDOW to _NET_WM_WINDOW_TYPE_UTILITY */
428 if (ex_style & WS_EX_TOOLWINDOW)
430 Atom a = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
431 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
432 XA_ATOM, 32, PropModeReplace, (unsigned char*)&a, 1);
435 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
436 mwm_hints.functions = 0;
437 if ((style & WS_CAPTION) == WS_CAPTION) mwm_hints.functions |= MWM_FUNC_MOVE;
438 if (style & WS_THICKFRAME) mwm_hints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
439 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
440 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
441 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
442 if (ex_style & WS_EX_APPWINDOW) mwm_hints.functions |= MWM_FUNC_MOVE;
443 mwm_hints.decorations = 0;
444 if ((style & WS_CAPTION) == WS_CAPTION)
446 mwm_hints.decorations |= MWM_DECOR_TITLE;
447 if (style & WS_SYSMENU) mwm_hints.decorations |= MWM_DECOR_MENU;
448 if (style & WS_MINIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MINIMIZE;
449 if (style & WS_MAXIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MAXIMIZE;
451 if (ex_style & WS_EX_DLGMODALFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
452 else if (style & WS_THICKFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
453 else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
454 else if (style & WS_BORDER) mwm_hints.decorations |= MWM_DECOR_BORDER;
455 else if (!(style & (WS_CHILD|WS_POPUP))) mwm_hints.decorations |= MWM_DECOR_BORDER;
457 XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
458 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
459 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
461 XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
462 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
464 wm_hints = XAllocWMHints();
470 wm_hints->flags = InputHint | StateHint | WindowGroupHint;
471 wm_hints->input = !(style & WS_DISABLED);
473 set_icon_hints( display, data, wm_hints,
474 (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON ) );
476 wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
477 wm_hints->window_group = group_leader;
480 XSetWMHints( display, data->whole_window, wm_hints );
487 /***********************************************************************
488 * X11DRV_set_iconic_state
490 * Set the X11 iconic state according to the window style.
492 void X11DRV_set_iconic_state( HWND hwnd )
494 Display *display = thread_display();
495 struct x11drv_win_data *data;
498 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
499 BOOL iconic = (style & WS_MINIMIZE) != 0;
501 if (!(data = X11DRV_get_win_data( hwnd ))) return;
502 if (!data->whole_window) return;
504 GetWindowRect( hwnd, &rect );
508 if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
509 wm_hints->flags |= StateHint | IconPositionHint;
510 wm_hints->initial_state = iconic ? IconicState : NormalState;
511 wm_hints->icon_x = rect.left;
512 wm_hints->icon_y = rect.top;
513 XSetWMHints( display, data->whole_window, wm_hints );
515 if (style & WS_VISIBLE)
518 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
520 if (X11DRV_is_window_rect_mapped( &rect ))
521 XMapWindow( display, data->whole_window );
529 /***********************************************************************
530 * X11DRV_window_to_X_rect
532 * Convert a rect from client to X window coordinates
534 void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
538 if (!data->managed) return;
539 if (IsRectEmpty( rect )) return;
541 rc.top = rc.bottom = rc.left = rc.right = 0;
543 AdjustWindowRectEx( &rc, GetWindowLongW( data->hwnd, GWL_STYLE ) & ~(WS_HSCROLL|WS_VSCROLL),
544 FALSE, GetWindowLongW( data->hwnd, GWL_EXSTYLE ) );
546 rect->left -= rc.left;
547 rect->right -= rc.right;
549 rect->bottom -= rc.bottom;
550 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
551 if (rect->left >= rect->right) rect->right = rect->left + 1;
555 /***********************************************************************
556 * X11DRV_X_to_window_rect
558 * Opposite of X11DRV_window_to_X_rect
560 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
562 if (!data->managed) return;
563 if (IsRectEmpty( rect )) return;
565 AdjustWindowRectEx( rect, GetWindowLongW( data->hwnd, GWL_STYLE ) & ~(WS_HSCROLL|WS_VSCROLL),
566 FALSE, GetWindowLongW( data->hwnd, GWL_EXSTYLE ));
568 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
569 if (rect->left >= rect->right) rect->right = rect->left + 1;
573 /***********************************************************************
574 * X11DRV_sync_window_position
576 * Synchronize the X window position with the Windows one
578 void X11DRV_sync_window_position( Display *display, struct x11drv_win_data *data,
579 UINT swp_flags, const RECT *new_client_rect,
580 const RECT *new_whole_rect )
582 XWindowChanges changes;
586 old_whole_rect = data->whole_rect;
587 data->whole_rect = *new_whole_rect;
589 data->client_rect = *new_client_rect;
590 OffsetRect( &data->client_rect, -data->whole_rect.left, -data->whole_rect.top );
592 if (!data->whole_window) return;
593 if (swp_flags & SWP_WINE_NOHOSTMOVE) return;
595 mask = get_window_changes( &changes, &old_whole_rect, &data->whole_rect );
597 if (!(swp_flags & SWP_NOZORDER))
599 /* find window that this one must be after */
600 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
601 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
602 prev = GetWindow( prev, GW_HWNDPREV );
603 if (!prev) /* top child */
605 changes.stack_mode = Above;
610 /* should use stack_mode Below but most window managers don't get it right */
611 /* so move it above the next one in Z order */
612 HWND next = GetWindow( data->hwnd, GW_HWNDNEXT );
613 while (next && !(GetWindowLongW( next, GWL_STYLE ) & WS_VISIBLE))
614 next = GetWindow( next, GW_HWNDNEXT );
617 changes.stack_mode = Above;
618 changes.sibling = X11DRV_get_whole_window(next);
619 mask |= CWStackMode | CWSibling;
626 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
628 TRACE( "setting win %lx pos %ld,%ld,%ldx%ld after %lx changes=%x\n",
629 data->whole_window, data->whole_rect.left, data->whole_rect.top,
630 data->whole_rect.right - data->whole_rect.left,
631 data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
634 XSync( gdi_display, False ); /* flush graphics operations before moving the window */
635 if (mask & (CWWidth|CWHeight)) set_size_hints( display, data, style );
636 XReconfigureWMWindow( display, data->whole_window,
637 DefaultScreen(display), mask, &changes );
643 /**********************************************************************
644 * create_whole_window
646 * Create the whole X window for a given window
648 static Window create_whole_window( Display *display, struct x11drv_win_data *data, DWORD style )
651 XSetWindowAttributes attr;
655 rect = data->window_rect;
656 X11DRV_window_to_X_rect( data, &rect );
658 if (!(cx = rect.right - rect.left)) cx = 1;
659 if (!(cy = rect.bottom - rect.top)) cy = 1;
661 mask = get_window_attributes( data, &attr );
663 /* set the attributes that don't change over the lifetime of the window */
664 attr.bit_gravity = NorthWestGravity;
665 attr.win_gravity = StaticGravity;
666 attr.backing_store = NotUseful;
667 mask |= CWBitGravity | CWWinGravity | CWBackingStore;
671 data->whole_rect = rect;
672 data->whole_window = XCreateWindow( display, root_window, rect.left, rect.top, cx, cy,
673 0, screen_depth, InputOutput, visual,
676 if (!data->whole_window)
681 XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
683 /* non-maximized child must be at bottom of Z order */
684 if ((style & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
686 XWindowChanges changes;
687 changes.stack_mode = Below;
688 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
690 XSync( display, False ); /* FIXME: should not be needed */
694 xim = x11drv_thread_data()->xim;
695 if (xim) data->xic = X11DRV_CreateIC( xim, display, data->whole_window );
697 X11DRV_set_wm_hints( display, data );
699 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
700 return data->whole_window;
704 /**********************************************************************
705 * destroy_whole_window
707 * Destroy the whole X window for a given window.
709 static void destroy_whole_window( Display *display, struct x11drv_win_data *data )
711 struct x11drv_thread_data *thread_data = x11drv_thread_data();
713 if (!data->whole_window) return;
715 TRACE( "win %p xwin %lx\n", data->hwnd, data->whole_window );
716 if (thread_data->cursor_window == data->whole_window) thread_data->cursor_window = None;
718 XSync( gdi_display, False ); /* flush any reference to this drawable in GDI queue */
719 XDeleteContext( display, data->whole_window, winContext );
720 XDestroyWindow( display, data->whole_window ); /* this destroys client too */
721 data->whole_window = 0;
724 XUnsetICFocus( data->xic );
725 XDestroyIC( data->xic );
728 RemovePropA( data->hwnd, whole_window_prop );
732 /*****************************************************************
733 * SetWindowText (X11DRV.@)
735 BOOL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
737 Display *display = thread_display();
744 if ((win = X11DRV_get_whole_window( hwnd )))
746 /* allocate new buffer for window text */
747 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
748 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count )))
750 ERR("Not enough memory for window text\n");
753 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
755 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
756 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
758 ERR("Not enough memory for window text in UTF-8\n");
759 HeapFree( GetProcessHeap(), 0, buffer );
762 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
765 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
767 XSetWMName( display, win, &prop );
768 XSetWMIconName( display, win, &prop );
772 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
773 according to the standard
774 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
776 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
777 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
780 HeapFree( GetProcessHeap(), 0, utf8_buffer );
781 HeapFree( GetProcessHeap(), 0, buffer );
787 /***********************************************************************
788 * DestroyWindow (X11DRV.@)
790 BOOL X11DRV_DestroyWindow( HWND hwnd )
792 struct x11drv_thread_data *thread_data = x11drv_thread_data();
793 Display *display = thread_data->display;
794 struct x11drv_win_data *data;
796 if (!(data = X11DRV_get_win_data( hwnd ))) return TRUE;
798 free_window_dce( data );
799 destroy_whole_window( display, data );
800 destroy_icon_window( display, data );
802 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
803 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
804 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
806 XDeleteContext( display, (XID)hwnd, win_data_context );
808 HeapFree( GetProcessHeap(), 0, data );
814 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
816 struct x11drv_win_data *data;
818 if ((data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data))))
821 data->whole_window = 0;
822 data->icon_window = 0;
824 data->managed = FALSE;
826 data->hWMIconBitmap = 0;
827 data->hWMIconMask = 0;
830 if (!winContext) winContext = XUniqueContext();
831 if (!win_data_context) win_data_context = XUniqueContext();
832 XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
839 /**********************************************************************
840 * CreateDesktopWindow (X11DRV.@)
842 BOOL X11DRV_CreateDesktopWindow( HWND hwnd )
844 Display *display = thread_display();
846 struct x11drv_win_data *data;
849 if (!(data = alloc_win_data( display, hwnd ))) return FALSE;
850 data->whole_window = root_window;
852 SetRect( &rect, 0, 0, screen_width, screen_height );
853 X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER, NULL );
856 visualid = XVisualIDFromVisual(visual);
859 SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
860 SetPropA( data->hwnd, visual_id_prop, (HANDLE)visualid );
862 if (root_window != DefaultRootWindow(display) && !desktop_tid) X11DRV_create_desktop_thread();
868 /**********************************************************************
869 * CreateWindow (X11DRV.@)
871 BOOL X11DRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
873 Display *display = thread_display();
875 struct x11drv_win_data *data;
883 if (!(data = alloc_win_data( display, hwnd ))) return FALSE;
887 ERR( "invalid window width %d\n", cs->cx );
892 ERR( "invalid window height %d\n", cs->cy );
897 ERR( "invalid window width %d\n", cs->cx );
902 ERR( "invalid window height %d\n", cs->cy );
906 /* initialize the dimensions before sending WM_GETMINMAXINFO */
907 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
908 X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER, NULL );
910 /* create an X window if it's a top level window */
911 if (GetAncestor( hwnd, GA_PARENT ) == GetDesktopWindow())
913 if (!create_whole_window( display, data, cs->style )) goto failed;
916 /* get class or window DC if needed */
917 alloc_window_dce( data );
919 /* Call the WH_CBT hook */
921 /* the window style passed to the hook must be the real window style,
922 * rather than just the window style that the caller to CreateWindowEx
923 * passed in, so we have to copy the original CREATESTRUCT and get the
926 cbcs.style = GetWindowLongW(hwnd, GWL_STYLE);
929 cbtc.hwndInsertAfter = HWND_TOP;
930 if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode ))
932 TRACE("CBT-hook returned !0\n");
936 /* Send the WM_GETMINMAXINFO message and fix the size if needed */
937 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
939 POINT maxSize, maxPos, minTrack, maxTrack;
941 WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
942 if (maxSize.x < cs->cx) cs->cx = maxSize.x;
943 if (maxSize.y < cs->cy) cs->cy = maxSize.y;
944 if (cs->cx < 0) cs->cx = 0;
945 if (cs->cy < 0) cs->cy = 0;
947 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
948 if (!X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER, NULL )) return FALSE;
951 /* send WM_NCCREATE */
952 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cs->cx, cs->cy );
954 ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
956 ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
959 WARN("aborted by WM_xxCREATE!\n");
963 /* make sure the window is still valid */
964 if (!(data = X11DRV_get_win_data( hwnd ))) return FALSE;
965 if (data->whole_window) X11DRV_sync_window_style( display, data );
967 /* send WM_NCCALCSIZE */
968 rect = data->window_rect;
969 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&rect );
971 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
973 /* yes, even if the CBT hook was called with HWND_TOP */
974 insert_after = ((wndPtr->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
976 X11DRV_set_window_pos( hwnd, insert_after, &wndPtr->rectWindow, &rect, 0, NULL );
978 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",
979 hwnd, wndPtr->rectWindow.left, wndPtr->rectWindow.top,
980 wndPtr->rectWindow.right, wndPtr->rectWindow.bottom,
981 wndPtr->rectClient.left, wndPtr->rectClient.top,
982 wndPtr->rectClient.right, wndPtr->rectClient.bottom,
983 data->whole_rect.left, data->whole_rect.top,
984 data->whole_rect.right, data->whole_rect.bottom,
985 data->client_rect.left, data->client_rect.top,
986 data->client_rect.right, data->client_rect.bottom,
987 (unsigned int)data->whole_window );
989 WIN_ReleasePtr( wndPtr );
992 ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
994 ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
996 if (!ret) return FALSE;
998 NotifyWinEvent(EVENT_OBJECT_CREATE, hwnd, OBJID_WINDOW, 0);
1000 /* Send the size messages */
1002 if (!(wndPtr = WIN_GetPtr(hwnd)) || wndPtr == WND_OTHER_PROCESS) return FALSE;
1003 if (!(wndPtr->flags & WIN_NEED_SIZE))
1005 RECT rect = wndPtr->rectClient;
1006 WIN_ReleasePtr( wndPtr );
1007 /* send it anyway */
1008 if (((rect.right-rect.left) <0) ||((rect.bottom-rect.top)<0))
1009 WARN("sending bogus WM_SIZE message 0x%08lx\n",
1010 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1011 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1012 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1013 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( rect.left, rect.top ) );
1015 else WIN_ReleasePtr( wndPtr );
1017 /* Show the window, maximizing or minimizing if needed */
1019 style = GetWindowLongW( hwnd, GWL_STYLE );
1020 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1022 extern UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect ); /*FIXME*/
1025 UINT swFlag = (style & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1026 WIN_SetStyle( hwnd, 0, WS_MAXIMIZE | WS_MINIMIZE );
1027 WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1028 swFlag = ((style & WS_CHILD) || GetActiveWindow())
1029 ? SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED
1030 : SWP_NOZORDER | SWP_FRAMECHANGED;
1031 SetWindowPos( hwnd, 0, newPos.left, newPos.top,
1032 newPos.right, newPos.bottom, swFlag );
1038 X11DRV_DestroyWindow( hwnd );
1043 /***********************************************************************
1044 * X11DRV_get_win_data
1046 * Return the X11 data structure associated with a window.
1048 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1052 if (!hwnd || XFindContext( thread_display(), (XID)hwnd, win_data_context, &data )) data = NULL;
1053 return (struct x11drv_win_data *)data;
1057 /***********************************************************************
1058 * X11DRV_get_whole_window
1060 * Return the X window associated with the full area of a window
1062 Window X11DRV_get_whole_window( HWND hwnd )
1064 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1066 if (!data) return (Window)GetPropA( hwnd, whole_window_prop );
1067 return data->whole_window;
1071 /***********************************************************************
1074 * Return the X input context associated with a window
1076 XIC X11DRV_get_ic( HWND hwnd )
1078 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1080 if (!data) return 0;
1085 /*****************************************************************
1086 * SetParent (X11DRV.@)
1088 HWND X11DRV_SetParent( HWND hwnd, HWND parent )
1090 Display *display = thread_display();
1093 HWND old_parent = 0;
1095 /* Windows hides the window first, then shows it again
1096 * including the WM_SHOWWINDOW messages and all */
1097 BOOL was_visible = ShowWindow( hwnd, SW_HIDE );
1099 wndPtr = WIN_GetPtr( hwnd );
1100 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return 0;
1102 SERVER_START_REQ( set_parent )
1105 req->parent = parent;
1106 if ((ret = !wine_server_call( req )))
1108 old_parent = reply->old_parent;
1109 wndPtr->parent = parent = reply->full_parent;
1114 WIN_ReleasePtr( wndPtr );
1117 if (parent != old_parent)
1119 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1121 if (!data) return 0;
1123 if (parent != GetDesktopWindow()) /* a child window */
1125 if (old_parent == GetDesktopWindow())
1127 /* destroy the old X windows */
1128 destroy_whole_window( display, data );
1129 destroy_icon_window( display, data );
1132 data->managed = FALSE;
1133 RemovePropA( data->hwnd, managed_prop );
1137 else /* new top level window */
1139 /* FIXME: we ignore errors since we can't really recover anyway */
1140 create_whole_window( display, data, GetWindowLongW( hwnd, GWL_STYLE ) );
1144 /* SetParent additionally needs to make hwnd the topmost window
1145 in the x-order and send the expected WM_WINDOWPOSCHANGING and
1146 WM_WINDOWPOSCHANGED notification messages.
1148 SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0,
1149 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | (was_visible ? SWP_SHOWWINDOW : 0) );
1150 /* FIXME: a WM_MOVE is also generated (in the DefWindowProc handler
1151 * for WM_WINDOWPOSCHANGED) in Windows, should probably remove SWP_NOMOVE */
1157 /*****************************************************************
1158 * SetFocus (X11DRV.@)
1161 * Explicit colormap management seems to work only with OLVWM.
1163 void X11DRV_SetFocus( HWND hwnd )
1165 Display *display = thread_display();
1166 struct x11drv_win_data *data;
1167 XWindowAttributes win_attr;
1169 /* Only mess with the X focus if there's */
1170 /* no desktop window and if the window is not managed by the WM. */
1171 if (root_window != DefaultRootWindow(display)) return;
1173 if (!hwnd) /* If setting the focus to 0, uninstall the colormap */
1176 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1177 XUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1178 wine_tsx11_unlock();
1182 hwnd = GetAncestor( hwnd, GA_ROOT );
1184 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1185 if (!data->managed || !data->whole_window) return;
1187 /* Set X focus and install colormap */
1189 if (XGetWindowAttributes( display, data->whole_window, &win_attr ) &&
1190 (win_attr.map_state == IsViewable))
1192 /* If window is not viewable, don't change anything */
1194 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1195 /* FIXME: this is not entirely correct */
1196 XSetInputFocus( display, data->whole_window, RevertToParent,
1198 GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1199 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1200 XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1202 wine_tsx11_unlock();
1206 /**********************************************************************
1207 * SetWindowIcon (X11DRV.@)
1209 * hIcon or hIconSm has changed (or is being initialised for the
1210 * first time). Complete the X11 driver-specific initialisation
1211 * and set the window hints.
1213 * This is not entirely correct, may need to create
1214 * an icon window and set the pixmap as a background
1216 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
1218 Display *display = thread_display();
1219 struct x11drv_win_data *data;
1222 if (type != ICON_BIG) return; /* nothing to do here */
1224 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1225 if (!data->whole_window) return;
1226 if (!data->managed) return;
1229 if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
1230 wine_tsx11_unlock();
1233 set_icon_hints( display, data, wm_hints, icon );
1235 XSetWMHints( display, data->whole_window, wm_hints );
1237 wine_tsx11_unlock();