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