2 * Window related functions
4 * Copyright 1993, 1994, 1995, 1996, 2001 Alexandre Julliard
5 * Copyright 1993 David Metcalfe
6 * Copyright 1995, 1996 Alex Korobka
18 #include "debugtools.h"
24 DEFAULT_DEBUG_CHANNEL(x11drv);
26 extern Pixmap X11DRV_BITMAP_Pixmap( HBITMAP );
28 #define HAS_DLGFRAME(style,exStyle) \
29 (((exStyle) & WS_EX_DLGMODALFRAME) || \
30 (((style) & WS_DLGFRAME) && !((style) & WS_THICKFRAME)))
32 #define HAS_THICKFRAME(style,exStyle) \
33 (((style) & WS_THICKFRAME) && \
34 !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
36 #define HAS_THINFRAME(style) \
37 (((style) & WS_BORDER) || !((style) & (WS_CHILD | WS_POPUP)))
39 /* X context to associate a hwnd to an X window */
40 XContext winContext = 0;
42 Atom wmProtocols = None;
43 Atom wmDeleteWindow = None;
44 Atom wmTakeFocus = None;
45 Atom dndProtocol = None;
46 Atom dndSelection = None;
47 Atom wmChangeState = None;
48 Atom kwmDockWindow = None;
49 Atom _kde_net_wm_system_tray_window_for = None; /* KDE 2 Final */
52 /***********************************************************************
55 * Check if a given window should be managed
57 inline static BOOL is_window_managed( WND *win )
59 if (!Options.managed) return FALSE;
61 /* tray window is always managed */
62 if (win->dwExStyle & WS_EX_TRAYWINDOW) return TRUE;
63 /* child windows are not managed */
64 if (win->dwStyle & WS_CHILD) return FALSE;
65 /* tool windows are not managed */
66 if (win->dwExStyle & WS_EX_TOOLWINDOW) return FALSE;
67 /* windows with caption or thick frame are managed */
68 if ((win->dwStyle & WS_CAPTION) == WS_CAPTION) return TRUE;
69 if (win->dwStyle & WS_THICKFRAME) return TRUE;
70 /* default: not managed */
75 /***********************************************************************
78 * Check if a given window is a top level X11 window
80 inline static BOOL is_window_top_level( WND *win )
82 return (root_window == DefaultRootWindow(gdi_display) &&
83 win->parent->hwndSelf == 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 !IsIconic( win->hwndSelf ) && !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->dwExStyle |= WS_EX_MANAGED;
111 else 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;
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*/;
192 data->icon_window = TSXCreateWindow( display, root_window, 0, 0,
193 GetSystemMetrics( SM_CXICON ),
194 GetSystemMetrics( SM_CYICON ),
197 CWEventMask | CWBitGravity | CWBackingStore, &attr );
198 XSaveContext( display, data->icon_window, winContext, (char *)win->hwndSelf );
199 TRACE( "created %lx\n", data->icon_window );
200 return data->icon_window;
205 /***********************************************************************
206 * destroy_icon_window
208 inline static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
210 if (!data->icon_window) return;
211 XDeleteContext( display, data->icon_window, winContext );
212 XDestroyWindow( display, data->icon_window );
213 data->icon_window = 0;
217 /***********************************************************************
220 * Set the icon wm hints
222 static void set_icon_hints( Display *display, WND *wndPtr, XWMHints *hints )
224 X11DRV_WND_DATA *data = wndPtr->pDriverData;
225 HICON hIcon = GetClassLongA( wndPtr->hwndSelf, GCL_HICON );
227 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
228 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
229 data->hWMIconBitmap = 0;
230 data->hWMIconMask = 0;
232 if (!(wndPtr->dwExStyle & WS_EX_MANAGED))
234 destroy_icon_window( display, data );
235 hints->flags &= ~(IconPixmapHint | IconMaskHint | IconWindowHint);
239 if (!data->icon_window) create_icon_window( display, wndPtr );
240 hints->icon_window = data->icon_window;
241 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
251 GetIconInfo(hIcon, &ii);
253 X11DRV_CreateBitmap(ii.hbmMask);
254 X11DRV_CreateBitmap(ii.hbmColor);
256 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
259 rcMask.right = bmMask.bmWidth;
260 rcMask.bottom = bmMask.bmHeight;
262 hDC = CreateCompatibleDC(0);
263 hbmOrig = SelectObject(hDC, ii.hbmMask);
264 InvertRect(hDC, &rcMask);
265 SelectObject(hDC, hbmOrig);
268 data->hWMIconBitmap = ii.hbmColor;
269 data->hWMIconMask = ii.hbmMask;
271 hints->icon_pixmap = X11DRV_BITMAP_Pixmap(data->hWMIconBitmap);
272 hints->icon_mask = X11DRV_BITMAP_Pixmap(data->hWMIconMask);
273 destroy_icon_window( display, data );
274 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
279 /***********************************************************************
282 * set the window size hints
284 static void set_size_hints( Display *display, WND *win )
286 XSizeHints* size_hints;
287 struct x11drv_win_data *data = win->pDriverData;
289 if ((size_hints = XAllocSizeHints()))
291 size_hints->win_gravity = StaticGravity;
292 size_hints->x = data->whole_rect.left;
293 size_hints->y = data->whole_rect.top;
294 size_hints->flags = PWinGravity | PPosition;
296 if (HAS_DLGFRAME( win->dwStyle, win->dwExStyle ))
298 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
299 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
300 size_hints->min_width = size_hints->max_width;
301 size_hints->min_height = size_hints->max_height;
302 size_hints->flags |= PMinSize | PMaxSize;
304 XSetWMNormalHints( display, data->whole_window, size_hints );
310 /***********************************************************************
313 * Set the window manager hints for a newly-created window
315 static void set_wm_hints( Display *display, WND *win )
317 struct x11drv_win_data *data = win->pDriverData;
319 XClassHint *class_hints;
328 protocols[i++] = wmDeleteWindow;
329 if (wmTakeFocus) protocols[i++] = wmTakeFocus;
330 XSetWMProtocols( display, data->whole_window, protocols, i );
333 if ((class_hints = XAllocClassHint()))
335 class_hints->res_name = "wine";
336 class_hints->res_class = "Wine";
337 XSetClassHint( display, data->whole_window, class_hints );
338 XFree( class_hints );
341 /* transient for hint */
344 struct x11drv_win_data *owner_data = win->owner->pDriverData;
345 XSetTransientForHint( display, data->whole_window, owner_data->whole_window );
346 group_leader = owner_data->whole_window;
348 else group_leader = data->whole_window;
351 if ((wm_hints = XAllocWMHints()))
353 wm_hints->flags = InputHint | StateHint | WindowGroupHint;
354 /* use globally active model if take focus is supported,
355 * passive model otherwise (cf. ICCCM) */
356 wm_hints->input = !wmTakeFocus;
358 set_icon_hints( display, win, wm_hints );
360 wm_hints->initial_state = (win->dwStyle & WS_MINIMIZE) ? IconicState : NormalState;
361 wm_hints->window_group = group_leader;
363 XSetWMHints( display, data->whole_window, wm_hints );
368 set_size_hints( display, win );
370 /* systray properties (KDE only for now) */
371 if (win->dwExStyle & WS_EX_TRAYWINDOW)
374 if (kwmDockWindow != None)
375 TSXChangeProperty( display, data->whole_window, kwmDockWindow, kwmDockWindow,
376 32, PropModeReplace, (char*)&val, 1 );
377 if (_kde_net_wm_system_tray_window_for != None)
378 TSXChangeProperty( display, data->whole_window, _kde_net_wm_system_tray_window_for,
379 XA_WINDOW, 32, PropModeReplace, (char*)&data->whole_window, 1 );
386 /***********************************************************************
387 * X11DRV_set_iconic_state
389 * Set the X11 iconic state according to the window style.
391 void X11DRV_set_iconic_state( WND *win )
393 Display *display = thread_display();
394 struct x11drv_win_data *data = win->pDriverData;
396 BOOL iconic = IsIconic( win->hwndSelf );
398 if (!(win->dwExStyle & WS_EX_MANAGED))
400 if (iconic) TSXUnmapWindow( display, data->client_window );
401 else if (is_client_window_mapped( win )) TSXMapWindow( display, data->client_window );
406 if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
407 wm_hints->flags |= StateHint | IconPositionHint;
408 wm_hints->initial_state = iconic ? IconicState : NormalState;
409 wm_hints->icon_x = win->rectWindow.left;
410 wm_hints->icon_y = win->rectWindow.top;
411 XSetWMHints( display, data->whole_window, wm_hints );
413 if (win->dwStyle & WS_VISIBLE)
416 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
418 if (!IsRectEmpty( &win->rectWindow )) XMapWindow( display, data->whole_window );
426 /***********************************************************************
427 * X11DRV_window_to_X_rect
429 * Convert a rect from client to X window coordinates
431 void X11DRV_window_to_X_rect( WND *win, RECT *rect )
433 if (!(win->dwExStyle & WS_EX_MANAGED)) return;
434 if (win->dwStyle & WS_ICONIC) return;
435 if (IsRectEmpty( rect )) return;
437 if (HAS_THICKFRAME( win->dwStyle, win->dwExStyle ))
438 InflateRect( rect, -GetSystemMetrics(SM_CXFRAME), -GetSystemMetrics(SM_CYFRAME) );
439 else if (HAS_DLGFRAME( win->dwStyle, win->dwExStyle ))
440 InflateRect( rect, -GetSystemMetrics(SM_CXDLGFRAME), -GetSystemMetrics(SM_CYDLGFRAME) );
441 else if (HAS_THINFRAME( win->dwStyle ))
442 InflateRect( rect, -GetSystemMetrics(SM_CXBORDER), -GetSystemMetrics(SM_CYBORDER) );
444 if ((win->dwStyle & WS_CAPTION) == WS_CAPTION)
446 if (win->dwExStyle & WS_EX_TOOLWINDOW)
447 rect->top += GetSystemMetrics(SM_CYSMCAPTION);
449 rect->top += GetSystemMetrics(SM_CYCAPTION);
452 if (win->dwExStyle & WS_EX_CLIENTEDGE)
453 InflateRect( rect, -GetSystemMetrics(SM_CXEDGE), -GetSystemMetrics(SM_CYEDGE) );
454 if (win->dwExStyle & WS_EX_STATICEDGE)
455 InflateRect( rect, -GetSystemMetrics(SM_CXBORDER), -GetSystemMetrics(SM_CYBORDER) );
457 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
458 if (rect->left >= rect->right) rect->right = rect->left + 1;
462 /***********************************************************************
463 * X11DRV_X_to_window_rect
465 * Opposite of X11DRV_window_to_X_rect
467 void X11DRV_X_to_window_rect( WND *win, RECT *rect )
469 if (!(win->dwExStyle & WS_EX_MANAGED)) return;
470 if (win->dwStyle & WS_ICONIC) return;
471 if (IsRectEmpty( rect )) return;
473 if (HAS_THICKFRAME( win->dwStyle, win->dwExStyle ))
474 InflateRect( rect, GetSystemMetrics(SM_CXFRAME), GetSystemMetrics(SM_CYFRAME) );
475 else if (HAS_DLGFRAME( win->dwStyle, win->dwExStyle ))
476 InflateRect( rect, GetSystemMetrics(SM_CXDLGFRAME), GetSystemMetrics(SM_CYDLGFRAME) );
477 else if (HAS_THINFRAME( win->dwStyle ))
478 InflateRect( rect, GetSystemMetrics(SM_CXBORDER), GetSystemMetrics(SM_CYBORDER) );
480 if ((win->dwStyle & WS_CAPTION) == WS_CAPTION)
482 if (win->dwExStyle & WS_EX_TOOLWINDOW)
483 rect->top -= GetSystemMetrics(SM_CYSMCAPTION);
485 rect->top -= GetSystemMetrics(SM_CYCAPTION);
488 if (win->dwExStyle & WS_EX_CLIENTEDGE)
489 InflateRect( rect, GetSystemMetrics(SM_CXEDGE), GetSystemMetrics(SM_CYEDGE) );
490 if (win->dwExStyle & WS_EX_STATICEDGE)
491 InflateRect( rect, GetSystemMetrics(SM_CXBORDER), GetSystemMetrics(SM_CYBORDER) );
493 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
494 if (rect->left >= rect->right) rect->right = rect->left + 1;
498 /***********************************************************************
499 * X11DRV_sync_whole_window_position
501 * Synchronize the X whole window position with the Windows one
503 int X11DRV_sync_whole_window_position( Display *display, WND *win, int zorder )
505 XWindowChanges changes;
507 struct x11drv_win_data *data = win->pDriverData;
508 RECT whole_rect = win->rectWindow;
510 X11DRV_window_to_X_rect( win, &whole_rect );
511 mask = get_window_changes( &changes, &data->whole_rect, &whole_rect );
515 /* find window that this one must be after */
516 WND *prev = win->parent->child;
517 if (prev == win) /* top child */
519 changes.stack_mode = Above;
524 while (prev && prev->next != win) prev = prev->next;
527 changes.stack_mode = Below;
528 changes.sibling = get_whole_window(prev);
529 mask |= CWStackMode | CWSibling;
531 else ERR( "previous window not found for %x, list corrupted?\n", win->hwndSelf );
535 data->whole_rect = whole_rect;
539 TRACE( "setting win %lx pos %d,%d,%dx%d after %lx changes=%x\n",
540 data->whole_window, whole_rect.left, whole_rect.top,
541 whole_rect.right - whole_rect.left, whole_rect.bottom - whole_rect.top,
542 changes.sibling, mask );
544 XSync( gdi_display, False ); /* flush graphics operations before moving the window */
545 if (is_window_top_level( win ))
547 if (mask & (CWWidth|CWHeight)) set_size_hints( display, win );
548 XReconfigureWMWindow( display, data->whole_window,
549 DefaultScreen(display), mask, &changes );
551 else XConfigureWindow( display, data->whole_window, mask, &changes );
558 /***********************************************************************
559 * X11DRV_sync_client_window_position
561 * Synchronize the X client window position with the Windows one
563 int X11DRV_sync_client_window_position( Display *display, WND *win )
565 XWindowChanges changes;
567 struct x11drv_win_data *data = win->pDriverData;
568 RECT client_rect = win->rectClient;
570 OffsetRect( &client_rect, -data->whole_rect.left, -data->whole_rect.top );
572 if ((mask = get_window_changes( &changes, &data->client_rect, &client_rect )))
574 BOOL was_mapped = is_client_window_mapped( win );
576 TRACE( "setting win %lx pos %d,%d,%dx%d (was %d,%d,%dx%d) after %lx changes=%x\n",
577 data->client_window, client_rect.left, client_rect.top,
578 client_rect.right - client_rect.left, client_rect.bottom - client_rect.top,
579 data->client_rect.left, data->client_rect.top,
580 data->client_rect.right - data->client_rect.left,
581 data->client_rect.bottom - data->client_rect.top,
582 changes.sibling, mask );
583 data->client_rect = client_rect;
585 XSync( gdi_display, False ); /* flush graphics operations before moving the window */
586 if (was_mapped && !is_client_window_mapped( win ))
587 XUnmapWindow( display, data->client_window );
588 XConfigureWindow( display, data->client_window, mask, &changes );
589 if (!was_mapped && is_client_window_mapped( win ))
590 XMapWindow( display, data->client_window );
597 /***********************************************************************
598 * X11DRV_register_window
600 * Associate an X window to a HWND.
602 void X11DRV_register_window( Display *display, HWND hwnd, struct x11drv_win_data *data )
605 XSaveContext( display, data->whole_window, winContext, (char *)hwnd );
606 XSaveContext( display, data->client_window, winContext, (char *)hwnd );
611 /**********************************************************************
614 static void create_desktop( Display *display, WND *wndPtr )
616 X11DRV_WND_DATA *data = wndPtr->pDriverData;
619 winContext = XUniqueContext();
620 wmProtocols = XInternAtom( display, "WM_PROTOCOLS", False );
621 wmDeleteWindow = XInternAtom( display, "WM_DELETE_WINDOW", False );
622 /* wmTakeFocus = XInternAtom( display, "WM_TAKE_FOCUS", False );*/
623 wmTakeFocus = 0; /* not yet */
624 dndProtocol = XInternAtom( display, "DndProtocol" , False );
625 dndSelection = XInternAtom( display, "DndSelection" , False );
626 wmChangeState = XInternAtom (display, "WM_CHANGE_STATE", False);
627 kwmDockWindow = XInternAtom( display, "KWM_DOCKWINDOW", False );
628 _kde_net_wm_system_tray_window_for = XInternAtom( display, "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR", False );
631 data->whole_window = data->client_window = root_window;
632 if (root_window != DefaultRootWindow(display)) X11DRV_create_desktop_thread();
636 /**********************************************************************
637 * create_whole_window
639 * Create the whole X window for a given window
641 static Window create_whole_window( Display *display, WND *win )
643 struct x11drv_win_data *data = win->pDriverData;
645 XSetWindowAttributes attr;
648 BOOL is_top_level = is_window_top_level( win );
650 rect = win->rectWindow;
651 X11DRV_window_to_X_rect( win, &rect );
653 if (!(cx = rect.right - rect.left)) cx = 1;
654 if (!(cy = rect.bottom - rect.top)) cy = 1;
656 parent = get_client_window( win->parent );
660 mask = get_window_attributes( display, win, &attr );
662 /* set the attributes that don't change over the lifetime of the window */
663 attr.bit_gravity = ForgetGravity;
664 attr.win_gravity = NorthWestGravity;
665 attr.backing_store = NotUseful/*WhenMapped*/;
666 mask |= CWBitGravity | CWWinGravity | CWBackingStore;
668 data->whole_rect = rect;
669 data->whole_window = XCreateWindow( display, parent, rect.left, rect.top, cx, cy,
670 0, screen_depth, InputOutput, visual,
672 if (attr.cursor) XFreeCursor( display, attr.cursor );
674 if (!data->whole_window) goto done;
676 /* non-maximized child must be at bottom of Z order */
677 if ((win->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
679 XWindowChanges changes;
680 changes.stack_mode = Below;
681 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
684 if (is_top_level) set_wm_hints( display, win );
688 return data->whole_window;
692 /**********************************************************************
693 * create_client_window
695 * Create the client window for a given window
697 static Window create_client_window( Display *display, WND *win )
699 struct x11drv_win_data *data = win->pDriverData;
700 RECT rect = data->whole_rect;
701 XSetWindowAttributes attr;
703 OffsetRect( &rect, -data->whole_rect.left, -data->whole_rect.top );
704 data->client_rect = rect;
706 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
707 ButtonPressMask | ButtonReleaseMask);
708 attr.bit_gravity = (win->clsStyle & (CS_VREDRAW | CS_HREDRAW)) ?
709 ForgetGravity : NorthWestGravity;
710 attr.backing_store = NotUseful/*WhenMapped*/;
713 data->client_window = XCreateWindow( display, data->whole_window, 0, 0,
714 max( rect.right - rect.left, 1 ),
715 max( rect.bottom - rect.top, 1 ),
718 CWEventMask | CWBitGravity | CWBackingStore, &attr );
719 if (data->client_window && is_client_window_mapped( win ))
720 XMapWindow( display, data->client_window );
722 return data->client_window;
726 /*****************************************************************
727 * SetWindowText (X11DRV.@)
729 BOOL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
731 Display *display = thread_display();
734 static UINT text_cp = (UINT)-1;
736 WND *wndPtr = WIN_FindWndPtr( hwnd );
738 if (!wndPtr) return FALSE;
739 if ((win = get_whole_window(wndPtr)))
741 if (text_cp == (UINT)-1)
743 text_cp = PROFILE_GetWineIniInt("x11drv", "TextCP", CP_ACP);
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 * sizeof(WCHAR) )))
751 ERR("Not enough memory for window text\n");
752 WIN_ReleaseWndPtr( wndPtr );
755 WideCharToMultiByte(text_cp, 0, text, -1, buffer, count, NULL, NULL);
758 XStoreName( display, win, buffer );
759 XSetIconName( display, win, buffer );
762 HeapFree( GetProcessHeap(), 0, buffer );
764 WIN_ReleaseWndPtr( wndPtr );
769 /***********************************************************************
770 * DestroyWindow (X11DRV.@)
772 BOOL X11DRV_DestroyWindow( HWND hwnd )
774 Display *display = thread_display();
775 WND *wndPtr = WIN_FindWndPtr( hwnd );
776 X11DRV_WND_DATA *data = wndPtr->pDriverData;
778 if (!data) goto done;
780 if (data->whole_window)
782 TRACE( "win %x xwin %lx/%lx\n", hwnd, data->whole_window, data->client_window );
784 XSync( gdi_display, False ); /* flush any reference to this drawable in GDI queue */
785 XDeleteContext( display, data->whole_window, winContext );
786 XDeleteContext( display, data->client_window, winContext );
787 XDestroyWindow( display, data->whole_window ); /* this destroys client too */
788 destroy_icon_window( display, data );
792 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
793 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
794 HeapFree( GetProcessHeap(), 0, data );
795 wndPtr->pDriverData = NULL;
797 WIN_ReleaseWndPtr( wndPtr );
802 /**********************************************************************
803 * CreateWindow (X11DRV.@)
805 BOOL X11DRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
807 Display *display = thread_display();
809 struct x11drv_win_data *data;
813 if (!(data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data)))) return FALSE;
814 data->whole_window = 0;
815 data->client_window = 0;
816 data->icon_window = 0;
817 data->hWMIconBitmap = 0;
818 data->hWMIconMask = 0;
820 wndPtr = WIN_FindWndPtr( hwnd );
821 wndPtr->pDriverData = data;
825 SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
826 create_desktop( display, wndPtr );
827 WIN_ReleaseWndPtr( wndPtr );
831 if (!create_whole_window( display, wndPtr )) goto failed;
832 if (!create_client_window( display, wndPtr )) goto failed;
833 TSXSync( display, False );
835 WIN_ReleaseWndPtr( wndPtr );
837 /* send WM_NCCREATE */
838 TRACE( "hwnd %x cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cs->cx, cs->cy );
840 ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
842 ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
845 X11DRV_DestroyWindow( hwnd );
849 if (!(wndPtr = WIN_FindWndPtr(hwnd))) return FALSE;
851 sync_window_style( display, wndPtr );
853 /* send WM_NCCALCSIZE */
854 rect = wndPtr->rectWindow;
855 SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&rect );
856 if (rect.left > rect.right || rect.top > rect.bottom) rect = wndPtr->rectWindow;
857 wndPtr->rectClient = rect;
858 X11DRV_sync_client_window_position( display, wndPtr );
859 X11DRV_register_window( display, hwnd, data );
861 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",
862 hwnd, wndPtr->rectWindow.left, wndPtr->rectWindow.top,
863 wndPtr->rectWindow.right, wndPtr->rectWindow.bottom,
864 wndPtr->rectClient.left, wndPtr->rectClient.top,
865 wndPtr->rectClient.right, wndPtr->rectClient.bottom,
866 data->whole_rect.left, data->whole_rect.top,
867 data->whole_rect.right, data->whole_rect.bottom,
868 data->client_rect.left, data->client_rect.top,
869 data->client_rect.right, data->client_rect.bottom,
870 (unsigned int)data->whole_window, (unsigned int)data->client_window );
872 if ((wndPtr->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
873 WIN_LinkWindow( hwnd, HWND_BOTTOM );
875 WIN_LinkWindow( hwnd, HWND_TOP );
877 WIN_ReleaseWndPtr( wndPtr );
880 ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
882 ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
886 WIN_UnlinkWindow( hwnd );
890 /* Send the size messages */
892 if (!(wndPtr = WIN_FindWndPtr(hwnd))) return FALSE;
893 if (!(wndPtr->flags & WIN_NEED_SIZE))
896 if (((wndPtr->rectClient.right-wndPtr->rectClient.left) <0)
897 ||((wndPtr->rectClient.bottom-wndPtr->rectClient.top)<0))
898 WARN("sending bogus WM_SIZE message 0x%08lx\n",
899 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
900 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
901 SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
902 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
903 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
904 SendMessageW( hwnd, WM_MOVE, 0,
905 MAKELONG( wndPtr->rectClient.left, wndPtr->rectClient.top ) );
908 /* Show the window, maximizing or minimizing if needed */
910 if (wndPtr->dwStyle & (WS_MINIMIZE | WS_MAXIMIZE))
912 extern UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect ); /*FIXME*/
915 UINT swFlag = (wndPtr->dwStyle & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
916 wndPtr->dwStyle &= ~(WS_MAXIMIZE | WS_MINIMIZE);
917 WINPOS_MinMaximize( hwnd, swFlag, &newPos );
918 swFlag = ((wndPtr->dwStyle & WS_CHILD) || GetActiveWindow())
919 ? SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED
920 : SWP_NOZORDER | SWP_FRAMECHANGED;
921 SetWindowPos( hwnd, 0, newPos.left, newPos.top,
922 newPos.right, newPos.bottom, swFlag );
925 WIN_ReleaseWndPtr( wndPtr );
930 X11DRV_DestroyWindow( wndPtr->hwndSelf );
931 WIN_ReleaseWndPtr( wndPtr );
936 /***********************************************************************
937 * X11DRV_get_client_window
939 * Return the X window associated with the client area of a window
941 Window X11DRV_get_client_window( HWND hwnd )
944 WND *win = WIN_FindWndPtr( hwnd );
947 struct x11drv_win_data *data = win->pDriverData;
948 ret = data->client_window;
949 WIN_ReleaseWndPtr( win );
955 /***********************************************************************
956 * X11DRV_get_whole_window
958 * Return the X window associated with the full area of a window
960 Window X11DRV_get_whole_window( HWND hwnd )
963 WND *win = WIN_FindWndPtr( hwnd );
966 struct x11drv_win_data *data = win->pDriverData;
967 ret = data->whole_window;
968 WIN_ReleaseWndPtr( win );
974 /***********************************************************************
975 * X11DRV_get_top_window
977 * Return the X window associated with the top-level parent of a window
979 Window X11DRV_get_top_window( HWND hwnd )
982 WND *win = WIN_FindWndPtr( hwnd );
983 while (win && win->parent->hwndSelf != GetDesktopWindow())
984 WIN_UpdateWndPtr( &win, win->parent );
987 struct x11drv_win_data *data = win->pDriverData;
988 ret = data->whole_window;
989 WIN_ReleaseWndPtr( win );
995 /*****************************************************************
996 * SetParent (X11DRV.@)
998 HWND X11DRV_SetParent( HWND hwnd, HWND parent )
1000 Display *display = thread_display();
1006 if (!(wndPtr = WIN_FindWndPtr(hwnd))) return 0;
1008 dwStyle = wndPtr->dwStyle;
1010 if (!parent) parent = GetDesktopWindow();
1012 if (!(pWndParent = WIN_FindWndPtr(parent)))
1014 WIN_ReleaseWndPtr( wndPtr );
1018 /* Windows hides the window first, then shows it again
1019 * including the WM_SHOWWINDOW messages and all */
1020 if (dwStyle & WS_VISIBLE) ShowWindow( hwnd, SW_HIDE );
1022 retvalue = wndPtr->parent->hwndSelf; /* old parent */
1023 if (pWndParent != wndPtr->parent)
1025 struct x11drv_win_data *data = wndPtr->pDriverData;
1027 WIN_UnlinkWindow(wndPtr->hwndSelf);
1028 wndPtr->parent = pWndParent;
1029 WIN_LinkWindow(wndPtr->hwndSelf, HWND_TOP);
1031 if (parent != GetDesktopWindow()) /* a child window */
1033 if (!(wndPtr->dwStyle & WS_CHILD) && wndPtr->wIDmenu)
1035 DestroyMenu( (HMENU)wndPtr->wIDmenu );
1036 wndPtr->wIDmenu = 0;
1041 sync_window_style( display, wndPtr );
1042 if (is_window_top_level( wndPtr )) set_wm_hints( display, wndPtr );
1043 XReparentWindow( display, data->whole_window, get_client_window(pWndParent),
1044 data->whole_rect.left, data->whole_rect.top );
1045 wine_tsx11_unlock();
1047 WIN_ReleaseWndPtr( pWndParent );
1048 WIN_ReleaseWndPtr( wndPtr );
1050 /* SetParent additionally needs to make hwnd the topmost window
1051 in the x-order and send the expected WM_WINDOWPOSCHANGING and
1052 WM_WINDOWPOSCHANGED notification messages.
1054 SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0,
1055 SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOSIZE|
1056 ((dwStyle & WS_VISIBLE)?SWP_SHOWWINDOW:0));
1057 /* FIXME: a WM_MOVE is also generated (in the DefWindowProc handler
1058 * for WM_WINDOWPOSCHANGED) in Windows, should probably remove SWP_NOMOVE */
1064 /*******************************************************************
1065 * EnableWindow (X11DRV.@)
1067 BOOL X11DRV_EnableWindow( HWND hwnd, BOOL enable )
1069 Display *display = thread_display();
1074 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
1076 retvalue = ((wndPtr->dwStyle & WS_DISABLED) != 0);
1078 if (enable && (wndPtr->dwStyle & WS_DISABLED))
1081 wndPtr->dwStyle &= ~WS_DISABLED;
1083 if (wndPtr->dwExStyle & WS_EX_MANAGED)
1086 if (!(wm_hints = XGetWMHints( display, get_whole_window(wndPtr) )))
1087 wm_hints = XAllocWMHints();
1090 wm_hints->flags |= InputHint;
1091 wm_hints->input = TRUE;
1092 XSetWMHints( display, get_whole_window(wndPtr), wm_hints );
1095 wine_tsx11_unlock();
1098 SendMessageA( hwnd, WM_ENABLE, TRUE, 0 );
1100 else if (!enable && !(wndPtr->dwStyle & WS_DISABLED))
1102 SendMessageA( wndPtr->hwndSelf, WM_CANCELMODE, 0, 0 );
1104 /* Disable window */
1105 wndPtr->dwStyle |= WS_DISABLED;
1107 if (wndPtr->dwExStyle & WS_EX_MANAGED)
1110 if (!(wm_hints = XGetWMHints( display, get_whole_window(wndPtr) )))
1111 wm_hints = XAllocWMHints();
1114 wm_hints->flags |= InputHint;
1115 wm_hints->input = FALSE;
1116 XSetWMHints( display, get_whole_window(wndPtr), wm_hints );
1119 wine_tsx11_unlock();
1122 if (hwnd == GetFocus())
1123 SetFocus( 0 ); /* A disabled window can't have the focus */
1125 if (hwnd == GetCapture())
1126 ReleaseCapture(); /* A disabled window can't capture the mouse */
1128 SendMessageA( hwnd, WM_ENABLE, FALSE, 0 );
1130 WIN_ReleaseWndPtr(wndPtr);
1135 /*****************************************************************
1136 * SetFocus (X11DRV.@)
1139 * Explicit colormap management seems to work only with OLVWM.
1141 void X11DRV_SetFocus( HWND hwnd )
1143 Display *display = thread_display();
1144 XWindowAttributes win_attr;
1146 WND *wndPtr = WIN_FindWndPtr( hwnd );
1149 if (!wndPtr) return;
1151 /* Only mess with the X focus if there's */
1152 /* no desktop window and if the window is not managed by the WM. */
1153 if (root_window != DefaultRootWindow(display)) goto done;
1155 while (w && !get_whole_window(w)) w = w->parent;
1157 if (w->dwExStyle & WS_EX_MANAGED) goto done;
1159 if (!hwnd) /* If setting the focus to 0, uninstall the colormap */
1161 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1162 TSXUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1164 else if ((win = get_whole_window(w)))
1166 /* Set X focus and install colormap */
1168 if (XGetWindowAttributes( display, win, &win_attr ) &&
1169 (win_attr.map_state == IsViewable))
1171 /* If window is not viewable, don't change anything */
1173 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1174 /* FIXME: this is not entirely correct */
1175 XSetInputFocus( display, win, RevertToParent,
1176 /*CurrentTime*/ GetMessageTime() + X11DRV_server_startticks );
1177 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1178 XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1180 wine_tsx11_unlock();
1184 WIN_ReleaseWndPtr( wndPtr );
1188 /**********************************************************************
1189 * X11DRV_SetWindowIcon
1191 * hIcon or hIconSm has changed (or is being initialised for the
1192 * first time). Complete the X11 driver-specific initialisation
1193 * and set the window hints.
1195 * This is not entirely correct, may need to create
1196 * an icon window and set the pixmap as a background
1198 HICON X11DRV_SetWindowIcon( HWND hwnd, HICON icon, BOOL small )
1200 Display *display = thread_display();
1201 WND *wndPtr = WIN_FindWndPtr( hwnd );
1202 int index = small ? GCL_HICONSM : GCL_HICON;
1205 if (!wndPtr) return 0;
1207 old = GetClassLongW( hwnd, index );
1208 SetClassLongW( hwnd, index, icon );
1210 SetWindowPos( hwnd, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOSIZE |
1211 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
1213 if (wndPtr->dwExStyle & WS_EX_MANAGED)
1215 Window win = get_whole_window(wndPtr);
1216 XWMHints* wm_hints = TSXGetWMHints( display, win );
1218 if (!wm_hints) wm_hints = TSXAllocWMHints();
1221 set_icon_hints( display, wndPtr, wm_hints );
1222 TSXSetWMHints( display, win, wm_hints );
1223 TSXFree( wm_hints );
1227 WIN_ReleaseWndPtr( wndPtr );