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 wmProtocols = None;
60 Atom wmDeleteWindow = None;
61 Atom wmTakeFocus = None;
62 Atom dndProtocol = None;
63 Atom dndSelection = None;
64 Atom wmChangeState = None;
66 Atom kwmDockWindow = None;
68 Atom netwmPing = None;
69 Atom _kde_net_wm_system_tray_window_for = None; /* KDE 2 Final */
71 static LPCSTR whole_window_atom;
72 static LPCSTR client_window_atom;
73 static LPCSTR icon_window_atom;
75 /***********************************************************************
78 * Check if a given window should be managed
80 inline static BOOL is_window_managed( WND *win )
82 if (!managed_mode) return FALSE;
83 /* tray window is always managed */
84 if (win->dwExStyle & WS_EX_TRAYWINDOW) return TRUE;
85 /* child windows are not managed */
86 if (win->dwStyle & WS_CHILD) return FALSE;
87 /* tool windows are not managed */
88 if (win->dwExStyle & WS_EX_TOOLWINDOW) return FALSE;
89 /* windows with caption or thick frame are managed */
90 if ((win->dwStyle & WS_CAPTION) == WS_CAPTION) return TRUE;
91 if (win->dwStyle & WS_THICKFRAME) return TRUE;
92 /* default: not managed */
97 /***********************************************************************
98 * is_client_window_mapped
100 * Check if the X client window should be mapped
102 inline static BOOL is_client_window_mapped( WND *win )
104 struct x11drv_win_data *data = win->pDriverData;
105 return !(win->dwStyle & WS_MINIMIZE) && !IsRectEmpty( &data->client_rect );
109 /***********************************************************************
110 * get_window_attributes
112 * Fill the window attributes structure for an X window.
114 static int get_window_attributes( Display *display, WND *win, XSetWindowAttributes *attr )
116 BOOL is_top_level = is_window_top_level( win );
117 BOOL managed = is_top_level && is_window_managed( win );
119 if (managed) WIN_SetExStyle( win->hwndSelf, win->dwExStyle | WS_EX_MANAGED );
120 else WIN_SetExStyle( win->hwndSelf, win->dwExStyle & ~WS_EX_MANAGED );
122 attr->override_redirect = !managed;
123 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
124 attr->save_under = ((win->clsStyle & CS_SAVEBITS) != 0);
125 attr->cursor = x11drv_thread_data()->cursor;
126 attr->event_mask = (ExposureMask | PointerMotionMask |
127 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
129 if (is_window_top_level( win ))
130 attr->event_mask |= (KeyPressMask | KeyReleaseMask | StructureNotifyMask |
131 FocusChangeMask | KeymapStateMask);
133 return (CWOverrideRedirect | CWSaveUnder | CWEventMask | CWColormap | CWCursor);
137 /***********************************************************************
138 * X11DRV_sync_window_style
140 * Change the X window attributes when the window style has changed.
142 void X11DRV_sync_window_style( Display *display, WND *win )
144 XSetWindowAttributes attr;
148 mask = get_window_attributes( display, win, &attr );
149 XChangeWindowAttributes( display, get_whole_window(win), mask, &attr );
154 /***********************************************************************
157 * fill the window changes structure
159 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
163 if (old->right - old->left != new->right - new->left )
165 if (!(changes->width = new->right - new->left)) changes->width = 1;
168 if (old->bottom - old->top != new->bottom - new->top)
170 if (!(changes->height = new->bottom - new->top)) changes->height = 1;
173 if (old->left != new->left)
175 changes->x = new->left;
178 if (old->top != new->top)
180 changes->y = new->top;
187 /***********************************************************************
190 static Window create_icon_window( Display *display, WND *win )
192 struct x11drv_win_data *data = win->pDriverData;
193 XSetWindowAttributes attr;
195 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
196 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
197 attr.bit_gravity = NorthWestGravity;
198 attr.backing_store = NotUseful/*WhenMapped*/;
199 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
202 data->icon_window = XCreateWindow( display, root_window, 0, 0,
203 GetSystemMetrics( SM_CXICON ),
204 GetSystemMetrics( SM_CYICON ),
207 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
208 XSaveContext( display, data->icon_window, winContext, (char *)win->hwndSelf );
211 TRACE( "created %lx\n", data->icon_window );
212 SetPropA( win->hwndSelf, icon_window_atom, (HANDLE)data->icon_window );
213 return data->icon_window;
218 /***********************************************************************
219 * destroy_icon_window
221 inline static void destroy_icon_window( Display *display, WND *win )
223 struct x11drv_win_data *data = win->pDriverData;
225 if (!data->icon_window) return;
226 if (x11drv_thread_data()->cursor_window == data->icon_window)
227 x11drv_thread_data()->cursor_window = None;
229 XDeleteContext( display, data->icon_window, winContext );
230 XDestroyWindow( display, data->icon_window );
231 data->icon_window = 0;
233 RemovePropA( win->hwndSelf, icon_window_atom );
237 /***********************************************************************
240 * Set the icon wm hints
242 static void set_icon_hints( Display *display, WND *wndPtr, XWMHints *hints )
244 X11DRV_WND_DATA *data = wndPtr->pDriverData;
245 HICON hIcon = (HICON)GetClassLongA( wndPtr->hwndSelf, GCL_HICON );
247 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
248 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
249 data->hWMIconBitmap = 0;
250 data->hWMIconMask = 0;
252 if (!(wndPtr->dwExStyle & WS_EX_MANAGED))
254 destroy_icon_window( display, wndPtr );
255 hints->flags &= ~(IconPixmapHint | IconMaskHint | IconWindowHint);
259 if (!data->icon_window) create_icon_window( display, wndPtr );
260 hints->icon_window = data->icon_window;
261 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
271 GetIconInfo(hIcon, &ii);
273 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
276 rcMask.right = bmMask.bmWidth;
277 rcMask.bottom = bmMask.bmHeight;
279 hDC = CreateCompatibleDC(0);
280 hbmOrig = SelectObject(hDC, ii.hbmMask);
281 InvertRect(hDC, &rcMask);
282 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
283 SelectObject(hDC, hbmOrig);
286 data->hWMIconBitmap = ii.hbmColor;
287 data->hWMIconMask = ii.hbmMask;
289 hints->icon_pixmap = X11DRV_BITMAP_Pixmap(data->hWMIconBitmap);
290 hints->icon_mask = X11DRV_BITMAP_Pixmap(data->hWMIconMask);
291 destroy_icon_window( display, wndPtr );
292 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
297 /***********************************************************************
300 * set the window size hints
302 static void set_size_hints( Display *display, WND *win )
304 XSizeHints* size_hints;
305 struct x11drv_win_data *data = win->pDriverData;
307 if ((size_hints = XAllocSizeHints()))
309 size_hints->win_gravity = StaticGravity;
310 size_hints->x = data->whole_rect.left;
311 size_hints->y = data->whole_rect.top;
312 size_hints->flags = PWinGravity | PPosition;
314 if (HAS_DLGFRAME( win->dwStyle, win->dwExStyle ))
316 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
317 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
318 size_hints->min_width = size_hints->max_width;
319 size_hints->min_height = size_hints->max_height;
320 size_hints->flags |= PMinSize | PMaxSize;
322 XSetWMNormalHints( display, data->whole_window, size_hints );
328 /***********************************************************************
329 * X11DRV_set_wm_hints
331 * Set the window manager hints for a newly-created window
333 void X11DRV_set_wm_hints( Display *display, WND *win )
335 struct x11drv_win_data *data = win->pDriverData;
337 XClassHint *class_hints;
346 protocols[i++] = wmDeleteWindow;
347 if (wmTakeFocus) protocols[i++] = wmTakeFocus;
348 if (netwmPing) protocols[i++] = netwmPing;
349 XSetWMProtocols( display, data->whole_window, protocols, i );
352 if ((class_hints = XAllocClassHint()))
354 class_hints->res_name = "wine";
355 class_hints->res_class = "Wine";
356 XSetClassHint( display, data->whole_window, class_hints );
357 XFree( class_hints );
360 /* transient for hint */
363 Window owner_win = X11DRV_get_whole_window( win->owner );
364 XSetTransientForHint( display, data->whole_window, owner_win );
365 group_leader = owner_win;
367 else group_leader = data->whole_window;
370 set_size_hints( display, win );
372 /* systray properties (KDE only for now) */
373 if (win->dwExStyle & WS_EX_TRAYWINDOW)
376 if (kwmDockWindow != None)
377 XChangeProperty( display, data->whole_window, kwmDockWindow, kwmDockWindow,
378 32, PropModeReplace, (char*)&val, 1 );
379 if (_kde_net_wm_system_tray_window_for != None)
380 XChangeProperty( display, data->whole_window, _kde_net_wm_system_tray_window_for,
381 XA_WINDOW, 32, PropModeReplace, (char*)&data->whole_window, 1 );
384 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
385 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
386 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
388 XChangeProperty(display, data->whole_window, netwmPid, XA_CARDINAL, 32, PropModeReplace, (char *)&i, 1);
390 if (mwmHints != None)
393 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
394 mwm_hints.functions = 0;
395 if ((win->dwStyle & WS_CAPTION) == WS_CAPTION) mwm_hints.functions |= MWM_FUNC_MOVE;
396 if (win->dwStyle & WS_THICKFRAME) mwm_hints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
397 if (win->dwStyle & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
398 if (win->dwStyle & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
399 if (win->dwStyle & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
400 mwm_hints.decorations = 0;
401 if ((win->dwStyle & WS_CAPTION) == WS_CAPTION) mwm_hints.decorations |= MWM_DECOR_TITLE;
402 if (win->dwExStyle & WS_EX_DLGMODALFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
403 else if (win->dwStyle & WS_THICKFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
404 else if ((win->dwStyle & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
405 else if (win->dwStyle & WS_BORDER) mwm_hints.decorations |= MWM_DECOR_BORDER;
406 else if (!(win->dwStyle & (WS_CHILD|WS_POPUP))) mwm_hints.decorations |= MWM_DECOR_BORDER;
407 if (win->dwStyle & WS_SYSMENU) mwm_hints.decorations |= MWM_DECOR_MENU;
408 if (win->dwStyle & WS_MINIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MINIMIZE;
409 if (win->dwStyle & WS_MAXIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MAXIMIZE;
411 XChangeProperty( display, data->whole_window, mwmHints, mwmHints, 32,
412 PropModeReplace, (char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
415 wm_hints = XAllocWMHints();
421 wm_hints->flags = InputHint | StateHint | WindowGroupHint;
422 wm_hints->input = !(win->dwStyle & WS_DISABLED);
424 set_icon_hints( display, win, wm_hints );
426 wm_hints->initial_state = (win->dwStyle & WS_MINIMIZE) ? IconicState : NormalState;
427 wm_hints->window_group = group_leader;
430 XSetWMHints( display, data->whole_window, wm_hints );
437 /***********************************************************************
438 * X11DRV_set_iconic_state
440 * Set the X11 iconic state according to the window style.
442 void X11DRV_set_iconic_state( WND *win )
444 Display *display = thread_display();
445 struct x11drv_win_data *data = win->pDriverData;
447 BOOL iconic = IsIconic( win->hwndSelf );
451 if (iconic) XUnmapWindow( display, data->client_window );
452 else if (is_client_window_mapped( win )) XMapWindow( display, data->client_window );
454 if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
455 wm_hints->flags |= StateHint | IconPositionHint;
456 wm_hints->initial_state = iconic ? IconicState : NormalState;
457 wm_hints->icon_x = win->rectWindow.left;
458 wm_hints->icon_y = win->rectWindow.top;
459 XSetWMHints( display, data->whole_window, wm_hints );
461 if (win->dwStyle & WS_VISIBLE)
464 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
466 if (!IsRectEmpty( &win->rectWindow )) XMapWindow( display, data->whole_window );
474 /***********************************************************************
475 * X11DRV_window_to_X_rect
477 * Convert a rect from client to X window coordinates
479 void X11DRV_window_to_X_rect( WND *win, RECT *rect )
483 if (!(win->dwExStyle & WS_EX_MANAGED)) return;
484 if (IsRectEmpty( rect )) return;
486 rc.top = rc.bottom = rc.left = rc.right = 0;
488 AdjustWindowRectEx( &rc, win->dwStyle & ~(WS_HSCROLL|WS_VSCROLL), FALSE, win->dwExStyle );
490 rect->left -= rc.left;
491 rect->right -= rc.right;
493 rect->bottom -= rc.bottom;
494 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
495 if (rect->left >= rect->right) rect->right = rect->left + 1;
499 /***********************************************************************
500 * X11DRV_X_to_window_rect
502 * Opposite of X11DRV_window_to_X_rect
504 void X11DRV_X_to_window_rect( WND *win, RECT *rect )
506 if (!(win->dwExStyle & WS_EX_MANAGED)) return;
507 if (IsRectEmpty( rect )) return;
509 AdjustWindowRectEx( rect, win->dwStyle & ~(WS_HSCROLL|WS_VSCROLL), FALSE, win->dwExStyle );
511 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
512 if (rect->left >= rect->right) rect->right = rect->left + 1;
516 /***********************************************************************
517 * X11DRV_sync_whole_window_position
519 * Synchronize the X whole window position with the Windows one
521 int X11DRV_sync_whole_window_position( Display *display, WND *win, int zorder )
523 XWindowChanges changes;
525 struct x11drv_win_data *data = win->pDriverData;
526 RECT whole_rect = win->rectWindow;
528 X11DRV_window_to_X_rect( win, &whole_rect );
529 mask = get_window_changes( &changes, &data->whole_rect, &whole_rect );
533 if (is_window_top_level( win ))
535 /* find window that this one must be after */
536 HWND prev = GetWindow( win->hwndSelf, GW_HWNDPREV );
537 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
538 prev = GetWindow( prev, GW_HWNDPREV );
539 if (!prev) /* top child */
541 changes.stack_mode = Above;
546 /* should use stack_mode Below but most window managers don't get it right */
547 /* so move it above the next one in Z order */
548 HWND next = GetWindow( win->hwndSelf, GW_HWNDNEXT );
549 while (next && !(GetWindowLongW( next, GWL_STYLE ) & WS_VISIBLE))
550 next = GetWindow( next, GW_HWNDNEXT );
553 changes.stack_mode = Above;
554 changes.sibling = X11DRV_get_whole_window(next);
555 mask |= CWStackMode | CWSibling;
561 HWND next = GetWindow( win->hwndSelf, GW_HWNDNEXT );
563 if (win->parent == GetDesktopWindow() &&
564 root_window != DefaultRootWindow(display))
566 /* in desktop mode we need the sibling to belong to the same process */
569 WND *ptr = WIN_GetPtr( next );
570 if (ptr != WND_OTHER_PROCESS)
572 WIN_ReleasePtr( ptr );
575 next = GetWindow( next, GW_HWNDNEXT );
579 if (!next) /* bottom child */
581 changes.stack_mode = Below;
586 changes.stack_mode = Above;
587 changes.sibling = X11DRV_get_whole_window(next);
588 mask |= CWStackMode | CWSibling;
593 data->whole_rect = whole_rect;
597 TRACE( "setting win %lx pos %ld,%ld,%ldx%ld after %lx changes=%x\n",
598 data->whole_window, whole_rect.left, whole_rect.top,
599 whole_rect.right - whole_rect.left, whole_rect.bottom - whole_rect.top,
600 changes.sibling, mask );
602 XSync( gdi_display, False ); /* flush graphics operations before moving the window */
603 if (is_window_top_level( win ))
605 if (mask & (CWWidth|CWHeight)) set_size_hints( display, win );
606 XReconfigureWMWindow( display, data->whole_window,
607 DefaultScreen(display), mask, &changes );
609 else XConfigureWindow( display, data->whole_window, mask, &changes );
616 /***********************************************************************
617 * X11DRV_sync_client_window_position
619 * Synchronize the X client window position with the Windows one
621 int X11DRV_sync_client_window_position( Display *display, WND *win )
623 XWindowChanges changes;
625 struct x11drv_win_data *data = win->pDriverData;
626 RECT client_rect = win->rectClient;
628 OffsetRect( &client_rect, -data->whole_rect.left, -data->whole_rect.top );
630 if ((mask = get_window_changes( &changes, &data->client_rect, &client_rect )))
632 BOOL was_mapped = is_client_window_mapped( win );
634 TRACE( "setting win %lx pos %ld,%ld,%ldx%ld (was %ld,%ld,%ldx%ld) after %lx changes=%x\n",
635 data->client_window, client_rect.left, client_rect.top,
636 client_rect.right - client_rect.left, client_rect.bottom - client_rect.top,
637 data->client_rect.left, data->client_rect.top,
638 data->client_rect.right - data->client_rect.left,
639 data->client_rect.bottom - data->client_rect.top,
640 changes.sibling, mask );
641 data->client_rect = client_rect;
643 XSync( gdi_display, False ); /* flush graphics operations before moving the window */
644 if (was_mapped && !is_client_window_mapped( win ))
645 XUnmapWindow( display, data->client_window );
646 XConfigureWindow( display, data->client_window, mask, &changes );
647 if (!was_mapped && is_client_window_mapped( win ))
648 XMapWindow( display, data->client_window );
655 /***********************************************************************
656 * X11DRV_register_window
658 * Associate an X window to a HWND.
660 void X11DRV_register_window( Display *display, HWND hwnd, struct x11drv_win_data *data )
663 XSaveContext( display, data->whole_window, winContext, (char *)hwnd );
664 XSaveContext( display, data->client_window, winContext, (char *)hwnd );
669 /**********************************************************************
672 static void create_desktop( Display *display, WND *wndPtr )
674 X11DRV_WND_DATA *data = wndPtr->pDriverData;
677 winContext = XUniqueContext();
678 wmProtocols = XInternAtom( display, "WM_PROTOCOLS", False );
679 wmDeleteWindow = XInternAtom( display, "WM_DELETE_WINDOW", False );
680 if (use_take_focus) wmTakeFocus = XInternAtom( display, "WM_TAKE_FOCUS", False );
681 dndProtocol = XInternAtom( display, "DndProtocol" , False );
682 dndSelection = XInternAtom( display, "DndSelection" , False );
683 wmChangeState = XInternAtom( display, "WM_CHANGE_STATE", False );
684 mwmHints = XInternAtom( display, _XA_MWM_HINTS, False );
685 kwmDockWindow = XInternAtom( display, "KWM_DOCKWINDOW", False );
686 _kde_net_wm_system_tray_window_for = XInternAtom( display, "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR", False );
687 netwmPid = XInternAtom( display, "_NET_WM_PID", False );
688 netwmPing = XInternAtom( display, "_NET_WM_PING", False );
691 whole_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_whole_window" ));
692 client_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_client_window" ));
693 icon_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_icon_window" ));
695 data->whole_window = data->client_window = root_window;
696 data->whole_rect = data->client_rect = wndPtr->rectWindow;
698 SetPropA( wndPtr->hwndSelf, whole_window_atom, (HANDLE)root_window );
699 SetPropA( wndPtr->hwndSelf, client_window_atom, (HANDLE)root_window );
700 SetPropA( wndPtr->hwndSelf, "__wine_x11_visual_id", (HANDLE)XVisualIDFromVisual(visual) );
702 if (root_window != DefaultRootWindow(display)) X11DRV_create_desktop_thread();
706 /**********************************************************************
707 * create_whole_window
709 * Create the whole X window for a given window
711 static Window create_whole_window( Display *display, WND *win )
713 struct x11drv_win_data *data = win->pDriverData;
715 XSetWindowAttributes attr;
718 BOOL is_top_level = is_window_top_level( win );
720 rect = win->rectWindow;
721 X11DRV_window_to_X_rect( win, &rect );
723 if (!(cx = rect.right - rect.left)) cx = 1;
724 if (!(cy = rect.bottom - rect.top)) cy = 1;
726 parent = X11DRV_get_client_window( win->parent );
730 mask = get_window_attributes( display, win, &attr );
732 /* set the attributes that don't change over the lifetime of the window */
733 attr.bit_gravity = ForgetGravity;
734 attr.win_gravity = NorthWestGravity;
735 attr.backing_store = NotUseful/*WhenMapped*/;
736 mask |= CWBitGravity | CWWinGravity | CWBackingStore;
738 data->whole_rect = rect;
739 data->whole_window = XCreateWindow( display, parent, rect.left, rect.top, cx, cy,
740 0, screen_depth, InputOutput, visual,
743 if (!data->whole_window)
751 XIM xim = x11drv_thread_data()->xim;
752 if (xim) data->xic = XCreateIC( xim,
753 XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
754 XNClientWindow, data->whole_window,
755 XNFocusWindow, data->whole_window,
759 /* non-maximized child must be at bottom of Z order */
760 if ((win->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
762 XWindowChanges changes;
763 changes.stack_mode = Below;
764 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
769 if (is_top_level) X11DRV_set_wm_hints( display, win );
771 return data->whole_window;
775 /**********************************************************************
776 * create_client_window
778 * Create the client window for a given window
780 static Window create_client_window( Display *display, WND *win )
782 struct x11drv_win_data *data = win->pDriverData;
783 RECT rect = data->whole_rect;
784 XSetWindowAttributes attr;
786 OffsetRect( &rect, -data->whole_rect.left, -data->whole_rect.top );
787 data->client_rect = rect;
789 attr.event_mask = (ExposureMask | PointerMotionMask |
790 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
791 attr.bit_gravity = (win->clsStyle & (CS_VREDRAW | CS_HREDRAW)) ?
792 ForgetGravity : NorthWestGravity;
793 attr.backing_store = NotUseful/*WhenMapped*/;
796 data->client_window = XCreateWindow( display, data->whole_window, 0, 0,
797 max( rect.right - rect.left, 1 ),
798 max( rect.bottom - rect.top, 1 ),
801 CWEventMask | CWBitGravity | CWBackingStore, &attr );
802 if (data->client_window && is_client_window_mapped( win ))
803 XMapWindow( display, data->client_window );
805 return data->client_window;
809 /*****************************************************************
810 * SetWindowText (X11DRV.@)
812 BOOL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
814 Display *display = thread_display();
821 if ((win = X11DRV_get_whole_window( hwnd )))
823 /* allocate new buffer for window text */
824 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
825 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count )))
827 ERR("Not enough memory for window text\n");
830 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
832 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
833 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
835 ERR("Not enough memory for window text in UTF-8\n");
838 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
841 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
843 XSetWMName( display, win, &prop );
844 XSetWMIconName( display, win, &prop );
848 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
849 according to the standard
850 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
852 XChangeProperty( display, win,
853 XInternAtom(display, "_NET_WM_NAME", False),
854 XInternAtom(display, "UTF8_STRING", False),
855 8, PropModeReplace, (unsigned char *) utf8_buffer,
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 WND *wndPtr = WIN_GetPtr( hwnd );
874 X11DRV_WND_DATA *data = wndPtr->pDriverData;
876 if (!data) goto done;
878 if (data->whole_window)
880 TRACE( "win %p xwin %lx/%lx\n", hwnd, data->whole_window, data->client_window );
881 if (thread_data->cursor_window == data->whole_window) thread_data->cursor_window = None;
882 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
884 XSync( gdi_display, False ); /* flush any reference to this drawable in GDI queue */
885 XDeleteContext( display, data->whole_window, winContext );
886 XDeleteContext( display, data->client_window, winContext );
887 XDestroyWindow( display, data->whole_window ); /* this destroys client too */
890 XUnsetICFocus( data->xic );
891 XDestroyIC( data->xic );
893 destroy_icon_window( display, wndPtr );
897 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
898 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
899 HeapFree( GetProcessHeap(), 0, data );
900 wndPtr->pDriverData = NULL;
902 WIN_ReleasePtr( wndPtr );
907 /**********************************************************************
908 * CreateWindow (X11DRV.@)
910 BOOL X11DRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
912 Display *display = thread_display();
914 struct x11drv_win_data *data;
921 ERR( "invalid window width %d\n", cs->cx );
926 ERR( "invalid window height %d\n", cs->cx );
930 if (!(data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data)))) return FALSE;
931 data->whole_window = 0;
932 data->client_window = 0;
933 data->icon_window = 0;
935 data->hWMIconBitmap = 0;
936 data->hWMIconMask = 0;
938 wndPtr = WIN_GetPtr( hwnd );
939 wndPtr->pDriverData = data;
941 /* initialize the dimensions before sending WM_GETMINMAXINFO */
942 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
943 WIN_SetRectangles( hwnd, &rect, &rect );
947 create_desktop( display, wndPtr );
948 WIN_ReleasePtr( wndPtr );
952 if (!create_whole_window( display, wndPtr )) goto failed;
953 if (!create_client_window( display, wndPtr )) goto failed;
954 TSXSync( display, False );
956 SetPropA( hwnd, whole_window_atom, (HANDLE)data->whole_window );
957 SetPropA( hwnd, client_window_atom, (HANDLE)data->client_window );
959 /* Call the WH_CBT hook */
961 cbtc.hwndInsertAfter = HWND_TOP;
962 if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode ))
964 TRACE("CBT-hook returned !0\n");
968 /* Send the WM_GETMINMAXINFO message and fix the size if needed */
969 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
971 POINT maxSize, maxPos, minTrack, maxTrack;
973 WIN_ReleasePtr( wndPtr );
974 WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
975 if (maxSize.x < cs->cx) cs->cx = maxSize.x;
976 if (maxSize.y < cs->cy) cs->cy = maxSize.y;
977 if (cs->cx < minTrack.x ) cs->cx = minTrack.x;
978 if (cs->cy < minTrack.y ) cs->cy = minTrack.y;
979 if (cs->cx < 0) cs->cx = 0;
980 if (cs->cy < 0) cs->cy = 0;
982 if (!(wndPtr = WIN_GetPtr( hwnd ))) return FALSE;
983 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
984 WIN_SetRectangles( hwnd, &rect, &rect );
985 X11DRV_sync_whole_window_position( display, wndPtr, 0 );
987 WIN_ReleasePtr( wndPtr );
989 /* send WM_NCCREATE */
990 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cs->cx, cs->cy );
992 ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
994 ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
997 WARN("aborted by WM_xxCREATE!\n");
1001 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
1003 X11DRV_sync_window_style( display, wndPtr );
1005 /* send WM_NCCALCSIZE */
1006 rect = wndPtr->rectWindow;
1007 WIN_ReleasePtr( wndPtr );
1008 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&rect );
1010 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
1011 if (rect.left > rect.right || rect.top > rect.bottom) rect = wndPtr->rectWindow;
1012 WIN_SetRectangles( hwnd, &wndPtr->rectWindow, &rect );
1013 X11DRV_sync_client_window_position( display, wndPtr );
1014 X11DRV_register_window( display, hwnd, data );
1016 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",
1017 hwnd, wndPtr->rectWindow.left, wndPtr->rectWindow.top,
1018 wndPtr->rectWindow.right, wndPtr->rectWindow.bottom,
1019 wndPtr->rectClient.left, wndPtr->rectClient.top,
1020 wndPtr->rectClient.right, wndPtr->rectClient.bottom,
1021 data->whole_rect.left, data->whole_rect.top,
1022 data->whole_rect.right, data->whole_rect.bottom,
1023 data->client_rect.left, data->client_rect.top,
1024 data->client_rect.right, data->client_rect.bottom,
1025 (unsigned int)data->whole_window, (unsigned int)data->client_window );
1027 if ((wndPtr->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
1028 WIN_LinkWindow( hwnd, wndPtr->parent, HWND_BOTTOM );
1030 WIN_LinkWindow( hwnd, wndPtr->parent, HWND_TOP );
1032 WIN_ReleasePtr( wndPtr );
1035 ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1037 ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1041 WIN_UnlinkWindow( hwnd );
1045 /* Send the size messages */
1047 if (!(wndPtr = WIN_FindWndPtr(hwnd))) return FALSE;
1048 if (!(wndPtr->flags & WIN_NEED_SIZE))
1050 /* send it anyway */
1051 if (((wndPtr->rectClient.right-wndPtr->rectClient.left) <0)
1052 ||((wndPtr->rectClient.bottom-wndPtr->rectClient.top)<0))
1053 WARN("sending bogus WM_SIZE message 0x%08lx\n",
1054 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
1055 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
1056 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1057 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
1058 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
1059 SendMessageW( hwnd, WM_MOVE, 0,
1060 MAKELONG( wndPtr->rectClient.left, wndPtr->rectClient.top ) );
1063 /* Show the window, maximizing or minimizing if needed */
1065 if (wndPtr->dwStyle & (WS_MINIMIZE | WS_MAXIMIZE))
1067 extern UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect ); /*FIXME*/
1070 UINT swFlag = (wndPtr->dwStyle & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1071 WIN_SetStyle( hwnd, wndPtr->dwStyle & ~(WS_MAXIMIZE | WS_MINIMIZE) );
1072 WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1073 swFlag = ((wndPtr->dwStyle & 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 );
1080 /* if the window was made visible set create struct flag so that
1081 * we do a proper ShowWindow later on */
1082 if (wndPtr->dwStyle & WS_VISIBLE) cs->style |= WS_VISIBLE;
1084 WIN_ReleaseWndPtr( wndPtr );
1089 X11DRV_DestroyWindow( hwnd );
1090 if (wndPtr) WIN_ReleasePtr( wndPtr );
1095 /***********************************************************************
1096 * X11DRV_get_client_window
1098 * Return the X window associated with the client area of a window
1100 Window X11DRV_get_client_window( HWND hwnd )
1103 WND *win = WIN_GetPtr( hwnd );
1105 if (win == WND_OTHER_PROCESS)
1106 return (Window)GetPropA( hwnd, client_window_atom );
1110 struct x11drv_win_data *data = win->pDriverData;
1111 ret = data->client_window;
1112 WIN_ReleasePtr( win );
1118 /***********************************************************************
1119 * X11DRV_get_whole_window
1121 * Return the X window associated with the full area of a window
1123 Window X11DRV_get_whole_window( HWND hwnd )
1126 WND *win = WIN_GetPtr( hwnd );
1128 if (win == WND_OTHER_PROCESS)
1129 return (Window)GetPropA( hwnd, whole_window_atom );
1133 struct x11drv_win_data *data = win->pDriverData;
1134 ret = data->whole_window;
1135 WIN_ReleasePtr( win );
1141 /***********************************************************************
1144 * Return the X input context associated with a window
1146 XIC X11DRV_get_ic( HWND hwnd )
1149 WND *win = WIN_GetPtr( hwnd );
1151 if (win && win != WND_OTHER_PROCESS)
1153 struct x11drv_win_data *data = win->pDriverData;
1155 WIN_ReleasePtr( win );
1161 /*****************************************************************
1162 * SetParent (X11DRV.@)
1164 HWND X11DRV_SetParent( HWND hwnd, HWND parent )
1166 Display *display = thread_display();
1170 /* Windows hides the window first, then shows it again
1171 * including the WM_SHOWWINDOW messages and all */
1172 BOOL was_visible = ShowWindow( hwnd, SW_HIDE );
1174 if (!IsWindow( parent )) return 0;
1175 if (!(wndPtr = WIN_GetPtr(hwnd)) || wndPtr == WND_OTHER_PROCESS) return 0;
1177 retvalue = wndPtr->parent; /* old parent */
1178 if (parent != retvalue)
1180 struct x11drv_win_data *data = wndPtr->pDriverData;
1182 WIN_LinkWindow( hwnd, parent, HWND_TOP );
1184 if (parent != GetDesktopWindow()) /* a child window */
1186 if (!(wndPtr->dwStyle & WS_CHILD))
1188 HMENU menu = (HMENU)SetWindowLongW( hwnd, GWL_ID, 0 );
1189 if (menu) DestroyMenu( menu );
1193 if (is_window_top_level( wndPtr )) X11DRV_set_wm_hints( display, wndPtr );
1195 X11DRV_sync_window_style( display, wndPtr );
1196 XReparentWindow( display, data->whole_window, X11DRV_get_client_window(parent),
1197 data->whole_rect.left, data->whole_rect.top );
1198 wine_tsx11_unlock();
1200 WIN_ReleasePtr( wndPtr );
1202 /* SetParent additionally needs to make hwnd the topmost window
1203 in the x-order and send the expected WM_WINDOWPOSCHANGING and
1204 WM_WINDOWPOSCHANGED notification messages.
1206 SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0,
1207 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | (was_visible ? SWP_SHOWWINDOW : 0) );
1208 /* FIXME: a WM_MOVE is also generated (in the DefWindowProc handler
1209 * for WM_WINDOWPOSCHANGED) in Windows, should probably remove SWP_NOMOVE */
1215 /*****************************************************************
1216 * SetFocus (X11DRV.@)
1219 * Explicit colormap management seems to work only with OLVWM.
1221 void X11DRV_SetFocus( HWND hwnd )
1223 Display *display = thread_display();
1224 XWindowAttributes win_attr;
1227 /* Only mess with the X focus if there's */
1228 /* no desktop window and if the window is not managed by the WM. */
1229 if (root_window != DefaultRootWindow(display)) return;
1231 if (!hwnd) /* If setting the focus to 0, uninstall the colormap */
1233 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1234 TSXUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1238 hwnd = GetAncestor( hwnd, GA_ROOT );
1239 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_MANAGED) return;
1240 if (!(win = X11DRV_get_whole_window( hwnd ))) return;
1242 /* Set X focus and install colormap */
1244 if (XGetWindowAttributes( display, win, &win_attr ) &&
1245 (win_attr.map_state == IsViewable))
1247 /* If window is not viewable, don't change anything */
1249 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1250 /* FIXME: this is not entirely correct */
1251 XSetInputFocus( display, win, RevertToParent,
1252 /*CurrentTime*/ GetMessageTime() + X11DRV_server_startticks );
1253 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1254 XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1256 wine_tsx11_unlock();
1260 /**********************************************************************
1261 * SetWindowIcon (X11DRV.@)
1263 * hIcon or hIconSm has changed (or is being initialised for the
1264 * first time). Complete the X11 driver-specific initialisation
1265 * and set the window hints.
1267 * This is not entirely correct, may need to create
1268 * an icon window and set the pixmap as a background
1270 HICON X11DRV_SetWindowIcon( HWND hwnd, HICON icon, BOOL small )
1273 Display *display = thread_display();
1274 HICON old = (HICON)SetClassLongW(hwnd, small ? GCL_HICONSM : GCL_HICON, (LONG)icon );
1276 SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOSIZE |
1277 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
1279 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return old;
1281 if (wndPtr->dwExStyle & WS_EX_MANAGED)
1283 Window win = get_whole_window(wndPtr);
1287 if (!(wm_hints = XGetWMHints( display, win ))) wm_hints = XAllocWMHints();
1288 wine_tsx11_unlock();
1291 set_icon_hints( display, wndPtr, wm_hints );
1293 XSetWMHints( display, win, wm_hints );
1295 wine_tsx11_unlock();
1298 WIN_ReleasePtr( wndPtr );