Fix some gcc 4.0 warnings.
[wine] / dlls / user / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
143   listbox_query (hLB, &answer);
144   listbox_ok (test, init, answer);
145
146   SendMessage (hLB, LB_GETITEMRECT, (WPARAM) 1, (LPARAM) &second_item);
147   buttonpress(hLB, (WORD)second_item.left, (WORD)second_item.top);
148
149   listbox_query (hLB, &answer);
150   listbox_ok (test, click, answer);
151
152   keypress (hLB, VK_DOWN, 0x50, TRUE);
153
154   listbox_query (hLB, &answer);
155   listbox_ok (test, step, answer);
156
157   DestroyWindow (hLB);
158   hLB=create_listbox (test.prop.add_style, 0);
159
160   SendMessage (hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(1, 2));
161   listbox_query (hLB, &answer);
162   listbox_ok (test, sel, answer);
163
164   for (i=0;i<4;i++) {
165         DWORD size = SendMessage (hLB, LB_GETTEXTLEN, i, 0);
166         CHAR *txt;
167         WCHAR *txtw;
168
169         txt = HeapAlloc (GetProcessHeap(), 0, size+1);
170         SendMessageA(hLB, LB_GETTEXT, i, (LPARAM)txt);
171         ok(!strcmp (txt, strings[i]), "returned string for item %d does not match %s vs %s\n", i, txt, strings[i]);
172
173         txtw = HeapAlloc (GetProcessHeap(), 0, 2*size+2);
174         SendMessageW(hLB, LB_GETTEXT, i, (LPARAM)txtw);
175         WideCharToMultiByte( CP_ACP, 0, txtw, -1, txt, size, NULL, NULL );
176         ok(!strcmp (txt, strings[i]), "returned string for item %d does not match %s vs %s\n", i, txt, strings[i]);
177
178         HeapFree (GetProcessHeap(), 0, txtw);
179         HeapFree (GetProcessHeap(), 0, txt);
180   }
181   
182   WAIT;
183   DestroyWindow (hLB);
184 }
185
186 static void check_item_height(void)
187 {
188     HWND hLB;
189     HDC hdc;
190     HFONT font;
191     TEXTMETRIC tm;
192     INT itemHeight;
193
194     hLB = create_listbox (0, 0);
195     ok ((hdc = GetDCEx( hLB, 0, DCX_CACHE )) != 0, "Can't get hdc\n");
196     ok ((font = GetCurrentObject(hdc, OBJ_FONT)) != 0, "Can't get the current font\n");
197     ok (GetTextMetrics( hdc, &tm ), "Can't read font metrics\n");
198     ReleaseDC( hLB, hdc);
199
200     ok (SendMessage(hLB, WM_SETFONT, (WPARAM)font, 0) == 0, "Can't set font\n");
201
202     itemHeight = SendMessage(hLB, LB_GETITEMHEIGHT, 0, 0);
203     ok (itemHeight == tm.tmHeight, "Item height wrong, got %d, expecting %ld\n", itemHeight, tm.tmHeight);
204
205     DestroyWindow (hLB);
206 }
207
208 static LRESULT WINAPI main_window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
209 {
210     switch (msg)
211     {
212     case WM_DRAWITEM:
213     {
214         RECT rc_item, rc_client, rc_clip;
215         DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT *)lparam;
216
217         trace("%p WM_DRAWITEM %08x %08lx\n", hwnd, wparam, lparam);
218
219         ok(wparam == dis->CtlID, "got wParam=%08x instead of %08x\n",
220                         wparam, dis->CtlID);
221         ok(dis->CtlType == ODT_LISTBOX, "wrong CtlType %04x\n", dis->CtlType);
222
223         GetClientRect(dis->hwndItem, &rc_client);
224         trace("hwndItem %p client rect (%ld,%ld-%ld,%ld)\n", dis->hwndItem,
225                rc_client.left, rc_client.top, rc_client.right, rc_client.bottom);
226         GetClipBox(dis->hDC, &rc_clip);
227         trace("clip rect (%ld,%ld-%ld,%ld)\n", rc_clip.left, rc_clip.top, rc_clip.right, rc_clip.bottom);
228         ok(EqualRect(&rc_client, &rc_clip), "client rect of the listbox should be equal to the clip box\n");
229
230         trace("rcItem (%ld,%ld-%ld,%ld)\n", dis->rcItem.left, dis->rcItem.top,
231                dis->rcItem.right, dis->rcItem.bottom);
232         SendMessage(dis->hwndItem, LB_GETITEMRECT, dis->itemID, (LPARAM)&rc_item);
233         trace("item rect (%ld,%ld-%ld,%ld)\n", rc_item.left, rc_item.top, rc_item.right, rc_item.bottom);
234         ok(EqualRect(&dis->rcItem, &rc_item), "item rects are not equal\n");
235
236         break;
237     }
238
239     default:
240         break;
241     }
242
243     return DefWindowProc(hwnd, msg, wparam, lparam);
244 }
245
246 static void test_ownerdraw(void)
247 {
248     WNDCLASS cls;
249     HWND parent, hLB;
250     INT ret;
251     RECT rc;
252
253     cls.style = 0;
254     cls.lpfnWndProc = main_window_proc;
255     cls.cbClsExtra = 0;
256     cls.cbWndExtra = 0;
257     cls.hInstance = GetModuleHandle(0);
258     cls.hIcon = 0;
259     cls.hCursor = LoadCursor(0, (LPSTR)IDC_ARROW);
260     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
261     cls.lpszMenuName = NULL;
262     cls.lpszClassName = "main_window_class";
263     assert(RegisterClass(&cls));
264
265     parent = CreateWindowEx(0, "main_window_class", NULL,
266                             WS_POPUP | WS_VISIBLE,
267                             100, 100, 400, 400,
268                             GetDesktopWindow(), 0,
269                             GetModuleHandle(0), NULL);
270     assert(parent);
271
272     hLB = create_listbox(LBS_OWNERDRAWFIXED | WS_CHILD | WS_VISIBLE, parent);
273     assert(hLB);
274
275     UpdateWindow(hLB);
276
277     /* make height short enough */
278     SendMessage(hLB, LB_GETITEMRECT, 0, (LPARAM)&rc);
279     SetWindowPos(hLB, 0, 0, 0, 100, rc.bottom - rc.top + 1,
280                  SWP_NOZORDER | SWP_NOMOVE);
281
282     /* make 0 item invisible */
283     SendMessage(hLB, LB_SETTOPINDEX, 1, 0);
284     ret = SendMessage(hLB, LB_GETTOPINDEX, 0, 0);
285     ok(ret == 1, "wrong top index %d\n", ret);
286
287     SendMessage(hLB, LB_GETITEMRECT, 0, (LPARAM)&rc);
288     trace("item 0 rect (%ld,%ld-%ld,%ld)\n", rc.left, rc.top, rc.right, rc.bottom);
289     ok(!IsRectEmpty(&rc), "empty item rect\n");
290     ok(rc.top < 0, "rc.top is not negative (%ld)\n", rc.top);
291
292     DestroyWindow(hLB);
293     DestroyWindow(parent);
294 }
295
296 START_TEST(listbox)
297 {
298   const struct listbox_test SS =
299 /*   {add_style} */
300     {{0},
301      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0},
302      {     1,      1,      1, LB_ERR}, {0,0,0,0},
303      {     2,      2,      2, LB_ERR}, {0,0,0,0},
304      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0}};
305 /* {selected, anchor,  caret, selcount}{TODO fields} */
306   const struct listbox_test SS_NS =
307     {{LBS_NOSEL},
308      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0},
309      {     1,      1,      1, LB_ERR}, {0,0,0,0},
310      {     2,      2,      2, LB_ERR}, {0,0,0,0},
311      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0}};
312   const struct listbox_test MS =
313     {{LBS_MULTIPLESEL},
314      {     0, LB_ERR,      0,      0}, {0,0,0,0},
315      {     1,      1,      1,      1}, {0,0,0,0},
316      {     2,      1,      2,      1}, {0,0,0,0},
317      {     0, LB_ERR,      0,      2}, {0,0,0,0}};
318   const struct listbox_test MS_NS =
319     {{LBS_MULTIPLESEL | LBS_NOSEL},
320      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0},
321      {     1,      1,      1, LB_ERR}, {0,0,0,0},
322      {     2,      2,      2, LB_ERR}, {0,0,0,0},
323      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0}};
324   const struct listbox_test ES =
325     {{LBS_EXTENDEDSEL},
326      {     0, LB_ERR,      0,      0}, {0,0,0,0},
327      {     1,      1,      1,      1}, {0,0,0,0},
328      {     2,      2,      2,      1}, {0,0,0,0},
329      {     0, LB_ERR,      0,      2}, {0,0,0,0}};
330   const struct listbox_test ES_NS =
331     {{LBS_EXTENDEDSEL | LBS_NOSEL},
332      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0},
333      {     1,      1,      1, LB_ERR}, {0,0,0,0},
334      {     2,      2,      2, LB_ERR}, {0,0,0,0},
335      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0}};
336   const struct listbox_test EMS =
337     {{LBS_EXTENDEDSEL | LBS_MULTIPLESEL},
338      {     0, LB_ERR,      0,      0}, {0,0,0,0},
339      {     1,      1,      1,      1}, {0,0,0,0},
340      {     2,      2,      2,      1}, {0,0,0,0},
341      {     0, LB_ERR,      0,      2}, {0,0,0,0}};
342   const struct listbox_test EMS_NS =
343     {{LBS_EXTENDEDSEL | LBS_MULTIPLESEL | LBS_NOSEL},
344      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0},
345      {     1,      1,      1, LB_ERR}, {0,0,0,0},
346      {     2,      2,      2, LB_ERR}, {0,0,0,0},
347      {LB_ERR, LB_ERR,      0, LB_ERR}, {0,0,0,0}};
348
349   trace (" Testing single selection...\n");
350   check (SS);
351   trace (" ... with NOSEL\n");
352   check (SS_NS);
353   trace (" Testing multiple selection...\n");
354   check (MS);
355   trace (" ... with NOSEL\n");
356   check (MS_NS);
357   trace (" Testing extended selection...\n");
358   check (ES);
359   trace (" ... with NOSEL\n");
360   check (ES_NS);
361   trace (" Testing extended and multiple selection...\n");
362   check (EMS);
363   trace (" ... with NOSEL\n");
364   check (EMS_NS);
365
366   check_item_height();
367   test_ownerdraw();
368 }