wintrust: Add a helper function to initialize chain creation parameters.
[wine] / dlls / winex11.drv / winpos.c
1 /*
2  * Window position related functions.
3  *
4  * Copyright 1993, 1994, 1995, 2001 Alexandre Julliard
5  * Copyright 1995, 1996, 1999 Alex Korobka
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23
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>
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35 #include "winerror.h"
36 #include "wine/wingdi16.h"
37
38 #include "x11drv.h"
39 #include "win.h"
40
41 #include "wine/server.h"
42 #include "wine/debug.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
45
46 #define SWP_AGG_NOPOSCHANGE \
47     (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
48
49 #define SWP_EX_NOCOPY       0x0001
50 #define SWP_EX_PAINTSELF    0x0002
51 #define SWP_EX_NONCLIENT    0x0004
52
53 #define HAS_THICKFRAME(style) \
54     (((style) & WS_THICKFRAME) && \
55      !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
56
57 #define ON_LEFT_BORDER(hit) \
58  (((hit) == HTLEFT) || ((hit) == HTTOPLEFT) || ((hit) == HTBOTTOMLEFT))
59 #define ON_RIGHT_BORDER(hit) \
60  (((hit) == HTRIGHT) || ((hit) == HTTOPRIGHT) || ((hit) == HTBOTTOMRIGHT))
61 #define ON_TOP_BORDER(hit) \
62  (((hit) == HTTOP) || ((hit) == HTTOPLEFT) || ((hit) == HTTOPRIGHT))
63 #define ON_BOTTOM_BORDER(hit) \
64  (((hit) == HTBOTTOM) || ((hit) == HTBOTTOMLEFT) || ((hit) == HTBOTTOMRIGHT))
65
66 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT      0
67 #define _NET_WM_MOVERESIZE_SIZE_TOP          1
68 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT     2
69 #define _NET_WM_MOVERESIZE_SIZE_RIGHT        3
70 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT  4
71 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM       5
72 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT   6
73 #define _NET_WM_MOVERESIZE_SIZE_LEFT         7
74 #define _NET_WM_MOVERESIZE_MOVE              8   /* movement only */
75 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD     9   /* size via keyboard */
76 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD    10   /* move via keyboard */
77
78 #define _NET_WM_STATE_REMOVE  0
79 #define _NET_WM_STATE_ADD     1
80 #define _NET_WM_STATE_TOGGLE  2
81
82 static const char managed_prop[] = "__wine_x11_managed";
83
84 /***********************************************************************
85  *           X11DRV_Expose
86  */
87 void X11DRV_Expose( HWND hwnd, XEvent *xev )
88 {
89     XExposeEvent *event = &xev->xexpose;
90     RECT rect;
91     struct x11drv_win_data *data;
92     int flags = RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN;
93
94     TRACE( "win %p (%lx) %d,%d %dx%d\n",
95            hwnd, event->window, event->x, event->y, event->width, event->height );
96
97     if (!(data = X11DRV_get_win_data( hwnd ))) return;
98
99     rect.left   = event->x;
100     rect.top    = event->y;
101     rect.right  = rect.left + event->width;
102     rect.bottom = rect.top + event->height;
103
104     if (event->window == root_window)
105         OffsetRect( &rect, virtual_screen_rect.left, virtual_screen_rect.top );
106
107     if (rect.left < data->client_rect.left ||
108         rect.top < data->client_rect.top ||
109         rect.right > data->client_rect.right ||
110         rect.bottom > data->client_rect.bottom) flags |= RDW_FRAME;
111
112     SERVER_START_REQ( update_window_zorder )
113     {
114         req->window      = hwnd;
115         req->rect.left   = rect.left + data->whole_rect.left;
116         req->rect.top    = rect.top + data->whole_rect.top;
117         req->rect.right  = rect.right + data->whole_rect.left;
118         req->rect.bottom = rect.bottom + data->whole_rect.top;
119         wine_server_call( req );
120     }
121     SERVER_END_REQ;
122
123     /* make position relative to client area instead of window */
124     OffsetRect( &rect, -data->client_rect.left, -data->client_rect.top );
125     RedrawWindow( hwnd, &rect, 0, flags );
126 }
127
128 /***********************************************************************
129  *              SetWindowStyle   (X11DRV.@)
130  *
131  * Update the X state of a window to reflect a style change
132  */
133 void X11DRV_SetWindowStyle( HWND hwnd, DWORD old_style )
134 {
135     Display *display = thread_display();
136     struct x11drv_win_data *data;
137     DWORD new_style, changed;
138
139     if (hwnd == GetDesktopWindow()) return;
140     if (!(data = X11DRV_get_win_data( hwnd ))) return;
141
142     new_style = GetWindowLongW( hwnd, GWL_STYLE );
143     changed = new_style ^ old_style;
144
145     if (changed & WS_VISIBLE)
146     {
147         if (data->whole_window && X11DRV_is_window_rect_mapped( &data->window_rect ))
148         {
149             if (new_style & WS_VISIBLE)
150             {
151                 TRACE( "mapping win %p\n", hwnd );
152                 X11DRV_sync_window_style( display, data );
153                 X11DRV_set_wm_hints( display, data );
154                 wine_tsx11_lock();
155                 XMapWindow( display, data->whole_window );
156                 wine_tsx11_unlock();
157             }
158             /* we don't unmap windows, that causes trouble with the window manager */
159         }
160         invalidate_dce( hwnd, &data->window_rect );
161     }
162
163     if (changed & WS_DISABLED)
164     {
165         if (data->whole_window && data->wm_hints)
166         {
167             wine_tsx11_lock();
168             data->wm_hints->input = !(new_style & WS_DISABLED);
169             XSetWMHints( display, data->whole_window, data->wm_hints );
170             wine_tsx11_unlock();
171         }
172     }
173 }
174
175
176 /***********************************************************************
177  *     update_fullscreen_state
178  *
179  * Use the NETWM protocol to set the fullscreen state.
180  * This only works for mapped windows.
181  */
182 static void update_fullscreen_state( Display *display, Window win, BOOL new_fs_state )
183 {
184     XEvent xev;
185
186     TRACE("setting fullscreen state for window %lx to %s\n", win, new_fs_state ? "true" : "false");
187
188     xev.xclient.type = ClientMessage;
189     xev.xclient.window = win;
190     xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
191     xev.xclient.serial = 0;
192     xev.xclient.display = display;
193     xev.xclient.send_event = True;
194     xev.xclient.format = 32;
195     xev.xclient.data.l[0] = new_fs_state ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
196     xev.xclient.data.l[1] = x11drv_atom(_NET_WM_STATE_FULLSCREEN);
197     xev.xclient.data.l[2] = 0;
198     wine_tsx11_lock();
199     XSendEvent(display, root_window, False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
200     wine_tsx11_unlock();
201 }
202
203 /***********************************************************************
204  *     fullscreen_state_changed
205  *
206  * Check if the fullscreen state of a given window has changed
207  */
208 static BOOL fullscreen_state_changed( const struct x11drv_win_data *data,
209                                       const RECT *old_client_rect, const RECT *old_screen_rect,
210                                       BOOL *new_fs_state )
211 {
212     BOOL old_fs_state = FALSE;
213
214     *new_fs_state = FALSE;
215
216     if (old_client_rect->left <= 0 && old_client_rect->right >= old_screen_rect->right &&
217         old_client_rect->top <= 0 && old_client_rect->bottom >= old_screen_rect->bottom)
218         old_fs_state = TRUE;
219
220     if (data->client_rect.left <= 0 && data->client_rect.right >= screen_width &&
221         data->client_rect.top <= 0 && data->client_rect.bottom >= screen_height)
222         *new_fs_state = TRUE;
223
224 #if 0 /* useful to debug fullscreen state problems */
225     TRACE("hwnd %p, old rect %s, new rect %s, old screen %s, new screen (0,0-%d,%d)\n",
226            data->hwnd,
227            wine_dbgstr_rect(old_client_rect), wine_dbgstr_rect(&data->client_rect),
228            wine_dbgstr_rect(old_screen_rect), screen_width, screen_height);
229     TRACE("old fs state %d\n, new fs state = %d\n", old_fs_state, *new_fs_state);
230 #endif
231     return *new_fs_state != old_fs_state;
232 }
233
234
235 /***********************************************************************
236  *              SetWindowPos   (X11DRV.@)
237  */
238 BOOL X11DRV_SetWindowPos( HWND hwnd, HWND insert_after, const RECT *rectWindow,
239                                    const RECT *rectClient, UINT swp_flags, const RECT *valid_rects )
240 {
241     Display *display = thread_display();
242     struct x11drv_win_data *data;
243     RECT new_whole_rect, old_client_rect, old_screen_rect;
244     WND *win;
245     DWORD old_style, new_style;
246     BOOL ret, make_managed = FALSE;
247
248     if (!(data = X11DRV_get_win_data( hwnd ))) return FALSE;
249
250     /* check if we need to switch the window to managed */
251     if (!data->managed && data->whole_window && managed_mode &&
252         root_window == DefaultRootWindow( display ) &&
253         data->whole_window != root_window)
254     {
255         if (is_window_managed( hwnd, swp_flags, rectWindow ))
256         {
257             TRACE( "making win %p/%lx managed\n", hwnd, data->whole_window );
258             make_managed = TRUE;
259             data->managed = TRUE;
260             SetPropA( hwnd, managed_prop, (HANDLE)1 );
261         }
262     }
263
264     new_whole_rect = *rectWindow;
265     X11DRV_window_to_X_rect( data, &new_whole_rect );
266
267     old_client_rect = data->client_rect;
268
269     if (!(win = WIN_GetPtr( hwnd ))) return FALSE;
270     if (win == WND_OTHER_PROCESS)
271     {
272         if (IsWindow( hwnd )) ERR( "cannot set rectangles of other process window %p\n", hwnd );
273         return FALSE;
274     }
275     SERVER_START_REQ( set_window_pos )
276     {
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] ))
289         {
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) );
293         }
294         ret = !wine_server_call( req );
295         new_style = reply->new_style;
296     }
297     SERVER_END_REQ;
298
299     if (win == WND_DESKTOP || data->whole_window == DefaultRootWindow(gdi_display))
300     {
301         data->whole_rect = data->client_rect = data->window_rect = *rectWindow;
302         if (win != WND_DESKTOP)
303         {
304             win->rectWindow   = *rectWindow;
305             win->rectClient   = *rectClient;
306             win->dwStyle      = new_style;
307             WIN_ReleasePtr( win );
308         }
309         return ret;
310     }
311
312     if (ret)
313     {
314         /* invalidate DCEs */
315
316         if ((((swp_flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) && (new_style & WS_VISIBLE)) ||
317              (swp_flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW)))
318         {
319             RECT rect;
320             UnionRect( &rect, rectWindow, &win->rectWindow );
321             invalidate_dce( hwnd, &rect );
322         }
323
324         win->rectWindow   = *rectWindow;
325         win->rectClient   = *rectClient;
326         old_style         = win->dwStyle;
327         win->dwStyle      = new_style;
328         data->window_rect = *rectWindow;
329
330         TRACE( "win %p window %s client %s style %08x\n",
331                hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style );
332
333         if (make_managed && (old_style & WS_VISIBLE))
334         {
335             wine_tsx11_lock();
336             XUnmapWindow( display, data->whole_window );
337             wine_tsx11_unlock();
338             old_style &= ~WS_VISIBLE;  /* force it to be mapped again below */
339         }
340
341         if (!IsRectEmpty( &valid_rects[0] ))
342         {
343             int x_offset = 0, y_offset = 0;
344
345             if (data->whole_window)
346             {
347                 /* the X server will move the bits for us */
348                 x_offset = data->whole_rect.left - new_whole_rect.left;
349                 y_offset = data->whole_rect.top - new_whole_rect.top;
350             }
351
352             if (x_offset != valid_rects[1].left - valid_rects[0].left ||
353                 y_offset != valid_rects[1].top - valid_rects[0].top)
354             {
355                 /* FIXME: should copy the window bits here */
356                 RECT invalid_rect = valid_rects[0];
357
358                 /* invalid_rects are relative to the client area */
359                 OffsetRect( &invalid_rect, -rectClient->left, -rectClient->top );
360                 RedrawWindow( hwnd, &invalid_rect, NULL, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
361             }
362         }
363
364
365         if (data->whole_window && !data->lock_changes)
366         {
367             if ((old_style & WS_VISIBLE) && !(new_style & WS_VISIBLE))
368             {
369                 /* window got hidden, unmap it */
370                 TRACE( "unmapping win %p\n", hwnd );
371                 wine_tsx11_lock();
372                 XUnmapWindow( display, data->whole_window );
373                 wine_tsx11_unlock();
374             }
375             else if ((new_style & WS_VISIBLE) && !X11DRV_is_window_rect_mapped( rectWindow ))
376             {
377                 /* resizing to zero size or off screen -> unmap */
378                 TRACE( "unmapping zero size or off-screen win %p\n", hwnd );
379                 wine_tsx11_lock();
380                 XUnmapWindow( display, data->whole_window );
381                 wine_tsx11_unlock();
382             }
383         }
384
385         X11DRV_sync_window_position( display, data, swp_flags, rectClient, &new_whole_rect );
386
387         if (data->whole_window && !data->lock_changes)
388         {
389             BOOL new_fs_state, mapped = FALSE;
390
391             if ((new_style & WS_VISIBLE) && !(new_style & WS_MINIMIZE) &&
392                 X11DRV_is_window_rect_mapped( rectWindow ))
393             {
394                 if (!(old_style & WS_VISIBLE))
395                 {
396                     /* window got shown, map it */
397                     TRACE( "mapping win %p\n", hwnd );
398                     mapped = TRUE;
399                 }
400                 else if ((swp_flags & (SWP_NOSIZE | SWP_NOMOVE)) != (SWP_NOSIZE | SWP_NOMOVE))
401                 {
402                     /* resizing from zero size to non-zero -> map */
403                     TRACE( "mapping non zero size or off-screen win %p\n", hwnd );
404                     mapped = TRUE;
405                 }
406
407                 if (mapped || (swp_flags & SWP_FRAMECHANGED))
408                     X11DRV_set_wm_hints( display, data );
409
410                 if (mapped)
411                 {
412                     X11DRV_sync_window_style( display, data );
413                     wine_tsx11_lock();
414                     XMapWindow( display, data->whole_window );
415                     XFlush( display );
416                     wine_tsx11_unlock();
417                 }
418                 SetRect( &old_screen_rect, 0, 0, screen_width, screen_height );
419                 if (fullscreen_state_changed( data, &old_client_rect, &old_screen_rect, &new_fs_state ) || mapped)
420                     update_fullscreen_state( display, data->whole_window, new_fs_state );
421             }
422         }
423     }
424     WIN_ReleasePtr( win );
425     return ret;
426 }
427
428
429 /***********************************************************************
430  *           WINPOS_FindIconPos
431  *
432  * Find a suitable place for an iconic window.
433  */
434 static POINT WINPOS_FindIconPos( HWND hwnd, POINT pt )
435 {
436     RECT rect, rectParent;
437     HWND parent, child;
438     HRGN hrgn, tmp;
439     int xspacing, yspacing;
440
441     parent = GetAncestor( hwnd, GA_PARENT );
442     GetClientRect( parent, &rectParent );
443     if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
444         (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
445         return pt;  /* The icon already has a suitable position */
446
447     xspacing = GetSystemMetrics(SM_CXICONSPACING);
448     yspacing = GetSystemMetrics(SM_CYICONSPACING);
449
450     /* Check if another icon already occupies this spot */
451     /* FIXME: this is completely inefficient */
452
453     hrgn = CreateRectRgn( 0, 0, 0, 0 );
454     tmp = CreateRectRgn( 0, 0, 0, 0 );
455     for (child = GetWindow( parent, GW_HWNDFIRST ); child; child = GetWindow( child, GW_HWNDNEXT ))
456     {
457         WND *childPtr;
458         if (child == hwnd) continue;
459         if ((GetWindowLongW( child, GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != (WS_VISIBLE|WS_MINIMIZE))
460             continue;
461         if (!(childPtr = WIN_GetPtr( child )) || childPtr == WND_OTHER_PROCESS)
462             continue;
463         SetRectRgn( tmp, childPtr->rectWindow.left, childPtr->rectWindow.top,
464                     childPtr->rectWindow.right, childPtr->rectWindow.bottom );
465         CombineRgn( hrgn, hrgn, tmp, RGN_OR );
466         WIN_ReleasePtr( childPtr );
467     }
468     DeleteObject( tmp );
469
470     for (rect.bottom = rectParent.bottom; rect.bottom >= yspacing; rect.bottom -= yspacing)
471     {
472         for (rect.left = rectParent.left; rect.left <= rectParent.right - xspacing; rect.left += xspacing)
473         {
474             rect.right = rect.left + xspacing;
475             rect.top = rect.bottom - yspacing;
476             if (!RectInRegion( hrgn, &rect ))
477             {
478                 /* No window was found, so it's OK for us */
479                 pt.x = rect.left + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
480                 pt.y = rect.top + (yspacing - GetSystemMetrics(SM_CYICON)) / 2;
481                 DeleteObject( hrgn );
482                 return pt;
483             }
484         }
485     }
486     DeleteObject( hrgn );
487     pt.x = pt.y = 0;
488     return pt;
489 }
490
491
492 /***********************************************************************
493  *           WINPOS_MinMaximize
494  */
495 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
496 {
497     WND *wndPtr;
498     UINT swpFlags = 0;
499     POINT size;
500     LONG old_style;
501     WINDOWPLACEMENT wpl;
502
503     TRACE("%p %u\n", hwnd, cmd );
504
505     wpl.length = sizeof(wpl);
506     GetWindowPlacement( hwnd, &wpl );
507
508     if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
509         return SWP_NOSIZE | SWP_NOMOVE;
510
511     if (IsIconic( hwnd ))
512     {
513         switch (cmd)
514         {
515         case SW_SHOWMINNOACTIVE:
516         case SW_SHOWMINIMIZED:
517         case SW_FORCEMINIMIZE:
518         case SW_MINIMIZE:
519             return SWP_NOSIZE | SWP_NOMOVE;
520         }
521         if (!SendMessageW( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
522         swpFlags |= SWP_NOCOPYBITS;
523     }
524
525     switch( cmd )
526     {
527     case SW_SHOWMINNOACTIVE:
528     case SW_SHOWMINIMIZED:
529     case SW_FORCEMINIMIZE:
530     case SW_MINIMIZE:
531         if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
532         if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
533         else wndPtr->flags &= ~WIN_RESTORE_MAX;
534         WIN_ReleasePtr( wndPtr );
535
536         old_style = WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
537
538         X11DRV_set_iconic_state( hwnd );
539
540         wpl.ptMinPosition = WINPOS_FindIconPos( hwnd, wpl.ptMinPosition );
541
542         if (!(old_style & WS_MINIMIZE)) swpFlags |= SWP_STATECHANGED;
543         SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
544                  GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON) );
545         swpFlags |= SWP_NOCOPYBITS;
546         break;
547
548     case SW_MAXIMIZE:
549         old_style = GetWindowLongW( hwnd, GWL_STYLE );
550         if ((old_style & WS_MAXIMIZE) && (old_style & WS_VISIBLE)) return SWP_NOSIZE | SWP_NOMOVE;
551
552         WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
553
554         old_style = WIN_SetStyle( hwnd, WS_MAXIMIZE, WS_MINIMIZE );
555         if (old_style & WS_MINIMIZE)
556         {
557             WINPOS_ShowIconTitle( hwnd, FALSE );
558             X11DRV_set_iconic_state( hwnd );
559         }
560         if (!(old_style & WS_MAXIMIZE)) swpFlags |= SWP_STATECHANGED;
561         SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
562         break;
563
564     case SW_SHOWNOACTIVATE:
565     case SW_SHOWNORMAL:
566     case SW_RESTORE:
567         old_style = WIN_SetStyle( hwnd, 0, WS_MINIMIZE | WS_MAXIMIZE );
568         if (old_style & WS_MINIMIZE)
569         {
570             BOOL restore_max;
571
572             WINPOS_ShowIconTitle( hwnd, FALSE );
573             X11DRV_set_iconic_state( hwnd );
574
575             if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
576             restore_max = (wndPtr->flags & WIN_RESTORE_MAX) != 0;
577             WIN_ReleasePtr( wndPtr );
578             if (restore_max)
579             {
580                 /* Restore to maximized position */
581                 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
582                 WIN_SetStyle( hwnd, WS_MAXIMIZE, 0 );
583                 swpFlags |= SWP_STATECHANGED;
584                 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
585                 break;
586             }
587         }
588         else if (!(old_style & WS_MAXIMIZE)) break;
589
590         swpFlags |= SWP_STATECHANGED;
591
592         /* Restore to normal position */
593
594         *rect = wpl.rcNormalPosition;
595         rect->right -= rect->left;
596         rect->bottom -= rect->top;
597
598         break;
599     }
600
601     return swpFlags;
602 }
603
604
605 /***********************************************************************
606  *              ShowWindow   (X11DRV.@)
607  */
608 BOOL X11DRV_ShowWindow( HWND hwnd, INT cmd )
609 {
610     WND *wndPtr;
611     HWND parent;
612     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
613     BOOL wasVisible = (style & WS_VISIBLE) != 0;
614     BOOL showFlag = TRUE;
615     RECT newPos = {0, 0, 0, 0};
616     UINT swp = 0;
617
618     TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);
619
620     switch(cmd)
621     {
622         case SW_HIDE:
623             if (!wasVisible) return FALSE;
624             showFlag = FALSE;
625             swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE;
626             if (hwnd != GetActiveWindow())
627                 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
628             break;
629
630         case SW_SHOWMINNOACTIVE:
631             swp |= SWP_NOACTIVATE | SWP_NOZORDER;
632             /* fall through */
633         case SW_MINIMIZE:
634         case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
635             if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
636             /* fall through */
637         case SW_SHOWMINIMIZED:
638             swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
639             swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
640             if ((style & WS_MINIMIZE) && wasVisible) return TRUE;
641             break;
642
643         case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
644             if (!wasVisible) swp |= SWP_SHOWWINDOW;
645             swp |= SWP_FRAMECHANGED;
646             swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
647             if ((style & WS_MAXIMIZE) && wasVisible) return TRUE;
648             break;
649
650         case SW_SHOWNA:
651             swp |= SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
652             if (style & WS_CHILD) swp |= SWP_NOZORDER;
653             break;
654         case SW_SHOW:
655             if (wasVisible) return TRUE;
656             swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
657             if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
658             break;
659
660         case SW_SHOWNOACTIVATE:
661             swp |= SWP_NOACTIVATE | SWP_NOZORDER;
662             /* fall through */
663         case SW_RESTORE:
664             /* fall through */
665         case SW_SHOWNORMAL:  /* same as SW_NORMAL: */
666         case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
667             if (!wasVisible) swp |= SWP_SHOWWINDOW;
668             if (style & (WS_MINIMIZE | WS_MAXIMIZE))
669             {
670                 swp |= SWP_FRAMECHANGED;
671                 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
672             }
673             else
674             {
675                 if (wasVisible) return TRUE;
676                 swp |= SWP_NOSIZE | SWP_NOMOVE;
677             }
678             if (style & WS_CHILD && !(swp & SWP_STATECHANGED)) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
679             break;
680     }
681
682     if ((showFlag != wasVisible || cmd == SW_SHOWNA) && cmd != SW_SHOWMAXIMIZED && !(swp & SWP_STATECHANGED))
683     {
684         SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
685         if (!IsWindow( hwnd )) return wasVisible;
686     }
687
688     parent = GetAncestor( hwnd, GA_PARENT );
689     if (parent && !IsWindowVisible( parent ) && !(swp & SWP_STATECHANGED))
690     {
691         /* if parent is not visible simply toggle WS_VISIBLE and return */
692         if (showFlag) WIN_SetStyle( hwnd, WS_VISIBLE, 0 );
693         else WIN_SetStyle( hwnd, 0, WS_VISIBLE );
694     }
695     else
696         SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
697                       newPos.right, newPos.bottom, LOWORD(swp) );
698
699     if (cmd == SW_HIDE)
700     {
701         HWND hFocus;
702
703         /* FIXME: This will cause the window to be activated irrespective
704          * of whether it is owned by the same thread. Has to be done
705          * asynchronously.
706          */
707
708         if (hwnd == GetActiveWindow())
709             WINPOS_ActivateOtherWindow(hwnd);
710
711         /* Revert focus to parent */
712         hFocus = GetFocus();
713         if (hwnd == hFocus || IsChild(hwnd, hFocus))
714         {
715             HWND parent = GetAncestor(hwnd, GA_PARENT);
716             if (parent == GetDesktopWindow()) parent = 0;
717             SetFocus(parent);
718         }
719     }
720
721     if (IsIconic(hwnd)) WINPOS_ShowIconTitle( hwnd, TRUE );
722
723     if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return wasVisible;
724
725     if (wndPtr->flags & WIN_NEED_SIZE)
726     {
727         /* should happen only in CreateWindowEx() */
728         int wParam = SIZE_RESTORED;
729         RECT client = wndPtr->rectClient;
730
731         wndPtr->flags &= ~WIN_NEED_SIZE;
732         if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
733         else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
734         WIN_ReleasePtr( wndPtr );
735
736         SendMessageW( hwnd, WM_SIZE, wParam,
737                       MAKELONG( client.right - client.left, client.bottom - client.top ));
738         SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( client.left, client.top ));
739     }
740     else WIN_ReleasePtr( wndPtr );
741
742     /* if previous state was minimized Windows sets focus to the window */
743     if (style & WS_MINIMIZE) SetFocus( hwnd );
744
745     return wasVisible;
746 }
747
748
749 /**********************************************************************
750  *              X11DRV_MapNotify
751  */
752 void X11DRV_MapNotify( HWND hwnd, XEvent *event )
753 {
754     struct x11drv_win_data *data;
755     HWND hwndFocus = GetFocus();
756     WND *win;
757
758     if (!(data = X11DRV_get_win_data( hwnd ))) return;
759
760     if (!(win = WIN_GetPtr( hwnd ))) return;
761
762     if (data->managed && (win->dwStyle & WS_VISIBLE) && (win->dwStyle & WS_MINIMIZE))
763     {
764         int x, y;
765         unsigned int width, height, border, depth;
766         Window root, top;
767         RECT rect;
768         LONG style = WS_VISIBLE;
769
770         /* FIXME: hack */
771         wine_tsx11_lock();
772         XGetGeometry( event->xmap.display, data->whole_window, &root, &x, &y, &width, &height,
773                         &border, &depth );
774         XTranslateCoordinates( event->xmap.display, data->whole_window, root, 0, 0, &x, &y, &top );
775         wine_tsx11_unlock();
776         rect.left   = x;
777         rect.top    = y;
778         rect.right  = x + width;
779         rect.bottom = y + height;
780         OffsetRect( &rect, virtual_screen_rect.left, virtual_screen_rect.top );
781         X11DRV_X_to_window_rect( data, &rect );
782
783         invalidate_dce( hwnd, &data->window_rect );
784
785         if (win->flags & WIN_RESTORE_MAX) style |= WS_MAXIMIZE;
786         WIN_SetStyle( hwnd, style, WS_MINIMIZE );
787         WIN_ReleasePtr( win );
788
789         SendMessageW( hwnd, WM_SHOWWINDOW, SW_RESTORE, 0 );
790         data->lock_changes++;
791         SetWindowPos( hwnd, 0, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
792                       SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED | SWP_STATECHANGED );
793         data->lock_changes--;
794     }
795     else WIN_ReleasePtr( win );
796     if (hwndFocus && IsChild( hwnd, hwndFocus )) X11DRV_SetFocus(hwndFocus);  /* FIXME */
797 }
798
799
800 /**********************************************************************
801  *              X11DRV_UnmapNotify
802  */
803 void X11DRV_UnmapNotify( HWND hwnd, XEvent *event )
804 {
805     struct x11drv_win_data *data;
806     WND *win;
807
808     if (!(data = X11DRV_get_win_data( hwnd ))) return;
809
810     if (!(win = WIN_GetPtr( hwnd ))) return;
811
812     if ((win->dwStyle & WS_VISIBLE) && data->managed &&
813         X11DRV_is_window_rect_mapped( &win->rectWindow ))
814     {
815         if (win->dwStyle & WS_MAXIMIZE)
816             win->flags |= WIN_RESTORE_MAX;
817         else if (!(win->dwStyle & WS_MINIMIZE))
818             win->flags &= ~WIN_RESTORE_MAX;
819
820         WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
821         WIN_ReleasePtr( win );
822
823         EndMenu();
824         SendMessageW( hwnd, WM_SHOWWINDOW, SW_MINIMIZE, 0 );
825         data->lock_changes++;
826         SetWindowPos( hwnd, 0, 0, 0, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON),
827                       SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_STATECHANGED );
828         data->lock_changes--;
829     }
830     else WIN_ReleasePtr( win );
831 }
832
833 struct desktop_resize_data
834 {
835     RECT  old_screen_rect;
836     RECT  old_virtual_rect;
837 };
838
839 static BOOL CALLBACK update_windows_on_desktop_resize( HWND hwnd, LPARAM lparam )
840 {
841     struct x11drv_win_data *data;
842     Display *display = thread_display();
843     struct desktop_resize_data *resize_data = (struct desktop_resize_data *)lparam;
844     int mask = 0;
845
846     if (!(data = X11DRV_get_win_data( hwnd ))) return TRUE;
847
848     if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)
849     {
850         BOOL new_fs_state;
851
852         if (fullscreen_state_changed( data, &data->client_rect, &resize_data->old_screen_rect, &new_fs_state ))
853             update_fullscreen_state( display, data->whole_window, new_fs_state );
854     }
855
856     if (resize_data->old_virtual_rect.left != virtual_screen_rect.left) mask |= CWX;
857     if (resize_data->old_virtual_rect.top != virtual_screen_rect.top) mask |= CWY;
858     if (mask && data->whole_window)
859     {
860         XWindowChanges changes;
861
862         wine_tsx11_lock();
863         changes.x = data->whole_rect.left - virtual_screen_rect.left;
864         changes.y = data->whole_rect.top - virtual_screen_rect.top;
865         XReconfigureWMWindow( display, data->whole_window,
866                               DefaultScreen(display), mask, &changes );
867         wine_tsx11_unlock();
868     }
869     return TRUE;
870 }
871
872
873 /***********************************************************************
874  *              X11DRV_handle_desktop_resize
875  */
876 void X11DRV_handle_desktop_resize( unsigned int width, unsigned int height )
877 {
878     HWND hwnd = GetDesktopWindow();
879     struct x11drv_win_data *data;
880     struct desktop_resize_data resize_data;
881
882     if (!(data = X11DRV_get_win_data( hwnd ))) return;
883
884     SetRect( &resize_data.old_screen_rect, 0, 0, screen_width, screen_height );
885     resize_data.old_virtual_rect = virtual_screen_rect;
886
887     screen_width  = width;
888     screen_height = height;
889     xinerama_init();
890     TRACE("desktop %p change to (%dx%d)\n", hwnd, width, height);
891     data->lock_changes++;
892     X11DRV_SetWindowPos( hwnd, 0, &virtual_screen_rect, &virtual_screen_rect,
893                          SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE, NULL );
894     data->lock_changes--;
895     ClipCursor(NULL);
896     SendMessageTimeoutW( HWND_BROADCAST, WM_DISPLAYCHANGE, screen_depth,
897                          MAKELPARAM( width, height ), SMTO_ABORTIFHUNG, 2000, NULL );
898
899     EnumWindows( update_windows_on_desktop_resize, (LPARAM)&resize_data );
900 }
901
902
903 /***********************************************************************
904  *              X11DRV_ConfigureNotify
905  */
906 void X11DRV_ConfigureNotify( HWND hwnd, XEvent *xev )
907 {
908     XConfigureEvent *event = &xev->xconfigure;
909     struct x11drv_win_data *data;
910     RECT rect;
911     UINT flags;
912     int cx, cy, x = event->x, y = event->y;
913
914     if (!hwnd) return;
915     if (!(data = X11DRV_get_win_data( hwnd ))) return;
916
917     /* Get geometry */
918
919     if (!event->send_event)  /* normal event, need to map coordinates to the root */
920     {
921         Window child;
922         wine_tsx11_lock();
923         XTranslateCoordinates( event->display, data->whole_window, root_window,
924                                0, 0, &x, &y, &child );
925         wine_tsx11_unlock();
926     }
927     rect.left   = x;
928     rect.top    = y;
929     rect.right  = x + event->width;
930     rect.bottom = y + event->height;
931     OffsetRect( &rect, virtual_screen_rect.left, virtual_screen_rect.top );
932     TRACE( "win %p new X rect %d,%d,%dx%d (event %d,%d,%dx%d)\n",
933            hwnd, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
934            event->x, event->y, event->width, event->height );
935     X11DRV_X_to_window_rect( data, &rect );
936
937     x     = rect.left;
938     y     = rect.top;
939     cx    = rect.right - rect.left;
940     cy    = rect.bottom - rect.top;
941     flags = SWP_NOACTIVATE | SWP_NOZORDER;
942
943     /* Compare what has changed */
944
945     GetWindowRect( hwnd, &rect );
946     if (rect.left == x && rect.top == y) flags |= SWP_NOMOVE;
947     else
948         TRACE( "%p moving from (%d,%d) to (%d,%d)\n",
949                hwnd, rect.left, rect.top, x, y );
950
951     if ((rect.right - rect.left == cx && rect.bottom - rect.top == cy) ||
952         IsIconic(hwnd) ||
953         (IsRectEmpty( &rect ) && event->width == 1 && event->height == 1))
954     {
955         if (flags & SWP_NOMOVE) return;  /* if nothing changed, don't do anything */
956         flags |= SWP_NOSIZE;
957     }
958     else
959         TRACE( "%p resizing from (%dx%d) to (%dx%d)\n",
960                hwnd, rect.right - rect.left, rect.bottom - rect.top, cx, cy );
961
962     data->lock_changes++;
963     SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
964     data->lock_changes--;
965 }
966
967
968 /***********************************************************************
969  *              SetWindowRgn  (X11DRV.@)
970  *
971  * Assign specified region to window (for non-rectangular windows)
972  */
973 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
974 {
975     struct x11drv_win_data *data;
976
977     if (!(data = X11DRV_get_win_data( hwnd )))
978     {
979         if (IsWindow( hwnd ))
980             FIXME( "not supported on other thread window %p\n", hwnd );
981         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
982         return FALSE;
983     }
984
985 #ifdef HAVE_LIBXSHAPE
986     if (data->whole_window)
987     {
988         Display *display = thread_display();
989
990         if (!hrgn)
991         {
992             wine_tsx11_lock();
993             XShapeCombineMask( display, data->whole_window,
994                                ShapeBounding, 0, 0, None, ShapeSet );
995             wine_tsx11_unlock();
996         }
997         else
998         {
999             RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
1000             if (pRegionData)
1001             {
1002                 wine_tsx11_lock();
1003                 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
1004                                          data->window_rect.left - data->whole_rect.left,
1005                                          data->window_rect.top - data->whole_rect.top,
1006                                          (XRectangle *)pRegionData->Buffer,
1007                                          pRegionData->rdh.nCount,
1008                                          ShapeSet, YXBanded );
1009                 wine_tsx11_unlock();
1010                 HeapFree(GetProcessHeap(), 0, pRegionData);
1011             }
1012         }
1013     }
1014 #endif  /* HAVE_LIBXSHAPE */
1015
1016     invalidate_dce( hwnd, &data->window_rect );
1017     return TRUE;
1018 }
1019
1020
1021 /***********************************************************************
1022  *           draw_moving_frame
1023  *
1024  * Draw the frame used when moving or resizing window.
1025  *
1026  * FIXME:  This causes problems in Win95 mode.  (why?)
1027  */
1028 static void draw_moving_frame( HDC hdc, RECT *rect, BOOL thickframe )
1029 {
1030     if (thickframe)
1031     {
1032         const int width = GetSystemMetrics(SM_CXFRAME);
1033         const int height = GetSystemMetrics(SM_CYFRAME);
1034
1035         HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
1036         PatBlt( hdc, rect->left, rect->top,
1037                 rect->right - rect->left - width, height, PATINVERT );
1038         PatBlt( hdc, rect->left, rect->top + height, width,
1039                 rect->bottom - rect->top - height, PATINVERT );
1040         PatBlt( hdc, rect->left + width, rect->bottom - 1,
1041                 rect->right - rect->left - width, -height, PATINVERT );
1042         PatBlt( hdc, rect->right - 1, rect->top, -width,
1043                 rect->bottom - rect->top - height, PATINVERT );
1044         SelectObject( hdc, hbrush );
1045     }
1046     else DrawFocusRect( hdc, rect );
1047 }
1048
1049
1050 /***********************************************************************
1051  *           start_size_move
1052  *
1053  * Initialisation of a move or resize, when initiatied from a menu choice.
1054  * Return hit test code for caption or sizing border.
1055  */
1056 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
1057 {
1058     LONG hittest = 0;
1059     POINT pt;
1060     MSG msg;
1061     RECT rectWindow;
1062
1063     GetWindowRect( hwnd, &rectWindow );
1064
1065     if ((wParam & 0xfff0) == SC_MOVE)
1066     {
1067         /* Move pointer at the center of the caption */
1068         RECT rect = rectWindow;
1069         /* Note: to be exactly centered we should take the different types
1070          * of border into account, but it shouldn't make more that a few pixels
1071          * of difference so let's not bother with that */
1072         rect.top += GetSystemMetrics(SM_CYBORDER);
1073         if (style & WS_SYSMENU)
1074             rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
1075         if (style & WS_MINIMIZEBOX)
1076             rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1077         if (style & WS_MAXIMIZEBOX)
1078             rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1079         pt.x = (rect.right + rect.left) / 2;
1080         pt.y = rect.top + GetSystemMetrics(SM_CYSIZE)/2;
1081         hittest = HTCAPTION;
1082         *capturePoint = pt;
1083     }
1084     else  /* SC_SIZE */
1085     {
1086         pt.x = pt.y = 0;
1087         while(!hittest)
1088         {
1089             GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST );
1090             if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1091
1092             switch(msg.message)
1093             {
1094             case WM_MOUSEMOVE:
1095                 pt = msg.pt;
1096                 hittest = SendMessageW( hwnd, WM_NCHITTEST, 0, MAKELONG( pt.x, pt.y ) );
1097                 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT)) hittest = 0;
1098                 break;
1099
1100             case WM_LBUTTONUP:
1101                 return 0;
1102
1103             case WM_KEYDOWN:
1104                 switch(msg.wParam)
1105                 {
1106                 case VK_UP:
1107                     hittest = HTTOP;
1108                     pt.x =(rectWindow.left+rectWindow.right)/2;
1109                     pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
1110                     break;
1111                 case VK_DOWN:
1112                     hittest = HTBOTTOM;
1113                     pt.x =(rectWindow.left+rectWindow.right)/2;
1114                     pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
1115                     break;
1116                 case VK_LEFT:
1117                     hittest = HTLEFT;
1118                     pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
1119                     pt.y =(rectWindow.top+rectWindow.bottom)/2;
1120                     break;
1121                 case VK_RIGHT:
1122                     hittest = HTRIGHT;
1123                     pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
1124                     pt.y =(rectWindow.top+rectWindow.bottom)/2;
1125                     break;
1126                 case VK_RETURN:
1127                 case VK_ESCAPE: return 0;
1128                 }
1129             }
1130         }
1131         *capturePoint = pt;
1132     }
1133     SetCursorPos( pt.x, pt.y );
1134     SendMessageW( hwnd, WM_SETCURSOR, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
1135     return hittest;
1136 }
1137
1138
1139 /***********************************************************************
1140  *           set_movesize_capture
1141  */
1142 static void set_movesize_capture( HWND hwnd )
1143 {
1144     HWND previous = 0;
1145
1146     SERVER_START_REQ( set_capture_window )
1147     {
1148         req->handle = hwnd;
1149         req->flags  = CAPTURE_MOVESIZE;
1150         if (!wine_server_call_err( req ))
1151         {
1152             previous = reply->previous;
1153             hwnd = reply->full_handle;
1154         }
1155     }
1156     SERVER_END_REQ;
1157     if (previous && previous != hwnd)
1158         SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
1159 }
1160
1161 /***********************************************************************
1162  *           X11DRV_WMMoveResizeWindow
1163  *
1164  * Tells the window manager to initiate a move or resize operation.
1165  *
1166  * SEE
1167  *  http://freedesktop.org/Standards/wm-spec/1.3/ar01s04.html
1168  *  or search for "_NET_WM_MOVERESIZE"
1169  */
1170 static BOOL X11DRV_WMMoveResizeWindow( HWND hwnd, int x, int y, WPARAM wparam )
1171 {
1172     WPARAM syscommand = wparam & 0xfff0;
1173     WPARAM hittest = wparam & 0x0f;
1174     int dir;
1175     XEvent xev;
1176     Display *display = thread_display();
1177
1178     if (syscommand == SC_MOVE)
1179     {
1180         if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
1181         else dir = _NET_WM_MOVERESIZE_MOVE;
1182     }
1183     else
1184     {
1185         /* windows without WS_THICKFRAME are not resizable through the window manager */
1186         if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_THICKFRAME)) return FALSE;
1187
1188         switch (hittest)
1189         {
1190         case WMSZ_LEFT:        dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
1191         case WMSZ_RIGHT:       dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
1192         case WMSZ_TOP:         dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
1193         case WMSZ_TOPLEFT:     dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
1194         case WMSZ_TOPRIGHT:    dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
1195         case WMSZ_BOTTOM:      dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
1196         case WMSZ_BOTTOMLEFT:  dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
1197         case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
1198         default:               dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD; break;
1199         }
1200     }
1201
1202     TRACE("hwnd %p, x %d, y %d, dir %d\n", hwnd, x, y, dir);
1203
1204     xev.xclient.type = ClientMessage;
1205     xev.xclient.window = X11DRV_get_whole_window(hwnd);
1206     xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
1207     xev.xclient.serial = 0;
1208     xev.xclient.display = display;
1209     xev.xclient.send_event = True;
1210     xev.xclient.format = 32;
1211     xev.xclient.data.l[0] = x; /* x coord */
1212     xev.xclient.data.l[1] = y; /* y coord */
1213     xev.xclient.data.l[2] = dir; /* direction */
1214     xev.xclient.data.l[3] = 1; /* button */
1215     xev.xclient.data.l[4] = 0; /* unused */
1216
1217     /* need to ungrab the pointer that may have been automatically grabbed
1218      * with a ButtonPress event */
1219     wine_tsx11_lock();
1220     XUngrabPointer( display, CurrentTime );
1221     XSendEvent(display, root_window, False, SubstructureNotifyMask, &xev);
1222     wine_tsx11_unlock();
1223     return TRUE;
1224 }
1225
1226 /***********************************************************************
1227  *           SysCommandSizeMove   (X11DRV.@)
1228  *
1229  * Perform SC_MOVE and SC_SIZE commands.
1230  */
1231 void X11DRV_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
1232 {
1233     MSG msg;
1234     RECT sizingRect, mouseRect, origRect;
1235     HDC hdc;
1236     HWND parent;
1237     LONG hittest = (LONG)(wParam & 0x0f);
1238     WPARAM syscommand = wParam & 0xfff0;
1239     HCURSOR hDragCursor = 0, hOldCursor = 0;
1240     POINT minTrack, maxTrack;
1241     POINT capturePoint, pt;
1242     LONG style = GetWindowLongA( hwnd, GWL_STYLE );
1243     BOOL    thickframe = HAS_THICKFRAME( style );
1244     BOOL    iconic = style & WS_MINIMIZE;
1245     BOOL    moved = FALSE;
1246     DWORD     dwPoint = GetMessagePos ();
1247     BOOL DragFullWindows = TRUE;
1248     Window parent_win, whole_win;
1249     struct x11drv_thread_data *thread_data = x11drv_thread_data();
1250     struct x11drv_win_data *data;
1251
1252     pt.x = (short)LOWORD(dwPoint);
1253     pt.y = (short)HIWORD(dwPoint);
1254     capturePoint = pt;
1255
1256     if (IsZoomed(hwnd) || !IsWindowVisible(hwnd)) return;
1257
1258     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1259
1260     TRACE("hwnd %p (%smanaged), command %04lx, hittest %d, pos %d,%d\n",
1261           hwnd, data->managed ? "" : "NOT ", syscommand, hittest, pt.x, pt.y);
1262
1263     /* if we are managed then we let the WM do all the work */
1264     if (data->managed && X11DRV_WMMoveResizeWindow( hwnd, pt.x, pt.y, wParam )) return;
1265
1266     if (syscommand == SC_MOVE)
1267     {
1268         if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1269         if (!hittest) return;
1270     }
1271     else  /* SC_SIZE */
1272     {
1273         if ( hittest && (syscommand != SC_MOUSEMENU) )
1274             hittest += (HTLEFT - WMSZ_LEFT);
1275         else
1276         {
1277             set_movesize_capture( hwnd );
1278             hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1279             if (!hittest)
1280             {
1281                 set_movesize_capture(0);
1282                 return;
1283             }
1284         }
1285     }
1286
1287       /* Get min/max info */
1288
1289     WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1290     GetWindowRect( hwnd, &sizingRect );
1291     if (style & WS_CHILD)
1292     {
1293         parent = GetParent(hwnd);
1294         /* make sizing rect relative to parent */
1295         MapWindowPoints( 0, parent, (POINT*)&sizingRect, 2 );
1296         GetClientRect( parent, &mouseRect );
1297     }
1298     else
1299     {
1300         parent = 0;
1301         mouseRect = virtual_screen_rect;
1302     }
1303     origRect = sizingRect;
1304
1305     if (ON_LEFT_BORDER(hittest))
1306     {
1307         mouseRect.left  = max( mouseRect.left, sizingRect.right-maxTrack.x );
1308         mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
1309     }
1310     else if (ON_RIGHT_BORDER(hittest))
1311     {
1312         mouseRect.left  = max( mouseRect.left, sizingRect.left+minTrack.x );
1313         mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
1314     }
1315     if (ON_TOP_BORDER(hittest))
1316     {
1317         mouseRect.top    = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
1318         mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
1319     }
1320     else if (ON_BOTTOM_BORDER(hittest))
1321     {
1322         mouseRect.top    = max( mouseRect.top, sizingRect.top+minTrack.y );
1323         mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
1324     }
1325     if (parent) MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
1326
1327     /* Retrieve a default cache DC (without using the window style) */
1328     hdc = GetDCEx( parent, 0, DCX_CACHE );
1329
1330     if( iconic ) /* create a cursor for dragging */
1331     {
1332         hDragCursor = (HCURSOR)GetClassLongPtrW( hwnd, GCLP_HICON);
1333         if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageW( hwnd, WM_QUERYDRAGICON, 0, 0L);
1334         if( !hDragCursor ) iconic = FALSE;
1335     }
1336
1337     /* repaint the window before moving it around */
1338     RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1339
1340     SendMessageW( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
1341     set_movesize_capture( hwnd );
1342
1343     /* we only allow disabling the full window drag for child windows, or in desktop mode */
1344     /* otherwise we'd need to grab the server and we want to avoid that */
1345     if (parent || (root_window != DefaultRootWindow(gdi_display)))
1346         SystemParametersInfoA(SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0);
1347
1348     whole_win = X11DRV_get_whole_window( GetAncestor(hwnd,GA_ROOT) );
1349     parent_win = parent ? X11DRV_get_whole_window( GetAncestor(parent,GA_ROOT) ) : root_window;
1350
1351     wine_tsx11_lock();
1352     XGrabPointer( thread_data->display, whole_win, False,
1353                   PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1354                   GrabModeAsync, GrabModeAsync, parent_win, None, CurrentTime );
1355     wine_tsx11_unlock();
1356     thread_data->grab_window = whole_win;
1357
1358     while(1)
1359     {
1360         int dx = 0, dy = 0;
1361
1362         if (!GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST )) break;
1363         if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1364
1365         /* Exit on button-up, Return, or Esc */
1366         if ((msg.message == WM_LBUTTONUP) ||
1367             ((msg.message == WM_KEYDOWN) &&
1368              ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
1369
1370         if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
1371             continue;  /* We are not interested in other messages */
1372
1373         pt = msg.pt;
1374
1375         if (msg.message == WM_KEYDOWN) switch(msg.wParam)
1376         {
1377         case VK_UP:    pt.y -= 8; break;
1378         case VK_DOWN:  pt.y += 8; break;
1379         case VK_LEFT:  pt.x -= 8; break;
1380         case VK_RIGHT: pt.x += 8; break;
1381         }
1382
1383         pt.x = max( pt.x, mouseRect.left );
1384         pt.x = min( pt.x, mouseRect.right );
1385         pt.y = max( pt.y, mouseRect.top );
1386         pt.y = min( pt.y, mouseRect.bottom );
1387
1388         dx = pt.x - capturePoint.x;
1389         dy = pt.y - capturePoint.y;
1390
1391         if (dx || dy)
1392         {
1393             if( !moved )
1394             {
1395                 moved = TRUE;
1396
1397                 if( iconic ) /* ok, no system popup tracking */
1398                 {
1399                     hOldCursor = SetCursor(hDragCursor);
1400                     ShowCursor( TRUE );
1401                     WINPOS_ShowIconTitle( hwnd, FALSE );
1402                 }
1403                 else if(!DragFullWindows)
1404                     draw_moving_frame( hdc, &sizingRect, thickframe );
1405             }
1406
1407             if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
1408             else
1409             {
1410                 RECT newRect = sizingRect;
1411                 WPARAM wpSizingHit = 0;
1412
1413                 if (hittest == HTCAPTION) OffsetRect( &newRect, dx, dy );
1414                 if (ON_LEFT_BORDER(hittest)) newRect.left += dx;
1415                 else if (ON_RIGHT_BORDER(hittest)) newRect.right += dx;
1416                 if (ON_TOP_BORDER(hittest)) newRect.top += dy;
1417                 else if (ON_BOTTOM_BORDER(hittest)) newRect.bottom += dy;
1418                 if(!iconic && !DragFullWindows) draw_moving_frame( hdc, &sizingRect, thickframe );
1419                 capturePoint = pt;
1420
1421                 /* determine the hit location */
1422                 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
1423                     wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
1424                 SendMessageW( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&newRect );
1425
1426                 if (!iconic)
1427                 {
1428                     if(!DragFullWindows)
1429                         draw_moving_frame( hdc, &newRect, thickframe );
1430                     else
1431                         SetWindowPos( hwnd, 0, newRect.left, newRect.top,
1432                                       newRect.right - newRect.left,
1433                                       newRect.bottom - newRect.top,
1434                                       ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1435                 }
1436                 sizingRect = newRect;
1437             }
1438         }
1439     }
1440
1441     set_movesize_capture(0);
1442     if( iconic )
1443     {
1444         if( moved ) /* restore cursors, show icon title later on */
1445         {
1446             ShowCursor( FALSE );
1447             SetCursor( hOldCursor );
1448         }
1449     }
1450     else if (moved && !DragFullWindows)
1451         draw_moving_frame( hdc, &sizingRect, thickframe );
1452
1453     ReleaseDC( parent, hdc );
1454
1455     wine_tsx11_lock();
1456     XUngrabPointer( thread_data->display, CurrentTime );
1457     wine_tsx11_unlock();
1458     thread_data->grab_window = None;
1459
1460     if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
1461         moved = FALSE;
1462
1463     SendMessageW( hwnd, WM_EXITSIZEMOVE, 0, 0 );
1464     SendMessageW( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
1465
1466     /* window moved or resized */
1467     if (moved)
1468     {
1469         /* if the moving/resizing isn't canceled call SetWindowPos
1470          * with the new position or the new size of the window
1471          */
1472         if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
1473         {
1474             /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
1475             if(!DragFullWindows)
1476                 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
1477                               sizingRect.right - sizingRect.left,
1478                               sizingRect.bottom - sizingRect.top,
1479                               ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1480         }
1481         else
1482         { /* restore previous size/position */
1483             if(DragFullWindows)
1484                 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
1485                               origRect.right - origRect.left,
1486                               origRect.bottom - origRect.top,
1487                               ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1488         }
1489     }
1490
1491     if (IsIconic(hwnd))
1492     {
1493         /* Single click brings up the system menu when iconized */
1494
1495         if( !moved )
1496         {
1497             if(style & WS_SYSMENU )
1498                 SendMessageW( hwnd, WM_SYSCOMMAND,
1499                               SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
1500         }
1501         else WINPOS_ShowIconTitle( hwnd, TRUE );
1502     }
1503 }