Remove part of test that crashes win95.
[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     check_parents( test, child, child, owner, owner, hwndMain, owner );
400     ok( !IsWindow(owner), "Owner %p not destroyed\n", owner );
401     DestroyWindow(test);
402
403     /* owned top-level popup */
404     owner = create_tool_window( 0, 0 );
405     test = create_tool_window( WS_POPUP, owner );
406     trace( "created owner %p and popup %p\n", owner, test );
407     check_parents( test, desktop, owner, owner, owner, test, owner );
408     DestroyWindow( owner );
409     ok( !IsWindow(test), "Window %p not destroyed by owner destruction\n", test );
410
411     /* top-level popup owned by child */
412     owner = create_tool_window( WS_CHILD, hwndMain2 );
413     test = create_tool_window( WS_POPUP, 0 );
414     trace( "created owner %p and popup %p\n", owner, test );
415     ret = (HWND)SetWindowLongA( test, GWL_HWNDPARENT, (ULONG_PTR)owner );
416     ok( ret == 0, "GWL_HWNDPARENT return value %p expected 0\n", ret );
417     check_parents( test, desktop, owner, owner, owner, test, hwndMain2 );
418     DestroyWindow( owner );
419     ok( IsWindow(test), "Window %p destroyed by owner destruction\n", test );
420     ok( !IsWindow(owner), "Owner %p not destroyed\n", owner );
421     check_parents( test, desktop, owner, owner, owner, test, owner );
422     DestroyWindow(test);
423
424     /* final cleanup */
425     DestroyWindow(child);
426 }
427
428
429 static LRESULT WINAPI main_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
430 {
431     switch (msg)
432     {
433         case WM_GETMINMAXINFO:
434         {
435             MINMAXINFO* minmax = (MINMAXINFO *)lparam;
436
437             trace("hwnd %p, WM_GETMINMAXINFO, %08x, %08lx\n", hwnd, wparam, lparam);
438             trace("ptReserved (%ld,%ld), ptMaxSize (%ld,%ld), ptMaxPosition (%ld,%ld)\n"
439                   "       ptMinTrackSize (%ld,%ld), ptMaxTrackSize (%ld,%ld)\n",
440                   minmax->ptReserved.x, minmax->ptReserved.y,
441                   minmax->ptMaxSize.x, minmax->ptMaxSize.y,
442                   minmax->ptMaxPosition.x, minmax->ptMaxPosition.y,
443                   minmax->ptMinTrackSize.x, minmax->ptMinTrackSize.y,
444                   minmax->ptMaxTrackSize.x, minmax->ptMaxTrackSize.y);
445             SetWindowLongA(hwnd, GWL_USERDATA, 0x20031021);
446             break;
447         }
448         case WM_WINDOWPOSCHANGING:
449         {
450             BOOL is_win9x = GetWindowLongW(hwnd, GWL_WNDPROC) == 0;
451             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
452             trace("main: WM_WINDOWPOSCHANGING\n");
453             trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
454                    winpos->hwnd, winpos->hwndInsertAfter,
455                    winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
456             if (!(winpos->flags & SWP_NOMOVE))
457             {
458                 ok(winpos->x >= -32768 && winpos->x <= 32767, "bad winpos->x %d\n", winpos->x);
459                 ok(winpos->y >= -32768 && winpos->y <= 32767, "bad winpos->y %d\n", winpos->y);
460             }
461             /* Win9x does not fixup cx/xy for WM_WINDOWPOSCHANGING */
462             if (!(winpos->flags & SWP_NOSIZE) && !is_win9x)
463             {
464                 ok(winpos->cx >= 0 && winpos->cx <= 32767, "bad winpos->cx %d\n", winpos->cx);
465                 ok(winpos->cy >= 0 && winpos->cy <= 32767, "bad winpos->cy %d\n", winpos->cy);
466             }
467             break;
468         }
469         case WM_WINDOWPOSCHANGED:
470         {
471             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
472             trace("main: WM_WINDOWPOSCHANGED\n");
473             trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
474                    winpos->hwnd, winpos->hwndInsertAfter,
475                    winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
476             ok(winpos->x >= -32768 && winpos->x <= 32767, "bad winpos->x %d\n", winpos->x);
477             ok(winpos->y >= -32768 && winpos->y <= 32767, "bad winpos->y %d\n", winpos->y);
478
479             ok(winpos->cx >= 0 && winpos->cx <= 32767, "bad winpos->cx %d\n", winpos->cx);
480             ok(winpos->cy >= 0 && winpos->cy <= 32767, "bad winpos->cy %d\n", winpos->cy);
481             break;
482         }
483         case WM_NCCREATE:
484         {
485             BOOL got_getminmaxinfo = GetWindowLongA(hwnd, GWL_USERDATA) == 0x20031021;
486             CREATESTRUCTA *cs = (CREATESTRUCTA *)lparam;
487
488             trace("WM_NCCREATE: hwnd %p, parent %p, style %08lx\n", hwnd, cs->hwndParent, cs->style);
489             if (got_getminmaxinfo)
490                 trace("%p got WM_GETMINMAXINFO\n", hwnd);
491
492             if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
493                 ok(got_getminmaxinfo, "main: WM_GETMINMAXINFO should have been received before WM_NCCREATE\n");
494             else
495                 ok(!got_getminmaxinfo, "main: WM_GETMINMAXINFO should NOT have been received before WM_NCCREATE\n");
496             break;
497         }
498     }
499
500     return DefWindowProcA(hwnd, msg, wparam, lparam);
501 }
502
503 static LRESULT WINAPI tool_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
504 {
505     switch (msg)
506     {
507         case WM_GETMINMAXINFO:
508         {
509             MINMAXINFO* minmax = (MINMAXINFO *)lparam;
510
511             trace("hwnd %p, WM_GETMINMAXINFO, %08x, %08lx\n", hwnd, wparam, lparam);
512             trace("ptReserved (%ld,%ld), ptMaxSize (%ld,%ld), ptMaxPosition (%ld,%ld)\n"
513                   "       ptMinTrackSize (%ld,%ld), ptMaxTrackSize (%ld,%ld)\n",
514                   minmax->ptReserved.x, minmax->ptReserved.y,
515                   minmax->ptMaxSize.x, minmax->ptMaxSize.y,
516                   minmax->ptMaxPosition.x, minmax->ptMaxPosition.y,
517                   minmax->ptMinTrackSize.x, minmax->ptMinTrackSize.y,
518                   minmax->ptMaxTrackSize.x, minmax->ptMaxTrackSize.y);
519             SetWindowLongA(hwnd, GWL_USERDATA, 0x20031021);
520             break;
521         }
522         case WM_NCCREATE:
523         {
524             BOOL got_getminmaxinfo = GetWindowLongA(hwnd, GWL_USERDATA) == 0x20031021;
525             CREATESTRUCTA *cs = (CREATESTRUCTA *)lparam;
526
527             trace("WM_NCCREATE: hwnd %p, parent %p, style %08lx\n", hwnd, cs->hwndParent, cs->style);
528             if (got_getminmaxinfo)
529                 trace("%p got WM_GETMINMAXINFO\n", hwnd);
530
531             if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
532                 ok(got_getminmaxinfo, "tool: WM_GETMINMAXINFO should have been received before WM_NCCREATE\n");
533             else
534                 ok(!got_getminmaxinfo, "tool: WM_GETMINMAXINFO should NOT have been received before WM_NCCREATE\n");
535             break;
536         }
537     }
538
539     return DefWindowProcA(hwnd, msg, wparam, lparam);
540 }
541
542 static BOOL RegisterWindowClasses(void)
543 {
544     WNDCLASSA cls;
545
546     cls.style = 0;
547     cls.lpfnWndProc = main_window_procA;
548     cls.cbClsExtra = 0;
549     cls.cbWndExtra = 0;
550     cls.hInstance = GetModuleHandleA(0);
551     cls.hIcon = 0;
552     cls.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
553     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
554     cls.lpszMenuName = NULL;
555     cls.lpszClassName = "MainWindowClass";
556
557     if(!RegisterClassA(&cls)) return FALSE;
558
559     cls.style = 0;
560     cls.lpfnWndProc = tool_window_procA;
561     cls.cbClsExtra = 0;
562     cls.cbWndExtra = 0;
563     cls.hInstance = GetModuleHandleA(0);
564     cls.hIcon = 0;
565     cls.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
566     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
567     cls.lpszMenuName = NULL;
568     cls.lpszClassName = "ToolWindowClass";
569
570     if(!RegisterClassA(&cls)) return FALSE;
571
572     return TRUE;
573 }
574
575 static void verify_window_info(HWND hwnd, const WINDOWINFO *info, BOOL test_borders)
576 {
577     RECT rcWindow, rcClient;
578     INT border;
579     DWORD status;
580
581     ok(IsWindow(hwnd), "bad window handle\n");
582
583     GetWindowRect(hwnd, &rcWindow);
584     ok(EqualRect(&rcWindow, &info->rcWindow), "wrong rcWindow\n");
585
586     GetClientRect(hwnd, &rcClient);
587     /* translate to screen coordinates */
588     MapWindowPoints(hwnd, 0, (LPPOINT)&rcClient, 2);
589     ok(EqualRect(&rcClient, &info->rcClient), "wrong rcClient\n");
590
591     ok(info->dwStyle == GetWindowLongA(hwnd, GWL_STYLE), "wrong dwStyle\n");
592     ok(info->dwExStyle == GetWindowLongA(hwnd, GWL_EXSTYLE), "wrong dwExStyle\n");
593     status = (GetActiveWindow() == hwnd) ? WS_ACTIVECAPTION : 0;
594     ok(info->dwWindowStatus == status, "wrong dwWindowStatus\n");
595
596     if (test_borders && !IsRectEmpty(&rcWindow))
597     {
598         trace("rcWindow: %ld,%ld - %ld,%ld\n", rcWindow.left, rcWindow.top, rcWindow.right, rcWindow.bottom);
599         trace("rcClient: %ld,%ld - %ld,%ld\n", rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
600
601         ok(info->cxWindowBorders == rcClient.left - rcWindow.left,
602             "wrong cxWindowBorders %d != %ld\n", info->cxWindowBorders, rcClient.left - rcWindow.left);
603         border = min(rcWindow.bottom - rcClient.bottom, rcClient.top - rcWindow.top);
604         ok(info->cyWindowBorders == border,
605            "wrong cyWindowBorders %d != %d\n", info->cyWindowBorders, border);
606     }
607
608     ok(info->atomWindowType == GetClassLongA(hwnd, GCW_ATOM), "wrong atomWindowType\n");
609     ok(info->wCreatorVersion == 0x0400, "wrong wCreatorVersion %04x\n", info->wCreatorVersion);
610 }
611
612 static LRESULT CALLBACK cbt_hook_proc(int nCode, WPARAM wParam, LPARAM lParam) 
613
614     static const char *CBT_code_name[10] = {
615         "HCBT_MOVESIZE",
616         "HCBT_MINMAX",
617         "HCBT_QS",
618         "HCBT_CREATEWND",
619         "HCBT_DESTROYWND",
620         "HCBT_ACTIVATE",
621         "HCBT_CLICKSKIPPED",
622         "HCBT_KEYSKIPPED",
623         "HCBT_SYSCOMMAND",
624         "HCBT_SETFOCUS" };
625     const char *code_name = (nCode >= 0 && nCode <= HCBT_SETFOCUS) ? CBT_code_name[nCode] : "Unknown";
626
627     trace("CBT: %d (%s), %08x, %08lx\n", nCode, code_name, wParam, lParam);
628
629     switch (nCode)
630     {
631         case HCBT_CREATEWND:
632         {
633 #if 0 /* Uncomment this once the test succeeds in all cases */
634             static const RECT rc_null;
635             RECT rc;
636 #endif
637             LONG style;
638             CBT_CREATEWNDA *createwnd = (CBT_CREATEWNDA *)lParam;
639             trace("HCBT_CREATEWND: hwnd %p, parent %p, style %08lx\n",
640                   (HWND)wParam, createwnd->lpcs->hwndParent, createwnd->lpcs->style);
641             ok(createwnd->hwndInsertAfter == HWND_TOP, "hwndInsertAfter should be always HWND_TOP\n");
642
643             /* WS_VISIBLE should be turned off yet */
644             style = createwnd->lpcs->style & ~WS_VISIBLE;
645             ok(style == GetWindowLongA((HWND)wParam, GWL_STYLE),
646                 "style of hwnd and style in the CREATESTRUCT do not match: %08lx != %08lx\n",
647                 GetWindowLongA((HWND)wParam, GWL_STYLE), style);
648
649 #if 0 /* Uncomment this once the test succeeds in all cases */
650             if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD)
651             {
652                 ok(GetParent((HWND)wParam) == hwndMessage,
653                    "wrong result from GetParent %p: message window %p\n",
654                    GetParent((HWND)wParam), hwndMessage);
655             }
656             else
657                 ok(!GetParent((HWND)wParam), "GetParent should return 0 at this point\n");
658
659             ok(!GetWindow((HWND)wParam, GW_OWNER), "GW_OWNER should be set to 0 at this point\n");
660 #endif
661 #if 0       /* while NT assigns GW_HWNDFIRST/LAST some values at this point,
662              * Win9x still has them set to 0.
663              */
664             ok(GetWindow((HWND)wParam, GW_HWNDFIRST) != 0, "GW_HWNDFIRST should not be set to 0 at this point\n");
665             ok(GetWindow((HWND)wParam, GW_HWNDLAST) != 0, "GW_HWNDLAST should not be set to 0 at this point\n");
666 #endif
667             ok(!GetWindow((HWND)wParam, GW_HWNDPREV), "GW_HWNDPREV should be set to 0 at this point\n");
668             ok(!GetWindow((HWND)wParam, GW_HWNDNEXT), "GW_HWNDNEXT should be set to 0 at this point\n");
669
670 #if 0 /* Uncomment this once the test succeeds in all cases */
671             if (pGetAncestor)
672             {
673                 ok(pGetAncestor((HWND)wParam, GA_PARENT) == hwndMessage, "GA_PARENT should be set to hwndMessage at this point\n");
674                 ok(pGetAncestor((HWND)wParam, GA_ROOT) == (HWND)wParam,
675                    "GA_ROOT is set to %p, expected %p\n", pGetAncestor((HWND)wParam, GA_ROOT), (HWND)wParam);
676
677                 if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD)
678                     ok(pGetAncestor((HWND)wParam, GA_ROOTOWNER) == hwndMessage,
679                        "GA_ROOTOWNER should be set to hwndMessage at this point\n");
680                 else
681                     ok(pGetAncestor((HWND)wParam, GA_ROOTOWNER) == (HWND)wParam,
682                        "GA_ROOTOWNER is set to %p, expected %p\n", pGetAncestor((HWND)wParam, GA_ROOTOWNER), (HWND)wParam);
683             }
684
685             ok(GetWindowRect((HWND)wParam, &rc), "GetWindowRect failed\n");
686             ok(EqualRect(&rc, &rc_null), "window rect should be set to 0 HCBT_CREATEWND\n");
687             ok(GetClientRect((HWND)wParam, &rc), "GetClientRect failed\n");
688             ok(EqualRect(&rc, &rc_null), "client rect should be set to 0 on HCBT_CREATEWND\n");
689 #endif
690             break;
691         }
692
693         case HCBT_DESTROYWND:
694         case HCBT_SETFOCUS:
695             if (wParam && pGetWindowInfo)
696             {
697                 WINDOWINFO info;
698
699                 info.cbSize = 0;
700                 ok(pGetWindowInfo((HWND)wParam, &info), "GetWindowInfo should not fail\n");
701                 /* win2k SP4 returns broken border info if GetWindowInfo
702                  * is being called from HCBT_DESTROYWND hook proc.
703                  */
704                 verify_window_info((HWND)wParam, &info, nCode != HCBT_DESTROYWND);
705
706                 info.cbSize = sizeof(WINDOWINFO) + 1;
707                 ok(pGetWindowInfo((HWND)wParam, &info), "GetWindowInfo should not fail\n");
708                 verify_window_info((HWND)wParam, &info, nCode != HCBT_DESTROYWND);
709             }
710             break;
711     }
712
713     return CallNextHookEx(hhook, nCode, wParam, lParam);
714 }
715
716 static void test_shell_window()
717 {
718     BOOL ret;
719     DWORD error;
720     HMODULE hinst, hUser32;
721     BOOL (WINAPI*SetShellWindow)(HWND);
722     BOOL (WINAPI*SetShellWindowEx)(HWND, HWND);
723     HWND hwnd1, hwnd2, hwnd3, hwnd4, hwnd5;
724     HWND shellWindow, nextWnd;
725
726     shellWindow = GetShellWindow();
727     hinst = GetModuleHandle(0);
728     hUser32 = GetModuleHandleA("user32");
729
730     SetShellWindow = (void *)GetProcAddress(hUser32, "SetShellWindow");
731     SetShellWindowEx = (void *)GetProcAddress(hUser32, "SetShellWindowEx");
732
733     trace("previous shell window: %p\n", shellWindow);
734
735     if (shellWindow) {
736         DWORD pid;
737         HANDLE hProcess;
738
739         ret = DestroyWindow(shellWindow);
740         error = GetLastError();
741
742         ok(!ret, "DestroyWindow(shellWindow)\n");
743         /* passes on Win XP, but not on Win98
744         ok(error==ERROR_ACCESS_DENIED, "ERROR_ACCESS_DENIED after DestroyWindow(shellWindow)\n"); */
745
746         /* close old shell instance */
747         GetWindowThreadProcessId(shellWindow, &pid);
748         hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
749         ret = TerminateProcess(hProcess, 0);
750         ok(ret, "termination of previous shell process failed: GetLastError()=%ld\n", GetLastError());
751         WaitForSingleObject(hProcess, INFINITE);    /* wait for termination */
752         CloseHandle(hProcess);
753     }
754
755     hwnd1 = CreateWindowEx(0, TEXT("#32770"), TEXT("TEST1"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 100, 100, 300, 200, 0, 0, hinst, 0);
756     trace("created window 1: %p\n", hwnd1);
757
758     ret = SetShellWindow(hwnd1);
759     ok(ret, "first call to SetShellWindow(hwnd1)\n");
760     shellWindow = GetShellWindow();
761     ok(shellWindow==hwnd1, "wrong shell window: %p\n", shellWindow);
762
763     ret = SetShellWindow(hwnd1);
764     ok(!ret, "second call to SetShellWindow(hwnd1)\n");
765
766     ret = SetShellWindow(0);
767     error = GetLastError();
768     /* passes on Win XP, but not on Win98
769     ok(!ret, "reset shell window by SetShellWindow(0)\n");
770     ok(error==ERROR_INVALID_WINDOW_HANDLE, "ERROR_INVALID_WINDOW_HANDLE after SetShellWindow(0)\n"); */
771
772     ret = SetShellWindow(hwnd1);
773     /* passes on Win XP, but not on Win98
774     ok(!ret, "third call to SetShellWindow(hwnd1)\n"); */
775
776     todo_wine
777     {
778         SetWindowLong(hwnd1, GWL_EXSTYLE, GetWindowLong(hwnd1,GWL_EXSTYLE)|WS_EX_TOPMOST);
779         ret = GetWindowLong(hwnd1,GWL_EXSTYLE)&WS_EX_TOPMOST? TRUE: FALSE;
780         ok(!ret, "SetWindowExStyle(hwnd1, WS_EX_TOPMOST)\n");
781     }
782
783     ret = DestroyWindow(hwnd1);
784     ok(ret, "DestroyWindow(hwnd1)\n");
785
786     hwnd2 = CreateWindowEx(WS_EX_TOPMOST, TEXT("#32770"), TEXT("TEST2"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 150, 250, 300, 200, 0, 0, hinst, 0);
787     trace("created window 2: %p\n", hwnd2);
788     ret = SetShellWindow(hwnd2);
789     ok(!ret, "SetShellWindow(hwnd2) with WS_EX_TOPMOST\n");
790
791     hwnd3 = CreateWindowEx(0, TEXT("#32770"), TEXT("TEST3"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 200, 400, 300, 200, 0, 0, hinst, 0);
792     trace("created window 3: %p\n", hwnd3);
793
794     hwnd4 = CreateWindowEx(0, TEXT("#32770"), TEXT("TEST4"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 250, 500, 300, 200, 0, 0, hinst, 0);
795     trace("created window 4: %p\n", hwnd4);
796
797     nextWnd = GetWindow(hwnd4, GW_HWNDNEXT);
798     ok(nextWnd==hwnd3, "wrong next window for hwnd4: %p - expected hwnd3\n", nextWnd);
799
800     ret = SetShellWindow(hwnd4);
801     ok(ret, "SetShellWindow(hwnd4)\n");
802     shellWindow = GetShellWindow();
803     ok(shellWindow==hwnd4, "wrong shell window: %p - expected hwnd4\n", shellWindow);
804
805     nextWnd = GetWindow(hwnd4, GW_HWNDNEXT);
806     ok(nextWnd==0, "wrong next window for hwnd4: %p - expected 0\n", nextWnd);
807
808     ret = SetWindowPos(hwnd4, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
809     ok(ret, "SetWindowPos(hwnd4, HWND_TOPMOST)\n");
810
811     ret = SetWindowPos(hwnd4, hwnd3, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
812     ok(ret, "SetWindowPos(hwnd4, hwnd3\n");
813
814     ret = SetShellWindow(hwnd3);
815     ok(!ret, "SetShellWindow(hwnd3)\n");
816     shellWindow = GetShellWindow();
817     ok(shellWindow==hwnd4, "wrong shell window: %p - expected hwnd4\n", shellWindow);
818
819     hwnd5 = CreateWindowEx(0, TEXT("#32770"), TEXT("TEST5"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 300, 600, 300, 200, 0, 0, hinst, 0);
820     trace("created window 5: %p\n", hwnd5);
821     ret = SetWindowPos(hwnd4, hwnd5, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
822     ok(ret, "SetWindowPos(hwnd4, hwnd5)\n");
823
824     todo_wine
825     {
826         nextWnd = GetWindow(hwnd4, GW_HWNDNEXT);
827         ok(nextWnd==0, "wrong next window for hwnd4 after SetWindowPos(): %p - expected 0\n", nextWnd);
828     }
829
830     /* destroy test windows */
831     DestroyWindow(hwnd2);
832     DestroyWindow(hwnd3);
833     DestroyWindow(hwnd4);
834     DestroyWindow(hwnd5);
835 }
836
837 /************** MDI test ****************/
838
839 static const char mdi_lParam_test_message[] = "just a test string";
840
841 static void test_MDI_create(HWND parent, HWND mdi_client)
842 {
843     MDICREATESTRUCTA mdi_cs;
844     HWND mdi_child;
845     static const WCHAR classW[] = {'M','D','I','_','c','h','i','l','d','_','C','l','a','s','s','_','1',0};
846     static const WCHAR titleW[] = {'M','D','I',' ','c','h','i','l','d',0};
847     BOOL isWin9x = FALSE;
848
849     mdi_cs.szClass = "MDI_child_Class_1";
850     mdi_cs.szTitle = "MDI child";
851     mdi_cs.hOwner = GetModuleHandle(0);
852     mdi_cs.x = CW_USEDEFAULT;
853     mdi_cs.y = CW_USEDEFAULT;
854     mdi_cs.cx = CW_USEDEFAULT;
855     mdi_cs.cy = CW_USEDEFAULT;
856     mdi_cs.style = 0;
857     mdi_cs.lParam = (LPARAM)mdi_lParam_test_message;
858     mdi_child = (HWND)SendMessageA(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
859     ok(mdi_child != 0, "MDI child creation failed\n");
860     DestroyWindow(mdi_child);
861
862     mdi_cs.style = 0x7fffffff; /* without WS_POPUP */
863     mdi_child = (HWND)SendMessageA(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
864     ok(mdi_child != 0, "MDI child creation failed\n");
865     DestroyWindow(mdi_child);
866
867     mdi_cs.style = 0xffffffff; /* with WS_POPUP */
868     mdi_child = (HWND)SendMessageA(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
869     if (GetWindowLongA(mdi_client, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
870     {
871         ok(!mdi_child, "MDI child with WS_POPUP and with MDIS_ALLCHILDSTYLES should fail\n");
872         DestroyWindow(mdi_child);
873     }
874     else
875         ok(mdi_child != 0, "MDI child creation failed\n");
876
877     /* test MDICREATESTRUCT A<->W mapping */
878     /* MDICREATESTRUCTA and MDICREATESTRUCTW have the same layout */
879     mdi_cs.style = 0;
880     mdi_cs.szClass = (LPCSTR)classW;
881     mdi_cs.szTitle = (LPCSTR)titleW;
882     SetLastError(0xdeadbeef);
883     mdi_child = (HWND)SendMessageW(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
884     if (!mdi_child)
885     {
886         if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
887             isWin9x = TRUE;
888         else
889             ok(mdi_child != 0, "MDI child creation failed\n");
890     }
891     DestroyWindow(mdi_child);
892
893     mdi_child = CreateMDIWindowA("MDI_child_Class_1", "MDI child",
894                                  0,
895                                  CW_USEDEFAULT, CW_USEDEFAULT,
896                                  CW_USEDEFAULT, CW_USEDEFAULT,
897                                  mdi_client, GetModuleHandle(0),
898                                  (LPARAM)mdi_lParam_test_message);
899     ok(mdi_child != 0, "MDI child creation failed\n");
900     DestroyWindow(mdi_child);
901
902     mdi_child = CreateMDIWindowA("MDI_child_Class_1", "MDI child",
903                                  0x7fffffff, /* without WS_POPUP */
904                                  CW_USEDEFAULT, CW_USEDEFAULT,
905                                  CW_USEDEFAULT, CW_USEDEFAULT,
906                                  mdi_client, GetModuleHandle(0),
907                                  (LPARAM)mdi_lParam_test_message);
908     ok(mdi_child != 0, "MDI child creation failed\n");
909     DestroyWindow(mdi_child);
910
911     mdi_child = CreateMDIWindowA("MDI_child_Class_1", "MDI child",
912                                  0xffffffff, /* with WS_POPUP */
913                                  CW_USEDEFAULT, CW_USEDEFAULT,
914                                  CW_USEDEFAULT, CW_USEDEFAULT,
915                                  mdi_client, GetModuleHandle(0),
916                                  (LPARAM)mdi_lParam_test_message);
917     if (GetWindowLongA(mdi_client, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
918     {
919         ok(!mdi_child, "MDI child with WS_POPUP and with MDIS_ALLCHILDSTYLES should fail\n");
920         DestroyWindow(mdi_child);
921     }
922     else
923         ok(mdi_child != 0, "MDI child creation failed\n");
924
925     /* test MDICREATESTRUCT A<->W mapping */
926     SetLastError(0xdeadbeef);
927     mdi_child = CreateMDIWindowW(classW, titleW,
928                                  0,
929                                  CW_USEDEFAULT, CW_USEDEFAULT,
930                                  CW_USEDEFAULT, CW_USEDEFAULT,
931                                  mdi_client, GetModuleHandle(0),
932                                  (LPARAM)mdi_lParam_test_message);
933     if (!mdi_child)
934     {
935         if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
936             isWin9x = TRUE;
937         else
938             ok(mdi_child != 0, "MDI child creation failed\n");
939     }
940     DestroyWindow(mdi_child);
941
942     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_1", "MDI child",
943                                 0,
944                                 CW_USEDEFAULT, CW_USEDEFAULT,
945                                 CW_USEDEFAULT, CW_USEDEFAULT,
946                                 mdi_client, 0, GetModuleHandle(0),
947                                 (LPVOID)mdi_lParam_test_message);
948     ok(mdi_child != 0, "MDI child creation failed\n");
949     DestroyWindow(mdi_child);
950
951     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_1", "MDI child",
952                                 0x7fffffff, /* without WS_POPUP */
953                                 CW_USEDEFAULT, CW_USEDEFAULT,
954                                 CW_USEDEFAULT, CW_USEDEFAULT,
955                                 mdi_client, 0, GetModuleHandle(0),
956                                 (LPVOID)mdi_lParam_test_message);
957     ok(mdi_child != 0, "MDI child creation failed\n");
958     DestroyWindow(mdi_child);
959
960     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_1", "MDI child",
961                                 0xffffffff, /* with WS_POPUP */
962                                 CW_USEDEFAULT, CW_USEDEFAULT,
963                                 CW_USEDEFAULT, CW_USEDEFAULT,
964                                 mdi_client, 0, GetModuleHandle(0),
965                                 (LPVOID)mdi_lParam_test_message);
966     if (GetWindowLongA(mdi_client, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
967     {
968         ok(!mdi_child, "MDI child with WS_POPUP and with MDIS_ALLCHILDSTYLES should fail\n");
969         DestroyWindow(mdi_child);
970     }
971     else
972         ok(mdi_child != 0, "MDI child creation failed\n");
973
974     /* test MDICREATESTRUCT A<->W mapping */
975     SetLastError(0xdeadbeef);
976     mdi_child = CreateWindowExW(WS_EX_MDICHILD, classW, titleW,
977                                 0,
978                                 CW_USEDEFAULT, CW_USEDEFAULT,
979                                 CW_USEDEFAULT, CW_USEDEFAULT,
980                                 mdi_client, 0, GetModuleHandle(0),
981                                 (LPVOID)mdi_lParam_test_message);
982     if (!mdi_child)
983     {
984         if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
985             isWin9x = TRUE;
986         else
987             ok(mdi_child != 0, "MDI child creation failed\n");
988     }
989     DestroyWindow(mdi_child);
990
991     /* This test fails on Win9x */
992     if (!isWin9x)
993     {
994         mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_2", "MDI child",
995                                 WS_CHILD,
996                                 CW_USEDEFAULT, CW_USEDEFAULT,
997                                 CW_USEDEFAULT, CW_USEDEFAULT,
998                                 parent, 0, GetModuleHandle(0),
999                                 (LPVOID)mdi_lParam_test_message);
1000         ok(!mdi_child, "WS_EX_MDICHILD with a not MDIClient parent should fail\n");
1001     }
1002
1003     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1004                                 WS_CHILD, /* without WS_POPUP */
1005                                 CW_USEDEFAULT, CW_USEDEFAULT,
1006                                 CW_USEDEFAULT, CW_USEDEFAULT,
1007                                 mdi_client, 0, GetModuleHandle(0),
1008                                 (LPVOID)mdi_lParam_test_message);
1009     ok(mdi_child != 0, "MDI child creation failed\n");
1010     DestroyWindow(mdi_child);
1011
1012     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1013                                 WS_CHILD | WS_POPUP, /* with WS_POPUP */
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     /* maximized child */
1022     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1023                                 WS_CHILD | WS_MAXIMIZE,
1024                                 CW_USEDEFAULT, CW_USEDEFAULT,
1025                                 CW_USEDEFAULT, CW_USEDEFAULT,
1026                                 mdi_client, 0, GetModuleHandle(0),
1027                                 (LPVOID)mdi_lParam_test_message);
1028     ok(mdi_child != 0, "MDI child creation failed\n");
1029     DestroyWindow(mdi_child);
1030
1031     trace("Creating maximized child with a caption\n");
1032     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1033                                 WS_CHILD | WS_MAXIMIZE | WS_CAPTION,
1034                                 CW_USEDEFAULT, CW_USEDEFAULT,
1035                                 CW_USEDEFAULT, CW_USEDEFAULT,
1036                                 mdi_client, 0, GetModuleHandle(0),
1037                                 (LPVOID)mdi_lParam_test_message);
1038     ok(mdi_child != 0, "MDI child creation failed\n");
1039     DestroyWindow(mdi_child);
1040
1041     trace("Creating maximized child with a caption and a thick frame\n");
1042     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1043                                 WS_CHILD | WS_MAXIMIZE | WS_CAPTION | WS_THICKFRAME,
1044                                 CW_USEDEFAULT, CW_USEDEFAULT,
1045                                 CW_USEDEFAULT, CW_USEDEFAULT,
1046                                 mdi_client, 0, GetModuleHandle(0),
1047                                 (LPVOID)mdi_lParam_test_message);
1048     ok(mdi_child != 0, "MDI child creation failed\n");
1049     DestroyWindow(mdi_child);
1050 }
1051
1052 /**********************************************************************
1053  * MDI_ChildGetMinMaxInfo (copied from windows/mdi.c)
1054  *
1055  * Note: The rule here is that client rect of the maximized MDI child
1056  *       is equal to the client rect of the MDI client window.
1057  */
1058 static void MDI_ChildGetMinMaxInfo( HWND client, HWND hwnd, MINMAXINFO* lpMinMax )
1059 {
1060     RECT rect;
1061
1062     GetClientRect( client, &rect );
1063     AdjustWindowRectEx( &rect, GetWindowLongA( hwnd, GWL_STYLE ),
1064                         0, GetWindowLongA( hwnd, GWL_EXSTYLE ));
1065
1066     rect.right -= rect.left;
1067     rect.bottom -= rect.top;
1068     lpMinMax->ptMaxSize.x = rect.right;
1069     lpMinMax->ptMaxSize.y = rect.bottom;
1070
1071     lpMinMax->ptMaxPosition.x = rect.left;
1072     lpMinMax->ptMaxPosition.y = rect.top;
1073
1074     trace("max rect (%ld,%ld - %ld, %ld)\n",
1075            rect.left, rect.top, rect.right, rect.bottom);
1076 }
1077
1078 static LRESULT WINAPI mdi_child_wnd_proc_1(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1079 {
1080     switch (msg)
1081     {
1082         case WM_NCCREATE:
1083         case WM_CREATE:
1084         {
1085             CREATESTRUCTA *cs = (CREATESTRUCTA *)lparam;
1086             MDICREATESTRUCTA *mdi_cs = (MDICREATESTRUCTA *)cs->lpCreateParams;
1087
1088             ok(cs->dwExStyle & WS_EX_MDICHILD, "WS_EX_MDICHILD should be set\n");
1089             ok(mdi_cs->lParam == (LPARAM)mdi_lParam_test_message, "wrong mdi_cs->lParam\n");
1090
1091             ok(!lstrcmpA(cs->lpszClass, "MDI_child_Class_1"), "wrong class name\n");
1092             ok(!lstrcmpA(cs->lpszClass, mdi_cs->szClass), "class name does not match\n");
1093             ok(!lstrcmpA(cs->lpszName, "MDI child"), "wrong title\n");
1094             ok(!lstrcmpA(cs->lpszName, mdi_cs->szTitle), "title does not match\n");
1095             ok(cs->hInstance == mdi_cs->hOwner, "%p != %p\n", cs->hInstance, mdi_cs->hOwner);
1096
1097             /* MDICREATESTRUCT should have original values */
1098             ok(mdi_cs->style == 0 || mdi_cs->style == 0x7fffffff || mdi_cs->style == 0xffffffff,
1099                 "mdi_cs->style does not match (%08lx)\n", mdi_cs->style);
1100             ok(mdi_cs->x == CW_USEDEFAULT, "%d != CW_USEDEFAULT\n", mdi_cs->x);
1101             ok(mdi_cs->y == CW_USEDEFAULT, "%d != CW_USEDEFAULT\n", mdi_cs->y);
1102             ok(mdi_cs->cx == CW_USEDEFAULT, "%d != CW_USEDEFAULT\n", mdi_cs->cx);
1103             ok(mdi_cs->cy == CW_USEDEFAULT, "%d != CW_USEDEFAULT\n", mdi_cs->cy);
1104
1105             /* CREATESTRUCT should have fixed values */
1106             ok(cs->x != CW_USEDEFAULT, "%d == CW_USEDEFAULT\n", cs->x);
1107             ok(cs->y != CW_USEDEFAULT, "%d == CW_USEDEFAULT\n", cs->y);
1108
1109             /* cx/cy == CW_USEDEFAULT are translated to NOT zero values */
1110             ok(cs->cx != CW_USEDEFAULT && cs->cx != 0, "%d == CW_USEDEFAULT\n", cs->cx);
1111             ok(cs->cy != CW_USEDEFAULT && cs->cy != 0, "%d == CW_USEDEFAULT\n", cs->cy);
1112
1113             ok(!(cs->style & WS_POPUP), "WS_POPUP is not allowed\n");
1114
1115             if (GetWindowLongA(cs->hwndParent, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1116             {
1117                 LONG style = mdi_cs->style | WS_CHILD | WS_CLIPSIBLINGS;
1118                 ok(cs->style == style,
1119                    "cs->style does not match (%08lx)\n", cs->style);
1120             }
1121             else
1122             {
1123                 LONG style = mdi_cs->style;
1124                 style &= ~WS_POPUP;
1125                 style |= WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CAPTION |
1126                     WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
1127                 ok(cs->style == style,
1128                    "cs->style does not match (%08lx)\n", cs->style);
1129             }
1130             break;
1131         }
1132
1133         case WM_GETMINMAXINFO:
1134         {
1135             HWND client = GetParent(hwnd);
1136             RECT rc;
1137             MINMAXINFO *minmax = (MINMAXINFO *)lparam;
1138             MINMAXINFO my_minmax;
1139             LONG style, exstyle;
1140
1141             style = GetWindowLongA(hwnd, GWL_STYLE);
1142             exstyle = GetWindowLongA(hwnd, GWL_EXSTYLE);
1143
1144             GetWindowRect(client, &rc);
1145             trace("MDI client %p window size = (%ld x %ld)\n", client, rc.right-rc.left, rc.bottom-rc.top);
1146             GetClientRect(client, &rc);
1147             trace("MDI client %p client size = (%ld x %ld)\n", client, rc.right, rc.bottom);
1148             trace("screen size: %d x %d\n", GetSystemMetrics(SM_CXSCREEN),
1149                                             GetSystemMetrics(SM_CYSCREEN));
1150
1151             GetClientRect(client, &rc);
1152             if ((style & WS_CAPTION) == WS_CAPTION)
1153                 style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
1154             AdjustWindowRectEx(&rc, style, 0, exstyle);
1155             trace("MDI child: calculated max window size = (%ld x %ld)\n", rc.right-rc.left, rc.bottom-rc.top);
1156
1157             trace("ptReserved = (%ld,%ld)\n"
1158                   "ptMaxSize = (%ld,%ld)\n"
1159                   "ptMaxPosition = (%ld,%ld)\n"
1160                   "ptMinTrackSize = (%ld,%ld)\n"
1161                   "ptMaxTrackSize = (%ld,%ld)\n",
1162                   minmax->ptReserved.x, minmax->ptReserved.y,
1163                   minmax->ptMaxSize.x, minmax->ptMaxSize.y,
1164                   minmax->ptMaxPosition.x, minmax->ptMaxPosition.y,
1165                   minmax->ptMinTrackSize.x, minmax->ptMinTrackSize.y,
1166                   minmax->ptMaxTrackSize.x, minmax->ptMaxTrackSize.y);
1167
1168             ok(minmax->ptMaxSize.x == rc.right - rc.left, "default width of maximized child %ld != %ld\n",
1169                minmax->ptMaxSize.x, rc.right - rc.left);
1170             ok(minmax->ptMaxSize.y == rc.bottom - rc.top, "default height of maximized child %ld != %ld\n",
1171                minmax->ptMaxSize.y, rc.bottom - rc.top);
1172
1173             DefMDIChildProcA(hwnd, msg, wparam, lparam);
1174
1175             trace("DefMDIChildProc returned:\n"
1176                   "ptReserved = (%ld,%ld)\n"
1177                   "ptMaxSize = (%ld,%ld)\n"
1178                   "ptMaxPosition = (%ld,%ld)\n"
1179                   "ptMinTrackSize = (%ld,%ld)\n"
1180                   "ptMaxTrackSize = (%ld,%ld)\n",
1181                   minmax->ptReserved.x, minmax->ptReserved.y,
1182                   minmax->ptMaxSize.x, minmax->ptMaxSize.y,
1183                   minmax->ptMaxPosition.x, minmax->ptMaxPosition.y,
1184                   minmax->ptMinTrackSize.x, minmax->ptMinTrackSize.y,
1185                   minmax->ptMaxTrackSize.x, minmax->ptMaxTrackSize.y);
1186
1187             MDI_ChildGetMinMaxInfo(client, hwnd, &my_minmax);
1188             ok(minmax->ptMaxSize.x == my_minmax.ptMaxSize.x, "default width of maximized child %ld != %ld\n",
1189                minmax->ptMaxSize.x, my_minmax.ptMaxSize.x);
1190             ok(minmax->ptMaxSize.y == my_minmax.ptMaxSize.y, "default height of maximized child %ld != %ld\n",
1191                minmax->ptMaxSize.y, my_minmax.ptMaxSize.y);
1192
1193             return 1;
1194         }
1195     }
1196     return DefMDIChildProcA(hwnd, msg, wparam, lparam);
1197 }
1198
1199 static LRESULT WINAPI mdi_child_wnd_proc_2(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1200 {
1201     switch (msg)
1202     {
1203         case WM_NCCREATE:
1204         case WM_CREATE:
1205         {
1206             CREATESTRUCTA *cs = (CREATESTRUCTA *)lparam;
1207
1208             trace("%s\n", (msg == WM_NCCREATE) ? "WM_NCCREATE" : "WM_CREATE");
1209             trace("x %d, y %d, cx %d, cy %d\n", cs->x, cs->y, cs->cx, cs->cy);
1210
1211             ok(!(cs->dwExStyle & WS_EX_MDICHILD), "WS_EX_MDICHILD should not be set\n");
1212             ok(cs->lpCreateParams == mdi_lParam_test_message, "wrong cs->lpCreateParams\n");
1213
1214             ok(!lstrcmpA(cs->lpszClass, "MDI_child_Class_2"), "wrong class name\n");
1215             ok(!lstrcmpA(cs->lpszName, "MDI child"), "wrong title\n");
1216
1217             /* CREATESTRUCT should have fixed values */
1218             /* For some reason Win9x doesn't translate cs->x from CW_USEDEFAULT,
1219                while NT does. */
1220             /*ok(cs->x != CW_USEDEFAULT, "%d == CW_USEDEFAULT\n", cs->x);*/
1221             ok(cs->y != CW_USEDEFAULT, "%d == CW_USEDEFAULT\n", cs->y);
1222
1223             /* cx/cy == CW_USEDEFAULT are translated to 0 */
1224             ok(cs->cx == 0, "%d != 0\n", cs->cx);
1225             ok(cs->cy == 0, "%d != 0\n", cs->cy);
1226             break;
1227         }
1228
1229         case WM_GETMINMAXINFO:
1230         {
1231             HWND parent = GetParent(hwnd);
1232             RECT rc;
1233             MINMAXINFO *minmax = (MINMAXINFO *)lparam;
1234             LONG style, exstyle;
1235
1236             trace("WM_GETMINMAXINFO\n");
1237
1238             style = GetWindowLongA(hwnd, GWL_STYLE);
1239             exstyle = GetWindowLongA(hwnd, GWL_EXSTYLE);
1240
1241             GetClientRect(parent, &rc);
1242             trace("parent %p client size = (%ld x %ld)\n", parent, rc.right, rc.bottom);
1243
1244             GetClientRect(parent, &rc);
1245             if ((style & WS_CAPTION) == WS_CAPTION)
1246                 style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
1247             AdjustWindowRectEx(&rc, style, 0, exstyle);
1248             trace("calculated max child window size = (%ld x %ld)\n", rc.right-rc.left, rc.bottom-rc.top);
1249
1250             trace("ptReserved = (%ld,%ld)\n"
1251                   "ptMaxSize = (%ld,%ld)\n"
1252                   "ptMaxPosition = (%ld,%ld)\n"
1253                   "ptMinTrackSize = (%ld,%ld)\n"
1254                   "ptMaxTrackSize = (%ld,%ld)\n",
1255                   minmax->ptReserved.x, minmax->ptReserved.y,
1256                   minmax->ptMaxSize.x, minmax->ptMaxSize.y,
1257                   minmax->ptMaxPosition.x, minmax->ptMaxPosition.y,
1258                   minmax->ptMinTrackSize.x, minmax->ptMinTrackSize.y,
1259                   minmax->ptMaxTrackSize.x, minmax->ptMaxTrackSize.y);
1260
1261             ok(minmax->ptMaxSize.x == rc.right - rc.left, "default width of maximized child %ld != %ld\n",
1262                minmax->ptMaxSize.x, rc.right - rc.left);
1263             ok(minmax->ptMaxSize.y == rc.bottom - rc.top, "default height of maximized child %ld != %ld\n",
1264                minmax->ptMaxSize.y, rc.bottom - rc.top);
1265             break;
1266         }
1267
1268         case WM_WINDOWPOSCHANGING:
1269         case WM_WINDOWPOSCHANGED:
1270         {
1271             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
1272             WINDOWPOS my_winpos = *winpos;
1273
1274             trace("%s\n", (msg == WM_WINDOWPOSCHANGING) ? "WM_WINDOWPOSCHANGING" : "WM_WINDOWPOSCHANGED");
1275             trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
1276                   winpos->hwnd, winpos->hwndInsertAfter,
1277                   winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
1278
1279             DefWindowProcA(hwnd, msg, wparam, lparam);
1280
1281             trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
1282                   winpos->hwnd, winpos->hwndInsertAfter,
1283                   winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
1284
1285             ok(!memcmp(&my_winpos, winpos, sizeof(WINDOWPOS)),
1286                "DefWindowProc should not change WINDOWPOS values\n");
1287
1288             return 1;
1289         }
1290     }
1291     return DefWindowProcA(hwnd, msg, wparam, lparam);
1292 }
1293
1294 static LRESULT WINAPI mdi_main_wnd_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1295 {
1296     static HWND mdi_client;
1297
1298     switch (msg)
1299     {
1300         case WM_CREATE:
1301         {
1302             CLIENTCREATESTRUCT client_cs;
1303             RECT rc;
1304
1305             GetClientRect(hwnd, &rc);
1306
1307             client_cs.hWindowMenu = 0;
1308             client_cs.idFirstChild = 1;
1309
1310             /* MDIClient without MDIS_ALLCHILDSTYLES */
1311             mdi_client = CreateWindowExA(0, "mdiclient",
1312                                          NULL,
1313                                          WS_CHILD /*| WS_VISIBLE*/,
1314                                           /* tests depend on a not zero MDIClient size */
1315                                          0, 0, rc.right, rc.bottom,
1316                                          hwnd, 0, GetModuleHandle(0),
1317                                          (LPVOID)&client_cs);
1318             assert(mdi_client);
1319             test_MDI_create(hwnd, mdi_client);
1320             DestroyWindow(mdi_client);
1321
1322             /* MDIClient with MDIS_ALLCHILDSTYLES */
1323             mdi_client = CreateWindowExA(0, "mdiclient",
1324                                          NULL,
1325                                          WS_CHILD | MDIS_ALLCHILDSTYLES /*| WS_VISIBLE*/,
1326                                           /* tests depend on a not zero MDIClient size */
1327                                          0, 0, rc.right, rc.bottom,
1328                                          hwnd, 0, GetModuleHandle(0),
1329                                          (LPVOID)&client_cs);
1330             assert(mdi_client);
1331             test_MDI_create(hwnd, mdi_client);
1332             DestroyWindow(mdi_client);
1333             break;
1334         }
1335
1336         case WM_CLOSE:
1337             PostQuitMessage(0);
1338             break;
1339     }
1340     return DefFrameProcA(hwnd, mdi_client, msg, wparam, lparam);
1341 }
1342
1343 static BOOL mdi_RegisterWindowClasses(void)
1344 {
1345     WNDCLASSA cls;
1346
1347     cls.style = 0;
1348     cls.lpfnWndProc = mdi_main_wnd_procA;
1349     cls.cbClsExtra = 0;
1350     cls.cbWndExtra = 0;
1351     cls.hInstance = GetModuleHandleA(0);
1352     cls.hIcon = 0;
1353     cls.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
1354     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
1355     cls.lpszMenuName = NULL;
1356     cls.lpszClassName = "MDI_parent_Class";
1357     if(!RegisterClassA(&cls)) return FALSE;
1358
1359     cls.lpfnWndProc = mdi_child_wnd_proc_1;
1360     cls.lpszClassName = "MDI_child_Class_1";
1361     if(!RegisterClassA(&cls)) return FALSE;
1362
1363     cls.lpfnWndProc = mdi_child_wnd_proc_2;
1364     cls.lpszClassName = "MDI_child_Class_2";
1365     if(!RegisterClassA(&cls)) return FALSE;
1366
1367     return TRUE;
1368 }
1369
1370 static void test_mdi(void)
1371 {
1372     HWND mdi_hwndMain;
1373     /*MSG msg;*/
1374
1375     if (!mdi_RegisterWindowClasses()) assert(0);
1376
1377     mdi_hwndMain = CreateWindowExA(0, "MDI_parent_Class", "MDI parent window",
1378                                    WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
1379                                    WS_MAXIMIZEBOX /*| WS_VISIBLE*/,
1380                                    100, 100, CW_USEDEFAULT, CW_USEDEFAULT,
1381                                    GetDesktopWindow(), 0,
1382                                    GetModuleHandle(0), NULL);
1383     assert(mdi_hwndMain);
1384 /*
1385     while(GetMessage(&msg, 0, 0, 0))
1386     {
1387         TranslateMessage(&msg);
1388         DispatchMessage(&msg);
1389     }
1390 */
1391 }
1392
1393 static void test_icons(void)
1394 {
1395     WNDCLASSEXA cls;
1396     HWND hwnd;
1397     HICON icon = LoadIconA(0, (LPSTR)IDI_APPLICATION);
1398     HICON icon2 = LoadIconA(0, (LPSTR)IDI_QUESTION);
1399     HICON small_icon = LoadImageA(0, (LPSTR)IDI_APPLICATION, IMAGE_ICON,
1400                                   GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED );
1401     HICON res;
1402
1403     cls.cbSize = sizeof(cls);
1404     cls.style = 0;
1405     cls.lpfnWndProc = DefWindowProcA;
1406     cls.cbClsExtra = 0;
1407     cls.cbWndExtra = 0;
1408     cls.hInstance = 0;
1409     cls.hIcon = LoadIconA(0, (LPSTR)IDI_HAND);
1410     cls.hIconSm = small_icon;
1411     cls.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
1412     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
1413     cls.lpszMenuName = NULL;
1414     cls.lpszClassName = "IconWindowClass";
1415
1416     RegisterClassExA(&cls);
1417
1418     hwnd = CreateWindowExA(0, "IconWindowClass", "icon test", 0,
1419                            100, 100, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, NULL, NULL);
1420     assert( hwnd );
1421
1422     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_BIG, 0 );
1423     ok( res == 0, "wrong big icon %p/0\n", res );
1424     res = (HICON)SendMessageA( hwnd, WM_SETICON, ICON_BIG, (LPARAM)icon );
1425     ok( res == 0, "wrong previous big icon %p/0\n", res );
1426     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_BIG, 0 );
1427     ok( res == icon, "wrong big icon after set %p/%p\n", res, icon );
1428     res = (HICON)SendMessageA( hwnd, WM_SETICON, ICON_BIG, (LPARAM)icon2 );
1429     ok( res == icon, "wrong previous big icon %p/%p\n", res, icon );
1430     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_BIG, 0 );
1431     ok( res == icon2, "wrong big icon after set %p/%p\n", res, icon2 );
1432
1433     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL, 0 );
1434     ok( res == 0, "wrong small icon %p/0\n", res );
1435     /* this test is XP specific */
1436     /*res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL2, 0 );
1437     ok( res != 0, "wrong small icon %p\n", res );*/
1438     res = (HICON)SendMessageA( hwnd, WM_SETICON, ICON_SMALL, (LPARAM)icon );
1439     ok( res == 0, "wrong previous small icon %p/0\n", res );
1440     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL, 0 );
1441     ok( res == icon, "wrong small icon after set %p/%p\n", res, icon );
1442     /* this test is XP specific */
1443     /*res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL2, 0 );
1444     ok( res == icon, "wrong small icon after set %p/%p\n", res, icon );*/
1445     res = (HICON)SendMessageA( hwnd, WM_SETICON, ICON_SMALL, (LPARAM)small_icon );
1446     ok( res == icon, "wrong previous small icon %p/%p\n", res, icon );
1447     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL, 0 );
1448     ok( res == small_icon, "wrong small icon after set %p/%p\n", res, small_icon );
1449     /* this test is XP specific */
1450     /*res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL2, 0 );
1451     ok( res == small_icon, "wrong small icon after set %p/%p\n", res, small_icon );*/
1452
1453     /* make sure the big icon hasn't changed */
1454     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_BIG, 0 );
1455     ok( res == icon2, "wrong big icon after set %p/%p\n", res, icon2 );
1456 }
1457
1458 static void test_SetWindowPos(HWND hwnd)
1459 {
1460     SetWindowPos(hwnd, 0, -32769, -40000, -32769, -90000, SWP_NOMOVE);
1461     SetWindowPos(hwnd, 0, 32768, 40000, 32768, 40000, SWP_NOMOVE);
1462
1463     SetWindowPos(hwnd, 0, -32769, -40000, -32769, -90000, SWP_NOSIZE);
1464     SetWindowPos(hwnd, 0, 32768, 40000, 32768, 40000, SWP_NOSIZE);
1465 }
1466
1467 static void test_SetMenu(HWND parent)
1468 {
1469     HWND child;
1470     HMENU hMenu, ret;
1471
1472     hMenu = CreateMenu();
1473     assert(hMenu);
1474
1475     /* parent */
1476     ret = GetMenu(parent);
1477     ok(ret == 0, "unexpected menu id %p\n", ret);
1478
1479     ok(!SetMenu(parent, (HMENU)20), "SetMenu with invalid menu handle should fail\n");
1480     ret = GetMenu(parent);
1481     ok(ret == 0, "unexpected menu id %p\n", ret);
1482
1483     ok(SetMenu(parent, hMenu), "SetMenu on a top level window should not fail\n");
1484     ret = GetMenu(parent);
1485     ok(ret == (HMENU)hMenu, "unexpected menu id %p\n", ret);
1486
1487     ok(SetMenu(parent, 0), "SetMenu(0) on a top level window should not fail\n");
1488     ret = GetMenu(parent);
1489     ok(ret == 0, "unexpected menu id %p\n", ret);
1490  
1491     /* child */
1492     child = CreateWindowExA(0, "static", NULL, WS_CHILD, 0, 0, 0, 0, parent, (HMENU)10, 0, NULL);
1493     assert(child);
1494
1495     ret = GetMenu(child);
1496     ok(ret == (HMENU)10, "unexpected menu id %p\n", ret);
1497
1498     ok(!SetMenu(child, (HMENU)20), "SetMenu with invalid menu handle should fail\n");
1499     ret = GetMenu(child);
1500     ok(ret == (HMENU)10, "unexpected menu id %p\n", ret);
1501
1502     ok(!SetMenu(child, hMenu), "SetMenu on a child window should fail\n");
1503     ret = GetMenu(child);
1504     ok(ret == (HMENU)10, "unexpected menu id %p\n", ret);
1505
1506     ok(!SetMenu(child, 0), "SetMenu(0) on a child window should fail\n");
1507     ret = GetMenu(child);
1508     ok(ret == (HMENU)10, "unexpected menu id %p\n", ret);
1509
1510     DestroyWindow(child);
1511     DestroyMenu(hMenu);
1512 }
1513
1514 static void test_window_tree(HWND parent, const DWORD *style, const int *order, int total)
1515 {
1516     HWND child[5], hwnd;
1517     int i;
1518
1519     assert(total <= 5);
1520
1521     hwnd = GetWindow(parent, GW_CHILD);
1522     ok(!hwnd, "have to start without children to perform the test\n");
1523
1524     for (i = 0; i < total; i++)
1525     {
1526         child[i] = CreateWindowExA(0, "static", "", style[i], 0,0,10,10,
1527                                    parent, 0, 0, NULL);
1528         trace("child[%d] = %p\n", i, child[i]);
1529         ok(child[i] != 0, "CreateWindowEx failed to create child window\n");
1530     }
1531
1532     hwnd = GetWindow(parent, GW_CHILD);
1533     ok(hwnd != 0, "GetWindow(GW_CHILD) failed\n");
1534     ok(hwnd == GetWindow(child[total - 1], GW_HWNDFIRST), "GW_HWNDFIRST is wrong\n");
1535     ok(child[order[total - 1]] == GetWindow(child[0], GW_HWNDLAST), "GW_HWNDLAST is wrong\n");
1536
1537     for (i = 0; i < total; i++)
1538     {
1539         trace("hwnd[%d] = %p\n", i, hwnd);
1540         ok(child[order[i]] == hwnd, "Z order of child #%d is wrong\n", i);
1541
1542         hwnd = GetWindow(hwnd, GW_HWNDNEXT);
1543     }
1544
1545     for (i = 0; i < total; i++)
1546         ok(DestroyWindow(child[i]), "DestroyWindow failed\n");
1547 }
1548
1549 static void test_children_zorder(HWND parent)
1550 {
1551     const DWORD simple_style[5] = { WS_CHILD, WS_CHILD, WS_CHILD, WS_CHILD,
1552                                     WS_CHILD };
1553     const int simple_order[5] = { 0, 1, 2, 3, 4 };
1554
1555     const DWORD complex_style[5] = { WS_CHILD, WS_CHILD | WS_MAXIMIZE,
1556                              WS_CHILD | WS_VISIBLE, WS_CHILD,
1557                              WS_CHILD | WS_MAXIMIZE | WS_VISIBLE };
1558     const int complex_order_1[1] = { 0 };
1559     const int complex_order_2[2] = { 1, 0 };
1560     const int complex_order_3[3] = { 1, 0, 2 };
1561     const int complex_order_4[4] = { 1, 0, 2, 3 };
1562     const int complex_order_5[5] = { 4, 1, 0, 2, 3 };
1563
1564     /* simple WS_CHILD */
1565     test_window_tree(parent, simple_style, simple_order, 5);
1566
1567     /* complex children styles */
1568     test_window_tree(parent, complex_style, complex_order_1, 1);
1569     test_window_tree(parent, complex_style, complex_order_2, 2);
1570     test_window_tree(parent, complex_style, complex_order_3, 3);
1571     test_window_tree(parent, complex_style, complex_order_4, 4);
1572     test_window_tree(parent, complex_style, complex_order_5, 5);
1573 }
1574
1575 static void test_SetFocus(HWND hwnd)
1576 {
1577     HWND child;
1578
1579     /* check if we can set focus to non-visible windows */
1580
1581     ShowWindow(hwnd, SW_SHOW);
1582     SetFocus(0);
1583     SetFocus(hwnd);
1584     ok( GetFocus() == hwnd, "Failed to set focus to visible window %p\n", hwnd );
1585     ok( GetWindowLong(hwnd,GWL_STYLE) & WS_VISIBLE, "Window %p not visible\n", hwnd );
1586     ShowWindow(hwnd, SW_HIDE);
1587     SetFocus(0);
1588     SetFocus(hwnd);
1589     ok( GetFocus() == hwnd, "Failed to set focus to invisible window %p\n", hwnd );
1590     ok( !(GetWindowLong(hwnd,GWL_STYLE) & WS_VISIBLE), "Window %p still visible\n", hwnd );
1591     child = CreateWindowExA(0, "static", NULL, WS_CHILD, 0, 0, 0, 0, hwnd, 0, 0, NULL);
1592     assert(child);
1593     SetFocus(child);
1594     ok( GetFocus() == child, "Failed to set focus to invisible child %p\n", child );
1595     ok( !(GetWindowLong(child,GWL_STYLE) & WS_VISIBLE), "Child %p is visible\n", child );
1596     ShowWindow(child, SW_SHOW);
1597     ok( GetWindowLong(child,GWL_STYLE) & WS_VISIBLE, "Child %p is not visible\n", child );
1598     ok( GetFocus() == child, "Focus no longer on child %p\n", child );
1599     ShowWindow(child, SW_HIDE);
1600     ok( !(GetWindowLong(child,GWL_STYLE) & WS_VISIBLE), "Child %p is visible\n", child );
1601     ok( GetFocus() == hwnd, "Focus should be on parent %p, not %p\n", hwnd, GetFocus() );
1602     ShowWindow(child, SW_SHOW);
1603     SetFocus(child);
1604     ok( GetFocus() == child, "Focus should be on child %p\n", child );
1605     SetWindowPos(child,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_HIDEWINDOW);
1606     ok( GetFocus() == child, "Focus should still be on child %p\n", child );
1607
1608     ShowWindow(child, SW_HIDE);
1609     SetFocus(hwnd);
1610     ok( GetFocus() == hwnd, "Focus should be on parent %p, not %p\n", hwnd, GetFocus() );
1611     SetWindowPos(child,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_SHOWWINDOW);
1612     ok( GetFocus() == hwnd, "Focus should still be on parent %p, not %p\n", hwnd, GetFocus() );
1613     ShowWindow(child, SW_HIDE);
1614     ok( GetFocus() == hwnd, "Focus should still be on parent %p, not %p\n", hwnd, GetFocus() );
1615
1616     DestroyWindow( child );
1617 }
1618
1619 static void test_SetActiveWindow(HWND hwnd)
1620 {
1621     HWND hwnd2;
1622
1623     ShowWindow(hwnd, SW_SHOW);
1624     SetActiveWindow(0);
1625     SetActiveWindow(hwnd);
1626     ok( GetActiveWindow() == hwnd, "Failed to set focus to visible window %p\n", hwnd );
1627     SetWindowPos(hwnd,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_HIDEWINDOW);
1628     ok( GetActiveWindow() == hwnd, "Window %p no longer active\n", hwnd );
1629     SetWindowPos(hwnd,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_SHOWWINDOW);
1630     ShowWindow(hwnd, SW_HIDE);
1631     ok( GetActiveWindow() != hwnd, "Window %p is still active\n", hwnd );
1632     ShowWindow(hwnd, SW_SHOW);
1633
1634     hwnd2 = CreateWindowExA(0, "static", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 0, 0, hwnd, 0, 0, NULL);
1635     ok( GetActiveWindow() == hwnd2, "Window %p is not active\n", hwnd2 );
1636     DestroyWindow(hwnd2);
1637     ok( GetActiveWindow() != hwnd2, "Window %p is still active\n", hwnd2 );
1638
1639     hwnd2 = CreateWindowExA(0, "static", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 0, 0, hwnd, 0, 0, NULL);
1640     ok( GetActiveWindow() == hwnd2, "Window %p is not active\n", hwnd2 );
1641     SetWindowPos(hwnd2,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_HIDEWINDOW);
1642     ok( GetActiveWindow() == hwnd2, "Window %p no longer active (%p)\n", hwnd2, GetActiveWindow() );
1643     DestroyWindow(hwnd2);
1644     ok( GetActiveWindow() != hwnd2, "Window %p is still active\n", hwnd2 );
1645 }
1646
1647 START_TEST(win)
1648 {
1649     pGetAncestor = (void *)GetProcAddress( GetModuleHandleA("user32.dll"), "GetAncestor" );
1650     pGetWindowInfo = (void *)GetProcAddress( GetModuleHandleA("user32.dll"), "GetWindowInfo" );
1651
1652     hwndMain = CreateWindowExA(0, "static", NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, 0, 0, NULL);
1653     if (hwndMain)
1654     {
1655         ok(!GetParent(hwndMain), "GetParent should return 0 for message only windows\n");
1656         if (pGetAncestor)
1657         {
1658             hwndMessage = pGetAncestor(hwndMain, GA_PARENT);
1659             ok(hwndMessage != 0, "GetAncestor(GA_PARENT) should not return 0 for message only windows\n");
1660             trace("hwndMessage %p\n", hwndMessage);
1661         }
1662         DestroyWindow(hwndMain);
1663     }
1664     else
1665         trace("CreateWindowExA with parent HWND_MESSAGE failed\n");
1666
1667     if (!RegisterWindowClasses()) assert(0);
1668
1669     hhook = SetWindowsHookExA(WH_CBT, cbt_hook_proc, 0, GetCurrentThreadId());
1670     assert(hhook);
1671
1672     hwndMain = CreateWindowExA(/*WS_EX_TOOLWINDOW*/ 0, "MainWindowClass", "Main window",
1673                                WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
1674                                WS_MAXIMIZEBOX | WS_POPUP,
1675                                100, 100, 200, 200,
1676                                0, 0, 0, NULL);
1677     hwndMain2 = CreateWindowExA(/*WS_EX_TOOLWINDOW*/ 0, "MainWindowClass", "Main window 2",
1678                                 WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
1679                                 WS_MAXIMIZEBOX | WS_POPUP,
1680                                 100, 100, 200, 200,
1681                                 0, 0, 0, NULL);
1682     assert( hwndMain );
1683     assert( hwndMain2 );
1684
1685     test_parent_owner();
1686     test_shell_window();
1687
1688     test_mdi();
1689     test_icons();
1690     test_SetWindowPos(hwndMain);
1691     test_SetMenu(hwndMain);
1692     test_SetFocus(hwndMain);
1693     test_SetActiveWindow(hwndMain);
1694
1695     test_children_zorder(hwndMain);
1696
1697     UnhookWindowsHookEx(hhook);
1698 }