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