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
32 #include <X11/Xresource.h>
33 #include <X11/Xutil.h>
40 #include "wine/unicode.h"
42 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(x11drv
);
50 extern Pixmap
X11DRV_BITMAP_Pixmap( HBITMAP
);
52 /* X context to associate a hwnd to an X window */
53 XContext winContext
= 0;
55 Atom X11DRV_Atoms
[NB_XATOMS
- FIRST_XATOM
];
57 static const char * const atom_names
[NB_XATOMS
- FIRST_XATOM
] =
76 "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR",
80 "_NET_WM_WINDOW_TYPE",
81 "_NET_WM_WINDOW_TYPE_UTILITY",
105 static LPCSTR whole_window_atom
;
106 static LPCSTR client_window_atom
;
107 static LPCSTR icon_window_atom
;
109 /***********************************************************************
112 * Check if a given window should be managed
114 inline static BOOL
is_window_managed( WND
*win
)
116 if (!managed_mode
) return FALSE
;
117 /* tray window is always managed */
118 if (win
->dwExStyle
& WS_EX_TRAYWINDOW
) return TRUE
;
119 /* child windows are not managed */
120 if (win
->dwStyle
& WS_CHILD
) return FALSE
;
121 /* windows with caption are managed */
122 if ((win
->dwStyle
& WS_CAPTION
) == WS_CAPTION
) return TRUE
;
123 /* tool windows are not managed */
124 if (win
->dwExStyle
& WS_EX_TOOLWINDOW
) return FALSE
;
125 /* windows with thick frame are managed */
126 if (win
->dwStyle
& WS_THICKFRAME
) return TRUE
;
127 /* application windows are managed */
128 if (win
->dwExStyle
& WS_EX_APPWINDOW
) return TRUE
;
129 /* full-screen popup windows are managed */
130 if ((win
->dwStyle
& WS_POPUP
) &&
131 (win
->rectWindow
.right
-win
->rectWindow
.left
) == screen_width
&&
132 (win
->rectWindow
.bottom
-win
->rectWindow
.top
) == screen_height
)
136 /* default: not managed */
141 /***********************************************************************
142 * is_client_window_mapped
144 * Check if the X client window should be mapped
146 inline static BOOL
is_client_window_mapped( WND
*win
)
148 struct x11drv_win_data
*data
= win
->pDriverData
;
149 return !(win
->dwStyle
& WS_MINIMIZE
) && !IsRectEmpty( &data
->client_rect
);
153 /***********************************************************************
154 * get_window_attributes
156 * Fill the window attributes structure for an X window.
158 static int get_window_attributes( Display
*display
, WND
*win
, XSetWindowAttributes
*attr
)
160 BOOL is_top_level
= is_window_top_level( win
);
161 BOOL managed
= is_top_level
&& is_window_managed( win
);
163 if (managed
) WIN_SetExStyle( win
->hwndSelf
, win
->dwExStyle
| WS_EX_MANAGED
);
164 else WIN_SetExStyle( win
->hwndSelf
, win
->dwExStyle
& ~WS_EX_MANAGED
);
166 attr
->override_redirect
= !managed
;
167 attr
->colormap
= X11DRV_PALETTE_PaletteXColormap
;
168 attr
->save_under
= ((win
->clsStyle
& CS_SAVEBITS
) != 0);
169 attr
->cursor
= x11drv_thread_data()->cursor
;
170 attr
->event_mask
= (ExposureMask
| PointerMotionMask
|
171 ButtonPressMask
| ButtonReleaseMask
| EnterWindowMask
);
173 if (is_window_top_level( win
))
174 attr
->event_mask
|= (KeyPressMask
| KeyReleaseMask
| StructureNotifyMask
|
175 FocusChangeMask
| KeymapStateMask
);
177 return (CWOverrideRedirect
| CWSaveUnder
| CWEventMask
| CWColormap
| CWCursor
);
181 /***********************************************************************
182 * X11DRV_sync_window_style
184 * Change the X window attributes when the window style has changed.
186 void X11DRV_sync_window_style( Display
*display
, WND
*win
)
188 XSetWindowAttributes attr
;
192 mask
= get_window_attributes( display
, win
, &attr
);
193 XChangeWindowAttributes( display
, get_whole_window(win
), mask
, &attr
);
198 /***********************************************************************
201 * fill the window changes structure
203 static int get_window_changes( XWindowChanges
*changes
, const RECT
*old
, const RECT
*new )
207 if (old
->right
- old
->left
!= new->right
- new->left
)
209 if (!(changes
->width
= new->right
- new->left
)) changes
->width
= 1;
212 if (old
->bottom
- old
->top
!= new->bottom
- new->top
)
214 if (!(changes
->height
= new->bottom
- new->top
)) changes
->height
= 1;
217 if (old
->left
!= new->left
)
219 changes
->x
= new->left
;
222 if (old
->top
!= new->top
)
224 changes
->y
= new->top
;
231 /***********************************************************************
234 static Window
create_icon_window( Display
*display
, WND
*win
)
236 struct x11drv_win_data
*data
= win
->pDriverData
;
237 XSetWindowAttributes attr
;
239 attr
.event_mask
= (ExposureMask
| KeyPressMask
| KeyReleaseMask
| PointerMotionMask
|
240 ButtonPressMask
| ButtonReleaseMask
| EnterWindowMask
);
241 attr
.bit_gravity
= NorthWestGravity
;
242 attr
.backing_store
= NotUseful
/*WhenMapped*/;
243 attr
.colormap
= X11DRV_PALETTE_PaletteXColormap
; /* Needed due to our visual */
246 data
->icon_window
= XCreateWindow( display
, root_window
, 0, 0,
247 GetSystemMetrics( SM_CXICON
),
248 GetSystemMetrics( SM_CYICON
),
251 CWEventMask
| CWBitGravity
| CWBackingStore
| CWColormap
, &attr
);
252 XSaveContext( display
, data
->icon_window
, winContext
, (char *)win
->hwndSelf
);
255 TRACE( "created %lx\n", data
->icon_window
);
256 SetPropA( win
->hwndSelf
, icon_window_atom
, (HANDLE
)data
->icon_window
);
257 return data
->icon_window
;
262 /***********************************************************************
263 * destroy_icon_window
265 inline static void destroy_icon_window( Display
*display
, WND
*win
)
267 struct x11drv_win_data
*data
= win
->pDriverData
;
269 if (!data
->icon_window
) return;
270 if (x11drv_thread_data()->cursor_window
== data
->icon_window
)
271 x11drv_thread_data()->cursor_window
= None
;
273 XDeleteContext( display
, data
->icon_window
, winContext
);
274 XDestroyWindow( display
, data
->icon_window
);
275 data
->icon_window
= 0;
277 RemovePropA( win
->hwndSelf
, icon_window_atom
);
281 /***********************************************************************
284 * Set the icon wm hints
286 static void set_icon_hints( Display
*display
, WND
*wndPtr
, XWMHints
*hints
, HICON hIcon
)
288 X11DRV_WND_DATA
*data
= wndPtr
->pDriverData
;
290 if (data
->hWMIconBitmap
) DeleteObject( data
->hWMIconBitmap
);
291 if (data
->hWMIconMask
) DeleteObject( data
->hWMIconMask
);
292 data
->hWMIconBitmap
= 0;
293 data
->hWMIconMask
= 0;
295 if (!(wndPtr
->dwExStyle
& WS_EX_MANAGED
))
297 destroy_icon_window( display
, wndPtr
);
298 hints
->flags
&= ~(IconPixmapHint
| IconMaskHint
| IconWindowHint
);
302 if (!data
->icon_window
) create_icon_window( display
, wndPtr
);
303 hints
->icon_window
= data
->icon_window
;
304 hints
->flags
= (hints
->flags
& ~(IconPixmapHint
| IconMaskHint
)) | IconWindowHint
;
314 GetIconInfo(hIcon
, &ii
);
316 GetObjectA(ii
.hbmMask
, sizeof(bmMask
), &bmMask
);
319 rcMask
.right
= bmMask
.bmWidth
;
320 rcMask
.bottom
= bmMask
.bmHeight
;
322 hDC
= CreateCompatibleDC(0);
323 hbmOrig
= SelectObject(hDC
, ii
.hbmMask
);
324 InvertRect(hDC
, &rcMask
);
325 SelectObject(hDC
, ii
.hbmColor
); /* force the color bitmap to x11drv mode too */
326 SelectObject(hDC
, hbmOrig
);
329 data
->hWMIconBitmap
= ii
.hbmColor
;
330 data
->hWMIconMask
= ii
.hbmMask
;
332 hints
->icon_pixmap
= X11DRV_BITMAP_Pixmap(data
->hWMIconBitmap
);
333 hints
->icon_mask
= X11DRV_BITMAP_Pixmap(data
->hWMIconMask
);
334 destroy_icon_window( display
, wndPtr
);
335 hints
->flags
= (hints
->flags
& ~IconWindowHint
) | IconPixmapHint
| IconMaskHint
;
340 /***********************************************************************
343 * set the window size hints
345 static void set_size_hints( Display
*display
, WND
*win
)
347 XSizeHints
* size_hints
;
348 struct x11drv_win_data
*data
= win
->pDriverData
;
350 if ((size_hints
= XAllocSizeHints()))
352 size_hints
->win_gravity
= StaticGravity
;
353 size_hints
->x
= data
->whole_rect
.left
;
354 size_hints
->y
= data
->whole_rect
.top
;
355 size_hints
->flags
= PWinGravity
| PPosition
;
357 if ( !(win
->dwStyle
& WS_THICKFRAME
) )
359 size_hints
->max_width
= data
->whole_rect
.right
- data
->whole_rect
.left
;
360 size_hints
->max_height
= data
->whole_rect
.bottom
- data
->whole_rect
.top
;
361 size_hints
->min_width
= size_hints
->max_width
;
362 size_hints
->min_height
= size_hints
->max_height
;
363 size_hints
->flags
|= PMinSize
| PMaxSize
;
365 XSetWMNormalHints( display
, data
->whole_window
, size_hints
);
371 /***********************************************************************
372 * X11DRV_set_wm_hints
374 * Set the window manager hints for a newly-created window
376 void X11DRV_set_wm_hints( Display
*display
, WND
*win
)
378 struct x11drv_win_data
*data
= win
->pDriverData
;
380 XClassHint
*class_hints
;
391 protocols
[i
++] = x11drv_atom(WM_DELETE_WINDOW
);
392 protocols
[i
++] = x11drv_atom(_NET_WM_PING
);
393 if (use_take_focus
) protocols
[i
++] = x11drv_atom(WM_TAKE_FOCUS
);
394 XChangeProperty( display
, data
->whole_window
, x11drv_atom(WM_PROTOCOLS
),
395 XA_ATOM
, 32, PropModeReplace
, (char *)protocols
, i
);
398 if ((class_hints
= XAllocClassHint()))
400 class_hints
->res_name
= "wine";
401 class_hints
->res_class
= "Wine";
402 XSetClassHint( display
, data
->whole_window
, class_hints
);
403 XFree( class_hints
);
406 /* transient for hint */
409 Window owner_win
= X11DRV_get_whole_window( win
->owner
);
410 XSetTransientForHint( display
, data
->whole_window
, owner_win
);
411 group_leader
= owner_win
;
413 else group_leader
= data
->whole_window
;
416 set_size_hints( display
, win
);
418 /* systray properties (KDE only for now) */
419 if (win
->dwExStyle
& WS_EX_TRAYWINDOW
)
422 XChangeProperty( display
, data
->whole_window
, x11drv_atom(KWM_DOCKWINDOW
),
423 x11drv_atom(KWM_DOCKWINDOW
), 32, PropModeReplace
, (char*)&val
, 1 );
424 XChangeProperty( display
, data
->whole_window
, x11drv_atom(_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR
),
425 XA_WINDOW
, 32, PropModeReplace
, (char*)&data
->whole_window
, 1 );
428 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
429 XSetWMProperties(display
, data
->whole_window
, NULL
, NULL
, NULL
, 0, NULL
, NULL
, NULL
);
430 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
432 XChangeProperty(display
, data
->whole_window
, x11drv_atom(_NET_WM_PID
),
433 XA_CARDINAL
, 32, PropModeReplace
, (char *)&i
, 1);
435 /* map WS_EX_TOOLWINDOW to _NET_WM_WINDOW_TYPE_UTILITY */
436 if (win
->dwExStyle
& WS_EX_TOOLWINDOW
)
438 Atom a
= x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY
);
439 XChangeProperty(display
, data
->whole_window
, x11drv_atom(_NET_WM_WINDOW_TYPE
),
440 XA_ATOM
, 32, PropModeReplace
, (char*)&a
, 1);
443 mwm_hints
.flags
= MWM_HINTS_FUNCTIONS
| MWM_HINTS_DECORATIONS
;
444 mwm_hints
.functions
= 0;
445 if ((win
->dwStyle
& WS_CAPTION
) == WS_CAPTION
) mwm_hints
.functions
|= MWM_FUNC_MOVE
;
446 if (win
->dwStyle
& WS_THICKFRAME
) mwm_hints
.functions
|= MWM_FUNC_MOVE
| MWM_FUNC_RESIZE
;
447 if (win
->dwStyle
& WS_MINIMIZEBOX
) mwm_hints
.functions
|= MWM_FUNC_MINIMIZE
;
448 if (win
->dwStyle
& WS_MAXIMIZEBOX
) mwm_hints
.functions
|= MWM_FUNC_MAXIMIZE
;
449 if (win
->dwStyle
& WS_SYSMENU
) mwm_hints
.functions
|= MWM_FUNC_CLOSE
;
450 mwm_hints
.decorations
= 0;
451 if ((win
->dwStyle
& WS_CAPTION
) == WS_CAPTION
) mwm_hints
.decorations
|= MWM_DECOR_TITLE
;
452 if (win
->dwExStyle
& WS_EX_DLGMODALFRAME
) mwm_hints
.decorations
|= MWM_DECOR_BORDER
;
453 else if (win
->dwStyle
& WS_THICKFRAME
) mwm_hints
.decorations
|= MWM_DECOR_BORDER
| MWM_DECOR_RESIZEH
;
454 else if ((win
->dwStyle
& (WS_DLGFRAME
|WS_BORDER
)) == WS_DLGFRAME
) mwm_hints
.decorations
|= MWM_DECOR_BORDER
;
455 else if (win
->dwStyle
& WS_BORDER
) mwm_hints
.decorations
|= MWM_DECOR_BORDER
;
456 else if (!(win
->dwStyle
& (WS_CHILD
|WS_POPUP
))) mwm_hints
.decorations
|= MWM_DECOR_BORDER
;
457 if (win
->dwStyle
& WS_SYSMENU
) mwm_hints
.decorations
|= MWM_DECOR_MENU
;
458 if (win
->dwStyle
& WS_MINIMIZEBOX
) mwm_hints
.decorations
|= MWM_DECOR_MINIMIZE
;
459 if (win
->dwStyle
& WS_MAXIMIZEBOX
) mwm_hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
461 XChangeProperty( display
, data
->whole_window
, x11drv_atom(_MOTIF_WM_HINTS
),
462 x11drv_atom(_MOTIF_WM_HINTS
), 32, PropModeReplace
,
463 (char*)&mwm_hints
, sizeof(mwm_hints
)/sizeof(long) );
465 XChangeProperty( display
, data
->whole_window
, x11drv_atom(XdndAware
),
466 XA_ATOM
, 32, PropModeReplace
, (unsigned char*)&dndVersion
, 1 );
468 wm_hints
= XAllocWMHints();
474 wm_hints
->flags
= InputHint
| StateHint
| WindowGroupHint
;
475 wm_hints
->input
= !(win
->dwStyle
& WS_DISABLED
);
477 set_icon_hints( display
, win
, wm_hints
, (HICON
)GetClassLongA( win
->hwndSelf
, GCL_HICON
));
479 wm_hints
->initial_state
= (win
->dwStyle
& WS_MINIMIZE
) ? IconicState
: NormalState
;
480 wm_hints
->window_group
= group_leader
;
483 XSetWMHints( display
, data
->whole_window
, wm_hints
);
490 /***********************************************************************
491 * X11DRV_set_iconic_state
493 * Set the X11 iconic state according to the window style.
495 void X11DRV_set_iconic_state( WND
*win
)
497 Display
*display
= thread_display();
498 struct x11drv_win_data
*data
= win
->pDriverData
;
500 BOOL iconic
= IsIconic( win
->hwndSelf
);
504 if (iconic
) XUnmapWindow( display
, data
->client_window
);
505 else if (is_client_window_mapped( win
)) XMapWindow( display
, data
->client_window
);
507 if (!(wm_hints
= XGetWMHints( display
, data
->whole_window
))) wm_hints
= XAllocWMHints();
508 wm_hints
->flags
|= StateHint
| IconPositionHint
;
509 wm_hints
->initial_state
= iconic
? IconicState
: NormalState
;
510 wm_hints
->icon_x
= win
->rectWindow
.left
;
511 wm_hints
->icon_y
= win
->rectWindow
.top
;
512 XSetWMHints( display
, data
->whole_window
, wm_hints
);
514 if (win
->dwStyle
& WS_VISIBLE
)
517 XIconifyWindow( display
, data
->whole_window
, DefaultScreen(display
) );
519 if (!IsRectEmpty( &win
->rectWindow
)) XMapWindow( display
, data
->whole_window
);
527 /***********************************************************************
528 * X11DRV_window_to_X_rect
530 * Convert a rect from client to X window coordinates
532 void X11DRV_window_to_X_rect( WND
*win
, RECT
*rect
)
536 if (!(win
->dwExStyle
& WS_EX_MANAGED
)) return;
537 if (IsRectEmpty( rect
)) return;
539 rc
.top
= rc
.bottom
= rc
.left
= rc
.right
= 0;
541 AdjustWindowRectEx( &rc
, win
->dwStyle
& ~(WS_HSCROLL
|WS_VSCROLL
), FALSE
, win
->dwExStyle
);
543 rect
->left
-= rc
.left
;
544 rect
->right
-= rc
.right
;
546 rect
->bottom
-= rc
.bottom
;
547 if (rect
->top
>= rect
->bottom
) rect
->bottom
= rect
->top
+ 1;
548 if (rect
->left
>= rect
->right
) rect
->right
= rect
->left
+ 1;
552 /***********************************************************************
553 * X11DRV_X_to_window_rect
555 * Opposite of X11DRV_window_to_X_rect
557 void X11DRV_X_to_window_rect( WND
*win
, RECT
*rect
)
559 if (!(win
->dwExStyle
& WS_EX_MANAGED
)) return;
560 if (IsRectEmpty( rect
)) return;
562 AdjustWindowRectEx( rect
, win
->dwStyle
& ~(WS_HSCROLL
|WS_VSCROLL
), FALSE
, win
->dwExStyle
);
564 if (rect
->top
>= rect
->bottom
) rect
->bottom
= rect
->top
+ 1;
565 if (rect
->left
>= rect
->right
) rect
->right
= rect
->left
+ 1;
569 /***********************************************************************
570 * X11DRV_sync_whole_window_position
572 * Synchronize the X whole window position with the Windows one
574 int X11DRV_sync_whole_window_position( Display
*display
, WND
*win
, int zorder
)
576 XWindowChanges changes
;
578 struct x11drv_win_data
*data
= win
->pDriverData
;
579 RECT whole_rect
= win
->rectWindow
;
581 X11DRV_window_to_X_rect( win
, &whole_rect
);
582 mask
= get_window_changes( &changes
, &data
->whole_rect
, &whole_rect
);
586 if (is_window_top_level( win
))
588 /* find window that this one must be after */
589 HWND prev
= GetWindow( win
->hwndSelf
, GW_HWNDPREV
);
590 while (prev
&& !(GetWindowLongW( prev
, GWL_STYLE
) & WS_VISIBLE
))
591 prev
= GetWindow( prev
, GW_HWNDPREV
);
592 if (!prev
) /* top child */
594 changes
.stack_mode
= Above
;
599 /* should use stack_mode Below but most window managers don't get it right */
600 /* so move it above the next one in Z order */
601 HWND next
= GetWindow( win
->hwndSelf
, GW_HWNDNEXT
);
602 while (next
&& !(GetWindowLongW( next
, GWL_STYLE
) & WS_VISIBLE
))
603 next
= GetWindow( next
, GW_HWNDNEXT
);
606 changes
.stack_mode
= Above
;
607 changes
.sibling
= X11DRV_get_whole_window(next
);
608 mask
|= CWStackMode
| CWSibling
;
614 HWND next
= GetWindow( win
->hwndSelf
, GW_HWNDNEXT
);
616 if (win
->parent
== GetDesktopWindow() &&
617 root_window
!= DefaultRootWindow(display
))
619 /* in desktop mode we need the sibling to belong to the same process */
622 WND
*ptr
= WIN_GetPtr( next
);
623 if (ptr
!= WND_OTHER_PROCESS
)
625 WIN_ReleasePtr( ptr
);
628 next
= GetWindow( next
, GW_HWNDNEXT
);
632 if (!next
) /* bottom child */
634 changes
.stack_mode
= Below
;
639 changes
.stack_mode
= Above
;
640 changes
.sibling
= X11DRV_get_whole_window(next
);
641 mask
|= CWStackMode
| CWSibling
;
646 data
->whole_rect
= whole_rect
;
650 TRACE( "setting win %lx pos %ld,%ld,%ldx%ld after %lx changes=%x\n",
651 data
->whole_window
, whole_rect
.left
, whole_rect
.top
,
652 whole_rect
.right
- whole_rect
.left
, whole_rect
.bottom
- whole_rect
.top
,
653 changes
.sibling
, mask
);
655 XSync( gdi_display
, False
); /* flush graphics operations before moving the window */
656 if (is_window_top_level( win
))
658 if (mask
& (CWWidth
|CWHeight
)) set_size_hints( display
, win
);
659 XReconfigureWMWindow( display
, data
->whole_window
,
660 DefaultScreen(display
), mask
, &changes
);
662 else XConfigureWindow( display
, data
->whole_window
, mask
, &changes
);
669 /***********************************************************************
670 * X11DRV_sync_client_window_position
672 * Synchronize the X client window position with the Windows one
674 int X11DRV_sync_client_window_position( Display
*display
, WND
*win
)
676 XWindowChanges changes
;
678 struct x11drv_win_data
*data
= win
->pDriverData
;
679 RECT client_rect
= win
->rectClient
;
681 OffsetRect( &client_rect
, -data
->whole_rect
.left
, -data
->whole_rect
.top
);
683 if ((mask
= get_window_changes( &changes
, &data
->client_rect
, &client_rect
)))
685 BOOL was_mapped
= is_client_window_mapped( win
);
687 TRACE( "setting win %lx pos %ld,%ld,%ldx%ld (was %ld,%ld,%ldx%ld) after %lx changes=%x\n",
688 data
->client_window
, client_rect
.left
, client_rect
.top
,
689 client_rect
.right
- client_rect
.left
, client_rect
.bottom
- client_rect
.top
,
690 data
->client_rect
.left
, data
->client_rect
.top
,
691 data
->client_rect
.right
- data
->client_rect
.left
,
692 data
->client_rect
.bottom
- data
->client_rect
.top
,
693 changes
.sibling
, mask
);
694 data
->client_rect
= client_rect
;
696 XSync( gdi_display
, False
); /* flush graphics operations before moving the window */
697 if (was_mapped
&& !is_client_window_mapped( win
))
698 XUnmapWindow( display
, data
->client_window
);
699 XConfigureWindow( display
, data
->client_window
, mask
, &changes
);
700 if (!was_mapped
&& is_client_window_mapped( win
))
701 XMapWindow( display
, data
->client_window
);
708 /***********************************************************************
709 * X11DRV_register_window
711 * Associate an X window to a HWND.
713 void X11DRV_register_window( Display
*display
, HWND hwnd
, struct x11drv_win_data
*data
)
716 XSaveContext( display
, data
->whole_window
, winContext
, (char *)hwnd
);
717 XSaveContext( display
, data
->client_window
, winContext
, (char *)hwnd
);
722 /**********************************************************************
725 static void create_desktop( Display
*display
, WND
*wndPtr
)
727 X11DRV_WND_DATA
*data
= wndPtr
->pDriverData
;
730 winContext
= XUniqueContext();
731 XInternAtoms( display
, (char **)atom_names
, NB_XATOMS
- FIRST_XATOM
, False
, X11DRV_Atoms
);
734 whole_window_atom
= MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_whole_window" ));
735 client_window_atom
= MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_client_window" ));
736 icon_window_atom
= MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_icon_window" ));
738 data
->whole_window
= data
->client_window
= root_window
;
739 data
->whole_rect
= data
->client_rect
= wndPtr
->rectWindow
;
741 SetPropA( wndPtr
->hwndSelf
, whole_window_atom
, (HANDLE
)root_window
);
742 SetPropA( wndPtr
->hwndSelf
, client_window_atom
, (HANDLE
)root_window
);
743 SetPropA( wndPtr
->hwndSelf
, "__wine_x11_visual_id", (HANDLE
)XVisualIDFromVisual(visual
) );
745 X11DRV_InitClipboard();
747 if (root_window
!= DefaultRootWindow(display
)) X11DRV_create_desktop_thread();
751 /**********************************************************************
752 * create_whole_window
754 * Create the whole X window for a given window
756 static Window
create_whole_window( Display
*display
, WND
*win
)
758 struct x11drv_win_data
*data
= win
->pDriverData
;
760 XSetWindowAttributes attr
;
763 BOOL is_top_level
= is_window_top_level( win
);
765 rect
= win
->rectWindow
;
766 X11DRV_window_to_X_rect( win
, &rect
);
768 if (!(cx
= rect
.right
- rect
.left
)) cx
= 1;
769 if (!(cy
= rect
.bottom
- rect
.top
)) cy
= 1;
771 parent
= X11DRV_get_client_window( win
->parent
);
775 mask
= get_window_attributes( display
, win
, &attr
);
777 /* set the attributes that don't change over the lifetime of the window */
778 attr
.bit_gravity
= ForgetGravity
;
779 attr
.win_gravity
= NorthWestGravity
;
780 attr
.backing_store
= NotUseful
/*WhenMapped*/;
781 mask
|= CWBitGravity
| CWWinGravity
| CWBackingStore
;
783 data
->whole_rect
= rect
;
784 data
->whole_window
= XCreateWindow( display
, parent
, rect
.left
, rect
.top
, cx
, cy
,
785 0, screen_depth
, InputOutput
, visual
,
788 if (!data
->whole_window
)
794 /* non-maximized child must be at bottom of Z order */
795 if ((win
->dwStyle
& (WS_CHILD
|WS_MAXIMIZE
)) == WS_CHILD
)
797 XWindowChanges changes
;
798 changes
.stack_mode
= Below
;
799 XConfigureWindow( display
, data
->whole_window
, CWStackMode
, &changes
);
806 XIM xim
= x11drv_thread_data()->xim
;
807 if (xim
) data
->xic
= X11DRV_CreateIC( xim
, display
, data
->whole_window
);
808 X11DRV_set_wm_hints( display
, win
);
811 return data
->whole_window
;
815 /**********************************************************************
816 * create_client_window
818 * Create the client window for a given window
820 static Window
create_client_window( Display
*display
, WND
*win
)
822 struct x11drv_win_data
*data
= win
->pDriverData
;
823 RECT rect
= data
->whole_rect
;
824 XSetWindowAttributes attr
;
826 OffsetRect( &rect
, -data
->whole_rect
.left
, -data
->whole_rect
.top
);
827 data
->client_rect
= rect
;
829 attr
.event_mask
= (ExposureMask
| PointerMotionMask
|
830 ButtonPressMask
| ButtonReleaseMask
| EnterWindowMask
);
831 attr
.bit_gravity
= (win
->clsStyle
& (CS_VREDRAW
| CS_HREDRAW
)) ?
832 ForgetGravity
: NorthWestGravity
;
833 attr
.backing_store
= NotUseful
/*WhenMapped*/;
836 data
->client_window
= XCreateWindow( display
, data
->whole_window
, 0, 0,
837 max( rect
.right
- rect
.left
, 1 ),
838 max( rect
.bottom
- rect
.top
, 1 ),
841 CWEventMask
| CWBitGravity
| CWBackingStore
, &attr
);
842 if (data
->client_window
&& is_client_window_mapped( win
))
843 XMapWindow( display
, data
->client_window
);
845 return data
->client_window
;
849 /*****************************************************************
850 * SetWindowText (X11DRV.@)
852 BOOL
X11DRV_SetWindowText( HWND hwnd
, LPCWSTR text
)
854 Display
*display
= thread_display();
861 if ((win
= X11DRV_get_whole_window( hwnd
)))
863 /* allocate new buffer for window text */
864 count
= WideCharToMultiByte(CP_UNIXCP
, 0, text
, -1, NULL
, 0, NULL
, NULL
);
865 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, count
)))
867 ERR("Not enough memory for window text\n");
870 WideCharToMultiByte(CP_UNIXCP
, 0, text
, -1, buffer
, count
, NULL
, NULL
);
872 count
= WideCharToMultiByte(CP_UTF8
, 0, text
, strlenW(text
), NULL
, 0, NULL
, NULL
);
873 if (!(utf8_buffer
= HeapAlloc( GetProcessHeap(), 0, count
)))
875 ERR("Not enough memory for window text in UTF-8\n");
878 WideCharToMultiByte(CP_UTF8
, 0, text
, strlenW(text
), utf8_buffer
, count
, NULL
, NULL
);
881 if (XmbTextListToTextProperty( display
, &buffer
, 1, XStdICCTextStyle
, &prop
) == Success
)
883 XSetWMName( display
, win
, &prop
);
884 XSetWMIconName( display
, win
, &prop
);
888 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
889 according to the standard
890 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
892 XChangeProperty( display
, win
, x11drv_atom(_NET_WM_NAME
), x11drv_atom(UTF8_STRING
),
893 8, PropModeReplace
, (unsigned char *) utf8_buffer
, count
);
896 HeapFree( GetProcessHeap(), 0, utf8_buffer
);
897 HeapFree( GetProcessHeap(), 0, buffer
);
903 /***********************************************************************
904 * DestroyWindow (X11DRV.@)
906 BOOL
X11DRV_DestroyWindow( HWND hwnd
)
908 struct x11drv_thread_data
*thread_data
= x11drv_thread_data();
909 Display
*display
= thread_data
->display
;
910 WND
*wndPtr
= WIN_GetPtr( hwnd
);
911 X11DRV_WND_DATA
*data
= wndPtr
->pDriverData
;
913 if (!data
) goto done
;
915 if (data
->whole_window
)
917 TRACE( "win %p xwin %lx/%lx\n", hwnd
, data
->whole_window
, data
->client_window
);
918 if (thread_data
->cursor_window
== data
->whole_window
) thread_data
->cursor_window
= None
;
919 if (thread_data
->last_focus
== hwnd
) thread_data
->last_focus
= 0;
921 XSync( gdi_display
, False
); /* flush any reference to this drawable in GDI queue */
922 XDeleteContext( display
, data
->whole_window
, winContext
);
923 XDeleteContext( display
, data
->client_window
, winContext
);
924 XDestroyWindow( display
, data
->whole_window
); /* this destroys client too */
927 XUnsetICFocus( data
->xic
);
928 XDestroyIC( data
->xic
);
930 destroy_icon_window( display
, wndPtr
);
934 if (data
->hWMIconBitmap
) DeleteObject( data
->hWMIconBitmap
);
935 if (data
->hWMIconMask
) DeleteObject( data
->hWMIconMask
);
936 HeapFree( GetProcessHeap(), 0, data
);
937 wndPtr
->pDriverData
= NULL
;
939 WIN_ReleasePtr( wndPtr
);
944 /**********************************************************************
945 * CreateWindow (X11DRV.@)
947 BOOL
X11DRV_CreateWindow( HWND hwnd
, CREATESTRUCTA
*cs
, BOOL unicode
)
949 Display
*display
= thread_display();
951 struct x11drv_win_data
*data
;
958 ERR( "invalid window width %d\n", cs
->cx
);
963 ERR( "invalid window height %d\n", cs
->cx
);
967 if (!(data
= HeapAlloc(GetProcessHeap(), 0, sizeof(*data
)))) return FALSE
;
968 data
->whole_window
= 0;
969 data
->client_window
= 0;
970 data
->icon_window
= 0;
972 data
->hWMIconBitmap
= 0;
973 data
->hWMIconMask
= 0;
975 wndPtr
= WIN_GetPtr( hwnd
);
976 wndPtr
->pDriverData
= data
;
978 /* initialize the dimensions before sending WM_GETMINMAXINFO */
979 SetRect( &rect
, cs
->x
, cs
->y
, cs
->x
+ cs
->cx
, cs
->y
+ cs
->cy
);
980 WIN_SetRectangles( hwnd
, &rect
, &rect
);
984 create_desktop( display
, wndPtr
);
985 WIN_ReleasePtr( wndPtr
);
989 if (!create_whole_window( display
, wndPtr
)) goto failed
;
990 if (!create_client_window( display
, wndPtr
)) goto failed
;
992 XSync( display
, False
);
995 SetPropA( hwnd
, whole_window_atom
, (HANDLE
)data
->whole_window
);
996 SetPropA( hwnd
, client_window_atom
, (HANDLE
)data
->client_window
);
998 /* Call the WH_CBT hook */
1000 cbtc
.hwndInsertAfter
= HWND_TOP
;
1001 if (HOOK_CallHooks( WH_CBT
, HCBT_CREATEWND
, (WPARAM
)hwnd
, (LPARAM
)&cbtc
, unicode
))
1003 TRACE("CBT-hook returned !0\n");
1007 /* Send the WM_GETMINMAXINFO message and fix the size if needed */
1008 if ((cs
->style
& WS_THICKFRAME
) || !(cs
->style
& (WS_POPUP
| WS_CHILD
)))
1010 POINT maxSize
, maxPos
, minTrack
, maxTrack
;
1012 WIN_ReleasePtr( wndPtr
);
1013 WINPOS_GetMinMaxInfo( hwnd
, &maxSize
, &maxPos
, &minTrack
, &maxTrack
);
1014 if (maxSize
.x
< cs
->cx
) cs
->cx
= maxSize
.x
;
1015 if (maxSize
.y
< cs
->cy
) cs
->cy
= maxSize
.y
;
1016 if (cs
->cx
< minTrack
.x
) cs
->cx
= minTrack
.x
;
1017 if (cs
->cy
< minTrack
.y
) cs
->cy
= minTrack
.y
;
1018 if (cs
->cx
< 0) cs
->cx
= 0;
1019 if (cs
->cy
< 0) cs
->cy
= 0;
1021 if (!(wndPtr
= WIN_GetPtr( hwnd
))) return FALSE
;
1022 SetRect( &rect
, cs
->x
, cs
->y
, cs
->x
+ cs
->cx
, cs
->y
+ cs
->cy
);
1023 WIN_SetRectangles( hwnd
, &rect
, &rect
);
1024 X11DRV_sync_whole_window_position( display
, wndPtr
, 0 );
1026 WIN_ReleasePtr( wndPtr
);
1028 /* send WM_NCCREATE */
1029 TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd
, cs
->x
, cs
->y
, cs
->cx
, cs
->cy
);
1031 ret
= SendMessageW( hwnd
, WM_NCCREATE
, 0, (LPARAM
)cs
);
1033 ret
= SendMessageA( hwnd
, WM_NCCREATE
, 0, (LPARAM
)cs
);
1036 WARN("aborted by WM_xxCREATE!\n");
1040 if (!(wndPtr
= WIN_GetPtr(hwnd
))) return FALSE
;
1042 X11DRV_sync_window_style( display
, wndPtr
);
1044 /* send WM_NCCALCSIZE */
1045 rect
= wndPtr
->rectWindow
;
1046 WIN_ReleasePtr( wndPtr
);
1047 SendMessageW( hwnd
, WM_NCCALCSIZE
, FALSE
, (LPARAM
)&rect
);
1049 if (!(wndPtr
= WIN_GetPtr(hwnd
))) return FALSE
;
1050 if (rect
.left
> rect
.right
|| rect
.top
> rect
.bottom
) rect
= wndPtr
->rectWindow
;
1051 WIN_SetRectangles( hwnd
, &wndPtr
->rectWindow
, &rect
);
1052 X11DRV_sync_client_window_position( display
, wndPtr
);
1053 X11DRV_register_window( display
, hwnd
, data
);
1055 TRACE( "win %p window %ld,%ld,%ld,%ld client %ld,%ld,%ld,%ld whole %ld,%ld,%ld,%ld X client %ld,%ld,%ld,%ld xwin %x/%x\n",
1056 hwnd
, wndPtr
->rectWindow
.left
, wndPtr
->rectWindow
.top
,
1057 wndPtr
->rectWindow
.right
, wndPtr
->rectWindow
.bottom
,
1058 wndPtr
->rectClient
.left
, wndPtr
->rectClient
.top
,
1059 wndPtr
->rectClient
.right
, wndPtr
->rectClient
.bottom
,
1060 data
->whole_rect
.left
, data
->whole_rect
.top
,
1061 data
->whole_rect
.right
, data
->whole_rect
.bottom
,
1062 data
->client_rect
.left
, data
->client_rect
.top
,
1063 data
->client_rect
.right
, data
->client_rect
.bottom
,
1064 (unsigned int)data
->whole_window
, (unsigned int)data
->client_window
);
1066 if ((wndPtr
->dwStyle
& (WS_CHILD
|WS_MAXIMIZE
)) == WS_CHILD
)
1067 WIN_LinkWindow( hwnd
, wndPtr
->parent
, HWND_BOTTOM
);
1069 WIN_LinkWindow( hwnd
, wndPtr
->parent
, HWND_TOP
);
1071 WIN_ReleasePtr( wndPtr
);
1074 ret
= (SendMessageW( hwnd
, WM_CREATE
, 0, (LPARAM
)cs
) != -1);
1076 ret
= (SendMessageA( hwnd
, WM_CREATE
, 0, (LPARAM
)cs
) != -1);
1080 WIN_UnlinkWindow( hwnd
);
1084 /* Send the size messages */
1086 if (!(wndPtr
= WIN_FindWndPtr(hwnd
))) return FALSE
;
1087 if (!(wndPtr
->flags
& WIN_NEED_SIZE
))
1089 /* send it anyway */
1090 if (((wndPtr
->rectClient
.right
-wndPtr
->rectClient
.left
) <0)
1091 ||((wndPtr
->rectClient
.bottom
-wndPtr
->rectClient
.top
)<0))
1092 WARN("sending bogus WM_SIZE message 0x%08lx\n",
1093 MAKELONG(wndPtr
->rectClient
.right
-wndPtr
->rectClient
.left
,
1094 wndPtr
->rectClient
.bottom
-wndPtr
->rectClient
.top
));
1095 SendMessageW( hwnd
, WM_SIZE
, SIZE_RESTORED
,
1096 MAKELONG(wndPtr
->rectClient
.right
-wndPtr
->rectClient
.left
,
1097 wndPtr
->rectClient
.bottom
-wndPtr
->rectClient
.top
));
1098 SendMessageW( hwnd
, WM_MOVE
, 0,
1099 MAKELONG( wndPtr
->rectClient
.left
, wndPtr
->rectClient
.top
) );
1102 /* Show the window, maximizing or minimizing if needed */
1104 if (wndPtr
->dwStyle
& (WS_MINIMIZE
| WS_MAXIMIZE
))
1106 extern UINT
WINPOS_MinMaximize( HWND hwnd
, UINT cmd
, LPRECT rect
); /*FIXME*/
1109 UINT swFlag
= (wndPtr
->dwStyle
& WS_MINIMIZE
) ? SW_MINIMIZE
: SW_MAXIMIZE
;
1110 WIN_SetStyle( hwnd
, wndPtr
->dwStyle
& ~(WS_MAXIMIZE
| WS_MINIMIZE
) );
1111 WINPOS_MinMaximize( hwnd
, swFlag
, &newPos
);
1112 swFlag
= ((wndPtr
->dwStyle
& WS_CHILD
) || GetActiveWindow())
1113 ? SWP_NOACTIVATE
| SWP_NOZORDER
| SWP_FRAMECHANGED
1114 : SWP_NOZORDER
| SWP_FRAMECHANGED
;
1115 SetWindowPos( hwnd
, 0, newPos
.left
, newPos
.top
,
1116 newPos
.right
, newPos
.bottom
, swFlag
);
1119 WIN_ReleaseWndPtr( wndPtr
);
1123 X11DRV_DestroyWindow( hwnd
);
1124 if (wndPtr
) WIN_ReleasePtr( wndPtr
);
1129 /***********************************************************************
1130 * X11DRV_get_client_window
1132 * Return the X window associated with the client area of a window
1134 Window
X11DRV_get_client_window( HWND hwnd
)
1137 WND
*win
= WIN_GetPtr( hwnd
);
1139 if (win
== WND_OTHER_PROCESS
)
1140 return (Window
)GetPropA( hwnd
, client_window_atom
);
1144 struct x11drv_win_data
*data
= win
->pDriverData
;
1145 ret
= data
->client_window
;
1146 WIN_ReleasePtr( win
);
1152 /***********************************************************************
1153 * X11DRV_get_whole_window
1155 * Return the X window associated with the full area of a window
1157 Window
X11DRV_get_whole_window( HWND hwnd
)
1160 WND
*win
= WIN_GetPtr( hwnd
);
1162 if (win
== WND_OTHER_PROCESS
)
1163 return (Window
)GetPropA( hwnd
, whole_window_atom
);
1167 struct x11drv_win_data
*data
= win
->pDriverData
;
1168 ret
= data
->whole_window
;
1169 WIN_ReleasePtr( win
);
1175 /***********************************************************************
1178 * Return the X input context associated with a window
1180 XIC
X11DRV_get_ic( HWND hwnd
)
1183 WND
*win
= WIN_GetPtr( hwnd
);
1185 if (win
&& win
!= WND_OTHER_PROCESS
)
1187 struct x11drv_win_data
*data
= win
->pDriverData
;
1189 WIN_ReleasePtr( win
);
1195 /*****************************************************************
1196 * SetParent (X11DRV.@)
1198 HWND
X11DRV_SetParent( HWND hwnd
, HWND parent
)
1200 Display
*display
= thread_display();
1204 /* Windows hides the window first, then shows it again
1205 * including the WM_SHOWWINDOW messages and all */
1206 BOOL was_visible
= ShowWindow( hwnd
, SW_HIDE
);
1208 if (!IsWindow( parent
)) return 0;
1209 if (!(wndPtr
= WIN_GetPtr(hwnd
)) || wndPtr
== WND_OTHER_PROCESS
) return 0;
1211 retvalue
= wndPtr
->parent
; /* old parent */
1212 if (parent
!= retvalue
)
1214 struct x11drv_win_data
*data
= wndPtr
->pDriverData
;
1216 WIN_LinkWindow( hwnd
, parent
, HWND_TOP
);
1218 if (parent
!= GetDesktopWindow()) /* a child window */
1220 if (!(wndPtr
->dwStyle
& WS_CHILD
))
1222 HMENU menu
= (HMENU
)SetWindowLongW( hwnd
, GWL_ID
, 0 );
1223 if (menu
) DestroyMenu( menu
);
1227 if (is_window_top_level( wndPtr
)) X11DRV_set_wm_hints( display
, wndPtr
);
1229 X11DRV_sync_window_style( display
, wndPtr
);
1230 XReparentWindow( display
, data
->whole_window
, X11DRV_get_client_window(parent
),
1231 data
->whole_rect
.left
, data
->whole_rect
.top
);
1232 wine_tsx11_unlock();
1234 WIN_ReleasePtr( wndPtr
);
1236 /* SetParent additionally needs to make hwnd the topmost window
1237 in the x-order and send the expected WM_WINDOWPOSCHANGING and
1238 WM_WINDOWPOSCHANGED notification messages.
1240 SetWindowPos( hwnd
, HWND_TOPMOST
, 0, 0, 0, 0,
1241 SWP_NOACTIVATE
| SWP_NOMOVE
| SWP_NOSIZE
| (was_visible
? SWP_SHOWWINDOW
: 0) );
1242 /* FIXME: a WM_MOVE is also generated (in the DefWindowProc handler
1243 * for WM_WINDOWPOSCHANGED) in Windows, should probably remove SWP_NOMOVE */
1249 /*****************************************************************
1250 * SetFocus (X11DRV.@)
1253 * Explicit colormap management seems to work only with OLVWM.
1255 void X11DRV_SetFocus( HWND hwnd
)
1257 Display
*display
= thread_display();
1258 XWindowAttributes win_attr
;
1261 /* Only mess with the X focus if there's */
1262 /* no desktop window and if the window is not managed by the WM. */
1263 if (root_window
!= DefaultRootWindow(display
)) return;
1265 if (!hwnd
) /* If setting the focus to 0, uninstall the colormap */
1268 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_PRIVATE
)
1269 XUninstallColormap( display
, X11DRV_PALETTE_PaletteXColormap
);
1270 wine_tsx11_unlock();
1274 hwnd
= GetAncestor( hwnd
, GA_ROOT
);
1275 if (GetWindowLongW( hwnd
, GWL_EXSTYLE
) & WS_EX_MANAGED
) return;
1276 if (!(win
= X11DRV_get_whole_window( hwnd
))) return;
1278 /* Set X focus and install colormap */
1280 if (XGetWindowAttributes( display
, win
, &win_attr
) &&
1281 (win_attr
.map_state
== IsViewable
))
1283 /* If window is not viewable, don't change anything */
1285 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1286 /* FIXME: this is not entirely correct */
1287 XSetInputFocus( display
, win
, RevertToParent
,
1288 /*CurrentTime*/ GetMessageTime() + X11DRV_server_startticks
);
1289 if (X11DRV_PALETTE_PaletteFlags
& X11DRV_PALETTE_PRIVATE
)
1290 XInstallColormap( display
, X11DRV_PALETTE_PaletteXColormap
);
1292 wine_tsx11_unlock();
1296 /**********************************************************************
1297 * SetWindowIcon (X11DRV.@)
1299 * hIcon or hIconSm has changed (or is being initialised for the
1300 * first time). Complete the X11 driver-specific initialisation
1301 * and set the window hints.
1303 * This is not entirely correct, may need to create
1304 * an icon window and set the pixmap as a background
1306 void X11DRV_SetWindowIcon( HWND hwnd
, UINT type
, HICON icon
)
1309 Display
*display
= thread_display();
1311 if (type
!= ICON_BIG
) return; /* nothing to do here */
1313 if (!(wndPtr
= WIN_GetPtr( hwnd
)) || wndPtr
== WND_OTHER_PROCESS
) return;
1315 if (wndPtr
->dwExStyle
& WS_EX_MANAGED
)
1317 Window win
= get_whole_window(wndPtr
);
1321 if (!(wm_hints
= XGetWMHints( display
, win
))) wm_hints
= XAllocWMHints();
1322 wine_tsx11_unlock();
1325 set_icon_hints( display
, wndPtr
, wm_hints
, icon
);
1327 XSetWMHints( display
, win
, wm_hints
);
1329 wine_tsx11_unlock();
1332 WIN_ReleasePtr( wndPtr
);