Added LGPL standard comment, and copyright notices where necessary.
[wine/multimedia.git] / dlls / x11drv / window.c
blob1c1bc4f811c0d36388f1050cab5dccc492ab0a0d
1 /*
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
23 #include "config.h"
25 #include <stdlib.h>
27 #include "ts_xlib.h"
28 #include "ts_xutil.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winreg.h"
33 #include "winuser.h"
34 #include "wine/unicode.h"
36 #include "wine/debug.h"
37 #include "x11drv.h"
38 #include "win.h"
39 #include "winpos.h"
40 #include "dce.h"
41 #include "options.h"
42 #include "hook.h"
43 #include "mwm.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
47 extern Pixmap X11DRV_BITMAP_Pixmap( HBITMAP );
49 #define HAS_DLGFRAME(style,exStyle) \
50 (((exStyle) & WS_EX_DLGMODALFRAME) || \
51 (((style) & WS_DLGFRAME) && !((style) & WS_THICKFRAME)))
53 /* X context to associate a hwnd to an X window */
54 XContext winContext = 0;
56 Atom wmProtocols = None;
57 Atom wmDeleteWindow = None;
58 Atom wmTakeFocus = None;
59 Atom dndProtocol = None;
60 Atom dndSelection = None;
61 Atom wmChangeState = None;
62 Atom mwmHints = None;
63 Atom kwmDockWindow = None;
64 Atom _kde_net_wm_system_tray_window_for = None; /* KDE 2 Final */
66 static LPCSTR whole_window_atom;
67 static LPCSTR client_window_atom;
68 static LPCSTR icon_window_atom;
70 /***********************************************************************
71 * is_window_managed
73 * Check if a given window should be managed
75 inline static BOOL is_window_managed( WND *win )
77 if (!Options.managed) 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 */
89 return FALSE;
93 /***********************************************************************
94 * is_window_top_level
96 * Check if a given window is a top level X11 window
98 inline static BOOL is_window_top_level( WND *win )
100 return (root_window == DefaultRootWindow(gdi_display) && win->parent == GetDesktopWindow());
104 /***********************************************************************
105 * is_client_window_mapped
107 * Check if the X client window should be mapped
109 inline static BOOL is_client_window_mapped( WND *win )
111 struct x11drv_win_data *data = win->pDriverData;
112 return !(win->dwStyle & WS_MINIMIZE) && !IsRectEmpty( &data->client_rect );
116 /***********************************************************************
117 * get_window_attributes
119 * Fill the window attributes structure for an X window.
120 * Returned cursor must be freed by caller.
122 static int get_window_attributes( Display *display, WND *win, XSetWindowAttributes *attr )
124 BOOL is_top_level = is_window_top_level( win );
125 BOOL managed = is_top_level && is_window_managed( win );
127 if (managed) WIN_SetExStyle( win->hwndSelf, win->dwExStyle | WS_EX_MANAGED );
128 else WIN_SetExStyle( win->hwndSelf, win->dwExStyle & ~WS_EX_MANAGED );
130 attr->override_redirect = !managed;
131 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
132 attr->save_under = ((win->clsStyle & CS_SAVEBITS) != 0);
133 attr->cursor = None;
134 attr->event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
135 ButtonPressMask | ButtonReleaseMask);
136 if (is_window_top_level( win ))
138 attr->event_mask |= StructureNotifyMask | FocusChangeMask | KeymapStateMask;
139 attr->cursor = X11DRV_GetCursor( display, GlobalLock16(GetCursor()) );
141 return (CWOverrideRedirect | CWSaveUnder | CWEventMask | CWColormap | CWCursor);
145 /***********************************************************************
146 * sync_window_style
148 * Change the X window attributes when the window style has changed.
150 static void sync_window_style( Display *display, WND *win )
152 XSetWindowAttributes attr;
153 int mask;
155 wine_tsx11_lock();
156 mask = get_window_attributes( display, win, &attr );
157 XChangeWindowAttributes( display, get_whole_window(win), mask, &attr );
158 if (attr.cursor) XFreeCursor( display, attr.cursor );
159 wine_tsx11_unlock();
163 /***********************************************************************
164 * get_window_changes
166 * fill the window changes structure
168 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
170 int mask = 0;
172 if (old->right - old->left != new->right - new->left )
174 if (!(changes->width = new->right - new->left)) changes->width = 1;
175 mask |= CWWidth;
177 if (old->bottom - old->top != new->bottom - new->top)
179 if (!(changes->height = new->bottom - new->top)) changes->height = 1;
180 mask |= CWHeight;
182 if (old->left != new->left)
184 changes->x = new->left;
185 mask |= CWX;
187 if (old->top != new->top)
189 changes->y = new->top;
190 mask |= CWY;
192 return mask;
196 /***********************************************************************
197 * create_icon_window
199 static Window create_icon_window( Display *display, WND *win )
201 struct x11drv_win_data *data = win->pDriverData;
202 XSetWindowAttributes attr;
204 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
205 ButtonPressMask | ButtonReleaseMask);
206 attr.bit_gravity = NorthWestGravity;
207 attr.backing_store = NotUseful/*WhenMapped*/;
208 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
210 wine_tsx11_lock();
211 data->icon_window = XCreateWindow( display, root_window, 0, 0,
212 GetSystemMetrics( SM_CXICON ),
213 GetSystemMetrics( SM_CYICON ),
214 0, screen_depth,
215 InputOutput, visual,
216 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
217 XSaveContext( display, data->icon_window, winContext, (char *)win->hwndSelf );
218 wine_tsx11_unlock();
220 TRACE( "created %lx\n", data->icon_window );
221 SetPropA( win->hwndSelf, icon_window_atom, (HANDLE)data->icon_window );
222 return data->icon_window;
227 /***********************************************************************
228 * destroy_icon_window
230 inline static void destroy_icon_window( Display *display, WND *win )
232 struct x11drv_win_data *data = win->pDriverData;
234 if (!data->icon_window) return;
235 wine_tsx11_lock();
236 XDeleteContext( display, data->icon_window, winContext );
237 XDestroyWindow( display, data->icon_window );
238 data->icon_window = 0;
239 wine_tsx11_unlock();
240 RemovePropA( win->hwndSelf, icon_window_atom );
244 /***********************************************************************
245 * set_icon_hints
247 * Set the icon wm hints
249 static void set_icon_hints( Display *display, WND *wndPtr, XWMHints *hints )
251 X11DRV_WND_DATA *data = wndPtr->pDriverData;
252 HICON hIcon = GetClassLongA( wndPtr->hwndSelf, GCL_HICON );
254 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
255 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
256 data->hWMIconBitmap = 0;
257 data->hWMIconMask = 0;
259 if (!(wndPtr->dwExStyle & WS_EX_MANAGED))
261 destroy_icon_window( display, wndPtr );
262 hints->flags &= ~(IconPixmapHint | IconMaskHint | IconWindowHint);
264 else if (!hIcon)
266 if (!data->icon_window) create_icon_window( display, wndPtr );
267 hints->icon_window = data->icon_window;
268 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
270 else
272 HBITMAP hbmOrig;
273 RECT rcMask;
274 BITMAP bmMask;
275 ICONINFO ii;
276 HDC hDC;
278 GetIconInfo(hIcon, &ii);
280 X11DRV_CreateBitmap(ii.hbmMask);
281 X11DRV_CreateBitmap(ii.hbmColor);
283 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
284 rcMask.top = 0;
285 rcMask.left = 0;
286 rcMask.right = bmMask.bmWidth;
287 rcMask.bottom = bmMask.bmHeight;
289 hDC = CreateCompatibleDC(0);
290 hbmOrig = SelectObject(hDC, ii.hbmMask);
291 InvertRect(hDC, &rcMask);
292 SelectObject(hDC, hbmOrig);
293 DeleteDC(hDC);
295 data->hWMIconBitmap = ii.hbmColor;
296 data->hWMIconMask = ii.hbmMask;
298 hints->icon_pixmap = X11DRV_BITMAP_Pixmap(data->hWMIconBitmap);
299 hints->icon_mask = X11DRV_BITMAP_Pixmap(data->hWMIconMask);
300 destroy_icon_window( display, wndPtr );
301 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
306 /***********************************************************************
307 * set_size_hints
309 * set the window size hints
311 static void set_size_hints( Display *display, WND *win )
313 XSizeHints* size_hints;
314 struct x11drv_win_data *data = win->pDriverData;
316 if ((size_hints = XAllocSizeHints()))
318 size_hints->win_gravity = StaticGravity;
319 size_hints->x = data->whole_rect.left;
320 size_hints->y = data->whole_rect.top;
321 size_hints->flags = PWinGravity | PPosition;
323 if (HAS_DLGFRAME( win->dwStyle, win->dwExStyle ))
325 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
326 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
327 size_hints->min_width = size_hints->max_width;
328 size_hints->min_height = size_hints->max_height;
329 size_hints->flags |= PMinSize | PMaxSize;
331 XSetWMNormalHints( display, data->whole_window, size_hints );
332 XFree( size_hints );
337 /***********************************************************************
338 * set_wm_hints
340 * Set the window manager hints for a newly-created window
342 static void set_wm_hints( Display *display, WND *win )
344 struct x11drv_win_data *data = win->pDriverData;
345 Window group_leader;
346 XClassHint *class_hints;
347 XWMHints* wm_hints;
348 Atom protocols[2];
349 int i;
351 wine_tsx11_lock();
353 /* wm protocols */
354 i = 0;
355 protocols[i++] = wmDeleteWindow;
356 if (wmTakeFocus) protocols[i++] = wmTakeFocus;
357 XSetWMProtocols( display, data->whole_window, protocols, i );
359 /* class hints */
360 if ((class_hints = XAllocClassHint()))
362 class_hints->res_name = "wine";
363 class_hints->res_class = "Wine";
364 XSetClassHint( display, data->whole_window, class_hints );
365 XFree( class_hints );
368 /* transient for hint */
369 if (win->owner)
371 Window owner_win = X11DRV_get_whole_window( win->owner );
372 XSetTransientForHint( display, data->whole_window, owner_win );
373 group_leader = owner_win;
375 else group_leader = data->whole_window;
377 /* size hints */
378 set_size_hints( display, win );
380 /* systray properties (KDE only for now) */
381 if (win->dwExStyle & WS_EX_TRAYWINDOW)
383 int val = 1;
384 if (kwmDockWindow != None)
385 XChangeProperty( display, data->whole_window, kwmDockWindow, kwmDockWindow,
386 32, PropModeReplace, (char*)&val, 1 );
387 if (_kde_net_wm_system_tray_window_for != None)
388 XChangeProperty( display, data->whole_window, _kde_net_wm_system_tray_window_for,
389 XA_WINDOW, 32, PropModeReplace, (char*)&data->whole_window, 1 );
392 if (mwmHints != None)
394 MwmHints mwm_hints;
395 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
396 mwm_hints.functions = 0;
397 if ((win->dwStyle & WS_CAPTION) == WS_CAPTION) mwm_hints.functions |= MWM_FUNC_MOVE;
398 if (win->dwStyle & WS_THICKFRAME) mwm_hints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
399 if (win->dwStyle & WS_MINIMIZE) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
400 if (win->dwStyle & WS_MAXIMIZE) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
401 if (win->dwStyle & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
402 mwm_hints.decorations = 0;
403 if ((win->dwStyle & WS_CAPTION) == WS_CAPTION) mwm_hints.decorations |= MWM_DECOR_TITLE;
404 if (win->dwExStyle & WS_EX_DLGMODALFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
405 else if (win->dwStyle & WS_THICKFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
406 else if ((win->dwStyle & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
407 else if (win->dwStyle & WS_BORDER) mwm_hints.decorations |= MWM_DECOR_BORDER;
408 else if (!(win->dwStyle & (WS_CHILD|WS_POPUP))) mwm_hints.decorations |= MWM_DECOR_BORDER;
409 if (win->dwStyle & WS_SYSMENU) mwm_hints.decorations |= MWM_DECOR_MENU;
410 if (win->dwStyle & WS_MINIMIZE) mwm_hints.decorations |= MWM_DECOR_MINIMIZE;
411 if (win->dwStyle & WS_MAXIMIZE) mwm_hints.decorations |= MWM_DECOR_MAXIMIZE;
413 XChangeProperty( display, data->whole_window, mwmHints, mwmHints, 32,
414 PropModeReplace, (char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
417 wine_tsx11_unlock();
419 /* wm hints */
420 if ((wm_hints = TSXAllocWMHints()))
422 wm_hints->flags = InputHint | StateHint | WindowGroupHint;
423 /* use globally active model if take focus is supported,
424 * passive model otherwise (cf. ICCCM) */
425 wm_hints->input = !wmTakeFocus;
427 set_icon_hints( display, win, wm_hints );
429 wm_hints->initial_state = (win->dwStyle & WS_MINIMIZE) ? IconicState : NormalState;
430 wm_hints->window_group = group_leader;
432 wine_tsx11_lock();
433 XSetWMHints( display, data->whole_window, wm_hints );
434 XFree(wm_hints);
435 wine_tsx11_unlock();
440 /***********************************************************************
441 * X11DRV_set_iconic_state
443 * Set the X11 iconic state according to the window style.
445 void X11DRV_set_iconic_state( WND *win )
447 Display *display = thread_display();
448 struct x11drv_win_data *data = win->pDriverData;
449 XWMHints* wm_hints;
450 BOOL iconic = IsIconic( win->hwndSelf );
452 wine_tsx11_lock();
454 if (iconic) XUnmapWindow( display, data->client_window );
455 else if (is_client_window_mapped( win )) XMapWindow( display, data->client_window );
457 if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
458 wm_hints->flags |= StateHint | IconPositionHint;
459 wm_hints->initial_state = iconic ? IconicState : NormalState;
460 wm_hints->icon_x = win->rectWindow.left;
461 wm_hints->icon_y = win->rectWindow.top;
462 XSetWMHints( display, data->whole_window, wm_hints );
464 if (win->dwStyle & WS_VISIBLE)
466 if (iconic)
467 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
468 else
469 if (!IsRectEmpty( &win->rectWindow )) XMapWindow( display, data->whole_window );
472 XFree(wm_hints);
473 wine_tsx11_unlock();
477 /***********************************************************************
478 * X11DRV_window_to_X_rect
480 * Convert a rect from client to X window coordinates
482 void X11DRV_window_to_X_rect( WND *win, RECT *rect )
484 RECT rc;
486 if (!(win->dwExStyle & WS_EX_MANAGED)) return;
487 if (IsRectEmpty( rect )) return;
489 rc.top = rc.bottom = rc.left = rc.right = 0;
491 AdjustWindowRectEx( &rc, win->dwStyle & ~(WS_HSCROLL|WS_VSCROLL), FALSE, win->dwExStyle );
493 rect->left -= rc.left;
494 rect->right -= rc.right;
495 rect->top -= rc.top;
496 rect->bottom -= rc.bottom;
497 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
498 if (rect->left >= rect->right) rect->right = rect->left + 1;
502 /***********************************************************************
503 * X11DRV_X_to_window_rect
505 * Opposite of X11DRV_window_to_X_rect
507 void X11DRV_X_to_window_rect( WND *win, RECT *rect )
509 if (!(win->dwExStyle & WS_EX_MANAGED)) return;
510 if (IsRectEmpty( rect )) return;
512 AdjustWindowRectEx( rect, win->dwStyle & ~(WS_HSCROLL|WS_VSCROLL), FALSE, win->dwExStyle );
514 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
515 if (rect->left >= rect->right) rect->right = rect->left + 1;
519 /***********************************************************************
520 * X11DRV_sync_whole_window_position
522 * Synchronize the X whole window position with the Windows one
524 int X11DRV_sync_whole_window_position( Display *display, WND *win, int zorder )
526 XWindowChanges changes;
527 int mask;
528 struct x11drv_win_data *data = win->pDriverData;
529 RECT whole_rect = win->rectWindow;
531 X11DRV_window_to_X_rect( win, &whole_rect );
532 mask = get_window_changes( &changes, &data->whole_rect, &whole_rect );
534 if (zorder)
536 /* find window that this one must be after */
537 HWND prev = GetWindow( win->hwndSelf, GW_HWNDPREV );
538 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
539 prev = GetWindow( prev, GW_HWNDPREV );
540 if (!prev) /* top child */
542 changes.stack_mode = Above;
543 mask |= CWStackMode;
545 else
547 changes.stack_mode = Below;
548 changes.sibling = X11DRV_get_whole_window(prev);
549 mask |= CWStackMode | CWSibling;
553 data->whole_rect = whole_rect;
555 if (mask)
557 TRACE( "setting win %lx pos %d,%d,%dx%d after %lx changes=%x\n",
558 data->whole_window, whole_rect.left, whole_rect.top,
559 whole_rect.right - whole_rect.left, whole_rect.bottom - whole_rect.top,
560 changes.sibling, mask );
561 wine_tsx11_lock();
562 XSync( gdi_display, False ); /* flush graphics operations before moving the window */
563 if (is_window_top_level( win ))
565 if (mask & (CWWidth|CWHeight)) set_size_hints( display, win );
566 XReconfigureWMWindow( display, data->whole_window,
567 DefaultScreen(display), mask, &changes );
569 else XConfigureWindow( display, data->whole_window, mask, &changes );
570 wine_tsx11_unlock();
572 return mask;
576 /***********************************************************************
577 * X11DRV_sync_client_window_position
579 * Synchronize the X client window position with the Windows one
581 int X11DRV_sync_client_window_position( Display *display, WND *win )
583 XWindowChanges changes;
584 int mask;
585 struct x11drv_win_data *data = win->pDriverData;
586 RECT client_rect = win->rectClient;
588 OffsetRect( &client_rect, -data->whole_rect.left, -data->whole_rect.top );
590 if ((mask = get_window_changes( &changes, &data->client_rect, &client_rect )))
592 BOOL was_mapped = is_client_window_mapped( win );
594 TRACE( "setting win %lx pos %d,%d,%dx%d (was %d,%d,%dx%d) after %lx changes=%x\n",
595 data->client_window, client_rect.left, client_rect.top,
596 client_rect.right - client_rect.left, client_rect.bottom - client_rect.top,
597 data->client_rect.left, data->client_rect.top,
598 data->client_rect.right - data->client_rect.left,
599 data->client_rect.bottom - data->client_rect.top,
600 changes.sibling, mask );
601 data->client_rect = client_rect;
602 wine_tsx11_lock();
603 XSync( gdi_display, False ); /* flush graphics operations before moving the window */
604 if (was_mapped && !is_client_window_mapped( win ))
605 XUnmapWindow( display, data->client_window );
606 XConfigureWindow( display, data->client_window, mask, &changes );
607 if (!was_mapped && is_client_window_mapped( win ))
608 XMapWindow( display, data->client_window );
609 wine_tsx11_unlock();
611 return mask;
615 /***********************************************************************
616 * X11DRV_register_window
618 * Associate an X window to a HWND.
620 void X11DRV_register_window( Display *display, HWND hwnd, struct x11drv_win_data *data )
622 wine_tsx11_lock();
623 XSaveContext( display, data->whole_window, winContext, (char *)hwnd );
624 XSaveContext( display, data->client_window, winContext, (char *)hwnd );
625 wine_tsx11_unlock();
629 /**********************************************************************
630 * create_desktop
632 static void create_desktop( Display *display, WND *wndPtr, CREATESTRUCTA *cs )
634 X11DRV_WND_DATA *data = wndPtr->pDriverData;
636 wine_tsx11_lock();
637 winContext = XUniqueContext();
638 wmProtocols = XInternAtom( display, "WM_PROTOCOLS", False );
639 wmDeleteWindow = XInternAtom( display, "WM_DELETE_WINDOW", False );
640 /* wmTakeFocus = XInternAtom( display, "WM_TAKE_FOCUS", False );*/
641 wmTakeFocus = 0; /* not yet */
642 dndProtocol = XInternAtom( display, "DndProtocol" , False );
643 dndSelection = XInternAtom( display, "DndSelection" , False );
644 wmChangeState = XInternAtom( display, "WM_CHANGE_STATE", False );
645 mwmHints = XInternAtom( display, _XA_MWM_HINTS, False );
646 kwmDockWindow = XInternAtom( display, "KWM_DOCKWINDOW", False );
647 _kde_net_wm_system_tray_window_for = XInternAtom( display, "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR", False );
648 wine_tsx11_unlock();
650 whole_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_whole_window" ));
651 client_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_client_window" ));
652 icon_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_icon_window" ));
654 data->whole_window = data->client_window = root_window;
655 data->whole_rect = data->client_rect = wndPtr->rectWindow;
657 SetPropA( wndPtr->hwndSelf, whole_window_atom, (HANDLE)root_window );
658 SetPropA( wndPtr->hwndSelf, client_window_atom, (HANDLE)root_window );
659 SetPropA( wndPtr->hwndSelf, "__wine_x11_visual_id", (HANDLE)XVisualIDFromVisual(visual) );
661 SendMessageW( wndPtr->hwndSelf, WM_NCCREATE, 0, (LPARAM)cs );
662 if (root_window != DefaultRootWindow(display)) X11DRV_create_desktop_thread();
666 /**********************************************************************
667 * create_whole_window
669 * Create the whole X window for a given window
671 static Window create_whole_window( Display *display, WND *win )
673 struct x11drv_win_data *data = win->pDriverData;
674 int cx, cy, mask;
675 XSetWindowAttributes attr;
676 Window parent;
677 RECT rect;
678 BOOL is_top_level = is_window_top_level( win );
680 rect = win->rectWindow;
681 X11DRV_window_to_X_rect( win, &rect );
683 if (!(cx = rect.right - rect.left)) cx = 1;
684 if (!(cy = rect.bottom - rect.top)) cy = 1;
686 parent = X11DRV_get_client_window( win->parent );
688 wine_tsx11_lock();
690 mask = get_window_attributes( display, win, &attr );
692 /* set the attributes that don't change over the lifetime of the window */
693 attr.bit_gravity = ForgetGravity;
694 attr.win_gravity = NorthWestGravity;
695 attr.backing_store = NotUseful/*WhenMapped*/;
696 mask |= CWBitGravity | CWWinGravity | CWBackingStore;
698 data->whole_rect = rect;
699 data->whole_window = XCreateWindow( display, parent, rect.left, rect.top, cx, cy,
700 0, screen_depth, InputOutput, visual,
701 mask, &attr );
702 if (attr.cursor) XFreeCursor( display, attr.cursor );
704 if (!data->whole_window)
706 wine_tsx11_unlock();
707 return 0;
710 /* non-maximized child must be at bottom of Z order */
711 if ((win->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
713 XWindowChanges changes;
714 changes.stack_mode = Below;
715 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
718 wine_tsx11_unlock();
720 if (is_top_level) set_wm_hints( display, win );
722 return data->whole_window;
726 /**********************************************************************
727 * create_client_window
729 * Create the client window for a given window
731 static Window create_client_window( Display *display, WND *win )
733 struct x11drv_win_data *data = win->pDriverData;
734 RECT rect = data->whole_rect;
735 XSetWindowAttributes attr;
737 OffsetRect( &rect, -data->whole_rect.left, -data->whole_rect.top );
738 data->client_rect = rect;
740 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
741 ButtonPressMask | ButtonReleaseMask);
742 attr.bit_gravity = (win->clsStyle & (CS_VREDRAW | CS_HREDRAW)) ?
743 ForgetGravity : NorthWestGravity;
744 attr.backing_store = NotUseful/*WhenMapped*/;
746 wine_tsx11_lock();
747 data->client_window = XCreateWindow( display, data->whole_window, 0, 0,
748 max( rect.right - rect.left, 1 ),
749 max( rect.bottom - rect.top, 1 ),
750 0, screen_depth,
751 InputOutput, visual,
752 CWEventMask | CWBitGravity | CWBackingStore, &attr );
753 if (data->client_window && is_client_window_mapped( win ))
754 XMapWindow( display, data->client_window );
755 wine_tsx11_unlock();
756 return data->client_window;
760 /*****************************************************************
761 * SetWindowText (X11DRV.@)
763 BOOL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
765 Display *display = thread_display();
766 UINT count;
767 char *buffer;
768 char *utf8_buffer;
769 static UINT text_cp = (UINT)-1;
770 Window win;
772 if ((win = X11DRV_get_whole_window( hwnd )))
774 if (text_cp == (UINT)-1)
776 HKEY hkey;
777 /* default value */
778 text_cp = CP_ACP;
779 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\x11drv", &hkey))
781 char buffer[20];
782 DWORD type, count = sizeof(buffer);
783 if(!RegQueryValueExA(hkey, "TextCP", 0, &type, buffer, &count))
784 text_cp = atoi(buffer);
785 RegCloseKey(hkey);
787 TRACE("text_cp = %u\n", text_cp);
790 /* allocate new buffer for window text */
791 count = WideCharToMultiByte(text_cp, 0, text, -1, NULL, 0, NULL, NULL);
792 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count )))
794 ERR("Not enough memory for window text\n");
795 return FALSE;
797 WideCharToMultiByte(text_cp, 0, text, -1, buffer, count, NULL, NULL);
799 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
800 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
802 ERR("Not enough memory for window text in UTF-8\n");
803 return FALSE;
805 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
807 wine_tsx11_lock();
808 XStoreName( display, win, buffer );
809 XSetIconName( display, win, buffer );
811 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
812 according to the standard
813 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
815 XChangeProperty( display, win,
816 XInternAtom(display, "_NET_WM_NAME", False),
817 XInternAtom(display, "UTF8_STRING", False),
818 8, PropModeReplace, (unsigned char *) utf8_buffer,
819 count);
820 wine_tsx11_unlock();
822 HeapFree( GetProcessHeap(), 0, utf8_buffer );
823 HeapFree( GetProcessHeap(), 0, buffer );
825 return TRUE;
829 /***********************************************************************
830 * DestroyWindow (X11DRV.@)
832 BOOL X11DRV_DestroyWindow( HWND hwnd )
834 Display *display = thread_display();
835 WND *wndPtr = WIN_GetPtr( hwnd );
836 X11DRV_WND_DATA *data = wndPtr->pDriverData;
838 if (!data) goto done;
840 if (data->whole_window)
842 TRACE( "win %x xwin %lx/%lx\n", hwnd, data->whole_window, data->client_window );
843 wine_tsx11_lock();
844 XSync( gdi_display, False ); /* flush any reference to this drawable in GDI queue */
845 XDeleteContext( display, data->whole_window, winContext );
846 XDeleteContext( display, data->client_window, winContext );
847 XDestroyWindow( display, data->whole_window ); /* this destroys client too */
848 destroy_icon_window( display, wndPtr );
849 wine_tsx11_unlock();
852 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
853 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
854 HeapFree( GetProcessHeap(), 0, data );
855 wndPtr->pDriverData = NULL;
856 done:
857 WIN_ReleasePtr( wndPtr );
858 return TRUE;
862 /**********************************************************************
863 * CreateWindow (X11DRV.@)
865 BOOL X11DRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
867 HWND hwndLinkAfter;
868 Display *display = thread_display();
869 WND *wndPtr;
870 struct x11drv_win_data *data;
871 RECT rect;
872 BOOL ret = FALSE;
874 if (!(data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data)))) return FALSE;
875 data->whole_window = 0;
876 data->client_window = 0;
877 data->icon_window = 0;
878 data->hWMIconBitmap = 0;
879 data->hWMIconMask = 0;
881 wndPtr = WIN_GetPtr( hwnd );
882 wndPtr->pDriverData = data;
884 /* initialize the dimensions before sending WM_GETMINMAXINFO */
885 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
886 WIN_SetRectangles( hwnd, &rect, &rect );
888 if (!wndPtr->parent)
890 create_desktop( display, wndPtr, cs );
891 WIN_ReleasePtr( wndPtr );
892 return TRUE;
895 if (!create_whole_window( display, wndPtr )) goto failed;
896 if (!create_client_window( display, wndPtr )) goto failed;
897 TSXSync( display, False );
899 SetPropA( hwnd, whole_window_atom, (HANDLE)data->whole_window );
900 SetPropA( hwnd, client_window_atom, (HANDLE)data->client_window );
902 /* Call the WH_CBT hook */
904 hwndLinkAfter = ((cs->style & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
905 ? HWND_BOTTOM : HWND_TOP;
907 if (HOOK_IsHooked( WH_CBT ))
909 CBT_CREATEWNDA cbtc;
910 LRESULT lret;
912 cbtc.lpcs = cs;
913 cbtc.hwndInsertAfter = hwndLinkAfter;
914 lret = (unicode) ? HOOK_CallHooksW(WH_CBT, HCBT_CREATEWND,
915 (WPARAM)hwnd, (LPARAM)&cbtc)
916 : HOOK_CallHooksA(WH_CBT, HCBT_CREATEWND,
917 (WPARAM)hwnd, (LPARAM)&cbtc);
918 if (lret)
920 TRACE("CBT-hook returned !0\n");
921 goto failed;
927 /* Send the WM_GETMINMAXINFO message and fix the size if needed */
928 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
930 POINT maxSize, maxPos, minTrack, maxTrack;
932 WIN_ReleasePtr( wndPtr );
933 WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
934 if (maxSize.x < cs->cx) cs->cx = maxSize.x;
935 if (maxSize.y < cs->cy) cs->cy = maxSize.y;
936 if (cs->cx < minTrack.x ) cs->cx = minTrack.x;
937 if (cs->cy < minTrack.y ) cs->cy = minTrack.y;
938 if (cs->cx < 0) cs->cx = 0;
939 if (cs->cy < 0) cs->cy = 0;
941 if (!(wndPtr = WIN_GetPtr( hwnd ))) return FALSE;
942 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
943 WIN_SetRectangles( hwnd, &rect, &rect );
944 X11DRV_sync_whole_window_position( display, wndPtr, 0 );
946 WIN_ReleasePtr( wndPtr );
948 /* send WM_NCCREATE */
949 TRACE( "hwnd %x cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cs->cx, cs->cy );
950 if (unicode)
951 ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
952 else
953 ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
954 if (!ret)
956 WARN("aborted by WM_xxCREATE!\n");
957 return FALSE;
960 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
962 sync_window_style( display, wndPtr );
964 /* send WM_NCCALCSIZE */
965 rect = wndPtr->rectWindow;
966 WIN_ReleasePtr( wndPtr );
967 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&rect );
969 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
970 if (rect.left > rect.right || rect.top > rect.bottom) rect = wndPtr->rectWindow;
971 WIN_SetRectangles( hwnd, &wndPtr->rectWindow, &rect );
972 X11DRV_sync_client_window_position( display, wndPtr );
973 X11DRV_register_window( display, hwnd, data );
975 TRACE( "win %x window %d,%d,%d,%d client %d,%d,%d,%d whole %d,%d,%d,%d X client %d,%d,%d,%d xwin %x/%x\n",
976 hwnd, wndPtr->rectWindow.left, wndPtr->rectWindow.top,
977 wndPtr->rectWindow.right, wndPtr->rectWindow.bottom,
978 wndPtr->rectClient.left, wndPtr->rectClient.top,
979 wndPtr->rectClient.right, wndPtr->rectClient.bottom,
980 data->whole_rect.left, data->whole_rect.top,
981 data->whole_rect.right, data->whole_rect.bottom,
982 data->client_rect.left, data->client_rect.top,
983 data->client_rect.right, data->client_rect.bottom,
984 (unsigned int)data->whole_window, (unsigned int)data->client_window );
986 if ((wndPtr->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
987 WIN_LinkWindow( hwnd, wndPtr->parent, HWND_BOTTOM );
988 else
989 WIN_LinkWindow( hwnd, wndPtr->parent, HWND_TOP );
991 WIN_ReleasePtr( wndPtr );
993 if (unicode)
994 ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
995 else
996 ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
998 if (!ret)
1000 WIN_UnlinkWindow( hwnd );
1001 return FALSE;
1004 /* Send the size messages */
1006 if (!(wndPtr = WIN_FindWndPtr(hwnd))) return FALSE;
1007 if (!(wndPtr->flags & WIN_NEED_SIZE))
1009 /* send it anyway */
1010 if (((wndPtr->rectClient.right-wndPtr->rectClient.left) <0)
1011 ||((wndPtr->rectClient.bottom-wndPtr->rectClient.top)<0))
1012 WARN("sending bogus WM_SIZE message 0x%08lx\n",
1013 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
1014 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
1015 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1016 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
1017 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
1018 SendMessageW( hwnd, WM_MOVE, 0,
1019 MAKELONG( wndPtr->rectClient.left, wndPtr->rectClient.top ) );
1022 /* Show the window, maximizing or minimizing if needed */
1024 if (wndPtr->dwStyle & (WS_MINIMIZE | WS_MAXIMIZE))
1026 extern UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect ); /*FIXME*/
1028 RECT newPos;
1029 UINT swFlag = (wndPtr->dwStyle & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1030 WIN_SetStyle( hwnd, wndPtr->dwStyle & ~(WS_MAXIMIZE | WS_MINIMIZE) );
1031 WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1032 swFlag = ((wndPtr->dwStyle & WS_CHILD) || GetActiveWindow())
1033 ? SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED
1034 : SWP_NOZORDER | SWP_FRAMECHANGED;
1035 SetWindowPos( hwnd, 0, newPos.left, newPos.top,
1036 newPos.right, newPos.bottom, swFlag );
1039 WIN_ReleaseWndPtr( wndPtr );
1040 return TRUE;
1043 failed:
1044 X11DRV_DestroyWindow( hwnd );
1045 if (wndPtr) WIN_ReleasePtr( wndPtr );
1046 return FALSE;
1050 /***********************************************************************
1051 * X11DRV_get_client_window
1053 * Return the X window associated with the client area of a window
1055 Window X11DRV_get_client_window( HWND hwnd )
1057 Window ret = 0;
1058 WND *win = WIN_GetPtr( hwnd );
1060 if (win == WND_OTHER_PROCESS)
1061 return GetPropA( hwnd, client_window_atom );
1063 if (win)
1065 struct x11drv_win_data *data = win->pDriverData;
1066 ret = data->client_window;
1067 WIN_ReleasePtr( win );
1069 return ret;
1073 /***********************************************************************
1074 * X11DRV_get_whole_window
1076 * Return the X window associated with the full area of a window
1078 Window X11DRV_get_whole_window( HWND hwnd )
1080 Window ret = 0;
1081 WND *win = WIN_GetPtr( hwnd );
1083 if (win == WND_OTHER_PROCESS)
1084 return GetPropA( hwnd, whole_window_atom );
1086 if (win)
1088 struct x11drv_win_data *data = win->pDriverData;
1089 ret = data->whole_window;
1090 WIN_ReleasePtr( win );
1092 return ret;
1096 /*****************************************************************
1097 * SetParent (X11DRV.@)
1099 HWND X11DRV_SetParent( HWND hwnd, HWND parent )
1101 Display *display = thread_display();
1102 WND *wndPtr;
1103 HWND retvalue;
1105 /* Windows hides the window first, then shows it again
1106 * including the WM_SHOWWINDOW messages and all */
1107 BOOL was_visible = ShowWindow( hwnd, SW_HIDE );
1109 if (!IsWindow( parent )) return 0;
1110 if (!(wndPtr = WIN_GetPtr(hwnd)) || wndPtr == WND_OTHER_PROCESS) return 0;
1112 retvalue = wndPtr->parent; /* old parent */
1113 if (parent != retvalue)
1115 struct x11drv_win_data *data = wndPtr->pDriverData;
1117 WIN_LinkWindow( hwnd, parent, HWND_TOP );
1119 if (parent != GetDesktopWindow()) /* a child window */
1121 if (!(wndPtr->dwStyle & WS_CHILD))
1123 HMENU menu = (HMENU)SetWindowLongW( hwnd, GWL_ID, 0 );
1124 if (menu) DestroyMenu( menu );
1128 if (is_window_top_level( wndPtr )) set_wm_hints( display, wndPtr );
1129 wine_tsx11_lock();
1130 sync_window_style( display, wndPtr );
1131 XReparentWindow( display, data->whole_window, X11DRV_get_client_window(parent),
1132 data->whole_rect.left, data->whole_rect.top );
1133 wine_tsx11_unlock();
1135 WIN_ReleasePtr( wndPtr );
1137 /* SetParent additionally needs to make hwnd the topmost window
1138 in the x-order and send the expected WM_WINDOWPOSCHANGING and
1139 WM_WINDOWPOSCHANGED notification messages.
1141 SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0,
1142 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | (was_visible ? SWP_SHOWWINDOW : 0) );
1143 /* FIXME: a WM_MOVE is also generated (in the DefWindowProc handler
1144 * for WM_WINDOWPOSCHANGED) in Windows, should probably remove SWP_NOMOVE */
1146 return retvalue;
1150 /*****************************************************************
1151 * SetFocus (X11DRV.@)
1153 * Set the X focus.
1154 * Explicit colormap management seems to work only with OLVWM.
1156 void X11DRV_SetFocus( HWND hwnd )
1158 Display *display = thread_display();
1159 XWindowAttributes win_attr;
1160 Window win;
1162 /* Only mess with the X focus if there's */
1163 /* no desktop window and if the window is not managed by the WM. */
1164 if (root_window != DefaultRootWindow(display)) return;
1166 if (!hwnd) /* If setting the focus to 0, uninstall the colormap */
1168 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1169 TSXUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1170 return;
1173 hwnd = GetAncestor( hwnd, GA_ROOT );
1174 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_MANAGED) return;
1175 if (!(win = X11DRV_get_whole_window( hwnd ))) return;
1177 /* Set X focus and install colormap */
1178 wine_tsx11_lock();
1179 if (XGetWindowAttributes( display, win, &win_attr ) &&
1180 (win_attr.map_state == IsViewable))
1182 /* If window is not viewable, don't change anything */
1184 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1185 /* FIXME: this is not entirely correct */
1186 XSetInputFocus( display, win, RevertToParent,
1187 /*CurrentTime*/ GetMessageTime() + X11DRV_server_startticks );
1188 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1189 XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1191 wine_tsx11_unlock();
1195 /**********************************************************************
1196 * SetWindowIcon (X11DRV.@)
1198 * hIcon or hIconSm has changed (or is being initialised for the
1199 * first time). Complete the X11 driver-specific initialisation
1200 * and set the window hints.
1202 * This is not entirely correct, may need to create
1203 * an icon window and set the pixmap as a background
1205 HICON X11DRV_SetWindowIcon( HWND hwnd, HICON icon, BOOL small )
1207 WND *wndPtr;
1208 Display *display = thread_display();
1209 HICON old = SetClassLongW( hwnd, small ? GCL_HICONSM : GCL_HICON, icon );
1211 SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOSIZE |
1212 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
1214 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return old;
1216 if (wndPtr->dwExStyle & WS_EX_MANAGED)
1218 Window win = get_whole_window(wndPtr);
1219 XWMHints* wm_hints = TSXGetWMHints( display, win );
1221 if (!wm_hints) wm_hints = TSXAllocWMHints();
1222 if (wm_hints)
1224 set_icon_hints( display, wndPtr, wm_hints );
1225 TSXSetWMHints( display, win, wm_hints );
1226 TSXFree( wm_hints );
1229 WIN_ReleasePtr( wndPtr );
1230 return old;