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