comctl32/tests: Tests for LVM_GETCOLUMNORDERARRAY: it actually uses HDM_GETORDERARRAY.
[wine] / dlls / comctl32 / tests / 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_QUERYNEWPALETTE,   sent|optional },
52     { WM_WINDOWPOSCHANGING, sent|wparam, 0 },
53     { WM_WINDOWPOSCHANGED,  sent|optional },
54     { WM_NCCALCSIZE,        sent|wparam|optional, 1 },
55     { WM_ACTIVATEAPP,       sent|wparam, 1 },
56     { WM_NCACTIVATE,        sent|wparam, 1 },
57     { WM_ACTIVATE,          sent|wparam, 1 },
58     { WM_IME_SETCONTEXT,    sent|wparam|defwinproc|optional, 1 },
59     { WM_IME_NOTIFY,        sent|defwinproc|optional },
60     { WM_SETFOCUS,          sent|wparam|defwinproc, 0 },
61     /* Win9x adds SWP_NOZORDER below */
62     { WM_WINDOWPOSCHANGED,  sent, /*|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE*/ },
63     { WM_NCCALCSIZE,        sent|wparam|optional, 1 },
64     { WM_SIZE,              sent },
65     { WM_MOVE,              sent },
66     { 0 }
67 };
68
69 static const struct message redraw_listview_seq[] = {
70     { WM_PAINT,      sent|id,            0, 0, LISTVIEW_ID },
71     { WM_PAINT,      sent|id,            0, 0, HEADER_ID },
72     { WM_NCPAINT,    sent|id|defwinproc, 0, 0, HEADER_ID },
73     { WM_ERASEBKGND, sent|id|defwinproc, 0, 0, HEADER_ID },
74     { WM_NOTIFY,     sent|id|defwinproc, 0, 0, LISTVIEW_ID },
75     { WM_NCPAINT,    sent|id|defwinproc, 0, 0, LISTVIEW_ID },
76     { WM_ERASEBKGND, sent|id|defwinproc, 0, 0, LISTVIEW_ID },
77     { 0 }
78 };
79
80 static const struct message listview_icon_spacing_seq[] = {
81     { LVM_SETICONSPACING, sent|lparam, 0, MAKELPARAM(20, 30) },
82     { LVM_SETICONSPACING, sent|lparam, 0, MAKELPARAM(25, 35) },
83     { LVM_SETICONSPACING, sent|lparam, 0, MAKELPARAM(-1, -1) },
84     { 0 }
85 };
86
87 static const struct message listview_color_seq[] = {
88     { LVM_SETBKCOLOR,     sent|lparam, 0, RGB(0,0,0) },
89     { LVM_GETBKCOLOR,     sent },
90     { LVM_SETTEXTCOLOR,   sent|lparam, 0, RGB(0,0,0) },
91     { LVM_GETTEXTCOLOR,   sent },
92     { LVM_SETTEXTBKCOLOR, sent|lparam, 0, RGB(0,0,0) },
93     { LVM_GETTEXTBKCOLOR, sent },
94
95     { LVM_SETBKCOLOR,     sent|lparam, 0, RGB(100,50,200) },
96     { LVM_GETBKCOLOR,     sent },
97     { LVM_SETTEXTCOLOR,   sent|lparam, 0, RGB(100,50,200) },
98     { LVM_GETTEXTCOLOR,   sent },
99     { LVM_SETTEXTBKCOLOR, sent|lparam, 0, RGB(100,50,200) },
100     { LVM_GETTEXTBKCOLOR, sent },
101
102     { LVM_SETBKCOLOR,     sent|lparam, 0, CLR_NONE },
103     { LVM_GETBKCOLOR,     sent },
104     { LVM_SETTEXTCOLOR,   sent|lparam, 0, CLR_NONE },
105     { LVM_GETTEXTCOLOR,   sent },
106     { LVM_SETTEXTBKCOLOR, sent|lparam, 0, CLR_NONE },
107     { LVM_GETTEXTBKCOLOR, sent },
108
109     { LVM_SETBKCOLOR,     sent|lparam, 0, RGB(255,255,255) },
110     { LVM_GETBKCOLOR,     sent },
111     { LVM_SETTEXTCOLOR,   sent|lparam, 0, RGB(255,255,255) },
112     { LVM_GETTEXTCOLOR,   sent },
113     { LVM_SETTEXTBKCOLOR, sent|lparam, 0, RGB(255,255,255) },
114     { LVM_GETTEXTBKCOLOR, sent },
115     { 0 }
116 };
117
118 static const struct message listview_item_count_seq[] = {
119     { LVM_GETITEMCOUNT,   sent },
120     { LVM_INSERTITEM,     sent },
121     { LVM_INSERTITEM,     sent },
122     { LVM_INSERTITEM,     sent },
123     { LVM_GETITEMCOUNT,   sent },
124     { LVM_DELETEITEM,     sent|wparam, 2 },
125     { LVM_GETITEMCOUNT,   sent },
126     { LVM_DELETEALLITEMS, sent },
127     { LVM_GETITEMCOUNT,   sent },
128     { LVM_INSERTITEM,     sent },
129     { LVM_INSERTITEM,     sent },
130     { LVM_GETITEMCOUNT,   sent },
131     { LVM_INSERTITEM,     sent },
132     { LVM_GETITEMCOUNT,   sent },
133     { 0 }
134 };
135
136 static const struct message listview_itempos_seq[] = {
137     { LVM_INSERTITEM,      sent },
138     { LVM_INSERTITEM,      sent },
139     { LVM_INSERTITEM,      sent },
140     { LVM_SETITEMPOSITION, sent|wparam|lparam, 1, MAKELPARAM(10,5) },
141     { LVM_GETITEMPOSITION, sent|wparam,        1 },
142     { LVM_SETITEMPOSITION, sent|wparam|lparam, 2, MAKELPARAM(0,0) },
143     { LVM_GETITEMPOSITION, sent|wparam,        2 },
144     { LVM_SETITEMPOSITION, sent|wparam|lparam, 0, MAKELPARAM(20,20) },
145     { LVM_GETITEMPOSITION, sent|wparam,        0 },
146     { 0 }
147 };
148
149 static const struct message listview_ownerdata_switchto_seq[] = {
150     { WM_STYLECHANGING,    sent },
151     { WM_STYLECHANGED,     sent },
152     { 0 }
153 };
154
155 static const struct message listview_getorderarray_seq[] = {
156     { LVM_GETCOLUMNORDERARRAY, sent|id|wparam, 2, 0, LISTVIEW_ID },
157     { HDM_GETORDERARRAY,       sent|id|wparam, 2, 0, HEADER_ID },
158     { 0 }
159 };
160
161 struct subclass_info
162 {
163     WNDPROC oldproc;
164 };
165
166 static LRESULT WINAPI parent_wnd_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
167 {
168     static LONG defwndproc_counter = 0;
169     LRESULT ret;
170     struct message msg;
171
172     /* log system messages, except for painting */
173     if (message < WM_USER &&
174         message != WM_PAINT &&
175         message != WM_ERASEBKGND &&
176         message != WM_NCPAINT &&
177         message != WM_NCHITTEST &&
178         message != WM_GETTEXT &&
179         message != WM_GETICON &&
180         message != WM_DEVICECHANGE)
181     {
182         trace("parent: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
183
184         msg.message = message;
185         msg.flags = sent|wparam|lparam;
186         if (defwndproc_counter) msg.flags |= defwinproc;
187         msg.wParam = wParam;
188         msg.lParam = lParam;
189         add_message(sequences, PARENT_SEQ_INDEX, &msg);
190     }
191
192     defwndproc_counter++;
193     ret = DefWindowProcA(hwnd, message, wParam, lParam);
194     defwndproc_counter--;
195
196     return ret;
197 }
198
199 static BOOL register_parent_wnd_class(void)
200 {
201     WNDCLASSA cls;
202
203     cls.style = 0;
204     cls.lpfnWndProc = parent_wnd_proc;
205     cls.cbClsExtra = 0;
206     cls.cbWndExtra = 0;
207     cls.hInstance = GetModuleHandleA(NULL);
208     cls.hIcon = 0;
209     cls.hCursor = LoadCursorA(0, IDC_ARROW);
210     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
211     cls.lpszMenuName = NULL;
212     cls.lpszClassName = "Listview test parent class";
213     return RegisterClassA(&cls);
214 }
215
216 static HWND create_parent_window(void)
217 {
218     if (!register_parent_wnd_class())
219         return NULL;
220
221     return CreateWindowEx(0, "Listview test parent class",
222                           "Listview test parent window",
223                           WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
224                           WS_MAXIMIZEBOX | WS_VISIBLE,
225                           0, 0, 100, 100,
226                           GetDesktopWindow(), NULL, GetModuleHandleA(NULL), NULL);
227 }
228
229 static LRESULT WINAPI listview_subclass_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
230 {
231     struct subclass_info *info = (struct subclass_info *)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
232     static LONG defwndproc_counter = 0;
233     LRESULT ret;
234     struct message msg;
235
236     trace("listview: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
237
238     /* some debug output for style changing */
239     if ((message == WM_STYLECHANGING ||
240          message == WM_STYLECHANGED) && lParam)
241     {
242         STYLESTRUCT *style = (STYLESTRUCT*)lParam;
243         trace("\told style: 0x%08x, new style: 0x%08x\n", style->styleOld, style->styleNew);
244     }
245
246     msg.message = message;
247     msg.flags = sent|wparam|lparam;
248     if (defwndproc_counter) msg.flags |= defwinproc;
249     msg.wParam = wParam;
250     msg.lParam = lParam;
251     msg.id = LISTVIEW_ID;
252     add_message(sequences, LISTVIEW_SEQ_INDEX, &msg);
253
254     defwndproc_counter++;
255     ret = CallWindowProcA(info->oldproc, hwnd, message, wParam, lParam);
256     defwndproc_counter--;
257     return ret;
258 }
259
260 static HWND create_listview_control(DWORD style)
261 {
262     struct subclass_info *info;
263     HWND hwnd;
264     RECT rect;
265
266     info = HeapAlloc(GetProcessHeap(), 0, sizeof(struct subclass_info));
267     if (!info)
268         return NULL;
269
270     GetClientRect(hwndparent, &rect);
271     hwnd = CreateWindowExA(0, WC_LISTVIEW, "foo",
272                            WS_CHILD | WS_BORDER | WS_VISIBLE | LVS_REPORT | style,
273                            0, 0, rect.right, rect.bottom,
274                            hwndparent, NULL, GetModuleHandleA(NULL), NULL);
275     ok(hwnd != NULL, "gle=%d\n", GetLastError());
276
277     if (!hwnd)
278     {
279         HeapFree(GetProcessHeap(), 0, info);
280         return NULL;
281     }
282
283     info->oldproc = (WNDPROC)SetWindowLongPtrA(hwnd, GWLP_WNDPROC,
284                                             (LONG_PTR)listview_subclass_proc);
285     SetWindowLongPtrA(hwnd, GWLP_USERDATA, (LONG_PTR)info);
286
287     return hwnd;
288 }
289
290 static HWND create_custom_listview_control(DWORD style)
291 {
292     struct subclass_info *info;
293     HWND hwnd;
294     RECT rect;
295
296     info = HeapAlloc(GetProcessHeap(), 0, sizeof(struct subclass_info));
297     if (!info)
298         return NULL;
299
300     GetClientRect(hwndparent, &rect);
301     hwnd = CreateWindowExA(0, WC_LISTVIEW, "foo",
302                            WS_CHILD | WS_BORDER | WS_VISIBLE | style,
303                            0, 0, rect.right, rect.bottom,
304                            hwndparent, NULL, GetModuleHandleA(NULL), NULL);
305     ok(hwnd != NULL, "gle=%d\n", GetLastError());
306
307     if (!hwnd)
308     {
309         HeapFree(GetProcessHeap(), 0, info);
310         return NULL;
311     }
312
313     info->oldproc = (WNDPROC)SetWindowLongPtrA(hwnd, GWLP_WNDPROC,
314                                             (LONG_PTR)listview_subclass_proc);
315     SetWindowLongPtrA(hwnd, GWLP_USERDATA, (LONG_PTR)info);
316
317     return hwnd;
318 }
319
320 static LRESULT WINAPI header_subclass_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
321 {
322     struct subclass_info *info = (struct subclass_info *)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
323     static LONG defwndproc_counter = 0;
324     LRESULT ret;
325     struct message msg;
326
327     trace("header: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
328
329     msg.message = message;
330     msg.flags = sent|wparam|lparam;
331     if (defwndproc_counter) msg.flags |= defwinproc;
332     msg.wParam = wParam;
333     msg.lParam = lParam;
334     msg.id = HEADER_ID;
335     add_message(sequences, LISTVIEW_SEQ_INDEX, &msg);
336
337     defwndproc_counter++;
338     ret = CallWindowProcA(info->oldproc, hwnd, message, wParam, lParam);
339     defwndproc_counter--;
340     return ret;
341 }
342
343 static HWND subclass_header(HWND hwndListview)
344 {
345     struct subclass_info *info;
346     HWND hwnd;
347
348     info = HeapAlloc(GetProcessHeap(), 0, sizeof(struct subclass_info));
349     if (!info)
350         return NULL;
351
352     hwnd = ListView_GetHeader(hwndListview);
353     info->oldproc = (WNDPROC)SetWindowLongPtrA(hwnd, GWLP_WNDPROC,
354                                             (LONG_PTR)header_subclass_proc);
355     SetWindowLongPtrA(hwnd, GWLP_USERDATA, (LONG_PTR)info);
356
357     return hwnd;
358 }
359
360 static void test_images(void)
361 {
362     HWND hwnd;
363     DWORD r;
364     LVITEM item;
365     HIMAGELIST himl;
366     HBITMAP hbmp;
367     RECT r1, r2;
368     static CHAR hello[] = "hello";
369
370     himl = ImageList_Create(40, 40, 0, 4, 4);
371     ok(himl != NULL, "failed to create imagelist\n");
372
373     hbmp = CreateBitmap(40, 40, 1, 1, NULL);
374     ok(hbmp != NULL, "failed to create bitmap\n");
375
376     r = ImageList_Add(himl, hbmp, 0);
377     ok(r == 0, "should be zero\n");
378
379     hwnd = CreateWindowEx(0, "SysListView32", "foo", LVS_OWNERDRAWFIXED, 
380                 10, 10, 100, 200, hwndparent, NULL, NULL, NULL);
381     ok(hwnd != NULL, "failed to create listview window\n");
382
383     r = SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, 0x940);
384     ok(r == 0, "should return zero\n");
385
386     r = SendMessage(hwnd, LVM_SETIMAGELIST, 0, (LPARAM)himl);
387     ok(r == 0, "should return zero\n");
388
389     r = SendMessage(hwnd, LVM_SETICONSPACING, 0, MAKELONG(100,50));
390     /* returns dimensions */
391
392     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
393     ok(r == 0, "should be zero items\n");
394
395     item.mask = LVIF_IMAGE | LVIF_TEXT;
396     item.iItem = 0;
397     item.iSubItem = 1;
398     item.iImage = 0;
399     item.pszText = 0;
400     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
401     ok(r == -1, "should fail\n");
402
403     item.iSubItem = 0;
404     item.pszText = hello;
405     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
406     ok(r == 0, "should not fail\n");
407
408     memset(&r1, 0, sizeof r1);
409     r1.left = LVIR_ICON;
410     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM) &r1);
411
412     r = SendMessage(hwnd, LVM_DELETEALLITEMS, 0, 0);
413     ok(r == TRUE, "should not fail\n");
414
415     item.iSubItem = 0;
416     item.pszText = hello;
417     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
418     ok(r == 0, "should not fail\n");
419
420     memset(&r2, 0, sizeof r2);
421     r2.left = LVIR_ICON;
422     r = SendMessage(hwnd, LVM_GETITEMRECT, 0, (LPARAM) &r2);
423
424     ok(!memcmp(&r1, &r2, sizeof r1), "rectangle should be the same\n");
425
426     DestroyWindow(hwnd);
427 }
428
429 static void test_checkboxes(void)
430 {
431     HWND hwnd;
432     LVITEMA item;
433     DWORD r;
434     static CHAR text[]  = "Text",
435                 text2[] = "Text2",
436                 text3[] = "Text3";
437
438     hwnd = CreateWindowEx(0, "SysListView32", "foo", LVS_REPORT, 
439                 10, 10, 100, 200, hwndparent, NULL, NULL, NULL);
440     ok(hwnd != NULL, "failed to create listview window\n");
441
442     /* first without LVS_EX_CHECKBOXES set and an item and check that state is preserved */
443     item.mask = LVIF_TEXT | LVIF_STATE;
444     item.stateMask = 0xffff;
445     item.state = 0xfccc;
446     item.iItem = 0;
447     item.iSubItem = 0;
448     item.pszText = text;
449     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
450     ok(r == 0, "ret %d\n", r);
451
452     item.iItem = 0;
453     item.mask = LVIF_STATE;
454     item.stateMask = 0xffff;
455     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
456     ok(item.state == 0xfccc, "state %x\n", item.state);
457
458     /* Don't set LVIF_STATE */
459     item.mask = LVIF_TEXT;
460     item.stateMask = 0xffff;
461     item.state = 0xfccc;
462     item.iItem = 1;
463     item.iSubItem = 0;
464     item.pszText = text;
465     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
466     ok(r == 1, "ret %d\n", r);
467
468     item.iItem = 1;
469     item.mask = LVIF_STATE;
470     item.stateMask = 0xffff;
471     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
472     ok(item.state == 0, "state %x\n", item.state);
473
474     r = SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_CHECKBOXES, LVS_EX_CHECKBOXES);
475     ok(r == 0, "should return zero\n");
476
477     /* Having turned on checkboxes, check that all existing items are set to 0x1000 (unchecked) */
478     item.iItem = 0;
479     item.mask = LVIF_STATE;
480     item.stateMask = 0xffff;
481     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
482     ok(item.state == 0x1ccc, "state %x\n", item.state);
483
484     /* Now add an item without specifying a state and check that its state goes to 0x1000 */
485     item.iItem = 2;
486     item.mask = LVIF_TEXT;
487     item.state = 0;
488     item.pszText = text2;
489     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
490     ok(r == 2, "ret %d\n", r);
491
492     item.iItem = 2;
493     item.mask = LVIF_STATE;
494     item.stateMask = 0xffff;
495     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
496     ok(item.state == 0x1000, "state %x\n", item.state);
497
498     /* Add a further item this time specifying a state and still its state goes to 0x1000 */
499     item.iItem = 3;
500     item.mask = LVIF_TEXT | LVIF_STATE;
501     item.stateMask = 0xffff;
502     item.state = 0x2aaa;
503     item.pszText = text3;
504     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
505     ok(r == 3, "ret %d\n", r);
506
507     item.iItem = 3;
508     item.mask = LVIF_STATE;
509     item.stateMask = 0xffff;
510     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
511     ok(item.state == 0x1aaa, "state %x\n", item.state);
512
513     /* Set an item's state to checked */
514     item.iItem = 3;
515     item.mask = LVIF_STATE;
516     item.stateMask = 0xf000;
517     item.state = 0x2000;
518     r = SendMessage(hwnd, LVM_SETITEMA, 0, (LPARAM) &item);
519
520     item.iItem = 3;
521     item.mask = LVIF_STATE;
522     item.stateMask = 0xffff;
523     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
524     ok(item.state == 0x2aaa, "state %x\n", item.state);
525
526     /* Check that only the bits we asked for are returned,
527      * and that all the others are set to zero
528      */
529     item.iItem = 3;
530     item.mask = LVIF_STATE;
531     item.stateMask = 0xf000;
532     item.state = 0xffff;
533     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
534     ok(item.state == 0x2000, "state %x\n", item.state);
535
536     /* Set the style again and check that doesn't change an item's state */
537     r = SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_CHECKBOXES, LVS_EX_CHECKBOXES);
538     ok(r == LVS_EX_CHECKBOXES, "ret %x\n", r);
539
540     item.iItem = 3;
541     item.mask = LVIF_STATE;
542     item.stateMask = 0xffff;
543     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
544     ok(item.state == 0x2aaa, "state %x\n", item.state);
545
546     /* Unsetting the checkbox extended style doesn't change an item's state */
547     r = SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_CHECKBOXES, 0);
548     ok(r == LVS_EX_CHECKBOXES, "ret %x\n", r);
549
550     item.iItem = 3;
551     item.mask = LVIF_STATE;
552     item.stateMask = 0xffff;
553     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
554     ok(item.state == 0x2aaa, "state %x\n", item.state);
555
556     /* Now setting the style again will change an item's state */
557     r = SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_CHECKBOXES, LVS_EX_CHECKBOXES);
558     ok(r == 0, "ret %x\n", r);
559
560     item.iItem = 3;
561     item.mask = LVIF_STATE;
562     item.stateMask = 0xffff;
563     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
564     ok(item.state == 0x1aaa, "state %x\n", item.state);
565
566     /* Toggle checkbox tests (bug 9934) */
567     memset (&item, 0xcc, sizeof(item));
568     item.mask = LVIF_STATE;
569     item.iItem = 3;
570     item.iSubItem = 0;
571     item.state = LVIS_FOCUSED;
572     item.stateMask = LVIS_FOCUSED;
573     r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM) &item);
574     expect(1, r);
575
576     item.iItem = 3;
577     item.mask = LVIF_STATE;
578     item.stateMask = 0xffff;
579     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
580     ok(item.state == 0x1aab, "state %x\n", item.state);
581
582     r = SendMessage(hwnd, WM_KEYDOWN, VK_SPACE, 0);
583     expect(0, r);
584     r = SendMessage(hwnd, WM_KEYUP, VK_SPACE, 0);
585     expect(0, r);
586
587     item.iItem = 3;
588     item.mask = LVIF_STATE;
589     item.stateMask = 0xffff;
590     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
591     ok(item.state == 0x2aab, "state %x\n", item.state);
592
593     r = SendMessage(hwnd, WM_KEYDOWN, VK_SPACE, 0);
594     expect(0, r);
595     r = SendMessage(hwnd, WM_KEYUP, VK_SPACE, 0);
596     expect(0, r);
597
598     item.iItem = 3;
599     item.mask = LVIF_STATE;
600     item.stateMask = 0xffff;
601     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
602     ok(item.state == 0x1aab, "state %x\n", item.state);
603
604     DestroyWindow(hwnd);
605 }
606
607 static void insert_column(HWND hwnd, int idx)
608 {
609     LVCOLUMN column;
610     DWORD rc;
611
612     memset(&column, 0xcc, sizeof(column));
613     column.mask = LVCF_SUBITEM;
614     column.iSubItem = idx;
615
616     rc = ListView_InsertColumn(hwnd, idx, &column);
617     expect(idx, rc);
618 }
619
620 static void insert_item(HWND hwnd, int idx)
621 {
622     static CHAR text[] = "foo";
623
624     LVITEMA item;
625     DWORD rc;
626
627     memset(&item, 0xcc, sizeof (item));
628     item.mask = LVIF_TEXT;
629     item.iItem = idx;
630     item.iSubItem = 0;
631     item.pszText = text;
632
633     rc = ListView_InsertItem(hwnd, &item);
634     expect(idx, rc);
635 }
636
637 static void test_items(void)
638 {
639     const LPARAM lparamTest = 0x42;
640     HWND hwnd;
641     LVITEMA item;
642     DWORD r;
643     static CHAR text[] = "Text";
644
645     hwnd = CreateWindowEx(0, "SysListView32", "foo", LVS_REPORT,
646                 10, 10, 100, 200, hwndparent, NULL, NULL, NULL);
647     ok(hwnd != NULL, "failed to create listview window\n");
648
649     /*
650      * Test setting/getting item params
651      */
652
653     /* Set up two columns */
654     insert_column(hwnd, 0);
655     insert_column(hwnd, 1);
656
657     /* LVIS_SELECTED with zero stateMask */
658     /* set */
659     memset (&item, 0, sizeof (item));
660     item.mask = LVIF_STATE;
661     item.state = LVIS_SELECTED;
662     item.stateMask = 0;
663     item.iItem = 0;
664     item.iSubItem = 0;
665     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
666     ok(r == 0, "ret %d\n", r);
667     /* get */
668     memset (&item, 0xcc, sizeof (item));
669     item.mask = LVIF_STATE;
670     item.stateMask = LVIS_SELECTED;
671     item.state = 0;
672     item.iItem = 0;
673     item.iSubItem = 0;
674     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
675     ok(r != 0, "ret %d\n", r);
676     ok(item.state & LVIS_SELECTED, "Expected LVIS_SELECTED\n");
677     SendMessage(hwnd, LVM_DELETEITEM, 0, 0);
678
679     /* LVIS_SELECTED with zero stateMask */
680     /* set */
681     memset (&item, 0, sizeof (item));
682     item.mask = LVIF_STATE;
683     item.state = LVIS_FOCUSED;
684     item.stateMask = 0;
685     item.iItem = 0;
686     item.iSubItem = 0;
687     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
688     ok(r == 0, "ret %d\n", r);
689     /* get */
690     memset (&item, 0xcc, sizeof (item));
691     item.mask = LVIF_STATE;
692     item.stateMask = LVIS_FOCUSED;
693     item.state = 0;
694     item.iItem = 0;
695     item.iSubItem = 0;
696     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
697     ok(r != 0, "ret %d\n", r);
698     ok(item.state & LVIS_FOCUSED, "Expected LVIS_FOCUSED\n");
699     SendMessage(hwnd, LVM_DELETEITEM, 0, 0);
700
701     /* LVIS_CUT with LVIS_FOCUSED stateMask */
702     /* set */
703     memset (&item, 0, sizeof (item));
704     item.mask = LVIF_STATE;
705     item.state = LVIS_CUT;
706     item.stateMask = LVIS_FOCUSED;
707     item.iItem = 0;
708     item.iSubItem = 0;
709     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
710     ok(r == 0, "ret %d\n", r);
711     /* get */
712     memset (&item, 0xcc, sizeof (item));
713     item.mask = LVIF_STATE;
714     item.stateMask = LVIS_CUT;
715     item.state = 0;
716     item.iItem = 0;
717     item.iSubItem = 0;
718     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
719     ok(r != 0, "ret %d\n", r);
720     ok(item.state & LVIS_CUT, "Expected LVIS_CUT\n");
721     SendMessage(hwnd, LVM_DELETEITEM, 0, 0);
722
723     /* Insert an item with just a param */
724     memset (&item, 0xcc, sizeof (item));
725     item.mask = LVIF_PARAM;
726     item.iItem = 0;
727     item.iSubItem = 0;
728     item.lParam = lparamTest;
729     r = SendMessage(hwnd, LVM_INSERTITEMA, 0, (LPARAM) &item);
730     ok(r == 0, "ret %d\n", r);
731
732     /* Test getting of the param */
733     memset (&item, 0xcc, sizeof (item));
734     item.mask = LVIF_PARAM;
735     item.iItem = 0;
736     item.iSubItem = 0;
737     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
738     ok(r != 0, "ret %d\n", r);
739     ok(item.lParam == lparamTest, "got lParam %lx, expected %lx\n", item.lParam, lparamTest);
740
741     /* Set up a subitem */
742     memset (&item, 0xcc, sizeof (item));
743     item.mask = LVIF_TEXT;
744     item.iItem = 0;
745     item.iSubItem = 1;
746     item.pszText = text;
747     r = SendMessage(hwnd, LVM_SETITEMA, 0, (LPARAM) &item);
748     ok(r != 0, "ret %d\n", r);
749
750     /* Query param from subitem: returns main item param */
751     memset (&item, 0xcc, sizeof (item));
752     item.mask = LVIF_PARAM;
753     item.iItem = 0;
754     item.iSubItem = 1;
755     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
756     ok(r != 0, "ret %d\n", r);
757     ok(item.lParam == lparamTest, "got lParam %lx, expected %lx\n", item.lParam, lparamTest);
758
759     /* Set up param on first subitem: no effect */
760     memset (&item, 0xcc, sizeof (item));
761     item.mask = LVIF_PARAM;
762     item.iItem = 0;
763     item.iSubItem = 1;
764     item.lParam = lparamTest+1;
765     r = SendMessage(hwnd, LVM_SETITEMA, 0, (LPARAM) &item);
766     ok(r == 0, "ret %d\n", r);
767
768     /* Query param from subitem again: should still return main item param */
769     memset (&item, 0xcc, sizeof (item));
770     item.mask = LVIF_PARAM;
771     item.iItem = 0;
772     item.iSubItem = 1;
773     r = SendMessage(hwnd, LVM_GETITEMA, 0, (LPARAM) &item);
774     ok(r != 0, "ret %d\n", r);
775     ok(item.lParam == lparamTest, "got lParam %lx, expected %lx\n", item.lParam, lparamTest);
776
777     /**** Some tests of state highlighting ****/
778     memset (&item, 0xcc, sizeof (item));
779     item.mask = LVIF_STATE;
780     item.iItem = 0;
781     item.iSubItem = 0;
782     item.state = LVIS_SELECTED;
783     item.stateMask = LVIS_SELECTED | LVIS_DROPHILITED;
784     r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM) &item);
785     ok(r != 0, "ret %d\n", r);
786     item.iSubItem = 1;
787     item.state = LVIS_DROPHILITED;
788     r = SendMessage(hwnd, LVM_SETITEM, 0, (LPARAM) &item);
789     ok(r != 0, "ret %d\n", r);
790
791     memset (&item, 0xcc, sizeof (item));
792     item.mask = LVIF_STATE;
793     item.iItem = 0;
794     item.iSubItem = 0;
795     item.stateMask = -1;
796     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
797     ok(r != 0, "ret %d\n", r);
798     ok(item.state == LVIS_SELECTED, "got state %x, expected %x\n", item.state, LVIS_SELECTED);
799     item.iSubItem = 1;
800     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
801     ok(r != 0, "ret %d\n", r);
802     todo_wine ok(item.state == LVIS_DROPHILITED, "got state %x, expected %x\n", item.state, LVIS_DROPHILITED);
803
804     DestroyWindow(hwnd);
805 }
806
807 static void test_columns(void)
808 {
809     HWND hwnd, hwndheader;
810     LVCOLUMN column;
811     DWORD rc;
812     INT order[2];
813
814     hwnd = CreateWindowEx(0, "SysListView32", "foo", LVS_REPORT,
815                 10, 10, 100, 200, hwndparent, NULL, NULL, NULL);
816     ok(hwnd != NULL, "failed to create listview window\n");
817
818     /* Add a column with no mask */
819     memset(&column, 0xcc, sizeof(column));
820     column.mask = 0;
821     rc = ListView_InsertColumn(hwnd, 0, &column);
822     ok(rc==0, "Inserting column with no mask failed with %d\n", rc);
823
824     /* Check its width */
825     rc = ListView_GetColumnWidth(hwnd, 0);
826     ok(rc==10 ||
827        broken(rc==0), /* win9x */
828        "Inserting column with no mask failed to set width to 10 with %d\n", rc);
829
830     DestroyWindow(hwnd);
831
832     /* LVM_GETCOLUMNORDERARRAY */
833     hwnd = create_listview_control(0);
834     hwndheader = subclass_header(hwnd);
835
836     memset(&column, 0, sizeof(column));
837     column.mask = LVCF_WIDTH;
838     column.cx = 100;
839     rc = ListView_InsertColumn(hwnd, 0, &column);
840     ok(rc == 0, "Inserting column failed with %d\n", rc);
841
842     column.cx = 200;
843     rc = ListView_InsertColumn(hwnd, 1, &column);
844     ok(rc == 1, "Inserting column failed with %d\n", rc);
845
846     flush_sequences(sequences, NUM_MSG_SEQUENCES);
847
848     rc = SendMessage(hwnd, LVM_GETCOLUMNORDERARRAY, 2, (LPARAM)&order);
849     ok(rc != 0, "Expected LVM_GETCOLUMNORDERARRAY to succeed\n");
850     ok(order[0] == 0, "Expected order 0, got %d\n", order[0]);
851     ok(order[1] == 1, "Expected order 1, got %d\n", order[1]);
852
853     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_getorderarray_seq, "get order array", TRUE);
854
855     DestroyWindow(hwnd);
856 }
857 /* test setting imagelist between WM_NCCREATE and WM_CREATE */
858 static WNDPROC listviewWndProc;
859 static HIMAGELIST test_create_imagelist;
860
861 static LRESULT CALLBACK create_test_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
862 {
863     LRESULT ret;
864
865     if (uMsg == WM_CREATE)
866     {
867         LPCREATESTRUCT lpcs = (LPCREATESTRUCT)lParam;
868         lpcs->style |= LVS_REPORT;
869     }
870     ret = CallWindowProc(listviewWndProc, hwnd, uMsg, wParam, lParam);
871     if (uMsg == WM_CREATE) SendMessage(hwnd, LVM_SETIMAGELIST, 0, (LPARAM)test_create_imagelist);
872     return ret;
873 }
874
875 static void test_create(void)
876 {
877     HWND hList;
878     HWND hHeader;
879     LONG_PTR ret;
880     LONG r;
881     LVCOLUMNA col;
882     RECT rect;
883     WNDCLASSEX cls;
884     cls.cbSize = sizeof(WNDCLASSEX);
885     ok(GetClassInfoEx(GetModuleHandle(NULL), "SysListView32", &cls), "GetClassInfoEx failed\n");
886     listviewWndProc = cls.lpfnWndProc;
887     cls.lpfnWndProc = create_test_wndproc;
888     cls.lpszClassName = "MyListView32";
889     ok(RegisterClassEx(&cls), "RegisterClassEx failed\n");
890
891     test_create_imagelist = ImageList_Create(16, 16, 0, 5, 10);
892     hList = CreateWindow("MyListView32", "Test", WS_VISIBLE, 0, 0, 100, 100, NULL, NULL, GetModuleHandle(NULL), 0);
893     ok((HIMAGELIST)SendMessage(hList, LVM_GETIMAGELIST, 0, 0) == test_create_imagelist, "Image list not obtained\n");
894     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
895     ok(IsWindow(hHeader) && IsWindowVisible(hHeader), "Listview not in report mode\n");
896     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
897     DestroyWindow(hList);
898
899     /* header isn't created on LVS_ICON and LVS_LIST styles */
900     hList = CreateWindow("SysListView32", "Test", WS_VISIBLE, 0, 0, 100, 100, NULL, NULL,
901                           GetModuleHandle(NULL), 0);
902     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
903     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
904     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
905     /* insert column */
906     memset(&col, 0, sizeof(LVCOLUMNA));
907     col.mask = LVCF_WIDTH;
908     col.cx = 100;
909     r = SendMessage(hList, LVM_INSERTCOLUMN, 0, (LPARAM)&col);
910     ok(r == 0, "Expected 0 column's inserted\n");
911     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
912     ok(IsWindow(hHeader), "Header should be created\n");
913     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
914     DestroyWindow(hList);
915
916     hList = CreateWindow("SysListView32", "Test", WS_VISIBLE|LVS_LIST, 0, 0, 100, 100, NULL, NULL,
917                           GetModuleHandle(NULL), 0);
918     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
919     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
920     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
921     /* insert column */
922     memset(&col, 0, sizeof(LVCOLUMNA));
923     col.mask = LVCF_WIDTH;
924     col.cx = 100;
925     r = SendMessage(hList, LVM_INSERTCOLUMN, 0, (LPARAM)&col);
926     ok(r == 0, "Expected 0 column's inserted\n");
927     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
928     ok(IsWindow(hHeader), "Header should be created\n");
929     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
930     DestroyWindow(hList);
931
932     /* try to switch LVS_ICON -> LVS_REPORT and back LVS_ICON -> LVS_REPORT */
933     hList = CreateWindow("SysListView32", "Test", WS_VISIBLE, 0, 0, 100, 100, NULL, NULL,
934                           GetModuleHandle(NULL), 0);
935     ret = SetWindowLongPtr(hList, GWL_STYLE, GetWindowLongPtr(hList, GWL_STYLE) | LVS_REPORT);
936     ok(ret & WS_VISIBLE, "Style wrong, should have WS_VISIBLE\n");
937     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
938     ok(IsWindow(hHeader), "Header should be created\n");
939     ret = SetWindowLongPtr(hList, GWL_STYLE, GetWindowLong(hList, GWL_STYLE) & ~LVS_REPORT);
940     ok((ret & WS_VISIBLE) && (ret & LVS_REPORT), "Style wrong, should have WS_VISIBLE|LVS_REPORT\n");
941     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
942     ok(IsWindow(hHeader), "Header should be created\n");
943     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
944     DestroyWindow(hList);
945
946     /* try to switch LVS_LIST -> LVS_REPORT and back LVS_LIST -> LVS_REPORT */
947     hList = CreateWindow("SysListView32", "Test", WS_VISIBLE|LVS_LIST, 0, 0, 100, 100, NULL, NULL,
948                           GetModuleHandle(NULL), 0);
949     ret = SetWindowLongPtr(hList, GWL_STYLE,
950                           (GetWindowLongPtr(hList, GWL_STYLE) & ~LVS_LIST) | LVS_REPORT);
951     ok(((ret & WS_VISIBLE) && (ret & LVS_LIST)), "Style wrong, should have WS_VISIBLE|LVS_LIST\n");
952     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
953     ok(IsWindow(hHeader), "Header should be created\n");
954     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
955     ret = SetWindowLongPtr(hList, GWL_STYLE,
956                           (GetWindowLongPtr(hList, GWL_STYLE) & ~LVS_REPORT) | LVS_LIST);
957     ok(((ret & WS_VISIBLE) && (ret & LVS_REPORT)), "Style wrong, should have WS_VISIBLE|LVS_REPORT\n");
958     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
959     ok(IsWindow(hHeader), "Header should be created\n");
960     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
961     DestroyWindow(hList);
962
963     /* LVS_REPORT without WS_VISIBLE */
964     hList = CreateWindow("SysListView32", "Test", LVS_REPORT, 0, 0, 100, 100, NULL, NULL,
965                           GetModuleHandle(NULL), 0);
966     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
967     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
968     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
969     /* insert column */
970     memset(&col, 0, sizeof(LVCOLUMNA));
971     col.mask = LVCF_WIDTH;
972     col.cx = 100;
973     r = SendMessage(hList, LVM_INSERTCOLUMN, 0, (LPARAM)&col);
974     ok(r == 0, "Expected 0 column's inserted\n");
975     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
976     ok(IsWindow(hHeader), "Header should be created\n");
977     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
978     DestroyWindow(hList);
979
980     /* LVS_REPORT without WS_VISIBLE, try to show it */
981     hList = CreateWindow("SysListView32", "Test", LVS_REPORT, 0, 0, 100, 100, NULL, NULL,
982                           GetModuleHandle(NULL), 0);
983     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
984     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
985     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
986     ShowWindow(hList, SW_SHOW);
987     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
988     ok(IsWindow(hHeader), "Header should be created\n");
989     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
990     DestroyWindow(hList);
991
992     /* LVS_REPORT with LVS_NOCOLUMNHEADER */
993     hList = CreateWindow("SysListView32", "Test", LVS_REPORT|LVS_NOCOLUMNHEADER|WS_VISIBLE,
994                           0, 0, 100, 100, NULL, NULL, GetModuleHandle(NULL), 0);
995     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
996     ok(IsWindow(hHeader), "Header should be created\n");
997     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
998     /* HDS_DRAGDROP set by default */
999     ok(GetWindowLongPtr(hHeader, GWL_STYLE) & HDS_DRAGDROP, "Expected header to have HDS_DRAGDROP\n");
1000     DestroyWindow(hList);
1001
1002     /* setting LVS_EX_HEADERDRAGDROP creates header */
1003     hList = CreateWindow("SysListView32", "Test", LVS_REPORT, 0, 0, 100, 100, NULL, NULL,
1004                           GetModuleHandle(NULL), 0);
1005     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
1006     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
1007     SendMessage(hList, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_HEADERDRAGDROP);
1008     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1009     ok(IsWindow(hHeader), "Header should be created\n");
1010     ok(hHeader == GetDlgItem(hList, 0), "Expected header as dialog item\n");
1011     DestroyWindow(hList);
1012
1013     /* requesting header info with LVM_GETSUBITEMRECT doesn't create it */
1014     hList = CreateWindow("SysListView32", "Test", LVS_REPORT, 0, 0, 100, 100, NULL, NULL,
1015                           GetModuleHandle(NULL), 0);
1016     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
1017     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
1018
1019     rect.left = LVIR_BOUNDS;
1020     rect.top  = 1;
1021     rect.right = rect.bottom = -10;
1022     r = SendMessage(hList, LVM_GETSUBITEMRECT, -1, (LPARAM)&rect);
1023     ok(r != 0, "Expected not-null LRESULT\n");
1024
1025     hHeader = (HWND)SendMessage(hList, LVM_GETHEADER, 0, 0);
1026     ok(!IsWindow(hHeader), "Header shouldn't be created\n");
1027     ok(NULL == GetDlgItem(hList, 0), "NULL dialog item expected\n");
1028
1029     DestroyWindow(hList);
1030 }
1031
1032 static void test_redraw(void)
1033 {
1034     HWND hwnd, hwndheader;
1035
1036     hwnd = create_listview_control(0);
1037     hwndheader = subclass_header(hwnd);
1038
1039     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1040
1041     trace("invalidate & update\n");
1042     InvalidateRect(hwnd, NULL, TRUE);
1043     UpdateWindow(hwnd);
1044     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, redraw_listview_seq, "redraw listview", FALSE);
1045
1046     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1047
1048     DestroyWindow(hwnd);
1049 }
1050
1051 static LRESULT WINAPI cd_wndproc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
1052 {
1053     COLORREF clr, c0ffee = RGB(0xc0, 0xff, 0xee);
1054
1055     if(msg == WM_NOTIFY) {
1056         NMHDR *nmhdr = (PVOID)lp;
1057         if(nmhdr->code == NM_CUSTOMDRAW) {
1058             NMLVCUSTOMDRAW *nmlvcd = (PVOID)nmhdr;
1059             trace("NMCUSTOMDRAW (0x%.8x)\n", nmlvcd->nmcd.dwDrawStage);
1060             switch(nmlvcd->nmcd.dwDrawStage) {
1061             case CDDS_PREPAINT:
1062                 SetBkColor(nmlvcd->nmcd.hdc, c0ffee);
1063                 return CDRF_NOTIFYITEMDRAW;
1064             case CDDS_ITEMPREPAINT:
1065                 nmlvcd->clrTextBk = CLR_DEFAULT;
1066                 return CDRF_NOTIFYSUBITEMDRAW;
1067             case CDDS_ITEMPREPAINT | CDDS_SUBITEM:
1068                 clr = GetBkColor(nmlvcd->nmcd.hdc);
1069                 todo_wine ok(clr == c0ffee, "clr=%.8x\n", clr);
1070                 return CDRF_NOTIFYPOSTPAINT;
1071             case CDDS_ITEMPOSTPAINT | CDDS_SUBITEM:
1072                 clr = GetBkColor(nmlvcd->nmcd.hdc);
1073                 todo_wine ok(clr == c0ffee, "clr=%.8x\n", clr);
1074                 return CDRF_DODEFAULT;
1075             }
1076             return CDRF_DODEFAULT;
1077         }
1078     }
1079
1080     return DefWindowProcA(hwnd, msg, wp, lp);
1081 }
1082
1083 static void test_customdraw(void)
1084 {
1085     HWND hwnd;
1086     WNDPROC oldwndproc;
1087
1088     hwnd = create_listview_control(0);
1089
1090     insert_column(hwnd, 0);
1091     insert_column(hwnd, 1);
1092     insert_item(hwnd, 0);
1093
1094     oldwndproc = (WNDPROC)SetWindowLongPtr(hwndparent, GWLP_WNDPROC,
1095                                            (LONG_PTR)cd_wndproc);
1096
1097     InvalidateRect(hwnd, NULL, TRUE);
1098     UpdateWindow(hwnd);
1099
1100     SetWindowLongPtr(hwndparent, GWLP_WNDPROC, (LONG_PTR)oldwndproc);
1101
1102     DestroyWindow(hwnd);
1103 }
1104
1105 static void test_icon_spacing(void)
1106 {
1107     /* LVM_SETICONSPACING */
1108     /* note: LVM_SETICONSPACING returns the previous icon spacing if successful */
1109
1110     HWND hwnd;
1111     WORD w, h;
1112     DWORD r;
1113
1114     hwnd = create_custom_listview_control(LVS_ICON);
1115     ok(hwnd != NULL, "failed to create a listview window\n");
1116
1117     r = SendMessage(hwnd, WM_NOTIFYFORMAT, (WPARAM)hwndparent, (LPARAM)NF_REQUERY);
1118     expect(NFR_ANSI, r);
1119
1120     /* reset the icon spacing to defaults */
1121     SendMessage(hwnd, LVM_SETICONSPACING, 0, MAKELPARAM(-1, -1));
1122
1123     /* now we can request what the defaults are */
1124     r = SendMessage(hwnd, LVM_SETICONSPACING, 0, MAKELPARAM(-1, -1));
1125     w = LOWORD(r);
1126     h = HIWORD(r);
1127
1128     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1129
1130     trace("test icon spacing\n");
1131
1132     r = SendMessage(hwnd, LVM_SETICONSPACING, 0, MAKELPARAM(20, 30));
1133     ok(r == MAKELONG(w, h) ||
1134        broken(r == MAKELONG(w, w)), /* win98 */
1135        "Expected %d, got %d\n", MAKELONG(w, h), r);
1136
1137     r = SendMessage(hwnd, LVM_SETICONSPACING, 0, MAKELPARAM(25, 35));
1138     expect(MAKELONG(20,30), r);
1139
1140     r = SendMessage(hwnd, LVM_SETICONSPACING, 0, MAKELPARAM(-1,-1));
1141     expect(MAKELONG(25,35), r);
1142
1143     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_icon_spacing_seq, "test icon spacing seq", FALSE);
1144
1145     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1146     DestroyWindow(hwnd);
1147 }
1148
1149 static void test_color(void)
1150 {
1151     /* SETBKCOLOR/GETBKCOLOR, SETTEXTCOLOR/GETTEXTCOLOR, SETTEXTBKCOLOR/GETTEXTBKCOLOR */
1152
1153     HWND hwnd;
1154     DWORD r;
1155     int i;
1156
1157     COLORREF color;
1158     COLORREF colors[4] = {RGB(0,0,0), RGB(100,50,200), CLR_NONE, RGB(255,255,255)};
1159
1160     hwnd = create_listview_control(0);
1161     ok(hwnd != NULL, "failed to create a listview window\n");
1162
1163     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1164
1165     trace("test color seq\n");
1166     for (i = 0; i < 4; i++)
1167     {
1168         color = colors[i];
1169
1170         r = SendMessage(hwnd, LVM_SETBKCOLOR, 0, color);
1171         expect(TRUE, r);
1172         r = SendMessage(hwnd, LVM_GETBKCOLOR, 0, color);
1173         expect(color, r);
1174
1175         r = SendMessage(hwnd, LVM_SETTEXTCOLOR, 0, color);
1176         expect (TRUE, r);
1177         r = SendMessage(hwnd, LVM_GETTEXTCOLOR, 0, color);
1178         expect(color, r);
1179
1180         r = SendMessage(hwnd, LVM_SETTEXTBKCOLOR, 0, color);
1181         expect(TRUE, r);
1182         r = SendMessage(hwnd, LVM_GETTEXTBKCOLOR, 0, color);
1183         expect(color, r);
1184     }
1185
1186     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_color_seq, "test color seq", FALSE);
1187
1188     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1189     DestroyWindow(hwnd);
1190 }
1191
1192 static void test_item_count(void)
1193 {
1194     /* LVM_INSERTITEM, LVM_DELETEITEM, LVM_DELETEALLITEMS, LVM_GETITEMCOUNT */
1195
1196     HWND hwnd;
1197     DWORD r;
1198
1199     LVITEM item0;
1200     LVITEM item1;
1201     LVITEM item2;
1202     static CHAR item0text[] = "item0";
1203     static CHAR item1text[] = "item1";
1204     static CHAR item2text[] = "item2";
1205
1206     hwnd = create_listview_control(0);
1207     ok(hwnd != NULL, "failed to create a listview window\n");
1208
1209     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1210
1211     trace("test item count\n");
1212
1213     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1214     expect(0, r);
1215
1216     /* [item0] */
1217     item0.mask = LVIF_TEXT;
1218     item0.iItem = 0;
1219     item0.iSubItem = 0;
1220     item0.pszText = item0text;
1221     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item0);
1222     expect(0, r);
1223
1224     /* [item0, item1] */
1225     item1.mask = LVIF_TEXT;
1226     item1.iItem = 1;
1227     item1.iSubItem = 0;
1228     item1.pszText = item1text;
1229     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item1);
1230     expect(1, r);
1231
1232     /* [item0, item1, item2] */
1233     item2.mask = LVIF_TEXT;
1234     item2.iItem = 2;
1235     item2.iSubItem = 0;
1236     item2.pszText = item2text;
1237     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item2);
1238     expect(2, r);
1239
1240     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1241     expect(3, r);
1242
1243     /* [item0, item1] */
1244     r = SendMessage(hwnd, LVM_DELETEITEM, 2, 0);
1245     expect(TRUE, r);
1246
1247     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1248     expect(2, r);
1249
1250     /* [] */
1251     r = SendMessage(hwnd, LVM_DELETEALLITEMS, 0, 0);
1252     expect(TRUE, r);
1253
1254     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1255     expect(0, r);
1256
1257     /* [item0] */
1258     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item1);
1259     expect(0, r);
1260
1261     /* [item0, item1] */
1262     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item1);
1263     expect(1, r);
1264
1265     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1266     expect(2, r);
1267
1268     /* [item0, item1, item2] */
1269     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item2);
1270     expect(2, r);
1271
1272     r = SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1273     expect(3, r);
1274
1275     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_item_count_seq, "test item count seq", FALSE);
1276
1277     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1278     DestroyWindow(hwnd);
1279 }
1280
1281 static void test_item_position(void)
1282 {
1283     /* LVM_SETITEMPOSITION/LVM_GETITEMPOSITION */
1284
1285     HWND hwnd;
1286     DWORD r;
1287     POINT position;
1288
1289     LVITEM item0;
1290     LVITEM item1;
1291     LVITEM item2;
1292     static CHAR item0text[] = "item0";
1293     static CHAR item1text[] = "item1";
1294     static CHAR item2text[] = "item2";
1295
1296     hwnd = create_custom_listview_control(LVS_ICON);
1297     ok(hwnd != NULL, "failed to create a listview window\n");
1298
1299     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1300
1301     trace("test item position\n");
1302
1303     /* [item0] */
1304     item0.mask = LVIF_TEXT;
1305     item0.iItem = 0;
1306     item0.iSubItem = 0;
1307     item0.pszText = item0text;
1308     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item0);
1309     expect(0, r);
1310
1311     /* [item0, item1] */
1312     item1.mask = LVIF_TEXT;
1313     item1.iItem = 1;
1314     item1.iSubItem = 0;
1315     item1.pszText = item1text;
1316     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item1);
1317     expect(1, r);
1318
1319     /* [item0, item1, item2] */
1320     item2.mask = LVIF_TEXT;
1321     item2.iItem = 2;
1322     item2.iSubItem = 0;
1323     item2.pszText = item2text;
1324     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item2);
1325     expect(2, r);
1326
1327     r = SendMessage(hwnd, LVM_SETITEMPOSITION, 1, MAKELPARAM(10,5));
1328     expect(TRUE, r);
1329     r = SendMessage(hwnd, LVM_GETITEMPOSITION, 1, (LPARAM) &position);
1330     expect(TRUE, r);
1331     expect2(10, 5, position.x, position.y);
1332
1333     r = SendMessage(hwnd, LVM_SETITEMPOSITION, 2, MAKELPARAM(0,0));
1334     expect(TRUE, r);
1335     r = SendMessage(hwnd, LVM_GETITEMPOSITION, 2, (LPARAM) &position);
1336     expect(TRUE, r);
1337     expect2(0, 0, position.x, position.y);
1338
1339     r = SendMessage(hwnd, LVM_SETITEMPOSITION, 0, MAKELPARAM(20,20));
1340     expect(TRUE, r);
1341     r = SendMessage(hwnd, LVM_GETITEMPOSITION, 0, (LPARAM) &position);
1342     expect(TRUE, r);
1343     expect2(20, 20, position.x, position.y);
1344
1345     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_itempos_seq, "test item position seq", TRUE);
1346
1347     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1348     DestroyWindow(hwnd);
1349 }
1350
1351 static void test_getorigin(void)
1352 {
1353     /* LVM_GETORIGIN */
1354
1355     HWND hwnd;
1356     DWORD r;
1357     POINT position;
1358
1359     position.x = position.y = 0;
1360
1361     hwnd = create_custom_listview_control(LVS_ICON);
1362     ok(hwnd != NULL, "failed to create a listview window\n");
1363     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1364     trace("test get origin results\n");
1365     r = SendMessage(hwnd, LVM_GETORIGIN, 0, (LPARAM)&position);
1366     expect(TRUE, r);
1367     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1368     DestroyWindow(hwnd);
1369
1370     hwnd = create_custom_listview_control(LVS_SMALLICON);
1371     ok(hwnd != NULL, "failed to create a listview window\n");
1372     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1373     trace("test get origin results\n");
1374     r = SendMessage(hwnd, LVM_GETORIGIN, 0, (LPARAM)&position);
1375     expect(TRUE, r);
1376     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1377     DestroyWindow(hwnd);
1378
1379     hwnd = create_custom_listview_control(LVS_LIST);
1380     ok(hwnd != NULL, "failed to create a listview window\n");
1381     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1382     trace("test get origin results\n");
1383     r = SendMessage(hwnd, LVM_GETORIGIN, 0, (LPARAM)&position);
1384     expect(FALSE, r);
1385     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1386     DestroyWindow(hwnd);
1387
1388     hwnd = create_custom_listview_control(LVS_REPORT);
1389     ok(hwnd != NULL, "failed to create a listview window\n");
1390     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1391     trace("test get origin results\n");
1392     r = SendMessage(hwnd, LVM_GETORIGIN, 0, (LPARAM)&position);
1393     expect(FALSE, r);
1394     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1395     DestroyWindow(hwnd);
1396
1397 }
1398
1399 static void test_multiselect(void)
1400 {
1401     typedef struct t_select_task
1402     {
1403         const char *descr;
1404         int initPos;
1405         int loopVK;
1406         int count;
1407         int result;
1408     } select_task;
1409
1410     HWND hwnd;
1411     DWORD r;
1412     int i,j,item_count,selected_count;
1413     static const int items=5;
1414     BYTE kstate[256];
1415     select_task task;
1416     LONG_PTR style;
1417
1418     static struct t_select_task task_list[] = {
1419         { "using VK_DOWN", 0, VK_DOWN, -1, -1 },
1420         { "using VK_UP", -1, VK_UP, -1, -1 },
1421         { "using VK_END", 0, VK_END, 1, -1 },
1422         { "using VK_HOME", -1, VK_HOME, 1, -1 }
1423     };
1424
1425
1426     hwnd = create_listview_control(0);
1427
1428     for (i=0;i<items;i++) {
1429             insert_item(hwnd, 0);
1430     }
1431
1432     item_count = (int)SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1433
1434     expect(items,item_count);
1435
1436     for (i=0;i<4;i++) {
1437         task = task_list[i];
1438
1439         /* deselect all items */
1440         ListView_SetItemState(hwnd, -1, 0, LVIS_SELECTED);
1441         SendMessage(hwnd, LVM_SETSELECTIONMARK, 0, -1);
1442
1443         /* set initial position */
1444         SendMessage(hwnd, LVM_SETSELECTIONMARK, 0, (task.initPos == -1 ? item_count -1 : task.initPos));
1445         ListView_SetItemState(hwnd,(task.initPos == -1 ? item_count -1 : task.initPos),LVIS_SELECTED ,LVIS_SELECTED);
1446
1447         selected_count = (int)SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
1448
1449         ok(selected_count == 1, "There should be only one selected item at the beginning (is %d)\n",selected_count);
1450
1451         /* Set SHIFT key pressed */
1452         GetKeyboardState(kstate);
1453         kstate[VK_SHIFT]=0x80;
1454         SetKeyboardState(kstate);
1455
1456         for (j=1;j<=(task.count == -1 ? item_count : task.count);j++) {
1457             r = SendMessage(hwnd, WM_KEYDOWN, task.loopVK, 0);
1458             expect(0,r);
1459             r = SendMessage(hwnd, WM_KEYUP, task.loopVK, 0);
1460             expect(0,r);
1461         }
1462
1463         selected_count = (int)SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
1464
1465         ok((task.result == -1 ? item_count : task.result) == selected_count, "Failed multiple selection %s. There should be %d selected items (is %d)\n", task.descr, item_count, selected_count);
1466
1467         /* Set SHIFT key released */
1468         GetKeyboardState(kstate);
1469         kstate[VK_SHIFT]=0x00;
1470         SetKeyboardState(kstate);
1471     }
1472     DestroyWindow(hwnd);
1473
1474     /* make multiple selection, then switch to LVS_SINGLESEL */
1475     hwnd = create_listview_control(0);
1476     for (i=0;i<items;i++) {
1477             insert_item(hwnd, 0);
1478     }
1479     item_count = (int)SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
1480     expect(items,item_count);
1481     /* deselect all items */
1482     ListView_SetItemState(hwnd, -1, 0, LVIS_SELECTED);
1483     SendMessage(hwnd, LVM_SETSELECTIONMARK, 0, -1);
1484     for (i=0;i<3;i++) {
1485         ListView_SetItemState(hwnd, i, LVIS_SELECTED, LVIS_SELECTED);
1486     }
1487
1488     r = SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
1489     expect(3, r);
1490     r = SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
1491 todo_wine
1492     expect(-1, r);
1493
1494     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
1495     ok(!(style & LVS_SINGLESEL), "LVS_SINGLESEL isn't expected\n");
1496     SetWindowLongPtrA(hwnd, GWL_STYLE, style | LVS_SINGLESEL);
1497     /* check that style is accepted */
1498     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
1499     ok(style & LVS_SINGLESEL, "LVS_SINGLESEL expected\n");
1500
1501     for (i=0;i<3;i++) {
1502         r = ListView_GetItemState(hwnd, i, LVIS_SELECTED);
1503         ok(r & LVIS_SELECTED, "Expected item %d to be selected\n", i);
1504     }
1505     r = SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
1506     expect(3, r);
1507     SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
1508     expect(3, r);
1509
1510     /* select one more */
1511     ListView_SetItemState(hwnd, 3, LVIS_SELECTED, LVIS_SELECTED);
1512
1513     for (i=0;i<3;i++) {
1514         r = ListView_GetItemState(hwnd, i, LVIS_SELECTED);
1515         ok(!(r & LVIS_SELECTED), "Expected item %d to be unselected\n", i);
1516     }
1517     r = ListView_GetItemState(hwnd, 3, LVIS_SELECTED);
1518     ok(r & LVIS_SELECTED, "Expected item %d to be selected\n", i);
1519
1520     r = SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
1521     expect(1, r);
1522     r = SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
1523 todo_wine
1524     expect(-1, r);
1525
1526     DestroyWindow(hwnd);
1527 }
1528
1529 static void test_subitem_rect(void)
1530 {
1531     HWND hwnd;
1532     DWORD r;
1533     LVCOLUMN col;
1534     RECT rect;
1535
1536     /* test LVM_GETSUBITEMRECT for header */
1537     hwnd = create_listview_control(0);
1538     ok(hwnd != NULL, "failed to create a listview window\n");
1539     /* add some columns */
1540     memset(&col, 0, sizeof(LVCOLUMN));
1541     col.mask = LVCF_WIDTH;
1542     col.cx = 100;
1543     r = -1;
1544     r = SendMessage(hwnd, LVM_INSERTCOLUMN, 0, (LPARAM)&col);
1545     expect(0, r);
1546     col.cx = 150;
1547     r = -1;
1548     r = SendMessage(hwnd, LVM_INSERTCOLUMN, 1, (LPARAM)&col);
1549     expect(1, r);
1550     col.cx = 200;
1551     r = -1;
1552     r = SendMessage(hwnd, LVM_INSERTCOLUMN, 2, (LPARAM)&col);
1553     expect(2, r);
1554     /* item = -1 means header, subitem index is 1 based */
1555     rect.left = LVIR_BOUNDS;
1556     rect.top  = 0;
1557     rect.right = rect.bottom = 0;
1558     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, -1, (LPARAM)&rect);
1559     expect(0, r);
1560
1561     rect.left = LVIR_BOUNDS;
1562     rect.top  = 1;
1563     rect.right = rect.bottom = 0;
1564     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, -1, (LPARAM)&rect);
1565
1566     ok(r != 0, "Expected not-null LRESULT\n");
1567     expect(100, rect.left);
1568     expect(250, rect.right);
1569 todo_wine
1570     expect(3, rect.top);
1571
1572     rect.left = LVIR_BOUNDS;
1573     rect.top  = 2;
1574     rect.right = rect.bottom = 0;
1575     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, -1, (LPARAM)&rect);
1576
1577     ok(r != 0, "Expected not-null LRESULT\n");
1578     expect(250, rect.left);
1579     expect(450, rect.right);
1580 todo_wine
1581     expect(3, rect.top);
1582
1583     DestroyWindow(hwnd);
1584
1585     /* try it for non LVS_REPORT style */
1586     hwnd = CreateWindow("SysListView32", "Test", LVS_ICON, 0, 0, 100, 100, NULL, NULL,
1587                          GetModuleHandle(NULL), 0);
1588     rect.left = LVIR_BOUNDS;
1589     rect.top  = 1;
1590     rect.right = rect.bottom = -10;
1591     r = SendMessage(hwnd, LVM_GETSUBITEMRECT, -1, (LPARAM)&rect);
1592     ok(r == 0, "Expected not-null LRESULT\n");
1593     /* rect is unchanged */
1594     expect(0, rect.left);
1595     expect(-10, rect.right);
1596     expect(1, rect.top);
1597     expect(-10, rect.bottom);
1598     DestroyWindow(hwnd);
1599 }
1600
1601 /* comparison callback for test_sorting */
1602 static INT WINAPI test_CallBackCompare(LPARAM first, LPARAM second, LPARAM lParam)
1603 {
1604     if (first == second) return 0;
1605     return (first > second ? 1 : -1);
1606 }
1607
1608 static void test_sorting(void)
1609 {
1610     HWND hwnd;
1611     LVITEMA item = {0};
1612     DWORD r;
1613     LONG_PTR style;
1614     static CHAR names[][4] = {"A", "B", "C", "D"};
1615     CHAR buff[10];
1616
1617     hwnd = create_listview_control(0);
1618     ok(hwnd != NULL, "failed to create a listview window\n");
1619
1620     /* insert some items */
1621     item.mask = LVIF_PARAM | LVIF_STATE;
1622     item.state = LVIS_SELECTED;
1623     item.iItem = 0;
1624     item.iSubItem = 0;
1625     item.lParam = 3;
1626     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
1627     expect(0, r);
1628
1629     item.mask = LVIF_PARAM;
1630     item.iItem = 1;
1631     item.iSubItem = 0;
1632     item.lParam = 2;
1633     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
1634     expect(1, r);
1635
1636     item.mask = LVIF_STATE | LVIF_PARAM;
1637     item.state = LVIS_SELECTED;
1638     item.iItem = 2;
1639     item.iSubItem = 0;
1640     item.lParam = 4;
1641     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
1642     expect(2, r);
1643
1644     r = SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
1645     expect(-1, r);
1646
1647     r = SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
1648     expect(2, r);
1649
1650     r = SendMessage(hwnd, LVM_SORTITEMS, 0, (LPARAM)test_CallBackCompare);
1651     expect(TRUE, r);
1652
1653     r = SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
1654     expect(2, r);
1655     r = SendMessage(hwnd, LVM_GETSELECTIONMARK, 0, 0);
1656     expect(-1, r);
1657     r = SendMessage(hwnd, LVM_GETITEMSTATE, 0, LVIS_SELECTED);
1658     expect(0, r);
1659     r = SendMessage(hwnd, LVM_GETITEMSTATE, 1, LVIS_SELECTED);
1660     expect(LVIS_SELECTED, r);
1661     r = SendMessage(hwnd, LVM_GETITEMSTATE, 2, LVIS_SELECTED);
1662     expect(LVIS_SELECTED, r);
1663
1664     DestroyWindow(hwnd);
1665
1666     /* switch to LVS_SORTASCENDING when some items added */
1667     hwnd = create_listview_control(0);
1668     ok(hwnd != NULL, "failed to create a listview window\n");
1669
1670     item.mask = LVIF_TEXT;
1671     item.iItem = 0;
1672     item.iSubItem = 0;
1673     item.pszText = names[1];
1674     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
1675     expect(0, r);
1676
1677     item.mask = LVIF_TEXT;
1678     item.iItem = 1;
1679     item.iSubItem = 0;
1680     item.pszText = names[2];
1681     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
1682     expect(1, r);
1683
1684     item.mask = LVIF_TEXT;
1685     item.iItem = 2;
1686     item.iSubItem = 0;
1687     item.pszText = names[0];
1688     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
1689     expect(2, r);
1690
1691     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
1692     SetWindowLongPtrA(hwnd, GWL_STYLE, style | LVS_SORTASCENDING);
1693     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
1694     ok(style & LVS_SORTASCENDING, "Expected LVS_SORTASCENDING to be set\n");
1695
1696     /* no sorting performed when switched to LVS_SORTASCENDING */
1697     item.mask = LVIF_TEXT;
1698     item.iItem = 0;
1699     item.pszText = buff;
1700     item.cchTextMax = sizeof(buff);
1701     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
1702     expect(TRUE, r);
1703     ok(lstrcmp(buff, names[1]) == 0, "Expected '%s', got '%s'\n", names[1], buff);
1704
1705     item.iItem = 1;
1706     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
1707     expect(TRUE, r);
1708     ok(lstrcmp(buff, names[2]) == 0, "Expected '%s', got '%s'\n", names[2], buff);
1709
1710     item.iItem = 2;
1711     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
1712     expect(TRUE, r);
1713     ok(lstrcmp(buff, names[0]) == 0, "Expected '%s', got '%s'\n", names[0], buff);
1714
1715     /* adding new item doesn't resort list */
1716     item.mask = LVIF_TEXT;
1717     item.iItem = 3;
1718     item.iSubItem = 0;
1719     item.pszText = names[3];
1720     r = SendMessage(hwnd, LVM_INSERTITEM, 0, (LPARAM) &item);
1721     expect(3, r);
1722
1723     item.mask = LVIF_TEXT;
1724     item.iItem = 0;
1725     item.pszText = buff;
1726     item.cchTextMax = sizeof(buff);
1727     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
1728     expect(TRUE, r);
1729     todo_wine ok(lstrcmp(buff, names[1]) == 0, "Expected '%s', got '%s'\n", names[1], buff);
1730
1731     item.iItem = 1;
1732     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
1733     expect(TRUE, r);
1734     todo_wine ok(lstrcmp(buff, names[2]) == 0, "Expected '%s', got '%s'\n", names[2], buff);
1735
1736     item.iItem = 2;
1737     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
1738     expect(TRUE, r);
1739     todo_wine ok(lstrcmp(buff, names[0]) == 0, "Expected '%s', got '%s'\n", names[0], buff);
1740
1741     item.iItem = 3;
1742     r = SendMessage(hwnd, LVM_GETITEM, 0, (LPARAM) &item);
1743     expect(TRUE, r);
1744     ok(lstrcmp(buff, names[3]) == 0, "Expected '%s', got '%s'\n", names[3], buff);
1745
1746     DestroyWindow(hwnd);
1747 }
1748
1749 static void test_ownerdata(void)
1750 {
1751     HWND hwnd;
1752     LONG_PTR style, ret;
1753     DWORD res;
1754     LVITEMA item;
1755
1756     /* it isn't possible to set LVS_OWNERDATA after creation */
1757     hwnd = create_listview_control(0);
1758     ok(hwnd != NULL, "failed to create a listview window\n");
1759     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
1760     ok(!(style & LVS_OWNERDATA) && style, "LVS_OWNERDATA isn't expected\n");
1761
1762     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1763
1764     ret = SetWindowLongPtrA(hwnd, GWL_STYLE, style | LVS_OWNERDATA);
1765     ok(ret == style, "Expected set GWL_STYLE to succeed\n");
1766     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_ownerdata_switchto_seq,
1767                 "try to switch to LVS_OWNERDATA seq", FALSE);
1768
1769     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
1770     ok(!(style & LVS_OWNERDATA), "LVS_OWNERDATA isn't expected\n");
1771     DestroyWindow(hwnd);
1772
1773     /* try to set LVS_OWNERDATA after creation just having it */
1774     hwnd = create_listview_control(LVS_OWNERDATA);
1775     ok(hwnd != NULL, "failed to create a listview window\n");
1776     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
1777     ok(style & LVS_OWNERDATA, "LVS_OWNERDATA is expected\n");
1778
1779     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1780
1781     ret = SetWindowLongPtrA(hwnd, GWL_STYLE, style | LVS_OWNERDATA);
1782     ok(ret == style, "Expected set GWL_STYLE to succeed\n");
1783     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_ownerdata_switchto_seq,
1784                 "try to switch to LVS_OWNERDATA seq", FALSE);
1785     DestroyWindow(hwnd);
1786
1787     /* try to remove LVS_OWNERDATA after creation just having it */
1788     hwnd = create_listview_control(LVS_OWNERDATA);
1789     ok(hwnd != NULL, "failed to create a listview window\n");
1790     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
1791     ok(style & LVS_OWNERDATA, "LVS_OWNERDATA is expected\n");
1792
1793     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1794
1795     ret = SetWindowLongPtrA(hwnd, GWL_STYLE, style & ~LVS_OWNERDATA);
1796     ok(ret == style, "Expected set GWL_STYLE to succeed\n");
1797     ok_sequence(sequences, LISTVIEW_SEQ_INDEX, listview_ownerdata_switchto_seq,
1798                 "try to switch to LVS_OWNERDATA seq", FALSE);
1799     style = GetWindowLongPtrA(hwnd, GWL_STYLE);
1800     ok(style & LVS_OWNERDATA, "LVS_OWNERDATA is expected\n");
1801     DestroyWindow(hwnd);
1802
1803     /* try select an item */
1804     hwnd = create_listview_control(LVS_OWNERDATA);
1805     ok(hwnd != NULL, "failed to create a listview window\n");
1806     res = SendMessageA(hwnd, LVM_SETITEMCOUNT, 1, 0);
1807     ok(res != 0, "Expected LVM_SETITEMCOUNT to succeed\n");
1808     res = SendMessageA(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
1809     expect(0, res);
1810     memset(&item, 0, sizeof(item));
1811     item.stateMask = LVIS_SELECTED;
1812     item.state     = LVIS_SELECTED;
1813     res = SendMessageA(hwnd, LVM_SETITEMSTATE, 0, (LPARAM)&item);
1814     expect(TRUE, res);
1815     res = SendMessageA(hwnd, LVM_GETSELECTEDCOUNT, 0, 0);
1816     expect(1, res);
1817     res = SendMessageA(hwnd, LVM_GETITEMCOUNT, 0, 0);
1818     expect(1, res);
1819     DestroyWindow(hwnd);
1820
1821     /* LVM_SETITEM is unsupported on LVS_OWNERDATA */
1822     hwnd = create_listview_control(LVS_OWNERDATA);
1823     ok(hwnd != NULL, "failed to create a listview window\n");
1824     res = SendMessageA(hwnd, LVM_SETITEMCOUNT, 1, 0);
1825     ok(res != 0, "Expected LVM_SETITEMCOUNT to succeed\n");
1826     res = SendMessageA(hwnd, LVM_GETITEMCOUNT, 0, 0);
1827     expect(1, res);
1828     memset(&item, 0, sizeof(item));
1829     item.mask = LVIF_STATE;
1830     item.iItem = 0;
1831     item.stateMask = LVIS_SELECTED;
1832     item.state     = LVIS_SELECTED;
1833     res = SendMessageA(hwnd, LVM_SETITEM, 0, (LPARAM)&item);
1834     expect(FALSE, res);
1835     DestroyWindow(hwnd);
1836 }
1837
1838 START_TEST(listview)
1839 {
1840     HMODULE hComctl32;
1841     BOOL (WINAPI *pInitCommonControlsEx)(const INITCOMMONCONTROLSEX*);
1842
1843     hComctl32 = GetModuleHandleA("comctl32.dll");
1844     pInitCommonControlsEx = (void*)GetProcAddress(hComctl32, "InitCommonControlsEx");
1845     if (pInitCommonControlsEx)
1846     {
1847         INITCOMMONCONTROLSEX iccex;
1848         iccex.dwSize = sizeof(iccex);
1849         iccex.dwICC  = ICC_LISTVIEW_CLASSES;
1850         pInitCommonControlsEx(&iccex);
1851     }
1852     else
1853         InitCommonControls();
1854
1855     init_msg_sequences(sequences, NUM_MSG_SEQUENCES);
1856
1857     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1858     hwndparent = create_parent_window();
1859     ok_sequence(sequences, PARENT_SEQ_INDEX, create_parent_wnd_seq, "create parent window", TRUE);
1860     flush_sequences(sequences, NUM_MSG_SEQUENCES);
1861
1862     test_images();
1863     test_checkboxes();
1864     test_items();
1865     test_create();
1866     test_redraw();
1867     test_customdraw();
1868     test_icon_spacing();
1869     test_color();
1870     test_item_count();
1871     test_item_position();
1872     test_columns();
1873     test_getorigin();
1874     test_multiselect();
1875     test_subitem_rect();
1876     test_sorting();
1877     test_ownerdata();
1878 }