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
29 #include <X11/Xresource.h>
30 #include <X11/Xutil.h>
36 #include "wine/unicode.h"
38 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
46 extern Pixmap X11DRV_BITMAP_Pixmap( HBITMAP );
48 #define HAS_DLGFRAME(style,exStyle) \
49 (((exStyle) & WS_EX_DLGMODALFRAME) || \
50 (((style) & WS_DLGFRAME) && !((style) & WS_THICKFRAME)))
52 /* X context to associate a hwnd to an X window */
53 XContext winContext = 0;
55 Atom wmProtocols = None;
56 Atom wmDeleteWindow = None;
57 Atom wmTakeFocus = None;
58 Atom dndProtocol = None;
59 Atom dndSelection = None;
60 Atom wmChangeState = None;
62 Atom kwmDockWindow = None;
64 Atom netwmPing = None;
65 Atom _kde_net_wm_system_tray_window_for = None; /* KDE 2 Final */
67 static LPCSTR whole_window_atom;
68 static LPCSTR client_window_atom;
69 static LPCSTR icon_window_atom;
71 /***********************************************************************
74 * Check if a given window should be managed
76 inline static BOOL is_window_managed( WND *win )
78 if (!managed_mode) return FALSE;
79 /* tray window is always managed */
80 if (win->dwExStyle & WS_EX_TRAYWINDOW) return TRUE;
81 /* child windows are not managed */
82 if (win->dwStyle & WS_CHILD) return FALSE;
83 /* tool windows are not managed */
84 if (win->dwExStyle & WS_EX_TOOLWINDOW) return FALSE;
85 /* windows with caption or thick frame are managed */
86 if ((win->dwStyle & WS_CAPTION) == WS_CAPTION) return TRUE;
87 if (win->dwStyle & WS_THICKFRAME) return TRUE;
88 /* default: not managed */
93 /***********************************************************************
94 * is_client_window_mapped
96 * Check if the X client window should be mapped
98 inline static BOOL is_client_window_mapped( WND *win )
100 struct x11drv_win_data *data = win->pDriverData;
101 return !(win->dwStyle & WS_MINIMIZE) && !IsRectEmpty( &data->client_rect );
105 /***********************************************************************
106 * get_window_attributes
108 * Fill the window attributes structure for an X window.
110 static int get_window_attributes( Display *display, WND *win, XSetWindowAttributes *attr )
112 BOOL is_top_level = is_window_top_level( win );
113 BOOL managed = is_top_level && is_window_managed( win );
115 if (managed) WIN_SetExStyle( win->hwndSelf, win->dwExStyle | WS_EX_MANAGED );
116 else WIN_SetExStyle( win->hwndSelf, win->dwExStyle & ~WS_EX_MANAGED );
118 attr->override_redirect = !managed;
119 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
120 attr->save_under = ((win->clsStyle & CS_SAVEBITS) != 0);
121 attr->cursor = x11drv_thread_data()->cursor;
122 attr->event_mask = (ExposureMask | PointerMotionMask |
123 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
125 if (is_window_top_level( win ))
126 attr->event_mask |= (KeyPressMask | KeyReleaseMask | StructureNotifyMask |
127 FocusChangeMask | KeymapStateMask);
129 return (CWOverrideRedirect | CWSaveUnder | CWEventMask | CWColormap | CWCursor);
133 /***********************************************************************
134 * X11DRV_sync_window_style
136 * Change the X window attributes when the window style has changed.
138 void X11DRV_sync_window_style( Display *display, WND *win )
140 XSetWindowAttributes attr;
144 mask = get_window_attributes( display, win, &attr );
145 XChangeWindowAttributes( display, get_whole_window(win), mask, &attr );
150 /***********************************************************************
153 * fill the window changes structure
155 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
159 if (old->right - old->left != new->right - new->left )
161 if (!(changes->width = new->right - new->left)) changes->width = 1;
164 if (old->bottom - old->top != new->bottom - new->top)
166 if (!(changes->height = new->bottom - new->top)) changes->height = 1;
169 if (old->left != new->left)
171 changes->x = new->left;
174 if (old->top != new->top)
176 changes->y = new->top;
183 /***********************************************************************
186 static Window create_icon_window( Display *display, WND *win )
188 struct x11drv_win_data *data = win->pDriverData;
189 XSetWindowAttributes attr;
191 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
192 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
193 attr.bit_gravity = NorthWestGravity;
194 attr.backing_store = NotUseful/*WhenMapped*/;
195 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
198 data->icon_window = XCreateWindow( display, root_window, 0, 0,
199 GetSystemMetrics( SM_CXICON ),
200 GetSystemMetrics( SM_CYICON ),
203 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
204 XSaveContext( display, data->icon_window, winContext, (char *)win->hwndSelf );
207 TRACE( "created %lx\n", data->icon_window );
208 SetPropA( win->hwndSelf, icon_window_atom, (HANDLE)data->icon_window );
209 return data->icon_window;
214 /***********************************************************************
215 * destroy_icon_window
217 inline static void destroy_icon_window( Display *display, WND *win )
219 struct x11drv_win_data *data = win->pDriverData;
221 if (!data->icon_window) return;
222 if (x11drv_thread_data()->cursor_window == data->icon_window)
223 x11drv_thread_data()->cursor_window = None;
225 XDeleteContext( display, data->icon_window, winContext );
226 XDestroyWindow( display, data->icon_window );
227 data->icon_window = 0;
229 RemovePropA( win->hwndSelf, icon_window_atom );
233 /***********************************************************************
236 * Set the icon wm hints
238 static void set_icon_hints( Display *display, WND *wndPtr, XWMHints *hints )
240 X11DRV_WND_DATA *data = wndPtr->pDriverData;
241 HICON hIcon = (HICON)GetClassLongA( wndPtr->hwndSelf, GCL_HICON );
243 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
244 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
245 data->hWMIconBitmap = 0;
246 data->hWMIconMask = 0;
248 if (!(wndPtr->dwExStyle & WS_EX_MANAGED))
250 destroy_icon_window( display, wndPtr );
251 hints->flags &= ~(IconPixmapHint | IconMaskHint | IconWindowHint);
255 if (!data->icon_window) create_icon_window( display, wndPtr );
256 hints->icon_window = data->icon_window;
257 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
267 GetIconInfo(hIcon, &ii);
269 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
272 rcMask.right = bmMask.bmWidth;
273 rcMask.bottom = bmMask.bmHeight;
275 hDC = CreateCompatibleDC(0);
276 hbmOrig = SelectObject(hDC, ii.hbmMask);
277 InvertRect(hDC, &rcMask);
278 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
279 SelectObject(hDC, hbmOrig);
282 data->hWMIconBitmap = ii.hbmColor;
283 data->hWMIconMask = ii.hbmMask;
285 hints->icon_pixmap = X11DRV_BITMAP_Pixmap(data->hWMIconBitmap);
286 hints->icon_mask = X11DRV_BITMAP_Pixmap(data->hWMIconMask);
287 destroy_icon_window( display, wndPtr );
288 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
293 /***********************************************************************
296 * set the window size hints
298 static void set_size_hints( Display *display, WND *win )
300 XSizeHints* size_hints;
301 struct x11drv_win_data *data = win->pDriverData;
303 if ((size_hints = XAllocSizeHints()))
305 size_hints->win_gravity = StaticGravity;
306 size_hints->x = data->whole_rect.left;
307 size_hints->y = data->whole_rect.top;
308 size_hints->flags = PWinGravity | PPosition;
310 if (HAS_DLGFRAME( win->dwStyle, win->dwExStyle ))
312 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
313 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
314 size_hints->min_width = size_hints->max_width;
315 size_hints->min_height = size_hints->max_height;
316 size_hints->flags |= PMinSize | PMaxSize;
318 XSetWMNormalHints( display, data->whole_window, size_hints );
324 /***********************************************************************
325 * X11DRV_set_wm_hints
327 * Set the window manager hints for a newly-created window
329 void X11DRV_set_wm_hints( Display *display, WND *win )
331 struct x11drv_win_data *data = win->pDriverData;
333 XClassHint *class_hints;
342 protocols[i++] = wmDeleteWindow;
343 if (wmTakeFocus) protocols[i++] = wmTakeFocus;
344 if (netwmPing) protocols[i++] = netwmPing;
345 XSetWMProtocols( display, data->whole_window, protocols, i );
348 if ((class_hints = XAllocClassHint()))
350 class_hints->res_name = "wine";
351 class_hints->res_class = "Wine";
352 XSetClassHint( display, data->whole_window, class_hints );
353 XFree( class_hints );
356 /* transient for hint */
359 Window owner_win = X11DRV_get_whole_window( win->owner );
360 XSetTransientForHint( display, data->whole_window, owner_win );
361 group_leader = owner_win;
363 else group_leader = data->whole_window;
366 set_size_hints( display, win );
368 /* systray properties (KDE only for now) */
369 if (win->dwExStyle & WS_EX_TRAYWINDOW)
372 if (kwmDockWindow != None)
373 XChangeProperty( display, data->whole_window, kwmDockWindow, kwmDockWindow,
374 32, PropModeReplace, (char*)&val, 1 );
375 if (_kde_net_wm_system_tray_window_for != None)
376 XChangeProperty( display, data->whole_window, _kde_net_wm_system_tray_window_for,
377 XA_WINDOW, 32, PropModeReplace, (char*)&data->whole_window, 1 );
380 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
381 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
382 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
384 XChangeProperty(display, data->whole_window, netwmPid, XA_CARDINAL, 32, PropModeReplace, (char *)&i, 1);
386 if (mwmHints != None)
389 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
390 mwm_hints.functions = 0;
391 if ((win->dwStyle & WS_CAPTION) == WS_CAPTION) mwm_hints.functions |= MWM_FUNC_MOVE;
392 if (win->dwStyle & WS_THICKFRAME) mwm_hints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
393 if (win->dwStyle & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
394 if (win->dwStyle & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
395 if (win->dwStyle & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
396 mwm_hints.decorations = 0;
397 if ((win->dwStyle & WS_CAPTION) == WS_CAPTION) mwm_hints.decorations |= MWM_DECOR_TITLE;
398 if (win->dwExStyle & WS_EX_DLGMODALFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
399 else if (win->dwStyle & WS_THICKFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
400 else if ((win->dwStyle & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
401 else if (win->dwStyle & WS_BORDER) mwm_hints.decorations |= MWM_DECOR_BORDER;
402 else if (!(win->dwStyle & (WS_CHILD|WS_POPUP))) mwm_hints.decorations |= MWM_DECOR_BORDER;
403 if (win->dwStyle & WS_SYSMENU) mwm_hints.decorations |= MWM_DECOR_MENU;
404 if (win->dwStyle & WS_MINIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MINIMIZE;
405 if (win->dwStyle & WS_MAXIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MAXIMIZE;
407 XChangeProperty( display, data->whole_window, mwmHints, mwmHints, 32,
408 PropModeReplace, (char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
411 wm_hints = XAllocWMHints();
417 wm_hints->flags = InputHint | StateHint | WindowGroupHint;
418 wm_hints->input = !(win->dwStyle & WS_DISABLED);
420 set_icon_hints( display, win, wm_hints );
422 wm_hints->initial_state = (win->dwStyle & WS_MINIMIZE) ? IconicState : NormalState;
423 wm_hints->window_group = group_leader;
426 XSetWMHints( display, data->whole_window, wm_hints );
433 /***********************************************************************
434 * X11DRV_set_iconic_state
436 * Set the X11 iconic state according to the window style.
438 void X11DRV_set_iconic_state( WND *win )
440 Display *display = thread_display();
441 struct x11drv_win_data *data = win->pDriverData;
443 BOOL iconic = IsIconic( win->hwndSelf );
447 if (iconic) XUnmapWindow( display, data->client_window );
448 else if (is_client_window_mapped( win )) XMapWindow( display, data->client_window );
450 if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
451 wm_hints->flags |= StateHint | IconPositionHint;
452 wm_hints->initial_state = iconic ? IconicState : NormalState;
453 wm_hints->icon_x = win->rectWindow.left;
454 wm_hints->icon_y = win->rectWindow.top;
455 XSetWMHints( display, data->whole_window, wm_hints );
457 if (win->dwStyle & WS_VISIBLE)
460 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
462 if (!IsRectEmpty( &win->rectWindow )) XMapWindow( display, data->whole_window );
470 /***********************************************************************
471 * X11DRV_window_to_X_rect
473 * Convert a rect from client to X window coordinates
475 void X11DRV_window_to_X_rect( WND *win, RECT *rect )
479 if (!(win->dwExStyle & WS_EX_MANAGED)) return;
480 if (IsRectEmpty( rect )) return;
482 rc.top = rc.bottom = rc.left = rc.right = 0;
484 AdjustWindowRectEx( &rc, win->dwStyle & ~(WS_HSCROLL|WS_VSCROLL), FALSE, win->dwExStyle );
486 rect->left -= rc.left;
487 rect->right -= rc.right;
489 rect->bottom -= rc.bottom;
490 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
491 if (rect->left >= rect->right) rect->right = rect->left + 1;
495 /***********************************************************************
496 * X11DRV_X_to_window_rect
498 * Opposite of X11DRV_window_to_X_rect
500 void X11DRV_X_to_window_rect( WND *win, RECT *rect )
502 if (!(win->dwExStyle & WS_EX_MANAGED)) return;
503 if (IsRectEmpty( rect )) return;
505 AdjustWindowRectEx( rect, win->dwStyle & ~(WS_HSCROLL|WS_VSCROLL), FALSE, win->dwExStyle );
507 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
508 if (rect->left >= rect->right) rect->right = rect->left + 1;
512 /***********************************************************************
513 * X11DRV_sync_whole_window_position
515 * Synchronize the X whole window position with the Windows one
517 int X11DRV_sync_whole_window_position( Display *display, WND *win, int zorder )
519 XWindowChanges changes;
521 struct x11drv_win_data *data = win->pDriverData;
522 RECT whole_rect = win->rectWindow;
524 X11DRV_window_to_X_rect( win, &whole_rect );
525 mask = get_window_changes( &changes, &data->whole_rect, &whole_rect );
529 /* find window that this one must be after */
530 HWND prev = GetWindow( win->hwndSelf, GW_HWNDPREV );
531 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
532 prev = GetWindow( prev, GW_HWNDPREV );
533 if (!prev) /* top child */
535 changes.stack_mode = Above;
540 changes.stack_mode = Below;
541 changes.sibling = X11DRV_get_whole_window(prev);
542 mask |= CWStackMode | CWSibling;
546 data->whole_rect = whole_rect;
550 TRACE( "setting win %lx pos %ld,%ld,%ldx%ld after %lx changes=%x\n",
551 data->whole_window, whole_rect.left, whole_rect.top,
552 whole_rect.right - whole_rect.left, whole_rect.bottom - whole_rect.top,
553 changes.sibling, mask );
555 XSync( gdi_display, False ); /* flush graphics operations before moving the window */
556 if (is_window_top_level( win ))
558 if (mask & (CWWidth|CWHeight)) set_size_hints( display, win );
559 XReconfigureWMWindow( display, data->whole_window,
560 DefaultScreen(display), mask, &changes );
562 else XConfigureWindow( display, data->whole_window, mask, &changes );
569 /***********************************************************************
570 * X11DRV_sync_client_window_position
572 * Synchronize the X client window position with the Windows one
574 int X11DRV_sync_client_window_position( Display *display, WND *win )
576 XWindowChanges changes;
578 struct x11drv_win_data *data = win->pDriverData;
579 RECT client_rect = win->rectClient;
581 OffsetRect( &client_rect, -data->whole_rect.left, -data->whole_rect.top );
583 if ((mask = get_window_changes( &changes, &data->client_rect, &client_rect )))
585 BOOL was_mapped = is_client_window_mapped( win );
587 TRACE( "setting win %lx pos %ld,%ld,%ldx%ld (was %ld,%ld,%ldx%ld) after %lx changes=%x\n",
588 data->client_window, client_rect.left, client_rect.top,
589 client_rect.right - client_rect.left, client_rect.bottom - client_rect.top,
590 data->client_rect.left, data->client_rect.top,
591 data->client_rect.right - data->client_rect.left,
592 data->client_rect.bottom - data->client_rect.top,
593 changes.sibling, mask );
594 data->client_rect = client_rect;
596 XSync( gdi_display, False ); /* flush graphics operations before moving the window */
597 if (was_mapped && !is_client_window_mapped( win ))
598 XUnmapWindow( display, data->client_window );
599 XConfigureWindow( display, data->client_window, mask, &changes );
600 if (!was_mapped && is_client_window_mapped( win ))
601 XMapWindow( display, data->client_window );
608 /***********************************************************************
609 * X11DRV_register_window
611 * Associate an X window to a HWND.
613 void X11DRV_register_window( Display *display, HWND hwnd, struct x11drv_win_data *data )
616 XSaveContext( display, data->whole_window, winContext, (char *)hwnd );
617 XSaveContext( display, data->client_window, winContext, (char *)hwnd );
622 /**********************************************************************
625 static void create_desktop( Display *display, WND *wndPtr )
627 X11DRV_WND_DATA *data = wndPtr->pDriverData;
630 winContext = XUniqueContext();
631 wmProtocols = XInternAtom( display, "WM_PROTOCOLS", False );
632 wmDeleteWindow = XInternAtom( display, "WM_DELETE_WINDOW", False );
633 if (use_take_focus) wmTakeFocus = XInternAtom( display, "WM_TAKE_FOCUS", False );
634 dndProtocol = XInternAtom( display, "DndProtocol" , False );
635 dndSelection = XInternAtom( display, "DndSelection" , False );
636 wmChangeState = XInternAtom( display, "WM_CHANGE_STATE", False );
637 mwmHints = XInternAtom( display, _XA_MWM_HINTS, False );
638 kwmDockWindow = XInternAtom( display, "KWM_DOCKWINDOW", False );
639 _kde_net_wm_system_tray_window_for = XInternAtom( display, "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR", False );
640 netwmPid = XInternAtom( display, "_NET_WM_PID", False );
641 netwmPing = XInternAtom( display, "_NET_WM_PING", False );
644 whole_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_whole_window" ));
645 client_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_client_window" ));
646 icon_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_icon_window" ));
648 data->whole_window = data->client_window = root_window;
649 data->whole_rect = data->client_rect = wndPtr->rectWindow;
651 SetPropA( wndPtr->hwndSelf, whole_window_atom, (HANDLE)root_window );
652 SetPropA( wndPtr->hwndSelf, client_window_atom, (HANDLE)root_window );
653 SetPropA( wndPtr->hwndSelf, "__wine_x11_visual_id", (HANDLE)XVisualIDFromVisual(visual) );
655 if (root_window != DefaultRootWindow(display)) X11DRV_create_desktop_thread();
659 /**********************************************************************
660 * create_whole_window
662 * Create the whole X window for a given window
664 static Window create_whole_window( Display *display, WND *win )
666 struct x11drv_win_data *data = win->pDriverData;
668 XSetWindowAttributes attr;
671 BOOL is_top_level = is_window_top_level( win );
673 rect = win->rectWindow;
674 X11DRV_window_to_X_rect( win, &rect );
676 if (!(cx = rect.right - rect.left)) cx = 1;
677 if (!(cy = rect.bottom - rect.top)) cy = 1;
679 parent = X11DRV_get_client_window( win->parent );
683 mask = get_window_attributes( display, win, &attr );
685 /* set the attributes that don't change over the lifetime of the window */
686 attr.bit_gravity = ForgetGravity;
687 attr.win_gravity = NorthWestGravity;
688 attr.backing_store = NotUseful/*WhenMapped*/;
689 mask |= CWBitGravity | CWWinGravity | CWBackingStore;
691 data->whole_rect = rect;
692 data->whole_window = XCreateWindow( display, parent, rect.left, rect.top, cx, cy,
693 0, screen_depth, InputOutput, visual,
696 if (!data->whole_window)
704 XIM xim = x11drv_thread_data()->xim;
705 if (xim) data->xic = XCreateIC( xim,
706 XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
707 XNClientWindow, data->whole_window,
708 XNFocusWindow, data->whole_window,
712 /* non-maximized child must be at bottom of Z order */
713 if ((win->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
715 XWindowChanges changes;
716 changes.stack_mode = Below;
717 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
722 if (is_top_level) X11DRV_set_wm_hints( display, win );
724 return data->whole_window;
728 /**********************************************************************
729 * create_client_window
731 * Create the client window for a given window
733 static Window create_client_window( Display *display, WND *win )
735 struct x11drv_win_data *data = win->pDriverData;
736 RECT rect = data->whole_rect;
737 XSetWindowAttributes attr;
739 OffsetRect( &rect, -data->whole_rect.left, -data->whole_rect.top );
740 data->client_rect = rect;
742 attr.event_mask = (ExposureMask | PointerMotionMask |
743 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
744 attr.bit_gravity = (win->clsStyle & (CS_VREDRAW | CS_HREDRAW)) ?
745 ForgetGravity : NorthWestGravity;
746 attr.backing_store = NotUseful/*WhenMapped*/;
749 data->client_window = XCreateWindow( display, data->whole_window, 0, 0,
750 max( rect.right - rect.left, 1 ),
751 max( rect.bottom - rect.top, 1 ),
754 CWEventMask | CWBitGravity | CWBackingStore, &attr );
755 if (data->client_window && is_client_window_mapped( win ))
756 XMapWindow( display, data->client_window );
758 return data->client_window;
762 /*****************************************************************
763 * SetWindowText (X11DRV.@)
765 BOOL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
767 Display *display = thread_display();
771 static UINT text_cp = (UINT)-1;
774 if ((win = X11DRV_get_whole_window( hwnd )))
776 if (text_cp == (UINT)-1)
781 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\x11drv", &hkey))
784 DWORD type, count = sizeof(buffer);
785 if(!RegQueryValueExA(hkey, "TextCP", 0, &type, buffer, &count))
786 text_cp = atoi(buffer);
789 TRACE("text_cp = %u\n", text_cp);
792 /* allocate new buffer for window text */
793 count = WideCharToMultiByte(text_cp, 0, text, -1, NULL, 0, NULL, NULL);
794 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count )))
796 ERR("Not enough memory for window text\n");
799 WideCharToMultiByte(text_cp, 0, text, -1, buffer, count, NULL, NULL);
801 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
802 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
804 ERR("Not enough memory for window text in UTF-8\n");
807 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
810 XStoreName( display, win, buffer );
811 XSetIconName( display, win, buffer );
813 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
814 according to the standard
815 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
817 XChangeProperty( display, win,
818 XInternAtom(display, "_NET_WM_NAME", False),
819 XInternAtom(display, "UTF8_STRING", False),
820 8, PropModeReplace, (unsigned char *) utf8_buffer,
824 HeapFree( GetProcessHeap(), 0, utf8_buffer );
825 HeapFree( GetProcessHeap(), 0, buffer );
831 /***********************************************************************
832 * DestroyWindow (X11DRV.@)
834 BOOL X11DRV_DestroyWindow( HWND hwnd )
836 struct x11drv_thread_data *thread_data = x11drv_thread_data();
837 Display *display = thread_data->display;
838 WND *wndPtr = WIN_GetPtr( hwnd );
839 X11DRV_WND_DATA *data = wndPtr->pDriverData;
841 if (!data) goto done;
843 if (data->whole_window)
845 TRACE( "win %p xwin %lx/%lx\n", hwnd, data->whole_window, data->client_window );
846 if (thread_data->cursor_window == data->whole_window) thread_data->cursor_window = None;
847 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
849 XSync( gdi_display, False ); /* flush any reference to this drawable in GDI queue */
850 XDeleteContext( display, data->whole_window, winContext );
851 XDeleteContext( display, data->client_window, winContext );
852 XDestroyWindow( display, data->whole_window ); /* this destroys client too */
855 XUnsetICFocus( data->xic );
856 XDestroyIC( data->xic );
858 destroy_icon_window( display, wndPtr );
862 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
863 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
864 HeapFree( GetProcessHeap(), 0, data );
865 wndPtr->pDriverData = NULL;
867 WIN_ReleasePtr( wndPtr );
872 /**********************************************************************
873 * CreateWindow (X11DRV.@)
875 BOOL X11DRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
878 Display *display = thread_display();
880 struct x11drv_win_data *data;
887 ERR( "invalid window width %d\n", cs->cx );
892 ERR( "invalid window height %d\n", cs->cx );
896 if (!(data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data)))) return FALSE;
897 data->whole_window = 0;
898 data->client_window = 0;
899 data->icon_window = 0;
901 data->hWMIconBitmap = 0;
902 data->hWMIconMask = 0;
904 wndPtr = WIN_GetPtr( hwnd );
905 wndPtr->pDriverData = data;
907 /* initialize the dimensions before sending WM_GETMINMAXINFO */
908 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
909 WIN_SetRectangles( hwnd, &rect, &rect );
913 create_desktop( display, wndPtr );
914 WIN_ReleasePtr( wndPtr );
918 if (!create_whole_window( display, wndPtr )) goto failed;
919 if (!create_client_window( display, wndPtr )) goto failed;
920 TSXSync( display, False );
922 SetPropA( hwnd, whole_window_atom, (HANDLE)data->whole_window );
923 SetPropA( hwnd, client_window_atom, (HANDLE)data->client_window );
925 /* Call the WH_CBT hook */
927 hwndLinkAfter = ((cs->style & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
930 cbtc.hwndInsertAfter = hwndLinkAfter;
931 if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode ))
933 TRACE("CBT-hook returned !0\n");
937 /* Send the WM_GETMINMAXINFO message and fix the size if needed */
938 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
940 POINT maxSize, maxPos, minTrack, maxTrack;
942 WIN_ReleasePtr( wndPtr );
943 WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
944 if (maxSize.x < cs->cx) cs->cx = maxSize.x;
945 if (maxSize.y < cs->cy) cs->cy = maxSize.y;
946 if (cs->cx < minTrack.x ) cs->cx = minTrack.x;
947 if (cs->cy < minTrack.y ) cs->cy = minTrack.y;
948 if (cs->cx < 0) cs->cx = 0;
949 if (cs->cy < 0) cs->cy = 0;
951 if (!(wndPtr = WIN_GetPtr( hwnd ))) return FALSE;
952 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
953 WIN_SetRectangles( hwnd, &rect, &rect );
954 X11DRV_sync_whole_window_position( display, wndPtr, 0 );
956 WIN_ReleasePtr( wndPtr );
958 /* send WM_NCCREATE */
959 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cs->cx, cs->cy );
961 ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
963 ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
966 WARN("aborted by WM_xxCREATE!\n");
970 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
972 X11DRV_sync_window_style( display, wndPtr );
974 /* send WM_NCCALCSIZE */
975 rect = wndPtr->rectWindow;
976 WIN_ReleasePtr( wndPtr );
977 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&rect );
979 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
980 if (rect.left > rect.right || rect.top > rect.bottom) rect = wndPtr->rectWindow;
981 WIN_SetRectangles( hwnd, &wndPtr->rectWindow, &rect );
982 X11DRV_sync_client_window_position( display, wndPtr );
983 X11DRV_register_window( display, hwnd, data );
985 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",
986 hwnd, wndPtr->rectWindow.left, wndPtr->rectWindow.top,
987 wndPtr->rectWindow.right, wndPtr->rectWindow.bottom,
988 wndPtr->rectClient.left, wndPtr->rectClient.top,
989 wndPtr->rectClient.right, wndPtr->rectClient.bottom,
990 data->whole_rect.left, data->whole_rect.top,
991 data->whole_rect.right, data->whole_rect.bottom,
992 data->client_rect.left, data->client_rect.top,
993 data->client_rect.right, data->client_rect.bottom,
994 (unsigned int)data->whole_window, (unsigned int)data->client_window );
996 if ((wndPtr->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
997 WIN_LinkWindow( hwnd, wndPtr->parent, HWND_BOTTOM );
999 WIN_LinkWindow( hwnd, wndPtr->parent, HWND_TOP );
1001 WIN_ReleasePtr( wndPtr );
1004 ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1006 ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1010 WIN_UnlinkWindow( hwnd );
1014 /* Send the size messages */
1016 if (!(wndPtr = WIN_FindWndPtr(hwnd))) return FALSE;
1017 if (!(wndPtr->flags & WIN_NEED_SIZE))
1019 /* send it anyway */
1020 if (((wndPtr->rectClient.right-wndPtr->rectClient.left) <0)
1021 ||((wndPtr->rectClient.bottom-wndPtr->rectClient.top)<0))
1022 WARN("sending bogus WM_SIZE message 0x%08lx\n",
1023 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
1024 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
1025 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1026 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
1027 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
1028 SendMessageW( hwnd, WM_MOVE, 0,
1029 MAKELONG( wndPtr->rectClient.left, wndPtr->rectClient.top ) );
1032 /* Show the window, maximizing or minimizing if needed */
1034 if (wndPtr->dwStyle & (WS_MINIMIZE | WS_MAXIMIZE))
1036 extern UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect ); /*FIXME*/
1039 UINT swFlag = (wndPtr->dwStyle & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1040 WIN_SetStyle( hwnd, wndPtr->dwStyle & ~(WS_MAXIMIZE | WS_MINIMIZE) );
1041 WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1042 swFlag = ((wndPtr->dwStyle & WS_CHILD) || GetActiveWindow())
1043 ? SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED
1044 : SWP_NOZORDER | SWP_FRAMECHANGED;
1045 SetWindowPos( hwnd, 0, newPos.left, newPos.top,
1046 newPos.right, newPos.bottom, swFlag );
1049 /* if the window was made visible set create struct flag so that
1050 * we do a proper ShowWindow later on */
1051 if (wndPtr->dwStyle & WS_VISIBLE) cs->style |= WS_VISIBLE;
1053 WIN_ReleaseWndPtr( wndPtr );
1058 X11DRV_DestroyWindow( hwnd );
1059 if (wndPtr) WIN_ReleasePtr( wndPtr );
1064 /***********************************************************************
1065 * X11DRV_get_client_window
1067 * Return the X window associated with the client area of a window
1069 Window X11DRV_get_client_window( HWND hwnd )
1072 WND *win = WIN_GetPtr( hwnd );
1074 if (win == WND_OTHER_PROCESS)
1075 return (Window)GetPropA( hwnd, client_window_atom );
1079 struct x11drv_win_data *data = win->pDriverData;
1080 ret = data->client_window;
1081 WIN_ReleasePtr( win );
1087 /***********************************************************************
1088 * X11DRV_get_whole_window
1090 * Return the X window associated with the full area of a window
1092 Window X11DRV_get_whole_window( HWND hwnd )
1095 WND *win = WIN_GetPtr( hwnd );
1097 if (win == WND_OTHER_PROCESS)
1098 return (Window)GetPropA( hwnd, whole_window_atom );
1102 struct x11drv_win_data *data = win->pDriverData;
1103 ret = data->whole_window;
1104 WIN_ReleasePtr( win );
1110 /***********************************************************************
1113 * Return the X input context associated with a window
1115 XIC X11DRV_get_ic( HWND hwnd )
1118 WND *win = WIN_GetPtr( hwnd );
1120 if (win && win != WND_OTHER_PROCESS)
1122 struct x11drv_win_data *data = win->pDriverData;
1124 WIN_ReleasePtr( win );
1130 /*****************************************************************
1131 * SetParent (X11DRV.@)
1133 HWND X11DRV_SetParent( HWND hwnd, HWND parent )
1135 Display *display = thread_display();
1139 /* Windows hides the window first, then shows it again
1140 * including the WM_SHOWWINDOW messages and all */
1141 BOOL was_visible = ShowWindow( hwnd, SW_HIDE );
1143 if (!IsWindow( parent )) return 0;
1144 if (!(wndPtr = WIN_GetPtr(hwnd)) || wndPtr == WND_OTHER_PROCESS) return 0;
1146 retvalue = wndPtr->parent; /* old parent */
1147 if (parent != retvalue)
1149 struct x11drv_win_data *data = wndPtr->pDriverData;
1151 WIN_LinkWindow( hwnd, parent, HWND_TOP );
1153 if (parent != GetDesktopWindow()) /* a child window */
1155 if (!(wndPtr->dwStyle & WS_CHILD))
1157 HMENU menu = (HMENU)SetWindowLongW( hwnd, GWL_ID, 0 );
1158 if (menu) DestroyMenu( menu );
1162 if (is_window_top_level( wndPtr )) X11DRV_set_wm_hints( display, wndPtr );
1164 X11DRV_sync_window_style( display, wndPtr );
1165 XReparentWindow( display, data->whole_window, X11DRV_get_client_window(parent),
1166 data->whole_rect.left, data->whole_rect.top );
1167 wine_tsx11_unlock();
1169 WIN_ReleasePtr( wndPtr );
1171 /* SetParent additionally needs to make hwnd the topmost window
1172 in the x-order and send the expected WM_WINDOWPOSCHANGING and
1173 WM_WINDOWPOSCHANGED notification messages.
1175 SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0,
1176 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | (was_visible ? SWP_SHOWWINDOW : 0) );
1177 /* FIXME: a WM_MOVE is also generated (in the DefWindowProc handler
1178 * for WM_WINDOWPOSCHANGED) in Windows, should probably remove SWP_NOMOVE */
1184 /*****************************************************************
1185 * SetFocus (X11DRV.@)
1188 * Explicit colormap management seems to work only with OLVWM.
1190 void X11DRV_SetFocus( HWND hwnd )
1192 Display *display = thread_display();
1193 XWindowAttributes win_attr;
1196 /* Only mess with the X focus if there's */
1197 /* no desktop window and if the window is not managed by the WM. */
1198 if (root_window != DefaultRootWindow(display)) return;
1200 if (!hwnd) /* If setting the focus to 0, uninstall the colormap */
1202 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1203 TSXUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1207 hwnd = GetAncestor( hwnd, GA_ROOT );
1208 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_MANAGED) return;
1209 if (!(win = X11DRV_get_whole_window( hwnd ))) return;
1211 /* Set X focus and install colormap */
1213 if (XGetWindowAttributes( display, win, &win_attr ) &&
1214 (win_attr.map_state == IsViewable))
1216 /* If window is not viewable, don't change anything */
1218 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1219 /* FIXME: this is not entirely correct */
1220 XSetInputFocus( display, win, RevertToParent,
1221 /*CurrentTime*/ GetMessageTime() + X11DRV_server_startticks );
1222 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1223 XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1225 wine_tsx11_unlock();
1229 /**********************************************************************
1230 * SetWindowIcon (X11DRV.@)
1232 * hIcon or hIconSm has changed (or is being initialised for the
1233 * first time). Complete the X11 driver-specific initialisation
1234 * and set the window hints.
1236 * This is not entirely correct, may need to create
1237 * an icon window and set the pixmap as a background
1239 HICON X11DRV_SetWindowIcon( HWND hwnd, HICON icon, BOOL small )
1242 Display *display = thread_display();
1243 HICON old = (HICON)SetClassLongW(hwnd, small ? GCL_HICONSM : GCL_HICON, (LONG)icon );
1245 SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOSIZE |
1246 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
1248 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return old;
1250 if (wndPtr->dwExStyle & WS_EX_MANAGED)
1252 Window win = get_whole_window(wndPtr);
1256 if (!(wm_hints = XGetWMHints( display, win ))) wm_hints = XAllocWMHints();
1257 wine_tsx11_unlock();
1260 set_icon_hints( display, wndPtr, wm_hints );
1262 XSetWMHints( display, win, wm_hints );
1264 wine_tsx11_unlock();
1267 WIN_ReleasePtr( wndPtr );