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 #include "precomp.h"
24 
25 #ifndef SPI_GETDESKWALLPAPER
26 #define SPI_GETDESKWALLPAPER 0x0073
27 #endif
28 
29 #ifndef WM_SYSTIMER
30 #define WM_SYSTIMER 0x0118
31 #endif
32 
33 #define LONG_PTR INT_PTR
34 #define ULONG_PTR UINT_PTR
35 
36 void dump_region(HRGN hrgn);
37 
38 static HWND (WINAPI *pGetAncestor)(HWND,UINT);
39 static BOOL (WINAPI *pGetWindowInfo)(HWND,WINDOWINFO*);
40 static UINT (WINAPI *pGetWindowModuleFileNameA)(HWND,LPSTR,UINT);
41 static BOOL (WINAPI *pGetLayeredWindowAttributes)(HWND,COLORREF*,BYTE*,DWORD*);
42 static BOOL (WINAPI *pSetLayeredWindowAttributes)(HWND,COLORREF,BYTE,DWORD);
43 static BOOL (WINAPI *pUpdateLayeredWindow)(HWND,HDC,POINT*,SIZE*,HDC,POINT*,COLORREF,BLENDFUNCTION*,DWORD);
44 static BOOL (WINAPI *pUpdateLayeredWindowIndirect)(HWND,const UPDATELAYEREDWINDOWINFO*);
45 static BOOL (WINAPI *pGetMonitorInfoA)(HMONITOR,LPMONITORINFO);
46 static HMONITOR (WINAPI *pMonitorFromPoint)(POINT,DWORD);
47 static int  (WINAPI *pGetWindowRgnBox)(HWND,LPRECT);
48 static BOOL (WINAPI *pGetGUIThreadInfo)(DWORD, GUITHREADINFO*);
49 static BOOL (WINAPI *pGetProcessDefaultLayout)( DWORD *layout );
50 static BOOL (WINAPI *pSetProcessDefaultLayout)( DWORD layout );
51 static BOOL (WINAPI *pFlashWindow)( HWND hwnd, BOOL bInvert );
52 static BOOL (WINAPI *pFlashWindowEx)( PFLASHWINFO pfwi );
53 static DWORD (WINAPI *pSetLayout)(HDC hdc, DWORD layout);
54 static DWORD (WINAPI *pGetLayout)(HDC hdc);
55 static BOOL (WINAPI *pMirrorRgn)(HWND hwnd, HRGN hrgn);
56 
57 static BOOL test_lbuttondown_flag;
58 static DWORD num_gettext_msgs;
59 static DWORD num_settext_msgs;
60 static HWND hwndMessage;
61 static HWND hwndMain, hwndMain2;
62 static HHOOK hhook;
63 static BOOL app_activated, app_deactivated;
64 
65 static const char* szAWRClass = "Winsize";
66 static HMENU hmenu;
67 static DWORD our_pid;
68 
69 static BOOL is_win9x = FALSE;
70 
71 #define COUNTOF(arr) (sizeof(arr)/sizeof(arr[0]))
72 
dump_minmax_info(const MINMAXINFO * minmax)73 static void dump_minmax_info( const MINMAXINFO *minmax )
74 {
75     trace("Reserved=%d,%d MaxSize=%d,%d MaxPos=%d,%d MinTrack=%d,%d MaxTrack=%d,%d\n",
76           minmax->ptReserved.x, minmax->ptReserved.y,
77           minmax->ptMaxSize.x, minmax->ptMaxSize.y,
78           minmax->ptMaxPosition.x, minmax->ptMaxPosition.y,
79           minmax->ptMinTrackSize.x, minmax->ptMinTrackSize.y,
80           minmax->ptMaxTrackSize.x, minmax->ptMaxTrackSize.y);
81 }
82 
83 /* try to make sure pending X events have been processed before continuing */
flush_events(BOOL remove_messages)84 static void flush_events( BOOL remove_messages )
85 {
86     MSG msg;
87     int diff = 200;
88     int min_timeout = 100;
89     DWORD time = GetTickCount() + diff;
90 
91     while (diff > 0)
92     {
93         if (MsgWaitForMultipleObjects( 0, NULL, FALSE, min_timeout, QS_ALLINPUT ) == WAIT_TIMEOUT) break;
94         if (remove_messages)
95             while (PeekMessageA( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessageA( &msg );
96         diff = time - GetTickCount();
97         min_timeout = 50;
98     }
99 }
100 
wait_for_event(HANDLE event,int timeout)101 static BOOL wait_for_event(HANDLE event, int timeout)
102 {
103     DWORD end_time = GetTickCount() + timeout;
104     MSG msg;
105 
106     do {
107         if(MsgWaitForMultipleObjects(1, &event, FALSE, timeout, QS_ALLINPUT) == WAIT_OBJECT_0)
108             return TRUE;
109         while(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE))
110             DispatchMessageA(&msg);
111         timeout = end_time - GetTickCount();
112     }while(timeout > 0);
113 
114     return FALSE;
115 }
116 
117 /* check the values returned by the various parent/owner functions on a given window */
check_parents(HWND hwnd,HWND ga_parent,HWND gwl_parent,HWND get_parent,HWND gw_owner,HWND ga_root,HWND ga_root_owner)118 static void check_parents( HWND hwnd, HWND ga_parent, HWND gwl_parent, HWND get_parent,
119                            HWND gw_owner, HWND ga_root, HWND ga_root_owner )
120 {
121     HWND res;
122 
123     if (pGetAncestor)
124     {
125         res = pGetAncestor( hwnd, GA_PARENT );
126         ok( res == ga_parent, "Wrong result for GA_PARENT %p expected %p\n", res, ga_parent );
127     }
128     res = (HWND)GetWindowLongPtrA( hwnd, GWLP_HWNDPARENT );
129     ok( res == gwl_parent, "Wrong result for GWL_HWNDPARENT %p expected %p\n", res, gwl_parent );
130     res = GetParent( hwnd );
131     ok( res == get_parent, "Wrong result for GetParent %p expected %p\n", res, get_parent );
132     res = GetWindow( hwnd, GW_OWNER );
133     ok( res == gw_owner, "Wrong result for GW_OWNER %p expected %p\n", res, gw_owner );
134     if (pGetAncestor)
135     {
136         res = pGetAncestor( hwnd, GA_ROOT );
137         ok( res == ga_root, "Wrong result for GA_ROOT %p expected %p\n", res, ga_root );
138         res = pGetAncestor( hwnd, GA_ROOTOWNER );
139         ok( res == ga_root_owner, "Wrong result for GA_ROOTOWNER %p expected %p\n", res, ga_root_owner );
140     }
141 }
142 
143 #define check_wnd_state(a,b,c,d) check_wnd_state_(__FILE__,__LINE__,a,b,c,d)
check_wnd_state_(const char * file,int line,HWND active,HWND foreground,HWND focus,HWND capture)144 static void check_wnd_state_(const char *file, int line,
145                              HWND active, HWND foreground, HWND focus, HWND capture)
146 {
147     ok_(file, line)(active == GetActiveWindow(), "GetActiveWindow() = %p\n", GetActiveWindow());
148     /* only check foreground if it belongs to the current thread */
149     /* foreground can be moved to a different app pretty much at any time */
150     if (foreground && GetForegroundWindow() &&
151         GetWindowThreadProcessId(GetForegroundWindow(), NULL) == GetCurrentThreadId())
152         disable_success_count
153         ok_(file, line)(foreground == GetForegroundWindow(), "GetForegroundWindow() = %p\n", GetForegroundWindow());
154     ok_(file, line)(focus == GetFocus(), "GetFocus() = %p\n", GetFocus());
155     ok_(file, line)(capture == GetCapture(), "GetCapture() = %p\n", GetCapture());
156 }
157 
158 /* same as above but without capture test */
159 #define check_active_state(a,b,c) check_active_state_(__FILE__,__LINE__,a,b,c)
check_active_state_(const char * file,int line,HWND active,HWND foreground,HWND focus)160 static void check_active_state_(const char *file, int line,
161                                 HWND active, HWND foreground, HWND focus)
162 {
163     ok_(file, line)(active == GetActiveWindow(), "GetActiveWindow() = %p\n", GetActiveWindow());
164     /* only check foreground if it belongs to the current thread */
165     /* foreground can be moved to a different app pretty much at any time */
166     if (foreground && GetForegroundWindow() &&
167         GetWindowThreadProcessId(GetForegroundWindow(), NULL) == GetCurrentThreadId())
168         disable_success_count
169         ok_(file, line)(foreground == GetForegroundWindow(), "GetForegroundWindow() = %p\n", GetForegroundWindow());
170     ok_(file, line)(focus == GetFocus(), "GetFocus() = %p\n", GetFocus());
171 }
172 
ignore_message(UINT message)173 static BOOL ignore_message( UINT message )
174 {
175     /* these are always ignored */
176     return (message >= 0xc000 ||
177             message == WM_GETICON ||
178             message == WM_GETOBJECT ||
179             message == WM_TIMER ||
180             message == WM_SYSTIMER ||
181             message == WM_TIMECHANGE ||
182             message == WM_DEVICECHANGE);
183 }
184 
EnumChildProc(HWND hwndChild,LPARAM lParam)185 static BOOL CALLBACK EnumChildProc( HWND hwndChild, LPARAM lParam)
186 {
187     (*(LPINT)lParam)++;
188     trace("EnumChildProc on %p\n", hwndChild);
189     if (*(LPINT)lParam > 1) return FALSE;
190     return TRUE;
191 }
192 
193 /* will search for the given window */
EnumChildProc1(HWND hwndChild,LPARAM lParam)194 static BOOL CALLBACK EnumChildProc1( HWND hwndChild, LPARAM lParam)
195 {
196     trace("EnumChildProc1 on %p\n", hwndChild);
197     if ((HWND)lParam == hwndChild) return FALSE;
198     return TRUE;
199 }
200 
create_tool_window(LONG style,HWND parent)201 static HWND create_tool_window( LONG style, HWND parent )
202 {
203     HWND ret = CreateWindowExA(0, "ToolWindowClass", "Tool window 1", style,
204                                0, 0, 100, 100, parent, 0, 0, NULL );
205     ok( ret != 0, "Creation failed\n" );
206     return ret;
207 }
208 
209 /* test parent and owner values for various combinations */
test_parent_owner(void)210 static void test_parent_owner(void)
211 {
212     LONG style;
213     HWND test, owner, ret;
214     HWND desktop = GetDesktopWindow();
215     HWND child = create_tool_window( WS_CHILD, hwndMain );
216     INT  numChildren;
217 
218     trace( "main window %p main2 %p desktop %p child %p\n", hwndMain, hwndMain2, desktop, child );
219 
220     /* child without parent, should fail */
221     SetLastError(0xdeadbeef);
222     test = CreateWindowExA(0, "ToolWindowClass", "Tool window 1",
223                            WS_CHILD, 0, 0, 100, 100, 0, 0, 0, NULL );
224     ok( !test, "WS_CHILD without parent created\n" );
225     ok( GetLastError() == ERROR_TLW_WITH_WSCHILD ||
226         broken(GetLastError() == 0xdeadbeef), /* win9x */
227         "CreateWindowExA error %u\n", GetLastError() );
228 
229     /* desktop window */
230     check_parents( desktop, 0, 0, 0, 0, 0, 0 );
231     style = GetWindowLongA( desktop, GWL_STYLE );
232     ok( !SetWindowLongA( desktop, GWL_STYLE, WS_POPUP ), "Set GWL_STYLE on desktop succeeded\n" );
233     ok( !SetWindowLongA( desktop, GWL_STYLE, 0 ), "Set GWL_STYLE on desktop succeeded\n" );
234     ok( GetWindowLongA( desktop, GWL_STYLE ) == style, "Desktop style changed\n" );
235 
236     /* normal child window */
237     test = create_tool_window( WS_CHILD, hwndMain );
238     trace( "created child %p\n", test );
239     check_parents( test, hwndMain, hwndMain, hwndMain, 0, hwndMain, hwndMain );
240     SetWindowLongA( test, GWL_STYLE, 0 );
241     check_parents( test, hwndMain, hwndMain, 0, 0, hwndMain, test );
242     SetWindowLongA( test, GWL_STYLE, WS_POPUP );
243     check_parents( test, hwndMain, hwndMain, 0, 0, hwndMain, test );
244     SetWindowLongA( test, GWL_STYLE, WS_POPUP|WS_CHILD );
245     check_parents( test, hwndMain, hwndMain, 0, 0, hwndMain, test );
246     SetWindowLongA( test, GWL_STYLE, WS_CHILD );
247     DestroyWindow( test );
248 
249     /* normal child window with WS_MAXIMIZE */
250     test = create_tool_window( WS_CHILD | WS_MAXIMIZE, hwndMain );
251     DestroyWindow( test );
252 
253     /* normal child window with WS_THICKFRAME */
254     test = create_tool_window( WS_CHILD | WS_THICKFRAME, hwndMain );
255     DestroyWindow( test );
256 
257     /* popup window with WS_THICKFRAME */
258     test = create_tool_window( WS_POPUP | WS_THICKFRAME, hwndMain );
259     DestroyWindow( test );
260 
261     /* child of desktop */
262     test = create_tool_window( WS_CHILD, desktop );
263     trace( "created child of desktop %p\n", test );
264     check_parents( test, desktop, 0, desktop, 0, test, desktop );
265     SetWindowLongA( test, GWL_STYLE, WS_POPUP );
266     check_parents( test, desktop, 0, 0, 0, test, test );
267     SetWindowLongA( test, GWL_STYLE, 0 );
268     check_parents( test, desktop, 0, 0, 0, test, test );
269     DestroyWindow( test );
270 
271     /* child of desktop with WS_MAXIMIZE */
272     test = create_tool_window( WS_CHILD | WS_MAXIMIZE, desktop );
273     DestroyWindow( test );
274 
275     /* child of desktop with WS_MINIMIZE */
276     test = create_tool_window( WS_CHILD | WS_MINIMIZE, desktop );
277     DestroyWindow( test );
278 
279     /* child of child */
280     test = create_tool_window( WS_CHILD, child );
281     trace( "created child of child %p\n", test );
282     check_parents( test, child, child, child, 0, hwndMain, hwndMain );
283     SetWindowLongA( test, GWL_STYLE, 0 );
284     check_parents( test, child, child, 0, 0, hwndMain, test );
285     SetWindowLongA( test, GWL_STYLE, WS_POPUP );
286     check_parents( test, child, child, 0, 0, hwndMain, test );
287     DestroyWindow( test );
288 
289     /* child of child with WS_MAXIMIZE */
290     test = create_tool_window( WS_CHILD | WS_MAXIMIZE, child );
291     DestroyWindow( test );
292 
293     /* child of child with WS_MINIMIZE */
294     test = create_tool_window( WS_CHILD | WS_MINIMIZE, child );
295     DestroyWindow( test );
296 
297     /* not owned top-level window */
298     test = create_tool_window( 0, 0 );
299     trace( "created top-level %p\n", test );
300     check_parents( test, desktop, 0, 0, 0, test, test );
301     SetWindowLongA( test, GWL_STYLE, WS_POPUP );
302     check_parents( test, desktop, 0, 0, 0, test, test );
303     SetWindowLongA( test, GWL_STYLE, WS_CHILD );
304     check_parents( test, desktop, 0, desktop, 0, test, desktop );
305     DestroyWindow( test );
306 
307     /* not owned top-level window with WS_MAXIMIZE */
308     test = create_tool_window( WS_MAXIMIZE, 0 );
309     DestroyWindow( test );
310 
311     /* owned top-level window */
312     test = create_tool_window( 0, hwndMain );
313     trace( "created owned top-level %p\n", test );
314     check_parents( test, desktop, hwndMain, 0, hwndMain, test, test );
315     SetWindowLongA( test, GWL_STYLE, WS_POPUP );
316     check_parents( test, desktop, hwndMain, hwndMain, hwndMain, test, hwndMain );
317     SetWindowLongA( test, GWL_STYLE, WS_CHILD );
318     check_parents( test, desktop, hwndMain, desktop, hwndMain, test, desktop );
319     DestroyWindow( test );
320 
321     /* owned top-level window with WS_MAXIMIZE */
322     test = create_tool_window( WS_MAXIMIZE, hwndMain );
323     DestroyWindow( test );
324 
325     /* not owned popup */
326     test = create_tool_window( WS_POPUP, 0 );
327     trace( "created popup %p\n", test );
328     check_parents( test, desktop, 0, 0, 0, test, test );
329     SetWindowLongA( test, GWL_STYLE, WS_CHILD );
330     check_parents( test, desktop, 0, desktop, 0, test, desktop );
331     SetWindowLongA( test, GWL_STYLE, 0 );
332     check_parents( test, desktop, 0, 0, 0, test, test );
333     DestroyWindow( test );
334 
335     /* not owned popup with WS_MAXIMIZE */
336     test = create_tool_window( WS_POPUP | WS_MAXIMIZE, 0 );
337     DestroyWindow( test );
338 
339     /* owned popup */
340     test = create_tool_window( WS_POPUP, hwndMain );
341     trace( "created owned popup %p\n", test );
342     check_parents( test, desktop, hwndMain, hwndMain, hwndMain, test, hwndMain );
343     SetWindowLongA( test, GWL_STYLE, WS_CHILD );
344     check_parents( test, desktop, hwndMain, desktop, hwndMain, test, desktop );
345     SetWindowLongA( test, GWL_STYLE, 0 );
346     check_parents( test, desktop, hwndMain, 0, hwndMain, test, test );
347     DestroyWindow( test );
348 
349     /* owned popup with WS_MAXIMIZE */
350     test = create_tool_window( WS_POPUP | WS_MAXIMIZE, hwndMain );
351     DestroyWindow( test );
352 
353     /* top-level window owned by child (same as owned by top-level) */
354     test = create_tool_window( 0, child );
355     trace( "created top-level owned by child %p\n", test );
356     check_parents( test, desktop, hwndMain, 0, hwndMain, test, test );
357     DestroyWindow( test );
358 
359     /* top-level window owned by child (same as owned by top-level) with WS_MAXIMIZE */
360     test = create_tool_window( WS_MAXIMIZE, child );
361     DestroyWindow( test );
362 
363     /* popup owned by desktop (same as not owned) */
364     test = create_tool_window( WS_POPUP, desktop );
365     trace( "created popup owned by desktop %p\n", test );
366     check_parents( test, desktop, 0, 0, 0, test, test );
367     DestroyWindow( test );
368 
369     /* popup owned by desktop (same as not owned) with WS_MAXIMIZE */
370     test = create_tool_window( WS_POPUP | WS_MAXIMIZE, desktop );
371     DestroyWindow( test );
372 
373     /* popup owned by child (same as owned by top-level) */
374     test = create_tool_window( WS_POPUP, child );
375     trace( "created popup owned by child %p\n", test );
376     check_parents( test, desktop, hwndMain, hwndMain, hwndMain, test, hwndMain );
377     DestroyWindow( test );
378 
379     /* popup owned by child (same as owned by top-level) with WS_MAXIMIZE */
380     test = create_tool_window( WS_POPUP | WS_MAXIMIZE, child );
381     DestroyWindow( test );
382 
383     /* not owned popup with WS_CHILD (same as WS_POPUP only) */
384     test = create_tool_window( WS_POPUP | WS_CHILD, 0 );
385     trace( "created WS_CHILD popup %p\n", test );
386     check_parents( test, desktop, 0, 0, 0, test, test );
387     DestroyWindow( test );
388 
389     /* not owned popup with WS_CHILD | WS_MAXIMIZE (same as WS_POPUP only) */
390     test = create_tool_window( WS_POPUP | WS_CHILD | WS_MAXIMIZE, 0 );
391     DestroyWindow( test );
392 
393     /* owned popup with WS_CHILD (same as WS_POPUP only) */
394     test = create_tool_window( WS_POPUP | WS_CHILD, hwndMain );
395     trace( "created owned WS_CHILD popup %p\n", test );
396     check_parents( test, desktop, hwndMain, hwndMain, hwndMain, test, hwndMain );
397     DestroyWindow( test );
398 
399     /* owned popup with WS_CHILD (same as WS_POPUP only) with WS_MAXIMIZE */
400     test = create_tool_window( WS_POPUP | WS_CHILD | WS_MAXIMIZE, hwndMain );
401     DestroyWindow( test );
402 
403     /******************** parent changes *************************/
404     trace( "testing parent changes\n" );
405 
406     /* desktop window */
407     check_parents( desktop, 0, 0, 0, 0, 0, 0 );
408     if (0)
409     {
410     /* this test succeeds on NT but crashes on win9x systems */
411     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)hwndMain2 );
412     ok( !ret, "Set GWL_HWNDPARENT succeeded on desktop\n" );
413     check_parents( desktop, 0, 0, 0, 0, 0, 0 );
414     ok( !SetParent( desktop, hwndMain ), "SetParent succeeded on desktop\n" );
415     check_parents( desktop, 0, 0, 0, 0, 0, 0 );
416     }
417     /* normal child window */
418     test = create_tool_window( WS_CHILD, hwndMain );
419     trace( "created child %p\n", test );
420 
421     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)hwndMain2 );
422     ok( ret == hwndMain, "GWL_HWNDPARENT return value %p expected %p\n", ret, hwndMain );
423     check_parents( test, hwndMain2, hwndMain2, hwndMain2, 0, hwndMain2, hwndMain2 );
424 
425     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)child );
426     ok( ret == hwndMain2, "GWL_HWNDPARENT return value %p expected %p\n", ret, hwndMain2 );
427     check_parents( test, child, child, child, 0, hwndMain, hwndMain );
428 
429     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)desktop );
430     ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
431     check_parents( test, desktop, 0, desktop, 0, test, desktop );
432 
433     /* window is now child of desktop so GWLP_HWNDPARENT changes owner from now on */
434     if (!is_win9x)
435     {
436         ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)test );
437         ok( ret == 0, "GWL_HWNDPARENT return value %p expected 0\n", ret );
438         check_parents( test, desktop, 0, desktop, 0, test, desktop );
439     }
440     else
441         win_skip("Test creates circular window tree under Win9x/WinMe\n" );
442 
443     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)child );
444     ok( ret == 0, "GWL_HWNDPARENT return value %p expected 0\n", ret );
445     check_parents( test, desktop, child, desktop, child, test, desktop );
446 
447     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, 0 );
448     ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
449     check_parents( test, desktop, 0, desktop, 0, test, desktop );
450     DestroyWindow( test );
451 
452     /* not owned top-level window */
453     test = create_tool_window( 0, 0 );
454     trace( "created top-level %p\n", test );
455 
456     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)hwndMain2 );
457     ok( ret == 0, "GWL_HWNDPARENT return value %p expected 0\n", ret );
458     check_parents( test, desktop, hwndMain2, 0, hwndMain2, test, test );
459 
460     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)child );
461     ok( ret == hwndMain2, "GWL_HWNDPARENT return value %p expected %p\n", ret, hwndMain2 );
462     check_parents( test, desktop, child, 0, child, test, test );
463 
464     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, 0 );
465     ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
466     check_parents( test, desktop, 0, 0, 0, test, test );
467     DestroyWindow( test );
468 
469     /* not owned popup */
470     test = create_tool_window( WS_POPUP, 0 );
471     trace( "created popup %p\n", test );
472 
473     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)hwndMain2 );
474     ok( ret == 0, "GWL_HWNDPARENT return value %p expected 0\n", ret );
475     check_parents( test, desktop, hwndMain2, hwndMain2, hwndMain2, test, hwndMain2 );
476 
477     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)child );
478     ok( ret == hwndMain2, "GWL_HWNDPARENT return value %p expected %p\n", ret, hwndMain2 );
479     check_parents( test, desktop, child, child, child, test, hwndMain );
480 
481     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, 0 );
482     ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
483     check_parents( test, desktop, 0, 0, 0, test, test );
484     DestroyWindow( test );
485 
486     /* normal child window */
487     test = create_tool_window( WS_CHILD, hwndMain );
488     trace( "created child %p\n", test );
489 
490     ret = SetParent( test, desktop );
491     ok( ret == hwndMain, "SetParent return value %p expected %p\n", ret, hwndMain );
492     check_parents( test, desktop, 0, desktop, 0, test, desktop );
493 
494     ret = SetParent( test, child );
495     ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
496     check_parents( test, child, child, child, 0, hwndMain, hwndMain );
497 
498     ret = SetParent( test, hwndMain2 );
499     ok( ret == child, "SetParent return value %p expected %p\n", ret, child );
500     check_parents( test, hwndMain2, hwndMain2, hwndMain2, 0, hwndMain2, hwndMain2 );
501     DestroyWindow( test );
502 
503     /* not owned top-level window */
504     test = create_tool_window( 0, 0 );
505     trace( "created top-level %p\n", test );
506 
507     ret = SetParent( test, child );
508     ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
509     check_parents( test, child, child, 0, 0, hwndMain, test );
510 
511     if (!is_win9x)
512     {
513         ShowWindow( test, SW_SHOW );
514         ret = SetParent( test, test );
515         ok( ret == NULL, "SetParent return value %p expected %p\n", ret, NULL );
516         ok( GetWindowLongA( test, GWL_STYLE ) & WS_VISIBLE, "window is not visible after SetParent\n" );
517         check_parents( test, child, child, 0, 0, hwndMain, test );
518     }
519     else
520         win_skip( "Test crashes on Win9x/WinMe\n" );
521     DestroyWindow( test );
522 
523     /* owned popup */
524     test = create_tool_window( WS_POPUP, hwndMain2 );
525     trace( "created owned popup %p\n", test );
526 
527     ret = SetParent( test, child );
528     ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
529     check_parents( test, child, child, hwndMain2, hwndMain2, hwndMain, hwndMain2 );
530 
531     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (ULONG_PTR)hwndMain );
532     ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
533     check_parents( test, hwndMain, hwndMain, hwndMain2, hwndMain2, hwndMain, hwndMain2 );
534     DestroyWindow( test );
535 
536     /**************** test owner destruction *******************/
537 
538     /* owned child popup */
539     owner = create_tool_window( 0, 0 );
540     test = create_tool_window( WS_POPUP, owner );
541     trace( "created owner %p and popup %p\n", owner, test );
542     ret = SetParent( test, child );
543     ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
544     check_parents( test, child, child, owner, owner, hwndMain, owner );
545     /* window is now child of 'child' but owned by 'owner' */
546     DestroyWindow( owner );
547     ok( IsWindow(test), "Window %p destroyed by owner destruction\n", test );
548     /* Win98 doesn't pass this test. It doesn't allow a destroyed owner,
549      * while Win95, Win2k, WinXP do.
550      */
551     /*check_parents( test, child, child, owner, owner, hwndMain, owner );*/
552     ok( !IsWindow(owner), "Owner %p not destroyed\n", owner );
553     DestroyWindow(test);
554 
555     /* owned top-level popup */
556     owner = create_tool_window( 0, 0 );
557     test = create_tool_window( WS_POPUP, owner );
558     trace( "created owner %p and popup %p\n", owner, test );
559     check_parents( test, desktop, owner, owner, owner, test, owner );
560     DestroyWindow( owner );
561     ok( !IsWindow(test), "Window %p not destroyed by owner destruction\n", test );
562 
563     /* top-level popup owned by child */
564     owner = create_tool_window( WS_CHILD, hwndMain2 );
565     test = create_tool_window( WS_POPUP, 0 );
566     trace( "created owner %p and popup %p\n", owner, test );
567     ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (ULONG_PTR)owner );
568     ok( ret == 0, "GWL_HWNDPARENT return value %p expected 0\n", ret );
569     check_parents( test, desktop, owner, owner, owner, test, hwndMain2 );
570     DestroyWindow( owner );
571     ok( IsWindow(test), "Window %p destroyed by owner destruction\n", test );
572     ok( !IsWindow(owner), "Owner %p not destroyed\n", owner );
573     /* Win98 doesn't pass this test. It doesn't allow a destroyed owner,
574      * while Win95, Win2k, WinXP do.
575      */
576     /*check_parents( test, desktop, owner, owner, owner, test, owner );*/
577     DestroyWindow(test);
578 
579     /* final cleanup */
580     DestroyWindow(child);
581 
582 
583     owner = create_tool_window( WS_OVERLAPPED, 0 );
584     test = create_tool_window( WS_POPUP, desktop );
585 
586     ok( !GetWindow( test, GW_OWNER ), "Wrong owner window\n" );
587     numChildren = 0;
588     ok( !EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
589         "EnumChildWindows should have returned FALSE\n" );
590     ok( numChildren == 0, "numChildren should be 0 got %d\n", numChildren );
591 
592     SetWindowLongA( test, GWL_STYLE, (GetWindowLongA( test, GWL_STYLE ) & ~WS_POPUP) | WS_CHILD );
593     ret = SetParent( test, owner );
594     ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
595 
596     numChildren = 0;
597     ok( EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
598         "EnumChildWindows should have returned TRUE\n" );
599     ok( numChildren == 1, "numChildren should be 1 got %d\n", numChildren );
600 
601     child = create_tool_window( WS_CHILD, owner );
602     numChildren = 0;
603     ok( !EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
604         "EnumChildWindows should have returned FALSE\n" );
605     ok( numChildren == 2, "numChildren should be 2 got %d\n", numChildren );
606     DestroyWindow( child );
607 
608     child = create_tool_window( WS_VISIBLE | WS_OVERLAPPEDWINDOW, owner );
609     ok( GetWindow( child, GW_OWNER ) == owner, "Wrong owner window\n" );
610     numChildren = 0;
611     ok( EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
612         "EnumChildWindows should have returned TRUE\n" );
613     ok( numChildren == 1, "numChildren should be 1 got %d\n", numChildren );
614 
615     ret = SetParent( child, owner );
616     ok( GetWindow( child, GW_OWNER ) == owner, "Wrong owner window\n" );
617     ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
618     numChildren = 0;
619     ok( !EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
620         "EnumChildWindows should have returned FALSE\n" );
621     ok( numChildren == 2, "numChildren should be 2 got %d\n", numChildren );
622 
623     ret = SetParent( child, NULL );
624     ok( GetWindow( child, GW_OWNER ) == owner, "Wrong owner window\n" );
625     ok( ret == owner, "SetParent return value %p expected %p\n", ret, owner );
626     numChildren = 0;
627     ok( EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
628         "EnumChildWindows should have returned TRUE\n" );
629     ok( numChildren == 1, "numChildren should be 1 got %d\n", numChildren );
630 
631     /* even GW_OWNER == owner it's still a desktop's child */
632     ok( !EnumChildWindows( desktop, EnumChildProc1, (LPARAM)child ),
633         "EnumChildWindows should have found %p and returned FALSE\n", child );
634 
635     DestroyWindow( child );
636     child = create_tool_window( WS_VISIBLE | WS_OVERLAPPEDWINDOW, NULL );
637 
638     ok( !EnumChildWindows( desktop, EnumChildProc1, (LPARAM)child ),
639         "EnumChildWindows should have found %p and returned FALSE\n", child );
640 
641     DestroyWindow( child );
642     DestroyWindow( test );
643     DestroyWindow( owner );
644 
645     /* Test that owner window takes into account WS_CHILD flag even if parent is set by SetParent. */
646     owner = create_tool_window( WS_VISIBLE | WS_OVERLAPPEDWINDOW, desktop );
647     SetParent(owner, hwndMain);
648     check_parents( owner, hwndMain, hwndMain, NULL, NULL, hwndMain, owner );
649     test = create_tool_window( WS_VISIBLE | WS_OVERLAPPEDWINDOW, owner );
650     check_parents( test, desktop, owner, NULL, owner, test, test );
651     DestroyWindow( owner );
652     DestroyWindow( test );
653 
654     owner = create_tool_window( WS_VISIBLE | WS_CHILD, desktop );
655     SetParent(owner, hwndMain);
656     check_parents( owner, hwndMain, hwndMain, hwndMain, NULL, hwndMain, hwndMain );
657     test = create_tool_window( WS_VISIBLE | WS_OVERLAPPEDWINDOW, owner );
658     check_parents( test, desktop, hwndMain, NULL, hwndMain, test, test );
659     DestroyWindow( owner );
660     DestroyWindow( test );
661 
662     owner = create_tool_window( WS_VISIBLE | WS_POPUP | WS_CHILD, desktop );
663     SetParent(owner, hwndMain);
664     check_parents( owner, hwndMain, hwndMain, NULL, NULL, hwndMain, owner );
665     test = create_tool_window( WS_VISIBLE | WS_OVERLAPPEDWINDOW, owner );
666     check_parents( test, desktop, owner, NULL, owner, test, test );
667     DestroyWindow( owner );
668     DestroyWindow( test );
669 }
670 
enum_proc(HWND hwnd,LPARAM lParam)671 static BOOL CALLBACK enum_proc( HWND hwnd, LPARAM lParam)
672 {
673     (*(LPINT)lParam)++;
674     if (*(LPINT)lParam > 2) return FALSE;
675     return TRUE;
676 }
enum_thread(void * arg)677 static DWORD CALLBACK enum_thread( void *arg )
678 {
679     INT count;
680     HWND hwnd[3];
681     BOOL ret;
682     MSG msg;
683 
684     if (pGetGUIThreadInfo)
685     {
686         GUITHREADINFO info;
687         info.cbSize = sizeof(info);
688         ret = pGetGUIThreadInfo( GetCurrentThreadId(), &info );
689         ok( ret || broken(!ret), /* win9x */
690             "GetGUIThreadInfo failed without message queue\n" );
691         SetLastError( 0xdeadbeef );
692         info.cbSize = sizeof(info) + 1;
693         ret = pGetGUIThreadInfo( GetCurrentThreadId(), &info );
694         ok( !ret, "GetGUIThreadInfo succeeded with wrong size\n" );
695         ok( GetLastError() == ERROR_INVALID_PARAMETER ||
696             broken(GetLastError() == 0xdeadbeef), /* win9x */
697             "wrong error %u\n", GetLastError() );
698     }
699 
700     PeekMessageA( &msg, 0, 0, 0, PM_NOREMOVE );  /* make sure we have a message queue */
701 
702     count = 0;
703     ret = EnumThreadWindows( GetCurrentThreadId(), enum_proc, (LPARAM)&count );
704     ok( ret, "EnumThreadWindows should have returned TRUE\n" );
705     ok( count == 0, "count should be 0 got %d\n", count );
706 
707     hwnd[0] = CreateWindowExA(0, "ToolWindowClass", "Tool window 1", WS_POPUP,
708                               0, 0, 100, 100, 0, 0, 0, NULL );
709     count = 0;
710     ret = EnumThreadWindows( GetCurrentThreadId(), enum_proc, (LPARAM)&count );
711     ok( ret, "EnumThreadWindows should have returned TRUE\n" );
712     if (count != 2)  /* Vista gives us two windows for the price of one */
713     {
714         ok( count == 1, "count should be 1 got %d\n", count );
715         hwnd[2] = CreateWindowExA(0, "ToolWindowClass", "Tool window 2", WS_POPUP,
716                                   0, 0, 100, 100, 0, 0, 0, NULL );
717     }
718     else hwnd[2] = 0;
719 
720     hwnd[1] = CreateWindowExA(0, "ToolWindowClass", "Tool window 3", WS_POPUP,
721                               0, 0, 100, 100, 0, 0, 0, NULL );
722     count = 0;
723     ret = EnumThreadWindows( GetCurrentThreadId(), enum_proc, (LPARAM)&count );
724     ok( !ret, "EnumThreadWindows should have returned FALSE\n" );
725     ok( count == 3, "count should be 3 got %d\n", count );
726 
727     if (hwnd[2]) DestroyWindow(hwnd[2]);
728     DestroyWindow(hwnd[1]);
729     DestroyWindow(hwnd[0]);
730     return 0;
731 }
732 
733 /* test EnumThreadWindows in a separate thread */
test_enum_thread_windows(void)734 static void test_enum_thread_windows(void)
735 {
736     DWORD id;
737     HANDLE handle = CreateThread( NULL, 0, enum_thread, 0, 0, &id );
738     ok( !WaitForSingleObject( handle, 10000 ), "wait failed\n" );
739     CloseHandle( handle );
740 }
741 
742 static struct wm_gettext_override_data
743 {
744     BOOL   enabled; /* when 1 bypasses default procedure */
745     char  *buff;    /* expected text buffer pointer */
746     WCHAR *buffW;   /* same, for W test */
747 } g_wm_gettext_override;
748 
main_window_procA(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)749 static LRESULT WINAPI main_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
750 {
751     switch (msg)
752     {
753 	case WM_GETMINMAXINFO:
754 	{
755 	    SetWindowLongPtrA(hwnd, GWLP_USERDATA, 0x20031021);
756 	    break;
757 	}
758 	case WM_WINDOWPOSCHANGING:
759 	{
760 	    WINDOWPOS *winpos = (WINDOWPOS *)lparam;
761 	    if (!(winpos->flags & SWP_NOMOVE))
762 	    {
763 		ok(winpos->x >= -32768 && winpos->x <= 32767, "bad winpos->x %d\n", winpos->x);
764 		ok(winpos->y >= -32768 && winpos->y <= 32767, "bad winpos->y %d\n", winpos->y);
765 	    }
766 	    /* Win9x does not fixup cx/xy for WM_WINDOWPOSCHANGING */
767 	    if (!(winpos->flags & SWP_NOSIZE) && !is_win9x)
768 	    {
769 		ok((winpos->cx >= 0 && winpos->cx <= 32767) ||
770                    winpos->cx == 32768, /* win7 doesn't truncate */
771                    "bad winpos->cx %d\n", winpos->cx);
772 		ok((winpos->cy >= 0 && winpos->cy <= 32767) ||
773                    winpos->cy == 40000, /* win7 doesn't truncate */
774                    "bad winpos->cy %d\n", winpos->cy);
775 	    }
776 	    break;
777 	}
778 	case WM_WINDOWPOSCHANGED:
779     disable_success_count
780 	{
781             RECT rc1, rc2;
782 	    WINDOWPOS *winpos = (WINDOWPOS *)lparam;
783 	    ok(winpos->x >= -32768 && winpos->x <= 32767, "bad winpos->x %d\n", winpos->x);
784 	    ok(winpos->y >= -32768 && winpos->y <= 32767, "bad winpos->y %d\n", winpos->y);
785 
786             ok((winpos->cx >= 0 && winpos->cx <= 32767) ||
787                winpos->cx == 32768, /* win7 doesn't truncate */
788                "bad winpos->cx %d\n", winpos->cx);
789             ok((winpos->cy >= 0 && winpos->cy <= 32767) ||
790                winpos->cy == 40000, /* win7 doesn't truncate */
791                "bad winpos->cy %d\n", winpos->cy);
792 
793             GetWindowRect(hwnd, &rc1);
794             SetRect(&rc2, winpos->x, winpos->y, winpos->x + winpos->cx, winpos->y + winpos->cy);
795             /* note: winpos coordinates are relative to parent */
796             MapWindowPoints(GetParent(hwnd), 0, (LPPOINT)&rc2, 2);
797             if (0)
798             {
799             /* Uncomment this once the test succeeds in all cases */
800             ok(EqualRect(&rc1, &rc2), "rects do not match %s / %s\n", wine_dbgstr_rect(&rc1),
801                wine_dbgstr_rect(&rc2));
802 
803             GetClientRect(hwnd, &rc2);
804             DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc1);
805             MapWindowPoints(0, hwnd, (LPPOINT)&rc1, 2);
806             ok(EqualRect(&rc1, &rc2), "rects do not match %s / %s\n", wine_dbgstr_rect(&rc1),
807                wine_dbgstr_rect(&rc2));
808             }
809 	    break;
810 	}
811 	case WM_NCCREATE:
812 	{
813 	    BOOL got_getminmaxinfo = GetWindowLongPtrA(hwnd, GWLP_USERDATA) == 0x20031021;
814 	    CREATESTRUCTA *cs = (CREATESTRUCTA *)lparam;
815 
816 	    if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
817 		ok(got_getminmaxinfo, "main: WM_GETMINMAXINFO should have been received before WM_NCCREATE\n");
818 	    else
819 		ok(!got_getminmaxinfo, "main: WM_GETMINMAXINFO should NOT have been received before WM_NCCREATE\n");
820 	    break;
821 	}
822         case WM_COMMAND:
823             if (test_lbuttondown_flag)
824             {
825                 ShowWindow((HWND)wparam, SW_SHOW);
826                 flush_events( FALSE );
827             }
828             break;
829         case WM_GETTEXT:
830             num_gettext_msgs++;
831             if (g_wm_gettext_override.enabled)
832             {
833                 char *text = (char*)lparam;
834                 ok(g_wm_gettext_override.buff == text, "expected buffer %p, got %p\n", g_wm_gettext_override.buff, text);
835                 ok(*text == 0, "expected empty string buffer %x\n", *text);
836                 return 0;
837             }
838             break;
839         case WM_SETTEXT:
840             num_settext_msgs++;
841             break;
842         case WM_ACTIVATEAPP:
843             if (wparam) app_activated = TRUE;
844             else app_deactivated = TRUE;
845             break;
846     }
847 
848     return DefWindowProcA(hwnd, msg, wparam, lparam);
849 }
850 
main_window_procW(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)851 static LRESULT WINAPI main_window_procW(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
852 {
853     switch (msg)
854     {
855         case WM_GETTEXT:
856             num_gettext_msgs++;
857             if (g_wm_gettext_override.enabled)
858             {
859                 WCHAR *text = (WCHAR*)lparam;
860                 ok(g_wm_gettext_override.buffW == text, "expected buffer %p, got %p\n", g_wm_gettext_override.buffW, text);
861                 ok(*text == 0, "expected empty string buffer %x\n", *text);
862                 return 0;
863             }
864             break;
865     }
866 
867     return DefWindowProcA(hwnd, msg, wparam, lparam);
868 }
869 
tool_window_procA(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)870 static LRESULT WINAPI tool_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
871 {
872     switch (msg)
873     {
874 	case WM_GETMINMAXINFO:
875 	{
876 	    SetWindowLongPtrA(hwnd, GWLP_USERDATA, 0x20031021);
877 	    break;
878 	}
879 	case WM_NCCREATE:
880 	{
881 	    BOOL got_getminmaxinfo = GetWindowLongPtrA(hwnd, GWLP_USERDATA) == 0x20031021;
882 	    CREATESTRUCTA *cs = (CREATESTRUCTA *)lparam;
883 
884 	    if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
885 		ok(got_getminmaxinfo, "tool: WM_GETMINMAXINFO should have been received before WM_NCCREATE\n");
886 	    else
887 		ok(!got_getminmaxinfo, "tool: WM_GETMINMAXINFO should NOT have been received before WM_NCCREATE\n");
888 	    break;
889 	}
890     }
891 
892     return DefWindowProcA(hwnd, msg, wparam, lparam);
893 }
894 
895 static const WCHAR mainclassW[] = {'M','a','i','n','W','i','n','d','o','w','C','l','a','s','s','W',0};
896 
RegisterWindowClasses(void)897 static BOOL RegisterWindowClasses(void)
898 {
899     WNDCLASSW clsW;
900     WNDCLASSA cls;
901 
902     cls.style = CS_DBLCLKS;
903     cls.lpfnWndProc = main_window_procA;
904     cls.cbClsExtra = 0;
905     cls.cbWndExtra = 0;
906     cls.hInstance = GetModuleHandleA(0);
907     cls.hIcon = 0;
908     cls.hCursor = LoadCursorA(0, (LPCSTR)IDC_ARROW);
909     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
910     cls.lpszMenuName = NULL;
911     cls.lpszClassName = "MainWindowClass";
912 
913     if(!RegisterClassA(&cls)) return FALSE;
914 
915     clsW.style = CS_DBLCLKS;
916     clsW.lpfnWndProc = main_window_procW;
917     clsW.cbClsExtra = 0;
918     clsW.cbWndExtra = 0;
919     clsW.hInstance = GetModuleHandleA(0);
920     clsW.hIcon = 0;
921     clsW.hCursor = LoadCursorA(0, (LPCSTR)IDC_ARROW);
922     clsW.hbrBackground = GetStockObject(WHITE_BRUSH);
923     clsW.lpszMenuName = NULL;
924     clsW.lpszClassName = mainclassW;
925 
926     if(!RegisterClassW(&clsW)) return FALSE;
927 
928     cls.style = 0;
929     cls.lpfnWndProc = tool_window_procA;
930     cls.cbClsExtra = 0;
931     cls.cbWndExtra = 0;
932     cls.hInstance = GetModuleHandleA(0);
933     cls.hIcon = 0;
934     cls.hCursor = LoadCursorA(0, (LPCSTR)IDC_ARROW);
935     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
936     cls.lpszMenuName = NULL;
937     cls.lpszClassName = "ToolWindowClass";
938 
939     if(!RegisterClassA(&cls)) return FALSE;
940 
941     return TRUE;
942 }
943 
verify_window_info(const char * hook,HWND hwnd,const WINDOWINFO * info)944 static void verify_window_info(const char *hook, HWND hwnd, const WINDOWINFO *info)
945 {
946     RECT rcWindow, rcClient;
947     DWORD status;
948 
949     ok(IsWindow(hwnd), "bad window handle %p in hook %s\n", hwnd, hook);
950 
951     GetWindowRect(hwnd, &rcWindow);
952     ok(EqualRect(&rcWindow, &info->rcWindow), "wrong rcWindow for %p in hook %s\n", hwnd, hook);
953 
954     GetClientRect(hwnd, &rcClient);
955     /* translate to screen coordinates */
956     MapWindowPoints(hwnd, 0, (LPPOINT)&rcClient, 2);
957     ok(EqualRect(&rcClient, &info->rcClient), "wrong rcClient for %p in hook %s\n", hwnd, hook);
958 
959     ok(info->dwStyle == (DWORD)GetWindowLongA(hwnd, GWL_STYLE),
960        "wrong dwStyle: %08x != %08x for %p in hook %s\n",
961        info->dwStyle, GetWindowLongA(hwnd, GWL_STYLE), hwnd, hook);
962     /* Windows reports some undocumented exstyles in WINDOWINFO, but
963      * doesn't return them in GetWindowLong(hwnd, GWL_EXSTYLE).
964      */
965     ok((info->dwExStyle & ~0xe0000800) == (DWORD)GetWindowLongA(hwnd, GWL_EXSTYLE),
966        "wrong dwExStyle: %08x != %08x for %p in hook %s\n",
967        info->dwExStyle, GetWindowLongA(hwnd, GWL_EXSTYLE), hwnd, hook);
968     status = (GetActiveWindow() == hwnd) ? WS_ACTIVECAPTION : 0;
969     if (GetForegroundWindow())
970         ok(info->dwWindowStatus == status, "wrong dwWindowStatus: %04x != %04x active %p fg %p in hook %s\n",
971            info->dwWindowStatus, status, GetActiveWindow(), GetForegroundWindow(), hook);
972     else
973         ok(1, "Just counting");
974 
975     /* win2k and XP return broken border info in GetWindowInfo most of
976      * the time, so there is no point in testing it.
977      */
978 if (0)
979 {
980     UINT border;
981     ok(info->cxWindowBorders == (unsigned)(rcClient.left - rcWindow.left),
982        "wrong cxWindowBorders %d != %d\n", info->cxWindowBorders, rcClient.left - rcWindow.left);
983     border = min(rcWindow.bottom - rcClient.bottom, rcClient.top - rcWindow.top);
984     ok(info->cyWindowBorders == border,
985        "wrong cyWindowBorders %d != %d\n", info->cyWindowBorders, border);
986 }
987     ok(info->atomWindowType == GetClassLongA(hwnd, GCW_ATOM), "wrong atomWindowType for %p in hook %s\n",
988        hwnd, hook);
989     ok(info->wCreatorVersion == 0x0400 /* NT4, Win2000, XP, Win2003 */ ||
990        info->wCreatorVersion == 0x0500 /* Vista */,
991        "wrong wCreatorVersion %04x for %p in hook %s\n", info->wCreatorVersion, hwnd, hook);
992 }
993 
FixedAdjustWindowRectEx(RECT * rc,LONG style,BOOL menu,LONG exstyle)994 static void FixedAdjustWindowRectEx(RECT* rc, LONG style, BOOL menu, LONG exstyle)
995 {
996     AdjustWindowRectEx(rc, style, menu, exstyle);
997     /* AdjustWindowRectEx does not include scroll bars */
998     if (style & WS_VSCROLL)
999     {
1000         if(exstyle & WS_EX_LEFTSCROLLBAR)
1001             rc->left  -= GetSystemMetrics(SM_CXVSCROLL);
1002         else
1003             rc->right += GetSystemMetrics(SM_CXVSCROLL);
1004     }
1005     if (style & WS_HSCROLL)
1006 	rc->bottom += GetSystemMetrics(SM_CYHSCROLL);
1007 }
1008 
1009 /* reimplement it to check that the Wine algorithm gives the correct result */
wine_AdjustWindowRectEx(RECT * rect,LONG style,BOOL menu,LONG exStyle)1010 static void wine_AdjustWindowRectEx( RECT *rect, LONG style, BOOL menu, LONG exStyle )
1011 {
1012     int adjust;
1013 
1014     if ((exStyle & (WS_EX_STATICEDGE|WS_EX_DLGMODALFRAME)) ==
1015         WS_EX_STATICEDGE)
1016     {
1017         adjust = 1; /* for the outer frame always present */
1018     }
1019     else
1020     {
1021         adjust = 0;
1022         if ((exStyle & WS_EX_DLGMODALFRAME) ||
1023             (style & (WS_THICKFRAME|WS_DLGFRAME))) adjust = 2; /* outer */
1024     }
1025     if (style & WS_THICKFRAME)
1026         adjust += GetSystemMetrics(SM_CXFRAME) - GetSystemMetrics(SM_CXDLGFRAME); /* The resize border */
1027     if ((style & (WS_BORDER|WS_DLGFRAME)) ||
1028         (exStyle & WS_EX_DLGMODALFRAME))
1029         adjust++; /* The other border */
1030 
1031     InflateRect (rect, adjust, adjust);
1032 
1033     if ((style & WS_CAPTION) == WS_CAPTION)
1034     {
1035         if (exStyle & WS_EX_TOOLWINDOW)
1036             rect->top -= GetSystemMetrics(SM_CYSMCAPTION);
1037         else
1038             rect->top -= GetSystemMetrics(SM_CYCAPTION);
1039     }
1040     if (menu) rect->top -= GetSystemMetrics(SM_CYMENU);
1041 
1042     if (exStyle & WS_EX_CLIENTEDGE)
1043         InflateRect(rect, GetSystemMetrics(SM_CXEDGE), GetSystemMetrics(SM_CYEDGE));
1044 
1045     if (style & WS_VSCROLL)
1046     {
1047         if((exStyle & WS_EX_LEFTSCROLLBAR) != 0)
1048             rect->left  -= GetSystemMetrics(SM_CXVSCROLL);
1049         else
1050             rect->right += GetSystemMetrics(SM_CXVSCROLL);
1051     }
1052     if (style & WS_HSCROLL) rect->bottom += GetSystemMetrics(SM_CYHSCROLL);
1053 }
1054 
test_nonclient_area(HWND hwnd)1055 static void test_nonclient_area(HWND hwnd)
1056 {
1057     DWORD style, exstyle;
1058     RECT rc_window, rc_client, rc;
1059     BOOL menu;
1060     LRESULT ret;
1061 
1062     style = GetWindowLongA(hwnd, GWL_STYLE);
1063     exstyle = GetWindowLongA(hwnd, GWL_EXSTYLE);
1064     menu = !(style & WS_CHILD) && GetMenu(hwnd) != 0;
1065 
1066     GetWindowRect(hwnd, &rc_window);
1067     GetClientRect(hwnd, &rc_client);
1068 
1069     /* avoid some cases when things go wrong */
1070     if (IsRectEmpty(&rc_window) || IsRectEmpty(&rc_client) ||
1071 	rc_window.right > 32768 || rc_window.bottom > 32768) return;
1072 
1073     rc = rc_client;
1074     MapWindowPoints(hwnd, 0, (LPPOINT)&rc, 2);
1075     FixedAdjustWindowRectEx(&rc, style, menu, exstyle);
1076 
1077     ok(EqualRect(&rc, &rc_window),
1078        "window rect does not match: style:exstyle=0x%08x:0x%08x, menu=%d, win=%s, calc=%s\n",
1079        style, exstyle, menu, wine_dbgstr_rect(&rc_window), wine_dbgstr_rect(&rc));
1080 
1081     rc = rc_client;
1082     MapWindowPoints(hwnd, 0, (LPPOINT)&rc, 2);
1083     wine_AdjustWindowRectEx(&rc, style, menu, exstyle);
1084     ok(EqualRect(&rc, &rc_window),
1085        "window rect does not match: style:exstyle=0x%08x:0x%08x, menu=%d, win=%s, calc=%s\n",
1086        style, exstyle, menu, wine_dbgstr_rect(&rc_window), wine_dbgstr_rect(&rc));
1087 
1088 
1089     rc = rc_window;
1090     DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc);
1091     MapWindowPoints(0, hwnd, (LPPOINT)&rc, 2);
1092     ok(EqualRect(&rc, &rc_client),
1093        "client rect does not match: style:exstyle=0x%08x:0x%08x, menu=%d client=%s, calc=%s\n",
1094        style, exstyle, menu, wine_dbgstr_rect(&rc_client), wine_dbgstr_rect(&rc));
1095 
1096     /* NULL rectangle shouldn't crash */
1097     ret = DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, 0);
1098     ok(ret == 0, "NULL rectangle returned %ld instead of 0\n", ret);
1099 
1100     /* Win9x doesn't like WM_NCCALCSIZE with synthetic data and crashes */;
1101     if (is_win9x)
1102 	return;
1103 
1104     /* and now test AdjustWindowRectEx and WM_NCCALCSIZE on synthetic data */
1105     SetRect(&rc_client, 0, 0, 250, 150);
1106     rc_window = rc_client;
1107     MapWindowPoints(hwnd, 0, (LPPOINT)&rc_window, 2);
1108     FixedAdjustWindowRectEx(&rc_window, style, menu, exstyle);
1109 
1110     rc = rc_window;
1111     DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc);
1112     MapWindowPoints(0, hwnd, (LPPOINT)&rc, 2);
1113     ok(EqualRect(&rc, &rc_client),
1114        "synthetic rect does not match: style:exstyle=0x%08x:0x%08x, menu=%d, client=%s, calc=%s\n",
1115        style, exstyle, menu, wine_dbgstr_rect(&rc_client), wine_dbgstr_rect(&rc));
1116 }
1117 
cbt_hook_proc(int nCode,WPARAM wParam,LPARAM lParam)1118 static LRESULT CALLBACK cbt_hook_proc(int nCode, WPARAM wParam, LPARAM lParam)
1119 {
1120     static const char *CBT_code_name[10] = {
1121 	"HCBT_MOVESIZE",
1122 	"HCBT_MINMAX",
1123 	"HCBT_QS",
1124 	"HCBT_CREATEWND",
1125 	"HCBT_DESTROYWND",
1126 	"HCBT_ACTIVATE",
1127 	"HCBT_CLICKSKIPPED",
1128 	"HCBT_KEYSKIPPED",
1129 	"HCBT_SYSCOMMAND",
1130 	"HCBT_SETFOCUS" };
1131     const char *code_name = (nCode >= 0 && nCode <= HCBT_SETFOCUS) ? CBT_code_name[nCode] : "Unknown";
1132     HWND hwnd = (HWND)wParam;
1133 
1134     switch (nCode)
1135     {
1136     case HCBT_CREATEWND:
1137 	{
1138 	    static const RECT rc_null;
1139 	    RECT rc;
1140 	    LONG style;
1141 	    CBT_CREATEWNDA *createwnd = (CBT_CREATEWNDA *)lParam;
1142 	    ok(createwnd->hwndInsertAfter == HWND_TOP, "hwndInsertAfter should be always HWND_TOP\n");
1143 
1144             if (pGetWindowInfo)
1145             {
1146                 WINDOWINFO info;
1147                 info.cbSize = sizeof(WINDOWINFO);
1148                 ok(pGetWindowInfo(hwnd, &info), "GetWindowInfo should not fail\n");
1149                 verify_window_info(code_name, hwnd, &info);
1150             }
1151 
1152 	    /* WS_VISIBLE should be turned off yet */
1153 	    style = createwnd->lpcs->style & ~WS_VISIBLE;
1154 	    ok(style == GetWindowLongA(hwnd, GWL_STYLE),
1155 		"style of hwnd and style in the CREATESTRUCT do not match: %08x != %08x\n",
1156 		GetWindowLongA(hwnd, GWL_STYLE), style);
1157 
1158             if (0)
1159             {
1160             /* Uncomment this once the test succeeds in all cases */
1161 	    if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD)
1162 	    {
1163 		ok(GetParent(hwnd) == hwndMessage,
1164 		   "wrong result from GetParent %p: message window %p\n",
1165 		   GetParent(hwnd), hwndMessage);
1166 	    }
1167 	    else
1168 		ok(!GetParent(hwnd), "GetParent should return 0 at this point\n");
1169 
1170 	    ok(!GetWindow(hwnd, GW_OWNER), "GW_OWNER should be set to 0 at this point\n");
1171             }
1172             if (0)
1173             {
1174 	    /* while NT assigns GW_HWNDFIRST/LAST some values at this point,
1175 	     * Win9x still has them set to 0.
1176 	     */
1177 	    ok(GetWindow(hwnd, GW_HWNDFIRST) != 0, "GW_HWNDFIRST should not be set to 0 at this point\n");
1178 	    ok(GetWindow(hwnd, GW_HWNDLAST) != 0, "GW_HWNDLAST should not be set to 0 at this point\n");
1179             }
1180 	    ok(!GetWindow(hwnd, GW_HWNDPREV), "GW_HWNDPREV should be set to 0 at this point\n");
1181 	    ok(!GetWindow(hwnd, GW_HWNDNEXT), "GW_HWNDNEXT should be set to 0 at this point\n");
1182 
1183             if (0)
1184             {
1185             /* Uncomment this once the test succeeds in all cases */
1186 	    if (pGetAncestor)
1187 	    {
1188 		ok(pGetAncestor(hwnd, GA_PARENT) == hwndMessage, "GA_PARENT should be set to hwndMessage at this point\n");
1189 		ok(pGetAncestor(hwnd, GA_ROOT) == hwnd,
1190 		   "GA_ROOT is set to %p, expected %p\n", pGetAncestor(hwnd, GA_ROOT), hwnd);
1191 
1192 		if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD)
1193 		    ok(pGetAncestor(hwnd, GA_ROOTOWNER) == hwndMessage,
1194 		       "GA_ROOTOWNER should be set to hwndMessage at this point\n");
1195 		else
1196 		    ok(pGetAncestor(hwnd, GA_ROOTOWNER) == hwnd,
1197 		       "GA_ROOTOWNER is set to %p, expected %p\n", pGetAncestor(hwnd, GA_ROOTOWNER), hwnd);
1198             }
1199 
1200 	    ok(GetWindowRect(hwnd, &rc), "GetWindowRect failed\n");
1201 	    ok(EqualRect(&rc, &rc_null), "window rect should be set to 0 HCBT_CREATEWND\n");
1202 	    ok(GetClientRect(hwnd, &rc), "GetClientRect failed\n");
1203 	    ok(EqualRect(&rc, &rc_null), "client rect should be set to 0 on HCBT_CREATEWND\n");
1204             }
1205 	    break;
1206 	}
1207     case HCBT_MOVESIZE:
1208     case HCBT_MINMAX:
1209     case HCBT_ACTIVATE:
1210 	if (pGetWindowInfo && IsWindow(hwnd))
1211 	{
1212 	    WINDOWINFO info;
1213 
1214 	    /* Win98 actually does check the info.cbSize and doesn't allow
1215 	     * it to be anything except sizeof(WINDOWINFO), while Win95, Win2k,
1216 	     * WinXP do not check it at all.
1217 	     */
1218 	    info.cbSize = sizeof(WINDOWINFO);
1219 	    ok(pGetWindowInfo(hwnd, &info), "GetWindowInfo should not fail\n");
1220 	    verify_window_info(code_name, hwnd, &info);
1221 	}
1222         break;
1223     /* window state is undefined */
1224     case HCBT_SETFOCUS:
1225     case HCBT_DESTROYWND:
1226         break;
1227     default:
1228         break;
1229     }
1230 
1231     return CallNextHookEx(hhook, nCode, wParam, lParam);
1232 }
1233 
test_shell_window(void)1234 static void test_shell_window(void)
1235 {
1236     BOOL ret;
1237     DWORD error;
1238     HMODULE hinst, hUser32;
1239     BOOL (WINAPI*SetShellWindow)(HWND);
1240     HWND hwnd1, hwnd2, hwnd3, hwnd4, hwnd5;
1241     HWND shellWindow, nextWnd;
1242 
1243     if (is_win9x)
1244     {
1245         win_skip("Skipping shell window test on Win9x\n");
1246         return;
1247     }
1248 
1249     shellWindow = GetShellWindow();
1250     hinst = GetModuleHandleA(NULL);
1251     hUser32 = GetModuleHandleA("user32");
1252 
1253     SetShellWindow = (void *)GetProcAddress(hUser32, "SetShellWindow");
1254 
1255     trace("previous shell window: %p\n", shellWindow);
1256 
1257     if (shellWindow) {
1258         DWORD pid;
1259         HANDLE hProcess;
1260 
1261         GetWindowThreadProcessId(shellWindow, &pid);
1262         hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
1263         if (!hProcess)
1264         {
1265             skip( "cannot get access to shell process\n" );
1266             return;
1267         }
1268 
1269         SetLastError(0xdeadbeef);
1270         ret = DestroyWindow(shellWindow);
1271         error = GetLastError();
1272 
1273         ok(!ret, "DestroyWindow(shellWindow)\n");
1274         /* passes on Win XP, but not on Win98 */
1275         ok(error==ERROR_ACCESS_DENIED || error == 0xdeadbeef,
1276            "got %u after DestroyWindow(shellWindow)\n", error);
1277 
1278         /* close old shell instance */
1279         ret = TerminateProcess(hProcess, 0);
1280         ok(ret, "termination of previous shell process failed: GetLastError()=%d\n", GetLastError());
1281         WaitForSingleObject(hProcess, INFINITE);    /* wait for termination */
1282         CloseHandle(hProcess);
1283     }
1284 
1285     hwnd1 = CreateWindowExA(0, "#32770", "TEST1", WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 100, 100, 300, 200, 0, 0, hinst, 0);
1286     trace("created window 1: %p\n", hwnd1);
1287 
1288     ret = SetShellWindow(hwnd1);
1289     ok(ret, "first call to SetShellWindow(hwnd1)\n");
1290     shellWindow = GetShellWindow();
1291     ok(shellWindow==hwnd1, "wrong shell window: %p\n", shellWindow);
1292 
1293     ret = SetShellWindow(hwnd1);
1294     ok(!ret, "second call to SetShellWindow(hwnd1)\n");
1295 
1296     ret = SetShellWindow(0);
1297     error = GetLastError();
1298     /* passes on Win XP, but not on Win98
1299     ok(!ret, "reset shell window by SetShellWindow(0)\n");
1300     ok(error==ERROR_INVALID_WINDOW_HANDLE, "ERROR_INVALID_WINDOW_HANDLE after SetShellWindow(0)\n"); */
1301 
1302     ret = SetShellWindow(hwnd1);
1303     /* passes on Win XP, but not on Win98
1304     ok(!ret, "third call to SetShellWindow(hwnd1)\n"); */
1305 
1306     SetWindowLongA(hwnd1, GWL_EXSTYLE, GetWindowLongA(hwnd1,GWL_EXSTYLE)|WS_EX_TOPMOST);
1307     ret = (GetWindowLongA(hwnd1,GWL_EXSTYLE) & WS_EX_TOPMOST) != 0;
1308     ok(!ret, "SetWindowExStyle(hwnd1, WS_EX_TOPMOST)\n");
1309 
1310     ret = DestroyWindow(hwnd1);
1311     ok(ret, "DestroyWindow(hwnd1)\n");
1312 
1313     hwnd2 = CreateWindowExA(WS_EX_TOPMOST, "#32770", "TEST2", WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 150, 250, 300, 200, 0, 0, hinst, 0);
1314     trace("created window 2: %p\n", hwnd2);
1315     ret = SetShellWindow(hwnd2);
1316     ok(!ret, "SetShellWindow(hwnd2) with WS_EX_TOPMOST\n");
1317 
1318     hwnd3 = CreateWindowExA(0, "#32770", "TEST3", WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 200, 400, 300, 200, 0, 0, hinst, 0);
1319     trace("created window 3: %p\n", hwnd3);
1320 
1321     hwnd4 = CreateWindowExA(0, "#32770", "TEST4", WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 250, 500, 300, 200, 0, 0, hinst, 0);
1322     trace("created window 4: %p\n", hwnd4);
1323 
1324     nextWnd = GetWindow(hwnd4, GW_HWNDNEXT);
1325     ok(nextWnd==hwnd3, "wrong next window for hwnd4: %p - expected hwnd3\n", nextWnd);
1326 
1327     ret = SetShellWindow(hwnd4);
1328     ok(ret, "SetShellWindow(hwnd4)\n");
1329     shellWindow = GetShellWindow();
1330     ok(shellWindow==hwnd4, "wrong shell window: %p - expected hwnd4\n", shellWindow);
1331 
1332     nextWnd = GetWindow(hwnd4, GW_HWNDNEXT);
1333     ok(nextWnd==0, "wrong next window for hwnd4: %p - expected 0\n", nextWnd);
1334 
1335     ret = SetWindowPos(hwnd4, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
1336     ok(ret, "SetWindowPos(hwnd4, HWND_TOPMOST)\n");
1337 
1338     ret = SetWindowPos(hwnd4, hwnd3, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
1339     ok(ret, "SetWindowPos(hwnd4, hwnd3\n");
1340 
1341     ret = SetShellWindow(hwnd3);
1342     ok(!ret, "SetShellWindow(hwnd3)\n");
1343     shellWindow = GetShellWindow();
1344     ok(shellWindow==hwnd4, "wrong shell window: %p - expected hwnd4\n", shellWindow);
1345 
1346     hwnd5 = CreateWindowExA(0, "#32770", "TEST5", WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 300, 600, 300, 200, 0, 0, hinst, 0);
1347     trace("created window 5: %p\n", hwnd5);
1348     ret = SetWindowPos(hwnd4, hwnd5, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
1349     ok(ret, "SetWindowPos(hwnd4, hwnd5)\n");
1350 
1351     todo_wine
1352     {
1353         nextWnd = GetWindow(hwnd4, GW_HWNDNEXT);
1354         ok(nextWnd==0, "wrong next window for hwnd4 after SetWindowPos(): %p - expected 0\n", nextWnd);
1355     }
1356 
1357     /* destroy test windows */
1358     DestroyWindow(hwnd2);
1359     DestroyWindow(hwnd3);
1360     DestroyWindow(hwnd4);
1361     DestroyWindow(hwnd5);
1362 }
1363 
1364 /************** MDI test ****************/
1365 
1366 static char mdi_lParam_test_message[] = "just a test string";
1367 
test_MDI_create(HWND parent,HWND mdi_client,INT_PTR first_id)1368 static void test_MDI_create(HWND parent, HWND mdi_client, INT_PTR first_id)
1369 {
1370     MDICREATESTRUCTA mdi_cs;
1371     HWND mdi_child, hwnd, exp_hwnd;
1372     INT_PTR id;
1373     static const WCHAR classW[] = {'M','D','I','_','c','h','i','l','d','_','C','l','a','s','s','_','1',0};
1374     static const WCHAR titleW[] = {'M','D','I',' ','c','h','i','l','d',0};
1375     BOOL isWin9x = FALSE;
1376     HMENU frame_menu = GetMenu(parent);
1377 
1378     ok(frame_menu != NULL, "Frame window didn't have a menu\n");
1379 
1380     mdi_cs.szClass = "MDI_child_Class_1";
1381     mdi_cs.szTitle = "MDI child";
1382     mdi_cs.hOwner = GetModuleHandleA(NULL);
1383     mdi_cs.x = CW_USEDEFAULT;
1384     mdi_cs.y = CW_USEDEFAULT;
1385     mdi_cs.cx = CW_USEDEFAULT;
1386     mdi_cs.cy = CW_USEDEFAULT;
1387     mdi_cs.style = 0;
1388     mdi_cs.lParam = (LPARAM)mdi_lParam_test_message;
1389     mdi_child = (HWND)SendMessageA(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
1390     ok(mdi_child != 0, "MDI child creation failed\n");
1391     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1392     ok(id == first_id, "wrong child id %ld\n", id);
1393     hwnd = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, 0);
1394     exp_hwnd = (GetWindowLongW(mdi_child, GWL_STYLE) & WS_VISIBLE) ? mdi_child : 0;
1395     ok(hwnd == exp_hwnd, "WM_MDIGETACTIVE should return %p, got %p\n", exp_hwnd, hwnd);
1396     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1397     ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1398 
1399     mdi_cs.style = 0x7fffffff; /* without WS_POPUP */
1400     mdi_child = (HWND)SendMessageA(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
1401     ok(mdi_child != 0, "MDI child creation failed\n");
1402     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1403     ok(id == first_id, "wrong child id %ld\n", id);
1404     hwnd = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, 0);
1405     ok(!hwnd, "WM_MDIGETACTIVE should return 0, got %p\n", hwnd);
1406     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1407     ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1408 
1409     mdi_cs.style = 0xffffffff; /* with WS_POPUP */
1410     mdi_child = (HWND)SendMessageA(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
1411     if (GetWindowLongA(mdi_client, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1412     {
1413         ok(!mdi_child, "MDI child with WS_POPUP and with MDIS_ALLCHILDSTYLES should fail\n");
1414     }
1415     else
1416     {
1417         ok(mdi_child != 0, "MDI child creation failed\n");
1418         id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1419         ok(id == first_id, "wrong child id %ld\n", id);
1420         hwnd = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, 0);
1421         ok(!hwnd, "WM_MDIGETACTIVE should return 0, got %p\n", hwnd);
1422         ok(GetMenuItemCount(frame_menu) == 0, "Got wrong frame menu item count: %u\n", GetMenuItemCount(frame_menu));
1423         SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1424         ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1425     }
1426 
1427     /* test MDICREATESTRUCT A<->W mapping */
1428     /* MDICREATESTRUCTA and MDICREATESTRUCTW have the same layout */
1429     mdi_cs.style = 0;
1430     mdi_cs.szClass = (LPCSTR)classW;
1431     mdi_cs.szTitle = (LPCSTR)titleW;
1432     SetLastError(0xdeadbeef);
1433     mdi_child = (HWND)SendMessageW(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
1434     if (!mdi_child)
1435     {
1436         if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1437             isWin9x = TRUE;
1438         else
1439             ok(mdi_child != 0, "MDI child creation failed\n");
1440     }
1441     else
1442     {
1443         id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1444         ok(id == first_id, "wrong child id %ld\n", id);
1445         hwnd = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, 0);
1446         exp_hwnd = (GetWindowLongW(mdi_child, GWL_STYLE) & WS_VISIBLE) ? mdi_child : 0;
1447         ok(hwnd == exp_hwnd, "WM_MDIGETACTIVE should return %p, got %p\n", exp_hwnd, hwnd);
1448         ok(GetMenuItemCount(frame_menu) == 0, "Got wrong frame menu item count: %u\n", GetMenuItemCount(frame_menu));
1449         SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1450         ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1451     }
1452 
1453     mdi_child = CreateMDIWindowA("MDI_child_Class_1", "MDI child",
1454                                  0,
1455                                  CW_USEDEFAULT, CW_USEDEFAULT,
1456                                  CW_USEDEFAULT, CW_USEDEFAULT,
1457                                  mdi_client, GetModuleHandleA(NULL),
1458                                  (LPARAM)mdi_lParam_test_message);
1459     ok(mdi_child != 0, "MDI child creation failed\n");
1460     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1461     ok(id == first_id, "wrong child id %ld\n", id);
1462     hwnd = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, 0);
1463     exp_hwnd = (GetWindowLongW(mdi_child, GWL_STYLE) & WS_VISIBLE) ? mdi_child : 0;
1464     ok(hwnd == exp_hwnd, "WM_MDIGETACTIVE should return %p, got %p\n", exp_hwnd, hwnd);
1465     ok(GetMenuItemCount(frame_menu) == 0, "Got wrong frame menu item count: %u\n", GetMenuItemCount(frame_menu));
1466     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1467     ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1468 
1469     mdi_child = CreateMDIWindowA("MDI_child_Class_1", "MDI child",
1470                                  0x7fffffff, /* without WS_POPUP */
1471                                  CW_USEDEFAULT, CW_USEDEFAULT,
1472                                  CW_USEDEFAULT, CW_USEDEFAULT,
1473                                  mdi_client, GetModuleHandleA(NULL),
1474                                  (LPARAM)mdi_lParam_test_message);
1475     ok(mdi_child != 0, "MDI child creation failed\n");
1476     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1477     ok(id == first_id, "wrong child id %ld\n", id);
1478     hwnd = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, 0);
1479     ok(!hwnd, "WM_MDIGETACTIVE should return 0, got %p\n", hwnd);
1480     ok(GetMenuItemCount(frame_menu) == 0, "Got wrong frame menu item count: %u\n", GetMenuItemCount(frame_menu));
1481     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1482     ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1483 
1484     mdi_child = CreateMDIWindowA("MDI_child_Class_1", "MDI child",
1485                                  0xffffffff, /* with WS_POPUP */
1486                                  CW_USEDEFAULT, CW_USEDEFAULT,
1487                                  CW_USEDEFAULT, CW_USEDEFAULT,
1488                                  mdi_client, GetModuleHandleA(NULL),
1489                                  (LPARAM)mdi_lParam_test_message);
1490     if (GetWindowLongA(mdi_client, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1491     {
1492         ok(!mdi_child, "MDI child with WS_POPUP and with MDIS_ALLCHILDSTYLES should fail\n");
1493     }
1494     else
1495     {
1496         ok(mdi_child != 0, "MDI child creation failed\n");
1497         id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1498         ok(id == first_id, "wrong child id %ld\n", id);
1499         hwnd = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, 0);
1500         ok(!hwnd, "WM_MDIGETACTIVE should return 0, got %p\n", hwnd);
1501         ok(GetMenuItemCount(frame_menu) == 0, "Got wrong frame menu item count: %u\n", GetMenuItemCount(frame_menu));
1502         SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1503         ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1504     }
1505 
1506     /* test MDICREATESTRUCT A<->W mapping */
1507     SetLastError(0xdeadbeef);
1508     mdi_child = CreateMDIWindowW(classW, titleW,
1509                                  0,
1510                                  CW_USEDEFAULT, CW_USEDEFAULT,
1511                                  CW_USEDEFAULT, CW_USEDEFAULT,
1512                                  mdi_client, GetModuleHandleA(NULL),
1513                                  (LPARAM)mdi_lParam_test_message);
1514     if (!mdi_child)
1515     {
1516         if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1517             isWin9x = TRUE;
1518         else
1519             ok(mdi_child != 0, "MDI child creation failed\n");
1520     }
1521     else
1522     {
1523         id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1524         ok(id == first_id, "wrong child id %ld\n", id);
1525         hwnd = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, 0);
1526         exp_hwnd = (GetWindowLongW(mdi_child, GWL_STYLE) & WS_VISIBLE) ? mdi_child : 0;
1527         ok(hwnd == exp_hwnd, "WM_MDIGETACTIVE should return %p, got %p\n", exp_hwnd, hwnd);
1528         ok(GetMenuItemCount(frame_menu) == 0, "Got wrong frame menu item count: %u\n", GetMenuItemCount(frame_menu));
1529         SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1530         ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1531     }
1532 
1533     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_1", "MDI child",
1534                                 0,
1535                                 CW_USEDEFAULT, CW_USEDEFAULT,
1536                                 CW_USEDEFAULT, CW_USEDEFAULT,
1537                                 mdi_client, 0, GetModuleHandleA(NULL),
1538                                 mdi_lParam_test_message);
1539     ok(mdi_child != 0, "MDI child creation failed\n");
1540     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1541     ok(id == first_id, "wrong child id %ld\n", id);
1542     hwnd = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, 0);
1543     exp_hwnd = (GetWindowLongW(mdi_child, GWL_STYLE) & WS_VISIBLE) ? mdi_child : 0;
1544     ok(hwnd == exp_hwnd, "WM_MDIGETACTIVE should return %p, got %p\n", exp_hwnd, hwnd);
1545     ok(GetMenuItemCount(frame_menu) == 0, "Got wrong frame menu item count: %u\n", GetMenuItemCount(frame_menu));
1546     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1547     ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1548 
1549     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_1", "MDI child",
1550                                 WS_MAXIMIZE,
1551                                 CW_USEDEFAULT, CW_USEDEFAULT,
1552                                 CW_USEDEFAULT, CW_USEDEFAULT,
1553                                 mdi_client, 0, GetModuleHandleA(NULL),
1554                                 mdi_lParam_test_message);
1555     ok(mdi_child != 0, "MDI child creation failed\n");
1556     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1557     ok(id == first_id, "wrong child id %ld\n", id);
1558     hwnd = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, 0);
1559     exp_hwnd = (GetWindowLongW(mdi_child, GWL_STYLE) & WS_VISIBLE) ? mdi_child : 0;
1560     ok(hwnd == exp_hwnd, "WM_MDIGETACTIVE should return %p, got %p\n", exp_hwnd, hwnd);
1561     if (GetWindowLongA(mdi_client, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1562         ok(GetMenuItemCount(frame_menu) == 0, "Got wrong frame menu item count: %u\n", GetMenuItemCount(frame_menu));
1563     else
1564         ok(GetMenuItemCount(frame_menu) == 4, "Got wrong frame menu item count: %u\n", GetMenuItemCount(frame_menu));
1565     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1566     ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1567 
1568     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_1", "MDI child",
1569                                 0x7fffffff, /* without WS_POPUP */
1570                                 CW_USEDEFAULT, CW_USEDEFAULT,
1571                                 CW_USEDEFAULT, CW_USEDEFAULT,
1572                                 mdi_client, 0, GetModuleHandleA(NULL),
1573                                 mdi_lParam_test_message);
1574     ok(mdi_child != 0, "MDI child creation failed\n");
1575     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1576     ok(id == first_id, "wrong child id %ld\n", id);
1577     hwnd = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, 0);
1578     ok(!hwnd, "WM_MDIGETACTIVE should return 0, got %p\n", hwnd);
1579     ok(GetMenuItemCount(frame_menu) == 0, "Got wrong frame menu item count: %u\n", GetMenuItemCount(frame_menu));
1580     SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1581     ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1582 
1583     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_1", "MDI child",
1584                                 0xffffffff, /* with WS_POPUP */
1585                                 CW_USEDEFAULT, CW_USEDEFAULT,
1586                                 CW_USEDEFAULT, CW_USEDEFAULT,
1587                                 mdi_client, 0, GetModuleHandleA(NULL),
1588                                 mdi_lParam_test_message);
1589     if (GetWindowLongA(mdi_client, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1590     {
1591         ok(!mdi_child, "MDI child with WS_POPUP and with MDIS_ALLCHILDSTYLES should fail\n");
1592     }
1593     else
1594     {
1595         ok(mdi_child != 0, "MDI child creation failed\n");
1596         id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1597         ok(id == first_id, "wrong child id %ld\n", id);
1598         hwnd = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, 0);
1599         ok(!hwnd, "WM_MDIGETACTIVE should return 0, got %p\n", hwnd);
1600         ok(GetMenuItemCount(frame_menu) == 0, "Got wrong frame menu item count: %u\n", GetMenuItemCount(frame_menu));
1601         SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1602         ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1603     }
1604 
1605     /* test MDICREATESTRUCT A<->W mapping */
1606     SetLastError(0xdeadbeef);
1607     mdi_child = CreateWindowExW(WS_EX_MDICHILD, classW, titleW,
1608                                 0,
1609                                 CW_USEDEFAULT, CW_USEDEFAULT,
1610                                 CW_USEDEFAULT, CW_USEDEFAULT,
1611                                 mdi_client, 0, GetModuleHandleA(NULL),
1612                                 mdi_lParam_test_message);
1613     if (!mdi_child)
1614     {
1615         if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1616             isWin9x = TRUE;
1617         else
1618             ok(mdi_child != 0, "MDI child creation failed\n");
1619     }
1620     else
1621     {
1622         id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1623         ok(id == first_id, "wrong child id %ld\n", id);
1624         hwnd = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, 0);
1625         exp_hwnd = (GetWindowLongW(mdi_child, GWL_STYLE) & WS_VISIBLE) ? mdi_child : 0;
1626         ok(hwnd == exp_hwnd, "WM_MDIGETACTIVE should return %p, got %p\n", exp_hwnd, hwnd);
1627         ok(GetMenuItemCount(frame_menu) == 0, "Got wrong frame menu item count: %u\n", GetMenuItemCount(frame_menu));
1628         SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1629         ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1630     }
1631 
1632     /* This test fails on Win9x */
1633     if (!isWin9x)
1634     {
1635         mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_2", "MDI child",
1636                                 WS_CHILD,
1637                                 CW_USEDEFAULT, CW_USEDEFAULT,
1638                                 CW_USEDEFAULT, CW_USEDEFAULT,
1639                                 parent, 0, GetModuleHandleA(NULL),
1640                                 mdi_lParam_test_message);
1641         ok(!mdi_child, "WS_EX_MDICHILD with a not MDIClient parent should fail\n");
1642     }
1643 
1644     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1645                                 WS_CHILD, /* without WS_POPUP */
1646                                 CW_USEDEFAULT, CW_USEDEFAULT,
1647                                 CW_USEDEFAULT, CW_USEDEFAULT,
1648                                 mdi_client, 0, GetModuleHandleA(NULL),
1649                                 mdi_lParam_test_message);
1650     ok(mdi_child != 0, "MDI child creation failed\n");
1651     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1652     ok(id == 0, "wrong child id %ld\n", id);
1653     ok(GetMenuItemCount(frame_menu) == 0, "Got wrong frame menu item count: %u\n", GetMenuItemCount(frame_menu));
1654     hwnd = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, 0);
1655     ok(!hwnd, "WM_MDIGETACTIVE should return 0, got %p\n", hwnd);
1656     DestroyWindow(mdi_child);
1657 
1658     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1659                                 WS_CHILD | WS_POPUP, /* with WS_POPUP */
1660                                 CW_USEDEFAULT, CW_USEDEFAULT,
1661                                 CW_USEDEFAULT, CW_USEDEFAULT,
1662                                 mdi_client, 0, GetModuleHandleA(NULL),
1663                                 mdi_lParam_test_message);
1664     ok(mdi_child != 0, "MDI child creation failed\n");
1665     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1666     ok(id == 0, "wrong child id %ld\n", id);
1667     hwnd = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, 0);
1668     ok(!hwnd, "WM_MDIGETACTIVE should return 0, got %p\n", hwnd);
1669     ok(GetMenuItemCount(frame_menu) == 0, "Got wrong frame menu item count: %u\n", GetMenuItemCount(frame_menu));
1670     DestroyWindow(mdi_child);
1671 
1672     /* maximized child */
1673     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1674                                 WS_CHILD | WS_MAXIMIZE,
1675                                 CW_USEDEFAULT, CW_USEDEFAULT,
1676                                 CW_USEDEFAULT, CW_USEDEFAULT,
1677                                 mdi_client, 0, GetModuleHandleA(NULL),
1678                                 mdi_lParam_test_message);
1679     ok(mdi_child != 0, "MDI child creation failed\n");
1680     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1681     ok(id == 0, "wrong child id %ld\n", id);
1682     hwnd = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, 0);
1683     ok(!hwnd, "WM_MDIGETACTIVE should return 0, got %p\n", hwnd);
1684     ok(GetMenuItemCount(frame_menu) == 0, "Got wrong frame menu item count: %u\n", GetMenuItemCount(frame_menu));
1685     DestroyWindow(mdi_child);
1686 
1687     trace("Creating maximized child with a caption\n");
1688     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1689                                 WS_CHILD | WS_MAXIMIZE | WS_CAPTION,
1690                                 CW_USEDEFAULT, CW_USEDEFAULT,
1691                                 CW_USEDEFAULT, CW_USEDEFAULT,
1692                                 mdi_client, 0, GetModuleHandleA(NULL),
1693                                 mdi_lParam_test_message);
1694     ok(mdi_child != 0, "MDI child creation failed\n");
1695     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1696     ok(id == 0, "wrong child id %ld\n", id);
1697     hwnd = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, 0);
1698     ok(!hwnd, "WM_MDIGETACTIVE should return 0, got %p\n", hwnd);
1699     ok(GetMenuItemCount(frame_menu) == 0, "Got wrong frame menu item count: %u\n", GetMenuItemCount(frame_menu));
1700     DestroyWindow(mdi_child);
1701 
1702     trace("Creating maximized child with a caption and a thick frame\n");
1703     mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1704                                 WS_CHILD | WS_MAXIMIZE | WS_CAPTION | WS_THICKFRAME,
1705                                 CW_USEDEFAULT, CW_USEDEFAULT,
1706                                 CW_USEDEFAULT, CW_USEDEFAULT,
1707                                 mdi_client, 0, GetModuleHandleA(NULL),
1708                                 mdi_lParam_test_message);
1709     ok(mdi_child != 0, "MDI child creation failed\n");
1710     id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1711     ok(id == 0, "wrong child id %ld\n", id);
1712     hwnd = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, 0);
1713     ok(!hwnd, "WM_MDIGETACTIVE should return 0, got %p\n", hwnd);
1714     ok(GetMenuItemCount(frame_menu) == 0, "Got wrong frame menu item count: %u\n", GetMenuItemCount(frame_menu));
1715     DestroyWindow(mdi_child);
1716 }
1717 
test_MDI_child_stack(HWND mdi_client)1718 static void test_MDI_child_stack(HWND mdi_client)
1719 {
1720     HWND child_1, child_2, child_3, child_4;
1721     HWND stack[4];
1722     MDICREATESTRUCTA cs;
1723 
1724     cs.szClass = "MDI_child_Class_1";
1725     cs.szTitle = "MDI child";
1726     cs.hOwner  = GetModuleHandleA(0);
1727     cs.x       = CW_USEDEFAULT;
1728     cs.y       = CW_USEDEFAULT;
1729     cs.cx      = CW_USEDEFAULT;
1730     cs.cy      = CW_USEDEFAULT;
1731     cs.style   = 0;
1732     cs.lParam  = (LPARAM)mdi_lParam_test_message;
1733 
1734     child_1 = (HWND)SendMessageA(mdi_client, WM_MDICREATE, 0, (LPARAM)&cs);
1735     ok(child_1 != 0, "expected child_1 to be non NULL\n");
1736     child_2 = (HWND)SendMessageA(mdi_client, WM_MDICREATE, 0, (LPARAM)&cs);
1737     ok(child_2 != 0, "expected child_2 to be non NULL\n");
1738     child_3 = (HWND)SendMessageA(mdi_client, WM_MDICREATE, 0, (LPARAM)&cs);
1739     ok(child_3 != 0, "expected child_3 to be non NULL\n");
1740     child_4 = (HWND)SendMessageA(mdi_client, WM_MDICREATE, 0, (LPARAM)&cs);
1741     ok(child_4 != 0, "expected child_4 to be non NULL\n");
1742 
1743     stack[0] = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, 0);
1744     stack[1] = GetWindow(stack[0], GW_HWNDNEXT);
1745     stack[2] = GetWindow(stack[1], GW_HWNDNEXT);
1746     stack[3] = GetWindow(stack[2], GW_HWNDNEXT);
1747     trace("Initial MDI child stack: %p->%p->%p->%p\n", stack[0], stack[1], stack[2], stack[3]);
1748     ok(stack[0] == child_4 && stack[1] == child_3 &&
1749         stack[2] == child_2 && stack[3] == child_1,
1750         "Unexpected initial order, should be: %p->%p->%p->%p\n",
1751             child_4, child_3, child_2, child_1);
1752 
1753     trace("Activate child next to %p\n", child_3);
1754     SendMessageA(mdi_client, WM_MDINEXT, (WPARAM)child_3, 0);
1755 
1756     stack[0] = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, 0);
1757     stack[1] = GetWindow(stack[0], GW_HWNDNEXT);
1758     stack[2] = GetWindow(stack[1], GW_HWNDNEXT);
1759     stack[3] = GetWindow(stack[2], GW_HWNDNEXT);
1760     ok(stack[0] == child_2 && stack[1] == child_4 &&
1761         stack[2] == child_1 && stack[3] == child_3,
1762         "Broken MDI child stack:\nexpected: %p->%p->%p->%p, but got: %p->%p->%p->%p\n",
1763             child_2, child_4, child_1, child_3, stack[0], stack[1], stack[2], stack[3]);
1764 
1765     trace("Activate child previous to %p\n", child_1);
1766     SendMessageA(mdi_client, WM_MDINEXT, (WPARAM)child_1, 1);
1767 
1768     stack[0] = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, 0);
1769     stack[1] = GetWindow(stack[0], GW_HWNDNEXT);
1770     stack[2] = GetWindow(stack[1], GW_HWNDNEXT);
1771     stack[3] = GetWindow(stack[2], GW_HWNDNEXT);
1772     ok(stack[0] == child_4 && stack[1] == child_2 &&
1773         stack[2] == child_1 && stack[3] == child_3,
1774         "Broken MDI child stack:\nexpected: %p->%p->%p->%p, but got: %p->%p->%p->%p\n",
1775             child_4, child_2, child_1, child_3, stack[0], stack[1], stack[2], stack[3]);
1776 
1777     DestroyWindow(child_1);
1778     DestroyWindow(child_2);
1779     DestroyWindow(child_3);
1780     DestroyWindow(child_4);
1781 }
1782 
1783 /**********************************************************************
1784  * MDI_ChildGetMinMaxInfo (copied from windows/mdi.c)
1785  *
1786  * Note: The rule here is that client rect of the maximized MDI child
1787  *	 is equal to the client rect of the MDI client window.
1788  */
MDI_ChildGetMinMaxInfo(HWND client,HWND hwnd,MINMAXINFO * lpMinMax)1789 static void MDI_ChildGetMinMaxInfo( HWND client, HWND hwnd, MINMAXINFO* lpMinMax )
1790 {
1791     RECT rect;
1792 
1793     GetClientRect( client, &rect );
1794     AdjustWindowRectEx( &rect, GetWindowLongA( hwnd, GWL_STYLE ),
1795                         0, GetWindowLongA( hwnd, GWL_EXSTYLE ));
1796 
1797     rect.right -= rect.left;
1798     rect.bottom -= rect.top;
1799     lpMinMax->ptMaxSize.x = rect.right;
1800     lpMinMax->ptMaxSize.y = rect.bottom;
1801 
1802     lpMinMax->ptMaxPosition.x = rect.left;
1803     lpMinMax->ptMaxPosition.y = rect.top;
1804 
1805     trace("max rect %s\n", wine_dbgstr_rect(&rect));
1806 }
1807 
mdi_child_wnd_proc_1(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)1808 static LRESULT WINAPI mdi_child_wnd_proc_1(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1809 {
1810     switch (msg)
1811     {
1812         case WM_NCCREATE:
1813         case WM_CREATE:
1814         {
1815             CREATESTRUCTA *cs = (CREATESTRUCTA *)lparam;
1816             MDICREATESTRUCTA *mdi_cs = cs->lpCreateParams;
1817 
1818             ok(cs->dwExStyle & WS_EX_MDICHILD, "WS_EX_MDICHILD should be set\n");
1819             ok(mdi_cs->lParam == (LPARAM)mdi_lParam_test_message, "wrong mdi_cs->lParam\n");
1820 
1821             ok(!lstrcmpA(cs->lpszClass, "MDI_child_Class_1"), "wrong class name\n");
1822             ok(!lstrcmpA(cs->lpszClass, mdi_cs->szClass), "class name does not match\n");
1823             ok(!lstrcmpA(cs->lpszName, "MDI child"), "wrong title\n");
1824             ok(!lstrcmpA(cs->lpszName, mdi_cs->szTitle), "title does not match\n");
1825             ok(cs->hInstance == mdi_cs->hOwner, "%p != %p\n", cs->hInstance, mdi_cs->hOwner);
1826 
1827             /* MDICREATESTRUCT should have original values */
1828             ok(mdi_cs->style == 0 || mdi_cs->style == 0x7fffffff || mdi_cs->style == 0xffffffff || mdi_cs->style == WS_MAXIMIZE,
1829                 "mdi_cs->style does not match (%08x)\n", mdi_cs->style);
1830             ok(mdi_cs->x == CW_USEDEFAULT, "%d != CW_USEDEFAULT\n", mdi_cs->x);
1831             ok(mdi_cs->y == CW_USEDEFAULT, "%d != CW_USEDEFAULT\n", mdi_cs->y);
1832             ok(mdi_cs->cx == CW_USEDEFAULT, "%d != CW_USEDEFAULT\n", mdi_cs->cx);
1833             ok(mdi_cs->cy == CW_USEDEFAULT, "%d != CW_USEDEFAULT\n", mdi_cs->cy);
1834 
1835             /* CREATESTRUCT should have fixed values */
1836             ok(cs->x != CW_USEDEFAULT, "%d == CW_USEDEFAULT\n", cs->x);
1837             ok(cs->y != CW_USEDEFAULT, "%d == CW_USEDEFAULT\n", cs->y);
1838 
1839             /* cx/cy == CW_USEDEFAULT are translated to NOT zero values */
1840             ok(cs->cx != CW_USEDEFAULT && cs->cx != 0, "%d == CW_USEDEFAULT\n", cs->cx);
1841             ok(cs->cy != CW_USEDEFAULT && cs->cy != 0, "%d == CW_USEDEFAULT\n", cs->cy);
1842 
1843             ok(!(cs->style & WS_POPUP), "WS_POPUP is not allowed\n");
1844 
1845             if (GetWindowLongA(cs->hwndParent, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1846             {
1847                 LONG style = mdi_cs->style | WS_CHILD | WS_CLIPSIBLINGS;
1848                 ok(cs->style == style,
1849                    "cs->style does not match (%08x)\n", cs->style);
1850             }
1851             else
1852             {
1853                 LONG style = mdi_cs->style;
1854                 style &= ~WS_POPUP;
1855                 style |= WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CAPTION |
1856                     WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
1857                 ok(cs->style == style,
1858                    "cs->style does not match (%08x)\n", cs->style);
1859             }
1860             break;
1861         }
1862 
1863         case WM_GETMINMAXINFO:
1864         {
1865             HWND client = GetParent(hwnd);
1866             RECT rc;
1867             MINMAXINFO *minmax = (MINMAXINFO *)lparam;
1868             MINMAXINFO my_minmax;
1869             LONG style, exstyle;
1870 
1871             style = GetWindowLongA(hwnd, GWL_STYLE);
1872             exstyle = GetWindowLongA(hwnd, GWL_EXSTYLE);
1873 
1874             GetClientRect(client, &rc);
1875 
1876             GetClientRect(client, &rc);
1877             if ((style & WS_CAPTION) == WS_CAPTION)
1878                 style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
1879             AdjustWindowRectEx(&rc, style, 0, exstyle);
1880             trace("MDI child: calculated max window size = (%d x %d)\n", rc.right-rc.left, rc.bottom-rc.top);
1881             dump_minmax_info( minmax );
1882 
1883             ok(minmax->ptMaxSize.x == rc.right - rc.left, "default width of maximized child %d != %d\n",
1884                minmax->ptMaxSize.x, rc.right - rc.left);
1885             ok(minmax->ptMaxSize.y == rc.bottom - rc.top, "default height of maximized child %d != %d\n",
1886                minmax->ptMaxSize.y, rc.bottom - rc.top);
1887 
1888             DefMDIChildProcA(hwnd, msg, wparam, lparam);
1889 
1890             trace("DefMDIChildProc returned:\n");
1891             dump_minmax_info( minmax );
1892 
1893             MDI_ChildGetMinMaxInfo(client, hwnd, &my_minmax);
1894             ok(minmax->ptMaxSize.x == my_minmax.ptMaxSize.x, "default width of maximized child %d != %d\n",
1895                minmax->ptMaxSize.x, my_minmax.ptMaxSize.x);
1896             ok(minmax->ptMaxSize.y == my_minmax.ptMaxSize.y, "default height of maximized child %d != %d\n",
1897                minmax->ptMaxSize.y, my_minmax.ptMaxSize.y);
1898 
1899             return 1;
1900         }
1901 
1902         case WM_MDIACTIVATE:
1903         {
1904             HWND active, client = GetParent(hwnd);
1905             /*trace("%p WM_MDIACTIVATE %08x %08lx\n", hwnd, wparam, lparam);*/
1906             active = (HWND)SendMessageA(client, WM_MDIGETACTIVE, 0, 0);
1907             if (hwnd == (HWND)lparam) /* if we are being activated */
1908                 ok (active == (HWND)lparam, "new active %p != active %p\n", (HWND)lparam, active);
1909             else
1910                 ok (active == (HWND)wparam, "old active %p != active %p\n", (HWND)wparam, active);
1911             break;
1912         }
1913     }
1914     return DefMDIChildProcA(hwnd, msg, wparam, lparam);
1915 }
1916 
mdi_child_wnd_proc_2(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)1917 static LRESULT WINAPI mdi_child_wnd_proc_2(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1918 {
1919     switch (msg)
1920     {
1921         case WM_NCCREATE:
1922         case WM_CREATE:
1923         {
1924             CREATESTRUCTA *cs = (CREATESTRUCTA *)lparam;
1925 
1926             trace("%s: x %d, y %d, cx %d, cy %d\n", (msg == WM_NCCREATE) ? "WM_NCCREATE" : "WM_CREATE",
1927                 cs->x, cs->y, cs->cx, cs->cy);
1928 
1929             ok(!(cs->dwExStyle & WS_EX_MDICHILD), "WS_EX_MDICHILD should not be set\n");
1930             ok(cs->lpCreateParams == mdi_lParam_test_message, "wrong cs->lpCreateParams\n");
1931 
1932             ok(!lstrcmpA(cs->lpszClass, "MDI_child_Class_2"), "wrong class name\n");
1933             ok(!lstrcmpA(cs->lpszName, "MDI child"), "wrong title\n");
1934 
1935             /* CREATESTRUCT should have fixed values */
1936             /* For some reason Win9x doesn't translate cs->x from CW_USEDEFAULT,
1937                while NT does. */
1938             /*ok(cs->x != CW_USEDEFAULT, "%d == CW_USEDEFAULT\n", cs->x);*/
1939             ok(cs->y != CW_USEDEFAULT, "%d == CW_USEDEFAULT\n", cs->y);
1940 
1941             /* cx/cy == CW_USEDEFAULT are translated to 0 */
1942             /* For some reason Win98 doesn't translate cs->cx from CW_USEDEFAULT,
1943                while Win95, Win2k, WinXP do. */
1944             /*ok(cs->cx == 0, "%d != 0\n", cs->cx);*/
1945             ok(cs->cy == 0, "%d != 0\n", cs->cy);
1946             break;
1947         }
1948 
1949         case WM_GETMINMAXINFO:
1950         {
1951             HWND parent = GetParent(hwnd);
1952             RECT rc;
1953             MINMAXINFO *minmax = (MINMAXINFO *)lparam;
1954             LONG style, exstyle;
1955 
1956             style = GetWindowLongA(hwnd, GWL_STYLE);
1957             exstyle = GetWindowLongA(hwnd, GWL_EXSTYLE);
1958 
1959             GetClientRect(parent, &rc);
1960             trace("WM_GETMINMAXINFO: parent %p client size = (%d x %d)\n", parent, rc.right, rc.bottom);
1961 
1962             GetClientRect(parent, &rc);
1963             if ((style & WS_CAPTION) == WS_CAPTION)
1964                 style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
1965             AdjustWindowRectEx(&rc, style, 0, exstyle);
1966             dump_minmax_info( minmax );
1967 
1968             ok(minmax->ptMaxSize.x == rc.right - rc.left, "default width of maximized child %d != %d\n",
1969                minmax->ptMaxSize.x, rc.right - rc.left);
1970             ok(minmax->ptMaxSize.y == rc.bottom - rc.top, "default height of maximized child %d != %d\n",
1971                minmax->ptMaxSize.y, rc.bottom - rc.top);
1972             break;
1973         }
1974 
1975         case WM_WINDOWPOSCHANGED:
1976         {
1977             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
1978             RECT rc1, rc2;
1979 
1980             GetWindowRect(hwnd, &rc1);
1981             SetRect(&rc2, winpos->x, winpos->y, winpos->x + winpos->cx, winpos->y + winpos->cy);
1982             /* note: winpos coordinates are relative to parent */
1983             MapWindowPoints(GetParent(hwnd), 0, (LPPOINT)&rc2, 2);
1984             ok(EqualRect(&rc1, &rc2), "rects do not match, window=%s pos=%s\n",
1985                wine_dbgstr_rect(&rc1), wine_dbgstr_rect(&rc2));
1986             GetWindowRect(hwnd, &rc1);
1987             GetClientRect(hwnd, &rc2);
1988             DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc1);
1989             MapWindowPoints(0, hwnd, (LPPOINT)&rc1, 2);
1990             ok(EqualRect(&rc1, &rc2), "rects do not match, window=%s client=%s\n",
1991                wine_dbgstr_rect(&rc1), wine_dbgstr_rect(&rc2));
1992         }
1993         /* fall through */
1994         case WM_WINDOWPOSCHANGING:
1995         {
1996             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
1997             WINDOWPOS my_winpos = *winpos;
1998 
1999             trace("%s: %p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
2000                   (msg == WM_WINDOWPOSCHANGING) ? "WM_WINDOWPOSCHANGING" : "WM_WINDOWPOSCHANGED",
2001                   winpos->hwnd, winpos->hwndInsertAfter,
2002                   winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
2003 
2004             DefWindowProcA(hwnd, msg, wparam, lparam);
2005 
2006             ok(!memcmp(&my_winpos, winpos, sizeof(WINDOWPOS)),
2007                "DefWindowProc should not change WINDOWPOS: %p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
2008                   winpos->hwnd, winpos->hwndInsertAfter,
2009                   winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
2010 
2011             return 1;
2012         }
2013     }
2014     return DefWindowProcA(hwnd, msg, wparam, lparam);
2015 }
2016 
mdi_main_wnd_procA(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)2017 static LRESULT WINAPI mdi_main_wnd_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
2018 {
2019     static HWND mdi_client;
2020 
2021     switch (msg)
2022     {
2023         case WM_CREATE:
2024             return 1;
2025 
2026         case WM_WINDOWPOSCHANGED:
2027         {
2028             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
2029             RECT rc1, rc2;
2030 
2031             GetWindowRect(hwnd, &rc1);
2032             trace("window: %s\n", wine_dbgstr_rect(&rc1));
2033             SetRect(&rc2, winpos->x, winpos->y, winpos->x + winpos->cx, winpos->y + winpos->cy);
2034             /* note: winpos coordinates are relative to parent */
2035             MapWindowPoints(GetParent(hwnd), 0, (LPPOINT)&rc2, 2);
2036             trace("pos: %s\n", wine_dbgstr_rect(&rc2));
2037             ok(EqualRect(&rc1, &rc2), "rects do not match\n");
2038 
2039             GetWindowRect(hwnd, &rc1);
2040             GetClientRect(hwnd, &rc2);
2041             DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc1);
2042             MapWindowPoints(0, hwnd, (LPPOINT)&rc1, 2);
2043             ok(EqualRect(&rc1, &rc2), "rects do not match\n");
2044         }
2045         /* fall through */
2046         case WM_WINDOWPOSCHANGING:
2047         {
2048             WINDOWPOS *winpos = (WINDOWPOS *)lparam;
2049             WINDOWPOS my_winpos = *winpos;
2050 
2051             trace("%s\n", (msg == WM_WINDOWPOSCHANGING) ? "WM_WINDOWPOSCHANGING" : "WM_WINDOWPOSCHANGED");
2052             trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
2053                   winpos->hwnd, winpos->hwndInsertAfter,
2054                   winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
2055 
2056             DefWindowProcA(hwnd, msg, wparam, lparam);
2057 
2058             trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
2059                   winpos->hwnd, winpos->hwndInsertAfter,
2060                   winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
2061 
2062             ok(!memcmp(&my_winpos, winpos, sizeof(WINDOWPOS)),
2063                "DefWindowProc should not change WINDOWPOS values\n");
2064 
2065             return 1;
2066         }
2067 
2068         case WM_CLOSE:
2069             PostQuitMessage(0);
2070             break;
2071     }
2072     return DefFrameProcA(hwnd, mdi_client, msg, wparam, lparam);
2073 }
2074 
mdi_RegisterWindowClasses(void)2075 static BOOL mdi_RegisterWindowClasses(void)
2076 {
2077     WNDCLASSA cls;
2078 
2079     cls.style = 0;
2080     cls.lpfnWndProc = mdi_main_wnd_procA;
2081     cls.cbClsExtra = 0;
2082     cls.cbWndExtra = 0;
2083     cls.hInstance = GetModuleHandleA(0);
2084     cls.hIcon = 0;
2085     cls.hCursor = LoadCursorA(0, (LPCSTR)IDC_ARROW);
2086     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
2087     cls.lpszMenuName = NULL;
2088     cls.lpszClassName = "MDI_parent_Class";
2089     if(!RegisterClassA(&cls)) return FALSE;
2090 
2091     cls.lpfnWndProc = mdi_child_wnd_proc_1;
2092     cls.lpszClassName = "MDI_child_Class_1";
2093     if(!RegisterClassA(&cls)) return FALSE;
2094 
2095     cls.lpfnWndProc = mdi_child_wnd_proc_2;
2096     cls.lpszClassName = "MDI_child_Class_2";
2097     if(!RegisterClassA(&cls)) return FALSE;
2098 
2099     return TRUE;
2100 }
2101 
test_mdi(void)2102 static void test_mdi(void)
2103 {
2104     static const DWORD style[] = { 0, WS_HSCROLL, WS_VSCROLL, WS_HSCROLL | WS_VSCROLL };
2105     HWND mdi_hwndMain, mdi_client, mdi_child;
2106     CLIENTCREATESTRUCT client_cs;
2107     RECT rc;
2108     DWORD i;
2109     MSG msg;
2110     HMENU frame_menu, child_menu;
2111 
2112     if (!mdi_RegisterWindowClasses()) assert(0);
2113 
2114     mdi_hwndMain = CreateWindowExA(0, "MDI_parent_Class", "MDI parent window",
2115                                    WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
2116                                    WS_MAXIMIZEBOX /*| WS_VISIBLE*/,
2117                                    100, 100, CW_USEDEFAULT, CW_USEDEFAULT,
2118                                    GetDesktopWindow(), 0,
2119                                    GetModuleHandleA(NULL), NULL);
2120     assert(mdi_hwndMain);
2121 
2122     frame_menu = CreateMenu();
2123 
2124     GetClientRect(mdi_hwndMain, &rc);
2125 
2126     client_cs.hWindowMenu = 0;
2127     client_cs.idFirstChild = 1;
2128 
2129     for (i = 0; i < sizeof(style)/sizeof(style[0]); i++)
2130     {
2131         SCROLLINFO si;
2132         BOOL ret, gotit;
2133 
2134         mdi_client = CreateWindowExA(0, "mdiclient", NULL,
2135                                  WS_CHILD | style[i],
2136                                  0, 0, rc.right, rc.bottom,
2137                                  mdi_hwndMain, 0, 0, &client_cs);
2138         ok(mdi_client != 0, "MDI client creation failed\n");
2139 
2140         mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_1", "MDI child",
2141                                 0,
2142                                 CW_USEDEFAULT, CW_USEDEFAULT,
2143                                 CW_USEDEFAULT, CW_USEDEFAULT,
2144                                 mdi_client, 0, 0,
2145                                 mdi_lParam_test_message);
2146         ok(mdi_child != 0, "MDI child creation failed\n");
2147 
2148         SendMessageW(mdi_child, WM_SIZE, SIZE_MAXIMIZED, 0);
2149         SetMenu(mdi_hwndMain, frame_menu);
2150 
2151         ok(GetMenuItemCount(frame_menu) == 0, "Frame menu should be empty after child maximize, but has %u\n",
2152                 GetMenuItemCount(frame_menu));
2153 
2154         child_menu = CreateMenu();
2155         SendMessageW(mdi_client, WM_MDISETMENU, 0, (LPARAM)child_menu);
2156 
2157         ok(GetMenuItemCount(frame_menu) == 0, "Frame menu should be empty after WM_MDISETMENU, but has %u\n",
2158                 GetMenuItemCount(frame_menu));
2159 
2160         SendMessageW(mdi_child, WM_SIZE, SIZE_RESTORED, 0);
2161 
2162         ok(GetMenuItemCount(frame_menu) == 0, "Frame menu should be empty after child restored, but has %u items\n",
2163                 GetMenuItemCount(frame_menu));
2164 
2165         SetMenu(mdi_hwndMain, NULL);
2166 
2167         si.cbSize = sizeof(si);
2168         si.fMask = SIF_ALL;
2169         ret = GetScrollInfo(mdi_client, SB_HORZ, &si);
2170         if (style[i] & (WS_HSCROLL | WS_VSCROLL))
2171         {
2172             ok(ret, "style %#x: GetScrollInfo(SB_HORZ) failed\n", style[i]);
2173             ok(si.nPage == 0, "expected 0\n");
2174             ok(si.nPos == 0, "expected 0\n");
2175             ok(si.nTrackPos == 0, "expected 0\n");
2176             ok(si.nMin == 0, "expected 0\n");
2177             ok(si.nMax == 100, "expected 100\n");
2178         }
2179         else
2180             ok(!ret, "style %#x: GetScrollInfo(SB_HORZ) should fail\n", style[i]);
2181 
2182         ret = GetScrollInfo(mdi_client, SB_VERT, &si);
2183         if (style[i] & (WS_HSCROLL | WS_VSCROLL))
2184         {
2185             ok(ret, "style %#x: GetScrollInfo(SB_VERT) failed\n", style[i]);
2186             ok(si.nPage == 0, "expected 0\n");
2187             ok(si.nPos == 0, "expected 0\n");
2188             ok(si.nTrackPos == 0, "expected 0\n");
2189             ok(si.nMin == 0, "expected 0\n");
2190             ok(si.nMax == 100, "expected 100\n");
2191         }
2192         else
2193             ok(!ret, "style %#x: GetScrollInfo(SB_VERT) should fail\n", style[i]);
2194 
2195         SetWindowPos(mdi_child, 0, -100, -100, 0, 0, SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOSIZE);
2196 
2197         si.cbSize = sizeof(si);
2198         si.fMask = SIF_ALL;
2199         ret = GetScrollInfo(mdi_client, SB_HORZ, &si);
2200         if (style[i] & (WS_HSCROLL | WS_VSCROLL))
2201         {
2202             ok(ret, "style %#x: GetScrollInfo(SB_HORZ) failed\n", style[i]);
2203             ok(si.nPage == 0, "expected 0\n");
2204             ok(si.nPos == 0, "expected 0\n");
2205             ok(si.nTrackPos == 0, "expected 0\n");
2206             ok(si.nMin == 0, "expected 0\n");
2207             ok(si.nMax == 100, "expected 100\n");
2208         }
2209         else
2210             ok(!ret, "style %#x: GetScrollInfo(SB_HORZ) should fail\n", style[i]);
2211 
2212         ret = GetScrollInfo(mdi_client, SB_VERT, &si);
2213         if (style[i] & (WS_HSCROLL | WS_VSCROLL))
2214         {
2215             ok(ret, "style %#x: GetScrollInfo(SB_VERT) failed\n", style[i]);
2216             ok(si.nPage == 0, "expected 0\n");
2217             ok(si.nPos == 0, "expected 0\n");
2218             ok(si.nTrackPos == 0, "expected 0\n");
2219             ok(si.nMin == 0, "expected 0\n");
2220             ok(si.nMax == 100, "expected 100\n");
2221         }
2222         else
2223             ok(!ret, "style %#x: GetScrollInfo(SB_VERT) should fail\n", style[i]);
2224 
2225         gotit = FALSE;
2226         while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE))
2227         {
2228             if (msg.message == WM_MOUSEMOVE || msg.message == WM_PAINT)
2229             {
2230                 DispatchMessageA(&msg);
2231                 continue;
2232             }
2233 
2234             if (msg.message == 0x003f) /* WM_MDICALCCHILDSCROLL ??? */
2235             {
2236                 ok(msg.hwnd == mdi_client, "message 0x003f should be posted to mdiclient\n");
2237                 gotit = TRUE;
2238             }
2239             else
2240                 ok(msg.hwnd != mdi_client, "message %04x should not be posted to mdiclient\n", msg.message);
2241             DispatchMessageA(&msg);
2242         }
2243         ok(gotit, "message 0x003f should appear after SetWindowPos\n");
2244 
2245         si.cbSize = sizeof(si);
2246         si.fMask = SIF_ALL;
2247         ret = GetScrollInfo(mdi_client, SB_HORZ, &si);
2248         if (style[i] & (WS_HSCROLL | WS_VSCROLL))
2249         {
2250             ok(ret, "style %#x: GetScrollInfo(SB_HORZ) failed\n", style[i]);
2251 todo_wine
2252             ok(si.nPage != 0, "expected !0\n");
2253             ok(si.nPos == 0, "expected 0\n");
2254             ok(si.nTrackPos == 0, "expected 0\n");
2255             ok(si.nMin != 0, "expected !0\n");
2256             ok(si.nMax != 100, "expected !100\n");
2257         }
2258         else
2259             ok(!ret, "style %#x: GetScrollInfo(SB_HORZ) should fail\n", style[i]);
2260 
2261         ret = GetScrollInfo(mdi_client, SB_VERT, &si);
2262         if (style[i] & (WS_HSCROLL | WS_VSCROLL))
2263         {
2264             ok(ret, "style %#x: GetScrollInfo(SB_VERT) failed\n", style[i]);
2265 todo_wine
2266             ok(si.nPage != 0, "expected !0\n");
2267             ok(si.nPos == 0, "expected 0\n");
2268             ok(si.nTrackPos == 0, "expected 0\n");
2269             ok(si.nMin != 0, "expected !0\n");
2270             ok(si.nMax != 100, "expected !100\n");
2271         }
2272         else
2273             ok(!ret, "style %#x: GetScrollInfo(SB_VERT) should fail\n", style[i]);
2274 
2275         DestroyMenu(child_menu);
2276         DestroyWindow(mdi_child);
2277         DestroyWindow(mdi_client);
2278     }
2279 
2280     SetMenu(mdi_hwndMain, frame_menu);
2281 
2282     mdi_client = CreateWindowExA(0, "mdiclient", NULL,
2283                              WS_CHILD,
2284                              0, 0, rc.right, rc.bottom,
2285                              mdi_hwndMain, 0, 0, &client_cs);
2286     ok(mdi_client != 0, "MDI client creation failed\n");
2287 
2288     mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_1", "MDI child",
2289                             0,
2290                             CW_USEDEFAULT, CW_USEDEFAULT,
2291                             CW_USEDEFAULT, CW_USEDEFAULT,
2292                             mdi_client, 0, 0,
2293                             mdi_lParam_test_message);
2294     ok(mdi_child != 0, "MDI child creation failed\n");
2295 
2296     SendMessageW(mdi_child, WM_SIZE, SIZE_MAXIMIZED, 0);
2297     ok(GetMenuItemCount(frame_menu) == 4, "Frame menu should have 4 items after child maximize, but has %u\n",
2298             GetMenuItemCount(frame_menu));
2299 
2300     child_menu = CreateMenu();
2301     SendMessageW(mdi_client, WM_MDISETMENU, 0, (LPARAM)child_menu);
2302 
2303     ok(GetMenuItemCount(frame_menu) == 4, "Frame menu should have 4 items after WM_MDISETMENU, but has %u\n",
2304             GetMenuItemCount(frame_menu));
2305 
2306     SendMessageW(mdi_child, WM_SIZE, SIZE_RESTORED, 0);
2307 
2308     ok(GetMenuItemCount(frame_menu) == 0, "Frame menu should be empty after child restored, but has %u items\n",
2309             GetMenuItemCount(frame_menu));
2310 
2311     DestroyMenu(child_menu);
2312     DestroyWindow(mdi_child);
2313     DestroyWindow(mdi_client);
2314 
2315     /* MDIClient without MDIS_ALLCHILDSTYLES */
2316     mdi_client = CreateWindowExA(0, "mdiclient",
2317                                  NULL,
2318                                  WS_CHILD /*| WS_VISIBLE*/,
2319                                   /* tests depend on a not zero MDIClient size */
2320                                  0, 0, rc.right, rc.bottom,
2321                                  mdi_hwndMain, 0, GetModuleHandleA(NULL),
2322                                  &client_cs);
2323     assert(mdi_client);
2324     test_MDI_create(mdi_hwndMain, mdi_client, client_cs.idFirstChild);
2325     DestroyWindow(mdi_client);
2326 
2327     /* MDIClient with MDIS_ALLCHILDSTYLES */
2328     mdi_client = CreateWindowExA(0, "mdiclient",
2329                                  NULL,
2330                                  WS_CHILD | MDIS_ALLCHILDSTYLES /*| WS_VISIBLE*/,
2331                                   /* tests depend on a not zero MDIClient size */
2332                                  0, 0, rc.right, rc.bottom,
2333                                  mdi_hwndMain, 0, GetModuleHandleA(NULL),
2334                                  &client_cs);
2335     assert(mdi_client);
2336     test_MDI_create(mdi_hwndMain, mdi_client, client_cs.idFirstChild);
2337     DestroyWindow(mdi_client);
2338 
2339     /* Test child window stack management */
2340     mdi_client = CreateWindowExA(0, "mdiclient",
2341                                  NULL,
2342                                  WS_CHILD,
2343                                  0, 0, rc.right, rc.bottom,
2344                                  mdi_hwndMain, 0, GetModuleHandleA(NULL),
2345                                  &client_cs);
2346     assert(mdi_client);
2347     test_MDI_child_stack(mdi_client);
2348     DestroyWindow(mdi_client);
2349 /*
2350     while(GetMessage(&msg, 0, 0, 0))
2351     {
2352         TranslateMessage(&msg);
2353         DispatchMessage(&msg);
2354     }
2355 */
2356     DestroyWindow(mdi_hwndMain);
2357 }
2358 
test_icons(void)2359 static void test_icons(void)
2360 {
2361     WNDCLASSEXA cls;
2362     HWND hwnd;
2363     HICON icon = LoadIconA(0, (LPCSTR)IDI_APPLICATION);
2364     HICON icon2 = LoadIconA(0, (LPCSTR)IDI_QUESTION);
2365     HICON small_icon = LoadImageA(0, (LPCSTR)IDI_APPLICATION, IMAGE_ICON,
2366                                   GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED );
2367     HICON res;
2368 
2369     cls.cbSize = sizeof(cls);
2370     cls.style = 0;
2371     cls.lpfnWndProc = DefWindowProcA;
2372     cls.cbClsExtra = 0;
2373     cls.cbWndExtra = 0;
2374     cls.hInstance = 0;
2375     cls.hIcon = LoadIconA(0, (LPCSTR)IDI_HAND);
2376     cls.hIconSm = small_icon;
2377     cls.hCursor = LoadCursorA(0, (LPCSTR)IDC_ARROW);
2378     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
2379     cls.lpszMenuName = NULL;
2380     cls.lpszClassName = "IconWindowClass";
2381 
2382     RegisterClassExA(&cls);
2383 
2384     hwnd = CreateWindowExA(0, "IconWindowClass", "icon test", 0,
2385                            100, 100, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, NULL, NULL);
2386     assert( hwnd );
2387 
2388     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_BIG, 0 );
2389     ok( res == 0, "wrong big icon %p/0\n", res );
2390     res = (HICON)SendMessageA( hwnd, WM_SETICON, ICON_BIG, (LPARAM)icon );
2391     ok( res == 0, "wrong previous big icon %p/0\n", res );
2392     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_BIG, 0 );
2393     ok( res == icon, "wrong big icon after set %p/%p\n", res, icon );
2394     res = (HICON)SendMessageA( hwnd, WM_SETICON, ICON_BIG, (LPARAM)icon2 );
2395     ok( res == icon, "wrong previous big icon %p/%p\n", res, icon );
2396     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_BIG, 0 );
2397     ok( res == icon2, "wrong big icon after set %p/%p\n", res, icon2 );
2398 
2399     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL, 0 );
2400     ok( res == 0, "wrong small icon %p/0\n", res );
2401     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL2, 0 );
2402     ok( (res && res != small_icon && res != icon2) || broken(!res), "wrong small2 icon %p\n", res );
2403     res = (HICON)SendMessageA( hwnd, WM_SETICON, ICON_SMALL, (LPARAM)icon );
2404     ok( res == 0, "wrong previous small icon %p/0\n", res );
2405     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL, 0 );
2406     ok( res == icon, "wrong small icon after set %p/%p\n", res, icon );
2407     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL2, 0 );
2408     ok( res == icon || broken(!res), "wrong small2 icon after set %p/%p\n", res, icon );
2409     res = (HICON)SendMessageA( hwnd, WM_SETICON, ICON_SMALL, (LPARAM)small_icon );
2410     ok( res == icon, "wrong previous small icon %p/%p\n", res, icon );
2411     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL, 0 );
2412     ok( res == small_icon, "wrong small icon after set %p/%p\n", res, small_icon );
2413     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL2, 0 );
2414     ok( res == small_icon || broken(!res), "wrong small2 icon after set %p/%p\n", res, small_icon );
2415 
2416     /* make sure the big icon hasn't changed */
2417     res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_BIG, 0 );
2418     ok( res == icon2, "wrong big icon after set %p/%p\n", res, icon2 );
2419 
2420     DestroyWindow( hwnd );
2421 }
2422 
nccalcsize_proc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)2423 static LRESULT WINAPI nccalcsize_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
2424 {
2425     if (msg == WM_NCCALCSIZE)
2426     {
2427         RECT *rect = (RECT *)lparam;
2428         /* first time around increase the rectangle, next time decrease it */
2429         if (rect->left == 100) InflateRect( rect, 10, 10 );
2430         else InflateRect( rect, -10, -10 );
2431         return 0;
2432     }
2433     return DefWindowProcA( hwnd, msg, wparam, lparam );
2434 }
2435 
test_SetWindowPos(HWND hwnd,HWND hwnd2)2436 static void test_SetWindowPos(HWND hwnd, HWND hwnd2)
2437 {
2438     RECT orig_win_rc, rect;
2439     LONG_PTR old_proc;
2440     HWND hwnd_grandchild, hwnd_child, hwnd_child2;
2441     HWND hwnd_desktop;
2442     RECT rc1, rc2;
2443     BOOL ret;
2444 
2445     SetRect(&rect, 111, 222, 333, 444);
2446     ok(!GetWindowRect(0, &rect), "GetWindowRect succeeded\n");
2447     ok(rect.left == 111 && rect.top == 222 && rect.right == 333 && rect.bottom == 444,
2448        "wrong window rect %s\n", wine_dbgstr_rect(&rect));
2449 
2450     SetRect(&rect, 111, 222, 333, 444);
2451     ok(!GetClientRect(0, &rect), "GetClientRect succeeded\n");
2452     ok(rect.left == 111 && rect.top == 222 && rect.right == 333 && rect.bottom == 444,
2453        "wrong window rect %s\n", wine_dbgstr_rect(&rect));
2454 
2455     GetWindowRect(hwnd, &orig_win_rc);
2456 
2457     old_proc = SetWindowLongPtrA( hwnd, GWLP_WNDPROC, (ULONG_PTR)nccalcsize_proc );
2458     ret = SetWindowPos(hwnd, 0, 100, 100, 0, 0, SWP_NOZORDER|SWP_FRAMECHANGED);
2459     ok(ret, "Got %d\n", ret);
2460     GetWindowRect( hwnd, &rect );
2461     ok( rect.left == 100 && rect.top == 100 && rect.right == 100 && rect.bottom == 100,
2462         "invalid window rect %s\n", wine_dbgstr_rect(&rect));
2463     GetClientRect( hwnd, &rect );
2464     MapWindowPoints( hwnd, 0, (POINT *)&rect, 2 );
2465     ok( rect.left == 90 && rect.top == 90 && rect.right == 110 && rect.bottom == 110,
2466         "invalid client rect %s\n", wine_dbgstr_rect(&rect));
2467 
2468     ret = SetWindowPos(hwnd, 0, 200, 200, 0, 0, SWP_NOZORDER|SWP_FRAMECHANGED);
2469     ok(ret, "Got %d\n", ret);
2470     GetWindowRect( hwnd, &rect );
2471     ok( rect.left == 200 && rect.top == 200 && rect.right == 200 && rect.bottom == 200,
2472         "invalid window rect %s\n", wine_dbgstr_rect(&rect));
2473     GetClientRect( hwnd, &rect );
2474     MapWindowPoints( hwnd, 0, (POINT *)&rect, 2 );
2475     ok( rect.left == 210 && rect.top == 210 && rect.right == 190 && rect.bottom == 190,
2476         "invalid client rect %s\n", wine_dbgstr_rect(&rect));
2477 
2478     ret = SetWindowPos(hwnd, 0, orig_win_rc.left, orig_win_rc.top,
2479                       orig_win_rc.right, orig_win_rc.bottom, 0);
2480     ok(ret, "Got %d\n", ret);
2481     SetWindowLongPtrA( hwnd, GWLP_WNDPROC, old_proc );
2482 
2483     /* Win9x truncates coordinates to 16-bit irrespectively */
2484     if (!is_win9x)
2485     {
2486         ret = SetWindowPos(hwnd, 0, -32769, -40000, -32769, -90000, SWP_NOMOVE);
2487         ok(ret, "Got %d\n", ret);
2488         ret = SetWindowPos(hwnd, 0, 32768, 40000, 32768, 40000, SWP_NOMOVE);
2489         ok(ret, "Got %d\n", ret);
2490 
2491         ret = SetWindowPos(hwnd, 0, -32769, -40000, -32769, -90000, SWP_NOSIZE);
2492         ok(ret, "Got %d\n", ret);
2493         ret = SetWindowPos(hwnd, 0, 32768, 40000, 32768, 40000, SWP_NOSIZE);
2494         ok(ret, "Got %d\n", ret);
2495     }
2496 
2497     ret = SetWindowPos(hwnd, 0, orig_win_rc.left, orig_win_rc.top,
2498                        orig_win_rc.right, orig_win_rc.bottom, 0);
2499     ok(ret, "Got %d\n", ret);
2500 
2501     ok(!(GetWindowLongA(hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST), "WS_EX_TOPMOST should not be set\n");
2502     ret = SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
2503     ok(ret, "Got %d\n", ret);
2504     ok(GetWindowLongA(hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST, "WS_EX_TOPMOST should be set\n");
2505     ret = SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
2506     ok(ret, "Got %d\n", ret);
2507     ok(GetWindowLongA(hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST, "WS_EX_TOPMOST should be set\n");
2508     ret = SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
2509     ok(ret, "Got %d\n", ret);
2510     ok(!(GetWindowLongA(hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST), "WS_EX_TOPMOST should not be set\n");
2511 
2512     hwnd_desktop = GetDesktopWindow();
2513     ok(!!hwnd_desktop, "Failed to get hwnd_desktop window (%d).\n", GetLastError());
2514     hwnd_child = create_tool_window(WS_VISIBLE|WS_CHILD, hwnd);
2515     ok(!!hwnd_child, "Failed to create child window (%d)\n", GetLastError());
2516     hwnd_grandchild = create_tool_window(WS_VISIBLE|WS_CHILD, hwnd_child);
2517     ok(!!hwnd_child, "Failed to create child window (%d)\n", GetLastError());
2518     hwnd_child2 = create_tool_window(WS_VISIBLE|WS_CHILD, hwnd);
2519     ok(!!hwnd_child2, "Failed to create second child window (%d)\n", GetLastError());
2520 
2521     ret = SetWindowPos(hwnd, hwnd2, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
2522     ok(ret, "Got %d\n", ret);
2523     check_active_state(hwnd, hwnd, hwnd);
2524 
2525     ret = SetWindowPos(hwnd2, hwnd, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
2526     ok(ret, "Got %d\n", ret);
2527     check_active_state(hwnd2, hwnd2, hwnd2);
2528 
2529     /* Returns TRUE also for windows that are not siblings */
2530     ret = SetWindowPos(hwnd_child, hwnd2, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
2531     ok(ret, "Got %d\n", ret);
2532     check_active_state(hwnd2, hwnd2, hwnd2);
2533 
2534     ret = SetWindowPos(hwnd2, hwnd_child, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
2535     ok(ret, "Got %d\n", ret);
2536     check_active_state(hwnd2, hwnd2, hwnd2);
2537 
2538     /* Does not seem to do anything even without passing flags, still returns TRUE */
2539     GetWindowRect(hwnd_child, &rc1);
2540     ret = SetWindowPos(hwnd_child, hwnd2 , 1, 2, 3, 4, 0);
2541     ok(ret, "Got %d\n", ret);
2542     GetWindowRect(hwnd_child, &rc2);
2543     ok(EqualRect(&rc1, &rc2), "%s != %s\n", wine_dbgstr_rect(&rc1), wine_dbgstr_rect(&rc2));
2544     check_active_state(hwnd2, hwnd2, hwnd2);
2545 
2546     /* Same thing the other way around. */
2547     GetWindowRect(hwnd2, &rc1);
2548     ret = SetWindowPos(hwnd2, hwnd_child, 1, 2, 3, 4, 0);
2549     ok(ret, "Got %d\n", ret);
2550     GetWindowRect(hwnd2, &rc2);
2551     ok(EqualRect(&rc1, &rc2), "%s != %s\n", wine_dbgstr_rect(&rc1), wine_dbgstr_rect(&rc2));
2552     check_active_state(hwnd2, hwnd2, hwnd2);
2553 
2554     /* .. and with these windows. */
2555     GetWindowRect(hwnd_grandchild, &rc1);
2556     ret = SetWindowPos(hwnd_grandchild, hwnd_child2, 1, 2, 3, 4, 0);
2557     ok(ret, "Got %d\n", ret);
2558     GetWindowRect(hwnd_grandchild, &rc2);
2559     ok(EqualRect(&rc1, &rc2), "%s != %s\n", wine_dbgstr_rect(&rc1), wine_dbgstr_rect(&rc2));
2560     check_active_state(hwnd2, hwnd2, hwnd2);
2561 
2562     /* Add SWP_NOZORDER and it will be properly resized. */
2563     GetWindowRect(hwnd_grandchild, &rc1);
2564     ret = SetWindowPos(hwnd_grandchild, hwnd_child2, 1, 2, 3, 4, SWP_NOZORDER);
2565     ok(ret, "Got %d\n", ret);
2566     GetWindowRect(hwnd_grandchild, &rc2);
2567     ok((rc1.left+1) == rc2.left && (rc1.top+2) == rc2.top &&
2568        (rc1.left+4) == rc2.right && (rc1.top+6) == rc2.bottom,
2569        "(%d,%d)-(%d,%d) != %s\n", rc1.left+1, rc1.top+2, rc1.left+4, rc1.top+6,
2570        wine_dbgstr_rect(&rc2));
2571     check_active_state(hwnd2, hwnd2, hwnd2);
2572 
2573     /* Given a sibling window, the window is properly resized. */
2574     GetWindowRect(hwnd_child, &rc1);
2575     ret = SetWindowPos(hwnd_child, hwnd_child2, 1, 2, 3, 4, 0);
2576     ok(ret, "Got %d\n", ret);
2577     GetWindowRect(hwnd_child, &rc2);
2578     ok((rc1.left+1) == rc2.left && (rc1.top+2) == rc2.top &&
2579        (rc1.left+4) == rc2.right && (rc1.top+6) == rc2.bottom,
2580        "(%d,%d)-(%d,%d) != %s\n", rc1.left+1, rc1.top+2, rc1.left+4, rc1.top+6,
2581        wine_dbgstr_rect(&rc2));
2582     check_active_state(hwnd2, hwnd2, hwnd2);
2583 
2584     /* Involving the desktop window changes things. */
2585     ret = SetWindowPos(hwnd_child, hwnd_desktop, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
2586     ok(!ret, "Got %d\n", ret);
2587     check_active_state(hwnd2, hwnd2, hwnd2);
2588 
2589     GetWindowRect(hwnd_child, &rc1);
2590     ret = SetWindowPos(hwnd_child, hwnd_desktop, 0, 0, 0, 0, 0);
2591     ok(!ret, "Got %d\n", ret);
2592     GetWindowRect(hwnd_child, &rc2);
2593     ok(EqualRect(&rc1, &rc2), "%s != %s\n", wine_dbgstr_rect(&rc1), wine_dbgstr_rect(&rc2));
2594     check_active_state(hwnd2, hwnd2, hwnd2);
2595 
2596     ret = SetWindowPos(hwnd_desktop, hwnd_child, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
2597     ok(!ret, "Got %d\n", ret);
2598     check_active_state(hwnd2, hwnd2, hwnd2);
2599 
2600     ret = SetWindowPos(hwnd_desktop, hwnd, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
2601     ok(!ret, "Got %d\n", ret);
2602     check_active_state(hwnd2, hwnd2, hwnd2);
2603 
2604     ret = SetWindowPos(hwnd, hwnd_desktop, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
2605     ok(!ret, "Got %d\n", ret);
2606     check_active_state(hwnd2, hwnd2, hwnd2);
2607 
2608     DestroyWindow(hwnd_grandchild);
2609     DestroyWindow(hwnd_child);
2610     DestroyWindow(hwnd_child2);
2611 
2612     hwnd_child = create_tool_window(WS_CHILD|WS_POPUP|WS_SYSMENU, hwnd2);
2613     ok(!!hwnd_child, "Failed to create child window (%d)\n", GetLastError());
2614     ret = SetWindowPos(hwnd_child, NULL, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_SHOWWINDOW);
2615     ok(ret, "Got %d\n", ret);
2616     flush_events( TRUE );
2617     todo_wine check_active_state(hwnd2, hwnd2, hwnd2);
2618     DestroyWindow(hwnd_child);
2619 }
2620 
test_SetMenu(HWND parent)2621 static void test_SetMenu(HWND parent)
2622 {
2623     HWND child;
2624     HMENU hMenu, ret;
2625     BOOL retok;
2626     DWORD style;
2627 
2628     hMenu = CreateMenu();
2629     assert(hMenu);
2630 
2631     ok(SetMenu(parent, hMenu), "SetMenu on a top level window should not fail\n");
2632     if (0)
2633     {
2634     /* fails on (at least) Wine, NT4, XP SP2 */
2635     test_nonclient_area(parent);
2636     }
2637     ret = GetMenu(parent);
2638     ok(ret == hMenu, "unexpected menu id %p\n", ret);
2639     /* test whether we can destroy a menu assigned to a window */
2640     retok = DestroyMenu(hMenu);
2641     ok( retok, "DestroyMenu error %d\n", GetLastError());
2642     retok = IsMenu(hMenu);
2643     ok(!retok || broken(retok) /* nt4 */, "menu handle should be not valid after DestroyMenu\n");
2644     ret = GetMenu(parent);
2645     /* This test fails on Win9x */
2646     if (!is_win9x)
2647         ok(ret == hMenu, "unexpected menu id %p\n", ret);
2648     ok(SetMenu(parent, 0), "SetMenu(0) on a top level window should not fail\n");
2649     test_nonclient_area(parent);
2650 
2651     hMenu = CreateMenu();
2652     assert(hMenu);
2653 
2654     /* parent */
2655     ret = GetMenu(parent);
2656     ok(ret == 0, "unexpected menu id %p\n", ret);
2657 
2658     ok(!SetMenu(parent, (HMENU)20), "SetMenu with invalid menu handle should fail\n");
2659     test_nonclient_area(parent);
2660     ret = GetMenu(parent);
2661     ok(ret == 0, "unexpected menu id %p\n", ret);
2662 
2663     ok(SetMenu(parent, hMenu), "SetMenu on a top level window should not fail\n");
2664     if (0)
2665     {
2666     /* fails on (at least) Wine, NT4, XP SP2 */
2667     test_nonclient_area(parent);
2668     }
2669     ret = GetMenu(parent);
2670     ok(ret == hMenu, "unexpected menu id %p\n", ret);
2671 
2672     ok(SetMenu(parent, 0), "SetMenu(0) on a top level window should not fail\n");
2673     test_nonclient_area(parent);
2674     ret = GetMenu(parent);
2675     ok(ret == 0, "unexpected menu id %p\n", ret);
2676 
2677     /* child */
2678     child = CreateWindowExA(0, "static", NULL, WS_CHILD, 0, 0, 0, 0, parent, (HMENU)10, 0, NULL);
2679     assert(child);
2680 
2681     ret = GetMenu(child);
2682     ok(ret == (HMENU)10, "unexpected menu id %p\n", ret);
2683 
2684     ok(!SetMenu(child, (HMENU)20), "SetMenu with invalid menu handle should fail\n");
2685     test_nonclient_area(child);
2686     ret = GetMenu(child);
2687     ok(ret == (HMENU)10, "unexpected menu id %p\n", ret);
2688 
2689     ok(!SetMenu(child, hMenu), "SetMenu on a child window should fail\n");
2690     test_nonclient_area(child);
2691     ret = GetMenu(child);
2692     ok(ret == (HMENU)10, "unexpected menu id %p\n", ret);
2693 
2694     ok(!SetMenu(child, 0), "SetMenu(0) on a child window should fail\n");
2695     test_nonclient_area(child);
2696     ret = GetMenu(child);
2697     ok(ret == (HMENU)10, "unexpected menu id %p\n", ret);
2698 
2699     style = GetWindowLongA(child, GWL_STYLE);
2700     SetWindowLongA(child, GWL_STYLE, style | WS_POPUP);
2701     ok(SetMenu(child, hMenu), "SetMenu on a popup child window should not fail\n");
2702     ok(SetMenu(child, 0), "SetMenu on a popup child window should not fail\n");
2703     SetWindowLongA(child, GWL_STYLE, style);
2704 
2705     SetWindowLongA(child, GWL_STYLE, style | WS_OVERLAPPED);
2706     ok(!SetMenu(child, hMenu), "SetMenu on an overlapped child window should fail\n");
2707     SetWindowLongA(child, GWL_STYLE, style);
2708 
2709     DestroyWindow(child);
2710     DestroyMenu(hMenu);
2711 }
2712 
test_window_tree(HWND parent,const DWORD * style,const int * order,int total)2713 static void test_window_tree(HWND parent, const DWORD *style, const int *order, int total)
2714 {
2715     HWND child[5], hwnd;
2716     INT_PTR i;
2717 
2718     assert(total <= 5);
2719 
2720     hwnd = GetWindow(parent, GW_CHILD);
2721     ok(!hwnd, "have to start without children to perform the test\n");
2722 
2723     for (i = 0; i < total; i++)
2724     {
2725         if (style[i] & DS_CONTROL)
2726         {
2727             child[i] = CreateWindowExA(0, (LPCSTR)MAKEINTATOM(32770), "", style[i] & ~WS_VISIBLE,
2728                                        0,0,0,0, parent, (HMENU)i, 0, NULL);
2729             if (style[i] & WS_VISIBLE)
2730                 ShowWindow(child[i], SW_SHOW);
2731 
2732             SetWindowPos(child[i], HWND_BOTTOM, 0,0,10,10, SWP_NOACTIVATE);
2733         }
2734         else
2735             child[i] = CreateWindowExA(0, "static", "", style[i], 0,0,10,10,
2736                                        parent, (HMENU)i, 0, NULL);
2737         trace("child[%ld] = %p\n", i, child[i]);
2738         ok(child[i] != 0, "CreateWindowEx failed to create child window\n");
2739     }
2740 
2741     hwnd = GetWindow(parent, GW_CHILD);
2742     ok(hwnd != 0, "GetWindow(GW_CHILD) failed\n");
2743     ok(hwnd == GetWindow(child[total - 1], GW_HWNDFIRST), "GW_HWNDFIRST is wrong\n");
2744     ok(child[order[total - 1]] == GetWindow(child[0], GW_HWNDLAST), "GW_HWNDLAST is wrong\n");
2745 
2746     for (i = 0; i < total; i++)
2747     {
2748         trace("hwnd[%ld] = %p\n", i, hwnd);
2749         ok(child[order[i]] == hwnd, "Z order of child #%ld is wrong\n", i);
2750 
2751         hwnd = GetWindow(hwnd, GW_HWNDNEXT);
2752     }
2753 
2754     for (i = 0; i < total; i++)
2755         ok(DestroyWindow(child[i]), "DestroyWindow failed\n");
2756 }
2757 
test_children_zorder(HWND parent)2758 static void test_children_zorder(HWND parent)
2759 {
2760     const DWORD simple_style[5] = { WS_CHILD, WS_CHILD, WS_CHILD, WS_CHILD,
2761                                     WS_CHILD };
2762     const int simple_order[5] = { 0, 1, 2, 3, 4 };
2763 
2764     const DWORD complex_style[5] = { WS_CHILD, WS_CHILD | WS_MAXIMIZE,
2765                              WS_CHILD | WS_VISIBLE, WS_CHILD,
2766                              WS_CHILD | WS_MAXIMIZE | WS_VISIBLE };
2767     const int complex_order_1[1] = { 0 };
2768     const int complex_order_2[2] = { 1, 0 };
2769     const int complex_order_3[3] = { 1, 0, 2 };
2770     const int complex_order_4[4] = { 1, 0, 2, 3 };
2771     const int complex_order_5[5] = { 4, 1, 0, 2, 3 };
2772     const DWORD complex_style_6[3] = { WS_CHILD | WS_VISIBLE,
2773                                        WS_CHILD | WS_CLIPSIBLINGS | DS_CONTROL | WS_VISIBLE,
2774                                        WS_CHILD | WS_VISIBLE };
2775     const int complex_order_6[3] = { 0, 1, 2 };
2776 
2777     /* simple WS_CHILD */
2778     test_window_tree(parent, simple_style, simple_order, 5);
2779 
2780     /* complex children styles */
2781     test_window_tree(parent, complex_style, complex_order_1, 1);
2782     test_window_tree(parent, complex_style, complex_order_2, 2);
2783     test_window_tree(parent, complex_style, complex_order_3, 3);
2784     test_window_tree(parent, complex_style, complex_order_4, 4);
2785     test_window_tree(parent, complex_style, complex_order_5, 5);
2786 
2787     /* another set of complex children styles */
2788     test_window_tree(parent, complex_style_6, complex_order_6, 3);
2789 }
2790 
2791 #define check_z_order(hwnd, next, prev, owner, topmost) \
2792         check_z_order_debug((hwnd), (next), (prev), (owner), (topmost), \
2793                             __FILE__, __LINE__)
2794 
check_z_order_debug(HWND hwnd,HWND next,HWND prev,HWND owner,BOOL topmost,const char * file,int line)2795 static void check_z_order_debug(HWND hwnd, HWND next, HWND prev, HWND owner,
2796                                 BOOL topmost, const char *file, int line)
2797 {
2798     HWND test;
2799     DWORD ex_style;
2800 
2801     test = GetWindow(hwnd, GW_HWNDNEXT);
2802     /* skip foreign windows */
2803     while (test && test != next &&
2804            (GetWindowThreadProcessId(test, NULL) != our_pid ||
2805             UlongToHandle(GetWindowLongPtrA(test, GWLP_HINSTANCE)) != GetModuleHandleA(NULL) ||
2806             GetWindow(test, GW_OWNER) == next))
2807     {
2808         /*trace("skipping next %p (%p)\n", test, UlongToHandle(GetWindowLongPtr(test, GWLP_HINSTANCE)));*/
2809         test = GetWindow(test, GW_HWNDNEXT);
2810     }
2811     ok_(file, line)(next == test, "%p: expected next %p, got %p\n", hwnd, next, test);
2812 
2813     test = GetWindow(hwnd, GW_HWNDPREV);
2814     /* skip foreign windows */
2815     while (test && test != prev &&
2816            (GetWindowThreadProcessId(test, NULL) != our_pid ||
2817             UlongToHandle(GetWindowLongPtrA(test, GWLP_HINSTANCE)) != GetModuleHandleA(NULL) ||
2818             GetWindow(test, GW_OWNER) == hwnd))
2819     {
2820         /*trace("skipping prev %p (%p)\n", test, UlongToHandle(GetWindowLongPtr(test, GWLP_HINSTANCE)));*/
2821         test = GetWindow(test, GW_HWNDPREV);
2822     }
2823     ok_(file, line)(prev == test, "%p: expected prev %p, got %p\n", hwnd, prev, test);
2824 
2825     test = GetWindow(hwnd, GW_OWNER);
2826     ok_(file, line)(owner == test, "%p: expected owner %p, got %p\n", hwnd, owner, test);
2827 
2828     ex_style = GetWindowLongA(hwnd, GWL_EXSTYLE);
2829     ok_(file, line)(!(ex_style & WS_EX_TOPMOST) == !topmost, "%p: expected %stopmost\n",
2830                     hwnd, topmost ? "" : "NOT ");
2831 }
2832 
test_popup_zorder(HWND hwnd_D,HWND hwnd_E,DWORD style)2833 static void test_popup_zorder(HWND hwnd_D, HWND hwnd_E, DWORD style)
2834 {
2835     HWND hwnd_A, hwnd_B, hwnd_C, hwnd_F;
2836 
2837     trace("hwnd_D %p, hwnd_E %p\n", hwnd_D, hwnd_E);
2838 
2839     SetWindowPos(hwnd_E, hwnd_D, 0,0,0,0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE);
2840 
2841     check_z_order(hwnd_D, hwnd_E, 0, 0, FALSE);
2842     check_z_order(hwnd_E, 0, hwnd_D, 0, FALSE);
2843 
2844     hwnd_F = CreateWindowExA(0, "MainWindowClass", "Owner window",
2845                             WS_OVERLAPPED | WS_CAPTION,
2846                             100, 100, 100, 100,
2847                             0, 0, GetModuleHandleA(NULL), NULL);
2848     trace("hwnd_F %p\n", hwnd_F);
2849     check_z_order(hwnd_F, hwnd_D, 0, 0, FALSE);
2850 
2851     SetWindowPos(hwnd_F, hwnd_E, 0,0,0,0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE);
2852     check_z_order(hwnd_F, 0, hwnd_E, 0, FALSE);
2853     check_z_order(hwnd_E, hwnd_F, hwnd_D, 0, FALSE);
2854     check_z_order(hwnd_D, hwnd_E, 0, 0, FALSE);
2855 
2856     hwnd_C = CreateWindowExA(0, "MainWindowClass", NULL,
2857                             style,
2858                             100, 100, 100, 100,
2859                             hwnd_F, 0, GetModuleHandleA(NULL), NULL);
2860     trace("hwnd_C %p\n", hwnd_C);
2861     check_z_order(hwnd_F, 0, hwnd_E, 0, FALSE);
2862     check_z_order(hwnd_E, hwnd_F, hwnd_D, 0, FALSE);
2863     check_z_order(hwnd_D, hwnd_E, hwnd_C, 0, FALSE);
2864     check_z_order(hwnd_C, hwnd_D, 0, hwnd_F, FALSE);
2865 
2866     hwnd_B = CreateWindowExA(WS_EX_TOPMOST, "MainWindowClass", NULL,
2867                             style,
2868                             100, 100, 100, 100,
2869                             hwnd_F, 0, GetModuleHandleA(NULL), NULL);
2870     trace("hwnd_B %p\n", hwnd_B);
2871     check_z_order(hwnd_F, 0, hwnd_E, 0, FALSE);
2872     check_z_order(hwnd_E, hwnd_F, hwnd_D, 0, FALSE);
2873     check_z_order(hwnd_D, hwnd_E, hwnd_C, 0, FALSE);
2874     check_z_order(hwnd_C, hwnd_D, hwnd_B, hwnd_F, FALSE);
2875     check_z_order(hwnd_B, hwnd_C, 0, hwnd_F, TRUE);
2876 
2877     hwnd_A = CreateWindowExA(WS_EX_TOPMOST, "MainWindowClass", NULL,
2878                             style,
2879                             100, 100, 100, 100,
2880                             0, 0, GetModuleHandleA(NULL), NULL);
2881     trace("hwnd_A %p\n", hwnd_A);
2882     check_z_order(hwnd_F, 0, hwnd_E, 0, FALSE);
2883     check_z_order(hwnd_E, hwnd_F, hwnd_D, 0, FALSE);
2884     check_z_order(hwnd_D, hwnd_E, hwnd_C, 0, FALSE);
2885     check_z_order(hwnd_C, hwnd_D, hwnd_B, hwnd_F, FALSE);
2886     check_z_order(hwnd_B, hwnd_C, hwnd_A, hwnd_F, TRUE);
2887     check_z_order(hwnd_A, hwnd_B, 0, 0, TRUE);
2888 
2889     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);
2890 
2891     /* move hwnd_F and its popups up */
2892     SetWindowPos(hwnd_F, HWND_TOP, 0,0,0,0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE);
2893     check_z_order(hwnd_E, 0, hwnd_D, 0, FALSE);
2894     check_z_order(hwnd_D, hwnd_E, hwnd_F, 0, FALSE);
2895     check_z_order(hwnd_F, hwnd_D, hwnd_C, 0, FALSE);
2896     check_z_order(hwnd_C, hwnd_F, hwnd_B, hwnd_F, FALSE);
2897     check_z_order(hwnd_B, hwnd_C, hwnd_A, hwnd_F, TRUE);
2898     check_z_order(hwnd_A, hwnd_B, 0, 0, TRUE);
2899 
2900     /* move hwnd_F and its popups down */
2901 #if 0 /* enable once Wine is fixed to pass this test */
2902     SetWindowPos(hwnd_F, HWND_BOTTOM, 0,0,0,0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE);
2903     check_z_order(hwnd_F, 0, hwnd_C, 0, FALSE);
2904     check_z_order(hwnd_C, hwnd_F, hwnd_B, hwnd_F, FALSE);
2905     check_z_order(hwnd_B, hwnd_C, hwnd_E, hwnd_F, FALSE);
2906     check_z_order(hwnd_E, hwnd_B, hwnd_D, 0, FALSE);
2907     check_z_order(hwnd_D, hwnd_E, hwnd_A, 0, FALSE);
2908     check_z_order(hwnd_A, hwnd_D, 0, 0, TRUE);
2909 #endif
2910 
2911     /* make hwnd_C owned by a topmost window */
2912     DestroyWindow( hwnd_C );
2913     hwnd_C = CreateWindowExA(0, "MainWindowClass", NULL,
2914                             style,
2915                             100, 100, 100, 100,
2916                             hwnd_A, 0, GetModuleHandleA(NULL), NULL);
2917     trace("hwnd_C %p\n", hwnd_C);
2918     check_z_order(hwnd_E, 0, hwnd_D, 0, FALSE);
2919     check_z_order(hwnd_D, hwnd_E, hwnd_F, 0, FALSE);
2920     check_z_order(hwnd_F, hwnd_D, hwnd_B, 0, FALSE);
2921     check_z_order(hwnd_B, hwnd_F, hwnd_A, hwnd_F, TRUE);
2922     check_z_order(hwnd_A, hwnd_B, hwnd_C, 0, TRUE);
2923     check_z_order(hwnd_C, hwnd_A, 0, hwnd_A, TRUE);
2924 
2925     DestroyWindow(hwnd_A);
2926     DestroyWindow(hwnd_B);
2927     DestroyWindow(hwnd_C);
2928     DestroyWindow(hwnd_F);
2929 }
2930 
test_vis_rgn(HWND hwnd)2931 static void test_vis_rgn( HWND hwnd )
2932 {
2933     RECT win_rect, rgn_rect;
2934     HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
2935     HDC hdc;
2936 
2937     ShowWindow(hwnd,SW_SHOW);
2938     hdc = GetDC( hwnd );
2939     ok( GetRandomRgn( hdc, hrgn, SYSRGN ) != 0, "GetRandomRgn failed\n" );
2940     GetWindowRect( hwnd, &win_rect );
2941     GetRgnBox( hrgn, &rgn_rect );
2942     if (is_win9x)
2943     {
2944         trace("win9x, mapping to screen coords\n");
2945         MapWindowPoints( hwnd, 0, (POINT *)&rgn_rect, 2 );
2946     }
2947     trace("win: %s\n", wine_dbgstr_rect(&win_rect));
2948     trace("rgn: %s\n", wine_dbgstr_rect(&rgn_rect));
2949     ok( win_rect.left <= rgn_rect.left, "rgn left %d not inside win rect %d\n",
2950         rgn_rect.left, win_rect.left );
2951     ok( win_rect.top <= rgn_rect.top, "rgn top %d not inside win rect %d\n",
2952         rgn_rect.top, win_rect.top );
2953     ok( win_rect.right >= rgn_rect.right, "rgn right %d not inside win rect %d\n",
2954         rgn_rect.right, win_rect.right );
2955     ok( win_rect.bottom >= rgn_rect.bottom, "rgn bottom %d not inside win rect %d\n",
2956         rgn_rect.bottom, win_rect.bottom );
2957     ReleaseDC( hwnd, hdc );
2958 }
2959 
set_focus_on_activate_proc(HWND hwnd,UINT msg,WPARAM wp,LPARAM lp)2960 static LRESULT WINAPI set_focus_on_activate_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
2961 {
2962     if (msg == WM_ACTIVATE && LOWORD(wp) == WA_ACTIVE)
2963     {
2964         HWND child = GetWindow(hwnd, GW_CHILD);
2965         ok(child != 0, "couldn't find child window\n");
2966         SetFocus(child);
2967         ok(GetFocus() == child, "Focus should be on child %p\n", child);
2968         return 0;
2969     }
2970     return DefWindowProcA(hwnd, msg, wp, lp);
2971 }
2972 
test_SetFocus(HWND hwnd)2973 static void test_SetFocus(HWND hwnd)
2974 {
2975     HWND child, child2, ret;
2976     WNDPROC old_wnd_proc;
2977 
2978     /* check if we can set focus to non-visible windows */
2979 
2980     ShowWindow(hwnd, SW_SHOW);
2981     SetFocus(0);
2982     SetFocus(hwnd);
2983     ok( GetFocus() == hwnd, "Failed to set focus to visible window %p\n", hwnd );
2984     ok( GetWindowLongA(hwnd,GWL_STYLE) & WS_VISIBLE, "Window %p not visible\n", hwnd );
2985     ShowWindow(hwnd, SW_HIDE);
2986     SetFocus(0);
2987     SetFocus(hwnd);
2988     ok( GetFocus() == hwnd, "Failed to set focus to invisible window %p\n", hwnd );
2989     ok( !(GetWindowLongA(hwnd,GWL_STYLE) & WS_VISIBLE), "Window %p still visible\n", hwnd );
2990     child = CreateWindowExA(0, "static", NULL, WS_CHILD, 0, 0, 0, 0, hwnd, 0, 0, NULL);
2991     assert(child);
2992     SetFocus(child);
2993     ok( GetFocus() == child, "Failed to set focus to invisible child %p\n", child );
2994     ok( !(GetWindowLongA(child,GWL_STYLE) & WS_VISIBLE), "Child %p is visible\n", child );
2995     ShowWindow(child, SW_SHOW);
2996     ok( GetWindowLongA(child,GWL_STYLE) & WS_VISIBLE, "Child %p is not visible\n", child );
2997     ok( GetFocus() == child, "Focus no longer on child %p\n", child );
2998     ShowWindow(child, SW_HIDE);
2999     ok( !(GetWindowLongA(child,GWL_STYLE) & WS_VISIBLE), "Child %p is visible\n", child );
3000     ok( GetFocus() == hwnd, "Focus should be on parent %p, not %p\n", hwnd, GetFocus() );
3001     ShowWindow(child, SW_SHOW);
3002     child2 = CreateWindowExA(0, "static", NULL, WS_CHILD, 0, 0, 0, 0, child, 0, 0, NULL);
3003     assert(child2);
3004     ShowWindow(child2, SW_SHOW);
3005     SetFocus(child2);
3006     ShowWindow(child, SW_HIDE);
3007     ok( !(GetWindowLongA(child,GWL_STYLE) & WS_VISIBLE), "Child %p is visible\n", child );
3008     ok( GetFocus() == child2, "Focus should be on %p, not %p\n", child2, GetFocus() );
3009     ShowWindow(child, SW_SHOW);
3010     SetFocus(child);
3011     ok( GetFocus() == child, "Focus should be on child %p\n", child );
3012     SetWindowPos(child,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_HIDEWINDOW);
3013     ok( GetFocus() == child, "Focus should still be on child %p\n", child );
3014 
3015     ShowWindow(child, SW_HIDE);
3016     SetFocus(hwnd);
3017     ok( GetFocus() == hwnd, "Focus should be on parent %p, not %p\n", hwnd, GetFocus() );
3018     SetWindowPos(child,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_SHOWWINDOW);
3019     ok( GetFocus() == hwnd, "Focus should still be on parent %p, not %p\n", hwnd, GetFocus() );
3020     ShowWindow(child, SW_HIDE);
3021     ok( GetFocus() == hwnd, "Focus should still be on parent %p, not %p\n", hwnd, GetFocus() );
3022 
3023     ShowWindow(hwnd, SW_SHOW);
3024     ShowWindow(child, SW_SHOW);
3025     SetFocus(child);
3026     ok( GetFocus() == child, "Focus should be on child %p\n", child );
3027     EnableWindow(hwnd, FALSE);
3028     ok( GetFocus() == child, "Focus should still be on child %p\n", child );
3029     EnableWindow(hwnd, TRUE);
3030 
3031     ok( GetActiveWindow() == hwnd, "parent window %p should be active\n", hwnd);
3032     ShowWindow(hwnd, SW_SHOWMINIMIZED);
3033     ok( GetActiveWindow() == hwnd, "parent window %p should be active\n", hwnd);
3034 todo_wine
3035     ok( GetFocus() != child, "Focus should not be on child %p\n", child );
3036     ok( GetFocus() != hwnd, "Focus should not be on parent %p\n", hwnd );
3037     ShowWindow(hwnd, SW_RESTORE);
3038     ok( GetActiveWindow() == hwnd, "parent window %p should be active\n", hwnd);
3039     ok( GetFocus() == hwnd, "Focus should be on parent %p\n", hwnd );
3040     ShowWindow(hwnd, SW_SHOWMINIMIZED);
3041     ok( GetActiveWindow() == hwnd, "parent window %p should be active\n", hwnd);
3042     ok( GetFocus() != child, "Focus should not be on child %p\n", child );
3043 todo_wine
3044     ok( GetFocus() != hwnd, "Focus should not be on parent %p\n", hwnd );
3045     old_wnd_proc = (WNDPROC)SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (LONG_PTR)set_focus_on_activate_proc);
3046     ShowWindow(hwnd, SW_RESTORE);
3047     ok( GetActiveWindow() == hwnd, "parent window %p should be active\n", hwnd);
3048 todo_wine
3049     ok( GetFocus() == child, "Focus should be on child %p, not %p\n", child, GetFocus() );
3050     SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (LONG_PTR)old_wnd_proc);
3051 
3052     SetFocus( hwnd );
3053     SetParent( child, GetDesktopWindow());
3054     SetParent( child2, child );
3055     ok( GetActiveWindow() == hwnd, "parent window %p should be active\n", hwnd);
3056     ok( GetFocus() == hwnd, "Focus should be on parent %p\n", hwnd );
3057     ret = SetFocus( child2 );
3058     ok( ret == 0, "SetFocus %p should fail\n", child2);
3059     ok( GetActiveWindow() == hwnd, "parent window %p should be active\n", hwnd);
3060     ok( GetFocus() == hwnd, "Focus should be on parent %p\n", hwnd );
3061     ret = SetFocus( child );
3062     ok( ret == 0, "SetFocus %p should fail\n", child);
3063     ok( GetActiveWindow() == hwnd, "parent window %p should be active\n", hwnd);
3064     ok( GetFocus() == hwnd, "Focus should be on parent %p\n", hwnd );
3065     SetWindowLongW( child, GWL_STYLE, WS_POPUP|WS_CHILD );
3066     SetFocus( child2 );
3067     ok( GetActiveWindow() == child, "child window %p should be active\n", child);
3068     ok( GetFocus() == child2, "Focus should be on child2 %p\n", child2 );
3069     SetFocus( hwnd );
3070     ok( GetActiveWindow() == hwnd, "parent window %p should be active\n", hwnd);
3071     ok( GetFocus() == hwnd, "Focus should be on parent %p\n", hwnd );
3072     SetFocus( child );
3073     ok( GetActiveWindow() == child, "child window %p should be active\n", child);
3074     ok( GetFocus() == child, "Focus should be on child %p\n", child );
3075 
3076     DestroyWindow( child2 );
3077     DestroyWindow( child );
3078 }
3079 
test_SetActiveWindow(HWND hwnd)3080 static void test_SetActiveWindow(HWND hwnd)
3081 {
3082     HWND hwnd2;
3083 
3084     flush_events( TRUE );
3085     ShowWindow(hwnd, SW_HIDE);
3086     SetFocus(0);
3087     SetActiveWindow(0);
3088     check_wnd_state(0, 0, 0, 0);
3089 
3090     /*trace("testing SetActiveWindow %p\n", hwnd);*/
3091 
3092     ShowWindow(hwnd, SW_SHOW);
3093     check_wnd_state(hwnd, hwnd, hwnd, 0);
3094 
3095     hwnd2 = SetActiveWindow(0);
3096     ok(hwnd2 == hwnd, "SetActiveWindow returned %p instead of %p\n", hwnd2, hwnd);
3097     if (!GetActiveWindow())  /* doesn't always work on vista */
3098     {
3099         ros_skip_flaky
3100         check_wnd_state(0, 0, 0, 0);
3101         hwnd2 = SetActiveWindow(hwnd);
3102         ros_skip_flaky
3103         ok(hwnd2 == 0, "SetActiveWindow returned %p instead of 0\n", hwnd2);
3104     }
3105     check_wnd_state(hwnd, hwnd, hwnd, 0);
3106 
3107     SetWindowPos(hwnd,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_HIDEWINDOW);
3108     check_wnd_state(hwnd, hwnd, hwnd, 0);
3109 
3110     SetWindowPos(hwnd,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_SHOWWINDOW);
3111     check_wnd_state(hwnd, hwnd, hwnd, 0);
3112 
3113     ShowWindow(hwnd, SW_HIDE);
3114     check_wnd_state(0, 0, 0, 0);
3115 
3116     /*trace("testing SetActiveWindow on an invisible window %p\n", hwnd);*/
3117     SetActiveWindow(hwnd);
3118     check_wnd_state(hwnd, hwnd, hwnd, 0);
3119 
3120     ShowWindow(hwnd, SW_SHOW);
3121     check_wnd_state(hwnd, hwnd, hwnd, 0);
3122 
3123     hwnd2 = CreateWindowExA(0, "static", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 0, 0, hwnd, 0, 0, NULL);
3124     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
3125 
3126     DestroyWindow(hwnd2);
3127     check_wnd_state(hwnd, hwnd, hwnd, 0);
3128 
3129     hwnd2 = CreateWindowExA(0, "static", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 0, 0, hwnd, 0, 0, NULL);
3130     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
3131 
3132     SetWindowPos(hwnd2,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_HIDEWINDOW);
3133     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
3134 
3135     DestroyWindow(hwnd2);
3136     check_wnd_state(hwnd, hwnd, hwnd, 0);
3137 }
3138 
3139 struct create_window_thread_params
3140 {
3141     HWND window;
3142     HANDLE window_created;
3143     HANDLE test_finished;
3144 };
3145 
create_window_thread(void * param)3146 static DWORD WINAPI create_window_thread(void *param)
3147 {
3148     struct create_window_thread_params *p = param;
3149     DWORD res;
3150     BOOL ret;
3151 
3152     p->window = CreateWindowA("static", NULL, WS_POPUP | WS_VISIBLE, 0, 0, 0, 0, 0, 0, 0, 0);
3153 
3154     ret = SetEvent(p->window_created);
3155     ok(ret, "SetEvent failed, last error %#x.\n", GetLastError());
3156 
3157     res = WaitForSingleObject(p->test_finished, INFINITE);
3158     ok(res == WAIT_OBJECT_0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
3159 
3160     DestroyWindow(p->window);
3161     return 0;
3162 }
3163 
test_SetForegroundWindow(HWND hwnd)3164 static void test_SetForegroundWindow(HWND hwnd)
3165 {
3166     struct create_window_thread_params thread_params;
3167     HANDLE thread;
3168     DWORD res, tid;
3169     BOOL ret;
3170     HWND hwnd2;
3171     MSG msg;
3172     LONG style;
3173 
3174     flush_events( TRUE );
3175     ShowWindow(hwnd, SW_HIDE);
3176     SetFocus(0);
3177     SetActiveWindow(0);
3178     check_wnd_state(0, 0, 0, 0);
3179 
3180     /*trace("testing SetForegroundWindow %p\n", hwnd);*/
3181 
3182     ShowWindow(hwnd, SW_SHOW);
3183     check_wnd_state(hwnd, hwnd, hwnd, 0);
3184 
3185     hwnd2 = SetActiveWindow(0);
3186     ok(hwnd2 == hwnd, "SetActiveWindow(0) returned %p instead of %p\n", hwnd2, hwnd);
3187     if (GetActiveWindow() == hwnd)  /* doesn't always work on vista */
3188         check_wnd_state(hwnd, hwnd, hwnd, 0);
3189     else
3190         check_wnd_state(0, 0, 0, 0);
3191 
3192     ret = SetForegroundWindow(hwnd);
3193     if (!ret)
3194     {
3195         skip( "SetForegroundWindow not working\n" );
3196         return;
3197     }
3198     check_wnd_state(hwnd, hwnd, hwnd, 0);
3199 
3200     SetLastError(0xdeadbeef);
3201     ret = SetForegroundWindow(0);
3202     ok(!ret, "SetForegroundWindow returned TRUE instead of FALSE\n");
3203     ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE ||
3204        broken(GetLastError() == 0xdeadbeef),  /* win9x */
3205        "got error %d expected ERROR_INVALID_WINDOW_HANDLE\n", GetLastError());
3206     check_wnd_state(hwnd, hwnd, hwnd, 0);
3207 
3208     SetWindowPos(hwnd,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_HIDEWINDOW);
3209     check_wnd_state(hwnd, hwnd, hwnd, 0);
3210 
3211     SetWindowPos(hwnd,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_SHOWWINDOW);
3212     check_wnd_state(hwnd, hwnd, hwnd, 0);
3213 
3214     hwnd2 = GetForegroundWindow();
3215     ok(hwnd2 == hwnd, "Wrong foreground window %p\n", hwnd2);
3216     ret = SetForegroundWindow( GetDesktopWindow() );
3217     ok(ret, "SetForegroundWindow(desktop) error: %d\n", GetLastError());
3218     hwnd2 = GetForegroundWindow();
3219     ok(hwnd2 != hwnd, "Wrong foreground window %p\n", hwnd2);
3220 
3221     ShowWindow(hwnd, SW_HIDE);
3222     check_wnd_state(0, 0, 0, 0);
3223 
3224     /*trace("testing SetForegroundWindow on an invisible window %p\n", hwnd);*/
3225     ret = SetForegroundWindow(hwnd);
3226     ok(ret || broken(!ret), /* win98 */ "SetForegroundWindow returned FALSE instead of TRUE\n");
3227     check_wnd_state(hwnd, hwnd, hwnd, 0);
3228 
3229     ShowWindow(hwnd, SW_SHOW);
3230     check_wnd_state(hwnd, hwnd, hwnd, 0);
3231 
3232     hwnd2 = CreateWindowExA(0, "static", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 0, 0, hwnd, 0, 0, NULL);
3233     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
3234 
3235     DestroyWindow(hwnd2);
3236     check_wnd_state(hwnd, hwnd, hwnd, 0);
3237 
3238     hwnd2 = CreateWindowExA(0, "static", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 0, 0, hwnd, 0, 0, NULL);
3239     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
3240 
3241     SetWindowPos(hwnd2,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_HIDEWINDOW);
3242     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
3243 
3244     DestroyWindow(hwnd2);
3245     check_wnd_state(hwnd, hwnd, hwnd, 0);
3246 
3247     hwnd2 = CreateWindowA("static", NULL, WS_POPUP | WS_VISIBLE, 0, 0, 0, 0, 0, 0, 0, 0);
3248     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
3249 
3250     thread_params.window_created = CreateEventW(NULL, FALSE, FALSE, NULL);
3251     ok(!!thread_params.window_created, "CreateEvent failed, last error %#x.\n", GetLastError());
3252     thread_params.test_finished = CreateEventW(NULL, FALSE, FALSE, NULL);
3253     ok(!!thread_params.test_finished, "CreateEvent failed, last error %#x.\n", GetLastError());
3254     thread = CreateThread(NULL, 0, create_window_thread, &thread_params, 0, &tid);
3255     ok(!!thread, "Failed to create thread, last error %#x.\n", GetLastError());
3256     res = WaitForSingleObject(thread_params.window_created, INFINITE);
3257     ok(res == WAIT_OBJECT_0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
3258     check_wnd_state(hwnd2, thread_params.window, hwnd2, 0);
3259 
3260     SetForegroundWindow(hwnd2);
3261     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
3262 
3263     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
3264     if (0) check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
3265     todo_wine ok(GetActiveWindow() == hwnd2, "Expected active window %p, got %p.\n", hwnd2, GetActiveWindow());
3266     todo_wine ok(GetFocus() == hwnd2, "Expected focus window %p, got %p.\n", hwnd2, GetFocus());
3267 
3268     SetForegroundWindow(hwnd);
3269     check_wnd_state(hwnd, hwnd, hwnd, 0);
3270     style = GetWindowLongA(hwnd2, GWL_STYLE) | WS_CHILD;
3271     ok(SetWindowLongA(hwnd2, GWL_STYLE, style), "SetWindowLong failed\n");
3272     ok(SetForegroundWindow(hwnd2), "SetForegroundWindow failed\n");
3273     check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
3274 
3275     SetForegroundWindow(hwnd);
3276     check_wnd_state(hwnd, hwnd, hwnd, 0);
3277     ok(SetWindowLongA(hwnd2, GWL_STYLE, style & (~WS_POPUP)), "SetWindowLong failed\n");
3278     ok(!SetForegroundWindow(hwnd2), "SetForegroundWindow failed\n");
3279     check_wnd_state(hwnd, hwnd, hwnd, 0);
3280 
3281     SetEvent(thread_params.test_finished);
3282     WaitForSingleObject(thread, INFINITE);
3283     CloseHandle(thread_params.test_finished);
3284     CloseHandle(thread_params.window_created);
3285     CloseHandle(thread);
3286     DestroyWindow(hwnd2);
3287 }
3288 
3289 static WNDPROC old_button_proc;
3290 
button_hook_proc(HWND button,UINT msg,WPARAM wparam,LPARAM lparam)3291 static LRESULT WINAPI button_hook_proc(HWND button, UINT msg, WPARAM wparam, LPARAM lparam)
3292 {
3293     LRESULT ret;
3294     USHORT key_state;
3295 
3296     key_state = GetKeyState(VK_LBUTTON);
3297     ok(!(key_state & 0x8000), "VK_LBUTTON should not be pressed, state %04x\n", key_state);
3298 
3299     ret = CallWindowProcA(old_button_proc, button, msg, wparam, lparam);
3300 
3301     if (msg == WM_LBUTTONDOWN)
3302     {
3303 	HWND hwnd, capture;
3304 
3305 	check_wnd_state(button, button, button, button);
3306 
3307 	hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0, 0, 10, 10, 0, 0, 0, NULL);
3308 	assert(hwnd);
3309 	trace("hwnd %p\n", hwnd);
3310 
3311 	check_wnd_state(button, button, button, button);
3312 
3313 	ShowWindow(hwnd, SW_SHOWNOACTIVATE);
3314 
3315 	check_wnd_state(button, button, button, button);
3316 
3317 	DestroyWindow(hwnd);
3318 
3319 	hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0, 0, 10, 10, 0, 0, 0, NULL);
3320 	assert(hwnd);
3321 	trace("hwnd %p\n", hwnd);
3322 
3323 	check_wnd_state(button, button, button, button);
3324 
3325 	/* button wnd proc should release capture on WM_KILLFOCUS if it does
3326 	 * match internal button state.
3327 	 */
3328 	SendMessageA(button, WM_KILLFOCUS, 0, 0);
3329 	check_wnd_state(button, button, button, 0);
3330 
3331 	ShowWindow(hwnd, SW_SHOW);
3332 	check_wnd_state(hwnd, hwnd, hwnd, 0);
3333 
3334 	capture = SetCapture(hwnd);
3335 	ok(capture == 0, "SetCapture() = %p\n", capture);
3336 
3337 	check_wnd_state(hwnd, hwnd, hwnd, hwnd);
3338 
3339 	DestroyWindow(hwnd);
3340 
3341 	check_wnd_state(button, 0, button, 0);
3342     }
3343 
3344     return ret;
3345 }
3346 
test_capture_1(void)3347 static void test_capture_1(void)
3348 {
3349     HWND button, capture;
3350 
3351     capture = GetCapture();
3352     ok(capture == 0, "GetCapture() = %p\n", capture);
3353 
3354     button = CreateWindowExA(0, "button", NULL, WS_POPUP | WS_VISIBLE, 0, 0, 10, 10, 0, 0, 0, NULL);
3355     assert(button);
3356     trace("button %p\n", button);
3357 
3358     old_button_proc = (WNDPROC)SetWindowLongPtrA(button, GWLP_WNDPROC, (LONG_PTR)button_hook_proc);
3359 
3360     SendMessageA(button, WM_LBUTTONDOWN, 0, 0);
3361 
3362     capture = SetCapture(button);
3363     ok(capture == 0, "SetCapture() = %p\n", capture);
3364     check_wnd_state(button, 0, button, button);
3365 
3366     DestroyWindow(button);
3367     /* old active window test depends on previously executed window
3368      * activation tests, and fails under NT4.
3369     check_wnd_state(oldActive, 0, oldFocus, 0);*/
3370 }
3371 
test_capture_2(void)3372 static void test_capture_2(void)
3373 {
3374     HWND button, hwnd, capture, oldFocus, oldActive;
3375 
3376     oldFocus = GetFocus();
3377     oldActive = GetActiveWindow();
3378     check_wnd_state(oldActive, 0, oldFocus, 0);
3379 
3380     button = CreateWindowExA(0, "button", NULL, WS_POPUP | WS_VISIBLE, 0, 0, 10, 10, 0, 0, 0, NULL);
3381     assert(button);
3382     trace("button %p\n", button);
3383 
3384     check_wnd_state(button, button, button, 0);
3385 
3386     capture = SetCapture(button);
3387     ok(capture == 0, "SetCapture() = %p\n", capture);
3388 
3389     check_wnd_state(button, button, button, button);
3390 
3391     /* button wnd proc should ignore WM_KILLFOCUS if it doesn't match
3392      * internal button state.
3393      */
3394     SendMessageA(button, WM_KILLFOCUS, 0, 0);
3395     check_wnd_state(button, button, button, button);
3396 
3397     hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0, 0, 10, 10, 0, 0, 0, NULL);
3398     assert(hwnd);
3399     trace("hwnd %p\n", hwnd);
3400 
3401     check_wnd_state(button, button, button, button);
3402 
3403     ShowWindow(hwnd, SW_SHOWNOACTIVATE);
3404 
3405     check_wnd_state(button, button, button, button);
3406 
3407     DestroyWindow(hwnd);
3408 
3409     hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0, 0, 10, 10, 0, 0, 0, NULL);
3410     assert(hwnd);
3411     trace("hwnd %p\n", hwnd);
3412 
3413     check_wnd_state(button, button, button, button);
3414 
3415     ShowWindow(hwnd, SW_SHOW);
3416 
3417     check_wnd_state(hwnd, hwnd, hwnd, button);
3418 
3419     capture = SetCapture(hwnd);
3420     ok(capture == button, "SetCapture() = %p\n", capture);
3421 
3422     check_wnd_state(hwnd, hwnd, hwnd, hwnd);
3423 
3424     DestroyWindow(hwnd);
3425     check_wnd_state(button, button, button, 0);
3426 
3427     DestroyWindow(button);
3428     check_wnd_state(oldActive, 0, oldFocus, 0);
3429 }
3430 
test_capture_3(HWND hwnd1,HWND hwnd2)3431 static void test_capture_3(HWND hwnd1, HWND hwnd2)
3432 {
3433     BOOL ret;
3434 
3435     ShowWindow(hwnd1, SW_HIDE);
3436     ShowWindow(hwnd2, SW_HIDE);
3437 
3438     ok(!IsWindowVisible(hwnd1), "%p should be invisible\n", hwnd1);
3439     ok(!IsWindowVisible(hwnd2), "%p should be invisible\n", hwnd2);
3440 
3441     SetCapture(hwnd1);
3442     check_wnd_state(0, 0, 0, hwnd1);
3443 
3444     SetCapture(hwnd2);
3445     check_wnd_state(0, 0, 0, hwnd2);
3446 
3447     ShowWindow(hwnd1, SW_SHOW);
3448     check_wnd_state(hwnd1, hwnd1, hwnd1, hwnd2);
3449 
3450     ret = ReleaseCapture();
3451     ok (ret, "releasecapture did not return TRUE.\n");
3452     ret = ReleaseCapture();
3453     ok (ret, "releasecapture did not return TRUE after second try.\n");
3454 }
3455 
test_capture_4_proc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)3456 static LRESULT CALLBACK test_capture_4_proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
3457 {
3458     GUITHREADINFO gti;
3459     HWND cap_wnd, cap_wnd2, set_cap_wnd;
3460     BOOL status;
3461     switch (msg)
3462     {
3463         case WM_CAPTURECHANGED:
3464 
3465             /* now try to release capture from menu. this should fail */
3466             if (pGetGUIThreadInfo)
3467             {
3468                 memset(&gti, 0, sizeof(GUITHREADINFO));
3469                 gti.cbSize = sizeof(GUITHREADINFO);
3470                 status = pGetGUIThreadInfo(GetCurrentThreadId(), &gti);
3471                 ok(status, "GetGUIThreadInfo() failed!\n");
3472                 ok(gti.flags & GUI_INMENUMODE, "Thread info incorrect (flags=%08X)!\n", gti.flags);
3473             }
3474             cap_wnd = GetCapture();
3475 
3476             ok(cap_wnd == (HWND)lParam, "capture window %p does not match lparam %lx\n", cap_wnd, lParam);
3477             todo_wine ok(cap_wnd == hWnd, "capture window %p does not match hwnd %p\n", cap_wnd, hWnd);
3478 
3479             /* check that re-setting the capture for the menu fails */
3480             set_cap_wnd = SetCapture(cap_wnd);
3481             ok(!set_cap_wnd || broken(set_cap_wnd == cap_wnd), /* nt4 */
3482                "SetCapture should have failed!\n");
3483             if (set_cap_wnd)
3484             {
3485                 DestroyWindow(hWnd);
3486                 break;
3487             }
3488 
3489             /* check that SetCapture fails for another window and that it does not touch the error code */
3490             set_cap_wnd = SetCapture(hWnd);
3491             ok(!set_cap_wnd, "SetCapture should have failed!\n");
3492 
3493             /* check that ReleaseCapture fails and does not touch the error code */
3494             status = ReleaseCapture();
3495             ok(!status, "ReleaseCapture should have failed!\n");
3496 
3497             /* check that thread info did not change */
3498             if (pGetGUIThreadInfo)
3499             {
3500                 memset(&gti, 0, sizeof(GUITHREADINFO));
3501                 gti.cbSize = sizeof(GUITHREADINFO);
3502                 status = pGetGUIThreadInfo(GetCurrentThreadId(), &gti);
3503                 ok(status, "GetGUIThreadInfo() failed!\n");
3504                 ok(gti.flags & GUI_INMENUMODE, "Thread info incorrect (flags=%08X)!\n", gti.flags);
3505             }
3506 
3507             /* verify that no capture change took place */
3508             cap_wnd2 = GetCapture();
3509             ok(cap_wnd2 == cap_wnd, "Capture changed!\n");
3510 
3511             /* we are done. kill the window */
3512             DestroyWindow(hWnd);
3513             break;
3514 
3515         default:
3516             return( DefWindowProcA( hWnd, msg, wParam, lParam ) );
3517     }
3518     return 0;
3519 }
3520 
3521 /* Test that no-one can mess around with the current capture while a menu is open */
test_capture_4(void)3522 static void test_capture_4(void)
3523 {
3524     BOOL ret;
3525     HMENU hmenu;
3526     HWND hwnd;
3527     WNDCLASSA wclass;
3528     HINSTANCE hInstance = GetModuleHandleA( NULL );
3529     ATOM aclass;
3530 
3531     if (!pGetGUIThreadInfo)
3532     {
3533         win_skip("GetGUIThreadInfo is not available\n");
3534         return;
3535     }
3536     wclass.lpszClassName = "TestCapture4Class";
3537     wclass.style         = CS_HREDRAW | CS_VREDRAW;
3538     wclass.lpfnWndProc   = test_capture_4_proc;
3539     wclass.hInstance     = hInstance;
3540     wclass.hIcon         = LoadIconA( 0, (LPCSTR)IDI_APPLICATION );
3541     wclass.hCursor       = LoadCursorA( 0, (LPCSTR)IDC_ARROW );
3542     wclass.hbrBackground = (HBRUSH)( COLOR_WINDOW + 1 );
3543     wclass.lpszMenuName  = 0;
3544     wclass.cbClsExtra    = 0;
3545     wclass.cbWndExtra    = 0;
3546     aclass = RegisterClassA( &wclass );
3547     ok( aclass, "RegisterClassA failed with error %d\n", GetLastError());
3548     hwnd = CreateWindowA( wclass.lpszClassName, "MenuTest",
3549                           WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0,
3550                           400, 200, NULL, NULL, hInstance, NULL);
3551     ok(hwnd != NULL, "CreateWindowEx failed with error %d\n", GetLastError());
3552     if (!hwnd) return;
3553     hmenu = CreatePopupMenu();
3554 
3555     ret = AppendMenuA( hmenu, MF_STRING, 1, "winetest2");
3556     ok( ret, "AppendMenuA has failed!\n");
3557 
3558     /* set main window to have initial capture */
3559     SetCapture(hwnd);
3560 
3561     if (is_win9x)
3562     {
3563         win_skip("TrackPopupMenu test crashes on Win9x/WinMe\n");
3564     }
3565     else
3566     {
3567         /* create popup (it will self-destruct) */
3568         ret = TrackPopupMenu(hmenu, TPM_RETURNCMD, 100, 100, 0, hwnd, NULL);
3569         ok( ret == 0, "TrackPopupMenu returned %d expected zero\n", ret);
3570     }
3571 
3572     /* clean up */
3573     DestroyMenu(hmenu);
3574     DestroyWindow(hwnd);
3575 }
3576 
3577 /* PeekMessage wrapper that ignores the messages we don't care about */
peek_message(MSG * msg)3578 static BOOL peek_message( MSG *msg )
3579 {
3580     BOOL ret;
3581     do
3582     {
3583         ret = PeekMessageA(msg, 0, 0, 0, PM_REMOVE);
3584     } while (ret && ignore_message(msg->message));
3585     return ret;
3586 }
3587 
test_keyboard_input(HWND hwnd)3588 static void test_keyboard_input(HWND hwnd)
3589 {
3590     MSG msg;
3591     BOOL ret;
3592 
3593     flush_events( TRUE );
3594     SetWindowPos(hwnd, 0, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE|SWP_SHOWWINDOW);
3595     UpdateWindow(hwnd);
3596     flush_events( TRUE );
3597 
3598     ok(GetActiveWindow() == hwnd, "wrong active window %p\n", GetActiveWindow());
3599 
3600     SetFocus(hwnd);
3601     ok(GetFocus() == hwnd, "wrong focus window %p\n", GetFocus());
3602 
3603     flush_events( TRUE );
3604 
3605     PostMessageA(hwnd, WM_KEYDOWN, 0, 0);
3606     ret = peek_message(&msg);
3607     ok( ret, "no message available\n");
3608     ok(msg.hwnd == hwnd && msg.message == WM_KEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
3609     ret = peek_message(&msg);
3610     ok( !ret, "message %04x available\n", msg.message);
3611 
3612     ok(GetFocus() == hwnd, "wrong focus window %p\n", GetFocus());
3613 
3614     PostThreadMessageA(GetCurrentThreadId(), WM_KEYDOWN, 0, 0);
3615     ret = peek_message(&msg);
3616     ok(ret, "no message available\n");
3617     ok(!msg.hwnd && msg.message == WM_KEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
3618     ret = peek_message(&msg);
3619     ok( !ret, "message %04x available\n", msg.message);
3620 
3621     ok(GetFocus() == hwnd, "wrong focus window %p\n", GetFocus());
3622 
3623     keybd_event(VK_SPACE, 0, 0, 0);
3624     if (!peek_message(&msg))
3625     {
3626         skip( "keybd_event didn't work, skipping keyboard test\n" );
3627         return;
3628     }
3629     ok(msg.hwnd == hwnd && msg.message == WM_KEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
3630     ret = peek_message(&msg);
3631     ok( !ret, "message %04x available\n", msg.message);
3632 
3633     SetFocus(0);
3634     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3635 
3636     flush_events( TRUE );
3637 
3638     PostMessageA(hwnd, WM_KEYDOWN, 0, 0);
3639     ret = peek_message(&msg);
3640     ok(ret, "no message available\n");
3641     ok(msg.hwnd == hwnd && msg.message == WM_KEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
3642     ret = peek_message(&msg);
3643     ok( !ret, "message %04x available\n", msg.message);
3644 
3645     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3646 
3647     PostThreadMessageA(GetCurrentThreadId(), WM_KEYDOWN, 0, 0);
3648     ret = peek_message(&msg);
3649     ok(ret, "no message available\n");
3650     ok(!msg.hwnd && msg.message == WM_KEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
3651     ret = peek_message(&msg);
3652     ok( !ret, "message %04x available\n", msg.message);
3653 
3654     ok(GetFocus() == 0, "wrong focus window %p\n", GetFocus());
3655 
3656     keybd_event(VK_SPACE, 0, 0, 0);
3657     ret = peek_message(&msg);
3658     ok(ret, "no message available\n");
3659     ok(msg.hwnd == hwnd && msg.message == WM_SYSKEYDOWN, "hwnd %p message %04x\n", msg.hwnd, msg.message);
3660     ret = peek_message(&msg);
3661     ok( !ret, "message %04x available\n", msg.message);
3662 }
3663 
wait_for_message(MSG * msg)3664 static BOOL wait_for_message( MSG *msg )
3665 {
3666     BOOL ret;
3667 
3668     for (;;)
3669     {
3670         ret = peek_message(msg);
3671         if (ret)
3672         {
3673             if (msg->message == WM_PAINT) DispatchMessageA(msg);
3674             else break;
3675         }
3676         else if (MsgWaitForMultipleObjects( 0, NULL, FALSE, 100, QS_ALLINPUT ) == WAIT_TIMEOUT) break;
3677     }
3678     if (!ret) msg->message = 0;
3679     return ret;
3680 }
3681 
test_mouse_input(HWND hwnd)3682 static void test_mouse_input(HWND hwnd)
3683 {
3684     RECT rc;
3685     POINT pt;
3686     int x, y;
3687     HWND popup;
3688     MSG msg;
3689     BOOL ret;
3690     LRESULT res;
3691 
3692     ShowWindow(hwnd, SW_SHOWNORMAL);
3693     UpdateWindow(hwnd);
3694     SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE );
3695 
3696     GetWindowRect(hwnd, &rc);
3697     trace("main window %p: %s\n", hwnd, wine_dbgstr_rect(&rc));
3698 
3699     popup = CreateWindowExA(0, "MainWindowClass", NULL, WS_POPUP,
3700                             rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top,
3701                             hwnd, 0, 0, NULL);
3702     assert(popup != 0);
3703     ShowWindow(popup, SW_SHOW);
3704     UpdateWindow(popup);
3705     SetWindowPos( popup, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE );
3706 
3707     GetWindowRect(popup, &rc);
3708     trace("popup window %p: %s\n", popup, wine_dbgstr_rect(&rc));
3709 
3710     x = rc.left + (rc.right - rc.left) / 2;
3711     y = rc.top + (rc.bottom - rc.top) / 2;
3712     trace("setting cursor to (%d,%d)\n", x, y);
3713 
3714     SetCursorPos(x, y);
3715     GetCursorPos(&pt);
3716     if (x != pt.x || y != pt.y)
3717     {
3718         skip( "failed to set mouse position, skipping mouse input tests\n" );
3719         goto done;
3720     }
3721 
3722     flush_events( TRUE );
3723 
3724     /* Check that setting the same position may generate WM_MOUSEMOVE */
3725     SetCursorPos(x, y);
3726     msg.message = 0;
3727     ret = peek_message(&msg);
3728     if (ret)
3729     {
3730         ok(msg.hwnd == popup && msg.message == WM_MOUSEMOVE, "hwnd %p message %04x\n",
3731            msg.hwnd, msg.message);
3732         ok(msg.pt.x == x && msg.pt.y == y, "wrong message coords (%d,%d)/(%d,%d)\n",
3733            x, y, msg.pt.x, msg.pt.y);
3734     }
3735 
3736     /* force the system to update its internal queue mouse position,
3737      * otherwise it won't generate relative mouse movements below.
3738      */
3739     mouse_event(MOUSEEVENTF_MOVE, -1, -1, 0, 0);
3740     flush_events( TRUE );
3741 
3742     msg.message = 0;
3743     mouse_event(MOUSEEVENTF_MOVE, 1, 1, 0, 0);
3744     flush_events( FALSE );
3745     /* FIXME: SetCursorPos in Wine generates additional WM_MOUSEMOVE message */
3746     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE))
3747     {
3748         if (ignore_message(msg.message)) continue;
3749         ok(msg.hwnd == popup && msg.message == WM_MOUSEMOVE,
3750            "hwnd %p message %04x\n", msg.hwnd, msg.message);
3751         DispatchMessageA(&msg);
3752     }
3753     ret = peek_message(&msg);
3754     ok( !ret, "message %04x available\n", msg.message);
3755 
3756     mouse_event(MOUSEEVENTF_MOVE, -1, -1, 0, 0);
3757     ShowWindow(popup, SW_HIDE);
3758     ret = wait_for_message( &msg );
3759     if (ret)
3760         ok(msg.hwnd == hwnd && msg.message == WM_MOUSEMOVE, "hwnd %p message %04x\n", msg.hwnd, msg.message);
3761     flush_events( TRUE );
3762 
3763     mouse_event(MOUSEEVENTF_MOVE, 1, 1, 0, 0);
3764     ShowWindow(hwnd, SW_HIDE);
3765     ret = wait_for_message( &msg );
3766     ok( !ret, "message %04x available\n", msg.message);
3767     flush_events( TRUE );
3768 
3769     /* test mouse clicks */
3770 
3771     ShowWindow(hwnd, SW_SHOW);
3772     SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE );
3773     flush_events( TRUE );
3774     ShowWindow(popup, SW_SHOW);
3775     SetWindowPos( popup, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE );
3776     flush_events( TRUE );
3777 
3778     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
3779     mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
3780     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
3781     mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
3782 
3783     ret = wait_for_message( &msg );
3784     if (!ret)
3785     {
3786         skip( "simulating mouse click doesn't work, skipping mouse button tests\n" );
3787         goto done;
3788     }
3789     if (msg.message == WM_MOUSEMOVE)  /* win2k has an extra WM_MOUSEMOVE here */
3790     {
3791         ret = wait_for_message( &msg );
3792         ok(ret, "no message available\n");
3793     }
3794 
3795     ok(msg.hwnd == popup && msg.message == WM_LBUTTONDOWN, "hwnd %p/%p message %04x\n",
3796        msg.hwnd, popup, msg.message);
3797 
3798     ret = wait_for_message( &msg );
3799     ok(ret, "no message available\n");
3800     ok(msg.hwnd == popup && msg.message == WM_LBUTTONUP, "hwnd %p/%p message %04x\n",
3801        msg.hwnd, popup, msg.message);
3802 
3803     ret = wait_for_message( &msg );
3804     ok(ret, "no message available\n");
3805     ok(msg.hwnd == popup && msg.message == WM_LBUTTONDBLCLK, "hwnd %p/%p message %04x\n",
3806        msg.hwnd, popup, msg.message);
3807 
3808     ret = wait_for_message( &msg );
3809     ok(ret, "no message available\n");
3810     ok(msg.hwnd == popup && msg.message == WM_LBUTTONUP, "hwnd %p/%p message %04x\n",
3811        msg.hwnd, popup, msg.message);
3812 
3813     ret = peek_message(&msg);
3814     ok(!ret, "message %04x available\n", msg.message);
3815 
3816     ShowWindow(popup, SW_HIDE);
3817     flush_events( TRUE );
3818 
3819     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
3820     mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
3821     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
3822     mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
3823 
3824     ret = wait_for_message( &msg );
3825     ok(ret, "no message available\n");
3826     ok(msg.hwnd == hwnd && msg.message == WM_LBUTTONDOWN, "hwnd %p/%p message %04x\n",
3827        msg.hwnd, hwnd, msg.message);
3828     ret = wait_for_message( &msg );
3829     ok(ret, "no message available\n");
3830     ok(msg.hwnd == hwnd && msg.message == WM_LBUTTONUP, "hwnd %p/%p message %04x\n",
3831        msg.hwnd, hwnd, msg.message);
3832 
3833     test_lbuttondown_flag = TRUE;
3834     SendMessageA(hwnd, WM_COMMAND, (WPARAM)popup, 0);
3835     test_lbuttondown_flag = FALSE;
3836 
3837     ret = wait_for_message( &msg );
3838     ok(ret, "no message available\n");
3839     ok(msg.hwnd == popup && msg.message == WM_LBUTTONDOWN, "hwnd %p/%p message %04x\n",
3840        msg.hwnd, popup, msg.message);
3841     ok(peek_message(&msg), "no message available\n");
3842     ok(msg.hwnd == popup && msg.message == WM_LBUTTONUP, "hwnd %p/%p message %04x\n",
3843        msg.hwnd, popup, msg.message);
3844     ok(peek_message(&msg), "no message available\n");
3845 
3846     /* Test WM_MOUSEACTIVATE */
3847 #define TEST_MOUSEACTIVATE(A,B)                                                          \
3848        res = SendMessageA(hwnd, WM_MOUSEACTIVATE, (WPARAM)hwnd, (LPARAM)MAKELRESULT(A,0));   \
3849        ok(res == B, "WM_MOUSEACTIVATE for %s returned %ld\n", #A, res);
3850 
3851     TEST_MOUSEACTIVATE(HTERROR,MA_ACTIVATE);
3852     TEST_MOUSEACTIVATE(HTTRANSPARENT,MA_ACTIVATE);
3853     TEST_MOUSEACTIVATE(HTNOWHERE,MA_ACTIVATE);
3854     TEST_MOUSEACTIVATE(HTCLIENT,MA_ACTIVATE);
3855     TEST_MOUSEACTIVATE(HTCAPTION,MA_ACTIVATE);
3856     TEST_MOUSEACTIVATE(HTSYSMENU,MA_ACTIVATE);
3857     TEST_MOUSEACTIVATE(HTSIZE,MA_ACTIVATE);
3858     TEST_MOUSEACTIVATE(HTMENU,MA_ACTIVATE);
3859     TEST_MOUSEACTIVATE(HTHSCROLL,MA_ACTIVATE);
3860     TEST_MOUSEACTIVATE(HTVSCROLL,MA_ACTIVATE);
3861     TEST_MOUSEACTIVATE(HTMINBUTTON,MA_ACTIVATE);
3862     TEST_MOUSEACTIVATE(HTMAXBUTTON,MA_ACTIVATE);
3863     TEST_MOUSEACTIVATE(HTLEFT,MA_ACTIVATE);
3864     TEST_MOUSEACTIVATE(HTRIGHT,MA_ACTIVATE);
3865     TEST_MOUSEACTIVATE(HTTOP,MA_ACTIVATE);
3866     TEST_MOUSEACTIVATE(HTTOPLEFT,MA_ACTIVATE);
3867     TEST_MOUSEACTIVATE(HTTOPRIGHT,MA_ACTIVATE);
3868     TEST_MOUSEACTIVATE(HTBOTTOM,MA_ACTIVATE);
3869     TEST_MOUSEACTIVATE(HTBOTTOMLEFT,MA_ACTIVATE);
3870     TEST_MOUSEACTIVATE(HTBOTTOMRIGHT,MA_ACTIVATE);
3871     TEST_MOUSEACTIVATE(HTBORDER,MA_ACTIVATE);
3872     TEST_MOUSEACTIVATE(HTOBJECT,MA_ACTIVATE);
3873     TEST_MOUSEACTIVATE(HTCLOSE,MA_ACTIVATE);
3874     TEST_MOUSEACTIVATE(HTHELP,MA_ACTIVATE);
3875 
3876 done:
3877     /* Clear any messages left behind by WM_MOUSEACTIVATE tests */
3878     flush_events( TRUE );
3879 
3880     DestroyWindow(popup);
3881 }
3882 
test_validatergn(HWND hwnd)3883 static void test_validatergn(HWND hwnd)
3884 {
3885     HWND child;
3886     RECT rc, rc2;
3887     HRGN rgn;
3888     int ret;
3889     child = CreateWindowExA(0, "static", NULL, WS_CHILD| WS_VISIBLE, 10, 10, 10, 10, hwnd, 0, 0, NULL);
3890     ShowWindow(hwnd, SW_SHOW);
3891     UpdateWindow( hwnd);
3892     /* test that ValidateRect validates children*/
3893     InvalidateRect( child, NULL, 1);
3894     GetWindowRect( child, &rc);
3895     MapWindowPoints( NULL, hwnd, (POINT*) &rc, 2);
3896     ret = GetUpdateRect( child, &rc2, 0);
3897     ok( ret == 1, "Expected GetUpdateRect to return non-zero, got %d\n", ret);
3898     ok( rc2.right > rc2.left && rc2.bottom > rc2.top,
3899             "Update rectangle is empty!\n");
3900     ValidateRect( hwnd, &rc);
3901     ret = GetUpdateRect( child, &rc2, 0);
3902     ok( !ret, "Expected GetUpdateRect to return zero, got %d\n", ret);
3903     ok( rc2.left == 0 && rc2.top == 0 && rc2.right == 0 && rc2.bottom == 0,
3904             "Update rectangle %s is not empty!\n", wine_dbgstr_rect(&rc2));
3905 
3906     /* now test ValidateRgn */
3907     InvalidateRect( child, NULL, 1);
3908     GetWindowRect( child, &rc);
3909     MapWindowPoints( NULL, hwnd, (POINT*) &rc, 2);
3910     rgn = CreateRectRgnIndirect( &rc);
3911     ValidateRgn( hwnd, rgn);
3912     ret = GetUpdateRect( child, &rc2, 0);
3913     ok( !ret, "Expected GetUpdateRect to return zero, got %d\n", ret);
3914     ok( rc2.left == 0 && rc2.top == 0 && rc2.right == 0 && rc2.bottom == 0,
3915             "Update rectangle %s is not empty!\n", wine_dbgstr_rect(&rc2));
3916 
3917     DeleteObject( rgn);
3918     DestroyWindow( child );
3919 }
3920 
nccalchelper(HWND hwnd,INT x,INT y,RECT * prc)3921 static void nccalchelper(HWND hwnd, INT x, INT y, RECT *prc)
3922 {
3923     RECT rc;
3924     MoveWindow( hwnd, 0, 0, x, y, 0);
3925     GetWindowRect( hwnd, prc);
3926     rc = *prc;
3927     DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)prc);
3928     trace("window rect is %s, nccalc rect is %s\n", wine_dbgstr_rect(&rc), wine_dbgstr_rect(prc));
3929 }
3930 
test_nccalcscroll(HWND parent)3931 static void test_nccalcscroll(HWND parent)
3932 {
3933     RECT rc1;
3934     INT sbheight = GetSystemMetrics( SM_CYHSCROLL);
3935     INT sbwidth = GetSystemMetrics( SM_CXVSCROLL);
3936     HWND hwnd = CreateWindowExA(0, "static", NULL,
3937             WS_CHILD| WS_VISIBLE | WS_VSCROLL | WS_HSCROLL ,
3938             10, 10, 200, 200, parent, 0, 0, NULL);
3939     ShowWindow( parent, SW_SHOW);
3940     UpdateWindow( parent);
3941 
3942     /* test window too low for a horizontal scroll bar */
3943     nccalchelper( hwnd, 100, sbheight, &rc1);
3944     ok( rc1.bottom - rc1.top == sbheight, "Height should be %d size is %s\n", sbheight,
3945             wine_dbgstr_rect(&rc1));
3946 
3947     /* test window just high enough for a horizontal scroll bar */
3948     nccalchelper( hwnd, 100, sbheight + 1, &rc1);
3949     ok( rc1.bottom - rc1.top == 1, "Height should be 1 size is %s\n", wine_dbgstr_rect(&rc1));
3950 
3951     /* test window too narrow for a vertical scroll bar */
3952     nccalchelper( hwnd, sbwidth - 1, 100, &rc1);
3953     ok( rc1.right - rc1.left == sbwidth - 1 , "Width should be %d size is %s\n", sbwidth - 1,
3954             wine_dbgstr_rect(&rc1));
3955 
3956     /* test window just wide enough for a vertical scroll bar */
3957     nccalchelper( hwnd, sbwidth, 100, &rc1);
3958     ok( rc1.right - rc1.left == 0, "Width should be 0 size is %s\n", wine_dbgstr_rect(&rc1));
3959 
3960     /* same test, but with client edge: not enough width */
3961     SetWindowLongA( hwnd, GWL_EXSTYLE, WS_EX_CLIENTEDGE | GetWindowLongA( hwnd, GWL_EXSTYLE));
3962     nccalchelper( hwnd, sbwidth, 100, &rc1);
3963     ok( rc1.right - rc1.left == sbwidth - 2 * GetSystemMetrics(SM_CXEDGE),
3964             "Width should be %d size is %s\n", sbwidth - 2 * GetSystemMetrics(SM_CXEDGE),
3965             wine_dbgstr_rect(&rc1));
3966 
3967     DestroyWindow( hwnd);
3968 }
3969 
test_SetParent(void)3970 static void test_SetParent(void)
3971 {
3972     HWND desktop = GetDesktopWindow();
3973     HMENU hMenu;
3974     HWND ret, parent, child1, child2, child3, child4, sibling, popup;
3975     BOOL bret;
3976 
3977     parent = CreateWindowExA(0, "static", NULL, WS_OVERLAPPEDWINDOW,
3978 			     100, 100, 200, 200, 0, 0, 0, NULL);
3979     assert(parent != 0);
3980     child1 = CreateWindowExA(0, "static", NULL, WS_CHILD,
3981 			     0, 0, 50, 50, parent, 0, 0, NULL);
3982     assert(child1 != 0);
3983     child2 = CreateWindowExA(0, "static", NULL, WS_POPUP,
3984 			     0, 0, 50, 50, child1, 0, 0, NULL);
3985     assert(child2 != 0);
3986     child3 = CreateWindowExA(0, "static", NULL, WS_CHILD,
3987 			     0, 0, 50, 50, child2, 0, 0, NULL);
3988     assert(child3 != 0);
3989     child4 = CreateWindowExA(0, "static", NULL, WS_POPUP,
3990 			     0, 0, 50, 50, child3, 0, 0, NULL);
3991     assert(child4 != 0);
3992 
3993     trace("parent %p, child1 %p, child2 %p, child3 %p, child4 %p\n",
3994 	   parent, child1, child2, child3, child4);
3995 
3996     check_parents(parent, desktop, 0, 0, 0, parent, parent);
3997     check_parents(child1, parent, parent, parent, 0, parent, parent);
3998     check_parents(child2, desktop, parent, parent, parent, child2, parent);
3999     check_parents(child3, child2, child2, child2, 0, child2, parent);
4000     check_parents(child4, desktop, child2, child2, child2, child4, parent);
4001 
4002     ok(!IsChild(desktop, parent), "wrong parent/child %p/%p\n", desktop, parent);
4003     ok(!IsChild(desktop, child1), "wrong parent/child %p/%p\n", desktop, child1);
4004     ok(!IsChild(desktop, child2), "wrong parent/child %p/%p\n", desktop, child2);
4005     ok(!IsChild(desktop, child3), "wrong parent/child %p/%p\n", desktop, child3);
4006     ok(!IsChild(desktop, child4), "wrong parent/child %p/%p\n", desktop, child4);
4007 
4008     ok(IsChild(parent, child1), "wrong parent/child %p/%p\n", parent, child1);
4009     ok(!IsChild(desktop, child2), "wrong parent/child %p/%p\n", desktop, child2);
4010     ok(!IsChild(parent, child2), "wrong parent/child %p/%p\n", parent, child2);
4011     ok(!IsChild(child1, child2), "wrong parent/child %p/%p\n", child1, child2);
4012     ok(!IsChild(parent, child3), "wrong parent/child %p/%p\n", parent, child3);
4013     ok(IsChild(child2, child3), "wrong parent/child %p/%p\n", child2, child3);
4014     ok(!IsChild(parent, child4), "wrong parent/child %p/%p\n", parent, child4);
4015     ok(!IsChild(child3, child4), "wrong parent/child %p/%p\n", child3, child4);
4016     ok(!IsChild(desktop, child4), "wrong parent/child %p/%p\n", desktop, child4);
4017 
4018     if (!is_win9x) /* Win9x doesn't survive this test */
4019     {
4020         ok(!SetParent(parent, child1), "SetParent should fail\n");
4021         ok(!SetParent(child2, child3), "SetParent should fail\n");
4022         ok(SetParent(child1, parent) != 0, "SetParent should not fail\n");
4023         ret = SetParent(parent, child2);
4024         todo_wine ok( !ret || broken( ret != 0 ), "SetParent should fail\n");
4025         if (ret)  /* nt4, win2k */
4026         {
4027             ret = SetParent(parent, child3);
4028             ok(ret != 0, "SetParent should not fail\n");
4029             ret = SetParent(child2, parent);
4030             ok(!ret, "SetParent should fail\n");
4031             ret = SetParent(parent, child4);
4032             ok(ret != 0, "SetParent should not fail\n");
4033             check_parents(parent, child4, child4, 0, 0, child4, parent);
4034             check_parents(child1, parent, parent, parent, 0, child4, parent);
4035             check_parents(child2, desktop, parent, parent, parent, child2, parent);
4036             check_parents(child3, child2, child2, child2, 0, child2, parent);
4037             check_parents(child4, desktop, child2, child2, child2, child4, parent);
4038         }
4039         else
4040         {
4041             ret = SetParent(parent, child3);
4042             ok(ret != 0, "SetParent should not fail\n");
4043             ret = SetParent(child2, parent);
4044             ok(!ret, "SetParent should fail\n");
4045             ret = SetParent(parent, child4);
4046             ok(!ret, "SetParent should fail\n");
4047             check_parents(parent, child3, child3, 0, 0, child2, parent);
4048             check_parents(child1, parent, parent, parent, 0, child2, parent);
4049             check_parents(child2, desktop, parent, parent, parent, child2, parent);
4050             check_parents(child3, child2, child2, child2, 0, child2, parent);
4051             check_parents(child4, desktop, child2, child2, child2, child4, parent);
4052         }
4053     }
4054     else
4055         skip("Win9x/WinMe crash\n");
4056 
4057     hMenu = CreateMenu();
4058     sibling = CreateWindowExA(0, "static", NULL, WS_OVERLAPPEDWINDOW,
4059 			     100, 100, 200, 200, 0, hMenu, 0, NULL);
4060     assert(sibling != 0);
4061 
4062     ok(SetParent(sibling, parent) != 0, "SetParent should not fail\n");
4063     ok(GetMenu(sibling) == hMenu, "SetParent should not remove menu\n");
4064 
4065     ok(SetParent(parent, desktop) != 0, "SetParent should not fail\n");
4066     ok(SetParent(child4, child3) != 0, "SetParent should not fail\n");
4067     ok(SetParent(child3, child2) != 0, "SetParent should not fail\n");
4068     ok(SetParent(child2, child1) != 0, "SetParent should not fail\n");
4069     ok(!IsChild(child3, child4), "wrong parent/child %p/%p\n", child3, child4);
4070     SetWindowLongW(child4, GWL_STYLE, WS_CHILD);
4071     ok(IsChild(child3, child4), "wrong parent/child %p/%p\n", child3, child4);
4072     ok(IsChild(child2, child4), "wrong parent/child %p/%p\n", child2, child4);
4073     ok(!IsChild(child1, child4), "wrong parent/child %p/%p\n", child1, child4);
4074     SetWindowLongW(child2, GWL_STYLE, WS_CHILD);
4075     ok(IsChild(child1, child4), "wrong parent/child %p/%p\n", child1, child4);
4076     ok(IsChild(parent, child4), "wrong parent/child %p/%p\n", parent, child4);
4077 
4078     ok(DestroyWindow(parent), "DestroyWindow() failed\n");
4079 
4080     ok(!IsWindow(parent), "parent still exists\n");
4081     ok(!IsWindow(sibling), "sibling still exists\n");
4082     ok(!IsWindow(child1), "child1 still exists\n");
4083     ok(!IsWindow(child2), "child2 still exists\n");
4084     ok(!IsWindow(child3), "child3 still exists\n");
4085     ok(!IsWindow(child4), "child4 still exists\n");
4086 
4087     parent = CreateWindowExA(0, "static", NULL, WS_OVERLAPPEDWINDOW,
4088 			     100, 100, 200, 200, 0, 0, 0, NULL);
4089     assert(parent != 0);
4090     child1 = CreateWindowExA(0, "static", NULL, WS_CHILD,
4091 			     0, 0, 50, 50, parent, 0, 0, NULL);
4092     assert(child1 != 0);
4093     popup = CreateWindowExA(0, "static", NULL, WS_POPUP,
4094 			     0, 0, 50, 50, 0, 0, 0, NULL);
4095     assert(popup != 0);
4096 
4097     trace("parent %p, child %p, popup %p\n", parent, child1, popup);
4098 
4099     check_parents(parent, desktop, 0, 0, 0, parent, parent);
4100     check_parents(child1, parent, parent, parent, 0, parent, parent);
4101     check_parents(popup, desktop, 0, 0, 0, popup, popup);
4102 
4103     SetActiveWindow(parent);
4104     SetFocus(parent);
4105     check_active_state(parent, 0, parent);
4106 
4107     ret = SetParent(popup, child1);
4108     ok(ret == desktop, "expected %p, got %p\n", desktop, ret);
4109     check_parents(popup, child1, child1, 0, 0, parent, popup);
4110     check_active_state(popup, 0, popup);
4111 
4112     SetActiveWindow(parent);
4113     SetFocus(popup);
4114     check_active_state(popup, 0, popup);
4115 
4116     EnableWindow(child1, FALSE);
4117     check_active_state(popup, 0, popup);
4118     SetFocus(parent);
4119     check_active_state(parent, 0, parent);
4120     SetFocus(popup);
4121     check_active_state(popup, 0, popup);
4122     EnableWindow(child1, TRUE);
4123 
4124     ShowWindow(child1, SW_MINIMIZE);
4125     SetFocus(parent);
4126     check_active_state(parent, 0, parent);
4127     SetFocus(popup);
4128     check_active_state(popup, 0, popup);
4129     ShowWindow(child1, SW_HIDE);
4130 
4131     SetActiveWindow(parent);
4132     SetFocus(parent);
4133     check_active_state(parent, 0, parent);
4134 
4135     bret = SetForegroundWindow(popup);
4136     ok(bret, "SetForegroundWindow() failed\n");
4137     check_active_state(popup, popup, popup);
4138 
4139     ShowWindow(parent, SW_SHOW);
4140     SetActiveWindow(popup);
4141     ok(DestroyWindow(popup), "DestroyWindow() failed\n");
4142     check_active_state(parent, parent, parent);
4143 
4144     ok(DestroyWindow(parent), "DestroyWindow() failed\n");
4145 
4146     ok(!IsWindow(parent), "parent still exists\n");
4147     ok(!IsWindow(child1), "child1 still exists\n");
4148     ok(!IsWindow(popup), "popup still exists\n");
4149 }
4150 
StyleCheckProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)4151 static LRESULT WINAPI StyleCheckProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
4152 {
4153     LPCREATESTRUCTA lpcs;
4154     LPSTYLESTRUCT lpss;
4155 
4156     switch (msg)
4157     {
4158     case WM_NCCREATE:
4159     case WM_CREATE:
4160         lpcs = (LPCREATESTRUCTA)lparam;
4161         lpss = lpcs->lpCreateParams;
4162         if (lpss)
4163         {
4164             if ((lpcs->dwExStyle & WS_EX_DLGMODALFRAME) ||
4165                 ((!(lpcs->dwExStyle & WS_EX_STATICEDGE)) &&
4166                     (lpcs->style & (WS_DLGFRAME | WS_THICKFRAME))))
4167                 ok(lpcs->dwExStyle & WS_EX_WINDOWEDGE, "Window should have WS_EX_WINDOWEDGE style\n");
4168             else
4169                 ok(!(lpcs->dwExStyle & WS_EX_WINDOWEDGE), "Window shouldn't have WS_EX_WINDOWEDGE style\n");
4170 
4171             ok((lpss->styleOld & ~WS_EX_WINDOWEDGE) == (lpcs->dwExStyle & ~WS_EX_WINDOWEDGE),
4172                 "Ex style (0x%08x) should match what the caller passed to CreateWindowEx (0x%08x)\n",
4173                 lpss->styleOld, lpcs->dwExStyle);
4174 
4175             ok(lpss->styleNew == lpcs->style,
4176                 "Style (0x%08x) should match what the caller passed to CreateWindowEx (0x%08x)\n",
4177                 lpss->styleNew, lpcs->style);
4178         }
4179         break;
4180     }
4181     return DefWindowProcA(hwnd, msg, wparam, lparam);
4182 }
4183 
4184 static ATOM atomStyleCheckClass;
4185 
register_style_check_class(void)4186 static void register_style_check_class(void)
4187 {
4188     WNDCLASSA wc =
4189     {
4190         0,
4191         StyleCheckProc,
4192         0,
4193         0,
4194         GetModuleHandleA(NULL),
4195         NULL,
4196         LoadCursorA(0, (LPCSTR)IDC_ARROW),
4197         (HBRUSH)(COLOR_BTNFACE+1),
4198         NULL,
4199         "WineStyleCheck",
4200     };
4201 
4202     atomStyleCheckClass = RegisterClassA(&wc);
4203     assert(atomStyleCheckClass);
4204 }
4205 
check_window_style(DWORD dwStyleIn,DWORD dwExStyleIn,DWORD dwStyleOut,DWORD dwExStyleOut)4206 static void check_window_style(DWORD dwStyleIn, DWORD dwExStyleIn, DWORD dwStyleOut, DWORD dwExStyleOut)
4207 {
4208     DWORD dwActualStyle;
4209     DWORD dwActualExStyle;
4210     STYLESTRUCT ss;
4211     HWND hwnd;
4212     HWND hwndParent = NULL;
4213 
4214     ss.styleNew = dwStyleIn;
4215     ss.styleOld = dwExStyleIn;
4216 
4217     if (dwStyleIn & WS_CHILD)
4218     {
4219         hwndParent = CreateWindowExA(0, (LPCSTR)MAKEINTATOM(atomStyleCheckClass), NULL,
4220             WS_OVERLAPPEDWINDOW, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
4221     }
4222 
4223     hwnd = CreateWindowExA(dwExStyleIn, (LPCSTR)MAKEINTATOM(atomStyleCheckClass), NULL,
4224                     dwStyleIn, 0, 0, 0, 0, hwndParent, NULL, NULL, &ss);
4225     assert(hwnd);
4226 
4227     flush_events( TRUE );
4228 
4229     dwActualStyle = GetWindowLongA(hwnd, GWL_STYLE);
4230     dwActualExStyle = GetWindowLongA(hwnd, GWL_EXSTYLE);
4231     ok(dwActualStyle == dwStyleOut, "expected style %#x, got %#x\n", dwStyleOut, dwActualStyle);
4232     ok(dwActualExStyle == dwExStyleOut, "expected ex_style %#x, got %#x\n", dwExStyleOut, dwActualExStyle);
4233 
4234     /* try setting the styles explicitly */
4235     SetWindowLongA( hwnd, GWL_EXSTYLE, dwExStyleIn );
4236     dwActualStyle = GetWindowLongA(hwnd, GWL_STYLE);
4237     dwActualExStyle = GetWindowLongA(hwnd, GWL_EXSTYLE);
4238     /* WS_EX_WINDOWEDGE can't always be changed */
4239     if (dwExStyleIn & WS_EX_DLGMODALFRAME)
4240         dwExStyleOut = dwExStyleIn | WS_EX_WINDOWEDGE;
4241     else if ((dwActualStyle & (WS_DLGFRAME | WS_THICKFRAME)) && !(dwExStyleIn & WS_EX_STATICEDGE))
4242         dwExStyleOut = dwExStyleIn | WS_EX_WINDOWEDGE;
4243     else
4244         dwExStyleOut = dwExStyleIn & ~WS_EX_WINDOWEDGE;
4245     ok(dwActualStyle == dwStyleOut, "expected style %#x, got %#x\n", dwStyleOut, dwActualStyle);
4246     ok(dwActualExStyle == dwExStyleOut, "expected ex_style %#x, got %#x\n", dwExStyleOut, dwActualExStyle);
4247 
4248     SetWindowLongA( hwnd, GWL_STYLE, dwStyleIn );
4249     dwActualStyle = GetWindowLongA(hwnd, GWL_STYLE);
4250     dwActualExStyle = GetWindowLongA(hwnd, GWL_EXSTYLE);
4251     /* WS_CLIPSIBLINGS can't be reset on top-level windows */
4252     if ((dwStyleIn & (WS_CHILD|WS_POPUP)) == WS_CHILD) dwStyleOut = dwStyleIn;
4253     else dwStyleOut = dwStyleIn | WS_CLIPSIBLINGS;
4254     /* WS_EX_WINDOWEDGE can't always be changed */
4255     if (dwExStyleIn & WS_EX_DLGMODALFRAME)
4256         dwExStyleOut = dwExStyleIn | WS_EX_WINDOWEDGE;
4257     else if ((dwActualStyle & (WS_DLGFRAME | WS_THICKFRAME)) && !(dwExStyleIn & WS_EX_STATICEDGE))
4258         dwExStyleOut = dwExStyleIn | WS_EX_WINDOWEDGE;
4259     else
4260         dwExStyleOut = dwExStyleIn & ~WS_EX_WINDOWEDGE;
4261     ok(dwActualStyle == dwStyleOut, "expected style %#x, got %#x\n", dwStyleOut, dwActualStyle);
4262     /* FIXME: Remove the condition below once Wine is fixed */
4263     todo_wine_if (dwActualExStyle != dwExStyleOut)
4264         ok(dwActualExStyle == dwExStyleOut, "expected ex_style %#x, got %#x\n", dwExStyleOut, dwActualExStyle);
4265 
4266     DestroyWindow(hwnd);
4267     if (hwndParent) DestroyWindow(hwndParent);
4268 }
4269 
4270 /* tests what window styles the window manager automatically adds */
test_window_styles(void)4271 static void test_window_styles(void)
4272 {
4273     register_style_check_class();
4274 
4275     check_window_style(0, 0, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_WINDOWEDGE);
4276     check_window_style(WS_DLGFRAME, 0, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_WINDOWEDGE);
4277     check_window_style(WS_THICKFRAME, 0, WS_THICKFRAME|WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_WINDOWEDGE);
4278     check_window_style(WS_DLGFRAME, WS_EX_STATICEDGE, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_WINDOWEDGE|WS_EX_STATICEDGE);
4279     check_window_style(WS_THICKFRAME, WS_EX_STATICEDGE, WS_THICKFRAME|WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_WINDOWEDGE|WS_EX_STATICEDGE);
4280     check_window_style(WS_OVERLAPPEDWINDOW, 0, WS_CLIPSIBLINGS|WS_OVERLAPPEDWINDOW, WS_EX_WINDOWEDGE);
4281     check_window_style(WS_CHILD, 0, WS_CHILD, 0);
4282     check_window_style(WS_CHILD|WS_DLGFRAME, 0, WS_CHILD|WS_DLGFRAME, WS_EX_WINDOWEDGE);
4283     check_window_style(WS_CHILD|WS_THICKFRAME, 0, WS_CHILD|WS_THICKFRAME, WS_EX_WINDOWEDGE);
4284     check_window_style(WS_CHILD|WS_DLGFRAME, WS_EX_STATICEDGE, WS_CHILD|WS_DLGFRAME, WS_EX_STATICEDGE);
4285     check_window_style(WS_CHILD|WS_THICKFRAME, WS_EX_STATICEDGE, WS_CHILD|WS_THICKFRAME, WS_EX_STATICEDGE);
4286     check_window_style(WS_CHILD|WS_CAPTION, 0, WS_CHILD|WS_CAPTION, WS_EX_WINDOWEDGE);
4287     check_window_style(WS_CHILD|WS_CAPTION|WS_SYSMENU, 0, WS_CHILD|WS_CAPTION|WS_SYSMENU, WS_EX_WINDOWEDGE);
4288     check_window_style(WS_CHILD, WS_EX_WINDOWEDGE, WS_CHILD, 0);
4289     check_window_style(WS_CHILD, WS_EX_DLGMODALFRAME, WS_CHILD, WS_EX_WINDOWEDGE|WS_EX_DLGMODALFRAME);
4290     check_window_style(WS_CHILD, WS_EX_DLGMODALFRAME|WS_EX_STATICEDGE, WS_CHILD, WS_EX_STATICEDGE|WS_EX_WINDOWEDGE|WS_EX_DLGMODALFRAME);
4291     check_window_style(WS_CHILD|WS_POPUP, 0, WS_CHILD|WS_POPUP|WS_CLIPSIBLINGS, 0);
4292     check_window_style(WS_CHILD|WS_POPUP|WS_DLGFRAME, 0, WS_CHILD|WS_POPUP|WS_DLGFRAME|WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE);
4293     check_window_style(WS_CHILD|WS_POPUP|WS_THICKFRAME, 0, WS_CHILD|WS_POPUP|WS_THICKFRAME|WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE);
4294     check_window_style(WS_CHILD|WS_POPUP|WS_DLGFRAME, WS_EX_STATICEDGE, WS_CHILD|WS_POPUP|WS_DLGFRAME|WS_CLIPSIBLINGS, WS_EX_STATICEDGE);
4295     check_window_style(WS_CHILD|WS_POPUP|WS_THICKFRAME, WS_EX_STATICEDGE, WS_CHILD|WS_POPUP|WS_THICKFRAME|WS_CLIPSIBLINGS, WS_EX_STATICEDGE);
4296     check_window_style(WS_CHILD|WS_POPUP, WS_EX_APPWINDOW, WS_CHILD|WS_POPUP|WS_CLIPSIBLINGS, WS_EX_APPWINDOW);
4297     check_window_style(WS_CHILD|WS_POPUP, WS_EX_WINDOWEDGE, WS_CHILD|WS_POPUP|WS_CLIPSIBLINGS, 0);
4298     check_window_style(WS_CHILD, WS_EX_WINDOWEDGE, WS_CHILD, 0);
4299     check_window_style(0, WS_EX_TOOLWINDOW, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_WINDOWEDGE|WS_EX_TOOLWINDOW);
4300     check_window_style(WS_POPUP, 0, WS_POPUP|WS_CLIPSIBLINGS, 0);
4301     check_window_style(WS_POPUP, WS_EX_WINDOWEDGE, WS_POPUP|WS_CLIPSIBLINGS, 0);
4302     check_window_style(WS_POPUP|WS_DLGFRAME, 0, WS_POPUP|WS_DLGFRAME|WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE);
4303     check_window_style(WS_POPUP|WS_THICKFRAME, 0, WS_POPUP|WS_THICKFRAME|WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE);
4304     check_window_style(WS_POPUP|WS_DLGFRAME, WS_EX_STATICEDGE, WS_POPUP|WS_DLGFRAME|WS_CLIPSIBLINGS, WS_EX_STATICEDGE);
4305     check_window_style(WS_POPUP|WS_THICKFRAME, WS_EX_STATICEDGE, WS_POPUP|WS_THICKFRAME|WS_CLIPSIBLINGS, WS_EX_STATICEDGE);
4306     check_window_style(WS_CAPTION, WS_EX_STATICEDGE, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_STATICEDGE|WS_EX_WINDOWEDGE);
4307     check_window_style(0, WS_EX_APPWINDOW, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_APPWINDOW|WS_EX_WINDOWEDGE);
4308 
4309     if (pGetLayeredWindowAttributes)
4310     {
4311         check_window_style(0, WS_EX_LAYERED, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_LAYERED|WS_EX_WINDOWEDGE);
4312         check_window_style(0, WS_EX_LAYERED|WS_EX_TRANSPARENT, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_LAYERED|WS_EX_TRANSPARENT|WS_EX_WINDOWEDGE);
4313         check_window_style(0, WS_EX_LAYERED|WS_EX_TRANSPARENT|WS_EX_TOOLWINDOW, WS_CLIPSIBLINGS|WS_CAPTION,
4314                                                       WS_EX_LAYERED|WS_EX_TRANSPARENT|WS_EX_TOOLWINDOW|WS_EX_WINDOWEDGE);
4315     }
4316 }
4317 
root_dialog(HWND hwnd)4318 static HWND root_dialog(HWND hwnd)
4319 {
4320     while ((GetWindowLongA(hwnd, GWL_EXSTYLE) & WS_EX_CONTROLPARENT) &&
4321            (GetWindowLongA(hwnd, GWL_STYLE) & (WS_CHILD|WS_POPUP)) == WS_CHILD)
4322     {
4323         HWND parent = GetParent(hwnd);
4324 
4325         /* simple detector for a window being a dialog */
4326         if (!DefDlgProcA(parent, DM_GETDEFID, 0, 0))
4327             break;
4328 
4329         hwnd = parent;
4330 
4331         if (!(GetWindowLongA(hwnd, GWL_STYLE) & DS_CONTROL))
4332             break;
4333     }
4334 
4335     return hwnd;
4336 }
4337 
empty_dlg_proc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)4338 static INT_PTR WINAPI empty_dlg_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
4339 {
4340     return 0;
4341 }
4342 
4343 static LRESULT expected_id;
4344 
empty_dlg_proc3(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)4345 static INT_PTR WINAPI empty_dlg_proc3(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
4346 {
4347     if (msg == WM_INITDIALOG)
4348     {
4349         HWND parent = GetParent(hwnd);
4350         LRESULT id, ret;
4351 
4352         id = DefDlgProcA(parent, DM_GETDEFID, 0, 0);
4353         if (!id || root_dialog(hwnd) == hwnd)
4354             parent = 0;
4355 
4356         id = DefDlgProcA(hwnd, DM_GETDEFID, 0, 0);
4357         if (!parent)
4358             ok(id == MAKELONG(IDOK,DC_HASDEFID), "expected (IDOK,DC_HASDEFID), got %08lx\n", id);
4359         else
4360             ok(id == expected_id, "expected %08lx, got %08lx\n", expected_id, id);
4361 
4362         ret = DefDlgProcA(hwnd, DM_SETDEFID, 0x3333, 0);
4363         ok(ret, "DefDlgProc(DM_SETDEFID) failed\n");
4364         id = DefDlgProcA(hwnd, DM_GETDEFID, 0, 0);
4365         ok(id == MAKELONG(0x3333,DC_HASDEFID), "expected (0x3333,DC_HASDEFID), got %08lx\n", id);
4366 
4367         if (parent)
4368         {
4369             id = DefDlgProcA(parent, DM_GETDEFID, 0, 0);
4370             ok(id == MAKELONG(0x3333,DC_HASDEFID), "expected (0x3333,DC_HASDEFID), got %08lx\n", id);
4371 
4372             expected_id = MAKELONG(0x3333,DC_HASDEFID);
4373         }
4374 
4375         EndDialog(hwnd, 0);
4376     }
4377 
4378     return 0;
4379 }
4380 
4381 struct dialog_param
4382 {
4383     HWND parent, grand_parent;
4384     DLGTEMPLATE *dlg_data;
4385 };
4386 
empty_dlg_proc2(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)4387 static INT_PTR WINAPI empty_dlg_proc2(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
4388 {
4389     if (msg == WM_INITDIALOG)
4390     {
4391         DWORD style = GetWindowLongA(hwnd, GWL_STYLE);
4392         struct dialog_param *param = (struct dialog_param *)lparam;
4393         BOOL parent_is_child;
4394         HWND disabled_hwnd;
4395         LRESULT id, ret;
4396 
4397         id = DefDlgProcA(hwnd, DM_GETDEFID, 0, 0);
4398         ok(id == MAKELONG(IDOK,DC_HASDEFID), "expected (IDOK,DC_HASDEFID), got %08lx\n", id);
4399         ret = DefDlgProcA(hwnd, DM_SETDEFID, 0x2222, 0);
4400         ok(ret, "DefDlgProc(DM_SETDEFID) failed\n");
4401         id = DefDlgProcA(hwnd, DM_GETDEFID, 0, 0);
4402         ok(id == MAKELONG(0x2222,DC_HASDEFID), "expected (0x2222,DC_HASDEFID), got %08lx\n", id);
4403 
4404         expected_id = MAKELONG(0x2222,DC_HASDEFID);
4405 
4406         parent_is_child = (GetWindowLongA(param->parent, GWL_STYLE) & (WS_POPUP | WS_CHILD)) == WS_CHILD;
4407 
4408         ok(IsWindowEnabled(hwnd), "wrong state for %p\n", hwnd);
4409         if (parent_is_child)
4410         {
4411             ok(IsWindowEnabled(param->parent), "wrong state for %08x\n", style);
4412             disabled_hwnd = param->grand_parent;
4413         }
4414         else
4415         {
4416             ok(!IsWindowEnabled(param->parent), "wrong state for %08x\n", style);
4417             disabled_hwnd = param->parent;
4418         }
4419 
4420         if (param->grand_parent)
4421         {
4422             if (parent_is_child)
4423                 ok(!IsWindowEnabled(param->grand_parent), "wrong state for %08x\n", style);
4424             else
4425                 ok(IsWindowEnabled(param->grand_parent), "wrong state for %08x\n", style);
4426         }
4427 
4428         DialogBoxIndirectParamA(GetModuleHandleA(NULL), param->dlg_data, disabled_hwnd, empty_dlg_proc3, 0);
4429         ok(IsWindowEnabled(disabled_hwnd), "wrong state for %08x\n", style);
4430 
4431         ok(IsWindowEnabled(hwnd), "wrong state for %p\n", hwnd);
4432         ok(IsWindowEnabled(param->parent), "wrong state for %p\n", param->parent);
4433         if (param->grand_parent)
4434             ok(IsWindowEnabled(param->grand_parent), "wrong state for %p (%08x)\n", param->grand_parent, style);
4435 
4436         DialogBoxIndirectParamA(GetModuleHandleA(NULL), param->dlg_data, hwnd, empty_dlg_proc3, 0);
4437         ok(IsWindowEnabled(hwnd), "wrong state for %p\n", hwnd);
4438         ok(IsWindowEnabled(param->parent), "wrong state for %p\n", param->parent);
4439         if (param->grand_parent)
4440             ok(IsWindowEnabled(param->grand_parent), "wrong state for %p (%08x)\n", param->grand_parent, style);
4441 
4442         param->dlg_data->style |= WS_CHILD;
4443         DialogBoxIndirectParamA(GetModuleHandleA(NULL), param->dlg_data, hwnd, empty_dlg_proc3, 0);
4444         ok(IsWindowEnabled(hwnd), "wrong state for %p (%08x)\n", hwnd, style);
4445 
4446         param->dlg_data->style |= DS_CONTROL;
4447         DialogBoxIndirectParamA(GetModuleHandleA(NULL), param->dlg_data, hwnd, empty_dlg_proc3, 0);
4448         ok(IsWindowEnabled(hwnd), "wrong state for %p (%08x)\n", hwnd, style);
4449 
4450         param->dlg_data->dwExtendedStyle |= WS_EX_CONTROLPARENT;
4451         SetWindowLongA(hwnd, GWL_EXSTYLE, GetWindowLongA(hwnd, GWL_EXSTYLE) | WS_EX_CONTROLPARENT);
4452         SetWindowLongA(hwnd, GWL_STYLE, style & ~DS_CONTROL);
4453         param->dlg_data->style &= ~DS_CONTROL;
4454         DialogBoxIndirectParamA(GetModuleHandleA(NULL), param->dlg_data, hwnd, empty_dlg_proc3, 0);
4455         ok(IsWindowEnabled(hwnd), "wrong state for %p (%08x)\n", hwnd, style);
4456 
4457         SetWindowLongA(hwnd, GWL_STYLE, style | DS_CONTROL);
4458         DialogBoxIndirectParamA(GetModuleHandleA(NULL), param->dlg_data, hwnd, empty_dlg_proc3, 0);
4459         ok(IsWindowEnabled(hwnd), "wrong state for %p (%08x)\n", hwnd, style);
4460 
4461         param->dlg_data->style |= DS_CONTROL;
4462         DialogBoxIndirectParamA(GetModuleHandleA(NULL), param->dlg_data, hwnd, empty_dlg_proc3, 0);
4463         ok(IsWindowEnabled(hwnd), "wrong state for %p (%08x)\n", hwnd, style);
4464 
4465         EndDialog(hwnd, 0);
4466     }
4467     return 0;
4468 }
4469 
check_dialog_style(DWORD style_in,DWORD ex_style_in,DWORD style_out,DWORD ex_style_out)4470 static void check_dialog_style(DWORD style_in, DWORD ex_style_in, DWORD style_out, DWORD ex_style_out)
4471 {
4472     struct
4473     {
4474         DLGTEMPLATE dt;
4475         WORD menu_name;
4476         WORD class_id;
4477         WORD class_atom;
4478         WCHAR caption[1];
4479     } dlg_data;
4480     DWORD style, ex_style;
4481     HWND hwnd, grand_parent = 0, parent = 0;
4482     struct dialog_param param;
4483     LRESULT id, ret;
4484 
4485     if (style_in & WS_CHILD)
4486     {
4487         grand_parent = CreateWindowExA(0, "static", NULL, WS_OVERLAPPEDWINDOW,
4488                                 0, 0, 0, 0, NULL, NULL, NULL, NULL);
4489         ok(grand_parent != 0, "grand_parent creation failed\n");
4490     }
4491 
4492     parent = CreateWindowExA(0, "static", NULL, style_in,
4493                              0, 0, 0, 0, grand_parent, NULL, NULL, NULL);
4494     ok(parent != 0, "parent creation failed, style %#x\n", style_in);
4495 
4496     dlg_data.dt.style = style_in;
4497     dlg_data.dt.dwExtendedStyle = ex_style_in;
4498     dlg_data.dt.cdit = 0;
4499     dlg_data.dt.x = 0;
4500     dlg_data.dt.y = 0;
4501     dlg_data.dt.cx = 100;
4502     dlg_data.dt.cy = 100;
4503     dlg_data.menu_name = 0;
4504     dlg_data.class_id = 0;
4505     dlg_data.class_atom = 0;
4506     dlg_data.caption[0] = 0;
4507 
4508     hwnd = CreateDialogIndirectParamA(GetModuleHandleA(NULL), &dlg_data.dt, parent, empty_dlg_proc, 0);
4509     ok(hwnd != 0, "dialog creation failed, style %#x, exstyle %#x\n", style_in, ex_style_in);
4510 
4511     id = DefDlgProcA(hwnd, DM_GETDEFID, 0, 0);
4512     ok(id == MAKELONG(IDOK,DC_HASDEFID), "expected (IDOK,DC_HASDEFID), got %08lx\n", id);
4513     ret = DefDlgProcA(hwnd, DM_SETDEFID, 0x1111, 0);
4514     ok(ret, "DefDlgProc(DM_SETDEFID) failed\n");
4515     id = DefDlgProcA(hwnd, DM_GETDEFID, 0, 0);
4516     ok(id == MAKELONG(0x1111,DC_HASDEFID), "expected (0x1111,DC_HASDEFID), got %08lx\n", id);
4517 
4518     flush_events( TRUE );
4519 
4520     style = GetWindowLongA(hwnd, GWL_STYLE);
4521     ex_style = GetWindowLongA(hwnd, GWL_EXSTYLE);
4522     ok(style == (style_out | DS_3DLOOK), "got %#x\n", style);
4523     ok(ex_style == ex_style_out, "expected ex_style %#x, got %#x\n", ex_style_out, ex_style);
4524 
4525     ok(IsWindowEnabled(parent), "wrong parent state (dialog style %#x)\n", style_in);
4526 
4527     /* try setting the styles explicitly */
4528     SetWindowLongA(hwnd, GWL_EXSTYLE, ex_style_in);
4529     style = GetWindowLongA(hwnd, GWL_STYLE);
4530     ex_style = GetWindowLongA(hwnd, GWL_EXSTYLE);
4531     ok(style == (style_out | DS_3DLOOK), "got %#x\n", style);
4532     /* WS_EX_WINDOWEDGE can't always be changed */
4533     if (ex_style_in & WS_EX_DLGMODALFRAME)
4534         ex_style_out = ex_style_in | WS_EX_WINDOWEDGE;
4535     else if ((style & (WS_DLGFRAME | WS_THICKFRAME)) && !(ex_style_in & WS_EX_STATICEDGE))
4536         ex_style_out = ex_style_in | WS_EX_WINDOWEDGE;
4537     else
4538         ex_style_out = ex_style_in & ~WS_EX_WINDOWEDGE;
4539     ok(ex_style == ex_style_out, "expected ex_style %#x, got %#x\n", ex_style_out, ex_style);
4540 
4541     SetWindowLongA(hwnd, GWL_STYLE, style_in);
4542     style = GetWindowLongA(hwnd, GWL_STYLE);
4543     ex_style = GetWindowLongA(hwnd, GWL_EXSTYLE);
4544     /* WS_CLIPSIBLINGS can't be reset on top-level windows */
4545     if ((style_in & (WS_CHILD | WS_POPUP)) == WS_CHILD) style_out = style_in;
4546     else style_out = style_in | WS_CLIPSIBLINGS;
4547     ok(style == style_out, "expected style %#x, got %#x\n", style_out, style);
4548     /* WS_EX_WINDOWEDGE can't always be changed */
4549     if (ex_style_in & WS_EX_DLGMODALFRAME)
4550         ex_style_out = ex_style_in | WS_EX_WINDOWEDGE;
4551     else if ((style & (WS_DLGFRAME | WS_THICKFRAME)) && !(ex_style_in & WS_EX_STATICEDGE))
4552         ex_style_out = ex_style_in | WS_EX_WINDOWEDGE;
4553     else
4554         ex_style_out = ex_style_in & ~WS_EX_WINDOWEDGE;
4555     /* FIXME: Remove the condition below once Wine is fixed */
4556     todo_wine_if (ex_style != ex_style_out)
4557         ok(ex_style == ex_style_out, "expected ex_style %#x, got %#x\n", ex_style_out, ex_style);
4558 
4559     DestroyWindow(hwnd);
4560 
4561     param.parent = parent;
4562     param.grand_parent = grand_parent;
4563     param.dlg_data = &dlg_data.dt;
4564     DialogBoxIndirectParamA(GetModuleHandleA(NULL), &dlg_data.dt, parent, empty_dlg_proc2, (LPARAM)&param);
4565 
4566     ok(IsWindowEnabled(parent), "wrong parent state (dialog style %#x)\n", style_in);
4567     if (grand_parent)
4568         ok(IsWindowEnabled(grand_parent), "wrong grand parent state (dialog style %#x)\n", style_in);
4569 
4570     DestroyWindow(parent);
4571     DestroyWindow(grand_parent);
4572 }
4573 
test_dialog_styles(void)4574 static void test_dialog_styles(void)
4575 {
4576     check_dialog_style(0, 0, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_WINDOWEDGE|WS_EX_CONTROLPARENT);
4577     check_dialog_style(WS_DLGFRAME, 0, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_WINDOWEDGE|WS_EX_CONTROLPARENT);
4578     check_dialog_style(WS_THICKFRAME, 0, WS_THICKFRAME|WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_WINDOWEDGE|WS_EX_CONTROLPARENT);
4579     check_dialog_style(WS_DLGFRAME, WS_EX_STATICEDGE, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_WINDOWEDGE|WS_EX_STATICEDGE|WS_EX_CONTROLPARENT);
4580     check_dialog_style(WS_THICKFRAME, WS_EX_STATICEDGE, WS_THICKFRAME|WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_WINDOWEDGE|WS_EX_STATICEDGE|WS_EX_CONTROLPARENT);
4581     check_dialog_style(DS_CONTROL, 0, WS_CLIPSIBLINGS|WS_CAPTION|DS_CONTROL, WS_EX_WINDOWEDGE|WS_EX_CONTROLPARENT);
4582     check_dialog_style(WS_CAPTION, 0, WS_CAPTION|WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE|WS_EX_CONTROLPARENT);
4583     check_dialog_style(WS_BORDER, 0, WS_CAPTION|WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE|WS_EX_CONTROLPARENT);
4584     check_dialog_style(WS_DLGFRAME, 0, WS_CAPTION|WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE|WS_EX_CONTROLPARENT);
4585     check_dialog_style(WS_BORDER|DS_CONTROL, 0, WS_CAPTION|DS_CONTROL|WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE|WS_EX_CONTROLPARENT);
4586     check_dialog_style(WS_DLGFRAME|DS_CONTROL, 0, WS_CAPTION|DS_CONTROL|WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE|WS_EX_CONTROLPARENT);
4587     check_dialog_style(WS_CAPTION|WS_SYSMENU, 0, WS_CAPTION|WS_SYSMENU|WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE|WS_EX_CONTROLPARENT);
4588     check_dialog_style(WS_SYSMENU, 0, WS_CAPTION|WS_SYSMENU|WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE|WS_EX_CONTROLPARENT);
4589     check_dialog_style(WS_CAPTION|DS_CONTROL, 0, WS_CAPTION|DS_CONTROL|WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE|WS_EX_CONTROLPARENT);
4590     check_dialog_style(WS_SYSMENU|DS_CONTROL, 0, WS_CAPTION|DS_CONTROL|WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE|WS_EX_CONTROLPARENT);
4591     check_dialog_style(WS_CAPTION|WS_SYSMENU|DS_CONTROL, 0, WS_CAPTION|DS_CONTROL|WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE|WS_EX_CONTROLPARENT);
4592     check_dialog_style(WS_OVERLAPPEDWINDOW, 0, WS_CLIPSIBLINGS|WS_OVERLAPPEDWINDOW, WS_EX_WINDOWEDGE|WS_EX_CONTROLPARENT);
4593     check_dialog_style(WS_CHILD, 0, WS_CHILD, 0);
4594     check_dialog_style(WS_CHILD|WS_DLGFRAME, 0, WS_CHILD|WS_DLGFRAME, WS_EX_WINDOWEDGE);
4595     check_dialog_style(WS_CHILD|WS_THICKFRAME, 0, WS_CHILD|WS_THICKFRAME, WS_EX_WINDOWEDGE);
4596     check_dialog_style(WS_CHILD|WS_DLGFRAME, WS_EX_STATICEDGE, WS_CHILD|WS_DLGFRAME, WS_EX_STATICEDGE);
4597     check_dialog_style(WS_CHILD|WS_THICKFRAME, WS_EX_STATICEDGE, WS_CHILD|WS_THICKFRAME, WS_EX_STATICEDGE);
4598     check_dialog_style(WS_CHILD|DS_CONTROL, 0, WS_CHILD|DS_CONTROL, WS_EX_CONTROLPARENT);
4599     check_dialog_style(WS_CHILD|WS_CAPTION, 0, WS_CHILD|WS_CAPTION, WS_EX_WINDOWEDGE);
4600     check_dialog_style(WS_CHILD|WS_BORDER, 0, WS_CHILD|WS_BORDER, 0);
4601     check_dialog_style(WS_CHILD|WS_DLGFRAME, 0, WS_CHILD|WS_DLGFRAME, WS_EX_WINDOWEDGE);
4602     check_dialog_style(WS_CHILD|WS_BORDER|DS_CONTROL, 0, WS_CHILD|DS_CONTROL, WS_EX_CONTROLPARENT);
4603     check_dialog_style(WS_CHILD|WS_DLGFRAME|DS_CONTROL, 0, WS_CHILD|DS_CONTROL, WS_EX_CONTROLPARENT);
4604     check_dialog_style(WS_CHILD|WS_CAPTION|WS_SYSMENU, 0, WS_CHILD|WS_CAPTION|WS_SYSMENU, WS_EX_WINDOWEDGE);
4605     check_dialog_style(WS_CHILD|WS_SYSMENU, 0, WS_CHILD|WS_SYSMENU, 0);
4606     check_dialog_style(WS_CHILD|WS_CAPTION|DS_CONTROL, 0, WS_CHILD|DS_CONTROL, WS_EX_CONTROLPARENT);
4607     check_dialog_style(WS_CHILD|WS_SYSMENU|DS_CONTROL, 0, WS_CHILD|DS_CONTROL, WS_EX_CONTROLPARENT);
4608     check_dialog_style(WS_CHILD|WS_CAPTION|WS_SYSMENU|DS_CONTROL, 0, WS_CHILD|DS_CONTROL, WS_EX_CONTROLPARENT);
4609     check_dialog_style(WS_CHILD, WS_EX_WINDOWEDGE, WS_CHILD, 0);
4610     check_dialog_style(WS_CHILD, WS_EX_DLGMODALFRAME, WS_CHILD, WS_EX_WINDOWEDGE|WS_EX_DLGMODALFRAME);
4611     check_dialog_style(WS_CHILD, WS_EX_DLGMODALFRAME|WS_EX_STATICEDGE, WS_CHILD, WS_EX_STATICEDGE|WS_EX_WINDOWEDGE|WS_EX_DLGMODALFRAME);
4612     check_dialog_style(WS_CHILD|WS_POPUP, 0, WS_CHILD|WS_POPUP|WS_CLIPSIBLINGS, 0);
4613     check_dialog_style(WS_CHILD|WS_POPUP|WS_DLGFRAME, 0, WS_CHILD|WS_POPUP|WS_DLGFRAME|WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE);
4614     check_dialog_style(WS_CHILD|WS_POPUP|WS_THICKFRAME, 0, WS_CHILD|WS_POPUP|WS_THICKFRAME|WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE);
4615     check_dialog_style(WS_CHILD|WS_POPUP|WS_DLGFRAME, WS_EX_STATICEDGE, WS_CHILD|WS_POPUP|WS_DLGFRAME|WS_CLIPSIBLINGS, WS_EX_STATICEDGE);
4616     check_dialog_style(WS_CHILD|WS_POPUP|WS_THICKFRAME, WS_EX_STATICEDGE, WS_CHILD|WS_POPUP|WS_THICKFRAME|WS_CLIPSIBLINGS, WS_EX_STATICEDGE);
4617     check_dialog_style(WS_CHILD|WS_POPUP|DS_CONTROL, 0, WS_CHILD|WS_POPUP|WS_CLIPSIBLINGS|DS_CONTROL, WS_EX_CONTROLPARENT);
4618     check_dialog_style(WS_CHILD|WS_POPUP|WS_CAPTION, 0, WS_CHILD|WS_POPUP|WS_CAPTION|WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE);
4619     check_dialog_style(WS_CHILD|WS_POPUP|WS_BORDER, 0, WS_CHILD|WS_POPUP|WS_BORDER|WS_CLIPSIBLINGS, 0);
4620     check_dialog_style(WS_CHILD|WS_POPUP|WS_DLGFRAME, 0, WS_CHILD|WS_POPUP|WS_DLGFRAME|WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE);
4621     check_dialog_style(WS_CHILD|WS_POPUP|WS_BORDER|DS_CONTROL, 0, WS_CHILD|WS_POPUP|DS_CONTROL|WS_CLIPSIBLINGS, WS_EX_CONTROLPARENT);
4622     check_dialog_style(WS_CHILD|WS_POPUP|WS_DLGFRAME|DS_CONTROL, 0, WS_CHILD|WS_POPUP|DS_CONTROL|WS_CLIPSIBLINGS, WS_EX_CONTROLPARENT);
4623     check_dialog_style(WS_CHILD|WS_POPUP|WS_CAPTION|WS_SYSMENU, 0, WS_CHILD|WS_POPUP|WS_CAPTION|WS_SYSMENU|WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE);
4624     check_dialog_style(WS_CHILD|WS_POPUP|WS_SYSMENU, 0, WS_CHILD|WS_POPUP|WS_SYSMENU|WS_CLIPSIBLINGS, 0);
4625     check_dialog_style(WS_CHILD|WS_POPUP|WS_CAPTION|DS_CONTROL, 0, WS_CHILD|WS_POPUP|DS_CONTROL|WS_CLIPSIBLINGS, WS_EX_CONTROLPARENT);
4626     check_dialog_style(WS_CHILD|WS_POPUP|WS_SYSMENU|DS_CONTROL, 0, WS_CHILD|WS_POPUP|DS_CONTROL|WS_CLIPSIBLINGS, WS_EX_CONTROLPARENT);
4627     check_dialog_style(WS_CHILD|WS_POPUP|WS_CAPTION|WS_SYSMENU|DS_CONTROL, 0, WS_CHILD|WS_POPUP|DS_CONTROL|WS_CLIPSIBLINGS, WS_EX_CONTROLPARENT);
4628     check_dialog_style(WS_CHILD|WS_POPUP, WS_EX_APPWINDOW, WS_CHILD|WS_POPUP|WS_CLIPSIBLINGS, WS_EX_APPWINDOW);
4629     check_dialog_style(WS_CHILD|WS_POPUP, WS_EX_WINDOWEDGE, WS_CHILD|WS_POPUP|WS_CLIPSIBLINGS, 0);
4630     check_dialog_style(WS_CHILD, WS_EX_WINDOWEDGE, WS_CHILD, 0);
4631     check_dialog_style(0, WS_EX_TOOLWINDOW, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_WINDOWEDGE|WS_EX_TOOLWINDOW|WS_EX_CONTROLPARENT);
4632     check_dialog_style(WS_POPUP, 0, WS_POPUP|WS_CLIPSIBLINGS, WS_EX_CONTROLPARENT);
4633     check_dialog_style(WS_POPUP, WS_EX_WINDOWEDGE, WS_POPUP|WS_CLIPSIBLINGS, WS_EX_CONTROLPARENT);
4634     check_dialog_style(WS_POPUP|WS_DLGFRAME, 0, WS_POPUP|WS_DLGFRAME|WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE|WS_EX_CONTROLPARENT);
4635     check_dialog_style(WS_POPUP|WS_THICKFRAME, 0, WS_POPUP|WS_THICKFRAME|WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE|WS_EX_CONTROLPARENT);
4636     check_dialog_style(WS_POPUP|WS_DLGFRAME, WS_EX_STATICEDGE, WS_POPUP|WS_DLGFRAME|WS_CLIPSIBLINGS, WS_EX_STATICEDGE|WS_EX_CONTROLPARENT);
4637     check_dialog_style(WS_POPUP|WS_THICKFRAME, WS_EX_STATICEDGE, WS_POPUP|WS_THICKFRAME|WS_CLIPSIBLINGS, WS_EX_STATICEDGE|WS_EX_CONTROLPARENT);
4638     check_dialog_style(WS_POPUP|DS_CONTROL, 0, WS_POPUP|WS_CLIPSIBLINGS|DS_CONTROL, WS_EX_CONTROLPARENT);
4639     check_dialog_style(WS_POPUP|WS_CAPTION, 0, WS_POPUP|WS_CAPTION|WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE|WS_EX_CONTROLPARENT);
4640     check_dialog_style(WS_POPUP|WS_BORDER, 0, WS_POPUP|WS_BORDER|WS_CLIPSIBLINGS, WS_EX_CONTROLPARENT);
4641     check_dialog_style(WS_POPUP|WS_DLGFRAME, 0, WS_POPUP|WS_DLGFRAME|WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE|WS_EX_CONTROLPARENT);
4642     check_dialog_style(WS_POPUP|WS_BORDER|DS_CONTROL, 0, WS_POPUP|DS_CONTROL|WS_CLIPSIBLINGS, WS_EX_CONTROLPARENT);
4643     check_dialog_style(WS_POPUP|WS_DLGFRAME|DS_CONTROL, 0, WS_POPUP|DS_CONTROL|WS_CLIPSIBLINGS, WS_EX_CONTROLPARENT);
4644     check_dialog_style(WS_POPUP|WS_CAPTION|WS_SYSMENU, 0, WS_POPUP|WS_CAPTION|WS_SYSMENU|WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE|WS_EX_CONTROLPARENT);
4645     check_dialog_style(WS_POPUP|WS_SYSMENU, 0, WS_POPUP|WS_SYSMENU|WS_CLIPSIBLINGS, WS_EX_CONTROLPARENT);
4646     check_dialog_style(WS_POPUP|WS_CAPTION|DS_CONTROL, 0, WS_POPUP|DS_CONTROL|WS_CLIPSIBLINGS, WS_EX_CONTROLPARENT);
4647     check_dialog_style(WS_POPUP|WS_SYSMENU|DS_CONTROL, 0, WS_POPUP|DS_CONTROL|WS_CLIPSIBLINGS, WS_EX_CONTROLPARENT);
4648     check_dialog_style(WS_POPUP|WS_CAPTION|WS_SYSMENU|DS_CONTROL, 0, WS_POPUP|DS_CONTROL|WS_CLIPSIBLINGS, WS_EX_CONTROLPARENT);
4649     check_dialog_style(WS_CAPTION, WS_EX_STATICEDGE, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_STATICEDGE|WS_EX_WINDOWEDGE|WS_EX_CONTROLPARENT);
4650     check_dialog_style(0, WS_EX_APPWINDOW, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_APPWINDOW|WS_EX_WINDOWEDGE|WS_EX_CONTROLPARENT);
4651 
4652     if (pGetLayeredWindowAttributes)
4653     {
4654         check_dialog_style(0, WS_EX_LAYERED, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_LAYERED|WS_EX_WINDOWEDGE|WS_EX_CONTROLPARENT);
4655         check_dialog_style(0, WS_EX_LAYERED|WS_EX_TRANSPARENT, WS_CLIPSIBLINGS|WS_CAPTION, WS_EX_LAYERED|WS_EX_TRANSPARENT|WS_EX_WINDOWEDGE|WS_EX_CONTROLPARENT);
4656         check_dialog_style(0, WS_EX_LAYERED|WS_EX_TRANSPARENT|WS_EX_TOOLWINDOW, WS_CLIPSIBLINGS|WS_CAPTION,
4657                               WS_EX_LAYERED|WS_EX_TRANSPARENT|WS_EX_TOOLWINDOW|WS_EX_WINDOWEDGE|WS_EX_CONTROLPARENT);
4658     }
4659 }
4660 
4661 struct dlg_parent_param
4662 {
4663     HWND ga_parent;
4664     HWND gwl_parent;
4665     HWND get_parent;
4666     HWND owner;
4667     HWND root;
4668     HWND ga_root_owner;
4669 };
4670 
parent_dlg_proc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)4671 static INT_PTR WINAPI parent_dlg_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
4672 {
4673     if (msg == WM_INITDIALOG) {
4674         struct dlg_parent_param *param = (void*)lparam;
4675         check_parents(hwnd, param->ga_parent, param->gwl_parent, param->get_parent, param->owner,
4676                       param->root ? param->root : hwnd, param->ga_root_owner ? param->ga_root_owner : hwnd);
4677 
4678         ok(!IsWindowEnabled(param->gwl_parent), "parent is not disabled\n");
4679         EndDialog(hwnd, 2);
4680         ok(IsWindowEnabled(param->gwl_parent), "parent is not enabled\n");
4681     }
4682 
4683     return 0;
4684 }
4685 
reparent_dlg_proc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)4686 static INT_PTR WINAPI reparent_dlg_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
4687 {
4688     if (msg == WM_INITDIALOG) {
4689         ok(!IsWindowEnabled(GetParent(hwnd)), "parent is not disabled\n");
4690         SetParent(hwnd, (HWND)lparam);
4691     }
4692 
4693     return 0;
4694 }
4695 
reparent_owned_dlg_proc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)4696 static INT_PTR WINAPI reparent_owned_dlg_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
4697 {
4698     if (msg == WM_INITDIALOG) {
4699         HWND new_parent = (HWND)lparam;
4700         HWND owner = GetWindow(hwnd, GW_OWNER);
4701         ok(!IsWindowEnabled(owner), "owner is not disabled\n");
4702         SetWindowLongA(hwnd, GWL_STYLE, GetWindowLongA(hwnd, GWL_STYLE) | WS_CHILD);
4703         SetParent(hwnd, new_parent);
4704         ok(GetParent(hwnd) == new_parent, "GetParent(hwnd) = %p, expected %p\n", GetParent(hwnd), new_parent);
4705         PostMessageA(hwnd, WM_QUIT, 0, 0);
4706     }
4707 
4708     return 0;
4709 }
4710 
reparent_dialog_owner_proc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)4711 static LRESULT WINAPI reparent_dialog_owner_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
4712 {
4713     if (msg == WM_ENTERIDLE) {
4714         HWND dialog = (HWND)lparam;
4715         HWND owner = GetParent(dialog);
4716         /* EndDialog will enable owner */
4717         EnableWindow(owner, FALSE);
4718         EndDialog(dialog, 2);
4719         ok(IsWindowEnabled(owner), "owner is not enabled\n");
4720         /* ...but it won't be enabled on dialog exit */
4721         EnableWindow(owner, FALSE);
4722     }
4723     return DefWindowProcA( hwnd, msg, wparam, lparam );
4724 }
4725 
post_quit_dialog_owner_proc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)4726 static LRESULT WINAPI post_quit_dialog_owner_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
4727 {
4728     if (msg == WM_ENTERIDLE) {
4729         HWND dialog = (HWND)lparam;
4730         PostMessageA(dialog, WM_QUIT, 0, 0);
4731     }
4732     return DefWindowProcA( hwnd, msg, wparam, lparam );
4733 }
4734 
destroy_dialog_owner_proc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)4735 static LRESULT WINAPI destroy_dialog_owner_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
4736 {
4737     if (msg == WM_ENTERIDLE) {
4738         HWND dialog = (HWND)lparam;
4739         DestroyWindow(dialog);
4740     }
4741     return DefWindowProcA( hwnd, msg, wparam, lparam );
4742 }
4743 
test_dialog_parent(void)4744 static void test_dialog_parent(void)
4745 {
4746     HWND dialog, parent, child, child2, other, desktop = GetDesktopWindow();
4747     struct dlg_parent_param param;
4748     INT_PTR ret;
4749     struct
4750     {
4751         DLGTEMPLATE dt;
4752         WORD menu_name;
4753         WORD class_id;
4754         WORD class_atom;
4755         WCHAR caption[1];
4756     } dlg_data;
4757 
4758     dlg_data.dt.dwExtendedStyle = 0;
4759     dlg_data.dt.cdit = 0;
4760     dlg_data.dt.x = 0;
4761     dlg_data.dt.y = 0;
4762     dlg_data.dt.cx = 100;
4763     dlg_data.dt.cy = 100;
4764     dlg_data.menu_name = 0;
4765     dlg_data.class_id = 0;
4766     dlg_data.class_atom = 0;
4767     dlg_data.caption[0] = 0;
4768 
4769     parent = CreateWindowExA(0, "static", NULL, WS_OVERLAPPEDWINDOW, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
4770     /* Create a child without WS_CHILD flag. It's a valid owner window. */
4771     child = CreateWindowExA(0, "static", NULL, WS_OVERLAPPEDWINDOW, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
4772     SetParent(child, parent);
4773     /* Regular child. If passed as an owner, its parent will be true owner window. */
4774     child2 = CreateWindowExA(0, "static", NULL, WS_CHILD, 0, 0, 0, 0, child, NULL, NULL, NULL);
4775 
4776     trace("parent %p child %p child2 %p desktop %p\n", parent, child, child2, desktop);
4777 
4778     /* When dialog is created with WS_CHILD style, its parent depends on function used to create it. */
4779     dlg_data.dt.style = WS_CHILD;
4780 
4781     /* CreateDialogIndirectParam uses passed parent as dialog parent. */
4782     dialog = CreateDialogIndirectParamA(GetModuleHandleA(NULL), &dlg_data.dt, child2, empty_dlg_proc, 0);
4783     ok(dialog != 0, "dialog creation failed\n");
4784     check_parents(dialog, child2, child2, child2, NULL, parent, child);
4785 
4786     ok(IsWindowEnabled(child2), "child2 is disabled\n");
4787     EnableWindow(child2, FALSE);
4788     EndDialog(dialog, 0);
4789     ok(IsWindowEnabled(child2), "child2 is not enabled\n");
4790     DestroyWindow(dialog);
4791 
4792     /* DialogBoxIndirectParam uses the first parent of passed owner that's not a child window as dialog
4793      * parent (like in case of dialog with owner). */
4794     param.ga_parent = param.gwl_parent = param.get_parent = child;
4795     param.owner = NULL;
4796     param.root = parent;
4797     param.ga_root_owner = child;
4798     ret = DialogBoxIndirectParamA(GetModuleHandleA(NULL), &dlg_data.dt, child2, parent_dlg_proc, (LPARAM)&param);
4799     ok(ret == 2, "DialogBoxIndirectParam returned %ld\n", ret);
4800 
4801     /* Dialogs without WS_CHILD behave as expected, they use passed owner just like CreateWindow does. */
4802     dlg_data.dt.style = WS_OVERLAPPEDWINDOW;
4803 
4804     dialog = CreateDialogIndirectParamA(GetModuleHandleA(NULL), &dlg_data.dt, child2, empty_dlg_proc, 0);
4805     ok(dialog != 0, "dialog creation failed\n");
4806     check_parents(dialog, desktop, child, NULL, child, dialog, dialog);
4807 
4808     ok(IsWindowEnabled(child), "child is disabled\n");
4809     EnableWindow(child, FALSE);
4810     EndDialog(dialog, 0);
4811     ok(IsWindowEnabled(child), "child is not enabled\n");
4812     DestroyWindow(dialog);
4813 
4814     param.ga_parent = desktop;
4815     param.gwl_parent = child;
4816     param.get_parent = NULL;
4817     param.owner = child;
4818     param.root = param.ga_root_owner = NULL;
4819     ret = DialogBoxIndirectParamA(GetModuleHandleA(NULL), &dlg_data.dt, child2, parent_dlg_proc, (LPARAM)&param);
4820     ok(ret == 2, "DialogBoxIndirectParam returned %ld\n", ret);
4821 
4822     other = CreateWindowExA(0, "static", NULL, WS_OVERLAPPEDWINDOW, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
4823     SetWindowLongPtrA(child, GWLP_WNDPROC, (ULONG_PTR)reparent_dialog_owner_proc);
4824 
4825     /* When dialog is created with WS_CHILD|WS_POPUP style, we have an owner. */
4826     dlg_data.dt.style = WS_CHILD|WS_POPUP;
4827 
4828     dialog = CreateDialogIndirectParamA(GetModuleHandleA(NULL), &dlg_data.dt, child2, empty_dlg_proc, 0);
4829     ok(dialog != 0, "dialog creation failed\n");
4830     check_parents(dialog, desktop, child, child, child, dialog, child);
4831 
4832     ok(IsWindowEnabled(child), "child is disabled\n");
4833     EnableWindow(child, FALSE);
4834     EndDialog(dialog, 0);
4835     ok(IsWindowEnabled(child), "child is not enabled\n");
4836     DestroyWindow(dialog);
4837 
4838     param.ga_parent = desktop;
4839     param.gwl_parent = param.get_parent = child;
4840     param.owner = child;
4841     param.root = NULL;
4842     param.ga_root_owner = child;
4843     ret = DialogBoxIndirectParamA(GetModuleHandleA(NULL), &dlg_data.dt, child2, parent_dlg_proc, (LPARAM)&param);
4844     ok(ret == 2, "DialogBoxIndirectParam returned %ld\n", ret);
4845 
4846     /* If we change parent in WM_INITDIALOG for WS_CHILD dialog WM_ENTERIDLE is still sent to the original
4847      * parent. EndDialog will enable the new parent. */
4848     EnableWindow(child, TRUE);
4849     EnableWindow(other, FALSE);
4850     dlg_data.dt.style = WS_CHILD;
4851     ret = DialogBoxIndirectParamA(GetModuleHandleA(NULL), &dlg_data.dt, child2, reparent_dlg_proc, (LPARAM)other);
4852     ok(ret == 2, "DialogBoxIndirectParam returned %ld\n", ret);
4853     ok(!IsWindowEnabled(other), "other is not disabled\n");
4854     ok(!IsWindowEnabled(child), "child is not disabled\n");
4855     ok(IsWindowEnabled(child2), "child2 is not enabled\n");
4856     EnableWindow(child, TRUE);
4857 
4858     /* If we change parent and style in WM_INITDIALOG for dialog with an owner to make it true child
4859      * (thus GetParent() will return the new parent instead of an owner), WM_ENTERIDLE is still sent
4860      * to the original parent. EndDialog will enable the new parent. */
4861     EnableWindow(other, FALSE);
4862     dlg_data.dt.style = WS_OVERLAPPED;
4863     ret = DialogBoxIndirectParamA(GetModuleHandleA(NULL), &dlg_data.dt, child2, reparent_owned_dlg_proc, (LPARAM)other);
4864     ok(ret == 1, "DialogBoxIndirectParam returned %ld\n", ret);
4865     ok(!IsWindowEnabled(other), "other is not disabled\n");
4866     ok(!IsWindowEnabled(child), "child is not disabled\n");
4867     ok(IsWindowEnabled(child2), "child2 is not enabled\n");
4868     EnableWindow(child, TRUE);
4869     EnableWindow(other, TRUE);
4870 
4871     /* Quit dialog message loop by sending WM_QUIT message. Dialog owner is not enabled. */
4872     SetWindowLongPtrA(child, GWLP_WNDPROC, (ULONG_PTR)post_quit_dialog_owner_proc);
4873     ret = DialogBoxIndirectParamA(GetModuleHandleA(NULL), &dlg_data.dt, other, empty_dlg_proc, 0);
4874     ok(ret == 1, "DialogBoxIndirectParam returned %ld\n", ret);
4875     ok(!IsWindowEnabled(other), "other is enabled\n");
4876     EnableWindow(other, TRUE);
4877 
4878     /* Quit dialog message loop by destroying the window. Dialog owner is not enabled. */
4879     SetWindowLongPtrA(child, GWLP_WNDPROC, (ULONG_PTR)destroy_dialog_owner_proc);
4880     ret = DialogBoxIndirectParamA(GetModuleHandleA(NULL), &dlg_data.dt, other, empty_dlg_proc, 0);
4881     ok(ret == 1, "DialogBoxIndirectParam returned %ld\n", ret);
4882     ok(!IsWindowEnabled(other), "other is enabled\n");
4883     EnableWindow(other, TRUE);
4884 
4885     DestroyWindow(parent);
4886 }
4887 
test_scrollwindow(HWND hwnd)4888 static void test_scrollwindow( HWND hwnd)
4889 {
4890     HDC hdc;
4891     RECT rc, rc2, rc3;
4892     COLORREF colr;
4893 
4894     ShowWindow( hwnd, SW_SHOW);
4895     UpdateWindow( hwnd);
4896     flush_events( TRUE );
4897     GetClientRect( hwnd, &rc);
4898     hdc = GetDC( hwnd);
4899     /* test ScrollWindow(Ex) with no clip rectangle */
4900     /* paint the lower half of the window black */
4901     rc2 = rc;
4902     rc2.top = ( rc2.top + rc2.bottom) / 2;
4903     FillRect( hdc, &rc2, GetStockObject(BLACK_BRUSH));
4904     /* paint the upper half of the window white */
4905     rc2.bottom = rc2.top;
4906     rc2.top =0;
4907     FillRect( hdc, &rc2, GetStockObject(WHITE_BRUSH));
4908     /* scroll lower half up */
4909     rc2 = rc;
4910     rc2.top = ( rc2.top + rc2.bottom) / 2;
4911     ScrollWindowEx( hwnd, 0, - rc2.top, &rc2, NULL, NULL, NULL, SW_ERASE);
4912     flush_events(FALSE);
4913     /* expected: black should have scrolled to the upper half */
4914     colr = GetPixel( hdc, (rc2.left+rc2.right)/ 2,  rc2.bottom / 4 );
4915     ok ( colr == 0, "pixel should be black, color is %08x\n", colr);
4916     /* Repeat that test of ScrollWindow(Ex) now with clip rectangle */
4917     /* paint the lower half of the window black */
4918     rc2 = rc;
4919     rc2.top = ( rc2.top + rc2.bottom) / 2;
4920     FillRect( hdc, &rc2, GetStockObject(BLACK_BRUSH));
4921     /* paint the upper half of the window white */
4922     rc2.bottom = rc2.top;
4923     rc2.top =0;
4924     FillRect( hdc, &rc2, GetStockObject(WHITE_BRUSH));
4925     /* scroll lower half up */
4926     rc2 = rc;
4927     rc2.top = ( rc2.top + rc2.bottom) / 2;
4928     rc3 = rc;
4929     rc3.left = rc3.right / 4;
4930     rc3.right -= rc3.right / 4;
4931     ScrollWindowEx( hwnd, 0, - rc2.top, &rc2, &rc3, NULL, NULL, SW_ERASE);
4932     flush_events(FALSE);
4933     /* expected: black should have scrolled to the upper half */
4934     colr = GetPixel( hdc, (rc2.left+rc2.right)/ 2,  rc2.bottom / 4 );
4935     ok ( colr == 0, "pixel should be black, color is %08x\n", colr);
4936 
4937     /* clean up */
4938     ReleaseDC( hwnd, hdc);
4939 }
4940 
test_scrollvalidate(HWND parent)4941 static void test_scrollvalidate( HWND parent)
4942 {
4943     HDC hdc;
4944     HRGN hrgn=CreateRectRgn(0,0,0,0);
4945     HRGN exprgn, tmprgn, clipping;
4946     RECT rc, rcu, cliprc;
4947     /* create two overlapping child windows. The visual region
4948      * of hwnd1 is clipped by the overlapping part of
4949      * hwnd2 because of the WS_CLIPSIBLING style */
4950     HWND hwnd1, hwnd2;
4951 
4952     clipping = CreateRectRgn(0,0,0,0);
4953     tmprgn = CreateRectRgn(0,0,0,0);
4954     exprgn = CreateRectRgn(0,0,0,0);
4955     hwnd2 = CreateWindowExA(0, "static", NULL,
4956             WS_CHILD| WS_VISIBLE | WS_CLIPSIBLINGS | WS_BORDER ,
4957             75, 30, 100, 100, parent, 0, 0, NULL);
4958     hwnd1 = CreateWindowExA(0, "static", NULL,
4959             WS_CHILD| WS_VISIBLE | WS_CLIPSIBLINGS | WS_BORDER ,
4960             25, 50, 100, 100, parent, 0, 0, NULL);
4961     ShowWindow( parent, SW_SHOW);
4962     UpdateWindow( parent);
4963     GetClientRect( hwnd1, &rc);
4964     cliprc=rc;
4965     SetRectRgn( clipping, 10, 10, 90, 90);
4966     hdc = GetDC( hwnd1);
4967     /* for a visual touch */
4968     TextOutA( hdc, 0,10, "0123456789", 10);
4969     ScrollDC( hdc, -10, -5, &rc, &cliprc, hrgn, &rcu);
4970     if (winetest_debug > 0) dump_region(hrgn);
4971     /* create a region with what is expected */
4972     SetRectRgn( exprgn, 39,0,49,74);
4973     SetRectRgn( tmprgn, 88,79,98,93);
4974     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
4975     SetRectRgn( tmprgn, 0,93,98,98);
4976     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
4977     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
4978     trace("update rect is %s\n", wine_dbgstr_rect(&rcu));
4979     /* now with clipping region */
4980     SelectClipRgn( hdc, clipping);
4981     ScrollDC( hdc, -10, -5, &rc, &cliprc, hrgn, &rcu);
4982     if (winetest_debug > 0) dump_region(hrgn);
4983     /* create a region with what is expected */
4984     SetRectRgn( exprgn, 39,10,49,74);
4985     SetRectRgn( tmprgn, 80,79,90,85);
4986     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
4987     SetRectRgn( tmprgn, 10,85,90,90);
4988     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
4989     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
4990     trace("update rect is %s\n", wine_dbgstr_rect(&rcu));
4991     ReleaseDC( hwnd1, hdc);
4992 
4993     /* test scrolling a rect by more than its size */
4994     DestroyWindow( hwnd2);
4995     ValidateRect( hwnd1, NULL);
4996     SetRect( &rc, 40,40, 50,50);
4997     InvalidateRect( hwnd1, &rc, 1);
4998     ScrollWindowEx( hwnd1, -20, 0, &rc, NULL, hrgn, &rcu,
4999       SW_SCROLLCHILDREN | SW_INVALIDATE);
5000     if (winetest_debug > 0) dump_region(hrgn);
5001     SetRectRgn( exprgn, 20, 40, 30, 50);
5002     SetRectRgn( tmprgn, 40, 40, 50, 50);
5003     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
5004     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
5005     ok( rcu.left == 20 && rcu.top == 40 && rcu.right == 50 && rcu.bottom == 50,
5006         "unexpected update rect: %s\n", wine_dbgstr_rect(&rcu));
5007 
5008     /* test scrolling a window with an update region */
5009     ValidateRect( hwnd1, NULL);
5010     SetRect( &rc, 40,40, 50,50);
5011     InvalidateRect( hwnd1, &rc, 1);
5012     GetClientRect( hwnd1, &rc);
5013     cliprc=rc;
5014     ScrollWindowEx( hwnd1, -10, 0, &rc, &cliprc, hrgn, &rcu,
5015       SW_SCROLLCHILDREN | SW_INVALIDATE);
5016     if (winetest_debug > 0) dump_region(hrgn);
5017     SetRectRgn( exprgn, 88,0,98,98);
5018     SetRectRgn( tmprgn, 30, 40, 50, 50);
5019     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
5020     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
5021 
5022     /* clear an update region */
5023     UpdateWindow( hwnd1 );
5024 
5025     SetRect( &rc, 0,40, 100,60);
5026     SetRect( &cliprc, 0,0, 100,100);
5027     ScrollWindowEx( hwnd1, 0, -25, &rc, &cliprc, hrgn, &rcu, SW_INVALIDATE);
5028     if (winetest_debug > 0) dump_region( hrgn );
5029     SetRectRgn( exprgn, 0, 40, 98, 60 );
5030     ok( EqualRgn( exprgn, hrgn), "wrong update region in excessive scroll\n");
5031 
5032     /* now test ScrollWindowEx with a combination of
5033      * WS_CLIPCHILDREN style and SW_SCROLLCHILDREN flag */
5034     /* make hwnd2 the child of hwnd1 */
5035     hwnd2 = CreateWindowExA(0, "static", NULL,
5036             WS_CHILD| WS_VISIBLE | WS_BORDER ,
5037             50, 50, 100, 100, hwnd1, 0, 0, NULL);
5038     SetWindowLongA( hwnd1, GWL_STYLE, GetWindowLongA( hwnd1, GWL_STYLE) & ~WS_CLIPSIBLINGS);
5039     GetClientRect( hwnd1, &rc);
5040     cliprc=rc;
5041 
5042     /* WS_CLIPCHILDREN and SW_SCROLLCHILDREN */
5043     SetWindowLongA( hwnd1, GWL_STYLE, GetWindowLongA( hwnd1, GWL_STYLE) | WS_CLIPCHILDREN );
5044     ValidateRect( hwnd1, NULL);
5045     ValidateRect( hwnd2, NULL);
5046     ScrollWindowEx( hwnd1, -10, -10, &rc, &cliprc, hrgn, &rcu,
5047       SW_SCROLLCHILDREN | SW_INVALIDATE);
5048     if (winetest_debug > 0) dump_region(hrgn);
5049     SetRectRgn( exprgn, 88,0,98,88);
5050     SetRectRgn( tmprgn, 0,88,98,98);
5051     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
5052     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
5053 
5054     /* SW_SCROLLCHILDREN */
5055     SetWindowLongA( hwnd1, GWL_STYLE, GetWindowLongA( hwnd1, GWL_STYLE) & ~WS_CLIPCHILDREN );
5056     ValidateRect( hwnd1, NULL);
5057     ValidateRect( hwnd2, NULL);
5058     ScrollWindowEx( hwnd1, -10, -10, &rc, &cliprc, hrgn, &rcu, SW_SCROLLCHILDREN | SW_INVALIDATE);
5059     if (winetest_debug > 0) dump_region(hrgn);
5060     /* expected region is the same as in previous test */
5061     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
5062 
5063     /* no SW_SCROLLCHILDREN */
5064     SetWindowLongA( hwnd1, GWL_STYLE, GetWindowLongA( hwnd1, GWL_STYLE) & ~WS_CLIPCHILDREN );
5065     ValidateRect( hwnd1, NULL);
5066     ValidateRect( hwnd2, NULL);
5067     ScrollWindowEx( hwnd1, -10, -10, &rc, &cliprc, hrgn, &rcu, SW_INVALIDATE);
5068     if (winetest_debug > 0) dump_region(hrgn);
5069     /* expected region is the same as in previous test */
5070     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
5071 
5072     /* WS_CLIPCHILDREN and no SW_SCROLLCHILDREN */
5073     SetWindowLongA( hwnd1, GWL_STYLE, GetWindowLongA( hwnd1, GWL_STYLE) | WS_CLIPCHILDREN );
5074     ValidateRect( hwnd1, NULL);
5075     ValidateRect( hwnd2, NULL);
5076     ScrollWindowEx( hwnd1, -10, -10, &rc, &cliprc, hrgn, &rcu, SW_INVALIDATE);
5077     if (winetest_debug > 0) dump_region(hrgn);
5078     SetRectRgn( exprgn, 88,0,98,20);
5079     SetRectRgn( tmprgn, 20,20,98,30);
5080     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
5081     SetRectRgn( tmprgn, 20,30,30,88);
5082     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
5083     SetRectRgn( tmprgn, 0,88,30,98);
5084     CombineRgn( exprgn, exprgn, tmprgn, RGN_OR);
5085     ok( EqualRgn( exprgn, hrgn), "wrong update region\n");
5086 
5087     /* clean up */
5088     DeleteObject( hrgn);
5089     DeleteObject( exprgn);
5090     DeleteObject( tmprgn);
5091     DestroyWindow( hwnd1);
5092     DestroyWindow( hwnd2);
5093 }
5094 
5095 /* couple of tests of return values of scrollbar functions
5096  * called on a scrollbarless window */
test_scroll(void)5097 static void test_scroll(void)
5098 {
5099     BOOL ret;
5100     INT min, max;
5101     SCROLLINFO si;
5102     HWND hwnd = CreateWindowExA(0, "Static", "Wine test window",
5103         WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_POPUP,
5104         100, 100, 200, 200, 0, 0, 0, NULL);
5105     /* horizontal */
5106     ret = GetScrollRange( hwnd, SB_HORZ, &min, &max);
5107     if (!ret)  /* win9x */
5108     {
5109         win_skip( "GetScrollRange doesn't work\n" );
5110         DestroyWindow( hwnd);
5111         return;
5112     }
5113     ok( min == 0, "minimum scroll pos is %d (should be zero)\n", min);
5114     ok( max == 0, "maximum scroll pos is %d (should be zero)\n", min);
5115     si.cbSize = sizeof( si);
5116     si.fMask = SIF_PAGE;
5117     si.nPage = 0xdeadbeef;
5118     ret = GetScrollInfo( hwnd, SB_HORZ, &si);
5119     ok( !ret, "GetScrollInfo returns %d (should be zero)\n", ret);
5120     ok( si.nPage == 0xdeadbeef, "unexpected value for nPage is %d\n", si.nPage);
5121     /* vertical */
5122     ret = GetScrollRange( hwnd, SB_VERT, &min, &max);
5123     ok( ret, "GetScrollRange returns FALSE\n");
5124     ok( min == 0, "minimum scroll pos is %d (should be zero)\n", min);
5125     ok( max == 0, "maximum scroll pos is %d (should be zero)\n", min);
5126     si.cbSize = sizeof( si);
5127     si.fMask = SIF_PAGE;
5128     si.nPage = 0xdeadbeef;
5129     ret = GetScrollInfo( hwnd, SB_VERT, &si);
5130     ok( !ret, "GetScrollInfo returns %d (should be zero)\n", ret);
5131     ok( si.nPage == 0xdeadbeef, "unexpected value for nPage is %d\n", si.nPage);
5132     /* clean up */
5133     DestroyWindow( hwnd);
5134 }
5135 
test_scrolldc(HWND parent)5136 static void test_scrolldc( HWND parent)
5137 {
5138     HDC hdc;
5139     HRGN exprgn, tmprgn, hrgn;
5140     RECT rc, rc2, rcu, cliprc;
5141     HWND hwnd1;
5142     COLORREF colr;
5143 
5144     hrgn = CreateRectRgn(0,0,0,0);
5145     tmprgn = CreateRectRgn(0,0,0,0);
5146     exprgn = CreateRectRgn(0,0,0,0);
5147 
5148     hwnd1 = CreateWindowExA(0, "static", NULL,
5149             WS_CHILD| WS_VISIBLE,
5150             25, 50, 100, 100, parent, 0, 0, NULL);
5151     ShowWindow( parent, SW_SHOW);
5152     UpdateWindow( parent);
5153     flush_events( TRUE );
5154     GetClientRect( hwnd1, &rc);
5155     hdc = GetDC( hwnd1);
5156     /* paint the upper half of the window black */
5157     rc2 = rc;
5158     rc2.bottom = ( rc.top + rc.bottom) /2;
5159     FillRect( hdc, &rc2, GetStockObject(BLACK_BRUSH));
5160     /* clip region is the lower half */
5161     cliprc=rc;
5162     cliprc.top = (rc.top + rc.bottom) /2;
5163     /* test whether scrolled pixels are properly clipped */
5164     colr = GetPixel( hdc, (rc.left+rc.right)/2, ( rc.top + rc.bottom) /2 - 1);
5165     ok ( colr == 0, "pixel should be black, color is %08x\n", colr);
5166     /* this scroll should not cause any visible changes */
5167     ScrollDC( hdc, 5, -20, &rc, &cliprc, hrgn, &rcu);
5168     colr = GetPixel( hdc, (rc.left+rc.right)/2, ( rc.top + rc.bottom) /2 - 1);
5169     ok ( colr == 0, "pixel should be black, color is %08x\n", colr);
5170     /* test with NULL clip rect */
5171     ScrollDC( hdc, 20, -20, &rc, NULL, hrgn, &rcu);
5172     /*FillRgn(hdc, hrgn, GetStockObject(WHITE_BRUSH));*/
5173     trace("update rect: %s\n", wine_dbgstr_rect(&rcu));
5174     if (winetest_debug > 0) dump_region(hrgn);
5175     SetRect(&rc2, 0, 0, 100, 100);
5176     ok(EqualRect(&rcu, &rc2), "rects do not match %s / %s\n", wine_dbgstr_rect(&rcu),
5177        wine_dbgstr_rect(&rc2));
5178 
5179     SetRectRgn( exprgn, 0, 0, 20, 80);
5180     SetRectRgn( tmprgn, 0, 80, 100, 100);
5181     CombineRgn(exprgn, exprgn, tmprgn, RGN_OR);
5182     if (winetest_debug > 0) dump_region(exprgn);
5183     ok(EqualRgn(exprgn, hrgn), "wrong update region\n");
5184     /* test clip rect > scroll rect */
5185     FillRect( hdc, &rc, GetStockObject(WHITE_BRUSH));
5186     rc2=rc;
5187     InflateRect( &rc2, -(rc.right-rc.left)/4, -(rc.bottom-rc.top)/4);
5188     FillRect( hdc, &rc2, GetStockObject(BLACK_BRUSH));
5189     ScrollDC( hdc, 10, 10, &rc2, &rc, hrgn, &rcu);
5190     SetRectRgn( exprgn, 25, 25, 75, 35);
5191     SetRectRgn( tmprgn, 25, 35, 35, 75);
5192     CombineRgn(exprgn, exprgn, tmprgn, RGN_OR);
5193     ok(EqualRgn(exprgn, hrgn), "wrong update region\n");
5194     trace("update rect: %s\n", wine_dbgstr_rect(&rcu));
5195     if (winetest_debug > 0) dump_region(hrgn);
5196 
5197     /* clean up */
5198     DeleteObject(hrgn);
5199     DeleteObject(exprgn);
5200     DeleteObject(tmprgn);
5201     DestroyWindow(hwnd1);
5202 }
5203 
test_params(void)5204 static void test_params(void)
5205 {
5206     HWND hwnd;
5207     INT rc;
5208 
5209     ok(!IsWindow(0), "IsWindow(0)\n");
5210     ok(!IsWindow(HWND_BROADCAST), "IsWindow(HWND_BROADCAST)\n");
5211     ok(!IsWindow(HWND_TOPMOST), "IsWindow(HWND_TOPMOST)\n");
5212 
5213     /* Just a param check */
5214     if (pGetMonitorInfoA)
5215     {
5216         SetLastError(0xdeadbeef);
5217         rc = GetWindowTextA(hwndMain2, NULL, 1024);
5218         ok( rc==0, "GetWindowText: rc=%d err=%d\n",rc,GetLastError());
5219     }
5220     else
5221     {
5222         /* Skips actually on Win95 and NT4 */
5223         win_skip("Test would crash on Win95\n");
5224     }
5225 
5226     SetLastError(0xdeadbeef);
5227     hwnd=CreateWindowA("LISTBOX", "TestList",
5228                       (LBS_STANDARD & ~LBS_SORT),
5229                       0, 0, 100, 100,
5230                       NULL, (HMENU)1, NULL, 0);
5231 
5232     ok(!hwnd || broken(hwnd != NULL), /* w2k3 sp2 */
5233        "CreateWindow with invalid menu handle should fail\n");
5234     if (!hwnd)
5235         ok(GetLastError() == ERROR_INVALID_MENU_HANDLE || /* NT */
5236            GetLastError() == 0xdeadbeef, /* Win9x */
5237            "wrong last error value %d\n", GetLastError());
5238 }
5239 
test_AWRwindow(LPCSTR class,LONG style,LONG exStyle,BOOL menu)5240 static void test_AWRwindow(LPCSTR class, LONG style, LONG exStyle, BOOL menu)
5241 {
5242     HWND hwnd = 0;
5243 
5244     hwnd = CreateWindowExA(exStyle, class, class, style,
5245 			  110, 100,
5246 			  225, 200,
5247 			  0,
5248 			  menu ? hmenu : 0,
5249 			  0, 0);
5250     if (!hwnd) {
5251 	trace("Failed to create window class=%s, style=0x%08x, exStyle=0x%08x\n", class, style, exStyle);
5252         return;
5253     }
5254     ShowWindow(hwnd, SW_SHOW);
5255 
5256     test_nonclient_area(hwnd);
5257 
5258     SetMenu(hwnd, 0);
5259     DestroyWindow(hwnd);
5260 }
5261 
AWR_init(void)5262 static BOOL AWR_init(void)
5263 {
5264     WNDCLASSA class;
5265 
5266     class.style         = CS_HREDRAW | CS_VREDRAW;
5267     class.lpfnWndProc     = DefWindowProcA;
5268     class.cbClsExtra    = 0;
5269     class.cbWndExtra    = 0;
5270     class.hInstance     = 0;
5271     class.hIcon         = LoadIconA(0, (LPCSTR)IDI_APPLICATION);
5272     class.hCursor       = LoadCursorA(0, (LPCSTR)IDC_ARROW);
5273     class.hbrBackground = 0;
5274     class.lpszMenuName  = 0;
5275     class.lpszClassName = szAWRClass;
5276 
5277     if (!RegisterClassA(&class)) {
5278 	ok(FALSE, "RegisterClass failed\n");
5279 	return FALSE;
5280     }
5281 
5282     hmenu = CreateMenu();
5283     if (!hmenu)
5284 	return FALSE;
5285     ok(hmenu != 0, "Failed to create menu\n");
5286     ok(AppendMenuA(hmenu, MF_STRING, 1, "Test!"), "Failed to create menu item\n");
5287 
5288     return TRUE;
5289 }
5290 
5291 
test_AWR_window_size(BOOL menu)5292 static void test_AWR_window_size(BOOL menu)
5293 {
5294     static const DWORD styles[] = {
5295         WS_POPUP, WS_MAXIMIZE, WS_BORDER, WS_DLGFRAME, WS_CAPTION, WS_SYSMENU,
5296         WS_THICKFRAME, WS_MINIMIZEBOX, WS_MAXIMIZEBOX, WS_HSCROLL, WS_VSCROLL
5297     };
5298     static const DWORD exStyles[] = {
5299 	WS_EX_CLIENTEDGE, WS_EX_TOOLWINDOW, WS_EX_WINDOWEDGE, WS_EX_APPWINDOW,
5300 	WS_EX_DLGMODALFRAME, WS_EX_DLGMODALFRAME | WS_EX_STATICEDGE
5301     };
5302 
5303     unsigned int i;
5304 
5305     /* A exhaustive check of all the styles takes too long
5306      * so just do a (hopefully representative) sample
5307      */
5308     for (i = 0; i < COUNTOF(styles); ++i)
5309         test_AWRwindow(szAWRClass, styles[i], 0, menu);
5310     for (i = 0; i < COUNTOF(exStyles); ++i) {
5311         test_AWRwindow(szAWRClass, WS_POPUP, exStyles[i], menu);
5312         test_AWRwindow(szAWRClass, WS_THICKFRAME, exStyles[i], menu);
5313     }
5314 }
5315 
test_AWR_flags(void)5316 static void test_AWR_flags(void)
5317 {
5318     static const DWORD styles[] = { WS_POPUP, WS_BORDER, WS_DLGFRAME, WS_THICKFRAME };
5319     static const DWORD exStyles[] = { WS_EX_CLIENTEDGE, WS_EX_TOOLWINDOW, WS_EX_WINDOWEDGE,
5320                                       WS_EX_APPWINDOW, WS_EX_DLGMODALFRAME, WS_EX_STATICEDGE };
5321 
5322     DWORD i, j, k, style, exstyle;
5323     RECT rect, rect2;
5324 
5325     for (i = 0; i < (1 << COUNTOF(styles)); i++)
5326     {
5327         for (k = style = 0; k < COUNTOF(styles); k++) if (i & (1 << k)) style |= styles[k];
5328 
5329         for (j = 0; j < (1 << COUNTOF(exStyles)); j++)
5330         {
5331             for (k = exstyle = 0; k < COUNTOF(exStyles); k++) if (j & (1 << k)) exstyle |= exStyles[k];
5332             SetRect( &rect, 100, 100, 200, 200 );
5333             rect2 = rect;
5334             AdjustWindowRectEx( &rect, style, FALSE, exstyle );
5335             wine_AdjustWindowRectEx( &rect2, style, FALSE, exstyle );
5336             ok( EqualRect( &rect, &rect2 ), "rects do not match: win %s wine %s\n",
5337                 wine_dbgstr_rect( &rect ), wine_dbgstr_rect( &rect2 ));
5338         }
5339     }
5340 }
5341 #undef COUNTOF
5342 
5343 #define SHOWSYSMETRIC(SM) trace(#SM "=%d\n", GetSystemMetrics(SM))
5344 
test_AdjustWindowRect(void)5345 static void test_AdjustWindowRect(void)
5346 {
5347     if (!AWR_init())
5348 	return;
5349 
5350     SHOWSYSMETRIC(SM_CYCAPTION);
5351     SHOWSYSMETRIC(SM_CYSMCAPTION);
5352     SHOWSYSMETRIC(SM_CYMENU);
5353     SHOWSYSMETRIC(SM_CXEDGE);
5354     SHOWSYSMETRIC(SM_CYEDGE);
5355     SHOWSYSMETRIC(SM_CXVSCROLL);
5356     SHOWSYSMETRIC(SM_CYHSCROLL);
5357     SHOWSYSMETRIC(SM_CXFRAME);
5358     SHOWSYSMETRIC(SM_CYFRAME);
5359     SHOWSYSMETRIC(SM_CXDLGFRAME);
5360     SHOWSYSMETRIC(SM_CYDLGFRAME);
5361     SHOWSYSMETRIC(SM_CXBORDER);
5362     SHOWSYSMETRIC(SM_CYBORDER);
5363 
5364     test_AWR_window_size(FALSE);
5365     test_AWR_window_size(TRUE);
5366     test_AWR_flags();
5367 
5368     DestroyMenu(hmenu);
5369 }
5370 #undef SHOWSYSMETRIC
5371 
5372 
5373 /* Global variables to trigger exit from loop */
5374 static int redrawComplete, WMPAINT_count;
5375 
redraw_window_procA(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)5376 static LRESULT WINAPI redraw_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
5377 {
5378     switch (msg)
5379     {
5380     case WM_PAINT:
5381         trace("doing WM_PAINT %d\n", WMPAINT_count);
5382         WMPAINT_count++;
5383         if (WMPAINT_count > 10 && redrawComplete == 0) {
5384             PAINTSTRUCT ps;
5385             BeginPaint(hwnd, &ps);
5386             EndPaint(hwnd, &ps);
5387             return 1;
5388         }
5389         return 0;
5390     }
5391     return DefWindowProcA(hwnd, msg, wparam, lparam);
5392 }
5393 
5394 /* Ensure we exit from RedrawNow regardless of invalidated area */
test_redrawnow(void)5395 static void test_redrawnow(void)
5396 {
5397    WNDCLASSA cls;
5398    HWND hwndMain;
5399 
5400    cls.style = CS_DBLCLKS;
5401    cls.lpfnWndProc = redraw_window_procA;
5402    cls.cbClsExtra = 0;
5403    cls.cbWndExtra = 0;
5404    cls.hInstance = GetModuleHandleA(0);
5405    cls.hIcon = 0;
5406    cls.hCursor = LoadCursorA(0, (LPCSTR)IDC_ARROW);
5407    cls.hbrBackground = GetStockObject(WHITE_BRUSH);
5408    cls.lpszMenuName = NULL;
5409    cls.lpszClassName = "RedrawWindowClass";
5410 
5411    if(!RegisterClassA(&cls)) {
5412        trace("Register failed %d\n", GetLastError());
5413        return;
5414    }
5415 
5416    hwndMain = CreateWindowA("RedrawWindowClass", "Main Window", WS_OVERLAPPEDWINDOW,
5417                             CW_USEDEFAULT, 0, 100, 100, NULL, NULL, 0, NULL);
5418 
5419    ok( WMPAINT_count == 0, "Multiple unexpected WM_PAINT calls %d\n", WMPAINT_count);
5420    ShowWindow(hwndMain, SW_SHOW);
5421    ok( WMPAINT_count == 0, "Multiple unexpected WM_PAINT calls %d\n", WMPAINT_count);
5422    RedrawWindow(hwndMain, NULL,NULL,RDW_UPDATENOW | RDW_ALLCHILDREN);
5423    ok( WMPAINT_count == 1 || broken(WMPAINT_count == 0), /* sometimes on win9x */
5424        "Multiple unexpected WM_PAINT calls %d\n", WMPAINT_count);
5425    redrawComplete = TRUE;
5426    ok( WMPAINT_count < 10, "RedrawWindow (RDW_UPDATENOW) never completed (%d)\n", WMPAINT_count);
5427 
5428    /* clean up */
5429    DestroyWindow( hwndMain);
5430 }
5431 
5432 struct parentdc_stat {
5433     RECT client;
5434     RECT clip;
5435     RECT paint;
5436 };
5437 
5438 struct parentdc_test {
5439    struct parentdc_stat main, main_todo;
5440    struct parentdc_stat child1, child1_todo;
5441    struct parentdc_stat child2, child2_todo;
5442 };
5443 
parentdc_window_procA(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)5444 static LRESULT WINAPI parentdc_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
5445 {
5446     RECT rc;
5447     PAINTSTRUCT ps;
5448 
5449     struct parentdc_stat *t = (struct parentdc_stat *)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
5450 
5451     switch (msg)
5452     {
5453     case WM_PAINT:
5454         GetClientRect(hwnd, &rc);
5455         t->client = rc;
5456         GetWindowRect(hwnd, &rc);
5457         trace("WM_PAINT: hwnd %p, client rect %s, window rect %s\n", hwnd,
5458               wine_dbgstr_rect(&t->client), wine_dbgstr_rect(&rc));
5459         BeginPaint(hwnd, &ps);
5460         t->paint = ps.rcPaint;
5461         GetClipBox(ps.hdc, &rc);
5462         t->clip = rc;
5463         trace("clip rect %s, paint rect %s\n", wine_dbgstr_rect(&rc),
5464               wine_dbgstr_rect(&ps.rcPaint));
5465         EndPaint(hwnd, &ps);
5466         return 0;
5467     }
5468     return DefWindowProcA(hwnd, msg, wparam, lparam);
5469 }
5470 
zero_parentdc_stat(struct parentdc_stat * t)5471 static void zero_parentdc_stat(struct parentdc_stat *t)
5472 {
5473     SetRectEmpty(&t->client);
5474     SetRectEmpty(&t->clip);
5475     SetRectEmpty(&t->paint);
5476 }
5477 
zero_parentdc_test(struct parentdc_test * t)5478 static void zero_parentdc_test(struct parentdc_test *t)
5479 {
5480     zero_parentdc_stat(&t->main);
5481     zero_parentdc_stat(&t->child1);
5482     zero_parentdc_stat(&t->child2);
5483 }
5484 
5485 #define parentdc_field_ok(t, w, r, f, got) \
5486   ok (t.w.r.f==got.w.r.f, "window " #w ", rect " #r ", field " #f \
5487       ": expected %d, got %d\n", \
5488       t.w.r.f, got.w.r.f)
5489 
5490 #define parentdc_todo_field_ok(t, w, r, f, got) \
5491   todo_wine_if (t.w##_todo.r.f) \
5492       parentdc_field_ok(t, w, r, f, got);
5493 
5494 #define parentdc_rect_ok(t, w, r, got) \
5495   parentdc_todo_field_ok(t, w, r, left, got); \
5496   parentdc_todo_field_ok(t, w, r, top, got); \
5497   parentdc_todo_field_ok(t, w, r, right, got); \
5498   parentdc_todo_field_ok(t, w, r, bottom, got);
5499 
5500 #define parentdc_win_ok(t, w, got) \
5501   parentdc_rect_ok(t, w, client, got); \
5502   parentdc_rect_ok(t, w, clip, got); \
5503   parentdc_rect_ok(t, w, paint, got);
5504 
5505 #define parentdc_ok(t, got) \
5506   parentdc_win_ok(t, main, got); \
5507   parentdc_win_ok(t, child1, got); \
5508   parentdc_win_ok(t, child2, got);
5509 
test_csparentdc(void)5510 static void test_csparentdc(void)
5511 {
5512    WNDCLASSA clsMain, cls;
5513    HWND hwndMain, hwnd1, hwnd2;
5514    RECT rc;
5515 
5516    struct parentdc_test test_answer;
5517 
5518 #define nothing_todo {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}
5519    const struct parentdc_test test1 =
5520    {
5521         {{0, 0, 150, 150}, {0, 0, 150, 150}, {0, 0, 150, 150}}, nothing_todo,
5522         {{0, 0, 40, 40}, {-20, -20, 130, 130}, {0, 0, 40, 40}}, {{0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}},
5523         {{0, 0, 40, 40}, {-40, -40, 110, 110}, {0, 0, 40, 40}}, {{0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}},
5524    };
5525 
5526    const struct parentdc_test test2 =
5527    {
5528         {{0, 0, 150, 150}, {0, 0, 50, 50}, {0, 0, 50, 50}}, nothing_todo,
5529         {{0, 0, 40, 40}, {-20, -20, 30, 30}, {0, 0, 30, 30}}, {{0, 0, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}},
5530         {{0, 0, 40, 40}, {-40, -40, 10, 10}, {0, 0, 10, 10}}, {{0, 0, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}},
5531    };
5532 
5533    const struct parentdc_test test3 =
5534    {
5535         {{0, 0, 150, 150}, {0, 0, 10, 10}, {0, 0, 10, 10}}, nothing_todo,
5536         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
5537         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
5538    };
5539 
5540    const struct parentdc_test test4 =
5541    {
5542         {{0, 0, 150, 150}, {40, 40, 50, 50}, {40, 40, 50, 50}}, nothing_todo,
5543         {{0, 0, 40, 40}, {20, 20, 30, 30}, {20, 20, 30, 30}}, nothing_todo,
5544         {{0, 0, 40, 40}, {0, 0, 10, 10}, {0, 0, 10, 10}}, nothing_todo,
5545    };
5546 
5547    const struct parentdc_test test5 =
5548    {
5549         {{0, 0, 150, 150}, {20, 20, 60, 60}, {20, 20, 60, 60}}, nothing_todo,
5550         {{0, 0, 40, 40}, {-20, -20, 130, 130}, {0, 0, 40, 40}}, {{0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}},
5551         {{0, 0, 40, 40}, {-20, -20, 20, 20}, {0, 0, 20, 20}}, {{0, 0, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}},
5552    };
5553 
5554    const struct parentdc_test test6 =
5555    {
5556         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
5557         {{0, 0, 40, 40}, {0, 0, 10, 10}, {0, 0, 10, 10}}, nothing_todo,
5558         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
5559    };
5560 
5561    const struct parentdc_test test7 =
5562    {
5563         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
5564         {{0, 0, 40, 40}, {-20, -20, 130, 130}, {0, 0, 40, 40}}, {{0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}},
5565         {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, nothing_todo,
5566    };
5567 #undef nothing_todo
5568 
5569    clsMain.style = CS_DBLCLKS;
5570    clsMain.lpfnWndProc = parentdc_window_procA;
5571    clsMain.cbClsExtra = 0;
5572    clsMain.cbWndExtra = 0;
5573    clsMain.hInstance = GetModuleHandleA(0);
5574    clsMain.hIcon = 0;
5575    clsMain.hCursor = LoadCursorA(0, (LPCSTR)IDC_ARROW);
5576    clsMain.hbrBackground = GetStockObject(WHITE_BRUSH);
5577    clsMain.lpszMenuName = NULL;
5578    clsMain.lpszClassName = "ParentDcMainWindowClass";
5579 
5580    if(!RegisterClassA(&clsMain)) {
5581        trace("Register failed %d\n", GetLastError());
5582        return;
5583    }
5584 
5585    cls.style = CS_DBLCLKS | CS_PARENTDC;
5586    cls.lpfnWndProc = parentdc_window_procA;
5587    cls.cbClsExtra = 0;
5588    cls.cbWndExtra = 0;
5589    cls.hInstance = GetModuleHandleA(0);
5590    cls.hIcon = 0;
5591    cls.hCursor = LoadCursorA(0, (LPCSTR)IDC_ARROW);
5592    cls.hbrBackground = GetStockObject(WHITE_BRUSH);
5593    cls.lpszMenuName = NULL;
5594    cls.lpszClassName = "ParentDcWindowClass";
5595 
5596    if(!RegisterClassA(&cls)) {
5597        trace("Register failed %d\n", GetLastError());
5598        return;
5599    }
5600 
5601    SetRect(&rc, 0, 0, 150, 150);
5602    AdjustWindowRectEx(&rc, WS_OVERLAPPEDWINDOW, FALSE, 0);
5603    hwndMain = CreateWindowA("ParentDcMainWindowClass", "Main Window", WS_OVERLAPPEDWINDOW,
5604                             CW_USEDEFAULT, 0, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, 0, NULL);
5605    SetWindowLongPtrA(hwndMain, GWLP_USERDATA, (DWORD_PTR)&test_answer.main);
5606    hwnd1 = CreateWindowA("ParentDcWindowClass", "Child Window 1", WS_CHILD,
5607                             20, 20, 40, 40, hwndMain, NULL, 0, NULL);
5608    SetWindowLongPtrA(hwnd1, GWLP_USERDATA, (DWORD_PTR)&test_answer.child1);
5609    hwnd2 = CreateWindowA("ParentDcWindowClass", "Child Window 2", WS_CHILD,
5610                             40, 40, 40, 40, hwndMain, NULL, 0, NULL);
5611    SetWindowLongPtrA(hwnd2, GWLP_USERDATA, (DWORD_PTR)&test_answer.child2);
5612    ShowWindow(hwndMain, SW_SHOW);
5613    ShowWindow(hwnd1, SW_SHOW);
5614    ShowWindow(hwnd2, SW_SHOW);
5615    SetWindowPos(hwndMain, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
5616    flush_events( TRUE );
5617 
5618    zero_parentdc_test(&test_answer);
5619    InvalidateRect(hwndMain, NULL, TRUE);
5620    flush_events( TRUE );
5621    parentdc_ok(test1, test_answer);
5622 
5623    zero_parentdc_test(&test_answer);
5624    SetRect(&rc, 0, 0, 50, 50);
5625    InvalidateRect(hwndMain, &rc, TRUE);
5626    flush_events( TRUE );
5627    parentdc_ok(test2, test_answer);
5628 
5629    zero_parentdc_test(&test_answer);
5630    SetRect(&rc, 0, 0, 10, 10);
5631    InvalidateRect(hwndMain, &rc, TRUE);
5632    flush_events( TRUE );
5633    parentdc_ok(test3, test_answer);
5634 
5635    zero_parentdc_test(&test_answer);
5636    SetRect(&rc, 40, 40, 50, 50);
5637    InvalidateRect(hwndMain, &rc, TRUE);
5638    flush_events( TRUE );
5639    parentdc_ok(test4, test_answer);
5640 
5641    zero_parentdc_test(&test_answer);
5642    SetRect(&rc, 20, 20, 60, 60);
5643    InvalidateRect(hwndMain, &rc, TRUE);
5644    flush_events( TRUE );
5645    parentdc_ok(test5, test_answer);
5646 
5647    zero_parentdc_test(&test_answer);
5648    SetRect(&rc, 0, 0, 10, 10);
5649    InvalidateRect(hwnd1, &rc, TRUE);
5650    flush_events( TRUE );
5651    parentdc_ok(test6, test_answer);
5652 
5653    zero_parentdc_test(&test_answer);
5654    SetRect(&rc, -5, -5, 65, 65);
5655    InvalidateRect(hwnd1, &rc, TRUE);
5656    flush_events( TRUE );
5657    parentdc_ok(test7, test_answer);
5658 
5659    DestroyWindow(hwndMain);
5660    DestroyWindow(hwnd1);
5661    DestroyWindow(hwnd2);
5662 }
5663 
def_window_procA(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)5664 static LRESULT WINAPI def_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
5665 {
5666     return DefWindowProcA(hwnd, msg, wparam, lparam);
5667 }
5668 
def_window_procW(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)5669 static LRESULT WINAPI def_window_procW(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
5670 {
5671     return DefWindowProcW(hwnd, msg, wparam, lparam);
5672 }
5673 
test_IsWindowUnicode(void)5674 static void test_IsWindowUnicode(void)
5675 {
5676     static const char ansi_class_nameA[] = "ansi class name";
5677     static const WCHAR ansi_class_nameW[] = {'a','n','s','i',' ','c','l','a','s','s',' ','n','a','m','e',0};
5678     static const char unicode_class_nameA[] = "unicode class name";
5679     static const WCHAR unicode_class_nameW[] = {'u','n','i','c','o','d','e',' ','c','l','a','s','s',' ','n','a','m','e',0};
5680     WNDCLASSA classA;
5681     WNDCLASSW classW;
5682     HWND hwnd;
5683     ATOM atom;
5684 
5685     memset(&classW, 0, sizeof(classW));
5686     classW.hInstance = GetModuleHandleA(0);
5687     classW.lpfnWndProc = def_window_procW;
5688     classW.lpszClassName = unicode_class_nameW;
5689     if (!RegisterClassW(&classW)) return; /* this catches Win9x as well */
5690 
5691     memset(&classA, 0, sizeof(classA));
5692     classA.hInstance = GetModuleHandleA(0);
5693     classA.lpfnWndProc = def_window_procA;
5694     classA.lpszClassName = ansi_class_nameA;
5695     atom = RegisterClassA(&classA);
5696     assert(atom);
5697 
5698     /* unicode class: window proc */
5699     hwnd = CreateWindowExW(0, unicode_class_nameW, NULL, WS_POPUP,
5700                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
5701     assert(hwnd);
5702 
5703     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
5704     SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (ULONG_PTR)def_window_procA);
5705     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
5706     SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (ULONG_PTR)def_window_procW);
5707     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
5708 
5709     DestroyWindow(hwnd);
5710 
5711     hwnd = CreateWindowExA(0, unicode_class_nameA, NULL, WS_POPUP,
5712                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
5713     assert(hwnd);
5714 
5715     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
5716     SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (ULONG_PTR)def_window_procA);
5717     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
5718     SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (ULONG_PTR)def_window_procW);
5719     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
5720 
5721     DestroyWindow(hwnd);
5722 
5723     /* ansi class: window proc */
5724     hwnd = CreateWindowExW(0, ansi_class_nameW, NULL, WS_POPUP,
5725                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
5726     assert(hwnd);
5727 
5728     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
5729     SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (ULONG_PTR)def_window_procW);
5730     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
5731     SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (ULONG_PTR)def_window_procA);
5732     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
5733 
5734     DestroyWindow(hwnd);
5735 
5736     hwnd = CreateWindowExA(0, ansi_class_nameA, NULL, WS_POPUP,
5737                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
5738     assert(hwnd);
5739 
5740     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
5741     SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (ULONG_PTR)def_window_procW);
5742     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
5743     SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (ULONG_PTR)def_window_procA);
5744     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
5745 
5746     DestroyWindow(hwnd);
5747 
5748     /* unicode class: class proc */
5749     hwnd = CreateWindowExW(0, unicode_class_nameW, NULL, WS_POPUP,
5750                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
5751     assert(hwnd);
5752 
5753     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
5754     SetClassLongPtrA(hwnd, GCLP_WNDPROC, (ULONG_PTR)def_window_procA);
5755     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
5756     /* do not restore class window proc back to unicode */
5757 
5758     DestroyWindow(hwnd);
5759 
5760     hwnd = CreateWindowExA(0, unicode_class_nameA, NULL, WS_POPUP,
5761                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
5762     assert(hwnd);
5763 
5764     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
5765     SetClassLongPtrW(hwnd, GCLP_WNDPROC, (ULONG_PTR)def_window_procW);
5766     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
5767 
5768     DestroyWindow(hwnd);
5769 
5770     /* ansi class: class proc */
5771     hwnd = CreateWindowExW(0, ansi_class_nameW, NULL, WS_POPUP,
5772                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
5773     assert(hwnd);
5774 
5775     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
5776     SetClassLongPtrW(hwnd, GCLP_WNDPROC, (ULONG_PTR)def_window_procW);
5777     ok(!IsWindowUnicode(hwnd), "IsWindowUnicode expected to return FALSE\n");
5778     /* do not restore class window proc back to ansi */
5779 
5780     DestroyWindow(hwnd);
5781 
5782     hwnd = CreateWindowExA(0, ansi_class_nameA, NULL, WS_POPUP,
5783                            0, 0, 100, 100, GetDesktopWindow(), 0, 0, NULL);
5784     assert(hwnd);
5785 
5786     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
5787     SetClassLongPtrA(hwnd, GCLP_WNDPROC, (ULONG_PTR)def_window_procA);
5788     ok(IsWindowUnicode(hwnd), "IsWindowUnicode expected to return TRUE\n");
5789 
5790     DestroyWindow(hwnd);
5791 }
5792 
minmax_wnd_proc(HWND hwnd,UINT msg,WPARAM wp,LPARAM lp)5793 static LRESULT CALLBACK minmax_wnd_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
5794 {
5795     MINMAXINFO *minmax;
5796 
5797     if (msg != WM_GETMINMAXINFO)
5798         return DefWindowProcA(hwnd, msg, wp, lp);
5799 
5800     minmax = (MINMAXINFO *)lp;
5801 
5802     if ((GetWindowLongA(hwnd, GWL_STYLE) & WS_CHILD))
5803     {
5804         minmax->ptReserved.x = 0;
5805         minmax->ptReserved.y = 0;
5806         minmax->ptMaxSize.x = 400;
5807         minmax->ptMaxSize.y = 400;
5808         minmax->ptMaxPosition.x = 300;
5809         minmax->ptMaxPosition.y = 300;
5810         minmax->ptMaxTrackSize.x = 200;
5811         minmax->ptMaxTrackSize.y = 200;
5812         minmax->ptMinTrackSize.x = 100;
5813         minmax->ptMinTrackSize.y = 100;
5814     }
5815     else
5816         DefWindowProcA(hwnd, msg, wp, lp);
5817     return 1;
5818 }
5819 
5820 static int expected_cx, expected_cy;
5821 static RECT expected_rect, broken_rect;
5822 
winsizes_wnd_proc(HWND hwnd,UINT msg,WPARAM wp,LPARAM lp)5823 static LRESULT CALLBACK winsizes_wnd_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
5824 {
5825     switch(msg)
5826     {
5827     case WM_GETMINMAXINFO:
5828     {
5829         RECT rect;
5830         GetWindowRect( hwnd, &rect );
5831         ok( !rect.left && !rect.top && !rect.right && !rect.bottom, "wrong rect %s\n",
5832             wine_dbgstr_rect( &rect ));
5833         return DefWindowProcA(hwnd, msg, wp, lp);
5834     }
5835     case WM_NCCREATE:
5836     case WM_CREATE:
5837     {
5838         CREATESTRUCTA *cs = (CREATESTRUCTA *)lp;
5839         RECT rect;
5840         GetWindowRect( hwnd, &rect );
5841         trace( "hwnd %p msg %x size %dx%d rect %s\n", hwnd, msg, cs->cx, cs->cy,
5842                wine_dbgstr_rect( &rect ));
5843         ok( cs->cx == expected_cx || broken(cs->cx == (short)expected_cx),
5844             "wrong x size %d/%d\n", cs->cx, expected_cx );
5845         ok( cs->cy == expected_cy || broken(cs->cy == (short)expected_cy),
5846             "wrong y size %d/%d\n", cs->cy, expected_cy );
5847         ok( (rect.right - rect.left == expected_rect.right - expected_rect.left &&
5848              rect.bottom - rect.top == expected_rect.bottom - expected_rect.top) ||
5849             (rect.right - rect.left == min( 65535, expected_rect.right - expected_rect.left ) &&
5850              rect.bottom - rect.top == min( 65535, expected_rect.bottom - expected_rect.top )) ||
5851             broken( rect.right - rect.left == broken_rect.right - broken_rect.left &&
5852                     rect.bottom - rect.top == broken_rect.bottom - broken_rect.top) ||
5853             broken( rect.right - rect.left == (short)broken_rect.right - (short)broken_rect.left &&
5854                     rect.bottom - rect.top == (short)broken_rect.bottom - (short)broken_rect.top),
5855             "wrong rect %s / %s\n", wine_dbgstr_rect( &rect ), wine_dbgstr_rect( &expected_rect ));
5856         return DefWindowProcA(hwnd, msg, wp, lp);
5857     }
5858     case WM_NCCALCSIZE:
5859     {
5860         RECT rect, *r = (RECT *)lp;
5861         GetWindowRect( hwnd, &rect );
5862         ok( EqualRect( &rect, r ), "passed rect %s doesn't match window rect %s\n",
5863             wine_dbgstr_rect( r ), wine_dbgstr_rect( &rect ));
5864         return DefWindowProcA(hwnd, msg, wp, lp);
5865     }
5866     default:
5867         return DefWindowProcA(hwnd, msg, wp, lp);
5868     }
5869 }
5870 
test_CreateWindow(void)5871 static void test_CreateWindow(void)
5872 {
5873     WNDCLASSA cls;
5874     HWND hwnd, parent;
5875     HMENU hmenu;
5876     RECT rc, rc_minmax;
5877     MINMAXINFO minmax;
5878     BOOL res;
5879 
5880 #define expect_menu(window, menu) \
5881     SetLastError(0xdeadbeef); \
5882     res = (GetMenu(window) == (HMENU)menu); \
5883     ok(res, "GetMenu error %d\n", GetLastError())
5884 
5885 #define expect_style(window, style)\
5886     ok((ULONG)GetWindowLongA(window, GWL_STYLE) == (style), "expected style %x != %x\n", (LONG)(style), GetWindowLongA(window, GWL_STYLE))
5887 
5888 #define expect_ex_style(window, ex_style)\
5889     ok((ULONG)GetWindowLongA(window, GWL_EXSTYLE) == (ex_style), "expected ex_style %x != %x\n", (LONG)(ex_style), GetWindowLongA(window, GWL_EXSTYLE))
5890 
5891 #define expect_gle_broken_9x(gle)\
5892     ok(GetLastError() == gle ||\
5893        broken(GetLastError() == 0xdeadbeef),\
5894        "IsMenu set error %d\n", GetLastError())
5895 
5896     hmenu = CreateMenu();
5897     assert(hmenu != 0);
5898     parent = GetDesktopWindow();
5899     assert(parent != 0);
5900 
5901     SetLastError(0xdeadbeef);
5902     res = IsMenu(hmenu);
5903     ok(res, "IsMenu error %d\n", GetLastError());
5904 
5905     /* WS_CHILD */
5906     SetLastError(0xdeadbeef);
5907     hwnd = CreateWindowExA(WS_EX_APPWINDOW, "static", NULL, WS_CHILD,
5908                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
5909     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
5910     expect_menu(hwnd, 1);
5911     expect_style(hwnd, WS_CHILD);
5912     expect_ex_style(hwnd, WS_EX_APPWINDOW);
5913     DestroyWindow(hwnd);
5914 
5915     SetLastError(0xdeadbeef);
5916     hwnd = CreateWindowExA(WS_EX_APPWINDOW, "static", NULL, WS_CHILD | WS_CAPTION,
5917                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
5918     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
5919     expect_menu(hwnd, 1);
5920     expect_style(hwnd, WS_CHILD | WS_CAPTION);
5921     expect_ex_style(hwnd, WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
5922     DestroyWindow(hwnd);
5923 
5924     SetLastError(0xdeadbeef);
5925     hwnd = CreateWindowExA(0, "static", NULL, WS_CHILD,
5926                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
5927     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
5928     expect_menu(hwnd, 1);
5929     expect_style(hwnd, WS_CHILD);
5930     expect_ex_style(hwnd, 0);
5931     DestroyWindow(hwnd);
5932 
5933     SetLastError(0xdeadbeef);
5934     hwnd = CreateWindowExA(0, "static", NULL, WS_CHILD | WS_CAPTION,
5935                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
5936     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
5937     expect_menu(hwnd, 1);
5938     expect_style(hwnd, WS_CHILD | WS_CAPTION);
5939     expect_ex_style(hwnd, WS_EX_WINDOWEDGE);
5940     DestroyWindow(hwnd);
5941 
5942     /* WS_POPUP */
5943     SetLastError(0xdeadbeef);
5944     hwnd = CreateWindowExA(WS_EX_APPWINDOW, "static", NULL, WS_POPUP,
5945                            0, 0, 100, 100, parent, hmenu, 0, NULL);
5946     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
5947     expect_menu(hwnd, hmenu);
5948     expect_style(hwnd, WS_POPUP | WS_CLIPSIBLINGS);
5949     expect_ex_style(hwnd, WS_EX_APPWINDOW);
5950     DestroyWindow(hwnd);
5951     SetLastError(0xdeadbeef);
5952     ok(!IsMenu(hmenu), "IsMenu should fail\n");
5953     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
5954 
5955     hmenu = CreateMenu();
5956     assert(hmenu != 0);
5957     SetLastError(0xdeadbeef);
5958     hwnd = CreateWindowExA(WS_EX_APPWINDOW, "static", NULL, WS_POPUP | WS_CAPTION,
5959                            0, 0, 100, 100, parent, hmenu, 0, NULL);
5960     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
5961     expect_menu(hwnd, hmenu);
5962     expect_style(hwnd, WS_POPUP | WS_CAPTION | WS_CLIPSIBLINGS);
5963     expect_ex_style(hwnd, WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
5964     DestroyWindow(hwnd);
5965     SetLastError(0xdeadbeef);
5966     ok(!IsMenu(hmenu), "IsMenu should fail\n");
5967     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
5968 
5969     hmenu = CreateMenu();
5970     assert(hmenu != 0);
5971     SetLastError(0xdeadbeef);
5972     hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
5973                            0, 0, 100, 100, parent, hmenu, 0, NULL);
5974     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
5975     expect_menu(hwnd, hmenu);
5976     expect_style(hwnd, WS_POPUP | WS_CLIPSIBLINGS);
5977     expect_ex_style(hwnd, 0);
5978     DestroyWindow(hwnd);
5979     SetLastError(0xdeadbeef);
5980     ok(!IsMenu(hmenu), "IsMenu should fail\n");
5981     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
5982 
5983     hmenu = CreateMenu();
5984     assert(hmenu != 0);
5985     SetLastError(0xdeadbeef);
5986     hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP | WS_CAPTION,
5987                            0, 0, 100, 100, parent, hmenu, 0, NULL);
5988     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
5989     expect_menu(hwnd, hmenu);
5990     expect_style(hwnd, WS_POPUP | WS_CAPTION | WS_CLIPSIBLINGS);
5991     expect_ex_style(hwnd, WS_EX_WINDOWEDGE);
5992     DestroyWindow(hwnd);
5993     SetLastError(0xdeadbeef);
5994     ok(!IsMenu(hmenu), "IsMenu should fail\n");
5995     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
5996 
5997     /* WS_CHILD | WS_POPUP */
5998     SetLastError(0xdeadbeef);
5999     hwnd = CreateWindowExA(WS_EX_APPWINDOW, "static", NULL, WS_CHILD | WS_POPUP,
6000                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
6001     ok(!hwnd || broken(hwnd != 0 /* Win9x */), "CreateWindowEx should fail\n");
6002     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
6003     if (hwnd)
6004         DestroyWindow(hwnd);
6005 
6006     hmenu = CreateMenu();
6007     assert(hmenu != 0);
6008     SetLastError(0xdeadbeef);
6009     hwnd = CreateWindowExA(WS_EX_APPWINDOW, "static", NULL, WS_CHILD | WS_POPUP,
6010                            0, 0, 100, 100, parent, hmenu, 0, NULL);
6011     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
6012     expect_menu(hwnd, hmenu);
6013     expect_style(hwnd, WS_CHILD | WS_POPUP | WS_CLIPSIBLINGS);
6014     expect_ex_style(hwnd, WS_EX_APPWINDOW);
6015     DestroyWindow(hwnd);
6016     SetLastError(0xdeadbeef);
6017     ok(!IsMenu(hmenu), "IsMenu should fail\n");
6018     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
6019 
6020     SetLastError(0xdeadbeef);
6021     hwnd = CreateWindowExA(WS_EX_APPWINDOW, "static", NULL, WS_CHILD | WS_POPUP | WS_CAPTION,
6022                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
6023     ok(!hwnd || broken(hwnd != 0 /* Win9x */), "CreateWindowEx should fail\n");
6024     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
6025     if (hwnd)
6026         DestroyWindow(hwnd);
6027 
6028     hmenu = CreateMenu();
6029     assert(hmenu != 0);
6030     SetLastError(0xdeadbeef);
6031     hwnd = CreateWindowExA(WS_EX_APPWINDOW, "static", NULL, WS_CHILD | WS_POPUP | WS_CAPTION,
6032                            0, 0, 100, 100, parent, hmenu, 0, NULL);
6033     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
6034     expect_menu(hwnd, hmenu);
6035     expect_style(hwnd, WS_CHILD | WS_POPUP | WS_CAPTION | WS_CLIPSIBLINGS);
6036     expect_ex_style(hwnd, WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
6037     DestroyWindow(hwnd);
6038     SetLastError(0xdeadbeef);
6039     ok(!IsMenu(hmenu), "IsMenu should fail\n");
6040     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
6041 
6042     SetLastError(0xdeadbeef);
6043     hwnd = CreateWindowExA(0, "static", NULL, WS_CHILD | WS_POPUP,
6044                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
6045     ok(!hwnd || broken(hwnd != 0 /* Win9x */), "CreateWindowEx should fail\n");
6046     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
6047     if (hwnd)
6048         DestroyWindow(hwnd);
6049 
6050     hmenu = CreateMenu();
6051     assert(hmenu != 0);
6052     SetLastError(0xdeadbeef);
6053     hwnd = CreateWindowExA(0, "static", NULL, WS_CHILD | WS_POPUP,
6054                            0, 0, 100, 100, parent, hmenu, 0, NULL);
6055     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
6056     expect_menu(hwnd, hmenu);
6057     expect_style(hwnd, WS_CHILD | WS_POPUP | WS_CLIPSIBLINGS);
6058     expect_ex_style(hwnd, 0);
6059     DestroyWindow(hwnd);
6060     SetLastError(0xdeadbeef);
6061     ok(!IsMenu(hmenu), "IsMenu should fail\n");
6062     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
6063 
6064     SetLastError(0xdeadbeef);
6065     hwnd = CreateWindowExA(0, "static", NULL, WS_CHILD | WS_POPUP | WS_CAPTION,
6066                            0, 0, 100, 100, parent, (HMENU)1, 0, NULL);
6067     ok(!hwnd || broken(hwnd != 0 /* Win9x */), "CreateWindowEx should fail\n");
6068     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
6069     if (hwnd)
6070         DestroyWindow(hwnd);
6071 
6072     hmenu = CreateMenu();
6073     assert(hmenu != 0);
6074     SetLastError(0xdeadbeef);
6075     hwnd = CreateWindowExA(0, "static", NULL, WS_CHILD | WS_POPUP | WS_CAPTION,
6076                            0, 0, 100, 100, parent, hmenu, 0, NULL);
6077     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
6078     expect_menu(hwnd, hmenu);
6079     expect_style(hwnd, WS_CHILD | WS_POPUP | WS_CAPTION | WS_CLIPSIBLINGS);
6080     expect_ex_style(hwnd, WS_EX_WINDOWEDGE);
6081     DestroyWindow(hwnd);
6082     SetLastError(0xdeadbeef);
6083     ok(!IsMenu(hmenu), "IsMenu should fail\n");
6084     expect_gle_broken_9x(ERROR_INVALID_MENU_HANDLE);
6085 
6086     /* test child window sizing */
6087     cls.style = 0;
6088     cls.lpfnWndProc = minmax_wnd_proc;
6089     cls.cbClsExtra = 0;
6090     cls.cbWndExtra = 0;
6091     cls.hInstance = GetModuleHandleA(NULL);
6092     cls.hIcon = 0;
6093     cls.hCursor = LoadCursorA(0, (LPCSTR)IDC_ARROW);
6094     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
6095     cls.lpszMenuName = NULL;
6096     cls.lpszClassName = "MinMax_WndClass";
6097     RegisterClassA(&cls);
6098 
6099     SetLastError(0xdeadbeef);
6100     parent = CreateWindowExA(0, "MinMax_WndClass", NULL, WS_CAPTION | WS_SYSMENU | WS_THICKFRAME,
6101                            0, 0, 100, 100, 0, 0, 0, NULL);
6102     ok(parent != 0, "CreateWindowEx error %d\n", GetLastError());
6103     expect_menu(parent, 0);
6104     expect_style(parent, WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_CLIPSIBLINGS);
6105     expect_ex_style(parent, WS_EX_WINDOWEDGE);
6106 
6107     memset(&minmax, 0, sizeof(minmax));
6108     SendMessageA(parent, WM_GETMINMAXINFO, 0, (LPARAM)&minmax);
6109     SetRect(&rc_minmax, 0, 0, minmax.ptMaxSize.x, minmax.ptMaxSize.y);
6110     ok(IsRectEmpty(&rc_minmax), "ptMaxSize is not empty\n");
6111     SetRect(&rc_minmax, 0, 0, minmax.ptMaxTrackSize.x, minmax.ptMaxTrackSize.y);
6112     ok(IsRectEmpty(&rc_minmax), "ptMaxTrackSize is not empty\n");
6113 
6114     GetWindowRect(parent, &rc);
6115     ok(!IsRectEmpty(&rc), "parent window rect is empty\n");
6116     GetClientRect(parent, &rc);
6117     ok(!IsRectEmpty(&rc), "parent client rect is empty\n");
6118 
6119     InflateRect(&rc, 200, 200);
6120     trace("creating child with rect %s\n", wine_dbgstr_rect(&rc));
6121 
6122     SetLastError(0xdeadbeef);
6123     hwnd = CreateWindowExA(0, "MinMax_WndClass", NULL, WS_CHILD | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME,
6124                           rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
6125                           parent, (HMENU)1, 0, NULL);
6126     ok(hwnd != 0, "CreateWindowEx error %d\n", GetLastError());
6127     expect_menu(hwnd, 1);
6128     expect_style(hwnd, WS_CHILD | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME);
6129     expect_ex_style(hwnd, WS_EX_WINDOWEDGE);
6130 
6131     memset(&minmax, 0, sizeof(minmax));
6132     SendMessageA(hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&minmax);
6133     SetRect(&rc_minmax, 0, 0, minmax.ptMaxTrackSize.x, minmax.ptMaxTrackSize.y);
6134 
6135     GetWindowRect(hwnd, &rc);
6136     OffsetRect(&rc, -rc.left, -rc.top);
6137     ok(EqualRect(&rc, &rc_minmax), "rects don't match: %s and %s\n", wine_dbgstr_rect(&rc),
6138        wine_dbgstr_rect(&rc_minmax));
6139     DestroyWindow(hwnd);
6140 
6141     cls.lpfnWndProc = winsizes_wnd_proc;
6142     cls.lpszClassName = "Sizes_WndClass";
6143     RegisterClassA(&cls);
6144 
6145     expected_cx = expected_cy = 200000;
6146     SetRect( &expected_rect, 0, 0, 200000, 200000 );
6147     broken_rect = expected_rect;
6148     hwnd = CreateWindowExA(0, "Sizes_WndClass", NULL, WS_CHILD, 300000, 300000, 200000, 200000, parent, 0, 0, NULL);
6149     ok( hwnd != 0, "creation failed err %u\n", GetLastError());
6150     GetClientRect( hwnd, &rc );
6151     ok( rc.right == 200000 || rc.right == 65535 || broken(rc.right == (short)200000),
6152         "invalid rect right %u\n", rc.right );
6153     ok( rc.bottom == 200000 || rc.bottom == 65535 || broken(rc.bottom == (short)200000),
6154         "invalid rect bottom %u\n", rc.bottom );
6155     DestroyWindow(hwnd);
6156 
6157     expected_cx = expected_cy = -10;
6158     SetRectEmpty(&expected_rect);
6159     SetRect( &broken_rect, 0, 0, -10, -10 );
6160     hwnd = CreateWindowExA(0, "Sizes_WndClass", NULL, WS_CHILD, -20, -20, -10, -10, parent, 0, 0, NULL);
6161     ok( hwnd != 0, "creation failed err %u\n", GetLastError());
6162     GetClientRect( hwnd, &rc );
6163     ok( rc.right == 0, "invalid rect right %u\n", rc.right );
6164     ok( rc.bottom == 0, "invalid rect bottom %u\n", rc.bottom );
6165     DestroyWindow(hwnd);
6166 
6167     expected_cx = expected_cy = -200000;
6168     SetRectEmpty(&expected_rect);
6169     SetRect( &broken_rect, 0, 0, -200000, -200000 );
6170     hwnd = CreateWindowExA(0, "Sizes_WndClass", NULL, WS_CHILD, -300000, -300000, -200000, -200000, parent, 0, 0, NULL);
6171     ok( hwnd != 0, "creation failed err %u\n", GetLastError());
6172     GetClientRect( hwnd, &rc );
6173     ok( rc.right == 0, "invalid rect right %u\n", rc.right );
6174     ok( rc.bottom == 0, "invalid rect bottom %u\n", rc.bottom );
6175     DestroyWindow(hwnd);
6176 
6177     /* we need a parent at 0,0 so that child coordinates match */
6178     DestroyWindow(parent);
6179     parent = CreateWindowExA(0, "MinMax_WndClass", NULL, WS_POPUP, 0, 0, 100, 100, 0, 0, 0, NULL);
6180     ok(parent != 0, "CreateWindowEx error %d\n", GetLastError());
6181 
6182     expected_cx = 100;
6183     expected_cy = 0x7fffffff;
6184     SetRect( &expected_rect, 10, 10, 110, 0x7fffffff );
6185     SetRect( &broken_rect, 10, 10, 110, 0x7fffffffU + 10 );
6186     hwnd = CreateWindowExA(0, "Sizes_WndClass", NULL, WS_CHILD, 10, 10, 100, 0x7fffffff, parent, 0, 0, NULL);
6187     ok( hwnd != 0, "creation failed err %u\n", GetLastError());
6188     GetClientRect( hwnd, &rc );
6189     ok( rc.right == 100, "invalid rect right %u\n", rc.right );
6190     ok( rc.bottom == 0x7fffffff - 10 || rc.bottom ==65535 || broken(rc.bottom == 0),
6191         "invalid rect bottom %u\n", rc.bottom );
6192     DestroyWindow(hwnd);
6193 
6194     expected_cx = 0x7fffffff;
6195     expected_cy = 0x7fffffff;
6196     SetRect( &expected_rect, 20, 10, 0x7fffffff, 0x7fffffff );
6197     SetRect( &broken_rect, 20, 10, 0x7fffffffU + 20, 0x7fffffffU + 10 );
6198     hwnd = CreateWindowExA(0, "Sizes_WndClass", NULL, WS_CHILD, 20, 10, 0x7fffffff, 0x7fffffff, parent, 0, 0, NULL);
6199     ok( hwnd != 0, "creation failed err %u\n", GetLastError());
6200     GetClientRect( hwnd, &rc );
6201     ok( rc.right == 0x7fffffff - 20 || rc.right == 65535 || broken(rc.right == 0),
6202         "invalid rect right %u\n", rc.right );
6203     ok( rc.bottom == 0x7fffffff - 10 || rc.right == 65535 || broken(rc.bottom == 0),
6204         "invalid rect bottom %u\n", rc.bottom );
6205     DestroyWindow(hwnd);
6206 
6207     /* top level window */
6208     expected_cx = expected_cy = 200000;
6209     SetRect( &expected_rect, 0, 0, GetSystemMetrics(SM_CXMAXTRACK), GetSystemMetrics(SM_CYMAXTRACK) );
6210     hwnd = CreateWindowExA(0, "Sizes_WndClass", NULL, WS_OVERLAPPEDWINDOW, 300000, 300000, 200000, 200000, 0, 0, 0, NULL);
6211     ok( hwnd != 0, "creation failed err %u\n", GetLastError());
6212     GetClientRect( hwnd, &rc );
6213     ok( rc.right <= expected_cx, "invalid rect right %u\n", rc.right );
6214     ok( rc.bottom <= expected_cy, "invalid rect bottom %u\n", rc.bottom );
6215     DestroyWindow(hwnd);
6216 
6217     if (pGetLayout && pSetLayout)
6218     {
6219         HDC hdc = GetDC( parent );
6220         pSetLayout( hdc, LAYOUT_RTL );
6221         if (pGetLayout( hdc ))
6222         {
6223             ReleaseDC( parent, hdc );
6224             DestroyWindow( parent );
6225             SetLastError( 0xdeadbeef );
6226             parent = CreateWindowExA(WS_EX_APPWINDOW | WS_EX_LAYOUTRTL, "static", NULL, WS_POPUP,
6227                                     0, 0, 100, 100, 0, 0, 0, NULL);
6228             ok( parent != 0, "creation failed err %u\n", GetLastError());
6229             expect_ex_style( parent, WS_EX_APPWINDOW | WS_EX_LAYOUTRTL );
6230             hwnd = CreateWindowExA(0, "static", NULL, WS_CHILD, 0, 0, 20, 20, parent, 0, 0, NULL);
6231             ok( hwnd != 0, "creation failed err %u\n", GetLastError());
6232             expect_ex_style( hwnd, WS_EX_LAYOUTRTL );
6233             DestroyWindow( hwnd );
6234             hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0, 0, 20, 20, parent, 0, 0, NULL);
6235             ok( hwnd != 0, "creation failed err %u\n", GetLastError());
6236             expect_ex_style( hwnd, 0 );
6237             DestroyWindow( hwnd );
6238             SetWindowLongW( parent, GWL_EXSTYLE, WS_EX_APPWINDOW | WS_EX_LAYOUTRTL | WS_EX_NOINHERITLAYOUT );
6239             hwnd = CreateWindowExA(0, "static", NULL, WS_CHILD, 0, 0, 20, 20, parent, 0, 0, NULL);
6240             ok( hwnd != 0, "creation failed err %u\n", GetLastError());
6241             expect_ex_style( hwnd, 0 );
6242             DestroyWindow( hwnd );
6243 
6244             if (pGetProcessDefaultLayout && pSetProcessDefaultLayout)
6245             {
6246                 DWORD layout;
6247 
6248                 SetLastError( 0xdeadbeef );
6249                 ok( !pGetProcessDefaultLayout( NULL ), "GetProcessDefaultLayout succeeded\n" );
6250                 ok( GetLastError() == ERROR_NOACCESS, "wrong error %u\n", GetLastError() );
6251                 SetLastError( 0xdeadbeef );
6252                 res = pGetProcessDefaultLayout( &layout );
6253                 ok( res, "GetProcessDefaultLayout failed err %u\n", GetLastError ());
6254                 ok( layout == 0, "GetProcessDefaultLayout wrong layout %x\n", layout );
6255                 SetLastError( 0xdeadbeef );
6256                 res = pSetProcessDefaultLayout( 7 );
6257                 ok( res, "SetProcessDefaultLayout failed err %u\n", GetLastError ());
6258                 res = pGetProcessDefaultLayout( &layout );
6259                 ok( res, "GetProcessDefaultLayout failed err %u\n", GetLastError ());
6260                 ok( layout == 7, "GetProcessDefaultLayout wrong layout %x\n", layout );
6261                 SetLastError( 0xdeadbeef );
6262                 res = pSetProcessDefaultLayout( LAYOUT_RTL );
6263                 ok( res, "SetProcessDefaultLayout failed err %u\n", GetLastError ());
6264                 res = pGetProcessDefaultLayout( &layout );
6265                 ok( res, "GetProcessDefaultLayout failed err %u\n", GetLastError ());
6266                 ok( layout == LAYOUT_RTL, "GetProcessDefaultLayout wrong layout %x\n", layout );
6267                 hwnd = CreateWindowExA(WS_EX_APPWINDOW, "static", NULL, WS_POPUP,
6268                                       0, 0, 100, 100, 0, 0, 0, NULL);
6269                 ok( hwnd != 0, "creation failed err %u\n", GetLastError());
6270                 expect_ex_style( hwnd, WS_EX_APPWINDOW | WS_EX_LAYOUTRTL );
6271                 DestroyWindow( hwnd );
6272                 hwnd = CreateWindowExA(WS_EX_APPWINDOW, "static", NULL, WS_POPUP,
6273                                       0, 0, 100, 100, parent, 0, 0, NULL);
6274                 ok( hwnd != 0, "creation failed err %u\n", GetLastError());
6275                 expect_ex_style( hwnd, WS_EX_APPWINDOW );
6276                 DestroyWindow( hwnd );
6277                 pSetProcessDefaultLayout( 0 );
6278             }
6279             else win_skip( "SetProcessDefaultLayout not supported\n" );
6280         }
6281         else win_skip( "SetLayout not supported\n" );
6282     }
6283     else win_skip( "SetLayout not available\n" );
6284 
6285     DestroyWindow(parent);
6286 
6287     UnregisterClassA("MinMax_WndClass", GetModuleHandleA(NULL));
6288     UnregisterClassA("Sizes_WndClass", GetModuleHandleA(NULL));
6289 
6290 #undef expect_gle_broken_9x
6291 #undef expect_menu
6292 #undef expect_style
6293 #undef expect_ex_style
6294 }
6295 
6296 /* function that remembers whether the system the test is running on sets the
6297  * last error for user32 functions to make the tests stricter */
check_error(DWORD actual,DWORD expected)6298 static int check_error(DWORD actual, DWORD expected)
6299 {
6300     static int sets_last_error = -1;
6301     if (sets_last_error == -1)
6302         sets_last_error = (actual != 0xdeadbeef);
6303     return (!sets_last_error && (actual == 0xdeadbeef)) || (actual == expected);
6304 }
6305 
test_SetWindowLong(void)6306 static void test_SetWindowLong(void)
6307 {
6308     LONG_PTR retval;
6309     WNDPROC old_window_procW;
6310 
6311     SetLastError(0xdeadbeef);
6312     retval = SetWindowLongPtrA(NULL, GWLP_WNDPROC, 0);
6313     ok(!retval, "SetWindowLongPtr on invalid window handle should have returned 0 instead of 0x%lx\n", retval);
6314     ok(check_error(GetLastError(), ERROR_INVALID_WINDOW_HANDLE),
6315         "SetWindowLongPtr should have set error to ERROR_INVALID_WINDOW_HANDLE instead of %d\n", GetLastError());
6316 
6317     SetLastError(0xdeadbeef);
6318     retval = SetWindowLongPtrA(hwndMain, 0xdeadbeef, 0);
6319     ok(!retval, "SetWindowLongPtr on invalid index should have returned 0 instead of 0x%lx\n", retval);
6320     ok(check_error(GetLastError(), ERROR_INVALID_INDEX),
6321         "SetWindowLongPtr should have set error to ERROR_INVALID_INDEX instead of %d\n", GetLastError());
6322 
6323     SetLastError(0xdeadbeef);
6324     retval = SetWindowLongPtrA(hwndMain, GWLP_WNDPROC, 0);
6325     ok((WNDPROC)retval == main_window_procA || broken(!retval), /* win9x */
6326         "SetWindowLongPtr on invalid window proc should have returned address of main_window_procA instead of 0x%lx\n", retval);
6327     ok(GetLastError() == 0xdeadbeef, "SetWindowLongPtr shouldn't have set the last error, instead of setting it to %d\n", GetLastError());
6328     retval = GetWindowLongPtrA(hwndMain, GWLP_WNDPROC);
6329     ok((WNDPROC)retval == main_window_procA,
6330         "SetWindowLongPtr on invalid window proc shouldn't have changed the value returned by GetWindowLongPtr, instead of changing it to 0x%lx\n", retval);
6331     ok(!IsWindowUnicode(hwndMain), "hwndMain shouldn't be Unicode\n");
6332 
6333     old_window_procW = (WNDPROC)GetWindowLongPtrW(hwndMain, GWLP_WNDPROC);
6334     SetLastError(0xdeadbeef);
6335     retval = SetWindowLongPtrW(hwndMain, GWLP_WNDPROC, 0);
6336     if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
6337     {
6338         ok(GetLastError() == 0xdeadbeef, "SetWindowLongPtr shouldn't have set the last error, instead of setting it to %d\n", GetLastError());
6339         ok(retval != 0, "SetWindowLongPtr error %d\n", GetLastError());
6340         ok((WNDPROC)retval == old_window_procW,
6341             "SetWindowLongPtr on invalid window proc shouldn't have changed the value returned by GetWindowLongPtr, instead of changing it to 0x%lx\n", retval);
6342         ok(IsWindowUnicode(hwndMain), "hwndMain should now be Unicode\n");
6343 
6344         /* set it back to ANSI */
6345         SetWindowLongPtrA(hwndMain, GWLP_WNDPROC, 0);
6346     }
6347 }
6348 
check_style_wnd_proc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)6349 static LRESULT WINAPI check_style_wnd_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
6350 {
6351     const STYLESTRUCT *expected = (STYLESTRUCT *)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
6352     const STYLESTRUCT *got = (STYLESTRUCT *)lParam;
6353 
6354     if (message == WM_STYLECHANGING && wParam == GWL_STYLE)
6355     {
6356         ok(got->styleOld == expected[0].styleOld, "expected old style %#x, got %#x\n",
6357             expected[0].styleOld, got->styleOld);
6358         ok(got->styleNew == expected[0].styleNew, "expected new style %#x, got %#x\n",
6359             expected[0].styleNew, got->styleNew);
6360     }
6361     else if (message == WM_STYLECHANGED && wParam == GWL_STYLE)
6362     {
6363         ok(got->styleOld == expected[1].styleOld, "expected old style %#x, got %#x\n",
6364             expected[1].styleOld, got->styleOld);
6365         ok(got->styleNew == expected[1].styleNew, "expected new style %#x, got %#x\n",
6366             expected[1].styleNew, got->styleNew);
6367     }
6368 
6369     return DefWindowProcA(hwnd, message, wParam, lParam);
6370 }
6371 
test_set_window_style(void)6372 static void test_set_window_style(void)
6373 {
6374     LONG expected_style, new_style, old_style;
6375     STYLESTRUCT expected_stylestruct[2];
6376     unsigned int i;
6377     WNDCLASSA cls;
6378     HWND hwnd;
6379 
6380     static const struct
6381     {
6382         LONG creation_style;
6383         LONG style;
6384     }
6385     tests[] =
6386     {
6387         { WS_MINIMIZE | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
6388           WS_VISIBLE | WS_CLIPSIBLINGS | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX },
6389         { WS_MINIMIZE | WS_CLIPSIBLINGS | WS_CAPTION,
6390           WS_CLIPSIBLINGS | WS_CAPTION },
6391         { WS_MAXIMIZE | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
6392           WS_VISIBLE | WS_CLIPSIBLINGS | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX },
6393         { WS_MAXIMIZE | WS_CLIPSIBLINGS | WS_CAPTION,
6394           WS_CLIPSIBLINGS | WS_CAPTION },
6395         { WS_VISIBLE | WS_CLIPSIBLINGS | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
6396           WS_MINIMIZE | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX },
6397         { WS_CLIPSIBLINGS | WS_CAPTION,
6398           WS_MINIMIZE | WS_CLIPSIBLINGS | WS_CAPTION },
6399         { WS_VISIBLE | WS_CLIPSIBLINGS | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
6400           WS_MAXIMIZE | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX },
6401         { WS_CLIPSIBLINGS | WS_CAPTION,
6402           WS_MAXIMIZE | WS_CLIPSIBLINGS | WS_CAPTION },
6403     };
6404 
6405     memset(&cls, 0, sizeof(cls));
6406     cls.lpfnWndProc = check_style_wnd_proc;
6407     cls.hInstance = GetModuleHandleA(0);
6408     cls.lpszClassName = "TestSetWindowStylesClass";
6409     ok(RegisterClassA(&cls), "RegisterClass failed\n");
6410 
6411     for (i = 0; i < sizeof(tests) / sizeof(*tests); i++)
6412     {
6413         expected_style = tests[i].style;
6414         if (tests[i].creation_style & WS_MINIMIZE)
6415             expected_style |= WS_MINIMIZE;
6416 
6417         expected_stylestruct[0].styleOld = tests[i].creation_style;
6418         expected_stylestruct[0].styleNew = tests[i].style;
6419         expected_stylestruct[1].styleOld = tests[i].creation_style;
6420         expected_stylestruct[1].styleNew = expected_style;
6421 
6422         hwnd = CreateWindowA(cls.lpszClassName, "Test set styles",
6423                              tests[i].creation_style, 100, 100, 200, 200, 0, 0, 0, NULL);
6424         ok(hwnd != 0, "CreateWindow failed\n");
6425         SetWindowLongPtrA(hwnd, GWLP_USERDATA, (LONG_PTR)&expected_stylestruct);
6426 
6427         old_style = SetWindowLongA(hwnd, GWL_STYLE, tests[i].style);
6428         ok(old_style == tests[i].creation_style, "expected old style %#x, got %#x\n",
6429             tests[i].creation_style, old_style);
6430         new_style = GetWindowLongA(hwnd, GWL_STYLE);
6431         ok(new_style == expected_style, "expected new style %#x, got %#x\n",
6432             expected_style, new_style);
6433 
6434         SetWindowLongPtrA(hwnd, GWLP_USERDATA, 0);
6435         DestroyWindow(hwnd);
6436     }
6437 
6438     UnregisterClassA(cls.lpszClassName, cls.hInstance);
6439 }
6440 
test_ShowWindow(void)6441 static void test_ShowWindow(void)
6442 {
6443     HWND hwnd;
6444     DWORD style;
6445     RECT rcMain, rc, rcMinimized;
6446     LPARAM ret;
6447 
6448     SetRect(&rcMain, 120, 120, 210, 210);
6449 
6450     hwnd = CreateWindowExA(0, "MainWindowClass", NULL,
6451                           WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
6452                           WS_MAXIMIZEBOX | WS_POPUP,
6453                           rcMain.left, rcMain.top,
6454                           rcMain.right - rcMain.left, rcMain.bottom - rcMain.top,
6455                           0, 0, 0, NULL);
6456     assert(hwnd);
6457 
6458     style = GetWindowLongA(hwnd, GWL_STYLE);
6459     ok(!(style & WS_DISABLED), "window should not be disabled\n");
6460     ok(!(style & WS_VISIBLE), "window should not be visible\n");
6461     ok(!(style & WS_MINIMIZE), "window should not be minimized\n");
6462     ok(!(style & WS_MAXIMIZE), "window should not be maximized\n");
6463     GetWindowRect(hwnd, &rc);
6464     ok(EqualRect(&rcMain, &rc), "expected %s, got %s\n", wine_dbgstr_rect(&rcMain),
6465        wine_dbgstr_rect(&rc));
6466 
6467     ret = ShowWindow(hwnd, SW_SHOW);
6468     ok(!ret, "not expected ret: %lu\n", ret);
6469     style = GetWindowLongA(hwnd, GWL_STYLE);
6470     ok(!(style & WS_DISABLED), "window should not be disabled\n");
6471     ok(style & WS_VISIBLE, "window should be visible\n");
6472     ok(!(style & WS_MINIMIZE), "window should not be minimized\n");
6473     ok(!(style & WS_MAXIMIZE), "window should not be maximized\n");
6474     GetWindowRect(hwnd, &rc);
6475     ok(EqualRect(&rcMain, &rc), "expected %s, got %s\n", wine_dbgstr_rect(&rcMain),
6476        wine_dbgstr_rect(&rc));
6477 
6478     ret = ShowWindow(hwnd, SW_MINIMIZE);
6479     ok(ret, "not expected ret: %lu\n", ret);
6480     style = GetWindowLongA(hwnd, GWL_STYLE);
6481     ok(!(style & WS_DISABLED), "window should not be disabled\n");
6482     ok(style & WS_VISIBLE, "window should be visible\n");
6483     ok(style & WS_MINIMIZE, "window should be minimized\n");
6484     ok(!(style & WS_MAXIMIZE), "window should not be maximized\n");
6485     GetWindowRect(hwnd, &rcMinimized);
6486     ok(!EqualRect(&rcMain, &rcMinimized), "rects shouldn't match\n");
6487     /* shouldn't be able to resize minimized windows */
6488     ret = SetWindowPos(hwnd, 0, 0, 0,
6489                        (rcMinimized.right - rcMinimized.left) * 2,
6490                        (rcMinimized.bottom - rcMinimized.top) * 2,
6491                        SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);
6492     ok(ret, "not expected ret: %lu\n", ret);
6493     GetWindowRect(hwnd, &rc);
6494     ok(EqualRect(&rc, &rcMinimized), "rects should match\n");
6495 
6496     ShowWindow(hwnd, SW_RESTORE);
6497     ok(ret, "not expected ret: %lu\n", ret);
6498     style = GetWindowLongA(hwnd, GWL_STYLE);
6499     ok(!(style & WS_DISABLED), "window should not be disabled\n");
6500     ok(style & WS_VISIBLE, "window should be visible\n");
6501     ok(!(style & WS_MINIMIZE), "window should not be minimized\n");
6502     ok(!(style & WS_MAXIMIZE), "window should not be maximized\n");
6503     GetWindowRect(hwnd, &rc);
6504     ok(EqualRect(&rcMain, &rc), "expected %s, got %s\n", wine_dbgstr_rect(&rcMain),
6505        wine_dbgstr_rect(&rc));
6506 
6507     ret = EnableWindow(hwnd, FALSE);
6508     ok(!ret, "not expected ret: %lu\n", ret);
6509     style = GetWindowLongA(hwnd, GWL_STYLE);
6510     ok(style & WS_DISABLED, "window should be disabled\n");
6511 
6512     ret = DefWindowProcA(hwnd, WM_SYSCOMMAND, SC_MINIMIZE, 0);
6513     ok(!ret, "not expected ret: %lu\n", ret);
6514     style = GetWindowLongA(hwnd, GWL_STYLE);
6515     ok(style & WS_DISABLED, "window should be disabled\n");
6516     ok(style & WS_VISIBLE, "window should be visible\n");
6517     ok(!(style & WS_MINIMIZE), "window should not be minimized\n");
6518     ok(!(style & WS_MAXIMIZE), "window should not be maximized\n");
6519     GetWindowRect(hwnd, &rc);
6520     ok(EqualRect(&rcMain, &rc), "expected %s, got %s\n", wine_dbgstr_rect(&rcMain),
6521        wine_dbgstr_rect(&rc));
6522 
6523     ret = DefWindowProcA(hwnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
6524     ok(!ret, "not expected ret: %lu\n", ret);
6525     style = GetWindowLongA(hwnd, GWL_STYLE);
6526     ok(style & WS_DISABLED, "window should be disabled\n");
6527     ok(style & WS_VISIBLE, "window should be visible\n");
6528     ok(!(style & WS_MINIMIZE), "window should not be minimized\n");
6529     ok(!(style & WS_MAXIMIZE), "window should not be maximized\n");
6530     GetWindowRect(hwnd, &rc);
6531     ok(EqualRect(&rcMain, &rc), "expected %s, got %s\n", wine_dbgstr_rect(&rcMain),
6532        wine_dbgstr_rect(&rc));
6533 
6534     ret = ShowWindow(hwnd, SW_MINIMIZE);
6535     ok(ret, "not expected ret: %lu\n", ret);
6536     style = GetWindowLongA(hwnd, GWL_STYLE);
6537     ok(style & WS_DISABLED, "window should be disabled\n");
6538     ok(style & WS_VISIBLE, "window should be visible\n");
6539     ok(style & WS_MINIMIZE, "window should be minimized\n");
6540     ok(!(style & WS_MAXIMIZE), "window should not be maximized\n");
6541     GetWindowRect(hwnd, &rc);
6542     ok(!EqualRect(&rcMain, &rc), "rects shouldn't match\n");
6543 
6544     ret = DefWindowProcA(hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
6545     ok(!ret, "not expected ret: %lu\n", ret);
6546     style = GetWindowLongA(hwnd, GWL_STYLE);
6547     ok(style & WS_DISABLED, "window should be disabled\n");
6548     ok(style & WS_VISIBLE, "window should be visible\n");
6549     ok(style & WS_MINIMIZE, "window should be minimized\n");
6550     ok(!(style & WS_MAXIMIZE), "window should not be maximized\n");
6551     GetWindowRect(hwnd, &rc);
6552     ok(!EqualRect(&rcMain, &rc), "rects shouldn't match\n");
6553 
6554     ret = ShowWindow(hwnd, SW_RESTORE);
6555     ok(ret, "not expected ret: %lu\n", ret);
6556     style = GetWindowLongA(hwnd, GWL_STYLE);
6557     ok(style & WS_DISABLED, "window should be disabled\n");
6558     ok(style & WS_VISIBLE, "window should be visible\n");
6559     ok(!(style & WS_MINIMIZE), "window should not be minimized\n");
6560     ok(!(style & WS_MAXIMIZE), "window should not be maximized\n");
6561     GetWindowRect(hwnd, &rc);
6562     ok(EqualRect(&rcMain, &rc), "expected %s, got %s\n", wine_dbgstr_rect(&rcMain),
6563        wine_dbgstr_rect(&rc));
6564 
6565     ret = DefWindowProcA(hwnd, WM_SYSCOMMAND, SC_CLOSE, 0);
6566     ok(!ret, "not expected ret: %lu\n", ret);
6567     ok(IsWindow(hwnd), "window should exist\n");
6568 
6569     ret = EnableWindow(hwnd, TRUE);
6570     ok(ret, "not expected ret: %lu\n", ret);
6571 
6572     ret = DefWindowProcA(hwnd, WM_SYSCOMMAND, SC_CLOSE, 0);
6573     ok(!ret, "not expected ret: %lu\n", ret);
6574     ok(!IsWindow(hwnd), "window should not exist\n");
6575 }
6576 
gettext_msg_thread(LPVOID arg)6577 static DWORD CALLBACK gettext_msg_thread( LPVOID arg )
6578 {
6579     HWND hwnd = arg;
6580     char buf[32];
6581     INT buf_len;
6582 
6583     /* test GetWindowTextA */
6584     num_gettext_msgs = 0;
6585     memset( buf, 0, sizeof(buf) );
6586     buf_len = GetWindowTextA( hwnd, buf, sizeof(buf) );
6587     ok( buf_len != 0, "expected a nonempty window text\n" );
6588     ok( !strcmp(buf, "another_caption"), "got wrong window text '%s'\n", buf );
6589     ok( num_gettext_msgs == 1, "got %u WM_GETTEXT messages\n", num_gettext_msgs );
6590 
6591     return 0;
6592 }
6593 
settext_msg_thread(LPVOID arg)6594 static DWORD CALLBACK settext_msg_thread( LPVOID arg )
6595 {
6596     HWND hwnd = arg;
6597     BOOL success;
6598 
6599     /* test SetWindowTextA */
6600     num_settext_msgs = 0;
6601     success = SetWindowTextA( hwnd, "thread_caption" );
6602     ok( success, "SetWindowTextA failed\n" );
6603     ok( num_settext_msgs == 1, "got %u WM_SETTEXT messages\n", num_settext_msgs );
6604 
6605     return 0;
6606 }
6607 
test_gettext(void)6608 static void test_gettext(void)
6609 {
6610     DWORD tid, num_msgs;
6611     WCHAR bufW[32];
6612     HANDLE thread;
6613     BOOL success;
6614     char buf[32];
6615     INT buf_len;
6616     HWND hwnd, hwnd2;
6617     LRESULT r;
6618     MSG msg;
6619 
6620     hwnd = CreateWindowExA( 0, "MainWindowClass", "caption", WS_POPUP, 0, 0, 0, 0, 0, 0, 0, NULL );
6621     ok( hwnd != 0, "CreateWindowExA error %d\n", GetLastError() );
6622 
6623     /* test GetWindowTextA */
6624     num_gettext_msgs = 0;
6625     memset( buf, 0, sizeof(buf) );
6626     buf_len = GetWindowTextA( hwnd, buf, sizeof(buf) );
6627     ok( buf_len != 0, "expected a nonempty window text\n" );
6628     ok( !strcmp(buf, "caption"), "got wrong window text '%s'\n", buf );
6629     ok( num_gettext_msgs == 1, "got %u WM_GETTEXT messages\n", num_gettext_msgs );
6630 
6631     /* other process window */
6632     strcpy( buf, "a" );
6633     buf_len = GetWindowTextA( GetDesktopWindow(), buf, sizeof(buf) );
6634     ok( buf_len == 0, "expected a nonempty window text\n" );
6635     ok( *buf == 0, "got wrong window text '%s'\n", buf );
6636 
6637     strcpy( buf, "blah" );
6638     buf_len = GetWindowTextA( GetDesktopWindow(), buf, 0 );
6639     ok( buf_len == 0, "expected a nonempty window text\n" );
6640     ok( !strcmp(buf, "blah"), "got wrong window text '%s'\n", buf );
6641 
6642     bufW[0] = 0xcc;
6643     buf_len = GetWindowTextW( GetDesktopWindow(), bufW, 0 );
6644     ok( buf_len == 0, "expected a nonempty window text\n" );
6645     ok( bufW[0] == 0xcc, "got %x\n", bufW[0] );
6646 
6647     g_wm_gettext_override.enabled = TRUE;
6648 
6649     num_gettext_msgs = 0;
6650     memset( buf, 0xcc, sizeof(buf) );
6651     g_wm_gettext_override.buff = buf;
6652     buf_len = GetWindowTextA( hwnd, buf, sizeof(buf) );
6653     ok( buf_len == 0, "got %d\n", buf_len );
6654     ok( *buf == 0, "got %x\n", *buf );
6655     ok( num_gettext_msgs == 1, "got %u WM_GETTEXT messages\n", num_gettext_msgs );
6656 
6657     num_gettext_msgs = 0;
6658     strcpy( buf, "blah" );
6659     g_wm_gettext_override.buff = buf;
6660     buf_len = GetWindowTextA( hwnd, buf, 0 );
6661     ok( buf_len == 0, "got %d\n", buf_len );
6662     ok( !strcmp(buf, "blah"), "got %s\n", buf );
6663     ok( num_gettext_msgs == 0, "got %u WM_GETTEXT messages\n", num_gettext_msgs );
6664 
6665     g_wm_gettext_override.enabled = FALSE;
6666 
6667     /* same for W window */
6668     hwnd2 = CreateWindowExW( 0, mainclassW, NULL, WS_POPUP, 0, 0, 0, 0, 0, 0, 0, NULL );
6669     ok( hwnd2 != 0, "CreateWindowExA error %d\n", GetLastError() );
6670 
6671     g_wm_gettext_override.enabled = TRUE;
6672 
6673     num_gettext_msgs = 0;
6674     memset( bufW, 0xcc, sizeof(bufW) );
6675     g_wm_gettext_override.buffW = bufW;
6676     buf_len = GetWindowTextW( hwnd2, bufW, sizeof(bufW)/sizeof(WCHAR) );
6677     ok( buf_len == 0, "got %d\n", buf_len );
6678     ok( *bufW == 0, "got %x\n", *bufW );
6679     ok( num_gettext_msgs == 1, "got %u WM_GETTEXT messages\n", num_gettext_msgs );
6680 
6681     num_gettext_msgs = 0;
6682     memset( bufW, 0xcc, sizeof(bufW) );
6683     g_wm_gettext_override.buffW = bufW;
6684     buf_len = GetWindowTextW( hwnd2, bufW, 0 );
6685     ok( buf_len == 0, "got %d\n", buf_len );
6686     ok( *bufW == 0xcccc, "got %x\n", *bufW );
6687     ok( num_gettext_msgs == 0, "got %u WM_GETTEXT messages\n", num_gettext_msgs );
6688 
6689     g_wm_gettext_override.enabled = FALSE;
6690 
6691     DestroyWindow( hwnd2 );
6692 
6693     /* test WM_GETTEXT */
6694     num_gettext_msgs = 0;
6695     memset( buf, 0, sizeof(buf) );
6696     r = SendMessageA( hwnd, WM_GETTEXT, sizeof(buf), (LONG_PTR)buf );
6697     ok( r != 0, "expected a nonempty window text\n" );
6698     ok( !strcmp(buf, "caption"), "got wrong window text '%s'\n", buf );
6699     ok( num_gettext_msgs == 1, "got %u WM_GETTEXT messages\n", num_gettext_msgs );
6700 
6701     /* test SetWindowTextA */
6702     num_settext_msgs = 0;
6703     success = SetWindowTextA( hwnd, "new_caption" );
6704     ok( success, "SetWindowTextA failed\n" );
6705     ok( num_settext_msgs == 1, "got %u WM_SETTEXT messages\n", num_settext_msgs );
6706 
6707     num_gettext_msgs = 0;
6708     memset( buf, 0, sizeof(buf) );
6709     buf_len = GetWindowTextA( hwnd, buf, sizeof(buf) );
6710     ok( buf_len != 0, "expected a nonempty window text\n" );
6711     ok( !strcmp(buf, "new_caption"), "got wrong window text '%s'\n", buf );
6712     ok( num_gettext_msgs == 1, "got %u WM_GETTEXT messages\n", num_gettext_msgs );
6713 
6714     /* test WM_SETTEXT */
6715     num_settext_msgs = 0;
6716     r = SendMessageA( hwnd, WM_SETTEXT, 0, (ULONG_PTR)"another_caption" );
6717     ok( r != 0, "WM_SETTEXT failed\n" );
6718     ok( num_settext_msgs == 1, "got %u WM_SETTEXT messages\n", num_settext_msgs );
6719 
6720     num_gettext_msgs = 0;
6721     memset( buf, 0, sizeof(buf) );
6722     buf_len = GetWindowTextA( hwnd, buf, sizeof(buf) );
6723     ok( buf_len != 0, "expected a nonempty window text\n" );
6724     ok( !strcmp(buf, "another_caption"), "got wrong window text '%s'\n", buf );
6725     ok( num_gettext_msgs == 1, "got %u WM_GETTEXT messages\n", num_gettext_msgs );
6726 
6727     while (PeekMessageA( &msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE ))
6728         DispatchMessageA( &msg );
6729 
6730     /* test interthread GetWindowTextA */
6731     num_msgs = 0;
6732     thread = CreateThread( NULL, 0, gettext_msg_thread, hwnd, 0, &tid );
6733     ok(thread != NULL, "CreateThread failed, error %d\n", GetLastError());
6734     while (MsgWaitForMultipleObjects( 1, &thread, FALSE, INFINITE, QS_SENDMESSAGE ) != WAIT_OBJECT_0)
6735     {
6736         while (PeekMessageA( &msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE ))
6737             DispatchMessageA( &msg );
6738         num_msgs++;
6739     }
6740     CloseHandle( thread );
6741     ok( num_msgs >= 1, "got %u wakeups from MsgWaitForMultipleObjects\n", num_msgs );
6742 
6743     /* test interthread SetWindowText */
6744     num_msgs = 0;
6745     thread = CreateThread( NULL, 0, settext_msg_thread, hwnd, 0, &tid );
6746     ok(thread != NULL, "CreateThread failed, error %d\n", GetLastError());
6747     while (MsgWaitForMultipleObjects( 1, &thread, FALSE, INFINITE, QS_SENDMESSAGE ) != WAIT_OBJECT_0)
6748     {
6749         while (PeekMessageA( &msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE ))
6750             DispatchMessageA( &msg );
6751         num_msgs++;
6752     }
6753     CloseHandle( thread );
6754     ok( num_msgs >= 1, "got %u wakeups from MsgWaitForMultipleObjects\n", num_msgs );
6755 
6756     num_gettext_msgs = 0;
6757     memset( buf, 0, sizeof(buf) );
6758     buf_len = GetWindowTextA( hwnd, buf, sizeof(buf) );
6759     ok( buf_len != 0, "expected a nonempty window text\n" );
6760     ok( !strcmp(buf, "thread_caption"), "got wrong window text '%s'\n", buf );
6761     ok( num_gettext_msgs == 1, "got %u WM_GETTEXT messages\n", num_gettext_msgs );
6762 
6763     /* seems to crash on every modern Windows version */
6764     if (0)
6765     {
6766     r = SendMessageA( hwnd, WM_GETTEXT, 0x10, 0x1000);
6767     ok( r == 0, "settext should return zero\n");
6768 
6769     r = SendMessageA( hwnd, WM_GETTEXT, 0x10000, 0);
6770     ok( r == 0, "settext should return zero (%ld)\n", r);
6771 
6772     r = SendMessageA( hwnd, WM_GETTEXT, 0xff000000, 0x1000);
6773     ok( r == 0, "settext should return zero (%ld)\n", r);
6774 
6775     r = SendMessageA( hwnd, WM_GETTEXT, 0x1000, 0xff000000);
6776     ok( r == 0, "settext should return zero (%ld)\n", r);
6777     }
6778 
6779     DestroyWindow(hwnd);
6780 }
6781 
6782 
test_GetUpdateRect(void)6783 static void test_GetUpdateRect(void)
6784 {
6785     MSG msg;
6786     BOOL ret, parent_wm_paint, grandparent_wm_paint;
6787     RECT rc1, rc2;
6788     HWND hgrandparent, hparent, hchild;
6789     WNDCLASSA cls;
6790     static const char classNameA[] = "GetUpdateRectClass";
6791 
6792     hgrandparent = CreateWindowA("static", "grandparent", WS_OVERLAPPEDWINDOW,
6793                                  0, 0, 100, 100, NULL, NULL, 0, NULL);
6794 
6795     hparent = CreateWindowA("static", "parent", WS_CHILD|WS_VISIBLE,
6796                             0, 0, 100, 100, hgrandparent, NULL, 0, NULL);
6797 
6798     hchild = CreateWindowA("static", "child", WS_CHILD|WS_VISIBLE,
6799                             10, 10, 30, 30, hparent, NULL, 0, NULL);
6800 
6801     ShowWindow(hgrandparent, SW_SHOW);
6802     UpdateWindow(hgrandparent);
6803     flush_events( TRUE );
6804 
6805     ShowWindow(hchild, SW_HIDE);
6806     SetRectEmpty(&rc2);
6807     ret = GetUpdateRect(hgrandparent, &rc1, FALSE);
6808     ok(!ret, "GetUpdateRect returned not empty region\n");
6809     ok(EqualRect(&rc1, &rc2), "rects do not match %s / %s\n", wine_dbgstr_rect(&rc1),
6810        wine_dbgstr_rect(&rc2));
6811 
6812     SetRect(&rc2, 10, 10, 40, 40);
6813     ret = GetUpdateRect(hparent, &rc1, FALSE);
6814     ok(ret, "GetUpdateRect returned empty region\n");
6815     ok(EqualRect(&rc1, &rc2), "rects do not match %s / %s\n", wine_dbgstr_rect(&rc1),
6816        wine_dbgstr_rect(&rc2));
6817 
6818     parent_wm_paint = FALSE;
6819     grandparent_wm_paint = FALSE;
6820     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE))
6821     {
6822         if (msg.message == WM_PAINT)
6823         {
6824             if (msg.hwnd == hgrandparent) grandparent_wm_paint = TRUE;
6825             if (msg.hwnd == hparent) parent_wm_paint = TRUE;
6826         }
6827         DispatchMessageA(&msg);
6828     }
6829     ok(parent_wm_paint, "WM_PAINT should have been received in parent\n");
6830     ok(!grandparent_wm_paint, "WM_PAINT should NOT have been received in grandparent\n");
6831 
6832     DestroyWindow(hgrandparent);
6833 
6834     cls.style = 0;
6835     cls.lpfnWndProc = DefWindowProcA;
6836     cls.cbClsExtra = 0;
6837     cls.cbWndExtra = 0;
6838     cls.hInstance = GetModuleHandleA(0);
6839     cls.hIcon = 0;
6840     cls.hCursor = LoadCursorA(0, (LPCSTR)IDC_ARROW);
6841     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
6842     cls.lpszMenuName = NULL;
6843     cls.lpszClassName = classNameA;
6844 
6845     if(!RegisterClassA(&cls)) {
6846        trace("Register failed %d\n", GetLastError());
6847        return;
6848     }
6849 
6850     hgrandparent = CreateWindowA(classNameA, "grandparent", WS_OVERLAPPEDWINDOW,
6851                                  0, 0, 100, 100, NULL, NULL, 0, NULL);
6852 
6853     hparent = CreateWindowA(classNameA, "parent", WS_CHILD|WS_VISIBLE,
6854                             0, 0, 100, 100, hgrandparent, NULL, 0, NULL);
6855 
6856     hchild = CreateWindowA(classNameA, "child", WS_CHILD|WS_VISIBLE,
6857                             10, 10, 30, 30, hparent, NULL, 0, NULL);
6858 
6859     ShowWindow(hgrandparent, SW_SHOW);
6860     UpdateWindow(hgrandparent);
6861     flush_events( TRUE );
6862 
6863     ret = GetUpdateRect(hgrandparent, &rc1, FALSE);
6864     ok(!ret, "GetUpdateRect returned not empty region\n");
6865 
6866     ShowWindow(hchild, SW_HIDE);
6867 
6868     SetRectEmpty(&rc2);
6869     ret = GetUpdateRect(hgrandparent, &rc1, FALSE);
6870     ok(!ret, "GetUpdateRect returned not empty region\n");
6871     ok(EqualRect(&rc1, &rc2), "rects do not match %s / %s\n", wine_dbgstr_rect(&rc1),
6872        wine_dbgstr_rect(&rc2));
6873 
6874     SetRect(&rc2, 10, 10, 40, 40);
6875     ret = GetUpdateRect(hparent, &rc1, FALSE);
6876     ok(ret, "GetUpdateRect returned empty region\n");
6877     ok(EqualRect(&rc1, &rc2), "rects do not match %s / %s\n", wine_dbgstr_rect(&rc1),
6878        wine_dbgstr_rect(&rc2));
6879 
6880     parent_wm_paint = FALSE;
6881     grandparent_wm_paint = FALSE;
6882     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE))
6883     {
6884         if (msg.message == WM_PAINT)
6885         {
6886             if (msg.hwnd == hgrandparent) grandparent_wm_paint = TRUE;
6887             if (msg.hwnd == hparent) parent_wm_paint = TRUE;
6888         }
6889         DispatchMessageA(&msg);
6890     }
6891     ok(parent_wm_paint, "WM_PAINT should have been received in parent\n");
6892     ok(!grandparent_wm_paint, "WM_PAINT should NOT have been received in grandparent\n");
6893 
6894     DestroyWindow(hgrandparent);
6895 }
6896 
6897 
TestExposedRegion_WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)6898 static LRESULT CALLBACK TestExposedRegion_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
6899 {
6900     if(msg == WM_PAINT)
6901     {
6902         PAINTSTRUCT ps;
6903         RECT updateRect;
6904         DWORD waitResult;
6905         HWND win;
6906         const int waitTime = 2000;
6907 
6908         BeginPaint(hwnd, &ps);
6909 
6910         /* create and destroy window to create an exposed region on this window */
6911         win = CreateWindowA("static", "win", WS_VISIBLE,
6912                              10,10,50,50, NULL, NULL, 0, NULL);
6913         DestroyWindow(win);
6914 
6915         waitResult = MsgWaitForMultipleObjects( 0, NULL, FALSE, waitTime, QS_PAINT );
6916 
6917         ValidateRect(hwnd, NULL);
6918         EndPaint(hwnd, &ps);
6919 
6920         if(waitResult != WAIT_TIMEOUT)
6921         {
6922             GetUpdateRect(hwnd, &updateRect, FALSE);
6923             ok(IsRectEmpty(&updateRect), "Exposed rect should be empty\n");
6924         }
6925 
6926         return 1;
6927     }
6928     return DefWindowProcA(hwnd, msg, wParam, lParam);
6929 }
6930 
test_Expose(void)6931 static void test_Expose(void)
6932 {
6933     WNDCLASSA cls;
6934     HWND mw;
6935 
6936     memset(&cls, 0, sizeof(WNDCLASSA));
6937     cls.lpfnWndProc = TestExposedRegion_WndProc;
6938     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
6939     cls.lpszClassName = "TestExposeClass";
6940     RegisterClassA(&cls);
6941 
6942     mw = CreateWindowA("TestExposeClass", "MainWindow", WS_VISIBLE|WS_OVERLAPPEDWINDOW,
6943                             0, 0, 200, 100, NULL, NULL, 0, NULL);
6944 
6945     UpdateWindow(mw);
6946     DestroyWindow(mw);
6947 }
6948 
TestNCRedraw_WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)6949 static LRESULT CALLBACK TestNCRedraw_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
6950 {
6951     static UINT ncredrawflags;
6952     PAINTSTRUCT ps;
6953 
6954     switch(msg)
6955     {
6956     case WM_CREATE:
6957         ncredrawflags = *(UINT *) (((CREATESTRUCTA *)lParam)->lpCreateParams);
6958         return 0;
6959     case WM_NCPAINT:
6960         RedrawWindow(hwnd, NULL, NULL, ncredrawflags);
6961         break;
6962     case WM_PAINT:
6963         BeginPaint(hwnd, &ps);
6964         EndPaint(hwnd, &ps);
6965         return 0;
6966     }
6967     return DefWindowProcA(hwnd, msg, wParam, lParam);
6968 }
6969 
run_NCRedrawLoop(UINT flags)6970 static void run_NCRedrawLoop(UINT flags)
6971 {
6972     HWND hwnd;
6973     MSG msg;
6974 
6975     UINT loopcount = 0;
6976 
6977     hwnd = CreateWindowA("TestNCRedrawClass", "MainWindow",
6978                          WS_OVERLAPPEDWINDOW, 0, 0, 200, 100,
6979                          NULL, NULL, 0, &flags);
6980     ShowWindow(hwnd, SW_SHOW);
6981     UpdateWindow(hwnd);
6982     flush_events( FALSE );
6983     while (PeekMessageA(&msg, hwnd, 0, 0, PM_REMOVE))
6984     {
6985         if (msg.message == WM_PAINT) loopcount++;
6986         if (loopcount >= 100) break;
6987         TranslateMessage(&msg);
6988         DispatchMessageA(&msg);
6989         MsgWaitForMultipleObjects(0, NULL, FALSE, 100, QS_ALLINPUT);
6990     }
6991     todo_wine_if (flags == (RDW_INVALIDATE | RDW_FRAME))
6992         ok(loopcount < 100, "Detected infinite WM_PAINT loop (%x).\n", flags);
6993     DestroyWindow(hwnd);
6994 }
6995 
test_NCRedraw(void)6996 static void test_NCRedraw(void)
6997 {
6998     WNDCLASSA wndclass;
6999 
7000     wndclass.lpszClassName = "TestNCRedrawClass";
7001     wndclass.style = CS_HREDRAW | CS_VREDRAW;
7002     wndclass.lpfnWndProc = TestNCRedraw_WndProc;
7003     wndclass.cbClsExtra = 0;
7004     wndclass.cbWndExtra = 0;
7005     wndclass.hInstance = 0;
7006     wndclass.hIcon = LoadIconA(0, (LPCSTR)IDI_APPLICATION);
7007     wndclass.hCursor = LoadCursorA(0, (LPCSTR)IDC_ARROW);
7008     wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
7009     wndclass.lpszMenuName = NULL;
7010 
7011     RegisterClassA(&wndclass);
7012 
7013     run_NCRedrawLoop(RDW_INVALIDATE | RDW_FRAME);
7014     run_NCRedrawLoop(RDW_INVALIDATE);
7015 }
7016 
test_GetWindowModuleFileName(void)7017 static void test_GetWindowModuleFileName(void)
7018 {
7019     HWND hwnd;
7020     HINSTANCE hinst;
7021     UINT ret1, ret2;
7022     char buf1[MAX_PATH], buf2[MAX_PATH];
7023 
7024     if (!pGetWindowModuleFileNameA)
7025     {
7026         win_skip("GetWindowModuleFileNameA is not available\n");
7027         return;
7028     }
7029 
7030     hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0,0,0,0, 0, 0, 0, NULL);
7031     assert(hwnd);
7032 
7033     hinst = (HINSTANCE)GetWindowLongPtrA(hwnd, GWLP_HINSTANCE);
7034     ok(hinst == 0 || broken(hinst == GetModuleHandleA(NULL)), /* win9x */ "expected 0, got %p\n", hinst);
7035 
7036     buf1[0] = 0;
7037     SetLastError(0xdeadbeef);
7038     ret1 = GetModuleFileNameA(hinst, buf1, sizeof(buf1));
7039     ok(ret1, "GetModuleFileName error %u\n", GetLastError());
7040 
7041     buf2[0] = 0;
7042     SetLastError(0xdeadbeef);
7043     ret2 = pGetWindowModuleFileNameA(hwnd, buf2, sizeof(buf2));
7044     ok(ret2 || broken(!ret2), /* nt4 sp 3 */
7045        "GetWindowModuleFileNameA error %u\n", GetLastError());
7046 
7047     if (ret2)
7048     {
7049         ok(ret1 == ret2 || broken(ret2 == ret1 + 1), /* win98 */ "%u != %u\n", ret1, ret2);
7050         ok(!strcmp(buf1, buf2), "%s != %s\n", buf1, buf2);
7051     }
7052     hinst = GetModuleHandleA(NULL);
7053 
7054     SetLastError(0xdeadbeef);
7055     ret2 = GetModuleFileNameA(hinst, buf2, ret1 - 2);
7056     ok(ret2 == ret1 - 2 || broken(ret2 == ret1 - 3), /* win98 */
7057        "expected %u, got %u\n", ret1 - 2, ret2);
7058     ok(GetLastError() == 0xdeadbeef /* XP */ ||
7059        GetLastError() == ERROR_INSUFFICIENT_BUFFER, /* win2k3, vista */
7060        "expected 0xdeadbeef or ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
7061 
7062     SetLastError(0xdeadbeef);
7063     ret2 = GetModuleFileNameA(hinst, buf2, 0);
7064     ok(!ret2, "GetModuleFileName should return 0\n");
7065     ok(GetLastError() == 0xdeadbeef /* XP */ ||
7066        GetLastError() == ERROR_INSUFFICIENT_BUFFER, /* win2k3, vista */
7067        "expected 0xdeadbeef or ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
7068 
7069     SetLastError(0xdeadbeef);
7070     ret2 = pGetWindowModuleFileNameA(hwnd, buf2, ret1 - 2);
7071     ok(ret2 == ret1 - 2 || broken(ret2 == ret1 - 3) /* win98 */ || broken(!ret2), /* nt4 sp3 */
7072        "expected %u, got %u\n", ret1 - 2, ret2);
7073     ok(GetLastError() == 0xdeadbeef /* XP */ ||
7074        GetLastError() == ERROR_INSUFFICIENT_BUFFER, /* win2k3, vista */
7075        "expected 0xdeadbeef or ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
7076 
7077     SetLastError(0xdeadbeef);
7078     ret2 = pGetWindowModuleFileNameA(hwnd, buf2, 0);
7079     ok(!ret2, "expected 0, got %u\n", ret2);
7080     ok(GetLastError() == 0xdeadbeef /* XP */ ||
7081        GetLastError() == ERROR_INSUFFICIENT_BUFFER, /* win2k3, vista */
7082        "expected 0xdeadbeef or ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
7083 
7084     DestroyWindow(hwnd);
7085 
7086     buf2[0] = 0;
7087     hwnd = (HWND)0xdeadbeef;
7088     SetLastError(0xdeadbeef);
7089     ret1 = pGetWindowModuleFileNameA(hwnd, buf1, sizeof(buf1));
7090     ok(!ret1, "expected 0, got %u\n", ret1);
7091     ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE || broken(GetLastError() == 0xdeadbeef), /* win9x */
7092        "expected ERROR_INVALID_WINDOW_HANDLE, got %u\n", GetLastError());
7093 
7094     hwnd = FindWindowA("Shell_TrayWnd", NULL);
7095     ok(IsWindow(hwnd) || broken(!hwnd), "got invalid tray window %p\n", hwnd);
7096     SetLastError(0xdeadbeef);
7097     ret1 = pGetWindowModuleFileNameA(hwnd, buf1, sizeof(buf1));
7098     ok(!ret1 || broken(ret1), /* win98 */ "expected 0, got %u\n", ret1);
7099 
7100     if (!ret1)  /* inter-process GetWindowModuleFileName works on win9x, so don't test the desktop there */
7101     {
7102         ret1 = GetModuleFileNameA(0, buf1, sizeof(buf1));
7103         hwnd = GetDesktopWindow();
7104         ok(IsWindow(hwnd), "got invalid desktop window %p\n", hwnd);
7105         SetLastError(0xdeadbeef);
7106         ret2 = pGetWindowModuleFileNameA(hwnd, buf2, sizeof(buf2));
7107         ok(!ret2 ||
7108            ret1 == ret2 || /* vista */
7109            broken(ret2),  /* some win98 return user.exe as file name */
7110            "expected 0 or %u, got %u %s\n", ret1, ret2, buf2);
7111     }
7112 }
7113 
test_hwnd_message(void)7114 static void test_hwnd_message(void)
7115 {
7116     static const WCHAR mainwindowclassW[] = {'M','a','i','n','W','i','n','d','o','w','C','l','a','s','s',0};
7117     static const WCHAR message_windowW[] = {'m','e','s','s','a','g','e',' ','w','i','n','d','o','w',0};
7118 
7119     HWND parent = 0, hwnd, found;
7120     RECT rect;
7121 
7122     /* HWND_MESSAGE is not supported below w2k, but win9x return != 0
7123        on CreateWindowExA and crash later in the test.
7124        Use UNICODE here to fail on win9x */
7125     hwnd = CreateWindowExW(0, mainwindowclassW, message_windowW, WS_CAPTION | WS_VISIBLE,
7126                            100, 100, 200, 200, HWND_MESSAGE, 0, 0, NULL);
7127     if (!hwnd)
7128     {
7129         win_skip("CreateWindowExW with parent HWND_MESSAGE failed\n");
7130         return;
7131     }
7132 
7133     ok( !GetParent(hwnd), "GetParent should return 0 for message only windows\n" );
7134     if (pGetAncestor)
7135     {
7136         char buffer[100];
7137         HWND root, desktop = GetDesktopWindow();
7138 
7139         parent = pGetAncestor(hwnd, GA_PARENT);
7140         ok(parent != 0, "GetAncestor(GA_PARENT) should not return 0 for message windows\n");
7141         ok(parent != desktop, "GetAncestor(GA_PARENT) should not return desktop for message windows\n");
7142         root = pGetAncestor(hwnd, GA_ROOT);
7143         ok(root == hwnd, "GetAncestor(GA_ROOT) should return hwnd for message windows\n");
7144         ok( !pGetAncestor(parent, GA_PARENT) || broken(pGetAncestor(parent, GA_PARENT) != 0), /* win2k */
7145             "parent shouldn't have parent %p\n", pGetAncestor(parent, GA_PARENT) );
7146         trace("parent %p root %p desktop %p\n", parent, root, desktop);
7147         if (!GetClassNameA( parent, buffer, sizeof(buffer) )) buffer[0] = 0;
7148         ok( !lstrcmpiA( buffer, "Message" ), "wrong parent class '%s'\n", buffer );
7149         GetWindowRect( parent, &rect );
7150         ok( rect.left == 0 && rect.right == 100 && rect.top == 0 && rect.bottom == 100,
7151             "wrong parent rect %s\n", wine_dbgstr_rect( &rect ));
7152     }
7153     GetWindowRect( hwnd, &rect );
7154     ok( rect.left == 100 && rect.right == 300 && rect.top == 100 && rect.bottom == 300,
7155         "wrong window rect %s\n", wine_dbgstr_rect( &rect ));
7156 
7157     /* test FindWindow behavior */
7158 
7159     found = FindWindowExA( 0, 0, 0, "message window" );
7160     ok( found == hwnd, "didn't find message window %p/%p\n", found, hwnd );
7161     SetLastError(0xdeadbeef);
7162     found = FindWindowExA( GetDesktopWindow(), 0, 0, "message window" );
7163     ok( found == 0, "found message window %p/%p\n", found, hwnd );
7164     ok( GetLastError() == 0xdeadbeef, "expected deadbeef, got %d\n", GetLastError() );
7165     if (parent)
7166     {
7167         found = FindWindowExA( parent, 0, 0, "message window" );
7168         ok( found == hwnd, "didn't find message window %p/%p\n", found, hwnd );
7169     }
7170 
7171     /* test IsChild behavior */
7172 
7173     if (parent) ok( !IsChild( parent, hwnd ), "HWND_MESSAGE is child of top window\n" );
7174 
7175     /* test IsWindowVisible behavior */
7176 
7177     ok( !IsWindowVisible( hwnd ), "HWND_MESSAGE window is visible\n" );
7178     if (parent) ok( !IsWindowVisible( parent ), "HWND_MESSAGE parent is visible\n" );
7179 
7180     DestroyWindow(hwnd);
7181 }
7182 
test_layered_window(void)7183 static void test_layered_window(void)
7184 {
7185     HWND hwnd;
7186     COLORREF key = 0;
7187     BYTE alpha = 0;
7188     DWORD flags = 0;
7189     POINT pt = { 0, 0 };
7190     SIZE sz = { 200, 200 };
7191     HDC hdc;
7192     HBITMAP hbm;
7193     BOOL ret;
7194 
7195     if (!pGetLayeredWindowAttributes || !pSetLayeredWindowAttributes || !pUpdateLayeredWindow)
7196     {
7197         win_skip( "layered windows not supported\n" );
7198         return;
7199     }
7200 
7201     hdc = CreateCompatibleDC( 0 );
7202     hbm = CreateCompatibleBitmap( hdc, 200, 200 );
7203     SelectObject( hdc, hbm );
7204 
7205     hwnd = CreateWindowExA(0, "MainWindowClass", "message window", WS_CAPTION,
7206                            100, 100, 200, 200, 0, 0, 0, NULL);
7207     assert( hwnd );
7208     SetLastError( 0xdeadbeef );
7209     ret = pUpdateLayeredWindow( hwnd, 0, NULL, &sz, hdc, &pt, 0, NULL, ULW_OPAQUE );
7210     ok( !ret, "UpdateLayeredWindow should fail on non-layered window\n" );
7211     ok( GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", GetLastError() );
7212     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
7213     ok( !ret, "GetLayeredWindowAttributes should fail on non-layered window\n" );
7214     ret = pSetLayeredWindowAttributes( hwnd, 0, 0, LWA_ALPHA );
7215     ok( !ret, "SetLayeredWindowAttributes should fail on non-layered window\n" );
7216     SetWindowLongA( hwnd, GWL_EXSTYLE, GetWindowLongA(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED );
7217     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
7218     ok( !ret, "GetLayeredWindowAttributes should fail on layered but not initialized window\n" );
7219     ret = pUpdateLayeredWindow( hwnd, 0, NULL, &sz, hdc, &pt, 0, NULL, ULW_OPAQUE );
7220     ok( ret, "UpdateLayeredWindow should succeed on layered window\n" );
7221     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
7222     ok( !ret, "GetLayeredWindowAttributes should fail on layered but not initialized window\n" );
7223     ret = pSetLayeredWindowAttributes( hwnd, 0x123456, 44, LWA_ALPHA );
7224     ok( ret, "SetLayeredWindowAttributes should succeed on layered window\n" );
7225     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
7226     ok( ret, "GetLayeredWindowAttributes should succeed on layered window\n" );
7227     ok( key == 0x123456 || key == 0, "wrong color key %x\n", key );
7228     ok( alpha == 44, "wrong alpha %u\n", alpha );
7229     ok( flags == LWA_ALPHA, "wrong flags %x\n", flags );
7230     SetLastError( 0xdeadbeef );
7231     ret = pUpdateLayeredWindow( hwnd, 0, NULL, &sz, hdc, &pt, 0, NULL, ULW_OPAQUE );
7232     ok( !ret, "UpdateLayeredWindow should fail on layered but initialized window\n" );
7233     ok( GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", GetLastError() );
7234 
7235     /* clearing WS_EX_LAYERED resets attributes */
7236     SetWindowLongA( hwnd, GWL_EXSTYLE, GetWindowLongA(hwnd, GWL_EXSTYLE) & ~WS_EX_LAYERED );
7237     SetLastError( 0xdeadbeef );
7238     ret = pUpdateLayeredWindow( hwnd, 0, NULL, &sz, hdc, &pt, 0, NULL, ULW_OPAQUE );
7239     ok( !ret, "UpdateLayeredWindow should fail on non-layered window\n" );
7240     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
7241     ok( !ret, "GetLayeredWindowAttributes should fail on no longer layered window\n" );
7242     SetWindowLongA( hwnd, GWL_EXSTYLE, GetWindowLongA(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED );
7243     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
7244     ok( !ret, "GetLayeredWindowAttributes should fail on layered but not initialized window\n" );
7245     ret = pUpdateLayeredWindow( hwnd, 0, NULL, &sz, hdc, &pt, 0, NULL, ULW_OPAQUE );
7246     ok( ret, "UpdateLayeredWindow should succeed on layered window\n" );
7247     ret = pUpdateLayeredWindow( hwnd, 0, NULL, &sz, hdc, &pt, 0, NULL, ULW_OPAQUE | ULW_EX_NORESIZE );
7248     ok( !ret, "UpdateLayeredWindow should fail with ex flag\n" );
7249     ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
7250     if (pUpdateLayeredWindowIndirect)
7251     {
7252         UPDATELAYEREDWINDOWINFO info;
7253         info.cbSize   = sizeof(info);
7254         info.hdcDst   = 0;
7255         info.pptDst   = NULL;
7256         info.psize    = &sz;
7257         info.hdcSrc   = hdc;
7258         info.pptSrc   = &pt;
7259         info.crKey    = 0;
7260         info.pblend   = NULL;
7261         info.dwFlags  = ULW_OPAQUE | ULW_EX_NORESIZE;
7262         info.prcDirty = NULL;
7263         ret = pUpdateLayeredWindowIndirect( hwnd, &info );
7264         ok( ret, "UpdateLayeredWindowIndirect should succeed on layered window\n" );
7265         sz.cx--;
7266         SetLastError(0);
7267         ret = pUpdateLayeredWindowIndirect( hwnd, &info );
7268         ok( !ret, "UpdateLayeredWindowIndirect should fail\n" );
7269         /* particular error code differs from version to version, could be ERROR_INCORRECT_SIZE,
7270            ERROR_MR_MID_NOT_FOUND or ERROR_GEN_FAILURE (Win8/Win10) */
7271         ok( GetLastError() != 0, "wrong error %u\n", GetLastError() );
7272         info.dwFlags  = ULW_OPAQUE;
7273         ret = pUpdateLayeredWindowIndirect( hwnd, &info );
7274         ok( ret, "UpdateLayeredWindowIndirect should succeed on layered window\n" );
7275         sz.cx++;
7276         info.dwFlags  = ULW_OPAQUE | 0xf00;
7277         ret = pUpdateLayeredWindowIndirect( hwnd, &info );
7278         ok( !ret, "UpdateLayeredWindowIndirect should fail\n" );
7279         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
7280         info.cbSize--;
7281         info.dwFlags  = ULW_OPAQUE;
7282         ret = pUpdateLayeredWindowIndirect( hwnd, &info );
7283         ok( !ret, "UpdateLayeredWindowIndirect should fail\n" );
7284         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
7285         ret = pUpdateLayeredWindowIndirect( hwnd, NULL );
7286         ok( !ret, "UpdateLayeredWindowIndirect should fail\n" );
7287         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
7288     }
7289 
7290     ret = pSetLayeredWindowAttributes( hwnd, 0x654321, 22, LWA_COLORKEY | LWA_ALPHA );
7291     ok( ret, "SetLayeredWindowAttributes should succeed on layered window\n" );
7292     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
7293     ok( ret, "GetLayeredWindowAttributes should succeed on layered window\n" );
7294     ok( key == 0x654321, "wrong color key %x\n", key );
7295     ok( alpha == 22, "wrong alpha %u\n", alpha );
7296     ok( flags == (LWA_COLORKEY | LWA_ALPHA), "wrong flags %x\n", flags );
7297     SetLastError( 0xdeadbeef );
7298     ret = pUpdateLayeredWindow( hwnd, 0, NULL, &sz, hdc, &pt, 0, NULL, ULW_OPAQUE );
7299     ok( !ret, "UpdateLayeredWindow should fail on layered but initialized window\n" );
7300     ok( GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", GetLastError() );
7301 
7302     ret = pSetLayeredWindowAttributes( hwnd, 0x888888, 33, LWA_COLORKEY );
7303     ok( ret, "SetLayeredWindowAttributes should succeed on layered window\n" );
7304     alpha = 0;
7305     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
7306     ok( ret, "GetLayeredWindowAttributes should succeed on layered window\n" );
7307     ok( key == 0x888888, "wrong color key %x\n", key );
7308     /* alpha not changed on vista if LWA_ALPHA is not set */
7309     ok( alpha == 22 || alpha == 33, "wrong alpha %u\n", alpha );
7310     ok( flags == LWA_COLORKEY, "wrong flags %x\n", flags );
7311 
7312     /* color key may or may not be changed without LWA_COLORKEY */
7313     ret = pSetLayeredWindowAttributes( hwnd, 0x999999, 44, 0 );
7314     ok( ret, "SetLayeredWindowAttributes should succeed on layered window\n" );
7315     alpha = 0;
7316     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
7317     ok( ret, "GetLayeredWindowAttributes should succeed on layered window\n" );
7318     ok( key == 0x888888 || key == 0x999999, "wrong color key %x\n", key );
7319     ok( alpha == 22 || alpha == 44, "wrong alpha %u\n", alpha );
7320     ok( flags == 0, "wrong flags %x\n", flags );
7321 
7322     /* default alpha and color key is 0 */
7323     SetWindowLongA( hwnd, GWL_EXSTYLE, GetWindowLongA(hwnd, GWL_EXSTYLE) & ~WS_EX_LAYERED );
7324     SetWindowLongA( hwnd, GWL_EXSTYLE, GetWindowLongA(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED );
7325     ret = pSetLayeredWindowAttributes( hwnd, 0x222222, 55, 0 );
7326     ok( ret, "SetLayeredWindowAttributes should succeed on layered window\n" );
7327     ret = pGetLayeredWindowAttributes( hwnd, &key, &alpha, &flags );
7328     ok( ret, "GetLayeredWindowAttributes should succeed on layered window\n" );
7329     ok( key == 0 || key == 0x222222, "wrong color key %x\n", key );
7330     ok( alpha == 0 || alpha == 55, "wrong alpha %u\n", alpha );
7331     ok( flags == 0, "wrong flags %x\n", flags );
7332 
7333     DestroyWindow( hwnd );
7334     DeleteDC( hdc );
7335     DeleteObject( hbm );
7336 }
7337 
7338 static MONITORINFO mi;
7339 
fullscreen_wnd_proc(HWND hwnd,UINT msg,WPARAM wp,LPARAM lp)7340 static LRESULT CALLBACK fullscreen_wnd_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
7341 {
7342     switch (msg)
7343     {
7344         case WM_NCCREATE:
7345         {
7346             CREATESTRUCTA *cs = (CREATESTRUCTA *)lp;
7347             ok(cs->x == mi.rcMonitor.left && cs->y == mi.rcMonitor.top &&
7348                cs->cx == mi.rcMonitor.right && cs->cy == mi.rcMonitor.bottom,
7349                "expected %s, got (%d,%d)-(%d,%d)\n", wine_dbgstr_rect(&mi.rcMonitor),
7350                cs->x, cs->y, cs->cx, cs->cy);
7351             break;
7352         }
7353         case WM_GETMINMAXINFO:
7354         {
7355             MINMAXINFO *minmax = (MINMAXINFO *)lp;
7356             ok(minmax->ptMaxPosition.x <= mi.rcMonitor.left, "%d <= %d\n", minmax->ptMaxPosition.x, mi.rcMonitor.left);
7357             ok(minmax->ptMaxPosition.y <= mi.rcMonitor.top, "%d <= %d\n", minmax->ptMaxPosition.y, mi.rcMonitor.top);
7358             ok(minmax->ptMaxSize.x >= mi.rcMonitor.right, "%d >= %d\n", minmax->ptMaxSize.x, mi.rcMonitor.right);
7359             ok(minmax->ptMaxSize.y >= mi.rcMonitor.bottom, "%d >= %d\n", minmax->ptMaxSize.y, mi.rcMonitor.bottom);
7360             break;
7361         }
7362     }
7363     return DefWindowProcA(hwnd, msg, wp, lp);
7364 }
7365 
test_fullscreen(void)7366 static void test_fullscreen(void)
7367 {
7368     static const DWORD t_style[] = {
7369         WS_OVERLAPPED, WS_POPUP, WS_CHILD, WS_THICKFRAME, WS_DLGFRAME
7370     };
7371     static const DWORD t_ex_style[] = {
7372         0, WS_EX_APPWINDOW, WS_EX_TOOLWINDOW
7373     };
7374     WNDCLASSA cls;
7375     HWND hwnd;
7376     int i, j;
7377     POINT pt;
7378     RECT rc;
7379     HMONITOR hmon;
7380     LRESULT ret;
7381 
7382     if (!pGetMonitorInfoA || !pMonitorFromPoint)
7383     {
7384         win_skip("GetMonitorInfoA or MonitorFromPoint are not available on this platform\n");
7385         return;
7386     }
7387 
7388     pt.x = pt.y = 0;
7389     SetLastError(0xdeadbeef);
7390     hmon = pMonitorFromPoint(pt, MONITOR_DEFAULTTOPRIMARY);
7391     ok(hmon != 0, "MonitorFromPoint error %u\n", GetLastError());
7392 
7393     mi.cbSize = sizeof(mi);
7394     SetLastError(0xdeadbeef);
7395     ret = pGetMonitorInfoA(hmon, &mi);
7396     ok(ret, "GetMonitorInfo error %u\n", GetLastError());
7397     trace("monitor %s, work %s\n", wine_dbgstr_rect(&mi.rcMonitor), wine_dbgstr_rect(&mi.rcWork));
7398 
7399     cls.style = 0;
7400     cls.lpfnWndProc = fullscreen_wnd_proc;
7401     cls.cbClsExtra = 0;
7402     cls.cbWndExtra = 0;
7403     cls.hInstance = GetModuleHandleA(NULL);
7404     cls.hIcon = 0;
7405     cls.hCursor = LoadCursorA(0, (LPCSTR)IDC_ARROW);
7406     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
7407     cls.lpszMenuName = NULL;
7408     cls.lpszClassName = "fullscreen_class";
7409     RegisterClassA(&cls);
7410 
7411     for (i = 0; i < sizeof(t_style)/sizeof(t_style[0]); i++)
7412     {
7413         DWORD style, ex_style;
7414 
7415         /* avoid a WM interaction */
7416         assert(!(t_style[i] & WS_VISIBLE));
7417 
7418         for (j = 0; j < sizeof(t_ex_style)/sizeof(t_ex_style[0]); j++)
7419         {
7420             int fixup;
7421 
7422             style = t_style[i];
7423             ex_style = t_ex_style[j];
7424 
7425             hwnd = CreateWindowExA(ex_style, "fullscreen_class", NULL, style,
7426                                    mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right, mi.rcMonitor.bottom,
7427                                    GetDesktopWindow(), 0, GetModuleHandleA(NULL), NULL);
7428             ok(hwnd != 0, "%d: CreateWindowExA(%#x/%#x) failed\n", i, ex_style, style);
7429             GetWindowRect(hwnd, &rc);
7430             trace("%#x/%#x: window rect %s\n", ex_style, style, wine_dbgstr_rect(&rc));
7431             ok(rc.left <= mi.rcMonitor.left && rc.top <= mi.rcMonitor.top &&
7432                rc.right >= mi.rcMonitor.right && rc.bottom >= mi.rcMonitor.bottom,
7433                "%#x/%#x: window rect %s\n", ex_style, style, wine_dbgstr_rect(&rc));
7434             DestroyWindow(hwnd);
7435 
7436             style = t_style[i] | WS_MAXIMIZE;
7437             hwnd = CreateWindowExA(ex_style, "fullscreen_class", NULL, style,
7438                                    mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right, mi.rcMonitor.bottom,
7439                                    GetDesktopWindow(), 0, GetModuleHandleA(NULL), NULL);
7440             ok(hwnd != 0, "%d: CreateWindowExA(%#x/%#x) failed\n", i, ex_style, style);
7441             GetWindowRect(hwnd, &rc);
7442             trace("%#x/%#x: window rect %s\n", ex_style, style, wine_dbgstr_rect(&rc));
7443             ok(rc.left <= mi.rcMonitor.left && rc.top <= mi.rcMonitor.top &&
7444                rc.right >= mi.rcMonitor.right && rc.bottom >= mi.rcMonitor.bottom,
7445                "%#x/%#x: window rect %s\n", ex_style, style, wine_dbgstr_rect(&rc));
7446             DestroyWindow(hwnd);
7447 
7448             style = t_style[i] | WS_MAXIMIZE | WS_CAPTION;
7449             hwnd = CreateWindowExA(ex_style, "fullscreen_class", NULL, style,
7450                                    mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right, mi.rcMonitor.bottom,
7451                                    GetDesktopWindow(), 0, GetModuleHandleA(NULL), NULL);
7452             ok(hwnd != 0, "%d: CreateWindowExA(%#x/%#x) failed\n", i, ex_style, style);
7453             GetWindowRect(hwnd, &rc);
7454             trace("%#x/%#x: window rect %s\n", ex_style, style, wine_dbgstr_rect(&rc));
7455             ok(rc.left <= mi.rcMonitor.left && rc.top <= mi.rcMonitor.top &&
7456                rc.right >= mi.rcMonitor.right && rc.bottom >= mi.rcMonitor.bottom,
7457                "%#x/%#x: window rect %s\n", ex_style, style, wine_dbgstr_rect(&rc));
7458             DestroyWindow(hwnd);
7459 
7460             style = t_style[i] | WS_CAPTION | WS_MAXIMIZEBOX;
7461             hwnd = CreateWindowExA(ex_style, "fullscreen_class", NULL, style,
7462                                    mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right, mi.rcMonitor.bottom,
7463                                    GetDesktopWindow(), 0, GetModuleHandleA(NULL), NULL);
7464             ok(hwnd != 0, "%d: CreateWindowExA(%#x/%#x) failed\n", i, ex_style, style);
7465             GetWindowRect(hwnd, &rc);
7466             trace("%#x/%#x: window rect %s\n", ex_style, style, wine_dbgstr_rect(&rc));
7467             ok(rc.left <= mi.rcMonitor.left && rc.top <= mi.rcMonitor.top &&
7468                rc.right >= mi.rcMonitor.right && rc.bottom >= mi.rcMonitor.bottom,
7469                "%#x/%#x: window rect %s\n", ex_style, style, wine_dbgstr_rect(&rc));
7470             DestroyWindow(hwnd);
7471 
7472             style = t_style[i] | WS_MAXIMIZE | WS_CAPTION | WS_MAXIMIZEBOX;
7473             hwnd = CreateWindowExA(ex_style, "fullscreen_class", NULL, style,
7474                                    mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right, mi.rcMonitor.bottom,
7475                                    GetDesktopWindow(), 0, GetModuleHandleA(NULL), NULL);
7476             ok(hwnd != 0, "%d: CreateWindowExA(%#x/%#x) failed\n", i, ex_style, style);
7477             GetWindowRect(hwnd, &rc);
7478             trace("%#x/%#x: window rect %s\n", ex_style, style, wine_dbgstr_rect(&rc));
7479             /* Windows makes a maximized window slightly larger (to hide the borders?) */
7480             fixup = min(abs(rc.left), abs(rc.top));
7481             InflateRect(&rc, -fixup, -fixup);
7482             ok(rc.left >= mi.rcMonitor.left && rc.top >= mi.rcMonitor.top &&
7483                rc.right <= mi.rcMonitor.right && rc.bottom <= mi.rcMonitor.bottom,
7484                "%#x/%#x: window rect %s must be in %s\n", ex_style, style, wine_dbgstr_rect(&rc),
7485                wine_dbgstr_rect(&mi.rcMonitor));
7486             DestroyWindow(hwnd);
7487 
7488             style = t_style[i] | WS_MAXIMIZE | WS_MAXIMIZEBOX;
7489             hwnd = CreateWindowExA(ex_style, "fullscreen_class", NULL, style,
7490                                    mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right, mi.rcMonitor.bottom,
7491                                    GetDesktopWindow(), 0, GetModuleHandleA(NULL), NULL);
7492             ok(hwnd != 0, "%d: CreateWindowExA(%#x/%#x) failed\n", i, ex_style, style);
7493             GetWindowRect(hwnd, &rc);
7494             trace("%#x/%#x: window rect %s\n", ex_style, style, wine_dbgstr_rect(&rc));
7495             /* Windows makes a maximized window slightly larger (to hide the borders?) */
7496             fixup = min(abs(rc.left), abs(rc.top));
7497             InflateRect(&rc, -fixup, -fixup);
7498             if (style & (WS_CHILD | WS_POPUP))
7499                 ok(rc.left <= mi.rcMonitor.left && rc.top <= mi.rcMonitor.top &&
7500                    rc.right >= mi.rcMonitor.right && rc.bottom >= mi.rcMonitor.bottom,
7501                    "%#x/%#x: window rect %s\n", ex_style, style, wine_dbgstr_rect(&rc));
7502             else
7503                 ok(rc.left >= mi.rcMonitor.left && rc.top >= mi.rcMonitor.top &&
7504                    rc.right <= mi.rcMonitor.right && rc.bottom <= mi.rcMonitor.bottom,
7505                    "%#x/%#x: window rect %s\n", ex_style, style, wine_dbgstr_rect(&rc));
7506             DestroyWindow(hwnd);
7507         }
7508     }
7509 
7510     UnregisterClassA("fullscreen_class", GetModuleHandleA(NULL));
7511 }
7512 
7513 static BOOL test_thick_child_got_minmax;
7514 static const char * test_thick_child_name;
7515 static LONG test_thick_child_style;
7516 static LONG test_thick_child_exStyle;
7517 
test_thick_child_size_winproc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)7518 static LRESULT WINAPI test_thick_child_size_winproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
7519 {
7520     MINMAXINFO* minmax;
7521     int expectedMinTrackX;
7522     int expectedMinTrackY;
7523     int actualMinTrackX;
7524     int actualMinTrackY;
7525     int expectedMaxTrackX;
7526     int expectedMaxTrackY;
7527     int actualMaxTrackX;
7528     int actualMaxTrackY;
7529     int expectedMaxSizeX;
7530     int expectedMaxSizeY;
7531     int actualMaxSizeX;
7532     int actualMaxSizeY;
7533     int expectedPosX;
7534     int expectedPosY;
7535     int actualPosX;
7536     int actualPosY;
7537     LONG adjustedStyle;
7538     RECT rect;
7539     switch (msg)
7540     {
7541         case WM_GETMINMAXINFO:
7542         {
7543             minmax = (MINMAXINFO *)lparam;
7544             trace("hwnd %p, WM_GETMINMAXINFO, %08lx, %08lx\n", hwnd, wparam, lparam);
7545             dump_minmax_info( minmax );
7546 
7547             test_thick_child_got_minmax = TRUE;
7548 
7549 
7550             adjustedStyle = test_thick_child_style;
7551             if ((adjustedStyle & WS_CAPTION) == WS_CAPTION)
7552                 adjustedStyle &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
7553             GetClientRect(GetParent(hwnd), &rect);
7554             AdjustWindowRectEx(&rect, adjustedStyle, FALSE, test_thick_child_exStyle);
7555 
7556             if (test_thick_child_style & (WS_DLGFRAME | WS_BORDER))
7557             {
7558                 expectedMinTrackX = GetSystemMetrics(SM_CXMINTRACK);
7559                 expectedMinTrackY = GetSystemMetrics(SM_CYMINTRACK);
7560             }
7561             else
7562             {
7563                 expectedMinTrackX = -2 * rect.left;
7564                 expectedMinTrackY = -2 * rect.top;
7565             }
7566             actualMinTrackX =  minmax->ptMinTrackSize.x;
7567             actualMinTrackY =  minmax->ptMinTrackSize.y;
7568 
7569             ok(actualMinTrackX == expectedMinTrackX && actualMinTrackY == expectedMinTrackY,
7570                 "expected minTrack %dx%d, actual minTrack %dx%d for %s\n",
7571                 expectedMinTrackX, expectedMinTrackY, actualMinTrackX, actualMinTrackY,
7572                 test_thick_child_name);
7573 
7574             actualMaxTrackX = minmax->ptMaxTrackSize.x;
7575             actualMaxTrackY = minmax->ptMaxTrackSize.y;
7576             expectedMaxTrackX = GetSystemMetrics(SM_CXMAXTRACK);
7577             expectedMaxTrackY = GetSystemMetrics(SM_CYMAXTRACK);
7578             ok(actualMaxTrackX == expectedMaxTrackX &&  actualMaxTrackY == expectedMaxTrackY,
7579                 "expected maxTrack %dx%d, actual maxTrack %dx%d for %s\n",
7580                  expectedMaxTrackX, expectedMaxTrackY, actualMaxTrackX, actualMaxTrackY,
7581                 test_thick_child_name);
7582 
7583             expectedMaxSizeX = rect.right - rect.left;
7584             expectedMaxSizeY = rect.bottom - rect.top;
7585             actualMaxSizeX = minmax->ptMaxSize.x;
7586             actualMaxSizeY = minmax->ptMaxSize.y;
7587 
7588             ok(actualMaxSizeX == expectedMaxSizeX &&  actualMaxSizeY == expectedMaxSizeY,
7589                 "expected maxSize %dx%d, actual maxSize %dx%d for %s\n",
7590                 expectedMaxSizeX, expectedMaxSizeY, actualMaxSizeX, actualMaxSizeY,
7591                 test_thick_child_name);
7592 
7593 
7594             expectedPosX = rect.left;
7595             expectedPosY = rect.top;
7596             actualPosX = minmax->ptMaxPosition.x;
7597             actualPosY = minmax->ptMaxPosition.y;
7598             ok(actualPosX == expectedPosX && actualPosY == expectedPosY,
7599                 "expected maxPosition (%d/%d), actual maxPosition (%d/%d) for %s\n",
7600                 expectedPosX, expectedPosY, actualPosX, actualPosY, test_thick_child_name);
7601 
7602             break;
7603         }
7604     }
7605 
7606     return DefWindowProcA(hwnd, msg, wparam, lparam);
7607 }
7608 
7609 #define NUMBER_OF_THICK_CHILD_TESTS 16
test_thick_child_size(HWND parentWindow)7610 static void test_thick_child_size(HWND parentWindow)
7611 {
7612     BOOL success;
7613     RECT childRect;
7614     RECT adjustedParentRect;
7615     HWND childWindow;
7616     LONG childWidth;
7617     LONG childHeight;
7618     LONG expectedWidth;
7619     LONG expectedHeight;
7620     WNDCLASSA cls;
7621     static const char className[] = "THICK_CHILD_CLASS";
7622     int i;
7623     LONG adjustedStyle;
7624     static const LONG styles[NUMBER_OF_THICK_CHILD_TESTS] = {
7625         WS_CHILD | WS_VISIBLE | WS_THICKFRAME,
7626         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME,
7627         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME | WS_BORDER,
7628         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_BORDER,
7629         WS_CHILD | WS_VISIBLE | WS_THICKFRAME,
7630         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME,
7631         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME | WS_BORDER,
7632         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_BORDER,
7633         WS_CHILD | WS_VISIBLE | WS_THICKFRAME,
7634         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME,
7635         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME | WS_BORDER,
7636         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_BORDER,
7637         WS_CHILD | WS_VISIBLE | WS_THICKFRAME,
7638         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME,
7639         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME | WS_BORDER,
7640         WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_BORDER,
7641     };
7642 
7643     static const LONG exStyles[NUMBER_OF_THICK_CHILD_TESTS] = {
7644         0,
7645         0,
7646         0,
7647         0,
7648         WS_EX_DLGMODALFRAME,
7649         WS_EX_DLGMODALFRAME,
7650         WS_EX_DLGMODALFRAME,
7651         WS_EX_DLGMODALFRAME,
7652         WS_EX_STATICEDGE,
7653         WS_EX_STATICEDGE,
7654         WS_EX_STATICEDGE,
7655         WS_EX_STATICEDGE,
7656         WS_EX_STATICEDGE | WS_EX_DLGMODALFRAME,
7657         WS_EX_STATICEDGE | WS_EX_DLGMODALFRAME,
7658         WS_EX_STATICEDGE | WS_EX_DLGMODALFRAME,
7659         WS_EX_STATICEDGE | WS_EX_DLGMODALFRAME,
7660     };
7661     static const char *styleName[NUMBER_OF_THICK_CHILD_TESTS] = {
7662         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME",
7663         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME",
7664         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME | WS_BORDER",
7665         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_BORDER",
7666         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME, exstyle= WS_EX_DLGMODALFRAME",
7667         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME exstyle= WS_EX_DLGMODALFRAME",
7668         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME | WS_BORDER exstyle= WS_EX_DLGMODALFRAME",
7669         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_BORDER exstyle= WS_EX_DLGMODALFRAME",
7670         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME exstyle= WS_EX_STATICEDGE",
7671         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME exstyle= WS_EX_STATICEDGE",
7672         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME | WS_BORDER exstyle= WS_EX_STATICEDGE",
7673         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_BORDER exstyle= WS_EX_STATICEDGE",
7674         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME, exstyle= WS_EX_STATICEDGE | WS_EX_DLGMODALFRAME",
7675         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME exstyle= WS_EX_STATICEDGE | WS_EX_DLGMODALFRAME",
7676         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_DLGFRAME | WS_BORDER exstyle= WS_EX_STATICEDGE | WS_EX_DLGMODALFRAME",
7677         "style=WS_CHILD | WS_VISIBLE | WS_THICKFRAME | WS_BORDER exstyle= WS_EX_STATICEDGE | WS_EX_DLGMODALFRAME",
7678     };
7679 
7680     cls.style = 0;
7681     cls.lpfnWndProc = test_thick_child_size_winproc;
7682     cls.cbClsExtra = 0;
7683     cls.cbWndExtra = 0;
7684     cls.hInstance = GetModuleHandleA(0);
7685     cls.hIcon = 0;
7686     cls.hCursor = LoadCursorA(0, (LPCSTR)IDC_ARROW);
7687     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
7688     cls.lpszMenuName = NULL;
7689     cls.lpszClassName = className;
7690     SetLastError(0xdeadbeef);
7691     success = RegisterClassA(&cls);
7692     ok(success,"RegisterClassA failed, error: %u\n", GetLastError());
7693 
7694     for(i = 0; i < NUMBER_OF_THICK_CHILD_TESTS; i++)
7695     {
7696         test_thick_child_name = styleName[i];
7697         test_thick_child_style = styles[i];
7698         test_thick_child_exStyle = exStyles[i];
7699         test_thick_child_got_minmax = FALSE;
7700 
7701         SetLastError(0xdeadbeef);
7702         childWindow = CreateWindowExA( exStyles[i], className, "", styles[i],  0, 0, 0, 0, parentWindow, 0,  GetModuleHandleA(0),  NULL );
7703         ok(childWindow != NULL, "Failed to create child window, error: %u\n", GetLastError());
7704 
7705         ok(test_thick_child_got_minmax, "Got no WM_GETMINMAXINFO\n");
7706 
7707         SetLastError(0xdeadbeef);
7708         success = GetWindowRect(childWindow, &childRect);
7709         ok(success,"GetWindowRect call failed, error: %u\n", GetLastError());
7710         childWidth = childRect.right - childRect.left;
7711         childHeight = childRect.bottom - childRect.top;
7712 
7713         adjustedStyle = styles[i];
7714         if ((adjustedStyle & WS_CAPTION) == WS_CAPTION)
7715             adjustedStyle &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
7716         GetClientRect(GetParent(childWindow), &adjustedParentRect);
7717         AdjustWindowRectEx(&adjustedParentRect, adjustedStyle, FALSE, test_thick_child_exStyle);
7718 
7719 
7720         if (test_thick_child_style & (WS_DLGFRAME | WS_BORDER))
7721         {
7722             expectedWidth = GetSystemMetrics(SM_CXMINTRACK);
7723             expectedHeight = GetSystemMetrics(SM_CYMINTRACK);
7724         }
7725         else
7726         {
7727             expectedWidth = -2 * adjustedParentRect.left;
7728             expectedHeight = -2 * adjustedParentRect.top;
7729         }
7730 
7731         ok((childWidth == expectedWidth) && (childHeight == expectedHeight),
7732             "size of window (%s) is wrong: expected size %dx%d != actual size %dx%d\n",
7733             test_thick_child_name, expectedWidth, expectedHeight, childWidth, childHeight);
7734 
7735         SetLastError(0xdeadbeef);
7736         success = DestroyWindow(childWindow);
7737         ok(success,"DestroyWindow call failed, error: %u\n", GetLastError());
7738     }
7739     ok(UnregisterClassA(className, GetModuleHandleA(NULL)),"UnregisterClass call failed\n");
7740 }
7741 
test_handles(HWND full_hwnd)7742 static void test_handles( HWND full_hwnd )
7743 {
7744     HWND hwnd = full_hwnd;
7745     BOOL ret;
7746     RECT rect;
7747 
7748     SetLastError( 0xdeadbeef );
7749     ret = GetWindowRect( hwnd, &rect );
7750     ok( ret, "GetWindowRect failed for %p err %u\n", hwnd, GetLastError() );
7751 
7752 #ifdef _WIN64
7753     if ((ULONG_PTR)full_hwnd >> 32)
7754         hwnd = (HWND)((ULONG_PTR)full_hwnd & ~0u);
7755     else
7756         hwnd = (HWND)((ULONG_PTR)full_hwnd | ((ULONG_PTR)~0u << 32));
7757     SetLastError( 0xdeadbeef );
7758     ret = GetWindowRect( hwnd, &rect );
7759     ok( ret, "GetWindowRect failed for %p err %u\n", hwnd, GetLastError() );
7760 
7761     hwnd = (HWND)(((ULONG_PTR)full_hwnd & ~0u) | ((ULONG_PTR)0x1234 << 32));
7762     SetLastError( 0xdeadbeef );
7763     ret = GetWindowRect( hwnd, &rect );
7764     ok( ret, "GetWindowRect failed for %p err %u\n", hwnd, GetLastError() );
7765 
7766     hwnd = (HWND)(((ULONG_PTR)full_hwnd & 0xffff) | ((ULONG_PTR)0x9876 << 16));
7767     SetLastError( 0xdeadbeef );
7768     ret = GetWindowRect( hwnd, &rect );
7769     ok( !ret, "GetWindowRect succeeded for %p\n", hwnd );
7770     ok( GetLastError() == ERROR_INVALID_WINDOW_HANDLE, "wrong error %u\n", GetLastError() );
7771 
7772     hwnd = (HWND)(((ULONG_PTR)full_hwnd & 0xffff) | ((ULONG_PTR)0x12345678 << 16));
7773     SetLastError( 0xdeadbeef );
7774     ret = GetWindowRect( hwnd, &rect );
7775     ok( !ret, "GetWindowRect succeeded for %p\n", hwnd );
7776     ok( GetLastError() == ERROR_INVALID_WINDOW_HANDLE, "wrong error %u\n", GetLastError() );
7777 #endif
7778 }
7779 
test_winregion(void)7780 static void test_winregion(void)
7781 {
7782     HWND hwnd;
7783     RECT r;
7784     int ret, width;
7785     HRGN hrgn;
7786 
7787     if (!pGetWindowRgnBox)
7788     {
7789         win_skip("GetWindowRgnBox not supported\n");
7790         return;
7791     }
7792 
7793     hwnd = CreateWindowExA(0, "static", NULL, WS_VISIBLE, 10, 10, 10, 10, NULL, 0, 0, NULL);
7794     /* NULL prect */
7795     SetLastError(0xdeadbeef);
7796     ret = pGetWindowRgnBox(hwnd, NULL);
7797     ok( ret == ERROR, "Expected ERROR, got %d\n", ret);
7798     ok( GetLastError() == 0xdeadbeef, "Expected , got %d\n", GetLastError());
7799 
7800     hrgn = CreateRectRgn(2, 3, 10, 15);
7801     ok( hrgn != NULL, "Region creation failed\n");
7802     if (hrgn)
7803     {
7804         SetWindowRgn(hwnd, hrgn, FALSE);
7805 
7806         SetLastError(0xdeadbeef);
7807         ret = pGetWindowRgnBox(hwnd, NULL);
7808         ok( ret == ERROR, "Expected ERROR, got %d\n", ret);
7809         ok( GetLastError() == 0xdeadbeef, "Expected , got %d\n", GetLastError());
7810 
7811         SetRectEmpty(&r);
7812         ret = pGetWindowRgnBox(hwnd, &r);
7813         ok( ret == SIMPLEREGION, "Expected SIMPLEREGION, got %d\n", ret);
7814         ok( r.left == 2 && r.top == 3 && r.right == 10 && r.bottom == 15,
7815            "Expected (2,3)-(10,15), got %s\n", wine_dbgstr_rect( &r ));
7816         if (pMirrorRgn)
7817         {
7818             hrgn = CreateRectRgn(2, 3, 10, 15);
7819             ret = pMirrorRgn( hwnd, hrgn );
7820             ok( ret == TRUE, "MirrorRgn failed %u\n", ret );
7821             SetRectEmpty(&r);
7822             GetWindowRect( hwnd, &r );
7823             width = r.right - r.left;
7824             SetRectEmpty(&r);
7825             ret = GetRgnBox( hrgn, &r );
7826             ok( ret == SIMPLEREGION, "GetRgnBox failed %u\n", ret );
7827             ok( r.left == width - 10 && r.top == 3 && r.right == width - 2 && r.bottom == 15,
7828                 "Wrong rectangle %s for width %d\n", wine_dbgstr_rect( &r ), width );
7829         }
7830         else win_skip( "MirrorRgn not supported\n" );
7831     }
7832     DestroyWindow(hwnd);
7833 }
7834 
test_rtl_layout(void)7835 static void test_rtl_layout(void)
7836 {
7837     HWND parent, child;
7838     RECT r;
7839     POINT pt;
7840 
7841     if (!pSetProcessDefaultLayout)
7842     {
7843         win_skip( "SetProcessDefaultLayout not supported\n" );
7844         return;
7845     }
7846 
7847     parent = CreateWindowExA(WS_EX_LAYOUTRTL, "static", NULL, WS_POPUP, 100, 100, 300, 300, NULL, 0, 0, NULL);
7848     child = CreateWindowExA(0, "static", NULL, WS_CHILD, 10, 10, 20, 20, parent, 0, 0, NULL);
7849 
7850     GetWindowRect( parent, &r );
7851     ok( r.left == 100 && r.right == 400, "wrong rect %s\n", wine_dbgstr_rect( &r ));
7852     GetClientRect( parent, &r );
7853     ok( r.left == 0 && r.right == 300, "wrong rect %s\n", wine_dbgstr_rect( &r ));
7854     GetClientRect( child, &r );
7855     ok( r.left == 0 && r.right == 20, "wrong rect %s\n", wine_dbgstr_rect( &r ));
7856     MapWindowPoints( child, parent, (POINT *)&r, 2 );
7857     ok( r.left == 10 && r.right == 30, "wrong rect %s\n", wine_dbgstr_rect( &r ));
7858     GetWindowRect( child, &r );
7859     ok( r.left == 370 && r.right == 390, "wrong rect %s\n", wine_dbgstr_rect( &r ));
7860     MapWindowPoints( NULL, parent, (POINT *)&r, 2 );
7861     ok( r.left == 10 && r.right == 30, "wrong rect %s\n", wine_dbgstr_rect( &r ));
7862     GetWindowRect( child, &r );
7863     MapWindowPoints( NULL, parent, (POINT *)&r, 1 );
7864     MapWindowPoints( NULL, parent, (POINT *)&r + 1, 1 );
7865     ok( r.left == 30 && r.right == 10, "wrong rect %s\n", wine_dbgstr_rect( &r ));
7866     pt.x = pt.y = 12;
7867     MapWindowPoints( child, parent, &pt, 1 );
7868     ok( pt.x == 22 && pt.y == 22, "wrong point %d,%d\n", pt.x, pt.y );
7869     SetWindowPos( parent, 0, 0, 0, 250, 250, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE );
7870     GetWindowRect( parent, &r );
7871     ok( r.left == 100 && r.right == 350, "wrong rect %s\n", wine_dbgstr_rect( &r ));
7872     GetWindowRect( child, &r );
7873     ok( r.left == 320 && r.right == 340, "wrong rect %s\n", wine_dbgstr_rect( &r ));
7874     SetWindowLongW( parent, GWL_EXSTYLE, 0 );
7875     GetWindowRect( child, &r );
7876     ok( r.left == 320 && r.right == 340, "wrong rect %s\n", wine_dbgstr_rect( &r ));
7877     MapWindowPoints( NULL, parent, (POINT *)&r, 2 );
7878     ok( r.left == 220 && r.right == 240, "wrong rect %s\n", wine_dbgstr_rect( &r ));
7879     SetWindowLongW( parent, GWL_EXSTYLE, WS_EX_LAYOUTRTL );
7880     GetWindowRect( child, &r );
7881     ok( r.left == 320 && r.right == 340, "wrong rect %s\n", wine_dbgstr_rect( &r ));
7882     MapWindowPoints( NULL, parent, (POINT *)&r, 2 );
7883     ok( r.left == 10 && r.right == 30, "wrong rect %s\n", wine_dbgstr_rect( &r ));
7884     SetWindowPos( child, 0, 0, 0, 30, 30, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE );
7885     GetWindowRect( child, &r );
7886     ok( r.left == 310 && r.right == 340, "wrong rect %s\n", wine_dbgstr_rect( &r ));
7887     MapWindowPoints( NULL, parent, (POINT *)&r, 2 );
7888     ok( r.left == 10 && r.right == 40, "wrong rect %s\n", wine_dbgstr_rect( &r ));
7889     DestroyWindow( child );
7890     DestroyWindow( parent );
7891 }
7892 
test_FlashWindow(void)7893 static void test_FlashWindow(void)
7894 {
7895     HWND hwnd;
7896     BOOL ret;
7897     if (!pFlashWindow)
7898     {
7899         win_skip( "FlashWindow not supported\n" );
7900         return;
7901     }
7902 
7903     hwnd = CreateWindowExA( 0, "MainWindowClass", "FlashWindow", WS_POPUP,
7904                             0, 0, 0, 0, 0, 0, 0, NULL );
7905     ok( hwnd != 0, "CreateWindowExA error %d\n", GetLastError() );
7906 
7907     SetLastError( 0xdeadbeef );
7908     ret = pFlashWindow( NULL, TRUE );
7909     ok( !ret && GetLastError() == ERROR_INVALID_PARAMETER,
7910         "FlashWindow returned with %d\n", GetLastError() );
7911 
7912     DestroyWindow( hwnd );
7913 
7914     SetLastError( 0xdeadbeef );
7915     ret = pFlashWindow( hwnd, TRUE );
7916     ok( !ret && GetLastError() == ERROR_INVALID_PARAMETER,
7917         "FlashWindow returned with %d\n", GetLastError() );
7918 }
7919 
test_FlashWindowEx(void)7920 static void test_FlashWindowEx(void)
7921 {
7922     HWND hwnd;
7923     FLASHWINFO finfo;
7924     BOOL prev, ret;
7925 
7926     if (!pFlashWindowEx)
7927     {
7928         win_skip( "FlashWindowEx not supported\n" );
7929         return;
7930     }
7931 
7932     hwnd = CreateWindowExA( 0, "MainWindowClass", "FlashWindow", WS_POPUP,
7933                             0, 0, 0, 0, 0, 0, 0, NULL );
7934     ok( hwnd != 0, "CreateWindowExA error %d\n", GetLastError() );
7935 
7936     finfo.cbSize = sizeof(FLASHWINFO);
7937     finfo.dwFlags = FLASHW_TIMER;
7938     finfo.uCount = 3;
7939     finfo.dwTimeout = 200;
7940     finfo.hwnd = NULL;
7941     SetLastError(0xdeadbeef);
7942     ret = pFlashWindowEx(&finfo);
7943     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
7944        "FlashWindowEx returned with %d\n", GetLastError());
7945 
7946     finfo.hwnd = hwnd;
7947     SetLastError(0xdeadbeef);
7948     ret = pFlashWindowEx(NULL);
7949     ok(!ret && GetLastError() == ERROR_NOACCESS,
7950        "FlashWindowEx returned with %d\n", GetLastError());
7951 
7952     SetLastError(0xdeadbeef);
7953     ret = pFlashWindowEx(&finfo);
7954     todo_wine ok(!ret, "previous window state should not be active\n");
7955 
7956     finfo.cbSize = sizeof(FLASHWINFO) - 1;
7957     SetLastError(0xdeadbeef);
7958     ret = pFlashWindowEx(&finfo);
7959     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
7960        "FlashWindowEx succeeded\n");
7961 
7962     finfo.cbSize = sizeof(FLASHWINFO) + 1;
7963     SetLastError(0xdeadbeef);
7964     ret = pFlashWindowEx(&finfo);
7965     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
7966        "FlashWindowEx succeeded\n");
7967     finfo.cbSize = sizeof(FLASHWINFO);
7968 
7969     DestroyWindow( hwnd );
7970 
7971     SetLastError(0xdeadbeef);
7972     ret = pFlashWindowEx(&finfo);
7973     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
7974        "FlashWindowEx returned with %d\n", GetLastError());
7975 
7976     ok(finfo.cbSize == sizeof(FLASHWINFO), "FlashWindowEx modified cdSize to %x\n", finfo.cbSize);
7977     ok(finfo.hwnd == hwnd, "FlashWindowEx modified hwnd to %p\n", finfo.hwnd);
7978     ok(finfo.dwFlags == FLASHW_TIMER, "FlashWindowEx modified dwFlags to %x\n", finfo.dwFlags);
7979     ok(finfo.uCount == 3, "FlashWindowEx modified uCount to %x\n", finfo.uCount);
7980     ok(finfo.dwTimeout == 200, "FlashWindowEx modified dwTimeout to %x\n", finfo.dwTimeout);
7981 
7982     hwnd = CreateWindowExA( 0, "MainWindowClass", "FlashWindow", WS_VISIBLE | WS_POPUPWINDOW,
7983                             0, 0, 0, 0, 0, 0, 0, NULL );
7984     ok( hwnd != 0, "CreateWindowExA error %d\n", GetLastError() );
7985     finfo.hwnd = hwnd;
7986 
7987     SetLastError(0xdeadbeef);
7988     ret = pFlashWindowEx(NULL);
7989     ok(!ret && GetLastError() == ERROR_NOACCESS,
7990        "FlashWindowEx returned with %d\n", GetLastError());
7991 
7992     SetLastError(0xdeadbeef);
7993     prev = pFlashWindowEx(&finfo);
7994 
7995     ok(finfo.cbSize == sizeof(FLASHWINFO), "FlashWindowEx modified cdSize to %x\n", finfo.cbSize);
7996     ok(finfo.hwnd == hwnd, "FlashWindowEx modified hwnd to %p\n", finfo.hwnd);
7997     ok(finfo.dwFlags == FLASHW_TIMER, "FlashWindowEx modified dwFlags to %x\n", finfo.dwFlags);
7998     ok(finfo.uCount == 3, "FlashWindowEx modified uCount to %x\n", finfo.uCount);
7999     ok(finfo.dwTimeout == 200, "FlashWindowEx modified dwTimeout to %x\n", finfo.dwTimeout);
8000 
8001     finfo.dwFlags = FLASHW_STOP;
8002     SetLastError(0xdeadbeef);
8003     ret = pFlashWindowEx(&finfo);
8004     ok(prev != ret, "previous window state should be different\n");
8005 
8006     DestroyWindow( hwnd );
8007 }
8008 
test_FindWindowEx(void)8009 static void test_FindWindowEx(void)
8010 {
8011     HWND hwnd, found;
8012 
8013     hwnd = CreateWindowExA( 0, "MainWindowClass", "caption", WS_POPUP, 0,0,0,0, 0, 0, 0, NULL );
8014     ok( hwnd != 0, "CreateWindowExA error %d\n", GetLastError() );
8015 
8016     num_gettext_msgs = 0;
8017     found = FindWindowExA( 0, 0, "MainWindowClass", "" );
8018     ok( found == NULL, "expected a NULL hwnd\n" );
8019     ok( num_gettext_msgs == 0, "got %u WM_GETTEXT messages\n", num_gettext_msgs );
8020 
8021     num_gettext_msgs = 0;
8022     found = FindWindowExA( 0, 0, "MainWindowClass", NULL );
8023     ok( found == hwnd, "found is %p, expected a valid hwnd\n", found );
8024     ok( num_gettext_msgs == 0, "got %u WM_GETTEXT messages\n", num_gettext_msgs );
8025 
8026     num_gettext_msgs = 0;
8027     found = FindWindowExA( 0, 0, "MainWindowClass", "caption" );
8028     ok( found == hwnd, "found is %p, expected a valid hwnd\n", found );
8029     ok( num_gettext_msgs == 0, "got %u WM_GETTEXT messages\n", num_gettext_msgs );
8030 
8031     DestroyWindow( hwnd );
8032 
8033     hwnd = CreateWindowExA( 0, "MainWindowClass", NULL, WS_POPUP, 0,0,0,0, 0, 0, 0, NULL );
8034     ok( hwnd != 0, "CreateWindowExA error %d\n", GetLastError() );
8035 
8036     num_gettext_msgs = 0;
8037     found = FindWindowExA( 0, 0, "MainWindowClass", "" );
8038     ok( found == hwnd, "found is %p, expected a valid hwnd\n", found );
8039     ok( num_gettext_msgs == 0, "got %u WM_GETTEXT messages\n", num_gettext_msgs );
8040 
8041     num_gettext_msgs = 0;
8042     found = FindWindowExA( 0, 0, "MainWindowClass", NULL );
8043     ok( found == hwnd, "found is %p, expected a valid hwnd\n", found );
8044     ok( num_gettext_msgs == 0, "got %u WM_GETTEXT messages\n", num_gettext_msgs );
8045 
8046     DestroyWindow( hwnd );
8047 
8048     /* test behaviour with a window title that is an empty character */
8049     found = FindWindowExA( 0, 0, "Shell_TrayWnd", "" );
8050     ok( found != NULL, "found is NULL, expected a valid hwnd\n" );
8051     found = FindWindowExA( 0, 0, "Shell_TrayWnd", NULL );
8052     ok( found != NULL, "found is NULL, expected a valid hwnd\n" );
8053 }
8054 
test_GetLastActivePopup(void)8055 static void test_GetLastActivePopup(void)
8056 {
8057     HWND hwndOwner, hwndPopup1, hwndPopup2;
8058 
8059     hwndOwner = CreateWindowExA(0, "MainWindowClass", NULL,
8060                                 WS_VISIBLE | WS_POPUPWINDOW,
8061                                 100, 100, 200, 200,
8062                                 NULL, 0, GetModuleHandleA(NULL), NULL);
8063     hwndPopup1 = CreateWindowExA(0, "MainWindowClass", NULL,
8064                                  WS_VISIBLE | WS_POPUPWINDOW,
8065                                  100, 100, 200, 200,
8066                                  hwndOwner, 0, GetModuleHandleA(NULL), NULL);
8067     hwndPopup2 = CreateWindowExA(0, "MainWindowClass", NULL,
8068                                  WS_VISIBLE | WS_POPUPWINDOW,
8069                                  100, 100, 200, 200,
8070                                  hwndPopup1, 0, GetModuleHandleA(NULL), NULL);
8071     ok( GetLastActivePopup(hwndOwner) == hwndPopup2, "wrong last active popup\n" );
8072     DestroyWindow( hwndPopup2 );
8073     DestroyWindow( hwndPopup1 );
8074     DestroyWindow( hwndOwner );
8075 }
8076 
my_httrasparent_proc(HWND hwnd,UINT msg,WPARAM wp,LPARAM lp)8077 static LRESULT WINAPI my_httrasparent_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
8078 {
8079     if (msg == WM_NCHITTEST) return HTTRANSPARENT;
8080     return DefWindowProcA(hwnd, msg, wp, lp);
8081 }
8082 
my_window_proc(HWND hwnd,UINT msg,WPARAM wp,LPARAM lp)8083 static LRESULT WINAPI my_window_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
8084 {
8085     return DefWindowProcA(hwnd, msg, wp, lp);
8086 }
8087 
create_window_tree(HWND parent,HWND * window,int size)8088 static void create_window_tree(HWND parent, HWND *window, int size)
8089 {
8090     static const DWORD style[] = { 0, WS_VISIBLE, WS_DISABLED, WS_VISIBLE | WS_DISABLED };
8091     int i, pos;
8092 
8093     memset(window, 0, size * sizeof(window[0]));
8094 
8095     pos = 0;
8096     for (i = 0; i < sizeof(style)/sizeof(style[0]); i++)
8097     {
8098         assert(pos < size);
8099         window[pos] = CreateWindowExA(0, "my_window", NULL, style[i] | WS_CHILD,
8100                                      0, 0, 100, 100, parent, 0, 0, NULL);
8101         ok(window[pos] != 0, "CreateWindowEx failed\n");
8102         pos++;
8103         assert(pos < size);
8104         window[pos] = CreateWindowExA(WS_EX_TRANSPARENT, "my_window", NULL, style[i] | WS_CHILD,
8105                                      0, 0, 100, 100, parent, 0, 0, NULL);
8106         ok(window[pos] != 0, "CreateWindowEx failed\n");
8107         pos++;
8108 
8109         assert(pos < size);
8110         window[pos] = CreateWindowExA(0, "my_httrasparent", NULL, style[i] | WS_CHILD,
8111                                      0, 0, 100, 100, parent, 0, 0, NULL);
8112         ok(window[pos] != 0, "CreateWindowEx failed\n");
8113         pos++;
8114         assert(pos < size);
8115         window[pos] = CreateWindowExA(WS_EX_TRANSPARENT, "my_httrasparent", NULL, style[i] | WS_CHILD,
8116                                      0, 0, 100, 100, parent, 0, 0, NULL);
8117         ok(window[pos] != 0, "CreateWindowEx failed\n");
8118         pos++;
8119 
8120         assert(pos < size);
8121         window[pos] = CreateWindowExA(0, "my_button", NULL, style[i] | WS_CHILD | BS_GROUPBOX,
8122                                      0, 0, 100, 100, parent, 0, 0, NULL);
8123         ok(window[pos] != 0, "CreateWindowEx failed\n");
8124         pos++;
8125         assert(pos < size);
8126         window[pos] = CreateWindowExA(WS_EX_TRANSPARENT, "my_button", NULL, style[i] | WS_CHILD | BS_GROUPBOX,
8127                                      0, 0, 100, 100, parent, 0, 0, NULL);
8128         ok(window[pos] != 0, "CreateWindowEx failed\n");
8129         pos++;
8130         assert(pos < size);
8131         window[pos] = CreateWindowExA(0, "my_button", NULL, style[i] | WS_CHILD | BS_PUSHBUTTON,
8132                                      0, 0, 100, 100, parent, 0, 0, NULL);
8133         ok(window[pos] != 0, "CreateWindowEx failed\n");
8134         pos++;
8135         assert(pos < size);
8136         window[pos] = CreateWindowExA(WS_EX_TRANSPARENT, "my_button", NULL, style[i] | WS_CHILD | BS_PUSHBUTTON,
8137                                      0, 0, 100, 100, parent, 0, 0, NULL);
8138         ok(window[pos] != 0, "CreateWindowEx failed\n");
8139         pos++;
8140 
8141         assert(pos < size);
8142         window[pos] = CreateWindowExA(0, "Button", NULL, style[i] | WS_CHILD | BS_GROUPBOX,
8143                                      0, 0, 100, 100, parent, 0, 0, NULL);
8144         ok(window[pos] != 0, "CreateWindowEx failed\n");
8145         pos++;
8146         assert(pos < size);
8147         window[pos] = CreateWindowExA(WS_EX_TRANSPARENT, "Button", NULL, style[i] | WS_CHILD | BS_GROUPBOX,
8148                                      0, 0, 100, 100, parent, 0, 0, NULL);
8149         ok(window[pos] != 0, "CreateWindowEx failed\n");
8150         pos++;
8151         assert(pos < size);
8152         window[pos] = CreateWindowExA(0, "Button", NULL, style[i] | WS_CHILD | BS_PUSHBUTTON,
8153                                      0, 0, 100, 100, parent, 0, 0, NULL);
8154         ok(window[pos] != 0, "CreateWindowEx failed\n");
8155         pos++;
8156         assert(pos < size);
8157         window[pos] = CreateWindowExA(WS_EX_TRANSPARENT, "Button", NULL, style[i] | WS_CHILD | BS_PUSHBUTTON,
8158                                      0, 0, 100, 100, parent, 0, 0, NULL);
8159         ok(window[pos] != 0, "CreateWindowEx failed\n");
8160         pos++;
8161 
8162         assert(pos < size);
8163         window[pos] = CreateWindowExA(0, "Static", NULL, style[i] | WS_CHILD,
8164                                      0, 0, 100, 100, parent, 0, 0, NULL);
8165         ok(window[pos] != 0, "CreateWindowEx failed\n");
8166         pos++;
8167         assert(pos < size);
8168         window[pos] = CreateWindowExA(WS_EX_TRANSPARENT, "Static", NULL, style[i] | WS_CHILD,
8169                                      0, 0, 100, 100, parent, 0, 0, NULL);
8170         ok(window[pos] != 0, "CreateWindowEx failed\n");
8171         pos++;
8172     }
8173 }
8174 
8175 struct window_attributes
8176 {
8177     char class_name[128];
8178     BOOL is_visible, is_enabled, is_groupbox, is_httransparent, is_extransparent;
8179 };
8180 
get_window_attributes(HWND hwnd,struct window_attributes * attrs)8181 static void get_window_attributes(HWND hwnd, struct window_attributes *attrs)
8182 {
8183     DWORD style, ex_style, hittest;
8184 
8185     style = GetWindowLongA(hwnd, GWL_STYLE);
8186     ex_style = GetWindowLongA(hwnd, GWL_EXSTYLE);
8187     attrs->class_name[0] = 0;
8188     GetClassNameA(hwnd, attrs->class_name, sizeof(attrs->class_name));
8189     hittest = SendMessageA(hwnd, WM_NCHITTEST, 0, 0);
8190 
8191     attrs->is_visible = (style & WS_VISIBLE) != 0;
8192     attrs->is_enabled = (style & WS_DISABLED) == 0;
8193     attrs->is_groupbox = !lstrcmpiA(attrs->class_name, "Button") && (style & BS_TYPEMASK) == BS_GROUPBOX;
8194     attrs->is_httransparent = hittest == HTTRANSPARENT;
8195     attrs->is_extransparent = (ex_style & WS_EX_TRANSPARENT) != 0;
8196 }
8197 
window_to_index(HWND hwnd,HWND * window,int size)8198 static int window_to_index(HWND hwnd, HWND *window, int size)
8199 {
8200     int i;
8201 
8202     for (i = 0; i < size; i++)
8203     {
8204         if (!window[i]) break;
8205         if (window[i] == hwnd) return i;
8206     }
8207     return -1;
8208 }
8209 
test_child_window_from_point(void)8210 static void test_child_window_from_point(void)
8211 {
8212     static const int real_child_pos[] = { 14,15,16,17,18,19,20,21,24,25,26,27,42,43,
8213                                           44,45,46,47,48,49,52,53,54,55,51,50,23,22,-1 };
8214     static const int real_child_pos_nt4[] = { 14,15,16,17,20,21,24,25,26,27,42,43,44,45,
8215                                               48,49,52,53,54,55,51,50,47,46,23,22,19,18,-1 };
8216     WNDCLASSA cls;
8217     HWND hwnd, parent, window[100];
8218     POINT pt;
8219     int found_invisible, found_disabled, found_groupbox, found_httransparent, found_extransparent;
8220     int ret, i;
8221 
8222     ret = GetClassInfoA(0, "Button", &cls);
8223     ok(ret, "GetClassInfo(Button) failed\n");
8224     cls.lpszClassName = "my_button";
8225     ret = RegisterClassA(&cls);
8226     ok(ret, "RegisterClass(my_button) failed\n");
8227 
8228     cls.lpszClassName = "my_httrasparent";
8229     cls.lpfnWndProc = my_httrasparent_proc;
8230     ret = RegisterClassA(&cls);
8231     ok(ret, "RegisterClass(my_httrasparent) failed\n");
8232 
8233     cls.lpszClassName = "my_window";
8234     cls.lpfnWndProc = my_window_proc;
8235     ret = RegisterClassA(&cls);
8236     ok(ret, "RegisterClass(my_window) failed\n");
8237 
8238     parent = CreateWindowExA(0, "MainWindowClass", NULL,
8239                             WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_POPUP | WS_VISIBLE,
8240                             100, 100, 200, 200,
8241                             0, 0, GetModuleHandleA(NULL), NULL);
8242     ok(parent != 0, "CreateWindowEx failed\n");
8243     trace("parent %p\n", parent);
8244 
8245     create_window_tree(parent, window, sizeof(window)/sizeof(window[0]));
8246 
8247     found_invisible = 0;
8248     found_disabled = 0;
8249     found_groupbox = 0;
8250     found_httransparent = 0;
8251     found_extransparent = 0;
8252 
8253     /* FIXME: also test WindowFromPoint, ChildWindowFromPoint, ChildWindowFromPointEx */
8254     for (i = 0; i < sizeof(real_child_pos)/sizeof(real_child_pos[0]); i++)
8255     {
8256         struct window_attributes attrs;
8257 
8258         pt.x = pt.y = 50;
8259         hwnd = RealChildWindowFromPoint(parent, pt);
8260         ok(hwnd != 0, "RealChildWindowFromPoint failed\n");
8261         ret = window_to_index(hwnd, window, sizeof(window)/sizeof(window[0]));
8262         /* FIXME: remove once Wine is fixed */
8263         todo_wine_if (ret != real_child_pos[i])
8264             ok(ret == real_child_pos[i] || broken(ret == real_child_pos_nt4[i]), "expected %d, got %d\n", real_child_pos[i], ret);
8265 
8266         get_window_attributes(hwnd, &attrs);
8267         if (!attrs.is_visible) found_invisible++;
8268         if (!attrs.is_enabled) found_disabled++;
8269         if (attrs.is_groupbox) found_groupbox++;
8270         if (attrs.is_httransparent) found_httransparent++;
8271         if (attrs.is_extransparent) found_extransparent++;
8272 
8273         if (ret != real_child_pos[i] && ret != -1)
8274         {
8275             trace("found hwnd %p (%s), is_visible %d, is_enabled %d, is_groupbox %d, is_httransparent %d, is_extransparent %d\n",
8276                   hwnd, attrs.class_name, attrs.is_visible, attrs.is_enabled, attrs.is_groupbox, attrs.is_httransparent, attrs.is_extransparent);
8277             get_window_attributes(window[real_child_pos[i]], &attrs);
8278             trace("expected hwnd %p (%s), is_visible %d, is_enabled %d, is_groupbox %d, is_httransparent %d, is_extransparent %d\n",
8279                   window[real_child_pos[i]], attrs.class_name, attrs.is_visible, attrs.is_enabled, attrs.is_groupbox, attrs.is_httransparent, attrs.is_extransparent);
8280         }
8281         if (ret == -1)
8282         {
8283             ok(hwnd == parent, "expected %p, got %p\n", parent, hwnd);
8284             break;
8285         }
8286         DestroyWindow(hwnd);
8287     }
8288 
8289     DestroyWindow(parent);
8290 
8291     ok(!found_invisible, "found %d invisible windows\n", found_invisible);
8292     ok(found_disabled, "found %d disabled windows\n", found_disabled);
8293 todo_wine
8294     ok(found_groupbox == 4, "found %d groupbox windows\n", found_groupbox);
8295     ok(found_httransparent, "found %d httransparent windows\n", found_httransparent);
8296 todo_wine
8297     ok(found_extransparent, "found %d extransparent windows\n", found_extransparent);
8298 
8299     ret = UnregisterClassA("my_button", cls.hInstance);
8300     ok(ret, "UnregisterClass(my_button) failed\n");
8301     ret = UnregisterClassA("my_httrasparent", cls.hInstance);
8302     ok(ret, "UnregisterClass(my_httrasparent) failed\n");
8303     ret = UnregisterClassA("my_window", cls.hInstance);
8304     ok(ret, "UnregisterClass(my_window) failed\n");
8305 }
8306 
simulate_click(int x,int y)8307 static void simulate_click(int x, int y)
8308 {
8309     INPUT input[2];
8310     UINT events_no;
8311 
8312     SetCursorPos(x, y);
8313     memset(input, 0, sizeof(input));
8314     input[0].type = INPUT_MOUSE;
8315     U(input[0]).mi.dx = x;
8316     U(input[0]).mi.dy = y;
8317     U(input[0]).mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
8318     input[1].type = INPUT_MOUSE;
8319     U(input[1]).mi.dx = x;
8320     U(input[1]).mi.dy = y;
8321     U(input[1]).mi.dwFlags = MOUSEEVENTF_LEFTUP;
8322     events_no = SendInput(2, input, sizeof(input[0]));
8323     ok(events_no == 2, "SendInput returned %d\n", events_no);
8324 }
8325 
8326 static WNDPROC def_static_proc;
8327 static BOOL got_hittest;
static_hook_proc(HWND hwnd,UINT msg,WPARAM wp,LPARAM lp)8328 static LRESULT WINAPI static_hook_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
8329 {
8330     if(msg == WM_NCHITTEST)
8331         got_hittest = TRUE;
8332     if(msg == WM_LBUTTONDOWN)
8333         ok(0, "unexpected call\n");
8334 
8335     return def_static_proc(hwnd, msg, wp, lp);
8336 }
8337 
window_from_point_proc(HWND parent)8338 static void window_from_point_proc(HWND parent)
8339 {
8340     HANDLE start_event, end_event;
8341     HANDLE win, child_static, child_button;
8342     BOOL got_click;
8343     DWORD ret;
8344     POINT pt;
8345     MSG msg;
8346 
8347     start_event = OpenEventA(EVENT_ALL_ACCESS, FALSE, "test_wfp_start");
8348     ok(start_event != 0, "OpenEvent failed\n");
8349     end_event = OpenEventA(EVENT_ALL_ACCESS, FALSE, "test_wfp_end");
8350     ok(end_event != 0, "OpenEvent failed\n");
8351 
8352     child_static = CreateWindowExA(0, "static", "static", WS_CHILD | WS_VISIBLE,
8353             0, 0, 100, 100, parent, 0, NULL, NULL);
8354     ok(child_static != 0, "CreateWindowEx failed\n");
8355     pt.x = pt.y = 150;
8356     win = WindowFromPoint(pt);
8357     ok(win == parent, "WindowFromPoint returned %p, expected %p\n", win, parent);
8358 
8359     child_button = CreateWindowExA(0, "button", "button", WS_CHILD | WS_VISIBLE,
8360             100, 0, 100, 100, parent, 0, NULL, NULL);
8361     ok(child_button != 0, "CreateWindowEx failed\n");
8362     pt.x = 250;
8363     win = WindowFromPoint(pt);
8364     ok(win == child_button, "WindowFromPoint returned %p, expected %p\n", win, child_button);
8365 
8366     /* without this window simulate click test keeps sending WM_NCHITTEST
8367      * message to child_static in an infinite loop */
8368     win = CreateWindowExA(0, "button", "button", WS_CHILD | WS_VISIBLE,
8369             0, 0, 100, 100, parent, 0, NULL, NULL);
8370     ok(win != 0, "CreateWindowEx failed\n");
8371     def_static_proc = (void*)SetWindowLongPtrA(child_static,
8372             GWLP_WNDPROC, (LONG_PTR)static_hook_proc);
8373     flush_events(TRUE);
8374     SetEvent(start_event);
8375 
8376     got_hittest = FALSE;
8377     got_click = FALSE;
8378     while(!got_click && wait_for_message(&msg)) {
8379         if(msg.message == WM_LBUTTONUP) {
8380             ok(msg.hwnd == win, "msg.hwnd = %p, expected %p\n", msg.hwnd, win);
8381             got_click = TRUE;
8382         }
8383         DispatchMessageA(&msg);
8384     }
8385     ok(got_hittest, "transparent window didn't get WM_NCHITTEST message\n");
8386     ok(got_click, "button under static window didn't get WM_LBUTTONUP\n");
8387 
8388     ret = WaitForSingleObject(end_event, 5000);
8389     ok(ret == WAIT_OBJECT_0, "WaitForSingleObject returned %x\n", ret);
8390 
8391     CloseHandle(start_event);
8392     CloseHandle(end_event);
8393 }
8394 
test_window_from_point(const char * argv0)8395 static void test_window_from_point(const char *argv0)
8396 {
8397     HWND hwnd, child, win;
8398     POINT pt;
8399     PROCESS_INFORMATION info;
8400     STARTUPINFOA startup;
8401     char cmd[MAX_PATH];
8402     HANDLE start_event, end_event;
8403 
8404     hwnd = CreateWindowExA(0, "MainWindowClass", NULL, WS_POPUP | WS_VISIBLE,
8405             100, 100, 200, 100, 0, 0, NULL, NULL);
8406     ok(hwnd != 0, "CreateWindowEx failed\n");
8407 
8408     pt.x = pt.y = 150;
8409     win = WindowFromPoint(pt);
8410     pt.x = 250;
8411     if(win == hwnd)
8412         win = WindowFromPoint(pt);
8413     if(win != hwnd) {
8414         skip("there's another window covering test window\n");
8415         DestroyWindow(hwnd);
8416         return;
8417     }
8418 
8419     child = CreateWindowExA(0, "static", "static", WS_CHILD | WS_VISIBLE,
8420             0, 0, 100, 100, hwnd, 0, NULL, NULL);
8421     ok(child != 0, "CreateWindowEx failed\n");
8422     pt.x = pt.y = 150;
8423     win = WindowFromPoint(pt);
8424     ok(win == hwnd, "WindowFromPoint returned %p, expected %p\n", win, hwnd);
8425     DestroyWindow(child);
8426 
8427     child = CreateWindowExA(0, "button", "button", WS_CHILD | WS_VISIBLE,
8428                 0, 0, 100, 100, hwnd, 0, NULL, NULL);
8429     ok(child != 0, "CreateWindowEx failed\n");
8430     win = WindowFromPoint(pt);
8431     ok(win == child, "WindowFromPoint returned %p, expected %p\n", win, child);
8432     DestroyWindow(child);
8433 
8434     start_event = CreateEventA(NULL, FALSE, FALSE, "test_wfp_start");
8435     ok(start_event != 0, "CreateEvent failed\n");
8436     end_event = CreateEventA(NULL, FALSE, FALSE, "test_wfp_end");
8437     ok(start_event != 0, "CreateEvent failed\n");
8438 
8439     sprintf(cmd, "%s win create_children %p\n", argv0, hwnd);
8440     memset(&startup, 0, sizeof(startup));
8441     startup.cb = sizeof(startup);
8442     ok(CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL,
8443                 &startup, &info), "CreateProcess failed.\n");
8444     ok(wait_for_event(start_event, 1000), "didn't get start_event\n");
8445 
8446     child = GetWindow(hwnd, GW_CHILD);
8447     win = WindowFromPoint(pt);
8448     ok(win == child, "WindowFromPoint returned %p, expected %p\n", win, child);
8449 
8450     simulate_click(150, 150);
8451     flush_events(TRUE);
8452 
8453     child = GetWindow(child, GW_HWNDNEXT);
8454     pt.x = 250;
8455     win = WindowFromPoint(pt);
8456     ok(win == child, "WindowFromPoint returned %p, expected %p\n", win, child);
8457 
8458     SetEvent(end_event);
8459     winetest_wait_child_process(info.hProcess);
8460     CloseHandle(start_event);
8461     CloseHandle(end_event);
8462     CloseHandle(info.hProcess);
8463     CloseHandle(info.hThread);
8464 
8465     DestroyWindow(hwnd);
8466 }
8467 
test_map_points(void)8468 static void test_map_points(void)
8469 {
8470     BOOL ret;
8471     POINT p;
8472     HWND wnd, wnd0, dwnd;
8473     INT n;
8474     DWORD err;
8475     POINT pos = { 100, 200 };
8476     int width = 150;
8477     int height = 150;
8478     RECT window_rect;
8479     RECT client_rect;
8480 
8481     /* Create test windows */
8482     wnd = CreateWindowA("static", "test1", WS_POPUP, pos.x, pos.y, width, height, NULL, NULL, NULL, NULL);
8483     ok(wnd != NULL, "Failed %p\n", wnd);
8484     wnd0 = CreateWindowA("static", "test2", WS_POPUP, 0, 0, width, height, NULL, NULL, NULL, NULL);
8485     ok(wnd0 != NULL, "Failed %p\n", wnd);
8486     dwnd = CreateWindowA("static", "test3", 0, 200, 300, 150, 150, NULL, NULL, NULL, NULL);
8487     DestroyWindow(dwnd);
8488     ok(dwnd != NULL, "Failed %p\n", dwnd);
8489 
8490     /* Verify window rect and client rect (they should have the same width and height) */
8491     GetWindowRect(wnd, &window_rect);
8492     ok(window_rect.left == pos.x, "left is %d instead of %d\n", window_rect.left, pos.x);
8493     ok(window_rect.top == pos.y, "top is %d instead of %d\n", window_rect.top, pos.y);
8494     ok(window_rect.right == pos.x + width, "right is %d instead of %d\n", window_rect.right, pos.x + width);
8495     ok(window_rect.bottom == pos.y + height, "bottom is %d instead of %d\n", window_rect.bottom, pos.y + height);
8496     GetClientRect(wnd, &client_rect);
8497     ok(client_rect.left == 0, "left is %d instead of 0\n", client_rect.left);
8498     ok(client_rect.top == 0, "top is %d instead of 0\n", client_rect.top);
8499     ok(client_rect.right == width, "right is %d instead of %d\n", client_rect.right, width);
8500     ok(client_rect.bottom == height, "bottom is %d instead of %d\n", client_rect.bottom, height);
8501 
8502     /* Test MapWindowPoints */
8503 
8504     /* MapWindowPoints(NULL or wnd, NULL or wnd, NULL, 1); crashes on Windows */
8505 
8506     SetLastError(0xdeadbeef);
8507     n = MapWindowPoints(NULL, NULL, NULL, 0);
8508     err = GetLastError();
8509     ok(n == 0, "Got %d, expected %d\n", n, 0);
8510     ok(err == 0xdeadbeef, "Got %x, expected %x\n", err, 0xdeadbeef);
8511 
8512     SetLastError(0xdeadbeef);
8513     n = MapWindowPoints(wnd, wnd, NULL, 0);
8514     err = GetLastError();
8515     ok(n == 0, "Got %d, expected %d\n", n, 0);
8516     ok(err == 0xdeadbeef, "Got %x, expected %x\n", err, 0xdeadbeef);
8517 
8518     n = MapWindowPoints(wnd, NULL, NULL, 0);
8519     ok(n == MAKELONG(window_rect.left, window_rect.top), "Got %x, expected %x\n",
8520        n, MAKELONG(window_rect.left, window_rect.top));
8521 
8522     n = MapWindowPoints(NULL, wnd, NULL, 0);
8523     ok(n == MAKELONG(-window_rect.left, -window_rect.top), "Got %x, expected %x\n",
8524        n, MAKELONG(-window_rect.left, -window_rect.top));
8525 
8526     SetLastError(0xdeadbeef);
8527     p.x = p.y = 100;
8528     n = MapWindowPoints(dwnd, NULL, &p, 1);
8529     err = GetLastError();
8530     ok(n == 0, "Got %d, expected %d\n", n, 0);
8531     ok(p.x == 100 && p.y == 100, "Failed got(%d, %d), expected (%d, %d)\n", p.x, p.y, 100, 100);
8532     ok(err == ERROR_INVALID_WINDOW_HANDLE, "Got %x, expected %x\n", err, ERROR_INVALID_WINDOW_HANDLE);
8533 
8534     SetLastError(0xdeadbeef);
8535     p.x = p.y = 100;
8536     n = MapWindowPoints(dwnd, wnd, &p, 1);
8537     err = GetLastError();
8538     ok(n == 0, "Got %d, expected %d\n", n, 0);
8539     ok(p.x == 100 && p.y == 100, "Failed got(%d, %d), expected (%d, %d)\n", p.x, p.y, 100, 100);
8540     ok(err == ERROR_INVALID_WINDOW_HANDLE, "Got %x, expected %x\n", err, ERROR_INVALID_WINDOW_HANDLE);
8541 
8542     SetLastError(0xdeadbeef);
8543     p.x = p.y = 100;
8544     n = MapWindowPoints(NULL, dwnd, &p, 1);
8545     err = GetLastError();
8546     ok(n == 0, "Got %d, expected %d\n", n, 0);
8547     ok(p.x == 100 && p.y == 100, "Failed got(%d, %d), expected (%d, %d)\n", p.x, p.y, 100, 100);
8548     ok(err == ERROR_INVALID_WINDOW_HANDLE, "Got %x, expected %x\n", err, ERROR_INVALID_WINDOW_HANDLE);
8549 
8550     SetLastError(0xdeadbeef);
8551     p.x = p.y = 100;
8552     n = MapWindowPoints(wnd, dwnd, &p, 1);
8553     err = GetLastError();
8554     ok(n == 0, "Got %d, expected %d\n", n, 0);
8555     ok(p.x == 100 && p.y == 100, "Failed got(%d, %d), expected (%d, %d)\n", p.x, p.y, 100, 100);
8556     ok(err == ERROR_INVALID_WINDOW_HANDLE, "Got %x, expected %x\n", err, ERROR_INVALID_WINDOW_HANDLE);
8557 
8558     SetLastError(0xdeadbeef);
8559     p.x = p.y = 100;
8560     n = MapWindowPoints(dwnd, dwnd, &p, 1);
8561     err = GetLastError();
8562     ok(n == 0, "Got %d, expected %d\n", n, 0);
8563     ok(p.x == 100 && p.y == 100, "Failed got(%d, %d), expected (%d, %d)\n", p.x, p.y, 100, 100);
8564     ok(err == ERROR_INVALID_WINDOW_HANDLE, "Got %x, expected %x\n", err, ERROR_INVALID_WINDOW_HANDLE);
8565 
8566     SetLastError(0xdeadbeef);
8567     p.x = p.y = 100;
8568     n = MapWindowPoints(NULL, NULL, &p, 1);
8569     err = GetLastError();
8570     ok(n == 0, "Got %d, expected %d\n", n, 0);
8571     ok(p.x == 100 && p.y == 100, "Failed got(%d, %d), expected (%d, %d)\n", p.x, p.y, 100, 100);
8572     ok(err == 0xdeadbeef, "Got %x, expected %x\n", err, 0xdeadbeef);
8573 
8574     SetLastError(0xdeadbeef);
8575     p.x = p.y = 100;
8576     n = MapWindowPoints(wnd, wnd, &p, 1);
8577     err = GetLastError();
8578     ok(n == 0, "Got %d, expected %d\n", n, 0);
8579     ok(p.x == 100 && p.y == 100, "Failed got(%d, %d), expected (%d, %d)\n", p.x, p.y, 100, 100);
8580     ok(err == 0xdeadbeef, "Got %x, expected %x\n", err, 0xdeadbeef);
8581 
8582     p.x = p.y = 100;
8583     n = MapWindowPoints(wnd, NULL, &p, 1);
8584     ok(n == MAKELONG(window_rect.left, window_rect.top), "Got %x, expected %x\n",
8585        n, MAKELONG(window_rect.left, window_rect.top));
8586     ok((p.x == (window_rect.left + 100)) && (p.y == (window_rect.top + 100)), "Failed got (%d, %d), expected (%d, %d)\n",
8587        p.x, p.y, window_rect.left + 100, window_rect.top + 100);
8588 
8589     p.x = p.y = 100;
8590     n = MapWindowPoints(NULL, wnd, &p, 1);
8591     ok(n == MAKELONG(-window_rect.left, -window_rect.top), "Got %x, expected %x\n",
8592        n, MAKELONG(-window_rect.left, -window_rect.top));
8593     ok((p.x == (-window_rect.left + 100)) && (p.y == (-window_rect.top + 100)), "Failed got (%d, %d), expected (%d, %d)\n",
8594        p.x, p.y, -window_rect.left + 100, -window_rect.top + 100);
8595 
8596     SetLastError(0xdeadbeef);
8597     p.x = p.y = 0;
8598     n = MapWindowPoints(wnd0, NULL, &p, 1);
8599     err = GetLastError();
8600     ok(n == 0, "Got %x,  expected 0\n", n);
8601     ok((p.x == 0) && (p.y == 0), "Failed got (%d, %d), expected (0, 0)\n", p.x, p.y);
8602     ok(err == 0xdeadbeef, "Got %x, expected %x\n", err, 0xdeadbeef);
8603 
8604     SetLastError(0xdeadbeef);
8605     p.x = p.y = 0;
8606     n = MapWindowPoints(NULL, wnd0, &p, 1);
8607     err = GetLastError();
8608     ok(n == 0, "Got %x,  expected 0\n", n);
8609     ok((p.x == 0) && (p.y == 0), "Failed got (%d, %d), expected (0, 0)\n", p.x, p.y);
8610     ok(err == 0xdeadbeef, "Got %x, expected %x\n", err, 0xdeadbeef);
8611 
8612     /* Test ClientToScreen */
8613 
8614     /* ClientToScreen(wnd, NULL); crashes on Windows */
8615 
8616     SetLastError(0xdeadbeef);
8617     ret = ClientToScreen(NULL, NULL);
8618     err = GetLastError();
8619     ok(!ret, "Should fail\n");
8620     ok(err == ERROR_INVALID_WINDOW_HANDLE, "Got %x, expected %x\n", err, ERROR_INVALID_WINDOW_HANDLE);
8621 
8622     SetLastError(0xdeadbeef);
8623     p.x = p.y = 100;
8624     ret = ClientToScreen(NULL, &p);
8625     err = GetLastError();
8626     ok(!ret, "Should fail\n");
8627     ok(p.x == 100 && p.y == 100, "Failed got(%d, %d), expected (%d, %d)\n", p.x, p.y, 100, 100);
8628     ok(err == ERROR_INVALID_WINDOW_HANDLE, "Got %x, expected %x\n", err, ERROR_INVALID_WINDOW_HANDLE);
8629 
8630     SetLastError(0xdeadbeef);
8631     p.x = p.y = 100;
8632     ret = ClientToScreen(dwnd, &p);
8633     err = GetLastError();
8634     ok(!ret, "Should fail\n");
8635     ok(p.x == 100 && p.y == 100, "Failed got(%d, %d), expected (%d, %d)\n", p.x, p.y, 100, 100);
8636     ok(err == ERROR_INVALID_WINDOW_HANDLE, "Got %x, expected %x\n", err, ERROR_INVALID_WINDOW_HANDLE);
8637 
8638     p.x = p.y = 100;
8639     ret = ClientToScreen(wnd, &p);
8640     ok(ret, "Failed with error %u\n", GetLastError());
8641     ok((p.x == (window_rect.left + 100)) && (p.y == (window_rect.top + 100)), "Failed got (%d, %d), expected (%d, %d)\n",
8642        p.x, p.y, window_rect.left + 100, window_rect.top + 100);
8643 
8644     p.x = p.y = 0;
8645     ret = ClientToScreen(wnd0, &p);
8646     ok(ret, "Failed with error %u\n", GetLastError());
8647     ok((p.x == 0) && (p.y == 0), "Failed got (%d, %d), expected (0, 0)\n", p.x, p.y);
8648 
8649     /* Test ScreenToClient */
8650 
8651     /* ScreenToClient(wnd, NULL); crashes on Windows */
8652 
8653     SetLastError(0xdeadbeef);
8654     ret = ScreenToClient(NULL, NULL);
8655     err = GetLastError();
8656     ok(!ret, "Should fail\n");
8657     ok(err == ERROR_INVALID_WINDOW_HANDLE, "Got %x, expected %x\n", err, ERROR_INVALID_WINDOW_HANDLE);
8658 
8659     SetLastError(0xdeadbeef);
8660     p.x = p.y = 100;
8661     ret = ScreenToClient(NULL, &p);
8662     err = GetLastError();
8663     ok(!ret, "Should fail\n");
8664     ok(p.x == 100 && p.y == 100, "Failed got(%d, %d), expected (%d, %d)\n", p.x, p.y, 100, 100);
8665     ok(err == ERROR_INVALID_WINDOW_HANDLE, "Got %x, expected %x\n", err, ERROR_INVALID_WINDOW_HANDLE);
8666 
8667     SetLastError(0xdeadbeef);
8668     p.x = p.y = 100;
8669     ret = ScreenToClient(dwnd, &p);
8670     err = GetLastError();
8671     ok(!ret, "Should fail\n");
8672     ok(p.x == 100 && p.y == 100, "Failed got(%d, %d), expected (%d, %d)\n", p.x, p.y, 100, 100);
8673     ok(err == ERROR_INVALID_WINDOW_HANDLE, "Got %x, expected %x\n", err, ERROR_INVALID_WINDOW_HANDLE);
8674 
8675     p.x = p.y = 100;
8676     ret = ScreenToClient(wnd, &p);
8677     ok(ret, "Failed with error %u\n", GetLastError());
8678     ok((p.x == (-window_rect.left + 100)) && (p.y == (-window_rect.top + 100)), "Failed got(%d, %d), expected (%d, %d)\n",
8679        p.x, p.y, -window_rect.left + 100, -window_rect.top + 100);
8680 
8681     p.x = p.y = 0;
8682     ret = ScreenToClient(wnd0, &p);
8683     ok(ret, "Failed with error %u\n", GetLastError());
8684     ok((p.x == 0) && (p.y == 0), "Failed got (%d, %d), expected (0, 0)\n", p.x, p.y);
8685 
8686     DestroyWindow(wnd);
8687     DestroyWindow(wnd0);
8688 }
8689 
test_update_region(void)8690 static void test_update_region(void)
8691 {
8692     HWND hwnd, parent, child;
8693     HRGN  rgn1, rgn2;
8694     const RECT rc = {15, 15, 40, 40};
8695     const POINT wnd_orig = {30, 20};
8696     const POINT child_orig = {10, 5};
8697 
8698     parent = CreateWindowExA(0, "MainWindowClass", NULL,
8699                 WS_VISIBLE | WS_CLIPCHILDREN,
8700                 0, 0, 300, 150, NULL, NULL, GetModuleHandleA(0), 0);
8701     hwnd = CreateWindowExA(0, "MainWindowClass", NULL,
8702                 WS_VISIBLE | WS_CLIPCHILDREN | WS_CHILD,
8703                 0, 0, 200, 100, parent, NULL, GetModuleHandleA(0), 0);
8704     child = CreateWindowExA(0, "MainWindowClass", NULL,
8705                 WS_VISIBLE | WS_CHILD,
8706                 child_orig.x, child_orig.y,  100, 50,
8707                 hwnd, NULL, GetModuleHandleA(0), 0);
8708     assert(parent && hwnd && child);
8709 
8710     ValidateRgn(parent, NULL);
8711     ValidateRgn(hwnd, NULL);
8712     InvalidateRect(hwnd, &rc, FALSE);
8713     ValidateRgn(child, NULL);
8714 
8715     rgn1 = CreateRectRgn(0, 0, 0, 0);
8716     ok(GetUpdateRgn(parent, rgn1, FALSE) == NULLREGION,
8717             "has invalid area after ValidateRgn(NULL)\n");
8718     GetUpdateRgn(hwnd, rgn1, FALSE);
8719     rgn2 = CreateRectRgnIndirect(&rc);
8720     ok(EqualRgn(rgn1, rgn2), "assigned and retrieved update regions are different\n");
8721     ok(GetUpdateRgn(child, rgn2, FALSE) == NULLREGION,
8722             "has invalid area after ValidateRgn(NULL)\n");
8723 
8724     SetWindowPos(hwnd, 0, wnd_orig.x, wnd_orig.y,  0, 0,
8725             SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOSIZE);
8726 
8727     /* parent now has non-simple update region, it consist of
8728      * two rects, that was exposed after hwnd moving ... */
8729     SetRectRgn(rgn1, 0, 0, 200, wnd_orig.y);
8730     SetRectRgn(rgn2, 0, 0, wnd_orig.x, 100);
8731     CombineRgn(rgn1, rgn1, rgn2, RGN_OR);
8732     /* ... and mapped hwnd's invalid area, that hwnd has before moving */
8733     SetRectRgn(rgn2, rc.left + wnd_orig.x, rc.top + wnd_orig.y,
8734             rc.right + wnd_orig.x, rc.bottom + wnd_orig.y);
8735     CombineRgn(rgn1, rgn1, rgn2, RGN_OR);
8736     GetUpdateRgn(parent, rgn2, FALSE);
8737 todo_wine
8738     ok(EqualRgn(rgn1, rgn2), "wrong update region\n");
8739 
8740     /* hwnd has the same invalid region as before moving */
8741     SetRectRgn(rgn1, rc.left, rc.top, rc.right, rc.bottom);
8742     GetUpdateRgn(hwnd, rgn2, FALSE);
8743     ok(EqualRgn(rgn1, rgn2), "wrong update region\n");
8744 
8745     /* hwnd's invalid area maps to child during moving */
8746     SetRectRgn(rgn1, rc.left - child_orig.x , rc.top - child_orig.y,
8747             rc.right - child_orig.x, rc.bottom - child_orig.y);
8748     GetUpdateRgn(child, rgn2, FALSE);
8749 todo_wine
8750     ok(EqualRgn(rgn1, rgn2), "wrong update region\n");
8751 
8752     DeleteObject(rgn1);
8753     DeleteObject(rgn2);
8754     DestroyWindow(parent);
8755 }
8756 
test_window_without_child_style(void)8757 static void test_window_without_child_style(void)
8758 {
8759     HWND hwnd;
8760 
8761     hwnd = CreateWindowExA(0, "edit", NULL, WS_VISIBLE|WS_CHILD,
8762             0, 0, 50, 50, hwndMain, NULL, 0, NULL);
8763     ok(hwnd != NULL, "CreateWindow failed\n");
8764 
8765     ok(SetWindowLongA(hwnd, GWL_STYLE, GetWindowLongA(hwnd, GWL_STYLE) & (~WS_CHILD)),
8766             "can't remove WS_CHILD style\n");
8767 
8768     SetActiveWindow(hwndMain);
8769     PostMessageW(hwnd, WM_LBUTTONUP, 0, 0);
8770     SendMessageW(hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0);
8771     check_active_state(hwnd, hwnd, hwnd);
8772     flush_events(TRUE);
8773 
8774     DestroyWindow(hwnd);
8775 }
8776 
8777 
8778 struct smresult_thread_data
8779 {
8780     HWND main_hwnd;
8781     HWND thread_hwnd;
8782     HANDLE thread_started;
8783     HANDLE thread_got_wm_app;
8784     HANDLE main_in_wm_app_1;
8785     HANDLE thread_replied;
8786 };
8787 
8788 
smresult_wndproc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)8789 static LRESULT WINAPI smresult_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
8790 {
8791     switch (msg)
8792     {
8793     case WM_APP:
8794     {
8795         struct smresult_thread_data *data = (struct smresult_thread_data*)lparam;
8796 
8797         ok(hwnd == data->thread_hwnd, "unexpected hwnd %p\n", hwnd);
8798 
8799         SendNotifyMessageA(data->main_hwnd, WM_APP+1, 0, lparam);
8800 
8801         /* Don't return until the main thread is processing our sent message. */
8802         ok(WaitForSingleObject(data->main_in_wm_app_1, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
8803 
8804         /* Break the PeekMessage loop so we can notify the main thread after we return. */
8805         SetEvent(data->thread_got_wm_app);
8806 
8807         return 0x240408ea;
8808     }
8809     case WM_APP+1:
8810     {
8811         struct smresult_thread_data *data = (struct smresult_thread_data*)lparam;
8812         LRESULT res;
8813 
8814         ok(hwnd == data->main_hwnd, "unexpected hwnd %p\n", hwnd);
8815 
8816         /* Ask the thread to reply to our WM_APP message. */
8817         SetEvent(data->main_in_wm_app_1);
8818 
8819         /* Wait until the thread has sent a reply. */
8820         ok(WaitForSingleObject(data->thread_replied, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
8821 
8822         /* Send another message while we have a reply queued for the current one. */
8823         res = SendMessageA(data->thread_hwnd, WM_APP+2, 0, lparam);
8824         ok(res == 0x449b0190, "unexpected result %lx\n", res);
8825 
8826         return 0;
8827     }
8828     case WM_APP+2:
8829     {
8830         struct smresult_thread_data *data = (struct smresult_thread_data*)lparam;
8831 
8832         ok(hwnd == data->thread_hwnd, "unexpected hwnd %p\n", hwnd);
8833 
8834         /* Don't return until we know the main thread is processing sent messages. */
8835         SendMessageA(data->main_hwnd, WM_NULL, 0, 0);
8836 
8837         return 0x449b0190;
8838     }
8839     case WM_CLOSE:
8840         PostQuitMessage(0);
8841         break;
8842     }
8843     return DefWindowProcA(hwnd, msg, wparam, lparam);
8844 }
8845 
smresult_thread_proc(void * param)8846 static DWORD WINAPI smresult_thread_proc(void *param)
8847 {
8848     MSG msg;
8849     struct smresult_thread_data *data = param;
8850 
8851     data->thread_hwnd = CreateWindowExA(0, "SmresultClass", "window caption text", WS_OVERLAPPEDWINDOW,
8852                                       100, 100, 200, 200, 0, 0, 0, NULL);
8853     ok(data->thread_hwnd != 0, "Failed to create overlapped window\n");
8854 
8855     SetEvent(data->thread_started);
8856 
8857     /* Loop until we've processed WM_APP. */
8858     while (WaitForSingleObject(data->thread_got_wm_app, 0) != WAIT_OBJECT_0)
8859     {
8860         if (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE))
8861         {
8862             TranslateMessage(&msg);
8863             DispatchMessageA(&msg);
8864         }
8865         else
8866         {
8867             MsgWaitForMultipleObjects(1, &data->thread_got_wm_app, FALSE, INFINITE, QS_SENDMESSAGE);
8868         }
8869     }
8870 
8871     /* Notify the main thread that we replied to its WM_APP message. */
8872     SetEvent(data->thread_replied);
8873 
8874     while (GetMessageA(&msg, 0, 0, 0))
8875     {
8876         TranslateMessage(&msg);
8877         DispatchMessageA(&msg);
8878     }
8879 
8880     return 0;
8881 }
8882 
test_smresult(void)8883 static void test_smresult(void)
8884 {
8885     WNDCLASSA cls;
8886     HANDLE hThread;
8887     DWORD tid;
8888     struct smresult_thread_data data;
8889     BOOL ret;
8890     LRESULT res;
8891 
8892     cls.style = CS_DBLCLKS;
8893     cls.lpfnWndProc = smresult_wndproc;
8894     cls.cbClsExtra = 0;
8895     cls.cbWndExtra = 0;
8896     cls.hInstance = GetModuleHandleA(0);
8897     cls.hIcon = 0;
8898     cls.hCursor = LoadCursorA(0, (LPCSTR)IDC_ARROW);
8899     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
8900     cls.lpszMenuName = NULL;
8901     cls.lpszClassName = "SmresultClass";
8902 
8903     ret = RegisterClassA(&cls);
8904     ok(ret, "RegisterClassA failed\n");
8905 
8906     data.thread_started = CreateEventA(NULL, TRUE, FALSE, NULL);
8907     ok(data.thread_started != NULL, "CreateEventA failed\n");
8908 
8909     data.thread_got_wm_app = CreateEventA(NULL, TRUE, FALSE, NULL);
8910     ok(data.thread_got_wm_app != NULL, "CreateEventA failed\n");
8911 
8912     data.main_in_wm_app_1 = CreateEventA(NULL, TRUE, FALSE, NULL);
8913     ok(data.main_in_wm_app_1 != NULL, "CreateEventA failed\n");
8914 
8915     data.thread_replied = CreateEventA(NULL, TRUE, FALSE, NULL);
8916     ok(data.thread_replied != NULL, "CreateEventA failed\n");
8917 
8918     data.main_hwnd = CreateWindowExA(0, "SmresultClass", "window caption text", WS_OVERLAPPEDWINDOW,
8919                                       100, 100, 200, 200, 0, 0, 0, NULL);
8920 
8921     hThread = CreateThread(NULL, 0, smresult_thread_proc, &data, 0, &tid);
8922     ok(hThread != NULL, "CreateThread failed, error %d\n", GetLastError());
8923 
8924     ok(WaitForSingleObject(data.thread_started, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
8925 
8926     res = SendMessageA(data.thread_hwnd, WM_APP, 0, (LPARAM)&data);
8927     ok(res == 0x240408ea, "unexpected result %lx\n", res);
8928 
8929     SendMessageA(data.thread_hwnd, WM_CLOSE, 0, 0);
8930 
8931     DestroyWindow(data.main_hwnd);
8932 
8933     ok(WaitForSingleObject(hThread, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
8934 
8935     CloseHandle(data.thread_started);
8936     CloseHandle(data.thread_got_wm_app);
8937     CloseHandle(data.main_in_wm_app_1);
8938     CloseHandle(data.thread_replied);
8939 }
8940 
test_GetMessagePos(void)8941 static void test_GetMessagePos(void)
8942 {
8943     HWND button;
8944     DWORD pos;
8945     MSG msg;
8946 
8947     button = CreateWindowExA(0, "button", "button", WS_VISIBLE,
8948             100, 100, 100, 100, 0, 0, 0, NULL);
8949     ok(button != 0, "CreateWindowExA failed\n");
8950 
8951     SetCursorPos(120, 140);
8952     flush_events(TRUE);
8953     pos = GetMessagePos();
8954     ok(pos == MAKELONG(120, 140), "pos = %08x\n", pos);
8955 
8956     SetCursorPos(340, 320);
8957     pos = GetMessagePos();
8958     ok(pos == MAKELONG(120, 140), "pos = %08x\n", pos);
8959 
8960     SendMessageW(button, WM_APP, 0, 0);
8961     pos = GetMessagePos();
8962     ok(pos == MAKELONG(120, 140), "pos = %08x\n", pos);
8963 
8964     PostMessageA(button, WM_APP, 0, 0);
8965     GetMessageA(&msg, button, 0, 0);
8966     ok(msg.message == WM_APP, "msg.message = %x\n", msg.message);
8967     pos = GetMessagePos();
8968     ok(pos == MAKELONG(340, 320), "pos = %08x\n", pos);
8969 
8970     PostMessageA(button, WM_APP, 0, 0);
8971     SetCursorPos(350, 330);
8972     GetMessageA(&msg, button, 0, 0);
8973     ok(msg.message == WM_APP, "msg.message = %x\n", msg.message);
8974     pos = GetMessagePos();
8975     ok(pos == MAKELONG(340, 320), "pos = %08x\n", pos);
8976 
8977     PostMessageA(button, WM_APP, 0, 0);
8978     SetCursorPos(320, 340);
8979     PostMessageA(button, WM_APP+1, 0, 0);
8980     pos = GetMessagePos();
8981     ok(pos == MAKELONG(340, 320), "pos = %08x\n", pos);
8982     GetMessageA(&msg, button, 0, 0);
8983     ok(msg.message == WM_APP, "msg.message = %x\n", msg.message);
8984     pos = GetMessagePos();
8985     ok(pos == MAKELONG(350, 330), "pos = %08x\n", pos);
8986     GetMessageA(&msg, button, 0, 0);
8987     ok(msg.message == WM_APP+1, "msg.message = %x\n", msg.message);
8988     pos = GetMessagePos();
8989     ok(pos == MAKELONG(320, 340), "pos = %08x\n", pos);
8990 
8991     SetTimer(button, 1, 250, NULL);
8992     SetCursorPos(330, 350);
8993     GetMessageA(&msg, button, 0, 0);
8994     while (msg.message == WM_PAINT)
8995     {
8996         UpdateWindow( button );
8997         GetMessageA(&msg, button, 0, 0);
8998     }
8999     ok(msg.message == WM_TIMER, "msg.message = %x\n", msg.message);
9000     pos = GetMessagePos();
9001     ok(pos == MAKELONG(330, 350), "pos = %08x\n", pos);
9002     KillTimer(button, 1);
9003 
9004     DestroyWindow(button);
9005 }
9006 
9007 #define SET_FOREGROUND_STEAL_1          0x01
9008 #define SET_FOREGROUND_SET_1            0x02
9009 #define SET_FOREGROUND_STEAL_2          0x04
9010 #define SET_FOREGROUND_SET_2            0x08
9011 #define SET_FOREGROUND_INJECT           0x10
9012 
9013 struct set_foreground_thread_params
9014 {
9015     UINT msg_quit, msg_command;
9016     HWND window1, window2, thread_window;
9017     HANDLE command_executed;
9018 };
9019 
set_foreground_thread(void * params)9020 static DWORD WINAPI set_foreground_thread(void *params)
9021 {
9022     struct set_foreground_thread_params *p = params;
9023     MSG msg;
9024 
9025     p->thread_window = CreateWindowExA(0, "static", "thread window", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
9026             0, 0, 10, 10, 0, 0, 0, NULL);
9027     SetEvent(p->command_executed);
9028 
9029     while(GetMessageA(&msg, 0, 0, 0))
9030     {
9031         if (msg.message == p->msg_quit)
9032             break;
9033 
9034         if (msg.message == p->msg_command)
9035         {
9036             if (msg.wParam & SET_FOREGROUND_STEAL_1)
9037             {
9038                 SetForegroundWindow(p->thread_window);
9039                 check_wnd_state(p->thread_window, p->thread_window, p->thread_window, 0);
9040             }
9041             if (msg.wParam & SET_FOREGROUND_INJECT)
9042             {
9043                 SendNotifyMessageA(p->window1, WM_ACTIVATEAPP, 0, 0);
9044             }
9045             if (msg.wParam & SET_FOREGROUND_SET_1)
9046             {
9047                 SetForegroundWindow(p->window1);
9048                 check_wnd_state(0, p->window1, 0, 0);
9049             }
9050             if (msg.wParam & SET_FOREGROUND_STEAL_2)
9051             {
9052                 SetForegroundWindow(p->thread_window);
9053                 check_wnd_state(p->thread_window, p->thread_window, p->thread_window, 0);
9054             }
9055             if (msg.wParam & SET_FOREGROUND_SET_2)
9056             {
9057                 SetForegroundWindow(p->window2);
9058                 check_wnd_state(0, p->window2, 0, 0);
9059             }
9060 
9061             SetEvent(p->command_executed);
9062             continue;
9063         }
9064 
9065         TranslateMessage(&msg);
9066         DispatchMessageA(&msg);
9067     }
9068 
9069     DestroyWindow(p->thread_window);
9070     return 0;
9071 }
9072 
test_activateapp(HWND window1)9073 static void test_activateapp(HWND window1)
9074 {
9075     HWND window2, test_window;
9076     HANDLE thread;
9077     struct set_foreground_thread_params thread_params;
9078     DWORD tid;
9079     MSG msg;
9080 
9081     window2 = CreateWindowExA(0, "static", "window 2", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
9082             300, 0, 10, 10, 0, 0, 0, NULL);
9083     thread_params.msg_quit = WM_USER;
9084     thread_params.msg_command = WM_USER + 1;
9085     thread_params.window1 = window1;
9086     thread_params.window2 = window2;
9087     thread_params.command_executed = CreateEventW(NULL, FALSE, FALSE, NULL);
9088 
9089     thread = CreateThread(NULL, 0, set_foreground_thread, &thread_params, 0, &tid);
9090     WaitForSingleObject(thread_params.command_executed, INFINITE);
9091 
9092     SetForegroundWindow(window1);
9093     check_wnd_state(window1, window1, window1, 0);
9094     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
9095 
9096     /* Steal foreground: WM_ACTIVATEAPP(0) is delivered. */
9097     app_activated = app_deactivated = FALSE;
9098     PostThreadMessageA(tid, thread_params.msg_command, SET_FOREGROUND_STEAL_1, 0);
9099     WaitForSingleObject(thread_params.command_executed, INFINITE);
9100     test_window = GetForegroundWindow();
9101     ok(test_window == thread_params.thread_window, "Expected foreground window %p, got %p\n",
9102             thread_params.thread_window, test_window);
9103     /* Active and Focus window are sometimes 0 on KDE. Ignore them.
9104      * check_wnd_state(window1, thread_params.thread_window, window1, 0); */
9105     ok(!app_activated, "Received WM_ACTIVATEAPP(1), did not expect it.\n");
9106     ok(!app_deactivated, "Received WM_ACTIVATEAPP(0), did not expect it.\n");
9107     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
9108     check_wnd_state(0, thread_params.thread_window, 0, 0);
9109     test_window = GetForegroundWindow();
9110     ok(test_window == thread_params.thread_window, "Expected foreground window %p, got %p\n",
9111             thread_params.thread_window, test_window);
9112     ok(!app_activated, "Received WM_ACTIVATEAPP(1), did not expect it.\n");
9113     /* This message is reliable on Windows and inside a virtual desktop.
9114      * It is unreliable on KDE (50/50) and never arrives on FVWM.
9115      * ok(app_deactivated, "Expected WM_ACTIVATEAPP(0), did not receive it.\n"); */
9116 
9117     /* Set foreground: WM_ACTIVATEAPP (1) is delivered. */
9118     app_activated = app_deactivated = FALSE;
9119     PostThreadMessageA(tid, thread_params.msg_command, SET_FOREGROUND_SET_1, 0);
9120     WaitForSingleObject(thread_params.command_executed, INFINITE);
9121     check_wnd_state(0, 0, 0, 0);
9122     test_window = GetForegroundWindow();
9123     ok(!test_window, "Expected foreground window 0, got %p\n", test_window);
9124     ok(!app_activated, "Received WM_ACTIVATEAPP(!= 0), did not expect it.\n");
9125     ok(!app_deactivated, "Received WM_ACTIVATEAPP(0), did not expect it.\n");
9126     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
9127     check_wnd_state(window1, window1, window1, 0);
9128     test_window = GetForegroundWindow();
9129     ok(test_window == window1, "Expected foreground window %p, got %p\n",
9130             window1, test_window);
9131     ok(app_activated, "Expected WM_ACTIVATEAPP(1), did not receive it.\n");
9132     ok(!app_deactivated, "Received WM_ACTIVATEAPP(0), did not expect it.\n");
9133 
9134     /* Steal foreground then set it back: No messages are delivered. */
9135     app_activated = app_deactivated = FALSE;
9136     PostThreadMessageA(tid, thread_params.msg_command, SET_FOREGROUND_STEAL_1 | SET_FOREGROUND_SET_1, 0);
9137     WaitForSingleObject(thread_params.command_executed, INFINITE);
9138     test_window = GetForegroundWindow();
9139     ok(test_window == window1, "Expected foreground window %p, got %p\n",
9140             window1, test_window);
9141     check_wnd_state(window1, window1, window1, 0);
9142     ok(!app_activated, "Received WM_ACTIVATEAPP(1), did not expect it.\n");
9143     ok(!app_deactivated, "Received WM_ACTIVATEAPP(0), did not expect it.\n");
9144     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
9145     test_window = GetForegroundWindow();
9146     ok(test_window == window1, "Expected foreground window %p, got %p\n",
9147             window1, test_window);
9148     check_wnd_state(window1, window1, window1, 0);
9149     ok(!app_activated, "Received WM_ACTIVATEAPP(1), did not expect it.\n");
9150     ok(!app_deactivated, "Received WM_ACTIVATEAPP(0), did not expect it.\n");
9151 
9152     /* This is not implemented with a plain WM_ACTIVATEAPP filter. */
9153     app_activated = app_deactivated = FALSE;
9154     PostThreadMessageA(tid, thread_params.msg_command, SET_FOREGROUND_STEAL_1
9155             | SET_FOREGROUND_INJECT | SET_FOREGROUND_SET_1, 0);
9156     WaitForSingleObject(thread_params.command_executed, INFINITE);
9157     test_window = GetForegroundWindow();
9158     ok(test_window == window1, "Expected foreground window %p, got %p\n",
9159             window1, test_window);
9160     check_wnd_state(window1, window1, window1, 0);
9161     ok(!app_activated, "Received WM_ACTIVATEAPP(1), did not expect it.\n");
9162     ok(!app_deactivated, "Received WM_ACTIVATEAPP(0), did not expect it.\n");
9163     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
9164     test_window = GetForegroundWindow();
9165     ok(test_window == window1, "Expected foreground window %p, got %p\n",
9166             window1, test_window);
9167     check_wnd_state(window1, window1, window1, 0);
9168     ok(!app_activated, "Received WM_ACTIVATEAPP(1), did not expect it.\n");
9169     ok(app_deactivated, "Expected WM_ACTIVATEAPP(0), did not receive it.\n");
9170 
9171     SetForegroundWindow(thread_params.thread_window);
9172 
9173     /* Set foreground then remove: Both messages are delivered. */
9174     app_activated = app_deactivated = FALSE;
9175     PostThreadMessageA(tid, thread_params.msg_command, SET_FOREGROUND_SET_1 | SET_FOREGROUND_STEAL_2, 0);
9176     WaitForSingleObject(thread_params.command_executed, INFINITE);
9177     test_window = GetForegroundWindow();
9178     ok(test_window == thread_params.thread_window, "Expected foreground window %p, got %p\n",
9179             thread_params.thread_window, test_window);
9180     check_wnd_state(0, thread_params.thread_window, 0, 0);
9181     ok(!app_activated, "Received WM_ACTIVATEAPP(1), did not expect it.\n");
9182     ok(!app_deactivated, "Received WM_ACTIVATEAPP(0), did not expect it.\n");
9183     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
9184     test_window = GetForegroundWindow();
9185     ok(test_window == thread_params.thread_window, "Expected foreground window %p, got %p\n",
9186             thread_params.thread_window, test_window);
9187     /* Active and focus are window1 on wine because the internal WM_WINE_SETACTIVEWINDOW(0)
9188      * message is never generated. GetCapture() returns 0 though, so we'd get a test success
9189      * in todo_wine in the line below.
9190      * todo_wine check_wnd_state(0, thread_params.thread_window, 0, 0); */
9191     ok(app_activated, "Expected WM_ACTIVATEAPP(1), did not receive it.\n");
9192     todo_wine ok(app_deactivated, "Expected WM_ACTIVATEAPP(0), did not receive it.\n");
9193 
9194     SetForegroundWindow(window1);
9195     test_window = GetForegroundWindow();
9196     ok(test_window == window1, "Expected foreground window %p, got %p\n",
9197             window1, test_window);
9198     check_wnd_state(window1, window1, window1, 0);
9199 
9200     /* Switch to a different window from the same thread? No messages. */
9201     app_activated = app_deactivated = FALSE;
9202     PostThreadMessageA(tid, thread_params.msg_command, SET_FOREGROUND_STEAL_1 | SET_FOREGROUND_SET_2, 0);
9203     WaitForSingleObject(thread_params.command_executed, INFINITE);
9204     test_window = GetForegroundWindow();
9205     ok(test_window == window1, "Expected foreground window %p, got %p\n",
9206             window1, test_window);
9207     check_wnd_state(window1, window1, window1, 0);
9208     ok(!app_activated, "Received WM_ACTIVATEAPP(1), did not expect it.\n");
9209     ok(!app_deactivated, "Received WM_ACTIVATEAPP(0), did not expect it.\n");
9210     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
9211     test_window = GetForegroundWindow();
9212     ok(test_window == window2, "Expected foreground window %p, got %p\n",
9213             window2, test_window);
9214     check_wnd_state(window2, window2, window2, 0);
9215     ok(!app_activated, "Received WM_ACTIVATEAPP(1), did not expect it.\n");
9216     ok(!app_deactivated, "Received WM_ACTIVATEAPP(0), did not expect it.\n");
9217 
9218     PostThreadMessageA(tid, thread_params.msg_quit, 0, 0);
9219     WaitForSingleObject(thread, INFINITE);
9220 
9221     CloseHandle(thread_params.command_executed);
9222     DestroyWindow(window2);
9223 }
9224 
winproc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)9225 static LRESULT WINAPI winproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
9226 {
9227     if(!hwnd) {
9228         int *count = (int*)lparam;
9229         (*count)++;
9230     }
9231     return 0;
9232 }
9233 
winproc_convA(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)9234 static LRESULT WINAPI winproc_convA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
9235 {
9236     if(msg == WM_SETTEXT)
9237     {
9238         const char *text = (const char*)lparam;
9239 
9240         ok(!wparam, "wparam = %08lx\n", wparam);
9241         ok(!strcmp(text, "text"), "WM_SETTEXT lparam = %s\n", text);
9242         return 1;
9243     }
9244     return 0;
9245 }
9246 
9247 static const WCHAR textW[] = {'t','e','x','t',0};
winproc_convW(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)9248 static LRESULT WINAPI winproc_convW(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
9249 {
9250     if(msg == WM_SETTEXT)
9251     {
9252         const WCHAR *text = (const WCHAR*)lparam;
9253 
9254         ok(!wparam, "wparam = %08lx\n", wparam);
9255         ok(!lstrcmpW(text, textW), "WM_SETTEXT lparam = %s\n", wine_dbgstr_w(text));
9256         return 1;
9257     }
9258     return 0;
9259 }
9260 
test_winproc_handles(const char * argv0)9261 static void test_winproc_handles(const char *argv0)
9262 {
9263     static const WCHAR winproc_testW[] = {'w','i','n','p','r','o','c','_','t','e','s','t',0};
9264 
9265     HINSTANCE hinst = GetModuleHandleA(NULL);
9266     WNDCLASSA wnd_classA;
9267     WNDCLASSW wnd_classW;
9268     int count, ret;
9269     PROCESS_INFORMATION info;
9270     STARTUPINFOA startup;
9271     char cmd[MAX_PATH];
9272 
9273     memset(&wnd_classA, 0, sizeof(wnd_classA));
9274     wnd_classA.lpszClassName = "winproc_test";
9275     wnd_classA.lpfnWndProc = winproc;
9276     ret = RegisterClassA(&wnd_classA);
9277     ok(ret, "RegisterClass failed with error %d\n", GetLastError());
9278 
9279     ret = GetClassInfoW(hinst, winproc_testW, &wnd_classW);
9280     ok(ret, "GetClassInfoW failed with error %d\n", GetLastError());
9281     ok(wnd_classA.lpfnWndProc != wnd_classW.lpfnWndProc,
9282             "winproc pointers should not be identical\n");
9283 
9284     count = 0;
9285     CallWindowProcA(wnd_classW.lpfnWndProc, 0, 0, 0, (LPARAM)&count);
9286     ok(count == 1, "winproc should be called once (%d)\n", count);
9287     count = 0;
9288     CallWindowProcW(wnd_classW.lpfnWndProc, 0, 0, 0, (LPARAM)&count);
9289     ok(count == 1, "winproc should be called once (%d)\n", count);
9290 
9291     ret = UnregisterClassW(winproc_testW, hinst);
9292     ok(ret, "UnregisterClass failed with error %d\n", GetLastError());
9293 
9294     /* crashes on 64-bit windows because lpfnWndProc handle is already freed */
9295     if (sizeof(void*) == 4)
9296     {
9297         count = 0;
9298         CallWindowProcA(wnd_classW.lpfnWndProc, 0, 0, 0, (LPARAM)&count);
9299         todo_wine ok(!count, "winproc should not be called (%d)\n", count);
9300         CallWindowProcW(wnd_classW.lpfnWndProc, 0, 0, 0, (LPARAM)&count);
9301         todo_wine ok(!count, "winproc should not be called (%d)\n", count);
9302     }
9303 
9304     sprintf(cmd, "%s win winproc_limit", argv0);
9305     memset(&startup, 0, sizeof(startup));
9306     startup.cb = sizeof(startup);
9307     ok(CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL,
9308                 &startup, &info), "CreateProcess failed.\n");
9309     winetest_wait_child_process(info.hProcess);
9310     CloseHandle(info.hProcess);
9311     CloseHandle(info.hThread);
9312 }
9313 
test_winproc_limit(void)9314 static void test_winproc_limit(void)
9315 {
9316     WNDPROC winproc_handle;
9317     LONG_PTR ret;
9318     HWND hwnd;
9319     int i;
9320 
9321     hwnd = CreateWindowExA(0, "static", "test", WS_POPUP, 0, 0, 0, 0, 0, 0, 0, 0);
9322     ok(hwnd != 0, "CreateWindowEx failed\n");
9323 
9324     ok(SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (LONG_PTR)winproc),
9325             "SetWindowLongPtr failed\n");
9326     winproc_handle = (WNDPROC)GetWindowLongPtrW(hwnd, GWLP_WNDPROC);
9327     ok(winproc_handle != winproc, "winproc pointers should not be identical\n");
9328 
9329     /* run out of winproc slots */
9330     for(i = 2; i<0xffff; i++)
9331     {
9332         ok(SetWindowLongPtrA(hwnd, GWLP_WNDPROC, i), "SetWindowLongPtr failed (%d)\n", i);
9333         if(GetWindowLongPtrW(hwnd, GWLP_WNDPROC) == i)
9334             break;
9335     }
9336     ok(i != 0xffff, "unable to run out of winproc slots\n");
9337 
9338     ret = SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (LONG_PTR)winproc_convA);
9339     ok(ret, "SetWindowLongPtr failed with error %d\n", GetLastError());
9340     ok(SendMessageA(hwnd, WM_SETTEXT, 0, (LPARAM)"text"), "WM_SETTEXT failed\n");
9341     ok(SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)textW), "WM_SETTEXT with conversion failed\n");
9342 
9343     ret = SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (LONG_PTR)winproc_convW);
9344     ok(ret, "SetWindowLongPtr failed with error %d\n", GetLastError());
9345     ok(SendMessageA(hwnd, WM_SETTEXT, 0, (LPARAM)"text"), "WM_SETTEXT failed\n");
9346     ok(SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)textW), "WM_SETTEXT with conversion failed\n");
9347 
9348     /* Show that there's no message conversion when CallWindowProc is used */
9349     ok(CallWindowProcA(winproc_convW, hwnd, WM_SETTEXT, 0, (LPARAM)textW) == 1,
9350             "winproc_convW returned error\n");
9351     ok(CallWindowProcW(winproc_convW, hwnd, WM_SETTEXT, 0, (LPARAM)textW) == 1,
9352             "winproc_convW returned error\n");
9353 
9354     i = 0;
9355     CallWindowProcA(winproc_handle, 0, 0, 0, (LPARAM)&i);
9356     ok(i == 1, "winproc should be called once (%d)\n", i);
9357     i = 0;
9358     CallWindowProcW(winproc_handle, 0, 0, 0, (LPARAM)&i);
9359     ok(i == 1, "winproc should be called once (%d)\n", i);
9360 
9361     DestroyWindow(hwnd);
9362 
9363     i = 0;
9364     CallWindowProcA(winproc_handle, 0, 0, 0, (LPARAM)&i);
9365     ok(i == 1, "winproc should be called once (%d)\n", i);
9366     i = 0;
9367     CallWindowProcW(winproc_handle, 0, 0, 0, (LPARAM)&i);
9368     ok(i == 1, "winproc should be called once (%d)\n", i);
9369 }
9370 
test_deferwindowpos(void)9371 static void test_deferwindowpos(void)
9372 {
9373     HDWP hdwp, hdwp2;
9374     BOOL ret;
9375 
9376     hdwp = BeginDeferWindowPos(0);
9377     ok(hdwp != NULL, "got %p\n", hdwp);
9378 
9379     ret = EndDeferWindowPos(NULL);
9380     ok(!ret, "got %d\n", ret);
9381 
9382     hdwp2 = DeferWindowPos(NULL, NULL, NULL, 0, 0, 10, 10, 0);
9383 todo_wine
9384     ok(hdwp2 == NULL && ((GetLastError() == ERROR_INVALID_DWP_HANDLE) ||
9385         broken(GetLastError() == ERROR_INVALID_WINDOW_HANDLE) /* before win8 */), "got %p, error %d\n", hdwp2, GetLastError());
9386 
9387     hdwp2 = DeferWindowPos((HDWP)0xdead, GetDesktopWindow(), NULL, 0, 0, 10, 10, 0);
9388 todo_wine
9389     ok(hdwp2 == NULL && ((GetLastError() == ERROR_INVALID_DWP_HANDLE) ||
9390         broken(GetLastError() == ERROR_INVALID_WINDOW_HANDLE) /* before win8 */), "got %p, error %d\n", hdwp2, GetLastError());
9391 
9392     hdwp2 = DeferWindowPos(hdwp, NULL, NULL, 0, 0, 10, 10, 0);
9393     ok(hdwp2 == NULL && GetLastError() == ERROR_INVALID_WINDOW_HANDLE, "got %p, error %d\n", hdwp2, GetLastError());
9394 
9395     hdwp2 = DeferWindowPos(hdwp, GetDesktopWindow(), NULL, 0, 0, 10, 10, 0);
9396     ok(hdwp2 == NULL && GetLastError() == ERROR_INVALID_WINDOW_HANDLE, "got %p, error %d\n", hdwp2, GetLastError());
9397 
9398     hdwp2 = DeferWindowPos(hdwp, (HWND)0xdead, NULL, 0, 0, 10, 10, 0);
9399     ok(hdwp2 == NULL && GetLastError() == ERROR_INVALID_WINDOW_HANDLE, "got %p, error %d\n", hdwp2, GetLastError());
9400 
9401     ret = EndDeferWindowPos(hdwp);
9402     ok(ret, "got %d\n", ret);
9403 }
9404 
START_TEST(win)9405 START_TEST(win)
9406 {
9407     char **argv;
9408     int argc = winetest_get_mainargs( &argv );
9409     HMODULE user32 = GetModuleHandleA( "user32.dll" );
9410     HMODULE gdi32 = GetModuleHandleA("gdi32.dll");
9411     pGetAncestor = (void *)GetProcAddress( user32, "GetAncestor" );
9412     pGetWindowInfo = (void *)GetProcAddress( user32, "GetWindowInfo" );
9413     pGetWindowModuleFileNameA = (void *)GetProcAddress( user32, "GetWindowModuleFileNameA" );
9414     pGetLayeredWindowAttributes = (void *)GetProcAddress( user32, "GetLayeredWindowAttributes" );
9415     pSetLayeredWindowAttributes = (void *)GetProcAddress( user32, "SetLayeredWindowAttributes" );
9416     pUpdateLayeredWindow = (void *)GetProcAddress( user32, "UpdateLayeredWindow" );
9417     pUpdateLayeredWindowIndirect = (void *)GetProcAddress( user32, "UpdateLayeredWindowIndirect" );
9418     pGetMonitorInfoA = (void *)GetProcAddress( user32,  "GetMonitorInfoA" );
9419     pMonitorFromPoint = (void *)GetProcAddress( user32,  "MonitorFromPoint" );
9420     pGetWindowRgnBox = (void *)GetProcAddress( user32, "GetWindowRgnBox" );
9421     pGetGUIThreadInfo = (void *)GetProcAddress( user32, "GetGUIThreadInfo" );
9422     pGetProcessDefaultLayout = (void *)GetProcAddress( user32, "GetProcessDefaultLayout" );
9423     pSetProcessDefaultLayout = (void *)GetProcAddress( user32, "SetProcessDefaultLayout" );
9424     pFlashWindow = (void *)GetProcAddress( user32, "FlashWindow" );
9425     pFlashWindowEx = (void *)GetProcAddress( user32, "FlashWindowEx" );
9426     pGetLayout = (void *)GetProcAddress( gdi32, "GetLayout" );
9427     pSetLayout = (void *)GetProcAddress( gdi32, "SetLayout" );
9428     pMirrorRgn = (void *)GetProcAddress( gdi32, "MirrorRgn" );
9429 
9430     if (argc==4 && !strcmp(argv[2], "create_children"))
9431     {
9432         HWND hwnd;
9433 
9434         sscanf(argv[3], "%p", &hwnd);
9435         window_from_point_proc(hwnd);
9436         return;
9437     }
9438 
9439     if (argc==3 && !strcmp(argv[2], "winproc_limit"))
9440     {
9441         test_winproc_limit();
9442         return;
9443     }
9444 
9445     if (!RegisterWindowClasses()) assert(0);
9446 
9447     hwndMain = CreateWindowExA(/*WS_EX_TOOLWINDOW*/ 0, "MainWindowClass", "Main window",
9448                                WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
9449                                WS_MAXIMIZEBOX | WS_POPUP | WS_VISIBLE,
9450                                100, 100, 200, 200,
9451                                0, 0, GetModuleHandleA(NULL), NULL);
9452     assert( hwndMain );
9453 
9454     if(!SetForegroundWindow(hwndMain)) {
9455         /* workaround for foreground lock timeout */
9456         simulate_click(101, 101);
9457         ok(SetForegroundWindow(hwndMain), "SetForegroundWindow failed\n");
9458     }
9459 
9460     SetLastError(0xdeafbeef);
9461     GetWindowLongPtrW(GetDesktopWindow(), GWLP_WNDPROC);
9462     is_win9x = (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED);
9463 
9464     hhook = SetWindowsHookExA(WH_CBT, cbt_hook_proc, 0, GetCurrentThreadId());
9465     if (!hhook) win_skip( "Cannot set CBT hook, skipping some tests\n" );
9466 
9467     /* make sure that these tests are executed first */
9468     test_FindWindowEx();
9469     test_SetParent();
9470 
9471     hwndMain2 = CreateWindowExA(/*WS_EX_TOOLWINDOW*/ 0, "MainWindowClass", "Main window 2",
9472                                 WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
9473                                 WS_MAXIMIZEBOX | WS_POPUP,
9474                                 100, 100, 200, 200,
9475                                 0, 0, GetModuleHandleA(NULL), NULL);
9476     assert( hwndMain2 );
9477 
9478     our_pid = GetWindowThreadProcessId(hwndMain, NULL);
9479 
9480     /* Add the tests below this line */
9481     test_child_window_from_point();
9482     test_window_from_point(argv[0]);
9483     test_thick_child_size(hwndMain);
9484     test_fullscreen();
9485     test_hwnd_message();
9486     test_nonclient_area(hwndMain);
9487     test_params();
9488     test_GetWindowModuleFileName();
9489     test_capture_1();
9490     test_capture_2();
9491     test_capture_3(hwndMain, hwndMain2);
9492     test_capture_4();
9493     test_rtl_layout();
9494     test_FlashWindow();
9495     test_FlashWindowEx();
9496 
9497     test_CreateWindow();
9498     test_parent_owner();
9499     test_enum_thread_windows();
9500 
9501     test_mdi();
9502     test_icons();
9503     test_SetWindowPos(hwndMain, hwndMain2);
9504     test_SetMenu(hwndMain);
9505     test_SetFocus(hwndMain);
9506     test_SetActiveWindow(hwndMain);
9507     test_NCRedraw();
9508 
9509     test_children_zorder(hwndMain);
9510     test_popup_zorder(hwndMain2, hwndMain, WS_POPUP);
9511     test_popup_zorder(hwndMain2, hwndMain, 0);
9512     test_GetLastActivePopup();
9513     test_keyboard_input(hwndMain);
9514     test_mouse_input(hwndMain);
9515     test_validatergn(hwndMain);
9516     test_nccalcscroll( hwndMain);
9517     test_scrollwindow( hwndMain);
9518     test_scrollvalidate( hwndMain);
9519     test_scrolldc( hwndMain);
9520     test_scroll();
9521     test_IsWindowUnicode();
9522     test_vis_rgn(hwndMain);
9523 
9524     test_AdjustWindowRect();
9525     test_window_styles();
9526     test_dialog_styles();
9527     test_dialog_parent();
9528     test_redrawnow();
9529     test_csparentdc();
9530     test_SetWindowLong();
9531     test_set_window_style();
9532     test_ShowWindow();
9533     test_gettext();
9534     test_GetUpdateRect();
9535     test_Expose();
9536     test_layered_window();
9537 
9538     test_SetForegroundWindow(hwndMain);
9539     test_shell_window();
9540     test_handles( hwndMain );
9541     test_winregion();
9542     test_map_points();
9543     test_update_region();
9544     test_window_without_child_style();
9545     test_smresult();
9546     test_GetMessagePos();
9547 
9548     test_activateapp(hwndMain);
9549     test_winproc_handles(argv[0]);
9550     test_deferwindowpos();
9551 
9552     /* add the tests above this line */
9553     if (hhook) UnhookWindowsHookEx(hhook);
9554 
9555     DestroyWindow(hwndMain2);
9556     DestroyWindow(hwndMain);
9557 }
9558