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
, UINT swp_flags
, 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 /* activated windows are managed */
85 if (!(swp_flags
& (SWP_NOACTIVATE
|SWP_HIDEWINDOW
))) return TRUE
;
86 if (hwnd
== GetActiveWindow()) return TRUE
;
87 /* windows with caption are managed */
88 if ((style
& WS_CAPTION
) == WS_CAPTION
) return TRUE
;
89 /* tool windows are not managed */
90 if (ex_style
& WS_EX_TOOLWINDOW
) return FALSE
;
91 /* windows with thick frame are managed */
92 if (style
& WS_THICKFRAME
) return TRUE
;
93 /* application windows are managed */
94 if (ex_style
& WS_EX_APPWINDOW
) return TRUE
;
98 /* popup with sysmenu == caption are managed */
99 if (style
& WS_SYSMENU
) return TRUE
;
100 /* full-screen popup windows are managed */
101 if ((window_rect
->right
- window_rect
->left
) == screen_width
&&
102 (window_rect
->bottom
- window_rect
->top
) == screen_height
)
105 /* all other popups are not managed */
110 /* default: managed */
113 /* default: not managed */
120 /***********************************************************************
121 * X11DRV_is_window_rect_mapped
123 * Check if the X whole window should be mapped based on its rectangle
125 BOOL
X11DRV_is_window_rect_mapped( const RECT
*rect
)
127 /* don't map if rect is empty */
128 if (IsRectEmpty( rect
)) return FALSE
;
130 /* don't map if rect is off-screen */
131 if (rect
->left
>= virtual_screen_rect
.right
||
132 rect
->top
>= virtual_screen_rect
.bottom
||
133 rect
->right
<= virtual_screen_rect
.left
||
134 rect
->bottom
<= virtual_screen_rect
.top
)
141 /***********************************************************************
142 * get_window_attributes
144 * Fill the window attributes structure for an X window.
146 static int get_window_attributes( Display
*display
, struct x11drv_win_data
*data
,
147 XSetWindowAttributes
*attr
)
149 attr
->override_redirect
= !data
->managed
;
150 attr
->colormap
= X11DRV_PALETTE_PaletteXColormap
;
151 attr
->save_under
= ((GetClassLongW( data
->hwnd
, GCL_STYLE
) & CS_SAVEBITS
) != 0);
152 attr
->cursor
= x11drv_thread_data()->cursor
;
153 attr
->bit_gravity
= NorthWestGravity
;
154 attr
->backing_store
= NotUseful
;
155 attr
->event_mask
= (ExposureMask
| PointerMotionMask
|
156 ButtonPressMask
| ButtonReleaseMask
| EnterWindowMask
|
157 KeyPressMask
| KeyReleaseMask
| FocusChangeMask
| KeymapStateMask
);
158 if (data
->managed
) attr
->event_mask
|= StructureNotifyMask
;
160 return (CWOverrideRedirect
| CWSaveUnder
| CWColormap
| CWCursor
|
161 CWEventMask
| CWBitGravity
| CWBackingStore
);
165 /***********************************************************************
166 * X11DRV_sync_window_style
168 * Change the X window attributes when the window style has changed.
170 void X11DRV_sync_window_style( Display
*display
, struct x11drv_win_data
*data
)
172 if (data
->whole_window
!= root_window
)
174 XSetWindowAttributes attr
;
175 int mask
= get_window_attributes( display
, data
, &attr
);
178 XChangeWindowAttributes( display
, data
->whole_window
, mask
, &attr
);
184 /***********************************************************************
187 * fill the window changes structure
189 static int get_window_changes( XWindowChanges
*changes
, const RECT
*old
, const RECT
*new )
193 if (old
->right
- old
->left
!= new->right
- new->left
)
195 if (!(changes
->width
= new->right
- new->left
)) changes
->width
= 1;
198 if (old
->bottom
- old
->top
!= new->bottom
- new->top
)
200 if (!(changes
->height
= new->bottom
- new->top
)) changes
->height
= 1;
203 if (old
->left
!= new->left
)
205 changes
->x
= new->left
;
208 if (old
->top
!= new->top
)
210 changes
->y
= new->top
;
217 /***********************************************************************
220 static Window
create_icon_window( Display
*display
, struct x11drv_win_data
*data
)
222 XSetWindowAttributes attr
;
224 attr
.event_mask
= (ExposureMask
| KeyPressMask
| KeyReleaseMask
| PointerMotionMask
|
225 ButtonPressMask
| ButtonReleaseMask
| EnterWindowMask
);
226 attr
.bit_gravity
= NorthWestGravity
;
227 attr
.backing_store
= NotUseful
/*WhenMapped*/;
228 attr
.colormap
= X11DRV_PALETTE_PaletteXColormap
; /* Needed due to our visual */
231 data
->icon_window
= XCreateWindow( display
, root_window
, 0, 0,
232 GetSystemMetrics( SM_CXICON
),
233 GetSystemMetrics( SM_CYICON
),
236 CWEventMask
| CWBitGravity
| CWBackingStore
| CWColormap
, &attr
);
237 XSaveContext( display
, data
->icon_window
, winContext
, (char *)data
->hwnd
);
240 TRACE( "created %lx\n", data
->icon_window
);
241 SetPropA( data
->hwnd
, icon_window_prop
, (HANDLE
)data
->icon_window
);
242 return data
->icon_window
;
247 /***********************************************************************
248 * destroy_icon_window
250 static void destroy_icon_window( Display
*display
, struct x11drv_win_data
*data
)
252 if (!data
->icon_window
) return;
253 if (x11drv_thread_data()->cursor_window
== data
->icon_window
)
254 x11drv_thread_data()->cursor_window
= None
;
256 XDeleteContext( display
, data
->icon_window
, winContext
);
257 XDestroyWindow( display
, data
->icon_window
);
258 data
->icon_window
= 0;
260 RemovePropA( data
->hwnd
, icon_window_prop
);
264 /***********************************************************************
267 * Set the icon wm hints
269 static void set_icon_hints( Display
*display
, struct x11drv_win_data
*data
, HICON hIcon
)
271 XWMHints
*hints
= data
->wm_hints
;
273 if (data
->hWMIconBitmap
) DeleteObject( data
->hWMIconBitmap
);
274 if (data
->hWMIconMask
) DeleteObject( data
->hWMIconMask
);
275 data
->hWMIconBitmap
= 0;
276 data
->hWMIconMask
= 0;
280 destroy_icon_window( display
, data
);
281 hints
->flags
&= ~(IconPixmapHint
| IconMaskHint
| IconWindowHint
);
285 if (!data
->icon_window
) create_icon_window( display
, data
);
286 hints
->icon_window
= data
->icon_window
;
287 hints
->flags
= (hints
->flags
& ~(IconPixmapHint
| IconMaskHint
)) | IconWindowHint
;
297 GetIconInfo(hIcon
, &ii
);
299 GetObjectA(ii
.hbmMask
, sizeof(bmMask
), &bmMask
);
302 rcMask
.right
= bmMask
.bmWidth
;
303 rcMask
.bottom
= bmMask
.bmHeight
;
305 hDC
= CreateCompatibleDC(0);
306 hbmOrig
= SelectObject(hDC
, ii
.hbmMask
);
307 InvertRect(hDC
, &rcMask
);
308 SelectObject(hDC
, ii
.hbmColor
); /* force the color bitmap to x11drv mode too */
309 SelectObject(hDC
, hbmOrig
);
312 data
->hWMIconBitmap
= ii
.hbmColor
;
313 data
->hWMIconMask
= ii
.hbmMask
;
315 hints
->icon_pixmap
= X11DRV_get_pixmap(data
->hWMIconBitmap
);
316 hints
->icon_mask
= X11DRV_get_pixmap(data
->hWMIconMask
);
317 destroy_icon_window( display
, data
);
318 hints
->flags
= (hints
->flags
& ~IconWindowHint
) | IconPixmapHint
| IconMaskHint
;
322 /***********************************************************************
323 * systray_dock_window
325 * Docks the given X window with the NETWM system tray.
327 static void systray_dock_window( Display
*display
, struct x11drv_win_data
*data
)
329 static Atom systray_atom
;
330 Window systray_window
;
335 if (DefaultScreen( display
) == 0)
336 systray_atom
= x11drv_atom(_NET_SYSTEM_TRAY_S0
);
339 char systray_buffer
[29]; /* strlen(_NET_SYSTEM_TRAY_S4294967295)+1 */
340 sprintf( systray_buffer
, "_NET_SYSTEM_TRAY_S%u", DefaultScreen( display
) );
341 systray_atom
= XInternAtom( display
, systray_buffer
, False
);
344 systray_window
= XGetSelectionOwner( display
, systray_atom
);
347 TRACE("Docking tray icon %p\n", data
->hwnd
);
349 if (systray_window
!= None
)
352 unsigned long info
[2];
354 /* Put the window offscreen so it isn't mapped. The window _cannot_ be
355 * mapped if we intend to dock with an XEMBED tray. If the window is
356 * mapped when we dock, it may become visible as a child of the root
357 * window after it docks, which isn't the proper behavior.
359 * For more information on this problem, see
360 * http://standards.freedesktop.org/xembed-spec/latest/ar01s04.html */
362 SetWindowPos( data
->hwnd
, NULL
, virtual_screen_rect
.right
+ 1, virtual_screen_rect
.bottom
+ 1,
363 0, 0, SWP_NOZORDER
| SWP_NOSIZE
| SWP_NOACTIVATE
);
365 /* set XEMBED protocol data on the window */
366 info
[0] = 0; /* protocol version */
367 info
[1] = 1; /* mapped = true */
370 XChangeProperty( display
, data
->whole_window
,
371 x11drv_atom(_XEMBED_INFO
),
372 x11drv_atom(_XEMBED_INFO
), 32, PropModeReplace
,
373 (unsigned char*)info
, 2 );
376 /* send the docking request message */
377 ZeroMemory( &ev
, sizeof(ev
) );
378 ev
.xclient
.type
= ClientMessage
;
379 ev
.xclient
.window
= systray_window
;
380 ev
.xclient
.message_type
= x11drv_atom( _NET_SYSTEM_TRAY_OPCODE
);
381 ev
.xclient
.format
= 32;
382 ev
.xclient
.data
.l
[0] = CurrentTime
;
383 ev
.xclient
.data
.l
[1] = SYSTEM_TRAY_REQUEST_DOCK
;
384 ev
.xclient
.data
.l
[2] = data
->whole_window
;
385 ev
.xclient
.data
.l
[3] = 0;
386 ev
.xclient
.data
.l
[4] = 0;
389 XSendEvent( display
, systray_window
, False
, NoEventMask
, &ev
);
397 /* fall back to he KDE hints if the WM doesn't support XEMBED'ed
401 XChangeProperty( display
, data
->whole_window
,
402 x11drv_atom(KWM_DOCKWINDOW
),
403 x11drv_atom(KWM_DOCKWINDOW
), 32, PropModeReplace
,
404 (unsigned char*)&val
, 1 );
405 XChangeProperty( display
, data
->whole_window
,
406 x11drv_atom(_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR
),
407 XA_WINDOW
, 32, PropModeReplace
,
408 (unsigned char*)&data
->whole_window
, 1 );
414 /***********************************************************************
417 * set the window size hints
419 static void set_size_hints( Display
*display
, struct x11drv_win_data
*data
, DWORD style
)
421 XSizeHints
* size_hints
;
423 if ((size_hints
= XAllocSizeHints()))
425 size_hints
->flags
= 0;
427 if (data
->hwnd
!= GetDesktopWindow()) /* don't force position of desktop */
429 size_hints
->win_gravity
= StaticGravity
;
430 size_hints
->x
= data
->whole_rect
.left
;
431 size_hints
->y
= data
->whole_rect
.top
;
432 size_hints
->flags
|= PWinGravity
| PPosition
;
435 if ( !(style
& WS_THICKFRAME
) )
437 size_hints
->max_width
= data
->whole_rect
.right
- data
->whole_rect
.left
;
438 size_hints
->max_height
= data
->whole_rect
.bottom
- data
->whole_rect
.top
;
439 size_hints
->min_width
= size_hints
->max_width
;
440 size_hints
->min_height
= size_hints
->max_height
;
441 size_hints
->flags
|= PMinSize
| PMaxSize
;
443 XSetWMNormalHints( display
, data
->whole_window
, size_hints
);
449 /***********************************************************************
452 * get the name of the current process for setting class hints
454 static char *get_process_name(void)
460 WCHAR module
[MAX_PATH
];
461 DWORD len
= GetModuleFileNameW( 0, module
, MAX_PATH
);
462 if (len
&& len
< MAX_PATH
)
465 WCHAR
*p
, *appname
= module
;
467 if ((p
= strrchrW( appname
, '/' ))) appname
= p
+ 1;
468 if ((p
= strrchrW( appname
, '\\' ))) appname
= p
+ 1;
469 len
= WideCharToMultiByte( CP_UNIXCP
, 0, appname
, -1, NULL
, 0, NULL
, NULL
);
470 if ((ptr
= HeapAlloc( GetProcessHeap(), 0, len
)))
472 WideCharToMultiByte( CP_UNIXCP
, 0, appname
, -1, ptr
, len
, NULL
, NULL
);
481 /***********************************************************************
482 * set_initial_wm_hints
484 * Set the window manager hints that don't change over the lifetime of a window.
486 static void set_initial_wm_hints( Display
*display
, struct x11drv_win_data
*data
)
491 XClassHint
*class_hints
;
492 char *process_name
= get_process_name();
498 protocols
[i
++] = x11drv_atom(WM_DELETE_WINDOW
);
499 protocols
[i
++] = x11drv_atom(_NET_WM_PING
);
500 if (use_take_focus
) protocols
[i
++] = x11drv_atom(WM_TAKE_FOCUS
);
501 XChangeProperty( display
, data
->whole_window
, x11drv_atom(WM_PROTOCOLS
),
502 XA_ATOM
, 32, PropModeReplace
, (unsigned char *)protocols
, i
);
505 if ((class_hints
= XAllocClassHint()))
507 static char wine
[] = "Wine";
509 class_hints
->res_name
= process_name
;
510 class_hints
->res_class
= wine
;
511 XSetClassHint( display
, data
->whole_window
, class_hints
);
512 XFree( class_hints
);
515 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
516 XSetWMProperties(display
, data
->whole_window
, NULL
, NULL
, NULL
, 0, NULL
, NULL
, NULL
);
517 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
519 XChangeProperty(display
, data
->whole_window
, x11drv_atom(_NET_WM_PID
),
520 XA_CARDINAL
, 32, PropModeReplace
, (unsigned char *)&i
, 1);
522 XChangeProperty( display
, data
->whole_window
, x11drv_atom(XdndAware
),
523 XA_ATOM
, 32, PropModeReplace
, (unsigned char*)&dndVersion
, 1 );
529 /***********************************************************************
530 * X11DRV_set_wm_hints
532 * Set the window manager hints for a newly-created window
534 void X11DRV_set_wm_hints( Display
*display
, struct x11drv_win_data
*data
)
539 DWORD style
= GetWindowLongW( data
->hwnd
, GWL_STYLE
);
540 DWORD ex_style
= GetWindowLongW( data
->hwnd
, GWL_EXSTYLE
);
541 HWND owner
= GetWindow( data
->hwnd
, GW_OWNER
);
543 if (data
->hwnd
== GetDesktopWindow())
545 if (data
->whole_window
== DefaultRootWindow(display
)) return;
546 /* force some styles for the desktop to get the correct decorations */
547 style
|= WS_CAPTION
| WS_SYSMENU
| WS_MINIMIZEBOX
;
551 /* transient for hint */
554 Window owner_win
= X11DRV_get_whole_window( owner
);
556 XSetTransientForHint( display
, data
->whole_window
, owner_win
);
558 group_leader
= owner_win
;
560 else group_leader
= data
->whole_window
;
565 set_size_hints( display
, data
, style
);
567 /* set the WM_WINDOW_TYPE */
568 window_type
= x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL
);
569 if (ex_style
& WS_EX_TOOLWINDOW
) window_type
= x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY
);
570 else if (style
& WS_THICKFRAME
) window_type
= x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL
);
571 else if (style
& WS_DLGFRAME
) window_type
= x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG
);
572 else if (ex_style
& WS_EX_DLGMODALFRAME
) window_type
= x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG
);
574 XChangeProperty(display
, data
->whole_window
, x11drv_atom(_NET_WM_WINDOW_TYPE
),
575 XA_ATOM
, 32, PropModeReplace
, (unsigned char*)&window_type
, 1);
577 mwm_hints
.flags
= MWM_HINTS_FUNCTIONS
| MWM_HINTS_DECORATIONS
;
578 mwm_hints
.functions
= MWM_FUNC_MOVE
;
579 if (style
& WS_THICKFRAME
) mwm_hints
.functions
|= MWM_FUNC_RESIZE
;
580 if (style
& WS_MINIMIZEBOX
) mwm_hints
.functions
|= MWM_FUNC_MINIMIZE
;
581 if (style
& WS_MAXIMIZEBOX
) mwm_hints
.functions
|= MWM_FUNC_MAXIMIZE
;
582 if (style
& WS_SYSMENU
) mwm_hints
.functions
|= MWM_FUNC_CLOSE
;
583 mwm_hints
.decorations
= 0;
584 if ((style
& WS_CAPTION
) == WS_CAPTION
)
586 mwm_hints
.decorations
|= MWM_DECOR_TITLE
;
587 if (style
& WS_SYSMENU
) mwm_hints
.decorations
|= MWM_DECOR_MENU
;
588 if (style
& WS_MINIMIZEBOX
) mwm_hints
.decorations
|= MWM_DECOR_MINIMIZE
;
589 if (style
& WS_MAXIMIZEBOX
) mwm_hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
591 if (ex_style
& WS_EX_DLGMODALFRAME
) mwm_hints
.decorations
|= MWM_DECOR_BORDER
;
592 else if (style
& WS_THICKFRAME
) mwm_hints
.decorations
|= MWM_DECOR_BORDER
| MWM_DECOR_RESIZEH
;
593 else if ((style
& (WS_DLGFRAME
|WS_BORDER
)) == WS_DLGFRAME
) mwm_hints
.decorations
|= MWM_DECOR_BORDER
;
594 else if (style
& WS_BORDER
) mwm_hints
.decorations
|= MWM_DECOR_BORDER
;
595 else if (!(style
& (WS_CHILD
|WS_POPUP
))) mwm_hints
.decorations
|= MWM_DECOR_BORDER
;
597 XChangeProperty( display
, data
->whole_window
, x11drv_atom(_MOTIF_WM_HINTS
),
598 x11drv_atom(_MOTIF_WM_HINTS
), 32, PropModeReplace
,
599 (unsigned char*)&mwm_hints
, sizeof(mwm_hints
)/sizeof(long) );
606 data
->wm_hints
->flags
= InputHint
| StateHint
| WindowGroupHint
;
607 data
->wm_hints
->input
= !(style
& WS_DISABLED
);
608 data
->wm_hints
->initial_state
= (style
& WS_MINIMIZE
) ? IconicState
: NormalState
;
609 data
->wm_hints
->window_group
= group_leader
;
610 set_icon_hints( display
, data
, (HICON
)GetClassLongPtrW( data
->hwnd
, GCLP_HICON
) );
613 XSetWMHints( display
, data
->whole_window
, data
->wm_hints
);
619 /***********************************************************************
620 * X11DRV_set_iconic_state
622 * Set the X11 iconic state according to the window style.
624 void X11DRV_set_iconic_state( HWND hwnd
)
626 Display
*display
= thread_display();
627 struct x11drv_win_data
*data
;
629 DWORD style
= GetWindowLongW( hwnd
, GWL_STYLE
);
630 BOOL iconic
= (style
& WS_MINIMIZE
) != 0;
632 if (!(data
= X11DRV_get_win_data( hwnd
))) return;
633 if (!data
->whole_window
|| data
->whole_window
== DefaultRootWindow(display
)) return;
635 GetWindowRect( hwnd
, &rect
);
641 data
->wm_hints
->flags
|= StateHint
| IconPositionHint
;
642 data
->wm_hints
->initial_state
= iconic
? IconicState
: NormalState
;
643 data
->wm_hints
->icon_x
= rect
.left
- virtual_screen_rect
.left
;
644 data
->wm_hints
->icon_y
= rect
.top
- virtual_screen_rect
.top
;
645 XSetWMHints( display
, data
->whole_window
, data
->wm_hints
);
648 if (style
& WS_VISIBLE
)
651 XIconifyWindow( display
, data
->whole_window
, DefaultScreen(display
) );
653 if (X11DRV_is_window_rect_mapped( &rect
))
654 XMapWindow( display
, data
->whole_window
);
661 /***********************************************************************
662 * X11DRV_window_to_X_rect
664 * Convert a rect from client to X window coordinates
666 void X11DRV_window_to_X_rect( struct x11drv_win_data
*data
, RECT
*rect
)
670 if (!data
->managed
) return;
671 if (IsRectEmpty( rect
)) return;
673 rc
.top
= rc
.bottom
= rc
.left
= rc
.right
= 0;
675 AdjustWindowRectEx( &rc
, GetWindowLongW( data
->hwnd
, GWL_STYLE
) & ~(WS_HSCROLL
|WS_VSCROLL
),
676 FALSE
, GetWindowLongW( data
->hwnd
, GWL_EXSTYLE
) );
678 rect
->left
-= rc
.left
;
679 rect
->right
-= rc
.right
;
681 rect
->bottom
-= rc
.bottom
;
682 if (rect
->top
>= rect
->bottom
) rect
->bottom
= rect
->top
+ 1;
683 if (rect
->left
>= rect
->right
) rect
->right
= rect
->left
+ 1;
687 /***********************************************************************
688 * X11DRV_X_to_window_rect
690 * Opposite of X11DRV_window_to_X_rect
692 void X11DRV_X_to_window_rect( struct x11drv_win_data
*data
, RECT
*rect
)
694 if (!data
->managed
) return;
695 if (IsRectEmpty( rect
)) return;
697 AdjustWindowRectEx( rect
, GetWindowLongW( data
->hwnd
, GWL_STYLE
) & ~(WS_HSCROLL
|WS_VSCROLL
),
698 FALSE
, GetWindowLongW( data
->hwnd
, GWL_EXSTYLE
));
700 if (rect
->top
>= rect
->bottom
) rect
->bottom
= rect
->top
+ 1;
701 if (rect
->left
>= rect
->right
) rect
->right
= rect
->left
+ 1;
705 /***********************************************************************
706 * X11DRV_sync_window_position
708 * Synchronize the X window position with the Windows one
710 void X11DRV_sync_window_position( Display
*display
, struct x11drv_win_data
*data
,
711 UINT swp_flags
, const RECT
*new_client_rect
,
712 const RECT
*new_whole_rect
)
714 XWindowChanges changes
;
718 old_whole_rect
= data
->whole_rect
;
719 data
->whole_rect
= *new_whole_rect
;
721 data
->client_rect
= *new_client_rect
;
722 OffsetRect( &data
->client_rect
, -data
->whole_rect
.left
, -data
->whole_rect
.top
);
724 if (!data
->whole_window
|| data
->lock_changes
) return;
726 mask
= get_window_changes( &changes
, &old_whole_rect
, &data
->whole_rect
);
728 if (!(swp_flags
& SWP_NOZORDER
))
730 /* find window that this one must be after */
731 HWND prev
= GetWindow( data
->hwnd
, GW_HWNDPREV
);
732 while (prev
&& !(GetWindowLongW( prev
, GWL_STYLE
) & WS_VISIBLE
))
733 prev
= GetWindow( prev
, GW_HWNDPREV
);
734 if (!prev
) /* top child */
736 changes
.stack_mode
= Above
;
741 /* should use stack_mode Below but most window managers don't get it right */
742 /* so move it above the next one in Z order */
743 HWND next
= GetWindow( data
->hwnd
, GW_HWNDNEXT
);
744 while (next
&& !(GetWindowLongW( next
, GWL_STYLE
) & WS_VISIBLE
))
745 next
= GetWindow( next
, GW_HWNDNEXT
);
748 changes
.stack_mode
= Above
;
749 changes
.sibling
= X11DRV_get_whole_window(next
);
750 mask
|= CWStackMode
| CWSibling
;
757 DWORD style
= GetWindowLongW( data
->hwnd
, GWL_STYLE
);
759 TRACE( "setting win %lx pos %d,%d,%dx%d after %lx changes=%x\n",
760 data
->whole_window
, data
->whole_rect
.left
, data
->whole_rect
.top
,
761 data
->whole_rect
.right
- data
->whole_rect
.left
,
762 data
->whole_rect
.bottom
- data
->whole_rect
.top
, changes
.sibling
, mask
);
765 if (mask
& (CWWidth
|CWHeight
)) set_size_hints( display
, data
, style
);
766 if (mask
& CWX
) changes
.x
-= virtual_screen_rect
.left
;
767 if (mask
& CWY
) changes
.y
-= virtual_screen_rect
.top
;
768 XReconfigureWMWindow( display
, data
->whole_window
,
769 DefaultScreen(display
), mask
, &changes
);
775 /**********************************************************************
776 * create_whole_window
778 * Create the whole X window for a given window
780 static Window
create_whole_window( Display
*display
, struct x11drv_win_data
*data
, DWORD style
)
783 XSetWindowAttributes attr
;
786 if (!(cx
= data
->window_rect
.right
- data
->window_rect
.left
)) cx
= 1;
787 if (!(cy
= data
->window_rect
.bottom
- data
->window_rect
.top
)) cy
= 1;
789 mask
= get_window_attributes( display
, data
, &attr
);
793 data
->whole_rect
= data
->window_rect
;
794 data
->whole_window
= XCreateWindow( display
, root_window
,
795 data
->window_rect
.left
- virtual_screen_rect
.left
,
796 data
->window_rect
.top
- virtual_screen_rect
.top
,
797 cx
, cy
, 0, screen_depth
, InputOutput
,
798 visual
, mask
, &attr
);
800 if (!data
->whole_window
)
805 XSaveContext( display
, data
->whole_window
, winContext
, (char *)data
->hwnd
);
807 /* non-maximized child must be at bottom of Z order */
808 if ((style
& (WS_CHILD
|WS_MAXIMIZE
)) == WS_CHILD
)
810 XWindowChanges changes
;
811 changes
.stack_mode
= Below
;
812 XConfigureWindow( display
, data
->whole_window
, CWStackMode
, &changes
);
816 xim
= x11drv_thread_data()->xim
;
817 if (xim
) data
->xic
= X11DRV_CreateIC( xim
, display
, data
->whole_window
);
819 set_initial_wm_hints( display
, data
);
820 X11DRV_set_wm_hints( display
, data
);
822 SetPropA( data
->hwnd
, whole_window_prop
, (HANDLE
)data
->whole_window
);
823 return data
->whole_window
;
827 /**********************************************************************
828 * destroy_whole_window
830 * Destroy the whole X window for a given window.
832 static void destroy_whole_window( Display
*display
, struct x11drv_win_data
*data
)
834 struct x11drv_thread_data
*thread_data
= x11drv_thread_data();
836 if (!data
->whole_window
) return;
838 TRACE( "win %p xwin %lx\n", data
->hwnd
, data
->whole_window
);
839 if (thread_data
->cursor_window
== data
->whole_window
) thread_data
->cursor_window
= None
;
841 XDeleteContext( display
, data
->whole_window
, winContext
);
842 if (data
->whole_window
!= DefaultRootWindow(display
))
843 XDestroyWindow( display
, data
->whole_window
);
844 data
->whole_window
= 0;
847 XUnsetICFocus( data
->xic
);
848 XDestroyIC( data
->xic
);
850 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
853 RemovePropA( data
->hwnd
, whole_window_prop
);
857 /*****************************************************************
858 * SetWindowText (X11DRV.@)
860 void X11DRV_SetWindowText( HWND hwnd
, LPCWSTR text
)
862 Display
*display
= thread_display();
869 if ((win
= X11DRV_get_whole_window( hwnd
)) && win
!= DefaultRootWindow(display
))
871 /* allocate new buffer for window text */
872 count
= WideCharToMultiByte(CP_UNIXCP
, 0, text
, -1, NULL
, 0, NULL
, NULL
);
873 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, count
)))
875 ERR("Not enough memory for window text\n");
878 WideCharToMultiByte(CP_UNIXCP
, 0, text
, -1, buffer
, count
, NULL
, NULL
);
880 count
= WideCharToMultiByte(CP_UTF8
, 0, text
, strlenW(text
), NULL
, 0, NULL
, NULL
);
881 if (!(utf8_buffer
= HeapAlloc( GetProcessHeap(), 0, count
)))
883 ERR("Not enough memory for window text in UTF-8\n");
884 HeapFree( GetProcessHeap(), 0, buffer
);
887 WideCharToMultiByte(CP_UTF8
, 0, text
, strlenW(text
), utf8_buffer
, count
, NULL
, NULL
);
890 if (XmbTextListToTextProperty( display
, &buffer
, 1, XStdICCTextStyle
, &prop
) == Success
)
892 XSetWMName( display
, win
, &prop
);
893 XSetWMIconName( display
, win
, &prop
);
897 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
898 according to the standard
899 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
901 XChangeProperty( display
, win
, x11drv_atom(_NET_WM_NAME
), x11drv_atom(UTF8_STRING
),
902 8, PropModeReplace
, (unsigned char *) utf8_buffer
, count
);
905 HeapFree( GetProcessHeap(), 0, utf8_buffer
);
906 HeapFree( GetProcessHeap(), 0, buffer
);
911 /***********************************************************************
912 * DestroyWindow (X11DRV.@)
914 void X11DRV_DestroyWindow( HWND hwnd
)
916 struct x11drv_thread_data
*thread_data
= x11drv_thread_data();
917 Display
*display
= thread_data
->display
;
918 struct x11drv_win_data
*data
;
920 if (!(data
= X11DRV_get_win_data( hwnd
))) return;
922 free_window_dce( data
);
923 destroy_whole_window( display
, data
);
924 destroy_icon_window( display
, data
);
926 if (thread_data
->last_focus
== hwnd
) thread_data
->last_focus
= 0;
927 if (data
->hWMIconBitmap
) DeleteObject( data
->hWMIconBitmap
);
928 if (data
->hWMIconMask
) DeleteObject( data
->hWMIconMask
);
930 XDeleteContext( display
, (XID
)hwnd
, win_data_context
);
931 XFree( data
->wm_hints
);
933 HeapFree( GetProcessHeap(), 0, data
);
937 static struct x11drv_win_data
*alloc_win_data( Display
*display
, HWND hwnd
)
939 struct x11drv_win_data
*data
;
941 if ((data
= HeapAlloc(GetProcessHeap(), 0, sizeof(*data
))))
944 data
->whole_window
= 0;
945 data
->icon_window
= 0;
947 data
->managed
= FALSE
;
949 data
->lock_changes
= 0;
950 data
->hWMIconBitmap
= 0;
951 data
->hWMIconMask
= 0;
954 if (!winContext
) winContext
= XUniqueContext();
955 if (!win_data_context
) win_data_context
= XUniqueContext();
956 XSaveContext( display
, (XID
)hwnd
, win_data_context
, (char *)data
);
957 data
->wm_hints
= XAllocWMHints();
964 /* fill in the desktop X window id in the x11drv_win_data structure */
965 static void get_desktop_xwin( Display
*display
, struct x11drv_win_data
*data
)
967 Window win
= (Window
)GetPropA( data
->hwnd
, whole_window_prop
);
971 unsigned int width
, height
;
973 /* retrieve the real size of the desktop */
974 SERVER_START_REQ( get_window_rectangles
)
976 req
->handle
= data
->hwnd
;
977 wine_server_call( req
);
978 width
= reply
->window
.right
- reply
->window
.left
;
979 height
= reply
->window
.bottom
- reply
->window
.top
;
982 data
->whole_window
= win
;
983 if (win
!= root_window
) X11DRV_init_desktop( win
, width
, height
);
990 visualid
= XVisualIDFromVisual(visual
);
992 SetPropA( data
->hwnd
, whole_window_prop
, (HANDLE
)root_window
);
993 SetPropA( data
->hwnd
, visual_id_prop
, (HANDLE
)visualid
);
994 data
->whole_window
= root_window
;
995 X11DRV_SetWindowPos( data
->hwnd
, 0, &virtual_screen_rect
, &virtual_screen_rect
,
996 SWP_NOZORDER
| SWP_NOACTIVATE
, NULL
);
997 if (root_window
!= DefaultRootWindow( display
))
999 data
->managed
= TRUE
;
1000 SetPropA( data
->hwnd
, managed_prop
, (HANDLE
)1 );
1005 /**********************************************************************
1006 * CreateDesktopWindow (X11DRV.@)
1008 BOOL
X11DRV_CreateDesktopWindow( HWND hwnd
)
1010 Display
*display
= thread_display();
1011 struct x11drv_win_data
*data
;
1013 if (!(data
= alloc_win_data( display
, hwnd
))) return FALSE
;
1015 get_desktop_xwin( display
, data
);
1021 /**********************************************************************
1022 * CreateWindow (X11DRV.@)
1024 BOOL
X11DRV_CreateWindow( HWND hwnd
, CREATESTRUCTA
*cs
, BOOL unicode
)
1026 Display
*display
= thread_display();
1028 struct x11drv_win_data
*data
;
1032 CBT_CREATEWNDA cbtc
;
1036 if (!(data
= alloc_win_data( display
, hwnd
))) return FALSE
;
1040 ERR( "invalid window width %d\n", cs
->cx
);
1045 ERR( "invalid window height %d\n", cs
->cy
);
1050 ERR( "invalid window width %d\n", cs
->cx
);
1055 ERR( "invalid window height %d\n", cs
->cy
);
1059 /* initialize the dimensions before sending WM_GETMINMAXINFO */
1060 SetRect( &rect
, cs
->x
, cs
->y
, cs
->x
+ cs
->cx
, cs
->y
+ cs
->cy
);
1061 X11DRV_SetWindowPos( hwnd
, 0, &rect
, &rect
, SWP_NOZORDER
| SWP_NOACTIVATE
, NULL
);
1063 /* create an X window if it's a top level window */
1064 if (GetAncestor( hwnd
, GA_PARENT
) == GetDesktopWindow())
1066 if (!create_whole_window( display
, data
, cs
->style
)) goto failed
;
1068 else if (hwnd
== GetDesktopWindow())
1070 get_desktop_xwin( display
, data
);
1073 /* get class or window DC if needed */
1074 alloc_window_dce( data
);
1076 /* Call the WH_CBT hook */
1078 /* the window style passed to the hook must be the real window style,
1079 * rather than just the window style that the caller to CreateWindowEx
1080 * passed in, so we have to copy the original CREATESTRUCT and get the
1081 * the real style. */
1083 cbcs
.style
= GetWindowLongW(hwnd
, GWL_STYLE
);
1086 cbtc
.hwndInsertAfter
= HWND_TOP
;
1087 if (HOOK_CallHooks( WH_CBT
, HCBT_CREATEWND
, (WPARAM
)hwnd
, (LPARAM
)&cbtc
, unicode
))
1089 TRACE("CBT-hook returned !0\n");
1093 /* Send the WM_GETMINMAXINFO message and fix the size if needed */
1094 if ((cs
->style
& WS_THICKFRAME
) || !(cs
->style
& (WS_POPUP
| WS_CHILD
)))
1096 POINT maxSize
, maxPos
, minTrack
, maxTrack
;
1098 WINPOS_GetMinMaxInfo( hwnd
, &maxSize
, &maxPos
, &minTrack
, &maxTrack
);
1099 if (maxTrack
.x
< cs
->cx
) cs
->cx
= maxTrack
.x
;
1100 if (maxTrack
.y
< cs
->cy
) cs
->cy
= maxTrack
.y
;
1101 if (cs
->cx
< 0) cs
->cx
= 0;
1102 if (cs
->cy
< 0) cs
->cy
= 0;
1104 SetRect( &rect
, cs
->x
, cs
->y
, cs
->x
+ cs
->cx
, cs
->y
+ cs
->cy
);
1105 if (!X11DRV_SetWindowPos( hwnd
, 0, &rect
, &rect
, SWP_NOZORDER
| SWP_NOACTIVATE
, NULL
))
1109 /* send WM_NCCREATE */
1110 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd
, cs
->x
, cs
->y
, cs
->cx
, cs
->cy
);
1112 ret
= SendMessageW( hwnd
, WM_NCCREATE
, 0, (LPARAM
)cs
);
1114 ret
= SendMessageA( hwnd
, WM_NCCREATE
, 0, (LPARAM
)cs
);
1117 WARN("aborted by WM_xxCREATE!\n");
1121 /* make sure the window is still valid */
1122 if (!(data
= X11DRV_get_win_data( hwnd
))) return FALSE
;
1123 if (data
->whole_window
) X11DRV_sync_window_style( display
, data
);
1125 /* send WM_NCCALCSIZE */
1126 rect
= data
->window_rect
;
1127 SendMessageW( hwnd
, WM_NCCALCSIZE
, FALSE
, (LPARAM
)&rect
);
1129 if (!(wndPtr
= WIN_GetPtr(hwnd
))) return FALSE
;
1131 /* yes, even if the CBT hook was called with HWND_TOP */
1132 insert_after
= (wndPtr
->dwStyle
& WS_CHILD
) ? HWND_BOTTOM
: HWND_TOP
;
1134 X11DRV_SetWindowPos( hwnd
, insert_after
, &wndPtr
->rectWindow
, &rect
, SWP_NOACTIVATE
, NULL
);
1136 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",
1137 hwnd
, wndPtr
->rectWindow
.left
, wndPtr
->rectWindow
.top
,
1138 wndPtr
->rectWindow
.right
, wndPtr
->rectWindow
.bottom
,
1139 wndPtr
->rectClient
.left
, wndPtr
->rectClient
.top
,
1140 wndPtr
->rectClient
.right
, wndPtr
->rectClient
.bottom
,
1141 data
->whole_rect
.left
, data
->whole_rect
.top
,
1142 data
->whole_rect
.right
, data
->whole_rect
.bottom
,
1143 data
->client_rect
.left
, data
->client_rect
.top
,
1144 data
->client_rect
.right
, data
->client_rect
.bottom
,
1145 (unsigned int)data
->whole_window
);
1147 WIN_ReleasePtr( wndPtr
);
1150 ret
= (SendMessageW( hwnd
, WM_CREATE
, 0, (LPARAM
)cs
) != -1);
1152 ret
= (SendMessageA( hwnd
, WM_CREATE
, 0, (LPARAM
)cs
) != -1);
1154 if (!ret
) return FALSE
;
1156 NotifyWinEvent(EVENT_OBJECT_CREATE
, hwnd
, OBJID_WINDOW
, 0);
1158 /* Send the size messages */
1160 if (!(wndPtr
= WIN_GetPtr(hwnd
)) || wndPtr
== WND_OTHER_PROCESS
) return FALSE
;
1161 if (!(wndPtr
->flags
& WIN_NEED_SIZE
))
1163 RECT rect
= wndPtr
->rectClient
;
1164 WIN_ReleasePtr( wndPtr
);
1165 /* send it anyway */
1166 if (((rect
.right
-rect
.left
) <0) ||((rect
.bottom
-rect
.top
)<0))
1167 WARN("sending bogus WM_SIZE message 0x%08x\n",
1168 MAKELONG(rect
.right
-rect
.left
, rect
.bottom
-rect
.top
));
1169 SendMessageW( hwnd
, WM_SIZE
, SIZE_RESTORED
,
1170 MAKELONG(rect
.right
-rect
.left
, rect
.bottom
-rect
.top
));
1171 SendMessageW( hwnd
, WM_MOVE
, 0, MAKELONG( rect
.left
, rect
.top
) );
1173 else WIN_ReleasePtr( wndPtr
);
1175 /* Show the window, maximizing or minimizing if needed */
1177 style
= GetWindowLongW( hwnd
, GWL_STYLE
);
1178 if (style
& (WS_MINIMIZE
| WS_MAXIMIZE
))
1180 extern UINT
WINPOS_MinMaximize( HWND hwnd
, UINT cmd
, LPRECT rect
); /*FIXME*/
1183 UINT swFlag
= (style
& WS_MINIMIZE
) ? SW_MINIMIZE
: SW_MAXIMIZE
;
1184 WIN_SetStyle( hwnd
, 0, WS_MAXIMIZE
| WS_MINIMIZE
);
1185 swFlag
= WINPOS_MinMaximize( hwnd
, swFlag
, &newPos
);
1187 swFlag
|= SWP_FRAMECHANGED
; /* Frame always gets changed */
1188 if (!(style
& WS_VISIBLE
) || (style
& WS_CHILD
) || GetActiveWindow()) swFlag
|= SWP_NOACTIVATE
;
1190 SetWindowPos( hwnd
, 0, newPos
.left
, newPos
.top
,
1191 newPos
.right
, newPos
.bottom
, swFlag
);
1194 /* Dock system tray windows. */
1195 /* Dock after the window is created so we don't have problems calling
1197 if (GetWindowLongW( hwnd
, GWL_EXSTYLE
) & WS_EX_TRAYWINDOW
)
1198 systray_dock_window( display
, data
);
1203 X11DRV_DestroyWindow( hwnd
);
1208 /***********************************************************************
1209 * X11DRV_get_win_data
1211 * Return the X11 data structure associated with a window.
1213 struct x11drv_win_data
*X11DRV_get_win_data( HWND hwnd
)
1217 if (!hwnd
|| XFindContext( thread_display(), (XID
)hwnd
, win_data_context
, &data
)) data
= NULL
;
1218 return (struct x11drv_win_data
*)data
;
1222 /***********************************************************************
1223 * X11DRV_get_whole_window
1225 * Return the X window associated with the full area of a window
1227 Window
X11DRV_get_whole_window( HWND hwnd
)
1229 struct x11drv_win_data
*data
= X11DRV_get_win_data( hwnd
);
1231 if (!data
) return (Window
)GetPropA( hwnd
, whole_window_prop
);
1232 return data
->whole_window
;
1236 /***********************************************************************
1239 * Return the X input context associated with a window
1241 XIC
X11DRV_get_ic( HWND hwnd
)
1243 struct x11drv_win_data
*data
= X11DRV_get_win_data( hwnd
);
1245 if (!data
) return 0;
1250 /*****************************************************************
1251 * SetParent (X11DRV.@)
1253 void X11DRV_SetParent( HWND hwnd
, HWND parent
, HWND old_parent
)
1255 Display
*display
= thread_display();
1256 struct x11drv_win_data
*data
= X11DRV_get_win_data( hwnd
);
1259 if (parent
== old_parent
) return;
1261 if (parent
!= GetDesktopWindow()) /* a child window */
1263 if (old_parent
== GetDesktopWindow())
1265 /* destroy the old X windows */
1266 destroy_whole_window( display
, data
);
1267 destroy_icon_window( display
, data
);
1270 data
->managed
= FALSE
;
1271 RemovePropA( data
->hwnd
, managed_prop
);
1275 else /* new top level window */
1277 /* FIXME: we ignore errors since we can't really recover anyway */
1278 create_whole_window( display
, data
, GetWindowLongW( hwnd
, GWL_STYLE
) );
1283 /*****************************************************************
1284 * SetFocus (X11DRV.@)
1287 * Explicit colormap management seems to work only with OLVWM.
1289 void X11DRV_SetFocus( HWND hwnd
)
1291 Display
*display
= thread_display();
1292 struct x11drv_win_data
*data
;
1293 XWindowAttributes win_attr
;
1295 /* Only mess with the X focus if there's */
1296 /* no desktop window and if the window is not managed by the WM. */
1297 if (root_window
!= DefaultRootWindow(display
)) return;
1299 if (!hwnd
) /* If setting the focus to 0, uninstall the colormap */
1302 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_PRIVATE
)
1303 XUninstallColormap( display
, X11DRV_PALETTE_PaletteXColormap
);
1304 wine_tsx11_unlock();
1308 hwnd
= GetAncestor( hwnd
, GA_ROOT
);
1310 if (!(data
= X11DRV_get_win_data( hwnd
))) return;
1311 if (data
->managed
|| !data
->whole_window
) return;
1313 /* Set X focus and install colormap */
1315 if (XGetWindowAttributes( display
, data
->whole_window
, &win_attr
) &&
1316 (win_attr
.map_state
== IsViewable
))
1318 /* If window is not viewable, don't change anything */
1320 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1321 /* FIXME: this is not entirely correct */
1322 XSetInputFocus( display
, data
->whole_window
, RevertToParent
,
1324 GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1325 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_PRIVATE
)
1326 XInstallColormap( display
, X11DRV_PALETTE_PaletteXColormap
);
1328 wine_tsx11_unlock();
1332 /**********************************************************************
1333 * SetWindowIcon (X11DRV.@)
1335 * hIcon or hIconSm has changed (or is being initialised for the
1336 * first time). Complete the X11 driver-specific initialisation
1337 * and set the window hints.
1339 * This is not entirely correct, may need to create
1340 * an icon window and set the pixmap as a background
1342 void X11DRV_SetWindowIcon( HWND hwnd
, UINT type
, HICON icon
)
1344 Display
*display
= thread_display();
1345 struct x11drv_win_data
*data
;
1347 if (type
!= ICON_BIG
) return; /* nothing to do here */
1349 if (!(data
= X11DRV_get_win_data( hwnd
))) return;
1350 if (!data
->whole_window
) return;
1351 if (!data
->managed
) return;
1355 set_icon_hints( display
, data
, icon
);
1357 XSetWMHints( display
, data
->whole_window
, data
->wm_hints
);
1358 wine_tsx11_unlock();