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 static inline BOOL
is_window_managed( HWND hwnd
)
71 DWORD style
, ex_style
;
73 if (!managed_mode
) return FALSE
;
74 /* tray window is always managed */
75 ex_style
= GetWindowLongW( hwnd
, GWL_EXSTYLE
);
76 if (ex_style
& WS_EX_TRAYWINDOW
) return TRUE
;
77 /* child windows are not managed */
78 style
= GetWindowLongW( hwnd
, GWL_STYLE
);
79 if ((style
& (WS_CHILD
|WS_POPUP
)) == WS_CHILD
) return FALSE
;
80 #ifndef DEFAULTMANAGED
81 /* set to 1 to enable default managed instead of default not managed */
82 #define DEFAULTMANAGED 0
85 /* windows with caption are managed */
86 if ((style
& WS_CAPTION
) == WS_CAPTION
) return TRUE
;
87 /* tool windows are not managed */
88 if (ex_style
& WS_EX_TOOLWINDOW
) return FALSE
;
89 /* windows with thick frame are managed */
90 if (style
& WS_THICKFRAME
) return TRUE
;
91 /* application windows are managed */
92 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 GetWindowRect( hwnd
, &rect
);
102 if ((rect
.right
- rect
.left
) == screen_width
&& (rect
.bottom
- 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 if (!data
->managed
&&
150 root_window
== DefaultRootWindow( display
) &&
151 data
->whole_window
!= root_window
&&
152 is_window_managed( data
->hwnd
))
154 data
->managed
= TRUE
;
155 SetPropA( data
->hwnd
, managed_prop
, (HANDLE
)1 );
157 attr
->override_redirect
= !data
->managed
;
158 attr
->colormap
= X11DRV_PALETTE_PaletteXColormap
;
159 attr
->save_under
= ((GetClassLongW( data
->hwnd
, GCL_STYLE
) & CS_SAVEBITS
) != 0);
160 attr
->cursor
= x11drv_thread_data()->cursor
;
161 attr
->bit_gravity
= NorthWestGravity
;
162 attr
->backing_store
= NotUseful
;
163 attr
->event_mask
= (ExposureMask
| PointerMotionMask
|
164 ButtonPressMask
| ButtonReleaseMask
| EnterWindowMask
|
165 KeyPressMask
| KeyReleaseMask
| FocusChangeMask
| KeymapStateMask
);
166 if (data
->managed
) attr
->event_mask
|= StructureNotifyMask
;
168 return (CWOverrideRedirect
| CWSaveUnder
| CWColormap
| CWCursor
|
169 CWEventMask
| CWBitGravity
| CWBackingStore
);
173 /***********************************************************************
174 * X11DRV_sync_window_style
176 * Change the X window attributes when the window style has changed.
178 void X11DRV_sync_window_style( Display
*display
, struct x11drv_win_data
*data
)
180 if (data
->whole_window
!= root_window
)
182 XSetWindowAttributes attr
;
183 int mask
= get_window_attributes( display
, data
, &attr
);
186 XChangeWindowAttributes( display
, data
->whole_window
, mask
, &attr
);
192 /***********************************************************************
195 * fill the window changes structure
197 static int get_window_changes( XWindowChanges
*changes
, const RECT
*old
, const RECT
*new )
201 if (old
->right
- old
->left
!= new->right
- new->left
)
203 if (!(changes
->width
= new->right
- new->left
)) changes
->width
= 1;
206 if (old
->bottom
- old
->top
!= new->bottom
- new->top
)
208 if (!(changes
->height
= new->bottom
- new->top
)) changes
->height
= 1;
211 if (old
->left
!= new->left
)
213 changes
->x
= new->left
;
216 if (old
->top
!= new->top
)
218 changes
->y
= new->top
;
225 /***********************************************************************
228 static Window
create_icon_window( Display
*display
, struct x11drv_win_data
*data
)
230 XSetWindowAttributes attr
;
232 attr
.event_mask
= (ExposureMask
| KeyPressMask
| KeyReleaseMask
| PointerMotionMask
|
233 ButtonPressMask
| ButtonReleaseMask
| EnterWindowMask
);
234 attr
.bit_gravity
= NorthWestGravity
;
235 attr
.backing_store
= NotUseful
/*WhenMapped*/;
236 attr
.colormap
= X11DRV_PALETTE_PaletteXColormap
; /* Needed due to our visual */
239 data
->icon_window
= XCreateWindow( display
, root_window
, 0, 0,
240 GetSystemMetrics( SM_CXICON
),
241 GetSystemMetrics( SM_CYICON
),
244 CWEventMask
| CWBitGravity
| CWBackingStore
| CWColormap
, &attr
);
245 XSaveContext( display
, data
->icon_window
, winContext
, (char *)data
->hwnd
);
248 TRACE( "created %lx\n", data
->icon_window
);
249 SetPropA( data
->hwnd
, icon_window_prop
, (HANDLE
)data
->icon_window
);
250 return data
->icon_window
;
255 /***********************************************************************
256 * destroy_icon_window
258 static void destroy_icon_window( Display
*display
, struct x11drv_win_data
*data
)
260 if (!data
->icon_window
) return;
261 if (x11drv_thread_data()->cursor_window
== data
->icon_window
)
262 x11drv_thread_data()->cursor_window
= None
;
264 XDeleteContext( display
, data
->icon_window
, winContext
);
265 XDestroyWindow( display
, data
->icon_window
);
266 data
->icon_window
= 0;
268 RemovePropA( data
->hwnd
, icon_window_prop
);
272 /***********************************************************************
275 * Set the icon wm hints
277 static void set_icon_hints( Display
*display
, struct x11drv_win_data
*data
, HICON hIcon
)
279 XWMHints
*hints
= data
->wm_hints
;
281 if (data
->hWMIconBitmap
) DeleteObject( data
->hWMIconBitmap
);
282 if (data
->hWMIconMask
) DeleteObject( data
->hWMIconMask
);
283 data
->hWMIconBitmap
= 0;
284 data
->hWMIconMask
= 0;
288 destroy_icon_window( display
, data
);
289 hints
->flags
&= ~(IconPixmapHint
| IconMaskHint
| IconWindowHint
);
293 if (!data
->icon_window
) create_icon_window( display
, data
);
294 hints
->icon_window
= data
->icon_window
;
295 hints
->flags
= (hints
->flags
& ~(IconPixmapHint
| IconMaskHint
)) | IconWindowHint
;
305 GetIconInfo(hIcon
, &ii
);
307 GetObjectA(ii
.hbmMask
, sizeof(bmMask
), &bmMask
);
310 rcMask
.right
= bmMask
.bmWidth
;
311 rcMask
.bottom
= bmMask
.bmHeight
;
313 hDC
= CreateCompatibleDC(0);
314 hbmOrig
= SelectObject(hDC
, ii
.hbmMask
);
315 InvertRect(hDC
, &rcMask
);
316 SelectObject(hDC
, ii
.hbmColor
); /* force the color bitmap to x11drv mode too */
317 SelectObject(hDC
, hbmOrig
);
320 data
->hWMIconBitmap
= ii
.hbmColor
;
321 data
->hWMIconMask
= ii
.hbmMask
;
323 hints
->icon_pixmap
= X11DRV_get_pixmap(data
->hWMIconBitmap
);
324 hints
->icon_mask
= X11DRV_get_pixmap(data
->hWMIconMask
);
325 destroy_icon_window( display
, data
);
326 hints
->flags
= (hints
->flags
& ~IconWindowHint
) | IconPixmapHint
| IconMaskHint
;
330 /***********************************************************************
331 * systray_dock_window
333 * Docks the given X window with the NETWM system tray.
335 static void systray_dock_window( Display
*display
, struct x11drv_win_data
*data
)
337 static Atom systray_atom
;
338 Window systray_window
;
343 if (DefaultScreen( display
) == 0)
344 systray_atom
= x11drv_atom(_NET_SYSTEM_TRAY_S0
);
347 char systray_buffer
[29]; /* strlen(_NET_SYSTEM_TRAY_S4294967295)+1 */
348 sprintf( systray_buffer
, "_NET_SYSTEM_TRAY_S%u", DefaultScreen( display
) );
349 systray_atom
= XInternAtom( display
, systray_buffer
, False
);
352 systray_window
= XGetSelectionOwner( display
, systray_atom
);
355 TRACE("Docking tray icon %p\n", data
->hwnd
);
357 if (systray_window
!= None
)
360 unsigned long info
[2];
362 /* Put the window offscreen so it isn't mapped. The window _cannot_ be
363 * mapped if we intend to dock with an XEMBED tray. If the window is
364 * mapped when we dock, it may become visible as a child of the root
365 * window after it docks, which isn't the proper behavior.
367 * For more information on this problem, see
368 * http://standards.freedesktop.org/xembed-spec/latest/ar01s04.html */
370 SetWindowPos( data
->hwnd
, NULL
, virtual_screen_rect
.right
+ 1, virtual_screen_rect
.bottom
+ 1,
371 0, 0, SWP_NOZORDER
| SWP_NOSIZE
);
373 /* set XEMBED protocol data on the window */
374 info
[0] = 0; /* protocol version */
375 info
[1] = 1; /* mapped = true */
378 XChangeProperty( display
, data
->whole_window
,
379 x11drv_atom(_XEMBED_INFO
),
380 x11drv_atom(_XEMBED_INFO
), 32, PropModeReplace
,
381 (unsigned char*)info
, 2 );
384 /* send the docking request message */
385 ZeroMemory( &ev
, sizeof(ev
) );
386 ev
.xclient
.type
= ClientMessage
;
387 ev
.xclient
.window
= systray_window
;
388 ev
.xclient
.message_type
= x11drv_atom( _NET_SYSTEM_TRAY_OPCODE
);
389 ev
.xclient
.format
= 32;
390 ev
.xclient
.data
.l
[0] = CurrentTime
;
391 ev
.xclient
.data
.l
[1] = SYSTEM_TRAY_REQUEST_DOCK
;
392 ev
.xclient
.data
.l
[2] = data
->whole_window
;
393 ev
.xclient
.data
.l
[3] = 0;
394 ev
.xclient
.data
.l
[4] = 0;
397 XSendEvent( display
, systray_window
, False
, NoEventMask
, &ev
);
405 /* fall back to he KDE hints if the WM doesn't support XEMBED'ed
409 XChangeProperty( display
, data
->whole_window
,
410 x11drv_atom(KWM_DOCKWINDOW
),
411 x11drv_atom(KWM_DOCKWINDOW
), 32, PropModeReplace
,
412 (unsigned char*)&val
, 1 );
413 XChangeProperty( display
, data
->whole_window
,
414 x11drv_atom(_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR
),
415 XA_WINDOW
, 32, PropModeReplace
,
416 (unsigned char*)&data
->whole_window
, 1 );
422 /***********************************************************************
425 * set the window size hints
427 static void set_size_hints( Display
*display
, struct x11drv_win_data
*data
, DWORD style
)
429 XSizeHints
* size_hints
;
431 if ((size_hints
= XAllocSizeHints()))
433 size_hints
->flags
= 0;
435 if (data
->hwnd
!= GetDesktopWindow()) /* don't force position of desktop */
437 size_hints
->win_gravity
= StaticGravity
;
438 size_hints
->x
= data
->whole_rect
.left
;
439 size_hints
->y
= data
->whole_rect
.top
;
440 size_hints
->flags
|= PWinGravity
| PPosition
;
443 if ( !(style
& WS_THICKFRAME
) )
445 size_hints
->max_width
= data
->whole_rect
.right
- data
->whole_rect
.left
;
446 size_hints
->max_height
= data
->whole_rect
.bottom
- data
->whole_rect
.top
;
447 size_hints
->min_width
= size_hints
->max_width
;
448 size_hints
->min_height
= size_hints
->max_height
;
449 size_hints
->flags
|= PMinSize
| PMaxSize
;
451 XSetWMNormalHints( display
, data
->whole_window
, size_hints
);
457 /***********************************************************************
460 * get the name of the current process for setting class hints
462 static char *get_process_name(void)
468 WCHAR module
[MAX_PATH
];
469 DWORD len
= GetModuleFileNameW( 0, module
, MAX_PATH
);
470 if (len
&& len
< MAX_PATH
)
473 WCHAR
*p
, *appname
= module
;
475 if ((p
= strrchrW( appname
, '/' ))) appname
= p
+ 1;
476 if ((p
= strrchrW( appname
, '\\' ))) appname
= p
+ 1;
477 len
= WideCharToMultiByte( CP_UNIXCP
, 0, appname
, -1, NULL
, 0, NULL
, NULL
);
478 if ((ptr
= HeapAlloc( GetProcessHeap(), 0, len
)))
480 WideCharToMultiByte( CP_UNIXCP
, 0, appname
, -1, ptr
, len
, NULL
, NULL
);
489 /***********************************************************************
490 * set_initial_wm_hints
492 * Set the window manager hints that don't change over the lifetime of a window.
494 static void set_initial_wm_hints( Display
*display
, struct x11drv_win_data
*data
)
499 XClassHint
*class_hints
;
500 char *process_name
= get_process_name();
506 protocols
[i
++] = x11drv_atom(WM_DELETE_WINDOW
);
507 protocols
[i
++] = x11drv_atom(_NET_WM_PING
);
508 if (use_take_focus
) protocols
[i
++] = x11drv_atom(WM_TAKE_FOCUS
);
509 XChangeProperty( display
, data
->whole_window
, x11drv_atom(WM_PROTOCOLS
),
510 XA_ATOM
, 32, PropModeReplace
, (unsigned char *)protocols
, i
);
513 if ((class_hints
= XAllocClassHint()))
515 static char wine
[] = "Wine";
517 class_hints
->res_name
= process_name
;
518 class_hints
->res_class
= wine
;
519 XSetClassHint( display
, data
->whole_window
, class_hints
);
520 XFree( class_hints
);
523 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
524 XSetWMProperties(display
, data
->whole_window
, NULL
, NULL
, NULL
, 0, NULL
, NULL
, NULL
);
525 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
527 XChangeProperty(display
, data
->whole_window
, x11drv_atom(_NET_WM_PID
),
528 XA_CARDINAL
, 32, PropModeReplace
, (unsigned char *)&i
, 1);
530 XChangeProperty( display
, data
->whole_window
, x11drv_atom(XdndAware
),
531 XA_ATOM
, 32, PropModeReplace
, (unsigned char*)&dndVersion
, 1 );
537 /***********************************************************************
538 * X11DRV_set_wm_hints
540 * Set the window manager hints for a newly-created window
542 void X11DRV_set_wm_hints( Display
*display
, struct x11drv_win_data
*data
)
547 DWORD style
= GetWindowLongW( data
->hwnd
, GWL_STYLE
);
548 DWORD ex_style
= GetWindowLongW( data
->hwnd
, GWL_EXSTYLE
);
549 HWND owner
= GetWindow( data
->hwnd
, GW_OWNER
);
551 if (data
->hwnd
== GetDesktopWindow())
553 if (data
->whole_window
== DefaultRootWindow(display
)) return;
554 /* force some styles for the desktop to get the correct decorations */
555 style
|= WS_CAPTION
| WS_SYSMENU
| WS_MINIMIZEBOX
;
559 /* transient for hint */
562 Window owner_win
= X11DRV_get_whole_window( owner
);
564 XSetTransientForHint( display
, data
->whole_window
, owner_win
);
566 group_leader
= owner_win
;
568 else group_leader
= data
->whole_window
;
573 set_size_hints( display
, data
, style
);
575 /* set the WM_WINDOW_TYPE */
576 window_type
= x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL
);
577 if (ex_style
& WS_EX_TOOLWINDOW
) window_type
= x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY
);
578 else if (style
& WS_THICKFRAME
) window_type
= x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL
);
579 else if (style
& WS_DLGFRAME
) window_type
= x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG
);
580 else if (ex_style
& WS_EX_DLGMODALFRAME
) window_type
= x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG
);
582 XChangeProperty(display
, data
->whole_window
, x11drv_atom(_NET_WM_WINDOW_TYPE
),
583 XA_ATOM
, 32, PropModeReplace
, (unsigned char*)&window_type
, 1);
585 mwm_hints
.flags
= MWM_HINTS_FUNCTIONS
| MWM_HINTS_DECORATIONS
;
586 mwm_hints
.functions
= MWM_FUNC_MOVE
;
587 if (style
& WS_THICKFRAME
) mwm_hints
.functions
|= MWM_FUNC_RESIZE
;
588 if (style
& WS_MINIMIZEBOX
) mwm_hints
.functions
|= MWM_FUNC_MINIMIZE
;
589 if (style
& WS_MAXIMIZEBOX
) mwm_hints
.functions
|= MWM_FUNC_MAXIMIZE
;
590 if (style
& WS_SYSMENU
) mwm_hints
.functions
|= MWM_FUNC_CLOSE
;
591 mwm_hints
.decorations
= 0;
592 if ((style
& WS_CAPTION
) == WS_CAPTION
)
594 mwm_hints
.decorations
|= MWM_DECOR_TITLE
;
595 if (style
& WS_SYSMENU
) mwm_hints
.decorations
|= MWM_DECOR_MENU
;
596 if (style
& WS_MINIMIZEBOX
) mwm_hints
.decorations
|= MWM_DECOR_MINIMIZE
;
597 if (style
& WS_MAXIMIZEBOX
) mwm_hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
599 if (ex_style
& WS_EX_DLGMODALFRAME
) mwm_hints
.decorations
|= MWM_DECOR_BORDER
;
600 else if (style
& WS_THICKFRAME
) mwm_hints
.decorations
|= MWM_DECOR_BORDER
| MWM_DECOR_RESIZEH
;
601 else if ((style
& (WS_DLGFRAME
|WS_BORDER
)) == WS_DLGFRAME
) mwm_hints
.decorations
|= MWM_DECOR_BORDER
;
602 else if (style
& WS_BORDER
) mwm_hints
.decorations
|= MWM_DECOR_BORDER
;
603 else if (!(style
& (WS_CHILD
|WS_POPUP
))) mwm_hints
.decorations
|= MWM_DECOR_BORDER
;
605 XChangeProperty( display
, data
->whole_window
, x11drv_atom(_MOTIF_WM_HINTS
),
606 x11drv_atom(_MOTIF_WM_HINTS
), 32, PropModeReplace
,
607 (unsigned char*)&mwm_hints
, sizeof(mwm_hints
)/sizeof(long) );
614 data
->wm_hints
->flags
= InputHint
| StateHint
| WindowGroupHint
;
615 data
->wm_hints
->input
= !(style
& WS_DISABLED
);
616 data
->wm_hints
->initial_state
= (style
& WS_MINIMIZE
) ? IconicState
: NormalState
;
617 data
->wm_hints
->window_group
= group_leader
;
618 set_icon_hints( display
, data
, (HICON
)GetClassLongPtrW( data
->hwnd
, GCLP_HICON
) );
621 XSetWMHints( display
, data
->whole_window
, data
->wm_hints
);
627 /***********************************************************************
628 * X11DRV_set_iconic_state
630 * Set the X11 iconic state according to the window style.
632 void X11DRV_set_iconic_state( HWND hwnd
)
634 Display
*display
= thread_display();
635 struct x11drv_win_data
*data
;
637 DWORD style
= GetWindowLongW( hwnd
, GWL_STYLE
);
638 BOOL iconic
= (style
& WS_MINIMIZE
) != 0;
640 if (!(data
= X11DRV_get_win_data( hwnd
))) return;
641 if (!data
->whole_window
|| data
->whole_window
== DefaultRootWindow(display
)) return;
643 GetWindowRect( hwnd
, &rect
);
649 data
->wm_hints
->flags
|= StateHint
| IconPositionHint
;
650 data
->wm_hints
->initial_state
= iconic
? IconicState
: NormalState
;
651 data
->wm_hints
->icon_x
= rect
.left
- virtual_screen_rect
.left
;
652 data
->wm_hints
->icon_y
= rect
.top
- virtual_screen_rect
.top
;
653 XSetWMHints( display
, data
->whole_window
, data
->wm_hints
);
656 if (style
& WS_VISIBLE
)
659 XIconifyWindow( display
, data
->whole_window
, DefaultScreen(display
) );
661 if (X11DRV_is_window_rect_mapped( &rect
))
662 XMapWindow( display
, data
->whole_window
);
669 /***********************************************************************
670 * X11DRV_window_to_X_rect
672 * Convert a rect from client to X window coordinates
674 void X11DRV_window_to_X_rect( struct x11drv_win_data
*data
, RECT
*rect
)
678 if (!data
->managed
) return;
679 if (IsRectEmpty( rect
)) return;
681 rc
.top
= rc
.bottom
= rc
.left
= rc
.right
= 0;
683 AdjustWindowRectEx( &rc
, GetWindowLongW( data
->hwnd
, GWL_STYLE
) & ~(WS_HSCROLL
|WS_VSCROLL
),
684 FALSE
, GetWindowLongW( data
->hwnd
, GWL_EXSTYLE
) );
686 rect
->left
-= rc
.left
;
687 rect
->right
-= rc
.right
;
689 rect
->bottom
-= rc
.bottom
;
690 if (rect
->top
>= rect
->bottom
) rect
->bottom
= rect
->top
+ 1;
691 if (rect
->left
>= rect
->right
) rect
->right
= rect
->left
+ 1;
695 /***********************************************************************
696 * X11DRV_X_to_window_rect
698 * Opposite of X11DRV_window_to_X_rect
700 void X11DRV_X_to_window_rect( struct x11drv_win_data
*data
, RECT
*rect
)
702 if (!data
->managed
) return;
703 if (IsRectEmpty( rect
)) return;
705 AdjustWindowRectEx( rect
, GetWindowLongW( data
->hwnd
, GWL_STYLE
) & ~(WS_HSCROLL
|WS_VSCROLL
),
706 FALSE
, GetWindowLongW( data
->hwnd
, GWL_EXSTYLE
));
708 if (rect
->top
>= rect
->bottom
) rect
->bottom
= rect
->top
+ 1;
709 if (rect
->left
>= rect
->right
) rect
->right
= rect
->left
+ 1;
713 /***********************************************************************
714 * X11DRV_sync_window_position
716 * Synchronize the X window position with the Windows one
718 void X11DRV_sync_window_position( Display
*display
, struct x11drv_win_data
*data
,
719 UINT swp_flags
, const RECT
*new_client_rect
,
720 const RECT
*new_whole_rect
)
722 XWindowChanges changes
;
726 old_whole_rect
= data
->whole_rect
;
727 data
->whole_rect
= *new_whole_rect
;
729 data
->client_rect
= *new_client_rect
;
730 OffsetRect( &data
->client_rect
, -data
->whole_rect
.left
, -data
->whole_rect
.top
);
732 if (!data
->whole_window
|| data
->lock_changes
) return;
734 mask
= get_window_changes( &changes
, &old_whole_rect
, &data
->whole_rect
);
736 if (!(swp_flags
& SWP_NOZORDER
))
738 /* find window that this one must be after */
739 HWND prev
= GetWindow( data
->hwnd
, GW_HWNDPREV
);
740 while (prev
&& !(GetWindowLongW( prev
, GWL_STYLE
) & WS_VISIBLE
))
741 prev
= GetWindow( prev
, GW_HWNDPREV
);
742 if (!prev
) /* top child */
744 changes
.stack_mode
= Above
;
749 /* should use stack_mode Below but most window managers don't get it right */
750 /* so move it above the next one in Z order */
751 HWND next
= GetWindow( data
->hwnd
, GW_HWNDNEXT
);
752 while (next
&& !(GetWindowLongW( next
, GWL_STYLE
) & WS_VISIBLE
))
753 next
= GetWindow( next
, GW_HWNDNEXT
);
756 changes
.stack_mode
= Above
;
757 changes
.sibling
= X11DRV_get_whole_window(next
);
758 mask
|= CWStackMode
| CWSibling
;
765 DWORD style
= GetWindowLongW( data
->hwnd
, GWL_STYLE
);
767 TRACE( "setting win %lx pos %d,%d,%dx%d after %lx changes=%x\n",
768 data
->whole_window
, data
->whole_rect
.left
, data
->whole_rect
.top
,
769 data
->whole_rect
.right
- data
->whole_rect
.left
,
770 data
->whole_rect
.bottom
- data
->whole_rect
.top
, changes
.sibling
, mask
);
773 if (mask
& (CWWidth
|CWHeight
)) set_size_hints( display
, data
, style
);
774 if (mask
& CWX
) changes
.x
-= virtual_screen_rect
.left
;
775 if (mask
& CWY
) changes
.y
-= virtual_screen_rect
.top
;
776 XReconfigureWMWindow( display
, data
->whole_window
,
777 DefaultScreen(display
), mask
, &changes
);
783 /**********************************************************************
784 * create_whole_window
786 * Create the whole X window for a given window
788 static Window
create_whole_window( Display
*display
, struct x11drv_win_data
*data
, DWORD style
)
791 XSetWindowAttributes attr
;
795 rect
= data
->window_rect
;
796 X11DRV_window_to_X_rect( data
, &rect
);
798 if (!(cx
= rect
.right
- rect
.left
)) cx
= 1;
799 if (!(cy
= rect
.bottom
- rect
.top
)) cy
= 1;
801 mask
= get_window_attributes( display
, data
, &attr
);
805 data
->whole_rect
= rect
;
806 data
->whole_window
= XCreateWindow( display
, root_window
,
807 rect
.left
- virtual_screen_rect
.left
,
808 rect
.top
- virtual_screen_rect
.top
,
809 cx
, cy
, 0, screen_depth
, InputOutput
,
810 visual
, mask
, &attr
);
812 if (!data
->whole_window
)
817 XSaveContext( display
, data
->whole_window
, winContext
, (char *)data
->hwnd
);
819 /* non-maximized child must be at bottom of Z order */
820 if ((style
& (WS_CHILD
|WS_MAXIMIZE
)) == WS_CHILD
)
822 XWindowChanges changes
;
823 changes
.stack_mode
= Below
;
824 XConfigureWindow( display
, data
->whole_window
, CWStackMode
, &changes
);
828 xim
= x11drv_thread_data()->xim
;
829 if (xim
) data
->xic
= X11DRV_CreateIC( xim
, display
, data
->whole_window
);
831 set_initial_wm_hints( display
, data
);
832 X11DRV_set_wm_hints( display
, data
);
834 SetPropA( data
->hwnd
, whole_window_prop
, (HANDLE
)data
->whole_window
);
835 return data
->whole_window
;
839 /**********************************************************************
840 * destroy_whole_window
842 * Destroy the whole X window for a given window.
844 static void destroy_whole_window( Display
*display
, struct x11drv_win_data
*data
)
846 struct x11drv_thread_data
*thread_data
= x11drv_thread_data();
848 if (!data
->whole_window
) return;
850 TRACE( "win %p xwin %lx\n", data
->hwnd
, data
->whole_window
);
851 if (thread_data
->cursor_window
== data
->whole_window
) thread_data
->cursor_window
= None
;
853 XDeleteContext( display
, data
->whole_window
, winContext
);
854 if (data
->whole_window
!= DefaultRootWindow(display
))
855 XDestroyWindow( display
, data
->whole_window
);
856 data
->whole_window
= 0;
859 XUnsetICFocus( data
->xic
);
860 XDestroyIC( data
->xic
);
862 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
865 RemovePropA( data
->hwnd
, whole_window_prop
);
869 /*****************************************************************
870 * SetWindowText (X11DRV.@)
872 void X11DRV_SetWindowText( HWND hwnd
, LPCWSTR text
)
874 Display
*display
= thread_display();
881 if ((win
= X11DRV_get_whole_window( hwnd
)) && win
!= DefaultRootWindow(display
))
883 /* allocate new buffer for window text */
884 count
= WideCharToMultiByte(CP_UNIXCP
, 0, text
, -1, NULL
, 0, NULL
, NULL
);
885 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, count
)))
887 ERR("Not enough memory for window text\n");
890 WideCharToMultiByte(CP_UNIXCP
, 0, text
, -1, buffer
, count
, NULL
, NULL
);
892 count
= WideCharToMultiByte(CP_UTF8
, 0, text
, strlenW(text
), NULL
, 0, NULL
, NULL
);
893 if (!(utf8_buffer
= HeapAlloc( GetProcessHeap(), 0, count
)))
895 ERR("Not enough memory for window text in UTF-8\n");
896 HeapFree( GetProcessHeap(), 0, buffer
);
899 WideCharToMultiByte(CP_UTF8
, 0, text
, strlenW(text
), utf8_buffer
, count
, NULL
, NULL
);
902 if (XmbTextListToTextProperty( display
, &buffer
, 1, XStdICCTextStyle
, &prop
) == Success
)
904 XSetWMName( display
, win
, &prop
);
905 XSetWMIconName( display
, win
, &prop
);
909 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
910 according to the standard
911 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
913 XChangeProperty( display
, win
, x11drv_atom(_NET_WM_NAME
), x11drv_atom(UTF8_STRING
),
914 8, PropModeReplace
, (unsigned char *) utf8_buffer
, count
);
917 HeapFree( GetProcessHeap(), 0, utf8_buffer
);
918 HeapFree( GetProcessHeap(), 0, buffer
);
923 /***********************************************************************
924 * DestroyWindow (X11DRV.@)
926 void X11DRV_DestroyWindow( HWND hwnd
)
928 struct x11drv_thread_data
*thread_data
= x11drv_thread_data();
929 Display
*display
= thread_data
->display
;
930 struct x11drv_win_data
*data
;
932 if (!(data
= X11DRV_get_win_data( hwnd
))) return;
934 free_window_dce( data
);
935 destroy_whole_window( display
, data
);
936 destroy_icon_window( display
, data
);
938 if (thread_data
->last_focus
== hwnd
) thread_data
->last_focus
= 0;
939 if (data
->hWMIconBitmap
) DeleteObject( data
->hWMIconBitmap
);
940 if (data
->hWMIconMask
) DeleteObject( data
->hWMIconMask
);
942 XDeleteContext( display
, (XID
)hwnd
, win_data_context
);
943 XFree( data
->wm_hints
);
945 HeapFree( GetProcessHeap(), 0, data
);
949 static struct x11drv_win_data
*alloc_win_data( Display
*display
, HWND hwnd
)
951 struct x11drv_win_data
*data
;
953 if ((data
= HeapAlloc(GetProcessHeap(), 0, sizeof(*data
))))
956 data
->whole_window
= 0;
957 data
->icon_window
= 0;
959 data
->managed
= FALSE
;
961 data
->lock_changes
= 0;
962 data
->hWMIconBitmap
= 0;
963 data
->hWMIconMask
= 0;
966 if (!winContext
) winContext
= XUniqueContext();
967 if (!win_data_context
) win_data_context
= XUniqueContext();
968 XSaveContext( display
, (XID
)hwnd
, win_data_context
, (char *)data
);
969 data
->wm_hints
= XAllocWMHints();
976 /* fill in the desktop X window id in the x11drv_win_data structure */
977 static void get_desktop_xwin( Display
*display
, struct x11drv_win_data
*data
)
979 Window win
= (Window
)GetPropA( data
->hwnd
, whole_window_prop
);
983 unsigned int width
, height
;
985 /* retrieve the real size of the desktop */
986 SERVER_START_REQ( get_window_rectangles
)
988 req
->handle
= data
->hwnd
;
989 wine_server_call( req
);
990 width
= reply
->window
.right
- reply
->window
.left
;
991 height
= reply
->window
.bottom
- reply
->window
.top
;
994 data
->whole_window
= win
;
995 if (win
!= root_window
) X11DRV_init_desktop( win
, width
, height
);
1002 visualid
= XVisualIDFromVisual(visual
);
1003 wine_tsx11_unlock();
1004 SetPropA( data
->hwnd
, whole_window_prop
, (HANDLE
)root_window
);
1005 SetPropA( data
->hwnd
, visual_id_prop
, (HANDLE
)visualid
);
1006 data
->whole_window
= root_window
;
1007 X11DRV_SetWindowPos( data
->hwnd
, 0, &virtual_screen_rect
, &virtual_screen_rect
,
1008 SWP_NOZORDER
, NULL
);
1009 if (root_window
!= DefaultRootWindow( display
))
1011 data
->managed
= TRUE
;
1012 SetPropA( data
->hwnd
, managed_prop
, (HANDLE
)1 );
1017 /**********************************************************************
1018 * CreateDesktopWindow (X11DRV.@)
1020 BOOL
X11DRV_CreateDesktopWindow( HWND hwnd
)
1022 Display
*display
= thread_display();
1023 struct x11drv_win_data
*data
;
1025 if (!(data
= alloc_win_data( display
, hwnd
))) return FALSE
;
1027 get_desktop_xwin( display
, data
);
1033 /**********************************************************************
1034 * CreateWindow (X11DRV.@)
1036 BOOL
X11DRV_CreateWindow( HWND hwnd
, CREATESTRUCTA
*cs
, BOOL unicode
)
1038 Display
*display
= thread_display();
1040 struct x11drv_win_data
*data
;
1044 CBT_CREATEWNDA cbtc
;
1048 if (!(data
= alloc_win_data( display
, hwnd
))) return FALSE
;
1052 ERR( "invalid window width %d\n", cs
->cx
);
1057 ERR( "invalid window height %d\n", cs
->cy
);
1062 ERR( "invalid window width %d\n", cs
->cx
);
1067 ERR( "invalid window height %d\n", cs
->cy
);
1071 /* initialize the dimensions before sending WM_GETMINMAXINFO */
1072 SetRect( &rect
, cs
->x
, cs
->y
, cs
->x
+ cs
->cx
, cs
->y
+ cs
->cy
);
1073 X11DRV_SetWindowPos( hwnd
, 0, &rect
, &rect
, SWP_NOZORDER
, NULL
);
1075 /* create an X window if it's a top level window */
1076 if (GetAncestor( hwnd
, GA_PARENT
) == GetDesktopWindow())
1078 if (!create_whole_window( display
, data
, cs
->style
)) goto failed
;
1080 else if (hwnd
== GetDesktopWindow())
1082 get_desktop_xwin( display
, data
);
1085 /* get class or window DC if needed */
1086 alloc_window_dce( data
);
1088 /* Call the WH_CBT hook */
1090 /* the window style passed to the hook must be the real window style,
1091 * rather than just the window style that the caller to CreateWindowEx
1092 * passed in, so we have to copy the original CREATESTRUCT and get the
1093 * the real style. */
1095 cbcs
.style
= GetWindowLongW(hwnd
, GWL_STYLE
);
1098 cbtc
.hwndInsertAfter
= HWND_TOP
;
1099 if (HOOK_CallHooks( WH_CBT
, HCBT_CREATEWND
, (WPARAM
)hwnd
, (LPARAM
)&cbtc
, unicode
))
1101 TRACE("CBT-hook returned !0\n");
1105 /* Send the WM_GETMINMAXINFO message and fix the size if needed */
1106 if ((cs
->style
& WS_THICKFRAME
) || !(cs
->style
& (WS_POPUP
| WS_CHILD
)))
1108 POINT maxSize
, maxPos
, minTrack
, maxTrack
;
1110 WINPOS_GetMinMaxInfo( hwnd
, &maxSize
, &maxPos
, &minTrack
, &maxTrack
);
1111 if (maxTrack
.x
< cs
->cx
) cs
->cx
= maxTrack
.x
;
1112 if (maxTrack
.y
< cs
->cy
) cs
->cy
= maxTrack
.y
;
1113 if (cs
->cx
< 0) cs
->cx
= 0;
1114 if (cs
->cy
< 0) cs
->cy
= 0;
1116 SetRect( &rect
, cs
->x
, cs
->y
, cs
->x
+ cs
->cx
, cs
->y
+ cs
->cy
);
1117 if (!X11DRV_SetWindowPos( hwnd
, 0, &rect
, &rect
, SWP_NOZORDER
, NULL
)) return FALSE
;
1120 /* send WM_NCCREATE */
1121 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd
, cs
->x
, cs
->y
, cs
->cx
, cs
->cy
);
1123 ret
= SendMessageW( hwnd
, WM_NCCREATE
, 0, (LPARAM
)cs
);
1125 ret
= SendMessageA( hwnd
, WM_NCCREATE
, 0, (LPARAM
)cs
);
1128 WARN("aborted by WM_xxCREATE!\n");
1132 /* make sure the window is still valid */
1133 if (!(data
= X11DRV_get_win_data( hwnd
))) return FALSE
;
1134 if (data
->whole_window
) X11DRV_sync_window_style( display
, data
);
1136 /* send WM_NCCALCSIZE */
1137 rect
= data
->window_rect
;
1138 SendMessageW( hwnd
, WM_NCCALCSIZE
, FALSE
, (LPARAM
)&rect
);
1140 if (!(wndPtr
= WIN_GetPtr(hwnd
))) return FALSE
;
1142 /* yes, even if the CBT hook was called with HWND_TOP */
1143 insert_after
= (wndPtr
->dwStyle
& WS_CHILD
) ? HWND_BOTTOM
: HWND_TOP
;
1145 X11DRV_SetWindowPos( hwnd
, insert_after
, &wndPtr
->rectWindow
, &rect
, 0, NULL
);
1147 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",
1148 hwnd
, wndPtr
->rectWindow
.left
, wndPtr
->rectWindow
.top
,
1149 wndPtr
->rectWindow
.right
, wndPtr
->rectWindow
.bottom
,
1150 wndPtr
->rectClient
.left
, wndPtr
->rectClient
.top
,
1151 wndPtr
->rectClient
.right
, wndPtr
->rectClient
.bottom
,
1152 data
->whole_rect
.left
, data
->whole_rect
.top
,
1153 data
->whole_rect
.right
, data
->whole_rect
.bottom
,
1154 data
->client_rect
.left
, data
->client_rect
.top
,
1155 data
->client_rect
.right
, data
->client_rect
.bottom
,
1156 (unsigned int)data
->whole_window
);
1158 WIN_ReleasePtr( wndPtr
);
1161 ret
= (SendMessageW( hwnd
, WM_CREATE
, 0, (LPARAM
)cs
) != -1);
1163 ret
= (SendMessageA( hwnd
, WM_CREATE
, 0, (LPARAM
)cs
) != -1);
1165 if (!ret
) return FALSE
;
1167 NotifyWinEvent(EVENT_OBJECT_CREATE
, hwnd
, OBJID_WINDOW
, 0);
1169 /* Send the size messages */
1171 if (!(wndPtr
= WIN_GetPtr(hwnd
)) || wndPtr
== WND_OTHER_PROCESS
) return FALSE
;
1172 if (!(wndPtr
->flags
& WIN_NEED_SIZE
))
1174 RECT rect
= wndPtr
->rectClient
;
1175 WIN_ReleasePtr( wndPtr
);
1176 /* send it anyway */
1177 if (((rect
.right
-rect
.left
) <0) ||((rect
.bottom
-rect
.top
)<0))
1178 WARN("sending bogus WM_SIZE message 0x%08x\n",
1179 MAKELONG(rect
.right
-rect
.left
, rect
.bottom
-rect
.top
));
1180 SendMessageW( hwnd
, WM_SIZE
, SIZE_RESTORED
,
1181 MAKELONG(rect
.right
-rect
.left
, rect
.bottom
-rect
.top
));
1182 SendMessageW( hwnd
, WM_MOVE
, 0, MAKELONG( rect
.left
, rect
.top
) );
1184 else WIN_ReleasePtr( wndPtr
);
1186 /* Show the window, maximizing or minimizing if needed */
1188 style
= GetWindowLongW( hwnd
, GWL_STYLE
);
1189 if (style
& (WS_MINIMIZE
| WS_MAXIMIZE
))
1191 extern UINT
WINPOS_MinMaximize( HWND hwnd
, UINT cmd
, LPRECT rect
); /*FIXME*/
1194 UINT swFlag
= (style
& WS_MINIMIZE
) ? SW_MINIMIZE
: SW_MAXIMIZE
;
1195 WIN_SetStyle( hwnd
, 0, WS_MAXIMIZE
| WS_MINIMIZE
);
1196 swFlag
= WINPOS_MinMaximize( hwnd
, swFlag
, &newPos
);
1198 swFlag
|= SWP_FRAMECHANGED
; /* Frame always gets changed */
1199 if (!(style
& WS_VISIBLE
) || (style
& WS_CHILD
) || GetActiveWindow()) swFlag
|= SWP_NOACTIVATE
;
1201 SetWindowPos( hwnd
, 0, newPos
.left
, newPos
.top
,
1202 newPos
.right
, newPos
.bottom
, swFlag
);
1205 /* Dock system tray windows. */
1206 /* Dock after the window is created so we don't have problems calling
1208 if (GetWindowLongW( hwnd
, GWL_EXSTYLE
) & WS_EX_TRAYWINDOW
)
1209 systray_dock_window( display
, data
);
1214 X11DRV_DestroyWindow( hwnd
);
1219 /***********************************************************************
1220 * X11DRV_get_win_data
1222 * Return the X11 data structure associated with a window.
1224 struct x11drv_win_data
*X11DRV_get_win_data( HWND hwnd
)
1228 if (!hwnd
|| XFindContext( thread_display(), (XID
)hwnd
, win_data_context
, &data
)) data
= NULL
;
1229 return (struct x11drv_win_data
*)data
;
1233 /***********************************************************************
1234 * X11DRV_get_whole_window
1236 * Return the X window associated with the full area of a window
1238 Window
X11DRV_get_whole_window( HWND hwnd
)
1240 struct x11drv_win_data
*data
= X11DRV_get_win_data( hwnd
);
1242 if (!data
) return (Window
)GetPropA( hwnd
, whole_window_prop
);
1243 return data
->whole_window
;
1247 /***********************************************************************
1250 * Return the X input context associated with a window
1252 XIC
X11DRV_get_ic( HWND hwnd
)
1254 struct x11drv_win_data
*data
= X11DRV_get_win_data( hwnd
);
1256 if (!data
) return 0;
1261 /*****************************************************************
1262 * SetParent (X11DRV.@)
1264 void X11DRV_SetParent( HWND hwnd
, HWND parent
, HWND old_parent
)
1266 Display
*display
= thread_display();
1267 struct x11drv_win_data
*data
= X11DRV_get_win_data( hwnd
);
1270 if (parent
== old_parent
) return;
1272 if (parent
!= GetDesktopWindow()) /* a child window */
1274 if (old_parent
== GetDesktopWindow())
1276 /* destroy the old X windows */
1277 destroy_whole_window( display
, data
);
1278 destroy_icon_window( display
, data
);
1281 data
->managed
= FALSE
;
1282 RemovePropA( data
->hwnd
, managed_prop
);
1286 else /* new top level window */
1288 /* FIXME: we ignore errors since we can't really recover anyway */
1289 create_whole_window( display
, data
, GetWindowLongW( hwnd
, GWL_STYLE
) );
1294 /*****************************************************************
1295 * SetFocus (X11DRV.@)
1298 * Explicit colormap management seems to work only with OLVWM.
1300 void X11DRV_SetFocus( HWND hwnd
)
1302 Display
*display
= thread_display();
1303 struct x11drv_win_data
*data
;
1304 XWindowAttributes win_attr
;
1306 /* Only mess with the X focus if there's */
1307 /* no desktop window and if the window is not managed by the WM. */
1308 if (root_window
!= DefaultRootWindow(display
)) return;
1310 if (!hwnd
) /* If setting the focus to 0, uninstall the colormap */
1313 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_PRIVATE
)
1314 XUninstallColormap( display
, X11DRV_PALETTE_PaletteXColormap
);
1315 wine_tsx11_unlock();
1319 hwnd
= GetAncestor( hwnd
, GA_ROOT
);
1321 if (!(data
= X11DRV_get_win_data( hwnd
))) return;
1322 if (data
->managed
|| !data
->whole_window
) return;
1324 /* Set X focus and install colormap */
1326 if (XGetWindowAttributes( display
, data
->whole_window
, &win_attr
) &&
1327 (win_attr
.map_state
== IsViewable
))
1329 /* If window is not viewable, don't change anything */
1331 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1332 /* FIXME: this is not entirely correct */
1333 XSetInputFocus( display
, data
->whole_window
, RevertToParent
,
1335 GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1336 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_PRIVATE
)
1337 XInstallColormap( display
, X11DRV_PALETTE_PaletteXColormap
);
1339 wine_tsx11_unlock();
1343 /**********************************************************************
1344 * SetWindowIcon (X11DRV.@)
1346 * hIcon or hIconSm has changed (or is being initialised for the
1347 * first time). Complete the X11 driver-specific initialisation
1348 * and set the window hints.
1350 * This is not entirely correct, may need to create
1351 * an icon window and set the pixmap as a background
1353 void X11DRV_SetWindowIcon( HWND hwnd
, UINT type
, HICON icon
)
1355 Display
*display
= thread_display();
1356 struct x11drv_win_data
*data
;
1358 if (type
!= ICON_BIG
) return; /* nothing to do here */
1360 if (!(data
= X11DRV_get_win_data( hwnd
))) return;
1361 if (!data
->whole_window
) return;
1362 if (!data
->managed
) return;
1366 set_icon_hints( display
, data
, icon
);
1368 XSetWMHints( display
, data
->whole_window
, data
->wm_hints
);
1369 wine_tsx11_unlock();