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
28 #include <X11/Xresource.h>
29 #include <X11/Xutil.h>
35 #include "wine/unicode.h"
37 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(x11drv
);
46 extern Pixmap
X11DRV_BITMAP_Pixmap( HBITMAP
);
48 #define HAS_DLGFRAME(style,exStyle) \
49 (((exStyle) & WS_EX_DLGMODALFRAME) || \
50 (((style) & WS_DLGFRAME) && !((style) & WS_THICKFRAME)))
52 /* X context to associate a hwnd to an X window */
53 XContext winContext
= 0;
55 Atom wmProtocols
= None
;
56 Atom wmDeleteWindow
= None
;
57 Atom wmTakeFocus
= None
;
58 Atom dndProtocol
= None
;
59 Atom dndSelection
= None
;
60 Atom wmChangeState
= None
;
62 Atom kwmDockWindow
= None
;
63 Atom _kde_net_wm_system_tray_window_for
= None
; /* KDE 2 Final */
65 static LPCSTR whole_window_atom
;
66 static LPCSTR client_window_atom
;
67 static LPCSTR icon_window_atom
;
69 /***********************************************************************
72 * Check if a given window should be managed
74 inline static BOOL
is_window_managed( WND
*win
)
76 if (!managed_mode
) return FALSE
;
77 /* tray window is always managed */
78 if (win
->dwExStyle
& WS_EX_TRAYWINDOW
) return TRUE
;
79 /* child windows are not managed */
80 if (win
->dwStyle
& WS_CHILD
) return FALSE
;
81 /* tool windows are not managed */
82 if (win
->dwExStyle
& WS_EX_TOOLWINDOW
) return FALSE
;
83 /* windows with caption or thick frame are managed */
84 if ((win
->dwStyle
& WS_CAPTION
) == WS_CAPTION
) return TRUE
;
85 if (win
->dwStyle
& WS_THICKFRAME
) return TRUE
;
86 /* default: not managed */
91 /***********************************************************************
92 * is_client_window_mapped
94 * Check if the X client window should be mapped
96 inline static BOOL
is_client_window_mapped( WND
*win
)
98 struct x11drv_win_data
*data
= win
->pDriverData
;
99 return !(win
->dwStyle
& WS_MINIMIZE
) && !IsRectEmpty( &data
->client_rect
);
103 /***********************************************************************
104 * get_window_attributes
106 * Fill the window attributes structure for an X window.
108 static int get_window_attributes( Display
*display
, WND
*win
, XSetWindowAttributes
*attr
)
110 BOOL is_top_level
= is_window_top_level( win
);
111 BOOL managed
= is_top_level
&& is_window_managed( win
);
113 if (managed
) WIN_SetExStyle( win
->hwndSelf
, win
->dwExStyle
| WS_EX_MANAGED
);
114 else WIN_SetExStyle( win
->hwndSelf
, win
->dwExStyle
& ~WS_EX_MANAGED
);
116 attr
->override_redirect
= !managed
;
117 attr
->colormap
= X11DRV_PALETTE_PaletteXColormap
;
118 attr
->save_under
= ((win
->clsStyle
& CS_SAVEBITS
) != 0);
119 attr
->cursor
= x11drv_thread_data()->cursor
;
120 attr
->event_mask
= (ExposureMask
| KeyPressMask
| KeyReleaseMask
| PointerMotionMask
|
121 ButtonPressMask
| ButtonReleaseMask
| EnterWindowMask
);
123 if (is_window_top_level( win
))
124 attr
->event_mask
|= StructureNotifyMask
| FocusChangeMask
| KeymapStateMask
;
126 return (CWOverrideRedirect
| CWSaveUnder
| CWEventMask
| CWColormap
| CWCursor
);
130 /***********************************************************************
131 * X11DRV_sync_window_style
133 * Change the X window attributes when the window style has changed.
135 void X11DRV_sync_window_style( Display
*display
, WND
*win
)
137 XSetWindowAttributes attr
;
141 mask
= get_window_attributes( display
, win
, &attr
);
142 XChangeWindowAttributes( display
, get_whole_window(win
), mask
, &attr
);
147 /***********************************************************************
150 * fill the window changes structure
152 static int get_window_changes( XWindowChanges
*changes
, const RECT
*old
, const RECT
*new )
156 if (old
->right
- old
->left
!= new->right
- new->left
)
158 if (!(changes
->width
= new->right
- new->left
)) changes
->width
= 1;
161 if (old
->bottom
- old
->top
!= new->bottom
- new->top
)
163 if (!(changes
->height
= new->bottom
- new->top
)) changes
->height
= 1;
166 if (old
->left
!= new->left
)
168 changes
->x
= new->left
;
171 if (old
->top
!= new->top
)
173 changes
->y
= new->top
;
180 /***********************************************************************
183 static Window
create_icon_window( Display
*display
, WND
*win
)
185 struct x11drv_win_data
*data
= win
->pDriverData
;
186 XSetWindowAttributes attr
;
188 attr
.event_mask
= (ExposureMask
| KeyPressMask
| KeyReleaseMask
| PointerMotionMask
|
189 ButtonPressMask
| ButtonReleaseMask
| EnterWindowMask
);
190 attr
.bit_gravity
= NorthWestGravity
;
191 attr
.backing_store
= NotUseful
/*WhenMapped*/;
192 attr
.colormap
= X11DRV_PALETTE_PaletteXColormap
; /* Needed due to our visual */
195 data
->icon_window
= XCreateWindow( display
, root_window
, 0, 0,
196 GetSystemMetrics( SM_CXICON
),
197 GetSystemMetrics( SM_CYICON
),
200 CWEventMask
| CWBitGravity
| CWBackingStore
| CWColormap
, &attr
);
201 XSaveContext( display
, data
->icon_window
, winContext
, (char *)win
->hwndSelf
);
204 TRACE( "created %lx\n", data
->icon_window
);
205 SetPropA( win
->hwndSelf
, icon_window_atom
, (HANDLE
)data
->icon_window
);
206 return data
->icon_window
;
211 /***********************************************************************
212 * destroy_icon_window
214 inline static void destroy_icon_window( Display
*display
, WND
*win
)
216 struct x11drv_win_data
*data
= win
->pDriverData
;
218 if (!data
->icon_window
) return;
219 if (x11drv_thread_data()->cursor_window
== data
->icon_window
)
220 x11drv_thread_data()->cursor_window
= None
;
222 XDeleteContext( display
, data
->icon_window
, winContext
);
223 XDestroyWindow( display
, data
->icon_window
);
224 data
->icon_window
= 0;
226 RemovePropA( win
->hwndSelf
, icon_window_atom
);
230 /***********************************************************************
233 * Set the icon wm hints
235 static void set_icon_hints( Display
*display
, WND
*wndPtr
, XWMHints
*hints
)
237 X11DRV_WND_DATA
*data
= wndPtr
->pDriverData
;
238 HICON hIcon
= (HICON
)GetClassLongA( wndPtr
->hwndSelf
, GCL_HICON
);
240 if (data
->hWMIconBitmap
) DeleteObject( data
->hWMIconBitmap
);
241 if (data
->hWMIconMask
) DeleteObject( data
->hWMIconMask
);
242 data
->hWMIconBitmap
= 0;
243 data
->hWMIconMask
= 0;
245 if (!(wndPtr
->dwExStyle
& WS_EX_MANAGED
))
247 destroy_icon_window( display
, wndPtr
);
248 hints
->flags
&= ~(IconPixmapHint
| IconMaskHint
| IconWindowHint
);
252 if (!data
->icon_window
) create_icon_window( display
, wndPtr
);
253 hints
->icon_window
= data
->icon_window
;
254 hints
->flags
= (hints
->flags
& ~(IconPixmapHint
| IconMaskHint
)) | IconWindowHint
;
264 GetIconInfo(hIcon
, &ii
);
266 GetObjectA(ii
.hbmMask
, sizeof(bmMask
), &bmMask
);
269 rcMask
.right
= bmMask
.bmWidth
;
270 rcMask
.bottom
= bmMask
.bmHeight
;
272 hDC
= CreateCompatibleDC(0);
273 hbmOrig
= SelectObject(hDC
, ii
.hbmMask
);
274 InvertRect(hDC
, &rcMask
);
275 SelectObject(hDC
, ii
.hbmColor
); /* force the color bitmap to x11drv mode too */
276 SelectObject(hDC
, hbmOrig
);
279 data
->hWMIconBitmap
= ii
.hbmColor
;
280 data
->hWMIconMask
= ii
.hbmMask
;
282 hints
->icon_pixmap
= X11DRV_BITMAP_Pixmap(data
->hWMIconBitmap
);
283 hints
->icon_mask
= X11DRV_BITMAP_Pixmap(data
->hWMIconMask
);
284 destroy_icon_window( display
, wndPtr
);
285 hints
->flags
= (hints
->flags
& ~IconWindowHint
) | IconPixmapHint
| IconMaskHint
;
290 /***********************************************************************
293 * set the window size hints
295 static void set_size_hints( Display
*display
, WND
*win
)
297 XSizeHints
* size_hints
;
298 struct x11drv_win_data
*data
= win
->pDriverData
;
300 if ((size_hints
= XAllocSizeHints()))
302 size_hints
->win_gravity
= StaticGravity
;
303 size_hints
->x
= data
->whole_rect
.left
;
304 size_hints
->y
= data
->whole_rect
.top
;
305 size_hints
->flags
= PWinGravity
| PPosition
;
307 if (HAS_DLGFRAME( win
->dwStyle
, win
->dwExStyle
))
309 size_hints
->max_width
= data
->whole_rect
.right
- data
->whole_rect
.left
;
310 size_hints
->max_height
= data
->whole_rect
.bottom
- data
->whole_rect
.top
;
311 size_hints
->min_width
= size_hints
->max_width
;
312 size_hints
->min_height
= size_hints
->max_height
;
313 size_hints
->flags
|= PMinSize
| PMaxSize
;
315 XSetWMNormalHints( display
, data
->whole_window
, size_hints
);
321 /***********************************************************************
322 * X11DRV_set_wm_hints
324 * Set the window manager hints for a newly-created window
326 void X11DRV_set_wm_hints( Display
*display
, WND
*win
)
328 struct x11drv_win_data
*data
= win
->pDriverData
;
330 XClassHint
*class_hints
;
339 protocols
[i
++] = wmDeleteWindow
;
340 if (wmTakeFocus
) protocols
[i
++] = wmTakeFocus
;
341 XSetWMProtocols( display
, data
->whole_window
, protocols
, i
);
344 if ((class_hints
= XAllocClassHint()))
346 class_hints
->res_name
= "wine";
347 class_hints
->res_class
= "Wine";
348 XSetClassHint( display
, data
->whole_window
, class_hints
);
349 XFree( class_hints
);
352 /* transient for hint */
355 Window owner_win
= X11DRV_get_whole_window( win
->owner
);
356 XSetTransientForHint( display
, data
->whole_window
, owner_win
);
357 group_leader
= owner_win
;
359 else group_leader
= data
->whole_window
;
362 set_size_hints( display
, win
);
364 /* systray properties (KDE only for now) */
365 if (win
->dwExStyle
& WS_EX_TRAYWINDOW
)
368 if (kwmDockWindow
!= None
)
369 XChangeProperty( display
, data
->whole_window
, kwmDockWindow
, kwmDockWindow
,
370 32, PropModeReplace
, (char*)&val
, 1 );
371 if (_kde_net_wm_system_tray_window_for
!= None
)
372 XChangeProperty( display
, data
->whole_window
, _kde_net_wm_system_tray_window_for
,
373 XA_WINDOW
, 32, PropModeReplace
, (char*)&data
->whole_window
, 1 );
376 if (mwmHints
!= None
)
379 mwm_hints
.flags
= MWM_HINTS_FUNCTIONS
| MWM_HINTS_DECORATIONS
;
380 mwm_hints
.functions
= 0;
381 if ((win
->dwStyle
& WS_CAPTION
) == WS_CAPTION
) mwm_hints
.functions
|= MWM_FUNC_MOVE
;
382 if (win
->dwStyle
& WS_THICKFRAME
) mwm_hints
.functions
|= MWM_FUNC_MOVE
| MWM_FUNC_RESIZE
;
383 if (win
->dwStyle
& WS_MINIMIZEBOX
) mwm_hints
.functions
|= MWM_FUNC_MINIMIZE
;
384 if (win
->dwStyle
& WS_MAXIMIZEBOX
) mwm_hints
.functions
|= MWM_FUNC_MAXIMIZE
;
385 if (win
->dwStyle
& WS_SYSMENU
) mwm_hints
.functions
|= MWM_FUNC_CLOSE
;
386 mwm_hints
.decorations
= 0;
387 if ((win
->dwStyle
& WS_CAPTION
) == WS_CAPTION
) mwm_hints
.decorations
|= MWM_DECOR_TITLE
;
388 if (win
->dwExStyle
& WS_EX_DLGMODALFRAME
) mwm_hints
.decorations
|= MWM_DECOR_BORDER
;
389 else if (win
->dwStyle
& WS_THICKFRAME
) mwm_hints
.decorations
|= MWM_DECOR_BORDER
| MWM_DECOR_RESIZEH
;
390 else if ((win
->dwStyle
& (WS_DLGFRAME
|WS_BORDER
)) == WS_DLGFRAME
) mwm_hints
.decorations
|= MWM_DECOR_BORDER
;
391 else if (win
->dwStyle
& WS_BORDER
) mwm_hints
.decorations
|= MWM_DECOR_BORDER
;
392 else if (!(win
->dwStyle
& (WS_CHILD
|WS_POPUP
))) mwm_hints
.decorations
|= MWM_DECOR_BORDER
;
393 if (win
->dwStyle
& WS_SYSMENU
) mwm_hints
.decorations
|= MWM_DECOR_MENU
;
394 if (win
->dwStyle
& WS_MINIMIZEBOX
) mwm_hints
.decorations
|= MWM_DECOR_MINIMIZE
;
395 if (win
->dwStyle
& WS_MAXIMIZEBOX
) mwm_hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
397 XChangeProperty( display
, data
->whole_window
, mwmHints
, mwmHints
, 32,
398 PropModeReplace
, (char*)&mwm_hints
, sizeof(mwm_hints
)/sizeof(long) );
401 wm_hints
= XAllocWMHints();
407 wm_hints
->flags
= InputHint
| StateHint
| WindowGroupHint
;
408 wm_hints
->input
= !(win
->dwStyle
& WS_DISABLED
);
410 set_icon_hints( display
, win
, wm_hints
);
412 wm_hints
->initial_state
= (win
->dwStyle
& WS_MINIMIZE
) ? IconicState
: NormalState
;
413 wm_hints
->window_group
= group_leader
;
416 XSetWMHints( display
, data
->whole_window
, wm_hints
);
423 /***********************************************************************
424 * X11DRV_set_iconic_state
426 * Set the X11 iconic state according to the window style.
428 void X11DRV_set_iconic_state( WND
*win
)
430 Display
*display
= thread_display();
431 struct x11drv_win_data
*data
= win
->pDriverData
;
433 BOOL iconic
= IsIconic( win
->hwndSelf
);
437 if (iconic
) XUnmapWindow( display
, data
->client_window
);
438 else if (is_client_window_mapped( win
)) XMapWindow( display
, data
->client_window
);
440 if (!(wm_hints
= XGetWMHints( display
, data
->whole_window
))) wm_hints
= XAllocWMHints();
441 wm_hints
->flags
|= StateHint
| IconPositionHint
;
442 wm_hints
->initial_state
= iconic
? IconicState
: NormalState
;
443 wm_hints
->icon_x
= win
->rectWindow
.left
;
444 wm_hints
->icon_y
= win
->rectWindow
.top
;
445 XSetWMHints( display
, data
->whole_window
, wm_hints
);
447 if (win
->dwStyle
& WS_VISIBLE
)
450 XIconifyWindow( display
, data
->whole_window
, DefaultScreen(display
) );
452 if (!IsRectEmpty( &win
->rectWindow
)) XMapWindow( display
, data
->whole_window
);
460 /***********************************************************************
461 * X11DRV_window_to_X_rect
463 * Convert a rect from client to X window coordinates
465 void X11DRV_window_to_X_rect( WND
*win
, RECT
*rect
)
469 if (!(win
->dwExStyle
& WS_EX_MANAGED
)) return;
470 if (IsRectEmpty( rect
)) return;
472 rc
.top
= rc
.bottom
= rc
.left
= rc
.right
= 0;
474 AdjustWindowRectEx( &rc
, win
->dwStyle
& ~(WS_HSCROLL
|WS_VSCROLL
), FALSE
, win
->dwExStyle
);
476 rect
->left
-= rc
.left
;
477 rect
->right
-= rc
.right
;
479 rect
->bottom
-= rc
.bottom
;
480 if (rect
->top
>= rect
->bottom
) rect
->bottom
= rect
->top
+ 1;
481 if (rect
->left
>= rect
->right
) rect
->right
= rect
->left
+ 1;
485 /***********************************************************************
486 * X11DRV_X_to_window_rect
488 * Opposite of X11DRV_window_to_X_rect
490 void X11DRV_X_to_window_rect( WND
*win
, RECT
*rect
)
492 if (!(win
->dwExStyle
& WS_EX_MANAGED
)) return;
493 if (IsRectEmpty( rect
)) return;
495 AdjustWindowRectEx( rect
, win
->dwStyle
& ~(WS_HSCROLL
|WS_VSCROLL
), FALSE
, win
->dwExStyle
);
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_sync_whole_window_position
505 * Synchronize the X whole window position with the Windows one
507 int X11DRV_sync_whole_window_position( Display
*display
, WND
*win
, int zorder
)
509 XWindowChanges changes
;
511 struct x11drv_win_data
*data
= win
->pDriverData
;
512 RECT whole_rect
= win
->rectWindow
;
514 X11DRV_window_to_X_rect( win
, &whole_rect
);
515 mask
= get_window_changes( &changes
, &data
->whole_rect
, &whole_rect
);
519 /* find window that this one must be after */
520 HWND prev
= GetWindow( win
->hwndSelf
, GW_HWNDPREV
);
521 while (prev
&& !(GetWindowLongW( prev
, GWL_STYLE
) & WS_VISIBLE
))
522 prev
= GetWindow( prev
, GW_HWNDPREV
);
523 if (!prev
) /* top child */
525 changes
.stack_mode
= Above
;
530 changes
.stack_mode
= Below
;
531 changes
.sibling
= X11DRV_get_whole_window(prev
);
532 mask
|= CWStackMode
| CWSibling
;
536 data
->whole_rect
= whole_rect
;
540 TRACE( "setting win %lx pos %d,%d,%dx%d after %lx changes=%x\n",
541 data
->whole_window
, whole_rect
.left
, whole_rect
.top
,
542 whole_rect
.right
- whole_rect
.left
, whole_rect
.bottom
- whole_rect
.top
,
543 changes
.sibling
, mask
);
545 XSync( gdi_display
, False
); /* flush graphics operations before moving the window */
546 if (is_window_top_level( win
))
548 if (mask
& (CWWidth
|CWHeight
)) set_size_hints( display
, win
);
549 XReconfigureWMWindow( display
, data
->whole_window
,
550 DefaultScreen(display
), mask
, &changes
);
552 else XConfigureWindow( display
, data
->whole_window
, mask
, &changes
);
559 /***********************************************************************
560 * X11DRV_sync_client_window_position
562 * Synchronize the X client window position with the Windows one
564 int X11DRV_sync_client_window_position( Display
*display
, WND
*win
)
566 XWindowChanges changes
;
568 struct x11drv_win_data
*data
= win
->pDriverData
;
569 RECT client_rect
= win
->rectClient
;
571 OffsetRect( &client_rect
, -data
->whole_rect
.left
, -data
->whole_rect
.top
);
573 if ((mask
= get_window_changes( &changes
, &data
->client_rect
, &client_rect
)))
575 BOOL was_mapped
= is_client_window_mapped( win
);
577 TRACE( "setting win %lx pos %d,%d,%dx%d (was %d,%d,%dx%d) after %lx changes=%x\n",
578 data
->client_window
, client_rect
.left
, client_rect
.top
,
579 client_rect
.right
- client_rect
.left
, client_rect
.bottom
- client_rect
.top
,
580 data
->client_rect
.left
, data
->client_rect
.top
,
581 data
->client_rect
.right
- data
->client_rect
.left
,
582 data
->client_rect
.bottom
- data
->client_rect
.top
,
583 changes
.sibling
, mask
);
584 data
->client_rect
= client_rect
;
586 XSync( gdi_display
, False
); /* flush graphics operations before moving the window */
587 if (was_mapped
&& !is_client_window_mapped( win
))
588 XUnmapWindow( display
, data
->client_window
);
589 XConfigureWindow( display
, data
->client_window
, mask
, &changes
);
590 if (!was_mapped
&& is_client_window_mapped( win
))
591 XMapWindow( display
, data
->client_window
);
598 /***********************************************************************
599 * X11DRV_register_window
601 * Associate an X window to a HWND.
603 void X11DRV_register_window( Display
*display
, HWND hwnd
, struct x11drv_win_data
*data
)
606 XSaveContext( display
, data
->whole_window
, winContext
, (char *)hwnd
);
607 XSaveContext( display
, data
->client_window
, winContext
, (char *)hwnd
);
612 /**********************************************************************
615 static void create_desktop( Display
*display
, WND
*wndPtr
, CREATESTRUCTA
*cs
)
617 X11DRV_WND_DATA
*data
= wndPtr
->pDriverData
;
620 winContext
= XUniqueContext();
621 wmProtocols
= XInternAtom( display
, "WM_PROTOCOLS", False
);
622 wmDeleteWindow
= XInternAtom( display
, "WM_DELETE_WINDOW", False
);
623 if (use_take_focus
) wmTakeFocus
= XInternAtom( display
, "WM_TAKE_FOCUS", False
);
624 dndProtocol
= XInternAtom( display
, "DndProtocol" , False
);
625 dndSelection
= XInternAtom( display
, "DndSelection" , False
);
626 wmChangeState
= XInternAtom( display
, "WM_CHANGE_STATE", False
);
627 mwmHints
= XInternAtom( display
, _XA_MWM_HINTS
, False
);
628 kwmDockWindow
= XInternAtom( display
, "KWM_DOCKWINDOW", False
);
629 _kde_net_wm_system_tray_window_for
= XInternAtom( display
, "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR", False
);
632 whole_window_atom
= MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_whole_window" ));
633 client_window_atom
= MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_client_window" ));
634 icon_window_atom
= MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_icon_window" ));
636 data
->whole_window
= data
->client_window
= root_window
;
637 data
->whole_rect
= data
->client_rect
= wndPtr
->rectWindow
;
639 SetPropA( wndPtr
->hwndSelf
, whole_window_atom
, (HANDLE
)root_window
);
640 SetPropA( wndPtr
->hwndSelf
, client_window_atom
, (HANDLE
)root_window
);
641 SetPropA( wndPtr
->hwndSelf
, "__wine_x11_visual_id", (HANDLE
)XVisualIDFromVisual(visual
) );
643 SendMessageW( wndPtr
->hwndSelf
, WM_NCCREATE
, 0, (LPARAM
)cs
);
644 if (root_window
!= DefaultRootWindow(display
)) X11DRV_create_desktop_thread();
648 /**********************************************************************
649 * create_whole_window
651 * Create the whole X window for a given window
653 static Window
create_whole_window( Display
*display
, WND
*win
)
655 struct x11drv_win_data
*data
= win
->pDriverData
;
657 XSetWindowAttributes attr
;
660 BOOL is_top_level
= is_window_top_level( win
);
662 rect
= win
->rectWindow
;
663 X11DRV_window_to_X_rect( win
, &rect
);
665 if (!(cx
= rect
.right
- rect
.left
)) cx
= 1;
666 if (!(cy
= rect
.bottom
- rect
.top
)) cy
= 1;
668 parent
= X11DRV_get_client_window( win
->parent
);
672 mask
= get_window_attributes( display
, win
, &attr
);
674 /* set the attributes that don't change over the lifetime of the window */
675 attr
.bit_gravity
= ForgetGravity
;
676 attr
.win_gravity
= NorthWestGravity
;
677 attr
.backing_store
= NotUseful
/*WhenMapped*/;
678 mask
|= CWBitGravity
| CWWinGravity
| CWBackingStore
;
680 data
->whole_rect
= rect
;
681 data
->whole_window
= XCreateWindow( display
, parent
, rect
.left
, rect
.top
, cx
, cy
,
682 0, screen_depth
, InputOutput
, visual
,
685 if (!data
->whole_window
)
691 /* non-maximized child must be at bottom of Z order */
692 if ((win
->dwStyle
& (WS_CHILD
|WS_MAXIMIZE
)) == WS_CHILD
)
694 XWindowChanges changes
;
695 changes
.stack_mode
= Below
;
696 XConfigureWindow( display
, data
->whole_window
, CWStackMode
, &changes
);
701 if (is_top_level
) X11DRV_set_wm_hints( display
, win
);
703 return data
->whole_window
;
707 /**********************************************************************
708 * create_client_window
710 * Create the client window for a given window
712 static Window
create_client_window( Display
*display
, WND
*win
)
714 struct x11drv_win_data
*data
= win
->pDriverData
;
715 RECT rect
= data
->whole_rect
;
716 XSetWindowAttributes attr
;
718 OffsetRect( &rect
, -data
->whole_rect
.left
, -data
->whole_rect
.top
);
719 data
->client_rect
= rect
;
721 attr
.event_mask
= (ExposureMask
| KeyPressMask
| KeyReleaseMask
| PointerMotionMask
|
722 ButtonPressMask
| ButtonReleaseMask
| EnterWindowMask
);
723 attr
.bit_gravity
= (win
->clsStyle
& (CS_VREDRAW
| CS_HREDRAW
)) ?
724 ForgetGravity
: NorthWestGravity
;
725 attr
.backing_store
= NotUseful
/*WhenMapped*/;
728 data
->client_window
= XCreateWindow( display
, data
->whole_window
, 0, 0,
729 max( rect
.right
- rect
.left
, 1 ),
730 max( rect
.bottom
- rect
.top
, 1 ),
733 CWEventMask
| CWBitGravity
| CWBackingStore
, &attr
);
734 if (data
->client_window
&& is_client_window_mapped( win
))
735 XMapWindow( display
, data
->client_window
);
737 return data
->client_window
;
741 /*****************************************************************
742 * SetWindowText (X11DRV.@)
744 BOOL
X11DRV_SetWindowText( HWND hwnd
, LPCWSTR text
)
746 Display
*display
= thread_display();
750 static UINT text_cp
= (UINT
)-1;
753 if ((win
= X11DRV_get_whole_window( hwnd
)))
755 if (text_cp
== (UINT
)-1)
760 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE
, "Software\\Wine\\Wine\\Config\\x11drv", &hkey
))
763 DWORD type
, count
= sizeof(buffer
);
764 if(!RegQueryValueExA(hkey
, "TextCP", 0, &type
, buffer
, &count
))
765 text_cp
= atoi(buffer
);
768 TRACE("text_cp = %u\n", text_cp
);
771 /* allocate new buffer for window text */
772 count
= WideCharToMultiByte(text_cp
, 0, text
, -1, NULL
, 0, NULL
, NULL
);
773 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, count
)))
775 ERR("Not enough memory for window text\n");
778 WideCharToMultiByte(text_cp
, 0, text
, -1, buffer
, count
, NULL
, NULL
);
780 count
= WideCharToMultiByte(CP_UTF8
, 0, text
, strlenW(text
), NULL
, 0, NULL
, NULL
);
781 if (!(utf8_buffer
= HeapAlloc( GetProcessHeap(), 0, count
)))
783 ERR("Not enough memory for window text in UTF-8\n");
786 WideCharToMultiByte(CP_UTF8
, 0, text
, strlenW(text
), utf8_buffer
, count
, NULL
, NULL
);
789 XStoreName( display
, win
, buffer
);
790 XSetIconName( display
, win
, buffer
);
792 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
793 according to the standard
794 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
796 XChangeProperty( display
, win
,
797 XInternAtom(display
, "_NET_WM_NAME", False
),
798 XInternAtom(display
, "UTF8_STRING", False
),
799 8, PropModeReplace
, (unsigned char *) utf8_buffer
,
803 HeapFree( GetProcessHeap(), 0, utf8_buffer
);
804 HeapFree( GetProcessHeap(), 0, buffer
);
810 /***********************************************************************
811 * DestroyWindow (X11DRV.@)
813 BOOL
X11DRV_DestroyWindow( HWND hwnd
)
815 struct x11drv_thread_data
*thread_data
= x11drv_thread_data();
816 Display
*display
= thread_data
->display
;
817 WND
*wndPtr
= WIN_GetPtr( hwnd
);
818 X11DRV_WND_DATA
*data
= wndPtr
->pDriverData
;
820 if (!data
) goto done
;
822 if (data
->whole_window
)
824 TRACE( "win %p xwin %lx/%lx\n", hwnd
, data
->whole_window
, data
->client_window
);
825 if (thread_data
->cursor_window
== data
->whole_window
) thread_data
->cursor_window
= None
;
826 if (thread_data
->last_focus
== hwnd
) thread_data
->last_focus
= 0;
828 XSync( gdi_display
, False
); /* flush any reference to this drawable in GDI queue */
829 XDeleteContext( display
, data
->whole_window
, winContext
);
830 XDeleteContext( display
, data
->client_window
, winContext
);
831 XDestroyWindow( display
, data
->whole_window
); /* this destroys client too */
832 destroy_icon_window( display
, wndPtr
);
836 if (data
->hWMIconBitmap
) DeleteObject( data
->hWMIconBitmap
);
837 if (data
->hWMIconMask
) DeleteObject( data
->hWMIconMask
);
838 HeapFree( GetProcessHeap(), 0, data
);
839 wndPtr
->pDriverData
= NULL
;
841 WIN_ReleasePtr( wndPtr
);
846 /**********************************************************************
847 * CreateWindow (X11DRV.@)
849 BOOL
X11DRV_CreateWindow( HWND hwnd
, CREATESTRUCTA
*cs
, BOOL unicode
)
852 Display
*display
= thread_display();
854 struct x11drv_win_data
*data
;
861 ERR( "invalid window width %d\n", cs
->cx
);
866 ERR( "invalid window height %d\n", cs
->cx
);
870 if (!(data
= HeapAlloc(GetProcessHeap(), 0, sizeof(*data
)))) return FALSE
;
871 data
->whole_window
= 0;
872 data
->client_window
= 0;
873 data
->icon_window
= 0;
874 data
->hWMIconBitmap
= 0;
875 data
->hWMIconMask
= 0;
877 wndPtr
= WIN_GetPtr( hwnd
);
878 wndPtr
->pDriverData
= data
;
880 /* initialize the dimensions before sending WM_GETMINMAXINFO */
881 SetRect( &rect
, cs
->x
, cs
->y
, cs
->x
+ cs
->cx
, cs
->y
+ cs
->cy
);
882 WIN_SetRectangles( hwnd
, &rect
, &rect
);
886 create_desktop( display
, wndPtr
, cs
);
887 WIN_ReleasePtr( wndPtr
);
891 if (!create_whole_window( display
, wndPtr
)) goto failed
;
892 if (!create_client_window( display
, wndPtr
)) goto failed
;
893 TSXSync( display
, False
);
895 SetPropA( hwnd
, whole_window_atom
, (HANDLE
)data
->whole_window
);
896 SetPropA( hwnd
, client_window_atom
, (HANDLE
)data
->client_window
);
898 /* Call the WH_CBT hook */
900 hwndLinkAfter
= ((cs
->style
& (WS_CHILD
|WS_MAXIMIZE
)) == WS_CHILD
) ? HWND_BOTTOM
: HWND_TOP
;
903 cbtc
.hwndInsertAfter
= hwndLinkAfter
;
904 if (HOOK_CallHooks( WH_CBT
, HCBT_CREATEWND
, (WPARAM
)hwnd
, (LPARAM
)&cbtc
, unicode
))
906 TRACE("CBT-hook returned !0\n");
910 /* Send the WM_GETMINMAXINFO message and fix the size if needed */
911 if ((cs
->style
& WS_THICKFRAME
) || !(cs
->style
& (WS_POPUP
| WS_CHILD
)))
913 POINT maxSize
, maxPos
, minTrack
, maxTrack
;
915 WIN_ReleasePtr( wndPtr
);
916 WINPOS_GetMinMaxInfo( hwnd
, &maxSize
, &maxPos
, &minTrack
, &maxTrack
);
917 if (maxSize
.x
< cs
->cx
) cs
->cx
= maxSize
.x
;
918 if (maxSize
.y
< cs
->cy
) cs
->cy
= maxSize
.y
;
919 if (cs
->cx
< minTrack
.x
) cs
->cx
= minTrack
.x
;
920 if (cs
->cy
< minTrack
.y
) cs
->cy
= minTrack
.y
;
921 if (cs
->cx
< 0) cs
->cx
= 0;
922 if (cs
->cy
< 0) cs
->cy
= 0;
924 if (!(wndPtr
= WIN_GetPtr( hwnd
))) return FALSE
;
925 SetRect( &rect
, cs
->x
, cs
->y
, cs
->x
+ cs
->cx
, cs
->y
+ cs
->cy
);
926 WIN_SetRectangles( hwnd
, &rect
, &rect
);
927 X11DRV_sync_whole_window_position( display
, wndPtr
, 0 );
929 WIN_ReleasePtr( wndPtr
);
931 /* send WM_NCCREATE */
932 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd
, cs
->x
, cs
->y
, cs
->cx
, cs
->cy
);
934 ret
= SendMessageW( hwnd
, WM_NCCREATE
, 0, (LPARAM
)cs
);
936 ret
= SendMessageA( hwnd
, WM_NCCREATE
, 0, (LPARAM
)cs
);
939 WARN("aborted by WM_xxCREATE!\n");
943 if (!(wndPtr
= WIN_GetPtr(hwnd
))) return FALSE
;
945 X11DRV_sync_window_style( display
, wndPtr
);
947 /* send WM_NCCALCSIZE */
948 rect
= wndPtr
->rectWindow
;
949 WIN_ReleasePtr( wndPtr
);
950 SendMessageW( hwnd
, WM_NCCALCSIZE
, FALSE
, (LPARAM
)&rect
);
952 if (!(wndPtr
= WIN_GetPtr(hwnd
))) return FALSE
;
953 if (rect
.left
> rect
.right
|| rect
.top
> rect
.bottom
) rect
= wndPtr
->rectWindow
;
954 WIN_SetRectangles( hwnd
, &wndPtr
->rectWindow
, &rect
);
955 X11DRV_sync_client_window_position( display
, wndPtr
);
956 X11DRV_register_window( display
, hwnd
, data
);
958 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/%x\n",
959 hwnd
, wndPtr
->rectWindow
.left
, wndPtr
->rectWindow
.top
,
960 wndPtr
->rectWindow
.right
, wndPtr
->rectWindow
.bottom
,
961 wndPtr
->rectClient
.left
, wndPtr
->rectClient
.top
,
962 wndPtr
->rectClient
.right
, wndPtr
->rectClient
.bottom
,
963 data
->whole_rect
.left
, data
->whole_rect
.top
,
964 data
->whole_rect
.right
, data
->whole_rect
.bottom
,
965 data
->client_rect
.left
, data
->client_rect
.top
,
966 data
->client_rect
.right
, data
->client_rect
.bottom
,
967 (unsigned int)data
->whole_window
, (unsigned int)data
->client_window
);
969 if ((wndPtr
->dwStyle
& (WS_CHILD
|WS_MAXIMIZE
)) == WS_CHILD
)
970 WIN_LinkWindow( hwnd
, wndPtr
->parent
, HWND_BOTTOM
);
972 WIN_LinkWindow( hwnd
, wndPtr
->parent
, HWND_TOP
);
974 WIN_ReleasePtr( wndPtr
);
977 ret
= (SendMessageW( hwnd
, WM_CREATE
, 0, (LPARAM
)cs
) != -1);
979 ret
= (SendMessageA( hwnd
, WM_CREATE
, 0, (LPARAM
)cs
) != -1);
983 WIN_UnlinkWindow( hwnd
);
987 /* Send the size messages */
989 if (!(wndPtr
= WIN_FindWndPtr(hwnd
))) return FALSE
;
990 if (!(wndPtr
->flags
& WIN_NEED_SIZE
))
993 if (((wndPtr
->rectClient
.right
-wndPtr
->rectClient
.left
) <0)
994 ||((wndPtr
->rectClient
.bottom
-wndPtr
->rectClient
.top
)<0))
995 WARN("sending bogus WM_SIZE message 0x%08lx\n",
996 MAKELONG(wndPtr
->rectClient
.right
-wndPtr
->rectClient
.left
,
997 wndPtr
->rectClient
.bottom
-wndPtr
->rectClient
.top
));
998 SendMessageW( hwnd
, WM_SIZE
, SIZE_RESTORED
,
999 MAKELONG(wndPtr
->rectClient
.right
-wndPtr
->rectClient
.left
,
1000 wndPtr
->rectClient
.bottom
-wndPtr
->rectClient
.top
));
1001 SendMessageW( hwnd
, WM_MOVE
, 0,
1002 MAKELONG( wndPtr
->rectClient
.left
, wndPtr
->rectClient
.top
) );
1005 /* Show the window, maximizing or minimizing if needed */
1007 if (wndPtr
->dwStyle
& (WS_MINIMIZE
| WS_MAXIMIZE
))
1009 extern UINT
WINPOS_MinMaximize( HWND hwnd
, UINT cmd
, LPRECT rect
); /*FIXME*/
1012 UINT swFlag
= (wndPtr
->dwStyle
& WS_MINIMIZE
) ? SW_MINIMIZE
: SW_MAXIMIZE
;
1013 WIN_SetStyle( hwnd
, wndPtr
->dwStyle
& ~(WS_MAXIMIZE
| WS_MINIMIZE
) );
1014 WINPOS_MinMaximize( hwnd
, swFlag
, &newPos
);
1015 swFlag
= ((wndPtr
->dwStyle
& WS_CHILD
) || GetActiveWindow())
1016 ? SWP_NOACTIVATE
| SWP_NOZORDER
| SWP_FRAMECHANGED
1017 : SWP_NOZORDER
| SWP_FRAMECHANGED
;
1018 SetWindowPos( hwnd
, 0, newPos
.left
, newPos
.top
,
1019 newPos
.right
, newPos
.bottom
, swFlag
);
1022 /* if the window was made visible set create struct flag so that
1023 * we do a proper ShowWindow later on */
1024 if (wndPtr
->dwStyle
& WS_VISIBLE
) cs
->style
|= WS_VISIBLE
;
1026 WIN_ReleaseWndPtr( wndPtr
);
1031 X11DRV_DestroyWindow( hwnd
);
1032 if (wndPtr
) WIN_ReleasePtr( wndPtr
);
1037 /***********************************************************************
1038 * X11DRV_get_client_window
1040 * Return the X window associated with the client area of a window
1042 Window
X11DRV_get_client_window( HWND hwnd
)
1045 WND
*win
= WIN_GetPtr( hwnd
);
1047 if (win
== WND_OTHER_PROCESS
)
1048 return (Window
)GetPropA( hwnd
, client_window_atom
);
1052 struct x11drv_win_data
*data
= win
->pDriverData
;
1053 ret
= data
->client_window
;
1054 WIN_ReleasePtr( win
);
1060 /***********************************************************************
1061 * X11DRV_get_whole_window
1063 * Return the X window associated with the full area of a window
1065 Window
X11DRV_get_whole_window( HWND hwnd
)
1068 WND
*win
= WIN_GetPtr( hwnd
);
1070 if (win
== WND_OTHER_PROCESS
)
1071 return (Window
)GetPropA( hwnd
, whole_window_atom
);
1075 struct x11drv_win_data
*data
= win
->pDriverData
;
1076 ret
= data
->whole_window
;
1077 WIN_ReleasePtr( win
);
1083 /*****************************************************************
1084 * SetParent (X11DRV.@)
1086 HWND
X11DRV_SetParent( HWND hwnd
, HWND parent
)
1088 Display
*display
= thread_display();
1092 /* Windows hides the window first, then shows it again
1093 * including the WM_SHOWWINDOW messages and all */
1094 BOOL was_visible
= ShowWindow( hwnd
, SW_HIDE
);
1096 if (!IsWindow( parent
)) return 0;
1097 if (!(wndPtr
= WIN_GetPtr(hwnd
)) || wndPtr
== WND_OTHER_PROCESS
) return 0;
1099 retvalue
= wndPtr
->parent
; /* old parent */
1100 if (parent
!= retvalue
)
1102 struct x11drv_win_data
*data
= wndPtr
->pDriverData
;
1104 WIN_LinkWindow( hwnd
, parent
, HWND_TOP
);
1106 if (parent
!= GetDesktopWindow()) /* a child window */
1108 if (!(wndPtr
->dwStyle
& WS_CHILD
))
1110 HMENU menu
= (HMENU
)SetWindowLongW( hwnd
, GWL_ID
, 0 );
1111 if (menu
) DestroyMenu( menu
);
1115 if (is_window_top_level( wndPtr
)) X11DRV_set_wm_hints( display
, wndPtr
);
1117 X11DRV_sync_window_style( display
, wndPtr
);
1118 XReparentWindow( display
, data
->whole_window
, X11DRV_get_client_window(parent
),
1119 data
->whole_rect
.left
, data
->whole_rect
.top
);
1120 wine_tsx11_unlock();
1122 WIN_ReleasePtr( wndPtr
);
1124 /* SetParent additionally needs to make hwnd the topmost window
1125 in the x-order and send the expected WM_WINDOWPOSCHANGING and
1126 WM_WINDOWPOSCHANGED notification messages.
1128 SetWindowPos( hwnd
, HWND_TOPMOST
, 0, 0, 0, 0,
1129 SWP_NOACTIVATE
| SWP_NOMOVE
| SWP_NOSIZE
| (was_visible
? SWP_SHOWWINDOW
: 0) );
1130 /* FIXME: a WM_MOVE is also generated (in the DefWindowProc handler
1131 * for WM_WINDOWPOSCHANGED) in Windows, should probably remove SWP_NOMOVE */
1137 /*****************************************************************
1138 * SetFocus (X11DRV.@)
1141 * Explicit colormap management seems to work only with OLVWM.
1143 void X11DRV_SetFocus( HWND hwnd
)
1145 Display
*display
= thread_display();
1146 XWindowAttributes win_attr
;
1149 /* Only mess with the X focus if there's */
1150 /* no desktop window and if the window is not managed by the WM. */
1151 if (root_window
!= DefaultRootWindow(display
)) return;
1153 if (!hwnd
) /* If setting the focus to 0, uninstall the colormap */
1155 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_PRIVATE
)
1156 TSXUninstallColormap( display
, X11DRV_PALETTE_PaletteXColormap
);
1160 hwnd
= GetAncestor( hwnd
, GA_ROOT
);
1161 if (GetWindowLongW( hwnd
, GWL_EXSTYLE
) & WS_EX_MANAGED
) return;
1162 if (!(win
= X11DRV_get_whole_window( hwnd
))) return;
1164 /* Set X focus and install colormap */
1166 if (XGetWindowAttributes( display
, win
, &win_attr
) &&
1167 (win_attr
.map_state
== IsViewable
))
1169 /* If window is not viewable, don't change anything */
1171 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1172 /* FIXME: this is not entirely correct */
1173 XSetInputFocus( display
, win
, RevertToParent
,
1174 /*CurrentTime*/ GetMessageTime() + X11DRV_server_startticks
);
1175 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_PRIVATE
)
1176 XInstallColormap( display
, X11DRV_PALETTE_PaletteXColormap
);
1178 wine_tsx11_unlock();
1182 /**********************************************************************
1183 * SetWindowIcon (X11DRV.@)
1185 * hIcon or hIconSm has changed (or is being initialised for the
1186 * first time). Complete the X11 driver-specific initialisation
1187 * and set the window hints.
1189 * This is not entirely correct, may need to create
1190 * an icon window and set the pixmap as a background
1192 HICON
X11DRV_SetWindowIcon( HWND hwnd
, HICON icon
, BOOL small
)
1195 Display
*display
= thread_display();
1196 HICON old
= (HICON
)SetClassLongW(hwnd
, small
? GCL_HICONSM
: GCL_HICON
, (LONG
)icon
);
1198 SetWindowPos( hwnd
, 0, 0, 0, 0, 0, SWP_FRAMECHANGED
| SWP_NOSIZE
|
1199 SWP_NOMOVE
| SWP_NOACTIVATE
| SWP_NOZORDER
);
1201 if (!(wndPtr
= WIN_GetPtr( hwnd
)) || wndPtr
== WND_OTHER_PROCESS
) return old
;
1203 if (wndPtr
->dwExStyle
& WS_EX_MANAGED
)
1205 Window win
= get_whole_window(wndPtr
);
1209 if (!(wm_hints
= XGetWMHints( display
, win
))) wm_hints
= XAllocWMHints();
1210 wine_tsx11_unlock();
1213 set_icon_hints( display
, wndPtr
, wm_hints
);
1215 XSetWMHints( display
, win
, wm_hints
);
1217 wine_tsx11_unlock();
1220 WIN_ReleasePtr( wndPtr
);