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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
33 #include <X11/Xresource.h>
34 #include <X11/Xutil.h>
40 #include "wine/unicode.h"
43 #include "wine/debug.h"
44 #include "wine/server.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 static const char whole_window_prop
[] = "__wine_x11_whole_window";
57 static const char icon_window_prop
[] = "__wine_x11_icon_window";
58 static const char managed_prop
[] = "__wine_x11_managed";
59 static const char visual_id_prop
[] = "__wine_x11_visual_id";
61 /* for XDG systray icons */
62 #define SYSTEM_TRAY_REQUEST_DOCK 0
64 /***********************************************************************
67 * Check if a given window should be managed
69 BOOL
is_window_managed( HWND hwnd
, const RECT
*window_rect
)
71 DWORD style
, ex_style
;
73 /* tray window is always managed */
74 ex_style
= GetWindowLongW( hwnd
, GWL_EXSTYLE
);
75 if (ex_style
& WS_EX_TRAYWINDOW
) return TRUE
;
76 /* child windows are not managed */
77 style
= GetWindowLongW( hwnd
, GWL_STYLE
);
78 if ((style
& (WS_CHILD
|WS_POPUP
)) == WS_CHILD
) return FALSE
;
79 #ifndef DEFAULTMANAGED
80 /* set to 1 to enable default managed instead of default not managed */
81 #define DEFAULTMANAGED 0
84 /* windows with caption are managed */
85 if ((style
& WS_CAPTION
) == WS_CAPTION
) return TRUE
;
86 /* tool windows are not managed */
87 if (ex_style
& WS_EX_TOOLWINDOW
) return FALSE
;
88 /* windows with thick frame are managed */
89 if (style
& WS_THICKFRAME
) return TRUE
;
90 /* application windows are managed */
91 if (ex_style
& WS_EX_APPWINDOW
) return TRUE
;
95 /* popup with sysmenu == caption are managed */
96 if (style
& WS_SYSMENU
) return TRUE
;
97 /* full-screen popup windows are managed */
98 if ((window_rect
->right
- window_rect
->left
) == screen_width
&&
99 (window_rect
->bottom
- window_rect
->top
) == screen_height
)
102 /* all other popups are not managed */
107 /* default: managed */
110 /* default: not managed */
117 /***********************************************************************
118 * X11DRV_is_window_rect_mapped
120 * Check if the X whole window should be mapped based on its rectangle
122 BOOL
X11DRV_is_window_rect_mapped( const RECT
*rect
)
124 /* don't map if rect is empty */
125 if (IsRectEmpty( rect
)) return FALSE
;
127 /* don't map if rect is off-screen */
128 if (rect
->left
>= virtual_screen_rect
.right
||
129 rect
->top
>= virtual_screen_rect
.bottom
||
130 rect
->right
<= virtual_screen_rect
.left
||
131 rect
->bottom
<= virtual_screen_rect
.top
)
138 /***********************************************************************
139 * get_window_attributes
141 * Fill the window attributes structure for an X window.
143 static int get_window_attributes( Display
*display
, struct x11drv_win_data
*data
,
144 XSetWindowAttributes
*attr
)
146 attr
->override_redirect
= !data
->managed
;
147 attr
->colormap
= X11DRV_PALETTE_PaletteXColormap
;
148 attr
->save_under
= ((GetClassLongW( data
->hwnd
, GCL_STYLE
) & CS_SAVEBITS
) != 0);
149 attr
->cursor
= x11drv_thread_data()->cursor
;
150 attr
->bit_gravity
= NorthWestGravity
;
151 attr
->backing_store
= NotUseful
;
152 attr
->event_mask
= (ExposureMask
| PointerMotionMask
|
153 ButtonPressMask
| ButtonReleaseMask
| EnterWindowMask
|
154 KeyPressMask
| KeyReleaseMask
| FocusChangeMask
| KeymapStateMask
);
155 if (data
->managed
) attr
->event_mask
|= StructureNotifyMask
;
157 return (CWOverrideRedirect
| CWSaveUnder
| CWColormap
| CWCursor
|
158 CWEventMask
| CWBitGravity
| CWBackingStore
);
162 /***********************************************************************
163 * X11DRV_sync_window_style
165 * Change the X window attributes when the window style has changed.
167 void X11DRV_sync_window_style( Display
*display
, struct x11drv_win_data
*data
)
169 if (data
->whole_window
!= root_window
)
171 XSetWindowAttributes attr
;
172 int mask
= get_window_attributes( display
, data
, &attr
);
175 XChangeWindowAttributes( display
, data
->whole_window
, mask
, &attr
);
181 /***********************************************************************
184 * fill the window changes structure
186 static int get_window_changes( XWindowChanges
*changes
, const RECT
*old
, const RECT
*new )
190 if (old
->right
- old
->left
!= new->right
- new->left
)
192 if (!(changes
->width
= new->right
- new->left
)) changes
->width
= 1;
195 if (old
->bottom
- old
->top
!= new->bottom
- new->top
)
197 if (!(changes
->height
= new->bottom
- new->top
)) changes
->height
= 1;
200 if (old
->left
!= new->left
)
202 changes
->x
= new->left
;
205 if (old
->top
!= new->top
)
207 changes
->y
= new->top
;
214 /***********************************************************************
217 static Window
create_icon_window( Display
*display
, struct x11drv_win_data
*data
)
219 XSetWindowAttributes attr
;
221 attr
.event_mask
= (ExposureMask
| KeyPressMask
| KeyReleaseMask
| PointerMotionMask
|
222 ButtonPressMask
| ButtonReleaseMask
| EnterWindowMask
);
223 attr
.bit_gravity
= NorthWestGravity
;
224 attr
.backing_store
= NotUseful
/*WhenMapped*/;
225 attr
.colormap
= X11DRV_PALETTE_PaletteXColormap
; /* Needed due to our visual */
228 data
->icon_window
= XCreateWindow( display
, root_window
, 0, 0,
229 GetSystemMetrics( SM_CXICON
),
230 GetSystemMetrics( SM_CYICON
),
233 CWEventMask
| CWBitGravity
| CWBackingStore
| CWColormap
, &attr
);
234 XSaveContext( display
, data
->icon_window
, winContext
, (char *)data
->hwnd
);
237 TRACE( "created %lx\n", data
->icon_window
);
238 SetPropA( data
->hwnd
, icon_window_prop
, (HANDLE
)data
->icon_window
);
239 return data
->icon_window
;
244 /***********************************************************************
245 * destroy_icon_window
247 static void destroy_icon_window( Display
*display
, struct x11drv_win_data
*data
)
249 if (!data
->icon_window
) return;
250 if (x11drv_thread_data()->cursor_window
== data
->icon_window
)
251 x11drv_thread_data()->cursor_window
= None
;
253 XDeleteContext( display
, data
->icon_window
, winContext
);
254 XDestroyWindow( display
, data
->icon_window
);
255 data
->icon_window
= 0;
257 RemovePropA( data
->hwnd
, icon_window_prop
);
261 /***********************************************************************
264 * Set the icon wm hints
266 static void set_icon_hints( Display
*display
, struct x11drv_win_data
*data
, HICON hIcon
)
268 XWMHints
*hints
= data
->wm_hints
;
270 if (data
->hWMIconBitmap
) DeleteObject( data
->hWMIconBitmap
);
271 if (data
->hWMIconMask
) DeleteObject( data
->hWMIconMask
);
272 data
->hWMIconBitmap
= 0;
273 data
->hWMIconMask
= 0;
277 destroy_icon_window( display
, data
);
278 hints
->flags
&= ~(IconPixmapHint
| IconMaskHint
| IconWindowHint
);
282 if (!data
->icon_window
) create_icon_window( display
, data
);
283 hints
->icon_window
= data
->icon_window
;
284 hints
->flags
= (hints
->flags
& ~(IconPixmapHint
| IconMaskHint
)) | IconWindowHint
;
294 GetIconInfo(hIcon
, &ii
);
296 GetObjectA(ii
.hbmMask
, sizeof(bmMask
), &bmMask
);
299 rcMask
.right
= bmMask
.bmWidth
;
300 rcMask
.bottom
= bmMask
.bmHeight
;
302 hDC
= CreateCompatibleDC(0);
303 hbmOrig
= SelectObject(hDC
, ii
.hbmMask
);
304 InvertRect(hDC
, &rcMask
);
305 SelectObject(hDC
, ii
.hbmColor
); /* force the color bitmap to x11drv mode too */
306 SelectObject(hDC
, hbmOrig
);
309 data
->hWMIconBitmap
= ii
.hbmColor
;
310 data
->hWMIconMask
= ii
.hbmMask
;
312 hints
->icon_pixmap
= X11DRV_get_pixmap(data
->hWMIconBitmap
);
313 hints
->icon_mask
= X11DRV_get_pixmap(data
->hWMIconMask
);
314 destroy_icon_window( display
, data
);
315 hints
->flags
= (hints
->flags
& ~IconWindowHint
) | IconPixmapHint
| IconMaskHint
;
319 /***********************************************************************
320 * systray_dock_window
322 * Docks the given X window with the NETWM system tray.
324 static void systray_dock_window( Display
*display
, struct x11drv_win_data
*data
)
326 static Atom systray_atom
;
327 Window systray_window
;
332 if (DefaultScreen( display
) == 0)
333 systray_atom
= x11drv_atom(_NET_SYSTEM_TRAY_S0
);
336 char systray_buffer
[29]; /* strlen(_NET_SYSTEM_TRAY_S4294967295)+1 */
337 sprintf( systray_buffer
, "_NET_SYSTEM_TRAY_S%u", DefaultScreen( display
) );
338 systray_atom
= XInternAtom( display
, systray_buffer
, False
);
341 systray_window
= XGetSelectionOwner( display
, systray_atom
);
344 TRACE("Docking tray icon %p\n", data
->hwnd
);
346 if (systray_window
!= None
)
349 unsigned long info
[2];
351 /* Put the window offscreen so it isn't mapped. The window _cannot_ be
352 * mapped if we intend to dock with an XEMBED tray. If the window is
353 * mapped when we dock, it may become visible as a child of the root
354 * window after it docks, which isn't the proper behavior.
356 * For more information on this problem, see
357 * http://standards.freedesktop.org/xembed-spec/latest/ar01s04.html */
359 SetWindowPos( data
->hwnd
, NULL
, virtual_screen_rect
.right
+ 1, virtual_screen_rect
.bottom
+ 1,
360 0, 0, SWP_NOZORDER
| SWP_NOSIZE
);
362 /* set XEMBED protocol data on the window */
363 info
[0] = 0; /* protocol version */
364 info
[1] = 1; /* mapped = true */
367 XChangeProperty( display
, data
->whole_window
,
368 x11drv_atom(_XEMBED_INFO
),
369 x11drv_atom(_XEMBED_INFO
), 32, PropModeReplace
,
370 (unsigned char*)info
, 2 );
373 /* send the docking request message */
374 ZeroMemory( &ev
, sizeof(ev
) );
375 ev
.xclient
.type
= ClientMessage
;
376 ev
.xclient
.window
= systray_window
;
377 ev
.xclient
.message_type
= x11drv_atom( _NET_SYSTEM_TRAY_OPCODE
);
378 ev
.xclient
.format
= 32;
379 ev
.xclient
.data
.l
[0] = CurrentTime
;
380 ev
.xclient
.data
.l
[1] = SYSTEM_TRAY_REQUEST_DOCK
;
381 ev
.xclient
.data
.l
[2] = data
->whole_window
;
382 ev
.xclient
.data
.l
[3] = 0;
383 ev
.xclient
.data
.l
[4] = 0;
386 XSendEvent( display
, systray_window
, False
, NoEventMask
, &ev
);
394 /* fall back to he KDE hints if the WM doesn't support XEMBED'ed
398 XChangeProperty( display
, data
->whole_window
,
399 x11drv_atom(KWM_DOCKWINDOW
),
400 x11drv_atom(KWM_DOCKWINDOW
), 32, PropModeReplace
,
401 (unsigned char*)&val
, 1 );
402 XChangeProperty( display
, data
->whole_window
,
403 x11drv_atom(_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR
),
404 XA_WINDOW
, 32, PropModeReplace
,
405 (unsigned char*)&data
->whole_window
, 1 );
411 /***********************************************************************
414 * set the window size hints
416 static void set_size_hints( Display
*display
, struct x11drv_win_data
*data
, DWORD style
)
418 XSizeHints
* size_hints
;
420 if ((size_hints
= XAllocSizeHints()))
422 size_hints
->flags
= 0;
424 if (data
->hwnd
!= GetDesktopWindow()) /* don't force position of desktop */
426 size_hints
->win_gravity
= StaticGravity
;
427 size_hints
->x
= data
->whole_rect
.left
;
428 size_hints
->y
= data
->whole_rect
.top
;
429 size_hints
->flags
|= PWinGravity
| PPosition
;
432 if ( !(style
& WS_THICKFRAME
) )
434 size_hints
->max_width
= data
->whole_rect
.right
- data
->whole_rect
.left
;
435 size_hints
->max_height
= data
->whole_rect
.bottom
- data
->whole_rect
.top
;
436 size_hints
->min_width
= size_hints
->max_width
;
437 size_hints
->min_height
= size_hints
->max_height
;
438 size_hints
->flags
|= PMinSize
| PMaxSize
;
440 XSetWMNormalHints( display
, data
->whole_window
, size_hints
);
446 /***********************************************************************
449 * get the name of the current process for setting class hints
451 static char *get_process_name(void)
457 WCHAR module
[MAX_PATH
];
458 DWORD len
= GetModuleFileNameW( 0, module
, MAX_PATH
);
459 if (len
&& len
< MAX_PATH
)
462 WCHAR
*p
, *appname
= module
;
464 if ((p
= strrchrW( appname
, '/' ))) appname
= p
+ 1;
465 if ((p
= strrchrW( appname
, '\\' ))) appname
= p
+ 1;
466 len
= WideCharToMultiByte( CP_UNIXCP
, 0, appname
, -1, NULL
, 0, NULL
, NULL
);
467 if ((ptr
= HeapAlloc( GetProcessHeap(), 0, len
)))
469 WideCharToMultiByte( CP_UNIXCP
, 0, appname
, -1, ptr
, len
, NULL
, NULL
);
478 /***********************************************************************
479 * set_initial_wm_hints
481 * Set the window manager hints that don't change over the lifetime of a window.
483 static void set_initial_wm_hints( Display
*display
, struct x11drv_win_data
*data
)
488 XClassHint
*class_hints
;
489 char *process_name
= get_process_name();
495 protocols
[i
++] = x11drv_atom(WM_DELETE_WINDOW
);
496 protocols
[i
++] = x11drv_atom(_NET_WM_PING
);
497 if (use_take_focus
) protocols
[i
++] = x11drv_atom(WM_TAKE_FOCUS
);
498 XChangeProperty( display
, data
->whole_window
, x11drv_atom(WM_PROTOCOLS
),
499 XA_ATOM
, 32, PropModeReplace
, (unsigned char *)protocols
, i
);
502 if ((class_hints
= XAllocClassHint()))
504 static char wine
[] = "Wine";
506 class_hints
->res_name
= process_name
;
507 class_hints
->res_class
= wine
;
508 XSetClassHint( display
, data
->whole_window
, class_hints
);
509 XFree( class_hints
);
512 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
513 XSetWMProperties(display
, data
->whole_window
, NULL
, NULL
, NULL
, 0, NULL
, NULL
, NULL
);
514 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
516 XChangeProperty(display
, data
->whole_window
, x11drv_atom(_NET_WM_PID
),
517 XA_CARDINAL
, 32, PropModeReplace
, (unsigned char *)&i
, 1);
519 XChangeProperty( display
, data
->whole_window
, x11drv_atom(XdndAware
),
520 XA_ATOM
, 32, PropModeReplace
, (unsigned char*)&dndVersion
, 1 );
526 /***********************************************************************
527 * X11DRV_set_wm_hints
529 * Set the window manager hints for a newly-created window
531 void X11DRV_set_wm_hints( Display
*display
, struct x11drv_win_data
*data
)
536 DWORD style
= GetWindowLongW( data
->hwnd
, GWL_STYLE
);
537 DWORD ex_style
= GetWindowLongW( data
->hwnd
, GWL_EXSTYLE
);
538 HWND owner
= GetWindow( data
->hwnd
, GW_OWNER
);
540 if (data
->hwnd
== GetDesktopWindow())
542 if (data
->whole_window
== DefaultRootWindow(display
)) return;
543 /* force some styles for the desktop to get the correct decorations */
544 style
|= WS_CAPTION
| WS_SYSMENU
| WS_MINIMIZEBOX
;
548 /* transient for hint */
551 Window owner_win
= X11DRV_get_whole_window( owner
);
553 XSetTransientForHint( display
, data
->whole_window
, owner_win
);
555 group_leader
= owner_win
;
557 else group_leader
= data
->whole_window
;
562 set_size_hints( display
, data
, style
);
564 /* set the WM_WINDOW_TYPE */
565 window_type
= x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL
);
566 if (ex_style
& WS_EX_TOOLWINDOW
) window_type
= x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY
);
567 else if (style
& WS_THICKFRAME
) window_type
= x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL
);
568 else if (style
& WS_DLGFRAME
) window_type
= x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG
);
569 else if (ex_style
& WS_EX_DLGMODALFRAME
) window_type
= x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG
);
571 XChangeProperty(display
, data
->whole_window
, x11drv_atom(_NET_WM_WINDOW_TYPE
),
572 XA_ATOM
, 32, PropModeReplace
, (unsigned char*)&window_type
, 1);
574 mwm_hints
.flags
= MWM_HINTS_FUNCTIONS
| MWM_HINTS_DECORATIONS
;
575 mwm_hints
.functions
= MWM_FUNC_MOVE
;
576 if (style
& WS_THICKFRAME
) mwm_hints
.functions
|= MWM_FUNC_RESIZE
;
577 if (style
& WS_MINIMIZEBOX
) mwm_hints
.functions
|= MWM_FUNC_MINIMIZE
;
578 if (style
& WS_MAXIMIZEBOX
) mwm_hints
.functions
|= MWM_FUNC_MAXIMIZE
;
579 if (style
& WS_SYSMENU
) mwm_hints
.functions
|= MWM_FUNC_CLOSE
;
580 mwm_hints
.decorations
= 0;
581 if ((style
& WS_CAPTION
) == WS_CAPTION
)
583 mwm_hints
.decorations
|= MWM_DECOR_TITLE
;
584 if (style
& WS_SYSMENU
) mwm_hints
.decorations
|= MWM_DECOR_MENU
;
585 if (style
& WS_MINIMIZEBOX
) mwm_hints
.decorations
|= MWM_DECOR_MINIMIZE
;
586 if (style
& WS_MAXIMIZEBOX
) mwm_hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
588 if (ex_style
& WS_EX_DLGMODALFRAME
) mwm_hints
.decorations
|= MWM_DECOR_BORDER
;
589 else if (style
& WS_THICKFRAME
) mwm_hints
.decorations
|= MWM_DECOR_BORDER
| MWM_DECOR_RESIZEH
;
590 else if ((style
& (WS_DLGFRAME
|WS_BORDER
)) == WS_DLGFRAME
) mwm_hints
.decorations
|= MWM_DECOR_BORDER
;
591 else if (style
& WS_BORDER
) mwm_hints
.decorations
|= MWM_DECOR_BORDER
;
592 else if (!(style
& (WS_CHILD
|WS_POPUP
))) mwm_hints
.decorations
|= MWM_DECOR_BORDER
;
594 XChangeProperty( display
, data
->whole_window
, x11drv_atom(_MOTIF_WM_HINTS
),
595 x11drv_atom(_MOTIF_WM_HINTS
), 32, PropModeReplace
,
596 (unsigned char*)&mwm_hints
, sizeof(mwm_hints
)/sizeof(long) );
603 data
->wm_hints
->flags
= InputHint
| StateHint
| WindowGroupHint
;
604 data
->wm_hints
->input
= !(style
& WS_DISABLED
);
605 data
->wm_hints
->initial_state
= (style
& WS_MINIMIZE
) ? IconicState
: NormalState
;
606 data
->wm_hints
->window_group
= group_leader
;
607 set_icon_hints( display
, data
, (HICON
)GetClassLongPtrW( data
->hwnd
, GCLP_HICON
) );
610 XSetWMHints( display
, data
->whole_window
, data
->wm_hints
);
616 /***********************************************************************
617 * X11DRV_set_iconic_state
619 * Set the X11 iconic state according to the window style.
621 void X11DRV_set_iconic_state( HWND hwnd
)
623 Display
*display
= thread_display();
624 struct x11drv_win_data
*data
;
626 DWORD style
= GetWindowLongW( hwnd
, GWL_STYLE
);
627 BOOL iconic
= (style
& WS_MINIMIZE
) != 0;
629 if (!(data
= X11DRV_get_win_data( hwnd
))) return;
630 if (!data
->whole_window
|| data
->whole_window
== DefaultRootWindow(display
)) return;
632 GetWindowRect( hwnd
, &rect
);
638 data
->wm_hints
->flags
|= StateHint
| IconPositionHint
;
639 data
->wm_hints
->initial_state
= iconic
? IconicState
: NormalState
;
640 data
->wm_hints
->icon_x
= rect
.left
- virtual_screen_rect
.left
;
641 data
->wm_hints
->icon_y
= rect
.top
- virtual_screen_rect
.top
;
642 XSetWMHints( display
, data
->whole_window
, data
->wm_hints
);
645 if (style
& WS_VISIBLE
)
648 XIconifyWindow( display
, data
->whole_window
, DefaultScreen(display
) );
650 if (X11DRV_is_window_rect_mapped( &rect
))
651 XMapWindow( display
, data
->whole_window
);
658 /***********************************************************************
659 * X11DRV_window_to_X_rect
661 * Convert a rect from client to X window coordinates
663 void X11DRV_window_to_X_rect( struct x11drv_win_data
*data
, RECT
*rect
)
667 if (!data
->managed
) return;
668 if (IsRectEmpty( rect
)) return;
670 rc
.top
= rc
.bottom
= rc
.left
= rc
.right
= 0;
672 AdjustWindowRectEx( &rc
, GetWindowLongW( data
->hwnd
, GWL_STYLE
) & ~(WS_HSCROLL
|WS_VSCROLL
),
673 FALSE
, GetWindowLongW( data
->hwnd
, GWL_EXSTYLE
) );
675 rect
->left
-= rc
.left
;
676 rect
->right
-= rc
.right
;
678 rect
->bottom
-= rc
.bottom
;
679 if (rect
->top
>= rect
->bottom
) rect
->bottom
= rect
->top
+ 1;
680 if (rect
->left
>= rect
->right
) rect
->right
= rect
->left
+ 1;
684 /***********************************************************************
685 * X11DRV_X_to_window_rect
687 * Opposite of X11DRV_window_to_X_rect
689 void X11DRV_X_to_window_rect( struct x11drv_win_data
*data
, RECT
*rect
)
691 if (!data
->managed
) return;
692 if (IsRectEmpty( rect
)) return;
694 AdjustWindowRectEx( rect
, GetWindowLongW( data
->hwnd
, GWL_STYLE
) & ~(WS_HSCROLL
|WS_VSCROLL
),
695 FALSE
, GetWindowLongW( data
->hwnd
, GWL_EXSTYLE
));
697 if (rect
->top
>= rect
->bottom
) rect
->bottom
= rect
->top
+ 1;
698 if (rect
->left
>= rect
->right
) rect
->right
= rect
->left
+ 1;
702 /***********************************************************************
703 * X11DRV_sync_window_position
705 * Synchronize the X window position with the Windows one
707 void X11DRV_sync_window_position( Display
*display
, struct x11drv_win_data
*data
,
708 UINT swp_flags
, const RECT
*new_client_rect
,
709 const RECT
*new_whole_rect
)
711 XWindowChanges changes
;
715 old_whole_rect
= data
->whole_rect
;
716 data
->whole_rect
= *new_whole_rect
;
718 data
->client_rect
= *new_client_rect
;
719 OffsetRect( &data
->client_rect
, -data
->whole_rect
.left
, -data
->whole_rect
.top
);
721 if (!data
->whole_window
|| data
->lock_changes
) return;
723 mask
= get_window_changes( &changes
, &old_whole_rect
, &data
->whole_rect
);
725 if (!(swp_flags
& SWP_NOZORDER
))
727 /* find window that this one must be after */
728 HWND prev
= GetWindow( data
->hwnd
, GW_HWNDPREV
);
729 while (prev
&& !(GetWindowLongW( prev
, GWL_STYLE
) & WS_VISIBLE
))
730 prev
= GetWindow( prev
, GW_HWNDPREV
);
731 if (!prev
) /* top child */
733 changes
.stack_mode
= Above
;
738 /* should use stack_mode Below but most window managers don't get it right */
739 /* so move it above the next one in Z order */
740 HWND next
= GetWindow( data
->hwnd
, GW_HWNDNEXT
);
741 while (next
&& !(GetWindowLongW( next
, GWL_STYLE
) & WS_VISIBLE
))
742 next
= GetWindow( next
, GW_HWNDNEXT
);
745 changes
.stack_mode
= Above
;
746 changes
.sibling
= X11DRV_get_whole_window(next
);
747 mask
|= CWStackMode
| CWSibling
;
754 DWORD style
= GetWindowLongW( data
->hwnd
, GWL_STYLE
);
756 TRACE( "setting win %lx pos %d,%d,%dx%d after %lx changes=%x\n",
757 data
->whole_window
, data
->whole_rect
.left
, data
->whole_rect
.top
,
758 data
->whole_rect
.right
- data
->whole_rect
.left
,
759 data
->whole_rect
.bottom
- data
->whole_rect
.top
, changes
.sibling
, mask
);
762 if (mask
& (CWWidth
|CWHeight
)) set_size_hints( display
, data
, style
);
763 if (mask
& CWX
) changes
.x
-= virtual_screen_rect
.left
;
764 if (mask
& CWY
) changes
.y
-= virtual_screen_rect
.top
;
765 XReconfigureWMWindow( display
, data
->whole_window
,
766 DefaultScreen(display
), mask
, &changes
);
772 /**********************************************************************
773 * create_whole_window
775 * Create the whole X window for a given window
777 static Window
create_whole_window( Display
*display
, struct x11drv_win_data
*data
, DWORD style
)
780 XSetWindowAttributes attr
;
783 if (!(cx
= data
->window_rect
.right
- data
->window_rect
.left
)) cx
= 1;
784 if (!(cy
= data
->window_rect
.bottom
- data
->window_rect
.top
)) cy
= 1;
786 mask
= get_window_attributes( display
, data
, &attr
);
790 data
->whole_rect
= data
->window_rect
;
791 data
->whole_window
= XCreateWindow( display
, root_window
,
792 data
->window_rect
.left
- virtual_screen_rect
.left
,
793 data
->window_rect
.top
- virtual_screen_rect
.top
,
794 cx
, cy
, 0, screen_depth
, InputOutput
,
795 visual
, mask
, &attr
);
797 if (!data
->whole_window
)
802 XSaveContext( display
, data
->whole_window
, winContext
, (char *)data
->hwnd
);
804 /* non-maximized child must be at bottom of Z order */
805 if ((style
& (WS_CHILD
|WS_MAXIMIZE
)) == WS_CHILD
)
807 XWindowChanges changes
;
808 changes
.stack_mode
= Below
;
809 XConfigureWindow( display
, data
->whole_window
, CWStackMode
, &changes
);
813 xim
= x11drv_thread_data()->xim
;
814 if (xim
) data
->xic
= X11DRV_CreateIC( xim
, display
, data
->whole_window
);
816 set_initial_wm_hints( display
, data
);
817 X11DRV_set_wm_hints( display
, data
);
819 SetPropA( data
->hwnd
, whole_window_prop
, (HANDLE
)data
->whole_window
);
820 return data
->whole_window
;
824 /**********************************************************************
825 * destroy_whole_window
827 * Destroy the whole X window for a given window.
829 static void destroy_whole_window( Display
*display
, struct x11drv_win_data
*data
)
831 struct x11drv_thread_data
*thread_data
= x11drv_thread_data();
833 if (!data
->whole_window
) return;
835 TRACE( "win %p xwin %lx\n", data
->hwnd
, data
->whole_window
);
836 if (thread_data
->cursor_window
== data
->whole_window
) thread_data
->cursor_window
= None
;
838 XDeleteContext( display
, data
->whole_window
, winContext
);
839 if (data
->whole_window
!= DefaultRootWindow(display
))
840 XDestroyWindow( display
, data
->whole_window
);
841 data
->whole_window
= 0;
844 XUnsetICFocus( data
->xic
);
845 XDestroyIC( data
->xic
);
847 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
850 RemovePropA( data
->hwnd
, whole_window_prop
);
854 /*****************************************************************
855 * SetWindowText (X11DRV.@)
857 void X11DRV_SetWindowText( HWND hwnd
, LPCWSTR text
)
859 Display
*display
= thread_display();
866 if ((win
= X11DRV_get_whole_window( hwnd
)) && win
!= DefaultRootWindow(display
))
868 /* allocate new buffer for window text */
869 count
= WideCharToMultiByte(CP_UNIXCP
, 0, text
, -1, NULL
, 0, NULL
, NULL
);
870 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, count
)))
872 ERR("Not enough memory for window text\n");
875 WideCharToMultiByte(CP_UNIXCP
, 0, text
, -1, buffer
, count
, NULL
, NULL
);
877 count
= WideCharToMultiByte(CP_UTF8
, 0, text
, strlenW(text
), NULL
, 0, NULL
, NULL
);
878 if (!(utf8_buffer
= HeapAlloc( GetProcessHeap(), 0, count
)))
880 ERR("Not enough memory for window text in UTF-8\n");
881 HeapFree( GetProcessHeap(), 0, buffer
);
884 WideCharToMultiByte(CP_UTF8
, 0, text
, strlenW(text
), utf8_buffer
, count
, NULL
, NULL
);
887 if (XmbTextListToTextProperty( display
, &buffer
, 1, XStdICCTextStyle
, &prop
) == Success
)
889 XSetWMName( display
, win
, &prop
);
890 XSetWMIconName( display
, win
, &prop
);
894 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
895 according to the standard
896 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
898 XChangeProperty( display
, win
, x11drv_atom(_NET_WM_NAME
), x11drv_atom(UTF8_STRING
),
899 8, PropModeReplace
, (unsigned char *) utf8_buffer
, count
);
902 HeapFree( GetProcessHeap(), 0, utf8_buffer
);
903 HeapFree( GetProcessHeap(), 0, buffer
);
908 /***********************************************************************
909 * DestroyWindow (X11DRV.@)
911 void X11DRV_DestroyWindow( HWND hwnd
)
913 struct x11drv_thread_data
*thread_data
= x11drv_thread_data();
914 Display
*display
= thread_data
->display
;
915 struct x11drv_win_data
*data
;
917 if (!(data
= X11DRV_get_win_data( hwnd
))) return;
919 free_window_dce( data
);
920 destroy_whole_window( display
, data
);
921 destroy_icon_window( display
, data
);
923 if (thread_data
->last_focus
== hwnd
) thread_data
->last_focus
= 0;
924 if (data
->hWMIconBitmap
) DeleteObject( data
->hWMIconBitmap
);
925 if (data
->hWMIconMask
) DeleteObject( data
->hWMIconMask
);
927 XDeleteContext( display
, (XID
)hwnd
, win_data_context
);
928 XFree( data
->wm_hints
);
930 HeapFree( GetProcessHeap(), 0, data
);
934 static struct x11drv_win_data
*alloc_win_data( Display
*display
, HWND hwnd
)
936 struct x11drv_win_data
*data
;
938 if ((data
= HeapAlloc(GetProcessHeap(), 0, sizeof(*data
))))
941 data
->whole_window
= 0;
942 data
->icon_window
= 0;
944 data
->managed
= FALSE
;
946 data
->lock_changes
= 0;
947 data
->hWMIconBitmap
= 0;
948 data
->hWMIconMask
= 0;
951 if (!winContext
) winContext
= XUniqueContext();
952 if (!win_data_context
) win_data_context
= XUniqueContext();
953 XSaveContext( display
, (XID
)hwnd
, win_data_context
, (char *)data
);
954 data
->wm_hints
= XAllocWMHints();
961 /* fill in the desktop X window id in the x11drv_win_data structure */
962 static void get_desktop_xwin( Display
*display
, struct x11drv_win_data
*data
)
964 Window win
= (Window
)GetPropA( data
->hwnd
, whole_window_prop
);
968 unsigned int width
, height
;
970 /* retrieve the real size of the desktop */
971 SERVER_START_REQ( get_window_rectangles
)
973 req
->handle
= data
->hwnd
;
974 wine_server_call( req
);
975 width
= reply
->window
.right
- reply
->window
.left
;
976 height
= reply
->window
.bottom
- reply
->window
.top
;
979 data
->whole_window
= win
;
980 if (win
!= root_window
) X11DRV_init_desktop( win
, width
, height
);
987 visualid
= XVisualIDFromVisual(visual
);
989 SetPropA( data
->hwnd
, whole_window_prop
, (HANDLE
)root_window
);
990 SetPropA( data
->hwnd
, visual_id_prop
, (HANDLE
)visualid
);
991 data
->whole_window
= root_window
;
992 X11DRV_SetWindowPos( data
->hwnd
, 0, &virtual_screen_rect
, &virtual_screen_rect
,
993 SWP_NOZORDER
| SWP_NOACTIVATE
, NULL
);
994 if (root_window
!= DefaultRootWindow( display
))
996 data
->managed
= TRUE
;
997 SetPropA( data
->hwnd
, managed_prop
, (HANDLE
)1 );
1002 /**********************************************************************
1003 * CreateDesktopWindow (X11DRV.@)
1005 BOOL
X11DRV_CreateDesktopWindow( HWND hwnd
)
1007 Display
*display
= thread_display();
1008 struct x11drv_win_data
*data
;
1010 if (!(data
= alloc_win_data( display
, hwnd
))) return FALSE
;
1012 get_desktop_xwin( display
, data
);
1018 /**********************************************************************
1019 * CreateWindow (X11DRV.@)
1021 BOOL
X11DRV_CreateWindow( HWND hwnd
, CREATESTRUCTA
*cs
, BOOL unicode
)
1023 Display
*display
= thread_display();
1025 struct x11drv_win_data
*data
;
1029 CBT_CREATEWNDA cbtc
;
1033 if (!(data
= alloc_win_data( display
, hwnd
))) return FALSE
;
1037 ERR( "invalid window width %d\n", cs
->cx
);
1042 ERR( "invalid window height %d\n", cs
->cy
);
1047 ERR( "invalid window width %d\n", cs
->cx
);
1052 ERR( "invalid window height %d\n", cs
->cy
);
1056 /* initialize the dimensions before sending WM_GETMINMAXINFO */
1057 SetRect( &rect
, cs
->x
, cs
->y
, cs
->x
+ cs
->cx
, cs
->y
+ cs
->cy
);
1058 X11DRV_SetWindowPos( hwnd
, 0, &rect
, &rect
, SWP_NOZORDER
| SWP_NOACTIVATE
, NULL
);
1060 /* create an X window if it's a top level window */
1061 if (GetAncestor( hwnd
, GA_PARENT
) == GetDesktopWindow())
1063 if (!create_whole_window( display
, data
, cs
->style
)) goto failed
;
1065 else if (hwnd
== GetDesktopWindow())
1067 get_desktop_xwin( display
, data
);
1070 /* get class or window DC if needed */
1071 alloc_window_dce( data
);
1073 /* Call the WH_CBT hook */
1075 /* the window style passed to the hook must be the real window style,
1076 * rather than just the window style that the caller to CreateWindowEx
1077 * passed in, so we have to copy the original CREATESTRUCT and get the
1078 * the real style. */
1080 cbcs
.style
= GetWindowLongW(hwnd
, GWL_STYLE
);
1083 cbtc
.hwndInsertAfter
= HWND_TOP
;
1084 if (HOOK_CallHooks( WH_CBT
, HCBT_CREATEWND
, (WPARAM
)hwnd
, (LPARAM
)&cbtc
, unicode
))
1086 TRACE("CBT-hook returned !0\n");
1090 /* Send the WM_GETMINMAXINFO message and fix the size if needed */
1091 if ((cs
->style
& WS_THICKFRAME
) || !(cs
->style
& (WS_POPUP
| WS_CHILD
)))
1093 POINT maxSize
, maxPos
, minTrack
, maxTrack
;
1095 WINPOS_GetMinMaxInfo( hwnd
, &maxSize
, &maxPos
, &minTrack
, &maxTrack
);
1096 if (maxTrack
.x
< cs
->cx
) cs
->cx
= maxTrack
.x
;
1097 if (maxTrack
.y
< cs
->cy
) cs
->cy
= maxTrack
.y
;
1098 if (cs
->cx
< 0) cs
->cx
= 0;
1099 if (cs
->cy
< 0) cs
->cy
= 0;
1101 SetRect( &rect
, cs
->x
, cs
->y
, cs
->x
+ cs
->cx
, cs
->y
+ cs
->cy
);
1102 if (!X11DRV_SetWindowPos( hwnd
, 0, &rect
, &rect
, SWP_NOZORDER
| SWP_NOACTIVATE
, NULL
))
1106 /* send WM_NCCREATE */
1107 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd
, cs
->x
, cs
->y
, cs
->cx
, cs
->cy
);
1109 ret
= SendMessageW( hwnd
, WM_NCCREATE
, 0, (LPARAM
)cs
);
1111 ret
= SendMessageA( hwnd
, WM_NCCREATE
, 0, (LPARAM
)cs
);
1114 WARN("aborted by WM_xxCREATE!\n");
1118 /* make sure the window is still valid */
1119 if (!(data
= X11DRV_get_win_data( hwnd
))) return FALSE
;
1120 if (data
->whole_window
) X11DRV_sync_window_style( display
, data
);
1122 /* send WM_NCCALCSIZE */
1123 rect
= data
->window_rect
;
1124 SendMessageW( hwnd
, WM_NCCALCSIZE
, FALSE
, (LPARAM
)&rect
);
1126 if (!(wndPtr
= WIN_GetPtr(hwnd
))) return FALSE
;
1128 /* yes, even if the CBT hook was called with HWND_TOP */
1129 insert_after
= (wndPtr
->dwStyle
& WS_CHILD
) ? HWND_BOTTOM
: HWND_TOP
;
1131 X11DRV_SetWindowPos( hwnd
, insert_after
, &wndPtr
->rectWindow
, &rect
, SWP_NOACTIVATE
, NULL
);
1133 TRACE( "win %p window %d,%d,%d,%d client %d,%d,%d,%d whole %d,%d,%d,%d X client %d,%d,%d,%d xwin %x\n",
1134 hwnd
, wndPtr
->rectWindow
.left
, wndPtr
->rectWindow
.top
,
1135 wndPtr
->rectWindow
.right
, wndPtr
->rectWindow
.bottom
,
1136 wndPtr
->rectClient
.left
, wndPtr
->rectClient
.top
,
1137 wndPtr
->rectClient
.right
, wndPtr
->rectClient
.bottom
,
1138 data
->whole_rect
.left
, data
->whole_rect
.top
,
1139 data
->whole_rect
.right
, data
->whole_rect
.bottom
,
1140 data
->client_rect
.left
, data
->client_rect
.top
,
1141 data
->client_rect
.right
, data
->client_rect
.bottom
,
1142 (unsigned int)data
->whole_window
);
1144 WIN_ReleasePtr( wndPtr
);
1147 ret
= (SendMessageW( hwnd
, WM_CREATE
, 0, (LPARAM
)cs
) != -1);
1149 ret
= (SendMessageA( hwnd
, WM_CREATE
, 0, (LPARAM
)cs
) != -1);
1151 if (!ret
) return FALSE
;
1153 NotifyWinEvent(EVENT_OBJECT_CREATE
, hwnd
, OBJID_WINDOW
, 0);
1155 /* Send the size messages */
1157 if (!(wndPtr
= WIN_GetPtr(hwnd
)) || wndPtr
== WND_OTHER_PROCESS
) return FALSE
;
1158 if (!(wndPtr
->flags
& WIN_NEED_SIZE
))
1160 RECT rect
= wndPtr
->rectClient
;
1161 WIN_ReleasePtr( wndPtr
);
1162 /* send it anyway */
1163 if (((rect
.right
-rect
.left
) <0) ||((rect
.bottom
-rect
.top
)<0))
1164 WARN("sending bogus WM_SIZE message 0x%08x\n",
1165 MAKELONG(rect
.right
-rect
.left
, rect
.bottom
-rect
.top
));
1166 SendMessageW( hwnd
, WM_SIZE
, SIZE_RESTORED
,
1167 MAKELONG(rect
.right
-rect
.left
, rect
.bottom
-rect
.top
));
1168 SendMessageW( hwnd
, WM_MOVE
, 0, MAKELONG( rect
.left
, rect
.top
) );
1170 else WIN_ReleasePtr( wndPtr
);
1172 /* Show the window, maximizing or minimizing if needed */
1174 style
= GetWindowLongW( hwnd
, GWL_STYLE
);
1175 if (style
& (WS_MINIMIZE
| WS_MAXIMIZE
))
1177 extern UINT
WINPOS_MinMaximize( HWND hwnd
, UINT cmd
, LPRECT rect
); /*FIXME*/
1180 UINT swFlag
= (style
& WS_MINIMIZE
) ? SW_MINIMIZE
: SW_MAXIMIZE
;
1181 WIN_SetStyle( hwnd
, 0, WS_MAXIMIZE
| WS_MINIMIZE
);
1182 swFlag
= WINPOS_MinMaximize( hwnd
, swFlag
, &newPos
);
1184 swFlag
|= SWP_FRAMECHANGED
; /* Frame always gets changed */
1185 if (!(style
& WS_VISIBLE
) || (style
& WS_CHILD
) || GetActiveWindow()) swFlag
|= SWP_NOACTIVATE
;
1187 SetWindowPos( hwnd
, 0, newPos
.left
, newPos
.top
,
1188 newPos
.right
, newPos
.bottom
, swFlag
);
1191 /* Dock system tray windows. */
1192 /* Dock after the window is created so we don't have problems calling
1194 if (GetWindowLongW( hwnd
, GWL_EXSTYLE
) & WS_EX_TRAYWINDOW
)
1195 systray_dock_window( display
, data
);
1200 X11DRV_DestroyWindow( hwnd
);
1205 /***********************************************************************
1206 * X11DRV_get_win_data
1208 * Return the X11 data structure associated with a window.
1210 struct x11drv_win_data
*X11DRV_get_win_data( HWND hwnd
)
1214 if (!hwnd
|| XFindContext( thread_display(), (XID
)hwnd
, win_data_context
, &data
)) data
= NULL
;
1215 return (struct x11drv_win_data
*)data
;
1219 /***********************************************************************
1220 * X11DRV_get_whole_window
1222 * Return the X window associated with the full area of a window
1224 Window
X11DRV_get_whole_window( HWND hwnd
)
1226 struct x11drv_win_data
*data
= X11DRV_get_win_data( hwnd
);
1228 if (!data
) return (Window
)GetPropA( hwnd
, whole_window_prop
);
1229 return data
->whole_window
;
1233 /***********************************************************************
1236 * Return the X input context associated with a window
1238 XIC
X11DRV_get_ic( HWND hwnd
)
1240 struct x11drv_win_data
*data
= X11DRV_get_win_data( hwnd
);
1242 if (!data
) return 0;
1247 /*****************************************************************
1248 * SetParent (X11DRV.@)
1250 void X11DRV_SetParent( HWND hwnd
, HWND parent
, HWND old_parent
)
1252 Display
*display
= thread_display();
1253 struct x11drv_win_data
*data
= X11DRV_get_win_data( hwnd
);
1256 if (parent
== old_parent
) return;
1258 if (parent
!= GetDesktopWindow()) /* a child window */
1260 if (old_parent
== GetDesktopWindow())
1262 /* destroy the old X windows */
1263 destroy_whole_window( display
, data
);
1264 destroy_icon_window( display
, data
);
1267 data
->managed
= FALSE
;
1268 RemovePropA( data
->hwnd
, managed_prop
);
1272 else /* new top level window */
1274 /* FIXME: we ignore errors since we can't really recover anyway */
1275 create_whole_window( display
, data
, GetWindowLongW( hwnd
, GWL_STYLE
) );
1280 /*****************************************************************
1281 * SetFocus (X11DRV.@)
1284 * Explicit colormap management seems to work only with OLVWM.
1286 void X11DRV_SetFocus( HWND hwnd
)
1288 Display
*display
= thread_display();
1289 struct x11drv_win_data
*data
;
1290 XWindowAttributes win_attr
;
1292 /* Only mess with the X focus if there's */
1293 /* no desktop window and if the window is not managed by the WM. */
1294 if (root_window
!= DefaultRootWindow(display
)) return;
1296 if (!hwnd
) /* If setting the focus to 0, uninstall the colormap */
1299 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_PRIVATE
)
1300 XUninstallColormap( display
, X11DRV_PALETTE_PaletteXColormap
);
1301 wine_tsx11_unlock();
1305 hwnd
= GetAncestor( hwnd
, GA_ROOT
);
1307 if (!(data
= X11DRV_get_win_data( hwnd
))) return;
1308 if (data
->managed
|| !data
->whole_window
) return;
1310 /* Set X focus and install colormap */
1312 if (XGetWindowAttributes( display
, data
->whole_window
, &win_attr
) &&
1313 (win_attr
.map_state
== IsViewable
))
1315 /* If window is not viewable, don't change anything */
1317 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1318 /* FIXME: this is not entirely correct */
1319 XSetInputFocus( display
, data
->whole_window
, RevertToParent
,
1321 GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1322 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_PRIVATE
)
1323 XInstallColormap( display
, X11DRV_PALETTE_PaletteXColormap
);
1325 wine_tsx11_unlock();
1329 /**********************************************************************
1330 * SetWindowIcon (X11DRV.@)
1332 * hIcon or hIconSm has changed (or is being initialised for the
1333 * first time). Complete the X11 driver-specific initialisation
1334 * and set the window hints.
1336 * This is not entirely correct, may need to create
1337 * an icon window and set the pixmap as a background
1339 void X11DRV_SetWindowIcon( HWND hwnd
, UINT type
, HICON icon
)
1341 Display
*display
= thread_display();
1342 struct x11drv_win_data
*data
;
1344 if (type
!= ICON_BIG
) return; /* nothing to do here */
1346 if (!(data
= X11DRV_get_win_data( hwnd
))) return;
1347 if (!data
->whole_window
) return;
1348 if (!data
->managed
) return;
1352 set_icon_hints( display
, data
, icon
);
1354 XSetWMHints( display
, data
->whole_window
, data
->wm_hints
);
1355 wine_tsx11_unlock();