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