wined3d: Implement more GLSL instructions and a little cleanup.
[wine] / dlls / user / tests / win.c
1 /*
2  * Unit tests for window handling
3  *
4  * Copyright 2002 Bill Medland
5  * Copyright 2002 Alexandre Julliard
6  * Copyright 2003 Dmitry Timoshkov
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 /* To get ICON_SMALL2 with the MSVC headers */
24 #define _WIN32_WINNT 0x0501
25
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
28
29 #include <assert.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wingdi.h"
37 #include "winuser.h"
38
39 #include "wine/test.h"
40
41 #ifndef SPI_GETDESKWALLPAPER
42 #define SPI_GETDESKWALLPAPER 0x0073
43 #endif
44
45 #define LONG_PTR INT_PTR
46 #define ULONG_PTR UINT_PTR
47
48 void dump_region(HRGN hrgn);
49
50 static HWND (WINAPI *pGetAncestor)(HWND,UINT);
51 static BOOL (WINAPI *pGetWindowInfo)(HWND,WINDOWINFO*);
52
53 static BOOL test_lbuttondown_flag;
54 static HWND hwndMessage;
55 static HWND hwndMain, hwndMain2;
56 static HHOOK hhook;
57
58 static const char* szAWRClass = "Winsize";
59 static HMENU hmenu;
60
61 #define COUNTOF(arr) (sizeof(arr)/sizeof(arr[0]))
62
63 /* try to make sure pending X events have been processed before continuing */
64 static void flush_events(void)
65 {
66     MSG msg;
67     int diff = 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     flush_events();
2318
2319     ok(GetActiveWindow() == hwnd, "wrong active window %p\n", GetActiveWindow());
2320
2321     SetFocus(hwnd);
2322     ok(GetFocus() == hwnd, "wrong focus window %p\n", GetFocus());
2323
2324     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
2325
2326     PostMessageA(hwnd, WM_KEYDOWN, 0, 0);
2327     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2328     ok(msg.hwnd == hwnd && msg.message == WM_KEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2329     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
2330     ok( !ret, "message %04x available\n", msg.message);
2331
2332     ok(GetFocus() == hwnd, "wrong focus window %p\n", GetFocus());
2333
2334     PostThreadMessageA(GetCurrentThreadId(), WM_KEYDOWN, 0, 0);
2335     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2336     ok(!msg.hwnd && msg.message == WM_KEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2337     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
2338     ok( !ret, "message %04x available\n", msg.message);
2339
2340     ok(GetFocus() == hwnd, "wrong focus window %p\n", GetFocus());
2341
2342     keybd_event(VK_SPACE, 0, 0, 0);
2343     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2344     ok(msg.hwnd == hwnd && msg.message == WM_KEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2345     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
2346     ok( !ret, "message %04x available\n", msg.message);
2347
2348     SetFocus(0);
2349     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
2350
2351     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
2352
2353     PostMessageA(hwnd, WM_KEYDOWN, 0, 0);
2354     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2355     ok(msg.hwnd == hwnd && msg.message == WM_KEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2356     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
2357     ok( !ret, "message %04x available\n", msg.message);
2358
2359     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
2360
2361     PostThreadMessageA(GetCurrentThreadId(), WM_KEYDOWN, 0, 0);
2362     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2363     ok(!msg.hwnd && msg.message == WM_KEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2364     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
2365     ok( !ret, "message %04x available\n", msg.message);
2366
2367     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
2368
2369     keybd_event(VK_SPACE, 0, 0, 0);
2370     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2371     ok(msg.hwnd == hwnd && msg.message == WM_SYSKEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2372     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
2373     ok( !ret, "message %04x available\n", msg.message);
2374 }
2375
2376 static void test_mouse_input(HWND hwnd)
2377 {
2378     RECT rc;
2379     POINT pt;
2380     int x, y;
2381     HWND popup;
2382     MSG msg;
2383     BOOL ret;
2384     LRESULT res;
2385
2386     ShowWindow(hwnd, SW_SHOW);
2387     UpdateWindow(hwnd);
2388
2389     GetWindowRect(hwnd, &rc);
2390     trace("main window %p: (%ld,%ld)-(%ld,%ld)\n", hwnd, rc.left, rc.top, rc.right, rc.bottom);
2391
2392     popup = CreateWindowExA(0, "MainWindowClass", NULL, WS_POPUP,
2393                             rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top,
2394                             hwnd, 0, 0, NULL);
2395     assert(popup != 0);
2396     ShowWindow(popup, SW_SHOW);
2397     UpdateWindow(popup);
2398
2399     GetWindowRect(popup, &rc);
2400     trace("popup window %p: (%ld,%ld)-(%ld,%ld)\n", popup, rc.left, rc.top, rc.right, rc.bottom);
2401
2402     x = rc.left + (rc.right - rc.left) / 2;
2403     y = rc.top + (rc.bottom - rc.top) / 2;
2404     trace("setting cursor to (%d,%d)\n", x, y);
2405
2406     SetCursorPos(x, y);
2407     GetCursorPos(&pt);
2408     ok(x == pt.x && y == pt.y, "wrong cursor pos (%ld,%ld), expected (%d,%d)\n", pt.x, pt.y, x, y);
2409
2410     /* force the system to update its internal queue mouse position,
2411      * otherwise it won't generate relative mouse movements below.
2412      */
2413     mouse_event(MOUSEEVENTF_MOVE, -1, -1, 0, 0);
2414     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
2415
2416     msg.message = 0;
2417     mouse_event(MOUSEEVENTF_MOVE, 1, 1, 0, 0);
2418     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2419     ok(msg.hwnd == popup && msg.message == WM_MOUSEMOVE, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2420     /* FIXME: SetCursorPos in Wine generates additional WM_MOUSEMOVE message */
2421     if (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE))
2422         ok(msg.hwnd == popup && msg.message == WM_MOUSEMOVE, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2423     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
2424     ok( !ret, "message %04x available\n", msg.message);
2425
2426     mouse_event(MOUSEEVENTF_MOVE, -1, -1, 0, 0);
2427     ShowWindow(popup, SW_HIDE);
2428     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2429     ok(msg.hwnd == hwnd && msg.message == WM_MOUSEMOVE, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2430     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
2431
2432     mouse_event(MOUSEEVENTF_MOVE, 1, 1, 0, 0);
2433     ShowWindow(hwnd, SW_HIDE);
2434     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
2435     ok( !ret, "message %04x available\n", msg.message);
2436
2437     /* test mouse clicks */
2438
2439     ShowWindow(hwnd, SW_SHOW);
2440     ShowWindow(popup, SW_SHOW);
2441
2442     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
2443
2444     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
2445     mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
2446     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
2447     mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
2448
2449     ok(PeekMessageA(&msg, 0, 0, 0, 0), "no message available\n");
2450     ok(msg.hwnd == popup && msg.message == WM_LBUTTONDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2451     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2452     ok(msg.hwnd == popup && msg.message == WM_LBUTTONDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2453
2454     ok(PeekMessageA(&msg, 0, 0, 0, 0), "no message available\n");
2455     ok(msg.hwnd == popup && msg.message == WM_LBUTTONUP, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2456     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2457     ok(msg.hwnd == popup && msg.message == WM_LBUTTONUP, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2458
2459     ok(PeekMessageA(&msg, 0, 0, 0, 0), "no message available\n");
2460     ok(msg.hwnd == popup && msg.message == WM_LBUTTONDBLCLK, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2461     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2462     ok(msg.hwnd == popup && msg.message == WM_LBUTTONDBLCLK, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2463
2464     ok(PeekMessageA(&msg, 0, 0, 0, 0), "no message available\n");
2465     ok(msg.hwnd == popup && msg.message == WM_LBUTTONUP, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2466     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2467     ok(msg.hwnd == popup && msg.message == WM_LBUTTONUP, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2468
2469     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE);
2470     ok(!ret, "message %04x available\n", msg.message);
2471
2472     ShowWindow(popup, SW_HIDE);
2473     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
2474
2475     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
2476     mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
2477     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
2478     mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
2479
2480     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2481     ok(msg.hwnd == hwnd && msg.message == WM_LBUTTONDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2482     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2483     ok(msg.hwnd == hwnd && msg.message == WM_LBUTTONUP, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2484
2485     test_lbuttondown_flag = TRUE;
2486     SendMessageA(hwnd, WM_COMMAND, (WPARAM)popup, 0);
2487     test_lbuttondown_flag = FALSE;
2488
2489     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2490     ok(msg.hwnd == popup && msg.message == WM_LBUTTONDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2491     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2492     ok(msg.hwnd == popup && msg.message == WM_LBUTTONUP, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2493     ok(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE), "no message available\n");
2494
2495     /* Test WM_MOUSEACTIVATE */
2496 #define TEST_MOUSEACTIVATE(A,B)                                                          \
2497        res = SendMessageA(hwnd, WM_MOUSEACTIVATE, (WPARAM)hwnd, (LPARAM)MAKELRESULT(A,0));   \
2498        ok(res == B, "WM_MOUSEACTIVATE for %s returned %ld\n", #A, res);
2499        
2500     TEST_MOUSEACTIVATE(HTERROR,MA_ACTIVATE);
2501     TEST_MOUSEACTIVATE(HTTRANSPARENT,MA_ACTIVATE);
2502     TEST_MOUSEACTIVATE(HTNOWHERE,MA_ACTIVATE);
2503     TEST_MOUSEACTIVATE(HTCLIENT,MA_ACTIVATE);
2504     TEST_MOUSEACTIVATE(HTCAPTION,MA_ACTIVATE);
2505     TEST_MOUSEACTIVATE(HTSYSMENU,MA_ACTIVATE);
2506     TEST_MOUSEACTIVATE(HTSIZE,MA_ACTIVATE);
2507     TEST_MOUSEACTIVATE(HTMENU,MA_ACTIVATE);
2508     TEST_MOUSEACTIVATE(HTHSCROLL,MA_ACTIVATE);
2509     TEST_MOUSEACTIVATE(HTVSCROLL,MA_ACTIVATE);
2510     TEST_MOUSEACTIVATE(HTMINBUTTON,MA_ACTIVATE);
2511     TEST_MOUSEACTIVATE(HTMAXBUTTON,MA_ACTIVATE);
2512     TEST_MOUSEACTIVATE(HTLEFT,MA_ACTIVATE);
2513     TEST_MOUSEACTIVATE(HTRIGHT,MA_ACTIVATE);
2514     TEST_MOUSEACTIVATE(HTTOP,MA_ACTIVATE);
2515     TEST_MOUSEACTIVATE(HTTOPLEFT,MA_ACTIVATE);
2516     TEST_MOUSEACTIVATE(HTTOPRIGHT,MA_ACTIVATE);
2517     TEST_MOUSEACTIVATE(HTBOTTOM,MA_ACTIVATE);
2518     TEST_MOUSEACTIVATE(HTBOTTOMLEFT,MA_ACTIVATE);
2519     TEST_MOUSEACTIVATE(HTBOTTOMRIGHT,MA_ACTIVATE);
2520     TEST_MOUSEACTIVATE(HTBORDER,MA_ACTIVATE);
2521     TEST_MOUSEACTIVATE(HTOBJECT,MA_ACTIVATE);
2522     TEST_MOUSEACTIVATE(HTCLOSE,MA_ACTIVATE);
2523     TEST_MOUSEACTIVATE(HTHELP,MA_ACTIVATE);
2524
2525     /* Clear any messages left behind by WM_MOUSEACTIVATE tests */
2526     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
2527
2528     DestroyWindow(popup);
2529 }
2530
2531 static void test_validatergn(HWND hwnd)
2532 {
2533     HWND child;
2534     RECT rc, rc2;
2535     HRGN rgn;
2536     int ret;
2537     child = CreateWindowExA(0, "static", NULL, WS_CHILD| WS_VISIBLE, 10, 10, 10, 10, hwnd, 0, 0, NULL);
2538     ShowWindow(hwnd, SW_SHOW);
2539     UpdateWindow( hwnd);
2540     /* test that ValidateRect validates children*/
2541     InvalidateRect( child, NULL, 1);
2542     GetWindowRect( child, &rc);
2543     MapWindowPoints( NULL, hwnd, (POINT*) &rc, 2);
2544     ret = GetUpdateRect( child, &rc2, 0);
2545     ok( rc2.right > rc2.left && rc2.bottom > rc2.top,
2546             "Update rectangle is empty!\n");
2547     ValidateRect( hwnd, &rc);
2548     ret = GetUpdateRect( child, &rc2, 0);
2549     ok( rc2.left == 0 && rc2.top == 0 && rc2.right == 0 && rc2.bottom == 0,
2550             "Update rectangle %ld,%ld-%ld,%ld is not empty!\n", rc2.left, rc2.top,
2551             rc2.right, rc2.bottom);
2552
2553     /* now test ValidateRgn */
2554     InvalidateRect( child, NULL, 1);
2555     GetWindowRect( child, &rc);
2556     MapWindowPoints( NULL, hwnd, (POINT*) &rc, 2);
2557     rgn = CreateRectRgnIndirect( &rc);
2558     ValidateRgn( hwnd, rgn);
2559     ret = GetUpdateRect( child, &rc2, 0);
2560     ok( rc2.left == 0 && rc2.top == 0 && rc2.right == 0 && rc2.bottom == 0,
2561             "Update rectangle %ld,%ld-%ld,%ld is not empty!\n", rc2.left, rc2.top,
2562             rc2.right, rc2.bottom);
2563
2564     DeleteObject( rgn);
2565     DestroyWindow( child );
2566 }
2567
2568 static void nccalchelper(HWND hwnd, INT x, INT y, RECT *prc)
2569 {
2570     MoveWindow( hwnd, 0, 0, x, y, 0);
2571     GetWindowRect( hwnd, prc);
2572     trace("window rect is %ld,%ld - %ld,%ld\n", 
2573             prc->left,prc->top,prc->right,prc->bottom);
2574     DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)prc);
2575     trace("nccalc rect is %ld,%ld - %ld,%ld\n",
2576             prc->left,prc->top,prc->right,prc->bottom);
2577 }
2578
2579 static void test_nccalcscroll(HWND parent)
2580 {
2581     RECT rc1;
2582     INT sbheight = GetSystemMetrics( SM_CYHSCROLL);
2583     INT sbwidth = GetSystemMetrics( SM_CXVSCROLL);
2584     HWND hwnd = CreateWindowExA(0, "static", NULL, 
2585             WS_CHILD| WS_VISIBLE | WS_VSCROLL | WS_HSCROLL , 
2586             10, 10, 200, 200, parent, 0, 0, NULL); 
2587     ShowWindow( parent, SW_SHOW);
2588     UpdateWindow( parent);
2589
2590     /* test window too low for a horizontal scroll bar */
2591     nccalchelper( hwnd, 100, sbheight, &rc1);
2592     ok( rc1.bottom - rc1.top == sbheight, "Height should be %d size is %ld,%ld - %ld,%ld\n", 
2593             sbheight, rc1.left, rc1.top, rc1.right, rc1.bottom);
2594
2595     /* test window just high enough for a horizontal scroll bar */
2596     nccalchelper( hwnd, 100, sbheight + 1, &rc1);
2597     ok( rc1.bottom - rc1.top == 1, "Height should be %d size is %ld,%ld - %ld,%ld\n", 
2598             1, rc1.left, rc1.top, rc1.right, rc1.bottom);
2599
2600     /* test window too narrow for a vertical scroll bar */
2601     nccalchelper( hwnd, sbwidth - 1, 100, &rc1);
2602     ok( rc1.right - rc1.left == sbwidth - 1 , "Width should be %d size is %ld,%ld - %ld,%ld\n", 
2603             sbwidth - 1, rc1.left, rc1.top, rc1.right, rc1.bottom);
2604
2605     /* test window just wide enough for a vertical scroll bar */
2606     nccalchelper( hwnd, sbwidth, 100, &rc1);
2607     ok( rc1.right - rc1.left == 0, "Width should be %d size is %ld,%ld - %ld,%ld\n", 
2608             0, rc1.left, rc1.top, rc1.right, rc1.bottom);
2609
2610     /* same test, but with client edge: not enough width */
2611     SetWindowLong( hwnd, GWL_EXSTYLE, WS_EX_CLIENTEDGE | GetWindowLong( hwnd, GWL_EXSTYLE));
2612     nccalchelper( hwnd, sbwidth, 100, &rc1);
2613     ok( rc1.right - rc1.left == sbwidth - 2 * GetSystemMetrics(SM_CXEDGE),
2614             "Width should be %d size is %ld,%ld - %ld,%ld\n", 
2615             sbwidth - 2 * GetSystemMetrics(SM_CXEDGE), rc1.left, rc1.top, rc1.right, rc1.bottom);
2616
2617     DestroyWindow( hwnd);
2618 }
2619
2620 static void test_SetParent(void)
2621 {
2622     BOOL ret;
2623     HWND desktop = GetDesktopWindow();
2624     HMENU hMenu;
2625     BOOL is_win9x = GetWindowLongPtrW(desktop, GWLP_WNDPROC) == 0;
2626     HWND parent, child1, child2, child3, child4, sibling;
2627
2628     parent = CreateWindowExA(0, "static", NULL, WS_OVERLAPPEDWINDOW,
2629                              100, 100, 200, 200, 0, 0, 0, NULL);
2630     assert(parent != 0);
2631     child1 = CreateWindowExA(0, "static", NULL, WS_CHILD,
2632                              0, 0, 50, 50, parent, 0, 0, NULL);
2633     assert(child1 != 0);
2634     child2 = CreateWindowExA(0, "static", NULL, WS_POPUP,
2635                              0, 0, 50, 50, child1, 0, 0, NULL);
2636     assert(child2 != 0);
2637     child3 = CreateWindowExA(0, "static", NULL, WS_CHILD,
2638                              0, 0, 50, 50, child2, 0, 0, NULL);
2639     assert(child3 != 0);
2640     child4 = CreateWindowExA(0, "static", NULL, WS_POPUP,
2641                              0, 0, 50, 50, child3, 0, 0, NULL);
2642     assert(child4 != 0);
2643
2644     trace("parent %p, child1 %p, child2 %p, child3 %p, child4 %p\n",
2645            parent, child1, child2, child3, child4);
2646
2647     check_parents(parent, desktop, 0, 0, 0, parent, parent);
2648     check_parents(child1, parent, parent, parent, 0, parent, parent);
2649     check_parents(child2, desktop, parent, parent, parent, child2, parent);
2650     check_parents(child3, child2, child2, child2, 0, child2, parent);
2651     check_parents(child4, desktop, child2, child2, child2, child4, parent);
2652
2653 todo_wine {
2654     ok(!IsChild(desktop, parent), "wrong parent/child %p/%p\n", desktop, parent);
2655     ok(!IsChild(desktop, child1), "wrong parent/child %p/%p\n", desktop, child1);
2656     ok(!IsChild(desktop, child2), "wrong parent/child %p/%p\n", desktop, child2);
2657     ok(!IsChild(desktop, child3), "wrong parent/child %p/%p\n", desktop, child3);
2658     ok(!IsChild(desktop, child4), "wrong parent/child %p/%p\n", desktop, child4);
2659 }
2660
2661     ok(IsChild(parent, child1), "wrong parent/child %p/%p\n", parent, child1);
2662 todo_wine {
2663     ok(!IsChild(desktop, child2), "wrong parent/child %p/%p\n", desktop, child2);
2664 }
2665     ok(!IsChild(parent, child2), "wrong parent/child %p/%p\n", parent, child2);
2666     ok(!IsChild(child1, child2), "wrong parent/child %p/%p\n", child1, child2);
2667     ok(!IsChild(parent, child3), "wrong parent/child %p/%p\n", parent, child3);
2668     ok(IsChild(child2, child3), "wrong parent/child %p/%p\n", child2, child3);
2669     ok(!IsChild(parent, child4), "wrong parent/child %p/%p\n", parent, child4);
2670     ok(!IsChild(child3, child4), "wrong parent/child %p/%p\n", child3, child4);
2671 todo_wine {
2672     ok(!IsChild(desktop, child4), "wrong parent/child %p/%p\n", desktop, child4);
2673 }
2674
2675     if (!is_win9x) /* Win9x doesn't survive this test */
2676     {
2677         ok(!SetParent(parent, child1), "SetParent should fail\n");
2678         ok(!SetParent(child2, child3), "SetParent should fail\n");
2679         ok(SetParent(child1, parent) != 0, "SetParent should not fail\n");
2680         ok(SetParent(parent, child2) != 0, "SetParent should not fail\n");
2681         ok(SetParent(parent, child3) != 0, "SetParent should not fail\n");
2682         ok(!SetParent(child2, parent), "SetParent should fail\n");
2683         ok(SetParent(parent, child4) != 0, "SetParent should not fail\n");
2684
2685         check_parents(parent, child4, child4, 0, 0, child4, parent);
2686         check_parents(child1, parent, parent, parent, 0, child4, parent);
2687         check_parents(child2, desktop, parent, parent, parent, child2, parent);
2688         check_parents(child3, child2, child2, child2, 0, child2, parent);
2689         check_parents(child4, desktop, child2, child2, child2, child4, parent);
2690     }
2691
2692     hMenu = CreateMenu();
2693     sibling = CreateWindowExA(0, "static", NULL, WS_OVERLAPPEDWINDOW,
2694                              100, 100, 200, 200, 0, hMenu, 0, NULL);
2695     assert(sibling != 0);
2696
2697     ok(SetParent(sibling, parent) != 0, "SetParent should not fail\n");
2698     ok(GetMenu(sibling) == hMenu, "SetParent should not remove menu\n");
2699
2700     ret = DestroyWindow(parent);
2701     ok( ret, "DestroyWindow() error %ld\n", GetLastError());
2702
2703     ok(!IsWindow(parent), "parent still exists\n");
2704     ok(!IsWindow(sibling), "sibling still exists\n");
2705     ok(!IsWindow(child1), "child1 still exists\n");
2706     ok(!IsWindow(child2), "child2 still exists\n");
2707     ok(!IsWindow(child3), "child3 still exists\n");
2708     ok(!IsWindow(child4), "child4 still exists\n");
2709 }
2710
2711 static LRESULT WINAPI StyleCheckProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
2712 {
2713     LPCREATESTRUCT lpcs;
2714     LPSTYLESTRUCT lpss;
2715
2716     switch (msg)
2717     {
2718     case WM_NCCREATE:
2719     case WM_CREATE:
2720         lpcs = (LPCREATESTRUCT)lparam;
2721         lpss = (LPSTYLESTRUCT)lpcs->lpCreateParams;
2722         if (lpss)
2723         {
2724             if ((lpcs->dwExStyle & WS_EX_DLGMODALFRAME) ||
2725                 ((!(lpcs->dwExStyle & WS_EX_STATICEDGE)) &&
2726                     (lpcs->style & (WS_DLGFRAME | WS_THICKFRAME))))
2727                 ok(lpcs->dwExStyle & WS_EX_WINDOWEDGE, "Window should have WS_EX_WINDOWEDGE style\n");
2728             else
2729                 ok(!(lpcs->dwExStyle & WS_EX_WINDOWEDGE), "Window shouldn't have WS_EX_WINDOWEDGE style\n");
2730
2731             ok((lpss->styleOld & ~WS_EX_WINDOWEDGE) == (lpcs->dwExStyle & ~WS_EX_WINDOWEDGE),
2732                 "Ex style (0x%08lx) should match what the caller passed to CreateWindowEx (0x%08lx)\n",
2733                 (lpss->styleOld & ~WS_EX_WINDOWEDGE), (lpcs->dwExStyle & ~WS_EX_WINDOWEDGE));
2734
2735             ok(lpss->styleNew == lpcs->style,
2736                 "Style (0x%08lx) should match what the caller passed to CreateWindowEx (0x%08lx)\n",
2737                 lpss->styleNew, lpcs->style);
2738         }
2739         break;
2740     }
2741     return DefWindowProc(hwnd, msg, wparam, lparam);
2742 }
2743
2744 static ATOM atomStyleCheckClass;
2745
2746 static void register_style_check_class(void)
2747 {
2748     WNDCLASS wc =
2749     {
2750         0,
2751         StyleCheckProc,
2752         0,
2753         0,
2754         GetModuleHandle(NULL),
2755         NULL,
2756         LoadCursor(NULL, IDC_ARROW),
2757         (HBRUSH)(COLOR_BTNFACE+1),
2758         NULL,
2759         TEXT("WineStyleCheck"),
2760     };
2761     
2762     atomStyleCheckClass = RegisterClass(&wc);
2763 }
2764
2765 static void check_window_style(DWORD dwStyleIn, DWORD dwExStyleIn, DWORD dwStyleOut, DWORD dwExStyleOut)
2766 {
2767     DWORD dwActualStyle;
2768     DWORD dwActualExStyle;
2769     STYLESTRUCT ss;
2770     HWND hwnd;
2771     HWND hwndParent = NULL;
2772     MSG msg;
2773
2774     ss.styleNew = dwStyleIn;
2775     ss.styleOld = dwExStyleIn;
2776
2777     if (dwStyleIn & WS_CHILD)
2778     {
2779         hwndParent = CreateWindowEx(0, MAKEINTATOM(atomStyleCheckClass), NULL,
2780             WS_OVERLAPPEDWINDOW, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
2781     }
2782
2783     hwnd = CreateWindowEx(dwExStyleIn, MAKEINTATOM(atomStyleCheckClass), NULL,
2784                     dwStyleIn, 0, 0, 0, 0, hwndParent, NULL, NULL, &ss);
2785     assert(hwnd);
2786
2787     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
2788     {
2789         TranslateMessage(&msg);
2790         DispatchMessage(&msg);
2791     }
2792
2793     dwActualStyle = GetWindowLong(hwnd, GWL_STYLE);
2794     dwActualExStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
2795     ok((dwActualStyle == dwStyleOut) && (dwActualExStyle == dwExStyleOut),
2796         "Style (0x%08lx) should really be 0x%08lx and/or Ex style (0x%08lx) should really be 0x%08lx\n",
2797         dwActualStyle, dwStyleOut, dwActualExStyle, dwExStyleOut);
2798
2799     DestroyWindow(hwnd);
2800     if (hwndParent) DestroyWindow(hwndParent);
2801 }
2802
2803 /* tests what window styles the window manager automatically adds */
2804 static void test_window_styles(void)
2805 {
2806     register_style_check_class();
2807
2808     check_window_style(0, 0, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_WINDOWEDGE);
2809     check_window_style(WS_OVERLAPPEDWINDOW, 0, WS_CLIPSIBLINGS|WS_OVERLAPPEDWINDOW, WS_EX_WINDOWEDGE);
2810     check_window_style(WS_CHILD, 0, WS_CHILD, 0);
2811     check_window_style(WS_CHILD, WS_EX_WINDOWEDGE, WS_CHILD, 0);
2812     check_window_style(0, WS_EX_TOOLWINDOW, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_WINDOWEDGE|WS_EX_TOOLWINDOW);
2813     check_window_style(WS_POPUP, 0, WS_POPUP|WS_CLIPSIBLINGS, 0);
2814     check_window_style(WS_POPUP, WS_EX_WINDOWEDGE, WS_POPUP|WS_CLIPSIBLINGS, 0);
2815     check_window_style(WS_CHILD, WS_EX_DLGMODALFRAME, WS_CHILD, WS_EX_WINDOWEDGE|WS_EX_DLGMODALFRAME);
2816     check_window_style(WS_CHILD, WS_EX_DLGMODALFRAME|WS_EX_STATICEDGE, WS_CHILD, WS_EX_STATICEDGE|WS_EX_WINDOWEDGE|WS_EX_DLGMODALFRAME);
2817     check_window_style(WS_CAPTION, WS_EX_STATICEDGE, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_STATICEDGE|WS_EX_WINDOWEDGE);
2818     check_window_style(0, WS_EX_APPWINDOW, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_APPWINDOW|WS_EX_WINDOWEDGE);
2819 }
2820
2821 static void test_scrollvalidate( HWND parent)
2822 {
2823     HDC hdc;
2824     HRGN hrgn=CreateRectRgn(0,0,0,0);
2825     HRGN exprgn, tmprgn, clipping;
2826     RECT rc, rcu, cliprc;
2827     /* create two overlapping child windows. The visual region
2828      * of hwnd1 is clipped by the overlapping part of
2829      * hwnd2 because of the WS_CLIPSIBLING style */
2830     HWND hwnd1, hwnd2;
2831
2832     clipping = CreateRectRgn(0,0,0,0);
2833     tmprgn = CreateRectRgn(0,0,0,0);
2834     exprgn = CreateRectRgn(0,0,0,0);
2835     hwnd2 = CreateWindowExA(0, "static", NULL,
2836             WS_CHILD| WS_VISIBLE | WS_CLIPSIBLINGS | WS_BORDER ,
2837             75, 30, 100, 100, parent, 0, 0, NULL);
2838     hwnd1 = CreateWindowExA(0, "static", NULL,
2839             WS_CHILD| WS_VISIBLE | WS_CLIPSIBLINGS | WS_BORDER ,
2840             25, 50, 100, 100, parent, 0, 0, NULL);
2841     ShowWindow( parent, SW_SHOW);
2842     UpdateWindow( parent);
2843     GetClientRect( hwnd1, &rc);
2844     cliprc=rc; 
2845     SetRectRgn( clipping, 10, 10, 90, 90);
2846     hdc = GetDC( hwnd1);
2847     /* for a visual touch */
2848     TextOut( hdc, 0,10, "0123456789", 10);
2849     ScrollDC( hdc, -10, -5, &rc, &cliprc, hrgn, &rcu);
2850     if (winetest_debug > 0) dump_region(hrgn);
2851     /* create a region with what is expected */
2852     SetRectRgn( exprgn, 39,0,49,74);
2853     SetRectRgn( tmprgn, 88,79,98,93);
2854     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
2855     SetRectRgn( tmprgn, 0,93,98,98);
2856     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
2857     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
2858     trace("update rect is %ld,%ld - %ld,%ld\n",
2859             rcu.left,rcu.top,rcu.right,rcu.bottom);
2860     /* now with clipping region */
2861     SelectClipRgn( hdc, clipping);
2862     ScrollDC( hdc, -10, -5, &rc, &cliprc, hrgn, &rcu);
2863     if (winetest_debug > 0) dump_region(hrgn);
2864     /* create a region with what is expected */
2865     SetRectRgn( exprgn, 39,10,49,74);
2866     SetRectRgn( tmprgn, 80,79,90,85);
2867     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
2868     SetRectRgn( tmprgn, 10,85,90,90);
2869     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
2870     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
2871     trace("update rect is %ld,%ld - %ld,%ld\n",
2872             rcu.left,rcu.top,rcu.right,rcu.bottom);
2873     ReleaseDC( hwnd1, hdc);
2874
2875     /* test scrolling a window with an update region */
2876     DestroyWindow( hwnd2);
2877     ValidateRect( hwnd1, NULL);
2878     SetRect( &rc, 40,40, 50,50);
2879     InvalidateRect( hwnd1, &rc, 1);
2880     GetClientRect( hwnd1, &rc);
2881     cliprc=rc;
2882     ScrollWindowEx( hwnd1, -10, 0, &rc, &cliprc, hrgn, &rcu,
2883       SW_SCROLLCHILDREN | SW_INVALIDATE);
2884     if (winetest_debug > 0) dump_region(hrgn);
2885     SetRectRgn( exprgn, 88,0,98,98);
2886     SetRectRgn( tmprgn, 30, 40, 50, 50);
2887     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
2888     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
2889
2890     /* now test ScrollWindowEx with a combination of
2891      * WS_CLIPCHILDREN style and SW_SCROLLCHILDREN flag */
2892     /* make hwnd2 the child of hwnd1 */
2893     hwnd2 = CreateWindowExA(0, "static", NULL,
2894             WS_CHILD| WS_VISIBLE | WS_BORDER ,
2895             50, 50, 100, 100, hwnd1, 0, 0, NULL);
2896     SetWindowLong( hwnd1, GWL_STYLE, GetWindowLong( hwnd1, GWL_STYLE) & ~WS_CLIPSIBLINGS);
2897     GetClientRect( hwnd1, &rc);
2898     cliprc=rc;
2899
2900     /* WS_CLIPCHILDREN and SW_SCROLLCHILDREN */
2901     SetWindowLong( hwnd1, GWL_STYLE, GetWindowLong( hwnd1, GWL_STYLE) | WS_CLIPCHILDREN );
2902     ValidateRect( hwnd1, NULL);
2903     ValidateRect( hwnd2, NULL);
2904     ScrollWindowEx( hwnd1, -10, -10, &rc, &cliprc, hrgn, &rcu,
2905       SW_SCROLLCHILDREN | SW_INVALIDATE);
2906     if (winetest_debug > 0) dump_region(hrgn);
2907     SetRectRgn( exprgn, 88,0,98,88);
2908     SetRectRgn( tmprgn, 0,88,98,98);
2909     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
2910     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
2911
2912     /* SW_SCROLLCHILDREN */
2913     SetWindowLong( hwnd1, GWL_STYLE, GetWindowLong( hwnd1, GWL_STYLE) & ~WS_CLIPCHILDREN );
2914     ValidateRect( hwnd1, NULL);
2915     ValidateRect( hwnd2, NULL);
2916     ScrollWindowEx( hwnd1, -10, -10, &rc, &cliprc, hrgn, &rcu, SW_SCROLLCHILDREN | SW_INVALIDATE);
2917     if (winetest_debug > 0) dump_region(hrgn);
2918     /* expected region is the same as in previous test */
2919     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
2920
2921     /* no SW_SCROLLCHILDREN */
2922     SetWindowLong( hwnd1, GWL_STYLE, GetWindowLong( hwnd1, GWL_STYLE) & ~WS_CLIPCHILDREN );
2923     ValidateRect( hwnd1, NULL);
2924     ValidateRect( hwnd2, NULL);
2925     ScrollWindowEx( hwnd1, -10, -10, &rc, &cliprc, hrgn, &rcu, SW_INVALIDATE);
2926     if (winetest_debug > 0) dump_region(hrgn);
2927     /* expected region is the same as in previous test */
2928     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
2929
2930     /* WS_CLIPCHILDREN and no SW_SCROLLCHILDREN */
2931     SetWindowLong( hwnd1, GWL_STYLE, GetWindowLong( hwnd1, GWL_STYLE) | WS_CLIPCHILDREN );
2932     ValidateRect( hwnd1, NULL);
2933     ValidateRect( hwnd2, NULL);
2934     ScrollWindowEx( hwnd1, -10, -10, &rc, &cliprc, hrgn, &rcu, SW_INVALIDATE);
2935     if (winetest_debug > 0) dump_region(hrgn);
2936     SetRectRgn( exprgn, 88,0,98,20);
2937     SetRectRgn( tmprgn, 20,20,98,30);
2938     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
2939     SetRectRgn( tmprgn, 20,30,30,88);
2940     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
2941     SetRectRgn( tmprgn, 0,88,30,98);
2942     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
2943     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
2944
2945     /* clean up */
2946     DeleteObject( hrgn);
2947     DeleteObject( exprgn);
2948     DeleteObject( tmprgn);
2949     DestroyWindow( hwnd1);
2950     DestroyWindow( hwnd2);
2951 }
2952
2953 /* couple of tests of return values of scrollbar functions
2954  * called on a scrollbarless window */ 
2955 static void test_scroll(void)
2956 {
2957     BOOL ret;
2958     INT min, max;
2959     SCROLLINFO si;
2960     HWND hwnd = CreateWindowExA(0, "Static", "Wine test window",
2961         WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_POPUP,
2962         100, 100, 200, 200, 0, 0, 0, NULL);
2963     /* horizontal */
2964     ret = GetScrollRange( hwnd, SB_HORZ, &min, &max);
2965     ok( ret, "GetScrollRange returns FALSE\n");
2966     ok( min == 0, "minimum scroll pos is %d (should be zero)\n", min);
2967     ok( max == 0, "maximum scroll pos is %d (should be zero)\n", min);
2968     si.cbSize = sizeof( si);
2969     si.fMask = SIF_PAGE;
2970     si.nPage = 0xdeadbeef;
2971     ret = GetScrollInfo( hwnd, SB_HORZ, &si);
2972     ok( !ret, "GetScrollInfo returns %d (should be zero)\n", ret);
2973     ok( si.nPage == 0xdeadbeef, "unexpected value for nPage is %d\n", si.nPage);
2974     /* vertical */
2975     ret = GetScrollRange( hwnd, SB_VERT, &min, &max);
2976     ok( ret, "GetScrollRange returns FALSE\n");
2977     ok( min == 0, "minimum scroll pos is %d (should be zero)\n", min);
2978     ok( max == 0, "maximum scroll pos is %d (should be zero)\n", min);
2979     si.cbSize = sizeof( si);
2980     si.fMask = SIF_PAGE;
2981     si.nPage = 0xdeadbeef;
2982     ret = GetScrollInfo( hwnd, SB_VERT, &si);
2983     ok( !ret, "GetScrollInfo returns %d (should be zero)\n", ret);
2984     ok( si.nPage == 0xdeadbeef, "unexpected value for nPage is %d\n", si.nPage);
2985     /* clean up */
2986     DestroyWindow( hwnd);
2987 }
2988
2989 static void test_scrolldc( HWND parent)
2990 {
2991     HDC hdc;
2992     HRGN exprgn, tmprgn, hrgn;
2993     RECT rc, rc2, rcu, cliprc;
2994     HWND hwnd1;
2995     COLORREF colr;
2996
2997     hrgn = CreateRectRgn(0,0,0,0);
2998     tmprgn = CreateRectRgn(0,0,0,0);
2999     exprgn = CreateRectRgn(0,0,0,0);
3000
3001     hwnd1 = CreateWindowExA(0, "static", NULL,
3002             WS_CHILD| WS_VISIBLE,
3003             25, 50, 100, 100, parent, 0, 0, NULL);
3004     ShowWindow( parent, SW_SHOW);
3005     UpdateWindow( parent);
3006     GetClientRect( hwnd1, &rc);
3007     hdc = GetDC( hwnd1);
3008     /* paint the upper half of the window black */
3009     rc2 = rc;
3010     rc2.bottom = ( rc.top + rc.bottom) /2;
3011     FillRect( hdc, &rc2, GetStockObject(BLACK_BRUSH));
3012     /* clip region is the lower half */
3013     cliprc=rc; 
3014     cliprc.top = (rc.top + rc.bottom) /2;
3015     /* test whether scrolled pixels are properly clipped */ 
3016     colr = GetPixel( hdc, (rc.left+rc.right)/2, ( rc.top + rc.bottom) /2 - 1);
3017     ok ( colr == 0, "pixel should be black, color is %08lx\n", colr); 
3018     /* this scroll should not cause any visible changes */ 
3019     ScrollDC( hdc, 5, -20, &rc, &cliprc, hrgn, &rcu);
3020     colr = GetPixel( hdc, (rc.left+rc.right)/2, ( rc.top + rc.bottom) /2 - 1);
3021     ok ( colr == 0, "pixel should be black, color is %08lx\n", colr); 
3022     /* test with NULL clip rect */
3023     ScrollDC( hdc, 20, -20, &rc, NULL, hrgn, &rcu);
3024     /*FillRgn(hdc, hrgn, GetStockObject(WHITE_BRUSH));*/
3025     trace("update rect: %ld,%ld - %ld,%ld\n",
3026            rcu.left, rcu.top, rcu.right, rcu.bottom);
3027     if (winetest_debug > 0) dump_region(hrgn);
3028     SetRect(&rc2, 0, 0, 100, 100);
3029     ok(EqualRect(&rcu, &rc2), "rects do not match (%ld,%ld-%ld,%ld) / (%ld,%ld-%ld,%ld)\n",
3030        rcu.left, rcu.top, rcu.right, rcu.bottom, rc2.left, rc2.top, rc2.right, rc2.bottom);
3031
3032     SetRectRgn( exprgn, 0, 0, 20, 80);
3033     SetRectRgn( tmprgn, 0, 80, 100, 100);
3034     CombineRgn(exprgn, exprgn, tmprgn, RGN_OR);
3035     if (winetest_debug > 0) dump_region(exprgn);
3036     ok(EqualRgn(exprgn, hrgn), "wrong update region\n");
3037     /* test clip rect > scroll rect */ 
3038     FillRect( hdc, &rc, GetStockObject(WHITE_BRUSH));
3039     rc2=rc;
3040     InflateRect( &rc2, -(rc.right-rc.left)/4, -(rc.bottom-rc.top)/4);
3041     FillRect( hdc, &rc2, GetStockObject(BLACK_BRUSH));
3042     ScrollDC( hdc, 10, 10, &rc2, &rc, hrgn, &rcu);
3043     SetRectRgn( exprgn, 25, 25, 75, 35);
3044     SetRectRgn( tmprgn, 25, 35, 35, 75);
3045     CombineRgn(exprgn, exprgn, tmprgn, RGN_OR);
3046     ok(EqualRgn(exprgn, hrgn), "wrong update region\n");
3047     colr = GetPixel( hdc, 80, 80);
3048     ok ( colr == 0, "pixel should be black, color is %08lx\n", colr); 
3049     trace("update rect: %ld,%ld - %ld,%ld\n",
3050            rcu.left, rcu.top, rcu.right, rcu.bottom);
3051     if (winetest_debug > 0) dump_region(hrgn);
3052
3053     /* clean up */
3054     DeleteObject(hrgn);
3055     DeleteObject(exprgn);
3056     DeleteObject(tmprgn);
3057     DestroyWindow(hwnd1);
3058 }
3059
3060 static void test_params(void)
3061 {
3062     HWND hwnd;
3063     INT rc;
3064
3065     /* Just a param check */
3066     SetLastError(0xdeadbeef);
3067     rc = GetWindowText(hwndMain2, NULL, 1024);
3068     ok( rc==0, "GetWindowText: rc=%d err=%ld\n",rc,GetLastError());
3069
3070     SetLastError(0xdeadbeef);
3071     hwnd=CreateWindow("LISTBOX", "TestList",
3072                       (LBS_STANDARD & ~LBS_SORT),
3073                       0, 0, 100, 100,
3074                       NULL, (HMENU)1, NULL, 0);
3075
3076     ok(!hwnd, "CreateWindow with invalid menu handle should fail\n");
3077     ok(GetLastError() == ERROR_INVALID_MENU_HANDLE || /* NT */
3078        GetLastError() == 0xdeadbeef, /* Win9x */
3079        "wrong last error value %ld\n", GetLastError());
3080 }
3081
3082 static void test_AWRwindow(LPCSTR class, LONG style, LONG exStyle, BOOL menu)
3083 {
3084     HWND hwnd = 0;
3085
3086     hwnd = CreateWindowEx(exStyle, class, class, style,
3087                           110, 100,
3088                           225, 200,
3089                           0,
3090                           menu ? hmenu : 0,
3091                           0, 0);
3092     if (!hwnd) {
3093         trace("Failed to create window class=%s, style=0x%08lx, exStyle=0x%08lx\n", class, style, exStyle);
3094         return;
3095     }
3096     ShowWindow(hwnd, SW_SHOW);
3097
3098     test_nonclient_area(hwnd);
3099
3100     SetMenu(hwnd, 0);
3101     DestroyWindow(hwnd);
3102 }
3103
3104 static BOOL AWR_init(void)
3105 {
3106     WNDCLASS class;
3107
3108     class.style         = CS_HREDRAW | CS_VREDRAW;
3109     class.lpfnWndProc     = DefWindowProcA;
3110     class.cbClsExtra    = 0;
3111     class.cbWndExtra    = 0;
3112     class.hInstance     = 0;
3113     class.hIcon         = LoadIcon (0, IDI_APPLICATION);
3114     class.hCursor       = LoadCursor (0, IDC_ARROW);
3115     class.hbrBackground = 0;
3116     class.lpszMenuName  = 0;
3117     class.lpszClassName = szAWRClass;
3118     
3119     if (!RegisterClass (&class)) {
3120         ok(FALSE, "RegisterClass failed\n");
3121         return FALSE;
3122     }
3123
3124     hmenu = CreateMenu();
3125     if (!hmenu)
3126         return FALSE;
3127     ok(hmenu != 0, "Failed to create menu\n");
3128     ok(AppendMenu(hmenu, MF_STRING, 1, "Test!"), "Failed to create menu item\n");
3129     
3130     return TRUE;
3131 }
3132
3133
3134 static void test_AWR_window_size(BOOL menu)
3135 {
3136     LONG styles[] = {
3137         WS_POPUP,
3138         WS_MAXIMIZE, WS_BORDER, WS_DLGFRAME, 
3139         WS_SYSMENU, 
3140         WS_THICKFRAME,
3141         WS_MINIMIZEBOX, WS_MAXIMIZEBOX,
3142         WS_HSCROLL, WS_VSCROLL
3143     };
3144     LONG exStyles[] = {
3145         WS_EX_CLIENTEDGE,
3146         WS_EX_TOOLWINDOW, WS_EX_WINDOWEDGE,
3147         WS_EX_APPWINDOW,
3148 #if 0
3149         /* These styles have problems on (at least) WinXP (SP2) and Wine */
3150         WS_EX_DLGMODALFRAME, 
3151         WS_EX_STATICEDGE, 
3152 #endif
3153     };
3154
3155     int i;    
3156
3157     /* A exhaustive check of all the styles takes too long
3158      * so just do a (hopefully representative) sample
3159      */
3160     for (i = 0; i < COUNTOF(styles); ++i)
3161         test_AWRwindow(szAWRClass, styles[i], 0, menu);
3162     for (i = 0; i < COUNTOF(exStyles); ++i) {
3163         test_AWRwindow(szAWRClass, WS_POPUP, exStyles[i], menu);
3164         test_AWRwindow(szAWRClass, WS_THICKFRAME, exStyles[i], menu);
3165     }
3166 }
3167 #undef COUNTOF
3168
3169 #define SHOWSYSMETRIC(SM) trace(#SM "=%d\n", GetSystemMetrics(SM))
3170
3171 static void test_AdjustWindowRect(void)
3172 {
3173     if (!AWR_init())
3174         return;
3175     
3176     SHOWSYSMETRIC(SM_CYCAPTION);
3177     SHOWSYSMETRIC(SM_CYSMCAPTION);
3178     SHOWSYSMETRIC(SM_CYMENU);
3179     SHOWSYSMETRIC(SM_CXEDGE);
3180     SHOWSYSMETRIC(SM_CYEDGE);
3181     SHOWSYSMETRIC(SM_CXVSCROLL);
3182     SHOWSYSMETRIC(SM_CYHSCROLL);
3183     SHOWSYSMETRIC(SM_CXFRAME);
3184     SHOWSYSMETRIC(SM_CYFRAME);
3185     SHOWSYSMETRIC(SM_CXDLGFRAME);
3186     SHOWSYSMETRIC(SM_CYDLGFRAME);
3187     SHOWSYSMETRIC(SM_CXBORDER);
3188     SHOWSYSMETRIC(SM_CYBORDER);  
3189
3190     test_AWR_window_size(FALSE);
3191     test_AWR_window_size(TRUE);
3192
3193     DestroyMenu(hmenu);
3194 }
3195 #undef SHOWSYSMETRIC
3196
3197
3198 /* Global variables to trigger exit from loop */
3199 static int redrawComplete, WMPAINT_count;
3200
3201 static LRESULT WINAPI redraw_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
3202 {
3203     switch (msg)
3204     {
3205     case WM_PAINT:
3206         trace("doing WM_PAINT %d\n", WMPAINT_count);
3207         WMPAINT_count++;
3208         if (WMPAINT_count > 10 && redrawComplete == 0) {
3209             PAINTSTRUCT ps;
3210             BeginPaint(hwnd, &ps);
3211             EndPaint(hwnd, &ps);
3212             return 1;
3213         }
3214         return 0;
3215         break;
3216     }
3217     return DefWindowProc(hwnd, msg, wparam, lparam);
3218 }
3219
3220 /* Ensure we exit from RedrawNow regardless of invalidated area */
3221 static void test_redrawnow(void)
3222 {
3223    WNDCLASSA cls;
3224    HWND hwndMain;
3225
3226    cls.style = CS_DBLCLKS;
3227    cls.lpfnWndProc = redraw_window_procA;
3228    cls.cbClsExtra = 0;
3229    cls.cbWndExtra = 0;
3230    cls.hInstance = GetModuleHandleA(0);
3231    cls.hIcon = 0;
3232    cls.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
3233    cls.hbrBackground = GetStockObject(WHITE_BRUSH);
3234    cls.lpszMenuName = NULL;
3235    cls.lpszClassName = "RedrawWindowClass";
3236
3237    if(!RegisterClassA(&cls)) {
3238        trace("Register failed %ld\n", GetLastError());
3239        return;
3240    }
3241
3242    hwndMain = CreateWindowA("RedrawWindowClass", "Main Window", WS_OVERLAPPEDWINDOW,
3243                             CW_USEDEFAULT, 0, 100, 100, NULL, NULL, 0, NULL);
3244
3245    ok( WMPAINT_count == 0, "Multiple unexpected WM_PAINT calls %d\n", WMPAINT_count);
3246    ShowWindow(hwndMain, SW_SHOW);
3247    ok( WMPAINT_count == 0, "Multiple unexpected WM_PAINT calls %d\n", WMPAINT_count);
3248    RedrawWindow(hwndMain, NULL,NULL,RDW_UPDATENOW | RDW_ALLCHILDREN);
3249    ok( WMPAINT_count == 1, "Multiple unexpected WM_PAINT calls %d\n", WMPAINT_count);
3250    redrawComplete = TRUE;
3251    ok( WMPAINT_count < 10, "RedrawWindow (RDW_UPDATENOW) never completed (%d)\n", WMPAINT_count);
3252
3253    /* clean up */
3254    DestroyWindow( hwndMain);
3255 }
3256
3257 struct parentdc_stat {
3258     RECT client;
3259     RECT clip;
3260     RECT paint;
3261 };
3262
3263 struct parentdc_test {
3264    struct parentdc_stat main, main_todo;
3265    struct parentdc_stat child1, child1_todo;
3266    struct parentdc_stat child2, child2_todo;
3267 };
3268
3269 static LRESULT WINAPI parentdc_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
3270 {
3271     RECT rc;
3272     PAINTSTRUCT ps;
3273
3274     struct parentdc_stat *t = (struct parentdc_stat *)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
3275
3276     switch (msg)
3277     {
3278     case WM_PAINT:
3279         trace("doing WM_PAINT on %p\n", hwnd);
3280         GetClientRect(hwnd, &rc);
3281         CopyRect(&t->client, &rc);
3282         trace("client rect (%ld, %ld)-(%ld, %ld)\n", rc.left, rc.top, rc.right, rc.bottom);
3283         GetWindowRect(hwnd, &rc);
3284         trace("window rect (%ld, %ld)-(%ld, %ld)\n", rc.left, rc.top, rc.right, rc.bottom);
3285         BeginPaint(hwnd, &ps);
3286         CopyRect(&t->paint, &ps.rcPaint);
3287         GetClipBox(ps.hdc, &rc);
3288         CopyRect(&t->clip, &rc);
3289         trace("clip rect (%ld, %ld)-(%ld, %ld)\n", rc.left, rc.top, rc.right, rc.bottom);
3290         trace("paint rect (%ld, %ld)-(%ld, %ld)\n", ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
3291         EndPaint(hwnd, &ps);
3292         return 0;
3293     }
3294     return DefWindowProc(hwnd, msg, wparam, lparam);
3295 }
3296
3297 static void zero_parentdc_stat(struct parentdc_stat *t)
3298 {
3299     SetRectEmpty(&t->client);
3300     SetRectEmpty(&t->clip);
3301     SetRectEmpty(&t->paint);
3302 }
3303
3304 static void zero_parentdc_test(struct parentdc_test *t)
3305 {
3306     zero_parentdc_stat(&t->main);
3307     zero_parentdc_stat(&t->child1);
3308     zero_parentdc_stat(&t->child2);
3309 }
3310
3311 #define parentdc_field_ok(t, w, r, f, got) \
3312   ok (t.w.r.f==got.w.r.f, "window " #w ", rect " #r ", field " #f \
3313       ": expected %ld, got %ld\n", \
3314       t.w.r.f, got.w.r.f)
3315
3316 #define parentdc_todo_field_ok(t, w, r, f, got) \
3317   if (t.w##_todo.r.f) todo_wine { parentdc_field_ok(t, w, r, f, got); } \
3318   else parentdc_field_ok(t, w, r, f, got)
3319
3320 #define parentdc_rect_ok(t, w, r, got) \
3321   parentdc_todo_field_ok(t, w, r, left, got); \
3322   parentdc_todo_field_ok(t, w, r, top, got); \
3323   parentdc_todo_field_ok(t, w, r, right, got); \
3324   parentdc_todo_field_ok(t, w, r, bottom, got);
3325
3326 #define parentdc_win_ok(t, w, got) \
3327   parentdc_rect_ok(t, w, client, got); \
3328   parentdc_rect_ok(t, w, clip, got); \
3329   parentdc_rect_ok(t, w, paint, got);
3330
3331 #define parentdc_ok(t, got) \
3332   parentdc_win_ok(t, main, got); \
3333   parentdc_win_ok(t, child1, got); \
3334   parentdc_win_ok(t, child2, got);
3335
3336 static void test_csparentdc(void)
3337 {
3338    WNDCLASSA clsMain, cls;
3339    HWND hwndMain, hwnd1, hwnd2;
3340    MSG msg;
3341    RECT rc;
3342
3343    struct parentdc_test test_answer;
3344
3345 #define nothing_todo {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}
3346    const struct parentdc_test test1 = 
3347    {
3348         {{0, 0, 150, 150}, {0, 0, 150, 150}, {0, 0, 150, 150}}, nothing_todo,
3349         {{0, 0, 40, 40}, {-20, -20, 130, 130}, {0, 0, 40, 40}}, {{0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}},
3350         {{0, 0, 40, 40}, {-40, -40, 110, 110}, {0, 0, 40, 40}}, {{0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}},
3351    };
3352
3353    const struct parentdc_test test2 = 
3354    {
3355         {{0, 0, 150, 150}, {0, 0, 50, 50}, {0, 0, 50, 50}}, nothing_todo,
3356         {{0, 0, 40, 40}, {-20, -20, 30, 30}, {0, 0, 30, 30}}, {{0, 0, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}},
3357         {{0, 0, 40, 40}, {-40, -40, 10, 10}, {0, 0, 10, 10}}, {{0, 0, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}},
3358    };
3359
3360    const struct parentdc_test test3 = 
3361    {
3362         {{0, 0, 150, 150}, {0, 0, 10, 10}, {0, 0, 10, 10}}, nothing_todo,
3363         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
3364         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
3365    };
3366
3367    const struct parentdc_test test4 = 
3368    {
3369         {{0, 0, 150, 150}, {40, 40, 50, 50}, {40, 40, 50, 50}}, nothing_todo,
3370         {{0, 0, 40, 40}, {20, 20, 30, 30}, {20, 20, 30, 30}}, nothing_todo,
3371         {{0, 0, 40, 40}, {0, 0, 10, 10}, {0, 0, 10, 10}}, nothing_todo,
3372    };
3373
3374    const struct parentdc_test test5 = 
3375    {
3376         {{0, 0, 150, 150}, {20, 20, 60, 60}, {20, 20, 60, 60}}, nothing_todo,
3377         {{0, 0, 40, 40}, {-20, -20, 130, 130}, {0, 0, 40, 40}}, {{0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}},
3378         {{0, 0, 40, 40}, {-20, -20, 20, 20}, {0, 0, 20, 20}}, {{0, 0, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}},
3379    };
3380
3381    const struct parentdc_test test6 = 
3382    {
3383         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
3384         {{0, 0, 40, 40}, {0, 0, 10, 10}, {0, 0, 10, 10}}, nothing_todo,
3385         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
3386    };
3387
3388    const struct parentdc_test test7 = 
3389    {
3390         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
3391         {{0, 0, 40, 40}, {-20, -20, 130, 130}, {0, 0, 40, 40}}, {{0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}},
3392         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
3393    };
3394 #undef nothing_todo
3395
3396    clsMain.style = CS_DBLCLKS;
3397    clsMain.lpfnWndProc = parentdc_window_procA;
3398    clsMain.cbClsExtra = 0;
3399    clsMain.cbWndExtra = 0;
3400    clsMain.hInstance = GetModuleHandleA(0);
3401    clsMain.hIcon = 0;
3402    clsMain.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
3403    clsMain.hbrBackground = GetStockObject(WHITE_BRUSH);
3404    clsMain.lpszMenuName = NULL;
3405    clsMain.lpszClassName = "ParentDcMainWindowClass";
3406
3407    if(!RegisterClassA(&clsMain)) {
3408        trace("Register failed %ld\n", GetLastError());
3409        return;
3410    }
3411
3412    cls.style = CS_DBLCLKS | CS_PARENTDC;
3413    cls.lpfnWndProc = parentdc_window_procA;
3414    cls.cbClsExtra = 0;
3415    cls.cbWndExtra = 0;
3416    cls.hInstance = GetModuleHandleA(0);
3417    cls.hIcon = 0;
3418    cls.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
3419    cls.hbrBackground = GetStockObject(WHITE_BRUSH);
3420    cls.lpszMenuName = NULL;
3421    cls.lpszClassName = "ParentDcWindowClass";
3422
3423    if(!RegisterClassA(&cls)) {
3424        trace("Register failed %ld\n", GetLastError());
3425        return;
3426    }
3427
3428    SetRect(&rc, 0, 0, 150, 150);
3429    AdjustWindowRectEx(&rc, WS_OVERLAPPEDWINDOW, FALSE, 0);
3430    hwndMain = CreateWindowA("ParentDcMainWindowClass", "Main Window", WS_OVERLAPPEDWINDOW,
3431                             CW_USEDEFAULT, 0, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, 0, NULL);
3432    SetWindowLongPtrA(hwndMain, GWLP_USERDATA, (DWORD_PTR)&test_answer.main);
3433    hwnd1 = CreateWindowA("ParentDcWindowClass", "Child Window 1", WS_CHILD,
3434                             20, 20, 40, 40, hwndMain, NULL, 0, NULL);
3435    SetWindowLongPtrA(hwnd1, GWLP_USERDATA, (DWORD_PTR)&test_answer.child1);
3436    hwnd2 = CreateWindowA("ParentDcWindowClass", "Child Window 2", WS_CHILD,
3437                             40, 40, 40, 40, hwndMain, NULL, 0, NULL);
3438    SetWindowLongPtrA(hwnd2, GWLP_USERDATA, (DWORD_PTR)&test_answer.child2);
3439    ShowWindow(hwndMain, SW_SHOW);
3440    ShowWindow(hwnd1, SW_SHOW);
3441    ShowWindow(hwnd2, SW_SHOW);
3442    flush_events();
3443
3444    zero_parentdc_test(&test_answer);
3445    InvalidateRect(hwndMain, NULL, TRUE);
3446    while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
3447    parentdc_ok(test1, test_answer);
3448
3449    zero_parentdc_test(&test_answer);
3450    SetRect(&rc, 0, 0, 50, 50);
3451    InvalidateRect(hwndMain, &rc, TRUE);
3452    while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
3453    parentdc_ok(test2, test_answer);
3454
3455    zero_parentdc_test(&test_answer);
3456    SetRect(&rc, 0, 0, 10, 10);
3457    InvalidateRect(hwndMain, &rc, TRUE);
3458    while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
3459    parentdc_ok(test3, test_answer);
3460
3461    zero_parentdc_test(&test_answer);
3462    SetRect(&rc, 40, 40, 50, 50);
3463    InvalidateRect(hwndMain, &rc, TRUE);
3464    while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
3465    parentdc_ok(test4, test_answer);
3466
3467    zero_parentdc_test(&test_answer);
3468    SetRect(&rc, 20, 20, 60, 60);
3469    InvalidateRect(hwndMain, &rc, TRUE);
3470    while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
3471    parentdc_ok(test5, test_answer);
3472
3473    zero_parentdc_test(&test_answer);
3474    SetRect(&rc, 0, 0, 10, 10);
3475    InvalidateRect(hwnd1, &rc, TRUE);
3476    while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
3477    parentdc_ok(test6, test_answer);
3478
3479    zero_parentdc_test(&test_answer);
3480    SetRect(&rc, -5, -5, 65, 65);
3481    InvalidateRect(hwnd1, &rc, TRUE);
3482    while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
3483    parentdc_ok(test7, test_answer);
3484
3485    DestroyWindow(hwndMain);
3486    DestroyWindow(hwnd1);
3487    DestroyWindow(hwnd2);
3488 }
3489
3490 static void test_IsWindowUnicode(void)
3491 {
3492     static const char ansi_class_nameA[] = "ansi class name";
3493     static const WCHAR ansi_class_nameW[] = {'a','n','s','i',' ','c','l','a','s','s',' ','n','a','m','e',0};
3494     static const char unicode_class_nameA[] = "unicode class name";
3495     static const WCHAR unicode_class_nameW[] = {'u','n','i','c','o','d','e',' ','c','l','a','s','s',' ','n','a','m','e',0};
3496     WNDCLASSA classA;
3497     WNDCLASSW classW;
3498     HWND hwnd;
3499
3500     memset(&classW, 0, sizeof(classW));
3501     classW.hInstance = GetModuleHandleA(0);
3502     classW.lpfnWndProc = DefWindowProcW;
3503     classW.lpszClassName = unicode_class_nameW;
3504     if (!RegisterClassW(&classW)) return;
3505
3506     memset(&classA, 0, sizeof(classA));
3507     classA.hInstance = GetModuleHandleA(0);
3508     classA.lpfnWndProc = DefWindowProcA;
3509     classA.lpszClassName = ansi_class_nameA;
3510     assert(RegisterClassA(&classA));
3511
3512     /* unicode class: window proc */
3513     hwnd = CreateWindowExW(0, unicode_class_nameW, NULL, WS_POPUP,
3514                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
3515     assert(hwnd);
3516
3517     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
3518     SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (ULONG_PTR)DefWindowProcA);
3519     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
3520     SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (ULONG_PTR)DefWindowProcW);
3521     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
3522
3523     DestroyWindow(hwnd);
3524
3525     hwnd = CreateWindowExA(0, unicode_class_nameA, NULL, WS_POPUP,
3526                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
3527     assert(hwnd);
3528
3529     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
3530     SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (ULONG_PTR)DefWindowProcA);
3531     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
3532     SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (ULONG_PTR)DefWindowProcW);
3533     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
3534
3535     DestroyWindow(hwnd);
3536
3537     /* ansi class: window proc */
3538     hwnd = CreateWindowExW(0, ansi_class_nameW, NULL, WS_POPUP,
3539                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
3540     assert(hwnd);
3541
3542     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
3543     SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (ULONG_PTR)DefWindowProcW);
3544     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
3545     SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (ULONG_PTR)DefWindowProcA);
3546     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
3547
3548     DestroyWindow(hwnd);
3549
3550     hwnd = CreateWindowExA(0, ansi_class_nameA, NULL, WS_POPUP,
3551                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
3552     assert(hwnd);
3553
3554     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
3555     SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (ULONG_PTR)DefWindowProcW);
3556     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
3557     SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (ULONG_PTR)DefWindowProcA);
3558     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
3559
3560     DestroyWindow(hwnd);
3561
3562     /* unicode class: class proc */
3563     hwnd = CreateWindowExW(0, unicode_class_nameW, NULL, WS_POPUP,
3564                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
3565     assert(hwnd);
3566
3567     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
3568     SetClassLongPtrA(hwnd, GCLP_WNDPROC, (ULONG_PTR)DefWindowProcA);
3569     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
3570     /* do not restore class window proc back to unicode */
3571
3572     DestroyWindow(hwnd);
3573
3574     hwnd = CreateWindowExA(0, unicode_class_nameA, NULL, WS_POPUP,
3575                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
3576     assert(hwnd);
3577
3578     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
3579     SetClassLongPtrW(hwnd, GCLP_WNDPROC, (ULONG_PTR)DefWindowProcW);
3580     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
3581
3582     DestroyWindow(hwnd);
3583
3584     /* ansi class: class proc */
3585     hwnd = CreateWindowExW(0, ansi_class_nameW, NULL, WS_POPUP,
3586                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
3587     assert(hwnd);
3588
3589     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
3590     SetClassLongPtrW(hwnd, GCLP_WNDPROC, (ULONG_PTR)DefWindowProcW);
3591     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
3592     /* do not restore class window proc back to ansi */
3593
3594     DestroyWindow(hwnd);
3595
3596     hwnd = CreateWindowExA(0, ansi_class_nameA, NULL, WS_POPUP,
3597                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
3598     assert(hwnd);
3599
3600     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
3601     SetClassLongPtrA(hwnd, GCLP_WNDPROC, (ULONG_PTR)DefWindowProcA);
3602     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
3603
3604     DestroyWindow(hwnd);
3605 }
3606
3607 static void test_CreateWindow(void)
3608 {
3609     HWND hwnd, parent;
3610     HMENU hmenu;
3611
3612 #define expect_menu(window, menu) \
3613     SetLastError(0xdeadbeef); \
3614     ok(GetMenu(window) == (HMENU)menu, "GetMenu error %ld\n", GetLastError())
3615
3616 #define expect_style(window, style)\
3617     ok(GetWindowLong(window, GWL_STYLE) == (style), "expected style %lx != %lx\n", (long)(style), GetWindowLong(window, GWL_STYLE))
3618
3619 #define expect_ex_style(window, ex_style)\
3620     ok(GetWindowLong(window, GWL_EXSTYLE) == (ex_style), "expected ex_style %lx != %lx\n", (long)(ex_style), GetWindowLong(window, GWL_EXSTYLE))
3621
3622     hmenu = CreateMenu();
3623     assert(hmenu != 0);
3624     parent = GetDesktopWindow();
3625     assert(parent != 0);
3626
3627     SetLastError(0xdeadbeef);
3628     ok(IsMenu(hmenu), "IsMenu error %ld\n", GetLastError());
3629
3630     SetLastError(0xdeadbeef);
3631     hwnd = CreateWindowEx(WS_EX_APPWINDOW, "static", NULL, WS_CHILD,
3632                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
3633     ok(hwnd != 0, "CreateWindowEx error %ld\n", GetLastError());
3634     expect_menu(hwnd, 1);
3635     expect_style(hwnd, WS_CHILD);
3636     expect_ex_style(hwnd, WS_EX_APPWINDOW);
3637     DestroyWindow(hwnd);
3638
3639     SetLastError(0xdeadbeef);
3640     hwnd = CreateWindowEx(WS_EX_APPWINDOW, "static", NULL, WS_CHILD | WS_CAPTION,
3641                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
3642     ok(hwnd != 0, "CreateWindowEx error %ld\n", GetLastError());
3643     expect_menu(hwnd, 1);
3644     expect_style(hwnd, WS_CHILD | WS_CAPTION);
3645     expect_ex_style(hwnd, WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
3646     DestroyWindow(hwnd);
3647
3648     SetLastError(0xdeadbeef);
3649     hwnd = CreateWindowEx(0, "static", NULL, WS_CHILD,
3650                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
3651     ok(hwnd != 0, "CreateWindowEx error %ld\n", GetLastError());
3652     expect_menu(hwnd, 1);
3653     expect_style(hwnd, WS_CHILD);
3654     expect_ex_style(hwnd, 0);
3655     DestroyWindow(hwnd);
3656
3657     SetLastError(0xdeadbeef);
3658     hwnd = CreateWindowEx(0, "static", NULL, WS_CHILD | WS_CAPTION,
3659                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
3660     ok(hwnd != 0, "CreateWindowEx error %ld\n", GetLastError());
3661     expect_menu(hwnd, 1);
3662     expect_style(hwnd, WS_CHILD | WS_CAPTION);
3663     expect_ex_style(hwnd, WS_EX_WINDOWEDGE);
3664     DestroyWindow(hwnd);
3665
3666     SetLastError(0xdeadbeef);
3667     hwnd = CreateWindowEx(WS_EX_APPWINDOW, "static", NULL, WS_POPUP,
3668                            0, 0, 100, 100, parent, hmenu, 0, NULL);
3669     ok(hwnd != 0, "CreateWindowEx error %ld\n", GetLastError());
3670     expect_menu(hwnd, hmenu);
3671     expect_style(hwnd, WS_POPUP | WS_CLIPSIBLINGS);
3672     expect_ex_style(hwnd, WS_EX_APPWINDOW);
3673     DestroyWindow(hwnd);
3674     SetLastError(0xdeadbeef);
3675     ok(!IsMenu(hmenu), "IsMenu should fail\n");
3676     ok(GetLastError() == ERROR_INVALID_MENU_HANDLE, "IsMenu set error %ld\n", GetLastError());
3677
3678     hmenu = CreateMenu();
3679     assert(hmenu != 0);
3680     SetLastError(0xdeadbeef);
3681     hwnd = CreateWindowEx(WS_EX_APPWINDOW, "static", NULL, WS_POPUP | WS_CAPTION,
3682                            0, 0, 100, 100, parent, hmenu, 0, NULL);
3683     ok(hwnd != 0, "CreateWindowEx error %ld\n", GetLastError());
3684     expect_menu(hwnd, hmenu);
3685     expect_style(hwnd, WS_POPUP | WS_CAPTION | WS_CLIPSIBLINGS);
3686     expect_ex_style(hwnd, WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
3687     DestroyWindow(hwnd);
3688     SetLastError(0xdeadbeef);
3689     ok(!IsMenu(hmenu), "IsMenu should fail\n");
3690     ok(GetLastError() == ERROR_INVALID_MENU_HANDLE, "IsMenu set error %ld\n", GetLastError());
3691
3692     hmenu = CreateMenu();
3693     assert(hmenu != 0);
3694     SetLastError(0xdeadbeef);
3695     hwnd = CreateWindowEx(WS_EX_APPWINDOW, "static", NULL, WS_POPUP,
3696                            0, 0, 100, 100, parent, hmenu, 0, NULL);
3697     ok(hwnd != 0, "CreateWindowEx error %ld\n", GetLastError());
3698     expect_menu(hwnd, hmenu);
3699     expect_style(hwnd, WS_POPUP | WS_CLIPSIBLINGS);
3700     expect_ex_style(hwnd, WS_EX_APPWINDOW);
3701     DestroyWindow(hwnd);
3702     SetLastError(0xdeadbeef);
3703     ok(!IsMenu(hmenu), "IsMenu should fail\n");
3704     ok(GetLastError() == ERROR_INVALID_MENU_HANDLE, "IsMenu set error %ld\n", GetLastError());
3705
3706     hmenu = CreateMenu();
3707     assert(hmenu != 0);
3708     SetLastError(0xdeadbeef);
3709     hwnd = CreateWindowEx(0, "static", NULL, WS_POPUP | WS_CAPTION,
3710                            0, 0, 100, 100, parent, hmenu, 0, NULL);
3711     ok(hwnd != 0, "CreateWindowEx error %ld\n", GetLastError());
3712     expect_menu(hwnd, hmenu);
3713     expect_style(hwnd, WS_POPUP | WS_CAPTION | WS_CLIPSIBLINGS);
3714     expect_ex_style(hwnd, WS_EX_WINDOWEDGE);
3715     DestroyWindow(hwnd);
3716     SetLastError(0xdeadbeef);
3717     ok(!IsMenu(hmenu), "IsMenu should fail\n");
3718     ok(GetLastError() == ERROR_INVALID_MENU_HANDLE, "IsMenu set error %ld\n", GetLastError());
3719
3720     hmenu = CreateMenu();
3721     assert(hmenu != 0);
3722     SetLastError(0xdeadbeef);
3723     hwnd = CreateWindowEx(0, "static", NULL, WS_POPUP,
3724                            0, 0, 100, 100, parent, hmenu, 0, NULL);
3725     ok(hwnd != 0, "CreateWindowEx error %ld\n", GetLastError());
3726     expect_menu(hwnd, hmenu);
3727     expect_style(hwnd, WS_POPUP | WS_CLIPSIBLINGS);
3728     expect_ex_style(hwnd, 0);
3729     DestroyWindow(hwnd);
3730     SetLastError(0xdeadbeef);
3731     ok(!IsMenu(hmenu), "IsMenu should fail\n");
3732     ok(GetLastError() == ERROR_INVALID_MENU_HANDLE, "IsMenu set error %ld\n", GetLastError());
3733
3734 #undef expect_menu
3735 #undef expect_style
3736 #undef expect_ex_style
3737 }
3738
3739 START_TEST(win)
3740 {
3741     pGetAncestor = (void *)GetProcAddress( GetModuleHandleA("user32.dll"), "GetAncestor" );
3742     pGetWindowInfo = (void *)GetProcAddress( GetModuleHandleA("user32.dll"), "GetWindowInfo" );
3743
3744     hwndMain = CreateWindowExA(0, "static", NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, 0, 0, NULL);
3745     if (hwndMain)
3746     {
3747         ok(!GetParent(hwndMain), "GetParent should return 0 for message only windows\n");
3748         if (pGetAncestor)
3749         {
3750             hwndMessage = pGetAncestor(hwndMain, GA_PARENT);
3751             ok(hwndMessage != 0, "GetAncestor(GA_PARENT) should not return 0 for message only windows\n");
3752             trace("hwndMessage %p\n", hwndMessage);
3753         }
3754         DestroyWindow(hwndMain);
3755     }
3756     else
3757         trace("CreateWindowExA with parent HWND_MESSAGE failed\n");
3758
3759     if (!RegisterWindowClasses()) assert(0);
3760
3761     hhook = SetWindowsHookExA(WH_CBT, cbt_hook_proc, 0, GetCurrentThreadId());
3762     assert(hhook);
3763
3764     hwndMain = CreateWindowExA(/*WS_EX_TOOLWINDOW*/ 0, "MainWindowClass", "Main window",
3765                                WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
3766                                WS_MAXIMIZEBOX | WS_POPUP,
3767                                100, 100, 200, 200,
3768                                0, 0, 0, NULL);
3769     test_nonclient_area(hwndMain);
3770
3771     hwndMain2 = CreateWindowExA(/*WS_EX_TOOLWINDOW*/ 0, "MainWindowClass", "Main window 2",
3772                                 WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
3773                                 WS_MAXIMIZEBOX | WS_POPUP,
3774                                 100, 100, 200, 200,
3775                                 0, 0, 0, NULL);
3776     assert( hwndMain );
3777     assert( hwndMain2 );
3778
3779     /* Add the tests below this line */
3780     test_params();
3781
3782     test_capture_1();
3783     test_capture_2();
3784     test_capture_3(hwndMain, hwndMain2);
3785
3786     test_CreateWindow();
3787     test_parent_owner();
3788     test_SetParent();
3789     test_shell_window();
3790
3791     test_mdi();
3792     test_icons();
3793     test_SetWindowPos(hwndMain);
3794     test_SetMenu(hwndMain);
3795     test_SetFocus(hwndMain);
3796     test_SetActiveWindow(hwndMain);
3797
3798     test_children_zorder(hwndMain);
3799     test_keyboard_input(hwndMain);
3800     test_mouse_input(hwndMain);
3801     test_validatergn(hwndMain);
3802     test_nccalcscroll( hwndMain);
3803     test_scrollvalidate( hwndMain);
3804     test_scrolldc( hwndMain);
3805     test_scroll();
3806     test_IsWindowUnicode();
3807     test_vis_rgn(hwndMain);
3808
3809     test_AdjustWindowRect();
3810     test_window_styles();
3811     test_redrawnow();
3812     test_csparentdc();
3813
3814     /* add the tests above this line */
3815     UnhookWindowsHookEx(hhook);
3816 }