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 );
562 if (!next) /* bottom child */
564 changes.stack_mode = Below;
569 changes.stack_mode = Above;
570 changes.sibling = X11DRV_get_whole_window(next);
571 mask |= CWStackMode | CWSibling;
576 data->whole_rect = whole_rect;
580 TRACE( "setting win %lx pos %ld,%ld,%ldx%ld after %lx changes=%x\n",
581 data->whole_window, whole_rect.left, whole_rect.top,
582 whole_rect.right - whole_rect.left, whole_rect.bottom - whole_rect.top,
583 changes.sibling, mask );
585 XSync( gdi_display, False ); /* flush graphics operations before moving the window */
586 if (is_window_top_level( win ))
588 if (mask & (CWWidth|CWHeight)) set_size_hints( display, win );
589 XReconfigureWMWindow( display, data->whole_window,
590 DefaultScreen(display), mask, &changes );
592 else XConfigureWindow( display, data->whole_window, mask, &changes );
599 /***********************************************************************
600 * X11DRV_sync_client_window_position
602 * Synchronize the X client window position with the Windows one
604 int X11DRV_sync_client_window_position( Display *display, WND *win )
606 XWindowChanges changes;
608 struct x11drv_win_data *data = win->pDriverData;
609 RECT client_rect = win->rectClient;
611 OffsetRect( &client_rect, -data->whole_rect.left, -data->whole_rect.top );
613 if ((mask = get_window_changes( &changes, &data->client_rect, &client_rect )))
615 BOOL was_mapped = is_client_window_mapped( win );
617 TRACE( "setting win %lx pos %ld,%ld,%ldx%ld (was %ld,%ld,%ldx%ld) after %lx changes=%x\n",
618 data->client_window, client_rect.left, client_rect.top,
619 client_rect.right - client_rect.left, client_rect.bottom - client_rect.top,
620 data->client_rect.left, data->client_rect.top,
621 data->client_rect.right - data->client_rect.left,
622 data->client_rect.bottom - data->client_rect.top,
623 changes.sibling, mask );
624 data->client_rect = client_rect;
626 XSync( gdi_display, False ); /* flush graphics operations before moving the window */
627 if (was_mapped && !is_client_window_mapped( win ))
628 XUnmapWindow( display, data->client_window );
629 XConfigureWindow( display, data->client_window, mask, &changes );
630 if (!was_mapped && is_client_window_mapped( win ))
631 XMapWindow( display, data->client_window );
638 /***********************************************************************
639 * X11DRV_register_window
641 * Associate an X window to a HWND.
643 void X11DRV_register_window( Display *display, HWND hwnd, struct x11drv_win_data *data )
646 XSaveContext( display, data->whole_window, winContext, (char *)hwnd );
647 XSaveContext( display, data->client_window, winContext, (char *)hwnd );
652 /**********************************************************************
655 static void create_desktop( Display *display, WND *wndPtr )
657 X11DRV_WND_DATA *data = wndPtr->pDriverData;
660 winContext = XUniqueContext();
661 wmProtocols = XInternAtom( display, "WM_PROTOCOLS", False );
662 wmDeleteWindow = XInternAtom( display, "WM_DELETE_WINDOW", False );
663 if (use_take_focus) wmTakeFocus = XInternAtom( display, "WM_TAKE_FOCUS", False );
664 dndProtocol = XInternAtom( display, "DndProtocol" , False );
665 dndSelection = XInternAtom( display, "DndSelection" , False );
666 wmChangeState = XInternAtom( display, "WM_CHANGE_STATE", False );
667 mwmHints = XInternAtom( display, _XA_MWM_HINTS, False );
668 kwmDockWindow = XInternAtom( display, "KWM_DOCKWINDOW", False );
669 _kde_net_wm_system_tray_window_for = XInternAtom( display, "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR", False );
670 netwmPid = XInternAtom( display, "_NET_WM_PID", False );
671 netwmPing = XInternAtom( display, "_NET_WM_PING", False );
674 whole_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_whole_window" ));
675 client_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_client_window" ));
676 icon_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_icon_window" ));
678 data->whole_window = data->client_window = root_window;
679 data->whole_rect = data->client_rect = wndPtr->rectWindow;
681 SetPropA( wndPtr->hwndSelf, whole_window_atom, (HANDLE)root_window );
682 SetPropA( wndPtr->hwndSelf, client_window_atom, (HANDLE)root_window );
683 SetPropA( wndPtr->hwndSelf, "__wine_x11_visual_id", (HANDLE)XVisualIDFromVisual(visual) );
685 if (root_window != DefaultRootWindow(display)) X11DRV_create_desktop_thread();
689 /**********************************************************************
690 * create_whole_window
692 * Create the whole X window for a given window
694 static Window create_whole_window( Display *display, WND *win )
696 struct x11drv_win_data *data = win->pDriverData;
698 XSetWindowAttributes attr;
701 BOOL is_top_level = is_window_top_level( win );
703 rect = win->rectWindow;
704 X11DRV_window_to_X_rect( win, &rect );
706 if (!(cx = rect.right - rect.left)) cx = 1;
707 if (!(cy = rect.bottom - rect.top)) cy = 1;
709 parent = X11DRV_get_client_window( win->parent );
713 mask = get_window_attributes( display, win, &attr );
715 /* set the attributes that don't change over the lifetime of the window */
716 attr.bit_gravity = ForgetGravity;
717 attr.win_gravity = NorthWestGravity;
718 attr.backing_store = NotUseful/*WhenMapped*/;
719 mask |= CWBitGravity | CWWinGravity | CWBackingStore;
721 data->whole_rect = rect;
722 data->whole_window = XCreateWindow( display, parent, rect.left, rect.top, cx, cy,
723 0, screen_depth, InputOutput, visual,
726 if (!data->whole_window)
734 XIM xim = x11drv_thread_data()->xim;
735 if (xim) data->xic = XCreateIC( xim,
736 XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
737 XNClientWindow, data->whole_window,
738 XNFocusWindow, data->whole_window,
742 /* non-maximized child must be at bottom of Z order */
743 if ((win->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
745 XWindowChanges changes;
746 changes.stack_mode = Below;
747 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
752 if (is_top_level) X11DRV_set_wm_hints( display, win );
754 return data->whole_window;
758 /**********************************************************************
759 * create_client_window
761 * Create the client window for a given window
763 static Window create_client_window( Display *display, WND *win )
765 struct x11drv_win_data *data = win->pDriverData;
766 RECT rect = data->whole_rect;
767 XSetWindowAttributes attr;
769 OffsetRect( &rect, -data->whole_rect.left, -data->whole_rect.top );
770 data->client_rect = rect;
772 attr.event_mask = (ExposureMask | PointerMotionMask |
773 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
774 attr.bit_gravity = (win->clsStyle & (CS_VREDRAW | CS_HREDRAW)) ?
775 ForgetGravity : NorthWestGravity;
776 attr.backing_store = NotUseful/*WhenMapped*/;
779 data->client_window = XCreateWindow( display, data->whole_window, 0, 0,
780 max( rect.right - rect.left, 1 ),
781 max( rect.bottom - rect.top, 1 ),
784 CWEventMask | CWBitGravity | CWBackingStore, &attr );
785 if (data->client_window && is_client_window_mapped( win ))
786 XMapWindow( display, data->client_window );
788 return data->client_window;
792 /*****************************************************************
793 * SetWindowText (X11DRV.@)
795 BOOL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
797 Display *display = thread_display();
804 if ((win = X11DRV_get_whole_window( hwnd )))
806 /* allocate new buffer for window text */
807 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
808 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count )))
810 ERR("Not enough memory for window text\n");
813 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
815 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
816 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
818 ERR("Not enough memory for window text in UTF-8\n");
821 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
824 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
826 XSetWMName( display, win, &prop );
827 XSetWMIconName( display, win, &prop );
831 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
832 according to the standard
833 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
835 XChangeProperty( display, win,
836 XInternAtom(display, "_NET_WM_NAME", False),
837 XInternAtom(display, "UTF8_STRING", False),
838 8, PropModeReplace, (unsigned char *) utf8_buffer,
842 HeapFree( GetProcessHeap(), 0, utf8_buffer );
843 HeapFree( GetProcessHeap(), 0, buffer );
849 /***********************************************************************
850 * DestroyWindow (X11DRV.@)
852 BOOL X11DRV_DestroyWindow( HWND hwnd )
854 struct x11drv_thread_data *thread_data = x11drv_thread_data();
855 Display *display = thread_data->display;
856 WND *wndPtr = WIN_GetPtr( hwnd );
857 X11DRV_WND_DATA *data = wndPtr->pDriverData;
859 if (!data) goto done;
861 if (data->whole_window)
863 TRACE( "win %p xwin %lx/%lx\n", hwnd, data->whole_window, data->client_window );
864 if (thread_data->cursor_window == data->whole_window) thread_data->cursor_window = None;
865 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
867 XSync( gdi_display, False ); /* flush any reference to this drawable in GDI queue */
868 XDeleteContext( display, data->whole_window, winContext );
869 XDeleteContext( display, data->client_window, winContext );
870 XDestroyWindow( display, data->whole_window ); /* this destroys client too */
873 XUnsetICFocus( data->xic );
874 XDestroyIC( data->xic );
876 destroy_icon_window( display, wndPtr );
880 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
881 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
882 HeapFree( GetProcessHeap(), 0, data );
883 wndPtr->pDriverData = NULL;
885 WIN_ReleasePtr( wndPtr );
890 /**********************************************************************
891 * CreateWindow (X11DRV.@)
893 BOOL X11DRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
896 Display *display = thread_display();
898 struct x11drv_win_data *data;
905 ERR( "invalid window width %d\n", cs->cx );
910 ERR( "invalid window height %d\n", cs->cx );
914 if (!(data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data)))) return FALSE;
915 data->whole_window = 0;
916 data->client_window = 0;
917 data->icon_window = 0;
919 data->hWMIconBitmap = 0;
920 data->hWMIconMask = 0;
922 wndPtr = WIN_GetPtr( hwnd );
923 wndPtr->pDriverData = data;
925 /* initialize the dimensions before sending WM_GETMINMAXINFO */
926 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
927 WIN_SetRectangles( hwnd, &rect, &rect );
931 create_desktop( display, wndPtr );
932 WIN_ReleasePtr( wndPtr );
936 if (!create_whole_window( display, wndPtr )) goto failed;
937 if (!create_client_window( display, wndPtr )) goto failed;
938 TSXSync( display, False );
940 SetPropA( hwnd, whole_window_atom, (HANDLE)data->whole_window );
941 SetPropA( hwnd, client_window_atom, (HANDLE)data->client_window );
943 /* Call the WH_CBT hook */
945 hwndLinkAfter = ((cs->style & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
948 cbtc.hwndInsertAfter = hwndLinkAfter;
949 if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode ))
951 TRACE("CBT-hook returned !0\n");
955 /* Send the WM_GETMINMAXINFO message and fix the size if needed */
956 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
958 POINT maxSize, maxPos, minTrack, maxTrack;
960 WIN_ReleasePtr( wndPtr );
961 WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
962 if (maxSize.x < cs->cx) cs->cx = maxSize.x;
963 if (maxSize.y < cs->cy) cs->cy = maxSize.y;
964 if (cs->cx < minTrack.x ) cs->cx = minTrack.x;
965 if (cs->cy < minTrack.y ) cs->cy = minTrack.y;
966 if (cs->cx < 0) cs->cx = 0;
967 if (cs->cy < 0) cs->cy = 0;
969 if (!(wndPtr = WIN_GetPtr( hwnd ))) return FALSE;
970 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
971 WIN_SetRectangles( hwnd, &rect, &rect );
972 X11DRV_sync_whole_window_position( display, wndPtr, 0 );
974 WIN_ReleasePtr( wndPtr );
976 /* send WM_NCCREATE */
977 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cs->cx, cs->cy );
979 ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
981 ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
984 WARN("aborted by WM_xxCREATE!\n");
988 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
990 X11DRV_sync_window_style( display, wndPtr );
992 /* send WM_NCCALCSIZE */
993 rect = wndPtr->rectWindow;
994 WIN_ReleasePtr( wndPtr );
995 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&rect );
997 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
998 if (rect.left > rect.right || rect.top > rect.bottom) rect = wndPtr->rectWindow;
999 WIN_SetRectangles( hwnd, &wndPtr->rectWindow, &rect );
1000 X11DRV_sync_client_window_position( display, wndPtr );
1001 X11DRV_register_window( display, hwnd, data );
1003 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",
1004 hwnd, wndPtr->rectWindow.left, wndPtr->rectWindow.top,
1005 wndPtr->rectWindow.right, wndPtr->rectWindow.bottom,
1006 wndPtr->rectClient.left, wndPtr->rectClient.top,
1007 wndPtr->rectClient.right, wndPtr->rectClient.bottom,
1008 data->whole_rect.left, data->whole_rect.top,
1009 data->whole_rect.right, data->whole_rect.bottom,
1010 data->client_rect.left, data->client_rect.top,
1011 data->client_rect.right, data->client_rect.bottom,
1012 (unsigned int)data->whole_window, (unsigned int)data->client_window );
1014 if ((wndPtr->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
1015 WIN_LinkWindow( hwnd, wndPtr->parent, HWND_BOTTOM );
1017 WIN_LinkWindow( hwnd, wndPtr->parent, HWND_TOP );
1019 WIN_ReleasePtr( wndPtr );
1022 ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1024 ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1028 WIN_UnlinkWindow( hwnd );
1032 /* Send the size messages */
1034 if (!(wndPtr = WIN_FindWndPtr(hwnd))) return FALSE;
1035 if (!(wndPtr->flags & WIN_NEED_SIZE))
1037 /* send it anyway */
1038 if (((wndPtr->rectClient.right-wndPtr->rectClient.left) <0)
1039 ||((wndPtr->rectClient.bottom-wndPtr->rectClient.top)<0))
1040 WARN("sending bogus WM_SIZE message 0x%08lx\n",
1041 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
1042 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
1043 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1044 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
1045 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
1046 SendMessageW( hwnd, WM_MOVE, 0,
1047 MAKELONG( wndPtr->rectClient.left, wndPtr->rectClient.top ) );
1050 /* Show the window, maximizing or minimizing if needed */
1052 if (wndPtr->dwStyle & (WS_MINIMIZE | WS_MAXIMIZE))
1054 extern UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect ); /*FIXME*/
1057 UINT swFlag = (wndPtr->dwStyle & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1058 WIN_SetStyle( hwnd, wndPtr->dwStyle & ~(WS_MAXIMIZE | WS_MINIMIZE) );
1059 WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1060 swFlag = ((wndPtr->dwStyle & WS_CHILD) || GetActiveWindow())
1061 ? SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED
1062 : SWP_NOZORDER | SWP_FRAMECHANGED;
1063 SetWindowPos( hwnd, 0, newPos.left, newPos.top,
1064 newPos.right, newPos.bottom, swFlag );
1067 /* if the window was made visible set create struct flag so that
1068 * we do a proper ShowWindow later on */
1069 if (wndPtr->dwStyle & WS_VISIBLE) cs->style |= WS_VISIBLE;
1071 WIN_ReleaseWndPtr( wndPtr );
1076 X11DRV_DestroyWindow( hwnd );
1077 if (wndPtr) WIN_ReleasePtr( wndPtr );
1082 /***********************************************************************
1083 * X11DRV_get_client_window
1085 * Return the X window associated with the client area of a window
1087 Window X11DRV_get_client_window( HWND hwnd )
1090 WND *win = WIN_GetPtr( hwnd );
1092 if (win == WND_OTHER_PROCESS)
1093 return (Window)GetPropA( hwnd, client_window_atom );
1097 struct x11drv_win_data *data = win->pDriverData;
1098 ret = data->client_window;
1099 WIN_ReleasePtr( win );
1105 /***********************************************************************
1106 * X11DRV_get_whole_window
1108 * Return the X window associated with the full area of a window
1110 Window X11DRV_get_whole_window( HWND hwnd )
1113 WND *win = WIN_GetPtr( hwnd );
1115 if (win == WND_OTHER_PROCESS)
1116 return (Window)GetPropA( hwnd, whole_window_atom );
1120 struct x11drv_win_data *data = win->pDriverData;
1121 ret = data->whole_window;
1122 WIN_ReleasePtr( win );
1128 /***********************************************************************
1131 * Return the X input context associated with a window
1133 XIC X11DRV_get_ic( HWND hwnd )
1136 WND *win = WIN_GetPtr( hwnd );
1138 if (win && win != WND_OTHER_PROCESS)
1140 struct x11drv_win_data *data = win->pDriverData;
1142 WIN_ReleasePtr( win );
1148 /*****************************************************************
1149 * SetParent (X11DRV.@)
1151 HWND X11DRV_SetParent( HWND hwnd, HWND parent )
1153 Display *display = thread_display();
1157 /* Windows hides the window first, then shows it again
1158 * including the WM_SHOWWINDOW messages and all */
1159 BOOL was_visible = ShowWindow( hwnd, SW_HIDE );
1161 if (!IsWindow( parent )) return 0;
1162 if (!(wndPtr = WIN_GetPtr(hwnd)) || wndPtr == WND_OTHER_PROCESS) return 0;
1164 retvalue = wndPtr->parent; /* old parent */
1165 if (parent != retvalue)
1167 struct x11drv_win_data *data = wndPtr->pDriverData;
1169 WIN_LinkWindow( hwnd, parent, HWND_TOP );
1171 if (parent != GetDesktopWindow()) /* a child window */
1173 if (!(wndPtr->dwStyle & WS_CHILD))
1175 HMENU menu = (HMENU)SetWindowLongW( hwnd, GWL_ID, 0 );
1176 if (menu) DestroyMenu( menu );
1180 if (is_window_top_level( wndPtr )) X11DRV_set_wm_hints( display, wndPtr );
1182 X11DRV_sync_window_style( display, wndPtr );
1183 XReparentWindow( display, data->whole_window, X11DRV_get_client_window(parent),
1184 data->whole_rect.left, data->whole_rect.top );
1185 wine_tsx11_unlock();
1187 WIN_ReleasePtr( wndPtr );
1189 /* SetParent additionally needs to make hwnd the topmost window
1190 in the x-order and send the expected WM_WINDOWPOSCHANGING and
1191 WM_WINDOWPOSCHANGED notification messages.
1193 SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0,
1194 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | (was_visible ? SWP_SHOWWINDOW : 0) );
1195 /* FIXME: a WM_MOVE is also generated (in the DefWindowProc handler
1196 * for WM_WINDOWPOSCHANGED) in Windows, should probably remove SWP_NOMOVE */
1202 /*****************************************************************
1203 * SetFocus (X11DRV.@)
1206 * Explicit colormap management seems to work only with OLVWM.
1208 void X11DRV_SetFocus( HWND hwnd )
1210 Display *display = thread_display();
1211 XWindowAttributes win_attr;
1214 /* Only mess with the X focus if there's */
1215 /* no desktop window and if the window is not managed by the WM. */
1216 if (root_window != DefaultRootWindow(display)) return;
1218 if (!hwnd) /* If setting the focus to 0, uninstall the colormap */
1220 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1221 TSXUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1225 hwnd = GetAncestor( hwnd, GA_ROOT );
1226 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_MANAGED) return;
1227 if (!(win = X11DRV_get_whole_window( hwnd ))) return;
1229 /* Set X focus and install colormap */
1231 if (XGetWindowAttributes( display, win, &win_attr ) &&
1232 (win_attr.map_state == IsViewable))
1234 /* If window is not viewable, don't change anything */
1236 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1237 /* FIXME: this is not entirely correct */
1238 XSetInputFocus( display, win, RevertToParent,
1239 /*CurrentTime*/ GetMessageTime() + X11DRV_server_startticks );
1240 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1241 XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1243 wine_tsx11_unlock();
1247 /**********************************************************************
1248 * SetWindowIcon (X11DRV.@)
1250 * hIcon or hIconSm has changed (or is being initialised for the
1251 * first time). Complete the X11 driver-specific initialisation
1252 * and set the window hints.
1254 * This is not entirely correct, may need to create
1255 * an icon window and set the pixmap as a background
1257 HICON X11DRV_SetWindowIcon( HWND hwnd, HICON icon, BOOL small )
1260 Display *display = thread_display();
1261 HICON old = (HICON)SetClassLongW(hwnd, small ? GCL_HICONSM : GCL_HICON, (LONG)icon );
1263 SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOSIZE |
1264 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
1266 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return old;
1268 if (wndPtr->dwExStyle & WS_EX_MANAGED)
1270 Window win = get_whole_window(wndPtr);
1274 if (!(wm_hints = XGetWMHints( display, win ))) wm_hints = XAllocWMHints();
1275 wine_tsx11_unlock();
1278 set_icon_hints( display, wndPtr, wm_hints );
1280 XSetWMHints( display, win, wm_hints );
1282 wine_tsx11_unlock();
1285 WIN_ReleasePtr( wndPtr );