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