user32: Don't use HIWORD() on a HINSTANCE.
[wine] / dlls / user32 / 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 #include <assert.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35
36 #include "wine/test.h"
37
38 #ifndef SPI_GETDESKWALLPAPER
39 #define SPI_GETDESKWALLPAPER 0x0073
40 #endif
41
42 #define LONG_PTR INT_PTR
43 #define ULONG_PTR UINT_PTR
44
45 void dump_region(HRGN hrgn);
46
47 static HWND (WINAPI *pGetAncestor)(HWND,UINT);
48 static BOOL (WINAPI *pGetWindowInfo)(HWND,WINDOWINFO*);
49 static UINT (WINAPI *pGetWindowModuleFileNameA)(HWND,LPSTR,UINT);
50 static BOOL (WINAPI *pGetLayeredWindowAttributes)(HWND,COLORREF*,BYTE*,DWORD*);
51 static BOOL (WINAPI *pSetLayeredWindowAttributes)(HWND,COLORREF,BYTE,DWORD);
52 static BOOL (WINAPI *pGetMonitorInfoA)(HMONITOR,LPMONITORINFO);
53 static HMONITOR (WINAPI *pMonitorFromPoint)(POINT,DWORD);
54 static int  (WINAPI *pGetWindowRgnBox)(HWND,LPRECT);
55 static BOOL (WINAPI *pGetGUIThreadInfo)(DWORD, GUITHREADINFO*);
56
57 static BOOL test_lbuttondown_flag;
58 static HWND hwndMessage;
59 static HWND hwndMain, hwndMain2;
60 static HHOOK hhook;
61
62 static const char* szAWRClass = "Winsize";
63 static HMENU hmenu;
64 static DWORD our_pid;
65
66 static BOOL is_win9x = FALSE;
67
68 #define COUNTOF(arr) (sizeof(arr)/sizeof(arr[0]))
69
70 static void dump_minmax_info( const MINMAXINFO *minmax )
71 {
72     trace("Reserved=%d,%d MaxSize=%d,%d MaxPos=%d,%d MinTrack=%d,%d MaxTrack=%d,%d\n",
73           minmax->ptReserved.x, minmax->ptReserved.y,
74           minmax->ptMaxSize.x, minmax->ptMaxSize.y,
75           minmax->ptMaxPosition.x, minmax->ptMaxPosition.y,
76           minmax->ptMinTrackSize.x, minmax->ptMinTrackSize.y,
77           minmax->ptMaxTrackSize.x, minmax->ptMaxTrackSize.y);
78 }
79
80 /* try to make sure pending X events have been processed before continuing */
81 static void flush_events( BOOL remove_messages )
82 {
83     MSG msg;
84     int diff = 200;
85     int min_timeout = 100;
86     DWORD time = GetTickCount() + diff;
87
88     while (diff > 0)
89     {
90         if (MsgWaitForMultipleObjects( 0, NULL, FALSE, min_timeout, QS_ALLINPUT ) == WAIT_TIMEOUT) break;
91         if (remove_messages)
92             while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
93         diff = time - GetTickCount();
94         min_timeout = 50;
95     }
96 }
97
98 /* check the values returned by the various parent/owner functions on a given window */
99 static void check_parents( HWND hwnd, HWND ga_parent, HWND gwl_parent, HWND get_parent,
100                            HWND gw_owner, HWND ga_root, HWND ga_root_owner )
101 {
102     HWND res;
103
104     if (pGetAncestor)
105     {
106         res = pGetAncestor( hwnd, GA_PARENT );
107         ok( res == ga_parent, "Wrong result for GA_PARENT %p expected %p\n", res, ga_parent );
108     }
109     res = (HWND)GetWindowLongPtrA( hwnd, GWLP_HWNDPARENT );
110     ok( res == gwl_parent, "Wrong result for GWL_HWNDPARENT %p expected %p\n", res, gwl_parent );
111     res = GetParent( hwnd );
112     ok( res == get_parent, "Wrong result for GetParent %p expected %p\n", res, get_parent );
113     res = GetWindow( hwnd, GW_OWNER );
114     ok( res == gw_owner, "Wrong result for GW_OWNER %p expected %p\n", res, gw_owner );
115     if (pGetAncestor)
116     {
117         res = pGetAncestor( hwnd, GA_ROOT );
118         ok( res == ga_root, "Wrong result for GA_ROOT %p expected %p\n", res, ga_root );
119         res = pGetAncestor( hwnd, GA_ROOTOWNER );
120         ok( res == ga_root_owner, "Wrong result for GA_ROOTOWNER %p expected %p\n", res, ga_root_owner );
121     }
122 }
123
124 static BOOL ignore_message( UINT message )
125 {
126     /* these are always ignored */
127     return (message >= 0xc000 ||
128             message == WM_GETICON ||
129             message == WM_GETOBJECT ||
130             message == WM_TIMECHANGE ||
131             message == WM_DEVICECHANGE);
132 }
133
134 static BOOL CALLBACK EnumChildProc( HWND hwndChild, LPARAM lParam)
135 {
136     (*(LPINT)lParam)++;
137     trace("EnumChildProc on %p\n", hwndChild);
138     if (*(LPINT)lParam > 1) return FALSE;
139     return TRUE;
140 }
141
142 /* will search for the given window */
143 static BOOL CALLBACK EnumChildProc1( HWND hwndChild, LPARAM lParam)
144 {
145     trace("EnumChildProc1 on %p\n", hwndChild);
146     if ((HWND)lParam == hwndChild) return FALSE;
147     return TRUE;
148 }
149
150 static HWND create_tool_window( LONG style, HWND parent )
151 {
152     HWND ret = CreateWindowExA(0, "ToolWindowClass", "Tool window 1", style,
153                                0, 0, 100, 100, parent, 0, 0, NULL );
154     ok( ret != 0, "Creation failed\n" );
155     return ret;
156 }
157
158 /* test parent and owner values for various combinations */
159 static void test_parent_owner(void)
160 {
161     LONG style;
162     HWND test, owner, ret;
163     HWND desktop = GetDesktopWindow();
164     HWND child = create_tool_window( WS_CHILD, hwndMain );
165     INT  numChildren;
166
167     trace( "main window %p main2 %p desktop %p child %p\n", hwndMain, hwndMain2, desktop, child );
168
169     /* child without parent, should fail */
170     SetLastError(0xdeadbeef);
171     test = CreateWindowExA(0, "ToolWindowClass", "Tool window 1",
172                            WS_CHILD, 0, 0, 100, 100, 0, 0, 0, NULL );
173     ok( !test, "WS_CHILD without parent created\n" );
174     ok( GetLastError() == ERROR_TLW_WITH_WSCHILD ||
175         broken(GetLastError() == 0xdeadbeef), /* win9x */
176         "CreateWindowExA error %u\n", GetLastError() );
177
178     /* desktop window */
179     check_parents( desktop, 0, 0, 0, 0, 0, 0 );
180     style = GetWindowLongA( desktop, GWL_STYLE );
181     ok( !SetWindowLongA( desktop, GWL_STYLE, WS_POPUP ), "Set GWL_STYLE on desktop succeeded\n" );
182     ok( !SetWindowLongA( desktop, GWL_STYLE, 0 ), "Set GWL_STYLE on desktop succeeded\n" );
183     ok( GetWindowLongA( desktop, GWL_STYLE ) == style, "Desktop style changed\n" );
184
185     /* normal child window */
186     test = create_tool_window( WS_CHILD, hwndMain );
187     trace( "created child %p\n", test );
188     check_parents( test, hwndMain, hwndMain, hwndMain, 0, hwndMain, hwndMain );
189     SetWindowLongA( test, GWL_STYLE, 0 );
190     check_parents( test, hwndMain, hwndMain, 0, 0, hwndMain, test );
191     SetWindowLongA( test, GWL_STYLE, WS_POPUP );
192     check_parents( test, hwndMain, hwndMain, 0, 0, hwndMain, test );
193     SetWindowLongA( test, GWL_STYLE, WS_POPUP|WS_CHILD );
194     check_parents( test, hwndMain, hwndMain, 0, 0, hwndMain, test );
195     SetWindowLongA( test, GWL_STYLE, WS_CHILD );
196     DestroyWindow( test );
197
198     /* normal child window with WS_MAXIMIZE */
199     test = create_tool_window( WS_CHILD | WS_MAXIMIZE, hwndMain );
200     DestroyWindow( test );
201
202     /* normal child window with WS_THICKFRAME */
203     test = create_tool_window( WS_CHILD | WS_THICKFRAME, hwndMain );
204     DestroyWindow( test );
205
206     /* popup window with WS_THICKFRAME */
207     test = create_tool_window( WS_POPUP | WS_THICKFRAME, hwndMain );
208     DestroyWindow( test );
209
210     /* child of desktop */
211     test = create_tool_window( WS_CHILD, desktop );
212     trace( "created child of desktop %p\n", test );
213     check_parents( test, desktop, 0, desktop, 0, test, desktop );
214     SetWindowLongA( test, GWL_STYLE, WS_POPUP );
215     check_parents( test, desktop, 0, 0, 0, test, test );
216     SetWindowLongA( test, GWL_STYLE, 0 );
217     check_parents( test, desktop, 0, 0, 0, test, test );
218     DestroyWindow( test );
219
220     /* child of desktop with WS_MAXIMIZE */
221     test = create_tool_window( WS_CHILD | WS_MAXIMIZE, desktop );
222     DestroyWindow( test );
223
224     /* child of desktop with WS_MINIMIZE */
225     test = create_tool_window( WS_CHILD | WS_MINIMIZE, desktop );
226     DestroyWindow( test );
227
228     /* child of child */
229     test = create_tool_window( WS_CHILD, child );
230     trace( "created child of child %p\n", test );
231     check_parents( test, child, child, child, 0, hwndMain, hwndMain );
232     SetWindowLongA( test, GWL_STYLE, 0 );
233     check_parents( test, child, child, 0, 0, hwndMain, test );
234     SetWindowLongA( test, GWL_STYLE, WS_POPUP );
235     check_parents( test, child, child, 0, 0, hwndMain, test );
236     DestroyWindow( test );
237
238     /* child of child with WS_MAXIMIZE */
239     test = create_tool_window( WS_CHILD | WS_MAXIMIZE, child );
240     DestroyWindow( test );
241
242     /* child of child with WS_MINIMIZE */
243     test = create_tool_window( WS_CHILD | WS_MINIMIZE, child );
244     DestroyWindow( test );
245
246     /* not owned top-level window */
247     test = create_tool_window( 0, 0 );
248     trace( "created top-level %p\n", test );
249     check_parents( test, desktop, 0, 0, 0, test, test );
250     SetWindowLongA( test, GWL_STYLE, WS_POPUP );
251     check_parents( test, desktop, 0, 0, 0, test, test );
252     SetWindowLongA( test, GWL_STYLE, WS_CHILD );
253     check_parents( test, desktop, 0, desktop, 0, test, desktop );
254     DestroyWindow( test );
255
256     /* not owned top-level window with WS_MAXIMIZE */
257     test = create_tool_window( WS_MAXIMIZE, 0 );
258     DestroyWindow( test );
259
260     /* owned top-level window */
261     test = create_tool_window( 0, hwndMain );
262     trace( "created owned top-level %p\n", test );
263     check_parents( test, desktop, hwndMain, 0, hwndMain, test, test );
264     SetWindowLongA( test, GWL_STYLE, WS_POPUP );
265     check_parents( test, desktop, hwndMain, hwndMain, hwndMain, test, hwndMain );
266     SetWindowLongA( test, GWL_STYLE, WS_CHILD );
267     check_parents( test, desktop, hwndMain, desktop, hwndMain, test, desktop );
268     DestroyWindow( test );
269
270     /* owned top-level window with WS_MAXIMIZE */
271     test = create_tool_window( WS_MAXIMIZE, hwndMain );
272     DestroyWindow( test );
273
274     /* not owned popup */
275     test = create_tool_window( WS_POPUP, 0 );
276     trace( "created popup %p\n", test );
277     check_parents( test, desktop, 0, 0, 0, test, test );
278     SetWindowLongA( test, GWL_STYLE, WS_CHILD );
279     check_parents( test, desktop, 0, desktop, 0, test, desktop );
280     SetWindowLongA( test, GWL_STYLE, 0 );
281     check_parents( test, desktop, 0, 0, 0, test, test );
282     DestroyWindow( test );
283
284     /* not owned popup with WS_MAXIMIZE */
285     test = create_tool_window( WS_POPUP | WS_MAXIMIZE, 0 );
286     DestroyWindow( test );
287
288     /* owned popup */
289     test = create_tool_window( WS_POPUP, hwndMain );
290     trace( "created owned popup %p\n", test );
291     check_parents( test, desktop, hwndMain, hwndMain, hwndMain, test, hwndMain );
292     SetWindowLongA( test, GWL_STYLE, WS_CHILD );
293     check_parents( test, desktop, hwndMain, desktop, hwndMain, test, desktop );
294     SetWindowLongA( test, GWL_STYLE, 0 );
295     check_parents( test, desktop, hwndMain, 0, hwndMain, test, test );
296     DestroyWindow( test );
297
298     /* owned popup with WS_MAXIMIZE */
299     test = create_tool_window( WS_POPUP | WS_MAXIMIZE, hwndMain );
300     DestroyWindow( test );
301
302     /* top-level window owned by child (same as owned by top-level) */
303     test = create_tool_window( 0, child );
304     trace( "created top-level owned by child %p\n", test );
305     check_parents( test, desktop, hwndMain, 0, hwndMain, test, test );
306     DestroyWindow( test );
307
308     /* top-level window owned by child (same as owned by top-level) with WS_MAXIMIZE */
309     test = create_tool_window( WS_MAXIMIZE, child );
310     DestroyWindow( test );
311
312     /* popup owned by desktop (same as not owned) */
313     test = create_tool_window( WS_POPUP, desktop );
314     trace( "created popup owned by desktop %p\n", test );
315     check_parents( test, desktop, 0, 0, 0, test, test );
316     DestroyWindow( test );
317
318     /* popup owned by desktop (same as not owned) with WS_MAXIMIZE */
319     test = create_tool_window( WS_POPUP | WS_MAXIMIZE, desktop );
320     DestroyWindow( test );
321
322     /* popup owned by child (same as owned by top-level) */
323     test = create_tool_window( WS_POPUP, child );
324     trace( "created popup owned by child %p\n", test );
325     check_parents( test, desktop, hwndMain, hwndMain, hwndMain, test, hwndMain );
326     DestroyWindow( test );
327
328     /* popup owned by child (same as owned by top-level) with WS_MAXIMIZE */
329     test = create_tool_window( WS_POPUP | WS_MAXIMIZE, child );
330     DestroyWindow( test );
331
332     /* not owned popup with WS_CHILD (same as WS_POPUP only) */
333     test = create_tool_window( WS_POPUP | WS_CHILD, 0 );
334     trace( "created WS_CHILD popup %p\n", test );
335     check_parents( test, desktop, 0, 0, 0, test, test );
336     DestroyWindow( test );
337
338     /* not owned popup with WS_CHILD | WS_MAXIMIZE (same as WS_POPUP only) */
339     test = create_tool_window( WS_POPUP | WS_CHILD | WS_MAXIMIZE, 0 );
340     DestroyWindow( test );
341
342     /* owned popup with WS_CHILD (same as WS_POPUP only) */
343     test = create_tool_window( WS_POPUP | WS_CHILD, hwndMain );
344     trace( "created owned WS_CHILD popup %p\n", test );
345     check_parents( test, desktop, hwndMain, hwndMain, hwndMain, test, hwndMain );
346     DestroyWindow( test );
347
348     /* owned popup with WS_CHILD (same as WS_POPUP only) with WS_MAXIMIZE */
349     test = create_tool_window( WS_POPUP | WS_CHILD | WS_MAXIMIZE, hwndMain );
350     DestroyWindow( test );
351
352     /******************** parent changes *************************/
353     trace( "testing parent changes\n" );
354
355     /* desktop window */
356     check_parents( desktop, 0, 0, 0, 0, 0, 0 );
357     if (0)
358     {
359     /* this test succeeds on NT but crashes on win9x systems */
360     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)hwndMain2 );
361     ok( !ret, "Set GWL_HWNDPARENT succeeded on desktop\n" );
362     check_parents( desktop, 0, 0, 0, 0, 0, 0 );
363     ok( !SetParent( desktop, hwndMain ), "SetParent succeeded on desktop\n" );
364     check_parents( desktop, 0, 0, 0, 0, 0, 0 );
365     }
366     /* normal child window */
367     test = create_tool_window( WS_CHILD, hwndMain );
368     trace( "created child %p\n", test );
369
370     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)hwndMain2 );
371     ok( ret == hwndMain, "GWL_HWNDPARENT return value %p expected %p\n", ret, hwndMain );
372     check_parents( test, hwndMain2, hwndMain2, hwndMain2, 0, hwndMain2, hwndMain2 );
373
374     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)child );
375     ok( ret == hwndMain2, "GWL_HWNDPARENT return value %p expected %p\n", ret, hwndMain2 );
376     check_parents( test, child, child, child, 0, hwndMain, hwndMain );
377
378     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)desktop );
379     ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
380     check_parents( test, desktop, 0, desktop, 0, test, desktop );
381
382     /* window is now child of desktop so GWLP_HWNDPARENT changes owner from now on */
383     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)child );
384     ok( ret == 0, "GWL_HWNDPARENT return value %p expected 0\n", ret );
385     check_parents( test, desktop, child, desktop, child, test, desktop );
386
387     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, 0 );
388     ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
389     check_parents( test, desktop, 0, desktop, 0, test, desktop );
390     DestroyWindow( test );
391
392     /* not owned top-level window */
393     test = create_tool_window( 0, 0 );
394     trace( "created top-level %p\n", test );
395
396     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)hwndMain2 );
397     ok( ret == 0, "GWL_HWNDPARENT return value %p expected 0\n", ret );
398     check_parents( test, desktop, hwndMain2, 0, hwndMain2, test, test );
399
400     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)child );
401     ok( ret == hwndMain2, "GWL_HWNDPARENT return value %p expected %p\n", ret, hwndMain2 );
402     check_parents( test, desktop, child, 0, child, test, test );
403
404     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, 0 );
405     ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
406     check_parents( test, desktop, 0, 0, 0, test, test );
407     DestroyWindow( test );
408
409     /* not owned popup */
410     test = create_tool_window( WS_POPUP, 0 );
411     trace( "created popup %p\n", test );
412
413     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)hwndMain2 );
414     ok( ret == 0, "GWL_HWNDPARENT return value %p expected 0\n", ret );
415     check_parents( test, desktop, hwndMain2, hwndMain2, hwndMain2, test, hwndMain2 );
416
417     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)child );
418     ok( ret == hwndMain2, "GWL_HWNDPARENT return value %p expected %p\n", ret, hwndMain2 );
419     check_parents( test, desktop, child, child, child, test, hwndMain );
420
421     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, 0 );
422     ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
423     check_parents( test, desktop, 0, 0, 0, test, test );
424     DestroyWindow( test );
425
426     /* normal child window */
427     test = create_tool_window( WS_CHILD, hwndMain );
428     trace( "created child %p\n", test );
429
430     ret = SetParent( test, desktop );
431     ok( ret == hwndMain, "SetParent return value %p expected %p\n", ret, hwndMain );
432     check_parents( test, desktop, 0, desktop, 0, test, desktop );
433
434     ret = SetParent( test, child );
435     ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
436     check_parents( test, child, child, child, 0, hwndMain, hwndMain );
437
438     ret = SetParent( test, hwndMain2 );
439     ok( ret == child, "SetParent return value %p expected %p\n", ret, child );
440     check_parents( test, hwndMain2, hwndMain2, hwndMain2, 0, hwndMain2, hwndMain2 );
441     DestroyWindow( test );
442
443     /* not owned top-level window */
444     test = create_tool_window( 0, 0 );
445     trace( "created top-level %p\n", test );
446
447     ret = SetParent( test, child );
448     ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
449     check_parents( test, child, child, 0, 0, hwndMain, test );
450
451     if (!is_win9x)
452     {
453         ShowWindow( test, SW_SHOW );
454         ret = SetParent( test, test );
455         ok( ret == NULL, "SetParent return value %p expected %p\n", ret, NULL );
456         ok( GetWindowLongA( test, GWL_STYLE ) & WS_VISIBLE, "window is not visible after SetParent\n" );
457         check_parents( test, child, child, 0, 0, hwndMain, test );
458     }
459     else
460         win_skip( "Test crashes on Win9x/WinMe\n" );
461     DestroyWindow( test );
462
463     /* owned popup */
464     test = create_tool_window( WS_POPUP, hwndMain2 );
465     trace( "created owned popup %p\n", test );
466
467     ret = SetParent( test, child );
468     ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
469     check_parents( test, child, child, hwndMain2, hwndMain2, hwndMain, hwndMain2 );
470
471     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (ULONG_PTR)hwndMain );
472     ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
473     check_parents( test, hwndMain, hwndMain, hwndMain2, hwndMain2, hwndMain, hwndMain2 );
474     DestroyWindow( test );
475
476     /**************** test owner destruction *******************/
477
478     /* owned child popup */
479     owner = create_tool_window( 0, 0 );
480     test = create_tool_window( WS_POPUP, owner );
481     trace( "created owner %p and popup %p\n", owner, test );
482     ret = SetParent( test, child );
483     ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
484     check_parents( test, child, child, owner, owner, hwndMain, owner );
485     /* window is now child of 'child' but owned by 'owner' */
486     DestroyWindow( owner );
487     ok( IsWindow(test), "Window %p destroyed by owner destruction\n", test );
488     /* Win98 doesn't pass this test. It doesn't allow a destroyed owner,
489      * while Win95, Win2k, WinXP do.
490      */
491     /*check_parents( test, child, child, owner, owner, hwndMain, owner );*/
492     ok( !IsWindow(owner), "Owner %p not destroyed\n", owner );
493     DestroyWindow(test);
494
495     /* owned top-level popup */
496     owner = create_tool_window( 0, 0 );
497     test = create_tool_window( WS_POPUP, owner );
498     trace( "created owner %p and popup %p\n", owner, test );
499     check_parents( test, desktop, owner, owner, owner, test, owner );
500     DestroyWindow( owner );
501     ok( !IsWindow(test), "Window %p not destroyed by owner destruction\n", test );
502
503     /* top-level popup owned by child */
504     owner = create_tool_window( WS_CHILD, hwndMain2 );
505     test = create_tool_window( WS_POPUP, 0 );
506     trace( "created owner %p and popup %p\n", owner, test );
507     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (ULONG_PTR)owner );
508     ok( ret == 0, "GWL_HWNDPARENT return value %p expected 0\n", ret );
509     check_parents( test, desktop, owner, owner, owner, test, hwndMain2 );
510     DestroyWindow( owner );
511     ok( IsWindow(test), "Window %p destroyed by owner destruction\n", test );
512     ok( !IsWindow(owner), "Owner %p not destroyed\n", owner );
513     /* Win98 doesn't pass this test. It doesn't allow a destroyed owner,
514      * while Win95, Win2k, WinXP do.
515      */
516     /*check_parents( test, desktop, owner, owner, owner, test, owner );*/
517     DestroyWindow(test);
518
519     /* final cleanup */
520     DestroyWindow(child);
521
522
523     owner = create_tool_window( WS_OVERLAPPED, 0 );
524     test = create_tool_window( WS_POPUP, desktop );
525
526     ok( !GetWindow( test, GW_OWNER ), "Wrong owner window\n" );
527     numChildren = 0;
528     ok( !EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
529         "EnumChildWindows should have returned FALSE\n" );
530     ok( numChildren == 0, "numChildren should be 0 got %d\n", numChildren );
531
532     SetWindowLongA( test, GWL_STYLE, (GetWindowLongA( test, GWL_STYLE ) & ~WS_POPUP) | WS_CHILD );
533     ret = SetParent( test, owner );
534     ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
535
536     numChildren = 0;
537     ok( EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
538         "EnumChildWindows should have returned TRUE\n" );
539     ok( numChildren == 1, "numChildren should be 1 got %d\n", numChildren );
540
541     child = create_tool_window( WS_CHILD, owner );
542     numChildren = 0;
543     ok( !EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
544         "EnumChildWindows should have returned FALSE\n" );
545     ok( numChildren == 2, "numChildren should be 2 got %d\n", numChildren );
546     DestroyWindow( child );
547
548     child = create_tool_window( WS_VISIBLE | WS_OVERLAPPEDWINDOW, owner );
549     ok( GetWindow( child, GW_OWNER ) == owner, "Wrong owner window\n" );
550     numChildren = 0;
551     ok( EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
552         "EnumChildWindows should have returned TRUE\n" );
553     ok( numChildren == 1, "numChildren should be 1 got %d\n", numChildren );
554
555     ret = SetParent( child, owner );
556     ok( GetWindow( child, GW_OWNER ) == owner, "Wrong owner window\n" );
557     ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
558     numChildren = 0;
559     ok( !EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
560         "EnumChildWindows should have returned FALSE\n" );
561     ok( numChildren == 2, "numChildren should be 2 got %d\n", numChildren );
562
563     ret = SetParent( child, NULL );
564     ok( GetWindow( child, GW_OWNER ) == owner, "Wrong owner window\n" );
565     ok( ret == owner, "SetParent return value %p expected %p\n", ret, owner );
566     numChildren = 0;
567     ok( EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
568         "EnumChildWindows should have returned TRUE\n" );
569     ok( numChildren == 1, "numChildren should be 1 got %d\n", numChildren );
570
571     /* even GW_OWNER == owner it's still a desktop's child */
572     ok( !EnumChildWindows( desktop, EnumChildProc1, (LPARAM)child ),
573         "EnumChildWindows should have found %p and returned FALSE\n", child );
574
575     DestroyWindow( child );
576     child = create_tool_window( WS_VISIBLE | WS_OVERLAPPEDWINDOW, NULL );
577
578     ok( !EnumChildWindows( desktop, EnumChildProc1, (LPARAM)child ),
579         "EnumChildWindows should have found %p and returned FALSE\n", child );
580
581     DestroyWindow( child );
582     DestroyWindow( test );
583     DestroyWindow( owner );
584 }
585
586 static BOOL CALLBACK enum_proc( HWND hwnd, LPARAM lParam)
587 {
588     (*(LPINT)lParam)++;
589     if (*(LPINT)lParam > 2) return FALSE;
590     return TRUE;
591 }
592 static DWORD CALLBACK enum_thread( void *arg )
593 {
594     INT count;
595     HWND hwnd[3];
596     BOOL ret;
597     MSG msg;
598
599     PeekMessage( &msg, 0, 0, 0, PM_NOREMOVE );  /* make sure we have a message queue */
600
601     count = 0;
602     ret = EnumThreadWindows( GetCurrentThreadId(), enum_proc, (LPARAM)&count );
603     ok( ret, "EnumThreadWindows should have returned TRUE\n" );
604     ok( count == 0, "count should be 0 got %d\n", count );
605
606     hwnd[0] = CreateWindowExA(0, "ToolWindowClass", "Tool window 1", WS_POPUP,
607                               0, 0, 100, 100, 0, 0, 0, NULL );
608     count = 0;
609     ret = EnumThreadWindows( GetCurrentThreadId(), enum_proc, (LPARAM)&count );
610     ok( ret, "EnumThreadWindows should have returned TRUE\n" );
611     if (count != 2)  /* Vista gives us two windows for the price of one */
612     {
613         ok( count == 1, "count should be 1 got %d\n", count );
614         hwnd[2] = CreateWindowExA(0, "ToolWindowClass", "Tool window 2", WS_POPUP,
615                                   0, 0, 100, 100, 0, 0, 0, NULL );
616     }
617     else hwnd[2] = 0;
618
619     hwnd[1] = CreateWindowExA(0, "ToolWindowClass", "Tool window 3", WS_POPUP,
620                               0, 0, 100, 100, 0, 0, 0, NULL );
621     count = 0;
622     ret = EnumThreadWindows( GetCurrentThreadId(), enum_proc, (LPARAM)&count );
623     ok( !ret, "EnumThreadWindows should have returned FALSE\n" );
624     ok( count == 3, "count should be 3 got %d\n", count );
625
626     if (hwnd[2]) DestroyWindow(hwnd[2]);
627     DestroyWindow(hwnd[1]);
628     DestroyWindow(hwnd[0]);
629     return 0;
630 }
631
632 /* test EnumThreadWindows in a separate thread */
633 static void test_enum_thread_windows(void)
634 {
635     DWORD id;
636     HANDLE handle = CreateThread( NULL, 0, enum_thread, 0, 0, &id );
637     ok( !WaitForSingleObject( handle, 10000 ), "wait failed\n" );
638     CloseHandle( handle );
639 }
640
641 static LRESULT WINAPI main_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
642 {
643     switch (msg)
644     {
645         case WM_GETMINMAXINFO:
646         {
647             MINMAXINFO* minmax = (MINMAXINFO *)lparam;
648
649             trace("WM_GETMINMAXINFO: hwnd %p, %08lx, %08lx\n", hwnd, wparam, lparam);
650             dump_minmax_info( minmax );
651             SetWindowLongPtrA(hwnd, GWLP_USERDATA, 0x20031021);
652             break;
653         }
654         case WM_WINDOWPOSCHANGING:
655         {
656             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
657             trace("main: WM_WINDOWPOSCHANGING %p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
658                    winpos->hwnd, winpos->hwndInsertAfter,
659                    winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
660             if (!(winpos->flags & SWP_NOMOVE))
661             {
662                 ok(winpos->x >= -32768 && winpos->x <= 32767, "bad winpos->x %d\n", winpos->x);
663                 ok(winpos->y >= -32768 && winpos->y <= 32767, "bad winpos->y %d\n", winpos->y);
664             }
665             /* Win9x does not fixup cx/xy for WM_WINDOWPOSCHANGING */
666             if (!(winpos->flags & SWP_NOSIZE) && !is_win9x)
667             {
668                 ok(winpos->cx >= 0 && winpos->cx <= 32767, "bad winpos->cx %d\n", winpos->cx);
669                 ok(winpos->cy >= 0 && winpos->cy <= 32767, "bad winpos->cy %d\n", winpos->cy);
670             }
671             break;
672         }
673         case WM_WINDOWPOSCHANGED:
674         {
675             RECT rc1, rc2;
676             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
677             trace("main: WM_WINDOWPOSCHANGED %p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
678                    winpos->hwnd, winpos->hwndInsertAfter,
679                    winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
680             ok(winpos->x >= -32768 && winpos->x <= 32767, "bad winpos->x %d\n", winpos->x);
681             ok(winpos->y >= -32768 && winpos->y <= 32767, "bad winpos->y %d\n", winpos->y);
682
683             ok(winpos->cx >= 0 && winpos->cx <= 32767, "bad winpos->cx %d\n", winpos->cx);
684             ok(winpos->cy >= 0 && winpos->cy <= 32767, "bad winpos->cy %d\n", winpos->cy);
685
686             GetWindowRect(hwnd, &rc1);
687             SetRect(&rc2, winpos->x, winpos->y, winpos->x + winpos->cx, winpos->y + winpos->cy);
688             /* note: winpos coordinates are relative to parent */
689             MapWindowPoints(GetParent(hwnd), 0, (LPPOINT)&rc2, 2);
690             if (0)
691             {
692             /* Uncomment this once the test succeeds in all cases */
693             ok(EqualRect(&rc1, &rc2), "rects do not match (%d,%d-%d,%d) / (%d,%d-%d,%d)\n",
694                rc1.left, rc1.top, rc1.right, rc1.bottom, rc2.left, rc2.top, rc2.right, rc2.bottom );
695
696             GetClientRect(hwnd, &rc2);
697             DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc1);
698             MapWindowPoints(0, hwnd, (LPPOINT)&rc1, 2);
699             ok(EqualRect(&rc1, &rc2), "rects do not match (%d,%d-%d,%d) / (%d,%d-%d,%d)\n",
700                rc1.left, rc1.top, rc1.right, rc1.bottom, rc2.left, rc2.top, rc2.right, rc2.bottom );
701             }
702             break;
703         }
704         case WM_NCCREATE:
705         {
706             BOOL got_getminmaxinfo = GetWindowLongPtrA(hwnd, GWLP_USERDATA) == 0x20031021;
707             CREATESTRUCTA *cs = (CREATESTRUCTA *)lparam;
708
709             trace("WM_NCCREATE: hwnd %p, parent %p, style %08x\n", hwnd, cs->hwndParent, cs->style);
710             if (got_getminmaxinfo)
711                 trace("%p got WM_GETMINMAXINFO\n", hwnd);
712
713             if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
714                 ok(got_getminmaxinfo, "main: WM_GETMINMAXINFO should have been received before WM_NCCREATE\n");
715             else
716                 ok(!got_getminmaxinfo, "main: WM_GETMINMAXINFO should NOT have been received before WM_NCCREATE\n");
717             break;
718         }
719         case WM_COMMAND:
720             if (test_lbuttondown_flag)
721             {
722                 ShowWindow((HWND)wparam, SW_SHOW);
723                 flush_events( FALSE );
724             }
725             break;
726     }
727
728     return DefWindowProcA(hwnd, msg, wparam, lparam);
729 }
730
731 static LRESULT WINAPI tool_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
732 {
733     switch (msg)
734     {
735         case WM_GETMINMAXINFO:
736         {
737             MINMAXINFO* minmax = (MINMAXINFO *)lparam;
738
739             trace("hwnd %p, WM_GETMINMAXINFO, %08lx, %08lx\n", hwnd, wparam, lparam);
740             dump_minmax_info( minmax );
741             SetWindowLongPtrA(hwnd, GWLP_USERDATA, 0x20031021);
742             break;
743         }
744         case WM_NCCREATE:
745         {
746             BOOL got_getminmaxinfo = GetWindowLongPtrA(hwnd, GWLP_USERDATA) == 0x20031021;
747             CREATESTRUCTA *cs = (CREATESTRUCTA *)lparam;
748
749             trace("WM_NCCREATE: hwnd %p, parent %p, style %08x\n", hwnd, cs->hwndParent, cs->style);
750             if (got_getminmaxinfo)
751                 trace("%p got WM_GETMINMAXINFO\n", hwnd);
752
753             if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
754                 ok(got_getminmaxinfo, "tool: WM_GETMINMAXINFO should have been received before WM_NCCREATE\n");
755             else
756                 ok(!got_getminmaxinfo, "tool: WM_GETMINMAXINFO should NOT have been received before WM_NCCREATE\n");
757             break;
758         }
759     }
760
761     return DefWindowProcA(hwnd, msg, wparam, lparam);
762 }
763
764 static BOOL RegisterWindowClasses(void)
765 {
766     WNDCLASSA cls;
767
768     cls.style = CS_DBLCLKS;
769     cls.lpfnWndProc = main_window_procA;
770     cls.cbClsExtra = 0;
771     cls.cbWndExtra = 0;
772     cls.hInstance = GetModuleHandleA(0);
773     cls.hIcon = 0;
774     cls.hCursor = LoadCursorA(0, IDC_ARROW);
775     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
776     cls.lpszMenuName = NULL;
777     cls.lpszClassName = "MainWindowClass";
778
779     if(!RegisterClassA(&cls)) return FALSE;
780
781     cls.style = 0;
782     cls.lpfnWndProc = tool_window_procA;
783     cls.cbClsExtra = 0;
784     cls.cbWndExtra = 0;
785     cls.hInstance = GetModuleHandleA(0);
786     cls.hIcon = 0;
787     cls.hCursor = LoadCursorA(0, IDC_ARROW);
788     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
789     cls.lpszMenuName = NULL;
790     cls.lpszClassName = "ToolWindowClass";
791
792     if(!RegisterClassA(&cls)) return FALSE;
793
794     return TRUE;
795 }
796
797 static void verify_window_info(const char *hook, HWND hwnd, const WINDOWINFO *info)
798 {
799     RECT rcWindow, rcClient;
800     DWORD status;
801
802     ok(IsWindow(hwnd), "bad window handle %p in hook %s\n", hwnd, hook);
803
804     GetWindowRect(hwnd, &rcWindow);
805     ok(EqualRect(&rcWindow, &info->rcWindow), "wrong rcWindow for %p in hook %s\n", hwnd, hook);
806
807     GetClientRect(hwnd, &rcClient);
808     /* translate to screen coordinates */
809     MapWindowPoints(hwnd, 0, (LPPOINT)&rcClient, 2);
810     ok(EqualRect(&rcClient, &info->rcClient), "wrong rcClient for %p in hook %s\n", hwnd, hook);
811
812     ok(info->dwStyle == (DWORD)GetWindowLongA(hwnd, GWL_STYLE),
813        "wrong dwStyle: %08x != %08x for %p in hook %s\n",
814        info->dwStyle, GetWindowLongA(hwnd, GWL_STYLE), hwnd, hook);
815     /* Windows reports some undocumented exstyles in WINDOWINFO, but
816      * doesn't return them in GetWindowLong(hwnd, GWL_EXSTYLE).
817      */
818     ok((info->dwExStyle & ~0xe0000800) == (DWORD)GetWindowLongA(hwnd, GWL_EXSTYLE),
819        "wrong dwExStyle: %08x != %08x for %p in hook %s\n",
820        info->dwExStyle, GetWindowLongA(hwnd, GWL_EXSTYLE), hwnd, hook);
821     status = (GetActiveWindow() == hwnd) ? WS_ACTIVECAPTION : 0;
822     if (GetForegroundWindow())
823         ok(info->dwWindowStatus == status, "wrong dwWindowStatus: %04x != %04x active %p fg %p in hook %s\n",
824            info->dwWindowStatus, status, GetActiveWindow(), GetForegroundWindow(), hook);
825
826     /* win2k and XP return broken border info in GetWindowInfo most of
827      * the time, so there is no point in testing it.
828      */
829 #if 0
830     UINT border;
831     ok(info->cxWindowBorders == (unsigned)(rcClient.left - rcWindow.left),
832        "wrong cxWindowBorders %d != %d\n", info->cxWindowBorders, rcClient.left - rcWindow.left);
833     border = min(rcWindow.bottom - rcClient.bottom, rcClient.top - rcWindow.top);
834     ok(info->cyWindowBorders == border,
835        "wrong cyWindowBorders %d != %d\n", info->cyWindowBorders, border);
836 #endif
837     ok(info->atomWindowType == GetClassLongA(hwnd, GCW_ATOM), "wrong atomWindowType for %p in hook %s\n",
838        hwnd, hook);
839     ok(info->wCreatorVersion == 0x0400 /* NT4, Win2000, XP, Win2003 */ ||
840        info->wCreatorVersion == 0x0500 /* Vista */,
841        "wrong wCreatorVersion %04x for %p in hook %s\n", info->wCreatorVersion, hwnd, hook);
842 }
843
844 static void FixedAdjustWindowRectEx(RECT* rc, LONG style, BOOL menu, LONG exstyle)
845 {
846     AdjustWindowRectEx(rc, style, menu, exstyle);
847     /* AdjustWindowRectEx does not include scroll bars */
848     if (style & WS_VSCROLL)
849     {
850         if(exstyle & WS_EX_LEFTSCROLLBAR)
851             rc->left  -= GetSystemMetrics(SM_CXVSCROLL);
852         else
853             rc->right += GetSystemMetrics(SM_CXVSCROLL);
854     }
855     if (style & WS_HSCROLL)
856         rc->bottom += GetSystemMetrics(SM_CYHSCROLL);
857 }
858
859 static void test_nonclient_area(HWND hwnd)
860 {
861     DWORD style, exstyle;
862     RECT rc_window, rc_client, rc;
863     BOOL menu;
864     LRESULT ret;
865
866     style = GetWindowLongA(hwnd, GWL_STYLE);
867     exstyle = GetWindowLongA(hwnd, GWL_EXSTYLE);
868     menu = !(style & WS_CHILD) && GetMenu(hwnd) != 0;
869
870     GetWindowRect(hwnd, &rc_window);
871     GetClientRect(hwnd, &rc_client);
872
873     /* avoid some cases when things go wrong */
874     if (IsRectEmpty(&rc_window) || IsRectEmpty(&rc_client) ||
875         rc_window.right > 32768 || rc_window.bottom > 32768) return;
876
877     CopyRect(&rc, &rc_client);
878     MapWindowPoints(hwnd, 0, (LPPOINT)&rc, 2);
879     FixedAdjustWindowRectEx(&rc, style, menu, exstyle);
880
881     ok(EqualRect(&rc, &rc_window),
882        "window rect does not match: style:exstyle=0x%08x:0x%08x, menu=%d, win=(%d,%d)-(%d,%d), calc=(%d,%d)-(%d,%d)\n",
883        style, exstyle, menu, rc_window.left, rc_window.top, rc_window.right, rc_window.bottom,
884        rc.left, rc.top, rc.right, rc.bottom);
885
886
887     CopyRect(&rc, &rc_window);
888     DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc);
889     MapWindowPoints(0, hwnd, (LPPOINT)&rc, 2);
890     ok(EqualRect(&rc, &rc_client),
891        "client rect does not match: style:exstyle=0x%08x:0x%08x, menu=%d client=(%d,%d)-(%d,%d), calc=(%d,%d)-(%d,%d)\n",
892        style, exstyle, menu, rc_client.left, rc_client.top, rc_client.right, rc_client.bottom,
893        rc.left, rc.top, rc.right, rc.bottom);
894
895     /* NULL rectangle shouldn't crash */
896     ret = DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, 0);
897     ok(ret == 0, "NULL rectangle returned %ld instead of 0\n", ret);
898
899     /* Win9x doesn't like WM_NCCALCSIZE with synthetic data and crashes */;
900     if (is_win9x)
901         return;
902
903     /* and now test AdjustWindowRectEx and WM_NCCALCSIZE on synthetic data */
904     SetRect(&rc_client, 0, 0, 250, 150);
905     CopyRect(&rc_window, &rc_client);
906     MapWindowPoints(hwnd, 0, (LPPOINT)&rc_window, 2);
907     FixedAdjustWindowRectEx(&rc_window, style, menu, exstyle);
908
909     CopyRect(&rc, &rc_window);
910     DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc);
911     MapWindowPoints(0, hwnd, (LPPOINT)&rc, 2);
912     ok(EqualRect(&rc, &rc_client),
913        "synthetic rect does not match: style:exstyle=0x%08x:0x%08x, menu=%d, client=(%d,%d)-(%d,%d), calc=(%d,%d)-(%d,%d)\n",
914        style, exstyle, menu, rc_client.left, rc_client.top, rc_client.right, rc_client.bottom,
915        rc.left, rc.top, rc.right, rc.bottom);
916 }
917
918 static LRESULT CALLBACK cbt_hook_proc(int nCode, WPARAM wParam, LPARAM lParam) 
919
920     static const char *CBT_code_name[10] = {
921         "HCBT_MOVESIZE",
922         "HCBT_MINMAX",
923         "HCBT_QS",
924         "HCBT_CREATEWND",
925         "HCBT_DESTROYWND",
926         "HCBT_ACTIVATE",
927         "HCBT_CLICKSKIPPED",
928         "HCBT_KEYSKIPPED",
929         "HCBT_SYSCOMMAND",
930         "HCBT_SETFOCUS" };
931     const char *code_name = (nCode >= 0 && nCode <= HCBT_SETFOCUS) ? CBT_code_name[nCode] : "Unknown";
932     HWND hwnd = (HWND)wParam;
933
934     switch (nCode)
935     {
936     case HCBT_CREATEWND:
937         {
938             static const RECT rc_null;
939             RECT rc;
940             LONG style;
941             CBT_CREATEWNDA *createwnd = (CBT_CREATEWNDA *)lParam;
942             trace("HCBT_CREATEWND: hwnd %p, parent %p, style %08x\n",
943                   hwnd, createwnd->lpcs->hwndParent, createwnd->lpcs->style);
944             ok(createwnd->hwndInsertAfter == HWND_TOP, "hwndInsertAfter should be always HWND_TOP\n");
945
946             if (pGetWindowInfo)
947             {
948                 WINDOWINFO info;
949                 info.cbSize = sizeof(WINDOWINFO);
950                 ok(pGetWindowInfo(hwnd, &info), "GetWindowInfo should not fail\n");
951                 verify_window_info(code_name, hwnd, &info);
952             }
953
954             /* WS_VISIBLE should be turned off yet */
955             style = createwnd->lpcs->style & ~WS_VISIBLE;
956             ok(style == GetWindowLongA(hwnd, GWL_STYLE),
957                 "style of hwnd and style in the CREATESTRUCT do not match: %08x != %08x\n",
958                 GetWindowLongA(hwnd, GWL_STYLE), style);
959
960             if (0)
961             {
962             /* Uncomment this once the test succeeds in all cases */
963             if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD)
964             {
965                 ok(GetParent(hwnd) == hwndMessage,
966                    "wrong result from GetParent %p: message window %p\n",
967                    GetParent(hwnd), hwndMessage);
968             }
969             else
970                 ok(!GetParent(hwnd), "GetParent should return 0 at this point\n");
971
972             ok(!GetWindow(hwnd, GW_OWNER), "GW_OWNER should be set to 0 at this point\n");
973             }
974             if (0)
975             {
976             /* while NT assigns GW_HWNDFIRST/LAST some values at this point,
977              * Win9x still has them set to 0.
978              */
979             ok(GetWindow(hwnd, GW_HWNDFIRST) != 0, "GW_HWNDFIRST should not be set to 0 at this point\n");
980             ok(GetWindow(hwnd, GW_HWNDLAST) != 0, "GW_HWNDLAST should not be set to 0 at this point\n");
981             }
982             ok(!GetWindow(hwnd, GW_HWNDPREV), "GW_HWNDPREV should be set to 0 at this point\n");
983             ok(!GetWindow(hwnd, GW_HWNDNEXT), "GW_HWNDNEXT should be set to 0 at this point\n");
984
985             if (0)
986             {
987             /* Uncomment this once the test succeeds in all cases */
988             if (pGetAncestor)
989             {
990                 ok(pGetAncestor(hwnd, GA_PARENT) == hwndMessage, "GA_PARENT should be set to hwndMessage at this point\n");
991                 ok(pGetAncestor(hwnd, GA_ROOT) == hwnd,
992                    "GA_ROOT is set to %p, expected %p\n", pGetAncestor(hwnd, GA_ROOT), hwnd);
993
994                 if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD)
995                     ok(pGetAncestor(hwnd, GA_ROOTOWNER) == hwndMessage,
996                        "GA_ROOTOWNER should be set to hwndMessage at this point\n");
997                 else
998                     ok(pGetAncestor(hwnd, GA_ROOTOWNER) == hwnd,
999                        "GA_ROOTOWNER is set to %p, expected %p\n", pGetAncestor(hwnd, GA_ROOTOWNER), hwnd);
1000             }
1001
1002             ok(GetWindowRect(hwnd, &rc), "GetWindowRect failed\n");
1003             ok(EqualRect(&rc, &rc_null), "window rect should be set to 0 HCBT_CREATEWND\n");
1004             ok(GetClientRect(hwnd, &rc), "GetClientRect failed\n");
1005             ok(EqualRect(&rc, &rc_null), "client rect should be set to 0 on HCBT_CREATEWND\n");
1006             }
1007             break;
1008         }
1009     case HCBT_MOVESIZE:
1010     case HCBT_MINMAX:
1011     case HCBT_ACTIVATE:
1012     case HCBT_SETFOCUS:
1013         if (pGetWindowInfo && IsWindow(hwnd))
1014         {
1015             WINDOWINFO info;
1016
1017             /* Win98 actually does check the info.cbSize and doesn't allow
1018              * it to be anything except sizeof(WINDOWINFO), while Win95, Win2k,
1019              * WinXP do not check it at all.
1020              */
1021             info.cbSize = sizeof(WINDOWINFO);
1022             ok(pGetWindowInfo(hwnd, &info), "GetWindowInfo should not fail\n");
1023             verify_window_info(code_name, hwnd, &info);
1024         }
1025         break;
1026     /* on HCBT_DESTROYWND window state is undefined */
1027     case HCBT_DESTROYWND:
1028         trace( "HCBT_DESTROYWND: hwnd %p\n", hwnd );
1029         break;
1030     default:
1031         break;
1032     }
1033
1034     return CallNextHookEx(hhook, nCode, wParam, lParam);
1035 }
1036
1037 static void test_shell_window(void)
1038 {
1039     BOOL ret;
1040     DWORD error;
1041     HMODULE hinst, hUser32;
1042     BOOL (WINAPI*SetShellWindow)(HWND);
1043     BOOL (WINAPI*SetShellWindowEx)(HWND, HWND);
1044     HWND hwnd1, hwnd2, hwnd3, hwnd4, hwnd5;
1045     HWND shellWindow, nextWnd;
1046
1047     if (is_win9x)
1048     {
1049         win_skip("Skipping shell window test on Win9x\n");
1050         return;
1051     }
1052
1053     shellWindow = GetShellWindow();
1054     hinst = GetModuleHandle(0);
1055     hUser32 = GetModuleHandleA("user32");
1056
1057     SetShellWindow = (void *)GetProcAddress(hUser32, "SetShellWindow");
1058     SetShellWindowEx = (void *)GetProcAddress(hUser32, "SetShellWindowEx");
1059
1060     trace("previous shell window: %p\n", shellWindow);
1061
1062     if (shellWindow) {
1063         DWORD pid;
1064         HANDLE hProcess;
1065
1066         GetWindowThreadProcessId(shellWindow, &pid);
1067         hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
1068         if (!hProcess)
1069         {
1070             skip( "cannot get access to shell process\n" );
1071             return;
1072         }
1073
1074         SetLastError(0xdeadbeef);
1075         ret = DestroyWindow(shellWindow);
1076         error = GetLastError();
1077
1078         ok(!ret, "DestroyWindow(shellWindow)\n");
1079         /* passes on Win XP, but not on Win98 */
1080         ok(error==ERROR_ACCESS_DENIED || error == 0xdeadbeef,
1081            "got %u after DestroyWindow(shellWindow)\n", error);
1082
1083         /* close old shell instance */
1084         ret = TerminateProcess(hProcess, 0);
1085         ok(ret, "termination of previous shell process failed: GetLastError()=%d\n", GetLastError());
1086         WaitForSingleObject(hProcess, INFINITE);    /* wait for termination */
1087         CloseHandle(hProcess);
1088     }
1089
1090     hwnd1 = CreateWindowEx(0, TEXT("#32770"), TEXT("TEST1"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 100, 100, 300, 200, 0, 0, hinst, 0);
1091     trace("created window 1: %p\n", hwnd1);
1092
1093     ret = SetShellWindow(hwnd1);
1094     ok(ret, "first call to SetShellWindow(hwnd1)\n");
1095     shellWindow = GetShellWindow();
1096     ok(shellWindow==hwnd1, "wrong shell window: %p\n", shellWindow);
1097
1098     ret = SetShellWindow(hwnd1);
1099     ok(!ret, "second call to SetShellWindow(hwnd1)\n");
1100
1101     ret = SetShellWindow(0);
1102     error = GetLastError();
1103     /* passes on Win XP, but not on Win98
1104     ok(!ret, "reset shell window by SetShellWindow(0)\n");
1105     ok(error==ERROR_INVALID_WINDOW_HANDLE, "ERROR_INVALID_WINDOW_HANDLE after SetShellWindow(0)\n"); */
1106
1107     ret = SetShellWindow(hwnd1);
1108     /* passes on Win XP, but not on Win98
1109     ok(!ret, "third call to SetShellWindow(hwnd1)\n"); */
1110
1111     SetWindowLong(hwnd1, GWL_EXSTYLE, GetWindowLong(hwnd1,GWL_EXSTYLE)|WS_EX_TOPMOST);
1112     ret = GetWindowLong(hwnd1,GWL_EXSTYLE)&WS_EX_TOPMOST? TRUE: FALSE;
1113     ok(!ret, "SetWindowExStyle(hwnd1, WS_EX_TOPMOST)\n");
1114
1115     ret = DestroyWindow(hwnd1);
1116     ok(ret, "DestroyWindow(hwnd1)\n");
1117
1118     hwnd2 = CreateWindowEx(WS_EX_TOPMOST, TEXT("#32770"), TEXT("TEST2"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 150, 250, 300, 200, 0, 0, hinst, 0);
1119     trace("created window 2: %p\n", hwnd2);
1120     ret = SetShellWindow(hwnd2);
1121     ok(!ret, "SetShellWindow(hwnd2) with WS_EX_TOPMOST\n");
1122
1123     hwnd3 = CreateWindowEx(0, TEXT("#32770"), TEXT("TEST3"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 200, 400, 300, 200, 0, 0, hinst, 0);
1124     trace("created window 3: %p\n", hwnd3);
1125
1126     hwnd4 = CreateWindowEx(0, TEXT("#32770"), TEXT("TEST4"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 250, 500, 300, 200, 0, 0, hinst, 0);
1127     trace("created window 4: %p\n", hwnd4);
1128
1129     nextWnd = GetWindow(hwnd4, GW_HWNDNEXT);
1130     ok(nextWnd==hwnd3, "wrong next window for hwnd4: %p - expected hwnd3\n", nextWnd);
1131
1132     ret = SetShellWindow(hwnd4);
1133     ok(ret, "SetShellWindow(hwnd4)\n");
1134     shellWindow = GetShellWindow();
1135     ok(shellWindow==hwnd4, "wrong shell window: %p - expected hwnd4\n", shellWindow);
1136
1137     nextWnd = GetWindow(hwnd4, GW_HWNDNEXT);
1138     ok(nextWnd==0, "wrong next window for hwnd4: %p - expected 0\n", nextWnd);
1139
1140     ret = SetWindowPos(hwnd4, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
1141     ok(ret, "SetWindowPos(hwnd4, HWND_TOPMOST)\n");
1142
1143     ret = SetWindowPos(hwnd4, hwnd3, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
1144     ok(ret, "SetWindowPos(hwnd4, hwnd3\n");
1145
1146     ret = SetShellWindow(hwnd3);
1147     ok(!ret, "SetShellWindow(hwnd3)\n");
1148     shellWindow = GetShellWindow();
1149     ok(shellWindow==hwnd4, "wrong shell window: %p - expected hwnd4\n", shellWindow);
1150
1151     hwnd5 = CreateWindowEx(0, TEXT("#32770"), TEXT("TEST5"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 300, 600, 300, 200, 0, 0, hinst, 0);
1152     trace("created window 5: %p\n", hwnd5);
1153     ret = SetWindowPos(hwnd4, hwnd5, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
1154     ok(ret, "SetWindowPos(hwnd4, hwnd5)\n");
1155
1156     todo_wine
1157     {
1158         nextWnd = GetWindow(hwnd4, GW_HWNDNEXT);
1159         ok(nextWnd==0, "wrong next window for hwnd4 after SetWindowPos(): %p - expected 0\n", nextWnd);
1160     }
1161
1162     /* destroy test windows */
1163     DestroyWindow(hwnd2);
1164     DestroyWindow(hwnd3);
1165     DestroyWindow(hwnd4);
1166     DestroyWindow(hwnd5);
1167 }
1168
1169 /************** MDI test ****************/
1170
1171 static char mdi_lParam_test_message[] = "just a test string";
1172
1173 static void test_MDI_create(HWND parent, HWND mdi_client, INT_PTR first_id)
1174 {
1175     MDICREATESTRUCTA mdi_cs;
1176     HWND mdi_child;
1177     INT_PTR id;
1178     static const WCHAR classW[] = {'M','D','I','_','c','h','i','l','d','_','C','l','a','s','s','_','1',0};
1179     static const WCHAR titleW[] = {'M','D','I',' ','c','h','i','l','d',0};
1180     BOOL isWin9x = FALSE;
1181
1182     mdi_cs.szClass = "MDI_child_Class_1";
1183     mdi_cs.szTitle = "MDI child";
1184     mdi_cs.hOwner = GetModuleHandle(0);
1185     mdi_cs.x = CW_USEDEFAULT;
1186     mdi_cs.y = CW_USEDEFAULT;
1187     mdi_cs.cx = CW_USEDEFAULT;
1188     mdi_cs.cy = CW_USEDEFAULT;
1189     mdi_cs.style = 0;
1190     mdi_cs.lParam = (LPARAM)mdi_lParam_test_message;
1191     mdi_child = (HWND)SendMessageA(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
1192     ok(mdi_child != 0, "MDI child creation failed\n");
1193     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1194     ok(id == first_id, "wrong child id %ld\n", id);
1195     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1196     ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1197
1198     mdi_cs.style = 0x7fffffff; /* without WS_POPUP */
1199     mdi_child = (HWND)SendMessageA(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
1200     ok(mdi_child != 0, "MDI child creation failed\n");
1201     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1202     ok(id == first_id, "wrong child id %ld\n", id);
1203     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1204     ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1205
1206     mdi_cs.style = 0xffffffff; /* with WS_POPUP */
1207     mdi_child = (HWND)SendMessageA(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
1208     if (GetWindowLongA(mdi_client, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1209     {
1210         ok(!mdi_child, "MDI child with WS_POPUP and with MDIS_ALLCHILDSTYLES should fail\n");
1211     }
1212     else
1213     {
1214         ok(mdi_child != 0, "MDI child creation failed\n");
1215         id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1216         ok(id == first_id, "wrong child id %ld\n", id);
1217         SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1218         ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1219     }
1220
1221     /* test MDICREATESTRUCT A<->W mapping */
1222     /* MDICREATESTRUCTA and MDICREATESTRUCTW have the same layout */
1223     mdi_cs.style = 0;
1224     mdi_cs.szClass = (LPCSTR)classW;
1225     mdi_cs.szTitle = (LPCSTR)titleW;
1226     SetLastError(0xdeadbeef);
1227     mdi_child = (HWND)SendMessageW(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
1228     if (!mdi_child)
1229     {
1230         if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1231             isWin9x = TRUE;
1232         else
1233             ok(mdi_child != 0, "MDI child creation failed\n");
1234     }
1235     else
1236     {
1237         id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1238         ok(id == first_id, "wrong child id %ld\n", id);
1239         SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1240         ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1241     }
1242
1243     mdi_child = CreateMDIWindowA("MDI_child_Class_1", "MDI child",
1244                                  0,
1245                                  CW_USEDEFAULT, CW_USEDEFAULT,
1246                                  CW_USEDEFAULT, CW_USEDEFAULT,
1247                                  mdi_client, GetModuleHandle(0),
1248                                  (LPARAM)mdi_lParam_test_message);
1249     ok(mdi_child != 0, "MDI child creation failed\n");
1250     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1251     ok(id == first_id, "wrong child id %ld\n", id);
1252     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1253     ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1254
1255     mdi_child = CreateMDIWindowA("MDI_child_Class_1", "MDI child",
1256                                  0x7fffffff, /* without WS_POPUP */
1257                                  CW_USEDEFAULT, CW_USEDEFAULT,
1258                                  CW_USEDEFAULT, CW_USEDEFAULT,
1259                                  mdi_client, GetModuleHandle(0),
1260                                  (LPARAM)mdi_lParam_test_message);
1261     ok(mdi_child != 0, "MDI child creation failed\n");
1262     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1263     ok(id == first_id, "wrong child id %ld\n", id);
1264     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1265     ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1266
1267     mdi_child = CreateMDIWindowA("MDI_child_Class_1", "MDI child",
1268                                  0xffffffff, /* with WS_POPUP */
1269                                  CW_USEDEFAULT, CW_USEDEFAULT,
1270                                  CW_USEDEFAULT, CW_USEDEFAULT,
1271                                  mdi_client, GetModuleHandle(0),
1272                                  (LPARAM)mdi_lParam_test_message);
1273     if (GetWindowLongA(mdi_client, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1274     {
1275         ok(!mdi_child, "MDI child with WS_POPUP and with MDIS_ALLCHILDSTYLES should fail\n");
1276     }
1277     else
1278     {
1279         ok(mdi_child != 0, "MDI child creation failed\n");
1280         id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1281         ok(id == first_id, "wrong child id %ld\n", id);
1282         SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1283         ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1284     }
1285
1286     /* test MDICREATESTRUCT A<->W mapping */
1287     SetLastError(0xdeadbeef);
1288     mdi_child = CreateMDIWindowW(classW, titleW,
1289                                  0,
1290                                  CW_USEDEFAULT, CW_USEDEFAULT,
1291                                  CW_USEDEFAULT, CW_USEDEFAULT,
1292                                  mdi_client, GetModuleHandle(0),
1293                                  (LPARAM)mdi_lParam_test_message);
1294     if (!mdi_child)
1295     {
1296         if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1297             isWin9x = TRUE;
1298         else
1299             ok(mdi_child != 0, "MDI child creation failed\n");
1300     }
1301     else
1302     {
1303         id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1304         ok(id == first_id, "wrong child id %ld\n", id);
1305         SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1306         ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1307     }
1308
1309     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_1", "MDI child",
1310                                 0,
1311                                 CW_USEDEFAULT, CW_USEDEFAULT,
1312                                 CW_USEDEFAULT, CW_USEDEFAULT,
1313                                 mdi_client, 0, GetModuleHandle(0),
1314                                 mdi_lParam_test_message);
1315     ok(mdi_child != 0, "MDI child creation failed\n");
1316     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1317     ok(id == first_id, "wrong child id %ld\n", id);
1318     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1319     ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1320
1321     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_1", "MDI child",
1322                                 0x7fffffff, /* without WS_POPUP */
1323                                 CW_USEDEFAULT, CW_USEDEFAULT,
1324                                 CW_USEDEFAULT, CW_USEDEFAULT,
1325                                 mdi_client, 0, GetModuleHandle(0),
1326                                 mdi_lParam_test_message);
1327     ok(mdi_child != 0, "MDI child creation failed\n");
1328     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1329     ok(id == first_id, "wrong child id %ld\n", id);
1330     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1331     ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1332
1333     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_1", "MDI child",
1334                                 0xffffffff, /* with WS_POPUP */
1335                                 CW_USEDEFAULT, CW_USEDEFAULT,
1336                                 CW_USEDEFAULT, CW_USEDEFAULT,
1337                                 mdi_client, 0, GetModuleHandle(0),
1338                                 mdi_lParam_test_message);
1339     if (GetWindowLongA(mdi_client, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1340     {
1341         ok(!mdi_child, "MDI child with WS_POPUP and with MDIS_ALLCHILDSTYLES should fail\n");
1342     }
1343     else
1344     {
1345         ok(mdi_child != 0, "MDI child creation failed\n");
1346         id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1347         ok(id == first_id, "wrong child id %ld\n", id);
1348         SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1349         ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1350     }
1351
1352     /* test MDICREATESTRUCT A<->W mapping */
1353     SetLastError(0xdeadbeef);
1354     mdi_child = CreateWindowExW(WS_EX_MDICHILD, classW, titleW,
1355                                 0,
1356                                 CW_USEDEFAULT, CW_USEDEFAULT,
1357                                 CW_USEDEFAULT, CW_USEDEFAULT,
1358                                 mdi_client, 0, GetModuleHandle(0),
1359                                 mdi_lParam_test_message);
1360     if (!mdi_child)
1361     {
1362         if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1363             isWin9x = TRUE;
1364         else
1365             ok(mdi_child != 0, "MDI child creation failed\n");
1366     }
1367     else
1368     {
1369         id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1370         ok(id == first_id, "wrong child id %ld\n", id);
1371         SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1372         ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1373     }
1374
1375     /* This test fails on Win9x */
1376     if (!isWin9x)
1377     {
1378         mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_2", "MDI child",
1379                                 WS_CHILD,
1380                                 CW_USEDEFAULT, CW_USEDEFAULT,
1381                                 CW_USEDEFAULT, CW_USEDEFAULT,
1382                                 parent, 0, GetModuleHandle(0),
1383                                 mdi_lParam_test_message);
1384         ok(!mdi_child, "WS_EX_MDICHILD with a not MDIClient parent should fail\n");
1385     }
1386
1387     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1388                                 WS_CHILD, /* without WS_POPUP */
1389                                 CW_USEDEFAULT, CW_USEDEFAULT,
1390                                 CW_USEDEFAULT, CW_USEDEFAULT,
1391                                 mdi_client, 0, GetModuleHandle(0),
1392                                 mdi_lParam_test_message);
1393     ok(mdi_child != 0, "MDI child creation failed\n");
1394     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1395     ok(id == 0, "wrong child id %ld\n", id);
1396     DestroyWindow(mdi_child);
1397
1398     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1399                                 WS_CHILD | WS_POPUP, /* with WS_POPUP */
1400                                 CW_USEDEFAULT, CW_USEDEFAULT,
1401                                 CW_USEDEFAULT, CW_USEDEFAULT,
1402                                 mdi_client, 0, GetModuleHandle(0),
1403                                 mdi_lParam_test_message);
1404     ok(mdi_child != 0, "MDI child creation failed\n");
1405     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1406     ok(id == 0, "wrong child id %ld\n", id);
1407     DestroyWindow(mdi_child);
1408
1409     /* maximized child */
1410     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1411                                 WS_CHILD | WS_MAXIMIZE,
1412                                 CW_USEDEFAULT, CW_USEDEFAULT,
1413                                 CW_USEDEFAULT, CW_USEDEFAULT,
1414                                 mdi_client, 0, GetModuleHandle(0),
1415                                 mdi_lParam_test_message);
1416     ok(mdi_child != 0, "MDI child creation failed\n");
1417     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1418     ok(id == 0, "wrong child id %ld\n", id);
1419     DestroyWindow(mdi_child);
1420
1421     trace("Creating maximized child with a caption\n");
1422     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1423                                 WS_CHILD | WS_MAXIMIZE | WS_CAPTION,
1424                                 CW_USEDEFAULT, CW_USEDEFAULT,
1425                                 CW_USEDEFAULT, CW_USEDEFAULT,
1426                                 mdi_client, 0, GetModuleHandle(0),
1427                                 mdi_lParam_test_message);
1428     ok(mdi_child != 0, "MDI child creation failed\n");
1429     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1430     ok(id == 0, "wrong child id %ld\n", id);
1431     DestroyWindow(mdi_child);
1432
1433     trace("Creating maximized child with a caption and a thick frame\n");
1434     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1435                                 WS_CHILD | WS_MAXIMIZE | WS_CAPTION | WS_THICKFRAME,
1436                                 CW_USEDEFAULT, CW_USEDEFAULT,
1437                                 CW_USEDEFAULT, CW_USEDEFAULT,
1438                                 mdi_client, 0, GetModuleHandle(0),
1439                                 mdi_lParam_test_message);
1440     ok(mdi_child != 0, "MDI child creation failed\n");
1441     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1442     ok(id == 0, "wrong child id %ld\n", id);
1443     DestroyWindow(mdi_child);
1444 }
1445
1446 /**********************************************************************
1447  * MDI_ChildGetMinMaxInfo (copied from windows/mdi.c)
1448  *
1449  * Note: The rule here is that client rect of the maximized MDI child
1450  *       is equal to the client rect of the MDI client window.
1451  */
1452 static void MDI_ChildGetMinMaxInfo( HWND client, HWND hwnd, MINMAXINFO* lpMinMax )
1453 {
1454     RECT rect;
1455
1456     GetClientRect( client, &rect );
1457     AdjustWindowRectEx( &rect, GetWindowLongA( hwnd, GWL_STYLE ),
1458                         0, GetWindowLongA( hwnd, GWL_EXSTYLE ));
1459
1460     rect.right -= rect.left;
1461     rect.bottom -= rect.top;
1462     lpMinMax->ptMaxSize.x = rect.right;
1463     lpMinMax->ptMaxSize.y = rect.bottom;
1464
1465     lpMinMax->ptMaxPosition.x = rect.left;
1466     lpMinMax->ptMaxPosition.y = rect.top;
1467
1468     trace("max rect (%d,%d - %d, %d)\n",
1469            rect.left, rect.top, rect.right, rect.bottom);
1470 }
1471
1472 static LRESULT WINAPI mdi_child_wnd_proc_1(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1473 {
1474     switch (msg)
1475     {
1476         case WM_NCCREATE:
1477         case WM_CREATE:
1478         {
1479             CREATESTRUCTA *cs = (CREATESTRUCTA *)lparam;
1480             MDICREATESTRUCTA *mdi_cs = cs->lpCreateParams;
1481
1482             ok(cs->dwExStyle & WS_EX_MDICHILD, "WS_EX_MDICHILD should be set\n");
1483             ok(mdi_cs->lParam == (LPARAM)mdi_lParam_test_message, "wrong mdi_cs->lParam\n");
1484
1485             ok(!lstrcmpA(cs->lpszClass, "MDI_child_Class_1"), "wrong class name\n");
1486             ok(!lstrcmpA(cs->lpszClass, mdi_cs->szClass), "class name does not match\n");
1487             ok(!lstrcmpA(cs->lpszName, "MDI child"), "wrong title\n");
1488             ok(!lstrcmpA(cs->lpszName, mdi_cs->szTitle), "title does not match\n");
1489             ok(cs->hInstance == mdi_cs->hOwner, "%p != %p\n", cs->hInstance, mdi_cs->hOwner);
1490
1491             /* MDICREATESTRUCT should have original values */
1492             ok(mdi_cs->style == 0 || mdi_cs->style == 0x7fffffff || mdi_cs->style == 0xffffffff,
1493                 "mdi_cs->style does not match (%08x)\n", mdi_cs->style);
1494             ok(mdi_cs->x == CW_USEDEFAULT, "%d != CW_USEDEFAULT\n", mdi_cs->x);
1495             ok(mdi_cs->y == CW_USEDEFAULT, "%d != CW_USEDEFAULT\n", mdi_cs->y);
1496             ok(mdi_cs->cx == CW_USEDEFAULT, "%d != CW_USEDEFAULT\n", mdi_cs->cx);
1497             ok(mdi_cs->cy == CW_USEDEFAULT, "%d != CW_USEDEFAULT\n", mdi_cs->cy);
1498
1499             /* CREATESTRUCT should have fixed values */
1500             ok(cs->x != CW_USEDEFAULT, "%d == CW_USEDEFAULT\n", cs->x);
1501             ok(cs->y != CW_USEDEFAULT, "%d == CW_USEDEFAULT\n", cs->y);
1502
1503             /* cx/cy == CW_USEDEFAULT are translated to NOT zero values */
1504             ok(cs->cx != CW_USEDEFAULT && cs->cx != 0, "%d == CW_USEDEFAULT\n", cs->cx);
1505             ok(cs->cy != CW_USEDEFAULT && cs->cy != 0, "%d == CW_USEDEFAULT\n", cs->cy);
1506
1507             ok(!(cs->style & WS_POPUP), "WS_POPUP is not allowed\n");
1508
1509             if (GetWindowLongA(cs->hwndParent, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1510             {
1511                 LONG style = mdi_cs->style | WS_CHILD | WS_CLIPSIBLINGS;
1512                 ok(cs->style == style,
1513                    "cs->style does not match (%08x)\n", cs->style);
1514             }
1515             else
1516             {
1517                 LONG style = mdi_cs->style;
1518                 style &= ~WS_POPUP;
1519                 style |= WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CAPTION |
1520                     WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
1521                 ok(cs->style == style,
1522                    "cs->style does not match (%08x)\n", cs->style);
1523             }
1524             break;
1525         }
1526
1527         case WM_GETMINMAXINFO:
1528         {
1529             HWND client = GetParent(hwnd);
1530             RECT rc;
1531             MINMAXINFO *minmax = (MINMAXINFO *)lparam;
1532             MINMAXINFO my_minmax;
1533             LONG style, exstyle;
1534
1535             style = GetWindowLongA(hwnd, GWL_STYLE);
1536             exstyle = GetWindowLongA(hwnd, GWL_EXSTYLE);
1537
1538             GetWindowRect(client, &rc);
1539             trace("MDI client %p window size = (%d x %d)\n", client, rc.right-rc.left, rc.bottom-rc.top);
1540             GetClientRect(client, &rc);
1541             trace("MDI client %p client size = (%d x %d)\n", client, rc.right, rc.bottom);
1542             trace("screen size: %d x %d\n", GetSystemMetrics(SM_CXSCREEN),
1543                                             GetSystemMetrics(SM_CYSCREEN));
1544
1545             GetClientRect(client, &rc);
1546             if ((style & WS_CAPTION) == WS_CAPTION)
1547                 style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
1548             AdjustWindowRectEx(&rc, style, 0, exstyle);
1549             trace("MDI child: calculated max window size = (%d x %d)\n", rc.right-rc.left, rc.bottom-rc.top);
1550             dump_minmax_info( minmax );
1551
1552             ok(minmax->ptMaxSize.x == rc.right - rc.left, "default width of maximized child %d != %d\n",
1553                minmax->ptMaxSize.x, rc.right - rc.left);
1554             ok(minmax->ptMaxSize.y == rc.bottom - rc.top, "default height of maximized child %d != %d\n",
1555                minmax->ptMaxSize.y, rc.bottom - rc.top);
1556
1557             DefMDIChildProcA(hwnd, msg, wparam, lparam);
1558
1559             trace("DefMDIChildProc returned:\n");
1560             dump_minmax_info( minmax );
1561
1562             MDI_ChildGetMinMaxInfo(client, hwnd, &my_minmax);
1563             ok(minmax->ptMaxSize.x == my_minmax.ptMaxSize.x, "default width of maximized child %d != %d\n",
1564                minmax->ptMaxSize.x, my_minmax.ptMaxSize.x);
1565             ok(minmax->ptMaxSize.y == my_minmax.ptMaxSize.y, "default height of maximized child %d != %d\n",
1566                minmax->ptMaxSize.y, my_minmax.ptMaxSize.y);
1567
1568             return 1;
1569         }
1570
1571         case WM_MDIACTIVATE:
1572         {
1573             HWND active, client = GetParent(hwnd);
1574             /*trace("%p WM_MDIACTIVATE %08x %08lx\n", hwnd, wparam, lparam);*/
1575             active = (HWND)SendMessageA(client, WM_MDIGETACTIVE, 0, 0);
1576             if (hwnd == (HWND)lparam) /* if we are being activated */
1577                 ok (active == (HWND)lparam, "new active %p != active %p\n", (HWND)lparam, active);
1578             else
1579                 ok (active == (HWND)wparam, "old active %p != active %p\n", (HWND)wparam, active);
1580             break;
1581         }
1582     }
1583     return DefMDIChildProcA(hwnd, msg, wparam, lparam);
1584 }
1585
1586 static LRESULT WINAPI mdi_child_wnd_proc_2(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1587 {
1588     switch (msg)
1589     {
1590         case WM_NCCREATE:
1591         case WM_CREATE:
1592         {
1593             CREATESTRUCTA *cs = (CREATESTRUCTA *)lparam;
1594
1595             trace("%s: x %d, y %d, cx %d, cy %d\n", (msg == WM_NCCREATE) ? "WM_NCCREATE" : "WM_CREATE",
1596                 cs->x, cs->y, cs->cx, cs->cy);
1597
1598             ok(!(cs->dwExStyle & WS_EX_MDICHILD), "WS_EX_MDICHILD should not be set\n");
1599             ok(cs->lpCreateParams == mdi_lParam_test_message, "wrong cs->lpCreateParams\n");
1600
1601             ok(!lstrcmpA(cs->lpszClass, "MDI_child_Class_2"), "wrong class name\n");
1602             ok(!lstrcmpA(cs->lpszName, "MDI child"), "wrong title\n");
1603
1604             /* CREATESTRUCT should have fixed values */
1605             /* For some reason Win9x doesn't translate cs->x from CW_USEDEFAULT,
1606                while NT does. */
1607             /*ok(cs->x != CW_USEDEFAULT, "%d == CW_USEDEFAULT\n", cs->x);*/
1608             ok(cs->y != CW_USEDEFAULT, "%d == CW_USEDEFAULT\n", cs->y);
1609
1610             /* cx/cy == CW_USEDEFAULT are translated to 0 */
1611             /* For some reason Win98 doesn't translate cs->cx from CW_USEDEFAULT,
1612                while Win95, Win2k, WinXP do. */
1613             /*ok(cs->cx == 0, "%d != 0\n", cs->cx);*/
1614             ok(cs->cy == 0, "%d != 0\n", cs->cy);
1615             break;
1616         }
1617
1618         case WM_GETMINMAXINFO:
1619         {
1620             HWND parent = GetParent(hwnd);
1621             RECT rc;
1622             MINMAXINFO *minmax = (MINMAXINFO *)lparam;
1623             LONG style, exstyle;
1624
1625             style = GetWindowLongA(hwnd, GWL_STYLE);
1626             exstyle = GetWindowLongA(hwnd, GWL_EXSTYLE);
1627
1628             GetClientRect(parent, &rc);
1629             trace("WM_GETMINMAXINFO: parent %p client size = (%d x %d)\n", parent, rc.right, rc.bottom);
1630
1631             GetClientRect(parent, &rc);
1632             if ((style & WS_CAPTION) == WS_CAPTION)
1633                 style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
1634             AdjustWindowRectEx(&rc, style, 0, exstyle);
1635             dump_minmax_info( minmax );
1636
1637             ok(minmax->ptMaxSize.x == rc.right - rc.left, "default width of maximized child %d != %d\n",
1638                minmax->ptMaxSize.x, rc.right - rc.left);
1639             ok(minmax->ptMaxSize.y == rc.bottom - rc.top, "default height of maximized child %d != %d\n",
1640                minmax->ptMaxSize.y, rc.bottom - rc.top);
1641             break;
1642         }
1643
1644         case WM_WINDOWPOSCHANGED:
1645         {
1646             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
1647             RECT rc1, rc2;
1648
1649             GetWindowRect(hwnd, &rc1);
1650             SetRect(&rc2, winpos->x, winpos->y, winpos->x + winpos->cx, winpos->y + winpos->cy);
1651             /* note: winpos coordinates are relative to parent */
1652             MapWindowPoints(GetParent(hwnd), 0, (LPPOINT)&rc2, 2);
1653             ok(EqualRect(&rc1, &rc2), "rects do not match, window=(%d,%d)-(%d,%d) pos=(%d,%d)-(%d,%d)\n",
1654                rc1.left, rc1.top, rc1.right, rc1.bottom,
1655                rc2.left, rc2.top, rc2.right, rc2.bottom);
1656             GetWindowRect(hwnd, &rc1);
1657             GetClientRect(hwnd, &rc2);
1658             DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc1);
1659             MapWindowPoints(0, hwnd, (LPPOINT)&rc1, 2);
1660             ok(EqualRect(&rc1, &rc2), "rects do not match, window=(%d,%d)-(%d,%d) client=(%d,%d)-(%d,%d)\n",
1661                rc1.left, rc1.top, rc1.right, rc1.bottom,
1662                rc2.left, rc2.top, rc2.right, rc2.bottom);
1663         }
1664         /* fall through */
1665         case WM_WINDOWPOSCHANGING:
1666         {
1667             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
1668             WINDOWPOS my_winpos = *winpos;
1669
1670             trace("%s: %p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
1671                   (msg == WM_WINDOWPOSCHANGING) ? "WM_WINDOWPOSCHANGING" : "WM_WINDOWPOSCHANGED",
1672                   winpos->hwnd, winpos->hwndInsertAfter,
1673                   winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
1674
1675             DefWindowProcA(hwnd, msg, wparam, lparam);
1676
1677             ok(!memcmp(&my_winpos, winpos, sizeof(WINDOWPOS)),
1678                "DefWindowProc should not change WINDOWPOS: %p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
1679                   winpos->hwnd, winpos->hwndInsertAfter,
1680                   winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
1681
1682             return 1;
1683         }
1684     }
1685     return DefWindowProcA(hwnd, msg, wparam, lparam);
1686 }
1687
1688 static LRESULT WINAPI mdi_main_wnd_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1689 {
1690     static HWND mdi_client;
1691
1692     switch (msg)
1693     {
1694         case WM_CREATE:
1695         {
1696             CLIENTCREATESTRUCT client_cs;
1697             RECT rc;
1698
1699             GetClientRect(hwnd, &rc);
1700
1701             client_cs.hWindowMenu = 0;
1702             client_cs.idFirstChild = 1;
1703
1704             /* MDIClient without MDIS_ALLCHILDSTYLES */
1705             mdi_client = CreateWindowExA(0, "mdiclient",
1706                                          NULL,
1707                                          WS_CHILD /*| WS_VISIBLE*/,
1708                                           /* tests depend on a not zero MDIClient size */
1709                                          0, 0, rc.right, rc.bottom,
1710                                          hwnd, 0, GetModuleHandle(0),
1711                                          &client_cs);
1712             assert(mdi_client);
1713             test_MDI_create(hwnd, mdi_client, client_cs.idFirstChild);
1714             DestroyWindow(mdi_client);
1715
1716             /* MDIClient with MDIS_ALLCHILDSTYLES */
1717             mdi_client = CreateWindowExA(0, "mdiclient",
1718                                          NULL,
1719                                          WS_CHILD | MDIS_ALLCHILDSTYLES /*| WS_VISIBLE*/,
1720                                           /* tests depend on a not zero MDIClient size */
1721                                          0, 0, rc.right, rc.bottom,
1722                                          hwnd, 0, GetModuleHandle(0),
1723                                          &client_cs);
1724             assert(mdi_client);
1725             test_MDI_create(hwnd, mdi_client, client_cs.idFirstChild);
1726             DestroyWindow(mdi_client);
1727             break;
1728         }
1729
1730         case WM_WINDOWPOSCHANGED:
1731         {
1732             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
1733             RECT rc1, rc2;
1734
1735             GetWindowRect(hwnd, &rc1);
1736             trace("window: (%d,%d)-(%d,%d)\n", rc1.left, rc1.top, rc1.right, rc1.bottom);
1737             SetRect(&rc2, winpos->x, winpos->y, winpos->x + winpos->cx, winpos->y + winpos->cy);
1738             /* note: winpos coordinates are relative to parent */
1739             MapWindowPoints(GetParent(hwnd), 0, (LPPOINT)&rc2, 2);
1740             trace("pos: (%d,%d)-(%d,%d)\n", rc2.left, rc2.top, rc2.right, rc2.bottom);
1741             ok(EqualRect(&rc1, &rc2), "rects do not match\n");
1742
1743             GetWindowRect(hwnd, &rc1);
1744             GetClientRect(hwnd, &rc2);
1745             DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc1);
1746             MapWindowPoints(0, hwnd, (LPPOINT)&rc1, 2);
1747             ok(EqualRect(&rc1, &rc2), "rects do not match\n");
1748         }
1749         /* fall through */
1750         case WM_WINDOWPOSCHANGING:
1751         {
1752             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
1753             WINDOWPOS my_winpos = *winpos;
1754
1755             trace("%s\n", (msg == WM_WINDOWPOSCHANGING) ? "WM_WINDOWPOSCHANGING" : "WM_WINDOWPOSCHANGED");
1756             trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
1757                   winpos->hwnd, winpos->hwndInsertAfter,
1758                   winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
1759
1760             DefWindowProcA(hwnd, msg, wparam, lparam);
1761
1762             trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
1763                   winpos->hwnd, winpos->hwndInsertAfter,
1764                   winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
1765
1766             ok(!memcmp(&my_winpos, winpos, sizeof(WINDOWPOS)),
1767                "DefWindowProc should not change WINDOWPOS values\n");
1768
1769             return 1;
1770         }
1771
1772         case WM_CLOSE:
1773             PostQuitMessage(0);
1774             break;
1775     }
1776     return DefFrameProcA(hwnd, mdi_client, msg, wparam, lparam);
1777 }
1778
1779 static BOOL mdi_RegisterWindowClasses(void)
1780 {
1781     WNDCLASSA cls;
1782
1783     cls.style = 0;
1784     cls.lpfnWndProc = mdi_main_wnd_procA;
1785     cls.cbClsExtra = 0;
1786     cls.cbWndExtra = 0;
1787     cls.hInstance = GetModuleHandleA(0);
1788     cls.hIcon = 0;
1789     cls.hCursor = LoadCursorA(0, IDC_ARROW);
1790     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
1791     cls.lpszMenuName = NULL;
1792     cls.lpszClassName = "MDI_parent_Class";
1793     if(!RegisterClassA(&cls)) return FALSE;
1794
1795     cls.lpfnWndProc = mdi_child_wnd_proc_1;
1796     cls.lpszClassName = "MDI_child_Class_1";
1797     if(!RegisterClassA(&cls)) return FALSE;
1798
1799     cls.lpfnWndProc = mdi_child_wnd_proc_2;
1800     cls.lpszClassName = "MDI_child_Class_2";
1801     if(!RegisterClassA(&cls)) return FALSE;
1802
1803     return TRUE;
1804 }
1805
1806 static void test_mdi(void)
1807 {
1808     HWND mdi_hwndMain;
1809     /*MSG msg;*/
1810
1811     if (!mdi_RegisterWindowClasses()) assert(0);
1812
1813     mdi_hwndMain = CreateWindowExA(0, "MDI_parent_Class", "MDI parent window",
1814                                    WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
1815                                    WS_MAXIMIZEBOX /*| WS_VISIBLE*/,
1816                                    100, 100, CW_USEDEFAULT, CW_USEDEFAULT,
1817                                    GetDesktopWindow(), 0,
1818                                    GetModuleHandle(0), NULL);
1819     assert(mdi_hwndMain);
1820 /*
1821     while(GetMessage(&msg, 0, 0, 0))
1822     {
1823         TranslateMessage(&msg);
1824         DispatchMessage(&msg);
1825     }
1826 */
1827     DestroyWindow(mdi_hwndMain);
1828 }
1829
1830 static void test_icons(void)
1831 {
1832     WNDCLASSEXA cls;
1833     HWND hwnd;
1834     HICON icon = LoadIconA(0, IDI_APPLICATION);
1835     HICON icon2 = LoadIconA(0, IDI_QUESTION);
1836     HICON small_icon = LoadImageA(0, IDI_APPLICATION, IMAGE_ICON,
1837                                   GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED );
1838     HICON res;
1839
1840     cls.cbSize = sizeof(cls);
1841     cls.style = 0;
1842     cls.lpfnWndProc = DefWindowProcA;
1843     cls.cbClsExtra = 0;
1844     cls.cbWndExtra = 0;
1845     cls.hInstance = 0;
1846     cls.hIcon = LoadIconA(0, IDI_HAND);
1847     cls.hIconSm = small_icon;
1848     cls.hCursor = LoadCursorA(0, IDC_ARROW);
1849     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
1850     cls.lpszMenuName = NULL;
1851     cls.lpszClassName = "IconWindowClass";
1852
1853     RegisterClassExA(&cls);
1854
1855     hwnd = CreateWindowExA(0, "IconWindowClass", "icon test", 0,
1856                            100, 100, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, NULL, NULL);
1857     assert( hwnd );
1858
1859     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_BIG, 0 );
1860     ok( res == 0, "wrong big icon %p/0\n", res );
1861     res = (HICON)SendMessageA( hwnd, WM_SETICON, ICON_BIG, (LPARAM)icon );
1862     ok( res == 0, "wrong previous big icon %p/0\n", res );
1863     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_BIG, 0 );
1864     ok( res == icon, "wrong big icon after set %p/%p\n", res, icon );
1865     res = (HICON)SendMessageA( hwnd, WM_SETICON, ICON_BIG, (LPARAM)icon2 );
1866     ok( res == icon, "wrong previous big icon %p/%p\n", res, icon );
1867     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_BIG, 0 );
1868     ok( res == icon2, "wrong big icon after set %p/%p\n", res, icon2 );
1869
1870     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL, 0 );
1871     ok( res == 0, "wrong small icon %p/0\n", res );
1872     /* this test is XP specific */
1873     /*res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL2, 0 );
1874     ok( res != 0, "wrong small icon %p\n", res );*/
1875     res = (HICON)SendMessageA( hwnd, WM_SETICON, ICON_SMALL, (LPARAM)icon );
1876     ok( res == 0, "wrong previous small icon %p/0\n", res );
1877     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL, 0 );
1878     ok( res == icon, "wrong small icon after set %p/%p\n", res, icon );
1879     /* this test is XP specific */
1880     /*res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL2, 0 );
1881     ok( res == icon, "wrong small icon after set %p/%p\n", res, icon );*/
1882     res = (HICON)SendMessageA( hwnd, WM_SETICON, ICON_SMALL, (LPARAM)small_icon );
1883     ok( res == icon, "wrong previous small icon %p/%p\n", res, icon );
1884     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL, 0 );
1885     ok( res == small_icon, "wrong small icon after set %p/%p\n", res, small_icon );
1886     /* this test is XP specific */
1887     /*res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL2, 0 );
1888     ok( res == small_icon, "wrong small icon after set %p/%p\n", res, small_icon );*/
1889
1890     /* make sure the big icon hasn't changed */
1891     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_BIG, 0 );
1892     ok( res == icon2, "wrong big icon after set %p/%p\n", res, icon2 );
1893
1894     DestroyWindow( hwnd );
1895 }
1896
1897 static LRESULT WINAPI nccalcsize_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1898 {
1899     if (msg == WM_NCCALCSIZE)
1900     {
1901         RECT *rect = (RECT *)lparam;
1902         /* first time around increase the rectangle, next time decrease it */
1903         if (rect->left == 100) InflateRect( rect, 10, 10 );
1904         else InflateRect( rect, -10, -10 );
1905         return 0;
1906     }
1907     return DefWindowProc( hwnd, msg, wparam, lparam );
1908 }
1909
1910 static void test_SetWindowPos(HWND hwnd)
1911 {
1912     RECT orig_win_rc, rect;
1913     LONG_PTR old_proc;
1914
1915     SetRect(&rect, 111, 222, 333, 444);
1916     ok(!GetWindowRect(0, &rect), "GetWindowRect succeeded\n");
1917     ok(rect.left == 111 && rect.top == 222 && rect.right == 333 && rect.bottom == 444,
1918        "wrong window rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
1919
1920     SetRect(&rect, 111, 222, 333, 444);
1921     ok(!GetClientRect(0, &rect), "GetClientRect succeeded\n");
1922     ok(rect.left == 111 && rect.top == 222 && rect.right == 333 && rect.bottom == 444,
1923        "wrong window rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
1924
1925     GetWindowRect(hwnd, &orig_win_rc);
1926
1927     old_proc = SetWindowLongPtr( hwnd, GWLP_WNDPROC, (ULONG_PTR)nccalcsize_proc );
1928     SetWindowPos(hwnd, 0, 100, 100, 0, 0, SWP_NOZORDER|SWP_FRAMECHANGED);
1929     GetWindowRect( hwnd, &rect );
1930     ok( rect.left == 100 && rect.top == 100 && rect.right == 100 && rect.bottom == 100,
1931         "invalid window rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
1932     GetClientRect( hwnd, &rect );
1933     MapWindowPoints( hwnd, 0, (POINT *)&rect, 2 );
1934     ok( rect.left == 90 && rect.top == 90 && rect.right == 110 && rect.bottom == 110,
1935         "invalid client rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
1936
1937     SetWindowPos(hwnd, 0, 200, 200, 0, 0, SWP_NOZORDER|SWP_FRAMECHANGED);
1938     GetWindowRect( hwnd, &rect );
1939     ok( rect.left == 200 && rect.top == 200 && rect.right == 200 && rect.bottom == 200,
1940         "invalid window rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
1941     GetClientRect( hwnd, &rect );
1942     MapWindowPoints( hwnd, 0, (POINT *)&rect, 2 );
1943     ok( rect.left == 210 && rect.top == 210 && rect.right == 190 && rect.bottom == 190,
1944         "invalid client rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
1945
1946     SetWindowPos(hwnd, 0, orig_win_rc.left, orig_win_rc.top,
1947                  orig_win_rc.right, orig_win_rc.bottom, 0);
1948     SetWindowLongPtr( hwnd, GWLP_WNDPROC, old_proc );
1949
1950     /* Win9x truncates coordinates to 16-bit irrespectively */
1951     if (!is_win9x)
1952     {
1953         SetWindowPos(hwnd, 0, -32769, -40000, -32769, -90000, SWP_NOMOVE);
1954         SetWindowPos(hwnd, 0, 32768, 40000, 32768, 40000, SWP_NOMOVE);
1955
1956         SetWindowPos(hwnd, 0, -32769, -40000, -32769, -90000, SWP_NOSIZE);
1957         SetWindowPos(hwnd, 0, 32768, 40000, 32768, 40000, SWP_NOSIZE);
1958     }
1959
1960     SetWindowPos(hwnd, 0, orig_win_rc.left, orig_win_rc.top,
1961                  orig_win_rc.right, orig_win_rc.bottom, 0);
1962
1963     ok(!(GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST), "WS_EX_TOPMOST should not be set\n");
1964     SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
1965     ok(GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST, "WS_EX_TOPMOST should be set\n");
1966     SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
1967     ok(GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST, "WS_EX_TOPMOST should be set\n");
1968     SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
1969     ok(!(GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST), "WS_EX_TOPMOST should not be set\n");
1970 }
1971
1972 static void test_SetMenu(HWND parent)
1973 {
1974     HWND child;
1975     HMENU hMenu, ret;
1976     BOOL retok;
1977     DWORD style;
1978
1979     hMenu = CreateMenu();
1980     assert(hMenu);
1981
1982     ok(SetMenu(parent, hMenu), "SetMenu on a top level window should not fail\n");
1983     if (0)
1984     {
1985     /* fails on (at least) Wine, NT4, XP SP2 */
1986     test_nonclient_area(parent); 
1987     }
1988     ret = GetMenu(parent);
1989     ok(ret == hMenu, "unexpected menu id %p\n", ret);
1990     /* test whether we can destroy a menu assigned to a window */
1991     retok = DestroyMenu(hMenu);
1992     ok( retok, "DestroyMenu error %d\n", GetLastError());
1993     retok = IsMenu(hMenu);
1994     ok(!retok || broken(retok) /* nt4 */, "menu handle should be not valid after DestroyMenu\n");
1995     ret = GetMenu(parent);
1996     /* This test fails on Win9x */
1997     if (!is_win9x)
1998         ok(ret == hMenu, "unexpected menu id %p\n", ret);
1999     ok(SetMenu(parent, 0), "SetMenu(0) on a top level window should not fail\n");
2000     test_nonclient_area(parent);
2001
2002     hMenu = CreateMenu();
2003     assert(hMenu);
2004
2005     /* parent */
2006     ret = GetMenu(parent);
2007     ok(ret == 0, "unexpected menu id %p\n", ret);
2008
2009     ok(!SetMenu(parent, (HMENU)20), "SetMenu with invalid menu handle should fail\n");
2010     test_nonclient_area(parent);
2011     ret = GetMenu(parent);
2012     ok(ret == 0, "unexpected menu id %p\n", ret);
2013
2014     ok(SetMenu(parent, hMenu), "SetMenu on a top level window should not fail\n");
2015     if (0)
2016     {
2017     /* fails on (at least) Wine, NT4, XP SP2 */
2018     test_nonclient_area(parent);
2019     }
2020     ret = GetMenu(parent);
2021     ok(ret == hMenu, "unexpected menu id %p\n", ret);
2022
2023     ok(SetMenu(parent, 0), "SetMenu(0) on a top level window should not fail\n");
2024     test_nonclient_area(parent);
2025     ret = GetMenu(parent);
2026     ok(ret == 0, "unexpected menu id %p\n", ret);
2027  
2028     /* child */
2029     child = CreateWindowExA(0, "static", NULL, WS_CHILD, 0, 0, 0, 0, parent, (HMENU)10, 0, NULL);
2030     assert(child);
2031
2032     ret = GetMenu(child);
2033     ok(ret == (HMENU)10, "unexpected menu id %p\n", ret);
2034
2035     ok(!SetMenu(child, (HMENU)20), "SetMenu with invalid menu handle should fail\n");
2036     test_nonclient_area(child);
2037     ret = GetMenu(child);
2038     ok(ret == (HMENU)10, "unexpected menu id %p\n", ret);
2039
2040     ok(!SetMenu(child, hMenu), "SetMenu on a child window should fail\n");
2041     test_nonclient_area(child);
2042     ret = GetMenu(child);
2043     ok(ret == (HMENU)10, "unexpected menu id %p\n", ret);
2044
2045     ok(!SetMenu(child, 0), "SetMenu(0) on a child window should fail\n");
2046     test_nonclient_area(child);
2047     ret = GetMenu(child);
2048     ok(ret == (HMENU)10, "unexpected menu id %p\n", ret);
2049
2050     style = GetWindowLong(child, GWL_STYLE);
2051     SetWindowLong(child, GWL_STYLE, style | WS_POPUP);
2052     ok(SetMenu(child, hMenu), "SetMenu on a popup child window should not fail\n");
2053     ok(SetMenu(child, 0), "SetMenu on a popup child window should not fail\n");
2054     SetWindowLong(child, GWL_STYLE, style);
2055
2056     SetWindowLong(child, GWL_STYLE, style | WS_OVERLAPPED);
2057     ok(!SetMenu(child, hMenu), "SetMenu on a overlapped child window should fail\n");
2058     SetWindowLong(child, GWL_STYLE, style);
2059
2060     DestroyWindow(child);
2061     DestroyMenu(hMenu);
2062 }
2063
2064 static void test_window_tree(HWND parent, const DWORD *style, const int *order, int total)
2065 {
2066     HWND child[5], hwnd;
2067     INT_PTR i;
2068
2069     assert(total <= 5);
2070
2071     hwnd = GetWindow(parent, GW_CHILD);
2072     ok(!hwnd, "have to start without children to perform the test\n");
2073
2074     for (i = 0; i < total; i++)
2075     {
2076         if (style[i] & DS_CONTROL)
2077         {
2078             child[i] = CreateWindowExA(0, MAKEINTATOM(32770), "", style[i] & ~WS_VISIBLE,
2079                                        0,0,0,0, parent, (HMENU)i, 0, NULL);
2080             if (style[i] & WS_VISIBLE)
2081                 ShowWindow(child[i], SW_SHOW);
2082
2083             SetWindowPos(child[i], HWND_BOTTOM, 0,0,10,10, SWP_NOACTIVATE);
2084         }
2085         else
2086             child[i] = CreateWindowExA(0, "static", "", style[i], 0,0,10,10,
2087                                        parent, (HMENU)i, 0, NULL);
2088         trace("child[%ld] = %p\n", i, child[i]);
2089         ok(child[i] != 0, "CreateWindowEx failed to create child window\n");
2090     }
2091
2092     hwnd = GetWindow(parent, GW_CHILD);
2093     ok(hwnd != 0, "GetWindow(GW_CHILD) failed\n");
2094     ok(hwnd == GetWindow(child[total - 1], GW_HWNDFIRST), "GW_HWNDFIRST is wrong\n");
2095     ok(child[order[total - 1]] == GetWindow(child[0], GW_HWNDLAST), "GW_HWNDLAST is wrong\n");
2096
2097     for (i = 0; i < total; i++)
2098     {
2099         trace("hwnd[%ld] = %p\n", i, hwnd);
2100         ok(child[order[i]] == hwnd, "Z order of child #%ld is wrong\n", i);
2101
2102         hwnd = GetWindow(hwnd, GW_HWNDNEXT);
2103     }
2104
2105     for (i = 0; i < total; i++)
2106         ok(DestroyWindow(child[i]), "DestroyWindow failed\n");
2107 }
2108
2109 static void test_children_zorder(HWND parent)
2110 {
2111     const DWORD simple_style[5] = { WS_CHILD, WS_CHILD, WS_CHILD, WS_CHILD,
2112                                     WS_CHILD };
2113     const int simple_order[5] = { 0, 1, 2, 3, 4 };
2114
2115     const DWORD complex_style[5] = { WS_CHILD, WS_CHILD | WS_MAXIMIZE,
2116                              WS_CHILD | WS_VISIBLE, WS_CHILD,
2117                              WS_CHILD | WS_MAXIMIZE | WS_VISIBLE };
2118     const int complex_order_1[1] = { 0 };
2119     const int complex_order_2[2] = { 1, 0 };
2120     const int complex_order_3[3] = { 1, 0, 2 };
2121     const int complex_order_4[4] = { 1, 0, 2, 3 };
2122     const int complex_order_5[5] = { 4, 1, 0, 2, 3 };
2123     const DWORD complex_style_6[3] = { WS_CHILD | WS_VISIBLE,
2124                                        WS_CHILD | WS_CLIPSIBLINGS | DS_CONTROL | WS_VISIBLE,
2125                                        WS_CHILD | WS_VISIBLE };
2126     const int complex_order_6[3] = { 0, 1, 2 };
2127
2128     /* simple WS_CHILD */
2129     test_window_tree(parent, simple_style, simple_order, 5);
2130
2131     /* complex children styles */
2132     test_window_tree(parent, complex_style, complex_order_1, 1);
2133     test_window_tree(parent, complex_style, complex_order_2, 2);
2134     test_window_tree(parent, complex_style, complex_order_3, 3);
2135     test_window_tree(parent, complex_style, complex_order_4, 4);
2136     test_window_tree(parent, complex_style, complex_order_5, 5);
2137
2138     /* another set of complex children styles */
2139     test_window_tree(parent, complex_style_6, complex_order_6, 3);
2140 }
2141
2142 #define check_z_order(hwnd, next, prev, owner, topmost) \
2143         check_z_order_debug((hwnd), (next), (prev), (owner), (topmost), \
2144                             __FILE__, __LINE__)
2145
2146 static void check_z_order_debug(HWND hwnd, HWND next, HWND prev, HWND owner,
2147                                 BOOL topmost, const char *file, int line)
2148 {
2149     HWND test;
2150     DWORD ex_style;
2151
2152     test = GetWindow(hwnd, GW_HWNDNEXT);
2153     /* skip foreign windows */
2154     while (test && test != next &&
2155            (GetWindowThreadProcessId(test, NULL) != our_pid ||
2156             UlongToHandle(GetWindowLongPtr(test, GWLP_HINSTANCE)) != GetModuleHandle(0) ||
2157             GetWindow(test, GW_OWNER) == next))
2158     {
2159         /*trace("skipping next %p (%p)\n", test, UlongToHandle(GetWindowLongPtr(test, GWLP_HINSTANCE)));*/
2160         test = GetWindow(test, GW_HWNDNEXT);
2161     }
2162     ok_(file, line)(next == test, "expected next %p, got %p\n", next, test);
2163
2164     test = GetWindow(hwnd, GW_HWNDPREV);
2165     /* skip foreign windows */
2166     while (test && test != prev &&
2167            (GetWindowThreadProcessId(test, NULL) != our_pid ||
2168             UlongToHandle(GetWindowLongPtr(test, GWLP_HINSTANCE)) != GetModuleHandle(0) ||
2169             GetWindow(test, GW_OWNER) == hwnd))
2170     {
2171         /*trace("skipping prev %p (%p)\n", test, UlongToHandle(GetWindowLongPtr(test, GWLP_HINSTANCE)));*/
2172         test = GetWindow(test, GW_HWNDPREV);
2173     }
2174     ok_(file, line)(prev == test, "expected prev %p, got %p\n", prev, test);
2175
2176     test = GetWindow(hwnd, GW_OWNER);
2177     ok_(file, line)(owner == test, "expected owner %p, got %p\n", owner, test);
2178
2179     ex_style = GetWindowLong(hwnd, GWL_EXSTYLE);
2180     ok_(file, line)(!(ex_style & WS_EX_TOPMOST) == !topmost, "expected %stopmost\n", topmost ? "" : "NOT ");
2181 }
2182
2183 static void test_popup_zorder(HWND hwnd_D, HWND hwnd_E)
2184 {
2185     HWND hwnd_A, hwnd_B, hwnd_C, hwnd_F;
2186
2187     trace("hwnd_D %p, hwnd_E %p\n", hwnd_D, hwnd_E);
2188
2189     SetWindowPos(hwnd_E, hwnd_D, 0,0,0,0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE);
2190
2191     check_z_order(hwnd_D, hwnd_E, 0, 0, FALSE);
2192     check_z_order(hwnd_E, 0, hwnd_D, 0, FALSE);
2193
2194     hwnd_F = CreateWindowEx(0, "MainWindowClass", "Owner window",
2195                             WS_OVERLAPPED | WS_CAPTION,
2196                             100, 100, 100, 100,
2197                             0, 0, GetModuleHandle(0), NULL);
2198     trace("hwnd_F %p\n", hwnd_F);
2199     check_z_order(hwnd_F, hwnd_D, 0, 0, FALSE);
2200
2201     SetWindowPos(hwnd_F, hwnd_E, 0,0,0,0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE);
2202     check_z_order(hwnd_F, 0, hwnd_E, 0, FALSE);
2203     check_z_order(hwnd_E, hwnd_F, hwnd_D, 0, FALSE);
2204     check_z_order(hwnd_D, hwnd_E, 0, 0, FALSE);
2205
2206     hwnd_C = CreateWindowEx(0, "MainWindowClass", NULL,
2207                             WS_POPUP,
2208                             100, 100, 100, 100,
2209                             hwnd_F, 0, GetModuleHandle(0), NULL);
2210     trace("hwnd_C %p\n", hwnd_C);
2211     check_z_order(hwnd_F, 0, hwnd_E, 0, FALSE);
2212     check_z_order(hwnd_E, hwnd_F, hwnd_D, 0, FALSE);
2213     check_z_order(hwnd_D, hwnd_E, hwnd_C, 0, FALSE);
2214     check_z_order(hwnd_C, hwnd_D, 0, hwnd_F, FALSE);
2215
2216     hwnd_B = CreateWindowEx(WS_EX_TOPMOST, "MainWindowClass", NULL,
2217                             WS_POPUP,
2218                             100, 100, 100, 100,
2219                             hwnd_F, 0, GetModuleHandle(0), NULL);
2220     trace("hwnd_B %p\n", hwnd_B);
2221     check_z_order(hwnd_F, 0, hwnd_E, 0, FALSE);
2222     check_z_order(hwnd_E, hwnd_F, hwnd_D, 0, FALSE);
2223     check_z_order(hwnd_D, hwnd_E, hwnd_C, 0, FALSE);
2224     check_z_order(hwnd_C, hwnd_D, hwnd_B, hwnd_F, FALSE);
2225     check_z_order(hwnd_B, hwnd_C, 0, hwnd_F, TRUE);
2226
2227     hwnd_A = CreateWindowEx(WS_EX_TOPMOST, "MainWindowClass", NULL,
2228                             WS_POPUP,
2229                             100, 100, 100, 100,
2230                             0, 0, GetModuleHandle(0), NULL);
2231     trace("hwnd_A %p\n", hwnd_A);
2232     check_z_order(hwnd_F, 0, hwnd_E, 0, FALSE);
2233     check_z_order(hwnd_E, hwnd_F, hwnd_D, 0, FALSE);
2234     check_z_order(hwnd_D, hwnd_E, hwnd_C, 0, FALSE);
2235     check_z_order(hwnd_C, hwnd_D, hwnd_B, hwnd_F, FALSE);
2236     check_z_order(hwnd_B, hwnd_C, hwnd_A, hwnd_F, TRUE);
2237     check_z_order(hwnd_A, hwnd_B, 0, 0, TRUE);
2238
2239     trace("A %p B %p C %p D %p E %p F %p\n", hwnd_A, hwnd_B, hwnd_C, hwnd_D, hwnd_E, hwnd_F);
2240
2241     /* move hwnd_F and its popups up */
2242     SetWindowPos(hwnd_F, HWND_TOP, 0,0,0,0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE);
2243     check_z_order(hwnd_E, 0, hwnd_D, 0, FALSE);
2244     check_z_order(hwnd_D, hwnd_E, hwnd_F, 0, FALSE);
2245     check_z_order(hwnd_F, hwnd_D, hwnd_C, 0, FALSE);
2246     check_z_order(hwnd_C, hwnd_F, hwnd_B, hwnd_F, FALSE);
2247     check_z_order(hwnd_B, hwnd_C, hwnd_A, hwnd_F, TRUE);
2248     check_z_order(hwnd_A, hwnd_B, 0, 0, TRUE);
2249
2250     /* move hwnd_F and its popups down */
2251 #if 0 /* enable once Wine is fixed to pass this test */
2252     SetWindowPos(hwnd_F, HWND_BOTTOM, 0,0,0,0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE);
2253     check_z_order(hwnd_F, 0, hwnd_C, 0, FALSE);
2254     check_z_order(hwnd_C, hwnd_F, hwnd_B, hwnd_F, FALSE);
2255     check_z_order(hwnd_B, hwnd_C, hwnd_E, hwnd_F, FALSE);
2256     check_z_order(hwnd_E, hwnd_B, hwnd_D, 0, FALSE);
2257     check_z_order(hwnd_D, hwnd_E, hwnd_A, 0, FALSE);
2258     check_z_order(hwnd_A, hwnd_D, 0, 0, TRUE);
2259 #endif
2260
2261     DestroyWindow(hwnd_A);
2262     DestroyWindow(hwnd_B);
2263     DestroyWindow(hwnd_C);
2264     DestroyWindow(hwnd_F);
2265 }
2266
2267 static void test_vis_rgn( HWND hwnd )
2268 {
2269     RECT win_rect, rgn_rect;
2270     HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
2271     HDC hdc;
2272
2273     ShowWindow(hwnd,SW_SHOW);
2274     hdc = GetDC( hwnd );
2275     ok( GetRandomRgn( hdc, hrgn, SYSRGN ) != 0, "GetRandomRgn failed\n" );
2276     GetWindowRect( hwnd, &win_rect );
2277     GetRgnBox( hrgn, &rgn_rect );
2278     if (is_win9x)
2279     {
2280         trace("win9x, mapping to screen coords\n");
2281         MapWindowPoints( hwnd, 0, (POINT *)&rgn_rect, 2 );
2282     }
2283     trace("win: %d,%d-%d,%d\n", win_rect.left, win_rect.top, win_rect.right, win_rect.bottom );
2284     trace("rgn: %d,%d-%d,%d\n", rgn_rect.left, rgn_rect.top, rgn_rect.right, rgn_rect.bottom );
2285     ok( win_rect.left <= rgn_rect.left, "rgn left %d not inside win rect %d\n",
2286         rgn_rect.left, win_rect.left );
2287     ok( win_rect.top <= rgn_rect.top, "rgn top %d not inside win rect %d\n",
2288         rgn_rect.top, win_rect.top );
2289     ok( win_rect.right >= rgn_rect.right, "rgn right %d not inside win rect %d\n",
2290         rgn_rect.right, win_rect.right );
2291     ok( win_rect.bottom >= rgn_rect.bottom, "rgn bottom %d not inside win rect %d\n",
2292         rgn_rect.bottom, win_rect.bottom );
2293     ReleaseDC( hwnd, hdc );
2294 }
2295
2296 static LRESULT WINAPI set_focus_on_activate_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
2297 {
2298     if (msg == WM_ACTIVATE && LOWORD(wp) == WA_ACTIVE)
2299     {
2300         HWND child = GetWindow(hwnd, GW_CHILD);
2301         ok(child != 0, "couldn't find child window\n");
2302         SetFocus(child);
2303         ok(GetFocus() == child, "Focus should be on child %p\n", child);
2304         return 0;
2305     }
2306     return DefWindowProc(hwnd, msg, wp, lp);
2307 }
2308
2309 static void test_SetFocus(HWND hwnd)
2310 {
2311     HWND child, child2;
2312     WNDPROC old_wnd_proc;
2313
2314     /* check if we can set focus to non-visible windows */
2315
2316     ShowWindow(hwnd, SW_SHOW);
2317     SetFocus(0);
2318     SetFocus(hwnd);
2319     ok( GetFocus() == hwnd, "Failed to set focus to visible window %p\n", hwnd );
2320     ok( GetWindowLong(hwnd,GWL_STYLE) & WS_VISIBLE, "Window %p not visible\n", hwnd );
2321     ShowWindow(hwnd, SW_HIDE);
2322     SetFocus(0);
2323     SetFocus(hwnd);
2324     ok( GetFocus() == hwnd, "Failed to set focus to invisible window %p\n", hwnd );
2325     ok( !(GetWindowLong(hwnd,GWL_STYLE) & WS_VISIBLE), "Window %p still visible\n", hwnd );
2326     child = CreateWindowExA(0, "static", NULL, WS_CHILD, 0, 0, 0, 0, hwnd, 0, 0, NULL);
2327     assert(child);
2328     SetFocus(child);
2329     ok( GetFocus() == child, "Failed to set focus to invisible child %p\n", child );
2330     ok( !(GetWindowLong(child,GWL_STYLE) & WS_VISIBLE), "Child %p is visible\n", child );
2331     ShowWindow(child, SW_SHOW);
2332     ok( GetWindowLong(child,GWL_STYLE) & WS_VISIBLE, "Child %p is not visible\n", child );
2333     ok( GetFocus() == child, "Focus no longer on child %p\n", child );
2334     ShowWindow(child, SW_HIDE);
2335     ok( !(GetWindowLong(child,GWL_STYLE) & WS_VISIBLE), "Child %p is visible\n", child );
2336     ok( GetFocus() == hwnd, "Focus should be on parent %p, not %p\n", hwnd, GetFocus() );
2337     ShowWindow(child, SW_SHOW);
2338     child2 = CreateWindowExA(0, "static", NULL, WS_CHILD, 0, 0, 0, 0, child, 0, 0, NULL);
2339     assert(child2);
2340     ShowWindow(child2, SW_SHOW);
2341     SetFocus(child2);
2342     ShowWindow(child, SW_HIDE);
2343     ok( !(GetWindowLong(child,GWL_STYLE) & WS_VISIBLE), "Child %p is visible\n", child );
2344     ok( GetFocus() == child2, "Focus should be on %p, not %p\n", child2, GetFocus() );
2345     ShowWindow(child, SW_SHOW);
2346     SetFocus(child);
2347     ok( GetFocus() == child, "Focus should be on child %p\n", child );
2348     SetWindowPos(child,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_HIDEWINDOW);
2349     ok( GetFocus() == child, "Focus should still be on child %p\n", child );
2350
2351     ShowWindow(child, SW_HIDE);
2352     SetFocus(hwnd);
2353     ok( GetFocus() == hwnd, "Focus should be on parent %p, not %p\n", hwnd, GetFocus() );
2354     SetWindowPos(child,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_SHOWWINDOW);
2355     ok( GetFocus() == hwnd, "Focus should still be on parent %p, not %p\n", hwnd, GetFocus() );
2356     ShowWindow(child, SW_HIDE);
2357     ok( GetFocus() == hwnd, "Focus should still be on parent %p, not %p\n", hwnd, GetFocus() );
2358
2359     ShowWindow(hwnd, SW_SHOW);
2360     ShowWindow(child, SW_SHOW);
2361     SetFocus(child);
2362     ok( GetFocus() == child, "Focus should be on child %p\n", child );
2363     EnableWindow(hwnd, FALSE);
2364     ok( GetFocus() == child, "Focus should still be on child %p\n", child );
2365     EnableWindow(hwnd, TRUE);
2366
2367     ok( GetActiveWindow() == hwnd, "parent window %p should be active\n", hwnd);
2368     ShowWindow(hwnd, SW_SHOWMINIMIZED);
2369     ok( GetActiveWindow() == hwnd, "parent window %p should be active\n", hwnd);
2370 todo_wine
2371     ok( GetFocus() != child, "Focus should not be on child %p\n", child );
2372     ok( GetFocus() != hwnd, "Focus should not be on parent %p\n", hwnd );
2373     ShowWindow(hwnd, SW_RESTORE);
2374     ok( GetActiveWindow() == hwnd, "parent window %p should be active\n", hwnd);
2375     ok( GetFocus() == hwnd, "Focus should be on parent %p\n", hwnd );
2376     ShowWindow(hwnd, SW_SHOWMINIMIZED);
2377     ok( GetActiveWindow() == hwnd, "parent window %p should be active\n", hwnd);
2378     ok( GetFocus() != child, "Focus should not be on child %p\n", child );
2379 todo_wine
2380     ok( GetFocus() != hwnd, "Focus should not be on parent %p\n", hwnd );
2381     old_wnd_proc = (WNDPROC)SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)set_focus_on_activate_proc);
2382     ShowWindow(hwnd, SW_RESTORE);
2383     ok( GetActiveWindow() == hwnd, "parent window %p should be active\n", hwnd);
2384 todo_wine
2385     ok( GetFocus() == child, "Focus should be on child %p, not %p\n", child, GetFocus() );
2386
2387     SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)old_wnd_proc);
2388
2389     DestroyWindow( child2 );
2390     DestroyWindow( child );
2391 }
2392
2393 #define check_wnd_state(a,b,c,d) check_wnd_state_(__FILE__,__LINE__,a,b,c,d)
2394 static void check_wnd_state_(const char *file, int line,
2395                              HWND active, HWND foreground, HWND focus, HWND capture)
2396 {
2397     ok_(file, line)(active == GetActiveWindow(), "GetActiveWindow() = %p\n", GetActiveWindow());
2398     /* only check foreground if it belongs to the current thread */
2399     /* foreground can be moved to a different app pretty much at any time */
2400     if (foreground && GetForegroundWindow() &&
2401         GetWindowThreadProcessId(GetForegroundWindow(), NULL) == GetCurrentThreadId())
2402         ok_(file, line)(foreground == GetForegroundWindow(), "GetForegroundWindow() = %p\n", GetForegroundWindow());
2403     ok_(file, line)(focus == GetFocus(), "GetFocus() = %p\n", GetFocus());
2404     ok_(file, line)(capture == GetCapture(), "GetCapture() = %p\n", GetCapture());
2405 }
2406
2407 static void test_SetActiveWindow(HWND hwnd)
2408 {
2409     HWND hwnd2;
2410
2411     flush_events( TRUE );
2412     ShowWindow(hwnd, SW_HIDE);
2413     SetFocus(0);
2414     SetActiveWindow(0);
2415     check_wnd_state(0, 0, 0, 0);
2416
2417     /*trace("testing SetActiveWindow %p\n", hwnd);*/
2418
2419     ShowWindow(hwnd, SW_SHOW);
2420     check_wnd_state(hwnd, hwnd, hwnd, 0);
2421
2422     hwnd2 = SetActiveWindow(0);
2423     ok(hwnd2 == hwnd, "SetActiveWindow returned %p instead of %p\n", hwnd2, hwnd);
2424     if (!GetActiveWindow())  /* doesn't always work on vista */
2425     {
2426         check_wnd_state(0, 0, 0, 0);
2427         hwnd2 = SetActiveWindow(hwnd);
2428         ok(hwnd2 == 0, "SetActiveWindow returned %p instead of 0\n", hwnd2);
2429     }
2430     check_wnd_state(hwnd, hwnd, hwnd, 0);
2431
2432     SetWindowPos(hwnd,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_HIDEWINDOW);
2433     check_wnd_state(hwnd, hwnd, hwnd, 0);
2434
2435     SetWindowPos(hwnd,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_SHOWWINDOW);
2436     check_wnd_state(hwnd, hwnd, hwnd, 0);
2437
2438     ShowWindow(hwnd, SW_HIDE);
2439     check_wnd_state(0, 0, 0, 0);
2440
2441     /*trace("testing SetActiveWindow on an invisible window %p\n", hwnd);*/
2442     SetActiveWindow(hwnd);
2443     check_wnd_state(hwnd, hwnd, hwnd, 0);
2444     
2445     ShowWindow(hwnd, SW_SHOW);
2446     check_wnd_state(hwnd, hwnd, hwnd, 0);
2447
2448     hwnd2 = CreateWindowExA(0, "static", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 0, 0, hwnd, 0, 0, NULL);
2449     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2450
2451     DestroyWindow(hwnd2);
2452     check_wnd_state(hwnd, hwnd, hwnd, 0);
2453
2454     hwnd2 = CreateWindowExA(0, "static", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 0, 0, hwnd, 0, 0, NULL);
2455     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2456
2457     SetWindowPos(hwnd2,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_HIDEWINDOW);
2458     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2459
2460     DestroyWindow(hwnd2);
2461     check_wnd_state(hwnd, hwnd, hwnd, 0);
2462 }
2463
2464 static void test_SetForegroundWindow(HWND hwnd)
2465 {
2466     BOOL ret;
2467     HWND hwnd2;
2468
2469     flush_events( TRUE );
2470     ShowWindow(hwnd, SW_HIDE);
2471     SetFocus(0);
2472     SetActiveWindow(0);
2473     check_wnd_state(0, 0, 0, 0);
2474
2475     /*trace("testing SetForegroundWindow %p\n", hwnd);*/
2476
2477     ShowWindow(hwnd, SW_SHOW);
2478     check_wnd_state(hwnd, hwnd, hwnd, 0);
2479
2480     hwnd2 = SetActiveWindow(0);
2481     ok(hwnd2 == hwnd, "SetActiveWindow(0) returned %p instead of %p\n", hwnd2, hwnd);
2482     if (GetActiveWindow() == hwnd)  /* doesn't always work on vista */
2483         check_wnd_state(hwnd, hwnd, hwnd, 0);
2484     else
2485         check_wnd_state(0, 0, 0, 0);
2486
2487     ret = SetForegroundWindow(hwnd);
2488     if (!ret)
2489     {
2490         skip( "SetForegroundWindow not working\n" );
2491         return;
2492     }
2493     check_wnd_state(hwnd, hwnd, hwnd, 0);
2494
2495     SetLastError(0xdeadbeef);
2496     ret = SetForegroundWindow(0);
2497     ok(!ret, "SetForegroundWindow returned TRUE instead of FALSE\n");
2498     ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE ||
2499        broken(GetLastError() == 0xdeadbeef),  /* win9x */
2500        "got error %d expected ERROR_INVALID_WINDOW_HANDLE\n", GetLastError());
2501     check_wnd_state(hwnd, hwnd, hwnd, 0);
2502
2503     SetWindowPos(hwnd,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_HIDEWINDOW);
2504     check_wnd_state(hwnd, hwnd, hwnd, 0);
2505
2506     SetWindowPos(hwnd,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_SHOWWINDOW);
2507     check_wnd_state(hwnd, hwnd, hwnd, 0);
2508
2509     hwnd2 = GetForegroundWindow();
2510     ok(hwnd2 == hwnd, "Wrong foreground window %p\n", hwnd2);
2511     ok(SetForegroundWindow( GetDesktopWindow() ), "SetForegroundWindow(desktop) error: %d\n", GetLastError());
2512     hwnd2 = GetForegroundWindow();
2513     ok(hwnd2 != hwnd, "Wrong foreground window %p\n", hwnd2);
2514
2515     ShowWindow(hwnd, SW_HIDE);
2516     check_wnd_state(0, 0, 0, 0);
2517
2518     /*trace("testing SetForegroundWindow on an invisible window %p\n", hwnd);*/
2519     ret = SetForegroundWindow(hwnd);
2520     ok(ret || broken(!ret), /* win98 */ "SetForegroundWindow returned FALSE instead of TRUE\n");
2521     check_wnd_state(hwnd, hwnd, hwnd, 0);
2522
2523     ShowWindow(hwnd, SW_SHOW);
2524     check_wnd_state(hwnd, hwnd, hwnd, 0);
2525
2526     hwnd2 = CreateWindowExA(0, "static", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 0, 0, hwnd, 0, 0, NULL);
2527     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2528
2529     DestroyWindow(hwnd2);
2530     check_wnd_state(hwnd, hwnd, hwnd, 0);
2531
2532     hwnd2 = CreateWindowExA(0, "static", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 0, 0, hwnd, 0, 0, NULL);
2533     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2534
2535     SetWindowPos(hwnd2,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_HIDEWINDOW);
2536     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2537
2538     DestroyWindow(hwnd2);
2539     check_wnd_state(hwnd, hwnd, hwnd, 0);
2540 }
2541
2542 static WNDPROC old_button_proc;
2543
2544 static LRESULT WINAPI button_hook_proc(HWND button, UINT msg, WPARAM wparam, LPARAM lparam)
2545 {
2546     LRESULT ret;
2547     USHORT key_state;
2548
2549     key_state = GetKeyState(VK_LBUTTON);
2550     ok(!(key_state & 0x8000), "VK_LBUTTON should not be pressed, state %04x\n", key_state);
2551
2552     ret = CallWindowProcA(old_button_proc, button, msg, wparam, lparam);
2553
2554     if (msg == WM_LBUTTONDOWN)
2555     {
2556         HWND hwnd, capture;
2557
2558         check_wnd_state(button, button, button, button);
2559
2560         hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0, 0, 10, 10, 0, 0, 0, NULL);
2561         assert(hwnd);
2562         trace("hwnd %p\n", hwnd);
2563
2564         check_wnd_state(button, button, button, button);
2565
2566         ShowWindow(hwnd, SW_SHOWNOACTIVATE);
2567
2568         check_wnd_state(button, button, button, button);
2569
2570         DestroyWindow(hwnd);
2571
2572         hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0, 0, 10, 10, 0, 0, 0, NULL);
2573         assert(hwnd);
2574         trace("hwnd %p\n", hwnd);
2575
2576         check_wnd_state(button, button, button, button);
2577
2578         /* button wnd proc should release capture on WM_KILLFOCUS if it does
2579          * match internal button state.
2580          */
2581         SendMessage(button, WM_KILLFOCUS, 0, 0);
2582         check_wnd_state(button, button, button, 0);
2583
2584         ShowWindow(hwnd, SW_SHOW);
2585         check_wnd_state(hwnd, hwnd, hwnd, 0);
2586
2587         capture = SetCapture(hwnd);
2588         ok(capture == 0, "SetCapture() = %p\n", capture);
2589
2590         check_wnd_state(hwnd, hwnd, hwnd, hwnd);
2591
2592         DestroyWindow(hwnd);
2593
2594         check_wnd_state(button, 0, button, 0);
2595     }
2596
2597     return ret;
2598 }
2599
2600 static void test_capture_1(void)
2601 {
2602     HWND button, capture, oldFocus, oldActive;
2603
2604     oldFocus = GetFocus();
2605     oldActive = GetActiveWindow();
2606
2607     capture = GetCapture();
2608     ok(capture == 0, "GetCapture() = %p\n", capture);
2609
2610     button = CreateWindowExA(0, "button", NULL, WS_POPUP | WS_VISIBLE, 0, 0, 10, 10, 0, 0, 0, NULL);
2611     assert(button);
2612     trace("button %p\n", button);
2613
2614     old_button_proc = (WNDPROC)SetWindowLongPtrA(button, GWLP_WNDPROC, (LONG_PTR)button_hook_proc);
2615
2616     SendMessageA(button, WM_LBUTTONDOWN, 0, 0);
2617
2618     capture = SetCapture(button);
2619     ok(capture == 0, "SetCapture() = %p\n", capture);
2620     check_wnd_state(button, 0, button, button);
2621
2622     DestroyWindow(button);
2623     check_wnd_state(oldActive, 0, oldFocus, 0);
2624 }
2625
2626 static void test_capture_2(void)
2627 {
2628     HWND button, hwnd, capture, oldFocus, oldActive;
2629
2630     oldFocus = GetFocus();
2631     oldActive = GetActiveWindow();
2632     check_wnd_state(oldActive, 0, oldFocus, 0);
2633
2634     button = CreateWindowExA(0, "button", NULL, WS_POPUP | WS_VISIBLE, 0, 0, 10, 10, 0, 0, 0, NULL);
2635     assert(button);
2636     trace("button %p\n", button);
2637
2638     check_wnd_state(button, button, button, 0);
2639
2640     capture = SetCapture(button);
2641     ok(capture == 0, "SetCapture() = %p\n", capture);
2642
2643     check_wnd_state(button, button, button, button);
2644
2645     /* button wnd proc should ignore WM_KILLFOCUS if it doesn't match
2646      * internal button state.
2647      */
2648     SendMessage(button, WM_KILLFOCUS, 0, 0);
2649     check_wnd_state(button, button, button, button);
2650
2651     hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0, 0, 10, 10, 0, 0, 0, NULL);
2652     assert(hwnd);
2653     trace("hwnd %p\n", hwnd);
2654
2655     check_wnd_state(button, button, button, button);
2656
2657     ShowWindow(hwnd, SW_SHOWNOACTIVATE);
2658
2659     check_wnd_state(button, button, button, button);
2660
2661     DestroyWindow(hwnd);
2662
2663     hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0, 0, 10, 10, 0, 0, 0, NULL);
2664     assert(hwnd);
2665     trace("hwnd %p\n", hwnd);
2666
2667     check_wnd_state(button, button, button, button);
2668
2669     ShowWindow(hwnd, SW_SHOW);
2670
2671     check_wnd_state(hwnd, hwnd, hwnd, button);
2672
2673     capture = SetCapture(hwnd);
2674     ok(capture == button, "SetCapture() = %p\n", capture);
2675
2676     check_wnd_state(hwnd, hwnd, hwnd, hwnd);
2677
2678     DestroyWindow(hwnd);
2679     check_wnd_state(button, button, button, 0);
2680
2681     DestroyWindow(button);
2682     check_wnd_state(oldActive, 0, oldFocus, 0);
2683 }
2684
2685 static void test_capture_3(HWND hwnd1, HWND hwnd2)
2686 {
2687     BOOL ret;
2688
2689     ShowWindow(hwnd1, SW_HIDE);
2690     ShowWindow(hwnd2, SW_HIDE);
2691
2692     ok(!IsWindowVisible(hwnd1), "%p should be invisible\n", hwnd1);
2693     ok(!IsWindowVisible(hwnd2), "%p should be invisible\n", hwnd2);
2694
2695     SetCapture(hwnd1);
2696     check_wnd_state(0, 0, 0, hwnd1);
2697
2698     SetCapture(hwnd2);
2699     check_wnd_state(0, 0, 0, hwnd2);
2700
2701     ShowWindow(hwnd1, SW_SHOW);
2702     check_wnd_state(hwnd1, hwnd1, hwnd1, hwnd2);
2703
2704     ret = ReleaseCapture();
2705     ok (ret, "releasecapture did not return TRUE.\n");
2706     ret = ReleaseCapture();
2707     ok (ret, "releasecapture did not return TRUE after second try.\n");
2708 }
2709
2710 static LRESULT CALLBACK test_capture_4_proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2711 {
2712     GUITHREADINFO gti;
2713     HWND cap_wnd, cap_wnd2, set_cap_wnd;
2714     BOOL status;
2715     switch (msg)
2716     {
2717         case WM_CAPTURECHANGED:
2718
2719             /* now try to release capture from menu. this should fail */
2720             if (pGetGUIThreadInfo)
2721             {
2722                 memset(&gti, 0, sizeof(GUITHREADINFO));
2723                 gti.cbSize = sizeof(GUITHREADINFO);
2724                 status = pGetGUIThreadInfo(GetCurrentThreadId(), &gti);
2725                 ok(status, "GetGUIThreadInfo() failed!\n");
2726                 ok(gti.flags & GUI_INMENUMODE, "Thread info incorrect (flags=%08X)!\n", gti.flags);
2727             }
2728             cap_wnd = GetCapture();
2729
2730             /* check that re-setting the capture for the menu fails */
2731             set_cap_wnd = SetCapture(cap_wnd);
2732             ok(!set_cap_wnd || broken(set_cap_wnd == cap_wnd), /* nt4 */
2733                "SetCapture should have failed!\n");
2734             if (set_cap_wnd)
2735             {
2736                 DestroyWindow(hWnd);
2737                 break;
2738             }
2739
2740             /* check that SetCapture fails for another window and that it does not touch the error code */
2741             set_cap_wnd = SetCapture(hWnd);
2742             ok(!set_cap_wnd, "SetCapture should have failed!\n");
2743
2744             /* check that ReleaseCapture fails and does not touch the error code */
2745             status = ReleaseCapture();
2746             ok(!status, "ReleaseCapture should have failed!\n");
2747
2748             /* check that thread info did not change */
2749             if (pGetGUIThreadInfo)
2750             {
2751                 memset(&gti, 0, sizeof(GUITHREADINFO));
2752                 gti.cbSize = sizeof(GUITHREADINFO);
2753                 status = pGetGUIThreadInfo(GetCurrentThreadId(), &gti);
2754                 ok(status, "GetGUIThreadInfo() failed!\n");
2755                 ok(gti.flags & GUI_INMENUMODE, "Thread info incorrect (flags=%08X)!\n", gti.flags);
2756             }
2757
2758             /* verify that no capture change took place */
2759             cap_wnd2 = GetCapture();
2760             ok(cap_wnd2 == cap_wnd, "Capture changed!\n");
2761
2762             /* we are done. kill the window */
2763             DestroyWindow(hWnd);
2764             break;
2765
2766         default:
2767             return( DefWindowProcA( hWnd, msg, wParam, lParam ) );
2768     }
2769     return 0;
2770 }
2771
2772 /* Test that no-one can mess around with the current capture while a menu is open */
2773 static void test_capture_4(void)
2774 {
2775     BOOL ret;
2776     HMENU hmenu;
2777     HWND hwnd;
2778     WNDCLASSA wclass;
2779     HINSTANCE hInstance = GetModuleHandleA( NULL );
2780
2781     if (!pGetGUIThreadInfo)
2782     {
2783         win_skip("GetGUIThreadInfo is not available\n");
2784         return;
2785     }
2786     wclass.lpszClassName = "TestCapture4Class";
2787     wclass.style         = CS_HREDRAW | CS_VREDRAW;
2788     wclass.lpfnWndProc   = test_capture_4_proc;
2789     wclass.hInstance     = hInstance;
2790     wclass.hIcon         = LoadIconA( 0, IDI_APPLICATION );
2791     wclass.hCursor       = LoadCursorA( NULL, IDC_ARROW );
2792     wclass.hbrBackground = (HBRUSH)( COLOR_WINDOW + 1 );
2793     wclass.lpszMenuName  = 0;
2794     wclass.cbClsExtra    = 0;
2795     wclass.cbWndExtra    = 0;
2796     assert (RegisterClassA( &wclass ));
2797     assert (hwnd = CreateWindowA( wclass.lpszClassName, "MenuTest",
2798                                   WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0,
2799                                   400, 200, NULL, NULL, hInstance, NULL) );
2800     ok(hwnd != NULL, "CreateWindowEx failed with error %d\n", GetLastError());
2801     if (!hwnd) return;
2802     hmenu = CreatePopupMenu();
2803
2804     ret = AppendMenuA( hmenu, MF_STRING, 1, "winetest2");
2805     ok( ret, "AppendMenuA has failed!\n");
2806
2807     /* set main window to have initial capture */
2808     SetCapture(hwnd);
2809
2810     if (is_win9x)
2811     {
2812         win_skip("TrackPopupMenu test crashes on Win9x/WinMe\n");
2813     }
2814     else
2815     {
2816         /* create popup (it will self-destruct) */
2817         ret = TrackPopupMenu(hmenu, TPM_RETURNCMD, 100, 100, 0, hwnd, NULL);
2818         ok( ret == 0, "TrackPopupMenu returned %d expected zero\n", ret);
2819     }
2820
2821     /* clean up */
2822     DestroyMenu(hmenu);
2823     DestroyWindow(hwnd);
2824 }
2825
2826 /* PeekMessage wrapper that ignores the messages we don't care about */
2827 static BOOL peek_message( MSG *msg )
2828 {
2829     BOOL ret;
2830     do
2831     {
2832         ret = PeekMessageA(msg, 0, 0, 0, PM_REMOVE);
2833     } while (ret && (msg->message == WM_TIMER || ignore_message(msg->message)));
2834     return ret;
2835 }
2836
2837 static void test_keyboard_input(HWND hwnd)
2838 {
2839     MSG msg;
2840     BOOL ret;
2841
2842     flush_events( TRUE );
2843     SetWindowPos(hwnd, 0, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE|SWP_SHOWWINDOW);
2844     UpdateWindow(hwnd);
2845     flush_events( TRUE );
2846
2847     ok(GetActiveWindow() == hwnd, "wrong active window %p\n", GetActiveWindow());
2848
2849     SetFocus(hwnd);
2850     ok(GetFocus() == hwnd, "wrong focus window %p\n", GetFocus());
2851
2852     flush_events( TRUE );
2853
2854     PostMessageA(hwnd, WM_KEYDOWN, 0, 0);
2855     ret = peek_message(&msg);
2856     ok( ret, "no message available\n");
2857     ok(msg.hwnd == hwnd && msg.message == WM_KEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2858     ret = peek_message(&msg);
2859     ok( !ret, "message %04x available\n", msg.message);
2860
2861     ok(GetFocus() == hwnd, "wrong focus window %p\n", GetFocus());
2862
2863     PostThreadMessageA(GetCurrentThreadId(), WM_KEYDOWN, 0, 0);
2864     ret = peek_message(&msg);
2865     ok(ret, "no message available\n");
2866     ok(!msg.hwnd && msg.message == WM_KEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2867     ret = peek_message(&msg);
2868     ok( !ret, "message %04x available\n", msg.message);
2869
2870     ok(GetFocus() == hwnd, "wrong focus window %p\n", GetFocus());
2871
2872     keybd_event(VK_SPACE, 0, 0, 0);
2873     if (!peek_message(&msg))
2874     {
2875         skip( "keybd_event didn't work, skipping keyboard test\n" );
2876         return;
2877     }
2878     ok(msg.hwnd == hwnd && msg.message == WM_KEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2879     ret = peek_message(&msg);
2880     ok( !ret, "message %04x available\n", msg.message);
2881
2882     SetFocus(0);
2883     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
2884
2885     flush_events( TRUE );
2886
2887     PostMessageA(hwnd, WM_KEYDOWN, 0, 0);
2888     ret = peek_message(&msg);
2889     ok(ret, "no message available\n");
2890     ok(msg.hwnd == hwnd && msg.message == WM_KEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2891     ret = peek_message(&msg);
2892     ok( !ret, "message %04x available\n", msg.message);
2893
2894     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
2895
2896     PostThreadMessageA(GetCurrentThreadId(), WM_KEYDOWN, 0, 0);
2897     ret = peek_message(&msg);
2898     ok(ret, "no message available\n");
2899     ok(!msg.hwnd && msg.message == WM_KEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2900     ret = peek_message(&msg);
2901     ok( !ret, "message %04x available\n", msg.message);
2902
2903     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
2904
2905     keybd_event(VK_SPACE, 0, 0, 0);
2906     ret = peek_message(&msg);
2907     ok(ret, "no message available\n");
2908     ok(msg.hwnd == hwnd && msg.message == WM_SYSKEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
2909     ret = peek_message(&msg);
2910     ok( !ret, "message %04x available\n", msg.message);
2911 }
2912
2913 static BOOL wait_for_message( MSG *msg )
2914 {
2915     BOOL ret;
2916
2917     for (;;)
2918     {
2919         ret = peek_message(msg);
2920         if (ret)
2921         {
2922             if (msg->message == WM_PAINT) DispatchMessage(msg);
2923             else break;
2924         }
2925         else if (MsgWaitForMultipleObjects( 0, NULL, FALSE, 100, QS_ALLINPUT ) == WAIT_TIMEOUT) break;
2926     }
2927     if (!ret) msg->message = 0;
2928     return ret;
2929 }
2930
2931 static void test_mouse_input(HWND hwnd)
2932 {
2933     RECT rc;
2934     POINT pt;
2935     int x, y;
2936     HWND popup;
2937     MSG msg;
2938     BOOL ret;
2939     LRESULT res;
2940
2941     ShowWindow(hwnd, SW_SHOW);
2942     UpdateWindow(hwnd);
2943     SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE );
2944
2945     GetWindowRect(hwnd, &rc);
2946     trace("main window %p: (%d,%d)-(%d,%d)\n", hwnd, rc.left, rc.top, rc.right, rc.bottom);
2947
2948     popup = CreateWindowExA(0, "MainWindowClass", NULL, WS_POPUP,
2949                             rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top,
2950                             hwnd, 0, 0, NULL);
2951     assert(popup != 0);
2952     ShowWindow(popup, SW_SHOW);
2953     UpdateWindow(popup);
2954     SetWindowPos( popup, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE );
2955
2956     GetWindowRect(popup, &rc);
2957     trace("popup window %p: (%d,%d)-(%d,%d)\n", popup, rc.left, rc.top, rc.right, rc.bottom);
2958
2959     x = rc.left + (rc.right - rc.left) / 2;
2960     y = rc.top + (rc.bottom - rc.top) / 2;
2961     trace("setting cursor to (%d,%d)\n", x, y);
2962
2963     SetCursorPos(x, y);
2964     GetCursorPos(&pt);
2965     if (x != pt.x || y != pt.y)
2966     {
2967         skip( "failed to set mouse position, skipping mouse input tests\n" );
2968         goto done;
2969     }
2970
2971     flush_events( TRUE );
2972
2973     /* Check that setting the same position may generate WM_MOUSEMOVE */
2974     SetCursorPos(x, y);
2975     msg.message = 0;
2976     ret = peek_message(&msg);
2977     if (ret)
2978     {
2979         ok(msg.hwnd == popup && msg.message == WM_MOUSEMOVE, "hwnd %p message %04x\n",
2980            msg.hwnd, msg.message);
2981         ok(msg.pt.x == x && msg.pt.y == y, "wrong message coords (%d,%d)/(%d,%d)\n",
2982            x, y, msg.pt.x, msg.pt.y);
2983     }
2984
2985     /* force the system to update its internal queue mouse position,
2986      * otherwise it won't generate relative mouse movements below.
2987      */
2988     mouse_event(MOUSEEVENTF_MOVE, -1, -1, 0, 0);
2989     flush_events( TRUE );
2990
2991     msg.message = 0;
2992     mouse_event(MOUSEEVENTF_MOVE, 1, 1, 0, 0);
2993     flush_events( FALSE );
2994     /* FIXME: SetCursorPos in Wine generates additional WM_MOUSEMOVE message */
2995     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE))
2996     {
2997         if (msg.message == WM_TIMER || ignore_message(msg.message)) continue;
2998         ok(msg.hwnd == popup && msg.message == WM_MOUSEMOVE,
2999            "hwnd %p message %04x\n", msg.hwnd, msg.message);
3000         DispatchMessage(&msg);
3001     }
3002     ret = peek_message(&msg);
3003     ok( !ret, "message %04x available\n", msg.message);
3004
3005     mouse_event(MOUSEEVENTF_MOVE, -1, -1, 0, 0);
3006     ShowWindow(popup, SW_HIDE);
3007     ret = wait_for_message( &msg );
3008     if (ret)
3009         ok(msg.hwnd == hwnd && msg.message == WM_MOUSEMOVE, "hwnd %p message %04x\n", msg.hwnd, msg.message);
3010     flush_events( TRUE );
3011
3012     mouse_event(MOUSEEVENTF_MOVE, 1, 1, 0, 0);
3013     ShowWindow(hwnd, SW_HIDE);
3014     ret = wait_for_message( &msg );
3015     ok( !ret, "message %04x available\n", msg.message);
3016     flush_events( TRUE );
3017
3018     /* test mouse clicks */
3019
3020     ShowWindow(hwnd, SW_SHOW);
3021     SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE );
3022     flush_events( TRUE );
3023     ShowWindow(popup, SW_SHOW);
3024     SetWindowPos( popup, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE );
3025     flush_events( TRUE );
3026
3027     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
3028     mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
3029     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
3030     mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
3031
3032     ret = wait_for_message( &msg );
3033     if (!ret)
3034     {
3035         skip( "simulating mouse click doesn't work, skipping mouse button tests\n" );
3036         goto done;
3037     }
3038     if (msg.message == WM_MOUSEMOVE)  /* win2k has an extra WM_MOUSEMOVE here */
3039     {
3040         ret = wait_for_message( &msg );
3041         ok(ret, "no message available\n");
3042     }
3043
3044     ok(msg.hwnd == popup && msg.message == WM_LBUTTONDOWN, "hwnd %p/%p message %04x\n",
3045        msg.hwnd, popup, msg.message);
3046
3047     ret = wait_for_message( &msg );
3048     ok(ret, "no message available\n");
3049     ok(msg.hwnd == popup && msg.message == WM_LBUTTONUP, "hwnd %p/%p message %04x\n",
3050        msg.hwnd, popup, msg.message);
3051
3052     ret = wait_for_message( &msg );
3053     ok(ret, "no message available\n");
3054     ok(msg.hwnd == popup && msg.message == WM_LBUTTONDBLCLK, "hwnd %p/%p message %04x\n",
3055        msg.hwnd, popup, msg.message);
3056
3057     ret = wait_for_message( &msg );
3058     ok(ret, "no message available\n");
3059     ok(msg.hwnd == popup && msg.message == WM_LBUTTONUP, "hwnd %p/%p message %04x\n",
3060        msg.hwnd, popup, msg.message);
3061
3062     ret = peek_message(&msg);
3063     ok(!ret, "message %04x available\n", msg.message);
3064
3065     ShowWindow(popup, SW_HIDE);
3066     flush_events( TRUE );
3067
3068     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
3069     mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
3070     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
3071     mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
3072
3073     ret = wait_for_message( &msg );
3074     ok(ret, "no message available\n");
3075     ok(msg.hwnd == hwnd && msg.message == WM_LBUTTONDOWN, "hwnd %p/%p message %04x\n",
3076        msg.hwnd, hwnd, msg.message);
3077     ret = wait_for_message( &msg );
3078     ok(ret, "no message available\n");
3079     ok(msg.hwnd == hwnd && msg.message == WM_LBUTTONUP, "hwnd %p/%p message %04x\n",
3080        msg.hwnd, hwnd, msg.message);
3081
3082     test_lbuttondown_flag = TRUE;
3083     SendMessageA(hwnd, WM_COMMAND, (WPARAM)popup, 0);
3084     test_lbuttondown_flag = FALSE;
3085
3086     ret = wait_for_message( &msg );
3087     ok(ret, "no message available\n");
3088     ok(msg.hwnd == popup && msg.message == WM_LBUTTONDOWN, "hwnd %p/%p message %04x\n",
3089        msg.hwnd, popup, msg.message);
3090     ok(peek_message(&msg), "no message available\n");
3091     ok(msg.hwnd == popup && msg.message == WM_LBUTTONUP, "hwnd %p/%p message %04x\n",
3092        msg.hwnd, popup, msg.message);
3093     ok(peek_message(&msg), "no message available\n");
3094
3095     /* Test WM_MOUSEACTIVATE */
3096 #define TEST_MOUSEACTIVATE(A,B)                                                          \
3097        res = SendMessageA(hwnd, WM_MOUSEACTIVATE, (WPARAM)hwnd, (LPARAM)MAKELRESULT(A,0));   \
3098        ok(res == B, "WM_MOUSEACTIVATE for %s returned %ld\n", #A, res);
3099        
3100     TEST_MOUSEACTIVATE(HTERROR,MA_ACTIVATE);
3101     TEST_MOUSEACTIVATE(HTTRANSPARENT,MA_ACTIVATE);
3102     TEST_MOUSEACTIVATE(HTNOWHERE,MA_ACTIVATE);
3103     TEST_MOUSEACTIVATE(HTCLIENT,MA_ACTIVATE);
3104     TEST_MOUSEACTIVATE(HTCAPTION,MA_ACTIVATE);
3105     TEST_MOUSEACTIVATE(HTSYSMENU,MA_ACTIVATE);
3106     TEST_MOUSEACTIVATE(HTSIZE,MA_ACTIVATE);
3107     TEST_MOUSEACTIVATE(HTMENU,MA_ACTIVATE);
3108     TEST_MOUSEACTIVATE(HTHSCROLL,MA_ACTIVATE);
3109     TEST_MOUSEACTIVATE(HTVSCROLL,MA_ACTIVATE);
3110     TEST_MOUSEACTIVATE(HTMINBUTTON,MA_ACTIVATE);
3111     TEST_MOUSEACTIVATE(HTMAXBUTTON,MA_ACTIVATE);
3112     TEST_MOUSEACTIVATE(HTLEFT,MA_ACTIVATE);
3113     TEST_MOUSEACTIVATE(HTRIGHT,MA_ACTIVATE);
3114     TEST_MOUSEACTIVATE(HTTOP,MA_ACTIVATE);
3115     TEST_MOUSEACTIVATE(HTTOPLEFT,MA_ACTIVATE);
3116     TEST_MOUSEACTIVATE(HTTOPRIGHT,MA_ACTIVATE);
3117     TEST_MOUSEACTIVATE(HTBOTTOM,MA_ACTIVATE);
3118     TEST_MOUSEACTIVATE(HTBOTTOMLEFT,MA_ACTIVATE);
3119     TEST_MOUSEACTIVATE(HTBOTTOMRIGHT,MA_ACTIVATE);
3120     TEST_MOUSEACTIVATE(HTBORDER,MA_ACTIVATE);
3121     TEST_MOUSEACTIVATE(HTOBJECT,MA_ACTIVATE);
3122     TEST_MOUSEACTIVATE(HTCLOSE,MA_ACTIVATE);
3123     TEST_MOUSEACTIVATE(HTHELP,MA_ACTIVATE);
3124
3125 done:
3126     /* Clear any messages left behind by WM_MOUSEACTIVATE tests */
3127     flush_events( TRUE );
3128
3129     DestroyWindow(popup);
3130 }
3131
3132 static void test_validatergn(HWND hwnd)
3133 {
3134     HWND child;
3135     RECT rc, rc2;
3136     HRGN rgn;
3137     int ret;
3138     child = CreateWindowExA(0, "static", NULL, WS_CHILD| WS_VISIBLE, 10, 10, 10, 10, hwnd, 0, 0, NULL);
3139     ShowWindow(hwnd, SW_SHOW);
3140     UpdateWindow( hwnd);
3141     /* test that ValidateRect validates children*/
3142     InvalidateRect( child, NULL, 1);
3143     GetWindowRect( child, &rc);
3144     MapWindowPoints( NULL, hwnd, (POINT*) &rc, 2);
3145     ret = GetUpdateRect( child, &rc2, 0);
3146     ok( rc2.right > rc2.left && rc2.bottom > rc2.top,
3147             "Update rectangle is empty!\n");
3148     ValidateRect( hwnd, &rc);
3149     ret = GetUpdateRect( child, &rc2, 0);
3150     ok( rc2.left == 0 && rc2.top == 0 && rc2.right == 0 && rc2.bottom == 0,
3151             "Update rectangle %d,%d-%d,%d is not empty!\n", rc2.left, rc2.top,
3152             rc2.right, rc2.bottom);
3153
3154     /* now test ValidateRgn */
3155     InvalidateRect( child, NULL, 1);
3156     GetWindowRect( child, &rc);
3157     MapWindowPoints( NULL, hwnd, (POINT*) &rc, 2);
3158     rgn = CreateRectRgnIndirect( &rc);
3159     ValidateRgn( hwnd, rgn);
3160     ret = GetUpdateRect( child, &rc2, 0);
3161     ok( rc2.left == 0 && rc2.top == 0 && rc2.right == 0 && rc2.bottom == 0,
3162             "Update rectangle %d,%d-%d,%d is not empty!\n", rc2.left, rc2.top,
3163             rc2.right, rc2.bottom);
3164
3165     DeleteObject( rgn);
3166     DestroyWindow( child );
3167 }
3168
3169 static void nccalchelper(HWND hwnd, INT x, INT y, RECT *prc)
3170 {
3171     RECT rc;
3172     MoveWindow( hwnd, 0, 0, x, y, 0);
3173     GetWindowRect( hwnd, prc);
3174     rc = *prc;
3175     DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)prc);
3176     trace("window rect is %d,%d - %d,%d, nccalc rect is %d,%d - %d,%d\n",
3177           rc.left,rc.top,rc.right,rc.bottom, prc->left,prc->top,prc->right,prc->bottom);
3178 }
3179
3180 static void test_nccalcscroll(HWND parent)
3181 {
3182     RECT rc1;
3183     INT sbheight = GetSystemMetrics( SM_CYHSCROLL);
3184     INT sbwidth = GetSystemMetrics( SM_CXVSCROLL);
3185     HWND hwnd = CreateWindowExA(0, "static", NULL, 
3186             WS_CHILD| WS_VISIBLE | WS_VSCROLL | WS_HSCROLL , 
3187             10, 10, 200, 200, parent, 0, 0, NULL); 
3188     ShowWindow( parent, SW_SHOW);
3189     UpdateWindow( parent);
3190
3191     /* test window too low for a horizontal scroll bar */
3192     nccalchelper( hwnd, 100, sbheight, &rc1);
3193     ok( rc1.bottom - rc1.top == sbheight, "Height should be %d size is %d,%d - %d,%d\n",
3194             sbheight, rc1.left, rc1.top, rc1.right, rc1.bottom);
3195
3196     /* test window just high enough for a horizontal scroll bar */
3197     nccalchelper( hwnd, 100, sbheight + 1, &rc1);
3198     ok( rc1.bottom - rc1.top == 1, "Height should be %d size is %d,%d - %d,%d\n",
3199             1, rc1.left, rc1.top, rc1.right, rc1.bottom);
3200
3201     /* test window too narrow for a vertical scroll bar */
3202     nccalchelper( hwnd, sbwidth - 1, 100, &rc1);
3203     ok( rc1.right - rc1.left == sbwidth - 1 , "Width should be %d size is %d,%d - %d,%d\n",
3204             sbwidth - 1, rc1.left, rc1.top, rc1.right, rc1.bottom);
3205
3206     /* test window just wide enough for a vertical scroll bar */
3207     nccalchelper( hwnd, sbwidth, 100, &rc1);
3208     ok( rc1.right - rc1.left == 0, "Width should be %d size is %d,%d - %d,%d\n",
3209             0, rc1.left, rc1.top, rc1.right, rc1.bottom);
3210
3211     /* same test, but with client edge: not enough width */
3212     SetWindowLong( hwnd, GWL_EXSTYLE, WS_EX_CLIENTEDGE | GetWindowLong( hwnd, GWL_EXSTYLE));
3213     nccalchelper( hwnd, sbwidth, 100, &rc1);
3214     ok( rc1.right - rc1.left == sbwidth - 2 * GetSystemMetrics(SM_CXEDGE),
3215             "Width should be %d size is %d,%d - %d,%d\n",
3216             sbwidth - 2 * GetSystemMetrics(SM_CXEDGE), rc1.left, rc1.top, rc1.right, rc1.bottom);
3217
3218     DestroyWindow( hwnd);
3219 }
3220
3221 static void test_SetParent(void)
3222 {
3223     BOOL ret;
3224     HWND desktop = GetDesktopWindow();
3225     HMENU hMenu;
3226     /* FIXME: This detection is not correct as it also covers (all?) XP+ */
3227     BOOL is_win9x = GetWindowLongPtrW(desktop, GWLP_WNDPROC) == 0;
3228     HWND parent, child1, child2, child3, child4, sibling;
3229
3230     parent = CreateWindowExA(0, "static", NULL, WS_OVERLAPPEDWINDOW,
3231                              100, 100, 200, 200, 0, 0, 0, NULL);
3232     assert(parent != 0);
3233     child1 = CreateWindowExA(0, "static", NULL, WS_CHILD,
3234                              0, 0, 50, 50, parent, 0, 0, NULL);
3235     assert(child1 != 0);
3236     child2 = CreateWindowExA(0, "static", NULL, WS_POPUP,
3237                              0, 0, 50, 50, child1, 0, 0, NULL);
3238     assert(child2 != 0);
3239     child3 = CreateWindowExA(0, "static", NULL, WS_CHILD,
3240                              0, 0, 50, 50, child2, 0, 0, NULL);
3241     assert(child3 != 0);
3242     child4 = CreateWindowExA(0, "static", NULL, WS_POPUP,
3243                              0, 0, 50, 50, child3, 0, 0, NULL);
3244     assert(child4 != 0);
3245
3246     trace("parent %p, child1 %p, child2 %p, child3 %p, child4 %p\n",
3247            parent, child1, child2, child3, child4);
3248
3249     check_parents(parent, desktop, 0, 0, 0, parent, parent);
3250     check_parents(child1, parent, parent, parent, 0, parent, parent);
3251     check_parents(child2, desktop, parent, parent, parent, child2, parent);
3252     check_parents(child3, child2, child2, child2, 0, child2, parent);
3253     check_parents(child4, desktop, child2, child2, child2, child4, parent);
3254
3255     ok(!IsChild(desktop, parent), "wrong parent/child %p/%p\n", desktop, parent);
3256     ok(!IsChild(desktop, child1), "wrong parent/child %p/%p\n", desktop, child1);
3257     ok(!IsChild(desktop, child2), "wrong parent/child %p/%p\n", desktop, child2);
3258     ok(!IsChild(desktop, child3), "wrong parent/child %p/%p\n", desktop, child3);
3259     ok(!IsChild(desktop, child4), "wrong parent/child %p/%p\n", desktop, child4);
3260
3261     ok(IsChild(parent, child1), "wrong parent/child %p/%p\n", parent, child1);
3262     ok(!IsChild(desktop, child2), "wrong parent/child %p/%p\n", desktop, child2);
3263     ok(!IsChild(parent, child2), "wrong parent/child %p/%p\n", parent, child2);
3264     ok(!IsChild(child1, child2), "wrong parent/child %p/%p\n", child1, child2);
3265     ok(!IsChild(parent, child3), "wrong parent/child %p/%p\n", parent, child3);
3266     ok(IsChild(child2, child3), "wrong parent/child %p/%p\n", child2, child3);
3267     ok(!IsChild(parent, child4), "wrong parent/child %p/%p\n", parent, child4);
3268     ok(!IsChild(child3, child4), "wrong parent/child %p/%p\n", child3, child4);
3269     ok(!IsChild(desktop, child4), "wrong parent/child %p/%p\n", desktop, child4);
3270
3271     if (!is_win9x) /* Win9x doesn't survive this test */
3272     {
3273         ok(!SetParent(parent, child1), "SetParent should fail\n");
3274         ok(!SetParent(child2, child3), "SetParent should fail\n");
3275         ok(SetParent(child1, parent) != 0, "SetParent should not fail\n");
3276         ok(SetParent(parent, child2) != 0, "SetParent should not fail\n");
3277         ok(SetParent(parent, child3) != 0, "SetParent should not fail\n");
3278         ok(!SetParent(child2, parent), "SetParent should fail\n");
3279         ok(SetParent(parent, child4) != 0, "SetParent should not fail\n");
3280
3281         check_parents(parent, child4, child4, 0, 0, child4, parent);
3282         check_parents(child1, parent, parent, parent, 0, child4, parent);
3283         check_parents(child2, desktop, parent, parent, parent, child2, parent);
3284         check_parents(child3, child2, child2, child2, 0, child2, parent);
3285         check_parents(child4, desktop, child2, child2, child2, child4, parent);
3286     }
3287     else
3288         skip("Win9x/WinMe crash\n");
3289
3290     hMenu = CreateMenu();
3291     sibling = CreateWindowExA(0, "static", NULL, WS_OVERLAPPEDWINDOW,
3292                              100, 100, 200, 200, 0, hMenu, 0, NULL);
3293     assert(sibling != 0);
3294
3295     ok(SetParent(sibling, parent) != 0, "SetParent should not fail\n");
3296     ok(GetMenu(sibling) == hMenu, "SetParent should not remove menu\n");
3297
3298     ret = DestroyWindow(parent);
3299     ok( ret, "DestroyWindow() error %d\n", GetLastError());
3300
3301     ok(!IsWindow(parent), "parent still exists\n");
3302     ok(!IsWindow(sibling), "sibling still exists\n");
3303     ok(!IsWindow(child1), "child1 still exists\n");
3304     ok(!IsWindow(child2), "child2 still exists\n");
3305     ok(!IsWindow(child3), "child3 still exists\n");
3306     ok(!IsWindow(child4), "child4 still exists\n");
3307 }
3308
3309 static LRESULT WINAPI StyleCheckProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
3310 {
3311     LPCREATESTRUCT lpcs;
3312     LPSTYLESTRUCT lpss;
3313
3314     switch (msg)
3315     {
3316     case WM_NCCREATE:
3317     case WM_CREATE:
3318         lpcs = (LPCREATESTRUCT)lparam;
3319         lpss = lpcs->lpCreateParams;
3320         if (lpss)
3321         {
3322             if ((lpcs->dwExStyle & WS_EX_DLGMODALFRAME) ||
3323                 ((!(lpcs->dwExStyle & WS_EX_STATICEDGE)) &&
3324                     (lpcs->style & (WS_DLGFRAME | WS_THICKFRAME))))
3325                 ok(lpcs->dwExStyle & WS_EX_WINDOWEDGE, "Window should have WS_EX_WINDOWEDGE style\n");
3326             else
3327                 ok(!(lpcs->dwExStyle & WS_EX_WINDOWEDGE), "Window shouldn't have WS_EX_WINDOWEDGE style\n");
3328
3329             ok((lpss->styleOld & ~WS_EX_WINDOWEDGE) == (lpcs->dwExStyle & ~WS_EX_WINDOWEDGE),
3330                 "Ex style (0x%08lx) should match what the caller passed to CreateWindowEx (0x%08lx)\n",
3331                 (lpss->styleOld & ~WS_EX_WINDOWEDGE), (lpcs->dwExStyle & ~WS_EX_WINDOWEDGE));
3332
3333             ok(lpss->styleNew == lpcs->style,
3334                 "Style (0x%08x) should match what the caller passed to CreateWindowEx (0x%08x)\n",
3335                 lpss->styleNew, lpcs->style);
3336         }
3337         break;
3338     }
3339     return DefWindowProc(hwnd, msg, wparam, lparam);
3340 }
3341
3342 static ATOM atomStyleCheckClass;
3343
3344 static void register_style_check_class(void)
3345 {
3346     WNDCLASS wc =
3347     {
3348         0,
3349         StyleCheckProc,
3350         0,
3351         0,
3352         GetModuleHandle(NULL),
3353         NULL,
3354         LoadCursor(NULL, IDC_ARROW),
3355         (HBRUSH)(COLOR_BTNFACE+1),
3356         NULL,
3357         TEXT("WineStyleCheck"),
3358     };
3359     
3360     atomStyleCheckClass = RegisterClass(&wc);
3361 }
3362
3363 static void check_window_style(DWORD dwStyleIn, DWORD dwExStyleIn, DWORD dwStyleOut, DWORD dwExStyleOut)
3364 {
3365     DWORD dwActualStyle;
3366     DWORD dwActualExStyle;
3367     STYLESTRUCT ss;
3368     HWND hwnd;
3369     HWND hwndParent = NULL;
3370
3371     ss.styleNew = dwStyleIn;
3372     ss.styleOld = dwExStyleIn;
3373
3374     if (dwStyleIn & WS_CHILD)
3375     {
3376         hwndParent = CreateWindowEx(0, MAKEINTATOM(atomStyleCheckClass), NULL,
3377             WS_OVERLAPPEDWINDOW, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
3378     }
3379
3380     hwnd = CreateWindowEx(dwExStyleIn, MAKEINTATOM(atomStyleCheckClass), NULL,
3381                     dwStyleIn, 0, 0, 0, 0, hwndParent, NULL, NULL, &ss);
3382     assert(hwnd);
3383
3384     flush_events( TRUE );
3385
3386     dwActualStyle = GetWindowLong(hwnd, GWL_STYLE);
3387     dwActualExStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
3388     ok((dwActualStyle == dwStyleOut) && (dwActualExStyle == dwExStyleOut),
3389         "Style (0x%08x) should really be 0x%08x and/or Ex style (0x%08x) should really be 0x%08x\n",
3390         dwActualStyle, dwStyleOut, dwActualExStyle, dwExStyleOut);
3391
3392     DestroyWindow(hwnd);
3393     if (hwndParent) DestroyWindow(hwndParent);
3394 }
3395
3396 /* tests what window styles the window manager automatically adds */
3397 static void test_window_styles(void)
3398 {
3399     register_style_check_class();
3400
3401     check_window_style(0, 0, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_WINDOWEDGE);
3402     check_window_style(WS_OVERLAPPEDWINDOW, 0, WS_CLIPSIBLINGS|WS_OVERLAPPEDWINDOW, WS_EX_WINDOWEDGE);
3403     check_window_style(WS_CHILD, 0, WS_CHILD, 0);
3404     check_window_style(WS_CHILD, WS_EX_WINDOWEDGE, WS_CHILD, 0);
3405     check_window_style(0, WS_EX_TOOLWINDOW, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_WINDOWEDGE|WS_EX_TOOLWINDOW);
3406     check_window_style(WS_POPUP, 0, WS_POPUP|WS_CLIPSIBLINGS, 0);
3407     check_window_style(WS_POPUP, WS_EX_WINDOWEDGE, WS_POPUP|WS_CLIPSIBLINGS, 0);
3408     check_window_style(WS_CHILD, WS_EX_DLGMODALFRAME, WS_CHILD, WS_EX_WINDOWEDGE|WS_EX_DLGMODALFRAME);
3409     check_window_style(WS_CHILD, WS_EX_DLGMODALFRAME|WS_EX_STATICEDGE, WS_CHILD, WS_EX_STATICEDGE|WS_EX_WINDOWEDGE|WS_EX_DLGMODALFRAME);
3410     check_window_style(WS_CAPTION, WS_EX_STATICEDGE, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_STATICEDGE|WS_EX_WINDOWEDGE);
3411     check_window_style(0, WS_EX_APPWINDOW, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_APPWINDOW|WS_EX_WINDOWEDGE);
3412 }
3413
3414 static void test_scrollwindow( HWND hwnd)
3415 {
3416     HDC hdc;
3417     RECT rc, rc2, rc3;
3418     COLORREF colr;
3419
3420     ShowWindow( hwnd, SW_SHOW);
3421     UpdateWindow( hwnd);
3422     flush_events( TRUE );
3423     GetClientRect( hwnd, &rc);
3424     hdc = GetDC( hwnd);
3425     /* test ScrollWindow(Ex) with no clip rectangle */
3426     /* paint the lower half of the window black */
3427     rc2 = rc;
3428     rc2.top = ( rc2.top + rc2.bottom) / 2;
3429     FillRect( hdc, &rc2, GetStockObject(BLACK_BRUSH));
3430     /* paint the upper half of the window white */
3431     rc2.bottom = rc2.top;
3432     rc2.top =0;
3433     FillRect( hdc, &rc2, GetStockObject(WHITE_BRUSH));
3434     /* scroll lower half up */
3435     rc2 = rc;
3436     rc2.top = ( rc2.top + rc2.bottom) / 2;
3437     ScrollWindowEx( hwnd, 0, - rc2.top, &rc2, NULL, NULL, NULL, SW_ERASE);
3438     flush_events(FALSE);
3439     /* expected: black should have scrolled to the upper half */
3440     colr = GetPixel( hdc, (rc2.left+rc2.right)/ 2,  rc2.bottom / 4 );
3441     ok ( colr == 0, "pixel should be black, color is %08x\n", colr);
3442     /* Repeat that test of ScrollWindow(Ex) now with clip rectangle */
3443     /* paint the lower half of the window black */
3444     rc2 = rc;
3445     rc2.top = ( rc2.top + rc2.bottom) / 2;
3446     FillRect( hdc, &rc2, GetStockObject(BLACK_BRUSH));
3447     /* paint the upper half of the window white */
3448     rc2.bottom = rc2.top;
3449     rc2.top =0;
3450     FillRect( hdc, &rc2, GetStockObject(WHITE_BRUSH));
3451     /* scroll lower half up */
3452     rc2 = rc;
3453     rc2.top = ( rc2.top + rc2.bottom) / 2;
3454     rc3 = rc;
3455     rc3.left = rc3.right / 4;
3456     rc3.right -= rc3.right / 4;
3457     ScrollWindowEx( hwnd, 0, - rc2.top, &rc2, &rc3, NULL, NULL, SW_ERASE);
3458     flush_events(FALSE);
3459     /* expected: black should have scrolled to the upper half */
3460     colr = GetPixel( hdc, (rc2.left+rc2.right)/ 2,  rc2.bottom / 4 );
3461     ok ( colr == 0, "pixel should be black, color is %08x\n", colr);
3462
3463     /* clean up */
3464     ReleaseDC( hwnd, hdc);
3465 }
3466
3467 static void test_scrollvalidate( HWND parent)
3468 {
3469     HDC hdc;
3470     HRGN hrgn=CreateRectRgn(0,0,0,0);
3471     HRGN exprgn, tmprgn, clipping;
3472     RECT rc, rcu, cliprc;
3473     /* create two overlapping child windows. The visual region
3474      * of hwnd1 is clipped by the overlapping part of
3475      * hwnd2 because of the WS_CLIPSIBLING style */
3476     HWND hwnd1, hwnd2;
3477
3478     clipping = CreateRectRgn(0,0,0,0);
3479     tmprgn = CreateRectRgn(0,0,0,0);
3480     exprgn = CreateRectRgn(0,0,0,0);
3481     hwnd2 = CreateWindowExA(0, "static", NULL,
3482             WS_CHILD| WS_VISIBLE | WS_CLIPSIBLINGS | WS_BORDER ,
3483             75, 30, 100, 100, parent, 0, 0, NULL);
3484     hwnd1 = CreateWindowExA(0, "static", NULL,
3485             WS_CHILD| WS_VISIBLE | WS_CLIPSIBLINGS | WS_BORDER ,
3486             25, 50, 100, 100, parent, 0, 0, NULL);
3487     ShowWindow( parent, SW_SHOW);
3488     UpdateWindow( parent);
3489     GetClientRect( hwnd1, &rc);
3490     cliprc=rc; 
3491     SetRectRgn( clipping, 10, 10, 90, 90);
3492     hdc = GetDC( hwnd1);
3493     /* for a visual touch */
3494     TextOut( hdc, 0,10, "0123456789", 10);
3495     ScrollDC( hdc, -10, -5, &rc, &cliprc, hrgn, &rcu);
3496     if (winetest_debug > 0) dump_region(hrgn);
3497     /* create a region with what is expected */
3498     SetRectRgn( exprgn, 39,0,49,74);
3499     SetRectRgn( tmprgn, 88,79,98,93);
3500     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
3501     SetRectRgn( tmprgn, 0,93,98,98);
3502     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
3503     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
3504     trace("update rect is %d,%d - %d,%d\n",
3505             rcu.left,rcu.top,rcu.right,rcu.bottom);
3506     /* now with clipping region */
3507     SelectClipRgn( hdc, clipping);
3508     ScrollDC( hdc, -10, -5, &rc, &cliprc, hrgn, &rcu);
3509     if (winetest_debug > 0) dump_region(hrgn);
3510     /* create a region with what is expected */
3511     SetRectRgn( exprgn, 39,10,49,74);
3512     SetRectRgn( tmprgn, 80,79,90,85);
3513     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
3514     SetRectRgn( tmprgn, 10,85,90,90);
3515     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
3516     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
3517     trace("update rect is %d,%d - %d,%d\n",
3518             rcu.left,rcu.top,rcu.right,rcu.bottom);
3519     ReleaseDC( hwnd1, hdc);
3520
3521     /* test scrolling a window with an update region */
3522     DestroyWindow( hwnd2);
3523     ValidateRect( hwnd1, NULL);
3524     SetRect( &rc, 40,40, 50,50);
3525     InvalidateRect( hwnd1, &rc, 1);
3526     GetClientRect( hwnd1, &rc);
3527     cliprc=rc;
3528     ScrollWindowEx( hwnd1, -10, 0, &rc, &cliprc, hrgn, &rcu,
3529       SW_SCROLLCHILDREN | SW_INVALIDATE);
3530     if (winetest_debug > 0) dump_region(hrgn);
3531     SetRectRgn( exprgn, 88,0,98,98);
3532     SetRectRgn( tmprgn, 30, 40, 50, 50);
3533     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
3534     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
3535
3536     /* clear an update region */
3537     UpdateWindow( hwnd1 );
3538
3539     SetRect( &rc, 0,40, 100,60);
3540     SetRect( &cliprc, 0,0, 100,100);
3541     ScrollWindowEx( hwnd1, 0, -25, &rc, &cliprc, hrgn, &rcu, SW_INVALIDATE);
3542     if (winetest_debug > 0) dump_region( hrgn );
3543     SetRectRgn( exprgn, 0, 40, 98, 60 );
3544     ok( EqualRgn( exprgn, hrgn), "wrong update region in excessive scroll\n");
3545
3546     /* now test ScrollWindowEx with a combination of
3547      * WS_CLIPCHILDREN style and SW_SCROLLCHILDREN flag */
3548     /* make hwnd2 the child of hwnd1 */
3549     hwnd2 = CreateWindowExA(0, "static", NULL,
3550             WS_CHILD| WS_VISIBLE | WS_BORDER ,
3551             50, 50, 100, 100, hwnd1, 0, 0, NULL);
3552     SetWindowLong( hwnd1, GWL_STYLE, GetWindowLong( hwnd1, GWL_STYLE) & ~WS_CLIPSIBLINGS);
3553     GetClientRect( hwnd1, &rc);
3554     cliprc=rc;
3555
3556     /* WS_CLIPCHILDREN and SW_SCROLLCHILDREN */
3557     SetWindowLong( hwnd1, GWL_STYLE, GetWindowLong( hwnd1, GWL_STYLE) | WS_CLIPCHILDREN );
3558     ValidateRect( hwnd1, NULL);
3559     ValidateRect( hwnd2, NULL);
3560     ScrollWindowEx( hwnd1, -10, -10, &rc, &cliprc, hrgn, &rcu,
3561       SW_SCROLLCHILDREN | SW_INVALIDATE);
3562     if (winetest_debug > 0) dump_region(hrgn);
3563     SetRectRgn( exprgn, 88,0,98,88);
3564     SetRectRgn( tmprgn, 0,88,98,98);
3565     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
3566     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
3567
3568     /* SW_SCROLLCHILDREN */
3569     SetWindowLong( hwnd1, GWL_STYLE, GetWindowLong( hwnd1, GWL_STYLE) & ~WS_CLIPCHILDREN );
3570     ValidateRect( hwnd1, NULL);
3571     ValidateRect( hwnd2, NULL);
3572     ScrollWindowEx( hwnd1, -10, -10, &rc, &cliprc, hrgn, &rcu, SW_SCROLLCHILDREN | SW_INVALIDATE);
3573     if (winetest_debug > 0) dump_region(hrgn);
3574     /* expected region is the same as in previous test */
3575     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
3576
3577     /* no SW_SCROLLCHILDREN */
3578     SetWindowLong( hwnd1, GWL_STYLE, GetWindowLong( hwnd1, GWL_STYLE) & ~WS_CLIPCHILDREN );
3579     ValidateRect( hwnd1, NULL);
3580     ValidateRect( hwnd2, NULL);
3581     ScrollWindowEx( hwnd1, -10, -10, &rc, &cliprc, hrgn, &rcu, SW_INVALIDATE);
3582     if (winetest_debug > 0) dump_region(hrgn);
3583     /* expected region is the same as in previous test */
3584     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
3585
3586     /* WS_CLIPCHILDREN and no SW_SCROLLCHILDREN */
3587     SetWindowLong( hwnd1, GWL_STYLE, GetWindowLong( hwnd1, GWL_STYLE) | WS_CLIPCHILDREN );
3588     ValidateRect( hwnd1, NULL);
3589     ValidateRect( hwnd2, NULL);
3590     ScrollWindowEx( hwnd1, -10, -10, &rc, &cliprc, hrgn, &rcu, SW_INVALIDATE);
3591     if (winetest_debug > 0) dump_region(hrgn);
3592     SetRectRgn( exprgn, 88,0,98,20);
3593     SetRectRgn( tmprgn, 20,20,98,30);
3594     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
3595     SetRectRgn( tmprgn, 20,30,30,88);
3596     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
3597     SetRectRgn( tmprgn, 0,88,30,98);
3598     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
3599     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
3600
3601     /* clean up */
3602     DeleteObject( hrgn);
3603     DeleteObject( exprgn);
3604     DeleteObject( tmprgn);
3605     DestroyWindow( hwnd1);
3606     DestroyWindow( hwnd2);
3607 }
3608
3609 /* couple of tests of return values of scrollbar functions
3610  * called on a scrollbarless window */ 
3611 static void test_scroll(void)
3612 {
3613     BOOL ret;
3614     INT min, max;
3615     SCROLLINFO si;
3616     HWND hwnd = CreateWindowExA(0, "Static", "Wine test window",
3617         WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_POPUP,
3618         100, 100, 200, 200, 0, 0, 0, NULL);
3619     /* horizontal */
3620     ret = GetScrollRange( hwnd, SB_HORZ, &min, &max);
3621     if (!ret)  /* win9x */
3622     {
3623         win_skip( "GetScrollRange doesn't work\n" );
3624         DestroyWindow( hwnd);
3625         return;
3626     }
3627     ok( min == 0, "minimum scroll pos is %d (should be zero)\n", min);
3628     ok( max == 0, "maximum scroll pos is %d (should be zero)\n", min);
3629     si.cbSize = sizeof( si);
3630     si.fMask = SIF_PAGE;
3631     si.nPage = 0xdeadbeef;
3632     ret = GetScrollInfo( hwnd, SB_HORZ, &si);
3633     ok( !ret, "GetScrollInfo returns %d (should be zero)\n", ret);
3634     ok( si.nPage == 0xdeadbeef, "unexpected value for nPage is %d\n", si.nPage);
3635     /* vertical */
3636     ret = GetScrollRange( hwnd, SB_VERT, &min, &max);
3637     ok( ret, "GetScrollRange returns FALSE\n");
3638     ok( min == 0, "minimum scroll pos is %d (should be zero)\n", min);
3639     ok( max == 0, "maximum scroll pos is %d (should be zero)\n", min);
3640     si.cbSize = sizeof( si);
3641     si.fMask = SIF_PAGE;
3642     si.nPage = 0xdeadbeef;
3643     ret = GetScrollInfo( hwnd, SB_VERT, &si);
3644     ok( !ret, "GetScrollInfo returns %d (should be zero)\n", ret);
3645     ok( si.nPage == 0xdeadbeef, "unexpected value for nPage is %d\n", si.nPage);
3646     /* clean up */
3647     DestroyWindow( hwnd);
3648 }
3649
3650 static void test_scrolldc( HWND parent)
3651 {
3652     HDC hdc;
3653     HRGN exprgn, tmprgn, hrgn;
3654     RECT rc, rc2, rcu, cliprc;
3655     HWND hwnd1;
3656     COLORREF colr;
3657
3658     hrgn = CreateRectRgn(0,0,0,0);
3659     tmprgn = CreateRectRgn(0,0,0,0);
3660     exprgn = CreateRectRgn(0,0,0,0);
3661
3662     hwnd1 = CreateWindowExA(0, "static", NULL,
3663             WS_CHILD| WS_VISIBLE,
3664             25, 50, 100, 100, parent, 0, 0, NULL);
3665     ShowWindow( parent, SW_SHOW);
3666     UpdateWindow( parent);
3667     flush_events( TRUE );
3668     GetClientRect( hwnd1, &rc);
3669     hdc = GetDC( hwnd1);
3670     /* paint the upper half of the window black */
3671     rc2 = rc;
3672     rc2.bottom = ( rc.top + rc.bottom) /2;
3673     FillRect( hdc, &rc2, GetStockObject(BLACK_BRUSH));
3674     /* clip region is the lower half */
3675     cliprc=rc; 
3676     cliprc.top = (rc.top + rc.bottom) /2;
3677     /* test whether scrolled pixels are properly clipped */ 
3678     colr = GetPixel( hdc, (rc.left+rc.right)/2, ( rc.top + rc.bottom) /2 - 1);
3679     ok ( colr == 0, "pixel should be black, color is %08x\n", colr);
3680     /* this scroll should not cause any visible changes */ 
3681     ScrollDC( hdc, 5, -20, &rc, &cliprc, hrgn, &rcu);
3682     colr = GetPixel( hdc, (rc.left+rc.right)/2, ( rc.top + rc.bottom) /2 - 1);
3683     ok ( colr == 0, "pixel should be black, color is %08x\n", colr);
3684     /* test with NULL clip rect */
3685     ScrollDC( hdc, 20, -20, &rc, NULL, hrgn, &rcu);
3686     /*FillRgn(hdc, hrgn, GetStockObject(WHITE_BRUSH));*/
3687     trace("update rect: %d,%d - %d,%d\n",
3688            rcu.left, rcu.top, rcu.right, rcu.bottom);
3689     if (winetest_debug > 0) dump_region(hrgn);
3690     SetRect(&rc2, 0, 0, 100, 100);
3691     ok(EqualRect(&rcu, &rc2), "rects do not match (%d,%d-%d,%d) / (%d,%d-%d,%d)\n",
3692        rcu.left, rcu.top, rcu.right, rcu.bottom, rc2.left, rc2.top, rc2.right, rc2.bottom);
3693
3694     SetRectRgn( exprgn, 0, 0, 20, 80);
3695     SetRectRgn( tmprgn, 0, 80, 100, 100);
3696     CombineRgn(exprgn, exprgn, tmprgn, RGN_OR);
3697     if (winetest_debug > 0) dump_region(exprgn);
3698     ok(EqualRgn(exprgn, hrgn), "wrong update region\n");
3699     /* test clip rect > scroll rect */ 
3700     FillRect( hdc, &rc, GetStockObject(WHITE_BRUSH));
3701     rc2=rc;
3702     InflateRect( &rc2, -(rc.right-rc.left)/4, -(rc.bottom-rc.top)/4);
3703     FillRect( hdc, &rc2, GetStockObject(BLACK_BRUSH));
3704     ScrollDC( hdc, 10, 10, &rc2, &rc, hrgn, &rcu);
3705     SetRectRgn( exprgn, 25, 25, 75, 35);
3706     SetRectRgn( tmprgn, 25, 35, 35, 75);
3707     CombineRgn(exprgn, exprgn, tmprgn, RGN_OR);
3708     ok(EqualRgn(exprgn, hrgn), "wrong update region\n");
3709     trace("update rect: %d,%d - %d,%d\n",
3710            rcu.left, rcu.top, rcu.right, rcu.bottom);
3711     if (winetest_debug > 0) dump_region(hrgn);
3712
3713     /* clean up */
3714     DeleteObject(hrgn);
3715     DeleteObject(exprgn);
3716     DeleteObject(tmprgn);
3717     DestroyWindow(hwnd1);
3718 }
3719
3720 static void test_params(void)
3721 {
3722     HWND hwnd;
3723     INT rc;
3724
3725     /* Just a param check */
3726     if (pGetMonitorInfoA)
3727     {
3728         SetLastError(0xdeadbeef);
3729         rc = GetWindowText(hwndMain2, NULL, 1024);
3730         ok( rc==0, "GetWindowText: rc=%d err=%d\n",rc,GetLastError());
3731     }
3732     else
3733     {
3734         /* Skips actually on Win95 and NT4 */
3735         win_skip("Test would crash on Win95\n");
3736     }
3737
3738     SetLastError(0xdeadbeef);
3739     hwnd=CreateWindow("LISTBOX", "TestList",
3740                       (LBS_STANDARD & ~LBS_SORT),
3741                       0, 0, 100, 100,
3742                       NULL, (HMENU)1, NULL, 0);
3743
3744     ok(!hwnd || broken(hwnd != NULL), /* w2k3 sp2 */
3745        "CreateWindow with invalid menu handle should fail\n");
3746     if (!hwnd)
3747         ok(GetLastError() == ERROR_INVALID_MENU_HANDLE || /* NT */
3748            GetLastError() == 0xdeadbeef, /* Win9x */
3749            "wrong last error value %d\n", GetLastError());
3750 }
3751
3752 static void test_AWRwindow(LPCSTR class, LONG style, LONG exStyle, BOOL menu)
3753 {
3754     HWND hwnd = 0;
3755
3756     hwnd = CreateWindowEx(exStyle, class, class, style,
3757                           110, 100,
3758                           225, 200,
3759                           0,
3760                           menu ? hmenu : 0,
3761                           0, 0);
3762     if (!hwnd) {
3763         trace("Failed to create window class=%s, style=0x%08x, exStyle=0x%08x\n", class, style, exStyle);
3764         return;
3765     }
3766     ShowWindow(hwnd, SW_SHOW);
3767
3768     test_nonclient_area(hwnd);
3769
3770     SetMenu(hwnd, 0);
3771     DestroyWindow(hwnd);
3772 }
3773
3774 static BOOL AWR_init(void)
3775 {
3776     WNDCLASS class;
3777
3778     class.style         = CS_HREDRAW | CS_VREDRAW;
3779     class.lpfnWndProc     = DefWindowProcA;
3780     class.cbClsExtra    = 0;
3781     class.cbWndExtra    = 0;
3782     class.hInstance     = 0;
3783     class.hIcon         = LoadIcon (0, IDI_APPLICATION);
3784     class.hCursor       = LoadCursor (0, IDC_ARROW);
3785     class.hbrBackground = 0;
3786     class.lpszMenuName  = 0;
3787     class.lpszClassName = szAWRClass;
3788     
3789     if (!RegisterClass (&class)) {
3790         ok(FALSE, "RegisterClass failed\n");
3791         return FALSE;
3792     }
3793
3794     hmenu = CreateMenu();
3795     if (!hmenu)
3796         return FALSE;
3797     ok(hmenu != 0, "Failed to create menu\n");
3798     ok(AppendMenu(hmenu, MF_STRING, 1, "Test!"), "Failed to create menu item\n");
3799     
3800     return TRUE;
3801 }
3802
3803
3804 static void test_AWR_window_size(BOOL menu)
3805 {
3806     LONG styles[] = {
3807         WS_POPUP,
3808         WS_MAXIMIZE, WS_BORDER, WS_DLGFRAME, 
3809         WS_SYSMENU, 
3810         WS_THICKFRAME,
3811         WS_MINIMIZEBOX, WS_MAXIMIZEBOX,
3812         WS_HSCROLL, WS_VSCROLL
3813     };
3814     LONG exStyles[] = {
3815         WS_EX_CLIENTEDGE,
3816         WS_EX_TOOLWINDOW, WS_EX_WINDOWEDGE,
3817         WS_EX_APPWINDOW,
3818 #if 0
3819         /* These styles have problems on (at least) WinXP (SP2) and Wine */
3820         WS_EX_DLGMODALFRAME, 
3821         WS_EX_STATICEDGE, 
3822 #endif
3823     };
3824
3825     int i;    
3826
3827     /* A exhaustive check of all the styles takes too long
3828      * so just do a (hopefully representative) sample
3829      */
3830     for (i = 0; i < COUNTOF(styles); ++i)
3831         test_AWRwindow(szAWRClass, styles[i], 0, menu);
3832     for (i = 0; i < COUNTOF(exStyles); ++i) {
3833         test_AWRwindow(szAWRClass, WS_POPUP, exStyles[i], menu);
3834         test_AWRwindow(szAWRClass, WS_THICKFRAME, exStyles[i], menu);
3835     }
3836 }
3837 #undef COUNTOF
3838
3839 #define SHOWSYSMETRIC(SM) trace(#SM "=%d\n", GetSystemMetrics(SM))
3840
3841 static void test_AdjustWindowRect(void)
3842 {
3843     if (!AWR_init())
3844         return;
3845     
3846     SHOWSYSMETRIC(SM_CYCAPTION);
3847     SHOWSYSMETRIC(SM_CYSMCAPTION);
3848     SHOWSYSMETRIC(SM_CYMENU);
3849     SHOWSYSMETRIC(SM_CXEDGE);
3850     SHOWSYSMETRIC(SM_CYEDGE);
3851     SHOWSYSMETRIC(SM_CXVSCROLL);
3852     SHOWSYSMETRIC(SM_CYHSCROLL);
3853     SHOWSYSMETRIC(SM_CXFRAME);
3854     SHOWSYSMETRIC(SM_CYFRAME);
3855     SHOWSYSMETRIC(SM_CXDLGFRAME);
3856     SHOWSYSMETRIC(SM_CYDLGFRAME);
3857     SHOWSYSMETRIC(SM_CXBORDER);
3858     SHOWSYSMETRIC(SM_CYBORDER);  
3859
3860     test_AWR_window_size(FALSE);
3861     test_AWR_window_size(TRUE);
3862
3863     DestroyMenu(hmenu);
3864 }
3865 #undef SHOWSYSMETRIC
3866
3867
3868 /* Global variables to trigger exit from loop */
3869 static int redrawComplete, WMPAINT_count;
3870
3871 static LRESULT WINAPI redraw_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
3872 {
3873     switch (msg)
3874     {
3875     case WM_PAINT:
3876         trace("doing WM_PAINT %d\n", WMPAINT_count);
3877         WMPAINT_count++;
3878         if (WMPAINT_count > 10 && redrawComplete == 0) {
3879             PAINTSTRUCT ps;
3880             BeginPaint(hwnd, &ps);
3881             EndPaint(hwnd, &ps);
3882             return 1;
3883         }
3884         return 0;
3885     }
3886     return DefWindowProc(hwnd, msg, wparam, lparam);
3887 }
3888
3889 /* Ensure we exit from RedrawNow regardless of invalidated area */
3890 static void test_redrawnow(void)
3891 {
3892    WNDCLASSA cls;
3893    HWND hwndMain;
3894
3895    cls.style = CS_DBLCLKS;
3896    cls.lpfnWndProc = redraw_window_procA;
3897    cls.cbClsExtra = 0;
3898    cls.cbWndExtra = 0;
3899    cls.hInstance = GetModuleHandleA(0);
3900    cls.hIcon = 0;
3901    cls.hCursor = LoadCursorA(0, IDC_ARROW);
3902    cls.hbrBackground = GetStockObject(WHITE_BRUSH);
3903    cls.lpszMenuName = NULL;
3904    cls.lpszClassName = "RedrawWindowClass";
3905
3906    if(!RegisterClassA(&cls)) {
3907        trace("Register failed %d\n", GetLastError());
3908        return;
3909    }
3910
3911    hwndMain = CreateWindowA("RedrawWindowClass", "Main Window", WS_OVERLAPPEDWINDOW,
3912                             CW_USEDEFAULT, 0, 100, 100, NULL, NULL, 0, NULL);
3913
3914    ok( WMPAINT_count == 0, "Multiple unexpected WM_PAINT calls %d\n", WMPAINT_count);
3915    ShowWindow(hwndMain, SW_SHOW);
3916    ok( WMPAINT_count == 0, "Multiple unexpected WM_PAINT calls %d\n", WMPAINT_count);
3917    RedrawWindow(hwndMain, NULL,NULL,RDW_UPDATENOW | RDW_ALLCHILDREN);
3918    ok( WMPAINT_count == 1 || broken(WMPAINT_count == 0), /* sometimes on win9x */
3919        "Multiple unexpected WM_PAINT calls %d\n", WMPAINT_count);
3920    redrawComplete = TRUE;
3921    ok( WMPAINT_count < 10, "RedrawWindow (RDW_UPDATENOW) never completed (%d)\n", WMPAINT_count);
3922
3923    /* clean up */
3924    DestroyWindow( hwndMain);
3925 }
3926
3927 struct parentdc_stat {
3928     RECT client;
3929     RECT clip;
3930     RECT paint;
3931 };
3932
3933 struct parentdc_test {
3934    struct parentdc_stat main, main_todo;
3935    struct parentdc_stat child1, child1_todo;
3936    struct parentdc_stat child2, child2_todo;
3937 };
3938
3939 static LRESULT WINAPI parentdc_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
3940 {
3941     RECT rc;
3942     PAINTSTRUCT ps;
3943
3944     struct parentdc_stat *t = (struct parentdc_stat *)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
3945
3946     switch (msg)
3947     {
3948     case WM_PAINT:
3949         GetClientRect(hwnd, &rc);
3950         CopyRect(&t->client, &rc);
3951         GetWindowRect(hwnd, &rc);
3952         trace("WM_PAINT: hwnd %p, client rect (%d,%d)-(%d,%d), window rect (%d,%d)-(%d,%d)\n", hwnd,
3953               t->client.left, t->client.top, t->client.right, t->client.bottom,
3954               rc.left, rc.top, rc.right, rc.bottom);
3955         BeginPaint(hwnd, &ps);
3956         CopyRect(&t->paint, &ps.rcPaint);
3957         GetClipBox(ps.hdc, &rc);
3958         CopyRect(&t->clip, &rc);
3959         trace("clip rect (%d,%d)-(%d,%d), paint rect (%d,%d)-(%d,%d)\n",
3960               rc.left, rc.top, rc.right, rc.bottom,
3961               ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
3962         EndPaint(hwnd, &ps);
3963         return 0;
3964     }
3965     return DefWindowProc(hwnd, msg, wparam, lparam);
3966 }
3967
3968 static void zero_parentdc_stat(struct parentdc_stat *t)
3969 {
3970     SetRectEmpty(&t->client);
3971     SetRectEmpty(&t->clip);
3972     SetRectEmpty(&t->paint);
3973 }
3974
3975 static void zero_parentdc_test(struct parentdc_test *t)
3976 {
3977     zero_parentdc_stat(&t->main);
3978     zero_parentdc_stat(&t->child1);
3979     zero_parentdc_stat(&t->child2);
3980 }
3981
3982 #define parentdc_field_ok(t, w, r, f, got) \
3983   ok (t.w.r.f==got.w.r.f, "window " #w ", rect " #r ", field " #f \
3984       ": expected %d, got %d\n", \
3985       t.w.r.f, got.w.r.f)
3986
3987 #define parentdc_todo_field_ok(t, w, r, f, got) \
3988   if (t.w##_todo.r.f) todo_wine { parentdc_field_ok(t, w, r, f, got); } \
3989   else parentdc_field_ok(t, w, r, f, got)
3990
3991 #define parentdc_rect_ok(t, w, r, got) \
3992   parentdc_todo_field_ok(t, w, r, left, got); \
3993   parentdc_todo_field_ok(t, w, r, top, got); \
3994   parentdc_todo_field_ok(t, w, r, right, got); \
3995   parentdc_todo_field_ok(t, w, r, bottom, got);
3996
3997 #define parentdc_win_ok(t, w, got) \
3998   parentdc_rect_ok(t, w, client, got); \
3999   parentdc_rect_ok(t, w, clip, got); \
4000   parentdc_rect_ok(t, w, paint, got);
4001
4002 #define parentdc_ok(t, got) \
4003   parentdc_win_ok(t, main, got); \
4004   parentdc_win_ok(t, child1, got); \
4005   parentdc_win_ok(t, child2, got);
4006
4007 static void test_csparentdc(void)
4008 {
4009    WNDCLASSA clsMain, cls;
4010    HWND hwndMain, hwnd1, hwnd2;
4011    RECT rc;
4012
4013    struct parentdc_test test_answer;
4014
4015 #define nothing_todo {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}
4016    const struct parentdc_test test1 = 
4017    {
4018         {{0, 0, 150, 150}, {0, 0, 150, 150}, {0, 0, 150, 150}}, nothing_todo,
4019         {{0, 0, 40, 40}, {-20, -20, 130, 130}, {0, 0, 40, 40}}, {{0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}},
4020         {{0, 0, 40, 40}, {-40, -40, 110, 110}, {0, 0, 40, 40}}, {{0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}},
4021    };
4022
4023    const struct parentdc_test test2 = 
4024    {
4025         {{0, 0, 150, 150}, {0, 0, 50, 50}, {0, 0, 50, 50}}, nothing_todo,
4026         {{0, 0, 40, 40}, {-20, -20, 30, 30}, {0, 0, 30, 30}}, {{0, 0, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}},
4027         {{0, 0, 40, 40}, {-40, -40, 10, 10}, {0, 0, 10, 10}}, {{0, 0, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}},
4028    };
4029
4030    const struct parentdc_test test3 = 
4031    {
4032         {{0, 0, 150, 150}, {0, 0, 10, 10}, {0, 0, 10, 10}}, nothing_todo,
4033         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
4034         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
4035    };
4036
4037    const struct parentdc_test test4 = 
4038    {
4039         {{0, 0, 150, 150}, {40, 40, 50, 50}, {40, 40, 50, 50}}, nothing_todo,
4040         {{0, 0, 40, 40}, {20, 20, 30, 30}, {20, 20, 30, 30}}, nothing_todo,
4041         {{0, 0, 40, 40}, {0, 0, 10, 10}, {0, 0, 10, 10}}, nothing_todo,
4042    };
4043
4044    const struct parentdc_test test5 = 
4045    {
4046         {{0, 0, 150, 150}, {20, 20, 60, 60}, {20, 20, 60, 60}}, nothing_todo,
4047         {{0, 0, 40, 40}, {-20, -20, 130, 130}, {0, 0, 40, 40}}, {{0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}},
4048         {{0, 0, 40, 40}, {-20, -20, 20, 20}, {0, 0, 20, 20}}, {{0, 0, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}},
4049    };
4050
4051    const struct parentdc_test test6 = 
4052    {
4053         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
4054         {{0, 0, 40, 40}, {0, 0, 10, 10}, {0, 0, 10, 10}}, nothing_todo,
4055         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
4056    };
4057
4058    const struct parentdc_test test7 = 
4059    {
4060         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
4061         {{0, 0, 40, 40}, {-20, -20, 130, 130}, {0, 0, 40, 40}}, {{0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}},
4062         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
4063    };
4064 #undef nothing_todo
4065
4066    clsMain.style = CS_DBLCLKS;
4067    clsMain.lpfnWndProc = parentdc_window_procA;
4068    clsMain.cbClsExtra = 0;
4069    clsMain.cbWndExtra = 0;
4070    clsMain.hInstance = GetModuleHandleA(0);
4071    clsMain.hIcon = 0;
4072    clsMain.hCursor = LoadCursorA(0, IDC_ARROW);
4073    clsMain.hbrBackground = GetStockObject(WHITE_BRUSH);
4074    clsMain.lpszMenuName = NULL;
4075    clsMain.lpszClassName = "ParentDcMainWindowClass";
4076
4077    if(!RegisterClassA(&clsMain)) {
4078        trace("Register failed %d\n", GetLastError());
4079        return;
4080    }
4081
4082    cls.style = CS_DBLCLKS | CS_PARENTDC;
4083    cls.lpfnWndProc = parentdc_window_procA;
4084    cls.cbClsExtra = 0;
4085    cls.cbWndExtra = 0;
4086    cls.hInstance = GetModuleHandleA(0);
4087    cls.hIcon = 0;
4088    cls.hCursor = LoadCursorA(0, IDC_ARROW);
4089    cls.hbrBackground = GetStockObject(WHITE_BRUSH);
4090    cls.lpszMenuName = NULL;
4091    cls.lpszClassName = "ParentDcWindowClass";
4092
4093    if(!RegisterClassA(&cls)) {
4094        trace("Register failed %d\n", GetLastError());
4095        return;
4096    }
4097
4098    SetRect(&rc, 0, 0, 150, 150);
4099    AdjustWindowRectEx(&rc, WS_OVERLAPPEDWINDOW, FALSE, 0);
4100    hwndMain = CreateWindowA("ParentDcMainWindowClass", "Main Window", WS_OVERLAPPEDWINDOW,
4101                             CW_USEDEFAULT, 0, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, 0, NULL);
4102    SetWindowLongPtrA(hwndMain, GWLP_USERDATA, (DWORD_PTR)&test_answer.main);
4103    hwnd1 = CreateWindowA("ParentDcWindowClass", "Child Window 1", WS_CHILD,
4104                             20, 20, 40, 40, hwndMain, NULL, 0, NULL);
4105    SetWindowLongPtrA(hwnd1, GWLP_USERDATA, (DWORD_PTR)&test_answer.child1);
4106    hwnd2 = CreateWindowA("ParentDcWindowClass", "Child Window 2", WS_CHILD,
4107                             40, 40, 40, 40, hwndMain, NULL, 0, NULL);
4108    SetWindowLongPtrA(hwnd2, GWLP_USERDATA, (DWORD_PTR)&test_answer.child2);
4109    ShowWindow(hwndMain, SW_SHOW);
4110    ShowWindow(hwnd1, SW_SHOW);
4111    ShowWindow(hwnd2, SW_SHOW);
4112    SetWindowPos(hwndMain, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
4113    flush_events( TRUE );
4114
4115    zero_parentdc_test(&test_answer);
4116    InvalidateRect(hwndMain, NULL, TRUE);
4117    flush_events( TRUE );
4118    parentdc_ok(test1, test_answer);
4119
4120    zero_parentdc_test(&test_answer);
4121    SetRect(&rc, 0, 0, 50, 50);
4122    InvalidateRect(hwndMain, &rc, TRUE);
4123    flush_events( TRUE );
4124    parentdc_ok(test2, test_answer);
4125
4126    zero_parentdc_test(&test_answer);
4127    SetRect(&rc, 0, 0, 10, 10);
4128    InvalidateRect(hwndMain, &rc, TRUE);
4129    flush_events( TRUE );
4130    parentdc_ok(test3, test_answer);
4131
4132    zero_parentdc_test(&test_answer);
4133    SetRect(&rc, 40, 40, 50, 50);
4134    InvalidateRect(hwndMain, &rc, TRUE);
4135    flush_events( TRUE );
4136    parentdc_ok(test4, test_answer);
4137
4138    zero_parentdc_test(&test_answer);
4139    SetRect(&rc, 20, 20, 60, 60);
4140    InvalidateRect(hwndMain, &rc, TRUE);
4141    flush_events( TRUE );
4142    parentdc_ok(test5, test_answer);
4143
4144    zero_parentdc_test(&test_answer);
4145    SetRect(&rc, 0, 0, 10, 10);
4146    InvalidateRect(hwnd1, &rc, TRUE);
4147    flush_events( TRUE );
4148    parentdc_ok(test6, test_answer);
4149
4150    zero_parentdc_test(&test_answer);
4151    SetRect(&rc, -5, -5, 65, 65);
4152    InvalidateRect(hwnd1, &rc, TRUE);
4153    flush_events( TRUE );
4154    parentdc_ok(test7, test_answer);
4155
4156    DestroyWindow(hwndMain);
4157    DestroyWindow(hwnd1);
4158    DestroyWindow(hwnd2);
4159 }
4160
4161 static LRESULT WINAPI def_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
4162 {
4163     return DefWindowProcA(hwnd, msg, wparam, lparam);
4164 }
4165
4166 static LRESULT WINAPI def_window_procW(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
4167 {
4168     return DefWindowProcW(hwnd, msg, wparam, lparam);
4169 }
4170
4171 static void test_IsWindowUnicode(void)
4172 {
4173     static const char ansi_class_nameA[] = "ansi class name";
4174     static const WCHAR ansi_class_nameW[] = {'a','n','s','i',' ','c','l','a','s','s',' ','n','a','m','e',0};
4175     static const char unicode_class_nameA[] = "unicode class name";
4176     static const WCHAR unicode_class_nameW[] = {'u','n','i','c','o','d','e',' ','c','l','a','s','s',' ','n','a','m','e',0};
4177     WNDCLASSA classA;
4178     WNDCLASSW classW;
4179     HWND hwnd;
4180
4181     memset(&classW, 0, sizeof(classW));
4182     classW.hInstance = GetModuleHandleA(0);
4183     classW.lpfnWndProc = def_window_procW;
4184     classW.lpszClassName = unicode_class_nameW;
4185     if (!RegisterClassW(&classW)) return; /* this catches Win9x as well */
4186
4187     memset(&classA, 0, sizeof(classA));
4188     classA.hInstance = GetModuleHandleA(0);
4189     classA.lpfnWndProc = def_window_procA;
4190     classA.lpszClassName = ansi_class_nameA;
4191     assert(RegisterClassA(&classA));
4192
4193     /* unicode class: window proc */
4194     hwnd = CreateWindowExW(0, unicode_class_nameW, NULL, WS_POPUP,
4195                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
4196     assert(hwnd);
4197
4198     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
4199     SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (ULONG_PTR)def_window_procA);
4200     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
4201     SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (ULONG_PTR)def_window_procW);
4202     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
4203
4204     DestroyWindow(hwnd);
4205
4206     hwnd = CreateWindowExA(0, unicode_class_nameA, NULL, WS_POPUP,
4207                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
4208     assert(hwnd);
4209
4210     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
4211     SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (ULONG_PTR)def_window_procA);
4212     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
4213     SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (ULONG_PTR)def_window_procW);
4214     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
4215
4216     DestroyWindow(hwnd);
4217
4218     /* ansi class: window proc */
4219     hwnd = CreateWindowExW(0, ansi_class_nameW, NULL, WS_POPUP,
4220                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
4221     assert(hwnd);
4222
4223     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
4224     SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (ULONG_PTR)def_window_procW);
4225     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
4226     SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (ULONG_PTR)def_window_procA);
4227     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
4228
4229     DestroyWindow(hwnd);
4230
4231     hwnd = CreateWindowExA(0, ansi_class_nameA, NULL, WS_POPUP,
4232                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
4233     assert(hwnd);
4234
4235     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
4236     SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (ULONG_PTR)def_window_procW);
4237     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
4238     SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (ULONG_PTR)def_window_procA);
4239     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
4240
4241     DestroyWindow(hwnd);
4242
4243     /* unicode class: class proc */
4244     hwnd = CreateWindowExW(0, unicode_class_nameW, NULL, WS_POPUP,
4245                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
4246     assert(hwnd);
4247
4248     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
4249     SetClassLongPtrA(hwnd, GCLP_WNDPROC, (ULONG_PTR)def_window_procA);
4250     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
4251     /* do not restore class window proc back to unicode */
4252
4253     DestroyWindow(hwnd);
4254
4255     hwnd = CreateWindowExA(0, unicode_class_nameA, NULL, WS_POPUP,
4256                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
4257     assert(hwnd);
4258
4259     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
4260     SetClassLongPtrW(hwnd, GCLP_WNDPROC, (ULONG_PTR)def_window_procW);
4261     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
4262
4263     DestroyWindow(hwnd);
4264
4265     /* ansi class: class proc */
4266     hwnd = CreateWindowExW(0, ansi_class_nameW, NULL, WS_POPUP,
4267                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
4268     assert(hwnd);
4269
4270     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
4271     SetClassLongPtrW(hwnd, GCLP_WNDPROC, (ULONG_PTR)def_window_procW);
4272     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
4273     /* do not restore class window proc back to ansi */
4274
4275     DestroyWindow(hwnd);
4276
4277     hwnd = CreateWindowExA(0, ansi_class_nameA, NULL, WS_POPUP,
4278                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
4279     assert(hwnd);
4280
4281     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
4282     SetClassLongPtrA(hwnd, GCLP_WNDPROC, (ULONG_PTR)def_window_procA);
4283     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
4284
4285     DestroyWindow(hwnd);
4286 }
4287
4288 static LRESULT CALLBACK minmax_wnd_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
4289 {
4290     MINMAXINFO *minmax;
4291
4292     if (msg != WM_GETMINMAXINFO)
4293         return DefWindowProc(hwnd, msg, wp, lp);
4294
4295     minmax = (MINMAXINFO *)lp;
4296
4297     if ((GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD))
4298     {
4299         minmax->ptReserved.x = 0;
4300         minmax->ptReserved.y = 0;
4301         minmax->ptMaxSize.x = 400;
4302         minmax->ptMaxSize.y = 400;
4303         minmax->ptMaxPosition.x = 300;
4304         minmax->ptMaxPosition.y = 300;
4305         minmax->ptMaxTrackSize.x = 200;
4306         minmax->ptMaxTrackSize.y = 200;
4307         minmax->ptMinTrackSize.x = 100;
4308         minmax->ptMinTrackSize.y = 100;
4309     }
4310     else
4311         DefWindowProc(hwnd, msg, wp, lp);
4312     return 1;
4313 }
4314
4315 static int expected_cx, expected_cy;
4316 static RECT expected_rect, broken_rect;
4317
4318 static LRESULT CALLBACK winsizes_wnd_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
4319 {
4320     switch(msg)
4321     {
4322     case WM_GETMINMAXINFO:
4323     {
4324         RECT rect;
4325         GetWindowRect( hwnd, &rect );
4326         ok( !rect.left && !rect.top && !rect.right && !rect.bottom,
4327             "wrong rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
4328         return DefWindowProc(hwnd, msg, wp, lp);
4329     }
4330     case WM_NCCREATE:
4331     case WM_CREATE:
4332     {
4333         CREATESTRUCTA *cs = (CREATESTRUCTA *)lp;
4334         RECT rect;
4335         GetWindowRect( hwnd, &rect );
4336         trace( "hwnd %p msg %x size %dx%d rect %d,%d-%d,%d\n",
4337                hwnd, msg, cs->cx, cs->cy, rect.left, rect.top, rect.right, rect.bottom );
4338         ok( cs->cx == expected_cx || broken(cs->cx == (short)expected_cx),
4339             "wrong x size %d/%d\n", cs->cx, expected_cx );
4340         ok( cs->cy == expected_cy || broken(cs->cy == (short)expected_cy),
4341             "wrong y size %d/%d\n", cs->cy, expected_cy );
4342         ok( (rect.right - rect.left == expected_rect.right - expected_rect.left &&
4343              rect.bottom - rect.top == expected_rect.bottom - expected_rect.top) ||
4344             broken( rect.right - rect.left == broken_rect.right - broken_rect.left &&
4345                     rect.bottom - rect.top == broken_rect.bottom - broken_rect.top) ||
4346             broken( rect.right - rect.left == (short)broken_rect.right - (short)broken_rect.left &&
4347                     rect.bottom - rect.top == (short)broken_rect.bottom - (short)broken_rect.top),
4348             "wrong rect %d,%d-%d,%d / %d,%d-%d,%d\n",
4349             rect.left, rect.top, rect.right, rect.bottom,
4350             expected_rect.left, expected_rect.top, expected_rect.right, expected_rect.bottom );
4351         return DefWindowProc(hwnd, msg, wp, lp);
4352     }
4353     case WM_NCCALCSIZE:
4354     {
4355         RECT rect, *r = (RECT *)lp;
4356         GetWindowRect( hwnd, &rect );
4357         ok( !memcmp( &rect, r, sizeof(rect) ),
4358             "passed rect %d,%d-%d,%d doesn't match window rect %d,%d-%d,%d\n",
4359             r->left, r->top, r->right, r->bottom, rect.left, rect.top, rect.right, rect.bottom );
4360         return DefWindowProc(hwnd, msg, wp, lp);
4361     }
4362     default:
4363         return DefWindowProc(hwnd, msg, wp, lp);
4364     }
4365 }
4366
4367 static void test_CreateWindow(void)
4368 {
4369     WNDCLASS cls;
4370     HWND hwnd, parent;
4371     HMENU hmenu;
4372     RECT rc, rc_minmax;
4373     MINMAXINFO minmax;
4374
4375 #define expect_menu(window, menu) \
4376     SetLastError(0xdeadbeef); \
4377     ok(GetMenu(window) == (HMENU)menu, "GetMenu error %d\n", GetLastError())
4378
4379 #define expect_style(window, style)\
4380     ok((ULONG)GetWindowLong(window, GWL_STYLE) == (style), "expected style %x != %x\n", (LONG)(style), GetWindowLong(window, GWL_STYLE))
4381
4382 #define expect_ex_style(window, ex_style)\
4383     ok((ULONG)GetWindowLong(window, GWL_EXSTYLE) == (ex_style), "expected ex_style %x != %x\n", (LONG)(ex_style), GetWindowLong(window, GWL_EXSTYLE))
4384
4385 #define expect_gle_broken_9x(gle)\
4386     ok(GetLastError() == gle ||\
4387        broken(GetLastError() == 0xdeadbeef),\
4388        "IsMenu set error %d\n", GetLastError())
4389
4390     hmenu = CreateMenu();
4391     assert(hmenu != 0);
4392     parent = GetDesktopWindow();
4393     assert(parent != 0);
4394
4395     SetLastError(0xdeadbeef);
4396     ok(IsMenu(hmenu), "IsMenu error %d\n", GetLastError());
4397
4398     /* WS_CHILD */
4399     SetLastError(0xdeadbeef);
4400     hwnd = CreateWindowEx(WS_EX_APPWINDOW, "static", NULL, WS_CHILD,
4401                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
4402     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
4403     expect_menu(hwnd, 1);
4404     expect_style(hwnd, WS_CHILD);
4405     expect_ex_style(hwnd, WS_EX_APPWINDOW);
4406     DestroyWindow(hwnd);
4407
4408     SetLastError(0xdeadbeef);
4409     hwnd = CreateWindowEx(WS_EX_APPWINDOW, "static", NULL, WS_CHILD | WS_CAPTION,
4410                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
4411     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
4412     expect_menu(hwnd, 1);
4413     expect_style(hwnd, WS_CHILD | WS_CAPTION);
4414     expect_ex_style(hwnd, WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
4415     DestroyWindow(hwnd);
4416
4417     SetLastError(0xdeadbeef);
4418     hwnd = CreateWindowEx(0, "static", NULL, WS_CHILD,
4419                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
4420     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
4421     expect_menu(hwnd, 1);
4422     expect_style(hwnd, WS_CHILD);
4423     expect_ex_style(hwnd, 0);
4424     DestroyWindow(hwnd);
4425
4426     SetLastError(0xdeadbeef);
4427     hwnd = CreateWindowEx(0, "static", NULL, WS_CHILD | WS_CAPTION,
4428                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
4429     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
4430     expect_menu(hwnd, 1);
4431     expect_style(hwnd, WS_CHILD | WS_CAPTION);
4432     expect_ex_style(hwnd, WS_EX_WINDOWEDGE);
4433     DestroyWindow(hwnd);
4434
4435     /* WS_POPUP */
4436     SetLastError(0xdeadbeef);
4437     hwnd = CreateWindowEx(WS_EX_APPWINDOW, "static", NULL, WS_POPUP,
4438                            0, 0, 100, 100, parent, hmenu, 0, NULL);
4439     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
4440     expect_menu(hwnd, hmenu);
4441     expect_style(hwnd, WS_POPUP | WS_CLIPSIBLINGS);
4442     expect_ex_style(hwnd, WS_EX_APPWINDOW);
4443     DestroyWindow(hwnd);
4444     SetLastError(0xdeadbeef);
4445     ok(!IsMenu(hmenu), "IsMenu should fail\n");
4446     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
4447
4448     hmenu = CreateMenu();
4449     assert(hmenu != 0);
4450     SetLastError(0xdeadbeef);
4451     hwnd = CreateWindowEx(WS_EX_APPWINDOW, "static", NULL, WS_POPUP | WS_CAPTION,
4452                            0, 0, 100, 100, parent, hmenu, 0, NULL);
4453     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
4454     expect_menu(hwnd, hmenu);
4455     expect_style(hwnd, WS_POPUP | WS_CAPTION | WS_CLIPSIBLINGS);
4456     expect_ex_style(hwnd, WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
4457     DestroyWindow(hwnd);
4458     SetLastError(0xdeadbeef);
4459     ok(!IsMenu(hmenu), "IsMenu should fail\n");
4460     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
4461
4462     hmenu = CreateMenu();
4463     assert(hmenu != 0);
4464     SetLastError(0xdeadbeef);
4465     hwnd = CreateWindowEx(0, "static", NULL, WS_POPUP,
4466                            0, 0, 100, 100, parent, hmenu, 0, NULL);
4467     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
4468     expect_menu(hwnd, hmenu);
4469     expect_style(hwnd, WS_POPUP | WS_CLIPSIBLINGS);
4470     expect_ex_style(hwnd, 0);
4471     DestroyWindow(hwnd);
4472     SetLastError(0xdeadbeef);
4473     ok(!IsMenu(hmenu), "IsMenu should fail\n");
4474     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
4475
4476     hmenu = CreateMenu();
4477     assert(hmenu != 0);
4478     SetLastError(0xdeadbeef);
4479     hwnd = CreateWindowEx(0, "static", NULL, WS_POPUP | WS_CAPTION,
4480                            0, 0, 100, 100, parent, hmenu, 0, NULL);
4481     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
4482     expect_menu(hwnd, hmenu);
4483     expect_style(hwnd, WS_POPUP | WS_CAPTION | WS_CLIPSIBLINGS);
4484     expect_ex_style(hwnd, WS_EX_WINDOWEDGE);
4485     DestroyWindow(hwnd);
4486     SetLastError(0xdeadbeef);
4487     ok(!IsMenu(hmenu), "IsMenu should fail\n");
4488     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
4489
4490     /* WS_CHILD | WS_POPUP */
4491     SetLastError(0xdeadbeef);
4492     hwnd = CreateWindowEx(WS_EX_APPWINDOW, "static", NULL, WS_CHILD | WS_POPUP,
4493                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
4494     ok(!hwnd || broken(hwnd != 0 /* Win9x */), "CreateWindowEx should fail\n");
4495     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
4496     if (hwnd)
4497         DestroyWindow(hwnd);
4498
4499     hmenu = CreateMenu();
4500     assert(hmenu != 0);
4501     SetLastError(0xdeadbeef);
4502     hwnd = CreateWindowEx(WS_EX_APPWINDOW, "static", NULL, WS_CHILD | WS_POPUP,
4503                            0, 0, 100, 100, parent, hmenu, 0, NULL);
4504     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
4505     expect_menu(hwnd, hmenu);
4506     expect_style(hwnd, WS_CHILD | WS_POPUP | WS_CLIPSIBLINGS);
4507     expect_ex_style(hwnd, WS_EX_APPWINDOW);
4508     DestroyWindow(hwnd);
4509     SetLastError(0xdeadbeef);
4510     ok(!IsMenu(hmenu), "IsMenu should fail\n");
4511     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
4512
4513     SetLastError(0xdeadbeef);
4514     hwnd = CreateWindowEx(WS_EX_APPWINDOW, "static", NULL, WS_CHILD | WS_POPUP | WS_CAPTION,
4515                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
4516     ok(!hwnd || broken(hwnd != 0 /* Win9x */), "CreateWindowEx should fail\n");
4517     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
4518     if (hwnd)
4519         DestroyWindow(hwnd);
4520
4521     hmenu = CreateMenu();
4522     assert(hmenu != 0);
4523     SetLastError(0xdeadbeef);
4524     hwnd = CreateWindowEx(WS_EX_APPWINDOW, "static", NULL, WS_CHILD | WS_POPUP | WS_CAPTION,
4525                            0, 0, 100, 100, parent, hmenu, 0, NULL);
4526     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
4527     expect_menu(hwnd, hmenu);
4528     expect_style(hwnd, WS_CHILD | WS_POPUP | WS_CAPTION | WS_CLIPSIBLINGS);
4529     expect_ex_style(hwnd, WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
4530     DestroyWindow(hwnd);
4531     SetLastError(0xdeadbeef);
4532     ok(!IsMenu(hmenu), "IsMenu should fail\n");
4533     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
4534
4535     SetLastError(0xdeadbeef);
4536     hwnd = CreateWindowEx(0, "static", NULL, WS_CHILD | WS_POPUP,
4537                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
4538     ok(!hwnd || broken(hwnd != 0 /* Win9x */), "CreateWindowEx should fail\n");
4539     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
4540     if (hwnd)
4541         DestroyWindow(hwnd);
4542
4543     hmenu = CreateMenu();
4544     assert(hmenu != 0);
4545     SetLastError(0xdeadbeef);
4546     hwnd = CreateWindowEx(0, "static", NULL, WS_CHILD | WS_POPUP,
4547                            0, 0, 100, 100, parent, hmenu, 0, NULL);
4548     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
4549     expect_menu(hwnd, hmenu);
4550     expect_style(hwnd, WS_CHILD | WS_POPUP | WS_CLIPSIBLINGS);
4551     expect_ex_style(hwnd, 0);
4552     DestroyWindow(hwnd);
4553     SetLastError(0xdeadbeef);
4554     ok(!IsMenu(hmenu), "IsMenu should fail\n");
4555     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
4556
4557     SetLastError(0xdeadbeef);
4558     hwnd = CreateWindowEx(0, "static", NULL, WS_CHILD | WS_POPUP | WS_CAPTION,
4559                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
4560     ok(!hwnd || broken(hwnd != 0 /* Win9x */), "CreateWindowEx should fail\n");
4561     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
4562     if (hwnd)
4563         DestroyWindow(hwnd);
4564
4565     hmenu = CreateMenu();
4566     assert(hmenu != 0);
4567     SetLastError(0xdeadbeef);
4568     hwnd = CreateWindowEx(0, "static", NULL, WS_CHILD | WS_POPUP | WS_CAPTION,
4569                            0, 0, 100, 100, parent, hmenu, 0, NULL);
4570     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
4571     expect_menu(hwnd, hmenu);
4572     expect_style(hwnd, WS_CHILD | WS_POPUP | WS_CAPTION | WS_CLIPSIBLINGS);
4573     expect_ex_style(hwnd, WS_EX_WINDOWEDGE);
4574     DestroyWindow(hwnd);
4575     SetLastError(0xdeadbeef);
4576     ok(!IsMenu(hmenu), "IsMenu should fail\n");
4577     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
4578
4579     /* test child window sizing */
4580     cls.style = 0;
4581     cls.lpfnWndProc = minmax_wnd_proc;
4582     cls.cbClsExtra = 0;
4583     cls.cbWndExtra = 0;
4584     cls.hInstance = GetModuleHandle(0);
4585     cls.hIcon = 0;
4586     cls.hCursor = LoadCursorA(0, IDC_ARROW);
4587     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
4588     cls.lpszMenuName = NULL;
4589     cls.lpszClassName = "MinMax_WndClass";
4590     RegisterClass(&cls);
4591
4592     SetLastError(0xdeadbeef);
4593     parent = CreateWindowEx(0, "MinMax_WndClass", NULL, WS_CAPTION | WS_SYSMENU | WS_THICKFRAME,
4594                            0, 0, 100, 100, 0, 0, 0, NULL);
4595     ok(parent != 0, "CreateWindowEx error %d\n", GetLastError());
4596     expect_menu(parent, 0);
4597     expect_style(parent, WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_CLIPSIBLINGS);
4598     expect_ex_style(parent, WS_EX_WINDOWEDGE);
4599
4600     memset(&minmax, 0, sizeof(minmax));
4601     SendMessage(parent, WM_GETMINMAXINFO, 0, (LPARAM)&minmax);
4602     SetRect(&rc_minmax, 0, 0, minmax.ptMaxSize.x, minmax.ptMaxSize.y);
4603     ok(IsRectEmpty(&rc_minmax), "ptMaxSize is not empty\n");
4604     SetRect(&rc_minmax, 0, 0, minmax.ptMaxTrackSize.x, minmax.ptMaxTrackSize.y);
4605     ok(IsRectEmpty(&rc_minmax), "ptMaxTrackSize is not empty\n");
4606
4607     GetWindowRect(parent, &rc);
4608     ok(!IsRectEmpty(&rc), "parent window rect is empty\n");
4609     GetClientRect(parent, &rc);
4610     ok(!IsRectEmpty(&rc), "parent client rect is empty\n");
4611
4612     InflateRect(&rc, 200, 200);
4613     trace("creating child with rect (%d,%d-%d,%d)\n", rc.left, rc.top, rc.right, rc.bottom);
4614
4615     SetLastError(0xdeadbeef);
4616     hwnd = CreateWindowEx(0, "MinMax_WndClass", NULL, WS_CHILD | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME,
4617                           rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
4618                           parent, (HMENU)1, 0, NULL);
4619     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
4620     expect_menu(hwnd, 1);
4621     expect_style(hwnd, WS_CHILD | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME);
4622     expect_ex_style(hwnd, WS_EX_WINDOWEDGE);
4623
4624     memset(&minmax, 0, sizeof(minmax));
4625     SendMessage(hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&minmax);
4626     SetRect(&rc_minmax, 0, 0, minmax.ptMaxTrackSize.x, minmax.ptMaxTrackSize.y);
4627
4628     GetWindowRect(hwnd, &rc);
4629     OffsetRect(&rc, -rc.left, -rc.top);
4630     ok(EqualRect(&rc, &rc_minmax), "rects don't match: (%d,%d-%d,%d) and (%d,%d-%d,%d)\n",
4631        rc.left, rc.top, rc.right, rc.bottom,
4632        rc_minmax.left, rc_minmax.top, rc_minmax.right, rc_minmax.bottom);
4633     DestroyWindow(hwnd);
4634
4635     cls.lpfnWndProc = winsizes_wnd_proc;
4636     cls.lpszClassName = "Sizes_WndClass";
4637     RegisterClass(&cls);
4638
4639     expected_cx = expected_cy = 200000;
4640     SetRect( &expected_rect, 0, 0, 200000, 200000 );
4641     broken_rect = expected_rect;
4642     hwnd = CreateWindowExA(0, "Sizes_WndClass", NULL, WS_CHILD, 300000, 300000, 200000, 200000, parent, 0, 0, NULL);
4643     ok( hwnd != 0, "creation failed err %u\n", GetLastError());
4644     GetClientRect( hwnd, &rc );
4645     ok( rc.right == 200000 || broken(rc.right == (short)200000), "invalid rect right %u\n", rc.right );
4646     ok( rc.bottom == 200000 || broken(rc.bottom == (short)200000), "invalid rect bottom %u\n", rc.bottom );
4647     DestroyWindow(hwnd);
4648
4649     expected_cx = expected_cy = -10;
4650     SetRect( &expected_rect, 0, 0, 0, 0 );
4651     SetRect( &broken_rect, 0, 0, -10, -10 );
4652     hwnd = CreateWindowExA(0, "Sizes_WndClass", NULL, WS_CHILD, -20, -20, -10, -10, parent, 0, 0, NULL);
4653     ok( hwnd != 0, "creation failed err %u\n", GetLastError());
4654     GetClientRect( hwnd, &rc );
4655     ok( rc.right == 0, "invalid rect right %u\n", rc.right );
4656     ok( rc.bottom == 0, "invalid rect bottom %u\n", rc.bottom );
4657     DestroyWindow(hwnd);
4658
4659     expected_cx = expected_cy = -200000;
4660     SetRect( &expected_rect, 0, 0, 0, 0 );
4661     SetRect( &broken_rect, 0, 0, -200000, -200000 );
4662     hwnd = CreateWindowExA(0, "Sizes_WndClass", NULL, WS_CHILD, -300000, -300000, -200000, -200000, parent, 0, 0, NULL);
4663     ok( hwnd != 0, "creation failed err %u\n", GetLastError());
4664     GetClientRect( hwnd, &rc );
4665     ok( rc.right == 0, "invalid rect right %u\n", rc.right );
4666     ok( rc.bottom == 0, "invalid rect bottom %u\n", rc.bottom );
4667     DestroyWindow(hwnd);
4668
4669     /* we need a parent at 0,0 so that child coordinates match */
4670     DestroyWindow(parent);
4671     parent = CreateWindowEx(0, "MinMax_WndClass", NULL, WS_POPUP, 0, 0, 100, 100, 0, 0, 0, NULL);
4672     ok(parent != 0, "CreateWindowEx error %d\n", GetLastError());
4673
4674     expected_cx = 100;
4675     expected_cy = 0x7fffffff;
4676     SetRect( &expected_rect, 10, 10, 110, 0x7fffffff );
4677     SetRect( &broken_rect, 10, 10, 110, 0x7fffffffU + 10 );
4678     hwnd = CreateWindowExA(0, "Sizes_WndClass", NULL, WS_CHILD, 10, 10, 100, 0x7fffffff, parent, 0, 0, NULL);
4679     ok( hwnd != 0, "creation failed err %u\n", GetLastError());
4680     GetClientRect( hwnd, &rc );
4681     ok( rc.right == 100, "invalid rect right %u\n", rc.right );
4682     ok( rc.bottom == 0x7fffffff - 10 || broken(rc.bottom == 0), "invalid rect bottom %u\n", rc.bottom );
4683     DestroyWindow(hwnd);
4684
4685     expected_cx = 0x7fffffff;
4686     expected_cy = 0x7fffffff;
4687     SetRect( &expected_rect, 20, 10, 0x7fffffff, 0x7fffffff );
4688     SetRect( &broken_rect, 20, 10, 0x7fffffffU + 20, 0x7fffffffU + 10 );
4689     hwnd = CreateWindowExA(0, "Sizes_WndClass", NULL, WS_CHILD, 20, 10, 0x7fffffff, 0x7fffffff, parent, 0, 0, NULL);
4690     ok( hwnd != 0, "creation failed err %u\n", GetLastError());
4691     GetClientRect( hwnd, &rc );
4692     ok( rc.right == 0x7fffffff - 20 || broken(rc.right == 0), "invalid rect right %u\n", rc.right );
4693     ok( rc.bottom == 0x7fffffff - 10 || broken(rc.bottom == 0), "invalid rect bottom %u\n", rc.bottom );
4694     DestroyWindow(hwnd);
4695
4696     /* top level window */
4697     expected_cx = expected_cy = 200000;
4698     SetRect( &expected_rect, 0, 0, GetSystemMetrics(SM_CXMAXTRACK), GetSystemMetrics(SM_CYMAXTRACK) );
4699     hwnd = CreateWindowExA(0, "Sizes_WndClass", NULL, WS_OVERLAPPEDWINDOW, 300000, 300000, 200000, 200000, 0, 0, 0, NULL);
4700     ok( hwnd != 0, "creation failed err %u\n", GetLastError());
4701     GetClientRect( hwnd, &rc );
4702     ok( rc.right <= expected_cx, "invalid rect right %u\n", rc.right );
4703     ok( rc.bottom <= expected_cy, "invalid rect bottom %u\n", rc.bottom );
4704     DestroyWindow(hwnd);
4705
4706     DestroyWindow(parent);
4707
4708     UnregisterClass("MinMax_WndClass", GetModuleHandle(0));
4709     UnregisterClass("Sizes_WndClass", GetModuleHandle(0));
4710
4711 #undef expect_gle_broken_9x
4712 #undef expect_menu
4713 #undef expect_style
4714 #undef expect_ex_style
4715 }
4716
4717 /* function that remembers whether the system the test is running on sets the
4718  * last error for user32 functions to make the tests stricter */
4719 static int check_error(DWORD actual, DWORD expected)
4720 {
4721     static int sets_last_error = -1;
4722     if (sets_last_error == -1)
4723         sets_last_error = (actual != 0xdeadbeef);
4724     return (!sets_last_error && (actual == 0xdeadbeef)) || (actual == expected);
4725 }
4726
4727 static void test_SetWindowLong(void)
4728 {
4729     LONG_PTR retval;
4730     WNDPROC old_window_procW;
4731
4732     SetLastError(0xdeadbeef);
4733     retval = SetWindowLongPtr(NULL, GWLP_WNDPROC, 0);
4734     ok(!retval, "SetWindowLongPtr on invalid window handle should have returned 0 instead of 0x%lx\n", retval);
4735     ok(check_error(GetLastError(), ERROR_INVALID_WINDOW_HANDLE),
4736         "SetWindowLongPtr should have set error to ERROR_INVALID_WINDOW_HANDLE instad of %d\n", GetLastError());
4737
4738     SetLastError(0xdeadbeef);
4739     retval = SetWindowLongPtr(hwndMain, 0xdeadbeef, 0);
4740     ok(!retval, "SetWindowLongPtr on invalid index should have returned 0 instead of 0x%lx\n", retval);
4741     ok(check_error(GetLastError(), ERROR_INVALID_INDEX),
4742         "SetWindowLongPtr should have set error to ERROR_INVALID_INDEX instad of %d\n", GetLastError());
4743
4744     SetLastError(0xdeadbeef);
4745     retval = SetWindowLongPtr(hwndMain, GWLP_WNDPROC, 0);
4746     ok((WNDPROC)retval == main_window_procA || broken(!retval), /* win9x */
4747         "SetWindowLongPtr on invalid window proc should have returned address of main_window_procA instead of 0x%lx\n", retval);
4748     ok(GetLastError() == 0xdeadbeef, "SetWindowLongPtr shouldn't have set the last error, instead of setting it to %d\n", GetLastError());
4749     retval = GetWindowLongPtr(hwndMain, GWLP_WNDPROC);
4750     ok((WNDPROC)retval == main_window_procA,
4751         "SetWindowLongPtr on invalid window proc shouldn't have changed the value returned by GetWindowLongPtr, instead of changing it to 0x%lx\n", retval);
4752     ok(!IsWindowUnicode(hwndMain), "hwndMain shouldn't be Unicode\n");
4753
4754     old_window_procW = (WNDPROC)GetWindowLongPtrW(hwndMain, GWLP_WNDPROC);
4755     SetLastError(0xdeadbeef);
4756     retval = SetWindowLongPtrW(hwndMain, GWLP_WNDPROC, 0);
4757     if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
4758     {
4759         ok(GetLastError() == 0xdeadbeef, "SetWindowLongPtr shouldn't have set the last error, instead of setting it to %d\n", GetLastError());
4760         ok(retval != 0, "SetWindowLongPtr error %d\n", GetLastError());
4761         ok((WNDPROC)retval == old_window_procW,
4762             "SetWindowLongPtr on invalid window proc shouldn't have changed the value returned by GetWindowLongPtr, instead of changing it to 0x%lx\n", retval);
4763         ok(IsWindowUnicode(hwndMain), "hwndMain should now be Unicode\n");
4764
4765         /* set it back to ANSI */
4766         SetWindowLongPtr(hwndMain, GWLP_WNDPROC, 0);
4767     }
4768 }
4769
4770 static void test_ShowWindow(void)
4771 {
4772     HWND hwnd;
4773     DWORD style;
4774     RECT rcMain, rc;
4775     LPARAM ret;
4776
4777     SetRect(&rcMain, 120, 120, 210, 210);
4778
4779     hwnd = CreateWindowEx(0, "MainWindowClass", NULL,
4780                           WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
4781                           WS_MAXIMIZEBOX | WS_POPUP,
4782                           rcMain.left, rcMain.top,
4783                           rcMain.right - rcMain.left, rcMain.bottom - rcMain.top,
4784                           0, 0, 0, NULL);
4785     assert(hwnd);
4786
4787     style = GetWindowLong(hwnd, GWL_STYLE);
4788     ok(!(style & WS_DISABLED), "window should not be disabled\n");
4789     ok(!(style & WS_VISIBLE), "window should not be visible\n");
4790     ok(!(style & WS_MINIMIZE), "window should not be minimized\n");
4791     ok(!(style & WS_MAXIMIZE), "window should not be maximized\n");
4792     GetWindowRect(hwnd, &rc);
4793     ok(EqualRect(&rcMain, &rc), "expected (%d,%d)-(%d,%d), got (%d,%d)-(%d,%d)\n",
4794        rcMain.left, rcMain.top, rcMain.right, rcMain.bottom,
4795        rc.left, rc.top, rc.right, rc.bottom);
4796
4797     ret = ShowWindow(hwnd, SW_SHOW);
4798     ok(!ret, "not expected ret: %lu\n", ret);
4799     style = GetWindowLong(hwnd, GWL_STYLE);
4800     ok(!(style & WS_DISABLED), "window should not be disabled\n");
4801     ok(style & WS_VISIBLE, "window should be visible\n");
4802     ok(!(style & WS_MINIMIZE), "window should not be minimized\n");
4803     ok(!(style & WS_MAXIMIZE), "window should not be maximized\n");
4804     GetWindowRect(hwnd, &rc);
4805     ok(EqualRect(&rcMain, &rc), "expected (%d,%d)-(%d,%d), got (%d,%d)-(%d,%d)\n",
4806        rcMain.left, rcMain.top, rcMain.right, rcMain.bottom,
4807        rc.left, rc.top, rc.right, rc.bottom);
4808
4809     ret = ShowWindow(hwnd, SW_MINIMIZE);
4810     ok(ret, "not expected ret: %lu\n", ret);
4811     style = GetWindowLong(hwnd, GWL_STYLE);
4812     ok(!(style & WS_DISABLED), "window should not be disabled\n");
4813     ok(style & WS_VISIBLE, "window should be visible\n");
4814     ok(style & WS_MINIMIZE, "window should be minimized\n");
4815     ok(!(style & WS_MAXIMIZE), "window should not be maximized\n");
4816     GetWindowRect(hwnd, &rc);
4817     ok(!EqualRect(&rcMain, &rc), "rects shouldn't match\n");
4818
4819     ShowWindow(hwnd, SW_RESTORE);
4820     ok(ret, "not expected ret: %lu\n", ret);
4821     style = GetWindowLong(hwnd, GWL_STYLE);
4822     ok(!(style & WS_DISABLED), "window should not be disabled\n");
4823     ok(style & WS_VISIBLE, "window should be visible\n");
4824     ok(!(style & WS_MINIMIZE), "window should not be minimized\n");
4825     ok(!(style & WS_MAXIMIZE), "window should not be maximized\n");
4826     GetWindowRect(hwnd, &rc);
4827     ok(EqualRect(&rcMain, &rc), "expected (%d,%d)-(%d,%d), got (%d,%d)-(%d,%d)\n",
4828        rcMain.left, rcMain.top, rcMain.right, rcMain.bottom,
4829        rc.left, rc.top, rc.right, rc.bottom);
4830
4831     ret = EnableWindow(hwnd, FALSE);
4832     ok(!ret, "not expected ret: %lu\n", ret);
4833     style = GetWindowLong(hwnd, GWL_STYLE);
4834     ok(style & WS_DISABLED, "window should be disabled\n");
4835
4836     ret = DefWindowProc(hwnd, WM_SYSCOMMAND, SC_MINIMIZE, 0);
4837     ok(!ret, "not expected ret: %lu\n", ret);
4838     style = GetWindowLong(hwnd, GWL_STYLE);
4839     ok(style & WS_DISABLED, "window should be disabled\n");
4840     ok(style & WS_VISIBLE, "window should be visible\n");
4841     ok(!(style & WS_MINIMIZE), "window should not be minimized\n");
4842     ok(!(style & WS_MAXIMIZE), "window should not be maximized\n");
4843     GetWindowRect(hwnd, &rc);
4844     ok(EqualRect(&rcMain, &rc), "expected (%d,%d)-(%d,%d), got (%d,%d)-(%d,%d)\n",
4845        rcMain.left, rcMain.top, rcMain.right, rcMain.bottom,
4846        rc.left, rc.top, rc.right, rc.bottom);
4847
4848     ret = DefWindowProc(hwnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
4849     ok(!ret, "not expected ret: %lu\n", ret);
4850     style = GetWindowLong(hwnd, GWL_STYLE);
4851     ok(style & WS_DISABLED, "window should be disabled\n");
4852     ok(style & WS_VISIBLE, "window should be visible\n");
4853     ok(!(style & WS_MINIMIZE), "window should not be minimized\n");
4854     ok(!(style & WS_MAXIMIZE), "window should not be maximized\n");
4855     GetWindowRect(hwnd, &rc);
4856     ok(EqualRect(&rcMain, &rc), "expected (%d,%d)-(%d,%d), got (%d,%d)-(%d,%d)\n",
4857        rcMain.left, rcMain.top, rcMain.right, rcMain.bottom,
4858        rc.left, rc.top, rc.right, rc.bottom);
4859
4860     ret = ShowWindow(hwnd, SW_MINIMIZE);
4861     ok(ret, "not expected ret: %lu\n", ret);
4862     style = GetWindowLong(hwnd, GWL_STYLE);
4863     ok(style & WS_DISABLED, "window should be disabled\n");
4864     ok(style & WS_VISIBLE, "window should be visible\n");
4865     ok(style & WS_MINIMIZE, "window should be minimized\n");
4866     ok(!(style & WS_MAXIMIZE), "window should not be maximized\n");
4867     GetWindowRect(hwnd, &rc);
4868     ok(!EqualRect(&rcMain, &rc), "rects shouldn't match\n");
4869
4870     ret = DefWindowProc(hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
4871     ok(!ret, "not expected ret: %lu\n", ret);
4872     style = GetWindowLong(hwnd, GWL_STYLE);
4873     ok(style & WS_DISABLED, "window should be disabled\n");
4874     ok(style & WS_VISIBLE, "window should be visible\n");
4875     ok(style & WS_MINIMIZE, "window should be minimized\n");
4876     ok(!(style & WS_MAXIMIZE), "window should not be maximized\n");
4877     GetWindowRect(hwnd, &rc);
4878     ok(!EqualRect(&rcMain, &rc), "rects shouldn't match\n");
4879
4880     ret = ShowWindow(hwnd, SW_RESTORE);
4881     ok(ret, "not expected ret: %lu\n", ret);
4882     style = GetWindowLong(hwnd, GWL_STYLE);
4883     ok(style & WS_DISABLED, "window should be disabled\n");
4884     ok(style & WS_VISIBLE, "window should be visible\n");
4885     ok(!(style & WS_MINIMIZE), "window should not be minimized\n");
4886     ok(!(style & WS_MAXIMIZE), "window should not be maximized\n");
4887     GetWindowRect(hwnd, &rc);
4888     ok(EqualRect(&rcMain, &rc), "expected (%d,%d)-(%d,%d), got (%d,%d)-(%d,%d)\n",
4889        rcMain.left, rcMain.top, rcMain.right, rcMain.bottom,
4890        rc.left, rc.top, rc.right, rc.bottom);
4891
4892     ret = DefWindowProc(hwnd, WM_SYSCOMMAND, SC_CLOSE, 0);
4893     ok(!ret, "not expected ret: %lu\n", ret);
4894     ok(IsWindow(hwnd), "window should exist\n");
4895
4896     ret = EnableWindow(hwnd, TRUE);
4897     ok(ret, "not expected ret: %lu\n", ret);
4898
4899     ret = DefWindowProc(hwnd, WM_SYSCOMMAND, SC_CLOSE, 0);
4900     ok(!ret, "not expected ret: %lu\n", ret);
4901     ok(!IsWindow(hwnd), "window should not exist\n");
4902 }
4903
4904 static void test_gettext(void)
4905 {
4906     WNDCLASS cls;
4907     LPCSTR clsname = "gettexttest";
4908     HWND hwnd;
4909     LRESULT r;
4910
4911     memset( &cls, 0, sizeof cls );
4912     cls.lpfnWndProc = DefWindowProc;
4913     cls.lpszClassName = clsname;
4914     cls.hInstance = GetModuleHandle(NULL);
4915
4916     if (!RegisterClass( &cls )) return;
4917
4918     hwnd = CreateWindow( clsname, "test text", WS_OVERLAPPED, 0, 0, 10, 10, 0, NULL, NULL, NULL);
4919     ok( hwnd != NULL, "window was null\n");
4920
4921     r = SendMessage( hwnd, WM_GETTEXT, 0x10, 0x1000);
4922     ok( r == 0, "settext should return zero\n");
4923
4924     r = SendMessage( hwnd, WM_GETTEXT, 0x10000, 0);
4925     ok( r == 0, "settext should return zero (%ld)\n", r);
4926
4927     r = SendMessage( hwnd, WM_GETTEXT, 0xff000000, 0x1000);
4928     ok( r == 0, "settext should return zero (%ld)\n", r);
4929
4930     r = SendMessage( hwnd, WM_GETTEXT, 0x1000, 0xff000000);
4931     ok( r == 0, "settext should return zero (%ld)\n", r);
4932
4933     DestroyWindow(hwnd);
4934     UnregisterClass( clsname, NULL );
4935 }
4936
4937
4938 static void test_GetUpdateRect(void)
4939 {
4940     MSG msg;
4941     BOOL ret, parent_wm_paint, grandparent_wm_paint;
4942     RECT rc1, rc2;
4943     HWND hgrandparent, hparent, hchild;
4944     WNDCLASSA cls;
4945     static const char classNameA[] = "GetUpdateRectClass";
4946
4947     hgrandparent = CreateWindowA("static", "grandparent", WS_OVERLAPPEDWINDOW,
4948                                  0, 0, 100, 100, NULL, NULL, 0, NULL);
4949
4950     hparent = CreateWindowA("static", "parent", WS_CHILD|WS_VISIBLE,
4951                             0, 0, 100, 100, hgrandparent, NULL, 0, NULL);
4952
4953     hchild = CreateWindowA("static", "child", WS_CHILD|WS_VISIBLE,
4954                             10, 10, 30, 30, hparent, NULL, 0, NULL);
4955
4956     ShowWindow(hgrandparent, SW_SHOW);
4957     UpdateWindow(hgrandparent);
4958     flush_events( TRUE );
4959
4960     ShowWindow(hchild, SW_HIDE);
4961     SetRect(&rc2, 0, 0, 0, 0);
4962     ret = GetUpdateRect(hgrandparent, &rc1, FALSE);
4963     ok(!ret, "GetUpdateRect returned not empty region\n");
4964     ok(EqualRect(&rc1, &rc2), "rects do not match (%d,%d,%d,%d) / (%d,%d,%d,%d)\n",
4965        rc1.left, rc1.top, rc1.right, rc1.bottom,
4966        rc2.left, rc2.top, rc2.right, rc2.bottom);
4967
4968     SetRect(&rc2, 10, 10, 40, 40);
4969     ret = GetUpdateRect(hparent, &rc1, FALSE);
4970     ok(ret, "GetUpdateRect returned empty region\n");
4971     ok(EqualRect(&rc1, &rc2), "rects do not match (%d,%d,%d,%d) / (%d,%d,%d,%d)\n",
4972             rc1.left, rc1.top, rc1.right, rc1.bottom,
4973             rc2.left, rc2.top, rc2.right, rc2.bottom);
4974
4975     parent_wm_paint = FALSE;
4976     grandparent_wm_paint = FALSE;
4977     while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
4978     {
4979         if (msg.message == WM_PAINT)
4980         {
4981             if (msg.hwnd == hgrandparent) grandparent_wm_paint = TRUE;
4982             if (msg.hwnd == hparent) parent_wm_paint = TRUE;
4983         }
4984         DispatchMessage(&msg);
4985     }
4986     ok(parent_wm_paint, "WM_PAINT should have been received in parent\n");
4987     ok(!grandparent_wm_paint, "WM_PAINT should NOT have been received in grandparent\n");
4988
4989     DestroyWindow(hgrandparent);
4990
4991     cls.style = 0;
4992     cls.lpfnWndProc = DefWindowProcA;
4993     cls.cbClsExtra = 0;
4994     cls.cbWndExtra = 0;
4995     cls.hInstance = GetModuleHandleA(0);
4996     cls.hIcon = 0;
4997     cls.hCursor = LoadCursorA(0, IDC_ARROW);
4998     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
4999     cls.lpszMenuName = NULL;
5000     cls.lpszClassName = classNameA;
5001
5002     if(!RegisterClassA(&cls)) {
5003        trace("Register failed %d\n", GetLastError());
5004        return;
5005     }
5006
5007     hgrandparent = CreateWindowA(classNameA, "grandparent", WS_OVERLAPPEDWINDOW,
5008                                  0, 0, 100, 100, NULL, NULL, 0, NULL);
5009
5010     hparent = CreateWindowA(classNameA, "parent", WS_CHILD|WS_VISIBLE,
5011                             0, 0, 100, 100, hgrandparent, NULL, 0, NULL);
5012
5013     hchild = CreateWindowA(classNameA, "child", WS_CHILD|WS_VISIBLE,
5014                             10, 10, 30, 30, hparent, NULL, 0, NULL);
5015
5016     ShowWindow(hgrandparent, SW_SHOW);
5017     UpdateWindow(hgrandparent);
5018     flush_events( TRUE );
5019
5020     ret = GetUpdateRect(hgrandparent, &rc1, FALSE);
5021     ok(!ret, "GetUpdateRect returned not empty region\n");
5022
5023     ShowWindow(hchild, SW_HIDE);
5024
5025     SetRect(&rc2, 0, 0, 0, 0);
5026     ret = GetUpdateRect(hgrandparent, &rc1, FALSE);
5027     ok(!ret, "GetUpdateRect returned not empty region\n");
5028     ok(EqualRect(&rc1, &rc2), "rects do not match (%d,%d,%d,%d) / (%d,%d,%d,%d)\n",
5029        rc1.left, rc1.top, rc1.right, rc1.bottom,
5030        rc2.left, rc2.top, rc2.right, rc2.bottom);
5031
5032     SetRect(&rc2, 10, 10, 40, 40);
5033     ret = GetUpdateRect(hparent, &rc1, FALSE);
5034     ok(ret, "GetUpdateRect returned empty region\n");
5035     ok(EqualRect(&rc1, &rc2), "rects do not match (%d,%d,%d,%d) / (%d,%d,%d,%d)\n",
5036             rc1.left, rc1.top, rc1.right, rc1.bottom,
5037             rc2.left, rc2.top, rc2.right, rc2.bottom);
5038
5039     parent_wm_paint = FALSE;
5040     grandparent_wm_paint = FALSE;
5041     while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
5042     {
5043         if (msg.message == WM_PAINT)
5044         {
5045             if (msg.hwnd == hgrandparent) grandparent_wm_paint = TRUE;
5046             if (msg.hwnd == hparent) parent_wm_paint = TRUE;
5047         }
5048         DispatchMessage(&msg);
5049     }
5050     ok(parent_wm_paint, "WM_PAINT should have been received in parent\n");
5051     ok(!grandparent_wm_paint, "WM_PAINT should NOT have been received in grandparent\n");
5052
5053     DestroyWindow(hgrandparent);
5054 }
5055
5056
5057 static LRESULT CALLBACK TestExposedRegion_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
5058 {
5059     if(msg == WM_PAINT)
5060     {
5061         PAINTSTRUCT ps;
5062         RECT updateRect;
5063         DWORD waitResult;
5064         HWND win;
5065         const int waitTime = 2000;
5066
5067         BeginPaint(hwnd, &ps);
5068
5069         /* create and destroy window to create an exposed region on this window */
5070         win = CreateWindowA("static", "win", WS_VISIBLE,
5071                              10,10,50,50, NULL, NULL, 0, NULL);
5072         DestroyWindow(win);
5073
5074         waitResult = MsgWaitForMultipleObjects( 0, NULL, FALSE, waitTime, QS_PAINT );
5075
5076         ValidateRect(hwnd, NULL);
5077         EndPaint(hwnd, &ps);
5078
5079         if(waitResult != WAIT_TIMEOUT)
5080         {
5081             GetUpdateRect(hwnd, &updateRect, FALSE);
5082             ok(IsRectEmpty(&updateRect), "Exposed rect should be empty\n");
5083         }
5084
5085         return 1;
5086     }
5087     return DefWindowProc(hwnd, msg, wParam, lParam);
5088 }
5089
5090 static void test_Expose(void)
5091 {
5092     ATOM atom;
5093     WNDCLASSA cls;
5094     HWND mw;
5095     memset(&cls, 0, sizeof(WNDCLASSA));
5096     cls.lpfnWndProc = TestExposedRegion_WndProc;
5097     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
5098     cls.lpszClassName = "TestExposeClass";
5099     atom = RegisterClassA(&cls);
5100
5101     mw = CreateWindowA("TestExposeClass", "MainWindow", WS_VISIBLE|WS_OVERLAPPEDWINDOW,
5102                             0, 0, 200, 100, NULL, NULL, 0, NULL);
5103
5104     UpdateWindow(mw);
5105     DestroyWindow(mw);
5106 }
5107
5108 static LRESULT CALLBACK TestNCRedraw_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
5109 {
5110     static UINT ncredrawflags;
5111     PAINTSTRUCT ps;
5112
5113     switch(msg)
5114     {
5115     case WM_CREATE:
5116         ncredrawflags = *(UINT *) (((CREATESTRUCT *)lParam)->lpCreateParams);
5117         return 0;
5118     case WM_NCPAINT:
5119         RedrawWindow(hwnd, NULL, NULL, ncredrawflags);
5120         break;
5121     case WM_PAINT:
5122         BeginPaint(hwnd, &ps);
5123         EndPaint(hwnd, &ps);
5124         return 0;
5125     }
5126     return DefWindowProc(hwnd, msg, wParam, lParam);
5127 }
5128
5129 static void run_NCRedrawLoop(UINT flags)
5130 {
5131     HWND hwnd;
5132     MSG msg;
5133
5134     UINT loopcount = 0;
5135
5136     hwnd = CreateWindowA("TestNCRedrawClass", "MainWindow",
5137                          WS_OVERLAPPEDWINDOW, 0, 0, 200, 100,
5138                          NULL, NULL, 0, &flags);
5139     ShowWindow(hwnd, SW_SHOW);
5140     UpdateWindow(hwnd);
5141     while(PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE) != 0)
5142     {
5143         if (msg.message == WM_PAINT) loopcount++;
5144         if (loopcount >= 100) break;
5145         TranslateMessage(&msg);
5146         DispatchMessage(&msg);
5147         MsgWaitForMultipleObjects(0, NULL, FALSE, 100, QS_ALLINPUT);
5148     }
5149     if (flags == (RDW_INVALIDATE | RDW_FRAME))
5150         todo_wine ok(loopcount < 100, "Detected infinite WM_PAINT loop (%x).\n", flags);
5151     else
5152         ok(loopcount < 100, "Detected infinite WM_PAINT loop (%x).\n", flags);
5153     DestroyWindow(hwnd);
5154 }
5155
5156 static void test_NCRedraw(void)
5157 {
5158     WNDCLASSA wndclass;
5159
5160     wndclass.lpszClassName = "TestNCRedrawClass";
5161     wndclass.style = CS_HREDRAW | CS_VREDRAW;
5162     wndclass.lpfnWndProc = TestNCRedraw_WndProc;
5163     wndclass.cbClsExtra = 0;
5164     wndclass.cbWndExtra = 0;
5165     wndclass.hInstance = 0;
5166     wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
5167     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5168     wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
5169     wndclass.lpszMenuName = NULL;
5170
5171     RegisterClassA(&wndclass);
5172
5173     run_NCRedrawLoop(RDW_INVALIDATE | RDW_FRAME);
5174     run_NCRedrawLoop(RDW_INVALIDATE);
5175 }
5176
5177 static void test_GetWindowModuleFileName(void)
5178 {
5179     HWND hwnd;
5180     HINSTANCE hinst;
5181     UINT ret1, ret2;
5182     char buf1[MAX_PATH], buf2[MAX_PATH];
5183
5184     if (!pGetWindowModuleFileNameA)
5185     {
5186         win_skip("GetWindowModuleFileNameA is not available\n");
5187         return;
5188     }
5189
5190     hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0,0,0,0, 0, 0, 0, NULL);
5191     assert(hwnd);
5192
5193     hinst = (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE);
5194     ok(hinst == 0 || broken(hinst == GetModuleHandle(0)), /* win9x */ "expected 0, got %p\n", hinst);
5195
5196     buf1[0] = 0;
5197     SetLastError(0xdeadbeef);
5198     ret1 = GetModuleFileName(hinst, buf1, sizeof(buf1));
5199     ok(ret1, "GetModuleFileName error %u\n", GetLastError());
5200
5201     buf2[0] = 0;
5202     SetLastError(0xdeadbeef);
5203     ret2 = pGetWindowModuleFileNameA(hwnd, buf2, sizeof(buf2));
5204     ok(ret2 || broken(!ret2), /* nt4 sp 3 */
5205        "GetWindowModuleFileNameA error %u\n", GetLastError());
5206
5207     if (ret2)
5208     {
5209         ok(ret1 == ret2 || broken(ret2 == ret1 + 1), /* win98 */ "%u != %u\n", ret1, ret2);
5210         ok(!strcmp(buf1, buf2), "%s != %s\n", buf1, buf2);
5211     }
5212     hinst = GetModuleHandle(0);
5213
5214     SetLastError(0xdeadbeef);
5215     ret2 = GetModuleFileName(hinst, buf2, ret1 - 2);
5216     ok(ret2 == ret1 - 2 || broken(ret2 == ret1 - 3), /* win98 */
5217        "expected %u, got %u\n", ret1 - 2, ret2);
5218     ok(GetLastError() == 0xdeadbeef /* XP */ ||
5219        GetLastError() == ERROR_INSUFFICIENT_BUFFER, /* win2k3, vista */
5220        "expected 0xdeadbeef or ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
5221
5222     SetLastError(0xdeadbeef);
5223     ret2 = GetModuleFileName(hinst, buf2, 0);
5224     ok(!ret2, "GetModuleFileName should return 0\n");
5225     ok(GetLastError() == 0xdeadbeef /* XP */ ||
5226        GetLastError() == ERROR_INSUFFICIENT_BUFFER, /* win2k3, vista */
5227        "expected 0xdeadbeef or ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
5228
5229     SetLastError(0xdeadbeef);
5230     ret2 = pGetWindowModuleFileNameA(hwnd, buf2, ret1 - 2);
5231     ok(ret2 == ret1 - 2 || broken(ret2 == ret1 - 3) /* win98 */ || broken(!ret2), /* nt4 sp3 */
5232        "expected %u, got %u\n", ret1 - 2, ret2);
5233     ok(GetLastError() == 0xdeadbeef /* XP */ ||
5234        GetLastError() == ERROR_INSUFFICIENT_BUFFER, /* win2k3, vista */
5235        "expected 0xdeadbeef or ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
5236
5237     SetLastError(0xdeadbeef);
5238     ret2 = pGetWindowModuleFileNameA(hwnd, buf2, 0);
5239     ok(!ret2, "expected 0, got %u\n", ret2);
5240     ok(GetLastError() == 0xdeadbeef /* XP */ ||
5241        GetLastError() == ERROR_INSUFFICIENT_BUFFER, /* win2k3, vista */
5242        "expected 0xdeadbeef or ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
5243
5244     DestroyWindow(hwnd);
5245
5246     buf2[0] = 0;
5247     hwnd = (HWND)0xdeadbeef;
5248     SetLastError(0xdeadbeef);
5249     ret1 = pGetWindowModuleFileNameA(hwnd, buf1, sizeof(buf1));
5250     ok(!ret1, "expected 0, got %u\n", ret1);
5251     ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE || broken(GetLastError() == 0xdeadbeef), /* win9x */
5252        "expected ERROR_INVALID_WINDOW_HANDLE, got %u\n", GetLastError());
5253
5254     hwnd = FindWindow("Shell_TrayWnd", NULL);
5255     ok(IsWindow(hwnd) || broken(!hwnd), "got invalid tray window %p\n", hwnd);
5256     SetLastError(0xdeadbeef);
5257     ret1 = pGetWindowModuleFileNameA(hwnd, buf1, sizeof(buf1));
5258     ok(!ret1 || broken(ret1), /* win98 */ "expected 0, got %u\n", ret1);
5259
5260     if (!ret1)  /* inter-process GetWindowModuleFileName works on win9x, so don't test the desktop there */
5261     {
5262         ret1 = GetModuleFileName(0, buf1, sizeof(buf1));
5263         hwnd = GetDesktopWindow();
5264         ok(IsWindow(hwnd), "got invalid desktop window %p\n", hwnd);
5265         SetLastError(0xdeadbeef);
5266         ret2 = pGetWindowModuleFileNameA(hwnd, buf2, sizeof(buf2));
5267         ok(!ret2 ||
5268            ret1 == ret2 || /* vista */
5269            broken(ret2),  /* some win98 return user.exe as file name */
5270            "expected 0 or %u, got %u %s\n", ret1, ret2, buf2);
5271     }
5272 }
5273
5274 static void test_hwnd_message(void)
5275 {
5276     static const WCHAR mainwindowclassW[] = {'M','a','i','n','W','i','n','d','o','w','C','l','a','s','s',0};
5277     static const WCHAR message_windowW[] = {'m','e','s','s','a','g','e',' ','w','i','n','d','o','w',0};
5278
5279     HWND parent = 0, hwnd, found;
5280     RECT rect;
5281
5282     /* HWND_MESSAGE is not supported below w2k, but win9x return != 0
5283        on CreateWindowExA and crash later in the test.
5284        Use UNICODE here to fail on win9x */
5285     hwnd = CreateWindowExW(0, mainwindowclassW, message_windowW, WS_CAPTION | WS_VISIBLE,
5286                            100, 100, 200, 200, HWND_MESSAGE, 0, 0, NULL);
5287     if (!hwnd)
5288     {
5289         win_skip("CreateWindowExW with parent HWND_MESSAGE failed\n");
5290         return;
5291     }
5292
5293     ok( !GetParent(hwnd), "GetParent should return 0 for message only windows\n" );
5294     if (pGetAncestor)
5295     {
5296         char buffer[100];
5297         HWND root, desktop = GetDesktopWindow();
5298
5299         parent = pGetAncestor(hwnd, GA_PARENT);
5300         ok(parent != 0, "GetAncestor(GA_PARENT) should not return 0 for message windows\n");
5301         ok(parent != desktop, "GetAncestor(GA_PARENT) should not return desktop for message windows\n");
5302         root = pGetAncestor(hwnd, GA_ROOT);
5303         ok(root == hwnd, "GetAncestor(GA_ROOT) should return hwnd for message windows\n");
5304         ok( !pGetAncestor(parent, GA_PARENT) || broken(pGetAncestor(parent, GA_PARENT) != 0), /* win2k */
5305             "parent shouldn't have parent %p\n", pGetAncestor(parent, GA_PARENT) );
5306         trace("parent %p root %p desktop %p\n", parent, root, desktop);
5307         if (!GetClassNameA( parent, buffer, sizeof(buffer) )) buffer[0] = 0;
5308         ok( !lstrcmpi( buffer, "Message" ), "wrong parent class '%s'\n", buffer );
5309         GetWindowRect( parent, &rect );
5310         ok( rect.left == 0 && rect.right == 100 && rect.top == 0 && rect.bottom == 100,
5311             "wrong parent rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
5312     }
5313     GetWindowRect( hwnd, &rect );
5314     ok( rect.left == 100 && rect.right == 300 && rect.top == 100 && rect.bottom == 300,
5315         "wrong window rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
5316
5317     /* test FindWindow behavior */
5318
5319     found = FindWindowExA( 0, 0, 0, "message window" );
5320     ok( found == hwnd, "didn't find message window %p/%p\n", found, hwnd );
5321     SetLastError(0xdeadbeef);
5322     found = FindWindowExA( GetDesktopWindow(), 0, 0, "message window" );
5323     ok( found == 0, "found message window %p/%p\n", found, hwnd );
5324     ok( GetLastError() == 0xdeadbeef, "expected deadbeef, got %d\n", GetLastError() );
5325     if (parent)
5326     {
5327         found = FindWindowExA( parent, 0, 0, "message window" );
5328         ok( found == hwnd, "didn't find message window %p/%p\n", found, hwnd );
5329     }
5330
5331     /* test IsChild behavior */
5332
5333     if (parent) ok( !IsChild( parent, hwnd ), "HWND_MESSAGE is child of top window\n" );
5334
5335     /* test IsWindowVisible behavior */
5336
5337     ok( !IsWindowVisible( hwnd ), "HWND_MESSAGE window is visible\n" );
5338     if (parent) ok( !IsWindowVisible( parent ), "HWND_MESSAGE parent is visible\n" );
5339
5340     DestroyWindow(hwnd);
5341 }
5342
5343 static void test_layered_window(void)
5344 {
5345     HWND hwnd;
5346     COLORREF key = 0;
5347     BYTE alpha = 0;
5348     DWORD flags = 0;
5349     BOOL ret;
5350
5351     if (!pGetLayeredWindowAttributes || !pSetLayeredWindowAttributes)
5352     {
5353         win_skip( "layered windows not supported\n" );
5354         return;
5355     }
5356     hwnd = CreateWindowExA(0, "MainWindowClass", "message window", WS_CAPTION,
5357                            100, 100, 200, 200, 0, 0, 0, NULL);
5358     assert( hwnd );
5359     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
5360     ok( !ret, "GetLayeredWindowAttributes should fail on non-layered window\n" );
5361     ret = pSetLayeredWindowAttributes( hwnd, 0, 0, LWA_ALPHA );
5362     ok( !ret, "SetLayeredWindowAttributes should fail on non-layered window\n" );
5363     SetWindowLong( hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED );
5364     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
5365     ok( !ret, "GetLayeredWindowAttributes should fail on layered but not initialized window\n" );
5366     ret = pSetLayeredWindowAttributes( hwnd, 0x123456, 44, LWA_ALPHA );
5367     ok( ret, "SetLayeredWindowAttributes should succeed on layered window\n" );
5368     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
5369     ok( ret, "GetLayeredWindowAttributes should succeed on layered window\n" );
5370     ok( key == 0x123456 || key == 0, "wrong color key %x\n", key );
5371     ok( alpha == 44, "wrong alpha %u\n", alpha );
5372     ok( flags == LWA_ALPHA, "wrong flags %x\n", flags );
5373
5374     /* clearing WS_EX_LAYERED resets attributes */
5375     SetWindowLong( hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) & ~WS_EX_LAYERED );
5376     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
5377     ok( !ret, "GetLayeredWindowAttributes should fail on no longer layered window\n" );
5378     SetWindowLong( hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED );
5379     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
5380     ok( !ret, "GetLayeredWindowAttributes should fail on layered but not initialized window\n" );
5381     ret = pSetLayeredWindowAttributes( hwnd, 0x654321, 22, LWA_COLORKEY | LWA_ALPHA );
5382     ok( ret, "SetLayeredWindowAttributes should succeed on layered window\n" );
5383     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
5384     ok( ret, "GetLayeredWindowAttributes should succeed on layered window\n" );
5385     ok( key == 0x654321, "wrong color key %x\n", key );
5386     ok( alpha == 22, "wrong alpha %u\n", alpha );
5387     ok( flags == (LWA_COLORKEY | LWA_ALPHA), "wrong flags %x\n", flags );
5388
5389     ret = pSetLayeredWindowAttributes( hwnd, 0x888888, 33, LWA_COLORKEY );
5390     ok( ret, "SetLayeredWindowAttributes should succeed on layered window\n" );
5391     alpha = 0;
5392     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
5393     ok( ret, "GetLayeredWindowAttributes should succeed on layered window\n" );
5394     ok( key == 0x888888, "wrong color key %x\n", key );
5395     /* alpha not changed on vista if LWA_ALPHA is not set */
5396     ok( alpha == 22 || alpha == 33, "wrong alpha %u\n", alpha );
5397     ok( flags == LWA_COLORKEY, "wrong flags %x\n", flags );
5398
5399     /* color key may or may not be changed without LWA_COLORKEY */
5400     ret = pSetLayeredWindowAttributes( hwnd, 0x999999, 44, 0 );
5401     ok( ret, "SetLayeredWindowAttributes should succeed on layered window\n" );
5402     alpha = 0;
5403     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
5404     ok( ret, "GetLayeredWindowAttributes should succeed on layered window\n" );
5405     ok( key == 0x888888 || key == 0x999999, "wrong color key %x\n", key );
5406     ok( alpha == 22 || alpha == 44, "wrong alpha %u\n", alpha );
5407     ok( flags == 0, "wrong flags %x\n", flags );
5408
5409     /* default alpha and color key is 0 */
5410     SetWindowLong( hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) & ~WS_EX_LAYERED );
5411     SetWindowLong( hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED );
5412     ret = pSetLayeredWindowAttributes( hwnd, 0x222222, 55, 0 );
5413     ok( ret, "SetLayeredWindowAttributes should succeed on layered window\n" );
5414     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
5415     ok( ret, "GetLayeredWindowAttributes should succeed on layered window\n" );
5416     ok( key == 0 || key == 0x222222, "wrong color key %x\n", key );
5417     ok( alpha == 0 || alpha == 55, "wrong alpha %u\n", alpha );
5418     ok( flags == 0, "wrong flags %x\n", flags );
5419
5420     DestroyWindow( hwnd );
5421 }
5422
5423 static MONITORINFO mi;
5424
5425 static LRESULT CALLBACK fullscreen_wnd_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
5426 {
5427     switch (msg)
5428     {
5429         case WM_NCCREATE:
5430         {
5431             CREATESTRUCTA *cs = (CREATESTRUCTA *)lp;
5432             trace("WM_NCCREATE: rect %d,%d-%d,%d\n", cs->x, cs->y, cs->cx, cs->cy);
5433             ok(cs->x == mi.rcMonitor.left && cs->y == mi.rcMonitor.top &&
5434                cs->cx == mi.rcMonitor.right && cs->cy == mi.rcMonitor.bottom,
5435                "expected %d,%d-%d,%d, got %d,%d-%d,%d\n",
5436                mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right, mi.rcMonitor.bottom,
5437                cs->x, cs->y, cs->cx, cs->cy);
5438             break;
5439         }
5440         case WM_GETMINMAXINFO:
5441         {
5442             MINMAXINFO *minmax = (MINMAXINFO *)lp;
5443             dump_minmax_info(minmax);
5444             ok(minmax->ptMaxPosition.x <= mi.rcMonitor.left, "%d <= %d\n", minmax->ptMaxPosition.x, mi.rcMonitor.left);
5445             ok(minmax->ptMaxPosition.y <= mi.rcMonitor.top, "%d <= %d\n", minmax->ptMaxPosition.y, mi.rcMonitor.top);
5446             ok(minmax->ptMaxSize.x >= mi.rcMonitor.right, "%d >= %d\n", minmax->ptMaxSize.x, mi.rcMonitor.right);
5447             ok(minmax->ptMaxSize.y >= mi.rcMonitor.bottom, "%d >= %d\n", minmax->ptMaxSize.y, mi.rcMonitor.bottom);
5448             break;
5449         }
5450     }
5451     return DefWindowProc(hwnd, msg, wp, lp);
5452 }
5453
5454 static void test_fullscreen(void)
5455 {
5456     static const DWORD t_style[] = {
5457         WS_OVERLAPPED, WS_POPUP, WS_CHILD, WS_THICKFRAME, WS_DLGFRAME
5458     };
5459     static const DWORD t_ex_style[] = {
5460         0, WS_EX_APPWINDOW, WS_EX_TOOLWINDOW
5461     };
5462     WNDCLASS cls;
5463     HWND hwnd;
5464     int i, j;
5465     POINT pt;
5466     RECT rc;
5467     HMONITOR hmon;
5468     LRESULT ret;
5469
5470     if (!pGetMonitorInfoA || !pMonitorFromPoint)
5471     {
5472         win_skip("GetMonitorInfoA or MonitorFromPoint are not available on this platform\n");
5473         return;
5474     }
5475
5476     pt.x = pt.y = 0;
5477     SetLastError(0xdeadbeef);
5478     hmon = pMonitorFromPoint(pt, MONITOR_DEFAULTTOPRIMARY);
5479     ok(hmon != 0, "MonitorFromPoint error %u\n", GetLastError());
5480
5481     mi.cbSize = sizeof(mi);
5482     SetLastError(0xdeadbeef);
5483     ret = pGetMonitorInfoA(hmon, &mi);
5484     ok(ret, "GetMonitorInfo error %u\n", GetLastError());
5485     trace("monitor (%d,%d-%d,%d), work (%d,%d-%d,%d)\n",
5486         mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right, mi.rcMonitor.bottom,
5487         mi.rcWork.left, mi.rcWork.top, mi.rcWork.right, mi.rcWork.bottom);
5488
5489     cls.style = 0;
5490     cls.lpfnWndProc = fullscreen_wnd_proc;
5491     cls.cbClsExtra = 0;
5492     cls.cbWndExtra = 0;
5493     cls.hInstance = GetModuleHandle(0);
5494     cls.hIcon = 0;
5495     cls.hCursor = LoadCursorA(0, IDC_ARROW);
5496     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
5497     cls.lpszMenuName = NULL;
5498     cls.lpszClassName = "fullscreen_class";
5499     RegisterClass(&cls);
5500
5501     for (i = 0; i < sizeof(t_style)/sizeof(t_style[0]); i++)
5502     {
5503         DWORD style, ex_style;
5504
5505         /* avoid a WM interaction */
5506         assert(!(t_style[i] & WS_VISIBLE));
5507
5508         for (j = 0; j < sizeof(t_ex_style)/sizeof(t_ex_style[0]); j++)
5509         {
5510             int fixup;
5511
5512             style = t_style[i];
5513             ex_style = t_ex_style[j];
5514
5515             hwnd = CreateWindowExA(ex_style, "fullscreen_class", NULL, style,
5516                                    mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right, mi.rcMonitor.bottom,
5517                                    GetDesktopWindow(), 0, GetModuleHandle(0), NULL);
5518             ok(hwnd != 0, "%d: CreateWindowExA(%#x/%#x) failed\n", i, ex_style, style);
5519             GetWindowRect(hwnd, &rc);
5520             trace("%#x/%#x: window rect %d,%d-%d,%d\n", ex_style, style, rc.left, rc.top, rc.right, rc.bottom);
5521             ok(rc.left <= mi.rcMonitor.left && rc.top <= mi.rcMonitor.top &&
5522                rc.right >= mi.rcMonitor.right && rc.bottom >= mi.rcMonitor.bottom,
5523                "%#x/%#x: window rect %d,%d-%d,%d\n", ex_style, style, rc.left, rc.top, rc.right, rc.bottom);
5524             DestroyWindow(hwnd);
5525
5526             style = t_style[i] | WS_MAXIMIZE;
5527             hwnd = CreateWindowExA(ex_style, "fullscreen_class", NULL, style,
5528                                    mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right, mi.rcMonitor.bottom,
5529                                    GetDesktopWindow(), 0, GetModuleHandle(0), NULL);
5530             ok(hwnd != 0, "%d: CreateWindowExA(%#x/%#x) failed\n", i, ex_style, style);
5531             GetWindowRect(hwnd, &rc);
5532             trace("%#x/%#x: window rect %d,%d-%d,%d\n", ex_style, style, rc.left, rc.top, rc.right, rc.bottom);
5533             ok(rc.left <= mi.rcMonitor.left && rc.top <= mi.rcMonitor.top &&
5534                rc.right >= mi.rcMonitor.right && rc.bottom >= mi.rcMonitor.bottom,
5535                "%#x/%#x: window rect %d,%d-%d,%d\n", ex_style, style, rc.left, rc.top, rc.right, rc.bottom);
5536             DestroyWindow(hwnd);
5537
5538             style = t_style[i] | WS_MAXIMIZE | WS_CAPTION;
5539             hwnd = CreateWindowExA(ex_style, "fullscreen_class", NULL, style,
5540                                    mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right, mi.rcMonitor.bottom,
5541                                    GetDesktopWindow(), 0, GetModuleHandle(0), NULL);
5542             ok(hwnd != 0, "%d: CreateWindowExA(%#x/%#x) failed\n", i, ex_style, style);
5543             GetWindowRect(hwnd, &rc);
5544             trace("%#x/%#x: window rect %d,%d-%d,%d\n", ex_style, style, rc.left, rc.top, rc.right, rc.bottom);
5545             ok(rc.left <= mi.rcMonitor.left && rc.top <= mi.rcMonitor.top &&
5546                rc.right >= mi.rcMonitor.right && rc.bottom >= mi.rcMonitor.bottom,
5547                "%#x/%#x: window rect %d,%d-%d,%d\n", ex_style, style, rc.left, rc.top, rc.right, rc.bottom);
5548             DestroyWindow(hwnd);
5549
5550             style = t_style[i] | WS_CAPTION | WS_MAXIMIZEBOX;
5551             hwnd = CreateWindowExA(ex_style, "fullscreen_class", NULL, style,
5552                                    mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right, mi.rcMonitor.bottom,
5553                                    GetDesktopWindow(), 0, GetModuleHandle(0), NULL);
5554             ok(hwnd != 0, "%d: CreateWindowExA(%#x/%#x) failed\n", i, ex_style, style);
5555             GetWindowRect(hwnd, &rc);
5556             trace("%#x/%#x: window rect %d,%d-%d,%d\n", ex_style, style, rc.left, rc.top, rc.right, rc.bottom);
5557             ok(rc.left <= mi.rcMonitor.left && rc.top <= mi.rcMonitor.top &&
5558                rc.right >= mi.rcMonitor.right && rc.bottom >= mi.rcMonitor.bottom,
5559                "%#x/%#x: window rect %d,%d-%d,%d\n", ex_style, style, rc.left, rc.top, rc.right, rc.bottom);
5560             DestroyWindow(hwnd);
5561
5562             style = t_style[i] | WS_MAXIMIZE | WS_CAPTION | WS_MAXIMIZEBOX;
5563             hwnd = CreateWindowExA(ex_style, "fullscreen_class", NULL, style,
5564                                    mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right, mi.rcMonitor.bottom,
5565                                    GetDesktopWindow(), 0, GetModuleHandle(0), NULL);
5566             ok(hwnd != 0, "%d: CreateWindowExA(%#x/%#x) failed\n", i, ex_style, style);
5567             GetWindowRect(hwnd, &rc);
5568             trace("%#x/%#x: window rect %d,%d-%d,%d\n", ex_style, style, rc.left, rc.top, rc.right, rc.bottom);
5569             /* Windows makes a maximized window slightly larger (to hide the borders?) */
5570             fixup = min(abs(rc.left), abs(rc.top));
5571             InflateRect(&rc, -fixup, -fixup);
5572             ok(rc.left >= mi.rcWork.left && rc.top <= mi.rcWork.top &&
5573                rc.right <= mi.rcWork.right && rc.bottom <= mi.rcWork.bottom,
5574                "%#x/%#x: window rect %d,%d-%d,%d must be in %d,%d-%d,%d\n",
5575                ex_style, style, rc.left, rc.top, rc.right, rc.bottom,
5576                mi.rcWork.left, mi.rcWork.top, mi.rcWork.right, mi.rcWork.bottom);
5577             DestroyWindow(hwnd);
5578
5579             style = t_style[i] | WS_MAXIMIZE | WS_MAXIMIZEBOX;
5580             hwnd = CreateWindowExA(ex_style, "fullscreen_class", NULL, style,
5581                                    mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right, mi.rcMonitor.bottom,
5582                                    GetDesktopWindow(), 0, GetModuleHandle(0), NULL);
5583             ok(hwnd != 0, "%d: CreateWindowExA(%#x/%#x) failed\n", i, ex_style, style);
5584             GetWindowRect(hwnd, &rc);
5585             trace("%#x/%#x: window rect %d,%d-%d,%d\n", ex_style, style, rc.left, rc.top, rc.right, rc.bottom);
5586             /* Windows makes a maximized window slightly larger (to hide the borders?) */
5587             fixup = min(abs(rc.left), abs(rc.top));
5588             InflateRect(&rc, -fixup, -fixup);
5589             if (style & (WS_CHILD | WS_POPUP))
5590                 ok(rc.left <= mi.rcMonitor.left && rc.top <= mi.rcMonitor.top &&
5591                    rc.right >= mi.rcMonitor.right && rc.bottom >= mi.rcMonitor.bottom,
5592                    "%#x/%#x: window rect %d,%d-%d,%d\n", ex_style, style, rc.left, rc.top, rc.right, rc.bottom);
5593             else
5594                 ok(rc.left >= mi.rcWork.left && rc.top <= mi.rcWork.top &&
5595                    rc.right <= mi.rcWork.right && rc.bottom <= mi.rcWork.bottom,
5596                    "%#x/%#x: window rect %d,%d-%d,%d\n", ex_style, style, rc.left, rc.top, rc.right, rc.bottom);
5597             DestroyWindow(hwnd);
5598         }
5599     }
5600
5601     UnregisterClass("fullscreen_class", GetModuleHandle(0));
5602 }
5603
5604 static BOOL test_thick_child_got_minmax;
5605 static const char * test_thick_child_name;
5606 static LONG test_thick_child_style;
5607 static LONG test_thick_child_exStyle;
5608
5609 static LRESULT WINAPI test_thick_child_size_winproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
5610 {
5611     MINMAXINFO* minmax;
5612     int expectedMinTrackX;
5613     int expectedMinTrackY;
5614     int actualMinTrackX;
5615     int actualMinTrackY;
5616     int expectedMaxTrackX;
5617     int expectedMaxTrackY;
5618     int actualMaxTrackX;
5619     int actualMaxTrackY;
5620     int expectedMaxSizeX;
5621     int expectedMaxSizeY;
5622     int actualMaxSizeX;
5623     int actualMaxSizeY;
5624     int expectedPosX;
5625     int expectedPosY;
5626     int actualPosX;
5627     int actualPosY;
5628     LONG adjustedStyle;
5629     RECT rect;
5630     switch (msg)
5631     {
5632         case WM_GETMINMAXINFO:
5633         {
5634             minmax = (MINMAXINFO *)lparam;
5635             trace("hwnd %p, WM_GETMINMAXINFO, %08lx, %08lx\n", hwnd, wparam, lparam);
5636             dump_minmax_info( minmax );
5637
5638             test_thick_child_got_minmax = TRUE;
5639
5640
5641             adjustedStyle = test_thick_child_style;
5642             if ((adjustedStyle & WS_CAPTION) == WS_CAPTION)
5643                 adjustedStyle &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
5644             GetClientRect(GetParent(hwnd), &rect);
5645             AdjustWindowRectEx(&rect, adjustedStyle, FALSE, test_thick_child_exStyle);
5646
5647             if (test_thick_child_style & (WS_DLGFRAME | WS_BORDER))
5648             {
5649                 expectedMinTrackX = GetSystemMetrics(SM_CXMINTRACK);
5650                 expectedMinTrackY = GetSystemMetrics(SM_CYMINTRACK);
5651             }
5652             else
5653             {
5654                 expectedMinTrackX = -2 * rect.left;
5655                 expectedMinTrackY = -2 * rect.top;
5656             }
5657             actualMinTrackX =  minmax->ptMinTrackSize.x;
5658             actualMinTrackY =  minmax->ptMinTrackSize.y;
5659
5660             ok(actualMinTrackX == expectedMinTrackX && actualMinTrackY == expectedMinTrackY,
5661                 "expected minTrack %dx%d, actual minTrack %dx%d for %s\n",
5662                 expectedMinTrackX, expectedMinTrackY, actualMinTrackX, actualMinTrackY,
5663                 test_thick_child_name);
5664
5665             actualMaxTrackX = minmax->ptMaxTrackSize.x;
5666             actualMaxTrackY = minmax->ptMaxTrackSize.y;
5667             expectedMaxTrackX = GetSystemMetrics(SM_CXMAXTRACK);
5668             expectedMaxTrackY = GetSystemMetrics(SM_CYMAXTRACK);
5669             ok(actualMaxTrackX == expectedMaxTrackX &&  actualMaxTrackY == expectedMaxTrackY,
5670                 "expected maxTrack %dx%d, actual maxTrack %dx%d for %s\n",
5671                  expectedMaxTrackX, expectedMaxTrackY, actualMaxTrackX, actualMaxTrackY,
5672                 test_thick_child_name);
5673
5674             expectedMaxSizeX = rect.right - rect.left;
5675             expectedMaxSizeY = rect.bottom - rect.top;
5676             actualMaxSizeX = minmax->ptMaxSize.x;
5677             actualMaxSizeY = minmax->ptMaxSize.y;
5678
5679             ok(actualMaxSizeX == expectedMaxSizeX &&  actualMaxSizeY == expectedMaxSizeY,
5680                 "expected maxSize %dx%d, actual maxSize %dx%d for %s\n",
5681                 expectedMaxSizeX, expectedMaxSizeY, actualMaxSizeX, actualMaxSizeY,
5682                 test_thick_child_name);
5683
5684
5685             expectedPosX = rect.left;
5686             expectedPosY = rect.top;
5687             actualPosX = minmax->ptMaxPosition.x;
5688             actualPosY = minmax->ptMaxPosition.y;
5689             ok(actualPosX == expectedPosX && actualPosY == expectedPosY,
5690                 "expected maxPosition (%d/%d), actual maxPosition (%d/%d) for %s\n",
5691                 expectedPosX, expectedPosY, actualPosX, actualPosY, test_thick_child_name);
5692
5693             break;
5694         }
5695     }
5696
5697     return DefWindowProcA(hwnd, msg, wparam, lparam);
5698 }
5699
5700 #define NUMBER_OF_THICK_CHILD_TESTS 16
5701 static void test_thick_child_size(HWND parentWindow)
5702 {
5703     BOOL success;
5704     RECT childRect;
5705     RECT adjustedParentRect;
5706     HWND childWindow;
5707     LONG childWidth;
5708     LONG childHeight;
5709     LONG expectedWidth;
5710     LONG expectedHeight;
5711     WNDCLASSA cls;
5712     LPCTSTR className = "THICK_CHILD_CLASS";
5713     int i;
5714     LONG adjustedStyle;
5715     static const LONG styles[NUMBER_OF_THICK_CHILD_TESTS] = {
5716         WS_CHILD | WS_VISIBLE | WS_THICKFRAME,
5717         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME,
5718         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME | WS_BORDER,
5719         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_BORDER,
5720         WS_CHILD | WS_VISIBLE | WS_THICKFRAME,
5721         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME,
5722         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME | WS_BORDER,
5723         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_BORDER,
5724         WS_CHILD | WS_VISIBLE | WS_THICKFRAME,
5725         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME,
5726         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME | WS_BORDER,
5727         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_BORDER,
5728         WS_CHILD | WS_VISIBLE | WS_THICKFRAME,
5729         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME,
5730         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME | WS_BORDER,
5731         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_BORDER,
5732     };
5733
5734     static const LONG exStyles[NUMBER_OF_THICK_CHILD_TESTS] = {
5735         0,
5736         0,
5737         0,
5738         0,
5739         WS_EX_DLGMODALFRAME,
5740         WS_EX_DLGMODALFRAME,
5741         WS_EX_DLGMODALFRAME,
5742         WS_EX_DLGMODALFRAME,
5743         WS_EX_STATICEDGE,
5744         WS_EX_STATICEDGE,
5745         WS_EX_STATICEDGE,
5746         WS_EX_STATICEDGE,
5747         WS_EX_STATICEDGE | WS_EX_DLGMODALFRAME,
5748         WS_EX_STATICEDGE | WS_EX_DLGMODALFRAME,
5749         WS_EX_STATICEDGE | WS_EX_DLGMODALFRAME,
5750         WS_EX_STATICEDGE | WS_EX_DLGMODALFRAME,
5751     };
5752     static const char *styleName[NUMBER_OF_THICK_CHILD_TESTS] = {
5753         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME",
5754         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME",
5755         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME | WS_BORDER",
5756         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_BORDER",
5757         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME, exstyle= WS_EX_DLGMODALFRAME",
5758         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME exstyle= WS_EX_DLGMODALFRAME",
5759         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME | WS_BORDER exstyle= WS_EX_DLGMODALFRAME",
5760         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_BORDER exstyle= WS_EX_DLGMODALFRAME",
5761         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME exstyle= WS_EX_STATICEDGE",
5762         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME exstyle= WS_EX_STATICEDGE",
5763         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME | WS_BORDER exstyle= WS_EX_STATICEDGE",
5764         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_BORDER exstyle= WS_EX_STATICEDGE",
5765         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME, exstyle= WS_EX_STATICEDGE | WS_EX_DLGMODALFRAME",
5766         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME exstyle= WS_EX_STATICEDGE | WS_EX_DLGMODALFRAME",
5767         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME | WS_BORDER exstyle= WS_EX_STATICEDGE | WS_EX_DLGMODALFRAME",
5768         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_BORDER exstyle= WS_EX_STATICEDGE | WS_EX_DLGMODALFRAME",
5769     };
5770
5771     cls.style = 0;
5772     cls.lpfnWndProc = test_thick_child_size_winproc;
5773     cls.cbClsExtra = 0;
5774     cls.cbWndExtra = 0;
5775     cls.hInstance = GetModuleHandleA(0);
5776     cls.hIcon = 0;
5777     cls.hCursor = LoadCursorA(0, IDC_ARROW);
5778     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
5779     cls.lpszMenuName = NULL;
5780     cls.lpszClassName = className;
5781     SetLastError(0xdeadbeef);
5782     ok(RegisterClassA(&cls),"RegisterClassA failed, error: %u\n", GetLastError());
5783
5784     for(i = 0; i < NUMBER_OF_THICK_CHILD_TESTS; i++)
5785     {
5786         test_thick_child_name = styleName[i];
5787         test_thick_child_style = styles[i];
5788         test_thick_child_exStyle = exStyles[i];
5789         test_thick_child_got_minmax = FALSE;
5790
5791         SetLastError(0xdeadbeef);
5792         childWindow = CreateWindowEx( exStyles[i], className, "", styles[i],  0, 0, 0, 0, parentWindow, 0,  GetModuleHandleA(0),  NULL );
5793         ok(childWindow != NULL, "Failed to create child window, error: %u\n", GetLastError());
5794
5795         ok(test_thick_child_got_minmax, "Got no WM_GETMINMAXINFO\n");
5796
5797         SetLastError(0xdeadbeef);
5798         success = GetWindowRect(childWindow, &childRect);
5799         ok(success,"GetWindowRect call failed, error: %u\n", GetLastError());
5800         childWidth = childRect.right - childRect.left;
5801         childHeight = childRect.bottom - childRect.top;
5802
5803         adjustedStyle = styles[i];
5804         if ((adjustedStyle & WS_CAPTION) == WS_CAPTION)
5805             adjustedStyle &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
5806         GetClientRect(GetParent(childWindow), &adjustedParentRect);
5807         AdjustWindowRectEx(&adjustedParentRect, adjustedStyle, FALSE, test_thick_child_exStyle);
5808
5809
5810         if (test_thick_child_style & (WS_DLGFRAME | WS_BORDER))
5811         {
5812             expectedWidth = GetSystemMetrics(SM_CXMINTRACK);
5813             expectedHeight = GetSystemMetrics(SM_CYMINTRACK);
5814         }
5815         else
5816         {
5817             expectedWidth = -2 * adjustedParentRect.left;
5818             expectedHeight = -2 * adjustedParentRect.top;
5819         }
5820
5821         ok((childWidth == expectedWidth) && (childHeight == expectedHeight),
5822             "size of window (%s) is wrong: expected size %dx%d != actual size %dx%d\n",
5823             test_thick_child_name, expectedWidth, expectedHeight, childWidth, childHeight);
5824
5825         SetLastError(0xdeadbeef);
5826         success = DestroyWindow(childWindow);
5827         ok(success,"DestroyWindow call failed, error: %u\n", GetLastError());
5828     }
5829     ok(UnregisterClass(className, GetModuleHandleA(0)),"UnregisterClass call failed\n");
5830 }
5831
5832 static void test_handles( HWND full_hwnd )
5833 {
5834     HWND hwnd = full_hwnd;
5835     BOOL ret;
5836     RECT rect;
5837
5838     SetLastError( 0xdeadbeef );
5839     ret = GetWindowRect( hwnd, &rect );
5840     ok( ret, "GetWindowRect failed for %p err %u\n", hwnd, GetLastError() );
5841
5842 #ifdef _WIN64
5843     if ((ULONG_PTR)full_hwnd >> 32)
5844         hwnd = (HWND)((ULONG_PTR)full_hwnd & ~0u);
5845     else
5846         hwnd = (HWND)((ULONG_PTR)full_hwnd | ((ULONG_PTR)~0u << 32));
5847     SetLastError( 0xdeadbeef );
5848     ret = GetWindowRect( hwnd, &rect );
5849     ok( ret, "GetWindowRect failed for %p err %u\n", hwnd, GetLastError() );
5850
5851     hwnd = (HWND)(((ULONG_PTR)full_hwnd & ~0u) | ((ULONG_PTR)0x1234 << 32));
5852     SetLastError( 0xdeadbeef );
5853     ret = GetWindowRect( hwnd, &rect );
5854     ok( ret, "GetWindowRect failed for %p err %u\n", hwnd, GetLastError() );
5855
5856     hwnd = (HWND)(((ULONG_PTR)full_hwnd & 0xffff) | ((ULONG_PTR)0x9876 << 16));
5857     SetLastError( 0xdeadbeef );
5858     ret = GetWindowRect( hwnd, &rect );
5859     ok( !ret, "GetWindowRect succeeded for %p\n", hwnd );
5860     ok( GetLastError() == ERROR_INVALID_WINDOW_HANDLE, "wrong error %u\n", GetLastError() );
5861
5862     hwnd = (HWND)(((ULONG_PTR)full_hwnd & 0xffff) | ((ULONG_PTR)0x12345678 << 16));
5863     SetLastError( 0xdeadbeef );
5864     ret = GetWindowRect( hwnd, &rect );
5865     ok( !ret, "GetWindowRect succeeded for %p\n", hwnd );
5866     ok( GetLastError() == ERROR_INVALID_WINDOW_HANDLE, "wrong error %u\n", GetLastError() );
5867 #endif
5868 }
5869
5870 static void test_winregion(void)
5871 {
5872     HWND hwnd;
5873     RECT r;
5874     int ret;
5875     HRGN hrgn;
5876
5877     if (!pGetWindowRgnBox)
5878     {
5879         win_skip("GetWindowRgnBox not supported\n");
5880         return;
5881     }
5882
5883     hwnd = CreateWindowExA(0, "static", NULL, WS_VISIBLE, 10, 10, 10, 10, NULL, 0, 0, NULL);
5884     /* NULL prect */
5885     SetLastError(0xdeadbeef);
5886     ret = pGetWindowRgnBox(hwnd, NULL);
5887     ok( ret == ERROR, "Expected ERROR, got %d\n", ret);
5888     ok( GetLastError() == 0xdeadbeef, "Expected , got %d\n", GetLastError());
5889
5890     hrgn = CreateRectRgn(2, 3, 10, 15);
5891     ok( hrgn != NULL, "Region creation failed\n");
5892     if (hrgn)
5893     {
5894         SetWindowRgn(hwnd, hrgn, FALSE);
5895
5896         SetLastError(0xdeadbeef);
5897         ret = pGetWindowRgnBox(hwnd, NULL);
5898         ok( ret == ERROR, "Expected ERROR, got %d\n", ret);
5899         ok( GetLastError() == 0xdeadbeef, "Expected , got %d\n", GetLastError());
5900
5901         r.left = r.top = r.right = r.bottom = 0;
5902         ret = pGetWindowRgnBox(hwnd, &r);
5903         ok( ret == SIMPLEREGION, "Expected SIMPLEREGION, got %d\n", ret);
5904         ok( r.left == 2 && r.top == 3 && r.right == 10 && r.bottom == 15,
5905            "Expected (2,3,10,15), got (%d,%d,%d,%d)\n", r.left, r.top,
5906                                                             r.right, r.bottom);
5907         DeleteObject(hrgn);
5908     }
5909     DestroyWindow(hwnd);
5910 }
5911
5912 START_TEST(win)
5913 {
5914     HMODULE user32 = GetModuleHandleA( "user32.dll" );
5915     pGetAncestor = (void *)GetProcAddress( user32, "GetAncestor" );
5916     pGetWindowInfo = (void *)GetProcAddress( user32, "GetWindowInfo" );
5917     pGetWindowModuleFileNameA = (void *)GetProcAddress( user32, "GetWindowModuleFileNameA" );
5918     pGetLayeredWindowAttributes = (void *)GetProcAddress( user32, "GetLayeredWindowAttributes" );
5919     pSetLayeredWindowAttributes = (void *)GetProcAddress( user32, "SetLayeredWindowAttributes" );
5920     pGetMonitorInfoA = (void *)GetProcAddress( user32,  "GetMonitorInfoA" );
5921     pMonitorFromPoint = (void *)GetProcAddress( user32,  "MonitorFromPoint" );
5922     pGetWindowRgnBox = (void *)GetProcAddress( user32, "GetWindowRgnBox" );
5923     pGetGUIThreadInfo = (void *)GetProcAddress( user32, "GetGUIThreadInfo" );
5924
5925     if (!RegisterWindowClasses()) assert(0);
5926
5927     SetLastError(0xdeafbeef);
5928     GetWindowLongPtrW(GetDesktopWindow(), GWLP_WNDPROC);
5929     is_win9x = (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED);
5930
5931     hhook = SetWindowsHookExA(WH_CBT, cbt_hook_proc, 0, GetCurrentThreadId());
5932     if (!hhook) win_skip( "Cannot set CBT hook, skipping some tests\n" );
5933
5934     hwndMain = CreateWindowExA(/*WS_EX_TOOLWINDOW*/ 0, "MainWindowClass", "Main window",
5935                                WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
5936                                WS_MAXIMIZEBOX | WS_POPUP,
5937                                100, 100, 200, 200,
5938                                0, 0, GetModuleHandle(0), NULL);
5939     hwndMain2 = CreateWindowExA(/*WS_EX_TOOLWINDOW*/ 0, "MainWindowClass", "Main window 2",
5940                                 WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
5941                                 WS_MAXIMIZEBOX | WS_POPUP,
5942                                 100, 100, 200, 200,
5943                                 0, 0, GetModuleHandle(0), NULL);
5944     assert( hwndMain );
5945     assert( hwndMain2 );
5946
5947     our_pid = GetWindowThreadProcessId(hwndMain, NULL);
5948
5949     /* Add the tests below this line */
5950     test_thick_child_size(hwndMain);
5951     test_fullscreen();
5952     test_hwnd_message();
5953     test_nonclient_area(hwndMain);
5954     test_params();
5955     test_GetWindowModuleFileName();
5956     test_capture_1();
5957     test_capture_2();
5958     test_capture_3(hwndMain, hwndMain2);
5959     test_capture_4();
5960
5961     test_CreateWindow();
5962     test_parent_owner();
5963     test_SetParent();
5964     test_enum_thread_windows();
5965
5966     test_mdi();
5967     test_icons();
5968     test_SetWindowPos(hwndMain);
5969     test_SetMenu(hwndMain);
5970     test_SetFocus(hwndMain);
5971     test_SetActiveWindow(hwndMain);
5972     test_NCRedraw();
5973
5974     test_children_zorder(hwndMain);
5975     test_popup_zorder(hwndMain2, hwndMain);
5976     test_keyboard_input(hwndMain);
5977     test_mouse_input(hwndMain);
5978     test_validatergn(hwndMain);
5979     test_nccalcscroll( hwndMain);
5980     test_scrollwindow( hwndMain);
5981     test_scrollvalidate( hwndMain);
5982     test_scrolldc( hwndMain);
5983     test_scroll();
5984     test_IsWindowUnicode();
5985     test_vis_rgn(hwndMain);
5986
5987     test_AdjustWindowRect();
5988     test_window_styles();
5989     test_redrawnow();
5990     test_csparentdc();
5991     test_SetWindowLong();
5992     test_ShowWindow();
5993     if (0) test_gettext(); /* crashes on NT4 */
5994     test_GetUpdateRect();
5995     test_Expose();
5996     test_layered_window();
5997
5998     test_SetForegroundWindow(hwndMain);
5999     test_shell_window();
6000     test_handles( hwndMain );
6001     test_winregion();
6002
6003     /* add the tests above this line */
6004     if (hhook) UnhookWindowsHookEx(hhook);
6005
6006     DestroyWindow(hwndMain2);
6007     DestroyWindow(hwndMain);
6008 }