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