2 * Window related functions
4 * Copyright 1993, 1994, 1995, 1996, 2001 Alexandre Julliard
5 * Copyright 1993 David Metcalfe
6 * Copyright 1995, 1996 Alex Korobka
20 #include "wine/unicode.h"
22 #include "debugtools.h"
29 DEFAULT_DEBUG_CHANNEL(x11drv);
31 extern Pixmap X11DRV_BITMAP_Pixmap( HBITMAP );
33 #define HAS_DLGFRAME(style,exStyle) \
34 (((exStyle) & WS_EX_DLGMODALFRAME) || \
35 (((style) & WS_DLGFRAME) && !((style) & WS_THICKFRAME)))
37 /* X context to associate a hwnd to an X window */
38 XContext winContext = 0;
40 Atom wmProtocols = None;
41 Atom wmDeleteWindow = None;
42 Atom wmTakeFocus = None;
43 Atom dndProtocol = None;
44 Atom dndSelection = None;
45 Atom wmChangeState = None;
46 Atom kwmDockWindow = None;
47 Atom _kde_net_wm_system_tray_window_for = None; /* KDE 2 Final */
49 static LPCSTR whole_window_atom;
50 static LPCSTR client_window_atom;
51 static LPCSTR icon_window_atom;
53 /***********************************************************************
56 * Check if a given window should be managed
58 inline static BOOL is_window_managed( WND *win )
60 if (!Options.managed) return FALSE;
62 /* tray window is always managed */
63 if (win->dwExStyle & WS_EX_TRAYWINDOW) return TRUE;
64 /* child windows are not managed */
65 if (win->dwStyle & WS_CHILD) return FALSE;
66 /* tool windows are not managed */
67 if (win->dwExStyle & WS_EX_TOOLWINDOW) return FALSE;
68 /* windows with caption or thick frame are managed */
69 if ((win->dwStyle & WS_CAPTION) == WS_CAPTION) return TRUE;
70 if (win->dwStyle & WS_THICKFRAME) return TRUE;
71 /* default: not managed */
76 /***********************************************************************
79 * Check if a given window is a top level X11 window
81 inline static BOOL is_window_top_level( WND *win )
83 return (root_window == DefaultRootWindow(gdi_display) && win->parent == GetDesktopWindow());
87 /***********************************************************************
88 * is_client_window_mapped
90 * Check if the X client window should be mapped
92 inline static BOOL is_client_window_mapped( WND *win )
94 struct x11drv_win_data *data = win->pDriverData;
95 return !(win->dwStyle & WS_MINIMIZE) && !IsRectEmpty( &data->client_rect );
99 /***********************************************************************
100 * get_window_attributes
102 * Fill the window attributes structure for an X window.
103 * Returned cursor must be freed by caller.
105 static int get_window_attributes( Display *display, WND *win, XSetWindowAttributes *attr )
107 BOOL is_top_level = is_window_top_level( win );
108 BOOL managed = is_top_level && is_window_managed( win );
110 if (managed) WIN_SetExStyle( win->hwndSelf, win->dwExStyle | WS_EX_MANAGED );
111 else WIN_SetExStyle( win->hwndSelf, win->dwExStyle & ~WS_EX_MANAGED );
113 attr->override_redirect = !managed;
114 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
115 attr->save_under = ((win->clsStyle & CS_SAVEBITS) != 0);
117 attr->event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
118 ButtonPressMask | ButtonReleaseMask);
119 if (is_window_top_level( win ))
121 attr->event_mask |= StructureNotifyMask | FocusChangeMask | KeymapStateMask;
122 attr->cursor = X11DRV_GetCursor( display, GlobalLock16(GetCursor()) );
124 return (CWOverrideRedirect | CWSaveUnder | CWEventMask | CWColormap | CWCursor);
128 /***********************************************************************
131 * Change the X window attributes when the window style has changed.
133 static void sync_window_style( Display *display, WND *win )
135 XSetWindowAttributes attr;
139 mask = get_window_attributes( display, win, &attr );
140 XChangeWindowAttributes( display, get_whole_window(win), mask, &attr );
141 if (attr.cursor) XFreeCursor( display, attr.cursor );
146 /***********************************************************************
149 * fill the window changes structure
151 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
155 if (old->right - old->left != new->right - new->left )
157 if (!(changes->width = new->right - new->left)) changes->width = 1;
160 if (old->bottom - old->top != new->bottom - new->top)
162 if (!(changes->height = new->bottom - new->top)) changes->height = 1;
165 if (old->left != new->left)
167 changes->x = new->left;
170 if (old->top != new->top)
172 changes->y = new->top;
179 /***********************************************************************
182 static Window create_icon_window( Display *display, WND *win )
184 struct x11drv_win_data *data = win->pDriverData;
185 XSetWindowAttributes attr;
187 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
188 ButtonPressMask | ButtonReleaseMask);
189 attr.bit_gravity = NorthWestGravity;
190 attr.backing_store = NotUseful/*WhenMapped*/;
191 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
194 data->icon_window = XCreateWindow( display, root_window, 0, 0,
195 GetSystemMetrics( SM_CXICON ),
196 GetSystemMetrics( SM_CYICON ),
199 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
200 XSaveContext( display, data->icon_window, winContext, (char *)win->hwndSelf );
203 TRACE( "created %lx\n", data->icon_window );
204 SetPropA( win->hwndSelf, icon_window_atom, (HANDLE)data->icon_window );
205 return data->icon_window;
210 /***********************************************************************
211 * destroy_icon_window
213 inline static void destroy_icon_window( Display *display, WND *win )
215 struct x11drv_win_data *data = win->pDriverData;
217 if (!data->icon_window) return;
219 XDeleteContext( display, data->icon_window, winContext );
220 XDestroyWindow( display, data->icon_window );
221 data->icon_window = 0;
223 RemovePropA( win->hwndSelf, icon_window_atom );
227 /***********************************************************************
230 * Set the icon wm hints
232 static void set_icon_hints( Display *display, WND *wndPtr, XWMHints *hints )
234 X11DRV_WND_DATA *data = wndPtr->pDriverData;
235 HICON hIcon = GetClassLongA( wndPtr->hwndSelf, GCL_HICON );
237 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
238 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
239 data->hWMIconBitmap = 0;
240 data->hWMIconMask = 0;
242 if (!(wndPtr->dwExStyle & WS_EX_MANAGED))
244 destroy_icon_window( display, wndPtr );
245 hints->flags &= ~(IconPixmapHint | IconMaskHint | IconWindowHint);
249 if (!data->icon_window) create_icon_window( display, wndPtr );
250 hints->icon_window = data->icon_window;
251 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
261 GetIconInfo(hIcon, &ii);
263 X11DRV_CreateBitmap(ii.hbmMask);
264 X11DRV_CreateBitmap(ii.hbmColor);
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, hbmOrig);
278 data->hWMIconBitmap = ii.hbmColor;
279 data->hWMIconMask = ii.hbmMask;
281 hints->icon_pixmap = X11DRV_BITMAP_Pixmap(data->hWMIconBitmap);
282 hints->icon_mask = X11DRV_BITMAP_Pixmap(data->hWMIconMask);
283 destroy_icon_window( display, wndPtr );
284 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
289 /***********************************************************************
292 * set the window size hints
294 static void set_size_hints( Display *display, WND *win )
296 XSizeHints* size_hints;
297 struct x11drv_win_data *data = win->pDriverData;
299 if ((size_hints = XAllocSizeHints()))
301 size_hints->win_gravity = StaticGravity;
302 size_hints->x = data->whole_rect.left;
303 size_hints->y = data->whole_rect.top;
304 size_hints->flags = PWinGravity | PPosition;
306 if (HAS_DLGFRAME( win->dwStyle, win->dwExStyle ))
308 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
309 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
310 size_hints->min_width = size_hints->max_width;
311 size_hints->min_height = size_hints->max_height;
312 size_hints->flags |= PMinSize | PMaxSize;
314 XSetWMNormalHints( display, data->whole_window, size_hints );
320 /***********************************************************************
323 * Set the window manager hints for a newly-created window
325 static void set_wm_hints( Display *display, WND *win )
327 struct x11drv_win_data *data = win->pDriverData;
329 XClassHint *class_hints;
338 protocols[i++] = wmDeleteWindow;
339 if (wmTakeFocus) protocols[i++] = wmTakeFocus;
340 XSetWMProtocols( display, data->whole_window, protocols, i );
343 if ((class_hints = XAllocClassHint()))
345 class_hints->res_name = "wine";
346 class_hints->res_class = "Wine";
347 XSetClassHint( display, data->whole_window, class_hints );
348 XFree( class_hints );
351 /* transient for hint */
354 Window owner_win = X11DRV_get_whole_window( win->owner );
355 XSetTransientForHint( display, data->whole_window, owner_win );
356 group_leader = owner_win;
358 else group_leader = data->whole_window;
361 set_size_hints( display, win );
363 /* systray properties (KDE only for now) */
364 if (win->dwExStyle & WS_EX_TRAYWINDOW)
367 if (kwmDockWindow != None)
368 TSXChangeProperty( display, data->whole_window, kwmDockWindow, kwmDockWindow,
369 32, PropModeReplace, (char*)&val, 1 );
370 if (_kde_net_wm_system_tray_window_for != None)
371 TSXChangeProperty( display, data->whole_window, _kde_net_wm_system_tray_window_for,
372 XA_WINDOW, 32, PropModeReplace, (char*)&data->whole_window, 1 );
378 if ((wm_hints = TSXAllocWMHints()))
380 wm_hints->flags = InputHint | StateHint | WindowGroupHint;
381 /* use globally active model if take focus is supported,
382 * passive model otherwise (cf. ICCCM) */
383 wm_hints->input = !wmTakeFocus;
385 set_icon_hints( display, win, wm_hints );
387 wm_hints->initial_state = (win->dwStyle & WS_MINIMIZE) ? IconicState : NormalState;
388 wm_hints->window_group = group_leader;
391 XSetWMHints( display, data->whole_window, wm_hints );
398 /***********************************************************************
399 * X11DRV_set_iconic_state
401 * Set the X11 iconic state according to the window style.
403 void X11DRV_set_iconic_state( WND *win )
405 Display *display = thread_display();
406 struct x11drv_win_data *data = win->pDriverData;
408 BOOL iconic = IsIconic( win->hwndSelf );
412 if (iconic) XUnmapWindow( display, data->client_window );
413 else if (is_client_window_mapped( win )) XMapWindow( display, data->client_window );
415 if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
416 wm_hints->flags |= StateHint | IconPositionHint;
417 wm_hints->initial_state = iconic ? IconicState : NormalState;
418 wm_hints->icon_x = win->rectWindow.left;
419 wm_hints->icon_y = win->rectWindow.top;
420 XSetWMHints( display, data->whole_window, wm_hints );
422 if (win->dwStyle & WS_VISIBLE)
425 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
427 if (!IsRectEmpty( &win->rectWindow )) XMapWindow( display, data->whole_window );
435 /***********************************************************************
436 * X11DRV_window_to_X_rect
438 * Convert a rect from client to X window coordinates
440 void X11DRV_window_to_X_rect( WND *win, RECT *rect )
444 if (!(win->dwExStyle & WS_EX_MANAGED)) return;
445 if (IsRectEmpty( rect )) return;
447 rc.top = rc.bottom = rc.left = rc.right = 0;
449 AdjustWindowRectEx( &rc, win->dwStyle & ~(WS_HSCROLL|WS_VSCROLL), FALSE, win->dwExStyle );
451 rect->left -= rc.left;
452 rect->right -= rc.right;
454 rect->bottom -= rc.bottom;
455 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
456 if (rect->left >= rect->right) rect->right = rect->left + 1;
460 /***********************************************************************
461 * X11DRV_X_to_window_rect
463 * Opposite of X11DRV_window_to_X_rect
465 void X11DRV_X_to_window_rect( WND *win, RECT *rect )
467 if (!(win->dwExStyle & WS_EX_MANAGED)) return;
468 if (IsRectEmpty( rect )) return;
470 AdjustWindowRectEx( rect, win->dwStyle & ~(WS_HSCROLL|WS_VSCROLL), FALSE, win->dwExStyle );
472 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
473 if (rect->left >= rect->right) rect->right = rect->left + 1;
477 /***********************************************************************
478 * X11DRV_sync_whole_window_position
480 * Synchronize the X whole window position with the Windows one
482 int X11DRV_sync_whole_window_position( Display *display, WND *win, int zorder )
484 XWindowChanges changes;
486 struct x11drv_win_data *data = win->pDriverData;
487 RECT whole_rect = win->rectWindow;
489 X11DRV_window_to_X_rect( win, &whole_rect );
490 mask = get_window_changes( &changes, &data->whole_rect, &whole_rect );
494 /* find window that this one must be after */
495 HWND prev = GetWindow( win->hwndSelf, GW_HWNDPREV );
496 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
497 prev = GetWindow( prev, GW_HWNDPREV );
498 if (!prev) /* top child */
500 changes.stack_mode = Above;
505 changes.stack_mode = Below;
506 changes.sibling = X11DRV_get_whole_window(prev);
507 mask |= CWStackMode | CWSibling;
511 data->whole_rect = whole_rect;
515 TRACE( "setting win %lx pos %d,%d,%dx%d after %lx changes=%x\n",
516 data->whole_window, whole_rect.left, whole_rect.top,
517 whole_rect.right - whole_rect.left, whole_rect.bottom - whole_rect.top,
518 changes.sibling, mask );
520 XSync( gdi_display, False ); /* flush graphics operations before moving the window */
521 if (is_window_top_level( win ))
523 if (mask & (CWWidth|CWHeight)) set_size_hints( display, win );
524 XReconfigureWMWindow( display, data->whole_window,
525 DefaultScreen(display), mask, &changes );
527 else XConfigureWindow( display, data->whole_window, mask, &changes );
534 /***********************************************************************
535 * X11DRV_sync_client_window_position
537 * Synchronize the X client window position with the Windows one
539 int X11DRV_sync_client_window_position( Display *display, WND *win )
541 XWindowChanges changes;
543 struct x11drv_win_data *data = win->pDriverData;
544 RECT client_rect = win->rectClient;
546 OffsetRect( &client_rect, -data->whole_rect.left, -data->whole_rect.top );
548 if ((mask = get_window_changes( &changes, &data->client_rect, &client_rect )))
550 BOOL was_mapped = is_client_window_mapped( win );
552 TRACE( "setting win %lx pos %d,%d,%dx%d (was %d,%d,%dx%d) after %lx changes=%x\n",
553 data->client_window, client_rect.left, client_rect.top,
554 client_rect.right - client_rect.left, client_rect.bottom - client_rect.top,
555 data->client_rect.left, data->client_rect.top,
556 data->client_rect.right - data->client_rect.left,
557 data->client_rect.bottom - data->client_rect.top,
558 changes.sibling, mask );
559 data->client_rect = client_rect;
561 XSync( gdi_display, False ); /* flush graphics operations before moving the window */
562 if (was_mapped && !is_client_window_mapped( win ))
563 XUnmapWindow( display, data->client_window );
564 XConfigureWindow( display, data->client_window, mask, &changes );
565 if (!was_mapped && is_client_window_mapped( win ))
566 XMapWindow( display, data->client_window );
573 /***********************************************************************
574 * X11DRV_register_window
576 * Associate an X window to a HWND.
578 void X11DRV_register_window( Display *display, HWND hwnd, struct x11drv_win_data *data )
581 XSaveContext( display, data->whole_window, winContext, (char *)hwnd );
582 XSaveContext( display, data->client_window, winContext, (char *)hwnd );
587 /**********************************************************************
590 static void create_desktop( Display *display, WND *wndPtr, CREATESTRUCTA *cs )
592 X11DRV_WND_DATA *data = wndPtr->pDriverData;
595 winContext = XUniqueContext();
596 wmProtocols = XInternAtom( display, "WM_PROTOCOLS", False );
597 wmDeleteWindow = XInternAtom( display, "WM_DELETE_WINDOW", False );
598 /* wmTakeFocus = XInternAtom( display, "WM_TAKE_FOCUS", False );*/
599 wmTakeFocus = 0; /* not yet */
600 dndProtocol = XInternAtom( display, "DndProtocol" , False );
601 dndSelection = XInternAtom( display, "DndSelection" , False );
602 wmChangeState = XInternAtom (display, "WM_CHANGE_STATE", False);
603 kwmDockWindow = XInternAtom( display, "KWM_DOCKWINDOW", False );
604 _kde_net_wm_system_tray_window_for = XInternAtom( display, "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR", False );
607 whole_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_whole_window" ));
608 client_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_client_window" ));
609 icon_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_icon_window" ));
611 data->whole_window = data->client_window = root_window;
612 data->whole_rect = data->client_rect = wndPtr->rectWindow;
614 SetPropA( wndPtr->hwndSelf, whole_window_atom, (HANDLE)root_window );
615 SetPropA( wndPtr->hwndSelf, client_window_atom, (HANDLE)root_window );
616 SetPropA( wndPtr->hwndSelf, "__wine_x11_visual_id", (HANDLE)XVisualIDFromVisual(visual) );
618 SendMessageW( wndPtr->hwndSelf, WM_NCCREATE, 0, (LPARAM)cs );
619 if (root_window != DefaultRootWindow(display)) X11DRV_create_desktop_thread();
623 /**********************************************************************
624 * create_whole_window
626 * Create the whole X window for a given window
628 static Window create_whole_window( Display *display, WND *win )
630 struct x11drv_win_data *data = win->pDriverData;
632 XSetWindowAttributes attr;
635 BOOL is_top_level = is_window_top_level( win );
637 rect = win->rectWindow;
638 X11DRV_window_to_X_rect( win, &rect );
640 if (!(cx = rect.right - rect.left)) cx = 1;
641 if (!(cy = rect.bottom - rect.top)) cy = 1;
643 parent = X11DRV_get_client_window( win->parent );
647 mask = get_window_attributes( display, win, &attr );
649 /* set the attributes that don't change over the lifetime of the window */
650 attr.bit_gravity = ForgetGravity;
651 attr.win_gravity = NorthWestGravity;
652 attr.backing_store = NotUseful/*WhenMapped*/;
653 mask |= CWBitGravity | CWWinGravity | CWBackingStore;
655 data->whole_rect = rect;
656 data->whole_window = XCreateWindow( display, parent, rect.left, rect.top, cx, cy,
657 0, screen_depth, InputOutput, visual,
659 if (attr.cursor) XFreeCursor( display, attr.cursor );
661 if (!data->whole_window)
667 /* non-maximized child must be at bottom of Z order */
668 if ((win->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
670 XWindowChanges changes;
671 changes.stack_mode = Below;
672 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
677 if (is_top_level) set_wm_hints( display, win );
679 return data->whole_window;
683 /**********************************************************************
684 * create_client_window
686 * Create the client window for a given window
688 static Window create_client_window( Display *display, WND *win )
690 struct x11drv_win_data *data = win->pDriverData;
691 RECT rect = data->whole_rect;
692 XSetWindowAttributes attr;
694 OffsetRect( &rect, -data->whole_rect.left, -data->whole_rect.top );
695 data->client_rect = rect;
697 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
698 ButtonPressMask | ButtonReleaseMask);
699 attr.bit_gravity = (win->clsStyle & (CS_VREDRAW | CS_HREDRAW)) ?
700 ForgetGravity : NorthWestGravity;
701 attr.backing_store = NotUseful/*WhenMapped*/;
704 data->client_window = XCreateWindow( display, data->whole_window, 0, 0,
705 max( rect.right - rect.left, 1 ),
706 max( rect.bottom - rect.top, 1 ),
709 CWEventMask | CWBitGravity | CWBackingStore, &attr );
710 if (data->client_window && is_client_window_mapped( win ))
711 XMapWindow( display, data->client_window );
713 return data->client_window;
717 /*****************************************************************
718 * SetWindowText (X11DRV.@)
720 BOOL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
722 Display *display = thread_display();
726 static UINT text_cp = (UINT)-1;
729 if ((win = X11DRV_get_whole_window( hwnd )))
731 if (text_cp == (UINT)-1)
736 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\x11drv", &hkey))
739 DWORD type, count = sizeof(buffer);
740 if(!RegQueryValueExA(hkey, "TextCP", 0, &type, buffer, &count))
741 text_cp = atoi(buffer);
744 TRACE("text_cp = %u\n", text_cp);
747 /* allocate new buffer for window text */
748 count = WideCharToMultiByte(text_cp, 0, text, -1, NULL, 0, NULL, NULL);
749 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count )))
751 ERR("Not enough memory for window text\n");
754 WideCharToMultiByte(text_cp, 0, text, -1, buffer, count, NULL, NULL);
756 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
757 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
759 ERR("Not enough memory for window text in UTF-8\n");
762 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
765 XStoreName( display, win, buffer );
766 XSetIconName( display, win, buffer );
768 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
769 according to the standard
770 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
772 XChangeProperty( display, win,
773 XInternAtom(display, "_NET_WM_NAME", False),
774 XInternAtom(display, "UTF8_STRING", False),
775 8, PropModeReplace, (unsigned char *) utf8_buffer,
779 HeapFree( GetProcessHeap(), 0, utf8_buffer );
780 HeapFree( GetProcessHeap(), 0, buffer );
786 /***********************************************************************
787 * DestroyWindow (X11DRV.@)
789 BOOL X11DRV_DestroyWindow( HWND hwnd )
791 Display *display = thread_display();
792 WND *wndPtr = WIN_GetPtr( hwnd );
793 X11DRV_WND_DATA *data = wndPtr->pDriverData;
795 if (!data) goto done;
797 if (data->whole_window)
799 TRACE( "win %x xwin %lx/%lx\n", hwnd, data->whole_window, data->client_window );
801 XSync( gdi_display, False ); /* flush any reference to this drawable in GDI queue */
802 XDeleteContext( display, data->whole_window, winContext );
803 XDeleteContext( display, data->client_window, winContext );
804 XDestroyWindow( display, data->whole_window ); /* this destroys client too */
805 destroy_icon_window( display, wndPtr );
809 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
810 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
811 HeapFree( GetProcessHeap(), 0, data );
812 wndPtr->pDriverData = NULL;
814 WIN_ReleasePtr( wndPtr );
819 /**********************************************************************
820 * CreateWindow (X11DRV.@)
822 BOOL X11DRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
824 Display *display = thread_display();
826 struct x11drv_win_data *data;
830 if (!(data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data)))) return FALSE;
831 data->whole_window = 0;
832 data->client_window = 0;
833 data->icon_window = 0;
834 data->hWMIconBitmap = 0;
835 data->hWMIconMask = 0;
837 wndPtr = WIN_GetPtr( hwnd );
838 wndPtr->pDriverData = data;
840 /* initialize the dimensions before sending WM_GETMINMAXINFO */
841 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
842 WIN_SetRectangles( hwnd, &rect, &rect );
846 create_desktop( display, wndPtr, cs );
847 WIN_ReleasePtr( wndPtr );
851 if (!create_whole_window( display, wndPtr )) goto failed;
852 if (!create_client_window( display, wndPtr )) goto failed;
853 TSXSync( display, False );
855 SetPropA( hwnd, whole_window_atom, (HANDLE)data->whole_window );
856 SetPropA( hwnd, client_window_atom, (HANDLE)data->client_window );
858 /* Send the WM_GETMINMAXINFO message and fix the size if needed */
859 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
861 POINT maxSize, maxPos, minTrack, maxTrack;
863 WIN_ReleasePtr( wndPtr );
864 WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
865 if (maxSize.x < cs->cx) cs->cx = maxSize.x;
866 if (maxSize.y < cs->cy) cs->cy = maxSize.y;
867 if (cs->cx < minTrack.x ) cs->cx = minTrack.x;
868 if (cs->cy < minTrack.y ) cs->cy = minTrack.y;
869 if (cs->cx < 0) cs->cx = 0;
870 if (cs->cy < 0) cs->cy = 0;
872 if (!(wndPtr = WIN_GetPtr( hwnd ))) return FALSE;
873 SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
874 WIN_SetRectangles( hwnd, &rect, &rect );
875 X11DRV_sync_whole_window_position( display, wndPtr, 0 );
877 WIN_ReleasePtr( wndPtr );
879 /* send WM_NCCREATE */
880 TRACE( "hwnd %x cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cs->cx, cs->cy );
882 ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
884 ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
887 WARN("aborted by WM_xxCREATE!\n");
891 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
893 sync_window_style( display, wndPtr );
895 /* send WM_NCCALCSIZE */
896 rect = wndPtr->rectWindow;
897 WIN_ReleasePtr( wndPtr );
898 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&rect );
900 if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
901 if (rect.left > rect.right || rect.top > rect.bottom) rect = wndPtr->rectWindow;
902 WIN_SetRectangles( hwnd, &wndPtr->rectWindow, &rect );
903 X11DRV_sync_client_window_position( display, wndPtr );
904 X11DRV_register_window( display, hwnd, data );
906 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",
907 hwnd, wndPtr->rectWindow.left, wndPtr->rectWindow.top,
908 wndPtr->rectWindow.right, wndPtr->rectWindow.bottom,
909 wndPtr->rectClient.left, wndPtr->rectClient.top,
910 wndPtr->rectClient.right, wndPtr->rectClient.bottom,
911 data->whole_rect.left, data->whole_rect.top,
912 data->whole_rect.right, data->whole_rect.bottom,
913 data->client_rect.left, data->client_rect.top,
914 data->client_rect.right, data->client_rect.bottom,
915 (unsigned int)data->whole_window, (unsigned int)data->client_window );
917 if ((wndPtr->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
918 WIN_LinkWindow( hwnd, wndPtr->parent, HWND_BOTTOM );
920 WIN_LinkWindow( hwnd, wndPtr->parent, HWND_TOP );
922 WIN_ReleasePtr( wndPtr );
925 ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
927 ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
931 WIN_UnlinkWindow( hwnd );
935 /* Send the size messages */
937 if (!(wndPtr = WIN_FindWndPtr(hwnd))) return FALSE;
938 if (!(wndPtr->flags & WIN_NEED_SIZE))
941 if (((wndPtr->rectClient.right-wndPtr->rectClient.left) <0)
942 ||((wndPtr->rectClient.bottom-wndPtr->rectClient.top)<0))
943 WARN("sending bogus WM_SIZE message 0x%08lx\n",
944 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
945 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
946 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
947 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
948 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
949 SendMessageW( hwnd, WM_MOVE, 0,
950 MAKELONG( wndPtr->rectClient.left, wndPtr->rectClient.top ) );
953 /* Show the window, maximizing or minimizing if needed */
955 if (wndPtr->dwStyle & (WS_MINIMIZE | WS_MAXIMIZE))
957 extern UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect ); /*FIXME*/
960 UINT swFlag = (wndPtr->dwStyle & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
961 WIN_SetStyle( hwnd, wndPtr->dwStyle & ~(WS_MAXIMIZE | WS_MINIMIZE) );
962 WINPOS_MinMaximize( hwnd, swFlag, &newPos );
963 swFlag = ((wndPtr->dwStyle & WS_CHILD) || GetActiveWindow())
964 ? SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED
965 : SWP_NOZORDER | SWP_FRAMECHANGED;
966 SetWindowPos( hwnd, 0, newPos.left, newPos.top,
967 newPos.right, newPos.bottom, swFlag );
970 WIN_ReleaseWndPtr( wndPtr );
975 X11DRV_DestroyWindow( hwnd );
976 if (wndPtr) WIN_ReleasePtr( wndPtr );
981 /***********************************************************************
982 * X11DRV_get_client_window
984 * Return the X window associated with the client area of a window
986 Window X11DRV_get_client_window( HWND hwnd )
989 WND *win = WIN_GetPtr( hwnd );
991 if (win == WND_OTHER_PROCESS)
992 return GetPropA( hwnd, client_window_atom );
996 struct x11drv_win_data *data = win->pDriverData;
997 ret = data->client_window;
998 WIN_ReleasePtr( win );
1004 /***********************************************************************
1005 * X11DRV_get_whole_window
1007 * Return the X window associated with the full area of a window
1009 Window X11DRV_get_whole_window( HWND hwnd )
1012 WND *win = WIN_GetPtr( hwnd );
1014 if (win == WND_OTHER_PROCESS)
1015 return GetPropA( hwnd, whole_window_atom );
1019 struct x11drv_win_data *data = win->pDriverData;
1020 ret = data->whole_window;
1021 WIN_ReleasePtr( win );
1027 /*****************************************************************
1028 * SetParent (X11DRV.@)
1030 HWND X11DRV_SetParent( HWND hwnd, HWND parent )
1032 Display *display = thread_display();
1036 /* Windows hides the window first, then shows it again
1037 * including the WM_SHOWWINDOW messages and all */
1038 BOOL was_visible = ShowWindow( hwnd, SW_HIDE );
1040 if (!IsWindow( parent )) return 0;
1041 if (!(wndPtr = WIN_GetPtr(hwnd)) || wndPtr == WND_OTHER_PROCESS) return 0;
1043 retvalue = wndPtr->parent; /* old parent */
1044 if (parent != retvalue)
1046 struct x11drv_win_data *data = wndPtr->pDriverData;
1048 WIN_LinkWindow( hwnd, parent, HWND_TOP );
1050 if (parent != GetDesktopWindow()) /* a child window */
1052 if (!(wndPtr->dwStyle & WS_CHILD))
1054 HMENU menu = (HMENU)SetWindowLongW( hwnd, GWL_ID, 0 );
1055 if (menu) DestroyMenu( menu );
1059 if (is_window_top_level( wndPtr )) set_wm_hints( display, wndPtr );
1061 sync_window_style( display, wndPtr );
1062 XReparentWindow( display, data->whole_window, X11DRV_get_client_window(parent),
1063 data->whole_rect.left, data->whole_rect.top );
1064 wine_tsx11_unlock();
1066 WIN_ReleasePtr( wndPtr );
1068 /* SetParent additionally needs to make hwnd the topmost window
1069 in the x-order and send the expected WM_WINDOWPOSCHANGING and
1070 WM_WINDOWPOSCHANGED notification messages.
1072 SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0,
1073 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | (was_visible ? SWP_SHOWWINDOW : 0) );
1074 /* FIXME: a WM_MOVE is also generated (in the DefWindowProc handler
1075 * for WM_WINDOWPOSCHANGED) in Windows, should probably remove SWP_NOMOVE */
1081 /*****************************************************************
1082 * SetFocus (X11DRV.@)
1085 * Explicit colormap management seems to work only with OLVWM.
1087 void X11DRV_SetFocus( HWND hwnd )
1089 Display *display = thread_display();
1090 XWindowAttributes win_attr;
1093 /* Only mess with the X focus if there's */
1094 /* no desktop window and if the window is not managed by the WM. */
1095 if (root_window != DefaultRootWindow(display)) return;
1097 if (!hwnd) /* If setting the focus to 0, uninstall the colormap */
1099 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1100 TSXUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1104 hwnd = GetAncestor( hwnd, GA_ROOT );
1105 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_MANAGED) return;
1106 if (!(win = X11DRV_get_whole_window( hwnd ))) return;
1108 /* Set X focus and install colormap */
1110 if (XGetWindowAttributes( display, win, &win_attr ) &&
1111 (win_attr.map_state == IsViewable))
1113 /* If window is not viewable, don't change anything */
1115 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1116 /* FIXME: this is not entirely correct */
1117 XSetInputFocus( display, win, RevertToParent,
1118 /*CurrentTime*/ GetMessageTime() + X11DRV_server_startticks );
1119 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1120 XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1122 wine_tsx11_unlock();
1126 /**********************************************************************
1127 * SetWindowIcon (X11DRV.@)
1129 * hIcon or hIconSm has changed (or is being initialised for the
1130 * first time). Complete the X11 driver-specific initialisation
1131 * and set the window hints.
1133 * This is not entirely correct, may need to create
1134 * an icon window and set the pixmap as a background
1136 HICON X11DRV_SetWindowIcon( HWND hwnd, HICON icon, BOOL small )
1139 Display *display = thread_display();
1140 HICON old = SetClassLongW( hwnd, small ? GCL_HICONSM : GCL_HICON, icon );
1142 SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOSIZE |
1143 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
1145 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return old;
1147 if (wndPtr->dwExStyle & WS_EX_MANAGED)
1149 Window win = get_whole_window(wndPtr);
1150 XWMHints* wm_hints = TSXGetWMHints( display, win );
1152 if (!wm_hints) wm_hints = TSXAllocWMHints();
1155 set_icon_hints( display, wndPtr, wm_hints );
1156 TSXSetWMHints( display, win, wm_hints );
1157 TSXFree( wm_hints );
1160 WIN_ReleasePtr( wndPtr );