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