[USER32_WINETEST]
[reactos.git] / rostests / winetests / user32 / dialog.c
1 /* Unit test suite for the dialog functions.
2 *
3 * Copyright 2004 Bill Medland
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 *
19 *
20 *
21 * This test suite currently works by building a quite complex hierarchy of
22 * objects in a variety of styles and then performs a limited number of tests
23 * for the previous and next dialog group or tab items.
24 *
25 * The test specifically does not test all possibilities at this time since
26 * there are several cases where the Windows behaviour is rather strange and
27 * significant work would be required to get the Wine code to duplicate the
28 * strangeness, especially since most are in situations that would not
29 * normally be met.
30 */
31
32 #include <assert.h>
33 #include <stdio.h>
34 #include <stdarg.h>
35
36 #undef WINVER
37 #define WINVER 0x0600 /* For NONCLIENTMETRICS with padding */
38
39 #include "wine/test.h"
40 #include "windef.h"
41 #include "winbase.h"
42 #include "wingdi.h"
43 #include "winuser.h"
44
45 #define MAXHWNDS 1024
46 static HWND hwnd [MAXHWNDS];
47 static unsigned int numwnds=1; /* 0 is reserved for null */
48
49 /* Global handles */
50 static HINSTANCE g_hinst; /* This application's HINSTANCE */
51 static HWND g_hwndMain, g_hwndButton1, g_hwndButton2, g_hwndButtonCancel;
52 static HWND g_hwndTestDlg, g_hwndTestDlgBut1, g_hwndTestDlgBut2, g_hwndTestDlgEdit;
53 static HWND g_hwndInitialFocusT1, g_hwndInitialFocusT2, g_hwndInitialFocusGroupBox;
54
55 static LONG g_styleInitialFocusT1, g_styleInitialFocusT2;
56 static BOOL g_bInitialFocusInitDlgResult, g_bReceivedCommand;
57
58 static int g_terminated;
59
60 typedef struct {
61 INT_PTR id;
62 int parent;
63 DWORD style;
64 DWORD exstyle;
65 } h_entry;
66
67 static const h_entry hierarchy [] = {
68 /* 0 is reserved for the null window */
69 { 1, 0, WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE},
70 { 20, 1, WS_CHILD | WS_VISIBLE | WS_GROUP, 0},
71 { 2, 1, WS_CHILD | WS_VISIBLE, WS_EX_CONTROLPARENT},
72 { 60, 2, WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0},
73 /* What happens with groups when the parent is disabled */
74 { 8, 2, WS_CHILD | WS_VISIBLE | WS_DISABLED | WS_TABSTOP, WS_EX_CONTROLPARENT},
75 { 85, 8, WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_GROUP, 0},
76 { 9, 8, WS_CHILD, WS_EX_CONTROLPARENT},
77 { 86, 9, WS_CHILD | WS_VISIBLE, 0},
78 { 87, 9, WS_CHILD | WS_VISIBLE, 0},
79 { 31, 8, WS_CHILD | WS_VISIBLE | WS_GROUP, 0},
80 { 10, 2, WS_CHILD | WS_VISIBLE, WS_EX_CONTROLPARENT},
81 { 88, 10, WS_CHILD | WS_VISIBLE | WS_GROUP, 0},
82 { 11, 10, WS_CHILD, WS_EX_CONTROLPARENT},
83 { 89, 11, WS_CHILD | WS_VISIBLE, 0},
84 { 32, 11, WS_CHILD | WS_VISIBLE | WS_GROUP, 0},
85 { 90, 11, WS_CHILD | WS_VISIBLE, 0},
86 { 33, 10, WS_CHILD | WS_VISIBLE | WS_GROUP, 0},
87 { 21, 2, WS_CHILD | WS_VISIBLE | WS_GROUP, 0},
88 { 61, 2, WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0},
89 { 3, 1, WS_CHILD | WS_VISIBLE | DS_CONTROL, 0},
90 { 22, 3, WS_CHILD | WS_VISIBLE | WS_GROUP, 0},
91 { 62, 3, WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0},
92 { 7, 3, WS_CHILD | WS_VISIBLE, WS_EX_CONTROLPARENT},
93 { 4, 7, WS_CHILD | WS_VISIBLE | DS_CONTROL, 0},
94 { 83, 4, WS_CHILD | WS_VISIBLE, 0},
95 { 5, 4, WS_CHILD | WS_VISIBLE | DS_CONTROL, 0},
96 /* A couple of controls around the main dialog */
97 { 29, 5, WS_CHILD | WS_VISIBLE | WS_GROUP, 0},
98 { 81, 5, WS_CHILD | WS_VISIBLE, 0},
99 /* The main dialog with lots of controls */
100 { 6, 5, WS_CHILD | WS_VISIBLE, WS_EX_CONTROLPARENT},
101 /* At the start of a dialog */
102 /* Disabled controls are skipped */
103 { 63, 6, WS_CHILD | WS_VISIBLE | WS_DISABLED | WS_TABSTOP, 0},
104 /* Invisible controls are skipped */
105 { 64, 6, WS_CHILD | WS_TABSTOP, 0},
106 /* Invisible disabled controls are skipped */
107 { 65, 6, WS_CHILD | WS_DISABLED | WS_TABSTOP, 0},
108 /* Non-tabstop controls are skipped for tabs but not for groups */
109 { 66, 6, WS_CHILD | WS_VISIBLE, 0},
110 /* End of first group, with no tabstops in it */
111 { 23, 6, WS_CHILD | WS_VISIBLE | WS_GROUP, 0},
112 /* At last a tabstop */
113 { 67, 6, WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0},
114 /* A group that is totally disabled or invisible */
115 { 24, 6, WS_CHILD | WS_DISABLED | WS_GROUP, 0},
116 { 68, 6, WS_CHILD | WS_VISIBLE | WS_DISABLED | WS_TABSTOP, 0},
117 { 69, 6, WS_CHILD | WS_TABSTOP, 0},
118 /* A valid group in the middle of the dialog (not the first nor last group*/
119 { 25, 6, WS_CHILD | WS_VISIBLE | WS_GROUP, 0},
120 /* A non-tabstop item will be skipped for tabs */
121 { 70, 6, WS_CHILD | WS_VISIBLE, 0},
122 /* A disabled item will be skipped for tabs and groups */
123 { 71, 6, WS_CHILD | WS_VISIBLE | WS_DISABLED | WS_TABSTOP, 0},
124 /* A valid item will be found for tabs and groups */
125 { 72, 6, WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0},
126 /* A disabled item to skip when looking for the next group item */
127 { 73, 6, WS_CHILD | WS_VISIBLE | WS_DISABLED | WS_TABSTOP, 0},
128 /* The next group begins with an enabled visible label */
129 { 26, 6, WS_CHILD | WS_VISIBLE | WS_GROUP, 0},
130 { 74, 6, WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0},
131 { 75, 6, WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0},
132 /* That group is terminated by a disabled label */
133 { 27, 6, WS_CHILD | WS_VISIBLE | WS_DISABLED | WS_GROUP, 0},
134 { 76, 6, WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0},
135 { 77, 6, WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0},
136 /* That group is terminated by an invisible label */
137 { 28, 6, WS_CHILD | WS_GROUP, 0},
138 /* The end of the dialog with item for loop and recursion testing */
139 { 78, 6, WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0},
140 /* No tabstop so skipped for prev tab, but found for prev group */
141 { 79, 6, WS_CHILD | WS_VISIBLE, 0},
142 { 80, 6, WS_CHILD | WS_VISIBLE | WS_DISABLED | WS_TABSTOP, 0},
143 /* A couple of controls after the main dialog */
144 { 82, 5, WS_CHILD | WS_VISIBLE, 0},
145 { 30, 5, WS_CHILD | WS_VISIBLE | WS_GROUP, 0},
146 /* And around them */
147 { 84, 4, WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0},
148 {0, 0, 0, 0}
149 };
150
151 static BOOL CreateWindows (HINSTANCE hinst)
152 {
153 const h_entry *p = hierarchy;
154
155 while (p->id != 0)
156 {
157 DWORD style, exstyle;
158 char ctrlname[9];
159
160 /* Basically assert that the hierarchy is valid and track the
161 * maximum control number
162 */
163 if (p->id >= numwnds)
164 {
165 if (p->id >= sizeof(hwnd)/sizeof(hwnd[0]))
166 {
167 trace ("Control %ld is out of range\n", p->id);
168 return FALSE;
169 }
170 else
171 numwnds = p->id+1;
172 }
173 if (p->id <= 0)
174 {
175 trace ("Control %ld is out of range\n", p->id);
176 return FALSE;
177 }
178 if (hwnd[p->id] != 0)
179 {
180 trace ("Control %ld is used more than once\n", p->id);
181 return FALSE;
182 }
183
184 /* Create the control */
185 sprintf (ctrlname, "ctrl%4.4ld", p->id);
186 hwnd[p->id] = CreateWindowEx (p->exstyle, TEXT(p->parent ? "static" : "GetNextDlgItemWindowClass"), TEXT(ctrlname), p->style, 10, 10, 10, 10, hwnd[p->parent], p->parent ? (HMENU) (2000 + p->id) : 0, hinst, 0);
187 if (!hwnd[p->id])
188 {
189 trace ("Failed to create control %ld\n", p->id);
190 return FALSE;
191 }
192
193 /* Check that the styles are as we specified (except the main one
194 * which is quite frequently messed up). If this keeps breaking then
195 * we could mask out the bits that don't concern us.
196 */
197 if (p->parent)
198 {
199 style = GetWindowLong (hwnd[p->id], GWL_STYLE);
200 exstyle = GetWindowLong (hwnd[p->id], GWL_EXSTYLE);
201 if (style != p->style || exstyle != p->exstyle)
202 {
203 trace ("Style mismatch at %ld: %8.8x %8.8x cf %8.8x %8.8x\n", p->id, style, exstyle, p->style, p->exstyle);
204 }
205 }
206 p++;
207 }
208
209 return TRUE;
210 }
211
212 /* Form the lParam of a WM_KEYDOWN message */
213 static DWORD KeyDownData (int repeat, int scancode, int extended, int wasdown)
214 {
215 return ((repeat & 0x0000FFFF) | ((scancode & 0x00FF) << 16) |
216 (extended ? 0x01000000 : 0) | (wasdown ? 0x40000000 : 0));
217 }
218
219 /* Form a WM_KEYDOWN VK_TAB message to the specified window */
220 static void FormTabMsg (MSG *pMsg, HWND hwnd)
221 {
222 pMsg->hwnd = hwnd;
223 pMsg->message = WM_KEYDOWN;
224 pMsg->wParam = VK_TAB;
225 pMsg->lParam = KeyDownData (1, 0x0F, 0, 0);
226 /* pMsg->time is not set. It shouldn't be needed */
227 /* pMsg->pt is ignored */
228 }
229
230 /* Form a WM_KEYDOWN VK_RETURN message to the specified window */
231 static void FormEnterMsg (MSG *pMsg, HWND hwnd)
232 {
233 pMsg->hwnd = hwnd;
234 pMsg->message = WM_KEYDOWN;
235 pMsg->wParam = VK_RETURN;
236 pMsg->lParam = KeyDownData (1, 0x1C, 0, 0);
237 /* pMsg->time is not set. It shouldn't be needed */
238 /* pMsg->pt is ignored */
239 }
240
241 /***********************************************************************
242 *
243 * The actual tests
244 */
245
246 typedef struct
247 {
248 int isok; /* or is it todo */
249 int test;
250 int dlg;
251 int ctl;
252 int tab;
253 int prev;
254 int res;
255 } test_record;
256
257 static int id (HWND h)
258 {
259 unsigned int i;
260 for (i = 0; i < numwnds; i++)
261 if (hwnd[i] == h)
262 return i;
263 return -1;
264 }
265
266 /* Tests
267 *
268 * Tests 1-8 test the hCtl argument of null or the dialog itself.
269 *
270 * 1. Prev Group of null is null
271 * 2. Prev Tab of null is null
272 * 3. Prev Group of hDlg in hDlg is null
273 * 4. Prev Tab of hDlg in hDlg is null
274 * 5. Next Group of null is first visible enabled child
275 * Check it skips invisible, disabled and both.
276 * 6. Next Tab of null is first visible enabled tabstop
277 * Check it skips invisible, disabled, nontabstop, and in combination.
278 * 7. Next Group of hDlg in hDlg is as of null
279 * 8. Next Tab of hDlg in hDlg is as of null
280 *
281 * Tests 9-14 test descent
282 *
283 * 9. DS_CONTROL does not result in descending the hierarchy for Tab Next
284 * 10. DS_CONTROL does not result in descending the hierarchy for Group Next
285 * 11. WS_EX_CONTROLPARENT results in descending the hierarchy for Tab Next
286 * 12. WS_EX_CONTROLPARENT results in descending the hierarchy for Group Next
287 * 13. WS_EX_CONTROLPARENT results in descending the hierarchy for Tab Prev
288 * 14. WS_EX_CONTROLPARENT results in descending the hierarchy for Group Prev
289 *
290 * Tests 15-24 are the basic Prev/Next Group tests
291 *
292 * 15. Next Group of a visible enabled non-group control is the next visible
293 * enabled non-group control, if there is one before the next group
294 * 16. Next Group of a visible enabled non-group control wraps around to the
295 * beginning of the group on finding a control that starts another group.
296 * Note that the group is in the middle of the dialog.
297 * 17. As 16 except note that the next group is started with a disabled
298 * visible control.
299 * 18. As 16 except note that the next group is started with an invisible
300 * enabled control.
301 * 19. Next Group wraps around the controls of the dialog
302 * 20. Next Group is the same even if the initial control is disabled.
303 * 21. Next Group is the same even if the initial control is invisible.
304 * 22. Next Group is the same even if the initial control has the group style
305 * 23. Next Group returns the initial control if there is no visible enabled
306 * control in the group. (Initial control disabled and not group style).
307 * 24. Prev version of test 16.
308 * Prev Group of a visible enabled non-group control wraps around to the
309 * beginning of the group on finding a control that starts the group.
310 * Note that the group is in the middle of the dialog.
311 *
312 * In tests 25 to 28 the control is sitting under dialogs which do not have
313 * the WS_EX_CONTROLPARENT style and so cannot be reached from the top of
314 * the dialog.
315 *
316 * 25. Next Group of an inaccessible control is as if it were accessible
317 * 26. Prev Group of an inaccessible control begins searching at the highest
318 * level ancestor that did not permit recursion down the hierarchy
319 * 27. Next Tab of an inaccessible control is as if it were accessible
320 * 28. Prev Tab of an inaccessible control begins searching at the highest
321 * level ancestor that did not permit recursion down the hierarchy.
322 *
323 * Tests 29- are the basic Tab tests
324 *
325 * 29. Next Tab of a control is the next visible enabled control with the
326 * Tabstop style (N.B. skips disabled, invisible and non-tabstop)
327 * 30. Prev Tab of a control is the previous visible enabled control with the
328 * Tabstop style (N.B. skips disabled, invisible and non-tabstop)
329 * 31. Next Tab test with at least two layers of descent and finding the
330 * result not at the first control.
331 * 32. Next Tab test with at least two layers of descent with the descent and
332 * control at the start of each level.
333 * 33. Prev Tab test with at least two layers of descent and finding the
334 * result not at the last control.
335 * 34. Prev Tab test with at least two layers of descent with the descent and
336 * control at the end of each level.
337 *
338 * 35. Passing NULL may result in the first child being the one returned.
339 * (group test)
340 * 36. Passing NULL may result in the first child being the one returned.
341 * (tab test)
342 */
343
344 static void test_GetNextDlgItem(void)
345 {
346 static test_record test [] =
347 {
348 /* isok test dlg ctl tab prev res */
349
350 { 1, 1, 6, 0, 0, 1, 0},
351 { 1, 2, 6, 0, 1, 1, 0},
352 { 1, 3, 6, 6, 0, 1, 0},
353 { 1, 4, 6, 6, 1, 1, 0},
354 { 1, 5, 6, 0, 0, 0, 66},
355 { 1, 6, 6, 0, 1, 0, 67},
356 { 1, 7, 6, 6, 0, 0, 66},
357 { 1, 8, 6, 6, 1, 0, 67},
358
359 { 1, 9, 4, 83, 1, 0, 84},
360 { 1, 10, 4, 83, 0, 0, 5},
361 { 1, 11, 5, 81, 1, 0, 67},
362 { 1, 12, 5, 81, 0, 0, 66},
363 { 1, 13, 5, 82, 1, 1, 78},
364
365 { 1, 14, 5, 82, 0, 1, 79},
366 { 1, 15, 6, 70, 0, 0, 72},
367 { 1, 16, 6, 72, 0, 0, 25},
368 { 1, 17, 6, 75, 0, 0, 26},
369 { 1, 18, 6, 77, 0, 0, 76},
370 { 1, 19, 6, 79, 0, 0, 66},
371 { 1, 20, 6, 71, 0, 0, 72},
372 { 1, 21, 6, 64, 0, 0, 66},
373
374 { 1, 22, 6, 25, 0, 0, 70},
375 { 1, 23, 6, 68, 0, 0, 68},
376 { 1, 24, 6, 25, 0, 1, 72},
377 { 1, 25, 1, 70, 0, 0, 72},
378 /*{ 0, 26, 1, 70, 0, 1, 3}, Crashes Win95*/
379 { 1, 27, 1, 70, 1, 0, 72},
380 /*{ 0, 28, 1, 70, 1, 1, 61}, Crashes Win95*/
381
382 { 1, 29, 6, 67, 1, 0, 72},
383 { 1, 30, 6, 72, 1, 1, 67},
384
385 { 1, 35, 2, 0, 0, 0, 60},
386 { 1, 36, 2, 0, 1, 0, 60},
387
388 { 0, 0, 0, 0, 0, 0, 0} /* End of test */
389 };
390 const test_record *p = test;
391
392 ok (CreateWindows (g_hinst), "Could not create test windows\n");
393
394 while (p->dlg)
395 {
396 HWND a;
397 a = (p->tab ? GetNextDlgTabItem : GetNextDlgGroupItem) (hwnd[p->dlg], hwnd[p->ctl], p->prev);
398 if (p->isok)
399 {
400 ok (a == hwnd[p->res], "Test %d: %s %s item of %d in %d was %d instead of %d\n", p->test, p->prev ? "Prev" : "Next", p->tab ? "Tab" : "Group", p->ctl, p->dlg, id(a), p->res);
401 }
402 else
403 {
404 todo_wine
405 {
406 ok (a == hwnd[p->res], "Test %d: %s %s item of %d in %d was actually %d matching expected %d\n", p->test, p->prev ? "Prev" : "Next", p->tab ? "Tab" : "Group", p->ctl, p->dlg, id(a), p->res);
407 }
408 }
409 p++;
410 }
411 }
412
413 /*
414 * OnMainWindowCreate
415 */
416 static BOOL OnMainWindowCreate (HWND hwnd, LPCREATESTRUCT lpcs)
417 {
418 g_hwndButton1 = CreateWindow (TEXT("button"), TEXT("Button &1"),
419 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_DEFPUSHBUTTON | BS_TEXT,
420 10, 10, 80, 80, hwnd, (HMENU)100, g_hinst, 0);
421 if (!g_hwndButton1) return FALSE;
422
423 g_hwndButton2 = CreateWindow (TEXT("button"), TEXT("Button &2"),
424 WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_TEXT,
425 110, 10, 80, 80, hwnd, (HMENU)200, g_hinst, 0);
426 if (!g_hwndButton2) return FALSE;
427
428 g_hwndButtonCancel = CreateWindow (TEXT("button"), TEXT("Cancel"),
429 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON | BS_TEXT,
430 210, 10, 80, 80, hwnd, (HMENU)IDCANCEL, g_hinst, 0);
431 if (!g_hwndButtonCancel) return FALSE;
432
433 return TRUE;
434 }
435
436
437 /*
438 * OnTestDlgCreate
439 */
440
441 static BOOL OnTestDlgCreate (HWND hwnd, LPCREATESTRUCT lpcs)
442 {
443 g_hwndTestDlgEdit = CreateWindowEx ( WS_EX_LEFT | WS_EX_LTRREADING |
444 WS_EX_RIGHTSCROLLBAR | WS_EX_NOPARENTNOTIFY | WS_EX_CLIENTEDGE,
445 TEXT("Edit"), TEXT("Edit"),
446 WS_CHILDWINDOW | WS_VISIBLE | WS_TABSTOP | ES_LEFT | ES_AUTOHSCROLL,
447 16,33,184,24, hwnd, (HMENU)101, g_hinst, 0);
448 if (!g_hwndTestDlgEdit) return FALSE;
449
450 g_hwndTestDlgBut1 = CreateWindowEx ( WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR
451 | WS_EX_NOPARENTNOTIFY,
452 TEXT("button"), TEXT("Button &1"),
453 WS_CHILDWINDOW | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON | BS_TEXT,
454 204,33,30,24, hwnd, (HMENU)201, g_hinst, 0);
455 if (!g_hwndTestDlgBut1) return FALSE;
456
457 g_hwndTestDlgBut2 = CreateWindowEx ( WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR
458 | WS_EX_NOPARENTNOTIFY, TEXT("button"),
459 TEXT("Button &2"),
460 WS_CHILDWINDOW | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON | BS_TEXT,
461 90,102,80,24, hwnd, (HMENU)IDCANCEL, g_hinst, 0);
462 if (!g_hwndTestDlgBut2) return FALSE;
463
464 return TRUE;
465 }
466
467 static LRESULT CALLBACK main_window_procA (HWND hwnd, UINT uiMsg, WPARAM wParam,
468 LPARAM lParam)
469 {
470 switch (uiMsg)
471 {
472 /* Add blank case statements for these to ensure we don't use them
473 * by mistake.
474 */
475 case DM_GETDEFID: break;
476 case DM_SETDEFID: break;
477
478 case WM_CREATE:
479 return (OnMainWindowCreate (hwnd,
480 (LPCREATESTRUCTA) lParam) ? 0 : (LRESULT) -1);
481 case WM_COMMAND:
482 if (wParam == IDCANCEL)
483 {
484 g_terminated = TRUE;
485 return 0;
486 }
487 break;
488 }
489
490 return DefWindowProcA (hwnd, uiMsg, wParam, lParam);
491 }
492
493 static LRESULT CALLBACK disabled_test_proc (HWND hwnd, UINT uiMsg,
494 WPARAM wParam, LPARAM lParam)
495 {
496 switch (uiMsg)
497 {
498 case WM_INITDIALOG:
499 {
500 DWORD dw;
501 HWND hwndOk;
502
503 dw = SendMessage(hwnd, DM_GETDEFID, 0, 0);
504 assert(DC_HASDEFID == HIWORD(dw));
505 hwndOk = GetDlgItem(hwnd, LOWORD(dw));
506 assert(hwndOk);
507 EnableWindow(hwndOk, FALSE);
508
509 PostMessage(hwnd, WM_KEYDOWN, VK_RETURN, 0);
510 PostMessage(hwnd, WM_COMMAND, IDCANCEL, 0);
511 break;
512 }
513 case WM_COMMAND:
514 if (wParam == IDOK)
515 {
516 g_terminated = TRUE;
517 EndDialog(hwnd, 0);
518 return 0;
519 }
520 else if (wParam == IDCANCEL)
521 {
522 EndDialog(hwnd, 0);
523 return 0;
524 }
525 break;
526 }
527
528 return DefWindowProcA (hwnd, uiMsg, wParam, lParam);
529 }
530
531 static LRESULT CALLBACK testDlgWinProc (HWND hwnd, UINT uiMsg, WPARAM wParam,
532 LPARAM lParam)
533 {
534 switch (uiMsg)
535 {
536 /* Add blank case statements for these to ensure we don't use them
537 * by mistake.
538 */
539 case DM_GETDEFID: break;
540 case DM_SETDEFID: break;
541
542 case WM_CREATE:
543 return (OnTestDlgCreate (hwnd,
544 (LPCREATESTRUCTA) lParam) ? 0 : (LRESULT) -1);
545 case WM_COMMAND:
546 if(LOWORD(wParam) == 300) g_bReceivedCommand = TRUE;
547 }
548
549 return DefDlgProcA (hwnd, uiMsg, wParam, lParam);
550 }
551
552 static BOOL RegisterWindowClasses (void)
553 {
554 WNDCLASSA cls;
555
556 cls.style = 0;
557 cls.lpfnWndProc = DefWindowProcA;
558 cls.cbClsExtra = 0;
559 cls.cbWndExtra = 0;
560 cls.hInstance = g_hinst;
561 cls.hIcon = NULL;
562 cls.hCursor = LoadCursorA (NULL, IDC_ARROW);
563 cls.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
564 cls.lpszMenuName = NULL;
565 cls.lpszClassName = "GetNextDlgItemWindowClass";
566
567 if (!RegisterClassA (&cls)) return FALSE;
568
569 cls.lpfnWndProc = main_window_procA;
570 cls.lpszClassName = "IsDialogMessageWindowClass";
571
572 if (!RegisterClassA (&cls)) return FALSE;
573
574 GetClassInfoA(0, "#32770", &cls);
575 cls.lpfnWndProc = testDlgWinProc;
576 cls.lpszClassName = "WM_NEXTDLGCTLWndClass";
577 if (!RegisterClassA (&cls)) return FALSE;
578
579 return TRUE;
580 }
581
582 static void test_WM_NEXTDLGCTL(void)
583 {
584 HWND child1, child2, child3;
585 MSG msg;
586 DWORD dwVal;
587
588 g_hwndTestDlg = CreateWindowEx( WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR
589 | WS_EX_DLGMODALFRAME | WS_EX_WINDOWEDGE | WS_EX_CONTROLPARENT | WS_EX_APPWINDOW,
590 "WM_NEXTDLGCTLWndClass",
591 "WM_NEXTDLGCTL Message test window",
592 WS_POPUPWINDOW | WS_CLIPSIBLINGS | WS_DLGFRAME | WS_OVERLAPPED |
593 WS_MINIMIZEBOX | WS_MAXIMIZEBOX | DS_3DLOOK | DS_SETFONT | DS_MODALFRAME,
594 0, 0, 235, 135,
595 NULL, NULL, g_hinst, 0);
596
597 assert (g_hwndTestDlg);
598 assert (g_hwndTestDlgBut1);
599 assert (g_hwndTestDlgBut2);
600 assert (g_hwndTestDlgEdit);
601
602 /*
603 * Test message DM_SETDEFID
604 */
605
606 DefDlgProcA( g_hwndTestDlg, DM_SETDEFID, IDCANCEL, 0 );
607 DefDlgProcA( g_hwndTestDlgBut1, BM_SETSTYLE, BS_DEFPUSHBUTTON, FALSE );
608 dwVal = DefDlgProcA(g_hwndTestDlg, DM_GETDEFID, 0, 0);
609
610 ok ( IDCANCEL == (LOWORD(dwVal)), "Did not set default ID\n" );
611
612 /*
613 * Check whether message WM_NEXTDLGCTL is changing the focus to next control and if
614 * the destination control is a button, style of the button should be changed to
615 * BS_DEFPUSHBUTTON with out making it default.
616 */
617
618 /*
619 * Keep the focus on Edit control.
620 */
621
622 if ( SetFocus( g_hwndTestDlgEdit ) )
623 {
624 ok ((GetFocus() == g_hwndTestDlgEdit), "Focus didn't set on Edit control\n");
625
626 /*
627 * Test message WM_NEXTDLGCTL
628 */
629 DefDlgProcA( g_hwndTestDlg, WM_NEXTDLGCTL, 0, 0 );
630 ok ((GetFocus() == g_hwndTestDlgBut1), "Focus didn't move to first button\n");
631
632 /*
633 * Check whether the default button ID got changed by sending message "WM_NEXTDLGCTL"
634 */
635 dwVal = DefDlgProcA(g_hwndTestDlg, DM_GETDEFID, 0, 0);
636 ok ( IDCANCEL == (LOWORD(dwVal)), "WM_NEXTDLGCTL changed default button\n");
637
638 /*
639 * Check whether the style of the button which got the focus, changed to BS_DEFPUSHBUTTON and
640 * the style of default button changed to BS_PUSHBUTTON.
641 */
642 if ( IDCANCEL == (LOWORD(dwVal)) )
643 {
644 ok ( ((GetWindowLong( g_hwndTestDlgBut1, GWL_STYLE)) & BS_DEFPUSHBUTTON),
645 "Button1 style not set to BS_DEFPUSHBUTTON\n" );
646
647 ok ( !((GetWindowLong( g_hwndTestDlgBut2, GWL_STYLE)) & BS_DEFPUSHBUTTON),
648 "Button2's style not changed to BS_PUSHBUTTON\n" );
649 }
650
651 /*
652 * Move focus to Button2 using "WM_NEXTDLGCTL"
653 */
654 DefDlgProcA( g_hwndTestDlg, WM_NEXTDLGCTL, 0, 0 );
655 ok ((GetFocus() == g_hwndTestDlgBut2), "Focus didn't move to second button\n");
656
657 /*
658 * Check whether the default button ID got changed by sending message "WM_NEXTDLGCTL"
659 */
660 dwVal = DefDlgProcA(g_hwndTestDlg, DM_GETDEFID, 0, 0);
661 ok ( IDCANCEL == (LOWORD(dwVal)), "WM_NEXTDLGCTL changed default button\n");
662
663 /*
664 * Check whether the style of the button which got the focus, changed to BS_DEFPUSHBUTTON and
665 * the style of button which lost the focus changed to BS_PUSHBUTTON.
666 */
667 if ( IDCANCEL == (LOWORD(dwVal)) )
668 {
669 ok ( ((GetWindowLong( g_hwndTestDlgBut2, GWL_STYLE)) & BS_DEFPUSHBUTTON),
670 "Button2 style not set to BS_DEFPUSHBUTTON\n" );
671
672 ok ( !((GetWindowLong( g_hwndTestDlgBut1, GWL_STYLE)) & BS_DEFPUSHBUTTON),
673 "Button1's style not changed to BS_PUSHBUTTON\n" );
674 }
675
676 /*
677 * Move focus to Edit control using "WM_NEXTDLGCTL"
678 */
679 DefDlgProcA( g_hwndTestDlg, WM_NEXTDLGCTL, 0, 0 );
680 ok ((GetFocus() == g_hwndTestDlgEdit), "Focus didn't move to Edit control\n");
681
682 /*
683 * Check whether the default button ID got changed by sending message "WM_NEXTDLGCTL"
684 */
685 dwVal = DefDlgProcA(g_hwndTestDlg, DM_GETDEFID, 0, 0);
686 ok ( IDCANCEL == (LOWORD(dwVal)), "WM_NEXTDLGCTL changed default button\n");
687 }
688
689 /* test nested default buttons */
690
691 child1 = CreateWindowA("button", "child1", WS_VISIBLE|WS_CHILD, 0, 0, 50, 50,
692 g_hwndTestDlg, (HMENU)100, g_hinst, NULL);
693 ok(child1 != NULL, "failed to create first child\n");
694 child2 = CreateWindowA("button", "child2", WS_VISIBLE|WS_CHILD, 60, 60, 30, 30,
695 g_hwndTestDlg, (HMENU)200, g_hinst, NULL);
696 ok(child2 != NULL, "failed to create second child\n");
697 /* create nested child */
698 child3 = CreateWindowA("button", "child3", WS_VISIBLE|WS_CHILD, 10, 10, 10, 10,
699 child1, (HMENU)300, g_hinst, NULL);
700 ok(child3 != NULL, "failed to create subchild\n");
701
702 DefDlgProcA( g_hwndTestDlg, DM_SETDEFID, 200, 0);
703 dwVal = DefDlgProcA( g_hwndTestDlg, DM_GETDEFID, 0, 0);
704 ok(LOWORD(dwVal) == 200, "expected 200, got %x\n", dwVal);
705
706 DefDlgProcA( g_hwndTestDlg, DM_SETDEFID, 300, 0);
707 dwVal = DefDlgProcA( g_hwndTestDlg, DM_GETDEFID, 0, 0);
708 ok(LOWORD(dwVal) == 300, "expected 300, got %x\n", dwVal);
709 ok(SendMessageW( child3, WM_GETDLGCODE, 0, 0) != DLGC_DEFPUSHBUTTON,
710 "expected child3 not to be marked as DLGC_DEFPUSHBUTTON\n");
711
712 g_bReceivedCommand = FALSE;
713 FormEnterMsg (&msg, child3);
714 ok(IsDialogMessage (g_hwndTestDlg, &msg), "Did not handle the ENTER\n");
715 ok(g_bReceivedCommand, "Did not trigger the default Button action\n");
716
717 DestroyWindow(child3);
718 DestroyWindow(child2);
719 DestroyWindow(child1);
720 DestroyWindow(g_hwndTestDlg);
721 }
722
723 static void test_IsDialogMessage(void)
724 {
725 MSG msg;
726
727 g_hwndMain = CreateWindow ("IsDialogMessageWindowClass", "IsDialogMessageWindowClass",
728 WS_OVERLAPPEDWINDOW,
729 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
730 NULL, NULL, g_hinst, 0);
731
732 assert (g_hwndMain);
733 assert (g_hwndButton1);
734 assert (g_hwndButtonCancel);
735
736 /* The focus should initially be nowhere. The first TAB should take it
737 * to the first button. The second TAB should take it to the Cancel
738 * button.
739 */
740 FormTabMsg (&msg, g_hwndMain);
741 ok (IsDialogMessage (g_hwndMain, &msg), "Did not handle first TAB\n");
742 ok ((GetFocus() == g_hwndButton1), "Focus did not move to first button\n");
743 FormTabMsg (&msg, g_hwndButton1);
744 ok (IsDialogMessage (g_hwndMain, &msg), "Did not handle second TAB\n");
745 ok ((GetFocus() == g_hwndButtonCancel),
746 "Focus did not move to cancel button\n");
747 FormEnterMsg (&msg, g_hwndButtonCancel);
748 ok (IsDialogMessage (g_hwndMain, &msg), "Did not handle the ENTER\n");
749 ok (g_terminated, "ENTER did not terminate\n");
750 }
751
752
753 static INT_PTR CALLBACK delayFocusDlgWinProc (HWND hDlg, UINT uiMsg, WPARAM wParam,
754 LPARAM lParam)
755 {
756 switch (uiMsg)
757 {
758 case WM_INITDIALOG:
759 g_hwndMain = hDlg;
760 g_hwndInitialFocusGroupBox = GetDlgItem(hDlg,100);
761 g_hwndButton1 = GetDlgItem(hDlg,200);
762 g_hwndButton2 = GetDlgItem(hDlg,201);
763 g_hwndButtonCancel = GetDlgItem(hDlg,IDCANCEL);
764 g_styleInitialFocusT1 = GetWindowLong(g_hwndInitialFocusGroupBox, GWL_STYLE);
765
766 /* Initially check the second radio button */
767 SendMessage(g_hwndButton1, BM_SETCHECK, BST_UNCHECKED, 0);
768 SendMessage(g_hwndButton2, BM_SETCHECK, BST_CHECKED , 0);
769 /* Continue testing after dialog initialization */
770 PostMessage(hDlg, WM_USER, 0, 0);
771 return g_bInitialFocusInitDlgResult;
772
773 case WM_COMMAND:
774 if (LOWORD(wParam) == IDCANCEL)
775 {
776 EndDialog(hDlg, LOWORD(wParam));
777 return TRUE;
778 }
779 return FALSE;
780
781 case WM_USER:
782 g_styleInitialFocusT2 = GetWindowLong(hDlg, GWL_STYLE);
783 g_hwndInitialFocusT1 = GetFocus();
784 SetFocus(hDlg);
785 g_hwndInitialFocusT2 = GetFocus();
786 PostMessage(hDlg, WM_COMMAND, IDCANCEL, 0);
787 return TRUE;
788 }
789
790 return FALSE;
791 }
792
793 static INT_PTR CALLBACK focusDlgWinProc (HWND hDlg, UINT uiMsg, WPARAM wParam,
794 LPARAM lParam)
795 {
796 switch (uiMsg)
797 {
798 case WM_INITDIALOG:
799 return TRUE;
800
801 case WM_COMMAND:
802 if (LOWORD(wParam) == 200)
803 {
804 if (HIWORD(wParam) == EN_SETFOCUS)
805 g_hwndInitialFocusT1 = (HWND)lParam;
806 }
807 return FALSE;
808 }
809
810 return FALSE;
811 }
812
813 /* Helper for InitialFocusTest */
814 static const char * GetHwndString(HWND hw)
815 {
816 if (hw == NULL)
817 return "a null handle";
818 if (hw == g_hwndMain)
819 return "the dialog handle";
820 if (hw == g_hwndInitialFocusGroupBox)
821 return "the group box control";
822 if (hw == g_hwndButton1)
823 return "the first button";
824 if (hw == g_hwndButton2)
825 return "the second button";
826 if (hw == g_hwndButtonCancel)
827 return "the cancel button";
828
829 return "unknown handle";
830 }
831
832 static void test_initial_focus(void)
833 {
834 /* Test 1:
835 * This test intentionally returns FALSE in response to WM_INITDIALOG
836 * without setting focus to a control. This is what MFC's CFormView does.
837 *
838 * Since the WM_INITDIALOG handler returns FALSE without setting the focus,
839 * the focus should initially be NULL. Later, when we manually set focus to
840 * the dialog, the default handler should set focus to the first control that
841 * is "visible, not disabled, and has the WS_TABSTOP style" (MSDN). Because the
842 * second radio button has been checked, it should be the first control
843 * that meets these criteria and should receive the focus.
844 */
845
846 g_bInitialFocusInitDlgResult = FALSE;
847 g_hwndInitialFocusT1 = (HWND) -1;
848 g_hwndInitialFocusT2 = (HWND) -1;
849 g_styleInitialFocusT1 = -1;
850 g_styleInitialFocusT2 = -1;
851
852 DialogBoxA(g_hinst, "RADIO_TEST_DIALOG", NULL, delayFocusDlgWinProc);
853
854 ok (((g_styleInitialFocusT1 & WS_TABSTOP) == 0),
855 "Error in wrc - Detected WS_TABSTOP as default style for GROUPBOX\n");
856
857 ok (((g_styleInitialFocusT2 & WS_VISIBLE) == 0),
858 "Modal dialogs should not be shown until the message queue first goes empty\n");
859
860 ok ((g_hwndInitialFocusT1 == NULL),
861 "Error in initial focus when WM_INITDIALOG returned FALSE: "
862 "Expected NULL focus, got %s (%p).\n",
863 GetHwndString(g_hwndInitialFocusT1), g_hwndInitialFocusT1);
864
865 ok ((g_hwndInitialFocusT2 == g_hwndButton2),
866 "Error after first SetFocus() when WM_INITDIALOG returned FALSE: "
867 "Expected the second button (%p), got %s (%p).\n",
868 g_hwndButton2, GetHwndString(g_hwndInitialFocusT2),
869 g_hwndInitialFocusT2);
870
871 /* Test 2:
872 * This is the same as above, except WM_INITDIALOG is made to return TRUE.
873 * This should cause the focus to go to the second radio button right away
874 * and stay there (until the user indicates otherwise).
875 */
876
877 g_bInitialFocusInitDlgResult = TRUE;
878 g_hwndInitialFocusT1 = (HWND) -1;
879 g_hwndInitialFocusT2 = (HWND) -1;
880 g_styleInitialFocusT1 = -1;
881 g_styleInitialFocusT2 = -1;
882
883 DialogBoxA(g_hinst, "RADIO_TEST_DIALOG", NULL, delayFocusDlgWinProc);
884
885 ok ((g_hwndInitialFocusT1 == g_hwndButton2),
886 "Error in initial focus when WM_INITDIALOG returned TRUE: "
887 "Expected the second button (%p), got %s (%p).\n",
888 g_hwndButton2, GetHwndString(g_hwndInitialFocusT1),
889 g_hwndInitialFocusT1);
890
891 ok ((g_hwndInitialFocusT2 == g_hwndButton2),
892 "Error after first SetFocus() when WM_INITDIALOG returned TRUE: "
893 "Expected the second button (%p), got %s (%p).\n",
894 g_hwndButton2, GetHwndString(g_hwndInitialFocusT2),
895 g_hwndInitialFocusT2);
896
897 /* Test 3:
898 * If the dialog has DS_CONTROL and it's not visible then we shouldn't change focus */
899 {
900 HWND hDlg;
901 HRSRC hResource;
902 HANDLE hTemplate;
903 DLGTEMPLATE* pTemplate;
904
905 hResource = FindResourceA(g_hinst,"FOCUS_TEST_DIALOG", RT_DIALOG);
906 hTemplate = LoadResource(g_hinst, hResource);
907 pTemplate = LockResource(hTemplate);
908
909 g_hwndInitialFocusT1 = 0;
910 hDlg = CreateDialogIndirectParamA(g_hinst, pTemplate, NULL, focusDlgWinProc, 0);
911 ok (hDlg != 0, "Failed to create test dialog.\n");
912
913 ok ((g_hwndInitialFocusT1 == 0),
914 "Focus should not be set for an invisible DS_CONTROL dialog %p.\n", g_hwndInitialFocusT1);
915
916 DestroyWindow(hDlg);
917 }
918
919 /* Test 4:
920 * If the dialog has no tab-accessible controls, set focus to first control */
921 {
922 HWND hDlg;
923 HRSRC hResource;
924 HANDLE hTemplate;
925 DLGTEMPLATE* pTemplate;
926 HWND hLabel;
927
928 hResource = FindResourceA(g_hinst,"FOCUS_TEST_DIALOG_2", RT_DIALOG);
929 hTemplate = LoadResource(g_hinst, hResource);
930 pTemplate = LockResource(hTemplate);
931
932 hDlg = CreateDialogIndirectParamA(g_hinst, pTemplate, NULL, focusDlgWinProc, 0);
933 g_hwndInitialFocusT1 = GetFocus();
934 hLabel = GetDlgItem(hDlg, 200);
935 ok (hDlg != 0, "Failed to create test dialog.\n");
936
937 ok ((g_hwndInitialFocusT1 == hLabel),
938 "Focus should have been set to the first control, expected (%p) got (%p).\n",
939 hLabel, g_hwndInitialFocusT1);
940
941 DestroyWindow(hDlg);
942 }
943 }
944
945 static void test_GetDlgItemText(void)
946 {
947 char string[64];
948 BOOL ret;
949
950 strcpy(string, "Overwrite Me");
951 ret = GetDlgItemTextA(NULL, 0, string, sizeof(string)/sizeof(string[0]));
952 ok(!ret, "GetDlgItemText(NULL) shouldn't have succeeded\n");
953
954 ok(string[0] == '\0' || broken(!strcmp(string, "Overwrite Me")),
955 "string retrieved using GetDlgItemText should have been NULL terminated\n");
956 }
957
958 static void test_GetDlgItem(void)
959 {
960 HWND hwnd, child1, child2, hwnd2;
961 BOOL ret;
962
963 hwnd = CreateWindowA("button", "parent", WS_VISIBLE, 0, 0, 100, 100, NULL, 0, g_hinst, NULL);
964 ok(hwnd != NULL, "failed to created window\n");
965
966 /* created with the same ID */
967 child1 = CreateWindowA("button", "child1", WS_VISIBLE|WS_CHILD, 0, 0, 10, 10, hwnd, 0, g_hinst, NULL);
968 ok(child1 != NULL, "failed to create first child\n");
969 child2 = CreateWindowA("button", "child2", WS_VISIBLE|WS_CHILD, 0, 0, 10, 10, hwnd, 0, g_hinst, NULL);
970 ok(child2 != NULL, "failed to create second child\n");
971
972 hwnd2 = GetDlgItem(hwnd, 0);
973 ok(hwnd2 == child1, "expected first child, got %p\n", hwnd2);
974
975 hwnd2 = GetTopWindow(hwnd);
976 ok(hwnd2 == child1, "expected first child to be top, got %p\n", hwnd2);
977
978 ret = SetWindowPos(child1, child2, 0, 0, 0, 0, SWP_NOMOVE);
979 ok(ret, "got %d\n", ret);
980 hwnd2 = GetTopWindow(hwnd);
981 ok(hwnd2 == child2, "expected second child to be top, got %p\n", hwnd2);
982
983 /* top window from child list is picked */
984 hwnd2 = GetDlgItem(hwnd, 0);
985 ok(hwnd2 == child2, "expected second child, got %p\n", hwnd2);
986
987 /* Now test how GetDlgItem searches */
988 DestroyWindow(child2);
989 child2 = CreateWindowA("button", "child2", WS_VISIBLE|WS_CHILD, 0, 0, 10, 10, child1, 0, g_hinst, NULL);
990 ok(child2 != NULL, "failed to create second child\n");
991
992 /* give child2 an ID */
993 SetWindowLong(child2, GWLP_ID, 100);
994
995 hwnd2 = GetDlgItem(hwnd, 100);
996 ok(!hwnd2, "expected child to not be found, got %p\n", hwnd2);
997
998 /* make the ID of child2 public with a WS_EX_CONTROLPARENT parent */
999 SetWindowLong(child1, GWL_EXSTYLE, WS_EX_CONTROLPARENT);
1000
1001 hwnd2 = GetDlgItem(hwnd, 100);
1002 ok(!hwnd2, "expected child to not be found, got %p\n", hwnd2);
1003
1004 DestroyWindow(child1);
1005 DestroyWindow(child2);
1006 DestroyWindow(hwnd);
1007 }
1008
1009 static INT_PTR CALLBACK DestroyDlgWinProc (HWND hDlg, UINT uiMsg,
1010 WPARAM wParam, LPARAM lParam)
1011 {
1012 if (uiMsg == WM_INITDIALOG)
1013 {
1014 DestroyWindow(hDlg);
1015 return TRUE;
1016 }
1017 return FALSE;
1018 }
1019
1020 static INT_PTR CALLBACK DestroyOnCloseDlgWinProc (HWND hDlg, UINT uiMsg,
1021 WPARAM wParam, LPARAM lParam)
1022 {
1023 switch (uiMsg)
1024 {
1025 case WM_INITDIALOG:
1026 PostMessage(hDlg, WM_CLOSE, 0, 0);
1027 return TRUE;
1028 case WM_CLOSE:
1029 DestroyWindow(hDlg);
1030 return TRUE;
1031 case WM_DESTROY:
1032 PostQuitMessage(0);
1033 return TRUE;
1034 }
1035 return FALSE;
1036 }
1037
1038 static INT_PTR CALLBACK TestInitDialogHandleProc (HWND hDlg, UINT uiMsg,
1039 WPARAM wParam, LPARAM lParam)
1040 {
1041 if (uiMsg == WM_INITDIALOG)
1042 {
1043 HWND expected = GetNextDlgTabItem(hDlg, NULL, FALSE);
1044 ok(expected == (HWND)wParam,
1045 "Expected wParam to be the handle to the first tabstop control (%p), got %p\n",
1046 expected, (HWND)wParam);
1047
1048 EndDialog(hDlg, LOWORD(SendMessage(hDlg, DM_GETDEFID, 0, 0)));
1049 return TRUE;
1050 }
1051 return FALSE;
1052 }
1053
1054 static INT_PTR CALLBACK TestDefButtonDlgProc (HWND hDlg, UINT uiMsg,
1055 WPARAM wParam, LPARAM lParam)
1056 {
1057 switch (uiMsg)
1058 {
1059 case WM_INITDIALOG:
1060 EndDialog(hDlg, LOWORD(SendMessage(hDlg, DM_GETDEFID, 0, 0)));
1061 return TRUE;
1062 }
1063 return FALSE;
1064 }
1065
1066 static INT_PTR CALLBACK TestReturnKeyDlgProc (HWND hDlg, UINT uiMsg,
1067 WPARAM wParam, LPARAM lParam)
1068 {
1069 static int received_idok;
1070
1071 switch (uiMsg)
1072 {
1073 case WM_INITDIALOG:
1074 {
1075 MSG msg = {hDlg, WM_KEYDOWN, VK_RETURN, 0x011c0001};
1076
1077 received_idok = -1;
1078 IsDialogMessage(hDlg, &msg);
1079 ok(received_idok == 0xdead, "WM_COMMAND/0xdead not received\n");
1080
1081 received_idok = -2;
1082 IsDialogMessage(hDlg, &msg);
1083 ok(received_idok == IDOK, "WM_COMMAND/IDOK not received\n");
1084
1085 EndDialog(hDlg, 0);
1086 return TRUE;
1087 }
1088
1089 case DM_GETDEFID:
1090 if (received_idok == -1)
1091 {
1092 HWND hwnd = GetDlgItem(hDlg, 0xdead);
1093 ok(!hwnd, "dialog item with ID 0xdead should not exist\n");
1094 SetWindowLong(hDlg, DWLP_MSGRESULT, MAKELRESULT(0xdead, DC_HASDEFID));
1095 return TRUE;
1096 }
1097 return FALSE;
1098
1099 case WM_COMMAND:
1100 received_idok = wParam;
1101 return TRUE;
1102 }
1103 return FALSE;
1104 }
1105
1106 static INT_PTR CALLBACK TestControlStyleDlgProc(HWND hdlg, UINT msg,
1107 WPARAM wparam, LPARAM lparam)
1108 {
1109 HWND control;
1110 DWORD style, exstyle;
1111 char buf[256];
1112
1113 switch (msg)
1114 {
1115 case WM_INITDIALOG:
1116 control = GetDlgItem(hdlg, 7);
1117 ok(control != 0, "dialog control with id 7 not found\n");
1118 style = GetWindowLong(control, GWL_STYLE);
1119 ok(style == (WS_CHILD|WS_VISIBLE), "expected WS_CHILD|WS_VISIBLE, got %#x\n", style);
1120 exstyle = GetWindowLong(control, GWL_EXSTYLE);
1121 ok(exstyle == (WS_EX_NOPARENTNOTIFY|WS_EX_TRANSPARENT|WS_EX_CLIENTEDGE), "expected WS_EX_NOPARENTNOTIFY|WS_EX_TRANSPARENT|WS_EX_CLIENTEDGE, got %#x\n", exstyle);
1122 buf[0] = 0;
1123 GetWindowText(control, buf, sizeof(buf));
1124 ok(lstrcmp(buf, "bump7") == 0, "expected bump7, got %s\n", buf);
1125
1126 control = GetDlgItem(hdlg, 8);
1127 ok(control != 0, "dialog control with id 8 not found\n");
1128 style = GetWindowLong(control, GWL_STYLE);
1129 ok(style == (WS_CHILD|WS_VISIBLE), "expected WS_CHILD|WS_VISIBLE, got %#x\n", style);
1130 exstyle = GetWindowLong(control, GWL_EXSTYLE);
1131 ok(exstyle == (WS_EX_NOPARENTNOTIFY|WS_EX_TRANSPARENT), "expected WS_EX_NOPARENTNOTIFY|WS_EX_TRANSPARENT, got %#x\n", exstyle);
1132 buf[0] = 0;
1133 GetWindowText(control, buf, sizeof(buf));
1134 ok(lstrcmp(buf, "bump8") == 0, "expected bump8, got %s\n", buf);
1135
1136 EndDialog(hdlg, -7);
1137 return TRUE;
1138 }
1139 return FALSE;
1140 }
1141
1142 static void test_DialogBoxParamA(void)
1143 {
1144 INT_PTR ret;
1145 HWND hwnd_invalid = (HWND)0x4444;
1146
1147 ret = DialogBoxParamA(GetModuleHandle(0), "TEST_DLG_CHILD_POPUP", 0, TestControlStyleDlgProc, 0);
1148 ok(ret == -7, "expected -7, got %ld\n", ret);
1149
1150 SetLastError(0xdeadbeef);
1151 ret = DialogBoxParamA(GetModuleHandle(NULL), "IDD_DIALOG" , hwnd_invalid, 0 , 0);
1152 ok(0 == ret || broken(ret == -1), "DialogBoxParamA returned %ld, expected 0\n", ret);
1153 ok(ERROR_INVALID_WINDOW_HANDLE == GetLastError() ||
1154 broken(GetLastError() == 0xdeadbeef),
1155 "got %d, expected ERROR_INVALID_WINDOW_HANDLE\n",GetLastError());
1156
1157 /* Test a dialog which destroys itself on WM_INITDIALOG. */
1158 SetLastError(0xdeadbeef);
1159 ret = DialogBoxParamA(GetModuleHandle(NULL), "IDD_DIALOG", 0, DestroyDlgWinProc, 0);
1160 ok(-1 == ret, "DialogBoxParamA returned %ld, expected -1\n", ret);
1161 ok(ERROR_INVALID_WINDOW_HANDLE == GetLastError() ||
1162 GetLastError() == ERROR_SUCCESS ||
1163 broken(GetLastError() == 0xdeadbeef),
1164 "got %d, expected ERROR_INVALID_WINDOW_HANDLE\n",GetLastError());
1165
1166 /* Test a dialog which destroys itself on WM_CLOSE. */
1167 ret = DialogBoxParamA(GetModuleHandle(NULL), "IDD_DIALOG", 0, DestroyOnCloseDlgWinProc, 0);
1168 ok(0 == ret, "DialogBoxParamA returned %ld, expected 0\n", ret);
1169
1170 SetLastError(0xdeadbeef);
1171 ret = DialogBoxParamA(GetModuleHandle(NULL), "RESOURCE_INVALID" , 0, 0, 0);
1172 ok(-1 == ret, "DialogBoxParamA returned %ld, expected -1\n", ret);
1173 ok(ERROR_RESOURCE_NAME_NOT_FOUND == GetLastError() ||
1174 broken(GetLastError() == 0xdeadbeef),
1175 "got %d, expected ERROR_RESOURCE_NAME_NOT_FOUND\n",GetLastError());
1176
1177 SetLastError(0xdeadbeef);
1178 ret = DefDlgProcA(0, WM_ERASEBKGND, 0, 0);
1179 ok(ret == 0, "DefDlgProcA returned %ld, expected 0\n", ret);
1180 ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE ||
1181 broken(GetLastError() == 0xdeadbeef),
1182 "got %d, expected ERROR_INVALID_WINDOW_HANDLE\n", GetLastError());
1183
1184 ret = DialogBoxParamA(GetModuleHandle(NULL), "TEST_EMPTY_DIALOG", 0, TestInitDialogHandleProc, 0);
1185 ok(ret == IDOK, "Expected IDOK\n");
1186
1187 ret = DialogBoxParamA(GetModuleHandle(NULL), "TEST_EMPTY_DIALOG", 0, TestDefButtonDlgProc, 0);
1188 ok(ret == IDOK, "Expected IDOK\n");
1189
1190 DialogBoxParamA(GetModuleHandle(NULL), "TEST_EMPTY_DIALOG", 0, TestReturnKeyDlgProc, 0);
1191 }
1192
1193 static void test_DisabledDialogTest(void)
1194 {
1195 g_terminated = FALSE;
1196 DialogBoxParam(g_hinst, "IDD_DIALOG", NULL, disabled_test_proc, 0);
1197 ok(FALSE == g_terminated, "dialog with disabled ok button has been terminated\n");
1198 }
1199
1200 static INT_PTR CALLBACK messageBoxFontDlgWinProc (HWND hDlg, UINT uiMsg, WPARAM wParam,
1201 LPARAM lParam)
1202 {
1203 return (uiMsg == WM_INITDIALOG);
1204 }
1205
1206 static void test_MessageBoxFontTest(void)
1207 {
1208 /* This dialog template defines a dialog template which got 0x7fff as its
1209 * font size and omits the other font members. On WinNT, passing such a
1210 * dialog template to CreateDialogIndirectParamW will result in a dialog
1211 * being created which uses the message box font. We test that here.
1212 */
1213
1214 static unsigned char dlgTemplate[] =
1215 {
1216 /* Dialog header */
1217 0x01,0x00, /* Version */
1218 0xff,0xff, /* Extended template marker */
1219 0x00,0x00,0x00,0x00, /* Context Help ID */
1220 0x00,0x00,0x00,0x00, /* Extended style */
1221 0xc0,0x00,0xc8,0x80, /* Style (WS_SYSMENU|WS_CAPTION|WS_POPUP|DS_SETFONT|DS_MODALFRAME) */
1222 0x01,0x00, /* Control count */
1223 0x00,0x00, /* X */
1224 0x00,0x00, /* Y */
1225 0x80,0x00, /* Width */
1226 0x80,0x00, /* Height */
1227 0x00,0x00, /* Menu name */
1228 0x00,0x00, /* Class name */
1229 'T',0x00,'e',0x00, /* Caption (unicode) */
1230 's',0x00,'t',0x00,
1231 0x00,0x00,
1232 0xff,0x7f, /* Font height (0x7fff = message box font) */
1233
1234 /* Control #1 */
1235 0x00,0x00, /* Align to DWORD (header is 42 bytes) */
1236 0x00,0x00,0x00,0x00, /* Context Help ID */
1237 0x00,0x00,0x00,0x00, /* Extended style */
1238 0x00,0x00,0x00,0x50, /* Style (WS_CHILD|WS_VISIBLE) */
1239 0x00,0x00, /* X */
1240 0x00,0x00, /* Y */
1241 0x80,0x00, /* Width */
1242 0x80,0x00, /* Height */
1243 0x00,0x01,0x00,0x00, /* Control ID (256) */
1244 0xff,0xff,0x82,0x00, /* Class (Static) */
1245 'W',0x00,'I',0x00, /* Caption (unicode) */
1246 'N',0x00,'E',0x00,
1247 ' ',0x00,'d',0x00,
1248 'i',0x00,'a',0x00,
1249 'l',0x00,'o',0x00,
1250 'g',0x00,' ',0x00,
1251 't',0x00,'e',0x00,
1252 's',0x00,'t',0x00,
1253 '.',0x00,0x00,0x00,
1254 0x00,0x00, /* Size of extended data */
1255
1256 0x00,0x00 /* Align to DWORD */
1257 };
1258
1259 HWND hDlg;
1260 HFONT hFont;
1261 LOGFONTW lfStaticFont;
1262 NONCLIENTMETRICSW ncMetrics;
1263
1264 /* Check if the dialog can be created from the template. On Win9x, this should fail
1265 * because we are calling the W function which is not implemented, but that's what
1266 * we want, because passing such a template to CreateDialogIndirectParamA would crash
1267 * anyway.
1268 */
1269 hDlg = CreateDialogIndirectParamW(g_hinst, (LPCDLGTEMPLATE)dlgTemplate, NULL, messageBoxFontDlgWinProc, 0);
1270 if (!hDlg)
1271 {
1272 win_skip("dialog wasn't created\n");
1273 return;
1274 }
1275
1276 hFont = (HFONT) SendDlgItemMessageW(hDlg, 256, WM_GETFONT, 0, 0);
1277 if (!hFont)
1278 {
1279 skip("dialog uses system font\n");
1280 DestroyWindow(hDlg);
1281 return;
1282 }
1283 GetObjectW(hFont, sizeof(LOGFONTW), &lfStaticFont);
1284
1285 ncMetrics.cbSize = FIELD_OFFSET(NONCLIENTMETRICSW, iPaddedBorderWidth);
1286 SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, 0, &ncMetrics, 0);
1287 ok( !memcmp(&lfStaticFont, &ncMetrics.lfMessageFont, FIELD_OFFSET(LOGFONTW, lfFaceName)) &&
1288 !lstrcmpW(lfStaticFont.lfFaceName, ncMetrics.lfMessageFont.lfFaceName),
1289 "dialog doesn't use message box font\n");
1290 DestroyWindow(hDlg);
1291 }
1292
1293 static void test_SaveRestoreFocus(void)
1294 {
1295 HWND hDlg;
1296 HRSRC hResource;
1297 HANDLE hTemplate;
1298 DLGTEMPLATE* pTemplate;
1299 LONG_PTR foundId;
1300 HWND foundHwnd;
1301
1302 /* create the dialog */
1303 hResource = FindResourceA(g_hinst, "MULTI_EDIT_DIALOG", RT_DIALOG);
1304 hTemplate = LoadResource(g_hinst, hResource);
1305 pTemplate = LockResource(hTemplate);
1306
1307 hDlg = CreateDialogIndirectParamA(g_hinst, pTemplate, NULL, messageBoxFontDlgWinProc, 0);
1308 ok (hDlg != 0, "Failed to create test dialog.\n");
1309
1310 foundId = GetWindowLongPtr(GetFocus(), GWLP_ID);
1311 ok (foundId == 1000, "First edit box should have gained focus on dialog creation. Expected: %d, Found: %ld\n", 1000, foundId);
1312
1313 /* de- then reactivate the dialog */
1314 SendMessage(hDlg, WM_ACTIVATE, MAKEWPARAM(WA_INACTIVE, 0), 0);
1315 SendMessage(hDlg, WM_ACTIVATE, MAKEWPARAM(WA_ACTIVE, 0), 0);
1316
1317 foundId = GetWindowLongPtr(GetFocus(), GWLP_ID);
1318 ok (foundId == 1000, "First edit box should have regained focus after dialog reactivation. Expected: %d, Found: %ld\n", 1000, foundId);
1319
1320 /* select the next tabbable item */
1321 SetFocus(GetNextDlgTabItem(hDlg, GetFocus(), FALSE));
1322
1323 foundId = GetWindowLongPtr(GetFocus(), GWLP_ID);
1324 ok (foundId == 1001, "Second edit box should have gained focus. Expected: %d, Found: %ld\n", 1001, foundId);
1325
1326 /* de- then reactivate the dialog */
1327 SendMessage(hDlg, WM_ACTIVATE, MAKEWPARAM(WA_INACTIVE, 0), 0);
1328 SendMessage(hDlg, WM_ACTIVATE, MAKEWPARAM(WA_ACTIVE, 0), 0);
1329
1330 foundId = GetWindowLongPtr(GetFocus(), GWLP_ID);
1331 ok (foundId == 1001, "Second edit box should have gained focus after dialog reactivation. Expected: %d, Found: %ld\n", 1001, foundId);
1332
1333 /* disable the 2nd box */
1334 EnableWindow(GetFocus(), FALSE);
1335
1336 foundHwnd = GetFocus();
1337 ok (foundHwnd == NULL, "Second edit box should have lost focus after being disabled. Expected: %p, Found: %p\n", NULL, foundHwnd);
1338
1339 /* de- then reactivate the dialog */
1340 SendMessage(hDlg, WM_ACTIVATE, MAKEWPARAM(WA_INACTIVE, 0), 0);
1341 SendMessage(hDlg, WM_ACTIVATE, MAKEWPARAM(WA_ACTIVE, 0), 0);
1342
1343 foundHwnd = GetFocus();
1344 ok (foundHwnd == NULL, "No controls should have gained focus after dialog reactivation. Expected: %p, Found: %p\n", NULL, foundHwnd);
1345
1346 /* clean up */
1347 DestroyWindow(hDlg);
1348 }
1349
1350 static INT_PTR CALLBACK timer_message_dlg_proc(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
1351 {
1352 static int count;
1353 BOOL visible;
1354
1355 switch (msg)
1356 {
1357 case WM_INITDIALOG:
1358 visible = GetWindowLong(wnd, GWL_STYLE) & WS_VISIBLE;
1359 ok(!visible, "Dialog should not be visible.\n");
1360 SetTimer(wnd, 1, 100, NULL);
1361 Sleep(200);
1362 return FALSE;
1363
1364 case WM_COMMAND:
1365 if (LOWORD(wparam) != IDCANCEL) return FALSE;
1366 EndDialog(wnd, LOWORD(wparam));
1367 return TRUE;
1368
1369 case WM_TIMER:
1370 if (wparam != 1) return FALSE;
1371 visible = GetWindowLong(wnd, GWL_STYLE) & WS_VISIBLE;
1372 if (!count++)
1373 {
1374 ok(!visible, "Dialog should not be visible.\n");
1375 PostMessage(wnd, WM_USER, 0, 0);
1376 }
1377 else
1378 {
1379 ok(visible, "Dialog should be visible.\n");
1380 PostMessage(wnd, WM_COMMAND, IDCANCEL, 0);
1381 }
1382 return TRUE;
1383
1384 case WM_USER:
1385 visible = GetWindowLong(wnd, GWL_STYLE) & WS_VISIBLE;
1386 ok(visible, "Dialog should be visible.\n");
1387 return TRUE;
1388
1389 default:
1390 return FALSE;
1391 }
1392 }
1393
1394 static void test_timer_message(void)
1395 {
1396 DialogBoxA(g_hinst, "RADIO_TEST_DIALOG", NULL, timer_message_dlg_proc);
1397 }
1398
1399 START_TEST(dialog)
1400 {
1401 g_hinst = GetModuleHandleA (0);
1402
1403 if (!RegisterWindowClasses()) assert(0);
1404
1405 test_GetNextDlgItem();
1406 test_IsDialogMessage();
1407 test_WM_NEXTDLGCTL();
1408 test_initial_focus();
1409 test_GetDlgItem();
1410 test_GetDlgItemText();
1411 test_DialogBoxParamA();
1412 test_DisabledDialogTest();
1413 test_MessageBoxFontTest();
1414 test_SaveRestoreFocus();
1415 test_timer_message();
1416 }