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