Fix signed/unsigned comparison warnings.
[wine] / dlls / user / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 static HWND (WINAPI *pGetAncestor)(HWND,UINT);
46 static BOOL (WINAPI *pGetWindowInfo)(HWND,WINDOWINFO*);
47
48 static HWND hwndMessage;
49 static HWND hwndMain, hwndMain2;
50 static HHOOK hhook;
51
52 /* check the values returned by the various parent/owner functions on a given window */
53 static void check_parents( HWND hwnd, HWND ga_parent, HWND gwl_parent, HWND get_parent,
54                            HWND gw_owner, HWND ga_root, HWND ga_root_owner )
55 {
56     HWND res;
57
58     if (pGetAncestor)
59     {
60         res = pGetAncestor( hwnd, GA_PARENT );
61         ok( res == ga_parent, "Wrong result for GA_PARENT %p expected %p\n", res, ga_parent );
62     }
63     res = (HWND)GetWindowLongA( hwnd, GWL_HWNDPARENT );
64     ok( res == gwl_parent, "Wrong result for GWL_HWNDPARENT %p expected %p\n", res, gwl_parent );
65     res = GetParent( hwnd );
66     ok( res == get_parent, "Wrong result for GetParent %p expected %p\n", res, get_parent );
67     res = GetWindow( hwnd, GW_OWNER );
68     ok( res == gw_owner, "Wrong result for GW_OWNER %p expected %p\n", res, gw_owner );
69     if (pGetAncestor)
70     {
71         res = pGetAncestor( hwnd, GA_ROOT );
72         ok( res == ga_root, "Wrong result for GA_ROOT %p expected %p\n", res, ga_root );
73         res = pGetAncestor( hwnd, GA_ROOTOWNER );
74         ok( res == ga_root_owner, "Wrong result for GA_ROOTOWNER %p expected %p\n", res, ga_root_owner );
75     }
76 }
77
78
79 static HWND create_tool_window( LONG style, HWND parent )
80 {
81     HWND ret = CreateWindowExA(0, "ToolWindowClass", "Tool window 1", style,
82                                0, 0, 100, 100, parent, 0, 0, NULL );
83     ok( ret != 0, "Creation failed\n" );
84     return ret;
85 }
86
87 /* test parent and owner values for various combinations */
88 static void test_parent_owner(void)
89 {
90     LONG style;
91     HWND test, owner, ret;
92     HWND desktop = GetDesktopWindow();
93     HWND child = create_tool_window( WS_CHILD, hwndMain );
94
95     trace( "main window %p main2 %p desktop %p child %p\n", hwndMain, hwndMain2, desktop, child );
96
97     /* child without parent, should fail */
98     test = CreateWindowExA(0, "ToolWindowClass", "Tool window 1",
99                            WS_CHILD, 0, 0, 100, 100, 0, 0, 0, NULL );
100     ok( !test, "WS_CHILD without parent created\n" );
101
102     /* desktop window */
103     check_parents( desktop, 0, 0, 0, 0, 0, 0 );
104     style = GetWindowLongA( desktop, GWL_STYLE );
105     ok( !SetWindowLongA( desktop, GWL_STYLE, WS_POPUP ), "Set GWL_STYLE on desktop succeeded\n" );
106     ok( !SetWindowLongA( desktop, GWL_STYLE, 0 ), "Set GWL_STYLE on desktop succeeded\n" );
107     ok( GetWindowLongA( desktop, GWL_STYLE ) == style, "Desktop style changed\n" );
108
109     /* normal child window */
110     test = create_tool_window( WS_CHILD, hwndMain );
111     trace( "created child %p\n", test );
112     check_parents( test, hwndMain, hwndMain, hwndMain, 0, hwndMain, hwndMain );
113     SetWindowLongA( test, GWL_STYLE, 0 );
114     check_parents( test, hwndMain, hwndMain, 0, 0, hwndMain, test );
115     SetWindowLongA( test, GWL_STYLE, WS_POPUP );
116     check_parents( test, hwndMain, hwndMain, 0, 0, hwndMain, test );
117     SetWindowLongA( test, GWL_STYLE, WS_POPUP|WS_CHILD );
118     check_parents( test, hwndMain, hwndMain, 0, 0, hwndMain, test );
119     SetWindowLongA( test, GWL_STYLE, WS_CHILD );
120     DestroyWindow( test );
121
122     /* normal child window with WS_MAXIMIZE */
123     test = create_tool_window( WS_CHILD | WS_MAXIMIZE, hwndMain );
124     DestroyWindow( test );
125
126     /* normal child window with WS_THICKFRAME */
127     test = create_tool_window( WS_CHILD | WS_THICKFRAME, hwndMain );
128     DestroyWindow( test );
129
130     /* popup window with WS_THICKFRAME */
131     test = create_tool_window( WS_POPUP | WS_THICKFRAME, hwndMain );
132     DestroyWindow( test );
133
134     /* child of desktop */
135     test = create_tool_window( WS_CHILD, desktop );
136     trace( "created child of desktop %p\n", test );
137     check_parents( test, desktop, 0, desktop, 0, test, desktop );
138     SetWindowLongA( test, GWL_STYLE, WS_POPUP );
139     check_parents( test, desktop, 0, 0, 0, test, test );
140     SetWindowLongA( test, GWL_STYLE, 0 );
141     check_parents( test, desktop, 0, 0, 0, test, test );
142     DestroyWindow( test );
143
144     /* child of desktop with WS_MAXIMIZE */
145     test = create_tool_window( WS_CHILD | WS_MAXIMIZE, desktop );
146     DestroyWindow( test );
147
148     /* child of desktop with WS_MINIMIZE */
149     test = create_tool_window( WS_CHILD | WS_MINIMIZE, desktop );
150     DestroyWindow( test );
151
152     /* child of child */
153     test = create_tool_window( WS_CHILD, child );
154     trace( "created child of child %p\n", test );
155     check_parents( test, child, child, child, 0, hwndMain, hwndMain );
156     SetWindowLongA( test, GWL_STYLE, 0 );
157     check_parents( test, child, child, 0, 0, hwndMain, test );
158     SetWindowLongA( test, GWL_STYLE, WS_POPUP );
159     check_parents( test, child, child, 0, 0, hwndMain, test );
160     DestroyWindow( test );
161
162     /* child of child with WS_MAXIMIZE */
163     test = create_tool_window( WS_CHILD | WS_MAXIMIZE, child );
164     DestroyWindow( test );
165
166     /* child of child with WS_MINIMIZE */
167     test = create_tool_window( WS_CHILD | WS_MINIMIZE, child );
168     DestroyWindow( test );
169
170     /* not owned top-level window */
171     test = create_tool_window( 0, 0 );
172     trace( "created top-level %p\n", test );
173     check_parents( test, desktop, 0, 0, 0, test, test );
174     SetWindowLongA( test, GWL_STYLE, WS_POPUP );
175     check_parents( test, desktop, 0, 0, 0, test, test );
176     SetWindowLongA( test, GWL_STYLE, WS_CHILD );
177     check_parents( test, desktop, 0, desktop, 0, test, desktop );
178     DestroyWindow( test );
179
180     /* not owned top-level window with WS_MAXIMIZE */
181     test = create_tool_window( WS_MAXIMIZE, 0 );
182     DestroyWindow( test );
183
184     /* owned top-level window */
185     test = create_tool_window( 0, hwndMain );
186     trace( "created owned top-level %p\n", test );
187     check_parents( test, desktop, hwndMain, 0, hwndMain, test, test );
188     SetWindowLongA( test, GWL_STYLE, WS_POPUP );
189     check_parents( test, desktop, hwndMain, hwndMain, hwndMain, test, hwndMain );
190     SetWindowLongA( test, GWL_STYLE, WS_CHILD );
191     check_parents( test, desktop, hwndMain, desktop, hwndMain, test, desktop );
192     DestroyWindow( test );
193
194     /* owned top-level window with WS_MAXIMIZE */
195     test = create_tool_window( WS_MAXIMIZE, hwndMain );
196     DestroyWindow( test );
197
198     /* not owned popup */
199     test = create_tool_window( WS_POPUP, 0 );
200     trace( "created popup %p\n", test );
201     check_parents( test, desktop, 0, 0, 0, test, test );
202     SetWindowLongA( test, GWL_STYLE, WS_CHILD );
203     check_parents( test, desktop, 0, desktop, 0, test, desktop );
204     SetWindowLongA( test, GWL_STYLE, 0 );
205     check_parents( test, desktop, 0, 0, 0, test, test );
206     DestroyWindow( test );
207
208     /* not owned popup with WS_MAXIMIZE */
209     test = create_tool_window( WS_POPUP | WS_MAXIMIZE, 0 );
210     DestroyWindow( test );
211
212     /* owned popup */
213     test = create_tool_window( WS_POPUP, hwndMain );
214     trace( "created owned popup %p\n", test );
215     check_parents( test, desktop, hwndMain, hwndMain, hwndMain, test, hwndMain );
216     SetWindowLongA( test, GWL_STYLE, WS_CHILD );
217     check_parents( test, desktop, hwndMain, desktop, hwndMain, test, desktop );
218     SetWindowLongA( test, GWL_STYLE, 0 );
219     check_parents( test, desktop, hwndMain, 0, hwndMain, test, test );
220     DestroyWindow( test );
221
222     /* owned popup with WS_MAXIMIZE */
223     test = create_tool_window( WS_POPUP | WS_MAXIMIZE, hwndMain );
224     DestroyWindow( test );
225
226     /* top-level window owned by child (same as owned by top-level) */
227     test = create_tool_window( 0, child );
228     trace( "created top-level owned by child %p\n", test );
229     check_parents( test, desktop, hwndMain, 0, hwndMain, test, test );
230     DestroyWindow( test );
231
232     /* top-level window owned by child (same as owned by top-level) with WS_MAXIMIZE */
233     test = create_tool_window( WS_MAXIMIZE, child );
234     DestroyWindow( test );
235
236     /* popup owned by desktop (same as not owned) */
237     test = create_tool_window( WS_POPUP, desktop );
238     trace( "created popup owned by desktop %p\n", test );
239     check_parents( test, desktop, 0, 0, 0, test, test );
240     DestroyWindow( test );
241
242     /* popup owned by desktop (same as not owned) with WS_MAXIMIZE */
243     test = create_tool_window( WS_POPUP | WS_MAXIMIZE, desktop );
244     DestroyWindow( test );
245
246     /* popup owned by child (same as owned by top-level) */
247     test = create_tool_window( WS_POPUP, child );
248     trace( "created popup owned by child %p\n", test );
249     check_parents( test, desktop, hwndMain, hwndMain, hwndMain, test, hwndMain );
250     DestroyWindow( test );
251
252     /* popup owned by child (same as owned by top-level) with WS_MAXIMIZE */
253     test = create_tool_window( WS_POPUP | WS_MAXIMIZE, child );
254     DestroyWindow( test );
255
256     /* not owned popup with WS_CHILD (same as WS_POPUP only) */
257     test = create_tool_window( WS_POPUP | WS_CHILD, 0 );
258     trace( "created WS_CHILD popup %p\n", test );
259     check_parents( test, desktop, 0, 0, 0, test, test );
260     DestroyWindow( test );
261
262     /* not owned popup with WS_CHILD | WS_MAXIMIZE (same as WS_POPUP only) */
263     test = create_tool_window( WS_POPUP | WS_CHILD | WS_MAXIMIZE, 0 );
264     DestroyWindow( test );
265
266     /* owned popup with WS_CHILD (same as WS_POPUP only) */
267     test = create_tool_window( WS_POPUP | WS_CHILD, hwndMain );
268     trace( "created owned WS_CHILD popup %p\n", test );
269     check_parents( test, desktop, hwndMain, hwndMain, hwndMain, test, hwndMain );
270     DestroyWindow( test );
271
272     /* owned popup with WS_CHILD (same as WS_POPUP only) with WS_MAXIMIZE */
273     test = create_tool_window( WS_POPUP | WS_CHILD | WS_MAXIMIZE, hwndMain );
274     DestroyWindow( test );
275
276     /******************** parent changes *************************/
277     trace( "testing parent changes\n" );
278
279     /* desktop window */
280     check_parents( desktop, 0, 0, 0, 0, 0, 0 );
281 #if 0 /* this test succeeds on NT but crashes on win9x systems */
282     ret = (HWND)SetWindowLongA( test, GWL_HWNDPARENT, (LONG_PTR)hwndMain2 );
283     ok( !ret, "Set GWL_HWNDPARENT succeeded on desktop\n" );
284     check_parents( desktop, 0, 0, 0, 0, 0, 0 );
285     ok( !SetParent( desktop, hwndMain ), "SetParent succeeded on desktop\n" );
286     check_parents( desktop, 0, 0, 0, 0, 0, 0 );
287 #endif
288     /* normal child window */
289     test = create_tool_window( WS_CHILD, hwndMain );
290     trace( "created child %p\n", test );
291
292     ret = (HWND)SetWindowLongA( test, GWL_HWNDPARENT, (LONG_PTR)hwndMain2 );
293     ok( ret == hwndMain, "GWL_HWNDPARENT return value %p expected %p\n", ret, hwndMain );
294     check_parents( test, hwndMain2, hwndMain2, hwndMain2, 0, hwndMain2, hwndMain2 );
295
296     ret = (HWND)SetWindowLongA( test, GWL_HWNDPARENT, (LONG_PTR)child );
297     ok( ret == hwndMain2, "GWL_HWNDPARENT return value %p expected %p\n", ret, hwndMain2 );
298     check_parents( test, child, child, child, 0, hwndMain, hwndMain );
299
300     ret = (HWND)SetWindowLongA( test, GWL_HWNDPARENT, (LONG_PTR)desktop );
301     ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
302     check_parents( test, desktop, 0, desktop, 0, test, desktop );
303
304     /* window is now child of desktop so GWL_HWNDPARENT changes owner from now on */
305     ret = (HWND)SetWindowLongA( test, GWL_HWNDPARENT, (LONG_PTR)child );
306     ok( ret == 0, "GWL_HWNDPARENT return value %p expected 0\n", ret );
307     check_parents( test, desktop, child, desktop, child, test, desktop );
308
309     ret = (HWND)SetWindowLongA( test, GWL_HWNDPARENT, 0 );
310     ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
311     check_parents( test, desktop, 0, desktop, 0, test, desktop );
312     DestroyWindow( test );
313
314     /* not owned top-level window */
315     test = create_tool_window( 0, 0 );
316     trace( "created top-level %p\n", test );
317
318     ret = (HWND)SetWindowLongA( test, GWL_HWNDPARENT, (LONG_PTR)hwndMain2 );
319     ok( ret == 0, "GWL_HWNDPARENT return value %p expected 0\n", ret );
320     check_parents( test, desktop, hwndMain2, 0, hwndMain2, test, test );
321
322     ret = (HWND)SetWindowLongA( test, GWL_HWNDPARENT, (LONG_PTR)child );
323     ok( ret == hwndMain2, "GWL_HWNDPARENT return value %p expected %p\n", ret, hwndMain2 );
324     check_parents( test, desktop, child, 0, child, test, test );
325
326     ret = (HWND)SetWindowLongA( test, GWL_HWNDPARENT, 0 );
327     ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
328     check_parents( test, desktop, 0, 0, 0, test, test );
329     DestroyWindow( test );
330
331     /* not owned popup */
332     test = create_tool_window( WS_POPUP, 0 );
333     trace( "created popup %p\n", test );
334
335     ret = (HWND)SetWindowLongA( test, GWL_HWNDPARENT, (LONG_PTR)hwndMain2 );
336     ok( ret == 0, "GWL_HWNDPARENT return value %p expected 0\n", ret );
337     check_parents( test, desktop, hwndMain2, hwndMain2, hwndMain2, test, hwndMain2 );
338
339     ret = (HWND)SetWindowLongA( test, GWL_HWNDPARENT, (LONG_PTR)child );
340     ok( ret == hwndMain2, "GWL_HWNDPARENT return value %p expected %p\n", ret, hwndMain2 );
341     check_parents( test, desktop, child, child, child, test, hwndMain );
342
343     ret = (HWND)SetWindowLongA( test, GWL_HWNDPARENT, 0 );
344     ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
345     check_parents( test, desktop, 0, 0, 0, test, test );
346     DestroyWindow( test );
347
348     /* normal child window */
349     test = create_tool_window( WS_CHILD, hwndMain );
350     trace( "created child %p\n", test );
351
352     ret = SetParent( test, desktop );
353     ok( ret == hwndMain, "SetParent return value %p expected %p\n", ret, hwndMain );
354     check_parents( test, desktop, 0, desktop, 0, test, desktop );
355
356     ret = SetParent( test, child );
357     ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
358     check_parents( test, child, child, child, 0, hwndMain, hwndMain );
359
360     ret = SetParent( test, hwndMain2 );
361     ok( ret == child, "SetParent return value %p expected %p\n", ret, child );
362     check_parents( test, hwndMain2, hwndMain2, hwndMain2, 0, hwndMain2, hwndMain2 );
363     DestroyWindow( test );
364
365     /* not owned top-level window */
366     test = create_tool_window( 0, 0 );
367     trace( "created top-level %p\n", test );
368
369     ret = SetParent( test, child );
370     ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
371     check_parents( test, child, child, 0, 0, hwndMain, test );
372     DestroyWindow( test );
373
374     /* owned popup */
375     test = create_tool_window( WS_POPUP, hwndMain2 );
376     trace( "created owned popup %p\n", test );
377
378     ret = SetParent( test, child );
379     ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
380     check_parents( test, child, child, hwndMain2, hwndMain2, hwndMain, hwndMain2 );
381
382     ret = (HWND)SetWindowLongA( test, GWL_HWNDPARENT, (ULONG_PTR)hwndMain );
383     ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
384     check_parents( test, hwndMain, hwndMain, hwndMain2, hwndMain2, hwndMain, hwndMain2 );
385     DestroyWindow( test );
386
387     /**************** test owner destruction *******************/
388
389     /* owned child popup */
390     owner = create_tool_window( 0, 0 );
391     test = create_tool_window( WS_POPUP, owner );
392     trace( "created owner %p and popup %p\n", owner, test );
393     ret = SetParent( test, child );
394     ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
395     check_parents( test, child, child, owner, owner, hwndMain, owner );
396     /* window is now child of 'child' but owned by 'owner' */
397     DestroyWindow( owner );
398     ok( IsWindow(test), "Window %p destroyed by owner destruction\n", test );
399     /* Win98 doesn't pass this test. It doesn't allow a destroyed owner,
400      * while Win95, Win2k, WinXP do.
401      */
402     /*check_parents( test, child, child, owner, owner, hwndMain, owner );*/
403     ok( !IsWindow(owner), "Owner %p not destroyed\n", owner );
404     DestroyWindow(test);
405
406     /* owned top-level popup */
407     owner = create_tool_window( 0, 0 );
408     test = create_tool_window( WS_POPUP, owner );
409     trace( "created owner %p and popup %p\n", owner, test );
410     check_parents( test, desktop, owner, owner, owner, test, owner );
411     DestroyWindow( owner );
412     ok( !IsWindow(test), "Window %p not destroyed by owner destruction\n", test );
413
414     /* top-level popup owned by child */
415     owner = create_tool_window( WS_CHILD, hwndMain2 );
416     test = create_tool_window( WS_POPUP, 0 );
417     trace( "created owner %p and popup %p\n", owner, test );
418     ret = (HWND)SetWindowLongA( test, GWL_HWNDPARENT, (ULONG_PTR)owner );
419     ok( ret == 0, "GWL_HWNDPARENT return value %p expected 0\n", ret );
420     check_parents( test, desktop, owner, owner, owner, test, hwndMain2 );
421     DestroyWindow( owner );
422     ok( IsWindow(test), "Window %p destroyed by owner destruction\n", test );
423     ok( !IsWindow(owner), "Owner %p not destroyed\n", owner );
424     /* Win98 doesn't pass this test. It doesn't allow a destroyed owner,
425      * while Win95, Win2k, WinXP do.
426      */
427     /*check_parents( test, desktop, owner, owner, owner, test, owner );*/
428     DestroyWindow(test);
429
430     /* final cleanup */
431     DestroyWindow(child);
432 }
433
434
435 static LRESULT WINAPI main_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
436 {
437     switch (msg)
438     {
439         case WM_GETMINMAXINFO:
440         {
441             MINMAXINFO* minmax = (MINMAXINFO *)lparam;
442
443             trace("hwnd %p, WM_GETMINMAXINFO, %08x, %08lx\n", hwnd, wparam, lparam);
444             trace("ptReserved (%ld,%ld), ptMaxSize (%ld,%ld), ptMaxPosition (%ld,%ld)\n"
445                   "       ptMinTrackSize (%ld,%ld), ptMaxTrackSize (%ld,%ld)\n",
446                   minmax->ptReserved.x, minmax->ptReserved.y,
447                   minmax->ptMaxSize.x, minmax->ptMaxSize.y,
448                   minmax->ptMaxPosition.x, minmax->ptMaxPosition.y,
449                   minmax->ptMinTrackSize.x, minmax->ptMinTrackSize.y,
450                   minmax->ptMaxTrackSize.x, minmax->ptMaxTrackSize.y);
451             SetWindowLongA(hwnd, GWL_USERDATA, 0x20031021);
452             break;
453         }
454         case WM_WINDOWPOSCHANGING:
455         {
456             BOOL is_win9x = GetWindowLongW(hwnd, GWL_WNDPROC) == 0;
457             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
458             trace("main: WM_WINDOWPOSCHANGING\n");
459             trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
460                    winpos->hwnd, winpos->hwndInsertAfter,
461                    winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
462             if (!(winpos->flags & SWP_NOMOVE))
463             {
464                 ok(winpos->x >= -32768 && winpos->x <= 32767, "bad winpos->x %d\n", winpos->x);
465                 ok(winpos->y >= -32768 && winpos->y <= 32767, "bad winpos->y %d\n", winpos->y);
466             }
467             /* Win9x does not fixup cx/xy for WM_WINDOWPOSCHANGING */
468             if (!(winpos->flags & SWP_NOSIZE) && !is_win9x)
469             {
470                 ok(winpos->cx >= 0 && winpos->cx <= 32767, "bad winpos->cx %d\n", winpos->cx);
471                 ok(winpos->cy >= 0 && winpos->cy <= 32767, "bad winpos->cy %d\n", winpos->cy);
472             }
473             break;
474         }
475         case WM_WINDOWPOSCHANGED:
476         {
477             RECT rc1, rc2;
478             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
479             trace("main: WM_WINDOWPOSCHANGED\n");
480             trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
481                    winpos->hwnd, winpos->hwndInsertAfter,
482                    winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
483             ok(winpos->x >= -32768 && winpos->x <= 32767, "bad winpos->x %d\n", winpos->x);
484             ok(winpos->y >= -32768 && winpos->y <= 32767, "bad winpos->y %d\n", winpos->y);
485
486             ok(winpos->cx >= 0 && winpos->cx <= 32767, "bad winpos->cx %d\n", winpos->cx);
487             ok(winpos->cy >= 0 && winpos->cy <= 32767, "bad winpos->cy %d\n", winpos->cy);
488
489             GetWindowRect(hwnd, &rc1);
490             trace("window: (%ld,%ld)-(%ld,%ld)\n", rc1.left, rc1.top, rc1.right, rc1.bottom);
491             SetRect(&rc2, winpos->x, winpos->y, winpos->x + winpos->cx, winpos->y + winpos->cy);
492             /* note: winpos coordinates are relative to parent */
493             MapWindowPoints(GetParent(hwnd), 0, (LPPOINT)&rc2, 2);
494             trace("pos: (%ld,%ld)-(%ld,%ld)\n", rc2.left, rc2.top, rc2.right, rc2.bottom);
495 #if 0 /* Uncomment this once the test succeeds in all cases */
496             ok(EqualRect(&rc1, &rc2), "rects do not match\n");
497 #endif
498
499             GetClientRect(hwnd, &rc2);
500             DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc1);
501             MapWindowPoints(0, hwnd, (LPPOINT)&rc1, 2);
502             ok(EqualRect(&rc1, &rc2), "rects do not match\n");
503             break;
504         }
505         case WM_NCCREATE:
506         {
507             BOOL got_getminmaxinfo = GetWindowLongA(hwnd, GWL_USERDATA) == 0x20031021;
508             CREATESTRUCTA *cs = (CREATESTRUCTA *)lparam;
509
510             trace("WM_NCCREATE: hwnd %p, parent %p, style %08lx\n", hwnd, cs->hwndParent, cs->style);
511             if (got_getminmaxinfo)
512                 trace("%p got WM_GETMINMAXINFO\n", hwnd);
513
514             if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
515                 ok(got_getminmaxinfo, "main: WM_GETMINMAXINFO should have been received before WM_NCCREATE\n");
516             else
517                 ok(!got_getminmaxinfo, "main: WM_GETMINMAXINFO should NOT have been received before WM_NCCREATE\n");
518             break;
519         }
520     }
521
522     return DefWindowProcA(hwnd, msg, wparam, lparam);
523 }
524
525 static LRESULT WINAPI tool_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
526 {
527     switch (msg)
528     {
529         case WM_GETMINMAXINFO:
530         {
531             MINMAXINFO* minmax = (MINMAXINFO *)lparam;
532
533             trace("hwnd %p, WM_GETMINMAXINFO, %08x, %08lx\n", hwnd, wparam, lparam);
534             trace("ptReserved (%ld,%ld), ptMaxSize (%ld,%ld), ptMaxPosition (%ld,%ld)\n"
535                   "       ptMinTrackSize (%ld,%ld), ptMaxTrackSize (%ld,%ld)\n",
536                   minmax->ptReserved.x, minmax->ptReserved.y,
537                   minmax->ptMaxSize.x, minmax->ptMaxSize.y,
538                   minmax->ptMaxPosition.x, minmax->ptMaxPosition.y,
539                   minmax->ptMinTrackSize.x, minmax->ptMinTrackSize.y,
540                   minmax->ptMaxTrackSize.x, minmax->ptMaxTrackSize.y);
541             SetWindowLongA(hwnd, GWL_USERDATA, 0x20031021);
542             break;
543         }
544         case WM_NCCREATE:
545         {
546             BOOL got_getminmaxinfo = GetWindowLongA(hwnd, GWL_USERDATA) == 0x20031021;
547             CREATESTRUCTA *cs = (CREATESTRUCTA *)lparam;
548
549             trace("WM_NCCREATE: hwnd %p, parent %p, style %08lx\n", hwnd, cs->hwndParent, cs->style);
550             if (got_getminmaxinfo)
551                 trace("%p got WM_GETMINMAXINFO\n", hwnd);
552
553             if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
554                 ok(got_getminmaxinfo, "tool: WM_GETMINMAXINFO should have been received before WM_NCCREATE\n");
555             else
556                 ok(!got_getminmaxinfo, "tool: WM_GETMINMAXINFO should NOT have been received before WM_NCCREATE\n");
557             break;
558         }
559     }
560
561     return DefWindowProcA(hwnd, msg, wparam, lparam);
562 }
563
564 static BOOL RegisterWindowClasses(void)
565 {
566     WNDCLASSA cls;
567
568     cls.style = 0;
569     cls.lpfnWndProc = main_window_procA;
570     cls.cbClsExtra = 0;
571     cls.cbWndExtra = 0;
572     cls.hInstance = GetModuleHandleA(0);
573     cls.hIcon = 0;
574     cls.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
575     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
576     cls.lpszMenuName = NULL;
577     cls.lpszClassName = "MainWindowClass";
578
579     if(!RegisterClassA(&cls)) return FALSE;
580
581     cls.style = 0;
582     cls.lpfnWndProc = tool_window_procA;
583     cls.cbClsExtra = 0;
584     cls.cbWndExtra = 0;
585     cls.hInstance = GetModuleHandleA(0);
586     cls.hIcon = 0;
587     cls.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
588     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
589     cls.lpszMenuName = NULL;
590     cls.lpszClassName = "ToolWindowClass";
591
592     if(!RegisterClassA(&cls)) return FALSE;
593
594     return TRUE;
595 }
596
597 static void verify_window_info(HWND hwnd, const WINDOWINFO *info, BOOL test_borders)
598 {
599     RECT rcWindow, rcClient;
600     UINT border;
601     DWORD status;
602
603     ok(IsWindow(hwnd), "bad window handle\n");
604
605     GetWindowRect(hwnd, &rcWindow);
606     ok(EqualRect(&rcWindow, &info->rcWindow), "wrong rcWindow\n");
607
608     GetClientRect(hwnd, &rcClient);
609     /* translate to screen coordinates */
610     MapWindowPoints(hwnd, 0, (LPPOINT)&rcClient, 2);
611     ok(EqualRect(&rcClient, &info->rcClient), "wrong rcClient\n");
612
613     ok(info->dwStyle == (DWORD)GetWindowLongA(hwnd, GWL_STYLE), "wrong dwStyle\n");
614     ok(info->dwExStyle == (DWORD)GetWindowLongA(hwnd, GWL_EXSTYLE), "wrong dwExStyle\n");
615     status = (GetActiveWindow() == hwnd) ? WS_ACTIVECAPTION : 0;
616     ok(info->dwWindowStatus == status, "wrong dwWindowStatus\n");
617
618     if (test_borders && !IsRectEmpty(&rcWindow))
619     {
620         trace("rcWindow: %ld,%ld - %ld,%ld\n", rcWindow.left, rcWindow.top, rcWindow.right, rcWindow.bottom);
621         trace("rcClient: %ld,%ld - %ld,%ld\n", rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
622
623         ok(info->cxWindowBorders == (unsigned)(rcClient.left - rcWindow.left),
624             "wrong cxWindowBorders %d != %ld\n", info->cxWindowBorders, rcClient.left - rcWindow.left);
625         border = min(rcWindow.bottom - rcClient.bottom, rcClient.top - rcWindow.top);
626         ok(info->cyWindowBorders == border,
627            "wrong cyWindowBorders %d != %d\n", info->cyWindowBorders, border);
628     }
629
630     ok(info->atomWindowType == GetClassLongA(hwnd, GCW_ATOM), "wrong atomWindowType\n");
631     ok(info->wCreatorVersion == 0x0400, "wrong wCreatorVersion %04x\n", info->wCreatorVersion);
632 }
633
634 static void test_nonclient_area(HWND hwnd)
635 {
636     DWORD style, exstyle;
637     RECT rc_window, rc_client, rc;
638     BOOL menu;
639
640     style = GetWindowLongA(hwnd, GWL_STYLE);
641     exstyle = GetWindowLongA(hwnd, GWL_EXSTYLE);
642     menu = !(style & WS_CHILD) && GetMenu(hwnd) != 0;
643
644     GetWindowRect(hwnd, &rc_window);
645     trace("window: (%ld,%ld)-(%ld,%ld)\n", rc_window.left, rc_window.top, rc_window.right, rc_window.bottom);
646     GetClientRect(hwnd, &rc_client);
647     trace("client: (%ld,%ld)-(%ld,%ld)\n", rc_client.left, rc_client.top, rc_client.right, rc_client.bottom);
648
649     /* avoid some cases when things go wrong */
650     if (IsRectEmpty(&rc_window) || IsRectEmpty(&rc_client) ||
651         rc_window.right > 32768 || rc_window.bottom > 32768) return;
652
653     CopyRect(&rc, &rc_client);
654     MapWindowPoints(hwnd, 0, (LPPOINT)&rc, 2);
655     AdjustWindowRectEx(&rc, style, menu, exstyle);
656     trace("calc window: (%ld,%ld)-(%ld,%ld)\n", rc.left, rc.top, rc.right, rc.bottom);
657 #if 0 /* Uncomment this once the test succeeds in all cases */
658     ok(EqualRect(&rc, &rc_window), "window rect does not match\n");
659 #endif
660
661     CopyRect(&rc, &rc_window);
662     DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc);
663     MapWindowPoints(0, hwnd, (LPPOINT)&rc, 2);
664     trace("calc client: (%ld,%ld)-(%ld,%ld)\n", rc.left, rc.top, rc.right, rc.bottom);
665 #if 0 /* Uncomment this once the test succeeds in all cases */
666     ok(EqualRect(&rc, &rc_client), "client rect does not match\n");
667 #endif
668
669     /* and now test AdjustWindowRectEx and WM_NCCALCSIZE on synthetic data */
670     SetRect(&rc_client, 0, 0, 250, 150);
671     CopyRect(&rc_window, &rc_client);
672     MapWindowPoints(hwnd, 0, (LPPOINT)&rc_window, 2);
673     AdjustWindowRectEx(&rc_window, style, menu, exstyle);
674     trace("calc window: (%ld,%ld)-(%ld,%ld)\n",
675         rc_window.left, rc_window.top, rc_window.right, rc_window.bottom);
676
677     CopyRect(&rc, &rc_window);
678     DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc);
679     MapWindowPoints(0, hwnd, (LPPOINT)&rc, 2);
680     trace("calc client: (%ld,%ld)-(%ld,%ld)\n", rc.left, rc.top, rc.right, rc.bottom);
681 #if 0 /* Uncomment this once the test succeeds in all cases */
682     ok(EqualRect(&rc, &rc_client), "client rect does not match\n");
683 #endif
684 }
685
686 static LRESULT CALLBACK cbt_hook_proc(int nCode, WPARAM wParam, LPARAM lParam) 
687
688     static const char *CBT_code_name[10] = {
689         "HCBT_MOVESIZE",
690         "HCBT_MINMAX",
691         "HCBT_QS",
692         "HCBT_CREATEWND",
693         "HCBT_DESTROYWND",
694         "HCBT_ACTIVATE",
695         "HCBT_CLICKSKIPPED",
696         "HCBT_KEYSKIPPED",
697         "HCBT_SYSCOMMAND",
698         "HCBT_SETFOCUS" };
699     const char *code_name = (nCode >= 0 && nCode <= HCBT_SETFOCUS) ? CBT_code_name[nCode] : "Unknown";
700
701     trace("CBT: %d (%s), %08x, %08lx\n", nCode, code_name, wParam, lParam);
702
703     /* on HCBT_DESTROYWND window state is undefined */
704     if (nCode != HCBT_DESTROYWND && wParam)
705     {
706         BOOL is_win9x = GetWindowLongW((HWND)wParam, GWL_WNDPROC) == 0;
707         if (is_win9x && nCode == HCBT_CREATEWND)
708             /* Win9x doesn't like WM_NCCALCSIZE with synthetic data and crashes */;
709         else
710             test_nonclient_area((HWND)wParam);
711
712         if (pGetWindowInfo)
713         {
714             WINDOWINFO info;
715
716             /* Win98 actually does check the info.cbSize and doesn't allow
717              * it to be anything except sizeof(WINDOWINFO), while Win95, Win2k,
718              * WinXP do not check it at all.
719              */
720             info.cbSize = sizeof(WINDOWINFO);
721             ok(pGetWindowInfo((HWND)wParam, &info), "GetWindowInfo should not fail\n");
722             /* win2k SP4 returns broken border info if GetWindowInfo
723              * is being called from HCBT_DESTROYWND or HCBT_MINMAX hook proc.
724              */
725             verify_window_info((HWND)wParam, &info, nCode != HCBT_MINMAX);
726         }
727     }
728
729     switch (nCode)
730     {
731         case HCBT_CREATEWND:
732         {
733 #if 0 /* Uncomment this once the test succeeds in all cases */
734             static const RECT rc_null;
735             RECT rc;
736 #endif
737             LONG style;
738             CBT_CREATEWNDA *createwnd = (CBT_CREATEWNDA *)lParam;
739             trace("HCBT_CREATEWND: hwnd %p, parent %p, style %08lx\n",
740                   (HWND)wParam, createwnd->lpcs->hwndParent, createwnd->lpcs->style);
741             ok(createwnd->hwndInsertAfter == HWND_TOP, "hwndInsertAfter should be always HWND_TOP\n");
742
743             /* WS_VISIBLE should be turned off yet */
744             style = createwnd->lpcs->style & ~WS_VISIBLE;
745             ok(style == GetWindowLongA((HWND)wParam, GWL_STYLE),
746                 "style of hwnd and style in the CREATESTRUCT do not match: %08lx != %08lx\n",
747                 GetWindowLongA((HWND)wParam, GWL_STYLE), style);
748
749 #if 0 /* Uncomment this once the test succeeds in all cases */
750             if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD)
751             {
752                 ok(GetParent((HWND)wParam) == hwndMessage,
753                    "wrong result from GetParent %p: message window %p\n",
754                    GetParent((HWND)wParam), hwndMessage);
755             }
756             else
757                 ok(!GetParent((HWND)wParam), "GetParent should return 0 at this point\n");
758
759             ok(!GetWindow((HWND)wParam, GW_OWNER), "GW_OWNER should be set to 0 at this point\n");
760 #endif
761 #if 0       /* while NT assigns GW_HWNDFIRST/LAST some values at this point,
762              * Win9x still has them set to 0.
763              */
764             ok(GetWindow((HWND)wParam, GW_HWNDFIRST) != 0, "GW_HWNDFIRST should not be set to 0 at this point\n");
765             ok(GetWindow((HWND)wParam, GW_HWNDLAST) != 0, "GW_HWNDLAST should not be set to 0 at this point\n");
766 #endif
767             ok(!GetWindow((HWND)wParam, GW_HWNDPREV), "GW_HWNDPREV should be set to 0 at this point\n");
768             ok(!GetWindow((HWND)wParam, GW_HWNDNEXT), "GW_HWNDNEXT should be set to 0 at this point\n");
769
770 #if 0 /* Uncomment this once the test succeeds in all cases */
771             if (pGetAncestor)
772             {
773                 ok(pGetAncestor((HWND)wParam, GA_PARENT) == hwndMessage, "GA_PARENT should be set to hwndMessage at this point\n");
774                 ok(pGetAncestor((HWND)wParam, GA_ROOT) == (HWND)wParam,
775                    "GA_ROOT is set to %p, expected %p\n", pGetAncestor((HWND)wParam, GA_ROOT), (HWND)wParam);
776
777                 if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD)
778                     ok(pGetAncestor((HWND)wParam, GA_ROOTOWNER) == hwndMessage,
779                        "GA_ROOTOWNER should be set to hwndMessage at this point\n");
780                 else
781                     ok(pGetAncestor((HWND)wParam, GA_ROOTOWNER) == (HWND)wParam,
782                        "GA_ROOTOWNER is set to %p, expected %p\n", pGetAncestor((HWND)wParam, GA_ROOTOWNER), (HWND)wParam);
783             }
784
785             ok(GetWindowRect((HWND)wParam, &rc), "GetWindowRect failed\n");
786             ok(EqualRect(&rc, &rc_null), "window rect should be set to 0 HCBT_CREATEWND\n");
787             ok(GetClientRect((HWND)wParam, &rc), "GetClientRect failed\n");
788             ok(EqualRect(&rc, &rc_null), "client rect should be set to 0 on HCBT_CREATEWND\n");
789 #endif
790             break;
791         }
792     }
793
794     return CallNextHookEx(hhook, nCode, wParam, lParam);
795 }
796
797 static void test_shell_window()
798 {
799     BOOL ret;
800     DWORD error;
801     HMODULE hinst, hUser32;
802     BOOL (WINAPI*SetShellWindow)(HWND);
803     BOOL (WINAPI*SetShellWindowEx)(HWND, HWND);
804     HWND hwnd1, hwnd2, hwnd3, hwnd4, hwnd5;
805     HWND shellWindow, nextWnd;
806
807     shellWindow = GetShellWindow();
808     hinst = GetModuleHandle(0);
809     hUser32 = GetModuleHandleA("user32");
810
811     SetShellWindow = (void *)GetProcAddress(hUser32, "SetShellWindow");
812     SetShellWindowEx = (void *)GetProcAddress(hUser32, "SetShellWindowEx");
813
814     trace("previous shell window: %p\n", shellWindow);
815
816     if (shellWindow) {
817         ret = DestroyWindow(shellWindow);
818         error = GetLastError();
819
820         ok(!ret, "DestroyWindow(shellWindow)\n");
821         /* passes on Win XP, but not on Win98
822         ok(error==ERROR_ACCESS_DENIED, "ERROR_ACCESS_DENIED after DestroyWindow(shellWindow)\n"); */
823     }
824
825     hwnd1 = CreateWindowEx(0, TEXT("#32770"), TEXT("TEST1"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 100, 100, 300, 200, 0, 0, hinst, 0);
826     trace("created window 1: %p\n", hwnd1);
827
828     ret = SetShellWindow(hwnd1);
829     ok(ret, "first call to SetShellWindow(hwnd1)\n");
830     shellWindow = GetShellWindow();
831     ok(shellWindow==hwnd1, "wrong shell window: %p\n", shellWindow);
832
833     ret = SetShellWindow(hwnd1);
834     ok(!ret, "second call to SetShellWindow(hwnd1)\n");
835
836     ret = SetShellWindow(0);
837     error = GetLastError();
838     /* passes on Win XP, but not on Win98
839     ok(!ret, "reset shell window by SetShellWindow(0)\n");
840     ok(error==ERROR_INVALID_WINDOW_HANDLE, "ERROR_INVALID_WINDOW_HANDLE after SetShellWindow(0)\n"); */
841
842     ret = SetShellWindow(hwnd1);
843     /* passes on Win XP, but not on Win98
844     ok(!ret, "third call to SetShellWindow(hwnd1)\n"); */
845
846     todo_wine
847     {
848         SetWindowLong(hwnd1, GWL_EXSTYLE, GetWindowLong(hwnd1,GWL_EXSTYLE)|WS_EX_TOPMOST);
849         ret = GetWindowLong(hwnd1,GWL_EXSTYLE)&WS_EX_TOPMOST? TRUE: FALSE;
850         ok(!ret, "SetWindowExStyle(hwnd1, WS_EX_TOPMOST)\n");
851     }
852
853     ret = DestroyWindow(hwnd1);
854     ok(ret, "DestroyWindow(hwnd1)\n");
855
856     hwnd2 = CreateWindowEx(WS_EX_TOPMOST, TEXT("#32770"), TEXT("TEST2"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 150, 250, 300, 200, 0, 0, hinst, 0);
857     trace("created window 2: %p\n", hwnd2);
858     ret = SetShellWindow(hwnd2);
859     ok(!ret, "SetShellWindow(hwnd2) with WS_EX_TOPMOST\n");
860
861     hwnd3 = CreateWindowEx(0, TEXT("#32770"), TEXT("TEST3"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 200, 400, 300, 200, 0, 0, hinst, 0);
862     trace("created window 3: %p\n", hwnd3);
863
864     hwnd4 = CreateWindowEx(0, TEXT("#32770"), TEXT("TEST4"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 250, 500, 300, 200, 0, 0, hinst, 0);
865     trace("created window 4: %p\n", hwnd4);
866
867     nextWnd = GetWindow(hwnd4, GW_HWNDNEXT);
868     ok(nextWnd==hwnd3, "wrong next window for hwnd4: %p - expected hwnd3\n", nextWnd);
869
870     ret = SetShellWindow(hwnd4);
871     ok(ret, "SetShellWindow(hwnd4)\n");
872     shellWindow = GetShellWindow();
873     ok(shellWindow==hwnd4, "wrong shell window: %p - expected hwnd4\n", shellWindow);
874
875     nextWnd = GetWindow(hwnd4, GW_HWNDNEXT);
876     ok(nextWnd==0, "wrong next window for hwnd4: %p - expected 0\n", nextWnd);
877
878     ret = SetWindowPos(hwnd4, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
879     ok(ret, "SetWindowPos(hwnd4, HWND_TOPMOST)\n");
880
881     ret = SetWindowPos(hwnd4, hwnd3, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
882     ok(ret, "SetWindowPos(hwnd4, hwnd3\n");
883
884     ret = SetShellWindow(hwnd3);
885     ok(!ret, "SetShellWindow(hwnd3)\n");
886     shellWindow = GetShellWindow();
887     ok(shellWindow==hwnd4, "wrong shell window: %p - expected hwnd4\n", shellWindow);
888
889     hwnd5 = CreateWindowEx(0, TEXT("#32770"), TEXT("TEST5"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 300, 600, 300, 200, 0, 0, hinst, 0);
890     trace("created window 5: %p\n", hwnd5);
891     ret = SetWindowPos(hwnd4, hwnd5, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
892     ok(ret, "SetWindowPos(hwnd4, hwnd5)\n");
893
894     todo_wine
895     {
896         nextWnd = GetWindow(hwnd4, GW_HWNDNEXT);
897         ok(nextWnd==0, "wrong next window for hwnd4 after SetWindowPos(): %p - expected 0\n", nextWnd);
898     }
899
900     /* destroy test windows */
901     DestroyWindow(hwnd2);
902     DestroyWindow(hwnd3);
903     DestroyWindow(hwnd4);
904     DestroyWindow(hwnd5);
905 }
906
907 /************** MDI test ****************/
908
909 static const char mdi_lParam_test_message[] = "just a test string";
910
911 static void test_MDI_create(HWND parent, HWND mdi_client)
912 {
913     MDICREATESTRUCTA mdi_cs;
914     HWND mdi_child;
915     static const WCHAR classW[] = {'M','D','I','_','c','h','i','l','d','_','C','l','a','s','s','_','1',0};
916     static const WCHAR titleW[] = {'M','D','I',' ','c','h','i','l','d',0};
917     BOOL isWin9x = FALSE;
918
919     mdi_cs.szClass = "MDI_child_Class_1";
920     mdi_cs.szTitle = "MDI child";
921     mdi_cs.hOwner = GetModuleHandle(0);
922     mdi_cs.x = CW_USEDEFAULT;
923     mdi_cs.y = CW_USEDEFAULT;
924     mdi_cs.cx = CW_USEDEFAULT;
925     mdi_cs.cy = CW_USEDEFAULT;
926     mdi_cs.style = 0;
927     mdi_cs.lParam = (LPARAM)mdi_lParam_test_message;
928     mdi_child = (HWND)SendMessageA(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
929     ok(mdi_child != 0, "MDI child creation failed\n");
930     DestroyWindow(mdi_child);
931
932     mdi_cs.style = 0x7fffffff; /* without WS_POPUP */
933     mdi_child = (HWND)SendMessageA(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
934     ok(mdi_child != 0, "MDI child creation failed\n");
935     DestroyWindow(mdi_child);
936
937     mdi_cs.style = 0xffffffff; /* with WS_POPUP */
938     mdi_child = (HWND)SendMessageA(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
939     if (GetWindowLongA(mdi_client, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
940     {
941         ok(!mdi_child, "MDI child with WS_POPUP and with MDIS_ALLCHILDSTYLES should fail\n");
942         DestroyWindow(mdi_child);
943     }
944     else
945         ok(mdi_child != 0, "MDI child creation failed\n");
946
947     /* test MDICREATESTRUCT A<->W mapping */
948     /* MDICREATESTRUCTA and MDICREATESTRUCTW have the same layout */
949     mdi_cs.style = 0;
950     mdi_cs.szClass = (LPCSTR)classW;
951     mdi_cs.szTitle = (LPCSTR)titleW;
952     SetLastError(0xdeadbeef);
953     mdi_child = (HWND)SendMessageW(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
954     if (!mdi_child)
955     {
956         if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
957             isWin9x = TRUE;
958         else
959             ok(mdi_child != 0, "MDI child creation failed\n");
960     }
961     DestroyWindow(mdi_child);
962
963     mdi_child = CreateMDIWindowA("MDI_child_Class_1", "MDI child",
964                                  0,
965                                  CW_USEDEFAULT, CW_USEDEFAULT,
966                                  CW_USEDEFAULT, CW_USEDEFAULT,
967                                  mdi_client, GetModuleHandle(0),
968                                  (LPARAM)mdi_lParam_test_message);
969     ok(mdi_child != 0, "MDI child creation failed\n");
970     DestroyWindow(mdi_child);
971
972     mdi_child = CreateMDIWindowA("MDI_child_Class_1", "MDI child",
973                                  0x7fffffff, /* without WS_POPUP */
974                                  CW_USEDEFAULT, CW_USEDEFAULT,
975                                  CW_USEDEFAULT, CW_USEDEFAULT,
976                                  mdi_client, GetModuleHandle(0),
977                                  (LPARAM)mdi_lParam_test_message);
978     ok(mdi_child != 0, "MDI child creation failed\n");
979     DestroyWindow(mdi_child);
980
981     mdi_child = CreateMDIWindowA("MDI_child_Class_1", "MDI child",
982                                  0xffffffff, /* with WS_POPUP */
983                                  CW_USEDEFAULT, CW_USEDEFAULT,
984                                  CW_USEDEFAULT, CW_USEDEFAULT,
985                                  mdi_client, GetModuleHandle(0),
986                                  (LPARAM)mdi_lParam_test_message);
987     if (GetWindowLongA(mdi_client, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
988     {
989         ok(!mdi_child, "MDI child with WS_POPUP and with MDIS_ALLCHILDSTYLES should fail\n");
990         DestroyWindow(mdi_child);
991     }
992     else
993         ok(mdi_child != 0, "MDI child creation failed\n");
994
995     /* test MDICREATESTRUCT A<->W mapping */
996     SetLastError(0xdeadbeef);
997     mdi_child = CreateMDIWindowW(classW, titleW,
998                                  0,
999                                  CW_USEDEFAULT, CW_USEDEFAULT,
1000                                  CW_USEDEFAULT, CW_USEDEFAULT,
1001                                  mdi_client, GetModuleHandle(0),
1002                                  (LPARAM)mdi_lParam_test_message);
1003     if (!mdi_child)
1004     {
1005         if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1006             isWin9x = TRUE;
1007         else
1008             ok(mdi_child != 0, "MDI child creation failed\n");
1009     }
1010     DestroyWindow(mdi_child);
1011
1012     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_1", "MDI child",
1013                                 0,
1014                                 CW_USEDEFAULT, CW_USEDEFAULT,
1015                                 CW_USEDEFAULT, CW_USEDEFAULT,
1016                                 mdi_client, 0, GetModuleHandle(0),
1017                                 (LPVOID)mdi_lParam_test_message);
1018     ok(mdi_child != 0, "MDI child creation failed\n");
1019     DestroyWindow(mdi_child);
1020
1021     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_1", "MDI child",
1022                                 0x7fffffff, /* without WS_POPUP */
1023                                 CW_USEDEFAULT, CW_USEDEFAULT,
1024                                 CW_USEDEFAULT, CW_USEDEFAULT,
1025                                 mdi_client, 0, GetModuleHandle(0),
1026                                 (LPVOID)mdi_lParam_test_message);
1027     ok(mdi_child != 0, "MDI child creation failed\n");
1028     DestroyWindow(mdi_child);
1029
1030     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_1", "MDI child",
1031                                 0xffffffff, /* with WS_POPUP */
1032                                 CW_USEDEFAULT, CW_USEDEFAULT,
1033                                 CW_USEDEFAULT, CW_USEDEFAULT,
1034                                 mdi_client, 0, GetModuleHandle(0),
1035                                 (LPVOID)mdi_lParam_test_message);
1036     if (GetWindowLongA(mdi_client, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1037     {
1038         ok(!mdi_child, "MDI child with WS_POPUP and with MDIS_ALLCHILDSTYLES should fail\n");
1039         DestroyWindow(mdi_child);
1040     }
1041     else
1042         ok(mdi_child != 0, "MDI child creation failed\n");
1043
1044     /* test MDICREATESTRUCT A<->W mapping */
1045     SetLastError(0xdeadbeef);
1046     mdi_child = CreateWindowExW(WS_EX_MDICHILD, classW, titleW,
1047                                 0,
1048                                 CW_USEDEFAULT, CW_USEDEFAULT,
1049                                 CW_USEDEFAULT, CW_USEDEFAULT,
1050                                 mdi_client, 0, GetModuleHandle(0),
1051                                 (LPVOID)mdi_lParam_test_message);
1052     if (!mdi_child)
1053     {
1054         if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1055             isWin9x = TRUE;
1056         else
1057             ok(mdi_child != 0, "MDI child creation failed\n");
1058     }
1059     DestroyWindow(mdi_child);
1060
1061     /* This test fails on Win9x */
1062     if (!isWin9x)
1063     {
1064         mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_2", "MDI child",
1065                                 WS_CHILD,
1066                                 CW_USEDEFAULT, CW_USEDEFAULT,
1067                                 CW_USEDEFAULT, CW_USEDEFAULT,
1068                                 parent, 0, GetModuleHandle(0),
1069                                 (LPVOID)mdi_lParam_test_message);
1070         ok(!mdi_child, "WS_EX_MDICHILD with a not MDIClient parent should fail\n");
1071     }
1072
1073     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1074                                 WS_CHILD, /* without WS_POPUP */
1075                                 CW_USEDEFAULT, CW_USEDEFAULT,
1076                                 CW_USEDEFAULT, CW_USEDEFAULT,
1077                                 mdi_client, 0, GetModuleHandle(0),
1078                                 (LPVOID)mdi_lParam_test_message);
1079     ok(mdi_child != 0, "MDI child creation failed\n");
1080     DestroyWindow(mdi_child);
1081
1082     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1083                                 WS_CHILD | WS_POPUP, /* with WS_POPUP */
1084                                 CW_USEDEFAULT, CW_USEDEFAULT,
1085                                 CW_USEDEFAULT, CW_USEDEFAULT,
1086                                 mdi_client, 0, GetModuleHandle(0),
1087                                 (LPVOID)mdi_lParam_test_message);
1088     ok(mdi_child != 0, "MDI child creation failed\n");
1089     DestroyWindow(mdi_child);
1090
1091     /* maximized child */
1092     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1093                                 WS_CHILD | WS_MAXIMIZE,
1094                                 CW_USEDEFAULT, CW_USEDEFAULT,
1095                                 CW_USEDEFAULT, CW_USEDEFAULT,
1096                                 mdi_client, 0, GetModuleHandle(0),
1097                                 (LPVOID)mdi_lParam_test_message);
1098     ok(mdi_child != 0, "MDI child creation failed\n");
1099     DestroyWindow(mdi_child);
1100
1101     trace("Creating maximized child with a caption\n");
1102     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1103                                 WS_CHILD | WS_MAXIMIZE | WS_CAPTION,
1104                                 CW_USEDEFAULT, CW_USEDEFAULT,
1105                                 CW_USEDEFAULT, CW_USEDEFAULT,
1106                                 mdi_client, 0, GetModuleHandle(0),
1107                                 (LPVOID)mdi_lParam_test_message);
1108     ok(mdi_child != 0, "MDI child creation failed\n");
1109     DestroyWindow(mdi_child);
1110
1111     trace("Creating maximized child with a caption and a thick frame\n");
1112     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1113                                 WS_CHILD | WS_MAXIMIZE | WS_CAPTION | WS_THICKFRAME,
1114                                 CW_USEDEFAULT, CW_USEDEFAULT,
1115                                 CW_USEDEFAULT, CW_USEDEFAULT,
1116                                 mdi_client, 0, GetModuleHandle(0),
1117                                 (LPVOID)mdi_lParam_test_message);
1118     ok(mdi_child != 0, "MDI child creation failed\n");
1119     DestroyWindow(mdi_child);
1120 }
1121
1122 /**********************************************************************
1123  * MDI_ChildGetMinMaxInfo (copied from windows/mdi.c)
1124  *
1125  * Note: The rule here is that client rect of the maximized MDI child
1126  *       is equal to the client rect of the MDI client window.
1127  */
1128 static void MDI_ChildGetMinMaxInfo( HWND client, HWND hwnd, MINMAXINFO* lpMinMax )
1129 {
1130     RECT rect;
1131
1132     GetClientRect( client, &rect );
1133     AdjustWindowRectEx( &rect, GetWindowLongA( hwnd, GWL_STYLE ),
1134                         0, GetWindowLongA( hwnd, GWL_EXSTYLE ));
1135
1136     rect.right -= rect.left;
1137     rect.bottom -= rect.top;
1138     lpMinMax->ptMaxSize.x = rect.right;
1139     lpMinMax->ptMaxSize.y = rect.bottom;
1140
1141     lpMinMax->ptMaxPosition.x = rect.left;
1142     lpMinMax->ptMaxPosition.y = rect.top;
1143
1144     trace("max rect (%ld,%ld - %ld, %ld)\n",
1145            rect.left, rect.top, rect.right, rect.bottom);
1146 }
1147
1148 static LRESULT WINAPI mdi_child_wnd_proc_1(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1149 {
1150     switch (msg)
1151     {
1152         case WM_NCCREATE:
1153         case WM_CREATE:
1154         {
1155             CREATESTRUCTA *cs = (CREATESTRUCTA *)lparam;
1156             MDICREATESTRUCTA *mdi_cs = (MDICREATESTRUCTA *)cs->lpCreateParams;
1157
1158             ok(cs->dwExStyle & WS_EX_MDICHILD, "WS_EX_MDICHILD should be set\n");
1159             ok(mdi_cs->lParam == (LPARAM)mdi_lParam_test_message, "wrong mdi_cs->lParam\n");
1160
1161             ok(!lstrcmpA(cs->lpszClass, "MDI_child_Class_1"), "wrong class name\n");
1162             ok(!lstrcmpA(cs->lpszClass, mdi_cs->szClass), "class name does not match\n");
1163             ok(!lstrcmpA(cs->lpszName, "MDI child"), "wrong title\n");
1164             ok(!lstrcmpA(cs->lpszName, mdi_cs->szTitle), "title does not match\n");
1165             ok(cs->hInstance == mdi_cs->hOwner, "%p != %p\n", cs->hInstance, mdi_cs->hOwner);
1166
1167             /* MDICREATESTRUCT should have original values */
1168             ok(mdi_cs->style == 0 || mdi_cs->style == 0x7fffffff || mdi_cs->style == 0xffffffff,
1169                 "mdi_cs->style does not match (%08lx)\n", mdi_cs->style);
1170             ok(mdi_cs->x == CW_USEDEFAULT, "%d != CW_USEDEFAULT\n", mdi_cs->x);
1171             ok(mdi_cs->y == CW_USEDEFAULT, "%d != CW_USEDEFAULT\n", mdi_cs->y);
1172             ok(mdi_cs->cx == CW_USEDEFAULT, "%d != CW_USEDEFAULT\n", mdi_cs->cx);
1173             ok(mdi_cs->cy == CW_USEDEFAULT, "%d != CW_USEDEFAULT\n", mdi_cs->cy);
1174
1175             /* CREATESTRUCT should have fixed values */
1176             ok(cs->x != CW_USEDEFAULT, "%d == CW_USEDEFAULT\n", cs->x);
1177             ok(cs->y != CW_USEDEFAULT, "%d == CW_USEDEFAULT\n", cs->y);
1178
1179             /* cx/cy == CW_USEDEFAULT are translated to NOT zero values */
1180             ok(cs->cx != CW_USEDEFAULT && cs->cx != 0, "%d == CW_USEDEFAULT\n", cs->cx);
1181             ok(cs->cy != CW_USEDEFAULT && cs->cy != 0, "%d == CW_USEDEFAULT\n", cs->cy);
1182
1183             ok(!(cs->style & WS_POPUP), "WS_POPUP is not allowed\n");
1184
1185             if (GetWindowLongA(cs->hwndParent, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1186             {
1187                 LONG style = mdi_cs->style | WS_CHILD | WS_CLIPSIBLINGS;
1188                 ok(cs->style == style,
1189                    "cs->style does not match (%08lx)\n", cs->style);
1190             }
1191             else
1192             {
1193                 LONG style = mdi_cs->style;
1194                 style &= ~WS_POPUP;
1195                 style |= WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CAPTION |
1196                     WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
1197                 ok(cs->style == style,
1198                    "cs->style does not match (%08lx)\n", cs->style);
1199             }
1200             break;
1201         }
1202
1203         case WM_GETMINMAXINFO:
1204         {
1205             HWND client = GetParent(hwnd);
1206             RECT rc;
1207             MINMAXINFO *minmax = (MINMAXINFO *)lparam;
1208             MINMAXINFO my_minmax;
1209             LONG style, exstyle;
1210
1211             style = GetWindowLongA(hwnd, GWL_STYLE);
1212             exstyle = GetWindowLongA(hwnd, GWL_EXSTYLE);
1213
1214             GetWindowRect(client, &rc);
1215             trace("MDI client %p window size = (%ld x %ld)\n", client, rc.right-rc.left, rc.bottom-rc.top);
1216             GetClientRect(client, &rc);
1217             trace("MDI client %p client size = (%ld x %ld)\n", client, rc.right, rc.bottom);
1218             trace("screen size: %d x %d\n", GetSystemMetrics(SM_CXSCREEN),
1219                                             GetSystemMetrics(SM_CYSCREEN));
1220
1221             GetClientRect(client, &rc);
1222             if ((style & WS_CAPTION) == WS_CAPTION)
1223                 style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
1224             AdjustWindowRectEx(&rc, style, 0, exstyle);
1225             trace("MDI child: calculated max window size = (%ld x %ld)\n", rc.right-rc.left, rc.bottom-rc.top);
1226
1227             trace("ptReserved = (%ld,%ld)\n"
1228                   "ptMaxSize = (%ld,%ld)\n"
1229                   "ptMaxPosition = (%ld,%ld)\n"
1230                   "ptMinTrackSize = (%ld,%ld)\n"
1231                   "ptMaxTrackSize = (%ld,%ld)\n",
1232                   minmax->ptReserved.x, minmax->ptReserved.y,
1233                   minmax->ptMaxSize.x, minmax->ptMaxSize.y,
1234                   minmax->ptMaxPosition.x, minmax->ptMaxPosition.y,
1235                   minmax->ptMinTrackSize.x, minmax->ptMinTrackSize.y,
1236                   minmax->ptMaxTrackSize.x, minmax->ptMaxTrackSize.y);
1237
1238             ok(minmax->ptMaxSize.x == rc.right - rc.left, "default width of maximized child %ld != %ld\n",
1239                minmax->ptMaxSize.x, rc.right - rc.left);
1240             ok(minmax->ptMaxSize.y == rc.bottom - rc.top, "default height of maximized child %ld != %ld\n",
1241                minmax->ptMaxSize.y, rc.bottom - rc.top);
1242
1243             DefMDIChildProcA(hwnd, msg, wparam, lparam);
1244
1245             trace("DefMDIChildProc returned:\n"
1246                   "ptReserved = (%ld,%ld)\n"
1247                   "ptMaxSize = (%ld,%ld)\n"
1248                   "ptMaxPosition = (%ld,%ld)\n"
1249                   "ptMinTrackSize = (%ld,%ld)\n"
1250                   "ptMaxTrackSize = (%ld,%ld)\n",
1251                   minmax->ptReserved.x, minmax->ptReserved.y,
1252                   minmax->ptMaxSize.x, minmax->ptMaxSize.y,
1253                   minmax->ptMaxPosition.x, minmax->ptMaxPosition.y,
1254                   minmax->ptMinTrackSize.x, minmax->ptMinTrackSize.y,
1255                   minmax->ptMaxTrackSize.x, minmax->ptMaxTrackSize.y);
1256
1257             MDI_ChildGetMinMaxInfo(client, hwnd, &my_minmax);
1258             ok(minmax->ptMaxSize.x == my_minmax.ptMaxSize.x, "default width of maximized child %ld != %ld\n",
1259                minmax->ptMaxSize.x, my_minmax.ptMaxSize.x);
1260             ok(minmax->ptMaxSize.y == my_minmax.ptMaxSize.y, "default height of maximized child %ld != %ld\n",
1261                minmax->ptMaxSize.y, my_minmax.ptMaxSize.y);
1262
1263             return 1;
1264         }
1265     }
1266     return DefMDIChildProcA(hwnd, msg, wparam, lparam);
1267 }
1268
1269 static LRESULT WINAPI mdi_child_wnd_proc_2(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1270 {
1271     switch (msg)
1272     {
1273         case WM_NCCREATE:
1274         case WM_CREATE:
1275         {
1276             CREATESTRUCTA *cs = (CREATESTRUCTA *)lparam;
1277
1278             trace("%s\n", (msg == WM_NCCREATE) ? "WM_NCCREATE" : "WM_CREATE");
1279             trace("x %d, y %d, cx %d, cy %d\n", cs->x, cs->y, cs->cx, cs->cy);
1280
1281             ok(!(cs->dwExStyle & WS_EX_MDICHILD), "WS_EX_MDICHILD should not be set\n");
1282             ok(cs->lpCreateParams == mdi_lParam_test_message, "wrong cs->lpCreateParams\n");
1283
1284             ok(!lstrcmpA(cs->lpszClass, "MDI_child_Class_2"), "wrong class name\n");
1285             ok(!lstrcmpA(cs->lpszName, "MDI child"), "wrong title\n");
1286
1287             /* CREATESTRUCT should have fixed values */
1288             /* For some reason Win9x doesn't translate cs->x from CW_USEDEFAULT,
1289                while NT does. */
1290             /*ok(cs->x != CW_USEDEFAULT, "%d == CW_USEDEFAULT\n", cs->x);*/
1291             ok(cs->y != CW_USEDEFAULT, "%d == CW_USEDEFAULT\n", cs->y);
1292
1293             /* cx/cy == CW_USEDEFAULT are translated to 0 */
1294             /* For some reason Win98 doesn't translate cs->cx from CW_USEDEFAULT,
1295                while Win95, Win2k, WinXP do. */
1296             /*ok(cs->cx == 0, "%d != 0\n", cs->cx);*/
1297             ok(cs->cy == 0, "%d != 0\n", cs->cy);
1298             break;
1299         }
1300
1301         case WM_GETMINMAXINFO:
1302         {
1303             HWND parent = GetParent(hwnd);
1304             RECT rc;
1305             MINMAXINFO *minmax = (MINMAXINFO *)lparam;
1306             LONG style, exstyle;
1307
1308             trace("WM_GETMINMAXINFO\n");
1309
1310             style = GetWindowLongA(hwnd, GWL_STYLE);
1311             exstyle = GetWindowLongA(hwnd, GWL_EXSTYLE);
1312
1313             GetClientRect(parent, &rc);
1314             trace("parent %p client size = (%ld x %ld)\n", parent, rc.right, rc.bottom);
1315
1316             GetClientRect(parent, &rc);
1317             if ((style & WS_CAPTION) == WS_CAPTION)
1318                 style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
1319             AdjustWindowRectEx(&rc, style, 0, exstyle);
1320             trace("calculated max child window size = (%ld x %ld)\n", rc.right-rc.left, rc.bottom-rc.top);
1321
1322             trace("ptReserved = (%ld,%ld)\n"
1323                   "ptMaxSize = (%ld,%ld)\n"
1324                   "ptMaxPosition = (%ld,%ld)\n"
1325                   "ptMinTrackSize = (%ld,%ld)\n"
1326                   "ptMaxTrackSize = (%ld,%ld)\n",
1327                   minmax->ptReserved.x, minmax->ptReserved.y,
1328                   minmax->ptMaxSize.x, minmax->ptMaxSize.y,
1329                   minmax->ptMaxPosition.x, minmax->ptMaxPosition.y,
1330                   minmax->ptMinTrackSize.x, minmax->ptMinTrackSize.y,
1331                   minmax->ptMaxTrackSize.x, minmax->ptMaxTrackSize.y);
1332
1333             ok(minmax->ptMaxSize.x == rc.right - rc.left, "default width of maximized child %ld != %ld\n",
1334                minmax->ptMaxSize.x, rc.right - rc.left);
1335             ok(minmax->ptMaxSize.y == rc.bottom - rc.top, "default height of maximized child %ld != %ld\n",
1336                minmax->ptMaxSize.y, rc.bottom - rc.top);
1337             break;
1338         }
1339
1340         case WM_WINDOWPOSCHANGED:
1341         {
1342             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
1343             RECT rc1, rc2;
1344
1345             GetWindowRect(hwnd, &rc1);
1346             trace("window: (%ld,%ld)-(%ld,%ld)\n", rc1.left, rc1.top, rc1.right, rc1.bottom);
1347             SetRect(&rc2, winpos->x, winpos->y, winpos->x + winpos->cx, winpos->y + winpos->cy);
1348             /* note: winpos coordinates are relative to parent */
1349             MapWindowPoints(GetParent(hwnd), 0, (LPPOINT)&rc2, 2);
1350             trace("pos: (%ld,%ld)-(%ld,%ld)\n", rc2.left, rc2.top, rc2.right, rc2.bottom);
1351             ok(EqualRect(&rc1, &rc2), "rects do not match\n");
1352
1353             GetWindowRect(hwnd, &rc1);
1354             GetClientRect(hwnd, &rc2);
1355             DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc1);
1356             MapWindowPoints(0, hwnd, (LPPOINT)&rc1, 2);
1357             ok(EqualRect(&rc1, &rc2), "rects do not match\n");
1358         }
1359         /* fall through */
1360         case WM_WINDOWPOSCHANGING:
1361         {
1362             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
1363             WINDOWPOS my_winpos = *winpos;
1364
1365             trace("%s\n", (msg == WM_WINDOWPOSCHANGING) ? "WM_WINDOWPOSCHANGING" : "WM_WINDOWPOSCHANGED");
1366             trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
1367                   winpos->hwnd, winpos->hwndInsertAfter,
1368                   winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
1369
1370             DefWindowProcA(hwnd, msg, wparam, lparam);
1371
1372             trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
1373                   winpos->hwnd, winpos->hwndInsertAfter,
1374                   winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
1375
1376             ok(!memcmp(&my_winpos, winpos, sizeof(WINDOWPOS)),
1377                "DefWindowProc should not change WINDOWPOS values\n");
1378
1379             return 1;
1380         }
1381     }
1382     return DefWindowProcA(hwnd, msg, wparam, lparam);
1383 }
1384
1385 static LRESULT WINAPI mdi_main_wnd_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1386 {
1387     static HWND mdi_client;
1388
1389     switch (msg)
1390     {
1391         case WM_CREATE:
1392         {
1393             CLIENTCREATESTRUCT client_cs;
1394             RECT rc;
1395
1396             GetClientRect(hwnd, &rc);
1397
1398             client_cs.hWindowMenu = 0;
1399             client_cs.idFirstChild = 1;
1400
1401             /* MDIClient without MDIS_ALLCHILDSTYLES */
1402             mdi_client = CreateWindowExA(0, "mdiclient",
1403                                          NULL,
1404                                          WS_CHILD /*| WS_VISIBLE*/,
1405                                           /* tests depend on a not zero MDIClient size */
1406                                          0, 0, rc.right, rc.bottom,
1407                                          hwnd, 0, GetModuleHandle(0),
1408                                          (LPVOID)&client_cs);
1409             assert(mdi_client);
1410             test_MDI_create(hwnd, mdi_client);
1411             DestroyWindow(mdi_client);
1412
1413             /* MDIClient with MDIS_ALLCHILDSTYLES */
1414             mdi_client = CreateWindowExA(0, "mdiclient",
1415                                          NULL,
1416                                          WS_CHILD | MDIS_ALLCHILDSTYLES /*| WS_VISIBLE*/,
1417                                           /* tests depend on a not zero MDIClient size */
1418                                          0, 0, rc.right, rc.bottom,
1419                                          hwnd, 0, GetModuleHandle(0),
1420                                          (LPVOID)&client_cs);
1421             assert(mdi_client);
1422             test_MDI_create(hwnd, mdi_client);
1423             DestroyWindow(mdi_client);
1424             break;
1425         }
1426
1427         case WM_WINDOWPOSCHANGED:
1428         {
1429             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
1430             RECT rc1, rc2;
1431
1432             GetWindowRect(hwnd, &rc1);
1433             trace("window: (%ld,%ld)-(%ld,%ld)\n", rc1.left, rc1.top, rc1.right, rc1.bottom);
1434             SetRect(&rc2, winpos->x, winpos->y, winpos->x + winpos->cx, winpos->y + winpos->cy);
1435             /* note: winpos coordinates are relative to parent */
1436             MapWindowPoints(GetParent(hwnd), 0, (LPPOINT)&rc2, 2);
1437             trace("pos: (%ld,%ld)-(%ld,%ld)\n", rc2.left, rc2.top, rc2.right, rc2.bottom);
1438             ok(EqualRect(&rc1, &rc2), "rects do not match\n");
1439
1440             GetWindowRect(hwnd, &rc1);
1441             GetClientRect(hwnd, &rc2);
1442             DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc1);
1443             MapWindowPoints(0, hwnd, (LPPOINT)&rc1, 2);
1444             ok(EqualRect(&rc1, &rc2), "rects do not match\n");
1445         }
1446         /* fall through */
1447         case WM_WINDOWPOSCHANGING:
1448         {
1449             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
1450             WINDOWPOS my_winpos = *winpos;
1451
1452             trace("%s\n", (msg == WM_WINDOWPOSCHANGING) ? "WM_WINDOWPOSCHANGING" : "WM_WINDOWPOSCHANGED");
1453             trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
1454                   winpos->hwnd, winpos->hwndInsertAfter,
1455                   winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
1456
1457             DefWindowProcA(hwnd, msg, wparam, lparam);
1458
1459             trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
1460                   winpos->hwnd, winpos->hwndInsertAfter,
1461                   winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
1462
1463             ok(!memcmp(&my_winpos, winpos, sizeof(WINDOWPOS)),
1464                "DefWindowProc should not change WINDOWPOS values\n");
1465
1466             return 1;
1467         }
1468
1469         case WM_CLOSE:
1470             PostQuitMessage(0);
1471             break;
1472     }
1473     return DefFrameProcA(hwnd, mdi_client, msg, wparam, lparam);
1474 }
1475
1476 static BOOL mdi_RegisterWindowClasses(void)
1477 {
1478     WNDCLASSA cls;
1479
1480     cls.style = 0;
1481     cls.lpfnWndProc = mdi_main_wnd_procA;
1482     cls.cbClsExtra = 0;
1483     cls.cbWndExtra = 0;
1484     cls.hInstance = GetModuleHandleA(0);
1485     cls.hIcon = 0;
1486     cls.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
1487     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
1488     cls.lpszMenuName = NULL;
1489     cls.lpszClassName = "MDI_parent_Class";
1490     if(!RegisterClassA(&cls)) return FALSE;
1491
1492     cls.lpfnWndProc = mdi_child_wnd_proc_1;
1493     cls.lpszClassName = "MDI_child_Class_1";
1494     if(!RegisterClassA(&cls)) return FALSE;
1495
1496     cls.lpfnWndProc = mdi_child_wnd_proc_2;
1497     cls.lpszClassName = "MDI_child_Class_2";
1498     if(!RegisterClassA(&cls)) return FALSE;
1499
1500     return TRUE;
1501 }
1502
1503 static void test_mdi(void)
1504 {
1505     HWND mdi_hwndMain;
1506     /*MSG msg;*/
1507
1508     if (!mdi_RegisterWindowClasses()) assert(0);
1509
1510     mdi_hwndMain = CreateWindowExA(0, "MDI_parent_Class", "MDI parent window",
1511                                    WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
1512                                    WS_MAXIMIZEBOX /*| WS_VISIBLE*/,
1513                                    100, 100, CW_USEDEFAULT, CW_USEDEFAULT,
1514                                    GetDesktopWindow(), 0,
1515                                    GetModuleHandle(0), NULL);
1516     assert(mdi_hwndMain);
1517 /*
1518     while(GetMessage(&msg, 0, 0, 0))
1519     {
1520         TranslateMessage(&msg);
1521         DispatchMessage(&msg);
1522     }
1523 */
1524 }
1525
1526 static void test_icons(void)
1527 {
1528     WNDCLASSEXA cls;
1529     HWND hwnd;
1530     HICON icon = LoadIconA(0, (LPSTR)IDI_APPLICATION);
1531     HICON icon2 = LoadIconA(0, (LPSTR)IDI_QUESTION);
1532     HICON small_icon = LoadImageA(0, (LPSTR)IDI_APPLICATION, IMAGE_ICON,
1533                                   GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED );
1534     HICON res;
1535
1536     cls.cbSize = sizeof(cls);
1537     cls.style = 0;
1538     cls.lpfnWndProc = DefWindowProcA;
1539     cls.cbClsExtra = 0;
1540     cls.cbWndExtra = 0;
1541     cls.hInstance = 0;
1542     cls.hIcon = LoadIconA(0, (LPSTR)IDI_HAND);
1543     cls.hIconSm = small_icon;
1544     cls.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
1545     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
1546     cls.lpszMenuName = NULL;
1547     cls.lpszClassName = "IconWindowClass";
1548
1549     RegisterClassExA(&cls);
1550
1551     hwnd = CreateWindowExA(0, "IconWindowClass", "icon test", 0,
1552                            100, 100, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, NULL, NULL);
1553     assert( hwnd );
1554
1555     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_BIG, 0 );
1556     ok( res == 0, "wrong big icon %p/0\n", res );
1557     res = (HICON)SendMessageA( hwnd, WM_SETICON, ICON_BIG, (LPARAM)icon );
1558     ok( res == 0, "wrong previous big icon %p/0\n", res );
1559     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_BIG, 0 );
1560     ok( res == icon, "wrong big icon after set %p/%p\n", res, icon );
1561     res = (HICON)SendMessageA( hwnd, WM_SETICON, ICON_BIG, (LPARAM)icon2 );
1562     ok( res == icon, "wrong previous big icon %p/%p\n", res, icon );
1563     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_BIG, 0 );
1564     ok( res == icon2, "wrong big icon after set %p/%p\n", res, icon2 );
1565
1566     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL, 0 );
1567     ok( res == 0, "wrong small icon %p/0\n", res );
1568     /* this test is XP specific */
1569     /*res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL2, 0 );
1570     ok( res != 0, "wrong small icon %p\n", res );*/
1571     res = (HICON)SendMessageA( hwnd, WM_SETICON, ICON_SMALL, (LPARAM)icon );
1572     ok( res == 0, "wrong previous small icon %p/0\n", res );
1573     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL, 0 );
1574     ok( res == icon, "wrong small icon after set %p/%p\n", res, icon );
1575     /* this test is XP specific */
1576     /*res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL2, 0 );
1577     ok( res == icon, "wrong small icon after set %p/%p\n", res, icon );*/
1578     res = (HICON)SendMessageA( hwnd, WM_SETICON, ICON_SMALL, (LPARAM)small_icon );
1579     ok( res == icon, "wrong previous small icon %p/%p\n", res, icon );
1580     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL, 0 );
1581     ok( res == small_icon, "wrong small icon after set %p/%p\n", res, small_icon );
1582     /* this test is XP specific */
1583     /*res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL2, 0 );
1584     ok( res == small_icon, "wrong small icon after set %p/%p\n", res, small_icon );*/
1585
1586     /* make sure the big icon hasn't changed */
1587     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_BIG, 0 );
1588     ok( res == icon2, "wrong big icon after set %p/%p\n", res, icon2 );
1589 }
1590
1591 static void test_SetWindowPos(HWND hwnd)
1592 {
1593     BOOL is_win9x = GetWindowLongW(hwnd, GWL_WNDPROC) == 0;
1594
1595     /* Win9x truncates coordinates to 16-bit irrespectively */
1596     if (is_win9x) return;
1597
1598     SetWindowPos(hwnd, 0, -32769, -40000, -32769, -90000, SWP_NOMOVE);
1599     SetWindowPos(hwnd, 0, 32768, 40000, 32768, 40000, SWP_NOMOVE);
1600
1601     SetWindowPos(hwnd, 0, -32769, -40000, -32769, -90000, SWP_NOSIZE);
1602     SetWindowPos(hwnd, 0, 32768, 40000, 32768, 40000, SWP_NOSIZE);
1603 }
1604
1605 static void test_SetMenu(HWND parent)
1606 {
1607     HWND child;
1608     HMENU hMenu, ret;
1609     BOOL is_win9x = GetWindowLongW(parent, GWL_WNDPROC) == 0;
1610
1611     hMenu = CreateMenu();
1612     assert(hMenu);
1613
1614     ok(SetMenu(parent, hMenu), "SetMenu on a top level window should not fail\n");
1615     test_nonclient_area(parent);
1616     ret = GetMenu(parent);
1617     ok(ret == hMenu, "unexpected menu id %p\n", ret);
1618     /* test whether we can destroy a menu assigned to a window */
1619     ok(DestroyMenu(hMenu), "DestroyMenu error %ld\n", GetLastError());
1620     ok(!IsMenu(hMenu), "menu handle should be not valid after DestroyMenu\n");
1621     ret = GetMenu(parent);
1622     /* This test fails on Win9x */
1623     if (!is_win9x)
1624         ok(ret == hMenu, "unexpected menu id %p\n", ret);
1625     ok(SetMenu(parent, 0), "SetMenu(0) on a top level window should not fail\n");
1626     test_nonclient_area(parent);
1627
1628     hMenu = CreateMenu();
1629     assert(hMenu);
1630
1631     /* parent */
1632     ret = GetMenu(parent);
1633     ok(ret == 0, "unexpected menu id %p\n", ret);
1634
1635     ok(!SetMenu(parent, (HMENU)20), "SetMenu with invalid menu handle should fail\n");
1636     test_nonclient_area(parent);
1637     ret = GetMenu(parent);
1638     ok(ret == 0, "unexpected menu id %p\n", ret);
1639
1640     ok(SetMenu(parent, hMenu), "SetMenu on a top level window should not fail\n");
1641     test_nonclient_area(parent);
1642     ret = GetMenu(parent);
1643     ok(ret == hMenu, "unexpected menu id %p\n", ret);
1644
1645     ok(SetMenu(parent, 0), "SetMenu(0) on a top level window should not fail\n");
1646     test_nonclient_area(parent);
1647     ret = GetMenu(parent);
1648     ok(ret == 0, "unexpected menu id %p\n", ret);
1649  
1650     /* child */
1651     child = CreateWindowExA(0, "static", NULL, WS_CHILD, 0, 0, 0, 0, parent, (HMENU)10, 0, NULL);
1652     assert(child);
1653
1654     ret = GetMenu(child);
1655     ok(ret == (HMENU)10, "unexpected menu id %p\n", ret);
1656
1657     ok(!SetMenu(child, (HMENU)20), "SetMenu with invalid menu handle should fail\n");
1658     test_nonclient_area(child);
1659     ret = GetMenu(child);
1660     ok(ret == (HMENU)10, "unexpected menu id %p\n", ret);
1661
1662     ok(!SetMenu(child, hMenu), "SetMenu on a child window should fail\n");
1663     test_nonclient_area(child);
1664     ret = GetMenu(child);
1665     ok(ret == (HMENU)10, "unexpected menu id %p\n", ret);
1666
1667     ok(!SetMenu(child, 0), "SetMenu(0) on a child window should fail\n");
1668     test_nonclient_area(child);
1669     ret = GetMenu(child);
1670     ok(ret == (HMENU)10, "unexpected menu id %p\n", ret);
1671
1672     DestroyWindow(child);
1673     DestroyMenu(hMenu);
1674 }
1675
1676 static void test_window_tree(HWND parent, const DWORD *style, const int *order, int total)
1677 {
1678     HWND child[5], hwnd;
1679     int i;
1680
1681     assert(total <= 5);
1682
1683     hwnd = GetWindow(parent, GW_CHILD);
1684     ok(!hwnd, "have to start without children to perform the test\n");
1685
1686     for (i = 0; i < total; i++)
1687     {
1688         child[i] = CreateWindowExA(0, "static", "", style[i], 0,0,10,10,
1689                                    parent, 0, 0, NULL);
1690         trace("child[%d] = %p\n", i, child[i]);
1691         ok(child[i] != 0, "CreateWindowEx failed to create child window\n");
1692     }
1693
1694     hwnd = GetWindow(parent, GW_CHILD);
1695     ok(hwnd != 0, "GetWindow(GW_CHILD) failed\n");
1696     ok(hwnd == GetWindow(child[total - 1], GW_HWNDFIRST), "GW_HWNDFIRST is wrong\n");
1697     ok(child[order[total - 1]] == GetWindow(child[0], GW_HWNDLAST), "GW_HWNDLAST is wrong\n");
1698
1699     for (i = 0; i < total; i++)
1700     {
1701         trace("hwnd[%d] = %p\n", i, hwnd);
1702         ok(child[order[i]] == hwnd, "Z order of child #%d is wrong\n", i);
1703
1704         hwnd = GetWindow(hwnd, GW_HWNDNEXT);
1705     }
1706
1707     for (i = 0; i < total; i++)
1708         ok(DestroyWindow(child[i]), "DestroyWindow failed\n");
1709 }
1710
1711 static void test_children_zorder(HWND parent)
1712 {
1713     const DWORD simple_style[5] = { WS_CHILD, WS_CHILD, WS_CHILD, WS_CHILD,
1714                                     WS_CHILD };
1715     const int simple_order[5] = { 0, 1, 2, 3, 4 };
1716
1717     const DWORD complex_style[5] = { WS_CHILD, WS_CHILD | WS_MAXIMIZE,
1718                              WS_CHILD | WS_VISIBLE, WS_CHILD,
1719                              WS_CHILD | WS_MAXIMIZE | WS_VISIBLE };
1720     const int complex_order_1[1] = { 0 };
1721     const int complex_order_2[2] = { 1, 0 };
1722     const int complex_order_3[3] = { 1, 0, 2 };
1723     const int complex_order_4[4] = { 1, 0, 2, 3 };
1724     const int complex_order_5[5] = { 4, 1, 0, 2, 3 };
1725
1726     /* simple WS_CHILD */
1727     test_window_tree(parent, simple_style, simple_order, 5);
1728
1729     /* complex children styles */
1730     test_window_tree(parent, complex_style, complex_order_1, 1);
1731     test_window_tree(parent, complex_style, complex_order_2, 2);
1732     test_window_tree(parent, complex_style, complex_order_3, 3);
1733     test_window_tree(parent, complex_style, complex_order_4, 4);
1734     test_window_tree(parent, complex_style, complex_order_5, 5);
1735 }
1736
1737 static void test_SetFocus(HWND hwnd)
1738 {
1739     HWND child;
1740
1741     /* check if we can set focus to non-visible windows */
1742
1743     ShowWindow(hwnd, SW_SHOW);
1744     SetFocus(0);
1745     SetFocus(hwnd);
1746     ok( GetFocus() == hwnd, "Failed to set focus to visible window %p\n", hwnd );
1747     ok( GetWindowLong(hwnd,GWL_STYLE) & WS_VISIBLE, "Window %p not visible\n", hwnd );
1748     ShowWindow(hwnd, SW_HIDE);
1749     SetFocus(0);
1750     SetFocus(hwnd);
1751     ok( GetFocus() == hwnd, "Failed to set focus to invisible window %p\n", hwnd );
1752     ok( !(GetWindowLong(hwnd,GWL_STYLE) & WS_VISIBLE), "Window %p still visible\n", hwnd );
1753     child = CreateWindowExA(0, "static", NULL, WS_CHILD, 0, 0, 0, 0, hwnd, 0, 0, NULL);
1754     assert(child);
1755     SetFocus(child);
1756     ok( GetFocus() == child, "Failed to set focus to invisible child %p\n", child );
1757     ok( !(GetWindowLong(child,GWL_STYLE) & WS_VISIBLE), "Child %p is visible\n", child );
1758     ShowWindow(child, SW_SHOW);
1759     ok( GetWindowLong(child,GWL_STYLE) & WS_VISIBLE, "Child %p is not visible\n", child );
1760     ok( GetFocus() == child, "Focus no longer on child %p\n", child );
1761     ShowWindow(child, SW_HIDE);
1762     ok( !(GetWindowLong(child,GWL_STYLE) & WS_VISIBLE), "Child %p is visible\n", child );
1763     ok( GetFocus() == hwnd, "Focus should be on parent %p, not %p\n", hwnd, GetFocus() );
1764     ShowWindow(child, SW_SHOW);
1765     SetFocus(child);
1766     ok( GetFocus() == child, "Focus should be on child %p\n", child );
1767     SetWindowPos(child,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_HIDEWINDOW);
1768     ok( GetFocus() == child, "Focus should still be on child %p\n", child );
1769
1770     ShowWindow(child, SW_HIDE);
1771     SetFocus(hwnd);
1772     ok( GetFocus() == hwnd, "Focus should be on parent %p, not %p\n", hwnd, GetFocus() );
1773     SetWindowPos(child,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_SHOWWINDOW);
1774     ok( GetFocus() == hwnd, "Focus should still be on parent %p, not %p\n", hwnd, GetFocus() );
1775     ShowWindow(child, SW_HIDE);
1776     ok( GetFocus() == hwnd, "Focus should still be on parent %p, not %p\n", hwnd, GetFocus() );
1777
1778     ShowWindow(hwnd, SW_SHOW);
1779     ShowWindow(child, SW_SHOW);
1780     SetFocus(child);
1781     ok( GetFocus() == child, "Focus should be on child %p\n", child );
1782     EnableWindow(hwnd, FALSE);
1783     ok( GetFocus() == child, "Focus should still be on child %p\n", child );
1784     EnableWindow(hwnd, TRUE);
1785
1786     DestroyWindow( child );
1787 }
1788
1789 static void test_SetActiveWindow(HWND hwnd)
1790 {
1791     HWND hwnd2;
1792
1793     ShowWindow(hwnd, SW_SHOW);
1794     SetActiveWindow(0);
1795     SetActiveWindow(hwnd);
1796     ok( GetActiveWindow() == hwnd, "Failed to set focus to visible window %p\n", hwnd );
1797     SetWindowPos(hwnd,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_HIDEWINDOW);
1798     ok( GetActiveWindow() == hwnd, "Window %p no longer active\n", hwnd );
1799     SetWindowPos(hwnd,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_SHOWWINDOW);
1800     ShowWindow(hwnd, SW_HIDE);
1801     ok( GetActiveWindow() != hwnd, "Window %p is still active\n", hwnd );
1802     ShowWindow(hwnd, SW_SHOW);
1803
1804     hwnd2 = CreateWindowExA(0, "static", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 0, 0, hwnd, 0, 0, NULL);
1805     ok( GetActiveWindow() == hwnd2, "Window %p is not active\n", hwnd2 );
1806     DestroyWindow(hwnd2);
1807     ok( GetActiveWindow() != hwnd2, "Window %p is still active\n", hwnd2 );
1808
1809     hwnd2 = CreateWindowExA(0, "static", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 0, 0, hwnd, 0, 0, NULL);
1810     ok( GetActiveWindow() == hwnd2, "Window %p is not active\n", hwnd2 );
1811     SetWindowPos(hwnd2,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_HIDEWINDOW);
1812     ok( GetActiveWindow() == hwnd2, "Window %p no longer active (%p)\n", hwnd2, GetActiveWindow() );
1813     DestroyWindow(hwnd2);
1814     ok( GetActiveWindow() != hwnd2, "Window %p is still active\n", hwnd2 );
1815 }
1816
1817 static void check_wnd_state(HWND active, HWND foreground, HWND focus, HWND capture)
1818 {
1819     ok(active == GetActiveWindow(), "GetActiveWindow() = %p\n", GetActiveWindow());
1820     if (foreground)
1821         ok(foreground == GetForegroundWindow(), "GetForegroundWindow() = %p\n", GetForegroundWindow());
1822     ok(focus == GetFocus(), "GetFocus() = %p\n", GetFocus());
1823     ok(capture == GetCapture(), "GetCapture() = %p\n", GetCapture());
1824 }
1825
1826 static WNDPROC old_button_proc;
1827
1828 static LRESULT WINAPI button_hook_proc(HWND button, UINT msg, WPARAM wparam, LPARAM lparam)
1829 {
1830     LRESULT ret;
1831     USHORT key_state;
1832
1833     key_state = GetKeyState(VK_LBUTTON);
1834     ok(!(key_state & 0x8000), "VK_LBUTTON should not be pressed, state %04x\n", key_state);
1835
1836     ret = CallWindowProcA(old_button_proc, button, msg, wparam, lparam);
1837
1838     if (msg == WM_LBUTTONDOWN)
1839     {
1840         HWND hwnd, capture;
1841
1842         check_wnd_state(button, button, button, button);
1843
1844         hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0, 0, 10, 10, 0, 0, 0, NULL);
1845         assert(hwnd);
1846         trace("hwnd %p\n", hwnd);
1847
1848         check_wnd_state(button, button, button, button);
1849
1850         ShowWindow(hwnd, SW_SHOWNOACTIVATE);
1851
1852         check_wnd_state(button, button, button, button);
1853
1854         DestroyWindow(hwnd);
1855
1856         hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0, 0, 10, 10, 0, 0, 0, NULL);
1857         assert(hwnd);
1858         trace("hwnd %p\n", hwnd);
1859
1860         check_wnd_state(button, button, button, button);
1861
1862         /* button wnd proc should release capture on WM_KILLFOCUS if it does
1863          * match internal button state.
1864          */
1865         SendMessage(button, WM_KILLFOCUS, 0, 0);
1866         check_wnd_state(button, button, button, 0);
1867
1868         ShowWindow(hwnd, SW_SHOW);
1869         check_wnd_state(hwnd, hwnd, hwnd, 0);
1870
1871         capture = SetCapture(hwnd);
1872         ok(capture == 0, "SetCapture() = %p\n", capture);
1873
1874         check_wnd_state(hwnd, hwnd, hwnd, hwnd);
1875
1876         DestroyWindow(hwnd);
1877
1878         check_wnd_state(button, 0, button, 0);
1879     }
1880
1881     return ret;
1882 }
1883
1884 static void test_capture_1(void)
1885 {
1886     HWND button, capture;
1887
1888     capture = GetCapture();
1889     ok(capture == 0, "GetCapture() = %p\n", capture);
1890
1891     button = CreateWindowExA(0, "button", NULL, WS_POPUP | WS_VISIBLE, 0, 0, 10, 10, 0, 0, 0, NULL);
1892     assert(button);
1893     trace("button %p\n", button);
1894
1895     old_button_proc = (WNDPROC)SetWindowLongPtrA(button, GWLP_WNDPROC, (LONG_PTR)button_hook_proc);
1896
1897     SendMessageA(button, WM_LBUTTONDOWN, 0, 0);
1898
1899     DestroyWindow(button);
1900 }
1901
1902 static void test_capture_2(void)
1903 {
1904     HWND button, hwnd, capture;
1905
1906     check_wnd_state(0, 0, 0, 0);
1907
1908     button = CreateWindowExA(0, "button", NULL, WS_POPUP | WS_VISIBLE, 0, 0, 10, 10, 0, 0, 0, NULL);
1909     assert(button);
1910     trace("button %p\n", button);
1911
1912     check_wnd_state(button, button, button, 0);
1913
1914     capture = SetCapture(button);
1915     ok(capture == 0, "SetCapture() = %p\n", capture);
1916
1917     check_wnd_state(button, button, button, button);
1918
1919     /* button wnd proc should ignore WM_KILLFOCUS if it doesn't match
1920      * internal button state.
1921      */
1922     SendMessage(button, WM_KILLFOCUS, 0, 0);
1923     check_wnd_state(button, button, button, button);
1924
1925     hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0, 0, 10, 10, 0, 0, 0, NULL);
1926     assert(hwnd);
1927     trace("hwnd %p\n", hwnd);
1928
1929     check_wnd_state(button, button, button, button);
1930
1931     ShowWindow(hwnd, SW_SHOWNOACTIVATE);
1932
1933     check_wnd_state(button, button, button, button);
1934
1935     DestroyWindow(hwnd);
1936
1937     hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0, 0, 10, 10, 0, 0, 0, NULL);
1938     assert(hwnd);
1939     trace("hwnd %p\n", hwnd);
1940
1941     check_wnd_state(button, button, button, button);
1942
1943     ShowWindow(hwnd, SW_SHOW);
1944
1945     check_wnd_state(hwnd, hwnd, hwnd, button);
1946
1947     capture = SetCapture(hwnd);
1948     ok(capture == button, "SetCapture() = %p\n", capture);
1949
1950     check_wnd_state(hwnd, hwnd, hwnd, hwnd);
1951
1952     DestroyWindow(hwnd);
1953     check_wnd_state(button, button, button, 0);
1954
1955     DestroyWindow(button);
1956     check_wnd_state(0, 0, 0, 0);
1957 }
1958
1959 static void test_capture_3(HWND hwnd1, HWND hwnd2)
1960 {
1961     ShowWindow(hwnd1, SW_HIDE);
1962     ShowWindow(hwnd2, SW_HIDE);
1963
1964     ok(!IsWindowVisible(hwnd1), "%p should be invisible\n", hwnd1);
1965     ok(!IsWindowVisible(hwnd2), "%p should be invisible\n", hwnd2);
1966
1967     SetCapture(hwnd1);
1968     check_wnd_state(0, 0, 0, hwnd1);
1969
1970     SetCapture(hwnd2);
1971     check_wnd_state(0, 0, 0, hwnd2);
1972
1973     ShowWindow(hwnd1, SW_SHOW);
1974     check_wnd_state(hwnd1, hwnd1, hwnd1, hwnd2);
1975 }
1976
1977 START_TEST(win)
1978 {
1979     pGetAncestor = (void *)GetProcAddress( GetModuleHandleA("user32.dll"), "GetAncestor" );
1980     pGetWindowInfo = (void *)GetProcAddress( GetModuleHandleA("user32.dll"), "GetWindowInfo" );
1981
1982     hwndMain = CreateWindowExA(0, "static", NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, 0, 0, NULL);
1983     if (hwndMain)
1984     {
1985         ok(!GetParent(hwndMain), "GetParent should return 0 for message only windows\n");
1986         if (pGetAncestor)
1987         {
1988             hwndMessage = pGetAncestor(hwndMain, GA_PARENT);
1989             ok(hwndMessage != 0, "GetAncestor(GA_PARENT) should not return 0 for message only windows\n");
1990             trace("hwndMessage %p\n", hwndMessage);
1991         }
1992         DestroyWindow(hwndMain);
1993     }
1994     else
1995         trace("CreateWindowExA with parent HWND_MESSAGE failed\n");
1996
1997     if (!RegisterWindowClasses()) assert(0);
1998
1999     hhook = SetWindowsHookExA(WH_CBT, cbt_hook_proc, 0, GetCurrentThreadId());
2000     assert(hhook);
2001
2002     hwndMain = CreateWindowExA(/*WS_EX_TOOLWINDOW*/ 0, "MainWindowClass", "Main window",
2003                                WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
2004                                WS_MAXIMIZEBOX | WS_POPUP,
2005                                100, 100, 200, 200,
2006                                0, 0, 0, NULL);
2007     hwndMain2 = CreateWindowExA(/*WS_EX_TOOLWINDOW*/ 0, "MainWindowClass", "Main window 2",
2008                                 WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
2009                                 WS_MAXIMIZEBOX | WS_POPUP,
2010                                 100, 100, 200, 200,
2011                                 0, 0, 0, NULL);
2012     assert( hwndMain );
2013     assert( hwndMain2 );
2014
2015     test_capture_1();
2016     test_capture_2();
2017     test_capture_3(hwndMain, hwndMain2);
2018
2019     test_parent_owner();
2020     test_shell_window();
2021
2022     test_mdi();
2023     test_icons();
2024     test_SetWindowPos(hwndMain);
2025     test_SetMenu(hwndMain);
2026     test_SetFocus(hwndMain);
2027     test_SetActiveWindow(hwndMain);
2028
2029     test_children_zorder(hwndMain);
2030
2031     UnhookWindowsHookEx(hhook);
2032 }