user32: Fix LB_ITEMFROMPOINT tests for listbox control.
[wine] / dlls / user32 / tests / listbox.c
1 /* Unit test suite for list boxes.
2  *
3  * Copyright 2003 Ferenc Wagner
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #include <assert.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "winnls.h"
29
30 #include "wine/test.h"
31
32 #ifdef VISIBLE
33 #define WAIT Sleep (1000)
34 #define REDRAW RedrawWindow (handle, NULL, 0, RDW_UPDATENOW)
35 #else
36 #define WAIT
37 #define REDRAW
38 #endif
39
40 static const char * const strings[4] = {
41   "First added",
42   "Second added",
43   "Third added",
44   "Fourth added which is very long because at some time we only had a 256 byte character buffer and that was overflowing in one of those applications that had a common dialog file open box and tried to add a 300 characters long custom filter string which of course the code did not like and crashed. Just make sure this string is longer than 256 characters."
45 };
46
47 static HWND
48 create_listbox (DWORD add_style, HWND parent)
49 {
50   HWND handle;
51   int ctl_id=0;
52   if (parent)
53     ctl_id=1;
54   handle=CreateWindow ("LISTBOX", "TestList",
55                             (LBS_STANDARD & ~LBS_SORT) | add_style,
56                             0, 0, 100, 100,
57                             parent, (HMENU)ctl_id, NULL, 0);
58
59   assert (handle);
60   SendMessage (handle, LB_ADDSTRING, 0, (LPARAM) (LPCTSTR) strings[0]);
61   SendMessage (handle, LB_ADDSTRING, 0, (LPARAM) (LPCTSTR) strings[1]);
62   SendMessage (handle, LB_ADDSTRING, 0, (LPARAM) (LPCTSTR) strings[2]);
63   SendMessage (handle, LB_ADDSTRING, 0, (LPARAM) (LPCTSTR) strings[3]);
64
65 #ifdef VISIBLE
66   ShowWindow (handle, SW_SHOW);
67 #endif
68   REDRAW;
69
70   return handle;
71 }
72
73 struct listbox_prop {
74   DWORD add_style;
75 };
76
77 struct listbox_stat {
78   int selected, anchor, caret, selcount;
79 };
80
81 struct listbox_test {
82   struct listbox_prop prop;
83   struct listbox_stat  init,  init_todo;
84   struct listbox_stat click, click_todo;
85   struct listbox_stat  step,  step_todo;
86   struct listbox_stat   sel,   sel_todo;
87 };
88
89 static void
90 listbox_query (HWND handle, struct listbox_stat *results)
91 {
92   results->selected = SendMessage (handle, LB_GETCURSEL, 0, 0);
93   results->anchor   = SendMessage (handle, LB_GETANCHORINDEX, 0, 0);
94   results->caret    = SendMessage (handle, LB_GETCARETINDEX, 0, 0);
95   results->selcount = SendMessage (handle, LB_GETSELCOUNT, 0, 0);
96 }
97
98 static void
99 buttonpress (HWND handle, WORD x, WORD y)
100 {
101   LPARAM lp=x+(y<<16);
102
103   WAIT;
104   SendMessage (handle, WM_LBUTTONDOWN, (WPARAM) MK_LBUTTON, lp);
105   SendMessage (handle, WM_LBUTTONUP  , (WPARAM) 0         , lp);
106   REDRAW;
107 }
108
109 static void
110 keypress (HWND handle, WPARAM keycode, BYTE scancode, BOOL extended)
111 {
112   LPARAM lp=1+(scancode<<16)+(extended?KEYEVENTF_EXTENDEDKEY:0);
113
114   WAIT;
115   SendMessage (handle, WM_KEYDOWN, keycode, lp);
116   SendMessage (handle, WM_KEYUP  , keycode, lp | 0xc000000);
117   REDRAW;
118 }
119
120 #define listbox_field_ok(t, s, f, got) \
121   ok (t.s.f==got.f, "style %#x, step " #s ", field " #f \
122       ": expected %d, got %d\n", (unsigned int)t.prop.add_style, \
123       t.s.f, got.f)
124
125 #define listbox_todo_field_ok(t, s, f, got) \
126   if (t.s##_todo.f) todo_wine { listbox_field_ok(t, s, f, got); } \
127   else listbox_field_ok(t, s, f, got)
128
129 #define listbox_ok(t, s, got) \
130   listbox_todo_field_ok(t, s, selected, got); \
131   listbox_todo_field_ok(t, s, anchor, got); \
132   listbox_todo_field_ok(t, s, caret, got); \
133   listbox_todo_field_ok(t, s, selcount, got)
134
135 static void
136 check (const struct listbox_test test)
137 {
138   struct listbox_stat answer;
139   HWND hLB=create_listbox (test.prop.add_style, 0);
140   RECT second_item;
141   int i;
142   int res;
143
144   listbox_query (hLB, &answer);
145   listbox_ok (test, init, answer);
146
147   SendMessage (hLB, LB_GETITEMRECT, (WPARAM) 1, (LPARAM) &second_item);
148   buttonpress(hLB, (WORD)second_item.left, (WORD)second_item.top);
149
150   listbox_query (hLB, &answer);
151   listbox_ok (test, click, answer);
152
153   keypress (hLB, VK_DOWN, 0x50, TRUE);
154
155   listbox_query (hLB, &answer);
156   listbox_ok (test, step, answer);
157
158   DestroyWindow (hLB);
159   hLB=create_listbox (test.prop.add_style, 0);
160
161   SendMessage (hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(1, 2));
162   listbox_query (hLB, &answer);
163   listbox_ok (test, sel, answer);
164
165   for (i=0;i<4;i++) {
166         DWORD size = SendMessage (hLB, LB_GETTEXTLEN, i, 0);
167         CHAR *txt;
168         WCHAR *txtw;
169
170         txt = HeapAlloc (GetProcessHeap(), 0, size+1);
171         SendMessageA(hLB, LB_GETTEXT, i, (LPARAM)txt);
172         ok(!strcmp (txt, strings[i]), "returned string for item %d does not match %s vs %s\n", i, txt, strings[i]);
173
174         txtw = HeapAlloc (GetProcessHeap(), 0, 2*size+2);
175         SendMessageW(hLB, LB_GETTEXT, i, (LPARAM)txtw);
176         WideCharToMultiByte( CP_ACP, 0, txtw, -1, txt, size, NULL, NULL );
177         ok(!strcmp (txt, strings[i]), "returned string for item %d does not match %s vs %s\n", i, txt, strings[i]);
178
179         HeapFree (GetProcessHeap(), 0, txtw);
180         HeapFree (GetProcessHeap(), 0, txt);
181   }
182   
183   /* Confirm the count of items, and that an invalid delete does not remove anything */
184   res = SendMessage (hLB, LB_GETCOUNT, 0, 0);
185   ok((res==4), "Expected 4 items, got %d\n", res);
186   res = SendMessage (hLB, LB_DELETESTRING, -1, 0);
187   ok((res==LB_ERR), "Expected LB_ERR items, got %d\n", res);
188   res = SendMessage (hLB, LB_DELETESTRING, 4, 0);
189   ok((res==LB_ERR), "Expected LB_ERR items, got %d\n", res);
190   res = SendMessage (hLB, LB_GETCOUNT, 0, 0);
191   ok((res==4), "Expected 4 items, got %d\n", res);
192
193   WAIT;
194   DestroyWindow (hLB);
195 }
196
197 static void check_item_height(void)
198 {
199     HWND hLB;
200     HDC hdc;
201     HFONT font;
202     TEXTMETRIC tm;
203     INT itemHeight;
204
205     hLB = create_listbox (0, 0);
206     ok ((hdc = GetDCEx( hLB, 0, DCX_CACHE )) != 0, "Can't get hdc\n");
207     ok ((font = GetCurrentObject(hdc, OBJ_FONT)) != 0, "Can't get the current font\n");
208     ok (GetTextMetrics( hdc, &tm ), "Can't read font metrics\n");
209     ReleaseDC( hLB, hdc);
210
211     ok (SendMessage(hLB, WM_SETFONT, (WPARAM)font, 0) == 0, "Can't set font\n");
212
213     itemHeight = SendMessage(hLB, LB_GETITEMHEIGHT, 0, 0);
214     ok (itemHeight == tm.tmHeight, "Item height wrong, got %d, expecting %d\n", itemHeight, tm.tmHeight);
215
216     DestroyWindow (hLB);
217
218     hLB = CreateWindow ("LISTBOX", "TestList", LBS_OWNERDRAWVARIABLE,
219                          0, 0, 100, 100, NULL, NULL, NULL, 0);
220     itemHeight = SendMessage(hLB, LB_GETITEMHEIGHT, 0, 0);
221     ok(itemHeight == tm.tmHeight, "itemHeight %d\n", itemHeight);
222     itemHeight = SendMessage(hLB, LB_GETITEMHEIGHT, 5, 0);
223     ok(itemHeight == tm.tmHeight, "itemHeight %d\n", itemHeight);
224     itemHeight = SendMessage(hLB, LB_GETITEMHEIGHT, -5, 0);
225     ok(itemHeight == tm.tmHeight, "itemHeight %d\n", itemHeight);
226     DestroyWindow (hLB);
227 }
228
229 static LRESULT WINAPI main_window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
230 {
231     switch (msg)
232     {
233     case WM_DRAWITEM:
234     {
235         RECT rc_item, rc_client, rc_clip;
236         DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT *)lparam;
237
238         trace("%p WM_DRAWITEM %08lx %08lx\n", hwnd, wparam, lparam);
239
240         ok(wparam == dis->CtlID, "got wParam=%08lx instead of %08x\n",
241                         wparam, dis->CtlID);
242         ok(dis->CtlType == ODT_LISTBOX, "wrong CtlType %04x\n", dis->CtlType);
243
244         GetClientRect(dis->hwndItem, &rc_client);
245         trace("hwndItem %p client rect (%d,%d-%d,%d)\n", dis->hwndItem,
246                rc_client.left, rc_client.top, rc_client.right, rc_client.bottom);
247         GetClipBox(dis->hDC, &rc_clip);
248         trace("clip rect (%d,%d-%d,%d)\n", rc_clip.left, rc_clip.top, rc_clip.right, rc_clip.bottom);
249         ok(EqualRect(&rc_client, &rc_clip), "client rect of the listbox should be equal to the clip box\n");
250
251         trace("rcItem (%d,%d-%d,%d)\n", dis->rcItem.left, dis->rcItem.top,
252                dis->rcItem.right, dis->rcItem.bottom);
253         SendMessage(dis->hwndItem, LB_GETITEMRECT, dis->itemID, (LPARAM)&rc_item);
254         trace("item rect (%d,%d-%d,%d)\n", rc_item.left, rc_item.top, rc_item.right, rc_item.bottom);
255         ok(EqualRect(&dis->rcItem, &rc_item), "item rects are not equal\n");
256
257         break;
258     }
259
260     default:
261         break;
262     }
263
264     return DefWindowProc(hwnd, msg, wparam, lparam);
265 }
266
267 static void test_ownerdraw(void)
268 {
269     WNDCLASS cls;
270     HWND parent, hLB;
271     INT ret;
272     RECT rc;
273
274     cls.style = 0;
275     cls.lpfnWndProc = main_window_proc;
276     cls.cbClsExtra = 0;
277     cls.cbWndExtra = 0;
278     cls.hInstance = GetModuleHandle(0);
279     cls.hIcon = 0;
280     cls.hCursor = LoadCursor(0, (LPSTR)IDC_ARROW);
281     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
282     cls.lpszMenuName = NULL;
283     cls.lpszClassName = "main_window_class";
284     assert(RegisterClass(&cls));
285
286     parent = CreateWindowEx(0, "main_window_class", NULL,
287                             WS_POPUP | WS_VISIBLE,
288                             100, 100, 400, 400,
289                             GetDesktopWindow(), 0,
290                             GetModuleHandle(0), NULL);
291     assert(parent);
292
293     hLB = create_listbox(LBS_OWNERDRAWFIXED | WS_CHILD | WS_VISIBLE, parent);
294     assert(hLB);
295
296     UpdateWindow(hLB);
297
298     /* make height short enough */
299     SendMessage(hLB, LB_GETITEMRECT, 0, (LPARAM)&rc);
300     SetWindowPos(hLB, 0, 0, 0, 100, rc.bottom - rc.top + 1,
301                  SWP_NOZORDER | SWP_NOMOVE);
302
303     /* make 0 item invisible */
304     SendMessage(hLB, LB_SETTOPINDEX, 1, 0);
305     ret = SendMessage(hLB, LB_GETTOPINDEX, 0, 0);
306     ok(ret == 1, "wrong top index %d\n", ret);
307
308     SendMessage(hLB, LB_GETITEMRECT, 0, (LPARAM)&rc);
309     trace("item 0 rect (%d,%d-%d,%d)\n", rc.left, rc.top, rc.right, rc.bottom);
310     ok(!IsRectEmpty(&rc), "empty item rect\n");
311     ok(rc.top < 0, "rc.top is not negative (%d)\n", rc.top);
312
313     DestroyWindow(hLB);
314     DestroyWindow(parent);
315 }
316
317 #define listbox_test_query(exp, got) \
318   ok(exp.selected == got.selected, "expected selected %d, got %d\n", exp.selected, got.selected); \
319   ok(exp.anchor == got.anchor, "expected anchor %d, got %d\n", exp.anchor, got.anchor); \
320   ok(exp.caret == got.caret, "expected caret %d, got %d\n", exp.caret, got.caret); \
321   ok(exp.selcount == got.selcount, "expected selcount %d, got %d\n", exp.selcount, got.selcount);
322
323 static void test_selection(void)
324 {
325     static const struct listbox_stat test_nosel = { 0, LB_ERR, 0, 0 };
326     static const struct listbox_stat test_1 = { 0, LB_ERR, 0, 2 };
327     static const struct listbox_stat test_2 = { 0, LB_ERR, 0, 3 };
328     static const struct listbox_stat test_3 = { 0, LB_ERR, 0, 4 };
329     HWND hLB;
330     struct listbox_stat answer;
331     INT ret;
332
333     trace("testing LB_SELITEMRANGE\n");
334
335     hLB = create_listbox(LBS_EXTENDEDSEL, 0);
336     assert(hLB);
337
338     listbox_query(hLB, &answer);
339     listbox_test_query(test_nosel, answer);
340
341     ret = SendMessage(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(1, 2));
342     ok(ret == LB_OKAY, "LB_SELITEMRANGE returned %d instead of LB_OKAY\n", ret);
343     listbox_query(hLB, &answer);
344     listbox_test_query(test_1, answer);
345
346     SendMessage(hLB, LB_SETSEL, FALSE, (LPARAM)-1);
347     listbox_query(hLB, &answer);
348     listbox_test_query(test_nosel, answer);
349
350     ret = SendMessage(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(0, 4));
351     ok(ret == LB_OKAY, "LB_SELITEMRANGE returned %d instead of LB_OKAY\n", ret);
352     listbox_query(hLB, &answer);
353     listbox_test_query(test_3, answer);
354
355     SendMessage(hLB, LB_SETSEL, FALSE, (LPARAM)-1);
356     listbox_query(hLB, &answer);
357     listbox_test_query(test_nosel, answer);
358
359     ret = SendMessage(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(-5, 5));
360     ok(ret == LB_OKAY, "LB_SELITEMRANGE returned %d instead of LB_OKAY\n", ret);
361     listbox_query(hLB, &answer);
362     listbox_test_query(test_nosel, answer);
363
364     SendMessage(hLB, LB_SETSEL, FALSE, (LPARAM)-1);
365     listbox_query(hLB, &answer);
366     listbox_test_query(test_nosel, answer);
367
368     ret = SendMessage(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(2, 10));
369     ok(ret == LB_OKAY, "LB_SELITEMRANGE returned %d instead of LB_OKAY\n", ret);
370     listbox_query(hLB, &answer);
371     listbox_test_query(test_1, answer);
372
373     SendMessage(hLB, LB_SETSEL, FALSE, (LPARAM)-1);
374     listbox_query(hLB, &answer);
375     listbox_test_query(test_nosel, answer);
376
377     ret = SendMessage(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(4, 10));
378     ok(ret == LB_OKAY, "LB_SELITEMRANGE returned %d instead of LB_OKAY\n", ret);
379     listbox_query(hLB, &answer);
380     listbox_test_query(test_nosel, answer);
381
382     SendMessage(hLB, LB_SETSEL, FALSE, (LPARAM)-1);
383     listbox_query(hLB, &answer);
384     listbox_test_query(test_nosel, answer);
385
386     ret = SendMessage(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(10, 1));
387     ok(ret == LB_OKAY, "LB_SELITEMRANGE returned %d instead of LB_OKAY\n", ret);
388     listbox_query(hLB, &answer);
389     listbox_test_query(test_2, answer);
390
391     SendMessage(hLB, LB_SETSEL, FALSE, (LPARAM)-1);
392     listbox_query(hLB, &answer);
393     listbox_test_query(test_nosel, answer);
394
395     ret = SendMessage(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(1, -1));
396     ok(ret == LB_OKAY, "LB_SELITEMRANGE returned %d instead of LB_OKAY\n", ret);
397     listbox_query(hLB, &answer);
398     listbox_test_query(test_2, answer);
399
400     DestroyWindow(hLB);
401 }
402
403 static void test_listbox_height(void)
404 {
405     HWND hList;
406     int r, id;
407
408     hList = CreateWindow( "ListBox", "list test", 0, 
409                           1, 1, 600, 100, NULL, NULL, NULL, NULL );
410     ok( hList != NULL, "failed to create listbox\n");
411
412     id = SendMessage( hList, LB_ADDSTRING, 0, (LPARAM) "hi");
413     ok( id == 0, "item id wrong\n");
414
415     r = SendMessage( hList, LB_SETITEMHEIGHT, 0, MAKELPARAM( 20, 0 ));
416     ok( r == 0, "send message failed\n");
417
418     r = SendMessage(hList, LB_GETITEMHEIGHT, 0, 0 );
419     ok( r == 20, "height wrong\n");
420
421     r = SendMessage( hList, LB_SETITEMHEIGHT, 0, MAKELPARAM( 0, 30 ));
422     ok( r == -1, "send message failed\n");
423
424     r = SendMessage(hList, LB_GETITEMHEIGHT, 0, 0 );
425     ok( r == 20, "height wrong\n");
426
427     r = SendMessage( hList, LB_SETITEMHEIGHT, 0, MAKELPARAM( 0x100, 0 ));
428     ok( r == -1, "send message failed\n");
429
430     r = SendMessage(hList, LB_GETITEMHEIGHT, 0, 0 );
431     ok( r == 20, "height wrong\n");
432
433     r = SendMessage( hList, LB_SETITEMHEIGHT, 0, MAKELPARAM( 0xff, 0 ));
434     ok( r == 0, "send message failed\n");
435
436     r = SendMessage(hList, LB_GETITEMHEIGHT, 0, 0 );
437     ok( r == 0xff, "height wrong\n");
438
439     DestroyWindow( hList );
440 }
441
442 static void test_itemfrompoint(void)
443 {
444     /* WS_POPUP is required in order to have a more accurate size calculation (
445        without caption). LBS_NOINTEGRALHEIGHT is required in order to test
446        behavior of partially-displayed item.
447      */
448     HWND hList = CreateWindow( "ListBox", "list test",
449                                WS_VISIBLE|WS_POPUP|LBS_NOINTEGRALHEIGHT,
450                                1, 1, 600, 100, NULL, NULL, NULL, NULL );
451     LONG r, id;
452     RECT rc;
453
454     /* For an empty listbox win2k returns 0x1ffff, win98 returns 0x10000 */
455     r = SendMessage(hList, LB_ITEMFROMPOINT, 0, MAKELPARAM( /* x */ 30, /* y */ 30 ));
456     ok( r == 0x1ffff || r == 0x10000, "ret %x\n", r );
457
458     r = SendMessage(hList, LB_ITEMFROMPOINT, 0, MAKELPARAM( 700, 30 ));
459     ok( r == 0x1ffff || r == 0x10000, "ret %x\n", r );
460
461     r = SendMessage(hList, LB_ITEMFROMPOINT, 0, MAKELPARAM( 30, 300 ));
462     ok( r == 0x1ffff || r == 0x10000, "ret %x\n", r );
463
464     id = SendMessage( hList, LB_ADDSTRING, 0, (LPARAM) "hi");
465     ok( id == 0, "item id wrong\n");
466     id = SendMessage( hList, LB_ADDSTRING, 0, (LPARAM) "hi1");
467     ok( id == 1, "item id wrong\n");
468
469     r = SendMessage(hList, LB_ITEMFROMPOINT, 0, MAKELPARAM( /* x */ 30, /* y */ 30 ));
470     ok( r == 0x1, "ret %x\n", r );
471
472     r = SendMessage(hList, LB_ITEMFROMPOINT, 0, MAKELPARAM( /* x */ 30, /* y */ 601 ));
473     ok( r == 0x10001, "ret %x\n", r );
474
475     /* Resize control so that below assertions about sizes are valid */
476     r = SendMessage( hList, LB_GETITEMRECT, 0, (LPARAM)&rc);
477     ok( r == 1, "ret %x\n", r);
478     r = MoveWindow(hList, 1, 1, 600, (rc.bottom - rc.top + 1) * 9 / 2, TRUE);
479     ok( r != 0, "ret %x\n", r);
480
481     id = SendMessage( hList, LB_ADDSTRING, 0, (LPARAM) "hi2");
482     ok( id == 2, "item id wrong\n");
483     id = SendMessage( hList, LB_ADDSTRING, 0, (LPARAM) "hi3");
484     ok( id == 3, "item id wrong\n");
485     id = SendMessage( hList, LB_ADDSTRING, 0, (LPARAM) "hi4");
486     ok( id == 4, "item id wrong\n");
487     id = SendMessage( hList, LB_ADDSTRING, 0, (LPARAM) "hi5");
488     ok( id == 5, "item id wrong\n");
489     id = SendMessage( hList, LB_ADDSTRING, 0, (LPARAM) "hi6");
490     ok( id == 6, "item id wrong\n");
491     id = SendMessage( hList, LB_ADDSTRING, 0, (LPARAM) "hi7");
492     ok( id == 7, "item id wrong\n");
493
494     /* Set the listbox up so that id 1 is at the top, this leaves 5
495        partially visible at the bottom and 6, 7 are invisible */
496
497     SendMessage( hList, LB_SETTOPINDEX, 1, 0);
498     r = SendMessage( hList, LB_GETTOPINDEX, 0, 0);
499     ok( r == 1, "top %d\n", r);
500
501     r = SendMessage( hList, LB_GETITEMRECT, 5, (LPARAM)&rc);
502     ok( r == 1, "ret %x\n", r);
503     r = SendMessage( hList, LB_GETITEMRECT, 6, (LPARAM)&rc);
504     ok( r == 0, "ret %x\n", r);
505
506     r = SendMessage( hList, LB_ITEMFROMPOINT, 0, MAKELPARAM(/* x */ 10, /* y */ 10) );
507     ok( r == 1, "ret %x\n", r);
508
509     r = SendMessage( hList, LB_ITEMFROMPOINT, 0, MAKELPARAM(1000, 10) );
510     ok( r == 0x10001, "ret %x\n", r );
511
512     r = SendMessage( hList, LB_ITEMFROMPOINT, 0, MAKELPARAM(10, -10) );
513     ok( r == 0x10001, "ret %x\n", r );
514
515     r = SendMessage( hList, LB_ITEMFROMPOINT, 0, MAKELPARAM(10, 100) );
516     ok( r == 0x10005, "item %x\n", r );
517
518     r = SendMessage( hList, LB_ITEMFROMPOINT, 0, MAKELPARAM(10, 200) );
519     ok( r == 0x10005, "item %x\n", r );
520
521     DestroyWindow( hList );
522 }
523
524 static void test_listbox_item_data(void)
525 {
526     HWND hList;
527     int r, id;
528
529     hList = CreateWindow( "ListBox", "list test", 0,
530                           1, 1, 600, 100, NULL, NULL, NULL, NULL );
531     ok( hList != NULL, "failed to create listbox\n");
532
533     id = SendMessage( hList, LB_ADDSTRING, 0, (LPARAM) "hi");
534     ok( id == 0, "item id wrong\n");
535
536     r = SendMessage( hList, LB_SETITEMDATA, 0, MAKELPARAM( 20, 0 ));
537     ok(r == TRUE, "LB_SETITEMDATA returned %d instead of TRUE\n", r);
538
539     r = SendMessage( hList, LB_GETITEMDATA, 0, 0);
540     ok( r == 20, "get item data failed\n");
541
542     DestroyWindow( hList );
543 }
544
545 START_TEST(listbox)
546 {
547   const struct listbox_test SS =
548 /*   {add_style} */
549     {{0},
550      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0},
551      {     1,      1,      1, LB_ERR}, {0,0,0,0},
552      {     2,      2,      2, LB_ERR}, {0,0,0,0},
553      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0}};
554 /* {selected, anchor,  caret, selcount}{TODO fields} */
555   const struct listbox_test SS_NS =
556     {{LBS_NOSEL},
557      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0},
558      {     1,      1,      1, LB_ERR}, {0,0,0,0},
559      {     2,      2,      2, LB_ERR}, {0,0,0,0},
560      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0}};
561   const struct listbox_test MS =
562     {{LBS_MULTIPLESEL},
563      {     0, LB_ERR,      0,      0}, {0,0,0,0},
564      {     1,      1,      1,      1}, {0,0,0,0},
565      {     2,      1,      2,      1}, {0,0,0,0},
566      {     0, LB_ERR,      0,      2}, {0,0,0,0}};
567   const struct listbox_test MS_NS =
568     {{LBS_MULTIPLESEL | LBS_NOSEL},
569      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0},
570      {     1,      1,      1, LB_ERR}, {0,0,0,0},
571      {     2,      2,      2, LB_ERR}, {0,0,0,0},
572      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0}};
573   const struct listbox_test ES =
574     {{LBS_EXTENDEDSEL},
575      {     0, LB_ERR,      0,      0}, {0,0,0,0},
576      {     1,      1,      1,      1}, {0,0,0,0},
577      {     2,      2,      2,      1}, {0,0,0,0},
578      {     0, LB_ERR,      0,      2}, {0,0,0,0}};
579   const struct listbox_test ES_NS =
580     {{LBS_EXTENDEDSEL | LBS_NOSEL},
581      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0},
582      {     1,      1,      1, LB_ERR}, {0,0,0,0},
583      {     2,      2,      2, LB_ERR}, {0,0,0,0},
584      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0}};
585   const struct listbox_test EMS =
586     {{LBS_EXTENDEDSEL | LBS_MULTIPLESEL},
587      {     0, LB_ERR,      0,      0}, {0,0,0,0},
588      {     1,      1,      1,      1}, {0,0,0,0},
589      {     2,      2,      2,      1}, {0,0,0,0},
590      {     0, LB_ERR,      0,      2}, {0,0,0,0}};
591   const struct listbox_test EMS_NS =
592     {{LBS_EXTENDEDSEL | LBS_MULTIPLESEL | LBS_NOSEL},
593      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0},
594      {     1,      1,      1, LB_ERR}, {0,0,0,0},
595      {     2,      2,      2, LB_ERR}, {0,0,0,0},
596      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0}};
597
598   trace (" Testing single selection...\n");
599   check (SS);
600   trace (" ... with NOSEL\n");
601   check (SS_NS);
602   trace (" Testing multiple selection...\n");
603   check (MS);
604   trace (" ... with NOSEL\n");
605   check (MS_NS);
606   trace (" Testing extended selection...\n");
607   check (ES);
608   trace (" ... with NOSEL\n");
609   check (ES_NS);
610   trace (" Testing extended and multiple selection...\n");
611   check (EMS);
612   trace (" ... with NOSEL\n");
613   check (EMS_NS);
614
615   check_item_height();
616   test_ownerdraw();
617   test_selection();
618   test_listbox_height();
619   test_itemfrompoint();
620   test_listbox_item_data();
621 }