wined3d: Get rid of wined3d_buffer_get_desc().
[wine] / dlls / user32 / tests / win.c
1 /*
2  * Unit tests for window handling
3  *
4  * Copyright 2002 Bill Medland
5  * Copyright 2002 Alexandre Julliard
6  * Copyright 2003 Dmitry Timoshkov
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 /* To get ICON_SMALL2 with the MSVC headers */
24 #define _WIN32_WINNT 0x0501
25
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35
36 #include "wine/test.h"
37
38 #ifndef SPI_GETDESKWALLPAPER
39 #define SPI_GETDESKWALLPAPER 0x0073
40 #endif
41
42 #define LONG_PTR INT_PTR
43 #define ULONG_PTR UINT_PTR
44
45 void dump_region(HRGN hrgn);
46
47 static HWND (WINAPI *pGetAncestor)(HWND,UINT);
48 static BOOL (WINAPI *pGetWindowInfo)(HWND,WINDOWINFO*);
49 static UINT (WINAPI *pGetWindowModuleFileNameA)(HWND,LPSTR,UINT);
50 static BOOL (WINAPI *pGetLayeredWindowAttributes)(HWND,COLORREF*,BYTE*,DWORD*);
51 static BOOL (WINAPI *pSetLayeredWindowAttributes)(HWND,COLORREF,BYTE,DWORD);
52 static BOOL (WINAPI *pUpdateLayeredWindow)(HWND,HDC,POINT*,SIZE*,HDC,POINT*,COLORREF,BLENDFUNCTION*,DWORD);
53 static BOOL (WINAPI *pGetMonitorInfoA)(HMONITOR,LPMONITORINFO);
54 static HMONITOR (WINAPI *pMonitorFromPoint)(POINT,DWORD);
55 static int  (WINAPI *pGetWindowRgnBox)(HWND,LPRECT);
56 static BOOL (WINAPI *pGetGUIThreadInfo)(DWORD, GUITHREADINFO*);
57 static BOOL (WINAPI *pGetProcessDefaultLayout)( DWORD *layout );
58 static BOOL (WINAPI *pSetProcessDefaultLayout)( DWORD layout );
59 static DWORD (WINAPI *pSetLayout)(HDC hdc, DWORD layout);
60 static DWORD (WINAPI *pGetLayout)(HDC hdc);
61 static BOOL (WINAPI *pMirrorRgn)(HWND hwnd, HRGN hrgn);
62
63 static BOOL test_lbuttondown_flag;
64 static HWND hwndMessage;
65 static HWND hwndMain, hwndMain2;
66 static HHOOK hhook;
67
68 static const char* szAWRClass = "Winsize";
69 static HMENU hmenu;
70 static DWORD our_pid;
71
72 static BOOL is_win9x = FALSE;
73
74 #define COUNTOF(arr) (sizeof(arr)/sizeof(arr[0]))
75
76 static void dump_minmax_info( const MINMAXINFO *minmax )
77 {
78     trace("Reserved=%d,%d MaxSize=%d,%d MaxPos=%d,%d MinTrack=%d,%d MaxTrack=%d,%d\n",
79           minmax->ptReserved.x, minmax->ptReserved.y,
80           minmax->ptMaxSize.x, minmax->ptMaxSize.y,
81           minmax->ptMaxPosition.x, minmax->ptMaxPosition.y,
82           minmax->ptMinTrackSize.x, minmax->ptMinTrackSize.y,
83           minmax->ptMaxTrackSize.x, minmax->ptMaxTrackSize.y);
84 }
85
86 /* try to make sure pending X events have been processed before continuing */
87 static void flush_events( BOOL remove_messages )
88 {
89     MSG msg;
90     int diff = 200;
91     int min_timeout = 100;
92     DWORD time = GetTickCount() + diff;
93
94     while (diff > 0)
95     {
96         if (MsgWaitForMultipleObjects( 0, NULL, FALSE, min_timeout, QS_ALLINPUT ) == WAIT_TIMEOUT) break;
97         if (remove_messages)
98             while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
99         diff = time - GetTickCount();
100         min_timeout = 50;
101     }
102 }
103
104 /* check the values returned by the various parent/owner functions on a given window */
105 static void check_parents( HWND hwnd, HWND ga_parent, HWND gwl_parent, HWND get_parent,
106                            HWND gw_owner, HWND ga_root, HWND ga_root_owner )
107 {
108     HWND res;
109
110     if (pGetAncestor)
111     {
112         res = pGetAncestor( hwnd, GA_PARENT );
113         ok( res == ga_parent, "Wrong result for GA_PARENT %p expected %p\n", res, ga_parent );
114     }
115     res = (HWND)GetWindowLongPtrA( hwnd, GWLP_HWNDPARENT );
116     ok( res == gwl_parent, "Wrong result for GWL_HWNDPARENT %p expected %p\n", res, gwl_parent );
117     res = GetParent( hwnd );
118     ok( res == get_parent, "Wrong result for GetParent %p expected %p\n", res, get_parent );
119     res = GetWindow( hwnd, GW_OWNER );
120     ok( res == gw_owner, "Wrong result for GW_OWNER %p expected %p\n", res, gw_owner );
121     if (pGetAncestor)
122     {
123         res = pGetAncestor( hwnd, GA_ROOT );
124         ok( res == ga_root, "Wrong result for GA_ROOT %p expected %p\n", res, ga_root );
125         res = pGetAncestor( hwnd, GA_ROOTOWNER );
126         ok( res == ga_root_owner, "Wrong result for GA_ROOTOWNER %p expected %p\n", res, ga_root_owner );
127     }
128 }
129
130 static BOOL ignore_message( UINT message )
131 {
132     /* these are always ignored */
133     return (message >= 0xc000 ||
134             message == WM_GETICON ||
135             message == WM_GETOBJECT ||
136             message == WM_TIMECHANGE ||
137             message == WM_DEVICECHANGE);
138 }
139
140 static BOOL CALLBACK EnumChildProc( HWND hwndChild, LPARAM lParam)
141 {
142     (*(LPINT)lParam)++;
143     trace("EnumChildProc on %p\n", hwndChild);
144     if (*(LPINT)lParam > 1) return FALSE;
145     return TRUE;
146 }
147
148 /* will search for the given window */
149 static BOOL CALLBACK EnumChildProc1( HWND hwndChild, LPARAM lParam)
150 {
151     trace("EnumChildProc1 on %p\n", hwndChild);
152     if ((HWND)lParam == hwndChild) return FALSE;
153     return TRUE;
154 }
155
156 static HWND create_tool_window( LONG style, HWND parent )
157 {
158     HWND ret = CreateWindowExA(0, "ToolWindowClass", "Tool window 1", style,
159                                0, 0, 100, 100, parent, 0, 0, NULL );
160     ok( ret != 0, "Creation failed\n" );
161     return ret;
162 }
163
164 /* test parent and owner values for various combinations */
165 static void test_parent_owner(void)
166 {
167     LONG style;
168     HWND test, owner, ret;
169     HWND desktop = GetDesktopWindow();
170     HWND child = create_tool_window( WS_CHILD, hwndMain );
171     INT  numChildren;
172
173     trace( "main window %p main2 %p desktop %p child %p\n", hwndMain, hwndMain2, desktop, child );
174
175     /* child without parent, should fail */
176     SetLastError(0xdeadbeef);
177     test = CreateWindowExA(0, "ToolWindowClass", "Tool window 1",
178                            WS_CHILD, 0, 0, 100, 100, 0, 0, 0, NULL );
179     ok( !test, "WS_CHILD without parent created\n" );
180     ok( GetLastError() == ERROR_TLW_WITH_WSCHILD ||
181         broken(GetLastError() == 0xdeadbeef), /* win9x */
182         "CreateWindowExA error %u\n", GetLastError() );
183
184     /* desktop window */
185     check_parents( desktop, 0, 0, 0, 0, 0, 0 );
186     style = GetWindowLongA( desktop, GWL_STYLE );
187     ok( !SetWindowLongA( desktop, GWL_STYLE, WS_POPUP ), "Set GWL_STYLE on desktop succeeded\n" );
188     ok( !SetWindowLongA( desktop, GWL_STYLE, 0 ), "Set GWL_STYLE on desktop succeeded\n" );
189     ok( GetWindowLongA( desktop, GWL_STYLE ) == style, "Desktop style changed\n" );
190
191     /* normal child window */
192     test = create_tool_window( WS_CHILD, hwndMain );
193     trace( "created child %p\n", test );
194     check_parents( test, hwndMain, hwndMain, hwndMain, 0, hwndMain, hwndMain );
195     SetWindowLongA( test, GWL_STYLE, 0 );
196     check_parents( test, hwndMain, hwndMain, 0, 0, hwndMain, test );
197     SetWindowLongA( test, GWL_STYLE, WS_POPUP );
198     check_parents( test, hwndMain, hwndMain, 0, 0, hwndMain, test );
199     SetWindowLongA( test, GWL_STYLE, WS_POPUP|WS_CHILD );
200     check_parents( test, hwndMain, hwndMain, 0, 0, hwndMain, test );
201     SetWindowLongA( test, GWL_STYLE, WS_CHILD );
202     DestroyWindow( test );
203
204     /* normal child window with WS_MAXIMIZE */
205     test = create_tool_window( WS_CHILD | WS_MAXIMIZE, hwndMain );
206     DestroyWindow( test );
207
208     /* normal child window with WS_THICKFRAME */
209     test = create_tool_window( WS_CHILD | WS_THICKFRAME, hwndMain );
210     DestroyWindow( test );
211
212     /* popup window with WS_THICKFRAME */
213     test = create_tool_window( WS_POPUP | WS_THICKFRAME, hwndMain );
214     DestroyWindow( test );
215
216     /* child of desktop */
217     test = create_tool_window( WS_CHILD, desktop );
218     trace( "created child of desktop %p\n", test );
219     check_parents( test, desktop, 0, desktop, 0, test, desktop );
220     SetWindowLongA( test, GWL_STYLE, WS_POPUP );
221     check_parents( test, desktop, 0, 0, 0, test, test );
222     SetWindowLongA( test, GWL_STYLE, 0 );
223     check_parents( test, desktop, 0, 0, 0, test, test );
224     DestroyWindow( test );
225
226     /* child of desktop with WS_MAXIMIZE */
227     test = create_tool_window( WS_CHILD | WS_MAXIMIZE, desktop );
228     DestroyWindow( test );
229
230     /* child of desktop with WS_MINIMIZE */
231     test = create_tool_window( WS_CHILD | WS_MINIMIZE, desktop );
232     DestroyWindow( test );
233
234     /* child of child */
235     test = create_tool_window( WS_CHILD, child );
236     trace( "created child of child %p\n", test );
237     check_parents( test, child, child, child, 0, hwndMain, hwndMain );
238     SetWindowLongA( test, GWL_STYLE, 0 );
239     check_parents( test, child, child, 0, 0, hwndMain, test );
240     SetWindowLongA( test, GWL_STYLE, WS_POPUP );
241     check_parents( test, child, child, 0, 0, hwndMain, test );
242     DestroyWindow( test );
243
244     /* child of child with WS_MAXIMIZE */
245     test = create_tool_window( WS_CHILD | WS_MAXIMIZE, child );
246     DestroyWindow( test );
247
248     /* child of child with WS_MINIMIZE */
249     test = create_tool_window( WS_CHILD | WS_MINIMIZE, child );
250     DestroyWindow( test );
251
252     /* not owned top-level window */
253     test = create_tool_window( 0, 0 );
254     trace( "created top-level %p\n", test );
255     check_parents( test, desktop, 0, 0, 0, test, test );
256     SetWindowLongA( test, GWL_STYLE, WS_POPUP );
257     check_parents( test, desktop, 0, 0, 0, test, test );
258     SetWindowLongA( test, GWL_STYLE, WS_CHILD );
259     check_parents( test, desktop, 0, desktop, 0, test, desktop );
260     DestroyWindow( test );
261
262     /* not owned top-level window with WS_MAXIMIZE */
263     test = create_tool_window( WS_MAXIMIZE, 0 );
264     DestroyWindow( test );
265
266     /* owned top-level window */
267     test = create_tool_window( 0, hwndMain );
268     trace( "created owned top-level %p\n", test );
269     check_parents( test, desktop, hwndMain, 0, hwndMain, test, test );
270     SetWindowLongA( test, GWL_STYLE, WS_POPUP );
271     check_parents( test, desktop, hwndMain, hwndMain, hwndMain, test, hwndMain );
272     SetWindowLongA( test, GWL_STYLE, WS_CHILD );
273     check_parents( test, desktop, hwndMain, desktop, hwndMain, test, desktop );
274     DestroyWindow( test );
275
276     /* owned top-level window with WS_MAXIMIZE */
277     test = create_tool_window( WS_MAXIMIZE, hwndMain );
278     DestroyWindow( test );
279
280     /* not owned popup */
281     test = create_tool_window( WS_POPUP, 0 );
282     trace( "created popup %p\n", test );
283     check_parents( test, desktop, 0, 0, 0, test, test );
284     SetWindowLongA( test, GWL_STYLE, WS_CHILD );
285     check_parents( test, desktop, 0, desktop, 0, test, desktop );
286     SetWindowLongA( test, GWL_STYLE, 0 );
287     check_parents( test, desktop, 0, 0, 0, test, test );
288     DestroyWindow( test );
289
290     /* not owned popup with WS_MAXIMIZE */
291     test = create_tool_window( WS_POPUP | WS_MAXIMIZE, 0 );
292     DestroyWindow( test );
293
294     /* owned popup */
295     test = create_tool_window( WS_POPUP, hwndMain );
296     trace( "created owned popup %p\n", test );
297     check_parents( test, desktop, hwndMain, hwndMain, hwndMain, test, hwndMain );
298     SetWindowLongA( test, GWL_STYLE, WS_CHILD );
299     check_parents( test, desktop, hwndMain, desktop, hwndMain, test, desktop );
300     SetWindowLongA( test, GWL_STYLE, 0 );
301     check_parents( test, desktop, hwndMain, 0, hwndMain, test, test );
302     DestroyWindow( test );
303
304     /* owned popup with WS_MAXIMIZE */
305     test = create_tool_window( WS_POPUP | WS_MAXIMIZE, hwndMain );
306     DestroyWindow( test );
307
308     /* top-level window owned by child (same as owned by top-level) */
309     test = create_tool_window( 0, child );
310     trace( "created top-level owned by child %p\n", test );
311     check_parents( test, desktop, hwndMain, 0, hwndMain, test, test );
312     DestroyWindow( test );
313
314     /* top-level window owned by child (same as owned by top-level) with WS_MAXIMIZE */
315     test = create_tool_window( WS_MAXIMIZE, child );
316     DestroyWindow( test );
317
318     /* popup owned by desktop (same as not owned) */
319     test = create_tool_window( WS_POPUP, desktop );
320     trace( "created popup owned by desktop %p\n", test );
321     check_parents( test, desktop, 0, 0, 0, test, test );
322     DestroyWindow( test );
323
324     /* popup owned by desktop (same as not owned) with WS_MAXIMIZE */
325     test = create_tool_window( WS_POPUP | WS_MAXIMIZE, desktop );
326     DestroyWindow( test );
327
328     /* popup owned by child (same as owned by top-level) */
329     test = create_tool_window( WS_POPUP, child );
330     trace( "created popup owned by child %p\n", test );
331     check_parents( test, desktop, hwndMain, hwndMain, hwndMain, test, hwndMain );
332     DestroyWindow( test );
333
334     /* popup owned by child (same as owned by top-level) with WS_MAXIMIZE */
335     test = create_tool_window( WS_POPUP | WS_MAXIMIZE, child );
336     DestroyWindow( test );
337
338     /* not owned popup with WS_CHILD (same as WS_POPUP only) */
339     test = create_tool_window( WS_POPUP | WS_CHILD, 0 );
340     trace( "created WS_CHILD popup %p\n", test );
341     check_parents( test, desktop, 0, 0, 0, test, test );
342     DestroyWindow( test );
343
344     /* not owned popup with WS_CHILD | WS_MAXIMIZE (same as WS_POPUP only) */
345     test = create_tool_window( WS_POPUP | WS_CHILD | WS_MAXIMIZE, 0 );
346     DestroyWindow( test );
347
348     /* owned popup with WS_CHILD (same as WS_POPUP only) */
349     test = create_tool_window( WS_POPUP | WS_CHILD, hwndMain );
350     trace( "created owned WS_CHILD popup %p\n", test );
351     check_parents( test, desktop, hwndMain, hwndMain, hwndMain, test, hwndMain );
352     DestroyWindow( test );
353
354     /* owned popup with WS_CHILD (same as WS_POPUP only) with WS_MAXIMIZE */
355     test = create_tool_window( WS_POPUP | WS_CHILD | WS_MAXIMIZE, hwndMain );
356     DestroyWindow( test );
357
358     /******************** parent changes *************************/
359     trace( "testing parent changes\n" );
360
361     /* desktop window */
362     check_parents( desktop, 0, 0, 0, 0, 0, 0 );
363     if (0)
364     {
365     /* this test succeeds on NT but crashes on win9x systems */
366     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)hwndMain2 );
367     ok( !ret, "Set GWL_HWNDPARENT succeeded on desktop\n" );
368     check_parents( desktop, 0, 0, 0, 0, 0, 0 );
369     ok( !SetParent( desktop, hwndMain ), "SetParent succeeded on desktop\n" );
370     check_parents( desktop, 0, 0, 0, 0, 0, 0 );
371     }
372     /* normal child window */
373     test = create_tool_window( WS_CHILD, hwndMain );
374     trace( "created child %p\n", test );
375
376     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)hwndMain2 );
377     ok( ret == hwndMain, "GWL_HWNDPARENT return value %p expected %p\n", ret, hwndMain );
378     check_parents( test, hwndMain2, hwndMain2, hwndMain2, 0, hwndMain2, hwndMain2 );
379
380     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)child );
381     ok( ret == hwndMain2, "GWL_HWNDPARENT return value %p expected %p\n", ret, hwndMain2 );
382     check_parents( test, child, child, child, 0, hwndMain, hwndMain );
383
384     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)desktop );
385     ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
386     check_parents( test, desktop, 0, desktop, 0, test, desktop );
387
388     /* window is now child of desktop so GWLP_HWNDPARENT changes owner from now on */
389     if (!is_win9x)
390     {
391         ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)test );
392         ok( ret == 0, "GWL_HWNDPARENT return value %p expected 0\n", ret );
393         check_parents( test, desktop, 0, desktop, 0, test, desktop );
394     }
395     else
396         win_skip("Test creates circular window tree under Win9x/WinMe\n" );
397
398     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)child );
399     ok( ret == 0, "GWL_HWNDPARENT return value %p expected 0\n", ret );
400     check_parents( test, desktop, child, desktop, child, test, desktop );
401
402     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, 0 );
403     ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
404     check_parents( test, desktop, 0, desktop, 0, test, desktop );
405     DestroyWindow( test );
406
407     /* not owned top-level window */
408     test = create_tool_window( 0, 0 );
409     trace( "created top-level %p\n", test );
410
411     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)hwndMain2 );
412     ok( ret == 0, "GWL_HWNDPARENT return value %p expected 0\n", ret );
413     check_parents( test, desktop, hwndMain2, 0, hwndMain2, test, test );
414
415     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)child );
416     ok( ret == hwndMain2, "GWL_HWNDPARENT return value %p expected %p\n", ret, hwndMain2 );
417     check_parents( test, desktop, child, 0, child, test, test );
418
419     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, 0 );
420     ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
421     check_parents( test, desktop, 0, 0, 0, test, test );
422     DestroyWindow( test );
423
424     /* not owned popup */
425     test = create_tool_window( WS_POPUP, 0 );
426     trace( "created popup %p\n", test );
427
428     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)hwndMain2 );
429     ok( ret == 0, "GWL_HWNDPARENT return value %p expected 0\n", ret );
430     check_parents( test, desktop, hwndMain2, hwndMain2, hwndMain2, test, hwndMain2 );
431
432     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)child );
433     ok( ret == hwndMain2, "GWL_HWNDPARENT return value %p expected %p\n", ret, hwndMain2 );
434     check_parents( test, desktop, child, child, child, test, hwndMain );
435
436     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, 0 );
437     ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
438     check_parents( test, desktop, 0, 0, 0, test, test );
439     DestroyWindow( test );
440
441     /* normal child window */
442     test = create_tool_window( WS_CHILD, hwndMain );
443     trace( "created child %p\n", test );
444
445     ret = SetParent( test, desktop );
446     ok( ret == hwndMain, "SetParent return value %p expected %p\n", ret, hwndMain );
447     check_parents( test, desktop, 0, desktop, 0, test, desktop );
448
449     ret = SetParent( test, child );
450     ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
451     check_parents( test, child, child, child, 0, hwndMain, hwndMain );
452
453     ret = SetParent( test, hwndMain2 );
454     ok( ret == child, "SetParent return value %p expected %p\n", ret, child );
455     check_parents( test, hwndMain2, hwndMain2, hwndMain2, 0, hwndMain2, hwndMain2 );
456     DestroyWindow( test );
457
458     /* not owned top-level window */
459     test = create_tool_window( 0, 0 );
460     trace( "created top-level %p\n", test );
461
462     ret = SetParent( test, child );
463     ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
464     check_parents( test, child, child, 0, 0, hwndMain, test );
465
466     if (!is_win9x)
467     {
468         ShowWindow( test, SW_SHOW );
469         ret = SetParent( test, test );
470         ok( ret == NULL, "SetParent return value %p expected %p\n", ret, NULL );
471         ok( GetWindowLongA( test, GWL_STYLE ) & WS_VISIBLE, "window is not visible after SetParent\n" );
472         check_parents( test, child, child, 0, 0, hwndMain, test );
473     }
474     else
475         win_skip( "Test crashes on Win9x/WinMe\n" );
476     DestroyWindow( test );
477
478     /* owned popup */
479     test = create_tool_window( WS_POPUP, hwndMain2 );
480     trace( "created owned popup %p\n", test );
481
482     ret = SetParent( test, child );
483     ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
484     check_parents( test, child, child, hwndMain2, hwndMain2, hwndMain, hwndMain2 );
485
486     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (ULONG_PTR)hwndMain );
487     ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
488     check_parents( test, hwndMain, hwndMain, hwndMain2, hwndMain2, hwndMain, hwndMain2 );
489     DestroyWindow( test );
490
491     /**************** test owner destruction *******************/
492
493     /* owned child popup */
494     owner = create_tool_window( 0, 0 );
495     test = create_tool_window( WS_POPUP, owner );
496     trace( "created owner %p and popup %p\n", owner, test );
497     ret = SetParent( test, child );
498     ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
499     check_parents( test, child, child, owner, owner, hwndMain, owner );
500     /* window is now child of 'child' but owned by 'owner' */
501     DestroyWindow( owner );
502     ok( IsWindow(test), "Window %p destroyed by owner destruction\n", test );
503     /* Win98 doesn't pass this test. It doesn't allow a destroyed owner,
504      * while Win95, Win2k, WinXP do.
505      */
506     /*check_parents( test, child, child, owner, owner, hwndMain, owner );*/
507     ok( !IsWindow(owner), "Owner %p not destroyed\n", owner );
508     DestroyWindow(test);
509
510     /* owned top-level popup */
511     owner = create_tool_window( 0, 0 );
512     test = create_tool_window( WS_POPUP, owner );
513     trace( "created owner %p and popup %p\n", owner, test );
514     check_parents( test, desktop, owner, owner, owner, test, owner );
515     DestroyWindow( owner );
516     ok( !IsWindow(test), "Window %p not destroyed by owner destruction\n", test );
517
518     /* top-level popup owned by child */
519     owner = create_tool_window( WS_CHILD, hwndMain2 );
520     test = create_tool_window( WS_POPUP, 0 );
521     trace( "created owner %p and popup %p\n", owner, test );
522     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (ULONG_PTR)owner );
523     ok( ret == 0, "GWL_HWNDPARENT return value %p expected 0\n", ret );
524     check_parents( test, desktop, owner, owner, owner, test, hwndMain2 );
525     DestroyWindow( owner );
526     ok( IsWindow(test), "Window %p destroyed by owner destruction\n", test );
527     ok( !IsWindow(owner), "Owner %p not destroyed\n", owner );
528     /* Win98 doesn't pass this test. It doesn't allow a destroyed owner,
529      * while Win95, Win2k, WinXP do.
530      */
531     /*check_parents( test, desktop, owner, owner, owner, test, owner );*/
532     DestroyWindow(test);
533
534     /* final cleanup */
535     DestroyWindow(child);
536
537
538     owner = create_tool_window( WS_OVERLAPPED, 0 );
539     test = create_tool_window( WS_POPUP, desktop );
540
541     ok( !GetWindow( test, GW_OWNER ), "Wrong owner window\n" );
542     numChildren = 0;
543     ok( !EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
544         "EnumChildWindows should have returned FALSE\n" );
545     ok( numChildren == 0, "numChildren should be 0 got %d\n", numChildren );
546
547     SetWindowLongA( test, GWL_STYLE, (GetWindowLongA( test, GWL_STYLE ) & ~WS_POPUP) | WS_CHILD );
548     ret = SetParent( test, owner );
549     ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
550
551     numChildren = 0;
552     ok( EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
553         "EnumChildWindows should have returned TRUE\n" );
554     ok( numChildren == 1, "numChildren should be 1 got %d\n", numChildren );
555
556     child = create_tool_window( WS_CHILD, owner );
557     numChildren = 0;
558     ok( !EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
559         "EnumChildWindows should have returned FALSE\n" );
560     ok( numChildren == 2, "numChildren should be 2 got %d\n", numChildren );
561     DestroyWindow( child );
562
563     child = create_tool_window( WS_VISIBLE | WS_OVERLAPPEDWINDOW, owner );
564     ok( GetWindow( child, GW_OWNER ) == owner, "Wrong owner window\n" );
565     numChildren = 0;
566     ok( EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
567         "EnumChildWindows should have returned TRUE\n" );
568     ok( numChildren == 1, "numChildren should be 1 got %d\n", numChildren );
569
570     ret = SetParent( child, owner );
571     ok( GetWindow( child, GW_OWNER ) == owner, "Wrong owner window\n" );
572     ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
573     numChildren = 0;
574     ok( !EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
575         "EnumChildWindows should have returned FALSE\n" );
576     ok( numChildren == 2, "numChildren should be 2 got %d\n", numChildren );
577
578     ret = SetParent( child, NULL );
579     ok( GetWindow( child, GW_OWNER ) == owner, "Wrong owner window\n" );
580     ok( ret == owner, "SetParent return value %p expected %p\n", ret, owner );
581     numChildren = 0;
582     ok( EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
583         "EnumChildWindows should have returned TRUE\n" );
584     ok( numChildren == 1, "numChildren should be 1 got %d\n", numChildren );
585
586     /* even GW_OWNER == owner it's still a desktop's child */
587     ok( !EnumChildWindows( desktop, EnumChildProc1, (LPARAM)child ),
588         "EnumChildWindows should have found %p and returned FALSE\n", child );
589
590     DestroyWindow( child );
591     child = create_tool_window( WS_VISIBLE | WS_OVERLAPPEDWINDOW, NULL );
592
593     ok( !EnumChildWindows( desktop, EnumChildProc1, (LPARAM)child ),
594         "EnumChildWindows should have found %p and returned FALSE\n", child );
595
596     DestroyWindow( child );
597     DestroyWindow( test );
598     DestroyWindow( owner );
599 }
600
601 static BOOL CALLBACK enum_proc( HWND hwnd, LPARAM lParam)
602 {
603     (*(LPINT)lParam)++;
604     if (*(LPINT)lParam > 2) return FALSE;
605     return TRUE;
606 }
607 static DWORD CALLBACK enum_thread( void *arg )
608 {
609     INT count;
610     HWND hwnd[3];
611     BOOL ret;
612     MSG msg;
613
614     if (pGetGUIThreadInfo)
615     {
616         GUITHREADINFO info;
617         info.cbSize = sizeof(info);
618         ret = pGetGUIThreadInfo( GetCurrentThreadId(), &info );
619         ok( ret || broken(!ret), /* win9x */
620             "GetGUIThreadInfo failed without message queue\n" );
621         SetLastError( 0xdeadbeef );
622         info.cbSize = sizeof(info) + 1;
623         ret = pGetGUIThreadInfo( GetCurrentThreadId(), &info );
624         ok( !ret, "GetGUIThreadInfo succeeded with wrong size\n" );
625         ok( GetLastError() == ERROR_INVALID_PARAMETER ||
626             broken(GetLastError() == 0xdeadbeef), /* win9x */
627             "wrong error %u\n", GetLastError() );
628     }
629
630     PeekMessage( &msg, 0, 0, 0, PM_NOREMOVE );  /* make sure we have a message queue */
631
632     count = 0;
633     ret = EnumThreadWindows( GetCurrentThreadId(), enum_proc, (LPARAM)&count );
634     ok( ret, "EnumThreadWindows should have returned TRUE\n" );
635     ok( count == 0, "count should be 0 got %d\n", count );
636
637     hwnd[0] = CreateWindowExA(0, "ToolWindowClass", "Tool window 1", WS_POPUP,
638                               0, 0, 100, 100, 0, 0, 0, NULL );
639     count = 0;
640     ret = EnumThreadWindows( GetCurrentThreadId(), enum_proc, (LPARAM)&count );
641     ok( ret, "EnumThreadWindows should have returned TRUE\n" );
642     if (count != 2)  /* Vista gives us two windows for the price of one */
643     {
644         ok( count == 1, "count should be 1 got %d\n", count );
645         hwnd[2] = CreateWindowExA(0, "ToolWindowClass", "Tool window 2", WS_POPUP,
646                                   0, 0, 100, 100, 0, 0, 0, NULL );
647     }
648     else hwnd[2] = 0;
649
650     hwnd[1] = CreateWindowExA(0, "ToolWindowClass", "Tool window 3", WS_POPUP,
651                               0, 0, 100, 100, 0, 0, 0, NULL );
652     count = 0;
653     ret = EnumThreadWindows( GetCurrentThreadId(), enum_proc, (LPARAM)&count );
654     ok( !ret, "EnumThreadWindows should have returned FALSE\n" );
655     ok( count == 3, "count should be 3 got %d\n", count );
656
657     if (hwnd[2]) DestroyWindow(hwnd[2]);
658     DestroyWindow(hwnd[1]);
659     DestroyWindow(hwnd[0]);
660     return 0;
661 }
662
663 /* test EnumThreadWindows in a separate thread */
664 static void test_enum_thread_windows(void)
665 {
666     DWORD id;
667     HANDLE handle = CreateThread( NULL, 0, enum_thread, 0, 0, &id );
668     ok( !WaitForSingleObject( handle, 10000 ), "wait failed\n" );
669     CloseHandle( handle );
670 }
671
672 static LRESULT WINAPI main_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
673 {
674     switch (msg)
675     {
676         case WM_GETMINMAXINFO:
677         {
678             MINMAXINFO* minmax = (MINMAXINFO *)lparam;
679
680             trace("WM_GETMINMAXINFO: hwnd %p, %08lx, %08lx\n", hwnd, wparam, lparam);
681             dump_minmax_info( minmax );
682             SetWindowLongPtrA(hwnd, GWLP_USERDATA, 0x20031021);
683             break;
684         }
685         case WM_WINDOWPOSCHANGING:
686         {
687             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
688             trace("main: WM_WINDOWPOSCHANGING %p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
689                    winpos->hwnd, winpos->hwndInsertAfter,
690                    winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
691             if (!(winpos->flags & SWP_NOMOVE))
692             {
693                 ok(winpos->x >= -32768 && winpos->x <= 32767, "bad winpos->x %d\n", winpos->x);
694                 ok(winpos->y >= -32768 && winpos->y <= 32767, "bad winpos->y %d\n", winpos->y);
695             }
696             /* Win9x does not fixup cx/xy for WM_WINDOWPOSCHANGING */
697             if (!(winpos->flags & SWP_NOSIZE) && !is_win9x)
698             {
699                 ok((winpos->cx >= 0 && winpos->cx <= 32767) ||
700                    winpos->cx == 32768, /* win7 doesn't truncate */
701                    "bad winpos->cx %d\n", winpos->cx);
702                 ok((winpos->cy >= 0 && winpos->cy <= 32767) ||
703                    winpos->cy == 40000, /* win7 doesn't truncate */
704                    "bad winpos->cy %d\n", winpos->cy);
705             }
706             break;
707         }
708         case WM_WINDOWPOSCHANGED:
709         {
710             RECT rc1, rc2;
711             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
712             trace("main: WM_WINDOWPOSCHANGED %p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
713                    winpos->hwnd, winpos->hwndInsertAfter,
714                    winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
715             ok(winpos->x >= -32768 && winpos->x <= 32767, "bad winpos->x %d\n", winpos->x);
716             ok(winpos->y >= -32768 && winpos->y <= 32767, "bad winpos->y %d\n", winpos->y);
717
718             ok((winpos->cx >= 0 && winpos->cx <= 32767) ||
719                winpos->cx == 32768, /* win7 doesn't truncate */
720                "bad winpos->cx %d\n", winpos->cx);
721             ok((winpos->cy >= 0 && winpos->cy <= 32767) ||
722                winpos->cy == 40000, /* win7 doesn't truncate */
723                "bad winpos->cy %d\n", winpos->cy);
724
725             GetWindowRect(hwnd, &rc1);
726             SetRect(&rc2, winpos->x, winpos->y, winpos->x + winpos->cx, winpos->y + winpos->cy);
727             /* note: winpos coordinates are relative to parent */
728             MapWindowPoints(GetParent(hwnd), 0, (LPPOINT)&rc2, 2);
729             if (0)
730             {
731             /* Uncomment this once the test succeeds in all cases */
732             ok(EqualRect(&rc1, &rc2), "rects do not match (%d,%d-%d,%d) / (%d,%d-%d,%d)\n",
733                rc1.left, rc1.top, rc1.right, rc1.bottom, rc2.left, rc2.top, rc2.right, rc2.bottom );
734
735             GetClientRect(hwnd, &rc2);
736             DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc1);
737             MapWindowPoints(0, hwnd, (LPPOINT)&rc1, 2);
738             ok(EqualRect(&rc1, &rc2), "rects do not match (%d,%d-%d,%d) / (%d,%d-%d,%d)\n",
739                rc1.left, rc1.top, rc1.right, rc1.bottom, rc2.left, rc2.top, rc2.right, rc2.bottom );
740             }
741             break;
742         }
743         case WM_NCCREATE:
744         {
745             BOOL got_getminmaxinfo = GetWindowLongPtrA(hwnd, GWLP_USERDATA) == 0x20031021;
746             CREATESTRUCTA *cs = (CREATESTRUCTA *)lparam;
747
748             trace("WM_NCCREATE: hwnd %p, parent %p, style %08x\n", hwnd, cs->hwndParent, cs->style);
749             if (got_getminmaxinfo)
750                 trace("%p got WM_GETMINMAXINFO\n", hwnd);
751
752             if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
753                 ok(got_getminmaxinfo, "main: WM_GETMINMAXINFO should have been received before WM_NCCREATE\n");
754             else
755                 ok(!got_getminmaxinfo, "main: WM_GETMINMAXINFO should NOT have been received before WM_NCCREATE\n");
756             break;
757         }
758         case WM_COMMAND:
759             if (test_lbuttondown_flag)
760             {
761                 ShowWindow((HWND)wparam, SW_SHOW);
762                 flush_events( FALSE );
763             }
764             break;
765     }
766
767     return DefWindowProcA(hwnd, msg, wparam, lparam);
768 }
769
770 static LRESULT WINAPI tool_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
771 {
772     switch (msg)
773     {
774         case WM_GETMINMAXINFO:
775         {
776             MINMAXINFO* minmax = (MINMAXINFO *)lparam;
777
778             trace("hwnd %p, WM_GETMINMAXINFO, %08lx, %08lx\n", hwnd, wparam, lparam);
779             dump_minmax_info( minmax );
780             SetWindowLongPtrA(hwnd, GWLP_USERDATA, 0x20031021);
781             break;
782         }
783         case WM_NCCREATE:
784         {
785             BOOL got_getminmaxinfo = GetWindowLongPtrA(hwnd, GWLP_USERDATA) == 0x20031021;
786             CREATESTRUCTA *cs = (CREATESTRUCTA *)lparam;
787
788             trace("WM_NCCREATE: hwnd %p, parent %p, style %08x\n", hwnd, cs->hwndParent, cs->style);
789             if (got_getminmaxinfo)
790                 trace("%p got WM_GETMINMAXINFO\n", hwnd);
791
792             if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
793                 ok(got_getminmaxinfo, "tool: WM_GETMINMAXINFO should have been received before WM_NCCREATE\n");
794             else
795                 ok(!got_getminmaxinfo, "tool: WM_GETMINMAXINFO should NOT have been received before WM_NCCREATE\n");
796             break;
797         }
798     }
799
800     return DefWindowProcA(hwnd, msg, wparam, lparam);
801 }
802
803 static BOOL RegisterWindowClasses(void)
804 {
805     WNDCLASSA cls;
806
807     cls.style = CS_DBLCLKS;
808     cls.lpfnWndProc = main_window_procA;
809     cls.cbClsExtra = 0;
810     cls.cbWndExtra = 0;
811     cls.hInstance = GetModuleHandleA(0);
812     cls.hIcon = 0;
813     cls.hCursor = LoadCursorA(0, IDC_ARROW);
814     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
815     cls.lpszMenuName = NULL;
816     cls.lpszClassName = "MainWindowClass";
817
818     if(!RegisterClassA(&cls)) return FALSE;
819
820     cls.style = 0;
821     cls.lpfnWndProc = tool_window_procA;
822     cls.cbClsExtra = 0;
823     cls.cbWndExtra = 0;
824     cls.hInstance = GetModuleHandleA(0);
825     cls.hIcon = 0;
826     cls.hCursor = LoadCursorA(0, IDC_ARROW);
827     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
828     cls.lpszMenuName = NULL;
829     cls.lpszClassName = "ToolWindowClass";
830
831     if(!RegisterClassA(&cls)) return FALSE;
832
833     return TRUE;
834 }
835
836 static void verify_window_info(const char *hook, HWND hwnd, const WINDOWINFO *info)
837 {
838     RECT rcWindow, rcClient;
839     DWORD status;
840
841     ok(IsWindow(hwnd), "bad window handle %p in hook %s\n", hwnd, hook);
842
843     GetWindowRect(hwnd, &rcWindow);
844     ok(EqualRect(&rcWindow, &info->rcWindow), "wrong rcWindow for %p in hook %s\n", hwnd, hook);
845
846     GetClientRect(hwnd, &rcClient);
847     /* translate to screen coordinates */
848     MapWindowPoints(hwnd, 0, (LPPOINT)&rcClient, 2);
849     ok(EqualRect(&rcClient, &info->rcClient), "wrong rcClient for %p in hook %s\n", hwnd, hook);
850
851     ok(info->dwStyle == (DWORD)GetWindowLongA(hwnd, GWL_STYLE),
852        "wrong dwStyle: %08x != %08x for %p in hook %s\n",
853        info->dwStyle, GetWindowLongA(hwnd, GWL_STYLE), hwnd, hook);
854     /* Windows reports some undocumented exstyles in WINDOWINFO, but
855      * doesn't return them in GetWindowLong(hwnd, GWL_EXSTYLE).
856      */
857     ok((info->dwExStyle & ~0xe0000800) == (DWORD)GetWindowLongA(hwnd, GWL_EXSTYLE),
858        "wrong dwExStyle: %08x != %08x for %p in hook %s\n",
859        info->dwExStyle, GetWindowLongA(hwnd, GWL_EXSTYLE), hwnd, hook);
860     status = (GetActiveWindow() == hwnd) ? WS_ACTIVECAPTION : 0;
861     if (GetForegroundWindow())
862         ok(info->dwWindowStatus == status, "wrong dwWindowStatus: %04x != %04x active %p fg %p in hook %s\n",
863            info->dwWindowStatus, status, GetActiveWindow(), GetForegroundWindow(), hook);
864
865     /* win2k and XP return broken border info in GetWindowInfo most of
866      * the time, so there is no point in testing it.
867      */
868 #if 0
869     UINT border;
870     ok(info->cxWindowBorders == (unsigned)(rcClient.left - rcWindow.left),
871        "wrong cxWindowBorders %d != %d\n", info->cxWindowBorders, rcClient.left - rcWindow.left);
872     border = min(rcWindow.bottom - rcClient.bottom, rcClient.top - rcWindow.top);
873     ok(info->cyWindowBorders == border,
874        "wrong cyWindowBorders %d != %d\n", info->cyWindowBorders, border);
875 #endif
876     ok(info->atomWindowType == GetClassLongA(hwnd, GCW_ATOM), "wrong atomWindowType for %p in hook %s\n",
877        hwnd, hook);
878     ok(info->wCreatorVersion == 0x0400 /* NT4, Win2000, XP, Win2003 */ ||
879        info->wCreatorVersion == 0x0500 /* Vista */,
880        "wrong wCreatorVersion %04x for %p in hook %s\n", info->wCreatorVersion, hwnd, hook);
881 }
882
883 static void FixedAdjustWindowRectEx(RECT* rc, LONG style, BOOL menu, LONG exstyle)
884 {
885     AdjustWindowRectEx(rc, style, menu, exstyle);
886     /* AdjustWindowRectEx does not include scroll bars */
887     if (style & WS_VSCROLL)
888     {
889         if(exstyle & WS_EX_LEFTSCROLLBAR)
890             rc->left  -= GetSystemMetrics(SM_CXVSCROLL);
891         else
892             rc->right += GetSystemMetrics(SM_CXVSCROLL);
893     }
894     if (style & WS_HSCROLL)
895         rc->bottom += GetSystemMetrics(SM_CYHSCROLL);
896 }
897
898 static void test_nonclient_area(HWND hwnd)
899 {
900     DWORD style, exstyle;
901     RECT rc_window, rc_client, rc;
902     BOOL menu;
903     LRESULT ret;
904
905     style = GetWindowLongA(hwnd, GWL_STYLE);
906     exstyle = GetWindowLongA(hwnd, GWL_EXSTYLE);
907     menu = !(style & WS_CHILD) && GetMenu(hwnd) != 0;
908
909     GetWindowRect(hwnd, &rc_window);
910     GetClientRect(hwnd, &rc_client);
911
912     /* avoid some cases when things go wrong */
913     if (IsRectEmpty(&rc_window) || IsRectEmpty(&rc_client) ||
914         rc_window.right > 32768 || rc_window.bottom > 32768) return;
915
916     CopyRect(&rc, &rc_client);
917     MapWindowPoints(hwnd, 0, (LPPOINT)&rc, 2);
918     FixedAdjustWindowRectEx(&rc, style, menu, exstyle);
919
920     ok(EqualRect(&rc, &rc_window),
921        "window rect does not match: style:exstyle=0x%08x:0x%08x, menu=%d, win=(%d,%d)-(%d,%d), calc=(%d,%d)-(%d,%d)\n",
922        style, exstyle, menu, rc_window.left, rc_window.top, rc_window.right, rc_window.bottom,
923        rc.left, rc.top, rc.right, rc.bottom);
924
925
926     CopyRect(&rc, &rc_window);
927     DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc);
928     MapWindowPoints(0, hwnd, (LPPOINT)&rc, 2);
929     ok(EqualRect(&rc, &rc_client),
930        "client rect does not match: style:exstyle=0x%08x:0x%08x, menu=%d client=(%d,%d)-(%d,%d), calc=(%d,%d)-(%d,%d)\n",
931        style, exstyle, menu, rc_client.left, rc_client.top, rc_client.right, rc_client.bottom,
932        rc.left, rc.top, rc.right, rc.bottom);
933
934     /* NULL rectangle shouldn't crash */
935     ret = DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, 0);
936     ok(ret == 0, "NULL rectangle returned %ld instead of 0\n", ret);
937
938     /* Win9x doesn't like WM_NCCALCSIZE with synthetic data and crashes */;
939     if (is_win9x)
940         return;
941
942     /* and now test AdjustWindowRectEx and WM_NCCALCSIZE on synthetic data */
943     SetRect(&rc_client, 0, 0, 250, 150);
944     CopyRect(&rc_window, &rc_client);
945     MapWindowPoints(hwnd, 0, (LPPOINT)&rc_window, 2);
946     FixedAdjustWindowRectEx(&rc_window, style, menu, exstyle);
947
948     CopyRect(&rc, &rc_window);
949     DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc);
950     MapWindowPoints(0, hwnd, (LPPOINT)&rc, 2);
951     ok(EqualRect(&rc, &rc_client),
952        "synthetic rect does not match: style:exstyle=0x%08x:0x%08x, menu=%d, client=(%d,%d)-(%d,%d), calc=(%d,%d)-(%d,%d)\n",
953        style, exstyle, menu, rc_client.left, rc_client.top, rc_client.right, rc_client.bottom,
954        rc.left, rc.top, rc.right, rc.bottom);
955 }
956
957 static LRESULT CALLBACK cbt_hook_proc(int nCode, WPARAM wParam, LPARAM lParam) 
958
959     static const char *CBT_code_name[10] = {
960         "HCBT_MOVESIZE",
961         "HCBT_MINMAX",
962         "HCBT_QS",
963         "HCBT_CREATEWND",
964         "HCBT_DESTROYWND",
965         "HCBT_ACTIVATE",
966         "HCBT_CLICKSKIPPED",
967         "HCBT_KEYSKIPPED",
968         "HCBT_SYSCOMMAND",
969         "HCBT_SETFOCUS" };
970     const char *code_name = (nCode >= 0 && nCode <= HCBT_SETFOCUS) ? CBT_code_name[nCode] : "Unknown";
971     HWND hwnd = (HWND)wParam;
972
973     switch (nCode)
974     {
975     case HCBT_CREATEWND:
976         {
977             static const RECT rc_null;
978             RECT rc;
979             LONG style;
980             CBT_CREATEWNDA *createwnd = (CBT_CREATEWNDA *)lParam;
981             ok(createwnd->hwndInsertAfter == HWND_TOP, "hwndInsertAfter should be always HWND_TOP\n");
982
983             if (pGetWindowInfo)
984             {
985                 WINDOWINFO info;
986                 info.cbSize = sizeof(WINDOWINFO);
987                 ok(pGetWindowInfo(hwnd, &info), "GetWindowInfo should not fail\n");
988                 verify_window_info(code_name, hwnd, &info);
989             }
990
991             /* WS_VISIBLE should be turned off yet */
992             style = createwnd->lpcs->style & ~WS_VISIBLE;
993             ok(style == GetWindowLongA(hwnd, GWL_STYLE),
994                 "style of hwnd and style in the CREATESTRUCT do not match: %08x != %08x\n",
995                 GetWindowLongA(hwnd, GWL_STYLE), style);
996
997             if (0)
998             {
999             /* Uncomment this once the test succeeds in all cases */
1000             if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD)
1001             {
1002                 ok(GetParent(hwnd) == hwndMessage,
1003                    "wrong result from GetParent %p: message window %p\n",
1004                    GetParent(hwnd), hwndMessage);
1005             }
1006             else
1007                 ok(!GetParent(hwnd), "GetParent should return 0 at this point\n");
1008
1009             ok(!GetWindow(hwnd, GW_OWNER), "GW_OWNER should be set to 0 at this point\n");
1010             }
1011             if (0)
1012             {
1013             /* while NT assigns GW_HWNDFIRST/LAST some values at this point,
1014              * Win9x still has them set to 0.
1015              */
1016             ok(GetWindow(hwnd, GW_HWNDFIRST) != 0, "GW_HWNDFIRST should not be set to 0 at this point\n");
1017             ok(GetWindow(hwnd, GW_HWNDLAST) != 0, "GW_HWNDLAST should not be set to 0 at this point\n");
1018             }
1019             ok(!GetWindow(hwnd, GW_HWNDPREV), "GW_HWNDPREV should be set to 0 at this point\n");
1020             ok(!GetWindow(hwnd, GW_HWNDNEXT), "GW_HWNDNEXT should be set to 0 at this point\n");
1021
1022             if (0)
1023             {
1024             /* Uncomment this once the test succeeds in all cases */
1025             if (pGetAncestor)
1026             {
1027                 ok(pGetAncestor(hwnd, GA_PARENT) == hwndMessage, "GA_PARENT should be set to hwndMessage at this point\n");
1028                 ok(pGetAncestor(hwnd, GA_ROOT) == hwnd,
1029                    "GA_ROOT is set to %p, expected %p\n", pGetAncestor(hwnd, GA_ROOT), hwnd);
1030
1031                 if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD)
1032                     ok(pGetAncestor(hwnd, GA_ROOTOWNER) == hwndMessage,
1033                        "GA_ROOTOWNER should be set to hwndMessage at this point\n");
1034                 else
1035                     ok(pGetAncestor(hwnd, GA_ROOTOWNER) == hwnd,
1036                        "GA_ROOTOWNER is set to %p, expected %p\n", pGetAncestor(hwnd, GA_ROOTOWNER), hwnd);
1037             }
1038
1039             ok(GetWindowRect(hwnd, &rc), "GetWindowRect failed\n");
1040             ok(EqualRect(&rc, &rc_null), "window rect should be set to 0 HCBT_CREATEWND\n");
1041             ok(GetClientRect(hwnd, &rc), "GetClientRect failed\n");
1042             ok(EqualRect(&rc, &rc_null), "client rect should be set to 0 on HCBT_CREATEWND\n");
1043             }
1044             break;
1045         }
1046     case HCBT_MOVESIZE:
1047     case HCBT_MINMAX:
1048     case HCBT_ACTIVATE:
1049         if (pGetWindowInfo && IsWindow(hwnd))
1050         {
1051             WINDOWINFO info;
1052
1053             /* Win98 actually does check the info.cbSize and doesn't allow
1054              * it to be anything except sizeof(WINDOWINFO), while Win95, Win2k,
1055              * WinXP do not check it at all.
1056              */
1057             info.cbSize = sizeof(WINDOWINFO);
1058             ok(pGetWindowInfo(hwnd, &info), "GetWindowInfo should not fail\n");
1059             verify_window_info(code_name, hwnd, &info);
1060         }
1061         break;
1062     /* window state is undefined */
1063     case HCBT_SETFOCUS:
1064     case HCBT_DESTROYWND:
1065         break;
1066     default:
1067         break;
1068     }
1069
1070     return CallNextHookEx(hhook, nCode, wParam, lParam);
1071 }
1072
1073 static void test_shell_window(void)
1074 {
1075     BOOL ret;
1076     DWORD error;
1077     HMODULE hinst, hUser32;
1078     BOOL (WINAPI*SetShellWindow)(HWND);
1079     HWND hwnd1, hwnd2, hwnd3, hwnd4, hwnd5;
1080     HWND shellWindow, nextWnd;
1081
1082     if (is_win9x)
1083     {
1084         win_skip("Skipping shell window test on Win9x\n");
1085         return;
1086     }
1087
1088     shellWindow = GetShellWindow();
1089     hinst = GetModuleHandle(0);
1090     hUser32 = GetModuleHandleA("user32");
1091
1092     SetShellWindow = (void *)GetProcAddress(hUser32, "SetShellWindow");
1093
1094     trace("previous shell window: %p\n", shellWindow);
1095
1096     if (shellWindow) {
1097         DWORD pid;
1098         HANDLE hProcess;
1099
1100         GetWindowThreadProcessId(shellWindow, &pid);
1101         hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
1102         if (!hProcess)
1103         {
1104             skip( "cannot get access to shell process\n" );
1105             return;
1106         }
1107
1108         SetLastError(0xdeadbeef);
1109         ret = DestroyWindow(shellWindow);
1110         error = GetLastError();
1111
1112         ok(!ret, "DestroyWindow(shellWindow)\n");
1113         /* passes on Win XP, but not on Win98 */
1114         ok(error==ERROR_ACCESS_DENIED || error == 0xdeadbeef,
1115            "got %u after DestroyWindow(shellWindow)\n", error);
1116
1117         /* close old shell instance */
1118         ret = TerminateProcess(hProcess, 0);
1119         ok(ret, "termination of previous shell process failed: GetLastError()=%d\n", GetLastError());
1120         WaitForSingleObject(hProcess, INFINITE);    /* wait for termination */
1121         CloseHandle(hProcess);
1122     }
1123
1124     hwnd1 = CreateWindowEx(0, TEXT("#32770"), TEXT("TEST1"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 100, 100, 300, 200, 0, 0, hinst, 0);
1125     trace("created window 1: %p\n", hwnd1);
1126
1127     ret = SetShellWindow(hwnd1);
1128     ok(ret, "first call to SetShellWindow(hwnd1)\n");
1129     shellWindow = GetShellWindow();
1130     ok(shellWindow==hwnd1, "wrong shell window: %p\n", shellWindow);
1131
1132     ret = SetShellWindow(hwnd1);
1133     ok(!ret, "second call to SetShellWindow(hwnd1)\n");
1134
1135     ret = SetShellWindow(0);
1136     error = GetLastError();
1137     /* passes on Win XP, but not on Win98
1138     ok(!ret, "reset shell window by SetShellWindow(0)\n");
1139     ok(error==ERROR_INVALID_WINDOW_HANDLE, "ERROR_INVALID_WINDOW_HANDLE after SetShellWindow(0)\n"); */
1140
1141     ret = SetShellWindow(hwnd1);
1142     /* passes on Win XP, but not on Win98
1143     ok(!ret, "third call to SetShellWindow(hwnd1)\n"); */
1144
1145     SetWindowLong(hwnd1, GWL_EXSTYLE, GetWindowLong(hwnd1,GWL_EXSTYLE)|WS_EX_TOPMOST);
1146     ret = GetWindowLong(hwnd1,GWL_EXSTYLE)&WS_EX_TOPMOST? TRUE: FALSE;
1147     ok(!ret, "SetWindowExStyle(hwnd1, WS_EX_TOPMOST)\n");
1148
1149     ret = DestroyWindow(hwnd1);
1150     ok(ret, "DestroyWindow(hwnd1)\n");
1151
1152     hwnd2 = CreateWindowEx(WS_EX_TOPMOST, TEXT("#32770"), TEXT("TEST2"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 150, 250, 300, 200, 0, 0, hinst, 0);
1153     trace("created window 2: %p\n", hwnd2);
1154     ret = SetShellWindow(hwnd2);
1155     ok(!ret, "SetShellWindow(hwnd2) with WS_EX_TOPMOST\n");
1156
1157     hwnd3 = CreateWindowEx(0, TEXT("#32770"), TEXT("TEST3"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 200, 400, 300, 200, 0, 0, hinst, 0);
1158     trace("created window 3: %p\n", hwnd3);
1159
1160     hwnd4 = CreateWindowEx(0, TEXT("#32770"), TEXT("TEST4"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 250, 500, 300, 200, 0, 0, hinst, 0);
1161     trace("created window 4: %p\n", hwnd4);
1162
1163     nextWnd = GetWindow(hwnd4, GW_HWNDNEXT);
1164     ok(nextWnd==hwnd3, "wrong next window for hwnd4: %p - expected hwnd3\n", nextWnd);
1165
1166     ret = SetShellWindow(hwnd4);
1167     ok(ret, "SetShellWindow(hwnd4)\n");
1168     shellWindow = GetShellWindow();
1169     ok(shellWindow==hwnd4, "wrong shell window: %p - expected hwnd4\n", shellWindow);
1170
1171     nextWnd = GetWindow(hwnd4, GW_HWNDNEXT);
1172     ok(nextWnd==0, "wrong next window for hwnd4: %p - expected 0\n", nextWnd);
1173
1174     ret = SetWindowPos(hwnd4, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
1175     ok(ret, "SetWindowPos(hwnd4, HWND_TOPMOST)\n");
1176
1177     ret = SetWindowPos(hwnd4, hwnd3, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
1178     ok(ret, "SetWindowPos(hwnd4, hwnd3\n");
1179
1180     ret = SetShellWindow(hwnd3);
1181     ok(!ret, "SetShellWindow(hwnd3)\n");
1182     shellWindow = GetShellWindow();
1183     ok(shellWindow==hwnd4, "wrong shell window: %p - expected hwnd4\n", shellWindow);
1184
1185     hwnd5 = CreateWindowEx(0, TEXT("#32770"), TEXT("TEST5"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 300, 600, 300, 200, 0, 0, hinst, 0);
1186     trace("created window 5: %p\n", hwnd5);
1187     ret = SetWindowPos(hwnd4, hwnd5, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
1188     ok(ret, "SetWindowPos(hwnd4, hwnd5)\n");
1189
1190     todo_wine
1191     {
1192         nextWnd = GetWindow(hwnd4, GW_HWNDNEXT);
1193         ok(nextWnd==0, "wrong next window for hwnd4 after SetWindowPos(): %p - expected 0\n", nextWnd);
1194     }
1195
1196     /* destroy test windows */
1197     DestroyWindow(hwnd2);
1198     DestroyWindow(hwnd3);
1199     DestroyWindow(hwnd4);
1200     DestroyWindow(hwnd5);
1201 }
1202
1203 /************** MDI test ****************/
1204
1205 static char mdi_lParam_test_message[] = "just a test string";
1206
1207 static void test_MDI_create(HWND parent, HWND mdi_client, INT_PTR first_id)
1208 {
1209     MDICREATESTRUCTA mdi_cs;
1210     HWND mdi_child;
1211     INT_PTR id;
1212     static const WCHAR classW[] = {'M','D','I','_','c','h','i','l','d','_','C','l','a','s','s','_','1',0};
1213     static const WCHAR titleW[] = {'M','D','I',' ','c','h','i','l','d',0};
1214     BOOL isWin9x = FALSE;
1215
1216     mdi_cs.szClass = "MDI_child_Class_1";
1217     mdi_cs.szTitle = "MDI child";
1218     mdi_cs.hOwner = GetModuleHandle(0);
1219     mdi_cs.x = CW_USEDEFAULT;
1220     mdi_cs.y = CW_USEDEFAULT;
1221     mdi_cs.cx = CW_USEDEFAULT;
1222     mdi_cs.cy = CW_USEDEFAULT;
1223     mdi_cs.style = 0;
1224     mdi_cs.lParam = (LPARAM)mdi_lParam_test_message;
1225     mdi_child = (HWND)SendMessageA(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
1226     ok(mdi_child != 0, "MDI child creation failed\n");
1227     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1228     ok(id == first_id, "wrong child id %ld\n", id);
1229     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1230     ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1231
1232     mdi_cs.style = 0x7fffffff; /* without WS_POPUP */
1233     mdi_child = (HWND)SendMessageA(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
1234     ok(mdi_child != 0, "MDI child creation failed\n");
1235     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1236     ok(id == first_id, "wrong child id %ld\n", id);
1237     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1238     ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1239
1240     mdi_cs.style = 0xffffffff; /* with WS_POPUP */
1241     mdi_child = (HWND)SendMessageA(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
1242     if (GetWindowLongA(mdi_client, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1243     {
1244         ok(!mdi_child, "MDI child with WS_POPUP and with MDIS_ALLCHILDSTYLES should fail\n");
1245     }
1246     else
1247     {
1248         ok(mdi_child != 0, "MDI child creation failed\n");
1249         id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1250         ok(id == first_id, "wrong child id %ld\n", id);
1251         SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1252         ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1253     }
1254
1255     /* test MDICREATESTRUCT A<->W mapping */
1256     /* MDICREATESTRUCTA and MDICREATESTRUCTW have the same layout */
1257     mdi_cs.style = 0;
1258     mdi_cs.szClass = (LPCSTR)classW;
1259     mdi_cs.szTitle = (LPCSTR)titleW;
1260     SetLastError(0xdeadbeef);
1261     mdi_child = (HWND)SendMessageW(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
1262     if (!mdi_child)
1263     {
1264         if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1265             isWin9x = TRUE;
1266         else
1267             ok(mdi_child != 0, "MDI child creation failed\n");
1268     }
1269     else
1270     {
1271         id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1272         ok(id == first_id, "wrong child id %ld\n", id);
1273         SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1274         ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1275     }
1276
1277     mdi_child = CreateMDIWindowA("MDI_child_Class_1", "MDI child",
1278                                  0,
1279                                  CW_USEDEFAULT, CW_USEDEFAULT,
1280                                  CW_USEDEFAULT, CW_USEDEFAULT,
1281                                  mdi_client, GetModuleHandle(0),
1282                                  (LPARAM)mdi_lParam_test_message);
1283     ok(mdi_child != 0, "MDI child creation failed\n");
1284     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1285     ok(id == first_id, "wrong child id %ld\n", id);
1286     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1287     ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1288
1289     mdi_child = CreateMDIWindowA("MDI_child_Class_1", "MDI child",
1290                                  0x7fffffff, /* without WS_POPUP */
1291                                  CW_USEDEFAULT, CW_USEDEFAULT,
1292                                  CW_USEDEFAULT, CW_USEDEFAULT,
1293                                  mdi_client, GetModuleHandle(0),
1294                                  (LPARAM)mdi_lParam_test_message);
1295     ok(mdi_child != 0, "MDI child creation failed\n");
1296     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1297     ok(id == first_id, "wrong child id %ld\n", id);
1298     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1299     ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1300
1301     mdi_child = CreateMDIWindowA("MDI_child_Class_1", "MDI child",
1302                                  0xffffffff, /* with WS_POPUP */
1303                                  CW_USEDEFAULT, CW_USEDEFAULT,
1304                                  CW_USEDEFAULT, CW_USEDEFAULT,
1305                                  mdi_client, GetModuleHandle(0),
1306                                  (LPARAM)mdi_lParam_test_message);
1307     if (GetWindowLongA(mdi_client, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1308     {
1309         ok(!mdi_child, "MDI child with WS_POPUP and with MDIS_ALLCHILDSTYLES should fail\n");
1310     }
1311     else
1312     {
1313         ok(mdi_child != 0, "MDI child creation failed\n");
1314         id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1315         ok(id == first_id, "wrong child id %ld\n", id);
1316         SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1317         ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1318     }
1319
1320     /* test MDICREATESTRUCT A<->W mapping */
1321     SetLastError(0xdeadbeef);
1322     mdi_child = CreateMDIWindowW(classW, titleW,
1323                                  0,
1324                                  CW_USEDEFAULT, CW_USEDEFAULT,
1325                                  CW_USEDEFAULT, CW_USEDEFAULT,
1326                                  mdi_client, GetModuleHandle(0),
1327                                  (LPARAM)mdi_lParam_test_message);
1328     if (!mdi_child)
1329     {
1330         if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1331             isWin9x = TRUE;
1332         else
1333             ok(mdi_child != 0, "MDI child creation failed\n");
1334     }
1335     else
1336     {
1337         id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1338         ok(id == first_id, "wrong child id %ld\n", id);
1339         SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1340         ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1341     }
1342
1343     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_1", "MDI child",
1344                                 0,
1345                                 CW_USEDEFAULT, CW_USEDEFAULT,
1346                                 CW_USEDEFAULT, CW_USEDEFAULT,
1347                                 mdi_client, 0, GetModuleHandle(0),
1348                                 mdi_lParam_test_message);
1349     ok(mdi_child != 0, "MDI child creation failed\n");
1350     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1351     ok(id == first_id, "wrong child id %ld\n", id);
1352     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1353     ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1354
1355     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_1", "MDI child",
1356                                 0x7fffffff, /* without WS_POPUP */
1357                                 CW_USEDEFAULT, CW_USEDEFAULT,
1358                                 CW_USEDEFAULT, CW_USEDEFAULT,
1359                                 mdi_client, 0, GetModuleHandle(0),
1360                                 mdi_lParam_test_message);
1361     ok(mdi_child != 0, "MDI child creation failed\n");
1362     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1363     ok(id == first_id, "wrong child id %ld\n", id);
1364     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1365     ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1366
1367     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_1", "MDI child",
1368                                 0xffffffff, /* with WS_POPUP */
1369                                 CW_USEDEFAULT, CW_USEDEFAULT,
1370                                 CW_USEDEFAULT, CW_USEDEFAULT,
1371                                 mdi_client, 0, GetModuleHandle(0),
1372                                 mdi_lParam_test_message);
1373     if (GetWindowLongA(mdi_client, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1374     {
1375         ok(!mdi_child, "MDI child with WS_POPUP and with MDIS_ALLCHILDSTYLES should fail\n");
1376     }
1377     else
1378     {
1379         ok(mdi_child != 0, "MDI child creation failed\n");
1380         id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1381         ok(id == first_id, "wrong child id %ld\n", id);
1382         SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1383         ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1384     }
1385
1386     /* test MDICREATESTRUCT A<->W mapping */
1387     SetLastError(0xdeadbeef);
1388     mdi_child = CreateWindowExW(WS_EX_MDICHILD, classW, titleW,
1389                                 0,
1390                                 CW_USEDEFAULT, CW_USEDEFAULT,
1391                                 CW_USEDEFAULT, CW_USEDEFAULT,
1392                                 mdi_client, 0, GetModuleHandle(0),
1393                                 mdi_lParam_test_message);
1394     if (!mdi_child)
1395     {
1396         if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1397             isWin9x = TRUE;
1398         else
1399             ok(mdi_child != 0, "MDI child creation failed\n");
1400     }
1401     else
1402     {
1403         id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1404         ok(id == first_id, "wrong child id %ld\n", id);
1405         SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1406         ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1407     }
1408
1409     /* This test fails on Win9x */
1410     if (!isWin9x)
1411     {
1412         mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_2", "MDI child",
1413                                 WS_CHILD,
1414                                 CW_USEDEFAULT, CW_USEDEFAULT,
1415                                 CW_USEDEFAULT, CW_USEDEFAULT,
1416                                 parent, 0, GetModuleHandle(0),
1417                                 mdi_lParam_test_message);
1418         ok(!mdi_child, "WS_EX_MDICHILD with a not MDIClient parent should fail\n");
1419     }
1420
1421     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1422                                 WS_CHILD, /* without WS_POPUP */
1423                                 CW_USEDEFAULT, CW_USEDEFAULT,
1424                                 CW_USEDEFAULT, CW_USEDEFAULT,
1425                                 mdi_client, 0, GetModuleHandle(0),
1426                                 mdi_lParam_test_message);
1427     ok(mdi_child != 0, "MDI child creation failed\n");
1428     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1429     ok(id == 0, "wrong child id %ld\n", id);
1430     DestroyWindow(mdi_child);
1431
1432     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1433                                 WS_CHILD | WS_POPUP, /* with WS_POPUP */
1434                                 CW_USEDEFAULT, CW_USEDEFAULT,
1435                                 CW_USEDEFAULT, CW_USEDEFAULT,
1436                                 mdi_client, 0, GetModuleHandle(0),
1437                                 mdi_lParam_test_message);
1438     ok(mdi_child != 0, "MDI child creation failed\n");
1439     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1440     ok(id == 0, "wrong child id %ld\n", id);
1441     DestroyWindow(mdi_child);
1442
1443     /* maximized child */
1444     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1445                                 WS_CHILD | WS_MAXIMIZE,
1446                                 CW_USEDEFAULT, CW_USEDEFAULT,
1447                                 CW_USEDEFAULT, CW_USEDEFAULT,
1448                                 mdi_client, 0, GetModuleHandle(0),
1449                                 mdi_lParam_test_message);
1450     ok(mdi_child != 0, "MDI child creation failed\n");
1451     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1452     ok(id == 0, "wrong child id %ld\n", id);
1453     DestroyWindow(mdi_child);
1454
1455     trace("Creating maximized child with a caption\n");
1456     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1457                                 WS_CHILD | WS_MAXIMIZE | WS_CAPTION,
1458                                 CW_USEDEFAULT, CW_USEDEFAULT,
1459                                 CW_USEDEFAULT, CW_USEDEFAULT,
1460                                 mdi_client, 0, GetModuleHandle(0),
1461                                 mdi_lParam_test_message);
1462     ok(mdi_child != 0, "MDI child creation failed\n");
1463     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1464     ok(id == 0, "wrong child id %ld\n", id);
1465     DestroyWindow(mdi_child);
1466
1467     trace("Creating maximized child with a caption and a thick frame\n");
1468     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1469                                 WS_CHILD | WS_MAXIMIZE | WS_CAPTION | WS_THICKFRAME,
1470                                 CW_USEDEFAULT, CW_USEDEFAULT,
1471                                 CW_USEDEFAULT, CW_USEDEFAULT,
1472                                 mdi_client, 0, GetModuleHandle(0),
1473                                 mdi_lParam_test_message);
1474     ok(mdi_child != 0, "MDI child creation failed\n");
1475     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1476     ok(id == 0, "wrong child id %ld\n", id);
1477     DestroyWindow(mdi_child);
1478 }
1479
1480 /**********************************************************************
1481  * MDI_ChildGetMinMaxInfo (copied from windows/mdi.c)
1482  *
1483  * Note: The rule here is that client rect of the maximized MDI child
1484  *       is equal to the client rect of the MDI client window.
1485  */
1486 static void MDI_ChildGetMinMaxInfo( HWND client, HWND hwnd, MINMAXINFO* lpMinMax )
1487 {
1488     RECT rect;
1489
1490     GetClientRect( client, &rect );
1491     AdjustWindowRectEx( &rect, GetWindowLongA( hwnd, GWL_STYLE ),
1492                         0, GetWindowLongA( hwnd, GWL_EXSTYLE ));
1493
1494     rect.right -= rect.left;
1495     rect.bottom -= rect.top;
1496     lpMinMax->ptMaxSize.x = rect.right;
1497     lpMinMax->ptMaxSize.y = rect.bottom;
1498
1499     lpMinMax->ptMaxPosition.x = rect.left;
1500     lpMinMax->ptMaxPosition.y = rect.top;
1501
1502     trace("max rect (%d,%d - %d, %d)\n",
1503            rect.left, rect.top, rect.right, rect.bottom);
1504 }
1505
1506 static LRESULT WINAPI mdi_child_wnd_proc_1(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1507 {
1508     switch (msg)
1509     {
1510         case WM_NCCREATE:
1511         case WM_CREATE:
1512         {
1513             CREATESTRUCTA *cs = (CREATESTRUCTA *)lparam;
1514             MDICREATESTRUCTA *mdi_cs = cs->lpCreateParams;
1515
1516             ok(cs->dwExStyle & WS_EX_MDICHILD, "WS_EX_MDICHILD should be set\n");
1517             ok(mdi_cs->lParam == (LPARAM)mdi_lParam_test_message, "wrong mdi_cs->lParam\n");
1518
1519             ok(!lstrcmpA(cs->lpszClass, "MDI_child_Class_1"), "wrong class name\n");
1520             ok(!lstrcmpA(cs->lpszClass, mdi_cs->szClass), "class name does not match\n");
1521             ok(!lstrcmpA(cs->lpszName, "MDI child"), "wrong title\n");
1522             ok(!lstrcmpA(cs->lpszName, mdi_cs->szTitle), "title does not match\n");
1523             ok(cs->hInstance == mdi_cs->hOwner, "%p != %p\n", cs->hInstance, mdi_cs->hOwner);
1524
1525             /* MDICREATESTRUCT should have original values */
1526             ok(mdi_cs->style == 0 || mdi_cs->style == 0x7fffffff || mdi_cs->style == 0xffffffff,
1527                 "mdi_cs->style does not match (%08x)\n", mdi_cs->style);
1528             ok(mdi_cs->x == CW_USEDEFAULT, "%d != CW_USEDEFAULT\n", mdi_cs->x);
1529             ok(mdi_cs->y == CW_USEDEFAULT, "%d != CW_USEDEFAULT\n", mdi_cs->y);
1530             ok(mdi_cs->cx == CW_USEDEFAULT, "%d != CW_USEDEFAULT\n", mdi_cs->cx);
1531             ok(mdi_cs->cy == CW_USEDEFAULT, "%d != CW_USEDEFAULT\n", mdi_cs->cy);
1532
1533             /* CREATESTRUCT should have fixed values */
1534             ok(cs->x != CW_USEDEFAULT, "%d == CW_USEDEFAULT\n", cs->x);
1535             ok(cs->y != CW_USEDEFAULT, "%d == CW_USEDEFAULT\n", cs->y);
1536
1537             /* cx/cy == CW_USEDEFAULT are translated to NOT zero values */
1538             ok(cs->cx != CW_USEDEFAULT && cs->cx != 0, "%d == CW_USEDEFAULT\n", cs->cx);
1539             ok(cs->cy != CW_USEDEFAULT && cs->cy != 0, "%d == CW_USEDEFAULT\n", cs->cy);
1540
1541             ok(!(cs->style & WS_POPUP), "WS_POPUP is not allowed\n");
1542
1543             if (GetWindowLongA(cs->hwndParent, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1544             {
1545                 LONG style = mdi_cs->style | WS_CHILD | WS_CLIPSIBLINGS;
1546                 ok(cs->style == style,
1547                    "cs->style does not match (%08x)\n", cs->style);
1548             }
1549             else
1550             {
1551                 LONG style = mdi_cs->style;
1552                 style &= ~WS_POPUP;
1553                 style |= WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CAPTION |
1554                     WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
1555                 ok(cs->style == style,
1556                    "cs->style does not match (%08x)\n", cs->style);
1557             }
1558             break;
1559         }
1560
1561         case WM_GETMINMAXINFO:
1562         {
1563             HWND client = GetParent(hwnd);
1564             RECT rc;
1565             MINMAXINFO *minmax = (MINMAXINFO *)lparam;
1566             MINMAXINFO my_minmax;
1567             LONG style, exstyle;
1568
1569             style = GetWindowLongA(hwnd, GWL_STYLE);
1570             exstyle = GetWindowLongA(hwnd, GWL_EXSTYLE);
1571
1572             GetWindowRect(client, &rc);
1573             trace("MDI client %p window size = (%d x %d)\n", client, rc.right-rc.left, rc.bottom-rc.top);
1574             GetClientRect(client, &rc);
1575             trace("MDI client %p client size = (%d x %d)\n", client, rc.right, rc.bottom);
1576             trace("screen size: %d x %d\n", GetSystemMetrics(SM_CXSCREEN),
1577                                             GetSystemMetrics(SM_CYSCREEN));
1578
1579             GetClientRect(client, &rc);
1580             if ((style & WS_CAPTION) == WS_CAPTION)
1581                 style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
1582             AdjustWindowRectEx(&rc, style, 0, exstyle);
1583             trace("MDI child: calculated max window size = (%d x %d)\n", rc.right-rc.left, rc.bottom-rc.top);
1584             dump_minmax_info( minmax );
1585
1586             ok(minmax->ptMaxSize.x == rc.right - rc.left, "default width of maximized child %d != %d\n",
1587                minmax->ptMaxSize.x, rc.right - rc.left);
1588             ok(minmax->ptMaxSize.y == rc.bottom - rc.top, "default height of maximized child %d != %d\n",
1589                minmax->ptMaxSize.y, rc.bottom - rc.top);
1590
1591             DefMDIChildProcA(hwnd, msg, wparam, lparam);
1592
1593             trace("DefMDIChildProc returned:\n");
1594             dump_minmax_info( minmax );
1595
1596             MDI_ChildGetMinMaxInfo(client, hwnd, &my_minmax);
1597             ok(minmax->ptMaxSize.x == my_minmax.ptMaxSize.x, "default width of maximized child %d != %d\n",
1598                minmax->ptMaxSize.x, my_minmax.ptMaxSize.x);
1599             ok(minmax->ptMaxSize.y == my_minmax.ptMaxSize.y, "default height of maximized child %d != %d\n",
1600                minmax->ptMaxSize.y, my_minmax.ptMaxSize.y);
1601
1602             return 1;
1603         }
1604
1605         case WM_MDIACTIVATE:
1606         {
1607             HWND active, client = GetParent(hwnd);
1608             /*trace("%p WM_MDIACTIVATE %08x %08lx\n", hwnd, wparam, lparam);*/
1609             active = (HWND)SendMessageA(client, WM_MDIGETACTIVE, 0, 0);
1610             if (hwnd == (HWND)lparam) /* if we are being activated */
1611                 ok (active == (HWND)lparam, "new active %p != active %p\n", (HWND)lparam, active);
1612             else
1613                 ok (active == (HWND)wparam, "old active %p != active %p\n", (HWND)wparam, active);
1614             break;
1615         }
1616     }
1617     return DefMDIChildProcA(hwnd, msg, wparam, lparam);
1618 }
1619
1620 static LRESULT WINAPI mdi_child_wnd_proc_2(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1621 {
1622     switch (msg)
1623     {
1624         case WM_NCCREATE:
1625         case WM_CREATE:
1626         {
1627             CREATESTRUCTA *cs = (CREATESTRUCTA *)lparam;
1628
1629             trace("%s: x %d, y %d, cx %d, cy %d\n", (msg == WM_NCCREATE) ? "WM_NCCREATE" : "WM_CREATE",
1630                 cs->x, cs->y, cs->cx, cs->cy);
1631
1632             ok(!(cs->dwExStyle & WS_EX_MDICHILD), "WS_EX_MDICHILD should not be set\n");
1633             ok(cs->lpCreateParams == mdi_lParam_test_message, "wrong cs->lpCreateParams\n");
1634
1635             ok(!lstrcmpA(cs->lpszClass, "MDI_child_Class_2"), "wrong class name\n");
1636             ok(!lstrcmpA(cs->lpszName, "MDI child"), "wrong title\n");
1637
1638             /* CREATESTRUCT should have fixed values */
1639             /* For some reason Win9x doesn't translate cs->x from CW_USEDEFAULT,
1640                while NT does. */
1641             /*ok(cs->x != CW_USEDEFAULT, "%d == CW_USEDEFAULT\n", cs->x);*/
1642             ok(cs->y != CW_USEDEFAULT, "%d == CW_USEDEFAULT\n", cs->y);
1643
1644             /* cx/cy == CW_USEDEFAULT are translated to 0 */
1645             /* For some reason Win98 doesn't translate cs->cx from CW_USEDEFAULT,
1646                while Win95, Win2k, WinXP do. */
1647             /*ok(cs->cx == 0, "%d != 0\n", cs->cx);*/
1648             ok(cs->cy == 0, "%d != 0\n", cs->cy);
1649             break;
1650         }
1651
1652         case WM_GETMINMAXINFO:
1653         {
1654             HWND parent = GetParent(hwnd);
1655             RECT rc;
1656             MINMAXINFO *minmax = (MINMAXINFO *)lparam;
1657             LONG style, exstyle;
1658
1659             style = GetWindowLongA(hwnd, GWL_STYLE);
1660             exstyle = GetWindowLongA(hwnd, GWL_EXSTYLE);
1661
1662             GetClientRect(parent, &rc);
1663             trace("WM_GETMINMAXINFO: parent %p client size = (%d x %d)\n", parent, rc.right, rc.bottom);
1664
1665             GetClientRect(parent, &rc);
1666             if ((style & WS_CAPTION) == WS_CAPTION)
1667                 style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
1668             AdjustWindowRectEx(&rc, style, 0, exstyle);
1669             dump_minmax_info( minmax );
1670
1671             ok(minmax->ptMaxSize.x == rc.right - rc.left, "default width of maximized child %d != %d\n",
1672                minmax->ptMaxSize.x, rc.right - rc.left);
1673             ok(minmax->ptMaxSize.y == rc.bottom - rc.top, "default height of maximized child %d != %d\n",
1674                minmax->ptMaxSize.y, rc.bottom - rc.top);
1675             break;
1676         }
1677
1678         case WM_WINDOWPOSCHANGED:
1679         {
1680             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
1681             RECT rc1, rc2;
1682
1683             GetWindowRect(hwnd, &rc1);
1684             SetRect(&rc2, winpos->x, winpos->y, winpos->x + winpos->cx, winpos->y + winpos->cy);
1685             /* note: winpos coordinates are relative to parent */
1686             MapWindowPoints(GetParent(hwnd), 0, (LPPOINT)&rc2, 2);
1687             ok(EqualRect(&rc1, &rc2), "rects do not match, window=(%d,%d)-(%d,%d) pos=(%d,%d)-(%d,%d)\n",
1688                rc1.left, rc1.top, rc1.right, rc1.bottom,
1689                rc2.left, rc2.top, rc2.right, rc2.bottom);
1690             GetWindowRect(hwnd, &rc1);
1691             GetClientRect(hwnd, &rc2);
1692             DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc1);
1693             MapWindowPoints(0, hwnd, (LPPOINT)&rc1, 2);
1694             ok(EqualRect(&rc1, &rc2), "rects do not match, window=(%d,%d)-(%d,%d) client=(%d,%d)-(%d,%d)\n",
1695                rc1.left, rc1.top, rc1.right, rc1.bottom,
1696                rc2.left, rc2.top, rc2.right, rc2.bottom);
1697         }
1698         /* fall through */
1699         case WM_WINDOWPOSCHANGING:
1700         {
1701             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
1702             WINDOWPOS my_winpos = *winpos;
1703
1704             trace("%s: %p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
1705                   (msg == WM_WINDOWPOSCHANGING) ? "WM_WINDOWPOSCHANGING" : "WM_WINDOWPOSCHANGED",
1706                   winpos->hwnd, winpos->hwndInsertAfter,
1707                   winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
1708
1709             DefWindowProcA(hwnd, msg, wparam, lparam);
1710
1711             ok(!memcmp(&my_winpos, winpos, sizeof(WINDOWPOS)),
1712                "DefWindowProc should not change WINDOWPOS: %p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
1713                   winpos->hwnd, winpos->hwndInsertAfter,
1714                   winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
1715
1716             return 1;
1717         }
1718     }
1719     return DefWindowProcA(hwnd, msg, wparam, lparam);
1720 }
1721
1722 static LRESULT WINAPI mdi_main_wnd_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1723 {
1724     static HWND mdi_client;
1725
1726     switch (msg)
1727     {
1728         case WM_CREATE:
1729         {
1730             CLIENTCREATESTRUCT client_cs;
1731             RECT rc;
1732
1733             GetClientRect(hwnd, &rc);
1734
1735             client_cs.hWindowMenu = 0;
1736             client_cs.idFirstChild = 1;
1737
1738             /* MDIClient without MDIS_ALLCHILDSTYLES */
1739             mdi_client = CreateWindowExA(0, "mdiclient",
1740                                          NULL,
1741                                          WS_CHILD /*| WS_VISIBLE*/,
1742                                           /* tests depend on a not zero MDIClient size */
1743                                          0, 0, rc.right, rc.bottom,
1744                                          hwnd, 0, GetModuleHandle(0),
1745                                          &client_cs);
1746             assert(mdi_client);
1747             test_MDI_create(hwnd, mdi_client, client_cs.idFirstChild);
1748             DestroyWindow(mdi_client);
1749
1750             /* MDIClient with MDIS_ALLCHILDSTYLES */
1751             mdi_client = CreateWindowExA(0, "mdiclient",
1752                                          NULL,
1753                                          WS_CHILD | MDIS_ALLCHILDSTYLES /*| WS_VISIBLE*/,
1754                                           /* tests depend on a not zero MDIClient size */
1755                                          0, 0, rc.right, rc.bottom,
1756                                          hwnd, 0, GetModuleHandle(0),
1757                                          &client_cs);
1758             assert(mdi_client);
1759             test_MDI_create(hwnd, mdi_client, client_cs.idFirstChild);
1760             DestroyWindow(mdi_client);
1761             break;
1762         }
1763
1764         case WM_WINDOWPOSCHANGED:
1765         {
1766             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
1767             RECT rc1, rc2;
1768
1769             GetWindowRect(hwnd, &rc1);
1770             trace("window: (%d,%d)-(%d,%d)\n", rc1.left, rc1.top, rc1.right, rc1.bottom);
1771             SetRect(&rc2, winpos->x, winpos->y, winpos->x + winpos->cx, winpos->y + winpos->cy);
1772             /* note: winpos coordinates are relative to parent */
1773             MapWindowPoints(GetParent(hwnd), 0, (LPPOINT)&rc2, 2);
1774             trace("pos: (%d,%d)-(%d,%d)\n", rc2.left, rc2.top, rc2.right, rc2.bottom);
1775             ok(EqualRect(&rc1, &rc2), "rects do not match\n");
1776
1777             GetWindowRect(hwnd, &rc1);
1778             GetClientRect(hwnd, &rc2);
1779             DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc1);
1780             MapWindowPoints(0, hwnd, (LPPOINT)&rc1, 2);
1781             ok(EqualRect(&rc1, &rc2), "rects do not match\n");
1782         }
1783         /* fall through */
1784         case WM_WINDOWPOSCHANGING:
1785         {
1786             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
1787             WINDOWPOS my_winpos = *winpos;
1788
1789             trace("%s\n", (msg == WM_WINDOWPOSCHANGING) ? "WM_WINDOWPOSCHANGING" : "WM_WINDOWPOSCHANGED");
1790             trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
1791                   winpos->hwnd, winpos->hwndInsertAfter,
1792                   winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
1793
1794             DefWindowProcA(hwnd, msg, wparam, lparam);
1795
1796             trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
1797                   winpos->hwnd, winpos->hwndInsertAfter,
1798                   winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
1799
1800             ok(!memcmp(&my_winpos, winpos, sizeof(WINDOWPOS)),
1801                "DefWindowProc should not change WINDOWPOS values\n");
1802
1803             return 1;
1804         }
1805
1806         case WM_CLOSE:
1807             PostQuitMessage(0);
1808             break;
1809     }
1810     return DefFrameProcA(hwnd, mdi_client, msg, wparam, lparam);
1811 }
1812
1813 static BOOL mdi_RegisterWindowClasses(void)
1814 {
1815     WNDCLASSA cls;
1816
1817     cls.style = 0;
1818     cls.lpfnWndProc = mdi_main_wnd_procA;
1819     cls.cbClsExtra = 0;
1820     cls.cbWndExtra = 0;
1821     cls.hInstance = GetModuleHandleA(0);
1822     cls.hIcon = 0;
1823     cls.hCursor = LoadCursorA(0, IDC_ARROW);
1824     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
1825     cls.lpszMenuName = NULL;
1826     cls.lpszClassName = "MDI_parent_Class";
1827     if(!RegisterClassA(&cls)) return FALSE;
1828
1829     cls.lpfnWndProc = mdi_child_wnd_proc_1;
1830     cls.lpszClassName = "MDI_child_Class_1";
1831     if(!RegisterClassA(&cls)) return FALSE;
1832
1833     cls.lpfnWndProc = mdi_child_wnd_proc_2;
1834     cls.lpszClassName = "MDI_child_Class_2";
1835     if(!RegisterClassA(&cls)) return FALSE;
1836
1837     return TRUE;
1838 }
1839
1840 static void test_mdi(void)
1841 {
1842     HWND mdi_hwndMain;
1843     /*MSG msg;*/
1844
1845     if (!mdi_RegisterWindowClasses()) assert(0);
1846
1847     mdi_hwndMain = CreateWindowExA(0, "MDI_parent_Class", "MDI parent window",
1848                                    WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
1849                                    WS_MAXIMIZEBOX /*| WS_VISIBLE*/,
1850                                    100, 100, CW_USEDEFAULT, CW_USEDEFAULT,
1851                                    GetDesktopWindow(), 0,
1852                                    GetModuleHandle(0), NULL);
1853     assert(mdi_hwndMain);
1854 /*
1855     while(GetMessage(&msg, 0, 0, 0))
1856     {
1857         TranslateMessage(&msg);
1858         DispatchMessage(&msg);
1859     }
1860 */
1861     DestroyWindow(mdi_hwndMain);
1862 }
1863
1864 static void test_icons(void)
1865 {
1866     WNDCLASSEXA cls;
1867     HWND hwnd;
1868     HICON icon = LoadIconA(0, IDI_APPLICATION);
1869     HICON icon2 = LoadIconA(0, IDI_QUESTION);
1870     HICON small_icon = LoadImageA(0, IDI_APPLICATION, IMAGE_ICON,
1871                                   GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED );
1872     HICON res;
1873
1874     cls.cbSize = sizeof(cls);
1875     cls.style = 0;
1876     cls.lpfnWndProc = DefWindowProcA;
1877     cls.cbClsExtra = 0;
1878     cls.cbWndExtra = 0;
1879     cls.hInstance = 0;
1880     cls.hIcon = LoadIconA(0, IDI_HAND);
1881     cls.hIconSm = small_icon;
1882     cls.hCursor = LoadCursorA(0, IDC_ARROW);
1883     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
1884     cls.lpszMenuName = NULL;
1885     cls.lpszClassName = "IconWindowClass";
1886
1887     RegisterClassExA(&cls);
1888
1889     hwnd = CreateWindowExA(0, "IconWindowClass", "icon test", 0,
1890                            100, 100, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, NULL, NULL);
1891     assert( hwnd );
1892
1893     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_BIG, 0 );
1894     ok( res == 0, "wrong big icon %p/0\n", res );
1895     res = (HICON)SendMessageA( hwnd, WM_SETICON, ICON_BIG, (LPARAM)icon );
1896     ok( res == 0, "wrong previous big icon %p/0\n", res );
1897     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_BIG, 0 );
1898     ok( res == icon, "wrong big icon after set %p/%p\n", res, icon );
1899     res = (HICON)SendMessageA( hwnd, WM_SETICON, ICON_BIG, (LPARAM)icon2 );
1900     ok( res == icon, "wrong previous big icon %p/%p\n", res, icon );
1901     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_BIG, 0 );
1902     ok( res == icon2, "wrong big icon after set %p/%p\n", res, icon2 );
1903
1904     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL, 0 );
1905     ok( res == 0, "wrong small icon %p/0\n", res );
1906     /* this test is XP specific */
1907     /*res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL2, 0 );
1908     ok( res != 0, "wrong small icon %p\n", res );*/
1909     res = (HICON)SendMessageA( hwnd, WM_SETICON, ICON_SMALL, (LPARAM)icon );
1910     ok( res == 0, "wrong previous small icon %p/0\n", res );
1911     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL, 0 );
1912     ok( res == icon, "wrong small icon after set %p/%p\n", res, icon );
1913     /* this test is XP specific */
1914     /*res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL2, 0 );
1915     ok( res == icon, "wrong small icon after set %p/%p\n", res, icon );*/
1916     res = (HICON)SendMessageA( hwnd, WM_SETICON, ICON_SMALL, (LPARAM)small_icon );
1917     ok( res == icon, "wrong previous small icon %p/%p\n", res, icon );
1918     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL, 0 );
1919     ok( res == small_icon, "wrong small icon after set %p/%p\n", res, small_icon );
1920     /* this test is XP specific */
1921     /*res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL2, 0 );
1922     ok( res == small_icon, "wrong small icon after set %p/%p\n", res, small_icon );*/
1923
1924     /* make sure the big icon hasn't changed */
1925     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_BIG, 0 );
1926     ok( res == icon2, "wrong big icon after set %p/%p\n", res, icon2 );
1927
1928     DestroyWindow( hwnd );
1929 }
1930
1931 static LRESULT WINAPI nccalcsize_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1932 {
1933     if (msg == WM_NCCALCSIZE)
1934     {
1935         RECT *rect = (RECT *)lparam;
1936         /* first time around increase the rectangle, next time decrease it */
1937         if (rect->left == 100) InflateRect( rect, 10, 10 );
1938         else InflateRect( rect, -10, -10 );
1939         return 0;
1940     }
1941     return DefWindowProc( hwnd, msg, wparam, lparam );
1942 }
1943
1944 static void test_SetWindowPos(HWND hwnd)
1945 {
1946     RECT orig_win_rc, rect;
1947     LONG_PTR old_proc;
1948
1949     SetRect(&rect, 111, 222, 333, 444);
1950     ok(!GetWindowRect(0, &rect), "GetWindowRect succeeded\n");
1951     ok(rect.left == 111 && rect.top == 222 && rect.right == 333 && rect.bottom == 444,
1952        "wrong window rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
1953
1954     SetRect(&rect, 111, 222, 333, 444);
1955     ok(!GetClientRect(0, &rect), "GetClientRect succeeded\n");
1956     ok(rect.left == 111 && rect.top == 222 && rect.right == 333 && rect.bottom == 444,
1957        "wrong window rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
1958
1959     GetWindowRect(hwnd, &orig_win_rc);
1960
1961     old_proc = SetWindowLongPtr( hwnd, GWLP_WNDPROC, (ULONG_PTR)nccalcsize_proc );
1962     SetWindowPos(hwnd, 0, 100, 100, 0, 0, SWP_NOZORDER|SWP_FRAMECHANGED);
1963     GetWindowRect( hwnd, &rect );
1964     ok( rect.left == 100 && rect.top == 100 && rect.right == 100 && rect.bottom == 100,
1965         "invalid window rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
1966     GetClientRect( hwnd, &rect );
1967     MapWindowPoints( hwnd, 0, (POINT *)&rect, 2 );
1968     ok( rect.left == 90 && rect.top == 90 && rect.right == 110 && rect.bottom == 110,
1969         "invalid client rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
1970
1971     SetWindowPos(hwnd, 0, 200, 200, 0, 0, SWP_NOZORDER|SWP_FRAMECHANGED);
1972     GetWindowRect( hwnd, &rect );
1973     ok( rect.left == 200 && rect.top == 200 && rect.right == 200 && rect.bottom == 200,
1974         "invalid window rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
1975     GetClientRect( hwnd, &rect );
1976     MapWindowPoints( hwnd, 0, (POINT *)&rect, 2 );
1977     ok( rect.left == 210 && rect.top == 210 && rect.right == 190 && rect.bottom == 190,
1978         "invalid client rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
1979
1980     SetWindowPos(hwnd, 0, orig_win_rc.left, orig_win_rc.top,
1981                  orig_win_rc.right, orig_win_rc.bottom, 0);
1982     SetWindowLongPtr( hwnd, GWLP_WNDPROC, old_proc );
1983
1984     /* Win9x truncates coordinates to 16-bit irrespectively */
1985     if (!is_win9x)
1986     {
1987         SetWindowPos(hwnd, 0, -32769, -40000, -32769, -90000, SWP_NOMOVE);
1988         SetWindowPos(hwnd, 0, 32768, 40000, 32768, 40000, SWP_NOMOVE);
1989
1990         SetWindowPos(hwnd, 0, -32769, -40000, -32769, -90000, SWP_NOSIZE);
1991         SetWindowPos(hwnd, 0, 32768, 40000, 32768, 40000, SWP_NOSIZE);
1992     }
1993
1994     SetWindowPos(hwnd, 0, orig_win_rc.left, orig_win_rc.top,
1995                  orig_win_rc.right, orig_win_rc.bottom, 0);
1996
1997     ok(!(GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST), "WS_EX_TOPMOST should not be set\n");
1998     SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
1999     ok(GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST, "WS_EX_TOPMOST should be set\n");
2000     SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
2001     ok(GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST, "WS_EX_TOPMOST should be set\n");
2002     SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
2003     ok(!(GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST), "WS_EX_TOPMOST should not be set\n");
2004 }
2005
2006 static void test_SetMenu(HWND parent)
2007 {
2008     HWND child;
2009     HMENU hMenu, ret;
2010     BOOL retok;
2011     DWORD style;
2012
2013     hMenu = CreateMenu();
2014     assert(hMenu);
2015
2016     ok(SetMenu(parent, hMenu), "SetMenu on a top level window should not fail\n");
2017     if (0)
2018     {
2019     /* fails on (at least) Wine, NT4, XP SP2 */
2020     test_nonclient_area(parent); 
2021     }
2022     ret = GetMenu(parent);
2023     ok(ret == hMenu, "unexpected menu id %p\n", ret);
2024     /* test whether we can destroy a menu assigned to a window */
2025     retok = DestroyMenu(hMenu);
2026     ok( retok, "DestroyMenu error %d\n", GetLastError());
2027     retok = IsMenu(hMenu);
2028     ok(!retok || broken(retok) /* nt4 */, "menu handle should be not valid after DestroyMenu\n");
2029     ret = GetMenu(parent);
2030     /* This test fails on Win9x */
2031     if (!is_win9x)
2032         ok(ret == hMenu, "unexpected menu id %p\n", ret);
2033     ok(SetMenu(parent, 0), "SetMenu(0) on a top level window should not fail\n");
2034     test_nonclient_area(parent);
2035
2036     hMenu = CreateMenu();
2037     assert(hMenu);
2038
2039     /* parent */
2040     ret = GetMenu(parent);
2041     ok(ret == 0, "unexpected menu id %p\n", ret);
2042
2043     ok(!SetMenu(parent, (HMENU)20), "SetMenu with invalid menu handle should fail\n");
2044     test_nonclient_area(parent);
2045     ret = GetMenu(parent);
2046     ok(ret == 0, "unexpected menu id %p\n", ret);
2047
2048     ok(SetMenu(parent, hMenu), "SetMenu on a top level window should not fail\n");
2049     if (0)
2050     {
2051     /* fails on (at least) Wine, NT4, XP SP2 */
2052     test_nonclient_area(parent);
2053     }
2054     ret = GetMenu(parent);
2055     ok(ret == hMenu, "unexpected menu id %p\n", ret);
2056
2057     ok(SetMenu(parent, 0), "SetMenu(0) on a top level window should not fail\n");
2058     test_nonclient_area(parent);
2059     ret = GetMenu(parent);
2060     ok(ret == 0, "unexpected menu id %p\n", ret);
2061  
2062     /* child */
2063     child = CreateWindowExA(0, "static", NULL, WS_CHILD, 0, 0, 0, 0, parent, (HMENU)10, 0, NULL);
2064     assert(child);
2065
2066     ret = GetMenu(child);
2067     ok(ret == (HMENU)10, "unexpected menu id %p\n", ret);
2068
2069     ok(!SetMenu(child, (HMENU)20), "SetMenu with invalid menu handle should fail\n");
2070     test_nonclient_area(child);
2071     ret = GetMenu(child);
2072     ok(ret == (HMENU)10, "unexpected menu id %p\n", ret);
2073
2074     ok(!SetMenu(child, hMenu), "SetMenu on a child window should fail\n");
2075     test_nonclient_area(child);
2076     ret = GetMenu(child);
2077     ok(ret == (HMENU)10, "unexpected menu id %p\n", ret);
2078
2079     ok(!SetMenu(child, 0), "SetMenu(0) on a child window should fail\n");
2080     test_nonclient_area(child);
2081     ret = GetMenu(child);
2082     ok(ret == (HMENU)10, "unexpected menu id %p\n", ret);
2083
2084     style = GetWindowLong(child, GWL_STYLE);
2085     SetWindowLong(child, GWL_STYLE, style | WS_POPUP);
2086     ok(SetMenu(child, hMenu), "SetMenu on a popup child window should not fail\n");
2087     ok(SetMenu(child, 0), "SetMenu on a popup child window should not fail\n");
2088     SetWindowLong(child, GWL_STYLE, style);
2089
2090     SetWindowLong(child, GWL_STYLE, style | WS_OVERLAPPED);
2091     ok(!SetMenu(child, hMenu), "SetMenu on a overlapped child window should fail\n");
2092     SetWindowLong(child, GWL_STYLE, style);
2093
2094     DestroyWindow(child);
2095     DestroyMenu(hMenu);
2096 }
2097
2098 static void test_window_tree(HWND parent, const DWORD *style, const int *order, int total)
2099 {
2100     HWND child[5], hwnd;
2101     INT_PTR i;
2102
2103     assert(total <= 5);
2104
2105     hwnd = GetWindow(parent, GW_CHILD);
2106     ok(!hwnd, "have to start without children to perform the test\n");
2107
2108     for (i = 0; i < total; i++)
2109     {
2110         if (style[i] & DS_CONTROL)
2111         {
2112             child[i] = CreateWindowExA(0, MAKEINTATOM(32770), "", style[i] & ~WS_VISIBLE,
2113                                        0,0,0,0, parent, (HMENU)i, 0, NULL);
2114             if (style[i] & WS_VISIBLE)
2115                 ShowWindow(child[i], SW_SHOW);
2116
2117             SetWindowPos(child[i], HWND_BOTTOM, 0,0,10,10, SWP_NOACTIVATE);
2118         }
2119         else
2120             child[i] = CreateWindowExA(0, "static", "", style[i], 0,0,10,10,
2121                                        parent, (HMENU)i, 0, NULL);
2122         trace("child[%ld] = %p\n", i, child[i]);
2123         ok(child[i] != 0, "CreateWindowEx failed to create child window\n");
2124     }
2125
2126     hwnd = GetWindow(parent, GW_CHILD);
2127     ok(hwnd != 0, "GetWindow(GW_CHILD) failed\n");
2128     ok(hwnd == GetWindow(child[total - 1], GW_HWNDFIRST), "GW_HWNDFIRST is wrong\n");
2129     ok(child[order[total - 1]] == GetWindow(child[0], GW_HWNDLAST), "GW_HWNDLAST is wrong\n");
2130
2131     for (i = 0; i < total; i++)
2132     {
2133         trace("hwnd[%ld] = %p\n", i, hwnd);
2134         ok(child[order[i]] == hwnd, "Z order of child #%ld is wrong\n", i);
2135
2136         hwnd = GetWindow(hwnd, GW_HWNDNEXT);
2137     }
2138
2139     for (i = 0; i < total; i++)
2140         ok(DestroyWindow(child[i]), "DestroyWindow failed\n");
2141 }
2142
2143 static void test_children_zorder(HWND parent)
2144 {
2145     const DWORD simple_style[5] = { WS_CHILD, WS_CHILD, WS_CHILD, WS_CHILD,
2146                                     WS_CHILD };
2147     const int simple_order[5] = { 0, 1, 2, 3, 4 };
2148
2149     const DWORD complex_style[5] = { WS_CHILD, WS_CHILD | WS_MAXIMIZE,
2150                              WS_CHILD | WS_VISIBLE, WS_CHILD,
2151                              WS_CHILD | WS_MAXIMIZE | WS_VISIBLE };
2152     const int complex_order_1[1] = { 0 };
2153     const int complex_order_2[2] = { 1, 0 };
2154     const int complex_order_3[3] = { 1, 0, 2 };
2155     const int complex_order_4[4] = { 1, 0, 2, 3 };
2156     const int complex_order_5[5] = { 4, 1, 0, 2, 3 };
2157     const DWORD complex_style_6[3] = { WS_CHILD | WS_VISIBLE,
2158                                        WS_CHILD | WS_CLIPSIBLINGS | DS_CONTROL | WS_VISIBLE,
2159                                        WS_CHILD | WS_VISIBLE };
2160     const int complex_order_6[3] = { 0, 1, 2 };
2161
2162     /* simple WS_CHILD */
2163     test_window_tree(parent, simple_style, simple_order, 5);
2164
2165     /* complex children styles */
2166     test_window_tree(parent, complex_style, complex_order_1, 1);
2167     test_window_tree(parent, complex_style, complex_order_2, 2);
2168     test_window_tree(parent, complex_style, complex_order_3, 3);
2169     test_window_tree(parent, complex_style, complex_order_4, 4);
2170     test_window_tree(parent, complex_style, complex_order_5, 5);
2171
2172     /* another set of complex children styles */
2173     test_window_tree(parent, complex_style_6, complex_order_6, 3);
2174 }
2175
2176 #define check_z_order(hwnd, next, prev, owner, topmost) \
2177         check_z_order_debug((hwnd), (next), (prev), (owner), (topmost), \
2178                             __FILE__, __LINE__)
2179
2180 static void check_z_order_debug(HWND hwnd, HWND next, HWND prev, HWND owner,
2181                                 BOOL topmost, const char *file, int line)
2182 {
2183     HWND test;
2184     DWORD ex_style;
2185
2186     test = GetWindow(hwnd, GW_HWNDNEXT);
2187     /* skip foreign windows */
2188     while (test && test != next &&
2189            (GetWindowThreadProcessId(test, NULL) != our_pid ||
2190             UlongToHandle(GetWindowLongPtr(test, GWLP_HINSTANCE)) != GetModuleHandle(0) ||
2191             GetWindow(test, GW_OWNER) == next))
2192     {
2193         /*trace("skipping next %p (%p)\n", test, UlongToHandle(GetWindowLongPtr(test, GWLP_HINSTANCE)));*/
2194         test = GetWindow(test, GW_HWNDNEXT);
2195     }
2196     ok_(file, line)(next == test, "%p: expected next %p, got %p\n", hwnd, next, test);
2197
2198     test = GetWindow(hwnd, GW_HWNDPREV);
2199     /* skip foreign windows */
2200     while (test && test != prev &&
2201            (GetWindowThreadProcessId(test, NULL) != our_pid ||
2202             UlongToHandle(GetWindowLongPtr(test, GWLP_HINSTANCE)) != GetModuleHandle(0) ||
2203             GetWindow(test, GW_OWNER) == hwnd))
2204     {
2205         /*trace("skipping prev %p (%p)\n", test, UlongToHandle(GetWindowLongPtr(test, GWLP_HINSTANCE)));*/
2206         test = GetWindow(test, GW_HWNDPREV);
2207     }
2208     ok_(file, line)(prev == test, "%p: expected prev %p, got %p\n", hwnd, prev, test);
2209
2210     test = GetWindow(hwnd, GW_OWNER);
2211     ok_(file, line)(owner == test, "%p: expected owner %p, got %p\n", hwnd, owner, test);
2212
2213     ex_style = GetWindowLong(hwnd, GWL_EXSTYLE);
2214     ok_(file, line)(!(ex_style & WS_EX_TOPMOST) == !topmost, "%p: expected %stopmost\n",
2215                     hwnd, topmost ? "" : "NOT ");
2216 }
2217
2218 static void test_popup_zorder(HWND hwnd_D, HWND hwnd_E, DWORD style)
2219 {
2220     HWND hwnd_A, hwnd_B, hwnd_C, hwnd_F;
2221
2222     trace("hwnd_D %p, hwnd_E %p\n", hwnd_D, hwnd_E);
2223
2224     SetWindowPos(hwnd_E, hwnd_D, 0,0,0,0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE);
2225
2226     check_z_order(hwnd_D, hwnd_E, 0, 0, FALSE);
2227     check_z_order(hwnd_E, 0, hwnd_D, 0, FALSE);
2228
2229     hwnd_F = CreateWindowEx(0, "MainWindowClass", "Owner window",
2230                             WS_OVERLAPPED | WS_CAPTION,
2231                             100, 100, 100, 100,
2232                             0, 0, GetModuleHandle(0), NULL);
2233     trace("hwnd_F %p\n", hwnd_F);
2234     check_z_order(hwnd_F, hwnd_D, 0, 0, FALSE);
2235
2236     SetWindowPos(hwnd_F, hwnd_E, 0,0,0,0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE);
2237     check_z_order(hwnd_F, 0, hwnd_E, 0, FALSE);
2238     check_z_order(hwnd_E, hwnd_F, hwnd_D, 0, FALSE);
2239     check_z_order(hwnd_D, hwnd_E, 0, 0, FALSE);
2240
2241     hwnd_C = CreateWindowEx(0, "MainWindowClass", NULL,
2242                             style,
2243                             100, 100, 100, 100,
2244                             hwnd_F, 0, GetModuleHandle(0), NULL);
2245     trace("hwnd_C %p\n", hwnd_C);
2246     check_z_order(hwnd_F, 0, hwnd_E, 0, FALSE);
2247     check_z_order(hwnd_E, hwnd_F, hwnd_D, 0, FALSE);
2248     check_z_order(hwnd_D, hwnd_E, hwnd_C, 0, FALSE);
2249     check_z_order(hwnd_C, hwnd_D, 0, hwnd_F, FALSE);
2250
2251     hwnd_B = CreateWindowEx(WS_EX_TOPMOST, "MainWindowClass", NULL,
2252                             style,
2253                             100, 100, 100, 100,
2254                             hwnd_F, 0, GetModuleHandle(0), NULL);
2255     trace("hwnd_B %p\n", hwnd_B);
2256     check_z_order(hwnd_F, 0, hwnd_E, 0, FALSE);
2257     check_z_order(hwnd_E, hwnd_F, hwnd_D, 0, FALSE);
2258     check_z_order(hwnd_D, hwnd_E, hwnd_C, 0, FALSE);
2259     check_z_order(hwnd_C, hwnd_D, hwnd_B, hwnd_F, FALSE);
2260     check_z_order(hwnd_B, hwnd_C, 0, hwnd_F, TRUE);
2261
2262     hwnd_A = CreateWindowEx(WS_EX_TOPMOST, "MainWindowClass", NULL,
2263                             style,
2264                             100, 100, 100, 100,
2265                             0, 0, GetModuleHandle(0), NULL);
2266     trace("hwnd_A %p\n", hwnd_A);
2267     check_z_order(hwnd_F, 0, hwnd_E, 0, FALSE);
2268     check_z_order(hwnd_E, hwnd_F, hwnd_D, 0, FALSE);
2269     check_z_order(hwnd_D, hwnd_E, hwnd_C, 0, FALSE);
2270     check_z_order(hwnd_C, hwnd_D, hwnd_B, hwnd_F, FALSE);
2271     check_z_order(hwnd_B, hwnd_C, hwnd_A, hwnd_F, TRUE);
2272     check_z_order(hwnd_A, hwnd_B, 0, 0, TRUE);
2273
2274     trace("A %p B %p C %p D %p E %p F %p\n", hwnd_A, hwnd_B, hwnd_C, hwnd_D, hwnd_E, hwnd_F);
2275
2276     /* move hwnd_F and its popups up */
2277     SetWindowPos(hwnd_F, HWND_TOP, 0,0,0,0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE);
2278     check_z_order(hwnd_E, 0, hwnd_D, 0, FALSE);
2279     check_z_order(hwnd_D, hwnd_E, hwnd_F, 0, FALSE);
2280     check_z_order(hwnd_F, hwnd_D, hwnd_C, 0, FALSE);
2281     check_z_order(hwnd_C, hwnd_F, hwnd_B, hwnd_F, FALSE);
2282     check_z_order(hwnd_B, hwnd_C, hwnd_A, hwnd_F, TRUE);
2283     check_z_order(hwnd_A, hwnd_B, 0, 0, TRUE);
2284
2285     /* move hwnd_F and its popups down */
2286 #if 0 /* enable once Wine is fixed to pass this test */
2287     SetWindowPos(hwnd_F, HWND_BOTTOM, 0,0,0,0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE);
2288     check_z_order(hwnd_F, 0, hwnd_C, 0, FALSE);
2289     check_z_order(hwnd_C, hwnd_F, hwnd_B, hwnd_F, FALSE);
2290     check_z_order(hwnd_B, hwnd_C, hwnd_E, hwnd_F, FALSE);
2291     check_z_order(hwnd_E, hwnd_B, hwnd_D, 0, FALSE);
2292     check_z_order(hwnd_D, hwnd_E, hwnd_A, 0, FALSE);
2293     check_z_order(hwnd_A, hwnd_D, 0, 0, TRUE);
2294 #endif
2295
2296     /* make hwnd_C owned by a topmost window */
2297     DestroyWindow( hwnd_C );
2298     hwnd_C = CreateWindowEx(0, "MainWindowClass", NULL,
2299                             style,
2300                             100, 100, 100, 100,
2301                             hwnd_A, 0, GetModuleHandle(0), NULL);
2302     trace("hwnd_C %p\n", hwnd_C);
2303     check_z_order(hwnd_E, 0, hwnd_D, 0, FALSE);
2304     check_z_order(hwnd_D, hwnd_E, hwnd_F, 0, FALSE);
2305     check_z_order(hwnd_F, hwnd_D, hwnd_B, 0, FALSE);
2306     check_z_order(hwnd_B, hwnd_F, hwnd_A, hwnd_F, TRUE);
2307     check_z_order(hwnd_A, hwnd_B, hwnd_C, 0, TRUE);
2308     check_z_order(hwnd_C, hwnd_A, 0, hwnd_A, TRUE);
2309
2310     DestroyWindow(hwnd_A);
2311     DestroyWindow(hwnd_B);
2312     DestroyWindow(hwnd_C);
2313     DestroyWindow(hwnd_F);
2314 }
2315
2316 static void test_vis_rgn( HWND hwnd )
2317 {
2318     RECT win_rect, rgn_rect;
2319     HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
2320     HDC hdc;
2321
2322     ShowWindow(hwnd,SW_SHOW);
2323     hdc = GetDC( hwnd );
2324     ok( GetRandomRgn( hdc, hrgn, SYSRGN ) != 0, "GetRandomRgn failed\n" );
2325     GetWindowRect( hwnd, &win_rect );
2326     GetRgnBox( hrgn, &rgn_rect );
2327     if (is_win9x)
2328     {
2329         trace("win9x, mapping to screen coords\n");
2330         MapWindowPoints( hwnd, 0, (POINT *)&rgn_rect, 2 );
2331     }
2332     trace("win: %d,%d-%d,%d\n", win_rect.left, win_rect.top, win_rect.right, win_rect.bottom );
2333     trace("rgn: %d,%d-%d,%d\n", rgn_rect.left, rgn_rect.top, rgn_rect.right, rgn_rect.bottom );
2334     ok( win_rect.left <= rgn_rect.left, "rgn left %d not inside win rect %d\n",
2335         rgn_rect.left, win_rect.left );
2336     ok( win_rect.top <= rgn_rect.top, "rgn top %d not inside win rect %d\n",
2337         rgn_rect.top, win_rect.top );
2338     ok( win_rect.right >= rgn_rect.right, "rgn right %d not inside win rect %d\n",
2339         rgn_rect.right, win_rect.right );
2340     ok( win_rect.bottom >= rgn_rect.bottom, "rgn bottom %d not inside win rect %d\n",
2341         rgn_rect.bottom, win_rect.bottom );
2342     ReleaseDC( hwnd, hdc );
2343 }
2344
2345 static LRESULT WINAPI set_focus_on_activate_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
2346 {
2347     if (msg == WM_ACTIVATE && LOWORD(wp) == WA_ACTIVE)
2348     {
2349         HWND child = GetWindow(hwnd, GW_CHILD);
2350         ok(child != 0, "couldn't find child window\n");
2351         SetFocus(child);
2352         ok(GetFocus() == child, "Focus should be on child %p\n", child);
2353         return 0;
2354     }
2355     return DefWindowProc(hwnd, msg, wp, lp);
2356 }
2357
2358 static void test_SetFocus(HWND hwnd)
2359 {
2360     HWND child, child2;
2361     WNDPROC old_wnd_proc;
2362
2363     /* check if we can set focus to non-visible windows */
2364
2365     ShowWindow(hwnd, SW_SHOW);
2366     SetFocus(0);
2367     SetFocus(hwnd);
2368     ok( GetFocus() == hwnd, "Failed to set focus to visible window %p\n", hwnd );
2369     ok( GetWindowLong(hwnd,GWL_STYLE) & WS_VISIBLE, "Window %p not visible\n", hwnd );
2370     ShowWindow(hwnd, SW_HIDE);
2371     SetFocus(0);
2372     SetFocus(hwnd);
2373     ok( GetFocus() == hwnd, "Failed to set focus to invisible window %p\n", hwnd );
2374     ok( !(GetWindowLong(hwnd,GWL_STYLE) & WS_VISIBLE), "Window %p still visible\n", hwnd );
2375     child = CreateWindowExA(0, "static", NULL, WS_CHILD, 0, 0, 0, 0, hwnd, 0, 0, NULL);
2376     assert(child);
2377     SetFocus(child);
2378     ok( GetFocus() == child, "Failed to set focus to invisible child %p\n", child );
2379     ok( !(GetWindowLong(child,GWL_STYLE) & WS_VISIBLE), "Child %p is visible\n", child );
2380     ShowWindow(child, SW_SHOW);
2381     ok( GetWindowLong(child,GWL_STYLE) & WS_VISIBLE, "Child %p is not visible\n", child );
2382     ok( GetFocus() == child, "Focus no longer on child %p\n", child );
2383     ShowWindow(child, SW_HIDE);
2384     ok( !(GetWindowLong(child,GWL_STYLE) & WS_VISIBLE), "Child %p is visible\n", child );
2385     ok( GetFocus() == hwnd, "Focus should be on parent %p, not %p\n", hwnd, GetFocus() );
2386     ShowWindow(child, SW_SHOW);
2387     child2 = CreateWindowExA(0, "static", NULL, WS_CHILD, 0, 0, 0, 0, child, 0, 0, NULL);
2388     assert(child2);
2389     ShowWindow(child2, SW_SHOW);
2390     SetFocus(child2);
2391     ShowWindow(child, SW_HIDE);
2392     ok( !(GetWindowLong(child,GWL_STYLE) & WS_VISIBLE), "Child %p is visible\n", child );
2393     ok( GetFocus() == child2, "Focus should be on %p, not %p\n", child2, GetFocus() );
2394     ShowWindow(child, SW_SHOW);
2395     SetFocus(child);
2396     ok( GetFocus() == child, "Focus should be on child %p\n", child );
2397     SetWindowPos(child,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_HIDEWINDOW);
2398     ok( GetFocus() == child, "Focus should still be on child %p\n", child );
2399
2400     ShowWindow(child, SW_HIDE);
2401     SetFocus(hwnd);
2402     ok( GetFocus() == hwnd, "Focus should be on parent %p, not %p\n", hwnd, GetFocus() );
2403     SetWindowPos(child,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_SHOWWINDOW);
2404     ok( GetFocus() == hwnd, "Focus should still be on parent %p, not %p\n", hwnd, GetFocus() );
2405     ShowWindow(child, SW_HIDE);
2406     ok( GetFocus() == hwnd, "Focus should still be on parent %p, not %p\n", hwnd, GetFocus() );
2407
2408     ShowWindow(hwnd, SW_SHOW);
2409     ShowWindow(child, SW_SHOW);
2410     SetFocus(child);
2411     ok( GetFocus() == child, "Focus should be on child %p\n", child );
2412     EnableWindow(hwnd, FALSE);
2413     ok( GetFocus() == child, "Focus should still be on child %p\n", child );
2414     EnableWindow(hwnd, TRUE);
2415
2416     ok( GetActiveWindow() == hwnd, "parent window %p should be active\n", hwnd);
2417     ShowWindow(hwnd, SW_SHOWMINIMIZED);
2418     ok( GetActiveWindow() == hwnd, "parent window %p should be active\n", hwnd);
2419 todo_wine
2420     ok( GetFocus() != child, "Focus should not be on child %p\n", child );
2421     ok( GetFocus() != hwnd, "Focus should not be on parent %p\n", hwnd );
2422     ShowWindow(hwnd, SW_RESTORE);
2423     ok( GetActiveWindow() == hwnd, "parent window %p should be active\n", hwnd);
2424     ok( GetFocus() == hwnd, "Focus should be on parent %p\n", hwnd );
2425     ShowWindow(hwnd, SW_SHOWMINIMIZED);
2426     ok( GetActiveWindow() == hwnd, "parent window %p should be active\n", hwnd);
2427     ok( GetFocus() != child, "Focus should not be on child %p\n", child );
2428 todo_wine
2429     ok( GetFocus() != hwnd, "Focus should not be on parent %p\n", hwnd );
2430     old_wnd_proc = (WNDPROC)SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)set_focus_on_activate_proc);
2431     ShowWindow(hwnd, SW_RESTORE);
2432     ok( GetActiveWindow() == hwnd, "parent window %p should be active\n", hwnd);
2433 todo_wine
2434     ok( GetFocus() == child, "Focus should be on child %p, not %p\n", child, GetFocus() );
2435
2436     SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)old_wnd_proc);
2437
2438     DestroyWindow( child2 );
2439     DestroyWindow( child );
2440 }
2441
2442 #define check_wnd_state(a,b,c,d) check_wnd_state_(__FILE__,__LINE__,a,b,c,d)
2443 static void check_wnd_state_(const char *file, int line,
2444                              HWND active, HWND foreground, HWND focus, HWND capture)
2445 {
2446     ok_(file, line)(active == GetActiveWindow(), "GetActiveWindow() = %p\n", GetActiveWindow());
2447     /* only check foreground if it belongs to the current thread */
2448     /* foreground can be moved to a different app pretty much at any time */
2449     if (foreground && GetForegroundWindow() &&
2450         GetWindowThreadProcessId(GetForegroundWindow(), NULL) == GetCurrentThreadId())
2451         ok_(file, line)(foreground == GetForegroundWindow(), "GetForegroundWindow() = %p\n", GetForegroundWindow());
2452     ok_(file, line)(focus == GetFocus(), "GetFocus() = %p\n", GetFocus());
2453     ok_(file, line)(capture == GetCapture(), "GetCapture() = %p\n", GetCapture());
2454 }
2455
2456 /* same as above but without capture test */
2457 #define check_active_state(a,b,c) check_active_state_(__FILE__,__LINE__,a,b,c)
2458 static void check_active_state_(const char *file, int line,
2459                                 HWND active, HWND foreground, HWND focus)
2460 {
2461     ok_(file, line)(active == GetActiveWindow(), "GetActiveWindow() = %p\n", GetActiveWindow());
2462     /* only check foreground if it belongs to the current thread */
2463     /* foreground can be moved to a different app pretty much at any time */
2464     if (foreground && GetForegroundWindow() &&
2465         GetWindowThreadProcessId(GetForegroundWindow(), NULL) == GetCurrentThreadId())
2466         ok_(file, line)(foreground == GetForegroundWindow(), "GetForegroundWindow() = %p\n", GetForegroundWindow());
2467     ok_(file, line)(focus == GetFocus(), "GetFocus() = %p\n", GetFocus());
2468 }
2469
2470 static void test_SetActiveWindow(HWND hwnd)
2471 {
2472     HWND hwnd2;
2473
2474     flush_events( TRUE );
2475     ShowWindow(hwnd, SW_HIDE);
2476     SetFocus(0);
2477     SetActiveWindow(0);
2478     check_wnd_state(0, 0, 0, 0);
2479
2480     /*trace("testing SetActiveWindow %p\n", hwnd);*/
2481
2482     ShowWindow(hwnd, SW_SHOW);
2483     check_wnd_state(hwnd, hwnd, hwnd, 0);
2484
2485     hwnd2 = SetActiveWindow(0);
2486     ok(hwnd2 == hwnd, "SetActiveWindow returned %p instead of %p\n", hwnd2, hwnd);
2487     if (!GetActiveWindow())  /* doesn't always work on vista */
2488     {
2489         check_wnd_state(0, 0, 0, 0);
2490         hwnd2 = SetActiveWindow(hwnd);
2491         ok(hwnd2 == 0, "SetActiveWindow returned %p instead of 0\n", hwnd2);
2492     }
2493     check_wnd_state(hwnd, hwnd, hwnd, 0);
2494
2495     SetWindowPos(hwnd,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_HIDEWINDOW);
2496     check_wnd_state(hwnd, hwnd, hwnd, 0);
2497
2498     SetWindowPos(hwnd,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_SHOWWINDOW);
2499     check_wnd_state(hwnd, hwnd, hwnd, 0);
2500
2501     ShowWindow(hwnd, SW_HIDE);
2502     check_wnd_state(0, 0, 0, 0);
2503
2504     /*trace("testing SetActiveWindow on an invisible window %p\n", hwnd);*/
2505     SetActiveWindow(hwnd);
2506     check_wnd_state(hwnd, hwnd, hwnd, 0);
2507     
2508     ShowWindow(hwnd, SW_SHOW);
2509     check_wnd_state(hwnd, hwnd, hwnd, 0);
2510
2511     hwnd2 = CreateWindowExA(0, "static", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 0, 0, hwnd, 0, 0, NULL);
2512     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2513
2514     DestroyWindow(hwnd2);
2515     check_wnd_state(hwnd, hwnd, hwnd, 0);
2516
2517     hwnd2 = CreateWindowExA(0, "static", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 0, 0, hwnd, 0, 0, NULL);
2518     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2519
2520     SetWindowPos(hwnd2,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_HIDEWINDOW);
2521     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2522
2523     DestroyWindow(hwnd2);
2524     check_wnd_state(hwnd, hwnd, hwnd, 0);
2525 }
2526
2527 struct create_window_thread_params
2528 {
2529     HWND window;
2530     HANDLE window_created;
2531     HANDLE test_finished;
2532 };
2533
2534 static DWORD WINAPI create_window_thread(void *param)
2535 {
2536     struct create_window_thread_params *p = param;
2537     DWORD res;
2538     BOOL ret;
2539
2540     p->window = CreateWindowA("static", NULL, WS_POPUP | WS_VISIBLE, 0, 0, 0, 0, 0, 0, 0, 0);
2541
2542     ret = SetEvent(p->window_created);
2543     ok(ret, "SetEvent failed, last error %#x.\n", GetLastError());
2544
2545     res = WaitForSingleObject(p->test_finished, INFINITE);
2546     ok(res == WAIT_OBJECT_0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
2547
2548     DestroyWindow(p->window);
2549     return 0;
2550 }
2551
2552 static void test_SetForegroundWindow(HWND hwnd)
2553 {
2554     struct create_window_thread_params thread_params;
2555     HANDLE thread;
2556     DWORD res, tid;
2557     BOOL ret;
2558     HWND hwnd2;
2559     MSG msg;
2560
2561     flush_events( TRUE );
2562     ShowWindow(hwnd, SW_HIDE);
2563     SetFocus(0);
2564     SetActiveWindow(0);
2565     check_wnd_state(0, 0, 0, 0);
2566
2567     /*trace("testing SetForegroundWindow %p\n", hwnd);*/
2568
2569     ShowWindow(hwnd, SW_SHOW);
2570     check_wnd_state(hwnd, hwnd, hwnd, 0);
2571
2572     hwnd2 = SetActiveWindow(0);
2573     ok(hwnd2 == hwnd, "SetActiveWindow(0) returned %p instead of %p\n", hwnd2, hwnd);
2574     if (GetActiveWindow() == hwnd)  /* doesn't always work on vista */
2575         check_wnd_state(hwnd, hwnd, hwnd, 0);
2576     else
2577         check_wnd_state(0, 0, 0, 0);
2578
2579     ret = SetForegroundWindow(hwnd);
2580     if (!ret)
2581     {
2582         skip( "SetForegroundWindow not working\n" );
2583         return;
2584     }
2585     check_wnd_state(hwnd, hwnd, hwnd, 0);
2586
2587     SetLastError(0xdeadbeef);
2588     ret = SetForegroundWindow(0);
2589     ok(!ret, "SetForegroundWindow returned TRUE instead of FALSE\n");
2590     ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE ||
2591        broken(GetLastError() == 0xdeadbeef),  /* win9x */
2592        "got error %d expected ERROR_INVALID_WINDOW_HANDLE\n", GetLastError());
2593     check_wnd_state(hwnd, hwnd, hwnd, 0);
2594
2595     SetWindowPos(hwnd,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_HIDEWINDOW);
2596     check_wnd_state(hwnd, hwnd, hwnd, 0);
2597
2598     SetWindowPos(hwnd,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_SHOWWINDOW);
2599     check_wnd_state(hwnd, hwnd, hwnd, 0);
2600
2601     hwnd2 = GetForegroundWindow();
2602     ok(hwnd2 == hwnd, "Wrong foreground window %p\n", hwnd2);
2603     ret = SetForegroundWindow( GetDesktopWindow() );
2604     ok(ret, "SetForegroundWindow(desktop) error: %d\n", GetLastError());
2605     hwnd2 = GetForegroundWindow();
2606     ok(hwnd2 != hwnd, "Wrong foreground window %p\n", hwnd2);
2607
2608     ShowWindow(hwnd, SW_HIDE);
2609     check_wnd_state(0, 0, 0, 0);
2610
2611     /*trace("testing SetForegroundWindow on an invisible window %p\n", hwnd);*/
2612     ret = SetForegroundWindow(hwnd);
2613     ok(ret || broken(!ret), /* win98 */ "SetForegroundWindow returned FALSE instead of TRUE\n");
2614     check_wnd_state(hwnd, hwnd, hwnd, 0);
2615
2616     ShowWindow(hwnd, SW_SHOW);
2617     check_wnd_state(hwnd, hwnd, hwnd, 0);
2618
2619     hwnd2 = CreateWindowExA(0, "static", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 0, 0, hwnd, 0, 0, NULL);
2620     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2621
2622     DestroyWindow(hwnd2);
2623     check_wnd_state(hwnd, hwnd, hwnd, 0);
2624
2625     hwnd2 = CreateWindowExA(0, "static", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 0, 0, hwnd, 0, 0, NULL);
2626     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2627
2628     SetWindowPos(hwnd2,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_HIDEWINDOW);
2629     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2630
2631     DestroyWindow(hwnd2);
2632     check_wnd_state(hwnd, hwnd, hwnd, 0);
2633
2634     hwnd2 = CreateWindowA("static", NULL, WS_POPUP | WS_VISIBLE, 0, 0, 0, 0, 0, 0, 0, 0);
2635     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2636
2637     thread_params.window_created = CreateEvent(NULL, FALSE, FALSE, NULL);
2638     ok(!!thread_params.window_created, "CreateEvent failed, last error %#x.\n", GetLastError());
2639     thread_params.test_finished = CreateEvent(NULL, FALSE, FALSE, NULL);
2640     ok(!!thread_params.test_finished, "CreateEvent failed, last error %#x.\n", GetLastError());
2641     thread = CreateThread(NULL, 0, create_window_thread, &thread_params, 0, &tid);
2642     ok(!!thread, "Failed to create thread, last error %#x.\n", GetLastError());
2643     res = WaitForSingleObject(thread_params.window_created, INFINITE);
2644     ok(res == WAIT_OBJECT_0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
2645     check_wnd_state(hwnd2, thread_params.window, hwnd2, 0);
2646
2647     SetForegroundWindow(hwnd2);
2648     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2649
2650     while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
2651     if (0) check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2652     todo_wine ok(GetActiveWindow() == hwnd2, "Expected active window %p, got %p.\n", hwnd2, GetActiveWindow());
2653     todo_wine ok(GetFocus() == hwnd2, "Expected focus window %p, got %p.\n", hwnd2, GetFocus());
2654
2655     SetEvent(thread_params.test_finished);
2656     WaitForSingleObject(thread, INFINITE);
2657     CloseHandle(thread_params.test_finished);
2658     CloseHandle(thread_params.window_created);
2659     CloseHandle(thread);
2660     DestroyWindow(hwnd2);
2661 }
2662
2663 static WNDPROC old_button_proc;
2664
2665 static LRESULT WINAPI button_hook_proc(HWND button, UINT msg, WPARAM wparam, LPARAM lparam)
2666 {
2667     LRESULT ret;
2668     USHORT key_state;
2669
2670     key_state = GetKeyState(VK_LBUTTON);
2671     ok(!(key_state & 0x8000), "VK_LBUTTON should not be pressed, state %04x\n", key_state);
2672
2673     ret = CallWindowProcA(old_button_proc, button, msg, wparam, lparam);
2674
2675     if (msg == WM_LBUTTONDOWN)
2676     {
2677         HWND hwnd, capture;
2678
2679         check_wnd_state(button, button, button, button);
2680
2681         hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0, 0, 10, 10, 0, 0, 0, NULL);
2682         assert(hwnd);
2683         trace("hwnd %p\n", hwnd);
2684
2685         check_wnd_state(button, button, button, button);
2686
2687         ShowWindow(hwnd, SW_SHOWNOACTIVATE);
2688
2689         check_wnd_state(button, button, button, button);
2690
2691         DestroyWindow(hwnd);
2692
2693         hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0, 0, 10, 10, 0, 0, 0, NULL);
2694         assert(hwnd);
2695         trace("hwnd %p\n", hwnd);
2696
2697         check_wnd_state(button, button, button, button);
2698
2699         /* button wnd proc should release capture on WM_KILLFOCUS if it does
2700          * match internal button state.
2701          */
2702         SendMessage(button, WM_KILLFOCUS, 0, 0);
2703         check_wnd_state(button, button, button, 0);
2704
2705         ShowWindow(hwnd, SW_SHOW);
2706         check_wnd_state(hwnd, hwnd, hwnd, 0);
2707
2708         capture = SetCapture(hwnd);
2709         ok(capture == 0, "SetCapture() = %p\n", capture);
2710
2711         check_wnd_state(hwnd, hwnd, hwnd, hwnd);
2712
2713         DestroyWindow(hwnd);
2714
2715         check_wnd_state(button, 0, button, 0);
2716     }
2717
2718     return ret;
2719 }
2720
2721 static void test_capture_1(void)
2722 {
2723     HWND button, capture;
2724
2725     capture = GetCapture();
2726     ok(capture == 0, "GetCapture() = %p\n", capture);
2727
2728     button = CreateWindowExA(0, "button", NULL, WS_POPUP | WS_VISIBLE, 0, 0, 10, 10, 0, 0, 0, NULL);
2729     assert(button);
2730     trace("button %p\n", button);
2731
2732     old_button_proc = (WNDPROC)SetWindowLongPtrA(button, GWLP_WNDPROC, (LONG_PTR)button_hook_proc);
2733
2734     SendMessageA(button, WM_LBUTTONDOWN, 0, 0);
2735
2736     capture = SetCapture(button);
2737     ok(capture == 0, "SetCapture() = %p\n", capture);
2738     check_wnd_state(button, 0, button, button);
2739
2740     DestroyWindow(button);
2741     /* old active window test depends on previously executed window
2742      * activation tests, and fails under NT4.
2743     check_wnd_state(oldActive, 0, oldFocus, 0);*/
2744 }
2745
2746 static void test_capture_2(void)
2747 {
2748     HWND button, hwnd, capture, oldFocus, oldActive;
2749
2750     oldFocus = GetFocus();
2751     oldActive = GetActiveWindow();
2752     check_wnd_state(oldActive, 0, oldFocus, 0);
2753
2754     button = CreateWindowExA(0, "button", NULL, WS_POPUP | WS_VISIBLE, 0, 0, 10, 10, 0, 0, 0, NULL);
2755     assert(button);
2756     trace("button %p\n", button);
2757
2758     check_wnd_state(button, button, button, 0);
2759
2760     capture = SetCapture(button);
2761     ok(capture == 0, "SetCapture() = %p\n", capture);
2762
2763     check_wnd_state(button, button, button, button);
2764
2765     /* button wnd proc should ignore WM_KILLFOCUS if it doesn't match
2766      * internal button state.
2767      */
2768     SendMessage(button, WM_KILLFOCUS, 0, 0);
2769     check_wnd_state(button, button, button, button);
2770
2771     hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0, 0, 10, 10, 0, 0, 0, NULL);
2772     assert(hwnd);
2773     trace("hwnd %p\n", hwnd);
2774
2775     check_wnd_state(button, button, button, button);
2776
2777     ShowWindow(hwnd, SW_SHOWNOACTIVATE);
2778
2779     check_wnd_state(button, button, button, button);
2780
2781     DestroyWindow(hwnd);
2782
2783     hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0, 0, 10, 10, 0, 0, 0, NULL);
2784     assert(hwnd);
2785     trace("hwnd %p\n", hwnd);
2786
2787     check_wnd_state(button, button, button, button);
2788
2789     ShowWindow(hwnd, SW_SHOW);
2790
2791     check_wnd_state(hwnd, hwnd, hwnd, button);
2792
2793     capture = SetCapture(hwnd);
2794     ok(capture == button, "SetCapture() = %p\n", capture);
2795
2796     check_wnd_state(hwnd, hwnd, hwnd, hwnd);
2797
2798     DestroyWindow(hwnd);
2799     check_wnd_state(button, button, button, 0);
2800
2801     DestroyWindow(button);
2802     check_wnd_state(oldActive, 0, oldFocus, 0);
2803 }
2804
2805 static void test_capture_3(HWND hwnd1, HWND hwnd2)
2806 {
2807     BOOL ret;
2808
2809     ShowWindow(hwnd1, SW_HIDE);
2810     ShowWindow(hwnd2, SW_HIDE);
2811
2812     ok(!IsWindowVisible(hwnd1), "%p should be invisible\n", hwnd1);
2813     ok(!IsWindowVisible(hwnd2), "%p should be invisible\n", hwnd2);
2814
2815     SetCapture(hwnd1);
2816     check_wnd_state(0, 0, 0, hwnd1);
2817
2818     SetCapture(hwnd2);
2819     check_wnd_state(0, 0, 0, hwnd2);
2820
2821     ShowWindow(hwnd1, SW_SHOW);
2822     check_wnd_state(hwnd1, hwnd1, hwnd1, hwnd2);
2823
2824     ret = ReleaseCapture();
2825     ok (ret, "releasecapture did not return TRUE.\n");
2826     ret = ReleaseCapture();
2827     ok (ret, "releasecapture did not return TRUE after second try.\n");
2828 }
2829
2830 static LRESULT CALLBACK test_capture_4_proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2831 {
2832     GUITHREADINFO gti;
2833     HWND cap_wnd, cap_wnd2, set_cap_wnd;
2834     BOOL status;
2835     switch (msg)
2836     {
2837         case WM_CAPTURECHANGED:
2838
2839             /* now try to release capture from menu. this should fail */
2840             if (pGetGUIThreadInfo)
2841             {
2842                 memset(&gti, 0, sizeof(GUITHREADINFO));
2843                 gti.cbSize = sizeof(GUITHREADINFO);
2844                 status = pGetGUIThreadInfo(GetCurrentThreadId(), &gti);
2845                 ok(status, "GetGUIThreadInfo() failed!\n");
2846                 ok(gti.flags & GUI_INMENUMODE, "Thread info incorrect (flags=%08X)!\n", gti.flags);
2847             }
2848             cap_wnd = GetCapture();
2849
2850             /* check that re-setting the capture for the menu fails */
2851             set_cap_wnd = SetCapture(cap_wnd);
2852             ok(!set_cap_wnd || broken(set_cap_wnd == cap_wnd), /* nt4 */
2853                "SetCapture should have failed!\n");
2854             if (set_cap_wnd)
2855             {
2856                 DestroyWindow(hWnd);
2857                 break;
2858             }
2859
2860             /* check that SetCapture fails for another window and that it does not touch the error code */
2861             set_cap_wnd = SetCapture(hWnd);
2862             ok(!set_cap_wnd, "SetCapture should have failed!\n");
2863
2864             /* check that ReleaseCapture fails and does not touch the error code */
2865             status = ReleaseCapture();
2866             ok(!status, "ReleaseCapture should have failed!\n");
2867
2868             /* check that thread info did not change */
2869             if (pGetGUIThreadInfo)
2870             {
2871                 memset(&gti, 0, sizeof(GUITHREADINFO));
2872                 gti.cbSize = sizeof(GUITHREADINFO);
2873                 status = pGetGUIThreadInfo(GetCurrentThreadId(), &gti);
2874                 ok(status, "GetGUIThreadInfo() failed!\n");
2875                 ok(gti.flags & GUI_INMENUMODE, "Thread info incorrect (flags=%08X)!\n", gti.flags);
2876             }
2877
2878             /* verify that no capture change took place */
2879             cap_wnd2 = GetCapture();
2880             ok(cap_wnd2 == cap_wnd, "Capture changed!\n");
2881
2882             /* we are done. kill the window */
2883             DestroyWindow(hWnd);
2884             break;
2885
2886         default:
2887             return( DefWindowProcA( hWnd, msg, wParam, lParam ) );
2888     }
2889     return 0;
2890 }
2891
2892 /* Test that no-one can mess around with the current capture while a menu is open */
2893 static void test_capture_4(void)
2894 {
2895     BOOL ret;
2896     HMENU hmenu;
2897     HWND hwnd;
2898     WNDCLASSA wclass;
2899     HINSTANCE hInstance = GetModuleHandleA( NULL );
2900     ATOM aclass;
2901
2902     if (!pGetGUIThreadInfo)
2903     {
2904         win_skip("GetGUIThreadInfo is not available\n");
2905         return;
2906     }
2907     wclass.lpszClassName = "TestCapture4Class";
2908     wclass.style         = CS_HREDRAW | CS_VREDRAW;
2909     wclass.lpfnWndProc   = test_capture_4_proc;
2910     wclass.hInstance     = hInstance;
2911     wclass.hIcon         = LoadIconA( 0, IDI_APPLICATION );
2912     wclass.hCursor       = LoadCursorA( NULL, IDC_ARROW );
2913     wclass.hbrBackground = (HBRUSH)( COLOR_WINDOW + 1 );
2914     wclass.lpszMenuName  = 0;
2915     wclass.cbClsExtra    = 0;
2916     wclass.cbWndExtra    = 0;
2917     aclass = RegisterClassA( &wclass );
2918     ok( aclass, "RegisterClassA failed with error %d\n", GetLastError());
2919     hwnd = CreateWindowA( wclass.lpszClassName, "MenuTest",
2920                           WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0,
2921                           400, 200, NULL, NULL, hInstance, NULL);
2922     ok(hwnd != NULL, "CreateWindowEx failed with error %d\n", GetLastError());
2923     if (!hwnd) return;
2924     hmenu = CreatePopupMenu();
2925
2926     ret = AppendMenuA( hmenu, MF_STRING, 1, "winetest2");
2927     ok( ret, "AppendMenuA has failed!\n");
2928
2929     /* set main window to have initial capture */
2930     SetCapture(hwnd);
2931
2932     if (is_win9x)
2933     {
2934         win_skip("TrackPopupMenu test crashes on Win9x/WinMe\n");
2935     }
2936     else
2937     {
2938         /* create popup (it will self-destruct) */
2939         ret = TrackPopupMenu(hmenu, TPM_RETURNCMD, 100, 100, 0, hwnd, NULL);
2940         ok( ret == 0, "TrackPopupMenu returned %d expected zero\n", ret);
2941     }
2942
2943     /* clean up */
2944     DestroyMenu(hmenu);
2945     DestroyWindow(hwnd);
2946 }
2947
2948 /* PeekMessage wrapper that ignores the messages we don't care about */
2949 static BOOL peek_message( MSG *msg )
2950 {
2951     BOOL ret;
2952     do
2953     {
2954         ret = PeekMessageA(msg, 0, 0, 0, PM_REMOVE);
2955     } while (ret && (msg->message == WM_TIMER || ignore_message(msg->message)));
2956     return ret;
2957 }
2958
2959 static void test_keyboard_input(HWND hwnd)
2960 {
2961     MSG msg;
2962     BOOL ret;
2963
2964     flush_events( TRUE );
2965     SetWindowPos(hwnd, 0, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE|SWP_SHOWWINDOW);
2966     UpdateWindow(hwnd);
2967     flush_events( TRUE );
2968
2969     ok(GetActiveWindow() == hwnd, "wrong active window %p\n", GetActiveWindow());
2970
2971     SetFocus(hwnd);
2972     ok(GetFocus() == hwnd, "wrong focus window %p\n", GetFocus());
2973
2974     flush_events( TRUE );
2975
2976     PostMessageA(hwnd, WM_KEYDOWN, 0, 0);
2977     ret = peek_message(&msg);
2978     ok( ret, "no message available\n");
2979     ok(msg.hwnd == hwnd && msg.message == WM_KEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2980     ret = peek_message(&msg);
2981     ok( !ret, "message %04x available\n", msg.message);
2982
2983     ok(GetFocus() == hwnd, "wrong focus window %p\n", GetFocus());
2984
2985     PostThreadMessageA(GetCurrentThreadId(), WM_KEYDOWN, 0, 0);
2986     ret = peek_message(&msg);
2987     ok(ret, "no message available\n");
2988     ok(!msg.hwnd && msg.message == WM_KEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2989     ret = peek_message(&msg);
2990     ok( !ret, "message %04x available\n", msg.message);
2991
2992     ok(GetFocus() == hwnd, "wrong focus window %p\n", GetFocus());
2993
2994     keybd_event(VK_SPACE, 0, 0, 0);
2995     if (!peek_message(&msg))
2996     {
2997         skip( "keybd_event didn't work, skipping keyboard test\n" );
2998         return;
2999     }
3000     ok(msg.hwnd == hwnd && msg.message == WM_KEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
3001     ret = peek_message(&msg);
3002     ok( !ret, "message %04x available\n", msg.message);
3003
3004     SetFocus(0);
3005     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3006
3007     flush_events( TRUE );
3008
3009     PostMessageA(hwnd, WM_KEYDOWN, 0, 0);
3010     ret = peek_message(&msg);
3011     ok(ret, "no message available\n");
3012     ok(msg.hwnd == hwnd && msg.message == WM_KEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
3013     ret = peek_message(&msg);
3014     ok( !ret, "message %04x available\n", msg.message);
3015
3016     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3017
3018     PostThreadMessageA(GetCurrentThreadId(), WM_KEYDOWN, 0, 0);
3019     ret = peek_message(&msg);
3020     ok(ret, "no message available\n");
3021     ok(!msg.hwnd && msg.message == WM_KEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
3022     ret = peek_message(&msg);
3023     ok( !ret, "message %04x available\n", msg.message);
3024
3025     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3026
3027     keybd_event(VK_SPACE, 0, 0, 0);
3028     ret = peek_message(&msg);
3029     ok(ret, "no message available\n");
3030     ok(msg.hwnd == hwnd && msg.message == WM_SYSKEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
3031     ret = peek_message(&msg);
3032     ok( !ret, "message %04x available\n", msg.message);
3033 }
3034
3035 static BOOL wait_for_message( MSG *msg )
3036 {
3037     BOOL ret;
3038
3039     for (;;)
3040     {
3041         ret = peek_message(msg);
3042         if (ret)
3043         {
3044             if (msg->message == WM_PAINT) DispatchMessage(msg);
3045             else break;
3046         }
3047         else if (MsgWaitForMultipleObjects( 0, NULL, FALSE, 100, QS_ALLINPUT ) == WAIT_TIMEOUT) break;
3048     }
3049     if (!ret) msg->message = 0;
3050     return ret;
3051 }
3052
3053 static void test_mouse_input(HWND hwnd)
3054 {
3055     RECT rc;
3056     POINT pt;
3057     int x, y;
3058     HWND popup;
3059     MSG msg;
3060     BOOL ret;
3061     LRESULT res;
3062
3063     ShowWindow(hwnd, SW_SHOWNORMAL);
3064     UpdateWindow(hwnd);
3065     SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE );
3066
3067     GetWindowRect(hwnd, &rc);
3068     trace("main window %p: (%d,%d)-(%d,%d)\n", hwnd, rc.left, rc.top, rc.right, rc.bottom);
3069
3070     popup = CreateWindowExA(0, "MainWindowClass", NULL, WS_POPUP,
3071                             rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top,
3072                             hwnd, 0, 0, NULL);
3073     assert(popup != 0);
3074     ShowWindow(popup, SW_SHOW);
3075     UpdateWindow(popup);
3076     SetWindowPos( popup, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE );
3077
3078     GetWindowRect(popup, &rc);
3079     trace("popup window %p: (%d,%d)-(%d,%d)\n", popup, rc.left, rc.top, rc.right, rc.bottom);
3080
3081     x = rc.left + (rc.right - rc.left) / 2;
3082     y = rc.top + (rc.bottom - rc.top) / 2;
3083     trace("setting cursor to (%d,%d)\n", x, y);
3084
3085     SetCursorPos(x, y);
3086     GetCursorPos(&pt);
3087     if (x != pt.x || y != pt.y)
3088     {
3089         skip( "failed to set mouse position, skipping mouse input tests\n" );
3090         goto done;
3091     }
3092
3093     flush_events( TRUE );
3094
3095     /* Check that setting the same position may generate WM_MOUSEMOVE */
3096     SetCursorPos(x, y);
3097     msg.message = 0;
3098     ret = peek_message(&msg);
3099     if (ret)
3100     {
3101         ok(msg.hwnd == popup && msg.message == WM_MOUSEMOVE, "hwnd %p message %04x\n",
3102            msg.hwnd, msg.message);
3103         ok(msg.pt.x == x && msg.pt.y == y, "wrong message coords (%d,%d)/(%d,%d)\n",
3104            x, y, msg.pt.x, msg.pt.y);
3105     }
3106
3107     /* force the system to update its internal queue mouse position,
3108      * otherwise it won't generate relative mouse movements below.
3109      */
3110     mouse_event(MOUSEEVENTF_MOVE, -1, -1, 0, 0);
3111     flush_events( TRUE );
3112
3113     msg.message = 0;
3114     mouse_event(MOUSEEVENTF_MOVE, 1, 1, 0, 0);
3115     flush_events( FALSE );
3116     /* FIXME: SetCursorPos in Wine generates additional WM_MOUSEMOVE message */
3117     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE))
3118     {
3119         if (msg.message == WM_TIMER || ignore_message(msg.message)) continue;
3120         ok(msg.hwnd == popup && msg.message == WM_MOUSEMOVE,
3121            "hwnd %p message %04x\n", msg.hwnd, msg.message);
3122         DispatchMessage(&msg);
3123     }
3124     ret = peek_message(&msg);
3125     ok( !ret, "message %04x available\n", msg.message);
3126
3127     mouse_event(MOUSEEVENTF_MOVE, -1, -1, 0, 0);
3128     ShowWindow(popup, SW_HIDE);
3129     ret = wait_for_message( &msg );
3130     if (ret)
3131         ok(msg.hwnd == hwnd && msg.message == WM_MOUSEMOVE, "hwnd %p message %04x\n", msg.hwnd, msg.message);
3132     flush_events( TRUE );
3133
3134     mouse_event(MOUSEEVENTF_MOVE, 1, 1, 0, 0);
3135     ShowWindow(hwnd, SW_HIDE);
3136     ret = wait_for_message( &msg );
3137     ok( !ret, "message %04x available\n", msg.message);
3138     flush_events( TRUE );
3139
3140     /* test mouse clicks */
3141
3142     ShowWindow(hwnd, SW_SHOW);
3143     SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE );
3144     flush_events( TRUE );
3145     ShowWindow(popup, SW_SHOW);
3146     SetWindowPos( popup, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE );
3147     flush_events( TRUE );
3148
3149     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
3150     mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
3151     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
3152     mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
3153
3154     ret = wait_for_message( &msg );
3155     if (!ret)
3156     {
3157         skip( "simulating mouse click doesn't work, skipping mouse button tests\n" );
3158         goto done;
3159     }
3160     if (msg.message == WM_MOUSEMOVE)  /* win2k has an extra WM_MOUSEMOVE here */
3161     {
3162         ret = wait_for_message( &msg );
3163         ok(ret, "no message available\n");
3164     }
3165
3166     ok(msg.hwnd == popup && msg.message == WM_LBUTTONDOWN, "hwnd %p/%p message %04x\n",
3167        msg.hwnd, popup, msg.message);
3168
3169     ret = wait_for_message( &msg );
3170     ok(ret, "no message available\n");
3171     ok(msg.hwnd == popup && msg.message == WM_LBUTTONUP, "hwnd %p/%p message %04x\n",
3172        msg.hwnd, popup, msg.message);
3173
3174     ret = wait_for_message( &msg );
3175     ok(ret, "no message available\n");
3176     ok(msg.hwnd == popup && msg.message == WM_LBUTTONDBLCLK, "hwnd %p/%p message %04x\n",
3177        msg.hwnd, popup, msg.message);
3178
3179     ret = wait_for_message( &msg );
3180     ok(ret, "no message available\n");
3181     ok(msg.hwnd == popup && msg.message == WM_LBUTTONUP, "hwnd %p/%p message %04x\n",
3182        msg.hwnd, popup, msg.message);
3183
3184     ret = peek_message(&msg);
3185     ok(!ret, "message %04x available\n", msg.message);
3186
3187     ShowWindow(popup, SW_HIDE);
3188     flush_events( TRUE );
3189
3190     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
3191     mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
3192     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
3193     mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
3194
3195     ret = wait_for_message( &msg );
3196     ok(ret, "no message available\n");
3197     ok(msg.hwnd == hwnd && msg.message == WM_LBUTTONDOWN, "hwnd %p/%p message %04x\n",
3198        msg.hwnd, hwnd, msg.message);
3199     ret = wait_for_message( &msg );
3200     ok(ret, "no message available\n");
3201     ok(msg.hwnd == hwnd && msg.message == WM_LBUTTONUP, "hwnd %p/%p message %04x\n",
3202        msg.hwnd, hwnd, msg.message);
3203
3204     test_lbuttondown_flag = TRUE;
3205     SendMessageA(hwnd, WM_COMMAND, (WPARAM)popup, 0);
3206     test_lbuttondown_flag = FALSE;
3207
3208     ret = wait_for_message( &msg );
3209     ok(ret, "no message available\n");
3210     ok(msg.hwnd == popup && msg.message == WM_LBUTTONDOWN, "hwnd %p/%p message %04x\n",
3211        msg.hwnd, popup, msg.message);
3212     ok(peek_message(&msg), "no message available\n");
3213     ok(msg.hwnd == popup && msg.message == WM_LBUTTONUP, "hwnd %p/%p message %04x\n",
3214        msg.hwnd, popup, msg.message);
3215     ok(peek_message(&msg), "no message available\n");
3216
3217     /* Test WM_MOUSEACTIVATE */
3218 #define TEST_MOUSEACTIVATE(A,B)                                                          \
3219        res = SendMessageA(hwnd, WM_MOUSEACTIVATE, (WPARAM)hwnd, (LPARAM)MAKELRESULT(A,0));   \
3220        ok(res == B, "WM_MOUSEACTIVATE for %s returned %ld\n", #A, res);
3221        
3222     TEST_MOUSEACTIVATE(HTERROR,MA_ACTIVATE);
3223     TEST_MOUSEACTIVATE(HTTRANSPARENT,MA_ACTIVATE);
3224     TEST_MOUSEACTIVATE(HTNOWHERE,MA_ACTIVATE);
3225     TEST_MOUSEACTIVATE(HTCLIENT,MA_ACTIVATE);
3226     TEST_MOUSEACTIVATE(HTCAPTION,MA_ACTIVATE);
3227     TEST_MOUSEACTIVATE(HTSYSMENU,MA_ACTIVATE);
3228     TEST_MOUSEACTIVATE(HTSIZE,MA_ACTIVATE);
3229     TEST_MOUSEACTIVATE(HTMENU,MA_ACTIVATE);
3230     TEST_MOUSEACTIVATE(HTHSCROLL,MA_ACTIVATE);
3231     TEST_MOUSEACTIVATE(HTVSCROLL,MA_ACTIVATE);
3232     TEST_MOUSEACTIVATE(HTMINBUTTON,MA_ACTIVATE);
3233     TEST_MOUSEACTIVATE(HTMAXBUTTON,MA_ACTIVATE);
3234     TEST_MOUSEACTIVATE(HTLEFT,MA_ACTIVATE);
3235     TEST_MOUSEACTIVATE(HTRIGHT,MA_ACTIVATE);
3236     TEST_MOUSEACTIVATE(HTTOP,MA_ACTIVATE);
3237     TEST_MOUSEACTIVATE(HTTOPLEFT,MA_ACTIVATE);
3238     TEST_MOUSEACTIVATE(HTTOPRIGHT,MA_ACTIVATE);
3239     TEST_MOUSEACTIVATE(HTBOTTOM,MA_ACTIVATE);
3240     TEST_MOUSEACTIVATE(HTBOTTOMLEFT,MA_ACTIVATE);
3241     TEST_MOUSEACTIVATE(HTBOTTOMRIGHT,MA_ACTIVATE);
3242     TEST_MOUSEACTIVATE(HTBORDER,MA_ACTIVATE);
3243     TEST_MOUSEACTIVATE(HTOBJECT,MA_ACTIVATE);
3244     TEST_MOUSEACTIVATE(HTCLOSE,MA_ACTIVATE);
3245     TEST_MOUSEACTIVATE(HTHELP,MA_ACTIVATE);
3246
3247 done:
3248     /* Clear any messages left behind by WM_MOUSEACTIVATE tests */
3249     flush_events( TRUE );
3250
3251     DestroyWindow(popup);
3252 }
3253
3254 static void test_validatergn(HWND hwnd)
3255 {
3256     HWND child;
3257     RECT rc, rc2;
3258     HRGN rgn;
3259     int ret;
3260     child = CreateWindowExA(0, "static", NULL, WS_CHILD| WS_VISIBLE, 10, 10, 10, 10, hwnd, 0, 0, NULL);
3261     ShowWindow(hwnd, SW_SHOW);
3262     UpdateWindow( hwnd);
3263     /* test that ValidateRect validates children*/
3264     InvalidateRect( child, NULL, 1);
3265     GetWindowRect( child, &rc);
3266     MapWindowPoints( NULL, hwnd, (POINT*) &rc, 2);
3267     ret = GetUpdateRect( child, &rc2, 0);
3268     ok( ret == 1, "Expected GetUpdateRect to return non-zero, got %d\n", ret);
3269     ok( rc2.right > rc2.left && rc2.bottom > rc2.top,
3270             "Update rectangle is empty!\n");
3271     ValidateRect( hwnd, &rc);
3272     ret = GetUpdateRect( child, &rc2, 0);
3273     ok( !ret, "Expected GetUpdateRect to return zero, got %d\n", ret);
3274     ok( rc2.left == 0 && rc2.top == 0 && rc2.right == 0 && rc2.bottom == 0,
3275             "Update rectangle %d,%d-%d,%d is not empty!\n", rc2.left, rc2.top,
3276             rc2.right, rc2.bottom);
3277
3278     /* now test ValidateRgn */
3279     InvalidateRect( child, NULL, 1);
3280     GetWindowRect( child, &rc);
3281     MapWindowPoints( NULL, hwnd, (POINT*) &rc, 2);
3282     rgn = CreateRectRgnIndirect( &rc);
3283     ValidateRgn( hwnd, rgn);
3284     ret = GetUpdateRect( child, &rc2, 0);
3285     ok( !ret, "Expected GetUpdateRect to return zero, got %d\n", ret);
3286     ok( rc2.left == 0 && rc2.top == 0 && rc2.right == 0 && rc2.bottom == 0,
3287             "Update rectangle %d,%d-%d,%d is not empty!\n", rc2.left, rc2.top,
3288             rc2.right, rc2.bottom);
3289
3290     DeleteObject( rgn);
3291     DestroyWindow( child );
3292 }
3293
3294 static void nccalchelper(HWND hwnd, INT x, INT y, RECT *prc)
3295 {
3296     RECT rc;
3297     MoveWindow( hwnd, 0, 0, x, y, 0);
3298     GetWindowRect( hwnd, prc);
3299     rc = *prc;
3300     DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)prc);
3301     trace("window rect is %d,%d - %d,%d, nccalc rect is %d,%d - %d,%d\n",
3302           rc.left,rc.top,rc.right,rc.bottom, prc->left,prc->top,prc->right,prc->bottom);
3303 }
3304
3305 static void test_nccalcscroll(HWND parent)
3306 {
3307     RECT rc1;
3308     INT sbheight = GetSystemMetrics( SM_CYHSCROLL);
3309     INT sbwidth = GetSystemMetrics( SM_CXVSCROLL);
3310     HWND hwnd = CreateWindowExA(0, "static", NULL, 
3311             WS_CHILD| WS_VISIBLE | WS_VSCROLL | WS_HSCROLL , 
3312             10, 10, 200, 200, parent, 0, 0, NULL); 
3313     ShowWindow( parent, SW_SHOW);
3314     UpdateWindow( parent);
3315
3316     /* test window too low for a horizontal scroll bar */
3317     nccalchelper( hwnd, 100, sbheight, &rc1);
3318     ok( rc1.bottom - rc1.top == sbheight, "Height should be %d size is %d,%d - %d,%d\n",
3319             sbheight, rc1.left, rc1.top, rc1.right, rc1.bottom);
3320
3321     /* test window just high enough for a horizontal scroll bar */
3322     nccalchelper( hwnd, 100, sbheight + 1, &rc1);
3323     ok( rc1.bottom - rc1.top == 1, "Height should be %d size is %d,%d - %d,%d\n",
3324             1, rc1.left, rc1.top, rc1.right, rc1.bottom);
3325
3326     /* test window too narrow for a vertical scroll bar */
3327     nccalchelper( hwnd, sbwidth - 1, 100, &rc1);
3328     ok( rc1.right - rc1.left == sbwidth - 1 , "Width should be %d size is %d,%d - %d,%d\n",
3329             sbwidth - 1, rc1.left, rc1.top, rc1.right, rc1.bottom);
3330
3331     /* test window just wide enough for a vertical scroll bar */
3332     nccalchelper( hwnd, sbwidth, 100, &rc1);
3333     ok( rc1.right - rc1.left == 0, "Width should be %d size is %d,%d - %d,%d\n",
3334             0, rc1.left, rc1.top, rc1.right, rc1.bottom);
3335
3336     /* same test, but with client edge: not enough width */
3337     SetWindowLong( hwnd, GWL_EXSTYLE, WS_EX_CLIENTEDGE | GetWindowLong( hwnd, GWL_EXSTYLE));
3338     nccalchelper( hwnd, sbwidth, 100, &rc1);
3339     ok( rc1.right - rc1.left == sbwidth - 2 * GetSystemMetrics(SM_CXEDGE),
3340             "Width should be %d size is %d,%d - %d,%d\n",
3341             sbwidth - 2 * GetSystemMetrics(SM_CXEDGE), rc1.left, rc1.top, rc1.right, rc1.bottom);
3342
3343     DestroyWindow( hwnd);
3344 }
3345
3346 static void test_SetParent(void)
3347 {
3348     HWND desktop = GetDesktopWindow();
3349     HMENU hMenu;
3350     HWND ret, parent, child1, child2, child3, child4, sibling, popup;
3351     BOOL bret;
3352
3353     parent = CreateWindowExA(0, "static", NULL, WS_OVERLAPPEDWINDOW,
3354                              100, 100, 200, 200, 0, 0, 0, NULL);
3355     assert(parent != 0);
3356     child1 = CreateWindowExA(0, "static", NULL, WS_CHILD,
3357                              0, 0, 50, 50, parent, 0, 0, NULL);
3358     assert(child1 != 0);
3359     child2 = CreateWindowExA(0, "static", NULL, WS_POPUP,
3360                              0, 0, 50, 50, child1, 0, 0, NULL);
3361     assert(child2 != 0);
3362     child3 = CreateWindowExA(0, "static", NULL, WS_CHILD,
3363                              0, 0, 50, 50, child2, 0, 0, NULL);
3364     assert(child3 != 0);
3365     child4 = CreateWindowExA(0, "static", NULL, WS_POPUP,
3366                              0, 0, 50, 50, child3, 0, 0, NULL);
3367     assert(child4 != 0);
3368
3369     trace("parent %p, child1 %p, child2 %p, child3 %p, child4 %p\n",
3370            parent, child1, child2, child3, child4);
3371
3372     check_parents(parent, desktop, 0, 0, 0, parent, parent);
3373     check_parents(child1, parent, parent, parent, 0, parent, parent);
3374     check_parents(child2, desktop, parent, parent, parent, child2, parent);
3375     check_parents(child3, child2, child2, child2, 0, child2, parent);
3376     check_parents(child4, desktop, child2, child2, child2, child4, parent);
3377
3378     ok(!IsChild(desktop, parent), "wrong parent/child %p/%p\n", desktop, parent);
3379     ok(!IsChild(desktop, child1), "wrong parent/child %p/%p\n", desktop, child1);
3380     ok(!IsChild(desktop, child2), "wrong parent/child %p/%p\n", desktop, child2);
3381     ok(!IsChild(desktop, child3), "wrong parent/child %p/%p\n", desktop, child3);
3382     ok(!IsChild(desktop, child4), "wrong parent/child %p/%p\n", desktop, child4);
3383
3384     ok(IsChild(parent, child1), "wrong parent/child %p/%p\n", parent, child1);
3385     ok(!IsChild(desktop, child2), "wrong parent/child %p/%p\n", desktop, child2);
3386     ok(!IsChild(parent, child2), "wrong parent/child %p/%p\n", parent, child2);
3387     ok(!IsChild(child1, child2), "wrong parent/child %p/%p\n", child1, child2);
3388     ok(!IsChild(parent, child3), "wrong parent/child %p/%p\n", parent, child3);
3389     ok(IsChild(child2, child3), "wrong parent/child %p/%p\n", child2, child3);
3390     ok(!IsChild(parent, child4), "wrong parent/child %p/%p\n", parent, child4);
3391     ok(!IsChild(child3, child4), "wrong parent/child %p/%p\n", child3, child4);
3392     ok(!IsChild(desktop, child4), "wrong parent/child %p/%p\n", desktop, child4);
3393
3394     if (!is_win9x) /* Win9x doesn't survive this test */
3395     {
3396         ok(!SetParent(parent, child1), "SetParent should fail\n");
3397         ok(!SetParent(child2, child3), "SetParent should fail\n");
3398         ok(SetParent(child1, parent) != 0, "SetParent should not fail\n");
3399         ret = SetParent(parent, child2);
3400         todo_wine ok( !ret || broken( ret != 0 ), "SetParent should fail\n");
3401         if (ret)  /* nt4, win2k */
3402         {
3403             ret = SetParent(parent, child3);
3404             ok(ret != 0, "SetParent should not fail\n");
3405             ret = SetParent(child2, parent);
3406             ok(!ret, "SetParent should fail\n");
3407             ret = SetParent(parent, child4);
3408             ok(ret != 0, "SetParent should not fail\n");
3409             check_parents(parent, child4, child4, 0, 0, child4, parent);
3410             check_parents(child1, parent, parent, parent, 0, child4, parent);
3411             check_parents(child2, desktop, parent, parent, parent, child2, parent);
3412             check_parents(child3, child2, child2, child2, 0, child2, parent);
3413             check_parents(child4, desktop, child2, child2, child2, child4, parent);
3414         }
3415         else
3416         {
3417             ret = SetParent(parent, child3);
3418             ok(ret != 0, "SetParent should not fail\n");
3419             ret = SetParent(child2, parent);
3420             ok(!ret, "SetParent should fail\n");
3421             ret = SetParent(parent, child4);
3422             ok(!ret, "SetParent should fail\n");
3423             check_parents(parent, child3, child3, 0, 0, child2, parent);
3424             check_parents(child1, parent, parent, parent, 0, child2, parent);
3425             check_parents(child2, desktop, parent, parent, parent, child2, parent);
3426             check_parents(child3, child2, child2, child2, 0, child2, parent);
3427             check_parents(child4, desktop, child2, child2, child2, child4, parent);
3428         }
3429     }
3430     else
3431         skip("Win9x/WinMe crash\n");
3432
3433     hMenu = CreateMenu();
3434     sibling = CreateWindowExA(0, "static", NULL, WS_OVERLAPPEDWINDOW,
3435                              100, 100, 200, 200, 0, hMenu, 0, NULL);
3436     assert(sibling != 0);
3437
3438     ok(SetParent(sibling, parent) != 0, "SetParent should not fail\n");
3439     ok(GetMenu(sibling) == hMenu, "SetParent should not remove menu\n");
3440
3441     ok(DestroyWindow(parent), "DestroyWindow() failed\n");
3442
3443     ok(!IsWindow(parent), "parent still exists\n");
3444     ok(!IsWindow(sibling), "sibling still exists\n");
3445     ok(!IsWindow(child1), "child1 still exists\n");
3446     ok(!IsWindow(child2), "child2 still exists\n");
3447     ok(!IsWindow(child3), "child3 still exists\n");
3448     ok(!IsWindow(child4), "child4 still exists\n");
3449
3450     parent = CreateWindowExA(0, "static", NULL, WS_OVERLAPPEDWINDOW,
3451                              100, 100, 200, 200, 0, 0, 0, NULL);
3452     assert(parent != 0);
3453     child1 = CreateWindowExA(0, "static", NULL, WS_CHILD,
3454                              0, 0, 50, 50, parent, 0, 0, NULL);
3455     assert(child1 != 0);
3456     popup = CreateWindowExA(0, "static", NULL, WS_POPUP,
3457                              0, 0, 50, 50, 0, 0, 0, NULL);
3458     assert(popup != 0);
3459
3460     trace("parent %p, child %p, popup %p\n", parent, child1, popup);
3461
3462     check_parents(parent, desktop, 0, 0, 0, parent, parent);
3463     check_parents(child1, parent, parent, parent, 0, parent, parent);
3464     check_parents(popup, desktop, 0, 0, 0, popup, popup);
3465
3466     SetActiveWindow(parent);
3467     SetFocus(parent);
3468     check_active_state(parent, 0, parent);
3469
3470     ret = SetParent(popup, child1);
3471     ok(ret == desktop, "expected %p, got %p\n", desktop, ret);
3472     check_parents(popup, child1, child1, 0, 0, parent, popup);
3473 todo_wine
3474     check_active_state(popup, 0, popup);
3475
3476     SetActiveWindow(parent);
3477     SetFocus(parent);
3478     check_active_state(parent, 0, parent);
3479
3480     bret = SetForegroundWindow(popup);
3481 todo_wine {
3482     ok(bret || broken(!bret), "SetForegroundWindow() failed\n");
3483     if (!bret)
3484         check_active_state(popup, 0, popup);
3485     else
3486         check_active_state(popup, popup, popup);
3487     }
3488
3489     ok(DestroyWindow(parent), "DestroyWindow() failed\n");
3490
3491     ok(!IsWindow(parent), "parent still exists\n");
3492     ok(!IsWindow(child1), "child1 still exists\n");
3493     ok(!IsWindow(popup), "popup still exists\n");
3494 }
3495
3496 static LRESULT WINAPI StyleCheckProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
3497 {
3498     LPCREATESTRUCT lpcs;
3499     LPSTYLESTRUCT lpss;
3500
3501     switch (msg)
3502     {
3503     case WM_NCCREATE:
3504     case WM_CREATE:
3505         lpcs = (LPCREATESTRUCT)lparam;
3506         lpss = lpcs->lpCreateParams;
3507         if (lpss)
3508         {
3509             if ((lpcs->dwExStyle & WS_EX_DLGMODALFRAME) ||
3510                 ((!(lpcs->dwExStyle & WS_EX_STATICEDGE)) &&
3511                     (lpcs->style & (WS_DLGFRAME | WS_THICKFRAME))))
3512                 ok(lpcs->dwExStyle & WS_EX_WINDOWEDGE, "Window should have WS_EX_WINDOWEDGE style\n");
3513             else
3514                 ok(!(lpcs->dwExStyle & WS_EX_WINDOWEDGE), "Window shouldn't have WS_EX_WINDOWEDGE style\n");
3515
3516             ok((lpss->styleOld & ~WS_EX_WINDOWEDGE) == (lpcs->dwExStyle & ~WS_EX_WINDOWEDGE),
3517                 "Ex style (0x%08lx) should match what the caller passed to CreateWindowEx (0x%08lx)\n",
3518                 (lpss->styleOld & ~WS_EX_WINDOWEDGE), (lpcs->dwExStyle & ~WS_EX_WINDOWEDGE));
3519
3520             ok(lpss->styleNew == lpcs->style,
3521                 "Style (0x%08x) should match what the caller passed to CreateWindowEx (0x%08x)\n",
3522                 lpss->styleNew, lpcs->style);
3523         }
3524         break;
3525     }
3526     return DefWindowProc(hwnd, msg, wparam, lparam);
3527 }
3528
3529 static ATOM atomStyleCheckClass;
3530
3531 static void register_style_check_class(void)
3532 {
3533     WNDCLASS wc =
3534     {
3535         0,
3536         StyleCheckProc,
3537         0,
3538         0,
3539         GetModuleHandle(NULL),
3540         NULL,
3541         LoadCursor(NULL, IDC_ARROW),
3542         (HBRUSH)(COLOR_BTNFACE+1),
3543         NULL,
3544         TEXT("WineStyleCheck"),
3545     };
3546     
3547     atomStyleCheckClass = RegisterClass(&wc);
3548 }
3549
3550 static void check_window_style(DWORD dwStyleIn, DWORD dwExStyleIn, DWORD dwStyleOut, DWORD dwExStyleOut)
3551 {
3552     DWORD dwActualStyle;
3553     DWORD dwActualExStyle;
3554     STYLESTRUCT ss;
3555     HWND hwnd;
3556     HWND hwndParent = NULL;
3557
3558     ss.styleNew = dwStyleIn;
3559     ss.styleOld = dwExStyleIn;
3560
3561     if (dwStyleIn & WS_CHILD)
3562     {
3563         hwndParent = CreateWindowEx(0, MAKEINTATOM(atomStyleCheckClass), NULL,
3564             WS_OVERLAPPEDWINDOW, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
3565     }
3566
3567     hwnd = CreateWindowEx(dwExStyleIn, MAKEINTATOM(atomStyleCheckClass), NULL,
3568                     dwStyleIn, 0, 0, 0, 0, hwndParent, NULL, NULL, &ss);
3569     assert(hwnd);
3570
3571     flush_events( TRUE );
3572
3573     dwActualStyle = GetWindowLong(hwnd, GWL_STYLE);
3574     dwActualExStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
3575     ok((dwActualStyle == dwStyleOut) && (dwActualExStyle == dwExStyleOut),
3576         "Style (0x%08x) should really be 0x%08x and/or Ex style (0x%08x) should really be 0x%08x\n",
3577         dwActualStyle, dwStyleOut, dwActualExStyle, dwExStyleOut);
3578
3579     /* try setting the styles explicitly */
3580     SetWindowLong( hwnd, GWL_EXSTYLE, dwExStyleIn );
3581     SetWindowLong( hwnd, GWL_STYLE, dwStyleIn );
3582     dwActualStyle = GetWindowLong(hwnd, GWL_STYLE);
3583     dwActualExStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
3584     /* WS_CLIPSIBLINGS can't be reset on top-level windows */
3585     if (dwStyleIn & WS_CHILD) dwStyleOut = dwStyleIn;
3586     else dwStyleOut = dwStyleIn | WS_CLIPSIBLINGS;
3587     /* WS_EX_WINDOWEDGE can't always be changed */
3588     if ((dwExStyleIn & WS_EX_DLGMODALFRAME) || (dwStyleIn & WS_THICKFRAME))
3589         dwExStyleOut = dwExStyleIn | WS_EX_WINDOWEDGE;
3590     else if (dwStyleIn & (WS_CHILD | WS_POPUP))
3591         dwExStyleOut = dwExStyleIn & ~WS_EX_WINDOWEDGE;
3592     else
3593         dwExStyleOut = dwExStyleIn;
3594     ok((dwActualStyle == dwStyleOut) && (dwActualExStyle == dwExStyleOut),
3595         "%08x/%08x: Style (0x%08x) should really be 0x%08x and/or Ex style (0x%08x) should really be 0x%08x\n",
3596        dwStyleIn, dwExStyleIn, dwActualStyle, dwStyleOut, dwActualExStyle, dwExStyleOut);
3597
3598     DestroyWindow(hwnd);
3599     if (hwndParent) DestroyWindow(hwndParent);
3600 }
3601
3602 /* tests what window styles the window manager automatically adds */
3603 static void test_window_styles(void)
3604 {
3605     register_style_check_class();
3606
3607     check_window_style(0, 0, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_WINDOWEDGE);
3608     check_window_style(WS_OVERLAPPEDWINDOW, 0, WS_CLIPSIBLINGS|WS_OVERLAPPEDWINDOW, WS_EX_WINDOWEDGE);
3609     check_window_style(WS_CHILD, 0, WS_CHILD, 0);
3610     check_window_style(WS_CHILD, WS_EX_WINDOWEDGE, WS_CHILD, 0);
3611     check_window_style(0, WS_EX_TOOLWINDOW, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_WINDOWEDGE|WS_EX_TOOLWINDOW);
3612     check_window_style(WS_POPUP, 0, WS_POPUP|WS_CLIPSIBLINGS, 0);
3613     check_window_style(WS_POPUP, WS_EX_WINDOWEDGE, WS_POPUP|WS_CLIPSIBLINGS, 0);
3614     check_window_style(WS_CHILD, WS_EX_DLGMODALFRAME, WS_CHILD, WS_EX_WINDOWEDGE|WS_EX_DLGMODALFRAME);
3615     check_window_style(WS_CHILD, WS_EX_DLGMODALFRAME|WS_EX_STATICEDGE, WS_CHILD, WS_EX_STATICEDGE|WS_EX_WINDOWEDGE|WS_EX_DLGMODALFRAME);
3616     check_window_style(WS_CAPTION, WS_EX_STATICEDGE, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_STATICEDGE|WS_EX_WINDOWEDGE);
3617     check_window_style(0, WS_EX_APPWINDOW, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_APPWINDOW|WS_EX_WINDOWEDGE);
3618
3619     if (pGetLayeredWindowAttributes)
3620     {
3621         check_window_style(0, WS_EX_LAYERED, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_LAYERED|WS_EX_WINDOWEDGE);
3622         check_window_style(0, WS_EX_LAYERED|WS_EX_TRANSPARENT, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_LAYERED|WS_EX_TRANSPARENT|WS_EX_WINDOWEDGE);
3623         check_window_style(0, WS_EX_LAYERED|WS_EX_TRANSPARENT|WS_EX_TOOLWINDOW, WS_CLIPSIBLINGS|WS_CAPTION,
3624                                                       WS_EX_LAYERED|WS_EX_TRANSPARENT|WS_EX_TOOLWINDOW|WS_EX_WINDOWEDGE);
3625     }
3626 }
3627
3628 static void test_scrollwindow( HWND hwnd)
3629 {
3630     HDC hdc;
3631     RECT rc, rc2, rc3;
3632     COLORREF colr;
3633
3634     ShowWindow( hwnd, SW_SHOW);
3635     UpdateWindow( hwnd);
3636     flush_events( TRUE );
3637     GetClientRect( hwnd, &rc);
3638     hdc = GetDC( hwnd);
3639     /* test ScrollWindow(Ex) with no clip rectangle */
3640     /* paint the lower half of the window black */
3641     rc2 = rc;
3642     rc2.top = ( rc2.top + rc2.bottom) / 2;
3643     FillRect( hdc, &rc2, GetStockObject(BLACK_BRUSH));
3644     /* paint the upper half of the window white */
3645     rc2.bottom = rc2.top;
3646     rc2.top =0;
3647     FillRect( hdc, &rc2, GetStockObject(WHITE_BRUSH));
3648     /* scroll lower half up */
3649     rc2 = rc;
3650     rc2.top = ( rc2.top + rc2.bottom) / 2;
3651     ScrollWindowEx( hwnd, 0, - rc2.top, &rc2, NULL, NULL, NULL, SW_ERASE);
3652     flush_events(FALSE);
3653     /* expected: black should have scrolled to the upper half */
3654     colr = GetPixel( hdc, (rc2.left+rc2.right)/ 2,  rc2.bottom / 4 );
3655     ok ( colr == 0, "pixel should be black, color is %08x\n", colr);
3656     /* Repeat that test of ScrollWindow(Ex) now with clip rectangle */
3657     /* paint the lower half of the window black */
3658     rc2 = rc;
3659     rc2.top = ( rc2.top + rc2.bottom) / 2;
3660     FillRect( hdc, &rc2, GetStockObject(BLACK_BRUSH));
3661     /* paint the upper half of the window white */
3662     rc2.bottom = rc2.top;
3663     rc2.top =0;
3664     FillRect( hdc, &rc2, GetStockObject(WHITE_BRUSH));
3665     /* scroll lower half up */
3666     rc2 = rc;
3667     rc2.top = ( rc2.top + rc2.bottom) / 2;
3668     rc3 = rc;
3669     rc3.left = rc3.right / 4;
3670     rc3.right -= rc3.right / 4;
3671     ScrollWindowEx( hwnd, 0, - rc2.top, &rc2, &rc3, NULL, NULL, SW_ERASE);
3672     flush_events(FALSE);
3673     /* expected: black should have scrolled to the upper half */
3674     colr = GetPixel( hdc, (rc2.left+rc2.right)/ 2,  rc2.bottom / 4 );
3675     ok ( colr == 0, "pixel should be black, color is %08x\n", colr);
3676
3677     /* clean up */
3678     ReleaseDC( hwnd, hdc);
3679 }
3680
3681 static void test_scrollvalidate( HWND parent)
3682 {
3683     HDC hdc;
3684     HRGN hrgn=CreateRectRgn(0,0,0,0);
3685     HRGN exprgn, tmprgn, clipping;
3686     RECT rc, rcu, cliprc;
3687     /* create two overlapping child windows. The visual region
3688      * of hwnd1 is clipped by the overlapping part of
3689      * hwnd2 because of the WS_CLIPSIBLING style */
3690     HWND hwnd1, hwnd2;
3691
3692     clipping = CreateRectRgn(0,0,0,0);
3693     tmprgn = CreateRectRgn(0,0,0,0);
3694     exprgn = CreateRectRgn(0,0,0,0);
3695     hwnd2 = CreateWindowExA(0, "static", NULL,
3696             WS_CHILD| WS_VISIBLE | WS_CLIPSIBLINGS | WS_BORDER ,
3697             75, 30, 100, 100, parent, 0, 0, NULL);
3698     hwnd1 = CreateWindowExA(0, "static", NULL,
3699             WS_CHILD| WS_VISIBLE | WS_CLIPSIBLINGS | WS_BORDER ,
3700             25, 50, 100, 100, parent, 0, 0, NULL);
3701     ShowWindow( parent, SW_SHOW);
3702     UpdateWindow( parent);
3703     GetClientRect( hwnd1, &rc);
3704     cliprc=rc; 
3705     SetRectRgn( clipping, 10, 10, 90, 90);
3706     hdc = GetDC( hwnd1);
3707     /* for a visual touch */
3708     TextOut( hdc, 0,10, "0123456789", 10);
3709     ScrollDC( hdc, -10, -5, &rc, &cliprc, hrgn, &rcu);
3710     if (winetest_debug > 0) dump_region(hrgn);
3711     /* create a region with what is expected */
3712     SetRectRgn( exprgn, 39,0,49,74);
3713     SetRectRgn( tmprgn, 88,79,98,93);
3714     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
3715     SetRectRgn( tmprgn, 0,93,98,98);
3716     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
3717     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
3718     trace("update rect is %d,%d - %d,%d\n",
3719             rcu.left,rcu.top,rcu.right,rcu.bottom);
3720     /* now with clipping region */
3721     SelectClipRgn( hdc, clipping);
3722     ScrollDC( hdc, -10, -5, &rc, &cliprc, hrgn, &rcu);
3723     if (winetest_debug > 0) dump_region(hrgn);
3724     /* create a region with what is expected */
3725     SetRectRgn( exprgn, 39,10,49,74);
3726     SetRectRgn( tmprgn, 80,79,90,85);
3727     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
3728     SetRectRgn( tmprgn, 10,85,90,90);
3729     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
3730     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
3731     trace("update rect is %d,%d - %d,%d\n",
3732             rcu.left,rcu.top,rcu.right,rcu.bottom);
3733     ReleaseDC( hwnd1, hdc);
3734
3735     /* test scrolling a window with an update region */
3736     DestroyWindow( hwnd2);
3737     ValidateRect( hwnd1, NULL);
3738     SetRect( &rc, 40,40, 50,50);
3739     InvalidateRect( hwnd1, &rc, 1);
3740     GetClientRect( hwnd1, &rc);
3741     cliprc=rc;
3742     ScrollWindowEx( hwnd1, -10, 0, &rc, &cliprc, hrgn, &rcu,
3743       SW_SCROLLCHILDREN | SW_INVALIDATE);
3744     if (winetest_debug > 0) dump_region(hrgn);
3745     SetRectRgn( exprgn, 88,0,98,98);
3746     SetRectRgn( tmprgn, 30, 40, 50, 50);
3747     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
3748     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
3749
3750     /* clear an update region */
3751     UpdateWindow( hwnd1 );
3752
3753     SetRect( &rc, 0,40, 100,60);
3754     SetRect( &cliprc, 0,0, 100,100);
3755     ScrollWindowEx( hwnd1, 0, -25, &rc, &cliprc, hrgn, &rcu, SW_INVALIDATE);
3756     if (winetest_debug > 0) dump_region( hrgn );
3757     SetRectRgn( exprgn, 0, 40, 98, 60 );
3758     ok( EqualRgn( exprgn, hrgn), "wrong update region in excessive scroll\n");
3759
3760     /* now test ScrollWindowEx with a combination of
3761      * WS_CLIPCHILDREN style and SW_SCROLLCHILDREN flag */
3762     /* make hwnd2 the child of hwnd1 */
3763     hwnd2 = CreateWindowExA(0, "static", NULL,
3764             WS_CHILD| WS_VISIBLE | WS_BORDER ,
3765             50, 50, 100, 100, hwnd1, 0, 0, NULL);
3766     SetWindowLong( hwnd1, GWL_STYLE, GetWindowLong( hwnd1, GWL_STYLE) & ~WS_CLIPSIBLINGS);
3767     GetClientRect( hwnd1, &rc);
3768     cliprc=rc;
3769
3770     /* WS_CLIPCHILDREN and SW_SCROLLCHILDREN */
3771     SetWindowLong( hwnd1, GWL_STYLE, GetWindowLong( hwnd1, GWL_STYLE) | WS_CLIPCHILDREN );
3772     ValidateRect( hwnd1, NULL);
3773     ValidateRect( hwnd2, NULL);
3774     ScrollWindowEx( hwnd1, -10, -10, &rc, &cliprc, hrgn, &rcu,
3775       SW_SCROLLCHILDREN | SW_INVALIDATE);
3776     if (winetest_debug > 0) dump_region(hrgn);
3777     SetRectRgn( exprgn, 88,0,98,88);
3778     SetRectRgn( tmprgn, 0,88,98,98);
3779     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
3780     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
3781
3782     /* SW_SCROLLCHILDREN */
3783     SetWindowLong( hwnd1, GWL_STYLE, GetWindowLong( hwnd1, GWL_STYLE) & ~WS_CLIPCHILDREN );
3784     ValidateRect( hwnd1, NULL);
3785     ValidateRect( hwnd2, NULL);
3786     ScrollWindowEx( hwnd1, -10, -10, &rc, &cliprc, hrgn, &rcu, SW_SCROLLCHILDREN | SW_INVALIDATE);
3787     if (winetest_debug > 0) dump_region(hrgn);
3788     /* expected region is the same as in previous test */
3789     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
3790
3791     /* no SW_SCROLLCHILDREN */
3792     SetWindowLong( hwnd1, GWL_STYLE, GetWindowLong( hwnd1, GWL_STYLE) & ~WS_CLIPCHILDREN );
3793     ValidateRect( hwnd1, NULL);
3794     ValidateRect( hwnd2, NULL);
3795     ScrollWindowEx( hwnd1, -10, -10, &rc, &cliprc, hrgn, &rcu, SW_INVALIDATE);
3796     if (winetest_debug > 0) dump_region(hrgn);
3797     /* expected region is the same as in previous test */
3798     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
3799
3800     /* WS_CLIPCHILDREN and no SW_SCROLLCHILDREN */
3801     SetWindowLong( hwnd1, GWL_STYLE, GetWindowLong( hwnd1, GWL_STYLE) | WS_CLIPCHILDREN );
3802     ValidateRect( hwnd1, NULL);
3803     ValidateRect( hwnd2, NULL);
3804     ScrollWindowEx( hwnd1, -10, -10, &rc, &cliprc, hrgn, &rcu, SW_INVALIDATE);
3805     if (winetest_debug > 0) dump_region(hrgn);
3806     SetRectRgn( exprgn, 88,0,98,20);
3807     SetRectRgn( tmprgn, 20,20,98,30);
3808     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
3809     SetRectRgn( tmprgn, 20,30,30,88);
3810     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
3811     SetRectRgn( tmprgn, 0,88,30,98);
3812     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
3813     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
3814
3815     /* clean up */
3816     DeleteObject( hrgn);
3817     DeleteObject( exprgn);
3818     DeleteObject( tmprgn);
3819     DestroyWindow( hwnd1);
3820     DestroyWindow( hwnd2);
3821 }
3822
3823 /* couple of tests of return values of scrollbar functions
3824  * called on a scrollbarless window */ 
3825 static void test_scroll(void)
3826 {
3827     BOOL ret;
3828     INT min, max;
3829     SCROLLINFO si;
3830     HWND hwnd = CreateWindowExA(0, "Static", "Wine test window",
3831         WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_POPUP,
3832         100, 100, 200, 200, 0, 0, 0, NULL);
3833     /* horizontal */
3834     ret = GetScrollRange( hwnd, SB_HORZ, &min, &max);
3835     if (!ret)  /* win9x */
3836     {
3837         win_skip( "GetScrollRange doesn't work\n" );
3838         DestroyWindow( hwnd);
3839         return;
3840     }
3841     ok( min == 0, "minimum scroll pos is %d (should be zero)\n", min);
3842     ok( max == 0, "maximum scroll pos is %d (should be zero)\n", min);
3843     si.cbSize = sizeof( si);
3844     si.fMask = SIF_PAGE;
3845     si.nPage = 0xdeadbeef;
3846     ret = GetScrollInfo( hwnd, SB_HORZ, &si);
3847     ok( !ret, "GetScrollInfo returns %d (should be zero)\n", ret);
3848     ok( si.nPage == 0xdeadbeef, "unexpected value for nPage is %d\n", si.nPage);
3849     /* vertical */
3850     ret = GetScrollRange( hwnd, SB_VERT, &min, &max);
3851     ok( ret, "GetScrollRange returns FALSE\n");
3852     ok( min == 0, "minimum scroll pos is %d (should be zero)\n", min);
3853     ok( max == 0, "maximum scroll pos is %d (should be zero)\n", min);
3854     si.cbSize = sizeof( si);
3855     si.fMask = SIF_PAGE;
3856     si.nPage = 0xdeadbeef;
3857     ret = GetScrollInfo( hwnd, SB_VERT, &si);
3858     ok( !ret, "GetScrollInfo returns %d (should be zero)\n", ret);
3859     ok( si.nPage == 0xdeadbeef, "unexpected value for nPage is %d\n", si.nPage);
3860     /* clean up */
3861     DestroyWindow( hwnd);
3862 }
3863
3864 static void test_scrolldc( HWND parent)
3865 {
3866     HDC hdc;
3867     HRGN exprgn, tmprgn, hrgn;
3868     RECT rc, rc2, rcu, cliprc;
3869     HWND hwnd1;
3870     COLORREF colr;
3871
3872     hrgn = CreateRectRgn(0,0,0,0);
3873     tmprgn = CreateRectRgn(0,0,0,0);
3874     exprgn = CreateRectRgn(0,0,0,0);
3875
3876     hwnd1 = CreateWindowExA(0, "static", NULL,
3877             WS_CHILD| WS_VISIBLE,
3878             25, 50, 100, 100, parent, 0, 0, NULL);
3879     ShowWindow( parent, SW_SHOW);
3880     UpdateWindow( parent);
3881     flush_events( TRUE );
3882     GetClientRect( hwnd1, &rc);
3883     hdc = GetDC( hwnd1);
3884     /* paint the upper half of the window black */
3885     rc2 = rc;
3886     rc2.bottom = ( rc.top + rc.bottom) /2;
3887     FillRect( hdc, &rc2, GetStockObject(BLACK_BRUSH));
3888     /* clip region is the lower half */
3889     cliprc=rc; 
3890     cliprc.top = (rc.top + rc.bottom) /2;
3891     /* test whether scrolled pixels are properly clipped */ 
3892     colr = GetPixel( hdc, (rc.left+rc.right)/2, ( rc.top + rc.bottom) /2 - 1);
3893     ok ( colr == 0, "pixel should be black, color is %08x\n", colr);
3894     /* this scroll should not cause any visible changes */ 
3895     ScrollDC( hdc, 5, -20, &rc, &cliprc, hrgn, &rcu);
3896     colr = GetPixel( hdc, (rc.left+rc.right)/2, ( rc.top + rc.bottom) /2 - 1);
3897     ok ( colr == 0, "pixel should be black, color is %08x\n", colr);
3898     /* test with NULL clip rect */
3899     ScrollDC( hdc, 20, -20, &rc, NULL, hrgn, &rcu);
3900     /*FillRgn(hdc, hrgn, GetStockObject(WHITE_BRUSH));*/
3901     trace("update rect: %d,%d - %d,%d\n",
3902            rcu.left, rcu.top, rcu.right, rcu.bottom);
3903     if (winetest_debug > 0) dump_region(hrgn);
3904     SetRect(&rc2, 0, 0, 100, 100);
3905     ok(EqualRect(&rcu, &rc2), "rects do not match (%d,%d-%d,%d) / (%d,%d-%d,%d)\n",
3906        rcu.left, rcu.top, rcu.right, rcu.bottom, rc2.left, rc2.top, rc2.right, rc2.bottom);
3907
3908     SetRectRgn( exprgn, 0, 0, 20, 80);
3909     SetRectRgn( tmprgn, 0, 80, 100, 100);
3910     CombineRgn(exprgn, exprgn, tmprgn, RGN_OR);
3911     if (winetest_debug > 0) dump_region(exprgn);
3912     ok(EqualRgn(exprgn, hrgn), "wrong update region\n");
3913     /* test clip rect > scroll rect */ 
3914     FillRect( hdc, &rc, GetStockObject(WHITE_BRUSH));
3915     rc2=rc;
3916     InflateRect( &rc2, -(rc.right-rc.left)/4, -(rc.bottom-rc.top)/4);
3917     FillRect( hdc, &rc2, GetStockObject(BLACK_BRUSH));
3918     ScrollDC( hdc, 10, 10, &rc2, &rc, hrgn, &rcu);
3919     SetRectRgn( exprgn, 25, 25, 75, 35);
3920     SetRectRgn( tmprgn, 25, 35, 35, 75);
3921     CombineRgn(exprgn, exprgn, tmprgn, RGN_OR);
3922     ok(EqualRgn(exprgn, hrgn), "wrong update region\n");
3923     trace("update rect: %d,%d - %d,%d\n",
3924            rcu.left, rcu.top, rcu.right, rcu.bottom);
3925     if (winetest_debug > 0) dump_region(hrgn);
3926
3927     /* clean up */
3928     DeleteObject(hrgn);
3929     DeleteObject(exprgn);
3930     DeleteObject(tmprgn);
3931     DestroyWindow(hwnd1);
3932 }
3933
3934 static void test_params(void)
3935 {
3936     HWND hwnd;
3937     INT rc;
3938
3939     /* Just a param check */
3940     if (pGetMonitorInfoA)
3941     {
3942         SetLastError(0xdeadbeef);
3943         rc = GetWindowText(hwndMain2, NULL, 1024);
3944         ok( rc==0, "GetWindowText: rc=%d err=%d\n",rc,GetLastError());
3945     }
3946     else
3947     {
3948         /* Skips actually on Win95 and NT4 */
3949         win_skip("Test would crash on Win95\n");
3950     }
3951
3952     SetLastError(0xdeadbeef);
3953     hwnd=CreateWindow("LISTBOX", "TestList",
3954                       (LBS_STANDARD & ~LBS_SORT),
3955                       0, 0, 100, 100,
3956                       NULL, (HMENU)1, NULL, 0);
3957
3958     ok(!hwnd || broken(hwnd != NULL), /* w2k3 sp2 */
3959        "CreateWindow with invalid menu handle should fail\n");
3960     if (!hwnd)
3961         ok(GetLastError() == ERROR_INVALID_MENU_HANDLE || /* NT */
3962            GetLastError() == 0xdeadbeef, /* Win9x */
3963            "wrong last error value %d\n", GetLastError());
3964 }
3965
3966 static void test_AWRwindow(LPCSTR class, LONG style, LONG exStyle, BOOL menu)
3967 {
3968     HWND hwnd = 0;
3969
3970     hwnd = CreateWindowEx(exStyle, class, class, style,
3971                           110, 100,
3972                           225, 200,
3973                           0,
3974                           menu ? hmenu : 0,
3975                           0, 0);
3976     if (!hwnd) {
3977         trace("Failed to create window class=%s, style=0x%08x, exStyle=0x%08x\n", class, style, exStyle);
3978         return;
3979     }
3980     ShowWindow(hwnd, SW_SHOW);
3981
3982     test_nonclient_area(hwnd);
3983
3984     SetMenu(hwnd, 0);
3985     DestroyWindow(hwnd);
3986 }
3987
3988 static BOOL AWR_init(void)
3989 {
3990     WNDCLASS class;
3991
3992     class.style         = CS_HREDRAW | CS_VREDRAW;
3993     class.lpfnWndProc     = DefWindowProcA;
3994     class.cbClsExtra    = 0;
3995     class.cbWndExtra    = 0;
3996     class.hInstance     = 0;
3997     class.hIcon         = LoadIcon (0, IDI_APPLICATION);
3998     class.hCursor       = LoadCursor (0, IDC_ARROW);
3999     class.hbrBackground = 0;
4000     class.lpszMenuName  = 0;
4001     class.lpszClassName = szAWRClass;
4002     
4003     if (!RegisterClass (&class)) {
4004         ok(FALSE, "RegisterClass failed\n");
4005         return FALSE;
4006     }
4007
4008     hmenu = CreateMenu();
4009     if (!hmenu)
4010         return FALSE;
4011     ok(hmenu != 0, "Failed to create menu\n");
4012     ok(AppendMenu(hmenu, MF_STRING, 1, "Test!"), "Failed to create menu item\n");
4013     
4014     return TRUE;
4015 }
4016
4017
4018 static void test_AWR_window_size(BOOL menu)
4019 {
4020     LONG styles[] = {
4021         WS_POPUP,
4022         WS_MAXIMIZE, WS_BORDER, WS_DLGFRAME, 
4023         WS_SYSMENU, 
4024         WS_THICKFRAME,
4025         WS_MINIMIZEBOX, WS_MAXIMIZEBOX,
4026         WS_HSCROLL, WS_VSCROLL
4027     };
4028     LONG exStyles[] = {
4029         WS_EX_CLIENTEDGE,
4030         WS_EX_TOOLWINDOW, WS_EX_WINDOWEDGE,
4031         WS_EX_APPWINDOW,
4032 #if 0
4033         /* These styles have problems on (at least) WinXP (SP2) and Wine */
4034         WS_EX_DLGMODALFRAME, 
4035         WS_EX_STATICEDGE, 
4036 #endif
4037     };
4038
4039     int i;    
4040
4041     /* A exhaustive check of all the styles takes too long
4042      * so just do a (hopefully representative) sample
4043      */
4044     for (i = 0; i < COUNTOF(styles); ++i)
4045         test_AWRwindow(szAWRClass, styles[i], 0, menu);
4046     for (i = 0; i < COUNTOF(exStyles); ++i) {
4047         test_AWRwindow(szAWRClass, WS_POPUP, exStyles[i], menu);
4048         test_AWRwindow(szAWRClass, WS_THICKFRAME, exStyles[i], menu);
4049     }
4050 }
4051 #undef COUNTOF
4052
4053 #define SHOWSYSMETRIC(SM) trace(#SM "=%d\n", GetSystemMetrics(SM))
4054
4055 static void test_AdjustWindowRect(void)
4056 {
4057     if (!AWR_init())
4058         return;
4059     
4060     SHOWSYSMETRIC(SM_CYCAPTION);
4061     SHOWSYSMETRIC(SM_CYSMCAPTION);
4062     SHOWSYSMETRIC(SM_CYMENU);
4063     SHOWSYSMETRIC(SM_CXEDGE);
4064     SHOWSYSMETRIC(SM_CYEDGE);
4065     SHOWSYSMETRIC(SM_CXVSCROLL);
4066     SHOWSYSMETRIC(SM_CYHSCROLL);
4067     SHOWSYSMETRIC(SM_CXFRAME);
4068     SHOWSYSMETRIC(SM_CYFRAME);
4069     SHOWSYSMETRIC(SM_CXDLGFRAME);
4070     SHOWSYSMETRIC(SM_CYDLGFRAME);
4071     SHOWSYSMETRIC(SM_CXBORDER);
4072     SHOWSYSMETRIC(SM_CYBORDER);  
4073
4074     test_AWR_window_size(FALSE);
4075     test_AWR_window_size(TRUE);
4076
4077     DestroyMenu(hmenu);
4078 }
4079 #undef SHOWSYSMETRIC
4080
4081
4082 /* Global variables to trigger exit from loop */
4083 static int redrawComplete, WMPAINT_count;
4084
4085 static LRESULT WINAPI redraw_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
4086 {
4087     switch (msg)
4088     {
4089     case WM_PAINT:
4090         trace("doing WM_PAINT %d\n", WMPAINT_count);
4091         WMPAINT_count++;
4092         if (WMPAINT_count > 10 && redrawComplete == 0) {
4093             PAINTSTRUCT ps;
4094             BeginPaint(hwnd, &ps);
4095             EndPaint(hwnd, &ps);
4096             return 1;
4097         }
4098         return 0;
4099     }
4100     return DefWindowProc(hwnd, msg, wparam, lparam);
4101 }
4102
4103 /* Ensure we exit from RedrawNow regardless of invalidated area */
4104 static void test_redrawnow(void)
4105 {
4106    WNDCLASSA cls;
4107    HWND hwndMain;
4108
4109    cls.style = CS_DBLCLKS;
4110    cls.lpfnWndProc = redraw_window_procA;
4111    cls.cbClsExtra = 0;
4112    cls.cbWndExtra = 0;
4113    cls.hInstance = GetModuleHandleA(0);
4114    cls.hIcon = 0;
4115    cls.hCursor = LoadCursorA(0, IDC_ARROW);
4116    cls.hbrBackground = GetStockObject(WHITE_BRUSH);
4117    cls.lpszMenuName = NULL;
4118    cls.lpszClassName = "RedrawWindowClass";
4119
4120    if(!RegisterClassA(&cls)) {
4121        trace("Register failed %d\n", GetLastError());
4122        return;
4123    }
4124
4125    hwndMain = CreateWindowA("RedrawWindowClass", "Main Window", WS_OVERLAPPEDWINDOW,
4126                             CW_USEDEFAULT, 0, 100, 100, NULL, NULL, 0, NULL);
4127
4128    ok( WMPAINT_count == 0, "Multiple unexpected WM_PAINT calls %d\n", WMPAINT_count);
4129    ShowWindow(hwndMain, SW_SHOW);
4130    ok( WMPAINT_count == 0, "Multiple unexpected WM_PAINT calls %d\n", WMPAINT_count);
4131    RedrawWindow(hwndMain, NULL,NULL,RDW_UPDATENOW | RDW_ALLCHILDREN);
4132    ok( WMPAINT_count == 1 || broken(WMPAINT_count == 0), /* sometimes on win9x */
4133        "Multiple unexpected WM_PAINT calls %d\n", WMPAINT_count);
4134    redrawComplete = TRUE;
4135    ok( WMPAINT_count < 10, "RedrawWindow (RDW_UPDATENOW) never completed (%d)\n", WMPAINT_count);
4136
4137    /* clean up */
4138    DestroyWindow( hwndMain);
4139 }
4140
4141 struct parentdc_stat {
4142     RECT client;
4143     RECT clip;
4144     RECT paint;
4145 };
4146
4147 struct parentdc_test {
4148    struct parentdc_stat main, main_todo;
4149    struct parentdc_stat child1, child1_todo;
4150    struct parentdc_stat child2, child2_todo;
4151 };
4152
4153 static LRESULT WINAPI parentdc_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
4154 {
4155     RECT rc;
4156     PAINTSTRUCT ps;
4157
4158     struct parentdc_stat *t = (struct parentdc_stat *)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
4159
4160     switch (msg)
4161     {
4162     case WM_PAINT:
4163         GetClientRect(hwnd, &rc);
4164         CopyRect(&t->client, &rc);
4165         GetWindowRect(hwnd, &rc);
4166         trace("WM_PAINT: hwnd %p, client rect (%d,%d)-(%d,%d), window rect (%d,%d)-(%d,%d)\n", hwnd,
4167               t->client.left, t->client.top, t->client.right, t->client.bottom,
4168               rc.left, rc.top, rc.right, rc.bottom);
4169         BeginPaint(hwnd, &ps);
4170         CopyRect(&t->paint, &ps.rcPaint);
4171         GetClipBox(ps.hdc, &rc);
4172         CopyRect(&t->clip, &rc);
4173         trace("clip rect (%d,%d)-(%d,%d), paint rect (%d,%d)-(%d,%d)\n",
4174               rc.left, rc.top, rc.right, rc.bottom,
4175               ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
4176         EndPaint(hwnd, &ps);
4177         return 0;
4178     }
4179     return DefWindowProc(hwnd, msg, wparam, lparam);
4180 }
4181
4182 static void zero_parentdc_stat(struct parentdc_stat *t)
4183 {
4184     SetRectEmpty(&t->client);
4185     SetRectEmpty(&t->clip);
4186     SetRectEmpty(&t->paint);
4187 }
4188
4189 static void zero_parentdc_test(struct parentdc_test *t)
4190 {
4191     zero_parentdc_stat(&t->main);
4192     zero_parentdc_stat(&t->child1);
4193     zero_parentdc_stat(&t->child2);
4194 }
4195
4196 #define parentdc_field_ok(t, w, r, f, got) \
4197   ok (t.w.r.f==got.w.r.f, "window " #w ", rect " #r ", field " #f \
4198       ": expected %d, got %d\n", \
4199       t.w.r.f, got.w.r.f)
4200
4201 #define parentdc_todo_field_ok(t, w, r, f, got) \
4202   if (t.w##_todo.r.f) todo_wine { parentdc_field_ok(t, w, r, f, got); } \
4203   else parentdc_field_ok(t, w, r, f, got)
4204
4205 #define parentdc_rect_ok(t, w, r, got) \
4206   parentdc_todo_field_ok(t, w, r, left, got); \
4207   parentdc_todo_field_ok(t, w, r, top, got); \
4208   parentdc_todo_field_ok(t, w, r, right, got); \
4209   parentdc_todo_field_ok(t, w, r, bottom, got);
4210
4211 #define parentdc_win_ok(t, w, got) \
4212   parentdc_rect_ok(t, w, client, got); \
4213   parentdc_rect_ok(t, w, clip, got); \
4214   parentdc_rect_ok(t, w, paint, got);
4215
4216 #define parentdc_ok(t, got) \
4217   parentdc_win_ok(t, main, got); \
4218   parentdc_win_ok(t, child1, got); \
4219   parentdc_win_ok(t, child2, got);
4220
4221 static void test_csparentdc(void)
4222 {
4223    WNDCLASSA clsMain, cls;
4224    HWND hwndMain, hwnd1, hwnd2;
4225    RECT rc;
4226
4227    struct parentdc_test test_answer;
4228
4229 #define nothing_todo {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}
4230    const struct parentdc_test test1 = 
4231    {
4232         {{0, 0, 150, 150}, {0, 0, 150, 150}, {0, 0, 150, 150}}, nothing_todo,
4233         {{0, 0, 40, 40}, {-20, -20, 130, 130}, {0, 0, 40, 40}}, {{0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}},
4234         {{0, 0, 40, 40}, {-40, -40, 110, 110}, {0, 0, 40, 40}}, {{0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}},
4235    };
4236
4237    const struct parentdc_test test2 = 
4238    {
4239         {{0, 0, 150, 150}, {0, 0, 50, 50}, {0, 0, 50, 50}}, nothing_todo,
4240         {{0, 0, 40, 40}, {-20, -20, 30, 30}, {0, 0, 30, 30}}, {{0, 0, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}},
4241         {{0, 0, 40, 40}, {-40, -40, 10, 10}, {0, 0, 10, 10}}, {{0, 0, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}},
4242    };
4243
4244    const struct parentdc_test test3 = 
4245    {
4246         {{0, 0, 150, 150}, {0, 0, 10, 10}, {0, 0, 10, 10}}, nothing_todo,
4247         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
4248         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
4249    };
4250
4251    const struct parentdc_test test4 = 
4252    {
4253         {{0, 0, 150, 150}, {40, 40, 50, 50}, {40, 40, 50, 50}}, nothing_todo,
4254         {{0, 0, 40, 40}, {20, 20, 30, 30}, {20, 20, 30, 30}}, nothing_todo,
4255         {{0, 0, 40, 40}, {0, 0, 10, 10}, {0, 0, 10, 10}}, nothing_todo,
4256    };
4257
4258    const struct parentdc_test test5 = 
4259    {
4260         {{0, 0, 150, 150}, {20, 20, 60, 60}, {20, 20, 60, 60}}, nothing_todo,
4261         {{0, 0, 40, 40}, {-20, -20, 130, 130}, {0, 0, 40, 40}}, {{0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}},
4262         {{0, 0, 40, 40}, {-20, -20, 20, 20}, {0, 0, 20, 20}}, {{0, 0, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}},
4263    };
4264
4265    const struct parentdc_test test6 = 
4266    {
4267         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
4268         {{0, 0, 40, 40}, {0, 0, 10, 10}, {0, 0, 10, 10}}, nothing_todo,
4269         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
4270    };
4271
4272    const struct parentdc_test test7 = 
4273    {
4274         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
4275         {{0, 0, 40, 40}, {-20, -20, 130, 130}, {0, 0, 40, 40}}, {{0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}},
4276         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
4277    };
4278 #undef nothing_todo
4279
4280    clsMain.style = CS_DBLCLKS;
4281    clsMain.lpfnWndProc = parentdc_window_procA;
4282    clsMain.cbClsExtra = 0;
4283    clsMain.cbWndExtra = 0;
4284    clsMain.hInstance = GetModuleHandleA(0);
4285    clsMain.hIcon = 0;
4286    clsMain.hCursor = LoadCursorA(0, IDC_ARROW);
4287    clsMain.hbrBackground = GetStockObject(WHITE_BRUSH);
4288    clsMain.lpszMenuName = NULL;
4289    clsMain.lpszClassName = "ParentDcMainWindowClass";
4290
4291    if(!RegisterClassA(&clsMain)) {
4292        trace("Register failed %d\n", GetLastError());
4293        return;
4294    }
4295
4296    cls.style = CS_DBLCLKS | CS_PARENTDC;
4297    cls.lpfnWndProc = parentdc_window_procA;
4298    cls.cbClsExtra = 0;
4299    cls.cbWndExtra = 0;
4300    cls.hInstance = GetModuleHandleA(0);
4301    cls.hIcon = 0;
4302    cls.hCursor = LoadCursorA(0, IDC_ARROW);
4303    cls.hbrBackground = GetStockObject(WHITE_BRUSH);
4304    cls.lpszMenuName = NULL;
4305    cls.lpszClassName = "ParentDcWindowClass";
4306
4307    if(!RegisterClassA(&cls)) {
4308        trace("Register failed %d\n", GetLastError());
4309        return;
4310    }
4311
4312    SetRect(&rc, 0, 0, 150, 150);
4313    AdjustWindowRectEx(&rc, WS_OVERLAPPEDWINDOW, FALSE, 0);
4314    hwndMain = CreateWindowA("ParentDcMainWindowClass", "Main Window", WS_OVERLAPPEDWINDOW,
4315                             CW_USEDEFAULT, 0, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, 0, NULL);
4316    SetWindowLongPtrA(hwndMain, GWLP_USERDATA, (DWORD_PTR)&test_answer.main);
4317    hwnd1 = CreateWindowA("ParentDcWindowClass", "Child Window 1", WS_CHILD,
4318                             20, 20, 40, 40, hwndMain, NULL, 0, NULL);
4319    SetWindowLongPtrA(hwnd1, GWLP_USERDATA, (DWORD_PTR)&test_answer.child1);
4320    hwnd2 = CreateWindowA("ParentDcWindowClass", "Child Window 2", WS_CHILD,
4321                             40, 40, 40, 40, hwndMain, NULL, 0, NULL);
4322    SetWindowLongPtrA(hwnd2, GWLP_USERDATA, (DWORD_PTR)&test_answer.child2);
4323    ShowWindow(hwndMain, SW_SHOW);
4324    ShowWindow(hwnd1, SW_SHOW);
4325    ShowWindow(hwnd2, SW_SHOW);
4326    SetWindowPos(hwndMain, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
4327    flush_events( TRUE );
4328
4329    zero_parentdc_test(&test_answer);
4330    InvalidateRect(hwndMain, NULL, TRUE);
4331    flush_events( TRUE );
4332    parentdc_ok(test1, test_answer);
4333
4334    zero_parentdc_test(&test_answer);
4335    SetRect(&rc, 0, 0, 50, 50);
4336    InvalidateRect(hwndMain, &rc, TRUE);
4337    flush_events( TRUE );
4338    parentdc_ok(test2, test_answer);
4339
4340    zero_parentdc_test(&test_answer);
4341    SetRect(&rc, 0, 0, 10, 10);
4342    InvalidateRect(hwndMain, &rc, TRUE);
4343    flush_events( TRUE );
4344    parentdc_ok(test3, test_answer);
4345
4346    zero_parentdc_test(&test_answer);
4347    SetRect(&rc, 40, 40, 50, 50);
4348    InvalidateRect(hwndMain, &rc, TRUE);
4349    flush_events( TRUE );
4350    parentdc_ok(test4, test_answer);
4351
4352    zero_parentdc_test(&test_answer);
4353    SetRect(&rc, 20, 20, 60, 60);
4354    InvalidateRect(hwndMain, &rc, TRUE);
4355    flush_events( TRUE );
4356    parentdc_ok(test5, test_answer);
4357
4358    zero_parentdc_test(&test_answer);
4359    SetRect(&rc, 0, 0, 10, 10);
4360    InvalidateRect(hwnd1, &rc, TRUE);
4361    flush_events( TRUE );
4362    parentdc_ok(test6, test_answer);
4363
4364    zero_parentdc_test(&test_answer);
4365    SetRect(&rc, -5, -5, 65, 65);
4366    InvalidateRect(hwnd1, &rc, TRUE);
4367    flush_events( TRUE );
4368    parentdc_ok(test7, test_answer);
4369
4370    DestroyWindow(hwndMain);
4371    DestroyWindow(hwnd1);
4372    DestroyWindow(hwnd2);
4373 }
4374
4375 static LRESULT WINAPI def_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
4376 {
4377     return DefWindowProcA(hwnd, msg, wparam, lparam);
4378 }
4379
4380 static LRESULT WINAPI def_window_procW(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
4381 {
4382     return DefWindowProcW(hwnd, msg, wparam, lparam);
4383 }
4384
4385 static void test_IsWindowUnicode(void)
4386 {
4387     static const char ansi_class_nameA[] = "ansi class name";
4388     static const WCHAR ansi_class_nameW[] = {'a','n','s','i',' ','c','l','a','s','s',' ','n','a','m','e',0};
4389     static const char unicode_class_nameA[] = "unicode class name";
4390     static const WCHAR unicode_class_nameW[] = {'u','n','i','c','o','d','e',' ','c','l','a','s','s',' ','n','a','m','e',0};
4391     WNDCLASSA classA;
4392     WNDCLASSW classW;
4393     HWND hwnd;
4394
4395     memset(&classW, 0, sizeof(classW));
4396     classW.hInstance = GetModuleHandleA(0);
4397     classW.lpfnWndProc = def_window_procW;
4398     classW.lpszClassName = unicode_class_nameW;
4399     if (!RegisterClassW(&classW)) return; /* this catches Win9x as well */
4400
4401     memset(&classA, 0, sizeof(classA));
4402     classA.hInstance = GetModuleHandleA(0);
4403     classA.lpfnWndProc = def_window_procA;
4404     classA.lpszClassName = ansi_class_nameA;
4405     assert(RegisterClassA(&classA));
4406
4407     /* unicode class: window proc */
4408     hwnd = CreateWindowExW(0, unicode_class_nameW, NULL, WS_POPUP,
4409                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
4410     assert(hwnd);
4411
4412     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
4413     SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (ULONG_PTR)def_window_procA);
4414     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
4415     SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (ULONG_PTR)def_window_procW);
4416     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
4417
4418     DestroyWindow(hwnd);
4419
4420     hwnd = CreateWindowExA(0, unicode_class_nameA, NULL, WS_POPUP,
4421                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
4422     assert(hwnd);
4423
4424     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
4425     SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (ULONG_PTR)def_window_procA);
4426     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
4427     SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (ULONG_PTR)def_window_procW);
4428     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
4429
4430     DestroyWindow(hwnd);
4431
4432     /* ansi class: window proc */
4433     hwnd = CreateWindowExW(0, ansi_class_nameW, NULL, WS_POPUP,
4434                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
4435     assert(hwnd);
4436
4437     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
4438     SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (ULONG_PTR)def_window_procW);
4439     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
4440     SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (ULONG_PTR)def_window_procA);
4441     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
4442
4443     DestroyWindow(hwnd);
4444
4445     hwnd = CreateWindowExA(0, ansi_class_nameA, NULL, WS_POPUP,
4446                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
4447     assert(hwnd);
4448
4449     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
4450     SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (ULONG_PTR)def_window_procW);
4451     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
4452     SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (ULONG_PTR)def_window_procA);
4453     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
4454
4455     DestroyWindow(hwnd);
4456
4457     /* unicode class: class proc */
4458     hwnd = CreateWindowExW(0, unicode_class_nameW, NULL, WS_POPUP,
4459                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
4460     assert(hwnd);
4461
4462     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
4463     SetClassLongPtrA(hwnd, GCLP_WNDPROC, (ULONG_PTR)def_window_procA);
4464     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
4465     /* do not restore class window proc back to unicode */
4466
4467     DestroyWindow(hwnd);
4468
4469     hwnd = CreateWindowExA(0, unicode_class_nameA, NULL, WS_POPUP,
4470                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
4471     assert(hwnd);
4472
4473     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
4474     SetClassLongPtrW(hwnd, GCLP_WNDPROC, (ULONG_PTR)def_window_procW);
4475     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
4476
4477     DestroyWindow(hwnd);
4478
4479     /* ansi class: class proc */
4480     hwnd = CreateWindowExW(0, ansi_class_nameW, NULL, WS_POPUP,
4481                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
4482     assert(hwnd);
4483
4484     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
4485     SetClassLongPtrW(hwnd, GCLP_WNDPROC, (ULONG_PTR)def_window_procW);
4486     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
4487     /* do not restore class window proc back to ansi */
4488
4489     DestroyWindow(hwnd);
4490
4491     hwnd = CreateWindowExA(0, ansi_class_nameA, NULL, WS_POPUP,
4492                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
4493     assert(hwnd);
4494
4495     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
4496     SetClassLongPtrA(hwnd, GCLP_WNDPROC, (ULONG_PTR)def_window_procA);
4497     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
4498
4499     DestroyWindow(hwnd);
4500 }
4501
4502 static LRESULT CALLBACK minmax_wnd_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
4503 {
4504     MINMAXINFO *minmax;
4505
4506     if (msg != WM_GETMINMAXINFO)
4507         return DefWindowProc(hwnd, msg, wp, lp);
4508
4509     minmax = (MINMAXINFO *)lp;
4510
4511     if ((GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD))
4512     {
4513         minmax->ptReserved.x = 0;
4514         minmax->ptReserved.y = 0;
4515         minmax->ptMaxSize.x = 400;
4516         minmax->ptMaxSize.y = 400;
4517         minmax->ptMaxPosition.x = 300;
4518         minmax->ptMaxPosition.y = 300;
4519         minmax->ptMaxTrackSize.x = 200;
4520         minmax->ptMaxTrackSize.y = 200;
4521         minmax->ptMinTrackSize.x = 100;
4522         minmax->ptMinTrackSize.y = 100;
4523     }
4524     else
4525         DefWindowProc(hwnd, msg, wp, lp);
4526     return 1;
4527 }
4528
4529 static int expected_cx, expected_cy;
4530 static RECT expected_rect, broken_rect;
4531
4532 static LRESULT CALLBACK winsizes_wnd_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
4533 {
4534     switch(msg)
4535     {
4536     case WM_GETMINMAXINFO:
4537     {
4538         RECT rect;
4539         GetWindowRect( hwnd, &rect );
4540         ok( !rect.left && !rect.top && !rect.right && !rect.bottom,
4541             "wrong rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
4542         return DefWindowProc(hwnd, msg, wp, lp);
4543     }
4544     case WM_NCCREATE:
4545     case WM_CREATE:
4546     {
4547         CREATESTRUCTA *cs = (CREATESTRUCTA *)lp;
4548         RECT rect;
4549         GetWindowRect( hwnd, &rect );
4550         trace( "hwnd %p msg %x size %dx%d rect %d,%d-%d,%d\n",
4551                hwnd, msg, cs->cx, cs->cy, rect.left, rect.top, rect.right, rect.bottom );
4552         ok( cs->cx == expected_cx || broken(cs->cx == (short)expected_cx),
4553             "wrong x size %d/%d\n", cs->cx, expected_cx );
4554         ok( cs->cy == expected_cy || broken(cs->cy == (short)expected_cy),
4555             "wrong y size %d/%d\n", cs->cy, expected_cy );
4556         ok( (rect.right - rect.left == expected_rect.right - expected_rect.left &&
4557              rect.bottom - rect.top == expected_rect.bottom - expected_rect.top) ||
4558             (rect.right - rect.left == min( 65535, expected_rect.right - expected_rect.left ) &&
4559              rect.bottom - rect.top == min( 65535, expected_rect.bottom - expected_rect.top )) ||
4560             broken( rect.right - rect.left == broken_rect.right - broken_rect.left &&
4561                     rect.bottom - rect.top == broken_rect.bottom - broken_rect.top) ||
4562             broken( rect.right - rect.left == (short)broken_rect.right - (short)broken_rect.left &&
4563                     rect.bottom - rect.top == (short)broken_rect.bottom - (short)broken_rect.top),
4564             "wrong rect %d,%d-%d,%d / %d,%d-%d,%d\n",
4565             rect.left, rect.top, rect.right, rect.bottom,
4566             expected_rect.left, expected_rect.top, expected_rect.right, expected_rect.bottom );
4567         return DefWindowProc(hwnd, msg, wp, lp);
4568     }
4569     case WM_NCCALCSIZE:
4570     {
4571         RECT rect, *r = (RECT *)lp;
4572         GetWindowRect( hwnd, &rect );
4573         ok( !memcmp( &rect, r, sizeof(rect) ),
4574             "passed rect %d,%d-%d,%d doesn't match window rect %d,%d-%d,%d\n",
4575             r->left, r->top, r->right, r->bottom, rect.left, rect.top, rect.right, rect.bottom );
4576         return DefWindowProc(hwnd, msg, wp, lp);
4577     }
4578     default:
4579         return DefWindowProc(hwnd, msg, wp, lp);
4580     }
4581 }
4582
4583 static void test_CreateWindow(void)
4584 {
4585     WNDCLASS cls;
4586     HWND hwnd, parent;
4587     HMENU hmenu;
4588     RECT rc, rc_minmax;
4589     MINMAXINFO minmax;
4590     BOOL res;
4591
4592 #define expect_menu(window, menu) \
4593     SetLastError(0xdeadbeef); \
4594     res = (GetMenu(window) == (HMENU)menu); \
4595     ok(res, "GetMenu error %d\n", GetLastError())
4596
4597 #define expect_style(window, style)\
4598     ok((ULONG)GetWindowLong(window, GWL_STYLE) == (style), "expected style %x != %x\n", (LONG)(style), GetWindowLong(window, GWL_STYLE))
4599
4600 #define expect_ex_style(window, ex_style)\
4601     ok((ULONG)GetWindowLong(window, GWL_EXSTYLE) == (ex_style), "expected ex_style %x != %x\n", (LONG)(ex_style), GetWindowLong(window, GWL_EXSTYLE))
4602
4603 #define expect_gle_broken_9x(gle)\
4604     ok(GetLastError() == gle ||\
4605        broken(GetLastError() == 0xdeadbeef),\
4606        "IsMenu set error %d\n", GetLastError())
4607
4608     hmenu = CreateMenu();
4609     assert(hmenu != 0);
4610     parent = GetDesktopWindow();
4611     assert(parent != 0);
4612
4613     SetLastError(0xdeadbeef);
4614     res = IsMenu(hmenu);
4615     ok(res, "IsMenu error %d\n", GetLastError());
4616
4617     /* WS_CHILD */
4618     SetLastError(0xdeadbeef);
4619     hwnd = CreateWindowEx(WS_EX_APPWINDOW, "static", NULL, WS_CHILD,
4620                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
4621     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
4622     expect_menu(hwnd, 1);
4623     expect_style(hwnd, WS_CHILD);
4624     expect_ex_style(hwnd, WS_EX_APPWINDOW);
4625     DestroyWindow(hwnd);
4626
4627     SetLastError(0xdeadbeef);
4628     hwnd = CreateWindowEx(WS_EX_APPWINDOW, "static", NULL, WS_CHILD | WS_CAPTION,
4629                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
4630     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
4631     expect_menu(hwnd, 1);
4632     expect_style(hwnd, WS_CHILD | WS_CAPTION);
4633     expect_ex_style(hwnd, WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
4634     DestroyWindow(hwnd);
4635
4636     SetLastError(0xdeadbeef);
4637     hwnd = CreateWindowEx(0, "static", NULL, WS_CHILD,
4638                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
4639     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
4640     expect_menu(hwnd, 1);
4641     expect_style(hwnd, WS_CHILD);
4642     expect_ex_style(hwnd, 0);
4643     DestroyWindow(hwnd);
4644
4645     SetLastError(0xdeadbeef);
4646     hwnd = CreateWindowEx(0, "static", NULL, WS_CHILD | WS_CAPTION,
4647                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
4648     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
4649     expect_menu(hwnd, 1);
4650     expect_style(hwnd, WS_CHILD | WS_CAPTION);
4651     expect_ex_style(hwnd, WS_EX_WINDOWEDGE);
4652     DestroyWindow(hwnd);
4653
4654     /* WS_POPUP */
4655     SetLastError(0xdeadbeef);
4656     hwnd = CreateWindowEx(WS_EX_APPWINDOW, "static", NULL, WS_POPUP,
4657                            0, 0, 100, 100, parent, hmenu, 0, NULL);
4658     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
4659     expect_menu(hwnd, hmenu);
4660     expect_style(hwnd, WS_POPUP | WS_CLIPSIBLINGS);
4661     expect_ex_style(hwnd, WS_EX_APPWINDOW);
4662     DestroyWindow(hwnd);
4663     SetLastError(0xdeadbeef);
4664     ok(!IsMenu(hmenu), "IsMenu should fail\n");
4665     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
4666
4667     hmenu = CreateMenu();
4668     assert(hmenu != 0);
4669     SetLastError(0xdeadbeef);
4670     hwnd = CreateWindowEx(WS_EX_APPWINDOW, "static", NULL, WS_POPUP | WS_CAPTION,
4671                            0, 0, 100, 100, parent, hmenu, 0, NULL);
4672     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
4673     expect_menu(hwnd, hmenu);
4674     expect_style(hwnd, WS_POPUP | WS_CAPTION | WS_CLIPSIBLINGS);
4675     expect_ex_style(hwnd, WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
4676     DestroyWindow(hwnd);
4677     SetLastError(0xdeadbeef);
4678     ok(!IsMenu(hmenu), "IsMenu should fail\n");
4679     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
4680
4681     hmenu = CreateMenu();
4682     assert(hmenu != 0);
4683     SetLastError(0xdeadbeef);
4684     hwnd = CreateWindowEx(0, "static", NULL, WS_POPUP,
4685                            0, 0, 100, 100, parent, hmenu, 0, NULL);
4686     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
4687     expect_menu(hwnd, hmenu);
4688     expect_style(hwnd, WS_POPUP | WS_CLIPSIBLINGS);
4689     expect_ex_style(hwnd, 0);
4690     DestroyWindow(hwnd);
4691     SetLastError(0xdeadbeef);
4692     ok(!IsMenu(hmenu), "IsMenu should fail\n");
4693     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
4694
4695     hmenu = CreateMenu();
4696     assert(hmenu != 0);
4697     SetLastError(0xdeadbeef);
4698     hwnd = CreateWindowEx(0, "static", NULL, WS_POPUP | WS_CAPTION,
4699                            0, 0, 100, 100, parent, hmenu, 0, NULL);
4700     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
4701     expect_menu(hwnd, hmenu);
4702     expect_style(hwnd, WS_POPUP | WS_CAPTION | WS_CLIPSIBLINGS);
4703     expect_ex_style(hwnd, WS_EX_WINDOWEDGE);
4704     DestroyWindow(hwnd);
4705     SetLastError(0xdeadbeef);
4706     ok(!IsMenu(hmenu), "IsMenu should fail\n");
4707     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
4708
4709     /* WS_CHILD | WS_POPUP */
4710     SetLastError(0xdeadbeef);
4711     hwnd = CreateWindowEx(WS_EX_APPWINDOW, "static", NULL, WS_CHILD | WS_POPUP,
4712                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
4713     ok(!hwnd || broken(hwnd != 0 /* Win9x */), "CreateWindowEx should fail\n");
4714     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
4715     if (hwnd)
4716         DestroyWindow(hwnd);
4717
4718     hmenu = CreateMenu();
4719     assert(hmenu != 0);
4720     SetLastError(0xdeadbeef);
4721     hwnd = CreateWindowEx(WS_EX_APPWINDOW, "static", NULL, WS_CHILD | WS_POPUP,
4722                            0, 0, 100, 100, parent, hmenu, 0, NULL);
4723     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
4724     expect_menu(hwnd, hmenu);
4725     expect_style(hwnd, WS_CHILD | WS_POPUP | WS_CLIPSIBLINGS);
4726     expect_ex_style(hwnd, WS_EX_APPWINDOW);
4727     DestroyWindow(hwnd);
4728     SetLastError(0xdeadbeef);
4729     ok(!IsMenu(hmenu), "IsMenu should fail\n");
4730     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
4731
4732     SetLastError(0xdeadbeef);
4733     hwnd = CreateWindowEx(WS_EX_APPWINDOW, "static", NULL, WS_CHILD | WS_POPUP | WS_CAPTION,
4734                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
4735     ok(!hwnd || broken(hwnd != 0 /* Win9x */), "CreateWindowEx should fail\n");
4736     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
4737     if (hwnd)
4738         DestroyWindow(hwnd);
4739
4740     hmenu = CreateMenu();
4741     assert(hmenu != 0);
4742     SetLastError(0xdeadbeef);
4743     hwnd = CreateWindowEx(WS_EX_APPWINDOW, "static", NULL, WS_CHILD | WS_POPUP | WS_CAPTION,
4744                            0, 0, 100, 100, parent, hmenu, 0, NULL);
4745     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
4746     expect_menu(hwnd, hmenu);
4747     expect_style(hwnd, WS_CHILD | WS_POPUP | WS_CAPTION | WS_CLIPSIBLINGS);
4748     expect_ex_style(hwnd, WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
4749     DestroyWindow(hwnd);
4750     SetLastError(0xdeadbeef);
4751     ok(!IsMenu(hmenu), "IsMenu should fail\n");
4752     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
4753
4754     SetLastError(0xdeadbeef);
4755     hwnd = CreateWindowEx(0, "static", NULL, WS_CHILD | WS_POPUP,
4756                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
4757     ok(!hwnd || broken(hwnd != 0 /* Win9x */), "CreateWindowEx should fail\n");
4758     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
4759     if (hwnd)
4760         DestroyWindow(hwnd);
4761
4762     hmenu = CreateMenu();
4763     assert(hmenu != 0);
4764     SetLastError(0xdeadbeef);
4765     hwnd = CreateWindowEx(0, "static", NULL, WS_CHILD | WS_POPUP,
4766                            0, 0, 100, 100, parent, hmenu, 0, NULL);
4767     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
4768     expect_menu(hwnd, hmenu);
4769     expect_style(hwnd, WS_CHILD | WS_POPUP | WS_CLIPSIBLINGS);
4770     expect_ex_style(hwnd, 0);
4771     DestroyWindow(hwnd);
4772     SetLastError(0xdeadbeef);
4773     ok(!IsMenu(hmenu), "IsMenu should fail\n");
4774     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
4775
4776     SetLastError(0xdeadbeef);
4777     hwnd = CreateWindowEx(0, "static", NULL, WS_CHILD | WS_POPUP | WS_CAPTION,
4778                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
4779     ok(!hwnd || broken(hwnd != 0 /* Win9x */), "CreateWindowEx should fail\n");
4780     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
4781     if (hwnd)
4782         DestroyWindow(hwnd);
4783
4784     hmenu = CreateMenu();
4785     assert(hmenu != 0);
4786     SetLastError(0xdeadbeef);
4787     hwnd = CreateWindowEx(0, "static", NULL, WS_CHILD | WS_POPUP | WS_CAPTION,
4788                            0, 0, 100, 100, parent, hmenu, 0, NULL);
4789     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
4790     expect_menu(hwnd, hmenu);
4791     expect_style(hwnd, WS_CHILD | WS_POPUP | WS_CAPTION | WS_CLIPSIBLINGS);
4792     expect_ex_style(hwnd, WS_EX_WINDOWEDGE);
4793     DestroyWindow(hwnd);
4794     SetLastError(0xdeadbeef);
4795     ok(!IsMenu(hmenu), "IsMenu should fail\n");
4796     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
4797
4798     /* test child window sizing */
4799     cls.style = 0;
4800     cls.lpfnWndProc = minmax_wnd_proc;
4801     cls.cbClsExtra = 0;
4802     cls.cbWndExtra = 0;
4803     cls.hInstance = GetModuleHandle(0);
4804     cls.hIcon = 0;
4805     cls.hCursor = LoadCursorA(0, IDC_ARROW);
4806     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
4807     cls.lpszMenuName = NULL;
4808     cls.lpszClassName = "MinMax_WndClass";
4809     RegisterClass(&cls);
4810
4811     SetLastError(0xdeadbeef);
4812     parent = CreateWindowEx(0, "MinMax_WndClass", NULL, WS_CAPTION | WS_SYSMENU | WS_THICKFRAME,
4813                            0, 0, 100, 100, 0, 0, 0, NULL);
4814     ok(parent != 0, "CreateWindowEx error %d\n", GetLastError());
4815     expect_menu(parent, 0);
4816     expect_style(parent, WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_CLIPSIBLINGS);
4817     expect_ex_style(parent, WS_EX_WINDOWEDGE);
4818
4819     memset(&minmax, 0, sizeof(minmax));
4820     SendMessage(parent, WM_GETMINMAXINFO, 0, (LPARAM)&minmax);
4821     SetRect(&rc_minmax, 0, 0, minmax.ptMaxSize.x, minmax.ptMaxSize.y);
4822     ok(IsRectEmpty(&rc_minmax), "ptMaxSize is not empty\n");
4823     SetRect(&rc_minmax, 0, 0, minmax.ptMaxTrackSize.x, minmax.ptMaxTrackSize.y);
4824     ok(IsRectEmpty(&rc_minmax), "ptMaxTrackSize is not empty\n");
4825
4826     GetWindowRect(parent, &rc);
4827     ok(!IsRectEmpty(&rc), "parent window rect is empty\n");
4828     GetClientRect(parent, &rc);
4829     ok(!IsRectEmpty(&rc), "parent client rect is empty\n");
4830
4831     InflateRect(&rc, 200, 200);
4832     trace("creating child with rect (%d,%d-%d,%d)\n", rc.left, rc.top, rc.right, rc.bottom);
4833
4834     SetLastError(0xdeadbeef);
4835     hwnd = CreateWindowEx(0, "MinMax_WndClass", NULL, WS_CHILD | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME,
4836                           rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
4837                           parent, (HMENU)1, 0, NULL);
4838     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
4839     expect_menu(hwnd, 1);
4840     expect_style(hwnd, WS_CHILD | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME);
4841     expect_ex_style(hwnd, WS_EX_WINDOWEDGE);
4842
4843     memset(&minmax, 0, sizeof(minmax));
4844     SendMessage(hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&minmax);
4845     SetRect(&rc_minmax, 0, 0, minmax.ptMaxTrackSize.x, minmax.ptMaxTrackSize.y);
4846
4847     GetWindowRect(hwnd, &rc);
4848     OffsetRect(&rc, -rc.left, -rc.top);
4849     ok(EqualRect(&rc, &rc_minmax), "rects don't match: (%d,%d-%d,%d) and (%d,%d-%d,%d)\n",
4850        rc.left, rc.top, rc.right, rc.bottom,
4851        rc_minmax.left, rc_minmax.top, rc_minmax.right, rc_minmax.bottom);
4852     DestroyWindow(hwnd);
4853
4854     cls.lpfnWndProc = winsizes_wnd_proc;
4855     cls.lpszClassName = "Sizes_WndClass";
4856     RegisterClass(&cls);
4857
4858     expected_cx = expected_cy = 200000;
4859     SetRect( &expected_rect, 0, 0, 200000, 200000 );
4860     broken_rect = expected_rect;
4861     hwnd = CreateWindowExA(0, "Sizes_WndClass", NULL, WS_CHILD, 300000, 300000, 200000, 200000, parent, 0, 0, NULL);
4862     ok( hwnd != 0, "creation failed err %u\n", GetLastError());
4863     GetClientRect( hwnd, &rc );
4864     ok( rc.right == 200000 || rc.right == 65535 || broken(rc.right == (short)200000),
4865         "invalid rect right %u\n", rc.right );
4866     ok( rc.bottom == 200000 || rc.bottom == 65535 || broken(rc.bottom == (short)200000),
4867         "invalid rect bottom %u\n", rc.bottom );
4868     DestroyWindow(hwnd);
4869
4870     expected_cx = expected_cy = -10;
4871     SetRect( &expected_rect, 0, 0, 0, 0 );
4872     SetRect( &broken_rect, 0, 0, -10, -10 );
4873     hwnd = CreateWindowExA(0, "Sizes_WndClass", NULL, WS_CHILD, -20, -20, -10, -10, parent, 0, 0, NULL);
4874     ok( hwnd != 0, "creation failed err %u\n", GetLastError());
4875     GetClientRect( hwnd, &rc );
4876     ok( rc.right == 0, "invalid rect right %u\n", rc.right );
4877     ok( rc.bottom == 0, "invalid rect bottom %u\n", rc.bottom );
4878     DestroyWindow(hwnd);
4879
4880     expected_cx = expected_cy = -200000;
4881     SetRect( &expected_rect, 0, 0, 0, 0 );
4882     SetRect( &broken_rect, 0, 0, -200000, -200000 );
4883     hwnd = CreateWindowExA(0, "Sizes_WndClass", NULL, WS_CHILD, -300000, -300000, -200000, -200000, parent, 0, 0, NULL);
4884     ok( hwnd != 0, "creation failed err %u\n", GetLastError());
4885     GetClientRect( hwnd, &rc );
4886     ok( rc.right == 0, "invalid rect right %u\n", rc.right );
4887     ok( rc.bottom == 0, "invalid rect bottom %u\n", rc.bottom );
4888     DestroyWindow(hwnd);
4889
4890     /* we need a parent at 0,0 so that child coordinates match */
4891     DestroyWindow(parent);
4892     parent = CreateWindowEx(0, "MinMax_WndClass", NULL, WS_POPUP, 0, 0, 100, 100, 0, 0, 0, NULL);
4893     ok(parent != 0, "CreateWindowEx error %d\n", GetLastError());
4894
4895     expected_cx = 100;
4896     expected_cy = 0x7fffffff;
4897     SetRect( &expected_rect, 10, 10, 110, 0x7fffffff );
4898     SetRect( &broken_rect, 10, 10, 110, 0x7fffffffU + 10 );
4899     hwnd = CreateWindowExA(0, "Sizes_WndClass", NULL, WS_CHILD, 10, 10, 100, 0x7fffffff, parent, 0, 0, NULL);
4900     ok( hwnd != 0, "creation failed err %u\n", GetLastError());
4901     GetClientRect( hwnd, &rc );
4902     ok( rc.right == 100, "invalid rect right %u\n", rc.right );
4903     ok( rc.bottom == 0x7fffffff - 10 || rc.bottom ==65535 || broken(rc.bottom == 0),
4904         "invalid rect bottom %u\n", rc.bottom );
4905     DestroyWindow(hwnd);
4906
4907     expected_cx = 0x7fffffff;
4908     expected_cy = 0x7fffffff;
4909     SetRect( &expected_rect, 20, 10, 0x7fffffff, 0x7fffffff );
4910     SetRect( &broken_rect, 20, 10, 0x7fffffffU + 20, 0x7fffffffU + 10 );
4911     hwnd = CreateWindowExA(0, "Sizes_WndClass", NULL, WS_CHILD, 20, 10, 0x7fffffff, 0x7fffffff, parent, 0, 0, NULL);
4912     ok( hwnd != 0, "creation failed err %u\n", GetLastError());
4913     GetClientRect( hwnd, &rc );
4914     ok( rc.right == 0x7fffffff - 20 || rc.right == 65535 || broken(rc.right == 0),
4915         "invalid rect right %u\n", rc.right );
4916     ok( rc.bottom == 0x7fffffff - 10 || rc.right == 65535 || broken(rc.bottom == 0),
4917         "invalid rect bottom %u\n", rc.bottom );
4918     DestroyWindow(hwnd);
4919
4920     /* top level window */
4921     expected_cx = expected_cy = 200000;
4922     SetRect( &expected_rect, 0, 0, GetSystemMetrics(SM_CXMAXTRACK), GetSystemMetrics(SM_CYMAXTRACK) );
4923     hwnd = CreateWindowExA(0, "Sizes_WndClass", NULL, WS_OVERLAPPEDWINDOW, 300000, 300000, 200000, 200000, 0, 0, 0, NULL);
4924     ok( hwnd != 0, "creation failed err %u\n", GetLastError());
4925     GetClientRect( hwnd, &rc );
4926     ok( rc.right <= expected_cx, "invalid rect right %u\n", rc.right );
4927     ok( rc.bottom <= expected_cy, "invalid rect bottom %u\n", rc.bottom );
4928     DestroyWindow(hwnd);
4929
4930     if (pGetLayout && pSetLayout)
4931     {
4932         HDC hdc = GetDC( parent );
4933         pSetLayout( hdc, LAYOUT_RTL );
4934         if (pGetLayout( hdc ))
4935         {
4936             ReleaseDC( parent, hdc );
4937             DestroyWindow( parent );
4938             SetLastError( 0xdeadbeef );
4939             parent = CreateWindowEx(WS_EX_APPWINDOW | WS_EX_LAYOUTRTL, "static", NULL, WS_POPUP,
4940                                     0, 0, 100, 100, 0, 0, 0, NULL);
4941             ok( parent != 0, "creation failed err %u\n", GetLastError());
4942             expect_ex_style( parent, WS_EX_APPWINDOW | WS_EX_LAYOUTRTL );
4943             hwnd = CreateWindowExA(0, "static", NULL, WS_CHILD, 0, 0, 20, 20, parent, 0, 0, NULL);
4944             ok( hwnd != 0, "creation failed err %u\n", GetLastError());
4945             expect_ex_style( hwnd, WS_EX_LAYOUTRTL );
4946             DestroyWindow( hwnd );
4947             hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0, 0, 20, 20, parent, 0, 0, NULL);
4948             ok( hwnd != 0, "creation failed err %u\n", GetLastError());
4949             expect_ex_style( hwnd, 0 );
4950             DestroyWindow( hwnd );
4951             SetWindowLongW( parent, GWL_EXSTYLE, WS_EX_APPWINDOW | WS_EX_LAYOUTRTL | WS_EX_NOINHERITLAYOUT );
4952             hwnd = CreateWindowExA(0, "static", NULL, WS_CHILD, 0, 0, 20, 20, parent, 0, 0, NULL);
4953             ok( hwnd != 0, "creation failed err %u\n", GetLastError());
4954             expect_ex_style( hwnd, 0 );
4955             DestroyWindow( hwnd );
4956
4957             if (pGetProcessDefaultLayout && pSetProcessDefaultLayout)
4958             {
4959                 DWORD layout;
4960
4961                 SetLastError( 0xdeadbeef );
4962                 ok( !pGetProcessDefaultLayout( NULL ), "GetProcessDefaultLayout succeeded\n" );
4963                 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
4964                 SetLastError( 0xdeadbeef );
4965                 ok( pGetProcessDefaultLayout( &layout ),
4966                     "GetProcessDefaultLayout failed err %u\n", GetLastError ());
4967                 ok( layout == 0, "GetProcessDefaultLayout wrong layout %x\n", layout );
4968                 SetLastError( 0xdeadbeef );
4969                 ok( pSetProcessDefaultLayout( 7 ),
4970                     "SetProcessDefaultLayout failed err %u\n", GetLastError ());
4971                 ok( pGetProcessDefaultLayout( &layout ),
4972                     "GetProcessDefaultLayout failed err %u\n", GetLastError ());
4973                 ok( layout == 7, "GetProcessDefaultLayout wrong layout %x\n", layout );
4974                 SetLastError( 0xdeadbeef );
4975                 ok( pSetProcessDefaultLayout( LAYOUT_RTL ),
4976                     "SetProcessDefaultLayout failed err %u\n", GetLastError ());
4977                 ok( pGetProcessDefaultLayout( &layout ),
4978                     "GetProcessDefaultLayout failed err %u\n", GetLastError ());
4979                 ok( layout == LAYOUT_RTL, "GetProcessDefaultLayout wrong layout %x\n", layout );
4980                 hwnd = CreateWindowEx(WS_EX_APPWINDOW, "static", NULL, WS_POPUP,
4981                                       0, 0, 100, 100, 0, 0, 0, NULL);
4982                 ok( hwnd != 0, "creation failed err %u\n", GetLastError());
4983                 expect_ex_style( hwnd, WS_EX_APPWINDOW | WS_EX_LAYOUTRTL );
4984                 DestroyWindow( hwnd );
4985                 hwnd = CreateWindowEx(WS_EX_APPWINDOW, "static", NULL, WS_POPUP,
4986                                       0, 0, 100, 100, parent, 0, 0, NULL);
4987                 ok( hwnd != 0, "creation failed err %u\n", GetLastError());
4988                 expect_ex_style( hwnd, WS_EX_APPWINDOW );
4989                 DestroyWindow( hwnd );
4990                 pSetProcessDefaultLayout( 0 );
4991             }
4992             else win_skip( "SetProcessDefaultLayout not supported\n" );
4993         }
4994         else win_skip( "SetLayout not supported\n" );
4995     }
4996     else win_skip( "SetLayout not available\n" );
4997
4998     DestroyWindow(parent);
4999
5000     UnregisterClass("MinMax_WndClass", GetModuleHandle(0));
5001     UnregisterClass("Sizes_WndClass", GetModuleHandle(0));
5002
5003 #undef expect_gle_broken_9x
5004 #undef expect_menu
5005 #undef expect_style
5006 #undef expect_ex_style
5007 }
5008
5009 /* function that remembers whether the system the test is running on sets the
5010  * last error for user32 functions to make the tests stricter */
5011 static int check_error(DWORD actual, DWORD expected)
5012 {
5013     static int sets_last_error = -1;
5014     if (sets_last_error == -1)
5015         sets_last_error = (actual != 0xdeadbeef);
5016     return (!sets_last_error && (actual == 0xdeadbeef)) || (actual == expected);
5017 }
5018
5019 static void test_SetWindowLong(void)
5020 {
5021     LONG_PTR retval;
5022     WNDPROC old_window_procW;
5023
5024     SetLastError(0xdeadbeef);
5025     retval = SetWindowLongPtr(NULL, GWLP_WNDPROC, 0);
5026     ok(!retval, "SetWindowLongPtr on invalid window handle should have returned 0 instead of 0x%lx\n", retval);
5027     ok(check_error(GetLastError(), ERROR_INVALID_WINDOW_HANDLE),
5028         "SetWindowLongPtr should have set error to ERROR_INVALID_WINDOW_HANDLE instad of %d\n", GetLastError());
5029
5030     SetLastError(0xdeadbeef);
5031     retval = SetWindowLongPtr(hwndMain, 0xdeadbeef, 0);
5032     ok(!retval, "SetWindowLongPtr on invalid index should have returned 0 instead of 0x%lx\n", retval);
5033     ok(check_error(GetLastError(), ERROR_INVALID_INDEX),
5034         "SetWindowLongPtr should have set error to ERROR_INVALID_INDEX instad of %d\n", GetLastError());
5035
5036     SetLastError(0xdeadbeef);
5037     retval = SetWindowLongPtr(hwndMain, GWLP_WNDPROC, 0);
5038     ok((WNDPROC)retval == main_window_procA || broken(!retval), /* win9x */
5039         "SetWindowLongPtr on invalid window proc should have returned address of main_window_procA instead of 0x%lx\n", retval);
5040     ok(GetLastError() == 0xdeadbeef, "SetWindowLongPtr shouldn't have set the last error, instead of setting it to %d\n", GetLastError());
5041     retval = GetWindowLongPtr(hwndMain, GWLP_WNDPROC);
5042     ok((WNDPROC)retval == main_window_procA,
5043         "SetWindowLongPtr on invalid window proc shouldn't have changed the value returned by GetWindowLongPtr, instead of changing it to 0x%lx\n", retval);
5044     ok(!IsWindowUnicode(hwndMain), "hwndMain shouldn't be Unicode\n");
5045
5046     old_window_procW = (WNDPROC)GetWindowLongPtrW(hwndMain, GWLP_WNDPROC);
5047     SetLastError(0xdeadbeef);
5048     retval = SetWindowLongPtrW(hwndMain, GWLP_WNDPROC, 0);
5049     if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
5050     {
5051         ok(GetLastError() == 0xdeadbeef, "SetWindowLongPtr shouldn't have set the last error, instead of setting it to %d\n", GetLastError());
5052         ok(retval != 0, "SetWindowLongPtr error %d\n", GetLastError());
5053         ok((WNDPROC)retval == old_window_procW,
5054             "SetWindowLongPtr on invalid window proc shouldn't have changed the value returned by GetWindowLongPtr, instead of changing it to 0x%lx\n", retval);
5055         ok(IsWindowUnicode(hwndMain), "hwndMain should now be Unicode\n");
5056
5057         /* set it back to ANSI */
5058         SetWindowLongPtr(hwndMain, GWLP_WNDPROC, 0);
5059     }
5060 }
5061
5062 static void test_ShowWindow(void)
5063 {
5064     HWND hwnd;
5065     DWORD style;
5066     RECT rcMain, rc, rcMinimized;
5067     LPARAM ret;
5068
5069     SetRect(&rcMain, 120, 120, 210, 210);
5070
5071     hwnd = CreateWindowEx(0, "MainWindowClass", NULL,
5072                           WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
5073                           WS_MAXIMIZEBOX | WS_POPUP,
5074                           rcMain.left, rcMain.top,
5075                           rcMain.right - rcMain.left, rcMain.bottom - rcMain.top,
5076                           0, 0, 0, NULL);
5077     assert(hwnd);
5078
5079     style = GetWindowLong(hwnd, GWL_STYLE);
5080     ok(!(style & WS_DISABLED), "window should not be disabled\n");
5081     ok(!(style & WS_VISIBLE), "window should not be visible\n");
5082     ok(!(style & WS_MINIMIZE), "window should not be minimized\n");
5083     ok(!(style & WS_MAXIMIZE), "window should not be maximized\n");
5084     GetWindowRect(hwnd, &rc);
5085     ok(EqualRect(&rcMain, &rc), "expected (%d,%d)-(%d,%d), got (%d,%d)-(%d,%d)\n",
5086        rcMain.left, rcMain.top, rcMain.right, rcMain.bottom,
5087        rc.left, rc.top, rc.right, rc.bottom);
5088
5089     ret = ShowWindow(hwnd, SW_SHOW);
5090     ok(!ret, "not expected ret: %lu\n", ret);
5091     style = GetWindowLong(hwnd, GWL_STYLE);
5092     ok(!(style & WS_DISABLED), "window should not be disabled\n");
5093     ok(style & WS_VISIBLE, "window should be visible\n");
5094     ok(!(style & WS_MINIMIZE), "window should not be minimized\n");
5095     ok(!(style & WS_MAXIMIZE), "window should not be maximized\n");
5096     GetWindowRect(hwnd, &rc);
5097     ok(EqualRect(&rcMain, &rc), "expected (%d,%d)-(%d,%d), got (%d,%d)-(%d,%d)\n",
5098        rcMain.left, rcMain.top, rcMain.right, rcMain.bottom,
5099        rc.left, rc.top, rc.right, rc.bottom);
5100
5101     ret = ShowWindow(hwnd, SW_MINIMIZE);
5102     ok(ret, "not expected ret: %lu\n", ret);
5103     style = GetWindowLong(hwnd, GWL_STYLE);
5104     ok(!(style & WS_DISABLED), "window should not be disabled\n");
5105     ok(style & WS_VISIBLE, "window should be visible\n");
5106     ok(style & WS_MINIMIZE, "window should be minimized\n");
5107     ok(!(style & WS_MAXIMIZE), "window should not be maximized\n");
5108     GetWindowRect(hwnd, &rcMinimized);
5109     ok(!EqualRect(&rcMain, &rcMinimized), "rects shouldn't match\n");
5110     /* shouldn't be able to resize minized windows */
5111     ret = SetWindowPos(hwnd, 0, 0, 0,
5112                        (rcMinimized.right - rcMinimized.left) * 2,
5113                        (rcMinimized.bottom - rcMinimized.top) * 2,
5114                        SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);
5115     ok(ret, "not expected ret: %lu\n", ret);
5116     GetWindowRect(hwnd, &rc);
5117     ok(EqualRect(&rc, &rcMinimized), "rects should match\n");
5118
5119     ShowWindow(hwnd, SW_RESTORE);
5120     ok(ret, "not expected ret: %lu\n", ret);
5121     style = GetWindowLong(hwnd, GWL_STYLE);
5122     ok(!(style & WS_DISABLED), "window should not be disabled\n");
5123     ok(style & WS_VISIBLE, "window should be visible\n");
5124     ok(!(style & WS_MINIMIZE), "window should not be minimized\n");
5125     ok(!(style & WS_MAXIMIZE), "window should not be maximized\n");
5126     GetWindowRect(hwnd, &rc);
5127     ok(EqualRect(&rcMain, &rc), "expected (%d,%d)-(%d,%d), got (%d,%d)-(%d,%d)\n",
5128        rcMain.left, rcMain.top, rcMain.right, rcMain.bottom,
5129        rc.left, rc.top, rc.right, rc.bottom);
5130
5131     ret = EnableWindow(hwnd, FALSE);
5132     ok(!ret, "not expected ret: %lu\n", ret);
5133     style = GetWindowLong(hwnd, GWL_STYLE);
5134     ok(style & WS_DISABLED, "window should be disabled\n");
5135
5136     ret = DefWindowProc(hwnd, WM_SYSCOMMAND, SC_MINIMIZE, 0);
5137     ok(!ret, "not expected ret: %lu\n", ret);
5138     style = GetWindowLong(hwnd, GWL_STYLE);
5139     ok(style & WS_DISABLED, "window should be disabled\n");
5140     ok(style & WS_VISIBLE, "window should be visible\n");
5141     ok(!(style & WS_MINIMIZE), "window should not be minimized\n");
5142     ok(!(style & WS_MAXIMIZE), "window should not be maximized\n");
5143     GetWindowRect(hwnd, &rc);
5144     ok(EqualRect(&rcMain, &rc), "expected (%d,%d)-(%d,%d), got (%d,%d)-(%d,%d)\n",
5145        rcMain.left, rcMain.top, rcMain.right, rcMain.bottom,
5146        rc.left, rc.top, rc.right, rc.bottom);
5147
5148     ret = DefWindowProc(hwnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
5149     ok(!ret, "not expected ret: %lu\n", ret);
5150     style = GetWindowLong(hwnd, GWL_STYLE);
5151     ok(style & WS_DISABLED, "window should be disabled\n");
5152     ok(style & WS_VISIBLE, "window should be visible\n");
5153     ok(!(style & WS_MINIMIZE), "window should not be minimized\n");
5154     ok(!(style & WS_MAXIMIZE), "window should not be maximized\n");
5155     GetWindowRect(hwnd, &rc);
5156     ok(EqualRect(&rcMain, &rc), "expected (%d,%d)-(%d,%d), got (%d,%d)-(%d,%d)\n",
5157        rcMain.left, rcMain.top, rcMain.right, rcMain.bottom,
5158        rc.left, rc.top, rc.right, rc.bottom);
5159
5160     ret = ShowWindow(hwnd, SW_MINIMIZE);
5161     ok(ret, "not expected ret: %lu\n", ret);
5162     style = GetWindowLong(hwnd, GWL_STYLE);
5163     ok(style & WS_DISABLED, "window should be disabled\n");
5164     ok(style & WS_VISIBLE, "window should be visible\n");
5165     ok(style & WS_MINIMIZE, "window should be minimized\n");
5166     ok(!(style & WS_MAXIMIZE), "window should not be maximized\n");
5167     GetWindowRect(hwnd, &rc);
5168     ok(!EqualRect(&rcMain, &rc), "rects shouldn't match\n");
5169
5170     ret = DefWindowProc(hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
5171     ok(!ret, "not expected ret: %lu\n", ret);
5172     style = GetWindowLong(hwnd, GWL_STYLE);
5173     ok(style & WS_DISABLED, "window should be disabled\n");
5174     ok(style & WS_VISIBLE, "window should be visible\n");
5175     ok(style & WS_MINIMIZE, "window should be minimized\n");
5176     ok(!(style & WS_MAXIMIZE), "window should not be maximized\n");
5177     GetWindowRect(hwnd, &rc);
5178     ok(!EqualRect(&rcMain, &rc), "rects shouldn't match\n");
5179
5180     ret = ShowWindow(hwnd, SW_RESTORE);
5181     ok(ret, "not expected ret: %lu\n", ret);
5182     style = GetWindowLong(hwnd, GWL_STYLE);
5183     ok(style & WS_DISABLED, "window should be disabled\n");
5184     ok(style & WS_VISIBLE, "window should be visible\n");
5185     ok(!(style & WS_MINIMIZE), "window should not be minimized\n");
5186     ok(!(style & WS_MAXIMIZE), "window should not be maximized\n");
5187     GetWindowRect(hwnd, &rc);
5188     ok(EqualRect(&rcMain, &rc), "expected (%d,%d)-(%d,%d), got (%d,%d)-(%d,%d)\n",
5189        rcMain.left, rcMain.top, rcMain.right, rcMain.bottom,
5190        rc.left, rc.top, rc.right, rc.bottom);
5191
5192     ret = DefWindowProc(hwnd, WM_SYSCOMMAND, SC_CLOSE, 0);
5193     ok(!ret, "not expected ret: %lu\n", ret);
5194     ok(IsWindow(hwnd), "window should exist\n");
5195
5196     ret = EnableWindow(hwnd, TRUE);
5197     ok(ret, "not expected ret: %lu\n", ret);
5198
5199     ret = DefWindowProc(hwnd, WM_SYSCOMMAND, SC_CLOSE, 0);
5200     ok(!ret, "not expected ret: %lu\n", ret);
5201     ok(!IsWindow(hwnd), "window should not exist\n");
5202 }
5203
5204 static void test_gettext(void)
5205 {
5206     WNDCLASS cls;
5207     LPCSTR clsname = "gettexttest";
5208     HWND hwnd;
5209     LRESULT r;
5210
5211     memset( &cls, 0, sizeof cls );
5212     cls.lpfnWndProc = DefWindowProc;
5213     cls.lpszClassName = clsname;
5214     cls.hInstance = GetModuleHandle(NULL);
5215
5216     if (!RegisterClass( &cls )) return;
5217
5218     hwnd = CreateWindow( clsname, "test text", WS_OVERLAPPED, 0, 0, 10, 10, 0, NULL, NULL, NULL);
5219     ok( hwnd != NULL, "window was null\n");
5220
5221     r = SendMessage( hwnd, WM_GETTEXT, 0x10, 0x1000);
5222     ok( r == 0, "settext should return zero\n");
5223
5224     r = SendMessage( hwnd, WM_GETTEXT, 0x10000, 0);
5225     ok( r == 0, "settext should return zero (%ld)\n", r);
5226
5227     r = SendMessage( hwnd, WM_GETTEXT, 0xff000000, 0x1000);
5228     ok( r == 0, "settext should return zero (%ld)\n", r);
5229
5230     r = SendMessage( hwnd, WM_GETTEXT, 0x1000, 0xff000000);
5231     ok( r == 0, "settext should return zero (%ld)\n", r);
5232
5233     DestroyWindow(hwnd);
5234     UnregisterClass( clsname, NULL );
5235 }
5236
5237
5238 static void test_GetUpdateRect(void)
5239 {
5240     MSG msg;
5241     BOOL ret, parent_wm_paint, grandparent_wm_paint;
5242     RECT rc1, rc2;
5243     HWND hgrandparent, hparent, hchild;
5244     WNDCLASSA cls;
5245     static const char classNameA[] = "GetUpdateRectClass";
5246
5247     hgrandparent = CreateWindowA("static", "grandparent", WS_OVERLAPPEDWINDOW,
5248                                  0, 0, 100, 100, NULL, NULL, 0, NULL);
5249
5250     hparent = CreateWindowA("static", "parent", WS_CHILD|WS_VISIBLE,
5251                             0, 0, 100, 100, hgrandparent, NULL, 0, NULL);
5252
5253     hchild = CreateWindowA("static", "child", WS_CHILD|WS_VISIBLE,
5254                             10, 10, 30, 30, hparent, NULL, 0, NULL);
5255
5256     ShowWindow(hgrandparent, SW_SHOW);
5257     UpdateWindow(hgrandparent);
5258     flush_events( TRUE );
5259
5260     ShowWindow(hchild, SW_HIDE);
5261     SetRect(&rc2, 0, 0, 0, 0);
5262     ret = GetUpdateRect(hgrandparent, &rc1, FALSE);
5263     ok(!ret, "GetUpdateRect returned not empty region\n");
5264     ok(EqualRect(&rc1, &rc2), "rects do not match (%d,%d,%d,%d) / (%d,%d,%d,%d)\n",
5265        rc1.left, rc1.top, rc1.right, rc1.bottom,
5266        rc2.left, rc2.top, rc2.right, rc2.bottom);
5267
5268     SetRect(&rc2, 10, 10, 40, 40);
5269     ret = GetUpdateRect(hparent, &rc1, FALSE);
5270     ok(ret, "GetUpdateRect returned empty region\n");
5271     ok(EqualRect(&rc1, &rc2), "rects do not match (%d,%d,%d,%d) / (%d,%d,%d,%d)\n",
5272             rc1.left, rc1.top, rc1.right, rc1.bottom,
5273             rc2.left, rc2.top, rc2.right, rc2.bottom);
5274
5275     parent_wm_paint = FALSE;
5276     grandparent_wm_paint = FALSE;
5277     while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
5278     {
5279         if (msg.message == WM_PAINT)
5280         {
5281             if (msg.hwnd == hgrandparent) grandparent_wm_paint = TRUE;
5282             if (msg.hwnd == hparent) parent_wm_paint = TRUE;
5283         }
5284         DispatchMessage(&msg);
5285     }
5286     ok(parent_wm_paint, "WM_PAINT should have been received in parent\n");
5287     ok(!grandparent_wm_paint, "WM_PAINT should NOT have been received in grandparent\n");
5288
5289     DestroyWindow(hgrandparent);
5290
5291     cls.style = 0;
5292     cls.lpfnWndProc = DefWindowProcA;
5293     cls.cbClsExtra = 0;
5294     cls.cbWndExtra = 0;
5295     cls.hInstance = GetModuleHandleA(0);
5296     cls.hIcon = 0;
5297     cls.hCursor = LoadCursorA(0, IDC_ARROW);
5298     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
5299     cls.lpszMenuName = NULL;
5300     cls.lpszClassName = classNameA;
5301
5302     if(!RegisterClassA(&cls)) {
5303        trace("Register failed %d\n", GetLastError());
5304        return;
5305     }
5306
5307     hgrandparent = CreateWindowA(classNameA, "grandparent", WS_OVERLAPPEDWINDOW,
5308                                  0, 0, 100, 100, NULL, NULL, 0, NULL);
5309
5310     hparent = CreateWindowA(classNameA, "parent", WS_CHILD|WS_VISIBLE,
5311                             0, 0, 100, 100, hgrandparent, NULL, 0, NULL);
5312
5313     hchild = CreateWindowA(classNameA, "child", WS_CHILD|WS_VISIBLE,
5314                             10, 10, 30, 30, hparent, NULL, 0, NULL);
5315
5316     ShowWindow(hgrandparent, SW_SHOW);
5317     UpdateWindow(hgrandparent);
5318     flush_events( TRUE );
5319
5320     ret = GetUpdateRect(hgrandparent, &rc1, FALSE);
5321     ok(!ret, "GetUpdateRect returned not empty region\n");
5322
5323     ShowWindow(hchild, SW_HIDE);
5324
5325     SetRect(&rc2, 0, 0, 0, 0);
5326     ret = GetUpdateRect(hgrandparent, &rc1, FALSE);
5327     ok(!ret, "GetUpdateRect returned not empty region\n");
5328     ok(EqualRect(&rc1, &rc2), "rects do not match (%d,%d,%d,%d) / (%d,%d,%d,%d)\n",
5329        rc1.left, rc1.top, rc1.right, rc1.bottom,
5330        rc2.left, rc2.top, rc2.right, rc2.bottom);
5331
5332     SetRect(&rc2, 10, 10, 40, 40);
5333     ret = GetUpdateRect(hparent, &rc1, FALSE);
5334     ok(ret, "GetUpdateRect returned empty region\n");
5335     ok(EqualRect(&rc1, &rc2), "rects do not match (%d,%d,%d,%d) / (%d,%d,%d,%d)\n",
5336             rc1.left, rc1.top, rc1.right, rc1.bottom,
5337             rc2.left, rc2.top, rc2.right, rc2.bottom);
5338
5339     parent_wm_paint = FALSE;
5340     grandparent_wm_paint = FALSE;
5341     while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
5342     {
5343         if (msg.message == WM_PAINT)
5344         {
5345             if (msg.hwnd == hgrandparent) grandparent_wm_paint = TRUE;
5346             if (msg.hwnd == hparent) parent_wm_paint = TRUE;
5347         }
5348         DispatchMessage(&msg);
5349     }
5350     ok(parent_wm_paint, "WM_PAINT should have been received in parent\n");
5351     ok(!grandparent_wm_paint, "WM_PAINT should NOT have been received in grandparent\n");
5352
5353     DestroyWindow(hgrandparent);
5354 }
5355
5356
5357 static LRESULT CALLBACK TestExposedRegion_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
5358 {
5359     if(msg == WM_PAINT)
5360     {
5361         PAINTSTRUCT ps;
5362         RECT updateRect;
5363         DWORD waitResult;
5364         HWND win;
5365         const int waitTime = 2000;
5366
5367         BeginPaint(hwnd, &ps);
5368
5369         /* create and destroy window to create an exposed region on this window */
5370         win = CreateWindowA("static", "win", WS_VISIBLE,
5371                              10,10,50,50, NULL, NULL, 0, NULL);
5372         DestroyWindow(win);
5373
5374         waitResult = MsgWaitForMultipleObjects( 0, NULL, FALSE, waitTime, QS_PAINT );
5375
5376         ValidateRect(hwnd, NULL);
5377         EndPaint(hwnd, &ps);
5378
5379         if(waitResult != WAIT_TIMEOUT)
5380         {
5381             GetUpdateRect(hwnd, &updateRect, FALSE);
5382             ok(IsRectEmpty(&updateRect), "Exposed rect should be empty\n");
5383         }
5384
5385         return 1;
5386     }
5387     return DefWindowProc(hwnd, msg, wParam, lParam);
5388 }
5389
5390 static void test_Expose(void)
5391 {
5392     WNDCLASSA cls;
5393     HWND mw;
5394
5395     memset(&cls, 0, sizeof(WNDCLASSA));
5396     cls.lpfnWndProc = TestExposedRegion_WndProc;
5397     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
5398     cls.lpszClassName = "TestExposeClass";
5399     RegisterClassA(&cls);
5400
5401     mw = CreateWindowA("TestExposeClass", "MainWindow", WS_VISIBLE|WS_OVERLAPPEDWINDOW,
5402                             0, 0, 200, 100, NULL, NULL, 0, NULL);
5403
5404     UpdateWindow(mw);
5405     DestroyWindow(mw);
5406 }
5407
5408 static LRESULT CALLBACK TestNCRedraw_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
5409 {
5410     static UINT ncredrawflags;
5411     PAINTSTRUCT ps;
5412
5413     switch(msg)
5414     {
5415     case WM_CREATE:
5416         ncredrawflags = *(UINT *) (((CREATESTRUCT *)lParam)->lpCreateParams);
5417         return 0;
5418     case WM_NCPAINT:
5419         RedrawWindow(hwnd, NULL, NULL, ncredrawflags);
5420         break;
5421     case WM_PAINT:
5422         BeginPaint(hwnd, &ps);
5423         EndPaint(hwnd, &ps);
5424         return 0;
5425     }
5426     return DefWindowProc(hwnd, msg, wParam, lParam);
5427 }
5428
5429 static void run_NCRedrawLoop(UINT flags)
5430 {
5431     HWND hwnd;
5432     MSG msg;
5433
5434     UINT loopcount = 0;
5435
5436     hwnd = CreateWindowA("TestNCRedrawClass", "MainWindow",
5437                          WS_OVERLAPPEDWINDOW, 0, 0, 200, 100,
5438                          NULL, NULL, 0, &flags);
5439     ShowWindow(hwnd, SW_SHOW);
5440     UpdateWindow(hwnd);
5441     flush_events( FALSE );
5442     while(PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE) != 0)
5443     {
5444         if (msg.message == WM_PAINT) loopcount++;
5445         if (loopcount >= 100) break;
5446         TranslateMessage(&msg);
5447         DispatchMessage(&msg);
5448         MsgWaitForMultipleObjects(0, NULL, FALSE, 100, QS_ALLINPUT);
5449     }
5450     if (flags == (RDW_INVALIDATE | RDW_FRAME))
5451         todo_wine ok(loopcount < 100, "Detected infinite WM_PAINT loop (%x).\n", flags);
5452     else
5453         ok(loopcount < 100, "Detected infinite WM_PAINT loop (%x).\n", flags);
5454     DestroyWindow(hwnd);
5455 }
5456
5457 static void test_NCRedraw(void)
5458 {
5459     WNDCLASSA wndclass;
5460
5461     wndclass.lpszClassName = "TestNCRedrawClass";
5462     wndclass.style = CS_HREDRAW | CS_VREDRAW;
5463     wndclass.lpfnWndProc = TestNCRedraw_WndProc;
5464     wndclass.cbClsExtra = 0;
5465     wndclass.cbWndExtra = 0;
5466     wndclass.hInstance = 0;
5467     wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
5468     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5469     wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
5470     wndclass.lpszMenuName = NULL;
5471
5472     RegisterClassA(&wndclass);
5473
5474     run_NCRedrawLoop(RDW_INVALIDATE | RDW_FRAME);
5475     run_NCRedrawLoop(RDW_INVALIDATE);
5476 }
5477
5478 static void test_GetWindowModuleFileName(void)
5479 {
5480     HWND hwnd;
5481     HINSTANCE hinst;
5482     UINT ret1, ret2;
5483     char buf1[MAX_PATH], buf2[MAX_PATH];
5484
5485     if (!pGetWindowModuleFileNameA)
5486     {
5487         win_skip("GetWindowModuleFileNameA is not available\n");
5488         return;
5489     }
5490
5491     hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0,0,0,0, 0, 0, 0, NULL);
5492     assert(hwnd);
5493
5494     hinst = (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE);
5495     ok(hinst == 0 || broken(hinst == GetModuleHandle(0)), /* win9x */ "expected 0, got %p\n", hinst);
5496
5497     buf1[0] = 0;
5498     SetLastError(0xdeadbeef);
5499     ret1 = GetModuleFileName(hinst, buf1, sizeof(buf1));
5500     ok(ret1, "GetModuleFileName error %u\n", GetLastError());
5501
5502     buf2[0] = 0;
5503     SetLastError(0xdeadbeef);
5504     ret2 = pGetWindowModuleFileNameA(hwnd, buf2, sizeof(buf2));
5505     ok(ret2 || broken(!ret2), /* nt4 sp 3 */
5506        "GetWindowModuleFileNameA error %u\n", GetLastError());
5507
5508     if (ret2)
5509     {
5510         ok(ret1 == ret2 || broken(ret2 == ret1 + 1), /* win98 */ "%u != %u\n", ret1, ret2);
5511         ok(!strcmp(buf1, buf2), "%s != %s\n", buf1, buf2);
5512     }
5513     hinst = GetModuleHandle(0);
5514
5515     SetLastError(0xdeadbeef);
5516     ret2 = GetModuleFileName(hinst, buf2, ret1 - 2);
5517     ok(ret2 == ret1 - 2 || broken(ret2 == ret1 - 3), /* win98 */
5518        "expected %u, got %u\n", ret1 - 2, ret2);
5519     ok(GetLastError() == 0xdeadbeef /* XP */ ||
5520        GetLastError() == ERROR_INSUFFICIENT_BUFFER, /* win2k3, vista */
5521        "expected 0xdeadbeef or ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
5522
5523     SetLastError(0xdeadbeef);
5524     ret2 = GetModuleFileName(hinst, buf2, 0);
5525     ok(!ret2, "GetModuleFileName should return 0\n");
5526     ok(GetLastError() == 0xdeadbeef /* XP */ ||
5527        GetLastError() == ERROR_INSUFFICIENT_BUFFER, /* win2k3, vista */
5528        "expected 0xdeadbeef or ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
5529
5530     SetLastError(0xdeadbeef);
5531     ret2 = pGetWindowModuleFileNameA(hwnd, buf2, ret1 - 2);
5532     ok(ret2 == ret1 - 2 || broken(ret2 == ret1 - 3) /* win98 */ || broken(!ret2), /* nt4 sp3 */
5533        "expected %u, got %u\n", ret1 - 2, ret2);
5534     ok(GetLastError() == 0xdeadbeef /* XP */ ||
5535        GetLastError() == ERROR_INSUFFICIENT_BUFFER, /* win2k3, vista */
5536        "expected 0xdeadbeef or ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
5537
5538     SetLastError(0xdeadbeef);
5539     ret2 = pGetWindowModuleFileNameA(hwnd, buf2, 0);
5540     ok(!ret2, "expected 0, got %u\n", ret2);
5541     ok(GetLastError() == 0xdeadbeef /* XP */ ||
5542        GetLastError() == ERROR_INSUFFICIENT_BUFFER, /* win2k3, vista */
5543        "expected 0xdeadbeef or ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
5544
5545     DestroyWindow(hwnd);
5546
5547     buf2[0] = 0;
5548     hwnd = (HWND)0xdeadbeef;
5549     SetLastError(0xdeadbeef);
5550     ret1 = pGetWindowModuleFileNameA(hwnd, buf1, sizeof(buf1));
5551     ok(!ret1, "expected 0, got %u\n", ret1);
5552     ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE || broken(GetLastError() == 0xdeadbeef), /* win9x */
5553        "expected ERROR_INVALID_WINDOW_HANDLE, got %u\n", GetLastError());
5554
5555     hwnd = FindWindow("Shell_TrayWnd", NULL);
5556     ok(IsWindow(hwnd) || broken(!hwnd), "got invalid tray window %p\n", hwnd);
5557     SetLastError(0xdeadbeef);
5558     ret1 = pGetWindowModuleFileNameA(hwnd, buf1, sizeof(buf1));
5559     ok(!ret1 || broken(ret1), /* win98 */ "expected 0, got %u\n", ret1);
5560
5561     if (!ret1)  /* inter-process GetWindowModuleFileName works on win9x, so don't test the desktop there */
5562     {
5563         ret1 = GetModuleFileName(0, buf1, sizeof(buf1));
5564         hwnd = GetDesktopWindow();
5565         ok(IsWindow(hwnd), "got invalid desktop window %p\n", hwnd);
5566         SetLastError(0xdeadbeef);
5567         ret2 = pGetWindowModuleFileNameA(hwnd, buf2, sizeof(buf2));
5568         ok(!ret2 ||
5569            ret1 == ret2 || /* vista */
5570            broken(ret2),  /* some win98 return user.exe as file name */
5571            "expected 0 or %u, got %u %s\n", ret1, ret2, buf2);
5572     }
5573 }
5574
5575 static void test_hwnd_message(void)
5576 {
5577     static const WCHAR mainwindowclassW[] = {'M','a','i','n','W','i','n','d','o','w','C','l','a','s','s',0};
5578     static const WCHAR message_windowW[] = {'m','e','s','s','a','g','e',' ','w','i','n','d','o','w',0};
5579
5580     HWND parent = 0, hwnd, found;
5581     RECT rect;
5582
5583     /* HWND_MESSAGE is not supported below w2k, but win9x return != 0
5584        on CreateWindowExA and crash later in the test.
5585        Use UNICODE here to fail on win9x */
5586     hwnd = CreateWindowExW(0, mainwindowclassW, message_windowW, WS_CAPTION | WS_VISIBLE,
5587                            100, 100, 200, 200, HWND_MESSAGE, 0, 0, NULL);
5588     if (!hwnd)
5589     {
5590         win_skip("CreateWindowExW with parent HWND_MESSAGE failed\n");
5591         return;
5592     }
5593
5594     ok( !GetParent(hwnd), "GetParent should return 0 for message only windows\n" );
5595     if (pGetAncestor)
5596     {
5597         char buffer[100];
5598         HWND root, desktop = GetDesktopWindow();
5599
5600         parent = pGetAncestor(hwnd, GA_PARENT);
5601         ok(parent != 0, "GetAncestor(GA_PARENT) should not return 0 for message windows\n");
5602         ok(parent != desktop, "GetAncestor(GA_PARENT) should not return desktop for message windows\n");
5603         root = pGetAncestor(hwnd, GA_ROOT);
5604         ok(root == hwnd, "GetAncestor(GA_ROOT) should return hwnd for message windows\n");
5605         ok( !pGetAncestor(parent, GA_PARENT) || broken(pGetAncestor(parent, GA_PARENT) != 0), /* win2k */
5606             "parent shouldn't have parent %p\n", pGetAncestor(parent, GA_PARENT) );
5607         trace("parent %p root %p desktop %p\n", parent, root, desktop);
5608         if (!GetClassNameA( parent, buffer, sizeof(buffer) )) buffer[0] = 0;
5609         ok( !lstrcmpi( buffer, "Message" ), "wrong parent class '%s'\n", buffer );
5610         GetWindowRect( parent, &rect );
5611         ok( rect.left == 0 && rect.right == 100 && rect.top == 0 && rect.bottom == 100,
5612             "wrong parent rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
5613     }
5614     GetWindowRect( hwnd, &rect );
5615     ok( rect.left == 100 && rect.right == 300 && rect.top == 100 && rect.bottom == 300,
5616         "wrong window rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
5617
5618     /* test FindWindow behavior */
5619
5620     found = FindWindowExA( 0, 0, 0, "message window" );
5621     ok( found == hwnd, "didn't find message window %p/%p\n", found, hwnd );
5622     SetLastError(0xdeadbeef);
5623     found = FindWindowExA( GetDesktopWindow(), 0, 0, "message window" );
5624     ok( found == 0, "found message window %p/%p\n", found, hwnd );
5625     ok( GetLastError() == 0xdeadbeef, "expected deadbeef, got %d\n", GetLastError() );
5626     if (parent)
5627     {
5628         found = FindWindowExA( parent, 0, 0, "message window" );
5629         ok( found == hwnd, "didn't find message window %p/%p\n", found, hwnd );
5630     }
5631
5632     /* test IsChild behavior */
5633
5634     if (parent) ok( !IsChild( parent, hwnd ), "HWND_MESSAGE is child of top window\n" );
5635
5636     /* test IsWindowVisible behavior */
5637
5638     ok( !IsWindowVisible( hwnd ), "HWND_MESSAGE window is visible\n" );
5639     if (parent) ok( !IsWindowVisible( parent ), "HWND_MESSAGE parent is visible\n" );
5640
5641     DestroyWindow(hwnd);
5642 }
5643
5644 static void test_layered_window(void)
5645 {
5646     HWND hwnd;
5647     COLORREF key = 0;
5648     BYTE alpha = 0;
5649     DWORD flags = 0;
5650     POINT pt = { 0, 0 };
5651     SIZE sz = { 200, 200 };
5652     HDC hdc;
5653     HBITMAP hbm;
5654     BOOL ret;
5655
5656     if (!pGetLayeredWindowAttributes || !pSetLayeredWindowAttributes || !pUpdateLayeredWindow)
5657     {
5658         win_skip( "layered windows not supported\n" );
5659         return;
5660     }
5661
5662     hdc = CreateCompatibleDC( 0 );
5663     hbm = CreateCompatibleBitmap( hdc, 200, 200 );
5664     SelectObject( hdc, hbm );
5665
5666     hwnd = CreateWindowExA(0, "MainWindowClass", "message window", WS_CAPTION,
5667                            100, 100, 200, 200, 0, 0, 0, NULL);
5668     assert( hwnd );
5669     SetLastError( 0xdeadbeef );
5670     ret = pUpdateLayeredWindow( hwnd, 0, NULL, &sz, hdc, &pt, 0, NULL, ULW_OPAQUE );
5671     ok( !ret, "UpdateLayeredWindow should fail on non-layered window\n" );
5672     ok( GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", GetLastError() );
5673     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
5674     ok( !ret, "GetLayeredWindowAttributes should fail on non-layered window\n" );
5675     ret = pSetLayeredWindowAttributes( hwnd, 0, 0, LWA_ALPHA );
5676     ok( !ret, "SetLayeredWindowAttributes should fail on non-layered window\n" );
5677     SetWindowLong( hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED );
5678     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
5679     ok( !ret, "GetLayeredWindowAttributes should fail on layered but not initialized window\n" );
5680     ret = pUpdateLayeredWindow( hwnd, 0, NULL, &sz, hdc, &pt, 0, NULL, ULW_OPAQUE );
5681     ok( ret, "UpdateLayeredWindow should succeed on layered window\n" );
5682     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
5683     ok( !ret, "GetLayeredWindowAttributes should fail on layered but not initialized window\n" );
5684     ret = pSetLayeredWindowAttributes( hwnd, 0x123456, 44, LWA_ALPHA );
5685     ok( ret, "SetLayeredWindowAttributes should succeed on layered window\n" );
5686     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
5687     ok( ret, "GetLayeredWindowAttributes should succeed on layered window\n" );
5688     ok( key == 0x123456 || key == 0, "wrong color key %x\n", key );
5689     ok( alpha == 44, "wrong alpha %u\n", alpha );
5690     ok( flags == LWA_ALPHA, "wrong flags %x\n", flags );
5691     SetLastError( 0xdeadbeef );
5692     ret = pUpdateLayeredWindow( hwnd, 0, NULL, &sz, hdc, &pt, 0, NULL, ULW_OPAQUE );
5693     ok( !ret, "UpdateLayeredWindow should fail on layered but initialized window\n" );
5694     ok( GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", GetLastError() );
5695
5696     /* clearing WS_EX_LAYERED resets attributes */
5697     SetWindowLong( hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) & ~WS_EX_LAYERED );
5698     SetLastError( 0xdeadbeef );
5699     ret = pUpdateLayeredWindow( hwnd, 0, NULL, &sz, hdc, &pt, 0, NULL, ULW_OPAQUE );
5700     ok( !ret, "UpdateLayeredWindow should fail on non-layered window\n" );
5701     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
5702     ok( !ret, "GetLayeredWindowAttributes should fail on no longer layered window\n" );
5703     SetWindowLong( hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED );
5704     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
5705     ok( !ret, "GetLayeredWindowAttributes should fail on layered but not initialized window\n" );
5706     ret = pUpdateLayeredWindow( hwnd, 0, NULL, &sz, hdc, &pt, 0, NULL, ULW_OPAQUE );
5707     ok( ret, "UpdateLayeredWindow should succeed on layered window\n" );
5708     ret = pSetLayeredWindowAttributes( hwnd, 0x654321, 22, LWA_COLORKEY | LWA_ALPHA );
5709     ok( ret, "SetLayeredWindowAttributes should succeed on layered window\n" );
5710     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
5711     ok( ret, "GetLayeredWindowAttributes should succeed on layered window\n" );
5712     ok( key == 0x654321, "wrong color key %x\n", key );
5713     ok( alpha == 22, "wrong alpha %u\n", alpha );
5714     ok( flags == (LWA_COLORKEY | LWA_ALPHA), "wrong flags %x\n", flags );
5715     SetLastError( 0xdeadbeef );
5716     ret = pUpdateLayeredWindow( hwnd, 0, NULL, &sz, hdc, &pt, 0, NULL, ULW_OPAQUE );
5717     ok( !ret, "UpdateLayeredWindow should fail on layered but initialized window\n" );
5718     ok( GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", GetLastError() );
5719
5720     ret = pSetLayeredWindowAttributes( hwnd, 0x888888, 33, LWA_COLORKEY );
5721     ok( ret, "SetLayeredWindowAttributes should succeed on layered window\n" );
5722     alpha = 0;
5723     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
5724     ok( ret, "GetLayeredWindowAttributes should succeed on layered window\n" );
5725     ok( key == 0x888888, "wrong color key %x\n", key );
5726     /* alpha not changed on vista if LWA_ALPHA is not set */
5727     ok( alpha == 22 || alpha == 33, "wrong alpha %u\n", alpha );
5728     ok( flags == LWA_COLORKEY, "wrong flags %x\n", flags );
5729
5730     /* color key may or may not be changed without LWA_COLORKEY */
5731     ret = pSetLayeredWindowAttributes( hwnd, 0x999999, 44, 0 );
5732     ok( ret, "SetLayeredWindowAttributes should succeed on layered window\n" );
5733     alpha = 0;
5734     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
5735     ok( ret, "GetLayeredWindowAttributes should succeed on layered window\n" );
5736     ok( key == 0x888888 || key == 0x999999, "wrong color key %x\n", key );
5737     ok( alpha == 22 || alpha == 44, "wrong alpha %u\n", alpha );
5738     ok( flags == 0, "wrong flags %x\n", flags );
5739
5740     /* default alpha and color key is 0 */
5741     SetWindowLong( hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) & ~WS_EX_LAYERED );
5742     SetWindowLong( hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED );
5743     ret = pSetLayeredWindowAttributes( hwnd, 0x222222, 55, 0 );
5744     ok( ret, "SetLayeredWindowAttributes should succeed on layered window\n" );
5745     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
5746     ok( ret, "GetLayeredWindowAttributes should succeed on layered window\n" );
5747     ok( key == 0 || key == 0x222222, "wrong color key %x\n", key );
5748     ok( alpha == 0 || alpha == 55, "wrong alpha %u\n", alpha );
5749     ok( flags == 0, "wrong flags %x\n", flags );
5750
5751     DestroyWindow( hwnd );
5752     DeleteDC( hdc );
5753     DeleteObject( hbm );
5754 }
5755
5756 static MONITORINFO mi;
5757
5758 static LRESULT CALLBACK fullscreen_wnd_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
5759 {
5760     switch (msg)
5761     {
5762         case WM_NCCREATE:
5763         {
5764             CREATESTRUCTA *cs = (CREATESTRUCTA *)lp;
5765             trace("WM_NCCREATE: rect %d,%d-%d,%d\n", cs->x, cs->y, cs->cx, cs->cy);
5766             ok(cs->x == mi.rcMonitor.left && cs->y == mi.rcMonitor.top &&
5767                cs->cx == mi.rcMonitor.right && cs->cy == mi.rcMonitor.bottom,
5768                "expected %d,%d-%d,%d, got %d,%d-%d,%d\n",
5769                mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right, mi.rcMonitor.bottom,
5770                cs->x, cs->y, cs->cx, cs->cy);
5771             break;
5772         }
5773         case WM_GETMINMAXINFO:
5774         {
5775             MINMAXINFO *minmax = (MINMAXINFO *)lp;
5776             dump_minmax_info(minmax);
5777             ok(minmax->ptMaxPosition.x <= mi.rcMonitor.left, "%d <= %d\n", minmax->ptMaxPosition.x, mi.rcMonitor.left);
5778             ok(minmax->ptMaxPosition.y <= mi.rcMonitor.top, "%d <= %d\n", minmax->ptMaxPosition.y, mi.rcMonitor.top);
5779             ok(minmax->ptMaxSize.x >= mi.rcMonitor.right, "%d >= %d\n", minmax->ptMaxSize.x, mi.rcMonitor.right);
5780             ok(minmax->ptMaxSize.y >= mi.rcMonitor.bottom, "%d >= %d\n", minmax->ptMaxSize.y, mi.rcMonitor.bottom);
5781             break;
5782         }
5783     }
5784     return DefWindowProc(hwnd, msg, wp, lp);
5785 }
5786
5787 static void test_fullscreen(void)
5788 {
5789     static const DWORD t_style[] = {
5790         WS_OVERLAPPED, WS_POPUP, WS_CHILD, WS_THICKFRAME, WS_DLGFRAME
5791     };
5792     static const DWORD t_ex_style[] = {
5793         0, WS_EX_APPWINDOW, WS_EX_TOOLWINDOW
5794     };
5795     WNDCLASS cls;
5796     HWND hwnd;
5797     int i, j;
5798     POINT pt;
5799     RECT rc;
5800     HMONITOR hmon;
5801     LRESULT ret;
5802
5803     if (!pGetMonitorInfoA || !pMonitorFromPoint)
5804     {
5805         win_skip("GetMonitorInfoA or MonitorFromPoint are not available on this platform\n");
5806         return;
5807     }
5808
5809     pt.x = pt.y = 0;
5810     SetLastError(0xdeadbeef);
5811     hmon = pMonitorFromPoint(pt, MONITOR_DEFAULTTOPRIMARY);
5812     ok(hmon != 0, "MonitorFromPoint error %u\n", GetLastError());
5813
5814     mi.cbSize = sizeof(mi);
5815     SetLastError(0xdeadbeef);
5816     ret = pGetMonitorInfoA(hmon, &mi);
5817     ok(ret, "GetMonitorInfo error %u\n", GetLastError());
5818     trace("monitor (%d,%d-%d,%d), work (%d,%d-%d,%d)\n",
5819         mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right, mi.rcMonitor.bottom,
5820         mi.rcWork.left, mi.rcWork.top, mi.rcWork.right, mi.rcWork.bottom);
5821
5822     cls.style = 0;
5823     cls.lpfnWndProc = fullscreen_wnd_proc;
5824     cls.cbClsExtra = 0;
5825     cls.cbWndExtra = 0;
5826     cls.hInstance = GetModuleHandle(0);
5827     cls.hIcon = 0;
5828     cls.hCursor = LoadCursorA(0, IDC_ARROW);
5829     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
5830     cls.lpszMenuName = NULL;
5831     cls.lpszClassName = "fullscreen_class";
5832     RegisterClass(&cls);
5833
5834     for (i = 0; i < sizeof(t_style)/sizeof(t_style[0]); i++)
5835     {
5836         DWORD style, ex_style;
5837
5838         /* avoid a WM interaction */
5839         assert(!(t_style[i] & WS_VISIBLE));
5840
5841         for (j = 0; j < sizeof(t_ex_style)/sizeof(t_ex_style[0]); j++)
5842         {
5843             int fixup;
5844
5845             style = t_style[i];
5846             ex_style = t_ex_style[j];
5847
5848             hwnd = CreateWindowExA(ex_style, "fullscreen_class", NULL, style,
5849                                    mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right, mi.rcMonitor.bottom,
5850                                    GetDesktopWindow(), 0, GetModuleHandle(0), NULL);
5851             ok(hwnd != 0, "%d: CreateWindowExA(%#x/%#x) failed\n", i, ex_style, style);
5852             GetWindowRect(hwnd, &rc);
5853             trace("%#x/%#x: window rect %d,%d-%d,%d\n", ex_style, style, rc.left, rc.top, rc.right, rc.bottom);
5854             ok(rc.left <= mi.rcMonitor.left && rc.top <= mi.rcMonitor.top &&
5855                rc.right >= mi.rcMonitor.right && rc.bottom >= mi.rcMonitor.bottom,
5856                "%#x/%#x: window rect %d,%d-%d,%d\n", ex_style, style, rc.left, rc.top, rc.right, rc.bottom);
5857             DestroyWindow(hwnd);
5858
5859             style = t_style[i] | WS_MAXIMIZE;
5860             hwnd = CreateWindowExA(ex_style, "fullscreen_class", NULL, style,
5861                                    mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right, mi.rcMonitor.bottom,
5862                                    GetDesktopWindow(), 0, GetModuleHandle(0), NULL);
5863             ok(hwnd != 0, "%d: CreateWindowExA(%#x/%#x) failed\n", i, ex_style, style);
5864             GetWindowRect(hwnd, &rc);
5865             trace("%#x/%#x: window rect %d,%d-%d,%d\n", ex_style, style, rc.left, rc.top, rc.right, rc.bottom);
5866             ok(rc.left <= mi.rcMonitor.left && rc.top <= mi.rcMonitor.top &&
5867                rc.right >= mi.rcMonitor.right && rc.bottom >= mi.rcMonitor.bottom,
5868                "%#x/%#x: window rect %d,%d-%d,%d\n", ex_style, style, rc.left, rc.top, rc.right, rc.bottom);
5869             DestroyWindow(hwnd);
5870
5871             style = t_style[i] | WS_MAXIMIZE | WS_CAPTION;
5872             hwnd = CreateWindowExA(ex_style, "fullscreen_class", NULL, style,
5873                                    mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right, mi.rcMonitor.bottom,
5874                                    GetDesktopWindow(), 0, GetModuleHandle(0), NULL);
5875             ok(hwnd != 0, "%d: CreateWindowExA(%#x/%#x) failed\n", i, ex_style, style);
5876             GetWindowRect(hwnd, &rc);
5877             trace("%#x/%#x: window rect %d,%d-%d,%d\n", ex_style, style, rc.left, rc.top, rc.right, rc.bottom);
5878             ok(rc.left <= mi.rcMonitor.left && rc.top <= mi.rcMonitor.top &&
5879                rc.right >= mi.rcMonitor.right && rc.bottom >= mi.rcMonitor.bottom,
5880                "%#x/%#x: window rect %d,%d-%d,%d\n", ex_style, style, rc.left, rc.top, rc.right, rc.bottom);
5881             DestroyWindow(hwnd);
5882
5883             style = t_style[i] | WS_CAPTION | WS_MAXIMIZEBOX;
5884             hwnd = CreateWindowExA(ex_style, "fullscreen_class", NULL, style,
5885                                    mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right, mi.rcMonitor.bottom,
5886                                    GetDesktopWindow(), 0, GetModuleHandle(0), NULL);
5887             ok(hwnd != 0, "%d: CreateWindowExA(%#x/%#x) failed\n", i, ex_style, style);
5888             GetWindowRect(hwnd, &rc);
5889             trace("%#x/%#x: window rect %d,%d-%d,%d\n", ex_style, style, rc.left, rc.top, rc.right, rc.bottom);
5890             ok(rc.left <= mi.rcMonitor.left && rc.top <= mi.rcMonitor.top &&
5891                rc.right >= mi.rcMonitor.right && rc.bottom >= mi.rcMonitor.bottom,
5892                "%#x/%#x: window rect %d,%d-%d,%d\n", ex_style, style, rc.left, rc.top, rc.right, rc.bottom);
5893             DestroyWindow(hwnd);
5894
5895             style = t_style[i] | WS_MAXIMIZE | WS_CAPTION | WS_MAXIMIZEBOX;
5896             hwnd = CreateWindowExA(ex_style, "fullscreen_class", NULL, style,
5897                                    mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right, mi.rcMonitor.bottom,
5898                                    GetDesktopWindow(), 0, GetModuleHandle(0), NULL);
5899             ok(hwnd != 0, "%d: CreateWindowExA(%#x/%#x) failed\n", i, ex_style, style);
5900             GetWindowRect(hwnd, &rc);
5901             trace("%#x/%#x: window rect %d,%d-%d,%d\n", ex_style, style, rc.left, rc.top, rc.right, rc.bottom);
5902             /* Windows makes a maximized window slightly larger (to hide the borders?) */
5903             fixup = min(abs(rc.left), abs(rc.top));
5904             InflateRect(&rc, -fixup, -fixup);
5905             ok(rc.left >= mi.rcWork.left && rc.top <= mi.rcWork.top &&
5906                rc.right <= mi.rcWork.right && rc.bottom <= mi.rcWork.bottom,
5907                "%#x/%#x: window rect %d,%d-%d,%d must be in %d,%d-%d,%d\n",
5908                ex_style, style, rc.left, rc.top, rc.right, rc.bottom,
5909                mi.rcWork.left, mi.rcWork.top, mi.rcWork.right, mi.rcWork.bottom);
5910             DestroyWindow(hwnd);
5911
5912             style = t_style[i] | WS_MAXIMIZE | WS_MAXIMIZEBOX;
5913             hwnd = CreateWindowExA(ex_style, "fullscreen_class", NULL, style,
5914                                    mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right, mi.rcMonitor.bottom,
5915                                    GetDesktopWindow(), 0, GetModuleHandle(0), NULL);
5916             ok(hwnd != 0, "%d: CreateWindowExA(%#x/%#x) failed\n", i, ex_style, style);
5917             GetWindowRect(hwnd, &rc);
5918             trace("%#x/%#x: window rect %d,%d-%d,%d\n", ex_style, style, rc.left, rc.top, rc.right, rc.bottom);
5919             /* Windows makes a maximized window slightly larger (to hide the borders?) */
5920             fixup = min(abs(rc.left), abs(rc.top));
5921             InflateRect(&rc, -fixup, -fixup);
5922             if (style & (WS_CHILD | WS_POPUP))
5923                 ok(rc.left <= mi.rcMonitor.left && rc.top <= mi.rcMonitor.top &&
5924                    rc.right >= mi.rcMonitor.right && rc.bottom >= mi.rcMonitor.bottom,
5925                    "%#x/%#x: window rect %d,%d-%d,%d\n", ex_style, style, rc.left, rc.top, rc.right, rc.bottom);
5926             else
5927                 ok(rc.left >= mi.rcWork.left && rc.top <= mi.rcWork.top &&
5928                    rc.right <= mi.rcWork.right && rc.bottom <= mi.rcWork.bottom,
5929                    "%#x/%#x: window rect %d,%d-%d,%d\n", ex_style, style, rc.left, rc.top, rc.right, rc.bottom);
5930             DestroyWindow(hwnd);
5931         }
5932     }
5933
5934     UnregisterClass("fullscreen_class", GetModuleHandle(0));
5935 }
5936
5937 static BOOL test_thick_child_got_minmax;
5938 static const char * test_thick_child_name;
5939 static LONG test_thick_child_style;
5940 static LONG test_thick_child_exStyle;
5941
5942 static LRESULT WINAPI test_thick_child_size_winproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
5943 {
5944     MINMAXINFO* minmax;
5945     int expectedMinTrackX;
5946     int expectedMinTrackY;
5947     int actualMinTrackX;
5948     int actualMinTrackY;
5949     int expectedMaxTrackX;
5950     int expectedMaxTrackY;
5951     int actualMaxTrackX;
5952     int actualMaxTrackY;
5953     int expectedMaxSizeX;
5954     int expectedMaxSizeY;
5955     int actualMaxSizeX;
5956     int actualMaxSizeY;
5957     int expectedPosX;
5958     int expectedPosY;
5959     int actualPosX;
5960     int actualPosY;
5961     LONG adjustedStyle;
5962     RECT rect;
5963     switch (msg)
5964     {
5965         case WM_GETMINMAXINFO:
5966         {
5967             minmax = (MINMAXINFO *)lparam;
5968             trace("hwnd %p, WM_GETMINMAXINFO, %08lx, %08lx\n", hwnd, wparam, lparam);
5969             dump_minmax_info( minmax );
5970
5971             test_thick_child_got_minmax = TRUE;
5972
5973
5974             adjustedStyle = test_thick_child_style;
5975             if ((adjustedStyle & WS_CAPTION) == WS_CAPTION)
5976                 adjustedStyle &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
5977             GetClientRect(GetParent(hwnd), &rect);
5978             AdjustWindowRectEx(&rect, adjustedStyle, FALSE, test_thick_child_exStyle);
5979
5980             if (test_thick_child_style & (WS_DLGFRAME | WS_BORDER))
5981             {
5982                 expectedMinTrackX = GetSystemMetrics(SM_CXMINTRACK);
5983                 expectedMinTrackY = GetSystemMetrics(SM_CYMINTRACK);
5984             }
5985             else
5986             {
5987                 expectedMinTrackX = -2 * rect.left;
5988                 expectedMinTrackY = -2 * rect.top;
5989             }
5990             actualMinTrackX =  minmax->ptMinTrackSize.x;
5991             actualMinTrackY =  minmax->ptMinTrackSize.y;
5992
5993             ok(actualMinTrackX == expectedMinTrackX && actualMinTrackY == expectedMinTrackY,
5994                 "expected minTrack %dx%d, actual minTrack %dx%d for %s\n",
5995                 expectedMinTrackX, expectedMinTrackY, actualMinTrackX, actualMinTrackY,
5996                 test_thick_child_name);
5997
5998             actualMaxTrackX = minmax->ptMaxTrackSize.x;
5999             actualMaxTrackY = minmax->ptMaxTrackSize.y;
6000             expectedMaxTrackX = GetSystemMetrics(SM_CXMAXTRACK);
6001             expectedMaxTrackY = GetSystemMetrics(SM_CYMAXTRACK);
6002             ok(actualMaxTrackX == expectedMaxTrackX &&  actualMaxTrackY == expectedMaxTrackY,
6003                 "expected maxTrack %dx%d, actual maxTrack %dx%d for %s\n",
6004                  expectedMaxTrackX, expectedMaxTrackY, actualMaxTrackX, actualMaxTrackY,
6005                 test_thick_child_name);
6006
6007             expectedMaxSizeX = rect.right - rect.left;
6008             expectedMaxSizeY = rect.bottom - rect.top;
6009             actualMaxSizeX = minmax->ptMaxSize.x;
6010             actualMaxSizeY = minmax->ptMaxSize.y;
6011
6012             ok(actualMaxSizeX == expectedMaxSizeX &&  actualMaxSizeY == expectedMaxSizeY,
6013                 "expected maxSize %dx%d, actual maxSize %dx%d for %s\n",
6014                 expectedMaxSizeX, expectedMaxSizeY, actualMaxSizeX, actualMaxSizeY,
6015                 test_thick_child_name);
6016
6017
6018             expectedPosX = rect.left;
6019             expectedPosY = rect.top;
6020             actualPosX = minmax->ptMaxPosition.x;
6021             actualPosY = minmax->ptMaxPosition.y;
6022             ok(actualPosX == expectedPosX && actualPosY == expectedPosY,
6023                 "expected maxPosition (%d/%d), actual maxPosition (%d/%d) for %s\n",
6024                 expectedPosX, expectedPosY, actualPosX, actualPosY, test_thick_child_name);
6025
6026             break;
6027         }
6028     }
6029
6030     return DefWindowProcA(hwnd, msg, wparam, lparam);
6031 }
6032
6033 #define NUMBER_OF_THICK_CHILD_TESTS 16
6034 static void test_thick_child_size(HWND parentWindow)
6035 {
6036     BOOL success;
6037     RECT childRect;
6038     RECT adjustedParentRect;
6039     HWND childWindow;
6040     LONG childWidth;
6041     LONG childHeight;
6042     LONG expectedWidth;
6043     LONG expectedHeight;
6044     WNDCLASSA cls;
6045     LPCTSTR className = "THICK_CHILD_CLASS";
6046     int i;
6047     LONG adjustedStyle;
6048     static const LONG styles[NUMBER_OF_THICK_CHILD_TESTS] = {
6049         WS_CHILD | WS_VISIBLE | WS_THICKFRAME,
6050         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME,
6051         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME | WS_BORDER,
6052         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_BORDER,
6053         WS_CHILD | WS_VISIBLE | WS_THICKFRAME,
6054         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME,
6055         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME | WS_BORDER,
6056         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_BORDER,
6057         WS_CHILD | WS_VISIBLE | WS_THICKFRAME,
6058         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME,
6059         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME | WS_BORDER,
6060         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_BORDER,
6061         WS_CHILD | WS_VISIBLE | WS_THICKFRAME,
6062         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME,
6063         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME | WS_BORDER,
6064         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_BORDER,
6065     };
6066
6067     static const LONG exStyles[NUMBER_OF_THICK_CHILD_TESTS] = {
6068         0,
6069         0,
6070         0,
6071         0,
6072         WS_EX_DLGMODALFRAME,
6073         WS_EX_DLGMODALFRAME,
6074         WS_EX_DLGMODALFRAME,
6075         WS_EX_DLGMODALFRAME,
6076         WS_EX_STATICEDGE,
6077         WS_EX_STATICEDGE,
6078         WS_EX_STATICEDGE,
6079         WS_EX_STATICEDGE,
6080         WS_EX_STATICEDGE | WS_EX_DLGMODALFRAME,
6081         WS_EX_STATICEDGE | WS_EX_DLGMODALFRAME,
6082         WS_EX_STATICEDGE | WS_EX_DLGMODALFRAME,
6083         WS_EX_STATICEDGE | WS_EX_DLGMODALFRAME,
6084     };
6085     static const char *styleName[NUMBER_OF_THICK_CHILD_TESTS] = {
6086         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME",
6087         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME",
6088         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME | WS_BORDER",
6089         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_BORDER",
6090         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME, exstyle= WS_EX_DLGMODALFRAME",
6091         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME exstyle= WS_EX_DLGMODALFRAME",
6092         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME | WS_BORDER exstyle= WS_EX_DLGMODALFRAME",
6093         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_BORDER exstyle= WS_EX_DLGMODALFRAME",
6094         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME exstyle= WS_EX_STATICEDGE",
6095         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME exstyle= WS_EX_STATICEDGE",
6096         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME | WS_BORDER exstyle= WS_EX_STATICEDGE",
6097         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_BORDER exstyle= WS_EX_STATICEDGE",
6098         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME, exstyle= WS_EX_STATICEDGE | WS_EX_DLGMODALFRAME",
6099         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME exstyle= WS_EX_STATICEDGE | WS_EX_DLGMODALFRAME",
6100         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME | WS_BORDER exstyle= WS_EX_STATICEDGE | WS_EX_DLGMODALFRAME",
6101         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_BORDER exstyle= WS_EX_STATICEDGE | WS_EX_DLGMODALFRAME",
6102     };
6103
6104     cls.style = 0;
6105     cls.lpfnWndProc = test_thick_child_size_winproc;
6106     cls.cbClsExtra = 0;
6107     cls.cbWndExtra = 0;
6108     cls.hInstance = GetModuleHandleA(0);
6109     cls.hIcon = 0;
6110     cls.hCursor = LoadCursorA(0, IDC_ARROW);
6111     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
6112     cls.lpszMenuName = NULL;
6113     cls.lpszClassName = className;
6114     SetLastError(0xdeadbeef);
6115     success = RegisterClassA(&cls);
6116     ok(success,"RegisterClassA failed, error: %u\n", GetLastError());
6117
6118     for(i = 0; i < NUMBER_OF_THICK_CHILD_TESTS; i++)
6119     {
6120         test_thick_child_name = styleName[i];
6121         test_thick_child_style = styles[i];
6122         test_thick_child_exStyle = exStyles[i];
6123         test_thick_child_got_minmax = FALSE;
6124
6125         SetLastError(0xdeadbeef);
6126         childWindow = CreateWindowEx( exStyles[i], className, "", styles[i],  0, 0, 0, 0, parentWindow, 0,  GetModuleHandleA(0),  NULL );
6127         ok(childWindow != NULL, "Failed to create child window, error: %u\n", GetLastError());
6128
6129         ok(test_thick_child_got_minmax, "Got no WM_GETMINMAXINFO\n");
6130
6131         SetLastError(0xdeadbeef);
6132         success = GetWindowRect(childWindow, &childRect);
6133         ok(success,"GetWindowRect call failed, error: %u\n", GetLastError());
6134         childWidth = childRect.right - childRect.left;
6135         childHeight = childRect.bottom - childRect.top;
6136
6137         adjustedStyle = styles[i];
6138         if ((adjustedStyle & WS_CAPTION) == WS_CAPTION)
6139             adjustedStyle &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
6140         GetClientRect(GetParent(childWindow), &adjustedParentRect);
6141         AdjustWindowRectEx(&adjustedParentRect, adjustedStyle, FALSE, test_thick_child_exStyle);
6142
6143
6144         if (test_thick_child_style & (WS_DLGFRAME | WS_BORDER))
6145         {
6146             expectedWidth = GetSystemMetrics(SM_CXMINTRACK);
6147             expectedHeight = GetSystemMetrics(SM_CYMINTRACK);
6148         }
6149         else
6150         {
6151             expectedWidth = -2 * adjustedParentRect.left;
6152             expectedHeight = -2 * adjustedParentRect.top;
6153         }
6154
6155         ok((childWidth == expectedWidth) && (childHeight == expectedHeight),
6156             "size of window (%s) is wrong: expected size %dx%d != actual size %dx%d\n",
6157             test_thick_child_name, expectedWidth, expectedHeight, childWidth, childHeight);
6158
6159         SetLastError(0xdeadbeef);
6160         success = DestroyWindow(childWindow);
6161         ok(success,"DestroyWindow call failed, error: %u\n", GetLastError());
6162     }
6163     ok(UnregisterClass(className, GetModuleHandleA(0)),"UnregisterClass call failed\n");
6164 }
6165
6166 static void test_handles( HWND full_hwnd )
6167 {
6168     HWND hwnd = full_hwnd;
6169     BOOL ret;
6170     RECT rect;
6171
6172     SetLastError( 0xdeadbeef );
6173     ret = GetWindowRect( hwnd, &rect );
6174     ok( ret, "GetWindowRect failed for %p err %u\n", hwnd, GetLastError() );
6175
6176 #ifdef _WIN64
6177     if ((ULONG_PTR)full_hwnd >> 32)
6178         hwnd = (HWND)((ULONG_PTR)full_hwnd & ~0u);
6179     else
6180         hwnd = (HWND)((ULONG_PTR)full_hwnd | ((ULONG_PTR)~0u << 32));
6181     SetLastError( 0xdeadbeef );
6182     ret = GetWindowRect( hwnd, &rect );
6183     ok( ret, "GetWindowRect failed for %p err %u\n", hwnd, GetLastError() );
6184
6185     hwnd = (HWND)(((ULONG_PTR)full_hwnd & ~0u) | ((ULONG_PTR)0x1234 << 32));
6186     SetLastError( 0xdeadbeef );
6187     ret = GetWindowRect( hwnd, &rect );
6188     ok( ret, "GetWindowRect failed for %p err %u\n", hwnd, GetLastError() );
6189
6190     hwnd = (HWND)(((ULONG_PTR)full_hwnd & 0xffff) | ((ULONG_PTR)0x9876 << 16));
6191     SetLastError( 0xdeadbeef );
6192     ret = GetWindowRect( hwnd, &rect );
6193     ok( !ret, "GetWindowRect succeeded for %p\n", hwnd );
6194     ok( GetLastError() == ERROR_INVALID_WINDOW_HANDLE, "wrong error %u\n", GetLastError() );
6195
6196     hwnd = (HWND)(((ULONG_PTR)full_hwnd & 0xffff) | ((ULONG_PTR)0x12345678 << 16));
6197     SetLastError( 0xdeadbeef );
6198     ret = GetWindowRect( hwnd, &rect );
6199     ok( !ret, "GetWindowRect succeeded for %p\n", hwnd );
6200     ok( GetLastError() == ERROR_INVALID_WINDOW_HANDLE, "wrong error %u\n", GetLastError() );
6201 #endif
6202 }
6203
6204 static void test_winregion(void)
6205 {
6206     HWND hwnd;
6207     RECT r;
6208     int ret, width;
6209     HRGN hrgn;
6210
6211     if (!pGetWindowRgnBox)
6212     {
6213         win_skip("GetWindowRgnBox not supported\n");
6214         return;
6215     }
6216
6217     hwnd = CreateWindowExA(0, "static", NULL, WS_VISIBLE, 10, 10, 10, 10, NULL, 0, 0, NULL);
6218     /* NULL prect */
6219     SetLastError(0xdeadbeef);
6220     ret = pGetWindowRgnBox(hwnd, NULL);
6221     ok( ret == ERROR, "Expected ERROR, got %d\n", ret);
6222     ok( GetLastError() == 0xdeadbeef, "Expected , got %d\n", GetLastError());
6223
6224     hrgn = CreateRectRgn(2, 3, 10, 15);
6225     ok( hrgn != NULL, "Region creation failed\n");
6226     if (hrgn)
6227     {
6228         SetWindowRgn(hwnd, hrgn, FALSE);
6229
6230         SetLastError(0xdeadbeef);
6231         ret = pGetWindowRgnBox(hwnd, NULL);
6232         ok( ret == ERROR, "Expected ERROR, got %d\n", ret);
6233         ok( GetLastError() == 0xdeadbeef, "Expected , got %d\n", GetLastError());
6234
6235         r.left = r.top = r.right = r.bottom = 0;
6236         ret = pGetWindowRgnBox(hwnd, &r);
6237         ok( ret == SIMPLEREGION, "Expected SIMPLEREGION, got %d\n", ret);
6238         ok( r.left == 2 && r.top == 3 && r.right == 10 && r.bottom == 15,
6239            "Expected (2,3,10,15), got (%d,%d,%d,%d)\n", r.left, r.top,
6240                                                             r.right, r.bottom);
6241         if (pMirrorRgn)
6242         {
6243             hrgn = CreateRectRgn(2, 3, 10, 15);
6244             ret = pMirrorRgn( hwnd, hrgn );
6245             ok( ret == TRUE, "MirrorRgn failed %u\n", ret );
6246             r.left = r.top = r.right = r.bottom = 0;
6247             GetWindowRect( hwnd, &r );
6248             width = r.right - r.left;
6249             r.left = r.top = r.right = r.bottom = 0;
6250             ret = GetRgnBox( hrgn, &r );
6251             ok( ret == SIMPLEREGION, "GetRgnBox failed %u\n", ret );
6252             ok( r.left == width - 10 && r.top == 3 && r.right == width - 2 && r.bottom == 15,
6253                 "Wrong rectangle (%d,%d,%d,%d) for width %d\n", r.left, r.top, r.right, r.bottom, width );
6254         }
6255         else win_skip( "MirrorRgn not supported\n" );
6256     }
6257     DestroyWindow(hwnd);
6258 }
6259
6260 static void test_rtl_layout(void)
6261 {
6262     HWND parent, child;
6263     RECT r;
6264     POINT pt;
6265
6266     if (!pSetProcessDefaultLayout)
6267     {
6268         win_skip( "SetProcessDefaultLayout not supported\n" );
6269         return;
6270     }
6271
6272     parent = CreateWindowExA(WS_EX_LAYOUTRTL, "static", NULL, WS_POPUP, 100, 100, 300, 300, NULL, 0, 0, NULL);
6273     child = CreateWindowExA(0, "static", NULL, WS_CHILD, 10, 10, 20, 20, parent, 0, 0, NULL);
6274
6275     GetWindowRect( parent, &r );
6276     ok( r.left == 100 && r.right == 400, "wrong rect %d,%d - %d,%d\n", r.left, r.top, r.right, r.bottom );
6277     GetClientRect( parent, &r );
6278     ok( r.left == 0 && r.right == 300, "wrong rect %d,%d - %d,%d\n", r.left, r.top, r.right, r.bottom );
6279     GetClientRect( child, &r );
6280     ok( r.left == 0 && r.right == 20, "wrong rect %d,%d - %d,%d\n", r.left, r.top, r.right, r.bottom );
6281     MapWindowPoints( child, parent, (POINT *)&r, 2 );
6282     ok( r.left == 10 && r.right == 30, "wrong rect %d,%d - %d,%d\n", r.left, r.top, r.right, r.bottom );
6283     GetWindowRect( child, &r );
6284     ok( r.left == 370 && r.right == 390, "wrong rect %d,%d - %d,%d\n", r.left, r.top, r.right, r.bottom );
6285     MapWindowPoints( NULL, parent, (POINT *)&r, 2 );
6286     ok( r.left == 10 && r.right == 30, "wrong rect %d,%d - %d,%d\n", r.left, r.top, r.right, r.bottom );
6287     GetWindowRect( child, &r );
6288     MapWindowPoints( NULL, parent, (POINT *)&r, 1 );
6289     MapWindowPoints( NULL, parent, (POINT *)&r + 1, 1 );
6290     ok( r.left == 30 && r.right == 10, "wrong rect %d,%d - %d,%d\n", r.left, r.top, r.right, r.bottom );
6291     pt.x = pt.y = 12;
6292     MapWindowPoints( child, parent, &pt, 1 );
6293     ok( pt.x == 22 && pt.y == 22, "wrong point %d,%d\n", pt.x, pt.y );
6294     SetWindowPos( parent, 0, 0, 0, 250, 250, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE );
6295     GetWindowRect( parent, &r );
6296     ok( r.left == 100 && r.right == 350, "wrong rect %d,%d - %d,%d\n", r.left, r.top, r.right, r.bottom );
6297     GetWindowRect( child, &r );
6298     ok( r.left == 320 && r.right == 340, "wrong rect %d,%d - %d,%d\n", r.left, r.top, r.right, r.bottom );
6299     SetWindowLongW( parent, GWL_EXSTYLE, 0 );
6300     GetWindowRect( child, &r );
6301     ok( r.left == 320 && r.right == 340, "wrong rect %d,%d - %d,%d\n", r.left, r.top, r.right, r.bottom );
6302     MapWindowPoints( NULL, parent, (POINT *)&r, 2 );
6303     ok( r.left == 220 && r.right == 240, "wrong rect %d,%d - %d,%d\n", r.left, r.top, r.right, r.bottom );
6304     SetWindowLongW( parent, GWL_EXSTYLE, WS_EX_LAYOUTRTL );
6305     GetWindowRect( child, &r );
6306     ok( r.left == 320 && r.right == 340, "wrong rect %d,%d - %d,%d\n", r.left, r.top, r.right, r.bottom );
6307     MapWindowPoints( NULL, parent, (POINT *)&r, 2 );
6308     ok( r.left == 10 && r.right == 30, "wrong rect %d,%d - %d,%d\n", r.left, r.top, r.right, r.bottom );
6309     SetWindowPos( child, 0, 0, 0, 30, 30, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE );
6310     GetWindowRect( child, &r );
6311     ok( r.left == 310 && r.right == 340, "wrong rect %d,%d - %d,%d\n", r.left, r.top, r.right, r.bottom );
6312     MapWindowPoints( NULL, parent, (POINT *)&r, 2 );
6313     ok( r.left == 10 && r.right == 40, "wrong rect %d,%d - %d,%d\n", r.left, r.top, r.right, r.bottom );
6314     DestroyWindow( child );
6315     DestroyWindow( parent );
6316 }
6317
6318 static void test_FindWindowEx(void)
6319 {
6320     HWND hwnd, found;
6321     CHAR title[1];
6322
6323     hwnd = CreateWindowExA( 0, "MainWindowClass", "caption", WS_POPUP, 0,0,0,0, 0, 0, 0, NULL );
6324     ok( hwnd != 0, "CreateWindowExA error %d\n", GetLastError() );
6325
6326     title[0] = 0;
6327
6328     found = FindWindowExA( 0, 0, "MainWindowClass", title );
6329     ok( found == NULL, "expected a NULL hwnd\n" );
6330     found = FindWindowExA( 0, 0, "MainWindowClass", NULL );
6331     ok( found == hwnd, "found is %p, expected a valid hwnd\n", found );
6332
6333     DestroyWindow( hwnd );
6334
6335     hwnd = CreateWindowExA( 0, "MainWindowClass", NULL, WS_POPUP, 0,0,0,0, 0, 0, 0, NULL );
6336     ok( hwnd != 0, "CreateWindowExA error %d\n", GetLastError() );
6337
6338     found = FindWindowExA( 0, 0, "MainWindowClass", title );
6339     ok( found == hwnd, "found is %p, expected a valid hwnd\n", found );
6340     found = FindWindowExA( 0, 0, "MainWindowClass", NULL );
6341     ok( found == hwnd, "found is %p, expected a valid hwnd\n", found );
6342
6343     DestroyWindow( hwnd );
6344
6345     /* test behaviour with a window title that is an empty character */
6346     found = FindWindowExA( 0, 0, "Shell_TrayWnd", title );
6347     ok( found != NULL, "found is NULL, expected a valid hwnd\n" );
6348     found = FindWindowExA( 0, 0, "Shell_TrayWnd", NULL );
6349     ok( found != NULL, "found is NULL, expected a valid hwnd\n" );
6350 }
6351
6352 static void test_GetLastActivePopup(void)
6353 {
6354     HWND hwndOwner, hwndPopup1, hwndPopup2;
6355
6356     hwndOwner = CreateWindowExA(0, "MainWindowClass", NULL,
6357                                 WS_VISIBLE | WS_POPUPWINDOW,
6358                                 100, 100, 200, 200,
6359                                 NULL, 0, GetModuleHandle(0), NULL);
6360     hwndPopup1 = CreateWindowExA(0, "MainWindowClass", NULL,
6361                                  WS_VISIBLE | WS_POPUPWINDOW,
6362                                  100, 100, 200, 200,
6363                                  hwndOwner, 0, GetModuleHandle(0), NULL);
6364     hwndPopup2 = CreateWindowExA(0, "MainWindowClass", NULL,
6365                                  WS_VISIBLE | WS_POPUPWINDOW,
6366                                  100, 100, 200, 200,
6367                                  hwndPopup1, 0, GetModuleHandle(0), NULL);
6368     ok( GetLastActivePopup(hwndOwner) == hwndPopup2, "wrong last active popup\n" );
6369     DestroyWindow( hwndPopup2 );
6370     DestroyWindow( hwndPopup1 );
6371     DestroyWindow( hwndOwner );
6372 }
6373
6374 START_TEST(win)
6375 {
6376     HMODULE user32 = GetModuleHandleA( "user32.dll" );
6377     HMODULE gdi32 = GetModuleHandleA("gdi32.dll");
6378     pGetAncestor = (void *)GetProcAddress( user32, "GetAncestor" );
6379     pGetWindowInfo = (void *)GetProcAddress( user32, "GetWindowInfo" );
6380     pGetWindowModuleFileNameA = (void *)GetProcAddress( user32, "GetWindowModuleFileNameA" );
6381     pGetLayeredWindowAttributes = (void *)GetProcAddress( user32, "GetLayeredWindowAttributes" );
6382     pSetLayeredWindowAttributes = (void *)GetProcAddress( user32, "SetLayeredWindowAttributes" );
6383     pUpdateLayeredWindow = (void *)GetProcAddress( user32, "UpdateLayeredWindow" );
6384     pGetMonitorInfoA = (void *)GetProcAddress( user32,  "GetMonitorInfoA" );
6385     pMonitorFromPoint = (void *)GetProcAddress( user32,  "MonitorFromPoint" );
6386     pGetWindowRgnBox = (void *)GetProcAddress( user32, "GetWindowRgnBox" );
6387     pGetGUIThreadInfo = (void *)GetProcAddress( user32, "GetGUIThreadInfo" );
6388     pGetProcessDefaultLayout = (void *)GetProcAddress( user32, "GetProcessDefaultLayout" );
6389     pSetProcessDefaultLayout = (void *)GetProcAddress( user32, "SetProcessDefaultLayout" );
6390     pGetLayout = (void *)GetProcAddress( gdi32, "GetLayout" );
6391     pSetLayout = (void *)GetProcAddress( gdi32, "SetLayout" );
6392     pMirrorRgn = (void *)GetProcAddress( gdi32, "MirrorRgn" );
6393
6394     if (!RegisterWindowClasses()) assert(0);
6395
6396     SetLastError(0xdeafbeef);
6397     GetWindowLongPtrW(GetDesktopWindow(), GWLP_WNDPROC);
6398     is_win9x = (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED);
6399
6400     hhook = SetWindowsHookExA(WH_CBT, cbt_hook_proc, 0, GetCurrentThreadId());
6401     if (!hhook) win_skip( "Cannot set CBT hook, skipping some tests\n" );
6402
6403     /* make sure that these tests are executed first */
6404     test_FindWindowEx();
6405     test_SetParent();
6406
6407     hwndMain = CreateWindowExA(/*WS_EX_TOOLWINDOW*/ 0, "MainWindowClass", "Main window",
6408                                WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
6409                                WS_MAXIMIZEBOX | WS_POPUP,
6410                                100, 100, 200, 200,
6411                                0, 0, GetModuleHandle(0), NULL);
6412     hwndMain2 = CreateWindowExA(/*WS_EX_TOOLWINDOW*/ 0, "MainWindowClass", "Main window 2",
6413                                 WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
6414                                 WS_MAXIMIZEBOX | WS_POPUP,
6415                                 100, 100, 200, 200,
6416                                 0, 0, GetModuleHandle(0), NULL);
6417     assert( hwndMain );
6418     assert( hwndMain2 );
6419
6420     our_pid = GetWindowThreadProcessId(hwndMain, NULL);
6421
6422     /* Add the tests below this line */
6423     test_thick_child_size(hwndMain);
6424     test_fullscreen();
6425     test_hwnd_message();
6426     test_nonclient_area(hwndMain);
6427     test_params();
6428     test_GetWindowModuleFileName();
6429     test_capture_1();
6430     test_capture_2();
6431     test_capture_3(hwndMain, hwndMain2);
6432     test_capture_4();
6433     test_rtl_layout();
6434
6435     test_CreateWindow();
6436     test_parent_owner();
6437     test_enum_thread_windows();
6438
6439     test_mdi();
6440     test_icons();
6441     test_SetWindowPos(hwndMain);
6442     test_SetMenu(hwndMain);
6443     test_SetFocus(hwndMain);
6444     test_SetActiveWindow(hwndMain);
6445     test_NCRedraw();
6446
6447     test_children_zorder(hwndMain);
6448     test_popup_zorder(hwndMain2, hwndMain, WS_POPUP);
6449     test_popup_zorder(hwndMain2, hwndMain, 0);
6450     test_GetLastActivePopup();
6451     test_keyboard_input(hwndMain);
6452     test_mouse_input(hwndMain);
6453     test_validatergn(hwndMain);
6454     test_nccalcscroll( hwndMain);
6455     test_scrollwindow( hwndMain);
6456     test_scrollvalidate( hwndMain);
6457     test_scrolldc( hwndMain);
6458     test_scroll();
6459     test_IsWindowUnicode();
6460     test_vis_rgn(hwndMain);
6461
6462     test_AdjustWindowRect();
6463     test_window_styles();
6464     test_redrawnow();
6465     test_csparentdc();
6466     test_SetWindowLong();
6467     test_ShowWindow();
6468     if (0) test_gettext(); /* crashes on NT4 */
6469     test_GetUpdateRect();
6470     test_Expose();
6471     test_layered_window();
6472
6473     test_SetForegroundWindow(hwndMain);
6474     test_shell_window();
6475     test_handles( hwndMain );
6476     test_winregion();
6477
6478     /* add the tests above this line */
6479     if (hhook) UnhookWindowsHookEx(hhook);
6480
6481     DestroyWindow(hwndMain2);
6482     DestroyWindow(hwndMain);
6483 }