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 Atom X11DRV_Atoms[NB_XATOMS - FIRST_XATOM];
59 static const char * const atom_names[NB_XATOMS - FIRST_XATOM] =
78 "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR",
83 "_NET_WM_WINDOW_TYPE",
84 "_NET_WM_WINDOW_TYPE_UTILITY",
108 static LPCSTR whole_window_atom;
109 static LPCSTR icon_window_atom;
110 static LPCSTR managed_atom;
112 /***********************************************************************
115 * Check if a given window should be managed
117 inline static BOOL is_window_managed( HWND hwnd )
119 DWORD style, ex_style;
121 if (!managed_mode) return FALSE;
122 /* tray window is always managed */
123 ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
124 if (ex_style & WS_EX_TRAYWINDOW) return TRUE;
125 /* child windows are not managed */
126 style = GetWindowLongW( hwnd, GWL_STYLE );
127 if (style & WS_CHILD) return FALSE;
128 /* windows with caption are managed */
129 if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
130 /* tool windows are not managed */
131 if (ex_style & WS_EX_TOOLWINDOW) return FALSE;
132 /* windows with thick frame are managed */
133 if (style & WS_THICKFRAME) return TRUE;
134 /* application windows are managed */
135 if (ex_style & WS_EX_APPWINDOW) return TRUE;
136 /* full-screen popup windows are managed */
137 if (style & WS_POPUP)
140 GetWindowRect( hwnd, &rect );
141 if ((rect.right - rect.left) == screen_width && (rect.bottom - rect.top) == screen_height)
144 /* default: not managed */
149 /***********************************************************************
150 * X11DRV_is_window_rect_mapped
152 * Check if the X whole window should be mapped based on its rectangle
154 BOOL X11DRV_is_window_rect_mapped( const RECT *rect )
156 /* don't map if rect is empty */
157 if (IsRectEmpty( rect )) return FALSE;
159 /* don't map if rect is off-screen */
160 if (rect->left >= (int)screen_width || rect->top >= (int)screen_height) return FALSE;
161 if (rect->right < 0 || rect->bottom < 0) return FALSE;
167 /***********************************************************************
168 * get_window_attributes
170 * Fill the window attributes structure for an X window.
172 static int get_window_attributes( struct x11drv_win_data *data, XSetWindowAttributes *attr )
174 if (!data->managed && !using_wine_desktop && is_window_managed( data->hwnd ))
176 data->managed = TRUE;
177 SetPropA( data->hwnd, managed_atom, (HANDLE)1 );
179 attr->override_redirect = !data->managed;
180 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
181 attr->save_under = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
182 attr->cursor = x11drv_thread_data()->cursor;
183 attr->event_mask = (ExposureMask | PointerMotionMask |
184 ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
185 KeyPressMask | KeyReleaseMask | StructureNotifyMask |
186 FocusChangeMask | KeymapStateMask);
188 return (CWOverrideRedirect | CWSaveUnder | CWEventMask | CWColormap | CWCursor);
192 /***********************************************************************
193 * X11DRV_sync_window_style
195 * Change the X window attributes when the window style has changed.
197 void X11DRV_sync_window_style( Display *display, struct x11drv_win_data *data )
199 XSetWindowAttributes attr;
200 int mask = get_window_attributes( data, &attr );
203 XChangeWindowAttributes( display, data->whole_window, mask, &attr );
208 /***********************************************************************
211 * fill the window changes structure
213 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
217 if (old->right - old->left != new->right - new->left )
219 if (!(changes->width = new->right - new->left)) changes->width = 1;
222 if (old->bottom - old->top != new->bottom - new->top)
224 if (!(changes->height = new->bottom - new->top)) changes->height = 1;
227 if (old->left != new->left)
229 changes->x = new->left;
232 if (old->top != new->top)
234 changes->y = new->top;
241 /***********************************************************************
244 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
246 XSetWindowAttributes attr;
248 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
249 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
250 attr.bit_gravity = NorthWestGravity;
251 attr.backing_store = NotUseful/*WhenMapped*/;
252 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
255 data->icon_window = XCreateWindow( display, root_window, 0, 0,
256 GetSystemMetrics( SM_CXICON ),
257 GetSystemMetrics( SM_CYICON ),
260 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
261 XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
264 TRACE( "created %lx\n", data->icon_window );
265 SetPropA( data->hwnd, icon_window_atom, (HANDLE)data->icon_window );
266 return data->icon_window;
271 /***********************************************************************
272 * destroy_icon_window
274 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
276 if (!data->icon_window) return;
277 if (x11drv_thread_data()->cursor_window == data->icon_window)
278 x11drv_thread_data()->cursor_window = None;
280 XDeleteContext( display, data->icon_window, winContext );
281 XDestroyWindow( display, data->icon_window );
282 data->icon_window = 0;
284 RemovePropA( data->hwnd, icon_window_atom );
288 /***********************************************************************
291 * Set the icon wm hints
293 static void set_icon_hints( Display *display, struct x11drv_win_data *data,
294 XWMHints *hints, HICON hIcon )
296 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
297 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
298 data->hWMIconBitmap = 0;
299 data->hWMIconMask = 0;
303 destroy_icon_window( display, data );
304 hints->flags &= ~(IconPixmapHint | IconMaskHint | IconWindowHint);
308 if (!data->icon_window) create_icon_window( display, data );
309 hints->icon_window = data->icon_window;
310 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
320 GetIconInfo(hIcon, &ii);
322 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
325 rcMask.right = bmMask.bmWidth;
326 rcMask.bottom = bmMask.bmHeight;
328 hDC = CreateCompatibleDC(0);
329 hbmOrig = SelectObject(hDC, ii.hbmMask);
330 InvertRect(hDC, &rcMask);
331 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
332 SelectObject(hDC, hbmOrig);
335 data->hWMIconBitmap = ii.hbmColor;
336 data->hWMIconMask = ii.hbmMask;
338 hints->icon_pixmap = X11DRV_get_pixmap(data->hWMIconBitmap);
339 hints->icon_mask = X11DRV_get_pixmap(data->hWMIconMask);
340 destroy_icon_window( display, data );
341 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
346 /***********************************************************************
349 * set the window size hints
351 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
353 XSizeHints* size_hints;
355 if ((size_hints = XAllocSizeHints()))
357 size_hints->win_gravity = StaticGravity;
358 size_hints->x = data->whole_rect.left;
359 size_hints->y = data->whole_rect.top;
360 size_hints->flags = PWinGravity | PPosition;
362 if ( !(style & WS_THICKFRAME) )
364 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
365 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
366 size_hints->min_width = size_hints->max_width;
367 size_hints->min_height = size_hints->max_height;
368 size_hints->flags |= PMinSize | PMaxSize;
370 XSetWMNormalHints( display, data->whole_window, size_hints );
376 /***********************************************************************
379 * get the name of the current process for setting class hints
381 static char *get_process_name(void)
387 WCHAR module[MAX_PATH];
388 DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
389 if (len && len < MAX_PATH)
392 WCHAR *p, *appname = module;
394 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
395 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
396 len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
397 if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
399 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
408 /***********************************************************************
409 * X11DRV_set_wm_hints
411 * Set the window manager hints for a newly-created window
413 void X11DRV_set_wm_hints( Display *display, struct x11drv_win_data *data )
416 XClassHint *class_hints;
422 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
423 DWORD ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
424 HWND owner = GetWindow( data->hwnd, GW_OWNER );
425 char *process_name = get_process_name();
427 /* transient for hint */
430 Window owner_win = X11DRV_get_whole_window( owner );
432 XSetTransientForHint( display, data->whole_window, owner_win );
434 group_leader = owner_win;
436 else group_leader = data->whole_window;
442 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
443 protocols[i++] = x11drv_atom(_NET_WM_PING);
444 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
445 XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
446 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
449 if ((class_hints = XAllocClassHint()))
451 class_hints->res_name = process_name;
452 class_hints->res_class = "Wine";
453 XSetClassHint( display, data->whole_window, class_hints );
454 XFree( class_hints );
458 set_size_hints( display, data, style );
460 /* systray properties (KDE only for now) */
461 if (ex_style & WS_EX_TRAYWINDOW)
464 XChangeProperty( display, data->whole_window, x11drv_atom(KWM_DOCKWINDOW),
465 x11drv_atom(KWM_DOCKWINDOW), 32, PropModeReplace, (unsigned char*)&val, 1 );
466 XChangeProperty( display, data->whole_window, x11drv_atom(_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR),
467 XA_WINDOW, 32, PropModeReplace, (unsigned char*)&data->whole_window, 1 );
470 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
471 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
472 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
474 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
475 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
477 /* map WS_EX_TOOLWINDOW to _NET_WM_WINDOW_TYPE_UTILITY */
478 if (ex_style & WS_EX_TOOLWINDOW)
480 Atom a = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
481 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
482 XA_ATOM, 32, PropModeReplace, (unsigned char*)&a, 1);
485 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
486 mwm_hints.functions = 0;
487 if ((style & WS_CAPTION) == WS_CAPTION) mwm_hints.functions |= MWM_FUNC_MOVE;
488 if (style & WS_THICKFRAME) mwm_hints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
489 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
490 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
491 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
492 if (ex_style & WS_EX_APPWINDOW) mwm_hints.functions |= MWM_FUNC_MOVE;
493 mwm_hints.decorations = 0;
494 if ((style & WS_CAPTION) == WS_CAPTION)
496 mwm_hints.decorations |= MWM_DECOR_TITLE;
497 if (style & WS_SYSMENU) mwm_hints.decorations |= MWM_DECOR_MENU;
498 if (style & WS_MINIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MINIMIZE;
499 if (style & WS_MAXIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MAXIMIZE;
501 if (ex_style & WS_EX_DLGMODALFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
502 else if (style & WS_THICKFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
503 else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
504 else if (style & WS_BORDER) mwm_hints.decorations |= MWM_DECOR_BORDER;
505 else if (!(style & (WS_CHILD|WS_POPUP))) mwm_hints.decorations |= MWM_DECOR_BORDER;
507 XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
508 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
509 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
511 XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
512 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
514 wm_hints = XAllocWMHints();
520 wm_hints->flags = InputHint | StateHint | WindowGroupHint;
521 wm_hints->input = !(style & WS_DISABLED);
523 set_icon_hints( display, data, wm_hints,
524 (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON ) );
526 wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
527 wm_hints->window_group = group_leader;
530 XSetWMHints( display, data->whole_window, wm_hints );
537 /***********************************************************************
538 * X11DRV_set_iconic_state
540 * Set the X11 iconic state according to the window style.
542 void X11DRV_set_iconic_state( HWND hwnd )
544 Display *display = thread_display();
545 struct x11drv_win_data *data;
548 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
549 BOOL iconic = (style & WS_MINIMIZE) != 0;
551 if (!(data = X11DRV_get_win_data( hwnd ))) return;
552 if (!data->whole_window) return;
554 GetWindowRect( hwnd, &rect );
558 if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
559 wm_hints->flags |= StateHint | IconPositionHint;
560 wm_hints->initial_state = iconic ? IconicState : NormalState;
561 wm_hints->icon_x = rect.left;
562 wm_hints->icon_y = rect.top;
563 XSetWMHints( display, data->whole_window, wm_hints );
565 if (style & WS_VISIBLE)
568 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
570 if (X11DRV_is_window_rect_mapped( &rect ))
571 XMapWindow( display, data->whole_window );
579 /***********************************************************************
580 * X11DRV_window_to_X_rect
582 * Convert a rect from client to X window coordinates
584 void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
588 if (!data->managed) return;
589 if (IsRectEmpty( rect )) return;
591 rc.top = rc.bottom = rc.left = rc.right = 0;
593 AdjustWindowRectEx( &rc, GetWindowLongW( data->hwnd, GWL_STYLE ) & ~(WS_HSCROLL|WS_VSCROLL),
594 FALSE, GetWindowLongW( data->hwnd, GWL_EXSTYLE ) );
596 rect->left -= rc.left;
597 rect->right -= rc.right;
599 rect->bottom -= rc.bottom;
600 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
601 if (rect->left >= rect->right) rect->right = rect->left + 1;
605 /***********************************************************************
606 * X11DRV_X_to_window_rect
608 * Opposite of X11DRV_window_to_X_rect
610 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
612 if (!data->managed) return;
613 if (IsRectEmpty( rect )) return;
615 AdjustWindowRectEx( rect, GetWindowLongW( data->hwnd, GWL_STYLE ) & ~(WS_HSCROLL|WS_VSCROLL),
616 FALSE, GetWindowLongW( data->hwnd, GWL_EXSTYLE ));
618 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
619 if (rect->left >= rect->right) rect->right = rect->left + 1;
623 /***********************************************************************
624 * X11DRV_sync_window_position
626 * Synchronize the X window position with the Windows one
628 void X11DRV_sync_window_position( Display *display, struct x11drv_win_data *data,
629 UINT swp_flags, const RECT *new_client_rect,
630 const RECT *new_whole_rect )
632 XWindowChanges changes;
636 old_whole_rect = data->whole_rect;
637 data->whole_rect = *new_whole_rect;
639 data->client_rect = *new_client_rect;
640 OffsetRect( &data->client_rect, -data->whole_rect.left, -data->whole_rect.top );
642 if (!data->whole_window) return;
643 if (swp_flags & SWP_WINE_NOHOSTMOVE) return;
645 mask = get_window_changes( &changes, &old_whole_rect, &data->whole_rect );
647 if (!(swp_flags & SWP_NOZORDER))
649 /* find window that this one must be after */
650 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
651 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
652 prev = GetWindow( prev, GW_HWNDPREV );
653 if (!prev) /* top child */
655 changes.stack_mode = Above;
660 /* should use stack_mode Below but most window managers don't get it right */
661 /* so move it above the next one in Z order */
662 HWND next = GetWindow( data->hwnd, GW_HWNDNEXT );
663 while (next && !(GetWindowLongW( next, GWL_STYLE ) & WS_VISIBLE))
664 next = GetWindow( next, GW_HWNDNEXT );
667 changes.stack_mode = Above;
668 changes.sibling = X11DRV_get_whole_window(next);
669 mask |= CWStackMode | CWSibling;
676 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
678 TRACE( "setting win %lx pos %ld,%ld,%ldx%ld after %lx changes=%x\n",
679 data->whole_window, data->whole_rect.left, data->whole_rect.top,
680 data->whole_rect.right - data->whole_rect.left,
681 data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
684 XSync( gdi_display, False ); /* flush graphics operations before moving the window */
685 if (mask & (CWWidth|CWHeight)) set_size_hints( display, data, style );
686 XReconfigureWMWindow( display, data->whole_window,
687 DefaultScreen(display), mask, &changes );
693 /**********************************************************************
696 static void create_desktop( Display *display, struct x11drv_win_data *data )
701 winContext = XUniqueContext();
702 XInternAtoms( display, (char **)atom_names, NB_XATOMS - FIRST_XATOM, False, X11DRV_Atoms );
703 visualid = XVisualIDFromVisual(visual);
706 whole_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_whole_window" ));
707 icon_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_icon_window" ));
708 managed_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_managed" ));
710 data->whole_window = root_window;
711 data->whole_rect = data->client_rect = data->window_rect;
713 SetPropA( data->hwnd, whole_window_atom, (HANDLE)root_window );
714 SetPropA( data->hwnd, "__wine_x11_visual_id", (HANDLE)visualid );
716 X11DRV_InitClipboard();
718 if (root_window != DefaultRootWindow(display)) X11DRV_create_desktop_thread();
722 /**********************************************************************
723 * create_whole_window
725 * Create the whole X window for a given window
727 static Window create_whole_window( Display *display, struct x11drv_win_data *data, DWORD style )
730 XSetWindowAttributes attr;
734 rect = data->window_rect;
735 X11DRV_window_to_X_rect( data, &rect );
737 if (!(cx = rect.right - rect.left)) cx = 1;
738 if (!(cy = rect.bottom - rect.top)) cy = 1;
740 mask = get_window_attributes( data, &attr );
742 /* set the attributes that don't change over the lifetime of the window */
743 attr.bit_gravity = NorthWestGravity;
744 attr.win_gravity = StaticGravity;
745 attr.backing_store = NotUseful;
746 mask |= CWBitGravity | CWWinGravity | CWBackingStore;
750 data->whole_rect = rect;
751 data->whole_window = XCreateWindow( display, root_window, rect.left, rect.top, cx, cy,
752 0, screen_depth, InputOutput, visual,
755 if (!data->whole_window)
760 XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
762 /* non-maximized child must be at bottom of Z order */
763 if ((style & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
765 XWindowChanges changes;
766 changes.stack_mode = Below;
767 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
769 XSync( display, False ); /* FIXME: should not be needed */
773 xim = x11drv_thread_data()->xim;
774 if (xim) data->xic = X11DRV_CreateIC( xim, display, data->whole_window );
776 X11DRV_set_wm_hints( display, data );
778 SetPropA( data->hwnd, whole_window_atom, (HANDLE)data->whole_window );
779 return data->whole_window;
783 /**********************************************************************
784 * destroy_whole_window
786 * Destroy the whole X window for a given window.
788 static void destroy_whole_window( Display *display, struct x11drv_win_data *data )
790 struct x11drv_thread_data *thread_data = x11drv_thread_data();
792 if (!data->whole_window) return;
794 TRACE( "win %p xwin %lx\n", data->hwnd, data->whole_window );
795 if (thread_data->cursor_window == data->whole_window) thread_data->cursor_window = None;
797 XSync( gdi_display, False ); /* flush any reference to this drawable in GDI queue */
798 XDeleteContext( display, data->whole_window, winContext );
799 XDestroyWindow( display, data->whole_window ); /* this destroys client too */
800 data->whole_window = 0;
803 XUnsetICFocus( data->xic );
804 XDestroyIC( data->xic );
807 RemovePropA( data->hwnd, whole_window_atom );
811 /*****************************************************************
812 * SetWindowText (X11DRV.@)
814 BOOL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
816 Display *display = thread_display();
823 if ((win = X11DRV_get_whole_window( hwnd )))
825 /* allocate new buffer for window text */
826 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
827 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count )))
829 ERR("Not enough memory for window text\n");
832 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
834 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
835 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
837 ERR("Not enough memory for window text in UTF-8\n");
838 HeapFree( GetProcessHeap(), 0, buffer );
841 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
844 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
846 XSetWMName( display, win, &prop );
847 XSetWMIconName( display, win, &prop );
851 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
852 according to the standard
853 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
855 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
856 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
859 HeapFree( GetProcessHeap(), 0, utf8_buffer );
860 HeapFree( GetProcessHeap(), 0, buffer );
866 /***********************************************************************
867 * DestroyWindow (X11DRV.@)
869 BOOL X11DRV_DestroyWindow( HWND hwnd )
871 struct x11drv_thread_data *thread_data = x11drv_thread_data();
872 Display *display = thread_data->display;
873 struct x11drv_win_data *data;
875 if (!(data = X11DRV_get_win_data( hwnd ))) return TRUE;
877 free_window_dce( data );
878 destroy_whole_window( display, data );
879 destroy_icon_window( display, data );
881 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
882 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
883 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
885 XDeleteContext( display, (XID)hwnd, win_data_context );
887 HeapFree( GetProcessHeap(), 0, data );
893 /**********************************************************************
894 * CreateWindow (X11DRV.@)
896 BOOL X11DRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
898 Display *display = thread_display();
900 struct x11drv_win_data *data;
901 HWND parent, insert_after;
910 ERR( "invalid window width %d\n", cs->cx );
915 ERR( "invalid window height %d\n", cs->cy );
920 ERR( "invalid window width %d\n", cs->cx );
925 ERR( "invalid window height %d\n", cs->cy );
929 if (!(data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data)))) return FALSE;
931 data->whole_window = 0;
932 data->icon_window = 0;
934 data->managed = FALSE;
936 data->hWMIconBitmap = 0;
937 data->hWMIconMask = 0;
940 if (!win_data_context) win_data_context = XUniqueContext();
941 XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
944 /* initialize the dimensions before sending WM_GETMINMAXINFO */
945 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
946 X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER, NULL );
948 parent = GetAncestor( hwnd, GA_PARENT );
951 create_desktop( display, data );
955 /* create an X window if it's a top level window */
956 if (parent == GetDesktopWindow())
958 if (!create_whole_window( display, data, cs->style )) goto failed;
961 /* get class or window DC if needed */
962 alloc_window_dce( data );
964 /* Call the WH_CBT hook */
966 /* the window style passed to the hook must be the real window style,
967 * rather than just the window style that the caller to CreateWindowEx
968 * passed in, so we have to copy the original CREATESTRUCT and get the
971 cbcs.style = GetWindowLongW(hwnd, GWL_STYLE);
974 cbtc.hwndInsertAfter = HWND_TOP;
975 if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode ))
977 TRACE("CBT-hook returned !0\n");
981 /* Send the WM_GETMINMAXINFO message and fix the size if needed */
982 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
984 POINT maxSize, maxPos, minTrack, maxTrack;
986 WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
987 if (maxSize.x < cs->cx) cs->cx = maxSize.x;
988 if (maxSize.y < cs->cy) cs->cy = maxSize.y;
989 if (cs->cx < 0) cs->cx = 0;
990 if (cs->cy < 0) cs->cy = 0;
992 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
993 if (!X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER, NULL )) return FALSE;
996 /* send WM_NCCREATE */
997 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cs->cx, cs->cy );
999 ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1001 ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1004 WARN("aborted by WM_xxCREATE!\n");
1008 /* make sure the window is still valid */
1009 if (!(data = X11DRV_get_win_data( hwnd ))) return FALSE;
1010 if (data->whole_window) X11DRV_sync_window_style( display, data );
1012 /* send WM_NCCALCSIZE */
1013 rect = data->window_rect;
1014 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&rect );
1016 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
1018 /* yes, even if the CBT hook was called with HWND_TOP */
1019 insert_after = ((wndPtr->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
1021 X11DRV_set_window_pos( hwnd, insert_after, &wndPtr->rectWindow, &rect, 0, NULL );
1023 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",
1024 hwnd, wndPtr->rectWindow.left, wndPtr->rectWindow.top,
1025 wndPtr->rectWindow.right, wndPtr->rectWindow.bottom,
1026 wndPtr->rectClient.left, wndPtr->rectClient.top,
1027 wndPtr->rectClient.right, wndPtr->rectClient.bottom,
1028 data->whole_rect.left, data->whole_rect.top,
1029 data->whole_rect.right, data->whole_rect.bottom,
1030 data->client_rect.left, data->client_rect.top,
1031 data->client_rect.right, data->client_rect.bottom,
1032 (unsigned int)data->whole_window );
1034 WIN_ReleasePtr( wndPtr );
1037 ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1039 ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1041 if (!ret) return FALSE;
1043 NotifyWinEvent(EVENT_OBJECT_CREATE, hwnd, OBJID_WINDOW, 0);
1045 /* Send the size messages */
1047 if (!(wndPtr = WIN_GetPtr(hwnd)) || wndPtr == WND_OTHER_PROCESS) return FALSE;
1048 if (!(wndPtr->flags & WIN_NEED_SIZE))
1050 RECT rect = wndPtr->rectClient;
1051 WIN_ReleasePtr( wndPtr );
1052 /* send it anyway */
1053 if (((rect.right-rect.left) <0) ||((rect.bottom-rect.top)<0))
1054 WARN("sending bogus WM_SIZE message 0x%08lx\n",
1055 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1056 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1057 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1058 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( rect.left, rect.top ) );
1060 else WIN_ReleasePtr( wndPtr );
1062 /* Show the window, maximizing or minimizing if needed */
1064 style = GetWindowLongW( hwnd, GWL_STYLE );
1065 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1067 extern UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect ); /*FIXME*/
1070 UINT swFlag = (style & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1071 WIN_SetStyle( hwnd, 0, WS_MAXIMIZE | WS_MINIMIZE );
1072 WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1073 swFlag = ((style & WS_CHILD) || GetActiveWindow())
1074 ? SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED
1075 : SWP_NOZORDER | SWP_FRAMECHANGED;
1076 SetWindowPos( hwnd, 0, newPos.left, newPos.top,
1077 newPos.right, newPos.bottom, swFlag );
1083 X11DRV_DestroyWindow( hwnd );
1088 /***********************************************************************
1089 * X11DRV_get_win_data
1091 * Return the X11 data structure associated with a window.
1093 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1097 if (!hwnd || XFindContext( thread_display(), (XID)hwnd, win_data_context, &data )) data = NULL;
1098 return (struct x11drv_win_data *)data;
1102 /***********************************************************************
1103 * X11DRV_get_whole_window
1105 * Return the X window associated with the full area of a window
1107 Window X11DRV_get_whole_window( HWND hwnd )
1109 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1111 if (!data) return (Window)GetPropA( hwnd, whole_window_atom );
1112 return data->whole_window;
1116 /***********************************************************************
1119 * Return the X input context associated with a window
1121 XIC X11DRV_get_ic( HWND hwnd )
1123 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1125 if (!data) return 0;
1130 /*****************************************************************
1131 * SetParent (X11DRV.@)
1133 HWND X11DRV_SetParent( HWND hwnd, HWND parent )
1135 Display *display = thread_display();
1138 HWND old_parent = 0;
1140 /* Windows hides the window first, then shows it again
1141 * including the WM_SHOWWINDOW messages and all */
1142 BOOL was_visible = ShowWindow( hwnd, SW_HIDE );
1144 wndPtr = WIN_GetPtr( hwnd );
1145 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return 0;
1147 SERVER_START_REQ( set_parent )
1150 req->parent = parent;
1151 if ((ret = !wine_server_call( req )))
1153 old_parent = reply->old_parent;
1154 wndPtr->parent = parent = reply->full_parent;
1159 WIN_ReleasePtr( wndPtr );
1162 if (parent != old_parent)
1164 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1166 if (!data) return 0;
1168 if (parent != GetDesktopWindow()) /* a child window */
1170 if (old_parent == GetDesktopWindow())
1172 /* destroy the old X windows */
1173 destroy_whole_window( display, data );
1174 destroy_icon_window( display, data );
1177 data->managed = FALSE;
1178 RemovePropA( data->hwnd, managed_atom );
1182 else /* new top level window */
1184 /* FIXME: we ignore errors since we can't really recover anyway */
1185 create_whole_window( display, data, GetWindowLongW( hwnd, GWL_STYLE ) );
1189 /* SetParent additionally needs to make hwnd the topmost window
1190 in the x-order and send the expected WM_WINDOWPOSCHANGING and
1191 WM_WINDOWPOSCHANGED notification messages.
1193 SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0,
1194 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | (was_visible ? SWP_SHOWWINDOW : 0) );
1195 /* FIXME: a WM_MOVE is also generated (in the DefWindowProc handler
1196 * for WM_WINDOWPOSCHANGED) in Windows, should probably remove SWP_NOMOVE */
1202 /*****************************************************************
1203 * SetFocus (X11DRV.@)
1206 * Explicit colormap management seems to work only with OLVWM.
1208 void X11DRV_SetFocus( HWND hwnd )
1210 Display *display = thread_display();
1211 struct x11drv_win_data *data;
1212 XWindowAttributes win_attr;
1214 /* Only mess with the X focus if there's */
1215 /* no desktop window and if the window is not managed by the WM. */
1216 if (root_window != DefaultRootWindow(display)) return;
1218 if (!hwnd) /* If setting the focus to 0, uninstall the colormap */
1221 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1222 XUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1223 wine_tsx11_unlock();
1227 hwnd = GetAncestor( hwnd, GA_ROOT );
1229 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1230 if (!data->managed || !data->whole_window) return;
1232 /* Set X focus and install colormap */
1234 if (XGetWindowAttributes( display, data->whole_window, &win_attr ) &&
1235 (win_attr.map_state == IsViewable))
1237 /* If window is not viewable, don't change anything */
1239 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1240 /* FIXME: this is not entirely correct */
1241 XSetInputFocus( display, data->whole_window, RevertToParent,
1243 GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1244 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1245 XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1247 wine_tsx11_unlock();
1251 /**********************************************************************
1252 * SetWindowIcon (X11DRV.@)
1254 * hIcon or hIconSm has changed (or is being initialised for the
1255 * first time). Complete the X11 driver-specific initialisation
1256 * and set the window hints.
1258 * This is not entirely correct, may need to create
1259 * an icon window and set the pixmap as a background
1261 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
1263 Display *display = thread_display();
1264 struct x11drv_win_data *data;
1267 if (type != ICON_BIG) return; /* nothing to do here */
1269 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1270 if (!data->whole_window) return;
1271 if (!data->managed) return;
1274 if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
1275 wine_tsx11_unlock();
1278 set_icon_hints( display, data, wm_hints, icon );
1280 XSetWMHints( display, data->whole_window, wm_hints );
1282 wine_tsx11_unlock();