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