[user32_winetest]
[reactos.git] / rostests / winetests / user32 / win.c
1 /*
2 * Unit tests for window handling
3 *
4 * Copyright 2002 Bill Medland
5 * Copyright 2002 Alexandre Julliard
6 * Copyright 2003 Dmitry Timoshkov
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22
23 /* To get ICON_SMALL2 with the MSVC headers */
24 #define _WIN32_WINNT 0x0501
25
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35
36 #include "wine/test.h"
37
38 #ifndef SPI_GETDESKWALLPAPER
39 #define SPI_GETDESKWALLPAPER 0x0073
40 #endif
41
42 #define LONG_PTR INT_PTR
43 #define ULONG_PTR UINT_PTR
44
45 void dump_region(HRGN hrgn);
46
47 static HWND (WINAPI *pGetAncestor)(HWND,UINT);
48 static BOOL (WINAPI *pGetWindowInfo)(HWND,WINDOWINFO*);
49 static UINT (WINAPI *pGetWindowModuleFileNameA)(HWND,LPSTR,UINT);
50 static BOOL (WINAPI *pGetLayeredWindowAttributes)(HWND,COLORREF*,BYTE*,DWORD*);
51 static BOOL (WINAPI *pSetLayeredWindowAttributes)(HWND,COLORREF,BYTE,DWORD);
52 static BOOL (WINAPI *pGetMonitorInfoA)(HMONITOR,LPMONITORINFO);
53 static HMONITOR (WINAPI *pMonitorFromPoint)(POINT,DWORD);
54 static int (WINAPI *pGetWindowRgnBox)(HWND,LPRECT);
55 static BOOL (WINAPI *pGetGUIThreadInfo)(DWORD, GUITHREADINFO*);
56
57 static BOOL test_lbuttondown_flag;
58 static HWND hwndMessage;
59 static HWND hwndMain, hwndMain2;
60 static HHOOK hhook;
61
62 static const char* szAWRClass = "Winsize";
63 static HMENU hmenu;
64 static DWORD our_pid;
65
66 static BOOL is_win9x = FALSE;
67
68 #define COUNTOF(arr) (sizeof(arr)/sizeof(arr[0]))
69
70 static void dump_minmax_info( const MINMAXINFO *minmax )
71 {
72 trace("Reserved=%d,%d MaxSize=%d,%d MaxPos=%d,%d MinTrack=%d,%d MaxTrack=%d,%d\n",
73 minmax->ptReserved.x, minmax->ptReserved.y,
74 minmax->ptMaxSize.x, minmax->ptMaxSize.y,
75 minmax->ptMaxPosition.x, minmax->ptMaxPosition.y,
76 minmax->ptMinTrackSize.x, minmax->ptMinTrackSize.y,
77 minmax->ptMaxTrackSize.x, minmax->ptMaxTrackSize.y);
78 }
79
80 /* try to make sure pending X events have been processed before continuing */
81 static void flush_events( BOOL remove_messages )
82 {
83 MSG msg;
84 int diff = 200;
85 int min_timeout = 100;
86 DWORD time = GetTickCount() + diff;
87
88 while (diff > 0)
89 {
90 if (MsgWaitForMultipleObjects( 0, NULL, FALSE, min_timeout, QS_ALLINPUT ) == WAIT_TIMEOUT) break;
91 if (remove_messages)
92 while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
93 diff = time - GetTickCount();
94 min_timeout = 50;
95 }
96 }
97
98 /* check the values returned by the various parent/owner functions on a given window */
99 static void check_parents( HWND hwnd, HWND ga_parent, HWND gwl_parent, HWND get_parent,
100 HWND gw_owner, HWND ga_root, HWND ga_root_owner )
101 {
102 HWND res;
103
104 if (pGetAncestor)
105 {
106 res = pGetAncestor( hwnd, GA_PARENT );
107 ok( res == ga_parent, "Wrong result for GA_PARENT %p expected %p\n", res, ga_parent );
108 }
109 res = (HWND)GetWindowLongPtrA( hwnd, GWLP_HWNDPARENT );
110 ok( res == gwl_parent, "Wrong result for GWL_HWNDPARENT %p expected %p\n", res, gwl_parent );
111 res = GetParent( hwnd );
112 ok( res == get_parent, "Wrong result for GetParent %p expected %p\n", res, get_parent );
113 res = GetWindow( hwnd, GW_OWNER );
114 ok( res == gw_owner, "Wrong result for GW_OWNER %p expected %p\n", res, gw_owner );
115 if (pGetAncestor)
116 {
117 res = pGetAncestor( hwnd, GA_ROOT );
118 ok( res == ga_root, "Wrong result for GA_ROOT %p expected %p\n", res, ga_root );
119 res = pGetAncestor( hwnd, GA_ROOTOWNER );
120 ok( res == ga_root_owner, "Wrong result for GA_ROOTOWNER %p expected %p\n", res, ga_root_owner );
121 }
122 }
123
124 static BOOL ignore_message( UINT message )
125 {
126 /* these are always ignored */
127 return (message >= 0xc000 ||
128 message == WM_GETICON ||
129 message == WM_GETOBJECT ||
130 message == WM_TIMECHANGE ||
131 message == WM_DEVICECHANGE);
132 }
133
134 static BOOL CALLBACK EnumChildProc( HWND hwndChild, LPARAM lParam)
135 {
136 (*(LPINT)lParam)++;
137 trace("EnumChildProc on %p\n", hwndChild);
138 if (*(LPINT)lParam > 1) return FALSE;
139 return TRUE;
140 }
141
142 /* will search for the given window */
143 static BOOL CALLBACK EnumChildProc1( HWND hwndChild, LPARAM lParam)
144 {
145 trace("EnumChildProc1 on %p\n", hwndChild);
146 if ((HWND)lParam == hwndChild) return FALSE;
147 return TRUE;
148 }
149
150 static HWND create_tool_window( LONG style, HWND parent )
151 {
152 HWND ret = CreateWindowExA(0, "ToolWindowClass", "Tool window 1", style,
153 0, 0, 100, 100, parent, 0, 0, NULL );
154 ok( ret != 0, "Creation failed\n" );
155 return ret;
156 }
157
158 /* test parent and owner values for various combinations */
159 static void test_parent_owner(void)
160 {
161 LONG style;
162 HWND test, owner, ret;
163 HWND desktop = GetDesktopWindow();
164 HWND child = create_tool_window( WS_CHILD, hwndMain );
165 INT numChildren;
166
167 trace( "main window %p main2 %p desktop %p child %p\n", hwndMain, hwndMain2, desktop, child );
168
169 /* child without parent, should fail */
170 SetLastError(0xdeadbeef);
171 test = CreateWindowExA(0, "ToolWindowClass", "Tool window 1",
172 WS_CHILD, 0, 0, 100, 100, 0, 0, 0, NULL );
173 ok( !test, "WS_CHILD without parent created\n" );
174 ok( GetLastError() == ERROR_TLW_WITH_WSCHILD ||
175 broken(GetLastError() == 0xdeadbeef), /* win9x */
176 "CreateWindowExA error %u\n", GetLastError() );
177
178 /* desktop window */
179 check_parents( desktop, 0, 0, 0, 0, 0, 0 );
180 style = GetWindowLongA( desktop, GWL_STYLE );
181 ok( !SetWindowLongA( desktop, GWL_STYLE, WS_POPUP ), "Set GWL_STYLE on desktop succeeded\n" );
182 ok( !SetWindowLongA( desktop, GWL_STYLE, 0 ), "Set GWL_STYLE on desktop succeeded\n" );
183 ok( GetWindowLongA( desktop, GWL_STYLE ) == style, "Desktop style changed\n" );
184
185 /* normal child window */
186 test = create_tool_window( WS_CHILD, hwndMain );
187 trace( "created child %p\n", test );
188 check_parents( test, hwndMain, hwndMain, hwndMain, 0, hwndMain, hwndMain );
189 SetWindowLongA( test, GWL_STYLE, 0 );
190 check_parents( test, hwndMain, hwndMain, 0, 0, hwndMain, test );
191 SetWindowLongA( test, GWL_STYLE, WS_POPUP );
192 check_parents( test, hwndMain, hwndMain, 0, 0, hwndMain, test );
193 SetWindowLongA( test, GWL_STYLE, WS_POPUP|WS_CHILD );
194 check_parents( test, hwndMain, hwndMain, 0, 0, hwndMain, test );
195 SetWindowLongA( test, GWL_STYLE, WS_CHILD );
196 DestroyWindow( test );
197
198 /* normal child window with WS_MAXIMIZE */
199 test = create_tool_window( WS_CHILD | WS_MAXIMIZE, hwndMain );
200 DestroyWindow( test );
201
202 /* normal child window with WS_THICKFRAME */
203 test = create_tool_window( WS_CHILD | WS_THICKFRAME, hwndMain );
204 DestroyWindow( test );
205
206 /* popup window with WS_THICKFRAME */
207 test = create_tool_window( WS_POPUP | WS_THICKFRAME, hwndMain );
208 DestroyWindow( test );
209
210 /* child of desktop */
211 test = create_tool_window( WS_CHILD, desktop );
212 trace( "created child of desktop %p\n", test );
213 check_parents( test, desktop, 0, desktop, 0, test, desktop );
214 SetWindowLongA( test, GWL_STYLE, WS_POPUP );
215 check_parents( test, desktop, 0, 0, 0, test, test );
216 SetWindowLongA( test, GWL_STYLE, 0 );
217 check_parents( test, desktop, 0, 0, 0, test, test );
218 DestroyWindow( test );
219
220 /* child of desktop with WS_MAXIMIZE */
221 test = create_tool_window( WS_CHILD | WS_MAXIMIZE, desktop );
222 DestroyWindow( test );
223
224 /* child of desktop with WS_MINIMIZE */
225 test = create_tool_window( WS_CHILD | WS_MINIMIZE, desktop );
226 DestroyWindow( test );
227
228 /* child of child */
229 test = create_tool_window( WS_CHILD, child );
230 trace( "created child of child %p\n", test );
231 check_parents( test, child, child, child, 0, hwndMain, hwndMain );
232 SetWindowLongA( test, GWL_STYLE, 0 );
233 check_parents( test, child, child, 0, 0, hwndMain, test );
234 SetWindowLongA( test, GWL_STYLE, WS_POPUP );
235 check_parents( test, child, child, 0, 0, hwndMain, test );
236 DestroyWindow( test );
237
238 /* child of child with WS_MAXIMIZE */
239 test = create_tool_window( WS_CHILD | WS_MAXIMIZE, child );
240 DestroyWindow( test );
241
242 /* child of child with WS_MINIMIZE */
243 test = create_tool_window( WS_CHILD | WS_MINIMIZE, child );
244 DestroyWindow( test );
245
246 /* not owned top-level window */
247 test = create_tool_window( 0, 0 );
248 trace( "created top-level %p\n", test );
249 check_parents( test, desktop, 0, 0, 0, test, test );
250 SetWindowLongA( test, GWL_STYLE, WS_POPUP );
251 check_parents( test, desktop, 0, 0, 0, test, test );
252 SetWindowLongA( test, GWL_STYLE, WS_CHILD );
253 check_parents( test, desktop, 0, desktop, 0, test, desktop );
254 DestroyWindow( test );
255
256 /* not owned top-level window with WS_MAXIMIZE */
257 test = create_tool_window( WS_MAXIMIZE, 0 );
258 DestroyWindow( test );
259
260 /* owned top-level window */
261 test = create_tool_window( 0, hwndMain );
262 trace( "created owned top-level %p\n", test );
263 check_parents( test, desktop, hwndMain, 0, hwndMain, test, test );
264 SetWindowLongA( test, GWL_STYLE, WS_POPUP );
265 check_parents( test, desktop, hwndMain, hwndMain, hwndMain, test, hwndMain );
266 SetWindowLongA( test, GWL_STYLE, WS_CHILD );
267 check_parents( test, desktop, hwndMain, desktop, hwndMain, test, desktop );
268 DestroyWindow( test );
269
270 /* owned top-level window with WS_MAXIMIZE */
271 test = create_tool_window( WS_MAXIMIZE, hwndMain );
272 DestroyWindow( test );
273
274 /* not owned popup */
275 test = create_tool_window( WS_POPUP, 0 );
276 trace( "created popup %p\n", test );
277 check_parents( test, desktop, 0, 0, 0, test, test );
278 SetWindowLongA( test, GWL_STYLE, WS_CHILD );
279 check_parents( test, desktop, 0, desktop, 0, test, desktop );
280 SetWindowLongA( test, GWL_STYLE, 0 );
281 check_parents( test, desktop, 0, 0, 0, test, test );
282 DestroyWindow( test );
283
284 /* not owned popup with WS_MAXIMIZE */
285 test = create_tool_window( WS_POPUP | WS_MAXIMIZE, 0 );
286 DestroyWindow( test );
287
288 /* owned popup */
289 test = create_tool_window( WS_POPUP, hwndMain );
290 trace( "created owned popup %p\n", test );
291 check_parents( test, desktop, hwndMain, hwndMain, hwndMain, test, hwndMain );
292 SetWindowLongA( test, GWL_STYLE, WS_CHILD );
293 check_parents( test, desktop, hwndMain, desktop, hwndMain, test, desktop );
294 SetWindowLongA( test, GWL_STYLE, 0 );
295 check_parents( test, desktop, hwndMain, 0, hwndMain, test, test );
296 DestroyWindow( test );
297
298 /* owned popup with WS_MAXIMIZE */
299 test = create_tool_window( WS_POPUP | WS_MAXIMIZE, hwndMain );
300 DestroyWindow( test );
301
302 /* top-level window owned by child (same as owned by top-level) */
303 test = create_tool_window( 0, child );
304 trace( "created top-level owned by child %p\n", test );
305 check_parents( test, desktop, hwndMain, 0, hwndMain, test, test );
306 DestroyWindow( test );
307
308 /* top-level window owned by child (same as owned by top-level) with WS_MAXIMIZE */
309 test = create_tool_window( WS_MAXIMIZE, child );
310 DestroyWindow( test );
311
312 /* popup owned by desktop (same as not owned) */
313 test = create_tool_window( WS_POPUP, desktop );
314 trace( "created popup owned by desktop %p\n", test );
315 check_parents( test, desktop, 0, 0, 0, test, test );
316 DestroyWindow( test );
317
318 /* popup owned by desktop (same as not owned) with WS_MAXIMIZE */
319 test = create_tool_window( WS_POPUP | WS_MAXIMIZE, desktop );
320 DestroyWindow( test );
321
322 /* popup owned by child (same as owned by top-level) */
323 test = create_tool_window( WS_POPUP, child );
324 trace( "created popup owned by child %p\n", test );
325 check_parents( test, desktop, hwndMain, hwndMain, hwndMain, test, hwndMain );
326 DestroyWindow( test );
327
328 /* popup owned by child (same as owned by top-level) with WS_MAXIMIZE */
329 test = create_tool_window( WS_POPUP | WS_MAXIMIZE, child );
330 DestroyWindow( test );
331
332 /* not owned popup with WS_CHILD (same as WS_POPUP only) */
333 test = create_tool_window( WS_POPUP | WS_CHILD, 0 );
334 trace( "created WS_CHILD popup %p\n", test );
335 check_parents( test, desktop, 0, 0, 0, test, test );
336 DestroyWindow( test );
337
338 /* not owned popup with WS_CHILD | WS_MAXIMIZE (same as WS_POPUP only) */
339 test = create_tool_window( WS_POPUP | WS_CHILD | WS_MAXIMIZE, 0 );
340 DestroyWindow( test );
341
342 /* owned popup with WS_CHILD (same as WS_POPUP only) */
343 test = create_tool_window( WS_POPUP | WS_CHILD, hwndMain );
344 trace( "created owned WS_CHILD popup %p\n", test );
345 check_parents( test, desktop, hwndMain, hwndMain, hwndMain, test, hwndMain );
346 DestroyWindow( test );
347
348 /* owned popup with WS_CHILD (same as WS_POPUP only) with WS_MAXIMIZE */
349 test = create_tool_window( WS_POPUP | WS_CHILD | WS_MAXIMIZE, hwndMain );
350 DestroyWindow( test );
351
352 /******************** parent changes *************************/
353 trace( "testing parent changes\n" );
354
355 /* desktop window */
356 check_parents( desktop, 0, 0, 0, 0, 0, 0 );
357 if (0)
358 {
359 /* this test succeeds on NT but crashes on win9x systems */
360 ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)hwndMain2 );
361 ok( !ret, "Set GWL_HWNDPARENT succeeded on desktop\n" );
362 check_parents( desktop, 0, 0, 0, 0, 0, 0 );
363 ok( !SetParent( desktop, hwndMain ), "SetParent succeeded on desktop\n" );
364 check_parents( desktop, 0, 0, 0, 0, 0, 0 );
365 }
366 /* normal child window */
367 test = create_tool_window( WS_CHILD, hwndMain );
368 trace( "created child %p\n", test );
369
370 ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)hwndMain2 );
371 ok( ret == hwndMain, "GWL_HWNDPARENT return value %p expected %p\n", ret, hwndMain );
372 check_parents( test, hwndMain2, hwndMain2, hwndMain2, 0, hwndMain2, hwndMain2 );
373
374 ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)child );
375 ok( ret == hwndMain2, "GWL_HWNDPARENT return value %p expected %p\n", ret, hwndMain2 );
376 check_parents( test, child, child, child, 0, hwndMain, hwndMain );
377
378 ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)desktop );
379 ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
380 check_parents( test, desktop, 0, desktop, 0, test, desktop );
381
382 /* window is now child of desktop so GWLP_HWNDPARENT changes owner from now on */
383 ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)child );
384 ok( ret == 0, "GWL_HWNDPARENT return value %p expected 0\n", ret );
385 check_parents( test, desktop, child, desktop, child, test, desktop );
386
387 ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, 0 );
388 ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
389 check_parents( test, desktop, 0, desktop, 0, test, desktop );
390 DestroyWindow( test );
391
392 /* not owned top-level window */
393 test = create_tool_window( 0, 0 );
394 trace( "created top-level %p\n", test );
395
396 ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)hwndMain2 );
397 ok( ret == 0, "GWL_HWNDPARENT return value %p expected 0\n", ret );
398 check_parents( test, desktop, hwndMain2, 0, hwndMain2, test, test );
399
400 ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)child );
401 ok( ret == hwndMain2, "GWL_HWNDPARENT return value %p expected %p\n", ret, hwndMain2 );
402 check_parents( test, desktop, child, 0, child, test, test );
403
404 ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, 0 );
405 ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
406 check_parents( test, desktop, 0, 0, 0, test, test );
407 DestroyWindow( test );
408
409 /* not owned popup */
410 test = create_tool_window( WS_POPUP, 0 );
411 trace( "created popup %p\n", test );
412
413 ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)hwndMain2 );
414 ok( ret == 0, "GWL_HWNDPARENT return value %p expected 0\n", ret );
415 check_parents( test, desktop, hwndMain2, hwndMain2, hwndMain2, test, hwndMain2 );
416
417 ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (LONG_PTR)child );
418 ok( ret == hwndMain2, "GWL_HWNDPARENT return value %p expected %p\n", ret, hwndMain2 );
419 check_parents( test, desktop, child, child, child, test, hwndMain );
420
421 ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, 0 );
422 ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
423 check_parents( test, desktop, 0, 0, 0, test, test );
424 DestroyWindow( test );
425
426 /* normal child window */
427 test = create_tool_window( WS_CHILD, hwndMain );
428 trace( "created child %p\n", test );
429
430 ret = SetParent( test, desktop );
431 ok( ret == hwndMain, "SetParent return value %p expected %p\n", ret, hwndMain );
432 check_parents( test, desktop, 0, desktop, 0, test, desktop );
433
434 ret = SetParent( test, child );
435 ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
436 check_parents( test, child, child, child, 0, hwndMain, hwndMain );
437
438 ret = SetParent( test, hwndMain2 );
439 ok( ret == child, "SetParent return value %p expected %p\n", ret, child );
440 check_parents( test, hwndMain2, hwndMain2, hwndMain2, 0, hwndMain2, hwndMain2 );
441 DestroyWindow( test );
442
443 /* not owned top-level window */
444 test = create_tool_window( 0, 0 );
445 trace( "created top-level %p\n", test );
446
447 ret = SetParent( test, child );
448 ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
449 check_parents( test, child, child, 0, 0, hwndMain, test );
450
451 if (!is_win9x)
452 {
453 ShowWindow( test, SW_SHOW );
454 ret = SetParent( test, test );
455 ok( ret == NULL, "SetParent return value %p expected %p\n", ret, NULL );
456 ok( GetWindowLongA( test, GWL_STYLE ) & WS_VISIBLE, "window is not visible after SetParent\n" );
457 check_parents( test, child, child, 0, 0, hwndMain, test );
458 }
459 else
460 win_skip( "Test crashes on Win9x/WinMe\n" );
461 DestroyWindow( test );
462
463 /* owned popup */
464 test = create_tool_window( WS_POPUP, hwndMain2 );
465 trace( "created owned popup %p\n", test );
466
467 ret = SetParent( test, child );
468 ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
469 check_parents( test, child, child, hwndMain2, hwndMain2, hwndMain, hwndMain2 );
470
471 ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (ULONG_PTR)hwndMain );
472 ok( ret == child, "GWL_HWNDPARENT return value %p expected %p\n", ret, child );
473 check_parents( test, hwndMain, hwndMain, hwndMain2, hwndMain2, hwndMain, hwndMain2 );
474 DestroyWindow( test );
475
476 /**************** test owner destruction *******************/
477
478 /* owned child popup */
479 owner = create_tool_window( 0, 0 );
480 test = create_tool_window( WS_POPUP, owner );
481 trace( "created owner %p and popup %p\n", owner, test );
482 ret = SetParent( test, child );
483 ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
484 check_parents( test, child, child, owner, owner, hwndMain, owner );
485 /* window is now child of 'child' but owned by 'owner' */
486 DestroyWindow( owner );
487 ok( IsWindow(test), "Window %p destroyed by owner destruction\n", test );
488 /* Win98 doesn't pass this test. It doesn't allow a destroyed owner,
489 * while Win95, Win2k, WinXP do.
490 */
491 /*check_parents( test, child, child, owner, owner, hwndMain, owner );*/
492 ok( !IsWindow(owner), "Owner %p not destroyed\n", owner );
493 DestroyWindow(test);
494
495 /* owned top-level popup */
496 owner = create_tool_window( 0, 0 );
497 test = create_tool_window( WS_POPUP, owner );
498 trace( "created owner %p and popup %p\n", owner, test );
499 check_parents( test, desktop, owner, owner, owner, test, owner );
500 DestroyWindow( owner );
501 ok( !IsWindow(test), "Window %p not destroyed by owner destruction\n", test );
502
503 /* top-level popup owned by child */
504 owner = create_tool_window( WS_CHILD, hwndMain2 );
505 test = create_tool_window( WS_POPUP, 0 );
506 trace( "created owner %p and popup %p\n", owner, test );
507 ret = (HWND)SetWindowLongPtrA( test, GWLP_HWNDPARENT, (ULONG_PTR)owner );
508 ok( ret == 0, "GWL_HWNDPARENT return value %p expected 0\n", ret );
509 check_parents( test, desktop, owner, owner, owner, test, hwndMain2 );
510 DestroyWindow( owner );
511 ok( IsWindow(test), "Window %p destroyed by owner destruction\n", test );
512 ok( !IsWindow(owner), "Owner %p not destroyed\n", owner );
513 /* Win98 doesn't pass this test. It doesn't allow a destroyed owner,
514 * while Win95, Win2k, WinXP do.
515 */
516 /*check_parents( test, desktop, owner, owner, owner, test, owner );*/
517 DestroyWindow(test);
518
519 /* final cleanup */
520 DestroyWindow(child);
521
522
523 owner = create_tool_window( WS_OVERLAPPED, 0 );
524 test = create_tool_window( WS_POPUP, desktop );
525
526 ok( !GetWindow( test, GW_OWNER ), "Wrong owner window\n" );
527 numChildren = 0;
528 ok( !EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
529 "EnumChildWindows should have returned FALSE\n" );
530 ok( numChildren == 0, "numChildren should be 0 got %d\n", numChildren );
531
532 SetWindowLongA( test, GWL_STYLE, (GetWindowLongA( test, GWL_STYLE ) & ~WS_POPUP) | WS_CHILD );
533 ret = SetParent( test, owner );
534 ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
535
536 numChildren = 0;
537 ok( EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
538 "EnumChildWindows should have returned TRUE\n" );
539 ok( numChildren == 1, "numChildren should be 1 got %d\n", numChildren );
540
541 child = create_tool_window( WS_CHILD, owner );
542 numChildren = 0;
543 ok( !EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
544 "EnumChildWindows should have returned FALSE\n" );
545 ok( numChildren == 2, "numChildren should be 2 got %d\n", numChildren );
546 DestroyWindow( child );
547
548 child = create_tool_window( WS_VISIBLE | WS_OVERLAPPEDWINDOW, owner );
549 ok( GetWindow( child, GW_OWNER ) == owner, "Wrong owner window\n" );
550 numChildren = 0;
551 ok( EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
552 "EnumChildWindows should have returned TRUE\n" );
553 ok( numChildren == 1, "numChildren should be 1 got %d\n", numChildren );
554
555 ret = SetParent( child, owner );
556 ok( GetWindow( child, GW_OWNER ) == owner, "Wrong owner window\n" );
557 ok( ret == desktop, "SetParent return value %p expected %p\n", ret, desktop );
558 numChildren = 0;
559 ok( !EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
560 "EnumChildWindows should have returned FALSE\n" );
561 ok( numChildren == 2, "numChildren should be 2 got %d\n", numChildren );
562
563 ret = SetParent( child, NULL );
564 ok( GetWindow( child, GW_OWNER ) == owner, "Wrong owner window\n" );
565 ok( ret == owner, "SetParent return value %p expected %p\n", ret, owner );
566 numChildren = 0;
567 ok( EnumChildWindows( owner, EnumChildProc, (LPARAM)&numChildren ),
568 "EnumChildWindows should have returned TRUE\n" );
569 ok( numChildren == 1, "numChildren should be 1 got %d\n", numChildren );
570
571 /* even GW_OWNER == owner it's still a desktop's child */
572 ok( !EnumChildWindows( desktop, EnumChildProc1, (LPARAM)child ),
573 "EnumChildWindows should have found %p and returned FALSE\n", child );
574
575 DestroyWindow( child );
576 child = create_tool_window( WS_VISIBLE | WS_OVERLAPPEDWINDOW, NULL );
577
578 ok( !EnumChildWindows( desktop, EnumChildProc1, (LPARAM)child ),
579 "EnumChildWindows should have found %p and returned FALSE\n", child );
580
581 DestroyWindow( child );
582 DestroyWindow( test );
583 DestroyWindow( owner );
584 }
585
586 static BOOL CALLBACK enum_proc( HWND hwnd, LPARAM lParam)
587 {
588 (*(LPINT)lParam)++;
589 if (*(LPINT)lParam > 2) return FALSE;
590 return TRUE;
591 }
592 static DWORD CALLBACK enum_thread( void *arg )
593 {
594 INT count;
595 HWND hwnd[3];
596 BOOL ret;
597 MSG msg;
598
599 PeekMessage( &msg, 0, 0, 0, PM_NOREMOVE ); /* make sure we have a message queue */
600
601 count = 0;
602 ret = EnumThreadWindows( GetCurrentThreadId(), enum_proc, (LPARAM)&count );
603 ok( ret, "EnumThreadWindows should have returned TRUE\n" );
604 ok( count == 0, "count should be 0 got %d\n", count );
605
606 hwnd[0] = CreateWindowExA(0, "ToolWindowClass", "Tool window 1", WS_POPUP,
607 0, 0, 100, 100, 0, 0, 0, NULL );
608 count = 0;
609 ret = EnumThreadWindows( GetCurrentThreadId(), enum_proc, (LPARAM)&count );
610 ok( ret, "EnumThreadWindows should have returned TRUE\n" );
611 if (count != 2) /* Vista gives us two windows for the price of one */
612 {
613 ok( count == 1, "count should be 1 got %d\n", count );
614 hwnd[2] = CreateWindowExA(0, "ToolWindowClass", "Tool window 2", WS_POPUP,
615 0, 0, 100, 100, 0, 0, 0, NULL );
616 }
617 else hwnd[2] = 0;
618
619 hwnd[1] = CreateWindowExA(0, "ToolWindowClass", "Tool window 3", WS_POPUP,
620 0, 0, 100, 100, 0, 0, 0, NULL );
621 count = 0;
622 ret = EnumThreadWindows( GetCurrentThreadId(), enum_proc, (LPARAM)&count );
623 ok( !ret, "EnumThreadWindows should have returned FALSE\n" );
624 ok( count == 3, "count should be 3 got %d\n", count );
625
626 if (hwnd[2]) DestroyWindow(hwnd[2]);
627 DestroyWindow(hwnd[1]);
628 DestroyWindow(hwnd[0]);
629 return 0;
630 }
631
632 /* test EnumThreadWindows in a separate thread */
633 static void test_enum_thread_windows(void)
634 {
635 DWORD id;
636 HANDLE handle = CreateThread( NULL, 0, enum_thread, 0, 0, &id );
637 ok( !WaitForSingleObject( handle, 10000 ), "wait failed\n" );
638 CloseHandle( handle );
639 }
640
641 static LRESULT WINAPI main_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
642 {
643 switch (msg)
644 {
645 case WM_GETMINMAXINFO:
646 {
647 MINMAXINFO* minmax = (MINMAXINFO *)lparam;
648
649 trace("WM_GETMINMAXINFO: hwnd %p, %08lx, %08lx\n", hwnd, wparam, lparam);
650 dump_minmax_info( minmax );
651 SetWindowLongPtrA(hwnd, GWLP_USERDATA, 0x20031021);
652 break;
653 }
654 case WM_WINDOWPOSCHANGING:
655 {
656 WINDOWPOS *winpos = (WINDOWPOS *)lparam;
657 trace("main: WM_WINDOWPOSCHANGING %p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
658 winpos->hwnd, winpos->hwndInsertAfter,
659 winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
660 if (!(winpos->flags & SWP_NOMOVE))
661 {
662 ok(winpos->x >= -32768 && winpos->x <= 32767, "bad winpos->x %d\n", winpos->x);
663 ok(winpos->y >= -32768 && winpos->y <= 32767, "bad winpos->y %d\n", winpos->y);
664 }
665 /* Win9x does not fixup cx/xy for WM_WINDOWPOSCHANGING */
666 if (!(winpos->flags & SWP_NOSIZE) && !is_win9x)
667 {
668 ok(winpos->cx >= 0 && winpos->cx <= 32767, "bad winpos->cx %d\n", winpos->cx);
669 ok(winpos->cy >= 0 && winpos->cy <= 32767, "bad winpos->cy %d\n", winpos->cy);
670 }
671 break;
672 }
673 case WM_WINDOWPOSCHANGED:
674 {
675 RECT rc1, rc2;
676 WINDOWPOS *winpos = (WINDOWPOS *)lparam;
677 trace("main: WM_WINDOWPOSCHANGED %p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
678 winpos->hwnd, winpos->hwndInsertAfter,
679 winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
680 ok(winpos->x >= -32768 && winpos->x <= 32767, "bad winpos->x %d\n", winpos->x);
681 ok(winpos->y >= -32768 && winpos->y <= 32767, "bad winpos->y %d\n", winpos->y);
682
683 ok(winpos->cx >= 0 && winpos->cx <= 32767, "bad winpos->cx %d\n", winpos->cx);
684 ok(winpos->cy >= 0 && winpos->cy <= 32767, "bad winpos->cy %d\n", winpos->cy);
685
686 GetWindowRect(hwnd, &rc1);
687 SetRect(&rc2, winpos->x, winpos->y, winpos->x + winpos->cx, winpos->y + winpos->cy);
688 /* note: winpos coordinates are relative to parent */
689 MapWindowPoints(GetParent(hwnd), 0, (LPPOINT)&rc2, 2);
690 if (0)
691 {
692 /* Uncomment this once the test succeeds in all cases */
693 ok(EqualRect(&rc1, &rc2), "rects do not match (%d,%d-%d,%d) / (%d,%d-%d,%d)\n",
694 rc1.left, rc1.top, rc1.right, rc1.bottom, rc2.left, rc2.top, rc2.right, rc2.bottom );
695
696 GetClientRect(hwnd, &rc2);
697 DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc1);
698 MapWindowPoints(0, hwnd, (LPPOINT)&rc1, 2);
699 ok(EqualRect(&rc1, &rc2), "rects do not match (%d,%d-%d,%d) / (%d,%d-%d,%d)\n",
700 rc1.left, rc1.top, rc1.right, rc1.bottom, rc2.left, rc2.top, rc2.right, rc2.bottom );
701 }
702 break;
703 }
704 case WM_NCCREATE:
705 {
706 BOOL got_getminmaxinfo = GetWindowLongPtrA(hwnd, GWLP_USERDATA) == 0x20031021;
707 CREATESTRUCTA *cs = (CREATESTRUCTA *)lparam;
708
709 trace("WM_NCCREATE: hwnd %p, parent %p, style %08x\n", hwnd, cs->hwndParent, cs->style);
710 if (got_getminmaxinfo)
711 trace("%p got WM_GETMINMAXINFO\n", hwnd);
712
713 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
714 ok(got_getminmaxinfo, "main: WM_GETMINMAXINFO should have been received before WM_NCCREATE\n");
715 else
716 ok(!got_getminmaxinfo, "main: WM_GETMINMAXINFO should NOT have been received before WM_NCCREATE\n");
717 break;
718 }
719 case WM_COMMAND:
720 if (test_lbuttondown_flag)
721 {
722 ShowWindow((HWND)wparam, SW_SHOW);
723 flush_events( FALSE );
724 }
725 break;
726 }
727
728 return DefWindowProcA(hwnd, msg, wparam, lparam);
729 }
730
731 static LRESULT WINAPI tool_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
732 {
733 switch (msg)
734 {
735 case WM_GETMINMAXINFO:
736 {
737 MINMAXINFO* minmax = (MINMAXINFO *)lparam;
738
739 trace("hwnd %p, WM_GETMINMAXINFO, %08lx, %08lx\n", hwnd, wparam, lparam);
740 dump_minmax_info( minmax );
741 SetWindowLongPtrA(hwnd, GWLP_USERDATA, 0x20031021);
742 break;
743 }
744 case WM_NCCREATE:
745 {
746 BOOL got_getminmaxinfo = GetWindowLongPtrA(hwnd, GWLP_USERDATA) == 0x20031021;
747 CREATESTRUCTA *cs = (CREATESTRUCTA *)lparam;
748
749 trace("WM_NCCREATE: hwnd %p, parent %p, style %08x\n", hwnd, cs->hwndParent, cs->style);
750 if (got_getminmaxinfo)
751 trace("%p got WM_GETMINMAXINFO\n", hwnd);
752
753 if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
754 ok(got_getminmaxinfo, "tool: WM_GETMINMAXINFO should have been received before WM_NCCREATE\n");
755 else
756 ok(!got_getminmaxinfo, "tool: WM_GETMINMAXINFO should NOT have been received before WM_NCCREATE\n");
757 break;
758 }
759 }
760
761 return DefWindowProcA(hwnd, msg, wparam, lparam);
762 }
763
764 static BOOL RegisterWindowClasses(void)
765 {
766 WNDCLASSA cls;
767
768 cls.style = CS_DBLCLKS;
769 cls.lpfnWndProc = main_window_procA;
770 cls.cbClsExtra = 0;
771 cls.cbWndExtra = 0;
772 cls.hInstance = GetModuleHandleA(0);
773 cls.hIcon = 0;
774 cls.hCursor = LoadCursorA(0, IDC_ARROW);
775 cls.hbrBackground = GetStockObject(WHITE_BRUSH);
776 cls.lpszMenuName = NULL;
777 cls.lpszClassName = "MainWindowClass";
778
779 if(!RegisterClassA(&cls)) return FALSE;
780
781 cls.style = 0;
782 cls.lpfnWndProc = tool_window_procA;
783 cls.cbClsExtra = 0;
784 cls.cbWndExtra = 0;
785 cls.hInstance = GetModuleHandleA(0);
786 cls.hIcon = 0;
787 cls.hCursor = LoadCursorA(0, IDC_ARROW);
788 cls.hbrBackground = GetStockObject(WHITE_BRUSH);
789 cls.lpszMenuName = NULL;
790 cls.lpszClassName = "ToolWindowClass";
791
792 if(!RegisterClassA(&cls)) return FALSE;
793
794 return TRUE;
795 }
796
797 static void verify_window_info(const char *hook, HWND hwnd, const WINDOWINFO *info)
798 {
799 RECT rcWindow, rcClient;
800 DWORD status;
801
802 ok(IsWindow(hwnd), "bad window handle %p in hook %s\n", hwnd, hook);
803
804 GetWindowRect(hwnd, &rcWindow);
805 ok(EqualRect(&rcWindow, &info->rcWindow), "wrong rcWindow for %p in hook %s\n", hwnd, hook);
806
807 GetClientRect(hwnd, &rcClient);
808 /* translate to screen coordinates */
809 MapWindowPoints(hwnd, 0, (LPPOINT)&rcClient, 2);
810 ok(EqualRect(&rcClient, &info->rcClient), "wrong rcClient for %p in hook %s\n", hwnd, hook);
811
812 ok(info->dwStyle == (DWORD)GetWindowLongA(hwnd, GWL_STYLE),
813 "wrong dwStyle: %08x != %08x for %p in hook %s\n",
814 info->dwStyle, GetWindowLongA(hwnd, GWL_STYLE), hwnd, hook);
815 /* Windows reports some undocumented exstyles in WINDOWINFO, but
816 * doesn't return them in GetWindowLong(hwnd, GWL_EXSTYLE).
817 */
818 ok((info->dwExStyle & ~0xe0000800) == (DWORD)GetWindowLongA(hwnd, GWL_EXSTYLE),
819 "wrong dwExStyle: %08x != %08x for %p in hook %s\n",
820 info->dwExStyle, GetWindowLongA(hwnd, GWL_EXSTYLE), hwnd, hook);
821 status = (GetActiveWindow() == hwnd) ? WS_ACTIVECAPTION : 0;
822 if (GetForegroundWindow())
823 ok(info->dwWindowStatus == status, "wrong dwWindowStatus: %04x != %04x active %p fg %p in hook %s\n",
824 info->dwWindowStatus, status, GetActiveWindow(), GetForegroundWindow(), hook);
825
826 /* win2k and XP return broken border info in GetWindowInfo most of
827 * the time, so there is no point in testing it.
828 */
829 #if 0
830 UINT border;
831 ok(info->cxWindowBorders == (unsigned)(rcClient.left - rcWindow.left),
832 "wrong cxWindowBorders %d != %d\n", info->cxWindowBorders, rcClient.left - rcWindow.left);
833 border = min(rcWindow.bottom - rcClient.bottom, rcClient.top - rcWindow.top);
834 ok(info->cyWindowBorders == border,
835 "wrong cyWindowBorders %d != %d\n", info->cyWindowBorders, border);
836 #endif
837 ok(info->atomWindowType == GetClassLongA(hwnd, GCW_ATOM), "wrong atomWindowType for %p in hook %s\n",
838 hwnd, hook);
839 ok(info->wCreatorVersion == 0x0400 /* NT4, Win2000, XP, Win2003 */ ||
840 info->wCreatorVersion == 0x0500 /* Vista */,
841 "wrong wCreatorVersion %04x for %p in hook %s\n", info->wCreatorVersion, hwnd, hook);
842 }
843
844 static void FixedAdjustWindowRectEx(RECT* rc, LONG style, BOOL menu, LONG exstyle)
845 {
846 AdjustWindowRectEx(rc, style, menu, exstyle);
847 /* AdjustWindowRectEx does not include scroll bars */
848 if (style & WS_VSCROLL)
849 {
850 if(exstyle & WS_EX_LEFTSCROLLBAR)
851 rc->left -= GetSystemMetrics(SM_CXVSCROLL);
852 else
853 rc->right += GetSystemMetrics(SM_CXVSCROLL);
854 }
855 if (style & WS_HSCROLL)
856 rc->bottom += GetSystemMetrics(SM_CYHSCROLL);
857 }
858
859 static void test_nonclient_area(HWND hwnd)
860 {
861 DWORD style, exstyle;
862 RECT rc_window, rc_client, rc;
863 BOOL menu;
864 LRESULT ret;
865
866 style = GetWindowLongA(hwnd, GWL_STYLE);
867 exstyle = GetWindowLongA(hwnd, GWL_EXSTYLE);
868 menu = !(style & WS_CHILD) && GetMenu(hwnd) != 0;
869
870 GetWindowRect(hwnd, &rc_window);
871 GetClientRect(hwnd, &rc_client);
872
873 /* avoid some cases when things go wrong */
874 if (IsRectEmpty(&rc_window) || IsRectEmpty(&rc_client) ||
875 rc_window.right > 32768 || rc_window.bottom > 32768) return;
876
877 CopyRect(&rc, &rc_client);
878 MapWindowPoints(hwnd, 0, (LPPOINT)&rc, 2);
879 FixedAdjustWindowRectEx(&rc, style, menu, exstyle);
880
881 ok(EqualRect(&rc, &rc_window),
882 "window rect does not match: style:exstyle=0x%08x:0x%08x, menu=%d, win=(%d,%d)-(%d,%d), calc=(%d,%d)-(%d,%d)\n",
883 style, exstyle, menu, rc_window.left, rc_window.top, rc_window.right, rc_window.bottom,
884 rc.left, rc.top, rc.right, rc.bottom);
885
886
887 CopyRect(&rc, &rc_window);
888 DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc);
889 MapWindowPoints(0, hwnd, (LPPOINT)&rc, 2);
890 ok(EqualRect(&rc, &rc_client),
891 "client rect does not match: style:exstyle=0x%08x:0x%08x, menu=%d client=(%d,%d)-(%d,%d), calc=(%d,%d)-(%d,%d)\n",
892 style, exstyle, menu, rc_client.left, rc_client.top, rc_client.right, rc_client.bottom,
893 rc.left, rc.top, rc.right, rc.bottom);
894
895 /* NULL rectangle shouldn't crash */
896 ret = DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, 0);
897 ok(ret == 0, "NULL rectangle returned %ld instead of 0\n", ret);
898
899 /* Win9x doesn't like WM_NCCALCSIZE with synthetic data and crashes */;
900 if (is_win9x)
901 return;
902
903 /* and now test AdjustWindowRectEx and WM_NCCALCSIZE on synthetic data */
904 SetRect(&rc_client, 0, 0, 250, 150);
905 CopyRect(&rc_window, &rc_client);
906 MapWindowPoints(hwnd, 0, (LPPOINT)&rc_window, 2);
907 FixedAdjustWindowRectEx(&rc_window, style, menu, exstyle);
908
909 CopyRect(&rc, &rc_window);
910 DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc);
911 MapWindowPoints(0, hwnd, (LPPOINT)&rc, 2);
912 ok(EqualRect(&rc, &rc_client),
913 "synthetic rect does not match: style:exstyle=0x%08x:0x%08x, menu=%d, client=(%d,%d)-(%d,%d), calc=(%d,%d)-(%d,%d)\n",
914 style, exstyle, menu, rc_client.left, rc_client.top, rc_client.right, rc_client.bottom,
915 rc.left, rc.top, rc.right, rc.bottom);
916 }
917
918 static LRESULT CALLBACK cbt_hook_proc(int nCode, WPARAM wParam, LPARAM lParam)
919 {
920 static const char *CBT_code_name[10] = {
921 "HCBT_MOVESIZE",
922 "HCBT_MINMAX",
923 "HCBT_QS",
924 "HCBT_CREATEWND",
925 "HCBT_DESTROYWND",
926 "HCBT_ACTIVATE",
927 "HCBT_CLICKSKIPPED",
928 "HCBT_KEYSKIPPED",
929 "HCBT_SYSCOMMAND",
930 "HCBT_SETFOCUS" };
931 const char *code_name = (nCode >= 0 && nCode <= HCBT_SETFOCUS) ? CBT_code_name[nCode] : "Unknown";
932 HWND hwnd = (HWND)wParam;
933
934 switch (nCode)
935 {
936 case HCBT_CREATEWND:
937 {
938 static const RECT rc_null;
939 RECT rc;
940 LONG style;
941 CBT_CREATEWNDA *createwnd = (CBT_CREATEWNDA *)lParam;
942 trace("HCBT_CREATEWND: hwnd %p, parent %p, style %08x\n",
943 hwnd, createwnd->lpcs->hwndParent, createwnd->lpcs->style);
944 ok(createwnd->hwndInsertAfter == HWND_TOP, "hwndInsertAfter should be always HWND_TOP\n");
945
946 if (pGetWindowInfo)
947 {
948 WINDOWINFO info;
949 info.cbSize = sizeof(WINDOWINFO);
950 ok(pGetWindowInfo(hwnd, &info), "GetWindowInfo should not fail\n");
951 verify_window_info(code_name, hwnd, &info);
952 }
953
954 /* WS_VISIBLE should be turned off yet */
955 style = createwnd->lpcs->style & ~WS_VISIBLE;
956 ok(style == GetWindowLongA(hwnd, GWL_STYLE),
957 "style of hwnd and style in the CREATESTRUCT do not match: %08x != %08x\n",
958 GetWindowLongA(hwnd, GWL_STYLE), style);
959
960 if (0)
961 {
962 /* Uncomment this once the test succeeds in all cases */
963 if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD)
964 {
965 ok(GetParent(hwnd) == hwndMessage,
966 "wrong result from GetParent %p: message window %p\n",
967 GetParent(hwnd), hwndMessage);
968 }
969 else
970 ok(!GetParent(hwnd), "GetParent should return 0 at this point\n");
971
972 ok(!GetWindow(hwnd, GW_OWNER), "GW_OWNER should be set to 0 at this point\n");
973 }
974 if (0)
975 {
976 /* while NT assigns GW_HWNDFIRST/LAST some values at this point,
977 * Win9x still has them set to 0.
978 */
979 ok(GetWindow(hwnd, GW_HWNDFIRST) != 0, "GW_HWNDFIRST should not be set to 0 at this point\n");
980 ok(GetWindow(hwnd, GW_HWNDLAST) != 0, "GW_HWNDLAST should not be set to 0 at this point\n");
981 }
982 ok(!GetWindow(hwnd, GW_HWNDPREV), "GW_HWNDPREV should be set to 0 at this point\n");
983 ok(!GetWindow(hwnd, GW_HWNDNEXT), "GW_HWNDNEXT should be set to 0 at this point\n");
984
985 if (0)
986 {
987 /* Uncomment this once the test succeeds in all cases */
988 if (pGetAncestor)
989 {
990 ok(pGetAncestor(hwnd, GA_PARENT) == hwndMessage, "GA_PARENT should be set to hwndMessage at this point\n");
991 ok(pGetAncestor(hwnd, GA_ROOT) == hwnd,
992 "GA_ROOT is set to %p, expected %p\n", pGetAncestor(hwnd, GA_ROOT), hwnd);
993
994 if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD)
995 ok(pGetAncestor(hwnd, GA_ROOTOWNER) == hwndMessage,
996 "GA_ROOTOWNER should be set to hwndMessage at this point\n");
997 else
998 ok(pGetAncestor(hwnd, GA_ROOTOWNER) == hwnd,
999 "GA_ROOTOWNER is set to %p, expected %p\n", pGetAncestor(hwnd, GA_ROOTOWNER), hwnd);
1000 }
1001
1002 ok(GetWindowRect(hwnd, &rc), "GetWindowRect failed\n");
1003 ok(EqualRect(&rc, &rc_null), "window rect should be set to 0 HCBT_CREATEWND\n");
1004 ok(GetClientRect(hwnd, &rc), "GetClientRect failed\n");
1005 ok(EqualRect(&rc, &rc_null), "client rect should be set to 0 on HCBT_CREATEWND\n");
1006 }
1007 break;
1008 }
1009 case HCBT_MOVESIZE:
1010 case HCBT_MINMAX:
1011 case HCBT_ACTIVATE:
1012 case HCBT_SETFOCUS:
1013 if (pGetWindowInfo && IsWindow(hwnd))
1014 {
1015 WINDOWINFO info;
1016
1017 /* Win98 actually does check the info.cbSize and doesn't allow
1018 * it to be anything except sizeof(WINDOWINFO), while Win95, Win2k,
1019 * WinXP do not check it at all.
1020 */
1021 info.cbSize = sizeof(WINDOWINFO);
1022 ok(pGetWindowInfo(hwnd, &info), "GetWindowInfo should not fail\n");
1023 verify_window_info(code_name, hwnd, &info);
1024 }
1025 break;
1026 /* on HCBT_DESTROYWND window state is undefined */
1027 case HCBT_DESTROYWND:
1028 trace( "HCBT_DESTROYWND: hwnd %p\n", hwnd );
1029 break;
1030 default:
1031 break;
1032 }
1033
1034 return CallNextHookEx(hhook, nCode, wParam, lParam);
1035 }
1036
1037 static void test_shell_window(void)
1038 {
1039 BOOL ret;
1040 DWORD error;
1041 HMODULE hinst, hUser32;
1042 BOOL (WINAPI*SetShellWindow)(HWND);
1043 HWND hwnd1, hwnd2, hwnd3, hwnd4, hwnd5;
1044 HWND shellWindow, nextWnd;
1045
1046 if (is_win9x)
1047 {
1048 win_skip("Skipping shell window test on Win9x\n");
1049 return;
1050 }
1051
1052 shellWindow = GetShellWindow();
1053 hinst = GetModuleHandle(0);
1054 hUser32 = GetModuleHandleA("user32");
1055
1056 SetShellWindow = (void *)GetProcAddress(hUser32, "SetShellWindow");
1057
1058 trace("previous shell window: %p\n", shellWindow);
1059
1060 if (shellWindow) {
1061 DWORD pid;
1062 HANDLE hProcess;
1063
1064 GetWindowThreadProcessId(shellWindow, &pid);
1065 hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
1066 if (!hProcess)
1067 {
1068 skip( "cannot get access to shell process\n" );
1069 return;
1070 }
1071
1072 SetLastError(0xdeadbeef);
1073 ret = DestroyWindow(shellWindow);
1074 error = GetLastError();
1075
1076 ok(!ret, "DestroyWindow(shellWindow)\n");
1077 /* passes on Win XP, but not on Win98 */
1078 ok(error==ERROR_ACCESS_DENIED || error == 0xdeadbeef,
1079 "got %u after DestroyWindow(shellWindow)\n", error);
1080
1081 /* close old shell instance */
1082 ret = TerminateProcess(hProcess, 0);
1083 ok(ret, "termination of previous shell process failed: GetLastError()=%d\n", GetLastError());
1084 WaitForSingleObject(hProcess, INFINITE); /* wait for termination */
1085 CloseHandle(hProcess);
1086 }
1087
1088 hwnd1 = CreateWindowEx(0, TEXT("#32770"), TEXT("TEST1"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 100, 100, 300, 200, 0, 0, hinst, 0);
1089 trace("created window 1: %p\n", hwnd1);
1090
1091 ret = SetShellWindow(hwnd1);
1092 ok(ret, "first call to SetShellWindow(hwnd1)\n");
1093 shellWindow = GetShellWindow();
1094 ok(shellWindow==hwnd1, "wrong shell window: %p\n", shellWindow);
1095
1096 ret = SetShellWindow(hwnd1);
1097 ok(!ret, "second call to SetShellWindow(hwnd1)\n");
1098
1099 ret = SetShellWindow(0);
1100 error = GetLastError();
1101 /* passes on Win XP, but not on Win98
1102 ok(!ret, "reset shell window by SetShellWindow(0)\n");
1103 ok(error==ERROR_INVALID_WINDOW_HANDLE, "ERROR_INVALID_WINDOW_HANDLE after SetShellWindow(0)\n"); */
1104
1105 ret = SetShellWindow(hwnd1);
1106 /* passes on Win XP, but not on Win98
1107 ok(!ret, "third call to SetShellWindow(hwnd1)\n"); */
1108
1109 SetWindowLong(hwnd1, GWL_EXSTYLE, GetWindowLong(hwnd1,GWL_EXSTYLE)|WS_EX_TOPMOST);
1110 ret = GetWindowLong(hwnd1,GWL_EXSTYLE)&WS_EX_TOPMOST? TRUE: FALSE;
1111 ok(!ret, "SetWindowExStyle(hwnd1, WS_EX_TOPMOST)\n");
1112
1113 ret = DestroyWindow(hwnd1);
1114 ok(ret, "DestroyWindow(hwnd1)\n");
1115
1116 hwnd2 = CreateWindowEx(WS_EX_TOPMOST, TEXT("#32770"), TEXT("TEST2"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 150, 250, 300, 200, 0, 0, hinst, 0);
1117 trace("created window 2: %p\n", hwnd2);
1118 ret = SetShellWindow(hwnd2);
1119 ok(!ret, "SetShellWindow(hwnd2) with WS_EX_TOPMOST\n");
1120
1121 hwnd3 = CreateWindowEx(0, TEXT("#32770"), TEXT("TEST3"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 200, 400, 300, 200, 0, 0, hinst, 0);
1122 trace("created window 3: %p\n", hwnd3);
1123
1124 hwnd4 = CreateWindowEx(0, TEXT("#32770"), TEXT("TEST4"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 250, 500, 300, 200, 0, 0, hinst, 0);
1125 trace("created window 4: %p\n", hwnd4);
1126
1127 nextWnd = GetWindow(hwnd4, GW_HWNDNEXT);
1128 ok(nextWnd==hwnd3, "wrong next window for hwnd4: %p - expected hwnd3\n", nextWnd);
1129
1130 ret = SetShellWindow(hwnd4);
1131 ok(ret, "SetShellWindow(hwnd4)\n");
1132 shellWindow = GetShellWindow();
1133 ok(shellWindow==hwnd4, "wrong shell window: %p - expected hwnd4\n", shellWindow);
1134
1135 nextWnd = GetWindow(hwnd4, GW_HWNDNEXT);
1136 ok(nextWnd==0, "wrong next window for hwnd4: %p - expected 0\n", nextWnd);
1137
1138 ret = SetWindowPos(hwnd4, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
1139 ok(ret, "SetWindowPos(hwnd4, HWND_TOPMOST)\n");
1140
1141 ret = SetWindowPos(hwnd4, hwnd3, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
1142 ok(ret, "SetWindowPos(hwnd4, hwnd3\n");
1143
1144 ret = SetShellWindow(hwnd3);
1145 ok(!ret, "SetShellWindow(hwnd3)\n");
1146 shellWindow = GetShellWindow();
1147 ok(shellWindow==hwnd4, "wrong shell window: %p - expected hwnd4\n", shellWindow);
1148
1149 hwnd5 = CreateWindowEx(0, TEXT("#32770"), TEXT("TEST5"), WS_OVERLAPPEDWINDOW/*|WS_VISIBLE*/, 300, 600, 300, 200, 0, 0, hinst, 0);
1150 trace("created window 5: %p\n", hwnd5);
1151 ret = SetWindowPos(hwnd4, hwnd5, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
1152 ok(ret, "SetWindowPos(hwnd4, hwnd5)\n");
1153
1154 todo_wine
1155 {
1156 nextWnd = GetWindow(hwnd4, GW_HWNDNEXT);
1157 ok(nextWnd==0, "wrong next window for hwnd4 after SetWindowPos(): %p - expected 0\n", nextWnd);
1158 }
1159
1160 /* destroy test windows */
1161 DestroyWindow(hwnd2);
1162 DestroyWindow(hwnd3);
1163 DestroyWindow(hwnd4);
1164 DestroyWindow(hwnd5);
1165 }
1166
1167 /************** MDI test ****************/
1168
1169 static char mdi_lParam_test_message[] = "just a test string";
1170
1171 static void test_MDI_create(HWND parent, HWND mdi_client, INT_PTR first_id)
1172 {
1173 MDICREATESTRUCTA mdi_cs;
1174 HWND mdi_child;
1175 INT_PTR id;
1176 static const WCHAR classW[] = {'M','D','I','_','c','h','i','l','d','_','C','l','a','s','s','_','1',0};
1177 static const WCHAR titleW[] = {'M','D','I',' ','c','h','i','l','d',0};
1178 BOOL isWin9x = FALSE;
1179
1180 mdi_cs.szClass = "MDI_child_Class_1";
1181 mdi_cs.szTitle = "MDI child";
1182 mdi_cs.hOwner = GetModuleHandle(0);
1183 mdi_cs.x = CW_USEDEFAULT;
1184 mdi_cs.y = CW_USEDEFAULT;
1185 mdi_cs.cx = CW_USEDEFAULT;
1186 mdi_cs.cy = CW_USEDEFAULT;
1187 mdi_cs.style = 0;
1188 mdi_cs.lParam = (LPARAM)mdi_lParam_test_message;
1189 mdi_child = (HWND)SendMessageA(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
1190 ok(mdi_child != 0, "MDI child creation failed\n");
1191 id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1192 ok(id == first_id, "wrong child id %ld\n", id);
1193 SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1194 ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1195
1196 mdi_cs.style = 0x7fffffff; /* without WS_POPUP */
1197 mdi_child = (HWND)SendMessageA(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
1198 ok(mdi_child != 0, "MDI child creation failed\n");
1199 id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1200 ok(id == first_id, "wrong child id %ld\n", id);
1201 SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1202 ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1203
1204 mdi_cs.style = 0xffffffff; /* with WS_POPUP */
1205 mdi_child = (HWND)SendMessageA(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
1206 if (GetWindowLongA(mdi_client, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1207 {
1208 ok(!mdi_child, "MDI child with WS_POPUP and with MDIS_ALLCHILDSTYLES should fail\n");
1209 }
1210 else
1211 {
1212 ok(mdi_child != 0, "MDI child creation failed\n");
1213 id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1214 ok(id == first_id, "wrong child id %ld\n", id);
1215 SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1216 ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1217 }
1218
1219 /* test MDICREATESTRUCT A<->W mapping */
1220 /* MDICREATESTRUCTA and MDICREATESTRUCTW have the same layout */
1221 mdi_cs.style = 0;
1222 mdi_cs.szClass = (LPCSTR)classW;
1223 mdi_cs.szTitle = (LPCSTR)titleW;
1224 SetLastError(0xdeadbeef);
1225 mdi_child = (HWND)SendMessageW(mdi_client, WM_MDICREATE, 0, (LPARAM)&mdi_cs);
1226 if (!mdi_child)
1227 {
1228 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1229 isWin9x = TRUE;
1230 else
1231 ok(mdi_child != 0, "MDI child creation failed\n");
1232 }
1233 else
1234 {
1235 id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1236 ok(id == first_id, "wrong child id %ld\n", id);
1237 SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1238 ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1239 }
1240
1241 mdi_child = CreateMDIWindowA("MDI_child_Class_1", "MDI child",
1242 0,
1243 CW_USEDEFAULT, CW_USEDEFAULT,
1244 CW_USEDEFAULT, CW_USEDEFAULT,
1245 mdi_client, GetModuleHandle(0),
1246 (LPARAM)mdi_lParam_test_message);
1247 ok(mdi_child != 0, "MDI child creation failed\n");
1248 id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1249 ok(id == first_id, "wrong child id %ld\n", id);
1250 SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1251 ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1252
1253 mdi_child = CreateMDIWindowA("MDI_child_Class_1", "MDI child",
1254 0x7fffffff, /* without WS_POPUP */
1255 CW_USEDEFAULT, CW_USEDEFAULT,
1256 CW_USEDEFAULT, CW_USEDEFAULT,
1257 mdi_client, GetModuleHandle(0),
1258 (LPARAM)mdi_lParam_test_message);
1259 ok(mdi_child != 0, "MDI child creation failed\n");
1260 id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1261 ok(id == first_id, "wrong child id %ld\n", id);
1262 SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1263 ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1264
1265 mdi_child = CreateMDIWindowA("MDI_child_Class_1", "MDI child",
1266 0xffffffff, /* with WS_POPUP */
1267 CW_USEDEFAULT, CW_USEDEFAULT,
1268 CW_USEDEFAULT, CW_USEDEFAULT,
1269 mdi_client, GetModuleHandle(0),
1270 (LPARAM)mdi_lParam_test_message);
1271 if (GetWindowLongA(mdi_client, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1272 {
1273 ok(!mdi_child, "MDI child with WS_POPUP and with MDIS_ALLCHILDSTYLES should fail\n");
1274 }
1275 else
1276 {
1277 ok(mdi_child != 0, "MDI child creation failed\n");
1278 id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1279 ok(id == first_id, "wrong child id %ld\n", id);
1280 SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1281 ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1282 }
1283
1284 /* test MDICREATESTRUCT A<->W mapping */
1285 SetLastError(0xdeadbeef);
1286 mdi_child = CreateMDIWindowW(classW, titleW,
1287 0,
1288 CW_USEDEFAULT, CW_USEDEFAULT,
1289 CW_USEDEFAULT, CW_USEDEFAULT,
1290 mdi_client, GetModuleHandle(0),
1291 (LPARAM)mdi_lParam_test_message);
1292 if (!mdi_child)
1293 {
1294 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1295 isWin9x = TRUE;
1296 else
1297 ok(mdi_child != 0, "MDI child creation failed\n");
1298 }
1299 else
1300 {
1301 id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1302 ok(id == first_id, "wrong child id %ld\n", id);
1303 SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1304 ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1305 }
1306
1307 mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_1", "MDI child",
1308 0,
1309 CW_USEDEFAULT, CW_USEDEFAULT,
1310 CW_USEDEFAULT, CW_USEDEFAULT,
1311 mdi_client, 0, GetModuleHandle(0),
1312 mdi_lParam_test_message);
1313 ok(mdi_child != 0, "MDI child creation failed\n");
1314 id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1315 ok(id == first_id, "wrong child id %ld\n", id);
1316 SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1317 ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1318
1319 mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_1", "MDI child",
1320 0x7fffffff, /* without WS_POPUP */
1321 CW_USEDEFAULT, CW_USEDEFAULT,
1322 CW_USEDEFAULT, CW_USEDEFAULT,
1323 mdi_client, 0, GetModuleHandle(0),
1324 mdi_lParam_test_message);
1325 ok(mdi_child != 0, "MDI child creation failed\n");
1326 id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1327 ok(id == first_id, "wrong child id %ld\n", id);
1328 SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1329 ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1330
1331 mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_1", "MDI child",
1332 0xffffffff, /* with WS_POPUP */
1333 CW_USEDEFAULT, CW_USEDEFAULT,
1334 CW_USEDEFAULT, CW_USEDEFAULT,
1335 mdi_client, 0, GetModuleHandle(0),
1336 mdi_lParam_test_message);
1337 if (GetWindowLongA(mdi_client, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1338 {
1339 ok(!mdi_child, "MDI child with WS_POPUP and with MDIS_ALLCHILDSTYLES should fail\n");
1340 }
1341 else
1342 {
1343 ok(mdi_child != 0, "MDI child creation failed\n");
1344 id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1345 ok(id == first_id, "wrong child id %ld\n", id);
1346 SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1347 ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1348 }
1349
1350 /* test MDICREATESTRUCT A<->W mapping */
1351 SetLastError(0xdeadbeef);
1352 mdi_child = CreateWindowExW(WS_EX_MDICHILD, classW, titleW,
1353 0,
1354 CW_USEDEFAULT, CW_USEDEFAULT,
1355 CW_USEDEFAULT, CW_USEDEFAULT,
1356 mdi_client, 0, GetModuleHandle(0),
1357 mdi_lParam_test_message);
1358 if (!mdi_child)
1359 {
1360 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1361 isWin9x = TRUE;
1362 else
1363 ok(mdi_child != 0, "MDI child creation failed\n");
1364 }
1365 else
1366 {
1367 id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1368 ok(id == first_id, "wrong child id %ld\n", id);
1369 SendMessageA(mdi_client, WM_MDIDESTROY, (WPARAM)mdi_child, 0);
1370 ok(!IsWindow(mdi_child), "WM_MDIDESTROY failed\n");
1371 }
1372
1373 /* This test fails on Win9x */
1374 if (!isWin9x)
1375 {
1376 mdi_child = CreateWindowExA(WS_EX_MDICHILD, "MDI_child_Class_2", "MDI child",
1377 WS_CHILD,
1378 CW_USEDEFAULT, CW_USEDEFAULT,
1379 CW_USEDEFAULT, CW_USEDEFAULT,
1380 parent, 0, GetModuleHandle(0),
1381 mdi_lParam_test_message);
1382 ok(!mdi_child, "WS_EX_MDICHILD with a not MDIClient parent should fail\n");
1383 }
1384
1385 mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1386 WS_CHILD, /* without WS_POPUP */
1387 CW_USEDEFAULT, CW_USEDEFAULT,
1388 CW_USEDEFAULT, CW_USEDEFAULT,
1389 mdi_client, 0, GetModuleHandle(0),
1390 mdi_lParam_test_message);
1391 ok(mdi_child != 0, "MDI child creation failed\n");
1392 id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1393 ok(id == 0, "wrong child id %ld\n", id);
1394 DestroyWindow(mdi_child);
1395
1396 mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1397 WS_CHILD | WS_POPUP, /* with WS_POPUP */
1398 CW_USEDEFAULT, CW_USEDEFAULT,
1399 CW_USEDEFAULT, CW_USEDEFAULT,
1400 mdi_client, 0, GetModuleHandle(0),
1401 mdi_lParam_test_message);
1402 ok(mdi_child != 0, "MDI child creation failed\n");
1403 id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1404 ok(id == 0, "wrong child id %ld\n", id);
1405 DestroyWindow(mdi_child);
1406
1407 /* maximized child */
1408 mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1409 WS_CHILD | WS_MAXIMIZE,
1410 CW_USEDEFAULT, CW_USEDEFAULT,
1411 CW_USEDEFAULT, CW_USEDEFAULT,
1412 mdi_client, 0, GetModuleHandle(0),
1413 mdi_lParam_test_message);
1414 ok(mdi_child != 0, "MDI child creation failed\n");
1415 id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1416 ok(id == 0, "wrong child id %ld\n", id);
1417 DestroyWindow(mdi_child);
1418
1419 trace("Creating maximized child with a caption\n");
1420 mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1421 WS_CHILD | WS_MAXIMIZE | WS_CAPTION,
1422 CW_USEDEFAULT, CW_USEDEFAULT,
1423 CW_USEDEFAULT, CW_USEDEFAULT,
1424 mdi_client, 0, GetModuleHandle(0),
1425 mdi_lParam_test_message);
1426 ok(mdi_child != 0, "MDI child creation failed\n");
1427 id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1428 ok(id == 0, "wrong child id %ld\n", id);
1429 DestroyWindow(mdi_child);
1430
1431 trace("Creating maximized child with a caption and a thick frame\n");
1432 mdi_child = CreateWindowExA(0, "MDI_child_Class_2", "MDI child",
1433 WS_CHILD | WS_MAXIMIZE | WS_CAPTION | WS_THICKFRAME,
1434 CW_USEDEFAULT, CW_USEDEFAULT,
1435 CW_USEDEFAULT, CW_USEDEFAULT,
1436 mdi_client, 0, GetModuleHandle(0),
1437 mdi_lParam_test_message);
1438 ok(mdi_child != 0, "MDI child creation failed\n");
1439 id = GetWindowLongPtrA(mdi_child, GWLP_ID);
1440 ok(id == 0, "wrong child id %ld\n", id);
1441 DestroyWindow(mdi_child);
1442 }
1443
1444 /**********************************************************************
1445 * MDI_ChildGetMinMaxInfo (copied from windows/mdi.c)
1446 *
1447 * Note: The rule here is that client rect of the maximized MDI child
1448 * is equal to the client rect of the MDI client window.
1449 */
1450 static void MDI_ChildGetMinMaxInfo( HWND client, HWND hwnd, MINMAXINFO* lpMinMax )
1451 {
1452 RECT rect;
1453
1454 GetClientRect( client, &rect );
1455 AdjustWindowRectEx( &rect, GetWindowLongA( hwnd, GWL_STYLE ),
1456 0, GetWindowLongA( hwnd, GWL_EXSTYLE ));
1457
1458 rect.right -= rect.left;
1459 rect.bottom -= rect.top;
1460 lpMinMax->ptMaxSize.x = rect.right;
1461 lpMinMax->ptMaxSize.y = rect.bottom;
1462
1463 lpMinMax->ptMaxPosition.x = rect.left;
1464 lpMinMax->ptMaxPosition.y = rect.top;
1465
1466 trace("max rect (%d,%d - %d, %d)\n",
1467 rect.left, rect.top, rect.right, rect.bottom);
1468 }
1469
1470 static LRESULT WINAPI mdi_child_wnd_proc_1(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1471 {
1472 switch (msg)
1473 {
1474 case WM_NCCREATE:
1475 case WM_CREATE:
1476 {
1477 CREATESTRUCTA *cs = (CREATESTRUCTA *)lparam;
1478 MDICREATESTRUCTA *mdi_cs = cs->lpCreateParams;
1479
1480 ok(cs->dwExStyle & WS_EX_MDICHILD, "WS_EX_MDICHILD should be set\n");
1481 ok(mdi_cs->lParam == (LPARAM)mdi_lParam_test_message, "wrong mdi_cs->lParam\n");
1482
1483 ok(!lstrcmpA(cs->lpszClass, "MDI_child_Class_1"), "wrong class name\n");
1484 ok(!lstrcmpA(cs->lpszClass, mdi_cs->szClass), "class name does not match\n");
1485 ok(!lstrcmpA(cs->lpszName, "MDI child"), "wrong title\n");
1486 ok(!lstrcmpA(cs->lpszName, mdi_cs->szTitle), "title does not match\n");
1487 ok(cs->hInstance == mdi_cs->hOwner, "%p != %p\n", cs->hInstance, mdi_cs->hOwner);
1488
1489 /* MDICREATESTRUCT should have original values */
1490 ok(mdi_cs->style == 0 || mdi_cs->style == 0x7fffffff || mdi_cs->style == 0xffffffff,
1491 "mdi_cs->style does not match (%08x)\n", mdi_cs->style);
1492 ok(mdi_cs->x == CW_USEDEFAULT, "%d != CW_USEDEFAULT\n", mdi_cs->x);
1493 ok(mdi_cs->y == CW_USEDEFAULT, "%d != CW_USEDEFAULT\n", mdi_cs->y);
1494 ok(mdi_cs->cx == CW_USEDEFAULT, "%d != CW_USEDEFAULT\n", mdi_cs->cx);
1495 ok(mdi_cs->cy == CW_USEDEFAULT, "%d != CW_USEDEFAULT\n", mdi_cs->cy);
1496
1497 /* CREATESTRUCT should have fixed values */
1498 ok(cs->x != CW_USEDEFAULT, "%d == CW_USEDEFAULT\n", cs->x);
1499 ok(cs->y != CW_USEDEFAULT, "%d == CW_USEDEFAULT\n", cs->y);
1500
1501 /* cx/cy == CW_USEDEFAULT are translated to NOT zero values */
1502 ok(cs->cx != CW_USEDEFAULT && cs->cx != 0, "%d == CW_USEDEFAULT\n", cs->cx);
1503 ok(cs->cy != CW_USEDEFAULT && cs->cy != 0, "%d == CW_USEDEFAULT\n", cs->cy);
1504
1505 ok(!(cs->style & WS_POPUP), "WS_POPUP is not allowed\n");
1506
1507 if (GetWindowLongA(cs->hwndParent, GWL_STYLE) & MDIS_ALLCHILDSTYLES)
1508 {
1509 LONG style = mdi_cs->style | WS_CHILD | WS_CLIPSIBLINGS;
1510 ok(cs->style == style,
1511 "cs->style does not match (%08x)\n", cs->style);
1512 }
1513 else
1514 {
1515 LONG style = mdi_cs->style;
1516 style &= ~WS_POPUP;
1517 style |= WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CAPTION |
1518 WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
1519 ok(cs->style == style,
1520 "cs->style does not match (%08x)\n", cs->style);
1521 }
1522 break;
1523 }
1524
1525 case WM_GETMINMAXINFO:
1526 {
1527 HWND client = GetParent(hwnd);
1528 RECT rc;
1529 MINMAXINFO *minmax = (MINMAXINFO *)lparam;
1530 MINMAXINFO my_minmax;
1531 LONG style, exstyle;
1532
1533 style = GetWindowLongA(hwnd, GWL_STYLE);
1534 exstyle = GetWindowLongA(hwnd, GWL_EXSTYLE);
1535
1536 GetWindowRect(client, &rc);
1537 trace("MDI client %p window size = (%d x %d)\n", client, rc.right-rc.left, rc.bottom-rc.top);
1538 GetClientRect(client, &rc);
1539 trace("MDI client %p client size = (%d x %d)\n", client, rc.right, rc.bottom);
1540 trace("screen size: %d x %d\n", GetSystemMetrics(SM_CXSCREEN),
1541 GetSystemMetrics(SM_CYSCREEN));
1542
1543 GetClientRect(client, &rc);
1544 if ((style & WS_CAPTION) == WS_CAPTION)
1545 style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
1546 AdjustWindowRectEx(&rc, style, 0, exstyle);
1547 trace("MDI child: calculated max window size = (%d x %d)\n", rc.right-rc.left, rc.bottom-rc.top);
1548 dump_minmax_info( minmax );
1549
1550 ok(minmax->ptMaxSize.x == rc.right - rc.left, "default width of maximized child %d != %d\n",
1551 minmax->ptMaxSize.x, rc.right - rc.left);
1552 ok(minmax->ptMaxSize.y == rc.bottom - rc.top, "default height of maximized child %d != %d\n",
1553 minmax->ptMaxSize.y, rc.bottom - rc.top);
1554
1555 DefMDIChildProcA(hwnd, msg, wparam, lparam);
1556
1557 trace("DefMDIChildProc returned:\n");
1558 dump_minmax_info( minmax );
1559
1560 MDI_ChildGetMinMaxInfo(client, hwnd, &my_minmax);
1561 ok(minmax->ptMaxSize.x == my_minmax.ptMaxSize.x, "default width of maximized child %d != %d\n",
1562 minmax->ptMaxSize.x, my_minmax.ptMaxSize.x);
1563 ok(minmax->ptMaxSize.y == my_minmax.ptMaxSize.y, "default height of maximized child %d != %d\n",
1564 minmax->ptMaxSize.y, my_minmax.ptMaxSize.y);
1565
1566 return 1;
1567 }
1568
1569 case WM_MDIACTIVATE:
1570 {
1571 HWND active, client = GetParent(hwnd);
1572 /*trace("%p WM_MDIACTIVATE %08x %08lx\n", hwnd, wparam, lparam);*/
1573 active = (HWND)SendMessageA(client, WM_MDIGETACTIVE, 0, 0);
1574 if (hwnd == (HWND)lparam) /* if we are being activated */
1575 ok (active == (HWND)lparam, "new active %p != active %p\n", (HWND)lparam, active);
1576 else
1577 ok (active == (HWND)wparam, "old active %p != active %p\n", (HWND)wparam, active);
1578 break;
1579 }
1580 }
1581 return DefMDIChildProcA(hwnd, msg, wparam, lparam);
1582 }
1583
1584 static LRESULT WINAPI mdi_child_wnd_proc_2(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1585 {
1586 switch (msg)
1587 {
1588 case WM_NCCREATE:
1589 case WM_CREATE:
1590 {
1591 CREATESTRUCTA *cs = (CREATESTRUCTA *)lparam;
1592
1593 trace("%s: x %d, y %d, cx %d, cy %d\n", (msg == WM_NCCREATE) ? "WM_NCCREATE" : "WM_CREATE",
1594 cs->x, cs->y, cs->cx, cs->cy);
1595
1596 ok(!(cs->dwExStyle & WS_EX_MDICHILD), "WS_EX_MDICHILD should not be set\n");
1597 ok(cs->lpCreateParams == mdi_lParam_test_message, "wrong cs->lpCreateParams\n");
1598
1599 ok(!lstrcmpA(cs->lpszClass, "MDI_child_Class_2"), "wrong class name\n");
1600 ok(!lstrcmpA(cs->lpszName, "MDI child"), "wrong title\n");
1601
1602 /* CREATESTRUCT should have fixed values */
1603 /* For some reason Win9x doesn't translate cs->x from CW_USEDEFAULT,
1604 while NT does. */
1605 /*ok(cs->x != CW_USEDEFAULT, "%d == CW_USEDEFAULT\n", cs->x);*/
1606 ok(cs->y != CW_USEDEFAULT, "%d == CW_USEDEFAULT\n", cs->y);
1607
1608 /* cx/cy == CW_USEDEFAULT are translated to 0 */
1609 /* For some reason Win98 doesn't translate cs->cx from CW_USEDEFAULT,
1610 while Win95, Win2k, WinXP do. */
1611 /*ok(cs->cx == 0, "%d != 0\n", cs->cx);*/
1612 ok(cs->cy == 0, "%d != 0\n", cs->cy);
1613 break;
1614 }
1615
1616 case WM_GETMINMAXINFO:
1617 {
1618 HWND parent = GetParent(hwnd);
1619 RECT rc;
1620 MINMAXINFO *minmax = (MINMAXINFO *)lparam;
1621 LONG style, exstyle;
1622
1623 style = GetWindowLongA(hwnd, GWL_STYLE);
1624 exstyle = GetWindowLongA(hwnd, GWL_EXSTYLE);
1625
1626 GetClientRect(parent, &rc);
1627 trace("WM_GETMINMAXINFO: parent %p client size = (%d x %d)\n", parent, rc.right, rc.bottom);
1628
1629 GetClientRect(parent, &rc);
1630 if ((style & WS_CAPTION) == WS_CAPTION)
1631 style &= ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
1632 AdjustWindowRectEx(&rc, style, 0, exstyle);
1633 dump_minmax_info( minmax );
1634
1635 ok(minmax->ptMaxSize.x == rc.right - rc.left, "default width of maximized child %d != %d\n",
1636 minmax->ptMaxSize.x, rc.right - rc.left);
1637 ok(minmax->ptMaxSize.y == rc.bottom - rc.top, "default height of maximized child %d != %d\n",
1638 minmax->ptMaxSize.y, rc.bottom - rc.top);
1639 break;
1640 }
1641
1642 case WM_WINDOWPOSCHANGED:
1643 {
1644 WINDOWPOS *winpos = (WINDOWPOS *)lparam;
1645 RECT rc1, rc2;
1646
1647 GetWindowRect(hwnd, &rc1);
1648 SetRect(&rc2, winpos->x, winpos->y, winpos->x + winpos->cx, winpos->y + winpos->cy);
1649 /* note: winpos coordinates are relative to parent */
1650 MapWindowPoints(GetParent(hwnd), 0, (LPPOINT)&rc2, 2);
1651 ok(EqualRect(&rc1, &rc2), "rects do not match, window=(%d,%d)-(%d,%d) pos=(%d,%d)-(%d,%d)\n",
1652 rc1.left, rc1.top, rc1.right, rc1.bottom,
1653 rc2.left, rc2.top, rc2.right, rc2.bottom);
1654 GetWindowRect(hwnd, &rc1);
1655 GetClientRect(hwnd, &rc2);
1656 DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc1);
1657 MapWindowPoints(0, hwnd, (LPPOINT)&rc1, 2);
1658 ok(EqualRect(&rc1, &rc2), "rects do not match, window=(%d,%d)-(%d,%d) client=(%d,%d)-(%d,%d)\n",
1659 rc1.left, rc1.top, rc1.right, rc1.bottom,
1660 rc2.left, rc2.top, rc2.right, rc2.bottom);
1661 }
1662 /* fall through */
1663 case WM_WINDOWPOSCHANGING:
1664 {
1665 WINDOWPOS *winpos = (WINDOWPOS *)lparam;
1666 WINDOWPOS my_winpos = *winpos;
1667
1668 trace("%s: %p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
1669 (msg == WM_WINDOWPOSCHANGING) ? "WM_WINDOWPOSCHANGING" : "WM_WINDOWPOSCHANGED",
1670 winpos->hwnd, winpos->hwndInsertAfter,
1671 winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
1672
1673 DefWindowProcA(hwnd, msg, wparam, lparam);
1674
1675 ok(!memcmp(&my_winpos, winpos, sizeof(WINDOWPOS)),
1676 "DefWindowProc should not change WINDOWPOS: %p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
1677 winpos->hwnd, winpos->hwndInsertAfter,
1678 winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
1679
1680 return 1;
1681 }
1682 }
1683 return DefWindowProcA(hwnd, msg, wparam, lparam);
1684 }
1685
1686 static LRESULT WINAPI mdi_main_wnd_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1687 {
1688 static HWND mdi_client;
1689
1690 switch (msg)
1691 {
1692 case WM_CREATE:
1693 {
1694 CLIENTCREATESTRUCT client_cs;
1695 RECT rc;
1696
1697 GetClientRect(hwnd, &rc);
1698
1699 client_cs.hWindowMenu = 0;
1700 client_cs.idFirstChild = 1;
1701
1702 /* MDIClient without MDIS_ALLCHILDSTYLES */
1703 mdi_client = CreateWindowExA(0, "mdiclient",
1704 NULL,
1705 WS_CHILD /*| WS_VISIBLE*/,
1706 /* tests depend on a not zero MDIClient size */
1707 0, 0, rc.right, rc.bottom,
1708 hwnd, 0, GetModuleHandle(0),
1709 &client_cs);
1710 assert(mdi_client);
1711 test_MDI_create(hwnd, mdi_client, client_cs.idFirstChild);
1712 DestroyWindow(mdi_client);
1713
1714 /* MDIClient with MDIS_ALLCHILDSTYLES */
1715 mdi_client = CreateWindowExA(0, "mdiclient",
1716 NULL,
1717 WS_CHILD | MDIS_ALLCHILDSTYLES /*| WS_VISIBLE*/,
1718 /* tests depend on a not zero MDIClient size */
1719 0, 0, rc.right, rc.bottom,
1720 hwnd, 0, GetModuleHandle(0),
1721 &client_cs);
1722 assert(mdi_client);
1723 test_MDI_create(hwnd, mdi_client, client_cs.idFirstChild);
1724 DestroyWindow(mdi_client);
1725 break;
1726 }
1727
1728 case WM_WINDOWPOSCHANGED:
1729 {
1730 WINDOWPOS *winpos = (WINDOWPOS *)lparam;
1731 RECT rc1, rc2;
1732
1733 GetWindowRect(hwnd, &rc1);
1734 trace("window: (%d,%d)-(%d,%d)\n", rc1.left, rc1.top, rc1.right, rc1.bottom);
1735 SetRect(&rc2, winpos->x, winpos->y, winpos->x + winpos->cx, winpos->y + winpos->cy);
1736 /* note: winpos coordinates are relative to parent */
1737 MapWindowPoints(GetParent(hwnd), 0, (LPPOINT)&rc2, 2);
1738 trace("pos: (%d,%d)-(%d,%d)\n", rc2.left, rc2.top, rc2.right, rc2.bottom);
1739 ok(EqualRect(&rc1, &rc2), "rects do not match\n");
1740
1741 GetWindowRect(hwnd, &rc1);
1742 GetClientRect(hwnd, &rc2);
1743 DefWindowProcA(hwnd, WM_NCCALCSIZE, 0, (LPARAM)&rc1);
1744 MapWindowPoints(0, hwnd, (LPPOINT)&rc1, 2);
1745 ok(EqualRect(&rc1, &rc2), "rects do not match\n");
1746 }
1747 /* fall through */
1748 case WM_WINDOWPOSCHANGING:
1749 {
1750 WINDOWPOS *winpos = (WINDOWPOS *)lparam;
1751 WINDOWPOS my_winpos = *winpos;
1752
1753 trace("%s\n", (msg == WM_WINDOWPOSCHANGING) ? "WM_WINDOWPOSCHANGING" : "WM_WINDOWPOSCHANGED");
1754 trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
1755 winpos->hwnd, winpos->hwndInsertAfter,
1756 winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
1757
1758 DefWindowProcA(hwnd, msg, wparam, lparam);
1759
1760 trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
1761 winpos->hwnd, winpos->hwndInsertAfter,
1762 winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
1763
1764 ok(!memcmp(&my_winpos, winpos, sizeof(WINDOWPOS)),
1765 "DefWindowProc should not change WINDOWPOS values\n");
1766
1767 return 1;
1768 }
1769
1770 case WM_CLOSE:
1771 PostQuitMessage(0);
1772 break;
1773 }
1774 return DefFrameProcA(hwnd, mdi_client, msg, wparam, lparam);
1775 }
1776
1777 static BOOL mdi_RegisterWindowClasses(void)
1778 {
1779 WNDCLASSA cls;
1780
1781 cls.style = 0;
1782 cls.lpfnWndProc = mdi_main_wnd_procA;
1783 cls.cbClsExtra = 0;
1784 cls.cbWndExtra = 0;
1785 cls.hInstance = GetModuleHandleA(0);
1786 cls.hIcon = 0;
1787 cls.hCursor = LoadCursorA(0, IDC_ARROW);
1788 cls.hbrBackground = GetStockObject(WHITE_BRUSH);
1789 cls.lpszMenuName = NULL;
1790 cls.lpszClassName = "MDI_parent_Class";
1791 if(!RegisterClassA(&cls)) return FALSE;
1792
1793 cls.lpfnWndProc = mdi_child_wnd_proc_1;
1794 cls.lpszClassName = "MDI_child_Class_1";
1795 if(!RegisterClassA(&cls)) return FALSE;
1796
1797 cls.lpfnWndProc = mdi_child_wnd_proc_2;
1798 cls.lpszClassName = "MDI_child_Class_2";
1799 if(!RegisterClassA(&cls)) return FALSE;
1800
1801 return TRUE;
1802 }
1803
1804 static void test_mdi(void)
1805 {
1806 HWND mdi_hwndMain;
1807 /*MSG msg;*/
1808
1809 if (!mdi_RegisterWindowClasses()) assert(0);
1810
1811 mdi_hwndMain = CreateWindowExA(0, "MDI_parent_Class", "MDI parent window",
1812 WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
1813 WS_MAXIMIZEBOX /*| WS_VISIBLE*/,
1814 100, 100, CW_USEDEFAULT, CW_USEDEFAULT,
1815 GetDesktopWindow(), 0,
1816 GetModuleHandle(0), NULL);
1817 assert(mdi_hwndMain);
1818 /*
1819 while(GetMessage(&msg, 0, 0, 0))
1820 {
1821 TranslateMessage(&msg);
1822 DispatchMessage(&msg);
1823 }
1824 */
1825 DestroyWindow(mdi_hwndMain);
1826 }
1827
1828 static void test_icons(void)
1829 {
1830 WNDCLASSEXA cls;
1831 HWND hwnd;
1832 HICON icon = LoadIconA(0, IDI_APPLICATION);
1833 HICON icon2 = LoadIconA(0, IDI_QUESTION);
1834 HICON small_icon = LoadImageA(0, IDI_APPLICATION, IMAGE_ICON,
1835 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED );
1836 HICON res;
1837
1838 cls.cbSize = sizeof(cls);
1839 cls.style = 0;
1840 cls.lpfnWndProc = DefWindowProcA;
1841 cls.cbClsExtra = 0;
1842 cls.cbWndExtra = 0;
1843 cls.hInstance = 0;
1844 cls.hIcon = LoadIconA(0, IDI_HAND);
1845 cls.hIconSm = small_icon;
1846 cls.hCursor = LoadCursorA(0, IDC_ARROW);
1847 cls.hbrBackground = GetStockObject(WHITE_BRUSH);
1848 cls.lpszMenuName = NULL;
1849 cls.lpszClassName = "IconWindowClass";
1850
1851 RegisterClassExA(&cls);
1852
1853 hwnd = CreateWindowExA(0, "IconWindowClass", "icon test", 0,
1854 100, 100, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, NULL, NULL);
1855 assert( hwnd );
1856
1857 res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_BIG, 0 );
1858 ok( res == 0, "wrong big icon %p/0\n", res );
1859 res = (HICON)SendMessageA( hwnd, WM_SETICON, ICON_BIG, (LPARAM)icon );
1860 ok( res == 0, "wrong previous big icon %p/0\n", res );
1861 res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_BIG, 0 );
1862 ok( res == icon, "wrong big icon after set %p/%p\n", res, icon );
1863 res = (HICON)SendMessageA( hwnd, WM_SETICON, ICON_BIG, (LPARAM)icon2 );
1864 ok( res == icon, "wrong previous big icon %p/%p\n", res, icon );
1865 res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_BIG, 0 );
1866 ok( res == icon2, "wrong big icon after set %p/%p\n", res, icon2 );
1867
1868 res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL, 0 );
1869 ok( res == 0, "wrong small icon %p/0\n", res );
1870 /* this test is XP specific */
1871 /*res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL2, 0 );
1872 ok( res != 0, "wrong small icon %p\n", res );*/
1873 res = (HICON)SendMessageA( hwnd, WM_SETICON, ICON_SMALL, (LPARAM)icon );
1874 ok( res == 0, "wrong previous small icon %p/0\n", res );
1875 res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL, 0 );
1876 ok( res == icon, "wrong small icon after set %p/%p\n", res, icon );
1877 /* this test is XP specific */
1878 /*res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL2, 0 );
1879 ok( res == icon, "wrong small icon after set %p/%p\n", res, icon );*/
1880 res = (HICON)SendMessageA( hwnd, WM_SETICON, ICON_SMALL, (LPARAM)small_icon );
1881 ok( res == icon, "wrong previous small icon %p/%p\n", res, icon );
1882 res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL, 0 );
1883 ok( res == small_icon, "wrong small icon after set %p/%p\n", res, small_icon );
1884 /* this test is XP specific */
1885 /*res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_SMALL2, 0 );
1886 ok( res == small_icon, "wrong small icon after set %p/%p\n", res, small_icon );*/
1887
1888 /* make sure the big icon hasn't changed */
1889 res = (HICON)SendMessageA( hwnd, WM_GETICON, ICON_BIG, 0 );
1890 ok( res == icon2, "wrong big icon after set %p/%p\n", res, icon2 );
1891
1892 DestroyWindow( hwnd );
1893 }
1894
1895 static LRESULT WINAPI nccalcsize_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1896 {
1897 if (msg == WM_NCCALCSIZE)
1898 {
1899 RECT *rect = (RECT *)lparam;
1900 /* first time around increase the rectangle, next time decrease it */
1901 if (rect->left == 100) InflateRect( rect, 10, 10 );
1902 else InflateRect( rect, -10, -10 );
1903 return 0;
1904 }
1905 return DefWindowProc( hwnd, msg, wparam, lparam );
1906 }
1907
1908 static void test_SetWindowPos(HWND hwnd)
1909 {
1910 RECT orig_win_rc, rect;
1911 LONG_PTR old_proc;
1912
1913 SetRect(&rect, 111, 222, 333, 444);
1914 ok(!GetWindowRect(0, &rect), "GetWindowRect succeeded\n");
1915 ok(rect.left == 111 && rect.top == 222 && rect.right == 333 && rect.bottom == 444,
1916 "wrong window rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
1917
1918 SetRect(&rect, 111, 222, 333, 444);
1919 ok(!GetClientRect(0, &rect), "GetClientRect succeeded\n");
1920 ok(rect.left == 111 && rect.top == 222 && rect.right == 333 && rect.bottom == 444,
1921 "wrong window rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
1922
1923 GetWindowRect(hwnd, &orig_win_rc);
1924
1925 old_proc = SetWindowLongPtr( hwnd, GWLP_WNDPROC, (ULONG_PTR)nccalcsize_proc );
1926 SetWindowPos(hwnd, 0, 100, 100, 0, 0, SWP_NOZORDER|SWP_FRAMECHANGED);
1927 GetWindowRect( hwnd, &rect );
1928 ok( rect.left == 100 && rect.top == 100 && rect.right == 100 && rect.bottom == 100,
1929 "invalid window rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
1930 GetClientRect( hwnd, &rect );
1931 MapWindowPoints( hwnd, 0, (POINT *)&rect, 2 );
1932 ok( rect.left == 90 && rect.top == 90 && rect.right == 110 && rect.bottom == 110,
1933 "invalid client rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
1934
1935 SetWindowPos(hwnd, 0, 200, 200, 0, 0, SWP_NOZORDER|SWP_FRAMECHANGED);
1936 GetWindowRect( hwnd, &rect );
1937 ok( rect.left == 200 && rect.top == 200 && rect.right == 200 && rect.bottom == 200,
1938 "invalid window rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
1939 GetClientRect( hwnd, &rect );
1940 MapWindowPoints( hwnd, 0, (POINT *)&rect, 2 );
1941 ok( rect.left == 210 && rect.top == 210 && rect.right == 190 && rect.bottom == 190,
1942 "invalid client rect %d,%d-%d,%d\n", rect.left, rect.top, rect.right, rect.bottom );
1943
1944 SetWindowPos(hwnd, 0, orig_win_rc.left, orig_win_rc.top,
1945 orig_win_rc.right, orig_win_rc.bottom, 0);
1946 SetWindowLongPtr( hwnd, GWLP_WNDPROC, old_proc );
1947
1948 /* Win9x truncates coordinates to 16-bit irrespectively */
1949 if (!is_win9x)
1950 {
1951 SetWindowPos(hwnd, 0, -32769, -40000, -32769, -90000, SWP_NOMOVE);
1952 SetWindowPos(hwnd, 0, 32768, 40000, 32768, 40000, SWP_NOMOVE);
1953
1954 SetWindowPos(hwnd, 0, -32769, -40000, -32769, -90000, SWP_NOSIZE);
1955 SetWindowPos(hwnd, 0, 32768, 40000, 32768, 40000, SWP_NOSIZE);
1956 }
1957
1958 SetWindowPos(hwnd, 0, orig_win_rc.left, orig_win_rc.top,
1959 orig_win_rc.right, orig_win_rc.bottom, 0);
1960
1961 ok(!(GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST), "WS_EX_TOPMOST should not be set\n");
1962 SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
1963 ok(GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST, "WS_EX_TOPMOST should be set\n");
1964 SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
1965 ok(GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST, "WS_EX_TOPMOST should be set\n");
1966 SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
1967 ok(!(GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST), "WS_EX_TOPMOST should not be set\n");
1968 }
1969
1970 static void test_SetMenu(HWND parent)
1971 {
1972 HWND child;
1973 HMENU hMenu, ret;
1974 BOOL retok;
1975 DWORD style;
1976
1977 hMenu = CreateMenu();
1978 assert(hMenu);
1979
1980 ok(SetMenu(parent, hMenu), "SetMenu on a top level window should not fail\n");
1981 if (0)
1982 {
1983 /* fails on (at least) Wine, NT4, XP SP2 */
1984 test_nonclient_area(parent);
1985 }
1986 ret = GetMenu(parent);
1987 ok(ret == hMenu, "unexpected menu id %p\n", ret);
1988 /* test whether we can destroy a menu assigned to a window */
1989 retok = DestroyMenu(hMenu);
1990 ok( retok, "DestroyMenu error %d\n", GetLastError());
1991 retok = IsMenu(hMenu);
1992 ok(!retok || broken(retok) /* nt4 */, "menu handle should be not valid after DestroyMenu\n");
1993 ret = GetMenu(parent);
1994 /* This test fails on Win9x */
1995 if (!is_win9x)
1996 ok(ret == hMenu, "unexpected menu id %p\n", ret);
1997 ok(SetMenu(parent, 0), "SetMenu(0) on a top level window should not fail\n");
1998 test_nonclient_area(parent);
1999
2000 hMenu = CreateMenu();
2001 assert(hMenu);
2002
2003 /* parent */
2004 ret = GetMenu(parent);
2005 ok(ret == 0, "unexpected menu id %p\n", ret);
2006
2007 ok(!SetMenu(parent, (HMENU)20), "SetMenu with invalid menu handle should fail\n");
2008 test_nonclient_area(parent);
2009 ret = GetMenu(parent);
2010 ok(ret == 0, "unexpected menu id %p\n", ret);
2011
2012 ok(SetMenu(parent, hMenu), "SetMenu on a top level window should not fail\n");
2013 if (0)
2014 {
2015 /* fails on (at least) Wine, NT4, XP SP2 */
2016 test_nonclient_area(parent);
2017 }
2018 ret = GetMenu(parent);
2019 ok(ret == hMenu, "unexpected menu id %p\n", ret);
2020
2021 ok(SetMenu(parent, 0), "SetMenu(0) on a top level window should not fail\n");
2022 test_nonclient_area(parent);
2023 ret = GetMenu(parent);
2024 ok(ret == 0, "unexpected menu id %p\n", ret);
2025
2026 /* child */
2027 child = CreateWindowExA(0, "static", NULL, WS_CHILD, 0, 0, 0, 0, parent, (HMENU)10, 0, NULL);
2028 assert(child);
2029
2030 ret = GetMenu(child);
2031 ok(ret == (HMENU)10, "unexpected menu id %p\n", ret);
2032
2033 ok(!SetMenu(child, (HMENU)20), "SetMenu with invalid menu handle should fail\n");
2034 test_nonclient_area(child);
2035 ret = GetMenu(child);
2036 ok(ret == (HMENU)10, "unexpected menu id %p\n", ret);
2037
2038 ok(!SetMenu(child, hMenu), "SetMenu on a child window should fail\n");
2039 test_nonclient_area(child);
2040 ret = GetMenu(child);
2041 ok(ret == (HMENU)10, "unexpected menu id %p\n", ret);
2042
2043 ok(!SetMenu(child, 0), "SetMenu(0) on a child window should fail\n");
2044 test_nonclient_area(child);
2045 ret = GetMenu(child);
2046 ok(ret == (HMENU)10, "unexpected menu id %p\n", ret);
2047
2048 style = GetWindowLong(child, GWL_STYLE);
2049 SetWindowLong(child, GWL_STYLE, style | WS_POPUP);
2050 ok(SetMenu(child, hMenu), "SetMenu on a popup child window should not fail\n");
2051 ok(SetMenu(child, 0), "SetMenu on a popup child window should not fail\n");
2052 SetWindowLong(child, GWL_STYLE, style);
2053
2054 SetWindowLong(child, GWL_STYLE, style | WS_OVERLAPPED);
2055 ok(!SetMenu(child, hMenu), "SetMenu on a overlapped child window should fail\n");
2056 SetWindowLong(child, GWL_STYLE, style);
2057
2058 DestroyWindow(child);
2059 DestroyMenu(hMenu);
2060 }
2061
2062 static void test_window_tree(HWND parent, const DWORD *style, const int *order, int total)
2063 {
2064 HWND child[5], hwnd;
2065 INT_PTR i;
2066
2067 assert(total <= 5);
2068
2069 hwnd = GetWindow(parent, GW_CHILD);
2070 ok(!hwnd, "have to start without children to perform the test\n");
2071
2072 for (i = 0; i < total; i++)
2073 {
2074 if (style[i] & DS_CONTROL)
2075 {
2076 child[i] = CreateWindowExA(0, MAKEINTATOM(32770), "", style[i] & ~WS_VISIBLE,
2077 0,0,0,0, parent, (HMENU)i, 0, NULL);
2078 if (style[i] & WS_VISIBLE)
2079 ShowWindow(child[i], SW_SHOW);
2080
2081 SetWindowPos(child[i], HWND_BOTTOM, 0,0,10,10, SWP_NOACTIVATE);
2082 }
2083 else
2084 child[i] = CreateWindowExA(0, "static", "", style[i], 0,0,10,10,
2085 parent, (HMENU)i, 0, NULL);
2086 trace("child[%ld] = %p\n", i, child[i]);
2087 ok(child[i] != 0, "CreateWindowEx failed to create child window\n");
2088 }
2089
2090 hwnd = GetWindow(parent, GW_CHILD);
2091 ok(hwnd != 0, "GetWindow(GW_CHILD) failed\n");
2092 ok(hwnd == GetWindow(child[total - 1], GW_HWNDFIRST), "GW_HWNDFIRST is wrong\n");
2093 ok(child[order[total - 1]] == GetWindow(child[0], GW_HWNDLAST), "GW_HWNDLAST is wrong\n");
2094
2095 for (i = 0; i < total; i++)
2096 {
2097 trace("hwnd[%ld] = %p\n", i, hwnd);
2098 ok(child[order[i]] == hwnd, "Z order of child #%ld is wrong\n", i);
2099
2100 hwnd = GetWindow(hwnd, GW_HWNDNEXT);
2101 }
2102
2103 for (i = 0; i < total; i++)
2104 ok(DestroyWindow(child[i]), "DestroyWindow failed\n");
2105 }
2106
2107 static void test_children_zorder(HWND parent)
2108 {
2109 const DWORD simple_style[5] = { WS_CHILD, WS_CHILD, WS_CHILD, WS_CHILD,
2110 WS_CHILD };
2111 const int simple_order[5] = { 0, 1, 2, 3, 4 };
2112
2113 const DWORD complex_style[5] = { WS_CHILD, WS_CHILD | WS_MAXIMIZE,
2114 WS_CHILD | WS_VISIBLE, WS_CHILD,
2115 WS_CHILD | WS_MAXIMIZE | WS_VISIBLE };
2116 const int complex_order_1[1] = { 0 };
2117 const int complex_order_2[2] = { 1, 0 };
2118 const int complex_order_3[3] = { 1, 0, 2 };
2119 const int complex_order_4[4] = { 1, 0, 2, 3 };
2120 const int complex_order_5[5] = { 4, 1, 0, 2, 3 };
2121 const DWORD complex_style_6[3] = { WS_CHILD | WS_VISIBLE,
2122 WS_CHILD | WS_CLIPSIBLINGS | DS_CONTROL | WS_VISIBLE,
2123 WS_CHILD | WS_VISIBLE };
2124 const int complex_order_6[3] = { 0, 1, 2 };
2125
2126 /* simple WS_CHILD */
2127 test_window_tree(parent, simple_style, simple_order, 5);
2128
2129 /* complex children styles */
2130 test_window_tree(parent, complex_style, complex_order_1, 1);
2131 test_window_tree(parent, complex_style, complex_order_2, 2);
2132 test_window_tree(parent, complex_style, complex_order_3, 3);
2133 test_window_tree(parent, complex_style, complex_order_4, 4);
2134 test_window_tree(parent, complex_style, complex_order_5, 5);
2135
2136 /* another set of complex children styles */
2137 test_window_tree(parent, complex_style_6, complex_order_6, 3);
2138 }
2139
2140 #define check_z_order(hwnd, next, prev, owner, topmost) \
2141 check_z_order_debug((hwnd), (next), (prev), (owner), (topmost), \
2142 __FILE__, __LINE__)
2143
2144 static void check_z_order_debug(HWND hwnd, HWND next, HWND prev, HWND owner,
2145 BOOL topmost, const char *file, int line)
2146 {
2147 HWND test;
2148 DWORD ex_style;
2149
2150 test = GetWindow(hwnd, GW_HWNDNEXT);
2151 /* skip foreign windows */
2152 while (test && test != next &&
2153 (GetWindowThreadProcessId(test, NULL) != our_pid ||
2154 UlongToHandle(GetWindowLongPtr(test, GWLP_HINSTANCE)) != GetModuleHandle(0) ||
2155 GetWindow(test, GW_OWNER) == next))
2156 {
2157 /*trace("skipping next %p (%p)\n", test, UlongToHandle(GetWindowLongPtr(test, GWLP_HINSTANCE)));*/
2158 test = GetWindow(test, GW_HWNDNEXT);
2159 }
2160 ok_(file, line)(next == test, "%p: expected next %p, got %p\n", hwnd, next, test);
2161
2162 test = GetWindow(hwnd, GW_HWNDPREV);
2163 /* skip foreign windows */
2164 while (test && test != prev &&
2165 (GetWindowThreadProcessId(test, NULL) != our_pid ||
2166 UlongToHandle(GetWindowLongPtr(test, GWLP_HINSTANCE)) != GetModuleHandle(0) ||
2167 GetWindow(test, GW_OWNER) == hwnd))
2168 {
2169 /*trace("skipping prev %p (%p)\n", test, UlongToHandle(GetWindowLongPtr(test, GWLP_HINSTANCE)));*/
2170 test = GetWindow(test, GW_HWNDPREV);
2171 }
2172 ok_(file, line)(prev == test, "%p: expected prev %p, got %p\n", hwnd, prev, test);
2173
2174 test = GetWindow(hwnd, GW_OWNER);
2175 ok_(file, line)(owner == test, "%p: expected owner %p, got %p\n", hwnd, owner, test);
2176
2177 ex_style = GetWindowLong(hwnd, GWL_EXSTYLE);
2178 ok_(file, line)(!(ex_style & WS_EX_TOPMOST) == !topmost, "%p: expected %stopmost\n",
2179 hwnd, topmost ? "" : "NOT ");
2180 }
2181
2182 static void test_popup_zorder(HWND hwnd_D, HWND hwnd_E, DWORD style)
2183 {
2184 HWND hwnd_A, hwnd_B, hwnd_C, hwnd_F;
2185
2186 trace("hwnd_D %p, hwnd_E %p\n", hwnd_D, hwnd_E);
2187
2188 SetWindowPos(hwnd_E, hwnd_D, 0,0,0,0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE);
2189
2190 check_z_order(hwnd_D, hwnd_E, 0, 0, FALSE);
2191 check_z_order(hwnd_E, 0, hwnd_D, 0, FALSE);
2192
2193 hwnd_F = CreateWindowEx(0, "MainWindowClass", "Owner window",
2194 WS_OVERLAPPED | WS_CAPTION,
2195 100, 100, 100, 100,
2196 0, 0, GetModuleHandle(0), NULL);
2197 trace("hwnd_F %p\n", hwnd_F);
2198 check_z_order(hwnd_F, hwnd_D, 0, 0, FALSE);
2199
2200 SetWindowPos(hwnd_F, hwnd_E, 0,0,0,0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE);
2201 check_z_order(hwnd_F, 0, hwnd_E, 0, FALSE);
2202 check_z_order(hwnd_E, hwnd_F, hwnd_D, 0, FALSE);
2203 check_z_order(hwnd_D, hwnd_E, 0, 0, FALSE);
2204
2205 hwnd_C = CreateWindowEx(0, "MainWindowClass", NULL,
2206 style,
2207 100, 100, 100, 100,
2208 hwnd_F, 0, GetModuleHandle(0), NULL);
2209 trace("hwnd_C %p\n", hwnd_C);
2210 check_z_order(hwnd_F, 0, hwnd_E, 0, FALSE);
2211 check_z_order(hwnd_E, hwnd_F, hwnd_D, 0, FALSE);
2212 check_z_order(hwnd_D, hwnd_E, hwnd_C, 0, FALSE);
2213 check_z_order(hwnd_C, hwnd_D, 0, hwnd_F, FALSE);
2214
2215 hwnd_B = CreateWindowEx(WS_EX_TOPMOST, "MainWindowClass", NULL,
2216 style,
2217 100, 100, 100, 100,
2218 hwnd_F, 0, GetModuleHandle(0), NULL);
2219 trace("hwnd_B %p\n", hwnd_B);
2220 check_z_order(hwnd_F, 0, hwnd_E, 0, FALSE);
2221 check_z_order(hwnd_E, hwnd_F, hwnd_D, 0, FALSE);
2222 check_z_order(hwnd_D, hwnd_E, hwnd_C, 0, FALSE);
2223 check_z_order(hwnd_C, hwnd_D, hwnd_B, hwnd_F, FALSE);
2224 check_z_order(hwnd_B, hwnd_C, 0, hwnd_F, TRUE);
2225
2226 hwnd_A = CreateWindowEx(WS_EX_TOPMOST, "MainWindowClass", NULL,
2227 style,
2228 100, 100, 100, 100,
2229 0, 0, GetModuleHandle(0), NULL);
2230 trace("hwnd_A %p\n", hwnd_A);
2231 check_z_order(hwnd_F, 0, hwnd_E, 0, FALSE);
2232 check_z_order(hwnd_E, hwnd_F, hwnd_D, 0, FALSE);
2233 check_z_order(hwnd_D, hwnd_E, hwnd_C, 0, FALSE);
2234 check_z_order(hwnd_C, hwnd_D, hwnd_B, hwnd_F, FALSE);
2235 check_z_order(hwnd_B, hwnd_C, hwnd_A, hwnd_F, TRUE);
2236 check_z_order(hwnd_A, hwnd_B, 0, 0, TRUE);
2237
2238 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);
2239
2240 /* move hwnd_F and its popups up */
2241 SetWindowPos(hwnd_F, HWND_TOP, 0,0,0,0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE);
2242 check_z_order(hwnd_E, 0, hwnd_D, 0, FALSE);
2243 check_z_order(hwnd_D, hwnd_E, hwnd_F, 0, FALSE);
2244 check_z_order(hwnd_F, hwnd_D, hwnd_C, 0, FALSE);
2245 check_z_order(hwnd_C, hwnd_F, hwnd_B, hwnd_F, FALSE);
2246 check_z_order(hwnd_B, hwnd_C, hwnd_A, hwnd_F, TRUE);
2247 check_z_order(hwnd_A, hwnd_B, 0, 0, TRUE);
2248
2249 /* move hwnd_F and its popups down */
2250 #if 0 /* enable once Wine is fixed to pass this test */
2251 SetWindowPos(hwnd_F, HWND_BOTTOM, 0,0,0,0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE);
2252 check_z_order(hwnd_F, 0, hwnd_C, 0, FALSE);
2253 check_z_order(hwnd_C, hwnd_F, hwnd_B, hwnd_F, FALSE);
2254 check_z_order(hwnd_B, hwnd_C, hwnd_E, hwnd_F, FALSE);
2255 check_z_order(hwnd_E, hwnd_B, hwnd_D, 0, FALSE);
2256 check_z_order(hwnd_D, hwnd_E, hwnd_A, 0, FALSE);
2257 check_z_order(hwnd_A, hwnd_D, 0, 0, TRUE);
2258 #endif
2259
2260 /* make hwnd_C owned by a topmost window */
2261 DestroyWindow( hwnd_C );
2262 hwnd_C = CreateWindowEx(0, "MainWindowClass", NULL,
2263 style,
2264 100, 100, 100, 100,
2265 hwnd_A, 0, GetModuleHandle(0), NULL);
2266 trace("hwnd_C %p\n", hwnd_C);
2267 check_z_order(hwnd_E, 0, hwnd_D, 0, FALSE);
2268 check_z_order(hwnd_D, hwnd_E, hwnd_F, 0, FALSE);
2269 check_z_order(hwnd_F, hwnd_D, hwnd_B, 0, FALSE);
2270 check_z_order(hwnd_B, hwnd_F, hwnd_A, hwnd_F, TRUE);
2271 check_z_order(hwnd_A, hwnd_B, hwnd_C, 0, TRUE);
2272 check_z_order(hwnd_C, hwnd_A, 0, hwnd_A, TRUE);
2273
2274 DestroyWindow(hwnd_A);
2275 DestroyWindow(hwnd_B);
2276 DestroyWindow(hwnd_C);
2277 DestroyWindow(hwnd_F);
2278 }
2279
2280 static void test_vis_rgn( HWND hwnd )
2281 {
2282 RECT win_rect, rgn_rect;
2283 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
2284 HDC hdc;
2285
2286 ShowWindow(hwnd,SW_SHOW);
2287 hdc = GetDC( hwnd );
2288 ok( GetRandomRgn( hdc, hrgn, SYSRGN ) != 0, "GetRandomRgn failed\n" );
2289 GetWindowRect( hwnd, &win_rect );
2290 GetRgnBox( hrgn, &rgn_rect );
2291 if (is_win9x)
2292 {
2293 trace("win9x, mapping to screen coords\n");
2294 MapWindowPoints( hwnd, 0, (POINT *)&rgn_rect, 2 );
2295 }
2296 trace("win: %d,%d-%d,%d\n", win_rect.left, win_rect.top, win_rect.right, win_rect.bottom );
2297 trace("rgn: %d,%d-%d,%d\n", rgn_rect.left, rgn_rect.top, rgn_rect.right, rgn_rect.bottom );
2298 ok( win_rect.left <= rgn_rect.left, "rgn left %d not inside win rect %d\n",
2299 rgn_rect.left, win_rect.left );
2300 ok( win_rect.top <= rgn_rect.top, "rgn top %d not inside win rect %d\n",
2301 rgn_rect.top, win_rect.top );
2302 ok( win_rect.right >= rgn_rect.right, "rgn right %d not inside win rect %d\n",
2303 rgn_rect.right, win_rect.right );
2304 ok( win_rect.bottom >= rgn_rect.bottom, "rgn bottom %d not inside win rect %d\n",
2305 rgn_rect.bottom, win_rect.bottom );
2306 ReleaseDC( hwnd, hdc );
2307 }
2308
2309 static LRESULT WINAPI set_focus_on_activate_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
2310 {
2311 if (msg == WM_ACTIVATE && LOWORD(wp) == WA_ACTIVE)
2312 {
2313 HWND child = GetWindow(hwnd, GW_CHILD);
2314 ok(child != 0, "couldn't find child window\n");
2315 SetFocus(child);
2316 ok(GetFocus() == child, "Focus should be on child %p\n", child);
2317 return 0;
2318 }
2319 return DefWindowProc(hwnd, msg, wp, lp);
2320 }
2321
2322 static void test_SetFocus(HWND hwnd)
2323 {
2324 HWND child, child2;
2325 WNDPROC old_wnd_proc;
2326
2327 /* check if we can set focus to non-visible windows */
2328
2329 ShowWindow(hwnd, SW_SHOW);
2330 SetFocus(0);
2331 SetFocus(hwnd);
2332 ok( GetFocus() == hwnd, "Failed to set focus to visible window %p\n", hwnd );
2333 ok( GetWindowLong(hwnd,GWL_STYLE) & WS_VISIBLE, "Window %p not visible\n", hwnd );
2334 ShowWindow(hwnd, SW_HIDE);
2335 SetFocus(0);
2336 SetFocus(hwnd);
2337 ok( GetFocus() == hwnd, "Failed to set focus to invisible window %p\n", hwnd );
2338 ok( !(GetWindowLong(hwnd,GWL_STYLE) & WS_VISIBLE), "Window %p still visible\n", hwnd );
2339 child = CreateWindowExA(0, "static", NULL, WS_CHILD, 0, 0, 0, 0, hwnd, 0, 0, NULL);
2340 assert(child);
2341 SetFocus(child);
2342 ok( GetFocus() == child, "Failed to set focus to invisible child %p\n", child );
2343 ok( !(GetWindowLong(child,GWL_STYLE) & WS_VISIBLE), "Child %p is visible\n", child );
2344 ShowWindow(child, SW_SHOW);
2345 ok( GetWindowLong(child,GWL_STYLE) & WS_VISIBLE, "Child %p is not visible\n", child );
2346 ok( GetFocus() == child, "Focus no longer on child %p\n", child );
2347 ShowWindow(child, SW_HIDE);
2348 ok( !(GetWindowLong(child,GWL_STYLE) & WS_VISIBLE), "Child %p is visible\n", child );
2349 ok( GetFocus() == hwnd, "Focus should be on parent %p, not %p\n", hwnd, GetFocus() );
2350 ShowWindow(child, SW_SHOW);
2351 child2 = CreateWindowExA(0, "static", NULL, WS_CHILD, 0, 0, 0, 0, child, 0, 0, NULL);
2352 assert(child2);
2353 ShowWindow(child2, SW_SHOW);
2354 SetFocus(child2);
2355 ShowWindow(child, SW_HIDE);
2356 ok( !(GetWindowLong(child,GWL_STYLE) & WS_VISIBLE), "Child %p is visible\n", child );
2357 ok( GetFocus() == child2, "Focus should be on %p, not %p\n", child2, GetFocus() );
2358 ShowWindow(child, SW_SHOW);
2359 SetFocus(child);
2360 ok( GetFocus() == child, "Focus should be on child %p\n", child );
2361 SetWindowPos(child,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_HIDEWINDOW);
2362 ok( GetFocus() == child, "Focus should still be on child %p\n", child );
2363
2364 ShowWindow(child, SW_HIDE);
2365 SetFocus(hwnd);
2366 ok( GetFocus() == hwnd, "Focus should be on parent %p, not %p\n", hwnd, GetFocus() );
2367 SetWindowPos(child,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_SHOWWINDOW);
2368 ok( GetFocus() == hwnd, "Focus should still be on parent %p, not %p\n", hwnd, GetFocus() );
2369 ShowWindow(child, SW_HIDE);
2370 ok( GetFocus() == hwnd, "Focus should still be on parent %p, not %p\n", hwnd, GetFocus() );
2371
2372 ShowWindow(hwnd, SW_SHOW);
2373 ShowWindow(child, SW_SHOW);
2374 SetFocus(child);
2375 ok( GetFocus() == child, "Focus should be on child %p\n", child );
2376 EnableWindow(hwnd, FALSE);
2377 ok( GetFocus() == child, "Focus should still be on child %p\n", child );
2378 EnableWindow(hwnd, TRUE);
2379
2380 ok( GetActiveWindow() == hwnd, "parent window %p should be active\n", hwnd);
2381 ShowWindow(hwnd, SW_SHOWMINIMIZED);
2382 ok( GetActiveWindow() == hwnd, "parent window %p should be active\n", hwnd);
2383 todo_wine
2384 ok( GetFocus() != child, "Focus should not be on child %p\n", child );
2385 ok( GetFocus() != hwnd, "Focus should not be on parent %p\n", hwnd );
2386 ShowWindow(hwnd, SW_RESTORE);
2387 ok( GetActiveWindow() == hwnd, "parent window %p should be active\n", hwnd);
2388 ok( GetFocus() == hwnd, "Focus should be on parent %p\n", hwnd );
2389 ShowWindow(hwnd, SW_SHOWMINIMIZED);
2390 ok( GetActiveWindow() == hwnd, "parent window %p should be active\n", hwnd);
2391 ok( GetFocus() != child, "Focus should not be on child %p\n", child );
2392 todo_wine
2393 ok( GetFocus() != hwnd, "Focus should not be on parent %p\n", hwnd );
2394 old_wnd_proc = (WNDPROC)SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)set_focus_on_activate_proc);
2395 ShowWindow(hwnd, SW_RESTORE);
2396 ok( GetActiveWindow() == hwnd, "parent window %p should be active\n", hwnd);
2397 todo_wine
2398 ok( GetFocus() == child, "Focus should be on child %p, not %p\n", child, GetFocus() );
2399
2400 SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)old_wnd_proc);
2401
2402 DestroyWindow( child2 );
2403 DestroyWindow( child );
2404 }
2405
2406 #define check_wnd_state(a,b,c,d) check_wnd_state_(__FILE__,__LINE__,a,b,c,d)
2407 static void check_wnd_state_(const char *file, int line,
2408 HWND active, HWND foreground, HWND focus, HWND capture)
2409 {
2410 ok_(file, line)(active == GetActiveWindow(), "GetActiveWindow() = %p\n", GetActiveWindow());
2411 /* only check foreground if it belongs to the current thread */
2412 /* foreground can be moved to a different app pretty much at any time */
2413 if (foreground && GetForegroundWindow() &&
2414 GetWindowThreadProcessId(GetForegroundWindow(), NULL) == GetCurrentThreadId())
2415 ok_(file, line)(foreground == GetForegroundWindow(), "GetForegroundWindow() = %p\n", GetForegroundWindow());
2416 ok_(file, line)(focus == GetFocus(), "GetFocus() = %p\n", GetFocus());
2417 ok_(file, line)(capture == GetCapture(), "GetCapture() = %p\n", GetCapture());
2418 }
2419
2420 static void test_SetActiveWindow(HWND hwnd)
2421 {
2422 HWND hwnd2;
2423
2424 flush_events( TRUE );
2425 ShowWindow(hwnd, SW_HIDE);
2426 SetFocus(0);
2427 SetActiveWindow(0);
2428 check_wnd_state(0, 0, 0, 0);
2429
2430 /*trace("testing SetActiveWindow %p\n", hwnd);*/
2431
2432 ShowWindow(hwnd, SW_SHOW);
2433 check_wnd_state(hwnd, hwnd, hwnd, 0);
2434
2435 hwnd2 = SetActiveWindow(0);
2436 ok(hwnd2 == hwnd, "SetActiveWindow returned %p instead of %p\n", hwnd2, hwnd);
2437 if (!GetActiveWindow()) /* doesn't always work on vista */
2438 {
2439 check_wnd_state(0, 0, 0, 0);
2440 hwnd2 = SetActiveWindow(hwnd);
2441 ok(hwnd2 == 0, "SetActiveWindow returned %p instead of 0\n", hwnd2);
2442 }
2443 check_wnd_state(hwnd, hwnd, hwnd, 0);
2444
2445 SetWindowPos(hwnd,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_HIDEWINDOW);
2446 check_wnd_state(hwnd, hwnd, hwnd, 0);
2447
2448 SetWindowPos(hwnd,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_SHOWWINDOW);
2449 check_wnd_state(hwnd, hwnd, hwnd, 0);
2450
2451 ShowWindow(hwnd, SW_HIDE);
2452 check_wnd_state(0, 0, 0, 0);
2453
2454 /*trace("testing SetActiveWindow on an invisible window %p\n", hwnd);*/
2455 SetActiveWindow(hwnd);
2456 check_wnd_state(hwnd, hwnd, hwnd, 0);
2457
2458 ShowWindow(hwnd, SW_SHOW);
2459 check_wnd_state(hwnd, hwnd, hwnd, 0);
2460
2461 hwnd2 = CreateWindowExA(0, "static", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 0, 0, hwnd, 0, 0, NULL);
2462 check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2463
2464 DestroyWindow(hwnd2);
2465 check_wnd_state(hwnd, hwnd, hwnd, 0);
2466
2467 hwnd2 = CreateWindowExA(0, "static", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 0, 0, hwnd, 0, 0, NULL);
2468 check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2469
2470 SetWindowPos(hwnd2,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_HIDEWINDOW);
2471 check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2472
2473 DestroyWindow(hwnd2);
2474 check_wnd_state(hwnd, hwnd, hwnd, 0);
2475 }
2476
2477 struct create_window_thread_params
2478 {
2479 HWND window;
2480 HANDLE window_created;
2481 HANDLE test_finished;
2482 };
2483
2484 static DWORD WINAPI create_window_thread(void *param)
2485 {
2486 struct create_window_thread_params *p = param;
2487 DWORD res;
2488 BOOL ret;
2489
2490 p->window = CreateWindowA("static", NULL, WS_POPUP | WS_VISIBLE, 0, 0, 0, 0, 0, 0, 0, 0);
2491
2492 ret = SetEvent(p->window_created);
2493 ok(ret, "SetEvent failed, last error %#x.\n", GetLastError());
2494
2495 res = WaitForSingleObject(p->test_finished, INFINITE);
2496 ok(res == WAIT_OBJECT_0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
2497
2498 DestroyWindow(p->window);
2499 return 0;
2500 }
2501
2502 static void test_SetForegroundWindow(HWND hwnd)
2503 {
2504 struct create_window_thread_params thread_params;
2505 HANDLE thread;
2506 DWORD res, tid;
2507 BOOL ret;
2508 HWND hwnd2;
2509 MSG msg;
2510
2511 flush_events( TRUE );
2512 ShowWindow(hwnd, SW_HIDE);
2513 SetFocus(0);
2514 SetActiveWindow(0);
2515 check_wnd_state(0, 0, 0, 0);
2516
2517 /*trace("testing SetForegroundWindow %p\n", hwnd);*/
2518
2519 ShowWindow(hwnd, SW_SHOW);
2520 check_wnd_state(hwnd, hwnd, hwnd, 0);
2521
2522 hwnd2 = SetActiveWindow(0);
2523 ok(hwnd2 == hwnd, "SetActiveWindow(0) returned %p instead of %p\n", hwnd2, hwnd);
2524 if (GetActiveWindow() == hwnd) /* doesn't always work on vista */
2525 check_wnd_state(hwnd, hwnd, hwnd, 0);
2526 else
2527 check_wnd_state(0, 0, 0, 0);
2528
2529 ret = SetForegroundWindow(hwnd);
2530 if (!ret)
2531 {
2532 skip( "SetForegroundWindow not working\n" );
2533 return;
2534 }
2535 check_wnd_state(hwnd, hwnd, hwnd, 0);
2536
2537 SetLastError(0xdeadbeef);
2538 ret = SetForegroundWindow(0);
2539 ok(!ret, "SetForegroundWindow returned TRUE instead of FALSE\n");
2540 ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE ||
2541 broken(GetLastError() == 0xdeadbeef), /* win9x */
2542 "got error %d expected ERROR_INVALID_WINDOW_HANDLE\n", GetLastError());
2543 check_wnd_state(hwnd, hwnd, hwnd, 0);
2544
2545 SetWindowPos(hwnd,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_HIDEWINDOW);
2546 check_wnd_state(hwnd, hwnd, hwnd, 0);
2547
2548 SetWindowPos(hwnd,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_SHOWWINDOW);
2549 check_wnd_state(hwnd, hwnd, hwnd, 0);
2550
2551 hwnd2 = GetForegroundWindow();
2552 ok(hwnd2 == hwnd, "Wrong foreground window %p\n", hwnd2);
2553 ok(SetForegroundWindow( GetDesktopWindow() ), "SetForegroundWindow(desktop) error: %d\n", GetLastError());
2554 hwnd2 = GetForegroundWindow();
2555 ok(hwnd2 != hwnd, "Wrong foreground window %p\n", hwnd2);
2556
2557 ShowWindow(hwnd, SW_HIDE);
2558 check_wnd_state(0, 0, 0, 0);
2559
2560 /*trace("testing SetForegroundWindow on an invisible window %p\n", hwnd);*/
2561 ret = SetForegroundWindow(hwnd);
2562 ok(ret || broken(!ret), /* win98 */ "SetForegroundWindow returned FALSE instead of TRUE\n");
2563 check_wnd_state(hwnd, hwnd, hwnd, 0);
2564
2565 ShowWindow(hwnd, SW_SHOW);
2566 check_wnd_state(hwnd, hwnd, hwnd, 0);
2567
2568 hwnd2 = CreateWindowExA(0, "static", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 0, 0, hwnd, 0, 0, NULL);
2569 check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2570
2571 DestroyWindow(hwnd2);
2572 check_wnd_state(hwnd, hwnd, hwnd, 0);
2573
2574 hwnd2 = CreateWindowExA(0, "static", NULL, WS_POPUP|WS_VISIBLE, 0, 0, 0, 0, hwnd, 0, 0, NULL);
2575 check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2576
2577 SetWindowPos(hwnd2,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_HIDEWINDOW);
2578 check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2579
2580 DestroyWindow(hwnd2);
2581 check_wnd_state(hwnd, hwnd, hwnd, 0);
2582
2583 hwnd2 = CreateWindowA("static", NULL, WS_POPUP | WS_VISIBLE, 0, 0, 0, 0, 0, 0, 0, 0);
2584 check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2585
2586 thread_params.window_created = CreateEvent(NULL, FALSE, FALSE, NULL);
2587 ok(!!thread_params.window_created, "CreateEvent failed, last error %#x.\n", GetLastError());
2588 thread_params.test_finished = CreateEvent(NULL, FALSE, FALSE, NULL);
2589 ok(!!thread_params.test_finished, "CreateEvent failed, last error %#x.\n", GetLastError());
2590 thread = CreateThread(NULL, 0, create_window_thread, &thread_params, 0, &tid);
2591 ok(!!thread, "Failed to create thread, last error %#x.\n", GetLastError());
2592 res = WaitForSingleObject(thread_params.window_created, INFINITE);
2593 ok(res == WAIT_OBJECT_0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
2594 check_wnd_state(hwnd2, thread_params.window, hwnd2, 0);
2595
2596 SetForegroundWindow(hwnd2);
2597 check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2598
2599 while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
2600 if (0) check_wnd_state(hwnd2, hwnd2, hwnd2, 0);
2601 todo_wine ok(GetActiveWindow() == hwnd2, "Expected active window %p, got %p.\n", hwnd2, GetActiveWindow());
2602 todo_wine ok(GetFocus() == hwnd2, "Expected focus window %p, got %p.\n", hwnd2, GetFocus());
2603
2604 SetEvent(thread_params.test_finished);
2605 WaitForSingleObject(thread, INFINITE);
2606 CloseHandle(thread_params.test_finished);
2607 CloseHandle(thread_params.window_created);
2608 CloseHandle(thread);
2609 DestroyWindow(hwnd2);
2610 }
2611
2612 static WNDPROC old_button_proc;
2613
2614 static LRESULT WINAPI button_hook_proc(HWND button, UINT msg, WPARAM wparam, LPARAM lparam)
2615 {
2616 LRESULT ret;
2617 USHORT key_state;
2618
2619 key_state = GetKeyState(VK_LBUTTON);
2620 ok(!(key_state & 0x8000), "VK_LBUTTON should not be pressed, state %04x\n", key_state);
2621
2622 ret = CallWindowProcA(old_button_proc, button, msg, wparam, lparam);
2623
2624 if (msg == WM_LBUTTONDOWN)
2625 {
2626 HWND hwnd, capture;
2627
2628 check_wnd_state(button, button, button, button);
2629
2630 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0, 0, 10, 10, 0, 0, 0, NULL);
2631 assert(hwnd);
2632 trace("hwnd %p\n", hwnd);
2633
2634 check_wnd_state(button, button, button, button);
2635
2636 ShowWindow(hwnd, SW_SHOWNOACTIVATE);
2637
2638 check_wnd_state(button, button, button, button);
2639
2640 DestroyWindow(hwnd);
2641
2642 hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP, 0, 0, 10, 10, 0, 0, 0, NULL);
2643 assert(hwnd);
2644 trace("hwnd %p\n", hwnd);
2645
2646 check_wnd_state(button, button, button, button);
2647
2648 /* button wnd proc should release capture on WM_KILLFOCUS if it does
2649 * match internal button state.
2650 */
2651 SendMessage(button, WM_KILLFOCUS, 0, 0);
2652 check_wnd_state(button, button, button, 0);
2653
2654 ShowWindow(hwnd, SW_SHOW);
2655 check_wnd_state(hwnd, hwnd, hwnd, 0);
2656
2657 capture = SetCapture(hwnd);
2658 ok(capture == 0, "SetCapture() = %p\n", capture);
2659
2660 check_wnd_state(hwnd, hwnd, hwnd, hwnd);
2661
2662 DestroyWindow(hwnd);
2663
2664 check_wnd_state(button, 0, button, 0);
2665 }
2666
2667 return ret;
2668 }
2669
2670 static void test_capture_1(void)
2671 {
2672 HWND button, capture, oldFocus, oldActive;
2673
2674 oldFocus = GetFocus();