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