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
32 #include <X11/Xresource.h>
33 #include <X11/Xutil.h>
40 #include "wine/unicode.h"
42 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
50 /* X context to associate a hwnd to an X window */
51 XContext winContext = 0;
53 /* X context to associate a struct x11drv_win_data to an hwnd */
54 static XContext win_data_context;
56 Atom X11DRV_Atoms[NB_XATOMS - FIRST_XATOM];
58 static const char * const atom_names[NB_XATOMS - FIRST_XATOM] =
77 "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR",
82 "_NET_WM_WINDOW_TYPE",
83 "_NET_WM_WINDOW_TYPE_UTILITY",
107 static LPCSTR whole_window_atom;
108 static LPCSTR client_window_atom;
109 static LPCSTR icon_window_atom;
111 /***********************************************************************
114 * Check if a given window should be managed
116 inline static BOOL is_window_managed( HWND hwnd )
118 DWORD style, ex_style;
120 if (!managed_mode) return FALSE;
121 /* tray window is always managed */
122 ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
123 if (ex_style & WS_EX_TRAYWINDOW) return TRUE;
124 /* child windows are not managed */
125 style = GetWindowLongW( hwnd, GWL_STYLE );
126 if (style & WS_CHILD) return FALSE;
127 /* windows with caption are managed */
128 if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
129 /* tool windows are not managed */
130 if (ex_style & WS_EX_TOOLWINDOW) return FALSE;
131 /* windows with thick frame are managed */
132 if (style & WS_THICKFRAME) return TRUE;
133 /* application windows are managed */
134 if (ex_style & WS_EX_APPWINDOW) return TRUE;
135 /* full-screen popup windows are managed */
136 if (style & WS_POPUP)
139 GetWindowRect( hwnd, &rect );
140 if ((rect.right - rect.left) == screen_width && (rect.bottom - rect.top) == screen_height)
143 /* default: not managed */
148 /***********************************************************************
149 * is_client_window_mapped
151 * Check if the X client window should be mapped
153 inline static BOOL is_client_window_mapped( struct x11drv_win_data *data )
155 return !(GetWindowLongW( data->hwnd, GWL_STYLE ) & WS_MINIMIZE) && !IsRectEmpty( &data->client_rect );
159 /***********************************************************************
160 * X11DRV_is_window_rect_mapped
162 * Check if the X whole window should be mapped based on its rectangle
164 BOOL X11DRV_is_window_rect_mapped( const RECT *rect )
166 /* don't map if rect is empty */
167 if (IsRectEmpty( rect )) return FALSE;
169 /* don't map if rect is off-screen */
170 if (rect->left >= (int)screen_width || rect->top >= (int)screen_height) return FALSE;
171 if (rect->right < 0 || rect->bottom < 0) return FALSE;
177 /***********************************************************************
178 * get_window_attributes
180 * Fill the window attributes structure for an X window.
182 static int get_window_attributes( struct x11drv_win_data *data, XSetWindowAttributes *attr )
184 BOOL is_top_level = is_window_top_level( data->hwnd );
185 BOOL managed = is_top_level && is_window_managed( data->hwnd );
186 DWORD ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
188 if (managed) WIN_SetExStyle( data->hwnd, ex_style | WS_EX_MANAGED );
189 else WIN_SetExStyle( data->hwnd, ex_style & ~WS_EX_MANAGED );
191 attr->override_redirect = !managed;
192 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
193 attr->save_under = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
194 attr->cursor = x11drv_thread_data()->cursor;
195 attr->event_mask = (ExposureMask | PointerMotionMask |
196 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
199 attr->event_mask |= (KeyPressMask | KeyReleaseMask | StructureNotifyMask |
200 FocusChangeMask | KeymapStateMask);
202 return (CWOverrideRedirect | CWSaveUnder | CWEventMask | CWColormap | CWCursor);
206 /***********************************************************************
207 * X11DRV_sync_window_style
209 * Change the X window attributes when the window style has changed.
211 void X11DRV_sync_window_style( Display *display, struct x11drv_win_data *data )
213 XSetWindowAttributes attr;
214 int mask = get_window_attributes( data, &attr );
217 XChangeWindowAttributes( display, data->whole_window, mask, &attr );
222 /***********************************************************************
225 * fill the window changes structure
227 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
231 if (old->right - old->left != new->right - new->left )
233 if (!(changes->width = new->right - new->left)) changes->width = 1;
236 if (old->bottom - old->top != new->bottom - new->top)
238 if (!(changes->height = new->bottom - new->top)) changes->height = 1;
241 if (old->left != new->left)
243 changes->x = new->left;
246 if (old->top != new->top)
248 changes->y = new->top;
255 /***********************************************************************
258 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
260 XSetWindowAttributes attr;
262 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
263 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
264 attr.bit_gravity = NorthWestGravity;
265 attr.backing_store = NotUseful/*WhenMapped*/;
266 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
269 data->icon_window = XCreateWindow( display, root_window, 0, 0,
270 GetSystemMetrics( SM_CXICON ),
271 GetSystemMetrics( SM_CYICON ),
274 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
275 XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
278 TRACE( "created %lx\n", data->icon_window );
279 SetPropA( data->hwnd, icon_window_atom, (HANDLE)data->icon_window );
280 return data->icon_window;
285 /***********************************************************************
286 * destroy_icon_window
288 inline static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
290 if (!data->icon_window) return;
291 if (x11drv_thread_data()->cursor_window == data->icon_window)
292 x11drv_thread_data()->cursor_window = None;
294 XDeleteContext( display, data->icon_window, winContext );
295 XDestroyWindow( display, data->icon_window );
296 data->icon_window = 0;
298 RemovePropA( data->hwnd, icon_window_atom );
302 /***********************************************************************
305 * Set the icon wm hints
307 static void set_icon_hints( Display *display, struct x11drv_win_data *data,
308 XWMHints *hints, HICON hIcon, DWORD ex_style )
310 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
311 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
312 data->hWMIconBitmap = 0;
313 data->hWMIconMask = 0;
315 if (!(ex_style & WS_EX_MANAGED))
317 destroy_icon_window( display, data );
318 hints->flags &= ~(IconPixmapHint | IconMaskHint | IconWindowHint);
322 if (!data->icon_window) create_icon_window( display, data );
323 hints->icon_window = data->icon_window;
324 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
334 GetIconInfo(hIcon, &ii);
336 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
339 rcMask.right = bmMask.bmWidth;
340 rcMask.bottom = bmMask.bmHeight;
342 hDC = CreateCompatibleDC(0);
343 hbmOrig = SelectObject(hDC, ii.hbmMask);
344 InvertRect(hDC, &rcMask);
345 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
346 SelectObject(hDC, hbmOrig);
349 data->hWMIconBitmap = ii.hbmColor;
350 data->hWMIconMask = ii.hbmMask;
352 hints->icon_pixmap = X11DRV_BITMAP_Pixmap(data->hWMIconBitmap);
353 hints->icon_mask = X11DRV_BITMAP_Pixmap(data->hWMIconMask);
354 destroy_icon_window( display, data );
355 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
360 /***********************************************************************
363 * set the window size hints
365 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
367 XSizeHints* size_hints;
369 if ((size_hints = XAllocSizeHints()))
371 size_hints->win_gravity = StaticGravity;
372 size_hints->x = data->whole_rect.left;
373 size_hints->y = data->whole_rect.top;
374 size_hints->flags = PWinGravity | PPosition;
376 if ( !(style & WS_THICKFRAME) )
378 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
379 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
380 size_hints->min_width = size_hints->max_width;
381 size_hints->min_height = size_hints->max_height;
382 size_hints->flags |= PMinSize | PMaxSize;
384 XSetWMNormalHints( display, data->whole_window, size_hints );
390 /***********************************************************************
391 * X11DRV_set_wm_hints
393 * Set the window manager hints for a newly-created window
395 void X11DRV_set_wm_hints( Display *display, struct x11drv_win_data *data )
398 XClassHint *class_hints;
404 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
405 DWORD ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
406 HWND owner = GetWindow( data->hwnd, GW_OWNER );
408 /* transient for hint */
411 Window owner_win = X11DRV_get_whole_window( owner );
413 XSetTransientForHint( display, data->whole_window, owner_win );
415 group_leader = owner_win;
417 else group_leader = data->whole_window;
423 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
424 protocols[i++] = x11drv_atom(_NET_WM_PING);
425 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
426 XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
427 XA_ATOM, 32, PropModeReplace, (char *)protocols, i );
430 if ((class_hints = XAllocClassHint()))
432 class_hints->res_name = "wine";
433 class_hints->res_class = "Wine";
434 XSetClassHint( display, data->whole_window, class_hints );
435 XFree( class_hints );
439 set_size_hints( display, data, style );
441 /* systray properties (KDE only for now) */
442 if (ex_style & WS_EX_TRAYWINDOW)
445 XChangeProperty( display, data->whole_window, x11drv_atom(KWM_DOCKWINDOW),
446 x11drv_atom(KWM_DOCKWINDOW), 32, PropModeReplace, (char*)&val, 1 );
447 XChangeProperty( display, data->whole_window, x11drv_atom(_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR),
448 XA_WINDOW, 32, PropModeReplace, (char*)&data->whole_window, 1 );
451 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
452 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
453 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
455 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
456 XA_CARDINAL, 32, PropModeReplace, (char *)&i, 1);
458 /* map WS_EX_TOOLWINDOW to _NET_WM_WINDOW_TYPE_UTILITY */
459 if (ex_style & WS_EX_TOOLWINDOW)
461 Atom a = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
462 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
463 XA_ATOM, 32, PropModeReplace, (char*)&a, 1);
466 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
467 mwm_hints.functions = 0;
468 if ((style & WS_CAPTION) == WS_CAPTION) mwm_hints.functions |= MWM_FUNC_MOVE;
469 if (style & WS_THICKFRAME) mwm_hints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
470 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
471 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
472 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
473 mwm_hints.decorations = 0;
474 if ((style & WS_CAPTION) == WS_CAPTION) mwm_hints.decorations |= MWM_DECOR_TITLE;
475 if (ex_style & WS_EX_DLGMODALFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
476 else if (style & WS_THICKFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
477 else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
478 else if (style & WS_BORDER) mwm_hints.decorations |= MWM_DECOR_BORDER;
479 else if (!(style & (WS_CHILD|WS_POPUP))) mwm_hints.decorations |= MWM_DECOR_BORDER;
480 if (style & WS_SYSMENU) mwm_hints.decorations |= MWM_DECOR_MENU;
481 if (style & WS_MINIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MINIMIZE;
482 if (style & WS_MAXIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MAXIMIZE;
484 XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
485 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
486 (char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
488 XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
489 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
491 wm_hints = XAllocWMHints();
497 wm_hints->flags = InputHint | StateHint | WindowGroupHint;
498 wm_hints->input = !(style & WS_DISABLED);
500 set_icon_hints( display, data, wm_hints,
501 (HICON)GetClassLongW( data->hwnd, GCL_HICON ), ex_style );
503 wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
504 wm_hints->window_group = group_leader;
507 XSetWMHints( display, data->whole_window, wm_hints );
514 /***********************************************************************
515 * X11DRV_set_iconic_state
517 * Set the X11 iconic state according to the window style.
519 void X11DRV_set_iconic_state( HWND hwnd )
521 Display *display = thread_display();
522 struct x11drv_win_data *data;
525 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
526 BOOL iconic = (style & WS_MINIMIZE) != 0;
528 if (!(data = X11DRV_get_win_data( hwnd ))) return;
530 GetWindowRect( hwnd, &rect );
534 if (iconic) XUnmapWindow( display, data->client_window );
535 else if (!IsRectEmpty( &data->client_rect )) XMapWindow( display, data->client_window );
537 if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
538 wm_hints->flags |= StateHint | IconPositionHint;
539 wm_hints->initial_state = iconic ? IconicState : NormalState;
540 wm_hints->icon_x = rect.left;
541 wm_hints->icon_y = rect.top;
542 XSetWMHints( display, data->whole_window, wm_hints );
544 if (style & WS_VISIBLE)
547 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
549 if (X11DRV_is_window_rect_mapped( &rect ))
550 XMapWindow( display, data->whole_window );
558 /***********************************************************************
559 * X11DRV_window_to_X_rect
561 * Convert a rect from client to X window coordinates
563 void X11DRV_window_to_X_rect( HWND hwnd, RECT *rect )
566 DWORD ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
568 if (!(ex_style & WS_EX_MANAGED)) return;
569 if (IsRectEmpty( rect )) return;
571 rc.top = rc.bottom = rc.left = rc.right = 0;
573 AdjustWindowRectEx( &rc, GetWindowLongW(hwnd, GWL_STYLE) & ~(WS_HSCROLL|WS_VSCROLL),
576 rect->left -= rc.left;
577 rect->right -= rc.right;
579 rect->bottom -= rc.bottom;
580 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
581 if (rect->left >= rect->right) rect->right = rect->left + 1;
585 /***********************************************************************
586 * X11DRV_X_to_window_rect
588 * Opposite of X11DRV_window_to_X_rect
590 void X11DRV_X_to_window_rect( HWND hwnd, RECT *rect )
592 DWORD ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
594 if (!(ex_style & WS_EX_MANAGED)) return;
595 if (IsRectEmpty( rect )) return;
597 AdjustWindowRectEx( rect, GetWindowLongW(hwnd, GWL_STYLE) & ~(WS_HSCROLL|WS_VSCROLL),
600 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
601 if (rect->left >= rect->right) rect->right = rect->left + 1;
605 /***********************************************************************
606 * X11DRV_sync_whole_window_position
608 * Synchronize the X whole window position with the Windows one
610 int X11DRV_sync_whole_window_position( Display *display, struct x11drv_win_data *data, int zorder )
612 XWindowChanges changes;
616 whole_rect = data->window_rect;
617 X11DRV_window_to_X_rect( data->hwnd, &whole_rect );
618 mask = get_window_changes( &changes, &data->whole_rect, &whole_rect );
622 if (is_window_top_level( data->hwnd ))
624 /* find window that this one must be after */
625 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
626 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
627 prev = GetWindow( prev, GW_HWNDPREV );
628 if (!prev) /* top child */
630 changes.stack_mode = Above;
635 /* should use stack_mode Below but most window managers don't get it right */
636 /* so move it above the next one in Z order */
637 HWND next = GetWindow( data->hwnd, GW_HWNDNEXT );
638 while (next && !(GetWindowLongW( next, GWL_STYLE ) & WS_VISIBLE))
639 next = GetWindow( next, GW_HWNDNEXT );
642 changes.stack_mode = Above;
643 changes.sibling = X11DRV_get_whole_window(next);
644 mask |= CWStackMode | CWSibling;
650 HWND next = GetWindow( data->hwnd, GW_HWNDNEXT );
652 if (GetAncestor( data->hwnd, GA_PARENT ) == GetDesktopWindow() &&
653 root_window != DefaultRootWindow(display))
655 /* in desktop mode we need the sibling to belong to the same process */
658 if (X11DRV_get_win_data( next )) break;
659 next = GetWindow( next, GW_HWNDNEXT );
663 if (!next) /* bottom child */
665 changes.stack_mode = Below;
670 changes.stack_mode = Above;
671 changes.sibling = X11DRV_get_whole_window(next);
672 mask |= CWStackMode | CWSibling;
677 data->whole_rect = whole_rect;
681 TRACE( "setting win %lx pos %ld,%ld,%ldx%ld after %lx changes=%x\n",
682 data->whole_window, whole_rect.left, whole_rect.top,
683 whole_rect.right - whole_rect.left, whole_rect.bottom - whole_rect.top,
684 changes.sibling, mask );
686 XSync( gdi_display, False ); /* flush graphics operations before moving the window */
689 if (is_window_top_level( data->hwnd ))
691 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
694 if (mask & (CWWidth|CWHeight)) set_size_hints( display, data, style );
695 XReconfigureWMWindow( display, data->whole_window,
696 DefaultScreen(display), mask, &changes );
702 XConfigureWindow( display, data->whole_window, mask, &changes );
710 /***********************************************************************
711 * X11DRV_sync_client_window_position
713 * Synchronize the X client window position with the Windows one
715 int X11DRV_sync_client_window_position( Display *display, struct x11drv_win_data *data,
716 const RECT *new_client_rect )
718 XWindowChanges changes;
720 RECT client_rect = *new_client_rect;
722 OffsetRect( &client_rect, -data->whole_rect.left, -data->whole_rect.top );
724 if ((mask = get_window_changes( &changes, &data->client_rect, &client_rect )))
728 TRACE( "setting win %lx pos %ld,%ld,%ldx%ld (was %ld,%ld,%ldx%ld) after %lx changes=%x\n",
729 data->client_window, client_rect.left, client_rect.top,
730 client_rect.right - client_rect.left, client_rect.bottom - client_rect.top,
731 data->client_rect.left, data->client_rect.top,
732 data->client_rect.right - data->client_rect.left,
733 data->client_rect.bottom - data->client_rect.top,
734 changes.sibling, mask );
736 data->client_rect = client_rect;
737 is_mapped = is_client_window_mapped( data );
739 XSync( gdi_display, False ); /* flush graphics operations before moving the window */
740 if (!is_mapped) XUnmapWindow( display, data->client_window );
741 XConfigureWindow( display, data->client_window, mask, &changes );
742 if (is_mapped) XMapWindow( display, data->client_window );
749 /**********************************************************************
752 static void create_desktop( Display *display, struct x11drv_win_data *data )
757 winContext = XUniqueContext();
758 XInternAtoms( display, (char **)atom_names, NB_XATOMS - FIRST_XATOM, False, X11DRV_Atoms );
759 visualid = XVisualIDFromVisual(visual);
762 whole_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_whole_window" ));
763 client_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_client_window" ));
764 icon_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_icon_window" ));
766 data->whole_window = data->client_window = root_window;
767 data->whole_rect = data->client_rect = data->window_rect;
769 SetPropA( data->hwnd, whole_window_atom, (HANDLE)root_window );
770 SetPropA( data->hwnd, client_window_atom, (HANDLE)root_window );
771 SetPropA( data->hwnd, "__wine_x11_visual_id", (HANDLE)visualid );
773 X11DRV_InitClipboard();
775 if (root_window != DefaultRootWindow(display)) X11DRV_create_desktop_thread();
779 /**********************************************************************
780 * create_whole_window
782 * Create the whole X window for a given window
784 static Window create_whole_window( Display *display, struct x11drv_win_data *data, DWORD style )
787 XSetWindowAttributes attr;
790 BOOL is_top_level = is_window_top_level( data->hwnd );
792 rect = data->window_rect;
793 X11DRV_window_to_X_rect( data->hwnd, &rect );
795 if (!(cx = rect.right - rect.left)) cx = 1;
796 if (!(cy = rect.bottom - rect.top)) cy = 1;
798 parent = X11DRV_get_client_window( GetAncestor( data->hwnd, GA_PARENT ) );
800 mask = get_window_attributes( data, &attr );
802 /* set the attributes that don't change over the lifetime of the window */
803 attr.bit_gravity = ForgetGravity;
804 attr.win_gravity = NorthWestGravity;
805 attr.backing_store = NotUseful/*WhenMapped*/;
806 mask |= CWBitGravity | CWWinGravity | CWBackingStore;
810 data->whole_rect = rect;
811 data->whole_window = XCreateWindow( display, parent, rect.left, rect.top, cx, cy,
812 0, screen_depth, InputOutput, visual,
815 if (!data->whole_window)
820 XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
822 /* non-maximized child must be at bottom of Z order */
823 if ((style & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
825 XWindowChanges changes;
826 changes.stack_mode = Below;
827 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
834 XIM xim = x11drv_thread_data()->xim;
835 if (xim) data->xic = X11DRV_CreateIC( xim, display, data->whole_window );
836 X11DRV_set_wm_hints( display, data );
839 return data->whole_window;
843 /**********************************************************************
844 * create_client_window
846 * Create the client window for a given window
848 static Window create_client_window( Display *display, struct x11drv_win_data *data )
850 RECT rect = data->whole_rect;
851 XSetWindowAttributes attr;
854 OffsetRect( &rect, -data->whole_rect.left, -data->whole_rect.top );
855 data->client_rect = rect;
856 is_mapped = is_client_window_mapped( data );
858 attr.event_mask = (ExposureMask | PointerMotionMask |
859 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
860 attr.bit_gravity = (GetClassLongW( data->hwnd, GCL_STYLE ) & (CS_VREDRAW | CS_HREDRAW)) ?
861 ForgetGravity : NorthWestGravity;
862 attr.backing_store = NotUseful/*WhenMapped*/;
865 data->client_window = XCreateWindow( display, data->whole_window, 0, 0,
866 max( rect.right - rect.left, 1 ),
867 max( rect.bottom - rect.top, 1 ),
870 CWEventMask | CWBitGravity | CWBackingStore, &attr );
871 XSaveContext( display, data->client_window, winContext, (char *)data->hwnd );
872 if (data->client_window && is_mapped) XMapWindow( display, data->client_window );
874 return data->client_window;
878 /*****************************************************************
879 * SetWindowText (X11DRV.@)
881 BOOL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
883 Display *display = thread_display();
890 if ((win = X11DRV_get_whole_window( hwnd )))
892 /* allocate new buffer for window text */
893 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
894 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count )))
896 ERR("Not enough memory for window text\n");
899 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
901 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
902 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
904 ERR("Not enough memory for window text in UTF-8\n");
905 HeapFree( GetProcessHeap(), 0, buffer );
908 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
911 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
913 XSetWMName( display, win, &prop );
914 XSetWMIconName( display, win, &prop );
918 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
919 according to the standard
920 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
922 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
923 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
926 HeapFree( GetProcessHeap(), 0, utf8_buffer );
927 HeapFree( GetProcessHeap(), 0, buffer );
933 /***********************************************************************
934 * DestroyWindow (X11DRV.@)
936 BOOL X11DRV_DestroyWindow( HWND hwnd )
938 struct x11drv_thread_data *thread_data = x11drv_thread_data();
939 Display *display = thread_data->display;
940 WND *wndPtr = WIN_GetPtr( hwnd );
941 struct x11drv_win_data *data;
943 if (!(data = X11DRV_get_win_data( hwnd ))) goto done;
945 if (data->whole_window)
947 TRACE( "win %p xwin %lx/%lx\n", hwnd, data->whole_window, data->client_window );
948 if (thread_data->cursor_window == data->whole_window) thread_data->cursor_window = None;
949 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
951 XSync( gdi_display, False ); /* flush any reference to this drawable in GDI queue */
952 XDeleteContext( display, data->whole_window, winContext );
953 XDeleteContext( display, data->client_window, winContext );
954 XDestroyWindow( display, data->whole_window ); /* this destroys client too */
957 XUnsetICFocus( data->xic );
958 XDestroyIC( data->xic );
961 destroy_icon_window( display, data );
964 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
965 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
967 XDeleteContext( gdi_display, (XID)hwnd, win_data_context );
969 HeapFree( GetProcessHeap(), 0, data );
972 WIN_ReleasePtr( wndPtr );
977 /**********************************************************************
978 * CreateWindow (X11DRV.@)
980 BOOL X11DRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
982 Display *display = thread_display();
984 struct x11drv_win_data *data;
985 HWND parent, insert_after;
993 ERR( "invalid window width %d\n", cs->cx );
998 ERR( "invalid window height %d\n", cs->cy );
1003 ERR( "invalid window width %d\n", cs->cx );
1008 ERR( "invalid window height %d\n", cs->cy );
1012 if (!(data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data)))) return FALSE;
1014 data->whole_window = 0;
1015 data->client_window = 0;
1016 data->icon_window = 0;
1018 data->hWMIconBitmap = 0;
1019 data->hWMIconMask = 0;
1021 /* use gdi_display so that it's available from all threads (FIXME) */
1023 if (!win_data_context) win_data_context = XUniqueContext();
1024 XSaveContext( gdi_display, (XID)hwnd, win_data_context, (char *)data );
1025 wine_tsx11_unlock();
1027 /* initialize the dimensions before sending WM_GETMINMAXINFO */
1028 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
1029 X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER, 0 );
1031 parent = GetAncestor( hwnd, GA_PARENT );
1034 create_desktop( display, data );
1038 if (!create_whole_window( display, data, cs->style )) goto failed;
1039 if (!create_client_window( display, data )) goto failed;
1041 XSync( display, False );
1042 wine_tsx11_unlock();
1044 SetPropA( hwnd, whole_window_atom, (HANDLE)data->whole_window );
1045 SetPropA( hwnd, client_window_atom, (HANDLE)data->client_window );
1047 /* Call the WH_CBT hook */
1049 cbtc.hwndInsertAfter = HWND_TOP;
1050 if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode ))
1052 TRACE("CBT-hook returned !0\n");
1056 /* Send the WM_GETMINMAXINFO message and fix the size if needed */
1057 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
1059 POINT maxSize, maxPos, minTrack, maxTrack;
1061 WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
1062 if (maxSize.x < cs->cx) cs->cx = maxSize.x;
1063 if (maxSize.y < cs->cy) cs->cy = maxSize.y;
1064 if (cs->cx < 0) cs->cx = 0;
1065 if (cs->cy < 0) cs->cy = 0;
1067 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
1068 if (!X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER, 0 )) return FALSE;
1071 /* send WM_NCCREATE */
1072 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cs->cx, cs->cy );
1074 ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1076 ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
1079 WARN("aborted by WM_xxCREATE!\n");
1083 /* make sure the window is still valid */
1084 if (!(data = X11DRV_get_win_data( hwnd ))) return FALSE;
1085 X11DRV_sync_window_style( display, data );
1087 /* send WM_NCCALCSIZE */
1088 rect = data->window_rect;
1089 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&rect );
1091 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
1092 if (rect.left < wndPtr->rectWindow.left) rect.left = wndPtr->rectWindow.left;
1093 if (rect.right > wndPtr->rectWindow.right) rect.right = wndPtr->rectWindow.right;
1094 if (rect.top < wndPtr->rectWindow.top) rect.top = wndPtr->rectWindow.top;
1095 if (rect.bottom > wndPtr->rectWindow.bottom) rect.bottom = wndPtr->rectWindow.bottom;
1096 if (rect.left > rect.right || rect.top > rect.bottom) rect = wndPtr->rectWindow;
1098 /* yes, even if the CBT hook was called with HWND_TOP */
1099 insert_after = ((wndPtr->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD) ? HWND_BOTTOM : HWND_TOP;
1101 X11DRV_set_window_pos( hwnd, insert_after, &wndPtr->rectWindow, &rect, 0, 0 );
1103 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",
1104 hwnd, wndPtr->rectWindow.left, wndPtr->rectWindow.top,
1105 wndPtr->rectWindow.right, wndPtr->rectWindow.bottom,
1106 wndPtr->rectClient.left, wndPtr->rectClient.top,
1107 wndPtr->rectClient.right, wndPtr->rectClient.bottom,
1108 data->whole_rect.left, data->whole_rect.top,
1109 data->whole_rect.right, data->whole_rect.bottom,
1110 data->client_rect.left, data->client_rect.top,
1111 data->client_rect.right, data->client_rect.bottom,
1112 (unsigned int)data->whole_window, (unsigned int)data->client_window );
1114 WIN_ReleasePtr( wndPtr );
1117 ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1119 ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
1123 WIN_UnlinkWindow( hwnd );
1127 /* Send the size messages */
1129 if (!(wndPtr = WIN_GetPtr(hwnd)) || wndPtr == WND_OTHER_PROCESS) return FALSE;
1130 if (!(wndPtr->flags & WIN_NEED_SIZE))
1132 RECT rect = wndPtr->rectClient;
1133 WIN_ReleasePtr( wndPtr );
1134 /* send it anyway */
1135 if (((rect.right-rect.left) <0) ||((rect.bottom-rect.top)<0))
1136 WARN("sending bogus WM_SIZE message 0x%08lx\n",
1137 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1138 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1139 MAKELONG(rect.right-rect.left, rect.bottom-rect.top));
1140 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( rect.left, rect.top ) );
1142 else WIN_ReleasePtr( wndPtr );
1144 /* Show the window, maximizing or minimizing if needed */
1146 style = GetWindowLongW( hwnd, GWL_STYLE );
1147 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1149 extern UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect ); /*FIXME*/
1152 UINT swFlag = (style & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1153 WIN_SetStyle( hwnd, 0, WS_MAXIMIZE | WS_MINIMIZE );
1154 WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1155 swFlag = ((style & WS_CHILD) || GetActiveWindow())
1156 ? SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED
1157 : SWP_NOZORDER | SWP_FRAMECHANGED;
1158 SetWindowPos( hwnd, 0, newPos.left, newPos.top,
1159 newPos.right, newPos.bottom, swFlag );
1165 X11DRV_DestroyWindow( hwnd );
1170 /***********************************************************************
1171 * X11DRV_get_win_data
1173 * Return the X11 data structure associated with a window.
1175 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1178 if (XFindContext( gdi_display, (XID)hwnd, win_data_context, &data )) data = NULL;
1179 return (struct x11drv_win_data *)data;
1183 /***********************************************************************
1184 * X11DRV_get_client_window
1186 * Return the X window associated with the client area of a window
1188 Window X11DRV_get_client_window( HWND hwnd )
1190 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1192 if (!data) return (Window)GetPropA( hwnd, client_window_atom );
1193 return data->client_window;
1197 /***********************************************************************
1198 * X11DRV_get_whole_window
1200 * Return the X window associated with the full area of a window
1202 Window X11DRV_get_whole_window( HWND hwnd )
1204 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1206 if (!data) return (Window)GetPropA( hwnd, whole_window_atom );
1207 return data->whole_window;
1211 /***********************************************************************
1214 * Return the X input context associated with a window
1216 XIC X11DRV_get_ic( HWND hwnd )
1218 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1220 if (!data) return 0;
1225 /*****************************************************************
1226 * SetParent (X11DRV.@)
1228 HWND X11DRV_SetParent( HWND hwnd, HWND parent )
1230 Display *display = thread_display();
1233 /* Windows hides the window first, then shows it again
1234 * including the WM_SHOWWINDOW messages and all */
1235 BOOL was_visible = ShowWindow( hwnd, SW_HIDE );
1237 if (!IsWindow( parent )) return 0;
1239 old_parent = GetAncestor( hwnd, GA_PARENT );
1240 if (parent != old_parent)
1242 struct x11drv_win_data *data;
1243 Window new_parent = X11DRV_get_client_window( parent );
1245 if (!(data = X11DRV_get_win_data( hwnd ))) return 0;
1247 WIN_LinkWindow( hwnd, parent, HWND_TOP );
1249 if (parent != GetDesktopWindow()) /* a child window */
1251 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD))
1253 HMENU menu = (HMENU)SetWindowLongPtrW( hwnd, GWLP_ID, 0 );
1254 if (menu) DestroyMenu( menu );
1258 if (is_window_top_level( data->hwnd )) X11DRV_set_wm_hints( display, data );
1259 X11DRV_sync_window_style( display, data );
1261 XReparentWindow( display, data->whole_window, new_parent,
1262 data->whole_rect.left, data->whole_rect.top );
1263 wine_tsx11_unlock();
1266 /* SetParent additionally needs to make hwnd the topmost window
1267 in the x-order and send the expected WM_WINDOWPOSCHANGING and
1268 WM_WINDOWPOSCHANGED notification messages.
1270 SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0,
1271 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | (was_visible ? SWP_SHOWWINDOW : 0) );
1272 /* FIXME: a WM_MOVE is also generated (in the DefWindowProc handler
1273 * for WM_WINDOWPOSCHANGED) in Windows, should probably remove SWP_NOMOVE */
1279 /*****************************************************************
1280 * SetFocus (X11DRV.@)
1283 * Explicit colormap management seems to work only with OLVWM.
1285 void X11DRV_SetFocus( HWND hwnd )
1287 Display *display = thread_display();
1288 XWindowAttributes win_attr;
1291 /* Only mess with the X focus if there's */
1292 /* no desktop window and if the window is not managed by the WM. */
1293 if (root_window != DefaultRootWindow(display)) return;
1295 if (!hwnd) /* If setting the focus to 0, uninstall the colormap */
1298 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1299 XUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1300 wine_tsx11_unlock();
1304 hwnd = GetAncestor( hwnd, GA_ROOT );
1305 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_MANAGED) return;
1306 if (!(win = X11DRV_get_whole_window( hwnd ))) return;
1308 /* Set X focus and install colormap */
1310 if (XGetWindowAttributes( display, win, &win_attr ) &&
1311 (win_attr.map_state == IsViewable))
1313 /* If window is not viewable, don't change anything */
1315 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1316 /* FIXME: this is not entirely correct */
1317 XSetInputFocus( display, win, RevertToParent,
1319 GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1320 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1321 XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1323 wine_tsx11_unlock();
1327 /**********************************************************************
1328 * SetWindowIcon (X11DRV.@)
1330 * hIcon or hIconSm has changed (or is being initialised for the
1331 * first time). Complete the X11 driver-specific initialisation
1332 * and set the window hints.
1334 * This is not entirely correct, may need to create
1335 * an icon window and set the pixmap as a background
1337 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
1339 Display *display = thread_display();
1340 struct x11drv_win_data *data;
1343 if (type != ICON_BIG) return; /* nothing to do here */
1345 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1347 ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
1348 if (ex_style & WS_EX_MANAGED)
1353 if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
1354 wine_tsx11_unlock();
1357 set_icon_hints( display, data, wm_hints, icon, ex_style );
1359 XSetWMHints( display, data->whole_window, wm_hints );
1361 wine_tsx11_unlock();