Restore the texture env mode on SetTexture - Fixes problem in
[wine/wine-gecko.git] / dlls / x11drv / window.c
blobf80f909509256cbe3a0ac8bdc07f858da418100d
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 "mwm.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
45 extern Pixmap X11DRV_BITMAP_Pixmap( HBITMAP );
47 #define HAS_DLGFRAME(style,exStyle) \
48 (((exStyle) & WS_EX_DLGMODALFRAME) || \
49 (((style) & WS_DLGFRAME) && !((style) & WS_THICKFRAME)))
51 /* X context to associate a hwnd to an X window */
52 XContext winContext = 0;
54 Atom wmProtocols = None;
55 Atom wmDeleteWindow = None;
56 Atom wmTakeFocus = None;
57 Atom dndProtocol = None;
58 Atom dndSelection = None;
59 Atom wmChangeState = None;
60 Atom mwmHints = None;
61 Atom kwmDockWindow = None;
62 Atom _kde_net_wm_system_tray_window_for = None; /* KDE 2 Final */
64 static LPCSTR whole_window_atom;
65 static LPCSTR client_window_atom;
66 static LPCSTR icon_window_atom;
68 /***********************************************************************
69 * is_window_managed
71 * Check if a given window should be managed
73 inline static BOOL is_window_managed( WND *win )
75 if (!managed_mode) return FALSE;
76 /* tray window is always managed */
77 if (win->dwExStyle & WS_EX_TRAYWINDOW) return TRUE;
78 /* child windows are not managed */
79 if (win->dwStyle & WS_CHILD) return FALSE;
80 /* tool windows are not managed */
81 if (win->dwExStyle & WS_EX_TOOLWINDOW) return FALSE;
82 /* windows with caption or thick frame are managed */
83 if ((win->dwStyle & WS_CAPTION) == WS_CAPTION) return TRUE;
84 if (win->dwStyle & WS_THICKFRAME) return TRUE;
85 /* default: not managed */
86 return FALSE;
90 /***********************************************************************
91 * is_client_window_mapped
93 * Check if the X client window should be mapped
95 inline static BOOL is_client_window_mapped( WND *win )
97 struct x11drv_win_data *data = win->pDriverData;
98 return !(win->dwStyle & WS_MINIMIZE) && !IsRectEmpty( &data->client_rect );
102 /***********************************************************************
103 * get_window_attributes
105 * Fill the window attributes structure for an X window.
107 static int get_window_attributes( Display *display, WND *win, XSetWindowAttributes *attr )
109 BOOL is_top_level = is_window_top_level( win );
110 BOOL managed = is_top_level && is_window_managed( win );
112 if (managed) WIN_SetExStyle( win->hwndSelf, win->dwExStyle | WS_EX_MANAGED );
113 else WIN_SetExStyle( win->hwndSelf, win->dwExStyle & ~WS_EX_MANAGED );
115 attr->override_redirect = !managed;
116 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
117 attr->save_under = ((win->clsStyle & CS_SAVEBITS) != 0);
118 attr->cursor = x11drv_thread_data()->cursor;
119 attr->event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
120 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
122 if (is_window_top_level( win ))
123 attr->event_mask |= StructureNotifyMask | FocusChangeMask | KeymapStateMask;
125 return (CWOverrideRedirect | CWSaveUnder | CWEventMask | CWColormap | CWCursor);
129 /***********************************************************************
130 * X11DRV_sync_window_style
132 * Change the X window attributes when the window style has changed.
134 void X11DRV_sync_window_style( Display *display, WND *win )
136 XSetWindowAttributes attr;
137 int mask;
139 wine_tsx11_lock();
140 mask = get_window_attributes( display, win, &attr );
141 XChangeWindowAttributes( display, get_whole_window(win), mask, &attr );
142 wine_tsx11_unlock();
146 /***********************************************************************
147 * get_window_changes
149 * fill the window changes structure
151 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
153 int mask = 0;
155 if (old->right - old->left != new->right - new->left )
157 if (!(changes->width = new->right - new->left)) changes->width = 1;
158 mask |= CWWidth;
160 if (old->bottom - old->top != new->bottom - new->top)
162 if (!(changes->height = new->bottom - new->top)) changes->height = 1;
163 mask |= CWHeight;
165 if (old->left != new->left)
167 changes->x = new->left;
168 mask |= CWX;
170 if (old->top != new->top)
172 changes->y = new->top;
173 mask |= CWY;
175 return mask;
179 /***********************************************************************
180 * create_icon_window
182 static Window create_icon_window( Display *display, WND *win )
184 struct x11drv_win_data *data = win->pDriverData;
185 XSetWindowAttributes attr;
187 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
188 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
189 attr.bit_gravity = NorthWestGravity;
190 attr.backing_store = NotUseful/*WhenMapped*/;
191 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
193 wine_tsx11_lock();
194 data->icon_window = XCreateWindow( display, root_window, 0, 0,
195 GetSystemMetrics( SM_CXICON ),
196 GetSystemMetrics( SM_CYICON ),
197 0, screen_depth,
198 InputOutput, visual,
199 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
200 XSaveContext( display, data->icon_window, winContext, (char *)win->hwndSelf );
201 wine_tsx11_unlock();
203 TRACE( "created %lx\n", data->icon_window );
204 SetPropA( win->hwndSelf, icon_window_atom, (HANDLE)data->icon_window );
205 return data->icon_window;
210 /***********************************************************************
211 * destroy_icon_window
213 inline static void destroy_icon_window( Display *display, WND *win )
215 struct x11drv_win_data *data = win->pDriverData;
217 if (!data->icon_window) return;
218 if (x11drv_thread_data()->cursor_window == data->icon_window)
219 x11drv_thread_data()->cursor_window = None;
220 wine_tsx11_lock();
221 XDeleteContext( display, data->icon_window, winContext );
222 XDestroyWindow( display, data->icon_window );
223 data->icon_window = 0;
224 wine_tsx11_unlock();
225 RemovePropA( win->hwndSelf, icon_window_atom );
229 /***********************************************************************
230 * set_icon_hints
232 * Set the icon wm hints
234 static void set_icon_hints( Display *display, WND *wndPtr, XWMHints *hints )
236 X11DRV_WND_DATA *data = wndPtr->pDriverData;
237 HICON hIcon = (HICON)GetClassLongA( wndPtr->hwndSelf, GCL_HICON );
239 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
240 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
241 data->hWMIconBitmap = 0;
242 data->hWMIconMask = 0;
244 if (!(wndPtr->dwExStyle & WS_EX_MANAGED))
246 destroy_icon_window( display, wndPtr );
247 hints->flags &= ~(IconPixmapHint | IconMaskHint | IconWindowHint);
249 else if (!hIcon)
251 if (!data->icon_window) create_icon_window( display, wndPtr );
252 hints->icon_window = data->icon_window;
253 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
255 else
257 HBITMAP hbmOrig;
258 RECT rcMask;
259 BITMAP bmMask;
260 ICONINFO ii;
261 HDC hDC;
263 GetIconInfo(hIcon, &ii);
265 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
266 rcMask.top = 0;
267 rcMask.left = 0;
268 rcMask.right = bmMask.bmWidth;
269 rcMask.bottom = bmMask.bmHeight;
271 hDC = CreateCompatibleDC(0);
272 hbmOrig = SelectObject(hDC, ii.hbmMask);
273 InvertRect(hDC, &rcMask);
274 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
275 SelectObject(hDC, hbmOrig);
276 DeleteDC(hDC);
278 data->hWMIconBitmap = ii.hbmColor;
279 data->hWMIconMask = ii.hbmMask;
281 hints->icon_pixmap = X11DRV_BITMAP_Pixmap(data->hWMIconBitmap);
282 hints->icon_mask = X11DRV_BITMAP_Pixmap(data->hWMIconMask);
283 destroy_icon_window( display, wndPtr );
284 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
289 /***********************************************************************
290 * set_size_hints
292 * set the window size hints
294 static void set_size_hints( Display *display, WND *win )
296 XSizeHints* size_hints;
297 struct x11drv_win_data *data = win->pDriverData;
299 if ((size_hints = XAllocSizeHints()))
301 size_hints->win_gravity = StaticGravity;
302 size_hints->x = data->whole_rect.left;
303 size_hints->y = data->whole_rect.top;
304 size_hints->flags = PWinGravity | PPosition;
306 if (HAS_DLGFRAME( win->dwStyle, win->dwExStyle ))
308 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
309 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
310 size_hints->min_width = size_hints->max_width;
311 size_hints->min_height = size_hints->max_height;
312 size_hints->flags |= PMinSize | PMaxSize;
314 XSetWMNormalHints( display, data->whole_window, size_hints );
315 XFree( size_hints );
320 /***********************************************************************
321 * X11DRV_set_wm_hints
323 * Set the window manager hints for a newly-created window
325 void X11DRV_set_wm_hints( Display *display, WND *win )
327 struct x11drv_win_data *data = win->pDriverData;
328 Window group_leader;
329 XClassHint *class_hints;
330 XWMHints* wm_hints;
331 Atom protocols[2];
332 int i;
334 wine_tsx11_lock();
336 /* wm protocols */
337 i = 0;
338 protocols[i++] = wmDeleteWindow;
339 if (wmTakeFocus) protocols[i++] = wmTakeFocus;
340 XSetWMProtocols( display, data->whole_window, protocols, i );
342 /* class hints */
343 if ((class_hints = XAllocClassHint()))
345 class_hints->res_name = "wine";
346 class_hints->res_class = "Wine";
347 XSetClassHint( display, data->whole_window, class_hints );
348 XFree( class_hints );
351 /* transient for hint */
352 if (win->owner)
354 Window owner_win = X11DRV_get_whole_window( win->owner );
355 XSetTransientForHint( display, data->whole_window, owner_win );
356 group_leader = owner_win;
358 else group_leader = data->whole_window;
360 /* size hints */
361 set_size_hints( display, win );
363 /* systray properties (KDE only for now) */
364 if (win->dwExStyle & WS_EX_TRAYWINDOW)
366 int val = 1;
367 if (kwmDockWindow != None)
368 XChangeProperty( display, data->whole_window, kwmDockWindow, kwmDockWindow,
369 32, PropModeReplace, (char*)&val, 1 );
370 if (_kde_net_wm_system_tray_window_for != None)
371 XChangeProperty( display, data->whole_window, _kde_net_wm_system_tray_window_for,
372 XA_WINDOW, 32, PropModeReplace, (char*)&data->whole_window, 1 );
375 if (mwmHints != None)
377 MwmHints mwm_hints;
378 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
379 mwm_hints.functions = 0;
380 if ((win->dwStyle & WS_CAPTION) == WS_CAPTION) mwm_hints.functions |= MWM_FUNC_MOVE;
381 if (win->dwStyle & WS_THICKFRAME) mwm_hints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
382 if (win->dwStyle & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
383 if (win->dwStyle & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
384 if (win->dwStyle & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
385 mwm_hints.decorations = 0;
386 if ((win->dwStyle & WS_CAPTION) == WS_CAPTION) mwm_hints.decorations |= MWM_DECOR_TITLE;
387 if (win->dwExStyle & WS_EX_DLGMODALFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
388 else if (win->dwStyle & WS_THICKFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
389 else if ((win->dwStyle & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
390 else if (win->dwStyle & WS_BORDER) mwm_hints.decorations |= MWM_DECOR_BORDER;
391 else if (!(win->dwStyle & (WS_CHILD|WS_POPUP))) mwm_hints.decorations |= MWM_DECOR_BORDER;
392 if (win->dwStyle & WS_SYSMENU) mwm_hints.decorations |= MWM_DECOR_MENU;
393 if (win->dwStyle & WS_MINIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MINIMIZE;
394 if (win->dwStyle & WS_MAXIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MAXIMIZE;
396 XChangeProperty( display, data->whole_window, mwmHints, mwmHints, 32,
397 PropModeReplace, (char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
400 wm_hints = XAllocWMHints();
401 wine_tsx11_unlock();
403 /* wm hints */
404 if (wm_hints)
406 wm_hints->flags = InputHint | StateHint | WindowGroupHint;
407 wm_hints->input = !(win->dwStyle & WS_DISABLED);
409 set_icon_hints( display, win, wm_hints );
411 wm_hints->initial_state = (win->dwStyle & WS_MINIMIZE) ? IconicState : NormalState;
412 wm_hints->window_group = group_leader;
414 wine_tsx11_lock();
415 XSetWMHints( display, data->whole_window, wm_hints );
416 XFree(wm_hints);
417 wine_tsx11_unlock();
422 /***********************************************************************
423 * X11DRV_set_iconic_state
425 * Set the X11 iconic state according to the window style.
427 void X11DRV_set_iconic_state( WND *win )
429 Display *display = thread_display();
430 struct x11drv_win_data *data = win->pDriverData;
431 XWMHints* wm_hints;
432 BOOL iconic = IsIconic( win->hwndSelf );
434 wine_tsx11_lock();
436 if (iconic) XUnmapWindow( display, data->client_window );
437 else if (is_client_window_mapped( win )) XMapWindow( display, data->client_window );
439 if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
440 wm_hints->flags |= StateHint | IconPositionHint;
441 wm_hints->initial_state = iconic ? IconicState : NormalState;
442 wm_hints->icon_x = win->rectWindow.left;
443 wm_hints->icon_y = win->rectWindow.top;
444 XSetWMHints( display, data->whole_window, wm_hints );
446 if (win->dwStyle & WS_VISIBLE)
448 if (iconic)
449 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
450 else
451 if (!IsRectEmpty( &win->rectWindow )) XMapWindow( display, data->whole_window );
454 XFree(wm_hints);
455 wine_tsx11_unlock();
459 /***********************************************************************
460 * X11DRV_window_to_X_rect
462 * Convert a rect from client to X window coordinates
464 void X11DRV_window_to_X_rect( WND *win, RECT *rect )
466 RECT rc;
468 if (!(win->dwExStyle & WS_EX_MANAGED)) return;
469 if (IsRectEmpty( rect )) return;
471 rc.top = rc.bottom = rc.left = rc.right = 0;
473 AdjustWindowRectEx( &rc, win->dwStyle & ~(WS_HSCROLL|WS_VSCROLL), FALSE, win->dwExStyle );
475 rect->left -= rc.left;
476 rect->right -= rc.right;
477 rect->top -= rc.top;
478 rect->bottom -= rc.bottom;
479 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
480 if (rect->left >= rect->right) rect->right = rect->left + 1;
484 /***********************************************************************
485 * X11DRV_X_to_window_rect
487 * Opposite of X11DRV_window_to_X_rect
489 void X11DRV_X_to_window_rect( WND *win, RECT *rect )
491 if (!(win->dwExStyle & WS_EX_MANAGED)) return;
492 if (IsRectEmpty( rect )) return;
494 AdjustWindowRectEx( rect, win->dwStyle & ~(WS_HSCROLL|WS_VSCROLL), FALSE, win->dwExStyle );
496 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
497 if (rect->left >= rect->right) rect->right = rect->left + 1;
501 /***********************************************************************
502 * X11DRV_sync_whole_window_position
504 * Synchronize the X whole window position with the Windows one
506 int X11DRV_sync_whole_window_position( Display *display, WND *win, int zorder )
508 XWindowChanges changes;
509 int mask;
510 struct x11drv_win_data *data = win->pDriverData;
511 RECT whole_rect = win->rectWindow;
513 X11DRV_window_to_X_rect( win, &whole_rect );
514 mask = get_window_changes( &changes, &data->whole_rect, &whole_rect );
516 if (zorder)
518 /* find window that this one must be after */
519 HWND prev = GetWindow( win->hwndSelf, GW_HWNDPREV );
520 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
521 prev = GetWindow( prev, GW_HWNDPREV );
522 if (!prev) /* top child */
524 changes.stack_mode = Above;
525 mask |= CWStackMode;
527 else
529 changes.stack_mode = Below;
530 changes.sibling = X11DRV_get_whole_window(prev);
531 mask |= CWStackMode | CWSibling;
535 data->whole_rect = whole_rect;
537 if (mask)
539 TRACE( "setting win %lx pos %ld,%ld,%ldx%ld after %lx changes=%x\n",
540 data->whole_window, whole_rect.left, whole_rect.top,
541 whole_rect.right - whole_rect.left, whole_rect.bottom - whole_rect.top,
542 changes.sibling, mask );
543 wine_tsx11_lock();
544 XSync( gdi_display, False ); /* flush graphics operations before moving the window */
545 if (is_window_top_level( win ))
547 if (mask & (CWWidth|CWHeight)) set_size_hints( display, win );
548 XReconfigureWMWindow( display, data->whole_window,
549 DefaultScreen(display), mask, &changes );
551 else XConfigureWindow( display, data->whole_window, mask, &changes );
552 wine_tsx11_unlock();
554 return mask;
558 /***********************************************************************
559 * X11DRV_sync_client_window_position
561 * Synchronize the X client window position with the Windows one
563 int X11DRV_sync_client_window_position( Display *display, WND *win )
565 XWindowChanges changes;
566 int mask;
567 struct x11drv_win_data *data = win->pDriverData;
568 RECT client_rect = win->rectClient;
570 OffsetRect( &client_rect, -data->whole_rect.left, -data->whole_rect.top );
572 if ((mask = get_window_changes( &changes, &data->client_rect, &client_rect )))
574 BOOL was_mapped = is_client_window_mapped( win );
576 TRACE( "setting win %lx pos %ld,%ld,%ldx%ld (was %ld,%ld,%ldx%ld) after %lx changes=%x\n",
577 data->client_window, client_rect.left, client_rect.top,
578 client_rect.right - client_rect.left, client_rect.bottom - client_rect.top,
579 data->client_rect.left, data->client_rect.top,
580 data->client_rect.right - data->client_rect.left,
581 data->client_rect.bottom - data->client_rect.top,
582 changes.sibling, mask );
583 data->client_rect = client_rect;
584 wine_tsx11_lock();
585 XSync( gdi_display, False ); /* flush graphics operations before moving the window */
586 if (was_mapped && !is_client_window_mapped( win ))
587 XUnmapWindow( display, data->client_window );
588 XConfigureWindow( display, data->client_window, mask, &changes );
589 if (!was_mapped && is_client_window_mapped( win ))
590 XMapWindow( display, data->client_window );
591 wine_tsx11_unlock();
593 return mask;
597 /***********************************************************************
598 * X11DRV_register_window
600 * Associate an X window to a HWND.
602 void X11DRV_register_window( Display *display, HWND hwnd, struct x11drv_win_data *data )
604 wine_tsx11_lock();
605 XSaveContext( display, data->whole_window, winContext, (char *)hwnd );
606 XSaveContext( display, data->client_window, winContext, (char *)hwnd );
607 wine_tsx11_unlock();
611 /**********************************************************************
612 * create_desktop
614 static void create_desktop( Display *display, WND *wndPtr, CREATESTRUCTA *cs )
616 X11DRV_WND_DATA *data = wndPtr->pDriverData;
618 wine_tsx11_lock();
619 winContext = XUniqueContext();
620 wmProtocols = XInternAtom( display, "WM_PROTOCOLS", False );
621 wmDeleteWindow = XInternAtom( display, "WM_DELETE_WINDOW", False );
622 if (use_take_focus) wmTakeFocus = XInternAtom( display, "WM_TAKE_FOCUS", False );
623 dndProtocol = XInternAtom( display, "DndProtocol" , False );
624 dndSelection = XInternAtom( display, "DndSelection" , False );
625 wmChangeState = XInternAtom( display, "WM_CHANGE_STATE", False );
626 mwmHints = XInternAtom( display, _XA_MWM_HINTS, False );
627 kwmDockWindow = XInternAtom( display, "KWM_DOCKWINDOW", False );
628 _kde_net_wm_system_tray_window_for = XInternAtom( display, "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR", False );
629 wine_tsx11_unlock();
631 whole_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_whole_window" ));
632 client_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_client_window" ));
633 icon_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_icon_window" ));
635 data->whole_window = data->client_window = root_window;
636 data->whole_rect = data->client_rect = wndPtr->rectWindow;
638 SetPropA( wndPtr->hwndSelf, whole_window_atom, (HANDLE)root_window );
639 SetPropA( wndPtr->hwndSelf, client_window_atom, (HANDLE)root_window );
640 SetPropA( wndPtr->hwndSelf, "__wine_x11_visual_id", (HANDLE)XVisualIDFromVisual(visual) );
642 SendMessageW( wndPtr->hwndSelf, WM_NCCREATE, 0, (LPARAM)cs );
643 if (root_window != DefaultRootWindow(display)) X11DRV_create_desktop_thread();
647 /**********************************************************************
648 * create_whole_window
650 * Create the whole X window for a given window
652 static Window create_whole_window( Display *display, WND *win )
654 struct x11drv_win_data *data = win->pDriverData;
655 int cx, cy, mask;
656 XSetWindowAttributes attr;
657 Window parent;
658 RECT rect;
659 BOOL is_top_level = is_window_top_level( win );
661 rect = win->rectWindow;
662 X11DRV_window_to_X_rect( win, &rect );
664 if (!(cx = rect.right - rect.left)) cx = 1;
665 if (!(cy = rect.bottom - rect.top)) cy = 1;
667 parent = X11DRV_get_client_window( win->parent );
669 wine_tsx11_lock();
671 mask = get_window_attributes( display, win, &attr );
673 /* set the attributes that don't change over the lifetime of the window */
674 attr.bit_gravity = ForgetGravity;
675 attr.win_gravity = NorthWestGravity;
676 attr.backing_store = NotUseful/*WhenMapped*/;
677 mask |= CWBitGravity | CWWinGravity | CWBackingStore;
679 data->whole_rect = rect;
680 data->whole_window = XCreateWindow( display, parent, rect.left, rect.top, cx, cy,
681 0, screen_depth, InputOutput, visual,
682 mask, &attr );
684 if (!data->whole_window)
686 wine_tsx11_unlock();
687 return 0;
690 /* non-maximized child must be at bottom of Z order */
691 if ((win->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
693 XWindowChanges changes;
694 changes.stack_mode = Below;
695 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
698 wine_tsx11_unlock();
700 if (is_top_level) X11DRV_set_wm_hints( display, win );
702 return data->whole_window;
706 /**********************************************************************
707 * create_client_window
709 * Create the client window for a given window
711 static Window create_client_window( Display *display, WND *win )
713 struct x11drv_win_data *data = win->pDriverData;
714 RECT rect = data->whole_rect;
715 XSetWindowAttributes attr;
717 OffsetRect( &rect, -data->whole_rect.left, -data->whole_rect.top );
718 data->client_rect = rect;
720 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
721 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
722 attr.bit_gravity = (win->clsStyle & (CS_VREDRAW | CS_HREDRAW)) ?
723 ForgetGravity : NorthWestGravity;
724 attr.backing_store = NotUseful/*WhenMapped*/;
726 wine_tsx11_lock();
727 data->client_window = XCreateWindow( display, data->whole_window, 0, 0,
728 max( rect.right - rect.left, 1 ),
729 max( rect.bottom - rect.top, 1 ),
730 0, screen_depth,
731 InputOutput, visual,
732 CWEventMask | CWBitGravity | CWBackingStore, &attr );
733 if (data->client_window && is_client_window_mapped( win ))
734 XMapWindow( display, data->client_window );
735 wine_tsx11_unlock();
736 return data->client_window;
740 /*****************************************************************
741 * SetWindowText (X11DRV.@)
743 BOOL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
745 Display *display = thread_display();
746 UINT count;
747 char *buffer;
748 char *utf8_buffer;
749 static UINT text_cp = (UINT)-1;
750 Window win;
752 if ((win = X11DRV_get_whole_window( hwnd )))
754 if (text_cp == (UINT)-1)
756 HKEY hkey;
757 /* default value */
758 text_cp = CP_ACP;
759 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\x11drv", &hkey))
761 char buffer[20];
762 DWORD type, count = sizeof(buffer);
763 if(!RegQueryValueExA(hkey, "TextCP", 0, &type, buffer, &count))
764 text_cp = atoi(buffer);
765 RegCloseKey(hkey);
767 TRACE("text_cp = %u\n", text_cp);
770 /* allocate new buffer for window text */
771 count = WideCharToMultiByte(text_cp, 0, text, -1, NULL, 0, NULL, NULL);
772 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count )))
774 ERR("Not enough memory for window text\n");
775 return FALSE;
777 WideCharToMultiByte(text_cp, 0, text, -1, buffer, count, NULL, NULL);
779 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
780 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
782 ERR("Not enough memory for window text in UTF-8\n");
783 return FALSE;
785 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
787 wine_tsx11_lock();
788 XStoreName( display, win, buffer );
789 XSetIconName( display, win, buffer );
791 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
792 according to the standard
793 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
795 XChangeProperty( display, win,
796 XInternAtom(display, "_NET_WM_NAME", False),
797 XInternAtom(display, "UTF8_STRING", False),
798 8, PropModeReplace, (unsigned char *) utf8_buffer,
799 count);
800 wine_tsx11_unlock();
802 HeapFree( GetProcessHeap(), 0, utf8_buffer );
803 HeapFree( GetProcessHeap(), 0, buffer );
805 return TRUE;
809 /***********************************************************************
810 * DestroyWindow (X11DRV.@)
812 BOOL X11DRV_DestroyWindow( HWND hwnd )
814 struct x11drv_thread_data *thread_data = x11drv_thread_data();
815 Display *display = thread_data->display;
816 WND *wndPtr = WIN_GetPtr( hwnd );
817 X11DRV_WND_DATA *data = wndPtr->pDriverData;
819 if (!data) goto done;
821 if (data->whole_window)
823 TRACE( "win %p xwin %lx/%lx\n", hwnd, data->whole_window, data->client_window );
824 if (thread_data->cursor_window == data->whole_window) thread_data->cursor_window = None;
825 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
826 wine_tsx11_lock();
827 XSync( gdi_display, False ); /* flush any reference to this drawable in GDI queue */
828 XDeleteContext( display, data->whole_window, winContext );
829 XDeleteContext( display, data->client_window, winContext );
830 XDestroyWindow( display, data->whole_window ); /* this destroys client too */
831 destroy_icon_window( display, wndPtr );
832 wine_tsx11_unlock();
835 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
836 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
837 HeapFree( GetProcessHeap(), 0, data );
838 wndPtr->pDriverData = NULL;
839 done:
840 WIN_ReleasePtr( wndPtr );
841 return TRUE;
845 /**********************************************************************
846 * CreateWindow (X11DRV.@)
848 BOOL X11DRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
850 HWND hwndLinkAfter;
851 Display *display = thread_display();
852 WND *wndPtr;
853 struct x11drv_win_data *data;
854 RECT rect;
855 CBT_CREATEWNDA cbtc;
856 BOOL ret = FALSE;
858 if (cs->cx > 65535)
860 ERR( "invalid window width %d\n", cs->cx );
861 cs->cx = 65535;
863 if (cs->cy > 65535)
865 ERR( "invalid window height %d\n", cs->cx );
866 cs->cy = 65535;
869 if (!(data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data)))) return FALSE;
870 data->whole_window = 0;
871 data->client_window = 0;
872 data->icon_window = 0;
873 data->hWMIconBitmap = 0;
874 data->hWMIconMask = 0;
876 wndPtr = WIN_GetPtr( hwnd );
877 wndPtr->pDriverData = data;
879 /* initialize the dimensions before sending WM_GETMINMAXINFO */
880 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
881 WIN_SetRectangles( hwnd, &rect, &rect );
883 if (!wndPtr->parent)
885 create_desktop( display, wndPtr, cs );
886 WIN_ReleasePtr( wndPtr );
887 return TRUE;
890 if (!create_whole_window( display, wndPtr )) goto failed;
891 if (!create_client_window( display, wndPtr )) goto failed;
892 TSXSync( display, False );
894 SetPropA( hwnd, whole_window_atom, (HANDLE)data->whole_window );
895 SetPropA( hwnd, client_window_atom, (HANDLE)data->client_window );
897 /* Call the WH_CBT hook */
899 hwndLinkAfter = ((cs->style & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
901 cbtc.lpcs = cs;
902 cbtc.hwndInsertAfter = hwndLinkAfter;
903 if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode ))
905 TRACE("CBT-hook returned !0\n");
906 goto failed;
909 /* Send the WM_GETMINMAXINFO message and fix the size if needed */
910 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
912 POINT maxSize, maxPos, minTrack, maxTrack;
914 WIN_ReleasePtr( wndPtr );
915 WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
916 if (maxSize.x < cs->cx) cs->cx = maxSize.x;
917 if (maxSize.y < cs->cy) cs->cy = maxSize.y;
918 if (cs->cx < minTrack.x ) cs->cx = minTrack.x;
919 if (cs->cy < minTrack.y ) cs->cy = minTrack.y;
920 if (cs->cx < 0) cs->cx = 0;
921 if (cs->cy < 0) cs->cy = 0;
923 if (!(wndPtr = WIN_GetPtr( hwnd ))) return FALSE;
924 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
925 WIN_SetRectangles( hwnd, &rect, &rect );
926 X11DRV_sync_whole_window_position( display, wndPtr, 0 );
928 WIN_ReleasePtr( wndPtr );
930 /* send WM_NCCREATE */
931 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cs->cx, cs->cy );
932 if (unicode)
933 ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
934 else
935 ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
936 if (!ret)
938 WARN("aborted by WM_xxCREATE!\n");
939 return FALSE;
942 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
944 X11DRV_sync_window_style( display, wndPtr );
946 /* send WM_NCCALCSIZE */
947 rect = wndPtr->rectWindow;
948 WIN_ReleasePtr( wndPtr );
949 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&rect );
951 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
952 if (rect.left > rect.right || rect.top > rect.bottom) rect = wndPtr->rectWindow;
953 WIN_SetRectangles( hwnd, &wndPtr->rectWindow, &rect );
954 X11DRV_sync_client_window_position( display, wndPtr );
955 X11DRV_register_window( display, hwnd, data );
957 TRACE( "win %p window %ld,%ld,%ld,%ld client %ld,%ld,%ld,%ld whole %ld,%ld,%ld,%ld X client %ld,%ld,%ld,%ld xwin %x/%x\n",
958 hwnd, wndPtr->rectWindow.left, wndPtr->rectWindow.top,
959 wndPtr->rectWindow.right, wndPtr->rectWindow.bottom,
960 wndPtr->rectClient.left, wndPtr->rectClient.top,
961 wndPtr->rectClient.right, wndPtr->rectClient.bottom,
962 data->whole_rect.left, data->whole_rect.top,
963 data->whole_rect.right, data->whole_rect.bottom,
964 data->client_rect.left, data->client_rect.top,
965 data->client_rect.right, data->client_rect.bottom,
966 (unsigned int)data->whole_window, (unsigned int)data->client_window );
968 if ((wndPtr->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
969 WIN_LinkWindow( hwnd, wndPtr->parent, HWND_BOTTOM );
970 else
971 WIN_LinkWindow( hwnd, wndPtr->parent, HWND_TOP );
973 WIN_ReleasePtr( wndPtr );
975 if (unicode)
976 ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
977 else
978 ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
980 if (!ret)
982 WIN_UnlinkWindow( hwnd );
983 return FALSE;
986 /* Send the size messages */
988 if (!(wndPtr = WIN_FindWndPtr(hwnd))) return FALSE;
989 if (!(wndPtr->flags & WIN_NEED_SIZE))
991 /* send it anyway */
992 if (((wndPtr->rectClient.right-wndPtr->rectClient.left) <0)
993 ||((wndPtr->rectClient.bottom-wndPtr->rectClient.top)<0))
994 WARN("sending bogus WM_SIZE message 0x%08lx\n",
995 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
996 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
997 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
998 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
999 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
1000 SendMessageW( hwnd, WM_MOVE, 0,
1001 MAKELONG( wndPtr->rectClient.left, wndPtr->rectClient.top ) );
1004 /* Show the window, maximizing or minimizing if needed */
1006 if (wndPtr->dwStyle & (WS_MINIMIZE | WS_MAXIMIZE))
1008 extern UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect ); /*FIXME*/
1010 RECT newPos;
1011 UINT swFlag = (wndPtr->dwStyle & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1012 WIN_SetStyle( hwnd, wndPtr->dwStyle & ~(WS_MAXIMIZE | WS_MINIMIZE) );
1013 WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1014 swFlag = ((wndPtr->dwStyle & WS_CHILD) || GetActiveWindow())
1015 ? SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED
1016 : SWP_NOZORDER | SWP_FRAMECHANGED;
1017 SetWindowPos( hwnd, 0, newPos.left, newPos.top,
1018 newPos.right, newPos.bottom, swFlag );
1021 /* if the window was made visible set create struct flag so that
1022 * we do a proper ShowWindow later on */
1023 if (wndPtr->dwStyle & WS_VISIBLE) cs->style |= WS_VISIBLE;
1025 WIN_ReleaseWndPtr( wndPtr );
1026 return TRUE;
1029 failed:
1030 X11DRV_DestroyWindow( hwnd );
1031 if (wndPtr) WIN_ReleasePtr( wndPtr );
1032 return FALSE;
1036 /***********************************************************************
1037 * X11DRV_get_client_window
1039 * Return the X window associated with the client area of a window
1041 Window X11DRV_get_client_window( HWND hwnd )
1043 Window ret = 0;
1044 WND *win = WIN_GetPtr( hwnd );
1046 if (win == WND_OTHER_PROCESS)
1047 return (Window)GetPropA( hwnd, client_window_atom );
1049 if (win)
1051 struct x11drv_win_data *data = win->pDriverData;
1052 ret = data->client_window;
1053 WIN_ReleasePtr( win );
1055 return ret;
1059 /***********************************************************************
1060 * X11DRV_get_whole_window
1062 * Return the X window associated with the full area of a window
1064 Window X11DRV_get_whole_window( HWND hwnd )
1066 Window ret = 0;
1067 WND *win = WIN_GetPtr( hwnd );
1069 if (win == WND_OTHER_PROCESS)
1070 return (Window)GetPropA( hwnd, whole_window_atom );
1072 if (win)
1074 struct x11drv_win_data *data = win->pDriverData;
1075 ret = data->whole_window;
1076 WIN_ReleasePtr( win );
1078 return ret;
1082 /*****************************************************************
1083 * SetParent (X11DRV.@)
1085 HWND X11DRV_SetParent( HWND hwnd, HWND parent )
1087 Display *display = thread_display();
1088 WND *wndPtr;
1089 HWND retvalue;
1091 /* Windows hides the window first, then shows it again
1092 * including the WM_SHOWWINDOW messages and all */
1093 BOOL was_visible = ShowWindow( hwnd, SW_HIDE );
1095 if (!IsWindow( parent )) return 0;
1096 if (!(wndPtr = WIN_GetPtr(hwnd)) || wndPtr == WND_OTHER_PROCESS) return 0;
1098 retvalue = wndPtr->parent; /* old parent */
1099 if (parent != retvalue)
1101 struct x11drv_win_data *data = wndPtr->pDriverData;
1103 WIN_LinkWindow( hwnd, parent, HWND_TOP );
1105 if (parent != GetDesktopWindow()) /* a child window */
1107 if (!(wndPtr->dwStyle & WS_CHILD))
1109 HMENU menu = (HMENU)SetWindowLongW( hwnd, GWL_ID, 0 );
1110 if (menu) DestroyMenu( menu );
1114 if (is_window_top_level( wndPtr )) X11DRV_set_wm_hints( display, wndPtr );
1115 wine_tsx11_lock();
1116 X11DRV_sync_window_style( display, wndPtr );
1117 XReparentWindow( display, data->whole_window, X11DRV_get_client_window(parent),
1118 data->whole_rect.left, data->whole_rect.top );
1119 wine_tsx11_unlock();
1121 WIN_ReleasePtr( wndPtr );
1123 /* SetParent additionally needs to make hwnd the topmost window
1124 in the x-order and send the expected WM_WINDOWPOSCHANGING and
1125 WM_WINDOWPOSCHANGED notification messages.
1127 SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0,
1128 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | (was_visible ? SWP_SHOWWINDOW : 0) );
1129 /* FIXME: a WM_MOVE is also generated (in the DefWindowProc handler
1130 * for WM_WINDOWPOSCHANGED) in Windows, should probably remove SWP_NOMOVE */
1132 return retvalue;
1136 /*****************************************************************
1137 * SetFocus (X11DRV.@)
1139 * Set the X focus.
1140 * Explicit colormap management seems to work only with OLVWM.
1142 void X11DRV_SetFocus( HWND hwnd )
1144 Display *display = thread_display();
1145 XWindowAttributes win_attr;
1146 Window win;
1148 /* Only mess with the X focus if there's */
1149 /* no desktop window and if the window is not managed by the WM. */
1150 if (root_window != DefaultRootWindow(display)) return;
1152 if (!hwnd) /* If setting the focus to 0, uninstall the colormap */
1154 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1155 TSXUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1156 return;
1159 hwnd = GetAncestor( hwnd, GA_ROOT );
1160 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_MANAGED) return;
1161 if (!(win = X11DRV_get_whole_window( hwnd ))) return;
1163 /* Set X focus and install colormap */
1164 wine_tsx11_lock();
1165 if (XGetWindowAttributes( display, win, &win_attr ) &&
1166 (win_attr.map_state == IsViewable))
1168 /* If window is not viewable, don't change anything */
1170 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1171 /* FIXME: this is not entirely correct */
1172 XSetInputFocus( display, win, RevertToParent,
1173 /*CurrentTime*/ GetMessageTime() + X11DRV_server_startticks );
1174 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1175 XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1177 wine_tsx11_unlock();
1181 /**********************************************************************
1182 * SetWindowIcon (X11DRV.@)
1184 * hIcon or hIconSm has changed (or is being initialised for the
1185 * first time). Complete the X11 driver-specific initialisation
1186 * and set the window hints.
1188 * This is not entirely correct, may need to create
1189 * an icon window and set the pixmap as a background
1191 HICON X11DRV_SetWindowIcon( HWND hwnd, HICON icon, BOOL small )
1193 WND *wndPtr;
1194 Display *display = thread_display();
1195 HICON old = (HICON)SetClassLongW(hwnd, small ? GCL_HICONSM : GCL_HICON, (LONG)icon );
1197 SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOSIZE |
1198 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
1200 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return old;
1202 if (wndPtr->dwExStyle & WS_EX_MANAGED)
1204 Window win = get_whole_window(wndPtr);
1205 XWMHints* wm_hints;
1207 wine_tsx11_lock();
1208 if (!(wm_hints = XGetWMHints( display, win ))) wm_hints = XAllocWMHints();
1209 wine_tsx11_unlock();
1210 if (wm_hints)
1212 set_icon_hints( display, wndPtr, wm_hints );
1213 wine_tsx11_lock();
1214 XSetWMHints( display, win, wm_hints );
1215 XFree( wm_hints );
1216 wine_tsx11_unlock();
1219 WIN_ReleasePtr( wndPtr );
1220 return old;