msi: Remember to free a handle.
[wine/multimedia.git] / dlls / winex11.drv / window.c
blob2bc90a77a5a853308650e5bbe80663eeccf809b0
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
31 #include <X11/Xlib.h>
32 #include <X11/Xresource.h>
33 #include <X11/Xutil.h>
35 #include "windef.h"
36 #include "winbase.h"
37 #include "wingdi.h"
38 #include "winreg.h"
39 #include "winuser.h"
40 #include "wine/unicode.h"
42 #include "x11drv.h"
43 #include "wine/debug.h"
44 #include "wine/server.h"
45 #include "win.h"
46 #include "mwm.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
50 /* X context to associate a hwnd to an X window */
51 XContext winContext = 0;
53 /* X context to associate a struct x11drv_win_data to an hwnd */
54 static XContext win_data_context;
56 static const char whole_window_prop[] = "__wine_x11_whole_window";
57 static const char icon_window_prop[] = "__wine_x11_icon_window";
58 static const char managed_prop[] = "__wine_x11_managed";
59 static const char visual_id_prop[] = "__wine_x11_visual_id";
61 /***********************************************************************
62 * is_window_managed
64 * Check if a given window should be managed
66 inline static BOOL is_window_managed( HWND hwnd )
68 DWORD style, ex_style;
70 if (!managed_mode) return FALSE;
71 /* tray window is always managed */
72 ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
73 if (ex_style & WS_EX_TRAYWINDOW) return TRUE;
74 /* child windows are not managed */
75 style = GetWindowLongW( hwnd, GWL_STYLE );
76 if (style & WS_CHILD) return FALSE;
77 /* windows with caption are managed */
78 if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
79 /* tool windows are not managed */
80 if (ex_style & WS_EX_TOOLWINDOW) return FALSE;
81 /* windows with thick frame are managed */
82 if (style & WS_THICKFRAME) return TRUE;
83 /* application windows are managed */
84 if (ex_style & WS_EX_APPWINDOW) return TRUE;
85 /* full-screen popup windows are managed */
86 if (style & WS_POPUP)
88 RECT rect;
89 GetWindowRect( hwnd, &rect );
90 if ((rect.right - rect.left) == screen_width && (rect.bottom - rect.top) == screen_height)
91 return TRUE;
93 /* default: not managed */
94 return FALSE;
98 /***********************************************************************
99 * X11DRV_is_window_rect_mapped
101 * Check if the X whole window should be mapped based on its rectangle
103 BOOL X11DRV_is_window_rect_mapped( const RECT *rect )
105 /* don't map if rect is empty */
106 if (IsRectEmpty( rect )) return FALSE;
108 /* don't map if rect is off-screen */
109 if (rect->left >= (int)screen_width || rect->top >= (int)screen_height) return FALSE;
110 if (rect->right < 0 || rect->bottom < 0) return FALSE;
112 return TRUE;
116 /***********************************************************************
117 * get_window_attributes
119 * Fill the window attributes structure for an X window.
121 static int get_window_attributes( Display *display, struct x11drv_win_data *data,
122 XSetWindowAttributes *attr )
124 if (!data->managed &&
125 root_window == DefaultRootWindow( display ) &&
126 data->whole_window != root_window &&
127 is_window_managed( data->hwnd ))
129 data->managed = TRUE;
130 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
132 attr->override_redirect = !data->managed;
133 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
134 attr->save_under = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
135 attr->cursor = x11drv_thread_data()->cursor;
136 return (CWOverrideRedirect | CWSaveUnder | CWColormap | CWCursor);
140 /***********************************************************************
141 * X11DRV_sync_window_style
143 * Change the X window attributes when the window style has changed.
145 void X11DRV_sync_window_style( Display *display, struct x11drv_win_data *data )
147 XSetWindowAttributes attr;
148 int mask = get_window_attributes( display, data, &attr );
150 wine_tsx11_lock();
151 if (data->whole_window != DefaultRootWindow(display))
152 XChangeWindowAttributes( display, data->whole_window, mask, &attr );
153 wine_tsx11_unlock();
157 /***********************************************************************
158 * get_window_changes
160 * fill the window changes structure
162 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
164 int mask = 0;
166 if (old->right - old->left != new->right - new->left )
168 if (!(changes->width = new->right - new->left)) changes->width = 1;
169 mask |= CWWidth;
171 if (old->bottom - old->top != new->bottom - new->top)
173 if (!(changes->height = new->bottom - new->top)) changes->height = 1;
174 mask |= CWHeight;
176 if (old->left != new->left)
178 changes->x = new->left;
179 mask |= CWX;
181 if (old->top != new->top)
183 changes->y = new->top;
184 mask |= CWY;
186 return mask;
190 /***********************************************************************
191 * create_icon_window
193 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
195 XSetWindowAttributes attr;
197 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
198 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
199 attr.bit_gravity = NorthWestGravity;
200 attr.backing_store = NotUseful/*WhenMapped*/;
201 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
203 wine_tsx11_lock();
204 data->icon_window = XCreateWindow( display, root_window, 0, 0,
205 GetSystemMetrics( SM_CXICON ),
206 GetSystemMetrics( SM_CYICON ),
207 0, screen_depth,
208 InputOutput, visual,
209 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
210 XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
211 wine_tsx11_unlock();
213 TRACE( "created %lx\n", data->icon_window );
214 SetPropA( data->hwnd, icon_window_prop, (HANDLE)data->icon_window );
215 return data->icon_window;
220 /***********************************************************************
221 * destroy_icon_window
223 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
225 if (!data->icon_window) return;
226 if (x11drv_thread_data()->cursor_window == data->icon_window)
227 x11drv_thread_data()->cursor_window = None;
228 wine_tsx11_lock();
229 XDeleteContext( display, data->icon_window, winContext );
230 XDestroyWindow( display, data->icon_window );
231 data->icon_window = 0;
232 wine_tsx11_unlock();
233 RemovePropA( data->hwnd, icon_window_prop );
237 /***********************************************************************
238 * set_icon_hints
240 * Set the icon wm hints
242 static void set_icon_hints( Display *display, struct x11drv_win_data *data,
243 XWMHints *hints, HICON hIcon )
245 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
246 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
247 data->hWMIconBitmap = 0;
248 data->hWMIconMask = 0;
250 if (!data->managed)
252 destroy_icon_window( display, data );
253 hints->flags &= ~(IconPixmapHint | IconMaskHint | IconWindowHint);
255 else if (!hIcon)
257 if (!data->icon_window) create_icon_window( display, data );
258 hints->icon_window = data->icon_window;
259 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
261 else
263 HBITMAP hbmOrig;
264 RECT rcMask;
265 BITMAP bmMask;
266 ICONINFO ii;
267 HDC hDC;
269 GetIconInfo(hIcon, &ii);
271 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
272 rcMask.top = 0;
273 rcMask.left = 0;
274 rcMask.right = bmMask.bmWidth;
275 rcMask.bottom = bmMask.bmHeight;
277 hDC = CreateCompatibleDC(0);
278 hbmOrig = SelectObject(hDC, ii.hbmMask);
279 InvertRect(hDC, &rcMask);
280 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
281 SelectObject(hDC, hbmOrig);
282 DeleteDC(hDC);
284 data->hWMIconBitmap = ii.hbmColor;
285 data->hWMIconMask = ii.hbmMask;
287 hints->icon_pixmap = X11DRV_get_pixmap(data->hWMIconBitmap);
288 hints->icon_mask = X11DRV_get_pixmap(data->hWMIconMask);
289 destroy_icon_window( display, data );
290 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
295 /***********************************************************************
296 * set_size_hints
298 * set the window size hints
300 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
302 XSizeHints* size_hints;
304 if ((size_hints = XAllocSizeHints()))
306 size_hints->flags = 0;
308 if (data->hwnd != GetDesktopWindow()) /* don't force position of desktop */
310 size_hints->win_gravity = StaticGravity;
311 size_hints->x = data->whole_rect.left;
312 size_hints->y = data->whole_rect.top;
313 size_hints->flags |= PWinGravity | PPosition;
316 if ( !(style & WS_THICKFRAME) )
318 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
319 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
320 size_hints->min_width = size_hints->max_width;
321 size_hints->min_height = size_hints->max_height;
322 size_hints->flags |= PMinSize | PMaxSize;
324 XSetWMNormalHints( display, data->whole_window, size_hints );
325 XFree( size_hints );
330 /***********************************************************************
331 * get_process_name
333 * get the name of the current process for setting class hints
335 static char *get_process_name(void)
337 static char *name;
339 if (!name)
341 WCHAR module[MAX_PATH];
342 DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
343 if (len && len < MAX_PATH)
345 char *ptr;
346 WCHAR *p, *appname = module;
348 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
349 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
350 len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
351 if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
353 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
354 name = ptr;
358 return name;
362 /***********************************************************************
363 * X11DRV_set_wm_hints
365 * Set the window manager hints for a newly-created window
367 void X11DRV_set_wm_hints( Display *display, struct x11drv_win_data *data )
369 Window group_leader;
370 XClassHint *class_hints;
371 XWMHints* wm_hints;
372 Atom protocols[3];
373 MwmHints mwm_hints;
374 Atom dndVersion = 4;
375 int i;
376 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
377 DWORD ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
378 HWND owner = GetWindow( data->hwnd, GW_OWNER );
379 char *process_name = get_process_name();
381 if (data->hwnd == GetDesktopWindow())
383 if (data->whole_window == DefaultRootWindow(display)) return;
384 /* force some styles for the desktop to get the correct decorations */
385 style |= WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
386 owner = 0;
389 /* transient for hint */
390 if (owner)
392 Window owner_win = X11DRV_get_whole_window( owner );
393 wine_tsx11_lock();
394 XSetTransientForHint( display, data->whole_window, owner_win );
395 wine_tsx11_unlock();
396 group_leader = owner_win;
398 else group_leader = data->whole_window;
400 wine_tsx11_lock();
402 /* wm protocols */
403 i = 0;
404 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
405 protocols[i++] = x11drv_atom(_NET_WM_PING);
406 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
407 XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
408 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
410 /* class hints */
411 if ((class_hints = XAllocClassHint()))
413 static char wine[] = "Wine";
415 class_hints->res_name = process_name;
416 class_hints->res_class = wine;
417 XSetClassHint( display, data->whole_window, class_hints );
418 XFree( class_hints );
421 /* size hints */
422 set_size_hints( display, data, style );
424 /* systray properties (KDE only for now) */
425 if (ex_style & WS_EX_TRAYWINDOW)
427 int val = 1;
428 XChangeProperty( display, data->whole_window, x11drv_atom(KWM_DOCKWINDOW),
429 x11drv_atom(KWM_DOCKWINDOW), 32, PropModeReplace, (unsigned char*)&val, 1 );
430 XChangeProperty( display, data->whole_window, x11drv_atom(_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR),
431 XA_WINDOW, 32, PropModeReplace, (unsigned char*)&data->whole_window, 1 );
434 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
435 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
436 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
437 i = getpid();
438 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
439 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
441 /* map WS_EX_TOOLWINDOW to _NET_WM_WINDOW_TYPE_UTILITY */
442 if (ex_style & WS_EX_TOOLWINDOW)
444 Atom a = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
445 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
446 XA_ATOM, 32, PropModeReplace, (unsigned char*)&a, 1);
449 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
450 mwm_hints.functions = 0;
451 if ((style & WS_CAPTION) == WS_CAPTION) mwm_hints.functions |= MWM_FUNC_MOVE;
452 if (style & WS_THICKFRAME) mwm_hints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
453 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
454 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
455 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
456 if (ex_style & WS_EX_APPWINDOW) mwm_hints.functions |= MWM_FUNC_MOVE;
457 mwm_hints.decorations = 0;
458 if ((style & WS_CAPTION) == WS_CAPTION)
460 mwm_hints.decorations |= MWM_DECOR_TITLE;
461 if (style & WS_SYSMENU) mwm_hints.decorations |= MWM_DECOR_MENU;
462 if (style & WS_MINIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MINIMIZE;
463 if (style & WS_MAXIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MAXIMIZE;
465 if (ex_style & WS_EX_DLGMODALFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
466 else if (style & WS_THICKFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
467 else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
468 else if (style & WS_BORDER) mwm_hints.decorations |= MWM_DECOR_BORDER;
469 else if (!(style & (WS_CHILD|WS_POPUP))) mwm_hints.decorations |= MWM_DECOR_BORDER;
471 XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
472 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
473 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
475 XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
476 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
478 wm_hints = XAllocWMHints();
479 wine_tsx11_unlock();
481 /* wm hints */
482 if (wm_hints)
484 wm_hints->flags = InputHint | StateHint | WindowGroupHint;
485 wm_hints->input = !(style & WS_DISABLED);
487 set_icon_hints( display, data, wm_hints,
488 (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON ) );
490 wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
491 wm_hints->window_group = group_leader;
493 wine_tsx11_lock();
494 XSetWMHints( display, data->whole_window, wm_hints );
495 XFree(wm_hints);
496 wine_tsx11_unlock();
501 /***********************************************************************
502 * X11DRV_set_iconic_state
504 * Set the X11 iconic state according to the window style.
506 void X11DRV_set_iconic_state( HWND hwnd )
508 Display *display = thread_display();
509 struct x11drv_win_data *data;
510 RECT rect;
511 XWMHints* wm_hints;
512 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
513 BOOL iconic = (style & WS_MINIMIZE) != 0;
515 if (!(data = X11DRV_get_win_data( hwnd ))) return;
516 if (!data->whole_window || data->whole_window == DefaultRootWindow(display)) return;
518 GetWindowRect( hwnd, &rect );
520 wine_tsx11_lock();
522 if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
523 wm_hints->flags |= StateHint | IconPositionHint;
524 wm_hints->initial_state = iconic ? IconicState : NormalState;
525 wm_hints->icon_x = rect.left;
526 wm_hints->icon_y = rect.top;
527 XSetWMHints( display, data->whole_window, wm_hints );
529 if (style & WS_VISIBLE)
531 if (iconic)
532 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
533 else
534 if (X11DRV_is_window_rect_mapped( &rect ))
535 XMapWindow( display, data->whole_window );
538 XFree(wm_hints);
539 wine_tsx11_unlock();
543 /***********************************************************************
544 * X11DRV_window_to_X_rect
546 * Convert a rect from client to X window coordinates
548 void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
550 RECT rc;
552 if (!data->managed) return;
553 if (IsRectEmpty( rect )) return;
555 rc.top = rc.bottom = rc.left = rc.right = 0;
557 AdjustWindowRectEx( &rc, GetWindowLongW( data->hwnd, GWL_STYLE ) & ~(WS_HSCROLL|WS_VSCROLL),
558 FALSE, GetWindowLongW( data->hwnd, GWL_EXSTYLE ) );
560 rect->left -= rc.left;
561 rect->right -= rc.right;
562 rect->top -= rc.top;
563 rect->bottom -= rc.bottom;
564 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
565 if (rect->left >= rect->right) rect->right = rect->left + 1;
569 /***********************************************************************
570 * X11DRV_X_to_window_rect
572 * Opposite of X11DRV_window_to_X_rect
574 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
576 if (!data->managed) return;
577 if (IsRectEmpty( rect )) return;
579 AdjustWindowRectEx( rect, GetWindowLongW( data->hwnd, GWL_STYLE ) & ~(WS_HSCROLL|WS_VSCROLL),
580 FALSE, GetWindowLongW( data->hwnd, GWL_EXSTYLE ));
582 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
583 if (rect->left >= rect->right) rect->right = rect->left + 1;
587 /***********************************************************************
588 * X11DRV_sync_window_position
590 * Synchronize the X window position with the Windows one
592 void X11DRV_sync_window_position( Display *display, struct x11drv_win_data *data,
593 UINT swp_flags, const RECT *new_client_rect,
594 const RECT *new_whole_rect )
596 XWindowChanges changes;
597 int mask;
598 RECT old_whole_rect;
600 old_whole_rect = data->whole_rect;
601 data->whole_rect = *new_whole_rect;
603 data->client_rect = *new_client_rect;
604 OffsetRect( &data->client_rect, -data->whole_rect.left, -data->whole_rect.top );
606 if (!data->whole_window || data->lock_changes) return;
608 mask = get_window_changes( &changes, &old_whole_rect, &data->whole_rect );
610 if (!(swp_flags & SWP_NOZORDER))
612 /* find window that this one must be after */
613 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
614 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
615 prev = GetWindow( prev, GW_HWNDPREV );
616 if (!prev) /* top child */
618 changes.stack_mode = Above;
619 mask |= CWStackMode;
621 else
623 /* should use stack_mode Below but most window managers don't get it right */
624 /* so move it above the next one in Z order */
625 HWND next = GetWindow( data->hwnd, GW_HWNDNEXT );
626 while (next && !(GetWindowLongW( next, GWL_STYLE ) & WS_VISIBLE))
627 next = GetWindow( next, GW_HWNDNEXT );
628 if (next)
630 changes.stack_mode = Above;
631 changes.sibling = X11DRV_get_whole_window(next);
632 mask |= CWStackMode | CWSibling;
637 if (mask)
639 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
641 TRACE( "setting win %lx pos %ld,%ld,%ldx%ld after %lx changes=%x\n",
642 data->whole_window, data->whole_rect.left, data->whole_rect.top,
643 data->whole_rect.right - data->whole_rect.left,
644 data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
646 wine_tsx11_lock();
647 if (mask & (CWWidth|CWHeight)) set_size_hints( display, data, style );
648 XReconfigureWMWindow( display, data->whole_window,
649 DefaultScreen(display), mask, &changes );
650 wine_tsx11_unlock();
655 /**********************************************************************
656 * create_whole_window
658 * Create the whole X window for a given window
660 static Window create_whole_window( Display *display, struct x11drv_win_data *data, DWORD style )
662 int cx, cy, mask;
663 XSetWindowAttributes attr;
664 XIM xim;
665 RECT rect;
667 rect = data->window_rect;
668 X11DRV_window_to_X_rect( data, &rect );
670 if (!(cx = rect.right - rect.left)) cx = 1;
671 if (!(cy = rect.bottom - rect.top)) cy = 1;
673 mask = get_window_attributes( display, data, &attr );
675 /* set the attributes that don't change over the lifetime of the window */
676 attr.bit_gravity = NorthWestGravity;
677 attr.backing_store = NotUseful;
678 attr.event_mask = (ExposureMask | PointerMotionMask |
679 ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
680 KeyPressMask | KeyReleaseMask | StructureNotifyMask |
681 FocusChangeMask | KeymapStateMask);
682 mask |= CWBitGravity | CWBackingStore | CWEventMask;
684 wine_tsx11_lock();
686 data->whole_rect = rect;
687 data->whole_window = XCreateWindow( display, root_window, rect.left, rect.top, cx, cy,
688 0, screen_depth, InputOutput, visual,
689 mask, &attr );
691 if (!data->whole_window)
693 wine_tsx11_unlock();
694 return 0;
696 XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
698 /* non-maximized child must be at bottom of Z order */
699 if ((style & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
701 XWindowChanges changes;
702 changes.stack_mode = Below;
703 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
705 wine_tsx11_unlock();
707 xim = x11drv_thread_data()->xim;
708 if (xim) data->xic = X11DRV_CreateIC( xim, display, data->whole_window );
710 X11DRV_set_wm_hints( display, data );
712 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
713 return data->whole_window;
717 /**********************************************************************
718 * destroy_whole_window
720 * Destroy the whole X window for a given window.
722 static void destroy_whole_window( Display *display, struct x11drv_win_data *data )
724 struct x11drv_thread_data *thread_data = x11drv_thread_data();
726 if (!data->whole_window) return;
728 TRACE( "win %p xwin %lx\n", data->hwnd, data->whole_window );
729 if (thread_data->cursor_window == data->whole_window) thread_data->cursor_window = None;
730 wine_tsx11_lock();
731 XDeleteContext( display, data->whole_window, winContext );
732 if (data->whole_window != DefaultRootWindow(display))
733 XDestroyWindow( display, data->whole_window );
734 data->whole_window = 0;
735 if (data->xic)
737 XUnsetICFocus( data->xic );
738 XDestroyIC( data->xic );
740 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
741 XFlush( display );
742 wine_tsx11_unlock();
743 RemovePropA( data->hwnd, whole_window_prop );
747 /*****************************************************************
748 * SetWindowText (X11DRV.@)
750 void X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
752 Display *display = thread_display();
753 UINT count;
754 char *buffer;
755 char *utf8_buffer;
756 Window win;
757 XTextProperty prop;
759 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(display))
761 /* allocate new buffer for window text */
762 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
763 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count )))
765 ERR("Not enough memory for window text\n");
766 return;
768 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
770 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
771 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
773 ERR("Not enough memory for window text in UTF-8\n");
774 HeapFree( GetProcessHeap(), 0, buffer );
775 return;
777 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
779 wine_tsx11_lock();
780 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
782 XSetWMName( display, win, &prop );
783 XSetWMIconName( display, win, &prop );
784 XFree( prop.value );
787 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
788 according to the standard
789 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
791 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
792 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
793 wine_tsx11_unlock();
795 HeapFree( GetProcessHeap(), 0, utf8_buffer );
796 HeapFree( GetProcessHeap(), 0, buffer );
801 /***********************************************************************
802 * DestroyWindow (X11DRV.@)
804 void X11DRV_DestroyWindow( HWND hwnd )
806 struct x11drv_thread_data *thread_data = x11drv_thread_data();
807 Display *display = thread_data->display;
808 struct x11drv_win_data *data;
810 if (!(data = X11DRV_get_win_data( hwnd ))) return;
812 free_window_dce( data );
813 destroy_whole_window( display, data );
814 destroy_icon_window( display, data );
816 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
817 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
818 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
819 wine_tsx11_lock();
820 XDeleteContext( display, (XID)hwnd, win_data_context );
821 wine_tsx11_unlock();
822 HeapFree( GetProcessHeap(), 0, data );
826 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
828 struct x11drv_win_data *data;
830 if ((data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data))))
832 data->hwnd = hwnd;
833 data->whole_window = 0;
834 data->icon_window = 0;
835 data->xic = 0;
836 data->managed = FALSE;
837 data->dce = NULL;
838 data->lock_changes = 0;
839 data->hWMIconBitmap = 0;
840 data->hWMIconMask = 0;
842 wine_tsx11_lock();
843 if (!winContext) winContext = XUniqueContext();
844 if (!win_data_context) win_data_context = XUniqueContext();
845 XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
846 wine_tsx11_unlock();
848 return data;
852 /* fill in the desktop X window id in the x11drv_win_data structure */
853 static void get_desktop_xwin( Display *display, struct x11drv_win_data *data )
855 RECT rect;
856 Window win = (Window)GetPropA( data->hwnd, whole_window_prop );
858 if (win)
860 unsigned int width, height;
862 /* retrieve the real size of the desktop */
863 SERVER_START_REQ( get_window_rectangles )
865 req->handle = data->hwnd;
866 wine_server_call( req );
867 width = reply->window.right - reply->window.left;
868 height = reply->window.bottom - reply->window.top;
870 SERVER_END_REQ;
871 data->whole_window = win;
872 if (win != root_window) X11DRV_init_desktop( win, width, height );
874 else
876 VisualID visualid;
878 wine_tsx11_lock();
879 visualid = XVisualIDFromVisual(visual);
880 wine_tsx11_unlock();
881 SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
882 SetPropA( data->hwnd, visual_id_prop, (HANDLE)visualid );
883 data->whole_window = root_window;
884 SetRect( &rect, 0, 0, screen_width, screen_height );
885 X11DRV_set_window_pos( data->hwnd, 0, &rect, &rect, SWP_NOZORDER, NULL );
886 if (root_window != DefaultRootWindow( display ))
888 data->managed = TRUE;
889 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
894 /**********************************************************************
895 * CreateDesktopWindow (X11DRV.@)
897 BOOL X11DRV_CreateDesktopWindow( HWND hwnd )
899 Display *display = thread_display();
900 struct x11drv_win_data *data;
902 if (!(data = alloc_win_data( display, hwnd ))) return FALSE;
904 get_desktop_xwin( display, data );
906 return TRUE;
910 /**********************************************************************
911 * CreateWindow (X11DRV.@)
913 BOOL X11DRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
915 Display *display = thread_display();
916 WND *wndPtr;
917 struct x11drv_win_data *data;
918 HWND insert_after;
919 RECT rect;
920 DWORD style;
921 CBT_CREATEWNDA cbtc;
922 CREATESTRUCTA cbcs;
923 BOOL ret = FALSE;
925 if (!(data = alloc_win_data( display, hwnd ))) return FALSE;
927 if (cs->cx > 65535)
929 ERR( "invalid window width %d\n", cs->cx );
930 cs->cx = 65535;
932 if (cs->cy > 65535)
934 ERR( "invalid window height %d\n", cs->cy );
935 cs->cy = 65535;
937 if (cs->cx < 0)
939 ERR( "invalid window width %d\n", cs->cx );
940 cs->cx = 0;
942 if (cs->cy < 0)
944 ERR( "invalid window height %d\n", cs->cy );
945 cs->cy = 0;
948 /* initialize the dimensions before sending WM_GETMINMAXINFO */
949 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
950 X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER, NULL );
952 /* create an X window if it's a top level window */
953 if (GetAncestor( hwnd, GA_PARENT ) == GetDesktopWindow())
955 if (!create_whole_window( display, data, cs->style )) goto failed;
957 else if (hwnd == GetDesktopWindow())
959 get_desktop_xwin( display, data );
962 /* get class or window DC if needed */
963 alloc_window_dce( data );
965 /* Call the WH_CBT hook */
967 /* the window style passed to the hook must be the real window style,
968 * rather than just the window style that the caller to CreateWindowEx
969 * passed in, so we have to copy the original CREATESTRUCT and get the
970 * the real style. */
971 cbcs = *cs;
972 cbcs.style = GetWindowLongW(hwnd, GWL_STYLE);
974 cbtc.lpcs = &cbcs;
975 cbtc.hwndInsertAfter = HWND_TOP;
976 if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode ))
978 TRACE("CBT-hook returned !0\n");
979 goto failed;
982 /* Send the WM_GETMINMAXINFO message and fix the size if needed */
983 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
985 POINT maxSize, maxPos, minTrack, maxTrack;
987 WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
988 if (maxSize.x < cs->cx) cs->cx = maxSize.x;
989 if (maxSize.y < cs->cy) cs->cy = maxSize.y;
990 if (cs->cx < 0) cs->cx = 0;
991 if (cs->cy < 0) cs->cy = 0;
993 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
994 if (!X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER, NULL )) return FALSE;
997 /* send WM_NCCREATE */
998 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cs->cx, cs->cy );
999 if (unicode)
1000 ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1001 else
1002 ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1003 if (!ret)
1005 WARN("aborted by WM_xxCREATE!\n");
1006 return FALSE;
1009 /* make sure the window is still valid */
1010 if (!(data = X11DRV_get_win_data( hwnd ))) return FALSE;
1011 if (data->whole_window) X11DRV_sync_window_style( display, data );
1013 /* send WM_NCCALCSIZE */
1014 rect = data->window_rect;
1015 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&rect );
1017 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
1019 /* yes, even if the CBT hook was called with HWND_TOP */
1020 insert_after = ((wndPtr->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
1022 X11DRV_set_window_pos( hwnd, insert_after, &wndPtr->rectWindow, &rect, 0, NULL );
1024 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\n",
1025 hwnd, wndPtr->rectWindow.left, wndPtr->rectWindow.top,
1026 wndPtr->rectWindow.right, wndPtr->rectWindow.bottom,
1027 wndPtr->rectClient.left, wndPtr->rectClient.top,
1028 wndPtr->rectClient.right, wndPtr->rectClient.bottom,
1029 data->whole_rect.left, data->whole_rect.top,
1030 data->whole_rect.right, data->whole_rect.bottom,
1031 data->client_rect.left, data->client_rect.top,
1032 data->client_rect.right, data->client_rect.bottom,
1033 (unsigned int)data->whole_window );
1035 WIN_ReleasePtr( wndPtr );
1037 if (unicode)
1038 ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1039 else
1040 ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1042 if (!ret) return FALSE;
1044 NotifyWinEvent(EVENT_OBJECT_CREATE, hwnd, OBJID_WINDOW, 0);
1046 /* Send the size messages */
1048 if (!(wndPtr = WIN_GetPtr(hwnd)) || wndPtr == WND_OTHER_PROCESS) return FALSE;
1049 if (!(wndPtr->flags & WIN_NEED_SIZE))
1051 RECT rect = wndPtr->rectClient;
1052 WIN_ReleasePtr( wndPtr );
1053 /* send it anyway */
1054 if (((rect.right-rect.left) <0) ||((rect.bottom-rect.top)<0))
1055 WARN("sending bogus WM_SIZE message 0x%08lx\n",
1056 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1057 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1058 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1059 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( rect.left, rect.top ) );
1061 else WIN_ReleasePtr( wndPtr );
1063 /* Show the window, maximizing or minimizing if needed */
1065 style = GetWindowLongW( hwnd, GWL_STYLE );
1066 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1068 extern UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect ); /*FIXME*/
1070 RECT newPos;
1071 UINT swFlag = (style & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1072 WIN_SetStyle( hwnd, 0, WS_MAXIMIZE | WS_MINIMIZE );
1073 WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1075 swFlag = SWP_FRAMECHANGED | SWP_NOZORDER; /* Frame always gets changed */
1076 if (!(style & WS_VISIBLE) || (style & WS_CHILD) || GetActiveWindow()) swFlag |= SWP_NOACTIVATE;
1078 SetWindowPos( hwnd, 0, newPos.left, newPos.top,
1079 newPos.right, newPos.bottom, swFlag );
1082 return TRUE;
1084 failed:
1085 X11DRV_DestroyWindow( hwnd );
1086 return FALSE;
1090 /***********************************************************************
1091 * X11DRV_get_win_data
1093 * Return the X11 data structure associated with a window.
1095 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1097 char *data;
1099 if (!hwnd || XFindContext( thread_display(), (XID)hwnd, win_data_context, &data )) data = NULL;
1100 return (struct x11drv_win_data *)data;
1104 /***********************************************************************
1105 * X11DRV_get_whole_window
1107 * Return the X window associated with the full area of a window
1109 Window X11DRV_get_whole_window( HWND hwnd )
1111 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1113 if (!data) return (Window)GetPropA( hwnd, whole_window_prop );
1114 return data->whole_window;
1118 /***********************************************************************
1119 * X11DRV_get_ic
1121 * Return the X input context associated with a window
1123 XIC X11DRV_get_ic( HWND hwnd )
1125 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1127 if (!data) return 0;
1128 return data->xic;
1132 /*****************************************************************
1133 * SetParent (X11DRV.@)
1135 HWND X11DRV_SetParent( HWND hwnd, HWND parent )
1137 Display *display = thread_display();
1138 WND *wndPtr;
1139 BOOL ret;
1140 HWND old_parent = 0;
1142 /* Windows hides the window first, then shows it again
1143 * including the WM_SHOWWINDOW messages and all */
1144 BOOL was_visible = ShowWindow( hwnd, SW_HIDE );
1146 wndPtr = WIN_GetPtr( hwnd );
1147 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return 0;
1149 SERVER_START_REQ( set_parent )
1151 req->handle = hwnd;
1152 req->parent = parent;
1153 if ((ret = !wine_server_call( req )))
1155 old_parent = reply->old_parent;
1156 wndPtr->parent = parent = reply->full_parent;
1160 SERVER_END_REQ;
1161 WIN_ReleasePtr( wndPtr );
1162 if (!ret) return 0;
1164 if (parent != old_parent)
1166 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1168 if (!data) return 0;
1170 if (parent != GetDesktopWindow()) /* a child window */
1172 if (old_parent == GetDesktopWindow())
1174 /* destroy the old X windows */
1175 destroy_whole_window( display, data );
1176 destroy_icon_window( display, data );
1177 if (data->managed)
1179 data->managed = FALSE;
1180 RemovePropA( data->hwnd, managed_prop );
1184 else /* new top level window */
1186 /* FIXME: we ignore errors since we can't really recover anyway */
1187 create_whole_window( display, data, GetWindowLongW( hwnd, GWL_STYLE ) );
1191 /* SetParent additionally needs to make hwnd the topmost window
1192 in the x-order and send the expected WM_WINDOWPOSCHANGING and
1193 WM_WINDOWPOSCHANGED notification messages.
1195 SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0,
1196 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | (was_visible ? SWP_SHOWWINDOW : 0) );
1197 /* FIXME: a WM_MOVE is also generated (in the DefWindowProc handler
1198 * for WM_WINDOWPOSCHANGED) in Windows, should probably remove SWP_NOMOVE */
1200 return old_parent;
1204 /*****************************************************************
1205 * SetFocus (X11DRV.@)
1207 * Set the X focus.
1208 * Explicit colormap management seems to work only with OLVWM.
1210 void X11DRV_SetFocus( HWND hwnd )
1212 Display *display = thread_display();
1213 struct x11drv_win_data *data;
1214 XWindowAttributes win_attr;
1216 /* Only mess with the X focus if there's */
1217 /* no desktop window and if the window is not managed by the WM. */
1218 if (root_window != DefaultRootWindow(display)) return;
1220 if (!hwnd) /* If setting the focus to 0, uninstall the colormap */
1222 wine_tsx11_lock();
1223 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1224 XUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1225 wine_tsx11_unlock();
1226 return;
1229 hwnd = GetAncestor( hwnd, GA_ROOT );
1231 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1232 if (data->managed || !data->whole_window) return;
1234 /* Set X focus and install colormap */
1235 wine_tsx11_lock();
1236 if (XGetWindowAttributes( display, data->whole_window, &win_attr ) &&
1237 (win_attr.map_state == IsViewable))
1239 /* If window is not viewable, don't change anything */
1241 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1242 /* FIXME: this is not entirely correct */
1243 XSetInputFocus( display, data->whole_window, RevertToParent,
1244 /* CurrentTime */
1245 GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1246 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1247 XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1249 wine_tsx11_unlock();
1253 /**********************************************************************
1254 * SetWindowIcon (X11DRV.@)
1256 * hIcon or hIconSm has changed (or is being initialised for the
1257 * first time). Complete the X11 driver-specific initialisation
1258 * and set the window hints.
1260 * This is not entirely correct, may need to create
1261 * an icon window and set the pixmap as a background
1263 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
1265 Display *display = thread_display();
1266 struct x11drv_win_data *data;
1267 XWMHints* wm_hints;
1269 if (type != ICON_BIG) return; /* nothing to do here */
1271 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1272 if (!data->whole_window) return;
1273 if (!data->managed) return;
1275 wine_tsx11_lock();
1276 if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
1277 wine_tsx11_unlock();
1278 if (wm_hints)
1280 set_icon_hints( display, data, wm_hints, icon );
1281 wine_tsx11_lock();
1282 XSetWMHints( display, data->whole_window, wm_hints );
1283 XFree( wm_hints );
1284 wine_tsx11_unlock();