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 /* activated windows are managed */
80 if (!(swp_flags
& (SWP_NOACTIVATE
|SWP_HIDEWINDOW
))) return TRUE
;
81 if (hwnd
== GetActiveWindow()) return TRUE
;
82 /* windows with caption are managed */
83 if ((style
& WS_CAPTION
) == WS_CAPTION
) return TRUE
;
84 /* tool windows are not managed */
85 if (ex_style
& WS_EX_TOOLWINDOW
) return FALSE
;
86 /* windows with thick frame are managed */
87 if (style
& WS_THICKFRAME
) return TRUE
;
88 /* application windows are managed */
89 if (ex_style
& WS_EX_APPWINDOW
) return TRUE
;
92 /* popup with sysmenu == caption are managed */
93 if (style
& WS_SYSMENU
) return TRUE
;
94 /* full-screen popup windows are managed */
95 if ((window_rect
->right
- window_rect
->left
) == screen_width
&&
96 (window_rect
->bottom
- window_rect
->top
) == screen_height
)
99 /* default: not managed */
104 /***********************************************************************
105 * X11DRV_is_window_rect_mapped
107 * Check if the X whole window should be mapped based on its rectangle
109 BOOL
X11DRV_is_window_rect_mapped( const RECT
*rect
)
111 /* don't map if rect is empty */
112 if (IsRectEmpty( rect
)) return FALSE
;
114 /* don't map if rect is off-screen */
115 if (rect
->left
>= virtual_screen_rect
.right
||
116 rect
->top
>= virtual_screen_rect
.bottom
||
117 rect
->right
<= virtual_screen_rect
.left
||
118 rect
->bottom
<= virtual_screen_rect
.top
)
125 /***********************************************************************
126 * get_window_attributes
128 * Fill the window attributes structure for an X window.
130 static int get_window_attributes( Display
*display
, struct x11drv_win_data
*data
,
131 XSetWindowAttributes
*attr
)
133 attr
->override_redirect
= !data
->managed
;
134 attr
->colormap
= X11DRV_PALETTE_PaletteXColormap
;
135 attr
->save_under
= ((GetClassLongW( data
->hwnd
, GCL_STYLE
) & CS_SAVEBITS
) != 0);
136 attr
->cursor
= x11drv_thread_data()->cursor
;
137 attr
->bit_gravity
= NorthWestGravity
;
138 attr
->backing_store
= NotUseful
;
139 attr
->event_mask
= (ExposureMask
| PointerMotionMask
|
140 ButtonPressMask
| ButtonReleaseMask
| EnterWindowMask
|
141 KeyPressMask
| KeyReleaseMask
| FocusChangeMask
| KeymapStateMask
);
142 if (data
->managed
) attr
->event_mask
|= StructureNotifyMask
;
144 return (CWOverrideRedirect
| CWSaveUnder
| CWColormap
| CWCursor
|
145 CWEventMask
| CWBitGravity
| CWBackingStore
);
149 /***********************************************************************
150 * X11DRV_sync_window_style
152 * Change the X window attributes when the window style has changed.
154 void X11DRV_sync_window_style( Display
*display
, struct x11drv_win_data
*data
)
156 if (data
->whole_window
!= root_window
)
158 XSetWindowAttributes attr
;
159 int mask
= get_window_attributes( display
, data
, &attr
);
162 XChangeWindowAttributes( display
, data
->whole_window
, mask
, &attr
);
168 /***********************************************************************
171 * fill the window changes structure
173 static int get_window_changes( XWindowChanges
*changes
, const RECT
*old
, const RECT
*new )
177 if (old
->right
- old
->left
!= new->right
- new->left
)
179 if (!(changes
->width
= new->right
- new->left
)) changes
->width
= 1;
182 if (old
->bottom
- old
->top
!= new->bottom
- new->top
)
184 if (!(changes
->height
= new->bottom
- new->top
)) changes
->height
= 1;
187 if (old
->left
!= new->left
)
189 changes
->x
= new->left
;
192 if (old
->top
!= new->top
)
194 changes
->y
= new->top
;
201 /***********************************************************************
204 static Window
create_icon_window( Display
*display
, struct x11drv_win_data
*data
)
206 XSetWindowAttributes attr
;
208 attr
.event_mask
= (ExposureMask
| KeyPressMask
| KeyReleaseMask
| PointerMotionMask
|
209 ButtonPressMask
| ButtonReleaseMask
| EnterWindowMask
);
210 attr
.bit_gravity
= NorthWestGravity
;
211 attr
.backing_store
= NotUseful
/*WhenMapped*/;
212 attr
.colormap
= X11DRV_PALETTE_PaletteXColormap
; /* Needed due to our visual */
215 data
->icon_window
= XCreateWindow( display
, root_window
, 0, 0,
216 GetSystemMetrics( SM_CXICON
),
217 GetSystemMetrics( SM_CYICON
),
220 CWEventMask
| CWBitGravity
| CWBackingStore
| CWColormap
, &attr
);
221 XSaveContext( display
, data
->icon_window
, winContext
, (char *)data
->hwnd
);
224 TRACE( "created %lx\n", data
->icon_window
);
225 SetPropA( data
->hwnd
, icon_window_prop
, (HANDLE
)data
->icon_window
);
226 return data
->icon_window
;
231 /***********************************************************************
232 * destroy_icon_window
234 static void destroy_icon_window( Display
*display
, struct x11drv_win_data
*data
)
236 if (!data
->icon_window
) return;
237 if (x11drv_thread_data()->cursor_window
== data
->icon_window
)
238 x11drv_thread_data()->cursor_window
= None
;
240 XDeleteContext( display
, data
->icon_window
, winContext
);
241 XDestroyWindow( display
, data
->icon_window
);
242 data
->icon_window
= 0;
244 RemovePropA( data
->hwnd
, icon_window_prop
);
248 /***********************************************************************
251 * Set the icon wm hints
253 static void set_icon_hints( Display
*display
, struct x11drv_win_data
*data
, HICON hIcon
)
255 XWMHints
*hints
= data
->wm_hints
;
257 if (data
->hWMIconBitmap
) DeleteObject( data
->hWMIconBitmap
);
258 if (data
->hWMIconMask
) DeleteObject( data
->hWMIconMask
);
259 data
->hWMIconBitmap
= 0;
260 data
->hWMIconMask
= 0;
264 destroy_icon_window( display
, data
);
265 hints
->flags
&= ~(IconPixmapHint
| IconMaskHint
| IconWindowHint
);
269 if (!data
->icon_window
) create_icon_window( display
, data
);
270 hints
->icon_window
= data
->icon_window
;
271 hints
->flags
= (hints
->flags
& ~(IconPixmapHint
| IconMaskHint
)) | IconWindowHint
;
281 GetIconInfo(hIcon
, &ii
);
283 GetObjectA(ii
.hbmMask
, sizeof(bmMask
), &bmMask
);
286 rcMask
.right
= bmMask
.bmWidth
;
287 rcMask
.bottom
= bmMask
.bmHeight
;
289 hDC
= CreateCompatibleDC(0);
290 hbmOrig
= SelectObject(hDC
, ii
.hbmMask
);
291 InvertRect(hDC
, &rcMask
);
292 SelectObject(hDC
, ii
.hbmColor
); /* force the color bitmap to x11drv mode too */
293 SelectObject(hDC
, hbmOrig
);
296 data
->hWMIconBitmap
= ii
.hbmColor
;
297 data
->hWMIconMask
= ii
.hbmMask
;
299 hints
->icon_pixmap
= X11DRV_get_pixmap(data
->hWMIconBitmap
);
300 hints
->icon_mask
= X11DRV_get_pixmap(data
->hWMIconMask
);
301 destroy_icon_window( display
, data
);
302 hints
->flags
= (hints
->flags
& ~IconWindowHint
) | IconPixmapHint
| IconMaskHint
;
306 /***********************************************************************
307 * systray_dock_window
309 * Docks the given X window with the NETWM system tray.
311 static void systray_dock_window( Display
*display
, struct x11drv_win_data
*data
)
313 static Atom systray_atom
;
314 Window systray_window
;
319 if (DefaultScreen( display
) == 0)
320 systray_atom
= x11drv_atom(_NET_SYSTEM_TRAY_S0
);
323 char systray_buffer
[29]; /* strlen(_NET_SYSTEM_TRAY_S4294967295)+1 */
324 sprintf( systray_buffer
, "_NET_SYSTEM_TRAY_S%u", DefaultScreen( display
) );
325 systray_atom
= XInternAtom( display
, systray_buffer
, False
);
328 systray_window
= XGetSelectionOwner( display
, systray_atom
);
331 TRACE("Docking tray icon %p\n", data
->hwnd
);
333 if (systray_window
!= None
)
336 unsigned long info
[2];
338 /* Put the window offscreen so it isn't mapped. The window _cannot_ be
339 * mapped if we intend to dock with an XEMBED tray. If the window is
340 * mapped when we dock, it may become visible as a child of the root
341 * window after it docks, which isn't the proper behavior.
343 * For more information on this problem, see
344 * http://standards.freedesktop.org/xembed-spec/latest/ar01s04.html */
346 SetWindowPos( data
->hwnd
, NULL
, virtual_screen_rect
.right
+ 1, virtual_screen_rect
.bottom
+ 1,
347 0, 0, SWP_NOZORDER
| SWP_NOSIZE
| SWP_NOACTIVATE
);
349 /* set XEMBED protocol data on the window */
350 info
[0] = 0; /* protocol version */
351 info
[1] = 1; /* mapped = true */
354 XChangeProperty( display
, data
->whole_window
,
355 x11drv_atom(_XEMBED_INFO
),
356 x11drv_atom(_XEMBED_INFO
), 32, PropModeReplace
,
357 (unsigned char*)info
, 2 );
360 /* send the docking request message */
361 ZeroMemory( &ev
, sizeof(ev
) );
362 ev
.xclient
.type
= ClientMessage
;
363 ev
.xclient
.window
= systray_window
;
364 ev
.xclient
.message_type
= x11drv_atom( _NET_SYSTEM_TRAY_OPCODE
);
365 ev
.xclient
.format
= 32;
366 ev
.xclient
.data
.l
[0] = CurrentTime
;
367 ev
.xclient
.data
.l
[1] = SYSTEM_TRAY_REQUEST_DOCK
;
368 ev
.xclient
.data
.l
[2] = data
->whole_window
;
369 ev
.xclient
.data
.l
[3] = 0;
370 ev
.xclient
.data
.l
[4] = 0;
373 XSendEvent( display
, systray_window
, False
, NoEventMask
, &ev
);
381 /* fall back to he KDE hints if the WM doesn't support XEMBED'ed
385 XChangeProperty( display
, data
->whole_window
,
386 x11drv_atom(KWM_DOCKWINDOW
),
387 x11drv_atom(KWM_DOCKWINDOW
), 32, PropModeReplace
,
388 (unsigned char*)&val
, 1 );
389 XChangeProperty( display
, data
->whole_window
,
390 x11drv_atom(_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR
),
391 XA_WINDOW
, 32, PropModeReplace
,
392 (unsigned char*)&data
->whole_window
, 1 );
398 /***********************************************************************
401 * set the window size hints
403 static void set_size_hints( Display
*display
, struct x11drv_win_data
*data
, DWORD style
)
405 XSizeHints
* size_hints
;
407 if ((size_hints
= XAllocSizeHints()))
409 size_hints
->flags
= 0;
411 if (data
->hwnd
!= GetDesktopWindow()) /* don't force position of desktop */
413 size_hints
->win_gravity
= StaticGravity
;
414 size_hints
->x
= data
->whole_rect
.left
;
415 size_hints
->y
= data
->whole_rect
.top
;
416 size_hints
->flags
|= PWinGravity
| PPosition
;
419 if ( !(style
& WS_THICKFRAME
) )
421 size_hints
->max_width
= data
->whole_rect
.right
- data
->whole_rect
.left
;
422 size_hints
->max_height
= data
->whole_rect
.bottom
- data
->whole_rect
.top
;
423 size_hints
->min_width
= size_hints
->max_width
;
424 size_hints
->min_height
= size_hints
->max_height
;
425 size_hints
->flags
|= PMinSize
| PMaxSize
;
427 XSetWMNormalHints( display
, data
->whole_window
, size_hints
);
433 /***********************************************************************
436 * get the name of the current process for setting class hints
438 static char *get_process_name(void)
444 WCHAR module
[MAX_PATH
];
445 DWORD len
= GetModuleFileNameW( 0, module
, MAX_PATH
);
446 if (len
&& len
< MAX_PATH
)
449 WCHAR
*p
, *appname
= module
;
451 if ((p
= strrchrW( appname
, '/' ))) appname
= p
+ 1;
452 if ((p
= strrchrW( appname
, '\\' ))) appname
= p
+ 1;
453 len
= WideCharToMultiByte( CP_UNIXCP
, 0, appname
, -1, NULL
, 0, NULL
, NULL
);
454 if ((ptr
= HeapAlloc( GetProcessHeap(), 0, len
)))
456 WideCharToMultiByte( CP_UNIXCP
, 0, appname
, -1, ptr
, len
, NULL
, NULL
);
465 /***********************************************************************
466 * set_initial_wm_hints
468 * Set the window manager hints that don't change over the lifetime of a window.
470 static void set_initial_wm_hints( Display
*display
, struct x11drv_win_data
*data
)
475 XClassHint
*class_hints
;
476 char *process_name
= get_process_name();
482 protocols
[i
++] = x11drv_atom(WM_DELETE_WINDOW
);
483 protocols
[i
++] = x11drv_atom(_NET_WM_PING
);
484 if (use_take_focus
) protocols
[i
++] = x11drv_atom(WM_TAKE_FOCUS
);
485 XChangeProperty( display
, data
->whole_window
, x11drv_atom(WM_PROTOCOLS
),
486 XA_ATOM
, 32, PropModeReplace
, (unsigned char *)protocols
, i
);
489 if ((class_hints
= XAllocClassHint()))
491 static char wine
[] = "Wine";
493 class_hints
->res_name
= process_name
;
494 class_hints
->res_class
= wine
;
495 XSetClassHint( display
, data
->whole_window
, class_hints
);
496 XFree( class_hints
);
499 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
500 XSetWMProperties(display
, data
->whole_window
, NULL
, NULL
, NULL
, 0, NULL
, NULL
, NULL
);
501 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
503 XChangeProperty(display
, data
->whole_window
, x11drv_atom(_NET_WM_PID
),
504 XA_CARDINAL
, 32, PropModeReplace
, (unsigned char *)&i
, 1);
506 XChangeProperty( display
, data
->whole_window
, x11drv_atom(XdndAware
),
507 XA_ATOM
, 32, PropModeReplace
, (unsigned char*)&dndVersion
, 1 );
513 /***********************************************************************
514 * X11DRV_set_wm_hints
516 * Set the window manager hints for a newly-created window
518 void X11DRV_set_wm_hints( Display
*display
, struct x11drv_win_data
*data
)
523 DWORD style
= GetWindowLongW( data
->hwnd
, GWL_STYLE
);
524 DWORD ex_style
= GetWindowLongW( data
->hwnd
, GWL_EXSTYLE
);
525 HWND owner
= GetWindow( data
->hwnd
, GW_OWNER
);
527 if (data
->hwnd
== GetDesktopWindow())
529 if (data
->whole_window
== DefaultRootWindow(display
)) return;
530 /* force some styles for the desktop to get the correct decorations */
531 style
|= WS_CAPTION
| WS_SYSMENU
| WS_MINIMIZEBOX
;
535 /* transient for hint */
538 Window owner_win
= X11DRV_get_whole_window( owner
);
540 XSetTransientForHint( display
, data
->whole_window
, owner_win
);
542 group_leader
= owner_win
;
544 else group_leader
= data
->whole_window
;
549 set_size_hints( display
, data
, style
);
551 /* set the WM_WINDOW_TYPE */
552 window_type
= x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL
);
553 if (ex_style
& WS_EX_TOOLWINDOW
) window_type
= x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY
);
554 else if (style
& WS_THICKFRAME
) window_type
= x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL
);
555 else if (style
& WS_DLGFRAME
) window_type
= x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG
);
556 else if (ex_style
& WS_EX_DLGMODALFRAME
) window_type
= x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG
);
558 XChangeProperty(display
, data
->whole_window
, x11drv_atom(_NET_WM_WINDOW_TYPE
),
559 XA_ATOM
, 32, PropModeReplace
, (unsigned char*)&window_type
, 1);
561 mwm_hints
.flags
= MWM_HINTS_FUNCTIONS
| MWM_HINTS_DECORATIONS
;
562 mwm_hints
.functions
= MWM_FUNC_MOVE
;
563 if (style
& WS_THICKFRAME
) mwm_hints
.functions
|= MWM_FUNC_RESIZE
;
564 if (style
& WS_MINIMIZEBOX
) mwm_hints
.functions
|= MWM_FUNC_MINIMIZE
;
565 if (style
& WS_MAXIMIZEBOX
) mwm_hints
.functions
|= MWM_FUNC_MAXIMIZE
;
566 if (style
& WS_SYSMENU
) mwm_hints
.functions
|= MWM_FUNC_CLOSE
;
567 mwm_hints
.decorations
= 0;
568 if (!(ex_style
& WS_EX_TOOLWINDOW
))
570 if ((style
& WS_CAPTION
) == WS_CAPTION
)
572 mwm_hints
.decorations
|= MWM_DECOR_TITLE
;
573 if (style
& WS_SYSMENU
) mwm_hints
.decorations
|= MWM_DECOR_MENU
;
574 if (style
& WS_MINIMIZEBOX
) mwm_hints
.decorations
|= MWM_DECOR_MINIMIZE
;
575 if (style
& WS_MAXIMIZEBOX
) mwm_hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
577 if (ex_style
& WS_EX_DLGMODALFRAME
) mwm_hints
.decorations
|= MWM_DECOR_BORDER
;
578 else if (style
& WS_THICKFRAME
) mwm_hints
.decorations
|= MWM_DECOR_BORDER
| MWM_DECOR_RESIZEH
;
579 else if ((style
& (WS_DLGFRAME
|WS_BORDER
)) == WS_DLGFRAME
) mwm_hints
.decorations
|= MWM_DECOR_BORDER
;
580 else if (style
& WS_BORDER
) mwm_hints
.decorations
|= MWM_DECOR_BORDER
;
581 else if (!(style
& (WS_CHILD
|WS_POPUP
))) mwm_hints
.decorations
|= MWM_DECOR_BORDER
;
584 XChangeProperty( display
, data
->whole_window
, x11drv_atom(_MOTIF_WM_HINTS
),
585 x11drv_atom(_MOTIF_WM_HINTS
), 32, PropModeReplace
,
586 (unsigned char*)&mwm_hints
, sizeof(mwm_hints
)/sizeof(long) );
593 data
->wm_hints
->flags
= InputHint
| StateHint
| WindowGroupHint
;
594 data
->wm_hints
->input
= !(style
& WS_DISABLED
);
595 data
->wm_hints
->initial_state
= (style
& WS_MINIMIZE
) ? IconicState
: NormalState
;
596 data
->wm_hints
->window_group
= group_leader
;
597 set_icon_hints( display
, data
, (HICON
)GetClassLongPtrW( data
->hwnd
, GCLP_HICON
) );
600 XSetWMHints( display
, data
->whole_window
, data
->wm_hints
);
606 /***********************************************************************
607 * X11DRV_set_iconic_state
609 * Set the X11 iconic state according to the window style.
611 void X11DRV_set_iconic_state( HWND hwnd
)
613 Display
*display
= thread_display();
614 struct x11drv_win_data
*data
;
616 DWORD style
= GetWindowLongW( hwnd
, GWL_STYLE
);
617 BOOL iconic
= (style
& WS_MINIMIZE
) != 0;
619 if (!(data
= X11DRV_get_win_data( hwnd
))) return;
620 if (!data
->whole_window
|| data
->whole_window
== DefaultRootWindow(display
)) return;
622 GetWindowRect( hwnd
, &rect
);
628 data
->wm_hints
->flags
|= StateHint
| IconPositionHint
;
629 data
->wm_hints
->initial_state
= iconic
? IconicState
: NormalState
;
630 data
->wm_hints
->icon_x
= rect
.left
- virtual_screen_rect
.left
;
631 data
->wm_hints
->icon_y
= rect
.top
- virtual_screen_rect
.top
;
632 XSetWMHints( display
, data
->whole_window
, data
->wm_hints
);
635 if (style
& WS_VISIBLE
)
638 XIconifyWindow( display
, data
->whole_window
, DefaultScreen(display
) );
640 if (X11DRV_is_window_rect_mapped( &rect
))
641 XMapWindow( display
, data
->whole_window
);
648 /***********************************************************************
649 * X11DRV_window_to_X_rect
651 * Convert a rect from client to X window coordinates
653 void X11DRV_window_to_X_rect( struct x11drv_win_data
*data
, RECT
*rect
)
658 if (!data
->managed
) return;
659 if (IsRectEmpty( rect
)) return;
660 ex_style
= GetWindowLongW( data
->hwnd
, GWL_EXSTYLE
);
661 if (ex_style
& WS_EX_TOOLWINDOW
) return;
663 rc
.top
= rc
.bottom
= rc
.left
= rc
.right
= 0;
665 AdjustWindowRectEx( &rc
, GetWindowLongW( data
->hwnd
, GWL_STYLE
) & ~(WS_HSCROLL
|WS_VSCROLL
),
668 rect
->left
-= rc
.left
;
669 rect
->right
-= rc
.right
;
671 rect
->bottom
-= rc
.bottom
;
672 if (rect
->top
>= rect
->bottom
) rect
->bottom
= rect
->top
+ 1;
673 if (rect
->left
>= rect
->right
) rect
->right
= rect
->left
+ 1;
677 /***********************************************************************
678 * X11DRV_X_to_window_rect
680 * Opposite of X11DRV_window_to_X_rect
682 void X11DRV_X_to_window_rect( struct x11drv_win_data
*data
, RECT
*rect
)
686 if (!data
->managed
) return;
687 if (IsRectEmpty( rect
)) return;
688 ex_style
= GetWindowLongW( data
->hwnd
, GWL_EXSTYLE
);
689 if (ex_style
& WS_EX_TOOLWINDOW
) return;
691 AdjustWindowRectEx( rect
, GetWindowLongW( data
->hwnd
, GWL_STYLE
) & ~(WS_HSCROLL
|WS_VSCROLL
),
694 if (rect
->top
>= rect
->bottom
) rect
->bottom
= rect
->top
+ 1;
695 if (rect
->left
>= rect
->right
) rect
->right
= rect
->left
+ 1;
699 /***********************************************************************
700 * X11DRV_sync_window_position
702 * Synchronize the X window position with the Windows one
704 void X11DRV_sync_window_position( Display
*display
, struct x11drv_win_data
*data
,
705 UINT swp_flags
, const RECT
*new_client_rect
,
706 const RECT
*new_whole_rect
)
708 XWindowChanges changes
;
712 old_whole_rect
= data
->whole_rect
;
713 data
->whole_rect
= *new_whole_rect
;
715 data
->client_rect
= *new_client_rect
;
716 OffsetRect( &data
->client_rect
, -data
->whole_rect
.left
, -data
->whole_rect
.top
);
718 if (!data
->whole_window
|| data
->lock_changes
) return;
720 mask
= get_window_changes( &changes
, &old_whole_rect
, &data
->whole_rect
);
722 if (!(swp_flags
& SWP_NOZORDER
))
724 /* find window that this one must be after */
725 HWND prev
= GetWindow( data
->hwnd
, GW_HWNDPREV
);
726 while (prev
&& !(GetWindowLongW( prev
, GWL_STYLE
) & WS_VISIBLE
))
727 prev
= GetWindow( prev
, GW_HWNDPREV
);
728 if (!prev
) /* top child */
730 changes
.stack_mode
= Above
;
735 /* should use stack_mode Below but most window managers don't get it right */
736 /* so move it above the next one in Z order */
737 HWND next
= GetWindow( data
->hwnd
, GW_HWNDNEXT
);
738 while (next
&& !(GetWindowLongW( next
, GWL_STYLE
) & WS_VISIBLE
))
739 next
= GetWindow( next
, GW_HWNDNEXT
);
742 changes
.stack_mode
= Above
;
743 changes
.sibling
= X11DRV_get_whole_window(next
);
744 mask
|= CWStackMode
| CWSibling
;
751 DWORD style
= GetWindowLongW( data
->hwnd
, GWL_STYLE
);
753 TRACE( "setting win %lx pos %d,%d,%dx%d after %lx changes=%x\n",
754 data
->whole_window
, data
->whole_rect
.left
, data
->whole_rect
.top
,
755 data
->whole_rect
.right
- data
->whole_rect
.left
,
756 data
->whole_rect
.bottom
- data
->whole_rect
.top
, changes
.sibling
, mask
);
759 if (mask
& (CWWidth
|CWHeight
)) set_size_hints( display
, data
, style
);
760 if (mask
& CWX
) changes
.x
-= virtual_screen_rect
.left
;
761 if (mask
& CWY
) changes
.y
-= virtual_screen_rect
.top
;
762 XReconfigureWMWindow( display
, data
->whole_window
,
763 DefaultScreen(display
), mask
, &changes
);
769 /**********************************************************************
770 * create_whole_window
772 * Create the whole X window for a given window
774 static Window
create_whole_window( Display
*display
, struct x11drv_win_data
*data
, DWORD style
)
777 XSetWindowAttributes attr
;
780 if (!(cx
= data
->window_rect
.right
- data
->window_rect
.left
)) cx
= 1;
781 if (!(cy
= data
->window_rect
.bottom
- data
->window_rect
.top
)) cy
= 1;
783 mask
= get_window_attributes( display
, data
, &attr
);
787 data
->whole_rect
= data
->window_rect
;
788 data
->whole_window
= XCreateWindow( display
, root_window
,
789 data
->window_rect
.left
- virtual_screen_rect
.left
,
790 data
->window_rect
.top
- virtual_screen_rect
.top
,
791 cx
, cy
, 0, screen_depth
, InputOutput
,
792 visual
, mask
, &attr
);
794 if (!data
->whole_window
)
799 XSaveContext( display
, data
->whole_window
, winContext
, (char *)data
->hwnd
);
801 /* non-maximized child must be at bottom of Z order */
802 if ((style
& (WS_CHILD
|WS_MAXIMIZE
)) == WS_CHILD
)
804 XWindowChanges changes
;
805 changes
.stack_mode
= Below
;
806 XConfigureWindow( display
, data
->whole_window
, CWStackMode
, &changes
);
810 xim
= x11drv_thread_data()->xim
;
811 if (xim
) data
->xic
= X11DRV_CreateIC( xim
, display
, data
->whole_window
);
813 set_initial_wm_hints( display
, data
);
814 X11DRV_set_wm_hints( display
, data
);
816 SetPropA( data
->hwnd
, whole_window_prop
, (HANDLE
)data
->whole_window
);
817 return data
->whole_window
;
821 /**********************************************************************
822 * destroy_whole_window
824 * Destroy the whole X window for a given window.
826 static void destroy_whole_window( Display
*display
, struct x11drv_win_data
*data
)
828 struct x11drv_thread_data
*thread_data
= x11drv_thread_data();
830 if (!data
->whole_window
) return;
832 TRACE( "win %p xwin %lx\n", data
->hwnd
, data
->whole_window
);
833 if (thread_data
->cursor_window
== data
->whole_window
) thread_data
->cursor_window
= None
;
835 XDeleteContext( display
, data
->whole_window
, winContext
);
836 if (data
->whole_window
!= DefaultRootWindow(display
))
837 XDestroyWindow( display
, data
->whole_window
);
838 data
->whole_window
= 0;
841 XUnsetICFocus( data
->xic
);
842 XDestroyIC( data
->xic
);
844 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
847 RemovePropA( data
->hwnd
, whole_window_prop
);
851 /*****************************************************************
852 * SetWindowText (X11DRV.@)
854 void X11DRV_SetWindowText( HWND hwnd
, LPCWSTR text
)
856 Display
*display
= thread_display();
863 if ((win
= X11DRV_get_whole_window( hwnd
)) && win
!= DefaultRootWindow(display
))
865 /* allocate new buffer for window text */
866 count
= WideCharToMultiByte(CP_UNIXCP
, 0, text
, -1, NULL
, 0, NULL
, NULL
);
867 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, count
)))
869 ERR("Not enough memory for window text\n");
872 WideCharToMultiByte(CP_UNIXCP
, 0, text
, -1, buffer
, count
, NULL
, NULL
);
874 count
= WideCharToMultiByte(CP_UTF8
, 0, text
, strlenW(text
), NULL
, 0, NULL
, NULL
);
875 if (!(utf8_buffer
= HeapAlloc( GetProcessHeap(), 0, count
)))
877 ERR("Not enough memory for window text in UTF-8\n");
878 HeapFree( GetProcessHeap(), 0, buffer
);
881 WideCharToMultiByte(CP_UTF8
, 0, text
, strlenW(text
), utf8_buffer
, count
, NULL
, NULL
);
884 if (XmbTextListToTextProperty( display
, &buffer
, 1, XStdICCTextStyle
, &prop
) == Success
)
886 XSetWMName( display
, win
, &prop
);
887 XSetWMIconName( display
, win
, &prop
);
891 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
892 according to the standard
893 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
895 XChangeProperty( display
, win
, x11drv_atom(_NET_WM_NAME
), x11drv_atom(UTF8_STRING
),
896 8, PropModeReplace
, (unsigned char *) utf8_buffer
, count
);
899 HeapFree( GetProcessHeap(), 0, utf8_buffer
);
900 HeapFree( GetProcessHeap(), 0, buffer
);
905 /***********************************************************************
906 * DestroyWindow (X11DRV.@)
908 void X11DRV_DestroyWindow( HWND hwnd
)
910 struct x11drv_thread_data
*thread_data
= x11drv_thread_data();
911 Display
*display
= thread_data
->display
;
912 struct x11drv_win_data
*data
;
914 if (!(data
= X11DRV_get_win_data( hwnd
))) return;
916 free_window_dce( data
);
917 destroy_whole_window( display
, data
);
918 destroy_icon_window( display
, data
);
920 if (thread_data
->last_focus
== hwnd
) thread_data
->last_focus
= 0;
921 if (data
->hWMIconBitmap
) DeleteObject( data
->hWMIconBitmap
);
922 if (data
->hWMIconMask
) DeleteObject( data
->hWMIconMask
);
924 XDeleteContext( display
, (XID
)hwnd
, win_data_context
);
925 XFree( data
->wm_hints
);
927 HeapFree( GetProcessHeap(), 0, data
);
931 static struct x11drv_win_data
*alloc_win_data( Display
*display
, HWND hwnd
)
933 struct x11drv_win_data
*data
;
935 if ((data
= HeapAlloc(GetProcessHeap(), 0, sizeof(*data
))))
938 data
->whole_window
= 0;
939 data
->icon_window
= 0;
941 data
->managed
= FALSE
;
943 data
->lock_changes
= 0;
944 data
->hWMIconBitmap
= 0;
945 data
->hWMIconMask
= 0;
948 if (!winContext
) winContext
= XUniqueContext();
949 if (!win_data_context
) win_data_context
= XUniqueContext();
950 XSaveContext( display
, (XID
)hwnd
, win_data_context
, (char *)data
);
951 data
->wm_hints
= XAllocWMHints();
958 /* fill in the desktop X window id in the x11drv_win_data structure */
959 static void get_desktop_xwin( Display
*display
, struct x11drv_win_data
*data
)
961 Window win
= (Window
)GetPropA( data
->hwnd
, whole_window_prop
);
965 unsigned int width
, height
;
967 /* retrieve the real size of the desktop */
968 SERVER_START_REQ( get_window_rectangles
)
970 req
->handle
= data
->hwnd
;
971 wine_server_call( req
);
972 width
= reply
->window
.right
- reply
->window
.left
;
973 height
= reply
->window
.bottom
- reply
->window
.top
;
976 data
->whole_window
= win
;
977 if (win
!= root_window
) X11DRV_init_desktop( win
, width
, height
);
984 visualid
= XVisualIDFromVisual(visual
);
986 SetPropA( data
->hwnd
, whole_window_prop
, (HANDLE
)root_window
);
987 SetPropA( data
->hwnd
, visual_id_prop
, (HANDLE
)visualid
);
988 data
->whole_window
= root_window
;
989 X11DRV_SetWindowPos( data
->hwnd
, 0, &virtual_screen_rect
, &virtual_screen_rect
,
990 SWP_NOZORDER
| SWP_NOACTIVATE
, NULL
);
991 if (root_window
!= DefaultRootWindow( display
))
993 data
->managed
= TRUE
;
994 SetPropA( data
->hwnd
, managed_prop
, (HANDLE
)1 );
995 set_initial_wm_hints( display
, data
);
1000 /**********************************************************************
1001 * CreateDesktopWindow (X11DRV.@)
1003 BOOL
X11DRV_CreateDesktopWindow( HWND hwnd
)
1005 Display
*display
= thread_display();
1006 struct x11drv_win_data
*data
;
1008 if (!(data
= alloc_win_data( display
, hwnd
))) return FALSE
;
1010 get_desktop_xwin( display
, data
);
1016 /**********************************************************************
1017 * CreateWindow (X11DRV.@)
1019 BOOL
X11DRV_CreateWindow( HWND hwnd
, CREATESTRUCTA
*cs
, BOOL unicode
)
1021 Display
*display
= thread_display();
1023 struct x11drv_win_data
*data
;
1027 CBT_CREATEWNDA cbtc
;
1031 if (!(data
= alloc_win_data( display
, hwnd
))) return FALSE
;
1035 ERR( "invalid window width %d\n", cs
->cx
);
1040 ERR( "invalid window height %d\n", cs
->cy
);
1045 ERR( "invalid window width %d\n", cs
->cx
);
1050 ERR( "invalid window height %d\n", cs
->cy
);
1054 /* initialize the dimensions before sending WM_GETMINMAXINFO */
1055 SetRect( &rect
, cs
->x
, cs
->y
, cs
->x
+ cs
->cx
, cs
->y
+ cs
->cy
);
1056 X11DRV_SetWindowPos( hwnd
, 0, &rect
, &rect
, SWP_NOZORDER
| SWP_NOACTIVATE
, NULL
);
1058 /* create an X window if it's a top level window */
1059 if (GetAncestor( hwnd
, GA_PARENT
) == GetDesktopWindow())
1061 if (!create_whole_window( display
, data
, cs
->style
)) goto failed
;
1063 else if (hwnd
== GetDesktopWindow())
1065 get_desktop_xwin( display
, data
);
1068 /* get class or window DC if needed */
1069 alloc_window_dce( data
);
1071 /* Call the WH_CBT hook */
1073 /* the window style passed to the hook must be the real window style,
1074 * rather than just the window style that the caller to CreateWindowEx
1075 * passed in, so we have to copy the original CREATESTRUCT and get the
1076 * the real style. */
1078 cbcs
.style
= GetWindowLongW(hwnd
, GWL_STYLE
);
1081 cbtc
.hwndInsertAfter
= HWND_TOP
;
1082 if (HOOK_CallHooks( WH_CBT
, HCBT_CREATEWND
, (WPARAM
)hwnd
, (LPARAM
)&cbtc
, unicode
))
1084 TRACE("CBT-hook returned !0\n");
1088 /* Send the WM_GETMINMAXINFO message and fix the size if needed */
1089 if ((cs
->style
& WS_THICKFRAME
) || !(cs
->style
& (WS_POPUP
| WS_CHILD
)))
1091 POINT maxSize
, maxPos
, minTrack
, maxTrack
;
1093 WINPOS_GetMinMaxInfo( hwnd
, &maxSize
, &maxPos
, &minTrack
, &maxTrack
);
1094 if (maxTrack
.x
< cs
->cx
) cs
->cx
= maxTrack
.x
;
1095 if (maxTrack
.y
< cs
->cy
) cs
->cy
= maxTrack
.y
;
1096 if (cs
->cx
< 0) cs
->cx
= 0;
1097 if (cs
->cy
< 0) cs
->cy
= 0;
1099 SetRect( &rect
, cs
->x
, cs
->y
, cs
->x
+ cs
->cx
, cs
->y
+ cs
->cy
);
1100 if (!X11DRV_SetWindowPos( hwnd
, 0, &rect
, &rect
, SWP_NOZORDER
| SWP_NOACTIVATE
, NULL
))
1104 /* send WM_NCCREATE */
1105 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd
, cs
->x
, cs
->y
, cs
->cx
, cs
->cy
);
1107 ret
= SendMessageW( hwnd
, WM_NCCREATE
, 0, (LPARAM
)cs
);
1109 ret
= SendMessageA( hwnd
, WM_NCCREATE
, 0, (LPARAM
)cs
);
1112 WARN("aborted by WM_xxCREATE!\n");
1116 /* make sure the window is still valid */
1117 if (!(data
= X11DRV_get_win_data( hwnd
))) return FALSE
;
1118 if (data
->whole_window
) X11DRV_sync_window_style( display
, data
);
1120 /* send WM_NCCALCSIZE */
1121 rect
= data
->window_rect
;
1122 SendMessageW( hwnd
, WM_NCCALCSIZE
, FALSE
, (LPARAM
)&rect
);
1124 if (!(wndPtr
= WIN_GetPtr(hwnd
))) return FALSE
;
1126 /* yes, even if the CBT hook was called with HWND_TOP */
1127 insert_after
= (wndPtr
->dwStyle
& WS_CHILD
) ? HWND_BOTTOM
: HWND_TOP
;
1129 X11DRV_SetWindowPos( hwnd
, insert_after
, &wndPtr
->rectWindow
, &rect
, SWP_NOACTIVATE
, NULL
);
1131 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",
1132 hwnd
, wndPtr
->rectWindow
.left
, wndPtr
->rectWindow
.top
,
1133 wndPtr
->rectWindow
.right
, wndPtr
->rectWindow
.bottom
,
1134 wndPtr
->rectClient
.left
, wndPtr
->rectClient
.top
,
1135 wndPtr
->rectClient
.right
, wndPtr
->rectClient
.bottom
,
1136 data
->whole_rect
.left
, data
->whole_rect
.top
,
1137 data
->whole_rect
.right
, data
->whole_rect
.bottom
,
1138 data
->client_rect
.left
, data
->client_rect
.top
,
1139 data
->client_rect
.right
, data
->client_rect
.bottom
,
1140 (unsigned int)data
->whole_window
);
1142 WIN_ReleasePtr( wndPtr
);
1145 ret
= (SendMessageW( hwnd
, WM_CREATE
, 0, (LPARAM
)cs
) != -1);
1147 ret
= (SendMessageA( hwnd
, WM_CREATE
, 0, (LPARAM
)cs
) != -1);
1149 if (!ret
) return FALSE
;
1151 NotifyWinEvent(EVENT_OBJECT_CREATE
, hwnd
, OBJID_WINDOW
, 0);
1153 /* Send the size messages */
1155 if (!(wndPtr
= WIN_GetPtr(hwnd
)) || wndPtr
== WND_OTHER_PROCESS
) return FALSE
;
1156 if (!(wndPtr
->flags
& WIN_NEED_SIZE
))
1158 RECT rect
= wndPtr
->rectClient
;
1159 WIN_ReleasePtr( wndPtr
);
1160 /* send it anyway */
1161 if (((rect
.right
-rect
.left
) <0) ||((rect
.bottom
-rect
.top
)<0))
1162 WARN("sending bogus WM_SIZE message 0x%08x\n",
1163 MAKELONG(rect
.right
-rect
.left
, rect
.bottom
-rect
.top
));
1164 SendMessageW( hwnd
, WM_SIZE
, SIZE_RESTORED
,
1165 MAKELONG(rect
.right
-rect
.left
, rect
.bottom
-rect
.top
));
1166 SendMessageW( hwnd
, WM_MOVE
, 0, MAKELONG( rect
.left
, rect
.top
) );
1168 else WIN_ReleasePtr( wndPtr
);
1170 /* Show the window, maximizing or minimizing if needed */
1172 style
= GetWindowLongW( hwnd
, GWL_STYLE
);
1173 if (style
& (WS_MINIMIZE
| WS_MAXIMIZE
))
1175 extern UINT
WINPOS_MinMaximize( HWND hwnd
, UINT cmd
, LPRECT rect
); /*FIXME*/
1178 UINT swFlag
= (style
& WS_MINIMIZE
) ? SW_MINIMIZE
: SW_MAXIMIZE
;
1179 WIN_SetStyle( hwnd
, 0, WS_MAXIMIZE
| WS_MINIMIZE
);
1180 swFlag
= WINPOS_MinMaximize( hwnd
, swFlag
, &newPos
);
1182 swFlag
|= SWP_FRAMECHANGED
; /* Frame always gets changed */
1183 if (!(style
& WS_VISIBLE
) || (style
& WS_CHILD
) || GetActiveWindow()) swFlag
|= SWP_NOACTIVATE
;
1185 SetWindowPos( hwnd
, 0, newPos
.left
, newPos
.top
,
1186 newPos
.right
, newPos
.bottom
, swFlag
);
1189 /* Dock system tray windows. */
1190 /* Dock after the window is created so we don't have problems calling
1192 if (GetWindowLongW( hwnd
, GWL_EXSTYLE
) & WS_EX_TRAYWINDOW
)
1193 systray_dock_window( display
, data
);
1198 X11DRV_DestroyWindow( hwnd
);
1203 /***********************************************************************
1204 * X11DRV_get_win_data
1206 * Return the X11 data structure associated with a window.
1208 struct x11drv_win_data
*X11DRV_get_win_data( HWND hwnd
)
1212 if (!hwnd
|| XFindContext( thread_display(), (XID
)hwnd
, win_data_context
, &data
)) data
= NULL
;
1213 return (struct x11drv_win_data
*)data
;
1217 /***********************************************************************
1218 * X11DRV_get_whole_window
1220 * Return the X window associated with the full area of a window
1222 Window
X11DRV_get_whole_window( HWND hwnd
)
1224 struct x11drv_win_data
*data
= X11DRV_get_win_data( hwnd
);
1226 if (!data
) return (Window
)GetPropA( hwnd
, whole_window_prop
);
1227 return data
->whole_window
;
1231 /***********************************************************************
1234 * Return the X input context associated with a window
1236 XIC
X11DRV_get_ic( HWND hwnd
)
1238 struct x11drv_win_data
*data
= X11DRV_get_win_data( hwnd
);
1240 if (!data
) return 0;
1245 /*****************************************************************
1246 * SetParent (X11DRV.@)
1248 void X11DRV_SetParent( HWND hwnd
, HWND parent
, HWND old_parent
)
1250 Display
*display
= thread_display();
1251 struct x11drv_win_data
*data
= X11DRV_get_win_data( hwnd
);
1254 if (parent
== old_parent
) return;
1256 if (parent
!= GetDesktopWindow()) /* a child window */
1258 if (old_parent
== GetDesktopWindow())
1260 /* destroy the old X windows */
1261 destroy_whole_window( display
, data
);
1262 destroy_icon_window( display
, data
);
1265 data
->managed
= FALSE
;
1266 RemovePropA( data
->hwnd
, managed_prop
);
1270 else /* new top level window */
1272 /* FIXME: we ignore errors since we can't really recover anyway */
1273 create_whole_window( display
, data
, GetWindowLongW( hwnd
, GWL_STYLE
) );
1278 /*****************************************************************
1279 * SetFocus (X11DRV.@)
1282 * Explicit colormap management seems to work only with OLVWM.
1284 void X11DRV_SetFocus( HWND hwnd
)
1286 Display
*display
= thread_display();
1287 struct x11drv_win_data
*data
;
1288 XWindowAttributes win_attr
;
1290 /* Only mess with the X focus if there's */
1291 /* no desktop window and if the window is not managed by the WM. */
1292 if (root_window
!= DefaultRootWindow(display
)) return;
1294 if (!hwnd
) /* If setting the focus to 0, uninstall the colormap */
1297 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_PRIVATE
)
1298 XUninstallColormap( display
, X11DRV_PALETTE_PaletteXColormap
);
1299 wine_tsx11_unlock();
1303 hwnd
= GetAncestor( hwnd
, GA_ROOT
);
1305 if (!(data
= X11DRV_get_win_data( hwnd
))) return;
1306 if (data
->managed
|| !data
->whole_window
) return;
1308 /* Set X focus and install colormap */
1310 if (XGetWindowAttributes( display
, data
->whole_window
, &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
, data
->whole_window
, 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
;
1342 if (type
!= ICON_BIG
) return; /* nothing to do here */
1344 if (!(data
= X11DRV_get_win_data( hwnd
))) return;
1345 if (!data
->whole_window
) return;
1346 if (!data
->managed
) return;
1350 set_icon_hints( display
, data
, icon
);
1352 XSetWMHints( display
, data
->whole_window
, data
->wm_hints
);
1353 wine_tsx11_unlock();