Protect sys/stat.h with a conditional.
[wine/multimedia.git] / dlls / x11drv / window.c
blob735afe52c695937293e9158e7226266d8515993c
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 <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 "winpos.h"
47 #include "mwm.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
51 /* X context to associate a hwnd to an X window */
52 XContext winContext = 0;
54 /* X context to associate a struct x11drv_win_data to an hwnd */
55 static XContext win_data_context;
57 static const char whole_window_prop[] = "__wine_x11_whole_window";
58 static const char icon_window_prop[] = "__wine_x11_icon_window";
59 static const char managed_prop[] = "__wine_x11_managed";
60 static const char visual_id_prop[] = "__wine_x11_visual_id";
62 /***********************************************************************
63 * is_window_managed
65 * Check if a given window should be managed
67 inline static BOOL is_window_managed( HWND hwnd )
69 DWORD style, ex_style;
71 if (!managed_mode) return FALSE;
72 /* tray window is always managed */
73 ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
74 if (ex_style & WS_EX_TRAYWINDOW) return TRUE;
75 /* child windows are not managed */
76 style = GetWindowLongW( hwnd, GWL_STYLE );
77 if (style & WS_CHILD) return FALSE;
78 /* windows with caption are managed */
79 if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
80 /* tool windows are not managed */
81 if (ex_style & WS_EX_TOOLWINDOW) return FALSE;
82 /* windows with thick frame are managed */
83 if (style & WS_THICKFRAME) return TRUE;
84 /* application windows are managed */
85 if (ex_style & WS_EX_APPWINDOW) return TRUE;
86 /* full-screen popup windows are managed */
87 if (style & WS_POPUP)
89 RECT rect;
90 GetWindowRect( hwnd, &rect );
91 if ((rect.right - rect.left) == screen_width && (rect.bottom - rect.top) == screen_height)
92 return TRUE;
94 /* default: not managed */
95 return FALSE;
99 /***********************************************************************
100 * X11DRV_is_window_rect_mapped
102 * Check if the X whole window should be mapped based on its rectangle
104 BOOL X11DRV_is_window_rect_mapped( const RECT *rect )
106 /* don't map if rect is empty */
107 if (IsRectEmpty( rect )) return FALSE;
109 /* don't map if rect is off-screen */
110 if (rect->left >= (int)screen_width || rect->top >= (int)screen_height) return FALSE;
111 if (rect->right < 0 || rect->bottom < 0) return FALSE;
113 return TRUE;
117 /***********************************************************************
118 * get_window_attributes
120 * Fill the window attributes structure for an X window.
122 static int get_window_attributes( struct x11drv_win_data *data, XSetWindowAttributes *attr )
124 if (!data->managed && !using_wine_desktop && is_window_managed( data->hwnd ))
126 data->managed = TRUE;
127 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
129 attr->override_redirect = !data->managed;
130 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
131 attr->save_under = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
132 attr->cursor = x11drv_thread_data()->cursor;
133 return (CWOverrideRedirect | CWSaveUnder | 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, struct x11drv_win_data *data )
144 XSetWindowAttributes attr;
145 int mask = get_window_attributes( data, &attr );
147 wine_tsx11_lock();
148 XChangeWindowAttributes( display, data->whole_window, mask, &attr );
149 wine_tsx11_unlock();
153 /***********************************************************************
154 * get_window_changes
156 * fill the window changes structure
158 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
160 int mask = 0;
162 if (old->right - old->left != new->right - new->left )
164 if (!(changes->width = new->right - new->left)) changes->width = 1;
165 mask |= CWWidth;
167 if (old->bottom - old->top != new->bottom - new->top)
169 if (!(changes->height = new->bottom - new->top)) changes->height = 1;
170 mask |= CWHeight;
172 if (old->left != new->left)
174 changes->x = new->left;
175 mask |= CWX;
177 if (old->top != new->top)
179 changes->y = new->top;
180 mask |= CWY;
182 return mask;
186 /***********************************************************************
187 * create_icon_window
189 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
191 XSetWindowAttributes attr;
193 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
194 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
195 attr.bit_gravity = NorthWestGravity;
196 attr.backing_store = NotUseful/*WhenMapped*/;
197 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
199 wine_tsx11_lock();
200 data->icon_window = XCreateWindow( display, root_window, 0, 0,
201 GetSystemMetrics( SM_CXICON ),
202 GetSystemMetrics( SM_CYICON ),
203 0, screen_depth,
204 InputOutput, visual,
205 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
206 XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
207 wine_tsx11_unlock();
209 TRACE( "created %lx\n", data->icon_window );
210 SetPropA( data->hwnd, icon_window_prop, (HANDLE)data->icon_window );
211 return data->icon_window;
216 /***********************************************************************
217 * destroy_icon_window
219 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
221 if (!data->icon_window) return;
222 if (x11drv_thread_data()->cursor_window == data->icon_window)
223 x11drv_thread_data()->cursor_window = None;
224 wine_tsx11_lock();
225 XDeleteContext( display, data->icon_window, winContext );
226 XDestroyWindow( display, data->icon_window );
227 data->icon_window = 0;
228 wine_tsx11_unlock();
229 RemovePropA( data->hwnd, icon_window_prop );
233 /***********************************************************************
234 * set_icon_hints
236 * Set the icon wm hints
238 static void set_icon_hints( Display *display, struct x11drv_win_data *data,
239 XWMHints *hints, HICON hIcon )
241 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
242 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
243 data->hWMIconBitmap = 0;
244 data->hWMIconMask = 0;
246 if (!data->managed)
248 destroy_icon_window( display, data );
249 hints->flags &= ~(IconPixmapHint | IconMaskHint | IconWindowHint);
251 else if (!hIcon)
253 if (!data->icon_window) create_icon_window( display, data );
254 hints->icon_window = data->icon_window;
255 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
257 else
259 HBITMAP hbmOrig;
260 RECT rcMask;
261 BITMAP bmMask;
262 ICONINFO ii;
263 HDC hDC;
265 GetIconInfo(hIcon, &ii);
267 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
268 rcMask.top = 0;
269 rcMask.left = 0;
270 rcMask.right = bmMask.bmWidth;
271 rcMask.bottom = bmMask.bmHeight;
273 hDC = CreateCompatibleDC(0);
274 hbmOrig = SelectObject(hDC, ii.hbmMask);
275 InvertRect(hDC, &rcMask);
276 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
277 SelectObject(hDC, hbmOrig);
278 DeleteDC(hDC);
280 data->hWMIconBitmap = ii.hbmColor;
281 data->hWMIconMask = ii.hbmMask;
283 hints->icon_pixmap = X11DRV_get_pixmap(data->hWMIconBitmap);
284 hints->icon_mask = X11DRV_get_pixmap(data->hWMIconMask);
285 destroy_icon_window( display, data );
286 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
291 /***********************************************************************
292 * set_size_hints
294 * set the window size hints
296 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
298 XSizeHints* size_hints;
300 if ((size_hints = XAllocSizeHints()))
302 size_hints->win_gravity = StaticGravity;
303 size_hints->x = data->whole_rect.left;
304 size_hints->y = data->whole_rect.top;
305 size_hints->flags = PWinGravity | PPosition;
307 if ( !(style & WS_THICKFRAME) )
309 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
310 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
311 size_hints->min_width = size_hints->max_width;
312 size_hints->min_height = size_hints->max_height;
313 size_hints->flags |= PMinSize | PMaxSize;
315 XSetWMNormalHints( display, data->whole_window, size_hints );
316 XFree( size_hints );
321 /***********************************************************************
322 * get_process_name
324 * get the name of the current process for setting class hints
326 static char *get_process_name(void)
328 static char *name;
330 if (!name)
332 WCHAR module[MAX_PATH];
333 DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
334 if (len && len < MAX_PATH)
336 char *ptr;
337 WCHAR *p, *appname = module;
339 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
340 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
341 len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
342 if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
344 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
345 name = ptr;
349 return name;
353 /***********************************************************************
354 * X11DRV_set_wm_hints
356 * Set the window manager hints for a newly-created window
358 void X11DRV_set_wm_hints( Display *display, struct x11drv_win_data *data )
360 Window group_leader;
361 XClassHint *class_hints;
362 XWMHints* wm_hints;
363 Atom protocols[3];
364 MwmHints mwm_hints;
365 Atom dndVersion = 4;
366 int i;
367 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
368 DWORD ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
369 HWND owner = GetWindow( data->hwnd, GW_OWNER );
370 char *process_name = get_process_name();
372 /* transient for hint */
373 if (owner)
375 Window owner_win = X11DRV_get_whole_window( owner );
376 wine_tsx11_lock();
377 XSetTransientForHint( display, data->whole_window, owner_win );
378 wine_tsx11_unlock();
379 group_leader = owner_win;
381 else group_leader = data->whole_window;
383 wine_tsx11_lock();
385 /* wm protocols */
386 i = 0;
387 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
388 protocols[i++] = x11drv_atom(_NET_WM_PING);
389 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
390 XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
391 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
393 /* class hints */
394 if ((class_hints = XAllocClassHint()))
396 class_hints->res_name = process_name;
397 class_hints->res_class = "Wine";
398 XSetClassHint( display, data->whole_window, class_hints );
399 XFree( class_hints );
402 /* size hints */
403 set_size_hints( display, data, style );
405 /* systray properties (KDE only for now) */
406 if (ex_style & WS_EX_TRAYWINDOW)
408 int val = 1;
409 XChangeProperty( display, data->whole_window, x11drv_atom(KWM_DOCKWINDOW),
410 x11drv_atom(KWM_DOCKWINDOW), 32, PropModeReplace, (unsigned char*)&val, 1 );
411 XChangeProperty( display, data->whole_window, x11drv_atom(_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR),
412 XA_WINDOW, 32, PropModeReplace, (unsigned char*)&data->whole_window, 1 );
415 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
416 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
417 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
418 i = getpid();
419 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
420 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
422 /* map WS_EX_TOOLWINDOW to _NET_WM_WINDOW_TYPE_UTILITY */
423 if (ex_style & WS_EX_TOOLWINDOW)
425 Atom a = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
426 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
427 XA_ATOM, 32, PropModeReplace, (unsigned char*)&a, 1);
430 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
431 mwm_hints.functions = 0;
432 if ((style & WS_CAPTION) == WS_CAPTION) mwm_hints.functions |= MWM_FUNC_MOVE;
433 if (style & WS_THICKFRAME) mwm_hints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
434 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
435 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
436 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
437 if (ex_style & WS_EX_APPWINDOW) mwm_hints.functions |= MWM_FUNC_MOVE;
438 mwm_hints.decorations = 0;
439 if ((style & WS_CAPTION) == WS_CAPTION)
441 mwm_hints.decorations |= MWM_DECOR_TITLE;
442 if (style & WS_SYSMENU) mwm_hints.decorations |= MWM_DECOR_MENU;
443 if (style & WS_MINIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MINIMIZE;
444 if (style & WS_MAXIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MAXIMIZE;
446 if (ex_style & WS_EX_DLGMODALFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
447 else if (style & WS_THICKFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
448 else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
449 else if (style & WS_BORDER) mwm_hints.decorations |= MWM_DECOR_BORDER;
450 else if (!(style & (WS_CHILD|WS_POPUP))) mwm_hints.decorations |= MWM_DECOR_BORDER;
452 XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
453 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
454 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
456 XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
457 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
459 wm_hints = XAllocWMHints();
460 wine_tsx11_unlock();
462 /* wm hints */
463 if (wm_hints)
465 wm_hints->flags = InputHint | StateHint | WindowGroupHint;
466 wm_hints->input = !(style & WS_DISABLED);
468 set_icon_hints( display, data, wm_hints,
469 (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON ) );
471 wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
472 wm_hints->window_group = group_leader;
474 wine_tsx11_lock();
475 XSetWMHints( display, data->whole_window, wm_hints );
476 XFree(wm_hints);
477 wine_tsx11_unlock();
482 /***********************************************************************
483 * X11DRV_set_iconic_state
485 * Set the X11 iconic state according to the window style.
487 void X11DRV_set_iconic_state( HWND hwnd )
489 Display *display = thread_display();
490 struct x11drv_win_data *data;
491 RECT rect;
492 XWMHints* wm_hints;
493 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
494 BOOL iconic = (style & WS_MINIMIZE) != 0;
496 if (!(data = X11DRV_get_win_data( hwnd ))) return;
497 if (!data->whole_window) return;
499 GetWindowRect( hwnd, &rect );
501 wine_tsx11_lock();
503 if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
504 wm_hints->flags |= StateHint | IconPositionHint;
505 wm_hints->initial_state = iconic ? IconicState : NormalState;
506 wm_hints->icon_x = rect.left;
507 wm_hints->icon_y = rect.top;
508 XSetWMHints( display, data->whole_window, wm_hints );
510 if (style & WS_VISIBLE)
512 if (iconic)
513 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
514 else
515 if (X11DRV_is_window_rect_mapped( &rect ))
516 XMapWindow( display, data->whole_window );
519 XFree(wm_hints);
520 wine_tsx11_unlock();
524 /***********************************************************************
525 * X11DRV_window_to_X_rect
527 * Convert a rect from client to X window coordinates
529 void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
531 RECT rc;
533 if (!data->managed) return;
534 if (IsRectEmpty( rect )) return;
536 rc.top = rc.bottom = rc.left = rc.right = 0;
538 AdjustWindowRectEx( &rc, GetWindowLongW( data->hwnd, GWL_STYLE ) & ~(WS_HSCROLL|WS_VSCROLL),
539 FALSE, GetWindowLongW( data->hwnd, GWL_EXSTYLE ) );
541 rect->left -= rc.left;
542 rect->right -= rc.right;
543 rect->top -= rc.top;
544 rect->bottom -= rc.bottom;
545 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
546 if (rect->left >= rect->right) rect->right = rect->left + 1;
550 /***********************************************************************
551 * X11DRV_X_to_window_rect
553 * Opposite of X11DRV_window_to_X_rect
555 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
557 if (!data->managed) return;
558 if (IsRectEmpty( rect )) return;
560 AdjustWindowRectEx( rect, GetWindowLongW( data->hwnd, GWL_STYLE ) & ~(WS_HSCROLL|WS_VSCROLL),
561 FALSE, GetWindowLongW( data->hwnd, GWL_EXSTYLE ));
563 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
564 if (rect->left >= rect->right) rect->right = rect->left + 1;
568 /***********************************************************************
569 * X11DRV_sync_window_position
571 * Synchronize the X window position with the Windows one
573 void X11DRV_sync_window_position( Display *display, struct x11drv_win_data *data,
574 UINT swp_flags, const RECT *new_client_rect,
575 const RECT *new_whole_rect )
577 XWindowChanges changes;
578 int mask;
579 RECT old_whole_rect;
581 old_whole_rect = data->whole_rect;
582 data->whole_rect = *new_whole_rect;
584 data->client_rect = *new_client_rect;
585 OffsetRect( &data->client_rect, -data->whole_rect.left, -data->whole_rect.top );
587 if (!data->whole_window) return;
588 if (swp_flags & SWP_WINE_NOHOSTMOVE) return;
590 mask = get_window_changes( &changes, &old_whole_rect, &data->whole_rect );
592 if (!(swp_flags & SWP_NOZORDER))
594 /* find window that this one must be after */
595 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
596 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
597 prev = GetWindow( prev, GW_HWNDPREV );
598 if (!prev) /* top child */
600 changes.stack_mode = Above;
601 mask |= CWStackMode;
603 else
605 /* should use stack_mode Below but most window managers don't get it right */
606 /* so move it above the next one in Z order */
607 HWND next = GetWindow( data->hwnd, GW_HWNDNEXT );
608 while (next && !(GetWindowLongW( next, GWL_STYLE ) & WS_VISIBLE))
609 next = GetWindow( next, GW_HWNDNEXT );
610 if (next)
612 changes.stack_mode = Above;
613 changes.sibling = X11DRV_get_whole_window(next);
614 mask |= CWStackMode | CWSibling;
619 if (mask)
621 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
623 TRACE( "setting win %lx pos %ld,%ld,%ldx%ld after %lx changes=%x\n",
624 data->whole_window, data->whole_rect.left, data->whole_rect.top,
625 data->whole_rect.right - data->whole_rect.left,
626 data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
628 wine_tsx11_lock();
629 XSync( gdi_display, False ); /* flush graphics operations before moving the window */
630 if (mask & (CWWidth|CWHeight)) set_size_hints( display, data, style );
631 XReconfigureWMWindow( display, data->whole_window,
632 DefaultScreen(display), mask, &changes );
633 wine_tsx11_unlock();
638 /**********************************************************************
639 * create_whole_window
641 * Create the whole X window for a given window
643 static Window create_whole_window( Display *display, struct x11drv_win_data *data, DWORD style )
645 int cx, cy, mask;
646 XSetWindowAttributes attr;
647 XIM xim;
648 RECT rect;
650 rect = data->window_rect;
651 X11DRV_window_to_X_rect( data, &rect );
653 if (!(cx = rect.right - rect.left)) cx = 1;
654 if (!(cy = rect.bottom - rect.top)) cy = 1;
656 mask = get_window_attributes( data, &attr );
658 /* set the attributes that don't change over the lifetime of the window */
659 attr.bit_gravity = NorthWestGravity;
660 attr.backing_store = NotUseful;
661 attr.event_mask = (ExposureMask | PointerMotionMask |
662 ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
663 KeyPressMask | KeyReleaseMask | StructureNotifyMask |
664 FocusChangeMask | KeymapStateMask);
665 mask |= CWBitGravity | CWBackingStore | CWEventMask;
667 wine_tsx11_lock();
669 data->whole_rect = rect;
670 data->whole_window = XCreateWindow( display, root_window, rect.left, rect.top, cx, cy,
671 0, screen_depth, InputOutput, visual,
672 mask, &attr );
674 if (!data->whole_window)
676 wine_tsx11_unlock();
677 return 0;
679 XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
681 /* non-maximized child must be at bottom of Z order */
682 if ((style & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
684 XWindowChanges changes;
685 changes.stack_mode = Below;
686 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
688 XSync( display, False ); /* FIXME: should not be needed */
690 wine_tsx11_unlock();
692 xim = x11drv_thread_data()->xim;
693 if (xim) data->xic = X11DRV_CreateIC( xim, display, data->whole_window );
695 X11DRV_set_wm_hints( display, data );
697 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
698 return data->whole_window;
702 /**********************************************************************
703 * destroy_whole_window
705 * Destroy the whole X window for a given window.
707 static void destroy_whole_window( Display *display, struct x11drv_win_data *data )
709 struct x11drv_thread_data *thread_data = x11drv_thread_data();
711 if (!data->whole_window) return;
713 TRACE( "win %p xwin %lx\n", data->hwnd, data->whole_window );
714 if (thread_data->cursor_window == data->whole_window) thread_data->cursor_window = None;
715 wine_tsx11_lock();
716 XSync( gdi_display, False ); /* flush any reference to this drawable in GDI queue */
717 XDeleteContext( display, data->whole_window, winContext );
718 XDestroyWindow( display, data->whole_window ); /* this destroys client too */
719 data->whole_window = 0;
720 if (data->xic)
722 XUnsetICFocus( data->xic );
723 XDestroyIC( data->xic );
725 wine_tsx11_unlock();
726 RemovePropA( data->hwnd, whole_window_prop );
730 /*****************************************************************
731 * SetWindowText (X11DRV.@)
733 void X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
735 Display *display = thread_display();
736 UINT count;
737 char *buffer;
738 char *utf8_buffer;
739 Window win;
740 XTextProperty prop;
742 if ((win = X11DRV_get_whole_window( hwnd )))
744 /* allocate new buffer for window text */
745 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
746 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count )))
748 ERR("Not enough memory for window text\n");
749 return;
751 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
753 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
754 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
756 ERR("Not enough memory for window text in UTF-8\n");
757 HeapFree( GetProcessHeap(), 0, buffer );
758 return;
760 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
762 wine_tsx11_lock();
763 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
765 XSetWMName( display, win, &prop );
766 XSetWMIconName( display, win, &prop );
767 XFree( prop.value );
770 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
771 according to the standard
772 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
774 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
775 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
776 wine_tsx11_unlock();
778 HeapFree( GetProcessHeap(), 0, utf8_buffer );
779 HeapFree( GetProcessHeap(), 0, buffer );
784 /***********************************************************************
785 * DestroyWindow (X11DRV.@)
787 void X11DRV_DestroyWindow( HWND hwnd )
789 struct x11drv_thread_data *thread_data = x11drv_thread_data();
790 Display *display = thread_data->display;
791 struct x11drv_win_data *data;
793 if (!(data = X11DRV_get_win_data( hwnd ))) return;
795 free_window_dce( data );
796 destroy_whole_window( display, data );
797 destroy_icon_window( display, data );
799 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
800 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
801 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
802 wine_tsx11_lock();
803 XDeleteContext( display, (XID)hwnd, win_data_context );
804 wine_tsx11_unlock();
805 HeapFree( GetProcessHeap(), 0, data );
809 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
811 struct x11drv_win_data *data;
813 if ((data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data))))
815 data->hwnd = hwnd;
816 data->whole_window = 0;
817 data->icon_window = 0;
818 data->xic = 0;
819 data->managed = FALSE;
820 data->dce = NULL;
821 data->hWMIconBitmap = 0;
822 data->hWMIconMask = 0;
824 wine_tsx11_lock();
825 if (!winContext) winContext = XUniqueContext();
826 if (!win_data_context) win_data_context = XUniqueContext();
827 XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
828 wine_tsx11_unlock();
830 return data;
834 /**********************************************************************
835 * CreateDesktopWindow (X11DRV.@)
837 BOOL X11DRV_CreateDesktopWindow( HWND hwnd )
839 Display *display = thread_display();
840 VisualID visualid;
841 struct x11drv_win_data *data;
842 RECT rect;
844 if (!(data = alloc_win_data( display, hwnd ))) return FALSE;
845 data->whole_window = root_window;
847 SetRect( &rect, 0, 0, screen_width, screen_height );
848 X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER, NULL );
850 wine_tsx11_lock();
851 visualid = XVisualIDFromVisual(visual);
852 wine_tsx11_unlock();
854 SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
855 SetPropA( data->hwnd, visual_id_prop, (HANDLE)visualid );
857 if (root_window != DefaultRootWindow(display) && !desktop_tid) X11DRV_create_desktop_thread();
859 return TRUE;
863 /**********************************************************************
864 * CreateWindow (X11DRV.@)
866 BOOL X11DRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
868 Display *display = thread_display();
869 WND *wndPtr;
870 struct x11drv_win_data *data;
871 HWND insert_after;
872 RECT rect;
873 DWORD style;
874 CBT_CREATEWNDA cbtc;
875 CREATESTRUCTA cbcs;
876 BOOL ret = FALSE;
878 if (!(data = alloc_win_data( display, hwnd ))) return FALSE;
880 if (cs->cx > 65535)
882 ERR( "invalid window width %d\n", cs->cx );
883 cs->cx = 65535;
885 if (cs->cy > 65535)
887 ERR( "invalid window height %d\n", cs->cy );
888 cs->cy = 65535;
890 if (cs->cx < 0)
892 ERR( "invalid window width %d\n", cs->cx );
893 cs->cx = 0;
895 if (cs->cy < 0)
897 ERR( "invalid window height %d\n", cs->cy );
898 cs->cy = 0;
901 /* initialize the dimensions before sending WM_GETMINMAXINFO */
902 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
903 X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER, NULL );
905 /* create an X window if it's a top level window */
906 if (GetAncestor( hwnd, GA_PARENT ) == GetDesktopWindow())
908 if (!create_whole_window( display, data, cs->style )) goto failed;
911 /* get class or window DC if needed */
912 alloc_window_dce( data );
914 /* Call the WH_CBT hook */
916 /* the window style passed to the hook must be the real window style,
917 * rather than just the window style that the caller to CreateWindowEx
918 * passed in, so we have to copy the original CREATESTRUCT and get the
919 * the real style. */
920 cbcs = *cs;
921 cbcs.style = GetWindowLongW(hwnd, GWL_STYLE);
923 cbtc.lpcs = &cbcs;
924 cbtc.hwndInsertAfter = HWND_TOP;
925 if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode ))
927 TRACE("CBT-hook returned !0\n");
928 goto failed;
931 /* Send the WM_GETMINMAXINFO message and fix the size if needed */
932 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
934 POINT maxSize, maxPos, minTrack, maxTrack;
936 WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
937 if (maxSize.x < cs->cx) cs->cx = maxSize.x;
938 if (maxSize.y < cs->cy) cs->cy = maxSize.y;
939 if (cs->cx < 0) cs->cx = 0;
940 if (cs->cy < 0) cs->cy = 0;
942 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
943 if (!X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER, NULL )) return FALSE;
946 /* send WM_NCCREATE */
947 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cs->cx, cs->cy );
948 if (unicode)
949 ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
950 else
951 ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
952 if (!ret)
954 WARN("aborted by WM_xxCREATE!\n");
955 return FALSE;
958 /* make sure the window is still valid */
959 if (!(data = X11DRV_get_win_data( hwnd ))) return FALSE;
960 if (data->whole_window) X11DRV_sync_window_style( display, data );
962 /* send WM_NCCALCSIZE */
963 rect = data->window_rect;
964 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&rect );
966 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
968 /* yes, even if the CBT hook was called with HWND_TOP */
969 insert_after = ((wndPtr->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
971 X11DRV_set_window_pos( hwnd, insert_after, &wndPtr->rectWindow, &rect, 0, NULL );
973 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",
974 hwnd, wndPtr->rectWindow.left, wndPtr->rectWindow.top,
975 wndPtr->rectWindow.right, wndPtr->rectWindow.bottom,
976 wndPtr->rectClient.left, wndPtr->rectClient.top,
977 wndPtr->rectClient.right, wndPtr->rectClient.bottom,
978 data->whole_rect.left, data->whole_rect.top,
979 data->whole_rect.right, data->whole_rect.bottom,
980 data->client_rect.left, data->client_rect.top,
981 data->client_rect.right, data->client_rect.bottom,
982 (unsigned int)data->whole_window );
984 WIN_ReleasePtr( wndPtr );
986 if (unicode)
987 ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
988 else
989 ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
991 if (!ret) return FALSE;
993 NotifyWinEvent(EVENT_OBJECT_CREATE, hwnd, OBJID_WINDOW, 0);
995 /* Send the size messages */
997 if (!(wndPtr = WIN_GetPtr(hwnd)) || wndPtr == WND_OTHER_PROCESS) return FALSE;
998 if (!(wndPtr->flags & WIN_NEED_SIZE))
1000 RECT rect = wndPtr->rectClient;
1001 WIN_ReleasePtr( wndPtr );
1002 /* send it anyway */
1003 if (((rect.right-rect.left) <0) ||((rect.bottom-rect.top)<0))
1004 WARN("sending bogus WM_SIZE message 0x%08lx\n",
1005 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1006 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1007 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1008 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( rect.left, rect.top ) );
1010 else WIN_ReleasePtr( wndPtr );
1012 /* Show the window, maximizing or minimizing if needed */
1014 style = GetWindowLongW( hwnd, GWL_STYLE );
1015 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1017 extern UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect ); /*FIXME*/
1019 RECT newPos;
1020 UINT swFlag = (style & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1021 WIN_SetStyle( hwnd, 0, WS_MAXIMIZE | WS_MINIMIZE );
1022 WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1023 swFlag = ((style & WS_CHILD) || GetActiveWindow())
1024 ? SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED
1025 : SWP_NOZORDER | SWP_FRAMECHANGED;
1026 SetWindowPos( hwnd, 0, newPos.left, newPos.top,
1027 newPos.right, newPos.bottom, swFlag );
1030 return TRUE;
1032 failed:
1033 X11DRV_DestroyWindow( hwnd );
1034 return FALSE;
1038 /***********************************************************************
1039 * X11DRV_get_win_data
1041 * Return the X11 data structure associated with a window.
1043 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1045 char *data;
1047 if (!hwnd || XFindContext( thread_display(), (XID)hwnd, win_data_context, &data )) data = NULL;
1048 return (struct x11drv_win_data *)data;
1052 /***********************************************************************
1053 * X11DRV_get_whole_window
1055 * Return the X window associated with the full area of a window
1057 Window X11DRV_get_whole_window( HWND hwnd )
1059 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1061 if (!data) return (Window)GetPropA( hwnd, whole_window_prop );
1062 return data->whole_window;
1066 /***********************************************************************
1067 * X11DRV_get_ic
1069 * Return the X input context associated with a window
1071 XIC X11DRV_get_ic( HWND hwnd )
1073 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1075 if (!data) return 0;
1076 return data->xic;
1080 /*****************************************************************
1081 * SetParent (X11DRV.@)
1083 HWND X11DRV_SetParent( HWND hwnd, HWND parent )
1085 Display *display = thread_display();
1086 WND *wndPtr;
1087 BOOL ret;
1088 HWND old_parent = 0;
1090 /* Windows hides the window first, then shows it again
1091 * including the WM_SHOWWINDOW messages and all */
1092 BOOL was_visible = ShowWindow( hwnd, SW_HIDE );
1094 wndPtr = WIN_GetPtr( hwnd );
1095 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return 0;
1097 SERVER_START_REQ( set_parent )
1099 req->handle = hwnd;
1100 req->parent = parent;
1101 if ((ret = !wine_server_call( req )))
1103 old_parent = reply->old_parent;
1104 wndPtr->parent = parent = reply->full_parent;
1108 SERVER_END_REQ;
1109 WIN_ReleasePtr( wndPtr );
1110 if (!ret) return 0;
1112 if (parent != old_parent)
1114 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1116 if (!data) return 0;
1118 if (parent != GetDesktopWindow()) /* a child window */
1120 if (old_parent == GetDesktopWindow())
1122 /* destroy the old X windows */
1123 destroy_whole_window( display, data );
1124 destroy_icon_window( display, data );
1125 if (data->managed)
1127 data->managed = FALSE;
1128 RemovePropA( data->hwnd, managed_prop );
1132 else /* new top level window */
1134 /* FIXME: we ignore errors since we can't really recover anyway */
1135 create_whole_window( display, data, GetWindowLongW( hwnd, GWL_STYLE ) );
1139 /* SetParent additionally needs to make hwnd the topmost window
1140 in the x-order and send the expected WM_WINDOWPOSCHANGING and
1141 WM_WINDOWPOSCHANGED notification messages.
1143 SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0,
1144 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | (was_visible ? SWP_SHOWWINDOW : 0) );
1145 /* FIXME: a WM_MOVE is also generated (in the DefWindowProc handler
1146 * for WM_WINDOWPOSCHANGED) in Windows, should probably remove SWP_NOMOVE */
1148 return old_parent;
1152 /*****************************************************************
1153 * SetFocus (X11DRV.@)
1155 * Set the X focus.
1156 * Explicit colormap management seems to work only with OLVWM.
1158 void X11DRV_SetFocus( HWND hwnd )
1160 Display *display = thread_display();
1161 struct x11drv_win_data *data;
1162 XWindowAttributes win_attr;
1164 /* Only mess with the X focus if there's */
1165 /* no desktop window and if the window is not managed by the WM. */
1166 if (root_window != DefaultRootWindow(display)) return;
1168 if (!hwnd) /* If setting the focus to 0, uninstall the colormap */
1170 wine_tsx11_lock();
1171 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1172 XUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1173 wine_tsx11_unlock();
1174 return;
1177 hwnd = GetAncestor( hwnd, GA_ROOT );
1179 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1180 if (data->managed || !data->whole_window) return;
1182 /* Set X focus and install colormap */
1183 wine_tsx11_lock();
1184 if (XGetWindowAttributes( display, data->whole_window, &win_attr ) &&
1185 (win_attr.map_state == IsViewable))
1187 /* If window is not viewable, don't change anything */
1189 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1190 /* FIXME: this is not entirely correct */
1191 XSetInputFocus( display, data->whole_window, RevertToParent,
1192 /* CurrentTime */
1193 GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1194 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1195 XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1197 wine_tsx11_unlock();
1201 /**********************************************************************
1202 * SetWindowIcon (X11DRV.@)
1204 * hIcon or hIconSm has changed (or is being initialised for the
1205 * first time). Complete the X11 driver-specific initialisation
1206 * and set the window hints.
1208 * This is not entirely correct, may need to create
1209 * an icon window and set the pixmap as a background
1211 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
1213 Display *display = thread_display();
1214 struct x11drv_win_data *data;
1215 XWMHints* wm_hints;
1217 if (type != ICON_BIG) return; /* nothing to do here */
1219 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1220 if (!data->whole_window) return;
1221 if (!data->managed) return;
1223 wine_tsx11_lock();
1224 if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
1225 wine_tsx11_unlock();
1226 if (wm_hints)
1228 set_icon_hints( display, data, wm_hints, icon );
1229 wine_tsx11_lock();
1230 XSetWMHints( display, data->whole_window, wm_hints );
1231 XFree( wm_hints );
1232 wine_tsx11_unlock();