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