Autosyncing with Wine HEAD
[reactos.git] / rostests / winetests / comctl32 / listview.c
1 /*
2 * ListView tests
3 *
4 * Copyright 2006 Mike McCormack for CodeWeavers
5 * Copyright 2007 George Gov
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include <stdio.h>
23 #include <windows.h>
24 #include <commctrl.h>
25
26 #include "wine/test.h"
27 #include "msg.h"
28
29 #define PARENT_SEQ_INDEX 0
30 #define LISTVIEW_SEQ_INDEX 1
31 #define NUM_MSG_SEQUENCES 2
32
33 #define LISTVIEW_ID 0
34 #define HEADER_ID 1
35
36 #define expect(expected, got) ok(got == expected, "Expected %d, got %d\n", expected, got)
37 #define expect2(expected1, expected2, got1, got2) ok(expected1 == got1 && expected2 == got2, \
38 "expected (%d,%d), got (%d,%d)\n", expected1, expected2, got1, got2)
39
40 HWND hwndparent;
41
42 static struct msg_sequence *sequences[NUM_MSG_SEQUENCES];
43
44 static const struct message create_parent_wnd_seq[] = {
45 { WM_GETMINMAXINFO, sent },
46 { WM_NCCREATE, sent },
47 { WM_NCCALCSIZE, sent|wparam, 0 },
48 { WM_CREATE, sent },
49 { WM_SHOWWINDOW, sent|wparam, 1 },
50 { WM_WINDOWPOSCHANGING, sent|wparam, 0 },
51 { WM_WINDOWPOSCHANGING, sent|wparam, 0 },
52 { WM_ACTIVATEAPP, sent|wparam, 1 },
53 { WM_NCACTIVATE, sent|wparam, 1 },
54 { WM_ACTIVATE, sent|wparam, 1 },
55 { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
56 { WM_IME_NOTIFY, sent|defwinproc|optional },
57 { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
58 /* Win9x adds SWP_NOZORDER below */
59 { WM_WINDOWPOSCHANGED, sent, /*|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE*/ },
60 { WM_NCCALCSIZE, sent|wparam|optional, 1 },
61 { WM_SIZE, sent },
62 { WM_MOVE, sent },
63 { 0 }
64 };
65
66 static const struct message redraw_listview_seq[] = {
67 { WM_PAINT, sent|id, 0, 0, LISTVIEW_ID },
68 { WM_PAINT, sent|id, 0, 0, HEADER_ID },
69 { WM_NCPAINT, sent|id|defwinproc, 0, 0, HEADER_ID },
70 { WM_ERASEBKGND, sent|id|defwinproc, 0, 0, HEADER_ID },
71 { WM_NOTIFY, sent|id|defwinproc, 0, 0, LISTVIEW_ID },
72 { WM_NCPAINT, sent|id|defwinproc, 0, 0, LISTVIEW_ID },
73 { WM_ERASEBKGND, sent|id|defwinproc, 0, 0, LISTVIEW_ID },
74 { 0 }
75 };
76
77 static const struct message listview_icon_spacing_seq[] = {
78 { LVM_SETICONSPACING, sent|lparam, 0, (LPARAM) MAKELONG(20, 30) },
79 { LVM_SETICONSPACING, sent|lparam, 0, (LPARAM) MAKELONG(25, 35) },
80 { LVM_SETICONSPACING, sent|lparam, 0, (LPARAM) MAKELONG(-1, -1) },
81 { 0 }
82 };
83
84 static const struct message listview_color_seq[] = {
85 { LVM_SETBKCOLOR, sent|lparam, 0, RGB(0,0,0) },
86 { LVM_GETBKCOLOR, sent },
87 { LVM_SETTEXTCOLOR, sent|lparam, 0, RGB(0,0,0) },
88 { LVM_GETTEXTCOLOR, sent },
89 { LVM_SETTEXTBKCOLOR, sent|lparam, 0, RGB(0,0,0) },
90 { LVM_GETTEXTBKCOLOR, sent },
91
92 { LVM_SETBKCOLOR, sent|lparam, 0, RGB(100,50,200) },
93 { LVM_GETBKCOLOR, sent },
94 { LVM_SETTEXTCOLOR, sent|lparam, 0, RGB(100,50,200) },
95 { LVM_GETTEXTCOLOR, sent },
96 { LVM_SETTEXTBKCOLOR, sent|lparam, 0, RGB(100,50,200) },
97 { LVM_GETTEXTBKCOLOR, sent },
98
99 { LVM_SETBKCOLOR, sent|lparam, 0, CLR_NONE },
100 { LVM_GETBKCOLOR, sent },
101 { LVM_SETTEXTCOLOR, sent|lparam, 0, CLR_NONE },
102 { LVM_GETTEXTCOLOR, sent },
103 { LVM_SETTEXTBKCOLOR, sent|lparam, 0, CLR_NONE },
104 { LVM_GETTEXTBKCOLOR, sent },
105
106 { LVM_SETBKCOLOR, sent|lparam, 0, RGB(255,255,255) },
107 { LVM_GETBKCOLOR, sent },
108 { LVM_SETTEXTCOLOR, sent|lparam, 0, RGB(255,255,255) },
109 { LVM_GETTEXTCOLOR, sent },
110 { LVM_SETTEXTBKCOLOR, sent|lparam, 0, RGB(255,255,255) },
111 { LVM_GETTEXTBKCOLOR, sent },
112 { 0 }
113 };
114
115 static const struct message listview_item_count_seq[] = {
116 { LVM_GETITEMCOUNT, sent },
117 { LVM_INSERTITEM, sent },
118 { LVM_INSERTITEM, sent },
119 { LVM_INSERTITEM, sent },
120 { LVM_GETITEMCOUNT, sent },
121 { LVM_DELETEITEM, sent|wparam, 2 },
122 { LVM_GETITEMCOUNT, sent },
123 { LVM_DELETEALLITEMS, sent },
124 { LVM_GETITEMCOUNT, sent },
125 { LVM_INSERTITEM, sent },
126 { LVM_INSERTITEM, sent },
127 { LVM_GETITEMCOUNT, sent },
128 { LVM_INSERTITEM, sent },
129 { LVM_GETITEMCOUNT, sent },
130 { 0 }
131 };
132
133 static const struct message listview_itempos_seq[] = {
134 { LVM_INSERTITEM, sent },
135 { LVM_INSERTITEM, sent },
136 { LVM_INSERTITEM, sent },
137 { LVM_SETITEMPOSITION, sent|wparam|lparam, 1, MAKELPARAM(10,5) },
138 { LVM_GETITEMPOSITION, sent|wparam, 1 },
139 { LVM_SETITEMPOSITION, sent|wparam|lparam, 2, MAKELPARAM(0,0) },
140 { LVM_GETITEMPOSITION, sent|wparam, 2 },
141 { LVM_SETITEMPOSITION, sent|wparam|lparam, 0, MAKELPARAM(20,20) },
142 { LVM_GETITEMPOSITION, sent|wparam, 0 },
143 { 0 }
144 };
145
146 struct subclass_info
147 {
148 WNDPROC oldproc;
149 };
150
151 static LRESULT WINAPI parent_wnd_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
152 {
153 static long defwndproc_counter = 0;
154 LRESULT ret;
155 struct message msg;
156
157 /* do not log painting messages */
158 if (message != WM_PAINT &&
159 message != WM_ERASEBKGND &&
160 message != WM_NCPAINT &&
161 message != WM_NCHITTEST &&
162 message != WM_GETTEXT &&
163 message != WM_GETICON &&
164 message != WM_DEVICECHANGE)
165 {
166 trace("parent: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
167
168 msg.message = message;
169 msg.flags = sent|wparam|lparam;
170 if (defwndproc_counter) msg.flags |= defwinproc;
171 msg.wParam = wParam;
172 msg.lParam = lParam;
173 add_message(sequences, PARENT_SEQ_INDEX, &msg);
174 }
175
176 defwndproc_counter++;
177 ret = DefWindowProcA(hwnd, message, wParam, lParam);
178 defwndproc_counter--;
179
180 return ret;
181 }
182
183 static BOOL register_parent_wnd_class(void)
184 {
185 WNDCLASSA cls;
186
187 cls.style = 0;
188 cls.lpfnWndProc = parent_wnd_proc;
189 cls.cbClsExtra = 0;
190 cls.cbWndExtra = 0;
191 cls.hInstance = GetModuleHandleA(NULL);
192 cls.hIcon = 0;
193 cls.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
194 cls.hbrBackground = GetStockObject(WHITE_BRUSH);
195 cls.lpszMenuName = NULL;
196 cls.lpszClassName = "Listview test parent class";
197 return RegisterClassA(&cls);
198 }
199
200 static HWND create_parent_window(void)
201 {
202 if (!register_parent_wnd_class())
203 return NULL;
204
205 return CreateWindowEx(0, "Listview test parent class",
206 "Listview test parent window",
207 WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
208 WS_MAXIMIZEBOX | WS_VISIBLE,
209 0, 0, 100, 100,
210 GetDesktopWindow(), NULL, GetModuleHandleA(NULL), NULL);
211 }
212
213 static LRESULT WINAPI listview_subclass_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
214 {
215 struct subclass_info *info = (struct subclass_info *)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
216 static long defwndproc_counter = 0;
217 LRESULT ret;
218 struct message msg;
219
220 trace("listview: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
221
222 msg.message = message;
223 msg.flags = sent|wparam|lparam;
224 if (defwndproc_counter) msg.flags |= defwinproc;
225 msg.wParam = wParam;
226 msg.lParam = lParam;
227 msg.id = LISTVIEW_ID;
228 add_message(sequences, LISTVIEW_SEQ_INDEX, &msg);
229
230 defwndproc_counter++;
231 ret = CallWindowProcA(info->oldproc, hwnd, message, wParam, lParam);
232 defwndproc_counter--;
233 return ret;
234 }
235
236 static HWND create_listview_control(void)
237 {
238 struct subclass_info *info;
239 HWND hwnd;
240 RECT rect;
241
242 info = HeapAlloc(GetProcessHeap(), 0, sizeof(struct subclass_info));
243 if (!info)
244 return NULL;
245
246 GetClientRect(hwndparent, &rect);
247 hwnd = CreateWindowExA(0, WC_LISTVIEW, "foo",
248 WS_CHILD | WS_BORDER | WS_VISIBLE | LVS_REPORT,
249 0, 0, rect.right, rect.bottom,
250 hwndparent, NULL, GetModuleHandleA(NULL), NULL);
251 ok(hwnd != NULL, "gle=%d\n", GetLastError());
252
253 if (!hwnd)
254 {
255 HeapFree(GetProcessHeap(), 0, info);
256 return NULL;
257 }
258
259 info->oldproc = (WNDPROC)SetWindowLongPtrA(hwnd, GWLP_WNDPROC,
260 (LONG_PTR)listview_subclass_proc);
261 SetWindowLongPtrA(hwnd, GWLP_USERDATA, (LONG_PTR)info);
262
263 return hwnd;
264 }
265
266 static HWND create_custom_listview_control(DWORD style)
267 {
268 struct subclass_info *info;
269 HWND hwnd;
270 RECT rect;
271
272 info = HeapAlloc(GetProcessHeap(), 0, sizeof(struct subclass_info));
273 if (!info)
274 return NULL;
275
276 GetClientRect(hwndparent, &rect);
277 hwnd = CreateWindowExA(0, WC_LISTVIEW, "foo",
278 WS_CHILD | WS_BORDER | WS_VISIBLE | style,
279 0, 0, rect.right, rect.bottom,
280 hwndparent, NULL, GetModuleHandleA(NULL), NULL);
281 ok(hwnd != NULL, "gle=%d\n", GetLastError());
282
283 if (!hwnd)
284 {
285 HeapFree(GetProcessHeap(), 0, info);
286 return NULL;
287 }
288
289 info->oldproc = (WNDPROC)SetWindowLongPtrA(hwnd, GWLP_WNDPROC,
290 (LONG_PTR)listview_subclass_proc);
291 SetWindowLongPtrA(hwnd, GWLP_USERDATA, (LONG_PTR)info);
292
293 return hwnd;
294 }
295
296 static LRESULT WINAPI header_subclass_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
297 {
298 struct subclass_info *info = (struct subclass_info *)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
299 static long defwndproc_counter = 0;
300 LRESULT ret;
301 struct message msg;
302
303 trace("header: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
304
305 msg.message = message;
306 msg.flags = sent|wparam|lparam;
307 if (defwndproc_counter) msg.flags |= defwinproc;
308 msg.wParam = wParam;
309 msg.lParam = lParam;
310 msg.id = HEADER_ID;
311 add_message(sequences, LISTVIEW_SEQ_INDEX, &msg);
312
313 defwndproc_counter++;
314 ret = CallWindowProcA(info->oldproc, hwnd, message, wParam, lParam);
315 defwndproc_counter--;
316 return ret;
317 }
318
319 static HWND subclass_header(HWND hwndListview)
320 {
321 struct subclass_info *info;
322 HWND hwnd;
323
324 info = HeapAlloc(GetProcessHeap(), 0, sizeof(struct subclass_info));
325 if (!info)
326 return NULL;
327
328 hwnd = ListView_GetHeader(hwndListview);
329 info->oldproc = (WNDPROC)SetWindowLongPtrA(hwnd, GWLP_WNDPROC,
330 (LONG_PTR)header_subclass_proc);
331 SetWindowLongPtrA(hwnd, GWLP_USERDATA, (LONG_PTR)info);
332
333 return hwnd;
334 }
335
336 static void test_images(void)
337 {
338 HWND hwnd;
339 DWORD r;
340 LVITEM item;
341 HIMAGELIST himl;
342 HBITMAP hbmp;
343 RECT r1, r2;
344 static CHAR hello[] = "hello";
345
346 himl = ImageList_Create(40, 40, 0, 4, 4);
347 ok(himl != NULL, "failed to create imagelist\n");
348
349 hbmp = CreateBitmap(40, 40, 1, 1, NULL);
350 ok(hbmp != NULL, "failed to create bitmap\n");
351
352 r = ImageList_Add(himl, hbmp, 0);
353 ok(r == 0, "should be zero\n");
354
355 hwnd = CreateWindowEx(0, "SysListView32", "foo", LVS_OWNERDRAWFIXED,
356 10, 10, 100, 200, hwndparent, NULL, NULL, NULL);
357 ok(hwnd != NULL, "failed to create listview window\n");
358
359 r = SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, 0x940);
360 ok(r == 0, "should return zero\n");
361
362 r = SendMessage(hwnd, LVM_SETIMAGELIST, 0, (LPARAM)himl);
363 ok(r == 0, "should return zero\n");
364
365 r = SendMessage(hwnd, LVM_SETICONSPACING, 0, MAKELONG(100,50));
366 /* returns dimensions */
367
368 r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
369 ok(r == 0, "should be zero items\n");
370
371 item.mask = LVIF_IMAGE | LVIF_TEXT;
372 item.iItem = 0;
373 item.iSubItem = 1;
374 item.iImage = 0;
375 item.pszText = 0;
376 r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
377 ok(r == -1, "should fail\n");
378
379 item.iSubItem = 0;
380 item.pszText = hello;
381 r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
382 ok(r == 0, "should not fail\n");
383
384 memset(&r1, 0, sizeof r1);
385 r1.left = LVIR_ICON;
386 r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM) &r1);
387
388 r = SendMessage(hwnd, LVM_DELETEALLITEMS, 0, 0);
389 ok(r == TRUE, "should not fail\n");
390
391 item.iSubItem = 0;
392 item.pszText = hello;
393 r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
394 ok(r == 0, "should not fail\n");
395
396 memset(&r2, 0, sizeof r2);
397 r2.left = LVIR_ICON;
398 r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM) &r2);
399
400 ok(!memcmp(&r1, &r2, sizeof r1), "rectangle should be the same\n");
401
402 DestroyWindow(hwnd);
403 }
404
405 static void test_checkboxes(void)
406 {
407 HWND hwnd;
408 LVITEMA item;
409 DWORD r;
410 static CHAR text[] = "Text",
411 text2[] = "Text2",
412 text3[] = "Text3";
413
414 hwnd = CreateWindowEx(0, "SysListView32", "foo", LVS_REPORT,
415 10, 10, 100, 200, hwndparent, NULL, NULL, NULL);
416 ok(hwnd != NULL, "failed to create listview window\n");
417
418 /* first without LVS_EX_CHECKBOXES set and an item and check that state is preserved */
419 item.mask = LVIF_TEXT | LVIF_STATE;
420 item.stateMask = 0xffff;
421 item.state = 0xfccc;
422 item.iItem = 0;
423 item.iSubItem = 0;
424 item.pszText = text;
425 r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
426 ok(r == 0, "ret %d\n", r);
427
428 item.iItem = 0;
429 item.mask = LVIF_STATE;
430 item.stateMask = 0xffff;
431 r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
432 ok(item.state == 0xfccc, "state %x\n", item.state);
433
434 /* Don't set LVIF_STATE */
435 item.mask = LVIF_TEXT;
436 item.stateMask = 0xffff;
437 item.state = 0xfccc;
438 item.iItem = 1;
439 item.iSubItem = 0;
440 item.pszText = text;
441 r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
442 ok(r == 1, "ret %d\n", r);
443
444 item.iItem = 1;
445 item.mask = LVIF_STATE;
446 item.stateMask = 0xffff;
447 r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
448 ok(item.state == 0, "state %x\n", item.state);
449
450 r = SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_CHECKBOXES, LVS_EX_CHECKBOXES);
451 ok(r == 0, "should return zero\n");
452
453 /* Having turned on checkboxes, check that all existing items are set to 0x1000 (unchecked) */
454 item.iItem = 0;
455 item.mask = LVIF_STATE;
456 item.stateMask = 0xffff;
457 r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
458 ok(item.state == 0x1ccc, "state %x\n", item.state);
459
460 /* Now add an item without specifying a state and check that its state goes to 0x1000 */
461 item.iItem = 2;
462 item.mask = LVIF_TEXT;
463 item.state = 0;
464 item.pszText = text2;
465 r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
466 ok(r == 2, "ret %d\n", r);
467
468 item.iItem = 2;
469 item.mask = LVIF_STATE;
470 item.stateMask = 0xffff;
471 r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
472 ok(item.state == 0x1000, "state %x\n", item.state);
473
474 /* Add a further item this time specifying a state and still its state goes to 0x1000 */
475 item.iItem = 3;
476 item.mask = LVIF_TEXT | LVIF_STATE;
477 item.stateMask = 0xffff;
478 item.state = 0x2aaa;
479 item.pszText = text3;
480 r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
481 ok(r == 3, "ret %d\n", r);
482
483 item.iItem = 3;
484 item.mask = LVIF_STATE;
485 item.stateMask = 0xffff;
486 r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
487 ok(item.state == 0x1aaa, "state %x\n", item.state);
488
489 /* Set an item's state to checked */
490 item.iItem = 3;
491 item.mask = LVIF_STATE;
492 item.stateMask = 0xf000;
493 item.state = 0x2000;
494 r = SendMessage(hwnd, LVM_SETITEMA, 0, (LPARAM) &item);
495
496 item.iItem = 3;
497 item.mask = LVIF_STATE;
498 item.stateMask = 0xffff;
499 r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
500 ok(item.state == 0x2aaa, "state %x\n", item.state);
501
502 /* Check that only the bits we asked for are returned,
503 * and that all the others are set to zero
504 */
505 item.iItem = 3;
506 item.mask = LVIF_STATE;
507 item.stateMask = 0xf000;
508 item.state = 0xffff;
509 r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
510 ok(item.state == 0x2000, "state %x\n", item.state);
511
512 /* Set the style again and check that doesn't change an item's state */
513 r = SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_CHECKBOXES, LVS_EX_CHECKBOXES);
514 ok(r == LVS_EX_CHECKBOXES, "ret %x\n", r);
515
516 item.iItem = 3;
517 item.mask = LVIF_STATE;
518 item.stateMask = 0xffff;
519 r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
520 ok(item.state == 0x2aaa, "state %x\n", item.state);
521
522 /* Unsetting the checkbox extended style doesn't change an item's state */
523 r = SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_CHECKBOXES, 0);
524 ok(r == LVS_EX_CHECKBOXES, "ret %x\n", r);
525
526 item.iItem = 3;
527 item.mask = LVIF_STATE;
528 item.stateMask = 0xffff;
529 r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
530 ok(item.state == 0x2aaa, "state %x\n", item.state);
531
532 /* Now setting the style again will change an item's state */
533 r = SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_CHECKBOXES, LVS_EX_CHECKBOXES);
534 ok(r == 0, "ret %x\n", r);
535
536 item.iItem = 3;
537 item.mask = LVIF_STATE;
538 item.stateMask = 0xffff;
539 r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
540 ok(item.state == 0x1aaa, "state %x\n", item.state);
541
542 DestroyWindow(hwnd);
543 }
544
545 static void insert_column(HWND hwnd, int idx)
546 {
547 LVCOLUMN column;
548 DWORD rc;
549
550 memset(&column, 0xaa, sizeof(column));
551 column.mask = LVCF_SUBITEM;
552 column.iSubItem = idx;
553
554 rc = ListView_InsertColumn(hwnd, idx, &column);
555 expect(idx, rc);
556 }
557
558 static void insert_item(HWND hwnd, int idx)
559 {
560 static CHAR text[] = "foo";
561
562 LVITEMA item;
563 DWORD rc;
564
565 memset(&item, 0xaa, sizeof (item));
566 item.mask = LVIF_TEXT;
567 item.iItem = idx;
568 item.iSubItem = 0;
569 item.pszText = text;
570
571 rc = ListView_InsertItem(hwnd, &item);
572 expect(idx, rc);
573 }
574
575 static void test_items(void)
576 {
577 const LPARAM lparamTest = 0x42;
578 HWND hwnd;
579 LVITEMA item;
580 DWORD r;
581 static CHAR text[] = "Text";
582
583 hwnd = CreateWindowEx(0, "SysListView32", "foo", LVS_REPORT,
584 10, 10, 100, 200, hwndparent, NULL, NULL, NULL);
585 ok(hwnd != NULL, "failed to create listview window\n");
586
587 /*
588 * Test setting/getting item params
589 */
590
591 /* Set up two columns */
592 insert_column(hwnd, 0);
593 insert_column(hwnd, 1);
594
595 /* Insert an item with just a param */
596 memset (&item, 0xaa, sizeof (item));
597 item.mask = LVIF_PARAM;
598 item.iItem = 0;
599 item.iSubItem = 0;
600 item.lParam = lparamTest;
601 r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
602 ok(r == 0, "ret %d\n", r);
603
604 /* Test getting of the param */
605 memset (&item, 0xaa, sizeof (item));
606 item.mask = LVIF_PARAM;
607 item.iItem = 0;
608 item.iSubItem = 0;
609 r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
610 ok(r != 0, "ret %d\n", r);
611 ok(item.lParam == lparamTest, "got lParam %lx, expected %lx\n", item.lParam, lparamTest);
612
613 /* Set up a subitem */
614 memset (&item, 0xaa, sizeof (item));
615 item.mask = LVIF_TEXT;
616 item.iItem = 0;
617 item.iSubItem = 1;
618 item.pszText = text;
619 r = SendMessage(hwnd, LVM_SETITEMA, 0, (LPARAM) &item);
620 ok(r != 0, "ret %d\n", r);
621
622 /* Query param from subitem: returns main item param */
623 memset (&item, 0xaa, sizeof (item));
624 item.mask = LVIF_PARAM;
625 item.iItem = 0;
626 item.iSubItem = 1;
627 r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
628 ok(r != 0, "ret %d\n", r);
629 ok(item.lParam == lparamTest, "got lParam %lx, expected %lx\n", item.lParam, lparamTest);
630
631 /* Set up param on first subitem: no effect */
632 memset (&item, 0xaa, sizeof (item));
633 item.mask = LVIF_PARAM;
634 item.iItem = 0;
635 item.iSubItem = 1;
636 item.lParam = lparamTest+1;
637 r = SendMessage(hwnd, LVM_SETITEMA, 0, (LPARAM) &item);
638 ok(r == 0, "ret %d\n", r);
639
640 /* Query param from subitem again: should still return main item param */
641 memset (&item, 0xaa, sizeof (item));
642 item.mask = LVIF_PARAM;
643 item.iItem = 0;
644 item.iSubItem = 1;
645 r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
646 ok(r != 0, "ret %d\n", r);
647 ok(item.lParam == lparamTest, "got lParam %lx, expected %lx\n", item.lParam, lparamTest);
648
649 /**** Some tests of state highlighting ****/
650 memset (&item, 0xaa, sizeof (item));
651 item.mask = LVIF_STATE;
652 item.iItem = 0;
653 item.iSubItem = 0;
654 item.state = LVIS_SELECTED;
655 item.stateMask = LVIS_SELECTED | LVIS_DROPHILITED;
656 r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM) &item);
657 ok(r != 0, "ret %d\n", r);
658 item.iSubItem = 1;
659 item.state = LVIS_DROPHILITED;
660 r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM) &item);
661 ok(r != 0, "ret %d\n", r);
662
663 memset (&item, 0xaa, sizeof (item));
664 item.mask = LVIF_STATE;
665 item.iItem = 0;
666 item.iSubItem = 0;
667 item.stateMask = -1;
668 r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
669 ok(r != 0, "ret %d\n", r);
670 ok(item.state == LVIS_SELECTED, "got state %x, expected %x\n", item.state, LVIS_SELECTED);
671 item.iSubItem = 1;
672 r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
673 ok(r != 0, "ret %d\n", r);
674 todo_wine ok(item.state == LVIS_DROPHILITED, "got state %x, expected %x\n", item.state, LVIS_DROPHILITED);
675
676 DestroyWindow(hwnd);
677 }
678
679 static void test_columns(void)
680 {
681 HWND hwnd;
682 LVCOLUMN column;
683 DWORD rc;
684
685 hwnd = CreateWindowEx(0, "SysListView32", "foo", LVS_REPORT,
686 10, 10, 100, 200, hwndparent, NULL, NULL, NULL);
687 ok(hwnd != NULL, "failed to create listview window\n");
688
689 /* Add a column with no mask */
690 memset(&column, 0xaa, sizeof(column));
691 column.mask = 0;
692 rc = ListView_InsertColumn(hwnd, 0, &column);
693 ok(rc==0, "Inserting column with no mask failed with %d\n", rc);
694
695 /* Check its width */
696 rc = ListView_GetColumnWidth(hwnd, 0);
697 ok(rc==10, "Inserting column with no mask failed to set width to 10 with %d\n", rc);
698
699 DestroyWindow(hwnd);
700 }
701 /* test setting imagelist between WM_NCCREATE and WM_CREATE */
702 static WNDPROC listviewWndProc;
703 static HIMAGELIST test_create_imagelist;
704
705 static LRESULT CALLBACK create_test_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
706 {
707 if (uMsg == WM_CREATE)
708 {
709 LPCREATESTRUCT lpcs = (LPCREATESTRUCT)lParam;
710 lpcs->style |= LVS_REPORT;
711 SendMessage(hwnd, LVM_SETIMAGELIST, 0, (LPARAM)test_create_imagelist);
712 }
713 return CallWindowProc(listviewWndProc, hwnd, uMsg, wParam, lParam);
714 }
715
716 static void test_create(void)
717 {
718 HWND hList;
719 HWND hHeader;
720 WNDCLASSEX cls;
721 cls.cbSize = sizeof(WNDCLASSEX);
722 ok(GetClassInfoEx(GetModuleHandle(NULL), "SysListView32", &cls), "GetClassInfoEx failed\n");
723 listviewWndProc = cls.lpfnWndProc;
724 cls.lpfnWndProc = create_test_wndproc;
725 cls.lpszClassName = "MyListView32";
726 ok(RegisterClassEx(&cls), "RegisterClassEx failed\n");
727
728 test_create_imagelist = ImageList_Create(16, 16, 0, 5, 10);
729 hList = CreateWindow("MyListView32", "Test", WS_VISIBLE, 0, 0, 100, 100, NULL, NULL, GetModuleHandle(NULL), 0);
730 ok((HIMAGELIST)SendMessage(hList, LVM_GETIMAGELIST, 0, 0) == test_create_imagelist, "Image list not obtained\n");
731 hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
732 ok(IsWindow(hHeader) && IsWindowVisible(hHeader), "Listview not in report mode\n");
733 DestroyWindow(hList);
734 }
735
736 static void test_redraw(void)
737 {
738 HWND hwnd, hwndheader;
739
740 hwnd = create_listview_control();
741 hwndheader = subclass_header(hwnd);
742
743 flush_sequences(sequences, NUM_MSG_SEQUENCES);
744
745 trace("invalidate & update\n");
746 InvalidateRect(hwnd, NULL, TRUE);
747 UpdateWindow(hwnd);
748 ok_sequence(sequences, LISTVIEW_SEQ_INDEX, redraw_listview_seq, "redraw listview", FALSE);
749
750 flush_sequences(sequences, NUM_MSG_SEQUENCES);
751
752 DestroyWindow(hwnd);
753 }
754
755 static LRESULT WINAPI cd_wndproc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
756 {
757 COLORREF clr, c0ffee = RGB(0xc0, 0xff, 0xee);
758
759 if(msg == WM_NOTIFY) {
760 NMHDR *nmhdr = (PVOID)lp;
761 if(nmhdr->code == NM_CUSTOMDRAW) {
762 NMLVCUSTOMDRAW *nmlvcd = (PVOID)nmhdr;
763 trace("NMCUSTOMDRAW (0x%.8x)\n", nmlvcd->nmcd.dwDrawStage);
764 switch(nmlvcd->nmcd.dwDrawStage) {
765 case CDDS_PREPAINT:
766 SetBkColor(nmlvcd->nmcd.hdc, c0ffee);
767 return CDRF_NOTIFYITEMDRAW;
768 case CDDS_ITEMPREPAINT:
769 nmlvcd->clrTextBk = CLR_DEFAULT;
770 return CDRF_NOTIFYSUBITEMDRAW;
771 case CDDS_ITEMPREPAINT | CDDS_SUBITEM:
772 clr = GetBkColor(nmlvcd->nmcd.hdc);
773 todo_wine ok(clr == c0ffee, "clr=%.8x\n", clr);
774 return CDRF_NOTIFYPOSTPAINT;
775 case CDDS_ITEMPOSTPAINT | CDDS_SUBITEM:
776 clr = GetBkColor(nmlvcd->nmcd.hdc);
777 todo_wine ok(clr == c0ffee, "clr=%.8x\n", clr);
778 return CDRF_DODEFAULT;
779 }
780 return CDRF_DODEFAULT;
781 }
782 }
783
784 return DefWindowProcA(hwnd, msg, wp, lp);
785 }
786
787 static void test_customdraw(void)
788 {
789 HWND hwnd;
790 WNDPROC oldwndproc;
791
792 hwnd = create_listview_control();
793
794 insert_column(hwnd, 0);
795 insert_column(hwnd, 1);
796 insert_item(hwnd, 0);
797
798 oldwndproc = (WNDPROC)SetWindowLongPtr(hwndparent, GWLP_WNDPROC,
799 (LONG_PTR)cd_wndproc);
800
801 InvalidateRect(hwnd, NULL, TRUE);
802 UpdateWindow(hwnd);
803
804 SetWindowLongPtr(hwndparent, GWLP_WNDPROC, (LONG_PTR)oldwndproc);
805
806 DestroyWindow(hwnd);
807 }
808
809 static void test_icon_spacing(void)
810 {
811 /* LVM_SETICONSPACING */
812 /* note: LVM_SETICONSPACING returns the previous icon spacing if successful */
813 /* note: the first test will fail if the default icon spacing is not (43,43) */
814
815 HWND hwnd;
816 DWORD r;
817
818 hwnd = create_custom_listview_control(LVS_ICON);
819 ok(hwnd != NULL, "failed to create a listview window\n");
820
821 flush_sequences(sequences, NUM_MSG_SEQUENCES);
822
823 trace("test icon spacing\n");
824 todo_wine {
825 r = SendMessage(hwnd, LVM_SETICONSPACING, 0, (LPARAM) MAKELONG(20, 30));
826 expect(MAKELONG(43,43), r);
827 }
828 r = SendMessage(hwnd, LVM_SETICONSPACING, 0, (LPARAM) MAKELONG(25, 35));
829 expect(MAKELONG(20,30), r);
830 r = SendMessage(hwnd, LVM_SETICONSPACING, 0, (LPARAM) MAKELONG(-1,-1));
831 expect(MAKELONG(25,35), r);
832
833 ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_icon_spacing_seq, "test icon spacing seq", FALSE);
834
835 flush_sequences(sequences, NUM_MSG_SEQUENCES);
836 DestroyWindow(hwnd);
837 }
838
839 static void test_color(void)
840 {
841 /* SETBKCOLOR/GETBKCOLOR, SETTEXTCOLOR/GETTEXTCOLOR, SETTEXTBKCOLOR/GETTEXTBKCOLOR */
842
843 HWND hwnd;
844 DWORD r;
845 int i;
846
847 COLORREF color;
848 COLORREF colors[4] = {RGB(0,0,0), RGB(100,50,200), CLR_NONE, RGB(255,255,255)};
849
850 hwnd = create_listview_control();
851 ok(hwnd != NULL, "failed to create a listview window\n");
852
853 flush_sequences(sequences, NUM_MSG_SEQUENCES);
854
855 trace("test color seq\n");
856 for (i = 0; i < 4; i++)
857 {
858 color = colors[i];
859
860 r = SendMessage(hwnd, LVM_SETBKCOLOR, 0, color);
861 expect(TRUE, r);
862 r = SendMessage(hwnd, LVM_GETBKCOLOR, 0, color);
863 expect(color, r);
864
865 r = SendMessage(hwnd, LVM_SETTEXTCOLOR, 0, color);
866 expect (TRUE, r);
867 r = SendMessage(hwnd, LVM_GETTEXTCOLOR, 0, color);
868 expect(color, r);
869
870 r = SendMessage(hwnd, LVM_SETTEXTBKCOLOR, 0, color);
871 expect(TRUE, r);
872 r = SendMessage(hwnd, LVM_GETTEXTBKCOLOR, 0, color);
873 expect(color, r);
874 }
875
876 ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_color_seq, "test color seq", FALSE);
877
878 flush_sequences(sequences, NUM_MSG_SEQUENCES);
879 DestroyWindow(hwnd);
880 }
881
882 static void test_item_count(void)
883 {
884 /* LVM_INSERTITEM, LVM_DELETEITEM, LVM_DELETEALLITEMS, LVM_GETITEMCOUNT */
885
886 HWND hwnd;
887 DWORD r;
888
889 LVITEM item0;
890 LVITEM item1;
891 LVITEM item2;
892 static CHAR item0text[] = "item0";
893 static CHAR item1text[] = "item1";
894 static CHAR item2text[] = "item2";
895
896 hwnd = create_listview_control();
897 ok(hwnd != NULL, "failed to create a listview window\n");
898
899 flush_sequences(sequences, NUM_MSG_SEQUENCES);
900
901 trace("test item count\n");
902
903 r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
904 expect(0, r);
905
906 /* [item0] */
907 item0.mask = LVIF_TEXT;
908 item0.iItem = 0;
909 item0.iSubItem = 0;
910 item0.pszText = item0text;
911 r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item0);
912 expect(0, r);
913
914 /* [item0, item1] */
915 item1.mask = LVIF_TEXT;
916 item1.iItem = 1;
917 item1.iSubItem = 0;
918 item1.pszText = item1text;
919 r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item1);
920 expect(1, r);
921
922 /* [item0, item1, item2] */
923 item2.mask = LVIF_TEXT;
924 item2.iItem = 2;
925 item2.iSubItem = 0;
926 item2.pszText = item2text;
927 r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item2);
928 expect(2, r);
929
930 r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
931 expect(3, r);
932
933 /* [item0, item1] */
934 r = SendMessage(hwnd, LVM_DELETEITEM, (WPARAM) 2, 0);
935 expect(TRUE, r);
936
937 r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
938 expect(2, r);
939
940 /* [] */
941 r = SendMessage(hwnd, LVM_DELETEALLITEMS, 0, 0);
942 expect(TRUE, r);
943
944 r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
945 expect(0, r);
946
947 /* [item0] */
948 r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item1);
949 expect(0, r);
950
951 /* [item0, item1] */
952 r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item1);
953 expect(1, r);
954
955 r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
956 expect(2, r);
957
958 /* [item0, item1, item2] */
959 r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item2);
960 expect(2, r);
961
962 r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
963 expect(3, r);
964
965 ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_item_count_seq, "test item count seq", FALSE);
966
967 flush_sequences(sequences, NUM_MSG_SEQUENCES);
968 DestroyWindow(hwnd);
969 }
970
971 static void test_item_position(void)
972 {
973 /* LVM_SETITEMPOSITION/LVM_GETITEMPOSITION */
974
975 HWND hwnd;
976 DWORD r;
977 POINT position;
978
979 LVITEM item0;
980 LVITEM item1;
981 LVITEM item2;
982 static CHAR item0text[] = "item0";
983 static CHAR item1text[] = "item1";
984 static CHAR item2text[] = "item2";
985
986 hwnd = create_custom_listview_control(LVS_ICON);
987 ok(hwnd != NULL, "failed to create a listview window\n");
988
989 flush_sequences(sequences, NUM_MSG_SEQUENCES);
990
991 trace("test item position\n");
992
993 /* [item0] */
994 item0.mask = LVIF_TEXT;
995 item0.iItem = 0;
996 item0.iSubItem = 0;
997 item0.pszText = item0text;
998 r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item0);
999 expect(0, r);
1000
1001 /* [item0, item1] */
1002 item1.mask = LVIF_TEXT;
1003 item1.iItem = 1;
1004 item1.iSubItem = 0;
1005 item1.pszText = item1text;
1006 r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item1);
1007 expect(1, r);
1008
1009 /* [item0, item1, item2] */
1010 item2.mask = LVIF_TEXT;
1011 item2.iItem = 2;
1012 item2.iSubItem = 0;
1013 item2.pszText = item2text;
1014 r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item2);
1015 expect(2, r);
1016
1017 r = SendMessage(hwnd, LVM_SETITEMPOSITION, 1, MAKELPARAM(10,5));
1018 expect(TRUE, r);
1019 r = SendMessage(hwnd, LVM_GETITEMPOSITION, 1, (LPARAM) &position);
1020 expect(TRUE, r);
1021 expect2(10, 5, position.x, position.y);
1022
1023 r = SendMessage(hwnd, LVM_SETITEMPOSITION, 2, MAKELPARAM(0,0));
1024 expect(TRUE, r);
1025 r = SendMessage(hwnd, LVM_GETITEMPOSITION, 2, (LPARAM) &position);
1026 expect(TRUE, r);
1027 expect2(0, 0, position.x, position.y);
1028
1029 r = SendMessage(hwnd, LVM_SETITEMPOSITION, 0, MAKELPARAM(20,20));
1030 expect(TRUE, r);
1031 r = SendMessage(hwnd, LVM_GETITEMPOSITION, 0, (LPARAM) &position);
1032 expect(TRUE, r);
1033 expect2(20, 20, position.x, position.y);
1034
1035 ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_itempos_seq, "test item position seq", TRUE);
1036
1037 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1038 DestroyWindow(hwnd);
1039 }
1040
1041 START_TEST(listview)
1042 {
1043 INITCOMMONCONTROLSEX icc;
1044
1045 icc.dwICC = 0;
1046 icc.dwSize = sizeof icc;
1047 InitCommonControlsEx(&icc);
1048
1049 init_msg_sequences(sequences, NUM_MSG_SEQUENCES);
1050
1051 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1052 hwndparent = create_parent_window();
1053 ok_sequence(sequences, PARENT_SEQ_INDEX, create_parent_wnd_seq, "create parent window", TRUE);
1054 flush_sequences(sequences, NUM_MSG_SEQUENCES);
1055
1056 test_images();
1057 test_checkboxes();
1058 test_items();
1059 test_create();
1060 test_redraw();
1061 test_customdraw();
1062 test_icon_spacing();
1063 test_color();
1064 test_item_count();
1065 test_item_position();
1066 test_columns();
1067 }