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"
42 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
50 extern Pixmap X11DRV_BITMAP_Pixmap( HBITMAP );
52 #define HAS_DLGFRAME(style,exStyle) \
53 (((exStyle) & WS_EX_DLGMODALFRAME) || \
54 (((style) & WS_DLGFRAME) && !((style) & WS_THICKFRAME)))
56 /* X context to associate a hwnd to an X window */
57 XContext winContext = 0;
59 Atom X11DRV_Atoms[NB_XATOMS - FIRST_XATOM];
61 static const char * const atom_names[NB_XATOMS - FIRST_XATOM] =
80 "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR",
84 "_NET_WM_WINDOW_TYPE",
85 "_NET_WM_WINDOW_TYPE_UTILITY",
108 static LPCSTR whole_window_atom;
109 static LPCSTR client_window_atom;
110 static LPCSTR icon_window_atom;
112 /***********************************************************************
115 * Check if a given window should be managed
117 inline static BOOL is_window_managed( WND *win )
119 if (!managed_mode) return FALSE;
120 /* tray window is always managed */
121 if (win->dwExStyle & WS_EX_TRAYWINDOW) return TRUE;
122 /* child windows are not managed */
123 if (win->dwStyle & WS_CHILD) return FALSE;
124 /* windows with caption are managed */
125 if ((win->dwStyle & WS_CAPTION) == WS_CAPTION) return TRUE;
126 /* tool windows are not managed */
127 if (win->dwExStyle & WS_EX_TOOLWINDOW) return FALSE;
128 /* windows with thick frame are managed */
129 if (win->dwStyle & WS_THICKFRAME) return TRUE;
130 /* application windows are managed */
131 if (win->dwExStyle & WS_EX_APPWINDOW) return TRUE;
132 /* popup windows that aren't owned are managed */
133 if ((win->dwStyle & WS_POPUP) && !win->owner) return TRUE;
134 /* default: not managed */
139 /***********************************************************************
140 * is_client_window_mapped
142 * Check if the X client window should be mapped
144 inline static BOOL is_client_window_mapped( WND *win )
146 struct x11drv_win_data *data = win->pDriverData;
147 return !(win->dwStyle & WS_MINIMIZE) && !IsRectEmpty( &data->client_rect );
151 /***********************************************************************
152 * get_window_attributes
154 * Fill the window attributes structure for an X window.
156 static int get_window_attributes( Display *display, WND *win, XSetWindowAttributes *attr )
158 BOOL is_top_level = is_window_top_level( win );
159 BOOL managed = is_top_level && is_window_managed( win );
161 if (managed) WIN_SetExStyle( win->hwndSelf, win->dwExStyle | WS_EX_MANAGED );
162 else WIN_SetExStyle( win->hwndSelf, win->dwExStyle & ~WS_EX_MANAGED );
164 attr->override_redirect = !managed;
165 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
166 attr->save_under = ((win->clsStyle & CS_SAVEBITS) != 0);
167 attr->cursor = x11drv_thread_data()->cursor;
168 attr->event_mask = (ExposureMask | PointerMotionMask |
169 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
171 if (is_window_top_level( win ))
172 attr->event_mask |= (KeyPressMask | KeyReleaseMask | StructureNotifyMask |
173 FocusChangeMask | KeymapStateMask);
175 return (CWOverrideRedirect | CWSaveUnder | CWEventMask | CWColormap | CWCursor);
179 /***********************************************************************
180 * X11DRV_sync_window_style
182 * Change the X window attributes when the window style has changed.
184 void X11DRV_sync_window_style( Display *display, WND *win )
186 XSetWindowAttributes attr;
190 mask = get_window_attributes( display, win, &attr );
191 XChangeWindowAttributes( display, get_whole_window(win), mask, &attr );
196 /***********************************************************************
199 * fill the window changes structure
201 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
205 if (old->right - old->left != new->right - new->left )
207 if (!(changes->width = new->right - new->left)) changes->width = 1;
210 if (old->bottom - old->top != new->bottom - new->top)
212 if (!(changes->height = new->bottom - new->top)) changes->height = 1;
215 if (old->left != new->left)
217 changes->x = new->left;
220 if (old->top != new->top)
222 changes->y = new->top;
229 /***********************************************************************
232 static Window create_icon_window( Display *display, WND *win )
234 struct x11drv_win_data *data = win->pDriverData;
235 XSetWindowAttributes attr;
237 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
238 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
239 attr.bit_gravity = NorthWestGravity;
240 attr.backing_store = NotUseful/*WhenMapped*/;
241 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
244 data->icon_window = XCreateWindow( display, root_window, 0, 0,
245 GetSystemMetrics( SM_CXICON ),
246 GetSystemMetrics( SM_CYICON ),
249 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
250 XSaveContext( display, data->icon_window, winContext, (char *)win->hwndSelf );
253 TRACE( "created %lx\n", data->icon_window );
254 SetPropA( win->hwndSelf, icon_window_atom, (HANDLE)data->icon_window );
255 return data->icon_window;
260 /***********************************************************************
261 * destroy_icon_window
263 inline static void destroy_icon_window( Display *display, WND *win )
265 struct x11drv_win_data *data = win->pDriverData;
267 if (!data->icon_window) return;
268 if (x11drv_thread_data()->cursor_window == data->icon_window)
269 x11drv_thread_data()->cursor_window = None;
271 XDeleteContext( display, data->icon_window, winContext );
272 XDestroyWindow( display, data->icon_window );
273 data->icon_window = 0;
275 RemovePropA( win->hwndSelf, icon_window_atom );
279 /***********************************************************************
282 * Set the icon wm hints
284 static void set_icon_hints( Display *display, WND *wndPtr, XWMHints *hints, HICON hIcon )
286 X11DRV_WND_DATA *data = wndPtr->pDriverData;
288 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
289 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
290 data->hWMIconBitmap = 0;
291 data->hWMIconMask = 0;
293 if (!(wndPtr->dwExStyle & WS_EX_MANAGED))
295 destroy_icon_window( display, wndPtr );
296 hints->flags &= ~(IconPixmapHint | IconMaskHint | IconWindowHint);
300 if (!data->icon_window) create_icon_window( display, wndPtr );
301 hints->icon_window = data->icon_window;
302 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
312 GetIconInfo(hIcon, &ii);
314 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
317 rcMask.right = bmMask.bmWidth;
318 rcMask.bottom = bmMask.bmHeight;
320 hDC = CreateCompatibleDC(0);
321 hbmOrig = SelectObject(hDC, ii.hbmMask);
322 InvertRect(hDC, &rcMask);
323 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
324 SelectObject(hDC, hbmOrig);
327 data->hWMIconBitmap = ii.hbmColor;
328 data->hWMIconMask = ii.hbmMask;
330 hints->icon_pixmap = X11DRV_BITMAP_Pixmap(data->hWMIconBitmap);
331 hints->icon_mask = X11DRV_BITMAP_Pixmap(data->hWMIconMask);
332 destroy_icon_window( display, wndPtr );
333 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
338 /***********************************************************************
341 * set the window size hints
343 static void set_size_hints( Display *display, WND *win )
345 XSizeHints* size_hints;
346 struct x11drv_win_data *data = win->pDriverData;
348 if ((size_hints = XAllocSizeHints()))
350 size_hints->win_gravity = StaticGravity;
351 size_hints->x = data->whole_rect.left;
352 size_hints->y = data->whole_rect.top;
353 size_hints->flags = PWinGravity | PPosition;
355 if (HAS_DLGFRAME( win->dwStyle, win->dwExStyle ))
357 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
358 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
359 size_hints->min_width = size_hints->max_width;
360 size_hints->min_height = size_hints->max_height;
361 size_hints->flags |= PMinSize | PMaxSize;
363 XSetWMNormalHints( display, data->whole_window, size_hints );
369 /***********************************************************************
370 * X11DRV_set_wm_hints
372 * Set the window manager hints for a newly-created window
374 void X11DRV_set_wm_hints( Display *display, WND *win )
376 struct x11drv_win_data *data = win->pDriverData;
378 XClassHint *class_hints;
389 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
390 protocols[i++] = x11drv_atom(_NET_WM_PING);
391 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
392 XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
393 XA_ATOM, 32, PropModeReplace, (char *)protocols, i );
396 if ((class_hints = XAllocClassHint()))
398 class_hints->res_name = "wine";
399 class_hints->res_class = "Wine";
400 XSetClassHint( display, data->whole_window, class_hints );
401 XFree( class_hints );
404 /* transient for hint */
407 Window owner_win = X11DRV_get_whole_window( win->owner );
408 XSetTransientForHint( display, data->whole_window, owner_win );
409 group_leader = owner_win;
411 else group_leader = data->whole_window;
414 set_size_hints( display, win );
416 /* systray properties (KDE only for now) */
417 if (win->dwExStyle & WS_EX_TRAYWINDOW)
420 XChangeProperty( display, data->whole_window, x11drv_atom(KWM_DOCKWINDOW),
421 x11drv_atom(KWM_DOCKWINDOW), 32, PropModeReplace, (char*)&val, 1 );
422 XChangeProperty( display, data->whole_window, x11drv_atom(_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR),
423 XA_WINDOW, 32, PropModeReplace, (char*)&data->whole_window, 1 );
426 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
427 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
428 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
430 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
431 XA_CARDINAL, 32, PropModeReplace, (char *)&i, 1);
433 /* map WS_EX_TOOLWINDOW to _NET_WM_WINDOW_TYPE_UTILITY */
434 if (win->dwExStyle & WS_EX_TOOLWINDOW)
436 Atom a = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
437 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
438 XA_ATOM, 32, PropModeReplace, (char*)&a, 1);
441 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
442 mwm_hints.functions = 0;
443 if ((win->dwStyle & WS_CAPTION) == WS_CAPTION) mwm_hints.functions |= MWM_FUNC_MOVE;
444 if (win->dwStyle & WS_THICKFRAME) mwm_hints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
445 if (win->dwStyle & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
446 if (win->dwStyle & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
447 if (win->dwStyle & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
448 mwm_hints.decorations = 0;
449 if ((win->dwStyle & WS_CAPTION) == WS_CAPTION) mwm_hints.decorations |= MWM_DECOR_TITLE;
450 if (win->dwExStyle & WS_EX_DLGMODALFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
451 else if (win->dwStyle & WS_THICKFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
452 else if ((win->dwStyle & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
453 else if (win->dwStyle & WS_BORDER) mwm_hints.decorations |= MWM_DECOR_BORDER;
454 else if (!(win->dwStyle & (WS_CHILD|WS_POPUP))) mwm_hints.decorations |= MWM_DECOR_BORDER;
455 if (win->dwStyle & WS_SYSMENU) mwm_hints.decorations |= MWM_DECOR_MENU;
456 if (win->dwStyle & WS_MINIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MINIMIZE;
457 if (win->dwStyle & WS_MAXIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MAXIMIZE;
459 XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
460 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
461 (char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
463 XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
464 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
466 wm_hints = XAllocWMHints();
472 wm_hints->flags = InputHint | StateHint | WindowGroupHint;
473 wm_hints->input = !(win->dwStyle & WS_DISABLED);
475 set_icon_hints( display, win, wm_hints, (HICON)GetClassLongA( win->hwndSelf, GCL_HICON ));
477 wm_hints->initial_state = (win->dwStyle & WS_MINIMIZE) ? IconicState : NormalState;
478 wm_hints->window_group = group_leader;
481 XSetWMHints( display, data->whole_window, wm_hints );
488 /***********************************************************************
489 * X11DRV_set_iconic_state
491 * Set the X11 iconic state according to the window style.
493 void X11DRV_set_iconic_state( WND *win )
495 Display *display = thread_display();
496 struct x11drv_win_data *data = win->pDriverData;
498 BOOL iconic = IsIconic( win->hwndSelf );
502 if (iconic) XUnmapWindow( display, data->client_window );
503 else if (is_client_window_mapped( win )) XMapWindow( display, data->client_window );
505 if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
506 wm_hints->flags |= StateHint | IconPositionHint;
507 wm_hints->initial_state = iconic ? IconicState : NormalState;
508 wm_hints->icon_x = win->rectWindow.left;
509 wm_hints->icon_y = win->rectWindow.top;
510 XSetWMHints( display, data->whole_window, wm_hints );
512 if (win->dwStyle & WS_VISIBLE)
515 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
517 if (!IsRectEmpty( &win->rectWindow )) XMapWindow( display, data->whole_window );
525 /***********************************************************************
526 * X11DRV_window_to_X_rect
528 * Convert a rect from client to X window coordinates
530 void X11DRV_window_to_X_rect( WND *win, RECT *rect )
534 if (!(win->dwExStyle & WS_EX_MANAGED)) return;
535 if (IsRectEmpty( rect )) return;
537 rc.top = rc.bottom = rc.left = rc.right = 0;
539 AdjustWindowRectEx( &rc, win->dwStyle & ~(WS_HSCROLL|WS_VSCROLL), FALSE, win->dwExStyle );
541 rect->left -= rc.left;
542 rect->right -= rc.right;
544 rect->bottom -= rc.bottom;
545 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
546 if (rect->left >= rect->right) rect->right = rect->left + 1;
550 /***********************************************************************
551 * X11DRV_X_to_window_rect
553 * Opposite of X11DRV_window_to_X_rect
555 void X11DRV_X_to_window_rect( WND *win, RECT *rect )
557 if (!(win->dwExStyle & WS_EX_MANAGED)) return;
558 if (IsRectEmpty( rect )) return;
560 AdjustWindowRectEx( rect, win->dwStyle & ~(WS_HSCROLL|WS_VSCROLL), FALSE, win->dwExStyle );
562 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
563 if (rect->left >= rect->right) rect->right = rect->left + 1;
567 /***********************************************************************
568 * X11DRV_sync_whole_window_position
570 * Synchronize the X whole window position with the Windows one
572 int X11DRV_sync_whole_window_position( Display *display, WND *win, int zorder )
574 XWindowChanges changes;
576 struct x11drv_win_data *data = win->pDriverData;
577 RECT whole_rect = win->rectWindow;
579 X11DRV_window_to_X_rect( win, &whole_rect );
580 mask = get_window_changes( &changes, &data->whole_rect, &whole_rect );
584 if (is_window_top_level( win ))
586 /* find window that this one must be after */
587 HWND prev = GetWindow( win->hwndSelf, GW_HWNDPREV );
588 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
589 prev = GetWindow( prev, GW_HWNDPREV );
590 if (!prev) /* top child */
592 changes.stack_mode = Above;
597 /* should use stack_mode Below but most window managers don't get it right */
598 /* so move it above the next one in Z order */
599 HWND next = GetWindow( win->hwndSelf, GW_HWNDNEXT );
600 while (next && !(GetWindowLongW( next, GWL_STYLE ) & WS_VISIBLE))
601 next = GetWindow( next, GW_HWNDNEXT );
604 changes.stack_mode = Above;
605 changes.sibling = X11DRV_get_whole_window(next);
606 mask |= CWStackMode | CWSibling;
612 HWND next = GetWindow( win->hwndSelf, GW_HWNDNEXT );
614 if (win->parent == GetDesktopWindow() &&
615 root_window != DefaultRootWindow(display))
617 /* in desktop mode we need the sibling to belong to the same process */
620 WND *ptr = WIN_GetPtr( next );
621 if (ptr != WND_OTHER_PROCESS)
623 WIN_ReleasePtr( ptr );
626 next = GetWindow( next, GW_HWNDNEXT );
630 if (!next) /* bottom child */
632 changes.stack_mode = Below;
637 changes.stack_mode = Above;
638 changes.sibling = X11DRV_get_whole_window(next);
639 mask |= CWStackMode | CWSibling;
644 data->whole_rect = whole_rect;
648 TRACE( "setting win %lx pos %ld,%ld,%ldx%ld after %lx changes=%x\n",
649 data->whole_window, whole_rect.left, whole_rect.top,
650 whole_rect.right - whole_rect.left, whole_rect.bottom - whole_rect.top,
651 changes.sibling, mask );
653 XSync( gdi_display, False ); /* flush graphics operations before moving the window */
654 if (is_window_top_level( win ))
656 if (mask & (CWWidth|CWHeight)) set_size_hints( display, win );
657 XReconfigureWMWindow( display, data->whole_window,
658 DefaultScreen(display), mask, &changes );
660 else XConfigureWindow( display, data->whole_window, mask, &changes );
667 /***********************************************************************
668 * X11DRV_sync_client_window_position
670 * Synchronize the X client window position with the Windows one
672 int X11DRV_sync_client_window_position( Display *display, WND *win )
674 XWindowChanges changes;
676 struct x11drv_win_data *data = win->pDriverData;
677 RECT client_rect = win->rectClient;
679 OffsetRect( &client_rect, -data->whole_rect.left, -data->whole_rect.top );
681 if ((mask = get_window_changes( &changes, &data->client_rect, &client_rect )))
683 BOOL was_mapped = is_client_window_mapped( win );
685 TRACE( "setting win %lx pos %ld,%ld,%ldx%ld (was %ld,%ld,%ldx%ld) after %lx changes=%x\n",
686 data->client_window, client_rect.left, client_rect.top,
687 client_rect.right - client_rect.left, client_rect.bottom - client_rect.top,
688 data->client_rect.left, data->client_rect.top,
689 data->client_rect.right - data->client_rect.left,
690 data->client_rect.bottom - data->client_rect.top,
691 changes.sibling, mask );
692 data->client_rect = client_rect;
694 XSync( gdi_display, False ); /* flush graphics operations before moving the window */
695 if (was_mapped && !is_client_window_mapped( win ))
696 XUnmapWindow( display, data->client_window );
697 XConfigureWindow( display, data->client_window, mask, &changes );
698 if (!was_mapped && is_client_window_mapped( win ))
699 XMapWindow( display, data->client_window );
706 /***********************************************************************
707 * X11DRV_register_window
709 * Associate an X window to a HWND.
711 void X11DRV_register_window( Display *display, HWND hwnd, struct x11drv_win_data *data )
714 XSaveContext( display, data->whole_window, winContext, (char *)hwnd );
715 XSaveContext( display, data->client_window, winContext, (char *)hwnd );
720 /**********************************************************************
723 static void create_desktop( Display *display, WND *wndPtr )
725 X11DRV_WND_DATA *data = wndPtr->pDriverData;
728 winContext = XUniqueContext();
729 XInternAtoms( display, (char **)atom_names, NB_XATOMS - FIRST_XATOM, False, X11DRV_Atoms );
732 whole_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_whole_window" ));
733 client_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_client_window" ));
734 icon_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_icon_window" ));
736 data->whole_window = data->client_window = root_window;
737 data->whole_rect = data->client_rect = wndPtr->rectWindow;
739 SetPropA( wndPtr->hwndSelf, whole_window_atom, (HANDLE)root_window );
740 SetPropA( wndPtr->hwndSelf, client_window_atom, (HANDLE)root_window );
741 SetPropA( wndPtr->hwndSelf, "__wine_x11_visual_id", (HANDLE)XVisualIDFromVisual(visual) );
743 X11DRV_InitClipboard();
745 if (root_window != DefaultRootWindow(display)) X11DRV_create_desktop_thread();
749 /**********************************************************************
750 * create_whole_window
752 * Create the whole X window for a given window
754 static Window create_whole_window( Display *display, WND *win )
756 struct x11drv_win_data *data = win->pDriverData;
758 XSetWindowAttributes attr;
761 BOOL is_top_level = is_window_top_level( win );
763 rect = win->rectWindow;
764 X11DRV_window_to_X_rect( win, &rect );
766 if (!(cx = rect.right - rect.left)) cx = 1;
767 if (!(cy = rect.bottom - rect.top)) cy = 1;
769 parent = X11DRV_get_client_window( win->parent );
773 mask = get_window_attributes( display, win, &attr );
775 /* set the attributes that don't change over the lifetime of the window */
776 attr.bit_gravity = ForgetGravity;
777 attr.win_gravity = NorthWestGravity;
778 attr.backing_store = NotUseful/*WhenMapped*/;
779 mask |= CWBitGravity | CWWinGravity | CWBackingStore;
781 data->whole_rect = rect;
782 data->whole_window = XCreateWindow( display, parent, rect.left, rect.top, cx, cy,
783 0, screen_depth, InputOutput, visual,
786 if (!data->whole_window)
792 /* non-maximized child must be at bottom of Z order */
793 if ((win->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
795 XWindowChanges changes;
796 changes.stack_mode = Below;
797 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
804 XIM xim = x11drv_thread_data()->xim;
805 if (xim) data->xic = X11DRV_CreateIC( xim, display, data->whole_window );
806 X11DRV_set_wm_hints( display, win );
809 return data->whole_window;
813 /**********************************************************************
814 * create_client_window
816 * Create the client window for a given window
818 static Window create_client_window( Display *display, WND *win )
820 struct x11drv_win_data *data = win->pDriverData;
821 RECT rect = data->whole_rect;
822 XSetWindowAttributes attr;
824 OffsetRect( &rect, -data->whole_rect.left, -data->whole_rect.top );
825 data->client_rect = rect;
827 attr.event_mask = (ExposureMask | PointerMotionMask |
828 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
829 attr.bit_gravity = (win->clsStyle & (CS_VREDRAW | CS_HREDRAW)) ?
830 ForgetGravity : NorthWestGravity;
831 attr.backing_store = NotUseful/*WhenMapped*/;
834 data->client_window = XCreateWindow( display, data->whole_window, 0, 0,
835 max( rect.right - rect.left, 1 ),
836 max( rect.bottom - rect.top, 1 ),
839 CWEventMask | CWBitGravity | CWBackingStore, &attr );
840 if (data->client_window && is_client_window_mapped( win ))
841 XMapWindow( display, data->client_window );
843 return data->client_window;
847 /*****************************************************************
848 * SetWindowText (X11DRV.@)
850 BOOL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
852 Display *display = thread_display();
859 if ((win = X11DRV_get_whole_window( hwnd )))
861 /* allocate new buffer for window text */
862 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
863 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count )))
865 ERR("Not enough memory for window text\n");
868 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
870 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
871 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
873 ERR("Not enough memory for window text in UTF-8\n");
876 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
879 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
881 XSetWMName( display, win, &prop );
882 XSetWMIconName( display, win, &prop );
886 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
887 according to the standard
888 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
890 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
891 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
894 HeapFree( GetProcessHeap(), 0, utf8_buffer );
895 HeapFree( GetProcessHeap(), 0, buffer );
901 /***********************************************************************
902 * DestroyWindow (X11DRV.@)
904 BOOL X11DRV_DestroyWindow( HWND hwnd )
906 struct x11drv_thread_data *thread_data = x11drv_thread_data();
907 Display *display = thread_data->display;
908 WND *wndPtr = WIN_GetPtr( hwnd );
909 X11DRV_WND_DATA *data = wndPtr->pDriverData;
911 if (!data) goto done;
913 if (data->whole_window)
915 TRACE( "win %p xwin %lx/%lx\n", hwnd, data->whole_window, data->client_window );
916 if (thread_data->cursor_window == data->whole_window) thread_data->cursor_window = None;
917 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
919 XSync( gdi_display, False ); /* flush any reference to this drawable in GDI queue */
920 XDeleteContext( display, data->whole_window, winContext );
921 XDeleteContext( display, data->client_window, winContext );
922 XDestroyWindow( display, data->whole_window ); /* this destroys client too */
925 XUnsetICFocus( data->xic );
926 XDestroyIC( data->xic );
928 destroy_icon_window( display, wndPtr );
932 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
933 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
934 HeapFree( GetProcessHeap(), 0, data );
935 wndPtr->pDriverData = NULL;
937 WIN_ReleasePtr( wndPtr );
942 /**********************************************************************
943 * CreateWindow (X11DRV.@)
945 BOOL X11DRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
947 Display *display = thread_display();
949 struct x11drv_win_data *data;
956 ERR( "invalid window width %d\n", cs->cx );
961 ERR( "invalid window height %d\n", cs->cx );
965 if (!(data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data)))) return FALSE;
966 data->whole_window = 0;
967 data->client_window = 0;
968 data->icon_window = 0;
970 data->hWMIconBitmap = 0;
971 data->hWMIconMask = 0;
973 wndPtr = WIN_GetPtr( hwnd );
974 wndPtr->pDriverData = data;
976 /* initialize the dimensions before sending WM_GETMINMAXINFO */
977 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
978 WIN_SetRectangles( hwnd, &rect, &rect );
982 create_desktop( display, wndPtr );
983 WIN_ReleasePtr( wndPtr );
987 if (!create_whole_window( display, wndPtr )) goto failed;
988 if (!create_client_window( display, wndPtr )) goto failed;
990 XSync( display, False );
993 SetPropA( hwnd, whole_window_atom, (HANDLE)data->whole_window );
994 SetPropA( hwnd, client_window_atom, (HANDLE)data->client_window );
996 /* Call the WH_CBT hook */
998 cbtc.hwndInsertAfter = HWND_TOP;
999 if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode ))
1001 TRACE("CBT-hook returned !0\n");
1005 /* Send the WM_GETMINMAXINFO message and fix the size if needed */
1006 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
1008 POINT maxSize, maxPos, minTrack, maxTrack;
1010 WIN_ReleasePtr( wndPtr );
1011 WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
1012 if (maxSize.x < cs->cx) cs->cx = maxSize.x;
1013 if (maxSize.y < cs->cy) cs->cy = maxSize.y;
1014 if (cs->cx < minTrack.x ) cs->cx = minTrack.x;
1015 if (cs->cy < minTrack.y ) cs->cy = minTrack.y;
1016 if (cs->cx < 0) cs->cx = 0;
1017 if (cs->cy < 0) cs->cy = 0;
1019 if (!(wndPtr = WIN_GetPtr( hwnd ))) return FALSE;
1020 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
1021 WIN_SetRectangles( hwnd, &rect, &rect );
1022 X11DRV_sync_whole_window_position( display, wndPtr, 0 );
1024 WIN_ReleasePtr( wndPtr );
1026 /* send WM_NCCREATE */
1027 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cs->cx, cs->cy );
1029 ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1031 ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1034 WARN("aborted by WM_xxCREATE!\n");
1038 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
1040 X11DRV_sync_window_style( display, wndPtr );
1042 /* send WM_NCCALCSIZE */
1043 rect = wndPtr->rectWindow;
1044 WIN_ReleasePtr( wndPtr );
1045 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&rect );
1047 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
1048 if (rect.left > rect.right || rect.top > rect.bottom) rect = wndPtr->rectWindow;
1049 WIN_SetRectangles( hwnd, &wndPtr->rectWindow, &rect );
1050 X11DRV_sync_client_window_position( display, wndPtr );
1051 X11DRV_register_window( display, hwnd, data );
1053 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/%x\n",
1054 hwnd, wndPtr->rectWindow.left, wndPtr->rectWindow.top,
1055 wndPtr->rectWindow.right, wndPtr->rectWindow.bottom,
1056 wndPtr->rectClient.left, wndPtr->rectClient.top,
1057 wndPtr->rectClient.right, wndPtr->rectClient.bottom,
1058 data->whole_rect.left, data->whole_rect.top,
1059 data->whole_rect.right, data->whole_rect.bottom,
1060 data->client_rect.left, data->client_rect.top,
1061 data->client_rect.right, data->client_rect.bottom,
1062 (unsigned int)data->whole_window, (unsigned int)data->client_window );
1064 if ((wndPtr->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
1065 WIN_LinkWindow( hwnd, wndPtr->parent, HWND_BOTTOM );
1067 WIN_LinkWindow( hwnd, wndPtr->parent, HWND_TOP );
1069 WIN_ReleasePtr( wndPtr );
1072 ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1074 ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1078 WIN_UnlinkWindow( hwnd );
1082 /* Send the size messages */
1084 if (!(wndPtr = WIN_FindWndPtr(hwnd))) return FALSE;
1085 if (!(wndPtr->flags & WIN_NEED_SIZE))
1087 /* send it anyway */
1088 if (((wndPtr->rectClient.right-wndPtr->rectClient.left) <0)
1089 ||((wndPtr->rectClient.bottom-wndPtr->rectClient.top)<0))
1090 WARN("sending bogus WM_SIZE message 0x%08lx\n",
1091 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
1092 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
1093 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1094 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
1095 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
1096 SendMessageW( hwnd, WM_MOVE, 0,
1097 MAKELONG( wndPtr->rectClient.left, wndPtr->rectClient.top ) );
1100 /* Show the window, maximizing or minimizing if needed */
1102 if (wndPtr->dwStyle & (WS_MINIMIZE | WS_MAXIMIZE))
1104 extern UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect ); /*FIXME*/
1107 UINT swFlag = (wndPtr->dwStyle & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1108 WIN_SetStyle( hwnd, wndPtr->dwStyle & ~(WS_MAXIMIZE | WS_MINIMIZE) );
1109 WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1110 swFlag = ((wndPtr->dwStyle & WS_CHILD) || GetActiveWindow())
1111 ? SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED
1112 : SWP_NOZORDER | SWP_FRAMECHANGED;
1113 SetWindowPos( hwnd, 0, newPos.left, newPos.top,
1114 newPos.right, newPos.bottom, swFlag );
1117 /* if the window was made visible set create struct flag so that
1118 * we do a proper ShowWindow later on */
1119 if (wndPtr->dwStyle & WS_VISIBLE) cs->style |= WS_VISIBLE;
1121 WIN_ReleaseWndPtr( wndPtr );
1126 X11DRV_DestroyWindow( hwnd );
1127 if (wndPtr) WIN_ReleasePtr( wndPtr );
1132 /***********************************************************************
1133 * X11DRV_get_client_window
1135 * Return the X window associated with the client area of a window
1137 Window X11DRV_get_client_window( HWND hwnd )
1140 WND *win = WIN_GetPtr( hwnd );
1142 if (win == WND_OTHER_PROCESS)
1143 return (Window)GetPropA( hwnd, client_window_atom );
1147 struct x11drv_win_data *data = win->pDriverData;
1148 ret = data->client_window;
1149 WIN_ReleasePtr( win );
1155 /***********************************************************************
1156 * X11DRV_get_whole_window
1158 * Return the X window associated with the full area of a window
1160 Window X11DRV_get_whole_window( HWND hwnd )
1163 WND *win = WIN_GetPtr( hwnd );
1165 if (win == WND_OTHER_PROCESS)
1166 return (Window)GetPropA( hwnd, whole_window_atom );
1170 struct x11drv_win_data *data = win->pDriverData;
1171 ret = data->whole_window;
1172 WIN_ReleasePtr( win );
1178 /***********************************************************************
1181 * Return the X input context associated with a window
1183 XIC X11DRV_get_ic( HWND hwnd )
1186 WND *win = WIN_GetPtr( hwnd );
1188 if (win && win != WND_OTHER_PROCESS)
1190 struct x11drv_win_data *data = win->pDriverData;
1192 WIN_ReleasePtr( win );
1198 /*****************************************************************
1199 * SetParent (X11DRV.@)
1201 HWND X11DRV_SetParent( HWND hwnd, HWND parent )
1203 Display *display = thread_display();
1207 /* Windows hides the window first, then shows it again
1208 * including the WM_SHOWWINDOW messages and all */
1209 BOOL was_visible = ShowWindow( hwnd, SW_HIDE );
1211 if (!IsWindow( parent )) return 0;
1212 if (!(wndPtr = WIN_GetPtr(hwnd)) || wndPtr == WND_OTHER_PROCESS) return 0;
1214 retvalue = wndPtr->parent; /* old parent */
1215 if (parent != retvalue)
1217 struct x11drv_win_data *data = wndPtr->pDriverData;
1219 WIN_LinkWindow( hwnd, parent, HWND_TOP );
1221 if (parent != GetDesktopWindow()) /* a child window */
1223 if (!(wndPtr->dwStyle & WS_CHILD))
1225 HMENU menu = (HMENU)SetWindowLongW( hwnd, GWL_ID, 0 );
1226 if (menu) DestroyMenu( menu );
1230 if (is_window_top_level( wndPtr )) X11DRV_set_wm_hints( display, wndPtr );
1232 X11DRV_sync_window_style( display, wndPtr );
1233 XReparentWindow( display, data->whole_window, X11DRV_get_client_window(parent),
1234 data->whole_rect.left, data->whole_rect.top );
1235 wine_tsx11_unlock();
1237 WIN_ReleasePtr( wndPtr );
1239 /* SetParent additionally needs to make hwnd the topmost window
1240 in the x-order and send the expected WM_WINDOWPOSCHANGING and
1241 WM_WINDOWPOSCHANGED notification messages.
1243 SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0,
1244 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | (was_visible ? SWP_SHOWWINDOW : 0) );
1245 /* FIXME: a WM_MOVE is also generated (in the DefWindowProc handler
1246 * for WM_WINDOWPOSCHANGED) in Windows, should probably remove SWP_NOMOVE */
1252 /*****************************************************************
1253 * SetFocus (X11DRV.@)
1256 * Explicit colormap management seems to work only with OLVWM.
1258 void X11DRV_SetFocus( HWND hwnd )
1260 Display *display = thread_display();
1261 XWindowAttributes win_attr;
1264 /* Only mess with the X focus if there's */
1265 /* no desktop window and if the window is not managed by the WM. */
1266 if (root_window != DefaultRootWindow(display)) return;
1268 if (!hwnd) /* If setting the focus to 0, uninstall the colormap */
1271 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1272 XUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1273 wine_tsx11_unlock();
1277 hwnd = GetAncestor( hwnd, GA_ROOT );
1278 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_MANAGED) return;
1279 if (!(win = X11DRV_get_whole_window( hwnd ))) return;
1281 /* Set X focus and install colormap */
1283 if (XGetWindowAttributes( display, win, &win_attr ) &&
1284 (win_attr.map_state == IsViewable))
1286 /* If window is not viewable, don't change anything */
1288 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1289 /* FIXME: this is not entirely correct */
1290 XSetInputFocus( display, win, RevertToParent,
1291 /*CurrentTime*/ GetMessageTime() + X11DRV_server_startticks );
1292 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1293 XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1295 wine_tsx11_unlock();
1299 /**********************************************************************
1300 * SetWindowIcon (X11DRV.@)
1302 * hIcon or hIconSm has changed (or is being initialised for the
1303 * first time). Complete the X11 driver-specific initialisation
1304 * and set the window hints.
1306 * This is not entirely correct, may need to create
1307 * an icon window and set the pixmap as a background
1309 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
1312 Display *display = thread_display();
1314 if (type != ICON_BIG) return; /* nothing to do here */
1316 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return;
1318 if (wndPtr->dwExStyle & WS_EX_MANAGED)
1320 Window win = get_whole_window(wndPtr);
1324 if (!(wm_hints = XGetWMHints( display, win ))) wm_hints = XAllocWMHints();
1325 wine_tsx11_unlock();
1328 set_icon_hints( display, wndPtr, wm_hints, icon );
1330 XSetWMHints( display, win, wm_hints );
1332 wine_tsx11_unlock();
1335 WIN_ReleasePtr( wndPtr );