Changes in crossover-wine-src-6.1.0 except for configure
[wine/hacks.git] / dlls / winex11.drv / winpos.c
blob387c49eac2a58f02d3f4aa6df0d5b654d6d8899c
1 /*
2 * Window position related functions.
4 * Copyright 1993, 1994, 1995, 2001 Alexandre Julliard
5 * Copyright 1995, 1996, 1999 Alex Korobka
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #ifdef HAVE_LIBXSHAPE
27 #include <X11/extensions/shape.h>
28 #endif /* HAVE_LIBXSHAPE */
29 #include <stdarg.h>
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35 #include "winerror.h"
36 #include "wownt32.h"
37 #include "wine/wingdi16.h"
39 #include "x11drv.h"
40 #include "win.h"
42 #include "wine/server.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
47 #define SWP_AGG_NOPOSCHANGE \
48 (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
50 #define SWP_EX_NOCOPY 0x0001
51 #define SWP_EX_PAINTSELF 0x0002
52 #define SWP_EX_NONCLIENT 0x0004
54 #define HAS_THICKFRAME(style) \
55 (((style) & WS_THICKFRAME) && \
56 !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
58 #define ON_LEFT_BORDER(hit) \
59 (((hit) == HTLEFT) || ((hit) == HTTOPLEFT) || ((hit) == HTBOTTOMLEFT))
60 #define ON_RIGHT_BORDER(hit) \
61 (((hit) == HTRIGHT) || ((hit) == HTTOPRIGHT) || ((hit) == HTBOTTOMRIGHT))
62 #define ON_TOP_BORDER(hit) \
63 (((hit) == HTTOP) || ((hit) == HTTOPLEFT) || ((hit) == HTTOPRIGHT))
64 #define ON_BOTTOM_BORDER(hit) \
65 (((hit) == HTBOTTOM) || ((hit) == HTBOTTOMLEFT) || ((hit) == HTBOTTOMRIGHT))
67 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
68 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
69 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
70 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
71 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
72 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
73 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
74 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
75 #define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
76 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
77 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
79 #define _NET_WM_STATE_REMOVE 0
80 #define _NET_WM_STATE_ADD 1
81 #define _NET_WM_STATE_TOGGLE 2
83 /***********************************************************************
84 * X11DRV_Expose
86 void X11DRV_Expose( HWND hwnd, XEvent *xev )
88 XExposeEvent *event = &xev->xexpose;
89 RECT rect;
90 struct x11drv_win_data *data;
91 int flags = RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN;
93 TRACE( "win %p (%lx) %d,%d %dx%d\n",
94 hwnd, event->window, event->x, event->y, event->width, event->height );
96 if (!(data = X11DRV_get_win_data( hwnd ))) return;
98 rect.left = event->x;
99 rect.top = event->y;
100 rect.right = rect.left + event->width;
101 rect.bottom = rect.top + event->height;
103 if (event->window == root_window)
104 OffsetRect( &rect, virtual_screen_rect.left, virtual_screen_rect.top );
106 if (rect.left < data->client_rect.left ||
107 rect.top < data->client_rect.top ||
108 rect.right > data->client_rect.right ||
109 rect.bottom > data->client_rect.bottom) flags |= RDW_FRAME;
111 if (data->toplevel)
113 SERVER_START_REQ( update_window_zorder )
115 req->window = hwnd;
116 req->rect.left = rect.left + data->whole_rect.left;
117 req->rect.top = rect.top + data->whole_rect.top;
118 req->rect.right = rect.right + data->whole_rect.left;
119 req->rect.bottom = rect.bottom + data->whole_rect.top;
120 wine_server_call( req );
122 SERVER_END_REQ;
125 /* make position relative to client area instead of window */
126 OffsetRect( &rect, -data->client_rect.left, -data->client_rect.top );
127 RedrawWindow( hwnd, &rect, 0, flags );
130 /***********************************************************************
131 * SetWindowStyle (X11DRV.@)
133 * Update the X state of a window to reflect a style change
135 void X11DRV_SetWindowStyle( HWND hwnd, DWORD old_style )
137 Display *display = thread_display();
138 struct x11drv_win_data *data;
139 DWORD new_style, changed;
141 if (hwnd == GetDesktopWindow()) return;
142 if (!(data = X11DRV_get_win_data( hwnd ))) return;
144 new_style = GetWindowLongW( hwnd, GWL_STYLE );
145 changed = new_style ^ old_style;
147 if (changed & WS_VISIBLE)
149 if (data->whole_window && X11DRV_is_window_rect_mapped( &data->window_rect ))
151 if (new_style & WS_VISIBLE && !(new_style & WS_MINIMIZE ))
153 TRACE( "mapping win %p\n", hwnd );
154 X11DRV_sync_window_style( display, data );
155 X11DRV_set_wm_hints( display, data );
156 wine_tsx11_lock();
157 XMapWindow( display, data->whole_window );
158 wine_tsx11_unlock();
160 /* we don't unmap windows, that causes trouble with the window manager */
162 invalidate_dce( hwnd, &data->window_rect );
164 else if (changed & WS_CAPTION)
166 if (data->whole_window && (new_style & WS_VISIBLE))
167 X11DRV_set_wm_hints( display, data );
170 if (changed & WS_DISABLED)
172 if (data->whole_window && data->managed)
174 XWMHints *wm_hints;
175 wine_tsx11_lock();
176 if (!(wm_hints = XGetWMHints( display, data->whole_window )))
177 wm_hints = XAllocWMHints();
178 if (wm_hints)
180 wm_hints->flags |= InputHint;
181 wm_hints->input = !(new_style & WS_DISABLED);
182 XSetWMHints( display, data->whole_window, wm_hints );
183 XFree(wm_hints);
185 wine_tsx11_unlock();
191 /***********************************************************************
192 * update_fullscreen_state
194 * Use the NETWM protocol to set the fullscreen state.
195 * This only works for mapped windows.
197 static void update_fullscreen_state( Display *display, Window win, BOOL new_fs_state )
199 XEvent xev;
201 TRACE("setting fullscreen state for window %lx to %s\n", win, new_fs_state ? "true" : "false");
203 xev.xclient.type = ClientMessage;
204 xev.xclient.window = win;
205 xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
206 xev.xclient.serial = 0;
207 xev.xclient.display = display;
208 xev.xclient.send_event = True;
209 xev.xclient.format = 32;
210 xev.xclient.data.l[0] = new_fs_state ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
211 xev.xclient.data.l[1] = x11drv_atom(_NET_WM_STATE_FULLSCREEN);
212 xev.xclient.data.l[2] = 0;
213 wine_tsx11_lock();
214 XSendEvent(display, root_window, False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
215 wine_tsx11_unlock();
218 /***********************************************************************
219 * fullscreen_state_changed
221 * Check if the fullscreen state of a given window has changed
223 static BOOL fullscreen_state_changed( const struct x11drv_win_data *data,
224 const RECT *old_client_rect, const RECT *old_screen_rect,
225 BOOL *new_fs_state )
227 BOOL old_fs_state = FALSE;
229 *new_fs_state = FALSE;
231 if (old_client_rect->left <= 0 && old_client_rect->right >= old_screen_rect->right &&
232 old_client_rect->top <= 0 && old_client_rect->bottom >= old_screen_rect->bottom)
233 old_fs_state = TRUE;
235 if (data->client_rect.left <= 0 && data->client_rect.right >= screen_width &&
236 data->client_rect.top <= 0 && data->client_rect.bottom >= screen_height)
237 *new_fs_state = TRUE;
239 #if 0 /* useful to debug fullscreen state problems */
240 TRACE("hwnd %p, old rect %s, new rect %s, old screen %s, new screen (0,0-%d,%d)\n",
241 data->hwnd,
242 wine_dbgstr_rect(old_client_rect), wine_dbgstr_rect(&data->client_rect),
243 wine_dbgstr_rect(old_screen_rect), screen_width, screen_height);
244 TRACE("old fs state %d\n, new fs state = %d\n", old_fs_state, *new_fs_state);
245 #endif
246 return *new_fs_state != old_fs_state;
250 /***********************************************************************
251 * SetWindowPos (X11DRV.@)
253 BOOL X11DRV_SetWindowPos( HWND hwnd, HWND insert_after, const RECT *rectWindow,
254 const RECT *rectClient, UINT swp_flags, const RECT *valid_rects )
256 struct x11drv_win_data *data;
257 RECT new_whole_rect, old_client_rect, old_screen_rect;
258 WND *win;
259 DWORD old_style, new_style;
260 BOOL ret;
262 if (!(data = X11DRV_get_win_data( hwnd ))) return FALSE;
264 new_whole_rect = *rectWindow;
265 X11DRV_window_to_X_rect( data, &new_whole_rect );
267 old_client_rect = data->client_rect;
269 if (!(win = WIN_GetPtr( hwnd ))) return FALSE;
270 if (win == WND_OTHER_PROCESS)
272 if (IsWindow( hwnd )) ERR( "cannot set rectangles of other process window %p\n", hwnd );
273 return FALSE;
275 SERVER_START_REQ( set_window_pos )
277 req->handle = hwnd;
278 req->previous = insert_after;
279 req->flags = swp_flags;
280 req->window.left = rectWindow->left;
281 req->window.top = rectWindow->top;
282 req->window.right = rectWindow->right;
283 req->window.bottom = rectWindow->bottom;
284 req->client.left = rectClient->left;
285 req->client.top = rectClient->top;
286 req->client.right = rectClient->right;
287 req->client.bottom = rectClient->bottom;
288 if (memcmp( rectWindow, &new_whole_rect, sizeof(RECT) ) || !IsRectEmpty( &valid_rects[0] ))
290 wine_server_add_data( req, &new_whole_rect, sizeof(new_whole_rect) );
291 if (!IsRectEmpty( &valid_rects[0] ))
292 wine_server_add_data( req, valid_rects, 2 * sizeof(*valid_rects) );
294 ret = !wine_server_call( req );
295 new_style = reply->new_style;
297 SERVER_END_REQ;
299 if (win == WND_DESKTOP || data->whole_window == DefaultRootWindow(gdi_display))
301 data->whole_rect = data->client_rect = data->window_rect = *rectWindow;
302 if (win != WND_DESKTOP)
304 win->rectWindow = *rectWindow;
305 win->rectClient = *rectClient;
306 win->dwStyle = new_style;
307 WIN_ReleasePtr( win );
309 return ret;
312 if (ret)
314 Display *display = thread_display();
316 /* invalidate DCEs */
318 if ((((swp_flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) && (new_style & WS_VISIBLE)) ||
319 (swp_flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW)))
321 RECT rect;
322 UnionRect( &rect, rectWindow, &win->rectWindow );
323 invalidate_dce( hwnd, &rect );
326 win->rectWindow = *rectWindow;
327 win->rectClient = *rectClient;
328 old_style = win->dwStyle;
329 win->dwStyle = new_style;
330 data->window_rect = *rectWindow;
332 TRACE( "win %p window %s client %s style %08x\n",
333 hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style );
335 if (!IsRectEmpty( &valid_rects[0] ))
337 int x_offset = 0, y_offset = 0;
339 if (data->whole_window)
341 /* the X server will move the bits for us */
342 x_offset = data->whole_rect.left - new_whole_rect.left;
343 y_offset = data->whole_rect.top - new_whole_rect.top;
346 if (x_offset != valid_rects[1].left - valid_rects[0].left ||
347 y_offset != valid_rects[1].top - valid_rects[0].top)
349 /* FIXME: should copy the window bits here */
350 RECT invalid_rect = valid_rects[0];
352 /* invalid_rects are relative to the client area */
353 OffsetRect( &invalid_rect, -rectClient->left, -rectClient->top );
354 RedrawWindow( hwnd, &invalid_rect, NULL, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
359 if (data->whole_window && !data->lock_changes && !data->adopted)
361 if ((old_style & WS_VISIBLE) && !(new_style & WS_VISIBLE))
363 /* window got hidden, unmap it */
364 TRACE( "unmapping win %p\n", hwnd );
365 wine_tsx11_lock();
366 XUnmapWindow( display, data->whole_window );
367 wine_tsx11_unlock();
369 else if ((new_style & WS_VISIBLE) && !X11DRV_is_window_rect_mapped( rectWindow ))
371 /* resizing to zero size or off screen -> unmap */
372 TRACE( "unmapping zero size or off-screen win %p\n", hwnd );
373 wine_tsx11_lock();
374 XUnmapWindow( display, data->whole_window );
375 wine_tsx11_unlock();
379 X11DRV_sync_window_position( display, data, swp_flags, rectClient, &new_whole_rect );
381 if (data->whole_window && !data->lock_changes && !data->adopted)
383 BOOL new_fs_state, mapped = FALSE;
385 if ((new_style & WS_VISIBLE) && !(new_style & WS_MINIMIZE) &&
386 X11DRV_is_window_rect_mapped( rectWindow ))
388 if (!(old_style & WS_VISIBLE))
390 /* window got shown, map it */
391 TRACE( "mapping win %p\n", hwnd );
392 X11DRV_sync_window_style( display, data );
393 X11DRV_set_wm_hints( display, data );
394 wine_tsx11_lock();
395 XMapWindow( display, data->whole_window );
396 XFlush( display );
397 wine_tsx11_unlock();
398 mapped = TRUE;
400 else if ((swp_flags & (SWP_NOSIZE | SWP_NOMOVE)) != (SWP_NOSIZE | SWP_NOMOVE))
402 /* resizing from zero size to non-zero -> map */
403 TRACE( "mapping non zero size or off-screen win %p\n", hwnd );
404 wine_tsx11_lock();
405 XMapWindow( display, data->whole_window );
406 XFlush( display );
407 wine_tsx11_unlock();
408 mapped = TRUE;
410 SetRect( &old_screen_rect, 0, 0, screen_width, screen_height );
411 if (fullscreen_state_changed( data, &old_client_rect, &old_screen_rect, &new_fs_state ) || mapped)
412 update_fullscreen_state( display, data->whole_window, new_fs_state );
416 WIN_ReleasePtr( win );
417 return ret;
421 /***********************************************************************
422 * WINPOS_FindIconPos
424 * Find a suitable place for an iconic window.
426 static POINT WINPOS_FindIconPos( HWND hwnd, POINT pt )
428 RECT rect, rectParent;
429 HWND parent, child;
430 HRGN hrgn, tmp;
431 int xspacing, yspacing;
433 parent = GetAncestor( hwnd, GA_PARENT );
434 GetClientRect( parent, &rectParent );
435 if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
436 (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
437 return pt; /* The icon already has a suitable position */
439 xspacing = GetSystemMetrics(SM_CXICONSPACING);
440 yspacing = GetSystemMetrics(SM_CYICONSPACING);
442 /* Check if another icon already occupies this spot */
443 /* FIXME: this is completely inefficient */
445 hrgn = CreateRectRgn( 0, 0, 0, 0 );
446 tmp = CreateRectRgn( 0, 0, 0, 0 );
447 for (child = GetWindow( parent, GW_HWNDFIRST ); child; child = GetWindow( child, GW_HWNDNEXT ))
449 WND *childPtr;
450 if (child == hwnd) continue;
451 if ((GetWindowLongW( child, GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != (WS_VISIBLE|WS_MINIMIZE))
452 continue;
453 if (!(childPtr = WIN_GetPtr( child )) || childPtr == WND_OTHER_PROCESS)
454 continue;
455 SetRectRgn( tmp, childPtr->rectWindow.left, childPtr->rectWindow.top,
456 childPtr->rectWindow.right, childPtr->rectWindow.bottom );
457 CombineRgn( hrgn, hrgn, tmp, RGN_OR );
458 WIN_ReleasePtr( childPtr );
460 DeleteObject( tmp );
462 for (rect.bottom = rectParent.bottom; rect.bottom >= yspacing; rect.bottom -= yspacing)
464 for (rect.left = rectParent.left; rect.left <= rectParent.right - xspacing; rect.left += xspacing)
466 rect.right = rect.left + xspacing;
467 rect.top = rect.bottom - yspacing;
468 if (!RectInRegion( hrgn, &rect ))
470 /* No window was found, so it's OK for us */
471 pt.x = rect.left + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
472 pt.y = rect.top + (yspacing - GetSystemMetrics(SM_CYICON)) / 2;
473 DeleteObject( hrgn );
474 return pt;
478 DeleteObject( hrgn );
479 pt.x = pt.y = 0;
480 return pt;
484 /***********************************************************************
485 * WINPOS_MinMaximize
487 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
489 WND *wndPtr;
490 UINT swpFlags = 0;
491 POINT size;
492 LONG old_style;
493 WINDOWPLACEMENT wpl;
495 TRACE("%p %u\n", hwnd, cmd );
497 wpl.length = sizeof(wpl);
498 GetWindowPlacement( hwnd, &wpl );
500 if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
501 return SWP_NOSIZE | SWP_NOMOVE;
503 if (IsIconic( hwnd ))
505 switch (cmd)
507 case SW_SHOWMINNOACTIVE:
508 case SW_SHOWMINIMIZED:
509 case SW_FORCEMINIMIZE:
510 case SW_MINIMIZE:
511 return SWP_NOSIZE | SWP_NOMOVE;
513 if (!SendMessageW( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
514 swpFlags |= SWP_NOCOPYBITS;
517 switch( cmd )
519 case SW_SHOWMINNOACTIVE:
520 case SW_SHOWMINIMIZED:
521 case SW_FORCEMINIMIZE:
522 case SW_MINIMIZE:
523 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
524 if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
525 else wndPtr->flags &= ~WIN_RESTORE_MAX;
526 WIN_ReleasePtr( wndPtr );
528 old_style = WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
530 X11DRV_set_iconic_state( hwnd );
532 wpl.ptMinPosition = WINPOS_FindIconPos( hwnd, wpl.ptMinPosition );
534 if (!(old_style & WS_MINIMIZE)) swpFlags |= SWP_STATECHANGED;
535 SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
536 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON) );
537 swpFlags |= SWP_NOCOPYBITS;
538 break;
540 case SW_MAXIMIZE:
541 old_style = GetWindowLongW( hwnd, GWL_STYLE );
542 if ((old_style & WS_MAXIMIZE) && (old_style & WS_VISIBLE)) return SWP_NOSIZE | SWP_NOMOVE;
544 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
546 old_style = WIN_SetStyle( hwnd, WS_MAXIMIZE, WS_MINIMIZE );
547 if (old_style & WS_MINIMIZE)
549 WINPOS_ShowIconTitle( hwnd, FALSE );
550 X11DRV_set_iconic_state( hwnd );
552 if (!(old_style & WS_MAXIMIZE)) swpFlags |= SWP_STATECHANGED;
553 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
554 break;
556 case SW_SHOWNOACTIVATE:
557 case SW_SHOWNORMAL:
558 case SW_RESTORE:
559 old_style = WIN_SetStyle( hwnd, 0, WS_MINIMIZE | WS_MAXIMIZE );
560 if (old_style & WS_MINIMIZE)
562 BOOL restore_max;
564 WINPOS_ShowIconTitle( hwnd, FALSE );
565 X11DRV_set_iconic_state( hwnd );
567 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
568 restore_max = (wndPtr->flags & WIN_RESTORE_MAX) != 0;
569 WIN_ReleasePtr( wndPtr );
570 if (restore_max)
572 /* Restore to maximized position */
573 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
574 WIN_SetStyle( hwnd, WS_MAXIMIZE, 0 );
575 swpFlags |= SWP_STATECHANGED;
576 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
577 break;
580 else if (!(old_style & WS_MAXIMIZE)) break;
582 swpFlags |= SWP_STATECHANGED;
584 /* Restore to normal position */
586 *rect = wpl.rcNormalPosition;
587 rect->right -= rect->left;
588 rect->bottom -= rect->top;
590 break;
593 return swpFlags;
597 /***********************************************************************
598 * X11DRV_WMMaximizeWindow
600 * Ask X to Maximize the window
601 * assume win lock is held
603 static BOOL X11DRV_WMMaximizeWindow(HWND hwnd, BOOL max)
605 Window win = X11DRV_get_whole_window( hwnd );
606 TRACE("%p max=%s\n", hwnd, max?"TRUE":"FALSE");
608 /* GNOME style */
609 if(WM_has_win_state)
611 XEvent xev;
613 wine_tsx11_lock();
614 xev.xclient.type = ClientMessage;
615 xev.xclient.window = win;
616 xev.xclient.message_type = x11drv_atom(_WIN_STATE);
617 xev.xclient.format = 32;
618 xev.xclient.serial = 0;
619 xev.xclient.display = thread_display();
620 xev.xclient.send_event = True;
621 xev.xclient.data.l[0] = WIN_STATE_MAXIMIZED_VERT | WIN_STATE_MAXIMIZED_HORIZ;
622 xev.xclient.data.l[1] = 0;
623 if(max)
624 xev.xclient.data.l[1] = WIN_STATE_MAXIMIZED_VERT | WIN_STATE_MAXIMIZED_HORIZ;
625 XSendEvent(thread_display(), root_window, False, SubstructureNotifyMask, &xev);
626 wine_tsx11_unlock();
628 return TRUE;
631 /* KDE 2 style */
632 if(WM_has_net_wm_state)
634 XEvent xev;
636 wine_tsx11_lock();
637 xev.xclient.type = ClientMessage;
638 xev.xclient.window = win;
639 xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
640 xev.xclient.serial = 0;
641 xev.xclient.display = thread_display();
642 xev.xclient.send_event = True;
643 xev.xclient.format = 32;
644 if(max)
645 xev.xclient.data.l[0] = _NET_WM_STATE_ADD;
646 else
647 xev.xclient.data.l[0] = _NET_WM_STATE_REMOVE;
648 xev.xclient.data.l[1] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
649 xev.xclient.data.l[2] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_VERT);
650 XSendEvent(thread_display(), root_window, False, SubstructureNotifyMask, &xev);
651 wine_tsx11_unlock();
653 return TRUE;
656 /* KDE 1 style */
657 if(x11drv_atom(KWM_MAXIMIZE_WINDOW)!=None)
659 wine_tsx11_lock();
660 XChangeProperty( thread_display(), win, x11drv_atom(KWM_MAXIMIZE_WINDOW),
661 x11drv_atom(KWM_MAXIMIZE_WINDOW), 8, PropModeReplace, (unsigned char*)&max, 1 );
662 wine_tsx11_unlock();
663 return TRUE;
666 return FALSE;
670 /***********************************************************************
671 * X11DRV_WMMinimizeWindow
673 * See the ICCCM section 4.1.4. Changing Window State for more details.
674 * http://tronche.com/gui/x/icccm/sec-4.html#s-4.1.4
676 static BOOL X11DRV_WMMinimizeWindow(HWND hwnd)
678 XEvent xev;
680 TRACE("%p\n", hwnd);
682 wine_tsx11_lock();
684 xev.xclient.type = ClientMessage;
685 xev.xclient.window = X11DRV_get_whole_window( hwnd );
686 xev.xclient.message_type = x11drv_atom(WM_CHANGE_STATE);
687 xev.xclient.serial = 0;
688 xev.xclient.display = thread_display();
689 xev.xclient.send_event = True;
690 xev.xclient.format = 32;
691 xev.xclient.data.l[0] = IconicState;
692 xev.xclient.data.l[2] = 0;
693 XSendEvent(thread_display(), root_window, False, SubstructureNotifyMask, &xev);
695 wine_tsx11_unlock();
697 return TRUE;
701 /***********************************************************************
702 * ShowWindow (X11DRV.@)
704 BOOL X11DRV_ShowWindow( HWND hwnd, INT cmd )
706 Display *display = thread_display();
707 WND *wndPtr;
708 HWND parent;
709 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
710 BOOL wasVisible = (style & WS_VISIBLE) != 0;
711 BOOL showFlag = TRUE;
712 BOOL sendWmMaximize, maximize;
713 RECT newPos = {0, 0, 0, 0};
714 UINT swp = 0;
716 TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);
718 /* check if we should request an action from the window manager */
719 sendWmMaximize = FALSE;
720 maximize = FALSE;
721 if(WM_has_win_state || WM_has_net_wm_state || (x11drv_atom(KWM_RUNNING)!=None) )
723 if ((root_window == DefaultRootWindow(display)) &&
724 GetAncestor(hwnd,GA_PARENT) == GetDesktopWindow())
726 if (cmd == SW_MAXIMIZE)
728 sendWmMaximize = TRUE;
729 maximize = TRUE;
734 switch(cmd)
736 case SW_HIDE:
737 if (!wasVisible) return FALSE;
738 showFlag = FALSE;
739 swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE;
740 if (hwnd != GetActiveWindow())
741 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
742 break;
744 case SW_SHOWMINNOACTIVE:
745 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
746 /* fall through */
747 case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
748 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
749 /* fall through */
750 case SW_MINIMIZE:
752 /* handle minimize a different way */
753 if ((root_window == DefaultRootWindow(display)) &&
754 GetAncestor(hwnd,GA_PARENT) == GetDesktopWindow())
756 if( !(style & WS_MINIMIZE) )
758 X11DRV_WMMinimizeWindow( hwnd );
759 WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
761 return wasVisible;
764 swp |= SWP_FRAMECHANGED;
765 if( !(style & WS_MINIMIZE) )
766 swp |= WINPOS_MinMaximize( hwnd, SW_MINIMIZE, &newPos );
767 else swp |= SWP_NOSIZE | SWP_NOMOVE;
768 break;
770 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
771 if (!wasVisible) swp |= SWP_SHOWWINDOW;
772 swp |= SWP_FRAMECHANGED;
773 swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
774 if ((style & WS_MAXIMIZE) && wasVisible) return TRUE;
775 break;
777 case SW_SHOWNA:
778 swp |= SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
779 if (style & WS_CHILD) swp |= SWP_NOZORDER;
780 break;
781 case SW_SHOW:
782 if (wasVisible) return TRUE;
784 if (getenv("CX_HACK_JINITIATOR")) /* Hide Java Console */
786 CHAR buf[16];
788 if (GetWindowTextA(hwnd, buf, 16) && !strcmp(buf, "Java Console"))
789 return FALSE;
791 swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
792 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
793 break;
795 case SW_SHOWNOACTIVATE:
796 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
797 /* fall through */
798 case SW_RESTORE:
799 /* fall through */
800 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
801 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
802 if (!wasVisible) swp |= SWP_SHOWWINDOW;
803 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
805 swp |= SWP_FRAMECHANGED;
806 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
808 else
810 if (wasVisible) return TRUE;
811 swp |= SWP_NOSIZE | SWP_NOMOVE;
813 if (style & WS_CHILD && !(swp & SWP_STATECHANGED)) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
814 break;
817 if ((showFlag != wasVisible || cmd == SW_SHOWNA) && cmd != SW_SHOWMAXIMIZED && !(swp & SWP_STATECHANGED))
819 SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
820 if (!IsWindow( hwnd )) return wasVisible;
823 parent = GetAncestor( hwnd, GA_PARENT );
824 if (parent && !IsWindowVisible( parent ) && !(swp & SWP_STATECHANGED))
826 /* if parent is not visible simply toggle WS_VISIBLE and return */
827 if (showFlag) WIN_SetStyle( hwnd, WS_VISIBLE, 0 );
828 else WIN_SetStyle( hwnd, 0, WS_VISIBLE );
830 else
831 SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
832 newPos.right, newPos.bottom, LOWORD(swp) );
834 if (cmd == SW_HIDE)
836 HWND hFocus;
838 /* FIXME: This will cause the window to be activated irrespective
839 * of whether it is owned by the same thread. Has to be done
840 * asynchronously.
843 if (hwnd == GetActiveWindow())
844 WINPOS_ActivateOtherWindow(hwnd);
846 /* Revert focus to parent */
847 hFocus = GetFocus();
848 if (hwnd == hFocus || IsChild(hwnd, hFocus))
850 HWND parent = GetAncestor(hwnd, GA_PARENT);
851 if (parent == GetDesktopWindow()) parent = 0;
852 SetFocus(parent);
856 if (IsIconic(hwnd)) WINPOS_ShowIconTitle( hwnd, TRUE );
858 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return wasVisible;
860 if (wndPtr->flags & WIN_NEED_SIZE)
862 /* should happen only in CreateWindowEx() */
863 int wParam = SIZE_RESTORED;
864 RECT client = wndPtr->rectClient;
866 wndPtr->flags &= ~WIN_NEED_SIZE;
867 if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
868 else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
869 WIN_ReleasePtr( wndPtr );
871 SendMessageW( hwnd, WM_SIZE, wParam,
872 MAKELONG( client.right - client.left, client.bottom - client.top ));
873 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( client.left, client.top ));
875 else WIN_ReleasePtr( wndPtr );
877 /* ask the window manager to maximize this window for us */
878 if( sendWmMaximize )
879 X11DRV_WMMaximizeWindow(hwnd, maximize);
881 /* if previous state was minimized Windows sets focus to the window */
882 if (style & WS_MINIMIZE) SetFocus( hwnd );
884 return wasVisible;
888 /**********************************************************************
889 * X11DRV_MapNotify
891 void X11DRV_MapNotify( HWND hwnd, XEvent *event )
893 struct x11drv_win_data *data;
894 HWND hwndFocus = GetFocus();
895 WND *win;
897 if (!(data = X11DRV_get_win_data( hwnd ))) return;
899 if (!(win = WIN_GetPtr( hwnd ))) return;
901 if (data->managed && (win->dwStyle & WS_VISIBLE) && (win->dwStyle & WS_MINIMIZE))
903 int x, y;
904 unsigned int width, height, border, depth;
905 Window root, top;
906 RECT rect;
907 LONG style = WS_VISIBLE;
909 /* FIXME: hack */
910 wine_tsx11_lock();
911 XGetGeometry( event->xmap.display, data->whole_window, &root, &x, &y, &width, &height,
912 &border, &depth );
913 XTranslateCoordinates( event->xmap.display, data->whole_window, root, 0, 0, &x, &y, &top );
914 wine_tsx11_unlock();
915 rect.left = x;
916 rect.top = y;
917 rect.right = x + width;
918 rect.bottom = y + height;
919 OffsetRect( &rect, virtual_screen_rect.left, virtual_screen_rect.top );
920 X11DRV_X_to_window_rect( data, &rect );
922 invalidate_dce( hwnd, &data->window_rect );
924 if (win->flags & WIN_RESTORE_MAX) style |= WS_MAXIMIZE;
925 WIN_SetStyle( hwnd, style, WS_MINIMIZE );
926 WIN_ReleasePtr( win );
928 SendMessageW( hwnd, WM_SHOWWINDOW, SW_RESTORE, 0 );
929 data->lock_changes++;
930 SetWindowPos( hwnd, 0, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
931 SWP_NOZORDER | SWP_FRAMECHANGED | SWP_STATECHANGED );
932 data->lock_changes--;
934 else WIN_ReleasePtr( win );
935 if (hwndFocus && IsChild( hwnd, hwndFocus )) X11DRV_SetFocus(hwndFocus); /* FIXME */
939 /**********************************************************************
940 * X11DRV_UnmapNotify
942 void X11DRV_UnmapNotify( HWND hwnd, XEvent *event )
944 struct x11drv_win_data *data;
945 WND *win;
947 if (!(data = X11DRV_get_win_data( hwnd ))) return;
949 if (!(win = WIN_GetPtr( hwnd ))) return;
951 if ((win->dwStyle & WS_VISIBLE) && data->managed &&
952 X11DRV_is_window_rect_mapped( &win->rectWindow ))
954 if (win->dwStyle & WS_MAXIMIZE)
955 win->flags |= WIN_RESTORE_MAX;
956 else if (!(win->dwStyle & WS_MINIMIZE))
957 win->flags &= ~WIN_RESTORE_MAX;
959 WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
960 WIN_ReleasePtr( win );
962 EndMenu();
963 SendMessageW( hwnd, WM_SHOWWINDOW, SW_MINIMIZE, 0 );
964 data->lock_changes++;
965 SetWindowPos( hwnd, 0, 0, 0, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON),
966 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_STATECHANGED );
967 data->lock_changes--;
969 else WIN_ReleasePtr( win );
972 struct desktop_resize_data
974 RECT old_screen_rect;
975 RECT old_virtual_rect;
978 static BOOL CALLBACK update_windows_on_desktop_resize( HWND hwnd, LPARAM lparam )
980 struct x11drv_win_data *data;
981 Display *display = thread_display();
982 struct desktop_resize_data *resize_data = (struct desktop_resize_data *)lparam;
983 int mask = 0;
985 if (!(data = X11DRV_get_win_data( hwnd ))) return TRUE;
987 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)
989 BOOL new_fs_state;
991 if (fullscreen_state_changed( data, &data->client_rect, &resize_data->old_screen_rect, &new_fs_state ))
992 update_fullscreen_state( display, data->whole_window, new_fs_state );
995 if (resize_data->old_virtual_rect.left != virtual_screen_rect.left) mask |= CWX;
996 if (resize_data->old_virtual_rect.top != virtual_screen_rect.top) mask |= CWY;
997 if (mask && data->whole_window)
999 XWindowChanges changes;
1001 wine_tsx11_lock();
1002 changes.x = data->whole_rect.left - virtual_screen_rect.left;
1003 changes.y = data->whole_rect.top - virtual_screen_rect.top;
1004 XReconfigureWMWindow( display, data->whole_window,
1005 DefaultScreen(display), mask, &changes );
1006 wine_tsx11_unlock();
1008 return TRUE;
1012 /***********************************************************************
1013 * X11DRV_handle_desktop_resize
1015 void X11DRV_handle_desktop_resize( unsigned int width, unsigned int height )
1017 HWND hwnd = GetDesktopWindow();
1018 struct x11drv_win_data *data;
1019 struct desktop_resize_data resize_data;
1021 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1023 SetRect( &resize_data.old_screen_rect, 0, 0, screen_width, screen_height );
1024 resize_data.old_virtual_rect = virtual_screen_rect;
1026 screen_width = width;
1027 screen_height = height;
1028 xinerama_init();
1029 TRACE("desktop %p change to (%dx%d)\n", hwnd, width, height);
1030 data->lock_changes++;
1031 X11DRV_SetWindowPos( hwnd, 0, &virtual_screen_rect, &virtual_screen_rect,
1032 SWP_NOZORDER|SWP_NOMOVE, NULL );
1033 data->lock_changes--;
1034 ClipCursor(NULL);
1035 SendMessageTimeoutW( HWND_BROADCAST, WM_DISPLAYCHANGE, screen_depth,
1036 MAKELPARAM( width, height ), SMTO_ABORTIFHUNG, 2000, NULL );
1038 EnumWindows( update_windows_on_desktop_resize, (LPARAM)&resize_data );
1042 /***********************************************************************
1043 * X11DRV_GetXWMWindowState
1045 * Check whether the X Window Manager thinks the window is maximized
1047 * return TRUE if the check was possible
1049 static BOOL X11DRV_GetXWMWindowState(Display* d, Window w, BOOL *maximized)
1051 int r;
1052 unsigned long left = 0, ret_length = 0;
1053 int ret_format = 0;
1054 Atom ret_atom = None;
1056 /* GNOME style _WIN_STATE */
1057 if( WM_has_win_state )
1059 unsigned char *data = NULL;
1061 wine_tsx11_lock();
1063 r = XGetWindowProperty( d, w, x11drv_atom(_WIN_STATE), 0L, 1, False, AnyPropertyType,
1064 &ret_atom, &ret_format, &ret_length, &left, &data);
1065 if(r==Success)
1067 if(ret_atom!=None)
1069 TRACE("_win_state=%d\n", *data);
1070 if( (*data & WIN_STATE_MAXIMIZED_VERT) &&
1071 (*data & WIN_STATE_MAXIMIZED_HORIZ) )
1072 *maximized = TRUE;
1073 else
1074 *maximized = FALSE;
1076 XFree(data);
1079 wine_tsx11_unlock();
1081 return ((r==Success) && (ret_atom!=None));
1084 /* KDE 2 style _NET_WM_STATE */
1085 if( WM_has_net_wm_state )
1087 long *data = NULL;
1088 int i, vert_max = 0, horz_max = 0;
1090 wine_tsx11_lock();
1091 r = XGetWindowProperty(
1092 d, w, x11drv_atom(_NET_WM_STATE), 0L, 10L, False, AnyPropertyType,
1093 &ret_atom, &ret_format, &ret_length, &left, (unsigned char **)&data);
1094 if(r==Success)
1096 if(ret_atom!=None)
1098 for(i=0; i<ret_length; i++)
1100 if(data[i]==x11drv_atom(_NET_WM_STATE_MAXIMIZED_VERT))
1101 vert_max = 1;
1102 if(data[i]==x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ))
1103 horz_max = 1;
1105 TRACE("_NET_WIN_STATE vmax %d hmax %d\n", vert_max, horz_max);
1106 *maximized = vert_max & horz_max;
1108 XFree(data);
1110 wine_tsx11_unlock();
1112 return ((r==Success) && (ret_atom!=None));
1115 /* KDE 1 style */
1116 if(x11drv_atom(KWM_WIN_MAXIMIZED)!=None)
1118 unsigned char *data = NULL;
1120 wine_tsx11_lock();
1121 r = XGetWindowProperty(
1122 d, w, x11drv_atom(KWM_WIN_MAXIMIZED), 0L, 1, False, AnyPropertyType,
1123 &ret_atom, &ret_format, &ret_length, &left, &data);
1124 if(r==Success)
1126 if(ret_atom!=None)
1128 TRACE("kwm_win_maximized=%d\n", *data);
1129 if(*data)
1130 *maximized = TRUE;
1131 else
1132 *maximized = FALSE;
1134 XFree(data);
1136 wine_tsx11_unlock();
1138 return ((r==Success) && (ret_atom!=None));
1141 return FALSE;
1145 /***********************************************************************
1146 * X11DRV_ConfigureNotify
1148 void X11DRV_ConfigureNotify( HWND hwnd, XEvent *xev )
1150 XConfigureEvent *event = &xev->xconfigure;
1151 struct x11drv_win_data *data;
1152 RECT rect;
1153 UINT flags;
1154 int cx, cy, x = event->x, y = event->y;
1155 BOOL max = FALSE;
1156 HWND parent;
1157 RECT ignore; /* suggested restore/maximize size */
1159 if (!hwnd) return;
1160 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1161 parent = GetAncestor( hwnd, GA_PARENT );
1163 /* Get geometry */
1165 if (!event->send_event && data->toplevel)
1167 /* normal event, need to map coordinates to the root */
1168 Window child;
1169 wine_tsx11_lock();
1170 XTranslateCoordinates( event->display, data->whole_window, root_window,
1171 0, 0, &x, &y, &child );
1172 wine_tsx11_unlock();
1175 /* HACK: find out the real window position... if it doesn't match ignore the ConfigureNotify */
1176 if (data->toplevel)
1178 Window ignore_root;
1179 int ignore_x, ignore_y;
1180 unsigned int height, width, ignore_depth, ignore_border;
1182 wine_tsx11_lock();
1183 XGetGeometry(event->display, data->whole_window, &ignore_root,
1184 &ignore_x, &ignore_y, &width, &height, &ignore_border, &ignore_depth);
1185 wine_tsx11_unlock();
1187 if ( (width!=event->width) || (height!=event->height ) )
1189 TRACE("Discarding old ConfigureNotify message for hwnd %p\n",hwnd);
1190 return;
1194 rect.left = x;
1195 rect.top = y;
1196 rect.right = x + event->width;
1197 rect.bottom = y + event->height;
1198 if (event->send_event || data->toplevel)
1199 OffsetRect( &rect, virtual_screen_rect.left, virtual_screen_rect.top );
1200 TRACE( "win %p new X rect %d,%d,%dx%d (event %d,%d,%dx%d) send=%d\n",
1201 hwnd, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1202 event->x, event->y, event->width, event->height, event->send_event);
1203 X11DRV_X_to_window_rect( data, &rect );
1204 if (event->send_event || data->toplevel) MapWindowPoints( 0, parent, (POINT *)&rect, 2 );
1206 /* where possible, this code attempts to synchronize the window manager's
1207 Maximized state with wine's internal one */
1208 if(X11DRV_GetXWMWindowState(event->display, data->whole_window, &max))
1210 TRACE("xmax %d zoomed %d\n", max, IsZoomed(hwnd));
1212 if( IsZoomed(hwnd) && !max )
1213 WINPOS_MinMaximize( hwnd, SW_RESTORE, &ignore );
1214 else if( max && !IsZoomed(hwnd) )
1215 WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &ignore );
1217 TRACE("xmax %d zoomed %d\n", max, IsZoomed(hwnd));
1219 else
1221 /* don't allow maximized state if we can't detect it! */
1222 if (IsZoomed(hwnd) && GetPropA( hwnd, "__wine_x11_managed" ))
1223 WINPOS_MinMaximize( hwnd, SW_RESTORE, &ignore );
1226 x = rect.left;
1227 y = rect.top;
1228 cx = rect.right - rect.left;
1229 cy = rect.bottom - rect.top;
1230 flags = SWP_NOACTIVATE | SWP_NOZORDER;
1232 /* Compare what has changed */
1234 GetWindowRect( hwnd, &rect );
1235 MapWindowPoints( 0, parent, (POINT *)&rect, 2 );
1236 if (rect.left == x && rect.top == y) flags |= SWP_NOMOVE;
1237 else
1238 TRACE( "%p moving from (%d,%d) to (%d,%d)\n",
1239 hwnd, rect.left, rect.top, x, y );
1241 if ((rect.right - rect.left == cx && rect.bottom - rect.top == cy) ||
1242 IsIconic(hwnd) ||
1243 (IsRectEmpty( &rect ) && event->width == 1 && event->height == 1))
1245 if (flags & SWP_NOMOVE) return; /* if nothing changed, don't do anything */
1246 flags |= SWP_NOSIZE;
1248 else
1249 TRACE( "%p resizing from (%dx%d) to (%dx%d)\n",
1250 hwnd, rect.right - rect.left, rect.bottom - rect.top, cx, cy );
1252 data->lock_changes++;
1253 SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
1254 data->lock_changes--;
1258 /***********************************************************************
1259 * SetWindowRgn (X11DRV.@)
1261 * Assign specified region to window (for non-rectangular windows)
1263 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
1265 struct x11drv_win_data *data;
1267 if (!(data = X11DRV_get_win_data( hwnd )))
1269 if (IsWindow( hwnd ))
1270 FIXME( "not supported on other thread window %p\n", hwnd );
1271 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1272 return FALSE;
1275 #ifdef HAVE_LIBXSHAPE
1276 if (data->whole_window)
1278 Display *display = thread_display();
1280 if (!hrgn)
1282 wine_tsx11_lock();
1283 XShapeCombineMask( display, data->whole_window,
1284 ShapeBounding, 0, 0, None, ShapeSet );
1285 wine_tsx11_unlock();
1287 else
1289 RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
1290 if (pRegionData)
1292 wine_tsx11_lock();
1293 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
1294 data->window_rect.left - data->whole_rect.left,
1295 data->window_rect.top - data->whole_rect.top,
1296 (XRectangle *)pRegionData->Buffer,
1297 pRegionData->rdh.nCount,
1298 ShapeSet, YXBanded );
1299 wine_tsx11_unlock();
1300 HeapFree(GetProcessHeap(), 0, pRegionData);
1304 #endif /* HAVE_LIBXSHAPE */
1306 invalidate_dce( hwnd, &data->window_rect );
1307 return TRUE;
1311 /***********************************************************************
1312 * draw_moving_frame
1314 * Draw the frame used when moving or resizing window.
1316 * FIXME: This causes problems in Win95 mode. (why?)
1318 static void draw_moving_frame( HDC hdc, RECT *rect, BOOL thickframe )
1320 if (thickframe)
1322 const int width = GetSystemMetrics(SM_CXFRAME);
1323 const int height = GetSystemMetrics(SM_CYFRAME);
1325 HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
1326 PatBlt( hdc, rect->left, rect->top,
1327 rect->right - rect->left - width, height, PATINVERT );
1328 PatBlt( hdc, rect->left, rect->top + height, width,
1329 rect->bottom - rect->top - height, PATINVERT );
1330 PatBlt( hdc, rect->left + width, rect->bottom - 1,
1331 rect->right - rect->left - width, -height, PATINVERT );
1332 PatBlt( hdc, rect->right - 1, rect->top, -width,
1333 rect->bottom - rect->top - height, PATINVERT );
1334 SelectObject( hdc, hbrush );
1336 else DrawFocusRect( hdc, rect );
1340 /***********************************************************************
1341 * start_size_move
1343 * Initialisation of a move or resize, when initiatied from a menu choice.
1344 * Return hit test code for caption or sizing border.
1346 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
1348 LONG hittest = 0;
1349 POINT pt;
1350 MSG msg;
1351 RECT rectWindow;
1353 GetWindowRect( hwnd, &rectWindow );
1355 if ((wParam & 0xfff0) == SC_MOVE)
1357 /* Move pointer at the center of the caption */
1358 RECT rect = rectWindow;
1359 /* Note: to be exactly centered we should take the different types
1360 * of border into account, but it shouldn't make more that a few pixels
1361 * of difference so let's not bother with that */
1362 rect.top += GetSystemMetrics(SM_CYBORDER);
1363 if (style & WS_SYSMENU)
1364 rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
1365 if (style & WS_MINIMIZEBOX)
1366 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1367 if (style & WS_MAXIMIZEBOX)
1368 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1369 pt.x = (rect.right + rect.left) / 2;
1370 pt.y = rect.top + GetSystemMetrics(SM_CYSIZE)/2;
1371 hittest = HTCAPTION;
1372 *capturePoint = pt;
1374 else /* SC_SIZE */
1376 pt.x = pt.y = 0;
1377 while(!hittest)
1379 GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST );
1380 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1382 switch(msg.message)
1384 case WM_MOUSEMOVE:
1385 pt = msg.pt;
1386 hittest = SendMessageW( hwnd, WM_NCHITTEST, 0, MAKELONG( pt.x, pt.y ) );
1387 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT)) hittest = 0;
1388 break;
1390 case WM_LBUTTONUP:
1391 return 0;
1393 case WM_KEYDOWN:
1394 switch(msg.wParam)
1396 case VK_UP:
1397 hittest = HTTOP;
1398 pt.x =(rectWindow.left+rectWindow.right)/2;
1399 pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
1400 break;
1401 case VK_DOWN:
1402 hittest = HTBOTTOM;
1403 pt.x =(rectWindow.left+rectWindow.right)/2;
1404 pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
1405 break;
1406 case VK_LEFT:
1407 hittest = HTLEFT;
1408 pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
1409 pt.y =(rectWindow.top+rectWindow.bottom)/2;
1410 break;
1411 case VK_RIGHT:
1412 hittest = HTRIGHT;
1413 pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
1414 pt.y =(rectWindow.top+rectWindow.bottom)/2;
1415 break;
1416 case VK_RETURN:
1417 case VK_ESCAPE: return 0;
1421 *capturePoint = pt;
1423 SetCursorPos( pt.x, pt.y );
1424 SendMessageW( hwnd, WM_SETCURSOR, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
1425 return hittest;
1429 /***********************************************************************
1430 * set_movesize_capture
1432 static void set_movesize_capture( HWND hwnd )
1434 HWND previous = 0;
1436 SERVER_START_REQ( set_capture_window )
1438 req->handle = hwnd;
1439 req->flags = CAPTURE_MOVESIZE;
1440 if (!wine_server_call_err( req ))
1442 previous = reply->previous;
1443 hwnd = reply->full_handle;
1446 SERVER_END_REQ;
1447 if (previous && previous != hwnd)
1448 SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
1451 /***********************************************************************
1452 * X11DRV_WMMoveResizeWindow
1454 * Tells the window manager to initiate a move or resize operation.
1456 * SEE
1457 * http://freedesktop.org/Standards/wm-spec/1.3/ar01s04.html
1458 * or search for "_NET_WM_MOVERESIZE"
1460 static void X11DRV_WMMoveResizeWindow( HWND hwnd, int x, int y, int dir )
1462 XEvent xev;
1463 Display *display = thread_display();
1465 TRACE("hwnd %p, x %d, y %d, dir %d\n", hwnd, x, y, dir);
1467 xev.xclient.type = ClientMessage;
1468 xev.xclient.window = X11DRV_get_whole_window(hwnd);
1469 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
1470 xev.xclient.serial = 0;
1471 xev.xclient.display = display;
1472 xev.xclient.send_event = True;
1473 xev.xclient.format = 32;
1474 xev.xclient.data.l[0] = x; /* x coord */
1475 xev.xclient.data.l[1] = y; /* y coord */
1476 xev.xclient.data.l[2] = dir; /* direction */
1477 xev.xclient.data.l[3] = 1; /* button */
1478 xev.xclient.data.l[4] = 0; /* unused */
1480 /* need to ungrab the pointer that may have been automatically grabbed
1481 * with a ButtonPress event */
1482 wine_tsx11_lock();
1483 XUngrabPointer( display, CurrentTime );
1484 XSendEvent(display, root_window, False, SubstructureNotifyMask, &xev);
1485 wine_tsx11_unlock();
1488 /***********************************************************************
1489 * SysCommandSizeMove (X11DRV.@)
1491 * Perform SC_MOVE and SC_SIZE commands.
1493 void X11DRV_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
1495 MSG msg;
1496 RECT sizingRect, mouseRect, origRect;
1497 HDC hdc;
1498 HWND parent;
1499 LONG hittest = (LONG)(wParam & 0x0f);
1500 WPARAM syscommand = wParam & 0xfff0;
1501 HCURSOR hDragCursor = 0, hOldCursor = 0;
1502 POINT minTrack, maxTrack;
1503 POINT capturePoint, pt;
1504 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
1505 BOOL thickframe = HAS_THICKFRAME( style );
1506 BOOL iconic = style & WS_MINIMIZE;
1507 BOOL moved = FALSE;
1508 DWORD dwPoint = GetMessagePos ();
1509 BOOL DragFullWindows = FALSE;
1510 BOOL grab;
1511 Window parent_win, whole_win;
1512 Display *old_gdi_display = NULL;
1513 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1514 struct x11drv_win_data *data;
1516 pt.x = (short)LOWORD(dwPoint);
1517 pt.y = (short)HIWORD(dwPoint);
1518 capturePoint = pt;
1520 if (IsZoomed(hwnd) || !IsWindowVisible(hwnd)) return;
1522 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1524 TRACE("hwnd %p (%smanaged), command %04x, hittest %d, pos %d,%d\n",
1525 hwnd, data->managed ? "" : "NOT ", syscommand, hittest, pt.x, pt.y);
1527 /* if we are managed then we let the WM do all the work */
1528 if (data->managed)
1530 int dir;
1531 if (syscommand == SC_MOVE)
1533 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
1534 else dir = _NET_WM_MOVERESIZE_MOVE;
1536 else if (!hittest) dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD;
1537 else
1538 switch (hittest)
1540 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
1541 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
1542 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
1543 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
1544 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
1545 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
1546 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
1547 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
1548 default:
1549 ERR("Invalid hittest value: %d\n", hittest);
1550 dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD;
1552 X11DRV_WMMoveResizeWindow( hwnd, pt.x, pt.y, dir );
1553 return;
1556 SystemParametersInfoA(SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0);
1558 if (syscommand == SC_MOVE)
1560 if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1561 if (!hittest) return;
1563 else /* SC_SIZE */
1565 if ( hittest && (syscommand != SC_MOUSEMENU) )
1566 hittest += (HTLEFT - WMSZ_LEFT);
1567 else
1569 set_movesize_capture( hwnd );
1570 hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1571 if (!hittest)
1573 set_movesize_capture(0);
1574 return;
1579 /* Get min/max info */
1581 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1582 GetWindowRect( hwnd, &sizingRect );
1583 if (style & WS_CHILD)
1585 parent = GetParent(hwnd);
1586 /* make sizing rect relative to parent */
1587 MapWindowPoints( 0, parent, (POINT*)&sizingRect, 2 );
1588 GetClientRect( parent, &mouseRect );
1590 else
1592 parent = 0;
1593 mouseRect = virtual_screen_rect;
1595 origRect = sizingRect;
1597 if (ON_LEFT_BORDER(hittest))
1599 mouseRect.left = max( mouseRect.left, sizingRect.right-maxTrack.x );
1600 mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
1602 else if (ON_RIGHT_BORDER(hittest))
1604 mouseRect.left = max( mouseRect.left, sizingRect.left+minTrack.x );
1605 mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
1607 if (ON_TOP_BORDER(hittest))
1609 mouseRect.top = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
1610 mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
1612 else if (ON_BOTTOM_BORDER(hittest))
1614 mouseRect.top = max( mouseRect.top, sizingRect.top+minTrack.y );
1615 mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
1617 if (parent) MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
1619 /* Retrieve a default cache DC (without using the window style) */
1620 hdc = GetDCEx( parent, 0, DCX_CACHE );
1622 if( iconic ) /* create a cursor for dragging */
1624 hDragCursor = (HCURSOR)GetClassLongPtrW( hwnd, GCLP_HICON);
1625 if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageW( hwnd, WM_QUERYDRAGICON, 0, 0L);
1626 if( !hDragCursor ) iconic = FALSE;
1629 /* repaint the window before moving it around */
1630 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1632 SendMessageW( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
1633 set_movesize_capture( hwnd );
1635 /* grab the server only when moving top-level windows without desktop */
1636 grab = (!DragFullWindows && !parent && (root_window == DefaultRootWindow(gdi_display)));
1638 if (grab)
1640 wine_tsx11_lock();
1641 XSync( gdi_display, False );
1642 XGrabServer( thread_data->display );
1643 XSync( thread_data->display, False );
1644 /* switch gdi display to the thread display, since the server is grabbed */
1645 old_gdi_display = gdi_display;
1646 gdi_display = thread_data->display;
1647 wine_tsx11_unlock();
1649 whole_win = X11DRV_get_whole_window( GetAncestor(hwnd,GA_ROOT) );
1650 parent_win = parent ? X11DRV_get_whole_window( GetAncestor(parent,GA_ROOT) ) : root_window;
1652 wine_tsx11_lock();
1653 XGrabPointer( thread_data->display, whole_win, False,
1654 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1655 GrabModeAsync, GrabModeAsync, parent_win, None, CurrentTime );
1656 wine_tsx11_unlock();
1657 thread_data->grab_window = whole_win;
1659 while(1)
1661 int dx = 0, dy = 0;
1663 if (!GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST )) break;
1664 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1666 /* Exit on button-up, Return, or Esc */
1667 if ((msg.message == WM_LBUTTONUP) ||
1668 ((msg.message == WM_KEYDOWN) &&
1669 ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
1671 if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
1672 continue; /* We are not interested in other messages */
1674 pt = msg.pt;
1676 if (msg.message == WM_KEYDOWN) switch(msg.wParam)
1678 case VK_UP: pt.y -= 8; break;
1679 case VK_DOWN: pt.y += 8; break;
1680 case VK_LEFT: pt.x -= 8; break;
1681 case VK_RIGHT: pt.x += 8; break;
1684 pt.x = max( pt.x, mouseRect.left );
1685 pt.x = min( pt.x, mouseRect.right );
1686 pt.y = max( pt.y, mouseRect.top );
1687 pt.y = min( pt.y, mouseRect.bottom );
1689 dx = pt.x - capturePoint.x;
1690 dy = pt.y - capturePoint.y;
1692 if (dx || dy)
1694 if( !moved )
1696 moved = TRUE;
1698 if( iconic ) /* ok, no system popup tracking */
1700 hOldCursor = SetCursor(hDragCursor);
1701 ShowCursor( TRUE );
1702 WINPOS_ShowIconTitle( hwnd, FALSE );
1704 else if(!DragFullWindows)
1705 draw_moving_frame( hdc, &sizingRect, thickframe );
1708 if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
1709 else
1711 RECT newRect = sizingRect;
1712 WPARAM wpSizingHit = 0;
1714 if (hittest == HTCAPTION) OffsetRect( &newRect, dx, dy );
1715 if (ON_LEFT_BORDER(hittest)) newRect.left += dx;
1716 else if (ON_RIGHT_BORDER(hittest)) newRect.right += dx;
1717 if (ON_TOP_BORDER(hittest)) newRect.top += dy;
1718 else if (ON_BOTTOM_BORDER(hittest)) newRect.bottom += dy;
1719 if(!iconic && !DragFullWindows) draw_moving_frame( hdc, &sizingRect, thickframe );
1720 capturePoint = pt;
1722 /* determine the hit location */
1723 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
1724 wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
1725 SendMessageW( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&newRect );
1727 if (!iconic)
1729 if(!DragFullWindows)
1730 draw_moving_frame( hdc, &newRect, thickframe );
1731 else
1732 SetWindowPos( hwnd, 0, newRect.left, newRect.top,
1733 newRect.right - newRect.left,
1734 newRect.bottom - newRect.top,
1735 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1737 sizingRect = newRect;
1742 set_movesize_capture(0);
1743 if( iconic )
1745 if( moved ) /* restore cursors, show icon title later on */
1747 ShowCursor( FALSE );
1748 SetCursor( hOldCursor );
1751 else if (moved && !DragFullWindows)
1752 draw_moving_frame( hdc, &sizingRect, thickframe );
1754 ReleaseDC( parent, hdc );
1756 wine_tsx11_lock();
1757 XUngrabPointer( thread_data->display, CurrentTime );
1758 if (grab)
1760 XSync( thread_data->display, False );
1761 XUngrabServer( thread_data->display );
1762 XSync( thread_data->display, False );
1763 gdi_display = old_gdi_display;
1765 wine_tsx11_unlock();
1766 thread_data->grab_window = None;
1768 if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
1769 moved = FALSE;
1771 SendMessageW( hwnd, WM_EXITSIZEMOVE, 0, 0 );
1772 SendMessageW( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
1774 /* window moved or resized */
1775 if (moved)
1777 /* if the moving/resizing isn't canceled call SetWindowPos
1778 * with the new position or the new size of the window
1780 if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
1782 /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
1783 if(!DragFullWindows)
1784 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
1785 sizingRect.right - sizingRect.left,
1786 sizingRect.bottom - sizingRect.top,
1787 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1789 else
1790 { /* restore previous size/position */
1791 if(DragFullWindows)
1792 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
1793 origRect.right - origRect.left,
1794 origRect.bottom - origRect.top,
1795 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1799 if (IsIconic(hwnd))
1801 /* Single click brings up the system menu when iconized */
1803 if( !moved )
1805 if(style & WS_SYSMENU )
1806 SendMessageW( hwnd, WM_SYSCOMMAND,
1807 SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
1809 else WINPOS_ShowIconTitle( hwnd, TRUE );