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