janitorial: Remove remaining NULL checks before free() (found by Smatch).
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 /* To get ICON_SMALL2 with the MSVC headers */
24 #define _WIN32_WINNT 0x0501
25
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
28
29 #include <assert.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wingdi.h"
37 #include "winuser.h"
38
39 #include "wine/test.h"
40
41 #ifndef SPI_GETDESKWALLPAPER
42 #define SPI_GETDESKWALLPAPER 0x0073
43 #endif
44
45 #define LONG_PTR INT_PTR
46 #define ULONG_PTR UINT_PTR
47
48 void dump_region(HRGN hrgn);
49
50 static HWND (WINAPI *pGetAncestor)(HWND,UINT);
51 static BOOL (WINAPI *pGetWindowInfo)(HWND,WINDOWINFO*);
52
53 static BOOL test_lbuttondown_flag;
54 static HWND hwndMessage;
55 static HWND hwndMain, hwndMain2;
56 static HHOOK hhook;
57
58 static const char* szAWRClass = "Winsize";
59 static HMENU hmenu;
60
61 #define COUNTOF(arr) (sizeof(arr)/sizeof(arr[0]))
62
63 /* try to make sure pending X events have been processed before continuing */
64 static void flush_events(void)
65 {
66     MSG msg;
67     int diff = 200;
68     DWORD time = GetTickCount() + diff;
69
70     while (diff > 0)
71     {
72         MsgWaitForMultipleObjects( 0, NULL, FALSE, diff, QS_ALLINPUT );
73         while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
74         diff = time - GetTickCount();
75     }
76 }
77
78 /* check the values returned by the various parent/owner functions on a given window */
79 static void check_parents( HWND hwnd, HWND ga_parent, HWND gwl_parent, HWND get_parent,
80                            HWND gw_owner, HWND ga_root, HWND ga_root_owner )
81 {
82     HWND res;
83
84     if (pGetAncestor)
85     {
86         res = pGetAncestor( hwnd, GA_PARENT );
87         ok( res == ga_parent, "Wrong result for GA_PARENT %p expected %p\n", res, ga_parent );
88     }
89     res = (HWND)GetWindowLongPtrA( hwnd, GWLP_HWNDPARENT );
90     ok( res == gwl_parent, "Wrong result for GWL_HWNDPARENT %p expected %p\n", res, gwl_parent );
91     res = GetParent( hwnd );
92     ok( res == get_parent, "Wrong result for GetParent %p expected %p\n", res, get_parent );
93     res = GetWindow( hwnd, GW_OWNER );
94     ok( res == gw_owner, "Wrong result for GW_OWNER %p expected %p\n", res, gw_owner );
95     if (pGetAncestor)
96     {
97         res = pGetAncestor( hwnd, GA_ROOT );
98         ok( res == ga_root, "Wrong result for GA_ROOT %p expected %p\n", res, ga_root );
99         res = pGetAncestor( hwnd, GA_ROOTOWNER );
100         ok( res == ga_root_owner, "Wrong result for GA_ROOTOWNER %p expected %p\n", res, ga_root_owner );
101     }
102 }
103
104 BOOL CALLBACK EnumChildProc( HWND hwndChild, LPARAM lParam) 
105 {
106     (*(LPINT)lParam)++;
107     trace("EnumChildProc on %p\n", hwndChild);
108     if (*(LPINT)lParam > 1) return FALSE;
109     return TRUE;
110 }
111
112 /* will search for the given window */
113 BOOL CALLBACK EnumChildProc1( HWND hwndChild, LPARAM lParam) 
114 {
115     trace("EnumChildProc1 on %p\n", hwndChild);
116     if ((HWND)lParam == hwndChild) return FALSE;
117     return TRUE;
118 }
119
120 static HWND create_tool_window( LONG style, HWND parent )
121 {
122     HWND ret = CreateWindowExA(0, "ToolWindowClass", "Tool window 1", style,
123                                0, 0, 100, 100, parent, 0, 0, NULL );
124     ok( ret != 0, "Creation failed\n" );
125     return ret;
126 }
127
128 /* test parent and owner values for various combinations */
129 static void test_parent_owner(void)
130 {
131     LONG style;
132     HWND test, owner, ret;
133     HWND desktop = GetDesktopWindow();
134     HWND child = create_tool_window( WS_CHILD, hwndMain );
135     INT  numChildren;
136
137     trace( "main window %p main2 %p desktop %p child %p\n", hwndMain, hwndMain2, desktop, child );
138
139     /* child without parent, should fail */
140     SetLastError(0xdeadbeef);
141     test = CreateWindowExA(0, "ToolWindowClass", "Tool window 1",
142                            WS_CHILD, 0, 0, 100, 100, 0, 0, 0, NULL );
143     ok( GetLastError() == ERROR_TLW_WITH_WSCHILD, "CreateWindowExA should call SetLastError\n" );
144     ok( !test, "WS_CHILD without parent created\n" );
145
146     /* desktop window */
147     check_parents( desktop, 0, 0, 0, 0, 0, 0 );
148     style = GetWindowLongA( desktop, GWL_STYLE );
149     ok( !SetWindowLongA( desktop, GWL_STYLE, WS_POPUP ), "Set GWL_STYLE on desktop succeeded\n" );
150     ok( !SetWindowLongA( desktop, GWL_STYLE, 0 ), "Set GWL_STYLE on desktop succeeded\n" );
151     ok( GetWindowLongA( desktop, GWL_STYLE ) == style, "Desktop style changed\n" );
152
153     /* normal child window */
154     test = create_tool_window( WS_CHILD, hwndMain );
155     trace( "created child %p\n", test );
156     check_parents( test, hwndMain, hwndMain, hwndMain, 0, hwndMain, hwndMain );
157     SetWindowLongA( test, GWL_STYLE, 0 );
158     check_parents( test, hwndMain, hwndMain, 0, 0, hwndMain, test );
159     SetWindowLongA( test, GWL_STYLE, WS_POPUP );
160     check_parents( test, hwndMain, hwndMain, 0, 0, hwndMain, test );
161     SetWindowLongA( test, GWL_STYLE, WS_POPUP|WS_CHILD );
162     check_parents( test, hwndMain, hwndMain, 0, 0, hwndMain, test );
163     SetWindowLongA( test, GWL_STYLE, WS_CHILD );
164     DestroyWindow( test );
165
166     /* normal child window with WS_MAXIMIZE */
167     test = create_tool_window( WS_CHILD | WS_MAXIMIZE, hwndMain );
168     DestroyWindow( test );
169
170     /* normal child window with WS_THICKFRAME */
171     test = create_tool_window( WS_CHILD | WS_THICKFRAME, hwndMain );
172     DestroyWindow( test );
173
174     /* popup window with WS_THICKFRAME */
175     test = create_tool_window( WS_POPUP | WS_THICKFRAME, hwndMain );
176     DestroyWindow( test );
177
178     /* child of desktop */
179     test = create_tool_window( WS_CHILD, desktop );
180     trace( "created child of desktop %p\n", test );
181     check_parents( test, desktop, 0, desktop, 0, test, desktop );
182     SetWindowLongA( test, GWL_STYLE, WS_POPUP );
183     check_parents( test, desktop, 0, 0, 0, test, test );
184     SetWindowLongA( test, GWL_STYLE, 0 );
185     check_parents( test, desktop, 0, 0, 0, test, test );
186     DestroyWindow( test );
187
188     /* child of desktop with WS_MAXIMIZE */
189     test = create_tool_window( WS_CHILD | WS_MAXIMIZE, desktop );
190     DestroyWindow( test );
191
192     /* child of desktop with WS_MINIMIZE */
193     test = create_tool_window( WS_CHILD | WS_MINIMIZE, desktop );
194     DestroyWindow( test );
195
196     /* child of child */
197     test = create_tool_window( WS_CHILD, child );
198     trace( "created child of child %p\n", test );
199     check_parents( test, child, child, child, 0, hwndMain, hwndMain );
200     SetWindowLongA( test, GWL_STYLE, 0 );
201     check_parents( test, child, child, 0, 0, hwndMain, test );
202     SetWindowLongA( test, GWL_STYLE, WS_POPUP );
203     check_parents( test, child, child, 0, 0, hwndMain, test );
204     DestroyWindow( test );
205
206     /* child of child with WS_MAXIMIZE */
207     test = create_tool_window( WS_CHILD | WS_MAXIMIZE, child );
208     DestroyWindow( test );
209
210     /* child of child with WS_MINIMIZE */
211     test = create_tool_window( WS_CHILD | WS_MINIMIZE, child );
212     DestroyWindow( test );
213
214     /* not owned top-level window */
215     test = create_tool_window( 0, 0 );
216     trace( "created top-level %p\n", test );
217     check_parents( test, desktop, 0, 0, 0, test, test );
218     SetWindowLongA( test, GWL_STYLE, WS_POPUP );
219     check_parents( test, desktop, 0, 0, 0, test, test );
220     SetWindowLongA( test, GWL_STYLE, WS_CHILD );
221     check_parents( test, desktop, 0, desktop, 0, test, desktop );
222     DestroyWindow( test );
223
224     /* not owned top-level window with WS_MAXIMIZE */
225     test = create_tool_window( WS_MAXIMIZE, 0 );
226     DestroyWindow( test );
227
228     /* owned top-level window */
229     test = create_tool_window( 0, hwndMain );
230     trace( "created owned top-level %p\n", test );
231     check_parents( test, desktop, hwndMain, 0, hwndMain, test, test );
232     SetWindowLongA( test, GWL_STYLE, WS_POPUP );
233     check_parents( test, desktop, hwndMain, hwndMain, hwndMain, test, hwndMain );
234     SetWindowLongA( test, GWL_STYLE, WS_CHILD );
235     check_parents( test, desktop, hwndMain, desktop, hwndMain, test, desktop );
236     DestroyWindow( test );
237
238     /* owned top-level window with WS_MAXIMIZE */
239     test = create_tool_window( WS_MAXIMIZE, hwndMain );
240     DestroyWindow( test );
241
242     /* not owned popup */
243     test = create_tool_window( WS_POPUP, 0 );
244     trace( "created popup %p\n", test );
245     check_parents( test, desktop, 0, 0, 0, test, test );
246     SetWindowLongA( test, GWL_STYLE, WS_CHILD );
247     check_parents( test, desktop, 0, desktop, 0, test, desktop );
248     SetWindowLongA( test, GWL_STYLE, 0 );
249     check_parents( test, desktop, 0, 0, 0, test, test );
250     DestroyWindow( test );
251
252     /* not owned popup with WS_MAXIMIZE */
253     test = create_tool_window( WS_POPUP | WS_MAXIMIZE, 0 );
254     DestroyWindow( test );
255
256     /* owned popup */
257     test = create_tool_window( WS_POPUP, hwndMain );
258     trace( "created owned popup %p\n", test );
259     check_parents( test, desktop, hwndMain, hwndMain, hwndMain, test, hwndMain );
260     SetWindowLongA( test, GWL_STYLE, WS_CHILD );
261     check_parents( test, desktop, hwndMain, desktop, hwndMain, test, desktop );
262     SetWindowLongA( test, GWL_STYLE, 0 );
263     check_parents( test, desktop, hwndMain, 0, hwndMain, test, test );
264     DestroyWindow( test );
265
266     /* owned popup with WS_MAXIMIZE */
267     test = create_tool_window( WS_POPUP | WS_MAXIMIZE, hwndMain );
268     DestroyWindow( test );
269
270     /* top-level window owned by child (same as owned by top-level) */
271     test = create_tool_window( 0, child );
272     trace( "created top-level owned by child %p\n", test );
273     check_parents( test, desktop, hwndMain, 0, hwndMain, test, test );
274     DestroyWindow( test );
275
276     /* top-level window owned by child (same as owned by top-level) with WS_MAXIMIZE */
277     test = create_tool_window( WS_MAXIMIZE, child );
278     DestroyWindow( test );
279
280     /* popup owned by desktop (same as not owned) */
281     test = create_tool_window( WS_POPUP, desktop );
282     trace( "created popup owned by desktop %p\n", test );
283     check_parents( test, desktop, 0, 0, 0, test, test );
284     DestroyWindow( test );
285
286     /* popup owned by desktop (same as not owned) with WS_MAXIMIZE */
287     test = create_tool_window( WS_POPUP | WS_MAXIMIZE, desktop );
288     DestroyWindow( test );
289
290     /* popup owned by child (same as owned by top-level) */
291     test = create_tool_window( WS_POPUP, child );
292     trace( "created popup owned by child %p\n", test );
293     check_parents( test, desktop, hwndMain, hwndMain, hwndMain, test, hwndMain );
294     DestroyWindow( test );
295
296     /* popup owned by child (same as owned by top-level) with WS_MAXIMIZE */
297     test = create_tool_window( WS_POPUP | WS_MAXIMIZE, child );
298     DestroyWindow( test );
299
300     /* not owned popup with WS_CHILD (same as WS_POPUP only) */
301     test = create_tool_window( WS_POPUP | WS_CHILD, 0 );
302     trace( "created WS_CHILD popup %p\n", test );
303     check_parents( test, desktop, 0, 0, 0, test, test );
304     DestroyWindow( test );
305
306     /* not owned popup with WS_CHILD | WS_MAXIMIZE (same as WS_POPUP only) */
307     test = create_tool_window( WS_POPUP | WS_CHILD | WS_MAXIMIZE, 0 );
308     DestroyWindow( test );
309
310     /* owned popup with WS_CHILD (same as WS_POPUP only) */
311     test = create_tool_window( WS_POPUP | WS_CHILD, hwndMain );
312     trace( "created owned WS_CHILD popup %p\n", test );
313     check_parents( test, desktop, hwndMain, hwndMain, hwndMain, test, hwndMain );
314     DestroyWindow( test );
315
316     /* owned popup with WS_CHILD (same as WS_POPUP only) with WS_MAXIMIZE */
317     test = create_tool_window( WS_POPUP | WS_CHILD | WS_MAXIMIZE, hwndMain );
318     DestroyWindow( test );
319
320     /******************** parent changes *************************/
321     trace( "testing parent changes\n" );
322
323     /* desktop window */
324     check_parents( desktop, 0, 0, 0, 0, 0, 0 );
325 #if 0 /* this test succeeds on NT but crashes on win9x systems */
326     ret = (HWND)SetWindowLongA( test, GWL_HWNDPARENT, (LONG_PTR)hwndMain2 );
327     ok( !ret, "Set GWL_HWNDPARENT succeeded on desktop\n" );
328     check_parents( desktop, 0, 0, 0, 0, 0, 0 );
329     ok( !SetParent( desktop, hwndMain ), "SetParent succeeded on desktop\n" );
330     check_parents( desktop, 0, 0, 0, 0, 0, 0 );
331 #endif
332     /* normal child window */
333     test = create_tool_window( WS_CHILD, hwndMain );
334     trace( "created child %p\n", test );
335
336     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)hwndMain2 );
337     ok( ret == hwndMain, "GWL_HWNDPARENT return value %p expected %p\n", ret, hwndMain );
338     check_parents( test, hwndMain2, hwndMain2, hwndMain2, 0, hwndMain2, hwndMain2 );
339
340     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)child );
341     ok( ret == hwndMain2, "GWL_HWNDPARENT return value %p expected %p\n", ret, hwndMain2 );
342     check_parents( test, child, child, child, 0, hwndMain, hwndMain );
343
344     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)desktop );
345     ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
346     check_parents( test, desktop, 0, desktop, 0, test, desktop );
347
348     /* window is now child of desktop so GWLP_HWNDPARENT changes owner from now on */
349     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)child );
350     ok( ret == 0, "GWL_HWNDPARENT return value %p expected 0\n", ret );
351     check_parents( test, desktop, child, desktop, child, test, desktop );
352
353     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, 0 );
354     ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
355     check_parents( test, desktop, 0, desktop, 0, test, desktop );
356     DestroyWindow( test );
357
358     /* not owned top-level window */
359     test = create_tool_window( 0, 0 );
360     trace( "created top-level %p\n", test );
361
362     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)hwndMain2 );
363     ok( ret == 0, "GWL_HWNDPARENT return value %p expected 0\n", ret );
364     check_parents( test, desktop, hwndMain2, 0, hwndMain2, test, test );
365
366     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)child );
367     ok( ret == hwndMain2, "GWL_HWNDPARENT return value %p expected %p\n", ret, hwndMain2 );
368     check_parents( test, desktop, child, 0, child, test, test );
369
370     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, 0 );
371     ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
372     check_parents( test, desktop, 0, 0, 0, test, test );
373     DestroyWindow( test );
374
375     /* not owned popup */
376     test = create_tool_window( WS_POPUP, 0 );
377     trace( "created popup %p\n", test );
378
379     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)hwndMain2 );
380     ok( ret == 0, "GWL_HWNDPARENT return value %p expected 0\n", ret );
381     check_parents( test, desktop, hwndMain2, hwndMain2, hwndMain2, test, hwndMain2 );
382
383     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)child );
384     ok( ret == hwndMain2, "GWL_HWNDPARENT return value %p expected %p\n", ret, hwndMain2 );
385     check_parents( test, desktop, child, child, child, test, hwndMain );
386
387     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, 0 );
388     ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
389     check_parents( test, desktop, 0, 0, 0, test, test );
390     DestroyWindow( test );
391
392     /* normal child window */
393     test = create_tool_window( WS_CHILD, hwndMain );
394     trace( "created child %p\n", test );
395
396     ret = SetParent( test, desktop );
397     ok( ret == hwndMain, "SetParent return value %p expected %p\n", ret, hwndMain );
398     check_parents( test, desktop, 0, desktop, 0, test, desktop );
399
400     ret = SetParent( test, child );
401     ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
402     check_parents( test, child, child, child, 0, hwndMain, hwndMain );
403
404     ret = SetParent( test, hwndMain2 );
405     ok( ret == child, "SetParent return value %p expected %p\n", ret, child );
406     check_parents( test, hwndMain2, hwndMain2, hwndMain2, 0, hwndMain2, hwndMain2 );
407     DestroyWindow( test );
408
409     /* not owned top-level window */
410     test = create_tool_window( 0, 0 );
411     trace( "created top-level %p\n", test );
412
413     ret = SetParent( test, child );
414     ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
415     check_parents( test, child, child, 0, 0, hwndMain, test );
416     DestroyWindow( test );
417
418     /* owned popup */
419     test = create_tool_window( WS_POPUP, hwndMain2 );
420     trace( "created owned popup %p\n", test );
421
422     ret = SetParent( test, child );
423     ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
424     check_parents( test, child, child, hwndMain2, hwndMain2, hwndMain, hwndMain2 );
425
426     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (ULONG_PTR)hwndMain );
427     ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
428     check_parents( test, hwndMain, hwndMain, hwndMain2, hwndMain2, hwndMain, hwndMain2 );
429     DestroyWindow( test );
430
431     /**************** test owner destruction *******************/
432
433     /* owned child popup */
434     owner = create_tool_window( 0, 0 );
435     test = create_tool_window( WS_POPUP, owner );
436     trace( "created owner %p and popup %p\n", owner, test );
437     ret = SetParent( test, child );
438     ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
439     check_parents( test, child, child, owner, owner, hwndMain, owner );
440     /* window is now child of 'child' but owned by 'owner' */
441     DestroyWindow( owner );
442     ok( IsWindow(test), "Window %p destroyed by owner destruction\n", test );
443     /* Win98 doesn't pass this test. It doesn't allow a destroyed owner,
444      * while Win95, Win2k, WinXP do.
445      */
446     /*check_parents( test, child, child, owner, owner, hwndMain, owner );*/
447     ok( !IsWindow(owner), "Owner %p not destroyed\n", owner );
448     DestroyWindow(test);
449
450     /* owned top-level popup */
451     owner = create_tool_window( 0, 0 );
452     test = create_tool_window( WS_POPUP, owner );
453     trace( "created owner %p and popup %p\n", owner, test );
454     check_parents( test, desktop, owner, owner, owner, test, owner );
455     DestroyWindow( owner );
456     ok( !IsWindow(test), "Window %p not destroyed by owner destruction\n", test );
457
458     /* top-level popup owned by child */
459     owner = create_tool_window( WS_CHILD, hwndMain2 );
460     test = create_tool_window( WS_POPUP, 0 );
461     trace( "created owner %p and popup %p\n", owner, test );
462     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (ULONG_PTR)owner );
463     ok( ret == 0, "GWL_HWNDPARENT return value %p expected 0\n", ret );
464     check_parents( test, desktop, owner, owner, owner, test, hwndMain2 );
465     DestroyWindow( owner );
466     ok( IsWindow(test), "Window %p destroyed by owner destruction\n", test );
467     ok( !IsWindow(owner), "Owner %p not destroyed\n", owner );
468     /* Win98 doesn't pass this test. It doesn't allow a destroyed owner,
469      * while Win95, Win2k, WinXP do.
470      */
471     /*check_parents( test, desktop, owner, owner, owner, test, owner );*/
472     DestroyWindow(test);
473
474     /* final cleanup */
475     DestroyWindow(child);
476
477
478     owner = create_tool_window( WS_OVERLAPPED, 0 );
479     test = create_tool_window( WS_POPUP, desktop );
480
481     ok( !GetWindow( test, GW_OWNER ), "Wrong owner window\n" );
482     numChildren = 0;
483     ok( !EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
484         "EnumChildWindows should have returned FALSE\n" );
485     ok( numChildren == 0, "numChildren should be 0 got %d\n", numChildren );
486
487     SetWindowLongA( test, GWL_STYLE, (GetWindowLongA( test, GWL_STYLE ) & ~WS_POPUP) | WS_CHILD );
488     ret = SetParent( test, owner );
489     ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
490
491     numChildren = 0;
492     ok( EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
493         "EnumChildWindows should have returned TRUE\n" );
494     ok( numChildren == 1, "numChildren should be 1 got %d\n", numChildren );
495
496     child = create_tool_window( WS_CHILD, owner );
497     numChildren = 0;
498     ok( !EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
499         "EnumChildWindows should have returned FALSE\n" );
500     ok( numChildren == 2, "numChildren should be 2 got %d\n", numChildren );
501     DestroyWindow( child );
502
503     child = create_tool_window( WS_VISIBLE | WS_OVERLAPPEDWINDOW, owner );
504     ok( GetWindow( child, GW_OWNER ) == owner, "Wrong owner window\n" );
505     numChildren = 0;
506     ok( EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
507         "EnumChildWindows should have returned TRUE\n" );
508     ok( numChildren == 1, "numChildren should be 1 got %d\n", numChildren );
509
510     ret = SetParent( child, owner );
511     ok( GetWindow( child, GW_OWNER ) == owner, "Wrong owner window\n" );
512     ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
513     numChildren = 0;
514     ok( !EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
515         "EnumChildWindows should have returned FALSE\n" );
516     ok( numChildren == 2, "numChildren should be 2 got %d\n", numChildren );
517
518     ret = SetParent( child, NULL );
519     ok( GetWindow( child, GW_OWNER ) == owner, "Wrong owner window\n" );
520     ok( ret == owner, "SetParent return value %p expected %p\n", ret, owner );
521     numChildren = 0;
522     ok( EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
523         "EnumChildWindows should have returned TRUE\n" );
524     ok( numChildren == 1, "numChildren should be 1 got %d\n", numChildren );
525
526     /* even GW_OWNER == owner it's still a desktop's child */
527     ok( !EnumChildWindows( desktop, EnumChildProc1, (LPARAM)child ),
528         "EnumChildWindows should have found %p and returned FALSE\n", child );
529
530     DestroyWindow( child );
531     child = create_tool_window( WS_VISIBLE | WS_OVERLAPPEDWINDOW, NULL );
532
533     ok( !EnumChildWindows( desktop, EnumChildProc1, (LPARAM)child ),
534         "EnumChildWindows should have found %p and returned FALSE\n", child );
535
536     DestroyWindow( child );
537     DestroyWindow( test );
538     DestroyWindow( owner );
539 }
540
541
542 static LRESULT WINAPI main_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
543 {
544     switch (msg)
545     {
546         case WM_GETMINMAXINFO:
547         {
548             MINMAXINFO* minmax = (MINMAXINFO *)lparam;
549
550             trace("hwnd %p, WM_GETMINMAXINFO, %08x, %08lx\n", hwnd, wparam, lparam);
551             trace("ptReserved (%d,%d), ptMaxSize (%d,%d), ptMaxPosition (%d,%d)\n"
552                   "       ptMinTrackSize (%d,%d), ptMaxTrackSize (%d,%d)\n",
553                   minmax->ptReserved.x, minmax->ptReserved.y,
554                   minmax->ptMaxSize.x, minmax->ptMaxSize.y,
555                   minmax->ptMaxPosition.x, minmax->ptMaxPosition.y,
556                   minmax->ptMinTrackSize.x, minmax->ptMinTrackSize.y,
557                   minmax->ptMaxTrackSize.x, minmax->ptMaxTrackSize.y);
558             SetWindowLongPtrA(hwnd, GWLP_USERDATA, 0x20031021);
559             break;
560         }
561         case WM_WINDOWPOSCHANGING:
562         {
563             BOOL is_win9x = GetWindowLongPtrW(hwnd, GWLP_WNDPROC) == 0;
564             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
565             trace("main: WM_WINDOWPOSCHANGING\n");
566             trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
567                    winpos->hwnd, winpos->hwndInsertAfter,
568                    winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
569             if (!(winpos->flags & SWP_NOMOVE))
570             {
571                 ok(winpos->x >= -32768 && winpos->x <= 32767, "bad winpos->x %d\n", winpos->x);
572                 ok(winpos->y >= -32768 && winpos->y <= 32767, "bad winpos->y %d\n", winpos->y);
573             }
574             /* Win9x does not fixup cx/xy for WM_WINDOWPOSCHANGING */
575             if (!(winpos->flags & SWP_NOSIZE) && !is_win9x)
576             {
577                 ok(winpos->cx >= 0 && winpos->cx <= 32767, "bad winpos->cx %d\n", winpos->cx);
578                 ok(winpos->cy >= 0 && winpos->cy <= 32767, "bad winpos->cy %d\n", winpos->cy);
579             }
580             break;
581         }
582         case WM_WINDOWPOSCHANGED:
583         {
584             RECT rc1, rc2;
585             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
586             trace("main: WM_WINDOWPOSCHANGED\n");
587             trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
588                    winpos->hwnd, winpos->hwndInsertAfter,
589                    winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
590             ok(winpos->x >= -32768 && winpos->x <= 32767, "bad winpos->x %d\n", winpos->x);
591             ok(winpos->y >= -32768 && winpos->y <= 32767, "bad winpos->y %d\n", winpos->y);
592
593             ok(winpos->cx >= 0 && winpos->cx <= 32767, "bad winpos->cx %d\n", winpos->cx);
594             ok(winpos->cy >= 0 && winpos->cy <= 32767, "bad winpos->cy %d\n", winpos->cy);
595
596             GetWindowRect(hwnd, &rc1);
597             trace("window: (%d,%d)-(%d,%d)\n", rc1.left, rc1.top, rc1.right, rc1.bottom);
598             SetRect(&rc2, winpos->x, winpos->y, winpos->x + winpos->cx, winpos->y + winpos->cy);
599             /* note: winpos coordinates are relative to parent */
600             MapWindowPoints(GetParent(hwnd), 0, (LPPOINT)&rc2, 2);
601             trace("pos: (%d,%d)-(%d,%d)\n", rc2.left, rc2.top, rc2.right, rc2.bottom);
602 #if 0 /* Uncomment this once the test succeeds in all cases */
603             ok(EqualRect(&rc1, &rc2), "rects do not match\n");
604 #endif
605
606             GetClientRect(hwnd, &rc2);
607             DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc1);
608             MapWindowPoints(0, hwnd, (LPPOINT)&rc1, 2);
609             ok(EqualRect(&rc1, &rc2), "rects do not match (%d,%d-%d,%d) / (%d,%d-%d,%d)\n",
610                rc1.left, rc1.top, rc1.right, rc1.bottom, rc2.left, rc2.top, rc2.right, rc2.bottom );
611             break;
612         }
613         case WM_NCCREATE:
614         {
615             BOOL got_getminmaxinfo = GetWindowLongPtrA(hwnd, GWLP_USERDATA) == 0x20031021;
616             CREATESTRUCTA *cs = (CREATESTRUCTA *)lparam;
617
618             trace("WM_NCCREATE: hwnd %p, parent %p, style %08x\n", hwnd, cs->hwndParent, cs->style);
619             if (got_getminmaxinfo)
620                 trace("%p got WM_GETMINMAXINFO\n", hwnd);
621
622             if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
623                 ok(got_getminmaxinfo, "main: WM_GETMINMAXINFO should have been received before WM_NCCREATE\n");
624             else
625                 ok(!got_getminmaxinfo, "main: WM_GETMINMAXINFO should NOT have been received before WM_NCCREATE\n");
626             break;
627         }
628         case WM_COMMAND:
629             if (test_lbuttondown_flag)
630                 ShowWindow((HWND)wparam, SW_SHOW);
631             break;
632     }
633
634     return DefWindowProcA(hwnd, msg, wparam, lparam);
635 }
636
637 static LRESULT WINAPI tool_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
638 {
639     switch (msg)
640     {
641         case WM_GETMINMAXINFO:
642         {
643             MINMAXINFO* minmax = (MINMAXINFO *)lparam;
644
645             trace("hwnd %p, WM_GETMINMAXINFO, %08x, %08lx\n", hwnd, wparam, lparam);
646             trace("ptReserved (%d,%d), ptMaxSize (%d,%d), ptMaxPosition (%d,%d)\n"
647                   "       ptMinTrackSize (%d,%d), ptMaxTrackSize (%d,%d)\n",
648                   minmax->ptReserved.x, minmax->ptReserved.y,
649                   minmax->ptMaxSize.x, minmax->ptMaxSize.y,
650                   minmax->ptMaxPosition.x, minmax->ptMaxPosition.y,
651                   minmax->ptMinTrackSize.x, minmax->ptMinTrackSize.y,
652                   minmax->ptMaxTrackSize.x, minmax->ptMaxTrackSize.y);
653             SetWindowLongPtrA(hwnd, GWLP_USERDATA, 0x20031021);
654             break;
655         }
656         case WM_NCCREATE:
657         {
658             BOOL got_getminmaxinfo = GetWindowLongPtrA(hwnd, GWLP_USERDATA) == 0x20031021;
659             CREATESTRUCTA *cs = (CREATESTRUCTA *)lparam;
660
661             trace("WM_NCCREATE: hwnd %p, parent %p, style %08x\n", hwnd, cs->hwndParent, cs->style);
662             if (got_getminmaxinfo)
663                 trace("%p got WM_GETMINMAXINFO\n", hwnd);
664
665             if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
666                 ok(got_getminmaxinfo, "tool: WM_GETMINMAXINFO should have been received before WM_NCCREATE\n");
667             else
668                 ok(!got_getminmaxinfo, "tool: WM_GETMINMAXINFO should NOT have been received before WM_NCCREATE\n");
669             break;
670         }
671     }
672
673     return DefWindowProcA(hwnd, msg, wparam, lparam);
674 }
675
676 static BOOL RegisterWindowClasses(void)
677 {
678     WNDCLASSA cls;
679
680     cls.style = CS_DBLCLKS;
681     cls.lpfnWndProc = main_window_procA;
682     cls.cbClsExtra = 0;
683     cls.cbWndExtra = 0;
684     cls.hInstance = GetModuleHandleA(0);
685     cls.hIcon = 0;
686     cls.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
687     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
688     cls.lpszMenuName = NULL;
689     cls.lpszClassName = "MainWindowClass";
690
691     if(!RegisterClassA(&cls)) return FALSE;
692
693     cls.style = 0;
694     cls.lpfnWndProc = tool_window_procA;
695     cls.cbClsExtra = 0;
696     cls.cbWndExtra = 0;
697     cls.hInstance = GetModuleHandleA(0);
698     cls.hIcon = 0;
699     cls.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
700     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
701     cls.lpszMenuName = NULL;
702     cls.lpszClassName = "ToolWindowClass";
703
704     if(!RegisterClassA(&cls)) return FALSE;
705
706     return TRUE;
707 }
708
709 static void verify_window_info(HWND hwnd, const WINDOWINFO *info, BOOL test_borders)
710 {
711     RECT rcWindow, rcClient;
712     UINT border;
713     DWORD status;
714
715     ok(IsWindow(hwnd), "bad window handle\n");
716
717     GetWindowRect(hwnd, &rcWindow);
718     ok(EqualRect(&rcWindow, &info->rcWindow), "wrong rcWindow\n");
719
720     GetClientRect(hwnd, &rcClient);
721     /* translate to screen coordinates */
722     MapWindowPoints(hwnd, 0, (LPPOINT)&rcClient, 2);
723     ok(EqualRect(&rcClient, &info->rcClient), "wrong rcClient\n");
724
725     ok(info->dwStyle == (DWORD)GetWindowLongA(hwnd, GWL_STYLE),
726        "wrong dwStyle: %08x != %08x\n", info->dwStyle, GetWindowLongA(hwnd, GWL_STYLE));
727     ok(info->dwExStyle == (DWORD)GetWindowLongA(hwnd, GWL_EXSTYLE),
728        "wrong dwExStyle: %08x != %08x\n", info->dwExStyle, GetWindowLongA(hwnd, GWL_EXSTYLE));
729     status = (GetActiveWindow() == hwnd) ? WS_ACTIVECAPTION : 0;
730     ok(info->dwWindowStatus == status, "wrong dwWindowStatus: %04x != %04x\n",
731        info->dwWindowStatus, status);
732
733     if (test_borders && !IsRectEmpty(&rcWindow))
734     {
735         trace("rcWindow: %d,%d - %d,%d\n", rcWindow.left, rcWindow.top, rcWindow.right, rcWindow.bottom);
736         trace("rcClient: %d,%d - %d,%d\n", rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
737
738         ok(info->cxWindowBorders == (unsigned)(rcClient.left - rcWindow.left),
739             "wrong cxWindowBorders %d != %d\n", info->cxWindowBorders, rcClient.left - rcWindow.left);
740         border = min(rcWindow.bottom - rcClient.bottom, rcClient.top - rcWindow.top);
741         ok(info->cyWindowBorders == border,
742            "wrong cyWindowBorders %d != %d\n", info->cyWindowBorders, border);
743     }
744
745     ok(info->atomWindowType == GetClassLongA(hwnd, GCW_ATOM), "wrong atomWindowType\n");
746     ok(info->wCreatorVersion == 0x0400, "wrong wCreatorVersion %04x\n", info->wCreatorVersion);
747 }
748
749 static void FixedAdjustWindowRectEx(RECT* rc, LONG style, BOOL menu, LONG exstyle)
750 {
751     AdjustWindowRectEx(rc, style, menu, exstyle);
752     /* AdjustWindowRectEx does not include scroll bars */
753     if (style & WS_VSCROLL)
754     {
755         if(exstyle & WS_EX_LEFTSCROLLBAR)
756             rc->left  -= GetSystemMetrics(SM_CXVSCROLL);
757         else
758             rc->right += GetSystemMetrics(SM_CXVSCROLL);
759     }
760     if (style & WS_HSCROLL)
761         rc->bottom += GetSystemMetrics(SM_CYHSCROLL);
762 }
763
764 static void test_nonclient_area(HWND hwnd)
765 {
766     DWORD style, exstyle;
767     RECT rc_window, rc_client, rc;
768     BOOL menu;
769     BOOL is_win9x = GetWindowLongPtrW(hwnd, GWLP_WNDPROC) == 0;
770
771     style = GetWindowLongA(hwnd, GWL_STYLE);
772     exstyle = GetWindowLongA(hwnd, GWL_EXSTYLE);
773     menu = !(style & WS_CHILD) && GetMenu(hwnd) != 0;
774
775     GetWindowRect(hwnd, &rc_window);
776     trace("window: (%d,%d)-(%d,%d)\n", rc_window.left, rc_window.top, rc_window.right, rc_window.bottom);
777     GetClientRect(hwnd, &rc_client);
778     trace("client: (%d,%d)-(%d,%d)\n", rc_client.left, rc_client.top, rc_client.right, rc_client.bottom);
779
780     /* avoid some cases when things go wrong */
781     if (IsRectEmpty(&rc_window) || IsRectEmpty(&rc_client) ||
782         rc_window.right > 32768 || rc_window.bottom > 32768) return;
783
784     CopyRect(&rc, &rc_client);
785     MapWindowPoints(hwnd, 0, (LPPOINT)&rc, 2);
786     FixedAdjustWindowRectEx(&rc, style, menu, exstyle);
787
788     trace("calc window: (%d,%d)-(%d,%d)\n", rc.left, rc.top, rc.right, rc.bottom);
789     ok(EqualRect(&rc, &rc_window), "window rect does not match: style:exstyle=0x%08x:0x%08x, menu=%d\n", style, exstyle, menu);
790
791
792     CopyRect(&rc, &rc_window);
793     DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc);
794     MapWindowPoints(0, hwnd, (LPPOINT)&rc, 2);
795     trace("calc client: (%d,%d)-(%d,%d)\n", rc.left, rc.top, rc.right, rc.bottom);
796     ok(EqualRect(&rc, &rc_client), "client rect does not match: style:exstyle=0x%08x:0x%08x, menu=%d\n", style, exstyle, menu);
797
798     /* Win9x doesn't like WM_NCCALCSIZE with synthetic data and crashes */;
799     if (is_win9x)
800         return;
801
802     /* and now test AdjustWindowRectEx and WM_NCCALCSIZE on synthetic data */
803     SetRect(&rc_client, 0, 0, 250, 150);
804     CopyRect(&rc_window, &rc_client);
805     MapWindowPoints(hwnd, 0, (LPPOINT)&rc_window, 2);
806     FixedAdjustWindowRectEx(&rc_window, style, menu, exstyle);
807     trace("calc window: (%d,%d)-(%d,%d)\n",
808         rc_window.left, rc_window.top, rc_window.right, rc_window.bottom);
809
810     CopyRect(&rc, &rc_window);
811     DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc);
812     MapWindowPoints(0, hwnd, (LPPOINT)&rc, 2);
813     trace("calc client: (%d,%d)-(%d,%d)\n", rc.left, rc.top, rc.right, rc.bottom);
814     ok(EqualRect(&rc, &rc_client), "synthetic rect does not match: style:exstyle=0x%08x:0x%08x, menu=%d\n", style, exstyle, menu);
815 }
816
817 static LRESULT CALLBACK cbt_hook_proc(int nCode, WPARAM wParam, LPARAM lParam) 
818
819     static const char *CBT_code_name[10] = {
820         "HCBT_MOVESIZE",
821         "HCBT_MINMAX",
822         "HCBT_QS",
823         "HCBT_CREATEWND",
824         "HCBT_DESTROYWND",
825         "HCBT_ACTIVATE",
826         "HCBT_CLICKSKIPPED",
827         "HCBT_KEYSKIPPED",
828         "HCBT_SYSCOMMAND",
829         "HCBT_SETFOCUS" };
830     const char *code_name = (nCode >= 0 && nCode <= HCBT_SETFOCUS) ? CBT_code_name[nCode] : "Unknown";
831
832     trace("CBT: %d (%s), %08x, %08lx\n", nCode, code_name, wParam, lParam);
833
834     /* on HCBT_DESTROYWND window state is undefined */
835     if (nCode != HCBT_DESTROYWND && IsWindow((HWND)wParam))
836     {
837         if (pGetWindowInfo)
838         {
839             WINDOWINFO info;
840
841             /* Win98 actually does check the info.cbSize and doesn't allow
842              * it to be anything except sizeof(WINDOWINFO), while Win95, Win2k,
843              * WinXP do not check it at all.
844              */
845             info.cbSize = sizeof(WINDOWINFO);
846             ok(pGetWindowInfo((HWND)wParam, &info), "GetWindowInfo should not fail\n");
847             /* win2k SP4 returns broken border info if GetWindowInfo
848              * is being called from HCBT_DESTROYWND or HCBT_MINMAX hook proc.
849              */
850             verify_window_info((HWND)wParam, &info, nCode != HCBT_MINMAX);
851         }
852     }
853
854     switch (nCode)
855     {
856         case HCBT_CREATEWND:
857         {
858 #if 0 /* Uncomment this once the test succeeds in all cases */
859             static const RECT rc_null;
860             RECT rc;
861 #endif
862             LONG style;
863             CBT_CREATEWNDA *createwnd = (CBT_CREATEWNDA *)lParam;
864             trace("HCBT_CREATEWND: hwnd %p, parent %p, style %08x\n",
865                   (HWND)wParam, createwnd->lpcs->hwndParent, createwnd->lpcs->style);
866             ok(createwnd->hwndInsertAfter == HWND_TOP, "hwndInsertAfter should be always HWND_TOP\n");
867
868             /* WS_VISIBLE should be turned off yet */
869             style = createwnd->lpcs->style & ~WS_VISIBLE;
870             ok(style == GetWindowLongA((HWND)wParam, GWL_STYLE),
871                 "style of hwnd and style in the CREATESTRUCT do not match: %08x != %08x\n",
872                 GetWindowLongA((HWND)wParam, GWL_STYLE), style);
873
874 #if 0 /* Uncomment this once the test succeeds in all cases */
875             if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD)
876             {
877                 ok(GetParent((HWND)wParam) == hwndMessage,
878                    "wrong result from GetParent %p: message window %p\n",
879                    GetParent((HWND)wParam), hwndMessage);
880             }
881             else
882                 ok(!GetParent((HWND)wParam), "GetParent should return 0 at this point\n");
883
884             ok(!GetWindow((HWND)wParam, GW_OWNER), "GW_OWNER should be set to 0 at this point\n");
885 #endif
886 #if 0       /* while NT assigns GW_HWNDFIRST/LAST some values at this point,
887              * Win9x still has them set to 0.
888              */
889             ok(GetWindow((HWND)wParam, GW_HWNDFIRST) != 0, "GW_HWNDFIRST should not be set to 0 at this point\n");
890             ok(GetWindow((HWND)wParam, GW_HWNDLAST) != 0, "GW_HWNDLAST should not be set to 0 at this point\n");
891 #endif
892             ok(!GetWindow((HWND)wParam, GW_HWNDPREV), "GW_HWNDPREV should be set to 0 at this point\n");
893             ok(!GetWindow((HWND)wParam, GW_HWNDNEXT), "GW_HWNDNEXT should be set to 0 at this point\n");
894
895 #if 0 /* Uncomment this once the test succeeds in all cases */
896             if (pGetAncestor)
897             {
898                 ok(pGetAncestor((HWND)wParam, GA_PARENT) == hwndMessage, "GA_PARENT should be set to hwndMessage at this point\n");
899                 ok(pGetAncestor((HWND)wParam, GA_ROOT) == (HWND)wParam,
900                    "GA_ROOT is set to %p, expected %p\n", pGetAncestor((HWND)wParam, GA_ROOT), (HWND)wParam);
901
902                 if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD)
903                     ok(pGetAncestor((HWND)wParam, GA_ROOTOWNER) == hwndMessage,
904                        "GA_ROOTOWNER should be set to hwndMessage at this point\n");
905                 else
906                     ok(pGetAncestor((HWND)wParam, GA_ROOTOWNER) == (HWND)wParam,
907                        "GA_ROOTOWNER is set to %p, expected %p\n", pGetAncestor((HWND)wParam, GA_ROOTOWNER), (HWND)wParam);
908             }
909
910             ok(GetWindowRect((HWND)wParam, &rc), "GetWindowRect failed\n");
911             ok(EqualRect(&rc, &rc_null), "window rect should be set to 0 HCBT_CREATEWND\n");
912             ok(GetClientRect((HWND)wParam, &rc), "GetClientRect failed\n");
913             ok(EqualRect(&rc, &rc_null), "client rect should be set to 0 on HCBT_CREATEWND\n");
914 #endif
915             break;
916         }
917     }
918
919     return CallNextHookEx(hhook, nCode, wParam, lParam);
920 }
921
922 static void test_shell_window(void)
923 {
924     BOOL ret;
925     DWORD error;
926     HMODULE hinst, hUser32;
927     BOOL (WINAPI*SetShellWindow)(HWND);
928     BOOL (WINAPI*SetShellWindowEx)(HWND, HWND);
929     HWND hwnd1, hwnd2, hwnd3, hwnd4, hwnd5;
930     HWND shellWindow, nextWnd;
931
932     if (!GetWindowLongW(GetDesktopWindow(), GWL_STYLE))
933     {
934         trace("Skipping shell window test on Win9x\n");
935         return;
936     }
937
938     shellWindow = GetShellWindow();
939     hinst = GetModuleHandle(0);
940     hUser32 = GetModuleHandleA("user32");
941
942     SetShellWindow = (void *)GetProcAddress(hUser32, "SetShellWindow");
943     SetShellWindowEx = (void *)GetProcAddress(hUser32, "SetShellWindowEx");
944
945     trace("previous shell window: %p\n", shellWindow);
946
947     if (shellWindow) {
948         DWORD pid;
949         HANDLE hProcess;
950
951         ret = DestroyWindow(shellWindow);
952         error = GetLastError();
953
954         ok(!ret, "DestroyWindow(shellWindow)\n");
955         /* passes on Win XP, but not on Win98 */
956         ok(error==ERROR_ACCESS_DENIED, "ERROR_ACCESS_DENIED after DestroyWindow(shellWindow)\n");
957
958         /* close old shell instance */
959         GetWindowThreadProcessId(shellWindow, &pid);
960         hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
961         ret = TerminateProcess(hProcess, 0);
962         ok(ret, "termination of previous shell process failed: GetLastError()=%d\n", GetLastError());
963         WaitForSingleObject(hProcess, INFINITE);    /* wait for termination */
964         CloseHandle(hProcess);
965     }
966
967     hwnd1 = CreateWindowEx(0, TEXT("#32770"), TEXT("TEST1"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 100, 100, 300, 200, 0, 0, hinst, 0);
968     trace("created window 1: %p\n", hwnd1);
969
970     ret = SetShellWindow(hwnd1);
971     ok(ret, "first call to SetShellWindow(hwnd1)\n");
972     shellWindow = GetShellWindow();
973     ok(shellWindow==hwnd1, "wrong shell window: %p\n", shellWindow);
974
975     ret = SetShellWindow(hwnd1);
976     ok(!ret, "second call to SetShellWindow(hwnd1)\n");
977
978     ret = SetShellWindow(0);
979     error = GetLastError();
980     /* passes on Win XP, but not on Win98
981     ok(!ret, "reset shell window by SetShellWindow(0)\n");
982     ok(error==ERROR_INVALID_WINDOW_HANDLE, "ERROR_INVALID_WINDOW_HANDLE after SetShellWindow(0)\n"); */
983
984     ret = SetShellWindow(hwnd1);
985     /* passes on Win XP, but not on Win98
986     ok(!ret, "third call to SetShellWindow(hwnd1)\n"); */
987
988     todo_wine
989     {
990         SetWindowLong(hwnd1, GWL_EXSTYLE, GetWindowLong(hwnd1,GWL_EXSTYLE)|WS_EX_TOPMOST);
991         ret = GetWindowLong(hwnd1,GWL_EXSTYLE)&WS_EX_TOPMOST? TRUE: FALSE;
992         ok(!ret, "SetWindowExStyle(hwnd1, WS_EX_TOPMOST)\n");
993     }
994
995     ret = DestroyWindow(hwnd1);
996     ok(ret, "DestroyWindow(hwnd1)\n");
997
998     hwnd2 = CreateWindowEx(WS_EX_TOPMOST, TEXT("#32770"), TEXT("TEST2"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 150, 250, 300, 200, 0, 0, hinst, 0);
999     trace("created window 2: %p\n", hwnd2);
1000     ret = SetShellWindow(hwnd2);
1001     ok(!ret, "SetShellWindow(hwnd2) with WS_EX_TOPMOST\n");
1002
1003     hwnd3 = CreateWindowEx(0, TEXT("#32770"), TEXT("TEST3"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 200, 400, 300, 200, 0, 0, hinst, 0);
1004     trace("created window 3: %p\n", hwnd3);
1005
1006     hwnd4 = CreateWindowEx(0, TEXT("#32770"), TEXT("TEST4"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 250, 500, 300, 200, 0, 0, hinst, 0);
1007     trace("created window 4: %p\n", hwnd4);
1008
1009     nextWnd = GetWindow(hwnd4, GW_HWNDNEXT);
1010     ok(nextWnd==hwnd3, "wrong next window for hwnd4: %p - expected hwnd3\n", nextWnd);
1011
1012     ret = SetShellWindow(hwnd4);
1013     ok(ret, "SetShellWindow(hwnd4)\n");
1014     shellWindow = GetShellWindow();
1015     ok(shellWindow==hwnd4, "wrong shell window: %p - expected hwnd4\n", shellWindow);
1016
1017     nextWnd = GetWindow(hwnd4, GW_HWNDNEXT);
1018     ok(nextWnd==0, "wrong next window for hwnd4: %p - expected 0\n", nextWnd);
1019
1020     ret = SetWindowPos(hwnd4, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
1021     ok(ret, "SetWindowPos(hwnd4, HWND_TOPMOST)\n");
1022
1023     ret = SetWindowPos(hwnd4, hwnd3, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
1024     ok(ret, "SetWindowPos(hwnd4, hwnd3\n");
1025
1026     ret = SetShellWindow(hwnd3);
1027     ok(!ret, "SetShellWindow(hwnd3)\n");
1028     shellWindow = GetShellWindow();
1029     ok(shellWindow==hwnd4, "wrong shell window: %p - expected hwnd4\n", shellWindow);
1030
1031     hwnd5 = CreateWindowEx(0, TEXT("#32770"), TEXT("TEST5"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 300, 600, 300, 200, 0, 0, hinst, 0);
1032     trace("created window 5: %p\n", hwnd5);
1033     ret = SetWindowPos(hwnd4, hwnd5, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
1034     ok(ret, "SetWindowPos(hwnd4, hwnd5)\n");
1035
1036     todo_wine
1037     {
1038         nextWnd = GetWindow(hwnd4, GW_HWNDNEXT);
1039         ok(nextWnd==0, "wrong next window for hwnd4 after SetWindowPos(): %p - expected 0\n", nextWnd);
1040     }
1041
1042     /* destroy test windows */
1043     DestroyWindow(hwnd2);
1044     DestroyWindow(hwnd3);
1045     DestroyWindow(hwnd4);
1046     DestroyWindow(hwnd5);
1047 }
1048
1049 /************** MDI test ****************/
1050
1051 static const char mdi_lParam_test_message[] = "just a test string";
1052
1053 static void test_MDI_create(HWND parent, HWND mdi_client, INT first_id)
1054 {
1055     MDICREATESTRUCTA mdi_cs;
1056     HWND mdi_child;
1057     static const WCHAR classW[] = {'M','D','I','_','c','h','i','l','d','_','C','l','a','s','s','_','1',0};
1058     static const WCHAR titleW[] = {'M','D','I',' ','c','h','i','l','d',0};
1059     BOOL isWin9x = FALSE;
1060
1061     mdi_cs.szClass = "MDI_child_Class_1";
1062     mdi_cs.szTitle = "MDI child";
1063     mdi_cs.hOwner = GetModuleHandle(0);
1064     mdi_cs.x = CW_USEDEFAULT;
1065     mdi_cs.y = CW_USEDEFAULT;
1066     mdi_cs.cx = CW_USEDEFAULT;
1067     mdi_cs.cy = CW_USEDEFAULT;
1068     mdi_cs.style = 0;
1069     mdi_cs.lParam = (LPARAM)mdi_lParam_test_message;
1070     mdi_child = (HWND)SendMessageA(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
1071     ok(mdi_child != 0, "MDI child creation failed\n");
1072     ok(GetWindowLongPtrA(mdi_child, GWLP_ID) == first_id, "wrong child id %d\n", GetWindowLongPtrA(mdi_child, GWLP_ID));
1073     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1074     ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1075
1076     mdi_cs.style = 0x7fffffff; /* without WS_POPUP */
1077     mdi_child = (HWND)SendMessageA(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
1078     ok(mdi_child != 0, "MDI child creation failed\n");
1079     ok(GetWindowLongPtrA(mdi_child, GWLP_ID) == first_id, "wrong child id %d\n", GetWindowLongPtrA(mdi_child, GWLP_ID));
1080     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1081     ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1082
1083     mdi_cs.style = 0xffffffff; /* with WS_POPUP */
1084     mdi_child = (HWND)SendMessageA(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
1085     if (GetWindowLongA(mdi_client, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1086     {
1087         ok(!mdi_child, "MDI child with WS_POPUP and with MDIS_ALLCHILDSTYLES should fail\n");
1088     }
1089     else
1090     {
1091         ok(mdi_child != 0, "MDI child creation failed\n");
1092         ok(GetWindowLongPtrA(mdi_child, GWLP_ID) == first_id, "wrong child id %d\n", GetWindowLongPtrA(mdi_child, GWLP_ID));
1093         SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1094         ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1095     }
1096
1097     /* test MDICREATESTRUCT A<->W mapping */
1098     /* MDICREATESTRUCTA and MDICREATESTRUCTW have the same layout */
1099     mdi_cs.style = 0;
1100     mdi_cs.szClass = (LPCSTR)classW;
1101     mdi_cs.szTitle = (LPCSTR)titleW;
1102     SetLastError(0xdeadbeef);
1103     mdi_child = (HWND)SendMessageW(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
1104     if (!mdi_child)
1105     {
1106         if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1107             isWin9x = TRUE;
1108         else
1109             ok(mdi_child != 0, "MDI child creation failed\n");
1110     }
1111     else
1112     {
1113         ok(GetWindowLongPtrA(mdi_child, GWLP_ID) == first_id, "wrong child id %d\n", GetWindowLongPtrA(mdi_child, GWLP_ID));
1114         SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1115         ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1116     }
1117
1118     mdi_child = CreateMDIWindowA("MDI_child_Class_1", "MDI child",
1119                                  0,
1120                                  CW_USEDEFAULT, CW_USEDEFAULT,
1121                                  CW_USEDEFAULT, CW_USEDEFAULT,
1122                                  mdi_client, GetModuleHandle(0),
1123                                  (LPARAM)mdi_lParam_test_message);
1124     ok(mdi_child != 0, "MDI child creation failed\n");
1125     ok(GetWindowLongPtrA(mdi_child, GWLP_ID) == first_id, "wrong child id %d\n", GetWindowLongPtrA(mdi_child, GWLP_ID));
1126     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1127     ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1128
1129     mdi_child = CreateMDIWindowA("MDI_child_Class_1", "MDI child",
1130                                  0x7fffffff, /* without WS_POPUP */
1131                                  CW_USEDEFAULT, CW_USEDEFAULT,
1132                                  CW_USEDEFAULT, CW_USEDEFAULT,
1133                                  mdi_client, GetModuleHandle(0),
1134                                  (LPARAM)mdi_lParam_test_message);
1135     ok(mdi_child != 0, "MDI child creation failed\n");
1136     ok(GetWindowLongPtrA(mdi_child, GWLP_ID) == first_id, "wrong child id %d\n", GetWindowLongPtrA(mdi_child, GWLP_ID));
1137     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1138     ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1139
1140     mdi_child = CreateMDIWindowA("MDI_child_Class_1", "MDI child",
1141                                  0xffffffff, /* with WS_POPUP */
1142                                  CW_USEDEFAULT, CW_USEDEFAULT,
1143                                  CW_USEDEFAULT, CW_USEDEFAULT,
1144                                  mdi_client, GetModuleHandle(0),
1145                                  (LPARAM)mdi_lParam_test_message);
1146     if (GetWindowLongA(mdi_client, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1147     {
1148         ok(!mdi_child, "MDI child with WS_POPUP and with MDIS_ALLCHILDSTYLES should fail\n");
1149     }
1150     else
1151     {
1152         ok(mdi_child != 0, "MDI child creation failed\n");
1153         ok(GetWindowLongPtrA(mdi_child, GWLP_ID) == first_id, "wrong child id %d\n", GetWindowLongPtrA(mdi_child, GWLP_ID));
1154         SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1155         ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1156     }
1157
1158     /* test MDICREATESTRUCT A<->W mapping */
1159     SetLastError(0xdeadbeef);
1160     mdi_child = CreateMDIWindowW(classW, titleW,
1161                                  0,
1162                                  CW_USEDEFAULT, CW_USEDEFAULT,
1163                                  CW_USEDEFAULT, CW_USEDEFAULT,
1164                                  mdi_client, GetModuleHandle(0),
1165                                  (LPARAM)mdi_lParam_test_message);
1166     if (!mdi_child)
1167     {
1168         if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1169             isWin9x = TRUE;
1170         else
1171             ok(mdi_child != 0, "MDI child creation failed\n");
1172     }
1173     else
1174     {
1175         ok(GetWindowLongPtrA(mdi_child, GWLP_ID) == first_id, "wrong child id %d\n", GetWindowLongPtrA(mdi_child, GWLP_ID));
1176         SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1177         ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1178     }
1179
1180     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_1", "MDI child",
1181                                 0,
1182                                 CW_USEDEFAULT, CW_USEDEFAULT,
1183                                 CW_USEDEFAULT, CW_USEDEFAULT,
1184                                 mdi_client, 0, GetModuleHandle(0),
1185                                 (LPVOID)mdi_lParam_test_message);
1186     ok(mdi_child != 0, "MDI child creation failed\n");
1187     ok(GetWindowLongPtrA(mdi_child, GWLP_ID) == first_id, "wrong child id %d\n", GetWindowLongPtrA(mdi_child, GWLP_ID));
1188     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1189     ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1190
1191     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_1", "MDI child",
1192                                 0x7fffffff, /* without WS_POPUP */
1193                                 CW_USEDEFAULT, CW_USEDEFAULT,
1194                                 CW_USEDEFAULT, CW_USEDEFAULT,
1195                                 mdi_client, 0, GetModuleHandle(0),
1196                                 (LPVOID)mdi_lParam_test_message);
1197     ok(mdi_child != 0, "MDI child creation failed\n");
1198     ok(GetWindowLongPtrA(mdi_child, GWLP_ID) == first_id, "wrong child id %d\n", GetWindowLongPtrA(mdi_child, GWLP_ID));
1199     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1200     ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1201
1202     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_1", "MDI child",
1203                                 0xffffffff, /* with WS_POPUP */
1204                                 CW_USEDEFAULT, CW_USEDEFAULT,
1205                                 CW_USEDEFAULT, CW_USEDEFAULT,
1206                                 mdi_client, 0, GetModuleHandle(0),
1207                                 (LPVOID)mdi_lParam_test_message);
1208     if (GetWindowLongA(mdi_client, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1209     {
1210         ok(!mdi_child, "MDI child with WS_POPUP and with MDIS_ALLCHILDSTYLES should fail\n");
1211     }
1212     else
1213     {
1214         ok(mdi_child != 0, "MDI child creation failed\n");
1215         ok(GetWindowLongPtrA(mdi_child, GWLP_ID) == first_id, "wrong child id %d\n", GetWindowLongPtrA(mdi_child, GWLP_ID));
1216         SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1217         ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1218     }
1219
1220     /* test MDICREATESTRUCT A<->W mapping */
1221     SetLastError(0xdeadbeef);
1222     mdi_child = CreateWindowExW(WS_EX_MDICHILD, classW, titleW,
1223                                 0,
1224                                 CW_USEDEFAULT, CW_USEDEFAULT,
1225                                 CW_USEDEFAULT, CW_USEDEFAULT,
1226                                 mdi_client, 0, GetModuleHandle(0),
1227                                 (LPVOID)mdi_lParam_test_message);
1228     if (!mdi_child)
1229     {
1230         if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1231             isWin9x = TRUE;
1232         else
1233             ok(mdi_child != 0, "MDI child creation failed\n");
1234     }
1235     else
1236     {
1237         ok(GetWindowLongPtrA(mdi_child, GWLP_ID) == first_id, "wrong child id %d\n", GetWindowLongPtrA(mdi_child, GWLP_ID));
1238         SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1239         ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1240     }
1241
1242     /* This test fails on Win9x */
1243     if (!isWin9x)
1244     {
1245         mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_2", "MDI child",
1246                                 WS_CHILD,
1247                                 CW_USEDEFAULT, CW_USEDEFAULT,
1248                                 CW_USEDEFAULT, CW_USEDEFAULT,
1249                                 parent, 0, GetModuleHandle(0),
1250                                 (LPVOID)mdi_lParam_test_message);
1251         ok(!mdi_child, "WS_EX_MDICHILD with a not MDIClient parent should fail\n");
1252     }
1253
1254     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1255                                 WS_CHILD, /* without WS_POPUP */
1256                                 CW_USEDEFAULT, CW_USEDEFAULT,
1257                                 CW_USEDEFAULT, CW_USEDEFAULT,
1258                                 mdi_client, 0, GetModuleHandle(0),
1259                                 (LPVOID)mdi_lParam_test_message);
1260     ok(mdi_child != 0, "MDI child creation failed\n");
1261     ok(GetWindowLongPtrA(mdi_child, GWLP_ID) == 0, "wrong child id %d\n", GetWindowLongPtrA(mdi_child, GWLP_ID));
1262     DestroyWindow(mdi_child);
1263
1264     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1265                                 WS_CHILD | WS_POPUP, /* with WS_POPUP */
1266                                 CW_USEDEFAULT, CW_USEDEFAULT,
1267                                 CW_USEDEFAULT, CW_USEDEFAULT,
1268                                 mdi_client, 0, GetModuleHandle(0),
1269                                 (LPVOID)mdi_lParam_test_message);
1270     ok(mdi_child != 0, "MDI child creation failed\n");
1271     ok(GetWindowLongPtrA(mdi_child, GWLP_ID) == 0, "wrong child id %d\n", GetWindowLongPtrA(mdi_child, GWLP_ID));
1272     DestroyWindow(mdi_child);
1273
1274     /* maximized child */
1275     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1276                                 WS_CHILD | WS_MAXIMIZE,
1277                                 CW_USEDEFAULT, CW_USEDEFAULT,
1278                                 CW_USEDEFAULT, CW_USEDEFAULT,
1279                                 mdi_client, 0, GetModuleHandle(0),
1280                                 (LPVOID)mdi_lParam_test_message);
1281     ok(mdi_child != 0, "MDI child creation failed\n");
1282     ok(GetWindowLongPtrA(mdi_child, GWLP_ID) == 0, "wrong child id %d\n", GetWindowLongPtrA(mdi_child, GWLP_ID));
1283     DestroyWindow(mdi_child);
1284
1285     trace("Creating maximized child with a caption\n");
1286     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1287                                 WS_CHILD | WS_MAXIMIZE | WS_CAPTION,
1288                                 CW_USEDEFAULT, CW_USEDEFAULT,
1289                                 CW_USEDEFAULT, CW_USEDEFAULT,
1290                                 mdi_client, 0, GetModuleHandle(0),
1291                                 (LPVOID)mdi_lParam_test_message);
1292     ok(mdi_child != 0, "MDI child creation failed\n");
1293     ok(GetWindowLongPtrA(mdi_child, GWLP_ID) == 0, "wrong child id %d\n", GetWindowLongPtrA(mdi_child, GWLP_ID));
1294     DestroyWindow(mdi_child);
1295
1296     trace("Creating maximized child with a caption and a thick frame\n");
1297     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1298                                 WS_CHILD | WS_MAXIMIZE | WS_CAPTION | WS_THICKFRAME,
1299                                 CW_USEDEFAULT, CW_USEDEFAULT,
1300                                 CW_USEDEFAULT, CW_USEDEFAULT,
1301                                 mdi_client, 0, GetModuleHandle(0),
1302                                 (LPVOID)mdi_lParam_test_message);
1303     ok(mdi_child != 0, "MDI child creation failed\n");
1304     ok(GetWindowLongPtrA(mdi_child, GWLP_ID) == 0, "wrong child id %d\n", GetWindowLongPtrA(mdi_child, GWLP_ID));
1305     DestroyWindow(mdi_child);
1306 }
1307
1308 /**********************************************************************
1309  * MDI_ChildGetMinMaxInfo (copied from windows/mdi.c)
1310  *
1311  * Note: The rule here is that client rect of the maximized MDI child
1312  *       is equal to the client rect of the MDI client window.
1313  */
1314 static void MDI_ChildGetMinMaxInfo( HWND client, HWND hwnd, MINMAXINFO* lpMinMax )
1315 {
1316     RECT rect;
1317
1318     GetClientRect( client, &rect );
1319     AdjustWindowRectEx( &rect, GetWindowLongA( hwnd, GWL_STYLE ),
1320                         0, GetWindowLongA( hwnd, GWL_EXSTYLE ));
1321
1322     rect.right -= rect.left;
1323     rect.bottom -= rect.top;
1324     lpMinMax->ptMaxSize.x = rect.right;
1325     lpMinMax->ptMaxSize.y = rect.bottom;
1326
1327     lpMinMax->ptMaxPosition.x = rect.left;
1328     lpMinMax->ptMaxPosition.y = rect.top;
1329
1330     trace("max rect (%d,%d - %d, %d)\n",
1331            rect.left, rect.top, rect.right, rect.bottom);
1332 }
1333
1334 static LRESULT WINAPI mdi_child_wnd_proc_1(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1335 {
1336     switch (msg)
1337     {
1338         case WM_NCCREATE:
1339         case WM_CREATE:
1340         {
1341             CREATESTRUCTA *cs = (CREATESTRUCTA *)lparam;
1342             MDICREATESTRUCTA *mdi_cs = (MDICREATESTRUCTA *)cs->lpCreateParams;
1343
1344             ok(cs->dwExStyle & WS_EX_MDICHILD, "WS_EX_MDICHILD should be set\n");
1345             ok(mdi_cs->lParam == (LPARAM)mdi_lParam_test_message, "wrong mdi_cs->lParam\n");
1346
1347             ok(!lstrcmpA(cs->lpszClass, "MDI_child_Class_1"), "wrong class name\n");
1348             ok(!lstrcmpA(cs->lpszClass, mdi_cs->szClass), "class name does not match\n");
1349             ok(!lstrcmpA(cs->lpszName, "MDI child"), "wrong title\n");
1350             ok(!lstrcmpA(cs->lpszName, mdi_cs->szTitle), "title does not match\n");
1351             ok(cs->hInstance == mdi_cs->hOwner, "%p != %p\n", cs->hInstance, mdi_cs->hOwner);
1352
1353             /* MDICREATESTRUCT should have original values */
1354             ok(mdi_cs->style == 0 || mdi_cs->style == 0x7fffffff || mdi_cs->style == 0xffffffff,
1355                 "mdi_cs->style does not match (%08x)\n", mdi_cs->style);
1356             ok(mdi_cs->x == CW_USEDEFAULT, "%d != CW_USEDEFAULT\n", mdi_cs->x);
1357             ok(mdi_cs->y == CW_USEDEFAULT, "%d != CW_USEDEFAULT\n", mdi_cs->y);
1358             ok(mdi_cs->cx == CW_USEDEFAULT, "%d != CW_USEDEFAULT\n", mdi_cs->cx);
1359             ok(mdi_cs->cy == CW_USEDEFAULT, "%d != CW_USEDEFAULT\n", mdi_cs->cy);
1360
1361             /* CREATESTRUCT should have fixed values */
1362             ok(cs->x != CW_USEDEFAULT, "%d == CW_USEDEFAULT\n", cs->x);
1363             ok(cs->y != CW_USEDEFAULT, "%d == CW_USEDEFAULT\n", cs->y);
1364
1365             /* cx/cy == CW_USEDEFAULT are translated to NOT zero values */
1366             ok(cs->cx != CW_USEDEFAULT && cs->cx != 0, "%d == CW_USEDEFAULT\n", cs->cx);
1367             ok(cs->cy != CW_USEDEFAULT && cs->cy != 0, "%d == CW_USEDEFAULT\n", cs->cy);
1368
1369             ok(!(cs->style & WS_POPUP), "WS_POPUP is not allowed\n");
1370
1371             if (GetWindowLongA(cs->hwndParent, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1372             {
1373                 LONG style = mdi_cs->style | WS_CHILD | WS_CLIPSIBLINGS;
1374                 ok(cs->style == style,
1375                    "cs->style does not match (%08x)\n", cs->style);
1376             }
1377             else
1378             {
1379                 LONG style = mdi_cs->style;
1380                 style &= ~WS_POPUP;
1381                 style |= WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CAPTION |
1382                     WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
1383                 ok(cs->style == style,
1384                    "cs->style does not match (%08x)\n", cs->style);
1385             }
1386             break;
1387         }
1388
1389         case WM_GETMINMAXINFO:
1390         {
1391             HWND client = GetParent(hwnd);
1392             RECT rc;
1393             MINMAXINFO *minmax = (MINMAXINFO *)lparam;
1394             MINMAXINFO my_minmax;
1395             LONG style, exstyle;
1396
1397             style = GetWindowLongA(hwnd, GWL_STYLE);
1398             exstyle = GetWindowLongA(hwnd, GWL_EXSTYLE);
1399
1400             GetWindowRect(client, &rc);
1401             trace("MDI client %p window size = (%d x %d)\n", client, rc.right-rc.left, rc.bottom-rc.top);
1402             GetClientRect(client, &rc);
1403             trace("MDI client %p client size = (%d x %d)\n", client, rc.right, rc.bottom);
1404             trace("screen size: %d x %d\n", GetSystemMetrics(SM_CXSCREEN),
1405                                             GetSystemMetrics(SM_CYSCREEN));
1406
1407             GetClientRect(client, &rc);
1408             if ((style & WS_CAPTION) == WS_CAPTION)
1409                 style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
1410             AdjustWindowRectEx(&rc, style, 0, exstyle);
1411             trace("MDI child: calculated max window size = (%d x %d)\n", rc.right-rc.left, rc.bottom-rc.top);
1412
1413             trace("ptReserved = (%d,%d)\n"
1414                   "ptMaxSize = (%d,%d)\n"
1415                   "ptMaxPosition = (%d,%d)\n"
1416                   "ptMinTrackSize = (%d,%d)\n"
1417                   "ptMaxTrackSize = (%d,%d)\n",
1418                   minmax->ptReserved.x, minmax->ptReserved.y,
1419                   minmax->ptMaxSize.x, minmax->ptMaxSize.y,
1420                   minmax->ptMaxPosition.x, minmax->ptMaxPosition.y,
1421                   minmax->ptMinTrackSize.x, minmax->ptMinTrackSize.y,
1422                   minmax->ptMaxTrackSize.x, minmax->ptMaxTrackSize.y);
1423
1424             ok(minmax->ptMaxSize.x == rc.right - rc.left, "default width of maximized child %d != %d\n",
1425                minmax->ptMaxSize.x, rc.right - rc.left);
1426             ok(minmax->ptMaxSize.y == rc.bottom - rc.top, "default height of maximized child %d != %d\n",
1427                minmax->ptMaxSize.y, rc.bottom - rc.top);
1428
1429             DefMDIChildProcA(hwnd, msg, wparam, lparam);
1430
1431             trace("DefMDIChildProc returned:\n"
1432                   "ptReserved = (%d,%d)\n"
1433                   "ptMaxSize = (%d,%d)\n"
1434                   "ptMaxPosition = (%d,%d)\n"
1435                   "ptMinTrackSize = (%d,%d)\n"
1436                   "ptMaxTrackSize = (%d,%d)\n",
1437                   minmax->ptReserved.x, minmax->ptReserved.y,
1438                   minmax->ptMaxSize.x, minmax->ptMaxSize.y,
1439                   minmax->ptMaxPosition.x, minmax->ptMaxPosition.y,
1440                   minmax->ptMinTrackSize.x, minmax->ptMinTrackSize.y,
1441                   minmax->ptMaxTrackSize.x, minmax->ptMaxTrackSize.y);
1442
1443             MDI_ChildGetMinMaxInfo(client, hwnd, &my_minmax);
1444             ok(minmax->ptMaxSize.x == my_minmax.ptMaxSize.x, "default width of maximized child %d != %d\n",
1445                minmax->ptMaxSize.x, my_minmax.ptMaxSize.x);
1446             ok(minmax->ptMaxSize.y == my_minmax.ptMaxSize.y, "default height of maximized child %d != %d\n",
1447                minmax->ptMaxSize.y, my_minmax.ptMaxSize.y);
1448
1449             return 1;
1450         }
1451
1452         case WM_MDIACTIVATE:
1453         {
1454             HWND active, client = GetParent(hwnd);
1455             /*trace("%p WM_MDIACTIVATE %08x %08lx\n", hwnd, wparam, lparam);*/
1456             active = (HWND)SendMessageA(client, WM_MDIGETACTIVE, 0, 0);
1457             if (hwnd == (HWND)lparam) /* if we are being activated */
1458                 ok (active == (HWND)lparam, "new active %p != active %p\n", (HWND)lparam, active);
1459             else
1460                 ok (active == (HWND)wparam, "old active %p != active %p\n", (HWND)wparam, active);
1461             break;
1462         }
1463     }
1464     return DefMDIChildProcA(hwnd, msg, wparam, lparam);
1465 }
1466
1467 static LRESULT WINAPI mdi_child_wnd_proc_2(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1468 {
1469     switch (msg)
1470     {
1471         case WM_NCCREATE:
1472         case WM_CREATE:
1473         {
1474             CREATESTRUCTA *cs = (CREATESTRUCTA *)lparam;
1475
1476             trace("%s\n", (msg == WM_NCCREATE) ? "WM_NCCREATE" : "WM_CREATE");
1477             trace("x %d, y %d, cx %d, cy %d\n", cs->x, cs->y, cs->cx, cs->cy);
1478
1479             ok(!(cs->dwExStyle & WS_EX_MDICHILD), "WS_EX_MDICHILD should not be set\n");
1480             ok(cs->lpCreateParams == mdi_lParam_test_message, "wrong cs->lpCreateParams\n");
1481
1482             ok(!lstrcmpA(cs->lpszClass, "MDI_child_Class_2"), "wrong class name\n");
1483             ok(!lstrcmpA(cs->lpszName, "MDI child"), "wrong title\n");
1484
1485             /* CREATESTRUCT should have fixed values */
1486             /* For some reason Win9x doesn't translate cs->x from CW_USEDEFAULT,
1487                while NT does. */
1488             /*ok(cs->x != CW_USEDEFAULT, "%d == CW_USEDEFAULT\n", cs->x);*/
1489             ok(cs->y != CW_USEDEFAULT, "%d == CW_USEDEFAULT\n", cs->y);
1490
1491             /* cx/cy == CW_USEDEFAULT are translated to 0 */
1492             /* For some reason Win98 doesn't translate cs->cx from CW_USEDEFAULT,
1493                while Win95, Win2k, WinXP do. */
1494             /*ok(cs->cx == 0, "%d != 0\n", cs->cx);*/
1495             ok(cs->cy == 0, "%d != 0\n", cs->cy);
1496             break;
1497         }
1498
1499         case WM_GETMINMAXINFO:
1500         {
1501             HWND parent = GetParent(hwnd);
1502             RECT rc;
1503             MINMAXINFO *minmax = (MINMAXINFO *)lparam;
1504             LONG style, exstyle;
1505
1506             trace("WM_GETMINMAXINFO\n");
1507
1508             style = GetWindowLongA(hwnd, GWL_STYLE);
1509             exstyle = GetWindowLongA(hwnd, GWL_EXSTYLE);
1510
1511             GetClientRect(parent, &rc);
1512             trace("parent %p client size = (%d x %d)\n", parent, rc.right, rc.bottom);
1513
1514             GetClientRect(parent, &rc);
1515             if ((style & WS_CAPTION) == WS_CAPTION)
1516                 style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
1517             AdjustWindowRectEx(&rc, style, 0, exstyle);
1518             trace("calculated max child window size = (%d x %d)\n", rc.right-rc.left, rc.bottom-rc.top);
1519
1520             trace("ptReserved = (%d,%d)\n"
1521                   "ptMaxSize = (%d,%d)\n"
1522                   "ptMaxPosition = (%d,%d)\n"
1523                   "ptMinTrackSize = (%d,%d)\n"
1524                   "ptMaxTrackSize = (%d,%d)\n",
1525                   minmax->ptReserved.x, minmax->ptReserved.y,
1526                   minmax->ptMaxSize.x, minmax->ptMaxSize.y,
1527                   minmax->ptMaxPosition.x, minmax->ptMaxPosition.y,
1528                   minmax->ptMinTrackSize.x, minmax->ptMinTrackSize.y,
1529                   minmax->ptMaxTrackSize.x, minmax->ptMaxTrackSize.y);
1530
1531             ok(minmax->ptMaxSize.x == rc.right - rc.left, "default width of maximized child %d != %d\n",
1532                minmax->ptMaxSize.x, rc.right - rc.left);
1533             ok(minmax->ptMaxSize.y == rc.bottom - rc.top, "default height of maximized child %d != %d\n",
1534                minmax->ptMaxSize.y, rc.bottom - rc.top);
1535             break;
1536         }
1537
1538         case WM_WINDOWPOSCHANGED:
1539         {
1540             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
1541             RECT rc1, rc2;
1542
1543             GetWindowRect(hwnd, &rc1);
1544             trace("window: (%d,%d)-(%d,%d)\n", rc1.left, rc1.top, rc1.right, rc1.bottom);
1545             SetRect(&rc2, winpos->x, winpos->y, winpos->x + winpos->cx, winpos->y + winpos->cy);
1546             /* note: winpos coordinates are relative to parent */
1547             MapWindowPoints(GetParent(hwnd), 0, (LPPOINT)&rc2, 2);
1548             trace("pos: (%d,%d)-(%d,%d)\n", rc2.left, rc2.top, rc2.right, rc2.bottom);
1549             ok(EqualRect(&rc1, &rc2), "rects do not match\n");
1550
1551             GetWindowRect(hwnd, &rc1);
1552             GetClientRect(hwnd, &rc2);
1553             DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc1);
1554             MapWindowPoints(0, hwnd, (LPPOINT)&rc1, 2);
1555             ok(EqualRect(&rc1, &rc2), "rects do not match\n");
1556         }
1557         /* fall through */
1558         case WM_WINDOWPOSCHANGING:
1559         {
1560             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
1561             WINDOWPOS my_winpos = *winpos;
1562
1563             trace("%s\n", (msg == WM_WINDOWPOSCHANGING) ? "WM_WINDOWPOSCHANGING" : "WM_WINDOWPOSCHANGED");
1564             trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
1565                   winpos->hwnd, winpos->hwndInsertAfter,
1566                   winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
1567
1568             DefWindowProcA(hwnd, msg, wparam, lparam);
1569
1570             trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
1571                   winpos->hwnd, winpos->hwndInsertAfter,
1572                   winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
1573
1574             ok(!memcmp(&my_winpos, winpos, sizeof(WINDOWPOS)),
1575                "DefWindowProc should not change WINDOWPOS values\n");
1576
1577             return 1;
1578         }
1579     }
1580     return DefWindowProcA(hwnd, msg, wparam, lparam);
1581 }
1582
1583 static LRESULT WINAPI mdi_main_wnd_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1584 {
1585     static HWND mdi_client;
1586
1587     switch (msg)
1588     {
1589         case WM_CREATE:
1590         {
1591             CLIENTCREATESTRUCT client_cs;
1592             RECT rc;
1593
1594             GetClientRect(hwnd, &rc);
1595
1596             client_cs.hWindowMenu = 0;
1597             client_cs.idFirstChild = 1;
1598
1599             /* MDIClient without MDIS_ALLCHILDSTYLES */
1600             mdi_client = CreateWindowExA(0, "mdiclient",
1601                                          NULL,
1602                                          WS_CHILD /*| WS_VISIBLE*/,
1603                                           /* tests depend on a not zero MDIClient size */
1604                                          0, 0, rc.right, rc.bottom,
1605                                          hwnd, 0, GetModuleHandle(0),
1606                                          (LPVOID)&client_cs);
1607             assert(mdi_client);
1608             test_MDI_create(hwnd, mdi_client, client_cs.idFirstChild);
1609             DestroyWindow(mdi_client);
1610
1611             /* MDIClient with MDIS_ALLCHILDSTYLES */
1612             mdi_client = CreateWindowExA(0, "mdiclient",
1613                                          NULL,
1614                                          WS_CHILD | MDIS_ALLCHILDSTYLES /*| WS_VISIBLE*/,
1615                                           /* tests depend on a not zero MDIClient size */
1616                                          0, 0, rc.right, rc.bottom,
1617                                          hwnd, 0, GetModuleHandle(0),
1618                                          (LPVOID)&client_cs);
1619             assert(mdi_client);
1620             test_MDI_create(hwnd, mdi_client, client_cs.idFirstChild);
1621             DestroyWindow(mdi_client);
1622             break;
1623         }
1624
1625         case WM_WINDOWPOSCHANGED:
1626         {
1627             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
1628             RECT rc1, rc2;
1629
1630             GetWindowRect(hwnd, &rc1);
1631             trace("window: (%d,%d)-(%d,%d)\n", rc1.left, rc1.top, rc1.right, rc1.bottom);
1632             SetRect(&rc2, winpos->x, winpos->y, winpos->x + winpos->cx, winpos->y + winpos->cy);
1633             /* note: winpos coordinates are relative to parent */
1634             MapWindowPoints(GetParent(hwnd), 0, (LPPOINT)&rc2, 2);
1635             trace("pos: (%d,%d)-(%d,%d)\n", rc2.left, rc2.top, rc2.right, rc2.bottom);
1636             ok(EqualRect(&rc1, &rc2), "rects do not match\n");
1637
1638             GetWindowRect(hwnd, &rc1);
1639             GetClientRect(hwnd, &rc2);
1640             DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc1);
1641             MapWindowPoints(0, hwnd, (LPPOINT)&rc1, 2);
1642             ok(EqualRect(&rc1, &rc2), "rects do not match\n");
1643         }
1644         /* fall through */
1645         case WM_WINDOWPOSCHANGING:
1646         {
1647             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
1648             WINDOWPOS my_winpos = *winpos;
1649
1650             trace("%s\n", (msg == WM_WINDOWPOSCHANGING) ? "WM_WINDOWPOSCHANGING" : "WM_WINDOWPOSCHANGED");
1651             trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
1652                   winpos->hwnd, winpos->hwndInsertAfter,
1653                   winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
1654
1655             DefWindowProcA(hwnd, msg, wparam, lparam);
1656
1657             trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
1658                   winpos->hwnd, winpos->hwndInsertAfter,
1659                   winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
1660
1661             ok(!memcmp(&my_winpos, winpos, sizeof(WINDOWPOS)),
1662                "DefWindowProc should not change WINDOWPOS values\n");
1663
1664             return 1;
1665         }
1666
1667         case WM_CLOSE:
1668             PostQuitMessage(0);
1669             break;
1670     }
1671     return DefFrameProcA(hwnd, mdi_client, msg, wparam, lparam);
1672 }
1673
1674 static BOOL mdi_RegisterWindowClasses(void)
1675 {
1676     WNDCLASSA cls;
1677
1678     cls.style = 0;
1679     cls.lpfnWndProc = mdi_main_wnd_procA;
1680     cls.cbClsExtra = 0;
1681     cls.cbWndExtra = 0;
1682     cls.hInstance = GetModuleHandleA(0);
1683     cls.hIcon = 0;
1684     cls.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
1685     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
1686     cls.lpszMenuName = NULL;
1687     cls.lpszClassName = "MDI_parent_Class";
1688     if(!RegisterClassA(&cls)) return FALSE;
1689
1690     cls.lpfnWndProc = mdi_child_wnd_proc_1;
1691     cls.lpszClassName = "MDI_child_Class_1";
1692     if(!RegisterClassA(&cls)) return FALSE;
1693
1694     cls.lpfnWndProc = mdi_child_wnd_proc_2;
1695     cls.lpszClassName = "MDI_child_Class_2";
1696     if(!RegisterClassA(&cls)) return FALSE;
1697
1698     return TRUE;
1699 }
1700
1701 static void test_mdi(void)
1702 {
1703     HWND mdi_hwndMain;
1704     /*MSG msg;*/
1705
1706     if (!mdi_RegisterWindowClasses()) assert(0);
1707
1708     mdi_hwndMain = CreateWindowExA(0, "MDI_parent_Class", "MDI parent window",
1709                                    WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
1710                                    WS_MAXIMIZEBOX /*| WS_VISIBLE*/,
1711                                    100, 100, CW_USEDEFAULT, CW_USEDEFAULT,
1712                                    GetDesktopWindow(), 0,
1713                                    GetModuleHandle(0), NULL);
1714     assert(mdi_hwndMain);
1715 /*
1716     while(GetMessage(&msg, 0, 0, 0))
1717     {
1718         TranslateMessage(&msg);
1719         DispatchMessage(&msg);
1720     }
1721 */
1722 }
1723
1724 static void test_icons(void)
1725 {
1726     WNDCLASSEXA cls;
1727     HWND hwnd;
1728     HICON icon = LoadIconA(0, (LPSTR)IDI_APPLICATION);
1729     HICON icon2 = LoadIconA(0, (LPSTR)IDI_QUESTION);
1730     HICON small_icon = LoadImageA(0, (LPSTR)IDI_APPLICATION, IMAGE_ICON,
1731                                   GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED );
1732     HICON res;
1733
1734     cls.cbSize = sizeof(cls);
1735     cls.style = 0;
1736     cls.lpfnWndProc = DefWindowProcA;
1737     cls.cbClsExtra = 0;
1738     cls.cbWndExtra = 0;
1739     cls.hInstance = 0;
1740     cls.hIcon = LoadIconA(0, (LPSTR)IDI_HAND);
1741     cls.hIconSm = small_icon;
1742     cls.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
1743     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
1744     cls.lpszMenuName = NULL;
1745     cls.lpszClassName = "IconWindowClass";
1746
1747     RegisterClassExA(&cls);
1748
1749     hwnd = CreateWindowExA(0, "IconWindowClass", "icon test", 0,
1750                            100, 100, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, NULL, NULL);
1751     assert( hwnd );
1752
1753     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_BIG, 0 );
1754     ok( res == 0, "wrong big icon %p/0\n", res );
1755     res = (HICON)SendMessageA( hwnd, WM_SETICON, ICON_BIG, (LPARAM)icon );
1756     ok( res == 0, "wrong previous big icon %p/0\n", res );
1757     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_BIG, 0 );
1758     ok( res == icon, "wrong big icon after set %p/%p\n", res, icon );
1759     res = (HICON)SendMessageA( hwnd, WM_SETICON, ICON_BIG, (LPARAM)icon2 );
1760     ok( res == icon, "wrong previous big icon %p/%p\n", res, icon );
1761     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_BIG, 0 );
1762     ok( res == icon2, "wrong big icon after set %p/%p\n", res, icon2 );
1763
1764     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL, 0 );
1765     ok( res == 0, "wrong small icon %p/0\n", res );
1766     /* this test is XP specific */
1767     /*res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL2, 0 );
1768     ok( res != 0, "wrong small icon %p\n", res );*/
1769     res = (HICON)SendMessageA( hwnd, WM_SETICON, ICON_SMALL, (LPARAM)icon );
1770     ok( res == 0, "wrong previous small icon %p/0\n", res );
1771     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL, 0 );
1772     ok( res == icon, "wrong small icon after set %p/%p\n", res, icon );
1773     /* this test is XP specific */
1774     /*res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL2, 0 );
1775     ok( res == icon, "wrong small icon after set %p/%p\n", res, icon );*/
1776     res = (HICON)SendMessageA( hwnd, WM_SETICON, ICON_SMALL, (LPARAM)small_icon );
1777     ok( res == icon, "wrong previous small icon %p/%p\n", res, icon );
1778     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL, 0 );
1779     ok( res == small_icon, "wrong small icon after set %p/%p\n", res, small_icon );
1780     /* this test is XP specific */
1781     /*res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL2, 0 );
1782     ok( res == small_icon, "wrong small icon after set %p/%p\n", res, small_icon );*/
1783
1784     /* make sure the big icon hasn't changed */
1785     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_BIG, 0 );
1786     ok( res == icon2, "wrong big icon after set %p/%p\n", res, icon2 );
1787 }
1788
1789 static LRESULT WINAPI nccalcsize_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1790 {
1791     if (msg == WM_NCCALCSIZE)
1792     {
1793         RECT *rect = (RECT *)lparam;
1794         /* first time around increase the rectangle, next time decrease it */
1795         if (rect->left == 100) InflateRect( rect, 10, 10 );
1796         else InflateRect( rect, -10, -10 );
1797         return 0;
1798     }
1799     return DefWindowProc( hwnd, msg, wparam, lparam );
1800 }
1801
1802 static void test_SetWindowPos(HWND hwnd)
1803 {
1804     RECT orig_win_rc, rect;
1805     LONG_PTR old_proc;
1806     BOOL is_win9x = GetWindowLongPtrW(hwnd, GWLP_WNDPROC) == 0;
1807
1808     SetRect(&rect, 111, 222, 333, 444);
1809     ok(!GetWindowRect(0, &rect), "GetWindowRect succeeded\n");
1810     ok(rect.left == 111 && rect.top == 222 && rect.right == 333 && rect.bottom == 444,
1811        "wrong window rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
1812
1813     SetRect(&rect, 111, 222, 333, 444);
1814     ok(!GetClientRect(0, &rect), "GetClientRect succeeded\n");
1815     ok(rect.left == 111 && rect.top == 222 && rect.right == 333 && rect.bottom == 444,
1816        "wrong window rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
1817
1818     GetWindowRect(hwnd, &orig_win_rc);
1819
1820     old_proc = SetWindowLongPtr( hwnd, GWLP_WNDPROC, (ULONG_PTR)nccalcsize_proc );
1821     SetWindowPos(hwnd, 0, 100, 100, 0, 0, SWP_NOZORDER|SWP_FRAMECHANGED);
1822     GetWindowRect( hwnd, &rect );
1823     ok( rect.left == 100 && rect.top == 100 && rect.right == 100 && rect.bottom == 100,
1824         "invalid window rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
1825     GetClientRect( hwnd, &rect );
1826     MapWindowPoints( hwnd, 0, (POINT *)&rect, 2 );
1827     ok( rect.left == 90 && rect.top == 90 && rect.right == 110 && rect.bottom == 110,
1828         "invalid client rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
1829
1830     SetWindowPos(hwnd, 0, 200, 200, 0, 0, SWP_NOZORDER|SWP_FRAMECHANGED);
1831     GetWindowRect( hwnd, &rect );
1832     ok( rect.left == 200 && rect.top == 200 && rect.right == 200 && rect.bottom == 200,
1833         "invalid window rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
1834     GetClientRect( hwnd, &rect );
1835     MapWindowPoints( hwnd, 0, (POINT *)&rect, 2 );
1836     ok( rect.left == 210 && rect.top == 210 && rect.right == 190 && rect.bottom == 190,
1837         "invalid client rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
1838
1839     SetWindowPos(hwnd, 0, orig_win_rc.left, orig_win_rc.top,
1840                  orig_win_rc.right, orig_win_rc.bottom, 0);
1841     SetWindowLongPtr( hwnd, GWLP_WNDPROC, old_proc );
1842
1843     /* Win9x truncates coordinates to 16-bit irrespectively */
1844     if (!is_win9x)
1845     {
1846         SetWindowPos(hwnd, 0, -32769, -40000, -32769, -90000, SWP_NOMOVE);
1847         SetWindowPos(hwnd, 0, 32768, 40000, 32768, 40000, SWP_NOMOVE);
1848
1849         SetWindowPos(hwnd, 0, -32769, -40000, -32769, -90000, SWP_NOSIZE);
1850         SetWindowPos(hwnd, 0, 32768, 40000, 32768, 40000, SWP_NOSIZE);
1851     }
1852
1853     SetWindowPos(hwnd, 0, orig_win_rc.left, orig_win_rc.top,
1854                  orig_win_rc.right, orig_win_rc.bottom, 0);
1855 }
1856
1857 static void test_SetMenu(HWND parent)
1858 {
1859     HWND child;
1860     HMENU hMenu, ret;
1861     BOOL is_win9x = GetWindowLongPtrW(parent, GWLP_WNDPROC) == 0;
1862     BOOL retok;
1863     DWORD style;
1864
1865     hMenu = CreateMenu();
1866     assert(hMenu);
1867
1868     ok(SetMenu(parent, hMenu), "SetMenu on a top level window should not fail\n");
1869 #if 0
1870     /* fails on (at least) Wine, NT4, XP SP2 */
1871     test_nonclient_area(parent); 
1872 #endif
1873     ret = GetMenu(parent);
1874     ok(ret == hMenu, "unexpected menu id %p\n", ret);
1875     /* test whether we can destroy a menu assigned to a window */
1876     retok = DestroyMenu(hMenu);
1877     ok( retok, "DestroyMenu error %d\n", GetLastError());
1878     ok(!IsMenu(hMenu), "menu handle should be not valid after DestroyMenu\n");
1879     ret = GetMenu(parent);
1880     /* This test fails on Win9x */
1881     if (!is_win9x)
1882         ok(ret == hMenu, "unexpected menu id %p\n", ret);
1883     ok(SetMenu(parent, 0), "SetMenu(0) on a top level window should not fail\n");
1884     test_nonclient_area(parent);
1885
1886     hMenu = CreateMenu();
1887     assert(hMenu);
1888
1889     /* parent */
1890     ret = GetMenu(parent);
1891     ok(ret == 0, "unexpected menu id %p\n", ret);
1892
1893     ok(!SetMenu(parent, (HMENU)20), "SetMenu with invalid menu handle should fail\n");
1894     test_nonclient_area(parent);
1895     ret = GetMenu(parent);
1896     ok(ret == 0, "unexpected menu id %p\n", ret);
1897
1898     ok(SetMenu(parent, hMenu), "SetMenu on a top level window should not fail\n");
1899 #if 0
1900     /* fails on (at least) Wine, NT4, XP SP2 */
1901     test_nonclient_area(parent);
1902 #endif
1903     ret = GetMenu(parent);
1904     ok(ret == hMenu, "unexpected menu id %p\n", ret);
1905
1906     ok(SetMenu(parent, 0), "SetMenu(0) on a top level window should not fail\n");
1907     test_nonclient_area(parent);
1908     ret = GetMenu(parent);
1909     ok(ret == 0, "unexpected menu id %p\n", ret);
1910  
1911     /* child */
1912     child = CreateWindowExA(0, "static", NULL, WS_CHILD, 0, 0, 0, 0, parent, (HMENU)10, 0, NULL);
1913     assert(child);
1914
1915     ret = GetMenu(child);
1916     ok(ret == (HMENU)10, "unexpected menu id %p\n", ret);
1917
1918     ok(!SetMenu(child, (HMENU)20), "SetMenu with invalid menu handle should fail\n");
1919     test_nonclient_area(child);
1920     ret = GetMenu(child);
1921     ok(ret == (HMENU)10, "unexpected menu id %p\n", ret);
1922
1923     ok(!SetMenu(child, hMenu), "SetMenu on a child window should fail\n");
1924     test_nonclient_area(child);
1925     ret = GetMenu(child);
1926     ok(ret == (HMENU)10, "unexpected menu id %p\n", ret);
1927
1928     ok(!SetMenu(child, 0), "SetMenu(0) on a child window should fail\n");
1929     test_nonclient_area(child);
1930     ret = GetMenu(child);
1931     ok(ret == (HMENU)10, "unexpected menu id %p\n", ret);
1932
1933     style = GetWindowLong(child, GWL_STYLE);
1934     SetWindowLong(child, GWL_STYLE, style | WS_POPUP);
1935     ok(SetMenu(child, hMenu), "SetMenu on a popup child window should not fail\n");
1936     ok(SetMenu(child, 0), "SetMenu on a popup child window should not fail\n");
1937     SetWindowLong(child, GWL_STYLE, style);
1938
1939     SetWindowLong(child, GWL_STYLE, style | WS_OVERLAPPED);
1940     ok(!SetMenu(child, hMenu), "SetMenu on a overlapped child window should fail\n");
1941     SetWindowLong(child, GWL_STYLE, style);
1942
1943     DestroyWindow(child);
1944     DestroyMenu(hMenu);
1945 }
1946
1947 static void test_window_tree(HWND parent, const DWORD *style, const int *order, int total)
1948 {
1949     HWND child[5], hwnd;
1950     int i;
1951
1952     assert(total <= 5);
1953
1954     hwnd = GetWindow(parent, GW_CHILD);
1955     ok(!hwnd, "have to start without children to perform the test\n");
1956
1957     for (i = 0; i < total; i++)
1958     {
1959         if (style[i] & DS_CONTROL)
1960         {
1961             child[i] = CreateWindowExA(0, MAKEINTATOMA(32770), "", style[i] & ~WS_VISIBLE,
1962                                        0,0,0,0, parent, (HMENU)i, 0, NULL);
1963             if (style[i] & WS_VISIBLE)
1964                 ShowWindow(child[i], SW_SHOW);
1965
1966             SetWindowPos(child[i], HWND_BOTTOM, 0,0,10,10, SWP_NOACTIVATE);
1967         }
1968         else
1969             child[i] = CreateWindowExA(0, "static", "", style[i], 0,0,10,10,
1970                                        parent, (HMENU)i, 0, NULL);
1971         trace("child[%d] = %p\n", i, child[i]);
1972         ok(child[i] != 0, "CreateWindowEx failed to create child window\n");
1973     }
1974
1975     hwnd = GetWindow(parent, GW_CHILD);
1976     ok(hwnd != 0, "GetWindow(GW_CHILD) failed\n");
1977     ok(hwnd == GetWindow(child[total - 1], GW_HWNDFIRST), "GW_HWNDFIRST is wrong\n");
1978     ok(child[order[total - 1]] == GetWindow(child[0], GW_HWNDLAST), "GW_HWNDLAST is wrong\n");
1979
1980     for (i = 0; i < total; i++)
1981     {
1982         trace("hwnd[%d] = %p\n", i, hwnd);
1983         ok(child[order[i]] == hwnd, "Z order of child #%d is wrong\n", i);
1984
1985         hwnd = GetWindow(hwnd, GW_HWNDNEXT);
1986     }
1987
1988     for (i = 0; i < total; i++)
1989         ok(DestroyWindow(child[i]), "DestroyWindow failed\n");
1990 }
1991
1992 static void test_children_zorder(HWND parent)
1993 {
1994     const DWORD simple_style[5] = { WS_CHILD, WS_CHILD, WS_CHILD, WS_CHILD,
1995                                     WS_CHILD };
1996     const int simple_order[5] = { 0, 1, 2, 3, 4 };
1997
1998     const DWORD complex_style[5] = { WS_CHILD, WS_CHILD | WS_MAXIMIZE,
1999                              WS_CHILD | WS_VISIBLE, WS_CHILD,
2000                              WS_CHILD | WS_MAXIMIZE | WS_VISIBLE };
2001     const int complex_order_1[1] = { 0 };
2002     const int complex_order_2[2] = { 1, 0 };
2003     const int complex_order_3[3] = { 1, 0, 2 };
2004     const int complex_order_4[4] = { 1, 0, 2, 3 };
2005     const int complex_order_5[5] = { 4, 1, 0, 2, 3 };
2006     const DWORD complex_style_6[3] = { WS_CHILD | WS_VISIBLE,
2007                                        WS_CHILD | WS_CLIPSIBLINGS | DS_CONTROL | WS_VISIBLE,
2008                                        WS_CHILD | WS_VISIBLE };
2009     const int complex_order_6[3] = { 0, 1, 2 };
2010
2011     /* simple WS_CHILD */
2012     test_window_tree(parent, simple_style, simple_order, 5);
2013
2014     /* complex children styles */
2015     test_window_tree(parent, complex_style, complex_order_1, 1);
2016     test_window_tree(parent, complex_style, complex_order_2, 2);
2017     test_window_tree(parent, complex_style, complex_order_3, 3);
2018     test_window_tree(parent, complex_style, complex_order_4, 4);
2019     test_window_tree(parent, complex_style, complex_order_5, 5);
2020
2021     /* another set of complex children styles */
2022     test_window_tree(parent, complex_style_6, complex_order_6, 3);
2023 }
2024
2025 static void test_vis_rgn( HWND hwnd )
2026 {
2027     RECT win_rect, rgn_rect;
2028     HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
2029     HDC hdc;
2030
2031     ShowWindow(hwnd,SW_SHOW);
2032     hdc = GetDC( hwnd );
2033     ok( GetRandomRgn( hdc, hrgn, SYSRGN ) != 0, "GetRandomRgn failed\n" );
2034     GetWindowRect( hwnd, &win_rect );
2035     GetRgnBox( hrgn, &rgn_rect );
2036     if (GetVersion() & 0x80000000)
2037     {
2038         trace("win9x, mapping to screen coords\n");
2039         MapWindowPoints( hwnd, 0, (POINT *)&rgn_rect, 2 );
2040     }
2041     trace("win: %d,%d-%d,%d\n", win_rect.left, win_rect.top, win_rect.right, win_rect.bottom );
2042     trace("rgn: %d,%d-%d,%d\n", rgn_rect.left, rgn_rect.top, rgn_rect.right, rgn_rect.bottom );
2043     ok( win_rect.left <= rgn_rect.left, "rgn left %d not inside win rect %d\n",
2044         rgn_rect.left, win_rect.left );
2045     ok( win_rect.top <= rgn_rect.top, "rgn top %d not inside win rect %d\n",
2046         rgn_rect.top, win_rect.top );
2047     ok( win_rect.right >= rgn_rect.right, "rgn right %d not inside win rect %d\n",
2048         rgn_rect.right, win_rect.right );
2049     ok( win_rect.bottom >= rgn_rect.bottom, "rgn bottom %d not inside win rect %d\n",
2050         rgn_rect.bottom, win_rect.bottom );
2051     ReleaseDC( hwnd, hdc );
2052 }
2053
2054 static void test_SetFocus(HWND hwnd)
2055 {
2056     HWND child;
2057
2058     /* check if we can set focus to non-visible windows */
2059
2060     ShowWindow(hwnd, SW_SHOW);
2061     SetFocus(0);
2062     SetFocus(hwnd);
2063     ok( GetFocus() == hwnd, "Failed to set focus to visible window %p\n", hwnd );
2064     ok( GetWindowLong(hwnd,GWL_STYLE) & WS_VISIBLE, "Window %p not visible\n", hwnd );
2065     ShowWindow(hwnd, SW_HIDE);
2066     SetFocus(0);
2067     SetFocus(hwnd);
2068     ok( GetFocus() == hwnd, "Failed to set focus to invisible window %p\n", hwnd );
2069     ok( !(GetWindowLong(hwnd,GWL_STYLE) & WS_VISIBLE), "Window %p still visible\n", hwnd );
2070     child = CreateWindowExA(0, "static", NULL, WS_CHILD, 0, 0, 0, 0, hwnd, 0, 0, NULL);
2071     assert(child);
2072     SetFocus(child);
2073     ok( GetFocus() == child, "Failed to set focus to invisible child %p\n", child );
2074     ok( !(GetWindowLong(child,GWL_STYLE) & WS_VISIBLE), "Child %p is visible\n", child );
2075     ShowWindow(child, SW_SHOW);
2076     ok( GetWindowLong(child,GWL_STYLE) & WS_VISIBLE, "Child %p is not visible\n", child );
2077     ok( GetFocus() == child, "Focus no longer on child %p\n", child );
2078     ShowWindow(child, SW_HIDE);
2079     ok( !(GetWindowLong(child,GWL_STYLE) & WS_VISIBLE), "Child %p is visible\n", child );
2080     ok( GetFocus() == hwnd, "Focus should be on parent %p, not %p\n", hwnd, GetFocus() );
2081     ShowWindow(child, SW_SHOW);
2082     SetFocus(child);
2083     ok( GetFocus() == child, "Focus should be on child %p\n", child );
2084     SetWindowPos(child,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_HIDEWINDOW);
2085     ok( GetFocus() == child, "Focus should still be on child %p\n", child );
2086
2087     ShowWindow(child, SW_HIDE);
2088     SetFocus(hwnd);
2089     ok( GetFocus() == hwnd, "Focus should be on parent %p, not %p\n", hwnd, GetFocus() );
2090     SetWindowPos(child,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_SHOWWINDOW);
2091     ok( GetFocus() == hwnd, "Focus should still be on parent %p, not %p\n", hwnd, GetFocus() );
2092     ShowWindow(child, SW_HIDE);
2093     ok( GetFocus() == hwnd, "Focus should still be on parent %p, not %p\n", hwnd, GetFocus() );
2094
2095     ShowWindow(hwnd, SW_SHOW);
2096     ShowWindow(child, SW_SHOW);
2097     SetFocus(child);
2098     ok( GetFocus() == child, "Focus should be on child %p\n", child );
2099     EnableWindow(hwnd, FALSE);
2100     ok( GetFocus() == child, "Focus should still be on child %p\n", child );
2101     EnableWindow(hwnd, TRUE);
2102
2103     DestroyWindow( child );
2104 }
2105
2106 static void check_wnd_state(HWND active, HWND foreground, HWND focus, HWND capture)
2107 {
2108     ok(active == GetActiveWindow(), "GetActiveWindow() = %p\n", GetActiveWindow());
2109     if (foreground)
2110         ok(foreground == GetForegroundWindow(), "GetForegroundWindow() = %p\n", GetForegroundWindow());
2111     ok(focus == GetFocus(), "GetFocus() = %p\n", GetFocus());
2112     ok(capture == GetCapture(), "GetCapture() = %p\n", GetCapture());
2113 }
2114
2115 static void test_SetActiveWindow(HWND hwnd)
2116 {
2117     HWND hwnd2;
2118
2119     ShowWindow(hwnd, SW_HIDE);
2120     SetFocus(0);
2121     SetActiveWindow(0);
2122     check_wnd_state(0, 0, 0, 0);
2123
2124     /*trace("testing SetActiveWindow %p\n", hwnd);*/
2125
2126     ShowWindow(hwnd, SW_SHOW);
2127     check_wnd_state(hwnd, hwnd, hwnd, 0);
2128
2129     hwnd2 = SetActiveWindow(0);
2130     ok(hwnd2 == hwnd, "SetActiveWindow returned %p instead of %p\n", hwnd2, hwnd);
2131     check_wnd_state(0, 0, 0, 0);
2132
2133     hwnd2 = SetActiveWindow(hwnd);
2134     ok(hwnd2 == 0, "SetActiveWindow returned %p instead of 0\n", hwnd2);
2135     check_wnd_state(hwnd, hwnd, hwnd, 0);
2136
2137     SetWindowPos(hwnd,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_HIDEWINDOW);
2138     check_wnd_state(hwnd, hwnd, hwnd, 0);
2139
2140     SetWindowPos(hwnd,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_SHOWWINDOW);
2141     check_wnd_state(hwnd, hwnd, hwnd, 0);
2142
2143     ShowWindow(hwnd, SW_HIDE);
2144     check_wnd_state(0, 0, 0, 0);
2145
2146     /*trace("testing SetActiveWindow on an invisible window %p\n", hwnd);*/
2147     SetActiveWindow(hwnd);
2148     check_wnd_state(hwnd, hwnd, hwnd, 0);
2149     
2150     ShowWindow(hwnd, SW_SHOW);
2151     check_wnd_state(hwnd, hwnd, hwnd, 0);
2152
2153     hwnd2 = CreateWindowExA(0, "static", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 0, 0, hwnd, 0, 0, NULL);
2154     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2155
2156     DestroyWindow(hwnd2);
2157     check_wnd_state(hwnd, hwnd, hwnd, 0);
2158
2159     hwnd2 = CreateWindowExA(0, "static", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 0, 0, hwnd, 0, 0, NULL);
2160     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2161
2162     SetWindowPos(hwnd2,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_HIDEWINDOW);
2163     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2164
2165     DestroyWindow(hwnd2);
2166     check_wnd_state(hwnd, hwnd, hwnd, 0);
2167 }
2168
2169 static void test_SetForegroundWindow(HWND hwnd)
2170 {
2171     BOOL ret;
2172     HWND hwnd2;
2173
2174     ShowWindow(hwnd, SW_HIDE);
2175     SetFocus(0);
2176     SetActiveWindow(0);
2177     check_wnd_state(0, 0, 0, 0);
2178
2179     /*trace("testing SetForegroundWindow %p\n", hwnd);*/
2180
2181     ShowWindow(hwnd, SW_SHOW);
2182     check_wnd_state(hwnd, hwnd, hwnd, 0);
2183
2184     hwnd2 = SetActiveWindow(0);
2185     ok(hwnd2 == hwnd, "SetActiveWindow(0) returned %p instead of %p\n", hwnd2, hwnd);
2186     check_wnd_state(0, 0, 0, 0);
2187
2188     ret = SetForegroundWindow(hwnd);
2189     ok(ret, "SetForegroundWindow returned FALSE instead of TRUE\n");
2190     check_wnd_state(hwnd, hwnd, hwnd, 0);
2191
2192     SetLastError(0xdeadbeef);
2193     ret = SetForegroundWindow(0);
2194     ok(!ret, "SetForegroundWindow returned TRUE instead of FALSE\n");
2195     ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE, "got error %d expected ERROR_INVALID_WINDOW_HANDLE\n", GetLastError());
2196     check_wnd_state(hwnd, hwnd, hwnd, 0);
2197
2198     SetWindowPos(hwnd,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_HIDEWINDOW);
2199     check_wnd_state(hwnd, hwnd, hwnd, 0);
2200
2201     SetWindowPos(hwnd,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_SHOWWINDOW);
2202     check_wnd_state(hwnd, hwnd, hwnd, 0);
2203
2204     ShowWindow(hwnd, SW_HIDE);
2205     check_wnd_state(0, 0, 0, 0);
2206
2207     /*trace("testing SetForegroundWindow on an invisible window %p\n", hwnd);*/
2208     ret = SetForegroundWindow(hwnd);
2209     ok(ret, "SetForegroundWindow returned FALSE instead of TRUE\n");
2210     check_wnd_state(hwnd, hwnd, hwnd, 0);
2211     
2212     ShowWindow(hwnd, SW_SHOW);
2213     check_wnd_state(hwnd, hwnd, hwnd, 0);
2214
2215     hwnd2 = CreateWindowExA(0, "static", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 0, 0, hwnd, 0, 0, NULL);
2216     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2217
2218     DestroyWindow(hwnd2);
2219     check_wnd_state(hwnd, hwnd, hwnd, 0);
2220
2221     hwnd2 = CreateWindowExA(0, "static", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 0, 0, hwnd, 0, 0, NULL);
2222     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2223
2224     SetWindowPos(hwnd2,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_HIDEWINDOW);
2225     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2226
2227     DestroyWindow(hwnd2);
2228     check_wnd_state(hwnd, hwnd, hwnd, 0);
2229 }
2230
2231 static WNDPROC old_button_proc;
2232
2233 static LRESULT WINAPI button_hook_proc(HWND button, UINT msg, WPARAM wparam, LPARAM lparam)
2234 {
2235     LRESULT ret;
2236     USHORT key_state;
2237
2238     key_state = GetKeyState(VK_LBUTTON);
2239     ok(!(key_state & 0x8000), "VK_LBUTTON should not be pressed, state %04x\n", key_state);
2240
2241     ret = CallWindowProcA(old_button_proc, button, msg, wparam, lparam);
2242
2243     if (msg == WM_LBUTTONDOWN)
2244     {
2245         HWND hwnd, capture;
2246
2247         check_wnd_state(button, button, button, button);
2248
2249         hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0, 0, 10, 10, 0, 0, 0, NULL);
2250         assert(hwnd);
2251         trace("hwnd %p\n", hwnd);
2252
2253         check_wnd_state(button, button, button, button);
2254
2255         ShowWindow(hwnd, SW_SHOWNOACTIVATE);
2256
2257         check_wnd_state(button, button, button, button);
2258
2259         DestroyWindow(hwnd);
2260
2261         hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0, 0, 10, 10, 0, 0, 0, NULL);
2262         assert(hwnd);
2263         trace("hwnd %p\n", hwnd);
2264
2265         check_wnd_state(button, button, button, button);
2266
2267         /* button wnd proc should release capture on WM_KILLFOCUS if it does
2268          * match internal button state.
2269          */
2270         SendMessage(button, WM_KILLFOCUS, 0, 0);
2271         check_wnd_state(button, button, button, 0);
2272
2273         ShowWindow(hwnd, SW_SHOW);
2274         check_wnd_state(hwnd, hwnd, hwnd, 0);
2275
2276         capture = SetCapture(hwnd);
2277         ok(capture == 0, "SetCapture() = %p\n", capture);
2278
2279         check_wnd_state(hwnd, hwnd, hwnd, hwnd);
2280
2281         DestroyWindow(hwnd);
2282
2283         check_wnd_state(button, 0, button, 0);
2284     }
2285
2286     return ret;
2287 }
2288
2289 static void test_capture_1(void)
2290 {
2291     HWND button, capture;
2292
2293     capture = GetCapture();
2294     ok(capture == 0, "GetCapture() = %p\n", capture);
2295
2296     button = CreateWindowExA(0, "button", NULL, WS_POPUP | WS_VISIBLE, 0, 0, 10, 10, 0, 0, 0, NULL);
2297     assert(button);
2298     trace("button %p\n", button);
2299
2300     old_button_proc = (WNDPROC)SetWindowLongPtrA(button, GWLP_WNDPROC, (LONG_PTR)button_hook_proc);
2301
2302     SendMessageA(button, WM_LBUTTONDOWN, 0, 0);
2303
2304     capture = SetCapture(button);
2305     ok(capture == 0, "SetCapture() = %p\n", capture);
2306     check_wnd_state(button, 0, button, button);
2307
2308     DestroyWindow(button);
2309     check_wnd_state(0, 0, 0, 0);
2310 }
2311
2312 static void test_capture_2(void)
2313 {
2314     HWND button, hwnd, capture;
2315
2316     check_wnd_state(0, 0, 0, 0);
2317
2318     button = CreateWindowExA(0, "button", NULL, WS_POPUP | WS_VISIBLE, 0, 0, 10, 10, 0, 0, 0, NULL);
2319     assert(button);
2320     trace("button %p\n", button);
2321
2322     check_wnd_state(button, button, button, 0);
2323
2324     capture = SetCapture(button);
2325     ok(capture == 0, "SetCapture() = %p\n", capture);
2326
2327     check_wnd_state(button, button, button, button);
2328
2329     /* button wnd proc should ignore WM_KILLFOCUS if it doesn't match
2330      * internal button state.
2331      */
2332     SendMessage(button, WM_KILLFOCUS, 0, 0);
2333     check_wnd_state(button, button, button, button);
2334
2335     hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0, 0, 10, 10, 0, 0, 0, NULL);
2336     assert(hwnd);
2337     trace("hwnd %p\n", hwnd);
2338
2339     check_wnd_state(button, button, button, button);
2340
2341     ShowWindow(hwnd, SW_SHOWNOACTIVATE);
2342
2343     check_wnd_state(button, button, button, button);
2344
2345     DestroyWindow(hwnd);
2346
2347     hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0, 0, 10, 10, 0, 0, 0, NULL);
2348     assert(hwnd);
2349     trace("hwnd %p\n", hwnd);
2350
2351     check_wnd_state(button, button, button, button);
2352
2353     ShowWindow(hwnd, SW_SHOW);
2354
2355     check_wnd_state(hwnd, hwnd, hwnd, button);
2356
2357     capture = SetCapture(hwnd);
2358     ok(capture == button, "SetCapture() = %p\n", capture);
2359
2360     check_wnd_state(hwnd, hwnd, hwnd, hwnd);
2361
2362     DestroyWindow(hwnd);
2363     check_wnd_state(button, button, button, 0);
2364
2365     DestroyWindow(button);
2366     check_wnd_state(0, 0, 0, 0);
2367 }
2368
2369 static void test_capture_3(HWND hwnd1, HWND hwnd2)
2370 {
2371     BOOL ret;
2372
2373     ShowWindow(hwnd1, SW_HIDE);
2374     ShowWindow(hwnd2, SW_HIDE);
2375
2376     ok(!IsWindowVisible(hwnd1), "%p should be invisible\n", hwnd1);
2377     ok(!IsWindowVisible(hwnd2), "%p should be invisible\n", hwnd2);
2378
2379     SetCapture(hwnd1);
2380     check_wnd_state(0, 0, 0, hwnd1);
2381
2382     SetCapture(hwnd2);
2383     check_wnd_state(0, 0, 0, hwnd2);
2384
2385     ShowWindow(hwnd1, SW_SHOW);
2386     check_wnd_state(hwnd1, hwnd1, hwnd1, hwnd2);
2387
2388     ret = ReleaseCapture();
2389     ok (ret, "releasecapture did not return TRUE.\n");
2390     ret = ReleaseCapture();
2391     ok (ret, "releasecapture did not return TRUE after second try.\n");
2392 }
2393
2394 static void test_keyboard_input(HWND hwnd)
2395 {
2396     MSG msg;
2397     BOOL ret;
2398
2399     ShowWindow(hwnd, SW_SHOW);
2400     UpdateWindow(hwnd);
2401     flush_events();
2402
2403     ok(GetActiveWindow() == hwnd, "wrong active window %p\n", GetActiveWindow());
2404
2405     SetFocus(hwnd);
2406     ok(GetFocus() == hwnd, "wrong focus window %p\n", GetFocus());
2407
2408     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
2409
2410     PostMessageA(hwnd, WM_KEYDOWN, 0, 0);
2411     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2412     ok(msg.hwnd == hwnd && msg.message == WM_KEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2413     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
2414     ok( !ret, "message %04x available\n", msg.message);
2415
2416     ok(GetFocus() == hwnd, "wrong focus window %p\n", GetFocus());
2417
2418     PostThreadMessageA(GetCurrentThreadId(), WM_KEYDOWN, 0, 0);
2419     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2420     ok(!msg.hwnd && msg.message == WM_KEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2421     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
2422     ok( !ret, "message %04x available\n", msg.message);
2423
2424     ok(GetFocus() == hwnd, "wrong focus window %p\n", GetFocus());
2425
2426     keybd_event(VK_SPACE, 0, 0, 0);
2427     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2428     ok(msg.hwnd == hwnd && msg.message == WM_KEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2429     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
2430     ok( !ret, "message %04x available\n", msg.message);
2431
2432     SetFocus(0);
2433     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
2434
2435     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
2436
2437     PostMessageA(hwnd, WM_KEYDOWN, 0, 0);
2438     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2439     ok(msg.hwnd == hwnd && msg.message == WM_KEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2440     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
2441     ok( !ret, "message %04x available\n", msg.message);
2442
2443     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
2444
2445     PostThreadMessageA(GetCurrentThreadId(), WM_KEYDOWN, 0, 0);
2446     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2447     ok(!msg.hwnd && msg.message == WM_KEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2448     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
2449     ok( !ret, "message %04x available\n", msg.message);
2450
2451     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
2452
2453     keybd_event(VK_SPACE, 0, 0, 0);
2454     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2455     ok(msg.hwnd == hwnd && msg.message == WM_SYSKEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2456     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
2457     ok( !ret, "message %04x available\n", msg.message);
2458 }
2459
2460 static void test_mouse_input(HWND hwnd)
2461 {
2462     RECT rc;
2463     POINT pt;
2464     int x, y;
2465     HWND popup;
2466     MSG msg;
2467     BOOL ret;
2468     LRESULT res;
2469
2470     ShowWindow(hwnd, SW_SHOW);
2471     UpdateWindow(hwnd);
2472
2473     GetWindowRect(hwnd, &rc);
2474     trace("main window %p: (%d,%d)-(%d,%d)\n", hwnd, rc.left, rc.top, rc.right, rc.bottom);
2475
2476     popup = CreateWindowExA(0, "MainWindowClass", NULL, WS_POPUP,
2477                             rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top,
2478                             hwnd, 0, 0, NULL);
2479     assert(popup != 0);
2480     ShowWindow(popup, SW_SHOW);
2481     UpdateWindow(popup);
2482
2483     GetWindowRect(popup, &rc);
2484     trace("popup window %p: (%d,%d)-(%d,%d)\n", popup, rc.left, rc.top, rc.right, rc.bottom);
2485
2486     x = rc.left + (rc.right - rc.left) / 2;
2487     y = rc.top + (rc.bottom - rc.top) / 2;
2488     trace("setting cursor to (%d,%d)\n", x, y);
2489
2490     SetCursorPos(x, y);
2491     GetCursorPos(&pt);
2492     ok(x == pt.x && y == pt.y, "wrong cursor pos (%d,%d), expected (%d,%d)\n", pt.x, pt.y, x, y);
2493
2494     /* force the system to update its internal queue mouse position,
2495      * otherwise it won't generate relative mouse movements below.
2496      */
2497     mouse_event(MOUSEEVENTF_MOVE, -1, -1, 0, 0);
2498     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
2499
2500     msg.message = 0;
2501     mouse_event(MOUSEEVENTF_MOVE, 1, 1, 0, 0);
2502     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2503     ok(msg.hwnd == popup && msg.message == WM_MOUSEMOVE, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2504     /* FIXME: SetCursorPos in Wine generates additional WM_MOUSEMOVE message */
2505     if (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE))
2506         ok(msg.hwnd == popup && msg.message == WM_MOUSEMOVE, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2507     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
2508     ok( !ret, "message %04x available\n", msg.message);
2509
2510     mouse_event(MOUSEEVENTF_MOVE, -1, -1, 0, 0);
2511     ShowWindow(popup, SW_HIDE);
2512     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2513     ok(msg.hwnd == hwnd && msg.message == WM_MOUSEMOVE, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2514     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
2515
2516     mouse_event(MOUSEEVENTF_MOVE, 1, 1, 0, 0);
2517     ShowWindow(hwnd, SW_HIDE);
2518     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
2519     ok( !ret, "message %04x available\n", msg.message);
2520
2521     /* test mouse clicks */
2522
2523     ShowWindow(hwnd, SW_SHOW);
2524     ShowWindow(popup, SW_SHOW);
2525
2526     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
2527     flush_events();
2528
2529     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
2530     mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
2531     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
2532     mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
2533
2534     ok(PeekMessageA(&msg, 0, 0, 0, 0), "no message available\n");
2535     ok(msg.hwnd == popup && msg.message == WM_LBUTTONDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2536     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2537     ok(msg.hwnd == popup && msg.message == WM_LBUTTONDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2538
2539     ok(PeekMessageA(&msg, 0, 0, 0, 0), "no message available\n");
2540     ok(msg.hwnd == popup && msg.message == WM_LBUTTONUP, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2541     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2542     ok(msg.hwnd == popup && msg.message == WM_LBUTTONUP, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2543
2544     ok(PeekMessageA(&msg, 0, 0, 0, 0), "no message available\n");
2545     ok(msg.hwnd == popup && msg.message == WM_LBUTTONDBLCLK, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2546     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2547     ok(msg.hwnd == popup && msg.message == WM_LBUTTONDBLCLK, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2548
2549     ok(PeekMessageA(&msg, 0, 0, 0, 0), "no message available\n");
2550     ok(msg.hwnd == popup && msg.message == WM_LBUTTONUP, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2551     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2552     ok(msg.hwnd == popup && msg.message == WM_LBUTTONUP, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2553
2554     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
2555     ok(!ret, "message %04x available\n", msg.message);
2556
2557     ShowWindow(popup, SW_HIDE);
2558     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
2559
2560     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
2561     mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
2562     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
2563     mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
2564
2565     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2566     ok(msg.hwnd == hwnd && msg.message == WM_LBUTTONDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2567     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2568     ok(msg.hwnd == hwnd && msg.message == WM_LBUTTONUP, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2569
2570     test_lbuttondown_flag = TRUE;
2571     SendMessageA(hwnd, WM_COMMAND, (WPARAM)popup, 0);
2572     test_lbuttondown_flag = FALSE;
2573
2574     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2575     ok(msg.hwnd == popup && msg.message == WM_LBUTTONDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2576     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2577     ok(msg.hwnd == popup && msg.message == WM_LBUTTONUP, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2578     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2579
2580     /* Test WM_MOUSEACTIVATE */
2581 #define TEST_MOUSEACTIVATE(A,B)                                                          \
2582        res = SendMessageA(hwnd, WM_MOUSEACTIVATE, (WPARAM)hwnd, (LPARAM)MAKELRESULT(A,0));   \
2583        ok(res == B, "WM_MOUSEACTIVATE for %s returned %ld\n", #A, res);
2584        
2585     TEST_MOUSEACTIVATE(HTERROR,MA_ACTIVATE);
2586     TEST_MOUSEACTIVATE(HTTRANSPARENT,MA_ACTIVATE);
2587     TEST_MOUSEACTIVATE(HTNOWHERE,MA_ACTIVATE);
2588     TEST_MOUSEACTIVATE(HTCLIENT,MA_ACTIVATE);
2589     TEST_MOUSEACTIVATE(HTCAPTION,MA_ACTIVATE);
2590     TEST_MOUSEACTIVATE(HTSYSMENU,MA_ACTIVATE);
2591     TEST_MOUSEACTIVATE(HTSIZE,MA_ACTIVATE);
2592     TEST_MOUSEACTIVATE(HTMENU,MA_ACTIVATE);
2593     TEST_MOUSEACTIVATE(HTHSCROLL,MA_ACTIVATE);
2594     TEST_MOUSEACTIVATE(HTVSCROLL,MA_ACTIVATE);
2595     TEST_MOUSEACTIVATE(HTMINBUTTON,MA_ACTIVATE);
2596     TEST_MOUSEACTIVATE(HTMAXBUTTON,MA_ACTIVATE);
2597     TEST_MOUSEACTIVATE(HTLEFT,MA_ACTIVATE);
2598     TEST_MOUSEACTIVATE(HTRIGHT,MA_ACTIVATE);
2599     TEST_MOUSEACTIVATE(HTTOP,MA_ACTIVATE);
2600     TEST_MOUSEACTIVATE(HTTOPLEFT,MA_ACTIVATE);
2601     TEST_MOUSEACTIVATE(HTTOPRIGHT,MA_ACTIVATE);
2602     TEST_MOUSEACTIVATE(HTBOTTOM,MA_ACTIVATE);
2603     TEST_MOUSEACTIVATE(HTBOTTOMLEFT,MA_ACTIVATE);
2604     TEST_MOUSEACTIVATE(HTBOTTOMRIGHT,MA_ACTIVATE);
2605     TEST_MOUSEACTIVATE(HTBORDER,MA_ACTIVATE);
2606     TEST_MOUSEACTIVATE(HTOBJECT,MA_ACTIVATE);
2607     TEST_MOUSEACTIVATE(HTCLOSE,MA_ACTIVATE);
2608     TEST_MOUSEACTIVATE(HTHELP,MA_ACTIVATE);
2609
2610     /* Clear any messages left behind by WM_MOUSEACTIVATE tests */
2611     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
2612
2613     DestroyWindow(popup);
2614 }
2615
2616 static void test_validatergn(HWND hwnd)
2617 {
2618     HWND child;
2619     RECT rc, rc2;
2620     HRGN rgn;
2621     int ret;
2622     child = CreateWindowExA(0, "static", NULL, WS_CHILD| WS_VISIBLE, 10, 10, 10, 10, hwnd, 0, 0, NULL);
2623     ShowWindow(hwnd, SW_SHOW);
2624     UpdateWindow( hwnd);
2625     /* test that ValidateRect validates children*/
2626     InvalidateRect( child, NULL, 1);
2627     GetWindowRect( child, &rc);
2628     MapWindowPoints( NULL, hwnd, (POINT*) &rc, 2);
2629     ret = GetUpdateRect( child, &rc2, 0);
2630     ok( rc2.right > rc2.left && rc2.bottom > rc2.top,
2631             "Update rectangle is empty!\n");
2632     ValidateRect( hwnd, &rc);
2633     ret = GetUpdateRect( child, &rc2, 0);
2634     ok( rc2.left == 0 && rc2.top == 0 && rc2.right == 0 && rc2.bottom == 0,
2635             "Update rectangle %d,%d-%d,%d is not empty!\n", rc2.left, rc2.top,
2636             rc2.right, rc2.bottom);
2637
2638     /* now test ValidateRgn */
2639     InvalidateRect( child, NULL, 1);
2640     GetWindowRect( child, &rc);
2641     MapWindowPoints( NULL, hwnd, (POINT*) &rc, 2);
2642     rgn = CreateRectRgnIndirect( &rc);
2643     ValidateRgn( hwnd, rgn);
2644     ret = GetUpdateRect( child, &rc2, 0);
2645     ok( rc2.left == 0 && rc2.top == 0 && rc2.right == 0 && rc2.bottom == 0,
2646             "Update rectangle %d,%d-%d,%d is not empty!\n", rc2.left, rc2.top,
2647             rc2.right, rc2.bottom);
2648
2649     DeleteObject( rgn);
2650     DestroyWindow( child );
2651 }
2652
2653 static void nccalchelper(HWND hwnd, INT x, INT y, RECT *prc)
2654 {
2655     MoveWindow( hwnd, 0, 0, x, y, 0);
2656     GetWindowRect( hwnd, prc);
2657     trace("window rect is %d,%d - %d,%d\n",
2658             prc->left,prc->top,prc->right,prc->bottom);
2659     DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)prc);
2660     trace("nccalc rect is %d,%d - %d,%d\n",
2661             prc->left,prc->top,prc->right,prc->bottom);
2662 }
2663
2664 static void test_nccalcscroll(HWND parent)
2665 {
2666     RECT rc1;
2667     INT sbheight = GetSystemMetrics( SM_CYHSCROLL);
2668     INT sbwidth = GetSystemMetrics( SM_CXVSCROLL);
2669     HWND hwnd = CreateWindowExA(0, "static", NULL, 
2670             WS_CHILD| WS_VISIBLE | WS_VSCROLL | WS_HSCROLL , 
2671             10, 10, 200, 200, parent, 0, 0, NULL); 
2672     ShowWindow( parent, SW_SHOW);
2673     UpdateWindow( parent);
2674
2675     /* test window too low for a horizontal scroll bar */
2676     nccalchelper( hwnd, 100, sbheight, &rc1);
2677     ok( rc1.bottom - rc1.top == sbheight, "Height should be %d size is %d,%d - %d,%d\n",
2678             sbheight, rc1.left, rc1.top, rc1.right, rc1.bottom);
2679
2680     /* test window just high enough for a horizontal scroll bar */
2681     nccalchelper( hwnd, 100, sbheight + 1, &rc1);
2682     ok( rc1.bottom - rc1.top == 1, "Height should be %d size is %d,%d - %d,%d\n",
2683             1, rc1.left, rc1.top, rc1.right, rc1.bottom);
2684
2685     /* test window too narrow for a vertical scroll bar */
2686     nccalchelper( hwnd, sbwidth - 1, 100, &rc1);
2687     ok( rc1.right - rc1.left == sbwidth - 1 , "Width should be %d size is %d,%d - %d,%d\n",
2688             sbwidth - 1, rc1.left, rc1.top, rc1.right, rc1.bottom);
2689
2690     /* test window just wide enough for a vertical scroll bar */
2691     nccalchelper( hwnd, sbwidth, 100, &rc1);
2692     ok( rc1.right - rc1.left == 0, "Width should be %d size is %d,%d - %d,%d\n",
2693             0, rc1.left, rc1.top, rc1.right, rc1.bottom);
2694
2695     /* same test, but with client edge: not enough width */
2696     SetWindowLong( hwnd, GWL_EXSTYLE, WS_EX_CLIENTEDGE | GetWindowLong( hwnd, GWL_EXSTYLE));
2697     nccalchelper( hwnd, sbwidth, 100, &rc1);
2698     ok( rc1.right - rc1.left == sbwidth - 2 * GetSystemMetrics(SM_CXEDGE),
2699             "Width should be %d size is %d,%d - %d,%d\n",
2700             sbwidth - 2 * GetSystemMetrics(SM_CXEDGE), rc1.left, rc1.top, rc1.right, rc1.bottom);
2701
2702     DestroyWindow( hwnd);
2703 }
2704
2705 static void test_SetParent(void)
2706 {
2707     BOOL ret;
2708     HWND desktop = GetDesktopWindow();
2709     HMENU hMenu;
2710     BOOL is_win9x = GetWindowLongPtrW(desktop, GWLP_WNDPROC) == 0;
2711     HWND parent, child1, child2, child3, child4, sibling;
2712
2713     parent = CreateWindowExA(0, "static", NULL, WS_OVERLAPPEDWINDOW,
2714                              100, 100, 200, 200, 0, 0, 0, NULL);
2715     assert(parent != 0);
2716     child1 = CreateWindowExA(0, "static", NULL, WS_CHILD,
2717                              0, 0, 50, 50, parent, 0, 0, NULL);
2718     assert(child1 != 0);
2719     child2 = CreateWindowExA(0, "static", NULL, WS_POPUP,
2720                              0, 0, 50, 50, child1, 0, 0, NULL);
2721     assert(child2 != 0);
2722     child3 = CreateWindowExA(0, "static", NULL, WS_CHILD,
2723                              0, 0, 50, 50, child2, 0, 0, NULL);
2724     assert(child3 != 0);
2725     child4 = CreateWindowExA(0, "static", NULL, WS_POPUP,
2726                              0, 0, 50, 50, child3, 0, 0, NULL);
2727     assert(child4 != 0);
2728
2729     trace("parent %p, child1 %p, child2 %p, child3 %p, child4 %p\n",
2730            parent, child1, child2, child3, child4);
2731
2732     check_parents(parent, desktop, 0, 0, 0, parent, parent);
2733     check_parents(child1, parent, parent, parent, 0, parent, parent);
2734     check_parents(child2, desktop, parent, parent, parent, child2, parent);
2735     check_parents(child3, child2, child2, child2, 0, child2, parent);
2736     check_parents(child4, desktop, child2, child2, child2, child4, parent);
2737
2738 todo_wine {
2739     ok(!IsChild(desktop, parent), "wrong parent/child %p/%p\n", desktop, parent);
2740     ok(!IsChild(desktop, child1), "wrong parent/child %p/%p\n", desktop, child1);
2741     ok(!IsChild(desktop, child2), "wrong parent/child %p/%p\n", desktop, child2);
2742     ok(!IsChild(desktop, child3), "wrong parent/child %p/%p\n", desktop, child3);
2743     ok(!IsChild(desktop, child4), "wrong parent/child %p/%p\n", desktop, child4);
2744 }
2745
2746     ok(IsChild(parent, child1), "wrong parent/child %p/%p\n", parent, child1);
2747 todo_wine {
2748     ok(!IsChild(desktop, child2), "wrong parent/child %p/%p\n", desktop, child2);
2749 }
2750     ok(!IsChild(parent, child2), "wrong parent/child %p/%p\n", parent, child2);
2751     ok(!IsChild(child1, child2), "wrong parent/child %p/%p\n", child1, child2);
2752     ok(!IsChild(parent, child3), "wrong parent/child %p/%p\n", parent, child3);
2753     ok(IsChild(child2, child3), "wrong parent/child %p/%p\n", child2, child3);
2754     ok(!IsChild(parent, child4), "wrong parent/child %p/%p\n", parent, child4);
2755     ok(!IsChild(child3, child4), "wrong parent/child %p/%p\n", child3, child4);
2756 todo_wine {
2757     ok(!IsChild(desktop, child4), "wrong parent/child %p/%p\n", desktop, child4);
2758 }
2759
2760     if (!is_win9x) /* Win9x doesn't survive this test */
2761     {
2762         ok(!SetParent(parent, child1), "SetParent should fail\n");
2763         ok(!SetParent(child2, child3), "SetParent should fail\n");
2764         ok(SetParent(child1, parent) != 0, "SetParent should not fail\n");
2765         ok(SetParent(parent, child2) != 0, "SetParent should not fail\n");
2766         ok(SetParent(parent, child3) != 0, "SetParent should not fail\n");
2767         ok(!SetParent(child2, parent), "SetParent should fail\n");
2768         ok(SetParent(parent, child4) != 0, "SetParent should not fail\n");
2769
2770         check_parents(parent, child4, child4, 0, 0, child4, parent);
2771         check_parents(child1, parent, parent, parent, 0, child4, parent);
2772         check_parents(child2, desktop, parent, parent, parent, child2, parent);
2773         check_parents(child3, child2, child2, child2, 0, child2, parent);
2774         check_parents(child4, desktop, child2, child2, child2, child4, parent);
2775     }
2776
2777     hMenu = CreateMenu();
2778     sibling = CreateWindowExA(0, "static", NULL, WS_OVERLAPPEDWINDOW,
2779                              100, 100, 200, 200, 0, hMenu, 0, NULL);
2780     assert(sibling != 0);
2781
2782     ok(SetParent(sibling, parent) != 0, "SetParent should not fail\n");
2783     ok(GetMenu(sibling) == hMenu, "SetParent should not remove menu\n");
2784
2785     ret = DestroyWindow(parent);
2786     ok( ret, "DestroyWindow() error %d\n", GetLastError());
2787
2788     ok(!IsWindow(parent), "parent still exists\n");
2789     ok(!IsWindow(sibling), "sibling still exists\n");
2790     ok(!IsWindow(child1), "child1 still exists\n");
2791     ok(!IsWindow(child2), "child2 still exists\n");
2792     ok(!IsWindow(child3), "child3 still exists\n");
2793     ok(!IsWindow(child4), "child4 still exists\n");
2794 }
2795
2796 static LRESULT WINAPI StyleCheckProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
2797 {
2798     LPCREATESTRUCT lpcs;
2799     LPSTYLESTRUCT lpss;
2800
2801     switch (msg)
2802     {
2803     case WM_NCCREATE:
2804     case WM_CREATE:
2805         lpcs = (LPCREATESTRUCT)lparam;
2806         lpss = (LPSTYLESTRUCT)lpcs->lpCreateParams;
2807         if (lpss)
2808         {
2809             if ((lpcs->dwExStyle & WS_EX_DLGMODALFRAME) ||
2810                 ((!(lpcs->dwExStyle & WS_EX_STATICEDGE)) &&
2811                     (lpcs->style & (WS_DLGFRAME | WS_THICKFRAME))))
2812                 ok(lpcs->dwExStyle & WS_EX_WINDOWEDGE, "Window should have WS_EX_WINDOWEDGE style\n");
2813             else
2814                 ok(!(lpcs->dwExStyle & WS_EX_WINDOWEDGE), "Window shouldn't have WS_EX_WINDOWEDGE style\n");
2815
2816             ok((lpss->styleOld & ~WS_EX_WINDOWEDGE) == (lpcs->dwExStyle & ~WS_EX_WINDOWEDGE),
2817                 "Ex style (0x%08lx) should match what the caller passed to CreateWindowEx (0x%08lx)\n",
2818                 (lpss->styleOld & ~WS_EX_WINDOWEDGE), (lpcs->dwExStyle & ~WS_EX_WINDOWEDGE));
2819
2820             ok(lpss->styleNew == lpcs->style,
2821                 "Style (0x%08x) should match what the caller passed to CreateWindowEx (0x%08x)\n",
2822                 lpss->styleNew, lpcs->style);
2823         }
2824         break;
2825     }
2826     return DefWindowProc(hwnd, msg, wparam, lparam);
2827 }
2828
2829 static ATOM atomStyleCheckClass;
2830
2831 static void register_style_check_class(void)
2832 {
2833     WNDCLASS wc =
2834     {
2835         0,
2836         StyleCheckProc,
2837         0,
2838         0,
2839         GetModuleHandle(NULL),
2840         NULL,
2841         LoadCursor(NULL, IDC_ARROW),
2842         (HBRUSH)(COLOR_BTNFACE+1),
2843         NULL,
2844         TEXT("WineStyleCheck"),
2845     };
2846     
2847     atomStyleCheckClass = RegisterClass(&wc);
2848 }
2849
2850 static void check_window_style(DWORD dwStyleIn, DWORD dwExStyleIn, DWORD dwStyleOut, DWORD dwExStyleOut)
2851 {
2852     DWORD dwActualStyle;
2853     DWORD dwActualExStyle;
2854     STYLESTRUCT ss;
2855     HWND hwnd;
2856     HWND hwndParent = NULL;
2857     MSG msg;
2858
2859     ss.styleNew = dwStyleIn;
2860     ss.styleOld = dwExStyleIn;
2861
2862     if (dwStyleIn & WS_CHILD)
2863     {
2864         hwndParent = CreateWindowEx(0, MAKEINTATOM(atomStyleCheckClass), NULL,
2865             WS_OVERLAPPEDWINDOW, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
2866     }
2867
2868     hwnd = CreateWindowEx(dwExStyleIn, MAKEINTATOM(atomStyleCheckClass), NULL,
2869                     dwStyleIn, 0, 0, 0, 0, hwndParent, NULL, NULL, &ss);
2870     assert(hwnd);
2871
2872     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
2873     {
2874         TranslateMessage(&msg);
2875         DispatchMessage(&msg);
2876     }
2877
2878     dwActualStyle = GetWindowLong(hwnd, GWL_STYLE);
2879     dwActualExStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
2880     ok((dwActualStyle == dwStyleOut) && (dwActualExStyle == dwExStyleOut),
2881         "Style (0x%08x) should really be 0x%08x and/or Ex style (0x%08x) should really be 0x%08x\n",
2882         dwActualStyle, dwStyleOut, dwActualExStyle, dwExStyleOut);
2883
2884     DestroyWindow(hwnd);
2885     if (hwndParent) DestroyWindow(hwndParent);
2886 }
2887
2888 /* tests what window styles the window manager automatically adds */
2889 static void test_window_styles(void)
2890 {
2891     register_style_check_class();
2892
2893     check_window_style(0, 0, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_WINDOWEDGE);
2894     check_window_style(WS_OVERLAPPEDWINDOW, 0, WS_CLIPSIBLINGS|WS_OVERLAPPEDWINDOW, WS_EX_WINDOWEDGE);
2895     check_window_style(WS_CHILD, 0, WS_CHILD, 0);
2896     check_window_style(WS_CHILD, WS_EX_WINDOWEDGE, WS_CHILD, 0);
2897     check_window_style(0, WS_EX_TOOLWINDOW, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_WINDOWEDGE|WS_EX_TOOLWINDOW);
2898     check_window_style(WS_POPUP, 0, WS_POPUP|WS_CLIPSIBLINGS, 0);
2899     check_window_style(WS_POPUP, WS_EX_WINDOWEDGE, WS_POPUP|WS_CLIPSIBLINGS, 0);
2900     check_window_style(WS_CHILD, WS_EX_DLGMODALFRAME, WS_CHILD, WS_EX_WINDOWEDGE|WS_EX_DLGMODALFRAME);
2901     check_window_style(WS_CHILD, WS_EX_DLGMODALFRAME|WS_EX_STATICEDGE, WS_CHILD, WS_EX_STATICEDGE|WS_EX_WINDOWEDGE|WS_EX_DLGMODALFRAME);
2902     check_window_style(WS_CAPTION, WS_EX_STATICEDGE, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_STATICEDGE|WS_EX_WINDOWEDGE);
2903     check_window_style(0, WS_EX_APPWINDOW, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_APPWINDOW|WS_EX_WINDOWEDGE);
2904 }
2905
2906 static void test_scrollvalidate( HWND parent)
2907 {
2908     HDC hdc;
2909     HRGN hrgn=CreateRectRgn(0,0,0,0);
2910     HRGN exprgn, tmprgn, clipping;
2911     RECT rc, rcu, cliprc;
2912     /* create two overlapping child windows. The visual region
2913      * of hwnd1 is clipped by the overlapping part of
2914      * hwnd2 because of the WS_CLIPSIBLING style */
2915     HWND hwnd1, hwnd2;
2916
2917     clipping = CreateRectRgn(0,0,0,0);
2918     tmprgn = CreateRectRgn(0,0,0,0);
2919     exprgn = CreateRectRgn(0,0,0,0);
2920     hwnd2 = CreateWindowExA(0, "static", NULL,
2921             WS_CHILD| WS_VISIBLE | WS_CLIPSIBLINGS | WS_BORDER ,
2922             75, 30, 100, 100, parent, 0, 0, NULL);
2923     hwnd1 = CreateWindowExA(0, "static", NULL,
2924             WS_CHILD| WS_VISIBLE | WS_CLIPSIBLINGS | WS_BORDER ,
2925             25, 50, 100, 100, parent, 0, 0, NULL);
2926     ShowWindow( parent, SW_SHOW);
2927     UpdateWindow( parent);
2928     GetClientRect( hwnd1, &rc);
2929     cliprc=rc; 
2930     SetRectRgn( clipping, 10, 10, 90, 90);
2931     hdc = GetDC( hwnd1);
2932     /* for a visual touch */
2933     TextOut( hdc, 0,10, "0123456789", 10);
2934     ScrollDC( hdc, -10, -5, &rc, &cliprc, hrgn, &rcu);
2935     if (winetest_debug > 0) dump_region(hrgn);
2936     /* create a region with what is expected */
2937     SetRectRgn( exprgn, 39,0,49,74);
2938     SetRectRgn( tmprgn, 88,79,98,93);
2939     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
2940     SetRectRgn( tmprgn, 0,93,98,98);
2941     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
2942     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
2943     trace("update rect is %d,%d - %d,%d\n",
2944             rcu.left,rcu.top,rcu.right,rcu.bottom);
2945     /* now with clipping region */
2946     SelectClipRgn( hdc, clipping);
2947     ScrollDC( hdc, -10, -5, &rc, &cliprc, hrgn, &rcu);
2948     if (winetest_debug > 0) dump_region(hrgn);
2949     /* create a region with what is expected */
2950     SetRectRgn( exprgn, 39,10,49,74);
2951     SetRectRgn( tmprgn, 80,79,90,85);
2952     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
2953     SetRectRgn( tmprgn, 10,85,90,90);
2954     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
2955     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
2956     trace("update rect is %d,%d - %d,%d\n",
2957             rcu.left,rcu.top,rcu.right,rcu.bottom);
2958     ReleaseDC( hwnd1, hdc);
2959
2960     /* test scrolling a window with an update region */
2961     DestroyWindow( hwnd2);
2962     ValidateRect( hwnd1, NULL);
2963     SetRect( &rc, 40,40, 50,50);
2964     InvalidateRect( hwnd1, &rc, 1);
2965     GetClientRect( hwnd1, &rc);
2966     cliprc=rc;
2967     ScrollWindowEx( hwnd1, -10, 0, &rc, &cliprc, hrgn, &rcu,
2968       SW_SCROLLCHILDREN | SW_INVALIDATE);
2969     if (winetest_debug > 0) dump_region(hrgn);
2970     SetRectRgn( exprgn, 88,0,98,98);
2971     SetRectRgn( tmprgn, 30, 40, 50, 50);
2972     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
2973     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
2974
2975     /* now test ScrollWindowEx with a combination of
2976      * WS_CLIPCHILDREN style and SW_SCROLLCHILDREN flag */
2977     /* make hwnd2 the child of hwnd1 */
2978     hwnd2 = CreateWindowExA(0, "static", NULL,
2979             WS_CHILD| WS_VISIBLE | WS_BORDER ,
2980             50, 50, 100, 100, hwnd1, 0, 0, NULL);
2981     SetWindowLong( hwnd1, GWL_STYLE, GetWindowLong( hwnd1, GWL_STYLE) & ~WS_CLIPSIBLINGS);
2982     GetClientRect( hwnd1, &rc);
2983     cliprc=rc;
2984
2985     /* WS_CLIPCHILDREN and SW_SCROLLCHILDREN */
2986     SetWindowLong( hwnd1, GWL_STYLE, GetWindowLong( hwnd1, GWL_STYLE) | WS_CLIPCHILDREN );
2987     ValidateRect( hwnd1, NULL);
2988     ValidateRect( hwnd2, NULL);
2989     ScrollWindowEx( hwnd1, -10, -10, &rc, &cliprc, hrgn, &rcu,
2990       SW_SCROLLCHILDREN | SW_INVALIDATE);
2991     if (winetest_debug > 0) dump_region(hrgn);
2992     SetRectRgn( exprgn, 88,0,98,88);
2993     SetRectRgn( tmprgn, 0,88,98,98);
2994     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
2995     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
2996
2997     /* SW_SCROLLCHILDREN */
2998     SetWindowLong( hwnd1, GWL_STYLE, GetWindowLong( hwnd1, GWL_STYLE) & ~WS_CLIPCHILDREN );
2999     ValidateRect( hwnd1, NULL);
3000     ValidateRect( hwnd2, NULL);
3001     ScrollWindowEx( hwnd1, -10, -10, &rc, &cliprc, hrgn, &rcu, SW_SCROLLCHILDREN | SW_INVALIDATE);
3002     if (winetest_debug > 0) dump_region(hrgn);
3003     /* expected region is the same as in previous test */
3004     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
3005
3006     /* no SW_SCROLLCHILDREN */
3007     SetWindowLong( hwnd1, GWL_STYLE, GetWindowLong( hwnd1, GWL_STYLE) & ~WS_CLIPCHILDREN );
3008     ValidateRect( hwnd1, NULL);
3009     ValidateRect( hwnd2, NULL);
3010     ScrollWindowEx( hwnd1, -10, -10, &rc, &cliprc, hrgn, &rcu, SW_INVALIDATE);
3011     if (winetest_debug > 0) dump_region(hrgn);
3012     /* expected region is the same as in previous test */
3013     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
3014
3015     /* WS_CLIPCHILDREN and no SW_SCROLLCHILDREN */
3016     SetWindowLong( hwnd1, GWL_STYLE, GetWindowLong( hwnd1, GWL_STYLE) | WS_CLIPCHILDREN );
3017     ValidateRect( hwnd1, NULL);
3018     ValidateRect( hwnd2, NULL);
3019     ScrollWindowEx( hwnd1, -10, -10, &rc, &cliprc, hrgn, &rcu, SW_INVALIDATE);
3020     if (winetest_debug > 0) dump_region(hrgn);
3021     SetRectRgn( exprgn, 88,0,98,20);
3022     SetRectRgn( tmprgn, 20,20,98,30);
3023     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
3024     SetRectRgn( tmprgn, 20,30,30,88);
3025     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
3026     SetRectRgn( tmprgn, 0,88,30,98);
3027     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
3028     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
3029
3030     /* clean up */
3031     DeleteObject( hrgn);
3032     DeleteObject( exprgn);
3033     DeleteObject( tmprgn);
3034     DestroyWindow( hwnd1);
3035     DestroyWindow( hwnd2);
3036 }
3037
3038 /* couple of tests of return values of scrollbar functions
3039  * called on a scrollbarless window */ 
3040 static void test_scroll(void)
3041 {
3042     BOOL ret;
3043     INT min, max;
3044     SCROLLINFO si;
3045     HWND hwnd = CreateWindowExA(0, "Static", "Wine test window",
3046         WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_POPUP,
3047         100, 100, 200, 200, 0, 0, 0, NULL);
3048     /* horizontal */
3049     ret = GetScrollRange( hwnd, SB_HORZ, &min, &max);
3050     ok( ret, "GetScrollRange returns FALSE\n");
3051     ok( min == 0, "minimum scroll pos is %d (should be zero)\n", min);
3052     ok( max == 0, "maximum scroll pos is %d (should be zero)\n", min);
3053     si.cbSize = sizeof( si);
3054     si.fMask = SIF_PAGE;
3055     si.nPage = 0xdeadbeef;
3056     ret = GetScrollInfo( hwnd, SB_HORZ, &si);
3057     ok( !ret, "GetScrollInfo returns %d (should be zero)\n", ret);
3058     ok( si.nPage == 0xdeadbeef, "unexpected value for nPage is %d\n", si.nPage);
3059     /* vertical */
3060     ret = GetScrollRange( hwnd, SB_VERT, &min, &max);
3061     ok( ret, "GetScrollRange returns FALSE\n");
3062     ok( min == 0, "minimum scroll pos is %d (should be zero)\n", min);
3063     ok( max == 0, "maximum scroll pos is %d (should be zero)\n", min);
3064     si.cbSize = sizeof( si);
3065     si.fMask = SIF_PAGE;
3066     si.nPage = 0xdeadbeef;
3067     ret = GetScrollInfo( hwnd, SB_VERT, &si);
3068     ok( !ret, "GetScrollInfo returns %d (should be zero)\n", ret);
3069     ok( si.nPage == 0xdeadbeef, "unexpected value for nPage is %d\n", si.nPage);
3070     /* clean up */
3071     DestroyWindow( hwnd);
3072 }
3073
3074 static void test_scrolldc( HWND parent)
3075 {
3076     HDC hdc;
3077     HRGN exprgn, tmprgn, hrgn;
3078     RECT rc, rc2, rcu, cliprc;
3079     HWND hwnd1;
3080     COLORREF colr;
3081
3082     hrgn = CreateRectRgn(0,0,0,0);
3083     tmprgn = CreateRectRgn(0,0,0,0);
3084     exprgn = CreateRectRgn(0,0,0,0);
3085
3086     hwnd1 = CreateWindowExA(0, "static", NULL,
3087             WS_CHILD| WS_VISIBLE,
3088             25, 50, 100, 100, parent, 0, 0, NULL);
3089     ShowWindow( parent, SW_SHOW);
3090     UpdateWindow( parent);
3091     GetClientRect( hwnd1, &rc);
3092     hdc = GetDC( hwnd1);
3093     /* paint the upper half of the window black */
3094     rc2 = rc;
3095     rc2.bottom = ( rc.top + rc.bottom) /2;
3096     FillRect( hdc, &rc2, GetStockObject(BLACK_BRUSH));
3097     /* clip region is the lower half */
3098     cliprc=rc; 
3099     cliprc.top = (rc.top + rc.bottom) /2;
3100     /* test whether scrolled pixels are properly clipped */ 
3101     colr = GetPixel( hdc, (rc.left+rc.right)/2, ( rc.top + rc.bottom) /2 - 1);
3102     ok ( colr == 0, "pixel should be black, color is %08x\n", colr);
3103     /* this scroll should not cause any visible changes */ 
3104     ScrollDC( hdc, 5, -20, &rc, &cliprc, hrgn, &rcu);
3105     colr = GetPixel( hdc, (rc.left+rc.right)/2, ( rc.top + rc.bottom) /2 - 1);
3106     ok ( colr == 0, "pixel should be black, color is %08x\n", colr);
3107     /* test with NULL clip rect */
3108     ScrollDC( hdc, 20, -20, &rc, NULL, hrgn, &rcu);
3109     /*FillRgn(hdc, hrgn, GetStockObject(WHITE_BRUSH));*/
3110     trace("update rect: %d,%d - %d,%d\n",
3111            rcu.left, rcu.top, rcu.right, rcu.bottom);
3112     if (winetest_debug > 0) dump_region(hrgn);
3113     SetRect(&rc2, 0, 0, 100, 100);
3114     ok(EqualRect(&rcu, &rc2), "rects do not match (%d,%d-%d,%d) / (%d,%d-%d,%d)\n",
3115        rcu.left, rcu.top, rcu.right, rcu.bottom, rc2.left, rc2.top, rc2.right, rc2.bottom);
3116
3117     SetRectRgn( exprgn, 0, 0, 20, 80);
3118     SetRectRgn( tmprgn, 0, 80, 100, 100);
3119     CombineRgn(exprgn, exprgn, tmprgn, RGN_OR);
3120     if (winetest_debug > 0) dump_region(exprgn);
3121     ok(EqualRgn(exprgn, hrgn), "wrong update region\n");
3122     /* test clip rect > scroll rect */ 
3123     FillRect( hdc, &rc, GetStockObject(WHITE_BRUSH));
3124     rc2=rc;
3125     InflateRect( &rc2, -(rc.right-rc.left)/4, -(rc.bottom-rc.top)/4);
3126     FillRect( hdc, &rc2, GetStockObject(BLACK_BRUSH));
3127     ScrollDC( hdc, 10, 10, &rc2, &rc, hrgn, &rcu);
3128     SetRectRgn( exprgn, 25, 25, 75, 35);
3129     SetRectRgn( tmprgn, 25, 35, 35, 75);
3130     CombineRgn(exprgn, exprgn, tmprgn, RGN_OR);
3131     ok(EqualRgn(exprgn, hrgn), "wrong update region\n");
3132     colr = GetPixel( hdc, 80, 80);
3133     ok ( colr == 0, "pixel should be black, color is %08x\n", colr);
3134     trace("update rect: %d,%d - %d,%d\n",
3135            rcu.left, rcu.top, rcu.right, rcu.bottom);
3136     if (winetest_debug > 0) dump_region(hrgn);
3137
3138     /* clean up */
3139     DeleteObject(hrgn);
3140     DeleteObject(exprgn);
3141     DeleteObject(tmprgn);
3142     DestroyWindow(hwnd1);
3143 }
3144
3145 static void test_params(void)
3146 {
3147     HWND hwnd;
3148     INT rc;
3149
3150     /* Just a param check */
3151     SetLastError(0xdeadbeef);
3152     rc = GetWindowText(hwndMain2, NULL, 1024);
3153     ok( rc==0, "GetWindowText: rc=%d err=%d\n",rc,GetLastError());
3154
3155     SetLastError(0xdeadbeef);
3156     hwnd=CreateWindow("LISTBOX", "TestList",
3157                       (LBS_STANDARD & ~LBS_SORT),
3158                       0, 0, 100, 100,
3159                       NULL, (HMENU)1, NULL, 0);
3160
3161     ok(!hwnd, "CreateWindow with invalid menu handle should fail\n");
3162     ok(GetLastError() == ERROR_INVALID_MENU_HANDLE || /* NT */
3163        GetLastError() == 0xdeadbeef, /* Win9x */
3164        "wrong last error value %d\n", GetLastError());
3165 }
3166
3167 static void test_AWRwindow(LPCSTR class, LONG style, LONG exStyle, BOOL menu)
3168 {
3169     HWND hwnd = 0;
3170
3171     hwnd = CreateWindowEx(exStyle, class, class, style,
3172                           110, 100,
3173                           225, 200,
3174                           0,
3175                           menu ? hmenu : 0,
3176                           0, 0);
3177     if (!hwnd) {
3178         trace("Failed to create window class=%s, style=0x%08x, exStyle=0x%08x\n", class, style, exStyle);
3179         return;
3180     }
3181     ShowWindow(hwnd, SW_SHOW);
3182
3183     test_nonclient_area(hwnd);
3184
3185     SetMenu(hwnd, 0);
3186     DestroyWindow(hwnd);
3187 }
3188
3189 static BOOL AWR_init(void)
3190 {
3191     WNDCLASS class;
3192
3193     class.style         = CS_HREDRAW | CS_VREDRAW;
3194     class.lpfnWndProc     = DefWindowProcA;
3195     class.cbClsExtra    = 0;
3196     class.cbWndExtra    = 0;
3197     class.hInstance     = 0;
3198     class.hIcon         = LoadIcon (0, IDI_APPLICATION);
3199     class.hCursor       = LoadCursor (0, IDC_ARROW);
3200     class.hbrBackground = 0;
3201     class.lpszMenuName  = 0;
3202     class.lpszClassName = szAWRClass;
3203     
3204     if (!RegisterClass (&class)) {
3205         ok(FALSE, "RegisterClass failed\n");
3206         return FALSE;
3207     }
3208
3209     hmenu = CreateMenu();
3210     if (!hmenu)
3211         return FALSE;
3212     ok(hmenu != 0, "Failed to create menu\n");
3213     ok(AppendMenu(hmenu, MF_STRING, 1, "Test!"), "Failed to create menu item\n");
3214     
3215     return TRUE;
3216 }
3217
3218
3219 static void test_AWR_window_size(BOOL menu)
3220 {
3221     LONG styles[] = {
3222         WS_POPUP,
3223         WS_MAXIMIZE, WS_BORDER, WS_DLGFRAME, 
3224         WS_SYSMENU, 
3225         WS_THICKFRAME,
3226         WS_MINIMIZEBOX, WS_MAXIMIZEBOX,
3227         WS_HSCROLL, WS_VSCROLL
3228     };
3229     LONG exStyles[] = {
3230         WS_EX_CLIENTEDGE,
3231         WS_EX_TOOLWINDOW, WS_EX_WINDOWEDGE,
3232         WS_EX_APPWINDOW,
3233 #if 0
3234         /* These styles have problems on (at least) WinXP (SP2) and Wine */
3235         WS_EX_DLGMODALFRAME, 
3236         WS_EX_STATICEDGE, 
3237 #endif
3238     };
3239
3240     int i;    
3241
3242     /* A exhaustive check of all the styles takes too long
3243      * so just do a (hopefully representative) sample
3244      */
3245     for (i = 0; i < COUNTOF(styles); ++i)
3246         test_AWRwindow(szAWRClass, styles[i], 0, menu);
3247     for (i = 0; i < COUNTOF(exStyles); ++i) {
3248         test_AWRwindow(szAWRClass, WS_POPUP, exStyles[i], menu);
3249         test_AWRwindow(szAWRClass, WS_THICKFRAME, exStyles[i], menu);
3250     }
3251 }
3252 #undef COUNTOF
3253
3254 #define SHOWSYSMETRIC(SM) trace(#SM "=%d\n", GetSystemMetrics(SM))
3255
3256 static void test_AdjustWindowRect(void)
3257 {
3258     if (!AWR_init())
3259         return;
3260     
3261     SHOWSYSMETRIC(SM_CYCAPTION);
3262     SHOWSYSMETRIC(SM_CYSMCAPTION);
3263     SHOWSYSMETRIC(SM_CYMENU);
3264     SHOWSYSMETRIC(SM_CXEDGE);
3265     SHOWSYSMETRIC(SM_CYEDGE);
3266     SHOWSYSMETRIC(SM_CXVSCROLL);
3267     SHOWSYSMETRIC(SM_CYHSCROLL);
3268     SHOWSYSMETRIC(SM_CXFRAME);
3269     SHOWSYSMETRIC(SM_CYFRAME);
3270     SHOWSYSMETRIC(SM_CXDLGFRAME);
3271     SHOWSYSMETRIC(SM_CYDLGFRAME);
3272     SHOWSYSMETRIC(SM_CXBORDER);
3273     SHOWSYSMETRIC(SM_CYBORDER);  
3274
3275     test_AWR_window_size(FALSE);
3276     test_AWR_window_size(TRUE);
3277
3278     DestroyMenu(hmenu);
3279 }
3280 #undef SHOWSYSMETRIC
3281
3282
3283 /* Global variables to trigger exit from loop */
3284 static int redrawComplete, WMPAINT_count;
3285
3286 static LRESULT WINAPI redraw_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
3287 {
3288     switch (msg)
3289     {
3290     case WM_PAINT:
3291         trace("doing WM_PAINT %d\n", WMPAINT_count);
3292         WMPAINT_count++;
3293         if (WMPAINT_count > 10 && redrawComplete == 0) {
3294             PAINTSTRUCT ps;
3295             BeginPaint(hwnd, &ps);
3296             EndPaint(hwnd, &ps);
3297             return 1;
3298         }
3299         return 0;
3300         break;
3301     }
3302     return DefWindowProc(hwnd, msg, wparam, lparam);
3303 }
3304
3305 /* Ensure we exit from RedrawNow regardless of invalidated area */
3306 static void test_redrawnow(void)
3307 {
3308    WNDCLASSA cls;
3309    HWND hwndMain;
3310
3311    cls.style = CS_DBLCLKS;
3312    cls.lpfnWndProc = redraw_window_procA;
3313    cls.cbClsExtra = 0;
3314    cls.cbWndExtra = 0;
3315    cls.hInstance = GetModuleHandleA(0);
3316    cls.hIcon = 0;
3317    cls.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
3318    cls.hbrBackground = GetStockObject(WHITE_BRUSH);
3319    cls.lpszMenuName = NULL;
3320    cls.lpszClassName = "RedrawWindowClass";
3321
3322    if(!RegisterClassA(&cls)) {
3323        trace("Register failed %d\n", GetLastError());
3324        return;
3325    }
3326
3327    hwndMain = CreateWindowA("RedrawWindowClass", "Main Window", WS_OVERLAPPEDWINDOW,
3328                             CW_USEDEFAULT, 0, 100, 100, NULL, NULL, 0, NULL);
3329
3330    ok( WMPAINT_count == 0, "Multiple unexpected WM_PAINT calls %d\n", WMPAINT_count);
3331    ShowWindow(hwndMain, SW_SHOW);
3332    ok( WMPAINT_count == 0, "Multiple unexpected WM_PAINT calls %d\n", WMPAINT_count);
3333    RedrawWindow(hwndMain, NULL,NULL,RDW_UPDATENOW | RDW_ALLCHILDREN);
3334    ok( WMPAINT_count == 1, "Multiple unexpected WM_PAINT calls %d\n", WMPAINT_count);
3335    redrawComplete = TRUE;
3336    ok( WMPAINT_count < 10, "RedrawWindow (RDW_UPDATENOW) never completed (%d)\n", WMPAINT_count);
3337
3338    /* clean up */
3339    DestroyWindow( hwndMain);
3340 }
3341
3342 struct parentdc_stat {
3343     RECT client;
3344     RECT clip;
3345     RECT paint;
3346 };
3347
3348 struct parentdc_test {
3349    struct parentdc_stat main, main_todo;
3350    struct parentdc_stat child1, child1_todo;
3351    struct parentdc_stat child2, child2_todo;
3352 };
3353
3354 static LRESULT WINAPI parentdc_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
3355 {
3356     RECT rc;
3357     PAINTSTRUCT ps;
3358
3359     struct parentdc_stat *t = (struct parentdc_stat *)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
3360
3361     switch (msg)
3362     {
3363     case WM_PAINT:
3364         trace("doing WM_PAINT on %p\n", hwnd);
3365         GetClientRect(hwnd, &rc);
3366         CopyRect(&t->client, &rc);
3367         trace("client rect (%d, %d)-(%d, %d)\n", rc.left, rc.top, rc.right, rc.bottom);
3368         GetWindowRect(hwnd, &rc);
3369         trace("window rect (%d, %d)-(%d, %d)\n", rc.left, rc.top, rc.right, rc.bottom);
3370         BeginPaint(hwnd, &ps);
3371         CopyRect(&t->paint, &ps.rcPaint);
3372         GetClipBox(ps.hdc, &rc);
3373         CopyRect(&t->clip, &rc);
3374         trace("clip rect (%d, %d)-(%d, %d)\n", rc.left, rc.top, rc.right, rc.bottom);
3375         trace("paint rect (%d, %d)-(%d, %d)\n", ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
3376         EndPaint(hwnd, &ps);
3377         return 0;
3378     }
3379     return DefWindowProc(hwnd, msg, wparam, lparam);
3380 }
3381
3382 static void zero_parentdc_stat(struct parentdc_stat *t)
3383 {
3384     SetRectEmpty(&t->client);
3385     SetRectEmpty(&t->clip);
3386     SetRectEmpty(&t->paint);
3387 }
3388
3389 static void zero_parentdc_test(struct parentdc_test *t)
3390 {
3391     zero_parentdc_stat(&t->main);
3392     zero_parentdc_stat(&t->child1);
3393     zero_parentdc_stat(&t->child2);
3394 }
3395
3396 #define parentdc_field_ok(t, w, r, f, got) \
3397   ok (t.w.r.f==got.w.r.f, "window " #w ", rect " #r ", field " #f \
3398       ": expected %d, got %d\n", \
3399       t.w.r.f, got.w.r.f)
3400
3401 #define parentdc_todo_field_ok(t, w, r, f, got) \
3402   if (t.w##_todo.r.f) todo_wine { parentdc_field_ok(t, w, r, f, got); } \
3403   else parentdc_field_ok(t, w, r, f, got)
3404
3405 #define parentdc_rect_ok(t, w, r, got) \
3406   parentdc_todo_field_ok(t, w, r, left, got); \
3407   parentdc_todo_field_ok(t, w, r, top, got); \
3408   parentdc_todo_field_ok(t, w, r, right, got); \
3409   parentdc_todo_field_ok(t, w, r, bottom, got);
3410
3411 #define parentdc_win_ok(t, w, got) \
3412   parentdc_rect_ok(t, w, client, got); \
3413   parentdc_rect_ok(t, w, clip, got); \
3414   parentdc_rect_ok(t, w, paint, got);
3415
3416 #define parentdc_ok(t, got) \
3417   parentdc_win_ok(t, main, got); \
3418   parentdc_win_ok(t, child1, got); \
3419   parentdc_win_ok(t, child2, got);
3420
3421 static void test_csparentdc(void)
3422 {
3423    WNDCLASSA clsMain, cls;
3424    HWND hwndMain, hwnd1, hwnd2;
3425    MSG msg;
3426    RECT rc;
3427
3428    struct parentdc_test test_answer;
3429
3430 #define nothing_todo {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}
3431    const struct parentdc_test test1 = 
3432    {
3433         {{0, 0, 150, 150}, {0, 0, 150, 150}, {0, 0, 150, 150}}, nothing_todo,
3434         {{0, 0, 40, 40}, {-20, -20, 130, 130}, {0, 0, 40, 40}}, {{0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}},
3435         {{0, 0, 40, 40}, {-40, -40, 110, 110}, {0, 0, 40, 40}}, {{0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}},
3436    };
3437
3438    const struct parentdc_test test2 = 
3439    {
3440         {{0, 0, 150, 150}, {0, 0, 50, 50}, {0, 0, 50, 50}}, nothing_todo,
3441         {{0, 0, 40, 40}, {-20, -20, 30, 30}, {0, 0, 30, 30}}, {{0, 0, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}},
3442         {{0, 0, 40, 40}, {-40, -40, 10, 10}, {0, 0, 10, 10}}, {{0, 0, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}},
3443    };
3444
3445    const struct parentdc_test test3 = 
3446    {
3447         {{0, 0, 150, 150}, {0, 0, 10, 10}, {0, 0, 10, 10}}, nothing_todo,
3448         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
3449         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
3450    };
3451
3452    const struct parentdc_test test4 = 
3453    {
3454         {{0, 0, 150, 150}, {40, 40, 50, 50}, {40, 40, 50, 50}}, nothing_todo,
3455         {{0, 0, 40, 40}, {20, 20, 30, 30}, {20, 20, 30, 30}}, nothing_todo,
3456         {{0, 0, 40, 40}, {0, 0, 10, 10}, {0, 0, 10, 10}}, nothing_todo,
3457    };
3458
3459    const struct parentdc_test test5 = 
3460    {
3461         {{0, 0, 150, 150}, {20, 20, 60, 60}, {20, 20, 60, 60}}, nothing_todo,
3462         {{0, 0, 40, 40}, {-20, -20, 130, 130}, {0, 0, 40, 40}}, {{0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}},
3463         {{0, 0, 40, 40}, {-20, -20, 20, 20}, {0, 0, 20, 20}}, {{0, 0, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}},
3464    };
3465
3466    const struct parentdc_test test6 = 
3467    {
3468         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
3469         {{0, 0, 40, 40}, {0, 0, 10, 10}, {0, 0, 10, 10}}, nothing_todo,
3470         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
3471    };
3472
3473    const struct parentdc_test test7 = 
3474    {
3475         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
3476         {{0, 0, 40, 40}, {-20, -20, 130, 130}, {0, 0, 40, 40}}, {{0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}},
3477         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
3478    };
3479 #undef nothing_todo
3480
3481    clsMain.style = CS_DBLCLKS;
3482    clsMain.lpfnWndProc = parentdc_window_procA;
3483    clsMain.cbClsExtra = 0;
3484    clsMain.cbWndExtra = 0;
3485    clsMain.hInstance = GetModuleHandleA(0);
3486    clsMain.hIcon = 0;
3487    clsMain.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
3488    clsMain.hbrBackground = GetStockObject(WHITE_BRUSH);
3489    clsMain.lpszMenuName = NULL;
3490    clsMain.lpszClassName = "ParentDcMainWindowClass";
3491
3492    if(!RegisterClassA(&clsMain)) {
3493        trace("Register failed %d\n", GetLastError());
3494        return;
3495    }
3496
3497    cls.style = CS_DBLCLKS | CS_PARENTDC;
3498    cls.lpfnWndProc = parentdc_window_procA;
3499    cls.cbClsExtra = 0;
3500    cls.cbWndExtra = 0;
3501    cls.hInstance = GetModuleHandleA(0);
3502    cls.hIcon = 0;
3503    cls.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
3504    cls.hbrBackground = GetStockObject(WHITE_BRUSH);
3505    cls.lpszMenuName = NULL;
3506    cls.lpszClassName = "ParentDcWindowClass";
3507
3508    if(!RegisterClassA(&cls)) {
3509        trace("Register failed %d\n", GetLastError());
3510        return;
3511    }
3512
3513    SetRect(&rc, 0, 0, 150, 150);
3514    AdjustWindowRectEx(&rc, WS_OVERLAPPEDWINDOW, FALSE, 0);
3515    hwndMain = CreateWindowA("ParentDcMainWindowClass", "Main Window", WS_OVERLAPPEDWINDOW,
3516                             CW_USEDEFAULT, 0, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, 0, NULL);
3517    SetWindowLongPtrA(hwndMain, GWLP_USERDATA, (DWORD_PTR)&test_answer.main);
3518    hwnd1 = CreateWindowA("ParentDcWindowClass", "Child Window 1", WS_CHILD,
3519                             20, 20, 40, 40, hwndMain, NULL, 0, NULL);
3520    SetWindowLongPtrA(hwnd1, GWLP_USERDATA, (DWORD_PTR)&test_answer.child1);
3521    hwnd2 = CreateWindowA("ParentDcWindowClass", "Child Window 2", WS_CHILD,
3522                             40, 40, 40, 40, hwndMain, NULL, 0, NULL);
3523    SetWindowLongPtrA(hwnd2, GWLP_USERDATA, (DWORD_PTR)&test_answer.child2);
3524    ShowWindow(hwndMain, SW_SHOW);
3525    ShowWindow(hwnd1, SW_SHOW);
3526    ShowWindow(hwnd2, SW_SHOW);
3527    flush_events();
3528
3529    zero_parentdc_test(&test_answer);
3530    InvalidateRect(hwndMain, NULL, TRUE);
3531    while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
3532    parentdc_ok(test1, test_answer);
3533
3534    zero_parentdc_test(&test_answer);
3535    SetRect(&rc, 0, 0, 50, 50);
3536    InvalidateRect(hwndMain, &rc, TRUE);
3537    while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
3538    parentdc_ok(test2, test_answer);
3539
3540    zero_parentdc_test(&test_answer);
3541    SetRect(&rc, 0, 0, 10, 10);
3542    InvalidateRect(hwndMain, &rc, TRUE);
3543    while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
3544    parentdc_ok(test3, test_answer);
3545
3546    zero_parentdc_test(&test_answer);
3547    SetRect(&rc, 40, 40, 50, 50);
3548    InvalidateRect(hwndMain, &rc, TRUE);
3549    while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
3550    parentdc_ok(test4, test_answer);
3551
3552    zero_parentdc_test(&test_answer);
3553    SetRect(&rc, 20, 20, 60, 60);
3554    InvalidateRect(hwndMain, &rc, TRUE);
3555    while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
3556    parentdc_ok(test5, test_answer);
3557
3558    zero_parentdc_test(&test_answer);
3559    SetRect(&rc, 0, 0, 10, 10);
3560    InvalidateRect(hwnd1, &rc, TRUE);
3561    while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
3562    parentdc_ok(test6, test_answer);
3563
3564    zero_parentdc_test(&test_answer);
3565    SetRect(&rc, -5, -5, 65, 65);
3566    InvalidateRect(hwnd1, &rc, TRUE);
3567    while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
3568    parentdc_ok(test7, test_answer);
3569
3570    DestroyWindow(hwndMain);
3571    DestroyWindow(hwnd1);
3572    DestroyWindow(hwnd2);
3573 }
3574
3575 static void test_IsWindowUnicode(void)
3576 {
3577     static const char ansi_class_nameA[] = "ansi class name";
3578     static const WCHAR ansi_class_nameW[] = {'a','n','s','i',' ','c','l','a','s','s',' ','n','a','m','e',0};
3579     static const char unicode_class_nameA[] = "unicode class name";
3580     static const WCHAR unicode_class_nameW[] = {'u','n','i','c','o','d','e',' ','c','l','a','s','s',' ','n','a','m','e',0};
3581     WNDCLASSA classA;
3582     WNDCLASSW classW;
3583     HWND hwnd;
3584
3585     memset(&classW, 0, sizeof(classW));
3586     classW.hInstance = GetModuleHandleA(0);
3587     classW.lpfnWndProc = DefWindowProcW;
3588     classW.lpszClassName = unicode_class_nameW;
3589     if (!RegisterClassW(&classW)) return;
3590
3591     memset(&classA, 0, sizeof(classA));
3592     classA.hInstance = GetModuleHandleA(0);
3593     classA.lpfnWndProc = DefWindowProcA;
3594     classA.lpszClassName = ansi_class_nameA;
3595     assert(RegisterClassA(&classA));
3596
3597     /* unicode class: window proc */
3598     hwnd = CreateWindowExW(0, unicode_class_nameW, NULL, WS_POPUP,
3599                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
3600     assert(hwnd);
3601
3602     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
3603     SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (ULONG_PTR)DefWindowProcA);
3604     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
3605     SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (ULONG_PTR)DefWindowProcW);
3606     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
3607
3608     DestroyWindow(hwnd);
3609
3610     hwnd = CreateWindowExA(0, unicode_class_nameA, NULL, WS_POPUP,
3611                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
3612     assert(hwnd);
3613
3614     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
3615     SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (ULONG_PTR)DefWindowProcA);
3616     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
3617     SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (ULONG_PTR)DefWindowProcW);
3618     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
3619
3620     DestroyWindow(hwnd);
3621
3622     /* ansi class: window proc */
3623     hwnd = CreateWindowExW(0, ansi_class_nameW, NULL, WS_POPUP,
3624                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
3625     assert(hwnd);
3626
3627     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
3628     SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (ULONG_PTR)DefWindowProcW);
3629     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
3630     SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (ULONG_PTR)DefWindowProcA);
3631     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
3632
3633     DestroyWindow(hwnd);
3634
3635     hwnd = CreateWindowExA(0, ansi_class_nameA, NULL, WS_POPUP,
3636                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
3637     assert(hwnd);
3638
3639     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
3640     SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (ULONG_PTR)DefWindowProcW);
3641     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
3642     SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (ULONG_PTR)DefWindowProcA);
3643     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
3644
3645     DestroyWindow(hwnd);
3646
3647     /* unicode class: class proc */
3648     hwnd = CreateWindowExW(0, unicode_class_nameW, NULL, WS_POPUP,
3649                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
3650     assert(hwnd);
3651
3652     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
3653     SetClassLongPtrA(hwnd, GCLP_WNDPROC, (ULONG_PTR)DefWindowProcA);
3654     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
3655     /* do not restore class window proc back to unicode */
3656
3657     DestroyWindow(hwnd);
3658
3659     hwnd = CreateWindowExA(0, unicode_class_nameA, NULL, WS_POPUP,
3660                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
3661     assert(hwnd);
3662
3663     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
3664     SetClassLongPtrW(hwnd, GCLP_WNDPROC, (ULONG_PTR)DefWindowProcW);
3665     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
3666
3667     DestroyWindow(hwnd);
3668
3669     /* ansi class: class proc */
3670     hwnd = CreateWindowExW(0, ansi_class_nameW, NULL, WS_POPUP,
3671                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
3672     assert(hwnd);
3673
3674     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
3675     SetClassLongPtrW(hwnd, GCLP_WNDPROC, (ULONG_PTR)DefWindowProcW);
3676     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
3677     /* do not restore class window proc back to ansi */
3678
3679     DestroyWindow(hwnd);
3680
3681     hwnd = CreateWindowExA(0, ansi_class_nameA, NULL, WS_POPUP,
3682                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
3683     assert(hwnd);
3684
3685     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
3686     SetClassLongPtrA(hwnd, GCLP_WNDPROC, (ULONG_PTR)DefWindowProcA);
3687     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
3688
3689     DestroyWindow(hwnd);
3690 }
3691
3692 static void test_CreateWindow(void)
3693 {
3694     HWND hwnd, parent;
3695     HMENU hmenu;
3696
3697 #define expect_menu(window, menu) \
3698     SetLastError(0xdeadbeef); \
3699     ok(GetMenu(window) == (HMENU)menu, "GetMenu error %d\n", GetLastError())
3700
3701 #define expect_style(window, style)\
3702     ok(GetWindowLong(window, GWL_STYLE) == (style), "expected style %x != %x\n", (LONG)(style), GetWindowLong(window, GWL_STYLE))
3703
3704 #define expect_ex_style(window, ex_style)\
3705     ok(GetWindowLong(window, GWL_EXSTYLE) == (ex_style), "expected ex_style %x != %x\n", (LONG)(ex_style), GetWindowLong(window, GWL_EXSTYLE))
3706
3707     hmenu = CreateMenu();
3708     assert(hmenu != 0);
3709     parent = GetDesktopWindow();
3710     assert(parent != 0);
3711
3712     SetLastError(0xdeadbeef);
3713     ok(IsMenu(hmenu), "IsMenu error %d\n", GetLastError());
3714
3715     SetLastError(0xdeadbeef);
3716     hwnd = CreateWindowEx(WS_EX_APPWINDOW, "static", NULL, WS_CHILD,
3717                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
3718     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
3719     expect_menu(hwnd, 1);
3720     expect_style(hwnd, WS_CHILD);
3721     expect_ex_style(hwnd, WS_EX_APPWINDOW);
3722     DestroyWindow(hwnd);
3723
3724     SetLastError(0xdeadbeef);
3725     hwnd = CreateWindowEx(WS_EX_APPWINDOW, "static", NULL, WS_CHILD | WS_CAPTION,
3726                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
3727     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
3728     expect_menu(hwnd, 1);
3729     expect_style(hwnd, WS_CHILD | WS_CAPTION);
3730     expect_ex_style(hwnd, WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
3731     DestroyWindow(hwnd);
3732
3733     SetLastError(0xdeadbeef);
3734     hwnd = CreateWindowEx(0, "static", NULL, WS_CHILD,
3735                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
3736     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
3737     expect_menu(hwnd, 1);
3738     expect_style(hwnd, WS_CHILD);
3739     expect_ex_style(hwnd, 0);
3740     DestroyWindow(hwnd);
3741
3742     SetLastError(0xdeadbeef);
3743     hwnd = CreateWindowEx(0, "static", NULL, WS_CHILD | WS_CAPTION,
3744                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
3745     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
3746     expect_menu(hwnd, 1);
3747     expect_style(hwnd, WS_CHILD | WS_CAPTION);
3748     expect_ex_style(hwnd, WS_EX_WINDOWEDGE);
3749     DestroyWindow(hwnd);
3750
3751     SetLastError(0xdeadbeef);
3752     hwnd = CreateWindowEx(WS_EX_APPWINDOW, "static", NULL, WS_POPUP,
3753                            0, 0, 100, 100, parent, hmenu, 0, NULL);
3754     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
3755     expect_menu(hwnd, hmenu);
3756     expect_style(hwnd, WS_POPUP | WS_CLIPSIBLINGS);
3757     expect_ex_style(hwnd, WS_EX_APPWINDOW);
3758     DestroyWindow(hwnd);
3759     SetLastError(0xdeadbeef);
3760     ok(!IsMenu(hmenu), "IsMenu should fail\n");
3761     ok(GetLastError() == ERROR_INVALID_MENU_HANDLE, "IsMenu set error %d\n", GetLastError());
3762
3763     hmenu = CreateMenu();
3764     assert(hmenu != 0);
3765     SetLastError(0xdeadbeef);
3766     hwnd = CreateWindowEx(WS_EX_APPWINDOW, "static", NULL, WS_POPUP | WS_CAPTION,
3767                            0, 0, 100, 100, parent, hmenu, 0, NULL);
3768     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
3769     expect_menu(hwnd, hmenu);
3770     expect_style(hwnd, WS_POPUP | WS_CAPTION | WS_CLIPSIBLINGS);
3771     expect_ex_style(hwnd, WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
3772     DestroyWindow(hwnd);
3773     SetLastError(0xdeadbeef);
3774     ok(!IsMenu(hmenu), "IsMenu should fail\n");
3775     ok(GetLastError() == ERROR_INVALID_MENU_HANDLE, "IsMenu set error %d\n", GetLastError());
3776
3777     hmenu = CreateMenu();
3778     assert(hmenu != 0);
3779     SetLastError(0xdeadbeef);
3780     hwnd = CreateWindowEx(WS_EX_APPWINDOW, "static", NULL, WS_POPUP,
3781                            0, 0, 100, 100, parent, hmenu, 0, NULL);
3782     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
3783     expect_menu(hwnd, hmenu);
3784     expect_style(hwnd, WS_POPUP | WS_CLIPSIBLINGS);
3785     expect_ex_style(hwnd, WS_EX_APPWINDOW);
3786     DestroyWindow(hwnd);
3787     SetLastError(0xdeadbeef);
3788     ok(!IsMenu(hmenu), "IsMenu should fail\n");
3789     ok(GetLastError() == ERROR_INVALID_MENU_HANDLE, "IsMenu set error %d\n", GetLastError());
3790
3791     hmenu = CreateMenu();
3792     assert(hmenu != 0);
3793     SetLastError(0xdeadbeef);
3794     hwnd = CreateWindowEx(0, "static", NULL, WS_POPUP | WS_CAPTION,
3795                            0, 0, 100, 100, parent, hmenu, 0, NULL);
3796     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
3797     expect_menu(hwnd, hmenu);
3798     expect_style(hwnd, WS_POPUP | WS_CAPTION | WS_CLIPSIBLINGS);
3799     expect_ex_style(hwnd, WS_EX_WINDOWEDGE);
3800     DestroyWindow(hwnd);
3801     SetLastError(0xdeadbeef);
3802     ok(!IsMenu(hmenu), "IsMenu should fail\n");
3803     ok(GetLastError() == ERROR_INVALID_MENU_HANDLE, "IsMenu set error %d\n", GetLastError());
3804
3805     hmenu = CreateMenu();
3806     assert(hmenu != 0);
3807     SetLastError(0xdeadbeef);
3808     hwnd = CreateWindowEx(0, "static", NULL, WS_POPUP,
3809                            0, 0, 100, 100, parent, hmenu, 0, NULL);
3810     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
3811     expect_menu(hwnd, hmenu);
3812     expect_style(hwnd, WS_POPUP | WS_CLIPSIBLINGS);
3813     expect_ex_style(hwnd, 0);
3814     DestroyWindow(hwnd);
3815     SetLastError(0xdeadbeef);
3816     ok(!IsMenu(hmenu), "IsMenu should fail\n");
3817     ok(GetLastError() == ERROR_INVALID_MENU_HANDLE, "IsMenu set error %d\n", GetLastError());
3818
3819 #undef expect_menu
3820 #undef expect_style
3821 #undef expect_ex_style
3822 }
3823
3824 /* function that remembers whether the system the test is running on sets the
3825  * last error for user32 functions to make the tests stricter */
3826 static int check_error(DWORD actual, DWORD expected)
3827 {
3828     static int sets_last_error = -1;
3829     if (sets_last_error == -1)
3830         sets_last_error = (actual != 0xdeadbeef);
3831     return (!sets_last_error && (actual == 0xdeadbeef)) || (actual == expected);
3832 }
3833
3834 static void test_SetWindowLong(void)
3835 {
3836     LONG_PTR retval;
3837     WNDPROC old_window_procW;
3838
3839     SetLastError(0xdeadbeef);
3840     retval = SetWindowLongPtr(NULL, GWLP_WNDPROC, 0);
3841     ok(!retval, "SetWindowLongPtr on invalid window handle should have returned 0 instead of 0x%x\n", retval);
3842     ok(check_error(GetLastError(), ERROR_INVALID_WINDOW_HANDLE),
3843         "SetWindowLongPtr should have set error to ERROR_INVALID_WINDOW_HANDLE instad of %d\n", GetLastError());
3844
3845     SetLastError(0xdeadbeef);
3846     retval = SetWindowLongPtr(hwndMain, 0xdeadbeef, 0);
3847     ok(!retval, "SetWindowLongPtr on invalid index should have returned 0 instead of 0x%x\n", retval);
3848     ok(check_error(GetLastError(), ERROR_INVALID_INDEX),
3849         "SetWindowLongPtr should have set error to ERROR_INVALID_INDEX instad of %d\n", GetLastError());
3850
3851     SetLastError(0xdeadbeef);
3852     retval = SetWindowLongPtr(hwndMain, GWLP_WNDPROC, 0);
3853     ok((WNDPROC)retval == main_window_procA,
3854         "SetWindowLongPtr on invalid window proc should have returned address of main_window_procA instead of 0x%x\n", retval);
3855     ok(GetLastError() == 0xdeadbeef, "SetWindowLongPtr shouldn't have set the last error, instead of setting it to %d\n", GetLastError());
3856     retval = GetWindowLongPtr(hwndMain, GWLP_WNDPROC);
3857     ok((WNDPROC)retval == main_window_procA,
3858         "SetWindowLongPtr on invalid window proc shouldn't have changed the value returned by GetWindowLongPtr, instead of changing it to 0x%x\n", retval);
3859     ok(!IsWindowUnicode(hwndMain), "hwndMain shouldn't be Unicode\n");
3860
3861     old_window_procW = (WNDPROC)GetWindowLongPtrW(hwndMain, GWLP_WNDPROC);
3862     SetLastError(0xdeadbeef);
3863     retval = SetWindowLongPtrW(hwndMain, GWLP_WNDPROC, 0);
3864     if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
3865     {
3866         ok(GetLastError() == 0xdeadbeef, "SetWindowLongPtr shouldn't have set the last error, instead of setting it to %d\n", GetLastError());
3867         ok(retval != 0, "SetWindowLongPtr error %d\n", GetLastError());
3868         ok((WNDPROC)retval == old_window_procW,
3869             "SetWindowLongPtr on invalid window proc shouldn't have changed the value returned by GetWindowLongPtr, instead of changing it to 0x%x\n", retval);
3870         ok(IsWindowUnicode(hwndMain), "hwndMain should now be Unicode\n");
3871
3872         /* set it back to ANSI */
3873         SetWindowLongPtr(hwndMain, GWLP_WNDPROC, 0);
3874     }
3875 }
3876
3877 START_TEST(win)
3878 {
3879     pGetAncestor = (void *)GetProcAddress( GetModuleHandleA("user32.dll"), "GetAncestor" );
3880     pGetWindowInfo = (void *)GetProcAddress( GetModuleHandleA("user32.dll"), "GetWindowInfo" );
3881
3882     hwndMain = CreateWindowExA(0, "static", NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, 0, 0, NULL);
3883     if (hwndMain)
3884     {
3885         ok(!GetParent(hwndMain), "GetParent should return 0 for message only windows\n");
3886         if (pGetAncestor)
3887         {
3888             hwndMessage = pGetAncestor(hwndMain, GA_PARENT);
3889             ok(hwndMessage != 0, "GetAncestor(GA_PARENT) should not return 0 for message only windows\n");
3890             trace("hwndMessage %p\n", hwndMessage);
3891         }
3892         DestroyWindow(hwndMain);
3893     }
3894     else
3895         trace("CreateWindowExA with parent HWND_MESSAGE failed\n");
3896
3897     if (!RegisterWindowClasses()) assert(0);
3898
3899     hhook = SetWindowsHookExA(WH_CBT, cbt_hook_proc, 0, GetCurrentThreadId());
3900     assert(hhook);
3901
3902     hwndMain = CreateWindowExA(/*WS_EX_TOOLWINDOW*/ 0, "MainWindowClass", "Main window",
3903                                WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
3904                                WS_MAXIMIZEBOX | WS_POPUP,
3905                                100, 100, 200, 200,
3906                                0, 0, 0, NULL);
3907     test_nonclient_area(hwndMain);
3908
3909     hwndMain2 = CreateWindowExA(/*WS_EX_TOOLWINDOW*/ 0, "MainWindowClass", "Main window 2",
3910                                 WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
3911                                 WS_MAXIMIZEBOX | WS_POPUP,
3912                                 100, 100, 200, 200,
3913                                 0, 0, 0, NULL);
3914     assert( hwndMain );
3915     assert( hwndMain2 );
3916
3917     /* Add the tests below this line */
3918     test_params();
3919
3920     test_capture_1();
3921     test_capture_2();
3922     test_capture_3(hwndMain, hwndMain2);
3923
3924     test_CreateWindow();
3925     test_parent_owner();
3926     test_SetParent();
3927     test_shell_window();
3928
3929     test_mdi();
3930     test_icons();
3931     test_SetWindowPos(hwndMain);
3932     test_SetMenu(hwndMain);
3933     test_SetFocus(hwndMain);
3934     test_SetActiveWindow(hwndMain);
3935     test_SetForegroundWindow(hwndMain);
3936
3937     test_children_zorder(hwndMain);
3938     test_keyboard_input(hwndMain);
3939     test_mouse_input(hwndMain);
3940     test_validatergn(hwndMain);
3941     test_nccalcscroll( hwndMain);
3942     test_scrollvalidate( hwndMain);
3943     test_scrolldc( hwndMain);
3944     test_scroll();
3945     test_IsWindowUnicode();
3946     test_vis_rgn(hwndMain);
3947
3948     test_AdjustWindowRect();
3949     test_window_styles();
3950     test_redrawnow();
3951     test_csparentdc();
3952     test_SetWindowLong();
3953
3954     /* add the tests above this line */
3955     UnhookWindowsHookEx(hhook);
3956 }