Added X11DRV_get_pixmap and X11DRV_set_pixmap functions to avoid
[wine/dcerpc.git] / dlls / x11drv / window.c
blobfc4311242c6852729aef4e160e602106c106b190
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 Atom X11DRV_Atoms[NB_XATOMS - FIRST_XATOM];
59 static const char * const atom_names[NB_XATOMS - FIRST_XATOM] =
61 "CLIPBOARD",
62 "COMPOUND_TEXT",
63 "MULTIPLE",
64 "SELECTION_DATA",
65 "TARGETS",
66 "TEXT",
67 "UTF8_STRING",
68 "RAW_ASCENT",
69 "RAW_DESCENT",
70 "RAW_CAP_HEIGHT",
71 "WM_PROTOCOLS",
72 "WM_DELETE_WINDOW",
73 "WM_TAKE_FOCUS",
74 "KWM_DOCKWINDOW",
75 "DndProtocol",
76 "DndSelection",
77 "_MOTIF_WM_HINTS",
78 "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR",
79 "_NET_WM_MOVERESIZE",
80 "_NET_WM_PID",
81 "_NET_WM_PING",
82 "_NET_WM_NAME",
83 "_NET_WM_WINDOW_TYPE",
84 "_NET_WM_WINDOW_TYPE_UTILITY",
85 "XdndAware",
86 "XdndEnter",
87 "XdndPosition",
88 "XdndStatus",
89 "XdndLeave",
90 "XdndFinished",
91 "XdndDrop",
92 "XdndActionCopy",
93 "XdndActionMove",
94 "XdndActionLink",
95 "XdndActionAsk",
96 "XdndActionPrivate",
97 "XdndSelection",
98 "XdndTarget",
99 "XdndTypeList",
100 "WCF_DIB",
101 "image/gif",
102 "text/html",
103 "text/plain",
104 "text/rtf",
105 "text/richtext"
108 static LPCSTR whole_window_atom;
109 static LPCSTR icon_window_atom;
110 static LPCSTR client_offset_atom;
111 static LPCSTR managed_atom;
113 /***********************************************************************
114 * is_window_managed
116 * Check if a given window should be managed
118 inline static BOOL is_window_managed( HWND hwnd )
120 DWORD style, ex_style;
122 if (!managed_mode) return FALSE;
123 /* tray window is always managed */
124 ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
125 if (ex_style & WS_EX_TRAYWINDOW) return TRUE;
126 /* child windows are not managed */
127 style = GetWindowLongW( hwnd, GWL_STYLE );
128 if (style & WS_CHILD) return FALSE;
129 /* windows with caption are managed */
130 if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
131 /* tool windows are not managed */
132 if (ex_style & WS_EX_TOOLWINDOW) return FALSE;
133 /* windows with thick frame are managed */
134 if (style & WS_THICKFRAME) return TRUE;
135 /* application windows are managed */
136 if (ex_style & WS_EX_APPWINDOW) return TRUE;
137 /* full-screen popup windows are managed */
138 if (style & WS_POPUP)
140 RECT rect;
141 GetWindowRect( hwnd, &rect );
142 if ((rect.right - rect.left) == screen_width && (rect.bottom - rect.top) == screen_height)
143 return TRUE;
145 /* default: not managed */
146 return FALSE;
150 /***********************************************************************
151 * X11DRV_is_window_rect_mapped
153 * Check if the X whole window should be mapped based on its rectangle
155 BOOL X11DRV_is_window_rect_mapped( const RECT *rect )
157 /* don't map if rect is empty */
158 if (IsRectEmpty( rect )) return FALSE;
160 /* don't map if rect is off-screen */
161 if (rect->left >= (int)screen_width || rect->top >= (int)screen_height) return FALSE;
162 if (rect->right < 0 || rect->bottom < 0) return FALSE;
164 return TRUE;
168 /***********************************************************************
169 * get_window_attributes
171 * Fill the window attributes structure for an X window.
173 static int get_window_attributes( struct x11drv_win_data *data, XSetWindowAttributes *attr )
175 if (!data->managed && !using_wine_desktop && is_window_managed( data->hwnd ))
177 data->managed = TRUE;
178 SetPropA( data->hwnd, managed_atom, (HANDLE)1 );
180 attr->override_redirect = !data->managed;
181 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
182 attr->save_under = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
183 attr->cursor = x11drv_thread_data()->cursor;
184 attr->event_mask = (ExposureMask | PointerMotionMask |
185 ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
186 KeyPressMask | KeyReleaseMask | StructureNotifyMask |
187 FocusChangeMask | KeymapStateMask);
189 return (CWOverrideRedirect | CWSaveUnder | CWEventMask | CWColormap | CWCursor);
193 /***********************************************************************
194 * X11DRV_sync_window_style
196 * Change the X window attributes when the window style has changed.
198 void X11DRV_sync_window_style( Display *display, struct x11drv_win_data *data )
200 XSetWindowAttributes attr;
201 int mask = get_window_attributes( data, &attr );
203 wine_tsx11_lock();
204 XChangeWindowAttributes( display, data->whole_window, mask, &attr );
205 wine_tsx11_unlock();
209 /***********************************************************************
210 * get_window_changes
212 * fill the window changes structure
214 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
216 int mask = 0;
218 if (old->right - old->left != new->right - new->left )
220 if (!(changes->width = new->right - new->left)) changes->width = 1;
221 mask |= CWWidth;
223 if (old->bottom - old->top != new->bottom - new->top)
225 if (!(changes->height = new->bottom - new->top)) changes->height = 1;
226 mask |= CWHeight;
228 if (old->left != new->left)
230 changes->x = new->left;
231 mask |= CWX;
233 if (old->top != new->top)
235 changes->y = new->top;
236 mask |= CWY;
238 return mask;
242 /***********************************************************************
243 * create_icon_window
245 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
247 XSetWindowAttributes attr;
249 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
250 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
251 attr.bit_gravity = NorthWestGravity;
252 attr.backing_store = NotUseful/*WhenMapped*/;
253 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
255 wine_tsx11_lock();
256 data->icon_window = XCreateWindow( display, root_window, 0, 0,
257 GetSystemMetrics( SM_CXICON ),
258 GetSystemMetrics( SM_CYICON ),
259 0, screen_depth,
260 InputOutput, visual,
261 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
262 XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
263 wine_tsx11_unlock();
265 TRACE( "created %lx\n", data->icon_window );
266 SetPropA( data->hwnd, icon_window_atom, (HANDLE)data->icon_window );
267 return data->icon_window;
272 /***********************************************************************
273 * destroy_icon_window
275 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
277 if (!data->icon_window) return;
278 if (x11drv_thread_data()->cursor_window == data->icon_window)
279 x11drv_thread_data()->cursor_window = None;
280 wine_tsx11_lock();
281 XDeleteContext( display, data->icon_window, winContext );
282 XDestroyWindow( display, data->icon_window );
283 data->icon_window = 0;
284 wine_tsx11_unlock();
285 RemovePropA( data->hwnd, icon_window_atom );
289 /***********************************************************************
290 * set_icon_hints
292 * Set the icon wm hints
294 static void set_icon_hints( Display *display, struct x11drv_win_data *data,
295 XWMHints *hints, HICON hIcon )
297 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
298 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
299 data->hWMIconBitmap = 0;
300 data->hWMIconMask = 0;
302 if (!data->managed)
304 destroy_icon_window( display, data );
305 hints->flags &= ~(IconPixmapHint | IconMaskHint | IconWindowHint);
307 else if (!hIcon)
309 if (!data->icon_window) create_icon_window( display, data );
310 hints->icon_window = data->icon_window;
311 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
313 else
315 HBITMAP hbmOrig;
316 RECT rcMask;
317 BITMAP bmMask;
318 ICONINFO ii;
319 HDC hDC;
321 GetIconInfo(hIcon, &ii);
323 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
324 rcMask.top = 0;
325 rcMask.left = 0;
326 rcMask.right = bmMask.bmWidth;
327 rcMask.bottom = bmMask.bmHeight;
329 hDC = CreateCompatibleDC(0);
330 hbmOrig = SelectObject(hDC, ii.hbmMask);
331 InvertRect(hDC, &rcMask);
332 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
333 SelectObject(hDC, hbmOrig);
334 DeleteDC(hDC);
336 data->hWMIconBitmap = ii.hbmColor;
337 data->hWMIconMask = ii.hbmMask;
339 hints->icon_pixmap = X11DRV_get_pixmap(data->hWMIconBitmap);
340 hints->icon_mask = X11DRV_get_pixmap(data->hWMIconMask);
341 destroy_icon_window( display, data );
342 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
347 /***********************************************************************
348 * set_size_hints
350 * set the window size hints
352 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
354 XSizeHints* size_hints;
356 if ((size_hints = XAllocSizeHints()))
358 size_hints->win_gravity = StaticGravity;
359 size_hints->x = data->whole_rect.left;
360 size_hints->y = data->whole_rect.top;
361 size_hints->flags = PWinGravity | PPosition;
363 if ( !(style & WS_THICKFRAME) )
365 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
366 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
367 size_hints->min_width = size_hints->max_width;
368 size_hints->min_height = size_hints->max_height;
369 size_hints->flags |= PMinSize | PMaxSize;
371 XSetWMNormalHints( display, data->whole_window, size_hints );
372 XFree( size_hints );
377 /***********************************************************************
378 * X11DRV_set_wm_hints
380 * Set the window manager hints for a newly-created window
382 void X11DRV_set_wm_hints( Display *display, struct x11drv_win_data *data )
384 Window group_leader;
385 XClassHint *class_hints;
386 XWMHints* wm_hints;
387 Atom protocols[3];
388 MwmHints mwm_hints;
389 Atom dndVersion = 4;
390 int i;
391 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
392 DWORD ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
393 HWND owner = GetWindow( data->hwnd, GW_OWNER );
395 /* transient for hint */
396 if (owner)
398 Window owner_win = X11DRV_get_whole_window( owner );
399 wine_tsx11_lock();
400 XSetTransientForHint( display, data->whole_window, owner_win );
401 wine_tsx11_unlock();
402 group_leader = owner_win;
404 else group_leader = data->whole_window;
406 wine_tsx11_lock();
408 /* wm protocols */
409 i = 0;
410 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
411 protocols[i++] = x11drv_atom(_NET_WM_PING);
412 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
413 XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
414 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
416 /* class hints */
417 if ((class_hints = XAllocClassHint()))
419 class_hints->res_name = "wine";
420 class_hints->res_class = "Wine";
421 XSetClassHint( display, data->whole_window, class_hints );
422 XFree( class_hints );
425 /* size hints */
426 set_size_hints( display, data, style );
428 /* systray properties (KDE only for now) */
429 if (ex_style & WS_EX_TRAYWINDOW)
431 int val = 1;
432 XChangeProperty( display, data->whole_window, x11drv_atom(KWM_DOCKWINDOW),
433 x11drv_atom(KWM_DOCKWINDOW), 32, PropModeReplace, (unsigned char*)&val, 1 );
434 XChangeProperty( display, data->whole_window, x11drv_atom(_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR),
435 XA_WINDOW, 32, PropModeReplace, (unsigned char*)&data->whole_window, 1 );
438 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
439 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
440 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
441 i = getpid();
442 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
443 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
445 /* map WS_EX_TOOLWINDOW to _NET_WM_WINDOW_TYPE_UTILITY */
446 if (ex_style & WS_EX_TOOLWINDOW)
448 Atom a = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
449 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
450 XA_ATOM, 32, PropModeReplace, (unsigned char*)&a, 1);
453 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
454 mwm_hints.functions = 0;
455 if ((style & WS_CAPTION) == WS_CAPTION) mwm_hints.functions |= MWM_FUNC_MOVE;
456 if (style & WS_THICKFRAME) mwm_hints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
457 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
458 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
459 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
460 if (ex_style & WS_EX_APPWINDOW) mwm_hints.functions |= MWM_FUNC_MOVE;
461 mwm_hints.decorations = 0;
462 if ((style & WS_CAPTION) == WS_CAPTION)
464 mwm_hints.decorations |= MWM_DECOR_TITLE;
465 if (style & WS_SYSMENU) mwm_hints.decorations |= MWM_DECOR_MENU;
466 if (style & WS_MINIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MINIMIZE;
467 if (style & WS_MAXIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MAXIMIZE;
469 if (ex_style & WS_EX_DLGMODALFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
470 else if (style & WS_THICKFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
471 else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
472 else if (style & WS_BORDER) mwm_hints.decorations |= MWM_DECOR_BORDER;
473 else if (!(style & (WS_CHILD|WS_POPUP))) mwm_hints.decorations |= MWM_DECOR_BORDER;
475 XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
476 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
477 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
479 XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
480 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
482 wm_hints = XAllocWMHints();
483 wine_tsx11_unlock();
485 /* wm hints */
486 if (wm_hints)
488 wm_hints->flags = InputHint | StateHint | WindowGroupHint;
489 wm_hints->input = !(style & WS_DISABLED);
491 set_icon_hints( display, data, wm_hints,
492 (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON ) );
494 wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
495 wm_hints->window_group = group_leader;
497 wine_tsx11_lock();
498 XSetWMHints( display, data->whole_window, wm_hints );
499 XFree(wm_hints);
500 wine_tsx11_unlock();
505 /***********************************************************************
506 * X11DRV_set_iconic_state
508 * Set the X11 iconic state according to the window style.
510 void X11DRV_set_iconic_state( HWND hwnd )
512 Display *display = thread_display();
513 struct x11drv_win_data *data;
514 RECT rect;
515 XWMHints* wm_hints;
516 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
517 BOOL iconic = (style & WS_MINIMIZE) != 0;
519 if (!(data = X11DRV_get_win_data( hwnd ))) return;
520 if (!data->whole_window) return;
522 GetWindowRect( hwnd, &rect );
524 wine_tsx11_lock();
526 if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
527 wm_hints->flags |= StateHint | IconPositionHint;
528 wm_hints->initial_state = iconic ? IconicState : NormalState;
529 wm_hints->icon_x = rect.left;
530 wm_hints->icon_y = rect.top;
531 XSetWMHints( display, data->whole_window, wm_hints );
533 if (style & WS_VISIBLE)
535 if (iconic)
536 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
537 else
538 if (X11DRV_is_window_rect_mapped( &rect ))
539 XMapWindow( display, data->whole_window );
542 XFree(wm_hints);
543 wine_tsx11_unlock();
547 /***********************************************************************
548 * X11DRV_window_to_X_rect
550 * Convert a rect from client to X window coordinates
552 void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
554 RECT rc;
556 if (!data->managed) return;
557 if (IsRectEmpty( rect )) return;
559 rc.top = rc.bottom = rc.left = rc.right = 0;
561 AdjustWindowRectEx( &rc, GetWindowLongW( data->hwnd, GWL_STYLE ) & ~(WS_HSCROLL|WS_VSCROLL),
562 FALSE, GetWindowLongW( data->hwnd, GWL_EXSTYLE ) );
564 rect->left -= rc.left;
565 rect->right -= rc.right;
566 rect->top -= rc.top;
567 rect->bottom -= rc.bottom;
568 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
569 if (rect->left >= rect->right) rect->right = rect->left + 1;
573 /***********************************************************************
574 * X11DRV_X_to_window_rect
576 * Opposite of X11DRV_window_to_X_rect
578 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
580 if (!data->managed) return;
581 if (IsRectEmpty( rect )) return;
583 AdjustWindowRectEx( rect, GetWindowLongW( data->hwnd, GWL_STYLE ) & ~(WS_HSCROLL|WS_VSCROLL),
584 FALSE, GetWindowLongW( data->hwnd, GWL_EXSTYLE ));
586 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
587 if (rect->left >= rect->right) rect->right = rect->left + 1;
591 /***********************************************************************
592 * X11DRV_sync_window_position
594 * Synchronize the X window position with the Windows one
596 void X11DRV_sync_window_position( Display *display, struct x11drv_win_data *data,
597 UINT swp_flags, const RECT *new_client_rect,
598 const RECT *new_whole_rect )
600 XWindowChanges changes;
601 int mask;
602 RECT old_whole_rect;
604 old_whole_rect = data->whole_rect;
605 data->whole_rect = *new_whole_rect;
607 data->client_rect = *new_client_rect;
608 OffsetRect( &data->client_rect, -data->whole_rect.left, -data->whole_rect.top );
609 SetPropA( data->hwnd, client_offset_atom,
610 (HANDLE)MAKELONG( data->client_rect.left, data->client_rect.top ));
612 if (!data->whole_window) return;
613 if (swp_flags & SWP_WINE_NOHOSTMOVE) return;
615 mask = get_window_changes( &changes, &old_whole_rect, &data->whole_rect );
617 if (!(swp_flags & SWP_NOZORDER))
619 /* find window that this one must be after */
620 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
621 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
622 prev = GetWindow( prev, GW_HWNDPREV );
623 if (!prev) /* top child */
625 changes.stack_mode = Above;
626 mask |= CWStackMode;
628 else
630 /* should use stack_mode Below but most window managers don't get it right */
631 /* so move it above the next one in Z order */
632 HWND next = GetWindow( data->hwnd, GW_HWNDNEXT );
633 while (next && !(GetWindowLongW( next, GWL_STYLE ) & WS_VISIBLE))
634 next = GetWindow( next, GW_HWNDNEXT );
635 if (next)
637 changes.stack_mode = Above;
638 changes.sibling = X11DRV_get_whole_window(next);
639 mask |= CWStackMode | CWSibling;
644 if (mask)
646 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
648 TRACE( "setting win %lx pos %ld,%ld,%ldx%ld after %lx changes=%x\n",
649 data->whole_window, data->whole_rect.left, data->whole_rect.top,
650 data->whole_rect.right - data->whole_rect.left,
651 data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
653 wine_tsx11_lock();
654 XSync( gdi_display, False ); /* flush graphics operations before moving the window */
655 if (mask & (CWWidth|CWHeight)) set_size_hints( display, data, style );
656 XReconfigureWMWindow( display, data->whole_window,
657 DefaultScreen(display), mask, &changes );
658 wine_tsx11_unlock();
663 /**********************************************************************
664 * create_desktop
666 static void create_desktop( Display *display, struct x11drv_win_data *data )
668 VisualID visualid;
670 wine_tsx11_lock();
671 winContext = XUniqueContext();
672 XInternAtoms( display, (char **)atom_names, NB_XATOMS - FIRST_XATOM, False, X11DRV_Atoms );
673 visualid = XVisualIDFromVisual(visual);
674 wine_tsx11_unlock();
676 whole_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_whole_window" ));
677 icon_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_icon_window" ));
678 client_offset_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_client_area_offset" ));
679 managed_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_managed" ));
681 data->whole_window = root_window;
682 data->whole_rect = data->client_rect = data->window_rect;
684 SetPropA( data->hwnd, whole_window_atom, (HANDLE)root_window );
685 SetPropA( data->hwnd, "__wine_x11_visual_id", (HANDLE)visualid );
687 X11DRV_InitClipboard();
689 if (root_window != DefaultRootWindow(display)) X11DRV_create_desktop_thread();
693 /**********************************************************************
694 * create_whole_window
696 * Create the whole X window for a given window
698 static Window create_whole_window( Display *display, struct x11drv_win_data *data, DWORD style )
700 int cx, cy, mask;
701 XSetWindowAttributes attr;
702 XIM xim;
703 RECT rect;
705 rect = data->window_rect;
706 X11DRV_window_to_X_rect( data, &rect );
708 if (!(cx = rect.right - rect.left)) cx = 1;
709 if (!(cy = rect.bottom - rect.top)) cy = 1;
711 mask = get_window_attributes( data, &attr );
713 /* set the attributes that don't change over the lifetime of the window */
714 attr.bit_gravity = NorthWestGravity;
715 attr.win_gravity = StaticGravity;
716 attr.backing_store = NotUseful;
717 mask |= CWBitGravity | CWWinGravity | CWBackingStore;
719 wine_tsx11_lock();
721 data->whole_rect = rect;
722 data->whole_window = XCreateWindow( display, root_window, rect.left, rect.top, cx, cy,
723 0, screen_depth, InputOutput, visual,
724 mask, &attr );
726 if (!data->whole_window)
728 wine_tsx11_unlock();
729 return 0;
731 XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
733 /* non-maximized child must be at bottom of Z order */
734 if ((style & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
736 XWindowChanges changes;
737 changes.stack_mode = Below;
738 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
740 XSync( display, False ); /* FIXME: should not be needed */
742 wine_tsx11_unlock();
744 xim = x11drv_thread_data()->xim;
745 if (xim) data->xic = X11DRV_CreateIC( xim, display, data->whole_window );
747 X11DRV_set_wm_hints( display, data );
749 SetPropA( data->hwnd, whole_window_atom, (HANDLE)data->whole_window );
750 return data->whole_window;
754 /**********************************************************************
755 * destroy_whole_window
757 * Destroy the whole X window for a given window.
759 static void destroy_whole_window( Display *display, struct x11drv_win_data *data )
761 struct x11drv_thread_data *thread_data = x11drv_thread_data();
763 if (!data->whole_window) return;
765 TRACE( "win %p xwin %lx\n", data->hwnd, data->whole_window );
766 if (thread_data->cursor_window == data->whole_window) thread_data->cursor_window = None;
767 wine_tsx11_lock();
768 XSync( gdi_display, False ); /* flush any reference to this drawable in GDI queue */
769 XDeleteContext( display, data->whole_window, winContext );
770 XDestroyWindow( display, data->whole_window ); /* this destroys client too */
771 data->whole_window = 0;
772 if (data->xic)
774 XUnsetICFocus( data->xic );
775 XDestroyIC( data->xic );
777 wine_tsx11_unlock();
778 RemovePropA( data->hwnd, whole_window_atom );
782 /*****************************************************************
783 * SetWindowText (X11DRV.@)
785 BOOL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
787 Display *display = thread_display();
788 UINT count;
789 char *buffer;
790 char *utf8_buffer;
791 Window win;
792 XTextProperty prop;
794 if ((win = X11DRV_get_whole_window( hwnd )))
796 /* allocate new buffer for window text */
797 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
798 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count )))
800 ERR("Not enough memory for window text\n");
801 return FALSE;
803 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
805 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
806 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
808 ERR("Not enough memory for window text in UTF-8\n");
809 HeapFree( GetProcessHeap(), 0, buffer );
810 return FALSE;
812 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
814 wine_tsx11_lock();
815 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
817 XSetWMName( display, win, &prop );
818 XSetWMIconName( display, win, &prop );
819 XFree( prop.value );
822 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
823 according to the standard
824 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
826 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
827 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
828 wine_tsx11_unlock();
830 HeapFree( GetProcessHeap(), 0, utf8_buffer );
831 HeapFree( GetProcessHeap(), 0, buffer );
833 return TRUE;
837 /***********************************************************************
838 * DestroyWindow (X11DRV.@)
840 BOOL X11DRV_DestroyWindow( HWND hwnd )
842 struct x11drv_thread_data *thread_data = x11drv_thread_data();
843 Display *display = thread_data->display;
844 struct x11drv_win_data *data;
846 if (!(data = X11DRV_get_win_data( hwnd ))) return TRUE;
848 free_window_dce( data );
849 destroy_whole_window( display, data );
850 destroy_icon_window( display, data );
852 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
853 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
854 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
855 wine_tsx11_lock();
856 XDeleteContext( display, (XID)hwnd, win_data_context );
857 wine_tsx11_unlock();
858 HeapFree( GetProcessHeap(), 0, data );
860 return TRUE;
864 /**********************************************************************
865 * CreateWindow (X11DRV.@)
867 BOOL X11DRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
869 Display *display = thread_display();
870 WND *wndPtr;
871 struct x11drv_win_data *data;
872 HWND parent, insert_after;
873 RECT rect;
874 DWORD style;
875 CBT_CREATEWNDA cbtc;
876 CREATESTRUCTA cbcs;
877 BOOL ret = FALSE;
879 if (cs->cx > 65535)
881 ERR( "invalid window width %d\n", cs->cx );
882 cs->cx = 65535;
884 if (cs->cy > 65535)
886 ERR( "invalid window height %d\n", cs->cy );
887 cs->cy = 65535;
889 if (cs->cx < 0)
891 ERR( "invalid window width %d\n", cs->cx );
892 cs->cx = 0;
894 if (cs->cy < 0)
896 ERR( "invalid window height %d\n", cs->cy );
897 cs->cy = 0;
900 if (!(data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data)))) return FALSE;
901 data->hwnd = hwnd;
902 data->whole_window = 0;
903 data->icon_window = 0;
904 data->xic = 0;
905 data->managed = FALSE;
906 data->dce = NULL;
907 data->hWMIconBitmap = 0;
908 data->hWMIconMask = 0;
910 wine_tsx11_lock();
911 if (!win_data_context) win_data_context = XUniqueContext();
912 XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
913 wine_tsx11_unlock();
915 /* initialize the dimensions before sending WM_GETMINMAXINFO */
916 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
917 X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER, NULL );
919 parent = GetAncestor( hwnd, GA_PARENT );
920 if (!parent)
922 create_desktop( display, data );
923 return TRUE;
926 /* create an X window if it's a top level window */
927 if (parent == GetDesktopWindow())
929 if (!create_whole_window( display, data, cs->style )) goto failed;
932 /* get class or window DC if needed */
933 alloc_window_dce( data );
935 /* Call the WH_CBT hook */
937 /* the window style passed to the hook must be the real window style,
938 * rather than just the window style that the caller to CreateWindowEx
939 * passed in, so we have to copy the original CREATESTRUCT and get the
940 * the real style. */
941 cbcs = *cs;
942 cbcs.style = GetWindowLongW(hwnd, GWL_STYLE);
944 cbtc.lpcs = &cbcs;
945 cbtc.hwndInsertAfter = HWND_TOP;
946 if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode ))
948 TRACE("CBT-hook returned !0\n");
949 goto failed;
952 /* Send the WM_GETMINMAXINFO message and fix the size if needed */
953 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
955 POINT maxSize, maxPos, minTrack, maxTrack;
957 WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
958 if (maxSize.x < cs->cx) cs->cx = maxSize.x;
959 if (maxSize.y < cs->cy) cs->cy = maxSize.y;
960 if (cs->cx < 0) cs->cx = 0;
961 if (cs->cy < 0) cs->cy = 0;
963 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
964 if (!X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER, NULL )) return FALSE;
967 /* send WM_NCCREATE */
968 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cs->cx, cs->cy );
969 if (unicode)
970 ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
971 else
972 ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
973 if (!ret)
975 WARN("aborted by WM_xxCREATE!\n");
976 return FALSE;
979 /* make sure the window is still valid */
980 if (!(data = X11DRV_get_win_data( hwnd ))) return FALSE;
981 if (data->whole_window) X11DRV_sync_window_style( display, data );
983 /* send WM_NCCALCSIZE */
984 rect = data->window_rect;
985 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&rect );
987 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
989 /* yes, even if the CBT hook was called with HWND_TOP */
990 insert_after = ((wndPtr->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
992 X11DRV_set_window_pos( hwnd, insert_after, &wndPtr->rectWindow, &rect, 0, NULL );
994 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",
995 hwnd, wndPtr->rectWindow.left, wndPtr->rectWindow.top,
996 wndPtr->rectWindow.right, wndPtr->rectWindow.bottom,
997 wndPtr->rectClient.left, wndPtr->rectClient.top,
998 wndPtr->rectClient.right, wndPtr->rectClient.bottom,
999 data->whole_rect.left, data->whole_rect.top,
1000 data->whole_rect.right, data->whole_rect.bottom,
1001 data->client_rect.left, data->client_rect.top,
1002 data->client_rect.right, data->client_rect.bottom,
1003 (unsigned int)data->whole_window );
1005 WIN_ReleasePtr( wndPtr );
1007 if (unicode)
1008 ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1009 else
1010 ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1012 if (!ret) return FALSE;
1014 NotifyWinEvent(EVENT_OBJECT_CREATE, hwnd, OBJID_WINDOW, 0);
1016 /* Send the size messages */
1018 if (!(wndPtr = WIN_GetPtr(hwnd)) || wndPtr == WND_OTHER_PROCESS) return FALSE;
1019 if (!(wndPtr->flags & WIN_NEED_SIZE))
1021 RECT rect = wndPtr->rectClient;
1022 WIN_ReleasePtr( wndPtr );
1023 /* send it anyway */
1024 if (((rect.right-rect.left) <0) ||((rect.bottom-rect.top)<0))
1025 WARN("sending bogus WM_SIZE message 0x%08lx\n",
1026 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1027 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1028 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1029 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( rect.left, rect.top ) );
1031 else WIN_ReleasePtr( wndPtr );
1033 /* Show the window, maximizing or minimizing if needed */
1035 style = GetWindowLongW( hwnd, GWL_STYLE );
1036 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1038 extern UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect ); /*FIXME*/
1040 RECT newPos;
1041 UINT swFlag = (style & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1042 WIN_SetStyle( hwnd, 0, WS_MAXIMIZE | WS_MINIMIZE );
1043 WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1044 swFlag = ((style & WS_CHILD) || GetActiveWindow())
1045 ? SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED
1046 : SWP_NOZORDER | SWP_FRAMECHANGED;
1047 SetWindowPos( hwnd, 0, newPos.left, newPos.top,
1048 newPos.right, newPos.bottom, swFlag );
1051 return TRUE;
1053 failed:
1054 X11DRV_DestroyWindow( hwnd );
1055 return FALSE;
1059 /***********************************************************************
1060 * X11DRV_get_win_data
1062 * Return the X11 data structure associated with a window.
1064 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1066 char *data;
1068 if (!hwnd || XFindContext( thread_display(), (XID)hwnd, win_data_context, &data )) data = NULL;
1069 return (struct x11drv_win_data *)data;
1073 /***********************************************************************
1074 * X11DRV_get_client_area_offset
1076 * Return the offset of the client area relative to the x11 window.
1078 POINT X11DRV_get_client_area_offset( HWND hwnd )
1080 POINT pt;
1081 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1083 if (data)
1085 pt.x = data->client_rect.left;
1086 pt.y = data->client_rect.top;
1088 else
1090 ULONG_PTR offset = (ULONG_PTR)GetPropA( hwnd, client_offset_atom );
1091 pt.x = (SHORT)LOWORD(offset);
1092 pt.y = (SHORT)HIWORD(offset);
1094 return pt;
1098 /***********************************************************************
1099 * X11DRV_get_whole_window
1101 * Return the X window associated with the full area of a window
1103 Window X11DRV_get_whole_window( HWND hwnd )
1105 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1107 if (!data) return (Window)GetPropA( hwnd, whole_window_atom );
1108 return data->whole_window;
1112 /***********************************************************************
1113 * X11DRV_get_ic
1115 * Return the X input context associated with a window
1117 XIC X11DRV_get_ic( HWND hwnd )
1119 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1121 if (!data) return 0;
1122 return data->xic;
1126 /*****************************************************************
1127 * SetParent (X11DRV.@)
1129 HWND X11DRV_SetParent( HWND hwnd, HWND parent )
1131 Display *display = thread_display();
1132 WND *wndPtr;
1133 BOOL ret;
1134 HWND old_parent = 0;
1136 /* Windows hides the window first, then shows it again
1137 * including the WM_SHOWWINDOW messages and all */
1138 BOOL was_visible = ShowWindow( hwnd, SW_HIDE );
1140 wndPtr = WIN_GetPtr( hwnd );
1141 if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return 0;
1143 SERVER_START_REQ( set_parent )
1145 req->handle = hwnd;
1146 req->parent = parent;
1147 if ((ret = !wine_server_call( req )))
1149 old_parent = reply->old_parent;
1150 wndPtr->parent = parent = reply->full_parent;
1154 SERVER_END_REQ;
1155 WIN_ReleasePtr( wndPtr );
1156 if (!ret) return 0;
1158 if (parent != old_parent)
1160 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1162 if (!data) return 0;
1164 if (parent != GetDesktopWindow()) /* a child window */
1166 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD))
1168 HMENU menu = (HMENU)SetWindowLongPtrW( hwnd, GWLP_ID, 0 );
1169 if (menu) DestroyMenu( menu );
1171 if (old_parent == GetDesktopWindow())
1173 /* destroy the old X windows */
1174 destroy_whole_window( display, data );
1175 destroy_icon_window( display, data );
1178 else /* new top level window */
1180 /* FIXME: we ignore errors since we can't really recover anyway */
1181 create_whole_window( display, data, GetWindowLongW( hwnd, GWL_STYLE ) );
1185 /* SetParent additionally needs to make hwnd the topmost window
1186 in the x-order and send the expected WM_WINDOWPOSCHANGING and
1187 WM_WINDOWPOSCHANGED notification messages.
1189 SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0,
1190 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | (was_visible ? SWP_SHOWWINDOW : 0) );
1191 /* FIXME: a WM_MOVE is also generated (in the DefWindowProc handler
1192 * for WM_WINDOWPOSCHANGED) in Windows, should probably remove SWP_NOMOVE */
1194 return old_parent;
1198 /*****************************************************************
1199 * SetFocus (X11DRV.@)
1201 * Set the X focus.
1202 * Explicit colormap management seems to work only with OLVWM.
1204 void X11DRV_SetFocus( HWND hwnd )
1206 Display *display = thread_display();
1207 struct x11drv_win_data *data;
1208 XWindowAttributes win_attr;
1210 /* Only mess with the X focus if there's */
1211 /* no desktop window and if the window is not managed by the WM. */
1212 if (root_window != DefaultRootWindow(display)) return;
1214 if (!hwnd) /* If setting the focus to 0, uninstall the colormap */
1216 wine_tsx11_lock();
1217 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1218 XUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1219 wine_tsx11_unlock();
1220 return;
1223 hwnd = GetAncestor( hwnd, GA_ROOT );
1225 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1226 if (!data->managed || !data->whole_window) return;
1228 /* Set X focus and install colormap */
1229 wine_tsx11_lock();
1230 if (XGetWindowAttributes( display, data->whole_window, &win_attr ) &&
1231 (win_attr.map_state == IsViewable))
1233 /* If window is not viewable, don't change anything */
1235 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1236 /* FIXME: this is not entirely correct */
1237 XSetInputFocus( display, data->whole_window, RevertToParent,
1238 /* CurrentTime */
1239 GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1240 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1241 XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1243 wine_tsx11_unlock();
1247 /**********************************************************************
1248 * SetWindowIcon (X11DRV.@)
1250 * hIcon or hIconSm has changed (or is being initialised for the
1251 * first time). Complete the X11 driver-specific initialisation
1252 * and set the window hints.
1254 * This is not entirely correct, may need to create
1255 * an icon window and set the pixmap as a background
1257 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
1259 Display *display = thread_display();
1260 struct x11drv_win_data *data;
1261 XWMHints* wm_hints;
1263 if (type != ICON_BIG) return; /* nothing to do here */
1265 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1266 if (!data->whole_window) return;
1267 if (!data->managed) return;
1269 wine_tsx11_lock();
1270 if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
1271 wine_tsx11_unlock();
1272 if (wm_hints)
1274 set_icon_hints( display, data, wm_hints, icon );
1275 wine_tsx11_lock();
1276 XSetWMHints( display, data->whole_window, wm_hints );
1277 XFree( wm_hints );
1278 wine_tsx11_unlock();