Moved a few remaining 16-bit window functions to wnd16.c and moved it
[wine.git] / dlls / x11drv / window.c
blob190bb19fd643d5c180297f78879b4fb3b41b6dfa
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 <X11/Xresource.h>
29 #include <X11/Xutil.h>
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winreg.h"
34 #include "winuser.h"
35 #include "wine/unicode.h"
37 #include "wine/debug.h"
38 #include "x11drv.h"
39 #include "win.h"
40 #include "winpos.h"
41 #include "dce.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 (!managed_mode) return FALSE;
78 /* tray window is always managed */
79 if (win->dwExStyle & WS_EX_TRAYWINDOW) return TRUE;
80 /* child windows are not managed */
81 if (win->dwStyle & WS_CHILD) return FALSE;
82 /* tool windows are not managed */
83 if (win->dwExStyle & WS_EX_TOOLWINDOW) return FALSE;
84 /* windows with caption or thick frame are managed */
85 if ((win->dwStyle & WS_CAPTION) == WS_CAPTION) return TRUE;
86 if (win->dwStyle & WS_THICKFRAME) return TRUE;
87 /* default: not managed */
88 return FALSE;
92 /***********************************************************************
93 * is_client_window_mapped
95 * Check if the X client window should be mapped
97 inline static BOOL is_client_window_mapped( WND *win )
99 struct x11drv_win_data *data = win->pDriverData;
100 return !(win->dwStyle & WS_MINIMIZE) && !IsRectEmpty( &data->client_rect );
104 /***********************************************************************
105 * get_window_attributes
107 * Fill the window attributes structure for an X window.
109 static int get_window_attributes( Display *display, WND *win, XSetWindowAttributes *attr )
111 BOOL is_top_level = is_window_top_level( win );
112 BOOL managed = is_top_level && is_window_managed( win );
114 if (managed) WIN_SetExStyle( win->hwndSelf, win->dwExStyle | WS_EX_MANAGED );
115 else WIN_SetExStyle( win->hwndSelf, win->dwExStyle & ~WS_EX_MANAGED );
117 attr->override_redirect = !managed;
118 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
119 attr->save_under = ((win->clsStyle & CS_SAVEBITS) != 0);
120 attr->cursor = x11drv_thread_data()->cursor;
121 attr->event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
122 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
124 if (is_window_top_level( win ))
125 attr->event_mask |= StructureNotifyMask | FocusChangeMask | KeymapStateMask;
127 return (CWOverrideRedirect | CWSaveUnder | CWEventMask | CWColormap | CWCursor);
131 /***********************************************************************
132 * X11DRV_sync_window_style
134 * Change the X window attributes when the window style has changed.
136 void X11DRV_sync_window_style( Display *display, WND *win )
138 XSetWindowAttributes attr;
139 int mask;
141 wine_tsx11_lock();
142 mask = get_window_attributes( display, win, &attr );
143 XChangeWindowAttributes( display, get_whole_window(win), mask, &attr );
144 wine_tsx11_unlock();
148 /***********************************************************************
149 * get_window_changes
151 * fill the window changes structure
153 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
155 int mask = 0;
157 if (old->right - old->left != new->right - new->left )
159 if (!(changes->width = new->right - new->left)) changes->width = 1;
160 mask |= CWWidth;
162 if (old->bottom - old->top != new->bottom - new->top)
164 if (!(changes->height = new->bottom - new->top)) changes->height = 1;
165 mask |= CWHeight;
167 if (old->left != new->left)
169 changes->x = new->left;
170 mask |= CWX;
172 if (old->top != new->top)
174 changes->y = new->top;
175 mask |= CWY;
177 return mask;
181 /***********************************************************************
182 * create_icon_window
184 static Window create_icon_window( Display *display, WND *win )
186 struct x11drv_win_data *data = win->pDriverData;
187 XSetWindowAttributes attr;
189 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
190 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
191 attr.bit_gravity = NorthWestGravity;
192 attr.backing_store = NotUseful/*WhenMapped*/;
193 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
195 wine_tsx11_lock();
196 data->icon_window = XCreateWindow( display, root_window, 0, 0,
197 GetSystemMetrics( SM_CXICON ),
198 GetSystemMetrics( SM_CYICON ),
199 0, screen_depth,
200 InputOutput, visual,
201 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
202 XSaveContext( display, data->icon_window, winContext, (char *)win->hwndSelf );
203 wine_tsx11_unlock();
205 TRACE( "created %lx\n", data->icon_window );
206 SetPropA( win->hwndSelf, icon_window_atom, (HANDLE)data->icon_window );
207 return data->icon_window;
212 /***********************************************************************
213 * destroy_icon_window
215 inline static void destroy_icon_window( Display *display, WND *win )
217 struct x11drv_win_data *data = win->pDriverData;
219 if (!data->icon_window) return;
220 if (x11drv_thread_data()->cursor_window == data->icon_window)
221 x11drv_thread_data()->cursor_window = None;
222 wine_tsx11_lock();
223 XDeleteContext( display, data->icon_window, winContext );
224 XDestroyWindow( display, data->icon_window );
225 data->icon_window = 0;
226 wine_tsx11_unlock();
227 RemovePropA( win->hwndSelf, icon_window_atom );
231 /***********************************************************************
232 * set_icon_hints
234 * Set the icon wm hints
236 static void set_icon_hints( Display *display, WND *wndPtr, XWMHints *hints )
238 X11DRV_WND_DATA *data = wndPtr->pDriverData;
239 HICON hIcon = (HICON)GetClassLongA( wndPtr->hwndSelf, GCL_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 (!(wndPtr->dwExStyle & WS_EX_MANAGED))
248 destroy_icon_window( display, wndPtr );
249 hints->flags &= ~(IconPixmapHint | IconMaskHint | IconWindowHint);
251 else if (!hIcon)
253 if (!data->icon_window) create_icon_window( display, wndPtr );
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_BITMAP_Pixmap(data->hWMIconBitmap);
284 hints->icon_mask = X11DRV_BITMAP_Pixmap(data->hWMIconMask);
285 destroy_icon_window( display, wndPtr );
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, WND *win )
298 XSizeHints* size_hints;
299 struct x11drv_win_data *data = win->pDriverData;
301 if ((size_hints = XAllocSizeHints()))
303 size_hints->win_gravity = StaticGravity;
304 size_hints->x = data->whole_rect.left;
305 size_hints->y = data->whole_rect.top;
306 size_hints->flags = PWinGravity | PPosition;
308 if (HAS_DLGFRAME( win->dwStyle, win->dwExStyle ))
310 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
311 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
312 size_hints->min_width = size_hints->max_width;
313 size_hints->min_height = size_hints->max_height;
314 size_hints->flags |= PMinSize | PMaxSize;
316 XSetWMNormalHints( display, data->whole_window, size_hints );
317 XFree( size_hints );
322 /***********************************************************************
323 * X11DRV_set_wm_hints
325 * Set the window manager hints for a newly-created window
327 void X11DRV_set_wm_hints( Display *display, WND *win )
329 struct x11drv_win_data *data = win->pDriverData;
330 Window group_leader;
331 XClassHint *class_hints;
332 XWMHints* wm_hints;
333 Atom protocols[2];
334 int i;
336 wine_tsx11_lock();
338 /* wm protocols */
339 i = 0;
340 protocols[i++] = wmDeleteWindow;
341 if (wmTakeFocus) protocols[i++] = wmTakeFocus;
342 XSetWMProtocols( display, data->whole_window, protocols, i );
344 /* class hints */
345 if ((class_hints = XAllocClassHint()))
347 class_hints->res_name = "wine";
348 class_hints->res_class = "Wine";
349 XSetClassHint( display, data->whole_window, class_hints );
350 XFree( class_hints );
353 /* transient for hint */
354 if (win->owner)
356 Window owner_win = X11DRV_get_whole_window( win->owner );
357 XSetTransientForHint( display, data->whole_window, owner_win );
358 group_leader = owner_win;
360 else group_leader = data->whole_window;
362 /* size hints */
363 set_size_hints( display, win );
365 /* systray properties (KDE only for now) */
366 if (win->dwExStyle & WS_EX_TRAYWINDOW)
368 int val = 1;
369 if (kwmDockWindow != None)
370 XChangeProperty( display, data->whole_window, kwmDockWindow, kwmDockWindow,
371 32, PropModeReplace, (char*)&val, 1 );
372 if (_kde_net_wm_system_tray_window_for != None)
373 XChangeProperty( display, data->whole_window, _kde_net_wm_system_tray_window_for,
374 XA_WINDOW, 32, PropModeReplace, (char*)&data->whole_window, 1 );
377 if (mwmHints != None)
379 MwmHints mwm_hints;
380 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
381 mwm_hints.functions = 0;
382 if ((win->dwStyle & WS_CAPTION) == WS_CAPTION) mwm_hints.functions |= MWM_FUNC_MOVE;
383 if (win->dwStyle & WS_THICKFRAME) mwm_hints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
384 if (win->dwStyle & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
385 if (win->dwStyle & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
386 if (win->dwStyle & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
387 mwm_hints.decorations = 0;
388 if ((win->dwStyle & WS_CAPTION) == WS_CAPTION) mwm_hints.decorations |= MWM_DECOR_TITLE;
389 if (win->dwExStyle & WS_EX_DLGMODALFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
390 else if (win->dwStyle & WS_THICKFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
391 else if ((win->dwStyle & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
392 else if (win->dwStyle & WS_BORDER) mwm_hints.decorations |= MWM_DECOR_BORDER;
393 else if (!(win->dwStyle & (WS_CHILD|WS_POPUP))) mwm_hints.decorations |= MWM_DECOR_BORDER;
394 if (win->dwStyle & WS_SYSMENU) mwm_hints.decorations |= MWM_DECOR_MENU;
395 if (win->dwStyle & WS_MINIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MINIMIZE;
396 if (win->dwStyle & WS_MAXIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MAXIMIZE;
398 XChangeProperty( display, data->whole_window, mwmHints, mwmHints, 32,
399 PropModeReplace, (char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
402 wm_hints = XAllocWMHints();
403 wine_tsx11_unlock();
405 /* wm hints */
406 if (wm_hints)
408 wm_hints->flags = InputHint | StateHint | WindowGroupHint;
409 wm_hints->input = !(win->dwStyle & WS_DISABLED);
411 set_icon_hints( display, win, wm_hints );
413 wm_hints->initial_state = (win->dwStyle & WS_MINIMIZE) ? IconicState : NormalState;
414 wm_hints->window_group = group_leader;
416 wine_tsx11_lock();
417 XSetWMHints( display, data->whole_window, wm_hints );
418 XFree(wm_hints);
419 wine_tsx11_unlock();
424 /***********************************************************************
425 * X11DRV_set_iconic_state
427 * Set the X11 iconic state according to the window style.
429 void X11DRV_set_iconic_state( WND *win )
431 Display *display = thread_display();
432 struct x11drv_win_data *data = win->pDriverData;
433 XWMHints* wm_hints;
434 BOOL iconic = IsIconic( win->hwndSelf );
436 wine_tsx11_lock();
438 if (iconic) XUnmapWindow( display, data->client_window );
439 else if (is_client_window_mapped( win )) XMapWindow( display, data->client_window );
441 if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
442 wm_hints->flags |= StateHint | IconPositionHint;
443 wm_hints->initial_state = iconic ? IconicState : NormalState;
444 wm_hints->icon_x = win->rectWindow.left;
445 wm_hints->icon_y = win->rectWindow.top;
446 XSetWMHints( display, data->whole_window, wm_hints );
448 if (win->dwStyle & WS_VISIBLE)
450 if (iconic)
451 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
452 else
453 if (!IsRectEmpty( &win->rectWindow )) XMapWindow( display, data->whole_window );
456 XFree(wm_hints);
457 wine_tsx11_unlock();
461 /***********************************************************************
462 * X11DRV_window_to_X_rect
464 * Convert a rect from client to X window coordinates
466 void X11DRV_window_to_X_rect( WND *win, RECT *rect )
468 RECT rc;
470 if (!(win->dwExStyle & WS_EX_MANAGED)) return;
471 if (IsRectEmpty( rect )) return;
473 rc.top = rc.bottom = rc.left = rc.right = 0;
475 AdjustWindowRectEx( &rc, win->dwStyle & ~(WS_HSCROLL|WS_VSCROLL), FALSE, win->dwExStyle );
477 rect->left -= rc.left;
478 rect->right -= rc.right;
479 rect->top -= rc.top;
480 rect->bottom -= rc.bottom;
481 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
482 if (rect->left >= rect->right) rect->right = rect->left + 1;
486 /***********************************************************************
487 * X11DRV_X_to_window_rect
489 * Opposite of X11DRV_window_to_X_rect
491 void X11DRV_X_to_window_rect( WND *win, RECT *rect )
493 if (!(win->dwExStyle & WS_EX_MANAGED)) return;
494 if (IsRectEmpty( rect )) return;
496 AdjustWindowRectEx( rect, win->dwStyle & ~(WS_HSCROLL|WS_VSCROLL), FALSE, win->dwExStyle );
498 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
499 if (rect->left >= rect->right) rect->right = rect->left + 1;
503 /***********************************************************************
504 * X11DRV_sync_whole_window_position
506 * Synchronize the X whole window position with the Windows one
508 int X11DRV_sync_whole_window_position( Display *display, WND *win, int zorder )
510 XWindowChanges changes;
511 int mask;
512 struct x11drv_win_data *data = win->pDriverData;
513 RECT whole_rect = win->rectWindow;
515 X11DRV_window_to_X_rect( win, &whole_rect );
516 mask = get_window_changes( &changes, &data->whole_rect, &whole_rect );
518 if (zorder)
520 /* find window that this one must be after */
521 HWND prev = GetWindow( win->hwndSelf, GW_HWNDPREV );
522 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
523 prev = GetWindow( prev, GW_HWNDPREV );
524 if (!prev) /* top child */
526 changes.stack_mode = Above;
527 mask |= CWStackMode;
529 else
531 changes.stack_mode = Below;
532 changes.sibling = X11DRV_get_whole_window(prev);
533 mask |= CWStackMode | CWSibling;
537 data->whole_rect = whole_rect;
539 if (mask)
541 TRACE( "setting win %lx pos %d,%d,%dx%d after %lx changes=%x\n",
542 data->whole_window, whole_rect.left, whole_rect.top,
543 whole_rect.right - whole_rect.left, whole_rect.bottom - whole_rect.top,
544 changes.sibling, mask );
545 wine_tsx11_lock();
546 XSync( gdi_display, False ); /* flush graphics operations before moving the window */
547 if (is_window_top_level( win ))
549 if (mask & (CWWidth|CWHeight)) set_size_hints( display, win );
550 XReconfigureWMWindow( display, data->whole_window,
551 DefaultScreen(display), mask, &changes );
553 else XConfigureWindow( display, data->whole_window, mask, &changes );
554 wine_tsx11_unlock();
556 return mask;
560 /***********************************************************************
561 * X11DRV_sync_client_window_position
563 * Synchronize the X client window position with the Windows one
565 int X11DRV_sync_client_window_position( Display *display, WND *win )
567 XWindowChanges changes;
568 int mask;
569 struct x11drv_win_data *data = win->pDriverData;
570 RECT client_rect = win->rectClient;
572 OffsetRect( &client_rect, -data->whole_rect.left, -data->whole_rect.top );
574 if ((mask = get_window_changes( &changes, &data->client_rect, &client_rect )))
576 BOOL was_mapped = is_client_window_mapped( win );
578 TRACE( "setting win %lx pos %d,%d,%dx%d (was %d,%d,%dx%d) after %lx changes=%x\n",
579 data->client_window, client_rect.left, client_rect.top,
580 client_rect.right - client_rect.left, client_rect.bottom - client_rect.top,
581 data->client_rect.left, data->client_rect.top,
582 data->client_rect.right - data->client_rect.left,
583 data->client_rect.bottom - data->client_rect.top,
584 changes.sibling, mask );
585 data->client_rect = client_rect;
586 wine_tsx11_lock();
587 XSync( gdi_display, False ); /* flush graphics operations before moving the window */
588 if (was_mapped && !is_client_window_mapped( win ))
589 XUnmapWindow( display, data->client_window );
590 XConfigureWindow( display, data->client_window, mask, &changes );
591 if (!was_mapped && is_client_window_mapped( win ))
592 XMapWindow( display, data->client_window );
593 wine_tsx11_unlock();
595 return mask;
599 /***********************************************************************
600 * X11DRV_register_window
602 * Associate an X window to a HWND.
604 void X11DRV_register_window( Display *display, HWND hwnd, struct x11drv_win_data *data )
606 wine_tsx11_lock();
607 XSaveContext( display, data->whole_window, winContext, (char *)hwnd );
608 XSaveContext( display, data->client_window, winContext, (char *)hwnd );
609 wine_tsx11_unlock();
613 /**********************************************************************
614 * create_desktop
616 static void create_desktop( Display *display, WND *wndPtr, CREATESTRUCTA *cs )
618 X11DRV_WND_DATA *data = wndPtr->pDriverData;
620 wine_tsx11_lock();
621 winContext = XUniqueContext();
622 wmProtocols = XInternAtom( display, "WM_PROTOCOLS", False );
623 wmDeleteWindow = XInternAtom( display, "WM_DELETE_WINDOW", False );
624 if (use_take_focus) wmTakeFocus = XInternAtom( display, "WM_TAKE_FOCUS", False );
625 dndProtocol = XInternAtom( display, "DndProtocol" , False );
626 dndSelection = XInternAtom( display, "DndSelection" , False );
627 wmChangeState = XInternAtom( display, "WM_CHANGE_STATE", False );
628 mwmHints = XInternAtom( display, _XA_MWM_HINTS, False );
629 kwmDockWindow = XInternAtom( display, "KWM_DOCKWINDOW", False );
630 _kde_net_wm_system_tray_window_for = XInternAtom( display, "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR", False );
631 wine_tsx11_unlock();
633 whole_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_whole_window" ));
634 client_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_client_window" ));
635 icon_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_icon_window" ));
637 data->whole_window = data->client_window = root_window;
638 data->whole_rect = data->client_rect = wndPtr->rectWindow;
640 SetPropA( wndPtr->hwndSelf, whole_window_atom, (HANDLE)root_window );
641 SetPropA( wndPtr->hwndSelf, client_window_atom, (HANDLE)root_window );
642 SetPropA( wndPtr->hwndSelf, "__wine_x11_visual_id", (HANDLE)XVisualIDFromVisual(visual) );
644 SendMessageW( wndPtr->hwndSelf, WM_NCCREATE, 0, (LPARAM)cs );
645 if (root_window != DefaultRootWindow(display)) X11DRV_create_desktop_thread();
649 /**********************************************************************
650 * create_whole_window
652 * Create the whole X window for a given window
654 static Window create_whole_window( Display *display, WND *win )
656 struct x11drv_win_data *data = win->pDriverData;
657 int cx, cy, mask;
658 XSetWindowAttributes attr;
659 Window parent;
660 RECT rect;
661 BOOL is_top_level = is_window_top_level( win );
663 rect = win->rectWindow;
664 X11DRV_window_to_X_rect( win, &rect );
666 if (!(cx = rect.right - rect.left)) cx = 1;
667 if (!(cy = rect.bottom - rect.top)) cy = 1;
669 parent = X11DRV_get_client_window( win->parent );
671 wine_tsx11_lock();
673 mask = get_window_attributes( display, win, &attr );
675 /* set the attributes that don't change over the lifetime of the window */
676 attr.bit_gravity = ForgetGravity;
677 attr.win_gravity = NorthWestGravity;
678 attr.backing_store = NotUseful/*WhenMapped*/;
679 mask |= CWBitGravity | CWWinGravity | CWBackingStore;
681 data->whole_rect = rect;
682 data->whole_window = XCreateWindow( display, parent, rect.left, rect.top, cx, cy,
683 0, screen_depth, InputOutput, visual,
684 mask, &attr );
686 if (!data->whole_window)
688 wine_tsx11_unlock();
689 return 0;
692 /* non-maximized child must be at bottom of Z order */
693 if ((win->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
695 XWindowChanges changes;
696 changes.stack_mode = Below;
697 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
700 wine_tsx11_unlock();
702 if (is_top_level) X11DRV_set_wm_hints( display, win );
704 return data->whole_window;
708 /**********************************************************************
709 * create_client_window
711 * Create the client window for a given window
713 static Window create_client_window( Display *display, WND *win )
715 struct x11drv_win_data *data = win->pDriverData;
716 RECT rect = data->whole_rect;
717 XSetWindowAttributes attr;
719 OffsetRect( &rect, -data->whole_rect.left, -data->whole_rect.top );
720 data->client_rect = rect;
722 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
723 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
724 attr.bit_gravity = (win->clsStyle & (CS_VREDRAW | CS_HREDRAW)) ?
725 ForgetGravity : NorthWestGravity;
726 attr.backing_store = NotUseful/*WhenMapped*/;
728 wine_tsx11_lock();
729 data->client_window = XCreateWindow( display, data->whole_window, 0, 0,
730 max( rect.right - rect.left, 1 ),
731 max( rect.bottom - rect.top, 1 ),
732 0, screen_depth,
733 InputOutput, visual,
734 CWEventMask | CWBitGravity | CWBackingStore, &attr );
735 if (data->client_window && is_client_window_mapped( win ))
736 XMapWindow( display, data->client_window );
737 wine_tsx11_unlock();
738 return data->client_window;
742 /*****************************************************************
743 * SetWindowText (X11DRV.@)
745 BOOL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
747 Display *display = thread_display();
748 UINT count;
749 char *buffer;
750 char *utf8_buffer;
751 static UINT text_cp = (UINT)-1;
752 Window win;
754 if ((win = X11DRV_get_whole_window( hwnd )))
756 if (text_cp == (UINT)-1)
758 HKEY hkey;
759 /* default value */
760 text_cp = CP_ACP;
761 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\x11drv", &hkey))
763 char buffer[20];
764 DWORD type, count = sizeof(buffer);
765 if(!RegQueryValueExA(hkey, "TextCP", 0, &type, buffer, &count))
766 text_cp = atoi(buffer);
767 RegCloseKey(hkey);
769 TRACE("text_cp = %u\n", text_cp);
772 /* allocate new buffer for window text */
773 count = WideCharToMultiByte(text_cp, 0, text, -1, NULL, 0, NULL, NULL);
774 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count )))
776 ERR("Not enough memory for window text\n");
777 return FALSE;
779 WideCharToMultiByte(text_cp, 0, text, -1, buffer, count, NULL, NULL);
781 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
782 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
784 ERR("Not enough memory for window text in UTF-8\n");
785 return FALSE;
787 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
789 wine_tsx11_lock();
790 XStoreName( display, win, buffer );
791 XSetIconName( display, win, buffer );
793 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
794 according to the standard
795 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
797 XChangeProperty( display, win,
798 XInternAtom(display, "_NET_WM_NAME", False),
799 XInternAtom(display, "UTF8_STRING", False),
800 8, PropModeReplace, (unsigned char *) utf8_buffer,
801 count);
802 wine_tsx11_unlock();
804 HeapFree( GetProcessHeap(), 0, utf8_buffer );
805 HeapFree( GetProcessHeap(), 0, buffer );
807 return TRUE;
811 /***********************************************************************
812 * DestroyWindow (X11DRV.@)
814 BOOL X11DRV_DestroyWindow( HWND hwnd )
816 struct x11drv_thread_data *thread_data = x11drv_thread_data();
817 Display *display = thread_data->display;
818 WND *wndPtr = WIN_GetPtr( hwnd );
819 X11DRV_WND_DATA *data = wndPtr->pDriverData;
821 if (!data) goto done;
823 if (data->whole_window)
825 TRACE( "win %x xwin %lx/%lx\n", hwnd, data->whole_window, data->client_window );
826 if (thread_data->cursor_window == data->whole_window) thread_data->cursor_window = None;
827 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
828 wine_tsx11_lock();
829 XSync( gdi_display, False ); /* flush any reference to this drawable in GDI queue */
830 XDeleteContext( display, data->whole_window, winContext );
831 XDeleteContext( display, data->client_window, winContext );
832 XDestroyWindow( display, data->whole_window ); /* this destroys client too */
833 destroy_icon_window( display, wndPtr );
834 wine_tsx11_unlock();
837 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
838 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
839 HeapFree( GetProcessHeap(), 0, data );
840 wndPtr->pDriverData = NULL;
841 done:
842 WIN_ReleasePtr( wndPtr );
843 return TRUE;
847 /**********************************************************************
848 * CreateWindow (X11DRV.@)
850 BOOL X11DRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
852 HWND hwndLinkAfter;
853 Display *display = thread_display();
854 WND *wndPtr;
855 struct x11drv_win_data *data;
856 RECT rect;
857 BOOL ret = FALSE;
859 if (cs->cx > 65535)
861 ERR( "invalid window width %d\n", cs->cx );
862 cs->cx = 65535;
864 if (cs->cy > 65535)
866 ERR( "invalid window height %d\n", cs->cx );
867 cs->cy = 65535;
870 if (!(data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data)))) return FALSE;
871 data->whole_window = 0;
872 data->client_window = 0;
873 data->icon_window = 0;
874 data->hWMIconBitmap = 0;
875 data->hWMIconMask = 0;
877 wndPtr = WIN_GetPtr( hwnd );
878 wndPtr->pDriverData = data;
880 /* initialize the dimensions before sending WM_GETMINMAXINFO */
881 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
882 WIN_SetRectangles( hwnd, &rect, &rect );
884 if (!wndPtr->parent)
886 create_desktop( display, wndPtr, cs );
887 WIN_ReleasePtr( wndPtr );
888 return TRUE;
891 if (!create_whole_window( display, wndPtr )) goto failed;
892 if (!create_client_window( display, wndPtr )) goto failed;
893 TSXSync( display, False );
895 SetPropA( hwnd, whole_window_atom, (HANDLE)data->whole_window );
896 SetPropA( hwnd, client_window_atom, (HANDLE)data->client_window );
898 /* Call the WH_CBT hook */
900 hwndLinkAfter = ((cs->style & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
901 ? HWND_BOTTOM : HWND_TOP;
903 if (HOOK_IsHooked( WH_CBT ))
905 CBT_CREATEWNDA cbtc;
906 LRESULT lret;
908 cbtc.lpcs = cs;
909 cbtc.hwndInsertAfter = hwndLinkAfter;
910 lret = (unicode) ? HOOK_CallHooksW(WH_CBT, HCBT_CREATEWND,
911 (WPARAM)hwnd, (LPARAM)&cbtc)
912 : HOOK_CallHooksA(WH_CBT, HCBT_CREATEWND,
913 (WPARAM)hwnd, (LPARAM)&cbtc);
914 if (lret)
916 TRACE("CBT-hook returned !0\n");
917 goto failed;
923 /* Send the WM_GETMINMAXINFO message and fix the size if needed */
924 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
926 POINT maxSize, maxPos, minTrack, maxTrack;
928 WIN_ReleasePtr( wndPtr );
929 WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
930 if (maxSize.x < cs->cx) cs->cx = maxSize.x;
931 if (maxSize.y < cs->cy) cs->cy = maxSize.y;
932 if (cs->cx < minTrack.x ) cs->cx = minTrack.x;
933 if (cs->cy < minTrack.y ) cs->cy = minTrack.y;
934 if (cs->cx < 0) cs->cx = 0;
935 if (cs->cy < 0) cs->cy = 0;
937 if (!(wndPtr = WIN_GetPtr( hwnd ))) return FALSE;
938 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
939 WIN_SetRectangles( hwnd, &rect, &rect );
940 X11DRV_sync_whole_window_position( display, wndPtr, 0 );
942 WIN_ReleasePtr( wndPtr );
944 /* send WM_NCCREATE */
945 TRACE( "hwnd %x cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cs->cx, cs->cy );
946 if (unicode)
947 ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
948 else
949 ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
950 if (!ret)
952 WARN("aborted by WM_xxCREATE!\n");
953 return FALSE;
956 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
958 X11DRV_sync_window_style( display, wndPtr );
960 /* send WM_NCCALCSIZE */
961 rect = wndPtr->rectWindow;
962 WIN_ReleasePtr( wndPtr );
963 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&rect );
965 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
966 if (rect.left > rect.right || rect.top > rect.bottom) rect = wndPtr->rectWindow;
967 WIN_SetRectangles( hwnd, &wndPtr->rectWindow, &rect );
968 X11DRV_sync_client_window_position( display, wndPtr );
969 X11DRV_register_window( display, hwnd, data );
971 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",
972 hwnd, wndPtr->rectWindow.left, wndPtr->rectWindow.top,
973 wndPtr->rectWindow.right, wndPtr->rectWindow.bottom,
974 wndPtr->rectClient.left, wndPtr->rectClient.top,
975 wndPtr->rectClient.right, wndPtr->rectClient.bottom,
976 data->whole_rect.left, data->whole_rect.top,
977 data->whole_rect.right, data->whole_rect.bottom,
978 data->client_rect.left, data->client_rect.top,
979 data->client_rect.right, data->client_rect.bottom,
980 (unsigned int)data->whole_window, (unsigned int)data->client_window );
982 if ((wndPtr->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
983 WIN_LinkWindow( hwnd, wndPtr->parent, HWND_BOTTOM );
984 else
985 WIN_LinkWindow( hwnd, wndPtr->parent, HWND_TOP );
987 WIN_ReleasePtr( wndPtr );
989 if (unicode)
990 ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
991 else
992 ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
994 if (!ret)
996 WIN_UnlinkWindow( hwnd );
997 return FALSE;
1000 /* Send the size messages */
1002 if (!(wndPtr = WIN_FindWndPtr(hwnd))) return FALSE;
1003 if (!(wndPtr->flags & WIN_NEED_SIZE))
1005 /* send it anyway */
1006 if (((wndPtr->rectClient.right-wndPtr->rectClient.left) <0)
1007 ||((wndPtr->rectClient.bottom-wndPtr->rectClient.top)<0))
1008 WARN("sending bogus WM_SIZE message 0x%08lx\n",
1009 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
1010 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
1011 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1012 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
1013 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
1014 SendMessageW( hwnd, WM_MOVE, 0,
1015 MAKELONG( wndPtr->rectClient.left, wndPtr->rectClient.top ) );
1018 /* Show the window, maximizing or minimizing if needed */
1020 if (wndPtr->dwStyle & (WS_MINIMIZE | WS_MAXIMIZE))
1022 extern UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect ); /*FIXME*/
1024 RECT newPos;
1025 UINT swFlag = (wndPtr->dwStyle & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1026 WIN_SetStyle( hwnd, wndPtr->dwStyle & ~(WS_MAXIMIZE | WS_MINIMIZE) );
1027 WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1028 swFlag = ((wndPtr->dwStyle & WS_CHILD) || GetActiveWindow())
1029 ? SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED
1030 : SWP_NOZORDER | SWP_FRAMECHANGED;
1031 SetWindowPos( hwnd, 0, newPos.left, newPos.top,
1032 newPos.right, newPos.bottom, swFlag );
1035 /* if the window was made visible set create struct flag so that
1036 * we do a proper ShowWindow later on */
1037 if (wndPtr->dwStyle & WS_VISIBLE) cs->style |= WS_VISIBLE;
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 )) X11DRV_set_wm_hints( display, wndPtr );
1129 wine_tsx11_lock();
1130 X11DRV_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 = (HICON)SetClassLongW(hwnd, small ? GCL_HICONSM : GCL_HICON, (LONG)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;
1221 wine_tsx11_lock();
1222 if (!(wm_hints = XGetWMHints( display, win ))) wm_hints = XAllocWMHints();
1223 wine_tsx11_unlock();
1224 if (wm_hints)
1226 set_icon_hints( display, wndPtr, wm_hints );
1227 wine_tsx11_lock();
1228 XSetWMHints( display, win, wm_hints );
1229 XFree( wm_hints );
1230 wine_tsx11_unlock();
1233 WIN_ReleasePtr( wndPtr );
1234 return old;