urlmon: Don't create stgmed_obj for binding to object.
[wine] / dlls / user32 / tests / listbox.c
1 /* Unit test suite for list boxes.
2  *
3  * Copyright 2003 Ferenc Wagner
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 #include <assert.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "winnls.h"
29
30 #include "wine/test.h"
31
32 #ifdef VISIBLE
33 #define WAIT Sleep (1000)
34 #define REDRAW RedrawWindow (handle, NULL, 0, RDW_UPDATENOW)
35 #else
36 #define WAIT
37 #define REDRAW
38 #endif
39
40 static const char * const strings[4] = {
41   "First added",
42   "Second added",
43   "Third added",
44   "Fourth added which is very long because at some time we only had a 256 byte character buffer and that was overflowing in one of those applications that had a common dialog file open box and tried to add a 300 characters long custom filter string which of course the code did not like and crashed. Just make sure this string is longer than 256 characters."
45 };
46
47 static HWND
48 create_listbox (DWORD add_style, HWND parent)
49 {
50   HWND handle;
51   int ctl_id=0;
52   if (parent)
53     ctl_id=1;
54   handle=CreateWindow ("LISTBOX", "TestList",
55                             (LBS_STANDARD & ~LBS_SORT) | add_style,
56                             0, 0, 100, 100,
57                             parent, (HMENU)ctl_id, NULL, 0);
58
59   assert (handle);
60   SendMessage (handle, LB_ADDSTRING, 0, (LPARAM) (LPCTSTR) strings[0]);
61   SendMessage (handle, LB_ADDSTRING, 0, (LPARAM) (LPCTSTR) strings[1]);
62   SendMessage (handle, LB_ADDSTRING, 0, (LPARAM) (LPCTSTR) strings[2]);
63   SendMessage (handle, LB_ADDSTRING, 0, (LPARAM) (LPCTSTR) strings[3]);
64
65 #ifdef VISIBLE
66   ShowWindow (handle, SW_SHOW);
67 #endif
68   REDRAW;
69
70   return handle;
71 }
72
73 struct listbox_prop {
74   DWORD add_style;
75 };
76
77 struct listbox_stat {
78   int selected, anchor, caret, selcount;
79 };
80
81 struct listbox_test {
82   struct listbox_prop prop;
83   struct listbox_stat  init,  init_todo;
84   struct listbox_stat click, click_todo;
85   struct listbox_stat  step,  step_todo;
86   struct listbox_stat   sel,   sel_todo;
87 };
88
89 static void
90 listbox_query (HWND handle, struct listbox_stat *results)
91 {
92   results->selected = SendMessage (handle, LB_GETCURSEL, 0, 0);
93   results->anchor   = SendMessage (handle, LB_GETANCHORINDEX, 0, 0);
94   results->caret    = SendMessage (handle, LB_GETCARETINDEX, 0, 0);
95   results->selcount = SendMessage (handle, LB_GETSELCOUNT, 0, 0);
96 }
97
98 static void
99 buttonpress (HWND handle, WORD x, WORD y)
100 {
101   LPARAM lp=x+(y<<16);
102
103   WAIT;
104   SendMessage (handle, WM_LBUTTONDOWN, (WPARAM) MK_LBUTTON, lp);
105   SendMessage (handle, WM_LBUTTONUP  , (WPARAM) 0         , lp);
106   REDRAW;
107 }
108
109 static void
110 keypress (HWND handle, WPARAM keycode, BYTE scancode, BOOL extended)
111 {
112   LPARAM lp=1+(scancode<<16)+(extended?KEYEVENTF_EXTENDEDKEY:0);
113
114   WAIT;
115   SendMessage (handle, WM_KEYDOWN, keycode, lp);
116   SendMessage (handle, WM_KEYUP  , keycode, lp | 0xc000000);
117   REDRAW;
118 }
119
120 #define listbox_field_ok(t, s, f, got) \
121   ok (t.s.f==got.f, "style %#x, step " #s ", field " #f \
122       ": expected %d, got %d\n", (unsigned int)t.prop.add_style, \
123       t.s.f, got.f)
124
125 #define listbox_todo_field_ok(t, s, f, got) \
126   if (t.s##_todo.f) todo_wine { listbox_field_ok(t, s, f, got); } \
127   else listbox_field_ok(t, s, f, got)
128
129 #define listbox_ok(t, s, got) \
130   listbox_todo_field_ok(t, s, selected, got); \
131   listbox_todo_field_ok(t, s, anchor, got); \
132   listbox_todo_field_ok(t, s, caret, got); \
133   listbox_todo_field_ok(t, s, selcount, got)
134
135 static void
136 check (const struct listbox_test test)
137 {
138   struct listbox_stat answer;
139   HWND hLB=create_listbox (test.prop.add_style, 0);
140   RECT second_item;
141   int i;
142   int res;
143
144   listbox_query (hLB, &answer);
145   listbox_ok (test, init, answer);
146
147   SendMessage (hLB, LB_GETITEMRECT, (WPARAM) 1, (LPARAM) &second_item);
148   buttonpress(hLB, (WORD)second_item.left, (WORD)second_item.top);
149
150   listbox_query (hLB, &answer);
151   listbox_ok (test, click, answer);
152
153   keypress (hLB, VK_DOWN, 0x50, TRUE);
154
155   listbox_query (hLB, &answer);
156   listbox_ok (test, step, answer);
157
158   DestroyWindow (hLB);
159   hLB=create_listbox (test.prop.add_style, 0);
160
161   SendMessage (hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(1, 2));
162   listbox_query (hLB, &answer);
163   listbox_ok (test, sel, answer);
164
165   for (i=0;i<4;i++) {
166         DWORD size = SendMessage (hLB, LB_GETTEXTLEN, i, 0);
167         CHAR *txt;
168         WCHAR *txtw;
169         int resA, resW;
170
171         txt = HeapAlloc (GetProcessHeap(), HEAP_ZERO_MEMORY, size+1);
172         resA=SendMessageA(hLB, LB_GETTEXT, i, (LPARAM)txt);
173         ok(!strcmp (txt, strings[i]), "returned string for item %d does not match %s vs %s\n", i, txt, strings[i]);
174
175         txtw = HeapAlloc (GetProcessHeap(), HEAP_ZERO_MEMORY, 2*size+2);
176         resW=SendMessageW(hLB, LB_GETTEXT, i, (LPARAM)txtw);
177         if (resA != resW) {
178             trace("SendMessageW(LB_GETTEXT) not supported on this platform (resA=%d resW=%d), skipping...\n",
179                 resA, resW);
180         } else {
181             WideCharToMultiByte( CP_ACP, 0, txtw, -1, txt, size, NULL, NULL );
182             ok(!strcmp (txt, strings[i]), "returned string for item %d does not match %s vs %s\n", i, txt, strings[i]);
183         }
184
185         HeapFree (GetProcessHeap(), 0, txtw);
186         HeapFree (GetProcessHeap(), 0, txt);
187   }
188   
189   /* Confirm the count of items, and that an invalid delete does not remove anything */
190   res = SendMessage (hLB, LB_GETCOUNT, 0, 0);
191   ok((res==4), "Expected 4 items, got %d\n", res);
192   res = SendMessage (hLB, LB_DELETESTRING, -1, 0);
193   ok((res==LB_ERR), "Expected LB_ERR items, got %d\n", res);
194   res = SendMessage (hLB, LB_DELETESTRING, 4, 0);
195   ok((res==LB_ERR), "Expected LB_ERR items, got %d\n", res);
196   res = SendMessage (hLB, LB_GETCOUNT, 0, 0);
197   ok((res==4), "Expected 4 items, got %d\n", res);
198
199   WAIT;
200   DestroyWindow (hLB);
201 }
202
203 static void check_item_height(void)
204 {
205     HWND hLB;
206     HDC hdc;
207     HFONT font;
208     TEXTMETRIC tm;
209     INT itemHeight;
210
211     hLB = create_listbox (0, 0);
212     ok ((hdc = GetDCEx( hLB, 0, DCX_CACHE )) != 0, "Can't get hdc\n");
213     ok ((font = GetCurrentObject(hdc, OBJ_FONT)) != 0, "Can't get the current font\n");
214     ok (GetTextMetrics( hdc, &tm ), "Can't read font metrics\n");
215     ReleaseDC( hLB, hdc);
216
217     ok (SendMessage(hLB, WM_SETFONT, (WPARAM)font, 0) == 0, "Can't set font\n");
218
219     itemHeight = SendMessage(hLB, LB_GETITEMHEIGHT, 0, 0);
220     ok (itemHeight == tm.tmHeight, "Item height wrong, got %d, expecting %d\n", itemHeight, tm.tmHeight);
221
222     DestroyWindow (hLB);
223
224     hLB = CreateWindow ("LISTBOX", "TestList", LBS_OWNERDRAWVARIABLE,
225                          0, 0, 100, 100, NULL, NULL, NULL, 0);
226     itemHeight = SendMessage(hLB, LB_GETITEMHEIGHT, 0, 0);
227     ok(itemHeight == tm.tmHeight, "itemHeight %d\n", itemHeight);
228     itemHeight = SendMessage(hLB, LB_GETITEMHEIGHT, 5, 0);
229     ok(itemHeight == tm.tmHeight, "itemHeight %d\n", itemHeight);
230     itemHeight = SendMessage(hLB, LB_GETITEMHEIGHT, -5, 0);
231     ok(itemHeight == tm.tmHeight, "itemHeight %d\n", itemHeight);
232     DestroyWindow (hLB);
233 }
234
235 static LRESULT WINAPI main_window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
236 {
237     switch (msg)
238     {
239     case WM_DRAWITEM:
240     {
241         RECT rc_item, rc_client, rc_clip;
242         DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT *)lparam;
243
244         trace("%p WM_DRAWITEM %08lx %08lx\n", hwnd, wparam, lparam);
245
246         ok(wparam == dis->CtlID, "got wParam=%08lx instead of %08x\n",
247                         wparam, dis->CtlID);
248         ok(dis->CtlType == ODT_LISTBOX, "wrong CtlType %04x\n", dis->CtlType);
249
250         GetClientRect(dis->hwndItem, &rc_client);
251         trace("hwndItem %p client rect (%d,%d-%d,%d)\n", dis->hwndItem,
252                rc_client.left, rc_client.top, rc_client.right, rc_client.bottom);
253         GetClipBox(dis->hDC, &rc_clip);
254         trace("clip rect (%d,%d-%d,%d)\n", rc_clip.left, rc_clip.top, rc_clip.right, rc_clip.bottom);
255         ok(EqualRect(&rc_client, &rc_clip), "client rect of the listbox should be equal to the clip box\n");
256
257         trace("rcItem (%d,%d-%d,%d)\n", dis->rcItem.left, dis->rcItem.top,
258                dis->rcItem.right, dis->rcItem.bottom);
259         SendMessage(dis->hwndItem, LB_GETITEMRECT, dis->itemID, (LPARAM)&rc_item);
260         trace("item rect (%d,%d-%d,%d)\n", rc_item.left, rc_item.top, rc_item.right, rc_item.bottom);
261         ok(EqualRect(&dis->rcItem, &rc_item), "item rects are not equal\n");
262
263         break;
264     }
265
266     default:
267         break;
268     }
269
270     return DefWindowProc(hwnd, msg, wparam, lparam);
271 }
272
273 static void test_ownerdraw(void)
274 {
275     WNDCLASS cls;
276     HWND parent, hLB;
277     INT ret;
278     RECT rc;
279
280     cls.style = 0;
281     cls.lpfnWndProc = main_window_proc;
282     cls.cbClsExtra = 0;
283     cls.cbWndExtra = 0;
284     cls.hInstance = GetModuleHandle(0);
285     cls.hIcon = 0;
286     cls.hCursor = LoadCursor(0, (LPSTR)IDC_ARROW);
287     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
288     cls.lpszMenuName = NULL;
289     cls.lpszClassName = "main_window_class";
290     assert(RegisterClass(&cls));
291
292     parent = CreateWindowEx(0, "main_window_class", NULL,
293                             WS_POPUP | WS_VISIBLE,
294                             100, 100, 400, 400,
295                             GetDesktopWindow(), 0,
296                             GetModuleHandle(0), NULL);
297     assert(parent);
298
299     hLB = create_listbox(LBS_OWNERDRAWFIXED | WS_CHILD | WS_VISIBLE, parent);
300     assert(hLB);
301
302     UpdateWindow(hLB);
303
304     /* make height short enough */
305     SendMessage(hLB, LB_GETITEMRECT, 0, (LPARAM)&rc);
306     SetWindowPos(hLB, 0, 0, 0, 100, rc.bottom - rc.top + 1,
307                  SWP_NOZORDER | SWP_NOMOVE);
308
309     /* make 0 item invisible */
310     SendMessage(hLB, LB_SETTOPINDEX, 1, 0);
311     ret = SendMessage(hLB, LB_GETTOPINDEX, 0, 0);
312     ok(ret == 1, "wrong top index %d\n", ret);
313
314     SendMessage(hLB, LB_GETITEMRECT, 0, (LPARAM)&rc);
315     trace("item 0 rect (%d,%d-%d,%d)\n", rc.left, rc.top, rc.right, rc.bottom);
316     ok(!IsRectEmpty(&rc), "empty item rect\n");
317     ok(rc.top < 0, "rc.top is not negative (%d)\n", rc.top);
318
319     DestroyWindow(hLB);
320     DestroyWindow(parent);
321 }
322
323 #define listbox_test_query(exp, got) \
324   ok(exp.selected == got.selected, "expected selected %d, got %d\n", exp.selected, got.selected); \
325   ok(exp.anchor == got.anchor, "expected anchor %d, got %d\n", exp.anchor, got.anchor); \
326   ok(exp.caret == got.caret, "expected caret %d, got %d\n", exp.caret, got.caret); \
327   ok(exp.selcount == got.selcount, "expected selcount %d, got %d\n", exp.selcount, got.selcount);
328
329 static void test_selection(void)
330 {
331     static const struct listbox_stat test_nosel = { 0, LB_ERR, 0, 0 };
332     static const struct listbox_stat test_1 = { 0, LB_ERR, 0, 2 };
333     static const struct listbox_stat test_2 = { 0, LB_ERR, 0, 3 };
334     static const struct listbox_stat test_3 = { 0, LB_ERR, 0, 4 };
335     HWND hLB;
336     struct listbox_stat answer;
337     INT ret;
338
339     trace("testing LB_SELITEMRANGE\n");
340
341     hLB = create_listbox(LBS_EXTENDEDSEL, 0);
342     assert(hLB);
343
344     listbox_query(hLB, &answer);
345     listbox_test_query(test_nosel, answer);
346
347     ret = SendMessage(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(1, 2));
348     ok(ret == LB_OKAY, "LB_SELITEMRANGE returned %d instead of LB_OKAY\n", ret);
349     listbox_query(hLB, &answer);
350     listbox_test_query(test_1, answer);
351
352     SendMessage(hLB, LB_SETSEL, FALSE, (LPARAM)-1);
353     listbox_query(hLB, &answer);
354     listbox_test_query(test_nosel, answer);
355
356     ret = SendMessage(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(0, 4));
357     ok(ret == LB_OKAY, "LB_SELITEMRANGE returned %d instead of LB_OKAY\n", ret);
358     listbox_query(hLB, &answer);
359     listbox_test_query(test_3, answer);
360
361     SendMessage(hLB, LB_SETSEL, FALSE, (LPARAM)-1);
362     listbox_query(hLB, &answer);
363     listbox_test_query(test_nosel, answer);
364
365     ret = SendMessage(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(-5, 5));
366     ok(ret == LB_OKAY, "LB_SELITEMRANGE returned %d instead of LB_OKAY\n", ret);
367     listbox_query(hLB, &answer);
368     listbox_test_query(test_nosel, answer);
369
370     SendMessage(hLB, LB_SETSEL, FALSE, (LPARAM)-1);
371     listbox_query(hLB, &answer);
372     listbox_test_query(test_nosel, answer);
373
374     ret = SendMessage(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(2, 10));
375     ok(ret == LB_OKAY, "LB_SELITEMRANGE returned %d instead of LB_OKAY\n", ret);
376     listbox_query(hLB, &answer);
377     listbox_test_query(test_1, answer);
378
379     SendMessage(hLB, LB_SETSEL, FALSE, (LPARAM)-1);
380     listbox_query(hLB, &answer);
381     listbox_test_query(test_nosel, answer);
382
383     ret = SendMessage(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(4, 10));
384     ok(ret == LB_OKAY, "LB_SELITEMRANGE returned %d instead of LB_OKAY\n", ret);
385     listbox_query(hLB, &answer);
386     listbox_test_query(test_nosel, answer);
387
388     SendMessage(hLB, LB_SETSEL, FALSE, (LPARAM)-1);
389     listbox_query(hLB, &answer);
390     listbox_test_query(test_nosel, answer);
391
392     ret = SendMessage(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(10, 1));
393     ok(ret == LB_OKAY, "LB_SELITEMRANGE returned %d instead of LB_OKAY\n", ret);
394     listbox_query(hLB, &answer);
395     listbox_test_query(test_2, answer);
396
397     SendMessage(hLB, LB_SETSEL, FALSE, (LPARAM)-1);
398     listbox_query(hLB, &answer);
399     listbox_test_query(test_nosel, answer);
400
401     ret = SendMessage(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(1, -1));
402     ok(ret == LB_OKAY, "LB_SELITEMRANGE returned %d instead of LB_OKAY\n", ret);
403     listbox_query(hLB, &answer);
404     listbox_test_query(test_2, answer);
405
406     DestroyWindow(hLB);
407 }
408
409 static void test_listbox_height(void)
410 {
411     HWND hList;
412     int r, id;
413
414     hList = CreateWindow( "ListBox", "list test", 0, 
415                           1, 1, 600, 100, NULL, NULL, NULL, NULL );
416     ok( hList != NULL, "failed to create listbox\n");
417
418     id = SendMessage( hList, LB_ADDSTRING, 0, (LPARAM) "hi");
419     ok( id == 0, "item id wrong\n");
420
421     r = SendMessage( hList, LB_SETITEMHEIGHT, 0, MAKELPARAM( 20, 0 ));
422     ok( r == 0, "send message failed\n");
423
424     r = SendMessage(hList, LB_GETITEMHEIGHT, 0, 0 );
425     ok( r == 20, "height wrong\n");
426
427     r = SendMessage( hList, LB_SETITEMHEIGHT, 0, MAKELPARAM( 0, 30 ));
428     ok( r == -1, "send message failed\n");
429
430     r = SendMessage(hList, LB_GETITEMHEIGHT, 0, 0 );
431     ok( r == 20, "height wrong\n");
432
433     r = SendMessage( hList, LB_SETITEMHEIGHT, 0, MAKELPARAM( 0x100, 0 ));
434     ok( r == -1, "send message failed\n");
435
436     r = SendMessage(hList, LB_GETITEMHEIGHT, 0, 0 );
437     ok( r == 20, "height wrong\n");
438
439     r = SendMessage( hList, LB_SETITEMHEIGHT, 0, MAKELPARAM( 0xff, 0 ));
440     ok( r == 0, "send message failed\n");
441
442     r = SendMessage(hList, LB_GETITEMHEIGHT, 0, 0 );
443     ok( r == 0xff, "height wrong\n");
444
445     DestroyWindow( hList );
446 }
447
448 static void test_itemfrompoint(void)
449 {
450     /* WS_POPUP is required in order to have a more accurate size calculation (
451        without caption). LBS_NOINTEGRALHEIGHT is required in order to test
452        behavior of partially-displayed item.
453      */
454     HWND hList = CreateWindow( "ListBox", "list test",
455                                WS_VISIBLE|WS_POPUP|LBS_NOINTEGRALHEIGHT,
456                                1, 1, 600, 100, NULL, NULL, NULL, NULL );
457     LONG r, id;
458     RECT rc;
459
460     /* For an empty listbox win2k returns 0x1ffff, win98 returns 0x10000 */
461     r = SendMessage(hList, LB_ITEMFROMPOINT, 0, MAKELPARAM( /* x */ 30, /* y */ 30 ));
462     ok( r == 0x1ffff || r == 0x10000, "ret %x\n", r );
463
464     r = SendMessage(hList, LB_ITEMFROMPOINT, 0, MAKELPARAM( 700, 30 ));
465     ok( r == 0x1ffff || r == 0x10000, "ret %x\n", r );
466
467     r = SendMessage(hList, LB_ITEMFROMPOINT, 0, MAKELPARAM( 30, 300 ));
468     ok( r == 0x1ffff || r == 0x10000, "ret %x\n", r );
469
470     id = SendMessage( hList, LB_ADDSTRING, 0, (LPARAM) "hi");
471     ok( id == 0, "item id wrong\n");
472     id = SendMessage( hList, LB_ADDSTRING, 0, (LPARAM) "hi1");
473     ok( id == 1, "item id wrong\n");
474
475     r = SendMessage(hList, LB_ITEMFROMPOINT, 0, MAKELPARAM( /* x */ 30, /* y */ 30 ));
476     ok( r == 0x1, "ret %x\n", r );
477
478     r = SendMessage(hList, LB_ITEMFROMPOINT, 0, MAKELPARAM( /* x */ 30, /* y */ 601 ));
479     ok( r == 0x10001, "ret %x\n", r );
480
481     /* Resize control so that below assertions about sizes are valid */
482     r = SendMessage( hList, LB_GETITEMRECT, 0, (LPARAM)&rc);
483     ok( r == 1, "ret %x\n", r);
484     r = MoveWindow(hList, 1, 1, 600, (rc.bottom - rc.top + 1) * 9 / 2, TRUE);
485     ok( r != 0, "ret %x\n", r);
486
487     id = SendMessage( hList, LB_ADDSTRING, 0, (LPARAM) "hi2");
488     ok( id == 2, "item id wrong\n");
489     id = SendMessage( hList, LB_ADDSTRING, 0, (LPARAM) "hi3");
490     ok( id == 3, "item id wrong\n");
491     id = SendMessage( hList, LB_ADDSTRING, 0, (LPARAM) "hi4");
492     ok( id == 4, "item id wrong\n");
493     id = SendMessage( hList, LB_ADDSTRING, 0, (LPARAM) "hi5");
494     ok( id == 5, "item id wrong\n");
495     id = SendMessage( hList, LB_ADDSTRING, 0, (LPARAM) "hi6");
496     ok( id == 6, "item id wrong\n");
497     id = SendMessage( hList, LB_ADDSTRING, 0, (LPARAM) "hi7");
498     ok( id == 7, "item id wrong\n");
499
500     /* Set the listbox up so that id 1 is at the top, this leaves 5
501        partially visible at the bottom and 6, 7 are invisible */
502
503     SendMessage( hList, LB_SETTOPINDEX, 1, 0);
504     r = SendMessage( hList, LB_GETTOPINDEX, 0, 0);
505     ok( r == 1, "top %d\n", r);
506
507     r = SendMessage( hList, LB_GETITEMRECT, 5, (LPARAM)&rc);
508     ok( r == 1, "ret %x\n", r);
509     r = SendMessage( hList, LB_GETITEMRECT, 6, (LPARAM)&rc);
510     ok( r == 0, "ret %x\n", r);
511
512     r = SendMessage( hList, LB_ITEMFROMPOINT, 0, MAKELPARAM(/* x */ 10, /* y */ 10) );
513     ok( r == 1, "ret %x\n", r);
514
515     r = SendMessage( hList, LB_ITEMFROMPOINT, 0, MAKELPARAM(1000, 10) );
516     ok( r == 0x10001, "ret %x\n", r );
517
518     r = SendMessage( hList, LB_ITEMFROMPOINT, 0, MAKELPARAM(10, -10) );
519     ok( r == 0x10001, "ret %x\n", r );
520
521     r = SendMessage( hList, LB_ITEMFROMPOINT, 0, MAKELPARAM(10, 100) );
522     ok( r == 0x10005, "item %x\n", r );
523
524     r = SendMessage( hList, LB_ITEMFROMPOINT, 0, MAKELPARAM(10, 200) );
525     ok( r == 0x10005, "item %x\n", r );
526
527     DestroyWindow( hList );
528 }
529
530 static void test_listbox_item_data(void)
531 {
532     HWND hList;
533     int r, id;
534
535     hList = CreateWindow( "ListBox", "list test", 0,
536                           1, 1, 600, 100, NULL, NULL, NULL, NULL );
537     ok( hList != NULL, "failed to create listbox\n");
538
539     id = SendMessage( hList, LB_ADDSTRING, 0, (LPARAM) "hi");
540     ok( id == 0, "item id wrong\n");
541
542     r = SendMessage( hList, LB_SETITEMDATA, 0, MAKELPARAM( 20, 0 ));
543     ok(r == TRUE, "LB_SETITEMDATA returned %d instead of TRUE\n", r);
544
545     r = SendMessage( hList, LB_GETITEMDATA, 0, 0);
546     ok( r == 20, "get item data failed\n");
547
548     DestroyWindow( hList );
549 }
550
551 static void test_listbox_LB_DIR()
552 {
553     HWND hList;
554     int res, itemCount;
555     int itemCount_justFiles;
556     int itemCount_justDrives;
557     int itemCount_allFiles;
558     int i;
559     char pathBuffer[MAX_PATH];
560     char * p;
561     char driveletter;
562
563     /* NOTE: for this test to succeed, there must be no subdirectories
564        under the current directory. In addition, there must be at least
565        one file that fits the wildcard w*.c . Normally, the test
566        directory itself satisfies both conditions.
567      */
568     hList = CreateWindow( "ListBox", "list test", WS_VISIBLE|WS_POPUP,
569                           1, 1, 600, 100, NULL, NULL, NULL, NULL );
570     assert(hList);
571
572     /* Test for standard usage */
573
574     /* This should list all the files in the test directory. */
575     strcpy(pathBuffer, "*");
576     SendMessage(hList, LB_RESETCONTENT, 0, 0);
577     res = SendMessage(hList, LB_DIR, 0, (LPARAM)pathBuffer);
578     ok (res >= 0, "SendMessage(LB_DIR, 0, *) failed - 0x%08x\n", GetLastError());
579
580     /* There should be some content in the listbox */
581     itemCount = SendMessage(hList, LB_GETCOUNT, 0, 0);
582     ok (itemCount > 0, "SendMessage(LB_DIR) did NOT fill the listbox!\n");
583     itemCount_allFiles = itemCount;
584     ok(res + 1 == itemCount,
585         "SendMessage(LB_DIR, 0, *) returned incorrect index (expected %d got %d)!\n",
586         itemCount - 1, res);
587
588     /* This tests behavior when no files match the wildcard */
589     strcpy(pathBuffer, "*.txt");
590     SendMessage(hList, LB_RESETCONTENT, 0, 0);
591     res = SendMessage(hList, LB_DIR, 0, (LPARAM)pathBuffer);
592     ok (res == -1, "SendMessage(LB_DIR, 0, *.txt) returned %d, expected -1\n", res);
593
594     /* There should be NO content in the listbox */
595     itemCount = SendMessage(hList, LB_GETCOUNT, 0, 0);
596     ok (itemCount == 0, "SendMessage(LB_DIR) DID fill the listbox!\n");
597
598
599     /* This should list all the w*.c files in the test directory
600      * As of this writing, this includes win.c, winstation.c, wsprintf.c
601      */
602     strcpy(pathBuffer, "w*.c");
603     SendMessage(hList, LB_RESETCONTENT, 0, 0);
604     res = SendMessage(hList, LB_DIR, 0, (LPARAM)pathBuffer);
605     ok (res >= 0, "SendMessage(LB_DIR, 0, w*.c) failed - 0x%08x\n", GetLastError());
606
607     /* Path specification does NOT converted to uppercase */
608     ok (!strcmp(pathBuffer, "w*.c"),
609         "expected no change to pathBuffer, got %s\n", pathBuffer);
610
611     /* There should be some content in the listbox */
612     itemCount = SendMessage(hList, LB_GETCOUNT, 0, 0);
613     ok (itemCount > 0, "SendMessage(LB_DIR) did NOT fill the listbox!\n");
614     itemCount_justFiles = itemCount;
615     ok(res + 1 == itemCount,
616         "SendMessage(LB_DIR, 0, w*.c) returned incorrect index (expected %d got %d)!\n",
617         itemCount - 1, res);
618
619     /* Every single item in the control should start with a w and end in .c */
620     for (i = 0; i < itemCount; i++) {
621         memset(pathBuffer, 0, MAX_PATH);
622         SendMessage(hList, LB_GETTEXT, i, (LPARAM)pathBuffer);
623         p = pathBuffer + strlen(pathBuffer);
624         ok(((pathBuffer[0] == 'w' || pathBuffer[0] == 'W') &&
625             (*(p-1) == 'c' || *(p-1) == 'C') &&
626             (*(p-2) == '.')), "Element %d (%s) does not fit requested w*.c\n", i, pathBuffer);
627     }
628
629     /* Test DDL_DIRECTORY */
630     strcpy(pathBuffer, "*");
631     SendMessage(hList, LB_RESETCONTENT, 0, 0);
632     res = SendMessage(hList, LB_DIR, DDL_DIRECTORY, (LPARAM)pathBuffer);
633     ok (res > 0, "SendMessage(LB_DIR, DDL_DIRECTORY, *) failed - 0x%08x\n", GetLastError());
634
635     /* There should be some content in the listbox.
636      * All files plus "[..]"
637      */
638     itemCount = SendMessage(hList, LB_GETCOUNT, 0, 0);
639     ok (itemCount == itemCount_allFiles + 1,
640         "SendMessage(LB_DIR, DDL_DIRECTORY, *) filled with %d entries, expected %d\n",
641         itemCount, itemCount_allFiles + 1);
642     ok(res + 1 == itemCount,
643         "SendMessage(LB_DIR, DDL_DIRECTORY, *) returned incorrect index (expected %d got %d)!\n",
644         itemCount - 1, res);
645
646     /* This tests behavior when no files match the wildcard */
647     strcpy(pathBuffer, "*.txt");
648     SendMessage(hList, LB_RESETCONTENT, 0, 0);
649     res = SendMessage(hList, LB_DIR, DDL_DIRECTORY, (LPARAM)pathBuffer);
650     ok (res == -1, "SendMessage(LB_DIR, DDL_DIRECTORY, *.txt) returned %d, expected -1\n", res);
651
652     /* There should be NO content in the listbox */
653     itemCount = SendMessage(hList, LB_GETCOUNT, 0, 0);
654     ok (itemCount == 0, "SendMessage(LB_DIR) DID fill the listbox!\n");
655
656
657     /* Test DDL_DIRECTORY */
658     strcpy(pathBuffer, "w*.c");
659     SendMessage(hList, LB_RESETCONTENT, 0, 0);
660     res = SendMessage(hList, LB_DIR, DDL_DIRECTORY, (LPARAM)pathBuffer);
661     ok (res >= 0, "SendMessage(LB_DIR, DDL_DIRECTORY, w*.c) failed - 0x%08x\n", GetLastError());
662
663     /* There should be some content in the listbox. Since the parent directory does not
664      * fit w*.c, there should be exactly the same number of items as without DDL_DIRECTORY
665      */
666     itemCount = SendMessage(hList, LB_GETCOUNT, 0, 0);
667     ok (itemCount == itemCount_justFiles,
668         "SendMessage(LB_DIR, DDL_DIRECTORY, w*.c) filled with %d entries, expected %d\n",
669         itemCount, itemCount_justFiles);
670     ok(res + 1 == itemCount,
671         "SendMessage(LB_DIR, DDL_DIRECTORY, w*.c) returned incorrect index (expected %d got %d)!\n",
672         itemCount - 1, res);
673
674     /* Every single item in the control should start with a w and end in .c. */
675     for (i = 0; i < itemCount; i++) {
676         memset(pathBuffer, 0, MAX_PATH);
677         SendMessage(hList, LB_GETTEXT, i, (LPARAM)pathBuffer);
678         p = pathBuffer + strlen(pathBuffer);
679         ok(
680             ((pathBuffer[0] == 'w' || pathBuffer[0] == 'W') &&
681             (*(p-1) == 'c' || *(p-1) == 'C') &&
682             (*(p-2) == '.')), "Element %d (%s) does not fit requested w*.c\n", i, pathBuffer);
683     }
684
685
686     /* Test DDL_DRIVES|DDL_EXCLUSIVE */
687     strcpy(pathBuffer, "*");
688     SendMessage(hList, LB_RESETCONTENT, 0, 0);
689     res = SendMessage(hList, LB_DIR, DDL_DRIVES|DDL_EXCLUSIVE, (LPARAM)pathBuffer);
690     ok (res > 0, "SendMessage(LB_DIR, DDL_DRIVES|DDL_EXCLUSIVE, *)  failed - 0x%08x\n", GetLastError());
691
692     /* There should be some content in the listbox. In particular, there should
693      * be at least one element before, since the string "[-c-]" should
694      * have been added. Depending on the user setting, more drives might have
695      * been added.
696      */
697     itemCount = SendMessage(hList, LB_GETCOUNT, 0, 0);
698     ok (itemCount >= 1,
699         "SendMessage(LB_DIR, DDL_DRIVES|DDL_EXCLUSIVE, *) filled with %d entries, expected at least %d\n",
700         itemCount, 1);
701     itemCount_justDrives = itemCount;
702     ok(res + 1 == itemCount, "SendMessage(LB_DIR, DDL_DRIVES|DDL_EXCLUSIVE, *) returned incorrect index!\n");
703
704     /* Every single item in the control should fit the format [-c-] */
705     for (i = 0; i < itemCount; i++) {
706         memset(pathBuffer, 0, MAX_PATH);
707         driveletter = '\0';
708         SendMessage(hList, LB_GETTEXT, i, (LPARAM)pathBuffer);
709         ok( strlen(pathBuffer) == 5, "Length of drive string is not 5\n" );
710         ok( sscanf(pathBuffer, "[-%c-]", &driveletter) == 1, "Element %d (%s) does not fit [-X-]\n", i, pathBuffer);
711         ok( driveletter >= 'a' && driveletter <= 'z', "Drive letter not in range a..z, got ascii %d\n", driveletter);
712         if (!(driveletter >= 'a' && driveletter <= 'z')) {
713             /* Correct after invalid entry is found */
714             trace("removing count of invalid entry %s\n", pathBuffer);
715             itemCount_justDrives--;
716         }
717     }
718
719     /* This tests behavior when no files match the wildcard */
720     strcpy(pathBuffer, "*.txt");
721     SendMessage(hList, LB_RESETCONTENT, 0, 0);
722     res = SendMessage(hList, LB_DIR, DDL_DRIVES|DDL_EXCLUSIVE, (LPARAM)pathBuffer);
723     ok (res == itemCount_justDrives -1, "SendMessage(LB_DIR, DDL_DRIVES|DDL_EXCLUSIVE, *.txt) returned %d, expected %d\n",
724         res, itemCount_justDrives -1);
725
726     itemCount = SendMessage(hList, LB_GETCOUNT, 0, 0);
727     ok (itemCount == itemCount_justDrives, "SendMessage(LB_DIR) returned %d expected %d\n",
728         itemCount, itemCount_justDrives);
729
730     trace("Files with w*.c: %d Mapped drives: %d Directories: 1\n",
731         itemCount_justFiles, itemCount_justDrives);
732
733     /* Test DDL_DRIVES. */
734     strcpy(pathBuffer, "*");
735     SendMessage(hList, LB_RESETCONTENT, 0, 0);
736     res = SendMessage(hList, LB_DIR, DDL_DRIVES, (LPARAM)pathBuffer);
737     ok (res > 0, "SendMessage(LB_DIR, DDL_DRIVES, *)  failed - 0x%08x\n", GetLastError());
738
739     /* There should be some content in the listbox. In particular, there should
740      * be at least one element before, since the string "[-c-]" should
741      * have been added. Depending on the user setting, more drives might have
742      * been added.
743      */
744     itemCount = SendMessage(hList, LB_GETCOUNT, 0, 0);
745     ok (itemCount == itemCount_justDrives + itemCount_allFiles,
746         "SendMessage(LB_DIR, DDL_DRIVES, w*.c) filled with %d entries, expected %d\n",
747         itemCount, itemCount_justDrives + itemCount_allFiles);
748     ok(res + 1 == itemCount, "SendMessage(LB_DIR, DDL_DRIVES, w*.c) returned incorrect index!\n");
749
750     /* This tests behavior when no files match the wildcard */
751     strcpy(pathBuffer, "*.txt");
752     SendMessage(hList, LB_RESETCONTENT, 0, 0);
753     res = SendMessage(hList, LB_DIR, DDL_DRIVES, (LPARAM)pathBuffer);
754     ok (res == itemCount_justDrives -1, "SendMessage(LB_DIR, DDL_DRIVES, *.txt) returned %d, expected %d\n",
755         res, itemCount_justDrives -1);
756
757     itemCount = SendMessage(hList, LB_GETCOUNT, 0, 0);
758     ok (itemCount == res + 1, "SendMessage(LB_DIR) returned %d expected %d\n", itemCount, res + 1);
759
760
761     /* Test DDL_DRIVES. */
762     strcpy(pathBuffer, "w*.c");
763     SendMessage(hList, LB_RESETCONTENT, 0, 0);
764     res = SendMessage(hList, LB_DIR, DDL_DRIVES, (LPARAM)pathBuffer);
765     ok (res > 0, "SendMessage(LB_DIR, DDL_DRIVES, w*.c)  failed - 0x%08x\n", GetLastError());
766
767     /* There should be some content in the listbox. In particular, there should
768      * be at least one element before, since the string "[-c-]" should
769      * have been added. Depending on the user setting, more drives might have
770      * been added.
771      */
772     itemCount = SendMessage(hList, LB_GETCOUNT, 0, 0);
773     ok (itemCount == itemCount_justDrives + itemCount_justFiles,
774         "SendMessage(LB_DIR, DDL_DRIVES, w*.c) filled with %d entries, expected %d\n",
775         itemCount, itemCount_justDrives + itemCount_justFiles);
776     ok(res + 1 == itemCount, "SendMessage(LB_DIR, DDL_DRIVES, w*.c) returned incorrect index!\n");
777
778     /* Every single item in the control should fit the format [-c-], or w*.c */
779     for (i = 0; i < itemCount; i++) {
780         memset(pathBuffer, 0, MAX_PATH);
781         driveletter = '\0';
782         SendMessage(hList, LB_GETTEXT, i, (LPARAM)pathBuffer);
783         p = pathBuffer + strlen(pathBuffer);
784         if (sscanf(pathBuffer, "[-%c-]", &driveletter) == 1) {
785             ok( strlen(pathBuffer) == 5, "Length of drive string is not 5\n" );
786             ok( driveletter >= 'a' && driveletter <= 'z', "Drive letter not in range a..z, got ascii %d\n", driveletter);
787         } else {
788             ok(
789                 ((pathBuffer[0] == 'w' || pathBuffer[0] == 'W') &&
790                 (*(p-1) == 'c' || *(p-1) == 'C') &&
791                 (*(p-2) == '.')), "Element %d (%s) does not fit requested w*.c\n", i, pathBuffer);
792         }
793     }
794
795
796     /* Test DDL_DIRECTORY|DDL_DRIVES. This does *not* imply DDL_EXCLUSIVE */
797     strcpy(pathBuffer, "*");
798     SendMessage(hList, LB_RESETCONTENT, 0, 0);
799     res = SendMessage(hList, LB_DIR, DDL_DIRECTORY|DDL_DRIVES, (LPARAM)pathBuffer);
800     ok (res > 0, "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_DRIVES, *) failed - 0x%08x\n", GetLastError());
801
802     /* There should be some content in the listbox. In particular, there should
803      * be exactly the number of plain files, plus the number of mapped drives.
804      */
805     itemCount = SendMessage(hList, LB_GETCOUNT, 0, 0);
806     ok (itemCount == itemCount_allFiles + itemCount_justDrives + 1,
807         "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_DRIVES) filled with %d entries, expected %d\n",
808         itemCount, itemCount_allFiles + itemCount_justDrives + 1);
809     ok(res + 1 == itemCount, "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_DRIVES, w*.c) returned incorrect index!\n");
810
811     /* Every single item in the control should start with a w and end in .c,
812      * except for the "[..]" string, which should appear exactly as it is,
813      * and the mapped drives in the format "[-X-]".
814      */
815     for (i = 0; i < itemCount; i++) {
816         memset(pathBuffer, 0, MAX_PATH);
817         driveletter = '\0';
818         SendMessage(hList, LB_GETTEXT, i, (LPARAM)pathBuffer);
819         p = pathBuffer + strlen(pathBuffer);
820         if (sscanf(pathBuffer, "[-%c-]", &driveletter) == 1) {
821             ok( driveletter >= 'a' && driveletter <= 'z', "Drive letter not in range a..z, got ascii %d\n", driveletter);
822         }
823     }
824
825     /* This tests behavior when no files match the wildcard */
826     strcpy(pathBuffer, "*.txt");
827     SendMessage(hList, LB_RESETCONTENT, 0, 0);
828     res = SendMessage(hList, LB_DIR, DDL_DIRECTORY|DDL_DRIVES, (LPARAM)pathBuffer);
829     ok (res == itemCount_justDrives -1, "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_DRIVES, *.txt) returned %d, expected %d\n",
830         res, itemCount_justDrives -1);
831
832     itemCount = SendMessage(hList, LB_GETCOUNT, 0, 0);
833     ok (itemCount == res + 1, "SendMessage(LB_DIR) returned %d expected %d\n", itemCount, res + 1);
834
835
836
837     /* Test DDL_DIRECTORY|DDL_DRIVES. */
838     strcpy(pathBuffer, "w*.c");
839     SendMessage(hList, LB_RESETCONTENT, 0, 0);
840     res = SendMessage(hList, LB_DIR, DDL_DIRECTORY|DDL_DRIVES, (LPARAM)pathBuffer);
841     ok (res > 0, "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_DRIVES, w*.c) failed - 0x%08x\n", GetLastError());
842
843     /* There should be some content in the listbox. In particular, there should
844      * be exactly the number of plain files, plus the number of mapped drives.
845      */
846     itemCount = SendMessage(hList, LB_GETCOUNT, 0, 0);
847     ok (itemCount == itemCount_justFiles + itemCount_justDrives,
848         "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_DRIVES) filled with %d entries, expected %d\n",
849         itemCount, itemCount_justFiles + itemCount_justDrives);
850     ok(res + 1 == itemCount, "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_DRIVES, w*.c) returned incorrect index!\n");
851
852     /* Every single item in the control should start with a w and end in .c,
853      * except the mapped drives in the format "[-X-]". The "[..]" directory
854      * should not appear.
855      */
856     for (i = 0; i < itemCount; i++) {
857         memset(pathBuffer, 0, MAX_PATH);
858         driveletter = '\0';
859         SendMessage(hList, LB_GETTEXT, i, (LPARAM)pathBuffer);
860         p = pathBuffer + strlen(pathBuffer);
861         if (sscanf(pathBuffer, "[-%c-]", &driveletter) == 1) {
862             ok( driveletter >= 'a' && driveletter <= 'z', "Drive letter not in range a..z, got ascii %d\n", driveletter);
863         } else {
864             ok(
865                 ((pathBuffer[0] == 'w' || pathBuffer[0] == 'W') &&
866                 (*(p-1) == 'c' || *(p-1) == 'C') &&
867                 (*(p-2) == '.')), "Element %d (%s) does not fit requested w*.c\n", i, pathBuffer);
868         }
869     }
870
871     /* Test DDL_DIRECTORY|DDL_EXCLUSIVE. */
872     strcpy(pathBuffer, "*");
873     SendMessage(hList, LB_RESETCONTENT, 0, 0);
874     res = SendMessage(hList, LB_DIR, DDL_DIRECTORY|DDL_EXCLUSIVE, (LPARAM)pathBuffer);
875     ok (res == 0, "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_EXCLUSIVE, *) failed - 0x%08x\n", GetLastError());
876
877     /* There should be exactly one element: "[..]" */
878     itemCount = SendMessage(hList, LB_GETCOUNT, 0, 0);
879     ok (itemCount == 1,
880         "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_EXCLUSIVE) filled with %d entries, expected %d\n",
881         itemCount, 1);
882     ok(res + 1 == itemCount, "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_EXCLUSIVE, *) returned incorrect index!\n");
883
884     memset(pathBuffer, 0, MAX_PATH);
885     SendMessage(hList, LB_GETTEXT, 0, (LPARAM)pathBuffer);
886     ok( !strcmp(pathBuffer, "[..]"), "First (and only) element is not [..]\n");
887
888     /* This tests behavior when no files match the wildcard */
889     strcpy(pathBuffer, "*.txt");
890     SendMessage(hList, LB_RESETCONTENT, 0, 0);
891     res = SendMessage(hList, LB_DIR, DDL_DIRECTORY|DDL_EXCLUSIVE, (LPARAM)pathBuffer);
892     ok (res == -1, "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_EXCLUSIVE, *.txt) returned %d, expected %d\n",
893         res, -1);
894
895     itemCount = SendMessage(hList, LB_GETCOUNT, 0, 0);
896     ok (itemCount == res + 1, "SendMessage(LB_DIR) returned %d expected %d\n", itemCount, res + 1);
897
898
899     /* Test DDL_DIRECTORY|DDL_EXCLUSIVE. */
900     strcpy(pathBuffer, "w*.c");
901     SendMessage(hList, LB_RESETCONTENT, 0, 0);
902     res = SendMessage(hList, LB_DIR, DDL_DIRECTORY|DDL_EXCLUSIVE, (LPARAM)pathBuffer);
903     ok (res == LB_ERR, "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_EXCLUSIVE, w*.c) returned %d expected %d\n", res, LB_ERR);
904
905     /* There should be no elements, since "[..]" does not fit w*.c */
906     itemCount = SendMessage(hList, LB_GETCOUNT, 0, 0);
907     ok (itemCount == 0,
908         "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_EXCLUSIVE) filled with %d entries, expected %d\n",
909         itemCount, 0);
910
911     /* Test DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE. */
912     strcpy(pathBuffer, "*");
913     SendMessage(hList, LB_RESETCONTENT, 0, 0);
914     res = SendMessage(hList, LB_DIR, DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE, (LPARAM)pathBuffer);
915     ok (res > 0, "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE, w*.c,) failed - 0x%08x\n", GetLastError());
916
917     /* There should be no plain files on the listbox */
918     itemCount = SendMessage(hList, LB_GETCOUNT, 0, 0);
919     ok (itemCount == itemCount_justDrives + 1,
920         "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE) filled with %d entries, expected %d\n",
921         itemCount, itemCount_justDrives + 1);
922     ok(res + 1 == itemCount, "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE, w*.c) returned incorrect index!\n");
923
924     for (i = 0; i < itemCount; i++) {
925         memset(pathBuffer, 0, MAX_PATH);
926         driveletter = '\0';
927         SendMessage(hList, LB_GETTEXT, i, (LPARAM)pathBuffer);
928         p = pathBuffer + strlen(pathBuffer);
929         if (sscanf(pathBuffer, "[-%c-]", &driveletter) == 1) {
930             ok( driveletter >= 'a' && driveletter <= 'z', "Drive letter not in range a..z, got ascii %d\n", driveletter);
931         } else {
932             ok( !strcmp(pathBuffer, "[..]"), "Element %d (%s) does not fit expected [..]\n", i, pathBuffer);
933         }
934     }
935
936     /* This tests behavior when no files match the wildcard */
937     strcpy(pathBuffer, "*.txt");
938     SendMessage(hList, LB_RESETCONTENT, 0, 0);
939     res = SendMessage(hList, LB_DIR, DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE, (LPARAM)pathBuffer);
940     ok (res == itemCount_justDrives -1, "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE, *.txt) returned %d, expected %d\n",
941         res, itemCount_justDrives -1);
942
943     itemCount = SendMessage(hList, LB_GETCOUNT, 0, 0);
944     ok (itemCount == res + 1, "SendMessage(LB_DIR) returned %d expected %d\n", itemCount, res + 1);
945
946     /* Test DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE. */
947     strcpy(pathBuffer, "w*.c");
948     SendMessage(hList, LB_RESETCONTENT, 0, 0);
949     res = SendMessage(hList, LB_DIR, DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE, (LPARAM)pathBuffer);
950     ok (res > 0, "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE, w*.c,) failed - 0x%08x\n", GetLastError());
951
952     /* There should be no plain files on the listbox, and no [..], since it does not fit w*.c */
953     itemCount = SendMessage(hList, LB_GETCOUNT, 0, 0);
954     ok (itemCount == itemCount_justDrives,
955         "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE) filled with %d entries, expected %d\n",
956         itemCount, itemCount_justDrives);
957     ok(res + 1 == itemCount, "SendMessage(LB_DIR, DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE, w*.c) returned incorrect index!\n");
958
959     for (i = 0; i < itemCount; i++) {
960         memset(pathBuffer, 0, MAX_PATH);
961         driveletter = '\0';
962         SendMessage(hList, LB_GETTEXT, i, (LPARAM)pathBuffer);
963         p = pathBuffer + strlen(pathBuffer);
964         ok (sscanf(pathBuffer, "[-%c-]", &driveletter) == 1, "Element %d (%s) does not fit [-X-]\n", i, pathBuffer);
965         ok( driveletter >= 'a' && driveletter <= 'z', "Drive letter not in range a..z, got ascii %d\n", driveletter);
966     }
967     DestroyWindow(hList);
968 }
969
970 HWND g_listBox;
971 HWND g_label;
972
973 #define ID_TEST_LABEL    1001
974 #define ID_TEST_LISTBOX  1002
975
976 static BOOL on_listbox_container_create (HWND hwnd, LPCREATESTRUCT lpcs)
977 {
978     g_label = CreateWindow(
979         "Static",
980         "Contents of static control before DlgDirList.",
981         WS_CHILD | WS_VISIBLE,
982         10, 10, 512, 32,
983         hwnd, (HMENU)ID_TEST_LABEL, NULL, 0);
984     if (!g_label) return FALSE;
985     g_listBox = CreateWindow(
986         "ListBox",
987         "DlgDirList test",
988         WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER | WS_VSCROLL,
989         10, 60, 256, 256,
990         hwnd, (HMENU)ID_TEST_LISTBOX, NULL, 0);
991     if (!g_listBox) return FALSE;
992
993     return TRUE;
994 }
995
996 static LRESULT CALLBACK listbox_container_window_procA (
997     HWND hwnd, UINT uiMsg, WPARAM wParam, LPARAM lParam)
998 {
999     LRESULT result = 0;
1000
1001     switch (uiMsg) {
1002     case WM_DESTROY:
1003         PostQuitMessage(0);
1004         break;
1005     case WM_CREATE:
1006         result = on_listbox_container_create(hwnd, (LPCREATESTRUCTA) lParam)
1007             ? 0 : (LRESULT)-1;
1008         break;
1009     default:
1010         result = DefWindowProcA (hwnd, uiMsg, wParam, lParam);
1011         break;
1012     }
1013     return result;
1014 }
1015
1016 static BOOL RegisterListboxWindowClass(HINSTANCE hInst)
1017 {
1018     WNDCLASSA cls;
1019
1020     cls.style = 0;
1021     cls.cbClsExtra = 0;
1022     cls.cbWndExtra = 0;
1023     cls.hInstance = hInst;
1024     cls.hIcon = NULL;
1025     cls.hCursor = LoadCursorA (NULL, IDC_ARROW);
1026     cls.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
1027     cls.lpszMenuName = NULL;
1028     cls.lpfnWndProc = listbox_container_window_procA;
1029     cls.lpszClassName = "ListboxContainerClass";
1030     if (!RegisterClassA (&cls)) return FALSE;
1031
1032     return TRUE;
1033 }
1034
1035 static void test_listbox_dlgdir(void)
1036 {
1037     HINSTANCE hInst;
1038     HWND hWnd;
1039     int res, itemCount;
1040     int itemCount_justFiles;
1041     int itemCount_justDrives;
1042     int i;
1043     char pathBuffer[MAX_PATH];
1044     char itemBuffer[MAX_PATH];
1045     char tempBuffer[MAX_PATH];
1046     char * p;
1047     char driveletter;
1048
1049     /* NOTE: for this test to succeed, there must be no subdirectories
1050        under the current directory. In addition, there must be at least
1051        one file that fits the wildcard w*.c . Normally, the test
1052        directory itself satisfies both conditions.
1053      */
1054
1055     hInst = GetModuleHandleA(0);
1056     if (!RegisterListboxWindowClass(hInst)) assert(0);
1057     hWnd = CreateWindow("ListboxContainerClass", "ListboxContainerClass",
1058                     WS_OVERLAPPEDWINDOW | WS_VISIBLE,
1059             CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
1060             NULL, NULL, hInst, 0);
1061     assert(hWnd);
1062
1063     /* Test for standard usage */
1064
1065     /* The following should be overwritten by the directory path */
1066     SendMessage(g_label, WM_SETTEXT, 0, (LPARAM)"default contents");
1067
1068     /* This should list all the w*.c files in the test directory
1069      * As of this writing, this includes win.c, winstation.c, wsprintf.c
1070      */
1071     strcpy(pathBuffer, "w*.c");
1072     res = DlgDirList(hWnd, pathBuffer, ID_TEST_LISTBOX, ID_TEST_LABEL, 0);
1073     ok (res == 1, "DlgDirList(*.c, 0) returned %d - expected 1 - 0x%08x\n", res, GetLastError());
1074
1075     /* Path specification gets converted to uppercase */
1076     ok (!strcmp(pathBuffer, "W*.C"),
1077         "expected conversion to uppercase, got %s\n", pathBuffer);
1078
1079     /* Loaded path should have overwritten the label text */
1080     SendMessage(g_label, WM_GETTEXT, (WPARAM)MAX_PATH, (LPARAM)pathBuffer);
1081     trace("Static control after DlgDirList: %s\n", pathBuffer);
1082     ok (strcmp("default contents", pathBuffer), "DlgDirList() did not modify static control!\n");
1083
1084     /* There should be some content in the listbox */
1085     itemCount = SendMessage(g_listBox, LB_GETCOUNT, 0, 0);
1086     ok (itemCount > 0, "DlgDirList() did NOT fill the listbox!\n");
1087     itemCount_justFiles = itemCount;
1088
1089     /* Every single item in the control should start with a w and end in .c */
1090     for (i = 0; i < itemCount; i++) {
1091         memset(pathBuffer, 0, MAX_PATH);
1092         SendMessage(g_listBox, LB_GETTEXT, i, (LPARAM)pathBuffer);
1093         p = pathBuffer + strlen(pathBuffer);
1094         ok(((pathBuffer[0] == 'w' || pathBuffer[0] == 'W') &&
1095             (*(p-1) == 'c' || *(p-1) == 'C') &&
1096             (*(p-2) == '.')), "Element %d (%s) does not fit requested w*.c\n", i, pathBuffer);
1097     }
1098
1099     /* Test behavior when no files match the wildcard */
1100     strcpy(pathBuffer, "*.txt");
1101     res = DlgDirList(hWnd, pathBuffer, ID_TEST_LISTBOX, ID_TEST_LABEL, 0);
1102     ok (res == 1, "DlgDirList(*.txt, 0) returned %d expected 1\n", res);
1103
1104     itemCount = SendMessage(g_listBox, LB_GETCOUNT, 0, 0);
1105     ok (itemCount == 0, "DlgDirList() DID fill the listbox!\n");
1106
1107     /* Test DDL_DIRECTORY */
1108     strcpy(pathBuffer, "w*.c");
1109     res = DlgDirList(hWnd, pathBuffer, ID_TEST_LISTBOX, ID_TEST_LABEL,
1110         DDL_DIRECTORY);
1111     ok (res == 1, "DlgDirList(*.c, DDL_DIRECTORY) failed - 0x%08x\n", GetLastError());
1112
1113     /* There should be some content in the listbox. In particular, there should
1114      * be exactly one more element than before, since the string "[..]" should
1115      * have been added.
1116      */
1117     itemCount = SendMessage(g_listBox, LB_GETCOUNT, 0, 0);
1118     ok (itemCount == itemCount_justFiles + 1,
1119         "DlgDirList(DDL_DIRECTORY) filled with %d entries, expected %d\n",
1120         itemCount, itemCount_justFiles + 1);
1121
1122     /* Every single item in the control should start with a w and end in .c,
1123      * except for the "[..]" string, which should appear exactly as it is.
1124      */
1125     for (i = 0; i < itemCount; i++) {
1126         memset(pathBuffer, 0, MAX_PATH);
1127         SendMessage(g_listBox, LB_GETTEXT, i, (LPARAM)pathBuffer);
1128         p = pathBuffer + strlen(pathBuffer);
1129         ok( !strcmp(pathBuffer, "[..]") ||
1130             ((pathBuffer[0] == 'w' || pathBuffer[0] == 'W') &&
1131             (*(p-1) == 'c' || *(p-1) == 'C') &&
1132             (*(p-2) == '.')), "Element %d (%s) does not fit requested w*.c\n", i, pathBuffer);
1133     }
1134
1135     /* Test behavior when no files match the wildcard */
1136     strcpy(pathBuffer, "*.txt");
1137     res = DlgDirList(hWnd, pathBuffer, ID_TEST_LISTBOX, ID_TEST_LABEL,
1138         DDL_DIRECTORY);
1139     ok (res == 1, "DlgDirList(*.txt, DDL_DIRECTORY) returned %d expected 1\n", res);
1140
1141     itemCount = SendMessage(g_listBox, LB_GETCOUNT, 0, 0);
1142     ok (itemCount == 1, "DlgDirList() incorrectly filled the listbox! (expected 1 got %d)\n",
1143         itemCount);
1144     for (i = 0; i < itemCount; i++) {
1145         memset(pathBuffer, 0, MAX_PATH);
1146         SendMessage(g_listBox, LB_GETTEXT, i, (LPARAM)pathBuffer);
1147         p = pathBuffer + strlen(pathBuffer);
1148         ok( !strcmp(pathBuffer, "[..]"), "Element %d (%s) does not fit requested [..]\n", i, pathBuffer);
1149     }
1150
1151
1152     /* Test DDL_DRIVES. At least on WinXP-SP2, this implies DDL_EXCLUSIVE */
1153     strcpy(pathBuffer, "w*.c");
1154     res = DlgDirList(hWnd, pathBuffer, ID_TEST_LISTBOX, ID_TEST_LABEL,
1155         DDL_DRIVES);
1156     ok (res == 1, "DlgDirList(*.c, DDL_DRIVES) failed - 0x%08x\n", GetLastError());
1157
1158     /* There should be some content in the listbox. In particular, there should
1159      * be at least one element before, since the string "[-c-]" should
1160      * have been added. Depending on the user setting, more drives might have
1161      * been added.
1162      */
1163     itemCount = SendMessage(g_listBox, LB_GETCOUNT, 0, 0);
1164     ok (itemCount >= 1,
1165         "DlgDirList(DDL_DRIVES) filled with %d entries, expected at least %d\n",
1166         itemCount, 1);
1167     itemCount_justDrives = itemCount;
1168
1169     /* Every single item in the control should fit the format [-c-] */
1170     for (i = 0; i < itemCount; i++) {
1171         memset(pathBuffer, 0, MAX_PATH);
1172         driveletter = '\0';
1173         SendMessage(g_listBox, LB_GETTEXT, i, (LPARAM)pathBuffer);
1174         ok( strlen(pathBuffer) == 5, "Length of drive string is not 5\n" );
1175         ok( sscanf(pathBuffer, "[-%c-]", &driveletter) == 1, "Element %d (%s) does not fit [-X-]\n", i, pathBuffer);
1176         ok( driveletter >= 'a' && driveletter <= 'z', "Drive letter not in range a..z, got ascii %d\n", driveletter);
1177         if (!(driveletter >= 'a' && driveletter <= 'z')) {
1178             /* Correct after invalid entry is found */
1179             trace("removing count of invalid entry %s\n", pathBuffer);
1180             itemCount_justDrives--;
1181         }
1182     }
1183
1184     /* Test behavior when no files match the wildcard */
1185     strcpy(pathBuffer, "*.txt");
1186     res = DlgDirList(hWnd, pathBuffer, ID_TEST_LISTBOX, ID_TEST_LABEL,
1187         DDL_DRIVES);
1188     ok (res == 1, "DlgDirList(*.txt, DDL_DRIVES) returned %d expected 1\n", res);
1189
1190     itemCount = SendMessage(g_listBox, LB_GETCOUNT, 0, 0);
1191     ok (itemCount == itemCount_justDrives, "DlgDirList() incorrectly filled the listbox!\n");
1192
1193
1194     /* Test DDL_DIRECTORY|DDL_DRIVES. This does *not* imply DDL_EXCLUSIVE */
1195     strcpy(pathBuffer, "w*.c");
1196     res = DlgDirList(hWnd, pathBuffer, ID_TEST_LISTBOX, ID_TEST_LABEL,
1197         DDL_DIRECTORY|DDL_DRIVES);
1198     ok (res == 1, "DlgDirList(*.c, DDL_DIRECTORY|DDL_DRIVES) failed - 0x%08x\n", GetLastError());
1199
1200     /* There should be some content in the listbox. In particular, there should
1201      * be exactly the number of plain files, plus the number of mapped drives,
1202      * plus one "[..]"
1203      */
1204     itemCount = SendMessage(g_listBox, LB_GETCOUNT, 0, 0);
1205     ok (itemCount == itemCount_justFiles + itemCount_justDrives + 1,
1206         "DlgDirList(DDL_DIRECTORY|DDL_DRIVES) filled with %d entries, expected %d\n",
1207         itemCount, itemCount_justFiles + itemCount_justDrives + 1);
1208
1209     /* Every single item in the control should start with a w and end in .c,
1210      * except for the "[..]" string, which should appear exactly as it is,
1211      * and the mapped drives in the format "[-X-]".
1212      */
1213     for (i = 0; i < itemCount; i++) {
1214         memset(pathBuffer, 0, MAX_PATH);
1215         driveletter = '\0';
1216         SendMessage(g_listBox, LB_GETTEXT, i, (LPARAM)pathBuffer);
1217         p = pathBuffer + strlen(pathBuffer);
1218         if (sscanf(pathBuffer, "[-%c-]", &driveletter) == 1) {
1219             ok( driveletter >= 'a' && driveletter <= 'z', "Drive letter not in range a..z, got ascii %d\n", driveletter);
1220         } else {
1221             ok( !strcmp(pathBuffer, "[..]") ||
1222                 ((pathBuffer[0] == 'w' || pathBuffer[0] == 'W') &&
1223                 (*(p-1) == 'c' || *(p-1) == 'C') &&
1224                 (*(p-2) == '.')), "Element %d (%s) does not fit requested w*.c\n", i, pathBuffer);
1225         }
1226     }
1227
1228     /* Test behavior when no files match the wildcard */
1229     strcpy(pathBuffer, "*.txt");
1230     res = DlgDirList(hWnd, pathBuffer, ID_TEST_LISTBOX, ID_TEST_LABEL,
1231         DDL_DIRECTORY|DDL_DRIVES);
1232     ok (res == 1, "DlgDirList(*.txt, DDL_DIRECTORY|DDL_DRIVES) returned %d expected 1\n", res);
1233
1234     itemCount = SendMessage(g_listBox, LB_GETCOUNT, 0, 0);
1235     ok (itemCount == itemCount_justDrives + 1,
1236         "DlgDirList() incorrectly filled the listbox! (expected %d got %d)\n",
1237         itemCount_justDrives + 1, itemCount);
1238
1239
1240
1241     /* Test DDL_DIRECTORY|DDL_EXCLUSIVE. */
1242     strcpy(pathBuffer, "w*.c");
1243     res = DlgDirList(hWnd, pathBuffer, ID_TEST_LISTBOX, ID_TEST_LABEL,
1244         DDL_DIRECTORY|DDL_EXCLUSIVE);
1245     ok (res == 1, "DlgDirList(*.c, DDL_DIRECTORY|DDL_EXCLUSIVE) failed - 0x%08x\n", GetLastError());
1246
1247     /* There should be exactly one element: "[..]" */
1248     itemCount = SendMessage(g_listBox, LB_GETCOUNT, 0, 0);
1249     ok (itemCount == 1,
1250         "DlgDirList(DDL_DIRECTORY|DDL_EXCLUSIVE) filled with %d entries, expected %d\n",
1251         itemCount, 1);
1252
1253     memset(pathBuffer, 0, MAX_PATH);
1254     SendMessage(g_listBox, LB_GETTEXT, 0, (LPARAM)pathBuffer);
1255     ok( !strcmp(pathBuffer, "[..]"), "First (and only) element is not [..]\n");
1256
1257
1258     /* Test behavior when no files match the wildcard */
1259     strcpy(pathBuffer, "*.txt");
1260     res = DlgDirList(hWnd, pathBuffer, ID_TEST_LISTBOX, ID_TEST_LABEL,
1261         DDL_DIRECTORY|DDL_EXCLUSIVE);
1262     ok (res == 1, "DlgDirList(*.txt, DDL_DIRECTORY|DDL_EXCLUSIVE) returned %d expected 1\n", res);
1263
1264     itemCount = SendMessage(g_listBox, LB_GETCOUNT, 0, 0);
1265     ok (itemCount == 1, "DlgDirList() incorrectly filled the listbox!\n");
1266
1267
1268     /* Test DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE. */
1269     strcpy(pathBuffer, "w*.c");
1270     res = DlgDirList(hWnd, pathBuffer, ID_TEST_LISTBOX, ID_TEST_LABEL,
1271         DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE);
1272     ok (res == 1, "DlgDirList(*.c, DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE) failed - 0x%08x\n", GetLastError());
1273
1274     /* There should be no plain files on the listbox */
1275     itemCount = SendMessage(g_listBox, LB_GETCOUNT, 0, 0);
1276     ok (itemCount == itemCount_justDrives + 1,
1277         "DlgDirList(DDL_DIRECTORY|DDL_EXCLUSIVE) filled with %d entries, expected %d\n",
1278         itemCount, itemCount_justDrives + 1);
1279
1280     for (i = 0; i < itemCount; i++) {
1281         memset(pathBuffer, 0, MAX_PATH);
1282         driveletter = '\0';
1283         SendMessage(g_listBox, LB_GETTEXT, i, (LPARAM)pathBuffer);
1284         p = pathBuffer + strlen(pathBuffer);
1285         if (sscanf(pathBuffer, "[-%c-]", &driveletter) == 1) {
1286             ok( driveletter >= 'a' && driveletter <= 'z', "Drive letter not in range a..z, got ascii %d\n", driveletter);
1287         } else {
1288             ok( !strcmp(pathBuffer, "[..]"), "Element %d (%s) does not fit expected [..]\n", i, pathBuffer);
1289         }
1290     }
1291
1292     /* Test behavior when no files match the wildcard */
1293     strcpy(pathBuffer, "*.txt");
1294     res = DlgDirList(hWnd, pathBuffer, ID_TEST_LISTBOX, ID_TEST_LABEL,
1295         DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE);
1296     ok (res == 1, "DlgDirList(*.txt, DDL_DIRECTORY|DDL_DRIVES|DDL_EXCLUSIVE) returned %d expected 1\n", res);
1297
1298     itemCount = SendMessage(g_listBox, LB_GETCOUNT, 0, 0);
1299     ok (itemCount == itemCount_justDrives + 1, "DlgDirList() incorrectly filled the listbox!\n");
1300
1301
1302     /* Now test DlgDirSelectEx() in normal operation */
1303     /* Fill with everything - drives, directory and all plain files. */
1304     strcpy(pathBuffer, "*");
1305     res = DlgDirList(hWnd, pathBuffer, ID_TEST_LISTBOX, ID_TEST_LABEL,
1306         DDL_DIRECTORY|DDL_DRIVES);
1307     ok (res != 0, "DlgDirList(*, DDL_DIRECTORY|DDL_DRIVES) failed - 0x%08x\n", GetLastError());
1308
1309     SendMessage(g_listBox, LB_SETCURSEL, -1, 0); /* Unselect any current selection */
1310     memset(pathBuffer, 0, MAX_PATH);
1311     SetLastError(0xdeadbeef);
1312     res = DlgDirSelectEx(hWnd, pathBuffer, MAX_PATH, ID_TEST_LISTBOX);
1313     ok (GetLastError() == 0xdeadbeef,
1314         "DlgDirSelectEx() with no selection modified last error code from 0xdeadbeef to 0x%08x\n",
1315         GetLastError());
1316     ok (res == 0, "DlgDirSelectEx() with no selection returned %d, expected 0\n", res);
1317     /* WinXP-SP2 leaves pathBuffer untouched, but Win98 fills it with garbage. */
1318     /*
1319     ok (strlen(pathBuffer) == 0, "DlgDirSelectEx() with no selection filled buffer with %s\n", pathBuffer);
1320     */
1321     /* Test proper drive/dir/file recognition */
1322     itemCount = SendMessage(g_listBox, LB_GETCOUNT, 0, 0);
1323     for (i = 0; i < itemCount; i++) {
1324         memset(itemBuffer, 0, MAX_PATH);
1325         memset(pathBuffer, 0, MAX_PATH);
1326         memset(tempBuffer, 0, MAX_PATH);
1327         driveletter = '\0';
1328         SendMessage(g_listBox, LB_GETTEXT, i, (LPARAM)itemBuffer);
1329         res = SendMessage(g_listBox, LB_SETCURSEL, i, 0);
1330         ok (res == i, "SendMessage(LB_SETCURSEL, %d) failed\n", i);
1331         if (sscanf(itemBuffer, "[-%c-]", &driveletter) == 1) {
1332             /* Current item is a drive letter */
1333             SetLastError(0xdeadbeef);
1334             res = DlgDirSelectEx(hWnd, pathBuffer, MAX_PATH, ID_TEST_LISTBOX);
1335             ok (GetLastError() == 0xdeadbeef,
1336                "DlgDirSelectEx() with selection at %d modified last error code from 0xdeadbeef to 0x%08x\n",
1337                 i, GetLastError());
1338             ok(res == 1, "DlgDirSelectEx() thinks %s (%s) is not a drive/directory!\n", itemBuffer, pathBuffer);
1339
1340             /* For drive letters, DlgDirSelectEx tacks on a colon */
1341             ok (pathBuffer[0] == driveletter && pathBuffer[1] == ':' && pathBuffer[2] == '\0',
1342                 "%d: got \"%s\" expected \"%c:\"\n", i, pathBuffer, driveletter);
1343         } else if (itemBuffer[0] == '[') {
1344             /* Current item is the parent directory */
1345             SetLastError(0xdeadbeef);
1346             res = DlgDirSelectEx(hWnd, pathBuffer, MAX_PATH, ID_TEST_LISTBOX);
1347             ok (GetLastError() == 0xdeadbeef,
1348                "DlgDirSelectEx() with selection at %d modified last error code from 0xdeadbeef to 0x%08x\n",
1349                 i, GetLastError());
1350             ok(res == 1, "DlgDirSelectEx() thinks %s (%s) is not a drive/directory!\n", itemBuffer, pathBuffer);
1351
1352             /* For directories, DlgDirSelectEx tacks on a backslash */
1353             p = pathBuffer + strlen(pathBuffer);
1354             ok (*(p-1) == '\\', "DlgDirSelectEx did NOT tack on a backslash to dir, got %s\n", pathBuffer);
1355
1356             tempBuffer[0] = '[';
1357             strncpy(tempBuffer + 1, pathBuffer, strlen(pathBuffer) - 1);
1358             strcat(tempBuffer, "]");
1359             ok (!strcmp(tempBuffer, itemBuffer), "Formatted directory should be %s, got %s\n", tempBuffer, itemBuffer);
1360         } else {
1361             /* Current item is a plain file */
1362             SetLastError(0xdeadbeef);
1363             res = DlgDirSelectEx(hWnd, pathBuffer, MAX_PATH, ID_TEST_LISTBOX);
1364             ok (GetLastError() == 0xdeadbeef,
1365                "DlgDirSelectEx() with selection at %d modified last error code from 0xdeadbeef to 0x%08x\n",
1366                 i, GetLastError());
1367             ok(res == 0, "DlgDirSelectEx() thinks %s (%s) is a drive/directory!\n", itemBuffer, pathBuffer);
1368
1369             /* NOTE: WinXP tacks a period on all files that lack an extension. This affects
1370              * for example, "Makefile", which gets reported as "Makefile."
1371              */
1372             strcpy(tempBuffer, itemBuffer);
1373             if (strchr(tempBuffer, '.') == NULL) strcat(tempBuffer, ".");
1374             ok (!strcmp(pathBuffer, tempBuffer), "Formatted file should be %s, got %s\n", tempBuffer, pathBuffer);
1375         }
1376     }
1377
1378     /* Now test DlgDirSelectEx() in abnormal operation */
1379     /* Fill list with bogus entries, that look somewhat valid */
1380     SendMessage(g_listBox, LB_RESETCONTENT, 0, 0);
1381     SendMessage(g_listBox, LB_ADDSTRING, 0, (LPARAM)"[notexist.dir]");
1382     SendMessage(g_listBox, LB_ADDSTRING, 0, (LPARAM)"notexist.fil");
1383     itemCount = SendMessage(g_listBox, LB_GETCOUNT, 0, 0);
1384     for (i = 0; i < itemCount; i++) {
1385         memset(itemBuffer, 0, MAX_PATH);
1386         memset(pathBuffer, 0, MAX_PATH);
1387         memset(tempBuffer, 0, MAX_PATH);
1388         driveletter = '\0';
1389         SendMessage(g_listBox, LB_GETTEXT, i, (LPARAM)itemBuffer);
1390         res = SendMessage(g_listBox, LB_SETCURSEL, i, 0);
1391         ok (res == i, "SendMessage(LB_SETCURSEL, %d) failed\n", i);
1392         if (sscanf(itemBuffer, "[-%c-]", &driveletter) == 1) {
1393             /* Current item is a drive letter */
1394             SetLastError(0xdeadbeef);
1395             res = DlgDirSelectEx(hWnd, pathBuffer, MAX_PATH, ID_TEST_LISTBOX);
1396             ok (GetLastError() == 0xdeadbeef,
1397                "DlgDirSelectEx() with selection at %d modified last error code from 0xdeadbeef to 0x%08x\n",
1398                 i, GetLastError());
1399             ok(res == 1, "DlgDirSelectEx() thinks %s (%s) is not a drive/directory!\n", itemBuffer, pathBuffer);
1400
1401             /* For drive letters, DlgDirSelectEx tacks on a colon */
1402             ok (pathBuffer[0] == driveletter && pathBuffer[1] == ':' && pathBuffer[2] == '\0',
1403                 "%d: got \"%s\" expected \"%c:\"\n", i, pathBuffer, driveletter);
1404         } else if (itemBuffer[0] == '[') {
1405             /* Current item is the parent directory */
1406             SetLastError(0xdeadbeef);
1407             res = DlgDirSelectEx(hWnd, pathBuffer, MAX_PATH, ID_TEST_LISTBOX);
1408             ok (GetLastError() == 0xdeadbeef,
1409                "DlgDirSelectEx() with selection at %d modified last error code from 0xdeadbeef to 0x%08x\n",
1410                 i, GetLastError());
1411             ok(res == 1, "DlgDirSelectEx() thinks %s (%s) is not a drive/directory!\n", itemBuffer, pathBuffer);
1412
1413             /* For directories, DlgDirSelectEx tacks on a backslash */
1414             p = pathBuffer + strlen(pathBuffer);
1415             ok (*(p-1) == '\\', "DlgDirSelectEx did NOT tack on a backslash to dir, got %s\n", pathBuffer);
1416
1417             tempBuffer[0] = '[';
1418             strncpy(tempBuffer + 1, pathBuffer, strlen(pathBuffer) - 1);
1419             strcat(tempBuffer, "]");
1420             ok (!strcmp(tempBuffer, itemBuffer), "Formatted directory should be %s, got %s\n", tempBuffer, itemBuffer);
1421         } else {
1422             /* Current item is a plain file */
1423             SetLastError(0xdeadbeef);
1424             res = DlgDirSelectEx(hWnd, pathBuffer, MAX_PATH, ID_TEST_LISTBOX);
1425             ok (GetLastError() == 0xdeadbeef,
1426                "DlgDirSelectEx() with selection at %d modified last error code from 0xdeadbeef to 0x%08x\n",
1427                 i, GetLastError());
1428             ok(res == 0, "DlgDirSelectEx() thinks %s (%s) is a drive/directory!\n", itemBuffer, pathBuffer);
1429
1430             /* NOTE: WinXP and Win98 tack a period on all files that lack an extension.
1431              * This affects for example, "Makefile", which gets reported as "Makefile."
1432              */
1433             strcpy(tempBuffer, itemBuffer);
1434             if (strchr(tempBuffer, '.') == NULL) strcat(tempBuffer, ".");
1435             ok (!strcmp(pathBuffer, tempBuffer), "Formatted file should be %s, got %s\n", tempBuffer, pathBuffer);
1436         }
1437     }
1438     DestroyWindow(hWnd);
1439 }
1440
1441 START_TEST(listbox)
1442 {
1443   const struct listbox_test SS =
1444 /*   {add_style} */
1445     {{0},
1446      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0},
1447      {     1,      1,      1, LB_ERR}, {0,0,0,0},
1448      {     2,      2,      2, LB_ERR}, {0,0,0,0},
1449      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0}};
1450 /* {selected, anchor,  caret, selcount}{TODO fields} */
1451   const struct listbox_test SS_NS =
1452     {{LBS_NOSEL},
1453      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0},
1454      {     1,      1,      1, LB_ERR}, {0,0,0,0},
1455      {     2,      2,      2, LB_ERR}, {0,0,0,0},
1456      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0}};
1457   const struct listbox_test MS =
1458     {{LBS_MULTIPLESEL},
1459      {     0, LB_ERR,      0,      0}, {0,0,0,0},
1460      {     1,      1,      1,      1}, {0,0,0,0},
1461      {     2,      1,      2,      1}, {0,0,0,0},
1462      {     0, LB_ERR,      0,      2}, {0,0,0,0}};
1463   const struct listbox_test MS_NS =
1464     {{LBS_MULTIPLESEL | LBS_NOSEL},
1465      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0},
1466      {     1,      1,      1, LB_ERR}, {0,0,0,0},
1467      {     2,      2,      2, LB_ERR}, {0,0,0,0},
1468      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0}};
1469   const struct listbox_test ES =
1470     {{LBS_EXTENDEDSEL},
1471      {     0, LB_ERR,      0,      0}, {0,0,0,0},
1472      {     1,      1,      1,      1}, {0,0,0,0},
1473      {     2,      2,      2,      1}, {0,0,0,0},
1474      {     0, LB_ERR,      0,      2}, {0,0,0,0}};
1475   const struct listbox_test ES_NS =
1476     {{LBS_EXTENDEDSEL | LBS_NOSEL},
1477      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0},
1478      {     1,      1,      1, LB_ERR}, {0,0,0,0},
1479      {     2,      2,      2, LB_ERR}, {0,0,0,0},
1480      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0}};
1481   const struct listbox_test EMS =
1482     {{LBS_EXTENDEDSEL | LBS_MULTIPLESEL},
1483      {     0, LB_ERR,      0,      0}, {0,0,0,0},
1484      {     1,      1,      1,      1}, {0,0,0,0},
1485      {     2,      2,      2,      1}, {0,0,0,0},
1486      {     0, LB_ERR,      0,      2}, {0,0,0,0}};
1487   const struct listbox_test EMS_NS =
1488     {{LBS_EXTENDEDSEL | LBS_MULTIPLESEL | LBS_NOSEL},
1489      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0},
1490      {     1,      1,      1, LB_ERR}, {0,0,0,0},
1491      {     2,      2,      2, LB_ERR}, {0,0,0,0},
1492      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0}};
1493
1494   trace (" Testing single selection...\n");
1495   check (SS);
1496   trace (" ... with NOSEL\n");
1497   check (SS_NS);
1498   trace (" Testing multiple selection...\n");
1499   check (MS);
1500   trace (" ... with NOSEL\n");
1501   check (MS_NS);
1502   trace (" Testing extended selection...\n");
1503   check (ES);
1504   trace (" ... with NOSEL\n");
1505   check (ES_NS);
1506   trace (" Testing extended and multiple selection...\n");
1507   check (EMS);
1508   trace (" ... with NOSEL\n");
1509   check (EMS_NS);
1510
1511   check_item_height();
1512   test_ownerdraw();
1513   test_selection();
1514   test_listbox_height();
1515   test_itemfrompoint();
1516   test_listbox_item_data();
1517   test_listbox_LB_DIR();
1518   test_listbox_dlgdir();
1519 }