2 * Window related functions
4 * Copyright 1993, 1994, 1995, 1996, 2001 Alexandre Julliard
5 * Copyright 1993 David Metcalfe
6 * Copyright 1995, 1996 Alex Korobka
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #include "wine/unicode.h"
36 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(x11drv
);
47 extern Pixmap
X11DRV_BITMAP_Pixmap( HBITMAP
);
49 #define HAS_DLGFRAME(style,exStyle) \
50 (((exStyle) & WS_EX_DLGMODALFRAME) || \
51 (((style) & WS_DLGFRAME) && !((style) & WS_THICKFRAME)))
53 /* X context to associate a hwnd to an X window */
54 XContext winContext
= 0;
56 Atom wmProtocols
= None
;
57 Atom wmDeleteWindow
= None
;
58 Atom wmTakeFocus
= None
;
59 Atom dndProtocol
= None
;
60 Atom dndSelection
= None
;
61 Atom wmChangeState
= None
;
63 Atom kwmDockWindow
= None
;
64 Atom _kde_net_wm_system_tray_window_for
= None
; /* KDE 2 Final */
66 static LPCSTR whole_window_atom
;
67 static LPCSTR client_window_atom
;
68 static LPCSTR icon_window_atom
;
70 /***********************************************************************
73 * Check if a given window should be managed
75 inline static BOOL
is_window_managed( WND
*win
)
77 if (!Options
.managed
) return FALSE
;
79 /* tray window is always managed */
80 if (win
->dwExStyle
& WS_EX_TRAYWINDOW
) return TRUE
;
81 /* child windows are not managed */
82 if (win
->dwStyle
& WS_CHILD
) return FALSE
;
83 /* tool windows are not managed */
84 if (win
->dwExStyle
& WS_EX_TOOLWINDOW
) return FALSE
;
85 /* windows with caption or thick frame are managed */
86 if ((win
->dwStyle
& WS_CAPTION
) == WS_CAPTION
) return TRUE
;
87 if (win
->dwStyle
& WS_THICKFRAME
) return TRUE
;
88 /* default: not managed */
93 /***********************************************************************
96 * Check if a given window is a top level X11 window
98 inline static BOOL
is_window_top_level( WND
*win
)
100 return (root_window
== DefaultRootWindow(gdi_display
) && win
->parent
== GetDesktopWindow());
104 /***********************************************************************
105 * is_client_window_mapped
107 * Check if the X client window should be mapped
109 inline static BOOL
is_client_window_mapped( WND
*win
)
111 struct x11drv_win_data
*data
= win
->pDriverData
;
112 return !(win
->dwStyle
& WS_MINIMIZE
) && !IsRectEmpty( &data
->client_rect
);
116 /***********************************************************************
117 * get_window_attributes
119 * Fill the window attributes structure for an X window.
120 * Returned cursor must be freed by caller.
122 static int get_window_attributes( Display
*display
, WND
*win
, XSetWindowAttributes
*attr
)
124 BOOL is_top_level
= is_window_top_level( win
);
125 BOOL managed
= is_top_level
&& is_window_managed( win
);
127 if (managed
) WIN_SetExStyle( win
->hwndSelf
, win
->dwExStyle
| WS_EX_MANAGED
);
128 else WIN_SetExStyle( win
->hwndSelf
, win
->dwExStyle
& ~WS_EX_MANAGED
);
130 attr
->override_redirect
= !managed
;
131 attr
->colormap
= X11DRV_PALETTE_PaletteXColormap
;
132 attr
->save_under
= ((win
->clsStyle
& CS_SAVEBITS
) != 0);
134 attr
->event_mask
= (ExposureMask
| KeyPressMask
| KeyReleaseMask
| PointerMotionMask
|
135 ButtonPressMask
| ButtonReleaseMask
);
136 if (is_window_top_level( win
))
138 attr
->event_mask
|= StructureNotifyMask
| FocusChangeMask
| KeymapStateMask
;
139 attr
->cursor
= X11DRV_GetCursor( display
, GlobalLock16(GetCursor()) );
141 return (CWOverrideRedirect
| CWSaveUnder
| CWEventMask
| CWColormap
| CWCursor
);
145 /***********************************************************************
148 * Change the X window attributes when the window style has changed.
150 static void sync_window_style( Display
*display
, WND
*win
)
152 XSetWindowAttributes attr
;
156 mask
= get_window_attributes( display
, win
, &attr
);
157 XChangeWindowAttributes( display
, get_whole_window(win
), mask
, &attr
);
158 if (attr
.cursor
) XFreeCursor( display
, attr
.cursor
);
163 /***********************************************************************
166 * fill the window changes structure
168 static int get_window_changes( XWindowChanges
*changes
, const RECT
*old
, const RECT
*new )
172 if (old
->right
- old
->left
!= new->right
- new->left
)
174 if (!(changes
->width
= new->right
- new->left
)) changes
->width
= 1;
177 if (old
->bottom
- old
->top
!= new->bottom
- new->top
)
179 if (!(changes
->height
= new->bottom
- new->top
)) changes
->height
= 1;
182 if (old
->left
!= new->left
)
184 changes
->x
= new->left
;
187 if (old
->top
!= new->top
)
189 changes
->y
= new->top
;
196 /***********************************************************************
199 static Window
create_icon_window( Display
*display
, WND
*win
)
201 struct x11drv_win_data
*data
= win
->pDriverData
;
202 XSetWindowAttributes attr
;
204 attr
.event_mask
= (ExposureMask
| KeyPressMask
| KeyReleaseMask
| PointerMotionMask
|
205 ButtonPressMask
| ButtonReleaseMask
);
206 attr
.bit_gravity
= NorthWestGravity
;
207 attr
.backing_store
= NotUseful
/*WhenMapped*/;
208 attr
.colormap
= X11DRV_PALETTE_PaletteXColormap
; /* Needed due to our visual */
211 data
->icon_window
= XCreateWindow( display
, root_window
, 0, 0,
212 GetSystemMetrics( SM_CXICON
),
213 GetSystemMetrics( SM_CYICON
),
216 CWEventMask
| CWBitGravity
| CWBackingStore
| CWColormap
, &attr
);
217 XSaveContext( display
, data
->icon_window
, winContext
, (char *)win
->hwndSelf
);
220 TRACE( "created %lx\n", data
->icon_window
);
221 SetPropA( win
->hwndSelf
, icon_window_atom
, (HANDLE
)data
->icon_window
);
222 return data
->icon_window
;
227 /***********************************************************************
228 * destroy_icon_window
230 inline static void destroy_icon_window( Display
*display
, WND
*win
)
232 struct x11drv_win_data
*data
= win
->pDriverData
;
234 if (!data
->icon_window
) return;
236 XDeleteContext( display
, data
->icon_window
, winContext
);
237 XDestroyWindow( display
, data
->icon_window
);
238 data
->icon_window
= 0;
240 RemovePropA( win
->hwndSelf
, icon_window_atom
);
244 /***********************************************************************
247 * Set the icon wm hints
249 static void set_icon_hints( Display
*display
, WND
*wndPtr
, XWMHints
*hints
)
251 X11DRV_WND_DATA
*data
= wndPtr
->pDriverData
;
252 HICON hIcon
= GetClassLongA( wndPtr
->hwndSelf
, GCL_HICON
);
254 if (data
->hWMIconBitmap
) DeleteObject( data
->hWMIconBitmap
);
255 if (data
->hWMIconMask
) DeleteObject( data
->hWMIconMask
);
256 data
->hWMIconBitmap
= 0;
257 data
->hWMIconMask
= 0;
259 if (!(wndPtr
->dwExStyle
& WS_EX_MANAGED
))
261 destroy_icon_window( display
, wndPtr
);
262 hints
->flags
&= ~(IconPixmapHint
| IconMaskHint
| IconWindowHint
);
266 if (!data
->icon_window
) create_icon_window( display
, wndPtr
);
267 hints
->icon_window
= data
->icon_window
;
268 hints
->flags
= (hints
->flags
& ~(IconPixmapHint
| IconMaskHint
)) | IconWindowHint
;
278 GetIconInfo(hIcon
, &ii
);
280 X11DRV_CreateBitmap(ii
.hbmMask
);
281 X11DRV_CreateBitmap(ii
.hbmColor
);
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
, hbmOrig
);
295 data
->hWMIconBitmap
= ii
.hbmColor
;
296 data
->hWMIconMask
= ii
.hbmMask
;
298 hints
->icon_pixmap
= X11DRV_BITMAP_Pixmap(data
->hWMIconBitmap
);
299 hints
->icon_mask
= X11DRV_BITMAP_Pixmap(data
->hWMIconMask
);
300 destroy_icon_window( display
, wndPtr
);
301 hints
->flags
= (hints
->flags
& ~IconWindowHint
) | IconPixmapHint
| IconMaskHint
;
306 /***********************************************************************
309 * set the window size hints
311 static void set_size_hints( Display
*display
, WND
*win
)
313 XSizeHints
* size_hints
;
314 struct x11drv_win_data
*data
= win
->pDriverData
;
316 if ((size_hints
= XAllocSizeHints()))
318 size_hints
->win_gravity
= StaticGravity
;
319 size_hints
->x
= data
->whole_rect
.left
;
320 size_hints
->y
= data
->whole_rect
.top
;
321 size_hints
->flags
= PWinGravity
| PPosition
;
323 if (HAS_DLGFRAME( win
->dwStyle
, win
->dwExStyle
))
325 size_hints
->max_width
= data
->whole_rect
.right
- data
->whole_rect
.left
;
326 size_hints
->max_height
= data
->whole_rect
.bottom
- data
->whole_rect
.top
;
327 size_hints
->min_width
= size_hints
->max_width
;
328 size_hints
->min_height
= size_hints
->max_height
;
329 size_hints
->flags
|= PMinSize
| PMaxSize
;
331 XSetWMNormalHints( display
, data
->whole_window
, size_hints
);
337 /***********************************************************************
340 * Set the window manager hints for a newly-created window
342 static void set_wm_hints( Display
*display
, WND
*win
)
344 struct x11drv_win_data
*data
= win
->pDriverData
;
346 XClassHint
*class_hints
;
355 protocols
[i
++] = wmDeleteWindow
;
356 if (wmTakeFocus
) protocols
[i
++] = wmTakeFocus
;
357 XSetWMProtocols( display
, data
->whole_window
, protocols
, i
);
360 if ((class_hints
= XAllocClassHint()))
362 class_hints
->res_name
= "wine";
363 class_hints
->res_class
= "Wine";
364 XSetClassHint( display
, data
->whole_window
, class_hints
);
365 XFree( class_hints
);
368 /* transient for hint */
371 Window owner_win
= X11DRV_get_whole_window( win
->owner
);
372 XSetTransientForHint( display
, data
->whole_window
, owner_win
);
373 group_leader
= owner_win
;
375 else group_leader
= data
->whole_window
;
378 set_size_hints( display
, win
);
380 /* systray properties (KDE only for now) */
381 if (win
->dwExStyle
& WS_EX_TRAYWINDOW
)
384 if (kwmDockWindow
!= None
)
385 XChangeProperty( display
, data
->whole_window
, kwmDockWindow
, kwmDockWindow
,
386 32, PropModeReplace
, (char*)&val
, 1 );
387 if (_kde_net_wm_system_tray_window_for
!= None
)
388 XChangeProperty( display
, data
->whole_window
, _kde_net_wm_system_tray_window_for
,
389 XA_WINDOW
, 32, PropModeReplace
, (char*)&data
->whole_window
, 1 );
392 if (mwmHints
!= None
)
395 mwm_hints
.flags
= MWM_HINTS_FUNCTIONS
| MWM_HINTS_DECORATIONS
;
396 mwm_hints
.functions
= 0;
397 if ((win
->dwStyle
& WS_CAPTION
) == WS_CAPTION
) mwm_hints
.functions
|= MWM_FUNC_MOVE
;
398 if (win
->dwStyle
& WS_THICKFRAME
) mwm_hints
.functions
|= MWM_FUNC_MOVE
| MWM_FUNC_RESIZE
;
399 if (win
->dwStyle
& WS_MINIMIZE
) mwm_hints
.functions
|= MWM_FUNC_MINIMIZE
;
400 if (win
->dwStyle
& WS_MAXIMIZE
) mwm_hints
.functions
|= MWM_FUNC_MAXIMIZE
;
401 if (win
->dwStyle
& WS_SYSMENU
) mwm_hints
.functions
|= MWM_FUNC_CLOSE
;
402 mwm_hints
.decorations
= 0;
403 if ((win
->dwStyle
& WS_CAPTION
) == WS_CAPTION
) mwm_hints
.decorations
|= MWM_DECOR_TITLE
;
404 if (win
->dwExStyle
& WS_EX_DLGMODALFRAME
) mwm_hints
.decorations
|= MWM_DECOR_BORDER
;
405 else if (win
->dwStyle
& WS_THICKFRAME
) mwm_hints
.decorations
|= MWM_DECOR_BORDER
| MWM_DECOR_RESIZEH
;
406 else if ((win
->dwStyle
& (WS_DLGFRAME
|WS_BORDER
)) == WS_DLGFRAME
) mwm_hints
.decorations
|= MWM_DECOR_BORDER
;
407 else if (win
->dwStyle
& WS_BORDER
) mwm_hints
.decorations
|= MWM_DECOR_BORDER
;
408 else if (!(win
->dwStyle
& (WS_CHILD
|WS_POPUP
))) mwm_hints
.decorations
|= MWM_DECOR_BORDER
;
409 if (win
->dwStyle
& WS_SYSMENU
) mwm_hints
.decorations
|= MWM_DECOR_MENU
;
410 if (win
->dwStyle
& WS_MINIMIZE
) mwm_hints
.decorations
|= MWM_DECOR_MINIMIZE
;
411 if (win
->dwStyle
& WS_MAXIMIZE
) mwm_hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
413 XChangeProperty( display
, data
->whole_window
, mwmHints
, mwmHints
, 32,
414 PropModeReplace
, (char*)&mwm_hints
, sizeof(mwm_hints
)/sizeof(long) );
420 if ((wm_hints
= TSXAllocWMHints()))
422 wm_hints
->flags
= InputHint
| StateHint
| WindowGroupHint
;
423 /* use globally active model if take focus is supported,
424 * passive model otherwise (cf. ICCCM) */
425 wm_hints
->input
= !wmTakeFocus
;
427 set_icon_hints( display
, win
, wm_hints
);
429 wm_hints
->initial_state
= (win
->dwStyle
& WS_MINIMIZE
) ? IconicState
: NormalState
;
430 wm_hints
->window_group
= group_leader
;
433 XSetWMHints( display
, data
->whole_window
, wm_hints
);
440 /***********************************************************************
441 * X11DRV_set_iconic_state
443 * Set the X11 iconic state according to the window style.
445 void X11DRV_set_iconic_state( WND
*win
)
447 Display
*display
= thread_display();
448 struct x11drv_win_data
*data
= win
->pDriverData
;
450 BOOL iconic
= IsIconic( win
->hwndSelf
);
454 if (iconic
) XUnmapWindow( display
, data
->client_window
);
455 else if (is_client_window_mapped( win
)) XMapWindow( display
, data
->client_window
);
457 if (!(wm_hints
= XGetWMHints( display
, data
->whole_window
))) wm_hints
= XAllocWMHints();
458 wm_hints
->flags
|= StateHint
| IconPositionHint
;
459 wm_hints
->initial_state
= iconic
? IconicState
: NormalState
;
460 wm_hints
->icon_x
= win
->rectWindow
.left
;
461 wm_hints
->icon_y
= win
->rectWindow
.top
;
462 XSetWMHints( display
, data
->whole_window
, wm_hints
);
464 if (win
->dwStyle
& WS_VISIBLE
)
467 XIconifyWindow( display
, data
->whole_window
, DefaultScreen(display
) );
469 if (!IsRectEmpty( &win
->rectWindow
)) XMapWindow( display
, data
->whole_window
);
477 /***********************************************************************
478 * X11DRV_window_to_X_rect
480 * Convert a rect from client to X window coordinates
482 void X11DRV_window_to_X_rect( WND
*win
, RECT
*rect
)
486 if (!(win
->dwExStyle
& WS_EX_MANAGED
)) return;
487 if (IsRectEmpty( rect
)) return;
489 rc
.top
= rc
.bottom
= rc
.left
= rc
.right
= 0;
491 AdjustWindowRectEx( &rc
, win
->dwStyle
& ~(WS_HSCROLL
|WS_VSCROLL
), FALSE
, win
->dwExStyle
);
493 rect
->left
-= rc
.left
;
494 rect
->right
-= rc
.right
;
496 rect
->bottom
-= rc
.bottom
;
497 if (rect
->top
>= rect
->bottom
) rect
->bottom
= rect
->top
+ 1;
498 if (rect
->left
>= rect
->right
) rect
->right
= rect
->left
+ 1;
502 /***********************************************************************
503 * X11DRV_X_to_window_rect
505 * Opposite of X11DRV_window_to_X_rect
507 void X11DRV_X_to_window_rect( WND
*win
, RECT
*rect
)
509 if (!(win
->dwExStyle
& WS_EX_MANAGED
)) return;
510 if (IsRectEmpty( rect
)) return;
512 AdjustWindowRectEx( rect
, win
->dwStyle
& ~(WS_HSCROLL
|WS_VSCROLL
), FALSE
, win
->dwExStyle
);
514 if (rect
->top
>= rect
->bottom
) rect
->bottom
= rect
->top
+ 1;
515 if (rect
->left
>= rect
->right
) rect
->right
= rect
->left
+ 1;
519 /***********************************************************************
520 * X11DRV_sync_whole_window_position
522 * Synchronize the X whole window position with the Windows one
524 int X11DRV_sync_whole_window_position( Display
*display
, WND
*win
, int zorder
)
526 XWindowChanges changes
;
528 struct x11drv_win_data
*data
= win
->pDriverData
;
529 RECT whole_rect
= win
->rectWindow
;
531 X11DRV_window_to_X_rect( win
, &whole_rect
);
532 mask
= get_window_changes( &changes
, &data
->whole_rect
, &whole_rect
);
536 /* find window that this one must be after */
537 HWND prev
= GetWindow( win
->hwndSelf
, GW_HWNDPREV
);
538 while (prev
&& !(GetWindowLongW( prev
, GWL_STYLE
) & WS_VISIBLE
))
539 prev
= GetWindow( prev
, GW_HWNDPREV
);
540 if (!prev
) /* top child */
542 changes
.stack_mode
= Above
;
547 changes
.stack_mode
= Below
;
548 changes
.sibling
= X11DRV_get_whole_window(prev
);
549 mask
|= CWStackMode
| CWSibling
;
553 data
->whole_rect
= whole_rect
;
557 TRACE( "setting win %lx pos %d,%d,%dx%d after %lx changes=%x\n",
558 data
->whole_window
, whole_rect
.left
, whole_rect
.top
,
559 whole_rect
.right
- whole_rect
.left
, whole_rect
.bottom
- whole_rect
.top
,
560 changes
.sibling
, mask
);
562 XSync( gdi_display
, False
); /* flush graphics operations before moving the window */
563 if (is_window_top_level( win
))
565 if (mask
& (CWWidth
|CWHeight
)) set_size_hints( display
, win
);
566 XReconfigureWMWindow( display
, data
->whole_window
,
567 DefaultScreen(display
), mask
, &changes
);
569 else XConfigureWindow( display
, data
->whole_window
, mask
, &changes
);
576 /***********************************************************************
577 * X11DRV_sync_client_window_position
579 * Synchronize the X client window position with the Windows one
581 int X11DRV_sync_client_window_position( Display
*display
, WND
*win
)
583 XWindowChanges changes
;
585 struct x11drv_win_data
*data
= win
->pDriverData
;
586 RECT client_rect
= win
->rectClient
;
588 OffsetRect( &client_rect
, -data
->whole_rect
.left
, -data
->whole_rect
.top
);
590 if ((mask
= get_window_changes( &changes
, &data
->client_rect
, &client_rect
)))
592 BOOL was_mapped
= is_client_window_mapped( win
);
594 TRACE( "setting win %lx pos %d,%d,%dx%d (was %d,%d,%dx%d) after %lx changes=%x\n",
595 data
->client_window
, client_rect
.left
, client_rect
.top
,
596 client_rect
.right
- client_rect
.left
, client_rect
.bottom
- client_rect
.top
,
597 data
->client_rect
.left
, data
->client_rect
.top
,
598 data
->client_rect
.right
- data
->client_rect
.left
,
599 data
->client_rect
.bottom
- data
->client_rect
.top
,
600 changes
.sibling
, mask
);
601 data
->client_rect
= client_rect
;
603 XSync( gdi_display
, False
); /* flush graphics operations before moving the window */
604 if (was_mapped
&& !is_client_window_mapped( win
))
605 XUnmapWindow( display
, data
->client_window
);
606 XConfigureWindow( display
, data
->client_window
, mask
, &changes
);
607 if (!was_mapped
&& is_client_window_mapped( win
))
608 XMapWindow( display
, data
->client_window
);
615 /***********************************************************************
616 * X11DRV_register_window
618 * Associate an X window to a HWND.
620 void X11DRV_register_window( Display
*display
, HWND hwnd
, struct x11drv_win_data
*data
)
623 XSaveContext( display
, data
->whole_window
, winContext
, (char *)hwnd
);
624 XSaveContext( display
, data
->client_window
, winContext
, (char *)hwnd
);
629 /**********************************************************************
632 static void create_desktop( Display
*display
, WND
*wndPtr
, CREATESTRUCTA
*cs
)
634 X11DRV_WND_DATA
*data
= wndPtr
->pDriverData
;
637 winContext
= XUniqueContext();
638 wmProtocols
= XInternAtom( display
, "WM_PROTOCOLS", False
);
639 wmDeleteWindow
= XInternAtom( display
, "WM_DELETE_WINDOW", False
);
640 /* wmTakeFocus = XInternAtom( display, "WM_TAKE_FOCUS", False );*/
641 wmTakeFocus
= 0; /* not yet */
642 dndProtocol
= XInternAtom( display
, "DndProtocol" , False
);
643 dndSelection
= XInternAtom( display
, "DndSelection" , False
);
644 wmChangeState
= XInternAtom( display
, "WM_CHANGE_STATE", False
);
645 mwmHints
= XInternAtom( display
, _XA_MWM_HINTS
, False
);
646 kwmDockWindow
= XInternAtom( display
, "KWM_DOCKWINDOW", False
);
647 _kde_net_wm_system_tray_window_for
= XInternAtom( display
, "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR", False
);
650 whole_window_atom
= MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_whole_window" ));
651 client_window_atom
= MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_client_window" ));
652 icon_window_atom
= MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_icon_window" ));
654 data
->whole_window
= data
->client_window
= root_window
;
655 data
->whole_rect
= data
->client_rect
= wndPtr
->rectWindow
;
657 SetPropA( wndPtr
->hwndSelf
, whole_window_atom
, (HANDLE
)root_window
);
658 SetPropA( wndPtr
->hwndSelf
, client_window_atom
, (HANDLE
)root_window
);
659 SetPropA( wndPtr
->hwndSelf
, "__wine_x11_visual_id", (HANDLE
)XVisualIDFromVisual(visual
) );
661 SendMessageW( wndPtr
->hwndSelf
, WM_NCCREATE
, 0, (LPARAM
)cs
);
662 if (root_window
!= DefaultRootWindow(display
)) X11DRV_create_desktop_thread();
666 /**********************************************************************
667 * create_whole_window
669 * Create the whole X window for a given window
671 static Window
create_whole_window( Display
*display
, WND
*win
)
673 struct x11drv_win_data
*data
= win
->pDriverData
;
675 XSetWindowAttributes attr
;
678 BOOL is_top_level
= is_window_top_level( win
);
680 rect
= win
->rectWindow
;
681 X11DRV_window_to_X_rect( win
, &rect
);
683 if (!(cx
= rect
.right
- rect
.left
)) cx
= 1;
684 if (!(cy
= rect
.bottom
- rect
.top
)) cy
= 1;
686 parent
= X11DRV_get_client_window( win
->parent
);
690 mask
= get_window_attributes( display
, win
, &attr
);
692 /* set the attributes that don't change over the lifetime of the window */
693 attr
.bit_gravity
= ForgetGravity
;
694 attr
.win_gravity
= NorthWestGravity
;
695 attr
.backing_store
= NotUseful
/*WhenMapped*/;
696 mask
|= CWBitGravity
| CWWinGravity
| CWBackingStore
;
698 data
->whole_rect
= rect
;
699 data
->whole_window
= XCreateWindow( display
, parent
, rect
.left
, rect
.top
, cx
, cy
,
700 0, screen_depth
, InputOutput
, visual
,
702 if (attr
.cursor
) XFreeCursor( display
, attr
.cursor
);
704 if (!data
->whole_window
)
710 /* non-maximized child must be at bottom of Z order */
711 if ((win
->dwStyle
& (WS_CHILD
|WS_MAXIMIZE
)) == WS_CHILD
)
713 XWindowChanges changes
;
714 changes
.stack_mode
= Below
;
715 XConfigureWindow( display
, data
->whole_window
, CWStackMode
, &changes
);
720 if (is_top_level
) set_wm_hints( display
, win
);
722 return data
->whole_window
;
726 /**********************************************************************
727 * create_client_window
729 * Create the client window for a given window
731 static Window
create_client_window( Display
*display
, WND
*win
)
733 struct x11drv_win_data
*data
= win
->pDriverData
;
734 RECT rect
= data
->whole_rect
;
735 XSetWindowAttributes attr
;
737 OffsetRect( &rect
, -data
->whole_rect
.left
, -data
->whole_rect
.top
);
738 data
->client_rect
= rect
;
740 attr
.event_mask
= (ExposureMask
| KeyPressMask
| KeyReleaseMask
| PointerMotionMask
|
741 ButtonPressMask
| ButtonReleaseMask
);
742 attr
.bit_gravity
= (win
->clsStyle
& (CS_VREDRAW
| CS_HREDRAW
)) ?
743 ForgetGravity
: NorthWestGravity
;
744 attr
.backing_store
= NotUseful
/*WhenMapped*/;
747 data
->client_window
= XCreateWindow( display
, data
->whole_window
, 0, 0,
748 max( rect
.right
- rect
.left
, 1 ),
749 max( rect
.bottom
- rect
.top
, 1 ),
752 CWEventMask
| CWBitGravity
| CWBackingStore
, &attr
);
753 if (data
->client_window
&& is_client_window_mapped( win
))
754 XMapWindow( display
, data
->client_window
);
756 return data
->client_window
;
760 /*****************************************************************
761 * SetWindowText (X11DRV.@)
763 BOOL
X11DRV_SetWindowText( HWND hwnd
, LPCWSTR text
)
765 Display
*display
= thread_display();
769 static UINT text_cp
= (UINT
)-1;
772 if ((win
= X11DRV_get_whole_window( hwnd
)))
774 if (text_cp
== (UINT
)-1)
779 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE
, "Software\\Wine\\Wine\\Config\\x11drv", &hkey
))
782 DWORD type
, count
= sizeof(buffer
);
783 if(!RegQueryValueExA(hkey
, "TextCP", 0, &type
, buffer
, &count
))
784 text_cp
= atoi(buffer
);
787 TRACE("text_cp = %u\n", text_cp
);
790 /* allocate new buffer for window text */
791 count
= WideCharToMultiByte(text_cp
, 0, text
, -1, NULL
, 0, NULL
, NULL
);
792 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, count
)))
794 ERR("Not enough memory for window text\n");
797 WideCharToMultiByte(text_cp
, 0, text
, -1, buffer
, count
, NULL
, NULL
);
799 count
= WideCharToMultiByte(CP_UTF8
, 0, text
, strlenW(text
), NULL
, 0, NULL
, NULL
);
800 if (!(utf8_buffer
= HeapAlloc( GetProcessHeap(), 0, count
)))
802 ERR("Not enough memory for window text in UTF-8\n");
805 WideCharToMultiByte(CP_UTF8
, 0, text
, strlenW(text
), utf8_buffer
, count
, NULL
, NULL
);
808 XStoreName( display
, win
, buffer
);
809 XSetIconName( display
, win
, buffer
);
811 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
812 according to the standard
813 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
815 XChangeProperty( display
, win
,
816 XInternAtom(display
, "_NET_WM_NAME", False
),
817 XInternAtom(display
, "UTF8_STRING", False
),
818 8, PropModeReplace
, (unsigned char *) utf8_buffer
,
822 HeapFree( GetProcessHeap(), 0, utf8_buffer
);
823 HeapFree( GetProcessHeap(), 0, buffer
);
829 /***********************************************************************
830 * DestroyWindow (X11DRV.@)
832 BOOL
X11DRV_DestroyWindow( HWND hwnd
)
834 Display
*display
= thread_display();
835 WND
*wndPtr
= WIN_GetPtr( hwnd
);
836 X11DRV_WND_DATA
*data
= wndPtr
->pDriverData
;
838 if (!data
) goto done
;
840 if (data
->whole_window
)
842 TRACE( "win %x xwin %lx/%lx\n", hwnd
, data
->whole_window
, data
->client_window
);
844 XSync( gdi_display
, False
); /* flush any reference to this drawable in GDI queue */
845 XDeleteContext( display
, data
->whole_window
, winContext
);
846 XDeleteContext( display
, data
->client_window
, winContext
);
847 XDestroyWindow( display
, data
->whole_window
); /* this destroys client too */
848 destroy_icon_window( display
, wndPtr
);
852 if (data
->hWMIconBitmap
) DeleteObject( data
->hWMIconBitmap
);
853 if (data
->hWMIconMask
) DeleteObject( data
->hWMIconMask
);
854 HeapFree( GetProcessHeap(), 0, data
);
855 wndPtr
->pDriverData
= NULL
;
857 WIN_ReleasePtr( wndPtr
);
862 /**********************************************************************
863 * CreateWindow (X11DRV.@)
865 BOOL
X11DRV_CreateWindow( HWND hwnd
, CREATESTRUCTA
*cs
, BOOL unicode
)
868 Display
*display
= thread_display();
870 struct x11drv_win_data
*data
;
874 if (!(data
= HeapAlloc(GetProcessHeap(), 0, sizeof(*data
)))) return FALSE
;
875 data
->whole_window
= 0;
876 data
->client_window
= 0;
877 data
->icon_window
= 0;
878 data
->hWMIconBitmap
= 0;
879 data
->hWMIconMask
= 0;
881 wndPtr
= WIN_GetPtr( hwnd
);
882 wndPtr
->pDriverData
= data
;
884 /* initialize the dimensions before sending WM_GETMINMAXINFO */
885 SetRect( &rect
, cs
->x
, cs
->y
, cs
->x
+ cs
->cx
, cs
->y
+ cs
->cy
);
886 WIN_SetRectangles( hwnd
, &rect
, &rect
);
890 create_desktop( display
, wndPtr
, cs
);
891 WIN_ReleasePtr( wndPtr
);
895 if (!create_whole_window( display
, wndPtr
)) goto failed
;
896 if (!create_client_window( display
, wndPtr
)) goto failed
;
897 TSXSync( display
, False
);
899 SetPropA( hwnd
, whole_window_atom
, (HANDLE
)data
->whole_window
);
900 SetPropA( hwnd
, client_window_atom
, (HANDLE
)data
->client_window
);
902 /* Call the WH_CBT hook */
904 hwndLinkAfter
= ((cs
->style
& (WS_CHILD
|WS_MAXIMIZE
)) == WS_CHILD
)
905 ? HWND_BOTTOM
: HWND_TOP
;
907 if (HOOK_IsHooked( WH_CBT
))
913 cbtc
.hwndInsertAfter
= hwndLinkAfter
;
914 lret
= (unicode
) ? HOOK_CallHooksW(WH_CBT
, HCBT_CREATEWND
,
915 (WPARAM
)hwnd
, (LPARAM
)&cbtc
)
916 : HOOK_CallHooksA(WH_CBT
, HCBT_CREATEWND
,
917 (WPARAM
)hwnd
, (LPARAM
)&cbtc
);
920 TRACE("CBT-hook returned !0\n");
927 /* Send the WM_GETMINMAXINFO message and fix the size if needed */
928 if ((cs
->style
& WS_THICKFRAME
) || !(cs
->style
& (WS_POPUP
| WS_CHILD
)))
930 POINT maxSize
, maxPos
, minTrack
, maxTrack
;
932 WIN_ReleasePtr( wndPtr
);
933 WINPOS_GetMinMaxInfo( hwnd
, &maxSize
, &maxPos
, &minTrack
, &maxTrack
);
934 if (maxSize
.x
< cs
->cx
) cs
->cx
= maxSize
.x
;
935 if (maxSize
.y
< cs
->cy
) cs
->cy
= maxSize
.y
;
936 if (cs
->cx
< minTrack
.x
) cs
->cx
= minTrack
.x
;
937 if (cs
->cy
< minTrack
.y
) cs
->cy
= minTrack
.y
;
938 if (cs
->cx
< 0) cs
->cx
= 0;
939 if (cs
->cy
< 0) cs
->cy
= 0;
941 if (!(wndPtr
= WIN_GetPtr( hwnd
))) return FALSE
;
942 SetRect( &rect
, cs
->x
, cs
->y
, cs
->x
+ cs
->cx
, cs
->y
+ cs
->cy
);
943 WIN_SetRectangles( hwnd
, &rect
, &rect
);
944 X11DRV_sync_whole_window_position( display
, wndPtr
, 0 );
946 WIN_ReleasePtr( wndPtr
);
948 /* send WM_NCCREATE */
949 TRACE( "hwnd %x cs %d,%d %dx%d\n", hwnd
, cs
->x
, cs
->y
, cs
->cx
, cs
->cy
);
951 ret
= SendMessageW( hwnd
, WM_NCCREATE
, 0, (LPARAM
)cs
);
953 ret
= SendMessageA( hwnd
, WM_NCCREATE
, 0, (LPARAM
)cs
);
956 WARN("aborted by WM_xxCREATE!\n");
960 if (!(wndPtr
= WIN_GetPtr(hwnd
))) return FALSE
;
962 sync_window_style( display
, wndPtr
);
964 /* send WM_NCCALCSIZE */
965 rect
= wndPtr
->rectWindow
;
966 WIN_ReleasePtr( wndPtr
);
967 SendMessageW( hwnd
, WM_NCCALCSIZE
, FALSE
, (LPARAM
)&rect
);
969 if (!(wndPtr
= WIN_GetPtr(hwnd
))) return FALSE
;
970 if (rect
.left
> rect
.right
|| rect
.top
> rect
.bottom
) rect
= wndPtr
->rectWindow
;
971 WIN_SetRectangles( hwnd
, &wndPtr
->rectWindow
, &rect
);
972 X11DRV_sync_client_window_position( display
, wndPtr
);
973 X11DRV_register_window( display
, hwnd
, data
);
975 TRACE( "win %x window %d,%d,%d,%d client %d,%d,%d,%d whole %d,%d,%d,%d X client %d,%d,%d,%d xwin %x/%x\n",
976 hwnd
, wndPtr
->rectWindow
.left
, wndPtr
->rectWindow
.top
,
977 wndPtr
->rectWindow
.right
, wndPtr
->rectWindow
.bottom
,
978 wndPtr
->rectClient
.left
, wndPtr
->rectClient
.top
,
979 wndPtr
->rectClient
.right
, wndPtr
->rectClient
.bottom
,
980 data
->whole_rect
.left
, data
->whole_rect
.top
,
981 data
->whole_rect
.right
, data
->whole_rect
.bottom
,
982 data
->client_rect
.left
, data
->client_rect
.top
,
983 data
->client_rect
.right
, data
->client_rect
.bottom
,
984 (unsigned int)data
->whole_window
, (unsigned int)data
->client_window
);
986 if ((wndPtr
->dwStyle
& (WS_CHILD
|WS_MAXIMIZE
)) == WS_CHILD
)
987 WIN_LinkWindow( hwnd
, wndPtr
->parent
, HWND_BOTTOM
);
989 WIN_LinkWindow( hwnd
, wndPtr
->parent
, HWND_TOP
);
991 WIN_ReleasePtr( wndPtr
);
994 ret
= (SendMessageW( hwnd
, WM_CREATE
, 0, (LPARAM
)cs
) != -1);
996 ret
= (SendMessageA( hwnd
, WM_CREATE
, 0, (LPARAM
)cs
) != -1);
1000 WIN_UnlinkWindow( hwnd
);
1004 /* Send the size messages */
1006 if (!(wndPtr
= WIN_FindWndPtr(hwnd
))) return FALSE
;
1007 if (!(wndPtr
->flags
& WIN_NEED_SIZE
))
1009 /* send it anyway */
1010 if (((wndPtr
->rectClient
.right
-wndPtr
->rectClient
.left
) <0)
1011 ||((wndPtr
->rectClient
.bottom
-wndPtr
->rectClient
.top
)<0))
1012 WARN("sending bogus WM_SIZE message 0x%08lx\n",
1013 MAKELONG(wndPtr
->rectClient
.right
-wndPtr
->rectClient
.left
,
1014 wndPtr
->rectClient
.bottom
-wndPtr
->rectClient
.top
));
1015 SendMessageW( hwnd
, WM_SIZE
, SIZE_RESTORED
,
1016 MAKELONG(wndPtr
->rectClient
.right
-wndPtr
->rectClient
.left
,
1017 wndPtr
->rectClient
.bottom
-wndPtr
->rectClient
.top
));
1018 SendMessageW( hwnd
, WM_MOVE
, 0,
1019 MAKELONG( wndPtr
->rectClient
.left
, wndPtr
->rectClient
.top
) );
1022 /* Show the window, maximizing or minimizing if needed */
1024 if (wndPtr
->dwStyle
& (WS_MINIMIZE
| WS_MAXIMIZE
))
1026 extern UINT
WINPOS_MinMaximize( HWND hwnd
, UINT cmd
, LPRECT rect
); /*FIXME*/
1029 UINT swFlag
= (wndPtr
->dwStyle
& WS_MINIMIZE
) ? SW_MINIMIZE
: SW_MAXIMIZE
;
1030 WIN_SetStyle( hwnd
, wndPtr
->dwStyle
& ~(WS_MAXIMIZE
| WS_MINIMIZE
) );
1031 WINPOS_MinMaximize( hwnd
, swFlag
, &newPos
);
1032 swFlag
= ((wndPtr
->dwStyle
& WS_CHILD
) || GetActiveWindow())
1033 ? SWP_NOACTIVATE
| SWP_NOZORDER
| SWP_FRAMECHANGED
1034 : SWP_NOZORDER
| SWP_FRAMECHANGED
;
1035 SetWindowPos( hwnd
, 0, newPos
.left
, newPos
.top
,
1036 newPos
.right
, newPos
.bottom
, swFlag
);
1039 WIN_ReleaseWndPtr( wndPtr
);
1044 X11DRV_DestroyWindow( hwnd
);
1045 if (wndPtr
) WIN_ReleasePtr( wndPtr
);
1050 /***********************************************************************
1051 * X11DRV_get_client_window
1053 * Return the X window associated with the client area of a window
1055 Window
X11DRV_get_client_window( HWND hwnd
)
1058 WND
*win
= WIN_GetPtr( hwnd
);
1060 if (win
== WND_OTHER_PROCESS
)
1061 return GetPropA( hwnd
, client_window_atom
);
1065 struct x11drv_win_data
*data
= win
->pDriverData
;
1066 ret
= data
->client_window
;
1067 WIN_ReleasePtr( win
);
1073 /***********************************************************************
1074 * X11DRV_get_whole_window
1076 * Return the X window associated with the full area of a window
1078 Window
X11DRV_get_whole_window( HWND hwnd
)
1081 WND
*win
= WIN_GetPtr( hwnd
);
1083 if (win
== WND_OTHER_PROCESS
)
1084 return GetPropA( hwnd
, whole_window_atom
);
1088 struct x11drv_win_data
*data
= win
->pDriverData
;
1089 ret
= data
->whole_window
;
1090 WIN_ReleasePtr( win
);
1096 /*****************************************************************
1097 * SetParent (X11DRV.@)
1099 HWND
X11DRV_SetParent( HWND hwnd
, HWND parent
)
1101 Display
*display
= thread_display();
1105 /* Windows hides the window first, then shows it again
1106 * including the WM_SHOWWINDOW messages and all */
1107 BOOL was_visible
= ShowWindow( hwnd
, SW_HIDE
);
1109 if (!IsWindow( parent
)) return 0;
1110 if (!(wndPtr
= WIN_GetPtr(hwnd
)) || wndPtr
== WND_OTHER_PROCESS
) return 0;
1112 retvalue
= wndPtr
->parent
; /* old parent */
1113 if (parent
!= retvalue
)
1115 struct x11drv_win_data
*data
= wndPtr
->pDriverData
;
1117 WIN_LinkWindow( hwnd
, parent
, HWND_TOP
);
1119 if (parent
!= GetDesktopWindow()) /* a child window */
1121 if (!(wndPtr
->dwStyle
& WS_CHILD
))
1123 HMENU menu
= (HMENU
)SetWindowLongW( hwnd
, GWL_ID
, 0 );
1124 if (menu
) DestroyMenu( menu
);
1128 if (is_window_top_level( wndPtr
)) set_wm_hints( display
, wndPtr
);
1130 sync_window_style( display
, wndPtr
);
1131 XReparentWindow( display
, data
->whole_window
, X11DRV_get_client_window(parent
),
1132 data
->whole_rect
.left
, data
->whole_rect
.top
);
1133 wine_tsx11_unlock();
1135 WIN_ReleasePtr( wndPtr
);
1137 /* SetParent additionally needs to make hwnd the topmost window
1138 in the x-order and send the expected WM_WINDOWPOSCHANGING and
1139 WM_WINDOWPOSCHANGED notification messages.
1141 SetWindowPos( hwnd
, HWND_TOPMOST
, 0, 0, 0, 0,
1142 SWP_NOACTIVATE
| SWP_NOMOVE
| SWP_NOSIZE
| (was_visible
? SWP_SHOWWINDOW
: 0) );
1143 /* FIXME: a WM_MOVE is also generated (in the DefWindowProc handler
1144 * for WM_WINDOWPOSCHANGED) in Windows, should probably remove SWP_NOMOVE */
1150 /*****************************************************************
1151 * SetFocus (X11DRV.@)
1154 * Explicit colormap management seems to work only with OLVWM.
1156 void X11DRV_SetFocus( HWND hwnd
)
1158 Display
*display
= thread_display();
1159 XWindowAttributes win_attr
;
1162 /* Only mess with the X focus if there's */
1163 /* no desktop window and if the window is not managed by the WM. */
1164 if (root_window
!= DefaultRootWindow(display
)) return;
1166 if (!hwnd
) /* If setting the focus to 0, uninstall the colormap */
1168 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_PRIVATE
)
1169 TSXUninstallColormap( display
, X11DRV_PALETTE_PaletteXColormap
);
1173 hwnd
= GetAncestor( hwnd
, GA_ROOT
);
1174 if (GetWindowLongW( hwnd
, GWL_EXSTYLE
) & WS_EX_MANAGED
) return;
1175 if (!(win
= X11DRV_get_whole_window( hwnd
))) return;
1177 /* Set X focus and install colormap */
1179 if (XGetWindowAttributes( display
, win
, &win_attr
) &&
1180 (win_attr
.map_state
== IsViewable
))
1182 /* If window is not viewable, don't change anything */
1184 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1185 /* FIXME: this is not entirely correct */
1186 XSetInputFocus( display
, win
, RevertToParent
,
1187 /*CurrentTime*/ GetMessageTime() + X11DRV_server_startticks
);
1188 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_PRIVATE
)
1189 XInstallColormap( display
, X11DRV_PALETTE_PaletteXColormap
);
1191 wine_tsx11_unlock();
1195 /**********************************************************************
1196 * SetWindowIcon (X11DRV.@)
1198 * hIcon or hIconSm has changed (or is being initialised for the
1199 * first time). Complete the X11 driver-specific initialisation
1200 * and set the window hints.
1202 * This is not entirely correct, may need to create
1203 * an icon window and set the pixmap as a background
1205 HICON
X11DRV_SetWindowIcon( HWND hwnd
, HICON icon
, BOOL small
)
1208 Display
*display
= thread_display();
1209 HICON old
= SetClassLongW( hwnd
, small
? GCL_HICONSM
: GCL_HICON
, icon
);
1211 SetWindowPos( hwnd
, 0, 0, 0, 0, 0, SWP_FRAMECHANGED
| SWP_NOSIZE
|
1212 SWP_NOMOVE
| SWP_NOACTIVATE
| SWP_NOZORDER
);
1214 if (!(wndPtr
= WIN_GetPtr( hwnd
)) || wndPtr
== WND_OTHER_PROCESS
) return old
;
1216 if (wndPtr
->dwExStyle
& WS_EX_MANAGED
)
1218 Window win
= get_whole_window(wndPtr
);
1219 XWMHints
* wm_hints
= TSXGetWMHints( display
, win
);
1221 if (!wm_hints
) wm_hints
= TSXAllocWMHints();
1224 set_icon_hints( display
, wndPtr
, wm_hints
);
1225 TSXSetWMHints( display
, win
, wm_hints
);
1226 TSXFree( wm_hints
);
1229 WIN_ReleasePtr( wndPtr
);