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