winex11: glXQueryExtension has its error and event base arguments swapped
[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 "wownt32.h"
37 #include "wine/wingdi16.h"
38
39 #include "x11drv.h"
40 #include "win.h"
41 #include "winpos.h"
42
43 #include "wine/server.h"
44 #include "wine/debug.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
47
48 #define SWP_AGG_NOPOSCHANGE \
49     (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
50 #define SWP_AGG_STATUSFLAGS \
51     (SWP_AGG_NOPOSCHANGE | SWP_FRAMECHANGED | SWP_HIDEWINDOW | SWP_SHOWWINDOW)
52
53 #define SWP_EX_NOCOPY       0x0001
54 #define SWP_EX_PAINTSELF    0x0002
55 #define SWP_EX_NONCLIENT    0x0004
56
57 #define HAS_THICKFRAME(style) \
58     (((style) & WS_THICKFRAME) && \
59      !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
60
61 #define ON_LEFT_BORDER(hit) \
62  (((hit) == HTLEFT) || ((hit) == HTTOPLEFT) || ((hit) == HTBOTTOMLEFT))
63 #define ON_RIGHT_BORDER(hit) \
64  (((hit) == HTRIGHT) || ((hit) == HTTOPRIGHT) || ((hit) == HTBOTTOMRIGHT))
65 #define ON_TOP_BORDER(hit) \
66  (((hit) == HTTOP) || ((hit) == HTTOPLEFT) || ((hit) == HTTOPRIGHT))
67 #define ON_BOTTOM_BORDER(hit) \
68  (((hit) == HTBOTTOM) || ((hit) == HTBOTTOMLEFT) || ((hit) == HTBOTTOMRIGHT))
69
70 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT      0
71 #define _NET_WM_MOVERESIZE_SIZE_TOP          1
72 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT     2
73 #define _NET_WM_MOVERESIZE_SIZE_RIGHT        3
74 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT  4
75 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM       5
76 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT   6
77 #define _NET_WM_MOVERESIZE_SIZE_LEFT         7
78 #define _NET_WM_MOVERESIZE_MOVE              8   /* movement only */
79 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD     9   /* size via keyboard */
80 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD    10   /* move via keyboard */
81
82 #define _NET_WM_STATE_REMOVE  0
83 #define _NET_WM_STATE_ADD     1
84 #define _NET_WM_STATE_TOGGLE  2
85
86 /***********************************************************************
87  *           X11DRV_Expose
88  */
89 void X11DRV_Expose( HWND hwnd, XEvent *xev )
90 {
91     XExposeEvent *event = &xev->xexpose;
92     RECT rect;
93     struct x11drv_win_data *data;
94     int flags = RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN;
95
96     TRACE( "win %p (%lx) %d,%d %dx%d\n",
97            hwnd, event->window, event->x, event->y, event->width, event->height );
98
99     if (!(data = X11DRV_get_win_data( hwnd ))) return;
100
101     rect.left   = event->x;
102     rect.top    = event->y;
103     rect.right  = rect.left + event->width;
104     rect.bottom = rect.top + event->height;
105
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;
110
111     SERVER_START_REQ( update_window_zorder )
112     {
113         req->window      = hwnd;
114         req->rect.left   = rect.left + data->whole_rect.left;
115         req->rect.top    = rect.top + data->whole_rect.top;
116         req->rect.right  = rect.right + data->whole_rect.left;
117         req->rect.bottom = rect.bottom + data->whole_rect.top;
118         wine_server_call( req );
119     }
120     SERVER_END_REQ;
121
122     /* make position relative to client area instead of window */
123     OffsetRect( &rect, -data->client_rect.left, -data->client_rect.top );
124     RedrawWindow( hwnd, &rect, 0, flags );
125 }
126
127
128 /***********************************************************************
129  *           SWP_DoWinPosChanging
130  */
131 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
132 {
133     WND *wndPtr;
134
135     /* Send WM_WINDOWPOSCHANGING message */
136
137     if (!(pWinpos->flags & SWP_NOSENDCHANGING))
138         SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
139
140     if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) ||
141         wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
142
143     /* Calculate new position and size */
144
145     *pNewWindowRect = wndPtr->rectWindow;
146     *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
147                                                     : wndPtr->rectClient;
148
149     if (!(pWinpos->flags & SWP_NOSIZE))
150     {
151         pNewWindowRect->right  = pNewWindowRect->left + pWinpos->cx;
152         pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
153     }
154     if (!(pWinpos->flags & SWP_NOMOVE))
155     {
156         pNewWindowRect->left    = pWinpos->x;
157         pNewWindowRect->top     = pWinpos->y;
158         pNewWindowRect->right  += pWinpos->x - wndPtr->rectWindow.left;
159         pNewWindowRect->bottom += pWinpos->y - wndPtr->rectWindow.top;
160
161         OffsetRect( pNewClientRect, pWinpos->x - wndPtr->rectWindow.left,
162                                     pWinpos->y - wndPtr->rectWindow.top );
163     }
164     pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
165
166     TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
167            pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
168            pWinpos->cx, pWinpos->cy, pWinpos->flags );
169     TRACE( "current %s style %08lx new %s\n",
170            wine_dbgstr_rect( &wndPtr->rectWindow ), wndPtr->dwStyle,
171            wine_dbgstr_rect( pNewWindowRect ));
172
173     WIN_ReleasePtr( wndPtr );
174     return TRUE;
175 }
176
177
178 /***********************************************************************
179  *           get_valid_rects
180  *
181  * Compute the valid rects from the old and new client rect and WVR_* flags.
182  * Helper for WM_NCCALCSIZE handling.
183  */
184 static inline void get_valid_rects( const RECT *old_client, const RECT *new_client, UINT flags,
185                                     RECT *valid )
186 {
187     int cx, cy;
188
189     if (flags & WVR_REDRAW)
190     {
191         SetRectEmpty( &valid[0] );
192         SetRectEmpty( &valid[1] );
193         return;
194     }
195
196     if (flags & WVR_VALIDRECTS)
197     {
198         if (!IntersectRect( &valid[0], &valid[0], new_client ) ||
199             !IntersectRect( &valid[1], &valid[1], old_client ))
200         {
201             SetRectEmpty( &valid[0] );
202             SetRectEmpty( &valid[1] );
203             return;
204         }
205         flags = WVR_ALIGNLEFT | WVR_ALIGNTOP;
206     }
207     else
208     {
209         valid[0] = *new_client;
210         valid[1] = *old_client;
211     }
212
213     /* make sure the rectangles have the same size */
214     cx = min( valid[0].right - valid[0].left, valid[1].right - valid[1].left );
215     cy = min( valid[0].bottom - valid[0].top, valid[1].bottom - valid[1].top );
216
217     if (flags & WVR_ALIGNBOTTOM)
218     {
219         valid[0].top = valid[0].bottom - cy;
220         valid[1].top = valid[1].bottom - cy;
221     }
222     else
223     {
224         valid[0].bottom = valid[0].top + cy;
225         valid[1].bottom = valid[1].top + cy;
226     }
227     if (flags & WVR_ALIGNRIGHT)
228     {
229         valid[0].left = valid[0].right - cx;
230         valid[1].left = valid[1].right - cx;
231     }
232     else
233     {
234         valid[0].right = valid[0].left + cx;
235         valid[1].right = valid[1].left + cx;
236     }
237 }
238
239
240 /***********************************************************************
241  *           SWP_DoNCCalcSize
242  */
243 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect,
244                               RECT *validRects )
245 {
246     UINT wvrFlags = 0;
247     WND *wndPtr;
248
249     if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
250
251       /* Send WM_NCCALCSIZE message to get new client area */
252     if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
253     {
254         NCCALCSIZE_PARAMS params;
255         WINDOWPOS winposCopy;
256
257         params.rgrc[0] = *pNewWindowRect;
258         params.rgrc[1] = wndPtr->rectWindow;
259         params.rgrc[2] = wndPtr->rectClient;
260         params.lppos = &winposCopy;
261         winposCopy = *pWinpos;
262         WIN_ReleasePtr( wndPtr );
263
264         wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
265
266         *pNewClientRect = params.rgrc[0];
267
268         if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
269
270         TRACE( "hwnd %p old win %s old client %s new win %s new client %s\n", pWinpos->hwnd,
271                wine_dbgstr_rect(&wndPtr->rectWindow), wine_dbgstr_rect(&wndPtr->rectClient),
272                wine_dbgstr_rect(pNewWindowRect), wine_dbgstr_rect(pNewClientRect) );
273
274         if( pNewClientRect->left != wndPtr->rectClient.left ||
275             pNewClientRect->top != wndPtr->rectClient.top )
276             pWinpos->flags &= ~SWP_NOCLIENTMOVE;
277
278         if( (pNewClientRect->right - pNewClientRect->left !=
279              wndPtr->rectClient.right - wndPtr->rectClient.left))
280             pWinpos->flags &= ~SWP_NOCLIENTSIZE;
281         else
282             wvrFlags &= ~WVR_HREDRAW;
283
284         if (pNewClientRect->bottom - pNewClientRect->top !=
285              wndPtr->rectClient.bottom - wndPtr->rectClient.top)
286             pWinpos->flags &= ~SWP_NOCLIENTSIZE;
287         else
288             wvrFlags &= ~WVR_VREDRAW;
289
290         validRects[0] = params.rgrc[1];
291         validRects[1] = params.rgrc[2];
292     }
293     else
294     {
295         if (!(pWinpos->flags & SWP_NOMOVE) &&
296             (pNewClientRect->left != wndPtr->rectClient.left ||
297              pNewClientRect->top != wndPtr->rectClient.top))
298             pWinpos->flags &= ~SWP_NOCLIENTMOVE;
299     }
300
301     if (pWinpos->flags & (SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
302     {
303         SetRectEmpty( &validRects[0] );
304         SetRectEmpty( &validRects[1] );
305     }
306     else get_valid_rects( &wndPtr->rectClient, pNewClientRect, wvrFlags, validRects );
307
308     WIN_ReleasePtr( wndPtr );
309     return wvrFlags;
310 }
311
312
313 struct move_owned_info
314 {
315     HWND owner;
316     HWND insert_after;
317 };
318
319 static BOOL CALLBACK move_owned_popups( HWND hwnd, LPARAM lparam )
320 {
321     struct move_owned_info *info = (struct move_owned_info *)lparam;
322
323     if (hwnd == info->owner) return FALSE;
324     if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) &&
325         GetWindow( hwnd, GW_OWNER ) == info->owner)
326     {
327         SetWindowPos( hwnd, info->insert_after, 0, 0, 0, 0,
328                       SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE |
329                       SWP_NOSENDCHANGING | SWP_DEFERERASE );
330         info->insert_after = hwnd;
331     }
332     return TRUE;
333 }
334
335 /***********************************************************************
336  *           SWP_DoOwnedPopups
337  *
338  * fix Z order taking into account owned popups -
339  * basically we need to maintain them above the window that owns them
340  *
341  * FIXME: hide/show owned popups when owner visibility changes.
342  */
343 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
344 {
345     HWND owner = GetWindow( hwnd, GW_OWNER );
346     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
347     struct move_owned_info info;
348
349     TRACE("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
350
351     if ((style & WS_POPUP) && owner)
352     {
353         /* make sure this popup stays above the owner */
354
355         if( hwndInsertAfter != HWND_TOP )
356         {
357             HWND hwndLocalPrev = HWND_TOP;
358             HWND prev = GetWindow( owner, GW_HWNDPREV );
359
360             while (prev && prev != hwndInsertAfter)
361             {
362                 if (hwndLocalPrev == HWND_TOP && GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE)
363                     hwndLocalPrev = prev;
364                 prev = GetWindow( prev, GW_HWNDPREV );
365             }
366             if (!prev) hwndInsertAfter = hwndLocalPrev;
367         }
368     }
369     else if (style & WS_CHILD) return hwndInsertAfter;
370
371     info.owner = hwnd;
372     info.insert_after = hwndInsertAfter;
373     EnumWindows( move_owned_popups, (LPARAM)&info );
374     return info.insert_after;
375 }
376
377
378 /* fix redundant flags and values in the WINDOWPOS structure */
379 static BOOL fixup_flags( WINDOWPOS *winpos )
380 {
381     HWND parent;
382     WND *wndPtr = WIN_GetPtr( winpos->hwnd );
383     BOOL ret = TRUE;
384
385     if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
386     {
387         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
388         return FALSE;
389     }
390     winpos->hwnd = wndPtr->hwndSelf;  /* make it a full handle */
391
392     /* Finally make sure that all coordinates are valid */
393     if (winpos->x < -32768) winpos->x = -32768;
394     else if (winpos->x > 32767) winpos->x = 32767;
395     if (winpos->y < -32768) winpos->y = -32768;
396     else if (winpos->y > 32767) winpos->y = 32767;
397
398     if (winpos->cx < 0) winpos->cx = 0;
399     else if (winpos->cx > 32767) winpos->cx = 32767;
400     if (winpos->cy < 0) winpos->cy = 0;
401     else if (winpos->cy > 32767) winpos->cy = 32767;
402
403     parent = GetAncestor( winpos->hwnd, GA_PARENT );
404     if (!IsWindowVisible( parent )) winpos->flags |= SWP_NOREDRAW;
405
406     if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
407     else
408     {
409         winpos->flags &= ~SWP_HIDEWINDOW;
410         if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
411     }
412
413     if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
414         (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
415         winpos->flags |= SWP_NOSIZE;    /* Already the right size */
416
417     if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
418         winpos->flags |= SWP_NOMOVE;    /* Already the right position */
419
420     if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
421     {
422         if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) /* Bring to the top when activating */
423         {
424             winpos->flags &= ~SWP_NOZORDER;
425             winpos->hwndInsertAfter = HWND_TOP;
426         }
427     }
428
429     /* Check hwndInsertAfter */
430     if (winpos->flags & SWP_NOZORDER) goto done;
431
432     /* fix sign extension */
433     if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
434     else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
435
436       /* FIXME: TOPMOST not supported yet */
437     if ((winpos->hwndInsertAfter == HWND_TOPMOST) ||
438         (winpos->hwndInsertAfter == HWND_NOTOPMOST)) winpos->hwndInsertAfter = HWND_TOP;
439
440     /* hwndInsertAfter must be a sibling of the window */
441     if (winpos->hwndInsertAfter == HWND_TOP)
442     {
443         if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
444             winpos->flags |= SWP_NOZORDER;
445     }
446     else if (winpos->hwndInsertAfter == HWND_BOTTOM)
447     {
448         if (GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
449             winpos->flags |= SWP_NOZORDER;
450     }
451     else
452     {
453         if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != parent) ret = FALSE;
454         else
455         {
456             /* don't need to change the Zorder of hwnd if it's already inserted
457              * after hwndInsertAfter or when inserting hwnd after itself.
458              */
459             if ((winpos->hwnd == winpos->hwndInsertAfter) ||
460                 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
461                 winpos->flags |= SWP_NOZORDER;
462         }
463     }
464  done:
465     WIN_ReleasePtr( wndPtr );
466     return ret;
467 }
468
469
470 /***********************************************************************
471  *              SetWindowStyle   (X11DRV.@)
472  *
473  * Update the X state of a window to reflect a style change
474  */
475 void X11DRV_SetWindowStyle( HWND hwnd, DWORD old_style )
476 {
477     Display *display = thread_display();
478     struct x11drv_win_data *data;
479     DWORD new_style, changed;
480
481     if (hwnd == GetDesktopWindow()) return;
482     if (!(data = X11DRV_get_win_data( hwnd ))) return;
483
484     new_style = GetWindowLongW( hwnd, GWL_STYLE );
485     changed = new_style ^ old_style;
486
487     if (changed & WS_VISIBLE)
488     {
489         if (data->whole_window && X11DRV_is_window_rect_mapped( &data->window_rect ))
490         {
491             if (new_style & WS_VISIBLE)
492             {
493                 TRACE( "mapping win %p\n", hwnd );
494                 X11DRV_sync_window_style( display, data );
495                 X11DRV_set_wm_hints( display, data );
496                 wine_tsx11_lock();
497                 XMapWindow( display, data->whole_window );
498                 wine_tsx11_unlock();
499             }
500             /* we don't unmap windows, that causes trouble with the window manager */
501         }
502         invalidate_dce( hwnd, &data->window_rect );
503     }
504
505     if (changed & WS_DISABLED)
506     {
507         if (data->whole_window && data->managed)
508         {
509             XWMHints *wm_hints;
510             wine_tsx11_lock();
511             if (!(wm_hints = XGetWMHints( display, data->whole_window )))
512                 wm_hints = XAllocWMHints();
513             if (wm_hints)
514             {
515                 wm_hints->flags |= InputHint;
516                 wm_hints->input = !(new_style & WS_DISABLED);
517                 XSetWMHints( display, data->whole_window, wm_hints );
518                 XFree(wm_hints);
519             }
520             wine_tsx11_unlock();
521         }
522     }
523 }
524
525
526 /***********************************************************************
527  *     update_fullscreen_state
528  *
529  * Use the NETWM protocol to set the fullscreen state.
530  * This only works for mapped windows.
531  */
532 static void update_fullscreen_state( Display *display, struct x11drv_win_data *data,
533                                      const RECT *old_client_rect )
534 {
535     XEvent xev;
536     BOOL old_fs_state = FALSE, new_fs_state = FALSE;
537
538     if (old_client_rect->left <= 0 && old_client_rect->right >= screen_width &&
539         old_client_rect->top <= 0 && old_client_rect->bottom >= screen_height)
540         old_fs_state = TRUE;
541
542     if (data->client_rect.left <= 0 && data->client_rect.right >= screen_width &&
543         data->client_rect.top <= 0 && data->client_rect.bottom >= screen_height)
544         new_fs_state = TRUE;
545
546     if (new_fs_state == old_fs_state) return;
547
548     TRACE("setting fullscreen state for hwnd %p to %s\n", data->hwnd, new_fs_state ? "true" : "false");
549
550     if (data->whole_window)
551     {
552         xev.xclient.type = ClientMessage;
553         xev.xclient.window = data->whole_window;
554         xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
555         xev.xclient.serial = 0;
556         xev.xclient.display = display;
557         xev.xclient.send_event = True;
558         xev.xclient.format = 32;
559         xev.xclient.data.l[0] = new_fs_state ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
560         xev.xclient.data.l[1] = x11drv_atom(_NET_WM_STATE_FULLSCREEN);
561         xev.xclient.data.l[2] = 0;
562         wine_tsx11_lock();
563         XSendEvent(display, root_window, False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
564         wine_tsx11_unlock();
565     }
566 }
567
568
569 /***********************************************************************
570  *           X11DRV_set_window_pos
571  *
572  * Set a window position and Z order.
573  */
574 BOOL X11DRV_set_window_pos( HWND hwnd, HWND insert_after, const RECT *rectWindow,
575                             const RECT *rectClient, UINT swp_flags, const RECT *valid_rects )
576 {
577     struct x11drv_win_data *data;
578     RECT new_whole_rect, old_client_rect;
579     WND *win;
580     DWORD old_style, new_style;
581     BOOL ret;
582
583     if (!(data = X11DRV_get_win_data( hwnd ))) return FALSE;
584
585     new_whole_rect = *rectWindow;
586     X11DRV_window_to_X_rect( data, &new_whole_rect );
587
588     old_client_rect = data->client_rect;
589
590     if (!IsRectEmpty( &valid_rects[0] ))
591     {
592         int x_offset = 0, y_offset = 0;
593
594         if (data->whole_window)
595         {
596             /* the X server will move the bits for us */
597             x_offset = data->whole_rect.left - new_whole_rect.left;
598             y_offset = data->whole_rect.top - new_whole_rect.top;
599         }
600
601         if (x_offset != valid_rects[1].left - valid_rects[0].left ||
602             y_offset != valid_rects[1].top - valid_rects[0].top)
603         {
604             /* FIXME: should copy the window bits here */
605             valid_rects = NULL;
606         }
607     }
608
609     if (!(win = WIN_GetPtr( hwnd ))) return FALSE;
610     if (win == WND_OTHER_PROCESS)
611     {
612         if (IsWindow( hwnd )) ERR( "cannot set rectangles of other process window %p\n", hwnd );
613         return FALSE;
614     }
615     SERVER_START_REQ( set_window_pos )
616     {
617         req->handle        = hwnd;
618         req->previous      = insert_after;
619         req->flags         = swp_flags;
620         req->window.left   = rectWindow->left;
621         req->window.top    = rectWindow->top;
622         req->window.right  = rectWindow->right;
623         req->window.bottom = rectWindow->bottom;
624         req->client.left   = rectClient->left;
625         req->client.top    = rectClient->top;
626         req->client.right  = rectClient->right;
627         req->client.bottom = rectClient->bottom;
628         if (memcmp( rectWindow, &new_whole_rect, sizeof(RECT) ) || !IsRectEmpty( &valid_rects[0] ))
629         {
630             wine_server_add_data( req, &new_whole_rect, sizeof(new_whole_rect) );
631             if (!IsRectEmpty( &valid_rects[0] ))
632                 wine_server_add_data( req, valid_rects, 2 * sizeof(*valid_rects) );
633         }
634         ret = !wine_server_call( req );
635         new_style = reply->new_style;
636     }
637     SERVER_END_REQ;
638
639     if (win == WND_DESKTOP || data->whole_window == DefaultRootWindow(gdi_display))
640     {
641         data->whole_rect = data->client_rect = data->window_rect = *rectWindow;
642         if (win != WND_DESKTOP)
643         {
644             win->rectWindow   = *rectWindow;
645             win->rectClient   = *rectClient;
646             win->dwStyle      = new_style;
647             WIN_ReleasePtr( win );
648         }
649         return ret;
650     }
651
652     if (ret)
653     {
654         Display *display = thread_display();
655
656         /* invalidate DCEs */
657
658         if ((((swp_flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) && (new_style & WS_VISIBLE)) ||
659              (swp_flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW)))
660         {
661             RECT rect;
662             UnionRect( &rect, rectWindow, &win->rectWindow );
663             invalidate_dce( hwnd, &rect );
664         }
665
666         win->rectWindow   = *rectWindow;
667         win->rectClient   = *rectClient;
668         old_style         = win->dwStyle;
669         win->dwStyle      = new_style;
670         data->window_rect = *rectWindow;
671
672         TRACE( "win %p window %s client %s style %08lx\n",
673                hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style );
674
675         /* FIXME: copy the valid bits */
676
677         if (data->whole_window && !data->lock_changes)
678         {
679             if ((old_style & WS_VISIBLE) && !(new_style & WS_VISIBLE))
680             {
681                 /* window got hidden, unmap it */
682                 TRACE( "unmapping win %p\n", hwnd );
683                 wine_tsx11_lock();
684                 XUnmapWindow( display, data->whole_window );
685                 wine_tsx11_unlock();
686             }
687             else if ((new_style & WS_VISIBLE) && !X11DRV_is_window_rect_mapped( rectWindow ))
688             {
689                 /* resizing to zero size or off screen -> unmap */
690                 TRACE( "unmapping zero size or off-screen win %p\n", hwnd );
691                 wine_tsx11_lock();
692                 XUnmapWindow( display, data->whole_window );
693                 wine_tsx11_unlock();
694             }
695         }
696
697         X11DRV_sync_window_position( display, data, swp_flags, rectClient, &new_whole_rect );
698
699         if (data->whole_window && !data->lock_changes)
700         {
701             if ((new_style & WS_VISIBLE) && !(new_style & WS_MINIMIZE) &&
702                 X11DRV_is_window_rect_mapped( rectWindow ))
703             {
704                 if (!(old_style & WS_VISIBLE))
705                 {
706                     /* window got shown, map it */
707                     TRACE( "mapping win %p\n", hwnd );
708                     X11DRV_sync_window_style( display, data );
709                     X11DRV_set_wm_hints( display, data );
710                     wine_tsx11_lock();
711                     XMapWindow( display, data->whole_window );
712                     wine_tsx11_unlock();
713                 }
714                 else if ((swp_flags & (SWP_NOSIZE | SWP_NOMOVE)) != (SWP_NOSIZE | SWP_NOMOVE))
715                 {
716                     /* resizing from zero size to non-zero -> map */
717                     TRACE( "mapping non zero size or off-screen win %p\n", hwnd );
718                     wine_tsx11_lock();
719                     XMapWindow( display, data->whole_window );
720                     wine_tsx11_unlock();
721                 }
722                 update_fullscreen_state( display, data, &old_client_rect );
723             }
724         }
725     }
726     WIN_ReleasePtr( win );
727     return ret;
728 }
729
730
731 /***********************************************************************
732  *              SetWindowPos   (X11DRV.@)
733  */
734 BOOL X11DRV_SetWindowPos( WINDOWPOS *winpos )
735 {
736     RECT newWindowRect, newClientRect, valid_rects[2];
737     UINT orig_flags;
738
739     TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
740            winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
741            winpos->cx, winpos->cy, winpos->flags);
742
743     orig_flags = winpos->flags;
744
745     /* First make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
746     if (!(winpos->flags & SWP_NOMOVE))
747     {
748         if (winpos->x < -32768) winpos->x = -32768;
749         else if (winpos->x > 32767) winpos->x = 32767;
750         if (winpos->y < -32768) winpos->y = -32768;
751         else if (winpos->y > 32767) winpos->y = 32767;
752     }
753     if (!(winpos->flags & SWP_NOSIZE))
754     {
755         if (winpos->cx < 0) winpos->cx = 0;
756         else if (winpos->cx > 32767) winpos->cx = 32767;
757         if (winpos->cy < 0) winpos->cy = 0;
758         else if (winpos->cy > 32767) winpos->cy = 32767;
759     }
760
761     if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
762
763     /* Fix redundant flags */
764     if (!fixup_flags( winpos )) return FALSE;
765
766     if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
767     {
768         if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
769             winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
770     }
771
772     /* Common operations */
773
774     SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect, valid_rects );
775
776     if (!X11DRV_set_window_pos( winpos->hwnd, winpos->hwndInsertAfter,
777                                 &newWindowRect, &newClientRect, orig_flags, valid_rects ))
778         return FALSE;
779
780     if (!(orig_flags & SWP_SHOWWINDOW))
781     {
782         UINT rdw_flags = RDW_FRAME | RDW_ERASE;
783         if ( !(orig_flags & SWP_DEFERERASE) ) rdw_flags |= RDW_ERASENOW;
784         RedrawWindow( winpos->hwnd, NULL, NULL, rdw_flags );
785     }
786
787     if( winpos->flags & SWP_HIDEWINDOW )
788         HideCaret(winpos->hwnd);
789     else if (winpos->flags & SWP_SHOWWINDOW)
790         ShowCaret(winpos->hwnd);
791
792     if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)))
793     {
794         /* child windows get WM_CHILDACTIVATE message */
795         if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
796             SendMessageW( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
797         else
798             SetForegroundWindow( winpos->hwnd );
799     }
800
801       /* And last, send the WM_WINDOWPOSCHANGED message */
802
803     TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
804
805     if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
806     {
807         /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
808            and always contains final window position.
809          */
810         winpos->x = newWindowRect.left;
811         winpos->y = newWindowRect.top;
812         winpos->cx = newWindowRect.right - newWindowRect.left;
813         winpos->cy = newWindowRect.bottom - newWindowRect.top;
814         SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
815     }
816
817     return TRUE;
818 }
819
820
821 /***********************************************************************
822  *           WINPOS_FindIconPos
823  *
824  * Find a suitable place for an iconic window.
825  */
826 static POINT WINPOS_FindIconPos( HWND hwnd, POINT pt )
827 {
828     RECT rect, rectParent;
829     HWND parent, child;
830     HRGN hrgn, tmp;
831     int xspacing, yspacing;
832
833     parent = GetAncestor( hwnd, GA_PARENT );
834     GetClientRect( parent, &rectParent );
835     if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
836         (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
837         return pt;  /* The icon already has a suitable position */
838
839     xspacing = GetSystemMetrics(SM_CXICONSPACING);
840     yspacing = GetSystemMetrics(SM_CYICONSPACING);
841
842     /* Check if another icon already occupies this spot */
843     /* FIXME: this is completely inefficient */
844
845     hrgn = CreateRectRgn( 0, 0, 0, 0 );
846     tmp = CreateRectRgn( 0, 0, 0, 0 );
847     for (child = GetWindow( parent, GW_HWNDFIRST ); child; child = GetWindow( child, GW_HWNDNEXT ))
848     {
849         WND *childPtr;
850         if (child == hwnd) continue;
851         if ((GetWindowLongW( child, GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != (WS_VISIBLE|WS_MINIMIZE))
852             continue;
853         if (!(childPtr = WIN_GetPtr( child )) || childPtr == WND_OTHER_PROCESS)
854             continue;
855         SetRectRgn( tmp, childPtr->rectWindow.left, childPtr->rectWindow.top,
856                     childPtr->rectWindow.right, childPtr->rectWindow.bottom );
857         CombineRgn( hrgn, hrgn, tmp, RGN_OR );
858         WIN_ReleasePtr( childPtr );
859     }
860     DeleteObject( tmp );
861
862     for (rect.bottom = rectParent.bottom; rect.bottom >= yspacing; rect.bottom -= yspacing)
863     {
864         for (rect.left = rectParent.left; rect.left <= rectParent.right - xspacing; rect.left += xspacing)
865         {
866             rect.right = rect.left + xspacing;
867             rect.top = rect.bottom - yspacing;
868             if (!RectInRegion( hrgn, &rect ))
869             {
870                 /* No window was found, so it's OK for us */
871                 pt.x = rect.left + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
872                 pt.y = rect.top + (yspacing - GetSystemMetrics(SM_CYICON)) / 2;
873                 DeleteObject( hrgn );
874                 return pt;
875             }
876         }
877     }
878     DeleteObject( hrgn );
879     pt.x = pt.y = 0;
880     return pt;
881 }
882
883
884
885
886
887 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
888 {
889     WND *wndPtr;
890     UINT swpFlags = 0;
891     POINT size;
892     LONG old_style;
893     WINDOWPLACEMENT wpl;
894
895     TRACE("%p %u\n", hwnd, cmd );
896
897     wpl.length = sizeof(wpl);
898     GetWindowPlacement( hwnd, &wpl );
899
900     if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
901         return SWP_NOSIZE | SWP_NOMOVE;
902
903     if (IsIconic( hwnd ))
904     {
905         switch (cmd)
906         {
907         case SW_SHOWMINNOACTIVE:
908         case SW_SHOWMINIMIZED:
909         case SW_FORCEMINIMIZE:
910         case SW_MINIMIZE:
911             return SWP_NOSIZE | SWP_NOMOVE;
912         }
913         if (!SendMessageW( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
914         swpFlags |= SWP_NOCOPYBITS;
915     }
916
917     switch( cmd )
918     {
919     case SW_SHOWMINNOACTIVE:
920     case SW_SHOWMINIMIZED:
921     case SW_FORCEMINIMIZE:
922     case SW_MINIMIZE:
923         if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
924         if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
925         else wndPtr->flags &= ~WIN_RESTORE_MAX;
926         WIN_ReleasePtr( wndPtr );
927
928         WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
929
930         X11DRV_set_iconic_state( hwnd );
931
932         wpl.ptMinPosition = WINPOS_FindIconPos( hwnd, wpl.ptMinPosition );
933
934         SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
935                  GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON) );
936         swpFlags |= SWP_NOCOPYBITS;
937         break;
938
939     case SW_MAXIMIZE:
940         old_style = GetWindowLongW( hwnd, GWL_STYLE );
941         if ((old_style & WS_MAXIMIZE) && (old_style & WS_CHILD)) return SWP_NOSIZE | SWP_NOMOVE;
942
943         WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
944
945         old_style = WIN_SetStyle( hwnd, WS_MAXIMIZE, WS_MINIMIZE );
946         if (old_style & WS_MINIMIZE)
947         {
948             WINPOS_ShowIconTitle( hwnd, FALSE );
949             X11DRV_set_iconic_state( hwnd );
950         }
951         SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
952         break;
953
954     case SW_RESTORE:
955         old_style = WIN_SetStyle( hwnd, 0, WS_MINIMIZE | WS_MAXIMIZE );
956         if (old_style & WS_MINIMIZE)
957         {
958             BOOL restore_max;
959
960             WINPOS_ShowIconTitle( hwnd, FALSE );
961             X11DRV_set_iconic_state( hwnd );
962
963             if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
964             restore_max = (wndPtr->flags & WIN_RESTORE_MAX) != 0;
965             WIN_ReleasePtr( wndPtr );
966             if (restore_max)
967             {
968                 /* Restore to maximized position */
969                 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
970                 WIN_SetStyle( hwnd, WS_MAXIMIZE, 0 );
971                 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
972                 break;
973             }
974         }
975         else if (!(old_style & WS_MAXIMIZE)) break;
976
977         /* Restore to normal position */
978
979         *rect = wpl.rcNormalPosition;
980         rect->right -= rect->left;
981         rect->bottom -= rect->top;
982
983         break;
984     }
985
986     return swpFlags;
987 }
988
989
990 /***********************************************************************
991  *              ShowWindow   (X11DRV.@)
992  */
993 BOOL X11DRV_ShowWindow( HWND hwnd, INT cmd )
994 {
995     WND *wndPtr;
996     HWND parent;
997     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
998     BOOL wasVisible = (style & WS_VISIBLE) != 0;
999     BOOL showFlag = TRUE, state_change = FALSE;
1000     RECT newPos = {0, 0, 0, 0};
1001     UINT swp = 0;
1002
1003     TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);
1004
1005     switch(cmd)
1006     {
1007         case SW_HIDE:
1008             if (!wasVisible) return FALSE;
1009             showFlag = FALSE;
1010             swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1011             if (hwnd != GetActiveWindow())
1012                 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1013             break;
1014
1015         case SW_SHOWMINNOACTIVE:
1016             swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1017             /* fall through */
1018         case SW_MINIMIZE:
1019         case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
1020             if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1021             /* fall through */
1022         case SW_SHOWMINIMIZED:
1023             swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1024             swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
1025             if (style & WS_MINIMIZE) return wasVisible;
1026             state_change = TRUE;
1027             break;
1028
1029         case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
1030             swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1031             swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
1032             if ((style & WS_MAXIMIZE) && (style & WS_CHILD)) return wasVisible;
1033             state_change = TRUE;
1034             break;
1035
1036         case SW_SHOWNA:
1037             swp |= SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1038             if (style & WS_CHILD) swp |= SWP_NOZORDER;
1039             break;
1040         case SW_SHOW:
1041             if (wasVisible) return TRUE;
1042             swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1043             if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1044             break;
1045
1046         case SW_RESTORE:
1047             swp |= SWP_FRAMECHANGED;
1048             state_change = TRUE;
1049             /* fall through */
1050         case SW_SHOWNOACTIVATE:
1051             swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1052             /* fall through */
1053         case SW_SHOWNORMAL:  /* same as SW_NORMAL: */
1054         case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
1055             swp |= SWP_SHOWWINDOW;
1056             if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1057                  swp |= WINPOS_MinMaximize( hwnd, SW_RESTORE, &newPos );
1058             else swp |= SWP_NOSIZE | SWP_NOMOVE;
1059             if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1060             break;
1061     }
1062
1063     if ((showFlag != wasVisible || cmd == SW_SHOWNA) && !state_change)
1064     {
1065         SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
1066         if (!IsWindow( hwnd )) return wasVisible;
1067     }
1068
1069     parent = GetAncestor( hwnd, GA_PARENT );
1070     if (parent && !IsWindowVisible( parent ) && !state_change)
1071     {
1072         /* if parent is not visible simply toggle WS_VISIBLE and return */
1073         if (showFlag) WIN_SetStyle( hwnd, WS_VISIBLE, 0 );
1074         else WIN_SetStyle( hwnd, 0, WS_VISIBLE );
1075     }
1076     else
1077     {
1078         if (style & WS_CHILD)
1079         {
1080             if (state_change)
1081             {
1082                 /* it appears that Windows always adds an undocumented 0x8000
1083                  * flag if the state of a window changes.
1084                  */
1085                 swp |= SWP_STATECHANGED;
1086             }
1087         }
1088
1089         SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1090                       newPos.right, newPos.bottom, LOWORD(swp) );
1091     }
1092
1093     if (cmd == SW_HIDE)
1094     {
1095         HWND hFocus;
1096
1097         /* FIXME: This will cause the window to be activated irrespective
1098          * of whether it is owned by the same thread. Has to be done
1099          * asynchronously.
1100          */
1101
1102         if (hwnd == GetActiveWindow())
1103             WINPOS_ActivateOtherWindow(hwnd);
1104
1105         /* Revert focus to parent */
1106         hFocus = GetFocus();
1107         if (hwnd == hFocus || IsChild(hwnd, hFocus))
1108         {
1109             HWND parent = GetAncestor(hwnd, GA_PARENT);
1110             if (parent == GetDesktopWindow()) parent = 0;
1111             SetFocus(parent);
1112         }
1113     }
1114
1115     if (IsIconic(hwnd)) WINPOS_ShowIconTitle( hwnd, TRUE );
1116
1117     if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return wasVisible;
1118
1119     if (wndPtr->flags & WIN_NEED_SIZE)
1120     {
1121         /* should happen only in CreateWindowEx() */
1122         int wParam = SIZE_RESTORED;
1123         RECT client = wndPtr->rectClient;
1124
1125         wndPtr->flags &= ~WIN_NEED_SIZE;
1126         if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1127         else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
1128         WIN_ReleasePtr( wndPtr );
1129
1130         SendMessageW( hwnd, WM_SIZE, wParam,
1131                       MAKELONG( client.right - client.left, client.bottom - client.top ));
1132         SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( client.left, client.top ));
1133     }
1134     else WIN_ReleasePtr( wndPtr );
1135
1136     return wasVisible;
1137 }
1138
1139
1140 /**********************************************************************
1141  *              X11DRV_MapNotify
1142  */
1143 void X11DRV_MapNotify( HWND hwnd, XEvent *event )
1144 {
1145     struct x11drv_win_data *data;
1146     HWND hwndFocus = GetFocus();
1147     WND *win;
1148
1149     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1150
1151     if (!(win = WIN_GetPtr( hwnd ))) return;
1152
1153     if (data->managed && (win->dwStyle & WS_VISIBLE) && (win->dwStyle & WS_MINIMIZE))
1154     {
1155         int x, y;
1156         unsigned int width, height, border, depth;
1157         Window root, top;
1158         RECT rect;
1159         LONG style = WS_VISIBLE;
1160
1161         /* FIXME: hack */
1162         wine_tsx11_lock();
1163         XGetGeometry( event->xmap.display, data->whole_window, &root, &x, &y, &width, &height,
1164                         &border, &depth );
1165         XTranslateCoordinates( event->xmap.display, data->whole_window, root, 0, 0, &x, &y, &top );
1166         wine_tsx11_unlock();
1167         rect.left   = x;
1168         rect.top    = y;
1169         rect.right  = x + width;
1170         rect.bottom = y + height;
1171         X11DRV_X_to_window_rect( data, &rect );
1172
1173         invalidate_dce( hwnd, &data->window_rect );
1174
1175         if (win->flags & WIN_RESTORE_MAX) style |= WS_MAXIMIZE;
1176         WIN_SetStyle( hwnd, style, WS_MINIMIZE );
1177         WIN_ReleasePtr( win );
1178
1179         SendMessageW( hwnd, WM_SHOWWINDOW, SW_RESTORE, 0 );
1180         data->lock_changes++;
1181         SetWindowPos( hwnd, 0, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1182                       SWP_NOZORDER );
1183         data->lock_changes--;
1184     }
1185     else WIN_ReleasePtr( win );
1186     if (hwndFocus && IsChild( hwnd, hwndFocus )) X11DRV_SetFocus(hwndFocus);  /* FIXME */
1187 }
1188
1189
1190 /**********************************************************************
1191  *              X11DRV_UnmapNotify
1192  */
1193 void X11DRV_UnmapNotify( HWND hwnd, XEvent *event )
1194 {
1195     struct x11drv_win_data *data;
1196     WND *win;
1197
1198     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1199
1200     if (!(win = WIN_GetPtr( hwnd ))) return;
1201
1202     if ((win->dwStyle & WS_VISIBLE) && data->managed &&
1203         X11DRV_is_window_rect_mapped( &win->rectWindow ))
1204     {
1205         if (win->dwStyle & WS_MAXIMIZE)
1206             win->flags |= WIN_RESTORE_MAX;
1207         else
1208             win->flags &= ~WIN_RESTORE_MAX;
1209
1210         WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
1211         WIN_ReleasePtr( win );
1212
1213         EndMenu();
1214         SendMessageW( hwnd, WM_SHOWWINDOW, SW_MINIMIZE, 0 );
1215         data->lock_changes++;
1216         SetWindowPos( hwnd, 0, 0, 0, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON),
1217                       SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
1218         data->lock_changes--;
1219     }
1220     else WIN_ReleasePtr( win );
1221 }
1222
1223
1224 /***********************************************************************
1225  *              X11DRV_handle_desktop_resize
1226  */
1227 void X11DRV_handle_desktop_resize( unsigned int width, unsigned int height )
1228 {
1229     RECT rect;
1230     HWND hwnd = GetDesktopWindow();
1231     struct x11drv_win_data *data;
1232
1233     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1234     screen_width  = width;
1235     screen_height = height;
1236     TRACE("desktop %p change to (%dx%d)\n", hwnd, width, height);
1237     SetRect( &rect, 0, 0, width, height );
1238     data->lock_changes++;
1239     X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER|SWP_NOMOVE, NULL );
1240     data->lock_changes--;
1241     SendMessageTimeoutW( HWND_BROADCAST, WM_DISPLAYCHANGE, screen_depth,
1242                          MAKELPARAM( width, height ), SMTO_ABORTIFHUNG, 2000, NULL );
1243 }
1244
1245
1246 /***********************************************************************
1247  *              X11DRV_ConfigureNotify
1248  */
1249 void X11DRV_ConfigureNotify( HWND hwnd, XEvent *xev )
1250 {
1251     XConfigureEvent *event = &xev->xconfigure;
1252     struct x11drv_win_data *data;
1253     RECT rect;
1254     UINT flags;
1255     int cx, cy, x = event->x, y = event->y;
1256
1257     if (!hwnd) return;
1258     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1259
1260     /* Get geometry */
1261
1262     if (!event->send_event)  /* normal event, need to map coordinates to the root */
1263     {
1264         Window child;
1265         wine_tsx11_lock();
1266         XTranslateCoordinates( event->display, data->whole_window, root_window,
1267                                0, 0, &x, &y, &child );
1268         wine_tsx11_unlock();
1269     }
1270     rect.left   = x;
1271     rect.top    = y;
1272     rect.right  = x + event->width;
1273     rect.bottom = y + event->height;
1274     TRACE( "win %p new X rect %ld,%ld,%ldx%ld (event %d,%d,%dx%d)\n",
1275            hwnd, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1276            event->x, event->y, event->width, event->height );
1277     X11DRV_X_to_window_rect( data, &rect );
1278
1279     x     = rect.left;
1280     y     = rect.top;
1281     cx    = rect.right - rect.left;
1282     cy    = rect.bottom - rect.top;
1283     flags = SWP_NOACTIVATE | SWP_NOZORDER;
1284
1285     /* Compare what has changed */
1286
1287     GetWindowRect( hwnd, &rect );
1288     if (rect.left == x && rect.top == y) flags |= SWP_NOMOVE;
1289     else
1290         TRACE( "%p moving from (%ld,%ld) to (%d,%d)\n",
1291                hwnd, rect.left, rect.top, x, y );
1292
1293     if ((rect.right - rect.left == cx && rect.bottom - rect.top == cy) ||
1294         IsIconic(hwnd) ||
1295         (IsRectEmpty( &rect ) && event->width == 1 && event->height == 1))
1296     {
1297         if (flags & SWP_NOMOVE) return;  /* if nothing changed, don't do anything */
1298         flags |= SWP_NOSIZE;
1299     }
1300     else
1301         TRACE( "%p resizing from (%ldx%ld) to (%dx%d)\n",
1302                hwnd, rect.right - rect.left, rect.bottom - rect.top, cx, cy );
1303
1304     data->lock_changes++;
1305     SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
1306     data->lock_changes--;
1307 }
1308
1309
1310 /***********************************************************************
1311  *              SetWindowRgn  (X11DRV.@)
1312  *
1313  * Assign specified region to window (for non-rectangular windows)
1314  */
1315 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
1316 {
1317     struct x11drv_win_data *data;
1318
1319     if (!(data = X11DRV_get_win_data( hwnd )))
1320     {
1321         if (IsWindow( hwnd ))
1322             FIXME( "not supported on other thread window %p\n", hwnd );
1323         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1324         return FALSE;
1325     }
1326
1327 #ifdef HAVE_LIBXSHAPE
1328     if (data->whole_window)
1329     {
1330         Display *display = thread_display();
1331
1332         if (!hrgn)
1333         {
1334             wine_tsx11_lock();
1335             XShapeCombineMask( display, data->whole_window,
1336                                ShapeBounding, 0, 0, None, ShapeSet );
1337             wine_tsx11_unlock();
1338         }
1339         else
1340         {
1341             RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
1342             if (pRegionData)
1343             {
1344                 wine_tsx11_lock();
1345                 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
1346                                          data->window_rect.left - data->whole_rect.left,
1347                                          data->window_rect.top - data->whole_rect.top,
1348                                          (XRectangle *)pRegionData->Buffer,
1349                                          pRegionData->rdh.nCount,
1350                                          ShapeSet, YXBanded );
1351                 wine_tsx11_unlock();
1352                 HeapFree(GetProcessHeap(), 0, pRegionData);
1353             }
1354         }
1355     }
1356 #endif  /* HAVE_LIBXSHAPE */
1357
1358     invalidate_dce( hwnd, &data->window_rect );
1359     return TRUE;
1360 }
1361
1362
1363 /***********************************************************************
1364  *           draw_moving_frame
1365  *
1366  * Draw the frame used when moving or resizing window.
1367  *
1368  * FIXME:  This causes problems in Win95 mode.  (why?)
1369  */
1370 static void draw_moving_frame( HDC hdc, RECT *rect, BOOL thickframe )
1371 {
1372     if (thickframe)
1373     {
1374         const int width = GetSystemMetrics(SM_CXFRAME);
1375         const int height = GetSystemMetrics(SM_CYFRAME);
1376
1377         HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
1378         PatBlt( hdc, rect->left, rect->top,
1379                 rect->right - rect->left - width, height, PATINVERT );
1380         PatBlt( hdc, rect->left, rect->top + height, width,
1381                 rect->bottom - rect->top - height, PATINVERT );
1382         PatBlt( hdc, rect->left + width, rect->bottom - 1,
1383                 rect->right - rect->left - width, -height, PATINVERT );
1384         PatBlt( hdc, rect->right - 1, rect->top, -width,
1385                 rect->bottom - rect->top - height, PATINVERT );
1386         SelectObject( hdc, hbrush );
1387     }
1388     else DrawFocusRect( hdc, rect );
1389 }
1390
1391
1392 /***********************************************************************
1393  *           start_size_move
1394  *
1395  * Initialisation of a move or resize, when initiatied from a menu choice.
1396  * Return hit test code for caption or sizing border.
1397  */
1398 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
1399 {
1400     LONG hittest = 0;
1401     POINT pt;
1402     MSG msg;
1403     RECT rectWindow;
1404
1405     GetWindowRect( hwnd, &rectWindow );
1406
1407     if ((wParam & 0xfff0) == SC_MOVE)
1408     {
1409         /* Move pointer at the center of the caption */
1410         RECT rect = rectWindow;
1411         /* Note: to be exactly centered we should take the different types
1412          * of border into account, but it shouldn't make more that a few pixels
1413          * of difference so let's not bother with that */
1414         rect.top += GetSystemMetrics(SM_CYBORDER);
1415         if (style & WS_SYSMENU)
1416             rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
1417         if (style & WS_MINIMIZEBOX)
1418             rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1419         if (style & WS_MAXIMIZEBOX)
1420             rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1421         pt.x = (rect.right + rect.left) / 2;
1422         pt.y = rect.top + GetSystemMetrics(SM_CYSIZE)/2;
1423         hittest = HTCAPTION;
1424         *capturePoint = pt;
1425     }
1426     else  /* SC_SIZE */
1427     {
1428         pt.x = pt.y = 0;
1429         while(!hittest)
1430         {
1431             GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST );
1432             if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1433
1434             switch(msg.message)
1435             {
1436             case WM_MOUSEMOVE:
1437                 pt = msg.pt;
1438                 hittest = SendMessageW( hwnd, WM_NCHITTEST, 0, MAKELONG( pt.x, pt.y ) );
1439                 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT)) hittest = 0;
1440                 break;
1441
1442             case WM_LBUTTONUP:
1443                 return 0;
1444
1445             case WM_KEYDOWN:
1446                 switch(msg.wParam)
1447                 {
1448                 case VK_UP:
1449                     hittest = HTTOP;
1450                     pt.x =(rectWindow.left+rectWindow.right)/2;
1451                     pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
1452                     break;
1453                 case VK_DOWN:
1454                     hittest = HTBOTTOM;
1455                     pt.x =(rectWindow.left+rectWindow.right)/2;
1456                     pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
1457                     break;
1458                 case VK_LEFT:
1459                     hittest = HTLEFT;
1460                     pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
1461                     pt.y =(rectWindow.top+rectWindow.bottom)/2;
1462                     break;
1463                 case VK_RIGHT:
1464                     hittest = HTRIGHT;
1465                     pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
1466                     pt.y =(rectWindow.top+rectWindow.bottom)/2;
1467                     break;
1468                 case VK_RETURN:
1469                 case VK_ESCAPE: return 0;
1470                 }
1471             }
1472         }
1473         *capturePoint = pt;
1474     }
1475     SetCursorPos( pt.x, pt.y );
1476     SendMessageW( hwnd, WM_SETCURSOR, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
1477     return hittest;
1478 }
1479
1480
1481 /***********************************************************************
1482  *           set_movesize_capture
1483  */
1484 static void set_movesize_capture( HWND hwnd )
1485 {
1486     HWND previous = 0;
1487
1488     SERVER_START_REQ( set_capture_window )
1489     {
1490         req->handle = hwnd;
1491         req->flags  = CAPTURE_MOVESIZE;
1492         if (!wine_server_call_err( req ))
1493         {
1494             previous = reply->previous;
1495             hwnd = reply->full_handle;
1496         }
1497     }
1498     SERVER_END_REQ;
1499     if (previous && previous != hwnd)
1500         SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
1501 }
1502
1503 /***********************************************************************
1504  *           X11DRV_WMMoveResizeWindow
1505  *
1506  * Tells the window manager to initiate a move or resize operation.
1507  *
1508  * SEE
1509  *  http://freedesktop.org/Standards/wm-spec/1.3/ar01s04.html
1510  *  or search for "_NET_WM_MOVERESIZE"
1511  */
1512 static void X11DRV_WMMoveResizeWindow( HWND hwnd, int x, int y, int dir )
1513 {
1514     XEvent xev;
1515     Display *display = thread_display();
1516
1517     xev.xclient.type = ClientMessage;
1518     xev.xclient.window = X11DRV_get_whole_window(hwnd);
1519     xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
1520     xev.xclient.serial = 0;
1521     xev.xclient.display = display;
1522     xev.xclient.send_event = True;
1523     xev.xclient.format = 32;
1524     xev.xclient.data.l[0] = x; /* x coord */
1525     xev.xclient.data.l[1] = y; /* y coord */
1526     xev.xclient.data.l[2] = dir; /* direction */
1527     xev.xclient.data.l[3] = 1; /* button */
1528     xev.xclient.data.l[4] = 0; /* unused */
1529
1530     /* need to ungrab the pointer that may have been automatically grabbed
1531      * with a ButtonPress event */
1532     wine_tsx11_lock();
1533     XUngrabPointer( display, CurrentTime );
1534     XSendEvent(display, root_window, False, SubstructureNotifyMask, &xev);
1535     wine_tsx11_unlock();
1536 }
1537
1538 /***********************************************************************
1539  *           SysCommandSizeMove   (X11DRV.@)
1540  *
1541  * Perform SC_MOVE and SC_SIZE commands.
1542  */
1543 void X11DRV_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
1544 {
1545     MSG msg;
1546     RECT sizingRect, mouseRect, origRect;
1547     HDC hdc;
1548     HWND parent;
1549     LONG hittest = (LONG)(wParam & 0x0f);
1550     WPARAM syscommand = wParam & 0xfff0;
1551     HCURSOR hDragCursor = 0, hOldCursor = 0;
1552     POINT minTrack, maxTrack;
1553     POINT capturePoint, pt;
1554     LONG style = GetWindowLongA( hwnd, GWL_STYLE );
1555     BOOL    thickframe = HAS_THICKFRAME( style );
1556     BOOL    iconic = style & WS_MINIMIZE;
1557     BOOL    moved = FALSE;
1558     DWORD     dwPoint = GetMessagePos ();
1559     BOOL DragFullWindows = FALSE;
1560     BOOL grab;
1561     Window parent_win, whole_win;
1562     Display *old_gdi_display = NULL;
1563     struct x11drv_thread_data *thread_data = x11drv_thread_data();
1564     struct x11drv_win_data *data;
1565
1566     pt.x = (short)LOWORD(dwPoint);
1567     pt.y = (short)HIWORD(dwPoint);
1568     capturePoint = pt;
1569
1570     if (IsZoomed(hwnd) || !IsWindowVisible(hwnd)) return;
1571
1572     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1573
1574     /* if we are managed then we let the WM do all the work */
1575     if (data->managed)
1576     {
1577         int dir;
1578         if (syscommand == SC_MOVE)
1579         {
1580             if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
1581             else dir = _NET_WM_MOVERESIZE_MOVE;
1582         }
1583         else if (!hittest) dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD;
1584         else
1585             switch (hittest)
1586             {
1587             case WMSZ_LEFT:        dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
1588             case WMSZ_RIGHT:       dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
1589             case WMSZ_TOP:         dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
1590             case WMSZ_TOPLEFT:     dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
1591             case WMSZ_TOPRIGHT:    dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
1592             case WMSZ_BOTTOM:      dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
1593             case WMSZ_BOTTOMLEFT:  dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
1594             case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
1595             default:
1596                 ERR("Invalid hittest value: %ld\n", hittest);
1597                 dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD;
1598             }
1599         X11DRV_WMMoveResizeWindow( hwnd, pt.x, pt.y, dir );
1600         return;
1601     }
1602
1603     SystemParametersInfoA(SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0);
1604
1605     if (syscommand == SC_MOVE)
1606     {
1607         if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1608         if (!hittest) return;
1609     }
1610     else  /* SC_SIZE */
1611     {
1612         if ( hittest && (syscommand != SC_MOUSEMENU) )
1613             hittest += (HTLEFT - WMSZ_LEFT);
1614         else
1615         {
1616             set_movesize_capture( hwnd );
1617             hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1618             if (!hittest)
1619             {
1620                 set_movesize_capture(0);
1621                 return;
1622             }
1623         }
1624     }
1625
1626       /* Get min/max info */
1627
1628     WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1629     GetWindowRect( hwnd, &sizingRect );
1630     if (style & WS_CHILD)
1631     {
1632         parent = GetParent(hwnd);
1633         /* make sizing rect relative to parent */
1634         MapWindowPoints( 0, parent, (POINT*)&sizingRect, 2 );
1635         GetClientRect( parent, &mouseRect );
1636     }
1637     else
1638     {
1639         parent = 0;
1640         SetRect(&mouseRect, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
1641     }
1642     origRect = sizingRect;
1643
1644     if (ON_LEFT_BORDER(hittest))
1645     {
1646         mouseRect.left  = max( mouseRect.left, sizingRect.right-maxTrack.x );
1647         mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
1648     }
1649     else if (ON_RIGHT_BORDER(hittest))
1650     {
1651         mouseRect.left  = max( mouseRect.left, sizingRect.left+minTrack.x );
1652         mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
1653     }
1654     if (ON_TOP_BORDER(hittest))
1655     {
1656         mouseRect.top    = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
1657         mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
1658     }
1659     else if (ON_BOTTOM_BORDER(hittest))
1660     {
1661         mouseRect.top    = max( mouseRect.top, sizingRect.top+minTrack.y );
1662         mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
1663     }
1664     if (parent) MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
1665
1666     /* Retrieve a default cache DC (without using the window style) */
1667     hdc = GetDCEx( parent, 0, DCX_CACHE );
1668
1669     if( iconic ) /* create a cursor for dragging */
1670     {
1671         hDragCursor = (HCURSOR)GetClassLongPtrW( hwnd, GCLP_HICON);
1672         if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageW( hwnd, WM_QUERYDRAGICON, 0, 0L);
1673         if( !hDragCursor ) iconic = FALSE;
1674     }
1675
1676     /* repaint the window before moving it around */
1677     RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1678
1679     SendMessageW( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
1680     set_movesize_capture( hwnd );
1681
1682     /* grab the server only when moving top-level windows without desktop */
1683     grab = (!DragFullWindows && !parent && (root_window == DefaultRootWindow(gdi_display)));
1684
1685     if (grab)
1686     {
1687         wine_tsx11_lock();
1688         XSync( gdi_display, False );
1689         XGrabServer( thread_data->display );
1690         XSync( thread_data->display, False );
1691         /* switch gdi display to the thread display, since the server is grabbed */
1692         old_gdi_display = gdi_display;
1693         gdi_display = thread_data->display;
1694         wine_tsx11_unlock();
1695     }
1696     whole_win = X11DRV_get_whole_window( GetAncestor(hwnd,GA_ROOT) );
1697     parent_win = parent ? X11DRV_get_whole_window( GetAncestor(parent,GA_ROOT) ) : root_window;
1698
1699     wine_tsx11_lock();
1700     XGrabPointer( thread_data->display, whole_win, False,
1701                   PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1702                   GrabModeAsync, GrabModeAsync, parent_win, None, CurrentTime );
1703     wine_tsx11_unlock();
1704     thread_data->grab_window = whole_win;
1705
1706     while(1)
1707     {
1708         int dx = 0, dy = 0;
1709
1710         if (!GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST )) break;
1711         if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1712
1713         /* Exit on button-up, Return, or Esc */
1714         if ((msg.message == WM_LBUTTONUP) ||
1715             ((msg.message == WM_KEYDOWN) &&
1716              ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
1717
1718         if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
1719             continue;  /* We are not interested in other messages */
1720
1721         pt = msg.pt;
1722
1723         if (msg.message == WM_KEYDOWN) switch(msg.wParam)
1724         {
1725         case VK_UP:    pt.y -= 8; break;
1726         case VK_DOWN:  pt.y += 8; break;
1727         case VK_LEFT:  pt.x -= 8; break;
1728         case VK_RIGHT: pt.x += 8; break;
1729         }
1730
1731         pt.x = max( pt.x, mouseRect.left );
1732         pt.x = min( pt.x, mouseRect.right );
1733         pt.y = max( pt.y, mouseRect.top );
1734         pt.y = min( pt.y, mouseRect.bottom );
1735
1736         dx = pt.x - capturePoint.x;
1737         dy = pt.y - capturePoint.y;
1738
1739         if (dx || dy)
1740         {
1741             if( !moved )
1742             {
1743                 moved = TRUE;
1744
1745                 if( iconic ) /* ok, no system popup tracking */
1746                 {
1747                     hOldCursor = SetCursor(hDragCursor);
1748                     ShowCursor( TRUE );
1749                     WINPOS_ShowIconTitle( hwnd, FALSE );
1750                 }
1751                 else if(!DragFullWindows)
1752                     draw_moving_frame( hdc, &sizingRect, thickframe );
1753             }
1754
1755             if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
1756             else
1757             {
1758                 RECT newRect = sizingRect;
1759                 WPARAM wpSizingHit = 0;
1760
1761                 if (hittest == HTCAPTION) OffsetRect( &newRect, dx, dy );
1762                 if (ON_LEFT_BORDER(hittest)) newRect.left += dx;
1763                 else if (ON_RIGHT_BORDER(hittest)) newRect.right += dx;
1764                 if (ON_TOP_BORDER(hittest)) newRect.top += dy;
1765                 else if (ON_BOTTOM_BORDER(hittest)) newRect.bottom += dy;
1766                 if(!iconic && !DragFullWindows) draw_moving_frame( hdc, &sizingRect, thickframe );
1767                 capturePoint = pt;
1768
1769                 /* determine the hit location */
1770                 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
1771                     wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
1772                 SendMessageW( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&newRect );
1773
1774                 if (!iconic)
1775                 {
1776                     if(!DragFullWindows)
1777                         draw_moving_frame( hdc, &newRect, thickframe );
1778                     else
1779                         SetWindowPos( hwnd, 0, newRect.left, newRect.top,
1780                                       newRect.right - newRect.left,
1781                                       newRect.bottom - newRect.top,
1782                                       ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1783                 }
1784                 sizingRect = newRect;
1785             }
1786         }
1787     }
1788
1789     set_movesize_capture(0);
1790     if( iconic )
1791     {
1792         if( moved ) /* restore cursors, show icon title later on */
1793         {
1794             ShowCursor( FALSE );
1795             SetCursor( hOldCursor );
1796         }
1797     }
1798     else if (moved && !DragFullWindows)
1799         draw_moving_frame( hdc, &sizingRect, thickframe );
1800
1801     ReleaseDC( parent, hdc );
1802
1803     wine_tsx11_lock();
1804     XUngrabPointer( thread_data->display, CurrentTime );
1805     if (grab)
1806     {
1807         XSync( thread_data->display, False );
1808         XUngrabServer( thread_data->display );
1809         XSync( thread_data->display, False );
1810         gdi_display = old_gdi_display;
1811     }
1812     wine_tsx11_unlock();
1813     thread_data->grab_window = None;
1814
1815     if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
1816         moved = FALSE;
1817
1818     SendMessageW( hwnd, WM_EXITSIZEMOVE, 0, 0 );
1819     SendMessageW( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
1820
1821     /* window moved or resized */
1822     if (moved)
1823     {
1824         /* if the moving/resizing isn't canceled call SetWindowPos
1825          * with the new position or the new size of the window
1826          */
1827         if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
1828         {
1829             /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
1830             if(!DragFullWindows)
1831                 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
1832                               sizingRect.right - sizingRect.left,
1833                               sizingRect.bottom - sizingRect.top,
1834                               ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1835         }
1836         else
1837         { /* restore previous size/position */
1838             if(DragFullWindows)
1839                 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
1840                               origRect.right - origRect.left,
1841                               origRect.bottom - origRect.top,
1842                               ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1843         }
1844     }
1845
1846     if (IsIconic(hwnd))
1847     {
1848         /* Single click brings up the system menu when iconized */
1849
1850         if( !moved )
1851         {
1852             if(style & WS_SYSMENU )
1853                 SendMessageW( hwnd, WM_SYSCOMMAND,
1854                               SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
1855         }
1856         else WINPOS_ShowIconTitle( hwnd, TRUE );
1857     }
1858 }